Skip to content
Open
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 @@ -2171,7 +2171,7 @@ private static void MakeSeparatorListVectorized(ReadOnlySpan<char> sourceSpan, r
}
}

while ((uint)remaining.Length >= (uint)Vector512<ushort>.Count)
while ((uint)remaining.Length > (uint)Vector512<ushort>.Count)
{
Vector512<ushort> vector = Vector512.Create(remaining);
Vector512<byte> cmp = Vector512.Equals(vector, v1).AsByte() | Vector512.Equals(vector, v2).AsByte() | Vector512.Equals(vector, v3).AsByte();
Expand All @@ -2194,17 +2194,21 @@ private static void MakeSeparatorListVectorized(ReadOnlySpan<char> sourceSpan, r

// Handle the last chunk in a vectorized way also.
// We do a whole vector's worth again, but just mask out the bits we've already handled.
if (remaining.Length > 0)
// Note: when Avx512BW.IsSupported is false, we never enter the first block, and thus remaining.Length is always > 0.
if (!Avx512BW.IsSupported || remaining.Length > 0)
{
Vector512<ushort> vector = Vector512.Create(sourceSpanUInt16.Slice(sourceSpanUInt16.Length - Vector512<ushort>.Count));
Vector512<byte> cmp = Vector512.Equals(vector, v1).AsByte() | Vector512.Equals(vector, v2).AsByte() | Vector512.Equals(vector, v3).AsByte();
int finalIndex = sourceSpanUInt16.Length - Vector512<ushort>.Count;
ulong mask = cmp.ExtractMostSignificantBits() & 0x5555555555555555 & ~((1UL << (Vector512<byte>.Count - remaining.Length * sizeof(char))) - 1);
while (mask != 0)
if (cmp != Vector512<byte>.Zero)
{
uint bitPos = (uint)BitOperations.TrailingZeroCount(mask) / sizeof(char);
sepListBuilder.Append(finalIndex + (int)bitPos);
mask = BitOperations.ResetLowestSetBit(mask);
int finalIndex = sourceSpanUInt16.Length - Vector512<ushort>.Count;
ulong mask = cmp.ExtractMostSignificantBits() & 0x5555555555555555 & ~((1UL << (Vector512<byte>.Count - remaining.Length * sizeof(char))) - 1);
while (mask != 0)
{
uint bitPos = (uint)BitOperations.TrailingZeroCount(mask) / sizeof(char);
sepListBuilder.Append(finalIndex + (int)bitPos);
mask = BitOperations.ResetLowestSetBit(mask);
}
}
}
return;
Expand Down Expand Up @@ -2266,7 +2270,7 @@ private static void MakeSeparatorListVectorized(ReadOnlySpan<char> sourceSpan, r
}
}

while ((uint)remaining.Length >= (uint)Vector256<ushort>.Count)
while ((uint)remaining.Length > (uint)Vector256<ushort>.Count)
{
Vector256<ushort> vector = Vector256.Create(remaining);
Vector256<byte> cmp = Vector256.Equals(vector, v1).AsByte() | Vector256.Equals(vector, v2).AsByte() | Vector256.Equals(vector, v3).AsByte();
Expand All @@ -2289,17 +2293,21 @@ private static void MakeSeparatorListVectorized(ReadOnlySpan<char> sourceSpan, r

// Handle the last chunk in a vectorized way also.
// We do a whole vector's worth again, but just mask out the bits we've already handled.
if (remaining.Length > 0)
// Note: when Avx2.IsSupported is false, we never enter the first block, and thus remaining.Length is always > 0.
if (!Avx2.IsSupported || remaining.Length > 0)
{
Vector256<ushort> vector = Vector256.Create(sourceSpanUInt16.Slice(sourceSpanUInt16.Length - Vector256<ushort>.Count));
Vector256<byte> cmp = Vector256.Equals(vector, v1).AsByte() | Vector256.Equals(vector, v2).AsByte() | Vector256.Equals(vector, v3).AsByte();
int finalIndex = sourceSpanUInt16.Length - Vector256<ushort>.Count;
uint mask = cmp.ExtractMostSignificantBits() & 0x55555555 & ~((1u << (Vector256<byte>.Count - remaining.Length * sizeof(char))) - 1);
while (mask != 0)
if (cmp != Vector256<byte>.Zero)
{
uint bitPos = (uint)BitOperations.TrailingZeroCount(mask) / sizeof(char);
sepListBuilder.Append(finalIndex + (int)bitPos);
mask = BitOperations.ResetLowestSetBit(mask);
int finalIndex = sourceSpanUInt16.Length - Vector256<ushort>.Count;
uint mask = cmp.ExtractMostSignificantBits() & 0x55555555 & ~((1u << (Vector256<byte>.Count - remaining.Length * sizeof(char))) - 1);
while (mask != 0)
{
uint bitPos = (uint)BitOperations.TrailingZeroCount(mask) / sizeof(char);
sepListBuilder.Append(finalIndex + (int)bitPos);
mask = BitOperations.ResetLowestSetBit(mask);
}
}
}
return;
Expand Down Expand Up @@ -2361,7 +2369,7 @@ private static void MakeSeparatorListVectorized(ReadOnlySpan<char> sourceSpan, r
}
}

while ((uint)remaining.Length >= (uint)Vector128<ushort>.Count)
while ((uint)remaining.Length > (uint)Vector128<ushort>.Count)
{
Vector128<ushort> vector = Vector128.Create(remaining);
Vector128<byte> cmp = Vector128.Equals(vector, v1).AsByte() | Vector128.Equals(vector, v2).AsByte() | Vector128.Equals(vector, v3).AsByte();
Expand All @@ -2384,17 +2392,23 @@ private static void MakeSeparatorListVectorized(ReadOnlySpan<char> sourceSpan, r

// Handle the last chunk in a vectorized way also.
// We do a whole vector's worth again, but just mask out the bits we've already handled.
if (remaining.Length > 0)
// Note: when Sse.IsSupported is false, we never enter the first block, and thus remaining.Length is always > 0.
if (!Sse.IsSupported || remaining.Length > 0)
{
Vector128<ushort> vector = Vector128.Create(sourceSpanUInt16.Slice(sourceSpanUInt16.Length - Vector128<ushort>.Count));
Debug.Assert(sourceSpanUInt16.Length >= Vector128<ushort>.Count);
Vector128<ushort> vector = Vector128.LoadUnsafe(in MemoryMarshal.GetReference(sourceSpanUInt16), (uint)(sourceSpanUInt16.Length - Vector128<ushort>.Count));

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.

please don't replace safe code with unsafe

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

It was unsafe before the PR that introduced the regression. Part of the regression is likely the bounds check that exists here now.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

That PR removed a lot more unsafe than just this one spot, and we only need to add this one spot back to get no bounds checks - surely that is fine?

Vector128<byte> cmp = Vector128.Equals(vector, v1).AsByte() | Vector128.Equals(vector, v2).AsByte() | Vector128.Equals(vector, v3).AsByte();
int finalIndex = sourceSpanUInt16.Length - Vector128<ushort>.Count;
uint mask = cmp.ExtractMostSignificantBits() & 0x5555 & ~((1u << (Vector128<byte>.Count - remaining.Length * sizeof(char))) - 1);
while (mask != 0)
if (cmp != Vector128<byte>.Zero)
{
uint bitPos = (uint)BitOperations.TrailingZeroCount(mask) / sizeof(char);
sepListBuilder.Append(finalIndex + (int)bitPos);
mask = BitOperations.ResetLowestSetBit(mask);
cmp &= Vector128.Create(0x0101010101010101UL).AsByte();
int finalIndex = sourceSpanUInt16.Length - Vector128<ushort>.Count;
uint mask = cmp.ExtractMostSignificantBits() & 0x5555 & ~((1u << (Vector128<byte>.Count - remaining.Length * sizeof(char))) - 1);
while (mask != 0)
{
uint bitPos = (uint)BitOperations.TrailingZeroCount(mask) / sizeof(char);
sepListBuilder.Append(finalIndex + (int)bitPos);
mask = BitOperations.ResetLowestSetBit(mask);
}
}
}
return;
Expand Down
Loading