summaryrefslogtreecommitdiffstats
path: root/third_party/aom/aom_dsp/mips/sad_msa.c
blob: 58cdd80d99bdf9bdcc1ca88530b86fb701601f4e (plain)
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
/*
 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
 *
 * This source code is subject to the terms of the BSD 2 Clause License and
 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
 * was not distributed with this source code in the LICENSE file, you can
 * obtain it at www.aomedia.org/license/software. If the Alliance for Open
 * Media Patent License 1.0 was not distributed with this source code in the
 * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
 */

#include "config/aom_dsp_rtcd.h"

#include "aom_dsp/mips/macros_msa.h"

#define SAD_INSVE_W4(RTYPE, in0, in1, in2, in3, out)       \
  {                                                        \
    out = (RTYPE)__msa_insve_w((v4i32)out, 0, (v4i32)in0); \
    out = (RTYPE)__msa_insve_w((v4i32)out, 1, (v4i32)in1); \
    out = (RTYPE)__msa_insve_w((v4i32)out, 2, (v4i32)in2); \
    out = (RTYPE)__msa_insve_w((v4i32)out, 3, (v4i32)in3); \
  }
#define SAD_INSVE_W4_UB(...) SAD_INSVE_W4(v16u8, __VA_ARGS__)

static uint32_t sad_4width_msa(const uint8_t *src_ptr, int32_t src_stride,
                               const uint8_t *ref_ptr, int32_t ref_stride,
                               int32_t height) {
  int32_t ht_cnt;
  uint32_t src0, src1, src2, src3, ref0, ref1, ref2, ref3;
  v16u8 src = { 0 };
  v16u8 ref = { 0 };
  v16u8 diff;
  v8u16 sad = { 0 };

  for (ht_cnt = (height >> 2); ht_cnt--;) {
    LW4(src_ptr, src_stride, src0, src1, src2, src3);
    src_ptr += (4 * src_stride);
    LW4(ref_ptr, ref_stride, ref0, ref1, ref2, ref3);
    ref_ptr += (4 * ref_stride);

    INSERT_W4_UB(src0, src1, src2, src3, src);
    INSERT_W4_UB(ref0, ref1, ref2, ref3, ref);

    diff = __msa_asub_u_b(src, ref);
    sad += __msa_hadd_u_h(diff, diff);
  }

  return HADD_UH_U32(sad);
}

static uint32_t sad_8width_msa(const uint8_t *src, int32_t src_stride,
                               const uint8_t *ref, int32_t ref_stride,
                               int32_t height) {
  int32_t ht_cnt;
  v16u8 src0, src1, src2, src3, ref0, ref1, ref2, ref3;
  v8u16 sad = { 0 };

  for (ht_cnt = (height >> 2); ht_cnt--;) {
    LD_UB4(src, src_stride, src0, src1, src2, src3);
    src += (4 * src_stride);
    LD_UB4(ref, ref_stride, ref0, ref1, ref2, ref3);
    ref += (4 * ref_stride);

    PCKEV_D4_UB(src1, src0, src3, src2, ref1, ref0, ref3, ref2, src0, src1,
                ref0, ref1);
    sad += SAD_UB2_UH(src0, src1, ref0, ref1);
  }

  return HADD_UH_U32(sad);
}

static uint32_t sad_16width_msa(const uint8_t *src, int32_t src_stride,
                                const uint8_t *ref, int32_t ref_stride,
                                int32_t height) {
  int32_t ht_cnt;
  v16u8 src0, src1, ref0, ref1;
  v8u16 sad = { 0 };

  for (ht_cnt = (height >> 2); ht_cnt--;) {
    LD_UB2(src, src_stride, src0, src1);
    src += (2 * src_stride);
    LD_UB2(ref, ref_stride, ref0, ref1);
    ref += (2 * ref_stride);
    sad += SAD_UB2_UH(src0, src1, ref0, ref1);

    LD_UB2(src, src_stride, src0, src1);
    src += (2 * src_stride);
    LD_UB2(ref, ref_stride, ref0, ref1);
    ref += (2 * ref_stride);
    sad += SAD_UB2_UH(src0, src1, ref0, ref1);
  }

  return HADD_UH_U32(sad);
}

