shader: match RDNA2 V_MED3_F32 on signed-zero ties and three NaN sources #2528

Manually merged
github merged 2 commits from gh-pr/2528/head into gh-pr/2528/base 2026-11-26 10:22:06 +00:00
Owner

@⁠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 UTC

V_MED3_F32 returns +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 of VectorMed3F32NanUsesMin3Path expect 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:

(a, b, c) main (and its test) RDNA2
(-0, -0, +0) +0 -0
(-0, +0, -0) +0 -0
(-1, -0, +0) +0 -0
(-0, -1, +0) +0 -0
(-0, +0, -1) +0 -0
(0x7fc01234, 0x7fa00001, 0xffc05678) 0xffc05678 0x7fc01234

The other 17 rows give the test's values. v_med3_f16 follows the same rules on that GPU: -0 in the five rows that mirror these zero ties, and the first source for three NaNs.

Cause

  • EmitFPMedTri32 compares MAX3 with a and b using OpFOrdEqual, so +0 and -0 count as equal and the tie picks the pair whose maximum is +0. RDNA2 orders -0 below +0 here, as EmitMinMaxF32 already does for V_MIN_F32 and V_MAX_F32.
  • The NaN path is min(c, min(b, a)), which returns the last source when all three are NaN.

Change

  • The median is 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 becomes min(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 of VectorMed3F32NanUsesMin3Path goes from 14,463 to 13,727 words (wave32, RX 9070 XT).
  • The NaN path is 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.
  • V_MED3_F16 uses the same function, so it changes the same way.

Tests

  • VectorMed3F32NanUsesMin3Path: the six rows above now expect the RDNA2 results. VectorMinMaxMed3F16Ops: adds med3_f16(-0, +0, -0) = -0.
  • With main's recompiler both tests fail on these rows (both waves of the F32 test; the new F16 row gives +0). With the change both pass on an RX 9070 XT, with native and with 32-wide subgroups (Windows 11).
  • The whole compute case list (935 entries) on the same GPU, native and 32-wide subgroups: apart from the three entries of these two tests, every entry passes or fails as on main. --fp32-compare-only and --relaxed-float-only pass; --float-min-max-only fails VectorMinMaxF32NanAndSignedZeroEdges the same way on main. shader_cfg_tests gives the same output as on main.
  • Ghost of Yōtei (PPSA26344): the 235 shader modules of a capture (main's translation reproduces all 235 byte for byte), translated offline by main and by this branch with the device profile the capture recorded (native float min/max). 63 of them (43 compute, 20 pixel shaders) contain MED3, and each MED3 is 6 SPIR-V instructions shorter (613 MED3, 3,678 instructions fewer); the other 172 modules contain none and are unchanged. All 235 modules of this branch pass the SPIR-V validator. I don't know whether the game reaches the changed inputs.
  • The test harness needs local changes to run on that GPU at all (#2038's robustness alignment change, and a runner that starts one case list entry per process); the code under test is this branch.
  • Fork CI built this head on Windows, Linux and macOS and its ctest set passed there (that set does not include shader_recompiler_compute_tests): https://github.com/Nawid3333/KytyPS5/actions/runs/45124180416

Not 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).

**@⁠Nawid3333** on GitHub · 2026-11-23 07:47 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/2528) `Nawid3333:fix/med3-signed-zero-tie` → `main` · +20 −23 · 2 files · 2 commits · merged 2026-11-23 14:12 UTC `V_MED3_F32` returns +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 of `VectorMed3F32NanUsesMin3Path` expect 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: | (a, b, c) | main (and its test) | RDNA2 | |---|---|---| | (-0, -0, +0) | +0 | -0 | | (-0, +0, -0) | +0 | -0 | | (-1, -0, +0) | +0 | -0 | | (-0, -1, +0) | +0 | -0 | | (-0, +0, -1) | +0 | -0 | | (0x7fc01234, 0x7fa00001, 0xffc05678) | 0xffc05678 | 0x7fc01234 | The other 17 rows give the test's values. `v_med3_f16` follows the same rules on that GPU: -0 in the five rows that mirror these zero ties, and the first source for three NaNs. **Cause** - `EmitFPMedTri32` compares MAX3 with `a` and `b` using `OpFOrdEqual`, so +0 and -0 count as equal and the tie picks the pair whose maximum is +0. RDNA2 orders -0 below +0 here, as `EmitMinMaxF32` already does for V_MIN_F32 and V_MAX_F32. - The NaN path is `min(c, min(b, a))`, which returns the last source when all three are NaN. **Change** - The median is `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 becomes `min(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 of `VectorMed3F32NanUsesMin3Path` goes from 14,463 to 13,727 words (wave32, RX 9070 XT). - The NaN path is `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. - V_MED3_F16 uses the same function, so it changes the same way. **Tests** - `VectorMed3F32NanUsesMin3Path`: the six rows above now expect the RDNA2 results. `VectorMinMaxMed3F16Ops`: adds med3_f16(-0, +0, -0) = -0. - With main's recompiler both tests fail on these rows (both waves of the F32 test; the new F16 row gives +0). With the change both pass on an RX 9070 XT, with native and with 32-wide subgroups (Windows 11). - The whole compute case list (935 entries) on the same GPU, native and 32-wide subgroups: apart from the three entries of these two tests, every entry passes or fails as on main. `--fp32-compare-only` and `--relaxed-float-only` pass; `--float-min-max-only` fails `VectorMinMaxF32NanAndSignedZeroEdges` the same way on main. `shader_cfg_tests` gives the same output as on main. - Ghost of Yōtei (PPSA26344): the 235 shader modules of a capture (main's translation reproduces all 235 byte for byte), translated offline by main and by this branch with the device profile the capture recorded (native float min/max). 63 of them (43 compute, 20 pixel shaders) contain MED3, and each MED3 is 6 SPIR-V instructions shorter (613 MED3, 3,678 instructions fewer); the other 172 modules contain none and are unchanged. All 235 modules of this branch pass the SPIR-V validator. I don't know whether the game reaches the changed inputs. - The test harness needs local changes to run on that GPU at all (#2038's robustness alignment change, and a runner that starts one case list entry per process); the code under test is this branch. - Fork CI built this head on Windows, Linux and macOS and its ctest set passed there (that set does not include `shader_recompiler_compute_tests`): https://github.com/Nawid3333/KytyPS5/actions/runs/45124180416 Not 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).
RDNA2 orders -0 below +0 in MED3, so a median of zeros that includes -0 twice or a
-0 below every other source is -0; the compare against MAX3 counted +0 and -0 as equal
and returned +0. With all three sources NaN, MED3 returns S0 like MIN3 and MAX3.

The median is now max(min(a, b), min(max(a, b), c)) on the existing min/max helpers, and
the NaN path is min(min(a, b), c). Six rows of VectorMed3F32NanUsesMin3Path now expect
the results of v_med3_f32 on an RDNA2 GPU, and VectorMinMaxMed3F16Ops checks the
V_MED3_F16 zero tie that shares this lowering.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 10:22:05 +00:00
github manually merged commit c00880fff9 into gh-pr/2528/base 2026-11-26 10:22:06 +00:00
Author
Owner

@⁠coderabbitai[bot] on GitHub · 2026-11-23 07:52 UTC · original · edited 2026-11-23 14:07 UTC

Explain Change →

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: nmzik

Change: Bug fix

Merge Risk: Minimal · up to c0088

This 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: Passed 4

Check name Status Explanation
Title check Passed 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 …
Description check Passed 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 …
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
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

Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between 8c9ab4d5a1 and c00880fff9.

Files selected for processing: 2

  • src/graphics/shader/recompiler/backend/spirv/spirvEmitterAluHelpers.cpp
  • tests/ShaderRecompilerComputeTests.cpp
Files skipped from review as they are similar to previous changes: 1

  • src/graphics/shader/recompiler/backend/spirv/spirvEmitterAluHelpers.cpp

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.

**@⁠coderabbitai[bot]** on GitHub · 2026-11-23 07:52 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/2528#issuecomment-7094795925) · edited 2026-11-23 14:07 UTC <!-- coderabbit-ai-generated:v1 --> <!-- This is an auto-generated comment: summarize by coderabbit.ai --> <!-- review_stack_entry_start --> <a href="https://app.coderabbit.ai/change-stack/KytyPS5/KytyPS5/pull/2528?scope=ghh_OusAJGA4HG8ljoOV0wJvVx8NmnZIjc2DdjmxOu0xZWA&amp;cs_source=review_comment"><picture><source media="(prefers-color-scheme: dark)" srcset="https://storage.googleapis.com/coderabbit_public_assets/review-stack-buttons-v1/explain-change-dark.svg"><source media="(prefers-color-scheme: light)" srcset="https://storage.googleapis.com/coderabbit_public_assets/review-stack-buttons-v1/explain-change.svg"><img src="https://storage.googleapis.com/coderabbit_public_assets/review-stack-buttons-v1/explain-change.svg" alt="Explain Change →" height="32"></picture></a> <!-- review_stack_entry_end --> <!-- recent_review_status_start --> No actionable comments were generated in the recent review. <!-- recent_review_status_end --> <!-- walkthrough_start --> <details> <summary><strong>Walkthrough</strong></summary> <dl> <dd> <br> 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:** `nmzik` </dd> </dl> </details> <!-- walkthrough_end --> <!-- change_assessment_start --> <!-- change_assessment_commit:"c00880fff932d6a02b61fc063d45a492028be177" --> <!-- change_assessment_priority:{"version":1,"urgency":"p2","manual":false} --> **Change:** Bug fix <!-- change_assessment_end --> <details> <summary><strong>Merge Risk:</strong> <strong><picture><img src="https://storage.googleapis.com/coderabbit_public_assets/review-comment-icons-v1/risk-unassessed-16.svg" width="16" height="16" align="absmiddle" alt=""></picture> Minimal</strong> · up to <code>c0088</code></summary> <dl> <dd> <br> <!-- final_review_risk_start --> <!-- final_review_risk_label **Merge Risk:** **⚪ Minimal** · up to `c0088` --> <!-- final_review_risk_coverage:{"sourceCommitId":"c00880fff932d6a02b61fc063d45a492028be177","coveredCommitId":"c00880fff932d6a02b61fc063d45a492028be177","kind":"reviewed"} --> This 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. <!-- final_review_risk_end --> </dd> </dl> </details> <!-- pre_merge_checks_walkthrough_start --> <details> <summary><strong>Pre-merge checks: <sup><picture><img src="https://storage.googleapis.com/coderabbit_public_assets/pre-merge-checks-counter-passed-14-v2.svg" width="14" height="14" align="middle" alt="Passed" title="Passed"></picture></sup> 4</strong></summary> <dl> <dd> <br> | Check name | Status | Explanation | | :------------------------ | :--------------------------------------------------------------------------------------------: | :-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | Title check | ![Passed](https://storage.googleapis.com/coderabbit_public_assets/pre-merge-checks-passed.svg) | 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 … | | Description check | ![Passed](https://storage.googleapis.com/coderabbit_public_assets/pre-merge-checks-passed.svg) | 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 … | | Linked Issues check | ![Passed](https://storage.googleapis.com/coderabbit_public_assets/pre-merge-checks-passed.svg) | Check skipped because no linked issues were found for this pull request. | | Out of Scope Changes check | ![Passed](https://storage.googleapis.com/coderabbit_public_assets/pre-merge-checks-passed.svg) | Check skipped because no linked issues were found for this pull request. | <details> <summary><strong>Full details: Title check</strong></summary> <dl> <dd> <br> **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. </dd> </dl> </details> <details> <summary><strong>Full details: Description check</strong></summary> <dl> <dd> <br> **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. </dd> </dl> </details> </dd> </dl> </details> <!-- pre_merge_checks_walkthrough_end --> <!-- tips_start --> Thanks for using [CodeRabbit](https://coderabbit.ai?utm_source=oss&utm_medium=github&utm_campaign=KytyPS5/KytyPS5&utm_content=2528)! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. <details> <summary><strong>Share</strong></summary> <dl> <dd> <br> - [X](https://twitter.com/intent/tweet?text=I%20just%20used%20%40coderabbitai%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20the%20proprietary%20code.%20Check%20it%20out%3A&url=https%3A//coderabbit.ai) - [Mastodon](https://mastodon.social/share?text=I%20just%20used%20%40coderabbitai%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20the%20proprietary%20code.%20Check%20it%20out%3A%20https%3A%2F%2Fcoderabbit.ai) - [Reddit](https://www.reddit.com/submit?title=Great%20tool%20for%20code%20review%20-%20CodeRabbit&text=I%20just%20used%20CodeRabbit%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20proprietary%20code.%20Check%20it%20out%3A%20https%3A//coderabbit.ai) - [LinkedIn](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fcoderabbit.ai&mini=true&title=Great%20tool%20for%20code%20review%20-%20CodeRabbit&summary=I%20just%20used%20CodeRabbit%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20proprietary%20code) </dd> </dl> </details> <!-- tips_end --> <!-- recent_review_start --> <h6> <details> <summary><strong><picture><img src="https://storage.googleapis.com/coderabbit_public_assets/review-comment-icons-v1/info.svg" width="12" height="12" align="absmiddle" alt=""></picture> Recent review info</strong></summary> <dl> <dd> <br> <!-- review_info_content --> <details> <summary><strong><picture><source media="(prefers-color-scheme: dark)" srcset="https://storage.googleapis.com/coderabbit_public_assets/review-comment-icons-v1/cog-dark.svg"><img src="https://storage.googleapis.com/coderabbit_public_assets/review-comment-icons-v1/cog.svg" width="16" height="16" align="absmiddle" alt=""></picture> Run configuration</strong></summary> <dl> <dd> <br> - **Source mode**: Auto - **Repository**: [KytyPS5/KytyPS5](<https://github.com/KytyPS5/KytyPS5>) - **Branch**: [main](<https://github.com/KytyPS5/KytyPS5/tree/main>) - **File**: [.coderabbit.yaml](<https://github.com/KytyPS5/KytyPS5/blob/a0b083c85293d55a48b4f8774285415c29d78c8b/.coderabbit.yaml>) - **Commit**: [a0b083c](<https://github.com/KytyPS5/KytyPS5/commit/a0b083c85293d55a48b4f8774285415c29d78c8b>) - **Layers**: Repository YAML - **Review profile**: CHILL - **Plan**: Advanced - **Run ID**: `16011c9c-e29c-4577-a7d8-283b1ce14e4b` </dd> </dl> </details> <details> <summary><strong><picture><source media="(prefers-color-scheme: dark)" srcset="https://storage.googleapis.com/coderabbit_public_assets/review-comment-icons-v1/git-commit-horizontal-dark.svg"><img src="https://storage.googleapis.com/coderabbit_public_assets/review-comment-icons-v1/git-commit-horizontal.svg" width="16" height="16" align="absmiddle" alt=""></picture> Commits</strong></summary> <dl> <dd> <br> Reviewing files that changed from the base of the PR and between 8c9ab4d5a1d2a449ca7136bcab6004c5e0706113 and c00880fff932d6a02b61fc063d45a492028be177. </dd> </dl> </details> <details> <summary><strong><picture><source media="(prefers-color-scheme: dark)" srcset="https://storage.googleapis.com/coderabbit_public_assets/review-comment-icons-v1/file-plus-corner-dark.svg"><img src="https://storage.googleapis.com/coderabbit_public_assets/review-comment-icons-v1/file-plus-corner.svg" width="16" height="16" align="absmiddle" alt=""></picture> Files selected for processing: 2</strong></summary> <dl> <dd> <br> * `src/graphics/shader/recompiler/backend/spirv/spirvEmitterAluHelpers.cpp` * `tests/ShaderRecompilerComputeTests.cpp` </dd> </dl> </details> <details> <summary><strong><picture><source media="(prefers-color-scheme: dark)" srcset="https://storage.googleapis.com/coderabbit_public_assets/review-comment-icons-v1/file-similar-dark.svg"><img src="https://storage.googleapis.com/coderabbit_public_assets/review-comment-icons-v1/file-similar.svg" width="16" height="16" align="absmiddle" alt=""></picture> Files skipped from review as they are similar to previous changes: 1</strong></summary> <dl> <dd> <br> * src/graphics/shader/recompiler/backend/spirv/spirvEmitterAluHelpers.cpp </dd> </dl> </details> **Included review availability:** This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review. </dd> </dl> </details> </h6> <!-- recent_review_end -->
This discussion has been locked. Commenting is limited to contributors.
No description provided.