shader: emit V_MED3_F32 on integer keys #1567

Closed
github-bot wants to merge 3 commits from gh-pr/1567/head into gh-pr/1567/base
Owner

@⁠Nawid3333 on GitHub · 2026-10-16 19:00 UTC · original
Nawid3333:shader/med3-integer-keys → main · +116 −34 · 7 files · 3 commits · closed without merging

Another one for nmzik's idea in #1534. V_MED3_F32 (both AMD drivers here compile clamp(x, lo, hi) to one v_med3, with constant or variable bounds) is built from six calls of the f32 min/max helper, five NaN classifications and two float compares against MAX3 (defb2f11).

Change (EmitFPMedTri32): map each input to an integer key that sorts like the float, bits ^ ((bits >> 31) & 0x7fffffff) (-0 before +0, denormals by value; the map is its own inverse), take the median with SMin/SMax and map it back. With a NaN input the result is min3 over the non-NaN inputs (NaN keys set to INT32_MAX), or the third input when all three are NaN, as on main. 37 instructions, all integer. In a compute shader whose FLOAT_MODE flushes f32 denormal inputs, the three inputs are first flushed to signed zero, as the med3 instructions of both GPUs here do in a DenormFlushToZero shader (four more instructions per input).

Ties between signed zeros: the keys put -0 below +0, so (-0, -0, +0), (-0, +0, -0), (-1, -0, +0), (-0, -1, +0) and (-0, +0, -1) give -0, where main gives +0. v_med3_f32 on an RDNA2 GPU (Ryzen 7600X iGPU) and v_med3_num_f32 on the RX 9070 XT return -0 for all five, so the PR changes those five expected values in VectorMed3F32NanUsesMin3Path to -0 (measurement in the comment below).

Size (the module of VectorMed3F32NanUsesMin3Path, 23 V_MED3_F32, harness dump on the RX 9070 XT)

main this PR
wave 32: words / instructions 17,593 / 3,849 13,164 / 2,977
wave 64 on a 32-wide host: words / instructions 36,607 / 7,888 27,739 / 6,142

About 38 instructions fewer per emitted V_MED3_F32 (a wave-64 shader on a 32-wide host emits each one twice, once per half). The test's size limit moves from 17,600 / 36,620 to 13,171 / 27,752 words, the same margins over the measured sizes.

Results

  • A GLSL copy of this form against the hardware instruction (mid3(), compiled to v_med3_f32 on the iGPU): every 32-bit value in each of the three positions with the other two from 12 edge values (both zeros, the smallest denormals, the smallest normals, ±1, infinities, a quiet and a signaling NaN), 1.86 × 10^12 triples, in a DenormPreserve shader and in a DenormFlushToZero shader (with the input flush): no difference on any triple without a NaN. With three NaN inputs the hardware returns the first input unchanged and this PR the third, as main does; that is left as it is.
  • On main the median of denormal inputs depends on the host: its float compares run without DenormPreserve, and VectorMed3F32DenormalInputs (float_mode 0xf0) fails on both GPUs. The integer keys don't depend on what the host does with denormals.
  • The two new tests, VectorMed3F32DenormalInputs (float_mode 0xf0) and VectorMed3F32FlushesDenormalInputs (0xc0), expect what v_med3_f32 (iGPU) and v_med3_num_f32 (RX 9070 XT) return for the same inputs. The flush test has the largest denormal and the smallest normal next to ±0.5, and both have +inf next to finite inputs. Each of three one-line bugs in the new code (an infinity counted as a NaN, the smallest normal flushed, the larger denormals not flushed) makes at least one of them fail; without those rows, none of the three makes these tests or VectorMed3F32NanUsesMin3Path fail.
  • At cd4eab9d on both GPUs, also with the harness forced to 32-wide subgroups (a local switch): VectorMed3F32NanUsesMin3Path (wave 32 and 64), VectorMed3F32DenormalInputs and VectorMed3F32FlushesDenormalInputs fail on main and pass with the PR, with no validation-layer messages. Where main's VectorMed3F32NanUsesMin3Path gets past the new size limit (wave 64 on a 64-wide host), it fails exactly the five rows above. The other 809 compute-suite entries and the 30 graphics cases give the same results with and without the PR.
  • Fork CI (build and tests on Windows, Linux and macOS) passes: https://github.com/Nawid3333/KytyPS5/actions/runs/39254554407

If you'd like this done differently, tell me and I'll change it.

Written with Claude Code (AI): it wrote the change and ran the tests and measurements on my PC (driver 26.8.1, Windows 11). I reviewed the diff and this description before opening the PR.

🤖 Generated with Claude Code