static uint32_t sad_32width_msa(const uint8_t *src, int32_t src_stride,
                                const uint8_t *ref, int32_t ref_stride,
                                int32_t height) {
  int32_t ht_cnt;
  v16u8 src0, src1, ref0, ref1;
  v8u16 sad = { 0 };

  for (ht_cnt = (height >> 2); ht_cnt--;) {
    LD_UB2(src, 16, src0, src1);
    src += src_stride;
    LD_UB2(ref, 16, ref0, ref1);
    ref += ref_stride;
    sad += SAD_UB2_UH(src0, src1, ref0, ref1);

    LD_UB2(src, 16, src0, src1);
    src += src_stride;
    LD_UB2(ref, 16, ref0, ref1);
    ref += ref_stride;
    sad += SAD_UB2_UH(src0, src1, ref0, ref1);

    LD_UB2(src, 16, src0, src1);
    src += src_stride;
    LD_UB2(ref, 16, ref0, ref1);
    ref += ref_stride;
    sad += SAD_UB2_UH(src0, src1, ref0, ref1);

    LD_UB2(src, 16, src0, src1);
    src += src_stride;
    LD_UB2(ref, 16, ref0, ref1);
    ref += ref_stride;
    sad += SAD_UB2_UH(src0, src1, ref0, ref1);
  }

  return HADD_UH_U32(sad);
}

static uint32_t sad_64width_msa(const uint8_t *src, int32_t src_stride,
                                const uint8_t *ref, int32_t ref_stride,
                                int32_t height) {
  int32_t ht_cnt;
  uint32_t sad = 0;
  v16u8 src0, src1, src2, src3;
  v16u8 ref0, ref1, ref2, ref3;
  v8u16 sad0 = { 0 };
  v8u16 sad1 = { 0 };

  for (ht_cnt = (height >> 1); ht_cnt--;) {
    LD_UB4(src, 16, src0, src1, src2, src3);
    src += src_stride;
    LD_UB4(ref, 16, ref0, ref1, ref2, ref3);
    ref += ref_stride;
    sad0 += SAD_UB2_UH(src0, src1, ref0, ref1);
    sad1 += SAD_UB2_UH(src2, src3, ref2, ref3);

    LD_UB4(src, 16, src0, src1, src2, src3);
    src += src_stride;
    LD_UB4(ref, 16, ref0, ref1, ref2, ref3);
    ref += ref_stride;
    sad0 += SAD_UB2_UH(src0, src1, ref0, ref1);
    sad1 += SAD_UB2_UH(src2, src3, ref2, ref3);
  }

  sad = HADD_UH_U32(sad0);
  sad += HADD_UH_U32(sad1);

  return sad;
}

static void sad_4width_x4d_msa(const uint8_t *src_ptr, int32_t src_stride,
                               const uint8_t *const aref_ptr[],
                               int32_t ref_stride, int32_t height,
                               uint32_t *sad_array) {
  const uint8_t *ref0_ptr, *ref1_ptr, *ref2_ptr, *ref3_ptr;
  int32_t ht_cnt;
  uint32_t src0, src1, src2, src3;
  uint32_t ref0, ref1, ref2, ref3;
  v16u8 src = { 0 };
  v16u8 ref = { 0 };
  v16u8 diff;
  v8u16 sad0 = { 0 };
  v8u16 sad1 = { 0 };
  v8u16 sad2 = { 0 };
  v8u16 sad3 = { 0 };

  ref0_ptr = aref_ptr[0];
  ref1_ptr = aref_ptr[1];
  ref2_ptr = aref_ptr[2];
  ref3_ptr = aref_ptr[3];

  for (ht_cnt = (height >> 2); ht_cnt--;) {
    LW4(src_ptr, src_stride, src0, src1, src2, src3);
    INSERT_W4_UB(src0, src1, src2, src3, src);
    src_ptr += (4 * src_stride);

    LW4(ref0_ptr, ref_stride, ref0, ref1, ref2, ref3);
    INSERT_W4_UB(ref0, ref1, ref2, ref3, ref);
    ref0_ptr += (4 * ref_stride);

    diff = __msa_asub_u_b(src, ref);
    sad0 += __msa_hadd_u_h(diff, diff);

    LW4(ref1_ptr, ref_stride, ref0, ref1, ref2, ref3);
    INSERT_W4_UB(ref0, ref1, ref2, ref3, ref);
    ref1_ptr += (4 * ref_stride);

    diff = __msa_asub_u_b(src, ref);
    sad1 += __msa_hadd_u_h(diff, diff);

    LW4(ref2_ptr, ref_stride, ref0, ref1, ref2, ref3);
    INSERT_W4_UB(ref0, ref1, ref2, ref3, ref);
    ref2_ptr += (4 * ref_stride);

    diff = __msa_asub_u_b(src, ref);
    sad2 += __msa_hadd_u_h(diff, diff);

    LW4(ref3_ptr, ref_stride, ref0, ref1, ref2, ref3);
    INSERT_W4_UB(ref0, ref1, ref2, ref3, ref);
    ref3_ptr += (4 * ref_stride);

    diff = __msa_asub_u_b(src, ref);
    sad3 += __msa_hadd_u_h(diff, diff);
  }

  sad_array[0] = HADD_UH_U32(sad0);
  sad_array[1] = HADD_UH_U32(sad1);
  sad_array[2] = HADD_UH_U32(sad2);
  sad_array[3] = HADD_UH_U32(sad3);
}

