fix(shader): reduce v_sin_f32 and v_cos_f32 inputs exactly #1979

Merged
Deftera186 merged 1 commit from upstream-pr/1979 into main 2026-10-10 06:06:47 +00:00
Deftera186 commented 2026-10-09 03:39:02 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Deftera186 on 2026-10-09T03:39:02Z.


What

v_sin_f32 and v_cos_f32 take their input in cycles. EmitTrigCycleF32 reduced it with GLSL Fract before EmitFPSin and EmitFPCos scale it by 2*pi. For x in (-0.5, 0), fract(x) = 1 + x is 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's v_fract_f32 clamps 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 when roundEven(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 of v_sin_f32 (preserveSignedZero) is kept, and the results for infinite and NaN inputs are still selected on the input bits in unaryFloatSpecials and floatUnary.

v_sin_f16 and v_cos_f16 go through the same reduction in float16Unary. Every f16 value is a multiple of 2^-24, so Fract was 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:

  • Hardware: RDNA3 (gfx1101) with the hardware oracle, see Tested. Not measured on gfx1013 or RDNA2.
  • RADV, Mesa 26.2.3 (31e9a6b2): nir_normalize_sin_cos turns fsin(x) into fsin_normalized_2_pi(x * 0.15915493667125702). NIR reassociates the two constant multiplies; 0x3e22f983 * 0x40c90fdb rounds to 1.0f, and fmul(a, 1.0) becomes fcanonicalize(a). ACO selects v_sin_f32/v_cos_f32, v_rndne_f32 and v_fract_f32. So RADV ran v_sin_f32(v_fract_f32(x)) before and runs v_sin_f32(x - v_rndne_f32(x)) now: RADV_DEBUG=shaders on the new test shows v_rndne_f32 and v_sub_f32 (issued as v_dual_sub_f32) feeding v_sin_f32 and v_cos_f32, with no v_fract_f32 and no multiply. No NIR rule rewrites a - fround_even(a); only a - ffloor(a) becomes ffract.

The new SinCosReduction execution test checks v_sin_f32 and v_cos_f32 on 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, 7 v_sin_f32 results 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 GLSL Sin and Cos take 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. Vop1FloatUnary compares 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.md is 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.

  • Before the fix: agc_driver_sin_cos_reduction fails with AGC graphics: sin cos reduction: lane 0 v_sin_f32 of 0x80800000 is 0xb4c90fde, expected near 0x81c90fd5.
  • After: agc_driver_sin_cos_reduction passes, with all 64 results equal to the hardware bits. Full suite: 100% tests passed, 0 tests failed out of 495.
  • lavapipe, in an Ubuntu 24.04 container set up like the CI job (mesa-vulkan-drivers 25.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_reduction and agc_driver_vop1_float_unary pass; the largest relative error on the test's inputs is 9.5e-7. Before the fix the new test fails there too, with lane 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):

x hardware before (RADV) after (RADV)
0x80800000 (-1.17549e-38) v_sin_f32 0x81c90fd5 (-7.38585e-38) 0xb4c90fde (-3.74507e-07) 0x81c90fd5
0x8f800000 (-1.26218e-29) v_sin_f32 0x90c90fd5 (-7.93049e-29) 0xb4c90fde (-3.74507e-07) 0x90c90fd5
0xa3d42337 (-2.3e-17) v_sin_f32 0xa5269cc5 (-1.44513e-16) 0xb4c90fde (-3.74507e-07) 0xa5269cc5
0xb0800000 (-9.31323e-10) v_sin_f32 0xb1c90fd5 (-5.85167e-09) 0xb4c90fde (-3.74507e-07) 0xb1c90fd5
0xb2800000 (-1.49012e-08) v_sin_f32 0xb3c90fd5 (-9.36267e-08) 0xb4c90fde (-3.74507e-07) 0xb3c90fd5
0xb3000000 (-2.98023e-08) v_sin_f32 0xb4490fd5 (-1.87253e-07) 0xb4c90fde (-3.74507e-07) 0xb4490fd5
0xb3800000 (-5.96046e-08) v_sin_f32 0xb4c90fd5 (-3.74507e-07) 0xb4c90fde (-3.74507e-07) 0xb4c90fd5
0xb3c00000 (-8.9407e-08) v_sin_f32 0xb516cbe0 (-5.6176e-07) 0xb5490fdf (-7.49014e-07) 0xb516cbe0
0xb8732b55 (-5.79761e-05) v_sin_f32 0xb9befc10 (-0.000364274) 0xb9bf0c55 (-0.000364395) 0xb9befc10
0xbc23d70a (-0.01) v_sin_f32 0xbd80984d (-0.0627905) 0xbd809845 (-0.0627904) 0xbd80984d
0xbef5c28f (-0.48) v_sin_f32 0xbe005760 (-0.125333) 0xbe005753 (-0.125333) 0xbe005760
0xbe82a4d7 (-0.255164) v_cos_f32 0xbd04dfba (-0.0324399) 0xbd04dfed (-0.0324401) 0xbd04dfba

Sweep 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) or v_cos_f32(x), then results more than 16 ulp off:

sin equal sin > 16 ulp cos equal cos > 16 ulp
hardware v_*(x - v_rndne_f32(x)) 519 (max 11 ulp off) 0 523 (max 7 ulp off) 0
hardware v_*(v_fract_f32(x)) 403 80 484 6
recompiler before, RADV 403 80 484 6
recompiler after, RADV 519 0 523 0
recompiler before, lavapipe 107 111 290 30
recompiler after, lavapipe 121 24 302 23

On RADV the recompiled shader returns the bits of the v_fract_f32 sequence before this change and of the v_rndne_f32 sequence after it, for all 544 inputs. Every row more than 16 ulp off with v_fract_f32 has 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_f16 and v_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 known FLOAT_MODE): v_sin_f16 now returns the hardware bits for all of them, where it was 1 ulp off for the 6 below before, and v_cos_f16 matches 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.

x hardware before (RADV) after (RADV)
0x8317 (-4.71473e-05) v_sin_f16 0x8cda 0x8cdb 0x8cda
0x83f9 (-6.06179e-05) v_sin_f16 0x8e3d 0x8e3e 0x8e3d
0x84db (-7.40886e-05) v_sin_f16 0x8fa0 0x8fa1 0x8fa0
0x862e (-9.42945e-05) v_sin_f16 0x90da 0x90db 0x90da
0x87f2 (-0.000121236) v_sin_f16 0x923d 0x923e 0x923d
0x88db (-0.000148177) v_sin_f16 0x93a0 0x93a1 0x93a0

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:4795671886 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1979). Originally posted by [Deftera186](https://github.com/Deftera186) on 2026-10-09T03:39:02Z. --- ### What `v_sin_f32` and `v_cos_f32` take their input in cycles. [`EmitTrigCycleF32`](https://github.com/boykopovar/AnyPS5/blob/f145139676a48306681d3a4d5fabf83938759daf/core/shader/recompiler/SpirvBackend/src/SpirvModuleEmitter.cpp#L870-L893) reduced it with GLSL `Fract` before [`EmitFPSin` and `EmitFPCos`](https://github.com/boykopovar/AnyPS5/blob/f145139676a48306681d3a4d5fabf83938759daf/core/shader/recompiler/SpirvBackend/src/SpirvAlu/SpirvAluEmitterMath.cpp#L681-L691) scale it by 2*pi. For x in (-0.5, 0), `fract(x) = 1 + x` is 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's `v_fract_f32` clamps 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 when `roundEven(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 of `v_sin_f32` (`preserveSignedZero`) is kept, and the results for infinite and NaN inputs are still selected on the input bits in [`unaryFloatSpecials`](https://github.com/boykopovar/AnyPS5/blob/f145139676a48306681d3a4d5fabf83938759daf/core/shader/recompiler/Translation/src/FloatInstructions.cpp#L149-L164) and [`floatUnary`](https://github.com/boykopovar/AnyPS5/blob/f145139676a48306681d3a4d5fabf83938759daf/core/shader/recompiler/Translation/src/FloatInstructions.cpp#L494-L503). `v_sin_f16` and `v_cos_f16` go through the same reduction in [`float16Unary`](https://github.com/boykopovar/AnyPS5/blob/f145139676a48306681d3a4d5fabf83938759daf/core/shader/recompiler/Translation/src/FloatInstructions.cpp#L85-L99). Every f16 value is a multiple of 2^-24, so `Fract` was 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: - Hardware: RDNA3 (gfx1101) with the hardware oracle, see Tested. Not measured on gfx1013 or RDNA2. - RADV, Mesa 26.2.3 (31e9a6b2): [`nir_normalize_sin_cos`](https://gitlab.freedesktop.org/mesa/mesa/-/blob/31e9a6b2e95e30d84bf3177d1f497d063e59b6b2/src/compiler/nir/nir_normalize_sin_cos.c#L19-20) turns `fsin(x)` into `fsin_normalized_2_pi(x * 0.15915493667125702)`. NIR [reassociates the two constant multiplies](https://gitlab.freedesktop.org/mesa/mesa/-/blob/31e9a6b2e95e30d84bf3177d1f497d063e59b6b2/src/compiler/nir/nir_opt_algebraic.py#L2403-2406); 0x3e22f983 * 0x40c90fdb rounds to 1.0f, and [`fmul(a, 1.0)`](https://gitlab.freedesktop.org/mesa/mesa/-/blob/31e9a6b2e95e30d84bf3177d1f497d063e59b6b2/src/compiler/nir/nir_opt_algebraic.py#L292) becomes `fcanonicalize(a)`. ACO selects [`v_sin_f32`/`v_cos_f32`](https://gitlab.freedesktop.org/mesa/mesa/-/blob/31e9a6b2e95e30d84bf3177d1f497d063e59b6b2/src/amd/compiler/instruction_selection/aco_select_nir_alu.cpp#L2508-2538), [`v_rndne_f32`](https://gitlab.freedesktop.org/mesa/mesa/-/blob/31e9a6b2e95e30d84bf3177d1f497d063e59b6b2/src/amd/compiler/instruction_selection/aco_select_nir_alu.cpp#L2448-2452) and [`v_fract_f32`](https://gitlab.freedesktop.org/mesa/mesa/-/blob/31e9a6b2e95e30d84bf3177d1f497d063e59b6b2/src/amd/compiler/instruction_selection/aco_select_nir_alu.cpp#L2375-2379). So RADV ran `v_sin_f32(v_fract_f32(x))` before and runs `v_sin_f32(x - v_rndne_f32(x))` now: `RADV_DEBUG=shaders` on the new test shows `v_rndne_f32` and `v_sub_f32` (issued as `v_dual_sub_f32`) feeding `v_sin_f32` and `v_cos_f32`, with no `v_fract_f32` and no multiply. No NIR rule rewrites `a - fround_even(a)`; only [`a - ffloor(a)`](https://gitlab.freedesktop.org/mesa/mesa/-/blob/31e9a6b2e95e30d84bf3177d1f497d063e59b6b2/src/compiler/nir/nir_opt_algebraic.py#L549-551) becomes `ffract`. The new `SinCosReduction` execution test checks `v_sin_f32` and `v_cos_f32` on 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, 7 `v_sin_f32` results 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 2*pi*x that GLSL `Sin` and `Cos` take 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. `Vop1FloatUnary` compares 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.md` is 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`. - Before the fix: `agc_driver_sin_cos_reduction` fails with `AGC graphics: sin cos reduction: lane 0 v_sin_f32 of 0x80800000 is 0xb4c90fde, expected near 0x81c90fd5`. - After: `agc_driver_sin_cos_reduction` passes, with all 64 results equal to the hardware bits. Full suite: `100% tests passed, 0 tests failed out of 495`. - lavapipe, in an Ubuntu 24.04 container set up like the CI job (`mesa-vulkan-drivers` 25.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_reduction` and `agc_driver_vop1_float_unary` pass; the largest relative error on the test's inputs is 9.5e-7. Before the fix the new test fails there too, with `lane 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): | x | | hardware | before (RADV) | after (RADV) | |---|---|---|---|---| | `0x80800000` (-1.17549e-38) | `v_sin_f32` | `0x81c90fd5` (-7.38585e-38) | `0xb4c90fde` (-3.74507e-07) | `0x81c90fd5` | | `0x8f800000` (-1.26218e-29) | `v_sin_f32` | `0x90c90fd5` (-7.93049e-29) | `0xb4c90fde` (-3.74507e-07) | `0x90c90fd5` | | `0xa3d42337` (-2.3e-17) | `v_sin_f32` | `0xa5269cc5` (-1.44513e-16) | `0xb4c90fde` (-3.74507e-07) | `0xa5269cc5` | | `0xb0800000` (-9.31323e-10) | `v_sin_f32` | `0xb1c90fd5` (-5.85167e-09) | `0xb4c90fde` (-3.74507e-07) | `0xb1c90fd5` | | `0xb2800000` (-1.49012e-08) | `v_sin_f32` | `0xb3c90fd5` (-9.36267e-08) | `0xb4c90fde` (-3.74507e-07) | `0xb3c90fd5` | | `0xb3000000` (-2.98023e-08) | `v_sin_f32` | `0xb4490fd5` (-1.87253e-07) | `0xb4c90fde` (-3.74507e-07) | `0xb4490fd5` | | `0xb3800000` (-5.96046e-08) | `v_sin_f32` | `0xb4c90fd5` (-3.74507e-07) | `0xb4c90fde` (-3.74507e-07) | `0xb4c90fd5` | | `0xb3c00000` (-8.9407e-08) | `v_sin_f32` | `0xb516cbe0` (-5.6176e-07) | `0xb5490fdf` (-7.49014e-07) | `0xb516cbe0` | | `0xb8732b55` (-5.79761e-05) | `v_sin_f32` | `0xb9befc10` (-0.000364274) | `0xb9bf0c55` (-0.000364395) | `0xb9befc10` | | `0xbc23d70a` (-0.01) | `v_sin_f32` | `0xbd80984d` (-0.0627905) | `0xbd809845` (-0.0627904) | `0xbd80984d` | | `0xbef5c28f` (-0.48) | `v_sin_f32` | `0xbe005760` (-0.125333) | `0xbe005753` (-0.125333) | `0xbe005760` | | `0xbe82a4d7` (-0.255164) | `v_cos_f32` | `0xbd04dfba` (-0.0324399) | `0xbd04dfed` (-0.0324401) | `0xbd04dfba` | Sweep 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)` or `v_cos_f32(x)`, then results more than 16 ulp off: | | sin equal | sin > 16 ulp | cos equal | cos > 16 ulp | |---|---|---|---|---| | hardware `v_*(x - v_rndne_f32(x))` | 519 (max 11 ulp off) | 0 | 523 (max 7 ulp off) | 0 | | hardware `v_*(v_fract_f32(x))` | 403 | 80 | 484 | 6 | | recompiler before, RADV | 403 | 80 | 484 | 6 | | recompiler after, RADV | 519 | 0 | 523 | 0 | | recompiler before, lavapipe | 107 | 111 | 290 | 30 | | recompiler after, lavapipe | 121 | 24 | 302 | 23 | On RADV the recompiled shader returns the bits of the `v_fract_f32` sequence before this change and of the `v_rndne_f32` sequence after it, for all 544 inputs. Every row more than 16 ulp off with `v_fract_f32` has 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_f16` and `v_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 known `FLOAT_MODE`): `v_sin_f16` now returns the hardware bits for all of them, where it was 1 ulp off for the 6 below before, and `v_cos_f16` matches 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. | x | | hardware | before (RADV) | after (RADV) | |---|---|---|---|---| | `0x8317` (-4.71473e-05) | `v_sin_f16` | `0x8cda` | `0x8cdb` | `0x8cda` | | `0x83f9` (-6.06179e-05) | `v_sin_f16` | `0x8e3d` | `0x8e3e` | `0x8e3d` | | `0x84db` (-7.40886e-05) | `v_sin_f16` | `0x8fa0` | `0x8fa1` | `0x8fa0` | | `0x862e` (-9.42945e-05) | `v_sin_f16` | `0x90da` | `0x90db` | `0x90da` | | `0x87f2` (-0.000121236) | `v_sin_f16` | `0x923d` | `0x923e` | `0x923d` | | `0x88db` (-0.000148177) | `v_sin_f16` | `0x93a0` | `0x93a1` | `0x93a0` | ### 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
CouchRiv commented 2026-10-09 05:54:24 +00:00 (Migrated from github.com)

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_f32 and v_cos_f32 on the 32 inputs of SinCosReduction give exactly the values in its Rows table, 64 of 64 in both modes (for example 0x80800000 gives sin 0x81c90fd5, 0xbe82a4d7 gives cos 0xbd04dfba). v_sin/v_cos(x - v_rndne_f32(x)) equals v_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 six v_sin_f16 rows 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, main returns 0x00000000 for v_sin_f32 of 0x00800000 and 0x30800000, so there the test will take its exact-only path.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6075201676 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1979#issuecomment-6075201676). Originally posted by [CouchRiv](https://github.com/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_f32` and `v_cos_f32` on the 32 inputs of `SinCosReduction` give exactly the values in its `Rows` table, 64 of 64 in both modes (for example `0x80800000` gives sin 0x81c90fd5, `0xbe82a4d7` gives cos 0xbd04dfba). `v_sin/v_cos(x - v_rndne_f32(x))` equals `v_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 six `v_sin_f16` rows 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, `main` returns 0x00000000 for `v_sin_f32` of 0x00800000 and 0x30800000, so there the test will take its exact-only path.
johnmaia commented 2026-10-09 08:59:48 +00:00 (Migrated from github.com)

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, full ctest with ANYPS5_REQUIRE_VULKAN=1 on 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 with ANYPS5_GPU. Base: main 6cc0503d plus #2093, since main alone does not build its tests right now (spirv/unified1/spirv.hpp not found).

RTX 4080 UHD 770
base 494/494 489/494
base + this PR 495/495 490/495

agc_driver_sin_cos_reduction passes 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.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6077780040 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1979#issuecomment-6077780040). Originally posted by [johnmaia](https://github.com/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`, full `ctest` with `ANYPS5_REQUIRE_VULKAN=1` on 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 with `ANYPS5_GPU`. Base: `main` `6cc0503d` plus #2093, since `main` alone does not build its tests right now (`spirv/unified1/spirv.hpp` not found). | | RTX 4080 | UHD 770 | |---|---|---| | base | 494/494 | 489/494 | | base + this PR | 495/495 | 490/495 | `agc_driver_sin_cos_reduction` passes 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.
popcoder565 commented 2026-10-09 09:26:07 +00:00 (Migrated from github.com)

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 e6eb64aa merged onto main d9484c71, together with #1963 and #1966.

agc_driver_sin_cos_reduction passes, 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 on main with the same test file:

x hardware NVIDIA, main NVIDIA, this PR
-FLT_MIN (0x80800000) 0x81c90fd5 0xb4491200 (-1.9e-7) 0x80000000 (-0)
-2^-26 (0xb2800000) 0xb3c90fd5 0xb4491200 0x80000000
-2^-24 (0xb3800000) 0xb4c90fd5 (-3.7e-7) 0xb516cd80 (-5.6e-7) 0xb4491200 (-1.9e-7)
2^-24 (0x33800000) 0x34c90fd5 0x34491200 0x34491200
  • Rows 0–5 (the tiny negatives) all read 0xb4491200 on main and -0 with this PR, like the positive side. Rows 6–7 move from about 1.5× to about 0.5× the hardware value.
  • The other rows are within 2^-10 of the hardware with and without the change, and the zero, infinite and NaN results match the bits.

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

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6078179471 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1979#issuecomment-6078179471). Originally posted by [popcoder565](https://github.com/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 `e6eb64aa` merged onto `main` d9484c71, together with #1963 and #1966. `agc_driver_sin_cos_reduction` passes, 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 on `main` with the same test file: | x | hardware | NVIDIA, `main` | NVIDIA, this PR | |---|---|---|---| | -FLT_MIN (`0x80800000`) | `0x81c90fd5` | `0xb4491200` (-1.9e-7) | `0x80000000` (-0) | | -2^-26 (`0xb2800000`) | `0xb3c90fd5` | `0xb4491200` | `0x80000000` | | -2^-24 (`0xb3800000`) | `0xb4c90fd5` (-3.7e-7) | `0xb516cd80` (-5.6e-7) | `0xb4491200` (-1.9e-7) | | 2^-24 (`0x33800000`) | `0x34c90fd5` | `0x34491200` | `0x34491200` | - Rows 0–5 (the tiny negatives) all read `0xb4491200` on `main` and `-0` with this PR, like the positive side. Rows 6–7 move from about 1.5× to about 0.5× the hardware value. - The other rows are within 2^-10 of the hardware with and without the change, and the zero, infinite and NaN results match the bits. 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](https://claude.com/claude-code)_
SleglJan commented 2026-10-09 16:56:50 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by SleglJan on 2026-10-09T16:56:50Z.


Measured on RDNA2 (gfx1036, Ryzen 7950X3D iGPU) with tools/hw-oracle on 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 at main d70b8998 and at e6eb64aa on 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:

RADV, main RADV, this PR RTX 3080, main RTX 3080, this PR
v_sin_f16 65,530 of 65,536, max 1 65,536 62,395, max 4 63,086, max 4
v_cos_f16 65,536 65,536 65,509, max 1 65,512, max 1
v_sin_f32 24,529 of 32,832, max 855,638,025 32,424, max 15 14,752, max 847,249,963, 6,466 zero for a finite value 14,864, max 8,388,062, 12,930 zero for a finite value
v_cos_f32 32,285, max 202 32,488, max 15 28,864, max 1,746,018,777 29,092, max 163, 2 zero for a finite value

agc_driver_sin_cos_reduction passes 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_f32 of 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).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6085413402 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1979#issuecomment-6085413402). Originally posted by [SleglJan](https://github.com/SleglJan) on 2026-10-09T16:56:50Z. --- Measured on RDNA2 (gfx1036, Ryzen 7950X3D iGPU) with `tools/hw-oracle` on 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 at `main` d70b8998 and at e6eb64aa on 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: | | RADV, `main` | RADV, this PR | RTX 3080, `main` | RTX 3080, this PR | |---|---|---|---|---| | `v_sin_f16` | 65,530 of 65,536, max 1 | 65,536 | 62,395, max 4 | 63,086, max 4 | | `v_cos_f16` | 65,536 | 65,536 | 65,509, max 1 | 65,512, max 1 | | `v_sin_f32` | 24,529 of 32,832, max 855,638,025 | 32,424, max 15 | 14,752, max 847,249,963, 6,466 zero for a finite value | 14,864, max 8,388,062, 12,930 zero for a finite value | | `v_cos_f32` | 32,285, max 202 | 32,488, max 15 | 28,864, max 1,746,018,777 | 29,092, max 163, 2 zero for a finite value | `agc_driver_sin_cos_reduction` passes 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_f32` of 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).
adriadam10 commented 2026-10-10 06:06:43 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-10T06:06:43Z.


Checked the 32 rows of SinCosReduction on 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.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6094493044 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1979#issuecomment-6094493044). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-10T06:06:43Z. --- Checked the 32 rows of `SinCosReduction` on 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.
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!1979
No description provided.