diff --git a/src/libraries/System.Linq/src/System/Linq/Max.cs b/src/libraries/System.Linq/src/System/Linq/Max.cs index bd08684270f6c7..d3189720456d73 100644 --- a/src/libraries/System.Linq/src/System/Linq/Max.cs +++ b/src/libraries/System.Linq/src/System/Linq/Max.cs @@ -319,16 +319,20 @@ public static decimal Max(this IEnumerable source) comparer ??= Comparer.Default; + // TODO https://github.com/dotnet/csharplang/discussions/6308: Update this to use generic constraint bridging if/when available. if (typeof(TSource) == typeof(byte) && comparer == Comparer.Default) return (TSource)(object)MinMaxInteger>((IEnumerable)source); if (typeof(TSource) == typeof(sbyte) && comparer == Comparer.Default) return (TSource)(object)MinMaxInteger>((IEnumerable)source); if (typeof(TSource) == typeof(ushort) && comparer == Comparer.Default) return (TSource)(object)MinMaxInteger>((IEnumerable)source); if (typeof(TSource) == typeof(short) && comparer == Comparer.Default) return (TSource)(object)MinMaxInteger>((IEnumerable)source); + if (typeof(TSource) == typeof(char) && comparer == Comparer.Default) return (TSource)(object)MinMaxInteger>((IEnumerable)source); if (typeof(TSource) == typeof(uint) && comparer == Comparer.Default) return (TSource)(object)MinMaxInteger>((IEnumerable)source); if (typeof(TSource) == typeof(int) && comparer == Comparer.Default) return (TSource)(object)MinMaxInteger>((IEnumerable)source); if (typeof(TSource) == typeof(ulong) && comparer == Comparer.Default) return (TSource)(object)MinMaxInteger>((IEnumerable)source); if (typeof(TSource) == typeof(long) && comparer == Comparer.Default) return (TSource)(object)MinMaxInteger>((IEnumerable)source); if (typeof(TSource) == typeof(nuint) && comparer == Comparer.Default) return (TSource)(object)MinMaxInteger>((IEnumerable)source); if (typeof(TSource) == typeof(nint) && comparer == Comparer.Default) return (TSource)(object)MinMaxInteger>((IEnumerable)source); + if (typeof(TSource) == typeof(Int128) && comparer == Comparer.Default) return (TSource)(object)MinMaxInteger>((IEnumerable)source); + if (typeof(TSource) == typeof(UInt128) && comparer == Comparer.Default) return (TSource)(object)MinMaxInteger>((IEnumerable)source); TSource? value = default; using (IEnumerator e = source.GetEnumerator()) diff --git a/src/libraries/System.Linq/src/System/Linq/MaxMin.cs b/src/libraries/System.Linq/src/System/Linq/MaxMin.cs index 47974c48a1c6fc..759c4ce65627dd 100644 --- a/src/libraries/System.Linq/src/System/Linq/MaxMin.cs +++ b/src/libraries/System.Linq/src/System/Linq/MaxMin.cs @@ -32,7 +32,7 @@ private static T MinMaxInteger(this IEnumerable source) ThrowHelper.ThrowNoElementsException(); } - if (!Vector128.IsHardwareAccelerated || span.Length < Vector128.Count) + if (!Vector128.IsHardwareAccelerated || !Vector128.IsSupported || span.Length < Vector128.Count) { value = span[0]; for (int i = 1; i < span.Length; i++) @@ -43,7 +43,7 @@ private static T MinMaxInteger(this IEnumerable source) } } } - else if (!Vector256.IsHardwareAccelerated || span.Length < Vector256.Count) + else if (!Vector256.IsHardwareAccelerated || !Vector256.IsSupported || span.Length < Vector256.Count) { ref T current = ref MemoryMarshal.GetReference(span); ref T lastVectorStart = ref Unsafe.Add(ref current, span.Length - Vector128.Count); @@ -67,7 +67,7 @@ private static T MinMaxInteger(this IEnumerable source) } } } - else if (!Vector512.IsHardwareAccelerated || span.Length < Vector512.Count) + else if (!Vector512.IsHardwareAccelerated || !Vector512.IsSupported || span.Length < Vector512.Count) { ref T current = ref MemoryMarshal.GetReference(span); ref T lastVectorStart = ref Unsafe.Add(ref current, span.Length - Vector256.Count); diff --git a/src/libraries/System.Linq/src/System/Linq/Min.cs b/src/libraries/System.Linq/src/System/Linq/Min.cs index 6c81274f3013c8..632d643655174a 100644 --- a/src/libraries/System.Linq/src/System/Linq/Min.cs +++ b/src/libraries/System.Linq/src/System/Linq/Min.cs @@ -297,16 +297,20 @@ public static decimal Min(this IEnumerable source) comparer ??= Comparer.Default; + // TODO https://github.com/dotnet/csharplang/discussions/6308: Update this to use generic constraint bridging if/when available. if (typeof(TSource) == typeof(byte) && comparer == Comparer.Default) return (TSource)(object)MinMaxInteger>((IEnumerable)source); if (typeof(TSource) == typeof(sbyte) && comparer == Comparer.Default) return (TSource)(object)MinMaxInteger>((IEnumerable)source); if (typeof(TSource) == typeof(ushort) && comparer == Comparer.Default) return (TSource)(object)MinMaxInteger>((IEnumerable)source); if (typeof(TSource) == typeof(short) && comparer == Comparer.Default) return (TSource)(object)MinMaxInteger>((IEnumerable)source); + if (typeof(TSource) == typeof(char) && comparer == Comparer.Default) return (TSource)(object)MinMaxInteger>((IEnumerable)source); if (typeof(TSource) == typeof(uint) && comparer == Comparer.Default) return (TSource)(object)MinMaxInteger>((IEnumerable)source); if (typeof(TSource) == typeof(int) && comparer == Comparer.Default) return (TSource)(object)MinMaxInteger>((IEnumerable)source); if (typeof(TSource) == typeof(ulong) && comparer == Comparer.Default) return (TSource)(object)MinMaxInteger>((IEnumerable)source); if (typeof(TSource) == typeof(long) && comparer == Comparer.Default) return (TSource)(object)MinMaxInteger>((IEnumerable)source); if (typeof(TSource) == typeof(nuint) && comparer == Comparer.Default) return (TSource)(object)MinMaxInteger>((IEnumerable)source); if (typeof(TSource) == typeof(nint) && comparer == Comparer.Default) return (TSource)(object)MinMaxInteger>((IEnumerable)source); + if (typeof(TSource) == typeof(Int128) && comparer == Comparer.Default) return (TSource)(object)MinMaxInteger>((IEnumerable)source); + if (typeof(TSource) == typeof(UInt128) && comparer == Comparer.Default) return (TSource)(object)MinMaxInteger>((IEnumerable)source); TSource? value = default; using (IEnumerator e = source.GetEnumerator()) diff --git a/src/libraries/System.Linq/tests/MaxTests.cs b/src/libraries/System.Linq/tests/MaxTests.cs index d9b020e7d47964..a1509855091d11 100644 --- a/src/libraries/System.Linq/tests/MaxTests.cs +++ b/src/libraries/System.Linq/tests/MaxTests.cs @@ -28,6 +28,9 @@ public static IEnumerable Max_AllTypes_TestData() yield return new object[] { Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (short)i)), (short)(length + length - 1) }; yield return new object[] { Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (short)i).ToArray()), (short)(length + length - 1) }; + yield return new object[] { Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (char)i)), (char)(length + length - 1) }; + yield return new object[] { Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (char)i).ToArray()), (char)(length + length - 1) }; + yield return new object[] { Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (uint)i)), (uint)(length + length - 1) }; yield return new object[] { Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (uint)i).ToArray()), (uint)(length + length - 1) }; @@ -54,6 +57,12 @@ public static IEnumerable Max_AllTypes_TestData() yield return new object[] { Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (nint)i)), (nint)(length + length - 1) }; yield return new object[] { Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (nint)i).ToArray()), (nint)(length + length - 1) }; + + yield return new object[] { Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (Int128)i)), (Int128)(length + length - 1) }; + yield return new object[] { Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (Int128)i).ToArray()), (Int128)(length + length - 1) }; + + yield return new object[] { Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (UInt128)i)), (UInt128)(length + length - 1) }; + yield return new object[] { Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (UInt128)i).ToArray()), (UInt128)(length + length - 1) }; } }