File size: 33,773 Bytes
be903e2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// Tencent is pleased to support the open source community by making ncnn available.
//
// Copyright (C) 2022 THL A29 Limited, a Tencent company. All rights reserved.
//
// Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
// in compliance with the License. You may obtain a copy of the License at
//
// https://opensource.org/licenses/BSD-3-Clause
//
// Unless required by applicable law or agreed to in writing, software distributed
// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
// CONDITIONS OF ANY KIND, either express or implied. See the License for the
// specific language governing permissions and limitations under the License.

#if NCNN_RUNTIME_CPU && NCNN_MMI && !__mips_msa && !__mips_loongson_mmi
void convolution_winograd_dot_int8_loongson_mmi(Mat& bottom_blob_tm, int outch, const Mat& kernel_tm, Mat& top_blob_tm, const Option& opt);
#endif

static void convolution_winograd_dot_int8_msa(Mat& bottom_blob_tm, int outch, const Mat& kernel_tm, Mat& top_blob_tm, const Option& opt)
{
#if NCNN_RUNTIME_CPU && NCNN_MMI && !__mips_msa && !__mips_loongson_mmi
    if (ncnn::cpu_support_loongson_mmi())
    {
        convolution_winograd_dot_int8_loongson_mmi(bottom_blob_tm, outch, kernel_tm, top_blob_tm, opt);
        return;
    }
#endif

    // Mat bottom_blob_tm(tiles, 16/36/64, inch, 2u, 1, opt.workspace_allocator);

    const int tiles = bottom_blob_tm.w;
    const int batch = bottom_blob_tm.h;
    const int inch = bottom_blob_tm.c;

    // permute
    Mat bottom_blob_tm2;
#if __mips_msa || __mips_loongson_mmi
    if (inch >= 4)
    {
        if (tiles >= 2)
            bottom_blob_tm2.create(inch / 4 + inch % 4, tiles / 2 + tiles % 2, batch, 16u, 8, opt.workspace_allocator);
        else // if (tiles >= 1)
            bottom_blob_tm2.create(inch / 4 + inch % 4, tiles, batch, 8u, 4, opt.workspace_allocator);
    }
    else
#endif // __mips_msa || __mips_loongson_mmi
    {
        if (tiles >= 2)
            bottom_blob_tm2.create(inch, tiles / 2 + tiles % 2, batch, 4u, 2, opt.workspace_allocator);
        else // if (tiles >= 1)
            bottom_blob_tm2.create(inch, tiles, batch, 2u, 1, opt.workspace_allocator);
    }

    #pragma omp parallel for num_threads(opt.num_threads)
    for (int r = 0; r < batch; r++)
    {
        Mat tm2 = bottom_blob_tm2.channel(r);

        // tile
        int i = 0;
        for (; i + 1 < tiles; i += 2)
        {
            short* tmpptr = tm2.row<short>(i / 2);

            const short* r0 = (const short*)bottom_blob_tm + r * tiles + i;

            int q = 0;
#if __mips_msa || __mips_loongson_mmi
            const short* r1 = (const short*)bottom_blob_tm.channel(1) + r * tiles + i;
            const short* r2 = (const short*)bottom_blob_tm.channel(2) + r * tiles + i;
            const short* r3 = (const short*)bottom_blob_tm.channel(3) + r * tiles + i;
            for (; q + 3 < inch; q += 4)
            {
#if __mips_loongson_mmi
                tmpptr[0] = r0[0];
                tmpptr[1] = r1[0];
                tmpptr[2] = r0[1];
                tmpptr[3] = r1[1];
                tmpptr[4] = r2[0];
                tmpptr[5] = r3[0];
                tmpptr[6] = r2[1];
                tmpptr[7] = r3[1];
#else  // __mips_loongson_mmi
                tmpptr[0] = r0[0];
                tmpptr[1] = r1[0];
                tmpptr[2] = r2[0];
                tmpptr[3] = r3[0];
                tmpptr[4] = r0[1];
                tmpptr[5] = r1[1];
                tmpptr[6] = r2[1];
                tmpptr[7] = r3[1];
#endif // __mips_loongson_mmi
                r0 += bottom_blob_tm.cstep * 4;
                r1 += bottom_blob_tm.cstep * 4;
                r2 += bottom_blob_tm.cstep * 4;
                r3 += bottom_blob_tm.cstep * 4;
                tmpptr += 8;
            }
#endif // __mips_msa || __mips_loongson_mmi
            for (; q < inch; q++)
            {
                tmpptr[0] = r0[0];
                tmpptr[1] = r0[1];
                r0 += bottom_blob_tm.cstep;
                tmpptr += 2;
            }
        }
        for (; i < tiles; i++)
        {
            short* tmpptr = tm2.row<short>(i / 2 + i % 2);

            const short* r0 = (const short*)bottom_blob_tm + r * tiles + i;

            int q = 0;
#if __mips_msa || __mips_loongson_mmi
            const short* r1 = (const short*)bottom_blob_tm.channel(1) + r * tiles + i;
            const short* r2 = (const short*)bottom_blob_tm.channel(2) + r * tiles + i;
            const short* r3 = (const short*)bottom_blob_tm.channel(3) + r * tiles + i;
            for (; q + 3 < inch; q += 4)
            {
                tmpptr[0] = r0[0];
                tmpptr[1] = r1[0];
                tmpptr[2] = r2[0];
                tmpptr[3] = r3[0];
                r0 += bottom_blob_tm.cstep * 4;
                r1 += bottom_blob_tm.cstep * 4;
                r2 += bottom_blob_tm.cstep * 4;
                r3 += bottom_blob_tm.cstep * 4;
                tmpptr += 4;
            }
#endif // __mips_msa || __mips_loongson_mmi
            for (; q < inch; q++)
            {
                tmpptr[0] = r0[0];
                r0 += bottom_blob_tm.cstep;
                tmpptr += 1;
            }
        }
    }

    bottom_blob_tm = Mat();
    // permute end

    top_blob_tm.create(tiles, batch, outch, 4u, 1, opt.workspace_allocator);

#if __mips_msa
    int nn_outch = outch >> 2;
    int remain_outch_start = nn_outch << 2;

    #pragma omp parallel for num_threads(opt.num_threads)
    for (int pp = 0; pp < nn_outch; pp++)
    {
        int p = pp * 4;

        int* output0_tm = top_blob_tm.channel(p);
        int* output1_tm = top_blob_tm.channel(p + 1);
        int* output2_tm = top_blob_tm.channel(p + 2);
        int* output3_tm = top_blob_tm.channel(p + 3);

        const Mat kernel0_tm = kernel_tm.channel(p / 4);

        for (int r = 0; r < batch; r++)
        {
            const Mat bb2 = bottom_blob_tm2.channel(r);

            int i = 0;
            for (; i + 1 < tiles; i += 2)
            {
                const short* r0 = bb2.row<const short>(i / 2);
                const short* k0 = kernel0_tm.row<const short>(r);

                int nn4 = inch / 4;
                int nn1 = inch % 4;

                v4i32 _sum00 = __msa_fill_w(0);
                v4i32 _sum10 = __msa_fill_w(0);

                if (nn4 > 0)
                {
                    v4i32 _sum01 = __msa_fill_w(0);
                    v4i32 _sum02 = __msa_fill_w(0);
                    v4i32 _sum03 = __msa_fill_w(0);
                    v4i32 _sum11 = __msa_fill_w(0);
                    v4i32 _sum12 = __msa_fill_w(0);
                    v4i32 _sum13 = __msa_fill_w(0);

                    int j = 0;
                    for (; j < nn4; j++)
                    {
                        v8i16 _val01 = __msa_ld_h(r0, 0);

                        v8i16 _val0 = (v8i16)__msa_ilvr_d((v2i64)_val01, (v2i64)_val01);
                        v8i16 _val1 = (v8i16)__msa_ilvl_d((v2i64)_val01, (v2i64)_val01);

                        v8i16 _w0 = __msa_ld_h(k0, 0);
                        v8i16 _w1 = __msa_ld_h(k0 + 8, 0);

                        v8i16 _extval0 = __msa_clti_s_h(_val0, 0);
                        v8i16 _extval1 = __msa_clti_s_h(_val1, 0);
                        v8i16 _extw0 = __msa_clti_s_h(_w0, 0);
                        v8i16 _extw1 = __msa_clti_s_h(_w1, 0);

                        v4i32 _val0l = (v4i32)__msa_ilvr_h(_extval0, _val0);
                        v4i32 _val0h = (v4i32)__msa_ilvl_h(_extval0, _val0);
                        v4i32 _val1l = (v4i32)__msa_ilvr_h(_extval1, _val1);
                        v4i32 _val1h = (v4i32)__msa_ilvl_h(_extval1, _val1);

                        v4i32 _w0l = (v4i32)__msa_ilvr_h(_extw0, _w0);
                        v4i32 _w0h = (v4i32)__msa_ilvl_h(_extw0, _w0);
                        v4i32 _w1l = (v4i32)__msa_ilvr_h(_extw1, _w1);
                        v4i32 _w1h = (v4i32)__msa_ilvl_h(_extw1, _w1);

                        _sum00 = __msa_maddv_w(_sum00, _val0l, _w0l);
                        _sum01 = __msa_maddv_w(_sum01, _val0h, _w0h);
                        _sum02 = __msa_maddv_w(_sum02, _val0l, _w1l);
                        _sum03 = __msa_maddv_w(_sum03, _val0h, _w1h);
                        _sum10 = __msa_maddv_w(_sum10, _val1l, _w0l);
                        _sum11 = __msa_maddv_w(_sum11, _val1h, _w0h);
                        _sum12 = __msa_maddv_w(_sum12, _val1l, _w1l);
                        _sum13 = __msa_maddv_w(_sum13, _val1h, _w1h);

                        r0 += 8;
                        k0 += 16;
                    }

                    // transpose 4x4
                    {
                        v4i32 _tmp0, _tmp1, _tmp2, _tmp3;
                        _tmp0 = __msa_ilvr_w(_sum01, _sum00);
                        _tmp1 = __msa_ilvr_w(_sum03, _sum02);
                        _tmp2 = __msa_ilvl_w(_sum01, _sum00);
                        _tmp3 = __msa_ilvl_w(_sum03, _sum02);
                        _sum00 = (v4i32)__msa_ilvr_d((v2i64)_tmp1, (v2i64)_tmp0);
                        _sum01 = (v4i32)__msa_ilvl_d((v2i64)_tmp1, (v2i64)_tmp0);
                        _sum02 = (v4i32)__msa_ilvr_d((v2i64)_tmp3, (v2i64)_tmp2);
                        _sum03 = (v4i32)__msa_ilvl_d((v2i64)_tmp3, (v2i64)_tmp2);
                    }
                    {
                        v4i32 _tmp0, _tmp1, _tmp2, _tmp3;
                        _tmp0 = __msa_ilvr_w(_sum11, _sum10);
                        _tmp1 = __msa_ilvr_w(_sum13, _sum12);
                        _tmp2 = __msa_ilvl_w(_sum11, _sum10);
                        _tmp3 = __msa_ilvl_w(_sum13, _sum12);
                        _sum10 = (v4i32)__msa_ilvr_d((v2i64)_tmp1, (v2i64)_tmp0);
                        _sum11 = (v4i32)__msa_ilvl_d((v2i64)_tmp1, (v2i64)_tmp0);
                        _sum12 = (v4i32)__msa_ilvr_d((v2i64)_tmp3, (v2i64)_tmp2);
                        _sum13 = (v4i32)__msa_ilvl_d((v2i64)_tmp3, (v2i64)_tmp2);
                    }

                    _sum00 = __msa_addv_w(_sum00, _sum01);
                    _sum02 = __msa_addv_w(_sum02, _sum03);
                    _sum10 = __msa_addv_w(_sum10, _sum11);
                    _sum12 = __msa_addv_w(_sum12, _sum13);

                    _sum00 = __msa_addv_w(_sum00, _sum02);
                    _sum10 = __msa_addv_w(_sum10, _sum12);
                }

                for (int j = 0; j < nn1; j++)
                {
                    v8i16 _val0 = __msa_fill_h(r0[0]);
                    v8i16 _val1 = __msa_fill_h(r0[1]);
                    v8i16 _val = (v8i16)__msa_ilvr_d((v2i64)_val1, (v2i64)_val0);

                    v8i16 _w16 = __msa_ld_h(k0, 0);

                    _w16 = (v8i16)__msa_ilvr_d((v2i64)_w16, (v2i64)_w16);

                    v8i16 _extval = __msa_clti_s_h(_val, 0);
                    v8i16 _extw16 = __msa_clti_s_h(_w16, 0);

                    v4i32 _vall = (v4i32)__msa_ilvr_h(_extval, _val);
                    v4i32 _valh = (v4i32)__msa_ilvl_h(_extval, _val);
                    v4i32 _w0l = (v4i32)__msa_ilvr_h(_extw16, _w16);
                    v4i32 _w0h = (v4i32)__msa_ilvl_h(_extw16, _w16);

                    _sum00 = __msa_maddv_w(_sum00, _vall, _w0l);
                    _sum10 = __msa_maddv_w(_sum10, _valh, _w0h);

                    r0 += 2;
                    k0 += 4;
                }

                int sum[8];
                __msa_st_w(_sum00, sum, 0);
                __msa_st_w(_sum10, sum + 4, 0);

                output0_tm[0] = sum[0];
                output1_tm[0] = sum[1];
                output2_tm[0] = sum[2];
                output3_tm[0] = sum[3];
                output0_tm[1] = sum[4];
                output1_tm[1] = sum[5];
                output2_tm[1] = sum[6];
                output3_tm[1] = sum[7];
                output0_tm += 2;
                output1_tm += 2;
                output2_tm += 2;
                output3_tm += 2;
            }
            for (; i < tiles; i++)
            {
                const short* r0 = bb2.row<const short>(i / 2 + i % 2);
                const short* k0 = kernel0_tm.row<const short>(r);

                int nn4 = inch / 4;
                int nn1 = inch % 4;

                v4i32 _sum0 = __msa_fill_w(0);

                if (nn4 > 0)
                {
                    v4i32 _sum1 = __msa_fill_w(0);
                    v4i32 _sum2 = __msa_fill_w(0);
                    v4i32 _sum3 = __msa_fill_w(0);

                    int j = 0;
                    for (; j < nn4; j++)
                    {
                        v8i16 _val16 = __msa_ld_h(r0, 0);

                        _val16 = (v8i16)__msa_ilvr_d((v2i64)_val16, (v2i64)_val16);

                        v8i16 _w0 = __msa_ld_h(k0, 0);
                        v8i16 _w1 = __msa_ld_h(k0 + 8, 0);

                        v8i16 _extval16 = __msa_clti_s_h(_val16, 0);
                        v8i16 _extw0 = __msa_clti_s_h(_w0, 0);
                        v8i16 _extw1 = __msa_clti_s_h(_w1, 0);

                        v4i32 _val0l = (v4i32)__msa_ilvr_h(_extval16, _val16);
                        v4i32 _val0h = (v4i32)__msa_ilvl_h(_extval16, _val16);

                        v4i32 _w0l = (v4i32)__msa_ilvr_h(_extw0, _w0);
                        v4i32 _w0h = (v4i32)__msa_ilvl_h(_extw0, _w0);
                        v4i32 _w1l = (v4i32)__msa_ilvr_h(_extw1, _w1);
                        v4i32 _w1h = (v4i32)__msa_ilvl_h(_extw1, _w1);

                        _sum0 = __msa_maddv_w(_sum0, _val0l, _w0l);
                        _sum1 = __msa_maddv_w(_sum1, _val0h, _w0h);
                        _sum2 = __msa_maddv_w(_sum2, _val0l, _w1l);
                        _sum3 = __msa_maddv_w(_sum3, _val0h, _w1h);

                        r0 += 4;
                        k0 += 16;
                    }

                    // transpose 4x4
                    {
                        v4i32 _tmp0, _tmp1, _tmp2, _tmp3;
                        _tmp0 = __msa_ilvr_w(_sum1, _sum0);
                        _tmp1 = __msa_ilvr_w(_sum3, _sum2);
                        _tmp2 = __msa_ilvl_w(_sum1, _sum0);
                        _tmp3 = __msa_ilvl_w(_sum3, _sum2);
                        _sum0 = (v4i32)__msa_ilvr_d((v2i64)_tmp1, (v2i64)_tmp0);
                        _sum1 = (v4i32)__msa_ilvl_d((v2i64)_tmp1, (v2i64)_tmp0);
                        _sum2 = (v4i32)__msa_ilvr_d((v2i64)_tmp3, (v2i64)_tmp2);
                        _sum3 = (v4i32)__msa_ilvl_d((v2i64)_tmp3, (v2i64)_tmp2);
                    }

                    _sum0 = __msa_addv_w(_sum0, _sum1);
                    _sum2 = __msa_addv_w(_sum2, _sum3);
                    _sum0 = __msa_addv_w(_sum0, _sum2);
                }

                for (int j = 0; j < nn1; j++)
                {
                    v4i32 _val = __msa_fill_w(r0[0]);
                    v8i16 _w16 = __msa_ld_h(k0, 0);

                    v8i16 _extw16 = __msa_clti_s_h(_w16, 0);
                    v4i32 _w0l = (v4i32)__msa_ilvr_h(_extw16, _w16);

                    _sum0 = __msa_maddv_w(_sum0, _val, _w0l);

                    r0 += 1;
                    k0 += 4;
                }

                int sum[4];
                __msa_st_w(_sum0, sum, 0);

                output0_tm[0] = sum[0];
                output1_tm[0] = sum[1];
                output2_tm[0] = sum[2];
                output3_tm[0] = sum[3];
                output0_tm += 1;
                output1_tm += 1;
                output2_tm += 1;
                output3_tm += 1;
            }
        }
    }
#else // __mips_msa
    int nn_outch = outch >> 1;
    int remain_outch_start = nn_outch << 1;

    #pragma omp parallel for num_threads(opt.num_threads)
    for (int pp = 0; pp < nn_outch; pp++)
    {
        int p = pp * 2;

        int* output0_tm = top_blob_tm.channel(p);
        int* output1_tm = top_blob_tm.channel(p + 1);

        const Mat kernel0_tm = kernel_tm.channel(p / 2);

        for (int r = 0; r < batch; r++)
        {
            const Mat bb2 = bottom_blob_tm2.channel(r);

            int i = 0;
            for (; i + 1 < tiles; i += 2)
            {
                const short* r0 = bb2.row<const short>(i / 2);
                const short* k0 = kernel0_tm.row<const short>(r);

                int sum00 = 0;
                int sum01 = 0;
                int sum10 = 0;
                int sum11 = 0;

#if __mips_loongson_mmi
                int nn4 = inch / 4;
                int nn1 = inch % 4;

                if (nn4 > 0)
                {
                    int32x2_t _sum0 = __mmi_pzerow_s();
                    int32x2_t _sum1 = __mmi_pzerow_s();

                    double temp0 = 0;
                    double temp1 = 0;
                    double temp2 = 0;
                    double temp3 = 0;
                    double temp4 = 0;
                    double temp5 = 0;

                    uint64_t flag_0x44 = 0x44;
                    uint64_t flag_0xee = 0xee;

                    int j = 0;
                    for (; j < nn4; j++)
                    {
                        asm volatile(
                            "ld         $0, 64(%0)      \n" // __builtin_prefetch(r0 + 32);
                            "ld         $0, 64(%1)      \n" // __builtin_prefetch(k0 + 32);

                            "ldc1       %4, 0(%0)       \n" // int16x4_t _v0 = __mmi_pldh_s(r0);
                            "ldc1       %5, 8(%0)       \n" // int16x4_t _v1 = __mmi_pldh_s(r0 + 4);
                            "ldc1       %6, 0(%1)       \n" // int16x4_t _k0 = __mmi_pldh_s(k0);
                            "ldc1       %7, 8(%1)       \n" // int16x4_t _k1 = __mmi_pldh_s(k0 + 4);

                            "pshufh     %8, %6, %20     \n" // int16x4_t _k0202 = __mmi_pshufh_s(_k0, 0x44);
                            "pshufh     %9, %6, %21     \n" // int16x4_t _k1313 = __mmi_pshufh_s(_k0, 0xee);
                            "pshufh     %6, %7, %20     \n" // int16x4_t _k4646 = __mmi_pshufh_s(_k1, 0x44);
                            "pshufh     %7, %7, %21     \n" // int16x4_t _k5757 = __mmi_pshufh_s(_k1, 0xee);

                            "pmaddhw    %8, %4, %8      \n" // int32x2_t _s0x = __mmi_pmaddhw(_v0, _k0202);
                            "pmaddhw    %9, %4, %9      \n" // int32x2_t _s1x = __mmi_pmaddhw(_v0, _k1313);
                            "pmaddhw    %6, %5, %6      \n" // int32x2_t _s0y = __mmi_pmaddhw(_v1, _k4646);
                            "pmaddhw    %7, %5, %7      \n" // int32x2_t _s1y = __mmi_pmaddhw(_v1, _k5757);

                            "paddw      %2, %2, %8      \n" // _sum0 = __mmi_paddw_s(_sum0, _s0x);
                            "paddw      %3, %3, %9      \n" // _sum1 = __mmi_paddw_s(_sum1, _s1x);
                            "paddw      %2, %2, %6      \n" // _sum0 = __mmi_paddw_s(_sum0, _s0y);
                            "paddw      %3, %3, %7      \n" // _sum1 = __mmi_paddw_s(_sum1, _s1y);

                            : "=r"(r0),    // %0
                            "=r"(k0),    // %1
                            "=f"(_sum0), // %2
                            "=f"(_sum1), // %3
                            "=f"(temp0), // %4
                            "=f"(temp1), // %5
                            "=f"(temp2), // %6
                            "=f"(temp3), // %7
                            "=f"(temp4), // %8
                            "=f"(temp5)  // %9
                            : "0"(r0),
                            "1"(k0),
                            "2"(_sum0),
                            "3"(_sum1),
                            "4"(temp0),
                            "5"(temp1),
                            "6"(temp2),
                            "7"(temp3),
                            "8"(temp4),
                            "9"(temp5),
                            "f"(flag_0x44), // %20
                            "f"(flag_0xee)  // %21
                            : "memory");

                        r0 += 8;
                        k0 += 8;
                    }

                    int sum[4];
                    __mmi_pstw_s(sum, _sum0);
                    __mmi_pstw_s(sum + 2, _sum1);

                    sum00 = sum[0];
                    sum01 = sum[1];
                    sum10 = sum[2];
                    sum11 = sum[3];
                }
#else  // __mips_loongson_mmi
                int nn1 = inch;
#endif // __mips_loongson_mmi

                for (int j = 0; j < nn1; j++)
                {
                    signed short val0 = r0[0];
                    signed short val1 = r0[1];
                    signed short w0 = k0[0];
                    signed short w1 = k0[1];

                    sum00 += val0 * w0;
                    sum01 += val1 * w0;
                    sum10 += val0 * w1;
                    sum11 += val1 * w1;

                    r0 += 2;
                    k0 += 2;
                }

                output0_tm[0] = sum00;
                output0_tm[1] = sum01;
                output1_tm[0] = sum10;
                output1_tm[1] = sum11;
                output0_tm += 2;
                output1_tm += 2;
            }
            for (; i < tiles; i++)
            {
                const short* r0 = bb2.row<const short>(i / 2 + i % 2);
                const short* k0 = kernel0_tm.row<const short>(r);

                int sum0 = 0;
                int sum1 = 0;

#if __mips_loongson_mmi
                int nn4 = inch / 4;
                int nn1 = inch % 4;

                if (nn4 > 0)
                {
                    int32x2_t _sum01 = __mmi_pzerow_s();

                    double temp0 = 0;
                    double temp1 = 0;
                    double temp2 = 0;
                    double temp3 = 0;
                    double temp4 = 0;

                    uint64_t flag_0x44 = 0x44;
                    uint64_t flag_0xee = 0xee;

                    int j = 0;
                    for (; j < nn4; j++)
                    {
                        asm volatile(
                            "ld         $0, 32(%0)      \n" // __builtin_prefetch(r0 + 16);
                            "ld         $0, 64(%1)      \n" // __builtin_prefetch(k0 + 32);

                            "ldc1       %4, 0(%0)       \n" // int16x4_t _v0 = __mmi_pldh_s(r0);
                            "ldc1       %5, 0(%1)       \n" // int16x4_t _k0 = __mmi_pldh_s(k0);
                            "ldc1       %6, 8(%1)       \n" // int16x4_t _k1 = __mmi_pldh_s(k0 + 4);

                            "pshufh     %7, %4, %16     \n" // int16x4_t _v0101 = __mmi_pshufh_s(_v0, 0x44);
                            "pshufh     %4, %4, %17     \n" // int16x4_t _v2323 = __mmi_pshufh_s(_v0, 0xee);

                            "pmaddhw    %5, %7, %5      \n" // int32x2_t _s01x = __mmi_pmaddhw(_v0101, _k0);
                            "pmaddhw    %6, %4, %6      \n" // int32x2_t _s01y = __mmi_pmaddhw(_v2323, _k1);

                            "paddw      %2, %2, %5      \n" // _sum01 = __mmi_paddw_s(_sum01, _s01x);
                            "paddw      %2, %2, %6      \n" // _sum01 = __mmi_paddw_s(_sum01, _s01y);

                            : "=r"(r0),     // %0
                            "=r"(k0),     // %1
                            "=f"(_sum01), // %2
                            "=f"(temp0),  // %3
                            "=f"(temp1),  // %4
                            "=f"(temp2),  // %5
                            "=f"(temp3),  // %6
                            "=f"(temp4)   // %7
                            : "0"(r0),
                            "1"(k0),
                            "2"(_sum01),
                            "3"(temp0),
                            "4"(temp1),
                            "5"(temp2),
                            "6"(temp3),
                            "7"(temp4),
                            "f"(flag_0x44), // %16
                            "f"(flag_0xee)  // %17
                            : "memory");

                        r0 += 4;
                        k0 += 8;
                    }

                    int sum[2];
                    __mmi_pstw_s(sum, _sum01);

                    sum0 = sum[0];
                    sum1 = sum[1];
                }
#else  // __mips_loongson_mmi
                int nn1 = inch;
#endif // __mips_loongson_mmi

                for (int j = 0; j < nn1; j++)
                {
                    signed short val0 = r0[0];
                    signed short w0 = k0[0];
                    signed short w1 = k0[1];

                    sum0 += val0 * w0;
                    sum1 += val0 * w1;

                    r0 += 1;
                    k0 += 2;
                }

                output0_tm[0] = sum0;
                output1_tm[0] = sum1;
                output0_tm += 1;
                output1_tm += 1;
            }
        }
    }
#endif // __mips_msa

    #pragma omp parallel for num_threads(opt.num_threads)
    for (int p = remain_outch_start; p < outch; p++)
    {
        int* output0_tm = top_blob_tm.channel(p);

#if __mips_msa
        const Mat kernel0_tm = kernel_tm.channel(p / 4 + p % 4);
#else
        const Mat kernel0_tm = kernel_tm.channel(p / 2 + p % 2);
#endif

        for (int r = 0; r < batch; r++)
        {
            const Mat bb2 = bottom_blob_tm2.channel(r);

            int i = 0;
            for (; i + 1 < tiles; i += 2)
            {
                const short* r0 = bb2.row<const short>(i / 2);
                const short* k0 = kernel0_tm.row<const short>(r);

                int sum0 = 0;
                int sum1 = 0;

#if __mips_msa
                int nn4 = inch / 4;
                int nn1 = inch % 4;

                if (nn4 > 0)
                {
                    v4i32 _sum0 = __msa_fill_w(0);
                    v4i32 _sum1 = __msa_fill_w(0);

                    int j = 0;
                    for (; j < nn4; j++)
                    {
                        v8i16 _val16 = __msa_ld_h(r0, 0);

                        v8i16 _w16 = __msa_ld_h(k0, 0);

                        _w16 = (v8i16)__msa_ilvr_d((v2i64)_w16, (v2i64)_w16);

                        v8i16 _extval16 = __msa_clti_s_h(_val16, 0);
                        v8i16 _extw16 = __msa_clti_s_h(_w16, 0);

                        v4i32 _val0l = (v4i32)__msa_ilvr_h(_extval16, _val16);
                        v4i32 _val0h = (v4i32)__msa_ilvl_h(_extval16, _val16);

                        v4i32 _w0l = (v4i32)__msa_ilvr_h(_extw16, _w16);
                        v4i32 _w0h = (v4i32)__msa_ilvl_h(_extw16, _w16);

                        _sum0 = __msa_maddv_w(_sum0, _val0l, _w0l);
                        _sum1 = __msa_maddv_w(_sum1, _val0h, _w0h);

                        r0 += 8;
                        k0 += 4;
                    }

                    sum0 = _sum0[0] + _sum0[1] + _sum0[2] + _sum0[3];
                    sum1 = _sum1[0] + _sum1[1] + _sum1[2] + _sum1[3];
                }
#elif __mips_loongson_mmi
                int nn4 = inch / 4;
                int nn1 = inch % 4;

                if (nn4 > 0)
                {
                    int32x2_t _sum01 = __mmi_pzerow_s();

                    double temp0 = 0;
                    double temp1 = 0;
                    double temp2 = 0;
                    double temp3 = 0;
                    double temp4 = 0;

                    uint64_t flag_0x44 = 0x44;
                    uint64_t flag_0xee = 0xee;

                    int j = 0;
                    for (; j < nn4; j++)
                    {
                        asm volatile(
                            "ld         $0, 64(%0)      \n" // __builtin_prefetch(r0 + 32);
                            "ld         $0, 32(%1)      \n" // __builtin_prefetch(k0 + 16);

                            "ldc1       %4, 0(%0)       \n" // int16x4_t _v0 = __mmi_pldh_s(r0);
                            "ldc1       %5, 8(%0)       \n" // int16x4_t _v1 = __mmi_pldh_s(r0 + 4);
                            "ldc1       %6, 0(%1)       \n" // int16x4_t _k0 = __mmi_pldh_s(k0);

                            "pshufh     %7, %6, %16     \n" // int16x4_t _k0202 = __mmi_pshufh_s(_k0, 0x44);
                            "pshufh     %6, %6, %17     \n" // int16x4_t _k1313 = __mmi_pshufh_s(_k0, 0xee);

                            "pmaddhw    %4, %4, %7      \n" // int32x2_t _s01x = __mmi_pmaddhw(_v0, _k0101);
                            "pmaddhw    %5, %5, %6      \n" // int32x2_t _s01y = __mmi_pmaddhw(_v1, _k2323);

                            "paddw      %2, %2, %4      \n" // _sum01 = __mmi_paddw_s(_sum01, _s01x);
                            "paddw      %2, %2, %5      \n" // _sum01 = __mmi_paddw_s(_sum01, _s01y);

                            : "=r"(r0),     // %0
                            "=r"(k0),     // %1
                            "=f"(_sum01), // %2
                            "=f"(temp0),  // %3
                            "=f"(temp1),  // %4
                            "=f"(temp2),  // %5
                            "=f"(temp3),  // %6
                            "=f"(temp4)   // %7
                            : "0"(r0),
                            "1"(k0),
                            "2"(_sum01),
                            "3"(temp0),
                            "4"(temp1),
                            "5"(temp2),
                            "6"(temp3),
                            "7"(temp4),
                            "f"(flag_0x44), // %16
                            "f"(flag_0xee)  // %17
                            : "memory");

                        r0 += 8;
                        k0 += 4;
                    }

                    int sum[2];
                    __mmi_pstw_s(sum, _sum01);

                    sum0 = sum[0];
                    sum1 = sum[1];
                }
#else  // __mips_loongson_mmi
                int nn1 = inch;
#endif // __mips_msa || __mips_loongson_mmi

                for (int q = 0; q < nn1; q++)
                {
                    signed short val0 = r0[0];
                    signed short val1 = r0[1];
                    signed short w = k0[0];

                    sum0 += val0 * w;
                    sum1 += val1 * w;

                    k0 += 1;
                    r0 += 2;
                }

                output0_tm[0] = sum0;
                output0_tm[1] = sum1;
                output0_tm += 2;
            }
            for (; i < tiles; i++)
            {
                const short* r0 = bb2.row<const short>(i / 2 + i % 2);
                const short* k0 = kernel0_tm.row<const short>(r);

                int sum = 0;

#if __mips_msa
                int nn4 = inch / 4;
                int nn1 = inch % 4;

                if (nn4 > 0)
                {
                    v4i32 _sum = __msa_fill_w(0);

                    int j = 0;
                    for (; j < nn4; j++)
                    {
                        v8i16 _val16 = __msa_ld_h(r0, 0);
                        v8i16 _w16 = __msa_ld_h(k0, 0);

                        v8i16 _extval16 = __msa_clti_s_h(_val16, 0);
                        v8i16 _extw16 = __msa_clti_s_h(_w16, 0);

                        v4i32 _val0l = (v4i32)__msa_ilvr_h(_extval16, _val16);
                        v4i32 _w0l = (v4i32)__msa_ilvr_h(_extw16, _w16);

                        _sum = __msa_maddv_w(_sum, _val0l, _w0l);

                        r0 += 4;
                        k0 += 4;
                    }

                    sum = _sum[0] + _sum[1] + _sum[2] + _sum[3];
                }
#elif __mips_loongson_mmi
                int nn4 = inch / 4;
                int nn1 = inch % 4;

                if (nn4 > 0)
                {
                    int32x2_t _sum0 = __mmi_pzerow_s();

                    double temp0 = 0;
                    double temp1 = 0;

                    int j = 0;
                    for (; j < nn4; j++)
                    {
                        asm volatile(
                            "ld         $0, 32(%0)      \n" // __builtin_prefetch(r0 + 16);
                            "ld         $0, 32(%1)      \n" // __builtin_prefetch(k0 + 16);

                            "ldc1       %3, 0(%0)       \n" // int16x4_t _v0 = __mmi_pldh_s(r0);
                            "ldc1       %4, 0(%1)       \n" // int16x4_t _k0 = __mmi_pldh_s(k0);

                            "pmaddhw    %3, %3, %4      \n" // int32x2_t _s0x = __mmi_pmaddhw(_v0, _k0);
                            "paddw      %2, %2, %3      \n" // _sum0 = __mmi_paddw_s(_sum0, _s0x);

                            : "=r"(r0),    // %0
                            "=r"(k0),    // %1
                            "=f"(_sum0), // %2
                            "=f"(temp0), // %3
                            "=f"(temp1)  // %4
                            : "0"(r0),
                            "1"(k0),
                            "2"(_sum0),
                            "3"(temp0),
                            "4"(temp1)
                            : "memory");

                        r0 += 4;
                        k0 += 4;
                    }

                    int tmp[2];
                    __mmi_pstw_s(tmp, _sum0);

                    sum = tmp[0] + tmp[1];
                }
#else  // __mips_loongson_mmi
                int nn1 = inch;
#endif // __mips_msa || __mips_loongson_mmi

                for (int q = 0; q < nn1; q++)
                {
                    signed short val = r0[0];
                    signed short w = k0[0];

                    sum += val * w;

                    k0 += 1;
                    r0 += 1;
                }

                output0_tm[0] = sum;
                output0_tm++;
            }
        }
    }
}