-
Notifications
You must be signed in to change notification settings - Fork 4.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
double.Cos and double.SinCos can produce very different results #98204
Comments
Tagging subscribers to this area: @dotnet/area-system-numerics Issue DetailsRepro: double d = 1e18;
double cos1 = double.Cos(d);
(_, double cos2) = double.SinCos(d);
Console.WriteLine(cos1);
Console.WriteLine(cos2); outputs:
on .NET 8
|
SinCos is returning sin for both values. Looks to repro for debug and release |
Looks to be Windows specific as well, but repros back to .NET 6 when SinCos was introduced |
Looks to be a bug in MSVC for We're generating: mov qword ptr [rsp+8],rbx
push rdi
sub rsp,20h
movaps xmm1,xmm0
mov rdi,r8
xorps xmm0,xmm0
mov rbx,rdx
movsd xmm0,xmm1
call __libm_sse2_sincos_ (07FF705521F90h)
movsd mmword ptr [rbx],xmm0
mov rbx,qword ptr [rsp+30h]
shufpd xmm0,xmm0,1
movsd mmword ptr [rdi],xmm0
add rsp,20h
pop rdi
ret So it looks like the expectation is that The API is internally calling This also explains why it reproduces on .NET 6, as we are dynamically invoking the C runtime and the underlying implementation has changed in the past couple years (it appears we are using SVML in some places now). |
There is indeed a This might date back to ~2019 given the announcement for using MSVC here: https://devblogs.microsoft.com/cppblog/msvc-backend-updates-in-visual-studio-2019-preview-2/ It's unclear when the optimization started occurring to use the internal helper, but it looks like it wasn't always being done, but since it's a dynamic call into the CRT, it happened somewhat implicitly as part of a tooling upgrade. |
Just copy/pasting my analysis from the PR
|
The MSVC bug has been filed here: https://developercommunity.visualstudio.com/t/MSVCs-sincos-implementation-is-incorrec/10582378 |
Repro:
outputs:
on .NET 8
The text was updated successfully, but these errors were encountered: