fix: v_min/max_f32 and v_min3/max3/med3_f32 give the other operand for a NaN and order -0 below +0 #269

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

@⁠lcg007xd-c on GitHub · 2026-11-25 22:52 UTC · original
lcg007xd-c:fix/rdna2-f32-min-max-nan → main · +140 −7 · 4 files · 1 commits · open

Change

v_min_f32, v_max_f32 (VOP2, VOP3, SDWA and DPP) and v_min3_f32, v_max3_f32, v_med3_f32 used Python's min/max/sorted. Python returns a NaN in the first position and keeps the first of two zeros of different sign, which is not what the ISA says. Results on main (e921268):

Instruction Sources main ISA
v_min_f32 NaN, 1.0 7FC00000 (NaN) 3F800000 (1.0)
v_max_f32 NaN, 1.0 7FC00000 3F800000
v_min_f32 +0, -0 00000000 80000000 (-0)
v_max_f32 -0, +0 80000000 00000000 (+0)
v_min3_f32 NaN, 1.0, 0 7FC00000 00000000
v_med3_f32 NaN, 1.0, 0 7FC00000 00000000
v_med3_f32 0, 1.0, NaN 3F800000 00000000

The pseudo-code in AMD's "RDNA 2" ISA (November 2020):

  • V_MIN_F32 / V_MAX_F32 (section 12.7, p. 132-133): else if (S0.f == NaN) D.f = S1.f; else if (S1.f == NaN) D.f = S0.f, and -0.0 is below +0.0.
  • V_MIN3_F32 / V_MAX3_F32 (p. 170) chain them, and V_MED3_F32 returns V_MIN3_F32(S0.f, S1.f, S2.f) when a source is NaN. So the med3(x, 0, 1.0) clamp gives 0 for a NaN.

v_min/max_f16 (half_min/half_max) and v_min/max_f64 (_pick) already follow these rules, and the HLSL min()/max() (DXIL FMin/FMax) already return the non-NaN operand, so only the f32 interpreter disagreed. The rules now live in the new emu/rdna2_exec_minmax.py, which the VOP2 table (rdna2_exec_ops.VECTOR, also used by VOP3, SDWA and DPP) and the VOP3 three-source table use. The IEEE_MODE cases (a signalling NaN comes back quieted) are not modelled, as for f16 and f64. Every NaN is already non-signalling once read (bits_float).

Not in this PR: the HLSL v_med3_f32 (max(min(a, b), min(max(a, b), c))) gives b for med3(NaN, b, c) where the ISA gives min(b, c). That is a separate HLSL change, which needs a WARP/GPU run.

Linked issue: none.

Verification

Linux, Python 3.14, no C++ runtime, no GPU (the runtime and GPU tests skip):

python -m pytest → 1094 passed, 325 skipped
python -m pytest tests/test_rdna2_exec_minmax.py → 3 passed (on main without the fix: 3 failed)
python -m ruff check --select W605,F,E9 PrototypeScarlett tests tools → All checks passed!

Not run: the Windows CI job and the GPU/WARP parity tests. No HLSL code changes.

Oracle and test data

  • How it was found: differential testing of the interpreter against LLVM's constant folding of llvm.amdgcn.fmed3.f32 (opt -passes=instcombine -mtriple=amdgcn-amd-amdhsa -mcpu=gfx1030, LLVM 23.1.5), with 3 × 256 random and edge-case source triples. On main, 12 of 256 differed. With the fix, every non-signalling NaN case matches. The 5-7 left per run all have a signalling NaN source (7F800001, 7FA00000), which LLVM quiets as in IEEE_MODE.
  • New tests/test_rdna2_exec_minmax.py: 14 VOP2+VOP3 pairs (NaN in either source, both NaN, ±0 in both orders, ±infinity), -v1 against v1 = +0 through the VOP3 neg modifier, and 16 min3/max3/med3 triples. Expected values are worked out by hand from the ISA pseudo-code, and the med3 NaN cases also agree with LLVM's folding. The data is built in the test.
  • Failure case: without the fix, all 3 tests fail.

Checklist

  • The PR covers a single reviewable behaviour.
  • A test fails without this change and passes with it.
  • The commands and results of each check are given above.
  • An unimplemented function returns an error (E_NOTIMPL), never a fake success.

AI assistance: written with Claude Code (Claude Opus 5.5).

🤖 Generated with Claude Code

