feat(shader): implement basic f64 VALU ops #446
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/446"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
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:
v_fma_f64,v_add_f64,v_mul_f64,v_min_f64,v_max_f64,v_ldexp_f64.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:
U64, auvec2) in the IR. New IR opcodes (FPAdd64,ConvertF64F32, ...) bitcast todoublein SPIR-V.shaderFloat64when it is available together withshaderSignedZeroInfNanPreserveFloat64, andpublishes
Float64. Shaders that use f64 declareFloat64andSignedZeroInfNanPreserve 64. On a device without itthey fail to compile with a clear error. Graphics validation now accepts
SignedZeroInfNanPreserve32 and 64.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):
cvt_f32_f64too). Clamp works: NaN → +0, -0 → +0.returns (RADV keeps a signalling one), so the NaN is chosen on the bits.
v_fma_f64is fused. GLSLFmais not enough: Vulkan defines its precision as "inherited from OpFMul followed byOpFAdd", 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 to0x3fefffffffffffff. inf → NaN.frexp: inf/NaN give the input (NaN quieted) and exponent 0.cvt_i32/u32_f64saturate, NaN → 0.cvtbetween f32 and f64 quiets NaN and keeps the payload.trunc/ceil/floor/rndneandldexpare done on the bits instead of GLSL.std.450. lavapipe returned wrong f64RoundEven(1.5)(1.0) andTrunc(fraction bits kept). GLSLLdexpis undefined past ±1024, and lavapipe gave NaNfor
ldexp(-inf, huge)in one sweep run.ldexpnormalises the mantissa, then scales in one exact step and onerounding step.
Overlap: #365 (VOPC f64 compares, DotDebian) adds a similar
readF64Bitshelper and does not touch these opcodes.Whichever merges second can switch to the other's helper. It merges cleanly with this one.
Tested
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.both, outside the PR.
shaderDenormPreserveFloat64), so the testvectors exclude denormal inputs and results below 2^-1021. On RADV, 6 more batches that include denormals all match
the hardware.
Fma(CI lavapipe), add without quieting (RADV), min/max without sNaN quieting, min/max ±0, GLSL
TruncandRoundEven(lavapipe), no fract clamp, f32→f64 without quieting (RADV), no i32 saturation, no clamp, 1/(2π) as thewidened f32.
ctest: 146/147. The failure isagc_driver_pixel_helper_lanes(wave64 case) on thismachine's RADV, and it fails the same way on
mainwithout this change.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt.mdfiles or images added to the repository (attach them to this PR)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
V_CEIL_F64V_CVT_F32_F64V_CVT_F64_F32V_CVT_F64_I32V_CVT_F64_U32V_CVT_I32_F64V_CVT_U32_F64V_FLOOR_F64V_FRACT_F64V_FREXP_EXP_I32_F64V_FREXP_MANT_F64V_RNDNE_F64V_TRUNC_F64V_ADD_F64V_FMA_F64V_LDEXP_F64V_MAX_F64V_MIN_F64V_MUL_F64Imported 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.
Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-04T06:25:38Z.
Thanks! Both done, rebased onto main:
readF64now just builds the U64 fromreadF64Bits, so the literal, 1/(2π), abs and neg rules live in one place.v_fma_f64is exact only for normal inputs; with a subnormal input it is a separate multiply and add that rounds twice.agc_driver_f64_arithmetic,_conversions,_roundingandagc_driver_vopc_compare_f64pass on RADV and lavapipe.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.
Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-04T06:45:38Z.
I ran the tests here and
agc_driver_f64_conversionsfails on NVIDIA:lane 0 cvt f64 f32 lo is 0xe0000000, expected 0x20000000. That's the f32 NaN0x7fc00001converted to f64: RDNA keeps the payload (0x7ff8000020000000), butOpFConvertleaves NaN payloads to the driver, and NVIDIA returns a different one, while RADV and lavapipe happen to match. Couldv_cvt_f64_f32build NaN results from the bits (sign, quiet bit set, payload shifted left by 29) and useOpFConvertonly 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.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_f32builds NaN results from the bits: sign,0x7ff8quiet exponent, payload shifted left by 29.OpFConvertis used only for non-NaN inputs.v_cvt_f32_f64had 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_f64had no test, so I addedF64NanConversionswith 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.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_conversionspass on NVIDIA here, and with the previousv_cvt_f64_f32the new test fails with the same lane 0 mismatch, so it catches the problem. Forv_cvt_f32_f64NVIDIA'sOpFConverthappens 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.