Shaders: v_mad/v_mac stay unfused and v_fract stays below 1.0 on NVIDIA #12

Manually merged
github merged 1 commit from gh-pr/12/head into gh-pr/12/base 2026-11-27 03:13:36 +00:00
Owner

@⁠marquezinii on GitHub · 2026-11-24 21:00 UTC · original
marquezinii:shaders/nvidia-fma-rounding → main · +37 −12 · 6 files · 1 commits · merged 2026-11-24 21:44 UTC

The FMA/rounding pair from #9.

fma_mac. v_mad_f32 / v_mac_f32 (and v_madak / v_madmk) are unfused on AMD: the product is rounded before the add. The translator emitted them as a * b + c. dxc marks that fast and the NVIDIA driver contracts it into one fused operation, so v22/v23 read -2^-46 instead of 0 on the 5070. They now go through mad_unfused in the prelude, a precise product and sum. That fixes them without -Gis for the whole program. The DXIL backend already left them unfused, since it emits no fast flags.

v_fma_f32 / v_fmac_f32 stay mad(). NVIDIA fuses that, which is the console result; WARP doesn't. So the case now takes either value for v20/v21 (new Expected.Either) and keeps the strict unfused expectation for v22/v23. That test is the one that fails on the 5070 without the change.

round_ldexp. v_fract_f32 was frac(). On NVIDIA, x - floor(x) of a small negative x rounds to 1.0, where AMD clamps the result to 0x3F7FFFFF. fract_f32 adds that clamp, and a NaN stays NaN.

I didn't touch cross-instruction contraction: a v_mul_f32 followed by a separate v_add_f32 can still be fused by the driver once dxc marks both fast. Doing that everywhere is -Gis or precise on every float op, which costs every program, so I left that call to you.

Checked:

  • WARP: dotnet test 518 passed, 0 failed, no build warnings.
  • RTX 5070 with FIVESTAR_TEST_ADAPTER=hardware: fma_mac and round_ldexp pass with dxc and the DXIL backend. The shader cases now fail only in sin_large_arg (both backends) and msfb_ngg_strips_rootdesc, the two items of #9 still open.
**@⁠marquezinii** on GitHub · 2026-11-24 21:00 UTC · [original](https://github.com/ThatRecompDev/five-star-recomp/pull/12) `marquezinii:shaders/nvidia-fma-rounding` → `main` · +37 −12 · 6 files · 1 commits · merged 2026-11-24 21:44 UTC The FMA/rounding pair from #9. **`fma_mac`.** `v_mad_f32` / `v_mac_f32` (and `v_madak` / `v_madmk`) are unfused on AMD: the product is rounded before the add. The translator emitted them as `a * b + c`. dxc marks that fast and the NVIDIA driver contracts it into one fused operation, so v22/v23 read -2^-46 instead of 0 on the 5070. They now go through `mad_unfused` in the prelude, a `precise` product and sum. That fixes them without `-Gis` for the whole program. The DXIL backend already left them unfused, since it emits no fast flags. `v_fma_f32` / `v_fmac_f32` stay `mad()`. NVIDIA fuses that, which is the console result; WARP doesn't. So the case now takes either value for v20/v21 (new `Expected.Either`) and keeps the strict unfused expectation for v22/v23. That test is the one that fails on the 5070 without the change. **`round_ldexp`.** `v_fract_f32` was `frac()`. On NVIDIA, x - floor(x) of a small negative x rounds to 1.0, where AMD clamps the result to 0x3F7FFFFF. `fract_f32` adds that clamp, and a NaN stays NaN. I didn't touch cross-instruction contraction: a `v_mul_f32` followed by a separate `v_add_f32` can still be fused by the driver once dxc marks both fast. Doing that everywhere is `-Gis` or `precise` on every float op, which costs every program, so I left that call to you. Checked: - WARP: `dotnet test` 518 passed, 0 failed, no build warnings. - RTX 5070 with `FIVESTAR_TEST_ADAPTER=hardware`: `fma_mac` and `round_ldexp` pass with dxc and the DXIL backend. The shader cases now fail only in `sin_large_arg` (both backends) and `msfb_ngg_strips_rootdesc`, the two items of #9 still open.
v_mad_f32 and v_mac_f32 (and madak/madmk) round the product before the
add on AMD. They were emitted as a * b + c, which dxc marks fast, and the
NVIDIA driver contracts that into one fused operation: fma_mac's v22/v23
read -2^-46 instead of 0 on an RTX 5070. They now go through mad_unfused,
a precise product and sum. v_fract_f32 was frac(), whose x - floor(x)
rounds to 1.0 for a small negative x on NVIDIA; AMD clamps it to
0x3F7FFFFF, and fract_f32 does too (round_ldexp's v31).

v_fma_f32 stays mad(), fused on AMD and NVIDIA and unfused on WARP, so
fma_mac now accepts either for v20/v21 (Expected.Either) and keeps the
unfused expectation for v_mad/v_mac.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-27 03:13:35 +00:00
github manually merged commit 33d215a4c0 into gh-pr/12/base 2026-11-27 03:13:36 +00:00
Author
Owner

@⁠ThatRecompDev on GitHub · 2026-11-24 21:44 UTC · original

Merged, thanks! Checked it on our 4090 as well: fma_mac and round_ldexp pass there too, and the only cases left failing are sin_large_arg and msfb_ngg_strips_rootdesc, same as on your 5070. Nice that it stays exact without going to -Gis for everything.

**@⁠ThatRecompDev** on GitHub · 2026-11-24 21:44 UTC · [original](https://github.com/ThatRecompDev/five-star-recomp/pull/12#issuecomment-7145328628) Merged, thanks! Checked it on our 4090 as well: `fma_mac` and `round_ldexp` pass there too, and the only cases left failing are `sin_large_arg` and `msfb_ngg_strips_rootdesc`, same as on your 5070. Nice that it stays exact without going to `-Gis` for everything.
This discussion has been locked. Commenting is limited to contributors.
No description provided.