fix(shader): keep f32 denormals in compares when denormals are kept #3895

Merged
CouchRiv merged 1 commit from upstream-pr/3895 into main 2026-10-12 06:39:35 +00:00
CouchRiv commented 2026-10-11 18:58:05 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by CouchRiv on 2026-10-11T18:58:05Z.


What

With the f32 denormal field of FLOAT_MODE set to keep denormals, v_cmp_X_f32 and v_cmpx_X_f32 read a denormal operand as zero:

v_cmp_eq_f32 0x00000000, 0x00000001   ->  1   (RDNA2: 0)
v_cmp_lg_f32 0x00000000, 0x00000001   ->  0   (RDNA2: 1)

The compares are emitted as the SPIR-V float compares, and NVIDIA, RADV and lavapipe all flush denormals in those. With denormals flushed the hardware does the same, so that case was right.

When denormals are kept, the operands are now compared as sign-magnitude integers (so +0 and -0 still compare equal) and a NaN in either operand gives the ordered or unordered result. Compares with flushed denormals, f16 and f64 compares and the constant-operand paths are untouched. Only shaders that keep f32 denormals pay the extra integer ops.

On RDNA2 (gfx1036, Radeon 610M) the IEEE mode makes no difference to these compares; the f32 denormal mode does. 256 operand pairs per opcode (zeros, denormals including the largest, the smallest normal, ±1, infinities, quiet and signalling NaNs, the largest finite value) for the 12 conditions eq ge gt le lg lt neq nge ngt nle nlg nlt in both encodings, in IEEE 0 and 1 with denormals kept and flushed: 24,576 rows measured, 0 differing after this change.

Tested

