fix(shader): flush v_ldexp_f16 denormals per the f16 denormal mode #3040

Merged
CouchRiv merged 3 commits from upstream-pr/3040 into main 2026-10-11 08:48:15 +00:00
CouchRiv commented 2026-10-10 20:55:31 +00:00 (Migrated from github.com)

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


What

AGC graphics: f16 ldexp denormal: denorm16 0 vector 0 (0x00000401, 0xffffffff) is 0xabcd0200, expected 0xabcd0000

On main, v_ldexp_f16 ignores FLOAT_MODE f16 denormal bits and keeps denormals the hardware flushes. It now flushes a denormal half input to its signed zero when denorm16 bit 0 is clear, and a denormal half result when bit 1 is clear. NaN and infinity handling and destination high-half preservation are unchanged. denorm16 = 3 and shaders without a known float mode stay as on main. Directed round16 is not changed here.

Shares the test file Float16Rounding.cpp with #2987 and #3012; I rebase whichever lands later.

Tested

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

  • agc_driver_float16_rounding fails on main with the line above and passes on the branch on NVIDIA (0.23 s), RADV (0.09 s) and lavapipe (0.15 s).
  • Replay of all 984 v_ldexp_f16_controls rows (denorm16 = 0/1/2 × 328) against gfx1036: 82 mismatches on main → 0 on the branch on each of NVIDIA, RADV and lavapipe; 0 regressions.
  • check_conventions.py --base origin/main; git diff --check.

