rav1e 0.5.1

The fastest and safest AV1 encoder
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
; Copyright (c) 2019, The rav1e contributors. 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.asm"
%include "ext/x86/x86inc.asm"

%if ARCH_X86_64

SECTION_RODATA 32
maddubsw_hsub: times 16 db 1, -1

SECTION .text

%define m(x) mangle(private_prefix %+ _ %+ x %+ SUFFIX)

; Perform 4x4 hadamard transform on input with 2 rows per register.
; Rows 0 and 2 are in m0 and rows 1 and 3 are in m1.
; A second set of packed input can also be taken in m2 and m3.
; Ends with sums in every other entry (i.e. already reduced horizontally).
%macro HADAMARD_4x4_PACKED 1
%if %1 == 1
    %define tmp m2
    ; 2->0, 1->2, 0->2
    %define ROTATE SWAP 2, 1, 0
%elif %1 == 2
    %define tmp m4
    ; 4->0, 3->2, 2->3, 1->2, 0->1
    %define ROTATE SWAP 4, 3, 2, 1, 0
%endif
    ; m0  d2 c2 b2 a2 d0 c0 b0 a0
    ; m1  d3 c3 b3 a3 d1 c1 b1 a1

    ; Stage 1
    ; m0  d2+d3 c2+c3 b2+b3 a2+a3 d0+d1 c0+c1 b0+b1 a0+a1
    ; m1  d2-d3 c2-c3 b2-b3 a2-a3 d0-d1 c0-c1 b0-b1 a0-a1
    paddw              tmp, m0, m1
    psubw               m0, m1
%if %1 == 2
    paddw               m1, m2, m3
    psubw               m2, m3
%endif
    ROTATE

    ; Stage 2
    ; m0  d0-d1 d0+d1 c0-c1 c0+c1 b0-b1 b0+b1 a0-a1 a0+a1
    ; m1  d2-d3 d2+d3 c2-c3 c2+c3 b2-b3 b2+b3 a2-a3 a2+a3
    punpcklwd          tmp, m0, m1
    punpckhwd           m0, m1
%if %1 == 2
    punpcklwd           m1, m2, m3
    punpckhwd           m2, m3
%endif
    ROTATE

    ; m0  d0-d1+d2-d3 d0+d1+d2+d3 c0-c1+c2-c3 c0+c1+c2+c3
    ;     b0-b1+b2-b3 b0+b1+b2+b3 a0-a1+a2-a3 a0+a1+a2+a3
    ; m1  d0-d2-d2+d3 d0+d1-d2-d3 c0-c1-c2+c3 c0+c1-c2-c3
    ;     b0-b1-b2+b3 b0+b1-b2-b3 a0-a1-a2-a3 a0+a1-a2-a3
    paddw              tmp, m0, m1
    psubw               m0, m1
%if %1 == 2
    paddw               m1, m2, m3
    psubw               m2, m3
%endif
    ROTATE

    ; m0  s2 s0 r2 r0 q2 q0 p2 p0
    ; m1  s3 s1 r3 r1 q3 q1 p3 p1

    ; Stage 1
    ; m0  q3 q1 q2 q0 p3 p1 p2 p0
    ; m1  s3 s1 s2 s0 r3 r1 r2 r0
    punpckldq          tmp, m0, m1
    punpckhdq           m0, m1
%if %1 == 2
    punpckldq           m1, m2, m3
    punpckhdq           m2, m3
%endif
    ROTATE

    ; m0  q3+s3 q1+s1 q2+s2 q0+s0 p3+r3 p1+r1 p2+r2 p0+r0
    ; m1  q3-s3 q1-s1 q2-s2 q0-s0 p3-r3 p1-r1 p2-r2 p0-r0
    paddw              tmp, m0, m1
    psubw               m0, m1
%if %1 == 2
    paddw               m1, m2, m3
    psubw               m2, m3
%endif
    ROTATE

    ; Stage 2
    ; m0  p3-r3 p1-r1 p2-r2 p0-r0 p3+r3 p1+r1 p2+r2 p0+r0
    ; m1  q3-s3 q1-s1 q2-s2 q0-s0 q3+s3 q1+s1 q2+s2 q0+s0
    punpcklqdq         tmp, m0, m1
    punpckhqdq          m0, m1
%if %1 == 2
    punpcklqdq          m1, m2, m3
    punpckhqdq          m2, m3
%endif
    ROTATE

    ; Use the fact that
    ;   (abs(a+b)+abs(a-b))/2 = max(abs(a),abs(b))
    ;  to merge the final butterfly with the abs and the first stage of
    ;  accumulation.
    ; Avoid pabsw by using max(a, b) + max(a + b + 0x7FFF, 0x7FFF) instead.
    ; Actually calculates (abs(a+b)+abs(a-b))/2-0x7FFF.
    ; The final sum must be offset to compensate for subtracting 0x7FFF.
    paddw              tmp, m0, m1
    pmaxsw              m0, m1
    ; m1 is free
    ; 0x7FFF
    pcmpeqb             m1, m1
    psrlw               m1, 1

    paddsw             tmp, m1
    psubw               m0, tmp