**@⁠Nawid3333** on GitHub · 2026-10-16 19:00 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/1567) `Nawid3333:shader/med3-integer-keys` → `main` · +116 −34 · 7 files · 3 commits · closed without merging Another one for nmzik's idea in #1534. V_MED3_F32 (both AMD drivers here compile `clamp(x, lo, hi)` to one `v_med3`, with constant or variable bounds) is built from six calls of the f32 min/max helper, five NaN classifications and two float compares against MAX3 (defb2f11). **Change** (`EmitFPMedTri32`): map each input to an integer key that sorts like the float, `bits ^ ((bits >> 31) & 0x7fffffff)` (-0 before +0, denormals by value; the map is its own inverse), take the median with `SMin`/`SMax` and map it back. With a NaN input the result is min3 over the non-NaN inputs (NaN keys set to `INT32_MAX`), or the third input when all three are NaN, as on main. 37 instructions, all integer. In a compute shader whose FLOAT_MODE flushes f32 denormal inputs, the three inputs are first flushed to signed zero, as the med3 instructions of both GPUs here do in a `DenormFlushToZero` shader (four more instructions per input). **Ties between signed zeros:** the keys put -0 below +0, so (-0, -0, +0), (-0, +0, -0), (-1, -0, +0), (-0, -1, +0) and (-0, +0, -1) give -0, where main gives +0. `v_med3_f32` on an RDNA2 GPU (Ryzen 7600X iGPU) and `v_med3_num_f32` on the RX 9070 XT return -0 for all five, so the PR changes those five expected values in `VectorMed3F32NanUsesMin3Path` to -0 (measurement in the comment below). **Size** (the module of `VectorMed3F32NanUsesMin3Path`, 23 V_MED3_F32, harness dump on the RX 9070 XT) | | main | this PR | |---|---|---| | wave 32: words / instructions | 17,593 / 3,849 | 13,164 / 2,977 | | wave 64 on a 32-wide host: words / instructions | 36,607 / 7,888 | 27,739 / 6,142 | About 38 instructions fewer per emitted V_MED3_F32 (a wave-64 shader on a 32-wide host emits each one twice, once per half). The test's size limit moves from 17,600 / 36,620 to 13,171 / 27,752 words, the same margins over the measured sizes. **Results** - A GLSL copy of this form against the hardware instruction (`mid3()`, compiled to `v_med3_f32` on the iGPU): every 32-bit value in each of the three positions with the other two from 12 edge values (both zeros, the smallest denormals, the smallest normals, ±1, infinities, a quiet and a signaling NaN), 1.86 × 10^12 triples, in a `DenormPreserve` shader and in a `DenormFlushToZero` shader (with the input flush): no difference on any triple without a NaN. With three NaN inputs the hardware returns the first input unchanged and this PR the third, as main does; that is left as it is. - On main the median of denormal inputs depends on the host: its float compares run without `DenormPreserve`, and `VectorMed3F32DenormalInputs` (float_mode 0xf0) fails on both GPUs. The integer keys don't depend on what the host does with denormals. - The two new tests, `VectorMed3F32DenormalInputs` (float_mode 0xf0) and `VectorMed3F32FlushesDenormalInputs` (0xc0), expect what `v_med3_f32` (iGPU) and `v_med3_num_f32` (RX 9070 XT) return for the same inputs. The flush test has the largest denormal and the smallest normal next to ±0.5, and both have +inf next to finite inputs. Each of three one-line bugs in the new code (an infinity counted as a NaN, the smallest normal flushed, the larger denormals not flushed) makes at least one of them fail; without those rows, none of the three makes these tests or `VectorMed3F32NanUsesMin3Path` fail. - At cd4eab9d on both GPUs, also with the harness forced to 32-wide subgroups (a local switch): `VectorMed3F32NanUsesMin3Path` (wave 32 and 64), `VectorMed3F32DenormalInputs` and `VectorMed3F32FlushesDenormalInputs` fail on main and pass with the PR, with no validation-layer messages. Where main's `VectorMed3F32NanUsesMin3Path` gets past the new size limit (wave 64 on a 64-wide host), it fails exactly the five rows above. The other 809 compute-suite entries and the 30 graphics cases give the same results with and without the PR. - Fork CI (build and tests on Windows, Linux and macOS) passes: https://github.com/Nawid3333/KytyPS5/actions/runs/39254554407 If you'd like this done differently, tell me and I'll change it. Written with Claude Code (AI): it wrote the change and ran the tests and measurements on my PC (driver 26.8.1, Windows 11). I reviewed the diff and this description before opening the PR. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
V_MED3_F32 was built from six calls of the f32 min/max helper, five NaN
classifications and two float compares against MAX3. Map each input to an
integer key that sorts like the floats (bits ^ ((bits >> 31) & 0x7fffffff):
-0 before +0, denormals by value), take the median with SMin/SMax and map
it back. With a NaN input the result is min3 over the non-NaN inputs (NaN
keys masked to INT32_MAX), or the third input when all three are NaN, as
before. 37 instructions. The module of VectorMed3F32NanUsesMin3Path drops
from 17,593 to 13,164 words (wave 32) and from 36,607 to 27,739 (wave 64
on a 32-wide host); its size limit follows.

