Files
mandy/tests/semantic/test_format_decode_branch.py
ItsNotRexed 5ed11f24be Fix two semantic tests the round-43 merges broke
- test_bary_fallback: #3647 (BUILD-1046) made a missing input folder exit 2 with "does not exist"; the test from
  #1379 still expected exit 1.
- test_format_decode_branch: the f16 reference uses the interpreter's pinned half_to_float (a NaN half widens to
  the canonical quiet NaN) instead of struct's '<e', whose NaN payload changed in Python 3.14 (CPython gh-130317).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-22 21:10:20 -05:00

81 lines
3.2 KiB
Python

"""The run-time FORMAT decode of buffer_load_format (Gen.decode_any_format) branches to one format's decode.
Every code goes through the translator and the mini interpreter, which follows the branches, and is compared with a
reference decoded here from the raw bits with exact quotients. A code outside FORMATS gives zeros.
"""
import struct
import unittest
from fractions import Fraction
import support
from spirv_mini_interp import half_to_float, run_fragment
import rdna2spirv
BASE = 0x1000
WORDS = (0x8123C4F7, 0x3E9A7F01, 0xC0490FDB, 0x5AA5FF80)
def f32_bits(x):
return struct.unpack("<I", struct.pack("<f", x))[0]
def reference(code):
if code not in rdna2spirv.Gen.FORMATS:
return (0, 0, 0, 0)
size, kind, widths = rdna2spirv.Gen.FORMATS[code]
raw, bit, out = sum(w << (32 * i) for i, w in enumerate(WORDS)), 0, []
for w in widths:
v = raw >> bit & ((1 << w) - 1)
bit += w
signed = v - (1 << w) if v >> (w - 1) else v
if kind in ("f32", "u32", "i32", "uint"):
out.append(v)
elif kind == "sint":
out.append(signed & 0xFFFFFFFF)
elif kind == "f16":
# The interpreter's pinned f16 -> f32 rule (a NaN half widens to the canonical quiet NaN with its sign),
# not struct's '<e', whose NaN payload depends on the Python version (CPython gh-130317).
out.append(f32_bits(half_to_float(v)))
elif kind == "unorm":
out.append(f32_bits(float(Fraction(v, (1 << w) - 1))))
elif kind == "snorm":
out.append(f32_bits(float(max(Fraction(-1), Fraction(signed, (1 << (w - 1)) - 1)))))
else:
return None # a kind this reference doesn't model: covered by its own tests
return tuple(out[c % len(out)] for c in range(4))
def load(code):
lines = [f"s_mov_b32 s0, {BASE:#x}", "s_mov_b32 s1, 0", "s_mov_b32 s2, 0x100",
f"s_mov_b32 s3, {code << 12 | 0x30000FAC:#x}", "v_mov_b32 v1, 0",
"buffer_load_format_xyzw v[2:5], v1, s[0:3], 0 offen", "exp mrt0 v2, v3, v4, v5 done vm", "s_endpgm"]
spv = support.translate_lines("ps_fmt_branch", lines)
return tuple(run_fragment(spv, memory={BASE + 4 * i: w for i, w in enumerate(WORDS)})["%out_mrt0"])
class FormatDecodeBranchTests(unittest.TestCase):
def test_every_format_and_the_codes_between(self):
for code in range(max(rdna2spirv.Gen.FORMATS) + 2):
want = reference(code)
if want is not None:
self.assertEqual(load(code), want, f"format {code}")
def test_only_the_chosen_decode_runs(self):
gen = rdna2spirv.Gen("ps")
gen.decode_any_format("%fmt", ["%w0", "%w1", "%w2", "%w3"])
lines = [ln.strip() for ln in gen.body]
merges = []
for ln in lines:
if ln.startswith("OpSelectionMerge"):
self.assertIn("DontFlatten", ln)
merges.append(ln.split()[1])
elif merges and ln == f"{merges[-1]} = OpLabel":
merges.pop()
elif "OpBitFieldUExtract" in ln or "OpIMul" in ln:
self.assertTrue(merges, f"decode work outside every branch: {ln}")
if __name__ == "__main__":
unittest.main()