Floats live in the existing 8-byte stack slots as raw bits and are computed with scalar SSE ops via xmm0/xmm1. Adds consts, loads/stores, comparisons (NaN-correct), abs/neg/copysign, ceil/floor/trunc/nearest (roundss/sd), sqrt, add/sub/mul/div, wasm-semantics min/max (NaN propagating, -0 < +0), int<->float conversions (incl. unsigned 64-bit), demote/promote, reinterpret, and float globals. Trapping truncation traps on NaN (new 'invalid conversion to integer' message) and on out-of-range values. Adds examples/float.wat (52 self-checks) and examples/fptrap.wat. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
79 lines
6.2 KiB
WebAssembly Text Format
79 lines
6.2 KiB
WebAssembly Text Format
;; f32/f64 arithmetic, comparisons, rounding, min/max, conversions, memory and globals.
|
|
;; Exits 0 if all checks pass, otherwise with the number of the first failing check.
|
|
(module
|
|
(memory 1)
|
|
(global $g (mut f64) (f64.const 1.5))
|
|
(global $h (mut f32) (f32.const 2.5))
|
|
(func $nan64 (result f64) (f64.div (f64.const 0) (f64.const 0)))
|
|
(func $nan32 (result f32) (f32.div (f32.const 0) (f32.const 0)))
|
|
(func $hyp (param $a f64) (param $b f64) (result f64)
|
|
(f64.sqrt (f64.add (f64.mul (local.get $a) (local.get $a)) (f64.mul (local.get $b) (local.get $b)))))
|
|
(func (export "main") (result i32)
|
|
(local $x f32)
|
|
;; 1-4: basic arithmetic
|
|
(if (f64.ne (call $hyp (f64.const 3) (f64.const 4)) (f64.const 5)) (then (return (i32.const 1))))
|
|
(if (f32.ne (f32.mul (f32.const 1.5) (f32.const 4)) (f32.const 6)) (then (return (i32.const 2))))
|
|
(if (f64.ne (f64.div (f64.const 1) (f64.const 4)) (f64.const 0.25)) (then (return (i32.const 3))))
|
|
(if (f32.ne (f32.sub (f32.const 0.5) (f32.const 2)) (f32.const -1.5)) (then (return (i32.const 4))))
|
|
;; 5-8: comparisons incl. NaN
|
|
(if (i32.eqz (f64.lt (f64.const 1) (f64.const 2))) (then (return (i32.const 5))))
|
|
(if (i32.eqz (f64.ge (f64.const 2) (f64.const 2))) (then (return (i32.const 6))))
|
|
(if (f64.eq (call $nan64) (call $nan64)) (then (return (i32.const 7))))
|
|
(if (i32.eqz (f64.ne (call $nan64) (f64.const 1))) (then (return (i32.const 8))))
|
|
(if (f64.lt (call $nan64) (f64.const 1)) (then (return (i32.const 9))))
|
|
(if (f64.le (f64.const 1) (call $nan64)) (then (return (i32.const 10))))
|
|
(if (f32.gt (call $nan32) (f32.const 1)) (then (return (i32.const 11))))
|
|
(if (i32.eqz (f32.le (f32.const 1) (f32.const 1))) (then (return (i32.const 12))))
|
|
;; 13-18: rounding
|
|
(if (f64.ne (f64.ceil (f64.const 1.2)) (f64.const 2)) (then (return (i32.const 13))))
|
|
(if (f64.ne (f64.floor (f64.const -1.2)) (f64.const -2)) (then (return (i32.const 14))))
|
|
(if (f64.ne (f64.trunc (f64.const -1.7)) (f64.const -1)) (then (return (i32.const 15))))
|
|
(if (f64.ne (f64.nearest (f64.const 2.5)) (f64.const 2)) (then (return (i32.const 16))))
|
|
(if (f64.ne (f64.nearest (f64.const 3.5)) (f64.const 4)) (then (return (i32.const 17))))
|
|
(if (f32.ne (f32.nearest (f32.const -0.5)) (f32.const -0)) (then (return (i32.const 18))))
|
|
;; 19-24: abs / neg / copysign / min / max (signed zero and NaN)
|
|
(if (f64.ne (f64.abs (f64.const -3)) (f64.const 3)) (then (return (i32.const 19))))
|
|
(if (f32.ne (f32.neg (f32.const 3)) (f32.const -3)) (then (return (i32.const 20))))
|
|
(if (f64.ne (f64.copysign (f64.const 2) (f64.const -0)) (f64.const -2)) (then (return (i32.const 21))))
|
|
(if (f64.ne (f64.min (f64.const 1) (f64.const 2)) (f64.const 1)) (then (return (i32.const 22))))
|
|
(if (f64.ne (f64.max (f64.const 1) (f64.const 2)) (f64.const 2)) (then (return (i32.const 23))))
|
|
(if (i64.ne (i64.reinterpret_f64 (f64.min (f64.const 0) (f64.const -0))) (i64.const 0x8000000000000000)) (then (return (i32.const 24))))
|
|
(if (i64.ne (i64.reinterpret_f64 (f64.max (f64.const -0) (f64.const 0))) (i64.const 0)) (then (return (i32.const 25))))
|
|
(if (f64.eq (f64.min (call $nan64) (f64.const 1)) (f64.const 1)) (then (return (i32.const 26))))
|
|
(if (f64.eq (f64.max (f64.const 1) (call $nan64)) (f64.const 1)) (then (return (i32.const 27))))
|
|
(if (f32.ne (f32.min (f32.const 3) (f32.const -4)) (f32.const -4)) (then (return (i32.const 28))))
|
|
;; 29-34: int -> float
|
|
(if (f64.ne (f64.convert_i32_s (i32.const -7)) (f64.const -7)) (then (return (i32.const 29))))
|
|
(if (f64.ne (f64.convert_i32_u (i32.const -1)) (f64.const 4294967295)) (then (return (i32.const 30))))
|
|
(if (f64.ne (f64.convert_i64_s (i64.const -5000000000)) (f64.const -5000000000)) (then (return (i32.const 31))))
|
|
(if (f64.ne (f64.convert_i64_u (i64.const -1)) (f64.const 18446744073709551616)) (then (return (i32.const 32))))
|
|
(if (f32.ne (f32.convert_i32_s (i32.const 100)) (f32.const 100)) (then (return (i32.const 33))))
|
|
(if (f32.ne (f32.convert_i64_u (i64.const 0x8000000000000000)) (f32.const 9223372036854775808)) (then (return (i32.const 34))))
|
|
;; 35-44: float -> int (truncating)
|
|
(if (i32.ne (i32.trunc_f64_s (f64.const -3.9)) (i32.const -3)) (then (return (i32.const 35))))
|
|
(if (i32.ne (i32.trunc_f32_u (f32.const 4000000000)) (i32.const -294967296)) (then (return (i32.const 36))))
|
|
(if (i32.ne (i32.trunc_f64_u (f64.const -0.5)) (i32.const 0)) (then (return (i32.const 37))))
|
|
(if (i64.ne (i64.trunc_f64_s (f64.const -9223372036854775808)) (i64.const 0x8000000000000000)) (then (return (i32.const 38))))
|
|
(if (i64.ne (i64.trunc_f64_s (f64.const 1e18)) (i64.const 1000000000000000000)) (then (return (i32.const 39))))
|
|
(if (i64.ne (i64.trunc_f64_u (f64.const 1.8e19)) (i64.const 0xF9CCD8A1C5080000)) (then (return (i32.const 40))))
|
|
(if (i64.ne (i64.trunc_f32_u (f32.const 3e9)) (i64.const 3000000000)) (then (return (i32.const 41))))
|
|
(if (i64.ne (i64.trunc_f32_s (f32.const -2e9)) (i64.const -2000000000)) (then (return (i32.const 42))))
|
|
;; 43-45: demote / promote / reinterpret
|
|
(if (f32.ne (f32.demote_f64 (f64.const 0.1)) (f32.const 0.1)) (then (return (i32.const 43))))
|
|
(if (f64.ne (f64.promote_f32 (f32.const 0.5)) (f64.const 0.5)) (then (return (i32.const 44))))
|
|
(if (i32.ne (i32.reinterpret_f32 (f32.const 1)) (i32.const 0x3f800000)) (then (return (i32.const 45))))
|
|
(if (f32.ne (f32.reinterpret_i32 (i32.const 0x40000000)) (f32.const 2)) (then (return (i32.const 46))))
|
|
;; 47-49: memory
|
|
(f64.store (i32.const 16) (f64.const 6.25))
|
|
(if (f64.ne (f64.load (i32.const 16)) (f64.const 6.25)) (then (return (i32.const 47))))
|
|
(f32.store (i32.const 32) (f32.const -0.75))
|
|
(if (f32.ne (f32.load (i32.const 32)) (f32.const -0.75)) (then (return (i32.const 48))))
|
|
(if (i32.ne (i32.load (i32.const 32)) (i32.const 0xbf400000)) (then (return (i32.const 49))))
|
|
;; 50-52: globals, locals, select
|
|
(global.set $g (f64.mul (global.get $g) (f64.const 2)))
|
|
(if (f64.ne (global.get $g) (f64.const 3)) (then (return (i32.const 50))))
|
|
(if (f32.ne (global.get $h) (f32.const 2.5)) (then (return (i32.const 51))))
|
|
(local.set $x (select (f32.const 7) (f32.const 8) (i32.const 0)))
|
|
(if (f32.ne (local.get $x) (f32.const 8)) (then (return (i32.const 52))))
|
|
(i32.const 0)))
|