Skip to content

Commit

Permalink
Fix some small bugs in the Sin, Cos, and SinCos impls
Browse files Browse the repository at this point in the history
  • Loading branch information
tannergooding committed Jul 15, 2024
1 parent 19183c5 commit 5d956da
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4546,7 +4546,7 @@ public void CosDoubleTest(double value, double expectedResult, double variance)

[Theory]
[MemberData(nameof(GenericMathTestMemberData.CosSingle), MemberType = typeof(GenericMathTestMemberData))]
public void CosCosgleTest(float value, float expectedResult, float variance)
public void CosSingleTest(float value, float expectedResult, float variance)
{
Vector<float> actualResult = Vector.Cos(Vector.Create(value));
AssertEqual(Vector.Create(expectedResult), actualResult, Vector.Create(variance));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3207,7 +3207,7 @@ public static Vector128<double> Sin(Vector128<double> vector)
{
if (IsHardwareAccelerated)
{
return VectorMath.CosDouble<Vector128<double>, Vector128<long>>(vector);
return VectorMath.SinDouble<Vector128<double>, Vector128<long>>(vector);
}
else
{
Expand All @@ -3226,11 +3226,11 @@ public static Vector128<float> Sin(Vector128<float> vector)
{
if (Vector256.IsHardwareAccelerated)
{
return VectorMath.CosSingle<Vector128<float>, Vector128<int>, Vector256<double>, Vector256<long>>(vector);
return VectorMath.SinSingle<Vector128<float>, Vector128<int>, Vector256<double>, Vector256<long>>(vector);
}
else
{
return VectorMath.CosSingle<Vector128<float>, Vector128<int>, Vector128<double>, Vector128<long>>(vector);
return VectorMath.SinSingle<Vector128<float>, Vector128<int>, Vector128<double>, Vector128<long>>(vector);
}
}
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3090,7 +3090,7 @@ public static Vector256<double> Sin(Vector256<double> vector)
{
if (IsHardwareAccelerated)
{
return VectorMath.CosDouble<Vector256<double>, Vector256<long>>(vector);
return VectorMath.SinDouble<Vector256<double>, Vector256<long>>(vector);
}
else
{
Expand All @@ -3109,11 +3109,11 @@ public static Vector256<float> Sin(Vector256<float> vector)
{
if (Vector512.IsHardwareAccelerated)
{
return VectorMath.CosSingle<Vector256<float>, Vector256<int>, Vector512<double>, Vector512<long>>(vector);
return VectorMath.SinSingle<Vector256<float>, Vector256<int>, Vector512<double>, Vector512<long>>(vector);
}
else
{
return VectorMath.CosSingle<Vector256<float>, Vector256<int>, Vector256<double>, Vector256<long>>(vector);
return VectorMath.SinSingle<Vector256<float>, Vector256<int>, Vector256<double>, Vector256<long>>(vector);
}
}
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3131,7 +3131,7 @@ public static Vector512<double> Sin(Vector512<double> vector)
{
if (IsHardwareAccelerated)
{
return VectorMath.CosDouble<Vector512<double>, Vector512<long>>(vector);
return VectorMath.SinDouble<Vector512<double>, Vector512<long>>(vector);
}
else
{
Expand All @@ -3148,7 +3148,7 @@ public static Vector512<float> Sin(Vector512<float> vector)
{
if (IsHardwareAccelerated)
{
return VectorMath.CosSingle<Vector512<float>, Vector512<int>, Vector512<double>, Vector512<long>>(vector);
return VectorMath.SinSingle<Vector512<float>, Vector512<int>, Vector512<double>, Vector512<long>>(vector);
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,30 +211,29 @@ public static TVectorSingle CosSingle<TVectorSingle, TVectorInt32, TVectorDouble
const int ARG_SMALLER = 0x39000000; // 2^-27

TVectorSingle ax = TVectorSingle.Abs(x);
TVectorInt32 ux = Unsafe.BitCast<TVectorSingle, TVectorInt32>(x);
TVectorInt32 ux = Unsafe.BitCast<TVectorSingle, TVectorInt32>(ax);

TVectorSingle result;

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

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

if (TVectorSingle.Count == TVectorDouble.Count)
{
result = Narrow<TVectorDouble, TVectorSingle>(
CosSingleSmall(Widen<TVectorSingle, TVectorDouble>(x2))
CosSingleSmall(Widen<TVectorSingle, TVectorDouble>(x))
);
}
else
{
result = Narrow<TVectorDouble, TVectorSingle>(
CosSingleSmall(WidenLower<TVectorSingle, TVectorDouble>(x2)),
CosSingleSmall(WidenUpper<TVectorSingle, TVectorDouble>(x2))
CosSingleSmall(WidenLower<TVectorSingle, TVectorDouble>(x)),
CosSingleSmall(WidenUpper<TVectorSingle, TVectorDouble>(x))
);
}
}
Expand Down Expand Up @@ -1760,8 +1759,8 @@ public static (TVectorDouble Sin, TVectorDouble Cos) SinCosDouble<TVectorDouble,

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

cosResult = TVectorDouble.ConditionalSelect(
Expand Down Expand Up @@ -1838,11 +1837,11 @@ public static (TVectorSingle Sin, TVectorSingle Cos) SinCosSingle<TVectorSingle,
const int ARG_SMALLER = 0x39000000; // 2^-27

TVectorSingle ax = TVectorSingle.Abs(x);
TVectorInt32 ux = Unsafe.BitCast<TVectorSingle, TVectorInt32>(x);
TVectorInt32 ux = Unsafe.BitCast<TVectorSingle, TVectorInt32>(ax);

TVectorSingle sinResult, cosResult;

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

Expand Down Expand Up @@ -1982,8 +1981,8 @@ public static (TVectorSingle Sin, TVectorSingle Cos) SinCosSingle<TVectorSingle,

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

cosResult = TVectorDouble.ConditionalSelect(
Expand Down Expand Up @@ -2095,8 +2094,8 @@ public static TVectorDouble SinDouble<TVectorDouble, TVectorInt64>(TVectorDouble

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

// Propagate the NaN that was passed in
Expand Down Expand Up @@ -2183,11 +2182,11 @@ public static TVectorSingle SinSingle<TVectorSingle, TVectorInt32, TVectorDouble
const int ARG_SMALLER = 0x39000000; // 2^-27

TVectorSingle ax = TVectorSingle.Abs(x);
TVectorInt32 ux = Unsafe.BitCast<TVectorSingle, TVectorInt32>(x);
TVectorInt32 ux = Unsafe.BitCast<TVectorSingle, TVectorInt32>(ax);

TVectorSingle result;

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

Expand Down Expand Up @@ -2274,8 +2273,8 @@ static TVectorDouble CoreImpl(TVectorDouble x)

return TVectorDouble.ConditionalSelect(
Unsafe.BitCast<TVectorInt64, TVectorDouble>(TVectorInt64.Equals(((sign & region) | (~sign & ~region)) & TVectorInt64.One, TVectorInt64.Zero)),
-result, // negative in region 1 or 3, positive in region 0 or 2
+result // negative in region 0 or 2, positive in region 1 or 3
+result, // negative in region 1 or 3, positive in region 0 or 2
-result // negative in region 0 or 2, positive in region 1 or 3
);
}
}
Expand Down

0 comments on commit 5d956da

Please sign in to comment.