Skip to content

Commit 82e2d92

Browse files
committed
Runtime: normalize nan when hashing floats
1 parent 9f9afcf commit 82e2d92

File tree

2 files changed

+22
-6
lines changed

2 files changed

+22
-6
lines changed

compiler/tests-ocaml/lib-hashtbl/hfun_js.expected

+3-3
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212
-0.0 07be548a
1313
+infty 23ea56fb
1414
-infty 059f7872
15-
NaN 23498e72
16-
NaN#2 23498e72
17-
NaN#3 23498e72
15+
NaN 3228858d
16+
NaN#2 3228858d
17+
NaN#3 3228858d
1818
-- Native integers:
1919
0 07be548a
2020
-1 3653e015

runtime/js/hash.js

+19-3
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,25 @@ function caml_hash_mix_final(h) {
4141
}
4242

4343
//Provides: caml_hash_mix_float
44-
//Requires: caml_int64_bits_of_float, caml_hash_mix_int64
45-
function caml_hash_mix_float(h, v0) {
46-
return caml_hash_mix_int64(h, caml_int64_bits_of_float(v0));
44+
//Requires: caml_int64_bits_of_float
45+
//Requires: caml_hash_mix_int
46+
//Requires: caml_int64_lo32, caml_int64_hi32
47+
function caml_hash_mix_float(hash, v0) {
48+
var i64 = caml_int64_bits_of_float(v0);
49+
var l = caml_int64_lo32(i64);
50+
var h = caml_int64_hi32(i64);
51+
/* Normalize NaNs */
52+
if ((h & 0x7FF00000) == 0x7FF00000 && (l | (h & 0xFFFFF)) != 0) {
53+
h = 0x7FF00000;
54+
l = 0x00000001;
55+
}
56+
/* Normalize -0 into +0 */
57+
else if (h == (0x80000000 | 0) && l == 0) {
58+
h = 1;
59+
}
60+
hash = caml_hash_mix_int(hash, l);
61+
hash = caml_hash_mix_int(hash, h);
62+
return hash;
4763
}
4864
//Provides: caml_hash_mix_int64
4965
//Requires: caml_hash_mix_int

0 commit comments

Comments
 (0)