File: mpd.c

package info (click to toggle)
gap-float 0.9.1%2Bds-6
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 612 kB
  • sloc: ansic: 2,537; cpp: 1,998; xml: 184; makefile: 103; sh: 1
file content (433 lines) | stat: -rw-r--r-- 11,538 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
/****************************************************************************
**
*W  mpd.c                       GAP source                  Laurent Bartholdi
**
*Y  Copyright (C) 2008 Laurent Bartholdi
**
**  This file contains the functions for the float package.
**  complex floats are implemented using the MPD package.
*/

#define BANNER_FLOAT_H

#include <string.h>
#include <malloc.h>
#include <stdio.h>
#include <gmp.h>

#include "src/compiled.h"
#include "mp_float.h"

/****************************************************************
 * mpd's are stored as follows:
 * +----------+-----------------------------------------+---------------------+
 * | TYPE_MPD |               __mpd_struct              |    __mp_limb_t[]    |
 * |          | __mpfr_struct real          imag        | limbr ... limbi ... |
 * |          | prec exp sign mant   prec exp sign mant |                     |
 * +----------+-----------------------------------------+---------------------+
 *                               \___________________\____^         ^
 *                                                    \____________/
 * it is assumed that the real and imaginary mpfr's are allocated with the
 * same precision
 ****************************************************************/
#define MPD_OBJ(obj) ((mpd_ptr) (ADDR_OBJ(obj)+1))
#define REMANTISSA_MPD(p) ((mp_limb_t *) (p+1))
#define IMMANTISSA_MPD(p) (REMANTISSA_MPD(p)+(mpd_get_prec(p)+GMP_NUMB_BITS-1)/GMP_NUMB_BITS)

static inline mpd_ptr GET_MPD(Obj obj) {
  mpd_ptr p = MPD_OBJ(obj);
  mpfr_custom_move (p->re, REMANTISSA_MPD(p));
  mpfr_custom_move (p->im, IMMANTISSA_MPD(p));
  return p;
}

/****************************************************************************
**
*F  allocate new object, initialize to NaN
**
*/
Obj TYPE_MPD;

static inline Obj NEW_MPD( mp_prec_t prec )
{
  Obj f = NEW_DATOBJ(sizeof(__mpd_struct)+2*mpfr_custom_get_size(prec), TYPE_MPD);
  mpd_ptr p = MPD_OBJ(f);
  mpfr_custom_init_set(p->re, MPFR_NAN_KIND, 0, prec, REMANTISSA_MPD(p));
  mpfr_custom_init_set(p->im, MPFR_NAN_KIND, 0, prec, IMMANTISSA_MPD(p));
  return f;
}

/****************************************************************************
**
*F Func1_MPD( <float> ) . . . . . . . . . . . . . . . .1-argument functions
**
*/
#define Func1_MPD(name,mpd_name)			\
  static Obj name(Obj self, Obj f)			\
  {						        \
    mp_prec_t prec = mpd_get_prec(MPD_OBJ(f));	        \
    Obj g = NEW_MPD(prec);				\
    mpd_name (MPD_OBJ(g), GET_MPD(f), MPD_RNDNN);	\
    return g;						\
  }
#define Func1_MPFRMPD(name,mpd_name)			\
  static Obj name(Obj self, Obj f)			\
  {						        \
    mp_prec_t prec = mpd_get_prec(MPD_OBJ(f));	        \
    Obj g = NEW_MPFR(prec);				\
    mpd_name (MPFR_OBJ(g), GET_MPD(f), MPD_RNDNN);	\
    return g;						\
  }
