summaryrefslogtreecommitdiffstats
path: root/third_party/dav1d/src/x86/looprestoration16_sse.asm
blob: 872e5029824330c8330ea40564091945677c3de3 (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
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
; Copyright © 2021, VideoLAN and dav1d authors
; Copyright © 2021, Two Orioles, LLC
; 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

wiener_shufA:  db  2,  3,  4,  5,  4,  5,  6,  7,  6,  7,  8,  9,  8,  9, 10, 11
wiener_shufB:  db  6,  7,  4,  5,  8,  9,  6,  7, 10, 11,  8,  9, 12, 13, 10, 11
wiener_shufC:  db  6,  7,  8,  9,  8,  9, 10, 11, 10, 11, 12, 13, 12, 13, 14, 15
wiener_shufD:  db  2,  3, -1, -1,  4,  5, -1, -1,  6,  7, -1, -1,  8,  9, -1, -1
wiener_shufE:  db  0,  1,  8,  9,  2,  3, 10, 11,  4,  5, 12, 13,  6,  7, 14, 15
wiener_lshuf5: db  0,  1,  0,  1,  0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11
wiener_lshuf7: db  0,  1,  0,  1,  0,  1,  0,  1,  0,  1,  2,  3,  4,  5,  6,  7
sgr_lshuf3:    db  0,  1,  0,  1,  0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11
sgr_lshuf5:    db  0,  1,  0,  1,  0,  1,  0,  1,  2,  3,  4,  5,  6,  7,  8,  9
pb_0to15:      db  0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15

pb_m14_m13:    times 8 db -14,-13
pb_m10_m9:     times 8 db -10, -9
pb_m6_m5:      times 8 db  -6, -5
pb_m2_m1:      times 8 db  -2, -1
pb_2_3:        times 8 db   2,  3
pb_6_7:        times 8 db   6,  7
pw_256:        times 8 dw 256
pw_1023:       times 8 dw 1023
pd_8:          times 4 dd 8
pd_4096:       times 4 dd 4096
pd_34816:      times 4 dd 34816
pd_m262128:    times 4 dd -262128
pd_0xffff:     times 4 dd 0xffff
pd_0xf00800a4: times 4 dd 0xf00800a4
pd_0xf00801c7: times 4 dd 0xf00801c7
pd_0xfffffff0: times 4 dd 0xfffffff0

wiener_shifts: dw 4, 4, 2048, 2048, 1, 1, 8192, 8192
wiener_round:  dd 1049600, 1048832

cextern sgr_x_by_x

SECTION .text

%macro movif64 2 ; dst, src
 %if ARCH_X86_64
    mov             %1, %2
 %endif
%endmacro

%macro movif32 2 ; dst, src
 %if ARCH_X86_32
    mov             %1, %2
 %endif
%endmacro

INIT_XMM ssse3
%if ARCH_X86_32
DECLARE_REG_TMP 5, 6
 %if STACK_ALIGNMENT < 16
  %assign extra_stack 13*16
 %else
  %assign extra_stack 12*16
 %endif
cglobal wiener_filter7_16bpc, 4, 7, 8, -384*12-16-extra_stack, \
                              dst, stride, left, lpf, w, flt
 %if STACK_ALIGNMENT < 16
  %define lpfm        dword [esp+calloff+16*12+ 0]
  %define wm          dword [esp+calloff+16*12+ 4]
  %define hd          dword [esp+calloff+16*12+ 8]
  %define edgeb        byte [esp+calloff+16*12+12]
  %define edged       dword [esp+calloff+16*12+12]
 %else
  %define hd dword r5m
  %define edgeb byte r7m
 %endif
 %define PICmem dword [esp+calloff+4*0]
 %define t0m    dword [esp+calloff+4*1] ; wiener ring buffer pointers
 %define t1m    dword [esp+calloff+4*2]
 %define t2m    dword [esp+calloff+4*3]
 %define t3m    dword [esp+calloff+4*4]
 %define t4m    dword [esp+calloff+4*5]
 %define t5m    dword [esp+calloff+4*6]
 %define t6m    dword [esp+calloff+4*7]
 %define t2 t2m
 %define t3 t3m
 %define t4 t4m
 %define t5 t5m
 %define t6 t6m
 %define  m8 [esp+calloff+16*2]
 %define  m9 [esp+calloff+16*3]
 %define m10 [esp+calloff+16*4]
 %define m11 [esp+calloff+16*5]
 %define m12 [esp+calloff+16*6]
 %define m13 [esp+calloff+16*7]
 %define m14 [esp+calloff+16*8]
 %define m15 [esp+calloff+16*9]
 %define r10 r4
 %define base t0-wiener_shifts
 %assign calloff 0
 %if STACK_ALIGNMENT < 16
    mov             wd, [rstk+stack_offset+20]
    mov             wm, wd
    mov             r5, [rstk+stack_offset+24]
    mov             hd, r5
    mov             r5, [rstk+stack_offset+32]
    mov          edged, r5 ; edge
 %endif
%else
DECLARE_REG_TMP 8, 7, 9, 11, 12, 13, 14 ; wiener ring buffer pointers
cglobal wiener_filter7_16bpc, 4, 15, 16, -384*12-16, dst, stride, left, lpf, \
                                                     w, h, edge, flt
 %define base
%endif
%if ARCH_X86_64 || STACK_ALIGNMENT >= 16
    movifnidn       wd, wm
%endif
%if ARCH_X86_64
    mov           fltq, r6mp
    movifnidn       hd, hm
    mov          edged, r7m
    mov            t3d, r8m ; pixel_max
    movq           m13, [fltq]
    movq           m15, [fltq+16]
%else
 %if STACK_ALIGNMENT < 16
    mov             t0, [rstk+stack_offset+28]
    mov             t1, [rstk+stack_offset+36] ; pixel_max
    movq            m1, [t0]    ; fx
    movq            m3, [t0+16] ; fy
    LEA             t0, wiener_shifts
 %else
    mov           fltq, r6m
    movq            m1, [fltq]
    movq            m3, [fltq+16]
    LEA             t0, wiener_shifts
    mov             t1, r8m ; pixel_max
 %endif
    mov         PICmem, t0
%endif
    mova            m6, [base+wiener_shufA]
    mova            m7, [base+wiener_shufB]
%if ARCH_X86_64
    lea             t4, [wiener_shifts]
    add             wd, wd
    pshufd         m12, m13, q0000 ; x0 x1
    pshufd         m13, m13, q1111 ; x2 x3
    pshufd         m14, m15, q0000 ; y0 y1
    pshufd         m15, m15, q1111 ; y2 y3
    mova            m8, [wiener_shufC]
    mova            m9, [wiener_shufD]
    add           lpfq, wq
    lea             t1, [rsp+wq+16]
    add           dstq, wq
    neg             wq
    shr            t3d, 11
 %define base t4-wiener_shifts
    movd           m10, [base+wiener_round+t3*4]
    movq           m11, [base+wiener_shifts+t3*8]
    pshufd         m10, m10, q0000
    pshufd          m0, m11, q0000
    pshufd         m11, m11, q1111
    pmullw         m12, m0 ; upshift filter coefs to make the
    pmullw         m13, m0 ; horizontal downshift constant
 DEFINE_ARGS dst, stride, left, lpf, _, h, edge, _, _, _, w
 %define lpfm [rsp]
 %define base
 %define wiener_lshuf7_mem [wiener_lshuf7]
 %define pd_m262128_mem [pd_m262128]
%else
    add             wd, wd
    mova            m4, [base+wiener_shufC]
    mova            m5, [base+wiener_shufD]
    pshufd          m0, m1, q0000
    pshufd          m1, m1, q1111
    pshufd          m2, m3, q0000
    pshufd          m3, m3, q1111
    mova            m8, m4
    mova            m9, m5
    mova           m14, m2
    mova           m15, m3
    shr             t1, 11
    add           lpfq, wq
    mova            m3, [base+pd_m262128]
    movd            m4, [base+wiener_round+t1*4]
    movq            m5, [base+wiener_shifts+t1*8]
    lea             t1, [esp+extra_stack+wq+16]
    add           dstq, wq
    neg             wq
    pshufd          m4, m4, q0000
    pshufd          m2, m5, q0000
    pshufd          m5, m5, q1111
    mov             wm, wq
    pmullw          m0, m2
    pmullw          m1, m2
    mova            m2, [base+wiener_lshuf7]
 %define pd_m262128_mem [esp+calloff+16*10]
    mova pd_m262128_mem, m3
    mova           m10, m4
    mova           m11, m5
    mova           m12, m0
    mova           m13, m1
 %define wiener_lshuf7_mem [esp+calloff+16*11]
    mova wiener_lshuf7_mem, m2
%endif
    test         edgeb, 4 ; LR_HAVE_TOP
    jz .no_top
    call .h_top
    add           lpfq, strideq
    mov             t6, t1
    mov             t5, t1
    add             t1, 384*2
    call .h_top
    lea            r10, [lpfq+strideq*4]
    mov           lpfq, dstq
    mov             t4, t1
    add             t1, 384*2
    add            r10, strideq
    mov           lpfm, r10 ; below
    call .h
    mov             t3, t1
    mov             t2, t1
    dec             hd
    jz .v1
    add           lpfq, strideq
    add             t1, 384*2
    call .h
    mov             t2, t1
    dec             hd
    jz .v2
    add           lpfq, strideq
    add             t1, 384*2
    call .h
    dec             hd
    jz .v3
.main:
    lea             t0, [t1+384*2]
.main_loop:
    call .hv
    dec             hd
    jnz .main_loop
    test         edgeb, 8 ; LR_HAVE_BOTTOM
    jz .v3
    mov           lpfq, lpfm
    call .hv_bottom
    add           lpfq, strideq
    call .hv_bottom
.v1:
    call .v
    RET
.no_top:
    lea            r10, [lpfq+strideq*4]
    mov           lpfq, dstq
    lea            r10, [r10+strideq*2]
    mov           lpfm, r10
    call .h
    mov             t6, t1
    mov             t5, t1
    mov             t4, t1
    mov             t3, t1
    mov             t2, t1
    dec             hd
    jz .v1
    add           lpfq, strideq
    add             t1, 384*2
    call .h
    mov             t2, t1
    dec             hd
    jz .v2
    add           lpfq, strideq
    add             t1, 384*2
    call .h
    dec             hd
    jz .v3
    lea             t0, [t1+384*2]
    call .hv
    dec             hd
    jz .v3
    add             t0, 384*8
    call .hv
    dec             hd
    jnz .main
.v3:
    call .v
    movif32         wq, wm
.v2:
    call .v
    movif32         wq, wm
    jmp .v1
.extend_right:
%assign stack_offset stack_offset+8
%assign calloff 8
    movif32         t0, PICmem
    pxor            m0, m0
    movd            m1, wd
    mova            m2, [base+pb_0to15]
    pshufb          m1, m0
    mova            m0, [base+pb_6_7]
    psubb           m0, m1
    pminub          m0, m2
    pshufb          m3, m0
    mova            m0, [base+pb_m2_m1]
    psubb           m0, m1
    pminub          m0, m2
    pshufb          m4, m0
    mova            m0, [base+pb_m10_m9]
    psubb           m0, m1
    pminub          m0, m2
    pshufb          m5, m0
    movif32         t0, t0m
    ret
%assign stack_offset stack_offset-4
%assign calloff 4
.h:
    movif64         wq, r4
    movif32         wq, wm
    test         edgeb, 1 ; LR_HAVE_LEFT
    jz .h_extend_left
    movq            m3, [leftq]
    movhps          m3, [lpfq+wq]
    add          leftq, 8
    jmp .h_main
.h_extend_left:
    mova            m3, [lpfq+wq]         ; avoid accessing memory located
    pshufb          m3, wiener_lshuf7_mem ; before the start of the buffer
    jmp .h_main
.h_top:
    movif64         wq, r4
    test         edgeb, 1 ; LR_HAVE_LEFT
    jz .h_extend_left
.h_loop:
    movu            m3, [lpfq+wq-8]
.h_main:
    mova            m4, [lpfq+wq+0]
    movu            m5, [lpfq+wq+8]
    test         edgeb, 2 ; LR_HAVE_RIGHT
    jnz .h_have_right
    cmp             wd, -20
    jl .h_have_right
    call .extend_right
.h_have_right:
    pshufb          m0, m3, m6
    pshufb          m1, m4, m7
    paddw           m0, m1
    pshufb          m3, m8
    pmaddwd         m0, m12
    pshufb          m1, m4, m9
    paddw           m3, m1
    pshufb          m1, m4, m6
    pmaddwd         m3, m13
    pshufb          m2, m5, m7
    paddw           m1, m2
    mova            m2, pd_m262128_mem ; (1 << 4) - (1 << 18)
    pshufb          m4, m8
    pmaddwd         m1, m12
    pshufb          m5, m9
    paddw           m4, m5
    pmaddwd         m4, m13
    paddd           m0, m2
    paddd           m1, m2
    paddd           m0, m3
    paddd           m1, m4
    psrad           m0, 4
    psrad           m1, 4
    packssdw        m0, m1
    psraw           m0, 1
    mova       [t1+wq], m0
    add             wq, 16
    jl .h_loop
    movif32         wq, wm
    ret
ALIGN function_align
.hv:
    add           lpfq, strideq
    movif64         wq, r4
    movif32        t0m, t0
    movif32        t1m, t1
    test         edgeb, 1 ; LR_HAVE_LEFT
    jz .hv_extend_left
    movq            m3, [leftq]
    movhps          m3, [lpfq+wq]
    add          leftq, 8
    jmp .hv_main
.hv_extend_left:
    mova            m3, [lpfq+wq]
    pshufb          m3, wiener_lshuf7_mem
    jmp .hv_main
.hv_bottom:
    movif64         wq, r4
    movif32        t0m, t0
    movif32        t1m, t1
    test         edgeb, 1 ; LR_HAVE_LEFT
    jz .hv_extend_left
.hv_loop:
    movu            m3, [lpfq+wq-8]
.hv_main:
    mova            m4, [lpfq+wq+0]
    movu            m5, [lpfq+wq+8]
    test         edgeb, 2 ; LR_HAVE_RIGHT
    jnz .hv_have_right
    cmp             wd, -20
    jl .hv_have_right
    call .extend_right
.hv_have_right:
    movif32         t1, t4m
    movif32         t0, t2m
    pshufb          m0, m3, m6
    pshufb          m1, m4, m7
    paddw           m0, m1
    pshufb          m3, m8
    pmaddwd         m0, m12
    pshufb          m1, m4, m9
    paddw           m3, m1
    pshufb          m1, m4, m6
    pmaddwd         m3, m13
    pshufb          m2, m5, m7
    paddw           m1, m2
    mova            m2, pd_m262128_mem
    pshufb          m4, m8
    pmaddwd         m1, m12
    pshufb          m5, m9
    paddw           m4, m5
    pmaddwd         m4, m13
    paddd           m0, m2
    paddd           m1, m2
%if ARCH_X86_64
    mova            m2, [t4+wq]
    paddw           m2, [t2+wq]
    mova            m5, [t3+wq]
%else
    mova            m2, [t1+wq]
    paddw           m2, [t0+wq]
    mov             t1, t3m
    mov             t0, t5m
    mova            m5, [t1+wq]
    mov             t1, t1m
%endif
    paddd           m0, m3
    paddd           m1, m4
    psrad           m0, 4
    psrad           m1, 4
    packssdw        m0, m1
%if ARCH_X86_64
    mova            m4, [t5+wq]
    paddw           m4, [t1+wq]
    psraw           m0, 1
    paddw           m3, m0, [t6+wq]
%else
    mova            m4, [t0+wq]
    paddw           m4, [t1+wq]
    mov             t0, t0m
    mov             t1, t6m
    psraw           m0, 1
    paddw           m3, m0, [t1+wq]
%endif
    mova       [t0+wq], m0
    punpcklwd       m0, m2, m5
    pmaddwd         m0, m15
    punpckhwd       m2, m5
    pmaddwd         m2, m15
    punpcklwd       m1, m3, m4
    pmaddwd         m1, m14
    punpckhwd       m3, m4
    pmaddwd         m3, m14
    paddd           m0, m10
    paddd           m2, m10
    paddd           m0, m1
    paddd           m2, m3
    psrad           m0, 6
    psrad           m2, 6
    packssdw        m0, m2
    pmulhw          m0, m11
    pxor            m1, m1
    pmaxsw          m0, m1
    mova     [dstq+wq], m0
    add             wq, 16
    jl .hv_loop
%if ARCH_X86_64
    mov             t6, t5
    mov             t5, t4
    mov             t4, t3
    mov             t3, t2
    mov             t2, t1
    mov             t1, t0
    mov             t0, t6
%else
    mov             r4, t5m
    mov             t1, t4m
    mov            t6m, r4
    mov            t5m, t1
    mov             r4, t3m
    mov             t1, t2m
    mov            t4m, r4
    mov            t3m, t1
    mov             r4, t1m
    mov             t1, t0
    mov            t2m, r4
    mov             t0, t6m
    mov             wq, wm
%endif
    add           dstq, strideq
    ret
.v:
    movif64         wq, r4
    movif32        t0m, t0
    movif32        t1m, t1
.v_loop:
%if ARCH_X86_64
    mova            m1, [t4+wq]
    paddw           m1, [t2+wq]
    mova            m2, [t3+wq]
    mova            m4, [t1+wq]
    paddw           m3, m4, [t6+wq]
    paddw           m4, [t5+wq]
%else
    mov             t0, t4m
    mov             t1, t2m
    mova            m1, [t0+wq]
    paddw           m1, [t1+wq]
    mov             t0, t3m
    mov             t1, t1m
    mova            m2, [t0+wq]
    mova            m4, [t1+wq]
    mov             t0, t6m
    mov             t1, t5m
    paddw           m3, m4, [t0+wq]
    paddw           m4, [t1+wq]
%endif
    punpcklwd       m0, m1, m2
    pmaddwd         m0, m15
    punpckhwd       m1, m2
    pmaddwd         m1, m15
    punpcklwd       m2, m3, m4
    pmaddwd         m2, m14
    punpckhwd       m3, m4
    pmaddwd         m3, m14
    paddd           m0, m10
    paddd           m1, m10
    paddd           m0, m2
    paddd           m1, m3
    psrad           m0, 6
    psrad           m1, 6
    packssdw        m0, m1
    pmulhw          m0, m11
    pxor            m1, m1
    pmaxsw          m0, m1
    mova     [dstq+wq], m0
    add             wq, 16
    jl .v_loop
%if ARCH_X86_64
    mov             t6, t5
    mov             t5, t4
    mov             t4, t3
    mov             t3, t2
    mov             t2, t1
%else
    mov             t0, t5m
    mov             t1, t4m
    mov             r4, t3m
    mov            t6m, t0
    mov            t5m, t1
    mov            t4m, r4
    mov             r4, t2m
    mov             t1, t1m
    mov             t0, t0m
    mov            t3m, r4
    mov            t2m, t1
%endif
    add           dstq, strideq
    ret

%if ARCH_X86_32
 %if STACK_ALIGNMENT < 16
  %assign stack_size 12*16+384*8
 %else
  %assign stack_size 11*16+384*8
 %endif
cglobal wiener_filter5_16bpc, 4, 7, 8, -stack_size, dst, stride, left, \
                                                    lpf, w, flt
 %if STACK_ALIGNMENT < 16
  %define lpfm        dword [esp+calloff+4*6]
  %define wm          dword [esp+calloff+4*7]
  %define hd          dword [esp+calloff+16*10+0]
  %define edgeb        byte [esp+calloff+16*10+4]
  %define edged       dword [esp+calloff+16*10+4]
 %else
  %define hd dword r5m
  %define edgeb byte r7m
 %endif
 %define PICmem dword [esp+calloff+4*0]
 %define t0m    dword [esp+calloff+4*1] ; wiener ring buffer pointers
 %define t1m    dword [esp+calloff+4*2]
 %define t2m    dword [esp+calloff+4*3]
 %define t3m    dword [esp+calloff+4*4]
 %define t4m    dword [esp+calloff+4*5]
 %define t2 t2m
 %define t3 t3m
 %define t4 t4m
 %define  m8 [esp+calloff+16*2]
 %define  m9 [esp+calloff+16*3]
 %define m10 [esp+calloff+16*4]
 %define m11 [esp+calloff+16*5]
 %define m12 [esp+calloff+16*6]
 %define m13 [esp+calloff+16*7]
 %define m14 [esp+calloff+16*8]
 %define m15 [esp+calloff+16*9]
 %define base t0-wiener_shifts
 %assign calloff 0
 %if STACK_ALIGNMENT < 16
    mov             wd, [rstk+stack_offset+20]
    mov             wm, wd
    mov             r5, [rstk+stack_offset+24]
    mov             hd, r5
    mov             r5, [rstk+stack_offset+32]
    mov          edged, r5 ; edge
 %endif
%else
cglobal wiener_filter5_16bpc, 4, 14, 16, 384*8+16, dst, stride, left, lpf, \
                                                   w, h, edge, flt
 %define base
%endif
%if ARCH_X86_64 || STACK_ALIGNMENT >= 16
    movifnidn       wd, wm
%endif
%if ARCH_X86_64
    mov           fltq, r6mp
    movifnidn       hd, hm
    mov          edged, r7m
    mov            t3d, r8m ; pixel_max
    movq           m12, [fltq]
    movq           m14, [fltq+16]
%else
 %if STACK_ALIGNMENT < 16
    mov             t0, [rstk+stack_offset+28]
    mov             t1, [rstk+stack_offset+36] ; pixel_max
    movq            m1, [t0]    ; fx
    movq            m3, [t0+16] ; fy
    LEA             t0, wiener_shifts
 %else
    mov           fltq, r6m
    movq            m1, [fltq]
    movq            m3, [fltq+16]
    LEA             t0, wiener_shifts
    mov             t1, r8m ; pixel_max
 %endif
    mov         PICmem, t0
%endif
    mova            m5, [base+wiener_shufE]
    mova            m6, [base+wiener_shufB]
    mova            m7, [base+wiener_shufD]
%if ARCH_X86_64
    lea             t4, [wiener_shifts]
    add             wd, wd
    punpcklwd      m11, m12, m12
    pshufd         m11, m11, q1111 ; x1
    pshufd         m12, m12, q1111 ; x2 x3
    punpcklwd      m13, m14, m14
    pshufd         m13, m13, q1111 ; y1
    pshufd         m14, m14, q1111 ; y2 y3
    shr            t3d, 11
    mova            m8, [pd_m262128] ; (1 << 4) - (1 << 18)
    add           lpfq, wq
    lea             t1, [rsp+wq+16]
    add           dstq, wq
    neg             wq
 %define base t4-wiener_shifts
    movd            m9, [base+wiener_round+t3*4]
    movq           m10, [base+wiener_shifts+t3*8]
    pshufd          m9, m9, q0000
    pshufd          m0, m10, q0000
    pshufd         m10, m10, q1111
    mova           m15, [wiener_lshuf5]
    pmullw         m11, m0
    pmullw         m12, m0
 DEFINE_ARGS dst, stride, left, lpf, _, h, edge, _, _, _, w
 %define lpfm [rsp]
 %define base
%else
    add             wd, wd
    punpcklwd       m0, m1, m1
    pshufd          m0, m0, q1111 ; x1
    pshufd          m1, m1, q1111 ; x2 x3
    punpcklwd       m2, m3, m3
    pshufd          m2, m2, q1111 ; y1
    pshufd          m3, m3, q1111 ; y2 y3
    mova            m4, [base+pd_m262128] ; (1 << 4) - (1 << 18)
    mova           m13, m2
    mova           m14, m3
    mova            m8, m4
    shr             t1, 11
    add           lpfq, wq
    movd            m2, [base+wiener_round+t1*4]
    movq            m3, [base+wiener_shifts+t1*8]
 %if STACK_ALIGNMENT < 16
    lea             t1, [esp+16*11+wq+16]
 %else
    lea             t1, [esp+16*10+wq+16]
 %endif
    add           dstq, wq
    neg             wq
    pshufd          m2, m2, q0000
    pshufd          m4, m3, q0000
    pshufd          m3, m3, q1111
    mov             wm, wq
    pmullw          m0, m4
    pmullw          m1, m4
    mova            m4, [base+wiener_lshuf5]
    mova            m9, m2
    mova           m10, m3
    mova           m11, m0
    mova           m12, m1
    mova           m15, m4
%endif
    test         edgeb, 4 ; LR_HAVE_TOP
    jz .no_top
    call .h_top
    add           lpfq, strideq
    mov             t4, t1
    add             t1, 384*2
    call .h_top
    lea            r10, [lpfq+strideq*4]
    mov           lpfq, dstq
    mov             t3, t1
    add             t1, 384*2
    add            r10, strideq
    mov           lpfm, r10 ; below
    call .h
    mov             t2, t1
    dec             hd
    jz .v1
    add           lpfq, strideq
    add             t1, 384*2
    call .h
    dec             hd
    jz .v2
.main:
    mov             t0, t4
.main_loop:
    call .hv
    dec             hd
    jnz .main_loop
    test         edgeb, 8 ; LR_HAVE_BOTTOM
    jz .v2
    mov           lpfq, lpfm
    call .hv_bottom
    add           lpfq, strideq
    call .hv_bottom
.end:
    RET
.no_top:
    lea            r10, [lpfq+strideq*4]
    mov           lpfq, dstq
    lea            r10, [r10+strideq*2]
    mov           lpfm, r10
    call .h
    mov             t4, t1
    mov             t3, t1
    mov             t2, t1
    dec             hd
    jz .v1
    add           lpfq, strideq
    add             t1, 384*2
    call .h
    dec             hd
    jz .v2
    lea             t0, [t1+384*2]
    call .hv
    dec             hd
    jz .v2
    add             t0, 384*6
    call .hv
    dec             hd
    jnz .main
.v2:
    call .v
%if ARCH_X86_64
    mov             t4, t3
    mov             t3, t2
    mov             t2, t1
%else
    mov             t0, t3m
    mov             r4, t2m
    mov             t1, t1m
    mov            t4m, t0
    mov            t3m, r4
    mov            t2m, t1
    mov             wq, wm
%endif
    add           dstq, strideq
.v1:
    call .v
    jmp .end
.extend_right:
%assign stack_offset stack_offset+8
%assign calloff 8
    movif32         t0, PICmem
    pxor            m1, m1
    movd            m2, wd
    mova            m0, [base+pb_2_3]
    pshufb          m2, m1
    mova            m1, [base+pb_m6_m5]
    psubb           m0, m2
    psubb           m1, m2
    mova            m2, [base+pb_0to15]
    pminub          m0, m2
    pminub          m1, m2
    pshufb          m3, m0
    pshufb          m4, m1
    ret
%assign stack_offset stack_offset-4
%assign calloff 4
.h:
    movif64         wq, r4
    movif32         wq, wm
    test         edgeb, 1 ; LR_HAVE_LEFT
    jz .h_extend_left
    mova            m4, [lpfq+wq]
    movd            m3, [leftq+4]
    pslldq          m4, 4
    por             m3, m4
    add          leftq, 8
    jmp .h_main
.h_extend_left:
    mova            m3, [lpfq+wq] ; avoid accessing memory located
    pshufb          m3, m15       ; before the start of the buffer
    jmp .h_main
.h_top:
    movif64         wq, r4
    movif32         wq, wm
    test         edgeb, 1 ; LR_HAVE_LEFT
    jz .h_extend_left
.h_loop:
    movu            m3, [lpfq+wq-4]
.h_main:
    movu            m4, [lpfq+wq+4]
    test         edgeb, 2 ; LR_HAVE_RIGHT
    jnz .h_have_right
    cmp             wd, -18
    jl .h_have_right
    call .extend_right
.h_have_right:
    pshufb          m0, m3, m5
    pmaddwd         m0, m11
    pshufb          m1, m4, m5
    pmaddwd         m1, m11
    pshufb          m2, m3, m6
    pshufb          m3, m7
    paddw           m2, m3
    pshufb          m3, m4, m6
    pmaddwd         m2, m12
    pshufb          m4, m7
    paddw           m3, m4
    pmaddwd         m3, m12
    paddd           m0, m8
    paddd           m1, m8
    paddd           m0, m2
    paddd           m1, m3
    psrad           m0, 4
    psrad           m1, 4
    packssdw        m0, m1
    psraw           m0, 1
    mova       [t1+wq], m0
    add             wq, 16
    jl .h_loop
    movif32         wq, wm
    ret
ALIGN function_align
.hv:
    add           lpfq, strideq
    movif64         wq, r4
    movif32        t0m, t0
    movif32        t1m, t1
    test         edgeb, 1 ; LR_HAVE_LEFT
    jz .hv_extend_left
    mova            m4, [lpfq+wq]
    movd            m3, [leftq+4]
    pslldq          m4, 4
    por             m3, m4
    add          leftq, 8
    jmp .hv_main
.hv_extend_left:
    mova            m3, [lpfq+wq]
    pshufb          m3, m15
    jmp .hv_main
.hv_bottom:
    movif64         wq, r4
    movif32        t0m, t0
    movif32        t1m, t1
    test         edgeb, 1 ; LR_HAVE_LEFT
    jz .hv_extend_left
.hv_loop:
    movu            m3, [lpfq+wq-4]
.hv_main:
    movu            m4, [lpfq+wq+4]
    test         edgeb, 2 ; LR_HAVE_RIGHT
    jnz .hv_have_right
    cmp             wd, -18
    jl .hv_have_right
    call .extend_right
.hv_have_right:
    movif32         t1, t1m
    movif32         t0, t3m
    pshufb          m0, m3, m5
    pmaddwd         m0, m11
    pshufb          m1, m4, m5
    pmaddwd         m1, m11
    pshufb          m2, m3, m6
    pshufb          m3, m7
    paddw           m2, m3
    pshufb          m3, m4, m6
    pmaddwd         m2, m12
    pshufb          m4, m7
    paddw           m3, m4
    pmaddwd         m3, m12
    paddd           m0, m8
    paddd           m1, m8
    paddd           m0, m2
%if ARCH_X86_64
    mova            m2, [t3+wq]
    paddw           m2, [t1+wq]
    paddd           m1, m3
    mova            m4, [t2+wq]
%else
    mova            m2, [t0+wq]
    mov             t0, t2m
    paddw           m2, [t1+wq]
    mov             t1, t4m
    paddd           m1, m3
    mova            m4, [t0+wq]
    mov             t0, t0m
%endif
    punpckhwd       m3, m2, m4
    pmaddwd         m3, m14
    punpcklwd       m2, m4
%if ARCH_X86_64
    mova            m4, [t4+wq]
%else
    mova            m4, [t1+wq]
%endif
    psrad           m0, 4
    psrad           m1, 4
    packssdw        m0, m1
    pmaddwd         m2, m14
    psraw           m0, 1
    mova       [t0+wq], m0
    punpckhwd       m1, m0, m4
    pmaddwd         m1, m13
    punpcklwd       m0, m4
    pmaddwd         m0, m13
    paddd           m3, m9
    paddd           m2, m9
    paddd           m1, m3
    paddd           m0, m2
    psrad           m1, 6
    psrad           m0, 6
    packssdw        m0, m1
    pmulhw          m0, m10
    pxor            m1, m1
    pmaxsw          m0, m1
    mova     [dstq+wq], m0
    add             wq, 16
    jl .hv_loop
%if ARCH_X86_64
    mov             t4, t3
    mov             t3, t2
    mov             t2, t1
    mov             t1, t0
    mov             t0, t4
%else
    mov             r4, t3m
    mov             t1, t2m
    mov            t4m, r4
    mov            t3m, t1
    mov             r4, t1m
    mov             t1, t0
    mov            t2m, r4
    mov             t0, t4m
    mov             wq, wm
%endif
    add           dstq, strideq
    ret
.v:
    movif64         wq, r4
    movif32        t1m, t1
.v_loop:
%if ARCH_X86_64
    mova            m0, [t1+wq]
    paddw           m2, m0, [t3+wq]
    mova            m1, [t2+wq]
    mova            m4, [t4+wq]
%else
    mov             t0, t3m
    mova            m0, [t1+wq]
    mov             t1, t2m
    paddw           m2, m0, [t0+wq]
    mov             t0, t4m
    mova            m1, [t1+wq]
    mova            m4, [t0+wq]
%endif
    punpckhwd       m3, m2, m1
    pmaddwd         m3, m14
    punpcklwd       m2, m1
    pmaddwd         m2, m14
    punpckhwd       m1, m0, m4
    pmaddwd         m1, m13
    punpcklwd       m0, m4
    pmaddwd         m0, m13
    paddd           m3, m9
    paddd           m2, m9
    paddd           m1, m3
    paddd           m0, m2
    psrad           m1, 6
    psrad           m0, 6
    packssdw        m0, m1
    pmulhw          m0, m10
    pxor            m1, m1
    pmaxsw          m0, m1
    mova     [dstq+wq], m0
    add             wq, 16
%if ARCH_X86_64
    jl .v_loop
%else
    jge .v_end
    mov             t1, t1m
    jmp .v_loop
.v_end:
%endif
    ret

%macro GATHERDD 3 ; dst, src, tmp
    movd           %3d, %2
 %if ARCH_X86_64
    movd            %1, [r13+%3]
    pextrw         %3d, %2, 2
    pinsrw          %1, [r13+%3+2], 3
    pextrw         %3d, %2, 4
    pinsrw          %1, [r13+%3+2], 5
    pextrw         %3d, %2, 6
    pinsrw          %1, [r13+%3+2], 7
 %else
    movd            %1, [base+sgr_x_by_x-0xf03+%3]
    pextrw          %3, %2, 2
    pinsrw          %1, [base+sgr_x_by_x-0xf03+%3+2], 3
    pextrw          %3, %2, 4
    pinsrw          %1, [base+sgr_x_by_x-0xf03+%3+2], 5
    pextrw          %3, %2, 6
    pinsrw          %1, [base+sgr_x_by_x-0xf03+%3+2], 7
 %endif
%endmacro

%macro GATHER_X_BY_X 5 ; dst, src0, src1, tmp32, tmp32_restore
 %if ARCH_X86_64
  %define tmp r14
 %else
  %define tmp %4
 %endif
    GATHERDD        %1, %2, tmp
    GATHERDD        %2, %3, tmp
    movif32         %4, %5
    psrld           %1, 24
    psrld           %2, 24
    packssdw        %1, %2
%endmacro

%macro MAXSD 3-4 0 ; dst, src, restore_tmp
    pcmpgtd         %3, %1, %2
    pand            %1, %3
    pandn           %3, %2
    por             %1, %3
 %if %4 == 1
    pxor            %3, %3
 %endif
%endmacro

%macro MULLD 3 ; dst, src, tmp
    pmulhuw         %3, %1, %2
    pmullw          %1, %2
    pslld           %3, 16
    paddd           %1, %3
%endmacro

%if ARCH_X86_32
DECLARE_REG_TMP 0, 1, 2, 3, 5
 %if STACK_ALIGNMENT < 16
  %assign extra_stack 5*16
 %else
  %assign extra_stack 3*16
 %endif
cglobal sgr_filter_5x5_16bpc, 1, 7, 8, -400*24-16-extra_stack, \
                              dst, stride, left, lpf, w
 %if STACK_ALIGNMENT < 16
  %define dstm         dword [esp+calloff+16*0+4*6]
  %define stridemp     dword [esp+calloff+16*0+4*7]
  %define leftm        dword [esp+calloff+16*3+4*0]
  %define lpfm         dword [esp+calloff+16*3+4*1]
  %define w0m          dword [esp+calloff+16*3+4*2]
  %define hd           dword [esp+calloff+16*3+4*3]
  %define edgeb         byte [esp+calloff+16*3+4*4]
  %define edged        dword [esp+calloff+16*3+4*4]
  %define leftmp leftm
 %else
  %define w0m wm
  %define hd dword r5m
  %define edgeb  byte r7m
  %define edged dword r7m
 %endif
 %define hvsrcm dword [esp+calloff+4*0]
 %define w1m    dword [esp+calloff+4*1]
 %define t0m    dword [esp+calloff+4*2]
 %define t2m    dword [esp+calloff+4*3]
 %define t3m    dword [esp+calloff+4*4]
 %define t4m    dword [esp+calloff+4*5]
 %define  m8 [base+pd_8]
 %define  m9 [base+pd_0xfffffff0]
 %define m10 [esp+calloff+16*2]
 %define m11 [base+pd_0xf00800a4]
 %define m12 [base+sgr_lshuf5]
 %define m13 [base+pd_34816]
 %define m14 [base+pw_1023]
 %define r10 r4
 %define base r6-$$
 %assign calloff 0
 %if STACK_ALIGNMENT < 16
    mov        strideq, [rstk+stack_offset+ 8]
    mov          leftq, [rstk+stack_offset+12]
    mov           lpfq, [rstk+stack_offset+16]
    mov             wd, [rstk+stack_offset+20]
    mov           dstm, dstq
    mov       stridemp, strideq
    mov          leftm, leftq
    mov             r1, [rstk+stack_offset+24]
    mov             r2, [rstk+stack_offset+32]
    mov           lpfm, lpfq
    mov             hd, r1
    mov          edged, r2
 %endif
%else
cglobal sgr_filter_5x5_16bpc, 4, 15, 15, -400*24-16, dst, stride, left, lpf, \
                                                     w, h, edge, params
%endif
%if ARCH_X86_64 || STACK_ALIGNMENT >= 16
    movifnidn       wd, wm
%endif
%if ARCH_X86_64
    mov        paramsq, r6mp
    lea            r13, [sgr_x_by_x-0xf03]
    movifnidn       hd, hm
    add             wd, wd
    mov          edged, r7m
    movu           m10, [paramsq]
    mova           m12, [sgr_lshuf5]
    add           lpfq, wq
    mova            m8, [pd_8]
    lea             t1, [rsp+wq+20]
    mova            m9, [pd_0xfffffff0]
    add           dstq, wq
    lea             t3, [rsp+wq*2+400*12+16]
    mova           m11, [pd_0xf00800a4]
    lea             t4, [rsp+wq+400*20+16]
    pshufhw         m7, m10, q0000
    pshufb         m10, [pw_256]  ; s0
    punpckhqdq      m7, m7        ; w0
    neg             wq
    mova           m13, [pd_34816]  ; (1 << 11) + (1 << 15)
    pxor            m6, m6
    mova           m14, [pw_1023]
    psllw           m7, 4
 DEFINE_ARGS dst, stride, left, lpf, _, h, edge, _, _, _, w
 %define lpfm        [rsp]
%else
    mov             r1, [rstk+stack_offset+28] ; params
    LEA             r6, $$
    add             wd, wd
    movu            m1, [r1]
    add           lpfm, wq
    lea             t1, [rsp+extra_stack+wq+20]
    add           dstq, wq
    lea             t3, [rsp+extra_stack+wq*2+400*12+16]
    mov           dstm, dstq
    lea             t4, [rsp+extra_stack+wq+400*20+16]
    mov            t3m, t3
    pshufhw         m7, m1, q0000
    mov            t4m, t4
    pshufb          m1, [base+pw_256] ; s0
    punpckhqdq      m7, m7            ; w0
    psllw           m7, 4
    neg             wq
    mova           m10, m1
    pxor            m6, m6
    mov            w1m, wd
    sub             wd, 4
    mov           lpfq, lpfm
    mov            w0m, wd
 %define strideq r5
%endif
    test         edgeb, 4 ; LR_HAVE_TOP
    jz .no_top
    call .h_top
    add           lpfq, stridemp
    movif32        t2m, t1
    mov             t2, t1
    call .top_fixup
    add             t1, 400*6
    call .h_top
    movif32    strideq, stridemp
    lea            r10, [lpfq+strideq*4]
    mov           lpfq, dstq
    add            r10, strideq
    mov           lpfm, r10 ; below
    movif32        t0m, t2
    mov             t0, t2
    dec             hd
    jz .height1
    or           edged, 16
    call .h
.main:
    add           lpfq, stridemp
    movif32         t4, t4m
    call .hv
    call .prep_n
    sub             hd, 2
    jl .extend_bottom
.main_loop:
    movif32       lpfq, hvsrcm
    add           lpfq, stridemp
%if ARCH_X86_64
    test            hb, hb
%else
    mov             r4, hd
    test            r4, r4
%endif
    jz .odd_height
    call .h
    add           lpfq, stridemp
    call .hv
    movif32       dstq, dstm
    call .n0
    call .n1
    sub             hd, 2
    movif32         t0, t0m
    jge .main_loop
    test         edgeb, 8 ; LR_HAVE_BOTTOM
    jz .extend_bottom
    mov           lpfq, lpfm
    call .h_top
    add           lpfq, stridemp
    call .hv_bottom
.end:
    movif32       dstq, dstm
    call .n0
    call .n1
.end2:
    RET
.height1:
    movif32         t4, t4m
    call .hv
    call .prep_n
    jmp .odd_height_end
.odd_height:
    call .hv
    movif32       dstq, dstm
    call .n0
    call .n1
.odd_height_end:
    call .v
    movif32       dstq, dstm
    call .n0
    jmp .end2
.extend_bottom:
    call .v
    jmp .end
.no_top:
    movif32    strideq, stridemp
    lea            r10, [lpfq+strideq*4]
    mov           lpfq, dstq
    lea            r10, [r10+strideq*2]
    mov           lpfm, r10
    call .h
    lea             t2, [t1+400*6]
    movif32        t2m, t2
    call .top_fixup
    dec             hd
    jz .no_top_height1
    or           edged, 16
    mov             t0, t1
    mov             t1, t2
    movif32        t0m, t0
    jmp .main
.no_top_height1:
    movif32         t3, t3m
    movif32         t4, t4m
    call .v
    call .prep_n
    jmp .odd_height_end
.extend_right:
    movd            m0, wd
    movd            m1, [lpfq-2]
    mova            m2, [base+pw_256]
    mova            m3, [base+pb_m14_m13]
    pshufb          m0, m6
    pshufb          m1, m2
    psubb           m2, m0
    psubb           m3, m0
    mova            m0, [base+pb_0to15]
    pcmpgtb         m2, m0
    pcmpgtb         m3, m0
    pand            m4, m2
    pand            m5, m3
    pandn           m2, m1
    pandn           m3, m1
    por             m4, m2
    por             m5, m3
    ret
%assign stack_offset stack_offset+4
%assign calloff 4
.h: ; horizontal boxsum
%if ARCH_X86_64
    lea             wq, [r4-4]
%else
 %define leftq r4
%endif
    test         edgeb, 1 ; LR_HAVE_LEFT
    jz .h_extend_left
    movif32      leftq, leftm
    movddup         m5, [leftq]
    movif32         wq, w0m
    mova            m4, [lpfq+wq+4]
    add         leftmp, 8
    palignr         m4, m5, 10
    jmp .h_main
.h_extend_left:
    movif32         wq, w0m
    mova            m4, [lpfq+wq+4]
    pshufb          m4, m12
    jmp .h_main
.h_top:
%if ARCH_X86_64
    lea             wq, [r4-4]
%endif
    test         edgeb, 1 ; LR_HAVE_LEFT
    jz .h_extend_left
    movif32         wq, w0m
.h_loop:
    movu            m4, [lpfq+wq- 2]
.h_main:
    movu            m5, [lpfq+wq+14]
    test         edgeb, 2 ; LR_HAVE_RIGHT
    jnz .h_have_right
    cmp             wd, -20
    jl .h_have_right
    call .extend_right
.h_have_right:
    palignr         m2, m5, m4, 2
    paddw           m0, m4, m2
    palignr         m3, m5, m4, 6
    paddw           m0, m3
    punpcklwd       m1, m2, m3
    pmaddwd         m1, m1
    punpckhwd       m2, m3
    pmaddwd         m2, m2
    palignr         m5, m4, 8
    paddw           m0, m5
    punpcklwd       m3, m4, m5
    pmaddwd         m3, m3
    paddd           m1, m3
    punpckhwd       m3, m4, m5
    pmaddwd         m3, m3
    shufps          m4, m5, q2121
    paddw           m0, m4             ; sum
    punpcklwd       m5, m4, m6
    pmaddwd         m5, m5
    punpckhwd       m4, m6
    pmaddwd         m4, m4
    paddd           m2, m3
    test         edgeb, 16             ; y > 0
    jz .h_loop_end
    paddw           m0, [t1+wq+400*0]
    paddd           m1, [t1+wq+400*2]
    paddd           m2, [t1+wq+400*4]
.h_loop_end:
    paddd           m1, m5             ; sumsq
    paddd           m2, m4
    mova [t1+wq+400*0], m0
    mova [t1+wq+400*2], m1
    mova [t1+wq+400*4], m2
    add             wq, 16
    jl .h_loop
    ret
.top_fixup:
%if ARCH_X86_64
    lea             wq, [r4-4]
%else
    mov             wd, w0m
%endif
.top_fixup_loop: ; the sums of the first row needs to be doubled
    mova            m0, [t1+wq+400*0]
    mova            m1, [t1+wq+400*2]
    mova            m2, [t1+wq+400*4]
    paddw           m0, m0
    paddd           m1, m1
    paddd           m2, m2
    mova [t2+wq+400*0], m0
    mova [t2+wq+400*2], m1
    mova [t2+wq+400*4], m2
    add             wq, 16
    jl .top_fixup_loop
    ret
ALIGN function_align
.hv: ; horizontal boxsum + vertical boxsum + ab
%if ARCH_X86_64
    lea             wq, [r4-4]
%else
    mov         hvsrcm, lpfq
%endif
    test         edgeb, 1 ; LR_HAVE_LEFT
    jz .hv_extend_left
    movif32      leftq, leftm
    movddup         m5, [leftq]
    movif32         wq, w0m
    mova            m4, [lpfq+wq+4]
    add         leftmp, 8
    palignr         m4, m5, 10
    jmp .hv_main
.hv_extend_left:
    movif32         wq, w0m
    mova            m4, [lpfq+wq+4]
    pshufb          m4, m12
    jmp .hv_main
.hv_bottom:
%if ARCH_X86_64
    lea             wq, [r4-4]
%else
    mov         hvsrcm, lpfq
%endif
    test         edgeb, 1 ; LR_HAVE_LEFT
    jz .hv_extend_left
    movif32         wq, w0m
%if ARCH_X86_32
    jmp .hv_loop_start
%endif
.hv_loop:
    movif32       lpfq, hvsrcm
.hv_loop_start:
    movu            m4, [lpfq+wq- 2]
.hv_main:
    movu            m5, [lpfq+wq+14]
    test         edgeb, 2 ; LR_HAVE_RIGHT
    jnz .hv_have_right
    cmp             wd, -20
    jl .hv_have_right
    call .extend_right
.hv_have_right:
    movif32         t3, hd
    palignr         m3, m5, m4, 2
    paddw           m0, m4, m3
    palignr         m1, m5, m4, 6
    paddw           m0, m1
    punpcklwd       m2, m3, m1
    pmaddwd         m2, m2
    punpckhwd       m3, m1
    pmaddwd         m3, m3
    palignr         m5, m4, 8
    paddw           m0, m5
    punpcklwd       m1, m4, m5
    pmaddwd         m1, m1
    paddd           m2, m1
    punpckhwd       m1, m4, m5
    pmaddwd         m1, m1
    shufps          m4, m5, q2121
    paddw           m0, m4            ; h sum
    punpcklwd       m5, m4, m6
    pmaddwd         m5, m5
    punpckhwd       m4, m6
    pmaddwd         m4, m4
    paddd           m3, m1
    paddd           m2, m5            ; h sumsq
    paddd           m3, m4
    paddw           m1, m0, [t1+wq+400*0]
    paddd           m4, m2, [t1+wq+400*2]
    paddd           m5, m3, [t1+wq+400*4]
%if ARCH_X86_64
    test            hd, hd
%else
    test            t3, t3
%endif
    jz .hv_last_row
.hv_main2:
    paddw           m1, [t2+wq+400*0] ; hv sum
    paddd           m4, [t2+wq+400*2] ; hv sumsq
    paddd           m5, [t2+wq+400*4]
    mova [t0+wq+400*0], m0
    mova [t0+wq+400*2], m2
    mova [t0+wq+400*4], m3
    psrlw           m3, m1, 1
    paddd           m4, m8
    pavgw           m3, m6             ; (b + 2) >> 2
    paddd           m5, m8
    pand            m4, m9             ; ((a + 8) >> 4) << 4
    pand            m5, m9
    psrld           m2, m4, 4
    psrld           m0, m5, 4
    paddd           m2, m4
    psrld           m4, 1
    paddd           m0, m5
    psrld           m5, 1
    paddd           m4, m2             ; a * 25
    paddd           m5, m0
    punpcklwd       m2, m3, m6
    punpckhwd       m3, m6
    pmaddwd         m2, m2             ; b * b
    pmaddwd         m3, m3
    punpcklwd       m0, m1, m6         ; b
    punpckhwd       m1, m6
    MAXSD           m4, m2, m6
    MAXSD           m5, m3, m6, 1
    psubd           m4, m2             ; p
    psubd           m5, m3
    MULLD           m4, m10, m2        ; p * s
    MULLD           m5, m10, m2
    pmaddwd         m0, m11            ; b * 164
    pmaddwd         m1, m11
    paddusw         m4, m11
    paddusw         m5, m11
    psrld           m4, 20             ; min(z, 255)
    movif32         t3, t3m
    psrld           m5, 20
    GATHER_X_BY_X   m3, m4, m5, t2, t2m
    punpcklwd       m4, m3, m3
    punpckhwd       m5, m3, m3
    MULLD           m0, m4, m2
    MULLD           m1, m5, m2
    paddd           m0, m13            ; x * b * 164 + (1 << 11) + (1 << 15)
    paddd           m1, m13
    mova     [t4+wq+4], m3
    psrld           m0, 12             ; b
    psrld           m1, 12
    mova  [t3+wq*2+ 8], m0
    mova  [t3+wq*2+24], m1
    add             wq, 16
    jl .hv_loop
    mov             t2, t1
    mov             t1, t0
    mov             t0, t2
    movif32        t2m, t2
    movif32        t0m, t0
    ret
.hv_last_row: ; esoteric edge case for odd heights
    mova [t1+wq+400*0], m1
    paddw           m1, m0
    mova [t1+wq+400*2], m4
    paddd           m4, m2
    mova [t1+wq+400*4], m5
    paddd           m5, m3
    jmp .hv_main2
.v: ; vertical boxsum + ab
%if ARCH_X86_64
    lea             wq, [r4-4]
%else
    mov             wd, w0m
%endif
.v_loop:
    mova            m0, [t1+wq+400*0]
    mova            m2, [t1+wq+400*2]
    mova            m3, [t1+wq+400*4]
    paddw           m1, m0, [t2+wq+400*0]
    paddd           m4, m2, [t2+wq+400*2]
    paddd           m5, m3, [t2+wq+400*4]
    paddw           m0, m0
    paddd           m2, m2
    paddd           m3, m3
    paddw           m1, m0             ; hv sum
    paddd           m4, m2             ; hv sumsq
    paddd           m5, m3
    psrlw           m3, m1, 1
    paddd           m4, m8
    pavgw           m3, m6             ; (b + 2) >> 2
    paddd           m5, m8
    pand            m4, m9             ; ((a + 8) >> 4) << 4
    pand            m5, m9
    psrld           m2, m4, 4
    psrld           m0, m5, 4
    paddd           m2, m4
    psrld           m4, 1
    paddd           m0, m5
    psrld           m5, 1
    paddd           m4, m2             ; a * 25
    paddd           m5, m0
    punpcklwd       m2, m3, m6
    punpckhwd       m3, m6
    pmaddwd         m2, m2             ; b * b
    pmaddwd         m3, m3
    punpcklwd       m0, m1, m6         ; b
    punpckhwd       m1, m6
    MAXSD           m4, m2, m6
    MAXSD           m5, m3, m6, 1
    psubd           m4, m2             ; p
    psubd           m5, m3
    MULLD           m4, m10, m2        ; p * s
    MULLD           m5, m10, m2
    pmaddwd         m0, m11            ; b * 164
    pmaddwd         m1, m11
    paddusw         m4, m11
    paddusw         m5, m11
    psrld           m4, 20             ; min(z, 255)
    psrld           m5, 20
    GATHER_X_BY_X   m3, m4, m5, t2, t2m
    punpcklwd       m4, m3, m3
    punpckhwd       m5, m3, m3
    MULLD           m0, m4, m2
    MULLD           m1, m5, m2
    paddd           m0, m13            ; x * b * 164 + (1 << 11) + (1 << 15)
    paddd           m1, m13
    mova     [t4+wq+4], m3
    psrld           m0, 12             ; b
    psrld           m1, 12
    mova  [t3+wq*2+ 8], m0
    mova  [t3+wq*2+24], m1
    add             wq, 16
    jl .v_loop
    ret
.prep_n: ; initial neighbor setup
    movif64         wq, r4
    movif32         wd, w1m
.prep_n_loop:
    movu            m0, [t4+wq*1+ 2]
    movu            m3, [t4+wq*1+ 4]
    movu            m1, [t3+wq*2+ 4]
    movu            m4, [t3+wq*2+ 8]
    movu            m2, [t3+wq*2+20]
    movu            m5, [t3+wq*2+24]
    paddw           m3, m0
    paddd           m4, m1
    paddd           m5, m2
    paddw           m3, [t4+wq*1+ 0]
    paddd           m4, [t3+wq*2+ 0]
    paddd           m5, [t3+wq*2+16]
    paddw           m0, m3
    psllw           m3, 2
    paddd           m1, m4
    pslld           m4, 2
    paddd           m2, m5
    pslld           m5, 2
    paddw           m0, m3             ; a 565
    paddd           m1, m4             ; b 565
    paddd           m2, m5
    mova [t4+wq*1+400*2+ 0], m0
    mova [t3+wq*2+400*4+ 0], m1
    mova [t3+wq*2+400*4+16], m2
    add             wq, 16
    jl .prep_n_loop
    ret
ALIGN function_align
.n0: ; neighbor + output (even rows)
    movif64         wq, r4
    movif32         wd, w1m
.n0_loop:
    movu            m0, [t4+wq*1+ 2]
    movu            m3, [t4+wq*1+ 4]
    movu            m1, [t3+wq*2+ 4]
    movu            m4, [t3+wq*2+ 8]
    movu            m2, [t3+wq*2+20]
    movu            m5, [t3+wq*2+24]
    paddw           m3, m0
    paddd           m4, m1
    paddd           m5, m2
    paddw           m3, [t4+wq*1+ 0]
    paddd           m4, [t3+wq*2+ 0]
    paddd           m5, [t3+wq*2+16]
    paddw           m0, m3
    psllw           m3, 2
    paddd           m1, m4
    pslld           m4, 2
    paddd           m2, m5
    pslld           m5, 2
    paddw           m0, m3             ; a 565
    paddd           m1, m4             ; b 565
    paddd           m2, m5
    paddw           m3, m0, [t4+wq*1+400*2+ 0]
    paddd           m4, m1, [t3+wq*2+400*4+ 0]
    paddd           m5, m2, [t3+wq*2+400*4+16]
    mova [t4+wq*1+400*2+ 0], m0
    mova [t3+wq*2+400*4+ 0], m1
    mova [t3+wq*2+400*4+16], m2
    mova            m0, [dstq+wq]
    punpcklwd       m1, m0, m6          ; src
    punpcklwd       m2, m3, m6          ; a
    pmaddwd         m2, m1              ; a * src
    punpckhwd       m1, m0, m6
    punpckhwd       m3, m6
    pmaddwd         m3, m1
    psubd           m4, m2              ; b - a * src + (1 << 8)
    psubd           m5, m3
    psrad           m4, 9
    psrad           m5, 9
    packssdw        m4, m5
    pmulhrsw        m4, m7
    paddw           m0, m4
    pmaxsw          m0, m6
    pminsw          m0, m14
    mova     [dstq+wq], m0
    add             wq, 16
    jl .n0_loop
    add           dstq, stridemp
    ret
ALIGN function_align
.n1: ; neighbor + output (odd rows)
    movif64         wq, r4
    movif32         wd, w1m
.n1_loop:
    mova            m0, [dstq+wq]
    mova            m3, [t4+wq*1+400*2+ 0]
    mova            m4, [t3+wq*2+400*4+ 0]
    mova            m5, [t3+wq*2+400*4+16]
    punpcklwd       m1, m0, m6          ; src
    punpcklwd       m2, m3, m6          ; a
    pmaddwd         m2, m1
    punpckhwd       m1, m0, m6
    punpckhwd       m3, m6
    pmaddwd         m3, m1
    psubd           m4, m2              ; b - a * src + (1 << 7)
    psubd           m5, m3
    psrad           m4, 8
    psrad           m5, 8
    packssdw        m4, m5
    pmulhrsw        m4, m7
    paddw           m0, m4
    pmaxsw          m0, m6
    pminsw          m0, m14
    mova     [dstq+wq], m0
    add             wq, 16
    jl .n1_loop
    add           dstq, stridemp
    movif32       dstm, dstq
    ret

%if ARCH_X86_32
 %if STACK_ALIGNMENT < 16
  %assign extra_stack 4*16
 %else
  %assign extra_stack 2*16
 %endif
cglobal sgr_filter_3x3_16bpc, 1, 7, 8, -400*42-16-extra_stack, \
                              dst, stride, left, lpf, w
 %if STACK_ALIGNMENT < 16
  %define dstm         dword [esp+calloff+16*2+4*0]
  %define stridemp     dword [esp+calloff+16*2+4*1]
  %define leftm        dword [esp+calloff+16*2+4*2]
  %define lpfm         dword [esp+calloff+16*2+4*3]
  %define w0m          dword [esp+calloff+16*2+4*4]
  %define hd           dword [esp+calloff+16*2+4*5]
  %define edgeb         byte [esp+calloff+16*2+4*6]
  %define edged        dword [esp+calloff+16*2+4*6]
  %define leftmp leftm
 %else
  %define w0m wm
  %define hd dword r5m
  %define edgeb  byte r7m
  %define edged dword r7m
 %endif
 %define hvsrcm dword [esp+calloff+4*0]
 %define w1m    dword [esp+calloff+4*1]
 %define t3m    dword [esp+calloff+4*2]
 %define t4m    dword [esp+calloff+4*3]
 %define  m8 [base+pd_8]
 %define  m9 [esp+calloff+16*1]
 %define m10 [base+pd_0xf00801c7]
 %define m11 [base+pd_34816]
 %define m12 [base+sgr_lshuf3]
 %define m13 [base+pw_1023]
 %define m14 m6
 %define base r6-$$
 %assign calloff 0
 %if STACK_ALIGNMENT < 16
    mov        strideq, [rstk+stack_offset+ 8]
    mov          leftq, [rstk+stack_offset+12]
    mov           lpfq, [rstk+stack_offset+16]
    mov             wd, [rstk+stack_offset+20]
    mov           dstm, dstq
    mov       stridemp, strideq
    mov          leftm, leftq
    mov             r1, [rstk+stack_offset+24]
    mov             r2, [rstk+stack_offset+32]
    mov           lpfm, lpfq
    mov             hd, r1
    mov          edged, r2
 %endif
%else
cglobal sgr_filter_3x3_16bpc, 4, 15, 15, -400*42-8, dst, stride, left, lpf, \
                                                    w, h, edge, params
%endif
%if ARCH_X86_64 || STACK_ALIGNMENT >= 16
    movifnidn       wd, wm
%endif
%if ARCH_X86_64
    mov        paramsq, r6mp
    lea            r13, [sgr_x_by_x-0xf03]
    movifnidn       hd, hm
    add             wd, wd
    mov          edged, r7m
    movq            m9, [paramsq+4]
    add           lpfq, wq
    lea             t1, [rsp+wq+12]
    mova            m8, [pd_8]
    add           dstq, wq
    lea             t3, [rsp+wq*2+400*12+8]
    mova           m10, [pd_0xf00801c7]
    lea             t4, [rsp+wq+400*32+8]
    mova           m11, [pd_34816]
    pshuflw         m7, m9, q3333
    pshufb          m9, [pw_256]  ; s1
    punpcklqdq      m7, m7        ; w1
    neg             wq
    pxor            m6, m6
    mova           m13, [pw_1023]
    psllw           m7, 4
    mova           m12, [sgr_lshuf3]
 DEFINE_ARGS dst, stride, left, lpf, _, h, edge, _, _, _, w
 %define lpfm [rsp]
%else
    mov             r1, [rstk+stack_offset+28] ; params
    LEA             r6, $$
    add             wd, wd
    movq            m1, [r1+4]
    add           lpfm, wq
    lea             t1, [rsp+extra_stack+wq+20]
    add           dstq, wq
    lea             t3, [rsp+extra_stack+wq*2+400*12+16]
    mov           dstm, dstq
    lea             t4, [rsp+extra_stack+wq+400*32+16]
    mov            t3m, t3
    pshuflw         m7, m1, q3333
    mov            t4m, t4
    pshufb          m1, [base+pw_256] ; s1
    punpcklqdq      m7, m7            ; w1
    psllw           m7, 4
    neg             wq
    mova            m9, m1
    pxor            m6, m6
    mov            w1m, wd
    sub             wd, 4
    mov           lpfq, lpfm
    mov            w0m, wd
 %define strideq r5
%endif
    test         edgeb, 4 ; LR_HAVE_TOP
    jz .no_top
    call .h_top
    add           lpfq, stridemp
    mov             t2, t1
    add             t1, 400*6
    call .h_top
    movif32    strideq, stridemp
    lea            r10, [lpfq+strideq*4]
    mov           lpfq, dstq
    add            r10, strideq
    mov           lpfm, r10 ; below
    movif32         t4, t4m
    call .hv0
.main:
    dec             hd
    jz .height1
    movif32       lpfq, hvsrcm
    add           lpfq, stridemp
    call .hv1
    call .prep_n
    sub             hd, 2
    jl .extend_bottom
.main_loop:
    movif32       lpfq, hvsrcm
    add           lpfq, stridemp
    call .hv0
%if ARCH_X86_64
    test            hb, hb
%else
    mov             r4, hd
    test            r4, r4
%endif
    jz .odd_height
    movif32       lpfq, hvsrcm
    add           lpfq, stridemp
    call .hv1
    call .n0
    call .n1
    sub             hd, 2
    jge .main_loop
    test         edgeb, 8 ; LR_HAVE_BOTTOM
    jz .extend_bottom
    mov           lpfq, lpfm
    call .hv0_bottom
    movif32       lpfq, hvsrcm
    add           lpfq, stridemp
    call .hv1_bottom
.end:
    call .n0
    call .n1
.end2:
    RET
.height1:
    call .v1
    call .prep_n
    jmp .odd_height_end
.odd_height:
    call .v1
    call .n0
    call .n1
.odd_height_end:
    call .v0
    call .v1
    call .n0
    jmp .end2
.extend_bottom:
    call .v0
    call .v1
    jmp .end
.no_top:
    movif32    strideq, stridemp
    lea            r10, [lpfq+strideq*4]
    mov           lpfq, dstq
    lea            r10, [r10+strideq*2]
    mov           lpfm, r10
    call .h
%if ARCH_X86_64
    lea             wq, [r4-4]
%else
    mov             wq, w0m
    mov         hvsrcm, lpfq
%endif
    lea             t2, [t1+400*6]
.top_fixup_loop:
    mova            m0, [t1+wq+400*0]
    mova            m1, [t1+wq+400*2]
    mova            m2, [t1+wq+400*4]
    mova [t2+wq+400*0], m0
    mova [t2+wq+400*2], m1
    mova [t2+wq+400*4], m2
    add             wq, 16
    jl .top_fixup_loop
    movif32         t3, t3m
    movif32         t4, t4m
    call .v0
    jmp .main
.extend_right:
    movd            m1, wd
    movd            m5, [lpfq-2]
    mova            m2, [base+pw_256]
    mova            m3, [base+pb_0to15]
    pshufb          m1, m6
    pshufb          m5, m2
    psubb           m2, m1
    pcmpgtb         m2, m3
    pand            m4, m2
    pandn           m2, m5
    por             m4, m2
    ret
%assign stack_offset stack_offset+4
%assign calloff 4
.h: ; horizontal boxsum
%if ARCH_X86_64
    lea             wq, [r4-4]
%else
 %define leftq r4
%endif
    test         edgeb, 1 ; LR_HAVE_LEFT
    jz .h_extend_left
    movif32      leftq, leftm
    movddup         m5, [leftq]
    movif32         wq, w0m
    mova            m4, [lpfq+wq+4]
    add         leftmp, 8
    palignr         m4, m5, 12
    jmp .h_main
.h_extend_left:
    movif32         wq, w0m
    mova            m4, [lpfq+wq+4]
    pshufb          m4, m12
    jmp .h_main
.h_top:
%if ARCH_X86_64
    lea             wq, [r4-4]
%endif
    test         edgeb, 1 ; LR_HAVE_LEFT
    jz .h_extend_left
    movif32         wq, w0m
.h_loop:
    movu            m4, [lpfq+wq+ 0]
.h_main:
    movu            m5, [lpfq+wq+16]
    test         edgeb, 2 ; LR_HAVE_RIGHT
    jnz .h_have_right
    cmp             wd, -18
    jl .h_have_right
    call .extend_right
.h_have_right:
    palignr         m0, m5, m4, 2
    paddw           m1, m4, m0
    punpcklwd       m2, m4, m0
    pmaddwd         m2, m2
    punpckhwd       m3, m4, m0
    pmaddwd         m3, m3
    palignr         m5, m4, 4
    paddw           m1, m5             ; sum
    punpcklwd       m4, m5, m6
    pmaddwd         m4, m4
    punpckhwd       m5, m6
    pmaddwd         m5, m5
    paddd           m2, m4             ; sumsq
    paddd           m3, m5
    mova [t1+wq+400*0], m1
    mova [t1+wq+400*2], m2
    mova [t1+wq+400*4], m3
    add             wq, 16
    jl .h_loop
    ret
ALIGN function_align
.hv0: ; horizontal boxsum + vertical boxsum + ab (even rows)
%if ARCH_X86_64
    lea             wq, [r4-4]
%else
    mov         hvsrcm, lpfq
%endif
    test         edgeb, 1 ; LR_HAVE_LEFT
    jz .hv0_extend_left
    movif32      leftq, leftm
    movddup         m5, [leftq]
    movif32         wq, w0m
    mova            m4, [lpfq+wq+4]
    add         leftmp, 8
    palignr         m4, m5, 12
    jmp .hv0_main
.hv0_extend_left:
    movif32         wq, w0m
    mova            m4, [lpfq+wq+4]
    pshufb          m4, m12
    jmp .hv0_main
.hv0_bottom:
%if ARCH_X86_64
    lea             wq, [r4-4]
%else
    mov         hvsrcm, lpfq
%endif
    test         edgeb, 1 ; LR_HAVE_LEFT
    jz .hv0_extend_left
    movif32         wq, w0m
%if ARCH_X86_32
    jmp .hv0_loop_start
%endif
.hv0_loop:
    movif32       lpfq, hvsrcm
.hv0_loop_start:
    movu            m4, [lpfq+wq+ 0]
.hv0_main:
    movu            m5, [lpfq+wq+16]
    test         edgeb, 2 ; LR_HAVE_RIGHT
    jnz .hv0_have_right
    cmp             wd, -18
    jl .hv0_have_right
    call .extend_right
.hv0_have_right:
    palignr         m0, m5, m4, 2
    paddw           m1, m4, m0
    punpcklwd       m2, m4, m0
    pmaddwd         m2, m2
    punpckhwd       m3, m4, m0
    pmaddwd         m3, m3
    palignr         m5, m4, 4
    paddw           m1, m5             ; sum
    punpcklwd       m4, m5, m6
    pmaddwd         m4, m4
    punpckhwd       m5, m6
    pmaddwd         m5, m5
    paddd           m2, m4             ; sumsq
    paddd           m3, m5
    paddw           m0, m1, [t1+wq+400*0]
    paddd           m4, m2, [t1+wq+400*2]
    paddd           m5, m3, [t1+wq+400*4]
    mova [t1+wq+400*0], m1
    mova [t1+wq+400*2], m2
    mova [t1+wq+400*4], m3
    paddw           m1, m0, [t2+wq+400*0]
    paddd           m2, m4, [t2+wq+400*2]
    paddd           m3, m5, [t2+wq+400*4]
    mova [t2+wq+400*0], m0
    mova [t2+wq+400*2], m4
    mova [t2+wq+400*4], m5
    paddd           m2, m8
    paddd           m3, m8
    psrld           m2, 4              ; (a + 8) >> 4
    psrld           m3, 4
    pslld           m4, m2, 3
    pslld           m5, m3, 3
    paddd           m4, m2             ; ((a + 8) >> 4) * 9
    paddd           m5, m3
    psrlw           m3, m1, 1
    pavgw           m3, m6             ; (b + 2) >> 2
    punpcklwd       m2, m3, m6
    pmaddwd         m2, m2
    punpckhwd       m3, m6
    pmaddwd         m3, m3
    punpcklwd       m0, m1, m6         ; b
    punpckhwd       m1, m6
    MAXSD           m4, m2, m14
    MAXSD           m5, m3, m14
    psubd           m4, m2             ; p
    psubd           m5, m3
    MULLD           m4, m9, m14        ; p * s
    MULLD           m5, m9, m14
    pmaddwd         m0, m10            ; b * 455
    pmaddwd         m1, m10
    paddusw         m4, m10
    paddusw         m5, m10
    psrld           m4, 20             ; min(z, 255)
    movif32         t3, t3m
    psrld           m5, 20
    GATHER_X_BY_X   m3, m4, m5, r0, dstm
    punpcklwd       m4, m3, m3
    punpckhwd       m5, m3, m3
    MULLD           m0, m4, m14
    MULLD           m1, m5, m14
%if ARCH_X86_32
    pxor            m6, m6
%endif
    paddd           m0, m11            ; x * b * 455 + (1 << 11) + (1 << 15)
    paddd           m1, m11
    mova     [t4+wq+4], m3
    psrld           m0, 12
    psrld           m1, 12
    mova  [t3+wq*2+ 8], m0
    mova  [t3+wq*2+24], m1
    add             wq, 16
    jl .hv0_loop
    ret
ALIGN function_align
.hv1: ; horizontal boxsums + vertical boxsums + ab (odd rows)
%if ARCH_X86_64
    lea             wq, [r4-4]
%else
    mov         hvsrcm, lpfq
%endif
    test         edgeb, 1 ; LR_HAVE_LEFT
    jz .hv1_extend_left
    movif32      leftq, leftm
    movddup         m5, [leftq]
    movif32         wq, w0m
    mova            m4, [lpfq+wq+4]
    add         leftmp, 8
    palignr         m4, m5, 12
    jmp .hv1_main
.hv1_extend_left:
    movif32         wq, w0m
    mova            m4, [lpfq+wq+4]
    pshufb          m4, m12
    jmp .hv1_main
.hv1_bottom:
%if ARCH_X86_64
    lea             wq, [r4-4]
%else
    mov         hvsrcm, lpfq
%endif
    test         edgeb, 1 ; LR_HAVE_LEFT
    jz .hv1_extend_left
    movif32         wq, w0m
%if ARCH_X86_32
    jmp .hv1_loop_start
%endif
.hv1_loop:
    movif32       lpfq, hvsrcm
.hv1_loop_start:
    movu            m4, [lpfq+wq+ 0]
.hv1_main:
    movu            m5, [lpfq+wq+16]
    test         edgeb, 2 ; LR_HAVE_RIGHT
    jnz .hv1_have_right
    cmp             wd, -18
    jl .hv1_have_right
    call .extend_right
.hv1_have_right:
    palignr         m1, m5, m4, 2
    paddw           m0, m4, m1
    punpcklwd       m2, m4, m1
    pmaddwd         m2, m2
    punpckhwd       m3, m4, m1
    pmaddwd         m3, m3
    palignr         m5, m4, 4
    paddw           m0, m5             ; h sum
    punpcklwd       m1, m5, m6
    pmaddwd         m1, m1
    punpckhwd       m5, m6
    pmaddwd         m5, m5
    paddd           m2, m1             ; h sumsq
    paddd           m3, m5
    paddw           m1, m0, [t2+wq+400*0]
    paddd           m4, m2, [t2+wq+400*2]
    paddd           m5, m3, [t2+wq+400*4]
    mova [t2+wq+400*0], m0
    mova [t2+wq+400*2], m2
    mova [t2+wq+400*4], m3
    paddd           m4, m8
    paddd           m5, m8
    psrld           m4, 4              ; (a + 8) >> 4
    psrld           m5, 4
    pslld           m2, m4, 3
    pslld           m3, m5, 3
    paddd           m4, m2             ; ((a + 8) >> 4) * 9
    paddd           m5, m3
    psrlw           m3, m1, 1
    pavgw           m3, m6             ; (b + 2) >> 2
    punpcklwd       m2, m3, m6
    pmaddwd         m2, m2
    punpckhwd       m3, m6
    pmaddwd         m3, m3
    punpcklwd       m0, m1, m6         ; b
    punpckhwd       m1, m6
    MAXSD           m4, m2, m14
    MAXSD           m5, m3, m14
    psubd           m4, m2             ; p
    psubd           m5, m3
    MULLD           m4, m9, m14        ; p * s
    MULLD           m5, m9, m14
    pmaddwd         m0, m10            ; b * 455
    pmaddwd         m1, m10
    paddusw         m4, m10
    paddusw         m5, m10
    psrld           m4, 20             ; min(z, 255)
    movif32         t3, t3m
    psrld           m5, 20
    GATHER_X_BY_X   m3, m4, m5, r0, dstm
    punpcklwd       m4, m3, m3
    punpckhwd       m5, m3, m3
    MULLD           m0, m4, m14
    MULLD           m1, m5, m14
%if ARCH_X86_32
    pxor            m6, m6
%endif
    paddd           m0, m11            ; x * b * 455 + (1 << 11) + (1 << 15)
    paddd           m1, m11
    mova [t4+wq*1+400*2 +4], m3
    psrld           m0, 12
    psrld           m1, 12
    mova [t3+wq*2+400*4+ 8], m0
    mova [t3+wq*2+400*4+24], m1
    add             wq, 16
    jl .hv1_loop
    mov            r10, t2
    mov             t2, t1
    mov             t1, r10
    ret
.v0: ; vertical boxsums + ab (even rows)
%if ARCH_X86_64
    lea             wq, [r4-4]
%else
    mov             wd, w0m
%endif
.v0_loop:
    mova            m0, [t1+wq+400*0]
    mova            m4, [t1+wq+400*2]
    mova            m5, [t1+wq+400*4]
    paddw           m0, m0
    paddd           m4, m4
    paddd           m5, m5
    paddw           m1, m0, [t2+wq+400*0]
    paddd           m2, m4, [t2+wq+400*2]
    paddd           m3, m5, [t2+wq+400*4]
    mova [t2+wq+400*0], m0
    mova [t2+wq+400*2], m4
    mova [t2+wq+400*4], m5
    paddd           m2, m8
    paddd           m3, m8
    psrld           m2, 4              ; (a + 8) >> 4
    psrld           m3, 4
    pslld           m4, m2, 3
    pslld           m5, m3, 3
    paddd           m4, m2             ; ((a + 8) >> 4) * 9
    paddd           m5, m3
    psrlw           m3, m1, 1
    pavgw           m3, m6             ; (b + 2) >> 2
    punpcklwd       m2, m3, m6
    pmaddwd         m2, m2
    punpckhwd       m3, m6
    pmaddwd         m3, m3
    punpcklwd       m0, m1, m6         ; b
    punpckhwd       m1, m6
    MAXSD           m4, m2, m14
    MAXSD           m5, m3, m14
    psubd           m4, m2             ; p
    psubd           m5, m3
    MULLD           m4, m9, m14        ; p * s
    MULLD           m5, m9, m14
    pmaddwd         m0, m10            ; b * 455
    pmaddwd         m1, m10
    paddusw         m4, m10
    paddusw         m5, m10
    psrld           m4, 20             ; min(z, 255)
    psrld           m5, 20
    GATHER_X_BY_X   m3, m4, m5, r0, dstm
    punpcklwd       m4, m3, m3
    punpckhwd       m5, m3, m3
    MULLD           m0, m4, m14
    MULLD           m1, m5, m14
%if ARCH_X86_32
    pxor            m6, m6
%endif
    paddd           m0, m11            ; x * b * 455 + (1 << 11) + (1 << 15)
    paddd           m1, m11
    mova [t4+wq*1+400*0+ 4], m3
    psrld           m0, 12
    psrld           m1, 12
    mova [t3+wq*2+400*0+ 8], m0
    mova [t3+wq*2+400*0+24], m1
    add             wq, 16
    jl .v0_loop
    ret
.v1: ; vertical boxsums + ab (odd rows)
%if ARCH_X86_64
    lea             wq, [r4-4]
%else
    mov             wd, w0m
%endif
.v1_loop:
    mova            m0, [t1+wq+400*0]
    mova            m4, [t1+wq+400*2]
    mova            m5, [t1+wq+400*4]
    paddw           m1, m0, [t2+wq+400*0]
    paddd           m2, m4, [t2+wq+400*2]
    paddd           m3, m5, [t2+wq+400*4]
    mova [t2+wq+400*0], m0
    mova [t2+wq+400*2], m4
    mova [t2+wq+400*4], m5
    paddd           m2, m8
    paddd           m3, m8
    psrld           m2, 4              ; (a + 8) >> 4
    psrld           m3, 4
    pslld           m4, m2, 3
    pslld           m5, m3, 3
    paddd           m4, m2             ; ((a + 8) >> 4) * 9
    paddd           m5, m3
    psrlw           m3, m1, 1
    pavgw           m3, m6             ; (b + 2) >> 2
    punpcklwd       m2, m3, m6
    pmaddwd         m2, m2
    punpckhwd       m3, m6
    pmaddwd         m3, m3
    punpcklwd       m0, m1, m6         ; b
    punpckhwd       m1, m6
    MAXSD           m4, m2, m14
    MAXSD           m5, m3, m14
    psubd           m4, m2             ; p
    psubd           m5, m3
    MULLD           m4, m9, m14        ; p * s
    MULLD           m5, m9, m14
    pmaddwd         m0, m10            ; b * 455
    pmaddwd         m1, m10
    paddusw         m4, m10
    paddusw         m5, m10
    psrld           m4, 20             ; min(z, 255)
    psrld           m5, 20
    GATHER_X_BY_X   m3, m4, m5, r0, dstm
    punpcklwd       m4, m3, m3
    punpckhwd       m5, m3, m3
    MULLD           m0, m4, m14
    MULLD           m1, m5, m14
%if ARCH_X86_32
    pxor            m6, m6
%endif
    paddd           m0, m11            ; x * b * 455 + (1 << 11) + (1 << 15)
    paddd           m1, m11
    mova [t4+wq*1+400*2+ 4], m3
    psrld           m0, 12
    psrld           m1, 12
    mova [t3+wq*2+400*4+ 8], m0
    mova [t3+wq*2+400*4+24], m1
    add             wq, 16
    jl .v1_loop
    mov            r10, t2
    mov             t2, t1
    mov             t1, r10
    ret
.prep_n: ; initial neighbor setup
    movif64         wq, r4
    movif32         wd, w1m
.prep_n_loop:
    movu            m0, [t4+wq*1+400*0+ 4]
    movu            m1, [t3+wq*2+400*0+ 8]
    movu            m2, [t3+wq*2+400*0+24]
    movu            m3, [t4+wq*1+400*0+ 2]
    movu            m4, [t3+wq*2+400*0+ 4]
    movu            m5, [t3+wq*2+400*0+20]
    paddw           m0, [t4+wq*1+400*0+ 0]
    paddd           m1, [t3+wq*2+400*0+ 0]
    paddd           m2, [t3+wq*2+400*0+16]
    paddw           m3, m0
    paddd           m4, m1
    paddd           m5, m2
    psllw           m3, 2                ; a[-1] 444
    pslld           m4, 2                ; b[-1] 444
    pslld           m5, 2
    psubw           m3, m0               ; a[-1] 343
    psubd           m4, m1               ; b[-1] 343
    psubd           m5, m2
    mova [t4+wq*1+400*4], m3
    mova [t3+wq*2+400*8+ 0], m4
    mova [t3+wq*2+400*8+16], m5
    movu            m0, [t4+wq*1+400*2+ 4]
    movu            m1, [t3+wq*2+400*4+ 8]
    movu            m2, [t3+wq*2+400*4+24]
    movu            m3, [t4+wq*1+400*2+ 2]
    movu            m4, [t3+wq*2+400*4+ 4]
    movu            m5, [t3+wq*2+400*4+20]
    paddw           m0, [t4+wq*1+400*2+ 0]
    paddd           m1, [t3+wq*2+400*4+ 0]
    paddd           m2, [t3+wq*2+400*4+16]
    paddw           m3, m0
    paddd           m4, m1
    paddd           m5, m2
    psllw           m3, 2                 ; a[ 0] 444
    pslld           m4, 2                 ; b[ 0] 444
    pslld           m5, 2
    mova [t4+wq*1+400* 6], m3
    mova [t3+wq*2+400*12+ 0], m4
    mova [t3+wq*2+400*12+16], m5
    psubw           m3, m0                ; a[ 0] 343
    psubd           m4, m1                ; b[ 0] 343
    psubd           m5, m2
    mova [t4+wq*1+400* 8], m3
    mova [t3+wq*2+400*16+ 0], m4
    mova [t3+wq*2+400*16+16], m5
    add             wq, 16
    jl .prep_n_loop
    ret
ALIGN function_align
.n0: ; neighbor + output (even rows)
    movif64         wq, r4
    movif32         wd, w1m
.n0_loop:
    movu            m3, [t4+wq*1+400*0+4]
    movu            m1, [t4+wq*1+400*0+2]
    paddw           m3, [t4+wq*1+400*0+0]
    paddw           m1, m3
    psllw           m1, 2                ; a[ 1] 444
    psubw           m2, m1, m3           ; a[ 1] 343
    paddw           m3, m2, [t4+wq*1+400*4]
    paddw           m3, [t4+wq*1+400*6]
    mova [t4+wq*1+400*4], m2
    mova [t4+wq*1+400*6], m1
    movu            m4, [t3+wq*2+400*0+8]
    movu            m1, [t3+wq*2+400*0+4]
    paddd           m4, [t3+wq*2+400*0+0]
    paddd           m1, m4
    pslld           m1, 2                ; b[ 1] 444
    psubd           m2, m1, m4           ; b[ 1] 343
    paddd           m4, m2, [t3+wq*2+400* 8+ 0]
    paddd           m4, [t3+wq*2+400*12+ 0]
    mova [t3+wq*2+400* 8+ 0], m2
    mova [t3+wq*2+400*12+ 0], m1
    movu            m5, [t3+wq*2+400*0+24]
    movu            m1, [t3+wq*2+400*0+20]
    paddd           m5, [t3+wq*2+400*0+16]
    paddd           m1, m5
    pslld           m1, 2
    psubd           m2, m1, m5
    paddd           m5, m2, [t3+wq*2+400* 8+16]
    paddd           m5, [t3+wq*2+400*12+16]
    mova [t3+wq*2+400* 8+16], m2
    mova [t3+wq*2+400*12+16], m1
    mova            m0, [dstq+wq]
    punpcklwd       m1, m0, m6
    punpcklwd       m2, m3, m6
    pmaddwd         m2, m1               ; a * src
    punpckhwd       m1, m0, m6
    punpckhwd       m3, m6
    pmaddwd         m3, m1
    psubd           m4, m2               ; b - a * src + (1 << 8)
    psubd           m5, m3
    psrad           m4, 9
    psrad           m5, 9
    packssdw        m4, m5
    pmulhrsw        m4, m7
    paddw           m0, m4
    pmaxsw          m0, m6
    pminsw          m0, m13
    mova     [dstq+wq], m0
    add             wq, 16
    jl .n0_loop
    add           dstq, stridemp
    ret
ALIGN function_align
.n1: ; neighbor + output (odd rows)
    movif64         wq, r4
    movif32         wd, w1m
.n1_loop:
    movu            m3, [t4+wq*1+400*2+4]
    movu            m1, [t4+wq*1+400*2+2]
    paddw           m3, [t4+wq*1+400*2+0]
    paddw           m1, m3
    psllw           m1, 2                ; a[ 1] 444
    psubw           m2, m1, m3           ; a[ 1] 343
    paddw           m3, m2, [t4+wq*1+400*6]
    paddw           m3, [t4+wq*1+400*8]
    mova [t4+wq*1+400*6], m1
    mova [t4+wq*1+400*8], m2
    movu            m4, [t3+wq*2+400*4+8]
    movu            m1, [t3+wq*2+400*4+4]
    paddd           m4, [t3+wq*2+400*4+0]
    paddd           m1, m4
    pslld           m1, 2                ; b[ 1] 444
    psubd           m2, m1, m4           ; b[ 1] 343
    paddd           m4, m2, [t3+wq*2+400*12+ 0]
    paddd           m4, [t3+wq*2+400*16+ 0]
    mova [t3+wq*2+400*12+ 0], m1
    mova [t3+wq*2+400*16+ 0], m2
    movu            m5, [t3+wq*2+400*4+24]
    movu            m1, [t3+wq*2+400*4+20]
    paddd           m5, [t3+wq*2+400*4+16]
    paddd           m1, m5
    pslld           m1, 2
    psubd           m2, m1, m5
    paddd           m5, m2, [t3+wq*2+400*12+16]
    paddd           m5, [t3+wq*2+400*16+16]
    mova [t3+wq*2+400*12+16], m1
    mova [t3+wq*2+400*16+16], m2
    mova            m0, [dstq+wq]
    punpcklwd       m1, m0, m6
    punpcklwd       m2, m3, m6
    pmaddwd         m2, m1               ; a * src
    punpckhwd       m1, m0, m6
    punpckhwd       m3, m6
    pmaddwd         m3, m1
    psubd           m4, m2               ; b - a * src + (1 << 8)
    psubd           m5, m3
    psrad           m4, 9
    psrad           m5, 9
    packssdw        m4, m5
    pmulhrsw        m4, m7
    paddw           m0, m4
    pmaxsw          m0, m6
    pminsw          m0, m13
    mova     [dstq+wq], m0
    add             wq, 16
    jl .n1_loop
    add           dstq, stridemp
    movif32       dstm, dstq
    ret

%if ARCH_X86_32
 %if STACK_ALIGNMENT < 16
  %assign extra_stack 10*16
 %else
  %assign extra_stack 8*16
 %endif
cglobal sgr_filter_mix_16bpc, 1, 7, 8, -400*66-48-extra_stack, \
                              dst, stride, left, lpf, w
 %if STACK_ALIGNMENT < 16
  %define dstm         dword [esp+calloff+16*8+4*0]
  %define stridemp     dword [esp+calloff+16*8+4*1]
  %define leftm        dword [esp+calloff+16*8+4*2]
  %define lpfm         dword [esp+calloff+16*8+4*3]
  %define w0m          dword [esp+calloff+16*8+4*4]
  %define hd           dword [esp+calloff+16*8+4*5]
  %define edgeb         byte [esp+calloff+16*8+4*6]
  %define edged        dword [esp+calloff+16*8+4*6]
  %define leftmp leftm
 %else
  %define w0m wm
  %define hd dword r5m
  %define edgeb  byte r7m
  %define edged dword r7m
 %endif
 %define hvsrcm dword [esp+calloff+4*0]
 %define w1m    dword [esp+calloff+4*1]
 %define t3m    dword [esp+calloff+4*2]
 %define t4m    dword [esp+calloff+4*3]
 %xdefine m8 m6
 %define  m9 [base+pd_8]
 %define m10 [base+pd_34816]
 %define m11 [base+pd_0xf00801c7]
 %define m12 [base+pd_0xf00800a4]
 %define m13 [esp+calloff+16*4]
 %define m14 [esp+calloff+16*5]
 %define m15 [esp+calloff+16*6]
 %define  m6 [esp+calloff+16*7]
 %define base r6-$$
 %assign calloff 0
 %if STACK_ALIGNMENT < 16
    mov        strideq, [rstk+stack_offset+ 8]
    mov          leftq, [rstk+stack_offset+12]
    mov           lpfq, [rstk+stack_offset+16]
    mov             wd, [rstk+stack_offset+20]
    mov           dstm, dstq
    mov       stridemp, strideq
    mov          leftm, leftq
    mov             r1, [rstk+stack_offset+24]
    mov             r2, [rstk+stack_offset+32]
    mov           lpfm, lpfq
    mov             hd, r1
    mov          edged, r2
 %endif
%else
cglobal sgr_filter_mix_16bpc, 4, 15, 16, -400*66-40, dst, stride, left, lpf, \
                                                     w, h, edge, params
%endif
%if ARCH_X86_64 || STACK_ALIGNMENT >= 16
    movifnidn       wd, wm
%endif
%if ARCH_X86_64
    mov        paramsq, r6mp
    lea            r13, [sgr_x_by_x-0xf03]
    movifnidn       hd, hm
    add             wd, wd
    mov          edged, r7m
    mova           m14, [paramsq]
    add           lpfq, wq
    mova            m9, [pd_8]
    lea             t1, [rsp+wq+44]
    mova           m10, [pd_34816]
    add           dstq, wq
    mova           m11, [pd_0xf00801c7]
    lea             t3, [rsp+wq*2+400*24+40]
    mova           m12, [pd_0xf00800a4]
    lea             t4, [rsp+wq+400*52+40]
    neg             wq
    pshufd         m15, m14, q2222 ; w0 w1
    punpcklwd      m14, m14
    pshufd         m13, m14, q0000 ; s0
    pshufd         m14, m14, q2222 ; s1
    pxor            m6, m6
    psllw          m15, 2
 DEFINE_ARGS dst, stride, left, lpf, _, h, edge, _, _, _, w
 %define lpfm [rsp]
%else
    mov             r1, [rstk+stack_offset+28] ; params
    LEA             r6, $$
    add             wd, wd
    mova            m2, [r1]
    add           lpfm, wq
    lea             t1, [rsp+extra_stack+wq+52]
    add           dstq, wq
    lea             t3, [rsp+extra_stack+wq*2+400*24+48]
    mov           dstm, dstq
    lea             t4, [rsp+extra_stack+wq+400*52+48]
    mov            t3m, t3
    mov            t4m, t4
    neg             wq
    pshuflw         m0, m2, q0000
    pshuflw         m1, m2, q2222
    pshufhw         m2, m2, q1010
    punpcklqdq      m0, m0 ; s0
    punpcklqdq      m1, m1 ; s1
    punpckhqdq      m2, m2 ; w0 w1
    mov            w1m, wd
    pxor            m3, m3
    psllw           m2, 2
    mova           m13, m0
    mova           m14, m1
    sub             wd, 4
    mova           m15, m2
    mova            m6, m3
    mov           lpfq, lpfm
    mov            w0m, wd
 %define strideq r5
%endif
    test         edgeb, 4 ; LR_HAVE_TOP
    jz .no_top
    call .h_top
    add           lpfq, stridemp
    mov             t2, t1
%if ARCH_X86_64
    call mangle(private_prefix %+ _sgr_filter_5x5_16bpc_ssse3).top_fixup
%else
    mov             wq, w0m
    call mangle(private_prefix %+ _sgr_filter_5x5_16bpc_ssse3).top_fixup_loop
%endif
    add             t1, 400*12
    call .h_top
    movif32    strideq, stridemp
    lea            r10, [lpfq+strideq*4]
    mov           lpfq, dstq
    add            r10, strideq
    mov           lpfm, r10 ; below
    movif32         t4, t4m
    call .hv0
.main:
    dec             hd
    jz .height1
    movif32       lpfq, hvsrcm
    add           lpfq, stridemp
    call .hv1
    call .prep_n
    sub             hd, 2
    jl .extend_bottom
.main_loop:
    movif32       lpfq, hvsrcm
    add           lpfq, stridemp
    call .hv0
%if ARCH_X86_64
    test            hd, hd
%else
    mov             r4, hd
    test            r4, r4
%endif
    jz .odd_height
    movif32       lpfq, hvsrcm
    add           lpfq, stridemp
    call .hv1
    call .n0
    call .n1
    sub             hd, 2
    jge .main_loop
    test         edgeb, 8 ; LR_HAVE_BOTTOM
    jz .extend_bottom
    mov           lpfq, lpfm
    call .hv0_bottom
    movif32       lpfq, hvsrcm
    add           lpfq, stridemp
    call .hv1_bottom
.end:
    call .n0
    call .n1
.end2:
    RET
.height1:
    call .v1
    call .prep_n
    jmp .odd_height_end
.odd_height:
    call .v1
    call .n0
    call .n1
.odd_height_end:
    call .v0
    call .v1
    call .n0
    jmp .end2
.extend_bottom:
    call .v0
    call .v1
    jmp .end
.no_top:
    movif32    strideq, stridemp
    lea            r10, [lpfq+strideq*4]
    mov           lpfq, dstq
    lea            r10, [r10+strideq*2]
    mov           lpfm, r10
    call .h
%if ARCH_X86_64
    lea             wq, [r4-4]
%else
    mov             wq, w0m
    mov         hvsrcm, lpfq
%endif
    lea             t2, [t1+400*12]
.top_fixup_loop:
    mova            m0, [t1+wq+400* 0]
    mova            m1, [t1+wq+400* 2]
    mova            m2, [t1+wq+400* 4]
    paddw           m0, m0
    mova            m3, [t1+wq+400* 6]
    paddd           m1, m1
    mova            m4, [t1+wq+400* 8]
    paddd           m2, m2
    mova            m5, [t1+wq+400*10]
    mova [t2+wq+400* 0], m0
    mova [t2+wq+400* 2], m1
    mova [t2+wq+400* 4], m2
    mova [t2+wq+400* 6], m3
    mova [t2+wq+400* 8], m4
    mova [t2+wq+400*10], m5
    add             wq, 16
    jl .top_fixup_loop
    movif32         t3, t3m
    movif32         t4, t4m
    call .v0
    jmp .main
.h: ; horizontal boxsum
%assign stack_offset stack_offset+4
%assign calloff 4
%if ARCH_X86_64
    lea             wq, [r4-4]
%else
 %define leftq r4
%endif
    test         edgeb, 1 ; LR_HAVE_LEFT
    jz .h_extend_left
    movif32      leftq, leftm
    movddup         m5, [leftq]
    movif32         wq, w0m
    mova            m4, [lpfq+wq+4]
    add         leftmp, 8
    palignr         m4, m5, 10
    jmp .h_main
.h_extend_left:
    movif32         wq, w0m
    mova            m4, [lpfq+wq+4]
    pshufb          m4, [base+sgr_lshuf5]
    jmp .h_main
.h_top:
%if ARCH_X86_64
    lea             wq, [r4-4]
%endif
    test         edgeb, 1 ; LR_HAVE_LEFT
    jz .h_extend_left
    movif32         wq, w0m
.h_loop:
    movu            m4, [lpfq+wq- 2]
.h_main:
    movu            m5, [lpfq+wq+14]
    test         edgeb, 2 ; LR_HAVE_RIGHT
    jnz .h_have_right
    cmp             wd, -20
    jl .h_have_right
%if ARCH_X86_32
    pxor            m8, m8
%endif
    call mangle(private_prefix %+ _sgr_filter_5x5_16bpc_ssse3).extend_right
.h_have_right:
    palignr         m3, m5, m4, 2
    palignr         m0, m5, m4, 4
    paddw           m1, m3, m0
    punpcklwd       m2, m3, m0
    pmaddwd         m2, m2
    punpckhwd       m3, m0
    pmaddwd         m3, m3
    palignr         m0, m5, m4, 6
    paddw           m1, m0             ; sum3
    punpcklwd       m7, m0, m6
    pmaddwd         m7, m7
    punpckhwd       m0, m6
    pmaddwd         m0, m0
    paddd           m2, m7             ; sumsq3
    palignr         m5, m4, 8
    punpcklwd       m7, m5, m4
    paddw           m8, m4, m5
    pmaddwd         m7, m7
    punpckhwd       m5, m4
    pmaddwd         m5, m5
    paddd           m3, m0
    mova [t1+wq+400* 6], m1
    mova [t1+wq+400* 8], m2
    mova [t1+wq+400*10], m3
    paddw           m8, m1             ; sum5
    paddd           m7, m2             ; sumsq5
    paddd           m5, m3
    mova [t1+wq+400* 0], m8
    mova [t1+wq+400* 2], m7
    mova [t1+wq+400* 4], m5
    add             wq, 16
    jl .h_loop
    ret
ALIGN function_align
.hv0: ; horizontal boxsum + vertical boxsum + ab3 (even rows)
%if ARCH_X86_64
    lea             wq, [r4-4]
%else
    mov         hvsrcm, lpfq
%endif
    test         edgeb, 1 ; LR_HAVE_LEFT
    jz .hv0_extend_left
    movif32      leftq, leftm
    movddup         m5, [leftq]
    movif32         wq, w0m
    mova            m4, [lpfq+wq+4]
    add         leftmp, 8
    palignr         m4, m5, 10
    jmp .hv0_main
.hv0_extend_left:
    movif32         wq, w0m
    mova            m4, [lpfq+wq+4]
    pshufb          m4, [base+sgr_lshuf5]
    jmp .hv0_main
.hv0_bottom:
%if ARCH_X86_64
    lea             wq, [r4-4]
%else
    mov         hvsrcm, lpfq
%endif
    test         edgeb, 1 ; LR_HAVE_LEFT
    jz .hv0_extend_left
    movif32         wq, w0m
%if ARCH_X86_32
    jmp .hv0_loop_start
%endif
.hv0_loop:
    movif32       lpfq, hvsrcm
.hv0_loop_start:
    movu            m4, [lpfq+wq- 2]
.hv0_main:
    movu            m5, [lpfq+wq+14]
    test         edgeb, 2 ; LR_HAVE_RIGHT
    jnz .hv0_have_right
    cmp             wd, -20
    jl .hv0_have_right
%if ARCH_X86_32
    pxor            m8, m8
%endif
    call mangle(private_prefix %+ _sgr_filter_5x5_16bpc_ssse3).extend_right
.hv0_have_right:
    palignr         m3, m5, m4, 2
    palignr         m0, m5, m4, 4
    movif32         t3, t3m
    paddw           m1, m3, m0
    punpcklwd       m2, m3, m0
    pmaddwd         m2, m2
    punpckhwd       m3, m0
    pmaddwd         m3, m3
    palignr         m0, m5, m4, 6
    paddw           m1, m0             ; h sum3
    punpcklwd       m7, m0, m6
    pmaddwd         m7, m7
    punpckhwd       m0, m6
    pmaddwd         m0, m0
    paddd           m2, m7             ; h sumsq3
    palignr         m5, m4, 8
    punpcklwd       m7, m5, m4
    paddw           m8, m4, m5
    pmaddwd         m7, m7
    punpckhwd       m5, m4
    pmaddwd         m5, m5
    paddd           m3, m0
    paddw           m8, m1             ; h sum5
    paddd           m7, m2             ; h sumsq5
    paddd           m5, m3
    mova [t3+wq*2+400*8+ 8], m8
    mova [t3+wq*2+400*0+ 8], m7
    mova [t3+wq*2+400*0+24], m5
    paddw           m8, [t1+wq+400* 0]
    paddd           m7, [t1+wq+400* 2]
    paddd           m5, [t1+wq+400* 4]
    mova [t1+wq+400* 0], m8
    mova [t1+wq+400* 2], m7
    mova [t1+wq+400* 4], m5
    paddw           m0, m1, [t1+wq+400* 6]
    paddd           m4, m2, [t1+wq+400* 8]
    paddd           m5, m3, [t1+wq+400*10]
    mova [t1+wq+400* 6], m1
    mova [t1+wq+400* 8], m2
    mova [t1+wq+400*10], m3
    paddw           m1, m0, [t2+wq+400* 6]
    paddd           m2, m4, [t2+wq+400* 8]
    paddd           m3, m5, [t2+wq+400*10]
    mova [t2+wq+400* 6], m0
    mova [t2+wq+400* 8], m4
    mova [t2+wq+400*10], m5
    paddd           m2, m9
    paddd           m3, m9
    psrld           m2, 4              ; (a3 + 8) >> 4
    psrld           m3, 4
%if ARCH_X86_32
    pxor            m7, m7
%else
    SWAP            m7, m6
%endif
    pslld           m4, m2, 3
    pslld           m5, m3, 3
    paddd           m4, m2             ; ((a3 + 8) >> 4) * 9
    paddd           m5, m3
    psrlw           m3, m1, 1
    pavgw           m3, m7             ; (b3 + 2) >> 2
    punpcklwd       m2, m3, m7
    pmaddwd         m2, m2
    punpckhwd       m3, m7
    pmaddwd         m3, m3
    punpcklwd       m0, m1, m7         ; b3
    punpckhwd       m1, m7
%if ARCH_X86_64
    SWAP            m7, m6
%endif
    MAXSD           m4, m2, m7
    MAXSD           m5, m3, m7
    psubd           m4, m2             ; p3
    psubd           m5, m3
    MULLD           m4, m14, m7        ; p3 * s1
    MULLD           m5, m14, m7
    pmaddwd         m0, m11            ; b3 * 455
    pmaddwd         m1, m11
    paddusw         m4, m11
    paddusw         m5, m11
    psrld           m4, 20             ; min(z3, 255)
    psrld           m5, 20
    GATHER_X_BY_X   m3, m4, m5, r0, dstm
    punpcklwd       m4, m3, m3
    punpckhwd       m5, m3, m3
    MULLD           m0, m4, m7
    MULLD           m1, m5, m7
    paddd           m0, m10            ; x3 * b3 * 455 + (1 << 11) + (1 << 15)
    paddd           m1, m10
    mova [t4+wq*1+400*2+ 4], m3
    psrld           m0, 12
    psrld           m1, 12
    mova [t3+wq*2+400*4+ 8], m0
    mova [t3+wq*2+400*4+24], m1
    add             wq, 16
    jl .hv0_loop
    ret
ALIGN function_align
.hv1: ; horizontal boxsums + vertical boxsums + ab (odd rows)
%if ARCH_X86_64
    lea             wq, [r4-4]
%else
    mov         hvsrcm, lpfq
%endif
    test         edgeb, 1 ; LR_HAVE_LEFT
    jz .hv1_extend_left
    movif32      leftq, leftm
    movddup         m5, [leftq]
    movif32         wq, w0m
    mova            m4, [lpfq+wq+4]
    add         leftmp, 8
    palignr         m4, m5, 10
    jmp .hv1_main
.hv1_extend_left:
    movif32         wq, w0m
    mova            m4, [lpfq+wq+4]
    pshufb          m4, [base+sgr_lshuf5]
    jmp .hv1_main
.hv1_bottom:
%if ARCH_X86_64
    lea             wq, [r4-4]
%else
    mov         hvsrcm, lpfq
%endif
    test         edgeb, 1 ; LR_HAVE_LEFT
    jz .hv1_extend_left
    movif32         wq, w0m
%if ARCH_X86_32
    jmp .hv1_loop_start
%endif
.hv1_loop:
    movif32       lpfq, hvsrcm
.hv1_loop_start:
    movu            m4, [lpfq+wq- 2]
.hv1_main:
    movu            m5, [lpfq+wq+14]
    test         edgeb, 2 ; LR_HAVE_RIGHT
    jnz .hv1_have_right
    cmp             wd, -20
    jl .hv1_have_right
%if ARCH_X86_32
    pxor            m8, m8
%endif
    call mangle(private_prefix %+ _sgr_filter_5x5_16bpc_ssse3).extend_right
.hv1_have_right:
    palignr         m7, m5, m4, 2
    palignr         m3, m5, m4, 4
    paddw           m2, m7, m3
    punpcklwd       m0, m7, m3
    pmaddwd         m0, m0
    punpckhwd       m7, m3
    pmaddwd         m7, m7
    palignr         m3, m5, m4, 6
    paddw           m2, m3             ; h sum3
    punpcklwd       m1, m3, m6
    pmaddwd         m1, m1
    punpckhwd       m3, m6
    pmaddwd         m3, m3
    paddd           m0, m1             ; h sumsq3
    palignr         m5, m4, 8
    punpckhwd       m1, m4, m5
    paddw           m8, m4, m5
    pmaddwd         m1, m1
    punpcklwd       m4, m5
    pmaddwd         m4, m4
    paddd           m7, m3
    paddw           m5, m2, [t2+wq+400* 6]
    mova [t2+wq+400* 6], m2
    paddw           m8, m2             ; h sum5
    paddd           m2, m0, [t2+wq+400* 8]
    paddd           m3, m7, [t2+wq+400*10]
    mova [t2+wq+400* 8], m0
    mova [t2+wq+400*10], m7
    paddd           m4, m0             ; h sumsq5
    paddd           m1, m7
    paddd           m2, m9
    paddd           m3, m9
    psrld           m2, 4              ; (a3 + 8) >> 4
    psrld           m3, 4
    pslld           m0, m2, 3
    pslld           m7, m3, 3
    paddd           m2, m0             ; ((a3 + 8) >> 4) * 9
    paddd           m3, m7
    psrlw           m7, m5, 1
    pavgw           m7, m6             ; (b3 + 2) >> 2
    punpcklwd       m0, m7, m6
    pmaddwd         m0, m0
    punpckhwd       m7, m6
    pmaddwd         m7, m7
%if ARCH_X86_32
    mova      [esp+20], m8
%else
    SWAP            m8, m6
%endif
    MAXSD           m2, m0, m8
    MAXSD           m3, m7, m8
    pxor            m8, m8
    psubd           m2, m0             ; p3
    psubd           m3, m7
    punpcklwd       m0, m5, m8         ; b3
    punpckhwd       m5, m8
    MULLD           m2, m14, m8        ; p3 * s1
    MULLD           m3, m14, m8
    pmaddwd         m0, m11            ; b3 * 455
    pmaddwd         m5, m11
    paddusw         m2, m11
    paddusw         m3, m11
    psrld           m2, 20             ; min(z3, 255)
    movif32         t3, t3m
    psrld           m3, 20
    GATHER_X_BY_X   m8, m2, m3, r0, dstm
    punpcklwd       m2, m8, m8
    punpckhwd       m3, m8, m8
    MULLD           m0, m2, m7
    MULLD           m5, m3, m7
    paddd           m0, m10            ; x3 * b3 * 455 + (1 << 11) + (1 << 15)
    paddd           m5, m10
    psrld           m0, 12
    psrld           m5, 12
    mova [t4+wq*1+400*4+4], m8
    mova [t3+wq*2+400*8+ 8], m0
    mova [t3+wq*2+400*8+24], m5
%if ARCH_X86_32
    mova            m8, [esp+20]
%else
    SWAP            m6, m8
    pxor            m6, m6
%endif
    paddw           m5, m8, [t2+wq+400*0]
    paddd           m2, m4, [t2+wq+400*2]
    paddd           m3, m1, [t2+wq+400*4]
    paddw           m5, [t1+wq+400*0]
    paddd           m2, [t1+wq+400*2]
    paddd           m3, [t1+wq+400*4]
    mova [t2+wq+400*0], m8
    paddd           m2, m9
    paddd           m3, m9
    psrld           m2, 4              ; (a5 + 8) >> 4
    psrld           m3, 4
    mova [t2+wq+400*2], m4
    pslld           m8, m2, 4
    mova [t2+wq+400*4], m1
    pslld           m4, m3, 4
    paddd           m8, m2
    pslld           m2, 3
    paddd           m4, m3
    pslld           m3, 3
    paddd           m2, m8             ; ((a5 + 8) >> 4) * 25
    paddd           m3, m4
%if ARCH_X86_32
    pxor            m7, m7
%else
    SWAP            m7, m6
%endif
    psrlw           m1, m5, 1
    pavgw           m1, m7             ; (b5 + 2) >> 2
    punpcklwd       m4, m1, m7
    pmaddwd         m4, m4
    punpckhwd       m1, m7
    pmaddwd         m1, m1
    punpcklwd       m0, m5, m7         ; b5
    punpckhwd       m5, m7
%if ARCH_X86_64
    SWAP            m7, m6
%endif
    MAXSD           m2, m4, m7
    psubd           m2, m4             ; p5
    MAXSD           m3, m1, m7
    psubd           m3, m1
    MULLD           m2, m13, m7        ; p5 * s0
    MULLD           m3, m13, m7
    pmaddwd         m0, m12             ; b5 * 164
    pmaddwd         m5, m12
    paddusw         m2, m12
    paddusw         m3, m12
    psrld           m2, 20             ; min(z5, 255)
    psrld           m3, 20
    GATHER_X_BY_X   m1, m2, m3, r0, dstm
    punpcklwd       m2, m1, m1
    punpckhwd       m3, m1, m1
    MULLD           m0, m2, m7
    MULLD           m5, m3, m7
    paddd           m0, m10            ; x5 * b5 * 164 + (1 << 11) + (1 << 15)
    paddd           m5, m10
    mova [t4+wq*1+400*0+ 4], m1
    psrld           m0, 12
    psrld           m5, 12
    mova [t3+wq*2+400*0+ 8], m0
    mova [t3+wq*2+400*0+24], m5
    add             wq, 16
    jl .hv1_loop
    mov            r10, t2
    mov             t2, t1
    mov             t1, r10
    ret
.v0: ; vertical boxsums + ab3 (even rows)
%if ARCH_X86_64
    lea             wq, [r4-4]
%else
    mov             wd, w0m
%endif
.v0_loop:
    mova            m0, [t1+wq+400* 6]
    mova            m4, [t1+wq+400* 8]
    mova            m5, [t1+wq+400*10]
    paddw           m0, m0
    paddd           m4, m4
    paddd           m5, m5
    paddw           m1, m0, [t2+wq+400* 6]
    paddd           m2, m4, [t2+wq+400* 8]
    paddd           m3, m5, [t2+wq+400*10]
    mova [t2+wq+400* 6], m0
    mova [t2+wq+400* 8], m4
    mova [t2+wq+400*10], m5
    paddd           m2, m9
    paddd           m3, m9
    psrld           m2, 4              ; (a3 + 8) >> 4
    psrld           m3, 4
%if ARCH_X86_32
    pxor            m7, m7
%else
    SWAP            m7, m6
%endif
    pslld           m4, m2, 3
    pslld           m5, m3, 3
    paddd           m4, m2             ; ((a3 + 8) >> 4) * 9
    paddd           m5, m3
    psrlw           m3, m1, 1
    pavgw           m3, m7             ; (b3 + 2) >> 2
    punpcklwd       m2, m3, m7
    pmaddwd         m2, m2
    punpckhwd       m3, m7
    pmaddwd         m3, m3
    punpcklwd       m0, m1, m7         ; b3
    punpckhwd       m1, m7
%if ARCH_X86_64
    SWAP            m7, m6
%endif
    MAXSD           m4, m2, m7
    MAXSD           m5, m3, m7
    psubd           m4, m2             ; p3
    psubd           m5, m3
    MULLD           m4, m14, m7        ; p3 * s1
    MULLD           m5, m14, m7
    pmaddwd         m0, m11            ; b3 * 455
    pmaddwd         m1, m11
    paddusw         m4, m11
    paddusw         m5, m11
    psrld           m4, 20             ; min(z3, 255)
    psrld           m5, 20
    GATHER_X_BY_X   m3, m4, m5, r0, dstm
    punpcklwd       m4, m3, m3
    punpckhwd       m5, m3, m3
    MULLD           m0, m4, m7
    MULLD           m1, m5, m7
    paddd           m0, m10            ; x3 * b3 * 455 + (1 << 11) + (1 << 15)
    paddd           m1, m10
    mova [t4+wq*1+400*2+4], m3
    psrld           m0, 12
    psrld           m1, 12
    mova            m3, [t1+wq+400*0]
    mova            m4, [t1+wq+400*2]
    mova            m5, [t1+wq+400*4]
    mova [t3+wq*2+400*8+ 8], m3
    mova [t3+wq*2+400*0+ 8], m4
    mova [t3+wq*2+400*0+24], m5
    paddw           m3, m3 ; cc5
    paddd           m4, m4
    paddd           m5, m5
    mova [t1+wq+400*0], m3
    mova [t1+wq+400*2], m4
    mova [t1+wq+400*4], m5
    mova [t3+wq*2+400*4+ 8], m0
    mova [t3+wq*2+400*4+24], m1
    add             wq, 16
    jl .v0_loop
    ret
.v1: ; vertical boxsums + ab (odd rows)
%if ARCH_X86_64
    lea             wq, [r4-4]
%else
    mov             wd, w0m
%endif
.v1_loop:
    mova            m4, [t1+wq+400* 6]
    mova            m5, [t1+wq+400* 8]
    mova            m7, [t1+wq+400*10]
    paddw           m1, m4, [t2+wq+400* 6]
    paddd           m2, m5, [t2+wq+400* 8]
    paddd           m3, m7, [t2+wq+400*10]
    mova [t2+wq+400* 6], m4
    mova [t2+wq+400* 8], m5
    mova [t2+wq+400*10], m7
    paddd           m2, m9
    paddd           m3, m9
    psrld           m2, 4              ; (a3 + 8) >> 4
    psrld           m3, 4
%if ARCH_X86_32
    pxor            m7, m7
%else
    SWAP            m7, m6
%endif
    pslld           m4, m2, 3
    pslld           m5, m3, 3
    paddd           m4, m2             ; ((a3 + 8) >> 4) * 9
    paddd           m5, m3
    psrlw           m3, m1, 1
    pavgw           m3, m7             ; (b3 + 2) >> 2
    punpcklwd       m2, m3, m7
    pmaddwd         m2, m2
    punpckhwd       m3, m7
    pmaddwd         m3, m3
    punpcklwd       m0, m1, m7         ; b3
    punpckhwd       m1, m7
%if ARCH_X86_64
    SWAP            m7, m6
%endif
    MAXSD           m4, m2, m7
    MAXSD           m5, m3, m7
    psubd           m4, m2             ; p3
    psubd           m5, m3
    MULLD           m4, m14, m7        ; p3 * s1
    MULLD           m5, m14, m7
    pmaddwd         m0, m11            ; b3 * 455
    pmaddwd         m1, m11
    paddusw         m4, m11
    paddusw         m5, m11
    psrld           m4, 20             ; min(z3, 255)
    psrld           m5, 20
    GATHER_X_BY_X   m3, m4, m5, r0, dstm
    punpcklwd       m4, m3, m3
    punpckhwd       m5, m3, m3
    MULLD           m0, m4, m7
    MULLD           m1, m5, m7
    paddd           m0, m10            ; x3 * b3 * 455 + (1 << 11) + (1 << 15)
    paddd           m1, m10
    mova [t4+wq*1+400*4+4], m3
    psrld           m0, 12
    psrld           m8, m1, 12
    mova            m4, [t3+wq*2+400*8+ 8]
    mova            m5, [t3+wq*2+400*0+ 8]
    mova            m7, [t3+wq*2+400*0+24]
    paddw           m1, m4, [t2+wq+400*0]
    paddd           m2, m5, [t2+wq+400*2]
    paddd           m3, m7, [t2+wq+400*4]
    paddw           m1, [t1+wq+400*0]
    paddd           m2, [t1+wq+400*2]
    paddd           m3, [t1+wq+400*4]
    mova [t2+wq+400*0], m4
    mova [t2+wq+400*2], m5
    mova [t2+wq+400*4], m7
    paddd           m2, m9
    paddd           m3, m9
    psrld           m2, 4              ; (a5 + 8) >> 4
    psrld           m3, 4
    mova         [t3+wq*2+400*8+ 8], m0
    pslld           m4, m2, 4
    mova         [t3+wq*2+400*8+24], m8
    pslld           m5, m3, 4
    paddd           m4, m2
    pslld           m2, 3
    paddd           m5, m3
    pslld           m3, 3
    paddd           m2, m4
    paddd           m3, m5
%if ARCH_X86_32
    pxor            m7, m7
%else
    SWAP            m7, m6
%endif
    psrlw           m5, m1, 1
    pavgw           m5, m7             ; (b5 + 2) >> 2
    punpcklwd       m4, m5, m7
    pmaddwd         m4, m4
    punpckhwd       m5, m7
    pmaddwd         m5, m5
    punpcklwd       m0, m1, m7         ; b5
    punpckhwd       m1, m7
%if ARCH_X86_64
    SWAP            m7, m6
%endif
    MAXSD           m2, m4, m7
    psubd           m2, m4             ; p5
    MAXSD           m3, m5, m7
    psubd           m3, m5
    MULLD           m2, m13, m7        ; p5 * s0
    MULLD           m3, m13, m7
    pmaddwd         m0, m12            ; b5 * 164
    pmaddwd         m1, m12
    paddusw         m2, m12
    paddusw         m3, m12
    psrld           m2, 20             ; min(z5, 255)
    psrld           m3, 20
    GATHER_X_BY_X   m4, m2, m3, r0, dstm
    punpcklwd       m2, m4, m4
    punpckhwd       m3, m4, m4
    MULLD           m0, m2, m7
    MULLD           m1, m3, m7
    paddd           m0, m10            ; x5 * b5 * 164 + (1 << 11) + (1 << 15)
    paddd           m1, m10
    mova [t4+wq*1+400*0+ 4], m4
    psrld           m0, 12
    psrld           m1, 12
    mova [t3+wq*2+400*0+ 8], m0
    mova [t3+wq*2+400*0+24], m1
    add             wq, 16
    jl .v1_loop
    mov            r10, t2
    mov             t2, t1
    mov             t1, r10
    ret
.prep_n: ; initial neighbor setup
    movif64         wq, r4
    movif32         wd, w1m
.prep_n_loop:
    movu            m0, [t4+wq*1+400*0+ 2]
    movu            m1, [t3+wq*2+400*0+ 4]
    movu            m2, [t3+wq*2+400*0+20]
    movu            m7, [t4+wq*1+400*0+ 4]
    movu            m8, [t3+wq*2+400*0+ 8]
    paddw           m3, m0, [t4+wq*1+400*0+ 0]
    paddd           m4, m1, [t3+wq*2+400*0+ 0]
    paddd           m5, m2, [t3+wq*2+400*0+16]
    paddw           m3, m7
    paddd           m4, m8
    movu            m7, [t3+wq*2+400*0+24]
    paddw           m0, m3
    paddd           m1, m4
    psllw           m3, 2
    pslld           m4, 2
    paddd           m5, m7
    paddd           m2, m5
    pslld           m5, 2
    paddw           m0, m3               ; a5 565
    paddd           m1, m4               ; b5 565
    paddd           m2, m5
    mova [t4+wq*1+400* 6+ 0], m0
    mova [t3+wq*2+400*12+ 0], m1
    mova [t3+wq*2+400*12+16], m2
    movu            m0, [t4+wq*1+400*2+ 4]
    movu            m1, [t3+wq*2+400*4+ 8]
    movu            m2, [t3+wq*2+400*4+24]
    movu            m3, [t4+wq*1+400*2+ 2]
    movu            m4, [t3+wq*2+400*4+ 4]
    movu            m5, [t3+wq*2+400*4+20]
    paddw           m0, [t4+wq*1+400*2+ 0]
    paddd           m1, [t3+wq*2+400*4+ 0]
    paddd           m2, [t3+wq*2+400*4+16]
    paddw           m3, m0
    paddd           m4, m1
    paddd           m5, m2
    psllw           m3, 2                ; a3[-1] 444
    pslld           m4, 2                ; b3[-1] 444
    pslld           m5, 2
    psubw           m3, m0               ; a3[-1] 343
    psubd           m4, m1               ; b3[-1] 343
    psubd           m5, m2
    mova [t4+wq*1+400* 8+ 0], m3
    mova [t3+wq*2+400*16+ 0], m4
    mova [t3+wq*2+400*16+16], m5
    movu            m0, [t4+wq*1+400*4+ 4]
    movu            m1, [t3+wq*2+400*8+ 8]
    movu            m2, [t3+wq*2+400*8+24]
    movu            m3, [t4+wq*1+400*4+ 2]
    movu            m4, [t3+wq*2+400*8+ 4]
    movu            m5, [t3+wq*2+400*8+20]
    paddw           m0, [t4+wq*1+400*4+ 0]
    paddd           m1, [t3+wq*2+400*8+ 0]
    paddd           m2, [t3+wq*2+400*8+16]
    paddw           m3, m0
    paddd           m4, m1
    paddd           m5, m2
    psllw           m3, 2                 ; a3[ 0] 444
    pslld           m4, 2                 ; b3[ 0] 444
    pslld           m5, 2
    mova [t4+wq*1+400*10+ 0], m3
    mova [t3+wq*2+400*20+ 0], m4
    mova [t3+wq*2+400*20+16], m5
    psubw           m3, m0                ; a3[ 0] 343
    psubd           m4, m1                ; b3[ 0] 343
    psubd           m5, m2
    mova [t4+wq*1+400*12+ 0], m3
    mova [t3+wq*2+400*24+ 0], m4
    mova [t3+wq*2+400*24+16], m5
    add             wq, 16
    jl .prep_n_loop
    ret
ALIGN function_align
.n0: ; neighbor + output (even rows)
    movif64         wq, r4
    movif32         wd, w1m
.n0_loop:
    movu            m0, [t4+wq*1+ 4]
    movu            m2, [t4+wq*1+ 2]
    paddw           m0, [t4+wq*1+ 0]
    paddw           m0, m2
    paddw           m2, m0
    psllw           m0, 2
    paddw           m0, m2               ; a5
    movu            m4, [t3+wq*2+ 8]
    movu            m5, [t3+wq*2+24]
    movu            m1, [t3+wq*2+ 4]
    movu            m3, [t3+wq*2+20]
    paddd           m4, [t3+wq*2+ 0]
    paddd           m5, [t3+wq*2+16]
    paddd           m4, m1
    paddd           m5, m3
    paddd           m1, m4
    paddd           m3, m5
    pslld           m4, 2
    pslld           m5, 2
    paddd           m4, m1               ; b5
    paddd           m5, m3
    movu            m2, [t4+wq*1+400* 6]
    paddw           m2, m0
    mova [t4+wq*1+400* 6], m0
    paddd           m0, m4, [t3+wq*2+400*12+ 0]
    paddd           m1, m5, [t3+wq*2+400*12+16]
    mova [t3+wq*2+400*12+ 0], m4
    mova [t3+wq*2+400*12+16], m5
    mova [rsp+16+ARCH_X86_32*4], m1
    movu            m3, [t4+wq*1+400*2+4]
    movu            m5, [t4+wq*1+400*2+2]
    paddw           m3, [t4+wq*1+400*2+0]
    paddw           m5, m3
    psllw           m5, 2                ; a3[ 1] 444
    psubw           m4, m5, m3           ; a3[ 1] 343
    movu            m3, [t4+wq*1+400* 8]
    paddw           m3, [t4+wq*1+400*10]
    paddw           m3, m4
    mova [t4+wq*1+400* 8], m4
    mova [t4+wq*1+400*10], m5
    movu            m1, [t3+wq*2+400*4+ 8]
    movu            m5, [t3+wq*2+400*4+ 4]
    movu            m7, [t3+wq*2+400*4+24]
    movu            m8, [t3+wq*2+400*4+20]
    paddd           m1, [t3+wq*2+400*4+ 0]
    paddd           m7, [t3+wq*2+400*4+16]
    paddd           m5, m1
    paddd           m8, m7
    pslld           m5, 2                ; b3[ 1] 444
    pslld           m8, 2
    psubd           m4, m5, m1           ; b3[ 1] 343
%if ARCH_X86_32
    mova      [esp+52], m8
    psubd           m8, m7
%else
    psubd           m6, m8, m7
    SWAP            m8, m6
%endif
    paddd           m1, m4, [t3+wq*2+400*16+ 0]
    paddd           m7, m8, [t3+wq*2+400*16+16]
    paddd           m1, [t3+wq*2+400*20+ 0]
    paddd           m7, [t3+wq*2+400*20+16]
    mova [t3+wq*2+400*16+ 0], m4
    mova [t3+wq*2+400*16+16], m8
    mova [t3+wq*2+400*20+ 0], m5
%if ARCH_X86_32
    mova            m8, [esp+52]
%else
    SWAP            m8, m6
    pxor            m6, m6
%endif
    mova [t3+wq*2+400*20+16], m8
    mova [rsp+32+ARCH_X86_32*4], m7
    movu            m5, [dstq+wq]
    punpcklwd       m4, m5, m6
    punpcklwd       m7, m2, m6
    pmaddwd         m7, m4               ; a5 * src
    punpcklwd       m8, m3, m6
    pmaddwd         m8, m4               ; a3 * src
    punpckhwd       m5, m6
    punpckhwd       m2, m6
    pmaddwd         m2, m5
    punpckhwd       m3, m6
    pmaddwd         m3, m5
    pslld           m4, 13
    pslld           m5, 13
    psubd           m0, m7               ; b5 - a5 * src + (1 << 8)
    psubd           m1, m8               ; b3 - a3 * src + (1 << 8)
    mova            m7, [base+pd_0xffff]
    psrld           m0, 9
    pslld           m1, 7
    pand            m0, m7
    pandn           m8, m7, m1
    por             m0, m8
    mova            m1, [rsp+16+ARCH_X86_32*4]
    mova            m8, [rsp+32+ARCH_X86_32*4]
    psubd           m1, m2
    psubd           m8, m3
    mova            m2, [base+pd_4096]
    psrld           m1, 9
    pslld           m8, 7
    pand            m1, m7
    pandn           m7, m8
    por             m1, m7
    pmaddwd         m0, m15
    pmaddwd         m1, m15
%if ARCH_X86_32
    pxor            m7, m7
%else
    SWAP            m7, m6
%endif
    paddd           m4, m2
    paddd           m5, m2
    paddd           m0, m4
    paddd           m1, m5
    psrad           m0, 8
    psrad           m1, 8
    packssdw        m0, m1               ; clip
    pmaxsw          m0, m7
    psrlw           m0, 5
    mova     [dstq+wq], m0
    add             wq, 16
    jl .n0_loop
    add           dstq, stridemp
    ret
%if ARCH_X86_64
    SWAP            m6, m7
%endif
ALIGN function_align
.n1: ; neighbor + output (odd rows)
    movif64         wq, r4
    movif32         wd, w1m
.n1_loop:
    movu            m3, [t4+wq*1+400*4+4]
    movu            m5, [t4+wq*1+400*4+2]
    paddw           m3, [t4+wq*1+400*4+0]
    paddw           m5, m3
    psllw           m5, 2                ; a3[ 1] 444
    psubw           m4, m5, m3           ; a3[ 1] 343
    paddw           m3, m4, [t4+wq*1+400*12]
    paddw           m3, [t4+wq*1+400*10]
    mova [t4+wq*1+400*10], m5
    mova [t4+wq*1+400*12], m4
    movu            m1, [t3+wq*2+400*8+ 8]
    movu            m5, [t3+wq*2+400*8+ 4]
    movu            m7, [t3+wq*2+400*8+24]
    movu            m8, [t3+wq*2+400*8+20]
    paddd           m1, [t3+wq*2+400*8+ 0]
    paddd           m7, [t3+wq*2+400*8+16]
    paddd           m5, m1
    paddd           m8, m7
    pslld           m5, 2                ; b3[ 1] 444
    pslld           m8, 2
    psubd           m4, m5, m1           ; b3[ 1] 343
    psubd           m0, m8, m7
    paddd           m1, m4, [t3+wq*2+400*24+ 0]
    paddd           m7, m0, [t3+wq*2+400*24+16]
    paddd           m1, [t3+wq*2+400*20+ 0]
    paddd           m7, [t3+wq*2+400*20+16]
    mova [t3+wq*2+400*20+ 0], m5
    mova [t3+wq*2+400*20+16], m8
    mova [t3+wq*2+400*24+ 0], m4
    mova [t3+wq*2+400*24+16], m0
    mova            m5, [dstq+wq]
    mova            m2, [t4+wq*1+400* 6]
    punpcklwd       m4, m5, m6
    punpcklwd       m8, m2, m6
    pmaddwd         m8, m4               ; a5 * src
    punpcklwd       m0, m3, m6
    pmaddwd         m0, m4               ; a3 * src
    punpckhwd       m5, m6
    punpckhwd       m2, m6
    pmaddwd         m2, m5
    punpckhwd       m3, m6
    pmaddwd         m3, m5
    psubd           m1, m0               ; b3 - a3 * src + (1 << 8)
    pslld           m4, 13
    pslld           m5, 13
    mova            m0, [t3+wq*2+400*12+ 0]
    psubd           m0, m8               ; b5 - a5 * src + (1 << 8)
    mova            m8, [t3+wq*2+400*12+16]
    psubd           m8, m2
    psubd           m7, m3
    mova            m2, [base+pd_0xffff]
    pslld           m1, 7
    psrld           m0, 8
    psrld           m8, 8
    pslld           m7, 7
    pand            m0, m2
    pandn           m3, m2, m1
    por             m0, m3
    pand            m8, m2
    pandn           m2, m7
    por             m2, m8
    mova            m1, [base+pd_4096]
    pmaddwd         m0, m15
    pmaddwd         m2, m15
%if ARCH_X86_64
    SWAP            m7, m6
%endif
    pxor            m7, m7
    paddd           m4, m1
    paddd           m5, m1
    paddd           m0, m4
    paddd           m2, m5
    psrad           m0, 8
    psrad           m2, 8
    packssdw        m0, m2              ; clip
    pmaxsw          m0, m7
    psrlw           m0, 5
    mova     [dstq+wq], m0
    add             wq, 16
    jl .n1_loop
    add           dstq, stridemp
    movif32       dstm, dstq
    ret