Skip to content
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -611,20 +611,52 @@ public static unsafe int IndexOf(ref byte searchSpace, byte value, int length)
}
else if (AdvSimd.Arm64.IsSupported)
{
Vector128<byte> mask = Vector128.Create((ushort)0x1001).AsByte();
// Mask to help find the first lane in compareResult that is set.
// MSB 0x10 corresponds to 1st lane, 0x01 corresponds to 0th lane and so forth.
Vector128<byte> values = Vector128.Create(value);
int matchedLane = 0;

if (offset < (nuint)(uint)length)
{
lengthToExamine = GetByteVector128SpanLength(offset, length);
// Try to process data using two Vector128 since we don't have Vector256 on ARM
lengthToExamine = GetByteTwoVector128SpanLength(offset, length);
if (lengthToExamine > offset)
{
while (lengthToExamine > offset)
{
Vector128<byte> search1 = Vector128.LoadUnsafe(ref searchSpace, offset);
Vector128<byte> search2 = Vector128.LoadUnsafe(ref searchSpace, offset + (nuint)Vector128<byte>.Count);

// Mask to help find the first lane in compareResult that is set.
// MSB 0x10 corresponds to 1st lane, 0x01 corresponds to 0th lane and so forth.
Vector128<byte> mask = Vector128.Create((ushort)0x1001).AsByte();
int matchedLane = 0;
Vector128<byte> compareResult1 = Vector128.Equals(values, search1);
Vector128<byte> compareResult2 = Vector128.Equals(values, search2);

Vector128<byte> values = Vector128.Create(value);
// Fast path: no matches in both comparisons
if ((compareResult1 | compareResult2) == Vector128<byte>.Zero)
{
offset += (nuint)Vector128<byte>.Count * 2;
continue;
}

if (!TryFindFirstMatchedLane(mask, compareResult1, ref matchedLane))
{
// The match is in the second comparison
offset += (nuint)Vector128<byte>.Count;
bool found = TryFindFirstMatchedLane(mask, compareResult2, ref matchedLane);
Debug.Assert(found);
}
return (int)(offset + (uint)matchedLane);
}
}
}

if (offset < (nuint)(uint)length)
{
lengthToExamine = GetByteVector128SpanLength(offset, length);
while (lengthToExamine > offset)
{
Vector128<byte> search = LoadVector128(ref searchSpace, offset);
Vector128<byte> compareResult = AdvSimd.CompareEqual(values, search);
Vector128<byte> search = Vector128.LoadUnsafe(ref searchSpace, offset);
Vector128<byte> compareResult = Vector128.Equals(values, search);

if (!TryFindFirstMatchedLane(mask, compareResult, ref matchedLane))
{
Expand Down Expand Up @@ -2213,6 +2245,10 @@ private static nuint GetByteVectorSpanLength(nuint offset, int length)
private static nuint GetByteVector128SpanLength(nuint offset, int length)
=> (nuint)(uint)((length - (int)offset) & ~(Vector128<byte>.Count - 1));

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static nuint GetByteTwoVector128SpanLength(nuint offset, int length)
=> (nuint)(uint)((length - (int)offset) & ~(Vector128<byte>.Count * 2 - 1));

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static nuint GetByteVector256SpanLength(nuint offset, int length)
=> (nuint)(uint)((length - (int)offset) & ~(Vector256<byte>.Count - 1));
Expand Down