feat(shader): follow FP16_OVFL in every f16 result #2025

Merged
snoozedDev merged 1 commit from upstream-pr/2025 into main 2026-10-10 07:44:37 +00:00
snoozedDev commented 2026-10-09 05:16:05 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by snoozedDev on 2026-10-09T05:16:05Z.


What

Part of #981. FP16_OVFL was decoded into GuestContext::floatMode, serialized and part of the cache key (#1816), but nothing read it: every f16 result overflowed to infinity. Translation now follows floatMode.fp16Overflow.

Measured on RDNA2 (gfx1036, a Ryzen 7 7700X iGPU, not the PS5's gfx1013) with the HW oracle, 32 f16 instruction forms (add, sub, mul, fma, fmac, div_fixup, med3, max, pk_mul, pk_add, pk_fma, fma_mixlo, mul with clamp, mul with mul:2, add through SDWA to WORD_1, fma with clamp, exp, log, rcp, rsq, sqrt, sin, cos, ldexp, cvt_f16_f32, cvt_f16_u16, cvt_f16_i16, cvt_pkrtz_f16_f32, rcp with mul:2, exp with clamp, frexp_mant, floor) over 32 rows each, at IEEE=0 and IEEE=1 with f16 denormals kept, and at IEEE=0 with them flushed, DX10_CLAMP=1, f32 denormals flushed, round to nearest even, FP16_OVFL=0 and =1 (--ieee I --dx10-clamp 1 --denorm32 0 --denorm16 D --round32 0 --round16 0 --fp16-overflow O):

  • FP16_OVFL=1: an f16 result that would round to ±infinity is written as ±0x7bff, in every form above, unless a source operand is infinite, in which case the infinity passes through (inf + 1, inf * 2, fma(65504, 2, -inf), ldexp(inf, 1), exp(inf), cvt_f16_f32(inf)). A zero source is not exempt: v_rcp_f16, v_rsq_f16 and v_log_f16 of ±0 give ±0x7bff, and so do v_exp_f16 of 16 or more and v_rcp_f16 of 2^-16, whose f32 intermediate overflows or not. The threshold is the nearest-even one: 65504 + 16 (a tie) overflows, 65504 + 15.99 does not. The sign is kept (-65504 - 16 gives 0xfbff).
  • v_div_fixup_f16 keeps the infinity it writes for a zero denominator or an infinite numerator and clamps any other infinite quotient, including the one it writes for a NaN quotient.
  • v_cvt_pkrtz_f16_f32 never overflows and is unchanged. NaN results, the clamp modifier and DX10_CLAMP are unaffected; with mul:2 applied (f16 denormals flushed, IEEE=0) the doubled result is clamped the same way.
  • IEEE_MODE and the f16 denormal mode do not change the rule: the IEEE=1 runs differ from the IEEE=0 ones only in signalling NaN quieting, the flushed runs only in the flushed denormals.

Changes:

  • clampF16Overflow applies the rule to an f32 value before it is converted to f16, given the source values whose infinity exempts the result. writeF16 takes those sources, so the scalar binary, ternary, mix, SDWA, convert and v_interp_p2_f16 paths get it, as do the packed lanes, the unary instructions, v_ldexp_f16 and v_div_fixup_f16 on its bits.
  • A shader without a known float mode, and FP16_OVFL=0, keep the previous lowering.
  • HW_ORACLE.md: the fp16_overflow row said division by zero still produces infinity; the measurement above says v_rcp_f16 of zero is clamped and only v_div_fixup_f16 keeps it. TechnicalDebt: the float mode entry lists FP16_OVFL as read, and a new entry records the rule and what it leaves open (v_fma_mix*_f16 with f32 sources and v_interp_p2_f16 with infinite inputs follow the rule unmeasured; round modes other than nearest even are not modelled).

Raw data (bodies, rows and the results in all six modes): https://gist.github.com/snoozedDev/9174fac268859aba2e9ff790b5dabe6b

Tested

Arch Linux (kernel 7.2.5), GCC 16.2.1, Release, RTX 3090 (NVIDIA 610.57.04) and the RADV iGPU (gfx1036, Mesa 26.2.2), main f1451396.

  • New agc_driver_fp16_overflow: 16 instructions (add, sub, mul, fma, div_fixup, pk_mul, pk_add, fma_mixlo, add through SDWA, exp, log, rcp, rsq, ldexp, cvt_f16_f32, cvt_f16_u16) × 32 rows captured on gfx1036 with FP16_OVFL=0 and =1, IEEE=0, FLOAT_MODE 0xc0, DX10_CLAMP=1; 109 of the 512 cells differ between the two settings. The test runs with no mode and with FP16_OVFL=0 (checked against the first table) and with FP16_OVFL=1 (the second). Where the NaN bits come from a driver op any NaN is accepted; where translation chooses them (v_div_fixup_f16, packed, v_ldexp_f16) they are compared bit for bit. Passes on both GPUs; with main's translation it fails on both at lane 0 FP16_OVFL=1 v_add_f16 is 0x00007c00, expected 0x00007bff.
  • Full Release ctest: 498/498 on the RTX 3090, and the 24 f16, modifier, SDWA, packed and division tests on RADV.
  • Not run on lavapipe.

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 (Claude Code, Claude Fable 5.1; the measurements are the hardware's output)

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4796307479 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2025). Originally posted by [snoozedDev](https://github.com/snoozedDev) on 2026-10-09T05:16:05Z. --- ### What Part of #981. `FP16_OVFL` was decoded into `GuestContext::floatMode`, serialized and part of the cache key (#1816), but nothing read it: every f16 result overflowed to infinity. Translation now follows `floatMode.fp16Overflow`. Measured on RDNA2 (gfx1036, a Ryzen 7 7700X iGPU, not the PS5's gfx1013) with the HW oracle, 32 f16 instruction forms (add, sub, mul, fma, fmac, div_fixup, med3, max, pk_mul, pk_add, pk_fma, fma_mixlo, mul with clamp, mul with `mul:2`, add through SDWA to `WORD_1`, fma with clamp, exp, log, rcp, rsq, sqrt, sin, cos, ldexp, cvt_f16_f32, cvt_f16_u16, cvt_f16_i16, cvt_pkrtz_f16_f32, rcp with `mul:2`, exp with clamp, frexp_mant, floor) over 32 rows each, at IEEE=0 and IEEE=1 with f16 denormals kept, and at IEEE=0 with them flushed, `DX10_CLAMP`=1, f32 denormals flushed, round to nearest even, `FP16_OVFL`=0 and =1 (`--ieee I --dx10-clamp 1 --denorm32 0 --denorm16 D --round32 0 --round16 0 --fp16-overflow O`): - `FP16_OVFL`=1: an f16 result that would round to ±infinity is written as ±0x7bff, in every form above, unless a source operand is infinite, in which case the infinity passes through (`inf + 1`, `inf * 2`, `fma(65504, 2, -inf)`, `ldexp(inf, 1)`, `exp(inf)`, `cvt_f16_f32(inf)`). A zero source is not exempt: `v_rcp_f16`, `v_rsq_f16` and `v_log_f16` of ±0 give ±0x7bff, and so do `v_exp_f16` of 16 or more and `v_rcp_f16` of 2^-16, whose f32 intermediate overflows or not. The threshold is the nearest-even one: 65504 + 16 (a tie) overflows, 65504 + 15.99 does not. The sign is kept (`-65504 - 16` gives 0xfbff). - `v_div_fixup_f16` keeps the infinity it writes for a zero denominator or an infinite numerator and clamps any other infinite quotient, including the one it writes for a NaN quotient. - `v_cvt_pkrtz_f16_f32` never overflows and is unchanged. NaN results, the clamp modifier and `DX10_CLAMP` are unaffected; with `mul:2` applied (f16 denormals flushed, IEEE=0) the doubled result is clamped the same way. - `IEEE_MODE` and the f16 denormal mode do not change the rule: the IEEE=1 runs differ from the IEEE=0 ones only in signalling NaN quieting, the flushed runs only in the flushed denormals. Changes: - `clampF16Overflow` applies the rule to an f32 value before it is converted to f16, given the source values whose infinity exempts the result. `writeF16` takes those sources, so the scalar binary, ternary, mix, SDWA, convert and `v_interp_p2_f16` paths get it, as do the packed lanes, the unary instructions, `v_ldexp_f16` and `v_div_fixup_f16` on its bits. - A shader without a known float mode, and `FP16_OVFL`=0, keep the previous lowering. - `HW_ORACLE.md`: the `fp16_overflow` row said division by zero still produces infinity; the measurement above says `v_rcp_f16` of zero is clamped and only `v_div_fixup_f16` keeps it. TechnicalDebt: the float mode entry lists `FP16_OVFL` as read, and a new entry records the rule and what it leaves open (`v_fma_mix*_f16` with f32 sources and `v_interp_p2_f16` with infinite inputs follow the rule unmeasured; round modes other than nearest even are not modelled). Raw data (bodies, rows and the results in all six modes): https://gist.github.com/snoozedDev/9174fac268859aba2e9ff790b5dabe6b ### Tested Arch Linux (kernel 7.2.5), GCC 16.2.1, Release, RTX 3090 (NVIDIA 610.57.04) and the RADV iGPU (gfx1036, Mesa 26.2.2), `main` f1451396. - New `agc_driver_fp16_overflow`: 16 instructions (add, sub, mul, fma, div_fixup, pk_mul, pk_add, fma_mixlo, add through SDWA, exp, log, rcp, rsq, ldexp, cvt_f16_f32, cvt_f16_u16) × 32 rows captured on gfx1036 with `FP16_OVFL`=0 and =1, IEEE=0, `FLOAT_MODE` 0xc0, `DX10_CLAMP`=1; 109 of the 512 cells differ between the two settings. The test runs with no mode and with `FP16_OVFL`=0 (checked against the first table) and with `FP16_OVFL`=1 (the second). Where the NaN bits come from a driver op any NaN is accepted; where translation chooses them (`v_div_fixup_f16`, packed, `v_ldexp_f16`) they are compared bit for bit. Passes on both GPUs; with `main`'s translation it fails on both at `lane 0 FP16_OVFL=1 v_add_f16 is 0x00007c00, expected 0x00007bff`. - Full Release ctest: 498/498 on the RTX 3090, and the 24 f16, modifier, SDWA, packed and division tests on RADV. - Not run on lavapipe. ### 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 (Claude Code, Claude Fable 5.1; the measurements are the hardware's output) 🤖 Generated with [Claude Code](https://claude.com/claude-code)
popcoder565 commented 2026-10-09 19:43:20 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by popcoder565 on 2026-10-09T19:43:20Z.


Native Windows run, since no Windows results were posted. Setup: Windows 10 (19045), RTX 3060 Laptop (NVIDIA 616.92), MinGW-w64 GCC 15.2.0 (WinLibs r7), Release. Head 0d1e1e08, merged onto main 8b393b6e in one build with 31 other PRs that had no Windows run.

  • This PR's tests pass: agc_driver_fp16_overflow.
  • Full ctest in that build: 520 of 521. The one failure is guest_offline_imports, broken by #2359 (reported there).

No title was run.

🤖 Addressed by Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6088031824 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2025#issuecomment-6088031824). Originally posted by [popcoder565](https://github.com/popcoder565) on 2026-10-09T19:43:20Z. --- Native Windows run, since no Windows results were posted. Setup: Windows 10 (19045), RTX 3060 Laptop (NVIDIA 616.92), MinGW-w64 GCC 15.2.0 (WinLibs r7), Release. Head `0d1e1e08`, merged onto `main` 8b393b6e in one build with 31 other PRs that had no Windows run. - This PR's tests pass: `agc_driver_fp16_overflow`. - Full ctest in that build: 520 of 521. The one failure is `guest_offline_imports`, broken by #2359 (reported there). No title was run. _🤖 Addressed by [Claude Code](https://claude.com/claude-code)_
adriadam10 commented 2026-10-10 07:06:41 +00:00 (Migrated from github.com)

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


Rebased onto current main: #3a9da568f changed float16Ternary (v_fma_mixlo/mixhi_f16 now go through FPInterpolateF16), so the conflict kept that lowering and passes the source operands to writeF16 there too, so the FP16_OVFL exemption for infinite sources applies. No other change.

Rechecked on the rebased tree: agc_driver_fp16_overflow passes on RADV (it covers v_fma_mixlo_f16) and fails without the translation change; the f16 fma, div_fixup and mix_precision tests pass. Thanks @popcoder565 for the Windows run on the previous head. Merging once the full Linux/Windows build is green on the rebased head.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6094953087 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2025#issuecomment-6094953087). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-10T07:06:41Z. --- Rebased onto current `main`: #3a9da568f changed `float16Ternary` (`v_fma_mixlo/mixhi_f16` now go through `FPInterpolateF16`), so the conflict kept that lowering and passes the source operands to `writeF16` there too, so the FP16_OVFL exemption for infinite sources applies. No other change. Rechecked on the rebased tree: `agc_driver_fp16_overflow` passes on RADV (it covers `v_fma_mixlo_f16`) and fails without the translation change; the f16 fma, div_fixup and mix_precision tests pass. Thanks @popcoder565 for the Windows run on the previous head. Merging once the full Linux/Windows build is green on the rebased head.
adriadam10 commented 2026-10-10 07:44:25 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-10T07:44:25Z.


Rechecked on the current head (0bea402a1, rebased onto main e8b47b927): agc_driver_fp16_overflow passes on RADV and fails without the translation change; the 326 tests of libSceAgc/libSceAgcDriver pass. The f16 semantics are unchanged from what was measured (including the v_fma_mix* path through FPInterpolateF16). Thanks @popcoder565 for the Windows run. Merging once the full Linux/Windows build is green.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6095245233 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2025#issuecomment-6095245233). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-10T07:44:25Z. --- Rechecked on the current head (0bea402a1, rebased onto `main` e8b47b927): `agc_driver_fp16_overflow` passes on RADV and fails without the translation change; the 326 tests of libSceAgc/libSceAgcDriver pass. The f16 semantics are unchanged from what was measured (including the `v_fma_mix*` path through `FPInterpolateF16`). Thanks @popcoder565 for the Windows run. Merging once the full Linux/Windows build is green.
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!2025
No description provided.