Skip to content

Commit ea913db

Browse files
committed
Code path refactoring and cleanup.
1 parent 01a43e7 commit ea913db

File tree

1 file changed

+54
-18
lines changed

1 file changed

+54
-18
lines changed

src/libraries/System.Private.CoreLib/src/System/Text/Ascii.Utility.cs

Lines changed: 54 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -130,9 +130,9 @@ private static unsafe nuint GetIndexOfFirstNonAsciiByte_Vector(byte* pBuffer, nu
130130

131131
if (Vector512.IsHardwareAccelerated && bufferLength >= 2 * (uint)Vector512<byte>.Count)
132132
{
133-
uint SizeOfVector512InBytes = (uint)Vector512<byte>.Count; // JIT will make this a const
133+
uint SizeOfVector512InBytes = (uint)Vector512.Size; // JIT will make this a const
134134

135-
if (Unsafe.ReadUnaligned<Vector512<byte>>(pBuffer).ExtractMostSignificantBits() == 0)
135+
if (!VectorContainsNonAsciiChar(Vector512.Load(pBuffer)))
136136
{
137137
// The first several elements of the input buffer were ASCII. Bump up the pointer to the
138138
// next aligned boundary, then perform aligned reads from here on out until we find non-ASCII
@@ -152,7 +152,7 @@ private static unsafe nuint GetIndexOfFirstNonAsciiByte_Vector(byte* pBuffer, nu
152152
do
153153
{
154154
Debug.Assert((nuint)pBuffer % SizeOfVector512InBytes == 0, "Vector read should be aligned.");
155-
if (Unsafe.Read<Vector512<byte>>(pBuffer).ExtractMostSignificantBits() != 0)
155+
if (VectorContainsNonAsciiChar(Vector512.LoadAligned(pBuffer)))
156156
{
157157
break; // found non-ASCII data
158158
}
@@ -168,9 +168,9 @@ private static unsafe nuint GetIndexOfFirstNonAsciiByte_Vector(byte* pBuffer, nu
168168
}
169169
else if (Vector256.IsHardwareAccelerated && bufferLength >= 2 * (uint)Vector256<byte>.Count)
170170
{
171-
uint SizeOfVector256InBytes = (uint)Vector256<byte>.Count; // JIT will make this a const
171+
uint SizeOfVector256InBytes = (uint)Vector256.Size; // JIT will make this a const
172172

173-
if (Unsafe.ReadUnaligned<Vector256<byte>>(pBuffer).ExtractMostSignificantBits() == 0)
173+
if (!VectorContainsNonAsciiChar(Vector256.Load(pBuffer)))
174174
{
175175
// The first several elements of the input buffer were ASCII. Bump up the pointer to the
176176
// next aligned boundary, then perform aligned reads from here on out until we find non-ASCII
@@ -190,7 +190,7 @@ private static unsafe nuint GetIndexOfFirstNonAsciiByte_Vector(byte* pBuffer, nu
190190
do
191191
{
192192
Debug.Assert((nuint)pBuffer % SizeOfVector256InBytes == 0, "Vector read should be aligned.");
193-
if (Unsafe.Read<Vector256<byte>>(pBuffer).ExtractMostSignificantBits() != 0)
193+
if (VectorContainsNonAsciiChar(Vector256.LoadAligned(pBuffer)))
194194
{
195195
break; // found non-ASCII data
196196
}
@@ -206,9 +206,9 @@ private static unsafe nuint GetIndexOfFirstNonAsciiByte_Vector(byte* pBuffer, nu
206206
}
207207
else if (Vector128.IsHardwareAccelerated && bufferLength >= 2 * (uint)Vector128<byte>.Count)
208208
{
209-
uint SizeOfVector128InBytes = (uint)Vector128<byte>.Count; // JIT will make this a const
209+
uint SizeOfVector128InBytes = (uint)Vector128.Size; // JIT will make this a const
210210

211-
if (Unsafe.ReadUnaligned<Vector128<byte>>(pBuffer).ExtractMostSignificantBits() == 0)
211+
if (!VectorContainsNonAsciiChar(Vector128.Load(pBuffer)))
212212
{
213213
// The first several elements of the input buffer were ASCII. Bump up the pointer to the
214214
// next aligned boundary, then perform aligned reads from here on out until we find non-ASCII
@@ -228,7 +228,7 @@ private static unsafe nuint GetIndexOfFirstNonAsciiByte_Vector(byte* pBuffer, nu
228228
do
229229
{
230230
Debug.Assert((nuint)pBuffer % SizeOfVector128InBytes == 0, "Vector read should be aligned.");
231-
if (Unsafe.Read<Vector128<byte>>(pBuffer).ExtractMostSignificantBits() != 0)
231+
if (VectorContainsNonAsciiChar(Vector128.LoadAligned(pBuffer)))
232232
{
233233
break; // found non-ASCII data
234234
}
@@ -899,11 +899,11 @@ private static unsafe nuint GetIndexOfFirstNonAsciiChar_Vector(char* pBuffer, nu
899899
if (Vector512.IsHardwareAccelerated && bufferLength >= 2 * (uint)Vector512<ushort>.Count)
900900
{
901901
uint SizeOfVector512InChars = (uint)Vector512<ushort>.Count; // JIT will make this a const
902-
uint SizeOfVector512InBytes = (uint)Vector512<byte>.Count; // JIT will make this a const
902+
uint SizeOfVector512InBytes = (uint)Vector512.Size; // JIT will make this a const
903903

904904
Vector512<ushort> asciiMask = Vector512.Create((ushort) 0xFF80);
905905

906-
if (!VectorContainsNonAsciiChar(Unsafe.ReadUnaligned<Vector512<ushort>>(pBuffer)))
906+
if (!VectorContainsNonAsciiChar(Vector512.Load((ushort*)pBuffer)))
907907
{
908908
// The first several elements of the input buffer were ASCII. Bump up the pointer to the
909909
// next aligned boundary, then perform aligned reads from here on out until we find non-ASCII
@@ -923,7 +923,7 @@ private static unsafe nuint GetIndexOfFirstNonAsciiChar_Vector(char* pBuffer, nu
923923
do
924924
{
925925
Debug.Assert((nuint)pBuffer % SizeOfVector512InChars == 0, "Vector read should be aligned.");
926-
if (VectorContainsNonAsciiChar(Unsafe.Read<Vector512<ushort>>(pBuffer)))
926+
if (VectorContainsNonAsciiChar(Vector512.LoadAligned((ushort*)pBuffer)))
927927
{
928928
break; // found non-ASCII data
929929
}
@@ -938,11 +938,11 @@ private static unsafe nuint GetIndexOfFirstNonAsciiChar_Vector(char* pBuffer, nu
938938
else if (Vector256.IsHardwareAccelerated && bufferLength >= 2 * (uint)Vector256<ushort>.Count)
939939
{
940940
uint SizeOfVector256InChars = (uint)Vector256<ushort>.Count; // JIT will make this a const
941-
uint SizeOfVector256InBytes = (uint)Vector256<byte>.Count; // JIT will make this a const
941+
uint SizeOfVector256InBytes = (uint)Vector256.Size; // JIT will make this a const
942942

943943
Vector256<ushort> asciiMask = Vector256.Create((ushort) 0xFF80);
944944

945-
if (!VectorContainsNonAsciiChar(Unsafe.ReadUnaligned<Vector256<ushort>>(pBuffer)))
945+
if (!VectorContainsNonAsciiChar(Vector256.Load((ushort*)pBuffer)))
946946
{
947947
// The first several elements of the input buffer were ASCII. Bump up the pointer to the
948948
// next aligned boundary, then perform aligned reads from here on out until we find non-ASCII
@@ -962,7 +962,7 @@ private static unsafe nuint GetIndexOfFirstNonAsciiChar_Vector(char* pBuffer, nu
962962
do
963963
{
964964
Debug.Assert((nuint)pBuffer % SizeOfVector256InChars == 0, "Vector read should be aligned.");
965-
if (VectorContainsNonAsciiChar(Unsafe.Read<Vector256<ushort>>(pBuffer)))
965+
if (VectorContainsNonAsciiChar(Vector256.LoadAligned((ushort*)pBuffer)))
966966
{
967967
break; // found non-ASCII data
968968
}
@@ -977,11 +977,11 @@ private static unsafe nuint GetIndexOfFirstNonAsciiChar_Vector(char* pBuffer, nu
977977
else if (Vector128.IsHardwareAccelerated && bufferLength >= 2 * (uint)Vector128<ushort>.Count)
978978
{
979979
uint SizeOfVector128InChars = (uint)Vector128<ushort>.Count; // JIT will make this a const
980-
uint SizeOfVector128InBytes = (uint)Vector128<byte>.Count; // JIT will make this a const
980+
uint SizeOfVector128InBytes = (uint)Vector128.Size; // JIT will make this a const
981981

982982
Vector128<ushort> asciiMask = Vector128.Create((ushort) 0xFF80);
983983

984-
if (!VectorContainsNonAsciiChar(Unsafe.ReadUnaligned<Vector128<ushort>>(pBuffer)))
984+
if (!VectorContainsNonAsciiChar(Vector128.Load((ushort*)pBuffer)))
985985
{
986986
// The first several elements of the input buffer were ASCII. Bump up the pointer to the
987987
// next aligned boundary, then perform aligned reads from here on out until we find non-ASCII
@@ -1000,7 +1000,7 @@ private static unsafe nuint GetIndexOfFirstNonAsciiChar_Vector(char* pBuffer, nu
10001000
do
10011001
{
10021002
Debug.Assert((nuint)pBuffer % SizeOfVector128InChars == 0, "Vector read should be aligned.");
1003-
if (VectorContainsNonAsciiChar(Unsafe.Read<Vector128<ushort>>(pBuffer)))
1003+
if (VectorContainsNonAsciiChar(Vector128.LoadAligned((ushort*)pBuffer)))
10041004
{
10051005
break; // found non-ASCII data
10061006
}
@@ -1867,6 +1867,20 @@ private static bool VectorContainsNonAsciiChar(Vector128<byte> asciiVector)
18671867
}
18681868
}
18691869

1870+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
1871+
private static bool VectorContainsNonAsciiChar(Vector256<byte> asciiVector)
1872+
{
1873+
// max ASCII character is 0b_0111_1111, so the most significant bit (0x80) tells whether it contains non ascii
1874+
return asciiVector.ExtractMostSignificantBits() != 0;
1875+
}
1876+
1877+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
1878+
private static bool VectorContainsNonAsciiChar(Vector512<byte> asciiVector)
1879+
{
1880+
// max ASCII character is 0b_0111_1111, so the most significant bit (0x80) tells whether it contains non ascii
1881+
return asciiVector.ExtractMostSignificantBits() != 0;
1882+
}
1883+
18701884
[MethodImpl(MethodImplOptions.AggressiveInlining)]
18711885
private static bool VectorContainsNonAsciiChar(Vector128<ushort> utf16Vector)
18721886
{
@@ -1941,6 +1955,28 @@ private static bool VectorContainsNonAsciiChar<T>(Vector128<T> vector)
19411955
: VectorContainsNonAsciiChar(vector.AsUInt16());
19421956
}
19431957

1958+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
1959+
private static bool VectorContainsNonAsciiChar<T>(Vector256<T> vector)
1960+
where T : unmanaged
1961+
{
1962+
Debug.Assert(typeof(T) == typeof(byte) || typeof(T) == typeof(ushort));
1963+
1964+
return typeof(T) == typeof(byte)
1965+
? VectorContainsNonAsciiChar(vector.AsByte())
1966+
: VectorContainsNonAsciiChar(vector.AsUInt16());
1967+
}
1968+
1969+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
1970+
private static bool VectorContainsNonAsciiChar<T>(Vector512<T> vector)
1971+
where T : unmanaged
1972+
{
1973+
Debug.Assert(typeof(T) == typeof(byte) || typeof(T) == typeof(ushort));
1974+
1975+
return typeof(T) == typeof(byte)
1976+
? VectorContainsNonAsciiChar(vector.AsByte())
1977+
: VectorContainsNonAsciiChar(vector.AsUInt16());
1978+
}
1979+
19441980
[MethodImpl(MethodImplOptions.AggressiveInlining)]
19451981
private static bool AllCharsInVectorAreAscii<T>(Vector128<T> vector)
19461982
where T : unmanaged

0 commit comments

Comments
 (0)