shader: match RDNA2 V_MED3_F32 on signed-zero ties and three NaN sources #2528
No reviewers
Labels
No labels
bug
documentation
duplicate
enhancement
GPU
GUI
help wanted
question
Shaders
Video
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/KytyPS5--KytyPS5!2528
Loading…
Reference in a new issue
No description provided.
Delete branch "gh-pr/2528/head"
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?
@Nawid3333 on GitHub · 2026-11-23 07:47 UTC · original
Nawid3333:fix/med3-signed-zero-tie→main· +20 −23 · 2 files · 2 commits · merged 2026-11-23 14:12 UTCV_MED3_F32returns +0 where RDNA2 returns -0 when the median is a zero tie that involves -0, and the third source where RDNA2 returns the first when all three sources are NaN. On main, six rows ofVectorMed3F32NanUsesMin3Pathexpect these current results.I ran the exact instruction,
v_med3_f32 v10, v4, v5, v6, on an RDNA2 GPU (Ryzen 7600X iGPU, gfx1036) through HIP with the 23 rows of that test, one row per lane, FLOAT_MODE 0xf0 and IEEE mode off:The other 17 rows give the test's values.
v_med3_f16follows the same rules on that GPU: -0 in the five rows that mirror these zero ties, and the first source for three NaNs.Cause
EmitFPMedTri32compares MAX3 withaandbusingOpFOrdEqual, so +0 and -0 count as equal and the tie picks the pair whose maximum is +0. RDNA2 orders -0 below +0 here, asEmitMinMaxF32already does for V_MIN_F32 and V_MAX_F32.min(c, min(b, a)), which returns the last source when all three are NaN.Change
max(min(a, b), min(max(a, b), c))on the same exact min/max helpers, which is the median with -0 ordered below +0. MAX3 becomesmin(max(a, b), c), and the two compares against MAX3 with their selects are gone, so V_MED3_F32 emits 6 fewer SPIR-V instructions (the relaxed-float path is unchanged): the module ofVectorMed3F32NanUsesMin3Pathgoes from 14,463 to 13,727 words (wave32, RX 9070 XT).min(min(a, b), c), the operand order of V_MIN3_F32. With the helper's two-NaN rule (first source) that gives the first source for three NaNs.Tests
VectorMed3F32NanUsesMin3Path: the six rows above now expect the RDNA2 results.VectorMinMaxMed3F16Ops: adds med3_f16(-0, +0, -0) = -0.--fp32-compare-onlyand--relaxed-float-onlypass;--float-min-max-onlyfailsVectorMinMaxF32NanAndSignedZeroEdgesthe same way on main.shader_cfg_testsgives the same output as on main.shader_recompiler_compute_tests): https://github.com/Nawid3333/KytyPS5/actions/runs/45124180416Not covered: the PS5's own GPU (gfx1013) was not measured, only the RDNA2 iGPU. With IEEE mode on, RDNA2 also quiets signaling NaNs here; the recompiler has no IEEE mode, so that is left as it is.
#2096 (mine) rewrites the same function to emit fewer instructions and keeps main's results there; if this one goes in first, I'll rebase #2096 onto it.
If you'd like this done differently, tell me and I'll change it.
Written with Claude Code (AI): it measured the instruction on the GPU, wrote the change and the tests, and ran them on my PC (Windows 11, RX 9070 XT and Ryzen 7600X iGPU).
@coderabbitai[bot] on GitHub · 2026-11-23 07:52 UTC · original · edited 2026-11-23 14:07 UTC
No actionable comments were generated in the recent review.
Walkthrough
The non-relaxed F32 median selection now uses pairwise minimum and maximum operations instead of the prior selection and tie-handling logic. The existing NaN check remains. Tests add an F16 median case and update F32 expected results for signed-zero cases. The test comment also reports lower SPIR-V word counts and maximum limits.
Suggested reviewers:
nmzikChange: Bug fix
Merge Risk:
Minimal · up to
c0088This change adjusts the V_MED3 median computation to match reported RDNA2 behavior for signed-zero ties and all-NaN inputs, and updates the tests to match. No concrete defect was found, so it looks ready to merge.
Pre-merge checks:
4
Full details: Title check
Explanation
The title names the main change: V_MED3_F32 is changed to match RDNA2 results on signed-zero ties and on three-NaN inputs. The raw summary and objectives both describe this median and NaN-path change in EmitFPMedTri32, so the title is directly related to the changeset.
Full details: Description check
Explanation
The description explains the change in detail. It covers the RDNA2 signed-zero and three-NaN behavior, the new median and NaN-path formulas in EmitFPMedTri32, the updated VectorMed3F32NanUsesMin3Path and VectorMinMaxMed3F16Ops tests, and the measured SPIR-V word reductions. This matches the raw summary and objectives.
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
Share
16011c9c-e29c-4577-a7d8-283b1ce14e4bReviewing files that changed from the base of the PR and between
8c9ab4d5a1andc00880fff9.src/graphics/shader/recompiler/backend/spirv/spirvEmitterAluHelpers.cpptests/ShaderRecompilerComputeTests.cppIncluded review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.