-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmlgmp_f.c
337 lines (276 loc) · 7.18 KB
/
mlgmp_f.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
#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.F."
/*** Allocation functions */
void _mlgmp_f_finalize(value r)
{
mpf_clear(*mpf_val(r));
}
int _mlgmp_f_custom_compare(value a, value b);
void _mlgmp_f_serialize(value v,
unsigned long * wsize_32,
unsigned long * wsize_64);
unsigned long _mlgmp_f_deserialize(void * dst);
struct custom_operations _mlgmp_custom_f =
{
field(identifier) "Gmp.F.t",
field(finalize) &_mlgmp_f_finalize,
field(compare) &_mlgmp_f_custom_compare,
field(hash) custom_hash_default,
#ifdef SERIALIZE
field(serialize) &_mlgmp_f_serialize,
field(deserialize) &_mlgmp_f_deserialize
#else
field(serialize) custom_serialize_default,
field(deserialize) custom_deserialize_default
#endif
};
value _mlgmp_f_create(value prec)
{
CAMLparam1(prec);
CAMLreturn(alloc_init_mpf(prec));
}
value _mlgmp_f_from_z(value prec, value a)
{
CAMLparam2(prec, a);
CAMLlocal1(r);
r=alloc_init_mpf(prec);
mpf_set_z(*mpf_val(r), *mpz_val(a));
CAMLreturn(r);
}
value _mlgmp_f_from_q(value prec, value a)
{
CAMLparam2(prec, a);
CAMLlocal1(r);
r=alloc_init_mpf(prec);
mpf_set_q(*mpf_val(r), *mpq_val(a));
CAMLreturn(r);
}
value _mlgmp_f_from_si(value prec, value a)
{
CAMLparam2(prec, a);
CAMLlocal1(r);
r=alloc_init_mpf(prec);
mpf_set_si(*mpf_val(r), Long_val(a));
CAMLreturn(r);
}
value _mlgmp_f_from_float(value prec, value v)
{
CAMLparam2(prec, v);
CAMLlocal1(r);
r=alloc_init_mpf(prec);
mpf_set_d(*mpf_val(r), Double_val(v));
CAMLreturn(r);
}
/*** Conversions */
value _mlgmp_f_to_float(value v)
{
CAMLparam1(v);
CAMLlocal1(r);
r = caml_copy_double(mpf_get_d(*mpf_val(v)));
CAMLreturn(r);
}
value _mlgmp_f_to_string_exp_base_digits(value base, value digits, value val)
{
CAMLparam3(base, digits, val);
CAMLlocal2(r, rs);
mp_exp_t exponent;
char *s= mpf_get_str(NULL, &exponent, Int_val(base), Int_val(digits),
*mpf_val(val));
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);
}
value _mlgmp_f_from_string(value prec, value base, value str)
{
CAMLparam3(prec, base, str);
CAMLlocal1(r);
r=alloc_init_mpf(prec);
mpf_set_str(*mpf_val(r), String_val(str), Int_val(base));
CAMLreturn(r);
}
/*** Operations */
/**** Arithmetic */
#define f_binary_op_mpf(op) \
value _mlgmp_f_##op(value prec, value a, value b) \
{ \
CAMLparam3(prec, a, b); \
CAMLlocal1(r); \
r=alloc_init_mpf(prec); \
mpf_##op(*mpf_val(r), *mpf_val(a), *mpf_val(b)); \
CAMLreturn(r); \
}
#define f_binary_op_ui(op) \
value _mlgmp_f_##op(value prec, value a, value b) \
{ \
CAMLparam3(prec, a, b); \
CAMLlocal1(r); \
r=alloc_init_mpf(prec); \
mpf_##op(*mpf_val(r), *mpf_val(a), Long_val(b)); \
CAMLreturn(r); \
}
#define f_binary_ui_op(op) \
value _mlgmp_f_##op(value prec, value a, value b) \
{ \
CAMLparam3(prec, a, b); \
CAMLlocal1(r); \
r=alloc_init_mpf(prec); \
mpf_##op(*mpf_val(r), Long_val(a), *mpf_val(b)); \
CAMLreturn(r); \
}
#define f_binary_op(op) \
f_binary_op_ui(op##_ui) \
f_binary_op_mpf(op)
#define f_unary_op(op) \
value _mlgmp_f_##op(value prec, value a) \
{ \
CAMLparam2(prec, a); \
CAMLlocal1(r); \
r=alloc_init_mpf(prec); \
mpf_##op(*mpf_val(r), *mpf_val(a)); \
CAMLreturn(r); \
}
f_binary_op(add)
f_binary_op(sub)
f_binary_op(mul)
value _mlgmp_f_div(value prec, value n, value d)
{
CAMLparam3(prec, n, d);
CAMLlocal1(r);
if (! mpf_sgn(*mpf_val(d)))
division_by_zero(); /* is it ok to launch the exception here? */
r = alloc_init_mpf(prec);
mpf_div(*mpf_val(r), *mpf_val(n), *mpf_val(d));
CAMLreturn(r);
}
value _mlgmp_f_div_ui(value prec, value n, value d)
{
int real_d;
CAMLparam3(prec, n, d);
CAMLlocal1(r);
real_d = Long_val(d);
if (! real_d)
division_by_zero(); /* is it ok to launch the exception here? */
r = alloc_init_mpf(prec);
mpf_div_ui(*mpf_val(r), *mpf_val(n), real_d);
CAMLreturn(r);
}
value _mlgmp_f_ui_div(value prec, value n, value d)
{
int real_d;
CAMLparam3(prec, n, d);
CAMLlocal1(r);
if (! mpf_sgn(*mpf_val(d)))
division_by_zero(); /* is it ok to launch the exception here? */
r = alloc_init_mpf(prec);
mpf_ui_div(*mpf_val(r), Long_val(n), *mpf_val(d));
CAMLreturn(r);
}
f_binary_ui_op(ui_sub)
f_unary_op(neg)
f_unary_op(abs)
f_unary_op(ceil)
f_unary_op(floor)
f_unary_op(trunc)
/*** Compare */
int _mlgmp_f_custom_compare(value a, value b)
{
CAMLparam2(a, b);
CAMLreturn(mpf_cmp(*mpf_val(a), *mpf_val(b)));
}
value _mlgmp_f_cmp(value a, value b)
{
CAMLparam2(a, b);
CAMLreturn(Val_int(mpf_cmp(*mpf_val(a), *mpf_val(b))));
}
value _mlgmp_f_cmp_si(value a, value b)
{
CAMLparam2(a, b);
CAMLreturn(Val_int(mpf_cmp_si(*mpf_val(a), Long_val(b))));
}
value _mlgmp_f_sgn(value a)
{
CAMLparam1(a);
CAMLreturn(Val_int(mpf_sgn(*mpf_val(a))));
}
value _mlgmp_f_eq(value a, value b, value nbits)
{
CAMLparam3(a, b, nbits);
CAMLreturn(mpf_eq(*mpf_val(a), *mpf_val(b), Int_val(nbits))
? Val_true : Val_false);
}
f_binary_op_mpf(reldiff)
/*** Random */
value _mlgmp_f_urandomb(value prec, value state, value nbits)
{
CAMLparam3(prec, state, nbits);
CAMLlocal1(r);
r = alloc_init_mpf(prec);
mpf_urandomb(*mpf_val(r), *randstate_val(state), Int_val(nbits));
CAMLreturn(r);
}
value _mlgmp_f_random2(value prec, value nlimbs, value max_exp)
{
CAMLparam3(prec, nlimbs, max_exp);
CAMLlocal1(r);
r = alloc_init_mpf(prec);
mpf_random2(*mpf_val(r), Int_val(nlimbs), Int_val(max_exp));
CAMLreturn(r);
}
/*** Serialization */
value _mlgmp_f_initialize()
{
CAMLparam0();
caml_register_custom_operations(& _mlgmp_custom_f);
CAMLreturn(Val_unit);
}
#ifdef SERIALIZE
void _mlgmp_f_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 = MPF_SIZE_ARCH32;
*wsize_64 = MPF_SIZE_ARCH64;
caml_serialize_int_4(mpf_get_prec(*mpf_val(v)));
s = mpf_get_str (NULL, &exponent, 16, 0, *mpf_val(v));
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_f_deserialize(void * dst)
{
char *s;
int len;
mpf_init2(*((mpf_t*) dst), caml_deserialize_uint_4());
len = caml_deserialize_uint_4();
s = malloc(len+1);
caml_deserialize_block_1(s, len);
s[len] = 0;
mpf_set_str (*((mpf_t*) dst), s, 16);
free(s);
return sizeof(mpf_t);
}
#endif