spirv_interp: handle GLSL IEEE edge domains, FMA sign/overflow, and OpFNegate NaNs #2266
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/2266"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from ubiquitarianism/leonida. Originally posted by kirigayakazima on 2026-11-26T11:50:04Z.
Claimed in the coordination issue as: SPIRV-INTERP-IEEE-DOMAIN-OVERFLOW-FMA-AND-BITCAST-SIGN (https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7191180874)
What and why
tests/spirv_interp.pypreviously raised Python exceptions or lost IEEE-754 sign/payload bits on valid edge-case inputs:Sqrt,InverseSqrt,Log2,Sin,Cos,Fract, andExp2called rawmath.*/2.0 ** xfunctions directly, raisingValueErrororOverflowErroron negative/zero/infinite/huge operands instead of returning IEEE-754NaN, signed infinity,0.0, or-0.0.FmaconvertedFractionsums unconditionally viafloat(...), losing-0.0sign when-0.0 * c + (-0.0)summed to zero and raisingOverflowErroron overflowing binary64 results.OpFNegateran unary-v, stripping_F32bit-pattern payloads (including signalling NaN bits) and failing on tuple vector operands.This adds IEEE-754 domain/overflow/
-0.0-safe evaluation inglsl(), preserves_F32bit patterns and vector/f64 NaN signs inOpFNegate, and adds regression tests intests/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)upstream/mainbaseline negative control: failed with the new tests before the fixpython -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, andgit diff --cached --checkGPUs tested: n/a
AI-assisted: yes
Checklist
mainand merges cleanly (a conflicting pull request is labeledneeds rebase).docs/design.md, with how it was confirmed.Imported from ubiquitarianism/leonida. Originally posted by real-rusher on 2026-11-26T12:23:30Z.
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)
Imported from ubiquitarianism/leonida. Originally posted by wavehs on 2026-11-26T19:45:28Z.
Review: COMMENTED
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
d1b14075amerged ontomainbdbc7e923as mergee575c7a77(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.pyandtest_ci_registration.pyalso OK, so the new file is selected by CI's standard-library loop (tests/test_ci_registration.pypasses).500 files, 6367 tests, 0 files failed- unchanged, as expected for a tests/ helper.tests/spirv_interp.pythat 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.pyandtest_values.pyimport pytest, which this interpreter does not have; they cannot be run here.Negative control (new tests kept,
tests/spirv_interp.pyreverted tomain)FAILED (failures=1, errors=2)intest_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.pystays OK on the old implementation, which is right: it tests the bitcast/NaN-payload mechanism, not these edge domains.Assessment
-0.0only when the product and the addend are both negative zero,+0.0otherwise (tests/spirv_interp.py:284-296). The overflow path now returns the signed infinity instead of raisingOverflowError, which is what previously made a test that hit it die with a traceback rather than compare values._fnegflipping the sign bit of a_F32(and of the binary64 pattern for a plain NaN) is the correctOpFNegatemodel: negating a NaN flips the sign bit and keeps the payload, where Python's unary-had dropped the_F32.bitsit carries for exactly this reason. Vector recursion is right too._ieee_exp2under/overflow (Python raisesOverflowErrorabove binary64, but the result is pre-rounded to f32 by the caller) and_ieee_log2at±0/negative look right.Nit: the "undefined on hardware" cases are choices, not measurements
Sqrtof a negative,InverseSqrtof<= 0, andSin/Cosof±infare 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 assertssin(inf)orsqrt(-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_sqrtsaying "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.pymixes 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.pyon both changed files: clean.No game files. AI-assisted.
Imported from ubiquitarianism/leonida. Originally posted by tiguan26 on 2026-11-27T04:37:27Z.
Review: APPROVED
Approving
d1b14075a.spirv_interpis 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
maind1dff7949in 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).Imported from ubiquitarianism/leonida. Originally posted by zmasi on 2026-11-28T10:35:10Z.
#2266 | head
d1b14075a4(merged cleanly with main6c7f79343) |python -I tests/test_spirv_interp_signs.py| red on main:FAILED (failures=1, errors=2)(ValueError: math domain erroronSqrt(-1),TypeErroron vectorOpFNegate, andFma(-0.0, 1.0, -0.0)returning+0.0instead of-0.00x80000000) | green on PR+main:11 tests OK| Windows 11, Python 3.12.10 | AI-assisted: yesAudited
_fneg(preserving_F32sNaN/qNaN payload bits and tuple vectors),_ieee_sqrt/_ieee_rsqrt/_ieee_log2/_ieee_exp2/_ieee_trig,Fract(+-inf) -> NaN, andFmaexact-zero sign (-0.0whena*bandcare both negative zero) +f64OverflowError -> +-infhandling intests/spirv_interp.py. Also passedtests/test_ci_registration.py(18 OK),tests/test_text_write_line_endings.py(3 OK),tests/test_runner_calls_all_tests.py(3 OK), andscripts/check_no_game_data.py.Imported from ubiquitarianism/leonida. Originally posted by MoonEyes on 2026-11-28T15:07:10Z.
Red/green:
pytest tests/test_spirv_interp_signs.pyfails on main (3 failed, 8 passed, 3 subtests passed) and passes on headd1b14075a4merged with main (11 passed, 3 subtests passed).Targeted batch (PR tests, tests importing the changed modules, docs tests, translator when touched) merged on main
d48a957bbin GitHub Actions: no new failure.AI-assisted (Claude Code, Claude Opus 5.5)