feat(shader): implement v_div_scale/fmas/fixup_f32 and s_denorm_mode (f32 division macro) #264

Merged
adriadam10 merged 2 commits from upstream-pr/264 into main 2026-10-04 22:41:38 +00:00
adriadam10 commented 2026-10-01 20:54:15 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-01T20:54:15Z.


What

Implements the three helpers of the precise f32 division macro and s_denorm_mode, which the macro uses. LLVM emits this for every a / b that is not fast-math, so these were a hard stop for ordinary shaders:

v_div_scale_f32 v4, s0, v3, v3, v2      ; scaled denominator
v_div_scale_f32 v6, vcc_lo, v2, v3, v2  ; scaled numerator, VCC = post-scale needed
v_rcp_f32 v5, v4
s_denorm_mode 15
... 5 fma steps (Newton-Raphson) ...
s_denorm_mode 12
v_div_fmas_f32 v4, v4, v5, v7           ; last fma, post-scaled when VCC
v_div_fixup_f32 v2, v4, v3, v2          ; 0/0, x/0, inf, NaN, underflow

KytyPS5, shadPS4 and fpPS4 don't implement them. The RDNA ISA pseudocode does not match the hardware, so I measured the behaviour on an RDNA2 GPU (Radeon 660M, gfx1035, same VALU as gfx1013) with raw kernels through the ROCm HSA runtime:

  • v_div_scale_f32: decided by the exponent fields only. The rules below matched the hardware on 12000/12000 random inputs with S0 ∈ {S1, S2}, which is how the macro uses it:
    • e2 - e1 >= 96: VCC=1, and the denominator call scales ×2^64.
    • Denominator denormal or zero: both calls scale ×2^64.
    • e1 >= 253: if e2 - e1 <= -96, VCC=1 and the denominator call scales ×2^-64; otherwise both calls scale ×2^-64.
    • e2 - e1 <= -96: VCC=1, and S0 scales ×2^64 unless it is the denominator.
    • e2 <= 24 (the ISA says 23): both calls scale ×2^64.
    • A zero numerator or denominator gives 0xffc00000; VCC still follows the rules above.
  • v_div_fmas_f32: with VCC, the scale is 2^64 when exponent(S2) >= 128 and 2^-64 otherwise (the ISA says a fixed 2^32). It is applied before the single rounding, so an fma that would overflow can still scale back into range. The translation multiplies the rounded fma by the power of two, which is exact when it stays normal. When it doesn't (scaling up from zero/denormal, or down from inf), it scales the factor that can't underflow or overflow instead.
  • v_div_fixup_f32: the ISA pseudocode, plus one case it misses: a NaN quotient with finite inputs gives ±inf. 8000/8000.
  • Whole macro on hardware: equals IEEE division with denormals flushed (DAZ/FTZ, from the trailing s_denorm_mode 12) on 20000/20000 inputs.

s_denorm_mode is accepted and ignored, because denormals follow the Vulkan float controls. It is listed under silent stubs in TechnicalDebt.

The VOP3B form of v_div_scale_f32 writes VCC through the SDST field like v_mad_u64_u32 and accepts neg. fmas/fixup take abs/neg; clamp and omod stay rejected.

Test

agc_driver_division, two hand-assembled shaders (words from llvm-mc -mcpu=gfx1013):

  • 256 vectors of (S0, S1, S2, VCC) → expected div_scale result and VCC, fmas and fixup, all captured on the RDNA2 GPU. They cover every scale branch plus inf/NaN/zero/max inputs. div_scale is checked only where S0 ∈ {S1, S2}.
  • The exact LLVM macro above (from llc -mcpu=gfx1013 on fdiv float) on 256 (a, b) pairs, with the quotient captured on the GPU: specials, exponent spreads ≥ 90, huge denominators, tiny numerators.

Like #263, fma fusion can't be required in Vulkan, and Ubuntu's lavapipe (CI) splits it. v_div_fmas_f32 and the macro depend on a fused fma, so the test first runs one v_fma_f32 on an input where fused and unfused differ. If the device splits it, those checks are skipped with a message; div_scale and div_fixup are exact and always checked.

Rows involving denormal inputs or results, and quotients in the denormal range, are left out, since their handling depends on the Vulkan driver's float controls.

Tested

