-
Notifications
You must be signed in to change notification settings - Fork 5.1k
AdvSimd support for System.Text.Unicode.Utf8Utility.TranscodeToUtf8 #39041
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
Changes from all commits
6e26a21
f596eaf
f52aaf8
c967b87
328718b
0024ad2
45d03d9
191ec1f
1e5519c
86ef01f
101c4f1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ | |
using System.Numerics; | ||
using System.Runtime.CompilerServices; | ||
using System.Runtime.Intrinsics; | ||
using System.Runtime.Intrinsics.Arm; | ||
using System.Runtime.Intrinsics.X86; | ||
|
||
#if SYSTEM_PRIVATE_CORELIB | ||
|
@@ -882,7 +883,7 @@ public static OperationStatus TranscodeToUtf8(char* pInputBuffer, int inputLengt | |
// is not enabled. | ||
|
||
Unsafe.SkipInit(out Vector128<short> nonAsciiUtf16DataMask); | ||
if (Sse41.X64.IsSupported) | ||
if (Sse41.X64.IsSupported || (AdvSimd.Arm64.IsSupported && BitConverter.IsLittleEndian)) | ||
{ | ||
nonAsciiUtf16DataMask = Vector128.Create(unchecked((short)0xFF80)); // mask of non-ASCII bits in a UTF-16 char | ||
} | ||
|
@@ -940,10 +941,8 @@ public static OperationStatus TranscodeToUtf8(char* pInputBuffer, int inputLengt | |
uint inputCharsRemaining = (uint)(pFinalPosWhereCanReadDWordFromInputBuffer - pInputBuffer) + 2; | ||
uint minElementsRemaining = (uint)Math.Min(inputCharsRemaining, outputBytesRemaining); | ||
|
||
if (Sse41.X64.IsSupported) | ||
if (Sse41.X64.IsSupported || (AdvSimd.Arm64.IsSupported && BitConverter.IsLittleEndian)) | ||
{ | ||
Debug.Assert(BitConverter.IsLittleEndian, "SSE41 requires little-endian."); | ||
|
||
// Try reading and writing 8 elements per iteration. | ||
uint maxIters = minElementsRemaining / 8; | ||
ulong possibleNonAsciiQWord; | ||
|
@@ -952,14 +951,30 @@ public static OperationStatus TranscodeToUtf8(char* pInputBuffer, int inputLengt | |
for (i = 0; (uint)i < maxIters; i++) | ||
{ | ||
utf16Data = Unsafe.ReadUnaligned<Vector128<short>>(pInputBuffer); | ||
if (!Sse41.TestZ(utf16Data, nonAsciiUtf16DataMask)) | ||
|
||
if (AdvSimd.IsSupported) | ||
{ | ||
goto LoopTerminatedDueToNonAsciiDataInVectorLocal; | ||
} | ||
Vector128<short> isUtf16DataNonAscii = AdvSimd.CompareTest(utf16Data, nonAsciiUtf16DataMask); | ||
bool hasNonAsciiDataInVector = AdvSimd.Arm64.MinPairwise(isUtf16DataNonAscii, isUtf16DataNonAscii).AsUInt64().ToScalar() != 0; | ||
|
||
// narrow and write | ||
if (hasNonAsciiDataInVector) | ||
{ | ||
carlossanlop marked this conversation as resolved.
Show resolved
Hide resolved
|
||
goto LoopTerminatedDueToNonAsciiDataInVectorLocal; | ||
} | ||
|
||
Sse2.StoreScalar((ulong*)pOutputBuffer /* unaligned */, Sse2.PackUnsignedSaturate(utf16Data, utf16Data).AsUInt64()); | ||
Vector64<byte> lower = AdvSimd.ExtractNarrowingSaturateUnsignedLower(utf16Data); | ||
AdvSimd.Store(pOutputBuffer, lower); | ||
} | ||
else | ||
{ | ||
if (!Sse41.TestZ(utf16Data, nonAsciiUtf16DataMask)) | ||
{ | ||
goto LoopTerminatedDueToNonAsciiDataInVectorLocal; | ||
} | ||
|
||
carlossanlop marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// narrow and write | ||
Sse2.StoreScalar((ulong*)pOutputBuffer /* unaligned */, Sse2.PackUnsignedSaturate(utf16Data, utf16Data).AsUInt64()); | ||
} | ||
|
||
pInputBuffer += 8; | ||
pOutputBuffer += 8; | ||
|
@@ -978,7 +993,16 @@ public static OperationStatus TranscodeToUtf8(char* pInputBuffer, int inputLengt | |
} | ||
|
||
utf16Data = Vector128.CreateScalarUnsafe(possibleNonAsciiQWord).AsInt16(); | ||
Unsafe.WriteUnaligned<uint>(pOutputBuffer, Sse2.ConvertToUInt32(Sse2.PackUnsignedSaturate(utf16Data, utf16Data).AsUInt32())); | ||
|
||
if (AdvSimd.IsSupported) | ||
{ | ||
Vector64<byte> lower = AdvSimd.ExtractNarrowingSaturateUnsignedLower(utf16Data); | ||
AdvSimd.StoreSelectedScalar((uint*)pOutputBuffer, lower.AsUInt32(), 0); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should comment on this line that |
||
} | ||
else | ||
{ | ||
Unsafe.WriteUnaligned<uint>(pOutputBuffer, Sse2.ConvertToUInt32(Sse2.PackUnsignedSaturate(utf16Data, utf16Data).AsUInt32())); | ||
carlossanlop marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
pInputBuffer += 4; | ||
pOutputBuffer += 4; | ||
|
@@ -990,7 +1014,15 @@ public static OperationStatus TranscodeToUtf8(char* pInputBuffer, int inputLengt | |
LoopTerminatedDueToNonAsciiDataInVectorLocal: | ||
|
||
outputBytesRemaining -= 8 * i; | ||
possibleNonAsciiQWord = Sse2.X64.ConvertToUInt64(utf16Data.AsUInt64()); | ||
|
||
if (Sse2.X64.IsSupported) | ||
{ | ||
possibleNonAsciiQWord = Sse2.X64.ConvertToUInt64(utf16Data.AsUInt64()); | ||
} | ||
else | ||
{ | ||
possibleNonAsciiQWord = utf16Data.AsUInt64().ToScalar(); | ||
carlossanlop marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
// Temporarily set 'possibleNonAsciiQWord' to be the low 64 bits of the vector, | ||
// then check whether it's all-ASCII. If so, narrow and write to the destination | ||
|
@@ -1000,7 +1032,15 @@ public static OperationStatus TranscodeToUtf8(char* pInputBuffer, int inputLengt | |
|
||
if (Utf16Utility.AllCharsInUInt64AreAscii(possibleNonAsciiQWord)) // all chars in first QWORD are ASCII | ||
{ | ||
Unsafe.WriteUnaligned<uint>(pOutputBuffer, Sse2.ConvertToUInt32(Sse2.PackUnsignedSaturate(utf16Data, utf16Data).AsUInt32())); | ||
if (AdvSimd.IsSupported) | ||
{ | ||
Vector64<byte> lower = AdvSimd.ExtractNarrowingSaturateUnsignedLower(utf16Data); | ||
AdvSimd.StoreSelectedScalar((uint*)pOutputBuffer, lower.AsUInt32(), 0); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add a comment saying There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If that's ok, I can add this to a list of TODOs for the next PR we open for intrinsics changes (there are things that the FC may have to address later). The CI is taking a really long time and I would like to get this merged today. I would like to avoid resetting the CI. |
||
} | ||
else | ||
{ | ||
Unsafe.WriteUnaligned<uint>(pOutputBuffer, Sse2.ConvertToUInt32(Sse2.PackUnsignedSaturate(utf16Data, utf16Data).AsUInt32())); | ||
} | ||
pInputBuffer += 4; | ||
pOutputBuffer += 4; | ||
outputBytesRemaining -= 4; | ||
|
Uh oh!
There was an error while loading. Please reload this page.