Skip to content

Commit 06c58ce

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

File tree

2 files changed

+10
-11
lines changed

2 files changed

+10
-11
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/VectorMath.cs

Lines changed: 9 additions & 10 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
}
@@ -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(ax, TVectorInt32.Create(ARG_LARGE)))
18461845
{
18471846
// We must be a finite value: (pi / 4) >= |x|
18481847

@@ -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(ax, TVectorInt32.Create(ARG_LARGE)))
21912190
{
21922191
// We must be a finite value: (pi / 4) >= |x|
21932192

0 commit comments

Comments
 (0)