Skip to content

Commit 4c085f2

Browse files
committed
avoid nativeint for hash
downsides: it is easy to mix functions with the same type but having different semantics. e.g, `- i ` vs `caml_nativeint_extern.neg i `, the former outputs i32, while the latter could output an overflowed i32 as u32
1 parent 00b960d commit 4c085f2

File tree

6 files changed

+31
-36
lines changed

6 files changed

+31
-36
lines changed

jscomp/runtime/caml_hash.ml

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -77,19 +77,18 @@ let unsafe_pop (q : 'a t) =
7777

7878

7979

80-
external ( +~ ) : nativeint -> nativeint -> nativeint =
81-
"%int32_add"
80+
let caml_hash_mix_int = Caml_hash_primitive.caml_hash_mix_int
81+
let caml_hash_final_mix = Caml_hash_primitive.caml_hash_final_mix
82+
let caml_hash_mix_string = Caml_hash_primitive.caml_hash_mix_string
8283

8384

84-
open Caml_hash_primitive
85-
86-
let caml_hash (count : int) _limit (seed : nativeint)
87-
(obj : Obj.t) : nativeint =
85+
let caml_hash (count : int) _limit (seed : int)
86+
(obj : Obj.t) : int =
8887
let hash = ref seed in
8988
if Js.typeof obj = "number" then
9089
begin
9190
let u = Caml_nativeint_extern.of_float (Obj.magic obj) in
92-
hash.contents <- caml_hash_mix_int hash.contents (u +~ u +~ 1n) ;
91+
hash.contents <- caml_hash_mix_int hash.contents (u + u + 1) ;
9392
caml_hash_final_mix hash.contents
9493
end
9594
else if Js.typeof obj = "string" then
@@ -111,7 +110,7 @@ let caml_hash (count : int) _limit (seed : nativeint)
111110
if Js.typeof obj = "number" then
112111
begin
113112
let u = Caml_nativeint_extern.of_float (Obj.magic obj) in
114-
hash.contents <- caml_hash_mix_int hash.contents (u +~ u +~ 1n) ;
113+
hash.contents <- caml_hash_mix_int hash.contents (u + u + 1) ;
115114
num.contents <- num.contents - 1;
116115
end
117116
else if Js.typeof obj = "string" then
@@ -134,10 +133,10 @@ let caml_hash (count : int) _limit (seed : nativeint)
134133
let tag = (size lsl 10) lor obj_tag in
135134
if obj_tag = 248 (* Obj.object_tag*) then
136135
hash.contents <- caml_hash_mix_int hash.contents
137-
(Caml_nativeint_extern.of_int (Obj.obj (Obj.field obj 1) : int))
136+
(Obj.obj (Obj.field obj 1) : int)
138137
else
139138
begin
140-
hash.contents <- caml_hash_mix_int hash.contents (Caml_nativeint_extern.of_int tag) ;
139+
hash.contents <- caml_hash_mix_int hash.contents tag ;
141140
let block =
142141
let v = size - 1 in if v < num.contents then v else num.contents in
143142
for i = 0 to block do
@@ -154,7 +153,7 @@ let caml_hash (count : int) _limit (seed : nativeint)
154153
}
155154
return size
156155
}|}] obj (fun [@bs] v -> push_back queue v ) [@bs]) in
157-
hash.contents <- caml_hash_mix_int hash.contents (Caml_nativeint_extern.of_int ((size lsl 10) lor 0)) (*tag*) ;
156+
hash.contents <- caml_hash_mix_int hash.contents ((size lsl 10) lor 0) (*tag*) ;
158157
end
159158
done;
160159
caml_hash_final_mix hash.contents

jscomp/runtime/caml_hash.mli

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,4 @@
2626

2727

2828
(** *)
29-
val caml_hash : int -> 'a -> nativeint -> Obj.t -> nativeint
29+
val caml_hash : int -> 'a -> int -> Obj.t -> int

jscomp/runtime/caml_hash_primitive.ml

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,29 +29,25 @@ let (>>>) = Caml_nativeint_extern.shift_right_logical
2929
let (|~) = Caml_nativeint_extern.logor
3030
let (^) = Caml_nativeint_extern.logxor
3131

32-
external ( *~ ) : nativeint -> nativeint -> nativeint = "%int32_mul"
33-
external ( +~ ) : nativeint -> nativeint -> nativeint = "%int32_add"
3432

35-
36-
37-
let rotl32 (x : nativeint) n =
33+
let rotl32 (x : int) n =
3834
(x << n) |~ (x >>> (32 - n))
3935

4036
external (.![]) : string -> int -> int = "charCodeAt" [@@bs.send]
4137
let caml_hash_mix_int h d =
4238
let d = ref d in
43-
d.contents <- d.contents *~ 0xcc9e2d51n ;
39+
d.contents <- d.contents * 0xcc9e2d51 ;
4440
d.contents <- rotl32 d.contents 15 ;
45-
d.contents <- d.contents *~ 0x1b873593n ;
41+
d.contents <- d.contents * 0x1b873593 ;
4642
let h = ref (h ^ d.contents) in
4743
h.contents <- rotl32 h.contents 13 ;
48-
h.contents +~ (h.contents << 2) +~ 0xe6546b64n
44+
h.contents + (h.contents << 2) + 0xe6546b64
4945

