Skip to content

Commit dfcbcb4

Browse files
Workaround an MSVC bug with __libm_sse2_sincos_ (#98207)
* Workaround an MSVC bug with __libm_sse2_sincos_ * Add the SinCos regression tests to the existing Math/MathF tests * Ensure the workaround also applies to x86 * Allow a larger amount of variance due to x86 Windows * Adjust the allowedVarianceCos for x86 * Add a link to the MSVC issue * Add a link to the MSVC issue
1 parent 4acd106 commit dfcbcb4

File tree

4 files changed

+26
-0
lines changed

4 files changed

+26
-0
lines changed

src/coreclr/classlibnative/float/floatdouble.cpp

+11
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,13 @@ FCIMPL1_V(double, COMDouble::Sin, double x)
253253
return sin(x);
254254
FCIMPLEND
255255

256+
#if defined(_MSC_VER)
257+
// The /fp:fast form of `sincos` for xarch returns sin twice, rather than sincos
258+
// https://developercommunity.visualstudio.com/t/MSVCs-sincos-implementation-is-incorrec/10582378
259+
#pragma float_control(push)
260+
#pragma float_control(precise, on)
261+
#endif
262+
256263
/*====================================SinCos====================================
257264
**
258265
==============================================================================*/
@@ -270,6 +277,10 @@ FCIMPL3_VII(void, COMDouble::SinCos, double x, double* pSin, double* pCos)
270277

271278
FCIMPLEND
272279

280+
#if defined(_MSC_VER)
281+
#pragma float_control(pop)
282+
#endif
283+
273284
/*=====================================Sinh=====================================
274285
**
275286
==============================================================================*/

src/coreclr/classlibnative/float/floatsingle.cpp

+11
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,13 @@ FCIMPL1_V(float, COMSingle::Sin, float x)
228228
return sinf(x);
229229
FCIMPLEND
230230

231+
#if defined(_MSC_VER)
232+
// The /fp:fast form of `sincos` for xarch returns sin twice, rather than sincos
233+
// https://developercommunity.visualstudio.com/t/MSVCs-sincos-implementation-is-incorrec/10582378
234+
#pragma float_control(push)
235+
#pragma float_control(precise, on)
236+
#endif
237+
231238
/*====================================SinCos====================================
232239
**
233240
==============================================================================*/
@@ -245,6 +252,10 @@ FCIMPL3_VII(void, COMSingle::SinCos, float x, float* pSin, float* pCos)
245252

246253
FCIMPLEND
247254

255+
#if defined(_MSC_VER)
256+
#pragma float_control(pop)
257+
#endif
258+
248259
/*=====================================Sinh=====================================
249260
**
250261
==============================================================================*/

src/libraries/System.Runtime/tests/System.Runtime.Extensions.Tests/System/Math.cs

+2
Original file line numberDiff line numberDiff line change
@@ -1497,6 +1497,7 @@ public static void Sin(double value, double expectedResult, double allowedVarian
14971497

14981498
[Theory]
14991499
[InlineData( double.NegativeInfinity, double.NaN, double.NaN, 0.0, 0.0)]
1500+
[InlineData(-1e18, 0.9929693207404051, 0.11837199021871073, 0.0002, 0.002)] // https://github.com/dotnet/runtime/issues/98204
15001501
[InlineData(-3.1415926535897932, -0.0, -1.0, CrossPlatformMachineEpsilon, CrossPlatformMachineEpsilon * 10)] // value: -(pi)
15011502
[InlineData(-2.7182818284590452, -0.41078129050290870, -0.91173391478696510, CrossPlatformMachineEpsilon, CrossPlatformMachineEpsilon)] // value: -(e)
15021503
[InlineData(-2.3025850929940457, -0.74398033695749319, -0.66820151019031295, CrossPlatformMachineEpsilon, CrossPlatformMachineEpsilon)] // value: -(ln(10))
@@ -1528,6 +1529,7 @@ public static void Sin(double value, double expectedResult, double allowedVarian
15281529
[InlineData( 2.3025850929940457, 0.74398033695749319, -0.66820151019031295, CrossPlatformMachineEpsilon, CrossPlatformMachineEpsilon)] // value: (ln(10))
15291530
[InlineData( 2.7182818284590452, 0.41078129050290870, -0.91173391478696510, CrossPlatformMachineEpsilon, CrossPlatformMachineEpsilon)] // value: (e)
15301531
[InlineData( 3.1415926535897932, 0.0, -1.0, CrossPlatformMachineEpsilon, CrossPlatformMachineEpsilon * 10)] // value: (pi)
1532+
[InlineData( 1e18, -0.9929693207404051, 0.11837199021871073, 0.0002, 0.002)] // https://github.com/dotnet/runtime/issues/98204
15311533
[InlineData( double.PositiveInfinity, double.NaN, double.NaN, 0.0, 0.0)]
15321534
public static void SinCos(double value, double expectedResultSin, double expectedResultCos, double allowedVarianceSin, double allowedVarianceCos)
15331535
{

src/libraries/System.Runtime/tests/System.Runtime.Extensions.Tests/System/MathF.cs

+2
Original file line numberDiff line numberDiff line change
@@ -1677,6 +1677,7 @@ public static void Sin(float value, float expectedResult, float allowedVariance)
16771677

16781678
[Theory]
16791679
[InlineData( float.NegativeInfinity, float.NaN, float.NaN, 0.0f, 0.0f)]
1680+
[InlineData(-1e8f, -0.931639, -0.36338508, CrossPlatformMachineEpsilon, CrossPlatformMachineEpsilon)] // https://github.com/dotnet/runtime/issues/98204
16801681
[InlineData(-3.14159265f, -0.0f, -1.0f, CrossPlatformMachineEpsilon, CrossPlatformMachineEpsilon * 10)] // value: -(pi)
16811682
[InlineData(-2.71828183f, -0.410781291f, -0.911733918f, CrossPlatformMachineEpsilon, CrossPlatformMachineEpsilon)] // value: -(e)
16821683
[InlineData(-2.30258509f, -0.743980337f, -0.668201510f, CrossPlatformMachineEpsilon, CrossPlatformMachineEpsilon)] // value: -(ln(10))
@@ -1708,6 +1709,7 @@ public static void Sin(float value, float expectedResult, float allowedVariance)
17081709
[InlineData( 2.30258509f, 0.743980337f, -0.668201510f, CrossPlatformMachineEpsilon, CrossPlatformMachineEpsilon)] // value: (ln(10))
17091710
[InlineData( 2.71828183f, 0.410781291f, -0.911733918f, CrossPlatformMachineEpsilon, CrossPlatformMachineEpsilon)] // value: (e)
17101711
[InlineData( 3.14159265f, 0.0f, -1.0f, CrossPlatformMachineEpsilon, CrossPlatformMachineEpsilon * 10)] // value: (pi)
1712+
[InlineData( 1e8f, 0.931639, -0.36338508, CrossPlatformMachineEpsilon, CrossPlatformMachineEpsilon)] // https://github.com/dotnet/runtime/issues/98204
17111713
[InlineData( float.PositiveInfinity, float.NaN, float.NaN, 0.0f, 0.0f)]
17121714
public static void SinCos(float value, float expectedResultSin, float expectedResultCos, float allowedVarianceSin, float allowedVarianceCos)
17131715
{

0 commit comments

Comments
 (0)