%if %1 == 2
    paddw              tmp, m2, m3
    pmaxsw              m2, m3
    paddsw             tmp, m1
    psubw               m2, tmp

    paddw               m0, m2
%endif
%endmacro

; Load diffs of 4 entries for 2 rows
%macro LOAD_PACK_DIFF_Dx2 7
    movd               m%1, %2
    movd               m%6, %4
    punpckldq          m%1, m%6
    pmovzxbw           m%1, m%1
    movd               m%6, %3
    movd               m%7, %5
    punpckldq          m%6, m%7
    pmovzxbw           m%6, m%6
    psubw              m%1, m%6
%endmacro

; Can only use 128-bit vectors
%macro SATD_4x4_FN 0
cglobal satd_4x4, 4, 6, 4, src, src_stride, dst, dst_stride, \
                           src_stride3, dst_stride3
    lea       src_stride3q, [src_strideq*3]
    lea       dst_stride3q, [dst_strideq*3]

    ; Load rows 0 and 2 to m0 and 1 and 3 to m1
    LOAD_PACK_DIFF_Dx2 0, [srcq], [dstq], \
                          [srcq+src_strideq*2], [dstq+dst_strideq*2], \
                          2, 3
    LOAD_PACK_DIFF_Dx2 1, [srcq+src_strideq*1], [dstq+dst_strideq*1], \
                          [srcq+src_stride3q], [dstq+dst_stride3q], \
                          2, 3

    HADAMARD_4x4_PACKED 1

    ; Reduce horizontally
    pshufd              m1, m0, q3232
    paddw               m0, m1
    pshuflw             m1, m0, q3232
    paddw               m0, m1
    pshuflw             m1, m0, q1111

    ; Perform normalization during the final stage of accumulation
    pavgw               m0, m1
    movd               eax, m0
    movzx              eax, ax

    ; Add an offset for how the final butterfly stage and the first stage of
    ;  accumulation was done. Since this offset is an even number, this can
    ;  safely be done after normalization using pavgw.
    sub                 ax, 4
    RET
%endmacro

INIT_XMM sse4
SATD_4x4_FN

INIT_XMM avx2
SATD_4x4_FN

; Load diffs of 8 entries for 2 row
; Each set of 4 columns share an 128-bit lane
%macro LOAD_PACK_DIFF_Qx2 7
    movq              xm%1, %2
    movq              xm%6, %4
    punpckldq         xm%1, xm%6
    pmovzxbw           m%1, xm%1
    movq              xm%6, %3
    movq              xm%7, %5
    punpckldq         xm%6, xm%7
    pmovzxbw           m%6, xm%6
    psubw              m%1, m%6
%endmacro

INIT_YMM avx2
cglobal satd_8x4, 4, 6, 4, src, src_stride, dst, dst_stride, \
                           src_stride3, dst_stride3
    lea       src_stride3q, [src_strideq*3]
    lea       dst_stride3q, [dst_strideq*3]
    ; Load rows 0 and 2 to m0 and 1 and 3 to m1
    ; Each set of 4 columns share 128-bit lanes
    LOAD_PACK_DIFF_Qx2 0, [srcq], [dstq], \
                          [srcq+src_strideq*2], [dstq+dst_strideq*2], \
                       2, 3
    LOAD_PACK_DIFF_Qx2 1, [srcq+src_strideq*1], [dstq+dst_strideq*1], \
                          [srcq+src_stride3q], [dstq+dst_stride3q], \
                       2, 3

    HADAMARD_4x4_PACKED 1

    ; Reduce horizontally
    vextracti128       xm1, m0, 1
    paddw              xm0, xm1
    pshufd             xm1, xm0, q3232
    paddw              xm0, xm1
    pshuflw            xm1, xm0, q3232
    paddw              xm0, xm1
    pshuflw            xm1, xm0, q1111

    ; Perform normalization during the final stage of accumulation
    pavgw              xm0, xm1
    movd               eax, xm0
    movzx              eax, ax

    ; Add an offset for how the final butterfly stage and the first stage of
    ;  accumulation was done. Since this offset is an even number, this can
    ;  safely be done after normalization using pavgw.
    sub                 ax, 8
    RET

; Load diffs of 4 entries for 4 rows
; Each set of two rows share 128-bit lanes
%macro LOAD_PACK_DIFF_Dx4 12
    movd              xm%1, %2
    movd             xm%10, %4
    punpckldq         xm%1, xm%10
    movd             xm%10, %6
    movd             xm%11, %8
    punpckldq        xm%10, xm%11
    punpcklqdq        xm%1, xm%10
    pmovzxbw           m%1, xm%1
    movd             xm%10, %3
    movd             xm%11, %5
    punpckldq        xm%10, xm%11
    movd             xm%11, %7
    movd             xm%12, %9
    punpckldq        xm%11, xm%12
    punpcklqdq       xm%10, xm%11
    pmovzxbw          m%10, xm%10
    psubw              m%1, m%10
%endmacro