5046
let caml_hash_final_mix h =
5147
let h = ref (h ^ (h >>> 16)) in
52-
h.contents <- h.contents *~ 0x85ebca6bn ;
48+
h.contents <- h.contents * 0x85ebca6b ;
5349
h.contents <- h.contents ^ (h.contents >>> 13);
54-
h.contents <- h.contents *~ 0xc2b2ae35n ;
50+
h.contents <- h.contents * 0xc2b2ae35 ;
5551
h.contents ^ (h.contents >>> 16)
5652
(* Caml_nativeint_extern.logand (h.contents ^ (h.contents >>> 16)) 0x3FFFFFFFn *)
5753

@@ -68,7 +64,7 @@ let caml_hash_mix_string h s =
6864
(s.![j+2] lsl 16) lor
6965
(s.![j+3] lsl 24)
7066
in
71-
hash.contents <- caml_hash_mix_int hash.contents (Caml_nativeint_extern.of_int w)
67+
hash.contents <- caml_hash_mix_int hash.contents w
7268
done ;
7369
let modulo = len land 0b11 in
7470
if modulo <> 0 then
@@ -83,9 +79,9 @@ let caml_hash_mix_string h s =
8379
s.![len -2]
8480
else s.![len - 1]
8581
in
86-
hash.contents <- caml_hash_mix_int hash.contents (Caml_nativeint_extern.of_int w)
82+
hash.contents <- caml_hash_mix_int hash.contents w
8783
end;
88-
hash.contents <- hash.contents ^ (Caml_nativeint_extern.of_int len) ;
84+
hash.contents <- hash.contents ^ len ;
8985
hash.contents
9086

9187

jscomp/runtime/caml_hash_primitive.mli

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,6 @@
2222
* along with this program; if not, write to the Free Software
2323
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
2424

25-
val caml_hash_mix_int : nativeint -> nativeint -> nativeint
26-
val caml_hash_mix_string : nativeint -> string -> nativeint
27-
val caml_hash_final_mix : nativeint -> nativeint
25+
val caml_hash_mix_int : int -> int -> int
26+
val caml_hash_mix_string : int -> string -> int
27+
val caml_hash_final_mix : int -> int

lib/es6/caml_hash_primitive.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,19 @@ function rotl32(x, n) {
77

88
function caml_hash_mix_int(h, d) {
99
var d$1 = d;
10-
d$1 = Math.imul(d$1, 3432918353);
10+
d$1 = Math.imul(d$1, -862048943);
1111
d$1 = rotl32(d$1, 15);
1212
d$1 = Math.imul(d$1, 461845907);
1313
var h$1 = h ^ d$1;
1414
h$1 = rotl32(h$1, 13);
15-
return (h$1 + (h$1 << 2) | 0) + 3864292196 | 0;
15+
return (h$1 + (h$1 << 2) | 0) - 430675100 | 0;
1616
}
1717

1818
function caml_hash_final_mix(h) {
1919
var h$1 = h ^ (h >>> 16);
20-
h$1 = Math.imul(h$1, 2246822507);
20+
h$1 = Math.imul(h$1, -2048144789);
2121
h$1 = h$1 ^ (h$1 >>> 13);
22-
h$1 = Math.imul(h$1, 3266489909);
22+
h$1 = Math.imul(h$1, -1028477387);
2323
return h$1 ^ (h$1 >>> 16);
2424
}
2525

lib/js/caml_hash_primitive.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,19 @@ function rotl32(x, n) {
77

88
function caml_hash_mix_int(h, d) {
99
var d$1 = d;
10-
d$1 = Math.imul(d$1, 3432918353);
10+
d$1 = Math.imul(d$1, -862048943);
1111
d$1 = rotl32(d$1, 15);
1212
d$1 = Math.imul(d$1, 461845907);
1313
var h$1 = h ^ d$1;
1414
h$1 = rotl32(h$1, 13);
15-
return (h$1 + (h$1 << 2) | 0) + 3864292196 | 0;
15+
return (h$1 + (h$1 << 2) | 0) - 430675100 | 0;
1616
}
1717

1818
function caml_hash_final_mix(h) {
1919
var h$1 = h ^ (h >>> 16);
20-
h$1 = Math.imul(h$1, 2246822507);
20+
h$1 = Math.imul(h$1, -2048144789);
2121
h$1 = h$1 ^ (h$1 >>> 13);
22-
h$1 = Math.imul(h$1, 3266489909);
22+
h$1 = Math.imul(h$1, -1028477387);
2323
return h$1 ^ (h$1 >>> 16);
2424
}
2525

0 commit comments

Comments
 (0)