Исходный код СУБД RuDB — свободной СУБД с открытым исходным кодом на базе PostgreSQL
rudb
3 days ago bd039d1e92813358c5c5a1e346b49074baf513f2
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
/*++
 
Copyright (c) Microsoft Corporation. All rights reserved.
 
Licensed under the MIT License.
 
Module Name:
 
    qgemm_kernel_udot.cpp
 
Abstract:
 
    This module implements udot QGEMM kernel.
 
--*/
 
#include "mlasi.h"
#include "qgemm.h"
 
//
// Define the prototypes of the NEON UDOT routines written in assembly.
//
 
extern "C" {
 
    size_t
    MLASCALL
    MlasGemmU8X8KernelUdot(
        const uint8_t* A,
        const uint8_t* B,
        int32_t* C,
        size_t PackedCountK,
        size_t CountM,
        size_t CountN,
        size_t ldc,
        const int32_t* RowSumVector,
        const int32_t* ColumnSumVector,
        const int32_t* ZeroPointB,
        bool ZeroMode
        );
}
 
struct MLAS_GEMM_U8X8_KERNEL_UDOT
{
    typedef uint8_t PackedAType;
    typedef uint8_t PackedBType;
    typedef uint8_t OffsetAType;
    typedef uint8_t OffsetBType;
 
    static constexpr size_t PackedK = 8;
    static constexpr MLAS_GEMM_QUANT_STRIDES Strides{ 24, 128, 256 };
    static constexpr MLAS_GEMM_QUANT_STRIDES PackedStrides{ 24, 128, 384 };
};
 
constexpr size_t MLAS_GEMM_U8X8_KERNEL_UDOT::PackedK;
constexpr MLAS_GEMM_QUANT_STRIDES MLAS_GEMM_U8X8_KERNEL_UDOT::Strides;
constexpr MLAS_GEMM_QUANT_STRIDES MLAS_GEMM_U8X8_KERNEL_UDOT::PackedStrides;
 
template<>
MLAS_FORCEINLINE
int32_t
MlasGemmQuantFixupZeroPointB<MLAS_GEMM_U8X8_KERNEL_UDOT>(
    int32_t ZeroPointB,
    bool BIsSigned
    )
{
    if (BIsSigned) {
        ZeroPointB = MLAS_GEMM_U8X8_KERNEL_UDOT::OffsetBType(ZeroPointB ^ 0x80);
    }
 
    return ZeroPointB;
}
 
template<>
MLAS_FORCEINLINE
int32_t
MlasGemmQuantFixupZeroPointA<MLAS_GEMM_U8X8_KERNEL_UDOT>(
    int32_t ZeroPointA,
    bool AIsSigned
    )
{
    if (AIsSigned) {
        ZeroPointA = MLAS_GEMM_U8X8_KERNEL_UDOT::OffsetAType(ZeroPointA ^ 0x80);
    }
 
    return ZeroPointA;
}
 
template<>
void
MlasGemmQuantCopyPackA<MLAS_GEMM_U8X8_KERNEL_UDOT>(
    MLAS_GEMM_U8X8_KERNEL_UDOT::PackedAType* D,
    const uint8_t* A,
    size_t lda,
    size_t CountM,
    size_t CountK,
    int32_t* RowSumBuffer,
    bool AIsSigned
    )
{
    uint8_t PaddedMatrixAData[16];
 
    //
    // UDOT is an unsigned x unsigned dot product instruction, so a signed A
    // matrix is shifted into the unsigned domain by flipping the sign bit of
    // every real data byte (equivalent to adding 128). Padding bytes must stay
    // literal zero so they keep contributing nothing to the dot product or to
    // RowSumBuffer; MlasGemmQuantFixupZeroPointA applies the matching shift to
    // ZeroPointA so the zero-point correction stays consistent.
    //
 
    // Four views of the same flip value, one per register width used below:
    // the 8/4/2/1-row blocks mix uint32_t scalar loads, uint32x4_t vector
    // loads, and byte-at-a-time tail copies, so each width needs its own
    // pre-splatted constant.
    const uint8_t AByteFlip = AIsSigned ? 0x80 : 0;
    const uint8x16_t AFlipVector = vdupq_n_u8(AByteFlip);
    const uint32x4_t AFlipVector32 = vdupq_n_u32(AIsSigned ? 0x80808080u : 0u);
    const uint32_t AFlipWord32 = AIsSigned ? 0x80808080u : 0u;
 
    //
    // Process 8 rows of matrix A.
    // 
    // DOT kernels load 8x4 block of A with two vector registers. So A is packed
    // a series of 16 byte vectors where four rows are interleaved with the
    // following pattern:
    //
    //      [ A0 A1 A2 A3 B0 B1 B2 B3 C0 C1 C2 C3 D0 D1 D2 D3 ]
    //      [ E0 E1 E2 E3 F0 F1 F2 F3 G0 G1 G2 G3 H0 H1 H2 H3 ]
    // 
    //      [ A4 A5 A6 A7 B4 B5 B6 B7 C4 C5 C6 C7 D4 D5 D6 D7 ]
    //      [ E4 E5 E6 E7 F4 F5 F6 F7 G4 G5 G6 G7 H4 H5 H6 H7 ]
    // 
    //      ...
    //
    // This pattern is repeated (CountK / 8) times.
    //
    // If CountK is not aligned to a multiple of eight, then the vector is padded
    // with zeroes.
    //
 
    while (CountM >= 8) {
        const uint8_t* a0 = A;
        const uint8_t* a1 = a0 + lda;
        const uint8_t* a2 = a0 + lda * 2;
        const uint8_t* a3 = a0 + lda * 3;
        const uint8_t* a4 = a0 + lda * 4;
        const uint8_t* a5 = a0 + lda * 5;
        const uint8_t* a6 = a0 + lda * 6;
        const uint8_t* a7 = a0 + lda * 7;
 
        size_t k = CountK;
        uint32x4_t RowSums0 = vmovq_n_u32(0);
        uint32x4_t RowSums1 = vmovq_n_u32(0);
 
        while (k >= 16) {
            uint32x4_t v0 = veorq_u32(vld1q_u32(reinterpret_cast<const uint32_t*>(a0)), AFlipVector32);
            a0 += 16;
            uint32x4_t v1 = veorq_u32(vld1q_u32(reinterpret_cast<const uint32_t*>(a1)), AFlipVector32);
            a1 += 16;
            uint32x4_t v2 = veorq_u32(vld1q_u32(reinterpret_cast<const uint32_t*>(a2)), AFlipVector32);
            a2 += 16;
            uint32x4_t v3 = veorq_u32(vld1q_u32(reinterpret_cast<const uint32_t*>(a3)), AFlipVector32);
            a3 += 16;
            uint32x4_t v4 = veorq_u32(vld1q_u32(reinterpret_cast<const uint32_t*>(a4)), AFlipVector32);
            a4 += 16;
            uint32x4_t v5 = veorq_u32(vld1q_u32(reinterpret_cast<const uint32_t*>(a5)), AFlipVector32);
            a5 += 16;
            uint32x4_t v6 = veorq_u32(vld1q_u32(reinterpret_cast<const uint32_t*>(a6)), AFlipVector32);
            a6 += 16;
            uint32x4_t v7 = veorq_u32(vld1q_u32(reinterpret_cast<const uint32_t*>(a7)), AFlipVector32);
            a7 += 16;
 
            uint32x4_t z0 = vzip1q_u32(v0, v2);
            uint32x4_t z1 = vzip2q_u32(v0, v2);
            uint32x4_t z2 = vzip1q_u32(v1, v3);
            uint32x4_t z3 = vzip2q_u32(v1, v3);
 
            uint32x4_t z4 = vzip1q_u32(v4, v6);
            uint32x4_t z5 = vzip2q_u32(v4, v6);
            uint32x4_t z6 = vzip1q_u32(v5, v7);
            uint32x4_t z7 = vzip2q_u32(v5, v7);
 
            v0 = vzip1q_u32(z0, z2);
            v1 = vzip2q_u32(z0, z2);
            v2 = vzip1q_u32(z1, z3);
            v3 = vzip2q_u32(z1, z3);
 
            v4 = vzip1q_u32(z4, z6);
            v5 = vzip2q_u32(z4, z6);
            v6 = vzip1q_u32(z5, z7);
            v7 = vzip2q_u32(z5, z7);
 
            vst1q_u8(&D[0], vreinterpretq_u8_u32(v0));
            vst1q_u8(&D[16], vreinterpretq_u8_u32(v4));
            vst1q_u8(&D[32], vreinterpretq_u8_u32(v1));
            vst1q_u8(&D[48], vreinterpretq_u8_u32(v5));
            vst1q_u8(&D[64], vreinterpretq_u8_u32(v2));
            vst1q_u8(&D[80], vreinterpretq_u8_u32(v6));
            vst1q_u8(&D[96], vreinterpretq_u8_u32(v3));
            vst1q_u8(&D[112], vreinterpretq_u8_u32(v7));
 
            RowSums0 = vpadalq_u16(RowSums0, vpaddlq_u8(vreinterpretq_u8_u32(v0)));
            RowSums0 = vpadalq_u16(RowSums0, vpaddlq_u8(vreinterpretq_u8_u32(v1)));
            RowSums0 = vpadalq_u16(RowSums0, vpaddlq_u8(vreinterpretq_u8_u32(v2)));
            RowSums0 = vpadalq_u16(RowSums0, vpaddlq_u8(vreinterpretq_u8_u32(v3)));
 
            RowSums1 = vpadalq_u16(RowSums1, vpaddlq_u8(vreinterpretq_u8_u32(v4)));
            RowSums1 = vpadalq_u16(RowSums1, vpaddlq_u8(vreinterpretq_u8_u32(v5)));
            RowSums1 = vpadalq_u16(RowSums1, vpaddlq_u8(vreinterpretq_u8_u32(v6)));
            RowSums1 = vpadalq_u16(RowSums1, vpaddlq_u8(vreinterpretq_u8_u32(v7)));
 
            D += 128;
            k -= 16;
        }
 
        while (k >= 4) {
            uint32_t v0 = *reinterpret_cast<const uint32_t*>(a0) ^ AFlipWord32;
            a0 += 4;
            uint32_t v1 = *reinterpret_cast<const uint32_t*>(a1) ^ AFlipWord32;
            a1 += 4;
            uint32_t v2 = *reinterpret_cast<const uint32_t*>(a2) ^ AFlipWord32;
            a2 += 4;
            uint32_t v3 = *reinterpret_cast<const uint32_t*>(a3) ^ AFlipWord32;
            a3 += 4;
            uint32_t v4 = *reinterpret_cast<const uint32_t*>(a4) ^ AFlipWord32;
            a4 += 4;
            uint32_t v5 = *reinterpret_cast<const uint32_t*>(a5) ^ AFlipWord32;
            a5 += 4;
            uint32_t v6 = *reinterpret_cast<const uint32_t*>(a6) ^ AFlipWord32;
            a6 += 4;
            uint32_t v7 = *reinterpret_cast<const uint32_t*>(a7) ^ AFlipWord32;
            a7 += 4;
 
            *reinterpret_cast<uint32_t*>(&D[0]) = v0;
            *reinterpret_cast<uint32_t*>(&D[4]) = v1;
            *reinterpret_cast<uint32_t*>(&D[8]) = v2;
            *reinterpret_cast<uint32_t*>(&D[12]) = v3;
            *reinterpret_cast<uint32_t*>(&D[16]) = v4;
            *reinterpret_cast<uint32_t*>(&D[20]) = v5;
            *reinterpret_cast<uint32_t*>(&D[24]) = v6;
            *reinterpret_cast<uint32_t*>(&D[28]) = v7;
 
            RowSums0 = vpadalq_u16(RowSums0, vpaddlq_u8(vld1q_u8(D)));
            RowSums1 = vpadalq_u16(RowSums1, vpaddlq_u8(vld1q_u8(&D[16])));
 
            D += 32;
            k -= 4;
        }
 
        if (k > 0) {
            //
            // Copy the remaining bytes to the zero padded stack buffer.
            //
            uint8_t* d = D;
 
            vst1q_u8(d, vmovq_n_u8(0));
            vst1q_u8(&d[16], vmovq_n_u8(0));
 
            while (k > 0) {
                d[0] = (*a0++) ^ AByteFlip;
                d[4] = (*a1++) ^ AByteFlip;
                d[8] = (*a2++) ^ AByteFlip;
                d[12] = (*a3++) ^ AByteFlip;
                d[16] = (*a4++) ^ AByteFlip;
                d[20] = (*a5++) ^ AByteFlip;
                d[24] = (*a6++) ^ AByteFlip;
                d[28] = (*a7++) ^ AByteFlip;
                d += 1;
                k -= 1;
            }
 
            RowSums0 = vpadalq_u16(RowSums0, vpaddlq_u8(vld1q_u8(D)));
            RowSums1 = vpadalq_u16(RowSums1, vpaddlq_u8(vld1q_u8(&D[16])));
 
            D += 32;
        }
 
        if (((CountK - 1) & 7) < 4) {
            vst1q_u8(D, vmovq_n_u8(0));
            vst1q_u8(&D[16], vmovq_n_u8(0));
            D += 32;
        }
 
        vst1q_s32(RowSumBuffer, vreinterpretq_s32_u32(RowSums0));
        vst1q_s32(&RowSumBuffer[4], vreinterpretq_s32_u32(RowSums1));
 
        RowSumBuffer += 8;
 
        A = A + lda * 8;
        CountM -= 8;
    }
 
    //
    // Process four rows of matrix A.
    //
    // The buffer is packed as a series of 16 byte vectors where four rows are
    // interleaved with the following pattern:
    //
    //      [ A0 A1 A2 A3 B0 B1 B2 B3 C0 C1 C2 C3 D0 D1 D2 D3 ]
    //      [ A4 A5 A6 A7 B4 B5 B6 B7 C4 C5 C6 C7 D4 D5 D6 D7 ]
    //
    // This pattern is repeated (CountK / 8) times.
    //
    // If CountK is not aligned to a multiple of eight, then the vector is padded
    // with zeroes.
    //
 
    if (CountM >= 4) {
 
        const uint8_t* a0 = A;
        const uint8_t* a1 = a0 + lda;
        const uint8_t* a2 = a1 + lda;
        const uint8_t* a3 = a2 + lda;
 
        size_t k = CountK;
        uint32x4_t RowSums = vmovq_n_u32(0);
 
        while (k >= 16) {
 
            uint32x4_t v0 = veorq_u32(vld1q_u32(reinterpret_cast<const uint32_t*>(a0)), AFlipVector32);
            a0 += 16;
            uint32x4_t v1 = veorq_u32(vld1q_u32(reinterpret_cast<const uint32_t*>(a1)), AFlipVector32);
            a1 += 16;
            uint32x4_t v2 = veorq_u32(vld1q_u32(reinterpret_cast<const uint32_t*>(a2)), AFlipVector32);
            a2 += 16;
            uint32x4_t v3 = veorq_u32(vld1q_u32(reinterpret_cast<const uint32_t*>(a3)), AFlipVector32);
            a3 += 16;
 
            uint32x4_t z0 = vzip1q_u32(v0, v2);
            uint32x4_t z1 = vzip2q_u32(v0, v2);
            uint32x4_t z2 = vzip1q_u32(v1, v3);
            uint32x4_t z3 = vzip2q_u32(v1, v3);
 
            v0 = vzip1q_u32(z0, z2);
            v1 = vzip2q_u32(z0, z2);
            v2 = vzip1q_u32(z1, z3);
            v3 = vzip2q_u32(z1, z3);
 
            vst1q_u8(&D[0], vreinterpretq_u8_u32(v0));
            vst1q_u8(&D[16], vreinterpretq_u8_u32(v1));
            vst1q_u8(&D[32], vreinterpretq_u8_u32(v2));
            vst1q_u8(&D[48], vreinterpretq_u8_u32(v3));
 
            RowSums = vpadalq_u16(RowSums, vpaddlq_u8(vreinterpretq_u8_u32(v0)));
            RowSums = vpadalq_u16(RowSums, vpaddlq_u8(vreinterpretq_u8_u32(v1)));
            RowSums = vpadalq_u16(RowSums, vpaddlq_u8(vreinterpretq_u8_u32(v2)));
            RowSums = vpadalq_u16(RowSums, vpaddlq_u8(vreinterpretq_u8_u32(v3)));
 
            D += 64;
            k -= 16;
        }
 
        while (k >= 4) {
 
            uint32_t v0 = *reinterpret_cast<const uint32_t*>(a0) ^ AFlipWord32;
            a0 += 4;
            uint32_t v1 = *reinterpret_cast<const uint32_t*>(a1) ^ AFlipWord32;
            a1 += 4;
            uint32_t v2 = *reinterpret_cast<const uint32_t*>(a2) ^ AFlipWord32;
            a2 += 4;
            uint32_t v3 = *reinterpret_cast<const uint32_t*>(a3) ^ AFlipWord32;
            a3 += 4;
 
            *reinterpret_cast<uint32_t*>(&D[0]) = v0;
            *reinterpret_cast<uint32_t*>(&D[4]) = v1;
            *reinterpret_cast<uint32_t*>(&D[8]) = v2;
            *reinterpret_cast<uint32_t*>(&D[12]) = v3;
 
            RowSums = vpadalq_u16(RowSums, vpaddlq_u8(vld1q_u8(D)));
 
            D += 16;
            k -= 4;
        }
 
        if (k > 0) {
 
            //
            // Copy the remaining bytes to the zero padded stack buffer.
            //
 
            uint8_t* d = PaddedMatrixAData;
 
            vst1q_u8(PaddedMatrixAData, vmovq_n_u8(0));
 
            while (k > 0) {
 
                d[0] = (*a0++) ^ AByteFlip;
                d[4] = (*a1++) ^ AByteFlip;
                d[8] = (*a2++) ^ AByteFlip;
                d[12] = (*a3++) ^ AByteFlip;
 
                d += 1;
                k -= 1;
            }
 
            uint8x16_t PackedVector = vld1q_u8(PaddedMatrixAData);
            vst1q_u8(D, PackedVector);
 
            RowSums = vpadalq_u16(RowSums, vpaddlq_u8(PackedVector));
 
            D += 16;
        }
 
        if (((CountK - 1) & 7) < 4) {
 
            vst1q_u8(D, vmovq_n_u8(0));
 
            D += 16;
        }
 
        vst1q_s32(RowSumBuffer, vreinterpretq_s32_u32(RowSums));
        RowSumBuffer += 4;
 
        A = A + lda * 4;
        CountM -= 4;
    }
 
    //
    // Process two rows of matrix A.
    //
    // The buffer is packed as a series of 8 byte vectors where two rows are
    // interleaved with the following pattern:
    //
    //      [ A0 A1 A2 A3 B0 B1 B2 B3 ]
    //      [ A4 A5 A6 A7 B4 B5 B6 B7 ]
    //
    // This pattern is repeated (CountK / 8) times.
    //
    // If CountK is not aligned to a multiple of four, then the vector is padded
    // with zeroes.
    //
 
    if (CountM >= 2) {
 
        const uint8_t* a0 = A;
        const uint8_t* a1 = a0 + lda;
 
        size_t k = CountK;
        uint32x2_t RowSums = vmov_n_u32(0);
 
        while (k >= 4) {
 
            uint32_t v0 = *reinterpret_cast<const uint32_t*>(a0) ^ AFlipWord32;
            a0 += 4;
            uint32_t v1 = *reinterpret_cast<const uint32_t*>(a1) ^ AFlipWord32;
            a1 += 4;
 
            *reinterpret_cast<uint32_t*>(&D[0]) = v0;
            *reinterpret_cast<uint32_t*>(&D[4]) = v1;
 
            RowSums = vpadal_u16(RowSums, vpaddl_u8(vld1_u8(D)));
 
            D += 8;
            k -= 4;
        }
 
        if (k > 0) {
 
            //
            // Copy the remaining bytes to the zero padded stack buffer.
            //
 
            uint8_t* d = PaddedMatrixAData;
 
            vst1_u8(PaddedMatrixAData, vmov_n_u8(0));
 
            while (k > 0) {
 
                d[0] = (*a0++) ^ AByteFlip;
                d[4] = (*a1++) ^ AByteFlip;
 
                d += 1;
                k -= 1;
            }
 
            uint8x8_t PackedVector = vld1_u8(PaddedMatrixAData);
            vst1_u8(D, PackedVector);
 
            RowSums = vpadal_u16(RowSums, vpaddl_u8(PackedVector));
 
            D += 8;
        }
 
        if (((CountK - 1) & 7) < 4) {
 
            vst1_u8(D, vmov_n_u8(0));
 
            D += 8;
        }
 
        vst1_s32(RowSumBuffer, vreinterpret_s32_u32(RowSums));
        RowSumBuffer += 2;
 
        A = A + lda * 2;
        CountM -= 2;
    }
 
    //
    // Process one row of matrix A.
    //
    // The buffer is packed as a series of 4 byte with the following pattern:
    //
    //      [ A0 A1 A2 A3 ]
    //      [ A4 A5 A6 A7 ]
    //
    // This pattern is repeated (CountK / 8) times.
    //
    // If CountK is not aligned to a multiple of four, then the vector is padded
    // with zeroes.
    //
 
    if (CountM > 0) {
 
        const uint8_t* a = A;
        size_t k = CountK;
        uint32x4_t RowSums = vmovq_n_u32(0);
 
        while (k >= 16) {
 
            uint8x16_t v = veorq_u8(vld1q_u8(a), AFlipVector);
            a += 16;
 
            vst1q_u8(D, v);
 
            RowSums = vpadalq_u16(RowSums, vpaddlq_u8(v));
 
            D += 16;
            k -= 16;
        }
 
        if (k > 0) {
 
            //
            // Copy the remaining bytes to the zero padded stack buffer.
            //
 
            vst1q_u8(PaddedMatrixAData, vmovq_n_u8(0));
 
            for (size_t kk = 0; kk < k; kk++) {
                PaddedMatrixAData[kk] = a[kk] ^ AByteFlip;
            }
 
            uint8x16_t v = vld1q_u8(PaddedMatrixAData);
            vst1q_u8(D, v);
 
            RowSums = vpadalq_u16(RowSums, vpaddlq_u8(v));
        }
 
#if defined(_M_ARM64)
        // N.B. The workaround of defining a local vaddvq_u32 doesn't work here
        // as VS2019 added new intrinsics to make the operation work. Also, not
        // all build environments using VS2019 have the up-to-date arm64_neon.h,
        // so fallback to pairwise addition.
        RowSums = vpaddq_u32(RowSums, RowSums);
        RowSums = vpaddq_u32(RowSums, RowSums);
        vst1q_lane_u32(reinterpret_cast<uint32_t*>(RowSumBuffer), RowSums, 0);
#else
        *RowSumBuffer = int32_t(vaddvq_u32(RowSums));
#endif
    }
}
 
MLAS_FORCEINLINE
void
MlasGemmU8X8CopyPackBProcessUdot(
    MLAS_GEMM_U8X8_KERNEL_UDOT::PackedBType* D,
    uint8x8_t BytesRow[4],
    uint8x16_t BitFlipVector,
    uint32x4_t ColumnSums[2]
    )
{
    uint8x16_t v02 = veorq_u8(vcombine_u8(BytesRow[0], BytesRow[2]), BitFlipVector);
    uint8x16_t v13 = veorq_u8(vcombine_u8(BytesRow[1], BytesRow[3]), BitFlipVector);
 
    uint8x16x2_t zw = vzipq_u8(v02, v13);
    uint16x8x2_t zd = vzipq_u16(vreinterpretq_u16_u8(zw.val[0]), vreinterpretq_u16_u8(zw.val[1]));
 
    vst1q_u8(&D[0], vreinterpretq_u8_u16(zd.val[0]));
    vst1q_u8(&D[16], vreinterpretq_u8_u16(zd.val[1]));
 
    ColumnSums[0] = vpadalq_u16(ColumnSums[0], vpaddlq_u8(vreinterpretq_u8_u16(zd.val[0])));
    ColumnSums[1] = vpadalq_u16(ColumnSums[1], vpaddlq_u8(vreinterpretq_u8_u16(zd.val[1])));
}
 
template<>
void
MlasGemmQuantCopyPackB<MLAS_GEMM_U8X8_KERNEL_UDOT>(
    MLAS_GEMM_U8X8_KERNEL_UDOT::PackedBType* D,
    const uint8_t* B,
    size_t ldb,
    size_t CountN,
    size_t CountK,
    int32_t* ColumnSumBuffer,
    bool BIsSigned
    )
{
    const uint8x16_t ZeroVector = vmovq_n_u8(0);
    const uint8x16_t BitFlipVector = vdupq_n_u8(BIsSigned ? 0x80 : 0);
    uint8x8_t BytesRow[4];
 
    //
    // Process 8 columns of matrix B in a loop.
    //
    // The buffer is packed as a series of 16 byte vectors where eight rows are
    // interleaved with the following pattern:
    //
    //      [ A0 A1 A2 A3 B0 B1 B2 B3 C0 C1 C2 C3 D0 D1 D2 D3 ]
    //      [ E0 E1 E2 E3 F0 F1 F2 F3 G0 G1 G2 G3 H0 H1 H2 H3 ]
    //      [ A4 A5 A6 A7 B4 B5 B6 B7 C4 C5 C6 C7 D4 D5 D6 D7 ]
    //      [ E4 E5 E6 E7 F4 F5 F6 F7 G4 G5 G6 G7 H4 H5 H6 H7 ]
    //
    // Copy columns from matrix B to the packed buffer. Signed buffers are
    // converted to unsigned buffers in order to share a common kernel.
    //
    // If CountK is not aligned to a multiple of eight, then the packed buffer
    // is padded with zero vectors.
    //
    // If CountN is not aligned to a multiple of four, then the extra columns
    // are padded with zeroes.
    //
 
    while (CountN >= 8) {
 
        const uint8_t* b = B;
        size_t k = CountK;
        uint32x4_t ColumnSums[2];
 
        ColumnSums[0] = vmovq_n_u32(0);
        ColumnSums[1] = vmovq_n_u32(0);
 
        //
        // Interleave rows of matrix B and write to the packed buffer.
        //
 
        while (k >= 4) {
 
            BytesRow[0] = vld1_u8(&b[ldb * 0]);
            BytesRow[1] = vld1_u8(&b[ldb * 1]);
            BytesRow[2] = vld1_u8(&b[ldb * 2]);
            BytesRow[3] = vld1_u8(&b[ldb * 3]);
 
            MlasGemmU8X8CopyPackBProcessUdot(D, BytesRow, BitFlipVector, ColumnSums);
 
            b += ldb * 4;
            D += 32;
            k -= 4;
        }
 
        if (k > 0) {
 
            BytesRow[0] = vld1_u8(&b[ldb * 0]);
            BytesRow[1] = (k >= 2) ? vld1_u8(&b[ldb * 1]) : vget_low_u8(BitFlipVector);
            BytesRow[2] = (k > 2) ? vld1_u8(&b[ldb * 2]) : vget_low_u8(BitFlipVector);
            BytesRow[3] = vget_low_u8(BitFlipVector);
 
            MlasGemmU8X8CopyPackBProcessUdot(D, BytesRow, BitFlipVector, ColumnSums);
 
            D += 32;
        }
 
        //
        // Zero pad the output buffer to a multiple of PackedK if the above
        // processed an odd number of four row bundles.
        //
 
        if (((CountK - 1) & 7) < 4) {
 
            vst1q_u8(&D[0], ZeroVector);
            vst1q_u8(&D[16], ZeroVector);
 
            D += 32;
        }
 
        vst1q_s32(&ColumnSumBuffer[0], vreinterpretq_s32_u32(ColumnSums[0]));
        vst1q_s32(&ColumnSumBuffer[4], vreinterpretq_s32_u32(ColumnSums[1]));
        ColumnSumBuffer += 8;
 
        B += 8;
        CountN -= 8;
    }
 
    //
    // Process the remaining columns of matrix B.
    //
 
    if (CountN > 0) {
 
        const uint8_t* b = B;
        size_t k = CountK;
        uint8_t PaddedMatrixBData[32];
        uint32x4_t ColumnSums[2];
 
        vst1q_u8(&PaddedMatrixBData[0], BitFlipVector);
        vst1q_u8(&PaddedMatrixBData[16], BitFlipVector);
 
        ColumnSums[0] = vmovq_n_u32(0);
        ColumnSums[1] = vmovq_n_u32(0);
 
        //
        // Interleave rows of matrix B using an intermediate zero padded stack
        // buffer and write to the packed buffer.
        //
 
        while (k > 0) {
 
            const uint8_t* bcopy0 = &b[ldb * 0];
            const uint8_t* bcopy1 = &b[ldb * 1];
            const uint8_t* bcopy2 = &b[ldb * 2];
            const uint8_t* bcopy3 = &b[ldb * 3];
 
            if (k >= 4) {
 
                b += ldb * 4;
                k -= 4;
 
            } else {
 
                vst1q_u8(&PaddedMatrixBData[0], BitFlipVector);
                vst1q_u8(&PaddedMatrixBData[16], BitFlipVector);
 
                bcopy1 = (k >= 2) ? bcopy1 : &PaddedMatrixBData[24];
                bcopy2 = (k > 2) ? bcopy2 : &PaddedMatrixBData[24];
                bcopy3 = &PaddedMatrixBData[24];
 
                k = 0;
            }
 
            uint8_t* padded = PaddedMatrixBData;
            uint8_t* padded_end = padded + CountN;
 
            do {
                padded[0] = *bcopy0++;
                padded[8] = *bcopy1++;
                padded[16] = *bcopy2++;
                padded[24] = *bcopy3++;
            } while (++padded < padded_end);
 
            BytesRow[0] = vld1_u8(&PaddedMatrixBData[0]);
            BytesRow[1] = vld1_u8(&PaddedMatrixBData[8]);
            BytesRow[2] = vld1_u8(&PaddedMatrixBData[16]);
            BytesRow[3] = vld1_u8(&PaddedMatrixBData[24]);
 
            MlasGemmU8X8CopyPackBProcessUdot(D, BytesRow, BitFlipVector, ColumnSums);
 
            D += 32;
        }
 
        //
        // Zero pad the output buffer to a multiple of PackedK if the above
        // processed an odd number of four row bundles.
        //
 
        if (((CountK - 1) & 7) < 4) {
 
            vst1q_u8(&D[0], ZeroVector);
            vst1q_u8(&D[16], ZeroVector);
 
            D += 32;
        }
 
        vst1q_s32(&ColumnSumBuffer[0], vreinterpretq_s32_u32(ColumnSums[0]));
        vst1q_s32(&ColumnSumBuffer[4], vreinterpretq_s32_u32(ColumnSums[1]));
    }
}
 
