@@ -190,16 +190,10 @@ public unsafe static void AdjustForSkippedBytes(byte* pInputBuffer,// int skippe
190
190
utf16CodeUnitCountAdjustment = TempUtf16CodeUnitCountAdjustment ;
191
191
scalarCountAdjustment = TempScalarCountAdjustment ;
192
192
return pInputBuffer + pos ; } // Too short
193
- // if (pInputBuffer[pos + 3] < 0b10000000) {
194
- // TempUtf16CodeUnitCountAdjustment -= 1;
195
- // } else {
196
- // TempUtf16CodeUnitCountAdjustment -= 2;
197
- // }
198
193
TempUtf16CodeUnitCountAdjustment -= 2 ;
199
194
}
200
195
else if ( ( firstByte & 0b11111000 ) == 0b11110000 )
201
- { // 0b11110000
202
-
196
+ {
203
197
nextPos = pos + 4 ;
204
198
if ( nextPos > inputLength ) {
205
199
utf16CodeUnitCountAdjustment = TempUtf16CodeUnitCountAdjustment ;
@@ -226,9 +220,6 @@ public unsafe static void AdjustForSkippedBytes(byte* pInputBuffer,// int skippe
226
220
return pInputBuffer + pos ; }
227
221
TempUtf16CodeUnitCountAdjustment -= 2 ;
228
222
TempScalarCountAdjustment -= 1 ;
229
-
230
-
231
-
232
223
}
233
224
else
234
225
{
@@ -525,36 +516,9 @@ public unsafe static void AdjustForSkippedBytes(byte* pInputBuffer,// int skippe
525
516
Vector256 < byte > thirdByte = Vector256 . Create ( ( byte ) ( 0b11100000u - 0x80 ) ) ;
526
517
Vector256 < byte > fourthByte = Vector256 . Create ( ( byte ) ( 0b11110000u - 0x80 ) ) ;
527
518
528
- // // Mask for the lower and upper parts of the vector
529
- // Vector128<byte> lowerMask = Vector128.Create(
530
- // 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
531
- // 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF).AsByte();
532
-
533
- // Vector128<byte> upperMask = Vector128.Create(
534
- // 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
535
- // 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00).AsByte();
536
-
537
- // // Combine lower and upper masks into a Vector256<byte>
538
- // Vector256<byte> mask = Vector256.Create(lowerMask, upperMask);
539
-
540
- // // Apply the mask to zero out the last 3 bytes of each vector
541
- // Vector256<byte> secondByteMasked = Avx2.And(secondByte, mask);
542
- // Vector256<byte> thirdByteMasked = Avx2.And(thirdByte, mask);
543
- // Vector256<byte> fourthByteMasked = Avx2.And(fourthByte, mask);
544
-
545
-
546
519
Vector256 < byte > v0f = Vector256 . Create ( ( byte ) 0x0F ) ;
547
520
Vector256 < byte > v80 = Vector256 . Create ( ( byte ) 0x80 ) ;
548
521
549
- // Vector to identify bytes right before the start of a 4-byte sequence in UTF-8.
550
- // Vector256<byte> beforeFourByteMarker = Vector256.Create((byte)(0xF0 - 1));
551
- // // Vector to identify bytes right before the start of a 3-byte sequence in UTF-8.
552
- // Vector256<byte> beforeThreeByteMarker = Vector256.Create((byte)(0xE0 - 1));
553
- // // Vector to identify bytes right before the start of a 2-byte sequence in UTF-8.
554
- // Vector256<byte> beforeTwoByteMarker = Vector256.Create((byte)(0xC0 - 1));
555
-
556
-
557
-
558
522
for ( ; processedLength + 32 <= inputLength ; processedLength += 32 )
559
523
{
560
524
Vector256 < byte > currentBlock = Avx . LoadVector256 ( pInputBuffer + processedLength ) ;
@@ -601,34 +565,26 @@ public unsafe static void AdjustForSkippedBytes(byte* pInputBuffer,// int skippe
601
565
}
602
566
}
603
567
}
604
- // else{ off = processedLength;}
605
568
606
- // return SimdUnicode.UTF8.RewindAndValidateWithErrors(off, pInputBuffer + off, inputLength - off);
607
569
return SimdUnicode . UTF8 . RewindAndValidateWithErrors ( off , pInputBuffer + off , inputLength - off , ref utf16CodeUnitCountAdjustment , ref scalarCountAdjustment ) ;
608
570
}
609
571
prevIncomplete = Vector256 < byte > . Zero ;
610
572
}
611
573
else // Contains non-ASCII characters, we need to do non-trivial processing
612
574
{
613
- // Use SubtractSaturate to effectively compare if bytes in block are greater than markers.
575
+ // Use SubtractSaturate to effectively compare if bytes in block are greater than markers.
614
576
615
- // Detect start of 4-byte sequences.
616
- Vector256 < byte > isStartOf4ByteSequence = Avx2 . SubtractSaturate ( currentBlock , fourthByte ) ;
617
- uint fourByteCount = Popcnt . PopCount ( ( uint ) Avx2 . MoveMask ( isStartOf4ByteSequence ) ) ;
618
-
619
- // Detect start of 3-byte sequences (including those that start 4-byte sequences).
620
- Vector256 < byte > isStartOf3OrMoreByteSequence = Avx2 . SubtractSaturate ( currentBlock , thirdByte ) ;
621
- uint threeBytePlusCount = Popcnt . PopCount ( ( uint ) Avx2 . MoveMask ( isStartOf3OrMoreByteSequence ) ) ;
622
-
623
- // Detect start of 2-byte sequences (including those that start 3-byte and 4-byte sequences).
624
- Vector256 < byte > isStartOf2OrMoreByteSequence = Avx2 . SubtractSaturate ( currentBlock , secondByte ) ;
625
- uint twoBytePlusCount = Popcnt . PopCount ( ( uint ) Avx2 . MoveMask ( isStartOf2OrMoreByteSequence ) ) ;
626
-
627
- // Calculate counts by isolating each type.
628
- uint threeByteCount = threeBytePlusCount - fourByteCount ; // Isolate 3-byte starts by subtracting 4-byte starts.
629
- uint twoByteCount = twoBytePlusCount - threeBytePlusCount ; // Isolate 2-byte starts by subtracting 3-byte and 4-byte starts.
577
+ Vector256 < byte > isStartOf4ByteSequence = Avx2 . SubtractSaturate ( currentBlock , fourthByte ) ;
578
+ Vector256 < byte > isStartOf3OrMoreByteSequence = Avx2 . SubtractSaturate ( currentBlock , thirdByte ) ;
579
+ Vector256 < byte > isStartOf2OrMoreByteSequence = Avx2 . SubtractSaturate ( currentBlock , secondByte ) ;
630
580
581
+ uint twoBytePlusCount = Popcnt . PopCount ( ( uint ) Avx2 . MoveMask ( isStartOf2OrMoreByteSequence ) ) ;
582
+ uint threeBytePlusCount = Popcnt . PopCount ( ( uint ) Avx2 . MoveMask ( isStartOf3OrMoreByteSequence ) ) ;
583
+ uint fourByteCount = Popcnt . PopCount ( ( uint ) Avx2 . MoveMask ( isStartOf4ByteSequence ) ) ;
631
584
585
+ // Calculate counts by isolating each type.
586
+ uint threeByteCount = threeBytePlusCount - fourByteCount ; // Isolate 3-byte starts by subtracting 4-byte starts.
587
+ uint twoByteCount = twoBytePlusCount - threeBytePlusCount ; // Isolate 2-byte starts by subtracting 3-byte and 4-byte starts.
632
588
633
589
Vector256 < byte > shuffled = Avx2 . Permute2x128 ( prevInputBlock , currentBlock , 0x21 ) ;
634
590
prevInputBlock = currentBlock ;
@@ -652,67 +608,24 @@ public unsafe static void AdjustForSkippedBytes(byte* pInputBuffer,// int skippe
652
608
TailScalarCodeUnitCountAdjustment = 0 ;
653
609
TailUtf16CodeUnitCountAdjustment = 0 ;
654
610
655
-
656
- int off = processedLength >= 32 ? processedLength : processedLength ;
657
-
658
- // Console.WriteLine("This is off :" + off);
659
- // return SimdUnicode.UTF8.RewindAndValidateWithErrors(off, pInputBuffer + off, inputLength - off);
660
- // byte* invalidBytePointer = SimdUnicode.UTF8.RewindAndValidateWithErrors(off, pInputBuffer + off, inputLength - off, ref utf16CodeUnitCountAdjustment,ref scalarCountAdjustment);
661
- byte * invalidBytePointer = SimdUnicode . UTF8 . RewindAndValidateWithErrors ( off , pInputBuffer + off , inputLength - processedLength , ref TailUtf16CodeUnitCountAdjustment , ref TailScalarCodeUnitCountAdjustment ) ;
662
-
663
- // byte* invalidBytePointer = SimdUnicode.UTF8.GetPointerToFirstInvalidByteScalar(pInputBuffer,processedLength,out TailUtf16CodeUnitCountAdjustment,out TailScalarCodeUnitCountAdjustment);
664
- // Adjustments not to double count
665
- // TempUtf16CodeUnitCountAdjustment += (int)fourByteCount * 2;
666
- // TempUtf16CodeUnitCountAdjustment += (int)twoByteCount;
667
- // TempUtf16CodeUnitCountAdjustment += (int)threeByteCount *2;
668
- // TempScalarCountAdjustment += (int)fourByteCount;
611
+ int off = processedLength >= 32 ? processedLength : processedLength ;
612
+ byte * invalidBytePointer = SimdUnicode . UTF8 . RewindAndValidateWithErrors ( off , pInputBuffer + off , inputLength - off , ref TailUtf16CodeUnitCountAdjustment , ref TailScalarCodeUnitCountAdjustment ) ;
669
613
670
614
utf16CodeUnitCountAdjustment = TempUtf16CodeUnitCountAdjustment + TailUtf16CodeUnitCountAdjustment ;
671
615
scalarCountAdjustment = TempScalarCountAdjustment + TailScalarCodeUnitCountAdjustment ;
672
616
673
-
674
-
675
617
return invalidBytePointer ;
676
618
677
619
}
678
- // Adjustments
679
- TempUtf16CodeUnitCountAdjustment -= ( int ) fourByteCount * 2 ;
680
- TempUtf16CodeUnitCountAdjustment -= ( int ) twoByteCount ;
681
- TempUtf16CodeUnitCountAdjustment -= ( int ) threeByteCount * 2 ;
682
- TempScalarCountAdjustment -= ( int ) fourByteCount ;
620
+ // Adjustments
621
+ TempUtf16CodeUnitCountAdjustment -= ( int ) fourByteCount * 2 ;
622
+ TempUtf16CodeUnitCountAdjustment -= ( int ) twoByteCount ;
623
+ TempUtf16CodeUnitCountAdjustment -= ( int ) threeByteCount * 2 ;
624
+ TempScalarCountAdjustment -= ( int ) fourByteCount ;
683
625
684
626
prevIncomplete = Avx2 . SubtractSaturate ( currentBlock , maxValue ) ;
685
627
}
686
628
}
687
-
688
- if ( ! Avx2 . TestZ ( prevIncomplete , prevIncomplete ) )
689
- {
690
-
691
- // Console.WriteLine("----Checkpoint 2:SIMD rewind");
692
- // We have an unterminated sequence.
693
- processedLength -= 3 ;
694
- for ( int k = 0 ; k < 3 ; k ++ )
695
- {
696
-
697
- int candidateByte = pInputBuffer [ processedLength + k ] ;
698
- if ( ( candidateByte & 0b11000000 ) == 0b11000000 )
699
- {
700
- if ( ( candidateByte & 0b11100000 ) == 0b11000000 ) // Start of a 2-byte sequence
701
- {
702
- TempUtf16CodeUnitCountAdjustment += 1 ;
703
- }
704
- if ( ( candidateByte & 0b11110000 ) == 0b11100000 ) // Start of a 3-byte sequence
705
- {
706
- TempUtf16CodeUnitCountAdjustment += 2 ;
707
- }
708
- if ( ( candidateByte & 0b11111000 ) == 0b11110000 ) // Start of a 4-byte sequence
709
- {
710
- TempUtf16CodeUnitCountAdjustment += 2 ;
711
- TempScalarCountAdjustment += 1 ;
712
- }
713
- }
714
- }
715
- }
716
629
}
717
630
}
718
631
@@ -750,8 +663,6 @@ public unsafe static void AdjustForSkippedBytes(byte* pInputBuffer,// int skippe
750
663
TempUtf16CodeUnitCountAdjustment += 2 ;
751
664
TempScalarCountAdjustment += 1 ;
752
665
}
753
-
754
- // processedLength += k;
755
666
break ;
756
667
}
757
668
}
0 commit comments