Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Vectorize IndexOfAnyExcept for four values #73696

Merged
merged 4 commits into from
Aug 15, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -561,6 +561,67 @@ public static int IndexOfAnyExcept<T>(this ReadOnlySpan<T> span, T value0, T val
return -1;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static int IndexOfAnyExcept<T>(this ReadOnlySpan<T> span, T value0, T value1, T value2, T value3) where T : IEquatable<T>?
{
if (RuntimeHelpers.IsBitwiseEquatable<T>())
{
if (Unsafe.SizeOf<T>() == sizeof(byte))
{
return SpanHelpers.IndexOfAnyExceptValueType(
ref Unsafe.As<T, byte>(ref MemoryMarshal.GetReference(span)),
Unsafe.As<T, byte>(ref value0),
Unsafe.As<T, byte>(ref value1),
Unsafe.As<T, byte>(ref value2),
Unsafe.As<T, byte>(ref value3),
span.Length);
}
else if (Unsafe.SizeOf<T>() == sizeof(short))
{
return SpanHelpers.IndexOfAnyExceptValueType(
ref Unsafe.As<T, short>(ref MemoryMarshal.GetReference(span)),
Unsafe.As<T, short>(ref value0),
Unsafe.As<T, short>(ref value1),
Unsafe.As<T, short>(ref value2),
Unsafe.As<T, short>(ref value3),
span.Length);
}
else if (Unsafe.SizeOf<T>() == sizeof(int))
{
return SpanHelpers.IndexOfAnyExceptValueType(
ref Unsafe.As<T, int>(ref MemoryMarshal.GetReference(span)),
Unsafe.As<T, int>(ref value0),
Unsafe.As<T, int>(ref value1),
Unsafe.As<T, int>(ref value2),
Unsafe.As<T, int>(ref value3),
span.Length);
}
else if (Unsafe.SizeOf<T>() == sizeof(long))
{
return SpanHelpers.IndexOfAnyExceptValueType(
ref Unsafe.As<T, long>(ref MemoryMarshal.GetReference(span)),
Unsafe.As<T, long>(ref value0),
Unsafe.As<T, long>(ref value1),
Unsafe.As<T, long>(ref value2),
Unsafe.As<T, long>(ref value3),
span.Length);
}
}

for (int i = 0; i < span.Length; i++)
{
if (!EqualityComparer<T>.Default.Equals(span[i], value0) &&
!EqualityComparer<T>.Default.Equals(span[i], value1) &&
!EqualityComparer<T>.Default.Equals(span[i], value2) &&
!EqualityComparer<T>.Default.Equals(span[i], value3))
{
return i;
}
}

return -1;
}

/// <summary>Searches for the first index of any value other than the specified <paramref name="values"/>.</summary>
/// <typeparam name="T">The type of the span and values.</typeparam>
/// <param name="span">The span to search.</param>
Expand Down Expand Up @@ -588,6 +649,9 @@ public static int IndexOfAnyExcept<T>(this ReadOnlySpan<T> span, ReadOnlySpan<T>
case 3:
return IndexOfAnyExcept(span, values[0], values[1], values[2]);

case 4: // common for searching whitespaces
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: (don't restart CI just for this)

Suggested change
case 4: // common for searching whitespaces
case 4: // common for searching ASCII whitespaces

return IndexOfAnyExcept(span, values[0], values[1], values[2], values[3]);

default:
for (int i = 0; i < span.Length; i++)
{
Expand Down
61 changes: 61 additions & 0 deletions src/libraries/System.Private.CoreLib/src/System/SpanHelpers.T.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1270,5 +1270,66 @@ public static int SequenceCompareTo<T>(ref T first, int firstLength, ref T secon
}
return firstLength.CompareTo(secondLength);
}

public static int IndexOfAnyExceptValueType<T>(ref T searchSpace, T value0, T value1, T value2, T value3, int length) where T : struct, IEquatable<T>
{
Debug.Assert(length >= 0, "Expected non-negative length");
Debug.Assert(value0 is byte or short or int or long, "Expected caller to normalize to one of these types");

if (!Vector128.IsHardwareAccelerated || length < Vector128<T>.Count)
{
for (int i = 0; i < length; i++)
{
T current = Unsafe.Add(ref searchSpace, i);
if (!current.Equals(value0) && !current.Equals(value1) && !current.Equals(value2) && !current.Equals(value3))
{
return i;
}
}
}
else
{
Vector128<T> notEquals, current, values0 = Vector128.Create(value0), values1 = Vector128.Create(value1), values2 = Vector128.Create(value2), values3 = Vector128.Create(value3);
ref T currentSearchSpace = ref searchSpace;
ref T oneVectorAwayFromEnd = ref Unsafe.Add(ref searchSpace, length - Vector128<T>.Count);

// Loop until either we've finished all elements or there's less than a vector's-worth remaining.
do
{
current = Vector128.LoadUnsafe(ref currentSearchSpace);
notEquals = ~(Vector128.Equals(values0, current) | Vector128.Equals(values1, current)
| Vector128.Equals(values2, current) | Vector128.Equals(values3, current));
if (notEquals != Vector128<T>.Zero)
{
return ComputeIndex(ref searchSpace, ref currentSearchSpace, notEquals);
}

currentSearchSpace = ref Unsafe.Add(ref currentSearchSpace, Vector128<T>.Count);
}
while (!Unsafe.IsAddressGreaterThan(ref currentSearchSpace, ref oneVectorAwayFromEnd));

// If any elements remain, process the last vector in the search space.
if ((uint)length % Vector128<T>.Count != 0)
{
current = Vector128.LoadUnsafe(ref oneVectorAwayFromEnd);
notEquals = ~(Vector128.Equals(values0, current) | Vector128.Equals(values1, current)
| Vector128.Equals(values2, current) | Vector128.Equals(values3, current));
if (notEquals != Vector128<T>.Zero)
{
return ComputeIndex(ref searchSpace, ref oneVectorAwayFromEnd, notEquals);
}
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
static int ComputeIndex(ref T searchSpace, ref T current, Vector128<T> notEquals)
{
uint notEqualsElements = notEquals.ExtractMostSignificantBits();
int index = BitOperations.TrailingZeroCount(notEqualsElements);
return index + (int)(Unsafe.ByteOffset(ref searchSpace, ref current) / Unsafe.SizeOf<T>());
}
}

return -1;
}
}
}