diff options
| -rw-r--r-- | tests/semantic/test_fp_edge_conformance.py | 126 |
1 files changed, 126 insertions, 0 deletions
diff --git a/tests/semantic/test_fp_edge_conformance.py b/tests/semantic/test_fp_edge_conformance.py new file mode 100644 index 000000000..2548d4407 --- /dev/null +++ b/tests/semantic/test_fp_edge_conformance.py | |||
| @@ -0,0 +1,126 @@ | |||
| 1 | """Floating-point edge-case conformance suite translated via rdna2spirv (SHADER-13). | ||
| 2 | |||
| 3 | Translates RDNA2 floating-point edge-case instructions (`v_min_f32`, `v_max_f32`, | ||
| 4 | `v_fract_f32`, `v_cvt_pkrtz_f16_f32`) through `tools/rdna2spirv.py` and evaluates | ||
| 5 | them in `spirv_mini_interp.py`: signed zero ordering (-0.0 < +0.0), single/dual NaN | ||
| 6 | operand selection, signaling/quiet NaN pass-through in `v_fract_f32` (game IEEE=0 | ||
| 7 | mode), `±inf` handling, and RTZ finite saturation in `v_cvt_pkrtz_f16_f32`. | ||
| 8 | Standard library only; no game data, no GPU. | ||
| 9 | """ | ||
| 10 | from __future__ import annotations | ||
| 11 | |||
| 12 | import os | ||
| 13 | import sys | ||
| 14 | import unittest | ||
| 15 | |||
| 16 | HERE = os.path.dirname(os.path.abspath(__file__)) | ||
| 17 | sys.dont_write_bytecode = True | ||
| 18 | if HERE not in sys.path: | ||
| 19 | sys.path.insert(0, HERE) | ||
| 20 | |||
| 21 | import support # noqa: E402 | ||
| 22 | |||
| 23 | POS_ZERO_F32 = 0x00000000 | ||
| 24 | NEG_ZERO_F32 = 0x80000000 | ||
| 25 | POS_INF_F32 = 0x7F800000 | ||
| 26 | NEG_INF_F32 = 0xFF800000 | ||
| 27 | IND_NAN_F32 = 0xFFC00000 | ||
| 28 | QNAN_A = 0x7FC10001 | ||
| 29 | QNAN_B = 0xFFC20002 | ||
| 30 | SNAN_A = 0x7F800001 | ||
| 31 | FINITE_3_5 = 0x40600000 | ||
| 32 | F32_65520 = 0x477FF000 | ||
| 33 | F32_NEG_65520 = 0xC77FF000 | ||
| 34 | |||
| 35 | |||
| 36 | def _run_ps(lines: list[str]) -> tuple[list[int], str]: | ||
| 37 | """Translate instruction lines as a pixel shader exporting v4..v7 and return (words, spvasm).""" | ||
| 38 | spvasm = support.translate_lines( | ||
| 39 | "ps_fp_edge", | ||
| 40 | lines + [ | ||
| 41 | "exp mrt0 v4, v5, v6, v7 done vm", | ||
| 42 | "s_endpgm", | ||
| 43 | ], | ||
| 44 | ) | ||
| 45 | return support.mrt0_words(spvasm), spvasm | ||
| 46 | |||
| 47 | |||
| 48 | def _alu1(op: str, a: int) -> int: | ||
| 49 | words, _ = _run_ps([ | ||
| 50 | f"v_mov_b32 v0, {a:#x}", | ||
| 51 | f"{op} v4, v0", | ||
| 52 | "v_mov_b32 v5, 0", | ||
| 53 | "v_mov_b32 v6, 0", | ||
| 54 | "v_mov_b32 v7, 0", | ||
| 55 | ]) | ||
| 56 | return words[0] | ||
| 57 | |||
| 58 | |||
| 59 | def _alu2(op: str, a: int, b: int) -> int: | ||
| 60 | words, _ = _run_ps([ | ||
| 61 | f"v_mov_b32 v0, {a:#x}", | ||
| 62 | f"v_mov_b32 v1, {b:#x}", | ||
| 63 | f"{op} v4, v0, v1", | ||
| 64 | "v_mov_b32 v5, 0", | ||
| 65 | "v_mov_b32 v6, 0", | ||
| 66 | "v_mov_b32 v7, 0", | ||
| 67 | ]) | ||
| 68 | return words[0] | ||
| 69 | |||
| 70 | |||
| 71 | class FloatingPointEdgeConformanceTests(unittest.TestCase): | ||
| 72 | """Translated RDNA2 floating-point edge-case verification (SHADER-13).""" | ||
| 73 | |||
| 74 | def test_signed_zero_min_max_ordering(self): | ||
| 75 | """RDNA2 v_min_f32 / v_max_f32 treat -0.0 < +0.0 regardless of operand order.""" | ||
| 76 | for a, b in ((NEG_ZERO_F32, POS_ZERO_F32), (POS_ZERO_F32, NEG_ZERO_F32)): | ||
| 77 | with self.subTest(op="v_min_f32", a=hex(a), b=hex(b)): | ||
| 78 | self.assertEqual(_alu2("v_min_f32", a, b), NEG_ZERO_F32) | ||
| 79 | with self.subTest(op="v_max_f32", a=hex(a), b=hex(b)): | ||
| 80 | self.assertEqual(_alu2("v_max_f32", a, b), POS_ZERO_F32) | ||
| 81 | |||
| 82 | def test_nan_minmax_operand_selection(self): | ||
| 83 | """One NaN returns the other operand; two NaNs preserve src0's exact bits.""" | ||
| 84 | for nan in (QNAN_A, SNAN_A): | ||
| 85 | for op in ("v_min_f32", "v_max_f32"): | ||
| 86 | with self.subTest(op=op, nan=hex(nan), order="nan_first"): | ||
| 87 | self.assertEqual(_alu2(op, nan, FINITE_3_5), FINITE_3_5) | ||
| 88 | with self.subTest(op=op, nan=hex(nan), order="nan_second"): | ||
| 89 | self.assertEqual(_alu2(op, FINITE_3_5, nan), FINITE_3_5) | ||
| 90 | |||
| 91 | for op in ("v_min_f32", "v_max_f32"): | ||
| 92 | with self.subTest(op=op, pair="a_b"): | ||
| 93 | self.assertEqual(_alu2(op, QNAN_A, QNAN_B), QNAN_A) | ||
| 94 | with self.subTest(op=op, pair="b_a"): | ||
| 95 | self.assertEqual(_alu2(op, QNAN_B, QNAN_A), QNAN_B) | ||
| 96 | with self.subTest(op=op, pair="snan_qnan"): | ||
| 97 | self.assertEqual(_alu2(op, SNAN_A, QNAN_B), SNAN_A) | ||
| 98 | |||
| 99 | def test_fract_signed_zero_infinities_and_nan_passthrough(self): | ||
| 100 | """v_fract_f32: -0 -> +0, +-inf -> 0xffc00000, qNaN and sNaN pass through unchanged (IEEE=0).""" | ||
| 101 | self.assertEqual(_alu1("v_fract_f32", NEG_ZERO_F32), POS_ZERO_F32) | ||
| 102 | self.assertEqual(_alu1("v_fract_f32", POS_INF_F32), IND_NAN_F32) | ||
| 103 | self.assertEqual(_alu1("v_fract_f32", NEG_INF_F32), IND_NAN_F32) | ||
| 104 | self.assertEqual(_alu1("v_fract_f32", QNAN_A), QNAN_A) | ||
| 105 | self.assertEqual(_alu1("v_fract_f32", SNAN_A), SNAN_A) | ||
| 106 | |||
| 107 | def test_cvt_pkrtz_f16_saturates_finite_overflow(self): | ||
| 108 | """v_cvt_pkrtz_f16_f32 clamps finite overflow to +-65504 (+-0x7BFF/0xFBFF), not +-inf.""" | ||
| 109 | packed = _alu2("v_cvt_pkrtz_f16_f32", F32_65520, F32_NEG_65520) | ||
| 110 | self.assertEqual(packed & 0xFFFF, 0x7BFF) | ||
| 111 | self.assertEqual((packed >> 16) & 0xFFFF, 0xFBFF) | ||
| 112 | |||
| 113 | def test_emits_signed_zero_inf_nan_preserve_mode(self): | ||
| 114 | """Translated shaders declare Vulkan SignedZeroInfNanPreserve 32.""" | ||
| 115 | _, spvasm = _run_ps([ | ||
| 116 | f"v_mov_b32 v0, {FINITE_3_5:#x}", | ||
| 117 | "v_fract_f32 v4, v0", | ||
| 118 | "v_mov_b32 v5, 0", | ||
| 119 | "v_mov_b32 v6, 0", | ||
| 120 | "v_mov_b32 v7, 0", | ||
| 121 | ]) | ||
| 122 | self.assertIn("SignedZeroInfNanPreserve", spvasm) | ||
| 123 | |||
| 124 | |||
| 125 | if __name__ == "__main__": | ||
| 126 | unittest.main() | ||