INIT_YMM avx2
cglobal satd_4x8, 4, 8, 5, src, src_stride, dst, dst_stride, \
                           src4, dst4, src_stride3, dst_stride3
    lea       src_stride3q, [src_strideq*3]
    lea       dst_stride3q, [dst_strideq*3]
    lea              src4q, [srcq+src_strideq*4]
    lea              dst4q, [dstq+dst_strideq*4]
    ; Load rows 0, 2, 4 and 6 to m0 and 1, 3, 5 and 7 to m1.
    ; Lanes split the low and high rows of m0 and m1.
    LOAD_PACK_DIFF_Dx4 0, [srcq], [dstq], \
                          [srcq+src_strideq*2], [dstq+dst_strideq*2], \
                          [src4q], [dst4q], \
                          [src4q+src_strideq*2], [dst4q+dst_strideq*2], \
                       2, 3, 4
    LOAD_PACK_DIFF_Dx4 1, [srcq+src_strideq*1], [dstq+dst_strideq*1], \
                          [srcq+src_stride3q], [dstq+dst_stride3q], \
                          [src4q+src_strideq*1], [dst4q+dst_strideq*1], \
                          [src4q+src_stride3q], [dst4q+dst_stride3q], \
                       2, 3, 4

    HADAMARD_4x4_PACKED 1

    ; Reduce horizontally
    vextracti128       xm1, m0, 1
    paddw              xm0, xm1
    pshufd             xm1, xm0, q3232
    paddw              xm0, xm1
    pshuflw            xm1, xm0, q3232
    paddw              xm0, xm1
    pshuflw            xm1, xm0, q1111

    ; Perform normalization during the final stage of accumulation.
    pavgw              xm0, xm1
    movd               eax, xm0
    movzx              eax, ax
    sub                 ax, 8
    RET

; Rudimentary fast hadamard transform
; Two Hadamard transforms share an 128-bit lane.
%macro HADAMARD_4x4 0
    ; 4->0, 3->2, 2->3, 1->2, 0->1
    %define ROTATE SWAP 4, 3, 2, 1, 0

    ; Stage 1
    paddw               m0, m1, m2
    psubw               m1, m2
    paddw               m2, m3, m4
    psubw               m3, m4
    ROTATE

    ; Stage 2
    paddw               m0, m1, m3
    psubw               m1, m3
    paddw               m3, m2, m4
    psubw               m2, m4
    SWAP                3, 2, 1
    ROTATE

    ; Transpose
    ; Since two transforms share an 128-bit lane, unpacking results in a single
    ;  transform's values on each register. This has to be resolved later.
    ; A and B indicate different 4x4 transforms.

    ; Start
    ; m1  B (a3 a2 a1 a0) A (a3 a2 a1 a0)
    ; m2  B (b3 b2 b1 b0) A (b3 b2 b1 b0)
    ; m3  B (c3 c2 c1 c0) A (c3 c2 c1 c0)
    ; m4  B (d3 d2 d1 d0) A (d3 d2 d1 d0)

    ; Stage 1
    ; m1  A (b3 a3 b2 a2 b1 a1 b0 a0)
    ; m2  B (b3 a3 b2 a2 b1 a1 b0 a0)
    ; m3  A (d3 c3 d2 c2 d1 c1 d0 c0)
    ; m4  B (d3 c3 d2 c2 d1 c1 d0 c0)
    punpcklwd           m0, m1, m2
    punpckhwd           m1, m2
    punpcklwd           m2, m3, m4
    punpckhwd           m3, m4
    ROTATE

    ; m1  A (d3 c3 b3 a3 d2 c2 b2 a2)
    ; m2  A (d1 c1 b1 a1 d0 c0 b0 a0)
    ; m3  B (d3 c3 b3 a3 d2 c2 b2 a2)
    ; m4  B (d1 c1 b1 a1 d0 c0 b0 a0)
    punpckldq           m0, m1, m3
    punpckhdq           m1, m3
    punpckldq           m3, m2, m4
    punpckhdq           m2, m4
    SWAP                3, 2, 1
    ROTATE

    ; Make the transforms share 128-bit lanes again.
    ; m1  B (d0 c0 b0 a0) A (d0 c0 b0 a0)
    ; m2  B (d1 c1 b1 a1) A (d1 c1 b1 a1)
    ; m3  B (d2 c2 b2 a2) A (d2 c2 b2 a2)
    ; m4  B (d3 c3 b3 a3) A (d3 c3 b3 a3)
    punpcklqdq          m0, m1, m2
    punpckhqdq          m1, m2
    punpcklqdq          m2, m3, m4
    punpckhqdq          m3, m4
    ROTATE

    ; Stage 1
    paddw               m0, m1, m2
    psubw               m1, m2
    paddw               m2, m3, m4
    psubw               m3, m4
    ROTATE

    ; Use the fact that
    ;   (abs(a+b)+abs(a-b))/2 = max(abs(a),abs(b))
    ;  to merge the final butterfly with the abs and the first stage of
    ;  accumulation.
    ; Avoid pabsw by using max(a, b) + max(a + b + 0x7FFF, 0x7FFF) instead.
    ; Actually calculates (abs(a+b)+abs(a-b))/2-0x7FFF.
    ; The final sum must be offset to compensate for subtracting 0x7FFF.
    paddw               m0, m1, m3
    pmaxsw              m1, m3
    ; m2 is free
    ; 0x7FFF
    pcmpeqb             m3, m3
    psrlw               m3, 1

    paddsw              m0, m3
    psubw               m1, m0

    paddw               m0, m2, m4
    pmaxsw              m2, m4
    paddsw              m0, m3
    psubw               m2, m0

    paddw               m1, m2
    SWAP                1, 0
%endmacro

; Load diffs of 16 entries for 1 row
%macro LOAD_DIFF_DQ 4
    movu              xm%1, %2
    movu              xm%4, %3
    vpmovzxbw          m%1, xm%1
    vpmovzxbw          m%4, xm%4
    psubw              m%1, m%4
%endmacro

INIT_YMM avx2
cglobal satd_16x4, 4, 6, 5, src, src_stride, dst, dst_stride, \
                            src_stride3, dst_stride3
    lea       src_stride3q, [src_strideq*3]
    lea       dst_stride3q, [dst_strideq*3]
    LOAD_DIFF_DQ 1, [srcq], [dstq], 0
    LOAD_DIFF_DQ 2, [srcq+src_strideq*1], [dstq+dst_strideq*1], 0
    LOAD_DIFF_DQ 3, [srcq+src_strideq*2], [dstq+dst_strideq*2], 0
    LOAD_DIFF_DQ 4, [srcq+src_stride3q], [dstq+dst_stride3q], 0

    HADAMARD_4x4

    ; Reduce horizontally
    vextracti128       xm1, m0, 1
    paddw              xm0, xm1
    pshufd             xm1, xm0, q3232
    paddw              xm0, xm1
    pshuflw            xm1, xm0, q3232
    paddw              xm0, xm1
    pshuflw            xm1, xm0, q1111

    ; Perform normalization during the final stage of accumulation
    ; Avoids overflow in this case
    pavgw              xm0, xm1
    movd               eax, xm0
    movzx              eax, ax

    ; Add an offset for how the final butterfly stage and the first stage of
    ;  accumulation was done. Since this offset is an even number, this can
    ;  safely be done after normalization using pavgw.
    sub                 ax, 16
    RET

INIT_YMM avx2
cglobal satd_4x16, 4, 8, 7, src, src_stride, dst, dst_stride, \
                            src4, dst4, src_stride3, dst_stride3
    lea       src_stride3q, [src_strideq*3]
    lea       dst_stride3q, [dst_strideq*3]
    lea              src4q, [srcq+src_strideq*4]
    lea              dst4q, [dstq+dst_strideq*4]
    LOAD_PACK_DIFF_Dx4 0, [srcq], [dstq], \
                          [srcq+src_strideq*2], [dstq+dst_strideq*2], \
                          [src4q], [dst4q], \
                          [src4q+src_strideq*2], [dst4q+dst_strideq*2], \
                       4, 5, 6
    LOAD_PACK_DIFF_Dx4 1, [srcq+src_strideq*1], [dstq+dst_strideq*1], \
                          [srcq+src_stride3q], [dstq+dst_stride3q], \
                          [src4q+src_strideq*1], [dst4q+dst_strideq*1], \
                          [src4q+src_stride3q], [dst4q+dst_stride3q], \
                       4, 5, 6
    lea               srcq, [srcq+src_strideq*8]
    lea               dstq, [dstq+dst_strideq*8]
    lea              src4q, [src4q+src_strideq*8]
    lea              dst4q, [dst4q+dst_strideq*8]
    LOAD_PACK_DIFF_Dx4 2, [srcq], [dstq], \
                          [srcq+src_strideq*2], [dstq+dst_strideq*2], \
                          [src4q], [dst4q], \
                          [src4q+src_strideq*2], [dst4q+dst_strideq*2], \
                       4, 5, 6
    LOAD_PACK_DIFF_Dx4 3, [srcq+src_strideq*1], [dstq+dst_strideq*1], \
                          [srcq+src_stride3q], [dstq+dst_stride3q], \
                          [src4q+src_strideq*1], [dst4q+dst_strideq*1], \
                          [src4q+src_stride3q], [dst4q+dst_stride3q], \
                       4, 5, 6
    HADAMARD_4x4_PACKED 2

    ; Reduce horizontally
    vextracti128       xm1, m0, 1
    paddw              xm0, xm1
    pshufd             xm1, xm0, q3232
    paddw              xm0, xm1
    pshuflw            xm1, xm0, q3232
    paddw              xm0, xm1
    pshuflw            xm1, xm0, q1111

    ; Perform normalization during the final stage of accumulation
    pavgw              xm0, xm1
    movd               eax, xm0
    movzx              eax, ax

    ; Add an offset for how the final butterfly stage and the first stage of
    ;  accumulation was done. Since this offset is an even number, this can
    ;  safely be done after normalization using pavgw.
    sub                 ax, 16
    RET

