mirror of
https://github.com/ProjectLeonida/mandy.git
synced 2026-12-06 08:58:36 +00:00
main translates all three (a59367f7,524d1e92), but no fuzzer generated them and --list-ops still called v_ldexp_f64 untranslated. 47 -> 50 forms. - Reference: v_ldexp_f64 is the exact S0 * 2^S1 rounded once by MODE.FP_ROUND[3:2] (subnormals kept, zeros/infinities/NaNs pass, toward zero never rounds up and overflows to DBL_MAX); the integer conversions are exact. Their clamp is judged: ldexp clamps with integer ops and an integer is never NaN or -0. - Generation: S1 aimed at the subnormal and overflow edges, S0 refilled with subnormals, boundaries and all-ones low words (rounding carry), and s_round_mode 0/12 before 30% of the ldexp cases. - Known divergence ldexp-f64-mode: main ignores a toward-zero MODE write in v_ldexp_f64 (#6218 fixes it); such results are counted skips, and the --known probe (DBL_MAX * 2) mismatches until #6218 lands. - tests/spirv_interp.py: OpConvertUToF/OpConvertSToF to %double are exact (they rounded through binary32: v_cvt_f64_u32 0xffffffff read 2^32), and SClamp, UClamp, FindUMsb, OpSLessThan and OpSGreaterThan exist, which the integer-only ldexp lowering needs once a module has a %double op. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
267 lines
10 KiB
Python
267 lines
10 KiB
Python
"""Regression tests for 64-bit %double and %v2double GLSL.std.450 execution in tests/spirv_interp.py."""
|
|
|
|
import math
|
|
import struct
|
|
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 # noqa: E402
|
|
import spirv_interp # noqa: E402
|
|
|
|
|
|
def f64_bits(x):
|
|
return struct.unpack("<Q", struct.pack("<d", x))[0]
|
|
|
|
|
|
def f64_words(x):
|
|
bits = f64_bits(x)
|
|
return bits & 0xFFFFFFFF, bits >> 32
|
|
|
|
|
|
def f32_round(x):
|
|
return struct.unpack("<f", struct.pack("<f", x))[0]
|
|
|
|
|
|
def run_f64_glsl(consts, body_lines, out_type="%double"):
|
|
const_lines = []
|
|
for name, ty, val in consts:
|
|
const_lines.append(f"{name} = OpConstant {ty} {val}")
|
|
spv = "\n".join(
|
|
[
|
|
"%uint = OpTypeInt 32 0",
|
|
"%bool = OpTypeBool",
|
|
"%float = OpTypeFloat 32",
|
|
"%double = OpTypeFloat 64",
|
|
"%v2double = OpTypeVector %double 2",
|
|
"%_c0 = OpConstant %uint 0",
|
|
"%_c1 = OpConstant %uint 1",
|
|
"%_arr_u1 = OpTypeArray %uint %_c1",
|
|
"%_ptr_fn_arr = OpTypePointer Function %_arr_u1",
|
|
"%_ptr_out = OpTypePointer Output " + out_type,
|
|
"%_null = OpConstantNull %_arr_u1",
|
|
*const_lines,
|
|
"%vgpr = OpVariable %_ptr_fn_arr Function %_null",
|
|
"%sgpr = OpVariable %_ptr_fn_arr Function %_null",
|
|
"%out_res = OpVariable %_ptr_out Output",
|
|
"%main = OpFunction %uint None %uint",
|
|
"%entry = OpLabel",
|
|
*body_lines,
|
|
"OpStore %out_res %res",
|
|
"OpReturn",
|
|
"OpFunctionEnd",
|
|
]
|
|
)
|
|
return spirv_interp.run(spv).outputs["out_res"]
|
|
|
|
|
|
class SpirvInterpF64GlslTests(unittest.TestCase):
|
|
def test_scalar_f64_glsl_preserves_53_bit_precision(self):
|
|
tiny = 2.0**-50
|
|
x_neg = -(1.0 + tiny)
|
|
x_pos = 1.0 + tiny
|
|
x_half = 0.5 + tiny
|
|
x_sq = (1.0 + tiny) * (1.0 + tiny)
|
|
self.assertNotEqual(f64_bits(x_pos), f64_bits(f32_round(x_pos)))
|
|
|
|
cases = [
|
|
(
|
|
"FAbs",
|
|
[("%a", "%double", repr(x_neg))],
|
|
"%res = OpExtInst %double %glsl FAbs %a",
|
|
x_pos,
|
|
),
|
|
(
|
|
"FClamp",
|
|
[("%a", "%double", repr(x_half)), ("%lo", "%double", "0.0"), ("%hi", "%double", "1.0")],
|
|
"%res = OpExtInst %double %glsl FClamp %a %lo %hi",
|
|
x_half,
|
|
),
|
|
(
|
|
"FMin",
|
|
[("%a", "%double", repr(x_pos)), ("%b", "%double", repr(1.0 + 2.0 * tiny))],
|
|
"%res = OpExtInst %double %glsl FMin %a %b",
|
|
x_pos,
|
|
),
|
|
(
|
|
"FMax",
|
|
[("%a", "%double", repr(1.0)), ("%b", "%double", repr(x_pos))],
|
|
"%res = OpExtInst %double %glsl FMax %a %b",
|
|
x_pos,
|
|
),
|
|
(
|
|
"Sqrt",
|
|
[("%a", "%double", repr(x_sq))],
|
|
"%res = OpExtInst %double %glsl Sqrt %a",
|
|
math.sqrt(x_sq),
|
|
),
|
|
(
|
|
"InverseSqrt",
|
|
[("%a", "%double", repr(x_sq))],
|
|
"%res = OpExtInst %double %glsl InverseSqrt %a",
|
|
1.0 / math.sqrt(x_sq),
|
|
),
|
|
(
|
|
"Fract",
|
|
[("%a", "%double", repr(x_pos))],
|
|
"%res = OpExtInst %double %glsl Fract %a",
|
|
x_pos - math.floor(x_pos),
|
|
),
|
|
(
|
|
"Exp2",
|
|
[("%a", "%double", repr(x_half))],
|
|
"%res = OpExtInst %double %glsl Exp2 %a",
|
|
2.0**x_half,
|
|
),
|
|
(
|
|
"Log2",
|
|
[("%a", "%double", repr(x_pos))],
|
|
"%res = OpExtInst %double %glsl Log2 %a",
|
|
math.log2(x_pos),
|
|
),
|
|
]
|
|
for name, consts, inst, expected in cases:
|
|
with self.subTest(op=name):
|
|
got = run_f64_glsl(consts, [inst])
|
|
self.assertEqual(f64_bits(got), f64_bits(expected))
|
|
|
|
def test_v2double_glsl_preserves_53_bit_precision(self):
|
|
tiny = 2.0**-48
|
|
a0, a1 = -(1.0 + tiny), 0.25 + tiny
|
|
b0, b1 = 1.0 + 2.0 * tiny, 0.25 + 3.0 * tiny
|
|
lo0, lo1 = 0.0, 0.0
|
|
hi0, hi1 = 2.0, 1.0
|
|
|
|
got_abs = run_f64_glsl(
|
|
[("%a0", "%double", repr(a0)), ("%a1", "%double", repr(a1))],
|
|
[
|
|
"%va = OpCompositeConstruct %v2double %a0 %a1",
|
|
"%res = OpExtInst %v2double %glsl FAbs %va",
|
|
],
|
|
out_type="%v2double",
|
|
)
|
|
self.assertEqual(tuple(f64_bits(v) for v in got_abs), (f64_bits(abs(a0)), f64_bits(abs(a1))))
|
|
|
|
got_min = run_f64_glsl(
|
|
[
|
|
("%a0", "%double", repr(1.0 + tiny)),
|
|
("%a1", "%double", repr(a1)),
|
|
("%b0", "%double", repr(b0)),
|
|
("%b1", "%double", repr(b1)),
|
|
],
|
|
[
|
|
"%va = OpCompositeConstruct %v2double %a0 %a1",
|
|
"%vb = OpCompositeConstruct %v2double %b0 %b1",
|
|
"%res = OpExtInst %v2double %glsl FMin %va %vb",
|
|
],
|
|
out_type="%v2double",
|
|
)
|
|
self.assertEqual(tuple(f64_bits(v) for v in got_min), (f64_bits(1.0 + tiny), f64_bits(a1)))
|
|
|
|
got_max = run_f64_glsl(
|
|
[
|
|
("%a0", "%double", repr(1.0 + tiny)),
|
|
("%a1", "%double", repr(a1)),
|
|
("%b0", "%double", repr(b0)),
|
|
("%b1", "%double", repr(b1)),
|
|
],
|
|
[
|
|
"%va = OpCompositeConstruct %v2double %a0 %a1",
|
|
"%vb = OpCompositeConstruct %v2double %b0 %b1",
|
|
"%res = OpExtInst %v2double %glsl FMax %va %vb",
|
|
],
|
|
out_type="%v2double",
|
|
)
|
|
self.assertEqual(tuple(f64_bits(v) for v in got_max), (f64_bits(b0), f64_bits(b1)))
|
|
|
|
got_clamp = run_f64_glsl(
|
|
[
|
|
("%a0", "%double", repr(1.0 + tiny)),
|
|
("%a1", "%double", repr(a1)),
|
|
("%lo0", "%double", repr(lo0)),
|
|
("%lo1", "%double", repr(lo1)),
|
|
("%hi0", "%double", repr(hi0)),
|
|
("%hi1", "%double", repr(hi1)),
|
|
],
|
|
[
|
|
"%va = OpCompositeConstruct %v2double %a0 %a1",
|
|
"%vlo = OpCompositeConstruct %v2double %lo0 %lo1",
|
|
"%vhi = OpCompositeConstruct %v2double %hi0 %hi1",
|
|
"%res = OpExtInst %v2double %glsl FClamp %va %vlo %vhi",
|
|
],
|
|
out_type="%v2double",
|
|
)
|
|
self.assertEqual(tuple(f64_bits(v) for v in got_clamp), (f64_bits(1.0 + tiny), f64_bits(a1)))
|
|
|
|
def test_f32_glsl_still_rounds_to_binary32(self):
|
|
x = -(1.0 + 2.0**-25)
|
|
got = run_f64_glsl(
|
|
[("%a", "%float", repr(x))],
|
|
["%res = OpExtInst %float %glsl FAbs %a"],
|
|
out_type="%float",
|
|
)
|
|
self.assertEqual(got, f32_round(abs(x)))
|
|
self.assertEqual(got, 1.0)
|
|
|
|
def test_end_to_end_v_add_f64_abs_and_clamp(self):
|
|
a = -(0.5 + 2.0**-45)
|
|
b = -(0.25 + 2.0**-46)
|
|
alo, ahi = f64_words(a)
|
|
blo, bhi = f64_words(b)
|
|
expected = min(max(abs(a) + abs(b), 0.0), 1.0)
|
|
elo, ehi = f64_words(expected)
|
|
|
|
listing = "\n".join(
|
|
[
|
|
f"v_mov_b32 v0, {alo:#x}",
|
|
f"v_mov_b32 v1, {ahi:#x}",
|
|
f"v_mov_b32 v2, {blo:#x}",
|
|
f"v_mov_b32 v3, {bhi:#x}",
|
|
"v_add_f64 v[4:5], |v[0:1]|, |v[2:3]| clamp",
|
|
]
|
|
)
|
|
with tempfile.TemporaryDirectory() as directory:
|
|
spv = translate_listing(Path(directory), listing)
|
|
res = spirv_interp.run(spv)
|
|
self.assertEqual((res.vgpr[4], res.vgpr[5]), (elo, ehi))
|
|
|
|
def test_integer_to_double_conversion_is_exact(self):
|
|
# Every 32-bit integer is a binary64; only a %float result rounds (v_cvt_f64_u32 of 0xffffffff once read 2^32).
|
|
cases = [("OpConvertUToF", 0xFFFFFFFF, 4294967295.0), ("OpConvertUToF", 0x80000001, 2147483649.0),
|
|
("OpConvertSToF", 0x80000001, -2147483647.0), ("OpConvertSToF", 0x80000000, -2147483648.0),
|
|
("OpConvertSToF", 0x7FFFFFFF, 2147483647.0)]
|
|
for op, word, expected in cases:
|
|
with self.subTest(op=op, word=hex(word)):
|
|
got = run_f64_glsl([("%a", "%uint", str(word))], [f"%res = {op} %double %a"])
|
|
self.assertEqual(f64_bits(got), f64_bits(expected))
|
|
got32 = run_f64_glsl([("%a", "%uint", str(word))], [f"%res = {op} %float %a"], out_type="%float")
|
|
self.assertEqual(got32, f32_round(expected))
|
|
|
|
|
|
def test_integer_ops_of_the_ldexp_lowering(self):
|
|
# v_ldexp_f64 lowers to 32-bit integer ops only; a module that also has a %double op runs here, not in spirv_mini_interp.
|
|
neg4096, big = 0xFFFFF000, 0x80000000
|
|
glsl = [("SClamp", (big, neg4096, 4096), neg4096), ("SClamp", (5000, neg4096, 4096), 4096),
|
|
("SClamp", (0xFFFFFFF9, neg4096, 4096), 0xFFFFFFF9), ("UClamp", (0xFFFFFFFF, 1, 10), 10),
|
|
("UClamp", (0, 1, 10), 1), ("FindUMsb", (0,), 0xFFFFFFFF), ("FindUMsb", (1,), 0),
|
|
("FindUMsb", (big,), 31), ("FindUMsb", (0x000FFFFF,), 19)]
|
|
for fn, args, expected in glsl:
|
|
with self.subTest(fn=fn, args=[hex(a) for a in args]):
|
|
consts = [(f"%a{i}", "%uint", str(v)) for i, v in enumerate(args)]
|
|
operands = " ".join(f"%a{i}" for i in range(len(args)))
|
|
self.assertEqual(run_f64_glsl(consts, [f"%res = OpExtInst %uint %glsl {fn} {operands}"], out_type="%uint"), expected)
|
|
compares = [("OpSGreaterThan", 1, 0xFFFFFFFF, 1), ("OpSGreaterThan", 0xFFFFFFFF, 1, 0), ("OpSGreaterThan", 5, 5, 0),
|
|
("OpSLessThan", big, 0, 1), ("OpSLessThan", 0, big, 0), ("OpSLessThan", 5, 5, 0)]
|
|
for op, a, b, expected in compares:
|
|
with self.subTest(op=op, a=hex(a), b=hex(b)):
|
|
consts = [("%a", "%uint", str(a)), ("%b", "%uint", str(b))]
|
|
body = [f"%t = {op} %bool %a %b", "%res = OpSelect %uint %t %_c1 %_c0"]
|
|
self.assertEqual(run_f64_glsl(consts, body, out_type="%uint"), expected)
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|