fix: v_min/max_f32 and v_min3/max3/med3_f32 give the other operand for a NaN and order -0 below +0 #269
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/269"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
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) andv_min3_f32,v_max3_f32,v_med3_f32used Python'smin/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 onmain(e921268):mainv_min_f327FC00000(NaN)3F800000(1.0)v_max_f327FC000003F800000v_min_f320000000080000000(-0)v_max_f328000000000000000(+0)v_min3_f327FC0000000000000v_med3_f327FC0000000000000v_med3_f323F80000000000000The pseudo-code in AMD's "RDNA 2" ISA (November 2020):
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(S0.f, S1.f, S2.f)when a source is NaN. So themed3(x, 0, 1.0)clamp gives 0 for a NaN.v_min/max_f16(half_min/half_max) andv_min/max_f64(_pick) already follow these rules, and the HLSLmin()/max()(DXIL FMin/FMax) already return the non-NaN operand, so only the f32 interpreter disagreed. The rules now live in the newemu/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))) givesbformed3(NaN, b, c)where the ISA givesmin(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.--select W605,F,E9.The broader reference run stops with 1,470 passed, 482 skipped, and two macOS failures:
test_unfused_multiply_add_nans_match_the_python_modeltest_interpolation_programs_match_the_python_modelBoth 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
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. Onmain, 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.tests/test_rdna2_exec_minmax.py: 14 VOP2+VOP3 pairs (NaN in either source, both NaN, ±0 in both orders, ±infinity),-v1againstv1= +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.Checklist
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
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
main3e258c8. 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 ontomain3e258c8(clean merge).mainpython -m pytesttests/test_rdna2*.pytests/test_rdna2*.pytests/test_rdna2_exec_minmax.pyonmaincodeHLSL side (rule 4). No GPU parity test runs
v_min/max_f32orv_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:mainmed3; 6 min/max with two NaN sources (the GPU gives0x7FFFFFFF, the interpreter gives S1)med3onlymed3; WARP'smin/maxtreat ±0 as equal and return S1 (min(-0, +0) = +0,max(+0, -0) = -0), also through min3/max3; with two NaN sources it returns S0So 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
med3differs 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'sV_MAX3_F32(...) == S0.ftreats -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.
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?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
Imported from LooperSalty/AnyXboxX. Originally posted by lcg007xd-c on 2026-12-01T13:32:04Z.
@greptileai Please review the latest changes.
Imported from LooperSalty/AnyXboxX. Originally posted by lcg007xd-c on 2026-12-01T14:24:06Z.
@greptileai Please review the current changes.
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.
View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.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.