Repository navigation
Description
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_f16The 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) = 0V_SIN_F32(0xff7fffff) => 0x00000000 // -MaxFloat, finiteV_COS_F32(0xff7fffff) => 0x3f800000V_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) = -0The 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
luckyteagirl commentedon Nov 24, 2026
Native RDNA2 measurement
I ran the four native
V_SIN/V_COSinstructions on a Radeon RX 6800M (gfx1031), Windows 11 build 26200, display driver30.0.13002.13003, AMD OpenCL3302.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.v_sin_f32v_cos_f32-0.0(0x80000000)0x800000000x3F8000000.125,255.125,100000.125,1000000.1250x3F3504F40x3F3504F30.25and-0.750x3F8000000x000000000.5and8388607.50x000000000xBF800000+MaxFloatand-MaxFloat0x000000000x3F800000+Infand-Inf0xFFC000000xFFC00000v_sin_f16v_cos_f16-0.0(0x8000)0x80000x3C000.125and255.1250x39A80x39A8255,+65504and-655040x00000x3C00+Infand-Inf0xFE000xFE00On this chip, the four f32 inputs ending in
.125produced 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.
upipibi commentedon Nov 24, 2026
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 givesin= +0 (including 8388607.5); quarter and three-quarter turns givecos= 0; ±MaxFloat givessin0 andcos1; ±Inf gives0xFFC00000; 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).
Recompiler: v_sin / v_cos f16 of ±Inf give 0xFE00; test against nativ…
Recompiler: v_sin / v_cos reduce the argument in turns before scaling (…
Recompiler: v_sin / v_cos reduce the argument in turns before scaling (…
Recompiler: v_sin / v_cos f16 of ±Inf give 0xFE00; test against nativ…