Skip to content

Commit 3c01e60

Browse files
committed
To ensure hash function works properly, use Zarith instead
1 parent fd8415c commit 3c01e60

File tree

4 files changed

+36
-221
lines changed

4 files changed

+36
-221
lines changed

stdlib/Files

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
STDLIB_FILES[] =
22
lm_debug
33
lm_nocompare
4-
lm_big_int
54
lm_num
65
lm_rformat
76
lm_rformat_raw

stdlib/OMakefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
#
44
OMakeVersion(0.9.8)
55

6+
OCAMLINCLUDES += $(CAMLLIB)/zarith
7+
68
#
79
# Threads are optional
810
#

stdlib/lm_num.ml

Lines changed: 32 additions & 216 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
(*
2-
* Our slow implementation of numbers
3-
* without using C libraries.
2+
* Compatibility layer to Zarith library.
43
*
54
* ----------------------------------------------------------------
65
*
@@ -37,29 +36,14 @@
3736
* Modified by: Yegor Bryukhov <[email protected]>
3837
* Modified by: Aleksey Nogin <[email protected]>
3938
*)
40-
open Lm_big_int
39+
40+
(* Uses Zarith *)
4141

4242
(************************************************************************
4343
* TYPES *
4444
************************************************************************)
4545

46-
(*
47-
* Have simple ints and big ints.
48-
*)
49-
type num =
50-
Int of int
51-
| Big_int of big_int
52-
53-
(*
54-
* This is the max value represented in an int.
55-
*)
56-
let shift_int = 30
57-
let max_int = pred (1 lsl shift_int)
58-
let min_int = -max_int
59-
60-
let shift_mult_int = 15
61-
let max_mult_int = pred (1 lsl shift_mult_int)
62-
let min_mult_int = -max_mult_int
46+
type num = Z.t
6347

6448
(************************************************************************
6549
* IMPLEMENTATIONS *
@@ -68,193 +52,56 @@ let min_mult_int = -max_mult_int
6852
(*
6953
* Catch overflows in addition.
7054
*)
71-
let add_num i j =
72-
match i, j with
73-
Int i, Int j ->
74-
let sum = i + j in
75-
if (i>0) && (j>0) then
76-
if max_int - i < j then
77-
Big_int (add_big_int (big_int_of_int i) (big_int_of_int j))
78-
else
79-
Int sum
80-
else if (i<0) && (j<0) then
81-
if min_int - i > j then
82-
Big_int (add_big_int (big_int_of_int i) (big_int_of_int j))
83-
else
84-
Int sum
85-
else
86-
Int sum
87-
| Int i, Big_int j ->
88-
Big_int (add_big_int (big_int_of_int i) j)
89-
| Big_int i, Int j ->
90-
Big_int (add_big_int i (big_int_of_int j))
91-
| Big_int i, Big_int j ->
92-
Big_int (add_big_int i j)
55+
let add_num = Z.add
9356

94-
let sub_num i j =
95-
match i, j with
96-
Int i, Int j ->
97-
let diff = i - j in
98-
if (i>0) && (j<0) then
99-
if i > max_int + j then
100-
Big_int (sub_big_int (big_int_of_int i) (big_int_of_int j))
101-
else
102-
Int diff
103-
else if (i<0) && (j>0) then
104-
if i < min_int + j then
105-
Big_int (sub_big_int (big_int_of_int i) (big_int_of_int j))
106-
else
107-
Int diff
108-
else
109-
Int diff
110-
| Int i, Big_int j ->
111-
Big_int (sub_big_int (big_int_of_int i) j)
112-
| Big_int i, Int j ->
113-
Big_int (sub_big_int i (big_int_of_int j))
114-
| Big_int i, Big_int j ->
115-
Big_int (sub_big_int i j)
57+
let sub_num = Z.sub
11658

117-
let succ_num i =
118-
add_num i (Int 1)
59+
let succ_num = Z.succ
11960

120-
let pred_num i =
121-
sub_num i (Int 1)
122-
123-
(*
124-
* Catch overflows in multiplication.
125-
*)
126-
let mult_int i j =
127-
if (i >= min_mult_int) &&
128-
(i <= max_mult_int) &&
129-
(j >= min_mult_int) &&
130-
(j <= max_mult_int)
131-
then
132-
Int (i * j)
133-
else
134-
Big_int (mult_big_int (big_int_of_int i) (big_int_of_int j))
61+
let pred_num = Z.pred
13562

136-
let mult_num i j =
137-
match i, j with
138-
Int i, Int j ->
139-
mult_int i j
140-
| Int i, Big_int j ->
141-
Big_int (mult_big_int (big_int_of_int i) j)
142-
| Big_int i, Int j ->
143-
Big_int (mult_big_int i (big_int_of_int j))
144-
| Big_int i, Big_int j ->
145-
Big_int (mult_big_int i j)
63+
let mult_num = Z.mul
14664

147-
let div_num i j =
148-
match i, j with
149-
_ , Int 0 ->
150-
raise (Invalid_argument "Lm_num.div_num: division by zero")
151-
| Int i, Int j ->
152-
Int (i / j)
153-
| Int i, Big_int j ->
154-
Big_int (div_big_int (big_int_of_int i) j)
155-
| Big_int i, Int j ->
156-
Big_int (div_big_int i (big_int_of_int j))
157-
| Big_int i, Big_int j ->
158-
Big_int (div_big_int i j)
65+
let div_num = Z.div
15966

160-
let mod_num i j =
161-
match i, j with
162-
_ , Int 0 ->
163-
raise (Invalid_argument "Lm_num.mod_num: division by zero")
164-
| Int i, Int j ->
165-
Int (i mod j)
166-
| Int i, Big_int j ->
167-
Big_int (mod_big_int (big_int_of_int i) j)
168-
| Big_int i, Int j ->
169-
Big_int (mod_big_int i (big_int_of_int j))
170-
| Big_int i, Big_int j ->
171-
Big_int (mod_big_int i j)
67+
let mod_num = Z.rem
17268

17369
let quo_num = div_num
17470
let rem_num = mod_num
17571

17672
(*
177-
* Power. We stop large powers here--they will just take
178-
* forever.
73+
* Power.
17974
*)
180-
let power_aux i j =
181-
if j = 0 then
182-
Int 1
183-
else
184-
let rec collect total j =
185-
if j = 0 then
186-
total
187-
else
188-
collect (mult_num total i) (pred j)
189-
in
190-
collect i (pred j)
19175

19276
let power_num i j =
193-
match j with
194-
Int j ->
195-
power_aux i j
196-
| Big_int j ->
197-
if is_integer_big_int j then
198-
power_aux i (integer_big_int j)
199-
else
200-
raise (Invalid_argument "power_num: argument is too big")
77+
if Z.fits_int j then
78+
Z.pow i (Z.to_int j)
79+
else
80+
raise (Invalid_argument "power_num: argument is too big")
20181

20282
(*
20383
* Absolute value.
20484
*)
205-
let abs_num = function
206-
Int i ->
207-
Int (abs i)
208-
| Big_int i ->
209-
Big_int (abs_big_int i)
85+
let abs_num = Z.abs
21086

211-
let neg_num = function
212-
Int i ->
213-
Int (-i)
214-
| Big_int i ->
215-
Big_int (neg_big_int i)
87+
let neg_num = Z.neg
21688

21789
(*
21890
* Equality.
21991
*)
220-
let eq_num i j =
221-
match i, j with
222-
Int i, Int j ->
223-
i = j
224-
| Int i, Big_int j ->
225-
eq_big_int (big_int_of_int i) j
226-
| Big_int i, Int j ->
227-
eq_big_int i (big_int_of_int j)
228-
| Big_int i, Big_int j ->
229-
eq_big_int i j
92+
let eq_num = Z.equal
23093

231-
let compare_num i j =
232-
match i, j with
233-
Int i, Int j ->
234-
Pervasives.compare i j
235-
| Int i, Big_int j ->
236-
compare_big_int (big_int_of_int i) j
237-
| Big_int i, Int j ->
238-
compare_big_int i (big_int_of_int j)
239-
| Big_int i, Big_int j ->
240-
compare_big_int i j
94+
let compare_num = Z.compare
24195

242-
let lt_num i j =
243-
compare_num i j < 0
96+
let lt_num = Z.lt
24497

245-
let le_num i j =
246-
compare_num i j <= 0
98+
let le_num = Z.leq
24799

248-
let gt_num i j =
249-
compare_num i j > 0
100+
let gt_num = Z.gt
250101

251-
let ge_num i j =
252-
compare_num i j >= 0
102+
let ge_num = Z.geq
253103

254-
let is_zero = function
255-
Int 0 -> true
256-
| Int _ -> false
257-
| Big_int i -> is_zero_big_int i
104+
let is_zero n = Z.equal Z.zero n
258105

259106
(************************************************************************
260107
* CONVERSION *
@@ -263,53 +110,22 @@ let is_zero = function
263110
(*
264111
* Integer conversions.
265112
*)
266-
let is_integer_num = function
267-
Int _ ->
268-
true
269-
| Big_int i ->
270-
is_integer_big_int i
113+
let is_integer_num = Z.fits_int
271114

272-
let integer_num = function
273-
Int i ->
274-
i
275-
| Big_int i ->
276-
integer_big_int i
115+
let integer_num = Z.to_int
277116

278-
let num_of_int i =
279-
Int i
117+
let num_of_int = Z.of_int
280118

281119
let int_of_num = integer_num
282120

283121
(*
284122
* String conversions.
285123
*)
286-
let string_of_num = function
287-
Int i ->
288-
string_of_int i
289-
| Big_int i ->
290-
string_of_big_int i
291-
292-
let num_of_string s =
293-
let i = big_int_of_string s in
294-
if is_integer_big_int i then
295-
Int (integer_big_int i)
296-
else
297-
Big_int i
298-
299-
let to_string = string_of_num
300-
let of_string = num_of_string
301-
302-
(*
303-
* Int32 conversions.
304-
*)
305-
let to_int32 = function
306-
Int i ->
307-
Int32.of_int i
308-
| Big_int i ->
309-
Lm_big_int.to_int32 i
124+
let to_string = Z.to_string
125+
let of_string = Z.of_string
310126

311-
let of_int32 i =
312-
Big_int (Lm_big_int.of_int32 i)
127+
let string_of_num = to_string
128+
let num_of_string = of_string
313129

314130
(*
315131
* -*-

stdlib/lm_num.mli

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,10 @@ val integer_num : num -> int
7474
val num_of_int : int -> num
7575
val int_of_num : num -> int
7676

77-
val string_of_num : num -> string
78-
val num_of_string : string -> num
7977
val to_string : num -> string
8078
val of_string : string -> num
81-
val to_int32 : num -> Int32.t
82-
val of_int32 : Int32.t -> num
79+
val string_of_num : num -> string
80+
val num_of_string : string -> num
8381

8482
(*
8583
* -*-

0 commit comments

Comments
 (0)