Recompiler: v_sin_f32 / v_cos_f32 scale to radians before reducing the argument, so large arguments lose their phase #796
Labels
No labels
accessibility
bug
documentation
duplicate
enhancement
goal:world-load
good first issue
help wanted
invalid
keep-open
off-goal
question
stale
test-only
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/xcl-dev--xcl#796
Loading…
Reference in a new issue
No description provided.
Delete branch "%!s()"
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?
@upipibi on GitHub · 2026-11-24 05:33 UTC · original
v_sin_*/v_cos_*take their argument in turns (the ISA writesD.f = sin(S0.f * 2 * PI)). XCL emits the product in f32 and hands it to HLSLsin/cos(emit.rs,valuandvalu16):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) = 0V_SIN_F32(0xff7fffff) => 0x00000000 // -MaxFloat, finiteV_COS_F32(0xff7fffff) => 0x3f800000V_SIN_F16(0xfbff) => 0x0000,V_COS_F16(0x7bff) => 0x3c00Mesa'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_f32only forchip_class < GFX9). So on RDNA2 a shader can pass a large phase straight in.Measured on
maind5442d2(one-off GPU kernel, not committed), RTX 3070, driver 616.56, wave64 program in the split form:v_cos_f32(0.25)0x000000000x00000000v_sin_f32(0.5)0x000000000x00000000v_cos_f32(-0.75)0xB4491200(-1.87e-7)0x00000000v_sin_f32(-MaxFloat)0xff7fffff0x7FFFFFFF(NaN)0x00000000v_cos_f32(-MaxFloat)0x7FFFFFFF(NaN)0x3F800000v_sin_f32(+MaxFloat)/v_cos_f32(+MaxFloat)v_sin_f32(255.125)v_sin_f32(100000.125)v_sin_f32(1000000.125)v_sin_f16(0x7bff)(65504 turns)0xA648(-0.0245)0x0000v_sin_f16(0xfbff)0x2648(0.0245)0x0000v_cos_f16(0x7bff)0x3BFF0x3C00v_sin_f16(0x5bf8)(255 turns)0x8645(-9.6e-5)0x0000The NVIDIA driver's
sin/cosdoes its own reduction, so small exact cases such ascos(0.25)come out right.sin(1000000.125)is also right there, butsin(100000.125)is off in the second digit. A correctly roundedsinof 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.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 forsinat a half turn andcosat a quarter turn, so returning 0 whereabs(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 asfrac(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 thexcl-smokeruns 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.
github-bot referenced this issue2026-11-26 04:33:34 +00:00
@luckyteagirl on GitHub · 2026-11-24 05:59 UTC · original
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 on GitHub · 2026-11-24 06:06 UTC · original
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).