With a tie between signed zeros the median is now -0 where the compare
against MAX3 gave +0, e.g. for (-0, -0, +0). v_med3_f32 on an RDNA2 GPU
(Ryzen 7600X iGPU) and v_med3_num_f32 on an RX 9070 XT return -0 for the
five such rows of VectorMed3F32NanUsesMin3Path, which now expect it.

The keys compare denormals by their bits, so the result no longer depends
on whether the host flushes f32 denormals in float comparisons.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
In a compute shader whose FLOAT_MODE flushes f32 denormal inputs,
V_MED3_F32 flushes its inputs to signed zero before it takes the median
(v_med3_f32 on the Ryzen 7600X iGPU and v_med3_num_f32 on the RX 9070
XT, measured with DenormFlushToZero). The integer-key form returned the
true median in every mode.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 09:59:52 +00:00
github-bot closed this pull request 2026-11-26 09:59:52 +00:00
Author
Owner

@⁠coderabbitai[bot] on GitHub · 2026-10-16 19:05 UTC · original · edited 2026-10-19 13:20 UTC

Review in Change Stack →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: Repository: KytyPS5/KytyPS5/.coderabbit.yaml
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 9f68ca2f-e5fe-4795-9161-195f8c3333c3
📥 Commits

Reviewing files that changed from the base of the PR and between e23737ca10a4b443f3d9a0b7cd846d2ffa76f8b1 and d8f3154d14.

📒 Files selected for processing: 3
  • src/graphics/shader/recompiler/backend/spirv/spirvEmitterAlu.cpp
  • src/graphics/shader/recompiler/backend/spirv/spirvEmitterAluHelpers.cpp
  • tests/ShaderRecompilerComputeTests.cpp
💤 Files with no reviewable changes: 1
  • src/graphics/shader/recompiler/backend/spirv/spirvEmitterAluHelpers.cpp
📝 Walkthrough

V_MED3_F32 now forwards the input-denormal flush flag through translation to SPIR-V emission. The emitter replaces subnormal inputs with signed zero when the flag is set, then selects a median or a NaN-dependent result. Compute tests cover denormal inputs in float modes 0xf0 and 0xc0, and update expected signed-zero results and SPIR-V word limits.

Suggested reviewers: nmzik, shadowbeat070

Priority: ➖ Normal

Change: Bug fix

Merge Risk: ⚪ Minimal · up to d8f31

V_MED3_F32 now produces a smaller shader and handles denormal and NaN inputs consistently with the reported GPU measurements. No actionable merge-blocking risk remains. The author could not verify PS5 hardware fidelity for the flush behavior.

Pre-merge checks: Passed 4 · Inconclusive 1

Failed Failed checks: 1 inconclusive

