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
lcg007xd-c wants to merge 2 commits from upstream-pr/269 into main
lcg007xd-c commented 2026-11-25 22:52:26 +00:00 (Migrated from github.com)

Imported from LooperSalty/AnyXboxX. Originally posted by lcg007xd-c on 2026-11-25T22:52:26Z.


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

Tested on macOS arm64 with Python 3.12 and the pinned Rust toolchain.

  • cargo test --locked -p anyxboxx-rdna2-ref: 151 passed.
  • Python suite without native binaries: 1,444 passed and 543 skipped.
  • The new min/max parity test and existing random-program parity test: 2 passed with the Rust reference built.
  • Both parity tests failed before the Rust reference update.
  • Ruff passes with --select W605,F,E9.
  • Rust formatting and the whitespace check pass.

The broader reference run stops with 1,470 passed, 482 skipped, and two macOS failures:

  • test_unfused_multiply_add_nans_match_the_python_model
  • test_interpolation_programs_match_the_python_model

Both failures reproduce with unchanged upstream Python and Rust interpreter sources.
They differ on NaN sign bits on macOS arm64.
The assertions and affected implementations remain unchanged.
Windows CI passes: lint, VIctory policy, pytest, and cargo test.

The Rust reference now uses the same f32 min/max and median rules as the Python interpreter.
A regression covers the existing golden cases in wave32 and wave64.
The HLSL path remains unchanged and needs separate GPU or WARP verification.

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: the original change used Claude Code. Conflict resolution and the Rust reference update used Codex.

🤖 Generated with Claude Code

