feat(shader): implement v_rcp/rsq/sqrt_f64 and v_trig_preop_f64 #447

Merged
adriadam10 merged 1 commit from upstream-pr/447 into main 2026-10-05 02:48:23 +00:00
adriadam10 commented 2026-10-03 08:07:28 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-03T08:07:28Z.


What

The rest of the unary f64 VALU ops threw "opcode not implemented". Found by the ISA coverage sweep:

  • VOP1 (and their VOP3 forms): v_rcp_f64, v_rsq_f64, v_sqrt_f64.
  • VOP3: v_trig_preop_f64.

Builds on the f64 IR/backend infrastructure of #446 (merged). The
v_div_scale/fmas/fixup_f64 ops are left for a later PR because they share structure with #264.

Behaviour measured on RDNA2 hardware (Radeon 660M, 8209 inputs, Python model first, then the C++):

  • v_rcp_f64, v_rsq_f64 and v_sqrt_f64 are approximations on hardware: relative error up to ~2^-24.3, and
    sqrt(1.0) returns 0x3ff0000004000000. Real code refines them with Newton steps, so they are emitted as exact
    double ops (1/x, Sqrt, 1/Sqrt) and the test compares normal results with a 2^-23 tolerance.
  • Special cases are exact and selected on the bits: NaN inputs are returned quieted (RADV keeps a signalling NaN on
    FDiv), any negative input to rsq/sqrt (incl. -inf) gives the default NaN 0xfff8000000000000, rcp(±0) = ±inf,
    rcp(±inf) = ±0, rsq(±0) = ±inf, rsq(+inf) = +0, sqrt(-0) = -0.
  • v_trig_preop_f64 matches the hardware bit for bit (24627 results + a sweep over the end of the table):
    shift = 53 * S1[4:0] + max(exp(S0) - 1077, 0), result = 53-bit window of 2/π at that shift scaled by
    2^(-53 - shift), +128 when exp(S0) >= 1968. The hardware table is 1184 bits: bits below b17 of the 1201-bit
    value in the ISA doc read as zero (only visible in denormal results). omod is ignored, like every f64 op (#446).
  • The window is read from a private u32[40] table and scaled in two exact steps, so only the last multiply rounds.

Tested

  • New execution test F64Transcendental, 32 lanes with expected values captured on hardware: rcp, rsq, sqrt,
    trig_preop with a VGPR / inline S1 and with abs/neg + omod, rcp with clamp, sqrt of -|x|. Rows were picked
    greedily to cover input/result classes, exponent ranges (>1077, >=1968) and two forced signalling NaN rows.
  • Passes on Linux with RADV, a current lavapipe and the CI's lavapipe (Mesa 25.2.8 / LLVM 20.1.2, run locally). Another 12 random batches of hardware rows (384 rows) also passed on both,
    outside the PR. As in #446, denormal inputs and results are left out (lavapipe flushes f64 denormals).
  • Negative checks, each fails the test: no NaN quieting in rcp (RADV), no negative → default NaN in rsq/sqrt (RADV),
    unsigned zero in rsq, no +128 for large exponents, no exponent shift, wrong low word, no aligned-shift case.
  • Full Release build + ctest: 147/148. 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

  • 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 (#446 is merged)
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4725078028 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/447). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-03T08:07:28Z. --- ### What The rest of the unary f64 VALU ops threw "opcode not implemented". Found by the ISA coverage sweep: - VOP1 (and their VOP3 forms): `v_rcp_f64`, `v_rsq_f64`, `v_sqrt_f64`. - VOP3: `v_trig_preop_f64`. Builds on the f64 IR/backend infrastructure of #446 (merged). The `v_div_scale/fmas/fixup_f64` ops are left for a later PR because they share structure with #264. Behaviour measured on RDNA2 hardware (Radeon 660M, 8209 inputs, Python model first, then the C++): - `v_rcp_f64`, `v_rsq_f64` and `v_sqrt_f64` are approximations on hardware: relative error up to ~2^-24.3, and `sqrt(1.0)` returns `0x3ff0000004000000`. Real code refines them with Newton steps, so they are emitted as exact double ops (`1/x`, `Sqrt`, `1/Sqrt`) and the test compares normal results with a 2^-23 tolerance. - Special cases are exact and selected on the bits: NaN inputs are returned quieted (RADV keeps a signalling NaN on `FDiv`), any negative input to rsq/sqrt (incl. -inf) gives the default NaN `0xfff8000000000000`, `rcp(±0) = ±inf`, `rcp(±inf) = ±0`, `rsq(±0) = ±inf`, `rsq(+inf) = +0`, `sqrt(-0) = -0`. - `v_trig_preop_f64` matches the hardware bit for bit (24627 results + a sweep over the end of the table): `shift = 53 * S1[4:0] + max(exp(S0) - 1077, 0)`, result = 53-bit window of 2/π at that shift scaled by 2^(-53 - shift), +128 when `exp(S0) >= 1968`. The hardware table is 1184 bits: bits below b17 of the 1201-bit value in the ISA doc read as zero (only visible in denormal results). omod is ignored, like every f64 op (#446). - The window is read from a private `u32[40]` table and scaled in two exact steps, so only the last multiply rounds. ### Tested - New execution test `F64Transcendental`, 32 lanes with expected values captured on hardware: rcp, rsq, sqrt, trig_preop with a VGPR / inline S1 and with abs/neg + omod, rcp with clamp, sqrt of `-|x|`. Rows were picked greedily to cover input/result classes, exponent ranges (>1077, >=1968) and two forced signalling NaN rows. - Passes on Linux with RADV, a current lavapipe and the CI's lavapipe (Mesa 25.2.8 / LLVM 20.1.2, run locally). Another 12 random batches of hardware rows (384 rows) also passed on both, outside the PR. As in #446, denormal inputs and results are left out (lavapipe flushes f64 denormals). - Negative checks, each fails the test: no NaN quieting in rcp (RADV), no negative → default NaN in rsq/sqrt (RADV), unsigned zero in rsq, no +128 for large exponents, no exponent shift, wrong low word, no aligned-shift case. - Full Release build + `ctest`: 147/148. 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 <!-- 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 (#446 is merged) - [x] AI-assisted: yes
github-actions[bot] commented 2026-10-03 08:07:53 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-03T08:07:53Z.


Progress report (ebf29f8 → 4f901b9)

📈 GPU shader instructions: 87.65% (+0.34%, +4 implemented instructions)

✅ 4 implemented
Encoding Name
VOP1 V_RCP_F64
VOP1 V_RSQ_F64
VOP1 V_SQRT_F64
VOP3 V_TRIG_PREOP_F64
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5967035588 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/447#issuecomment-5967035588). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-03T08:07:53Z. --- <!-- progress-report --> ### Progress report (ebf29f8 → 4f901b9) 📈 **GPU shader instructions**: 87.65% (+0.34%, +4 implemented instructions) <details> <summary>✅ 4 implemented</summary> | Encoding | Name | | - | - | | VOP1 | `V_RCP_F64` | | VOP1 | `V_RSQ_F64` | | VOP1 | `V_SQRT_F64` | | VOP3 | `V_TRIG_PREOP_F64` | </details>
adriadam10 commented 2026-10-04 10:05:12 +00:00 (Migrated from github.com)

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


Rebased on current main now that #446 is merged: the branch is a single commit on top of main (no conflicts in it). F64Transcendental passes on RADV, local lavapipe and CI's lavapipe (Mesa 25.2.8). Description no longer says stacked.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5978832748 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/447#issuecomment-5978832748). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-04T10:05:12Z. --- Rebased on current main now that #446 is merged: the branch is a single commit on top of main (no conflicts in it). F64Transcendental passes on RADV, local lavapipe and CI's lavapipe (Mesa 25.2.8). Description no longer says stacked.
oneandonlydean commented 2026-10-04 23:09:18 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-04T23:09:18Z.


Thanks. I went through this against the ISA and it checks out:

  • Opcodes: llvm-mc (gfx1013) encodes v_rcp_f64, v_rsq_f64 and v_sqrt_f64 as VOP1 0x2f, 0x31 and 0x34, and v_trig_preop_f64 as VOP3 0x174, as in the tables.
  • The TwoOverPi table: I computed 2/pi independently (Machin, integer arithmetic) and the first 37 words match, with zeros after that, which is your 1184-bit hardware table.
  • v_trig_preop_f64: a Python model of the ISA formula (shift = 53 * S1[4:0] + max(exp - 1077, 0), a 53-bit window of 2/pi, scale 2^(-53 - shift), +128 when exp >= 1968) reproduces all 96 trig_preop cells of the test, including the inline-S1 and -|x| mul:2 columns (omod ignored). The two-step exact scaling leaves one rounding, as you say.
  • rcp/rsq/sqrt special cases follow the ISA: quieted NaN, default NaN for negative inputs to rsq/sqrt (also -inf), rsq(-0) = -inf, sqrt(-0) = -0, rsq(+inf) = +0. The 2^-23 tolerance on normal results is reasonable for an approximate op, and emitting exact doubles is fine since real code refines them anyway.

agc_driver_f64_transcendental passes on my machine (in a batch with this PR's commit); with only the test file on main's code it fails with "VOP1 opcode is not implemented".

One thing before it can go in: #264 was just merged, and this branch now conflicts with main in RdnaOpcode.hpp, RdnaOpcode.cpp and RdnaVectorOpDecoder.cpp (both add opcodes at the same spots). It also has a one-line clash with #498 in supportsNativeVop3SourceModifiers (keep both VLdexpF16 and VTrigPreopF64). Could you rebase it? After that it's good to merge from my side. None of my PPSA21564 shader dumps uses these four ops.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5985450407 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/447#issuecomment-5985450407). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-04T23:09:18Z. --- Thanks. I went through this against the ISA and it checks out: - Opcodes: llvm-mc (gfx1013) encodes `v_rcp_f64`, `v_rsq_f64` and `v_sqrt_f64` as VOP1 0x2f, 0x31 and 0x34, and `v_trig_preop_f64` as VOP3 0x174, as in the tables. - The `TwoOverPi` table: I computed 2/pi independently (Machin, integer arithmetic) and the first 37 words match, with zeros after that, which is your 1184-bit hardware table. - `v_trig_preop_f64`: a Python model of the ISA formula (shift = 53 * S1[4:0] + max(exp - 1077, 0), a 53-bit window of 2/pi, scale 2^(-53 - shift), +128 when exp >= 1968) reproduces all 96 trig_preop cells of the test, including the inline-S1 and `-|x| mul:2` columns (omod ignored). The two-step exact scaling leaves one rounding, as you say. - rcp/rsq/sqrt special cases follow the ISA: quieted NaN, default NaN for negative inputs to rsq/sqrt (also -inf), `rsq(-0) = -inf`, `sqrt(-0) = -0`, `rsq(+inf) = +0`. The 2^-23 tolerance on normal results is reasonable for an approximate op, and emitting exact doubles is fine since real code refines them anyway. `agc_driver_f64_transcendental` passes on my machine (in a batch with this PR's commit); with only the test file on main's code it fails with "VOP1 opcode is not implemented". One thing before it can go in: #264 was just merged, and this branch now conflicts with main in `RdnaOpcode.hpp`, `RdnaOpcode.cpp` and `RdnaVectorOpDecoder.cpp` (both add opcodes at the same spots). It also has a one-line clash with #498 in `supportsNativeVop3SourceModifiers` (keep both `VLdexpF16` and `VTrigPreopF64`). Could you rebase it? After that it's good to merge from my side. None of my PPSA21564 shader dumps uses these four ops.
adriadam10 commented 2026-10-05 01:37:00 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-05T01:37:00Z.


Rebased on current main (after #264): the conflicts were adjacent enum, classifier and VOP3 table lines next to the v_div_*_f32 ones, kept both sides. Commit otherwise unchanged; agc_driver_f64_transcendental_tests passes on RADV and lavapipe, full ctest green on RADV apart from the known agc_driver_pixel_helper_lanes.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5986633806 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/447#issuecomment-5986633806). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-05T01:37:00Z. --- Rebased on current main (after #264): the conflicts were adjacent enum, classifier and VOP3 table lines next to the `v_div_*_f32` ones, kept both sides. Commit otherwise unchanged; `agc_driver_f64_transcendental_tests` passes on RADV and lavapipe, full ctest green on RADV apart from the known `agc_driver_pixel_helper_lanes`.
oneandonlydean commented 2026-10-05 02:47:03 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-05T02:47:03Z.


Thanks, the rebase only moves the enum, classifier and VOP3 rows next to main's v_div_*_f32 ones; the code is the same as before. agc_driver_f64_transcendental passes here on the merged branch. PPSA21564 has no f64 transcendentals, so this doesn't need my PPSA21564 check. Ready to merge from my side.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5987227154 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/447#issuecomment-5987227154). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-05T02:47:03Z. --- Thanks, the rebase only moves the enum, classifier and VOP3 rows next to main's `v_div_*_f32` ones; the code is the same as before. `agc_driver_f64_transcendental` passes here on the merged branch. PPSA21564 has no f64 transcendentals, so this doesn't need my PPSA21564 check. 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!447
No description provided.