File: transpose.cpp

package info (click to toggle)
onnxruntime 1.21.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 333,864 kB
  • sloc: cpp: 3,153,080; python: 179,219; ansic: 109,131; asm: 37,791; cs: 34,424; perl: 13,070; java: 11,047; javascript: 6,330; pascal: 4,126; sh: 3,277; xml: 598; objc: 281; makefile: 63
file content (928 lines) | stat: -rw-r--r-- 29,928 bytes parent folder | download
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
/*++

Copyright (c) Microsoft Corporation. All rights reserved.

Licensed under the MIT License.

Module Name:

    transpose.cpp

Abstract:

    This module implements the transpose operation.

--*/

#include "mlasi.h"

#if defined(MLAS_SSE2_INTRINSICS)

MLAS_FORCEINLINE
void
MlasTranspose4x4Block(
    const uint32_t* Input,
    size_t InputStride,
    uint32_t* Output,
    size_t OutputStride
    )
{
    __m128i a0 = _mm_loadu_si128((const __m128i*)&Input[InputStride * 0]);
    __m128i a1 = _mm_loadu_si128((const __m128i*)&Input[InputStride * 1]);
    __m128i a2 = _mm_loadu_si128((const __m128i*)&Input[InputStride * 2]);
    __m128i a3 = _mm_loadu_si128((const __m128i*)&Input[InputStride * 3]);

    __m128i b0 = _mm_unpacklo_epi32(a0, a2);
    __m128i b1 = _mm_unpackhi_epi32(a0, a2);
    __m128i b2 = _mm_unpacklo_epi32(a1, a3);
    __m128i b3 = _mm_unpackhi_epi32(a1, a3);

    __m128i c0 = _mm_unpacklo_epi32(b0, b2);
    __m128i c1 = _mm_unpackhi_epi32(b0, b2);
    __m128i c2 = _mm_unpacklo_epi32(b1, b3);
    __m128i c3 = _mm_unpackhi_epi32(b1, b3);

    _mm_storeu_si128((__m128i*)&Output[OutputStride * 0], c0);
    _mm_storeu_si128((__m128i*)&Output[OutputStride * 1], c1);
    _mm_storeu_si128((__m128i*)&Output[OutputStride * 2], c2);
    _mm_storeu_si128((__m128i*)&Output[OutputStride * 3], c3);
}

MLAS_FORCEINLINE
void
MlasTranspose4x4Block(
    const uint16_t* Input,
    size_t InputStride,
    uint16_t* Output,
    size_t OutputStride
    )
{
    __m128i a0 = _mm_loadl_epi64((const __m128i*)&Input[InputStride * 0]);
    __m128i a1 = _mm_loadl_epi64((const __m128i*)&Input[InputStride * 1]);
    __m128i a2 = _mm_loadl_epi64((const __m128i*)&Input[InputStride * 2]);
    __m128i a3 = _mm_loadl_epi64((const __m128i*)&Input[InputStride * 3]);

    __m128i b0 = _mm_unpacklo_epi16(a0, a2);
    __m128i b1 = _mm_unpacklo_epi16(a1, a3);

    __m128i c0 = _mm_unpacklo_epi16(b0, b1);
    __m128i c1 = _mm_unpackhi_epi16(b0, b1);

    _mm_storel_pi((__m64*)&Output[OutputStride * 0], _mm_castsi128_ps(c0));
    _mm_storeh_pi((__m64*)&Output[OutputStride * 1], _mm_castsi128_ps(c0));
    _mm_storel_pi((__m64*)&Output[OutputStride * 2], _mm_castsi128_ps(c1));
    _mm_storeh_pi((__m64*)&Output[OutputStride * 3], _mm_castsi128_ps(c1));
}

MLAS_FORCEINLINE
void
MlasTranspose8x8Block(
    const uint8_t* Input,
    size_t InputStride,
    uint8_t* Output,
    size_t OutputStride
    )
{
    __m128i a0 = _mm_loadl_epi64((const __m128i*)&Input[InputStride * 0]);
    __m128i a1 = _mm_loadl_epi64((const __m128i*)&Input[InputStride * 1]);
    __m128i b0 = _mm_unpacklo_epi8(a0, a1);

    __m128i a2 = _mm_loadl_epi64((const __m128i*)&Input[InputStride * 2]);
    __m128i a3 = _mm_loadl_epi64((const __m128i*)&Input[InputStride * 3]);
    __m128i b1 = _mm_unpacklo_epi8(a2, a3);

    __m128i a4 = _mm_loadl_epi64((const __m128i*)&Input[InputStride * 4]);
    __m128i a5 = _mm_loadl_epi64((const __m128i*)&Input[InputStride * 5]);
    __m128i b2 = _mm_unpacklo_epi8(a4, a5);

    __m128i a6 = _mm_loadl_epi64((const __m128i*)&Input[InputStride * 6]);
    __m128i a7 = _mm_loadl_epi64((const __m128i*)&Input[InputStride * 7]);
    __m128i b3 = _mm_unpacklo_epi8(a6, a7);

    __m128i c0 = _mm_unpacklo_epi16(b0, b1);
    __m128i c1 = _mm_unpackhi_epi16(b0, b1);
    __m128i c2 = _mm_unpacklo_epi16(b2, b3);
    __m128i c3 = _mm_unpackhi_epi16(b2, b3);

    __m128 d0 = _mm_castsi128_ps(_mm_unpacklo_epi32(c0, c2));
    _mm_storel_pi((__m64*)&Output[OutputStride * 0], d0);
    _mm_storeh_pi((__m64*)&Output[OutputStride * 1], d0);

    __m128 d1 = _mm_castsi128_ps(_mm_unpackhi_epi32(c0, c2));
    _mm_storel_pi((__m64*)&Output[OutputStride * 2], d1);
    _mm_storeh_pi((__m64*)&Output[OutputStride * 3], d1);

    __m128 d2 = _mm_castsi128_ps(_mm_unpacklo_epi32(c1, c3));
    _mm_storel_pi((__m64*)&Output[OutputStride * 4], d2);
    _mm_storeh_pi((__m64*)&Output[OutputStride * 5], d2);

    __m128 d3 = _mm_castsi128_ps(_mm_unpackhi_epi32(c1, c3));
    _mm_storel_pi((__m64*)&Output[OutputStride * 6], d3);
    _mm_storeh_pi((__m64*)&Output[OutputStride * 7], d3);
}

#elif defined(MLAS_NEON_INTRINSICS)

MLAS_FORCEINLINE
void
MlasTranspose4x4Block(
    const uint32_t* Input,
    size_t InputStride,
    uint32_t* Output,
    size_t OutputStride
    )
{
    uint32x4_t a0 = vld1q_u32(&Input[InputStride * 0]);
    uint32x4_t a1 = vld1q_u32(&Input[InputStride * 1]);
    uint32x4_t a2 = vld1q_u32(&Input[InputStride * 2]);
    uint32x4_t a3 = vld1q_u32(&Input[InputStride * 3]);

    uint32x4x2_t b0 = vzipq_u32(a0, a2);
    uint32x4x2_t b1 = vzipq_u32(a1, a3);

    uint32x4x2_t c0 = vzipq_u32(b0.val[0], b1.val[0]);
    uint32x4x2_t c1 = vzipq_u32(b0.val[1], b1.val[1]);

    vst1q_u32(&Output[OutputStride * 0], c0.val[0]);
    vst1q_u32(&Output[OutputStride * 1], c0.val[1]);
    vst1q_u32(&Output[OutputStride * 2], c1.val[0]);
    vst1q_u32(&Output[OutputStride * 3], c1.val[1]);
}

MLAS_FORCEINLINE
void
MlasTranspose4x4Block(
    const uint16_t* Input,
    size_t InputStride,
    uint16_t* Output,
    size_t OutputStride
    )
{
    uint16x4_t a0 = vld1_u16(&Input[InputStride * 0]);
    uint16x4_t a1 = vld1_u16(&Input[InputStride * 1]);
    uint16x4_t a2 = vld1_u16(&Input[InputStride * 2]);
    uint16x4_t a3 = vld1_u16(&Input[InputStride * 3]);

    uint16x4x2_t b0 = vzip_u16(a0, a2);
    uint16x4x2_t b1 = vzip_u16(a1, a3);

    uint16x4x2_t c0 = vzip_u16(b0.val[0], b1.val[0]);
    uint16x4x2_t c1 = vzip_u16(b0.val[1], b1.val[1]);

    vst1_u16(&Output[OutputStride * 0], c0.val[0]);
    vst1_u16(&Output[OutputStride * 1], c0.val[1]);
    vst1_u16(&Output[OutputStride * 2], c1.val[0]);
    vst1_u16(&Output[OutputStride * 3], c1.val[1]);
}