<!-- greyfinch-sync:LooperSalty/AnyXboxX:issue:5500552461 --> Imported from [LooperSalty/AnyXboxX](https://github.com/LooperSalty/AnyXboxX/pull/269). Originally posted by [lcg007xd-c](https://github.com/lcg007xd-c) on 2026-11-25T22:52:26Z. --- ## 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 Tested on macOS arm64 with Python 3.12 and the pinned Rust toolchain. - `cargo test --locked -p anyxboxx-rdna2-ref`: 151 passed. - Python suite without native binaries: 1,444 passed and 543 skipped. - The new min/max parity test and existing random-program parity test: 2 passed with the Rust reference built. - Both parity tests failed before the Rust reference update. - Ruff passes with `--select W605,F,E9`. - Rust formatting and the whitespace check pass. The broader reference run stops with 1,470 passed, 482 skipped, and two macOS failures: - `test_unfused_multiply_add_nans_match_the_python_model` - `test_interpolation_programs_match_the_python_model` Both failures reproduce with unchanged upstream Python and Rust interpreter sources. They differ on NaN sign bits on macOS arm64. The assertions and affected implementations remain unchanged. Windows CI passes: lint, VIctory policy, pytest, and cargo test. The Rust reference now uses the same f32 min/max and median rules as the Python interpreter. A regression covers the existing golden cases in wave32 and wave64. The HLSL path remains unchanged and needs separate GPU or WARP verification. ## 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: the original change used Claude Code. Conflict resolution and the Rust reference update used Codex. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
KainanP commented 2026-11-25 23:42:41 +00:00 (Migrated from github.com)

Imported from LooperSalty/AnyXboxX. Originally posted by KainanP on 2026-11-25T23:42:41Z.


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.

<!-- greyfinch-sync:LooperSalty/AnyXboxX:comment:7177942347 --> Imported from [LooperSalty/AnyXboxX](https://github.com/LooperSalty/AnyXboxX/pull/269#issuecomment-7177942347). Originally posted by [KainanP](https://github.com/KainanP) on 2026-11-25T23:42:41Z. --- **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._
LooperSalty commented 2026-11-26 00:06:47 +00:00 (Migrated from github.com)

Imported from LooperSalty/AnyXboxX. Originally posted by LooperSalty on 2026-11-26T00:06:47Z.


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?

<!-- greyfinch-sync:LooperSalty/AnyXboxX:comment:7178407931 --> Imported from [LooperSalty/AnyXboxX](https://github.com/LooperSalty/AnyXboxX/pull/269#issuecomment-7178407931). Originally posted by [LooperSalty](https://github.com/LooperSalty) on 2026-11-26T00:06:47Z. --- 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 commented 2026-11-26 14:01:44 +00:00 (Migrated from github.com)

Imported from LooperSalty/AnyXboxX. Originally posted by LooperSalty on 2026-11-26T14:01:44Z.


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

<!-- greyfinch-sync:LooperSalty/AnyXboxX:comment:7194173422 --> Imported from [LooperSalty/AnyXboxX](https://github.com/LooperSalty/AnyXboxX/pull/269#issuecomment-7194173422). Originally posted by [LooperSalty](https://github.com/LooperSalty) on 2026-11-26T14:01:44Z. --- 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)
lcg007xd-c commented 2026-12-01 13:32:04 +00:00 (Migrated from github.com)

Imported from LooperSalty/AnyXboxX. Originally posted by lcg007xd-c on 2026-12-01T13:32:04Z.


@greptileai Please review the latest changes.

<!-- greyfinch-sync:LooperSalty/AnyXboxX:comment:7312776371 --> Imported from [LooperSalty/AnyXboxX](https://github.com/LooperSalty/AnyXboxX/pull/269#issuecomment-7312776371). Originally posted by [lcg007xd-c](https://github.com/lcg007xd-c) on 2026-12-01T13:32:04Z. --- @greptileai Please review the latest changes. <!-- nexus-review-request:3f8660ba4930 -->
lcg007xd-c commented 2026-12-01 14:24:06 +00:00 (Migrated from github.com)

Imported from LooperSalty/AnyXboxX. Originally posted by lcg007xd-c on 2026-12-01T14:24:06Z.


@greptileai Please review the current changes.

<!-- greyfinch-sync:LooperSalty/AnyXboxX:comment:7314190188 --> Imported from [LooperSalty/AnyXboxX](https://github.com/LooperSalty/AnyXboxX/pull/269#issuecomment-7314190188). Originally posted by [lcg007xd-c](https://github.com/lcg007xd-c) on 2026-12-01T14:24:06Z. --- @greptileai Please review the current changes. <!-- nexus-review-pass:20261201-conflicts -->
lcg007xd-c commented 2026-12-01 14:36:33 +00:00 (Migrated from github.com)

Imported from LooperSalty/AnyXboxX. Originally posted by lcg007xd-c on 2026-12-01T14:36:33Z.


The Rust reference now matches the corrected Python min/max semantics.
The new parity case and existing random-program test both pass; they failed before this update.
All 151 Rust reference unit tests also pass.

The broader macOS run has existing multiply-add and interpolation NaN-sign failures.
Both reproduce with upstream Python and Rust code.
The PR body records it; no test assertion was loosened.

@LooperSalty @KainanP Please review the updated fix.
@greptileai Please review the current commit.

<!-- greyfinch-sync:LooperSalty/AnyXboxX:comment:7314535924 --> Imported from [LooperSalty/AnyXboxX](https://github.com/LooperSalty/AnyXboxX/pull/269#issuecomment-7314535924). Originally posted by [lcg007xd-c](https://github.com/lcg007xd-c) on 2026-12-01T14:36:33Z. --- The Rust reference now matches the corrected Python min/max semantics. The new parity case and existing random-program test both pass; they failed before this update. All 151 Rust reference unit tests also pass. The broader macOS run has existing multiply-add and interpolation NaN-sign failures. Both reproduce with upstream Python and Rust code. The PR body records it; no test assertion was loosened. @LooperSalty @KainanP Please review the updated fix. @greptileai Please review the current commit.
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/269:upstream-pr/269'
git switch 'upstream-pr/269'

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