Repository navigation
spirv_mini_interp: binary64 Fma, Sqrt and InverseSqrt stay binary64 - #4768
paratustra wants to merge 3 commits into
Conversation
|
At I ran this hand-written module through import sys
from pathlib import Path
sys.path.insert(0,str(Path("tests/semantic").resolve()))
from test_interp import HEADER, FOOTER
from spirv_mini_interp import run_fragment
for x in (2.0**1023,-2.0**1023):
decl=f"%d = OpTypeFloat 64\n%u2 = OpTypeVector %uint 2\n%x = OpConstant %d {x!r}\n%two = OpConstant %d 2\n%z = OpConstant %d 0\n"
code=HEADER.replace("%main = OpFunction",decl+"%main = OpFunction")
code+="%r = OpExtInst %d %glsl Fma %x %two %z\n%w = OpBitcast %u2 %r\n%hi = OpCompositeExtract %uint %w 1\nOpStore %out %hi\n"+FOOTER
try: print(hex(run_fragment(code)["%out"][0]))
except Exception as e: print(type(e).__name__, str(e))Expected high words: All 40 tests in AI-assisted. |
Kam471
left a comment
There was a problem hiding this comment.
Ran this at fde85290b on Windows 11: pytest tests/semantic/test_interp.py -q -> 40 passed, 104 subtests passed; git diff --check clean.
spirv_mini_interp now keeps Fma, Sqrt and InverseSqrt in binary64 instead of round-tripping through the f32 path, with the added cases covering the precision-sensitive values. No blocking issue.
|
At head |
|
Thanks @shdann and @editrice. Both were real, and both are fixed in 05be80b:
The test now also covers AI-assisted (Claude Code). |
|
Consolidated per the coordinator's note in #4816: @illusionart453-max closed #4829 (TEST-1068) so that the vector binary64 interpreter fixes sit in one PR. Their commit is cherry-picked here with authorship kept: a0b4cf6, "preserve binary64 vector multiplication precision". It resolves a vector result's component type in So this PR now covers binary64 Checked at a0b4cf6 (macOS, Python 3.15.2):
AI-assisted (Claude Code). |
|
Tested head
AI-assisted review (Gemini 4 Neon). |
abnalstlll-byte
left a comment
There was a problem hiding this comment.
Verified at head fde8529 on Windows 11 (Python 3.14, CPU only):
- \python -m unittest discover -s tests/semantic -p test_interp.py\ -- 40/40 OK, including \ est_binary64_fma_sqrt_inverse_sqrt_stay_binary64.
- \python -I tests/test_docs_links.py\ -- 6/6 OK. \git diff --check\ clean.
- Code read: \ma_f64\ evaluates via exact \Fraction(x) * Fraction(y) + Fraction(z)\ and rounds once, \Sqrt\ and \InverseSqrt\ preserve %double\ without \32\ conversion, and the tie test cleanly separates fused from unfused evaluation.
No game files. AI-assisted review (Gemini).
|
Verified on the current main (3faac55): clean merge, compileall clean, tests/semantic/test_interp.py passes 42/42 via unittest discover (re-run), and the closest users of the changed interpreter paths (test_valu_fma_f64, test_valu_sqrt_f64, test_valu_rsq_f64, test_fma_exact, test_fma_khr, test_valu_f32_transcendentals, test_alu_sqrt_f32, test_alu_rsq_f32) are all green. Exactly-right fix: binary64 Fma via fractions-exact products, f64 Sqrt/InverseSqrt via scaling. One heads-up for future work, not a defect here: the f64 Sqrt/InverseSqrt dispatch is scalar-only — a vector double Sqrt would still be f32-rounded, while the OpFMul and Fma paths do handle vectors. No CI checks are reported on this branch. AI-assisted: yes. Tested on Linux (RTX 5080 host, CPU-only run). |
|
Tested
|
GLSL.std.450 Fma always went through fma_f32, and Sqrt and InverseSqrt always returned f32(...), whatever the result type. FMax, FClamp and the roundings next to them already keep a %double result. The translator emits all three on %double (v_fma_f64, v_sqrt_f64, v_rsq_f64), so a CPU run of those modules gave binary32-rounded results: Sqrt(3.0) was 1.7320507765 instead of 1.7320508075688772. A %double Fma now rounds the exact product and sum once (fma_f64: float() of the exact rational is a single round-to-nearest-even), and a %double Sqrt or InverseSqrt keeps the binary64 value.
…; v2double Fma is binary64 The OverflowError handler converted the out-of-range fraction to a float again inside math.copysign, so it raised instead of returning infinity. A %v2double Fma checked only a scalar result type and still rounded each component to binary32. Both cases are in the test now.
|
Rebased onto main AI-assisted (Claude Code). |
a0b4cf6 to
32109d3
Compare
|
Tested 32109d3 (on main 1cc6e3c) on Ubuntu 24.04, Python 3.13.18: pass.
No game files used. AI-assisted (Claude). |
Claimed in the coordination issue as: new TEST item (claim in #4683, no ID yet)
What and why
tests/semantic/spirv_mini_interp.pyrounded GLSL.std.450Fma,SqrtandInverseSqrtto binary32 whatever their result type.Fmaalways went throughfma_f32, andSqrt/InverseSqrtalways returnedf32(...).FMax,FClampand the roundings right next to them already keep a%doubleresult (types.t[a[0]] == ("float", 64)).The translator emits all three on
%double:v_fma_f64(rdna2spirv.py~8509, 8552, 8597),v_sqrt_f64(~6688) andv_rsq_f64(~6684). So a CPU run of those modules gave binary32-rounded results:%doubleopSqrt(3.0)InverseSqrt(3.0)Fma(1.0000000000000002, 3, 0.1)This matters for anything that judges binary64 output with this interpreter, such as the FUZZ-55 f64 differential fuzzer (#4536, which runs through
run_fragment) and the f64 fuzzing claimed in the coordination issue. It belongs with #4172 (FNegate/FAbs) and #4122 (int-to-float), which fix other ops of the same kind and don't touch these three.Change (
tests/semantic/spirv_mini_interp.py):fma_f64: the exact rational product and sum, thenfloat()of it, which is one int / int division that Python rounds to nearest even once (subnormals included). It uses the same signed-zero rule asfma_f32, and overflow to ±inf.Fmapicksfma_f64for a%doubleresult;Sqrt/InverseSqrtskip thef32()for one.FMin,Exp2,Log2,Sin,Cos. The translator doesn't emit them on%double.FAbsis tests: preserve binary64 in SPIR-V negate and abs (TEST-912) #4172's.Tested
macOS, Python 3.15.2, CPU only, no game files.
test_binary64_fma_sqrt_inverse_sqrt_stay_binary64intests/semantic/test_interp.py(the interpreter self-tests, which take their expectations from the specs, not the translator):Sqrt(3),InverseSqrt(3)and anFmawhose result is exact only with a single rounding:x = 1 + 2^-26,y = 1 + 2^-27,z = 2^-100. The product's tail is exactly half an ulp andzbreaks the tie upward. Rounding the product first gives a different double, and the test asserts that too.OpBitcast %v2uint.test_interp.pyOK.Checklist
docs/design.md, with how it was confirmed. (Test-interpreter change only; the translator is untouched.)AI-assisted (Claude Code).