spirv_interp: handle GLSL IEEE edge domains, FMA sign/overflow, and OpFNegate NaNs #2266

Open
github-bot wants to merge 1 commit from github-bot/gh-2266 into main AGit
Owner

@⁠kirigayakazima on GitHub · 2026-11-26 11:50 UTC · original
kirigayakazima:fix/spirv-interp-ieee-safety → main · +224 −5 · 2 files · 1 commits · open

Claimed in the coordination issue as: SPIRV-INTERP-IEEE-DOMAIN-OVERFLOW-FMA-AND-BITCAST-SIGN (#1608)

What and why

tests/spirv_interp.py previously raised Python exceptions or lost IEEE-754 sign/payload bits on valid edge-case inputs:

  • Sqrt, InverseSqrt, Log2, Sin, Cos, Fract, and Exp2 called raw math.* / 2.0 ** x functions directly, raising ValueError or OverflowError on negative/zero/infinite/huge operands instead of returning IEEE-754 NaN, signed infinity, 0.0, or -0.0.
  • Fma converted Fraction sums unconditionally via float(...), losing -0.0 sign when -0.0 * c + (-0.0) summed to zero and raising OverflowError on overflowing binary64 results.
  • OpFNegate ran unary -v, stripping _F32 bit-pattern payloads (including signalling NaN bits) and failing on tuple vector operands.

This adds IEEE-754 domain/overflow/-0.0-safe evaluation in glsl(), preserves _F32 bit patterns and vector/f64 NaN signs in OpFNegate, and adds regression tests in tests/test_spirv_interp_signs.py.

Tested

  • python -I tests/test_spirv_interp_signs.py -v (11 passed)
  • python -I tests/test_spirv_interp_bits.py -v (5 passed)
  • python -I tests/test_spirv_interp_f64_glsl.py -v (6 passed)
  • Detached upstream/main baseline negative control: failed with the new tests before the fix
  • python -I tests/test_ci_registration.py, python -I tests/test_text_write_line_endings.py, python -I scripts/check_no_game_data.py, python -I -m compileall -q, and git diff --cached --check

GPUs tested: n/a

AI-assisted: yes

Checklist

  • Based on current main and merges cleanly (a conflicting pull request is labeled needs rebase).
  • One topic; follow-up work goes in a new pull request.
  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed.
**@⁠kirigayakazima** on GitHub · 2026-11-26 11:50 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2266) `kirigayakazima:fix/spirv-interp-ieee-safety` → `main` · +224 −5 · 2 files · 1 commits · open Claimed in the coordination issue as: SPIRV-INTERP-IEEE-DOMAIN-OVERFLOW-FMA-AND-BITCAST-SIGN (https://git.gtahub.st/github/ubiquitarianism--leonida/issues/1608) ### What and why `tests/spirv_interp.py` previously raised Python exceptions or lost IEEE-754 sign/payload bits on valid edge-case inputs: - `Sqrt`, `InverseSqrt`, `Log2`, `Sin`, `Cos`, `Fract`, and `Exp2` called raw `math.*` / `2.0 ** x` functions directly, raising `ValueError` or `OverflowError` on negative/zero/infinite/huge operands instead of returning IEEE-754 `NaN`, signed infinity, `0.0`, or `-0.0`. - `Fma` converted `Fraction` sums unconditionally via `float(...)`, losing `-0.0` sign when `-0.0 * c + (-0.0)` summed to zero and raising `OverflowError` on overflowing binary64 results. - `OpFNegate` ran unary `-v`, stripping `_F32` bit-pattern payloads (including signalling NaN bits) and failing on tuple vector operands. This adds IEEE-754 domain/overflow/`-0.0`-safe evaluation in `glsl()`, preserves `_F32` bit patterns and vector/f64 NaN signs in `OpFNegate`, and adds regression tests in `tests/test_spirv_interp_signs.py`. ### Tested - `python -I tests/test_spirv_interp_signs.py -v` (11 passed) - `python -I tests/test_spirv_interp_bits.py -v` (5 passed) - `python -I tests/test_spirv_interp_f64_glsl.py -v` (6 passed) - Detached `upstream/main` baseline negative control: failed with the new tests before the fix - `python -I tests/test_ci_registration.py`, `python -I tests/test_text_write_line_endings.py`, `python -I scripts/check_no_game_data.py`, `python -I -m compileall -q`, and `git diff --cached --check` GPUs tested: n/a AI-assisted: yes ### Checklist - [x] Based on current `main` and merges cleanly (a conflicting pull request is labeled `needs rebase`). - [x] One topic; follow-up work goes in a new pull request. - [x] No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here. - [ ] A new translator rule is in the contract table of `docs/design.md`, with how it was confirmed.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 11:53:03 +00:00
Author
Owner

@⁠real-rusher on GitHub · 2026-11-26 12:23 UTC · original

Verified on main a73a57e1 (Ubuntu 24.04 x86-64, Python 3.13, no GPU). Merges cleanly. With this PR, python -I tests/test_spirv_interp_signs.py: 11 tests OK. The PR's test file against main's interpreter: 1 failure and 2 errors, so the new tests fail on main and pass with the change. compileall clean.

AI-assisted: yes (Claude Code)

**@⁠real-rusher** on GitHub · 2026-11-26 12:23 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2266#issuecomment-7191940616) Verified on main a73a57e1 (Ubuntu 24.04 x86-64, Python 3.13, no GPU). Merges cleanly. With this PR, python -I tests/test_spirv_interp_signs.py: 11 tests OK. The PR's test file against main's interpreter: 1 failure and 2 errors, so the new tests fail on main and pass with the change. compileall clean. AI-assisted: yes (Claude Code)
Author
Owner

@⁠wavehs on GitHub · 2026-11-26 19:45 UTC · original · reviewed

macOS arm64 check of #2266 (spirv_interp IEEE edge domains). A test-oracle change only; the new tests run here and the negative control confirms they bite.

Tested head d1b14075a merged onto main bdbc7e923 as merge e575c7a77 (clean, no conflicts).

Environment: Apple M3 Pro arm64, macOS 27.2.1, CPython 3.15.2.

On the merge

  • python -I tests/test_spirv_interp_signs.py: 11 tests OK. test_spirv_interp_bits.py, test_spirv_interp_f64_glsl.py and test_ci_registration.py also OK, so the new file is selected by CI's standard-library loop (tests/test_ci_registration.py passes).
  • The whole semantic suite on the merge: 500 files, 6367 tests, 0 files failed - unchanged, as expected for a tests/ helper.
  • Every standalone user of tests/spirv_interp.py that can run without pytest passes: test_rcp_iflag_mods.py, test_spirv_interp_bits.py, test_spirv_interp_f64_glsl.py, test_spirv_interp_signs.py. test_f16_hw_words.py, test_f16_op_sel_values.py, test_mode_round.py, test_u16_high_half.py and test_values.py import pytest, which this interpreter does not have; they cannot be run here.

Negative control (new tests kept, tests/spirv_interp.py reverted to main)

  • FAILED (failures=1, errors=2) in test_spirv_interp_signs.py - exactly the three tests that encode the new behaviour (test_fma_negative_zero_and_f64_overflow, test_glsl_domain_and_overflow_ieee, test_op_fnegate_preserves_f32_nan_bits_and_vectors); the file's other eight cases cover pre-existing interpreter behaviour and stay green.
  • test_spirv_interp_bits.py stays OK on the old implementation, which is right: it tests the bitcast/NaN-payload mechanism, not these edge domains.

Assessment

  • The FMA zero-sign rule matches IEEE: a zero exact sum is -0.0 only when the product and the addend are both negative zero, +0.0 otherwise (tests/spirv_interp.py:284-296). The overflow path now returns the signed infinity instead of raising OverflowError, which is what previously made a test that hit it die with a traceback rather than compare values.
  • _fneg flipping the sign bit of a _F32 (and of the binary64 pattern for a plain NaN) is the correct OpFNegate model: negating a NaN flips the sign bit and keeps the payload, where Python's unary - had dropped the _F32.bits it carries for exactly this reason. Vector recursion is right too.
  • _ieee_exp2 under/overflow (Python raises OverflowError above binary64, but the result is pre-rounded to f32 by the caller) and _ieee_log2 at ±0/negative look right.

Nit: the "undefined on hardware" cases are choices, not measurements
Sqrt of a negative, InverseSqrt of <= 0, and Sin/Cos of ±inf are undefined in GLSL.std.450, and the interpreter now picks NaN (Sqrt), signed infinity (InverseSqrt(-0)), and NaN for the trig infinities. That is a reasonable oracle, and no current fixture asks those questions, but it is a guess rather than something measured on RDNA2 or in SPIRV-Cross; if a translator test ever asserts sin(inf) or sqrt(-1) against this helper, the expected value will be whatever this function says, not what the console does. A one-line comment on _ieee_trig/_ieee_sqrt saying "undefined by the spec; NaN is our choice" would keep a later reader from treating it as measured. Nothing to change for this PR.

Nit: tests/test_spirv_interp_signs.py mixes the new edge-domain cases with older coverage (signed compares, SMIN/SMAX, shifts, conversions). Not wrong, but it hides which cases belong to this change - the NC above had to be read case by case to see it.

Not run: the five pytest-based users of the helper (no pytest on this interpreter), and any GPU; this change cannot affect a compiled module.

python -I scripts/check_no_game_data.py on both changed files: clean.

No game files. AI-assisted.

**@⁠wavehs** on GitHub · 2026-11-26 19:45 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2266#pullrequestreview-6079715906) · **reviewed** **macOS arm64 check of #2266** (spirv_interp IEEE edge domains). A test-oracle change only; the new tests run here and the negative control confirms they bite. Tested head `d1b14075a` merged onto `main` `bdbc7e923` as merge `e575c7a77` (clean, no conflicts). Environment: Apple M3 Pro arm64, macOS 27.2.1, CPython 3.15.2. **On the merge** - `python -I tests/test_spirv_interp_signs.py`: 11 tests OK. `test_spirv_interp_bits.py`, `test_spirv_interp_f64_glsl.py` and `test_ci_registration.py` also OK, so the new file is selected by CI's standard-library loop (`tests/test_ci_registration.py` passes). - The whole semantic suite on the merge: `500 files, 6367 tests, 0 files failed` - unchanged, as expected for a tests/ helper. - Every standalone user of `tests/spirv_interp.py` that can run without pytest passes: `test_rcp_iflag_mods.py`, `test_spirv_interp_bits.py`, `test_spirv_interp_f64_glsl.py`, `test_spirv_interp_signs.py`. `test_f16_hw_words.py`, `test_f16_op_sel_values.py`, `test_mode_round.py`, `test_u16_high_half.py` and `test_values.py` import pytest, which this interpreter does not have; they cannot be run here. **Negative control (new tests kept, `tests/spirv_interp.py` reverted to `main`)** - `FAILED (failures=1, errors=2)` in `test_spirv_interp_signs.py` - exactly the three tests that encode the new behaviour (`test_fma_negative_zero_and_f64_overflow`, `test_glsl_domain_and_overflow_ieee`, `test_op_fnegate_preserves_f32_nan_bits_and_vectors`); the file's other eight cases cover pre-existing interpreter behaviour and stay green. - `test_spirv_interp_bits.py` stays OK on the old implementation, which is right: it tests the bitcast/NaN-payload mechanism, not these edge domains. **Assessment** - The FMA zero-sign rule matches IEEE: a zero exact sum is `-0.0` only when the product and the addend are both negative zero, `+0.0` otherwise (`tests/spirv_interp.py:284-296`). The overflow path now returns the signed infinity instead of raising `OverflowError`, which is what previously made a test that hit it die with a traceback rather than compare values. - `_fneg` flipping the sign bit of a `_F32` (and of the binary64 pattern for a plain NaN) is the correct `OpFNegate` model: negating a NaN flips the sign bit and keeps the payload, where Python's unary `-` had dropped the `_F32.bits` it carries for exactly this reason. Vector recursion is right too. - `_ieee_exp2` under/overflow (Python raises `OverflowError` above binary64, but the result is pre-rounded to f32 by the caller) and `_ieee_log2` at `±0`/negative look right. **Nit: the "undefined on hardware" cases are choices, not measurements** `Sqrt` of a negative, `InverseSqrt` of `<= 0`, and `Sin`/`Cos` of `±inf` are undefined in GLSL.std.450, and the interpreter now picks NaN (`Sqrt`), signed infinity (`InverseSqrt(-0)`), and NaN for the trig infinities. That is a reasonable oracle, and no current fixture asks those questions, but it is a guess rather than something measured on RDNA2 or in SPIRV-Cross; if a translator test ever asserts `sin(inf)` or `sqrt(-1)` against this helper, the expected value will be whatever this function says, not what the console does. A one-line comment on `_ieee_trig`/`_ieee_sqrt` saying "undefined by the spec; NaN is our choice" would keep a later reader from treating it as measured. Nothing to change for this PR. **Nit**: `tests/test_spirv_interp_signs.py` mixes the new edge-domain cases with older coverage (signed compares, SMIN/SMAX, shifts, conversions). Not wrong, but it hides which cases belong to this change - the NC above had to be read case by case to see it. **Not run**: the five pytest-based users of the helper (no pytest on this interpreter), and any GPU; this change cannot affect a compiled module. `python -I scripts/check_no_game_data.py` on both changed files: clean. No game files. AI-assisted.
Author
Owner

@⁠tiguan26 on GitHub · 2026-11-27 04:37 UTC · original · approved these changes

Approving d1b14075a.

spirv_interp is what many translator tests compute expected values with, so beyond its own tests I ran every test that imports it on this batch: 781 passed (main: 770, the difference being this PR's tests), so the IEEE edge cases (sqrt/rsqrt/log2 of negatives and zeros, exp2 overflow, trig of infinities, an exact-zero FMA's sign) and the NaN-preserving negate move no existing result.

Merged onto main d1dff7949 in one batch with 22 other tool PRs of this series (no conflicts), on Linux with Python 3.13. tests/test_spirv_interp_signs.py: 11 passed (main: 8). AI-assisted (Claude Code, Claude Opus 5.5).

**@⁠tiguan26** on GitHub · 2026-11-27 04:37 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2266#pullrequestreview-6083543929) · **approved these changes** Approving d1b14075a. `spirv_interp` is what many translator tests compute expected values with, so beyond its own tests I ran every test that imports it on this batch: 781 passed (main: 770, the difference being this PR's tests), so the IEEE edge cases (sqrt/rsqrt/log2 of negatives and zeros, exp2 overflow, trig of infinities, an exact-zero FMA's sign) and the NaN-preserving negate move no existing result. Merged onto `main` `d1dff7949` in one batch with 22 other tool PRs of this series (no conflicts), on Linux with Python 3.13. `tests/test_spirv_interp_signs.py`: 11 passed (main: 8). AI-assisted (Claude Code, Claude Opus 5.5).
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/2266/head:github-bot/gh-2266'
git switch 'github-bot/gh-2266'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-2266'
git switch 'github-bot/gh-2266'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-2266'
git switch 'github-bot/gh-2266'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-2266'
git switch 'main'
git merge --squash 'github-bot/gh-2266'
git switch 'main'
git merge --ff-only 'github-bot/gh-2266'
git switch 'main'
git merge 'github-bot/gh-2266'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.