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

Open
SchmashinPrunes wants to merge 1 commit from upstream-pr/389 into main
SchmashinPrunes commented 2026-11-25 19:38:54 +00:00 (Migrated from github.com)

Imported from omonkeyz/xbox-emulator. Originally posted by SchmashinPrunes on 2026-11-25T19:38:54Z.


@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

<!-- greyfinch-sync:omonkeyz/xbox-emulator:issue:5497920486 --> Imported from [omonkeyz/xbox-emulator](https://github.com/omonkeyz/xbox-emulator/pull/389). Originally posted by [SchmashinPrunes](https://github.com/SchmashinPrunes) on 2026-11-25T19:38:54Z. --- @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)
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/389:upstream-pr/389'
git switch 'upstream-pr/389'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/389'
git switch 'upstream-pr/389'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/389'
git switch 'upstream-pr/389'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/389'
git switch 'main'
git merge --squash 'upstream-pr/389'
git switch 'main'
git merge --ff-only 'upstream-pr/389'
git switch 'main'
git merge 'upstream-pr/389'
git push origin 'main'
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!389
No description provided.