@@ -881,7 +881,12 @@ private unsafe static (int utfadjust, int scalaradjust) calculateErrorPathadjust
881
881
}
882
882
return GetPointerToFirstInvalidByteScalar ( pInputBuffer + processedLength , inputLength - processedLength , out utf16CodeUnitCountAdjustment , out scalarCountAdjustment ) ;
883
883
}
884
-
884
+ public static void ToString ( Vector128 < byte > v )
885
+ {
886
+ Span < byte > b = stackalloc byte [ 16 ] ;
887
+ v . CopyTo ( b ) ;
888
+ Console . WriteLine ( Convert . ToHexString ( b ) ) ;
889
+ }
885
890
public unsafe static byte * GetPointerToFirstInvalidByteArm64 ( byte * pInputBuffer , int inputLength , out int utf16CodeUnitCountAdjustment , out int scalarCountAdjustment )
886
891
{
887
892
int processedLength = 0 ;
@@ -974,6 +979,7 @@ private unsafe static (int utfadjust, int scalaradjust) calculateErrorPathadjust
974
979
// we need to check if the previous block was incomplete.
975
980
if ( AdvSimd . Arm64 . MaxAcross ( prevIncomplete ) . ToScalar ( ) != 0 )
976
981
{
982
+ // Console.WriteLine("ASCII block, but previous block was incomplete");
977
983
int off = processedLength >= 3 ? processedLength - 3 : processedLength ;
978
984
byte * invalidBytePointer = SimdUnicode . UTF8 . SimpleRewindAndValidateWithErrors ( 16 - 3 , pInputBuffer + processedLength - 3 , inputLength - processedLength + 3 ) ;
979
985
// So the code is correct up to invalidBytePointer
@@ -1013,6 +1019,19 @@ private unsafe static (int utfadjust, int scalaradjust) calculateErrorPathadjust
1013
1019
// hardware:
1014
1020
if ( AdvSimd . Arm64 . MaxAcross ( Vector128 . AsUInt32 ( error ) ) . ToScalar ( ) != 0 )
1015
1021
{
1022
+ // Console.WriteLine("Error block detected");
1023
+ int slown4 = 0 ;
1024
+ int slowcontbytes = 0 ;
1025
+ int slowasciibytes = 0 ;
1026
+ addCounters ( pInputBuffer , pInputBuffer + processedLength , ref slowasciibytes , ref slown4 , ref slowcontbytes ) ;
1027
+ if ( slowasciibytes != asciibytes || slown4 != n4 || slowcontbytes != contbytes )
1028
+ {
1029
+ Console . WriteLine ( "Error in counting" ) ;
1030
+ Console . WriteLine ( $ "asciibytes: { asciibytes } { slowasciibytes } ") ;
1031
+ Console . WriteLine ( $ "n4: { n4 } { slown4 } ") ;
1032
+ Console . WriteLine ( $ "contbytes: { contbytes } { slowcontbytes } ") ;
1033
+ }
1034
+
1016
1035
byte * invalidBytePointer ;
1017
1036
if ( processedLength == 0 )
1018
1037
{
@@ -1038,13 +1057,38 @@ private unsafe static (int utfadjust, int scalaradjust) calculateErrorPathadjust
1038
1057
Vector128 < sbyte > largestcont = Vector128 . Create ( ( sbyte ) - 65 ) ; // -65 => 0b10111111
1039
1058
contbytes += - AdvSimd . Arm64 . AddAcross ( AdvSimd . CompareLessThanOrEqual ( Vector128 . AsSByte ( currentBlock ) , largestcont ) ) . ToScalar ( ) ;
1040
1059
Vector128 < byte > fourthByteMinusOne = Vector128 . Create ( ( byte ) ( 0b11110000u - 1 ) ) ;
1041
- n4 += ( int ) ( AdvSimd . Arm64 . AddAcross ( AdvSimd . SubtractSaturate ( currentBlock , fourthByteMinusOne ) ) . ToScalar ( ) ) ;
1060
+
1061
+ int reallyslown4 = 0 ;
1062
+ for ( int i = 0 ; i < 16 ; i ++ )
1063
+ {
1064
+ if ( pInputBuffer [ processedLength + i ] >= 0xF0 )
1065
+ {
1066
+ reallyslown4 ++ ;
1067
+ }
1068
+ }
1069
+ var largerthan0f = AdvSimd . CompareGreaterThan ( currentBlock , fourthByteMinusOne ) ;
1070
+ var largerthan0fones = AdvSimd . And ( largerthan0f , Vector128 . Create ( ( byte ) 1 ) ) ;
1071
+ var largerthan0fonescount = AdvSimd . Arm64 . AddAcross ( largerthan0fones ) . ToScalar ( ) ;
1072
+ if ( largerthan0fonescount != reallyslown4 )
1073
+ {
1074
+ Console . WriteLine ( "***********Error in counting 4-byte sequences" ) ;
1075
+ ToString ( currentBlock ) ;
1076
+ ToString ( fourthByteMinusOne ) ;
1077
+ ToString ( AdvSimd . SubtractSaturate ( currentBlock , fourthByteMinusOne ) ) ;
1078
+ ToString ( AdvSimd . CompareGreaterThan ( currentBlock , fourthByteMinusOne ) ) ;
1079
+ Console . WriteLine ( ( ( AdvSimd . Arm64 . AddAcross ( AdvSimd . CompareGreaterThan ( currentBlock , fourthByteMinusOne ) ) . ToScalar ( ) ^ 0xff ) + 1 ) & 0xff ) ;
1080
+
1081
+ Console . WriteLine ( reallyslown4 ) ;
1082
+ }
1083
+ n4 += largerthan0fonescount ;
1084
+
1085
+ //n4 += (int)(((AdvSimd.Arm64.AddAcross(AdvSimd.CompareGreaterThan(currentBlock, fourthByteMinusOne)).ToScalar()^0xff)+1)&0xff);
1042
1086
}
1043
1087
asciibytes -= ( sbyte ) AdvSimd . Arm64 . AddAcross ( AdvSimd . CompareLessThan ( currentBlock , v80 ) ) . ToScalar ( ) ;
1044
1088
}
1045
1089
1046
1090
// We may still have an error.
1047
- if ( processedLength < inputLength || ! Avx2 . TestZ ( prevIncomplete , prevIncomplete ) )
1091
+ if ( processedLength < inputLength || AdvSimd . Arm64 . MaxAcross ( prevIncomplete ) . ToScalar ( ) != 0 )
1048
1092
{
1049
1093
byte * invalidBytePointer ;
1050
1094
if ( processedLength == 0 )
@@ -1054,10 +1098,11 @@ private unsafe static (int utfadjust, int scalaradjust) calculateErrorPathadjust
1054
1098
else
1055
1099
{
1056
1100
invalidBytePointer = SimdUnicode . UTF8 . SimpleRewindAndValidateWithErrors ( processedLength - 3 , pInputBuffer + processedLength - 3 , inputLength - processedLength + 3 ) ;
1057
-
1058
1101
}
1059
1102
if ( invalidBytePointer != pInputBuffer + inputLength )
1060
1103
{
1104
+ // Console.WriteLine("trailing Error block detected");
1105
+
1061
1106
if ( invalidBytePointer < pInputBuffer + processedLength )
1062
1107
{
1063
1108
removeCounters ( invalidBytePointer , pInputBuffer + processedLength , ref asciibytes , ref n4 , ref contbytes ) ;
@@ -1075,11 +1120,15 @@ private unsafe static (int utfadjust, int scalaradjust) calculateErrorPathadjust
1075
1120
addCounters ( pInputBuffer + processedLength , invalidBytePointer , ref asciibytes , ref n4 , ref contbytes ) ;
1076
1121
}
1077
1122
}
1123
+ //Console.WriteLine("trailing simd method");
1124
+
1078
1125
int final_total_bytes_processed = inputLength - start_point ;
1079
1126
( utf16CodeUnitCountAdjustment , scalarCountAdjustment ) = CalculateN2N3FinalSIMDAdjustments ( asciibytes , n4 , contbytes , final_total_bytes_processed ) ;
1080
1127
return pInputBuffer + inputLength ;
1081
1128
}
1082
1129
}
1130
+ //Console.WriteLine("trailing scalar method");
1131
+
1083
1132
return GetPointerToFirstInvalidByteScalar ( pInputBuffer + processedLength , inputLength - processedLength , out utf16CodeUnitCountAdjustment , out scalarCountAdjustment ) ;
1084
1133
}
1085
1134
0 commit comments