fix(shader): flush scalar f16 add/mul/sub denormals to signed zero #2987

Merged
CouchRiv merged 1 commit from upstream-pr/2987 into main 2026-10-10 23:51:46 +00:00
CouchRiv commented 2026-10-10 19:48:55 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by CouchRiv on 2026-10-10T19:48:55Z.


What

AGC graphics: f16 rounding: vector 0 (0x00000000, 0x00000401, 0x000003ff) v_add_f16 is 0xabcd0800, expected 0xabcd0401

On main, scalar f16 add/mul/sub ignore FLOAT_MODE f16 denormal bits and keep denormals the hardware flushes. float16Binary now flushes inputs when denorm16 bit 0 is clear and results when bit 1 is clear (nearest-even, known float mode). Multiply checks tininess after rounding to 11 significant bits (0x387ff000); add/sub use 0x38800000. v_sub_f16 / v_subrev_f16 share the same helper. Unknown float mode and other rounding modes stay as on main. NaN payload selection is unchanged (still driver-dependent).

Tested

CachyOS Linux; RTX 4070 Ti (NVIDIA), Radeon 610M (RADV / gfx1036 hw-oracle), lavapipe (CI ICD).

  • Replay of 1156 non-NaN-input operand pairs per mode (denorm16 = 0, 1, 2; ieee 1) through the recompiler on RTX 4070 Ti (NVIDIA), Radeon 610M (RADV) and lavapipe, against gfx1036 on the hardware oracle: v_add_f16 / v_mul_f16 rows that differed from hardware went from 800 to 0 on RADV and lavapipe with no regression on any of the 1681 rows; v_sub_f16 / v_subrev_f16 from 380 to 0. On NVIDIA 760 and 380 are fixed; the remaining 70 and 12 rows are NaN payloads (7fff vs hardware fe00), and 40 of the 70 (denormal x infinity in v_mul_f16) moved from an infinity to that NaN.
  • agc_driver_float16_rounding (add and mul only) fails on main with the line above and passes on the branch on all three drivers.
  • Live tininess on gfx1036: with denorm16 = 1 (output flush), 0x3bff*0x0400 → 0, 0x3c01*0x03ff → 0x0400 (after-rounding at threshold 0x387ff000).
  • check_conventions.py --base origin/main; git diff --check.

Not tested here: Windows, packed f16, ternary f16, directed round16 ≠ 0. Sub and subrev are covered by the replay above, not by agc_driver_float16_rounding.

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 (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
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4813982802 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2987). Originally posted by [CouchRiv](https://github.com/CouchRiv) on 2026-10-10T19:48:55Z. --- ### What ```text AGC graphics: f16 rounding: vector 0 (0x00000000, 0x00000401, 0x000003ff) v_add_f16 is 0xabcd0800, expected 0xabcd0401 ``` On `main`, scalar f16 add/mul/sub ignore `FLOAT_MODE` f16 denormal bits and keep denormals the hardware flushes. `float16Binary` now flushes inputs when denorm16 bit 0 is clear and results when bit 1 is clear (nearest-even, known float mode). Multiply checks tininess after rounding to 11 significant bits (`0x387ff000`); add/sub use `0x38800000`. `v_sub_f16` / `v_subrev_f16` share the same helper. Unknown float mode and other rounding modes stay as on `main`. NaN payload selection is unchanged (still driver-dependent). ### Tested CachyOS Linux; RTX 4070 Ti (NVIDIA), Radeon 610M (RADV / gfx1036 hw-oracle), lavapipe (CI ICD). - Replay of 1156 non-NaN-input operand pairs per mode (denorm16 = 0, 1, 2; ieee 1) through the recompiler on RTX 4070 Ti (NVIDIA), Radeon 610M (RADV) and lavapipe, against gfx1036 on the hardware oracle: v_add_f16 / v_mul_f16 rows that differed from hardware went from 800 to 0 on RADV and lavapipe with no regression on any of the 1681 rows; v_sub_f16 / v_subrev_f16 from 380 to 0. On NVIDIA 760 and 380 are fixed; the remaining 70 and 12 rows are NaN payloads (`7fff` vs hardware `fe00`), and 40 of the 70 (denormal x infinity in v_mul_f16) moved from an infinity to that NaN. - `agc_driver_float16_rounding` (add and mul only) fails on main with the line above and passes on the branch on all three drivers. - Live tininess on gfx1036: with denorm16 = 1 (output flush), `0x3bff*0x0400` → `0`, `0x3c01*0x03ff` → `0x0400` (after-rounding at threshold `0x387ff000`). - `check_conventions.py --base origin/main`; `git diff --check`. Not tested here: Windows, packed f16, ternary f16, directed `round16` ≠ 0. Sub and subrev are covered by the replay above, not by `agc_driver_float16_rounding`. ### 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 in [TechnicalDebt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/TechnicalDebt.md#silent-stubs) - [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] AI-assisted: yes
adriadam10 commented 2026-10-10 23:51:43 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-10T23:51:43Z.


Measured on RDNA2 (gfx1035) with the HW oracle: v_add/sub/subrev/mul_f16 over 10492 operand pairs in all four f16 denormal modes, at IEEE 0/1 and FP16_OVFL 0/1, match this PR's lowering bit for bit (NaN results compared as NaN only), and so do the 32 × 4 test vectors including the FP16_OVFL override and the VOP3 clamp forms. agc_driver_float16_rounding passes on current main and fails without the change. The macos-15-intel write-watch failure was a flake; it passes on rerun. Merging.

Optional follow-up: the test only runs add and mul, so a sub/subrev row set in ctest would keep those two covered.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6103483885 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2987#issuecomment-6103483885). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-10T23:51:43Z. --- Measured on RDNA2 (gfx1035) with the HW oracle: `v_add/sub/subrev/mul_f16` over 10492 operand pairs in all four f16 denormal modes, at IEEE 0/1 and FP16_OVFL 0/1, match this PR's lowering bit for bit (NaN results compared as NaN only), and so do the 32 × 4 test vectors including the FP16_OVFL override and the VOP3 `clamp` forms. `agc_driver_float16_rounding` passes on current main and fails without the change. The macos-15-intel write-watch failure was a flake; it passes on rerun. Merging. Optional follow-up: the test only runs add and mul, so a sub/subrev row set in ctest would keep those two covered.
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!2987
No description provided.