spirv_mini_interp: binary64 Sqrt/InverseSqrt stay binary64, and OpFAdd/OpFSub/OpFDiv run on doubles (TEST-MINI-INTERP-F64) #856

Manually merged
github merged 1 commit from gh-pr/856/head into gh-pr/856/base 2026-11-26 09:24:44 +00:00
Owner

@⁠qhukz13 on GitHub · 2026-11-24 14:40 UTC · original
qhukz13:cont/mini-interp-f64 → main · +144 −3 · 2 files · 1 commits · merged 2026-11-24 15:50 UTC

Claimed in the coordination issue as: TEST-MINI-INTERP-F64

What and why

tests/semantic/spirv_mini_interp.py mishandled binary64:

  • GLSL.std.450 Sqrt and InverseSqrt rounded a double result to binary32: the translated v_sqrt_f64 of 2.0 gave 0x3ff6a09e60000000 instead of 0x3ff6a09e667f3bcd, and v_rsq_f64 likewise.
  • OpFAdd, OpFSub and OpFDiv on binary64 raised UnsupportedConstruct (only OpFMul ran), so v_add_f64 and v_rcp_f64 could not run in this interpreter at all.

tests/spirv_interp.py, the other interpreter, had the matching OpFDiv gap (#746).

Changes:

  • A double Sqrt is math.sqrt (correctly rounded). A double InverseSqrt is the new rsqrt_f64, rounded once from the exact value through an integer square root: 1.0 / math.sqrt(x) rounds twice and misses by an ulp on about a quarter of random doubles (5,219 of 20,008, checked against a 90-digit decimal; rsqrt_f64 matched all 20,008).
  • Binary64 OpFAdd/OpFSub/OpFDiv follow the binary32 path's rules: a division by zero is still refused as undefined, and a NaN made from non-NaN operands goes through ieee_nan. OpFMul is unchanged.

Tested

  • New tests/semantic/test_mini_interp_f64.py (7 tests): translates v_sqrt_f64, v_rsq_f64, v_rcp_f64 and v_add_f64 and checks the pair against Python's binary64 operations and, for the inverse root, a 60-digit decimal reference (not against rsqrt_f64 itself); rcp of zero stays refused. Against main's interpreter it gives 2 failures and 14 errors.
  • python -I scripts/run_semantic_tests.py tests/semantic: 466 files, 6,115 tests; the only failures are test_sopc_validation and test_unary_f16_validation, which need spirv-as/spirv-val and those are not installed here.
  • The top-level users of spirv_mini_interp: test_fma_mix_probe, test_image_query_translate, test_rdna2_decode, test_spirv_interp_bits, test_spirv_interp_f64_glsl, test_shader_diff_exit, test_wave_diff (stdlib) and test_rdna2spirv_pixelinput, test_u16_high_half (pytest): OK. test_translate.py skips without llvm-mc.
  • tests/test_semantic_translator_use.py, tests/test_text_write_line_endings.py, scripts/check_no_game_data.py, compileall: OK.

Linux, CPU only; no GPU, no game files.

AI-assisted (Claude Code).

🤖 Generated with Claude Code

**@⁠qhukz13** on GitHub · 2026-11-24 14:40 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/856) `qhukz13:cont/mini-interp-f64` → `main` · +144 −3 · 2 files · 1 commits · merged 2026-11-24 15:50 UTC Claimed in the coordination issue as: TEST-MINI-INTERP-F64 ### What and why `tests/semantic/spirv_mini_interp.py` mishandled binary64: - GLSL.std.450 `Sqrt` and `InverseSqrt` rounded a double result to binary32: the translated `v_sqrt_f64` of 2.0 gave `0x3ff6a09e60000000` instead of `0x3ff6a09e667f3bcd`, and `v_rsq_f64` likewise. - `OpFAdd`, `OpFSub` and `OpFDiv` on binary64 raised `UnsupportedConstruct` (only `OpFMul` ran), so `v_add_f64` and `v_rcp_f64` could not run in this interpreter at all. `tests/spirv_interp.py`, the other interpreter, had the matching `OpFDiv` gap (#746). Changes: - A double `Sqrt` is `math.sqrt` (correctly rounded). A double `InverseSqrt` is the new `rsqrt_f64`, rounded once from the exact value through an integer square root: `1.0 / math.sqrt(x)` rounds twice and misses by an ulp on about a quarter of random doubles (5,219 of 20,008, checked against a 90-digit decimal; `rsqrt_f64` matched all 20,008). - Binary64 `OpFAdd`/`OpFSub`/`OpFDiv` follow the binary32 path's rules: a division by zero is still refused as undefined, and a NaN made from non-NaN operands goes through `ieee_nan`. `OpFMul` is unchanged. ### Tested - New `tests/semantic/test_mini_interp_f64.py` (7 tests): translates `v_sqrt_f64`, `v_rsq_f64`, `v_rcp_f64` and `v_add_f64` and checks the pair against Python's binary64 operations and, for the inverse root, a 60-digit decimal reference (not against `rsqrt_f64` itself); `rcp` of zero stays refused. Against `main`'s interpreter it gives 2 failures and 14 errors. - `python -I scripts/run_semantic_tests.py tests/semantic`: 466 files, 6,115 tests; the only failures are `test_sopc_validation` and `test_unary_f16_validation`, which need `spirv-as`/`spirv-val` and those are not installed here. - The top-level users of `spirv_mini_interp`: `test_fma_mix_probe`, `test_image_query_translate`, `test_rdna2_decode`, `test_spirv_interp_bits`, `test_spirv_interp_f64_glsl`, `test_shader_diff_exit`, `test_wave_diff` (stdlib) and `test_rdna2spirv_pixelinput`, `test_u16_high_half` (pytest): OK. `test_translate.py` skips without llvm-mc. - `tests/test_semantic_translator_use.py`, `tests/test_text_write_line_endings.py`, `scripts/check_no_game_data.py`, `compileall`: OK. Linux, CPU only; no GPU, no game files. AI-assisted (Claude Code). 🤖 Generated with [Claude Code](https://claude.com/claude-code)
GLSL.std.450 Sqrt and InverseSqrt rounded a double result to binary32: the translated v_sqrt_f64 of 2.0 gave
0x3ff6a09e60000000 instead of 0x3ff6a09e667f3bcd, and v_rsq_f64 likewise. OpFAdd, OpFSub and OpFDiv on binary64 raised
UnsupportedConstruct, so v_add_f64 and v_rcp_f64 could not run in this interpreter at all.

