Add f32/f64 support #2

Merged
nandi merged 1 commits from geordi/add-float-support into geordi/add-i64-support 2026-11-21 09:56:35 +00:00
Owner

Stacked on #1 (i64 support); the base is geordi/add-i64-support, so retarget to main once that merges.

What

Adds the f32 and f64 value types: params/results/locals/globals/block types, constants, loads/stores, comparisons, abs/neg/copysign, ceil/floor/trunc/nearest, sqrt, add/sub/mul/div, min/max, int<->float conversions, demote/promote and reinterpret.

Why

After i64, floats were the remaining numeric types the compiler rejected, so any module using them failed to compile.

Details

  • Floats live in the existing 8-byte stack slots as raw bits and are computed with scalar SSE ops through xmm0/xmm1, so locals, globals, calls, select and memory needed little change.
  • Comparisons are NaN-correct (all false except ne).
  • min/max follow wasm semantics: NaN propagates and -0 < +0.
  • Rounding uses roundss/roundsd, which needs SSE4.1.
  • Trapping float->int truncation traps on NaN (new "invalid conversion to integer" message) and on out-of-range values. Unsigned 64-bit conversions in both directions are handled explicitly.
  • Not included: trunc_sat, clz/ctz/popcnt, v128 and reference types.

Testing

  • New examples/float.wat has 52 self-checks (arithmetic, NaN comparisons, rounding, signed-zero min/max, all conversions, memory, globals) and exits 0.
  • New examples/fptrap.wat checks that an out-of-range truncation traps (exit 134). I also checked NaN, +/-2^63, 2^64 and f32 2^31 boundaries by hand.
  • i64 and the older examples give the same results as before.
  • wasm-tools wasn't available here, so I assembled the .wat files with the npm wabt package; moon test and just examples were not run.

🤖 Generated with Claude Code

Stacked on #1 (i64 support); the base is `geordi/add-i64-support`, so retarget to `main` once that merges. ## What Adds the f32 and f64 value types: params/results/locals/globals/block types, constants, loads/stores, comparisons, `abs`/`neg`/`copysign`, `ceil`/`floor`/`trunc`/`nearest`, `sqrt`, `add`/`sub`/`mul`/`div`, `min`/`max`, int<->float conversions, `demote`/`promote` and `reinterpret`. ## Why After i64, floats were the remaining numeric types the compiler rejected, so any module using them failed to compile. ## Details - Floats live in the existing 8-byte stack slots as raw bits and are computed with scalar SSE ops through xmm0/xmm1, so locals, globals, calls, `select` and memory needed little change. - Comparisons are NaN-correct (all false except `ne`). - `min`/`max` follow wasm semantics: NaN propagates and -0 < +0. - Rounding uses `roundss`/`roundsd`, which needs SSE4.1. - Trapping float->int truncation traps on NaN (new "invalid conversion to integer" message) and on out-of-range values. Unsigned 64-bit conversions in both directions are handled explicitly. - Not included: `trunc_sat`, clz/ctz/popcnt, v128 and reference types. ## Testing - New `examples/float.wat` has 52 self-checks (arithmetic, NaN comparisons, rounding, signed-zero min/max, all conversions, memory, globals) and exits 0. - New `examples/fptrap.wat` checks that an out-of-range truncation traps (exit 134). I also checked NaN, +/-2^63, 2^64 and f32 2^31 boundaries by hand. - i64 and the older examples give the same results as before. - `wasm-tools` wasn't available here, so I assembled the `.wat` files with the npm `wabt` package; `moon test` and `just examples` were not run. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
nandi added 1 commit 2026-11-21 09:55:53 +00:00
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>
nandi merged commit cc8516a369 into geordi/add-i64-support 2026-11-21 09:56:35 +00:00
Sign in to join this conversation.
No Reviewers
No labels
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: nandi/wasm2elf-mb#2