Skip to content
Closed
Show file tree
Hide file tree
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
49 changes: 23 additions & 26 deletions src/libraries/System.Linq/src/System/Linq/MaxMin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@

using System.Collections.Generic;
using System.Numerics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Intrinsics;

namespace System.Linq
Expand Down Expand Up @@ -50,18 +48,19 @@ private static T MinMaxInteger<T, TMinMax>(this IEnumerable<T> source)
}
else if (!Vector256.IsHardwareAccelerated || !Vector256<T>.IsSupported || span.Length < Vector256<T>.Count)
{
ref T current = ref MemoryMarshal.GetReference(span);
ref T lastVectorStart = ref Unsafe.Add(ref current, span.Length - Vector128<T>.Count);
// Capture the trailing overlapping vector before slicing, so it remains valid
// even when span.Length == Vector128<T>.Count (in which case it equals the first vector).
ReadOnlySpan<T> lastVec = span.Slice(span.Length - Vector128<T>.Count);

Vector128<T> best = Vector128.LoadUnsafe(ref current);
current = ref Unsafe.Add(ref current, Vector128<T>.Count);
Vector128<T> best = Vector128.Create(span);
span = span.Slice(Vector128<T>.Count);

while (Unsafe.IsAddressLessThan(ref current, ref lastVectorStart))
while (span.Length >= Vector128<T>.Count)
{
best = TMinMax.Compare(best, Vector128.LoadUnsafe(ref current));
current = ref Unsafe.Add(ref current, Vector128<T>.Count);
best = TMinMax.Compare(best, Vector128.Create(span));
span = span.Slice(Vector128<T>.Count);
}
best = TMinMax.Compare(best, Vector128.LoadUnsafe(ref lastVectorStart));
best = TMinMax.Compare(best, Vector128.Create(lastVec));
Comment thread
EgorBo marked this conversation as resolved.

Comment thread
EgorBo marked this conversation as resolved.
value = best[0];
for (int i = 1; i < Vector128<T>.Count; i++)
Expand All @@ -74,18 +73,17 @@ private static T MinMaxInteger<T, TMinMax>(this IEnumerable<T> source)
}
else if (!Vector512.IsHardwareAccelerated || !Vector512<T>.IsSupported || span.Length < Vector512<T>.Count)
{
ref T current = ref MemoryMarshal.GetReference(span);
ref T lastVectorStart = ref Unsafe.Add(ref current, span.Length - Vector256<T>.Count);
ReadOnlySpan<T> lastVec = span.Slice(span.Length - Vector256<T>.Count);

Vector256<T> best = Vector256.LoadUnsafe(ref current);
current = ref Unsafe.Add(ref current, Vector256<T>.Count);
Vector256<T> best = Vector256.Create(span);
span = span.Slice(Vector256<T>.Count);

while (Unsafe.IsAddressLessThan(ref current, ref lastVectorStart))
while (span.Length >= Vector256<T>.Count)
{
best = TMinMax.Compare(best, Vector256.LoadUnsafe(ref current));
current = ref Unsafe.Add(ref current, Vector256<T>.Count);
best = TMinMax.Compare(best, Vector256.Create(span));
span = span.Slice(Vector256<T>.Count);
}
best = TMinMax.Compare(best, Vector256.LoadUnsafe(ref lastVectorStart));
best = TMinMax.Compare(best, Vector256.Create(lastVec));
Comment thread
EgorBo marked this conversation as resolved.

value = best[0];
for (int i = 1; i < Vector256<T>.Count; i++)
Expand All @@ -98,18 +96,17 @@ private static T MinMaxInteger<T, TMinMax>(this IEnumerable<T> source)
}
else
{
ref T current = ref MemoryMarshal.GetReference(span);
ref T lastVectorStart = ref Unsafe.Add(ref current, span.Length - Vector512<T>.Count);
ReadOnlySpan<T> lastVec = span.Slice(span.Length - Vector512<T>.Count);

Vector512<T> best = Vector512.LoadUnsafe(ref current);
current = ref Unsafe.Add(ref current, Vector512<T>.Count);
Vector512<T> best = Vector512.Create(span);
span = span.Slice(Vector512<T>.Count);

while (Unsafe.IsAddressLessThan(ref current, ref lastVectorStart))
while (span.Length >= Vector512<T>.Count)
{
best = TMinMax.Compare(best, Vector512.LoadUnsafe(ref current));
current = ref Unsafe.Add(ref current, Vector512<T>.Count);
best = TMinMax.Compare(best, Vector512.Create(span));
span = span.Slice(Vector512<T>.Count);
}
best = TMinMax.Compare(best, Vector512.LoadUnsafe(ref lastVectorStart));
best = TMinMax.Compare(best, Vector512.Create(lastVec));
Comment thread
EgorBo marked this conversation as resolved.

Comment thread
EgorBo marked this conversation as resolved.
value = best[0];
for (int i = 1; i < Vector512<T>.Count; i++)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ namespace System.Runtime.Intrinsics
// the internal inlining limits of the JIT.

/// <summary>Provides a collection of static methods for creating, manipulating, and otherwise operating on 128-bit vectors.</summary>
[Intrinsic]
public static partial class Vector128
{
internal const int Size = 16;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ namespace System.Runtime.Intrinsics
// the internal inlining limits of the JIT.

/// <summary>Provides a collection of static methods for creating, manipulating, and otherwise operating on 256-bit vectors.</summary>
[Intrinsic]
public static class Vector256
{
internal const int Size = 32;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ namespace System.Runtime.Intrinsics
// the internal inlining limits of the JIT.

/// <summary>Provides a collection of static methods for creating, manipulating, and otherwise operting on 512-bit vectors.</summary>
[Intrinsic]
public static class Vector512
{
internal const int Size = 64;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
namespace System.Runtime.Intrinsics
{
/// <summary>Provides a collection of static methods for creating, manipulating, and otherwise operating on 64-bit vectors.</summary>
[Intrinsic]
public static class Vector64
{
internal const int Size = 8;
Expand Down