MLAS_FORCEINLINE
void
MlasTranspose8x8Block(
    const uint8_t* Input,
    size_t InputStride,
    uint8_t* Output,
    size_t OutputStride
    )
{
    uint8x8_t a0 = vld1_u8(&Input[InputStride * 0]);
    uint8x8_t a1 = vld1_u8(&Input[InputStride * 1]);
    uint8x8x2_t b0 = vzip_u8(a0, a1);

    uint8x8_t a2 = vld1_u8(&Input[InputStride * 2]);
    uint8x8_t a3 = vld1_u8(&Input[InputStride * 3]);
    uint8x8x2_t b1 = vzip_u8(a2, a3);

    uint8x8_t a4 = vld1_u8(&Input[InputStride * 4]);
    uint8x8_t a5 = vld1_u8(&Input[InputStride * 5]);
    uint8x8x2_t b2 = vzip_u8(a4, a5);

    uint8x8_t a6 = vld1_u8(&Input[InputStride * 6]);
    uint8x8_t a7 = vld1_u8(&Input[InputStride * 7]);
    uint8x8x2_t b3 = vzip_u8(a6, a7);

    uint16x4x2_t c0 = vzip_u16(vreinterpret_u16_u8(b0.val[0]), vreinterpret_u16_u8(b1.val[0]));
    uint16x4x2_t c1 = vzip_u16(vreinterpret_u16_u8(b0.val[1]), vreinterpret_u16_u8(b1.val[1]));
    uint16x4x2_t c2 = vzip_u16(vreinterpret_u16_u8(b2.val[0]), vreinterpret_u16_u8(b3.val[0]));
    uint16x4x2_t c3 = vzip_u16(vreinterpret_u16_u8(b2.val[1]), vreinterpret_u16_u8(b3.val[1]));

    uint32x2x2_t d0 = vzip_u32(vreinterpret_u32_u16(c0.val[0]), vreinterpret_u32_u16(c2.val[0]));
    uint32x2x2_t d1 = vzip_u32(vreinterpret_u32_u16(c0.val[1]), vreinterpret_u32_u16(c2.val[1]));
    uint32x2x2_t d2 = vzip_u32(vreinterpret_u32_u16(c1.val[0]), vreinterpret_u32_u16(c3.val[0]));
    uint32x2x2_t d3 = vzip_u32(vreinterpret_u32_u16(c1.val[1]), vreinterpret_u32_u16(c3.val[1]));

    vst1_u8(&Output[OutputStride * 0], vreinterpret_u8_u32(d0.val[0]));
    vst1_u8(&Output[OutputStride * 1], vreinterpret_u8_u32(d0.val[1]));
    vst1_u8(&Output[OutputStride * 2], vreinterpret_u8_u32(d1.val[0]));
    vst1_u8(&Output[OutputStride * 3], vreinterpret_u8_u32(d1.val[1]));
    vst1_u8(&Output[OutputStride * 4], vreinterpret_u8_u32(d2.val[0]));
    vst1_u8(&Output[OutputStride * 5], vreinterpret_u8_u32(d2.val[1]));
    vst1_u8(&Output[OutputStride * 6], vreinterpret_u8_u32(d3.val[0]));
    vst1_u8(&Output[OutputStride * 7], vreinterpret_u8_u32(d3.val[1]));
}

#elif defined(MLAS_TARGET_POWER)

MLAS_FORCEINLINE
void
MlasTranspose4x4Block(
    const uint32_t* Input,
    size_t InputStride,
    uint32_t* Output,
    size_t OutputStride
    )
{
    __vector unsigned int a0 = vec_vsx_ld(0, Input);
    __vector unsigned int a1 = vec_vsx_ld(0, &Input[InputStride]);
    __vector unsigned int a2 = vec_vsx_ld(0, &Input[InputStride * 2]);
    __vector unsigned int a3 = vec_vsx_ld(0, &Input[InputStride * 3]);

    __vector unsigned int b0 = vec_mergeh(a0, a1);
    __vector unsigned int b1 = vec_mergeh(a2, a3);
    __vector unsigned int b2 = vec_mergel(a0, a1);
    __vector unsigned int b3 = vec_mergel(a2, a3);

    __vector unsigned int c0 = vec_xxpermdi(b0, b1, 0);
    __vector unsigned int c1 = vec_xxpermdi(b0, b1, 3);
    __vector unsigned int c2 = vec_xxpermdi(b2, b3, 0);
    __vector unsigned int c3 = vec_xxpermdi(b2, b3, 3);

    vec_vsx_st(c0, 0, Output);
    vec_vsx_st(c1, 0, &Output[OutputStride]);
    vec_vsx_st(c2, 0, &Output[OutputStride * 2]);
    vec_vsx_st(c3, 0, &Output[OutputStride * 3]);
}

