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

Add native ARM64 popcount for sizeof(T) above 1 to TensorPrimitives #103214

Merged
merged 1 commit into from
Jun 12, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -38,20 +38,40 @@ public static void PopCount<T>(ReadOnlySpan<T> x, Span<T> destination)
// This relies on 64-bit shifts for sizeof(T) == 8, and such shifts aren't accelerated on today's hardware.
// Alternative approaches, such as doing two 32-bit operations and combining them were observed to not
// provide any meaningfuls speedup over scalar. So for now, we don't vectorize when sizeof(T) == 8.
sizeof(T) == 1 || sizeof(T) == 2 || sizeof(T) == 4;
(sizeof(T) is 1 or 2 or 4) || (AdvSimd.IsSupported && sizeof(T) == 8);

public static T Invoke(T x) => T.PopCount(x);

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector128<T> Invoke(Vector128<T> x)
{
if (sizeof(T) == 1)
if (AdvSimd.IsSupported)
{
if (AdvSimd.IsSupported)
Vector128<byte> cnt = AdvSimd.PopCount(x.AsByte());

if (sizeof(T) == 1)
{
return cnt.As<byte, T>();
}

if (sizeof(T) == 2)
{
return AdvSimd.AddPairwiseWidening(cnt).As<ushort, T>();
}

if (sizeof(T) == 4)
{
return AdvSimd.PopCount(x.AsByte()).As<byte, T>();
return AdvSimd.AddPairwiseWidening(AdvSimd.AddPairwiseWidening(cnt)).As<uint, T>();
}

if (sizeof(T) == 8)
{
return AdvSimd.AddPairwiseWidening(AdvSimd.AddPairwiseWidening(AdvSimd.AddPairwiseWidening(cnt))).As<ulong, T>();
}
}

if (sizeof(T) == 1)
{
if (PackedSimd.IsSupported)
{
return PackedSimd.PopCount(x.AsByte()).As<byte, T>();
Expand Down
Loading