Check name Status Explanation Resolution
Docstring Coverage Inconclusive Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 10 functions across 5 files. (1 skipped: 1… Write docstrings for the functions missing them to satisfy the coverage threshold.
Passed Passed checks: 4 passed
Check name Status Explanation
Description check Passed The description clearly explains the integer-key implementation, denormal handling, NaN behavior, test updates, performance changes, and validation results.
Title check Passed The title concisely and accurately identifies the main change: emitting V_MED3_F32 using integer keys.
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: Docstring Coverage

Explanation

Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 10 functions across 5 files. (1 skipped: 1 too large.)

Resolution

Write docstrings for the functions missing them to satisfy the coverage threshold.

  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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

Comment @coderabbitai help to get the list of available commands.

**@⁠coderabbitai[bot]** on GitHub · 2026-10-16 19:05 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/1567#issuecomment-6222726774) · edited 2026-10-19 13:20 UTC <!-- 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/1567?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-in-coderabbit-ui-dark.svg?v=3"><source media="(prefers-color-scheme: light)" srcset="https://storage.googleapis.com/coderabbit_public_assets/review-stack-in-coderabbit-ui.svg?v=3"><img src="https://storage.googleapis.com/coderabbit_public_assets/review-stack-in-coderabbit-ui.svg?v=3" alt="Review in Change Stack →" height="32"></picture></a> <!-- review_stack_entry_end --> <!-- recent_review_start --> No actionable comments were generated in the recent review. 🎉 <details> <summary><strong>ℹ️ Recent review info</strong></summary> <dl> <dd> <details> <summary><strong>⚙️ Run configuration</strong></summary> <dl> <dd> - **Configuration used**: Repository: KytyPS5/KytyPS5/.coderabbit.yaml - **Review profile**: CHILL - **Plan**: Advanced - **Run ID**: `9f68ca2f-e5fe-4795-9161-195f8c3333c3` </dd> </dl> </details> <details> <summary><strong>📥 Commits</strong></summary> <dl> <dd> Reviewing files that changed from the base of the PR and between e23737ca10a4b443f3d9a0b7cd846d2ffa76f8b1 and d8f3154d146361ad058ca7fb90d04554c1aeeba5. </dd> </dl> </details> <details> <summary><strong>📒 Files selected for processing: 3</strong></summary> <dl> <dd> * `src/graphics/shader/recompiler/backend/spirv/spirvEmitterAlu.cpp` * `src/graphics/shader/recompiler/backend/spirv/spirvEmitterAluHelpers.cpp` * `tests/ShaderRecompilerComputeTests.cpp` </dd> </dl> </details> <details> <summary><strong>💤 Files with no reviewable changes: 1</strong></summary> <dl> <dd> * src/graphics/shader/recompiler/backend/spirv/spirvEmitterAluHelpers.cpp </dd> </dl> </details> </dd> </dl> </details> <!-- recent_review_end --> <!-- walkthrough_start --> <details> <summary><strong>📝 Walkthrough</strong></summary> <dl> <dd> `V_MED3_F32` now forwards the input-denormal flush flag through translation to SPIR-V emission. The emitter replaces subnormal inputs with signed zero when the flag is set, then selects a median or a NaN-dependent result. Compute tests cover denormal inputs in float modes `0xf0` and `0xc0`, and update expected signed-zero results and SPIR-V word limits. **Suggested reviewers:** `nmzik`, `shadowbeat070` </dd> </dl> </details> <!-- walkthrough_end --> <!-- change_assessment_start --> **Priority:** ➖ Normal <!-- change_assessment_commit:"d8f3154d146361ad058ca7fb90d04554c1aeeba5" --> **Change:** Bug fix <!-- change_assessment_end --> <details> <summary><strong>Merge Risk:</strong> <strong>⚪ Minimal</strong> · up to <code>d8f31</code></summary> <dl> <dd> <!-- final_review_risk_start --> <!-- final_review_risk_label **Merge Risk:** **⚪ Minimal** · up to `d8f31` --> <!-- final_review_risk_coverage:{"sourceCommitId":"d8f3154d146361ad058ca7fb90d04554c1aeeba5","coveredCommitId":"d8f3154d146361ad058ca7fb90d04554c1aeeba5","kind":"reviewed"} --> V_MED3_F32 now produces a smaller shader and handles denormal and NaN inputs consistently with the reported GPU measurements. No actionable merge-blocking risk remains. The author could not verify PS5 hardware fidelity for the flush behavior. <!-- 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 · <sup><picture><img src="https://storage.googleapis.com/coderabbit_public_assets/pre-merge-checks-counter-inconclusive-14-v2.svg" width="14" height="14" align="middle" alt="Inconclusive" title="Inconclusive"></picture></sup> 1</strong></summary> <dl> <dd> <strong><sup><picture><img src="https://storage.googleapis.com/coderabbit_public_assets/pre-merge-checks-counter-failed-14-v2.svg" width="14" height="14" align="middle" alt="Failed" title="Failed"></picture></sup> Failed checks: 1 inconclusive</strong> | Check name | Status | Explanation | Resolution | | :---------------- | :--------------------------------------------------------------------------------------------------------: | :-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | :--------------------------------------------------------------------------------- | | Docstring Coverage | ![Inconclusive](https://storage.googleapis.com/coderabbit_public_assets/pre-merge-checks-inconclusive.svg) | Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 10 functions across 5 files. (1 skipped: 1… | Write docstrings for the functions missing them to satisfy the coverage threshold. | <details> <summary><strong><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> Passed checks: 4 passed</strong></summary> <dl> <dd> | Check name | Status | Explanation | | :------------------------ | :--------------------------------------------------------------------------------------------: | :----------------------------------------------------------------------------------------------------------------------------------------------------------- | | Description check | ![Passed](https://storage.googleapis.com/coderabbit_public_assets/pre-merge-checks-passed.svg) | The description clearly explains the integer-key implementation, denormal handling, NaN behavior, test updates, performance changes, and validation results. | | Title check | ![Passed](https://storage.googleapis.com/coderabbit_public_assets/pre-merge-checks-passed.svg) | The title concisely and accurately identifies the main change: emitting V_MED3_F32 using integer keys. | | 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. | </dd> </dl> </details> <details> <summary><strong>Full details: Docstring Coverage</strong></summary> <dl> <dd> **Explanation** Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 10 functions across 5 files. (1 skipped: 1 too large.) **Resolution** Write docstrings for the functions missing them to satisfy the coverage threshold. </dd> </dl> </details> </dd> </dl> </details> <!-- pre_merge_checks_walkthrough_end --> <!-- autopilot:start --> - [ ] <!-- {"checkboxId":"2708ad07-9f24-4260-9c11-7dc76a49f2e3"} --> <strong title="Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts">Autofix</strong> · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts <!-- autopilot:end --> <!-- tips_start --> Thanks for using [CodeRabbit](https://coderabbit.ai?utm_source=oss&utm_medium=github&utm_campaign=KytyPS5/KytyPS5&utm_content=1567)! 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> - [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> <sub>Comment `@coderabbitai help` to get the list of available commands.</sub> <!-- tips_end -->
Author
Owner

@⁠NeySlim on GitHub · 2026-10-17 23:09 UTC · original

RADV data point: Radeon 8060S (RDNA 3.5), Mesa 26.2.4, against 763d3be3. VectorMed3F32DenormalInputs fails on main the same way as on your GPUs, e.g. 0x00019000 for (+0, 0x00019000, +0), and passes with the PR; the rest of the suite is unchanged.

**@⁠NeySlim** on GitHub · 2026-10-17 23:09 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/1567#issuecomment-6242864465) RADV data point: Radeon 8060S (RDNA 3.5), Mesa 26.2.4, against 763d3be3. `VectorMed3F32DenormalInputs` fails on main the same way as on your GPUs, e.g. `0x00019000` for `(+0, 0x00019000, +0)`, and passes with the PR; the rest of the suite is unchanged.
Author
Owner

@⁠Nawid3333 on GitHub · 2026-10-18 15:49 UTC · original

Follow-up commit e23737ca: in a compute shader that flushes f32 denormal inputs (FLOAT_MODE bit 4 clear, for example 0xc0), V_MED3_F32 now flushes its three inputs to signed zero before it takes the median.

Measured in shaders that declare DenormFlushToZero 32, the hardware instruction (v_med3_f32 on the Ryzen 7600X iGPU, RDNA2, through mid3(), and v_med3_num_f32 on the RX 9070 XT) returns +0 for (-1, d, 1) and -0 for (-d, -1, 1), with d = 0x00019000. With DenormPreserve 32 it returns d and -d. The integer keys of this PR gave the true median in every mode, which is right for FLOAT_MODE 0xf0 (the existing test) but not in shaders that flush.

The decision uses the same flush_f32_inputs as the compares, passed as FPCompareFlags on FPMedTri32. I have no PS5, so the link from FLOAT_MODE to this flush rests on that existing decision. V_MED3_F16 and the graphics stages are unchanged.

Tests, with the PR applied on 763d3be3:

  • New VectorMed3F32FlushesDenormalInputs (FLOAT_MODE 0xc0, the eight measured triples, one with a NaN): fails without the commit and passes with it on both GPUs, also with the harness forced to 32-wide subgroups and under the validation layer. VectorMed3F32DenormalInputs and VectorMed3F32NanUsesMin3Path still pass.
  • On both GPUs, the whole compute suite (809 entries, native and with 32-wide subgroups) and the 30 graphics cases give the same results with and without the commit, apart from the new test. shader_cfg_tests passes with and without it.
  • Against the hardware instruction itself (v_med3_f32 on the iGPU), a GLSL copy of the integer form gives the same result for every triple without a NaN, with this flush in a DenormFlushToZero shader and without it in a DenormPreserve shader: every 32-bit value in each position, the other two from 12 edge values. With three NaN inputs the hardware returns the first one and Kyty the third, as before this PR (the ISA pseudocode gives the third); that is not changed here.

Written with Claude Code (AI): it found this in a review of the PR, measured the hardware and wrote the commit; I read this before posting.

**@⁠Nawid3333** on GitHub · 2026-10-18 15:49 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/1567#issuecomment-6251896122) Follow-up commit `e23737ca`: in a compute shader that flushes f32 denormal inputs (FLOAT_MODE bit 4 clear, for example `0xc0`), `V_MED3_F32` now flushes its three inputs to signed zero before it takes the median. Measured in shaders that declare `DenormFlushToZero 32`, the hardware instruction (`v_med3_f32` on the Ryzen 7600X iGPU, RDNA2, through `mid3()`, and `v_med3_num_f32` on the RX 9070 XT) returns `+0` for (-1, d, 1) and `-0` for (-d, -1, 1), with d = `0x00019000`. With `DenormPreserve 32` it returns d and -d. The integer keys of this PR gave the true median in every mode, which is right for FLOAT_MODE `0xf0` (the existing test) but not in shaders that flush. The decision uses the same `flush_f32_inputs` as the compares, passed as `FPCompareFlags` on `FPMedTri32`. I have no PS5, so the link from FLOAT_MODE to this flush rests on that existing decision. `V_MED3_F16` and the graphics stages are unchanged. Tests, with the PR applied on 763d3be3: - New `VectorMed3F32FlushesDenormalInputs` (FLOAT_MODE `0xc0`, the eight measured triples, one with a NaN): fails without the commit and passes with it on both GPUs, also with the harness forced to 32-wide subgroups and under the validation layer. `VectorMed3F32DenormalInputs` and `VectorMed3F32NanUsesMin3Path` still pass. - On both GPUs, the whole compute suite (809 entries, native and with 32-wide subgroups) and the 30 graphics cases give the same results with and without the commit, apart from the new test. `shader_cfg_tests` passes with and without it. - Against the hardware instruction itself (`v_med3_f32` on the iGPU), a GLSL copy of the integer form gives the same result for every triple without a NaN, with this flush in a `DenormFlushToZero` shader and without it in a `DenormPreserve` shader: every 32-bit value in each position, the other two from 12 edge values. With three NaN inputs the hardware returns the first one and Kyty the third, as before this PR (the ISA pseudocode gives the third); that is not changed here. Written with Claude Code (AI): it found this in a review of the PR, measured the hardware and wrote the commit; I read this before posting.
Author
Owner

@⁠Nawid3333 on GitHub · 2026-10-19 13:17 UTC · original

Rebased on cd4eab9d. defb2f11 moved EmitFPMedTri32 into spirvEmitterAluHelpers.cpp; the integer-key form is back in spirvEmitterAlu.cpp next to EmitExt, and your EmitMinMaxF32 split stays for EmitMinMaxF32Value.

Before resolving it I ran the 23 rows of your new VectorMed3F32NanUsesMin3Path through the hardware instruction. Five rows that expect +0 come back as -0, the same on an RDNA2 GPU (Ryzen 7600X iGPU, v_med3_f32) and on the RX 9070 XT (v_med3_num_f32):

(a, b, c) defb2f11 expects both GPUs
(-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

The other rows match, apart from the one with three NaNs (0x7fc01234, 0x7fa00001, 0xffc05678): both GPUs return the first input unchanged, 0x7fc01234, where the table, main and this PR return the third. I left that row and that behaviour as they are.

The ISA pseudo-code gives the hardware's result if its == tells the zeros apart, as it has to in V_MIN_F32 and V_MAX_F32, which have separate branches for S0.f == +0.0 && S1.f == -0.0 and S0.f == -0.0 && S1.f == +0.0. Read that way, for (-0, -0, +0) MAX3 is +0, which equals neither S0 nor S1, so the result is V_MAX_F32(-0, -0) = -0. With a numeric compare (+0 == -0) it is +0.

This PR's keys sort -0 below +0, so it returns the hardware's value in all five rows, and the PR now expects -0 there.

How I measured: one compute shader that loads the 23 rows from a buffer and calls mid3() (GL_AMD_shader_trinary_minmax) with DenormPreserve, as in float_mode 0xf0. The driver's ISA dump has only v_med3_f32 (iGPU) or v_med3_num_f32 (RX 9070 XT) for it, and the validation layer reports nothing. The shader and all 23 results are below.

Size: the module of VectorMed3F32NanUsesMin3Path (23 V_MED3_F32) is 17,593 / 36,607 words on main and 13,164 / 27,739 with this PR (wave 32 / wave 64 on a 32-wide host), about 38 instructions fewer per emitted V_MED3_F32. I moved the test's size limit down with it and kept your margins: 13,171 / 27,752.

I also added four rows to this PR's two tests, with the medians both GPUs return: the largest denormal and the smallest normal next to ±0.5 in the flush test, and +inf next to finite inputs in both. Before, an infinity counted as a NaN, the smallest normal flushed, or the larger denormals left unflushed passed these tests and VectorMed3F32NanUsesMin3Path; now each of the three makes at least one of the two tests fail.

Tests on both GPUs, also with 32-wide subgroups: VectorMed3F32NanUsesMin3Path (both waves), VectorMed3F32DenormalInputs and VectorMed3F32FlushesDenormalInputs fail on main and pass with the PR. Where main's VectorMed3F32NanUsesMin3Path gets past the new size limit (wave 64 on a 64-wide host), it fails exactly the five rows above. The other 809 compute-suite entries and the 30 graphics cases give the same results as on main. Fork CI (Windows, Linux, macOS): https://github.com/Nawid3333/KytyPS5/actions/runs/39254554407

Probe shader and the 23 rows (hex bits)
#version 450
#extension GL_AMD_shader_trinary_minmax : require
#extension GL_EXT_spirv_intrinsics : require
spirv_execution_mode(extensions = ["SPV_KHR_float_controls"], capabilities = [4464], 4459, 32);
layout(local_size_x = 1) in;
layout(std430, set = 0, binding = 0) buffer B { uint w[]; } b;
// Words 0..68: the 23 (a, b, c) rows of nmzik's VectorMed3F32NanUsesMin3Path (defb2f11). Words 128..150: mid3.
void main() {
  for (int i = 0; i < 23; ++i) {
    float a = uintBitsToFloat(b.w[i * 3 + 0]);
    float x = uintBitsToFloat(b.w[i * 3 + 1]);
    float c = uintBitsToFloat(b.w[i * 3 + 2]);
    b.w[128 + i] = floatBitsToUint(mid3(a, x, c));
  }
}
row a b c defb2f11 expects Ryzen 7600X iGPU RX 9070 XT
1 40000000 40400000 7fa00001 40000000 40000000 40000000
2 40000000 40400000 7fc00000 40000000 40000000 40000000
3 7fa00001 40000000 40400000 40000000 40000000 40000000
4 40400000 7fc01234 40000000 40000000 40000000 40000000
5 80000000 00000000 3f800000 00000000 00000000 00000000
6 00000000 80000000 bf800000 80000000 80000000 80000000
7 80000000 80000000 00000000 00000000 80000000 80000000
8 00000000 00000000 80000000 00000000 00000000 00000000
9 80000000 00000000 80000000 00000000 80000000 80000000
10 00000000 80000000 80000000 80000000 80000000 80000000
11 bf800000 80000000 00000000 00000000 80000000 80000000
12 bf800000 00000000 80000000 80000000 80000000 80000000
13 80000000 bf800000 00000000 00000000 80000000 80000000
14 00000000 bf800000 80000000 80000000 80000000 80000000
15 80000000 00000000 bf800000 00000000 80000000 80000000
16 7fc01234 80000000 00000000 80000000 80000000 80000000
17 3f800000 40400000 40000000 40000000 40000000 40000000
18 c0400000 bf800000 c0000000 c0000000 c0000000 c0000000
19 ff800000 7f800000 3f800000 3f800000 3f800000 3f800000
20 00000001 7f800000 7fc01234 00000001 00000001 00000001
21 80000001 80000001 00000000 80000001 80000001 80000001
22 7fc01234 7fa00001 ffc05678 ffc05678 7fc01234 7fc01234
23 7fa00001 ffc05678 40000000 40000000 40000000 40000000
**@⁠Nawid3333** on GitHub · 2026-10-19 13:17 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/1567#issuecomment-6268161024) Rebased on cd4eab9d. defb2f11 moved `EmitFPMedTri32` into `spirvEmitterAluHelpers.cpp`; the integer-key form is back in `spirvEmitterAlu.cpp` next to `EmitExt`, and your `EmitMinMaxF32` split stays for `EmitMinMaxF32Value`. Before resolving it I ran the 23 rows of your new `VectorMed3F32NanUsesMin3Path` through the hardware instruction. Five rows that expect +0 come back as -0, the same on an RDNA2 GPU (Ryzen 7600X iGPU, `v_med3_f32`) and on the RX 9070 XT (`v_med3_num_f32`): | (a, b, c) | defb2f11 expects | both GPUs | |---|---|---| | (-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 | The other rows match, apart from the one with three NaNs (0x7fc01234, 0x7fa00001, 0xffc05678): both GPUs return the first input unchanged, 0x7fc01234, where the table, main and this PR return the third. I left that row and that behaviour as they are. The ISA pseudo-code gives the hardware's result if its `==` tells the zeros apart, as it has to in V_MIN_F32 and V_MAX_F32, which have separate branches for `S0.f == +0.0 && S1.f == -0.0` and `S0.f == -0.0 && S1.f == +0.0`. Read that way, for (-0, -0, +0) MAX3 is +0, which equals neither S0 nor S1, so the result is V_MAX_F32(-0, -0) = -0. With a numeric compare (+0 == -0) it is +0. This PR's keys sort -0 below +0, so it returns the hardware's value in all five rows, and the PR now expects -0 there. How I measured: one compute shader that loads the 23 rows from a buffer and calls `mid3()` (GL_AMD_shader_trinary_minmax) with DenormPreserve, as in float_mode 0xf0. The driver's ISA dump has only `v_med3_f32` (iGPU) or `v_med3_num_f32` (RX 9070 XT) for it, and the validation layer reports nothing. The shader and all 23 results are below. Size: the module of `VectorMed3F32NanUsesMin3Path` (23 V_MED3_F32) is 17,593 / 36,607 words on main and 13,164 / 27,739 with this PR (wave 32 / wave 64 on a 32-wide host), about 38 instructions fewer per emitted V_MED3_F32. I moved the test's size limit down with it and kept your margins: 13,171 / 27,752. I also added four rows to this PR's two tests, with the medians both GPUs return: the largest denormal and the smallest normal next to ±0.5 in the flush test, and +inf next to finite inputs in both. Before, an infinity counted as a NaN, the smallest normal flushed, or the larger denormals left unflushed passed these tests and `VectorMed3F32NanUsesMin3Path`; now each of the three makes at least one of the two tests fail. Tests on both GPUs, also with 32-wide subgroups: `VectorMed3F32NanUsesMin3Path` (both waves), `VectorMed3F32DenormalInputs` and `VectorMed3F32FlushesDenormalInputs` fail on main and pass with the PR. Where main's `VectorMed3F32NanUsesMin3Path` gets past the new size limit (wave 64 on a 64-wide host), it fails exactly the five rows above. The other 809 compute-suite entries and the 30 graphics cases give the same results as on main. Fork CI (Windows, Linux, macOS): https://github.com/Nawid3333/KytyPS5/actions/runs/39254554407 <details><summary>Probe shader and the 23 rows (hex bits)</summary> ```glsl #version 450 #extension GL_AMD_shader_trinary_minmax : require #extension GL_EXT_spirv_intrinsics : require spirv_execution_mode(extensions = ["SPV_KHR_float_controls"], capabilities = [4464], 4459, 32); layout(local_size_x = 1) in; layout(std430, set = 0, binding = 0) buffer B { uint w[]; } b; // Words 0..68: the 23 (a, b, c) rows of nmzik's VectorMed3F32NanUsesMin3Path (defb2f11). Words 128..150: mid3. void main() { for (int i = 0; i < 23; ++i) { float a = uintBitsToFloat(b.w[i * 3 + 0]); float x = uintBitsToFloat(b.w[i * 3 + 1]); float c = uintBitsToFloat(b.w[i * 3 + 2]); b.w[128 + i] = floatBitsToUint(mid3(a, x, c)); } } ``` | row | a | b | c | defb2f11 expects | Ryzen 7600X iGPU | RX 9070 XT | |---|---|---|---|---|---|---| | 1 | 40000000 | 40400000 | 7fa00001 | 40000000 | 40000000 | 40000000 | | 2 | 40000000 | 40400000 | 7fc00000 | 40000000 | 40000000 | 40000000 | | 3 | 7fa00001 | 40000000 | 40400000 | 40000000 | 40000000 | 40000000 | | 4 | 40400000 | 7fc01234 | 40000000 | 40000000 | 40000000 | 40000000 | | 5 | 80000000 | 00000000 | 3f800000 | 00000000 | 00000000 | 00000000 | | 6 | 00000000 | 80000000 | bf800000 | 80000000 | 80000000 | 80000000 | | 7 | 80000000 | 80000000 | 00000000 | 00000000 | 80000000 | 80000000 | | 8 | 00000000 | 00000000 | 80000000 | 00000000 | 00000000 | 00000000 | | 9 | 80000000 | 00000000 | 80000000 | 00000000 | 80000000 | 80000000 | | 10 | 00000000 | 80000000 | 80000000 | 80000000 | 80000000 | 80000000 | | 11 | bf800000 | 80000000 | 00000000 | 00000000 | 80000000 | 80000000 | | 12 | bf800000 | 00000000 | 80000000 | 80000000 | 80000000 | 80000000 | | 13 | 80000000 | bf800000 | 00000000 | 00000000 | 80000000 | 80000000 | | 14 | 00000000 | bf800000 | 80000000 | 80000000 | 80000000 | 80000000 | | 15 | 80000000 | 00000000 | bf800000 | 00000000 | 80000000 | 80000000 | | 16 | 7fc01234 | 80000000 | 00000000 | 80000000 | 80000000 | 80000000 | | 17 | 3f800000 | 40400000 | 40000000 | 40000000 | 40000000 | 40000000 | | 18 | c0400000 | bf800000 | c0000000 | c0000000 | c0000000 | c0000000 | | 19 | ff800000 | 7f800000 | 3f800000 | 3f800000 | 3f800000 | 3f800000 | | 20 | 00000001 | 7f800000 | 7fc01234 | 00000001 | 00000001 | 00000001 | | 21 | 80000001 | 80000001 | 00000000 | 80000001 | 80000001 | 80000001 | | 22 | 7fc01234 | 7fa00001 | ffc05678 | ffc05678 | 7fc01234 | 7fc01234 | | 23 | 7fa00001 | ffc05678 | 40000000 | 40000000 | 40000000 | 40000000 | </details>
Author
Owner

@⁠Nawid3333 on GitHub · 2026-10-24 18:36 UTC · original

Closing: main has its own V_MED3_F32 lowering since 3c4124da ("emit F32 min/max as exact compare-selects, MED3 with one max and saturate as NClamp"), so this version no longer applies.

**@⁠Nawid3333** on GitHub · 2026-10-24 18:36 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/1567#issuecomment-6388017362) Closing: `main` has its own V_MED3_F32 lowering since 3c4124da ("emit F32 min/max as exact compare-selects, MED3 with one max and saturate as NClamp"), so this version no longer applies.

Pull request closed

This discussion has been locked. Commenting is limited to contributors.
No description provided.