MLAS_FORCEINLINE
void
MlasTranspose16x16Block(
    const uint8_t* Input,
    size_t InputStride,
    uint8_t* Output,
    size_t OutputStride
    )
{
    __vector unsigned char a0 = vec_vsx_ld(0, Input);
    __vector unsigned char a1 = vec_vsx_ld(0, &Input[InputStride]);
    __vector unsigned char a2 = vec_vsx_ld(0, &Input[InputStride * 2]);
    __vector unsigned char a3 = vec_vsx_ld(0, &Input[InputStride * 3]);
    __vector unsigned char a4 = vec_vsx_ld(0, &Input[InputStride * 4]);
    __vector unsigned char a5 = vec_vsx_ld(0, &Input[InputStride * 5]);
    __vector unsigned char a6 = vec_vsx_ld(0, &Input[InputStride * 6]);
    __vector unsigned char a7 = vec_vsx_ld(0, &Input[InputStride * 7]);
    __vector unsigned char a8 = vec_vsx_ld(0, &Input[InputStride * 8]);
    __vector unsigned char a9 = vec_vsx_ld(0, &Input[InputStride * 9]);
    __vector unsigned char a10 = vec_vsx_ld(0, &Input[InputStride * 10]);
    __vector unsigned char a11 = vec_vsx_ld(0, &Input[InputStride * 11]);
    __vector unsigned char a12 = vec_vsx_ld(0, &Input[InputStride * 12]);
    __vector unsigned char a13 = vec_vsx_ld(0, &Input[InputStride * 13]);
    __vector unsigned char a14 = vec_vsx_ld(0, &Input[InputStride * 14]);
    __vector unsigned char a15 = vec_vsx_ld(0, &Input[InputStride * 15]);

    __vector unsigned char b0 = vec_mergeh(a0, a1);
    __vector unsigned char b1 = vec_mergeh(a2, a3);
    __vector unsigned char b2 = vec_mergeh(a4, a5);
    __vector unsigned char b3 = vec_mergeh(a6, a7);
    __vector unsigned char b4 = vec_mergeh(a8, a9);
    __vector unsigned char b5 = vec_mergeh(a10, a11);
    __vector unsigned char b6 = vec_mergeh(a12, a13);
    __vector unsigned char b7 = vec_mergeh(a14, a15);
    __vector unsigned char c0 = reinterpret_cast<__vector unsigned char>(vec_mergeh(reinterpret_cast<__vector unsigned short>(b0), reinterpret_cast<__vector unsigned short>(b1)));
    __vector unsigned char c1 = reinterpret_cast<__vector unsigned char>(vec_mergeh(reinterpret_cast<__vector unsigned short>(b2), reinterpret_cast<__vector unsigned short>(b3)));
    __vector unsigned char c2 = reinterpret_cast<__vector unsigned char>(vec_mergeh(reinterpret_cast<__vector unsigned short>(b4), reinterpret_cast<__vector unsigned short>(b5)));
    __vector unsigned char c3 = reinterpret_cast<__vector unsigned char>(vec_mergeh(reinterpret_cast<__vector unsigned short>(b6), reinterpret_cast<__vector unsigned short>(b7)));

    __vector unsigned char d0 = reinterpret_cast<__vector unsigned char>(vec_mergeh(reinterpret_cast<__vector unsigned int>(c0), reinterpret_cast<__vector unsigned int>(c1)));
    __vector unsigned char d1 = reinterpret_cast<__vector unsigned char>(vec_mergeh(reinterpret_cast<__vector unsigned int>(c2), reinterpret_cast<__vector unsigned int>(c3)));
    __vector unsigned char e0 = vec_xxpermdi(d0, d1, 0);
    __vector unsigned char e1 = vec_xxpermdi(d0, d1, 3);
    vec_vsx_st(e0, 0, &Output[0]);
    vec_vsx_st(e1, 0, &Output[OutputStride]);

    d0 = reinterpret_cast<__vector unsigned char>(vec_mergel(reinterpret_cast<__vector unsigned int>(c0), reinterpret_cast<__vector unsigned int>(c1)));
    d1 = reinterpret_cast<__vector unsigned char>(vec_mergel(reinterpret_cast<__vector unsigned int>(c2), reinterpret_cast<__vector unsigned int>(c3)));
    e0 = vec_xxpermdi(d0, d1, 0);
    e1 = vec_xxpermdi(d0, d1, 3);
    vec_vsx_st(e0, 0, &Output[OutputStride * 2]);
    vec_vsx_st(e1, 0, &Output[OutputStride * 3]);

    c0 = reinterpret_cast<__vector unsigned char>(vec_mergel(reinterpret_cast<__vector unsigned short>(b0), reinterpret_cast<__vector unsigned short>(b1)));
    c1 = reinterpret_cast<__vector unsigned char>(vec_mergel(reinterpret_cast<__vector unsigned short>(b2), reinterpret_cast<__vector unsigned short>(b3)));
    c2 = reinterpret_cast<__vector unsigned char>(vec_mergel(reinterpret_cast<__vector unsigned short>(b4), reinterpret_cast<__vector unsigned short>(b5)));
    c3 = reinterpret_cast<__vector unsigned char>(vec_mergel(reinterpret_cast<__vector unsigned short>(b6), reinterpret_cast<__vector unsigned short>(b7)));

    d0 = reinterpret_cast<__vector unsigned char>(vec_mergeh(reinterpret_cast<__vector unsigned int>(c0), reinterpret_cast<__vector unsigned int>(c1)));
    d1 = reinterpret_cast<__vector unsigned char>(vec_mergeh(reinterpret_cast<__vector unsigned int>(c2), reinterpret_cast<__vector unsigned int>(c3)));
    e0 = vec_xxpermdi(d0, d1, 0);
    e1 = vec_xxpermdi(d0, d1, 3);
    vec_vsx_st(e0, 0, &Output[OutputStride * 4]);
    vec_vsx_st(e1, 0, &Output[OutputStride * 5]);

    d0 = reinterpret_cast<__vector unsigned char>(vec_mergel(reinterpret_cast<__vector unsigned int>(c0), reinterpret_cast<__vector unsigned int>(c1)));
    d1 = reinterpret_cast<__vector unsigned char>(vec_mergel(reinterpret_cast<__vector unsigned int>(c2), reinterpret_cast<__vector unsigned int>(c3)));
    e0 = vec_xxpermdi(d0, d1, 0);
    e1 = vec_xxpermdi(d0, d1, 3);
    vec_vsx_st(e0, 0, &Output[OutputStride * 6]);
    vec_vsx_st(e1, 0, &Output[OutputStride * 7]);

    b0 = vec_mergel(a0, a1);
    b1 = vec_mergel(a2, a3);
    b2 = vec_mergel(a4, a5);
    b3 = vec_mergel(a6, a7);
    b4 = vec_mergel(a8, a9);
    b5 = vec_mergel(a10, a11);
    b6 = vec_mergel(a12, a13);
    b7 = vec_mergel(a14, a15);

    c0 = reinterpret_cast<__vector unsigned char>(vec_mergeh(reinterpret_cast<__vector unsigned short>(b0), reinterpret_cast<__vector unsigned short>(b1)));
    c1 = reinterpret_cast<__vector unsigned char>(vec_mergeh(reinterpret_cast<__vector unsigned short>(b2), reinterpret_cast<__vector unsigned short>(b3)));
    c2 = reinterpret_cast<__vector unsigned char>(vec_mergeh(reinterpret_cast<__vector unsigned short>(b4), reinterpret_cast<__vector unsigned short>(b5)));
    c3 = reinterpret_cast<__vector unsigned char>(vec_mergeh(reinterpret_cast<__vector unsigned short>(b6), reinterpret_cast<__vector unsigned short>(b7)));

    d0 = reinterpret_cast<__vector unsigned char>(vec_mergeh(reinterpret_cast<__vector unsigned int>(c0), reinterpret_cast<__vector unsigned int>(c1)));
    d1 = reinterpret_cast<__vector unsigned char>(vec_mergeh(reinterpret_cast<__vector unsigned int>(c2), reinterpret_cast<__vector unsigned int>(c3)));
    e0 = vec_xxpermdi(d0, d1, 0);
    e1 = vec_xxpermdi(d0, d1, 3);
    vec_vsx_st(e0, 0, &Output[OutputStride * 8]);
    vec_vsx_st(e1, 0, &Output[OutputStride * 9]);

    d0 = reinterpret_cast<__vector unsigned char>(vec_mergel(reinterpret_cast<__vector unsigned int>(c0), reinterpret_cast<__vector unsigned int>(c1)));
    d1 = reinterpret_cast<__vector unsigned char>(vec_mergel(reinterpret_cast<__vector unsigned int>(c2), reinterpret_cast<__vector unsigned int>(c3)));
    e0 = vec_xxpermdi(d0, d1, 0);
    e1 = vec_xxpermdi(d0, d1, 3);
    vec_vsx_st(e0, 0, &Output[OutputStride * 10]);
    vec_vsx_st(e1, 0, &Output[OutputStride * 11]);

    c0 = reinterpret_cast<__vector unsigned char>(vec_mergel(reinterpret_cast<__vector unsigned short>(b0), reinterpret_cast<__vector unsigned short>(b1)));
    c1 = reinterpret_cast<__vector unsigned char>(vec_mergel(reinterpret_cast<__vector unsigned short>(b2), reinterpret_cast<__vector unsigned short>(b3)));
    c2 = reinterpret_cast<__vector unsigned char>(vec_mergel(reinterpret_cast<__vector unsigned short>(b4), reinterpret_cast<__vector unsigned short>(b5)));
    c3 = reinterpret_cast<__vector unsigned char>(vec_mergel(reinterpret_cast<__vector unsigned short>(b6), reinterpret_cast<__vector unsigned short>(b7)));

    d0 = reinterpret_cast<__vector unsigned char>(vec_mergeh(reinterpret_cast<__vector unsigned int>(c0), reinterpret_cast<__vector unsigned int>(c1)));
    d1 = reinterpret_cast<__vector unsigned char>(vec_mergeh(reinterpret_cast<__vector unsigned int>(c2), reinterpret_cast<__vector unsigned int>(c3)));
    e0 = vec_xxpermdi(d0, d1, 0);
    e1 = vec_xxpermdi(d0, d1, 3);
    vec_vsx_st(e0, 0, &Output[OutputStride * 12]);
    vec_vsx_st(e1, 0, &Output[OutputStride * 13]);

    d0 = reinterpret_cast<__vector unsigned char>(vec_mergel(reinterpret_cast<__vector unsigned int>(c0), reinterpret_cast<__vector unsigned int>(c1)));
    d1 = reinterpret_cast<__vector unsigned char>(vec_mergel(reinterpret_cast<__vector unsigned int>(c2), reinterpret_cast<__vector unsigned int>(c3)));
    e0 = vec_xxpermdi(d0, d1, 0);
    e1 = vec_xxpermdi(d0, d1, 3);
    vec_vsx_st(e0, 0, &Output[OutputStride * 14]);
    vec_vsx_st(e1, 0, &Output[OutputStride * 15]);
}

#elif defined(MLAS_LSX_INTRINSICS)

MLAS_FORCEINLINE
void
MlasTranspose4x4Block(
    const uint32_t* Input,
    size_t InputStride,
    uint32_t* Output,
    size_t OutputStride
    )
{
    __m128i a0 = __lsx_vld((const __m128i*)&Input[InputStride * 0], 0);
    __m128i a1 = __lsx_vld((const __m128i*)&Input[InputStride * 1], 0);
    __m128i a2 = __lsx_vld((const __m128i*)&Input[InputStride * 2], 0);
    __m128i a3 = __lsx_vld((const __m128i*)&Input[InputStride * 3], 0);

    __m128i b0 = __lsx_vilvl_w(a2, a0);
    __m128i b1 = __lsx_vilvh_w(a2, a0);
    __m128i b2 = __lsx_vilvl_w(a3, a1);
    __m128i b3 = __lsx_vilvh_w(a3, a1);
    __m128i c0 = __lsx_vilvl_w(b2, b0);
    __m128i c1 = __lsx_vilvh_w(b2, b0);
    __m128i c2 = __lsx_vilvl_w(b3, b1);
    __m128i c3 = __lsx_vilvh_w(b3, b1);

    __lsx_vst(c0, (__m128i*)&Output[OutputStride * 0], 0);
    __lsx_vst(c1, (__m128i*)&Output[OutputStride * 1], 0);
    __lsx_vst(c2, (__m128i*)&Output[OutputStride * 2], 0);
    __lsx_vst(c3, (__m128i*)&Output[OutputStride * 3], 0);
}