; On x86-64 we can transpose in-place without spilling registers.
; By clever choices of the order to apply the butterflies and the order of
;  their outputs, we can take the rows in order and output the columns in order
;  without any extra operations and using just one temporary register.
%macro TRANSPOSE8x8 9
    punpckhwd           m%9, m%5, m%6
    punpcklwd           m%5, m%6
    ; m%6 is free
    punpckhwd           m%6, m%1, m%2
    punpcklwd           m%1, m%2
    ; m%2 is free
    punpckhwd           m%2, m%7, m%8
    punpcklwd           m%7, m%8
    ; m%8 is free
    punpckhwd           m%8, m%3, m%4
    punpcklwd           m%3, m%4
    ; m%4 is free
    punpckhdq           m%4, m%1, m%3
    punpckldq           m%1, m%3
    ; m%3 is free
    punpckldq           m%3, m%5, m%7
    punpckhdq           m%5, m%7
    ; m%7 is free
    punpckhdq           m%7, m%6, m%8
    punpckldq           m%6, m%8
    ; m%8 is free
    punpckldq           m%8, m%9, m%2
    punpckhdq           m%9, m%2
    ; m%2 is free
    punpckhqdq          m%2, m%1, m%3
    punpcklqdq          m%1, m%3
    ; m%3 is free
    punpcklqdq          m%3, m%4, m%5
    punpckhqdq          m%4, m%5
    ; m%5 is free
    punpcklqdq          m%5, m%6, m%8
    punpckhqdq          m%6, m%8
    ; m%8 is free
    punpckhqdq          m%8, m%7, m%9
    punpcklqdq          m%7, m%9
%endmacro

; Load diff of 8 entries for 1 row
%macro LOAD_DIFF_Q 4
    movq                %1, %2
    movq                %4, %3
    punpcklbw           %1, %4
    pmaddubsw           %1, hsub
%endmacro

%macro HADAMARD_8_STAGE_1 9
    paddw              m%9, m%1, m%2
    psubw              m%1, m%2
    paddw              m%2, m%3, m%4
    psubw              m%3, m%4
    paddw              m%4, m%5, m%6
    psubw              m%5, m%6
    paddw              m%6, m%7, m%8
    psubw              m%7, m%8
    ; 8->9, 7->8, 6->7, 5->6, 4->5, 3->4, 2->3, 1->2, 9->1
    SWAP                %8, %7, %6, %5, %4, %3, %2, %1, %9
%endmacro

%macro HADAMARD_8_STAGE_2 9
    paddw              m%9, m%1, m%3 ; 0
    psubw              m%1, m%3      ; 2
    paddw              m%3, m%2, m%4 ; 1
    psubw              m%2, m%4      ; 3
    SWAP                %3, %2, %1
    paddw              m%4, m%5, m%7 ; 4
    psubw              m%5, m%7      ; 6
    paddw              m%7, m%6, m%8 ; 5
    psubw              m%6, m%8      ; 7
    SWAP                %7, %6, %5
    ; 8->9, 7->8, 6->7, 5->6, 4->5, 3->4, 2->3, 1->2, 9->1
    SWAP                %8, %7, %6, %5, %4, %3, %2, %1, %9
%endmacro

%macro HADAMARD_8_STAGE_3 9
    paddw              m%9, m%1, m%5 ; 0
    psubw              m%1, m%5      ; 4
    paddw              m%5, m%2, m%6 ; 1
    psubw              m%2, m%6      ; 5
    paddw              m%6, m%3, m%7 ; 2
    psubw              m%3, m%7      ; 6
    paddw              m%7, m%4, m%8 ; 3
    psubw              m%4, m%8      ; 7
    SWAP                %5, %2, %6, %3, %7, %4, %1
    ; 8->9, 7->8, 6->7, 5->6, 4->5, 3->4, 2->3, 1->2, 9->1
    SWAP                %8, %7, %6, %5, %4, %3, %2, %1, %9
%endmacro

; Rudimentary fast hadamard transform
%macro HADAMARD_8x8 0
    HADAMARD_8_STAGE_1 1, 2, 3, 4, 5, 6, 7, 8, 0
    HADAMARD_8_STAGE_2 1, 2, 3, 4, 5, 6, 7, 8, 0
    HADAMARD_8_STAGE_3 1, 2, 3, 4, 5, 6, 7, 8, 0

    TRANSPOSE8x8 1, 2, 3, 4, 5, 6, 7, 8, 0

    HADAMARD_8_STAGE_1 1, 2, 3, 4, 5, 6, 7, 8, 0
    HADAMARD_8_STAGE_2 1, 2, 3, 4, 5, 6, 7, 8, 0

    ; Stage 3
    ; Use the fact that
    ;   (abs(a+b)+abs(a-b))/2 = max(abs(a),abs(b))
    ;  to merge the final butterfly with the abs and the first stage of
    ;  accumulation.
    ; Avoid pabsw by using max(a, b) + max(a + b + 0x7FFF, 0x7FFF) instead.
    ; Actually calculates (abs(a+b)+abs(a-b))/2-0x7FFF.
    ; The final sum must be offset to compensate for subtracting 0x7FFF.
    paddw               m0, m1, m5
    pmaxsw              m1, m5
    ; m1 is free
    ; 0x7FFF
    pcmpeqb             m5, m5
    psrlw               m5, 1

    paddsw              m0, m5
    psubw               m1, m0

    paddw               m0, m2, m6
    pmaxsw              m2, m6
    paddsw              m0, m5
    psubw               m2, m0

    paddw               m0, m3, m7
    pmaxsw              m3, m7
    paddsw              m0, m5
    psubw               m3, m0

    paddw               m0, m4, m8
    pmaxsw              m4, m8
    paddsw              m0, m5
    psubw               m4, m0

    paddw               m1, m2
    paddw               m3, m4

    paddw               m1, m3
    SWAP                 1, 0
