tombox-xshader: f16 NaN sign, propagation and default NaN as RDNA2 does #390

Open
SchmashinPrunes wants to merge 1 commit from upstream-pr/390 into main
SchmashinPrunes commented 2026-11-25 20:12:21 +00:00 (Migrated from github.com)

Imported from omonkeyz/xbox-emulator. Originally posted by SchmashinPrunes on 2026-11-25T20:12:21Z.


@omonkeyz, this is a small follow-up to #389 for the default MODE path. It also came out of comparing the translator with gfx1036 on every lane (#387 harness).

The bug

tb_hadd / tb_hsub / tb_hmul (the f16 add/sub/subrev/mul helpers from #375), and the f16 VOP3 abs/neg source modifiers, left NaN signs and the generated NaN to the host:

  • v_sub_f16(−0, NaN 0x7e00) gives 0xfe00 on gfx1036, but 0x7e00 on both WARP and an RX 9060 XT;
  • v_subrev_f16(NaN, 0) behaves the same way;
  • v_add_f16 with VOP3 neg on a NaN source behaves the same way.

The gfx1036 record (the #387 run, default MODE) gives the rule:

operands gfx1036
0xfe55 + 1.0, 0x7e00 + x the input NaN, payload and sign kept (not quieted)
−0 − 0x7e00 0xfe00: the subtrahend's NaN with its sign flipped
−inf + inf, inf × 0 0xfe00, the generated f16 NaN (the same rule #389 implements for the MODE path)

Change

  • tb_hadd/tb_hsub/tb_hmul propagate an input NaN, src0 first, with the subtrahend's sign flipped in tb_hsub, and return 0xfe00 for an invalid operation.
  • In h16, the f16 VOP3 abs and neg modifiers clear or flip the sign bit instead of using abs() / (-x).

The signalling-NaN passthrough (gfx1036 keeps 0x7c01, while the hosts' f16tof32 quiets it on the operand read) is not covered. That needs operand reads changed across all f16 ops.

Tests

New exec_vop::basic::f16_nan_sign_and_generation_as_rdna2, 8 cases from the gfx1036 record.

run result
WARP, without the hlsl.rs change fails 3 cases: the NaN sign after sub, subrev and VOP3 neg
WARP, tests/exec.rs 190/190
Radeon RX 9060 XT, tests/exec.rs 190/190
isa_sweep target, WARP ok (the 4 existing WARP_KNOWN f64 deviations)
translator vs gfx1036 on every lane, MODE 0xf0, WARP and AMD the qNaN sign differences are gone; what remains in f16 is the signalling-NaN passthrough above, plus the min/max signed-zero cases fixed separately in #388

The PR gate is clean. This touches the same h16 closure as #385 and the prelude next to #388's helpers, so whichever lands second may need a trivial rebase. I'll do it.

🤖 Generated with Claude Code

<!-- greyfinch-sync:omonkeyz/xbox-emulator:issue:5498412380 --> Imported from [omonkeyz/xbox-emulator](https://github.com/omonkeyz/xbox-emulator/pull/390). Originally posted by [SchmashinPrunes](https://github.com/SchmashinPrunes) on 2026-11-25T20:12:21Z. --- @omonkeyz, this is a small follow-up to #389 for the **default** MODE path. It also came out of comparing the translator with gfx1036 on every lane (#387 harness). ### The bug `tb_hadd` / `tb_hsub` / `tb_hmul` (the f16 add/sub/subrev/mul helpers from #375), and the f16 VOP3 `abs`/`neg` source modifiers, left NaN signs and the generated NaN to the host: - `v_sub_f16(−0, NaN 0x7e00)` gives `0xfe00` on gfx1036, but `0x7e00` on both WARP and an RX 9060 XT; - `v_subrev_f16(NaN, 0)` behaves the same way; - `v_add_f16` with VOP3 `neg` on a NaN source behaves the same way. The gfx1036 record (the #387 run, default MODE) gives the rule: | operands | gfx1036 | |---|---| | `0xfe55 + 1.0`, `0x7e00 + x` | the input NaN, payload and sign kept (not quieted) | | `−0 − 0x7e00` | `0xfe00`: the subtrahend's NaN with its sign flipped | | `−inf + inf`, `inf × 0` | `0xfe00`, the generated f16 NaN (the same rule #389 implements for the MODE path) | ### Change - `tb_hadd`/`tb_hsub`/`tb_hmul` propagate an input NaN, src0 first, with the subtrahend's sign flipped in `tb_hsub`, and return `0xfe00` for an invalid operation. - In `h16`, the f16 VOP3 `abs` and `neg` modifiers clear or flip the sign bit instead of using `abs()` / `(-x)`. The signalling-NaN passthrough (gfx1036 keeps `0x7c01`, while the hosts' `f16tof32` quiets it on the operand read) is not covered. That needs operand reads changed across all f16 ops. ### Tests New `exec_vop::basic::f16_nan_sign_and_generation_as_rdna2`, 8 cases from the gfx1036 record. | run | result | |---|---| | WARP, without the `hlsl.rs` change | fails 3 cases: the NaN sign after sub, subrev and VOP3 neg | | WARP, `tests/exec.rs` | 190/190 | | Radeon RX 9060 XT, `tests/exec.rs` | 190/190 | | `isa_sweep` target, WARP | ok (the 4 existing `WARP_KNOWN` f64 deviations) | | translator vs gfx1036 on every lane, MODE 0xf0, WARP and AMD | the qNaN sign differences are gone; what remains in f16 is the signalling-NaN passthrough above, plus the min/max signed-zero cases fixed separately in #388 | The PR gate is clean. This touches the same `h16` closure as #385 and the prelude next to #388's helpers, so whichever lands second may need a trivial rebase. I'll do it. 🤖 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/390:upstream-pr/390'
git switch 'upstream-pr/390'

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