A double Sqrt is math.sqrt (correctly rounded). A double InverseSqrt is the new rsqrt_f64, rounded once from the
exact value through an integer square root: 1.0 / math.sqrt(x) rounds twice and misses by an ulp on about a quarter
of random doubles (5,219 of 20,008, checked against a 90-digit decimal; rsqrt_f64 matched all of them). Binary64
OpFAdd/OpFSub/OpFDiv follow the binary32 path's rules: a division by zero is still refused as undefined, and a NaN
made from non-NaN operands goes through ieee_nan.

tests/semantic/test_mini_interp_f64.py translates v_sqrt_f64, v_rsq_f64, v_rcp_f64 and v_add_f64 and checks the
pair against Python's binary64 operations and, for the inverse root, a 60-digit decimal reference; against main's
interpreter it gives 2 failures and 14 errors.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 09:24:42 +00:00
github manually merged commit adc25699fd into gh-pr/856/base 2026-11-26 09:24:44 +00:00
Author
Owner

@⁠abnalstlll-byte on GitHub · 2026-11-24 14:43 UTC · original · approved these changes

Audited and verified. Implements exact binary64 arithmetic in spirv_mini_interp.py for OpFAdd, OpFSub, OpFDiv, Sqrt, and single-rounded InverseSqrt (using
sqrt_f64 with rational arithmetic to avoid double rounding). Includes comprehensive numerical validation against high-precision Decimal in ests/semantic/test_mini_interp_f64.py. Clean-room compliant. Approved.

**@⁠abnalstlll-byte** on GitHub · 2026-11-24 14:43 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/856#pullrequestreview-6041356541) · **approved these changes** Audited and verified. Implements exact binary64 arithmetic in spirv_mini_interp.py for OpFAdd, OpFSub, OpFDiv, Sqrt, and single-rounded InverseSqrt (using sqrt_f64 with rational arithmetic to avoid double rounding). Includes comprehensive numerical validation against high-precision Decimal in ests/semantic/test_mini_interp_f64.py. Clean-room compliant. Approved.
Author
Owner

@⁠abnalstlll-byte on GitHub · 2026-11-24 14:44 UTC · original · approved these changes

Clean-Room Audit & Math Verification — PR #856 (TEST-MINI-INTERP-F64)

Superb floating-point implementation and numerical precision verification.

Technical Verification

  1. Binary64 Rounding & Precision:
    • rsqrt_f64(x) correctly avoids double-rounding artifacts by scaling rational fraction 4^p / x with exact integer square root (math.isqrt) and ties-to-even rounding down to 53 bits.
    • Evaluates OpFAdd, OpFSub, OpFDiv, and GLSL Sqrt/InverseSqrt for binary64 operands in spirv_mini_interp.py.
  2. Test Coverage:
    • tests/semantic/test_mini_interp_f64.py validates precision against 60-digit Decimal reference calculations, testing edge cases including subnormals, infinities, and signed zeros.
    • Verified locally with Python: all tests pass.
  3. Clean-Room Compliance:
    • Pure standard library Python math. 100% clean-room.

Verdict: APPROVED

**@⁠abnalstlll-byte** on GitHub · 2026-11-24 14:44 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/856#pullrequestreview-6041372028) · **approved these changes** ### Clean-Room Audit & Math Verification — PR #856 (`TEST-MINI-INTERP-F64`) Superb floating-point implementation and numerical precision verification. #### Technical Verification 1. **Binary64 Rounding & Precision**: - `rsqrt_f64(x)` correctly avoids double-rounding artifacts by scaling rational fraction `4^p / x` with exact integer square root (`math.isqrt`) and ties-to-even rounding down to 53 bits. - Evaluates `OpFAdd`, `OpFSub`, `OpFDiv`, and GLSL `Sqrt`/`InverseSqrt` for binary64 operands in `spirv_mini_interp.py`. 2. **Test Coverage**: - `tests/semantic/test_mini_interp_f64.py` validates precision against 60-digit `Decimal` reference calculations, testing edge cases including subnormals, infinities, and signed zeros. - Verified locally with Python: all tests pass. 3. **Clean-Room Compliance**: - Pure standard library Python math. 100% clean-room. **Verdict: APPROVED**
This discussion has been locked. Commenting is limited to contributors.
No description provided.