tombox-xshader: exact v_fma_f32 on devices without 64-bit float shader ops (integer tb_fma, probe with a CPU reference) #73
No reviewers
Labels
No labels
accessibility
bug
documentation
duplicate
enhancement
good first issue
help wanted
invalid
library
question
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/omonkeyz--xbox-emulator!73
Loading…
Reference in a new issue
No description provided.
Delete branch "gh-pr/73/head"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
@realkodr on GitHub · 2026-11-21 23:20 UTC · original
realkodr:xshader/fma-probe-no-fp64→main· +166 −12 · 6 files · 89 commits · closed without mergingLibrary / area: tombox-xshader (
tb_fmahelpers,fma_probe,compile_fma, shader cache key), d3d12_xs (one log line)Exports changed (exports.txt rows): none
Source of the behaviour: unchanged:
v_fma_f32is a single-rounding fused multiply-add with the NaN rules the existing helpers already have. This adds a third way to compute the same result. The integer algorithm is the standard one (exact 48-bit product, aligned add with a sticky bit, round to nearest even), written for this PR and checked againstf32::mul_add.Problem
The exact
tb_fmaneeds HLSL doubles. A device withoutDoublePrecisionFloatShaderOpscannot build it. Intel Xe is one, and I'd expect the same on Windows. On such a device the new fma probe cannot run either: its shader holds both helpers,CreateComputePipelineStatefails, and the runtime logs "probe failed; exact path kept". No shader that usesv_fma_f32can then be created.The fast helper is not a way out there. Measured on the device below,
mad()is not fused: 848,336 of 5,242,880 probe results differ from the exact value.Change
fma_probe::reference()is the exact helper's arithmetic done on the CPU in f64. When the device has no doubles, the probe runs the fast helper alone and compares it with that. A unit test checks the reference againstf32::mul_addon 1,000,000 inputs.tb_fmaunderTB_INT_FMA, in 64-bit integer arithmetic, with gradual underflow and the same NaN rules as the other two.Fma::intonly if every result matches the reference.compile_fmathen passes-D TB_INT_FMA. The flag is in the shader cache key and in the cached probe result;PROBE_VERSIONis 3.Devices with doubles are probed exactly as before and never get the integer helper. The fast/exact block in
hlsl.rskeeps its shape, sohelper_lines()parses it unchanged.Results
Intel Iris Xe (TGL GT2), Mesa 26.2.7, vkd3d-proton 3.1 under Wine 11.21 (built and run with the tooling in #52). The device reports no 64-bit float ops.
mainCreateComputePipelineState 0x80070057Fma { preserve: false, any: false, int: true }execfma_single_rounding,fma_f32_full_rangewith the f64 helper-D TB_INT_FMA--lib --test opcodes --test syntheticBefore porting to HLSL, the algorithm was checked as a CPU model against
f32::mul_addon 200,000,000 inputs (random, cancellation, rounding ties, denormal inputs and results, overflow, every triple of 20 special values) with no mismatch.Not checked
Windows, and any device that has doubles. The new
hw_integer_fma_matches_referencetest (ignored, needs a GPU) runs the integer helper on whatever device is present; it should pass on WARP, NVIDIA and AMD too. I can't run it there.Cost. I have not measured the integer helper against the f64 one. It only runs where the f64 one cannot.
FXC. The helper uses
uint64_t, so it is SM6 only, like the f64 helper.The exec tests do not go through the probe. For the two
execrows above I added-D TB_INT_FMAtodxc::compilelocally; that switch is not in this PR.Other f64 uses (
v_fma_f64,tb_ldexp64, ...) still need doubles.vector_vop3keeps 6 failing cases on this device for that reason.Under Wine the adapter id reads "NVIDIA GeForce GTX 470", which is Wine's placeholder, so the cached probe result is keyed by that string there.
cargo test -p tombox-xshader --lib --test opcodes --test syntheticpasses (under Wine); the GPU tests named above pass on the Iris XeNo game or console files and no binaries included
No fake success; anything not possible is left
missingwith a note🤖 Generated with Claude Code
The exact tb_fma helper needs HLSL doubles. A device without DoublePrecisionFloatShaderOps (Intel Xe, for one) cannot build it, and the fma probe could not run there either: its shader holds both helpers, CreateComputePipelineState failed, and the runtime kept the f64 path, on which no shader using v_fma_f32 can be created. - fma_probe: `reference()` is the exact helper's arithmetic on the CPU. When the device has no doubles the probe runs the fast helper alone and compares it with that. A unit test checks the reference against f32::mul_add on 1,000,000 inputs. - hlsl: a third tb_fma under TB_INT_FMA: a single-rounding fused multiply-add in 64-bit integer arithmetic (48-bit product, aligned add with a sticky bit, round to nearest even, gradual underflow), with the same NaN rules as the other two. - The probe verifies the integer helper on the device in both denormal modes and sets `Fma::int` only when every result matches. compile_fma then passes -D TB_INT_FMA where the fast helper is off. The flag is part of the shader cache key and of the cached probe result, and PROBE_VERSION is 3. Devices with doubles are probed exactly as before and never get the integer helper. Measured on an Intel Iris Xe (TGL GT2), Mesa 26.2.7, vkd3d-proton 3.1 under Wine 11.21, where the device reports no 64-bit float ops: - fast helper against the reference: 848,336 of 5,242,880 results differ with denorm preserve and 1,181,699 with denorm any, so mad() is not fused there and the fast path is rightly left off; - integer helper against the reference: 0 of 5,242,880 differ in each mode, and the probe returns Fma { preserve: false, any: false, int: true }; - tests/exec.rs fma_single_rounding and fma_f32_full_range, compiled with -D TB_INT_FMA: both pass. With the f64 helper they fail at pipeline creation (131 and 303 cases). The integer algorithm was first checked as a CPU model against f32::mul_add on 200,000,000 inputs (random, cancellation, rounding ties, denormal inputs and results, overflow, every triple of 20 special values) with no mismatch. Not run on Windows, and not on a device that has doubles: there the new hw_integer_fma_matches_reference test (ignored, needs a GPU) should pass too. Co-Authored-By: Claude Fable 5.2 <[email protected]>@QuentinDanblon on GitHub · 2026-11-21 23:42 UTC · original
I ran the part listed under "Not checked" on Windows, on a device that has doubles: RTX 5090, driver 32.0.16.1742, head
c00a1bf.hw_integer_fma_matches_reference(ignored test, run with--ignored)hw_fast_fma_matches_exact_pathFma { preserve: true, any: false, int: false }: preserve 0 differ, any 503,648 differ.intstays off, as intended on a device with doubles--lib--test exec fmaTOMBOX_EXEC_HW=1)Not checked:
fma_probetests picked the hardware adapter in both of my runs;TOMBOX_EXEC_HWdoes not change that.The branch merges cleanly with #36, #61, #63 and #76 (textual merge only, not tested together).
Related:
reference()and the three helpers share one NaN rule (first NaN operand, quieted). Whether RDNA2 does that forv_fma_f32is one of the things thenanprobe in #72 would measure, if someone with the hardware runs it.@SchmashinPrunes on GitHub · 2026-11-22 00:11 UTC · original
FYI, since
reference()and the helpers here share main's NaN rule (first NaN operand, quieted;0x7fc00000for an invalid operation): I ran thenanprobe from #72 on RDNA2 hardware (gfx1036), andv_fma_f32doesn't quite do that. Details and the fitted rules are here: #41In short:
0xffc00000;0 * inf + NaNreturns that default NaN rather than c;This PR keeps main's existing rule on purpose, so I don't see it as a blocker here. It's just useful context if the reference or helpers change later. Which IEEE mode console shaders use is still open (see QuentinDanblon's note on #41).
@omonkeyz on GitHub · 2026-11-22 13:12 UTC · original
Main's history was rewritten, which auto-closed this PR (sorry for the noise). Good idea. Please rebase on main's current fma_probe/shader_xlat code and strip the comment lines (the project keeps code free of comment lines). To resubmit:
git fetchthis repo, branch from the currentmain, cherry-pick only your own commits (the old branch is based on the previous history and cannot be merged), and open a new PR.Pull request closed