diff --git a/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/TensorPrimitives.netcore.cs b/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/TensorPrimitives.netcore.cs index dec6446cd7653b..bb44398cd10c58 100644 --- a/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/TensorPrimitives.netcore.cs +++ b/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/TensorPrimitives.netcore.cs @@ -18683,11 +18683,54 @@ public static Vector512 Invoke(Vector512 x) /// T.LeadingZeroCount(x) internal readonly struct LeadingZeroCountOperator : IUnaryOperator where T : IBinaryInteger { - public static bool Vectorizable => false; // TODO: Vectorize + public static bool Vectorizable => + (Avx512CD.VL.IsSupported && (sizeof(T) == 4 || sizeof(T) == 8)) || + (AdvSimd.IsSupported && (sizeof(T) == 1 || sizeof(T) == 2 || sizeof(T) == 4)); + public static T Invoke(T x) => T.LeadingZeroCount(x); - public static Vector128 Invoke(Vector128 x) => throw new NotSupportedException(); - public static Vector256 Invoke(Vector256 x) => throw new NotSupportedException(); - public static Vector512 Invoke(Vector512 x) => throw new NotSupportedException(); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static Vector128 Invoke(Vector128 x) + { + if (Avx512CD.VL.IsSupported) + { + if (sizeof(T) == 4) return Avx512CD.VL.LeadingZeroCount(x.AsUInt32()).As(); + if (sizeof(T) == 8) return Avx512CD.VL.LeadingZeroCount(x.AsUInt64()).As(); + } + + if (AdvSimd.IsSupported) + { + if (sizeof(T) == 1) return AdvSimd.LeadingZeroCount(x.AsByte()).As(); + if (sizeof(T) == 2) return AdvSimd.LeadingZeroCount(x.AsUInt16()).As(); + if (sizeof(T) == 4) return AdvSimd.LeadingZeroCount(x.AsUInt32()).As(); + } + + throw new NotSupportedException(); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static Vector256 Invoke(Vector256 x) + { + if (Avx512CD.VL.IsSupported) + { + if (sizeof(T) == 4) return Avx512CD.VL.LeadingZeroCount(x.AsUInt32()).As(); + if (sizeof(T) == 8) return Avx512CD.VL.LeadingZeroCount(x.AsUInt64()).As(); + } + + return Vector256.Create(Invoke(x.GetLower()), Invoke(x.GetUpper())); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static Vector512 Invoke(Vector512 x) + { + if (Avx512CD.IsSupported) + { + if (sizeof(T) == 4) return Avx512CD.LeadingZeroCount(x.AsUInt32()).As(); + if (sizeof(T) == 8) return Avx512CD.LeadingZeroCount(x.AsUInt64()).As(); + } + + return Vector512.Create(Invoke(x.GetLower()), Invoke(x.GetUpper())); + } } /// T.TrailingZeroCount(x)