%endmacro

; Only works with 128 bit vectors
%macro SATD_8x8_FN 0
cglobal satd_8x8, 4, 6, 10, src, src_stride, dst, dst_stride, \
                           src_stride3, dst_stride3
    %define           hsub  m0
    mova              hsub, [maddubsw_hsub]
    ; Load rows into m1-m8
    lea       src_stride3q, [src_strideq*3]
    lea       dst_stride3q, [dst_strideq*3]
    LOAD_DIFF_Q m1, [srcq], [dstq], m2
    LOAD_DIFF_Q m2, [srcq+src_strideq*1], [dstq+dst_strideq*1], m3
    LOAD_DIFF_Q m3, [srcq+src_strideq*2], [dstq+dst_strideq*2], m4
    LOAD_DIFF_Q m4, [srcq+src_stride3q], [dstq+dst_stride3q], m5
    lea               srcq, [srcq+src_strideq*4]
    lea               dstq, [dstq+dst_strideq*4]
    LOAD_DIFF_Q m5, [srcq], [dstq], m6
    LOAD_DIFF_Q m6, [srcq+src_strideq*1], [dstq+dst_strideq*1], m7
    LOAD_DIFF_Q m7, [srcq+src_strideq*2], [dstq+dst_strideq*2], m8
    LOAD_DIFF_Q m8, [srcq+src_stride3q], [dstq+dst_stride3q], m9

    HADAMARD_8x8

    ; Reduce horizontally and convert to 32 bits
    pxor                m2, m2
    punpcklwd           m1, m0, m2
    punpckhwd           m0, m2
    paddd               m0, m1

    pshufd              m1, m0, q3232
    paddd               m0, m1
    pshuflw             m1, m0, q3232
    paddd               m0, m1
    movd               eax, m0

    ; Normalize
    ; Add rounding offset and an offset for how the final butterfly stage and
    ;  the first stage of accumulation was done.
    sub                eax, 32-2
    shr                eax, 2
    RET
%endmacro

INIT_XMM ssse3
SATD_8x8_FN

INIT_XMM avx2
SATD_8x8_FN

INIT_YMM avx2
cglobal satd_16x8, 4, 6, 9, src, src_stride, dst, dst_stride, \
                            src_stride3, dst_stride3
    ; Load rows into m1-m8
    lea       src_stride3q, [src_strideq*3]
    lea       dst_stride3q, [dst_strideq*3]
    LOAD_DIFF_DQ 1, [srcq], [dstq], 0
    LOAD_DIFF_DQ 2, [srcq+src_strideq*1], [dstq+dst_strideq*1], 0
    LOAD_DIFF_DQ 3, [srcq+src_strideq*2], [dstq+dst_strideq*2], 0
    LOAD_DIFF_DQ 4, [srcq+src_stride3q], [dstq+dst_stride3q], 0
    lea               srcq, [srcq+src_strideq*4]
    lea               dstq, [dstq+dst_strideq*4]
    LOAD_DIFF_DQ 5, [srcq], [dstq], 0
    LOAD_DIFF_DQ 6, [srcq+src_strideq*1], [dstq+dst_strideq*1], 0
    LOAD_DIFF_DQ 7, [srcq+src_strideq*2], [dstq+dst_strideq*2], 0
    LOAD_DIFF_DQ 8, [srcq+src_stride3q], [dstq+dst_stride3q], 0

    HADAMARD_8x8

    ; Reduce horizontally and convert to 32 bits
    pxor                m2, m2
    punpcklwd           m1, m0, m2
    punpckhwd           m0, m2
    paddd               m0, m1

    vextracti128       xm1, m0, 1
    paddd              xm0, xm1
    pshufd             xm1, xm0, q3232
    paddd              xm0, xm1
    pshuflw            xm1, xm0, q3232
    paddd              xm0, xm1
    movd               eax, xm0

    ; Normalize
    ; Add rounding offset and an offset for how the final butterfly stage and
    ;  the first stage of accumulation was done.
    sub                eax, 64-2
    shr                eax, 2
    RET

%macro LOAD_DIFF_Qx2 7
    movq              xm%1, %2
    movq              xm%6, %3
    punpcklbw         xm%1, xm%6
    movq              xm%6, %4
    movq              xm%7, %5
    punpcklbw         xm%6, xm%7
    vinserti128        m%1, xm%6, 1
    pmaddubsw          m%1, hsub
%endmacro

INIT_YMM avx2
cglobal satd_8x16, 4, 8, 11, src, src_stride, dst, dst_stride, \
                             src8, dst8, src_stride3, dst_stride3
    %define           hsub  m0
    mova              hsub, [maddubsw_hsub]
    ; Load rows into m1-m8
    lea              src8q, [srcq+src_strideq*8]
    lea              dst8q, [dstq+dst_strideq*8]
    lea       src_stride3q, [src_strideq*3]
    lea       dst_stride3q, [dst_strideq*3]
    LOAD_DIFF_Qx2 1, [srcq], [dstq], \
                     [src8q], [dst8q], \
                     9, 10
    LOAD_DIFF_Qx2 2, [srcq+src_strideq*1], [dstq+dst_strideq*1], \
                     [src8q+src_strideq*1], [dst8q+dst_strideq*1], \
                     9, 10
    LOAD_DIFF_Qx2 3, [srcq+src_strideq*2], [dstq+dst_strideq*2], \
                     [src8q+src_strideq*2], [dst8q+dst_strideq*2], \
                     9, 10
    LOAD_DIFF_Qx2 4, [srcq+src_stride3q], [dstq+dst_stride3q], \
                     [src8q+src_stride3q], [dst8q+dst_stride3q], \
                     9, 10
    lea               srcq, [srcq+src_strideq*4]
    lea               dstq, [dstq+dst_strideq*4]
    lea              src8q, [src8q+src_strideq*4]
    lea              dst8q, [dst8q+dst_strideq*4]
    LOAD_DIFF_Qx2 5, [srcq], [dstq], \
                     [src8q], [dst8q], \
                     9, 10
    LOAD_DIFF_Qx2 6, [srcq+src_strideq*1], [dstq+dst_strideq*1], \
                     [src8q+src_strideq*1], [dst8q+dst_strideq*1], \
                     9, 10
    LOAD_DIFF_Qx2 7, [srcq+src_strideq*2], [dstq+dst_strideq*2], \
                     [src8q+src_strideq*2], [dst8q+dst_strideq*2], \
                     9, 10
    LOAD_DIFF_Qx2 8, [srcq+src_stride3q], [dstq+dst_stride3q], \
                     [src8q+src_stride3q], [dst8q+dst_stride3q], \
                     9, 10

    HADAMARD_8x8

    ; Reduce horizontally and convert to 32 bits
    pxor                m2, m2
    punpcklwd           m1, m0, m2
    punpckhwd           m0, m2
    paddd               m0, m1

    vextracti128       xm1, m0, 1
    paddd              xm0, xm1
    pshufd             xm1, xm0, q3232
    paddd              xm0, xm1
    pshuflw            xm1, xm0, q3232
    paddd              xm0, xm1
    movd               eax, xm0

    ; Normalize
    ; Add rounding offset and an offset for how the final butterfly stage and
    ;  the first stage of accumulation was done.
    sub                eax, 64-2
    shr                eax, 2
    RET

; Less optimized, boilerplate implementations

INIT_YMM avx2
cglobal satd_8x32, 4, 9, 13, src, src_stride, dst, dst_stride, \
                             src8, dst8, src_stride3, dst_stride3, cnt
    ; ones for converting to 32-bit with pmaddwd
    pcmpeqw            m11, m11
    pabsw              m11, m11
    ; sum
    pxor               m12, m12
    mov               cntd, 1
    lea       src_stride3q, [src_strideq*3]
    lea       dst_stride3q, [dst_strideq*3]
    lea              src8q, [srcq+src_strideq*8]
    lea              dst8q, [dstq+dst_strideq*8]
.loop:
    %define           hsub  m0
    mova              hsub, [maddubsw_hsub]
    ; Load rows into m1-m8
    LOAD_DIFF_Qx2 1, [srcq], [dstq], \
                     [src8q], [dst8q], \
                  9, 10
    LOAD_DIFF_Qx2 2, [srcq+src_strideq*1], [dstq+dst_strideq*1], \
                     [src8q+src_strideq*1], [dst8q+dst_strideq*1], \
                  9, 10
    LOAD_DIFF_Qx2 3, [srcq+src_strideq*2], [dstq+dst_strideq*2], \
                     [src8q+src_strideq*2], [dst8q+dst_strideq*2], \
                  9, 10
    LOAD_DIFF_Qx2 4, [srcq+src_stride3q], [dstq+dst_stride3q], \
                     [src8q+src_stride3q], [dst8q+dst_stride3q], \
                  9, 10
    lea               srcq, [srcq+src_strideq*4]
    lea               dstq, [dstq+dst_strideq*4]
    lea              src8q, [src8q+src_strideq*4]
    lea              dst8q, [dst8q+dst_strideq*4]
    LOAD_DIFF_Qx2 5, [srcq], [dstq], \
                     [src8q], [dst8q], \
                  9, 10
    LOAD_DIFF_Qx2 6, [srcq+src_strideq*1], [dstq+dst_strideq*1], \
                     [src8q+src_strideq*1], [dst8q+dst_strideq*1], \
                  9, 10
    LOAD_DIFF_Qx2 7, [srcq+src_strideq*2], [dstq+dst_strideq*2], \
                     [src8q+src_strideq*2], [dst8q+dst_strideq*2], \
                  9, 10
    LOAD_DIFF_Qx2 8, [srcq+src_stride3q], [dstq+dst_stride3q], \
                     [src8q+src_stride3q], [dst8q+dst_stride3q], \
                  9, 10

    HADAMARD_8x8

    ; Reduce horizontally and convert to 32 bits
    pmaddwd             m0, m11
    paddd              m12, m0

    lea               srcq, [srcq+src_stride3q*4]
    lea               dstq, [dstq+dst_stride3q*4]
    lea              src8q, [src8q+src_stride3q*4]
    lea              dst8q, [dst8q+dst_stride3q*4]
    dec               cntd
    jge .loop

    vextracti128       xm0, m12, 1
    paddd              xm0, xm12
    pshufd             xm1, xm0, q3232
    paddd              xm0, xm1
    pshuflw            xm1, xm0, q3232
    paddd              xm0, xm1
    movd               eax, xm0

    ; Normalize
    ; Add rounding offset and an offset for how the final butterfly stage and
    ;  the first stage of accumulation was done.
    sub                eax, 128-2
    shr                eax, 2
    RET

