feat(shader): implement basic f64 VALU ops #446

Merged
adriadam10 merged 4 commits from upstream-pr/446 into main 2026-10-04 09:55:24 +00:00
adriadam10 commented 2026-10-03 07:53:58 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-03T07:53:58Z.


What

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

  • VOP3: v_fma_f64, v_add_f64, v_mul_f64, v_min_f64, v_max_f64, v_ldexp_f64.
  • VOP1 (and their VOP3 forms): v_cvt_{i32,u32,f32}_f64, v_cvt_f64_{i32,u32,f32}, v_{trunc,ceil,floor,rndne,fract}_f64,
    v_frexp_mant_f64, v_frexp_exp_i32_f64.

There was no f64 path in the backend, so this adds one:

  • f64 values stay as raw bits (U64, a uvec2) in the IR. New IR opcodes (FPAdd64, ConvertF64F32, ...) bitcast to
    double in SPIR-V.
  • The device enables shaderFloat64 when it is available together with shaderSignedZeroInfNanPreserveFloat64, and
    publishes Float64. Shaders that use f64 declare Float64 and SignedZeroInfNanPreserve 64. On a device without it
    they fail to compile with a clear error. Graphics validation now accepts SignedZeroInfNanPreserve 32 and 64.
  • Operand reading: a literal is the high dword (low = 0), the 1/(2π) inline constant is the exact double
    0x3fc45f306dc9c882, and abs/neg act on bit 63.

Behaviour measured on RDNA2 hardware (Radeon 660M, 8209 inputs per op, matched 100% by a Python model before the C++
was written):

  • omod is ignored by every f64 op (cvt_f32_f64 too). Clamp works: NaN → +0, -0 → +0.
  • NaN results are quiet: the first NaN input, quieted, else the default NaN. Vulkan doesn't say which NaN an operation
    returns (RADV keeps a signalling one), so the NaN is chosen on the bits.
  • min/max: a signalling NaN is returned quieted (src0 first), a quiet NaN loses, and -0 < +0.
  • v_fma_f64 is fused. GLSL Fma is not enough: Vulkan defines its precision as "inherited from OpFMul followed by
    OpFAdd", and the lavapipe in CI (Mesa 25.2.8) rounds twice. So fma is emulated with correctly rounded mul/add only:
    exact product (Dekker split) + TwoSum + round-to-odd, one final rounding (Boldo-Melquiond), on operands scaled to
    exponent 0. It matches the hardware on all 13253 captured fma results and an exact model on 300k random inputs.
  • fract: x - floor(x) clamped to 0x3fefffffffffffff. inf → NaN.
  • frexp: inf/NaN give the input (NaN quieted) and exponent 0.
  • cvt_i32/u32_f64 saturate, NaN → 0. cvt between f32 and f64 quiets NaN and keeps the payload.

trunc/ceil/floor/rndne and ldexp are done on the bits instead of GLSL.std.450. lavapipe returned wrong f64
RoundEven(1.5) (1.0) and Trunc (fraction bits kept). GLSL Ldexp is undefined past ±1024, and lavapipe gave NaN
for ldexp(-inf, huge) in one sweep run. ldexp normalises the mantissa, then scales in one exact step and one
rounding step.

Overlap: #365 (VOPC f64 compares, DotDebian) adds a similar readF64Bits helper and does not touch these opcodes.
Whichever merges second can switch to the other's helper. It merges cleanly with this one.

Tested

  • 3 new execution tests, 32 lanes each, with expected values captured on hardware. Rows were picked greedily to cover
    result classes and forced edge rows (fract of -2^-60, ldexp with INT_MIN/INT_MAX/-2100 exponents, ±inf):
    • F64Arithmetic: add, mul, fma, fma rounding error, min, max, ldexp, abs/neg.
    • F64Rounding: trunc, ceil, floor, rndne, fract, frexp, cvt_f32 with omod, cvt_i32/u32.
    • F64Conversions: cvt from f32/i32/u32, clamp, omod, literal, VOP3 VOP1 form with neg, 1/(2π) inline constant.
  • Passes on Linux with RADV, a current lavapipe and the CI's lavapipe (Mesa 25.2.8 / LLVM 20.1.2, run locally). Another 14 random batches of hardware rows (1344 rows) also passed on
    both, outside the PR.
  • f64 denormals follow the driver: lavapipe flushes them (it has no shaderDenormPreserveFloat64), so the test
    vectors exclude denormal inputs and results below 2^-1021. On RADV, 6 more batches that include denormals all match
    the hardware.
  • Negative checks, each fails a test: abs/neg on the low dword, literal in the low dword, omod applied, GLSL Fma
    (CI lavapipe), add without quieting (RADV), min/max without sNaN quieting, min/max ±0, GLSL Trunc and
    RoundEven (lavapipe), no fract clamp, f32→f64 without quieting (RADV), no i32 saturation, no clamp, 1/(2π) as the
    widened f32.
  • Full Release build + ctest: 146/147. 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
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4725014535 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/446). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-03T07:53:58Z. --- ### What The basic f64 VALU ops threw "opcode not implemented". Found by the ISA coverage sweep: - VOP3: `v_fma_f64`, `v_add_f64`, `v_mul_f64`, `v_min_f64`, `v_max_f64`, `v_ldexp_f64`. - VOP1 (and their VOP3 forms): `v_cvt_{i32,u32,f32}_f64`, `v_cvt_f64_{i32,u32,f32}`, `v_{trunc,ceil,floor,rndne,fract}_f64`, `v_frexp_mant_f64`, `v_frexp_exp_i32_f64`. There was no f64 path in the backend, so this adds one: - f64 values stay as raw bits (`U64`, a `uvec2`) in the IR. New IR opcodes (`FPAdd64`, `ConvertF64F32`, ...) bitcast to `double` in SPIR-V. - The device enables `shaderFloat64` when it is available together with `shaderSignedZeroInfNanPreserveFloat64`, and publishes `Float64`. Shaders that use f64 declare `Float64` and `SignedZeroInfNanPreserve 64`. On a device without it they fail to compile with a clear error. Graphics validation now accepts `SignedZeroInfNanPreserve` 32 and 64. - Operand reading: a literal is the high dword (low = 0), the 1/(2π) inline constant is the exact double `0x3fc45f306dc9c882`, and abs/neg act on bit 63. Behaviour measured on RDNA2 hardware (Radeon 660M, 8209 inputs per op, matched 100% by a Python model before the C++ was written): - omod is ignored by every f64 op (`cvt_f32_f64` too). Clamp works: NaN → +0, -0 → +0. - NaN results are quiet: the first NaN input, quieted, else the default NaN. Vulkan doesn't say which NaN an operation returns (RADV keeps a signalling one), so the NaN is chosen on the bits. - min/max: a signalling NaN is returned quieted (src0 first), a quiet NaN loses, and -0 < +0. - `v_fma_f64` is fused. GLSL `Fma` is not enough: Vulkan defines its precision as "inherited from OpFMul followed by OpFAdd", and the lavapipe in CI (Mesa 25.2.8) rounds twice. So fma is emulated with correctly rounded mul/add only: exact product (Dekker split) + TwoSum + round-to-odd, one final rounding (Boldo-Melquiond), on operands scaled to exponent 0. It matches the hardware on all 13253 captured fma results and an exact model on 300k random inputs. - `fract`: `x - floor(x)` clamped to `0x3fefffffffffffff`. inf → NaN. - `frexp`: inf/NaN give the input (NaN quieted) and exponent 0. - `cvt_i32/u32_f64` saturate, NaN → 0. `cvt` between f32 and f64 quiets NaN and keeps the payload. `trunc`/`ceil`/`floor`/`rndne` and `ldexp` are done on the bits instead of GLSL.std.450. lavapipe returned wrong f64 `RoundEven(1.5)` (1.0) and `Trunc` (fraction bits kept). GLSL `Ldexp` is undefined past ±1024, and lavapipe gave NaN for `ldexp(-inf, huge)` in one sweep run. `ldexp` normalises the mantissa, then scales in one exact step and one rounding step. Overlap: #365 (VOPC f64 compares, DotDebian) adds a similar `readF64Bits` helper and does not touch these opcodes. Whichever merges second can switch to the other's helper. It merges cleanly with this one. ### Tested - 3 new execution tests, 32 lanes each, with expected values captured on hardware. Rows were picked greedily to cover result classes and forced edge rows (fract of -2^-60, ldexp with INT_MIN/INT_MAX/-2100 exponents, ±inf): - `F64Arithmetic`: add, mul, fma, fma rounding error, min, max, ldexp, abs/neg. - `F64Rounding`: trunc, ceil, floor, rndne, fract, frexp, cvt_f32 with omod, cvt_i32/u32. - `F64Conversions`: cvt from f32/i32/u32, clamp, omod, literal, VOP3 VOP1 form with neg, 1/(2π) inline constant. - Passes on Linux with RADV, a current lavapipe and the CI's lavapipe (Mesa 25.2.8 / LLVM 20.1.2, run locally). Another 14 random batches of hardware rows (1344 rows) also passed on both, outside the PR. - f64 denormals follow the driver: lavapipe flushes them (it has no `shaderDenormPreserveFloat64`), so the test vectors exclude denormal inputs and results below 2^-1021. On RADV, 6 more batches that include denormals all match the hardware. - Negative checks, each fails a test: abs/neg on the low dword, literal in the low dword, omod applied, GLSL `Fma` (CI lavapipe), add without quieting (RADV), min/max without sNaN quieting, min/max ±0, GLSL `Trunc` and `RoundEven` (lavapipe), no fract clamp, f32→f64 without quieting (RADV), no i32 saturation, no clamp, 1/(2π) as the widened f32. - Full Release build + `ctest`: 146/147. 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 - [x] AI-assisted: yes
github-actions[bot] commented 2026-10-03 07:54:18 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-03T07:54:18Z.


