Skip to content

tests/spirv_interp.py: OpBitcast keeps signalling-NaN bits (#509, as #533) - #606

Merged
ItsNotRexed merged 1 commit into
ItsNotRexed:mainfrom
BBareth:fix/spirv-interp-snan-bits
Nov 22, 2026
Merged

ItsNotRexed merged 1 commit into
ItsNotRexed:mainfrom
BBareth:fix/spirv-interp-snan-bits

Conversation

@BBareth

@BBareth BBareth commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Claim: #374 (comment) (new TEST item, follow-up to TEST-67)

Bug. #533 fixed #509 in tests/semantic/spirv_mini_interp.py. The repository has a second interpreter, tests/spirv_interp.py, behind test_values.py, test_f16_op_sel_values.py, test_f16_hw_words.py and test_interp_p1_p2.py. Its u2f/f2u go through struct's binary32 ↔ double conversion in the same way, and that conversion quiets a signalling NaN:

>>> spirv_interp.f2u(spirv_interp.u2f(0xffa3a095))
0xffe3a095

So any sNaN-pattern word that passes through an OpBitcast comes back with bit 22 set. None of today's tests happen to hit this. A seeded or random-word test would, at about 0.4% per word, as test_alu_shift_b64 and test_mem_diff did.

Fix. The same as #533. u2f() returns a small float subclass (_F32) that keeps the 32-bit pattern, and f2u() returns that pattern. Copies, selects, composites, loads and stores pass the value on unchanged. Arithmetic returns a plain float, so the pattern is dropped there, because SPIR-V leaves NaN result bits open.

Tested. New tests/test_spirv_interp_bits.py, standard library and hand-written SPIR-V only, so it runs in CI's Windows check job:

  • Four sNaN words plus a quiet NaN, 1.0 and -0.0 go through OpBitcast → OpCopyObject → composite → OpSelect → a float Output and back to a uint in v0. Each must come out unchanged.
  • OpIsNan on a bitcast sNaN.
  • OpFAdd of an sNaN still gives a NaN. Only "a NaN" is asserted, not its bits.
  • u2f/f2u round trips.

Against main's interpreter exactly the four sNaN words fail. Results with the fix:

  • python -I tests/test_spirv_interp_bits.py: OK (4 tests).
  • The interpreter's users: pytest tests/test_values.py tests/test_f16_op_sel_values.py tests/test_f16_hw_words.py gives 181 passed. python -I tests/semantic/test_interp_p1_p2.py is OK.
  • Python 3.12.15 on macOS arm64. CPU only, no game data. No GPU involved.

AI-assisted (Claude Code).

Checklist

  • No game data, keys or decryption, including in logs or reports.
  • Translator contract is inapplicable (tests only).

🤖 Generated with Claude Code

…ed#509, as ItsNotRexed#533)

u2f()/f2u() went through struct's binary32 <-> double conversion, which
quiets a signalling NaN (sets bit 22), so a word in 0x7f800001..
0x7fbfffff / 0xff800001..0xffbfffff that passed through an OpBitcast
came back changed: 0xffa3a095 became 0xffe3a095. This is the second
interpreter (test_values, test_f16_op_sel_values, test_f16_hw_words,
test_interp_p1_p2); ItsNotRexed#533 fixed the same bug in spirv_mini_interp.

Same fix: u2f() returns a float subclass that keeps the pattern and
f2u() returns it; copies, selects, composites and stores pass it on,
and arithmetic gives a plain float. New test_spirv_interp_bits.py
checks four sNaN words through every pass-through path, OpIsNan, and
that arithmetic still gives a NaN.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@rafaelpssz

Copy link
Copy Markdown
Contributor

Tested bad14ed on Windows 11, Python 3.14.7: pass

  • python -I tests/test_spirv_interp_bits.py -v: 4 tests OK.
  • python -I -m pytest -q tests/test_values.py tests/test_f16_op_sel_values.py tests/test_f16_hw_words.py tests/test_spirv_interp_bits.py: 185 passed, 14 subtests passed.
  • python -I tests/semantic/test_interp_p1_p2.py: 20 tests OK. compileall clean.

Note: on Python 3.14 the new test does not catch the bug. Run against main's tests/spirv_interp.py on 3.14.7 it also passes (4 OK), because 3.14's struct round trip already keeps the signalling bit (0xffa3a095 comes back as 0xffa3a095). It still catches it on CI's 3.12, as the description says, so this is only a caveat for anyone checking locally on 3.14.

By @rafaelpssz, with Claude Code (AI-assisted).

@ItsNotRexed
ItsNotRexed merged commit 78475e9 into ItsNotRexed:main Nov 22, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

spirv_mini_interp: OpBitcast and float outputs quiet signalling-NaN bit patterns (bit 22 flips)

3 participants