static void sad_8width_x4d_msa(const uint8_t *src_ptr, int32_t src_stride,
                               const uint8_t *const aref_ptr[],
                               int32_t ref_stride, int32_t height,
                               uint32_t *sad_array) {
  int32_t ht_cnt;
  const uint8_t *ref0_ptr, *ref1_ptr, *ref2_ptr, *ref3_ptr;
  v16u8 src0, src1, src2, src3;
  v16u8 ref0, ref1, ref2, ref3, ref4, ref5, ref6, ref7;
  v16u8 ref8, ref9, ref10, ref11, ref12, ref13, ref14, ref15;
  v8u16 sad0 = { 0 };
  v8u16 sad1 = { 0 };
  v8u16 sad2 = { 0 };
  v8u16 sad3 = { 0 };

  ref0_ptr = aref_ptr[0];
  ref1_ptr = aref_ptr[1];
  ref2_ptr = aref_ptr[2];
  ref3_ptr = aref_ptr[3];

  for (ht_cnt = (height >> 2); ht_cnt--;) {
    LD_UB4(src_ptr, src_stride, src0, src1, src2, src3);
    src_ptr += (4 * src_stride);
    LD_UB4(ref0_ptr, ref_stride, ref0, ref1, ref2, ref3);
    ref0_ptr += (4 * ref_stride);
    LD_UB4(ref1_ptr, ref_stride, ref4, ref5, ref6, ref7);
    ref1_ptr += (4 * ref_stride);
    LD_UB4(ref2_ptr, ref_stride, ref8, ref9, ref10, ref11);
    ref2_ptr += (4 * ref_stride);
    LD_UB4(ref3_ptr, ref_stride, ref12, ref13, ref14, ref15);
    ref3_ptr += (4 * ref_stride);

    PCKEV_D2_UB(src1, src0, src3, src2, src0, src1);
    PCKEV_D2_UB(ref1, ref0, ref3, ref2, ref0, ref1);
    sad0 += SAD_UB2_UH(src0, src1, ref0, ref1);

    PCKEV_D2_UB(ref5, ref4, ref7, ref6, ref0, ref1);
    sad1 += SAD_UB2_UH(src0, src1, ref0, ref1);

    PCKEV_D2_UB(ref9, ref8, ref11, ref10, ref0, ref1);
    sad2 += SAD_UB2_UH(src0, src1, ref0, ref1);

    PCKEV_D2_UB(ref13, ref12, ref15, ref14, ref0, ref1);
    sad3 += SAD_UB2_UH(src0, src1, ref0, ref1);
  }

  sad_array[0] = HADD_UH_U32(sad0);
  sad_array[1] = HADD_UH_U32(sad1);
  sad_array[2] = HADD_UH_U32(sad2);
  sad_array[3] = HADD_UH_U32(sad3);
}

static void sad_16width_x4d_msa(const uint8_t *src_ptr, int32_t src_stride,
                                const uint8_t *const aref_ptr[],
                                int32_t ref_stride, int32_t height,
                                uint32_t *sad_array) {
  int32_t ht_cnt;
  const uint8_t *ref0_ptr, *ref1_ptr, *ref2_ptr, *ref3_ptr;
  v16u8 src, ref0, ref1, ref2, ref3, diff;
  v8u16 sad0 = { 0 };
  v8u16 sad1 = { 0 };
  v8u16 sad2 = { 0 };
  v8u16 sad3 = { 0 };

  ref0_ptr = aref_ptr[0];
  ref1_ptr = aref_ptr[1];
  ref2_ptr = aref_ptr[2];
  ref3_ptr = aref_ptr[3];

  for (ht_cnt = (height >> 1); ht_cnt--;) {
    src = LD_UB(src_ptr);
    src_ptr += src_stride;
    ref0 = LD_UB(ref0_ptr);
    ref0_ptr += ref_stride;
    ref1 = LD_UB(ref1_ptr);
    ref1_ptr += ref_stride;
    ref2 = LD_UB(ref2_ptr);
    ref2_ptr += ref_stride;
    ref3 = LD_UB(ref3_ptr);
    ref3_ptr += ref_stride;

    diff = __msa_asub_u_b(src, ref0);
    sad0 += __msa_hadd_u_h(diff, diff);
    diff = __msa_asub_u_b(src, ref1);
    sad1 += __msa_hadd_u_h(diff, diff);
    diff = __msa_asub_u_b(src, ref2);
    sad2 += __msa_hadd_u_h(diff, diff);
    diff = __msa_asub_u_b(src, ref3);
    sad3 += __msa_hadd_u_h(diff, diff);

    src = LD_UB(src_ptr);
    src_ptr += src_stride;
    ref0 = LD_UB(ref0_ptr);
    ref0_ptr += ref_stride;
    ref1 = LD_UB(ref1_ptr);
    ref1_ptr += ref_stride;
    ref2 = LD_UB(ref2_ptr);
    ref2_ptr += ref_stride;
    ref3 = LD_UB(ref3_ptr);
    ref3_ptr += ref_stride;

    diff = __msa_asub_u_b(src, ref0);
    sad0 += __msa_hadd_u_h(diff, diff);
    diff = __msa_asub_u_b(src, ref1);
    sad1 += __msa_hadd_u_h(diff, diff);
    diff = __msa_asub_u_b(src, ref2);
    sad2 += __msa_hadd_u_h(diff, diff);
    diff = __msa_asub_u_b(src, ref3);
    sad3 += __msa_hadd_u_h(diff, diff);
  }

  sad_array[0] = HADD_UH_U32(sad0);
  sad_array[1] = HADD_UH_U32(sad1);
  sad_array[2] = HADD_UH_U32(sad2);
  sad_array[3] = HADD_UH_U32(sad3);
}

