xshader: guard f32 sub and f16 add/sub/mul against AMD host folding (fixes three #333 failures) #375

Closed
SchmashinPrunes wants to merge 2 commits from upstream-pr/375 into main
SchmashinPrunes commented 2026-11-24 18:44:34 +00:00 (Migrated from github.com)

Imported from omonkeyz/xbox-emulator. Originally posted by SchmashinPrunes on 2026-11-24T18:44:34Z.


Library / area: tombox-xshader. hlsl.rs gets the tb_fsub guard and new tb_hadd/tb_hsub/tb_hmul helpers; tests/exec.rs gets the UAV view fix in run_res_psv.

Fixes the three #333 failures that reproduce on our AMD host, a Radeon RX 9060 XT with TOMBOX_EXEC_HW=1. They pass on WARP, and per #333 on an RTX 3070.

What was wrong

test AMD result cause
vop2_basic runtime inputs #642/643/707/708 v_sub_f32/v_subrev_f32 with ±inf − ±inf gives 0, want 0x7fc00000 Both runtime-input operands are the same expression (0x7f800000 | v200), so DXC's CSE merges them. The DXIL is fsub float %16, %16, and the precise in tb_fsub keeps fast-math flags off it, but AMD's driver still folds x - x to 0.
f16_omod_follows_mode runtime inputs #4 v_add_f16(-0, -0) gives +0 DXC lowers f16tof32(x) + f16tof32(x) to fmul fast x, 2.0, and AMD drops the zero sign. Compiling with -Gis does not help.
scalar_smem_loads #39–46 s_load_dword* descriptor probes read word 3 of the slot instead of word 9 (e.g. a5002323 vs a5002929) Harness bug: run_res_psv only recognised RWStructuredBuffer<TbElem_ubuf0>. These probes use ubuf1/ubuf2/ubuf6, which were therefore given typed R32_UINT views under a structured declaration.

The change

  • tb_fsub returns the quiet NaN from an integer test when either input is NaN or both are the same infinity. Otherwise it returns the precise difference. Finite inputs never produce NaN, so the old tb_qn on the result is no longer needed.

  • v_add_f16, v_sub_f16, v_subrev_f16 and v_mul_f16 now go through tb_hadd/tb_hsub/tb_hmul. When the f32 result is ±0, these OR in the IEEE round-to-nearest zero sign with integer ops:

    • add: −0 only when both operands are −0;
    • sub: −0 only for −0 − (+0);
    • mul: the XOR of the operand signs.

    An f16×f16 or f16±f16 result can't underflow to an f32 zero, so a zero result is always exact.

  • The harness picks the structured view stride from any TbElem_ubufN declaration, not just ubuf0.

Validation

  • RX 9060 XT, TOMBOX_EXEC_HW=1 cargo test --locked -p tombox-xshader --release --test exec: 128 passed, 0 failed. On main the same run fails scalar_smem_loads, vop2_basic and f16_omod_follows_mode.
  • WARP, cargo test --locked -p tombox-xshader --release --no-fail-fast: all pass.
  • isa_sweep on the RX 9060 XT: the first commit fixes the v_mul_f16 signed-zero case. The remaining sweep failures are handled in the second commit (below).
  • The upstream PR gate passes.

Second commit: the remaining AMD sweep failures

This comes from the RX 9060 XT isa_sweep run (TOMBOX_EXEC_HW=1). Failing mnemonics go from 11 to 3.

sweep failure on AMD cause change
v_trunc/floor/ceil/fract_f64 with VOP3 neg: trunc(−1.0) = +1.0, floor(−1.0) = 0 The helpers truncate through (double)(int64_t)x. The DXIL is correct (fsub fast -0.0, x then fptosi), but AMD's driver drops the folded negation in the f64→i64 conversion. tb_trunc64 clears the fraction bits with integer masks, with no int64 conversion. Checked bit for bit against math.trunc on 400k doubles: ±0, denormals, the 2^52 boundary, infinities.
v_fmaak/v_madak_f32 and v_fmaak_f16 with K = 0: −0·0 + 0 gives −0 AMD folds x + 0.0 to x, the same class as the x − x fold above. tb_pz forces an exact zero result to +0. It's also applied to v_add_f32 with an inline/literal +0 and to v_fma/v_mad_f32/v_fma_f16 with a +0 addend, before denorm flushing and output modifiers. For the fused f32 forms, tb_pzf applies it only when an operand is zero, since a product that underflows must still round to a signed zero.
v_ldexp_f16(−0, e) gives +0 Zero sign lost in ldexp. The result keeps the input's zero sign.

It also fixes a bug in this PR's first commit: tb_hz OR-ed the zero sign in, so it could never turn a folded −0 back into +0. It now sets the sign.

Tests.

  • New exec test amd_host_zero_sign_and_f64_neg: 48 cases with constant and runtime inputs, including controls where the guard must not fire (a neg-modified constant, K = −0, a fused underflow).
  • On the RX 9060 XT without the hlsl.rs changes, 23 runtime-input cases fail. With them, the test passes there and on WARP.
  • WARP now matches the reference for v_ceil/floor/trunc/fract_f64, v_madak_f32 and v_fmaak_f16, so they're removed from WARP_KNOWN in isa_sweep.rs and CI guards them.

Still failing on AMD, not addressed: v_add/sub/mul_f16 with SDWA byte select plus abs on src0. The operand reduces to and i32 %x, 255 before legacyF16ToF32, so it is provably a positive f16 denormal, and AMD returns 0 for it in the sweep's compute shader. The same pattern in the exec harness's pixel shader passes, the neg variant passes, and forcing -denorm preserve or -denorm any makes no difference. A real compiler wouldn't emit SDWA byte select on an f16 operand, so I've left it.

Notes

  • The fourth #333 test, mtbuf_d16_store_formats, passes on the RX 9060 XT, so this PR doesn't touch it.
  • #346 edits the same if line in run_res_psv. It's a one-line textual conflict, and I'll rebase whichever lands second. #347 and #368 merge cleanly with this branch.

🤖 Generated with Claude Code

<!-- greyfinch-sync:omonkeyz/xbox-emulator:issue:5478232484 --> Imported from [omonkeyz/xbox-emulator](https://github.com/omonkeyz/xbox-emulator/pull/375). Originally posted by [SchmashinPrunes](https://github.com/SchmashinPrunes) on 2026-11-24T18:44:34Z. --- **Library / area:** tombox-xshader. `hlsl.rs` gets the `tb_fsub` guard and new `tb_hadd`/`tb_hsub`/`tb_hmul` helpers; `tests/exec.rs` gets the UAV view fix in `run_res_psv`. Fixes the three #333 failures that reproduce on our AMD host, a Radeon RX 9060 XT with `TOMBOX_EXEC_HW=1`. They pass on WARP, and per #333 on an RTX 3070. ### What was wrong | test | AMD result | cause | |---|---|---| | `vop2_basic` runtime inputs #642/643/707/708 | `v_sub_f32`/`v_subrev_f32` with ±inf − ±inf gives `0`, want `0x7fc00000` | Both runtime-input operands are the same expression (`0x7f800000 \| v200`), so DXC's CSE merges them. The DXIL is `fsub float %16, %16`, and the `precise` in `tb_fsub` keeps fast-math flags off it, but AMD's driver still folds `x - x` to 0. | | `f16_omod_follows_mode` runtime inputs #4 | `v_add_f16(-0, -0)` gives `+0` | DXC lowers `f16tof32(x) + f16tof32(x)` to `fmul fast x, 2.0`, and AMD drops the zero sign. Compiling with `-Gis` does not help. | | `scalar_smem_loads` #39–46 | `s_load_dword*` descriptor probes read word 3 of the slot instead of word 9 (e.g. `a5002323` vs `a5002929`) | Harness bug: `run_res_psv` only recognised `RWStructuredBuffer<TbElem_ubuf0>`. These probes use `ubuf1`/`ubuf2`/`ubuf6`, which were therefore given typed `R32_UINT` views under a structured declaration. | ### The change - `tb_fsub` returns the quiet NaN from an integer test when either input is NaN or both are the same infinity. Otherwise it returns the `precise` difference. Finite inputs never produce NaN, so the old `tb_qn` on the result is no longer needed. - `v_add_f16`, `v_sub_f16`, `v_subrev_f16` and `v_mul_f16` now go through `tb_hadd`/`tb_hsub`/`tb_hmul`. When the f32 result is ±0, these OR in the IEEE round-to-nearest zero sign with integer ops: - add: −0 only when both operands are −0; - sub: −0 only for −0 − (+0); - mul: the XOR of the operand signs. An f16×f16 or f16±f16 result can't underflow to an f32 zero, so a zero result is always exact. - The harness picks the structured view stride from any `TbElem_ubufN` declaration, not just `ubuf0`. ### Validation - RX 9060 XT, `TOMBOX_EXEC_HW=1 cargo test --locked -p tombox-xshader --release --test exec`: 128 passed, 0 failed. On `main` the same run fails `scalar_smem_loads`, `vop2_basic` and `f16_omod_follows_mode`. - WARP, `cargo test --locked -p tombox-xshader --release --no-fail-fast`: all pass. - `isa_sweep` on the RX 9060 XT: the first commit fixes the `v_mul_f16` signed-zero case. The remaining sweep failures are handled in the second commit (below). - The upstream PR gate passes. ### Second commit: the remaining AMD sweep failures This comes from the RX 9060 XT `isa_sweep` run (`TOMBOX_EXEC_HW=1`). Failing mnemonics go from 11 to 3. | sweep failure on AMD | cause | change | |---|---|---| | `v_trunc/floor/ceil/fract_f64` with VOP3 `neg`: trunc(−1.0) = +1.0, floor(−1.0) = 0 | The helpers truncate through `(double)(int64_t)x`. The DXIL is correct (`fsub fast -0.0, x` then `fptosi`), but AMD's driver drops the folded negation in the f64→i64 conversion. | `tb_trunc64` clears the fraction bits with integer masks, with no int64 conversion. Checked bit for bit against `math.trunc` on 400k doubles: ±0, denormals, the 2^52 boundary, infinities. | | `v_fmaak/v_madak_f32` and `v_fmaak_f16` with K = 0: −0·0 + 0 gives −0 | AMD folds `x + 0.0` to `x`, the same class as the `x − x` fold above. | `tb_pz` forces an exact zero result to +0. It's also applied to `v_add_f32` with an inline/literal +0 and to `v_fma/v_mad_f32`/`v_fma_f16` with a +0 addend, before denorm flushing and output modifiers. For the fused f32 forms, `tb_pzf` applies it only when an operand is zero, since a product that underflows must still round to a signed zero. | | `v_ldexp_f16(−0, e)` gives +0 | Zero sign lost in `ldexp`. | The result keeps the input's zero sign. | It also fixes a bug in this PR's first commit: `tb_hz` OR-ed the zero sign in, so it could never turn a folded −0 back into +0. It now sets the sign. **Tests.** - New exec test `amd_host_zero_sign_and_f64_neg`: 48 cases with constant and runtime inputs, including controls where the guard must not fire (a `neg`-modified constant, K = −0, a fused underflow). - On the RX 9060 XT without the `hlsl.rs` changes, 23 runtime-input cases fail. With them, the test passes there and on WARP. - WARP now matches the reference for `v_ceil/floor/trunc/fract_f64`, `v_madak_f32` and `v_fmaak_f16`, so they're removed from `WARP_KNOWN` in `isa_sweep.rs` and CI guards them. **Still failing on AMD, not addressed:** `v_add/sub/mul_f16` with SDWA byte select plus `abs` on src0. The operand reduces to `and i32 %x, 255` before `legacyF16ToF32`, so it is provably a positive f16 denormal, and AMD returns 0 for it in the sweep's compute shader. The same pattern in the exec harness's pixel shader passes, the `neg` variant passes, and forcing `-denorm preserve` or `-denorm any` makes no difference. A real compiler wouldn't emit SDWA byte select on an f16 operand, so I've left it. ### Notes - The fourth #333 test, `mtbuf_d16_store_formats`, passes on the RX 9060 XT, so this PR doesn't touch it. - #346 edits the same `if` line in `run_res_psv`. It's a one-line textual conflict, and I'll rebase whichever lands second. #347 and #368 merge cleanly with this branch. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
omonkeyz commented 2026-11-25 13:50:59 +00:00 (Migrated from github.com)

Imported from omonkeyz/xbox-emulator. Originally posted by omonkeyz on 2026-11-25T13:50:59Z.


Applied on main in d0fabcb (cherry-picked onto the current history with your authorship kept; comment lines stripped per CONTRIBUTING). Thank you.

<!-- greyfinch-sync:omonkeyz/xbox-emulator:comment:7164379983 --> Imported from [omonkeyz/xbox-emulator](https://github.com/omonkeyz/xbox-emulator/pull/375#issuecomment-7164379983). Originally posted by [omonkeyz](https://github.com/omonkeyz) on 2026-11-25T13:50:59Z. --- Applied on main in d0fabcb (cherry-picked onto the current history with your authorship kept; comment lines stripped per CONTRIBUTING). Thank you.

Pull request closed

Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
omonkeyz/xbox-emulator!375
No description provided.