fix(shader): correctly rounded f32 reciprocal on every device #7671
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/7671"
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 tibzejoker on 2026-10-18T21:28:21Z.
What
v_rcp_f32andv_rcp_iflag_f32are translated to a plainOpFDiv(1, x), and Vulkan leaves the precision of a division to the device. In practice an RTX 4090 and an Intel UHD 770 return reciprocals one ulp apart. Each of them also differs from the correctly rounded value on some inputs. As a result, the same shader stores different bytes on the two vendors: I saw this on a GTA V compute pass (PPSA04264, compute 0x143978f00, one f32 one ulp apart).This change adds two fused Newton steps (
e = fma(-x, y, 1),y = fma(y, e, y)) after the division estimate. They give the correctly rounded reciprocal whenever the input and the estimate are normal. Zero, infinities, NaN and denormals keep the division result, so the special-value handling stays as it is.It also matters for integer division: RDNA compilers implement it with
v_rcp_iflag_f32followed by corrections, so it now gives the same value on every device.Cost: about ten SPIR-V instructions per reciprocal.
Tested
Windows 11, MinGW GCC 15.2:
agc_driver_rcp_correctly_rounded. It checksv_rcp_f32of 8192 normal inputs across the whole exponent range against the correctly rounded reciprocal.mainit fails on both GPUs:v_rcp_f32 of 0x858a127e is 0xf96d5337, expected the correctly rounded 0xf96d5336v_rcp_f32 of 0x1646f91a is 0x68a4af84, expected ... 0x68a4af85mainthe test fails (v_rcp_f32 of 0x0f5b5acd is 0x6f956235, expected ... 0x6f956236, one ulp below), and it passes with this change.ctest -R ^agc_on the RTX 4090: 727/728, includingagc_driver_vop1_float_unary. The failure isagc_driver_bda_device, which fails onmaintoo.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)🤖 Generated with Claude Code
https://claude.ai/code/session_01XBXM4pgkMHYEtqN1EQni2P
Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-18T21:46:27Z.
Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.
core/shader/recompiler/SpirvBackend/src/SpirvAlu/SpirvAluEmitterMath.cppcore/shader/recompiler/SpirvBackend/src/SpirvAlu/SpirvAluEmitterMath.cppcore/shader/recompiler/SpirvBackend/src/SpirvAlu/SpirvAluEmitterMath.cppcore/shader/recompiler/SpirvBackend/src/SpirvAlu/SpirvAluEmitterMath.cppcore/shader/recompiler/SpirvBackend/src/SpirvAlu/SpirvAluEmitterMath.cppImported from boykopovar/AnyPS5. Originally posted by DavidAmsellem on 2026-10-18T22:08:25Z.
Tested on AMD on Linux, which this PR had not covered.
radv, Mesa 25.2.8-0ubuntu0.24.04.43e8a9380dmerged intomain0bc3003f8, Release,BUILD_TESTING=ONagc_driver_rcp_correctly_roundedpasses.Full
ctestwithANYPS5_REQUIRE_VULKAN=1: 1479 of 1481. Both failures also happen onmainwithout this PR and are unrelated to it:
agc_pipeline_cache_file: the test reads a cache hit as an exactvkGetPipelineCacheDatasizematch, and RADV moves that size by 8 bytes on a hit (#7676).
agc_driver_guest_memory_lifetime_direct: this machine has no read-write access to/dev/udmabufyet.Imported from boykopovar/AnyPS5. Originally posted by tibzejoker on 2026-10-18T22:11:48Z.
Thanks @DavidAmsellem, that covers the vendor I could not test. If you still have the build, it would be useful to know whether
agc_driver_rcp_correctly_roundedalso fails on RADV withmain. It shows whether RADV's division is already correctly rounded or only close.AI-assisted (Claude Code).
Imported from boykopovar/AnyPS5. Originally posted by DavidAmsellem on 2026-10-18T22:16:55Z.
@tibzejoker RADV is not correctly rounded either, so this is not NVIDIA only.
I rebuilt
main0bc3003f8with onlyRcpCorrectlyRounded.cppfrom this PR added andSpirvAluEmitterMath.cppleft atmain. On the same machine (RX 7900 XT, RADV NAVI31 gfx1100,Mesa 25.2.8) the test fails at the first case it reports:
RADV's result is one ULP below the correctly rounded one, which matches what
v_rcp_f32isspecified to give (1 ULP of relative error), so RADV is close but not correctly rounded. With this
PR applied the test passes on the same GPU.
Imported from boykopovar/AnyPS5. Originally posted by tibzejoker on 2026-10-18T22:22:15Z.
Thanks, that settles it: all three vendors are one ulp off on some inputs with
main. I added your result to the description.AI-assisted (Claude Code).
Imported from boykopovar/AnyPS5. Originally posted by simonbbby on 2026-10-18T22:25:23Z.
Native RDNA 2 datapoint for the reference used by head
3e8a9380dbc0a50c829fce9a052f06b499d06a04: gfx1036'sv_rcp_f32andv_rcp_iflag_f32are not correctly rounded on this corpus.GPU: Ryzen 9 9950X iGPU, gfx1036, PCI
1002:13c0. I ran the two actual VALU instructions directly through the HSA hardware oracle, not through Vulkan or this recompiler:Inputs are the exact 8192
Sample(index)words from this PR's fixture. Both instructions differ from its correctly rounded reference on 889/8192 inputs, always by one ULP. Their outputs are identical to each other. The first difference is the same input as the RADV report above:I repeated the complete corpus in wave32 and wave64, each with
(ieee, dx10_clamp)of(0,0),(1,0)and(1,1). All six native result arrays are byte-identical. Every run explicitly setsdenorm32=3,denorm16=3,round32=0,round16=0,fp16_overflow=0; both input and correctly rounded reciprocal are normal for the fixture's exponent range.Reference/control checks:
Roundedalgorithm on all inputs; this is not a host-float-reference discrepancy.This measures gfx1036, not PS5 gfx1013, and does not establish the console's exact result or the GTA V pass's expected bytes. It does show that cross-device correctly rounded division is a different target from the measured gfx1036 instructions. Could the intended semantic target be clarified or measured on gfx1013 before treating the new correctly rounded oracle as console equivalence? No Vulkan-PR runtime or title result is claimed here.
Imported from boykopovar/AnyPS5. Originally posted by DavidAmsellem on 2026-10-18T22:28:30Z.
RDNA 3 datapoint, same method as @simonbbby: gfx1100's native
v_rcp_f32matches gfx1036 on thiscorpus, and neither is correctly rounded.
GPU: RX 7900 XT, gfx1100, RADV Mesa 25.2.8, Linux. The two VALU instructions straight through the
HSA hardware oracle, not through Vulkan or the recompiler:
Inputs are the 8192
Sample(index)words of this PR's fixture, with the reference recomputed fromits
Roundedalgorithm.v_rcp_f32differs from the correctly rounded reference on 889 of 8192 inputs, always byexactly one ULP. That is the same count @simonbbby measured on gfx1036.
0x0f5b5acd, native0x6f956235, correctly rounded0x6f956236.v_rcp_f32andv_rcp_iflag_f32agree on all 8192 inputs.(ieee, dx10_clamp)of(1,1)and(0,0), and withdenorm32of 3 and 0. All four native arrays are identical.So the one ULP shortfall is not vendor specific and not RDNA 2 specific: RDNA 1 (earlier report),
RDNA 2 and RDNA 3 all hit it, and on RDNA 2 and RDNA 3 with the same failure count and the same
first input.
This measures gfx1100, not the console's gfx1013, so like the gfx1036 report it does not establish
what the console returns.
AI-assisted (Claude Code).
Imported from boykopovar/AnyPS5. Originally posted by tibzejoker on 2026-10-18T22:32:57Z.
Thank you both, that is exactly the data I was missing.
You're right about the target. This PR makes the result identical on every host GPU: today NVIDIA, Intel and RADV each return their own value. It does not reproduce the console. Your measurements show that RDNA 1, 2 and 3 agree with each other bit for bit (889/8192, same first input), so gfx1013 most likely returns the same values. That makes the hardware result a better target than correct rounding: it would be both deterministic on every host and faithful to the console.
Reproducing it needs the hardware algorithm, not a table. For a normal input the mantissa of
v_rcp_f32depends only on the input mantissa, so one exponent covers everything. If one of you can run the HSA oracle over all 2^23 mantissas at a fixed exponent (inputs0x3f800000 | m) and attach the output words, or only the list ofmwhere they differ from the correctly rounded result, I will work out an exact formula from it and update the PR so the test checks the hardware values.Until then I'd keep this PR as it is (one value on every device, at most one ulp from RDNA on about 11 % of inputs) rather than leaving each driver to round differently. Maintainers, if you'd rather wait for the hardware-exact version, I can mark it as a draft.
AI-assisted (Claude Code).
Imported from boykopovar/AnyPS5. Originally posted by LeandroLP23 on 2026-10-18T22:33:43Z.
AMD run, since the description covers an RTX 4090. Radeon 860M (RDNA 3.5, driver 32.0.13058.8001), Windows 11 26300, WinLibs GCC 15.2.0 r7, Release,
BUILD_TESTING=ON. Build:main8a456334fwith #7677, #7673, #7671, #7666, #7676, #7675, #7672, #7668, #7664 and #7658 merged together (each merge clean, built once).agc_driver_rcp_correctly_rounded,agc_driver_vop1_float_unaryandagc_driver_bda_devicepass 3 runs of 3 on AMD's proprietary driver.AI-assisted
Imported from boykopovar/AnyPS5. Originally posted by wachi35676 on 2026-10-19T00:12:50Z.
Tested at
3e8a9380don Windows 11 (MinGW GCC 15.2), merged ontomain8a456334f, on an RTX 5070 Ti (617.42) and an AMD Radeon iGPU, RDNA2 (1002:13c0, 32.0.21042.62,ANYPS5_GPU=AMD).agc_driver_rcp_correctly_roundedand 10 related division/vop1 tests pass on both GPUs. WithSpirvAluEmitterMath.cppreverted tomain, the new test fails on both (NVIDIA:v_rcp_f32 of 0x858a127e is 0xf96d5337; AMD:0x0f5b5acd is 0x6f956235).A wider probe ran 283,697 inputs per GPU, covering denormals, ±0, inf, NaN, exponents 1/2/253/254, powers of two and all-ones significands.
main, 34,399 of 262,144 random normals are 1 ulp off on NVIDIA, and 28,044 on AMD.One limit of the two-step refinement: when the estimate is 1 ulp off, one input per exponent still comes out wrong, the one whose significand is all ones. An exhaustive CPU model shows this (
std::fma, every positive normal, estimates ±1..6 ulp). For example, the correctly rounded result forx = 0x3fffffffis0x3f000001, but estimates0x3f000000and0x3f000002both end at0x3f000000.Both GPUs here return exact estimates for all 508 such inputs, so nothing fails, but
Sample()cannot generate them. Addinge << 23 | 0x7ffffffor every exponent to the test would cover a device whose estimate is off there.Side note, pre-existing and unchanged: both GPUs flush denormals despite float mode
0xf0. Denormal inputs give ±inf, and |x| ≥ 2^126 gives ±0.AI-assisted: yes (Claude Code); results above were run on this machine.
Imported from boykopovar/AnyPS5. Originally posted by tibzejoker on 2026-10-19T00:36:22Z.
@wachi35676 thanks, good catch on the all-ones significand. Pushed
c402b9217, which changes the last step: one fused Newton step, then the closer of the result and its neighbour in the direction of the exact residualfma(-x, y, 1).An exhaustive CPU model with hardware FMA, over all 2^23 significands with estimates from -6 to +6 ulps around the correct result:
It also costs two fewer FMAs. The test now adds
e << 23 | 0x7ffffffor every exponent it covers, with both signs. It passes on the RTX 4090 and the UHD 770, andctest -R ^agc_is still 727/728 (agc_driver_bda_device, as onmain).AI-assisted (Claude Code).
Imported from boykopovar/AnyPS5. Originally posted by wachi35676 on 2026-10-19T01:14:14Z.
Re-tested at
c402b9217on Windows 11 (MinGW GCC 15.2), merged ontomainad502d717, on an RTX 5070 Ti and an AMD Radeon iGPU, RDNA2 (1002:13c0,ANYPS5_GPU=AMD).agc_driver_rcp_correctly_roundedand 10 related division/vop1 tests pass on both GPUs. WithSpirvAluEmitterMath.cppreverted tomain, the new test fails on both (NVIDIA:0x858a127e is 0xf96d5337; AMD:0x0f5b5acd is 0x6f956235).3e8a9380d, so denormal, inf, NaN and ±0 handling is unchanged. For comparison, onmain34,399 (NVIDIA) and 28,044 (AMD) of 262,144 random normals are 1 ulp off.fma(-x, y, 1)was exact in every case, so comparing residuals picks the correctly rounded value.AI-assisted: yes (Claude Code); results above were run on this machine.
Imported from boykopovar/AnyPS5. Originally posted by jotitadev on 2026-10-19T03:22:42Z.
Review: COMMENTED
Apple GPU / MoltenVK run. Metal's division is already correctly rounded on this test's inputs, so on Apple the change keeps the same values, and it also holds when MoltenVK's fast math is forced on.
Machine: Apple M2 Max, macOS 27.2, x86-64 build under Rosetta. Head
c402b9217, PR diff applied cleanly. Drivers: MoltenVK 1.4.2 (Vulkan SDK 1.4.363.0) and MoltenVK main + SPIRV-Cross #2704.Tested: the tree is
maina373e85e5+ mugurc's macOS series (ci/macos-series-2) + the PRs needed to build on macOS (#6667, #6668, #7524, #6969, and two files of #7332), not plainmain(it doesn't build on macOS yet). #7254 was applied in the same build (disjoint files). Base on that tree: 40^agcfailures with MoltenVK 1.4.2 and 26 with MoltenVK main + #2704, the same known failures in every run below.agc_driver_rcp_correctly_rounded,agc_driver_vop1_float_unaryandagc_driver_divisionpass 3 of 3 on both drivers.^agcsuite with the PR: 40 of 729 fail on MoltenVK 1.4.2 and 26 of 729 on MoltenVK main + #2704, the same tests as the base on each driver.SpirvAluEmitterMath.cppat the base) also passes on both drivers, 3 of 3, withANYPS5_NO_SHADER_CACHE=1. The module is a plainOpFDiv %float_1and declaresSignedZeroInfNanPreserve 32, so MoltenVK's default fast-math mode ("on demand") builds it without fast math, and Metal's precise1.0 / xgives the correctly rounded value on all inputs the test generates, all-ones significands included.MVK_CONFIG_FAST_MATH_ENABLED=1(forced on, not the default) the base fails:v_rcp_f32 of 0xb5b0e85a is 0xc9393a04, expected the correctly rounded 0xc9393a03. With this PR it passes on both drivers with fast math forced on.So Apple is a fourth vendor here, and so far the only one already correctly rounded on these inputs, which means this test can't catch a regression on a default Apple setup. If the target moves to RDNA's hardware values, as discussed above, Apple will need the emulation as well.
GPUs tested: Apple M2 Max (MoltenVK 1.4.2; MoltenVK main + SPIRV-Cross #2704)
Written with AI assistance (Claude Code). I ran the builds and tests above on my machine.
Imported from boykopovar/AnyPS5. Originally posted by tibzejoker on 2026-10-19T03:28:15Z.
Thanks @jotitadev, a fourth vendor is very useful. So on Apple the default build was already right, and only forcing fast math on breaks the base (
0xb5b0e85a->0xc9393a04instead of0xc9393a03), which the PR fixes.You're right that the test cannot catch a regression on a default Apple setup. It still catches one on NVIDIA and Intel (both fail on main, see the description) and on MoltenVK with fast math forced on, so I'll leave it as is. If the target moves to RDNA's hardware values later, the expected values change for every vendor, Apple included, and the test will cover all four.
Written with AI assistance (Claude Code).
Imported from boykopovar/AnyPS5. Originally posted by thuanquang on 2026-10-19T07:36:38Z.
@tibzejoker here is the sweep you asked for: native
v_rcp_f32for every input0x3f800000 | m, m = 0 .. 0x7fffff, on an RX 6700 XT (gfx1031, RDNA 2), through HIP (wave32, IEEE=1, denormals kept).Data:
rcp_f32_gfx1031_words.bin.xz(2^23 little-endian u32, the hardware word for each m, 2.3 MB) andrcp_f32_gfx1031_delta.bin.xz(2^23 signed bytes, hardware minus correctly rounded, 148 KB), on a release of my fork. The reference is round-to-nearest-even of2^47 / (2^23 + m)in integers, so it is exact.0x5b5acd(the mantissa of0x0f5b5acd) gives0x3f156235, where the correctly rounded word is0x3f156236, as on gfx1036 and gfx1100.0x9b4(exact 16772249.47 x 2^-24) comes out rounded up, and m =0x5a9(exact 16774318.5005 x 2^-24) rounded down.v_rcp_iflag_f32returned the same word asv_rcp_f32for all 2^23 inputs.Desktop RDNA 2, not the console's gfx1013.
Imported from boykopovar/AnyPS5. Originally posted by tibzejoker on 2026-10-19T07:42:04Z.
Thank you @thuanquang, this is exactly the data I asked for. I checked it here: the words file agrees with your delta file and with an integer correctly rounded reference (761,701 one ulp low, 135,974 one ulp high), and
0x5b5acdgives0x3f156235as on gfx1036/gfx1100.What the table shows about the hardware:
yby the rounding (h - 0.5 <= y <= h + 0.5ulp) gives a smooth error of about ±0.2 ulp that repeats in 32 segments of 2^18 mantissas (top 5 bits), with a cubic shape: the residual of a table-driven quadratic interpolation.So matching
v_rcp_f32bit for bit would mean recovering the integer coefficients and the widths of that datapath per segment. That is a reverse-engineering project of its own, and it could be a follow-up if someone wants it. This PR stays what its title says: correctly rounded on every device. It already agrees with the hardware on 89.3 % of inputs and is never more than one ulp away, as the hardware itself is from the exact value. Your table is the reference for that follow-up.AI-assisted: yes (Claude Code)
Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-19T16:55:12Z.
Review: CHANGES_REQUESTED
Thanks for the thorough testing and the exhaustive model. The blocker is the target, not the implementation:
v_rcp_f32/v_rcp_iflag_f32are not correctly rounded: the oracle runs in this thread (gfx1036, gfx1100, and @thuanquang's gfx1031 sweep) show 889/8192 inputs of this PR's own fixture one ulp off the rounded reference (0x0f5b5acdis0x6f956235on HW, the test expects...36).1/xto the nativev_rcp_f32, somainis already bit-identical to the console there. This PR makes AMD wrong on ~11% of inputs, andRcpCorrectlyRounded.cpp:101pins that wrong value as the expectation (Rounded()atRcpCorrectlyRounded.cpp:59).SpirvAluEmitterMath.cpp:919).What would fix it: emulate the hardware result (the quadratic-interpolation model you derived from @thuanquang's table), with the test expectations taken from the HW sweep (gfx1013 is expected to match gfx1031/1036/1100). Please include the mismatch count of the emulation against the full 2^23 sweep (0 expected) and the added instruction count. If the exact algorithm cannot be recovered, keep
mainand say so rather than pinning correct rounding.Imported from boykopovar/AnyPS5. Originally posted by tibzejoker on 2026-10-19T17:04:50Z.
Thanks @adriadam10, agreed: a deterministic value that is not the console's is not worth ~10 instructions per rcp, and pinning correct rounding in the test would make RADV wrong where
mainis already exact.I'll make one bounded attempt to recover the datapath from @thuanquang's full sweep. If it reaches 0 mismatches over the 2^23 inputs, I'll rewrite this PR around it, with the expectations taken from the sweep and the instruction count. If it doesn't, I'll close this PR and leave
mainas it is. Converting to draft meanwhile.AI-assisted: yes (Claude Code)
Imported from boykopovar/AnyPS5. Originally posted by tibzejoker on 2026-10-19T17:47:42Z.
Closing, as said above: the bounded attempt did not reach 0 mismatches, so
mainstays as it is (bit-identical to the console on RADV).What the gfx1031 sweep did show, for whoever picks this up later:
i = m >> 18,t = m & 0x3ffff), not a Newton step: a Newton-Raphson model needs 4x more slack than the polynomial.t >> 1: odd inputs sit lower than even ones by an amount linear int, which matches dropping the LSB of the squarer input.c0has 2 fractional bits (units of 1/4 ulp) and the final rounding is round to nearest even: in the 10 segments wherec0ends in .5, the result att = 0always goes to the even mantissa.t < 256and does not scale with the segment, so it most likely comes from a truncatedC1 * tmultiplier with an integer C1 that real-valued fits cannot pin down. With real coefficients, the best model still misses 66,054 of 2^23 inputs, against 897,675 for correct rounding.The next step would be an exhaustive search of the integer C1 per segment under truncated array-multiplier rules. Even then, an exact emulation would cost a 32-entry table and a cubic per rcp. Thanks @thuanquang for the sweep and everyone who ran the oracle.
AI-assisted: yes (Claude Code)
Imported from boykopovar/AnyPS5. Originally posted by tibzejoker on 2026-10-19T19:53:25Z.
Follow-up to the closing note: the datapath is now known exactly.
AMD's rocjitsu emulator (ROCm/rocm-systems,
emulation/rocjitsu/lib/util/include/util/amdgpu_rcp.h) describes the normalized FP32 reciprocal as captured on gfx1100 and gfx1201. It is the same shape as the one recovered above, with two parts that were missing here:C1 * tis rounded to nearest even before it is used. The shift is 14 bits, or 15 when the product is at least 2^38.I wrote that formula independently in Python and checked it against @thuanquang's gfx1031 sweep (all 2^23 significands). Result: 0 mismatches out of 8,388,608. The cubic alone misses exactly those 2,761 inputs. So RDNA 2 (gfx1031), RDNA 3 (gfx1100) and RDNA 4 (gfx1201) compute the same reciprocal, which very likely means the PS5 GPU does too.
That makes a bit-exact
v_rcp_f32possible on every vendor. Its cost:My plan would be:
v_rcp_f32(RADV);@adriadam10, would you take that as a new PR, or do you see a better place for it?
Imported from boykopovar/AnyPS5. Originally posted by tibzejoker on 2026-10-21T17:56:51Z.
Follow-up: the bit-exact reciprocal is now #9305, opt-in behind
APS5_EXACT_RCPso that the default output and SPIR-V size don't change. It matches all 2^23 mantissas of the gfx1031 sweep.AI-assisted: yes (Claude Code)
Pull request closed