fix(shader): reduce v_sin_f32 and v_cos_f32 inputs exactly #1979
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/1979"
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 Deftera186 on 2026-10-09T03:39:02Z.
What
v_sin_f32andv_cos_f32take their input in cycles.EmitTrigCycleF32reduced it with GLSLFractbeforeEmitFPSinandEmitFPCosscale it by 2*pi. For x in (-0.5, 0),fract(x) = 1 + xis rounded to a multiple of 2^-24, which drops the low bits of x, and for |x| <= 2^-25 it rounds to 1.0, which RADV'sv_fract_f32clamps to 0x3f7fffff. On RADV,v_sin_f32(-FLT_MIN)returned 0xb4c90fde (-3.7e-7) where RDNA3 returns 0x81c90fd5 (-7.4e-38).The cycle is now
x - roundEven(x). It lies in [-0.5, 0.5] and is exact for every finite x: it is x whenroundEven(x)is 0, and Sterbenz's lemma applies otherwise. Finite inputs with |x| >= 2^23 still reduce to +0, so the select that forced them to 0 is removed. The ±0 select ofv_sin_f32(preserveSignedZero) is kept, and the results for infinite and NaN inputs are still selected on the input bits inunaryFloatSpecialsandfloatUnary.v_sin_f16andv_cos_f16go through the same reduction infloat16Unary. Every f16 value is a multiple of 2^-24, soFractwas already exact for them; what changes is that the driver gets x instead of 1 + x for x in (-0.5, 0). See Tested for the measured effect.Sources:
nir_normalize_sin_costurnsfsin(x)intofsin_normalized_2_pi(x * 0.15915493667125702). NIR reassociates the two constant multiplies; 0x3e22f983 * 0x40c90fdb rounds to 1.0f, andfmul(a, 1.0)becomesfcanonicalize(a). ACO selectsv_sin_f32/v_cos_f32,v_rndne_f32andv_fract_f32. So RADV ranv_sin_f32(v_fract_f32(x))before and runsv_sin_f32(x - v_rndne_f32(x))now:RADV_DEBUG=shaderson the new test showsv_rndne_f32andv_sub_f32(issued asv_dual_sub_f32) feedingv_sin_f32andv_cos_f32, with nov_fract_f32and no multiply. No NIR rule rewritesa - fround_even(a); onlya - ffloor(a)becomesffract.The new
SinCosReductionexecution test checksv_sin_f32andv_cos_f32on 32 inputs against RDNA3: negatives in (-0.5, 0) from -FLT_MIN to -0.48, positive controls, inputs beyond ±0.5, and ±0, -0.5, -0.25, -1, -(2^23 + 1), FLT_MAX and -inf. Zero, infinite and NaN results must match the bits; other results must be within 2^-10 relative, since the low bits are the driver's. That bound is tighter than the Vulkan precision of sin and cos (absolute 2^-11 in [-pi, pi]), so the test applies it only if the device meets it for both functions on the 5 rows with x in (0, 0.5), whose cycle is x before and after this change; otherwise it prints that and checks only the zero, infinite and NaN results. RADV and lavapipe meet it. Before the fix, 7v_sin_f32results on RADV are outside that bound, with relative errors from 0.33 to 5e30. Except at -0.5 and -0.25, whose zero results the translation sets on the bits, the inputs are at least 0.005 cycles from the zeros of sin at k + 0.5 and of cos at k ± 0.25 (k an integer). Close to those zeros the f32 product 2pix that GLSLSinandCostake is too coarse for that bound: within 4 ulp of ±0.25, ±0.5 and ±0.75, even the correctly rounded sin and cos of the product are up to 0.51 relative off after this change (1.06 before), and lavapipe stays within 6 ulp of them. RADV folds the multiply away.Vop1FloatUnarycompares sin and cos with the Vulkan bound, an absolute 2^-11, which the old errors pass (at most 3.75e-7 on the sweep below).docs/dev/TechnicalDebt.mdis unchanged: its sin/cos entry already says the low bits are the driver's.Tested
Gentoo Linux, RX 7700 XT (gfx1101), RADV (Mesa 26.2.3), Release build with
BUILD_TESTING=ON.agc_driver_sin_cos_reductionfails withAGC graphics: sin cos reduction: lane 0 v_sin_f32 of 0x80800000 is 0xb4c90fde, expected near 0x81c90fd5.agc_driver_sin_cos_reductionpasses, with all 64 results equal to the hardware bits. Full suite:100% tests passed, 0 tests failed out of 495.mesa-vulkan-drivers25.2.8-0ubuntu0.24.04.4, llvmpipe with LLVM 20.1.2,GALLIUM_OVERRIDE_CPU_CAPS=avx,ANYPS5_ENABLE_SPIRV_TOOLS=ON):agc_driver_sin_cos_reductionandagc_driver_vop1_float_unarypass; the largest relative error on the test's inputs is 9.5e-7. Before the fix the new test fails there too, withlane 0 v_sin_f32 of 0x80800000 is 0x343bbd2e, expected near 0x81c90fd5.Hardware oracle on gfx1101. Every row below, and every row of the test, gives the same bits in all 16 combinations of ieee 0/1, denorm32 0/3, denorm16 0/3 and dx10_clamp 0/1 (round modes 0, fp16_overflow 0):
0x80800000(-1.17549e-38)v_sin_f320x81c90fd5(-7.38585e-38)0xb4c90fde(-3.74507e-07)0x81c90fd50x8f800000(-1.26218e-29)v_sin_f320x90c90fd5(-7.93049e-29)0xb4c90fde(-3.74507e-07)0x90c90fd50xa3d42337(-2.3e-17)v_sin_f320xa5269cc5(-1.44513e-16)0xb4c90fde(-3.74507e-07)0xa5269cc50xb0800000(-9.31323e-10)v_sin_f320xb1c90fd5(-5.85167e-09)0xb4c90fde(-3.74507e-07)0xb1c90fd50xb2800000(-1.49012e-08)v_sin_f320xb3c90fd5(-9.36267e-08)0xb4c90fde(-3.74507e-07)0xb3c90fd50xb3000000(-2.98023e-08)v_sin_f320xb4490fd5(-1.87253e-07)0xb4c90fde(-3.74507e-07)0xb4490fd50xb3800000(-5.96046e-08)v_sin_f320xb4c90fd5(-3.74507e-07)0xb4c90fde(-3.74507e-07)0xb4c90fd50xb3c00000(-8.9407e-08)v_sin_f320xb516cbe0(-5.6176e-07)0xb5490fdf(-7.49014e-07)0xb516cbe00xb8732b55(-5.79761e-05)v_sin_f320xb9befc10(-0.000364274)0xb9bf0c55(-0.000364395)0xb9befc100xbc23d70a(-0.01)v_sin_f320xbd80984d(-0.0627905)0xbd809845(-0.0627904)0xbd80984d0xbef5c28f(-0.48)v_sin_f320xbe005760(-0.125333)0xbe005753(-0.125333)0xbe0057600xbe82a4d7(-0.255164)v_cos_f320xbd04dfba(-0.0324399)0xbd04dfed(-0.0324401)0xbd04dfbaSweep of 544 inputs: 64 tiny negatives (-FLT_MIN to -2^-24) and their absolute values, 64 in (-0.5, 0) and their absolute values, 32 each in (-1, -0.5) and (0.5, 1), 64 each in [-256, -1] and [1, 256], 64 with random sign and magnitude in [2^-27, 256), and the values 1 and 4 ulp on either side of ±0.25, ±0.5, ±0.75 and ±1. The hardware results are the same in all 16 combinations. The recompiler rows come from a temporary execution test that is not part of this PR. Counts are results equal to hardware
v_sin_f32(x)orv_cos_f32(x), then results more than 16 ulp off:v_*(x - v_rndne_f32(x))v_*(v_fract_f32(x))On RADV the recompiled shader returns the bits of the
v_fract_f32sequence before this change and of thev_rndne_f32sequence after it, for all 544 inputs. Every row more than 16 ulp off withv_fract_f32has x in (-0.5, 0). The lavapipe rows more than 16 ulp off after the fix all have x within 0.017 of a zero of sin at k + 0.5 or of cos at k ± 0.25.v_sin_f16andv_cos_f16, all 65536 inputs through the recompiler on RADV against hardware with f16 denormals kept (denorm16 3, the mode the recompiler assumes without a knownFLOAT_MODE):v_sin_f16now returns the hardware bits for all of them, where it was 1 ulp off for the 6 below before, andv_cos_f16matches all of them before and after. With denorm16 0 the hardware flushes the two subnormal inputs and returns 0x8000; the other four rows are the same in all 16 combinations.0x8317(-4.71473e-05)v_sin_f160x8cda0x8cdb0x8cda0x83f9(-6.06179e-05)v_sin_f160x8e3d0x8e3e0x8e3d0x84db(-7.40886e-05)v_sin_f160x8fa00x8fa10x8fa00x862e(-9.42945e-05)v_sin_f160x90da0x90db0x90da0x87f2(-0.000121236)v_sin_f160x923d0x923e0x923d0x88db(-0.000148177)v_sin_f160x93a00x93a10x93a0Checklist
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 CouchRiv on 2026-10-09T05:54:24Z.
Measured on RDNA2 (gfx1036, Radeon 610M) with
tools/hw-oracle, wave32, round to nearest, in IEEE=0 / f32 denormals flushed / f16 denormals kept and in IEEE=1 / all denormals kept:v_sin_f32andv_cos_f32on the 32 inputs ofSinCosReductiongive exactly the values in itsRowstable, 64 of 64 in both modes (for example0x80800000gives sin 0x81c90fd5,0xbe82a4d7gives cos 0xbd04dfba).v_sin/v_cos(x - v_rndne_f32(x))equalsv_sin/v_cos(x)on 31 of 32 inputs; the one difference is -0.0, which gives +0.v_sin/v_cos(v_fract_f32(x))matches on only 17. The sixv_sin_f16rows also match (0x8cda, 0x8e3d, 0x8fa0, 0x90da, 0x923d, 0x93a0). With f16 denormals flushed the two subnormal inputs give 0x8000, as you describe. So RDNA2 agrees with your RDNA3 values. One note: on an RTX 4070 Ti,mainreturns 0x00000000 forv_sin_f32of 0x00800000 and 0x30800000, so there the test will take its exact-only path.Imported from boykopovar/AnyPS5. Originally posted by johnmaia on 2026-10-09T08:59:48Z.
NVIDIA and Intel results on Windows, to go with the RADV runs.
Windows 11, WinLibs GCC 15.2.0 r7, Release,
BUILD_TESTING=ON, fullctestwithANYPS5_REQUIRE_VULKAN=1on both GPUs of one machine: RTX 4080 (driver 617.42) and Intel UHD Graphics 770 (Raptor Lake-S, driver 101.6129, Vulkan 1.3.295), selected withANYPS5_GPU. Base:main6cc0503dplus #2093, sincemainalone does not build its tests right now (spirv/unified1/spirv.hppnot found).agc_driver_sin_cos_reductionpasses on both GPUs. The five UHD 770 failures are the same tests with and without this PR (vop3_mad_i64, #1246, plus four filed as #2114), so nothing new fails.Imported from boykopovar/AnyPS5. Originally posted by popcoder565 on 2026-10-09T09:26:07Z.
NVIDIA data, since the description covers RADV, lavapipe and RDNA3. Setup: Windows 10 (19045), RTX 3060 Laptop (NVIDIA 616.92), GCC 15.2.0 (WinLibs r7), Release. Head
e6eb64aamerged ontomaind9484c71, together with #1963 and #1966.agc_driver_sin_cos_reductionpasses, but in its reduced mode:the device's sin and cos are not within 2^-10 of the hardware for small positive inputs. NVIDIA's sin seems to have an absolute error of about 2^-22 for small arguments: small results come out as multiples of about 1.9e-7 or as 0. That is within Vulkan's bound but fails the relative check on the control rows. I printed every row, with this PR and onmainwith the same test file:main0x80800000)0x81c90fd50xb4491200(-1.9e-7)0x80000000(-0)0xb2800000)0xb3c90fd50xb44912000x800000000xb3800000)0xb4c90fd5(-3.7e-7)0xb516cd80(-5.6e-7)0xb4491200(-1.9e-7)0x33800000)0x34c90fd50x344912000x344912000xb4491200onmainand-0with this PR, like the positive side. Rows 6–7 move from about 1.5× to about 0.5× the hardware value.So the fix changes NVIDIA's results in the expected direction, but what's left is the device's own error, and the test can't tell the two apart there.
🤖 Addressed by Claude Code
Imported from boykopovar/AnyPS5. Originally posted by SleglJan on 2026-10-09T16:56:50Z.
Measured on RDNA2 (gfx1036, Ryzen 7950X3D iGPU) with
tools/hw-oracleon Linux (CachyOS kernel 7.2.9, hsa-rocr 7.2.4, clang 23.1.1 as the assembler; IEEE 0, f32 denormals flushed, f16 kept, DX10_CLAMP 1, RNE), then the same inputs through the recompiler atmaind70b8998and ate6eb64aaon two hosts of that machine, RADV (Mesa 26.2.4, the iGPU) and an RTX 3080 (615.71.09): every f16 input, and 32,832 f32 inputs (every exponent with 64 mantissas of both signs, the specials, cycle counts around 256 and beyond). Results equal to the hardware bits, then the largest ULP distance:mainmainv_sin_f16v_cos_f16v_sin_f32v_cos_f32agc_driver_sin_cos_reductionpasses on RADV and takes its reduced path on the RTX 3080. On RADV the 15 ULP maximum is reached only beyond ±256 cycles (0xc7ffffff gives 0x3d48fb28 for the hardware's 0x3d48fb37), where the hardware's own reduction differs from the exact fraction. On the RTX 3080 this PR raises the zero-for-finite sines from 6,466 to 12,930 (the tiny negatives that fract used to send through a full turn now reach the driver's sine as they are); what remains is the driver's: its sine's absolute error (popcoder565's observation above) turns the reduced tiny cycles into zeros (the 12,930; the hardware returns x × 6.2831826, 0x40c90fd5, for every positive input below 2^-12 of a turn), and the f32 radians near the zeros drop the low bits (v_cos_f32of 0x3e7fffff is 0 for the hardware's 0x33c90fd9). Returning the linear term of the exact distance to the zero within 2^-12 of a turn, on top of this PR, brings the RTX 3080 to 29,456 sine and 29,092 cosine results equal to the hardware with no zero for a finite value (max 1,166 and 163 ULP, the driver's own just outside the band) and moves 4 of the 32,832 RADV results, by at most 2 ULP: the follow-up is #2380. Data: https://github.com/SleglJan/AnyPS5/tree/audit/oracle-2026-10-08/oracle-audit/transcendentals.AI-assisted: yes (Claude Code).
Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-10T06:06:43Z.
Checked the 32 rows of
SinCosReductionon the hardware oracle (RDNA2, IEEE 0/1 × f32 denormals flushed/kept: 64/64 bit-identical), plus ±inf, NaN, ±2^23 and the half/quarter cycles against the existing special-case selects; the new test passes on RADV and on CI's lavapipe in its strict mode and fails without the change. Merging.