Skip to content

Instantly share code, notes, and snippets.

@neon-sunset
Last active September 22, 2023 16:05
Show Gist options
  • Save neon-sunset/0ed3c6c8ce1e96b2450074a906408c7e to your computer and use it in GitHub Desktop.
Save neon-sunset/0ed3c6c8ce1e96b2450074a906408c7e to your computer and use it in GitHub Desktop.
ARM64 snippets for C#
using System.Runtime.CompilerServices;
using System.Runtime.Intrinsics.Arm;
namespace System.Runtime.Instrinsics;
public static class VectorExtensions
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static int CountMatches<T>(this Vector256<T> mask)
{
if (Vector256.IsHardwareAccelerated)
{
return BitOperations.PopCount(mask.ExtractMostSignificantBits());
}
var (lower, upper) = mask;
var lowerCount = CountMatches(lower);
var upperCount = CountMatches(upper);
return upperCount + lowerCount;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static int CountMatches<T>(this Vector128<T> mask)
{
if (AdvSimd.Arm64.IsSupported)
{
return AdvSimd.Arm64
.AddAcross(AdvSimd.PopCount(mask.AsByte()))
.ToScalar() / (8 * Unsafe.SizeOf<T>());
}
return BitOperations.PopCount(mask.ExtractMostSignificantBits());
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static int CountMatches<T>(this Vector64<T> mask)
{
return AdvSimd.Arm64
.AddAcross(AdvSimd.PopCount(mask.AsByte()))
.ToScalar() / (8 * Unsafe.SizeOf<T>());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment