Skip to content

Commit

Permalink
Merge pull request #165 from WebAssembly/nan-syntax
Browse files Browse the repository at this point in the history
Change nan literal syntax to avoid including parens.
  • Loading branch information
sunfishcode committed Nov 6, 2015
2 parents 6717e74 + be0c38e commit 0e02e3d
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 39 deletions.
2 changes: 1 addition & 1 deletion ml-proto/host/lexer.mll
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ let float =
| sign "0x" hexdigit+ '.'? hexdigit* 'p' sign digit+
| sign "infinity"
| sign "nan"
| sign "nan(0x" hexdigit+ ")"
| sign "nan:0x" hexdigit+
let text = '"' character* '"'
let name = '$' (letter | digit | '_' | tick | symbol)+

Expand Down
8 changes: 4 additions & 4 deletions ml-proto/spec/float.ml
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,9 @@ struct

let of_signless_string x len =
if x <> "nan" &&
(len > 7) &&
(String.sub x 0 6) = "nan(0x" && (String.get x (len - 1)) = ')' then
(let s = Rep.of_string (String.sub x 4 ((len - 5))) in
(len > 6) &&
(String.sub x 0 6) = "nan:0x" then
(let s = Rep.of_string (String.sub x 4 (len - 4)) in
if s = Rep.zero then
raise (Failure "nan payload must not be zero")
else if Rep.logand s bare_nan <> Rep.zero then
Expand All @@ -172,7 +172,7 @@ struct
let a = abs x in
let af = arith_of_bits a in
if af <> af then
("nan(0x" ^ Rep.print_nan_significand_digits a ^ ")")
("nan:0x" ^ Rep.print_nan_significand_digits a)
else
(* TODO: OCaml's string_of_float is insufficient *)
string_of_float (to_float a)
Expand Down
24 changes: 12 additions & 12 deletions ml-proto/test/float_literals.wast
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
(func $f32.nan (result i32) (i32.reinterpret/f32 (f32.const nan)))
(func $f32.positive_nan (result i32) (i32.reinterpret/f32 (f32.const +nan)))
(func $f32.negative_nan (result i32) (i32.reinterpret/f32 (f32.const -nan)))
(func $f32.plain_nan (result i32) (i32.reinterpret/f32 (f32.const nan(0x400000))))
(func $f32.informally_known_as_plain_snan (result i32) (i32.reinterpret/f32 (f32.const nan(0x200000))))
(func $f32.allones_nan (result i32) (i32.reinterpret/f32 (f32.const -nan(0x7fffff))))
(func $f32.misc_nan (result i32) (i32.reinterpret/f32 (f32.const nan(0x012345))))
(func $f32.misc_positive_nan (result i32) (i32.reinterpret/f32 (f32.const +nan(0x304050))))
(func $f32.misc_negative_nan (result i32) (i32.reinterpret/f32 (f32.const -nan(0x2abcde))))
(func $f32.plain_nan (result i32) (i32.reinterpret/f32 (f32.const nan:0x400000)))
(func $f32.informally_known_as_plain_snan (result i32) (i32.reinterpret/f32 (f32.const nan:0x200000)))
(func $f32.allones_nan (result i32) (i32.reinterpret/f32 (f32.const -nan:0x7fffff)))
(func $f32.misc_nan (result i32) (i32.reinterpret/f32 (f32.const nan:0x012345)))
(func $f32.misc_positive_nan (result i32) (i32.reinterpret/f32 (f32.const +nan:0x304050)))
(func $f32.misc_negative_nan (result i32) (i32.reinterpret/f32 (f32.const -nan:0x2abcde)))
(func $f32.infinity (result i32) (i32.reinterpret/f32 (f32.const infinity)))
(func $f32.positive_infinity (result i32) (i32.reinterpret/f32 (f32.const +infinity)))
(func $f32.negative_infinity (result i32) (i32.reinterpret/f32 (f32.const -infinity)))
Expand All @@ -24,12 +24,12 @@
(func $f64.nan (result i64) (i64.reinterpret/f64 (f64.const nan)))
(func $f64.positive_nan (result i64) (i64.reinterpret/f64 (f64.const +nan)))
(func $f64.negative_nan (result i64) (i64.reinterpret/f64 (f64.const -nan)))
(func $f64.plain_nan (result i64) (i64.reinterpret/f64 (f64.const nan(0x8000000000000))))
(func $f64.informally_known_as_plain_snan (result i64) (i64.reinterpret/f64 (f64.const nan(0x4000000000000))))
(func $f64.allones_nan (result i64) (i64.reinterpret/f64 (f64.const -nan(0xfffffffffffff))))
(func $f64.misc_nan (result i64) (i64.reinterpret/f64 (f64.const nan(0x0123456789abc))))
(func $f64.misc_positive_nan (result i64) (i64.reinterpret/f64 (f64.const +nan(0x3040506070809))))
(func $f64.misc_negative_nan (result i64) (i64.reinterpret/f64 (f64.const -nan(0x2abcdef012345))))
(func $f64.plain_nan (result i64) (i64.reinterpret/f64 (f64.const nan:0x8000000000000)))
(func $f64.informally_known_as_plain_snan (result i64) (i64.reinterpret/f64 (f64.const nan:0x4000000000000)))
(func $f64.allones_nan (result i64) (i64.reinterpret/f64 (f64.const -nan:0xfffffffffffff)))
(func $f64.misc_nan (result i64) (i64.reinterpret/f64 (f64.const nan:0x0123456789abc)))
(func $f64.misc_positive_nan (result i64) (i64.reinterpret/f64 (f64.const +nan:0x3040506070809)))
(func $f64.misc_negative_nan (result i64) (i64.reinterpret/f64 (f64.const -nan:0x2abcdef012345)))
(func $f64.infinity (result i64) (i64.reinterpret/f64 (f64.const infinity)))
(func $f64.positive_infinity (result i64) (i64.reinterpret/f64 (f64.const +infinity)))
(func $f64.negative_infinity (result i64) (i64.reinterpret/f64 (f64.const -infinity)))
Expand Down
8 changes: 4 additions & 4 deletions ml-proto/test/float_memory.wast
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@
)

(assert_return (invoke "store_i32" (i32.const 0x7f800001)) (i32.const 0x7f800001))
(assert_return (invoke "load_f32") (f32.const nan(0x000001)))
(assert_return (invoke "load_f32") (f32.const nan:0x000001))
(assert_return (invoke "store_i32" (i32.const 0x80000000)) (i32.const 0x80000000))
(assert_return (invoke "load_f32") (f32.const -0.0))

(assert_return (invoke "store_f32" (f32.const nan(0x000001))) (f32.const nan(0x000001)))
(assert_return (invoke "store_f32" (f32.const nan:0x000001)) (f32.const nan:0x000001))
(assert_return (invoke "load_i32") (i32.const 0x7f800001))
(assert_return (invoke "store_f32" (f32.const -0.0)) (f32.const -0.0))
(assert_return (invoke "load_i32") (i32.const 0x80000000))
Expand All @@ -54,11 +54,11 @@
)

(assert_return (invoke "store_i64" (i64.const 0x7ff0000000000001)) (i64.const 0x7ff0000000000001))
(assert_return (invoke "load_f64") (f64.const nan(0x0000000000001)))
(assert_return (invoke "load_f64") (f64.const nan:0x0000000000001))
(assert_return (invoke "store_i64" (i64.const 0x8000000000000000)) (i64.const 0x8000000000000000))
(assert_return (invoke "load_f64") (f64.const -0.0))

(assert_return (invoke "store_f64" (f64.const nan(0x0000000000001))) (f64.const nan(0x0000000000001)))
(assert_return (invoke "store_f64" (f64.const nan:0x0000000000001)) (f64.const nan:0x0000000000001))
(assert_return (invoke "load_i64") (i64.const 0x7ff0000000000001))
(assert_return (invoke "store_f64" (f64.const -0.0)) (f64.const -0.0))
(assert_return (invoke "load_i64") (i64.const 0x8000000000000000))
36 changes: 18 additions & 18 deletions ml-proto/test/float_misc.wast
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
(assert_return (invoke "f32.add" (f32.const 0x1.000002p+23) (f32.const 0x1p-1)) (f32.const 0x1.000004p+23))

;; Test that what some systems call signalling NaN behaves as a quiet NaN.
(assert_return_nan (invoke "f32.add" (f32.const nan(0x200000)) (f32.const 1.0)))
(assert_return_nan (invoke "f32.add" (f32.const nan:0x200000) (f32.const 1.0)))

(assert_return (invoke "f64.add" (f64.const 1.1234567890) (f64.const 1.2345e-10)) (f64.const 0x1.1f9add37c11f7p+0))

Expand Down Expand Up @@ -136,7 +136,7 @@
(assert_return (invoke "f64.add" (f64.const -0x0.3f3d1a052fa2bp-1022) (f64.const -0x1.4b78292c7d2adp-1021)) (f64.const -0x1.6b16b62f14fc2p-1021))

;; Test that what some systems call signalling NaN behaves as a quiet NaN.
(assert_return_nan (invoke "f64.add" (f64.const nan(0x4000000000000)) (f64.const 1.0)))
(assert_return_nan (invoke "f64.add" (f64.const nan:0x4000000000000) (f64.const 1.0)))

;; Test for a historic spreadsheet bug.
;; https://blogs.office.com/2007/09/25/calculation-issue-update/
Expand Down Expand Up @@ -260,41 +260,41 @@

(assert_return (invoke "f32.abs" (f32.const nan)) (f32.const nan))
(assert_return (invoke "f32.abs" (f32.const -nan)) (f32.const nan))
(assert_return (invoke "f32.abs" (f32.const nan(0x0f1e2))) (f32.const nan(0x0f1e2)))
(assert_return (invoke "f32.abs" (f32.const -nan(0x0f1e2))) (f32.const nan(0x0f1e2)))
(assert_return (invoke "f32.abs" (f32.const nan:0x0f1e2)) (f32.const nan:0x0f1e2))
(assert_return (invoke "f32.abs" (f32.const -nan:0x0f1e2)) (f32.const nan:0x0f1e2))

(assert_return (invoke "f64.abs" (f64.const nan)) (f64.const nan))
(assert_return (invoke "f64.abs" (f64.const -nan)) (f64.const nan))
(assert_return (invoke "f64.abs" (f64.const nan(0x0f1e27a6b))) (f64.const nan(0x0f1e27a6b)))
(assert_return (invoke "f64.abs" (f64.const -nan(0x0f1e27a6b))) (f64.const nan(0x0f1e27a6b)))
(assert_return (invoke "f64.abs" (f64.const nan:0x0f1e27a6b)) (f64.const nan:0x0f1e27a6b))
(assert_return (invoke "f64.abs" (f64.const -nan:0x0f1e27a6b)) (f64.const nan:0x0f1e27a6b))

(assert_return (invoke "f32.neg" (f32.const nan)) (f32.const -nan))
(assert_return (invoke "f32.neg" (f32.const -nan)) (f32.const nan))
(assert_return (invoke "f32.neg" (f32.const nan(0x0f1e2))) (f32.const -nan(0x0f1e2)))
(assert_return (invoke "f32.neg" (f32.const -nan(0x0f1e2))) (f32.const nan(0x0f1e2)))
(assert_return (invoke "f32.neg" (f32.const nan:0x0f1e2)) (f32.const -nan:0x0f1e2))
(assert_return (invoke "f32.neg" (f32.const -nan:0x0f1e2)) (f32.const nan:0x0f1e2))

(assert_return (invoke "f64.neg" (f64.const nan)) (f64.const -nan))
(assert_return (invoke "f64.neg" (f64.const -nan)) (f64.const nan))
(assert_return (invoke "f64.neg" (f64.const nan(0x0f1e27a6b))) (f64.const -nan(0x0f1e27a6b)))
(assert_return (invoke "f64.neg" (f64.const -nan(0x0f1e27a6b))) (f64.const nan(0x0f1e27a6b)))
(assert_return (invoke "f64.neg" (f64.const nan:0x0f1e27a6b)) (f64.const -nan:0x0f1e27a6b))
(assert_return (invoke "f64.neg" (f64.const -nan:0x0f1e27a6b)) (f64.const nan:0x0f1e27a6b))

(assert_return (invoke "f32.copysign" (f32.const nan) (f32.const nan)) (f32.const nan))
(assert_return (invoke "f32.copysign" (f32.const nan) (f32.const -nan)) (f32.const -nan))
(assert_return (invoke "f32.copysign" (f32.const -nan) (f32.const nan)) (f32.const nan))
(assert_return (invoke "f32.copysign" (f32.const -nan) (f32.const -nan)) (f32.const -nan))
(assert_return (invoke "f32.copysign" (f32.const nan(0x0f1e2)) (f32.const nan)) (f32.const nan(0x0f1e2)))
(assert_return (invoke "f32.copysign" (f32.const nan(0x0f1e2)) (f32.const -nan)) (f32.const -nan(0x0f1e2)))
(assert_return (invoke "f32.copysign" (f32.const -nan(0x0f1e2)) (f32.const nan)) (f32.const nan(0x0f1e2)))
(assert_return (invoke "f32.copysign" (f32.const -nan(0x0f1e2)) (f32.const -nan)) (f32.const -nan(0x0f1e2)))
(assert_return (invoke "f32.copysign" (f32.const nan:0x0f1e2) (f32.const nan)) (f32.const nan:0x0f1e2))
(assert_return (invoke "f32.copysign" (f32.const nan:0x0f1e2) (f32.const -nan)) (f32.const -nan:0x0f1e2))
(assert_return (invoke "f32.copysign" (f32.const -nan:0x0f1e2) (f32.const nan)) (f32.const nan:0x0f1e2))
(assert_return (invoke "f32.copysign" (f32.const -nan:0x0f1e2) (f32.const -nan)) (f32.const -nan:0x0f1e2))

(assert_return (invoke "f64.copysign" (f64.const nan) (f64.const nan)) (f64.const nan))
(assert_return (invoke "f64.copysign" (f64.const nan) (f64.const -nan)) (f64.const -nan))
(assert_return (invoke "f64.copysign" (f64.const -nan) (f64.const nan)) (f64.const nan))
(assert_return (invoke "f64.copysign" (f64.const -nan) (f64.const -nan)) (f64.const -nan))
(assert_return (invoke "f64.copysign" (f64.const nan(0x0f1e27a6b)) (f64.const nan)) (f64.const nan(0x0f1e27a6b)))
(assert_return (invoke "f64.copysign" (f64.const nan(0x0f1e27a6b)) (f64.const -nan)) (f64.const -nan(0x0f1e27a6b)))
(assert_return (invoke "f64.copysign" (f64.const -nan(0x0f1e27a6b)) (f64.const nan)) (f64.const nan(0x0f1e27a6b)))
(assert_return (invoke "f64.copysign" (f64.const -nan(0x0f1e27a6b)) (f64.const -nan)) (f64.const -nan(0x0f1e27a6b)))
(assert_return (invoke "f64.copysign" (f64.const nan:0x0f1e27a6b) (f64.const nan)) (f64.const nan:0x0f1e27a6b))
(assert_return (invoke "f64.copysign" (f64.const nan:0x0f1e27a6b) (f64.const -nan)) (f64.const -nan:0x0f1e27a6b))
(assert_return (invoke "f64.copysign" (f64.const -nan:0x0f1e27a6b) (f64.const nan)) (f64.const nan:0x0f1e27a6b))
(assert_return (invoke "f64.copysign" (f64.const -nan:0x0f1e27a6b) (f64.const -nan)) (f64.const -nan:0x0f1e27a6b))

;; Test that ceil isn't implemented as adding 0.5 and rounding to nearest.
(assert_return (invoke "f32.ceil" (f32.const 0x1.fffffep-1)) (f32.const 1.0))
Expand Down

0 comments on commit 0e02e3d

Please sign in to comment.