Skip to content
This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
/ corefx Public archive

Commit

Permalink
Fixing Span IndexOf vectorization.
Browse files Browse the repository at this point in the history
  • Loading branch information
ahsonkhan committed Mar 24, 2017
1 parent d1c4eea commit 22e4c23
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions src/System.Memory/src/System/SpanHelpers.byte.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public static unsafe int IndexOf(ref byte searchSpace, byte value, int length)
unchecked
{
int unaligned = (int)(byte*)Unsafe.AsPointer(ref searchSpace) & (Vector<byte>.Count - 1);
nLength = (IntPtr)(uint)unaligned;
nLength = (IntPtr)(uint)((Vector<byte>.Count - unaligned) % Vector<byte>.Count);
}
}
SequentialScan:
Expand Down Expand Up @@ -122,15 +122,16 @@ public static unsafe int IndexOf(ref byte searchSpace, byte value, int length)
{
goto NotFound;
}
nLength = (IntPtr)(uint)(length - Vector<byte>.Count);
nLength = (IntPtr)(uint)(length - (uint)index);
// Get comparision Vector
Vector<byte> vComparision = GetVector(value);
while ((byte*)nLength > (byte*)index)
while ((byte*)nLength >= (byte*)Vector<byte>.Count)
{
var vMatches = Vector.Equals(vComparision, Unsafe.ReadUnaligned<Vector<byte>>(ref Unsafe.AddByteOffset(ref searchSpace, index)));
if (Vector<byte>.Zero.Equals(vMatches))
{
index += Vector<byte>.Count;
nLength -= Vector<byte>.Count;
continue;
}
// Found match, reuse Vector vComparision to keep register pressure low
Expand All @@ -139,7 +140,7 @@ public static unsafe int IndexOf(ref byte searchSpace, byte value, int length)
goto VectorFound;
}

if ((int)(byte*)index > length)
if ((int)(byte*)index >= length)
{
goto NotFound;
}
Expand Down

0 comments on commit 22e4c23

Please sign in to comment.