Skip to content

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

Merged
oneandonlydean merged 2 commits into
boykopovar:mainfrom
adriadam10:feat/div-scale-fmas-fixup
Oct 4, 2026
Merged

oneandonlydean merged 2 commits into
boykopovar:mainfrom
adriadam10:feat/div-scale-fmas-fixup

Conversation

@adriadam10

@adriadam10 adriadam10 commented Oct 1, 2026 •

Copy link
Copy Markdown
Collaborator

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.

Checklist

@github-actions

github-actions Bot commented Oct 1, 2026 •

Copy link
Copy Markdown

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

@adriadam10
adriadam10 force-pushed the feat/div-scale-fmas-fixup branch 2 times, most recently from 6b9e06d to d4498b6 Compare October 1, 2026 21:00
@github-actions github-actions Bot added build shader core/shader docs agc libSceAgc* ci .github labels Oct 2, 2026
This was referenced Oct 2, 2026
@adriadam10

Copy link
Copy Markdown
Collaborator Author

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.

@github-actions github-actions Bot removed the needs rebase Conflicts with main, rebase needed label Oct 3, 2026
@oneandonlydean

Copy link
Copy Markdown
Collaborator

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

Copy link
Copy Markdown
Collaborator Author

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 feat(shader): implement v_div_scale_f64, v_div_fmas_f64 and v_div_fixup_f64 #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
adriadam10 force-pushed the feat/div-scale-fmas-fixup branch from de02e86 to 87151db Compare October 4, 2026 06:33
@github-actions github-actions Bot added the needs rebase Conflicts with main, rebase needed label Oct 4, 2026
adriadam10 and others added 2 commits October 4, 2026 12:05
…f32 and s_denorm_mode

These make up the precise f32 division macro compilers emit for a / b. No
reference emulator implements them and the ISA pseudocode disagrees with the
hardware, so the rules were measured on an RDNA2 GPU:
- div_scale decides from the exponent fields only: 2^64 or 2^-64 scaling
  and VCC for differences >= 96 or <= -96, a denormal or huge (>= 253)
  denominator and a numerator exponent <= 24; a zero input gives 0xffc00000.
- div_fmas scales by 2^64 when the exponent of S2 is >= 128, else 2^-64,
  before the single rounding (the ISA says a fixed 2^32).
- div_fixup follows the ISA, and a NaN quotient with finite inputs becomes
  +-inf.
s_denorm_mode is accepted and ignored; listed in TechnicalDebt.

Co-Authored-By: Claude <noreply@anthropic.com>
…nd refuse s_denorm_mode

v_div_scale_f32 returns a NaN source unchanged instead of scaling it,
and v_div_fmas_f32 quiets the first NaN input or returns the default
NaN 0xffc00000, so results no longer depend on the driver's NaNs. The
test compares NaN results bit for bit. s_denorm_mode throws: the
recompiler does not pin a denormal mode, so no value matches what it
models.

Co-Authored-By: Claude <noreply@anthropic.com>
@adriadam10

Copy link
Copy Markdown
Collaborator Author

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.

@adriadam10
adriadam10 force-pushed the feat/div-scale-fmas-fixup branch from 87151db to 1a55076 Compare October 4, 2026 10:06
@github-actions github-actions Bot removed the needs rebase Conflicts with main, rebase needed label Oct 4, 2026
@oneandonlydean

Copy link
Copy Markdown
Collaborator

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 up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agc libSceAgc* build ci .github docs shader core/shader

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants