Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace Utf16Utility.GetPointerToFirstInvalidChar fallback with Vector128 #90391

Merged
merged 3 commits into from
Aug 11, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -311,16 +311,16 @@ internal static unsafe partial class Utf16Utility
pInputBuffer -= Vector128<ushort>.Count;
}
}
else if (Vector.IsHardwareAccelerated)
else if (Vector128.IsHardwareAccelerated)
{
if (inputLength >= Vector<ushort>.Count)
if (inputLength >= Vector128<ushort>.Count)
{
Vector<ushort> vector0080 = new Vector<ushort>(0x0080);
Vector<ushort> vector0400 = new Vector<ushort>(0x0400);
Vector<ushort> vector0800 = new Vector<ushort>(0x0800);
Vector<ushort> vectorD800 = new Vector<ushort>(0xD800);
Vector128<ushort> vector0080 = Vector128.Create<ushort>(0x0080);
Vector128<ushort> vector0400 = Vector128.Create<ushort>(0x0400);
Vector128<ushort> vector0800 = Vector128.Create<ushort>(0x0800);
Vector128<ushort> vectorD800 = Vector128.Create<ushort>(0xD800);

char* pHighestAddressWhereCanReadOneVector = pEndOfInputBuffer - Vector<ushort>.Count;
char* pHighestAddressWhereCanReadOneVector = pEndOfInputBuffer - Vector128<ushort>.Count;
Debug.Assert(pHighestAddressWhereCanReadOneVector >= pInputBuffer);

do
Expand All @@ -340,16 +340,16 @@ internal static unsafe partial class Utf16Utility
// performed by the SSE2 code path. This will overcount surrogates, but we'll
// handle that shortly.

Vector<ushort> utf16Data = Unsafe.ReadUnaligned<Vector<ushort>>(pInputBuffer);
Vector<ushort> twoOrMoreUtf8Bytes = Vector.GreaterThanOrEqual(utf16Data, vector0080);
Vector<ushort> threeOrMoreUtf8Bytes = Vector.GreaterThanOrEqual(utf16Data, vector0800);
Vector<nuint> sumVector = (Vector<nuint>)(Vector<ushort>.Zero - twoOrMoreUtf8Bytes - threeOrMoreUtf8Bytes);
Vector128<ushort> utf16Data = Vector128.Load((ushort*)pInputBuffer);
Vector128<ushort> twoOrMoreUtf8Bytes = Vector128.GreaterThanOrEqual(utf16Data, vector0080);
Vector128<ushort> threeOrMoreUtf8Bytes = Vector128.GreaterThanOrEqual(utf16Data, vector0800);
Vector128<nuint> sumVector = (Vector128<ushort>.Zero - twoOrMoreUtf8Bytes - threeOrMoreUtf8Bytes).AsNUInt();

// We'll try summing by a natural word (rather than a 16-bit word) at a time,
// which should halve the number of operations we must perform.

nuint popcnt = 0;
for (int i = 0; i < Vector<nuint>.Count; i++)
for (int i = 0; i < Vector128<nuint>.Count; i++)
{
popcnt += (nuint)sumVector[i];
}
Expand All @@ -368,16 +368,16 @@ internal static unsafe partial class Utf16Utility
// Now check for surrogates.

utf16Data -= vectorD800;
Vector<ushort> surrogateChars = Vector.LessThan(utf16Data, vector0800);
if (surrogateChars != Vector<ushort>.Zero)
Vector128<ushort> surrogateChars = Vector128.LessThan(utf16Data, vector0800);
if (surrogateChars != Vector128<ushort>.Zero)
{
// There's at least one surrogate (high or low) UTF-16 code unit in
// the vector. We'll build up additional vectors: 'highSurrogateChars'
// and 'lowSurrogateChars', where the elements are 0xFFFF iff the original
// UTF-16 code unit was a high or low surrogate, respectively.

Vector<ushort> highSurrogateChars = Vector.LessThan(utf16Data, vector0400);
Vector<ushort> lowSurrogateChars = Vector.AndNot(surrogateChars, highSurrogateChars);
Vector128<ushort> highSurrogateChars = Vector128.LessThan(utf16Data, vector0400);
Vector128<ushort> lowSurrogateChars = Vector128.AndNot(surrogateChars, highSurrogateChars);

// We want to make sure that each high surrogate code unit is followed by
// a low surrogate code unit and each low surrogate code unit follows a
Expand All @@ -392,7 +392,7 @@ internal static unsafe partial class Utf16Utility
}

ushort surrogatePairsCount = 0;
for (int i = 0; i < Vector<ushort>.Count - 1; i++)
for (int i = 0; i < Vector128<ushort>.Count - 1; i++)
{
surrogatePairsCount -= highSurrogateChars[i]; // turns into +1 or +0
if (highSurrogateChars[i] != lowSurrogateChars[i + 1])
Expand All @@ -401,7 +401,7 @@ internal static unsafe partial class Utf16Utility
}
}

if (highSurrogateChars[Vector<ushort>.Count - 1] != 0)
if (highSurrogateChars[Vector128<ushort>.Count - 1] != 0)
{
// There was a standalone high surrogate at the end of the vector.
// We'll adjust our counters so that we don't consider this char consumed.
Expand All @@ -426,7 +426,7 @@ internal static unsafe partial class Utf16Utility
}

tempUtf8CodeUnitCountAdjustment += popcnt32;
pInputBuffer += Vector<ushort>.Count;
pInputBuffer += Vector128<ushort>.Count;
} while (pInputBuffer <= pHighestAddressWhereCanReadOneVector);
}
}
Expand Down