static void sad_32width_x4d_msa(const uint8_t *src, int32_t src_stride,
                                const uint8_t *const aref_ptr[],
                                int32_t ref_stride, int32_t height,
                                uint32_t *sad_array) {
  const uint8_t *ref0_ptr, *ref1_ptr, *ref2_ptr, *ref3_ptr;
  int32_t ht_cnt;
  v16u8 src0, src1, ref0, ref1;
  v8u16 sad0 = { 0 };
  v8u16 sad1 = { 0 };
  v8u16 sad2 = { 0 };
  v8u16 sad3 = { 0 };

  ref0_ptr = aref_ptr[0];
  ref1_ptr = aref_ptr[1];
  ref2_ptr = aref_ptr[2];
  ref3_ptr = aref_ptr[3];

  for (ht_cnt = height; ht_cnt--;) {
    LD_UB2(src, 16, src0, src1);
    src += src_stride;

    LD_UB2(ref0_ptr, 16, ref0, ref1);
    ref0_ptr += ref_stride;
    sad0 += SAD_UB2_UH(src0, src1, ref0, ref1);

    LD_UB2(ref1_ptr, 16, ref0, ref1);
    ref1_ptr += ref_stride;
    sad1 += SAD_UB2_UH(src0, src1, ref0, ref1);

    LD_UB2(ref2_ptr, 16, ref0, ref1);
    ref2_ptr += ref_stride;
    sad2 += SAD_UB2_UH(src0, src1, ref0, ref1);

    LD_UB2(ref3_ptr, 16, ref0, ref1);
    ref3_ptr += ref_stride;
    sad3 += SAD_UB2_UH(src0, src1, ref0, ref1);
  }

  sad_array[0] = HADD_UH_U32(sad0);
  sad_array[1] = HADD_UH_U32(sad1);
  sad_array[2] = HADD_UH_U32(sad2);
  sad_array[3] = HADD_UH_U32(sad3);
}

static void sad_64width_x4d_msa(const uint8_t *src, int32_t src_stride,
                                const uint8_t *const aref_ptr[],
                                int32_t ref_stride, int32_t height,
                                uint32_t *sad_array) {
  const uint8_t *ref0_ptr, *ref1_ptr, *ref2_ptr, *ref3_ptr;
  int32_t ht_cnt;
  v16u8 src0, src1, src2, src3;
  v16u8 ref0, ref1, ref2, ref3;
  v8u16 sad0_0 = { 0 };
  v8u16 sad0_1 = { 0 };
  v8u16 sad1_0 = { 0 };
  v8u16 sad1_1 = { 0 };
  v8u16 sad2_0 = { 0 };
  v8u16 sad2_1 = { 0 };
  v8u16 sad3_0 = { 0 };
  v8u16 sad3_1 = { 0 };

  ref0_ptr = aref_ptr[0];
  ref1_ptr = aref_ptr[1];
  ref2_ptr = aref_ptr[2];
  ref3_ptr = aref_ptr[3];

  for (ht_cnt = height; ht_cnt--;) {
    LD_UB4(src, 16, src0, src1, src2, src3);
    src += src_stride;

    LD_UB4(ref0_ptr, 16, ref0, ref1, ref2, ref3);
    ref0_ptr += ref_stride;
    sad0_0 += SAD_UB2_UH(src0, src1, ref0, ref1);
    sad0_1 += SAD_UB2_UH(src2, src3, ref2, ref3);

    LD_UB4(ref1_ptr, 16, ref0, ref1, ref2, ref3);
    ref1_ptr += ref_stride;
    sad1_0 += SAD_UB2_UH(src0, src1, ref0, ref1);
    sad1_1 += SAD_UB2_UH(src2, src3, ref2, ref3);

    LD_UB4(ref2_ptr, 16, ref0, ref1, ref2, ref3);
    ref2_ptr += ref_stride;
    sad2_0 += SAD_UB2_UH(src0, src1, ref0, ref1);
    sad2_1 += SAD_UB2_UH(src2, src3, ref2, ref3);

    LD_UB4(ref3_ptr, 16, ref0, ref1, ref2, ref3);
    ref3_ptr += ref_stride;
    sad3_0 += SAD_UB2_UH(src0, src1, ref0, ref1);
    sad3_1 += SAD_UB2_UH(src2, src3, ref2, ref3);
  }

  sad_array[0] = HADD_UH_U32(sad0_0);
  sad_array[0] += HADD_UH_U32(sad0_1);
  sad_array[1] = HADD_UH_U32(sad1_0);
  sad_array[1] += HADD_UH_U32(sad1_1);
  sad_array[2] = HADD_UH_U32(sad2_0);
  sad_array[2] += HADD_UH_U32(sad2_1);
  sad_array[3] = HADD_UH_U32(sad3_0);
  sad_array[3] += HADD_UH_U32(sad3_1);
}

static uint32_t avgsad_4width_msa(const uint8_t *src_ptr, int32_t src_stride,
                                  const uint8_t *ref_ptr, int32_t ref_stride,
                                  int32_t height, const uint8_t *sec_pred) {
  int32_t ht_cnt;
  uint32_t src0, src1, src2, src3, ref0, ref1, ref2, ref3;
  v16u8 src = { 0 };
  v16u8 ref = { 0 };
  v16u8 diff, pred, comp;
  v8u16 sad = { 0 };

  for (ht_cnt = (height >> 2); ht_cnt--;) {
    LW4(src_ptr, src_stride, src0, src1, src2, src3);
    src_ptr += (4 * src_stride);
    LW4(ref_ptr, ref_stride, ref0, ref1, ref2, ref3);
    ref_ptr += (4 * ref_stride);
    pred = LD_UB(sec_pred);
    sec_pred += 16;

    INSERT_W4_UB(src0, src1, src2, src3, src);
    INSERT_W4_UB(ref0, ref1, ref2, ref3, ref);

    comp = __msa_aver_u_b(pred, ref);
    diff = __msa_asub_u_b(src, comp);
    sad += __msa_hadd_u_h(diff, diff);
  }

  return HADD_UH_U32(sad);
}

static uint32_t avgsad_8width_msa(const uint8_t *src, int32_t src_stride,
                                  const uint8_t *ref, int32_t ref_stride,
                                  int32_t height, const uint8_t *sec_pred) {
  int32_t ht_cnt;
  v16u8 src0, src1, src2, src3, ref0, ref1, ref2, ref3;
  v16u8 diff0, diff1, pred0, pred1;
  v8u16 sad = { 0 };

  for (ht_cnt = (height >> 2); ht_cnt--;) {
    LD_UB4(src, src_stride, src0, src1, src2, src3);
    src += (4 * src_stride);
    LD_UB4(ref, ref_stride, ref0, ref1, ref2, ref3);
    ref += (4 * ref_stride);
    LD_UB2(sec_pred, 16, pred0, pred1);
    sec_pred += 32;
    PCKEV_D4_UB(src1, src0, src3, src2, ref1, ref0, ref3, ref2, src0, src1,
                ref0, ref1);
    AVER_UB2_UB(pred0, ref0, pred1, ref1, diff0, diff1);
    sad += SAD_UB2_UH(src0, src1, diff0, diff1);
  }

  return HADD_UH_U32(sad);
}

static uint32_t avgsad_16width_msa(const uint8_t *src, int32_t src_stride,
                                   const uint8_t *ref, int32_t ref_stride,
                                   int32_t height, const uint8_t *sec_pred) {
  int32_t ht_cnt;
  v16u8 src0, src1, src2, src3, ref0, ref1, ref2, ref3;
  v16u8 pred0, pred1, pred2, pred3, comp0, comp1;
  v8u16 sad = { 0 };

  for (ht_cnt = (height >> 3); ht_cnt--;) {
    LD_UB4(src, src_stride, src0, src1, src2, src3);
    src += (4 * src_stride);
    LD_UB4(ref, ref_stride, ref0, ref1, ref2, ref3);
    ref += (4 * ref_stride);
    LD_UB4(sec_pred, 16, pred0, pred1, pred2, pred3);
    sec_pred += (4 * 16);
    AVER_UB2_UB(pred0, ref0, pred1, ref1, comp0, comp1);
    sad += SAD_UB2_UH(src0, src1, comp0, comp1);
    AVER_UB2_UB(pred2, ref2, pred3, ref3, comp0, comp1);
    sad += SAD_UB2_UH(src2, src3, comp0, comp1);

    LD_UB4(src, src_stride, src0, src1, src2, src3);
    src += (4 * src_stride);
    LD_UB4(ref, ref_stride, ref0, ref1, ref2, ref3);
    ref += (4 * ref_stride);
    LD_UB4(sec_pred, 16, pred0, pred1, pred2, pred3);
    sec_pred += (4 * 16);
    AVER_UB2_UB(pred0, ref0, pred1, ref1, comp0, comp1);
    sad += SAD_UB2_UH(src0, src1, comp0, comp1);
    AVER_UB2_UB(pred2, ref2, pred3, ref3, comp0, comp1);
    sad += SAD_UB2_UH(src2, src3, comp0, comp1);
  }

  return HADD_UH_U32(sad);
}

