-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathUTF8.cs
1573 lines (1477 loc) · 91 KB
/
UTF8.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Runtime.Intrinsics;
using System.Runtime.Intrinsics.X86;
using System.Runtime.Intrinsics.Arm;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
namespace SimdUnicode
{
public static class UTF8
{
// Returns &inputBuffer[inputLength] if the input buffer is valid.
/// <summary>
/// Given an input buffer <paramref name="pInputBuffer"/> of byte length <paramref name="inputLength"/>,
/// returns a pointer to where the first invalid data appears in <paramref name="pInputBuffer"/>.
/// The parameter <paramref name="Utf16CodeUnitCountAdjustment"/> is set according to the content of the valid UTF-8 characters encountered, counting -1 for each 2-byte character, -2 for each 3-byte and 4-byte characters.
/// The parameter <paramref name="ScalarCodeUnitCountAdjustment"/> is set according to the content of the valid UTF-8 characters encountered, counting -1 for each 4-byte character.
/// </summary>
/// <remarks>
/// Returns a pointer to the end of <paramref name="pInputBuffer"/> if the buffer is well-formed.
/// </remarks>
public unsafe static byte* GetPointerToFirstInvalidByte(byte* pInputBuffer, int inputLength, out int Utf16CodeUnitCountAdjustment, out int ScalarCodeUnitCountAdjustment)
{
if (AdvSimd.Arm64.IsSupported && BitConverter.IsLittleEndian)
{
return GetPointerToFirstInvalidByteArm64(pInputBuffer, inputLength, out Utf16CodeUnitCountAdjustment, out ScalarCodeUnitCountAdjustment);
}
if (Vector512.IsHardwareAccelerated && Avx512Vbmi.IsSupported)
{
return GetPointerToFirstInvalidByteAvx512(pInputBuffer, inputLength, out Utf16CodeUnitCountAdjustment, out ScalarCodeUnitCountAdjustment);
}
if (Avx2.IsSupported)
{
return GetPointerToFirstInvalidByteAvx2(pInputBuffer, inputLength, out Utf16CodeUnitCountAdjustment, out ScalarCodeUnitCountAdjustment);
}
if (Ssse3.IsSupported)
{
return GetPointerToFirstInvalidByteSse(pInputBuffer, inputLength, out Utf16CodeUnitCountAdjustment, out ScalarCodeUnitCountAdjustment);
}
return GetPointerToFirstInvalidByteScalar(pInputBuffer, inputLength, out Utf16CodeUnitCountAdjustment, out ScalarCodeUnitCountAdjustment);
}
// prevents double counting in case there is a toolong error on the edge
private static (int utfAdjust, int scalarAdjust) GetFinalScalarUtfAdjustments(byte headerByte)
{
// Check if the header byte belongs to a 2-byte UTF-8 character
if ((headerByte & 0b11100000) == 0b11000000)
{
return (1, 0);
}
// Check if the header byte belongs to a 3-byte UTF-8 character
else if ((headerByte & 0b11110000) == 0b11100000)
{
return (2, 0);
}
// Check if the header byte belongs to a 4-byte UTF-8 character
else if ((headerByte & 0b11111000) == 0b11110000)
{
return (2, 1);
}
// Otherwise, it's a 1-byte character or continuation byte
return (0, 0);
}
// We scan the input from buf to len, possibly going back howFarBack bytes, to find the end of
// a valid UTF-8 sequence. We return buf + len if the buffer is valid, otherwise we return the
// pointer to the first invalid byte.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private unsafe static byte* SimpleRewindAndValidateWithErrors(int howFarBack, byte* buf, int len)
{
int extraLen = 0;
bool foundLeadingBytes = false;
for (int i = 0; i <= howFarBack; i++)
{
byte candidateByte = buf[0 - i];
foundLeadingBytes = (candidateByte & 0b11000000) != 0b10000000;
if (foundLeadingBytes)
{
buf -= i;
extraLen = i;
break;
}
}
if (!foundLeadingBytes)
{
return buf - howFarBack;
}
int pos = 0;
int nextPos;
uint codePoint = 0;
len += extraLen;
while (pos < len)
{
byte firstByte = buf[pos];
while (firstByte < 0b10000000)
{
if (++pos == len)
{
return buf + len;
}
firstByte = buf[pos];
}
if ((firstByte & 0b11100000) == 0b11000000)
{
nextPos = pos + 2;
if (nextPos > len)
{
return buf + pos;
} // Too short
if ((buf[pos + 1] & 0b11000000) != 0b10000000)
{
return buf + pos;
} // Too short
// range check
codePoint = (uint)(firstByte & 0b00011111) << 6 | (uint)(buf[pos + 1] & 0b00111111);
if ((codePoint < 0x80) || (0x7ff < codePoint))
{
return buf + pos;
} // Overlong
}
else if ((firstByte & 0b11110000) == 0b11100000)
{
nextPos = pos + 3;
if (nextPos > len)
{
return buf + pos;
} // Too short
// range check
codePoint = (uint)(firstByte & 0b00001111) << 12 |
(uint)(buf[pos + 1] & 0b00111111) << 6 |
(uint)(buf[pos + 2] & 0b00111111);
// Either overlong or too large:
if ((codePoint < 0x800) || (0xffff < codePoint) ||
(0xd7ff < codePoint && codePoint < 0xe000))
{
return buf + pos;
}
if ((buf[pos + 1] & 0b11000000) != 0b10000000)
{
return buf + pos;
} // Too short
if ((buf[pos + 2] & 0b11000000) != 0b10000000)
{
return buf + pos;
} // Too short
}
else if ((firstByte & 0b11111000) == 0b11110000)
{
nextPos = pos + 4;
if (nextPos > len)
{
return buf + pos;
}
if ((buf[pos + 1] & 0b11000000) != 0b10000000)
{
return buf + pos;
}
if ((buf[pos + 2] & 0b11000000) != 0b10000000)
{
return buf + pos;
}
if ((buf[pos + 3] & 0b11000000) != 0b10000000)
{
return buf + pos;
}
// range check
codePoint =
(uint)(firstByte & 0b00000111) << 18 | (uint)(buf[pos + 1] & 0b00111111) << 12 |
(uint)(buf[pos + 2] & 0b00111111) << 6 | (uint)(buf[pos + 3] & 0b00111111);
if (codePoint <= 0xffff || 0x10ffff < codePoint)
{
return buf + pos;
}
}
else
{
// we may have a continuation/too long error
return buf + pos;
}
pos = nextPos;
}
return buf + len; // no error
}
public unsafe static byte* GetPointerToFirstInvalidByteScalar(byte* pInputBuffer, int inputLength, out int utf16CodeUnitCountAdjustment, out int scalarCountAdjustment)
{
int TempUtf16CodeUnitCountAdjustment = 0;
int TempScalarCountAdjustment = 0;
int pos = 0;
int nextPos;
uint codePoint = 0;
while (pos < inputLength)
{
byte firstByte = pInputBuffer[pos];
while (firstByte < 0b10000000)
{
if (++pos == inputLength)
{
utf16CodeUnitCountAdjustment = TempUtf16CodeUnitCountAdjustment;
scalarCountAdjustment = TempScalarCountAdjustment;
return pInputBuffer + inputLength;
}
firstByte = pInputBuffer[pos];
}
if ((firstByte & 0b11100000) == 0b11000000)
{
nextPos = pos + 2;
if (nextPos > inputLength)
{
utf16CodeUnitCountAdjustment = TempUtf16CodeUnitCountAdjustment;
scalarCountAdjustment = TempScalarCountAdjustment;
return pInputBuffer + pos;
} // Too short
if ((pInputBuffer[pos + 1] & 0b11000000) != 0b10000000)
{
utf16CodeUnitCountAdjustment = TempUtf16CodeUnitCountAdjustment;
scalarCountAdjustment = TempScalarCountAdjustment;
return pInputBuffer + pos;
} // Too short
// range check
codePoint = (uint)(firstByte & 0b00011111) << 6 | (uint)(pInputBuffer[pos + 1] & 0b00111111);
if ((codePoint < 0x80) || (0x7ff < codePoint))
{
utf16CodeUnitCountAdjustment = TempUtf16CodeUnitCountAdjustment;
scalarCountAdjustment = TempScalarCountAdjustment;
return pInputBuffer + pos;
} // Overlong
TempUtf16CodeUnitCountAdjustment -= 1;
}
else if ((firstByte & 0b11110000) == 0b11100000)
{
nextPos = pos + 3;
if (nextPos > inputLength)
{
utf16CodeUnitCountAdjustment = TempUtf16CodeUnitCountAdjustment;
scalarCountAdjustment = TempScalarCountAdjustment;
return pInputBuffer + pos;
} // Too short
// range check
codePoint = (uint)(firstByte & 0b00001111) << 12 |
(uint)(pInputBuffer[pos + 1] & 0b00111111) << 6 |
(uint)(pInputBuffer[pos + 2] & 0b00111111);
// Either overlong or too large:
if ((codePoint < 0x800) || (0xffff < codePoint) ||
(0xd7ff < codePoint && codePoint < 0xe000))
{
utf16CodeUnitCountAdjustment = TempUtf16CodeUnitCountAdjustment;
scalarCountAdjustment = TempScalarCountAdjustment;
return pInputBuffer + pos;
}
if ((pInputBuffer[pos + 1] & 0b11000000) != 0b10000000)
{
utf16CodeUnitCountAdjustment = TempUtf16CodeUnitCountAdjustment;
scalarCountAdjustment = TempScalarCountAdjustment;
return pInputBuffer + pos;
} // Too short
if ((pInputBuffer[pos + 2] & 0b11000000) != 0b10000000)
{
utf16CodeUnitCountAdjustment = TempUtf16CodeUnitCountAdjustment;
scalarCountAdjustment = TempScalarCountAdjustment;
return pInputBuffer + pos;
} // Too short
TempUtf16CodeUnitCountAdjustment -= 2;
}
else if ((firstByte & 0b11111000) == 0b11110000)
{
nextPos = pos + 4;
if (nextPos > inputLength)
{
utf16CodeUnitCountAdjustment = TempUtf16CodeUnitCountAdjustment;
scalarCountAdjustment = TempScalarCountAdjustment; return pInputBuffer + pos;
}
if ((pInputBuffer[pos + 1] & 0b11000000) != 0b10000000)
{
utf16CodeUnitCountAdjustment = TempUtf16CodeUnitCountAdjustment;
scalarCountAdjustment = TempScalarCountAdjustment;
return pInputBuffer + pos;
}
if ((pInputBuffer[pos + 2] & 0b11000000) != 0b10000000)
{
utf16CodeUnitCountAdjustment = TempUtf16CodeUnitCountAdjustment;
scalarCountAdjustment = TempScalarCountAdjustment;
return pInputBuffer + pos;
}
if ((pInputBuffer[pos + 3] & 0b11000000) != 0b10000000)
{
utf16CodeUnitCountAdjustment = TempUtf16CodeUnitCountAdjustment;
scalarCountAdjustment = TempScalarCountAdjustment;
return pInputBuffer + pos;
}
// range check
codePoint =
(uint)(firstByte & 0b00000111) << 18 | (uint)(pInputBuffer[pos + 1] & 0b00111111) << 12 |
(uint)(pInputBuffer[pos + 2] & 0b00111111) << 6 | (uint)(pInputBuffer[pos + 3] & 0b00111111);
if (codePoint <= 0xffff || 0x10ffff < codePoint)
{
utf16CodeUnitCountAdjustment = TempUtf16CodeUnitCountAdjustment;
scalarCountAdjustment = TempScalarCountAdjustment;
return pInputBuffer + pos;
}
TempUtf16CodeUnitCountAdjustment -= 2;
TempScalarCountAdjustment -= 1;
}
else
{
// we may have a continuation/too long error
utf16CodeUnitCountAdjustment = TempUtf16CodeUnitCountAdjustment;
scalarCountAdjustment = TempScalarCountAdjustment;
return pInputBuffer + pos;
}
pos = nextPos;
}
utf16CodeUnitCountAdjustment = TempUtf16CodeUnitCountAdjustment;
scalarCountAdjustment = TempScalarCountAdjustment;
return pInputBuffer + inputLength;
}
const byte TOO_SHORT = 1 << 0;
const byte TOO_LONG = 1 << 1;
const byte OVERLONG_3 = 1 << 2;
const byte SURROGATE = 1 << 4;
const byte OVERLONG_2 = 1 << 5;
const byte TWO_CONTS = 1 << 7;
const byte TOO_LARGE = 1 << 3;
const byte TOO_LARGE_1000 = 1 << 6;
const byte OVERLONG_4 = 1 << 6;
const byte CARRY = TOO_SHORT | TOO_LONG | TWO_CONTS;
// Assuming that a valid UTF-8 sequence ends at pInputBuffer,
// computes how many bytes are needed to complete the last character. also counts the number of n4, n2 and ascii affected
// This will return 1, 2, 3. If the whole byte sequence is valid UTF-8,
// and this function returns returnedvalue>0, then the bytes at pInputBuffer[0],
// ... pInputBuffer[returnedvalue - 1] should be continuation bytes.
// Note that this function is unsafe, and it is the caller's responsibility
// to ensure that we can read at least 4 bytes before pInputBuffer.
private unsafe static (int totalbyteadjustment, int backedupByHowMuch, int ascii, int contbyte, int n4) adjustmentFactor(byte* pInputBuffer)
{
// Find the first non-continuation byte, working backward.
int i = 1;
int contbyteadjust = 0;
for (; i <= 4; i++)
{
if ((pInputBuffer[-i] & 0b11000000) != 0b10000000)
{
break;
}
contbyteadjust -= 1;
}
if ((pInputBuffer[-i] & 0b10000000) == 0)
{
return (0, i, -1, contbyteadjust, 0); // We must have that i == 1
}
if ((pInputBuffer[-i] & 0b11100000) == 0b11000000)
{
return (2 - i, i, 0, contbyteadjust, 0); // We have that i == 1 or i == 2, if i == 1, we are missing one byte.
}
if ((pInputBuffer[-i] & 0b11110000) == 0b11100000)
{
return (3 - i, i, 0, contbyteadjust, 0); // We have that i == 1 or i == 2 or i == 3, if i == 1, we are missing two bytes, if i == 2, we are missing one byte.
}
// We must have that (pInputBuffer[-i] & 0b11111000) == 0b11110000
return (4 - i, i, 0, contbyteadjust, -1); // We have that i == 1 or i == 2 or i == 3 or i == 4, if i == 1, we are missing three bytes, if i == 2, we are missing two bytes, if i == 3, we are missing one byte.
}
private static (int utfadjust, int scalaradjust) CalculateN2N3FinalSIMDAdjustments(int n4, int contbytes)
{
int n3 = -2 * n4 + 2 * contbytes;
int n2 = n4 - 3 * contbytes;
int utfadjust = -2 * n4 - 2 * n3 - n2;
int scalaradjust = -n4;
return (utfadjust, scalaradjust);
}
private unsafe static (int utfadjust, int scalaradjust) calculateErrorPathadjust(int start_point, int processedLength, byte* pInputBuffer, int n4, int contbytes)
{
// Calculate the total bytes from start_point to processedLength
int totalbyte = processedLength - start_point;
int adjusttotalbyte = 0, backedupByHowMuch = 0, adjustascii = 0, adjustcont = 0, adjustn4 = 0;
// Adjust the length to include a complete character, if necessary
if (totalbyte > 0)
{
(adjusttotalbyte, backedupByHowMuch, adjustascii, adjustcont, adjustn4) = adjustmentFactor(pInputBuffer + processedLength);
}
var (utfadjust, scalaradjust) = CalculateN2N3FinalSIMDAdjustments(n4, contbytes);
return (utfadjust, scalaradjust);
}
public unsafe static byte* GetPointerToFirstInvalidByteSse(byte* pInputBuffer, int inputLength, out int utf16CodeUnitCountAdjustment, out int scalarCountAdjustment)
{
int processedLength = 0;
if (pInputBuffer == null || inputLength <= 0)
{
utf16CodeUnitCountAdjustment = 0;
scalarCountAdjustment = 0;
return pInputBuffer;
}
if (inputLength > 128)
{
// We skip any ASCII characters at the start of the buffer
int asciirun = 0;
for (; asciirun + 64 <= inputLength; asciirun += 64)
{
Vector128<byte> block1 = Sse2.LoadVector128(pInputBuffer + asciirun);
Vector128<byte> block2 = Sse2.LoadVector128(pInputBuffer + asciirun + 16);
Vector128<byte> block3 = Sse2.LoadVector128(pInputBuffer + asciirun + 32);
Vector128<byte> block4 = Sse2.LoadVector128(pInputBuffer + asciirun + 48);
Vector128<byte> or = Sse2.Or(Sse2.Or(block1, block2), Sse2.Or(block3, block4));
if (Sse2.MoveMask(or) != 0)
{
break;
}
}
processedLength = asciirun;
if (processedLength + 16 < inputLength)
{
Vector128<byte> prevInputBlock = Vector128<byte>.Zero;
Vector128<byte> maxValue = Vector128.Create(
255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 0b11110000 - 1, 0b11100000 - 1, 0b11000000 - 1);
Vector128<byte> prevIncomplete = Sse3.SubtractSaturate(prevInputBlock, maxValue);
Vector128<byte> shuf1 = Vector128.Create(
TOO_LONG, TOO_LONG, TOO_LONG, TOO_LONG,
TOO_LONG, TOO_LONG, TOO_LONG, TOO_LONG,
TWO_CONTS, TWO_CONTS, TWO_CONTS, TWO_CONTS,
TOO_SHORT | OVERLONG_2,
TOO_SHORT,
TOO_SHORT | OVERLONG_3 | SURROGATE,
TOO_SHORT | TOO_LARGE | TOO_LARGE_1000 | OVERLONG_4);
Vector128<byte> shuf2 = Vector128.Create(
CARRY | OVERLONG_3 | OVERLONG_2 | OVERLONG_4,
CARRY | OVERLONG_2,
CARRY,
CARRY,
CARRY | TOO_LARGE,
CARRY | TOO_LARGE | TOO_LARGE_1000,
CARRY | TOO_LARGE | TOO_LARGE_1000,
CARRY | TOO_LARGE | TOO_LARGE_1000,
CARRY | TOO_LARGE | TOO_LARGE_1000,
CARRY | TOO_LARGE | TOO_LARGE_1000,
CARRY | TOO_LARGE | TOO_LARGE_1000,
CARRY | TOO_LARGE | TOO_LARGE_1000,
CARRY | TOO_LARGE | TOO_LARGE_1000,
CARRY | TOO_LARGE | TOO_LARGE_1000 | SURROGATE,
CARRY | TOO_LARGE | TOO_LARGE_1000,
CARRY | TOO_LARGE | TOO_LARGE_1000);
Vector128<byte> shuf3 = Vector128.Create(
TOO_SHORT, TOO_SHORT, TOO_SHORT, TOO_SHORT,
TOO_SHORT, TOO_SHORT, TOO_SHORT, TOO_SHORT,
TOO_LONG | OVERLONG_2 | TWO_CONTS | OVERLONG_3 | TOO_LARGE_1000 | OVERLONG_4,
TOO_LONG | OVERLONG_2 | TWO_CONTS | OVERLONG_3 | TOO_LARGE,
TOO_LONG | OVERLONG_2 | TWO_CONTS | SURROGATE | TOO_LARGE,
TOO_LONG | OVERLONG_2 | TWO_CONTS | SURROGATE | TOO_LARGE,
TOO_SHORT, TOO_SHORT, TOO_SHORT, TOO_SHORT);
Vector128<byte> thirdByte = Vector128.Create((byte)(0b11100000u - 0x80));
Vector128<byte> fourthByte = Vector128.Create((byte)(0b11110000u - 0x80));
Vector128<byte> v0f = Vector128.Create((byte)0x0F);
Vector128<byte> v80 = Vector128.Create((byte)0x80);
/****
* So we want to count the number of 4-byte sequences,
* the number of 4-byte sequences, 3-byte sequences, and
* the number of 2-byte sequences.
* We can do it indirectly. We know how many bytes in total
* we have (length). Let us assume that the length covers
* only complete sequences (we need to adjust otherwise).
* We have that
* length = 4 * n4 + 3 * n3 + 2 * n2 + n1
* where n1 is the number of 1-byte sequences (ASCII),
* n2 is the number of 2-byte sequences, n3 is the number
* of 3-byte sequences, and n4 is the number of 4-byte sequences.
*
* Let ncon be the number of continuation bytes, then we have
* length = n4 + n3 + n2 + ncon + n1
*
* We can solve for n2 and n3 in terms of the other variables:
* n3 = n1 - 2 * n4 + 2 * ncon - length
* n2 = -2 * n1 + n4 - 4 * ncon + 2 * length
* Thus we only need to count the number of continuation bytes,
* the number of ASCII bytes and the number of 4-byte sequences.
* But we need even less because we compute
* utfadjust = -2 * n4 - 2 * n3 - n2
* so n1 and length cancel out in the end. Thus we only need to compute
* n3' = - 2 * n4 + 2 * ncon
* n2' = n4 - 4 * ncon
*/
////////////
// The *block* here is what begins at processedLength and ends
// at processedLength/16*16 or when an error occurs.
///////////
int start_point = processedLength;
// The block goes from processedLength to processedLength/16*16.
int contbytes = 0; // number of continuation bytes in the block
int n4 = 0; // number of 4-byte sequences that start in this block
for (; processedLength + 16 <= inputLength; processedLength += 16)
{
Vector128<byte> currentBlock = Sse2.LoadVector128(pInputBuffer + processedLength);
int mask = Sse42.MoveMask(currentBlock);
if (mask == 0)
{
// We have an ASCII block, no need to process it, but
// we need to check if the previous block was incomplete.
//
if (!Sse41.TestZ(prevIncomplete, prevIncomplete))
{
int off = processedLength >= 3 ? processedLength - 3 : processedLength;
byte* invalidBytePointer = SimdUnicode.UTF8.SimpleRewindAndValidateWithErrors(16 - 3, pInputBuffer + processedLength - 3, inputLength - processedLength + 3);
// So the code is correct up to invalidBytePointer
if (invalidBytePointer < pInputBuffer + processedLength)
{
removeCounters(invalidBytePointer, pInputBuffer + processedLength, ref n4, ref contbytes);
}
else
{
addCounters(pInputBuffer + processedLength, invalidBytePointer, ref n4, ref contbytes);
}
int totalbyteasciierror = processedLength - start_point;
(utf16CodeUnitCountAdjustment, scalarCountAdjustment) = CalculateN2N3FinalSIMDAdjustments(n4, contbytes);
return invalidBytePointer;
}
prevIncomplete = Vector128<byte>.Zero;
// Often, we have a lot of ASCII characters in a row.
int localasciirun = 16;
if (processedLength + localasciirun + 64 <= inputLength)
{
for (; processedLength + localasciirun + 64 <= inputLength; localasciirun += 64)
{
Vector128<byte> block1 = Sse2.LoadVector128(pInputBuffer + processedLength + localasciirun);
Vector128<byte> block2 = Sse2.LoadVector128(pInputBuffer + processedLength + localasciirun + 16);
Vector128<byte> block3 = Sse2.LoadVector128(pInputBuffer + processedLength + localasciirun + 32);
Vector128<byte> block4 = Sse2.LoadVector128(pInputBuffer + processedLength + localasciirun + 48);
Vector128<byte> or = Sse2.Or(Sse2.Or(block1, block2), Sse2.Or(block3, block4));
if (Sse2.MoveMask(or) != 0)
{
break;
}
}
processedLength += localasciirun - 16;
}
}
else // Contains non-ASCII characters, we need to do non-trivial processing
{
// Use SubtractSaturate to effectively compare if bytes in block are greater than markers.
// Contains non-ASCII characters, we need to do non-trivial processing
Vector128<byte> prev1 = Ssse3.AlignRight(currentBlock, prevInputBlock, (byte)(16 - 1));
Vector128<byte> byte_1_high = Ssse3.Shuffle(shuf1, Sse2.ShiftRightLogical(prev1.AsUInt16(), 4).AsByte() & v0f);
Vector128<byte> byte_1_low = Ssse3.Shuffle(shuf2, (prev1 & v0f));
Vector128<byte> byte_2_high = Ssse3.Shuffle(shuf3, Sse2.ShiftRightLogical(currentBlock.AsUInt16(), 4).AsByte() & v0f);
Vector128<byte> sc = Sse2.And(Sse2.And(byte_1_high, byte_1_low), byte_2_high);
Vector128<byte> prev2 = Ssse3.AlignRight(currentBlock, prevInputBlock, (byte)(16 - 2));
Vector128<byte> prev3 = Ssse3.AlignRight(currentBlock, prevInputBlock, (byte)(16 - 3));
prevInputBlock = currentBlock;
Vector128<byte> isThirdByte = Sse2.SubtractSaturate(prev2, thirdByte);
Vector128<byte> isFourthByte = Sse2.SubtractSaturate(prev3, fourthByte);
Vector128<byte> must23 = Sse2.Or(isThirdByte, isFourthByte);
Vector128<byte> must23As80 = Sse2.And(must23, v80);
Vector128<byte> error = Sse2.Xor(must23As80, sc);
if (!Sse42.TestZ(error, error))
{
byte* invalidBytePointer;
if (processedLength == 0)
{
invalidBytePointer = SimdUnicode.UTF8.SimpleRewindAndValidateWithErrors(0, pInputBuffer + processedLength, inputLength - processedLength);
}
else
{
invalidBytePointer = SimdUnicode.UTF8.SimpleRewindAndValidateWithErrors(3, pInputBuffer + processedLength - 3, inputLength - processedLength + 3);
}
if (invalidBytePointer < pInputBuffer + processedLength)
{
removeCounters(invalidBytePointer, pInputBuffer + processedLength, ref n4, ref contbytes);
}
else
{
addCounters(pInputBuffer + processedLength, invalidBytePointer, ref n4, ref contbytes);
}
int total_bytes_processed = (int)(invalidBytePointer - (pInputBuffer + start_point));
(utf16CodeUnitCountAdjustment, scalarCountAdjustment) = CalculateN2N3FinalSIMDAdjustments(n4, contbytes);
return invalidBytePointer;
}
prevIncomplete = Sse3.SubtractSaturate(currentBlock, maxValue);
contbytes += (int)Popcnt.PopCount((uint)Sse42.MoveMask(byte_2_high));
// We use two instructions (SubtractSaturate and MoveMask) to update n4, with one arithmetic operation.
n4 += (int)Popcnt.PopCount((uint)Sse42.MoveMask(Sse42.SubtractSaturate(currentBlock, fourthByte)));
}
}
// We may still have an error.
bool hasIncompete = !Sse42.TestZ(prevIncomplete, prevIncomplete);
if (processedLength < inputLength || hasIncompete)
{
byte* invalidBytePointer;
if (processedLength == 0 || !hasIncompete)
{
invalidBytePointer = SimdUnicode.UTF8.SimpleRewindAndValidateWithErrors(0, pInputBuffer + processedLength, inputLength - processedLength);
}
else
{
invalidBytePointer = SimdUnicode.UTF8.SimpleRewindAndValidateWithErrors(3, pInputBuffer + processedLength - 3, inputLength - processedLength + 3);
}
if (invalidBytePointer != pInputBuffer + inputLength)
{
if (invalidBytePointer < pInputBuffer + processedLength)
{
removeCounters(invalidBytePointer, pInputBuffer + processedLength, ref n4, ref contbytes);
}
else
{
addCounters(pInputBuffer + processedLength, invalidBytePointer, ref n4, ref contbytes);
}
int total_bytes_processed = (int)(invalidBytePointer - (pInputBuffer + start_point));
(utf16CodeUnitCountAdjustment, scalarCountAdjustment) = CalculateN2N3FinalSIMDAdjustments(n4, contbytes);
return invalidBytePointer;
}
else
{
addCounters(pInputBuffer + processedLength, invalidBytePointer, ref n4, ref contbytes);
}
}
int final_total_bytes_processed = inputLength - start_point;
(utf16CodeUnitCountAdjustment, scalarCountAdjustment) = CalculateN2N3FinalSIMDAdjustments(n4, contbytes);
return pInputBuffer + inputLength;
}
}
return GetPointerToFirstInvalidByteScalar(pInputBuffer + processedLength, inputLength - processedLength, out utf16CodeUnitCountAdjustment, out scalarCountAdjustment);
}
public unsafe static byte* GetPointerToFirstInvalidByteAvx2(byte* pInputBuffer, int inputLength, out int utf16CodeUnitCountAdjustment, out int scalarCountAdjustment)
{
int processedLength = 0;
if (pInputBuffer == null || inputLength <= 0)
{
utf16CodeUnitCountAdjustment = 0;
scalarCountAdjustment = 0;
return pInputBuffer;
}
if (inputLength > 128)
{
// We skip any ASCII characters at the start of the buffer
int asciirun = 0;
for (; asciirun + 64 <= inputLength; asciirun += 64)
{
Vector256<byte> block1 = Avx.LoadVector256(pInputBuffer + asciirun);
Vector256<byte> block2 = Avx.LoadVector256(pInputBuffer + asciirun + 32);
Vector256<byte> or = Avx2.Or(block1, block2);
if (Avx2.MoveMask(or) != 0)
{
break;
}
}
processedLength = asciirun;
if (processedLength + 32 < inputLength)
{
// We still have work to do!
Vector256<byte> prevInputBlock = Vector256<byte>.Zero;
Vector256<byte> maxValue = Vector256.Create(255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 0b11110000 - 1, 0b11100000 - 1, 0b11000000 - 1);
Vector256<byte> prevIncomplete = Avx2.SubtractSaturate(prevInputBlock, maxValue);
Vector256<byte> shuf1 = Vector256.Create(TOO_LONG, TOO_LONG, TOO_LONG, TOO_LONG,
TOO_LONG, TOO_LONG, TOO_LONG, TOO_LONG,
TWO_CONTS, TWO_CONTS, TWO_CONTS, TWO_CONTS,
TOO_SHORT | OVERLONG_2,
TOO_SHORT,
TOO_SHORT | OVERLONG_3 | SURROGATE,
TOO_SHORT | TOO_LARGE | TOO_LARGE_1000 | OVERLONG_4,
TOO_LONG, TOO_LONG, TOO_LONG, TOO_LONG,
TOO_LONG, TOO_LONG, TOO_LONG, TOO_LONG,
TWO_CONTS, TWO_CONTS, TWO_CONTS, TWO_CONTS,
TOO_SHORT | OVERLONG_2,
TOO_SHORT,
TOO_SHORT | OVERLONG_3 | SURROGATE,
TOO_SHORT | TOO_LARGE | TOO_LARGE_1000 | OVERLONG_4);
Vector256<byte> shuf2 = Vector256.Create(CARRY | OVERLONG_3 | OVERLONG_2 | OVERLONG_4,
CARRY | OVERLONG_2,
CARRY,
CARRY,
CARRY | TOO_LARGE,
CARRY | TOO_LARGE | TOO_LARGE_1000,
CARRY | TOO_LARGE | TOO_LARGE_1000,
CARRY | TOO_LARGE | TOO_LARGE_1000,
CARRY | TOO_LARGE | TOO_LARGE_1000,
CARRY | TOO_LARGE | TOO_LARGE_1000,
CARRY | TOO_LARGE | TOO_LARGE_1000,
CARRY | TOO_LARGE | TOO_LARGE_1000,
CARRY | TOO_LARGE | TOO_LARGE_1000,
CARRY | TOO_LARGE | TOO_LARGE_1000 | SURROGATE,
CARRY | TOO_LARGE | TOO_LARGE_1000,
CARRY | TOO_LARGE | TOO_LARGE_1000,
CARRY | OVERLONG_3 | OVERLONG_2 | OVERLONG_4,
CARRY | OVERLONG_2,
CARRY,
CARRY,
CARRY | TOO_LARGE,
CARRY | TOO_LARGE | TOO_LARGE_1000,
CARRY | TOO_LARGE | TOO_LARGE_1000,
CARRY | TOO_LARGE | TOO_LARGE_1000,
CARRY | TOO_LARGE | TOO_LARGE_1000,
CARRY | TOO_LARGE | TOO_LARGE_1000,
CARRY | TOO_LARGE | TOO_LARGE_1000,
CARRY | TOO_LARGE | TOO_LARGE_1000,
CARRY | TOO_LARGE | TOO_LARGE_1000,
CARRY | TOO_LARGE | TOO_LARGE_1000 | SURROGATE,
CARRY | TOO_LARGE | TOO_LARGE_1000,
CARRY | TOO_LARGE | TOO_LARGE_1000);
Vector256<byte> shuf3 = Vector256.Create(TOO_SHORT, TOO_SHORT, TOO_SHORT, TOO_SHORT,
TOO_SHORT, TOO_SHORT, TOO_SHORT, TOO_SHORT,
TOO_LONG | OVERLONG_2 | TWO_CONTS | OVERLONG_3 | TOO_LARGE_1000 | OVERLONG_4,
TOO_LONG | OVERLONG_2 | TWO_CONTS | OVERLONG_3 | TOO_LARGE,
TOO_LONG | OVERLONG_2 | TWO_CONTS | SURROGATE | TOO_LARGE,
TOO_LONG | OVERLONG_2 | TWO_CONTS | SURROGATE | TOO_LARGE,
TOO_SHORT, TOO_SHORT, TOO_SHORT, TOO_SHORT, TOO_SHORT, TOO_SHORT, TOO_SHORT, TOO_SHORT,
TOO_SHORT, TOO_SHORT, TOO_SHORT, TOO_SHORT,
TOO_LONG | OVERLONG_2 | TWO_CONTS | OVERLONG_3 | TOO_LARGE_1000 | OVERLONG_4,
TOO_LONG | OVERLONG_2 | TWO_CONTS | OVERLONG_3 | TOO_LARGE,
TOO_LONG | OVERLONG_2 | TWO_CONTS | SURROGATE | TOO_LARGE,
TOO_LONG | OVERLONG_2 | TWO_CONTS | SURROGATE | TOO_LARGE,
TOO_SHORT, TOO_SHORT, TOO_SHORT, TOO_SHORT);
Vector256<byte> thirdByte = Vector256.Create((byte)(0b11100000u - 0x80));
Vector256<byte> fourthByte = Vector256.Create((byte)(0b11110000u - 0x80));
Vector256<byte> v0f = Vector256.Create((byte)0x0F);
Vector256<byte> v80 = Vector256.Create((byte)0x80);
/****
* So we want to count the number of 4-byte sequences,
* the number of 4-byte sequences, 3-byte sequences, and
* the number of 2-byte sequences.
* We can do it indirectly. We know how many bytes in total
* we have (length). Let us assume that the length covers
* only complete sequences (we need to adjust otherwise).
* We have that
* length = 4 * n4 + 3 * n3 + 2 * n2 + n1
* where n1 is the number of 1-byte sequences (ASCII),
* n2 is the number of 2-byte sequences, n3 is the number
* of 3-byte sequences, and n4 is the number of 4-byte sequences.
*
* Let ncon be the number of continuation bytes, then we have
* length = n4 + n3 + n2 + ncon + n1
*
* We can solve for n2 and n3 in terms of the other variables:
* n3 = n1 - 2 * n4 + 2 * ncon - length
* n2 = -2 * n1 + n4 - 4 * ncon + 2 * length
* Thus we only need to count the number of continuation bytes,
* the number of ASCII bytes and the number of 4-byte sequences.
* But we need even less because we compute
* utfadjust = -2 * n4 - 2 * n3 - n2
* so n1 and length cancel out in the end. Thus we only need to compute
* n3' = - 2 * n4 + 2 * ncon
* n2' = n4 - 4 * ncon
*/
////////////
// The *block* here is what begins at processedLength and ends
// at processedLength/16*16 or when an error occurs.
///////////
int start_point = processedLength;
// The block goes from processedLength to processedLength/16*16.
int contbytes = 0; // number of continuation bytes in the block
int n4 = 0; // number of 4-byte sequences that start in this block
for (; processedLength + 32 <= inputLength; processedLength += 32)
{
Vector256<byte> currentBlock = Avx.LoadVector256(pInputBuffer + processedLength);
int mask = Avx2.MoveMask(currentBlock);
if (mask == 0)
{
// We have an ASCII block, no need to process it, but
// we need to check if the previous block was incomplete.
if (!Avx2.TestZ(prevIncomplete, prevIncomplete))
{
int off = processedLength >= 3 ? processedLength - 3 : processedLength;
byte* invalidBytePointer = SimdUnicode.UTF8.SimpleRewindAndValidateWithErrors(32 - 3, pInputBuffer + processedLength - 3, inputLength - processedLength + 3);
// So the code is correct up to invalidBytePointer
if (invalidBytePointer < pInputBuffer + processedLength)
{
removeCounters(invalidBytePointer, pInputBuffer + processedLength, ref n4, ref contbytes);
}
else
{
addCounters(pInputBuffer + processedLength, invalidBytePointer, ref n4, ref contbytes);
}
int totalbyteasciierror = processedLength - start_point;
(utf16CodeUnitCountAdjustment, scalarCountAdjustment) = CalculateN2N3FinalSIMDAdjustments(n4, contbytes);
return invalidBytePointer;
}
prevIncomplete = Vector256<byte>.Zero;
// Often, we have a lot of ASCII characters in a row.
int localasciirun = 32;
if (processedLength + localasciirun + 64 <= inputLength)
{
for (; processedLength + localasciirun + 64 <= inputLength; localasciirun += 64)
{
Vector256<byte> block1 = Avx.LoadVector256(pInputBuffer + processedLength + localasciirun);
Vector256<byte> block2 = Avx.LoadVector256(pInputBuffer + processedLength + localasciirun + 32);
Vector256<byte> or = Avx2.Or(block1, block2);
if (Avx2.MoveMask(or) != 0)
{
break;
}
}
processedLength += localasciirun - 32;
}
}
else // Contains non-ASCII characters, we need to do non-trivial processing
{
// Use SubtractSaturate to effectively compare if bytes in block are greater than markers.
Vector256<byte> shuffled = Avx2.Permute2x128(prevInputBlock, currentBlock, 0x21);
prevInputBlock = currentBlock;
Vector256<byte> prev1 = Avx2.AlignRight(prevInputBlock, shuffled, (byte)(16 - 1));
// Vector256.Shuffle vs Avx2.Shuffle
// https://github.com/dotnet/runtime/blob/1400c1e7a888ea1e710e5c08d55c800e0b04bf8a/docs/coding-guidelines/vectorization-guidelines.md#vector256shuffle-vs-avx2shuffle
Vector256<byte> byte_1_high = Avx2.Shuffle(shuf1, Avx2.ShiftRightLogical(prev1.AsUInt16(), 4).AsByte() & v0f);// takes the XXXX 0000 part of the previous byte
Vector256<byte> byte_1_low = Avx2.Shuffle(shuf2, (prev1 & v0f)); // takes the 0000 XXXX part of the previous part
Vector256<byte> byte_2_high = Avx2.Shuffle(shuf3, Avx2.ShiftRightLogical(currentBlock.AsUInt16(), 4).AsByte() & v0f); // takes the XXXX 0000 part of the current byte
Vector256<byte> sc = Avx2.And(Avx2.And(byte_1_high, byte_1_low), byte_2_high);
Vector256<byte> prev2 = Avx2.AlignRight(prevInputBlock, shuffled, (byte)(16 - 2));
Vector256<byte> prev3 = Avx2.AlignRight(prevInputBlock, shuffled, (byte)(16 - 3));
Vector256<byte> isThirdByte = Avx2.SubtractSaturate(prev2, thirdByte);
Vector256<byte> isFourthByte = Avx2.SubtractSaturate(prev3, fourthByte);
Vector256<byte> must23 = Avx2.Or(isThirdByte, isFourthByte);
Vector256<byte> must23As80 = Avx2.And(must23, v80);
Vector256<byte> error = Avx2.Xor(must23As80, sc);
if (!Avx2.TestZ(error, error))
{
byte* invalidBytePointer;
if (processedLength == 0)
{
invalidBytePointer = SimdUnicode.UTF8.SimpleRewindAndValidateWithErrors(0, pInputBuffer + processedLength, inputLength - processedLength);
}
else
{
invalidBytePointer = SimdUnicode.UTF8.SimpleRewindAndValidateWithErrors(3, pInputBuffer + processedLength - 3, inputLength - processedLength + 3);
}
if (invalidBytePointer < pInputBuffer + processedLength)
{
removeCounters(invalidBytePointer, pInputBuffer + processedLength, ref n4, ref contbytes);
}
else
{
addCounters(pInputBuffer + processedLength, invalidBytePointer, ref n4, ref contbytes);
}
int total_bytes_processed = (int)(invalidBytePointer - (pInputBuffer + start_point));
(utf16CodeUnitCountAdjustment, scalarCountAdjustment) = CalculateN2N3FinalSIMDAdjustments(n4, contbytes);
return invalidBytePointer;
}
prevIncomplete = Avx2.SubtractSaturate(currentBlock, maxValue);
contbytes += (int)Popcnt.PopCount((uint)Avx2.MoveMask(byte_2_high));
// We use two instructions (SubtractSaturate and MoveMask) to update n4, with one arithmetic operation.
n4 += (int)Popcnt.PopCount((uint)Avx2.MoveMask(Avx2.SubtractSaturate(currentBlock, fourthByte)));
}
}
// We may still have an error.
bool hasIncompete = !Avx2.TestZ(prevIncomplete, prevIncomplete);
if (processedLength < inputLength || hasIncompete)
{
byte* invalidBytePointer;
if (processedLength == 0 || !hasIncompete)
{
invalidBytePointer = SimdUnicode.UTF8.SimpleRewindAndValidateWithErrors(0, pInputBuffer + processedLength, inputLength - processedLength);
}
else
{
invalidBytePointer = SimdUnicode.UTF8.SimpleRewindAndValidateWithErrors(3, pInputBuffer + processedLength - 3, inputLength - processedLength + 3);
}
if (invalidBytePointer != pInputBuffer + inputLength)
{
if (invalidBytePointer < pInputBuffer + processedLength)
{
removeCounters(invalidBytePointer, pInputBuffer + processedLength, ref n4, ref contbytes);
}
else
{
addCounters(pInputBuffer + processedLength, invalidBytePointer, ref n4, ref contbytes);
}
int total_bytes_processed = (int)(invalidBytePointer - (pInputBuffer + start_point));
(utf16CodeUnitCountAdjustment, scalarCountAdjustment) = CalculateN2N3FinalSIMDAdjustments(n4, contbytes);
return invalidBytePointer;
}
else
{
addCounters(pInputBuffer + processedLength, invalidBytePointer, ref n4, ref contbytes);
}
}
int final_total_bytes_processed = inputLength - start_point;
(utf16CodeUnitCountAdjustment, scalarCountAdjustment) = CalculateN2N3FinalSIMDAdjustments(n4, contbytes);
return pInputBuffer + inputLength;
}
}
return GetPointerToFirstInvalidByteScalar(pInputBuffer + processedLength, inputLength - processedLength, out utf16CodeUnitCountAdjustment, out scalarCountAdjustment);
}
public unsafe static byte* GetPointerToFirstInvalidByteAvx512(byte* pInputBuffer, int inputLength, out int utf16CodeUnitCountAdjustment, out int scalarCountAdjustment)
{
int processedLength = 0;
if (pInputBuffer == null || inputLength <= 0)
{
utf16CodeUnitCountAdjustment = 0;
scalarCountAdjustment = 0;
return pInputBuffer;
}
if (inputLength > 128)
{
// We skip any ASCII characters at the start of the buffer
// We intentionally use AVX2 instead of AVX-512.
int asciirun = 0;
for (; asciirun + 64 <= inputLength; asciirun += 64)
{
Vector256<byte> block1 = Avx.LoadVector256(pInputBuffer + asciirun);
Vector256<byte> block2 = Avx.LoadVector256(pInputBuffer + asciirun + 32);
Vector256<byte> or = Avx2.Or(block1, block2);
if (Avx2.MoveMask(or) != 0)
{
break;
}
}
processedLength = asciirun;
if (processedLength + 64 < inputLength)
{
Vector512<byte> prevInputBlock = Vector512<byte>.Zero;
Vector512<byte> maxValue = Vector512.Create(
255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 0b11110000 - 1, 0b11100000 - 1, 0b11000000 - 1);
Vector512<byte> prevIncomplete = Avx512BW.SubtractSaturate(prevInputBlock, maxValue);
Vector512<byte> shuf1 = Vector512.Create(TOO_LONG, TOO_LONG, TOO_LONG, TOO_LONG,
TOO_LONG, TOO_LONG, TOO_LONG, TOO_LONG,
TWO_CONTS, TWO_CONTS, TWO_CONTS, TWO_CONTS,
TOO_SHORT | OVERLONG_2,
TOO_SHORT,
TOO_SHORT | OVERLONG_3 | SURROGATE,
TOO_SHORT | TOO_LARGE | TOO_LARGE_1000 | OVERLONG_4,
TOO_LONG, TOO_LONG, TOO_LONG, TOO_LONG,
TOO_LONG, TOO_LONG, TOO_LONG, TOO_LONG,
TWO_CONTS, TWO_CONTS, TWO_CONTS, TWO_CONTS,
TOO_SHORT | OVERLONG_2,
TOO_SHORT,
TOO_SHORT | OVERLONG_3 | SURROGATE,
TOO_SHORT | TOO_LARGE | TOO_LARGE_1000 | OVERLONG_4,
TOO_LONG, TOO_LONG, TOO_LONG, TOO_LONG,