Skip to content

feat(shader): implement v_div_scale_f64, v_div_fmas_f64 and v_div_fixup_f64 - #474

Merged
oneandonlydean merged 2 commits into
boykopovar:mainfrom
adriadam10:feat/div-f64
Oct 5, 2026
Merged

oneandonlydean merged 2 commits into
boykopovar:mainfrom
adriadam10:feat/div-f64

Conversation

@adriadam10

@adriadam10 adriadam10 commented Oct 3, 2026 •

Copy link
Copy Markdown
Collaborator

What

The f64 division ops threw "opcode not implemented". Found by the ISA coverage sweep:

  • VOP3: v_div_scale_f64, v_div_fmas_f64, v_div_fixup_f64, the f64 version of the precise division macro compilers emit for a / b.

Stacked on #447 and #264 (#446 is merged): it uses the f64 ops of #447 and the f32 versions from #264. Their
commits are included here unchanged, with only adjacent opcode lines resolved between them. Only the last commit is new.

Behaviour measured on HW

RDNA2 (Radeon 660M), 41152 results from two kernels (plain operands, then abs/neg modifiers and per-lane VCC). I wrote a Python model first, it matched every result, and then the C++:

  • v_div_scale_f64 decides from the exponent fields only, like the f32 op (feat(shader): implement v_div_scale/fmas/fixup_f32 and s_denorm_mode (f32 division macro) #264), with f64 constants: it scales by
    2^128 or 2^-128 and sets VCC for exponent differences >= 768 or <= -768, a denormal or huge (exponent >= 2045)
    denominator and a numerator exponent <= 53. A NaN source is returned unchanged (signalling NaNs stay
    signalling), and a zero numerator or denominator gives 0xfff8000000000000.
  • v_div_fmas_f64 with VCC set computes (S0 * S1 + S2) * 2^±128 with a single rounding: 2^128 when the exponent
    of S2 is >= 1024, else 2^-128. Inf/NaN inputs and VCC clear give a plain fma. It reuses the exact fma emulation from
    feat(shader): implement basic f64 VALU ops #446 (Dekker + TwoSum + round-to-odd), with the scale folded into its final rescale, so the result does not depend
    on how the driver rounds Fma.
  • v_div_fixup_f64 follows the ISA: NaN sources come back quieted, 0/0 and inf/inf give the default NaN, a NaN
    quotient with finite inputs becomes ±inf, and exponent differences below -1075 give a signed zero.
  • Scaling is done on the bits (FPLdexp64), and so is NaN handling.

Tested

  • New execution tests F64Division and F64DivisionModifiers, 32 lanes each with expected values captured on
    hardware. Rows are picked greedily to cover input/result classes and exponent-difference ranges, and these are forced in: two
    signalling NaN rows, each threshold and the value next to it (768, -768, 2045, 53, -1075), and rows where rounding
    the fma first and then scaling gives a different result.
  • Passes on Linux with RADV, a current lavapipe and the CI's lavapipe (Mesa 25.2.8 / LLVM 20.1.2, run locally).
    Another 6 random batches per kernel (384 hardware rows) also passed on RADV and lavapipe, outside the PR. As in feat(shader): implement basic f64 VALU ops #446,
    denormal inputs and results are left out (lavapipe flushes f64 denormals).
  • Negative checks, each fails the test: div_scale quiets NaNs, each threshold off by one (768, 2045, 53),
    no zero → NaN, VCC without the <= -768 case, neg ignored on div_scale; div_fmas as a plain fma, as an fma followed by ldexp
    (double rounding), with the scale left out of the exact path, wrong direction threshold; div_fixup without NaN
    quotient → inf, without quieting, without the invalid cases, underflow threshold off by one.
  • Full Release build + ctest: 151/152. The failure is agc_driver_pixel_helper_lanes (wave64 case) on this
    machine's RADV, and it fails the same way on main without this change.

Checklist

@github-actions

github-actions Bot commented Oct 3, 2026 •

Copy link
Copy Markdown

Progress report (ebf29f8 → 063437c)

📈 GPU shader instructions: 87.91% (+0.6%, +7 implemented instructions)

✅ 7 implemented
Encoding Name
VOP1 V_RCP_F64
VOP1 V_RSQ_F64
VOP1 V_SQRT_F64
VOP3 V_DIV_FIXUP_F64
VOP3 V_DIV_FMAS_F64
VOP3 V_DIV_SCALE_F64
VOP3 V_TRIG_PREOP_F64

@adriadam10

Copy link
Copy Markdown
Collaborator Author

Rebased on current main: now carries the rebased #447 commit and both #264 commits (it had only the first one before, so the f32 NaN/s_denorm_mode fix of #264 is now included too), then this PR's commit. F64Division, F64DivisionModifiers, Division and F64Transcendental pass on RADV, local lavapipe and CI's lavapipe.

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

Copy link
Copy Markdown
Collaborator Author

Rebased on current main: the only Windows failure was guest_audio_in_no_device, fixed by #532. No other changes.

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

Copy link
Copy Markdown
Collaborator

Thanks. I reviewed only the last commit (c7b0dd4); the #447 and #264 commits are patch-identical to those PRs, apart from context.

  • v_div_fixup_f64 follows the ISA's priority order (numerator NaN, denominator NaN, 0/0 and inf/inf, x/0 and inf/y, x/inf and 0/y, underflow below -1075, then the sign-fixed quotient), plus the measured NaN quotient to inf rule. Taking NaNs and zeros on the bits avoids driver canonicalisation.
  • v_div_scale_f64 / v_div_fmas_f64 mirror the f32 versions from feat(shader): implement v_div_scale/fmas/fixup_f32 and s_denorm_mode (f32 division macro) #264 with the f64 thresholds you measured. neg is kept on div_scale (VOP3B), and clamp/omod still throw.
  • The FusedMultiplyAdd refactor keeps plain v_fma_f64 the same: with scale 0 the far branch now returns c * 1.0 instead of the raw c bits. That only differs for a denormal c on a driver that flushes f64 denormals, which the tests already leave out. The new [-2044, 2046] clamp only matters when the result overflows or underflows to zero anyway. The feat(shader): implement basic f64 VALU ops #446 f64 tests still pass with it.

agc_driver_f64_division and agc_driver_f64_division_modifiers pass on my machine at this head. On the commit below yours (#447 + #264 only) both fail with "VOP3 opcode is not implemented".

What needs doing: #264 is merged now, so this branch conflicts with main in RdnaOpcode.hpp, RdnaOpcode.cpp, RdnaVectorOpDecoder.cpp and VectorInstructions.cpp. Once #447 is rebased and merged, could you rebase this onto main so only your commit is left? Also, supportsNativeVop3SourceModifiers clashes by one line with #498. None of my PPSA21564 shader dumps uses the f64 division ops, so it doesn't need a gate run.

adriadam10 and others added 2 commits October 5, 2026 03:36
Decode and translate the remaining unary f64 transcendentals and
v_trig_preop_f64.

On RDNA2 hardware v_rcp_f64, v_rsq_f64 and v_sqrt_f64 are approximations
(relative error up to ~2^-24.3 over 8209 inputs; sqrt(1.0) is not even
exact), so they are emitted as exact double operations and the test
compares against the hardware value with a 2^-23 tolerance. Special
cases are exact and selected on bits: NaN inputs are quieted (RADV keeps
signaling NaN on FDiv), negative inputs give the default NaN
0xfff8000000000000, rsq(-0) = -inf, sqrt(-0) = -0.

v_trig_preop_f64 matches the hardware bit for bit on 24627 results plus
a sweep of the table tail: the 2/PI table is 1184 bits (bits below b17
read as zero), shift = 53 * S1[4:0] + max(exp - 1077, 0), +128 scale for
exp >= 1968, omod ignored. The 53-bit window is read from a private u32
table and scaled in two exact steps so only the final multiply rounds.

Stacked on the f64 infrastructure of boykopovar#446.

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

These make up the precise f64 division macro compilers emit for a / b.
Like the f32 ops, the rules were measured on an RDNA2 GPU (41152 rows,
model matches all of them):
- div_scale decides from the exponent fields only: 2^128 or 2^-128
  scaling and VCC for differences >= 768 or <= -768, a denormal or huge
  (>= 2045) denominator and a numerator exponent <= 53; a NaN source is
  passed through unchanged and a zero input gives 0xfff8000000000000.
- div_fmas scales by 2^128 when the exponent of S2 is >= 1024, else
  2^-128, before the single rounding, so it reuses the exact fma
  emulation with the scale folded into its final rescale.
- div_fixup follows the ISA: NaN sources are quieted, a NaN quotient
  with finite inputs becomes +-inf, exponent differences below -1075
  give a signed zero.

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

Copy link
Copy Markdown
Collaborator Author

Rebased on current main: #264's commits are now on main and dropped from this branch, which carries #447's rebased head plus the last commit (c7b0dd4 content unchanged apart from context: the f64 enum/classifier/VOP3 rows sit next to main's v_div_*_f32 ones). agc_driver_f64_division{,_modifiers}_tests and agc_driver_division_tests pass on RADV and lavapipe, full ctest green on RADV apart from the known agc_driver_pixel_helper_lanes.

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

Copy link
Copy Markdown
Collaborator

Thanks, this now carries #447's rebased head (the same commit), and your own commit only changed in context.

One small overlap to know about: #563 changes the same line in checkNativeVop3Modifiers (the clamp exception for v_mad_u64_u32/v_mad_i64_i32), so whichever of the two lands second needs a one-line rebase that keeps both exceptions:

if ((clamp != 0u && !isVop3BMadU64Opcode(opcode)) || omod != 0u || (neg != 0u && !floatCompare && opcode != RdnaOpcode::VDivScaleF32 && opcode != RdnaOpcode::VDivScaleF64)) {

With that resolved, agc_driver_f64_division, agc_driver_f64_division_modifiers and agc_driver_division pass here on the merged branch. PPSA21564 has no f64 division, so it doesn't need my PPSA21564 check. Ready to merge from my side after #447.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agc libSceAgc* build docs shader core/shader

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants