diff --git a/src/System.Memory/src/System/SpanHelpers.byte.cs b/src/System.Memory/src/System/SpanHelpers.byte.cs index a702ffca5367..fd007a0334e2 100644 --- a/src/System.Memory/src/System/SpanHelpers.byte.cs +++ b/src/System.Memory/src/System/SpanHelpers.byte.cs @@ -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.Count - 1); - nLength = (IntPtr)(uint)unaligned; + nLength = (IntPtr)(uint)((Vector.Count - unaligned) & (Vector.Count - 1)); } } SequentialScan: @@ -122,7 +122,7 @@ public static unsafe int IndexOf(ref byte searchSpace, byte value, int length) { goto NotFound; } - nLength = (IntPtr)(uint)(length - Vector.Count); + nLength = (IntPtr)(uint)((length - (uint)index) & ~(Vector.Count - 1)); // Get comparision Vector Vector vComparision = GetVector(value); while ((byte*)nLength > (byte*)index) @@ -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; }