Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit 0e8c1af

Browse files
committed
128 * 2 alignment
1 parent 04819de commit 0e8c1af

File tree

1 file changed

+21
-24
lines changed

1 file changed

+21
-24
lines changed

src/System.Private.CoreLib/shared/System/SpanHelpers.Byte.cs

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -200,12 +200,7 @@ public static unsafe int IndexOf(ref byte searchSpace, byte value, int length)
200200
IntPtr index = (IntPtr)0; // Use IntPtr for arithmetic to avoid unnecessary 64->32->64 truncations
201201
IntPtr nLength = (IntPtr)length;
202202

203-
if (Avx2.IsSupported && length >= Vector256<byte>.Count * 2)
204-
{
205-
int unaligned = (int)Unsafe.AsPointer(ref searchSpace) & (Vector256<byte>.Count - 1);
206-
nLength = (IntPtr)((Vector256<byte>.Count - unaligned) & (Vector256<byte>.Count - 1));
207-
}
208-
else if (!Avx2.IsSupported && Sse2.IsSupported && length >= Vector128<byte>.Count * 2)
203+
if ((Avx2.IsSupported || Sse2.IsSupported) && length >= Vector128<byte>.Count * 2)
209204
{
210205
int unaligned = (int)Unsafe.AsPointer(ref searchSpace) & (Vector128<byte>.Count - 1);
211206
nLength = (IntPtr)((Vector128<byte>.Count - unaligned) & (Vector128<byte>.Count - 1));
@@ -269,26 +264,28 @@ public static unsafe int IndexOf(ref byte searchSpace, byte value, int length)
269264
if (Avx2.IsSupported && ((int)(byte*)index < length))
270265
{
271266
nLength = (IntPtr)((length - (int)(byte*)index) & ~(Vector256<byte>.Count - 1));
272-
273-
Vector256<byte> comparison256 = Vector256.Create(value);
274-
while ((byte*)nLength > (byte*)index)
267+
if ((byte*)nLength > (byte*)index)
275268
{
276-
Vector256<byte> vSearch = Unsafe.ReadUnaligned<Vector256<byte>>(ref Unsafe.AddByteOffset(ref searchSpace, index));
277-
int matches = Avx2.MoveMask(Avx2.CompareEqual(comparison256, vSearch));
278-
if (matches == 0)
269+
Vector256<byte> comparison256 = Vector256.Create(value);
270+
do
279271
{
280-
index += Vector256<byte>.Count;
281-
continue;
282-
}
283-
// Find offset of first match
284-
else if (Bmi1.IsSupported)
285-
{
286-
return ((int)(byte*)index) + (int)Bmi1.TrailingZeroCount((uint)matches);
287-
}
288-
else
289-
{
290-
return (int)(byte*)index + TrailingZeroCountFallback(matches);
291-
}
272+
Vector256<byte> vSearch = Unsafe.ReadUnaligned<Vector256<byte>>(ref Unsafe.AddByteOffset(ref searchSpace, index));
273+
int matches = Avx2.MoveMask(Avx2.CompareEqual(comparison256, vSearch));
274+
if (matches == 0)
275+
{
276+
index += Vector256<byte>.Count;
277+
continue;
278+
}
279+
// Find offset of first match
280+
else if (Bmi1.IsSupported)
281+
{
282+
return ((int)(byte*)index) + (int)Bmi1.TrailingZeroCount((uint)matches);
283+
}
284+
else
285+
{
286+
return (int)(byte*)index + TrailingZeroCountFallback(matches);
287+
}
288+
} while ((byte*)nLength > (byte*)index);
292289
}
293290

294291
nLength = (IntPtr)((length - (int)(byte*)index) & ~(Vector128<byte>.Count - 1));

0 commit comments

Comments
 (0)