Linux, full ctest (100) passes.

  • RADV (Radeon 660M) and lavapipe: the test passes.
  • Without the change it fails with "VOP3 opcode is not implemented".
  • On RADV the macro is one ulp off without #263. RADV contracted a v_mul_f32 and the following fma, so this PR is stacked on #263.

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: #251 (test harness), #263 (fma fusion, needed on RADV); review only the last commit
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4709479407 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/264). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-01T20:54:15Z. --- ### What Implements the three helpers of the precise f32 division macro and `s_denorm_mode`, which the macro uses. LLVM emits this for every `a / b` that is not fast-math, so these were a hard stop for ordinary shaders: ``` v_div_scale_f32 v4, s0, v3, v3, v2 ; scaled denominator v_div_scale_f32 v6, vcc_lo, v2, v3, v2 ; scaled numerator, VCC = post-scale needed v_rcp_f32 v5, v4 s_denorm_mode 15 ... 5 fma steps (Newton-Raphson) ... s_denorm_mode 12 v_div_fmas_f32 v4, v4, v5, v7 ; last fma, post-scaled when VCC v_div_fixup_f32 v2, v4, v3, v2 ; 0/0, x/0, inf, NaN, underflow ``` KytyPS5, shadPS4 and fpPS4 don't implement them. The RDNA ISA pseudocode does not match the hardware, so I measured the behaviour on an RDNA2 GPU (Radeon 660M, gfx1035, same VALU as gfx1013) with raw kernels through the ROCm HSA runtime: - **`v_div_scale_f32`**: decided by the exponent fields only. The rules below matched the hardware on 12000/12000 random inputs with S0 ∈ {S1, S2}, which is how the macro uses it: - `e2 - e1 >= 96`: VCC=1, and the denominator call scales ×2^64. - Denominator denormal or zero: both calls scale ×2^64. - `e1 >= 253`: if `e2 - e1 <= -96`, VCC=1 and the denominator call scales ×2^-64; otherwise both calls scale ×2^-64. - `e2 - e1 <= -96`: VCC=1, and S0 scales ×2^64 unless it is the denominator. - `e2 <= 24` (the ISA says 23): both calls scale ×2^64. - A zero numerator or denominator gives `0xffc00000`; VCC still follows the rules above. - **`v_div_fmas_f32`**: with VCC, the scale is 2^64 when exponent(S2) >= 128 and 2^-64 otherwise (the ISA says a fixed 2^32). It is applied before the single rounding, so an fma that would overflow can still scale back into range. The translation multiplies the rounded fma by the power of two, which is exact when it stays normal. When it doesn't (scaling up from zero/denormal, or down from inf), it scales the factor that can't underflow or overflow instead. - **`v_div_fixup_f32`**: the ISA pseudocode, plus one case it misses: a NaN quotient with finite inputs gives ±inf. 8000/8000. - **Whole macro on hardware**: equals IEEE division with denormals flushed (DAZ/FTZ, from the trailing `s_denorm_mode 12`) on 20000/20000 inputs. `s_denorm_mode` is accepted and ignored, because denormals follow the Vulkan float controls. It is listed under silent stubs in TechnicalDebt. The VOP3B form of `v_div_scale_f32` writes VCC through the SDST field like `v_mad_u64_u32` and accepts `neg`. `fmas`/`fixup` take abs/neg; clamp and omod stay rejected. ### Test `agc_driver_division`, two hand-assembled shaders (words from `llvm-mc -mcpu=gfx1013`): - 256 vectors of (S0, S1, S2, VCC) → expected `div_scale` result and VCC, `fmas` and `fixup`, **all captured on the RDNA2 GPU**. They cover every scale branch plus inf/NaN/zero/max inputs. `div_scale` is checked only where S0 ∈ {S1, S2}. - The exact LLVM macro above (from `llc -mcpu=gfx1013` on `fdiv float`) on 256 (a, b) pairs, with the quotient captured on the GPU: specials, exponent spreads ≥ 90, huge denominators, tiny numerators. Like #263, fma fusion can't be required in Vulkan, and Ubuntu's lavapipe (CI) splits it. `v_div_fmas_f32` and the macro depend on a fused fma, so the test first runs one `v_fma_f32` on an input where fused and unfused differ. If the device splits it, those checks are skipped with a message; `div_scale` and `div_fixup` are exact and always checked. Rows involving denormal inputs or results, and quotients in the denormal range, are left out, since their handling depends on the Vulkan driver's float controls. ### Tested Linux, full ctest (100) passes. - RADV (Radeon 660M) and lavapipe: the test passes. - Without the change it fails with "VOP3 opcode is not implemented". - On RADV the macro is one ulp off without #263. RADV contracted a `v_mul_f32` and the following fma, so this PR is stacked on #263. ### 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: #251 (test harness), #263 (fma fusion, needed on RADV); review only the last commit - [x] AI-assisted: yes
github-actions[bot] commented 2026-10-01 20:54:42 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-01T20:54:42Z.


Progress report (2cf901b → 1a55076)

📈 GPU shader instructions: 80.62% (+0.35%, +4 implemented instructions)

