@@ -130,9 +130,9 @@ private static unsafe nuint GetIndexOfFirstNonAsciiByte_Vector(byte* pBuffer, nu
130
130
131
131
if ( Vector512 . IsHardwareAccelerated && bufferLength >= 2 * ( uint ) Vector512 < byte > . Count )
132
132
{
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
134
134
135
- if ( Unsafe . ReadUnaligned < Vector512 < byte > > ( pBuffer ) . ExtractMostSignificantBits ( ) == 0 )
135
+ if ( ! VectorContainsNonAsciiChar ( Vector512 . Load ( pBuffer ) ) )
136
136
{
137
137
// The first several elements of the input buffer were ASCII. Bump up the pointer to the
138
138
// 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
152
152
do
153
153
{
154
154
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 ) ) )
156
156
{
157
157
break ; // found non-ASCII data
158
158
}
@@ -168,9 +168,9 @@ private static unsafe nuint GetIndexOfFirstNonAsciiByte_Vector(byte* pBuffer, nu
168
168
}
169
169
else if ( Vector256 . IsHardwareAccelerated && bufferLength >= 2 * ( uint ) Vector256 < byte > . Count )
170
170
{
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
172
172
173
- if ( Unsafe . ReadUnaligned < Vector256 < byte > > ( pBuffer ) . ExtractMostSignificantBits ( ) == 0 )
173
+ if ( ! VectorContainsNonAsciiChar ( Vector256 . Load ( pBuffer ) ) )
174
174
{
175
175
// The first several elements of the input buffer were ASCII. Bump up the pointer to the
176
176
// 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
190
190
do
191
191
{
192
192
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 ) ) )
194
194
{
195
195
break ; // found non-ASCII data
196
196
}
@@ -206,9 +206,9 @@ private static unsafe nuint GetIndexOfFirstNonAsciiByte_Vector(byte* pBuffer, nu
206
206
}
207
207
else if ( Vector128 . IsHardwareAccelerated && bufferLength >= 2 * ( uint ) Vector128 < byte > . Count )
208
208
{
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
210
210
211
- if ( Unsafe . ReadUnaligned < Vector128 < byte > > ( pBuffer ) . ExtractMostSignificantBits ( ) == 0 )
211
+ if ( ! VectorContainsNonAsciiChar ( Vector128 . Load ( pBuffer ) ) )
212
212
{
213
213
// The first several elements of the input buffer were ASCII. Bump up the pointer to the
214
214
// 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
228
228
do
229
229
{
230
230
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 ) ) )
232
232
{
233
233
break ; // found non-ASCII data
234
234
}
@@ -899,11 +899,11 @@ private static unsafe nuint GetIndexOfFirstNonAsciiChar_Vector(char* pBuffer, nu
899
899
if ( Vector512 . IsHardwareAccelerated && bufferLength >= 2 * ( uint ) Vector512 < ushort > . Count )
900
900
{
901
901
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
903
903
904
904
Vector512 < ushort > asciiMask = Vector512 . Create ( ( ushort ) 0xFF80 ) ;
905
905
906
- if ( ! VectorContainsNonAsciiChar ( Unsafe . ReadUnaligned < Vector512 < ushort > > ( pBuffer ) ) )
906
+ if ( ! VectorContainsNonAsciiChar ( Vector512 . Load ( ( ushort * ) pBuffer ) ) )
907
907
{
908
908
// The first several elements of the input buffer were ASCII. Bump up the pointer to the
909
909
// 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
923
923
do
924
924
{
925
925
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 ) ) )
927
927
{
928
928
break ; // found non-ASCII data
929
929
}
@@ -938,11 +938,11 @@ private static unsafe nuint GetIndexOfFirstNonAsciiChar_Vector(char* pBuffer, nu
938
938
else if ( Vector256 . IsHardwareAccelerated && bufferLength >= 2 * ( uint ) Vector256 < ushort > . Count )
939
939
{
940
940
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
942
942
943
943
Vector256 < ushort > asciiMask = Vector256 . Create ( ( ushort ) 0xFF80 ) ;
944
944
945
- if ( ! VectorContainsNonAsciiChar ( Unsafe . ReadUnaligned < Vector256 < ushort > > ( pBuffer ) ) )
945
+ if ( ! VectorContainsNonAsciiChar ( Vector256 . Load ( ( ushort * ) pBuffer ) ) )
946
946
{
947
947
// The first several elements of the input buffer were ASCII. Bump up the pointer to the
948
948
// 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
962
962
do
963
963
{
964
964
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 ) ) )
966
966
{
967
967
break ; // found non-ASCII data
968
968
}
@@ -977,11 +977,11 @@ private static unsafe nuint GetIndexOfFirstNonAsciiChar_Vector(char* pBuffer, nu
977
977
else if ( Vector128 . IsHardwareAccelerated && bufferLength >= 2 * ( uint ) Vector128 < ushort > . Count )
978
978
{
979
979
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
981
981
982
982
Vector128 < ushort > asciiMask = Vector128 . Create ( ( ushort ) 0xFF80 ) ;
983
983
984
- if ( ! VectorContainsNonAsciiChar ( Unsafe . ReadUnaligned < Vector128 < ushort > > ( pBuffer ) ) )
984
+ if ( ! VectorContainsNonAsciiChar ( Vector128 . Load ( ( ushort * ) pBuffer ) ) )
985
985
{
986
986
// The first several elements of the input buffer were ASCII. Bump up the pointer to the
987
987
// 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
1000
1000
do
1001
1001
{
1002
1002
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 ) ) )
1004
1004
{
1005
1005
break ; // found non-ASCII data
1006
1006
}
@@ -1867,6 +1867,20 @@ private static bool VectorContainsNonAsciiChar(Vector128<byte> asciiVector)
1867
1867
}
1868
1868
}
1869
1869
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
+
1870
1884
[ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
1871
1885
private static bool VectorContainsNonAsciiChar( Vector128 < ushort > utf16Vector )
1872
1886
{
@@ -1941,6 +1955,28 @@ private static bool VectorContainsNonAsciiChar<T>(Vector128<T> vector)
1941
1955
: VectorContainsNonAsciiChar( vector. AsUInt16( ) ) ;
1942
1956
}
1943
1957
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
+
1944
1980
[ MethodImpl( MethodImplOptions. AggressiveInlining) ]
1945
1981
private static bool AllCharsInVectorAreAscii< T> ( Vector128< T> vector)
1946
1982
where T : unmanaged
0 commit comments