Package: mlgmp / 20021123-19

0003-Various-bugfixes-in-C-bindings.patch Patch series | download
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
From: Mike Furr <mfurr@debian.org>
Date: Mon, 17 Jul 2017 09:52:55 +0200
Subject: Various bugfixes in C bindings

---
 gmp.ml        |  3 ++-
 gmp.mli       |  3 ++-
 mlgmp_z.c     | 13 ++++++++++---
 test_suite.ml |  6 ++++++
 4 files changed, 20 insertions(+), 5 deletions(-)

diff --git a/gmp.ml b/gmp.ml
index e423868..135c3dc 100644
--- a/gmp.ml
+++ b/gmp.ml
@@ -66,7 +66,7 @@ module Z2 = struct
   external from_float: dest: t->float->unit = "_mlgmp_z2_from_float";;
 
   external create: unit->t = "_mlgmp_z_create";;
-  external copy: dest: t-> from: t-> unit = "_mlgmp_z_copy";;
+  external copy: dest: t-> from: t-> unit = "_mlgmp_z2_copy";;
   external add: dest: t-> t->t->unit = "_mlgmp_z2_add";;
   external sub: dest: t-> t->t->unit = "_mlgmp_z2_sub";;
   external mul: dest: t-> t->t->unit = "_mlgmp_z2_mul";;
@@ -85,6 +85,7 @@ end;;
 
 module Z = struct
   type t = Z2.t;;
+  external copy: t->t = "_mlgmp_z_copy";;
   external of_int: int->t = "_mlgmp_z_from_int";;
   external from_int: int->t = "_mlgmp_z_from_int";;
   external from_string_base: base: int->string->t="_mlgmp_z_from_string_base";;
diff --git a/gmp.mli b/gmp.mli
index 2d47f1f..9a660e4 100644
--- a/gmp.mli
+++ b/gmp.mli
@@ -14,7 +14,7 @@ module Z2 :
       = "_mlgmp_z2_from_string_base"
     external from_float : dest:t -> float -> unit = "_mlgmp_z2_from_float"
     external create : unit -> t = "_mlgmp_z_create"
-    external copy : dest:t -> from:t -> unit = "_mlgmp_z_copy"
+    external copy : dest:t -> from:t -> unit = "_mlgmp_z2_copy"
     external add : dest:t -> t -> t -> unit = "_mlgmp_z2_add"
     external sub : dest:t -> t -> t -> unit = "_mlgmp_z2_sub"
     external mul : dest:t -> t -> t -> unit = "_mlgmp_z2_mul"
@@ -31,6 +31,7 @@ module Z2 :
 module Z :
   sig
     type t = Z2.t
+    external copy : t -> t = "_mlgmp_z_copy"
     external from_int : int -> t = "_mlgmp_z_from_int"
     external of_int : int -> t = "_mlgmp_z_from_int"
     external from_string_base : base:int -> string -> t
diff --git a/mlgmp_z.c b/mlgmp_z.c
index 7f81a80..35c8583 100644
--- a/mlgmp_z.c
+++ b/mlgmp_z.c
@@ -58,6 +58,13 @@ value _mlgmp_z_copy(value 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);
@@ -88,21 +95,21 @@ value _mlgmp_z_from_float(value ml_val)
 value _mlgmp_z2_from_int(value r, value ml_val)
 {
   CAMLparam2(r, ml_val);
-  mpz_init_set_si(*mpz_val(r), Int_val(ml_val));
+  mpz_set_si(*mpz_val(r), Int_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_init_set_str(*mpz_val(r), String_val(ml_val), Int_val(base));
+  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_init_set_d(*mpz_val(r), Double_val(ml_val));
+  mpz_set_d(*mpz_val(r), Double_val(ml_val));
   CAMLreturn(Val_unit);
 }
 
diff --git a/test_suite.ml b/test_suite.ml
index 64ffcdd..b450386 100644
--- a/test_suite.ml
+++ b/test_suite.ml
@@ -1,5 +1,11 @@
 open Gmp;;
 
+assert ((Z.from_int 578) = (let r = Z2.create () in Z2.copy r (Z.from_int 578); r));
+assert ((Z.from_int 578) = (let r = Z2.create () in Z2.from_int r 578; r));
+assert ((Z.from_int 578) = (let r = Z2.create () in Z2.from_string_base r 10 "578"; r));
+(*to be used only with corrected sources *)
+(*assert ((Z.from_int 578) = (Z.copy (Z.from_int 578)));*)
+assert ((Z.from_int 578) = (let r = Z2.create () in Z2.from_float r 578.; r));
 assert ((Z.from_int 578) = (Z.from_string_base ~base: 10 "578"));
 assert ((Z.from_int 578) = (Z.from_float 578.));
 assert ((Z.to_string_base ~base: 10 (Z.from_int 578)) = "578");