tombox-xshader: f16 NaNs keep their bits as on RDNA2, signalling ones included (stacked on #390) #392

Open
github-bot wants to merge 2 commits from github-bot/gh-392 into main AGit
Owner

@⁠SchmashinPrunes on GitHub · 2026-11-25 21:54 UTC · original
SchmashinPrunes:f16-snan-passthrough → main · +72 −31 · 3 files · 2 commits · open

@⁠omonkeyz, this is stacked on #390: the first commit is #390's, so review the second. It finishes the f16 NaN work. gfx1036 passes a signalling NaN through unquieted, and the translator quieted it on both the read (f16tof32) and the write (tb_f2h).

How it was found

The #387 host-vs-silicon comparison treats any f16 NaN as equal to any other, so this didn't show up there. With a bit-exact NaN check added locally (not part of this PR), 21 f16 mnemonics on WARP and 25 on the RX 9060 XT returned different NaN bits from gfx1036 at MODE 0xf0, for example v_add_f16(0x7c01, 0) gave 0x7e01 where silicon gives 0x7c01. The record shows the f16 ALU ops pass the input NaN through with its exact bits.

The rule (gfx1036, default MODE)

op result for NaN inputs
add, sub, mul, floor/ceil/trunc/rndne/fract, ldexp, rcp/sqrt/rsq, v_pk_add/mul the first NaN source, bits unchanged (src0 first)
v_subrev_f16 src0 (the subtrahend) is checked first, sign flipped (subrev(0x7e00, 0x7e00) gives 0xfe00)
min, max, v_pk_min/max src0 when both sources are NaN (one NaN still returns the other operand, as before)
fma, fmac, fmaak, fmamk, v_pk_fma, v_pk_fmac src0, then src1, then an invalid product (inf × 0, which gives 0xfe00 even when src2 is a NaN), then src2
v_fract_f16(±inf) 0xfe00
v_cvt_f32_f16(0x7c01) 0x7f802000: still a signalling NaN

Change

  • tb_h2fk reads f16 bits into an f32, keeping a signalling NaN signalling. tb_f2hk writes f16 without forcing the quiet bit. For quiet NaNs it gives exactly what tb_f2h gives.
  • v_cvt_f16_f32 keeps tb_f2h, because silicon quiets there.
  • tb_hp1 / tb_hp2 / tb_hpm / tb_hpf implement the table. Packed-op neg now flips the sign bit, as #390 did for VOP3.
  • exec_vop/fract.rs: the f16 inf/NaN expectations follow the gfx1036 record instead of a canonical 0x7e00. ±inf gives 0xfe00, NaNs pass through, and a 0x7c01 row is added.

Tests

New f16_signalling_nan_passes_through_as_rdna2: 12 gfx1036 vectors.

run result
WARP, without the hlsl.rs change 10 of the 12 cases fail
WARP, tests/exec.rs 191/191 (run three times; one earlier run lost the WARP device partway through and was rerun)
Radeon RX 9060 XT, tests/exec.rs 191/191
isa_sweep target, WARP ok (existing WARP_KNOWN deviations only)
f16 mnemonics with NaN-bit differences from gfx1036, WARP / AMD 21 → 3 / 25 → 2

What's left in f16: v_fma_mixlo/hi_f16, which have their own f32/f16 conversion rules, and v_cvt_pkrtz_f16_f32(qNaN) giving 0x7fff on WARP only. #387's comparison (NaNs equal) is unchanged apart from these ops, plus one case that was hidden before: AMD-host SDWA byte-select + abs on a denormal. That's the known AMD-host case from #375 and isn't related to this change.

A question: f32 and f64 NaN bits

The same bit-exact check shows the same class of difference in f32 and f64, in about 30 mnemonics on each host:

  • signalling NaNs are quieted: v_add_f32(0x7f800001, 0) gives 0x7fc00001, silicon 0x7f800001;
  • the generated NaN is +qNaN where silicon's default is negative: v_fma_f32(inf, 0, c) gives 0x7fc00000, silicon 0xffc00000;
  • neg on a NaN source doesn't flip its sign.

Matching silicon there means a NaN check on every f32/f64 add and multiply, which costs speed in every shader, not just ones that produce NaNs. f16 already went through helpers, so the extra cost here is small. Do you want f32/f64 handled the same way? If so, I'll do it as its own PR.

The PR gate is clean. No game or console files are involved.

🤖 Generated with Claude Code

**@⁠SchmashinPrunes** on GitHub · 2026-11-25 21:54 UTC · [original](https://github.com/omonkeyz/xbox-emulator/pull/392) `SchmashinPrunes:f16-snan-passthrough` → `main` · +72 −31 · 3 files · 2 commits · open @⁠omonkeyz, this is **stacked on #390**: the first commit is #390's, so review the second. It finishes the f16 NaN work. gfx1036 passes a signalling NaN through unquieted, and the translator quieted it on both the read (`f16tof32`) and the write (`tb_f2h`). ### How it was found The #387 host-vs-silicon comparison treats any f16 NaN as equal to any other, so this didn't show up there. With a bit-exact NaN check added locally (not part of this PR), 21 f16 mnemonics on WARP and 25 on the RX 9060 XT returned different NaN bits from gfx1036 at MODE 0xf0, for example `v_add_f16(0x7c01, 0)` gave `0x7e01` where silicon gives `0x7c01`. The record shows the f16 ALU ops pass the input NaN through with its exact bits. ### The rule (gfx1036, default MODE) | op | result for NaN inputs | |---|---| | add, sub, mul, floor/ceil/trunc/rndne/fract, ldexp, rcp/sqrt/rsq, `v_pk_add/mul` | the first NaN source, bits unchanged (src0 first) | | `v_subrev_f16` | src0 (the subtrahend) is checked first, sign flipped (`subrev(0x7e00, 0x7e00)` gives `0xfe00`) | | min, max, `v_pk_min/max` | src0 when both sources are NaN (one NaN still returns the other operand, as before) | | fma, fmac, fmaak, fmamk, `v_pk_fma`, `v_pk_fmac` | src0, then src1, then an **invalid product** (inf × 0, which gives `0xfe00` even when src2 is a NaN), then src2 | | `v_fract_f16(±inf)` | `0xfe00` | | `v_cvt_f32_f16(0x7c01)` | `0x7f802000`: still a signalling NaN | ### Change - `tb_h2fk` reads f16 bits into an f32, keeping a signalling NaN signalling. `tb_f2hk` writes f16 without forcing the quiet bit. For quiet NaNs it gives exactly what `tb_f2h` gives. - `v_cvt_f16_f32` keeps `tb_f2h`, because silicon quiets there. - `tb_hp1` / `tb_hp2` / `tb_hpm` / `tb_hpf` implement the table. Packed-op `neg` now flips the sign bit, as #390 did for VOP3. - `exec_vop/fract.rs`: the f16 inf/NaN expectations follow the gfx1036 record instead of a canonical `0x7e00`. ±inf gives `0xfe00`, NaNs pass through, and a `0x7c01` row is added. ### Tests New `f16_signalling_nan_passes_through_as_rdna2`: 12 gfx1036 vectors. | run | result | |---|---| | WARP, without the `hlsl.rs` change | 10 of the 12 cases fail | | WARP, `tests/exec.rs` | 191/191 (run three times; one earlier run lost the WARP device partway through and was rerun) | | Radeon RX 9060 XT, `tests/exec.rs` | 191/191 | | `isa_sweep` target, WARP | ok (existing `WARP_KNOWN` deviations only) | | f16 mnemonics with NaN-bit differences from gfx1036, WARP / AMD | 21 → 3 / 25 → 2 | What's left in f16: `v_fma_mixlo/hi_f16`, which have their own f32/f16 conversion rules, and `v_cvt_pkrtz_f16_f32(qNaN)` giving `0x7fff` on WARP only. #387's comparison (NaNs equal) is unchanged apart from these ops, plus one case that was hidden before: AMD-host SDWA byte-select + `abs` on a denormal. That's the known AMD-host case from #375 and isn't related to this change. ### A question: f32 and f64 NaN bits The same bit-exact check shows the same class of difference in f32 and f64, in about 30 mnemonics on each host: - signalling NaNs are quieted: `v_add_f32(0x7f800001, 0)` gives `0x7fc00001`, silicon `0x7f800001`; - the generated NaN is `+qNaN` where silicon's default is negative: `v_fma_f32(inf, 0, c)` gives `0x7fc00000`, silicon `0xffc00000`; - `neg` on a NaN source doesn't flip its sign. Matching silicon there means a NaN check on every f32/f64 add and multiply, which costs speed in every shader, not just ones that produce NaNs. f16 already went through helpers, so the extra cost here is small. Do you want f32/f64 handled the same way? If so, I'll do it as its own PR. The PR gate is clean. No game or console files are involved. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
At the default MODE, tb_hadd/tb_hsub/tb_hmul left a NaN's sign and the
generated NaN to the host: -0 - NaN gave 0x7e00 on WARP and AMD where
gfx1036 gives 0xfe00. They now propagate an input NaN with its payload
(src0 first, the subtrahend's sign flipped) and return 0xfe00 for an
invalid operation, as recorded on gfx1036. The f16 VOP3 abs and neg
source modifiers clear or flip the sign bit instead of using abs()/(-x),
whose effect on a NaN is host dependent.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
f16 operands were read with f16tof32 and results written with tb_f2h,
and both quiet a signalling NaN, so v_add_f16(0x7c01, 0) gave 0x7e01
where gfx1036 returns 0x7c01. Operands are now read with tb_h2fk and
results written with tb_f2hk, which keep a NaN's bits, and each op
returns its first NaN source as gfx1036 does:

- add/sub/mul, the rounding ops, ldexp, rcp/sqrt/rsq and the packed
  ops pass the first NaN source through (src0 first);
- v_subrev_f16 checks its subtrahend, src0, first (sign flipped);
- min/max return src0 when both sources are NaN;
- the fma family checks src0, src1, then an invalid product (inf * 0,
  which gives 0xfe00 even when src2 is a NaN), then src2;
- v_fract_f16 of +-inf gives 0xfe00;
- v_cvt_f32_f16 keeps a signalling NaN signalling.

The fract exec expectations for inf/NaN follow the gfx1036 record.

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:16 +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/392/head:github-bot/gh-392'
git switch 'github-bot/gh-392'

Merge

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