Linux (CachyOS), GCC; RTX 4070 Ti (NVIDIA), Radeon 610M (RADV) and lavapipe (Mesa 26.2.4).

  • Red on main: agc_driver_vcmp_f32_denormals fails on all three drivers (v_cmpx_eq_f32 of 0x00000000 and 0x00000001 is 1, expected 0). Green with this change on every driver below.
  • ctest -R "agc_driver_(vcmp_f32_denormals|f32_compare_constant|f32_denormal_flush|vop3_compare_modifiers|vopc_compare|vopc_compare_table|f32_min_max_nan|f32_arithmetic_nan|f32_result_clamp)$" after a clean build:
    • nvidia: 100% tests passed out of 9
    • radeon: 100% tests passed out of 9
    • lavapipe: 100% tests passed out of 9
  • tools/check_conventions.py --base origin/main and git diff --check: clean.
  • Windows and macOS not run. Denormal modes 1 and 2 still throw, as before.

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 in TechnicalDebt
  • No notes, investigation .md files or images added to the repository
  • New third-party code is a submodule built from source (none in this change)
  • Generic behaviour, not specific to one title
  • Depends on: none
    AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4821509865 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3895). Originally posted by [CouchRiv](https://github.com/CouchRiv) on 2026-10-11T18:58:05Z. --- ### What With the f32 denormal field of `FLOAT_MODE` set to keep denormals, `v_cmp_X_f32` and `v_cmpx_X_f32` read a denormal operand as zero: ``` v_cmp_eq_f32 0x00000000, 0x00000001 -> 1 (RDNA2: 0) v_cmp_lg_f32 0x00000000, 0x00000001 -> 0 (RDNA2: 1) ``` The compares are emitted as the SPIR-V float compares, and NVIDIA, RADV and lavapipe all flush denormals in those. With denormals flushed the hardware does the same, so that case was right. When denormals are kept, the operands are now compared as sign-magnitude integers (so +0 and -0 still compare equal) and a NaN in either operand gives the ordered or unordered result. Compares with flushed denormals, f16 and f64 compares and the constant-operand paths are untouched. Only shaders that keep f32 denormals pay the extra integer ops. On RDNA2 (gfx1036, Radeon 610M) the IEEE mode makes no difference to these compares; the f32 denormal mode does. 256 operand pairs per opcode (zeros, denormals including the largest, the smallest normal, ±1, infinities, quiet and signalling NaNs, the largest finite value) for the 12 conditions `eq ge gt le lg lt neq nge ngt nle nlg nlt` in both encodings, in IEEE 0 and 1 with denormals kept and flushed: 24,576 rows measured, 0 differing after this change. ### Tested Linux (CachyOS), GCC; RTX 4070 Ti (NVIDIA), Radeon 610M (RADV) and lavapipe (Mesa 26.2.4). - Red on `main`: `agc_driver_vcmp_f32_denormals` fails on all three drivers (`v_cmpx_eq_f32` of 0x00000000 and 0x00000001 is 1, expected 0). Green with this change on every driver below. - `ctest -R "agc_driver_(vcmp_f32_denormals|f32_compare_constant|f32_denormal_flush|vop3_compare_modifiers|vopc_compare|vopc_compare_table|f32_min_max_nan|f32_arithmetic_nan|f32_result_clamp)$"` after a clean build: - nvidia: 100% tests passed out of 9 - radeon: 100% tests passed out of 9 - lavapipe: 100% tests passed out of 9 - `tools/check_conventions.py --base origin/main` and `git diff --check`: clean. - Windows and macOS not run. Denormal modes 1 and 2 still throw, as before. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [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 in TechnicalDebt - [x] No notes, investigation `.md` files or images added to the repository - [x] New third-party code is a submodule built from source (none in this change) - [x] Generic behaviour, not specific to one title - [x] Depends on: none AI-assisted: yes
LeandroLP23 commented 2026-10-11 19:41:17 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by LeandroLP23 on 2026-10-11T19:41:17Z.


Windows run of this head (124bfde44, on main a11c8f7f7): Windows 11 (26300), WinLibs GCC 15.2.0 r7, Release, full build. GPU: Radeon 860M (RDNA 3.5), AMD's Windows driver 32.0.13058.8001 (Vulkan 24.30.58.08, LLPC). Shader cache cleared before each run.

  • Your 9 tests, 3 runs: 8/9 pass every time, including agc_driver_vcmp_f32_denormals. agc_driver_f32_compare_constant fails with the same message as on main (v_cmp_lt_f32 0x7fc00000, 0x7f800000 is 1, expected 0). That's #3536 on this driver, fixed by #3541, not this change.
  • With CompareInstructions.cpp and TranslationContext.hpp reverted to main, agc_driver_vcmp_f32_denormals fails as on your drivers: v_cmpx_eq_f32 denormals kept, IEEE mode on of 0x00000000 and 0x00000001 is 1, expected 0. So AMD's Windows driver reads a kept denormal as zero in the float compares as well.
  • This head merged with #3541 (d668b7c2b): no conflicts (TechnicalDebt.md auto-merges), 9/9 pass in 3 runs.
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6112953499 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3895#issuecomment-6112953499). Originally posted by [LeandroLP23](https://github.com/LeandroLP23) on 2026-10-11T19:41:17Z. --- Windows run of this head (`124bfde44`, on `main` `a11c8f7f7`): Windows 11 (26300), WinLibs GCC 15.2.0 r7, Release, full build. GPU: Radeon 860M (RDNA 3.5), AMD's Windows driver 32.0.13058.8001 (Vulkan `24.30.58.08`, LLPC). Shader cache cleared before each run. - Your 9 tests, 3 runs: 8/9 pass every time, including `agc_driver_vcmp_f32_denormals`. `agc_driver_f32_compare_constant` fails with the same message as on `main` (`v_cmp_lt_f32 0x7fc00000, 0x7f800000 is 1, expected 0`). That's #3536 on this driver, fixed by #3541, not this change. - With `CompareInstructions.cpp` and `TranslationContext.hpp` reverted to `main`, `agc_driver_vcmp_f32_denormals` fails as on your drivers: `v_cmpx_eq_f32 denormals kept, IEEE mode on of 0x00000000 and 0x00000001 is 1, expected 0`. So AMD's Windows driver reads a kept denormal as zero in the float compares as well. - This head merged with #3541 (`d668b7c2b`): no conflicts (`TechnicalDebt.md` auto-merges), 9/9 pass in 3 runs.
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!3895
No description provided.