Skip to content

Commit b1d0310

Browse files
committed
Runtime: normalize nan when hashing floats
1 parent 2c7814d commit b1d0310

11 files changed

+48
-6
lines changed

compiler/tests-check-prim/main.4.14.output

+3
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,9 @@ caml_gr_text_size
9191
caml_gr_wait_event
9292
caml_gr_window_id
9393

94+
From +hash.js:
95+
caml_hash_mix_int64
96+
9497
From +ints.js:
9598
caml_div
9699
caml_mod

compiler/tests-check-prim/main.5.2.output

+3
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,9 @@ caml_gr_text_size
8787
caml_gr_wait_event
8888
caml_gr_window_id
8989

90+
From +hash.js:
91+
caml_hash_mix_int64
92+
9093
From +ints.js:
9194
caml_div
9295
caml_mod

compiler/tests-check-prim/main.5.3.output

+3
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,9 @@ caml_gr_text_size
8686
caml_gr_wait_event
8787
caml_gr_window_id
8888

89+
From +hash.js:
90+
caml_hash_mix_int64
91+
8992
From +ints.js:
9093
caml_div
9194
caml_mod

compiler/tests-check-prim/unix-Unix.4.14.output

+3
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,9 @@ caml_gr_text_size
167167
caml_gr_wait_event
168168
caml_gr_window_id
169169

170+
From +hash.js:
171+
caml_hash_mix_int64
172+
170173
From +ints.js:
171174
caml_div
172175
caml_mod

compiler/tests-check-prim/unix-Unix.5.2.output

+3
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,9 @@ caml_gr_text_size
163163
caml_gr_wait_event
164164
caml_gr_window_id
165165

166+
From +hash.js:
167+
caml_hash_mix_int64
168+
166169
From +ints.js:
167170
caml_div
168171
caml_mod

compiler/tests-check-prim/unix-Unix.5.3.output

+3
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,9 @@ caml_gr_text_size
162162
caml_gr_wait_event
163163
caml_gr_window_id
164164

165+
From +hash.js:
166+
caml_hash_mix_int64
167+
165168
From +ints.js:
166169
caml_div
167170
caml_mod

compiler/tests-check-prim/unix-Win32.4.14.output

+3
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,9 @@ caml_gr_text_size
139139
caml_gr_wait_event
140140
caml_gr_window_id
141141

142+
From +hash.js:
143+
caml_hash_mix_int64
144+
142145
From +ints.js:
143146
caml_div
144147
caml_mod

compiler/tests-check-prim/unix-Win32.5.2.output

+3
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,9 @@ caml_gr_text_size
136136
caml_gr_wait_event
137137
caml_gr_window_id
138138

139+
From +hash.js:
140+
caml_hash_mix_int64
141+
139142
From +ints.js:
140143
caml_div
141144
caml_mod

compiler/tests-check-prim/unix-Win32.5.3.output

+3
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,9 @@ caml_gr_text_size
135135
caml_gr_wait_event
136136
caml_gr_window_id
137137

138+
From +hash.js:
139+
caml_hash_mix_int64
140+
138141
From +ints.js:
139142
caml_div
140143
caml_mod

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

+18-3
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,24 @@ 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+
} else if (h === (0x80000000 | 0) && l === 0) {
56+
/* Normalize -0 into +0 */
57+
h = 1;
58+
}
59+
hash = caml_hash_mix_int(hash, l);
60+
hash = caml_hash_mix_int(hash, h);
61+
return hash;
4762
}
4863
//Provides: caml_hash_mix_int64
4964
//Requires: caml_hash_mix_int

0 commit comments

Comments
 (0)