static uint32_t avgsad_32width_msa(const uint8_t *src, int32_t src_stride,
                                   const uint8_t *ref, int32_t ref_stride,
                                   int32_t height, const uint8_t *sec_pred) {
  int32_t ht_cnt;
  v16u8 src0, src1, src2, src3, src4, src5, src6, src7;
  v16u8 ref0, ref1, ref2, ref3, ref4, ref5, ref6, ref7;
  v16u8 pred0, pred1, pred2, pred3, pred4, pred5, pred6, pred7;
  v16u8 comp0, comp1;
  v8u16 sad = { 0 };

  for (ht_cnt = (height >> 2); ht_cnt--;) {
    LD_UB4(src, src_stride, src0, src2, src4, src6);
    LD_UB4(src + 16, src_stride, src1, src3, src5, src7);
    src += (4 * src_stride);

    LD_UB4(ref, ref_stride, ref0, ref2, ref4, ref6);
    LD_UB4(ref + 16, ref_stride, ref1, ref3, ref5, ref7);
    ref += (4 * ref_stride);

    LD_UB4(sec_pred, 32, pred0, pred2, pred4, pred6);
    LD_UB4(sec_pred + 16, 32, pred1, pred3, pred5, pred7);
    sec_pred += (4 * 32);

    AVER_UB2_UB(pred0, ref0, pred1, ref1, comp0, comp1);
    sad += SAD_UB2_UH(src0, src1, comp0, comp1);
    AVER_UB2_UB(pred2, ref2, pred3, ref3, comp0, comp1);
    sad += SAD_UB2_UH(src2, src3, comp0, comp1);
    AVER_UB2_UB(pred4, ref4, pred5, ref5, comp0, comp1);
    sad += SAD_UB2_UH(src4, src5, comp0, comp1);
    AVER_UB2_UB(pred6, ref6, pred7, ref7, comp0, comp1);
    sad += SAD_UB2_UH(src6, src7, comp0, comp1);
  }

  return HADD_UH_U32(sad);
}

static uint32_t avgsad_64width_msa(const uint8_t *src, int32_t src_stride,
                                   const uint8_t *ref, int32_t ref_stride,
                                   int32_t height, const uint8_t *sec_pred) {
  int32_t ht_cnt;
  v16u8 src0, src1, src2, src3;
  v16u8 ref0, ref1, ref2, ref3;
  v16u8 comp0, comp1, comp2, comp3;
  v16u8 pred0, pred1, pred2, pred3;
  v8u16 sad0 = { 0 };
  v8u16 sad1 = { 0 };
  v4u32 sad;

  for (ht_cnt = (height >> 2); ht_cnt--;) {
    LD_UB4(src, 16, src0, src1, src2, src3);
    src += src_stride;
    LD_UB4(ref, 16, ref0, ref1, ref2, ref3);
    ref += ref_stride;
    LD_UB4(sec_pred, 16, pred0, pred1, pred2, pred3);
    sec_pred += 64;
    AVER_UB4_UB(pred0, ref0, pred1, ref1, pred2, ref2, pred3, ref3, comp0,
                comp1, comp2, comp3);
    sad0 += SAD_UB2_UH(src0, src1, comp0, comp1);
    sad1 += SAD_UB2_UH(src2, src3, comp2, comp3);

    LD_UB4(src, 16, src0, src1, src2, src3);
    src += src_stride;
    LD_UB4(ref, 16, ref0, ref1, ref2, ref3);
    ref += ref_stride;
    LD_UB4(sec_pred, 16, pred0, pred1, pred2, pred3);
    sec_pred += 64;
    AVER_UB4_UB(pred0, ref0, pred1, ref1, pred2, ref2, pred3, ref3, comp0,
                comp1, comp2, comp3);
    sad0 += SAD_UB2_UH(src0, src1, comp0, comp1);
    sad1 += SAD_UB2_UH(src2, src3, comp2, comp3);

    LD_UB4(src, 16, src0, src1, src2, src3);
    src += src_stride;
    LD_UB4(ref, 16, ref0, ref1, ref2, ref3);
    ref += ref_stride;
    LD_UB4(sec_pred, 16, pred0, pred1, pred2, pred3);
    sec_pred += 64;
    AVER_UB4_UB(pred0, ref0, pred1, ref1, pred2, ref2, pred3, ref3, comp0,
                comp1, comp2, comp3);
    sad0 += SAD_UB2_UH(src0, src1, comp0, comp1);
    sad1 += SAD_UB2_UH(src2, src3, comp2, comp3);

    LD_UB4(src, 16, src0, src1, src2, src3);
    src += src_stride;
    LD_UB4(ref, 16, ref0, ref1, ref2, ref3);
    ref += ref_stride;
    LD_UB4(sec_pred, 16, pred0, pred1, pred2, pred3);
    sec_pred += 64;
    AVER_UB4_UB(pred0, ref0, pred1, ref1, pred2, ref2, pred3, ref3, comp0,
                comp1, comp2, comp3);
    sad0 += SAD_UB2_UH(src0, src1, comp0, comp1);
    sad1 += SAD_UB2_UH(src2, src3, comp2, comp3);
  }

  sad = __msa_hadd_u_w(sad0, sad0);
  sad += __msa_hadd_u_w(sad1, sad1);

  return HADD_SW_S32(sad);
}

#define AOM_SAD_4xHEIGHT_MSA(height)                                         \
  uint32_t aom_sad4x##height##_msa(const uint8_t *src, int32_t src_stride,   \
                                   const uint8_t *ref, int32_t ref_stride) { \
    return sad_4width_msa(src, src_stride, ref, ref_stride, height);         \
  }

#define AOM_SAD_8xHEIGHT_MSA(height)                                         \
  uint32_t aom_sad8x##height##_msa(const uint8_t *src, int32_t src_stride,   \
                                   const uint8_t *ref, int32_t ref_stride) { \
    return sad_8width_msa(src, src_stride, ref, ref_stride, height);         \
  }

#define AOM_SAD_16xHEIGHT_MSA(height)                                         \
  uint32_t aom_sad16x##height##_msa(const uint8_t *src, int32_t src_stride,   \
                                    const uint8_t *ref, int32_t ref_stride) { \
    return sad_16width_msa(src, src_stride, ref, ref_stride, height);         \
  }

#define AOM_SAD_32xHEIGHT_MSA(height)                                         \
  uint32_t aom_sad32x##height##_msa(const uint8_t *src, int32_t src_stride,   \
                                    const uint8_t *ref, int32_t ref_stride) { \
    return sad_32width_msa(src, src_stride, ref, ref_stride, height);         \
  }

#define AOM_SAD_64xHEIGHT_MSA(height)                                         \
  uint32_t aom_sad64x##height##_msa(const uint8_t *src, int32_t src_stride,   \
                                    const uint8_t *ref, int32_t ref_stride) { \
    return sad_64width_msa(src, src_stride, ref, ref_stride, height);         \
  }

#define AOM_SAD_4xHEIGHTx4D_MSA(height)                                   \
  void aom_sad4x##height##x4d_msa(const uint8_t *src, int32_t src_stride, \
                                  const uint8_t *const refs[],            \
                                  int32_t ref_stride, uint32_t *sads) {   \
    sad_4width_x4d_msa(src, src_stride, refs, ref_stride, height, sads);  \
  }

#define AOM_SAD_8xHEIGHTx4D_MSA(height)                                   \
  void aom_sad8x##height##x4d_msa(const uint8_t *src, int32_t src_stride, \
                                  const uint8_t *const refs[],            \
                                  int32_t ref_stride, uint32_t *sads) {   \
    sad_8width_x4d_msa(src, src_stride, refs, ref_stride, height, sads);  \
  }

#define AOM_SAD_16xHEIGHTx4D_MSA(height)                                   \
  void aom_sad16x##height##x4d_msa(const uint8_t *src, int32_t src_stride, \
                                   const uint8_t *const refs[],            \
                                   int32_t ref_stride, uint32_t *sads) {   \
    sad_16width_x4d_msa(src, src_stride, refs, ref_stride, height, sads);  \
  }

#define AOM_SAD_32xHEIGHTx4D_MSA(height)                                   \
  void aom_sad32x##height##x4d_msa(const uint8_t *src, int32_t src_stride, \
                                   const uint8_t *const refs[],            \
                                   int32_t ref_stride, uint32_t *sads) {   \
    sad_32width_x4d_msa(src, src_stride, refs, ref_stride, height, sads);  \
  }

#define AOM_SAD_64xHEIGHTx4D_MSA(height)                                   \
  void aom_sad64x##height##x4d_msa(const uint8_t *src, int32_t src_stride, \
                                   const uint8_t *const refs[],            \
                                   int32_t ref_stride, uint32_t *sads) {   \
    sad_64width_x4d_msa(src, src_stride, refs, ref_stride, height, sads);  \
  }

