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

Adding vectorized implementations of Exp to Vector64/128/256/512 #97114

Merged
merged 7 commits into from
Jan 19, 2024

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -1426,6 +1426,50 @@ public static bool EqualsAny<T>(Vector128<T> left, Vector128<T> right)
|| Vector64.EqualsAny(left._upper, right._upper);
}

internal static Vector128<T> Exp<T>(Vector128<T> vector)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this used somewhere outside of this type, or could it be private?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy/paste error. V128/256/512 should be calling the next smaller size on the upper/lower halves, as that gives the simplest implementation and tends to better performance on hardware that can't directly accelerate that size.

Log/Log2 are doing the right thing, just messed up here for Exp

where T : IExponentialFunctions<T>
{
Unsafe.SkipInit(out Vector128<T> result);

for (int index = 0; index < Vector128<T>.Count; index++)
{
T value = T.Exp(vector.GetElement(index));
result.SetElementUnsafe(index, value);
}

return result;
}

/// <summary>Computes the Exp of each element in a vector.</summary>
/// <param name="vector">The vector that will have its Exp computed.</param>
/// <returns>A vector whose elements are the Exp of the elements in <paramref name="vector" />.</returns>
public static Vector128<double> Exp(Vector128<double> vector)
{
if (IsHardwareAccelerated)
{
return VectorMath.ExpDouble<Vector128<double>, Vector128<long>, Vector128<ulong>>(vector);
}
else
{
return Exp<double>(vector);
}
}

/// <summary>Computes the Exp of each element in a vector.</summary>
/// <param name="vector">The vector that will have its Exp computed.</param>
/// <returns>A vector whose elements are the Exp of the elements in <paramref name="vector" />.</returns>
public static Vector128<float> Exp(Vector128<float> vector)
{
if (IsHardwareAccelerated)
{
return VectorMath.ExpSingle<Vector128<float>, Vector128<uint>, Vector128<double>, Vector128<ulong>>(vector);
}
else
{
return Exp<float>(vector);
}
}

/// <summary>Extracts the most significant bit from each element in a vector.</summary>
/// <typeparam name="T">The type of the elements in the vector.</typeparam>
/// <param name="vector">The vector whose elements should have their most significant bit extracted.</param>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1402,6 +1402,50 @@ public static bool EqualsAny<T>(Vector256<T> left, Vector256<T> right)
|| Vector128.EqualsAny(left._upper, right._upper);
}

internal static Vector256<T> Exp<T>(Vector256<T> vector)
where T : IExponentialFunctions<T>
{
Unsafe.SkipInit(out Vector256<T> result);

for (int index = 0; index < Vector256<T>.Count; index++)
{
T value = T.Exp(vector.GetElement(index));
result.SetElementUnsafe(index, value);
}

return result;
}

/// <summary>Computes the Exp of each element in a vector.</summary>
/// <param name="vector">The vector that will have its Exp computed.</param>
/// <returns>A vector whose elements are the Exp of the elements in <paramref name="vector" />.</returns>
public static Vector256<double> Exp(Vector256<double> vector)
{
if (IsHardwareAccelerated)
{
return VectorMath.ExpDouble<Vector256<double>, Vector256<long>, Vector256<ulong>>(vector);
}
else
{
return Exp<double>(vector);
}
}

/// <summary>Computes the Exp of each element in a vector.</summary>
/// <param name="vector">The vector that will have its Exp computed.</param>
/// <returns>A vector whose elements are the Exp of the elements in <paramref name="vector" />.</returns>
public static Vector256<float> Exp(Vector256<float> vector)
{
if (IsHardwareAccelerated)
{
return VectorMath.ExpSingle<Vector256<float>, Vector256<uint>, Vector256<double>, Vector256<ulong>>(vector);
}
else
{
return Exp<float>(vector);
}
}

/// <summary>Extracts the most significant bit from each element in a vector.</summary>
/// <param name="vector">The vector whose elements should have their most significant bit extracted.</param>
/// <typeparam name="T">The type of the elements in the vector.</typeparam>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1453,6 +1453,50 @@ public static bool EqualsAny<T>(Vector512<T> left, Vector512<T> right)
|| Vector256.EqualsAny(left._upper, right._upper);
}

internal static Vector512<T> Exp<T>(Vector512<T> vector)
where T : IExponentialFunctions<T>
{
Unsafe.SkipInit(out Vector512<T> result);

for (int index = 0; index < Vector512<T>.Count; index++)
{
T value = T.Exp(vector.GetElement(index));
result.SetElementUnsafe(index, value);
}

return result;
}

/// <summary>Computes the Exp of each element in a vector.</summary>
/// <param name="vector">The vector that will have its Exp computed.</param>
/// <returns>A vector whose elements are the Exp of the elements in <paramref name="vector" />.</returns>
public static Vector512<double> Exp(Vector512<double> vector)
{
if (IsHardwareAccelerated)
{
return VectorMath.ExpDouble<Vector512<double>, Vector512<long>, Vector512<ulong>>(vector);
}
else
{
return Exp<double>(vector);
}
}

/// <summary>Computes the Exp of each element in a vector.</summary>
/// <param name="vector">The vector that will have its Exp computed.</param>
/// <returns>A vector whose elements are the Exp of the elements in <paramref name="vector" />.</returns>
public static Vector512<float> Exp(Vector512<float> vector)
{
if (IsHardwareAccelerated)
{
return VectorMath.ExpSingle<Vector512<float>, Vector512<uint>, Vector512<double>, Vector512<ulong>>(vector);
}
else
{
return Exp<float>(vector);
}
}

/// <summary>Extracts the most significant bit from each element in a vector.</summary>
/// <param name="vector">The vector whose elements should have their most significant bit extracted.</param>
/// <typeparam name="T">The type of the elements in the vector.</typeparam>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1156,6 +1156,50 @@ public static bool EqualsAny<T>(Vector64<T> left, Vector64<T> right)
return false;
}

internal static Vector64<T> Exp<T>(Vector64<T> vector)
where T : IExponentialFunctions<T>
{
Unsafe.SkipInit(out Vector64<T> result);

for (int index = 0; index < Vector64<T>.Count; index++)
{
T value = T.Exp(vector.GetElement(index));
result.SetElementUnsafe(index, value);
}

return result;
}

/// <summary>Computes the Exp of each element in a vector.</summary>
/// <param name="vector">The vector that will have its Exp computed.</param>
/// <returns>A vector whose elements are the Exp of the elements in <paramref name="vector" />.</returns>
public static Vector64<double> Exp(Vector64<double> vector)
{
if (IsHardwareAccelerated)
{
return VectorMath.ExpDouble<Vector64<double>, Vector64<long>, Vector64<ulong>>(vector);
}
else
{
return Exp<double>(vector);
}
}

/// <summary>Computes the Exp of each element in a vector.</summary>
/// <param name="vector">The vector that will have its Exp computed.</param>
/// <returns>A vector whose elements are the Exp of the elements in <paramref name="vector" />.</returns>
public static Vector64<float> Exp(Vector64<float> vector)
{
if (IsHardwareAccelerated)
{
return VectorMath.ExpSingle<Vector64<float>, Vector64<uint>, Vector64<double>, Vector64<ulong>>(vector);
}
else
{
return Exp<float>(vector);
}
}

/// <summary>Extracts the most significant bit from each element in a vector.</summary>
/// <typeparam name="T">The type of the elements in the vector.</typeparam>
/// <param name="vector">The vector whose elements should have their most significant bit extracted.</param>
Expand Down
Loading
Loading