Files
mandy/tests/test_f64_conversion.py
aadheel a9bed902d2 rdna2spirv: VOP3 clamp of a NaN gives +0 (DX10_CLAMP)
Gen.clamp_mods applied the VOP3 clamp bit as a bare GLSL.std.450
FClamp, which is undefined for a NaN operand. The game's MODE has
DX10_CLAMP=1, under which the hardware turns a NaN result into +0
(measured on gfx1036: the v_rcp_iflag_f32 run and SHADER-186 for
v_fmac_f16 / v_fma_mix*). Select +0 for a NaN before FClamp, as the
div_fixup, cvt_pk_u8 and cvt_pknorm clamps already do. This covers
every f32/f16 op that applies clamp through clamp_mods/clamp_f16.

- tests/semantic/test_vop3_clamp_nan.py: NaN operands (quiet,
  negative, signalling) and invalid operations (inf-inf, -inf*0)
  under clamp give +0 for f32, f16 (low and high half), conversions,
  legacy and mix ops; controls without a NaN are unchanged. It fails
  33 subtests on the old translator.
- The two negative controls that expected the undefined FClamp
  (test_alu_f32_min_max, test_alu_min3_max3) now expect +0, and the
  f16 unary harness checks clamp of a NaN result instead of skipping.
- test_rcp_iflag_mods / test_f64_conversion match the new SPIR-V.
- docs/design.md and docs/shader_validation.md no longer list the
  f32/f16 clamp of a NaN as unmodelled. Still a bare FClamp: the f64
  clamp (open PR #475) and the packed v_pk_* clamp.

AI-assisted (Claude Code).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-24 16:10:13 +05:30

61 lines
2.3 KiB
Python

"""Check the SPIR-V data flow for output modifiers on v_cvt_f32_f64."""
import re
import sys
import tempfile
import unittest
from pathlib import Path
TEST_DIR = Path(__file__).resolve().parent
sys.path[:0] = [str(TEST_DIR.parent / "tools"), str(TEST_DIR)]
from listing import translate_listing
FIXTURE = TEST_DIR / "fixtures" / "f64_conversion.asm"
class F64ConversionTest(unittest.TestCase):
def test_output_modifiers(self):
body = "\n".join(
line for line in FIXTURE.read_text(encoding="utf-8").splitlines()
if not line.startswith(";")
)
cases = (
("", None, False),
("mul:2", 0x40000000, False),
("mul:4", 0x40800000, False),
("div:2", 0x3F000000, False),
("clamp", None, True),
("clamp div:2", 0x3F000000, True),
)
with tempfile.TemporaryDirectory() as directory:
for modifiers, scale_bits, clamp in cases:
with self.subTest(modifiers=modifiers):
spv = translate_listing(Path(directory), f"{body} {modifiers}".rstrip())
self.check_conversion(spv, scale_bits, clamp)
def check_conversion(self, spv, scale_bits, clamp):
def result(instruction):
match = re.search(rf"^ *(%\w+) = {instruction}$", spv, re.M)
self.assertIsNotNone(match, f"Missing SPIR-V instruction: {instruction}")
return match.group(1)
value = result(r"OpFConvert %float %\w+")
if scale_bits is not None:
constant = result(rf"OpConstant %uint {scale_bits}")
scale = result(rf"OpBitcast %float {constant}")
value = result(rf"OpFMul %float {value} {scale}")
if clamp:
# DX10_CLAMP: a NaN is replaced by +0 before FClamp (Gen.clamp_mods).
nan = result(rf"OpIsNan %bool {value}")
value = result(rf"OpSelect %float {nan} %f0 {value}")
value = result(rf"OpExtInst %float %glsl FClamp {value} %f0 %f1")
word = result(rf"OpBitcast %uint {value}")
dest = result(r"OpAccessChain %_ptr_Function_uint %vgpr %c3")
# This straight-line fixture keeps EXEC full, so the result is stored directly.
self.assertRegex(spv, rf"(?m)^ *OpStore {dest} {word}$")
if __name__ == "__main__":
unittest.main()