Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve precision for Sinh and Cosh TP routines #99763

Closed
Closed
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,12 @@ public static void Cosh<T>(ReadOnlySpan<T> x, Span<T> destination)
//
// coshf = v/2 * exp(x - log(v)) where v = 0x1.0000e8p-1

private const uint Single_MAX_VECTORIZED_VALUE = 0x4E8565AAu;
private const float Single_LOGV = 0.693161f;
private const float Single_HALFV = 1.0000138f;
private const float Single_INVV2 = 0.24999309f;

private const ulong Double_MAX_VECTORIZED_VALUE = 0x4030800000000000ul;
private const double Double_LOGV = 0.6931471805599453;
private const double Double_HALFV = 1.0;
private const double Double_INVV2 = 0.25;
Expand All @@ -75,17 +77,27 @@ public static Vector128<T> Invoke(Vector128<T> t)
if (typeof(T) == typeof(float))
{
Vector128<float> x = t.AsSingle();

Vector128<float> y = Vector128.Abs(x);
Vector128<float> z = ExpOperator<float>.Invoke(y - Vector128.Create((float)Single_LOGV));
return (Vector128.Create((float)Single_HALFV) * (z + (Vector128.Create((float)Single_INVV2) / z))).As<float, T>();

if (Vector128.GreaterThanAny(y.AsUInt32(), Vector128.Create(Single_MAX_VECTORIZED_VALUE)))
{
return ApplyScalar<CoshOperator<float>>(x).As<float, T>();
}

Vector128<float> z = ExpOperator<float>.Invoke(y - Vector128.Create(Single_LOGV));
return (Vector128.Create(Single_HALFV) * (z + (Vector128.Create(Single_INVV2) / z))).As<float, T>();
}
else
{
Debug.Assert(typeof(T) == typeof(double));
Vector128<double> x = t.AsDouble();

Vector128<double> y = Vector128.Abs(x);

if (Vector128.GreaterThanAny(y.AsUInt64(), Vector128.Create(Double_MAX_VECTORIZED_VALUE)))
{
return ApplyScalar<CoshOperator<double>>(x).As<double, T>();
}

Vector128<double> z = ExpOperator<double>.Invoke(y - Vector128.Create(Double_LOGV));
return (Vector128.Create(Double_HALFV) * (z + (Vector128.Create(Double_INVV2) / z))).As<double, T>();
}
Expand All @@ -96,17 +108,27 @@ public static Vector256<T> Invoke(Vector256<T> t)
if (typeof(T) == typeof(float))
{
Vector256<float> x = t.AsSingle();

Vector256<float> y = Vector256.Abs(x);
Vector256<float> z = ExpOperator<float>.Invoke(y - Vector256.Create((float)Single_LOGV));
return (Vector256.Create((float)Single_HALFV) * (z + (Vector256.Create((float)Single_INVV2) / z))).As<float, T>();

if (Vector256.GreaterThanAny(y.AsUInt32(), Vector256.Create(Single_MAX_VECTORIZED_VALUE)))
{
return ApplyScalar<CoshOperator<float>>(x).As<float, T>();
}

Vector256<float> z = ExpOperator<float>.Invoke(y - Vector256.Create(Single_LOGV));
return (Vector256.Create(Single_HALFV) * (z + (Vector256.Create(Single_INVV2) / z))).As<float, T>();
}
else
{
Debug.Assert(typeof(T) == typeof(double));
Vector256<double> x = t.AsDouble();

Vector256<double> y = Vector256.Abs(x);

if (Vector256.GreaterThanAny(y.AsUInt64(), Vector256.Create(Double_MAX_VECTORIZED_VALUE)))
{
return ApplyScalar<CoshOperator<double>>(x).As<double, T>();
}

Vector256<double> z = ExpOperator<double>.Invoke(y - Vector256.Create(Double_LOGV));
return (Vector256.Create(Double_HALFV) * (z + (Vector256.Create(Double_INVV2) / z))).As<double, T>();
}
Expand All @@ -117,17 +139,27 @@ public static Vector512<T> Invoke(Vector512<T> t)
if (typeof(T) == typeof(float))
{
Vector512<float> x = t.AsSingle();

Vector512<float> y = Vector512.Abs(x);
Vector512<float> z = ExpOperator<float>.Invoke(y - Vector512.Create((float)Single_LOGV));
return (Vector512.Create((float)Single_HALFV) * (z + (Vector512.Create((float)Single_INVV2) / z))).As<float, T>();

if (Vector512.GreaterThanAny(y.AsUInt32(), Vector512.Create(Single_MAX_VECTORIZED_VALUE)))
{
return ApplyScalar<CoshOperator<float>>(x).As<float, T>();
}

Vector512<float> z = ExpOperator<float>.Invoke(y - Vector512.Create(Single_LOGV));
return (Vector512.Create(Single_HALFV) * (z + (Vector512.Create(Single_INVV2) / z))).As<float, T>();
}
else
{
Debug.Assert(typeof(T) == typeof(double));
Vector512<double> x = t.AsDouble();

Vector512<double> y = Vector512.Abs(x);

if (Vector512.GreaterThanAny(y.AsUInt64(), Vector512.Create(Double_MAX_VECTORIZED_VALUE)))
{
return ApplyScalar<CoshOperator<double>>(x).As<double, T>();
}

Vector512<double> z = ExpOperator<double>.Invoke(y - Vector512.Create(Double_LOGV));
return (Vector512.Create(Double_HALFV) * (z + (Vector512.Create(Double_INVV2) / z))).As<double, T>();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,12 @@ public static void Sinh<T>(ReadOnlySpan<T> x, Span<T> destination)
// Same as cosh, but with `z -` rather than `z +`, and with the sign
// flipped on the result based on the sign of the input.

private const uint Single_MAX_VECTORIZED_VALUE = 0x4E8565AAu;
private const float Single_LOGV = 0.693161f;
private const float Single_HALFV = 1.0000138f;
private const float Single_INVV2 = 0.24999309f;

private const ulong Double_MAX_VECTORIZED_VALUE = 0x4030800000000000ul;
private const double Double_LOGV = 0.6931471805599453;
private const double Double_HALFV = 1.0;
private const double Double_INVV2 = 0.25;
Expand All @@ -57,19 +59,29 @@ public static Vector128<T> Invoke(Vector128<T> t)
if (typeof(T) == typeof(float))
{
Vector128<float> x = t.AsSingle();

Vector128<float> y = Vector128.Abs(x);
Vector128<float> z = ExpOperator<float>.Invoke(y - Vector128.Create((float)Single_LOGV));
Vector128<float> result = Vector128.Create((float)Single_HALFV) * (z - (Vector128.Create((float)Single_INVV2) / z));

if (Vector128.GreaterThanAny(y.AsUInt32(), Vector128.Create(Single_MAX_VECTORIZED_VALUE)))
{
return ApplyScalar<SinhOperator<float>>(x).As<float, T>();
}

Vector128<float> z = ExpOperator<float>.Invoke(y - Vector128.Create(Single_LOGV));
Vector128<float> result = Vector128.Create(Single_HALFV) * (z - (Vector128.Create(Single_INVV2) / z));
Vector128<uint> sign = x.AsUInt32() & Vector128.Create(~(uint)int.MaxValue);
return (sign ^ result.AsUInt32()).As<uint, T>();
}
else
{
Debug.Assert(typeof(T) == typeof(double));
Vector128<double> x = t.AsDouble();

Vector128<double> y = Vector128.Abs(x);

if (Vector128.GreaterThanAny(y.AsUInt64(), Vector128.Create(Double_MAX_VECTORIZED_VALUE)))
{
return ApplyScalar<SinhOperator<double>>(x).As<double, T>();
}

Vector128<double> z = ExpOperator<double>.Invoke(y - Vector128.Create(Double_LOGV));
Vector128<double> result = Vector128.Create(Double_HALFV) * (z - (Vector128.Create(Double_INVV2) / z));
Vector128<ulong> sign = x.AsUInt64() & Vector128.Create(~(ulong)long.MaxValue);
Expand All @@ -82,19 +94,29 @@ public static Vector256<T> Invoke(Vector256<T> t)
if (typeof(T) == typeof(float))
{
Vector256<float> x = t.AsSingle();

Vector256<float> y = Vector256.Abs(x);
Vector256<float> z = ExpOperator<float>.Invoke(y - Vector256.Create((float)Single_LOGV));
Vector256<float> result = Vector256.Create((float)Single_HALFV) * (z - (Vector256.Create((float)Single_INVV2) / z));

if (Vector256.GreaterThanAny(y.AsUInt32(), Vector256.Create(Single_MAX_VECTORIZED_VALUE)))
{
return ApplyScalar<SinhOperator<float>>(x).As<float, T>();
}

Vector256<float> z = ExpOperator<float>.Invoke(y - Vector256.Create(Single_LOGV));
Vector256<float> result = Vector256.Create(Single_HALFV) * (z - (Vector256.Create(Single_INVV2) / z));
Vector256<uint> sign = x.AsUInt32() & Vector256.Create(~(uint)int.MaxValue);
return (sign ^ result.AsUInt32()).As<uint, T>();
}
else
{
Debug.Assert(typeof(T) == typeof(double));
Vector256<double> x = t.AsDouble();

Vector256<double> y = Vector256.Abs(x);

if (Vector256.GreaterThanAny(y.AsUInt64(), Vector256.Create(Double_MAX_VECTORIZED_VALUE)))
{
return ApplyScalar<SinhOperator<double>>(x).As<double, T>();
}

Vector256<double> z = ExpOperator<double>.Invoke(y - Vector256.Create(Double_LOGV));
Vector256<double> result = Vector256.Create(Double_HALFV) * (z - (Vector256.Create(Double_INVV2) / z));
Vector256<ulong> sign = x.AsUInt64() & Vector256.Create(~(ulong)long.MaxValue);
Expand All @@ -107,19 +129,29 @@ public static Vector512<T> Invoke(Vector512<T> t)
if (typeof(T) == typeof(float))
{
Vector512<float> x = t.AsSingle();

Vector512<float> y = Vector512.Abs(x);
Vector512<float> z = ExpOperator<float>.Invoke(y - Vector512.Create((float)Single_LOGV));
Vector512<float> result = Vector512.Create((float)Single_HALFV) * (z - (Vector512.Create((float)Single_INVV2) / z));

if (Vector512.GreaterThanAny(y.AsUInt32(), Vector512.Create(Single_MAX_VECTORIZED_VALUE)))
{
return ApplyScalar<SinhOperator<float>>(x).As<float, T>();
}

Vector512<float> z = ExpOperator<float>.Invoke(y - Vector512.Create(Single_LOGV));
Vector512<float> result = Vector512.Create(Single_HALFV) * (z - (Vector512.Create(Single_INVV2) / z));
Vector512<uint> sign = x.AsUInt32() & Vector512.Create(~(uint)int.MaxValue);
return (sign ^ result.AsUInt32()).As<uint, T>();
}
else
{
Debug.Assert(typeof(T) == typeof(double));
Vector512<double> x = t.AsDouble();

Vector512<double> y = Vector512.Abs(x);

if (Vector512.GreaterThanAny(y.AsUInt64(), Vector512.Create(Double_MAX_VECTORIZED_VALUE)))
{
return ApplyScalar<SinhOperator<double>>(x).As<double, T>();
}

Vector512<double> z = ExpOperator<double>.Invoke(y - Vector512.Create(Double_LOGV));
Vector512<double> result = Vector512.Create(Double_HALFV) * (z - (Vector512.Create(Double_INVV2) / z));
Vector512<ulong> sign = x.AsUInt64() & Vector512.Create(~(ulong)long.MaxValue);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -367,8 +367,7 @@ public static IEnumerable<object[]> SpanDestinationFunctionsToTest()
yield return Create(TensorPrimitives.Cbrt, T.Cbrt, Helpers.DetermineTolerance<T>(doubleTolerance: 1e-13));
yield return Create(TensorPrimitives.Ceiling, T.Ceiling);
yield return Create(TensorPrimitives.Cos, T.Cos, trigTolerance);
// TODO https://github.com/dotnet/runtime/issues/98861
yield return Create(TensorPrimitives.Cosh, T.Cosh, Helpers.DetermineTolerance<T>(doubleTolerance: 1e-14));
yield return Create(TensorPrimitives.Cosh, T.Cosh);
// TODO https://github.com/dotnet/runtime/issues/98861
yield return Create(TensorPrimitives.CosPi, T.CosPi, trigTolerance ?? Helpers.DetermineTolerance<T>(floatTolerance: 1e-5f));
yield return Create(TensorPrimitives.DegreesToRadians, T.DegreesToRadians);
Expand Down Expand Up @@ -396,8 +395,7 @@ public static IEnumerable<object[]> SpanDestinationFunctionsToTest()
yield return Create(TensorPrimitives.ReciprocalSqrtEstimate, T.ReciprocalSqrtEstimate, T.CreateTruncating(Helpers.DefaultToleranceForEstimates));
yield return Create(TensorPrimitives.Round, T.Round);
yield return Create(TensorPrimitives.Sin, T.Sin, trigTolerance);
// TODO https://github.com/dotnet/runtime/issues/98861
yield return Create(TensorPrimitives.Sinh, T.Sinh, Helpers.DetermineTolerance<T>(doubleTolerance: 1e-14));
yield return Create(TensorPrimitives.Sinh, T.Sinh);
// TODO https://github.com/dotnet/runtime/issues/98861
yield return Create(TensorPrimitives.SinPi, T.SinPi, Helpers.DetermineTolerance<T>(doubleTolerance: 1e-13, floatTolerance: 1e-4f));
yield return Create(TensorPrimitives.Sqrt, T.Sqrt);
Expand Down Expand Up @@ -521,7 +519,7 @@ public static IEnumerable<object[]> SpanSpanDestinationFunctionsToTest()
yield return Create(TensorPrimitives.Ieee754Remainder, T.Ieee754Remainder);
yield return Create(TensorPrimitives.Log, T.Log);
// TODO https://github.com/dotnet/runtime/issues/98861
yield return Create(TensorPrimitives.Pow, T.Pow, Helpers.DetermineTolerance<T>(doubleTolerance: 1e-13, floatTolerance: 1e-4f));
yield return Create(TensorPrimitives.Pow, T.Pow, Helpers.DetermineTolerance<T>(doubleTolerance: 1e-13, floatTolerance: 1e-5f));

static object[] Create(SpanSpanDestinationDelegate tensorPrimitivesMethod, Func<T, T, T> expectedMethod, T? tolerance = null)
=> new object[] { tensorPrimitivesMethod, expectedMethod, tolerance };
Expand Down Expand Up @@ -651,7 +649,7 @@ public static IEnumerable<object[]> SpanScalarDestinationFunctionsToTest()
yield return Create(TensorPrimitives.CopySign, T.CopySign);
yield return Create(TensorPrimitives.Ieee754Remainder, T.Ieee754Remainder);
// TODO https://github.com/dotnet/runtime/issues/98861
yield return Create(TensorPrimitives.Pow, T.Pow, Helpers.DetermineTolerance<T>(doubleTolerance: 1e-13, floatTolerance: 1e-4f));
yield return Create(TensorPrimitives.Pow, T.Pow, Helpers.DetermineTolerance<T>(doubleTolerance: 1e-13, floatTolerance: 1e-5f));
yield return Create(TensorPrimitives.Log, T.Log);
yield return Create(TensorPrimitives.Max, T.Max);
yield return Create(TensorPrimitives.MaxMagnitude, T.MaxMagnitude);
Expand Down Expand Up @@ -756,7 +754,7 @@ public static IEnumerable<object[]> ScalarSpanFloatDestinationFunctionsToTest()
yield return Create(TensorPrimitives.Atan2, T.Atan2);
yield return Create(TensorPrimitives.Atan2Pi, T.Atan2Pi);
// TODO https://github.com/dotnet/runtime/issues/98861
yield return Create(TensorPrimitives.Pow, T.Pow, Helpers.DetermineTolerance<T>(floatTolerance: 1e-4f));
yield return Create(TensorPrimitives.Pow, T.Pow, Helpers.DetermineTolerance<T>(floatTolerance: 1e-5f));
yield return Create(TensorPrimitives.Ieee754Remainder, T.Ieee754Remainder);

static object[] Create(ScalarSpanDestinationDelegate tensorPrimitivesMethod, Func<T, T, T> expectedMethod, T? tolerance = null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -956,7 +956,7 @@ public void Dot_ThrowsForMismatchedLengths_x_y()
public void Dot_AllLengths()
{
// TODO https://github.com/dotnet/runtime/issues/98861
T? tolerance = Helpers.DetermineTolerance<T>(doubleTolerance: 1e-14f, floatTolerance: 1e-3f);
T? tolerance = Helpers.DetermineTolerance<T>(doubleTolerance: 1e-14f, floatTolerance: 1e-5f);

Assert.All(Helpers.TensorLengthsIncluding0, tensorLength =>
{
Expand Down