From bad14edbef9862652325949adaca206bfe8d60ca Mon Sep 17 00:00:00 2001 From: bbareth <179921368+BBareth@users.noreply.github.com> Date: Sun, 22 Nov 2026 02:08:48 +0100 Subject: [PATCH] tests/spirv_interp.py: OpBitcast keeps signalling-NaN bits (#509, as #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); #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 --- tests/spirv_interp.py | 20 ++++++- tests/test_spirv_interp_bits.py | 93 +++++++++++++++++++++++++++++++++ 2 files changed, 112 insertions(+), 1 deletion(-) create mode 100644 tests/test_spirv_interp_bits.py diff --git a/tests/spirv_interp.py b/tests/spirv_interp.py index d4f2571a9..e9de94be3 100644 --- a/tests/spirv_interp.py +++ b/tests/spirv_interp.py @@ -20,12 +20,30 @@ def f32(x): return math.copysign(math.inf, x) +class _F32(float): + """A binary32 value made from a bit pattern (OpBitcast) that keeps that exact pattern. + + struct's binary32 -> double conversion quiets a signalling NaN (sets bit 22, issue #509), so the float alone + cannot give the word back. Copies, selects, composites, loads and stores pass the value on unchanged and keep + `bits`; arithmetic on it returns a plain float, which drops them (SPIR-V leaves the bits of a NaN result + open). The same fix as spirv_mini_interp (#533).""" + + __slots__ = ("bits",) + + def __new__(cls, bits): + self = super().__new__(cls, struct.unpack(" float -> OpCopyObject -> composite -> OpSelect -> a float Output and back to a uint in v0. + body = ("%x = OpBitcast %float %cw\n" + "%y = OpCopyObject %float %x\n" + "%v = OpCompositeConstruct %v2float %f1 %y\n" + "%e = OpCompositeExtract %float %v 1\n" + "%s = OpSelect %float %t %e %f1\n" + "OpStore %out_mrt0 %s\n" + "%w = OpBitcast %uint %s\n" + "%p = OpAccessChain %_ptr_Function_uint %vgpr %c0\n" + "OpStore %p %w\n") + for word in WORDS: + with self.subTest(word=f"{word:#010x}"): + r = run(word, body) + self.assertEqual(r.vgpr[0], word) + self.assertEqual(si.f2u(r.outputs["out_mrt0"]), word) + + def test_still_a_nan_to_float_operations(self): + r = run(0xFFA3A095, "%x = OpBitcast %float %cw\n%n = OpIsNan %bool %x\n" + "%k = OpSelect %uint %n %c4 %c0\n" + "%p = OpAccessChain %_ptr_Function_uint %vgpr %c0\nOpStore %p %k\n") + self.assertEqual(r.vgpr[0], 4) + + def test_arithmetic_gives_a_nan(self): + # Arithmetic drops the kept pattern: SPIR-V does not fix the bits of a NaN result, so only "a NaN" is + # asserted (exponent all ones, mantissa not zero). + r = run(0xFFA3A095, "%x = OpBitcast %float %cw\n%y = OpFAdd %float %x %f1\n%w = OpBitcast %uint %y\n" + "%p = OpAccessChain %_ptr_Function_uint %vgpr %c0\nOpStore %p %w\n") + self.assertEqual(r.vgpr[0] & 0x7F800000, 0x7F800000) + self.assertNotEqual(r.vgpr[0] & 0x007FFFFF, 0) + + def test_helpers(self): + for word in WORDS: + with self.subTest(word=f"{word:#010x}"): + self.assertEqual(si.f2u(si.u2f(word)), word) + self.assertEqual(si.f2u(1.5), 0x3FC00000) # a plain float still converts as before + + +if __name__ == "__main__": + unittest.main()