Not tested here: Windows, directed round16 ≠ 0, packed or ternary f16.

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:4814374401 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3040). Originally posted by [CouchRiv](https://github.com/CouchRiv) on 2026-10-10T20:55:31Z. --- ### What ```text AGC graphics: f16 ldexp denormal: denorm16 0 vector 0 (0x00000401, 0xffffffff) is 0xabcd0200, expected 0xabcd0000 ``` On `main`, `v_ldexp_f16` ignores `FLOAT_MODE` f16 denormal bits and keeps denormals the hardware flushes. It now flushes a denormal half input to its signed zero when denorm16 bit 0 is clear, and a denormal half result when bit 1 is clear. NaN and infinity handling and destination high-half preservation are unchanged. `denorm16 = 3` and shaders without a known float mode stay as on `main`. Directed `round16` is not changed here. Shares the test file `Float16Rounding.cpp` with #2987 and #3012; I rebase whichever lands later. ### Tested CachyOS Linux; RTX 4070 Ti (NVIDIA), Radeon 610M (RADV / gfx1036 hw-oracle), lavapipe (CI ICD). - `agc_driver_float16_rounding` fails on main with the line above and passes on the branch on NVIDIA (0.23 s), RADV (0.09 s) and lavapipe (0.15 s). - Replay of all 984 `v_ldexp_f16_controls` rows (denorm16 = 0/1/2 × 328) against gfx1036: 82 mismatches on main → 0 on the branch on each of NVIDIA, RADV and lavapipe; 0 regressions. - `check_conventions.py --base origin/main`; `git diff --check`. Not tested here: Windows, directed `round16` ≠ 0, packed or ternary f16. ### 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](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
Dextroid17 commented 2026-10-10 22:23:05 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Dextroid17 on 2026-10-10T22:23:05Z.

Review: COMMENTED


Six PRs landed as one series in a few hours, so I read them as one series rather than six times — and the part worth saying is that the shared premise holds up.

The premise, checked in the tree. Every change in the series reads the shader's FLOAT_MODE through the model that is already here: the f32 denormal field in bits 5:4, the f16 field in bits 7:6, IEEE_MODE, and the rounding field in bits 1:0. That model is load-bearing already — AttributeInstructions.cpp derives the interpolation flush flags from the same two fields (and refuses the combination it has not measured), OperandAccess.cpp gates the output modifier on the field's high bit, and the quieting the NaN vectors expect (0x00400000 for f32, 0x0200 for f16) is exactly the constants OperandAccess.cpp already uses. Nothing in the series invents a second encoding, and every PR carries its own test under tests/execution/ — collected automatically, so there is no build-list churn — with debt entries on most of them.

One thing I would write down, because your own measurement implies it. This PR's debt entry reads the f16 field per bit — bit 0 preserves the half input, bit 1 the half result. The f32 field is treated differently: VectorInstructions.cpp refuses f32 denormal fields 1 and 2 as not implemented. That is two shapes for the same register, and this series is the first place both are visible at once. If the f32 field has the same per-bit structure, then that existing refusal is refusing states that are legal, and the same gfx1036 oracle that settled f16 could settle it the same way. Worth a line in the entry whichever way it falls.

One triage note for whoever merges these, since they are a series and not a cluster. The overlap is wider than any single diff suggests: FloatInstructions.cpp is touched by four of the six, docs/dev/TechnicalDebt.md by four, Division.cpp by two, and Float16Rounding.cpp by two. Merged in order, each stays the small diff it appears to be; out of order, expect conflicts in exactly those files — and expect the debt file to conflict on nearly every rebase. The v_log_f32 change is not part of the series at all — a different author, a different mechanism, a numerics correction against the driver's log2 near 1.0. Reading it as one of these would misjudge it.

Measured on silicon is the bar this tree sets for instruction semantics, and the series clears it. Written with AI assistance.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5481017282 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3040#pullrequestreview-5481017282). Originally posted by [Dextroid17](https://github.com/Dextroid17) on 2026-10-10T22:23:05Z. **Review:** COMMENTED --- Six PRs landed as one series in a few hours, so I read them as one series rather than six times — and the part worth saying is that the shared premise holds up. **The premise, checked in the tree.** Every change in the series reads the shader's `FLOAT_MODE` through the model that is already here: the f32 denormal field in bits 5:4, the f16 field in bits 7:6, `IEEE_MODE`, and the rounding field in bits 1:0. That model is load-bearing already — `AttributeInstructions.cpp` derives the interpolation flush flags from the same two fields (and refuses the combination it has not measured), `OperandAccess.cpp` gates the output modifier on the field's high bit, and the quieting the NaN vectors expect (`0x00400000` for f32, `0x0200` for f16) is exactly the constants `OperandAccess.cpp` already uses. Nothing in the series invents a second encoding, and every PR carries its own test under `tests/execution/` — collected automatically, so there is no build-list churn — with debt entries on most of them. **One thing I would write down, because your own measurement implies it.** This PR's debt entry reads the f16 field per bit — bit 0 preserves the half input, bit 1 the half result. The f32 field is treated differently: `VectorInstructions.cpp` refuses f32 denormal fields 1 and 2 as not implemented. That is two shapes for the same register, and this series is the first place both are visible at once. If the f32 field has the same per-bit structure, then that existing refusal is refusing states that are legal, and the same gfx1036 oracle that settled f16 could settle it the same way. Worth a line in the entry whichever way it falls. **One triage note for whoever merges these, since they are a series and not a cluster.** The overlap is wider than any single diff suggests: `FloatInstructions.cpp` is touched by four of the six, `docs/dev/TechnicalDebt.md` by four, `Division.cpp` by two, and `Float16Rounding.cpp` by two. Merged in order, each stays the small diff it appears to be; out of order, expect conflicts in exactly those files — and expect the debt file to conflict on nearly every rebase. The `v_log_f32` change is not part of the series at all — a different author, a different mechanism, a numerics correction against the driver's `log2` near 1.0. Reading it as one of these would misjudge it. Measured on silicon is the bar this tree sets for instruction semantics, and the series clears it. Written with AI assistance.
oneandonlydean commented 2026-10-11 01:08:33 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-11T01:08:33Z.


Same as #3012: #2987 is in, please rebase (Float16Rounding.cpp, TechnicalDebt).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6104054296 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3040#issuecomment-6104054296). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-11T01:08:33Z. --- Same as #3012: #2987 is in, please rebase (`Float16Rounding.cpp`, TechnicalDebt).
Silverash-i8 commented 2026-10-11 02:16:26 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Silverash-i8 on 2026-10-11T02:16:26Z.


Windows run of the rebased head 3de384f2 (the PR lists Windows as not tested): Windows 11, WinLibs GCC 15.2.0 r7, Release, merged onto main 84cc975e alone (it still conflicts with #3012 in vLdexpF16), RTX 5060 Laptop and the Arrow Lake iGPU (ANYPS5_GPU=Intel).

  • agc_driver_float16_rounding passes on both GPUs, as do the other 13 f16 and half tests. The Intel iGPU is worth having here since its f16 denormal handling differs (#2822), and the new flush cases pass on it.

AI-assisted (Claude Code); I ran this on my machine.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6104536053 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3040#issuecomment-6104536053). Originally posted by [Silverash-i8](https://github.com/Silverash-i8) on 2026-10-11T02:16:26Z. --- Windows run of the rebased head `3de384f2` (the PR lists Windows as not tested): Windows 11, WinLibs GCC 15.2.0 r7, Release, merged onto `main` 84cc975e alone (it still conflicts with #3012 in `vLdexpF16`), RTX 5060 Laptop and the Arrow Lake iGPU (`ANYPS5_GPU=Intel`). - `agc_driver_float16_rounding` passes on both GPUs, as do the other 13 f16 and half tests. The Intel iGPU is worth having here since its f16 denormal handling differs (#2822), and the new flush cases pass on it. AI-assisted (Claude Code); I ran this on my machine.
adriadam10 commented 2026-10-11 07:06:08 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-11T07:06:08Z.

Review: CHANGES_REQUESTED


Rebase looks fine and the test fails without the change, but I measured v_ldexp_f16 on RDNA2 (gfx1035) over 544 operand rows × denorm16 0..3 × IEEE 0/1 and found one mismatch class:

  • FloatInstructions.cpp:342-345: with denorm16 bit 1 clear (modes 0 and 1), the result is flushed after ConvertF16F32 rounds it. The hardware flushes when the exact result is below the minimum normal, before rounding: v_ldexp_f16 0x07ff, -1 (and 0x87ff) returns ±0 on HW, the PR returns 0x0400/0x8400 (0x03ff.8 rounds up to the minimum normal). Modes 2 and 3 match.
  • Fix: decide the flush on the unrounded scaled value (e.g. test the f32 product's magnitude against 0x38800000, as the multiply path in #2987 does for its tininess check) instead of the rounded half, and add {0x000007ff, 0xffffffff} / {0x000087ff, 0xffffffff} rows to LdexpDenormalVectors (expected ±0 for denorm16 0/1, 0x0400/0x8400 for 2/3).

Everything else matched the hardware bit for bit (input flush, signed zeros, inf, overflow clamp, exponent clamp). Please keep both vLdexpF16 changes when #3012 lands, as already discussed.

Also, the branch now carries a merge commit (78e448c13); please rebase onto main instead of merging it in.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5482275400 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3040#pullrequestreview-5482275400). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-11T07:06:08Z. **Review:** CHANGES_REQUESTED --- Rebase looks fine and the test fails without the change, but I measured `v_ldexp_f16` on RDNA2 (gfx1035) over 544 operand rows × denorm16 0..3 × IEEE 0/1 and found one mismatch class: - `FloatInstructions.cpp:342-345`: with denorm16 bit 1 clear (modes 0 and 1), the result is flushed after `ConvertF16F32` rounds it. The hardware flushes when the exact result is below the minimum normal, before rounding: `v_ldexp_f16 0x07ff, -1` (and `0x87ff`) returns `±0` on HW, the PR returns `0x0400`/`0x8400` (0x03ff.8 rounds up to the minimum normal). Modes 2 and 3 match. - Fix: decide the flush on the unrounded scaled value (e.g. test the f32 product's magnitude against `0x38800000`, as the multiply path in #2987 does for its tininess check) instead of the rounded half, and add `{0x000007ff, 0xffffffff}` / `{0x000087ff, 0xffffffff}` rows to `LdexpDenormalVectors` (expected `±0` for denorm16 0/1, `0x0400`/`0x8400` for 2/3). Everything else matched the hardware bit for bit (input flush, signed zeros, inf, overflow clamp, exponent clamp). Please keep both `vLdexpF16` changes when #3012 lands, as already discussed. Also, the branch now carries a merge commit (`78e448c13`); please rebase onto `main` instead of merging it in.
CouchRiv commented 2026-10-11 08:37:27 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by CouchRiv on 2026-10-11T08:37:27Z.


Thanks, you were right. Rebased onto current main (7b5214d42, no merge commit); the result flush is now decided on the unrounded product.

  • FloatInstructions.cpp:342-345: with denorm16 bit 1 clear, the f32 product's magnitude is compared against 0x38800000 before ConvertF16F32 rounds it, and a smaller value gives the signed zero. The input flush (:329-332) and the directed rounding from #3012 are unchanged.
  • Float16Rounding.cpp:745-746: {0x000007ff, 0xffffffff} and {0x000087ff, 0xffffffff} added to LdexpDenormalVectors: ±0 for denorm16 0 and 1, 0x0400/0x8400 for 2 and 3.

Tested (CachyOS, RTX 4070 Ti, Radeon 610M RADV, lavapipe):

  • agc_driver_float16_rounding: passes on NVIDIA, RADV and lavapipe; it fails against main's libraries (f16 ldexp denormal: denorm16 0 vector 0 (0x00000401, 0xffffffff) is 0xabcd0200, expected 0xabcd0000).
  • Replay against gfx1036, every half magnitude 0x0000..0x0fff of both signs with exponents -1 and 0 (16384 rows), denorm16 0..3, IEEE 0 and 1: 0 mismatches on the branch on NVIDIA, RADV and lavapipe, including both new rows. Main differs on 6138 rows for modes 0 and 1 and 4090 for mode 2, including those two rows (0x07ff, -1 gives 0x0400 on main, 0x0000 on hardware). The earlier 328-row grid also has 0 mismatches at round16 0.
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6107199943 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3040#issuecomment-6107199943). Originally posted by [CouchRiv](https://github.com/CouchRiv) on 2026-10-11T08:37:27Z. --- Thanks, you were right. Rebased onto current `main` (`7b5214d42`, no merge commit); the result flush is now decided on the unrounded product. - `FloatInstructions.cpp:342-345`: with denorm16 bit 1 clear, the f32 product's magnitude is compared against `0x38800000` before `ConvertF16F32` rounds it, and a smaller value gives the signed zero. The input flush (`:329-332`) and the directed rounding from #3012 are unchanged. - `Float16Rounding.cpp:745-746`: `{0x000007ff, 0xffffffff}` and `{0x000087ff, 0xffffffff}` added to `LdexpDenormalVectors`: `±0` for denorm16 0 and 1, `0x0400`/`0x8400` for 2 and 3. Tested (CachyOS, RTX 4070 Ti, Radeon 610M RADV, lavapipe): - `agc_driver_float16_rounding`: passes on NVIDIA, RADV and lavapipe; it fails against main's libraries (`f16 ldexp denormal: denorm16 0 vector 0 (0x00000401, 0xffffffff) is 0xabcd0200, expected 0xabcd0000`). - Replay against gfx1036, every half magnitude `0x0000..0x0fff` of both signs with exponents -1 and 0 (16384 rows), denorm16 0..3, IEEE 0 and 1: 0 mismatches on the branch on NVIDIA, RADV and lavapipe, including both new rows. Main differs on 6138 rows for modes 0 and 1 and 4090 for mode 2, including those two rows (`0x07ff, -1` gives `0x0400` on main, `0x0000` on hardware). The earlier 328-row grid also has 0 mismatches at round16 0.
Silverash-i8 commented 2026-10-11 08:46:07 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Silverash-i8 on 2026-10-11T08:46:07Z.


Reran the updated head 327bf794 on Windows (merged onto current main, WinLibs GCC 15.2.0 r7, Release): agc_driver_float16_rounding with the two new LdexpDenormalVectors rows passes on both the RTX 5060 and the Arrow Lake iGPU, along with the other 14 f16/half tests on each.

AI-assisted (Claude Code); I ran this on my machine.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6107264710 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3040#issuecomment-6107264710). Originally posted by [Silverash-i8](https://github.com/Silverash-i8) on 2026-10-11T08:46:07Z. --- Reran the updated head `327bf794` on Windows (merged onto current `main`, WinLibs GCC 15.2.0 r7, Release): `agc_driver_float16_rounding` with the two new `LdexpDenormalVectors` rows passes on both the RTX 5060 and the Arrow Lake iGPU, along with the other 14 f16/half tests on each. AI-assisted (Claude Code); I ran this on my machine.
adriadam10 commented 2026-10-11 08:48:11 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-11T08:48:11Z.

Review: APPROVED


Thanks for the fix. Re-measured v_ldexp_f16 on RDNA2 (gfx1035): every half value × 16 exponents (incl. ±65 clamp and high exponent bits) × denorm16 0..3 × IEEE 0/1 matches this lowering bit for bit (only sNaN payloads at IEEE=0 differ, unchanged from main). agc_driver_float16_rounding passes on RADV and fails without the change. Merging.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5482619854 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3040#pullrequestreview-5482619854). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-11T08:48:11Z. **Review:** APPROVED --- Thanks for the fix. Re-measured `v_ldexp_f16` on RDNA2 (gfx1035): every half value × 16 exponents (incl. ±65 clamp and high exponent bits) × denorm16 0..3 × IEEE 0/1 matches this lowering bit for bit (only sNaN payloads at IEEE=0 differ, unchanged from `main`). `agc_driver_float16_rounding` passes on RADV and fails without the change. Merging.
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!3040
No description provided.