Progress report (b0a4a99 → fe26911)

📈 GPU shader instructions: 75.13% (+1.63%, +19 implemented instructions)

✅ 19 implemented
Encoding Name
VOP1 V_CEIL_F64
VOP1 V_CVT_F32_F64
VOP1 V_CVT_F64_F32
VOP1 V_CVT_F64_I32
VOP1 V_CVT_F64_U32
VOP1 V_CVT_I32_F64
VOP1 V_CVT_U32_F64
VOP1 V_FLOOR_F64
VOP1 V_FRACT_F64
VOP1 V_FREXP_EXP_I32_F64
VOP1 V_FREXP_MANT_F64
VOP1 V_RNDNE_F64
VOP1 V_TRUNC_F64
VOP3 V_ADD_F64
VOP3 V_FMA_F64
VOP3 V_LDEXP_F64
VOP3 V_MAX_F64
VOP3 V_MIN_F64
VOP3 V_MUL_F64
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5966943832 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/446#issuecomment-5966943832). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-03T07:54:18Z. --- <!-- progress-report --> ### Progress report (b0a4a99 → fe26911) 📈 **GPU shader instructions**: 75.13% (+1.63%, +19 implemented instructions) <details> <summary>✅ 19 implemented</summary> | Encoding | Name | | - | - | | VOP1 | `V_CEIL_F64` | | VOP1 | `V_CVT_F32_F64` | | VOP1 | `V_CVT_F64_F32` | | VOP1 | `V_CVT_F64_I32` | | VOP1 | `V_CVT_F64_U32` | | VOP1 | `V_CVT_I32_F64` | | VOP1 | `V_CVT_U32_F64` | | VOP1 | `V_FLOOR_F64` | | VOP1 | `V_FRACT_F64` | | VOP1 | `V_FREXP_EXP_I32_F64` | | VOP1 | `V_FREXP_MANT_F64` | | VOP1 | `V_RNDNE_F64` | | VOP1 | `V_TRUNC_F64` | | VOP3 | `V_ADD_F64` | | VOP3 | `V_FMA_F64` | | VOP3 | `V_LDEXP_F64` | | VOP3 | `V_MAX_F64` | | VOP3 | `V_MIN_F64` | | VOP3 | `V_MUL_F64` | </details>
oneandonlydean commented 2026-10-04 05:44:55 +00:00 (Migrated from github.com)

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


