Skip to content

Recompiler: v_sin_f32 / v_cos_f32 scale to radians before reducing the argument, so large arguments lose their phase #796

Backlog

Description

@upipibi

v_sin_* / v_cos_* take their argument in turns (the ISA writes D.f = sin(S0.f * 2 * PI)). XCL emits the product in f32 and hands it to HLSL sin / cos (emit.rs, valu and valu16):

sin(x * 6.2831853f)   // v_sin_f32, likewise v_cos_f32, v_sin_f16, v_cos_f16

The product rounds the argument at radian scale before any reduction. The error grows with the number of turns, and above about 5.4e37 the product overflows to infinity and the result is NaN. The RDNA 2 ISA's own functional examples show the hardware reducing in turns, with every finite input valid:

  • V_COS_F32(0x3e800000) => 0x00000000 // cos(0.25) = 0
  • V_SIN_F32(0xff7fffff) => 0x00000000 // -MaxFloat, finite
  • V_COS_F32(0xff7fffff) => 0x3f800000
  • V_SIN_F16(0xfbff) => 0x0000, V_COS_F16(0x7bff) => 0x3c00

Mesa's ACO only reduces explicitly on older chips: "before GFX9, v_sin_f32 and v_cos_f32 had a valid input domain of [-256, +256]" (aco_instruction_selection.cpp, v_fract_f32 only for chip_class < GFX9). So on RDNA2 a shader can pass a large phase straight in.

Measured on main d5442d2 (one-off GPU kernel, not committed), RTX 3070, driver 616.56, wave64 program in the split form:

Instruction, input XCL on the 3070 ISA / exact
v_cos_f32(0.25) 0x00000000 0x00000000
v_sin_f32(0.5) 0x00000000 0x00000000
v_cos_f32(-0.75) 0xB4491200 (-1.87e-7) 0x00000000
v_sin_f32(-MaxFloat) 0xff7fffff 0x7FFFFFFF (NaN) 0x00000000
v_cos_f32(-MaxFloat) 0x7FFFFFFF (NaN) 0x3F800000
v_sin_f32(+MaxFloat) / v_cos_f32(+MaxFloat) NaN / NaN 0 / 1 (an integer number of turns)
v_sin_f32(255.125) 0.707039 0.707107
v_sin_f32(100000.125) 0.671559 0.707107
v_sin_f32(1000000.125) 0.707107 0.707107
v_sin_f16(0x7bff) (65504 turns) 0xA648 (-0.0245) 0x0000
v_sin_f16(0xfbff) 0x2648 (0.0245) 0x0000
v_cos_f16(0x7bff) 0x3BFF 0x3C00
v_sin_f16(0x5bf8) (255 turns) 0x8645 (-9.6e-5) 0x0000

The NVIDIA driver's sin / cos does its own reduction, so small exact cases such as cos(0.25) come out right. sin(1000000.125) is also right there, but sin(100000.125) is off in the second digit. A correctly rounded sin of the same f32 product gives 0.7046 and 0.929 for those two, so another GPU's driver may be wrong at different points. The MaxFloat NaNs do not depend on the driver.

Open test PR #643 measures the same growth ("about 1e-4 at 255 turns" on an RTX 4060), allows 2^-10 absolute error, and skips inputs above 256 turns as "outside the ISA's sin / cos domain". The RDNA2 examples above contradict that limit, which ACO says ended with GFX9. If this is fixed, #643's skip and tolerance could tighten.

Fix (not written yet): reduce in turns first, then scale. x - trunc(x) is exact in f32, so every finite |x| ≥ 2^23 reduces to 0, giving sin 0 and cos 1 for MaxFloat as in the ISA examples. Inf still gives NaN.

float r = x - trunc(x);                         // exact
sin((x == 0.0f ? x : r) * 6.2831853f)           // keep sin(-0) = -0

The same applies to the f16 forms, with the reduction done in f32. Reducing in turns alone still leaves cos(-0.75) at about -1.2e-7 (a quarter turn times 2π is not exact in f32); the ISA examples give exactly 0 for sin at a half turn and cos at a quarter turn, so returning 0 where abs(r) is 0.5 (sin) or 0.25 / 0.75 (cos) covers those too.

Low impact: it shows where a shader feeds a large phase to sin / cos, such as frac(sin(dot(p, k)) * 43758.5) hash noise or a long-running time phase, and as NaN above about 5.4e37. I can open the PR with a GPU test of the ISA examples if this direction is fine. It needs the xcl-smoke runs from someone with a title.

AI use: Claude Code (Claude Opus 5.5) found this in a read-through of the recompiler, checked it against the ISA and ACO, ran the measurement above and wrote this issue.

Activity

  1. luckyteagirl commented on Nov 24, 2026

    @luckyteagirl
    Contributor

    Native RDNA2 measurement

    I ran the four native V_SIN / V_COS instructions on a Radeon RX 6800M (gfx1031), Windows 11 build 26200, display driver 30.0.13002.13003, AMD OpenCL 3302.6 (PAL,LC). The OpenCL kernel passed raw inputs to inline ISA and captured raw results. Four work-items per case agreed, and the probe checked the returned input bits.

    f32 input in turns v_sin_f32 v_cos_f32
    -0.0 (0x80000000) 0x80000000 0x3F800000
    0.125, 255.125, 100000.125, 1000000.125 0x3F3504F4 0x3F3504F3
    0.25 and -0.75 0x3F800000 0x00000000
    0.5 and 8388607.5 0x00000000 0xBF800000
    +MaxFloat and -MaxFloat 0x00000000 0x3F800000
    +Inf and -Inf 0xFFC00000 0xFFC00000
    f16 input in turns v_sin_f16 v_cos_f16
    -0.0 (0x8000) 0x8000 0x3C00
    0.125 and 255.125 0x39A8 0x39A8
    255, +65504 and -65504 0x0000 0x3C00
    +Inf and -Inf 0xFE00 0xFE00

    On this chip, the four f32 inputs ending in .125 produced the same bits for each instruction, including inputs above 256 turns. The finite maxima produced exact zero sine and one cosine in both precisions. The signed-zero observations could also be used as a regression case.

    Not tested

    XCL's HLSL output, other GPU generations, subnormal inputs, and signaling NaNs. No game data was used.

    AI use

    OpenAI Codex (GPT-6 family, exact model variant unavailable) wrote and ran the probe and drafted this report.

  2. upipibi commented on Nov 24, 2026

    @upipibi
    ContributorAuthor

    Thanks @luckyteagirl, that's the measurement this issue was missing. #818 (the fix for this issue) gives the same results on every row whose value is pinned exactly: sin(-0) = -0; half turns give sin = +0 (including 8388607.5); quarter and three-quarter turns give cos = 0; ±MaxFloat gives sin 0 and cos 1; ±Inf gives 0xFFC00000; and f16 integer turns (255, ±65504) give 0 / 1. I'll add your table to #818's test as hardware-sourced cases, including f16 ±Inf → 0xFE00, and credit your comment.

    AI-assisted (Claude Code).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions