-
Notifications
You must be signed in to change notification settings - Fork 5.4k
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 18 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,35 @@ 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; | ||
| int trailingZeroCount; | ||
|
|
||
| Vector128<byte> bitMask128 = BitConverter.IsLittleEndian ? | ||
| Vector128.Create((ushort)0x1001).AsByte() : | ||
| Vector128.Create((ushort)0x0110).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)); | ||
| ulong mask = GetNonAsciiBytes(AdvSimd.LoadVector128(pInputBuffer), bitMask128); | ||
| if (mask != 0) | ||
| { | ||
| trailingZeroCount = BitOperations.TrailingZeroCount(mask) >> 2; | ||
|
carlossanlop marked this conversation as resolved.
Outdated
|
||
| goto LoopTerminatedEarlyDueToNonAsciiData; | ||
| } | ||
| } | ||
| else if (Sse2.IsSupported) | ||
| { | ||
| uint mask = (uint)Sse2.MoveMask(Sse2.LoadVector128(pInputBuffer)); | ||
| if (mask != 0) | ||
| { | ||
| goto Sse2LoopTerminatedEarlyDueToNonAsciiData; | ||
| trailingZeroCount = BitOperations.TrailingZeroCount(mask); | ||
|
carlossanlop marked this conversation as resolved.
Outdated
|
||
| goto LoopTerminatedEarlyDueToNonAsciiData; | ||
| } | ||
| } | ||
| else | ||
|
|
@@ -153,19 +168,20 @@ 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((AdvSimd.Arm64.IsSupported && BitConverter.IsLittleEndian) || Sse2.IsSupported); | ||
|
|
||
| Debug.Assert(BitConverter.IsLittleEndian); | ||
| Debug.Assert(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, | ||
| // for each non-ASCII byte we saw. trailingZeroCount will count the number of ASCII bytes, | ||
| // bump our input counter by that amount, and resume processing from the | ||
| // "the first byte is no longer ASCII" portion of the main loop. | ||
| // We should not expect a total number of zeroes equal or larger than 16. | ||
|
Contributor
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.
minor: I would re-phrase the comment to "Make sure that pInputBuffer is not advanced by more than 15 positions."
Contributor
Author
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 with you, I would like to address this in another PR so that I don't reset the CI (it's taking a really long time to finish).
Contributor
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. Sure. No problem. |
||
| Debug.Assert(trailingZeroCount < 16); | ||
|
|
||
| Debug.Assert(mask != 0); | ||
|
|
||
| pInputBuffer += BitOperations.TrailingZeroCount(mask); | ||
| pInputBuffer += trailingZeroCount; | ||
|
carlossanlop marked this conversation as resolved.
carlossanlop marked this conversation as resolved.
|
||
| if (pInputBuffer > pFinalPosWhereCanReadDWordFromInputBuffer) | ||
| { | ||
| goto ProcessRemainingBytesSlow; | ||
|
|
@@ -719,5 +735,19 @@ internal static unsafe partial class Utf8Utility | |
| scalarCountAdjustment = tempScalarCountAdjustment; | ||
| return pInputBuffer; | ||
| } | ||
|
|
||
| [MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
| private static ulong GetNonAsciiBytes(Vector128<byte> value, Vector128<byte> bitMask128) | ||
| { | ||
| if (!AdvSimd.Arm64.IsSupported || !BitConverter.IsLittleEndian) | ||
| { | ||
| throw new PlatformNotSupportedException(); | ||
| } | ||
|
|
||
|
carlossanlop marked this conversation as resolved.
Outdated
|
||
| Vector128<byte> mostSignificantBitIsSet = AdvSimd.ShiftRightArithmetic(value.AsSByte(), 7).AsByte(); | ||
| Vector128<byte> extractedBits = AdvSimd.And(mostSignificantBitIsSet, bitMask128); | ||
| extractedBits = AdvSimd.Arm64.AddPairwise(extractedBits, extractedBits); | ||
| return extractedBits.AsUInt64().ToScalar(); | ||
| } | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.