✅ 4 implemented
Encoding Name
SOPP S_DENORM_MODE
VOP3 V_DIV_FIXUP_F32
VOP3 V_DIV_FMAS_F32
VOP3 V_DIV_SCALE_F32
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5940347628 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/264#issuecomment-5940347628). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-01T20:54:42Z. --- <!-- progress-report --> ### Progress report (2cf901b → 1a55076) 📈 **GPU shader instructions**: 80.62% (+0.35%, +4 implemented instructions) <details> <summary>✅ 4 implemented</summary> | Encoding | Name | | - | - | | SOPP | `S_DENORM_MODE` | | VOP3 | `V_DIV_FIXUP_F32` | | VOP3 | `V_DIV_FMAS_F32` | | VOP3 | `V_DIV_SCALE_F32` | </details>
adriadam10 commented 2026-10-03 09:44:42 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-03T09:44:42Z.


Rebased on main. #263 is no longer needed as a base: its commit landed with #251, so #263 is closed and this PR is a single commit now. In the VOP3 modifier check, v_div_scale_f32's neg exception now sits next to main's VOPC float-compare exception.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5967900930 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/264#issuecomment-5967900930). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-03T09:44:42Z. --- Rebased on main. #263 is no longer needed as a base: its commit landed with #251, so #263 is closed and this PR is a single commit now. In the VOP3 modifier check, `v_div_scale_f32`'s neg exception now sits next to main's VOPC float-compare exception.
oneandonlydean commented 2026-10-04 05:39:24 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-04T05:39:24Z.


Thanks for measuring all of this on hardware; the div_scale thresholds and the NaN quotient case in div_fixup are really useful. One thing I noticed: v_div_scale_f32 scales a NaN source with an FPMul by 2^64 or 2^-64, so the NaN that comes back is whatever the driver returns. Your own vector at Division.cpp line 62 (S0 = S1 = 0x7f800001) expects the signalling NaN back unchanged, but an x86 lavapipe multiply gives 0x7fc00001, and the test still passes because Same() accepts any NaN for any NaN. v_div_fmas_f32 has the same issue through FPFma32. In #474 the f64 versions choose NaNs on the bits (div_scale returns a NaN source unchanged), so doing the same here, and comparing NaN results bit for bit in Division.cpp, would make the two consistent and exact. Separately, s_denorm_mode goes into the silent stubs list. CONTRIBUTING keeps silent stubs to UI-only behaviour that unblocks a title, and a denormal mode change alters results, so could it translate only the mode the recompiler already assumes as a no-op and throw for any other value, as on #333 and #436? Main's existing s_setreg_b32 no-op is better recorded under "Functional" in TechnicalDebt.md than under silent stubs.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5976999748 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/264#issuecomment-5976999748). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-04T05:39:24Z. --- Thanks for measuring all of this on hardware; the div_scale thresholds and the NaN quotient case in div_fixup are really useful. One thing I noticed: v_div_scale_f32 scales a NaN source with an FPMul by 2^64 or 2^-64, so the NaN that comes back is whatever the driver returns. Your own vector at Division.cpp line 62 (S0 = S1 = 0x7f800001) expects the signalling NaN back unchanged, but an x86 lavapipe multiply gives 0x7fc00001, and the test still passes because Same() accepts any NaN for any NaN. v_div_fmas_f32 has the same issue through FPFma32. In #474 the f64 versions choose NaNs on the bits (div_scale returns a NaN source unchanged), so doing the same here, and comparing NaN results bit for bit in Division.cpp, would make the two consistent and exact. Separately, s_denorm_mode goes into the silent stubs list. CONTRIBUTING keeps silent stubs to UI-only behaviour that unblocks a title, and a denormal mode change alters results, so could it translate only the mode the recompiler already assumes as a no-op and throw for any other value, as on #333 and #436? Main's existing s_setreg_b32 no-op is better recorded under "Functional" in TechnicalDebt.md than under silent stubs.
adriadam10 commented 2026-10-04 06:33:14 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-04T06:33:14Z.


