summaryrefslogtreecommitdiffstats
path: root/media/libvpx/vp9/encoder/x86/vp9_subpel_variance.asm
blob: 292cf34d1a2ae3ca795c20d2633f0a05bd40bff3 (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
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
;
;  Copyright (c) 2010 The WebM project authors. All Rights Reserved.
;
;  Use of this source code is governed by a BSD-style license
;  that can be found in the LICENSE file in the root of the source
;  tree. An additional intellectual property rights grant can be found
;  in the file PATENTS.  All contributing project authors may
;  be found in the AUTHORS file in the root of the source tree.
;

%include "third_party/x86inc/x86inc.asm"

SECTION_RODATA
pw_8: times  8 dw  8
bilin_filter_m_sse2: times  8 dw 16
                     times  8 dw  0
                     times  8 dw 14
                     times  8 dw  2
                     times  8 dw 12
                     times  8 dw  4
                     times  8 dw 10
                     times  8 dw  6
                     times 16 dw  8
                     times  8 dw  6
                     times  8 dw 10
                     times  8 dw  4
                     times  8 dw 12
                     times  8 dw  2
                     times  8 dw 14

bilin_filter_m_ssse3: times  8 db 16,  0
                      times  8 db 14,  2
                      times  8 db 12,  4
                      times  8 db 10,  6
                      times 16 db  8
                      times  8 db  6, 10
                      times  8 db  4, 12
                      times  8 db  2, 14

SECTION .text

; int vp9_sub_pixel_varianceNxh(const uint8_t *src, ptrdiff_t src_stride,
;                               int x_offset, int y_offset,
;                               const uint8_t *dst, ptrdiff_t dst_stride,
;                               int height, unsigned int *sse);
;
; This function returns the SE and stores SSE in the given pointer.

%macro SUM_SSE 6 ; src1, dst1, src2, dst2, sum, sse
  psubw                %3, %4
  psubw                %1, %2
  paddw                %5, %3
  pmaddwd              %3, %3
  paddw                %5, %1
  pmaddwd              %1, %1
  paddd                %6, %3
  paddd                %6, %1
%endmacro

%macro STORE_AND_RET 0
%if mmsize == 16
  ; if H=64 and W=16, we have 8 words of each 2(1bit)x64(6bit)x9bit=16bit
  ; in m6, i.e. it _exactly_ fits in a signed word per word in the xmm reg.
  ; We have to sign-extend it before adding the words within the register
  ; and outputing to a dword.
  pcmpgtw              m5, m6           ; mask for 0 > x
  movhlps              m3, m7
  punpcklwd            m4, m6, m5
  punpckhwd            m6, m5           ; sign-extend m6 word->dword
  paddd                m7, m3
  paddd                m6, m4
  pshufd               m3, m7, 0x1
  movhlps              m4, m6
  paddd                m7, m3
  paddd                m6, m4
  mov                  r1, ssem         ; r1 = unsigned int *sse
  pshufd               m4, m6, 0x1
  movd               [r1], m7           ; store sse
  paddd                m6, m4
  movd               raxd, m6           ; store sum as return value
%else ; mmsize == 8
  pshufw               m4, m6, 0xe
  pshufw               m3, m7, 0xe
  paddw                m6, m4
  paddd                m7, m3
  pcmpgtw              m5, m6           ; mask for 0 > x
  mov                  r1, ssem         ; r1 = unsigned int *sse
  punpcklwd            m6, m5           ; sign-extend m6 word->dword
  movd               [r1], m7           ; store sse
  pshufw               m4, m6, 0xe
  paddd                m6, m4
  movd               raxd, m6           ; store sum as return value
%endif
  RET
%endmacro

%macro INC_SRC_BY_SRC_STRIDE  0
%if ARCH_X86=1 && CONFIG_PIC=1
  add                srcq, src_stridemp
%else
  add                srcq, src_strideq
%endif
%endmacro

%macro SUBPEL_VARIANCE 1-2 0 ; W
%if cpuflag(ssse3)
%define bilin_filter_m bilin_filter_m_ssse3
%define filter_idx_shift 4
%else
%define bilin_filter_m bilin_filter_m_sse2
%define filter_idx_shift 5
%endif
; FIXME(rbultje) only bilinear filters use >8 registers, and ssse3 only uses
; 11, not 13, if the registers are ordered correctly. May make a minor speed
; difference on Win64

%ifdef PIC    ; 64bit PIC
  %if %2 == 1 ; avg
    cglobal sub_pixel_avg_variance%1xh, 9, 10, 13, src, src_stride, \
                                      x_offset, y_offset, \
                                      dst, dst_stride, \
                                      sec, sec_stride, height, sse
    %define sec_str sec_strideq
  %else
    cglobal sub_pixel_variance%1xh, 7, 8, 13, src, src_stride, x_offset, \
                                  y_offset, dst, dst_stride, height, sse
  %endif
  %define h heightd
  %define bilin_filter sseq
%else
  %if ARCH_X86=1 && CONFIG_PIC=1
    %if %2 == 1 ; avg
      cglobal sub_pixel_avg_variance%1xh, 7, 7, 13, src, src_stride, \
                                  x_offset, y_offset, \
                                  dst, dst_stride, \
                                  sec, sec_stride, \
                                  height, sse, g_bilin_filter, g_pw_8
      %define h dword heightm
      %define sec_str sec_stridemp

      ;Store bilin_filter and pw_8 location in stack
      GET_GOT eax
      add esp, 4                ; restore esp

      lea ecx, [GLOBAL(bilin_filter_m)]
      mov g_bilin_filterm, ecx

      lea ecx, [GLOBAL(pw_8)]
      mov g_pw_8m, ecx

      LOAD_IF_USED 0, 1         ; load eax, ecx back
    %else
      cglobal sub_pixel_variance%1xh, 7, 7, 13, src, src_stride, x_offset, \
                                y_offset, dst, dst_stride, height, sse, \
                                g_bilin_filter, g_pw_8
      %define h heightd

      ;Store bilin_filter and pw_8 location in stack
      GET_GOT eax
      add esp, 4                ; restore esp

      lea ecx, [GLOBAL(bilin_filter_m)]
      mov g_bilin_filterm, ecx

      lea ecx, [GLOBAL(pw_8)]
      mov g_pw_8m, ecx

      LOAD_IF_USED 0, 1         ; load eax, ecx back
    %endif
  %else
    %if %2 == 1 ; avg
      cglobal sub_pixel_avg_variance%1xh, 7 + 2 * ARCH_X86_64, \
                        7 + 2 * ARCH_X86_64, 13, src, src_stride, \
                                             x_offset, y_offset, \
                                             dst, dst_stride, \
                                             sec, sec_stride, \
                                             height, sse
      %if ARCH_X86_64
      %define h heightd
      %define sec_str sec_strideq
      %else
      %define h dword heightm
      %define sec_str sec_stridemp
      %endif
    %else
      cglobal sub_pixel_variance%1xh, 7, 7, 13, src, src_stride, x_offset, \
                              y_offset, dst, dst_stride, height, sse
      %define h heightd
    %endif

    %define bilin_filter bilin_filter_m
  %endif
%endif

  ASSERT               %1 <= 16         ; m6 overflows if w > 16
  pxor                 m6, m6           ; sum
  pxor                 m7, m7           ; sse
  ; FIXME(rbultje) if both filters are bilinear, we don't actually use m5; we
  ; could perhaps use it for something more productive then
  pxor                 m5, m5           ; dedicated zero register
%if %1 < 16
  sar                   h, 1
%if %2 == 1 ; avg
  shl             sec_str, 1
%endif
%endif

  ; FIXME(rbultje) replace by jumptable?
  test          x_offsetd, x_offsetd
  jnz .x_nonzero
  ; x_offset == 0
  test          y_offsetd, y_offsetd
  jnz .x_zero_y_nonzero

  ; x_offset == 0 && y_offset == 0
.x_zero_y_zero_loop:
%if %1 == 16
  movu                 m0, [srcq]
  mova                 m1, [dstq]
%if %2 == 1 ; avg
  pavgb                m0, [secq]
  punpckhbw            m3, m1, m5
  punpcklbw            m1, m5
%endif
  punpckhbw            m2, m0, m5
  punpcklbw            m0, m5
%if %2 == 0 ; !avg
  punpckhbw            m3, m1, m5
  punpcklbw            m1, m5
%endif
  SUM_SSE              m0, m1, m2, m3, m6, m7

  add                srcq, src_strideq
  add                dstq, dst_strideq
%else ; %1 < 16
  movh                 m0, [srcq]
%if %2 == 1 ; avg
%if mmsize == 16
  movhps               m0, [srcq+src_strideq]
%else ; mmsize == 8
  punpckldq            m0, [srcq+src_strideq]
%endif
%else ; !avg
  movh                 m2, [srcq+src_strideq]
%endif
  movh                 m1, [dstq]
  movh                 m3, [dstq+dst_strideq]
%if %2 == 1 ; avg
  pavgb                m0, [secq]
  punpcklbw            m3, m5
  punpcklbw            m1, m5
  punpckhbw            m2, m0, m5
  punpcklbw            m0, m5
%else ; !avg
  punpcklbw            m0, m5
  punpcklbw            m2, m5
  punpcklbw            m3, m5
  punpcklbw            m1, m5
%endif
  SUM_SSE              m0, m1, m2, m3, m6, m7

  lea                srcq, [srcq+src_strideq*2]
  lea                dstq, [dstq+dst_strideq*2]
%endif
%if %2 == 1 ; avg
  add                secq, sec_str
%endif
  dec                   h
  jg .x_zero_y_zero_loop
  STORE_AND_RET

.x_zero_y_nonzero:
  cmp           y_offsetd, 8
  jne .x_zero_y_nonhalf

  ; x_offset == 0 && y_offset == 0.5
.x_zero_y_half_loop:
%if %1 == 16
  movu                 m0, [srcq]
  movu                 m4, [srcq+src_strideq]
  mova                 m1, [dstq]
  pavgb                m0, m4
  punpckhbw            m3, m1, m5
%if %2 == 1 ; avg
  pavgb                m0, [secq]
%endif
  punpcklbw            m1, m5
  punpckhbw            m2, m0, m5
  punpcklbw            m0, m5
  SUM_SSE              m0, m1, m2, m3, m6, m7

  add                srcq, src_strideq
  add                dstq, dst_strideq
%else ; %1 < 16
  movh                 m0, [srcq]
  movh                 m2, [srcq+src_strideq]
%if %2 == 1 ; avg
%if mmsize == 16
  movhps               m2, [srcq+src_strideq*2]
%else ; mmsize == 8
%if %1 == 4
  movh                 m1, [srcq+src_strideq*2]
  punpckldq            m2, m1
%else
  punpckldq            m2, [srcq+src_strideq*2]
%endif
%endif
  movh                 m1, [dstq]
%if mmsize == 16
  movlhps              m0, m2
%else ; mmsize == 8
  punpckldq            m0, m2
%endif
  movh                 m3, [dstq+dst_strideq]
  pavgb                m0, m2
  punpcklbw            m1, m5
  pavgb                m0, [secq]
  punpcklbw            m3, m5
  punpckhbw            m2, m0, m5
  punpcklbw            m0, m5
%else ; !avg
  movh                 m4, [srcq+src_strideq*2]
  movh                 m1, [dstq]
  pavgb                m0, m2
  movh                 m3, [dstq+dst_strideq]
  pavgb                m2, m4
  punpcklbw            m0, m5
  punpcklbw            m2, m5
  punpcklbw            m3, m5
  punpcklbw            m1, m5
%endif
  SUM_SSE              m0, m1, m2, m3, m6, m7

  lea                srcq, [srcq+src_strideq*2]
  lea                dstq, [dstq+dst_strideq*2]
%endif
%if %2 == 1 ; avg
  add                secq, sec_str
%endif
  dec                   h
  jg .x_zero_y_half_loop
  STORE_AND_RET

.x_zero_y_nonhalf:
  ; x_offset == 0 && y_offset == bilin interpolation
%ifdef PIC
  lea        bilin_filter, [bilin_filter_m]
%endif
  shl           y_offsetd, filter_idx_shift
%if ARCH_X86_64 && mmsize == 16
  mova                 m8, [bilin_filter+y_offsetq]
%if notcpuflag(ssse3) ; FIXME(rbultje) don't scatter registers on x86-64
  mova                 m9, [bilin_filter+y_offsetq+16]
%endif
  mova                m10, [pw_8]
%define filter_y_a m8
%define filter_y_b m9
%define filter_rnd m10
%else ; x86-32 or mmx
%if ARCH_X86=1 && CONFIG_PIC=1
; x_offset == 0, reuse x_offset reg
%define tempq x_offsetq
  add y_offsetq, g_bilin_filterm
%define filter_y_a [y_offsetq]
%define filter_y_b [y_offsetq+16]
  mov tempq, g_pw_8m
%define filter_rnd [tempq]
%else
  add           y_offsetq, bilin_filter
%define filter_y_a [y_offsetq]
%define filter_y_b [y_offsetq+16]
%define filter_rnd [pw_8]
%endif
%endif

.x_zero_y_other_loop:
%if %1 == 16
  movu                 m0, [srcq]
  movu                 m4, [srcq+src_strideq]
  mova                 m1, [dstq]
%if cpuflag(ssse3)
  punpckhbw            m2, m0, m4
  punpcklbw            m0, m4
  pmaddubsw            m2, filter_y_a
  pmaddubsw            m0, filter_y_a
  paddw                m2, filter_rnd
  paddw                m0, filter_rnd
%else
  punpckhbw            m2, m0, m5
  punpckhbw            m3, m4, m5
  punpcklbw            m0, m5
  punpcklbw            m4, m5
  ; FIXME(rbultje) instead of out=((num-x)*in1+x*in2+rnd)>>log2(num), we can
  ; also do out=in1+(((num-x)*(in2-in1)+rnd)>>log2(num)). Total number of
  ; instructions is the same (5), but it is 1 mul instead of 2, so might be
  ; slightly faster because of pmullw latency. It would also cut our rodata
  ; tables in half for this function, and save 1-2 registers on x86-64.
  pmullw               m2, filter_y_a
  pmullw               m3, filter_y_b
  paddw                m2, filter_rnd
  pmullw               m0, filter_y_a
  pmullw               m4, filter_y_b
  paddw                m0, filter_rnd
  paddw                m2, m3
  paddw                m0, m4
%endif
  psraw                m2, 4
  psraw                m0, 4
%if %2 == 1 ; avg
  ; FIXME(rbultje) pipeline
  packuswb             m0, m2
  pavgb                m0, [secq]
  punpckhbw            m2, m0, m5
  punpcklbw            m0, m5
%endif
  punpckhbw            m3, m1, m5
  punpcklbw            m1, m5
  SUM_SSE              m0, m1, m2, m3, m6, m7

  add                srcq, src_strideq
  add                dstq, dst_strideq
%else ; %1 < 16
  movh                 m0, [srcq]
  movh                 m2, [srcq+src_strideq]
  movh                 m4, [srcq+src_strideq*2]
  movh                 m3, [dstq+dst_strideq]
%if cpuflag(ssse3)
  movh                 m1, [dstq]
  punpcklbw            m0, m2
  punpcklbw            m2, m4
  pmaddubsw            m0, filter_y_a
  pmaddubsw            m2, filter_y_a
  punpcklbw            m3, m5
  paddw                m2, filter_rnd
  paddw                m0, filter_rnd
%else
  punpcklbw            m0, m5
  punpcklbw            m2, m5
  punpcklbw            m4, m5
  pmullw               m0, filter_y_a
  pmullw               m1, m2, filter_y_b
  punpcklbw            m3, m5
  paddw                m0, filter_rnd
  pmullw               m2, filter_y_a
  pmullw               m4, filter_y_b
  paddw                m0, m1
  paddw                m2, filter_rnd
  movh                 m1, [dstq]
  paddw                m2, m4
%endif
  psraw                m0, 4
  psraw                m2, 4
%if %2 == 1 ; avg
  ; FIXME(rbultje) pipeline
  packuswb             m0, m2
  pavgb                m0, [secq]
  punpckhbw            m2, m0, m5
  punpcklbw            m0, m5
%endif
  punpcklbw            m1, m5
  SUM_SSE              m0, m1, m2, m3, m6, m7

  lea                srcq, [srcq+src_strideq*2]
  lea                dstq, [dstq+dst_strideq*2]
%endif
%if %2 == 1 ; avg
  add                secq, sec_str
%endif
  dec                   h
  jg .x_zero_y_other_loop
%undef filter_y_a
%undef filter_y_b
%undef filter_rnd
  STORE_AND_RET

.x_nonzero:
  cmp           x_offsetd, 8
  jne .x_nonhalf
  ; x_offset == 0.5
  test          y_offsetd, y_offsetd
  jnz .x_half_y_nonzero

  ; x_offset == 0.5 && y_offset == 0
.x_half_y_zero_loop:
%if %1 == 16
  movu                 m0, [srcq]
  movu                 m4, [srcq+1]
  mova                 m1, [dstq]
  pavgb                m0, m4
  punpckhbw            m3, m1, m5
%if %2 == 1 ; avg
  pavgb                m0, [secq]
%endif
  punpcklbw            m1, m5
  punpckhbw            m2, m0, m5
  punpcklbw            m0, m5
  SUM_SSE              m0, m1, m2, m3, m6, m7

  add                srcq, src_strideq
  add                dstq, dst_strideq
%else ; %1 < 16
  movh                 m0, [srcq]
  movh                 m4, [srcq+1]
%if %2 == 1 ; avg
%if mmsize == 16
  movhps               m0, [srcq+src_strideq]
  movhps               m4, [srcq+src_strideq+1]
%else ; mmsize == 8
  punpckldq            m0, [srcq+src_strideq]
  punpckldq            m4, [srcq+src_strideq+1]
%endif
  movh                 m1, [dstq]
  movh                 m3, [dstq+dst_strideq]
  pavgb                m0, m4
  punpcklbw            m3, m5
  pavgb                m0, [secq]
  punpcklbw            m1, m5
  punpckhbw            m2, m0, m5
  punpcklbw            m0, m5
%else ; !avg
  movh                 m2, [srcq+src_strideq]
  movh                 m1, [dstq]
  pavgb                m0, m4
  movh                 m4, [srcq+src_strideq+1]
  movh                 m3, [dstq+dst_strideq]
  pavgb                m2, m4
  punpcklbw            m0, m5
  punpcklbw            m2, m5
  punpcklbw            m3, m5
  punpcklbw            m1, m5
%endif
  SUM_SSE              m0, m1, m2, m3, m6, m7

  lea                srcq, [srcq+src_strideq*2]
  lea                dstq, [dstq+dst_strideq*2]
%endif
%if %2 == 1 ; avg
  add                secq, sec_str
%endif
  dec                   h
  jg .x_half_y_zero_loop
  STORE_AND_RET

.x_half_y_nonzero:
  cmp           y_offsetd, 8
  jne .x_half_y_nonhalf

  ; x_offset == 0.5 && y_offset == 0.5
%if %1 == 16
  movu                 m0, [srcq]
  movu                 m3, [srcq+1]
  add                srcq, src_strideq
  pavgb                m0, m3
.x_half_y_half_loop:
  movu                 m4, [srcq]
  movu                 m3, [srcq+1]
  mova                 m1, [dstq]
  pavgb                m4, m3
  punpckhbw            m3, m1, m5
  pavgb                m0, m4
%if %2 == 1 ; avg
  punpcklbw            m1, m5
  pavgb                m0, [secq]
  punpckhbw            m2, m0, m5
  punpcklbw            m0, m5
%else
  punpckhbw            m2, m0, m5
  punpcklbw            m0, m5
  punpcklbw            m1, m5
%endif
  SUM_SSE              m0, m1, m2, m3, m6, m7
  mova                 m0, m4

  add                srcq, src_strideq
  add                dstq, dst_strideq
%else ; %1 < 16
  movh                 m0, [srcq]
  movh                 m3, [srcq+1]
  add                srcq, src_strideq
  pavgb                m0, m3
.x_half_y_half_loop:
  movh                 m2, [srcq]
  movh                 m3, [srcq+1]
%if %2 == 1 ; avg
%if mmsize == 16
  movhps               m2, [srcq+src_strideq]
  movhps               m3, [srcq+src_strideq+1]
%else
%if %1 == 4
  movh                 m1, [srcq+src_strideq]
  punpckldq            m2, m1
  movh                 m1, [srcq+src_strideq+1]
  punpckldq            m3, m1
%else
  punpckldq            m2, [srcq+src_strideq]
  punpckldq            m3, [srcq+src_strideq+1]
%endif
%endif
  pavgb                m2, m3
%if mmsize == 16
  movlhps              m0, m2
  movhlps              m4, m2
%else ; mmsize == 8
  punpckldq            m0, m2
  pshufw               m4, m2, 0xe
%endif
  movh                 m1, [dstq]
  pavgb                m0, m2
  movh                 m3, [dstq+dst_strideq]
  pavgb                m0, [secq]
  punpcklbw            m3, m5
  punpcklbw            m1, m5
  punpckhbw            m2, m0, m5
  punpcklbw            m0, m5
%else ; !avg
  movh                 m4, [srcq+src_strideq]
  movh                 m1, [srcq+src_strideq+1]
  pavgb                m2, m3
  pavgb                m4, m1
  pavgb                m0, m2
  pavgb                m2, m4
  movh                 m1, [dstq]
  movh                 m3, [dstq+dst_strideq]
  punpcklbw            m0, m5
  punpcklbw            m2, m5
  punpcklbw            m3, m5
  punpcklbw            m1, m5
%endif
  SUM_SSE              m0, m1, m2, m3, m6, m7
  mova                 m0, m4

  lea                srcq, [srcq+src_strideq*2]
  lea                dstq, [dstq+dst_strideq*2]
%endif
%if %2 == 1 ; avg
  add                secq, sec_str
%endif
  dec                   h
  jg .x_half_y_half_loop
  STORE_AND_RET

.x_half_y_nonhalf:
  ; x_offset == 0.5 && y_offset == bilin interpolation
%ifdef PIC
  lea        bilin_filter, [bilin_filter_m]
%endif
  shl           y_offsetd, filter_idx_shift
%if ARCH_X86_64 && mmsize == 16
  mova                 m8, [bilin_filter+y_offsetq]
%if notcpuflag(ssse3) ; FIXME(rbultje) don't scatter registers on x86-64
  mova                 m9, [bilin_filter+y_offsetq+16]
%endif
  mova                m10, [pw_8]
%define filter_y_a m8
%define filter_y_b m9
%define filter_rnd m10
%else  ;x86_32
%if ARCH_X86=1 && CONFIG_PIC=1
; x_offset == 0.5. We can reuse x_offset reg
%define tempq x_offsetq
  add y_offsetq, g_bilin_filterm
%define filter_y_a [y_offsetq]
%define filter_y_b [y_offsetq+16]
  mov tempq, g_pw_8m
%define filter_rnd [tempq]
%else
  add           y_offsetq, bilin_filter
%define filter_y_a [y_offsetq]
%define filter_y_b [y_offsetq+16]
%define filter_rnd [pw_8]
%endif
%endif

%if %1 == 16
  movu                 m0, [srcq]
  movu                 m3, [srcq+1]
  add                srcq, src_strideq
  pavgb                m0, m3
.x_half_y_other_loop:
  movu                 m4, [srcq]
  movu                 m2, [srcq+1]
  mova                 m1, [dstq]
  pavgb                m4, m2
%if cpuflag(ssse3)
  punpckhbw            m2, m0, m4
  punpcklbw            m0, m4
  pmaddubsw            m2, filter_y_a
  pmaddubsw            m0, filter_y_a
  paddw                m2, filter_rnd
  paddw                m0, filter_rnd
  psraw                m2, 4
%else
  punpckhbw            m2, m0, m5
  punpckhbw            m3, m4, m5
  pmullw               m2, filter_y_a
  pmullw               m3, filter_y_b
  paddw                m2, filter_rnd
  punpcklbw            m0, m5
  paddw                m2, m3
  punpcklbw            m3, m4, m5
  pmullw               m0, filter_y_a
  pmullw               m3, filter_y_b
  paddw                m0, filter_rnd
  psraw                m2, 4
  paddw                m0, m3
%endif
  punpckhbw            m3, m1, m5
  psraw                m0, 4
%if %2 == 1 ; avg
  ; FIXME(rbultje) pipeline
  packuswb             m0, m2
  pavgb                m0, [secq]
  punpckhbw            m2, m0, m5
  punpcklbw            m0, m5
%endif
  punpcklbw            m1, m5
  SUM_SSE              m0, m1, m2, m3, m6, m7
  mova                 m0, m4

  add                srcq, src_strideq
  add                dstq, dst_strideq
%else ; %1 < 16
  movh                 m0, [srcq]
  movh                 m3, [srcq+1]
  add                srcq, src_strideq
  pavgb                m0, m3
%if notcpuflag(ssse3)
  punpcklbw            m0, m5
%endif
.x_half_y_other_loop:
  movh                 m2, [srcq]
  movh                 m1, [srcq+1]
  movh                 m4, [srcq+src_strideq]
  movh                 m3, [srcq+src_strideq+1]
  pavgb                m2, m1
  pavgb                m4, m3
  movh                 m3, [dstq+dst_strideq]
%if cpuflag(ssse3)
  movh                 m1, [dstq]
  punpcklbw            m0, m2
  punpcklbw            m2, m4
  pmaddubsw            m0, filter_y_a
  pmaddubsw            m2, filter_y_a
  punpcklbw            m3, m5
  paddw                m0, filter_rnd
  paddw                m2, filter_rnd
%else
  punpcklbw            m2, m5
  punpcklbw            m4, m5
  pmullw               m0, filter_y_a
  pmullw               m1, m2, filter_y_b
  punpcklbw            m3, m5
  paddw                m0, filter_rnd
  pmullw               m2, filter_y_a
  paddw                m0, m1
  pmullw               m1, m4, filter_y_b
  paddw                m2, filter_rnd
  paddw                m2, m1
  movh                 m1, [dstq]
%endif
  psraw                m0, 4
  psraw                m2, 4
%if %2 == 1 ; avg
  ; FIXME(rbultje) pipeline
  packuswb             m0, m2
  pavgb                m0, [secq]
  punpckhbw            m2, m0, m5
  punpcklbw            m0, m5
%endif
  punpcklbw            m1, m5
  SUM_SSE              m0, m1, m2, m3, m6, m7
  mova                 m0, m4

  lea                srcq, [srcq+src_strideq*2]
  lea                dstq, [dstq+dst_strideq*2]
%endif
%if %2 == 1 ; avg
  add                secq, sec_str
%endif
  dec                   h
  jg .x_half_y_other_loop
%undef filter_y_a
%undef filter_y_b
%undef filter_rnd
  STORE_AND_RET

.x_nonhalf:
  test          y_offsetd, y_offsetd
  jnz .x_nonhalf_y_nonzero

  ; x_offset == bilin interpolation && y_offset == 0
%ifdef PIC
  lea        bilin_filter, [bilin_filter_m]
%endif
  shl           x_offsetd, filter_idx_shift
%if ARCH_X86_64 && mmsize == 16
  mova                 m8, [bilin_filter+x_offsetq]
%if notcpuflag(ssse3) ; FIXME(rbultje) don't scatter registers on x86-64
  mova                 m9, [bilin_filter+x_offsetq+16]
%endif
  mova                m10, [pw_8]
%define filter_x_a m8
%define filter_x_b m9
%define filter_rnd m10
%else    ; x86-32
%if ARCH_X86=1 && CONFIG_PIC=1
;y_offset == 0. We can reuse y_offset reg.
%define tempq y_offsetq
  add x_offsetq, g_bilin_filterm
%define filter_x_a [x_offsetq]
%define filter_x_b [x_offsetq+16]
  mov tempq, g_pw_8m
%define filter_rnd [tempq]
%else
  add           x_offsetq, bilin_filter
%define filter_x_a [x_offsetq]
%define filter_x_b [x_offsetq+16]
%define filter_rnd [pw_8]
%endif
%endif

.x_other_y_zero_loop:
%if %1 == 16
  movu                 m0, [srcq]
  movu                 m4, [srcq+1]
  mova                 m1, [dstq]
%if cpuflag(ssse3)
  punpckhbw            m2, m0, m4
  punpcklbw            m0, m4
  pmaddubsw            m2, filter_x_a
  pmaddubsw            m0, filter_x_a
  paddw                m2, filter_rnd
  paddw                m0, filter_rnd
%else
  punpckhbw            m2, m0, m5
  punpckhbw            m3, m4, m5
  punpcklbw            m0, m5
  punpcklbw            m4, m5
  pmullw               m2, filter_x_a
  pmullw               m3, filter_x_b
  paddw                m2, filter_rnd
  pmullw               m0, filter_x_a
  pmullw               m4, filter_x_b
  paddw                m0, filter_rnd
  paddw                m2, m3
  paddw                m0, m4
%endif
  psraw                m2, 4
  psraw                m0, 4
%if %2 == 1 ; avg
  ; FIXME(rbultje) pipeline
  packuswb             m0, m2
  pavgb                m0, [secq]
  punpckhbw            m2, m0, m5
  punpcklbw            m0, m5
%endif
  punpckhbw            m3, m1, m5
  punpcklbw            m1, m5
  SUM_SSE              m0, m1, m2, m3, m6, m7

  add                srcq, src_strideq
  add                dstq, dst_strideq
%else ; %1 < 16
  movh                 m0, [srcq]
  movh                 m1, [srcq+1]
  movh                 m2, [srcq+src_strideq]
  movh                 m4, [srcq+src_strideq+1]
  movh                 m3, [dstq+dst_strideq]
%if cpuflag(ssse3)
  punpcklbw            m0, m1
  movh                 m1, [dstq]
  punpcklbw            m2, m4
  pmaddubsw            m0, filter_x_a
  pmaddubsw            m2, filter_x_a
  punpcklbw            m3, m5
  paddw                m0, filter_rnd
  paddw                m2, filter_rnd
%else
  punpcklbw            m0, m5
  punpcklbw            m1, m5
  punpcklbw            m2, m5
  punpcklbw            m4, m5
  pmullw               m0, filter_x_a
  pmullw               m1, filter_x_b
  punpcklbw            m3, m5
  paddw                m0, filter_rnd
  pmullw               m2, filter_x_a
  pmullw               m4, filter_x_b
  paddw                m0, m1
  paddw                m2, filter_rnd
  movh                 m1, [dstq]
  paddw                m2, m4
%endif
  psraw                m0, 4
  psraw                m2, 4
%if %2 == 1 ; avg
  ; FIXME(rbultje) pipeline
  packuswb             m0, m2
  pavgb                m0, [secq]
  punpckhbw            m2, m0, m5
  punpcklbw            m0, m5
%endif
  punpcklbw            m1, m5
  SUM_SSE              m0, m1, m2, m3, m6, m7

  lea                srcq, [srcq+src_strideq*2]
  lea                dstq, [dstq+dst_strideq*2]
%endif
%if %2 == 1 ; avg
  add                secq, sec_str
%endif
  dec                   h
  jg .x_other_y_zero_loop
%undef filter_x_a
%undef filter_x_b
%undef filter_rnd
  STORE_AND_RET

.x_nonhalf_y_nonzero:
  cmp           y_offsetd, 8
  jne .x_nonhalf_y_nonhalf

  ; x_offset == bilin interpolation && y_offset == 0.5
%ifdef PIC
  lea        bilin_filter, [bilin_filter_m]
%endif
  shl           x_offsetd, filter_idx_shift
%if ARCH_X86_64 && mmsize == 16
  mova                 m8, [bilin_filter+x_offsetq]
%if notcpuflag(ssse3) ; FIXME(rbultje) don't scatter registers on x86-64
  mova                 m9, [bilin_filter+x_offsetq+16]
%endif
  mova                m10, [pw_8]
%define filter_x_a m8
%define filter_x_b m9
%define filter_rnd m10
%else    ; x86-32
%if ARCH_X86=1 && CONFIG_PIC=1
; y_offset == 0.5. We can reuse y_offset reg.
%define tempq y_offsetq
  add x_offsetq, g_bilin_filterm
%define filter_x_a [x_offsetq]
%define filter_x_b [x_offsetq+16]
  mov tempq, g_pw_8m
%define filter_rnd [tempq]
%else
  add           x_offsetq, bilin_filter
%define filter_x_a [x_offsetq]
%define filter_x_b [x_offsetq+16]
%define filter_rnd [pw_8]
%endif
%endif

%if %1 == 16
  movu                 m0, [srcq]
  movu                 m1, [srcq+1]
%if cpuflag(ssse3)
  punpckhbw            m2, m0, m1
  punpcklbw            m0, m1
  pmaddubsw            m2, filter_x_a
  pmaddubsw            m0, filter_x_a
  paddw                m2, filter_rnd
  paddw                m0, filter_rnd
%else
  punpckhbw            m2, m0, m5
  punpckhbw            m3, m1, m5
  punpcklbw            m0, m5
  punpcklbw            m1, m5
  pmullw               m0, filter_x_a
  pmullw               m1, filter_x_b
  paddw                m0, filter_rnd
  pmullw               m2, filter_x_a
  pmullw               m3, filter_x_b
  paddw                m2, filter_rnd
  paddw                m0, m1
  paddw                m2, m3
%endif
  psraw                m0, 4
  psraw                m2, 4
  add                srcq, src_strideq
  packuswb             m0, m2
.x_other_y_half_loop:
  movu                 m4, [srcq]
  movu                 m3, [srcq+1]
%if cpuflag(ssse3)
  mova                 m1, [dstq]
  punpckhbw            m2, m4, m3
  punpcklbw            m4, m3
  pmaddubsw            m2, filter_x_a
  pmaddubsw            m4, filter_x_a
  paddw                m2, filter_rnd
  paddw                m4, filter_rnd
  psraw                m2, 4
  psraw                m4, 4
  packuswb             m4, m2
  pavgb                m0, m4
  punpckhbw            m3, m1, m5
  punpcklbw            m1, m5
%else
  punpckhbw            m2, m4, m5
  punpckhbw            m1, m3, m5
  punpcklbw            m4, m5
  punpcklbw            m3, m5
  pmullw               m4, filter_x_a
  pmullw               m3, filter_x_b
  paddw                m4, filter_rnd
  pmullw               m2, filter_x_a
  pmullw               m1, filter_x_b
  paddw                m2, filter_rnd
  paddw                m4, m3
  paddw                m2, m1
  mova                 m1, [dstq]
  psraw                m4, 4
  psraw                m2, 4
  punpckhbw            m3, m1, m5
  ; FIXME(rbultje) the repeated pack/unpack here around m0/m2 is because we
  ; have a 1-register shortage to be able to store the backup of the bilin
  ; filtered second line as words as cache for the next line. Packing into
  ; a byte costs 1 pack and 2 unpacks, but saves a register.
  packuswb             m4, m2
  punpcklbw            m1, m5
  pavgb                m0, m4
%endif
%if %2 == 1 ; avg
  ; FIXME(rbultje) pipeline
  pavgb                m0, [secq]
%endif
  punpckhbw            m2, m0, m5
  punpcklbw            m0, m5
  SUM_SSE              m0, m1, m2, m3, m6, m7
  mova                 m0, m4

  add                srcq, src_strideq
  add                dstq, dst_strideq
%else ; %1 < 16
  movh                 m0, [srcq]
  movh                 m1, [srcq+1]
%if cpuflag(ssse3)
  punpcklbw            m0, m1
  pmaddubsw            m0, filter_x_a
  paddw                m0, filter_rnd
%else
  punpcklbw            m0, m5
  punpcklbw            m1, m5
  pmullw               m0, filter_x_a
  pmullw               m1, filter_x_b
  paddw                m0, filter_rnd
  paddw                m0, m1
%endif
  add                srcq, src_strideq
  psraw                m0, 4
.x_other_y_half_loop:
  movh                 m2, [srcq]
  movh                 m1, [srcq+1]
  movh                 m4, [srcq+src_strideq]
  movh                 m3, [srcq+src_strideq+1]
%if cpuflag(ssse3)
  punpcklbw            m2, m1
  punpcklbw            m4, m3
  pmaddubsw            m2, filter_x_a
  pmaddubsw            m4, filter_x_a
  movh                 m1, [dstq]
  movh                 m3, [dstq+dst_strideq]
  paddw                m2, filter_rnd
  paddw                m4, filter_rnd
%else
  punpcklbw            m2, m5
  punpcklbw            m1, m5
  punpcklbw            m4, m5
  punpcklbw            m3, m5
  pmullw               m2, filter_x_a
  pmullw               m1, filter_x_b
  paddw                m2, filter_rnd
  pmullw               m4, filter_x_a
  pmullw               m3, filter_x_b
  paddw                m4, filter_rnd
  paddw                m2, m1
  movh                 m1, [dstq]
  paddw                m4, m3
  movh                 m3, [dstq+dst_strideq]
%endif
  psraw                m2, 4
  psraw                m4, 4
  pavgw                m0, m2
  pavgw                m2, m4
%if %2 == 1 ; avg
  ; FIXME(rbultje) pipeline - also consider going to bytes here
  packuswb             m0, m2
  pavgb                m0, [secq]
  punpckhbw            m2, m0, m5
  punpcklbw            m0, m5
%endif
  punpcklbw            m3, m5
  punpcklbw            m1, m5
  SUM_SSE              m0, m1, m2, m3, m6, m7
  mova                 m0, m4

  lea                srcq, [srcq+src_strideq*2]
  lea                dstq, [dstq+dst_strideq*2]
%endif
%if %2 == 1 ; avg
  add                secq, sec_str
%endif
  dec                   h
  jg .x_other_y_half_loop
%undef filter_x_a
%undef filter_x_b
%undef filter_rnd
  STORE_AND_RET

.x_nonhalf_y_nonhalf:
%ifdef PIC
  lea        bilin_filter, [bilin_filter_m]
%endif
  shl           x_offsetd, filter_idx_shift
  shl           y_offsetd, filter_idx_shift
%if ARCH_X86_64 && mmsize == 16
  mova                 m8, [bilin_filter+x_offsetq]
%if notcpuflag(ssse3) ; FIXME(rbultje) don't scatter registers on x86-64
  mova                 m9, [bilin_filter+x_offsetq+16]
%endif
  mova                m10, [bilin_filter+y_offsetq]
%if notcpuflag(ssse3) ; FIXME(rbultje) don't scatter registers on x86-64
  mova                m11, [bilin_filter+y_offsetq+16]
%endif
  mova                m12, [pw_8]
%define filter_x_a m8
%define filter_x_b m9
%define filter_y_a m10
%define filter_y_b m11
%define filter_rnd m12
%else   ; x86-32
%if ARCH_X86=1 && CONFIG_PIC=1
; In this case, there is NO unused register. Used src_stride register. Later,
; src_stride has to be loaded from stack when it is needed.
%define tempq src_strideq
  mov tempq, g_bilin_filterm
  add           x_offsetq, tempq
  add           y_offsetq, tempq
%define filter_x_a [x_offsetq]
%define filter_x_b [x_offsetq+16]
%define filter_y_a [y_offsetq]
%define filter_y_b [y_offsetq+16]

  mov tempq, g_pw_8m
%define filter_rnd [tempq]
%else
  add           x_offsetq, bilin_filter
  add           y_offsetq, bilin_filter
%define filter_x_a [x_offsetq]
%define filter_x_b [x_offsetq+16]
%define filter_y_a [y_offsetq]
%define filter_y_b [y_offsetq+16]
%define filter_rnd [pw_8]
%endif
%endif

  ; x_offset == bilin interpolation && y_offset == bilin interpolation
%if %1 == 16
  movu                 m0, [srcq]
  movu                 m1, [srcq+1]
%if cpuflag(ssse3)
  punpckhbw            m2, m0, m1
  punpcklbw            m0, m1
  pmaddubsw            m2, filter_x_a
  pmaddubsw            m0, filter_x_a
  paddw                m2, filter_rnd
  paddw                m0, filter_rnd
%else
  punpckhbw            m2, m0, m5
  punpckhbw            m3, m1, m5
  punpcklbw            m0, m5
  punpcklbw            m1, m5
  pmullw               m0, filter_x_a
  pmullw               m1, filter_x_b
  paddw                m0, filter_rnd
  pmullw               m2, filter_x_a
  pmullw               m3, filter_x_b
  paddw                m2, filter_rnd
  paddw                m0, m1
  paddw                m2, m3
%endif
  psraw                m0, 4
  psraw                m2, 4

  INC_SRC_BY_SRC_STRIDE

  packuswb             m0, m2
.x_other_y_other_loop:
%if cpuflag(ssse3)
  movu                 m4, [srcq]
  movu                 m3, [srcq+1]
  mova                 m1, [dstq]
  punpckhbw            m2, m4, m3
  punpcklbw            m4, m3
  pmaddubsw            m2, filter_x_a
  pmaddubsw            m4, filter_x_a
  punpckhbw            m3, m1, m5
  paddw                m2, filter_rnd
  paddw                m4, filter_rnd
  psraw                m2, 4
  psraw                m4, 4
  packuswb             m4, m2
  punpckhbw            m2, m0, m4
  punpcklbw            m0, m4
  pmaddubsw            m2, filter_y_a
  pmaddubsw            m0, filter_y_a
  punpcklbw            m1, m5
  paddw                m2, filter_rnd
  paddw                m0, filter_rnd
  psraw                m2, 4
  psraw                m0, 4
%else
  movu                 m3, [srcq]
  movu                 m4, [srcq+1]
  punpckhbw            m1, m3, m5
  punpckhbw            m2, m4, m5
  punpcklbw            m3, m5
  punpcklbw            m4, m5
  pmullw               m3, filter_x_a
  pmullw               m4, filter_x_b
  paddw                m3, filter_rnd
  pmullw               m1, filter_x_a
  pmullw               m2, filter_x_b
  paddw                m1, filter_rnd
  paddw                m3, m4
  paddw                m1, m2
  psraw                m3, 4
  psraw                m1, 4
  packuswb             m4, m3, m1
  punpckhbw            m2, m0, m5
  punpcklbw            m0, m5
  pmullw               m2, filter_y_a
  pmullw               m1, filter_y_b
  paddw                m2, filter_rnd
  pmullw               m0, filter_y_a
  pmullw               m3, filter_y_b
  paddw                m2, m1
  mova                 m1, [dstq]
  paddw                m0, filter_rnd
  psraw                m2, 4
  paddw                m0, m3
  punpckhbw            m3, m1, m5
  psraw                m0, 4
  punpcklbw            m1, m5
%endif
%if %2 == 1 ; avg
  ; FIXME(rbultje) pipeline
  packuswb             m0, m2
  pavgb                m0, [secq]
  punpckhbw            m2, m0, m5
  punpcklbw            m0, m5
%endif
  SUM_SSE              m0, m1, m2, m3, m6, m7
  mova                 m0, m4

  INC_SRC_BY_SRC_STRIDE
  add                dstq, dst_strideq
%else ; %1 < 16
  movh                 m0, [srcq]
  movh                 m1, [srcq+1]
%if cpuflag(ssse3)
  punpcklbw            m0, m1
  pmaddubsw            m0, filter_x_a
  paddw                m0, filter_rnd
%else
  punpcklbw            m0, m5
  punpcklbw            m1, m5
  pmullw               m0, filter_x_a
  pmullw               m1, filter_x_b
  paddw                m0, filter_rnd
  paddw                m0, m1
%endif
  psraw                m0, 4
%if cpuflag(ssse3)
  packuswb             m0, m0
%endif

  INC_SRC_BY_SRC_STRIDE

.x_other_y_other_loop:
  movh                 m2, [srcq]
  movh                 m1, [srcq+1]

  INC_SRC_BY_SRC_STRIDE
  movh                 m4, [srcq]
  movh                 m3, [srcq+1]

%if cpuflag(ssse3)
  punpcklbw            m2, m1
  punpcklbw            m4, m3
  pmaddubsw            m2, filter_x_a
  pmaddubsw            m4, filter_x_a
  movh                 m3, [dstq+dst_strideq]
  movh                 m1, [dstq]
  paddw                m2, filter_rnd
  paddw                m4, filter_rnd
  psraw                m2, 4
  psraw                m4, 4
  packuswb             m2, m2
  packuswb             m4, m4
  punpcklbw            m0, m2
  punpcklbw            m2, m4
  pmaddubsw            m0, filter_y_a
  pmaddubsw            m2, filter_y_a
  punpcklbw            m3, m5
  paddw                m0, filter_rnd
  paddw                m2, filter_rnd
  psraw                m0, 4
  psraw                m2, 4
  punpcklbw            m1, m5
%else
  punpcklbw            m2, m5
  punpcklbw            m1, m5
  punpcklbw            m4, m5
  punpcklbw            m3, m5
  pmullw               m2, filter_x_a
  pmullw               m1, filter_x_b
  paddw                m2, filter_rnd
  pmullw               m4, filter_x_a
  pmullw               m3, filter_x_b
  paddw                m4, filter_rnd
  paddw                m2, m1
  paddw                m4, m3
  psraw                m2, 4
  psraw                m4, 4
  pmullw               m0, filter_y_a
  pmullw               m3, m2, filter_y_b
  paddw                m0, filter_rnd
  pmullw               m2, filter_y_a
  pmullw               m1, m4, filter_y_b
  paddw                m2, filter_rnd
  paddw                m0, m3
  movh                 m3, [dstq+dst_strideq]
  paddw                m2, m1
  movh                 m1, [dstq]
  psraw                m0, 4
  psraw                m2, 4
  punpcklbw            m3, m5
  punpcklbw            m1, m5
%endif
%if %2 == 1 ; avg
  ; FIXME(rbultje) pipeline
  packuswb             m0, m2
  pavgb                m0, [secq]
  punpckhbw            m2, m0, m5
  punpcklbw            m0, m5
%endif
  SUM_SSE              m0, m1, m2, m3, m6, m7
  mova                 m0, m4

  INC_SRC_BY_SRC_STRIDE
  lea                dstq, [dstq+dst_strideq*2]
%endif
%if %2 == 1 ; avg
  add                secq, sec_str
%endif
  dec                   h
  jg .x_other_y_other_loop
%undef filter_x_a
%undef filter_x_b
%undef filter_y_a
%undef filter_y_b
%undef filter_rnd
  STORE_AND_RET
%endmacro

; FIXME(rbultje) the non-bilinear versions (i.e. x=0,8&&y=0,8) are identical
; between the ssse3 and non-ssse3 version. It may make sense to merge their
; code in the sense that the ssse3 version would jump to the appropriate
; location in the sse/2 version, rather than duplicating that code in the
; binary.

INIT_MMX sse
SUBPEL_VARIANCE  4
INIT_XMM sse2
SUBPEL_VARIANCE  8
SUBPEL_VARIANCE 16

INIT_MMX ssse3
SUBPEL_VARIANCE  4
INIT_XMM ssse3
SUBPEL_VARIANCE  8
SUBPEL_VARIANCE 16

INIT_MMX sse
SUBPEL_VARIANCE  4, 1
INIT_XMM sse2
SUBPEL_VARIANCE  8, 1
SUBPEL_VARIANCE 16, 1

INIT_MMX ssse3
SUBPEL_VARIANCE  4, 1
INIT_XMM ssse3
SUBPEL_VARIANCE  8, 1
SUBPEL_VARIANCE 16, 1