tombox-xshader: exact v_fma_f32 on devices without 64-bit float shader ops (integer tb_fma, probe with a CPU reference) #73

Closed
github-bot wants to merge 1 commit from gh-pr/73/head into gh-pr/73/base
Owner

@⁠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 merging

Library / area: tombox-xshader (tb_fma helpers, 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_f32 is 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 against f32::mul_add.

Problem

The exact tb_fma needs HLSL doubles. A device without DoublePrecisionFloatShaderOps cannot 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, CreateComputePipelineState fails, and the runtime logs "probe failed; exact path kept". No shader that uses v_fma_f32 can 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

  • CPU reference. 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 against f32::mul_add on 1,000,000 inputs.
  • Integer helper. A third tb_fma under TB_INT_FMA, in 64-bit integer arithmetic, with gradual underflow and the same NaN rules as the other two.
  • Selection. Only on a device without doubles, and only where the fast helper is off: the probe runs the integer helper on the device in both denormal modes and sets Fma::int only if every result matches the reference. compile_fma then passes -D TB_INT_FMA. The flag is in the shader cache key and in the cached probe result; PROBE_VERSION is 3.

Devices with doubles are probed exactly as before and never get the integer helper. The fast/exact block in hlsl.rs keeps its shape, so helper_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.

Check Result
Probe on main fails: CreateComputePipelineState 0x80070057
Fast helper vs reference 848,336 of 5,242,880 differ (denorm preserve), 1,181,699 (denorm any)
Integer helper vs reference 0 of 5,242,880 differ in each mode
Probe with this PR Fma { preserve: false, any: false, int: true }
exec fma_single_rounding, fma_f32_full_range with the f64 helper fail at pipeline creation (131 and 303 cases)
The same two with -D TB_INT_FMA pass
--lib --test opcodes --test synthetic 26 / 22 / 9 pass

Before porting to HLSL, the algorithm was 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 checked

  • Windows, and any device that has doubles. The new hw_integer_fma_matches_reference test (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 exec rows above I added -D TB_INT_FMA to dxc::compile locally; that switch is not in this PR.

  • Other f64 uses (v_fma_f64, tb_ldexp64, ...) still need doubles. vector_vop3 keeps 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 synthetic passes (under Wine); the GPU tests named above pass on the Iris Xe

  • No game or console files and no binaries included

  • No fake success; anything not possible is left missing with a note

🤖 Generated with Claude Code

**@⁠realkodr** on GitHub · 2026-11-21 23:20 UTC · [original](https://github.com/omonkeyz/xbox-emulator/pull/73) `realkodr:xshader/fma-probe-no-fp64` → `main` · +166 −12 · 6 files · 89 commits · closed without merging **Library / area:** tombox-xshader (`tb_fma` helpers, `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_f32` is 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 against `f32::mul_add`. ### Problem The exact `tb_fma` needs HLSL doubles. A device without `DoublePrecisionFloatShaderOps` cannot 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, `CreateComputePipelineState` fails, and the runtime logs "probe failed; exact path kept". No shader that uses `v_fma_f32` can 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 - **CPU reference.** `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 against `f32::mul_add` on 1,000,000 inputs. - **Integer helper.** A third `tb_fma` under `TB_INT_FMA`, in 64-bit integer arithmetic, with gradual underflow and the same NaN rules as the other two. - **Selection.** Only on a device without doubles, and only where the fast helper is off: the probe runs the integer helper on the device in both denormal modes and sets `Fma::int` only if every result matches the reference. `compile_fma` then passes `-D TB_INT_FMA`. The flag is in the shader cache key and in the cached probe result; `PROBE_VERSION` is 3. Devices with doubles are probed exactly as before and never get the integer helper. The fast/exact block in `hlsl.rs` keeps its shape, so `helper_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. | Check | Result | |---|---| | Probe on `main` | fails: `CreateComputePipelineState 0x80070057` | | Fast helper vs reference | 848,336 of 5,242,880 differ (denorm preserve), 1,181,699 (denorm any) | | Integer helper vs reference | 0 of 5,242,880 differ in each mode | | Probe with this PR | `Fma { preserve: false, any: false, int: true }` | | `exec` `fma_single_rounding`, `fma_f32_full_range` with the f64 helper | fail at pipeline creation (131 and 303 cases) | | The same two with `-D TB_INT_FMA` | pass | | `--lib --test opcodes --test synthetic` | 26 / 22 / 9 pass | Before porting to HLSL, the algorithm was 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 checked - **Windows, and any device that has doubles.** The new `hw_integer_fma_matches_reference` test (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 `exec` rows above I added `-D TB_INT_FMA` to `dxc::compile` locally; that switch is not in this PR. - Other f64 uses (`v_fma_f64`, `tb_ldexp64`, ...) still need doubles. `vector_vop3` keeps 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. - [x] `cargo test -p tombox-xshader --lib --test opcodes --test synthetic` passes (under Wine); the GPU tests named above pass on the Iris Xe - [x] No game or console files and no binaries included - [x] No fake success; anything not possible is left `missing` with a note 🤖 Generated with [Claude Code](https://claude.com/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]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 06:09:26 +00:00
github-bot closed this pull request 2026-11-26 06:09:26 +00:00
Author
Owner

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

Check Result
hw_integer_fma_matches_reference (ignored test, run with --ignored) 0 of 5,242,880 results differ, denorm preserve and denorm any
hw_fast_fma_matches_exact_path Fma { preserve: true, any: false, int: false }: preserve 0 differ, any 503,648 differ. int stays off, as intended on a device with doubles
--lib 26 passed, 0 failed, 3 ignored
--test exec fma 3 passed on WARP, 3 passed on the 5090 (TOMBOX_EXEC_HW=1)

Not checked:

  • WARP for the integer helper. The fma_probe tests picked the hardware adapter in both of my runs; TOMBOX_EXEC_HW does not change that.
  • Cost of the integer helper against the f64 one.
  • An AMD device.

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 for v_fma_f32 is one of the things the nan probe in #72 would measure, if someone with the hardware runs it.

**@⁠QuentinDanblon** on GitHub · 2026-11-21 23:42 UTC · [original](https://github.com/omonkeyz/xbox-emulator/pull/73#issuecomment-7071052335) 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`. | Check | Result | |---|---| | `hw_integer_fma_matches_reference` (ignored test, run with `--ignored`) | 0 of 5,242,880 results differ, denorm preserve and denorm any | | `hw_fast_fma_matches_exact_path` | `Fma { preserve: true, any: false, int: false }`: preserve 0 differ, any 503,648 differ. `int` stays off, as intended on a device with doubles | | `--lib` | 26 passed, 0 failed, 3 ignored | | `--test exec fma` | 3 passed on WARP, 3 passed on the 5090 (`TOMBOX_EXEC_HW=1`) | Not checked: - **WARP for the integer helper.** The `fma_probe` tests picked the hardware adapter in both of my runs; `TOMBOX_EXEC_HW` does not change that. - **Cost** of the integer helper against the f64 one. - An AMD device. 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 for `v_fma_f32` is one of the things the `nan` probe in #72 would measure, if someone with the hardware runs it.
Author
Owner

@⁠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; 0x7fc00000 for an invalid operation): I ran the nan probe from #72 on RDNA2 hardware (gfx1036), and v_fma_f32 doesn't quite do that. Details and the fitted rules are here: #41

In short:

  • an invalid operation returns 0xffc00000;
  • 0 * inf + NaN returns that default NaN rather than c;
  • a signalling NaN is returned unchanged in IEEE mode 0 and quieted only in mode 1.

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

**@⁠SchmashinPrunes** on GitHub · 2026-11-22 00:11 UTC · [original](https://github.com/omonkeyz/xbox-emulator/pull/73#issuecomment-7071379387) FYI, since `reference()` and the helpers here share main's NaN rule (first NaN operand, quieted; `0x7fc00000` for an invalid operation): I ran the `nan` probe from #72 on RDNA2 hardware (gfx1036), and `v_fma_f32` doesn't quite do that. Details and the fitted rules are here: https://git.gtahub.st/github/omonkeyz--xbox-emulator/pulls/41 In short: - an invalid operation returns `0xffc00000`; - `0 * inf + NaN` returns that default NaN rather than c; - a signalling NaN is returned unchanged in IEEE mode 0 and quieted only in mode 1. 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).
Author
Owner

@⁠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 fetch this repo, branch from the current main, cherry-pick only your own commits (the old branch is based on the previous history and cannot be merged), and open a new PR.

**@⁠omonkeyz** on GitHub · 2026-11-22 13:12 UTC · [original](https://github.com/omonkeyz/xbox-emulator/pull/73#issuecomment-7079646305) 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 fetch` this repo, branch from the current `main`, 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

This discussion has been locked. Commenting is limited to contributors.
No description provided.