File: mlgmp_z.c

package info (click to toggle)
mlgmp 20021123-20
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 512 kB
  • sloc: ansic: 3,257; ml: 3,126; makefile: 167
file content (810 lines) | stat: -rw-r--r-- 19,592 bytes parent folder | download | duplicates (3)
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
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
#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 <stdlib.h>
#include <string.h>

#include "config.h"
#include "mlgmp.h"
#include "conversions.c"

#define MODULE "Gmp.Z."

/*** Allocation functions */

void _mlgmp_z_finalize(value r)
{
  mpz_clear(*mpz_val(r));
}

int _mlgmp_z_custom_compare(value a, value b);
void _mlgmp_z_serialize(value v,
			unsigned long * wsize_32,
			unsigned long * wsize_64);
unsigned long _mlgmp_z_deserialize(void * dst);
long _mlgmp_z_hash(value v);

struct custom_operations _mlgmp_custom_z =
  {
    field(identifier)  "Gmp.Z.t",
    field(finalize)    &_mlgmp_z_finalize,
    field(compare)     &_mlgmp_z_custom_compare,
    field(hash)        &_mlgmp_z_hash,
#ifdef SERIALIZE
    field(serialize)   &_mlgmp_z_serialize,
    field(deserialize) &_mlgmp_z_deserialize
#else
    field(serialize)   custom_serialize_default,
    field(deserialize) custom_deserialize_default
#endif
  };

value _mlgmp_z_create(void)
{
  CAMLparam0();
  CAMLreturn(alloc_init_mpz());
}

value _mlgmp_z_copy(value from)
{
  CAMLparam1(from);
  CAMLlocal1(r);
  r = alloc_mpz();
  mpz_init_set(*mpz_val(r), *mpz_val(from));
  CAMLreturn(r);
}

value _mlgmp_z2_copy(value dest, value from)
{
  CAMLparam2(dest, from);
  mpz_set(*mpz_val(dest), *mpz_val(from));
  CAMLreturn(Val_unit);
}

value _mlgmp_z_from_int(value ml_val)
{
  CAMLparam1(ml_val);
  CAMLlocal1(r);
  r=alloc_mpz();
  mpz_init_set_si(*mpz_val(r), Long_val(ml_val)); /* FIX */
  CAMLreturn(r);
}

value _mlgmp_z_from_string_base(value base, value ml_val)
{
  CAMLparam2(base, ml_val);
  CAMLlocal1(r);
  r=alloc_mpz();
  mpz_init_set_str(*mpz_val(r), String_val(ml_val), Int_val(base));
  CAMLreturn(r);
}

value _mlgmp_z_from_float(value ml_val)
{
  CAMLparam1(ml_val);
  CAMLlocal1(r);
  r=alloc_mpz();
  mpz_init_set_d(*mpz_val(r), Double_val(ml_val));
  CAMLreturn(r);
}

value _mlgmp_z2_from_int(value r, value ml_val)
{
  CAMLparam2(r, ml_val);
  mpz_set_si(*mpz_val(r), Long_val(ml_val));
  CAMLreturn(Val_unit);
}

value _mlgmp_z2_from_string_base(value r, value base, value ml_val)
{
  CAMLparam3(r, base, ml_val);
  mpz_set_str(*mpz_val(r), String_val(ml_val), Int_val(base));
  CAMLreturn(Val_unit);
}

value _mlgmp_z2_from_float(value r, value ml_val)
{
  CAMLparam2(r, ml_val);
  mpz_set_d(*mpz_val(r), Double_val(ml_val));
  CAMLreturn(Val_unit);
}

/*** Conversions */

value _mlgmp_z_to_string_base(value ml_base, value ml_val)
{
  int base;
  char *s;

  CAMLparam2(ml_base, ml_val);
  CAMLlocal1(r);
  base=Int_val(ml_base);

  /* This is sub-optimal, but using mpz_sizeinbase would
     need a means of shortening the length of a pre-allocated
     Caml string (mpz_sizeinbase sometimes overestimates lengths). */
  s=mpz_get_str(NULL, base, *mpz_val(ml_val));
  r=alloc_string(strlen(s));
  strcpy(String_val(r), s);
  free(s);

  CAMLreturn(r);
}

value _mlgmp_z_to_int(value ml_val)
{
  CAMLparam1(ml_val);
  CAMLreturn(Val_int(mpz_get_si(* mpz_val(ml_val))));
}

