Skip to content

Commit 9bb9de9

Browse files
committed
hash are bits operations, they can use int primtives
when using lsr, `x lsr i` -- it's translated into ` (x >>> i | 0)` - FIXME: need check associativity -- seems to be wrong - `| 0` is reduandent since the last operation is `|`
1 parent 4c085f2 commit 9bb9de9

File tree

4 files changed

+12
-14
lines changed

4 files changed

+12
-14
lines changed

Diff for: jscomp/runtime/bs_stdlib_mini.mli

+2-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ external ( lsl ) : int -> int -> int = "%lslint"
2929
external ( lor ) : int -> int -> int = "%orint"
3030
external ( land ) : int -> int -> int = "%andint"
3131
external ( mod ) : int -> int -> int = "%modint"
32-
32+
external ( lsr ) : int -> int -> int = "%lsrint"
33+
external ( lxor ) : int -> int -> int = "%xorint"
3334
type 'a ref = { mutable contents : 'a }
3435
external ref : 'a -> 'a ref = "%makemutable"
3536

Diff for: jscomp/runtime/caml_hash_primitive.ml

+8-11
Original file line numberDiff line numberDiff line change
@@ -24,31 +24,28 @@
2424

2525

2626

27-
let (<< ) = Caml_nativeint_extern.shift_left
28-
let (>>>) = Caml_nativeint_extern.shift_right_logical
29-
let (|~) = Caml_nativeint_extern.logor
30-
let (^) = Caml_nativeint_extern.logxor
27+
3128

3229

3330
let rotl32 (x : int) n =
34-
(x << n) |~ (x >>> (32 - n))
31+
(x lsl n) lor (x lsr (32 - n))
3532

3633
external (.![]) : string -> int -> int = "charCodeAt" [@@bs.send]
3734
let caml_hash_mix_int h d =
3835
let d = ref d in
3936
d.contents <- d.contents * 0xcc9e2d51 ;
4037
d.contents <- rotl32 d.contents 15 ;
4138
d.contents <- d.contents * 0x1b873593 ;
42-
let h = ref (h ^ d.contents) in
39+
let h = ref (h lxor d.contents) in
4340
h.contents <- rotl32 h.contents 13 ;
44-
h.contents + (h.contents << 2) + 0xe6546b64
41+
h.contents + (h.contents lsl 2) + 0xe6546b64
4542

4643
let caml_hash_final_mix h =
47-
let h = ref (h ^ (h >>> 16)) in
44+
let h = ref (h lxor (h lsr 16)) in
4845
h.contents <- h.contents * 0x85ebca6b ;
49-
h.contents <- h.contents ^ (h.contents >>> 13);
46+
h.contents <- h.contents lxor (h.contents lsr 13);
5047
h.contents <- h.contents * 0xc2b2ae35 ;
51-
h.contents ^ (h.contents >>> 16)
48+
h.contents lxor (h.contents lsr 16)
5249
(* Caml_nativeint_extern.logand (h.contents ^ (h.contents >>> 16)) 0x3FFFFFFFn *)
5350

5451
let caml_hash_mix_string h s =
@@ -81,7 +78,7 @@ let caml_hash_mix_string h s =
8178
in
8279
hash.contents <- caml_hash_mix_int hash.contents w
8380
end;
84-
hash.contents <- hash.contents ^ len ;
81+
hash.contents <- hash.contents lxor len ;
8582
hash.contents
8683

8784

Diff for: lib/es6/caml_hash_primitive.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33

44
function rotl32(x, n) {
5-
return (x << n) | (x >>> (32 - n | 0));
5+
return (x << n) | (x >>> (32 - n | 0)) | 0;
66
}
77

88
function caml_hash_mix_int(h, d) {

Diff for: lib/js/caml_hash_primitive.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33

44
function rotl32(x, n) {
5-
return (x << n) | (x >>> (32 - n | 0));
5+
return (x << n) | (x >>> (32 - n | 0)) | 0;
66
}
77

88
function caml_hash_mix_int(h, d) {

0 commit comments

Comments
 (0)