Thanks, this is a big and careful piece of work, and the hardware vectors make it easy to trust. Two small things. Main already has TranslationContext::readF64Bits (from #365) with exactly the rules readF64 implements here (literal as the high dword, the exact 1/(2π) double, abs and neg on bit 63); could readF64 just build the U64 from readF64Bits, so the rules live in one place? #474 already uses both. And v_fma_f64 falls back to a separate multiply and add whenever an input is subnormal, which rounds twice; since f64 denormals follow the driver that may be fine for now, but I think it should be written down in docs/dev/TechnicalDebt.md so the gap is recorded.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5977034597 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/446#issuecomment-5977034597). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-04T05:44:55Z. --- Thanks, this is a big and careful piece of work, and the hardware vectors make it easy to trust. Two small things. Main already has TranslationContext::readF64Bits (from #365) with exactly the rules readF64 implements here (literal as the high dword, the exact 1/(2π) double, abs and neg on bit 63); could readF64 just build the U64 from readF64Bits, so the rules live in one place? #474 already uses both. And v_fma_f64 falls back to a separate multiply and add whenever an input is subnormal, which rounds twice; since f64 denormals follow the driver that may be fine for now, but I think it should be written down in docs/dev/TechnicalDebt.md so the gap is recorded.
adriadam10 commented 2026-10-04 06:25:38 +00:00 (Migrated from github.com)

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


Thanks! Both done, rebased onto main:

  • readF64 now just builds the U64 from readF64Bits, so the literal, 1/(2π), abs and neg rules live in one place.
  • Added a line under "Functional" in TechnicalDebt.md: v_fma_f64 is exact only for normal inputs; with a subnormal input it is a separate multiply and add that rounds twice.

agc_driver_f64_arithmetic, _conversions, _rounding and agc_driver_vopc_compare_f64 pass on RADV and lavapipe.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5977296224 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/446#issuecomment-5977296224). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-04T06:25:38Z. --- Thanks! Both done, rebased onto main: - `readF64` now just builds the U64 from `readF64Bits`, so the literal, 1/(2π), abs and neg rules live in one place. - Added a line under "Functional" in TechnicalDebt.md: `v_fma_f64` is exact only for normal inputs; with a subnormal input it is a separate multiply and add that rounds twice. `agc_driver_f64_arithmetic`, `_conversions`, `_rounding` and `agc_driver_vopc_compare_f64` pass on RADV and lavapipe.
oneandonlydean commented 2026-10-04 06:29:21 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-04T06:29:21Z.


Thanks for the quick update, that covers it. I'll test it here and merge once CI is green.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5977319571 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/446#issuecomment-5977319571). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-04T06:29:21Z. --- Thanks for the quick update, that covers it. I'll test it here and merge once CI is green.
oneandonlydean commented 2026-10-04 06:45:38 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-04T06:45:38Z.


I ran the tests here and agc_driver_f64_conversions fails on NVIDIA: lane 0 cvt f64 f32 lo is 0xe0000000, expected 0x20000000. That's the f32 NaN 0x7fc00001 converted to f64: RDNA keeps the payload (0x7ff8000020000000), but OpFConvert leaves NaN payloads to the driver, and NVIDIA returns a different one, while RADV and lavapipe happen to match. Could v_cvt_f64_f32 build NaN results from the bits (sign, quiet bit set, payload shifted left by 29) and use OpFConvert only for non-NaN inputs? The test stops at the first mismatch, so other conversions in the PR that can see a NaN may need the same treatment.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5977425948 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/446#issuecomment-5977425948). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-04T06:45:38Z. --- I ran the tests here and `agc_driver_f64_conversions` fails on NVIDIA: `lane 0 cvt f64 f32 lo is 0xe0000000, expected 0x20000000`. That's the f32 NaN `0x7fc00001` converted to f64: RDNA keeps the payload (`0x7ff8000020000000`), but `OpFConvert` leaves NaN payloads to the driver, and NVIDIA returns a different one, while RADV and lavapipe happen to match. Could `v_cvt_f64_f32` build NaN results from the bits (sign, quiet bit set, payload shifted left by 29) and use `OpFConvert` only for non-NaN inputs? The test stops at the first mismatch, so other conversions in the PR that can see a NaN may need the same treatment.
adriadam10 commented 2026-10-04 06:52:27 +00:00 (Migrated from github.com)

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


Thanks for running it on NVIDIA. Fixed and rebased onto main:

  • v_cvt_f64_f32 builds NaN results from the bits: sign, 0x7ff8 quiet exponent, payload shifted left by 29. OpFConvert is used only for non-NaN inputs.
  • The reverse conversion v_cvt_f32_f64 had the same issue, so it is fixed the same way: sign, 0x7fc00000, and the top 23 payload bits ((hi & 0xfffff) << 3 | lo >> 29).

Both match the hardware on all 60 NaN conversions I measured on RDNA2 (quiet and signalling, both signs, random payloads). The other conversions here already pick their NaN results without the driver (f64 to i32/u32 select 0 for NaN; the arithmetic, rounding, min/max and fma paths go through NanResult/Quiet).

v_cvt_f32_f64 had no test, so I added F64NanConversions with those hardware rows for both directions. RADV and lavapipe keep payloads natively, so here the old code passes too. To check the test can catch a wrong payload, I dropped the shifted payload bits in each direction, and each mutation fails the test. Release build passes, and the five f64 tests pass on RADV, local lavapipe and CI's lavapipe.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5977469528 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/446#issuecomment-5977469528). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-04T06:52:27Z. --- Thanks for running it on NVIDIA. Fixed and rebased onto main: - `v_cvt_f64_f32` builds NaN results from the bits: sign, `0x7ff8` quiet exponent, payload shifted left by 29. `OpFConvert` is used only for non-NaN inputs. - The reverse conversion `v_cvt_f32_f64` had the same issue, so it is fixed the same way: sign, `0x7fc00000`, and the top 23 payload bits (`(hi & 0xfffff) << 3 | lo >> 29`). Both match the hardware on all 60 NaN conversions I measured on RDNA2 (quiet and signalling, both signs, random payloads). The other conversions here already pick their NaN results without the driver (f64 to i32/u32 select 0 for NaN; the arithmetic, rounding, min/max and fma paths go through `NanResult`/`Quiet`). `v_cvt_f32_f64` had no test, so I added `F64NanConversions` with those hardware rows for both directions. RADV and lavapipe keep payloads natively, so here the old code passes too. To check the test can catch a wrong payload, I dropped the shifted payload bits in each direction, and each mutation fails the test. Release build passes, and the five f64 tests pass on RADV, local lavapipe and CI's lavapipe.
oneandonlydean commented 2026-10-04 07:39:52 +00:00 (Migrated from github.com)

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


Thanks, that fixes it. Merged with current main, all five f64 tests including the new agc_driver_f64_nan_conversions pass on NVIDIA here, and with the previous v_cvt_f64_f32 the new test fails with the same lane 0 mismatch, so it catches the problem. For v_cvt_f32_f64 NVIDIA's OpFConvert happens to keep the top payload bits already, but building it from the bits makes it independent of the driver. I'll run it through my PPSA21564 check with the other shader PRs and then merge it.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5977779190 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/446#issuecomment-5977779190). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-04T07:39:52Z. --- Thanks, that fixes it. Merged with current main, all five f64 tests including the new `agc_driver_f64_nan_conversions` pass on NVIDIA here, and with the previous `v_cvt_f64_f32` the new test fails with the same lane 0 mismatch, so it catches the problem. For `v_cvt_f32_f64` NVIDIA's `OpFConvert` happens to keep the top payload bits already, but building it from the bits makes it independent of the driver. I'll run it through my PPSA21564 check with the other shader PRs and then merge it.
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!446
No description provided.