Last active
September 22, 2023 16:05
-
-
Save neon-sunset/0ed3c6c8ce1e96b2450074a906408c7e to your computer and use it in GitHub Desktop.
ARM64 snippets for C#
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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