**@⁠lcg007xd-c** on GitHub · 2026-11-25 22:52 UTC · [original](https://github.com/LooperSalty/AnyXboxX/pull/269) `lcg007xd-c:fix/rdna2-f32-min-max-nan` → `main` · +140 −7 · 4 files · 1 commits · open ## Change `v_min_f32`, `v_max_f32` (VOP2, VOP3, SDWA and DPP) and `v_min3_f32`, `v_max3_f32`, `v_med3_f32` used Python's `min`/`max`/`sorted`. Python returns a NaN in the first position and keeps the first of two zeros of different sign, which is not what the ISA says. Results on `main` (`e921268`): | Instruction | Sources | `main` | ISA | | --- | --- | --- | --- | | `v_min_f32` | NaN, 1.0 | `7FC00000` (NaN) | `3F800000` (1.0) | | `v_max_f32` | NaN, 1.0 | `7FC00000` | `3F800000` | | `v_min_f32` | +0, -0 | `00000000` | `80000000` (-0) | | `v_max_f32` | -0, +0 | `80000000` | `00000000` (+0) | | `v_min3_f32` | NaN, 1.0, 0 | `7FC00000` | `00000000` | | `v_med3_f32` | NaN, 1.0, 0 | `7FC00000` | `00000000` | | `v_med3_f32` | 0, 1.0, NaN | `3F800000` | `00000000` | The pseudo-code in AMD's "RDNA 2" ISA (November 2020): - V_MIN_F32 / V_MAX_F32 (section 12.7, p. 132-133): `else if (S0.f == NaN) D.f = S1.f; else if (S1.f == NaN) D.f = S0.f`, and -0.0 is below +0.0. - V_MIN3_F32 / V_MAX3_F32 (p. 170) chain them, and V_MED3_F32 returns `V_MIN3_F32(S0.f, S1.f, S2.f)` when a source is NaN. So the `med3(x, 0, 1.0)` clamp gives 0 for a NaN. `v_min/max_f16` (`half_min`/`half_max`) and `v_min/max_f64` (`_pick`) already follow these rules, and the HLSL `min()`/`max()` (DXIL FMin/FMax) already return the non-NaN operand, so only the f32 interpreter disagreed. The rules now live in the new `emu/rdna2_exec_minmax.py`, which the VOP2 table (`rdna2_exec_ops.VECTOR`, also used by VOP3, SDWA and DPP) and the VOP3 three-source table use. The IEEE_MODE cases (a signalling NaN comes back quieted) are not modelled, as for f16 and f64. Every NaN is already non-signalling once read (`bits_float`). Not in this PR: the HLSL `v_med3_f32` (`max(min(a, b), min(max(a, b), c))`) gives `b` for `med3(NaN, b, c)` where the ISA gives `min(b, c)`. That is a separate HLSL change, which needs a WARP/GPU run. Linked issue: none. ## Verification Linux, Python 3.14, no C++ runtime, no GPU (the runtime and GPU tests skip): ``` python -m pytest → 1094 passed, 325 skipped python -m pytest tests/test_rdna2_exec_minmax.py → 3 passed (on main without the fix: 3 failed) python -m ruff check --select W605,F,E9 PrototypeScarlett tests tools → All checks passed! ``` Not run: the Windows CI job and the GPU/WARP parity tests. No HLSL code changes. ## Oracle and test data - How it was found: differential testing of the interpreter against LLVM's constant folding of `llvm.amdgcn.fmed3.f32` (`opt -passes=instcombine -mtriple=amdgcn-amd-amdhsa -mcpu=gfx1030`, LLVM 23.1.5), with 3 × 256 random and edge-case source triples. On `main`, 12 of 256 differed. With the fix, every non-signalling NaN case matches. The 5-7 left per run all have a signalling NaN source (`7F800001`, `7FA00000`), which LLVM quiets as in IEEE_MODE. - New `tests/test_rdna2_exec_minmax.py`: 14 VOP2+VOP3 pairs (NaN in either source, both NaN, ±0 in both orders, ±infinity), `-v1` against `v1` = +0 through the VOP3 neg modifier, and 16 min3/max3/med3 triples. Expected values are worked out by hand from the ISA pseudo-code, and the med3 NaN cases also agree with LLVM's folding. The data is built in the test. - Failure case: without the fix, all 3 tests fail. ## Checklist - [x] The PR covers a single reviewable behaviour. - [x] A test fails without this change and passes with it. - [x] The commands and results of each check are given above. - [x] An unimplemented function returns an error (`E_NOTIMPL`), never a fake success. AI assistance: written with Claude Code (Claude Opus 5.5). 🤖 Generated with [Claude Code](https://claude.com/claude-code)
The interpreter used Python's min and max, which return a NaN in the
first position (min(nan, 1.0) is nan) and keep the first of two zeros of
different sign (max(-0.0, 0.0) is -0.0). The ISA pseudo-code of
V_MIN_F32 and V_MAX_F32 (p. 132-133) gives the other operand when one is
NaN and orders -0.0 below +0.0; V_MIN3/MAX3/MED3_F32 (p. 170) are built
on them, and V_MED3_F32 returns V_MIN3_F32 of its sources when one is
NaN, so the med3(x, 0, 1) clamp turns a NaN into 0. v_min/max_f16 and
v_min/max_f64 already followed these rules, and the HLSL min()/max()
already return the non-NaN operand.

The new emu/rdna2_exec_minmax.py serves the VOP2/VOP3/SDWA/DPP tables.
Checked against LLVM's constant folding of llvm.amdgcn.fmed3: every
non-signalling NaN case matches; only signalling NaNs differ
(IEEE_MODE, not modelled, as for f16 and f64).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 05:41:14 +00:00
Author
Owner

@⁠KainanP on GitHub · 2026-11-25 23:42 UTC · original

Setup: Windows 11 26200, dxc 1.8.2502.11 (Windows SDK 10.0.26100.0), Python 3.12.10, C++ runtime built from main 3e258c8. Adapters: RTX 4060 Laptop (driver 32.0.16.1047, i.e. 610.47; default adapter), Intel UHD 8086:a78b (31.0.101.5445, ANYXBOXX_GPU=Intel, 16 lanes, no FP64), WARP 10.0.26100.9818 (ANYXBOXX_GPU=warp). This PR was merged onto main 3e258c8 (clean merge).

main this PR
RTX 4060, python -m pytest 1404 passed, 17 skipped 1407 passed, 17 skipped
WARP, tests/test_rdna2*.py 996 passed, 2 skipped 999 passed, 2 skipped
Intel UHD, tests/test_rdna2*.py 994 passed, 4 skipped 997 passed, 4 skipped
tests/test_rdna2_exec_minmax.py on main code 3 failed 3 passed

HLSL side (rule 4). No GPU parity test runs v_min/max_f32 or v_min3/max3/med3_f32, so the runs above don't check the HLSL side. I ran a probe program on 32 lanes: your tests' sources plus more mixes of NaN and ±0, through VOP2 min/max and VOP3 min3/max3/med3, with the interpreter compared to the GPU byte for byte. Differing words, out of 160:

Adapter main this PR Left with this PR
RTX 4060 72 20 14 med3; 6 min/max with two NaN sources (the GPU gives 0x7FFFFFFF, the interpreter gives S1)
Intel UHD 70 14 14 med3 only
WARP 83 35 med3; WARP's min/max treat ±0 as equal and return S1 (min(-0, +0) = +0, max(+0, -0) = -0), also through min3/max3; with two NaN sources it returns S0

So the PR brings the interpreter much closer to all three adapters. But the ±0 rule now differs from WARP, the CI adapter, for min/max/min3/max3, and med3 differs on every adapter (the HLSL follow-up you mention). The HLSL side would need zeros ordered in integer code to match byte for byte on WARP. That shouldn't block an interpreter-only fix, but the HLSL follow-up should come with a GPU parity test for these ops.

One case that follows the pseudo-code literally: med3(-0, +0, -1) (also (-0, +0, -0) and (-0, -0, +0)) gives +0 here, because the pseudo-code's V_MAX3_F32(...) == S0.f treats -0 and +0 as equal. The HLSL formula gives -0 on all three adapters. Real RDNA 2 output for this case is unmeasured as far as I know.

AI use: Claude Code (Claude Opus 5.5) ran the tests and wrote this.

**@⁠KainanP** on GitHub · 2026-11-25 23:42 UTC · [original](https://github.com/LooperSalty/AnyXboxX/pull/269#issuecomment-7177942347) **Setup:** Windows 11 26200, dxc 1.8.2502.11 (Windows SDK 10.0.26100.0), Python 3.12.10, C++ runtime built from `main` `3e258c8`. Adapters: RTX 4060 Laptop (driver 32.0.16.1047, i.e. 610.47; default adapter), Intel UHD 8086:a78b (31.0.101.5445, `ANYXBOXX_GPU=Intel`, 16 lanes, no FP64), WARP 10.0.26100.9818 (`ANYXBOXX_GPU=warp`). This PR was merged onto `main` `3e258c8` (clean merge). | | `main` | this PR | |---|---|---| | RTX 4060, `python -m pytest` | 1404 passed, 17 skipped | 1407 passed, 17 skipped | | WARP, `tests/test_rdna2*.py` | 996 passed, 2 skipped | 999 passed, 2 skipped | | Intel UHD, `tests/test_rdna2*.py` | 994 passed, 4 skipped | 997 passed, 4 skipped | | `tests/test_rdna2_exec_minmax.py` on `main` code | 3 failed | 3 passed | **HLSL side (rule 4).** No GPU parity test runs `v_min/max_f32` or `v_min3/max3/med3_f32`, so the runs above don't check the HLSL side. I ran a probe program on 32 lanes: your tests' sources plus more mixes of NaN and ±0, through VOP2 min/max and VOP3 min3/max3/med3, with the interpreter compared to the GPU byte for byte. Differing words, out of 160: | Adapter | `main` | this PR | Left with this PR | |---|---|---|---| | RTX 4060 | 72 | 20 | 14 `med3`; 6 min/max with two NaN sources (the GPU gives `0x7FFFFFFF`, the interpreter gives S1) | | Intel UHD | 70 | 14 | 14 `med3` only | | WARP | 83 | 35 | `med3`; WARP's `min`/`max` treat ±0 as equal and return S1 (`min(-0, +0) = +0`, `max(+0, -0) = -0`), also through min3/max3; with two NaN sources it returns S0 | So the PR brings the interpreter much closer to all three adapters. But the ±0 rule now differs from WARP, the CI adapter, for min/max/min3/max3, and `med3` differs on every adapter (the HLSL follow-up you mention). The HLSL side would need zeros ordered in integer code to match byte for byte on WARP. That shouldn't block an interpreter-only fix, but the HLSL follow-up should come with a GPU parity test for these ops. One case that follows the pseudo-code literally: `med3(-0, +0, -1)` (also `(-0, +0, -0)` and `(-0, -0, +0)`) gives +0 here, because the pseudo-code's `V_MAX3_F32(...) == S0.f` treats -0 and +0 as equal. The HLSL formula gives -0 on all three adapters. Real RDNA 2 output for this case is unmeasured as far as I know. _AI use: Claude Code (Claude Opus 5.5) ran the tests and wrote this._
Author
Owner

@⁠LooperSalty on GitHub · 2026-11-26 00:06 UTC · original

Thanks — the interpreter side looks right. The HLSL recompiler still uses plain min/max for these: rdna2_hlsl_ops.py:180-181 (v_min/max_f32), :255 (v_min3), :257 (v_med3). Could you give the HLSL side the same NaN / -0 behaviour (with a GPU/WARP parity case), or state in the PR that the HLSL side is out of scope and keep a test that documents the gap?

**@⁠LooperSalty** on GitHub · 2026-11-26 00:06 UTC · [original](https://github.com/LooperSalty/AnyXboxX/pull/269#issuecomment-7178407931) Thanks — the interpreter side looks right. The HLSL recompiler still uses plain min/max for these: `rdna2_hlsl_ops.py:180-181` (v_min/max_f32), `:255` (v_min3), `:257` (v_med3). Could you give the HLSL side the same NaN / -0 behaviour (with a GPU/WARP parity case), or state in the PR that the HLSL side is out of scope and keep a test that documents the gap?
Author
Owner

@⁠LooperSalty on GitHub · 2026-11-26 14:01 UTC · original

This PR now conflicts with main in PrototypeScarlett/emu/rdna2_exec_vop3.py. Please rebase on main, make sure the NaN and -0 rules still agree with the HLSL side and the LLVM oracle, run the RDNA 2 tests and force-push your branch.

🤖 Generated with Claude Code

**@⁠LooperSalty** on GitHub · 2026-11-26 14:01 UTC · [original](https://github.com/LooperSalty/AnyXboxX/pull/269#issuecomment-7194173422) This PR now conflicts with main in `PrototypeScarlett/emu/rdna2_exec_vop3.py`. Please rebase on main, make sure the NaN and -0 rules still agree with the HLSL side and the LLVM oracle, run the RDNA 2 tests and force-push your branch. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
This pull request has changes conflicting with the target branch.
  • PrototypeScarlett/emu/rdna2_exec_vop3.py
View command line instructions

Manual merge helper

Use this merge commit message when completing the merge manually.

Checkout

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

Merge

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