INIT_YMM avx2
cglobal satd_16x8_internal, 0, 0, 0, \
                            dummy1, src_stride, dummy2, dst_stride, \
                            src_stride3, dst_stride3, src, dst
    %define hadd m9
    %define sum m10
    ; Load rows into m1-m8
    LOAD_DIFF_DQ 1, [srcq], [dstq], 0
    LOAD_DIFF_DQ 2, [srcq+src_strideq*1], [dstq+dst_strideq*1], 0
    LOAD_DIFF_DQ 3, [srcq+src_strideq*2], [dstq+dst_strideq*2], 0
    LOAD_DIFF_DQ 4, [srcq+src_stride3q], [dstq+dst_stride3q], 0
    lea               srcq, [srcq+src_strideq*4]
    lea               dstq, [dstq+dst_strideq*4]
    LOAD_DIFF_DQ 5, [srcq], [dstq], 0
    LOAD_DIFF_DQ 6, [srcq+src_strideq*1], [dstq+dst_strideq*1], 0
    LOAD_DIFF_DQ 7, [srcq+src_strideq*2], [dstq+dst_strideq*2], 0
    LOAD_DIFF_DQ 8, [srcq+src_stride3q], [dstq+dst_stride3q], 0

    HADAMARD_8x8

    pmaddwd             m0, hadd
    paddd              sum, m0
    ret

%macro SATD_NXM 2
%if %1 > 16
%if %2 > 8
cglobal satd_%1x%2, 4, 10, 11, src, src_stride, dst, dst_stride, \
                              src_stride3, dst_stride3, call_src, call_dst, \
                              w, h
%else
cglobal satd_%1x%2, 4, 9, 11, src, src_stride, dst, dst_stride, \
                              src_stride3, dst_stride3, call_src, call_dst, \
                              w
%endif
%else ; %2 > 8
cglobal satd_%1x%2, 4, 9, 11, src, src_stride, dst, dst_stride, \
                              src_stride3, dst_stride3, call_src, call_dst, \
                              h
%endif
    ; ones for converting to 32-bit with pmaddwd
    pcmpeqw             m9, m9
    pabsw               m9, m9
    ; sum
    pxor               m10, m10
    lea       src_stride3q, [src_strideq*3]
    lea       dst_stride3q, [dst_strideq*3]
%if %2 > 8
    mov                 hd, %2/8 - 1
.looph:
%endif
%if %1 > 16
    mov                 wd, %1/16 - 1
.loopv:
%endif
    mov          call_srcq, srcq
    mov          call_dstq, dstq
    call m(satd_16x8_internal)
%if %1 > 16
    add               srcq, 16
    add               dstq, 16
    dec                 wd
    jge .loopv
    sub               srcq, %1
    sub               dstq, %1
%endif
%if %2 > 8
    lea               srcq, [srcq+src_strideq*8]
    lea               dstq, [dstq+dst_strideq*8]
    dec                 hd
    jge .looph
%endif

    ; Reduce horizontally
    vextracti128       xm0, m10, 1
    paddd              xm0, xm10
    pshufd             xm1, xm0, q3232
    paddd              xm0, xm1
    pshuflw            xm1, xm0, q3232
    paddd              xm0, xm1
    movd               eax, xm0

    ; Normalize
    ; Add rounding offset and an offset for how the final butterfly stage and
    ;  the first stage of accumulation was done.
    sub                eax, %1*%2/2 - 2
    shr                eax, 2
    RET
%endmacro

INIT_YMM avx2
SATD_NXM 16, 16
SATD_NXM 32, 32
SATD_NXM 64, 64
SATD_NXM 128, 128

SATD_NXM 16, 32
SATD_NXM 32, 16
SATD_NXM 32, 64
SATD_NXM 64, 32
SATD_NXM 64, 128
SATD_NXM 128, 64

SATD_NXM 32, 8
SATD_NXM 16, 64
SATD_NXM 64, 16

%endif ; ARCH_X86_64