summaryrefslogtreecommitdiffstats
path: root/third_party/dav1d/src/x86/cdef16_sse.asm
blob: 1bd67ace64d0970c9d7426f29c65e816ad70a29c (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
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
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
; Copyright © 2021, VideoLAN and dav1d authors
; Copyright © 2021, Two Orioles, LLC
; Copyright (c) 2017-2021, The rav1e contributors
; Copyright (c) 2021, Nathan Egge
; All rights reserved.
;
; Redistribution and use in source and binary forms, with or without
; modification, are permitted provided that the following conditions are met:
;
; 1. Redistributions of source code must retain the above copyright notice, this
;    list of conditions and the following disclaimer.
;
; 2. Redistributions in binary form must reproduce the above copyright notice,
;    this list of conditions and the following disclaimer in the documentation
;    and/or other materials provided with the distribution.
;
; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
; ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
; WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
; DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
; ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
; (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
; ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
; (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

%include "config.asm"
%include "ext/x86/x86inc.asm"

SECTION_RODATA

%macro DUP8 1-*
    %rep %0
        times 8 dw %1
        %rotate 1
    %endrep
%endmacro

pri_taps:  DUP8 4, 2, 3, 3
dir_table: db  1 * 32 + 0,  2 * 32 + 0
           db  1 * 32 + 0,  2 * 32 - 2
           db -1 * 32 + 2, -2 * 32 + 4
           db  0 * 32 + 2, -1 * 32 + 4
           db  0 * 32 + 2,  0 * 32 + 4
           db  0 * 32 + 2,  1 * 32 + 4
           db  1 * 32 + 2,  2 * 32 + 4
           db  1 * 32 + 0,  2 * 32 + 2
           db  1 * 32 + 0,  2 * 32 + 0
           db  1 * 32 + 0,  2 * 32 - 2
           db -1 * 32 + 2, -2 * 32 + 4
           db  0 * 32 + 2, -1 * 32 + 4

dir_shift: times 4 dw 0x4000
           times 4 dw 0x1000

pw_128:    times 4 dw 128
pw_2048:   times 8 dw 2048
pw_m16384: times 8 dw -16384

cextern cdef_dir_8bpc_ssse3.main
cextern cdef_dir_8bpc_sse4.main
cextern shufw_6543210x

SECTION .text

%if ARCH_X86_32
DECLARE_REG_TMP 5, 3
%elif WIN64
DECLARE_REG_TMP 8, 4
%else
DECLARE_REG_TMP 8, 6
%endif

%macro CDEF_FILTER 2 ; w, h
%if ARCH_X86_64
    DEFINE_ARGS dst, stride, _, tmp, pridmp, pri, sec, dir
    mova            m8, [base+pw_2048]
%else
    DEFINE_ARGS dst, pridmp, tmp, sec, pri, _, dir
    %define         m8  [base+pw_2048]
    %define         m9  [rsp+16*1+gprsize]
    %define        m10  [rsp+16*2+gprsize]
%endif
    movifnidn     prid, r5m
    movifnidn     secd, r6m
    test          prid, prid
    jz .sec_only
    movd            m6, r5m
%if ARCH_X86_32
    mov       [rsp+24], pridmpd
%endif
    bsr        pridmpd, prid
    lea           tmpd, [priq*4]
    cmp     dword r10m, 0x3ff ; if (bpc == 10)
    cmove         prid, tmpd  ;     pri <<= 2
    mov           tmpd, r8m   ; damping
    mov           dird, r7m
    and           prid, 16
    pshufb          m6, m7    ; splat
    lea           dirq, [base+dir_table+dirq*2]
    lea           priq, [base+pri_taps+priq*2]
    test          secd, secd
    jz .pri_only
    mova         [rsp], m6
    movd            m6, secd
    tzcnt         secd, secd
    sub        pridmpd, tmpd
    sub           tmpd, secd
    pshufb          m6, m7
    xor           secd, secd
    neg        pridmpd
    cmovs      pridmpd, secd
%if ARCH_X86_32
    mov  [pri_shift+4], secd
    mov  [sec_shift+4], secd
%endif
    mov  [pri_shift+0], pridmpq
    mov  [sec_shift+0], tmpq
    lea           tmpq, [px]
%if WIN64
    movaps         r4m, m9
    movaps         r6m, m10
%elif ARCH_X86_32
    mov        pridmpd, [rsp+24]
%endif
%rep %1*%2/8
    call mangle(private_prefix %+ _cdef_filter_%1x%1_16bpc %+ SUFFIX).pri_sec
%endrep
%if WIN64
    movaps          m9, r4m
    movaps         m10, r6m
%endif
    jmp .end
.pri_only:
    sub           tmpd, pridmpd
    cmovs         tmpd, secd
%if ARCH_X86_32
    mov        pridmpd, [rsp+24]
    mov  [pri_shift+4], secd
%endif
    mov  [pri_shift+0], tmpq
    lea           tmpq, [px]
%rep %1*%2/8
    call mangle(private_prefix %+ _cdef_filter_%1x%1_16bpc %+ SUFFIX).pri
%endrep
.end:
    RET
.sec_only:
    mov           tmpd, r8m ; damping
    movd            m6, r6m
    tzcnt         secd, secd
    mov           dird, r7m
    pshufb          m6, m7
    sub           tmpd, secd
    lea           dirq, [base+dir_table+dirq*2]
%if ARCH_X86_32
    mov  [sec_shift+4], prid
%endif
    mov  [sec_shift+0], tmpq
    lea           tmpq, [px]
%rep %1*%2/8
    call mangle(private_prefix %+ _cdef_filter_%1x%1_16bpc %+ SUFFIX).sec
%endrep
    jmp .end
%if %1 == %2
 %if ARCH_X86_64
  DEFINE_ARGS dst, stride, _, tmp, off, pri, _, dir
 %else
  DEFINE_ARGS dst, stride, tmp, off, pri, _, dir
 %endif
ALIGN function_align
.pri:
    movsx         offq, byte [dirq+4]    ; off_k0
%if %1 == 4
    movq            m1, [dstq+strideq*0]
    movhps          m1, [dstq+strideq*1]
    movq            m2, [tmpq+offq+32*0] ; k0p0
    movhps          m2, [tmpq+offq+32*1]
    neg           offq
    movq            m3, [tmpq+offq+32*0] ; k0p1
    movhps          m3, [tmpq+offq+32*1]
%else
    mova            m1, [dstq]
    movu            m2, [tmpq+offq]
    neg           offq
    movu            m3, [tmpq+offq]
%endif
    movsx         offq, byte [dirq+5]    ; off_k1
    psubw           m2, m1               ; diff_k0p0
    psubw           m3, m1               ; diff_k0p1
    pabsw           m4, m2               ; adiff_k0p0
    psrlw           m5, m4, [pri_shift+gprsize]
    psubusw         m0, m6, m5
    pabsw           m5, m3               ; adiff_k0p1
    pminsw          m0, m4
    psrlw           m4, m5, [pri_shift+gprsize]
    psignw          m0, m2               ; constrain(diff_k0p0)
    psubusw         m2, m6, m4
    pminsw          m2, m5
%if %1 == 4
    movq            m4, [tmpq+offq+32*0] ; k1p0
    movhps          m4, [tmpq+offq+32*1]
    neg           offq
    movq            m5, [tmpq+offq+32*0] ; k1p1
    movhps          m5, [tmpq+offq+32*1]
%else
    movu            m4, [tmpq+offq]
    neg           offq
    movu            m5, [tmpq+offq]
%endif
    psubw           m4, m1               ; diff_k1p0
    psubw           m5, m1               ; diff_k1p1
    psignw          m2, m3               ; constrain(diff_k0p1)
    pabsw           m3, m4               ; adiff_k1p0
    paddw           m0, m2               ; constrain(diff_k0)
    psrlw           m2, m3, [pri_shift+gprsize]
    psubusw         m7, m6, m2
    pabsw           m2, m5               ; adiff_k1p1
    pminsw          m7, m3
    psrlw           m3, m2, [pri_shift+gprsize]
    psignw          m7, m4               ; constrain(diff_k1p0)
    psubusw         m4, m6, m3
    pminsw          m4, m2
    psignw          m4, m5               ; constrain(diff_k1p1)
    paddw           m7, m4               ; constrain(diff_k1)
    pmullw          m0, [priq+16*0]      ; pri_tap_k0
    pmullw          m7, [priq+16*1]      ; pri_tap_k1
    paddw           m0, m7               ; sum
    psraw           m2, m0, 15
    paddw           m0, m2
    pmulhrsw        m0, m8
    paddw           m0, m1
%if %1 == 4
    add           tmpq, 32*2
    movq   [dstq+strideq*0], m0
    movhps [dstq+strideq*1], m0
    lea           dstq, [dstq+strideq*2]
%else
    add           tmpq, 32
    mova        [dstq], m0
    add           dstq, strideq
%endif
    ret
ALIGN function_align
.sec:
    movsx         offq, byte [dirq+8]    ; off1_k0
%if %1 == 4
    movq            m1, [dstq+strideq*0]
    movhps          m1, [dstq+strideq*1]
    movq            m2, [tmpq+offq+32*0] ; k0s0
    movhps          m2, [tmpq+offq+32*1]
    neg           offq
    movq            m3, [tmpq+offq+32*0] ; k0s1
    movhps          m3, [tmpq+offq+32*1]
%else
    mova            m1, [dstq]
    movu            m2, [tmpq+offq]
    neg           offq
    movu            m3, [tmpq+offq]
%endif
    movsx         offq, byte [dirq+0]    ; off2_k0
    psubw           m2, m1               ; diff_k0s0
    psubw           m3, m1               ; diff_k0s1
    pabsw           m4, m2               ; adiff_k0s0
    psrlw           m5, m4, [sec_shift+gprsize]
    psubusw         m0, m6, m5
    pabsw           m5, m3               ; adiff_k0s1
    pminsw          m0, m4
    psrlw           m4, m5, [sec_shift+gprsize]
    psignw          m0, m2               ; constrain(diff_k0s0)
    psubusw         m2, m6, m4
    pminsw          m2, m5
%if %1 == 4
    movq            m4, [tmpq+offq+32*0] ; k0s2
    movhps          m4, [tmpq+offq+32*1]
    neg           offq
    movq            m5, [tmpq+offq+32*0] ; k0s3
    movhps          m5, [tmpq+offq+32*1]
%else
    movu            m4, [tmpq+offq]
    neg           offq
    movu            m5, [tmpq+offq]
%endif
    movsx         offq, byte [dirq+9]    ; off1_k1
    psubw           m4, m1               ; diff_k0s2
    psubw           m5, m1               ; diff_k0s3
    psignw          m2, m3               ; constrain(diff_k0s1)
    pabsw           m3, m4               ; adiff_k0s2
    paddw           m0, m2
    psrlw           m2, m3, [sec_shift+gprsize]
    psubusw         m7, m6, m2
    pabsw           m2, m5               ; adiff_k0s3
    pminsw          m7, m3
    psrlw           m3, m2, [sec_shift+gprsize]
    psignw          m7, m4               ; constrain(diff_k0s2)
    psubusw         m4, m6, m3
    pminsw          m4, m2
%if %1 == 4
    movq            m2, [tmpq+offq+32*0] ; k1s0
    movhps          m2, [tmpq+offq+32*1]
    neg           offq
    movq            m3, [tmpq+offq+32*0] ; k1s1
    movhps          m3, [tmpq+offq+32*1]
%else
    movu            m2, [tmpq+offq]
    neg           offq
    movu            m3, [tmpq+offq]
%endif
    movsx         offq, byte [dirq+1]    ; off2_k1
    paddw           m0, m7
    psignw          m4, m5               ; constrain(diff_k0s3)
    paddw           m0, m4               ; constrain(diff_k0)
    psubw           m2, m1               ; diff_k1s0
    psubw           m3, m1               ; diff_k1s1
    paddw           m0, m0               ; sec_tap_k0
    pabsw           m4, m2               ; adiff_k1s0
    psrlw           m5, m4, [sec_shift+gprsize]
    psubusw         m7, m6, m5
    pabsw           m5, m3               ; adiff_k1s1
    pminsw          m7, m4
    psrlw           m4, m5, [sec_shift+gprsize]
    psignw          m7, m2               ; constrain(diff_k1s0)
    psubusw         m2, m6, m4
    pminsw          m2, m5
%if %1 == 4
    movq            m4, [tmpq+offq+32*0] ; k1s2
    movhps          m4, [tmpq+offq+32*1]
    neg           offq
    movq            m5, [tmpq+offq+32*0] ; k1s3
    movhps          m5, [tmpq+offq+32*1]
%else
    movu            m4, [tmpq+offq]
    neg           offq
    movu            m5, [tmpq+offq]
%endif
    paddw           m0, m7
    psubw           m4, m1               ; diff_k1s2
    psubw           m5, m1               ; diff_k1s3
    psignw          m2, m3               ; constrain(diff_k1s1)
    pabsw           m3, m4               ; adiff_k1s2
    paddw           m0, m2
    psrlw           m2, m3, [sec_shift+gprsize]
    psubusw         m7, m6, m2
    pabsw           m2, m5               ; adiff_k1s3
    pminsw          m7, m3
    psrlw           m3, m2, [sec_shift+gprsize]
    psignw          m7, m4               ; constrain(diff_k1s2)
    psubusw         m4, m6, m3
    pminsw          m4, m2
    paddw           m0, m7
    psignw          m4, m5               ; constrain(diff_k1s3)
    paddw           m0, m4               ; sum
    psraw           m2, m0, 15
    paddw           m0, m2
    pmulhrsw        m0, m8
    paddw           m0, m1
%if %1 == 4
    add           tmpq, 32*2
    movq   [dstq+strideq*0], m0
    movhps [dstq+strideq*1], m0
    lea           dstq, [dstq+strideq*2]
%else
    add           tmpq, 32
    mova        [dstq], m0
    add           dstq, strideq
%endif
    ret
ALIGN function_align
.pri_sec:
    movsx         offq, byte [dirq+8]    ; off2_k0
%if %1 == 4
    movq            m1, [dstq+strideq*0]
    movhps          m1, [dstq+strideq*1]
    movq            m2, [tmpq+offq+32*0] ; k0s0
    movhps          m2, [tmpq+offq+32*1]
    neg           offq
    movq            m3, [tmpq+offq+32*0] ; k0s1
    movhps          m3, [tmpq+offq+32*1]
%else
    mova            m1, [dstq]
    movu            m2, [tmpq+offq]
    neg           offq
    movu            m3, [tmpq+offq]
%endif
    movsx         offq, byte [dirq+0]    ; off3_k0
    pabsw           m4, m2
%if ARCH_X86_64
    pabsw          m10, m3
    pmaxsw          m9, m2, m3
    pminsw         m10, m4
%else
    pabsw           m7, m3
    pmaxsw          m5, m2, m3
    pminsw          m4, m7
    mova            m9, m5
    mova           m10, m4
%endif
    psubw           m2, m1               ; diff_k0s0
    psubw           m3, m1               ; diff_k0s1
    pabsw           m4, m2               ; adiff_k0s0
    psrlw           m5, m4, [sec_shift+gprsize]
    psubusw         m0, m6, m5
    pabsw           m5, m3               ; adiff_k0s1
    pminsw          m0, m4
    psrlw           m4, m5, [sec_shift+gprsize]
    psignw          m0, m2               ; constrain(diff_k0s0)
    psubusw         m2, m6, m4
    pminsw          m2, m5
%if %1 == 4
    movq            m4, [tmpq+offq+32*0] ; k0s2
    movhps          m4, [tmpq+offq+32*1]
    neg           offq
    movq            m5, [tmpq+offq+32*0] ; k0s3
    movhps          m5, [tmpq+offq+32*1]
%else
    movu            m4, [tmpq+offq]
    neg           offq
    movu            m5, [tmpq+offq]
%endif
    movsx         offq, byte [dirq+9]    ; off2_k1
    pabsw           m7, m4
    psignw          m2, m3
    pabsw           m3, m5               ; constrain(diff_k0s1)
%if ARCH_X86_64
    pmaxsw          m9, m4
    pminsw         m10, m7
    pmaxsw          m9, m5
    pminsw         m10, m3
%else
    pminsw          m7, m10
    pminsw          m7, m3
    pmaxsw          m3, m9, m4
    pmaxsw          m3, m5
    mova           m10, m7
    mova            m9, m3
%endif
    psubw           m4, m1               ; diff_k0s2
    psubw           m5, m1               ; diff_k0s3
    paddw           m0, m2
    pabsw           m3, m4               ; adiff_k0s2
    psrlw           m2, m3, [sec_shift+gprsize]
    psubusw         m7, m6, m2
    pabsw           m2, m5               ; adiff_k0s3
    pminsw          m7, m3
    psrlw           m3, m2, [sec_shift+gprsize]
    psignw          m7, m4               ; constrain(diff_k0s2)
    psubusw         m4, m6, m3
    pminsw          m4, m2
%if %1 == 4
    movq            m2, [tmpq+offq+32*0] ; k1s0
    movhps          m2, [tmpq+offq+32*1]
    neg           offq
    movq            m3, [tmpq+offq+32*0] ; k1s1
    movhps          m3, [tmpq+offq+32*1]
%else
    movu            m2, [tmpq+offq]
    neg           offq
    movu            m3, [tmpq+offq]
%endif
    movsx         offq, byte [dirq+1]    ; off3_k1
    paddw           m0, m7
    pabsw           m7, m2
    psignw          m4, m5               ; constrain(diff_k0s3)
    pabsw           m5, m3
%if ARCH_X86_64
    pmaxsw          m9, m2
    pminsw         m10, m7
    pmaxsw          m9, m3
    pminsw         m10, m5
%else
    pminsw          m7, m10
    pminsw          m7, m5
    pmaxsw          m5, m9, m2
    pmaxsw          m5, m3
    mova           m10, m7
    mova            m9, m5
%endif
    paddw           m0, m4               ; constrain(diff_k0)
    psubw           m2, m1               ; diff_k1s0
    psubw           m3, m1               ; diff_k1s1
    paddw           m0, m0               ; sec_tap_k0
    pabsw           m4, m2               ; adiff_k1s0
    psrlw           m5, m4, [sec_shift+gprsize]
    psubusw         m7, m6, m5
    pabsw           m5, m3               ; adiff_k1s1
    pminsw          m7, m4
    psrlw           m4, m5, [sec_shift+gprsize]
    psignw          m7, m2               ; constrain(diff_k1s0)
    psubusw         m2, m6, m4
    pminsw          m2, m5
%if %1 == 4
    movq            m4, [tmpq+offq+32*0] ; k1s2
    movhps          m4, [tmpq+offq+32*1]
    neg           offq
    movq            m5, [tmpq+offq+32*0] ; k1s3
    movhps          m5, [tmpq+offq+32*1]
%else
    movu            m4, [tmpq+offq]
    neg           offq
    movu            m5, [tmpq+offq]
%endif
    movsx         offq, byte [dirq+4]    ; off1_k0
    paddw           m0, m7
    pabsw           m7, m4
    psignw          m2, m3               ; constrain(diff_k1s1)
    pabsw           m3, m5
%if ARCH_X86_64
    pmaxsw          m9, m4
    pminsw         m10, m7
    pmaxsw          m9, m5
    pminsw         m10, m3
%else
    pminsw          m7, m10
    pminsw          m7, m3
    pmaxsw          m3, m9, m4
    pmaxsw          m3, m5
    mova           m10, m7
    mova            m9, m3
%endif
    psubw           m4, m1               ; diff_k1s2
    psubw           m5, m1               ; diff_k1s3
    pabsw           m3, m4               ; adiff_k1s2
    paddw           m0, m2
    psrlw           m2, m3, [sec_shift+gprsize]
    psubusw         m7, m6, m2
    pabsw           m2, m5               ; adiff_k1s3
    pminsw          m7, m3
    psrlw           m3, m2, [sec_shift+gprsize]
    psignw          m7, m4               ; constrain(diff_k1s2)
    psubusw         m4, m6, m3
    pminsw          m4, m2
    paddw           m0, m7
%if %1 == 4
    movq            m2, [tmpq+offq+32*0] ; k0p0
    movhps          m2, [tmpq+offq+32*1]
    neg           offq
    movq            m3, [tmpq+offq+32*0] ; k0p1
    movhps          m3, [tmpq+offq+32*1]
%else
    movu            m2, [tmpq+offq]
    neg           offq
    movu            m3, [tmpq+offq]
%endif
    movsx         offq, byte [dirq+5]    ; off1_k1
    pabsw           m7, m2
    psignw          m4, m5               ; constrain(diff_k1s3)
    pabsw           m5, m3
%if ARCH_X86_64
    pmaxsw          m9, m2
    pminsw         m10, m7
    pmaxsw          m9, m3
    pminsw         m10, m5
%else
    pminsw          m7, m10
    pminsw          m7, m5
    pmaxsw          m5, m9, m2
    pmaxsw          m5, m3
    mova           m10, m7
    mova            m9, m5
%endif
    psubw           m2, m1               ; diff_k0p0
    psubw           m3, m1               ; diff_k0p1
    paddw           m0, m4
    pabsw           m4, m2               ; adiff_k0p0
    psrlw           m5, m4, [pri_shift+gprsize]
    psubusw         m7, [rsp+gprsize], m5
    pabsw           m5, m3               ; adiff_k0p1
    pminsw          m7, m4
    psrlw           m4, m5, [pri_shift+gprsize]
    psignw          m7, m2               ; constrain(diff_k0p0)
    psubusw         m2, [rsp+gprsize], m4
    pminsw          m2, m5
%if %1 == 4
    movq            m4, [tmpq+offq+32*0] ; k1p0
    movhps          m4, [tmpq+offq+32*1]
    neg           offq
    movq            m5, [tmpq+offq+32*0] ; k1p1
    movhps          m5, [tmpq+offq+32*1]
%else
    movu            m4, [tmpq+offq]
    neg           offq
    movu            m5, [tmpq+offq]
%endif
    psignw          m2, m3               ; constrain(diff_k0p1)
    pabsw           m3, m4
    paddw           m7, m2               ; constrain(diff_k0)
    pabsw           m2, m5
%if ARCH_X86_64
    pmaxsw          m9, m4
    pminsw         m10, m3
    pmaxsw          m9, m5
    pminsw         m10, m2
%else
    pminsw          m3, m10
    pminsw          m3, m2
    pmaxsw          m2, m9, m4
    pmaxsw          m2, m5
    mova           m10, m3
    mova            m9, m2
%endif
    psubw           m4, m1               ; diff_k1p0
    psubw           m5, m1               ; diff_k1p1
    pabsw           m3, m4               ; adiff_k1p0
    pmullw          m7, [priq+16*0]      ; pri_tap_k0
    paddw           m0, m7
    psrlw           m2, m3, [pri_shift+gprsize]
    psubusw         m7, [rsp+16*0+gprsize], m2
    pabsw           m2, m5               ; adiff_k1p1
    pminsw          m7, m3
    psrlw           m3, m2, [pri_shift+gprsize]
    psignw          m7, m4               ; constrain(diff_k1p0)
    psubusw         m4, [rsp+16*0+gprsize], m3
    pminsw          m4, m2
    psignw          m4, m5               ; constrain(diff_k1p1)
    paddw           m7, m4               ; constrain(diff_k1)
    pmullw          m7, [priq+16*1]      ; pri_tap_k1
    paddw           m0, m7               ; sum
    psraw           m2, m0, 15
    paddw           m0, m2
    pmulhrsw        m0, m8
    paddw           m0, m1
%if ARCH_X86_64
    pmaxsw          m9, m1
    pminsw          m0, m9
%else
    pmaxsw          m2, m9, m1
    pminsw          m0, m2
%endif
    pminsw          m1, m10
    pmaxsw          m0, m1
%if %1 == 4
    add           tmpq, 32*2
    movq   [dstq+strideq*0], m0
    movhps [dstq+strideq*1], m0
    lea           dstq, [dstq+strideq*2]
%else
    add           tmpq, 32
    mova        [dstq], m0
    add           dstq, strideq
%endif
    ret
%endif
%endmacro

INIT_XMM ssse3
%if ARCH_X86_64
cglobal cdef_filter_4x4_16bpc, 5, 9, 9, 32*10, dst, stride, left, top, bot, \
                                               pri, sec, edge
    %define         px  rsp+32*4
%else
cglobal cdef_filter_4x4_16bpc, 2, 7, 8, -32*11, dst, stride, edge, top, left
    %define       botq  topq
    %define         px  rsp+32*5
%endif
    %define       base  t0-dir_table
    %define  pri_shift  px-16*6
    %define  sec_shift  px-16*5
    mov          edged, r9m
    LEA             t0, dir_table
    movu            m0, [dstq+strideq*0]
    movu            m1, [dstq+strideq*1]
    lea             t1, [dstq+strideq*2]
    movu            m2, [t1  +strideq*0]
    movu            m3, [t1  +strideq*1]
    movddup         m7, [base+pw_m16384]
    mova   [px+32*0+0], m0
    mova   [px+32*1+0], m1
    mova   [px+32*2+0], m2
    mova   [px+32*3+0], m3
    test         edgeb, 4 ; HAVE_TOP
    jz .no_top
    movifnidn     topq, topmp
    movu            m0, [topq+strideq*0]
    movu            m1, [topq+strideq*1]
    mova   [px-32*2+0], m0
    mova   [px-32*1+0], m1
    test         edgeb, 1 ; HAVE_LEFT
    jz .top_no_left
    movd            m0, [topq+strideq*0-4]
    movd            m1, [topq+strideq*1-4]
    movd   [px-32*2-4], m0
    movd   [px-32*1-4], m1
    jmp .top_done
.no_top:
    mova   [px-32*2+0], m7
    mova   [px-32*1+0], m7
.top_no_left:
    movd   [px-32*2-4], m7
    movd   [px-32*1-4], m7
.top_done:
    test         edgeb, 8 ; HAVE_BOTTOM
    jz .no_bottom
    movifnidn     botq, r4mp
    movu            m0, [botq+strideq*0]
    movu            m1, [botq+strideq*1]
    mova   [px+32*4+0], m0
    mova   [px+32*5+0], m1
    test         edgeb, 1 ; HAVE_LEFT
    jz .bottom_no_left
    movd            m0, [botq+strideq*0-4]
    movd            m1, [botq+strideq*1-4]
    movd   [px+32*4-4], m0
    movd   [px+32*5-4], m1
    jmp .bottom_done
.no_bottom:
    mova   [px+32*4+0], m7
    mova   [px+32*5+0], m7
.bottom_no_left:
    movd   [px+32*4-4], m7
    movd   [px+32*5-4], m7
.bottom_done:
    test         edgeb, 1 ; HAVE_LEFT
    jz .no_left
    movifnidn    leftq, r2mp
    movd            m0, [leftq+4*0]
    movd            m1, [leftq+4*1]
    movd            m2, [leftq+4*2]
    movd            m3, [leftq+4*3]
    movd   [px+32*0-4], m0
    movd   [px+32*1-4], m1
    movd   [px+32*2-4], m2
    movd   [px+32*3-4], m3
    jmp .left_done
.no_left:
    REPX {movd [px+32*x-4], m7}, 0, 1, 2, 3
.left_done:
    test         edgeb, 2 ; HAVE_RIGHT
    jnz .padding_done
    REPX {movd [px+32*x+8], m7}, -2, -1, 0, 1, 2, 3, 4, 5
.padding_done:
    CDEF_FILTER      4, 4

%if ARCH_X86_64
cglobal cdef_filter_4x8_16bpc, 5, 9, 9, 32*14, dst, stride, left, top, bot, \
                                               pri, sec, edge
%else
cglobal cdef_filter_4x8_16bpc, 2, 7, 8, -32*15, dst, stride, edge, top, left
%endif
    mov          edged, r9m
    LEA             t0, dir_table
    movu            m0, [dstq+strideq*0]
    movu            m1, [dstq+strideq*1]
    lea             t1, [dstq+strideq*2]
    movu            m2, [t1  +strideq*0]
    movu            m3, [t1  +strideq*1]
    lea             t1, [t1  +strideq*2]
    movu            m4, [t1  +strideq*0]
    movu            m5, [t1  +strideq*1]
    lea             t1, [t1  +strideq*2]
    movu            m6, [t1  +strideq*0]
    movu            m7, [t1  +strideq*1]
    mova   [px+32*0+0], m0
    mova   [px+32*1+0], m1
    mova   [px+32*2+0], m2
    mova   [px+32*3+0], m3
    mova   [px+32*4+0], m4
    mova   [px+32*5+0], m5
    mova   [px+32*6+0], m6
    mova   [px+32*7+0], m7
    movddup         m7, [base+pw_m16384]
    test         edgeb, 4 ; HAVE_TOP
    jz .no_top
    movifnidn     topq, topmp
    movu            m0, [topq+strideq*0]
    movu            m1, [topq+strideq*1]
    mova   [px-32*2+0], m0
    mova   [px-32*1+0], m1
    test         edgeb, 1 ; HAVE_LEFT
    jz .top_no_left
    movd            m0, [topq+strideq*0-4]
    movd            m1, [topq+strideq*1-4]
    movd   [px-32*2-4], m0
    movd   [px-32*1-4], m1
    jmp .top_done
.no_top:
    mova   [px-32*2+0], m7
    mova   [px-32*1+0], m7
.top_no_left:
    movd   [px-32*2-4], m7
    movd   [px-32*1-4], m7
.top_done:
    test         edgeb, 8 ; HAVE_BOTTOM
    jz .no_bottom
    movifnidn     botq, r4mp
    movu            m0, [botq+strideq*0]
    movu            m1, [botq+strideq*1]
    mova   [px+32*8+0], m0
    mova   [px+32*9+0], m1
    test         edgeb, 1 ; HAVE_LEFT
    jz .bottom_no_left
    movd            m0, [botq+strideq*0-4]
    movd            m1, [botq+strideq*1-4]
    movd   [px+32*8-4], m0
    movd   [px+32*9-4], m1
    jmp .bottom_done
.no_bottom:
    mova   [px+32*8+0], m7
    mova   [px+32*9+0], m7
.bottom_no_left:
    movd   [px+32*8-4], m7
    movd   [px+32*9-4], m7
.bottom_done:
    test         edgeb, 1 ; HAVE_LEFT
    jz .no_left
    movifnidn    leftq, r2mp
    movd            m0, [leftq+4*0]
    movd            m1, [leftq+4*1]
    movd            m2, [leftq+4*2]
    movd            m3, [leftq+4*3]
    movd   [px+32*0-4], m0
    movd   [px+32*1-4], m1
    movd   [px+32*2-4], m2
    movd   [px+32*3-4], m3
    movd            m0, [leftq+4*4]
    movd            m1, [leftq+4*5]
    movd            m2, [leftq+4*6]
    movd            m3, [leftq+4*7]
    movd   [px+32*4-4], m0
    movd   [px+32*5-4], m1
    movd   [px+32*6-4], m2
    movd   [px+32*7-4], m3
    jmp .left_done
.no_left:
    REPX {movd [px+32*x-4], m7}, 0, 1, 2, 3, 4, 5, 6, 7
.left_done:
    test         edgeb, 2 ; HAVE_RIGHT
    jnz .padding_done
    REPX {movd [px+32*x+8], m7}, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
.padding_done:
    CDEF_FILTER      4, 8

%if ARCH_X86_64
cglobal cdef_filter_8x8_16bpc, 5, 9, 9, 32*14, dst, stride, left, top, bot, \
                                               pri, sec, edge
%else
cglobal cdef_filter_8x8_16bpc, 2, 7, 8, -32*15, dst, stride, edge, top, left
%endif
    mov          edged, r9m
    LEA             t0, dir_table
    mova            m0, [dstq+strideq*0+ 0]
    movd            m1, [dstq+strideq*0+16]
    mova            m2, [dstq+strideq*1+ 0]
    movd            m3, [dstq+strideq*1+16]
    lea             t1, [dstq+strideq*2]
    mova            m4, [t1  +strideq*0+ 0]
    movd            m5, [t1  +strideq*0+16]
    mova            m6, [t1  +strideq*1+ 0]
    movd            m7, [t1  +strideq*1+16]
    lea             t1, [t1  +strideq*2]
    mova  [px+32*0+ 0], m0
    movd  [px+32*0+16], m1
    mova  [px+32*1+ 0], m2
    movd  [px+32*1+16], m3
    mova  [px+32*2+ 0], m4
    movd  [px+32*2+16], m5
    mova  [px+32*3+ 0], m6
    movd  [px+32*3+16], m7
    mova            m0, [t1  +strideq*0+ 0]
    movd            m1, [t1  +strideq*0+16]
    mova            m2, [t1  +strideq*1+ 0]
    movd            m3, [t1  +strideq*1+16]
    lea             t1, [t1  +strideq*2]
    mova            m4, [t1  +strideq*0+ 0]
    movd            m5, [t1  +strideq*0+16]
    mova            m6, [t1  +strideq*1+ 0]
    movd            m7, [t1  +strideq*1+16]
    mova  [px+32*4+ 0], m0
    movd  [px+32*4+16], m1
    mova  [px+32*5+ 0], m2
    movd  [px+32*5+16], m3
    mova  [px+32*6+ 0], m4
    movd  [px+32*6+16], m5
    mova  [px+32*7+ 0], m6
    movd  [px+32*7+16], m7
    movddup         m7, [base+pw_m16384]
    test         edgeb, 4 ; HAVE_TOP
    jz .no_top
    movifnidn     topq, topmp
    mova            m0, [topq+strideq*0+ 0]
    mova            m1, [topq+strideq*0+16]
    mova            m2, [topq+strideq*1+ 0]
    mova            m3, [topq+strideq*1+16]
    mova  [px-32*2+ 0], m0
    movd  [px-32*2+16], m1
    mova  [px-32*1+ 0], m2
    movd  [px-32*1+16], m3
    test         edgeb, 1 ; HAVE_LEFT
    jz .top_no_left
    movd            m0, [topq+strideq*0-4]
    movd            m1, [topq+strideq*1-4]
    movd   [px-32*2-4], m0
    movd   [px-32*1-4], m1
    jmp .top_done
.no_top:
    mova  [px-32*2+ 0], m7
    movd  [px-32*2+16], m7
    mova  [px-32*1+ 0], m7
    movd  [px-32*1+16], m7
.top_no_left:
    movd  [px-32*2- 4], m7
    movd  [px-32*1- 4], m7
.top_done:
    test         edgeb, 8 ; HAVE_BOTTOM
    jz .no_bottom
    movifnidn     botq, r4mp
    mova            m0, [botq+strideq*0+ 0]
    movd            m1, [botq+strideq*0+16]
    mova            m2, [botq+strideq*1+ 0]
    movd            m3, [botq+strideq*1+16]
    mova  [px+32*8+ 0], m0
    movd  [px+32*8+16], m1
    mova  [px+32*9+ 0], m2
    movd  [px+32*9+16], m3
    test         edgeb, 1 ; HAVE_LEFT
    jz .bottom_no_left
    movd            m0, [botq+strideq*0-4]
    movd            m1, [botq+strideq*1-4]
    movd  [px+32*8- 4], m0
    movd  [px+32*9- 4], m1
    jmp .bottom_done
.no_bottom:
    mova  [px+32*8+ 0], m7
    movd  [px+32*8+16], m7
    mova  [px+32*9+ 0], m7
    movd  [px+32*9+16], m7
.bottom_no_left:
    movd  [px+32*8- 4], m7
    movd  [px+32*9- 4], m7
.bottom_done:
    test         edgeb, 1 ; HAVE_LEFT
    jz .no_left
    movifnidn    leftq, r2mp
    movd            m0, [leftq+4*0]
    movd            m1, [leftq+4*1]
    movd            m2, [leftq+4*2]
    movd            m3, [leftq+4*3]
    movd  [px+32*0- 4], m0
    movd  [px+32*1- 4], m1
    movd  [px+32*2- 4], m2
    movd  [px+32*3- 4], m3
    movd            m0, [leftq+4*4]
    movd            m1, [leftq+4*5]
    movd            m2, [leftq+4*6]
    movd            m3, [leftq+4*7]
    movd  [px+32*4- 4], m0
    movd  [px+32*5- 4], m1
    movd  [px+32*6- 4], m2
    movd  [px+32*7- 4], m3
    jmp .left_done
.no_left:
    REPX {movd [px+32*x- 4], m7}, 0, 1, 2, 3, 4, 5, 6, 7
.left_done:
    test         edgeb, 2 ; HAVE_RIGHT
    jnz .padding_done
    REPX {movd [px+32*x+16], m7}, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
.padding_done:
    CDEF_FILTER      8, 8

%macro CDEF_DIR 0
%if ARCH_X86_64
cglobal cdef_dir_16bpc, 4, 7, 16, src, stride, var, bdmax
    lea             r6, [dir_shift]
    shr         bdmaxd, 11 ; 0 for 10bpc, 1 for 12bpc
    movddup         m7, [r6+bdmaxq*8]
    lea             r6, [strideq*3]
    mova            m0, [srcq+strideq*0]
    mova            m1, [srcq+strideq*1]
    mova            m2, [srcq+strideq*2]
    mova            m3, [srcq+r6       ]
    lea           srcq, [srcq+strideq*4]
    mova            m4, [srcq+strideq*0]
    mova            m5, [srcq+strideq*1]
    mova            m6, [srcq+strideq*2]
    REPX {pmulhuw x, m7}, m0, m1, m2, m3, m4, m5, m6
    pmulhuw         m7, [srcq+r6       ]
    pxor            m8, m8
    packuswb        m9, m0, m1
    packuswb       m10, m2, m3
    packuswb       m11, m4, m5
    packuswb       m12, m6, m7
    REPX {psadbw x, m8}, m9, m10, m11, m12
    packssdw        m9, m10
    packssdw       m11, m12
    packssdw        m9, m11
    jmp mangle(private_prefix %+ _cdef_dir_8bpc %+ SUFFIX).main
%else
cglobal cdef_dir_16bpc, 2, 4, 8, 96, src, stride, var, bdmax
    mov         bdmaxd, bdmaxm
    LEA             r2, dir_shift
    shr         bdmaxd, 11
    movddup         m7, [r2+bdmaxq*8]
    lea             r3, [strideq*3]
    pmulhuw         m3, m7, [srcq+strideq*0]
    pmulhuw         m4, m7, [srcq+strideq*1]
    pmulhuw         m5, m7, [srcq+strideq*2]
    pmulhuw         m6, m7, [srcq+r3       ]
    movddup         m1, [r2-dir_shift+pw_128]
    lea           srcq, [srcq+strideq*4]
    pxor            m0, m0
    packuswb        m2, m3, m4
    psubw           m3, m1
    psubw           m4, m1
    mova    [esp+0x00], m3
    mova    [esp+0x10], m4
    packuswb        m3, m5, m6
    psadbw          m2, m0
    psadbw          m3, m0
    psubw           m5, m1
    psubw           m6, m1
    packssdw        m2, m3
    mova    [esp+0x20], m5
    mova    [esp+0x50], m6
    pmulhuw         m4, m7, [srcq+strideq*0]
    pmulhuw         m5, m7, [srcq+strideq*1]
    pmulhuw         m6, m7, [srcq+strideq*2]
    pmulhuw         m7,     [srcq+r3       ]
    packuswb        m3, m4, m5
    packuswb        m1, m6, m7
    psadbw          m3, m0
    psadbw          m1, m0
    packssdw        m3, m1
    movddup         m1, [r2-dir_shift+pw_128]
    LEA             r2, shufw_6543210x
    jmp mangle(private_prefix %+ _cdef_dir_8bpc %+ SUFFIX).main
%endif
%endmacro

INIT_XMM ssse3
CDEF_DIR

INIT_XMM sse4
CDEF_DIR