Skip to content

Commit 5f999cb

Browse files
committed
Make NaNs quiet when converting to/from bits
V8 can turn signalling NaNs into quite NaNs, so it is more consistent to hide the difference.
1 parent 015bf73 commit 5f999cb

File tree

2 files changed

+11
-9
lines changed

2 files changed

+11
-9
lines changed

compiler/lib/eval.ml

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,8 @@ let nativeint_shiftop (l : constant list) (f : int32 -> int -> int32) : constant
139139
| [ NativeInt i; Int j ] -> Some (NativeInt (f i (Targetint.to_int_exn j)))
140140
| _ -> None
141141

142+
let quiet_nan n = Int64.logor n 0x00_08_00_00_00_00_00_00L
143+
142144
let eval_prim ~target x =
143145
match x with
144146
| Not, [ Int i ] -> bool (Targetint.is_zero i)
@@ -236,7 +238,9 @@ let eval_prim ~target x =
236238
| `JavaScript ->
237239
let f = Int32.float_of_bits i in
238240
(not (Float.is_nan f))
239-
|| Int64.equal (Int64.bits_of_float f) (Int64.bits_of_float nan)
241+
|| Int64.equal
242+
(quiet_nan (Int64.bits_of_float f))
243+
(Int64.bits_of_float nan)
240244
| `Wasm -> true -> Some (float (Int32.float_of_bits i))
241245
| "caml_int32_of_float", [ Float f ] ->
242246
int32 (Int32.of_float (Int64.float_of_bits f))
@@ -269,7 +273,9 @@ let eval_prim ~target x =
269273
| `JavaScript ->
270274
let f = Int32.float_of_bits i in
271275
(not (Float.is_nan f))
272-
|| Int64.equal (Int64.bits_of_float f) (Int64.bits_of_float nan)
276+
|| Int64.equal
277+
(quiet_nan (Int64.bits_of_float f))
278+
(Int64.bits_of_float nan)
273279
| `Wasm -> true -> Some (float (Int32.float_of_bits i))
274280
| "caml_nativeint_of_float", [ Float f ] ->
275281
nativeint (Int32.of_float (Int64.float_of_bits f))
@@ -299,7 +305,7 @@ let eval_prim ~target x =
299305
when match target with
300306
| `JavaScript ->
301307
(not (Float.is_nan (Int64.float_of_bits i)))
302-
|| Int64.equal i (Int64.bits_of_float nan)
308+
|| Int64.equal (quiet_nan i) (Int64.bits_of_float nan)
303309
| `Wasm -> true -> Some (Float i)
304310
| "caml_int64_of_float", [ Float f ] ->
305311
int64 (Int64.of_float (Int64.float_of_bits f))

compiler/tests-full/stdlib.cma.expected.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8028,7 +8028,6 @@
80288028
caml_floatarray_make = runtime.caml_floatarray_make,
80298029
caml_floatarray_sub = runtime.caml_floatarray_sub,
80308030
caml_hash = runtime.caml_hash,
8031-
caml_int64_create_lo_mi_hi = runtime.caml_int64_create_lo_mi_hi,
80328031
caml_maybe_attach_backtrace = runtime.caml_maybe_attach_backtrace,
80338032
caml_nextafter_float = runtime.caml_nextafter_float,
80348033
caml_signbit_float = runtime.caml_signbit_float,
@@ -8051,10 +8050,7 @@
80518050
Stdlib = global_data.Stdlib,
80528051
infinity = Stdlib[22],
80538052
neg_infinity = Stdlib[23],
8054-
nan = Stdlib[24],
8055-
signaling_nan =
8056-
/*<<float.ml:38:20>>*/ runtime.caml_int64_float_of_bits
8057-
(caml_int64_create_lo_mi_hi(1, 0, 32752));
8053+
nan = Stdlib[24];
80588054
function is_finite(x){
80598055
/*<<float.ml:39:33>>*/ return x - x === 0. ? 1 : 0;
80608056
/*<<float.ml:39:38>>*/ }
@@ -8987,7 +8983,7 @@
89878983
infinity,
89888984
neg_infinity,
89898985
nan,
8990-
signaling_nan,
8986+
NaN,
89918987
nan,
89928988
3.141592653589793,
89938989
max_float,

0 commit comments

Comments
 (0)