Skip to content
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
38e528b
AdvSimd support for System.Text.Unicode.Utf8Utility.GetPointerToFirst…
carlossanlop Jul 1, 2020
db7a4b1
Move comment to the top, add shims.
carlossanlop Jul 1, 2020
3daf5cc
Little endian checks
carlossanlop Jul 1, 2020
3fb9b55
Use custom MoveMask method for AdvSimd
carlossanlop Jul 10, 2020
3a340b7
Address suggestions to improve the AdvSimdMoveMask method
carlossanlop Jul 10, 2020
45bb8dd
Define initialMask outside MoveMask method
carlossanlop Jul 10, 2020
d4e5497
UInt64 in Arm64MoveMask
carlossanlop Jul 13, 2020
d761124
Add unit test case to verify intrinsics improvement
carlossanlop Jul 14, 2020
af42e59
Avoid casting to smaller integer type
carlossanlop Jul 14, 2020
bb07819
Typo and comment
carlossanlop Jul 14, 2020
55dd236
Use ShiftRightArithmetic instead of CompareEqual + And.
carlossanlop Jul 14, 2020
46bbf26
Use AddPairwise version of GetNotAsciiBytes
carlossanlop Jul 14, 2020
6f4cca9
Add missing shims causing Linux build to fail
carlossanlop Jul 15, 2020
b2d3705
Simplify GetNonAsciiBytes to only one AddPairwise call, shorter bitmask
carlossanlop Jul 16, 2020
8536a8d
Respect data type returned by masking method
carlossanlop Jul 16, 2020
a45fe16
Address suggestions - assert trailingzerocount and bring back uint mask
carlossanlop Jul 17, 2020
b09e92c
Trailing zeroes in AdvSimd need to be divided by 4, and total number …
carlossanlop Jul 17, 2020
5c3cee2
Avoid declaring static field which causes PNSE in Utf8String.Experime…
carlossanlop Jul 17, 2020
d9dd878
Prefer using nuint for BitConverter.TrailingZeroCount
carlossanlop Jul 20, 2020
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 @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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);
Copy link
Contributor

Choose a reason for hiding this comment

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

AdvSimd.Arm64.IsSupported [](start = 37, length = 26)

Reading the comments, won't the assert be :

Debug.Assert(AdvSimd.Arm64.IsSupported || (Sse2.IsSupported && BitConverter.IsLittleEndian));

Copy link
Contributor Author

@carlossanlop carlossanlop Jul 15, 2020

Choose a reason for hiding this comment

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

I'm not sure I understand your comment. Is that assert correct?
The line above it is already checking for little endian. Is it ok if these asserts stay like they currently are?


// 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,
Expand Down Expand Up @@ -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);

Vector128<byte> mostSignificantBitIsSet = AdvSimd.CompareEqual(AdvSimd.And(value, mostSignficantBitMask), mostSignficantBitMask);
Vector128<byte> extractedBits = AdvSimd.And(mostSignificantBitIsSet, initialMask);
extractedBits = AdvSimd.Arm64.AddPairwise(extractedBits, extractedBits);
return extractedBits.AsUInt64().ToScalar();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,7 @@ public static IEnumerable<object[]> TranscodingTestData()
yield return new object[] { "Hello" }; // simple ASCII
yield return new object[] { "a\U00000123b\U00001234c\U00101234d" }; // with multi-byte sequences of varying lengths
yield return new object[] { "\uF8FF\uE000\U000FFFFF" }; // with scalars from the private use areas
yield return new object[] { "\u00E921222324303132333435363738393A3B3C3D3E3F3031323334353637\u00E938393A3B3C3D3E3F" }; // reaches the intrinsics optimizations in System.Text.Unicode.Utf8Utility.GetPointerToFirstInvalidByte
}
}
}