tombox-xshader: f16 MODE path NaNs, before-rounding flush and SDWA, as measured on RDNA2 #389

Open
github-bot wants to merge 1 commit from github-bot/gh-389 into main AGit
Owner

@⁠SchmashinPrunes on GitHub · 2026-11-25 19:38 UTC · original
SchmashinPrunes:mode-path-f16-nan-sdwa → main · +37 −9 · 2 files · 1 commits · open

@⁠omonkeyz, this fixes the f16 MODE path: the code used when a shader writes MODE with s_setreg, or when its initial MODE (PGM_RSRC1) flushes f16/f64 denormals. I found the problems by running the ISA sweep on gfx1036 silicon under five initial MODEs (the #387 harness) and comparing the translator's output with the hardware on every lane, on WARP and on an RX 9060 XT. Only differences seen on both hosts count as translator bugs.

What was wrong, with f16/f64 denormals flushed

  1. NaN results became infinity. tb_h_rnd tested x != x, which both hosts fold away for this double. A NaN fell through to the overflow branch and came out as 0x7c00. Hit: v_cvt_f16_f32, v_add/sub/subrev/mul_f16, v_fma_f16, v_fmaak/fmamk/fmac_f16, v_ldexp_f16, v_pk_add/mul/fma/fmac_f16 and v_fma_mixlo/hi_f16. tb_h_rnd now tests the double's bits, as tb_fsub does for f32 (#375).
  2. NaN results now follow gfx1036 in IEEE 0:
    • an input NaN propagates with its payload and is not quieted, checking src0, then src1, then src2;
    • an invalid operation generates 0xfe00, for example v_mul_f16(inf, 0) and v_fma_f16(inf, 0, c). That's the f16 counterpart of the f32 default NaN 0xffc00000;
    • with IEEE 1 the propagated payload is quieted;
    • v_fma_mix* quiets an f32 signalling-NaN input on silicon (0x7fa97056 gives 0x7f4b), so the mix call passes the quiet bit.
  3. Tininess is checked before rounding. Under f16 output flush, silicon flushes an exact result below 2⁻¹⁴ to a zero of its sign before rounding. v_fmaak_f16(1 − 2⁻¹¹, 2⁻¹⁴, 0) gives 0x0000; the translator rounded it to 0x0400 and then saw nothing to flush. This is the same before-rounding rule #363 measured for f16 omod. v_fma_mix* is the exception: silicon returns 0x0400 there in every MODE, so mix keeps its previous behaviour.
  4. NaN sign after negation. v_sub/subrev_f16 negated with (-x), and WARP keeps a NaN's sign there while AMD flips it. Silicon flips it (v_subrev_f16(NaN, 0) gives 0xfe00), so these now flip the sign bit directly. The VOP3 neg modifier in vop3_mods still uses (-x); it's shared with the f64 path, so I left it for a follow-up.
  5. f16 SDWA was refused ("SDWA operand selection under a shader-written MODE is not supported"). mode_own ran before emit_vec's SDWA lowering. It now steps aside for SDWA, so the lowering materialises the operands and the MODE path handles the plain instruction. In this sweep that refusal had taken down 13 more mnemonics, because each pack is one program.

Results

Translator vs gfx1036, number of mnemonics that differ on both hosts:

initial MODE before after
0x30 (f16/f64 flushed) 47 20
0x00 (all flushed) 48 20
0xf0, 0xc0, 0xf0 + IEEE 17 / 17 / 46 unchanged, nothing new

The 20 that remain are out of scope here:

  • f32 omod (#386);
  • v_mul_lo_i32, absent on gfx103x;
  • v_div_fixup/fmas/scale_f64 in the f64 MODE path;
  • cube ops with NaN inputs;
  • v_pack_b32_f16 flushing;
  • f16 signalling NaNs read through f16tof32, which the hosts quiet. That one also happens at MODE 0xf0.

The IEEE 1 list (signalling-NaN quieting in transcendentals and v_min/max_f64) is a separate change.

Tests

New f16_flush_mode_nan_tininess_and_sdwa_as_rdna2: 9 cases taken from the gfx1036 outputs, run under f16/f64 flush set by s_setreg. They cover generated NaN (mul, fma), propagated NaN (add, subrev, ldexp, cvt_f16_f32), the before-rounding flush, a default-MODE control for it, and an SDWA v_add_f16.

run result
WARP, without the mode.rs change 7 of the 9 cases fail: NaN reads as 0x7c00, the tininess case gives 0x0400, SDWA is refused
WARP, tests/exec.rs 190/190
Radeon RX 9060 XT, tests/exec.rs 190/190
WARP, rest of the tombox-xshader suite all ok

The PR gate is clean.

🤖 Generated with Claude Code

**@⁠SchmashinPrunes** on GitHub · 2026-11-25 19:38 UTC · [original](https://github.com/omonkeyz/xbox-emulator/pull/389) `SchmashinPrunes:mode-path-f16-nan-sdwa` → `main` · +37 −9 · 2 files · 1 commits · open @⁠omonkeyz, this fixes the f16 MODE path: the code used when a shader writes MODE with `s_setreg`, or when its initial MODE (PGM_RSRC1) flushes f16/f64 denormals. I found the problems by running the ISA sweep on gfx1036 silicon under five initial MODEs (the #387 harness) and comparing the translator's output with the hardware on every lane, on WARP and on an RX 9060 XT. Only differences seen on both hosts count as translator bugs. ### What was wrong, with f16/f64 denormals flushed 1. **NaN results became infinity.** `tb_h_rnd` tested `x != x`, which both hosts fold away for this double. A NaN fell through to the overflow branch and came out as `0x7c00`. Hit: `v_cvt_f16_f32`, `v_add/sub/subrev/mul_f16`, `v_fma_f16`, `v_fmaak/fmamk/fmac_f16`, `v_ldexp_f16`, `v_pk_add/mul/fma/fmac_f16` and `v_fma_mixlo/hi_f16`. `tb_h_rnd` now tests the double's bits, as `tb_fsub` does for f32 (#375). 2. **NaN results now follow gfx1036 in IEEE 0:** - an input NaN propagates with its payload and is not quieted, checking src0, then src1, then src2; - an invalid operation generates **`0xfe00`**, for example `v_mul_f16(inf, 0)` and `v_fma_f16(inf, 0, c)`. That's the f16 counterpart of the f32 default NaN `0xffc00000`; - with IEEE 1 the propagated payload is quieted; - `v_fma_mix*` quiets an f32 signalling-NaN input on silicon (`0x7fa97056` gives `0x7f4b`), so the mix call passes the quiet bit. 3. **Tininess is checked before rounding.** Under f16 output flush, silicon flushes an exact result below 2⁻¹⁴ to a zero of its sign before rounding. `v_fmaak_f16(1 − 2⁻¹¹, 2⁻¹⁴, 0)` gives `0x0000`; the translator rounded it to `0x0400` and then saw nothing to flush. This is the same before-rounding rule #363 measured for f16 omod. `v_fma_mix*` is the exception: silicon returns `0x0400` there in every MODE, so mix keeps its previous behaviour. 4. **NaN sign after negation.** `v_sub/subrev_f16` negated with `(-x)`, and WARP keeps a NaN's sign there while AMD flips it. Silicon flips it (`v_subrev_f16(NaN, 0)` gives `0xfe00`), so these now flip the sign bit directly. The VOP3 `neg` modifier in `vop3_mods` still uses `(-x)`; it's shared with the f64 path, so I left it for a follow-up. 5. **f16 SDWA was refused** ("SDWA operand selection under a shader-written MODE is not supported"). `mode_own` ran before `emit_vec`'s SDWA lowering. It now steps aside for SDWA, so the lowering materialises the operands and the MODE path handles the plain instruction. In this sweep that refusal had taken down 13 more mnemonics, because each pack is one program. ### Results Translator vs gfx1036, number of mnemonics that differ on both hosts: | initial MODE | before | after | |---|---|---| | `0x30` (f16/f64 flushed) | 47 | 20 | | `0x00` (all flushed) | 48 | 20 | | `0xf0`, `0xc0`, `0xf0` + IEEE | 17 / 17 / 46 | unchanged, nothing new | The 20 that remain are out of scope here: - f32 omod (#386); - `v_mul_lo_i32`, absent on gfx103x; - `v_div_fixup/fmas/scale_f64` in the f64 MODE path; - cube ops with NaN inputs; - `v_pack_b32_f16` flushing; - f16 signalling NaNs read through `f16tof32`, which the hosts quiet. That one also happens at MODE 0xf0. The IEEE 1 list (signalling-NaN quieting in transcendentals and `v_min/max_f64`) is a separate change. ### Tests New `f16_flush_mode_nan_tininess_and_sdwa_as_rdna2`: 9 cases taken from the gfx1036 outputs, run under f16/f64 flush set by `s_setreg`. They cover generated NaN (mul, fma), propagated NaN (add, subrev, ldexp, cvt_f16_f32), the before-rounding flush, a default-MODE control for it, and an SDWA `v_add_f16`. | run | result | |---|---| | WARP, without the `mode.rs` change | 7 of the 9 cases fail: NaN reads as `0x7c00`, the tininess case gives `0x0400`, SDWA is refused | | WARP, `tests/exec.rs` | 190/190 | | Radeon RX 9060 XT, `tests/exec.rs` | 190/190 | | WARP, rest of the `tombox-xshader` suite | all ok | The PR gate is clean. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
Under a flushing f16/f64 denormal MODE (shader-written or initial):
- tb_h_rnd tested a double NaN with x != x, which the hosts fold, so NaN
  results became 0x7c00; it now tests the bits.
- An input NaN propagates with its payload (src0, src1, src2; quieted
  only with IEEE 1), an invalid operation generates 0xfe00, and fma_mix
  quiets an f32 signalling NaN, as gfx1036 does.
- An exact result below 2^-14 is flushed before rounding (fma_mix
  excepted), matching gfx1036 and the f16 omod rule.
- SDWA is no longer refused: mode_own defers to emit_vec's SDWA
  lowering and handles the instruction it re-enters with.
- v_sub/subrev_f16 negate by flipping the sign bit, so a NaN's sign does
  not depend on the host.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 06:21:11 +00:00
This pull request can be merged automatically.
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/389/head:github-bot/gh-389'
git switch 'github-bot/gh-389'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-389'
git switch 'github-bot/gh-389'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-389'
git switch 'github-bot/gh-389'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-389'
git switch 'main'
git merge --squash 'github-bot/gh-389'
git switch 'main'
git merge --ff-only 'github-bot/gh-389'
git switch 'main'
git merge 'github-bot/gh-389'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.