fix(shader): flush f16 denormals in unary, frexp and conversion ops per the float mode #3932

Merged
CouchRiv merged 1 commit from upstream-pr/3932 into main 2026-10-12 04:29:43 +00:00
CouchRiv commented 2026-10-11 19:17:54 +00:00 (Migrated from github.com)

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


What

With the f16 denormal field of FLOAT_MODE set to flush (what most titles use), RDNA2 turns a denormal half operand into its signed zero before these instructions use it, and turns a denormal half result into its signed zero. The recompiler read denormal halves as they are and wrote denormal results as they are, so for example v_ceil_f16 0x0001 gave 0x3c00 where the hardware gives 0, and v_rcp_f16 0x7bff gave the denormal 0x0100 where the hardware gives 0.

The affected instructions are v_ceil_f16, v_floor_f16, v_fract_f16, v_rcp_f16, v_rsq_f16, v_sqrt_f16, v_log_f16, v_exp_f16, v_sin_f16, v_frexp_mant_f16, v_frexp_exp_i16_f16, v_cvt_norm_i16_f16, v_cvt_norm_u16_f16 and v_cvt_f32_f16. They now share one rule driven by the float mode: bit 0 of the denormal field clear flushes a denormal half input, bit 1 clear flushes a denormal half result. v_ldexp_f16 uses the same helper for its input instead of its own copy. A shader without a known float mode, and every mode that keeps denormals, compile as before. The pknorm packs are not measured and keep their old lowering (listed in TechnicalDebt).

On main, 310 of 1,456 checks of the new test differ (14 of 14 instructions); with this change none do.

Tested

Linux (CachyOS), GCC; RTX 4070 Ti (NVIDIA), Radeon 610M (RADV) and lavapipe (Mesa 26.2.4). Expected values were measured on a Radeon 610M (gfx1036).

  • New agc_driver_f16_denormal_flush: 182 input rows over the 14 instructions, each in all four denorm16 modes and with IEEE on and off. Red on main, green with this change on NVIDIA, RADV and lavapipe.
  • Also compared every non-NaN half input (63,490 per mode) of the 14 instructions with the hardware in six float modes on lavapipe: the only differences are 16 one-ULP results of v_exp_f16 and v_sin_f16, which are the same with denormals kept.
  • tools/check_conventions.py --base origin/main and git diff --check: clean.
  • Windows and macOS not run.

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:4821646212 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3932). Originally posted by [CouchRiv](https://github.com/CouchRiv) on 2026-10-11T19:17:54Z. --- ### What With the f16 denormal field of FLOAT_MODE set to flush (what most titles use), RDNA2 turns a denormal half operand into its signed zero before these instructions use it, and turns a denormal half result into its signed zero. The recompiler read denormal halves as they are and wrote denormal results as they are, so for example `v_ceil_f16 0x0001` gave 0x3c00 where the hardware gives 0, and `v_rcp_f16 0x7bff` gave the denormal 0x0100 where the hardware gives 0. The affected instructions are `v_ceil_f16`, `v_floor_f16`, `v_fract_f16`, `v_rcp_f16`, `v_rsq_f16`, `v_sqrt_f16`, `v_log_f16`, `v_exp_f16`, `v_sin_f16`, `v_frexp_mant_f16`, `v_frexp_exp_i16_f16`, `v_cvt_norm_i16_f16`, `v_cvt_norm_u16_f16` and `v_cvt_f32_f16`. They now share one rule driven by the float mode: bit 0 of the denormal field clear flushes a denormal half input, bit 1 clear flushes a denormal half result. `v_ldexp_f16` uses the same helper for its input instead of its own copy. A shader without a known float mode, and every mode that keeps denormals, compile as before. The pknorm packs are not measured and keep their old lowering (listed in TechnicalDebt). On `main`, 310 of 1,456 checks of the new test differ (14 of 14 instructions); with this change none do. ### Tested Linux (CachyOS), GCC; RTX 4070 Ti (NVIDIA), Radeon 610M (RADV) and lavapipe (Mesa 26.2.4). Expected values were measured on a Radeon 610M (gfx1036). - New `agc_driver_f16_denormal_flush`: 182 input rows over the 14 instructions, each in all four denorm16 modes and with IEEE on and off. Red on `main`, green with this change on NVIDIA, RADV and lavapipe. - Also compared every non-NaN half input (63,490 per mode) of the 14 instructions with the hardware in six float modes on lavapipe: the only differences are 16 one-ULP results of `v_exp_f16` and `v_sin_f16`, which are the same with denormals kept. - `tools/check_conventions.py --base origin/main` and `git diff --check`: clean. - Windows and macOS not run. ### 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
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!3932
No description provided.