Thanks, both points were right. Rebased onto main and changed:

  • v_div_scale_f32 returns a NaN source unchanged instead of multiplying it by 2^±64. v_div_fmas_f32 returns the first NaN input (a, b, c) quieted, or the default NaN 0xffc00000 when the fma itself is invalid, so it no longer depends on the driver's NaN either. This matches the f64 versions in #474 and the hardware rows.
  • Division.cpp now compares every result bit for bit (Same() is gone). Without the div_scale change, lavapipe fails on vector 13 (0x7fc00001, expected 0x7f800001). The default-NaN select only shows up on NVIDIA, because RADV and x86 lavapipe already produce 0xffc00000. Passes on RADV, local lavapipe and CI's lavapipe (Mesa 25.2.8).
  • s_denorm_mode: the recompiler doesn't set any float controls (no DenormPreserve/FlushToZero), so denormals follow the driver and no immediate matches what it models. It now throws for every value, and the silent stubs line is gone. In the macro test the two s_denorm_mode words are now s_nop 0. Translating the macro's 15/12 pair exactly would need DenormPreserve32 where the device supports it, which I'd rather do as a separate PR.
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5977345613 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/264#issuecomment-5977345613). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-04T06:33:14Z. --- Thanks, both points were right. Rebased onto main and changed: - `v_div_scale_f32` returns a NaN source unchanged instead of multiplying it by 2^±64. `v_div_fmas_f32` returns the first NaN input (a, b, c) quieted, or the default NaN 0xffc00000 when the fma itself is invalid, so it no longer depends on the driver's NaN either. This matches the f64 versions in #474 and the hardware rows. - Division.cpp now compares every result bit for bit (`Same()` is gone). Without the div_scale change, lavapipe fails on vector 13 (`0x7fc00001`, expected `0x7f800001`). The default-NaN select only shows up on NVIDIA, because RADV and x86 lavapipe already produce 0xffc00000. Passes on RADV, local lavapipe and CI's lavapipe (Mesa 25.2.8). - `s_denorm_mode`: the recompiler doesn't set any float controls (no DenormPreserve/FlushToZero), so denormals follow the driver and no immediate matches what it models. It now throws for every value, and the silent stubs line is gone. In the macro test the two `s_denorm_mode` words are now `s_nop 0`. Translating the macro's 15/12 pair exactly would need DenormPreserve32 where the device supports it, which I'd rather do as a separate PR.
adriadam10 commented 2026-10-04 10:06:20 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-04T10:06:20Z.


Rebased on current main (conflicts with the f64 ops of #446 and v_mad_i64_i32 of #435 in the opcode enum/classifier, the VOP3 table and the VOP3b sdst/float-modifier lists; both sides kept). Division passes on RADV, local lavapipe and CI's lavapipe; DivFixupF16 and Vop3MadI64 still pass.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5978843510 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/264#issuecomment-5978843510). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-04T10:06:20Z. --- Rebased on current main (conflicts with the f64 ops of #446 and `v_mad_i64_i32` of #435 in the opcode enum/classifier, the VOP3 table and the VOP3b sdst/float-modifier lists; both sides kept). Division passes on RADV, local lavapipe and CI's lavapipe; DivFixupF16 and Vop3MadI64 still pass.
oneandonlydean commented 2026-10-04 22:37:46 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-04T22:37:46Z.


Thanks, both points are fixed. v_div_scale_f32 now leaves a NaN source alone, so vector 13 gets 0x7f800001 back instead of whatever the driver's multiply makes of it, and v_div_fmas_f32 picks its NaN on the bits: the first NaN of a, b, c quieted, otherwise 0xffc00000 when the fma itself is invalid, the same rule as the f64 versions in #474. With Same() gone the test compares every result bit for bit. agc_driver_division passes on my NVIDIA machine, built on current main together with the other open shader PRs, and it fails on main's code (the decoder refuses the opcodes).

s_denorm_mode throwing for every value is fine with me: main didn't decode it at all, so nothing that worked before stops working, and you're right that no immediate matches what the recompiler models while it sets no float controls. DenormPreserve as a separate PR sounds good.

One thing for whoever lands second: this and #436 both add a SOPP opcode next to each other (0x25 here, 0x24 there), so the second one needs a one-line rebase in RdnaScalarOpDecoder.cpp and ScalarInstructions.cpp. These are new opcodes that main refused, so it's ready to merge from my side.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5985217285 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/264#issuecomment-5985217285). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-04T22:37:46Z. --- Thanks, both points are fixed. `v_div_scale_f32` now leaves a NaN source alone, so vector 13 gets 0x7f800001 back instead of whatever the driver's multiply makes of it, and `v_div_fmas_f32` picks its NaN on the bits: the first NaN of a, b, c quieted, otherwise 0xffc00000 when the fma itself is invalid, the same rule as the f64 versions in #474. With `Same()` gone the test compares every result bit for bit. `agc_driver_division` passes on my NVIDIA machine, built on current main together with the other open shader PRs, and it fails on main's code (the decoder refuses the opcodes). `s_denorm_mode` throwing for every value is fine with me: main didn't decode it at all, so nothing that worked before stops working, and you're right that no immediate matches what the recompiler models while it sets no float controls. DenormPreserve as a separate PR sounds good. One thing for whoever lands second: this and #436 both add a SOPP opcode next to each other (0x25 here, 0x24 there), so the second one needs a one-line rebase in `RdnaScalarOpDecoder.cpp` and `ScalarInstructions.cpp`. These are new opcodes that main refused, so it's ready to merge from my side.
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!264
No description provided.