fix: v_min/max_f32 and v_min3/max3/med3_f32 give the other operand for a NaN and order -0 below +0 #269
No reviewers
Labels
No labels
accessibility
bloqué : binaire console
bug
chargeur
console-dll
cpu
documentation
duplicate
enhancement
good first issue
gpu
help wanted
invalid
performance
question
runtime-gdk
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/LooperSalty--AnyXboxX!269
Loading…
Reference in a new issue
No description provided.
Delete branch "github-bot/gh-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?
@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 · openChange
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
Linux, Python 3.14, no C++ runtime, no GPU (the runtime and GPU tests skip):
Not run: the Windows CI job and the GPU/WARP parity tests. No HLSL code changes.
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: written with Claude Code (Claude Opus 5.5).
🤖 Generated with Claude Code
@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
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.
@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 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
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.Merge
Merge the changes and update on Forgejo.