value _mlgmp_z_to_float(value v)
{
  CAMLparam1(v);
  CAMLlocal1(r);
  r = copy_double(mpz_get_d(*mpz_val(v)));
  CAMLreturn(r);
}

/*** Operations */
/**** Arithmetic */

#define z_binary_op_ui(op)                              \
value _mlgmp_z_##op(value a, value b)		        \
{							\
  CAMLparam2(a, b);                                     \
  CAMLlocal1(r);                                        \
  r=alloc_init_mpz();				        \
  mpz_##op(*mpz_val(r), *mpz_val(a), Long_val(b));	\
  CAMLreturn(r);					\
}                                                       \
                                                        \
value _mlgmp_z2_##op(value r, value a, value b)		\
{							\
  CAMLparam3(r, a, b);                                  \
  mpz_##op(*mpz_val(r), *mpz_val(a), Long_val(b));	\
  CAMLreturn(Val_unit);					\
}

#define z_binary_op_mpz(op)				\
value _mlgmp_z_##op(value a, value b)			\
{							\
  CAMLparam2(a, b);                                     \
  CAMLlocal1(r);                                        \
  r=alloc_init_mpz();				        \
  mpz_##op(*mpz_val(r), *mpz_val(a), *mpz_val(b));	\
  CAMLreturn(r);	       				\
}                                                       \
                                                        \
value _mlgmp_z2_##op(value r, value a, value b)	       	\
{							\
  CAMLparam3(r, a, b);                                  \
  mpz_##op(*mpz_val(r), *mpz_val(a), *mpz_val(b));	\
  CAMLreturn(Val_unit);	       				\
}

