Skip to content

Commit 549ef80

Browse files
committed
Fix some small bugs in the Sin, Cos, and SinCos impls
1 parent 19183c5 commit 549ef80

File tree

5 files changed

+33
-34
lines changed

5 files changed

+33
-34
lines changed

src/libraries/System.Numerics.Vectors/tests/GenericVectorTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4546,7 +4546,7 @@ public void CosDoubleTest(double value, double expectedResult, double variance)
45464546

45474547
[Theory]
45484548
[MemberData(nameof(GenericMathTestMemberData.CosSingle), MemberType = typeof(GenericMathTestMemberData))]
4549-
public void CosCosgleTest(float value, float expectedResult, float variance)
4549+
public void CosSingleTest(float value, float expectedResult, float variance)
45504550
{
45514551
Vector<float> actualResult = Vector.Cos(Vector.Create(value));
45524552
AssertEqual(Vector.Create(expectedResult), actualResult, Vector.Create(variance));

src/libraries/System.Private.CoreLib/src/System/Runtime/Intrinsics/Vector128.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3207,7 +3207,7 @@ public static Vector128<double> Sin(Vector128<double> vector)
32073207
{
32083208
if (IsHardwareAccelerated)
32093209
{
3210-
return VectorMath.CosDouble<Vector128<double>, Vector128<long>>(vector);
3210+
return VectorMath.SinDouble<Vector128<double>, Vector128<long>>(vector);
32113211
}
32123212
else
32133213
{
@@ -3226,11 +3226,11 @@ public static Vector128<float> Sin(Vector128<float> vector)
32263226
{
32273227
if (Vector256.IsHardwareAccelerated)
32283228
{
3229-
return VectorMath.CosSingle<Vector128<float>, Vector128<int>, Vector256<double>, Vector256<long>>(vector);
3229+
return VectorMath.SinSingle<Vector128<float>, Vector128<int>, Vector256<double>, Vector256<long>>(vector);
32303230
}
32313231
else
32323232
{
3233-
return VectorMath.CosSingle<Vector128<float>, Vector128<int>, Vector128<double>, Vector128<long>>(vector);
3233+
return VectorMath.SinSingle<Vector128<float>, Vector128<int>, Vector128<double>, Vector128<long>>(vector);
32343234
}
32353235
}
32363236
else

src/libraries/System.Private.CoreLib/src/System/Runtime/Intrinsics/Vector256.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3090,7 +3090,7 @@ public static Vector256<double> Sin(Vector256<double> vector)
30903090
{
30913091
if (IsHardwareAccelerated)
30923092
{
3093-
return VectorMath.CosDouble<Vector256<double>, Vector256<long>>(vector);
3093+
return VectorMath.SinDouble<Vector256<double>, Vector256<long>>(vector);
30943094
}
30953095
else
30963096
{
@@ -3109,11 +3109,11 @@ public static Vector256<float> Sin(Vector256<float> vector)
31093109
{
31103110
if (Vector512.IsHardwareAccelerated)
31113111
{
3112-
return VectorMath.CosSingle<Vector256<float>, Vector256<int>, Vector512<double>, Vector512<long>>(vector);
3112+
return VectorMath.SinSingle<Vector256<float>, Vector256<int>, Vector512<double>, Vector512<long>>(vector);
31133113
}
31143114
else
31153115
{
3116-
return VectorMath.CosSingle<Vector256<float>, Vector256<int>, Vector256<double>, Vector256<long>>(vector);
3116+
return VectorMath.SinSingle<Vector256<float>, Vector256<int>, Vector256<double>, Vector256<long>>(vector);
31173117
}
31183118
}
31193119
else

src/libraries/System.Private.CoreLib/src/System/Runtime/Intrinsics/Vector512.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3131,7 +3131,7 @@ public static Vector512<double> Sin(Vector512<double> vector)
31313131
{
31323132
if (IsHardwareAccelerated)
31333133
{
3134-
return VectorMath.CosDouble<Vector512<double>, Vector512<long>>(vector);
3134+
return VectorMath.SinDouble<Vector512<double>, Vector512<long>>(vector);
31353135
}
31363136
else
31373137
{
@@ -3148,7 +3148,7 @@ public static Vector512<float> Sin(Vector512<float> vector)
31483148
{
31493149
if (IsHardwareAccelerated)
31503150
{
3151-
return VectorMath.CosSingle<Vector512<float>, Vector512<int>, Vector512<double>, Vector512<long>>(vector);
3151+
return VectorMath.SinSingle<Vector512<float>, Vector512<int>, Vector512<double>, Vector512<long>>(vector);
31523152
}
31533153
else
31543154
{

src/libraries/System.Private.CoreLib/src/System/Runtime/Intrinsics/VectorMath.cs

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -211,30 +211,29 @@ public static TVectorSingle CosSingle<TVectorSingle, TVectorInt32, TVectorDouble
211211
const int ARG_SMALLER = 0x39000000; // 2^-27
212212

213213
TVectorSingle ax = TVectorSingle.Abs(x);
214-
TVectorInt32 ux = Unsafe.BitCast<TVectorSingle, TVectorInt32>(x);
214+
TVectorInt32 ux = Unsafe.BitCast<TVectorSingle, TVectorInt32>(ax);
215215

216216
TVectorSingle result;
217217

218-
if (TVectorSingle.LessThanOrEqualAll(ax, TVectorSingle.Create(ARG_LARGE)))
218+
if (TVectorInt32.LessThanOrEqualAll(ux, TVectorInt32.Create(ARG_LARGE)))
219219
{
220220
// We must be a finite value: (pi / 4) >= |x|
221221

222222
if (TVectorInt32.GreaterThanAny(ux, TVectorInt32.Create(ARG_SMALL - 1)))
223223
{
224224
// at least one element is: |x| >= 2^-13
225-
TVectorSingle x2 = x * x;
226225

227226
if (TVectorSingle.Count == TVectorDouble.Count)
228227
{
229228
result = Narrow<TVectorDouble, TVectorSingle>(
230-
CosSingleSmall(Widen<TVectorSingle, TVectorDouble>(x2))
229+
CosSingleSmall(Widen<TVectorSingle, TVectorDouble>(x))
231230
);
232231
}
233232
else
234233
{
235234
result = Narrow<TVectorDouble, TVectorSingle>(
236-
CosSingleSmall(WidenLower<TVectorSingle, TVectorDouble>(x2)),
237-
CosSingleSmall(WidenUpper<TVectorSingle, TVectorDouble>(x2))
235+
CosSingleSmall(WidenLower<TVectorSingle, TVectorDouble>(x)),
236+
CosSingleSmall(WidenUpper<TVectorSingle, TVectorDouble>(x))
238237
);
239238
}
240239
}
@@ -1760,8 +1759,8 @@ public static (TVectorDouble Sin, TVectorDouble Cos) SinCosDouble<TVectorDouble,
17601759

17611760
sinResult = TVectorDouble.ConditionalSelect(
17621761
Unsafe.BitCast<TVectorInt64, TVectorDouble>(TVectorInt64.Equals(((sign & region) | (~sign & ~region)) & TVectorInt64.One, TVectorInt64.Zero)),
1763-
-sinResult, // negative in region 1 or 3, positive in region 0 or 2
1764-
+sinResult // negative in region 0 or 2, positive in region 1 or 3
1762+
+sinResult, // negative in region 1 or 3, positive in region 0 or 2
1763+
-sinResult // negative in region 0 or 2, positive in region 1 or 3
17651764
);
17661765

17671766
cosResult = TVectorDouble.ConditionalSelect(
@@ -1806,12 +1805,12 @@ public static (TVectorDouble Sin, TVectorDouble Cos) SinCosDouble<TVectorDouble,
18061805
sinResult = TVectorDouble.ConditionalSelect(
18071806
argNotSmallerMask,
18081807
sinResult, // for elements: |x| >= 2^-27, infinity, or NaN
1809-
TVectorDouble.One // for elements: 2^-27 > |x|
1808+
x // for elements: 2^-27 > |x|
18101809
);
18111810

18121811
cosResult = TVectorDouble.ConditionalSelect(
18131812
argNotSmallerMask,
1814-
cosResult, // for elements: |x| >= 2^-27, infinity, or NaN
1813+
cosResult, // for elements: |x| >= 2^-27, infinity, or NaN
18151814
TVectorDouble.One // for elements: 2^-27 > |x|
18161815
);
18171816

@@ -1838,11 +1837,11 @@ public static (TVectorSingle Sin, TVectorSingle Cos) SinCosSingle<TVectorSingle,
18381837
const int ARG_SMALLER = 0x39000000; // 2^-27
18391838

18401839
TVectorSingle ax = TVectorSingle.Abs(x);
1841-
TVectorInt32 ux = Unsafe.BitCast<TVectorSingle, TVectorInt32>(x);
1840+
TVectorInt32 ux = Unsafe.BitCast<TVectorSingle, TVectorInt32>(ax);
18421841

18431842
TVectorSingle sinResult, cosResult;
18441843

1845-
if (TVectorSingle.LessThanOrEqualAll(ax, TVectorSingle.Create(ARG_LARGE)))
1844+
if (TVectorInt32.LessThanOrEqualAll(ux, TVectorInt32.Create(ARG_LARGE)))
18461845
{
18471846
// We must be a finite value: (pi / 4) >= |x|
18481847

@@ -1945,7 +1944,7 @@ public static (TVectorSingle Sin, TVectorSingle Cos) SinCosSingle<TVectorSingle,
19451944
sinResult = TVectorSingle.ConditionalSelect(
19461945
argNotSmallerMask,
19471946
sinResult, // for elements: |x| >= 2^-27, infinity, or NaN
1948-
TVectorSingle.One // for elements: 2^-27 > |x|
1947+
x // for elements: 2^-27 > |x|
19491948
);
19501949

19511950
cosResult = TVectorSingle.ConditionalSelect(
@@ -1982,8 +1981,8 @@ public static (TVectorSingle Sin, TVectorSingle Cos) SinCosSingle<TVectorSingle,
19821981

19831982
sinResult = TVectorDouble.ConditionalSelect(
19841983
Unsafe.BitCast<TVectorInt64, TVectorDouble>(TVectorInt64.Equals(((sign & region) | (~sign & ~region)) & TVectorInt64.One, TVectorInt64.Zero)),
1985-
-sinResult, // negative in region 1 or 3, positive in region 0 or 2
1986-
+sinResult // negative in region 0 or 2, positive in region 1 or 3
1984+
+sinResult, // negative in region 1 or 3, positive in region 0 or 2
1985+
-sinResult // negative in region 0 or 2, positive in region 1 or 3
19871986
);
19881987

19891988
cosResult = TVectorDouble.ConditionalSelect(
@@ -2095,8 +2094,8 @@ public static TVectorDouble SinDouble<TVectorDouble, TVectorInt64>(TVectorDouble
20952094

20962095
result = TVectorDouble.ConditionalSelect(
20972096
Unsafe.BitCast<TVectorInt64, TVectorDouble>(TVectorInt64.Equals(((sign & region) | (~sign & ~region)) & TVectorInt64.One, TVectorInt64.Zero)),
2098-
-result, // negative in region 1 or 3, positive in region 0 or 2
2099-
+result // negative in region 0 or 2, positive in region 1 or 3
2097+
+result, // negative in region 1 or 3, positive in region 0 or 2
2098+
-result // negative in region 0 or 2, positive in region 1 or 3
21002099
);
21012100

21022101
// Propagate the NaN that was passed in
@@ -2116,8 +2115,8 @@ public static TVectorDouble SinDouble<TVectorDouble, TVectorInt64>(TVectorDouble
21162115

21172116
return TVectorDouble.ConditionalSelect(
21182117
Unsafe.BitCast<TVectorInt64, TVectorDouble>(TVectorInt64.GreaterThan(ux, TVectorInt64.Create(ARG_SMALLER - 1))),
2119-
result, // for elements: |x| >= 2^-27, infinity, or NaN
2120-
TVectorDouble.One // for elements: 2^-27 > |x|
2118+
result, // for elements: |x| >= 2^-27, infinity, or NaN
2119+
x // for elements: 2^-27 > |x|
21212120
);
21222121
}
21232122

@@ -2183,11 +2182,11 @@ public static TVectorSingle SinSingle<TVectorSingle, TVectorInt32, TVectorDouble
21832182
const int ARG_SMALLER = 0x39000000; // 2^-27
21842183

21852184
TVectorSingle ax = TVectorSingle.Abs(x);
2186-
TVectorInt32 ux = Unsafe.BitCast<TVectorSingle, TVectorInt32>(x);
2185+
TVectorInt32 ux = Unsafe.BitCast<TVectorSingle, TVectorInt32>(ax);
21872186

21882187
TVectorSingle result;
21892188

2190-
if (TVectorSingle.LessThanOrEqualAll(ax, TVectorSingle.Create(ARG_LARGE)))
2189+
if (TVectorInt32.LessThanOrEqualAll(ux, TVectorInt32.Create(ARG_LARGE)))
21912190
{
21922191
// We must be a finite value: (pi / 4) >= |x|
21932192

@@ -2251,8 +2250,8 @@ public static TVectorSingle SinSingle<TVectorSingle, TVectorInt32, TVectorDouble
22512250

22522251
return TVectorSingle.ConditionalSelect(
22532252
Unsafe.BitCast<TVectorInt32, TVectorSingle>(TVectorInt32.GreaterThan(ux, TVectorInt32.Create(ARG_SMALLER - 1))),
2254-
result, // for elements: |x| >= 2^-27, infinity, or NaN
2255-
TVectorSingle.One // for elements: 2^-27 > |x|
2253+
result, // for elements: |x| >= 2^-27, infinity, or NaN
2254+
x // for elements: 2^-27 > |x|
22562255
);
22572256

22582257
[MethodImpl(MethodImplOptions.AggressiveInlining)]
@@ -2274,8 +2273,8 @@ static TVectorDouble CoreImpl(TVectorDouble x)
22742273

22752274
return TVectorDouble.ConditionalSelect(
22762275
Unsafe.BitCast<TVectorInt64, TVectorDouble>(TVectorInt64.Equals(((sign & region) | (~sign & ~region)) & TVectorInt64.One, TVectorInt64.Zero)),
2277-
-result, // negative in region 1 or 3, positive in region 0 or 2
2278-
+result // negative in region 0 or 2, positive in region 1 or 3
2276+
+result, // negative in region 1 or 3, positive in region 0 or 2
2277+
-result // negative in region 0 or 2, positive in region 1 or 3
22792278
);
22802279
}
22812280
}

0 commit comments

Comments
 (0)