fix(shader): give a tiny f32 product that rounds to 2^-126 that value #17932

Merged
Gonic0 merged 2 commits from upstream-pr/17932 into main 2026-11-15 20:20:36 +00:00
Gonic0 commented 2026-11-09 23:46:05 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Gonic0 on 2026-11-09T23:46:05Z.


What

To nearest even, flushTinyProduct scales one factor by 2^64 and compares the scaled product with 2^-62. A product below it is flushed to a zero of its sign; otherwise the multiply's own result is kept. That relies on the host rounding a product whose exact value is just below 2^-126 up to 2^-126, as RDNA2 does.

Apple GPUs do not: KosmicKrisp and MoltenVK on an M5 Pro flush such a product before rounding, so 0x3f7ffffe * 0x00800001 comes back as zero instead of 0x00800000, and agc_driver_f32_constant_factor_tininess fails on both.

When the scaled product is exactly 2^-62, the result is now 2^-126 of the product's sign. That is the value the multiply returns on RDNA2, so nothing changes there. The new select wraps the existing one, which keeps the shape DenormalFlushEliminator recognizes; with the select the other way round, agc_denormal_flush_elimination fails (3 f32 exponent masks, expected 1).

The startup flush probe runs these products, so on both Apple drivers it now reports emulated (264 of 5472 probe results differ from the emulated flush): the hardware flush does not round them the way the console does. MoltenVK therefore keeps the emulated flush, as KosmicKrisp already does after #17588. That costs speed on MoltenVK. PPSA21564 on MoltenVK 1.4.2, same binary with and without APS5_NO_HW_F32_FLUSH=1 (what this change makes the probe choose), fixed save, runs alternated hardware/emulated/emulated/hardware, mean of the four 10 s windows from 45 to 85 s:

run presents / 10 s GPU busy per frame
hardware flush 125.2, 130.0 50.1, 44.5 ms
emulated flush 116.0, 117.5 56.4, 50.2 ms

About 8.5 % fewer presents and 13 % more GPU time. Only that early window could be measured: on MoltenVK the title stops presenting about 81 s in, with or without this change (no error is logged), so the heavier scenes were not reached. The tree for these runs is main 31b4aa91e8 with #17611, #17588, the macOS build pull requests and imellag's mesh list restart change from #9356, without which the title stops 7 s in on MoltenVK. The trade is the exact 2^-126 result for a measurable slowdown on MoltenVK.

A fused result with a nonzero addend that rounds up to 2^-126 is still flushed on Apple GPUs (agc_driver_f32_denormal_flush, v_fma_f32 with 0x00800000, 0x80800001, 0x00800000). Fixing it would need a second, scaled fma for every fma, so it is left out and recorded in docs/dev/debt/shader.md.

Tested

macOS 27.3, Apple M5 Pro, Apple clang, x86-64 Release under Rosetta 2. Tree: main b673b14c39 with #16591, #17425, #17532 and #17767, which macOS needs to build, and this change. Nothing in OperandAccess.cpp, FloatInstructions.cpp or DenormalFlushEliminator.cpp changed between that commit and main 1c288270a7.

ctest -R 'f32|f16|denorm|float32|mullit' (77 tests, ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1), before and after:

driver before after
KosmicKrisp (Mesa main 94ff826188c) 7 fail 6 fail: agc_driver_f32_constant_factor_tininess now passes
MoltenVK 1.4.2 2 fail 1 fail: agc_driver_f32_constant_factor_tininess now passes

No test newly fails on either driver, agc_denormal_flush_elimination included. The remaining failures are the ones that already failed: on KosmicKrisp the NaN and interpolation cases listed in #15859, and agc_driver_f32_denormal_flush on both for the nonzero-addend fma above.

