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 Log2 to Vector64/128/256/512 #96455

Merged
merged 8 commits into from
Jan 11, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
using System.Linq;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
#if NET6_0_OR_GREATER
using System.Runtime.Intrinsics;
#endif
using System.Threading;
using System.Threading.Tasks;
using Xunit;
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -602,7 +602,6 @@
<Compile Include="$(MSBuildThisFileDirectory)System\Numerics\Vector4.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Numerics\Vector4.Extensions.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Numerics\VectorDebugView_1.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Numerics\VectorMath.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Object.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\ObjectDisposedException.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\ObsoleteAttribute.cs" />
Expand Down Expand Up @@ -1040,6 +1039,7 @@
<Compile Include="$(MSBuildThisFileDirectory)System\Runtime\Intrinsics\Vector64.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Runtime\Intrinsics\Vector64_1.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Runtime\Intrinsics\Vector64DebugView_1.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Runtime\Intrinsics\VectorMath.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Runtime\Intrinsics\X86\Enums.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Runtime\JitInfo.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Runtime\Loader\AssemblyLoadContext.cs" />
Expand Down
3 changes: 3 additions & 0 deletions src/libraries/System.Private.CoreLib/src/System/Double.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,9 @@ public readonly struct Double
internal const int TrailingSignificandLength = 52;
internal const int SignificandLength = TrailingSignificandLength + 1;

internal const long PositiveInfinityBits = 0x7FF00000_00000000;
internal const long SmallestNormalBits = 0x00100000_00000000;

