"""Semantic tests for v_cmp_class_f32 / v_cmpx_class_f32 with VOP3 neg/abs modifiers. Hardware rule (measured natively on AMD RDNA2 gfx1036, wave32 & wave64, IEEE=0/1, denorm32 keep/flush, 672/672 exact across all 21 values x 4 modifiers x 10 class bits): v_cmp_class_f32 and v_cmpx_class_f32 apply VOP3 abs (& 0x7FFFFFFF) then neg (^ 0x80000000) as raw sign-bit moves on the 32-bit operand before testing the IEEE-754 class bits. A signalling NaN stays signalling (class bit 0, never quieted to bit 1) and subnormals stay subnormal (never flushed to zero), regardless of MODE.IEEE or MODE.FP32_DENORM. Before this fix, Gen.cmp_class loaded src0 through src_f -> modify(), emitting SPIR-V FAbs / OpFNegate, which SPIR-V leaves free to quiet NaNs and which spirv_mini_interp.py quieted from sNaN (bit 0) to qNaN (bit 1). No game files, llvm-mc, or GPU required; spirv-val check runs when spirv-as and spirv-val are on PATH. """ import gc import os import shutil import struct import subprocess import sys import tempfile import unittest import warnings HERE = os.path.dirname(os.path.abspath(__file__)) ROOT = os.path.dirname(os.path.dirname(HERE)) TOOLS_DIR = os.environ.get("LEONIDA_TOOLS_DIR") or os.path.join(ROOT, "tools") sys.dont_write_bytecode = True if TOOLS_DIR not in sys.path: sys.path.insert(0, TOOLS_DIR) if HERE not in sys.path: sys.path.insert(0, HERE) import rdna2spirv # noqa: E402 import spirv_mini_interp as si # noqa: E402 STUB_DXBC = b"DXBC" + bytes(24) + struct.pack(" int: """Return the single 10-bit class mask bit for raw binary32 word u.""" sign = (u >> 31) & 1 absu = u & 0x7FFFFFFF exp = absu >> 23 man = absu & 0x7FFFFF if exp == 255: if man == 0: return CLASS_NINF if sign else CLASS_PINF return CLASS_QNAN if man >= 0x400000 else CLASS_SNAN if absu == 0: return CLASS_NZERO if sign else CLASS_PZERO if exp == 0: return CLASS_NSUB if sign else CLASS_PSUB return CLASS_NNORM if sign else CLASS_PNORM def translate_listing(body_lines, *, stage="ps", wave=32, group=(1, 1, 1)): """Translate a hand-written RDNA2 listing through rdna2spirv.translate.""" with tempfile.TemporaryDirectory() as td: dis = os.path.join(td, "disasm") fssm = os.path.join(td, "fssm") os.makedirs(dis) os.makedirs(fssm) name = "ps_test" if stage == "ps" else "cs_test" with open(os.path.join(td, f"{name}.dxbc"), "wb") as f: f.write(STUB_DXBC if stage == "ps" else COMPUTE_DXBC) # Minimal FSSM header: flags word at 0xD4 bit 5 (0x20) = wave32, 0 = wave64; # compute rsrc2 + group size at 0x138..0x147. hdr = bytearray(0x148) hdr[0:4] = b"FSSM" struct.pack_into(" bit 0 (sNaN) "v_cndmask_b32_e64 v2, 0, 1, s4", "v_cmp_class_f32_e64 s5, |0xffa00000|, 1", # |(-sNaN)| -> bit 0 (sNaN) "v_cndmask_b32_e64 v4, 0, 1, s5", "v_cmp_class_f32_e64 s6, -|0x7fbfffff|, 3", # -|(+sNaN)| -> bit 0, check bits 0|1 "v_cndmask_b32_e64 v5, 0, 1, s6", "v_cmp_class_f32_e64 s7, -|0x7fbfffff|, 2", # must NOT match bit 1 (qNaN) "v_cndmask_b32_e64 v6, 0, 1, s7", ]) self.assertEqual(got, (1, 1, 1, 0)) self.assertNotIn("FAbs", spv) self.assertNotIn("OpFNegate", spv) def test_multilane_cmpx_snan_mods_and_inactive_lanes(self): """Workgroup test: v_cmpx_class_f32 with -v/|v|/-|v| updates EXEC per lane and keeps disabled lanes off.""" # 4 lanes: # lane 0: 0x7f800001 (+sNaN) -> -v is -sNaN (bit 0 = 1) -> active # lane 1: 0x7fc00000 (+qNaN) -> -v is -qNaN (bit 0 = 0) -> deactivated # lane 2: 0xffa00000 (-sNaN) -> |v| is +sNaN (bit 0 = 1), but pre-disabled -> stays off # lane 3: 0xffbfffff (-sNaN) -> -|v| is -sNaN (bit 0 = 1) -> active body = [ "s_mov_b32 s8, 0", "s_mov_b32 s9, 0", "s_mov_b32 s10, 64", "s_mov_b32 s11, 0x31016fac", # Load per-lane test value from buffer offset 4*lane "v_lshlrev_b32 v10, 2, v0", "buffer_load_dword v1, v10, s[8:11], 0 offen", "s_waitcnt vmcnt(0)", # Pre-disable lane 2 via EXEC "v_cmp_ne_u32_e32 vcc_lo, 2, v0", "s_and_b32 exec_lo, exec_lo, vcc_lo", # Intersect EXEC with sNaN check through -v, |v|, and -|v| sequentially "v_cmpx_class_f32_e64 -v1, 1", "v_cmpx_class_f32_e64 |v1|, 1", "v_cmpx_class_f32_e64 -|v1|, 1", # Only lanes 0 and 3 remain active and write 0xCAFE0000 | lane to offset 32 + 4*lane "v_or_b32 v2, 0xcafe0000, v0", "v_add_nc_u32 v11, 32, v10", "buffer_store_dword v2, v11, s[8:11], 0 offen", "s_endpgm", ] spv = translate_listing(body, stage="cs", wave=32, group=(4, 1, 1)) memory = { 0: 0x7F800001, 4: 0x7FC00000, 8: 0xFFA00000, 12: 0xFFBFFFFF, 32: 0xDEAD0000, 36: 0xDEAD0001, 40: 0xDEAD0002, 44: 0xDEAD0003, } si.run_workgroup( spv, 4, 32, inputs_for=lambda i: {"%wg_id": (0, 0, 0), "%local_id": (i, 0, 0)}, push_constants=[0] * 32, memory=memory, ) got = (memory[32], memory[36], memory[40], memory[44]) self.assertEqual(got, (0xCAFE0000, 0xDEAD0001, 0xDEAD0002, 0xCAFE0003)) def test_spirv_val_accepts_modified_cmp_class(self): """Translated SPIR-V with -v, |v|, -|v| passes spirv-as and spirv-val when installed.""" spirv_as = shutil.which("spirv-as") spirv_val = shutil.which("spirv-val") if not spirv_as or not spirv_val: self.skipTest("spirv-as / spirv-val not on PATH") _, spv = run_ps([ "v_mov_b32 v1, 0x7f800001", "v_cmp_class_f32_e64 vcc_lo, -v1, 1", "v_cndmask_b32_e64 v2, 0, 1, vcc_lo", "v_cmp_class_f32_e64 s4, |v1|, 1", "v_cndmask_b32_e64 v4, 0, 1, s4", "v_cmp_class_f32_e64 s5, -|v1|, 1", "v_cndmask_b32_e64 v5, 0, 1, s5", "v_mov_b32 v6, 0", "v_cmpx_class_f32_e64 -|v1|, 1", "v_mov_b32 v6, 1", ]) with tempfile.TemporaryDirectory() as td: asm_p = os.path.join(td, "m.spvasm") bin_p = os.path.join(td, "m.spv") with open(asm_p, "w", encoding="utf-8", newline="\n") as f: f.write(spv) subprocess.run([spirv_as, "--target-env", "vulkan1.2", asm_p, "-o", bin_p], check=True) subprocess.run([spirv_val, "--target-env", "vulkan1.2", bin_p], check=True) if __name__ == "__main__": unittest.main()