Skip to content
Closed
Changes from 3 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
Loading