MLAS_FORCEINLINE
void
MlasTranspose4x4Block(
    const uint16_t* Input,
    size_t InputStride,
    uint16_t* Output,
    size_t OutputStride
    )
{
    __m128i a0 = __lsx_vld((const __m128i*)&Input[InputStride * 0], 0);
    __lsx_vinsgr2vr_d(a0, 0 , 1);
    __m128i a1 = __lsx_vld((const __m128i*)&Input[InputStride * 1], 0);
    __lsx_vinsgr2vr_d(a1, 0 , 1);
    __m128i a2 = __lsx_vld((const __m128i*)&Input[InputStride * 2], 0);
    __lsx_vinsgr2vr_d(a2, 0 , 1);
    __m128i a3 = __lsx_vld((const __m128i*)&Input[InputStride * 3], 0);
    __lsx_vinsgr2vr_d(a3, 0 , 1);

    __m128i b0 = __lsx_vilvl_h(a2, a0);
    __m128i b1 = __lsx_vilvl_h(a3, a1);
    __m128i c0 = __lsx_vilvl_h(b1, b0);
    __m128i c1 = __lsx_vilvh_h(b1, b0);

    __lsx_vst(__lsx_vinsgr2vr_d(__lsx_vld((__m128i *)&Output[OutputStride * 0], 0), __lsx_vpickve2gr_d(c0, 0), 0), (__m128i *)&Output[OutputStride * 0], 0);
    __lsx_vst(__lsx_vinsgr2vr_d(__lsx_vld((__m128i *)&Output[OutputStride * 1], 0), __lsx_vpickve2gr_d(c0, 1), 0), (__m128i *)&Output[OutputStride * 1], 0);
    __lsx_vst(__lsx_vinsgr2vr_d(__lsx_vld((__m128i *)&Output[OutputStride * 2], 0), __lsx_vpickve2gr_d(c1, 0), 0), (__m128i *)&Output[OutputStride * 2], 0);
    __lsx_vst(__lsx_vinsgr2vr_d(__lsx_vld((__m128i *)&Output[OutputStride * 3], 0), __lsx_vpickve2gr_d(c1, 1), 0), (__m128i *)&Output[OutputStride * 3], 0);
}

MLAS_FORCEINLINE
void
MlasTranspose8x8Block(
    const uint8_t* Input,
    size_t InputStride,
    uint8_t* Output,
    size_t OutputStride
    )
{
    __m128i a0 = __lsx_vld((const __m128i*)&Input[InputStride * 0], 0);
    __lsx_vinsgr2vr_d(a0, 0, 1);
    __m128i a1 = __lsx_vld((const __m128i*)&Input[InputStride * 1], 0);
    __lsx_vinsgr2vr_d(a1, 0, 1);
    __m128i b0 = __lsx_vilvl_b(a1, a0);

    __m128i a2 = __lsx_vld((const __m128i*)&Input[InputStride * 2], 0);
    __lsx_vinsgr2vr_d(a2, 0, 1);
    __m128i a3 = __lsx_vld((const __m128i*)&Input[InputStride * 3], 0);
    __lsx_vinsgr2vr_d(a3, 0, 1);
    __m128i b1 = __lsx_vilvl_b(a3, a2);

    __m128i a4 = __lsx_vld((const __m128i*)&Input[InputStride * 4], 0);
    __lsx_vinsgr2vr_d(a4, 0, 1);
    __m128i a5 = __lsx_vld((const __m128i*)&Input[InputStride * 5], 0);
    __lsx_vinsgr2vr_d(a5, 0, 1);
    __m128i b2 = __lsx_vilvl_b(a5, a4);

    __m128i a6 = __lsx_vld((const __m128i*)&Input[InputStride * 6], 0);
    __lsx_vinsgr2vr_d(a6, 0, 1);
    __m128i a7 = __lsx_vld((const __m128i*)&Input[InputStride * 7], 0);
    __lsx_vinsgr2vr_d(a7, 0, 1);
    __m128i b3 = __lsx_vilvl_b(a7, a6);
    __m128i c0 = __lsx_vilvl_h(b1, b0);
    __m128i c1 = __lsx_vilvh_h(b1, b0);
    __m128i c2 = __lsx_vilvl_h(b3, b2);
    __m128i c3 = __lsx_vilvh_h(b3, b2);

    __m128 d0 = (__m128)(__lsx_vilvl_w(c2, c0));
    __lsx_vstelm_d(d0, &Output[OutputStride * 0], 0, 0);
    __lsx_vstelm_d(d0, &Output[OutputStride * 1], 0, 1);

    __m128 d1 = (__m128)(__lsx_vilvh_w(c2, c0));
    __lsx_vstelm_d(d1, &Output[OutputStride * 2], 0, 0);
    __lsx_vstelm_d(d1, &Output[OutputStride * 3], 0, 1);

    __m128 d2 = (__m128)(__lsx_vilvl_w(c3, c1));
    __lsx_vstelm_d(d2, &Output[OutputStride * 4], 0, 0);
    __lsx_vstelm_d(d2, &Output[OutputStride * 5], 0, 1);

    __m128 d3 = (__m128)(__lsx_vilvh_w(c3, c1));
    __lsx_vstelm_d(d3, &Output[OutputStride * 6], 0, 0);
    __lsx_vstelm_d(d3, &Output[OutputStride * 7], 0, 1);
}

#endif

template<typename ElementType>
MLAS_FORCEINLINE
void
MlasTranspose4xNVector(
    const ElementType* Input,
    size_t InputStride,
    ElementType* Output,
    size_t OutputStride
    )
{
    ElementType a0 = Input[InputStride * 0];
    ElementType a1 = Input[InputStride * 1];
    ElementType a2 = Input[InputStride * 2];
    ElementType a3 = Input[InputStride * 3];

    Output[OutputStride * 0] = a0;
    Output[OutputStride * 1] = a1;
    Output[OutputStride * 2] = a2;
    Output[OutputStride * 3] = a3;
}

#if defined(MLAS_TARGET_POWER)
template<typename ElementType>
MLAS_FORCEINLINE
void
MlasTranspose16xNVector(
    const ElementType* Input,
    size_t InputStride,
    ElementType* Output,
    size_t OutputStride
    )
{
    MlasTranspose4xNVector(&Input[InputStride * 0], InputStride, &Output[OutputStride * 0], OutputStride);
    MlasTranspose4xNVector(&Input[InputStride * 4], InputStride, &Output[OutputStride * 4], OutputStride);
    MlasTranspose4xNVector(&Input[InputStride * 8], InputStride, &Output[OutputStride * 8], OutputStride);
    MlasTranspose4xNVector(&Input[InputStride * 12], InputStride, &Output[OutputStride * 12], OutputStride);
}
#endif

template<typename ElementType>
MLAS_FORCEINLINE
void
MlasTranspose8xNVector(
    const ElementType* Input,
    size_t InputStride,
    ElementType* Output,
    size_t OutputStride
    )
{
    MlasTranspose4xNVector(&Input[InputStride * 0], InputStride, &Output[OutputStride * 0], OutputStride);
    MlasTranspose4xNVector(&Input[InputStride * 4], InputStride, &Output[OutputStride * 4], OutputStride);
}

void
MLASCALL
MlasTranspose(
    const uint32_t* Input,
    uint32_t* Output,
    size_t M,
    size_t N
    )
/*++

Routine Description:

    This routine transposes the input matrix (M rows by N columns) to the
    output matrix (N rows by M columns).

Arguments:

    Input - Supplies the input buffer.

    Output - Supplies the output buffer.

    M - Supplies the number of rows for the input matrix and the number of
        columns for the output matrix.

    N - Supplies the number of columns for the input matrix and the number of
        rows for the output matrix.

Return Value:

    None.

--*/
{
    size_t n = N;

    //
    // Transpose elements from the input matrix to the output matrix 4 columns
    // at a time.
    //

    while (n >= 4) {

        const uint32_t* s = Input;
        uint32_t* d = Output;
        size_t m = M;

#if defined(MLAS_SSE2_INTRINSICS) || defined(MLAS_NEON_INTRINSICS) || defined(MLAS_TARGET_POWER) || \
    defined(MLAS_LSX_INTRINSICS)

        while (m >= 4) {

            MlasTranspose4x4Block(s, N, d, M);

            s += N * 4;
            d += 4;
            m -= 4;
        }

#endif

        while (m > 0) {

            MlasTranspose4xNVector(s, 1, d, M);

            s += N;
            d += 1;
            m -= 1;
        }

        Input += 4;
        Output += M * 4;
        n -= 4;
    }

    //
    // Transpose elements from the input matrix to the output matrix for the
    // remaining columns.
    //

    while (n > 0) {

        const uint32_t* s = Input;
        uint32_t* d = Output;
        size_t m = M;

        while (m >= 4) {

            MlasTranspose4xNVector(s, N, d, 1);

            s += N * 4;
            d += 4;
            m -= 4;
        }

        while (m > 0) {

            d[0] = s[0];

            s += N;
            d += 1;
            m -= 1;
        }

        Input += 1;
        Output += M;
        n -= 1;
    }
}

void
MLASCALL
MlasTranspose(
    const float* Input,
    float* Output,
    size_t M,
    size_t N
    )
{
    MlasTranspose(
        reinterpret_cast<const uint32_t*>(Input),
        reinterpret_cast<uint32_t*>(Output),
        M,
        N);
}


void
MLASCALL
MlasTranspose(
    const uint16_t* Input,
    uint16_t* Output,
    size_t M,
    size_t N
    )
/*++

Routine Description:

    This routine transposes the input matrix (M rows by N columns) to the
    output matrix (N rows by M columns).

Arguments:

    Input - Supplies the input buffer.

    Output - Supplies the output buffer.

    M - Supplies the number of rows for the input matrix and the number of
        columns for the output matrix.

    N - Supplies the number of columns for the input matrix and the number of
        rows for the output matrix.

Return Value:

    None.

--*/
{
    size_t n = N;

    //
    // Transpose elements from the input matrix to the output matrix 4 columns
    // at a time.
    //

    while (n >= 4) {

        const uint16_t* s = Input;
        uint16_t* d = Output;
        size_t m = M;

#if defined(MLAS_SSE2_INTRINSICS) || defined(MLAS_NEON_INTRINSICS)  || defined(MLAS_LSX_INTRINSICS)

        while (m >= 4) {

            MlasTranspose4x4Block(s, N, d, M);

            s += N * 4;
            d += 4;
            m -= 4;
        }

#endif

        while (m > 0) {

            MlasTranspose4xNVector(s, 1, d, M);

            s += N;
            d += 1;
            m -= 1;
        }

        Input += 4;
        Output += M * 4;
        n -= 4;
    }

    //
    // Transpose elements from the input matrix to the output matrix for the
    // remaining columns.
    //

    while (n > 0) {

        const uint16_t* s = Input;
        uint16_t* d = Output;
        size_t m = M;

        while (m >= 4) {

            MlasTranspose4xNVector(s, N, d, 1);

            s += N * 4;
            d += 4;
            m -= 4;
        }

        while (m > 0) {

            d[0] = s[0];

            s += N;
            d += 1;
            m -= 1;
        }

        Input += 1;
        Output += M;
        n -= 1;
    }
}


void
MLASCALL
MlasTranspose(
    const uint8_t* Input,
    uint8_t* Output,
    size_t M,
    size_t N
    )
/*++

Routine Description:

    This routine transposes the input matrix (M rows by N columns) to the
    output matrix (N rows by M columns).

Arguments:

    Input - Supplies the input buffer.

    Output - Supplies the output buffer.

    M - Supplies the number of rows for the input matrix and the number of
        columns for the output matrix.

    N - Supplies the number of columns for the input matrix and the number of
        rows for the output matrix.

Return Value:

    None.

--*/
{
    size_t n = N;

    //
    // Transpose elements from the input matrix to the output matrix 8 columns
    // at a time.
    //
#if defined(MLAS_TARGET_POWER)
    while (n >= 16) {

        const uint8_t* s = Input;
        uint8_t* d = Output;
        size_t m = M;
        while (m >= 16) {

            MlasTranspose16x16Block(s, N, d, M);

            s += N * 16;
            d += 16;
            m -= 16;
        }

        while (m > 0) {

            MlasTranspose16xNVector(s, 1, d, M);

            s += N;
            d += 1;
            m -= 1;
        }

        Input += 16;
        Output += M * 16;
        n -= 16;
    }
#endif
    while (n >= 8) {

        const uint8_t* s = Input;
        uint8_t* d = Output;
        size_t m = M;

#if defined(MLAS_SSE2_INTRINSICS) || defined(MLAS_NEON_INTRINSICS)  || defined(MLAS_LSX_INTRINSICS)

        while (m >= 8) {

            MlasTranspose8x8Block(s, N, d, M);

            s += N * 8;
            d += 8;
            m -= 8;
        }

#endif

        while (m > 0) {

            MlasTranspose8xNVector(s, 1, d, M);

            s += N;
            d += 1;
            m -= 1;
        }

        Input += 8;
        Output += M * 8;
        n -= 8;
    }

    //
    // Transpose elements from the input matrix to the output matrix for the
    // remaining columns.
    //

    while (n > 0) {

        const uint8_t* s = Input;
        uint8_t* d = Output;
        size_t m = M;

        while (m >= 8) {

            MlasTranspose8xNVector(s, N, d, 1);

            s += N * 8;
            d += 8;
            m -= 8;
        }

        while (m > 0) {

            d[0] = s[0];

            s += N;
            d += 1;
            m -= 1;
        }

        Input += 1;
        Output += M;
        n -= 1;
    }
}

void
MLASCALL
MlasTranspose(
    const int8_t* Input,
    int8_t* Output,
    size_t M,
    size_t N)
{
    MlasTranspose(
        reinterpret_cast<const uint8_t*>(Input),
        reinterpret_cast<uint8_t*>(Output),
        M,
        N);
}