internal ushort BiasedExponent
{
get
Expand Down
4 changes: 2 additions & 2 deletions src/libraries/System.Private.CoreLib/src/System/Math.cs
Original file line number Diff line number Diff line change
Expand Up @@ -292,9 +292,9 @@ public static double BitIncrement(double x)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static double CopySign(double x, double y)
{
if (Sse2.IsSupported || AdvSimd.IsSupported)
if (Vector128.IsHardwareAccelerated)
{
return VectorMath.ConditionalSelectBitwise(Vector128.CreateScalarUnsafe(-0.0), Vector128.CreateScalarUnsafe(y), Vector128.CreateScalarUnsafe(x)).ToScalar();
return Vector128.ConditionalSelect(Vector128.CreateScalarUnsafe(-0.0), Vector128.CreateScalarUnsafe(y), Vector128.CreateScalarUnsafe(x)).ToScalar();
}
else
{
Expand Down
4 changes: 2 additions & 2 deletions src/libraries/System.Private.CoreLib/src/System/MathF.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,9 @@ public static float BitIncrement(float x)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static float CopySign(float x, float y)
{
if (Sse.IsSupported || AdvSimd.IsSupported)
if (Vector128.IsHardwareAccelerated)
{
return VectorMath.ConditionalSelectBitwise(Vector128.CreateScalarUnsafe(-0.0f), Vector128.CreateScalarUnsafe(y), Vector128.CreateScalarUnsafe(x)).ToScalar();
return Vector128.ConditionalSelect(Vector128.CreateScalarUnsafe(-0.0f), Vector128.CreateScalarUnsafe(y), Vector128.CreateScalarUnsafe(x)).ToScalar();
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1781,6 +1781,38 @@ internal static Vector128<ushort> LoadUnsafe(ref char source) =>
internal static Vector128<ushort> LoadUnsafe(ref char source, nuint elementOffset) =>
LoadUnsafe(ref Unsafe.As<char, ushort>(ref source), elementOffset);

/// <inheritdoc cref="Vector64.Log2(Vector64{double})" />
public static Vector128<double> Log2(Vector128<double> vector)
{
if (IsHardwareAccelerated)
{
return VectorMath.Log2Double<Vector128<double>, Vector128<long>, Vector128<ulong>>(vector);
}
else
{
return Create(
Vector64.Log2(vector._lower),
Vector64.Log2(vector._upper)
);
}
}

/// <inheritdoc cref="Vector64.Log2(Vector64{float})" />
public static Vector128<float> Log2(Vector128<float> vector)
{
if (IsHardwareAccelerated)
{
return VectorMath.Log2Single<Vector128<float>, Vector128<int>, Vector128<uint>>(vector);
}
else
{
return Create(
Vector64.Log2(vector._lower),
Vector64.Log2(vector._upper)
);
}
}

/// <summary>Computes the maximum of two vectors on a per-element basis.</summary>
/// <typeparam name="T">The type of the elements in the vector.</typeparam>
/// <param name="left">The vector to compare with <paramref name="right" />.</param>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1755,6 +1755,38 @@ internal static Vector256<ushort> LoadUnsafe(ref char source) =>
internal static Vector256<ushort> LoadUnsafe(ref char source, nuint elementOffset) =>
LoadUnsafe(ref Unsafe.As<char, ushort>(ref source), elementOffset);

/// <inheritdoc cref="Vector128.Log2(Vector128{double})" />
public static Vector256<double> Log2(Vector256<double> vector)
{
if (IsHardwareAccelerated)
{
return VectorMath.Log2Double<Vector256<double>, Vector256<long>, Vector256<ulong>>(vector);
}
else
{
return Create(
Vector128.Log2(vector._lower),
Vector128.Log2(vector._upper)
);
}
}

/// <inheritdoc cref="Vector128.Log2(Vector128{float})" />
public static Vector256<float> Log2(Vector256<float> vector)
{
if (IsHardwareAccelerated)
{
return VectorMath.Log2Single<Vector256<float>, Vector256<int>, Vector256<uint>>(vector);
}
else
{
return Create(
Vector128.Log2(vector._lower),
Vector128.Log2(vector._upper)
);
}
}

/// <summary>Computes the maximum of two vectors on a per-element basis.</summary>
/// <typeparam name="T">The type of the elements in the vector.</typeparam>
/// <param name="left">The vector to compare with <paramref name="right" />.</param>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1806,6 +1806,38 @@ internal static Vector512<ushort> LoadUnsafe(ref char source) =>
internal static Vector512<ushort> LoadUnsafe(ref char source, nuint elementOffset) =>
LoadUnsafe(ref Unsafe.As<char, ushort>(ref source), elementOffset);

/// <inheritdoc cref="Vector256.Log2(Vector256{double})" />
public static Vector512<double> Log2(Vector512<double> vector)
{
if (IsHardwareAccelerated)
{
return VectorMath.Log2Double<Vector512<double>, Vector512<long>, Vector512<ulong>>(vector);
}
else
{
return Create(
Vector256.Log2(vector._lower),
Vector256.Log2(vector._upper)
);
}
}

/// <inheritdoc cref="Vector256.Log2(Vector256{float})" />
public static Vector512<float> Log2(Vector512<float> vector)
{
if (IsHardwareAccelerated)
{
return VectorMath.Log2Single<Vector512<float>, Vector512<int>, Vector512<uint>>(vector);
}
else
{
return Create(
Vector256.Log2(vector._lower),
Vector256.Log2(vector._upper)
);
}
}

/// <summary>Computes the maximum of two vectors on a per-element basis.</summary>
/// <typeparam name="T">The type of the elements in the vector.</typeparam>
/// <param name="left">The vector to compare with <paramref name="right" />.</param>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1571,6 +1571,50 @@ public static Vector64<T> LoadUnsafe<T>(ref readonly T source, nuint elementOffs
return Unsafe.ReadUnaligned<Vector64<T>>(in address);
}

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

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

return result;
}

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

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

/// <summary>Computes the maximum of two vectors on a per-element basis.</summary>
/// <typeparam name="T">The type of the elements in the vector.</typeparam>
/// <param name="left">The vector to compare with <paramref name="right" />.</param>
Expand Down
Loading
Loading