Skip to content

Commit 0e6a327

Browse files
committed
Remove unnecessary special-handling path from cosh
1 parent 85133f9 commit 0e6a327

File tree

2 files changed

+8
-30
lines changed

2 files changed

+8
-30
lines changed

src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/TensorPrimitives.netcore.cs

Lines changed: 7 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using System.Runtime.Intrinsics;
88
using System.Runtime.Intrinsics.Arm;
99
using System.Runtime.Intrinsics.X86;
10+
using System.Security.Cryptography;
1011

1112
namespace System.Numerics.Tensors
1213
{
@@ -3028,7 +3029,6 @@ public static Vector512<float> Invoke(Vector512<float> x)
30283029
// coshf = v/2 * exp(x - log(v)) where v = 0x1.0000e8p-1
30293030

30303031
private const uint SIGN_MASK = 0x7FFFFFFF;
3031-
private const uint ARG_MAX = 0x42B2D4FC;
30323032
private const uint LOGV = 0x3f317300;
30333033
private const uint HALFV = 0x3f800074;
30343034
private const uint INVV2 = 0x3e7ffe30;
@@ -3037,46 +3037,24 @@ public static Vector512<float> Invoke(Vector512<float> x)
30373037

30383038
public static Vector128<float> Invoke(Vector128<float> x)
30393039
{
3040-
Vector128<uint> ux = x.AsUInt32() & Vector128.Create(SIGN_MASK);
3041-
if (Vector128.GreaterThanAny(ux, Vector128.Create(ARG_MAX)))
3042-
{
3043-
return Vector128.Create(
3044-
MathF.Cosh(x.GetElement(0)),
3045-
MathF.Cosh(x.GetElement(1)),
3046-
MathF.Cosh(x.GetElement(2)),
3047-
MathF.Cosh(x.GetElement(3)));
3048-
}
3049-
3050-
Vector128<float> y = ux.AsSingle();
3040+
Vector128<float> y = (x.AsUInt32() & Vector128.Create(SIGN_MASK)).AsSingle();
30513041
Vector128<float> z = ExpOperator.Invoke(y - Vector128.Create(LOGV).AsSingle());
3052-
return Vector128.Create(HALFV).AsSingle() * (z + Vector128.Create(INVV2).AsSingle() * 1f / z);
3042+
return Vector128.Create(HALFV).AsSingle() * (z + (Vector128.Create(INVV2).AsSingle() / z));
30533043
}
30543044

30553045
public static Vector256<float> Invoke(Vector256<float> x)
30563046
{
3057-
Vector256<uint> ux = x.AsUInt32() & Vector256.Create(SIGN_MASK);
3058-
if (Vector256.GreaterThanAny(ux, Vector256.Create(ARG_MAX)))
3059-
{
3060-
return Vector256.Create(Invoke(x.GetLower()), Invoke(x.GetUpper()));
3061-
}
3062-
3063-
Vector256<float> y = ux.AsSingle();
3047+
Vector256<float> y = (x.AsUInt32() & Vector256.Create(SIGN_MASK)).AsSingle();
30643048
Vector256<float> z = ExpOperator.Invoke(y - Vector256.Create(LOGV).AsSingle());
3065-
return Vector256.Create(HALFV).AsSingle() * (z + Vector256.Create(INVV2).AsSingle() * 1f / z);
3049+
return Vector256.Create(HALFV).AsSingle() * (z + (Vector256.Create(INVV2).AsSingle() / z));
30663050
}
30673051

30683052
#if NET8_0_OR_GREATER
30693053
public static Vector512<float> Invoke(Vector512<float> x)
30703054
{
3071-
Vector512<uint> ux = x.AsUInt32() & Vector512.Create(SIGN_MASK);
3072-
if (Vector512.GreaterThanAny(ux, Vector512.Create(ARG_MAX)))
3073-
{
3074-
return Vector512.Create(Invoke(x.GetLower()), Invoke(x.GetUpper()));
3075-
}
3076-
3077-
Vector512<float> y = ux.AsSingle();
3055+
Vector512<float> y = (x.AsUInt32() & Vector512.Create(SIGN_MASK)).AsSingle();
30783056
Vector512<float> z = ExpOperator.Invoke(y - Vector512.Create(LOGV).AsSingle());
3079-
return Vector512.Create(HALFV).AsSingle() * (z + Vector512.Create(INVV2).AsSingle() * 1f / z);
3057+
return Vector512.Create(HALFV).AsSingle() * (z + (Vector512.Create(INVV2).AsSingle() / z));
30803058
}
30813059
#endif
30823060
}

src/libraries/System.Numerics.Tensors/tests/TensorPrimitivesTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -598,7 +598,7 @@ public static void Cosh_SpecialValues(int tensorLength)
598598
}
599599

600600
[Theory]
601-
[MemberData(nameof(VectorLengthAndIteratedRange), new object[] { -90f, 90f, 3f })]
601+
[MemberData(nameof(VectorLengthAndIteratedRange), new object[] { -100f, 100f, 3f })]
602602
public static void Cosh_ValueRange(int vectorLength, float element)
603603
{
604604
float[] x = new float[vectorLength];

0 commit comments

Comments
 (0)