-
Notifications
You must be signed in to change notification settings - Fork 5.2k
AdvSimd support for System.Text.Unicode.Utf8Utility.GetPointerToFirstInvalidByte #38653
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 9 commits
38e528b
db7a4b1
3daf5cc
3fb9b55
3a340b7
45bb8dd
d4e5497
d761124
af42e59
bb07819
55dd236
46bbf26
6f4cca9
b2d3705
8536a8d
a45fe16
b09e92c
5c3cee2
d9dd878
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 |
|---|---|---|
|
|
@@ -4,6 +4,8 @@ | |
| using System.Diagnostics; | ||
| 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 | ||
|
|
@@ -117,22 +119,32 @@ internal static unsafe partial class Utf8Utility | |
| // the alignment check consumes at most a single DWORD.) | ||
|
|
||
| byte* pInputBufferFinalPosAtWhichCanSafelyLoop = pFinalPosWhereCanReadDWordFromInputBuffer - 3 * sizeof(uint); // can safely read 4 DWORDs here | ||
| uint mask; | ||
| ulong mask; | ||
|
|
||
| Vector128<byte> initialMask = Vector128.Create((ushort)0x1001).AsByte(); | ||
| Vector128<byte> mostSignficantBitMask = Vector128.Create((byte)0x80).AsByte(); | ||
|
|
||
| do | ||
| { | ||
| if (Sse2.IsSupported) | ||
| // pInputBuffer is 32-bit aligned but not necessary 128-bit aligned, so we're | ||
| // going to perform an unaligned load. We don't necessarily care about aligning | ||
| // this because we pessimistically assume we'll encounter non-ASCII data at some | ||
| // point in the not-too-distant future (otherwise we would've stayed entirely | ||
| // within the all-ASCII vectorized code at the entry to this method). | ||
| if (AdvSimd.Arm64.IsSupported && BitConverter.IsLittleEndian) | ||
| { | ||
| // pInputBuffer is 32-bit aligned but not necessary 128-bit aligned, so we're | ||
| // going to perform an unaligned load. We don't necessarily care about aligning | ||
| // this because we pessimistically assume we'll encounter non-ASCII data at some | ||
| // point in the not-too-distant future (otherwise we would've stayed entirely | ||
| // within the all-ASCII vectorized code at the entry to this method). | ||
|
|
||
| mask = (uint)Sse2.MoveMask(Sse2.LoadVector128((byte*)pInputBuffer)); | ||
| mask = GetNonAsciiBytes(AdvSimd.LoadVector128(pInputBuffer), initialMask, mostSignficantBitMask); | ||
| if (mask != 0) | ||
| { | ||
| goto LoopTerminatedEarlyDueToNonAsciiData; | ||
| } | ||
| } | ||
| else if (Sse2.IsSupported) | ||
| { | ||
| mask = (ulong)Sse2.MoveMask(Sse2.LoadVector128(pInputBuffer)); | ||
| if (mask != 0) | ||
| { | ||
| goto Sse2LoopTerminatedEarlyDueToNonAsciiData; | ||
| goto LoopTerminatedEarlyDueToNonAsciiData; | ||
| } | ||
| } | ||
| else | ||
|
|
@@ -153,10 +165,11 @@ internal static unsafe partial class Utf8Utility | |
|
|
||
| continue; // need to perform a bounds check because we might be running out of data | ||
|
|
||
| Sse2LoopTerminatedEarlyDueToNonAsciiData: | ||
|
|
||
| LoopTerminatedEarlyDueToNonAsciiData: | ||
| // x86 can only be little endian, while ARM can be big or little endian | ||
| // so if we reached this label we need to check both combinations are supported | ||
| Debug.Assert(BitConverter.IsLittleEndian); | ||
| Debug.Assert(Sse2.IsSupported); | ||
| Debug.Assert(AdvSimd.Arm64.IsSupported || Sse2.IsSupported); | ||
|
||
|
|
||
| // The 'mask' value will have a 0 bit for each ASCII byte we saw and a 1 bit | ||
| // for each non-ASCII byte we saw. We can count the number of ASCII bytes, | ||
|
|
@@ -719,5 +732,16 @@ internal static unsafe partial class Utf8Utility | |
| scalarCountAdjustment = tempScalarCountAdjustment; | ||
| return pInputBuffer; | ||
| } | ||
|
|
||
| [MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
| private static ulong GetNonAsciiBytes(Vector128<byte> value, Vector128<byte> initialMask, Vector128<byte> mostSignficantBitMask) | ||
| { | ||
| Debug.Assert(AdvSimd.Arm64.IsSupported); | ||
|
|
||
carlossanlop marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| Vector128<byte> mostSignificantBitIsSet = AdvSimd.CompareEqual(AdvSimd.And(value, mostSignficantBitMask), mostSignficantBitMask); | ||
| Vector128<byte> extractedBits = AdvSimd.And(mostSignificantBitIsSet, initialMask); | ||
carlossanlop marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| extractedBits = AdvSimd.Arm64.AddPairwise(extractedBits, extractedBits); | ||
| return extractedBits.AsUInt64().ToScalar(); | ||
| } | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.