Skip to content

tests: negate a NaN by flipping its sign bit in the mini interpreter - #7285

Closed
bobcatchris wants to merge 1 commit into
ItsNotRexed:mainfrom
bobcatchris:fix/spirv-negate-snan-sign
Closed

bobcatchris wants to merge 1 commit into
ItsNotRexed:mainfrom
bobcatchris:fix/spirv-negate-snan-sign

Conversation

@bobcatchris

Copy link
Copy Markdown
Contributor

tests/semantic/test_float_nan_fract.py fails deterministically on CPython 3.11 and 3.12, and passes on 3.14:

FAIL: test_f32_three_input_all_nan_keeps_src0 (op='v_min3_f32')
  run_op("v_min3_f32 v0, -v1, v2, v3", (0x7F800001, NAN_A, NAN_B))
  AssertionError: 4290772993 != 4286578689      # 0xFFC00001 != 0xFF800001

Cause

spirv_mini_interp.OpFNegate negated a Python float and rounded it with f32(). The operand loaded by OpBitcast is an _F32 that carries its exact word, but -x returns a plain float, and struct's binary32 -> double conversion quiets a signalling NaN before 3.14 (CPython gh-130317, the same version pin already documented in half_to_float). So the measured -sNaN word came back with bit 22 set on 3.11/3.12; 3.14 kept the payload, which is why only those versions failed.

Hardware negation flips the sign bit and leaves the rest of a NaN alone, which is what the test's RDNA2 words pin.

Change

When the operand carries exact 32-bit bits, OpFNegate now xors the sign bit and keeps the pattern. Binary64 and plain-float operands take the old path, so finite values are unchanged.

Tested

  • CPython 3.12.0, Windows 11: tests/semantic/test_float_nan_fract.py 3 failures before, 31/31 after.
  • The eight semantic files that mention OpFNegate plus test_float_nan_fract.py: 177 passed.
  • 3.14 is not installed here; the new path is bit-level and does not depend on struct's NaN behaviour.

Synthetic SPIR-V only, no game files.
AI-assisted.

OpFNegate used Python float negation and an f32 round trip. struct's
binary32 -> double conversion quiets a signalling NaN before 3.14, so
-v1 on 0x7F800001 became 0xFFC00001 on 3.11/3.12 and the three all-NaN
three-input cases in test_float_nan_fract failed; 3.14 kept the payload
and passed. A sign-bit flip keeps the measured RDNA2 result on every
Python and does not touch finite values.

AI-assisted.
@slint6614-dev

Copy link
Copy Markdown
Contributor

Heads-up: this now conflicts with main and looks superseded by #7302 (14b2e44, merged), which makes the same OpFNegate change (flip bit 31 of a binary32 operand, keep -x for f64). The only difference is that this PR keeps f32(-operand) for a non-_F32 operand, while #7302 goes through f32_bits(x) for every binary32 value. Probably worth closing unless the author sees a case #7302 misses. AI-assisted (Claude).

@bobcatchris

Copy link
Copy Markdown
Contributor Author

Superseded by #7302 (14b2e44), merged on main: it changes the same OpFNegate branch in tests/semantic/spirv_mini_interp.py to flip the sign bit of a binary32 operand instead of round-tripping it through a float, and it covers the same AllNaNSrc0PayloadTests case (0x7F800001 -> 0xFF800001, not the quiet 0xFFC00001). Rebasing this branch produced a conflict in exactly that hunk, and tests/semantic/spirv_mini_interp.py passes on main (5cf3dafe3) without this branch.

Closing rather than pushing a second version of the same fix. Nothing here needs re-landing.

AI-assisted.

@dadsmallbuk1

Copy link
Copy Markdown

Tested b035d40 on Windows 11 (26200), CPython 3.12.10: python -m unittest test_float_nan_fract test_interp gives 83 tests OK (1 skipped). On main, test_float_nan_fract fails here with the assertion you quote.

Three open PRs now address this one failure in different ways, and they disagree on what the interpreter should model:

PR Change test_float_nan_fract on 3.12
this one interpreter OpFNegate flips the sign bit and keeps an sNaN signalling passes, with the translator unchanged
#7200 (@paratustra) interpreter f32/f32_bits quiet every NaN, on every Python (CI's 3.12 rule) fails until the translator changes
#7173 (@thiefyzheng) translator applies neg/abs on the bits (OpBitwiseXor/OpBitwiseAnd) for the all-NaN select, no OpFNegate there passes (31/31 here), under either interpreter rule

The point to settle: the SPIR-V that #6293's test checks still uses OpFNegate on main, and a Vulkan driver may quiet a signalling NaN in a float op. If the interpreter models OpFNegate as a bit flip, the semantic test passes while the GPU result can differ. #7173 removes the dependence. #7200's rule is then the stricter interpreter, and this PR's rule matches OpFNegate on drivers that don't quiet. Could the three of you agree an order? For example #7173 first, then one of the two interpreter rules.

Code read: test interpreter only (spirv_mini_interp.py, the OpFNegate case); f64 and non-_F32 operands keep the old path.

AI-assisted (Claude Code).

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.

3 participants