Linux, Windows, AMD and NVIDIA: not run. On a device that rounds the product to 2^-126 itself, the new select picks the same value.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Fixes: none
  • Unblocks: none
  • GPUs tested: Apple M5 Pro with KosmicKrisp (Mesa main 94ff826188c) and MoltenVK 1.4.2, macOS 27.3
  • AI-assisted: yes

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5233105142 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/17932). Originally posted by [Gonic0](https://github.com/Gonic0) on 2026-11-09T23:46:05Z. --- ### What To nearest even, `flushTinyProduct` scales one factor by 2^64 and compares the scaled product with 2^-62. A product below it is flushed to a zero of its sign; otherwise the multiply's own result is kept. That relies on the host rounding a product whose exact value is just below 2^-126 up to 2^-126, as RDNA2 does. Apple GPUs do not: KosmicKrisp and MoltenVK on an M5 Pro flush such a product before rounding, so `0x3f7ffffe * 0x00800001` comes back as zero instead of `0x00800000`, and `agc_driver_f32_constant_factor_tininess` fails on both. When the scaled product is exactly 2^-62, the result is now 2^-126 of the product's sign. That is the value the multiply returns on RDNA2, so nothing changes there. The new select wraps the existing one, which keeps the shape `DenormalFlushEliminator` recognizes; with the select the other way round, `agc_denormal_flush_elimination` fails (`3 f32 exponent masks, expected 1`). The startup flush probe runs these products, so on both Apple drivers it now reports `emulated (264 of 5472 probe results differ from the emulated flush)`: the hardware flush does not round them the way the console does. MoltenVK therefore keeps the emulated flush, as KosmicKrisp already does after #17588. That costs speed on MoltenVK. PPSA21564 on MoltenVK 1.4.2, same binary with and without `APS5_NO_HW_F32_FLUSH=1` (what this change makes the probe choose), fixed save, runs alternated hardware/emulated/emulated/hardware, mean of the four 10 s windows from 45 to 85 s: | run | presents / 10 s | GPU busy per frame | |---|---|---| | hardware flush | 125.2, 130.0 | 50.1, 44.5 ms | | emulated flush | 116.0, 117.5 | 56.4, 50.2 ms | About 8.5 % fewer presents and 13 % more GPU time. Only that early window could be measured: on MoltenVK the title stops presenting about 81 s in, with or without this change (no error is logged), so the heavier scenes were not reached. The tree for these runs is `main` `31b4aa91e8` with #17611, #17588, the macOS build pull requests and imellag's mesh list restart change from #9356, without which the title stops 7 s in on MoltenVK. The trade is the exact 2^-126 result for a measurable slowdown on MoltenVK. A fused result with a nonzero addend that rounds up to 2^-126 is still flushed on Apple GPUs (`agc_driver_f32_denormal_flush`, `v_fma_f32` with `0x00800000`, `0x80800001`, `0x00800000`). Fixing it would need a second, scaled fma for every fma, so it is left out and recorded in `docs/dev/debt/shader.md`. ### Tested macOS 27.3, Apple M5 Pro, Apple clang, x86-64 Release under Rosetta 2. Tree: `main` `b673b14c39` with #16591, #17425, #17532 and #17767, which macOS needs to build, and this change. Nothing in `OperandAccess.cpp`, `FloatInstructions.cpp` or `DenormalFlushEliminator.cpp` changed between that commit and `main` `1c288270a7`. `ctest -R 'f32|f16|denorm|float32|mullit'` (77 tests, `ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1`), before and after: | driver | before | after | |---|---|---| | KosmicKrisp (Mesa `main` `94ff826188c`) | 7 fail | 6 fail: `agc_driver_f32_constant_factor_tininess` now passes | | MoltenVK 1.4.2 | 2 fail | 1 fail: `agc_driver_f32_constant_factor_tininess` now passes | No test newly fails on either driver, `agc_denormal_flush_elimination` included. The remaining failures are the ones that already failed: on KosmicKrisp the NaN and interpolation cases listed in #15859, and `agc_driver_f32_denormal_flush` on both for the nonzero-addend fma above. Linux, Windows, AMD and NVIDIA: not run. On a device that rounds the product to 2^-126 itself, the new select picks the same value. ### Checklist - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: Apple M5 Pro with KosmicKrisp (Mesa `main` `94ff826188c`) and MoltenVK 1.4.2, macOS 27.3 - [x] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code)
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5!17932
No description provided.