fix(shader): choose unary f32/f16 special results on the bits, as RDNA does #537
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/537"
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-04T16:05:02Z.
What
The unary float ops
v_ceil,v_floor,v_trunc,v_rndne,v_fract,v_sqrt,v_rsq,v_rcp,v_log,v_exp,v_sin,v_cos(f32 and f16), plusv_rcp_iflag_f32andv_frexp_mant_f32, left their special results to the driver. Vulkan doesn't specify those results (NaN payloads,InverseSqrt/Log2at 0,FDivby 0 or inf), and on local drivers several were wrong:v_frexp_mant_f32), and lavapipe dropped the NaN sign and payload onlog/sin/cos, f32 and f16.log/sqrt/rsqof a negative,fract/sin/cosof inf) were+NaN(0x7fc00000) on lavapipe. The hardware gives-NaN(0xffc00000, f160xfe00).sinat whole and half cycles andcosat quarter cycles returned values like-8.7e-8where the hardware gives+0. The f16 path already had this rule.v_fract_f16(-2^-14)rounded to1.0. The hardware stops at0x3bff, as f32 stops at0x3f7fffff.A shared helper now picks these results on the bits, from the source bits after
neg/abs. Finite results still come from the driver's approximation.float16Trigis merged intofloat16Unary. f16 results go throughapplyF16ResultModifiersfrom #323, so clamp/omod behave as they do there.Behaviour measured on HW (RDNA2, gfx1035, IEEE=1, f32 denormals on)
| 0x400000, f16| 0x200), sign kept, for every op here.0xffc00000/0xfe00.rcp(±0) = ±inf,rcp(±inf) = ±0;rsq(±0) = ±inf,rsq(+inf) = +0;sqrt(±0) = ±0,sqrt(+inf) = +inf;log(±0) = -inf,log(1) = +0,log(+inf) = +inf;exp(±0) = 1,exp(x ≥ 128) = +inf,exp(x < -126) = +0(f32 denormal results are flushed, the same assumption as the existing denormal input flush of these ops);fract(-0) = +0;sin(±0) = ±0.v_rcp,v_rcp_iflag,v_rsq,v_sqrt,v_logandv_expcount as zero with their sign, in every mode (IEEE 0/1, denormals on/off):rcp/rsqgive ±inf,sqrt±0,log-inf (also for a negative denormal),exp1. f16 ops keep denormals.main.sin/coslose accuracy beyond about ±2^20 cycles (the input is converted with limited precision). This is recorded in TechnicalDebt, and the test keeps f32 trig inputs below 256 cycles.Tested
New execution test
agc_driver_vop1_float_unary_testsruns the 27 opcodes (15 f32, 12 f16) on two inputs per lane, 54 results × 32 rows. Rows were picked by class (sign, zero, inf, quiet/signaling NaN, invalid, magnitude bands, zero/inf results), plus fixed rows for sNaN, -inf/-0,fract(-2^-30)and f32 denormals (0x807fffff,0x007fffff). Expected values are what the hardware returns, and a Python model matches all of them. Exact ops and special results are compared bit for bit. Finite results of the approximate ops are compared with tolerances based on the Vulkan precision table: rcp/rsq/sqrt 2^-20 relative, log 2^-21 absolute or 2^-20 relative, exp (4+2|x|) ulp, f32 sin/cos 2^-11 absolute, f16 2^-9 relative or 2^-10 absolute for sin/cos. Denormal results are excluded, and the denormal row is only checked on the six ops that flush (on the others the result depends on the denormal mode).It passes on RADV, local lavapipe and CI's lavapipe (Mesa 25.2.8). The test fails with each of these changes: NaN not quieted or left to the driver (f32, f16), positive invalid NaN,
fractreaching 1 (f32 on lavapipe, f16 on both),sin/coscardinals off (lavapipe; RADV happens to return them exactly), sign ofsin(-0), andv_frexp_mant_f32NaN. The zero/inf pins for rcp/rsq/sqrt/log/exp already come out right on RADV and lavapipe, which is why they're picked on the bits for other drivers. Setting each pin to a wrong value fails the test, so every pin is covered (exceptexp(0), which falls inside the exp tolerance). Without the denormal flush the test fails (sqrt(-denormal)gives0xffc00000, and0x807fffffwith the sign kept but not flushed); RADV and lavapipe already flush theFDivinput ofrcp_iflag, andexpof a denormal rounds to 1 anyway, so those two can't fail locally.agc_driver_float16_result_modifiers_tests(#323) still passes. The full Release ctest passes exceptagc_driver_pixel_helper_lanes,agc_driver_global_load_addtidandagc_driver_vop3_carry_out(wave32 lane 32 on RADV's 64-wide subgroup), which also fail onmainon local RADV.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 oneandonlydean on 2026-10-04T21:19:16Z.
Thanks, the NaN, invalid, zero and inf picks match the ISA functional examples, and reading the f32 source as bits (so neg on a NaN keeps its sign) is a nice fix. Two things before it can go in:
EmitFlushF32DenormToSignedZero).unaryFloatSpecialstakeszeroandnegativefrom the unflushed bits, though. So a negative denormal gives 0xffc00000 for log, rsq and sqrt, where the hardware gives -inf, -inf and -0 (for sqrt that is a change from main, which gives -0). A positive denormal skips the rcp/rsq/log pins, so the result comes from FDiv/InverseSqrt/Log2 of 0, which is the undefined case the pins are there for. Could you takezero/negativefrom the flushed value for those five ops (magnitude below 0x00800000 counts as zero, sign kept), and add a denormal row for them? The f16 path is fine, since the f16 ops keep denormals.agc_driver_float16_result_modifiers(from #323) fails on my NVIDIA machine: "lane 7 sin_mul2 is 0x00000003, expected 0x00000006". Input 0x0001 gives sin(2pi * 2^-24), about 6 f16 denormal ulps. That value comes straight from the driver's sine, and NVIDIA's sine has an absolute error bound larger than the value itself (Vulkan only asks for 2^-11 absolute), so 0x0003 is a valid result there. Neither this commit nor #533 touches it. Earlier the test stopped at lane 4 (the NaN payload), which is why it didn't show up then. Could the sin columns use an input whose sine is well above that error (around 0.25 to 0.5, so mul:2 applied or ignored is still obvious) and compare with a tolerance like Vop1FloatUnary does? I'll leave the same note on #323.agc_driver_vop1_float_unaryitself passes on NVIDIA and fails on main there. This changes every unary float op PPSA21564 uses, so once it's updated I'll run it through my PPSA21564 check before merging.Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-04T21:42:35Z.
Thanks, both done (rebased on current main, last commit amended):
v_rcp,v_rcp_iflag,v_rsq,v_sqrt,v_logandv_expnow takezero/negativefrom the flushed value (magnitude below 0x00800000 counts as zero, sign kept), andsqrtof a zero returns the sign instead of the source bits. I measured it on the hardware first, in all four IEEE/denormal mode combinations:rcp/rsq±inf,sqrt±0,log-inf (also for a negative denormal),exp1, the same in every mode.v_rcp_iflag_f32flushes too, so it's included. New fixed row0x807fffff/0x007fffff, checked only on those six ops (on the others the result depends on the denormal mode). Without the fix the test fails on lane 0 (sqrt_f32gives 0xffc00000; with the sign but not the flush, 0x807fffff). The rcp_iflag and exp parts can't fail locally: RADV and lavapipe already flush theFDivinput, and exp2 of a denormal rounds to 1 anyway.While rerunning the oracle I also ran IEEE=0: everything matches except signaling NaNs, which come back unquieted there (113 cells). The PR keeps the IEEE=1 quieting, as
maindoes for the packed f16 ops. Noted in the description.Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-04T22:38:14Z.
Thanks, both points are fixed.
unaryFloatSpecialsnow takeszerofrom a magnitude below 0x00800000 for rcp, rcp_iflag, rsq, sqrt, log and exp, andnegativeexcludes it, so a negative denormal gives -inf for rsq and log and -0 for sqrt, and a positive one hits the rcp/rsq/log/exp pins instead of reaching the driver's undefined case. The f16 path is unaffected, since a widened f16 value is never an f32 denormal. The new0x807fffff/0x007fffffrow is checked only on those six ops (bits 14 to 25 ofChecked), which is the right scope, and #323's sine change comes through unchanged.agc_driver_vop1_float_unaryandagc_driver_float16_result_modifiersboth pass on my NVIDIA machine on current main, andagc_driver_vop1_float_unaryfails on main's code (lane 1ceil_f32gives 0x7fffffff where 0x7fc00001 is expected).One small request, not blocking: the description says the sNaN quieting is the IEEE=1 result and that IEEE=0 leaves 113 cells unquieted. Could that go into TechnicalDebt as a line like the one on #501? Main's unary f32 ops currently pass a signalling NaN through unquieted on RADV, so this is a change in behaviour that the recompiler can't check against the mode, and #508 went the other way (NaN unchanged, IEEE=0). A line makes the split visible until MODE is read.
This lands after #323. Since it changes every unary float op PPSA21564 uses, I'll run it through my PPSA21564 check together with #323 and merge both if it's clean.
Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-05T01:38:41Z.
Rebased on current main: the only conflict was the neighbouring TechnicalDebt line (
--to-intelRDPRU/MCOMMIT, now main's wording); the unary-ops line is unchanged. #323's three commits are patch-identical to #323's head.agc_driver_vop1_float_unary_testspasses on RADV and lavapipe, full ctest green on RADV apart from the knownagc_driver_pixel_helper_lanes.Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-05T02:47:13Z.
Thanks for the rebase. #323's three commits are patch-identical, the unary commit only changed in context, and the TechnicalDebt line is unchanged next to main's new
--to-intelwording.One merge-order detail: #323's own branch is still at
cc69286(the old base), while this branch carries rebased copies of those commits. If #323 is merged first as it stands, this PR then conflicts inFloatInstructions.cpp. Could you push the rebased commits to #323's branch too (6686bffis the one this branch carries)? Then #323 followed by this one merges cleanly. I checked that order locally.agc_driver_vop1_float_unarypasses here on the merged branch. These special results apply tov_rcp_f32,v_rsq_f32,v_sqrt_f32and friends, which PPSA21564 uses in nearly all of its shaders, so #323 and this one go through my PPSA21564 check before merging.Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-05T03:06:48Z.
Done: #323's branch is now at
6686bff, the same three commits this branch carries, so #323 followed by this one merges cleanly.Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-05T05:03:49Z.
Thanks. #323's branch is at
6686bffnow, so this branch is #323 plus one commit, and #323, then this one, then #567 merges cleanly at every step. The full ctest passes here with the three merged.These go through my PPSA21564 check before merging (
v_rcp_f32,v_rsq_f32,v_sqrt_f32and friends are in 5,470 of the 5,688 shaders I have dumped). Ready to merge from my side once that's done.Gated on PPSA21564 on top of #323, with #567: no visual or FPS change.