#define AOM_AVGSAD_4xHEIGHT_MSA(height)                                        \
  uint32_t aom_sad4x##height##_avg_msa(const uint8_t *src, int32_t src_stride, \
                                       const uint8_t *ref, int32_t ref_stride, \
                                       const uint8_t *second_pred) {           \
    return avgsad_4width_msa(src, src_stride, ref, ref_stride, height,         \
                             second_pred);                                     \
  }

#define AOM_AVGSAD_8xHEIGHT_MSA(height)                                        \
  uint32_t aom_sad8x##height##_avg_msa(const uint8_t *src, int32_t src_stride, \
                                       const uint8_t *ref, int32_t ref_stride, \
                                       const uint8_t *second_pred) {           \
    return avgsad_8width_msa(src, src_stride, ref, ref_stride, height,         \
                             second_pred);                                     \
  }

#define AOM_AVGSAD_16xHEIGHT_MSA(height)                                \
  uint32_t aom_sad16x##height##_avg_msa(                                \
      const uint8_t *src, int32_t src_stride, const uint8_t *ref,       \
      int32_t ref_stride, const uint8_t *second_pred) {                 \
    return avgsad_16width_msa(src, src_stride, ref, ref_stride, height, \
                              second_pred);                             \
  }

#define AOM_AVGSAD_32xHEIGHT_MSA(height)                                \
  uint32_t aom_sad32x##height##_avg_msa(                                \
      const uint8_t *src, int32_t src_stride, const uint8_t *ref,       \
      int32_t ref_stride, const uint8_t *second_pred) {                 \
    return avgsad_32width_msa(src, src_stride, ref, ref_stride, height, \
                              second_pred);                             \
  }

#define AOM_AVGSAD_64xHEIGHT_MSA(height)                                \
  uint32_t aom_sad64x##height##_avg_msa(                                \
      const uint8_t *src, int32_t src_stride, const uint8_t *ref,       \
      int32_t ref_stride, const uint8_t *second_pred) {                 \
    return avgsad_64width_msa(src, src_stride, ref, ref_stride, height, \
                              second_pred);                             \
  }

/* clang-format off */
// 64x64
AOM_SAD_64xHEIGHT_MSA(64)
AOM_SAD_64xHEIGHTx4D_MSA(64)
AOM_AVGSAD_64xHEIGHT_MSA(64)

// 64x32
AOM_SAD_64xHEIGHT_MSA(32)
AOM_SAD_64xHEIGHTx4D_MSA(32)
AOM_AVGSAD_64xHEIGHT_MSA(32)

// 32x64
AOM_SAD_32xHEIGHT_MSA(64)
AOM_SAD_32xHEIGHTx4D_MSA(64)
AOM_AVGSAD_32xHEIGHT_MSA(64)

// 32x32
AOM_SAD_32xHEIGHT_MSA(32)
AOM_SAD_32xHEIGHTx4D_MSA(32)
AOM_AVGSAD_32xHEIGHT_MSA(32)

// 32x16
AOM_SAD_32xHEIGHT_MSA(16)
AOM_SAD_32xHEIGHTx4D_MSA(16)
AOM_AVGSAD_32xHEIGHT_MSA(16)

// 16x32
AOM_SAD_16xHEIGHT_MSA(32)
AOM_SAD_16xHEIGHTx4D_MSA(32)
AOM_AVGSAD_16xHEIGHT_MSA(32)

// 16x16
AOM_SAD_16xHEIGHT_MSA(16)
AOM_SAD_16xHEIGHTx4D_MSA(16)
AOM_AVGSAD_16xHEIGHT_MSA(16)

// 16x8
AOM_SAD_16xHEIGHT_MSA(8)
AOM_SAD_16xHEIGHTx4D_MSA(8)
AOM_AVGSAD_16xHEIGHT_MSA(8)

// 8x16
AOM_SAD_8xHEIGHT_MSA(16)
AOM_SAD_8xHEIGHTx4D_MSA(16)
AOM_AVGSAD_8xHEIGHT_MSA(16)

// 8x8
AOM_SAD_8xHEIGHT_MSA(8)
AOM_SAD_8xHEIGHTx4D_MSA(8)
AOM_AVGSAD_8xHEIGHT_MSA(8)

// 8x4
AOM_SAD_8xHEIGHT_MSA(4)
AOM_SAD_8xHEIGHTx4D_MSA(4)
AOM_AVGSAD_8xHEIGHT_MSA(4)

// 4x8
AOM_SAD_4xHEIGHT_MSA(8)
AOM_SAD_4xHEIGHTx4D_MSA(8)
AOM_AVGSAD_4xHEIGHT_MSA(8)

// 4x4
AOM_SAD_4xHEIGHT_MSA(4)
AOM_SAD_4xHEIGHTx4D_MSA(4)
AOM_AVGSAD_4xHEIGHT_MSA(4)
    /* clang-format on */