#define z_binary_op(op)				\
z_binary_op_mpz(op)				\
z_binary_op_ui(op##_ui)

z_binary_op(add)
z_binary_op(sub)
z_binary_op(mul)

/**** Powers */
z_binary_op_ui(pow_ui)


value _mlgmp_z_powm_ui(value a, value b, value modulus)
{
  CAMLparam3(a, b, modulus);
  CAMLlocal1(r);
  r=alloc_init_mpz();
  mpz_powm_ui(*mpz_val(r), *mpz_val(a), Long_val(b), *mpz_val(modulus));
  CAMLreturn(r);
}

value _mlgmp_z_ui_pow_ui(value a, value b)
{
  CAMLparam2(a, b);
  CAMLlocal1(r);
  r=alloc_init_mpz();
  mpz_ui_pow_ui(*mpz_val(r), Long_val(a), Long_val(b));
  CAMLreturn(r);
}

value _mlgmp_z_powm(value a, value b, value modulus)
{
  CAMLparam3(a, b, modulus);
  CAMLlocal1(r);
  r=alloc_init_mpz();
  mpz_powm(*mpz_val(r), *mpz_val(a), *mpz_val(b), *mpz_val(modulus));
  CAMLreturn(r);
}

value _mlgmp_z2_powm_ui(value r, value a, value b, value modulus)
{
  CAMLparam4(r, a, b, modulus);
  mpz_powm_ui(*mpz_val(r), *mpz_val(a), Long_val(b), *mpz_val(modulus));
  CAMLreturn(Val_unit);
}

value _mlgmp_z2_ui_pow_ui(value r, value a, value b)
{
  CAMLparam3(r, a, b);
  mpz_ui_pow_ui(*mpz_val(r), Long_val(a), Long_val(b));
  CAMLreturn(Val_unit);
}

value _mlgmp_z2_powm(value r, value a, value b, value modulus)
{
  CAMLparam4(r, a, b, modulus);
  mpz_powm(*mpz_val(r), *mpz_val(a), *mpz_val(b), *mpz_val(modulus));
  CAMLreturn(Val_unit);
}

/**** Unary */
#define z_unary_op(op)				\
value _mlgmp_z_##op(value a)			\
{						\
  CAMLparam1(a);				\
  CAMLlocal1(r);				\
  r=alloc_init_mpz();   			\
  mpz_##op(*mpz_val(r), *mpz_val(a));		\
  CAMLreturn(r);				\
}                                               \
                                                \
value _mlgmp_z2_##op(value r, value a)	        \
{						\
  CAMLparam2(r, a);				\
  mpz_##op(*mpz_val(r), *mpz_val(a));		\
  CAMLreturn(Val_unit);				\
}

z_unary_op(neg)
z_unary_op(abs)

/**** Roots */

/* Negative ?*/
z_unary_op(sqrt)

value _mlgmp_z_sqrtrem(value a)
{
  CAMLparam1(a);
  CAMLlocal3(q, r, qr);
  q=alloc_init_mpz();
  r=alloc_init_mpz();

  mpz_sqrtrem(*mpz_val(q), *mpz_val(r), *mpz_val(a));

  qr=alloc_tuple(2);
  Store_field(qr, 0, q);
  Store_field(qr, 1, r);
  CAMLreturn(qr);
}
									
z_binary_op_ui(root)

#define z_unary_p(name)				\
value _mlgmp_z_##name(value a)			\
{						\
  CAMLparam1(a);                                \
  CAMLreturn(Val_bool(mpz_##name(*mpz_val(a))));\
}

z_unary_p(perfect_power_p)
z_unary_p(perfect_square_p)

/**** Division */

/* IMPORTANT NOTE:
Storing mpz_val(d) into a temporary pointer won't work because the GC
may move the data when allocating q and r.
*/

#define z_xdivision_op(kind)						\
value _mlgmp_z_##kind##div_qr(value n, value d)				\
{									\
  CAMLparam2(n, d);							\
  CAMLlocal3(q, r, qr);							\
  if (! mpz_sgn(*mpz_val(d)))						\
    division_by_zero();							\
									\
  q=alloc_init_mpz();							\
  r=alloc_init_mpz();							\
									\
  mpz_##kind##div_qr(*mpz_val(q), *mpz_val(r), *mpz_val(n), *mpz_val(d));\
									\
  qr=alloc_tuple(2);							\
  Store_field(qr, 0, q);						\
  Store_field(qr, 1, r);						\
  CAMLreturn(qr);						        \
}									\
									\
value _mlgmp_z_##kind##div_q(value n, value d)				\
{									\
  CAMLparam2(n, d);                                                     \
  CAMLlocal1(q);						      	\
									\
  if (! mpz_sgn(*mpz_val(d)))						\
    division_by_zero();							\
									\
  q=alloc_init_mpz();							\
									\
  mpz_##kind##div_q(*mpz_val(q), *mpz_val(n), *mpz_val(d));	       	\
									\
  CAMLreturn(q);	       						\
}									\
									\
value _mlgmp_z2_##kind##div_q(value q, value n, value d)		\
{									\
  CAMLparam3(q, n, d);                                                  \
									\
  if (! mpz_sgn(*mpz_val(d)))						\
    division_by_zero();							\
									\
  mpz_##kind##div_q(*mpz_val(q), *mpz_val(n), *mpz_val(d));	       	\
									\
  CAMLreturn(Val_unit);	       						\
}									\
									\
value _mlgmp_z_##kind##div_r(value n, value d)				\
{									\
  CAMLparam2(n, d);                                                     \
  CAMLlocal1(r);						      	\
									\
  if (! mpz_sgn(*mpz_val(d)))						\
    division_by_zero();							\
									\
  r=alloc_init_mpz();							\
									\
  mpz_##kind##div_r(*mpz_val(r), *mpz_val(n), *mpz_val(d));	       	\
									\
  CAMLreturn(r);	       						\
}									\
									\
value _mlgmp_z2_##kind##div_r(value r, value n, value d)      		\
{									\
  CAMLparam3(r, n, d);                                                     \
									\
  if (! mpz_sgn(*mpz_val(d)))						\
    division_by_zero();							\
									\
  mpz_##kind##div_r(*mpz_val(r), *mpz_val(n), *mpz_val(d));	       	\
									\
  CAMLreturn(Val_unit);	       						\
}									\
									\
value _mlgmp_z_##kind##div_qr_ui(value n, value d)			\
{									\
  CAMLparam2(n, d);                                                     \
  CAMLlocal3(q, r, qr);							\
  unsigned long int ui_d = Long_val(d);					\
									\
  if (! ui_d) division_by_zero();					\
									\
  q=alloc_init_mpz();							\
  r=alloc_init_mpz();							\
									\
  mpz_##kind##div_qr_ui(*mpz_val(q), *mpz_val(r), *mpz_val(n), ui_d);	\
									\
  qr=alloc_tuple(2);							\
  Store_field(qr, 0, q);						\
  Store_field(qr, 1, r);						\
  CAMLreturn(qr);	       						\
}									\
									\
value _mlgmp_z_##kind##div_q_ui(value n, value d)			\
{									\
  CAMLparam2(n, d);                                                     \
  CAMLlocal1(q);       							\
  unsigned long int ui_d = Long_val(d);					\
									\
 if (! ui_d) division_by_zero();					\
									\
  q=alloc_init_mpz();							\
									\
  mpz_##kind##div_q_ui(*mpz_val(q), *mpz_val(n), ui_d);			\
									\
  CAMLreturn(q);	       						\
}									\
									\
value _mlgmp_z2_##kind##div_q_ui(value q, value n, value d)		\
{									\
  CAMLparam3(q, n, d);                                                     \
  unsigned long int ui_d = Long_val(d);					\
									\
 if (! ui_d) division_by_zero();					\
									\
  mpz_##kind##div_q_ui(*mpz_val(q), *mpz_val(n), ui_d);			\
									\
  CAMLreturn(Val_unit);	       						\
}									\
									\
value _mlgmp_z_##kind##div_r_ui(value n, value d)			\
{									\
  CAMLparam2(n, d);                                                     \
  CAMLlocal1(r);       							\
  unsigned long int ui_d = Long_val(d);					\
									\
  if (! ui_d) division_by_zero();					\
									\
  r=alloc_init_mpz();							\
									\
  mpz_##kind##div_r_ui(*mpz_val(r), *mpz_val(n), ui_d);			\
									\
  CAMLreturn(r);	       						\
}									\
									\
value _mlgmp_z2_##kind##div_r_ui(value r, value n, value d)		\
{									\
  CAMLparam3(r, n, d);                                                  \
  unsigned long int ui_d = Long_val(d);					\
									\
 if (! ui_d) division_by_zero();					\
									\
  mpz_##kind##div_r_ui(*mpz_val(r), *mpz_val(n), ui_d);			\
									\
  CAMLreturn(Val_unit);	       						\
}									\
									\
value _mlgmp_z_##kind##div_ui(value n, value d)				\
{									\
  CAMLparam2(n, d);                                                     \
  unsigned long int ui_d = Long_val(d);					\
									\
  if (! ui_d) division_by_zero();					\
									\
  CAMLreturn(Val_int(mpz_##kind##div_ui(*mpz_val(n), ui_d)));	        \
}

z_xdivision_op(t)
z_xdivision_op(f)
z_xdivision_op(c)

#define z_division_op(op)			\
value _mlgmp_z_##op(value n, value d)		\
{						\
  CAMLparam2(n, d);				\
  CAMLlocal1(q);				\
						\
  if (! mpz_sgn(*mpz_val(d)))			\
    division_by_zero();				\
						\
  q=alloc_init_mpz();				\
						\
  mpz_##op(*mpz_val(q), *mpz_val(n), *mpz_val(d)); \
						\
  CAMLreturn(q);				\
}						\
						\
value _mlgmp_z2_##op(value q, value n, value d)	\
{						\
  CAMLparam3(q, n, d);				\
						\
  if (! mpz_sgn(*mpz_val(d)))			\
    division_by_zero();				\
						\
  mpz_##op(*mpz_val(q), *mpz_val(n), *mpz_val(d)); \
						\
  CAMLreturn(Val_unit);				\
}

#define z_division_op_ui(op)			\
value _mlgmp_z_##op(value n, value d)		\
{						\
  CAMLparam2(n, d);				\
  CAMLlocal1(q);				\
  unsigned int ld = Long_val(d);			\
						\
  if (! ld)	                 		\
    division_by_zero();				\
						\
  q=alloc_init_mpz();				\
						\
  mpz_##op(*mpz_val(q), *mpz_val(n), ld);	\
						\
  CAMLreturn(q);				\
}						\
						\
value _mlgmp_z2_##op(value q, value n, value d)	\
{						\
  CAMLparam3(q, n, d);				\
  unsigned int ld = Long_val(d);			\
						\
  if (! ld)			                \
    division_by_zero();				\
						\
  mpz_##op(*mpz_val(q), *mpz_val(n), ld);	\
						\
  CAMLreturn(Val_unit);				\
}

z_division_op(divexact)
z_division_op(mod)
z_division_op_ui(mod_ui)

/*** Shift ops */
#define z_shift_op(type)				\
value _mlgmp_z_##type(value a, value shift)		\
{                                                       \
  CAMLparam2(a, shift);                                 \
  CAMLlocal1(r);					\
  r=alloc_init_mpz();   				\
  mpz_##type(*mpz_val(r), *mpz_val(a), Int_val(shift));	\
  CAMLreturn(r);       					\
}                                                       \
                                                        \
value _mlgmp_z2_##type(value r, value a, value shift)	\
{                                                       \
  CAMLparam3(r, a, shift);                              \
  mpz_##type(*mpz_val(r), *mpz_val(a), Int_val(shift));	\
  CAMLreturn(Val_unit);     				\
}

#define z_shift_op_unimplemented(type)			\
value _mlgmp_z_##type(value a, value shift)		\
{                                                       \
  CAMLparam2(a, shift);                                 \
  CAMLreturn0();       					\
}                                                       \
                                                        \
value _mlgmp_z2_##type(value r, value a, value shift)	\
{                                                       \
  CAMLparam3(r, a, shift);                              \
  unimplemented(z2_##type);                             \
  CAMLreturn0();     				        \
}

z_shift_op(mul_2exp)
z_shift_op(tdiv_q_2exp)
z_shift_op(tdiv_r_2exp)
z_shift_op(fdiv_q_2exp)
z_shift_op(fdiv_r_2exp)

#if __GNU_MP_VERSION >= 4
z_shift_op(cdiv_q_2exp)
z_shift_op(cdiv_r_2exp)
#else
z_shift_op_unimplemented(cdiv_q_2exp)
z_shift_op_unimplemented(cdiv_r_2exp)
#endif

/*** Comparisons */

int _mlgmp_z_custom_compare(value a, value b)
{
  CAMLparam2(a, b);
  CAMLreturn(mpz_cmp(*mpz_val(a), *mpz_val(b)));
}

value _mlgmp_z_compare(value a, value b)
{
  CAMLparam2(a, b);
  CAMLreturn(Val_int(mpz_cmp(*mpz_val(a), *mpz_val(b))));
}

value _mlgmp_z_compare_si(value a, value b)
{
  CAMLparam2(a, b);
  CAMLreturn(Val_int(mpz_cmp_si(*mpz_val(a), Long_val(b))));
}

/*** Number theory */

value _mlgmp_z_probab_prime_p(value n, value reps)
{
  CAMLparam2(n, reps);
  CAMLreturn(Val_bool(mpz_probab_prime_p(*mpz_val(n), Int_val(reps))));
}

z_unary_op(nextprime)

z_binary_op(gcd)
z_binary_op_mpz(lcm)

value  _mlgmp_z_gcdext(value a, value b)
{
  CAMLparam2(a, b);
  CAMLlocal4(g, s, t, r);
  g=alloc_init_mpz();  
  s=alloc_init_mpz();  
  t=alloc_init_mpz();
  mpz_gcdext(*mpz_val(g), *mpz_val(s), *mpz_val(t), *mpz_val(a), *mpz_val(b));
  r=alloc_tuple(3);
  Store_field(r, 0, g);
  Store_field(r, 1, s);
  Store_field(r, 2, t);
  CAMLreturn(r);
}						     

value  _mlgmp_z_invert(value a, value b)
{
  CAMLparam2(a, b);
  CAMLlocal2(i, r);
  i = alloc_init_mpz();
  if (! mpz_invert(*mpz_val(i),*mpz_val(a), *mpz_val(b)))
    {
      r=Val_false;
    }
  else
    {
      r=alloc_tuple(1);
      Store_field(r, 0, i);
    }
  CAMLreturn(r);
}						     

#define z_int_binary_op(op)					\
value _mlgmp_z_##op(value a, value b)				\
{								\
  CAMLparam2(a, b);						\
  CAMLreturn(Val_int(mpz_##op(*mpz_val(a), *mpz_val(b))));	\
}

z_int_binary_op(legendre)
z_int_binary_op(jacobi)

value _mlgmp_z_kronecker_si(value a, value b)
{
  CAMLparam2(a, b);
  CAMLreturn(Val_int(mpz_kronecker_si(*mpz_val(a), Long_val(b))));
}

value _mlgmp_z_si_kronecker(value a, value b)
{
  CAMLparam2(a, b);
  CAMLreturn(Val_int(mpz_si_kronecker(Long_val(a), *mpz_val(b))));
}

value _mlgmp_z_remove(value a, value b)
{
  int x;
  CAMLparam2(a, b);
  CAMLlocal2(f, r);
  f = alloc_init_mpz();
  x = mpz_remove(*mpz_val(f), *mpz_val(a), *mpz_val(b));
  r=alloc_tuple(2);
  Store_field(r, 0, f);
  Store_field(r, 1, Val_int(x));
  CAMLreturn(r);
}

#define z_unary_op_ui(op)			\
value _mlgmp_z_##op(value a)			\
{						\
  CAMLparam1(a);				\
  CAMLlocal1(r);				\
  r = alloc_init_mpz();				\
  mpz_##op(*mpz_val(r), Long_val(a));		\
  CAMLreturn(r);				\
}

z_unary_op_ui(fac_ui)
z_unary_op_ui(fib_ui)
z_binary_op_ui(bin_ui)

value _mlgmp_z_bin_uiui(value n, value k)
{
  CAMLparam2(n, k);
  CAMLlocal1(r);
  r = alloc_init_mpz();
  mpz_bin_uiui(*mpz_val(r), Long_val(n), Long_val(k));
  CAMLreturn(r);
}

#define z_int_unary_op(op)			\
value _mlgmp_z_##op(value a)			\
{						\
  CAMLparam1(a);				\
  CAMLreturn(Val_int(mpz_##op(*mpz_val(a))));	\
}

z_int_unary_op(sgn)

z_binary_op_mpz(and)
z_binary_op_mpz(ior)
z_binary_op_mpz(xor)
z_unary_op(com)

z_int_unary_op(popcount)
z_int_binary_op(hamdist)

#define z_int_binary_op_ui(op)					\
value _mlgmp_z_##op(value a, value b)				\
{								\
  CAMLparam2(a, b);						\
  CAMLreturn(Val_int(mpz_##op(*mpz_val(a), Long_val(b))));	\
}

z_int_binary_op_ui(scan0)
z_int_binary_op_ui(scan1)

/*** Random */
#define z_random_op_ui(op)					\
value _mlgmp_z_##op(value state, value n)			\
{								\
  CAMLparam2(state, n);						\
  CAMLlocal1(r);						\
  r = alloc_init_mpz();						\
  mpz_##op(*mpz_val(r), *randstate_val(state), Long_val(n));	\
  CAMLreturn(r);						\
}

#define z_random_op(op)			        		\
value _mlgmp_z_##op(value state, value n)			\
{								\
  CAMLparam2(state, n);						\
  CAMLlocal1(r);						\
  r = alloc_init_mpz();						\
  mpz_##op(*mpz_val(r), *randstate_val(state), *mpz_val(n));	\
  CAMLreturn(r);						\
}

z_random_op_ui(urandomb)
z_random_op(urandomm)
z_random_op_ui(rrandomb)

/*** Serialization */
value _mlgmp_z_initialize()
{
  CAMLparam0();
  register_custom_operations(& _mlgmp_custom_z);
  CAMLreturn(Val_unit);
}

#ifdef SERIALIZE
void _mlgmp_z_serialize(value v,
			unsigned long * wsize_32,
			unsigned long * wsize_64)
{
  CAMLparam1(v);
  char *s;
  int len;

  *wsize_32 = MPZ_SIZE_ARCH32;
  *wsize_64 = MPZ_SIZE_ARCH64;

  s = mpz_get_str (NULL, 16, *mpz_val(v));
  len = strlen(s);
  serialize_int_4(len);
  serialize_block_1(s, len);

  free(s);
  CAMLreturn0;
}

unsigned long _mlgmp_z_deserialize(void * dst)
{
  char *s;
  int len;

  len = deserialize_uint_4();
  s = malloc(len+1);
  deserialize_block_1(s, len);
  s[len] = 0;
  mpz_init_set_str (*((mpz_t*) dst), s, 16);
  free(s);

  return sizeof(mpz_t);
}
#endif

/* Hash */

long _mlgmp_z_hash(value v)
{
  CAMLparam1(v);
  mpz_t dummy;
  long r;
  mpz_init(dummy);

  r = mpz_mod_ui(dummy, *mpz_val(v), HASH_MODULUS);

  mpz_clear(dummy);
  CAMLreturn(r);
}