#define Inc1_MPD_arg(name,arg)		\
  { #name, 1, arg, name, "src/mpd.c:" #name }
#define Inc1_MPD(name) Inc1_MPD_arg(name,"complex")

Func1_MPD(SIN_MPD,mpd_sin);
Func1_MPD(EXP_MPD,mpd_exp);
Func1_MPD(CONJ_MPD,mpd_conj);
Func1_MPD(AINV_MPD,mpd_neg);
Func1_MPD(SQRT_MPD,mpd_sqrt);
Func1_MPD(SQR_MPD,mpd_sqr);

Func1_MPFRMPD(ABS_MPD,mpd_abs);
Func1_MPFRMPD(NORM_MPD,mpd_norm);

static Obj ZERO_MPD(Obj self, Obj f)
{
  mp_prec_t prec = mpd_get_prec(GET_MPD(f));
  Obj g = NEW_MPD(prec);
  mpd_set_ui(MPD_OBJ(g), 0, MPD_RNDNN);
  return g;
}

static Obj ONE_MPD(Obj self, Obj f)
{
  mp_prec_t prec = mpd_get_prec(GET_MPD(f));
  Obj g = NEW_MPD(prec);
  mpd_set_ui (MPD_OBJ(g), 1, MPD_RNDNN);
  return g;
}

static Obj MPD_MAKENAN(Obj self, Obj prec)
{
  TEST_IS_INTOBJ("MPD_MAKENAN",prec);

  Obj g = NEW_MPD(INT_INTOBJ(prec));
  mpfr_set_nan (MPD_OBJ(g)->re);
  mpfr_set_nan (MPD_OBJ(g)->im);
  return g;
}

static Obj MPD_MAKEINFINITY(Obj self, Obj prec)
{
  TEST_IS_INTOBJ("MPD_MAKEINFINITY",prec);

  int p = INT_INTOBJ(prec);
  Obj g = NEW_MPD(p < 0 ? -p : p);
  mpfr_set_inf (MPD_OBJ(g)->re, p);
  mpfr_set_inf (MPD_OBJ(g)->im, p);
  return g;
}

static Obj INV_MPD(Obj self, Obj f)
{
  Obj g = NEW_MPD(mpd_get_prec(GET_MPD(f)));
  mpd_ui_div (MPD_OBJ(g), 1, GET_MPD(f), MPD_RNDNN);
  return g;
}

static Obj REAL_MPD(Obj self, Obj f)
{
  mp_prec_t prec = mpd_get_prec(MPD_OBJ(f));
  Obj g = NEW_MPFR(prec);
  mpfr_set (MPFR_OBJ(g), GET_MPD(f)->re, GMP_RNDN);
  return g;
}

static Obj IMAG_MPD(Obj self, Obj f)
{
  mp_prec_t prec = mpd_get_prec(MPD_OBJ(f));
  Obj g = NEW_MPFR(prec);
  mpfr_set (MPFR_OBJ(g), GET_MPD(f)->im, GMP_RNDN);
  return g;
}

static Obj MPD_INT(Obj self, Obj i)
{
  Obj g;
  if (IS_INTOBJ(i)) {
    g = NEW_MPD(8*sizeof(long));
    mpd_set_si(MPD_OBJ(g), INT_INTOBJ(i), MPD_RNDNN);
  } else {
    Obj f = MPZ_LONGINT(i);
    g = NEW_MPD(8*sizeof(mp_limb_t)*SIZE_INT(i));
    
    mpfr_set_z(MPD_OBJ(g)->re, mpz_MPZ(f), GMP_RNDN);
    mpfr_set_ui(MPD_OBJ(g)->im, 0, GMP_RNDN);
  }
  return g;
}

static Obj MPD_INTPREC(Obj self, Obj i, Obj prec)
{
  Obj g;
  TEST_IS_INTOBJ("MPD_INTPREC",prec);

  if (IS_INTOBJ(i)) {
    g = NEW_MPD(INT_INTOBJ(prec));
    mpd_set_si(MPD_OBJ(g), INT_INTOBJ(i), MPD_RNDNN);
  } else {
    Obj f = MPZ_LONGINT(i);
    g = NEW_MPD(INT_INTOBJ(prec));
    
    mpfr_set_z(MPD_OBJ(g)->re, mpz_MPZ(f), GMP_RNDN);
    mpfr_set_ui(MPD_OBJ(g)->im, 0, GMP_RNDN);
  }
  return g;
}

static Obj PREC_MPD(Obj self, Obj f)
{
  return INTOBJ_INT(mpd_get_prec(GET_MPD(f)));
}

static Obj MPD_MPDPREC(Obj self, Obj f, Obj prec)
{
  TEST_IS_INTOBJ("MPD_MPDPREC",prec);

  Obj g = NEW_MPD(INT_INTOBJ(prec));
  mpd_set (MPD_OBJ(g), GET_MPD(f), MPD_RNDNN);
  return g;
}

static Obj STRING_MPD(Obj self, Obj f, Obj digits)
{
  mp_prec_t prec = mpd_get_prec(GET_MPD(f));
  Obj str = NEW_STRING(2*(prec*302/1000+10)+3);
  int slen = 0, n;

  TEST_IS_INTOBJ("STRING_MPD",digits);
  n = INT_INTOBJ(digits);
  if (n == 1) n = 2;

  char *c = CSTR_STRING(str);
  slen += PRINT_MPFR(c+slen, 0, n, GET_MPD(f)->re, GMP_RNDN);
  c[slen++] = '+';
  c[slen++] = 'I';
  c[slen++] = '*';
  slen += PRINT_MPFR(c+slen, 0, n, MPD_OBJ(f)->im, GMP_RNDN);
  SET_LEN_STRING(str, slen);
  SHRINK_STRING(str);

  return str;
}

static Obj VIEWSTRING_MPD(Obj self, Obj f, Obj digits)
{
  mp_prec_t prec = mpd_get_prec(GET_MPD(f));
  Obj str = NEW_STRING(2*(prec*302/1000+10)+3);
  int slen = 0, n;

  TEST_IS_INTOBJ("STRING_MPD",digits);
  n = INT_INTOBJ(digits);
  if (n == 1) n = 2;

  char *c = CSTR_STRING(str);
  slen += PRINT_MPFR(c+slen, 0, n, GET_MPD(f)->re, GMP_RNDN);
  Obj im = NEW_MPFR(prec);
  mpfr_add(MPFR_OBJ(im), GET_MPD(f)->re, GET_MPD(f)->im, GMP_RNDN);
  mpfr_sub(MPFR_OBJ(im), MPFR_OBJ(im), MPD_OBJ(f)->re, GMP_RNDN); /* round off small im */
  if (!mpfr_zero_p(MPFR_OBJ(im))) {
    if (mpfr_sgn(MPFR_OBJ(im)) < 0)
      c[slen++] = '-';
    else
      c[slen++] = '+';
    mpfr_abs (MPFR_OBJ(im), MPD_OBJ(f)->im, GMP_RNDN);
    c[slen++] = 'I';
    c[slen++] = '*';
    slen += PRINT_MPFR(c+slen, 0, n, MPFR_OBJ(im), GMP_RNDN);
  }
  SET_LEN_STRING(str, slen);
  SHRINK_STRING(str);

  return str;
}

static Obj MPD_STRING(Obj self, Obj s, Obj prec)
{
  while (!IsStringConv(s))
    {
      s = ErrorReturnObj("MPD_STRING: object to be converted must be a string, not a %s",
			 (Int)TNAM_OBJ(s),0,
			 "You can return a string to continue" );
    }
  TEST_IS_INTOBJ("MPD_STRING",prec);
  int n = INT_INTOBJ(prec);
  if (n == 0)
    n = GET_LEN_STRING(s)*1000 / 301;

  Obj g = NEW_MPD(INT_INTOBJ(prec));
  char *p = (char *) CHARS_STRING(s), *newp;
  int sign = 1;
  mpd_set_ui(MPD_OBJ(g), 0, MPD_RNDNN);
  mpfr_ptr f = MPD_OBJ(g)->re;
  Obj newg = NEW_MPFR(INT_INTOBJ(prec));

  for (;;) {
    printf("<%c>",*p);
    switch (*p) {
    case '-':
    case '+':
    case 0:
      if (!mpfr_nan_p(MPFR_OBJ(newg))) { /* drop the last read float */
	mpfr_add (f, f, MPFR_OBJ(newg), GMP_RNDN);
	mpfr_set_nan (MPFR_OBJ(newg));
	f = MPD_OBJ(g)->re;
	sign = 1;
      }
      if (!*p)
	return g;
      if (*p == '-')
	sign = -sign;
    case '*': p++; break;
    case 'i':
    case 'I': if (f == GET_MPD(g)->re) {
	f = MPD_OBJ(g)->im;
	if (mpfr_nan_p(MPFR_OBJ(newg)))
	  mpfr_set_si (MPFR_OBJ(newg), sign, GMP_RNDN); /* accept 'i' as '1*i' */
      } else return Fail;
      p++; break;
    default:
      mpfr_strtofr(MPFR_OBJ(newg), p, &newp, 10, GMP_RNDN);
      if (newp == p && f != GET_MPD(g)->im)
	return Fail; /* no valid characters read */
      if (sign == -1)
	mpfr_neg(MPFR_OBJ(newg), MPFR_OBJ(newg), GMP_RNDN);
      p = newp;
    }
  }
  return g;
}

static Obj MPD_MPFR(Obj self, Obj f)
{
  Obj g = NEW_MPD (mpfr_get_prec(GET_MPFR(f)));
  mpfr_set (MPD_OBJ(g)->re, GET_MPFR(f), GMP_RNDN);
  mpfr_set_ui (MPD_OBJ(g)->im, 0, GMP_RNDN);

  return g;
}

/****************************************************************************
**
*F Func2_MPD( <float>, <float> ) . . . . . . . . . . . 2-argument functions
**
*/
#define Func2_MPD(name,mpd_name)					\
  static Obj name(Obj self, Obj fl, Obj fr)				\
  {									\
    mp_prec_t precl = mpd_get_prec(GET_MPD(fl)),			\
      precr = mpd_get_prec(GET_MPD(fr));				\
									\
    Obj g = NEW_MPD(precl > precr ? precl : precr);			\
    mpd_name (MPD_OBJ(g), GET_MPD(fl), GET_MPD(fr), MPD_RNDNN);		\
    return g;								\
  }
#define Inc2_MPD_arg(name,arg)			\
  { #name, 2, arg, name, "src/mpd.c:" #name }
#define Inc2_MPD(name) Inc2_MPD_arg(name,"complex, complex")

Func2_MPD(SUM_MPD,mpd_add);
Func2_MPD(DIFF_MPD,mpd_sub);
Func2_MPD(PROD_MPD,mpd_mul);
Func2_MPD(QUO_MPD,mpd_div);

static Obj LQUO_MPD(Obj self, Obj fl, Obj fr)
{
  mp_prec_t precl = mpd_get_prec(GET_MPD(fl)),
    precr = mpd_get_prec(GET_MPD(fr));
  
  Obj g = NEW_MPD(precl > precr ? precl : precr);
  mpd_div (MPD_OBJ(g), GET_MPD(fr), GET_MPD(fl), MPD_RNDNN);
  return g;
}

static Obj EQ_MPD (Obj self, Obj fl, Obj fr)
{
  return mpd_cmp(GET_MPD(fr),GET_MPD(fl)) == 0 ? True : False;
}

static Obj LT_MPD (Obj self, Obj fl, Obj fr)
{
  return mpd_cmp(GET_MPD(fl),GET_MPD(fr)) < 0 ? True : False;
}

static Obj MPD_2MPFR (Obj self, Obj fl, Obj fr)
{
  mp_prec_t precl = mpfr_get_prec(GET_MPFR(fl)),
    precr = mpfr_get_prec(GET_MPFR(fr));
  
  Obj g = NEW_MPD(precl > precr ? precl : precr);
  mpfr_set (MPD_OBJ(g)->re, GET_MPFR(fl), GMP_RNDN);
  mpfr_set (MPD_OBJ(g)->im, GET_MPFR(fr), GMP_RNDN);

  return g;
}

/****************************************************************
 * export functions
 ****************************************************************/
static StructGVarFunc GVarFuncs [] = {  
  Inc1_MPD(AINV_MPD),
  Inc1_MPD(ABS_MPD),
  Inc1_MPD(INV_MPD),
  Inc1_MPD(SIN_MPD),
  Inc1_MPD(EXP_MPD),
  Inc1_MPD(SQRT_MPD),
  Inc1_MPD(SQR_MPD),
  Inc1_MPD(CONJ_MPD),

  Inc1_MPD(ZERO_MPD),
  Inc1_MPD(ONE_MPD),
  Inc1_MPD_arg(MPD_MAKENAN,"int"),
  Inc1_MPD_arg(MPD_MAKEINFINITY,"int"),
  Inc1_MPD_arg(MPD_INT,"int"),
  Inc1_MPD(PREC_MPD),
  Inc1_MPD(REAL_MPD),
  Inc1_MPD(IMAG_MPD),
  Inc1_MPD(MPD_MPFR),
  Inc1_MPD(NORM_MPD),
  
  Inc2_MPD(SUM_MPD),
  Inc2_MPD(DIFF_MPD),
  Inc2_MPD(PROD_MPD),
  Inc2_MPD(QUO_MPD),
  Inc2_MPD(LQUO_MPD),
  Inc2_MPD(EQ_MPD),
  Inc2_MPD(LT_MPD),
  Inc2_MPD_arg(MPD_STRING,"string, int"),
  Inc2_MPD_arg(STRING_MPD,"complex, int"),
  Inc2_MPD_arg(VIEWSTRING_MPD,"complex, int"),
  Inc2_MPD_arg(MPD_INTPREC,"int, int"),
  Inc2_MPD_arg(MPD_MPDPREC,"complex, int"),
  Inc2_MPD(MPD_2MPFR),

  {0}
};

int InitMPDKernel (void)
{
  ImportGVarFromLibrary ("TYPE_MPD", &TYPE_MPD);
  return 0;
}

int InitMPDLibrary (void)
{
  InitGVarFuncsFromTable (GVarFuncs);
  return 0;
}

/****************************************************************************
**
*E  mpd.c  . . . . . . . . . . . . . . . . . . . . . . . . . . . . .ends here
*/