-
Notifications
You must be signed in to change notification settings - Fork 2
/
mlgmp_fr.c
483 lines (419 loc) · 10.1 KB
/
mlgmp_fr.c
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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
#include <caml/mlvalues.h>
#include <caml/custom.h>
#include <caml/alloc.h>
#include <caml/memory.h>
#include <caml/fail.h>
#include <caml/callback.h>
#include <stdio.h>
#include <string.h>
#include "config.h"
#include "mlgmp.h"
#include "conversions.c"
#define MODULE "Gmp.FR."
#define unimplemented(message) \
raise_unimplemented("Gmp.FR." #message);
/*** Allocation functions */
void _mlgmp_fr_finalize(value r)
{
#ifdef USE_MPFR
mpfr_clear(*mpfr_val(r));
#endif
}
int _mlgmp_fr_custom_compare(value a, value b);
void _mlgmp_fr_serialize(value v,
unsigned long * wsize_32,
unsigned long * wsize_64);
unsigned long _mlgmp_fr_deserialize(void * dst);
struct custom_operations _mlgmp_custom_fr =
{
field(identifier) "Gmp.F.t",
field(finalize) &_mlgmp_fr_finalize,
field(compare) &_mlgmp_fr_custom_compare,
field(hash) custom_hash_default,
#if defined(SERIALIZE) && defined(USE_MPFR)
field(serialize) &_mlgmp_fr_serialize,
field(deserialize) &_mlgmp_fr_deserialize
#else
field(serialize) custom_serialize_default,
field(deserialize) custom_deserialize_default
#endif
};
value _mlgmp_fr_create(value prec, value dummy)
{
#ifdef USE_MPFR
CAMLparam2(prec, dummy);
CAMLreturn(alloc_init_mpfr(prec));
#else
unimplemented(create);
#endif
}
value _mlgmp_fr_from_z(value prec, value mode, value a)
{
#ifdef USE_MPFR
CAMLparam2(prec, a);
CAMLlocal1(r);
r=alloc_init_mpfr(prec);
mpfr_set_z(*mpfr_val(r), *mpz_val(a), Mode_val(mode));
CAMLreturn(r);
#else
unimplemented(from_z);
#endif
}
value _mlgmp_fr_from_q(value prec, value mode, value a)
{
#ifdef USE_MPFR
CAMLparam2(prec, a);
CAMLlocal1(r);
r=alloc_init_mpfr(prec);
mpfr_set_q(*mpfr_val(r), *mpq_val(a), Mode_val(mode));
CAMLreturn(r);
#else
unimplemented(from_q);
#endif
}
value _mlgmp_fr_from_si(value prec, value mode, value a)
{
#ifdef USE_MPFR
CAMLparam2(prec, a);
CAMLlocal1(r);
r=alloc_init_mpfr(prec);
mpfr_set_si(*mpfr_val(r), Long_val(a), Mode_val(mode));
CAMLreturn(r);
#else
unimplemented(from_si);
#endif
}
value _mlgmp_fr_from_float(value prec, value mode, value v)
{
#ifdef USE_MPFR
CAMLparam2(prec, v);
CAMLlocal1(r);
r=alloc_init_mpfr(prec);
mpfr_set_d(*mpfr_val(r), Double_val(v), Mode_val(mode));
CAMLreturn(r);
#else
unimplemented(from_float);
#endif
}
/*** Conversions */
value _mlgmp_fr_to_float(value mode, value v)
{
#ifdef USE_MPFR
CAMLparam1(v);
CAMLlocal1(r);
r = caml_copy_double(mpfr_get_d(*mpfr_val(v), Mode_val(mode)));
CAMLreturn(r);
#else
unimplemented(to_float);
#endif
}
value _mlgmp_fr_to_z_exp(value v)
{
#ifdef USE_MPFR
CAMLparam1(v);
CAMLlocal2(r, z);
r = caml_alloc_tuple(2);
z = alloc_init_mpz();
Store_field(r, 0, z);
Store_field(r, 1, Val_int(mpfr_get_z_exp(*mpz_val(z), *mpfr_val(v))));
CAMLreturn(r);
#else
unimplemented(to_z_exp);
#endif
}
value _mlgmp_fr_to_string_exp_base_digits(value mode,
value base, value digits, value val)
{
#ifdef USE_MPFR
CAMLparam4(mode, base, digits, val);
CAMLlocal2(r, rs);
mp_exp_t exponent;
char *s= mpfr_get_str(NULL, &exponent, Int_val(base), Int_val(digits),
*mpfr_val(val), Mode_val(mode));
rs=caml_alloc_string(strlen(s));
strcpy((char*) String_val(rs), s);
free(s);
r=caml_alloc_tuple(2);
Store_field(r, 0, rs);
Store_field(r, 1, Val_int(exponent));
CAMLreturn(r);
#else
unimplemented(to_string);
#endif
}
value _mlgmp_fr_from_string(value prec, value mode, value base, value str)
{
#ifdef USE_MPFR
CAMLparam4(prec, mode, base, str);
CAMLlocal1(r);
r=alloc_init_mpfr(prec);
mpfr_set_str(*mpfr_val(r), String_val(str), Int_val(base), Mode_val(mode));
CAMLreturn(r);
#else
unimplemented(from_string);
#endif
}
/*** Operations */
/**** Arithmetic */
#ifdef USE_MPFR
#define fr_binary_op_mpfr(op) \
value _mlgmp_fr_##op(value prec, value mode, value a, value b) \
{ \
CAMLparam3(prec, a, b); \
CAMLlocal1(r); \
r=alloc_init_mpfr(prec); \
mpfr_##op(*mpfr_val(r), *mpfr_val(a), *mpfr_val(b), Mode_val(mode)); \
CAMLreturn(r); \
}
#define fr_binary_op_ui(op) \
value _mlgmp_fr_##op(value prec, value mode, value a, value b) \
{ \
CAMLparam3(prec, a, b); \
CAMLlocal1(r); \
r=alloc_init_mpfr(prec); \
mpfr_##op(*mpfr_val(r), *mpfr_val(a), Long_val(b), Mode_val(mode)); \
CAMLreturn(r); \
}
#define fr_binary_ui_op(op) \
value _mlgmp_fr_##op(value prec, value mode, value a, value b) \
{ \
CAMLparam3(prec, a, b); \
CAMLlocal1(r); \
r=alloc_init_mpfr(prec); \
mpfr_##op(*mpfr_val(r), Long_val(a), *mpfr_val(b), Mode_val(mode)); \
CAMLreturn(r); \
}
#define fr_unary_op(op) \
value _mlgmp_fr_##op(value prec, value mode, value a) \
{ \
CAMLparam2(prec, a); \
CAMLlocal1(r); \
r=alloc_init_mpfr(prec); \
mpfr_##op(*mpfr_val(r), *mpfr_val(a), Mode_val(mode)); \
CAMLreturn(r); \
}
#define fr_rounding_op(op) \
value _mlgmp_fr_##op(value prec, value a) \
{ \
CAMLparam2(prec, a); \
CAMLlocal1(r); \
r=alloc_init_mpfr(prec); \
mpfr_##op(*mpfr_val(r), *mpfr_val(a)); \
CAMLreturn(r); \
}
#else
#define fr_binary_op_mpfr(op) \
value _mlgmp_fr_##op(value prec, value mode, value a, value b) \
{ \
unimplemented(op) \
}
#define fr_binary_op_ui(op) \
value _mlgmp_fr_##op(value prec, value mode, value a, value b) \
{ \
unimplemented(op) \
}
#define fr_binary_ui_op(op) \
value _mlgmp_fr_##op(value prec, value mode, value a, value b) \
{ \
unimplemented(op) \
}
#define fr_unary_op(op) \
value _mlgmp_fr_##op(value prec, value mode, value a) \
{ \
unimplemented(op) \
}
#define fr_rounding_op(op) \
value _mlgmp_fr_##op(value prec, value mode, value a) \
{ \
unimplemented(op) \
}
#endif
#define fr_binary_op(op) \
fr_binary_op_ui(op##_ui) \
fr_binary_op_mpfr(op)
fr_binary_op(add)
fr_binary_op(sub)
fr_binary_op(mul)
fr_binary_op(div)
fr_binary_ui_op(ui_sub)
fr_binary_ui_op(ui_div)
fr_binary_op_ui(mul_2ui)
fr_binary_op_ui(div_2ui)
fr_binary_op(pow)
fr_binary_ui_op(ui_pow)
fr_unary_op(neg)
fr_unary_op(abs)
fr_unary_op(sin)
fr_unary_op(cos)
fr_unary_op(tan)
fr_unary_op(asin)
fr_unary_op(acos)
fr_unary_op(atan)
fr_unary_op(sinh)
fr_unary_op(cosh)
fr_unary_op(tanh)
fr_unary_op(asinh)
fr_unary_op(acosh)
fr_unary_op(atanh)
fr_unary_op(sqrt)
fr_unary_op(exp)
fr_unary_op(exp2)
fr_unary_op(rint)
fr_rounding_op(ceil)
fr_rounding_op(floor)
fr_rounding_op(trunc)
/*** Compare */
int _mlgmp_fr_custom_compare(value a, value b)
{
#ifdef USE_MPFR
CAMLparam2(a, b);
CAMLreturn(mpfr_cmp(*mpfr_val(a), *mpfr_val(b)));
#else
unimplemented(create);
#endif
}
value _mlgmp_fr_cmp(value a, value b)
{
#ifdef USE_MPFR
CAMLparam2(a, b);
CAMLreturn(Val_int(mpfr_cmp(*mpfr_val(a), *mpfr_val(b))));
#else
unimplemented(create);
#endif
}
value _mlgmp_fr_cmp_si(value a, value b)
{
#ifdef USE_MPFR
CAMLparam2(a, b);
CAMLreturn(Val_int(mpfr_cmp_si(*mpfr_val(a), Long_val(b))));
#else
unimplemented(create);
#endif
}
value _mlgmp_fr_sgn(value a)
{
#ifdef USE_MPFR
CAMLparam1(a);
CAMLreturn(Val_int(mpfr_sgn(*mpfr_val(a))));
#else
unimplemented(create);
#endif
}
value _mlgmp_fr_is_nan(value a)
{
#ifdef USE_MPFR
CAMLparam1(a);
CAMLreturn(mpfr_nan_p(*mpfr_val(a)) ? Val_true : Val_false);
#else
unimplemented(create);
#endif
}
value _mlgmp_fr_eq(value a, value b, value nbits)
{
#ifdef USE_MPFR
CAMLparam3(a, b, nbits);
CAMLreturn(mpfr_eq(*mpfr_val(a), *mpfr_val(b), Int_val(nbits))
? Val_true : Val_false);
#else
unimplemented(create);
#endif
}
fr_binary_op_mpfr(reldiff)
/*** Random */
value _mlgmp_fr_urandomb(value prec, value state)
{
#ifdef USE_MPFR
CAMLparam2(prec, state);
CAMLlocal1(r);
r = alloc_init_mpfr(prec);
mpfr_urandomb(*mpfr_val(r), *randstate_val(state));
CAMLreturn(r);
#else
unimplemented(urandomb);
#endif
}
/* Old mpfr - no longer exists in 3.0.1-p3
value _mlgmp_fr_random(value prec)
{
#ifdef USE_MPFR
CAMLparam1(prec);
CAMLlocal1(r);
r = alloc_init_mpfr(prec);
mpfr_random(*mpfr_val(r));
CAMLreturn(r);
#else
unimplemented(urandomb);
#endif
}
*/
/* Old MPFR -no longer exists in 20011026
value _mlgmp_fr_srandom(value seed)
{
#ifdef USE_MPFR
mpfr_srandom(Int_val(seed));
return Val_unit;
#else
unimplemented(urandomb);
#endif
}
*/
/* Old mpfr - no longer exists in 3.0.1-p3
value _mlgmp_fr_random2(value prec, value nlimbs, value max_exp)
{
#ifdef USE_MPFR
CAMLparam3(prec, nlimbs, max_exp);
CAMLlocal1(r);
r = alloc_init_mpfr(prec);
mpfr_random2(*mpfr_val(r), Int_val(nlimbs), Int_val(max_exp));
CAMLreturn(r);
#else
unimplemented(urandomb);
#endif
}
*/
/*** Serialization */
value _mlgmp_fr_initialize(void)
{
#ifdef USE_MPFR
CAMLparam0();
caml_register_custom_operations(& _mlgmp_custom_f);
CAMLreturn(Val_unit);
#endif
}
#if defined(SERIALIZE) && defined(USE_MPFR)
void _mlgmp_fr_serialize(value v,
unsigned long * wsize_32,
unsigned long * wsize_64)
{
CAMLparam1(v);
char *s;
int len;
mp_exp_t exponent;
char exponent_buf[10];
*wsize_32 = MPFR_SIZE_ARCH32;
*wsize_64 = MPFR_SIZE_ARCH64;
caml_serialize_int_4(mpfr_get_prec(*mpfr_val(v)));
s = mpfr_get_str (NULL, &exponent, 16, 0, *mpfr_val(v), GMP_RNDN);
len = strlen(s);
caml_serialize_int_4(len + 11);
caml_serialize_block_1("0.", 2);
caml_serialize_block_1(s, len);
free(s);
sprintf(exponent_buf, "@%08lx", (exponent & 0xFFFFFFFFUL));
caml_serialize_block_1(exponent_buf, 9);
CAMLreturn0;
}
unsigned long _mlgmp_fr_deserialize(void * dst)
{
char *s;
int len;
mpfr_init2(*((mpfr_t*) dst), caml_deserialize_uint_4());
len = caml_deserialize_uint_4();
s = malloc(len+1);
caml_deserialize_block_1(s, len);
s[len] = 0;
mpfr_set_str (*((mpfr_t*) dst), s, 16, GMP_RNDN);
free(s);
return sizeof(mpfr_t);
}
#endif