template<>
MLAS_FORCEINLINE
size_t
MlasGemmQuantKernel<MLAS_GEMM_U8X8_KERNEL_UDOT>(
    const MLAS_GEMM_U8X8_KERNEL_UDOT::PackedAType* A,
    const MLAS_GEMM_U8X8_KERNEL_UDOT::PackedBType* B,
    int32_t* C,
    size_t PackedCountK,
    size_t CountM,
    size_t CountN,
    size_t ldc,
    const int32_t* RowSumBuffer,
    const int32_t* ColumnSumBuffer,
    const int32_t* ZeroPointB,
    bool ZeroMode
    )
{
    return MlasGemmU8X8KernelUdot(A, B, C, PackedCountK, CountM, CountN, ldc,
        RowSumBuffer, ColumnSumBuffer, ZeroPointB, ZeroMode);
}
 
const MLAS_GEMM_QUANT_DISPATCH MlasGemmU8X8DispatchUdot = {
    MlasGemmQuantOperation<MLAS_GEMM_U8X8_KERNEL_UDOT>,
    MlasGemmQuantPackedOperation<MLAS_GEMM_U8X8_KERNEL_UDOT>,
    MlasGemmQuantCopyPackB<MLAS_GEMM_U8X8_KERNEL_UDOT>,
    MLAS_GEMM_U8X8_KERNEL_UDOT::PackedK,
    MLAS_GEMM_U8X8_KERNEL_UDOT::PackedStrides.K,
    8
};