Skip to content

spirv_mini_interp: binary64 Fma, Sqrt and InverseSqrt stay binary64 - #4768

Open
paratustra wants to merge 3 commits into
ItsNotRexed:mainfrom
paratustra:test-mini-interp-binary64-fma-sqrt
Open

paratustra wants to merge 3 commits into
ItsNotRexed:mainfrom
paratustra:test-mini-interp-binary64-fma-sqrt

Conversation

@paratustra

Copy link
Copy Markdown

Claimed in the coordination issue as: new TEST item (claim in #4683, no ID yet)

What and why

tests/semantic/spirv_mini_interp.py rounded GLSL.std.450 Fma, Sqrt and InverseSqrt to binary32 whatever their result type. Fma always went through fma_f32, and Sqrt/InverseSqrt always returned f32(...). FMax, FClamp and the roundings right next to them already keep a %double result (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) and v_rsq_f64 (~6684). So a CPU run of those modules gave binary32-rounded results:

%double op main exact (this PR)
Sqrt(3.0) 1.7320507765… 1.7320508075688772
InverseSqrt(3.0) binary32-rounded 0.5773502691896258
Fma(1.0000000000000002, 3, 0.1) 3.0999999046… 3.1000000000000005

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):

  • New fma_f64: the exact rational product and sum, then float() of it, which is one int / int division that Python rounds to nearest even once (subnormals included). It uses the same signed-zero rule as fma_f32, and overflow to ±inf.
  • Fma picks fma_f64 for a %double result; Sqrt/InverseSqrt skip the f32() for one.
  • Not changed: FMin, Exp2, Log2, Sin, Cos. The translator doesn't emit them on %double. FAbs is tests: preserve binary64 in SPIR-V negate and abs (TEST-912) #4172's.

Tested

macOS, Python 3.15.2, CPU only, no game files.

  • New test_binary64_fma_sqrt_inverse_sqrt_stay_binary64 in tests/semantic/test_interp.py (the interpreter self-tests, which take their expectations from the specs, not the translator):
    • Sqrt(3), InverseSqrt(3) and an Fma whose 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 and z breaks the tie upward. Rounding the product first gives a different double, and the test asserts that too.
    • Each result is compared bit for bit through OpBitcast %v2uint.
    • It fails on main (all three subtests) and passes here.
  • test_interp.py OK.
  • Full semantic suite, per test, against main (98fa503): the same 35 failures/errors on both sides, all of them the known arm64/environment ones listed in Coordinating who works on what (25) (moved to #4816) #4683. Nothing new.

Checklist

  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed. (Test-interpreter change only; the translator is untouched.)

AI-assisted (Claude Code).

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.
@shdann

shdann commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

At fde85290, a binary64 Fma with finite inputs still raises OverflowError when the exact result exceeds the binary64 range. The new overflow handler tries to return a signed infinity. Its call to math.copysign(math.inf, exact) converts the same out-of-range fraction to a float again.

I ran this hand-written module through run_fragment on Windows 11 with CPython 3.14.7. Save the script and run python -I overflow.py from the checkout root.

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: 0x7ff00000 and 0xfff00000. Actual result for both signs: OverflowError: integer division result too large for a float. Please select the infinity sign with an exact comparison such as exact < 0.

All 40 tests in test_interp.py pass on this head. On parent 98fa503f, the same overflow inputs also raise, and the three new ordinary-value comparisons fail. The PR fixes the ordinary cases and leaves this boundary case open.

AI-assisted.

@Kam471 Kam471 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@editrice

Copy link
Copy Markdown

At head fde85290, the new f64 Fma dispatch only matches a scalar result type: types.t[a[0]] == ("float", 64). A %v2double result has vector type metadata, so the existing tuple path still calls fma_f32 lane by lane. I ran a hand-written %v2double module using OpCompositeConstruct, OpExtInst %v2double %glsl Fma %vx %vy %vz, then extracted/bitcast lane 0. With x=1+2^-26, y=1+2^-27, z=2^-100, it returns binary64 words 0x00000000 0x3ff00000 (1.0), versus exact single-rounding 0x06000001 0x3ff00000 (1.000000022351742). Please resolve the vector component type before selecting fma_f64 and add a vector f64 regression. The scalar interpreter control suite passes 35 tests. AI-assisted review; synthetic SPIR-V only.

…; 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.
@paratustra

Copy link
Copy Markdown
Author

Thanks @shdann and @editrice. Both were real, and both are fixed in 05be80b:

  • Overflow: math.copysign(math.inf, exact) turned the out-of-range fraction into a float again, so it raised. It now returns -math.inf if exact < 0 else math.inf.
  • %v2double: the dispatch now resolves a vector's component type (("vec", comp, n) → types.t[comp]) before it picks fma_f64.

The test now also covers Fma(1e200, 1e200, 3) → +inf, Fma(-1e200, 1e200, 3) → −inf, and a %v2double Fma on the tie inputs (x = 1+2^-26, y = 1+2^-27, z = 2^-100), extracting component 1. All three new subtests fail on the previous head fde8529: two OverflowErrors, and the vector case returns the binary32-rounded value. They pass now. test_interp.py is OK. Every tests/semantic file that mentions Fma passes, except test_alu_vop3_mods_hw, whose 27 failures are the same set as with main's interpreter: the known arm64 NaN-sign ones listed in #4683.

AI-assisted (Claude Code).

@paratustra

Copy link
Copy Markdown
Author

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 OpFAdd/OpFSub/OpFMul/OpFDiv, keeps a binary64 vector OpFMul in binary64, and refuses the other binary64 vector ops as the scalar path already did, with exact-word tests for widths 2, 3 and 4. The only conflict was the import block of test_interp.py; I kept both sides.

So this PR now covers binary64 Fma (scalar and vector, including overflow), Sqrt, InverseSqrt and vector OpFMul.

Checked at a0b4cf6 (macOS, Python 3.15.2):

  • test_interp.py: 42 tests OK.
  • Against main's interpreter, all 12 new subtests fail (both sets), as intended.
  • Over test_interp.py, test_alu_vop3_mods_hw.py and the 20 test_*f64*.py files, this branch fails nothing that main's interpreter passes. The 27 failures both share are the known arm64 NaN-sign ones in test_alu_vop3_mods_hw.

AI-assisted (Claude Code).

@DenRakEiw

Copy link
Copy Markdown

Tested head a0b4cf66f6e2e863f14370a98729037ab2a1b051 on Windows 11 / Python 3.12.10.

  • Trial merge onto origin/main: clean (git diff --cached --check clean).
  • python -m unittest discover -s tests/semantic -p test_interp.py: Ran 42 tests, OK.
  • Code read: Preserves binary64 precision in tests/semantic/spirv_mini_interp.py for vector OpFMul (while refusing OpFAdd/OpFSub/OpFDiv on binary64), scalar/vector Fma (via exact Fraction-based fma_f64), and scalar Sqrt/InverseSqrt, with 3 new unit test methods in tests/semantic/test_interp.py.

AI-assisted review (Gemini 4 Neon).

@abnalstlll-byte abnalstlll-byte left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants