Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.
Merged
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
6 changes: 3 additions & 3 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 - 1));
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Actually, unaligned will always be < Vector<byte>.Count so (from mask on line above) the & part isn't needed?

Copy link
Copy Markdown
Author

@ahsonkhan ahsonkhan Mar 25, 2017

Choose a reason for hiding this comment

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

What if unaligned == 0?
If we don't do the &, nLength would equal Vector<byte>.Count rather than 0.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

True

}
}
SequentialScan:
Expand Down Expand Up @@ -122,7 +122,7 @@ 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) & ~(Vector<byte>.Count - 1));
// Get comparision Vector
Vector<byte> vComparision = GetVector(value);
while ((byte*)nLength > (byte*)index)
Expand All @@ -139,7 +139,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