-
Notifications
You must be signed in to change notification settings - Fork 6
/
gmp_caml.c
509 lines (453 loc) · 13 KB
/
gmp_caml.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
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
/* ********************************************************************** */
/* gmp_caml.c */
/* ********************************************************************** */
/* This file is part of the MLGmpIDL interface, released under LGPL license
with an exception allowing the redistribution of statically linked
executables.
Please read the COPYING file packaged in the distribution */
#include <assert.h>
#include <stdio.h>
#include <limits.h>
#include <math.h>
#include "caml/version.h"
#include "caml/fail.h"
#include "caml/alloc.h"
#include "caml/custom.h"
#include "caml/memory.h"
#include "caml/callback.h"
#include "caml/camlidlruntime.h"
#include "gmp_caml.h"
/* ********************************************************************** */
/* I. Custom datatypes: fastest coding */
/* ********************************************************************** */
/* ====================================================================== */
/* I.1 mpz_t */
/* ====================================================================== */
void camlidl_custom_mpz_finalize(value val)
{
__mpz_struct* mpz = (__mpz_struct*)(Data_custom_val(val));
mpz_clear(mpz);
}
int camlidl_custom_mpz_compare(value val1, value val2)
{
int res;
__mpz_struct* mpz1;
__mpz_struct* mpz2;
mpz1 = (__mpz_struct*)(Data_custom_val(val1));
mpz2 = (__mpz_struct*)(Data_custom_val(val2));
res = mpz_cmp(mpz1,mpz2);
res = res > 0 ? 1 : res==0 ? 0 : -1;
return res;
}
long camlidl_custom_mpz_hash(value val)
{
__mpz_struct* mpz = (__mpz_struct*)(Data_custom_val(val));
long hash = mpz_get_si(mpz);
return hash;
}
struct custom_operations camlidl_custom_mpz = {
"camlidl_gmp_custom_mpz",
&camlidl_custom_mpz_finalize,
&camlidl_custom_mpz_compare,
&camlidl_custom_mpz_hash,
custom_serialize_default,
custom_deserialize_default,
custom_compare_ext_default,
#if OCAML_VERSION >= 40800
custom_fixed_length_default
#endif
};
value camlidl_mpz_ptr_c2ml(mpz_ptr* mpz)
{
value val;
val = caml_alloc_custom(&camlidl_custom_mpz, sizeof(__mpz_struct), 0, 1);
*(((__mpz_struct*)(Data_custom_val(val)))) = *(*mpz);
return val;
}
void camlidl_mpz_ptr_ml2c(value val, mpz_ptr* mpz)
{
*mpz = (mpz_ptr)(Data_custom_val(val));
}
/*
void camlidl_mpz_ml2c(value val, __mpz_struct* mpz)
{
*mpz = *((mpz_ptr)(Data_custom_val(val)));
}
*/
/* ====================================================================== */
/* I.2 mpq_t */
/* ====================================================================== */
void camlidl_custom_mpq_finalize(value val)
{
__mpq_struct* mpq = (__mpq_struct*)(Data_custom_val(val));
mpq_clear(mpq);
}
int camlidl_custom_mpq_compare(value val1, value val2)
{
int res;
__mpq_struct* mpq1;
__mpq_struct* mpq2;
mpq1 = (__mpq_struct*)(Data_custom_val(val1));
mpq2 = (__mpq_struct*)(Data_custom_val(val2));
res = mpq_cmp(mpq1,mpq2);
res = res > 0 ? 1 : res==0 ? 0 : -1;
return res;
}
long camlidl_custom_mpq_hash(value val)
{
__mpq_struct* mpq = (__mpq_struct*)(Data_custom_val(val));
unsigned long num = mpz_get_ui(mpq_numref(mpq));
unsigned long den = mpz_get_ui(mpq_denref(mpq));
long hash;
if (num==0) hash = 0;
else if (den==0) hash = num>0 ? LONG_MAX : LONG_MIN;
else hash = (num<den ? den/num : num/den);
return hash;
}
struct custom_operations camlidl_custom_mpq = {
"camlidl_gmp_custom_mpq",
&camlidl_custom_mpq_finalize,
&camlidl_custom_mpq_compare,
&camlidl_custom_mpq_hash,
custom_serialize_default,
custom_deserialize_default,
custom_compare_ext_default,
#if OCAML_VERSION >= 40800
custom_fixed_length_default
#endif
};
value camlidl_mpq_ptr_c2ml(mpq_ptr* mpq)
{
value val;
val = caml_alloc_custom(&camlidl_custom_mpq, sizeof(__mpq_struct), 0, 1);
*(((__mpq_struct*)(Data_custom_val(val)))) = *(*mpq);
return val;
}
void camlidl_mpq_ptr_ml2c(value val, mpq_ptr* mpq)
{
*mpq = (mpq_ptr)(Data_custom_val(val));
}
/*
void camlidl_mpq_ml2c(value val, __mpq_struct* mpq)
{
*mpq = *((mpq_ptr)(Data_custom_val(val)));
}
*/
int mpz_fits_int_p (mpz_t OP)
{
if (mpz_fits_slong_p(OP)){
long v = mpz_get_si(OP);
return (Min_long <= v && v<=Max_long);
}
else {
return 0;
}
}
/* ====================================================================== */
/* I.3 mpf_t */
/* ====================================================================== */
void camlidl_custom_mpf_finalize(value val)
{
__mpf_struct* mpf = (__mpf_struct*)(Data_custom_val(val));
mpf_clear(mpf);
}
int camlidl_custom_mpf_compare(value val1, value val2)
{
int res;
__mpf_struct* mpf1;
__mpf_struct* mpf2;
mpf1 = (__mpf_struct*)(Data_custom_val(val1));
mpf2 = (__mpf_struct*)(Data_custom_val(val2));
res = mpf_cmp(mpf1,mpf2);
res = res > 0 ? 1 : res==0 ? 0 : -1;
return res;
}
long camlidl_custom_mpf_hash(value val)
{
__mpf_struct* mpf = (__mpf_struct*)(Data_custom_val(val));
long hash;
double d;
signed long int exp;
d = mpf_get_d_2exp (&exp, mpf);
hash = (long)d;
return hash;
}
struct custom_operations camlidl_custom_mpf = {
"camlidl_gmp_custom_mpf",
&camlidl_custom_mpf_finalize,
&camlidl_custom_mpf_compare,
&camlidl_custom_mpf_hash,
custom_serialize_default,
custom_deserialize_default,
custom_compare_ext_default,
#if OCAML_VERSION >= 40800
custom_fixed_length_default
#endif
};
value camlidl_mpf_ptr_c2ml(mpf_ptr* mpf)
{
value val;
val = caml_alloc_custom(&camlidl_custom_mpf, sizeof(__mpf_struct), 0, 1);
*(((__mpf_struct*)(Data_custom_val(val)))) = *(*mpf);
return val;
}
void camlidl_mpf_ptr_ml2c(value val, mpf_ptr* mpf)
{
*mpf = (mpf_ptr)(Data_custom_val(val));
}
/*
void camlidl_mpf_ml2c(value val, __mpf_struct* mpf)
{
*mpf = *((mpf_ptr)(Data_custom_val(val)));
}
*/
int mpf_fits_int_p (mpf_t OP)
{
if (mpf_fits_slong_p(OP)){
long v = mpf_get_si(OP);
return (Min_long <= v && v <= Max_long);
}
else {
return 0;
}
}
/* ====================================================================== */
/* I.4 mpfr_t */
/* ====================================================================== */
void camlidl_custom_mpfr_finalize(value val)
{
__mpfr_struct* mpfr = (__mpfr_struct*)(Data_custom_val(val));
mpfr_clear(mpfr);
}
int camlidl_custom_mpfr_compare(value val1, value val2)
{
int res;
__mpfr_struct* mpfr1;
__mpfr_struct* mpfr2;
mpfr1 = (__mpfr_struct*)(Data_custom_val(val1));
mpfr2 = (__mpfr_struct*)(Data_custom_val(val2));
res = mpfr_cmp(mpfr1,mpfr2);
res = res > 0 ? 1 : res==0 ? 0 : -1;
return res;
}
long camlidl_custom_mpfr_hash(value val)
{
__mpfr_struct* mpfr = (__mpfr_struct*)(Data_custom_val(val));
long hash;
double d;
int exp;
d = mpfr_get_d(mpfr, GMP_RNDN);
d = frexp(d,&exp);
hash = (long)d;
return hash;
}
struct custom_operations camlidl_custom_mpfr = {
"camlidl_gmp_custom_mpfr",
&camlidl_custom_mpfr_finalize,
&camlidl_custom_mpfr_compare,
&camlidl_custom_mpfr_hash,
custom_serialize_default,
custom_deserialize_default,
custom_compare_ext_default,
#if OCAML_VERSION >= 40800
custom_fixed_length_default
#endif
};
value camlidl_mpfr_ptr_c2ml(mpfr_ptr* mpfr)
{
value val;
val = caml_alloc_custom(&camlidl_custom_mpfr, sizeof(__mpfr_struct), 0, 1);
*(((__mpfr_struct*)(Data_custom_val(val)))) = *(*mpfr);
return val;
}
void camlidl_mpfr_ptr_ml2c(value val, mpfr_ptr* mpfr)
{
*mpfr = (mpfr_ptr)(Data_custom_val(val));
}
/*
void camlidl_mpfr_ml2c(value val, __mpfr_struct* mpfr)
{
*mpfr = *((mpfr_ptr)(Data_custom_val(val)));
}
*/
/* ====================================================================== */
/* I.5 gmp_randstate_t */
/* ====================================================================== */
void camlidl_custom_gmp_randstate_finalize(value val)
{
__gmp_randstate_struct* gmp_randstate = (__gmp_randstate_struct*)(Data_custom_val(val));
gmp_randclear(gmp_randstate);
}
struct custom_operations camlidl_custom_gmp_randstate = {
"camlidl_gmp_custom_randstate",
&camlidl_custom_gmp_randstate_finalize,
custom_compare_default,
custom_hash_default,
custom_serialize_default,
custom_deserialize_default,
custom_compare_ext_default,
#if OCAML_VERSION >= 40800
custom_fixed_length_default
#endif
};
value camlidl_gmp_randstate_ptr_c2ml(gmp_randstate_ptr* gmp_randstate)
{
value val;
val = caml_alloc_custom(&camlidl_custom_gmp_randstate, sizeof(__gmp_randstate_struct), 0, 1);
*((__gmp_randstate_struct*)(Data_custom_val(val))) = *(*gmp_randstate);
return val;
}
void camlidl_gmp_randstate_ptr_ml2c(value val, gmp_randstate_ptr* gmp_randstate)
{
*gmp_randstate = (gmp_randstate_ptr)(Data_custom_val(val));
}
/* ********************************************************************** */
/* II. Custom datatypes: safer coding in case of repeated callbacks */
/* ********************************************************************** */
/* ====================================================================== */
/* II.1 mpz2_t */
/* ====================================================================== */
void camlidl_custom_mpz2_finalize(value val)
{
CAMLparam1(val);
__mpz_struct** mpz = (__mpz_struct**)(Data_custom_val(val));
mpz_clear(*mpz);
free(*mpz);
}
int camlidl_custom_mpz2_compare(value val1, value val2)
{
CAMLparam2(val1,val2);
int res;
__mpz_struct** mpz1;
__mpz_struct** mpz2;
mpz1 = (__mpz_struct**)(Data_custom_val(val1));
mpz2 = (__mpz_struct**)(Data_custom_val(val2));
res = mpz_cmp(*mpz1,*mpz2);
res = res > 0 ? 1 : res==0 ? 0 : -1;
CAMLreturn(res);
}
long camlidl_custom_mpz2_hash(value val)
{
CAMLparam1(val);
__mpz_struct** mpz = (__mpz_struct**)(Data_custom_val(val));
long hash = mpz_get_si(*mpz);
CAMLreturn(hash);
}
struct custom_operations camlidl_custom_mpz2 = {
"camlidl_gmp_custom_mpz2",
&camlidl_custom_mpz2_finalize,
&camlidl_custom_mpz2_compare,
&camlidl_custom_mpz2_hash,
custom_serialize_default,
custom_deserialize_default,
custom_compare_ext_default,
#if OCAML_VERSION >= 40800
custom_fixed_length_default
#endif
};
value camlidl_mpz2_ptr_c2ml(mpz_ptr* mpz)
{
value val;
__mpz_struct* p;
p = malloc(sizeof(__mpz_struct));
*p = *(*mpz);
val = caml_alloc_custom(&camlidl_custom_mpz2, sizeof(__mpz_struct*), 0, 1);
*(((__mpz_struct**)(Data_custom_val(val)))) = p;
return val;
}
void camlidl_mpz2_ptr_ml2c(value val, mpz_ptr* mpz)
{
*mpz = *(__mpz_struct**)(Data_custom_val(val));
}
/* ====================================================================== */
/* II.2 mpq2_t */
/* ====================================================================== */
void camlidl_custom_mpq2_finalize(value val)
{
CAMLparam1(val);
__mpq_struct** mpq = (__mpq_struct**)(Data_custom_val(val));
mpq_clear(*mpq);
}
int camlidl_custom_mpq2_compare(value val1, value val2)
{
CAMLparam2(val1,val2);
int res;
__mpq_struct** mpq1;
__mpq_struct** mpq2;
mpq1 = (__mpq_struct**)(Data_custom_val(val1));
mpq2 = (__mpq_struct**)(Data_custom_val(val2));
res = mpq_cmp(*mpq1,*mpq2);
res = res > 0 ? 1 : res==0 ? 0 : -1;
CAMLreturn(res);
}
long camlidl_custom_mpq2_hash(value val)
{
CAMLparam1(val);
__mpq_struct** mpq = (__mpq_struct**)(Data_custom_val(val));
unsigned long num = mpz_get_ui(mpq_numref(*mpq));
unsigned long den = mpz_get_ui(mpq_denref(*mpq));
long hash = num<den ? den/num : num/den;
CAMLreturn(hash);
}
struct custom_operations camlidl_custom_mpq2 = {
"camlidl_gmp_custom_mpq2",
&camlidl_custom_mpq2_finalize,
&camlidl_custom_mpq2_compare,
&camlidl_custom_mpq2_hash,
custom_serialize_default,
custom_deserialize_default,
custom_compare_ext_default,
#if OCAML_VERSION >= 40800
custom_fixed_length_default
#endif
};
value camlidl_mpq2_ptr_c2ml(mpq_ptr* mpq)
{
value val;
__mpq_struct* p;
p = malloc(sizeof(__mpq_struct));
*p = *(*mpq);
val = caml_alloc_custom(&camlidl_custom_mpq2, sizeof(__mpq_struct), 0, 1);
*((__mpq_struct**)(Data_custom_val(val))) = p;
return val;
}
void camlidl_mpq2_ptr_ml2c(value val, mpq_ptr* mpq)
{
*mpq = *(__mpq_struct**)(Data_custom_val(val));
}
/* ====================================================================== */
/* II.3 gmp_randstate2_t */
/* ====================================================================== */
void camlidl_custom_gmp_randstate2_finalize(value val)
{
CAMLparam1(val);
__gmp_randstate_struct** gmp_randstate = (__gmp_randstate_struct**)(Data_custom_val(val));
gmp_randclear(*gmp_randstate);
free(*gmp_randstate);
}
struct custom_operations camlidl_custom_gmp_randstate2 = {
"camlidl_gmp_custom_randstate2",
&camlidl_custom_gmp_randstate2_finalize,
custom_compare_default,
custom_hash_default,
custom_serialize_default,
custom_deserialize_default,
custom_compare_ext_default,
#if OCAML_VERSION >= 40800
custom_fixed_length_default
#endif
};
value camlidl_gmp_randstate2_ptr_c2ml(gmp_randstate_ptr* gmp_randstate)
{
value val;
__gmp_randstate_struct* p;
p = malloc(sizeof(__gmp_randstate_struct));
*p = *(*gmp_randstate);
val = caml_alloc_custom(&camlidl_custom_gmp_randstate2, sizeof(__gmp_randstate_struct*), 0, 1);
*((__gmp_randstate_struct**)(Data_custom_val(val))) = p;
return val;
}
void camlidl_gmp_randstate2_ptr_ml2c(value val, gmp_randstate_ptr* gmp_randstate)
{
*gmp_randstate = *(__gmp_randstate_struct**)(Data_custom_val(val));
}