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
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
|
/* pnggccrd.c - mixed C/assembler version of utilities to read a PNG file
*
* For Intel x86 CPU (Pentium-MMX or later) and GNU C compiler.
*
* See http://www.intel.com/drg/pentiumII/appnotes/916/916.htm
* and http://www.intel.com/drg/pentiumII/appnotes/923/923.htm
* for Intel's performance analysis of the MMX vs. non-MMX code.
*
* libpng 1.0.7beta13 - May 16, 2000
* For conditions of distribution and use, see copyright notice in png.h
* Copyright (c) 1998, Intel Corporation
* Copyright (c) 1998, 1999, 2000 Glenn Randers-Pehrson
*
* Based on MSVC code contributed by Nirav Chhatrapati, Intel Corp., 1998.
* Interface to libpng contributed by Gilles Vollant, 1999.
* GNU C port by Greg Roelofs, 1999.
*
* Lines 2350-4300 converted in place with intel2gas 1.3.1:
*
* intel2gas -mdI pnggccrd.c.partially-msvc -o pnggccrd.c
*
* and then cleaned up by hand. See http://hermes.terminal.at/intel2gas/ .
*
* NOTE: A sufficiently recent version of GNU as (or as.exe under DOS/Windows)
* is required to assemble the newer MMX instructions such as movq.
* For djgpp, see
*
* ftp://ftp.simtel.net/pub/simtelnet/gnu/djgpp/v2gnu/bnu281b.zip
*
* (or a later version in the same directory). For Linux, check your
* distribution's web site(s) or try these links:
*
* http://rufus.w3.org/linux/RPM/binutils.html
* http://www.debian.org/Packages/stable/devel/binutils.html
* ftp://ftp.slackware.com/pub/linux/slackware/slackware/slakware/d1/
* binutils.tgz
*
* For other platforms, see the main GNU site:
*
* ftp://ftp.gnu.org/pub/gnu/binutils/
*
* Version 2.5.2l.15 is definitely too old...
*/
/*
* GRR NOTES
* =========
*
* 19991006:
* - fixed sign error in post-MMX cleanup code (16- & 32-bit cases)
*
* 19991007:
* - additional optimizations (possible or definite):
* x [DONE] write MMX code for 64-bit case (pixel_bytes == 8) [not tested]
* - write MMX code for 48-bit case (pixel_bytes == 6)
* - figure out what's up with 24-bit case (pixel_bytes == 3):
* why subtract 8 from width_mmx in the pass 4/5 case?
* (only width_mmx case)
* x [DONE] replace pixel_bytes within each block with the true
* constant value (or are compilers smart enough to do that?)
* - rewrite all MMX interlacing code so it's aligned with
* the *beginning* of the row buffer, not the end. This
* would not only allow one to eliminate half of the memory
* writes for odd passes (i.e., pass == odd), it may also
* eliminate some unaligned-data-access exceptions (assuming
* there's a penalty for not aligning 64-bit accesses on
* 64-bit boundaries). The only catch is that the "leftover"
* pixel(s) at the end of the row would have to be saved,
* but there are enough unused MMX registers in every case,
* so this is not a problem. A further benefit is that the
* post-MMX cleanup code (C code) in at least some of the
* cases could be done within the assembler block.
* x [DONE] the "v3 v2 v1 v0 v7 v6 v5 v4" comments are confusing,
* inconsistent, and don't match the MMX Programmer's Reference
* Manual conventions anyway. They should be changed to
* "b7 b6 b5 b4 b3 b2 b1 b0," where b0 indicates the byte that
* was lowest in memory (e.g., corresponding to a left pixel)
* and b7 is the byte that was highest (e.g., a right pixel).
*
* 19991016:
* - Brennan's Guide notwithstanding, gcc under Linux does *not*
* want globals prefixed by underscores when referencing them--
* i.e., if the variable is const4, then refer to it as const4,
* not _const4. This seems to be a djgpp-specific requirement.
* Also, such variables apparently *must* be declared outside
* of functions; neither static nor automatic variables work if
* defined within the scope of a single function, but both
* static and truly global (multi-module) variables work fine.
*
* 19991023:
* - fixed png_combine_row() non-MMX replication bug (odd passes only?)
* - switched from string-concatenation-with-macros to cleaner method of
* renaming global variables for djgpp--i.e., always use prefixes in
* inlined assembler code (== strings) and conditionally rename the
* variables, not the other way around. Hence _const4, _mask8_0, etc.
*
* 19991024:
* - fixed mmxsupport()/png_do_interlace() first-row bug
* This one was severely weird: even though mmxsupport() doesn't touch
* ebx (where "row" pointer was stored), it nevertheless managed to zero
* the register (even in static/non-fPIC code--see below), which in turn
* caused png_do_interlace() to return prematurely on the first row of
* interlaced images (i.e., without expanding the interlaced pixels).
* Inspection of the generated assembly code didn't turn up any clues,
* although it did point at a minor optimization (i.e., get rid of
* mmx_supported_local variable and just use eax). Possibly the CPUID
* instruction is more destructive than it looks? (Not yet checked.)
* - "info gcc" was next to useless, so compared fPIC and non-fPIC assembly
* listings... Apparently register spillage has to do with ebx, since
* it's used to index the global offset table. Commenting it out of the
* input-reg lists in png_combine_row() eliminated compiler barfage, so
* ifdef'd with __PIC__ macro: if defined, use a global for unmask
*
* 19991107:
* - verified CPUID clobberage: 12-char string constant ("GenuineIntel",
* "AuthenticAMD", etc.) placed in EBX:ECX:EDX. Still need to polish.
*
* 19991120:
* - made "diff" variable (now "_dif") global to simplify conversion of
* filtering routines (running out of regs, sigh). "diff" is still used
* in interlacing routines, however.
* - fixed up both versions of mmxsupport() (ORIG_THAT_USED_TO_CLOBBER_EBX
* macro determines which is used); original not yet tested.
*
* 20000319:
* - fixed a register-name typo in png_do_read_interlace(), default (MMX) case,
* pass == 4 or 5, that caused visible corruption of interlaced images
*/
#define PNG_INTERNAL
#include "png.h"
#if defined(PNG_ASSEMBLER_CODE_SUPPORTED) && defined(PNG_USE_PNGGCCRD)
int mmxsupport(void);
static int mmx_supported = 2;
#ifdef PNG_USE_LOCAL_ARRAYS
static const int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
static const int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
static const int png_pass_width[7] = {8, 4, 4, 2, 2, 1, 1};
#endif
// djgpp adds its own underscores to global variables, so define them without:
#ifdef __DJGPP__
# define _unmask unmask
# define _const4 const4
# define _const6 const6
# define _mask8_0 mask8_0
# define _mask16_1 mask16_1
# define _mask16_0 mask16_0
# define _mask24_2 mask24_2
# define _mask24_1 mask24_1
# define _mask24_0 mask24_0
# define _mask32_3 mask32_3
# define _mask32_2 mask32_2
# define _mask32_1 mask32_1
# define _mask32_0 mask32_0
# define _mask48_5 mask48_5
# define _mask48_4 mask48_4
# define _mask48_3 mask48_3
# define _mask48_2 mask48_2
# define _mask48_1 mask48_1
# define _mask48_0 mask48_0
# define _FullLength FullLength
# define _MMXLength MMXLength
# define _dif dif
#endif
/* These constants are used in the inlined MMX assembly code.
Ignore gcc's "At top level: defined but not used" warnings. */
#ifdef __PIC__
static int _unmask; // not enough regs when compiling with -fPIC, so...
#endif
static unsigned long long _mask8_0 = 0x0102040810204080LL;
static unsigned long long _mask16_1 = 0x0101020204040808LL;
static unsigned long long _mask16_0 = 0x1010202040408080LL;
static unsigned long long _mask24_2 = 0x0101010202020404LL;
static unsigned long long _mask24_1 = 0x0408080810101020LL;
static unsigned long long _mask24_0 = 0x2020404040808080LL;
static unsigned long long _mask32_3 = 0x0101010102020202LL;
static unsigned long long _mask32_2 = 0x0404040408080808LL;
static unsigned long long _mask32_1 = 0x1010101020202020LL;
static unsigned long long _mask32_0 = 0x4040404080808080LL;
static unsigned long long _mask48_5 = 0x0101010101010202LL;
static unsigned long long _mask48_4 = 0x0202020204040404LL;
static unsigned long long _mask48_3 = 0x0404080808080808LL;
static unsigned long long _mask48_2 = 0x1010101010102020LL;
static unsigned long long _mask48_1 = 0x2020202040404040LL;
static unsigned long long _mask48_0 = 0x4040808080808080LL;
static unsigned long long _const4 = 0x0000000000FFFFFFLL;
//static unsigned long long _const5 = 0x000000FFFFFF0000LL; // NOT USED
static unsigned long long _const6 = 0x00000000000000FFLL;
// These are used in the row-filter routines and should/would be local
// variables if not for gcc addressing limitations.
static png_uint_32 _FullLength;
static png_uint_32 _MMXLength;
static int _dif;
void /* PRIVATE */
png_read_filter_row_c(png_structp png_ptr, png_row_infop row_info,
png_bytep row, png_bytep prev_row, int filter);
#if defined(PNG_HAVE_ASSEMBLER_COMBINE_ROW)
/* Combines the row recently read in with the previous row.
This routine takes care of alpha and transparency if requested.
This routine also handles the two methods of progressive display
of interlaced images, depending on the mask value.
The mask value describes which pixels are to be combined with
the row. The pattern always repeats every 8 pixels, so just 8
bits are needed. A one indicates the pixel is to be combined; a
zero indicates the pixel is to be skipped. This is in addition
to any alpha or transparency value associated with the pixel.
If you want all pixels to be combined, pass 0xff (255) in mask. */
/* Use this routine for the x86 platform - it uses a faster MMX routine
if the machine supports MMX. */
void /* PRIVATE */
png_combine_row(png_structp png_ptr, png_bytep row, int mask)
{
png_debug(1,"in png_combine_row_asm\n");
if (mmx_supported == 2)
mmx_supported = mmxsupport();
/*
fprintf(stderr, "GRR DEBUG: png_combine_row() pixel_depth = %d, mask = 0x%02x, unmask = 0x%02x\n", png_ptr->row_info.pixel_depth, mask, ~mask);
fflush(stderr);
*/
if (mask == 0xff)
{
png_memcpy(row, png_ptr->row_buf + 1,
(png_size_t)((png_ptr->width * png_ptr->row_info.pixel_depth + 7) >> 3));
}
/* GRR: add "else if (mask == 0)" case?
* or does png_combine_row() not even get called in that case? */
else
{
switch (png_ptr->row_info.pixel_depth)
{
case 1: // png_ptr->row_info.pixel_depth
{
png_bytep sp;
png_bytep dp;
int s_inc, s_start, s_end;
int m;
int shift;
png_uint_32 i;
sp = png_ptr->row_buf + 1;
dp = row;
m = 0x80;
#if defined(PNG_READ_PACKSWAP_SUPPORTED)
if (png_ptr->transformations & PNG_PACKSWAP)
{
s_start = 0;
s_end = 7;
s_inc = 1;
}
else
#endif
{
s_start = 7;
s_end = 0;
s_inc = -1;
}
shift = s_start;
for (i = 0; i < png_ptr->width; i++)
{
if (m & mask)
{
int value;
value = (*sp >> shift) & 0x1;
*dp &= (png_byte)((0x7f7f >> (7 - shift)) & 0xff);
*dp |= (png_byte)(value << shift);
}
if (shift == s_end)
{
shift = s_start;
sp++;
dp++;
}
else
shift += s_inc;
if (m == 1)
m = 0x80;
else
m >>= 1;
}
break;
}
case 2: // png_ptr->row_info.pixel_depth
{
png_bytep sp;
png_bytep dp;
int s_start, s_end, s_inc;
int m;
int shift;
png_uint_32 i;
int value;
sp = png_ptr->row_buf + 1;
dp = row;
m = 0x80;
#if defined(PNG_READ_PACKSWAP_SUPPORTED)
if (png_ptr->transformations & PNG_PACKSWAP)
{
s_start = 0;
s_end = 6;
s_inc = 2;
}
else
#endif
{
s_start = 6;
s_end = 0;
s_inc = -2;
}
shift = s_start;
for (i = 0; i < png_ptr->width; i++)
{
if (m & mask)
{
value = (*sp >> shift) & 0x3;
*dp &= (png_byte)((0x3f3f >> (6 - shift)) & 0xff);
*dp |= (png_byte)(value << shift);
}
if (shift == s_end)
{
shift = s_start;
sp++;
dp++;
}
else
shift += s_inc;
if (m == 1)
m = 0x80;
else
m >>= 1;
}
break;
}
case 4: // png_ptr->row_info.pixel_depth
{
png_bytep sp;
png_bytep dp;
int s_start, s_end, s_inc;
int m;
int shift;
png_uint_32 i;
int value;
sp = png_ptr->row_buf + 1;
dp = row;
m = 0x80;
#if defined(PNG_READ_PACKSWAP_SUPPORTED)
if (png_ptr->transformations & PNG_PACKSWAP)
{
s_start = 0;
s_end = 4;
s_inc = 4;
}
else
#endif
{
s_start = 4;
s_end = 0;
s_inc = -4;
}
shift = s_start;
for (i = 0; i < png_ptr->width; i++)
{
if (m & mask)
{
value = (*sp >> shift) & 0xf;
*dp &= (png_byte)((0xf0f >> (4 - shift)) & 0xff);
*dp |= (png_byte)(value << shift);
}
if (shift == s_end)
{
shift = s_start;
sp++;
dp++;
}
else
shift += s_inc;
if (m == 1)
m = 0x80;
else
m >>= 1;
}
break;
}
case 8: // png_ptr->row_info.pixel_depth
{
png_bytep srcptr;
png_bytep dstptr;
if (mmx_supported)
{
png_uint_32 len;
int diff;
#ifndef __PIC__
int unmask = ~mask;
#else
_unmask = ~mask; // global variable for -fPIC version
#endif
srcptr = png_ptr->row_buf + 1;
dstptr = row;
len = png_ptr->width &~7; // reduce to multiple of 8
diff = png_ptr->width & 7; // amount lost
__asm__ (
#ifdef __PIC__
"movd _unmask, %%mm7 \n\t" // load bit pattern
#else
// preload "movd unmask, %%mm7 \n\t" // (unmask is in ebx)
"movd %%ebx, %%mm7 \n\t" // load bit pattern (unmask)
#endif
"psubb %%mm6, %%mm6 \n\t" // zero mm6
"punpcklbw %%mm7, %%mm7 \n\t"
"punpcklwd %%mm7, %%mm7 \n\t"
"punpckldq %%mm7, %%mm7 \n\t" // fill reg with 8 masks
"movq _mask8_0, %%mm0 \n\t"
"pand %%mm7, %%mm0 \n\t" // nonzero if keep byte
"pcmpeqb %%mm6, %%mm0 \n\t" // zeros->1s, v versa
// preload "movl len, %%ecx \n\t" // load length of line
// preload "movl srcptr, %%esi \n\t" // load source
// preload "movl dstptr, %%edi \n\t" // load dest
"cmpl $0, %%ecx \n\t" // len == 0 ?
"je mainloop8end \n\t"
"mainloop8: \n\t"
"movq (%%esi), %%mm4 \n\t" // *srcptr
"pand %%mm0, %%mm4 \n\t"
"movq %%mm0, %%mm6 \n\t"
"pandn (%%edi), %%mm6 \n\t" // *dstptr
"por %%mm6, %%mm4 \n\t"
"movq %%mm4, (%%edi) \n\t"
"addl $8, %%esi \n\t" // inc by 8 bytes processed
"addl $8, %%edi \n\t"
"subl $8, %%ecx \n\t" // dec by 8 pixels processed
"ja mainloop8 \n\t"
"mainloop8end: \n\t"
// preload "movl diff, %%ecx \n\t" // (diff is in eax)
"movl %%eax, %%ecx \n\t"
"cmpl $0, %%ecx \n\t"
"jz end8 \n\t"
// preload "movl mask, %%edx \n\t"
"sall $24, %%edx \n\t" // make low byte, high byte
"secondloop8: \n\t"
"sall %%edx \n\t" // move high bit to CF
"jnc skip8 \n\t" // if CF = 0
"movb (%%esi), %%al \n\t"
"movb %%al, (%%edi) \n\t"
"skip8: \n\t"
"incl %%esi \n\t"
"incl %%edi \n\t"
"decl %%ecx \n\t"
"jnz secondloop8 \n\t"
"end8: \n\t"
"EMMS \n\t" // DONE
: // output regs (none)
: "S" (srcptr), // esi // input regs
"D" (dstptr), // edi
"a" (diff), // eax
#ifndef __PIC__
"b" (unmask), // ebx // Global Offset Table idx
#endif
"c" (len), // ecx
"d" (mask) // edx
: "%esi", "%edi", "%eax", // clobber list
"%ecx", "%edx"
#if 0 /* MMX regs (%mm0, etc.) not supported by gcc 2.7.2.3 or egcs 1.1 */
, "%mm0", "%mm4", "%mm6", "%mm7"
#endif
);
}
else /* mmx _not supported - Use modified C routine */
{
register png_uint_32 i;
png_uint_32 initial_val = png_pass_start[png_ptr->pass];
// png.c: png_pass_start[] = {0, 4, 0, 2, 0, 1, 0};
register int stride = png_pass_inc[png_ptr->pass];
// png.c: png_pass_inc[] = {8, 8, 4, 4, 2, 2, 1};
register int rep_bytes = png_pass_width[png_ptr->pass];
// png.c: png_pass_width[] = {8, 4, 4, 2, 2, 1, 1};
register png_uint_32 final_val = png_ptr->width;
srcptr = png_ptr->row_buf + 1 + initial_val;
dstptr = row + initial_val;
for (i = initial_val; i < final_val; i += stride)
{
png_memcpy(dstptr, srcptr, rep_bytes);
srcptr += stride;
dstptr += stride;
}
} /* end of else */
break;
} // end 8 bpp
case 16: // png_ptr->row_info.pixel_depth
{
png_bytep srcptr;
png_bytep dstptr;
if (mmx_supported)
{
png_uint_32 len;
int diff;
#ifndef __PIC__
int unmask = ~mask;
#else
_unmask = ~mask; // global variable for -fPIC version
#endif
srcptr = png_ptr->row_buf + 1;
dstptr = row;
len = png_ptr->width &~7; // reduce to multiple of 8
diff = png_ptr->width & 7; // amount lost
__asm__ (
#ifdef __PIC__
"movd _unmask, %%mm7 \n\t" // load bit pattern
#else
// preload "movd unmask, %%mm7 \n\t" // (unmask is in ebx)
"movd %%ebx, %%mm7 \n\t" // load bit pattern (unmask)
#endif
"psubb %%mm6, %%mm6 \n\t" // zero mm6
"punpcklbw %%mm7, %%mm7 \n\t"
"punpcklwd %%mm7, %%mm7 \n\t"
"punpckldq %%mm7, %%mm7 \n\t" // fill reg with 8 masks
"movq _mask16_0, %%mm0 \n\t"
"movq _mask16_1, %%mm1 \n\t"
"pand %%mm7, %%mm0 \n\t"
"pand %%mm7, %%mm1 \n\t"
"pcmpeqb %%mm6, %%mm0 \n\t"
"pcmpeqb %%mm6, %%mm1 \n\t"
// preload "movl len, %%ecx \n\t" // load length of line
// preload "movl srcptr, %%esi \n\t" // load source
// preload "movl dstptr, %%edi \n\t" // load dest
"cmpl $0, %%ecx \n\t"
"jz mainloop16end \n\t"
"mainloop16: \n\t"
"movq (%%esi), %%mm4 \n\t"
"pand %%mm0, %%mm4 \n\t"
"movq %%mm0, %%mm6 \n\t"
"movq (%%edi), %%mm7 \n\t"
"pandn %%mm7, %%mm6 \n\t"
"por %%mm6, %%mm4 \n\t"
"movq %%mm4, (%%edi) \n\t"
"movq 8(%%esi), %%mm5 \n\t"
"pand %%mm1, %%mm5 \n\t"
"movq %%mm1, %%mm7 \n\t"
"movq 8(%%edi), %%mm6 \n\t"
"pandn %%mm6, %%mm7 \n\t"
"por %%mm7, %%mm5 \n\t"
"movq %%mm5, 8(%%edi) \n\t"
"addl $16, %%esi \n\t" // inc by 16 bytes processed
"addl $16, %%edi \n\t"
"subl $8, %%ecx \n\t" // dec by 8 pixels processed
"ja mainloop16 \n\t"
"mainloop16end: \n\t"
// preload "movl diff, %%ecx \n\t" // (diff is in eax)
"movl %%eax, %%ecx \n\t"
"cmpl $0, %%ecx \n\t"
"jz end16 \n\t"
// preload "movl mask, %%edx \n\t"
"sall $24, %%edx \n\t" // make low byte, high byte
"secondloop16: \n\t"
"sall %%edx \n\t" // move high bit to CF
"jnc skip16 \n\t" // if CF = 0
"movw (%%esi), %%ax \n\t"
"movw %%ax, (%%edi) \n\t"
"skip16: \n\t"
"addl $2, %%esi \n\t"
"addl $2, %%edi \n\t"
"decl %%ecx \n\t"
"jnz secondloop16 \n\t"
"end16: \n\t"
"EMMS \n\t" // DONE
: // output regs (none)
: "S" (srcptr), // esi // input regs
"D" (dstptr), // edi
"a" (diff), // eax
#ifndef __PIC__
"b" (unmask), // ebx // Global Offset Table idx
#endif
"c" (len), // ecx
"d" (mask) // edx
: "%esi", "%edi", "%eax", // clobber list
"%ecx", "%edx"
#if 0 /* MMX regs (%mm0, etc.) not supported by gcc 2.7.2.3 or egcs 1.1 */
, "%mm0", "%mm1",
"%mm4", "%mm5", "%mm6", "%mm7"
#endif
);
}
else /* mmx _not supported - Use modified C routine */
{
register png_uint_32 i;
png_uint_32 initial_val = 2 * png_pass_start[png_ptr->pass];
// png.c: png_pass_start[] = {0, 4, 0, 2, 0, 1, 0};
register int stride = 2 * png_pass_inc[png_ptr->pass];
// png.c: png_pass_inc[] = {8, 8, 4, 4, 2, 2, 1};
register int rep_bytes = 2 * png_pass_width[png_ptr->pass];
// png.c: png_pass_width[] = {8, 4, 4, 2, 2, 1, 1};
register png_uint_32 final_val = 2 * png_ptr->width;
srcptr = png_ptr->row_buf + 1 + initial_val;
dstptr = row + initial_val;
for (i = initial_val; i < final_val; i += stride)
{
png_memcpy(dstptr, srcptr, rep_bytes);
srcptr += stride;
dstptr += stride;
}
} /* end of else */
break;
} // end 16 bpp
case 24: // png_ptr->row_info.pixel_depth
{
png_bytep srcptr;
png_bytep dstptr;
if (mmx_supported)
{
png_uint_32 len;
int diff;
#ifndef __PIC__
int unmask = ~mask;
#else
_unmask = ~mask; // global variable for -fPIC version
#endif
srcptr = png_ptr->row_buf + 1;
dstptr = row;
len = png_ptr->width &~7; // reduce to multiple of 8
diff = png_ptr->width & 7; // amount lost
__asm__ (
#ifdef __PIC__
"movd _unmask, %%mm7 \n\t" // load bit pattern
#else
// preload "movd unmask, %%mm7 \n\t" // (unmask is in ebx)
"movd %%ebx, %%mm7 \n\t" // load bit pattern (unmask)
#endif
"psubb %%mm6, %%mm6 \n\t" // zero mm6
"punpcklbw %%mm7, %%mm7 \n\t"
"punpcklwd %%mm7, %%mm7 \n\t"
"punpckldq %%mm7, %%mm7 \n\t" // fill reg with 8 masks
"movq _mask24_0, %%mm0 \n\t"
"movq _mask24_1, %%mm1 \n\t"
"movq _mask24_2, %%mm2 \n\t"
"pand %%mm7, %%mm0 \n\t"
"pand %%mm7, %%mm1 \n\t"
"pand %%mm7, %%mm2 \n\t"
"pcmpeqb %%mm6, %%mm0 \n\t"
"pcmpeqb %%mm6, %%mm1 \n\t"
"pcmpeqb %%mm6, %%mm2 \n\t"
// preload "movl len, %%ecx \n\t" // load length of line
// preload "movl srcptr, %%esi \n\t" // load source
// preload "movl dstptr, %%edi \n\t" // load dest
"cmpl $0, %%ecx \n\t"
"jz mainloop24end \n\t"
"mainloop24: \n\t"
"movq (%%esi), %%mm4 \n\t"
"pand %%mm0, %%mm4 \n\t"
"movq %%mm0, %%mm6 \n\t"
"movq (%%edi), %%mm7 \n\t"
"pandn %%mm7, %%mm6 \n\t"
"por %%mm6, %%mm4 \n\t"
"movq %%mm4, (%%edi) \n\t"
"movq 8(%%esi), %%mm5 \n\t"
"pand %%mm1, %%mm5 \n\t"
"movq %%mm1, %%mm7 \n\t"
"movq 8(%%edi), %%mm6 \n\t"
"pandn %%mm6, %%mm7 \n\t"
"por %%mm7, %%mm5 \n\t"
"movq %%mm5, 8(%%edi) \n\t"
"movq 16(%%esi), %%mm6 \n\t"
"pand %%mm2, %%mm6 \n\t"
"movq %%mm2, %%mm4 \n\t"
"movq 16(%%edi), %%mm7 \n\t"
"pandn %%mm7, %%mm4 \n\t"
"por %%mm4, %%mm6 \n\t"
"movq %%mm6, 16(%%edi) \n\t"
"addl $24, %%esi \n\t" // inc by 24 bytes processed
"addl $24, %%edi \n\t"
"subl $8, %%ecx \n\t" // dec by 8 pixels processed
"ja mainloop24 \n\t"
"mainloop24end: \n\t"
// preload "movl diff, %%ecx \n\t" // (diff is in eax)
"movl %%eax, %%ecx \n\t"
"cmpl $0, %%ecx \n\t"
"jz end24 \n\t"
// preload "movl mask, %%edx \n\t"
"sall $24, %%edx \n\t" // make low byte, high byte
"secondloop24: \n\t"
"sall %%edx \n\t" // move high bit to CF
"jnc skip24 \n\t" // if CF = 0
"movw (%%esi), %%ax \n\t"
"movw %%ax, (%%edi) \n\t"
"xorl %%eax, %%eax \n\t"
"movb 2(%%esi), %%al \n\t"
"movb %%al, 2(%%edi) \n\t"
"skip24: \n\t"
"addl $3, %%esi \n\t"
"addl $3, %%edi \n\t"
"decl %%ecx \n\t"
"jnz secondloop24 \n\t"
"end24: \n\t"
"EMMS \n\t" // DONE
: // output regs (none)
: "S" (srcptr), // esi // input regs
"D" (dstptr), // edi
"a" (diff), // eax
#ifndef __PIC__
"b" (unmask), // ebx // Global Offset Table idx
#endif
"c" (len), // ecx
"d" (mask) // edx
: "%esi", "%edi", "%eax", // clobber list
"%ecx", "%edx"
#if 0 /* MMX regs (%mm0, etc.) not supported by gcc 2.7.2.3 or egcs 1.1 */
, "%mm0", "%mm1", "%mm2",
"%mm4", "%mm5", "%mm6", "%mm7"
#endif
);
}
else /* mmx _not supported - Use modified C routine */
{
register png_uint_32 i;
png_uint_32 initial_val = 3 * png_pass_start[png_ptr->pass];
// png.c: png_pass_start[] = {0, 4, 0, 2, 0, 1, 0};
register int stride = 3 * png_pass_inc[png_ptr->pass];
// png.c: png_pass_inc[] = {8, 8, 4, 4, 2, 2, 1};
register int rep_bytes = 3 * png_pass_width[png_ptr->pass];
// png.c: png_pass_width[] = {8, 4, 4, 2, 2, 1, 1};
register png_uint_32 final_val = 3 * png_ptr->width;
srcptr = png_ptr->row_buf + 1 + initial_val;
dstptr = row + initial_val;
for (i = initial_val; i < final_val; i += stride)
{
png_memcpy(dstptr, srcptr, rep_bytes);
srcptr += stride;
dstptr += stride;
}
} /* end of else */
break;
} // end 24 bpp
case 32: // png_ptr->row_info.pixel_depth
{
png_bytep srcptr;
png_bytep dstptr;
if (mmx_supported)
{
png_uint_32 len;
int diff;
#ifndef __PIC__
int unmask = ~mask;
#else
_unmask = ~mask; // global variable for -fPIC version
#endif
srcptr = png_ptr->row_buf + 1;
dstptr = row;
len = png_ptr->width &~7; // reduce to multiple of 8
diff = png_ptr->width & 7; // amount lost
__asm__ (
#ifdef __PIC__
"movd _unmask, %%mm7 \n\t" // load bit pattern
#else
// preload "movd unmask, %%mm7 \n\t" // (unmask is in ebx)
"movd %%ebx, %%mm7 \n\t" // load bit pattern (unmask)
#endif
"psubb %%mm6, %%mm6 \n\t" // zero mm6
"punpcklbw %%mm7, %%mm7 \n\t"
"punpcklwd %%mm7, %%mm7 \n\t"
"punpckldq %%mm7, %%mm7 \n\t" // fill reg with 8 masks
"movq _mask32_0, %%mm0 \n\t"
"movq _mask32_1, %%mm1 \n\t"
"movq _mask32_2, %%mm2 \n\t"
"movq _mask32_3, %%mm3 \n\t"
"pand %%mm7, %%mm0 \n\t"
"pand %%mm7, %%mm1 \n\t"
"pand %%mm7, %%mm2 \n\t"
"pand %%mm7, %%mm3 \n\t"
"pcmpeqb %%mm6, %%mm0 \n\t"
"pcmpeqb %%mm6, %%mm1 \n\t"
"pcmpeqb %%mm6, %%mm2 \n\t"
"pcmpeqb %%mm6, %%mm3 \n\t"
// preload "movl len, %%ecx \n\t" // load length of line
// preload "movl srcptr, %%esi \n\t" // load source
// preload "movl dstptr, %%edi \n\t" // load dest
"cmpl $0, %%ecx \n\t" // lcr
"jz mainloop32end \n\t"
"mainloop32: \n\t"
"movq (%%esi), %%mm4 \n\t"
"pand %%mm0, %%mm4 \n\t"
"movq %%mm0, %%mm6 \n\t"
"movq (%%edi), %%mm7 \n\t"
"pandn %%mm7, %%mm6 \n\t"
"por %%mm6, %%mm4 \n\t"
"movq %%mm4, (%%edi) \n\t"
"movq 8(%%esi), %%mm5 \n\t"
"pand %%mm1, %%mm5 \n\t"
"movq %%mm1, %%mm7 \n\t"
"movq 8(%%edi), %%mm6 \n\t"
"pandn %%mm6, %%mm7 \n\t"
"por %%mm7, %%mm5 \n\t"
"movq %%mm5, 8(%%edi) \n\t"
"movq 16(%%esi), %%mm6 \n\t"
"pand %%mm2, %%mm6 \n\t"
"movq %%mm2, %%mm4 \n\t"
"movq 16(%%edi), %%mm7 \n\t"
"pandn %%mm7, %%mm4 \n\t"
"por %%mm4, %%mm6 \n\t"
"movq %%mm6, 16(%%edi) \n\t"
"movq 24(%%esi), %%mm7 \n\t"
"pand %%mm3, %%mm7 \n\t"
"movq %%mm3, %%mm5 \n\t"
"movq 24(%%edi), %%mm4 \n\t"
"pandn %%mm4, %%mm5 \n\t"
"por %%mm5, %%mm7 \n\t"
"movq %%mm7, 24(%%edi) \n\t"
"addl $32, %%esi \n\t" // inc by 32 bytes processed
"addl $32, %%edi \n\t"
"subl $8, %%ecx \n\t" // dec by 8 pixels processed
"ja mainloop32 \n\t"
"mainloop32end: \n\t"
// preload "movl diff, %%ecx \n\t" // (diff is in eax)
"movl %%eax, %%ecx \n\t"
"cmpl $0, %%ecx \n\t"
"jz end32 \n\t"
// preload "movl mask, %%edx \n\t"
"sall $24, %%edx \n\t" // low byte => high byte
"secondloop32: \n\t"
"sall %%edx \n\t" // move high bit to CF
"jnc skip32 \n\t" // if CF = 0
"movl (%%esi), %%eax \n\t"
"movl %%eax, (%%edi) \n\t"
"skip32: \n\t"
"addl $4, %%esi \n\t"
"addl $4, %%edi \n\t"
"decl %%ecx \n\t"
"jnz secondloop32 \n\t"
"end32: \n\t"
"EMMS \n\t" // DONE
: // output regs (none)
: "S" (srcptr), // esi // input regs
"D" (dstptr), // edi
"a" (diff), // eax
#ifndef __PIC__
"b" (unmask), // ebx // Global Offset Table idx
#endif
"c" (len), // ecx
"d" (mask) // edx
: "%esi", "%edi", "%eax", // clobber list
"%ecx", "%edx"
#if 0 /* MMX regs (%mm0, etc.) not supported by gcc 2.7.2.3 or egcs 1.1 */
, "%mm0", "%mm1", "%mm2", "%mm3",
"%mm4", "%mm5", "%mm6", "%mm7"
#endif
);
}
else /* mmx _not supported - Use modified C routine */
{
register png_uint_32 i;
png_uint_32 initial_val = 4 * png_pass_start[png_ptr->pass];
// png.c: png_pass_start[] = {0, 4, 0, 2, 0, 1, 0};
register int stride = 4 * png_pass_inc[png_ptr->pass];
// png.c: png_pass_inc[] = {8, 8, 4, 4, 2, 2, 1};
register int rep_bytes = 4 * png_pass_width[png_ptr->pass];
// png.c: png_pass_width[] = {8, 4, 4, 2, 2, 1, 1};
register png_uint_32 final_val = 4 * png_ptr->width;
srcptr = png_ptr->row_buf + 1 + initial_val;
dstptr = row + initial_val;
for (i = initial_val; i < final_val; i += stride)
{
png_memcpy(dstptr, srcptr, rep_bytes);
srcptr += stride;
dstptr += stride;
}
} /* end of else */
break;
} // end 32 bpp
case 48: // png_ptr->row_info.pixel_depth
{
png_bytep srcptr;
png_bytep dstptr;
if (mmx_supported)
{
png_uint_32 len;
int diff;
#ifndef __PIC__
int unmask = ~mask;
#else
_unmask = ~mask; // global variable for -fPIC version
#endif
srcptr = png_ptr->row_buf + 1;
dstptr = row;
len = png_ptr->width &~7; // reduce to multiple of 8
diff = png_ptr->width & 7; // amount lost
__asm__ (
#ifdef __PIC__
"movd _unmask, %%mm7 \n\t" // load bit pattern
#else
// preload "movd unmask, %%mm7 \n\t" // (unmask is in ebx)
"movd %%ebx, %%mm7 \n\t" // load bit pattern (unmask)
#endif
"psubb %%mm6, %%mm6 \n\t" // zero mm6
"punpcklbw %%mm7, %%mm7 \n\t"
"punpcklwd %%mm7, %%mm7 \n\t"
"punpckldq %%mm7, %%mm7 \n\t" // fill reg with 8 masks
"movq _mask48_0, %%mm0 \n\t"
"movq _mask48_1, %%mm1 \n\t"
"movq _mask48_2, %%mm2 \n\t"
"movq _mask48_3, %%mm3 \n\t"
"movq _mask48_4, %%mm4 \n\t"
"movq _mask48_5, %%mm5 \n\t"
"pand %%mm7, %%mm0 \n\t"
"pand %%mm7, %%mm1 \n\t"
"pand %%mm7, %%mm2 \n\t"
"pand %%mm7, %%mm3 \n\t"
"pand %%mm7, %%mm4 \n\t"
"pand %%mm7, %%mm5 \n\t"
"pcmpeqb %%mm6, %%mm0 \n\t"
"pcmpeqb %%mm6, %%mm1 \n\t"
"pcmpeqb %%mm6, %%mm2 \n\t"
"pcmpeqb %%mm6, %%mm3 \n\t"
"pcmpeqb %%mm6, %%mm4 \n\t"
"pcmpeqb %%mm6, %%mm5 \n\t"
// preload "movl len, %%ecx \n\t" // load length of line
// preload "movl srcptr, %%esi \n\t" // load source
// preload "movl dstptr, %%edi \n\t" // load dest
"cmpl $0, %%ecx \n\t"
"jz mainloop48end \n\t"
"mainloop48: \n\t"
"movq (%%esi), %%mm7 \n\t"
"pand %%mm0, %%mm7 \n\t"
"movq %%mm0, %%mm6 \n\t"
"pandn (%%edi), %%mm6 \n\t"
"por %%mm6, %%mm7 \n\t"
"movq %%mm7, (%%edi) \n\t"
"movq 8(%%esi), %%mm6 \n\t"
"pand %%mm1, %%mm6 \n\t"
"movq %%mm1, %%mm7 \n\t"
"pandn 8(%%edi), %%mm7 \n\t"
"por %%mm7, %%mm6 \n\t"
"movq %%mm6, 8(%%edi) \n\t"
"movq 16(%%esi), %%mm6 \n\t"
"pand %%mm2, %%mm6 \n\t"
"movq %%mm2, %%mm7 \n\t"
"pandn 16(%%edi), %%mm7 \n\t"
"por %%mm7, %%mm6 \n\t"
"movq %%mm6, 16(%%edi) \n\t"
"movq 24(%%esi), %%mm7 \n\t"
"pand %%mm3, %%mm7 \n\t"
"movq %%mm3, %%mm6 \n\t"
"pandn 24(%%edi), %%mm6 \n\t"
"por %%mm6, %%mm7 \n\t"
"movq %%mm7, 24(%%edi) \n\t"
"movq 32(%%esi), %%mm6 \n\t"
"pand %%mm4, %%mm6 \n\t"
"movq %%mm4, %%mm7 \n\t"
"pandn 32(%%edi), %%mm7 \n\t"
"por %%mm7, %%mm6 \n\t"
"movq %%mm6, 32(%%edi) \n\t"
"movq 40(%%esi), %%mm7 \n\t"
"pand %%mm5, %%mm7 \n\t"
"movq %%mm5, %%mm6 \n\t"
"pandn 40(%%edi), %%mm6 \n\t"
"por %%mm6, %%mm7 \n\t"
"movq %%mm7, 40(%%edi) \n\t"
"addl $48, %%esi \n\t" // inc by 48 bytes processed
"addl $48, %%edi \n\t"
"subl $8, %%ecx \n\t" // dec by 8 pixels processed
"ja mainloop48 \n\t"
"mainloop48end: \n\t"
// preload "movl diff, %%ecx \n\t" // (diff is in eax)
"movl %%eax, %%ecx \n\t"
"cmpl $0, %%ecx \n\t"
"jz end48 \n\t"
// preload "movl mask, %%edx \n\t"
"sall $24, %%edx \n\t" // make low byte, high byte
"secondloop48: \n\t"
"sall %%edx \n\t" // move high bit to CF
"jnc skip48 \n\t" // if CF = 0
"movl (%%esi), %%eax \n\t"
"movl %%eax, (%%edi) \n\t"
"skip48: \n\t"
"addl $4, %%esi \n\t"
"addl $4, %%edi \n\t"
"decl %%ecx \n\t"
"jnz secondloop48 \n\t"
"end48: \n\t"
"EMMS \n\t" // DONE
: // output regs (none)
: "S" (srcptr), // esi // input regs
"D" (dstptr), // edi
"a" (diff), // eax
#ifndef __PIC__
"b" (unmask), // ebx // Global Offset Table idx
#endif
"c" (len), // ecx
"d" (mask) // edx
: "%esi", "%edi", "%eax", // clobber list
"%ecx", "%edx"
#if 0 /* MMX regs (%mm0, etc.) not supported by gcc 2.7.2.3 or egcs 1.1 */
, "%mm0", "%mm1", "%mm2", "%mm3",
"%mm4", "%mm5", "%mm6", "%mm7"
#endif
);
}
else /* mmx _not supported - Use modified C routine */
{
register png_uint_32 i;
png_uint_32 initial_val = 6 * png_pass_start[png_ptr->pass];
// png.c: png_pass_start[] = {0, 4, 0, 2, 0, 1, 0};
register int stride = 6 * png_pass_inc[png_ptr->pass];
// png.c: png_pass_inc[] = {8, 8, 4, 4, 2, 2, 1};
register int rep_bytes = 6 * png_pass_width[png_ptr->pass];
// png.c: png_pass_width[] = {8, 4, 4, 2, 2, 1, 1};
register png_uint_32 final_val = 6 * png_ptr->width;
srcptr = png_ptr->row_buf + 1 + initial_val;
dstptr = row + initial_val;
for (i = initial_val; i < final_val; i += stride)
{
png_memcpy(dstptr, srcptr, rep_bytes);
srcptr += stride;
dstptr += stride;
}
} /* end of else */
break;
} // end 48 bpp
case 64: // png_ptr->row_info.pixel_depth
{
png_bytep srcptr;
png_bytep dstptr;
register png_uint_32 i;
png_uint_32 initial_val = 8 * png_pass_start[png_ptr->pass];
// png.c: png_pass_start[] = {0, 4, 0, 2, 0, 1, 0};
register int stride = 8 * png_pass_inc[png_ptr->pass];
// png.c: png_pass_inc[] = {8, 8, 4, 4, 2, 2, 1};
register int rep_bytes = 8 * png_pass_width[png_ptr->pass];
// png.c: png_pass_width[] = {8, 4, 4, 2, 2, 1, 1};
register png_uint_32 final_val = 8 * png_ptr->width;
srcptr = png_ptr->row_buf + 1 + initial_val;
dstptr = row + initial_val;
for (i = initial_val; i < final_val; i += stride)
{
png_memcpy(dstptr, srcptr, rep_bytes);
srcptr += stride;
dstptr += stride;
}
break;
} // end 64 bpp
default: // png_ptr->row_info.pixel_depth != 1,2,4,8,16,24,32,48,64
{
// this should never happen
fprintf(stderr,
"libpng internal error: png_ptr->row_info.pixel_depth = %d\n",
png_ptr->row_info.pixel_depth);
fflush(stderr);
break;
}
} /* end switch (png_ptr->row_info.pixel_depth) */
} /* end if (non-trivial mask) */
} /* end png_combine_row() */
#endif /* PNG_HAVE_ASSEMBLER_COMBINE_ROW */
#if defined(PNG_READ_INTERLACING_SUPPORTED)
#if defined(PNG_HAVE_ASSEMBLER_READ_INTERLACE)
/* png_do_read_interlace() is called after any 16-bit to 8-bit conversion
* has taken place. [GRR: what other steps come before and/or after?]
*/
void /* PRIVATE */
png_do_read_interlace(png_row_infop row_info, png_bytep row, int pass,
png_uint_32 transformations)
{
/*
fprintf(stderr, "GRR DEBUG: entering png_do_read_interlace()\n");
if (row == NULL) fprintf(stderr, "GRR DEBUG: row == NULL\n");
if (row_info == NULL) fprintf(stderr, "GRR DEBUG: row_info == NULL\n");
fflush(stderr);
*/
png_debug(1,"in png_do_read_interlace\n");
if (mmx_supported == 2)
mmx_supported = mmxsupport();
/*
{
fprintf(stderr, "GRR DEBUG: calling mmxsupport()\n");
fprintf(stderr, "GRR DEBUG: done with mmxsupport() (mmx_supported = %d)\n", mmx_supported);
}
*/
/*
this one happened on first row due to weirdness with mmxsupport():
if (row == NULL) fprintf(stderr, "GRR DEBUG: now row == NULL!!!\n");
row was in ebx, and even though nothing touched ebx, it still got wiped...
[weird side effect of CPUID instruction?]
if (row_info == NULL) fprintf(stderr, "GRR DEBUG: now row_info == NULL!!!\n");
*/
if (row != NULL && row_info != NULL)
{
png_uint_32 final_width;
final_width = row_info->width * png_pass_inc[pass];
/*
fprintf(stderr, "GRR DEBUG: png_do_read_interlace() row_info->width = %d, final_width = %d\n", row_info->width, final_width);
fprintf(stderr, "GRR DEBUG: png_do_read_interlace() pixel_depth = %d\n", row_info->pixel_depth);
fflush(stderr);
*/
switch (row_info->pixel_depth)
{
case 1:
{
png_bytep sp, dp;
int sshift, dshift;
int s_start, s_end, s_inc;
png_byte v;
png_uint_32 i;
int j;
sp = row + (png_size_t)((row_info->width - 1) >> 3);
dp = row + (png_size_t)((final_width - 1) >> 3);
#if defined(PNG_READ_PACKSWAP_SUPPORTED)
if (transformations & PNG_PACKSWAP)
{
sshift = (int)((row_info->width + 7) & 7);
dshift = (int)((final_width + 7) & 7);
s_start = 7;
s_end = 0;
s_inc = -1;
}
else
#endif
{
sshift = 7 - (int)((row_info->width + 7) & 7);
dshift = 7 - (int)((final_width + 7) & 7);
s_start = 0;
s_end = 7;
s_inc = 1;
}
for (i = row_info->width; i; i--)
{
v = (png_byte)((*sp >> sshift) & 0x1);
for (j = 0; j < png_pass_inc[pass]; j++)
{
*dp &= (png_byte)((0x7f7f >> (7 - dshift)) & 0xff);
*dp |= (png_byte)(v << dshift);
if (dshift == s_end)
{
dshift = s_start;
dp--;
}
else
dshift += s_inc;
}
if (sshift == s_end)
{
sshift = s_start;
sp--;
}
else
sshift += s_inc;
}
break;
}
case 2:
{
png_bytep sp, dp;
int sshift, dshift;
int s_start, s_end, s_inc;
png_uint_32 i;
sp = row + (png_size_t)((row_info->width - 1) >> 2);
dp = row + (png_size_t)((final_width - 1) >> 2);
#if defined(PNG_READ_PACKSWAP_SUPPORTED)
if (transformations & PNG_PACKSWAP)
{
sshift = (png_size_t)(((row_info->width + 3) & 3) << 1);
dshift = (png_size_t)(((final_width + 3) & 3) << 1);
s_start = 6;
s_end = 0;
s_inc = -2;
}
else
#endif
{
sshift = (png_size_t)((3 - ((row_info->width + 3) & 3)) << 1);
dshift = (png_size_t)((3 - ((final_width + 3) & 3)) << 1);
s_start = 0;
s_end = 6;
s_inc = 2;
}
for (i = row_info->width; i; i--)
{
png_byte v;
int j;
v = (png_byte)((*sp >> sshift) & 0x3);
for (j = 0; j < png_pass_inc[pass]; j++)
{
*dp &= (png_byte)((0x3f3f >> (6 - dshift)) & 0xff);
*dp |= (png_byte)(v << dshift);
if (dshift == s_end)
{
dshift = s_start;
dp--;
}
else
dshift += s_inc;
}
if (sshift == s_end)
{
sshift = s_start;
sp--;
}
else
sshift += s_inc;
}
break;
}
case 4:
{
png_bytep sp, dp;
int sshift, dshift;
int s_start, s_end, s_inc;
png_uint_32 i;
sp = row + (png_size_t)((row_info->width - 1) >> 1);
dp = row + (png_size_t)((final_width - 1) >> 1);
#if defined(PNG_READ_PACKSWAP_SUPPORTED)
if (transformations & PNG_PACKSWAP)
{
sshift = (png_size_t)(((row_info->width + 1) & 1) << 2);
dshift = (png_size_t)(((final_width + 1) & 1) << 2);
s_start = 4;
s_end = 0;
s_inc = -4;
}
else
#endif
{
sshift = (png_size_t)((1 - ((row_info->width + 1) & 1)) << 2);
dshift = (png_size_t)((1 - ((final_width + 1) & 1)) << 2);
s_start = 0;
s_end = 4;
s_inc = 4;
}
for (i = row_info->width; i; i--)
{
png_byte v;
int j;
v = (png_byte)((*sp >> sshift) & 0xf);
for (j = 0; j < png_pass_inc[pass]; j++)
{
*dp &= (png_byte)((0xf0f >> (4 - dshift)) & 0xff);
*dp |= (png_byte)(v << dshift);
if (dshift == s_end)
{
dshift = s_start;
dp--;
}
else
dshift += s_inc;
}
if (sshift == s_end)
{
sshift = s_start;
sp--;
}
else
sshift += s_inc;
}
break;
}
//====================================================================
default: // 8-bit or larger (this is where the routine is modified)
{
// static unsigned long long _const4 = 0x0000000000FFFFFFLL; no good
// static unsigned long long const4 = 0x0000000000FFFFFFLL; no good
// unsigned long long _const4 = 0x0000000000FFFFFFLL; no good
// unsigned long long const4 = 0x0000000000FFFFFFLL; no good
png_bytep sptr, dp;
png_uint_32 i;
png_size_t pixel_bytes;
int width = row_info->width;
pixel_bytes = (row_info->pixel_depth >> 3);
// point sptr at the last pixel in the pre-expanded row:
sptr = row + (width - 1) * pixel_bytes;
// point dp at the last pixel position in the expanded row:
dp = row + (final_width - 1) * pixel_bytes;
// New code by Nirav Chhatrapati - Intel Corporation
if (mmx_supported) // use MMX code if machine supports it
{
//--------------------------------------------------------------
if (pixel_bytes == 3)
{
if (((pass == 0) || (pass == 1)) && width)
{
__asm__ (
"subl $21, %%edi \n\t"
// (png_pass_inc[pass] - 1)*pixel_bytes
".loop3_pass0: \n\t"
"movd (%%esi), %%mm0 \n\t" // x x x x x 2 1 0
"pand _const4, %%mm0 \n\t" // z z z z z 2 1 0
"movq %%mm0, %%mm1 \n\t" // z z z z z 2 1 0
"psllq $16, %%mm0 \n\t" // z z z 2 1 0 z z
"movq %%mm0, %%mm2 \n\t" // z z z 2 1 0 z z
"psllq $24, %%mm0 \n\t" // 2 1 0 z z z z z
"psrlq $8, %%mm1 \n\t" // z z z z z z 2 1
"por %%mm2, %%mm0 \n\t" // 2 1 0 2 1 0 z z
"por %%mm1, %%mm0 \n\t" // 2 1 0 2 1 0 2 1
"movq %%mm0, %%mm3 \n\t" // 2 1 0 2 1 0 2 1
"psllq $16, %%mm0 \n\t" // 0 2 1 0 2 1 z z
"movq %%mm3, %%mm4 \n\t" // 2 1 0 2 1 0 2 1
"punpckhdq %%mm0, %%mm3 \n\t" // 0 2 1 0 2 1 0 2
"movq %%mm4, 16(%%edi) \n\t"
"psrlq $32, %%mm0 \n\t" // z z z z 0 2 1 0
"movq %%mm3, 8(%%edi) \n\t"
"punpckldq %%mm4, %%mm0 \n\t" // 1 0 2 1 0 2 1 0
"subl $3, %%esi \n\t"
"movq %%mm0, (%%edi) \n\t"
"subl $24, %%edi \n\t"
"decl %%ecx \n\t"
"jnz .loop3_pass0 \n\t"
"EMMS \n\t" // DONE
: // output regs (none)
: "S" (sptr), // esi // input regs
"D" (dp), // edi
"c" (width) // ecx
// doesn't work "i" (0x0000000000FFFFFFLL) // %1 (a.k.a. _const4)
: "%esi", "%edi", "%ecx" // clobber list
#if 0 /* %mm0, ..., %mm4 not supported by gcc 2.7.2.3 or egcs 1.1 */
, "%mm0", "%mm1", "%mm2", "%mm3", "%mm4"
#endif
);
}
else if (((pass == 2) || (pass == 3)) && width)
{
__asm__ (
"subl $9, %%edi \n\t"
// (png_pass_inc[pass] - 1)*pixel_bytes
".loop3_pass2: \n\t"
"movd (%%esi), %%mm0 \n\t" // x x x x x 2 1 0
"pand _const4, %%mm0 \n\t" // z z z z z 2 1 0
"movq %%mm0, %%mm1 \n\t" // z z z z z 2 1 0
"psllq $16, %%mm0 \n\t" // z z z 2 1 0 z z
"movq %%mm0, %%mm2 \n\t" // z z z 2 1 0 z z
"psllq $24, %%mm0 \n\t" // 2 1 0 z z z z z
"psrlq $8, %%mm1 \n\t" // z z z z z z 2 1
"por %%mm2, %%mm0 \n\t" // 2 1 0 2 1 0 z z
"por %%mm1, %%mm0 \n\t" // 2 1 0 2 1 0 2 1
"movq %%mm0, 4(%%edi) \n\t"
"psrlq $16, %%mm0 \n\t" // z z 2 1 0 2 1 0
"subl $3, %%esi \n\t"
"movd %%mm0, (%%edi) \n\t"
"subl $12, %%edi \n\t"
"decl %%ecx \n\t"
"jnz .loop3_pass2 \n\t"
"EMMS \n\t" // DONE
: // output regs (none)
: "S" (sptr), // esi // input regs
"D" (dp), // edi
"c" (width) // ecx
: "%esi", "%edi", "%ecx" // clobber list
#if 0 /* %mm0, ..., %mm2 not supported by gcc 2.7.2.3 or egcs 1.1 */
, "%mm0", "%mm1", "%mm2"
#endif
);
}
else if (width) /* && ((pass == 4) || (pass == 5)) */
{
int width_mmx = ((width >> 1) << 1) - 8; // GRR: huh?
if (width_mmx < 0)
width_mmx = 0;
width -= width_mmx; // 8 or 9 pix, 24 or 27 bytes
if (width_mmx)
{
// png_pass_inc[] = {8, 8, 4, 4, 2, 2, 1};
// sptr points at last pixel in pre-expanded row
// dp points at last pixel position in expanded row
__asm__ (
"subl $3, %%esi \n\t"
"subl $9, %%edi \n\t"
// (png_pass_inc[pass] + 1)*pixel_bytes
".loop3_pass4: \n\t"
"movq (%%esi), %%mm0 \n\t" // x x 5 4 3 2 1 0
"movq %%mm0, %%mm1 \n\t" // x x 5 4 3 2 1 0
"movq %%mm0, %%mm2 \n\t" // x x 5 4 3 2 1 0
"psllq $24, %%mm0 \n\t" // 4 3 2 1 0 z z z
"pand _const4, %%mm1 \n\t" // z z z z z 2 1 0
"psrlq $24, %%mm2 \n\t" // z z z x x 5 4 3
"por %%mm1, %%mm0 \n\t" // 4 3 2 1 0 2 1 0
"movq %%mm2, %%mm3 \n\t" // z z z x x 5 4 3
"psllq $8, %%mm2 \n\t" // z z x x 5 4 3 z
"movq %%mm0, (%%edi) \n\t"
"psrlq $16, %%mm3 \n\t" // z z z z z x x 5
"pand _const6, %%mm3 \n\t" // z z z z z z z 5
"por %%mm3, %%mm2 \n\t" // z z x x 5 4 3 5
"subl $6, %%esi \n\t"
"movd %%mm2, 8(%%edi) \n\t"
"subl $12, %%edi \n\t"
"subl $2, %%ecx \n\t"
"jnz .loop3_pass4 \n\t"
"EMMS \n\t" // DONE
: // output regs (none)
: "S" (sptr), // esi // input regs
"D" (dp), // edi
"c" (width_mmx) // ecx
: "%esi", "%edi", "%ecx" // clobber list
#if 0 /* %mm0, ..., %mm3 not supported by gcc 2.7.2.3 or egcs 1.1 */
, "%mm0", "%mm1", "%mm2", "%mm3"
#endif
);
}
sptr -= width_mmx*3;
dp -= width_mmx*6;
for (i = width; i; i--)
{
png_byte v[8];
int j;
png_memcpy(v, sptr, 3);
for (j = 0; j < png_pass_inc[pass]; j++)
{
png_memcpy(dp, v, 3);
dp -= 3;
}
sptr -= 3;
}
}
} /* end of pixel_bytes == 3 */
//--------------------------------------------------------------
else if (pixel_bytes == 1)
{
if (((pass == 0) || (pass == 1)) && width)
{
int width_mmx = ((width >> 2) << 2);
width -= width_mmx; // 0-3 pixels => 0-3 bytes
if (width_mmx)
{
__asm__ (
"subl $3, %%esi \n\t"
"subl $31, %%edi \n\t"
".loop1_pass0: \n\t"
"movd (%%esi), %%mm0 \n\t" // x x x x 3 2 1 0
"movq %%mm0, %%mm1 \n\t" // x x x x 3 2 1 0
"punpcklbw %%mm0, %%mm0 \n\t" // 3 3 2 2 1 1 0 0
"movq %%mm0, %%mm2 \n\t" // 3 3 2 2 1 1 0 0
"punpcklwd %%mm0, %%mm0 \n\t" // 1 1 1 1 0 0 0 0
"movq %%mm0, %%mm3 \n\t" // 1 1 1 1 0 0 0 0
"punpckldq %%mm0, %%mm0 \n\t" // 0 0 0 0 0 0 0 0
"punpckhdq %%mm3, %%mm3 \n\t" // 1 1 1 1 1 1 1 1
"movq %%mm0, (%%edi) \n\t"
"punpckhwd %%mm2, %%mm2 \n\t" // 3 3 3 3 2 2 2 2
"movq %%mm3, 8(%%edi) \n\t"
"movq %%mm2, %%mm4 \n\t" // 3 3 3 3 2 2 2 2
"punpckldq %%mm2, %%mm2 \n\t" // 2 2 2 2 2 2 2 2
"punpckhdq %%mm4, %%mm4 \n\t" // 3 3 3 3 3 3 3 3
"movq %%mm2, 16(%%edi) \n\t"
"subl $4, %%esi \n\t"
"movq %%mm4, 24(%%edi) \n\t"
"subl $32, %%edi \n\t"
"subl $4, %%ecx \n\t"
"jnz .loop1_pass0 \n\t"
"EMMS \n\t" // DONE
: // output regs (none)
: "S" (sptr), // esi // input regs
"D" (dp), // edi
"c" (width_mmx) // ecx
: "%esi", "%edi", "%ecx" // clobber list
#if 0 /* %mm0, ..., %mm4 not supported by gcc 2.7.2.3 or egcs 1.1 */
, "%mm0", "%mm1", "%mm2", "%mm3", "%mm4"
#endif
);
}
sptr -= width_mmx;
dp -= width_mmx*8;
for (i = width; i; i--)
{
int j;
/* I simplified this part in version 1.0.4e
* here and in several other instances where
* pixel_bytes == 1 -- GR-P
*
* Original code:
*
* png_byte v[8];
* png_memcpy(v, sptr, pixel_bytes);
* for (j = 0; j < png_pass_inc[pass]; j++)
* {
* png_memcpy(dp, v, pixel_bytes);
* dp -= pixel_bytes;
* }
* sptr -= pixel_bytes;
*
* Replacement code is in the next three lines:
*/
for (j = 0; j < png_pass_inc[pass]; j++)
*dp-- = *sptr;
--sptr;
}
}
else if (((pass == 2) || (pass == 3)) && width)
{
int width_mmx = ((width >> 2) << 2);
width -= width_mmx; // 0-3 pixels => 0-3 bytes
if (width_mmx)
{
__asm__ (
"subl $3, %%esi \n\t"
"subl $15, %%edi \n\t"
".loop1_pass2: \n\t"
"movd (%%esi), %%mm0 \n\t" // x x x x 3 2 1 0
"punpcklbw %%mm0, %%mm0 \n\t" // 3 3 2 2 1 1 0 0
"movq %%mm0, %%mm1 \n\t" // 3 3 2 2 1 1 0 0
"punpcklwd %%mm0, %%mm0 \n\t" // 1 1 1 1 0 0 0 0
"punpckhwd %%mm1, %%mm1 \n\t" // 3 3 3 3 2 2 2 2
"movq %%mm0, (%%edi) \n\t"
"subl $4, %%esi \n\t"
"movq %%mm1, 8(%%edi) \n\t"
"subl $16, %%edi \n\t"
"subl $4, %%ecx \n\t"
"jnz .loop1_pass2 \n\t"
"EMMS \n\t" // DONE
: // output regs (none)
: "S" (sptr), // esi // input regs
"D" (dp), // edi
"c" (width_mmx) // ecx
: "%esi", "%edi", "%ecx" // clobber list
#if 0 /* %mm0, %mm1 not supported by gcc 2.7.2.3 or egcs 1.1 */
, "%mm0", "%mm1"
#endif
);
}
sptr -= width_mmx;
dp -= width_mmx*4;
for (i = width; i; i--)
{
int j;
for (j = 0; j < png_pass_inc[pass]; j++)
*dp-- = *sptr;
--sptr;
}
}
else if (width) /* && ((pass == 4) || (pass == 5)) */
{
int width_mmx = ((width >> 3) << 3);
width -= width_mmx; // 0-3 pixels => 0-3 bytes
if (width_mmx)
{
__asm__ (
"subl $7, %%esi \n\t"
"subl $15, %%edi \n\t"
".loop1_pass4: \n\t"
"movq (%%esi), %%mm0 \n\t" // 7 6 5 4 3 2 1 0
"movq %%mm0, %%mm1 \n\t" // 7 6 5 4 3 2 1 0
"punpcklbw %%mm0, %%mm0 \n\t" // 3 3 2 2 1 1 0 0
"punpckhbw %%mm1, %%mm1 \n\t" // 7 7 6 6 5 5 4 4
"movq %%mm1, 8(%%edi) \n\t"
"subl $8, %%esi \n\t"
"movq %%mm0, (%%edi) \n\t"
"subl $16, %%edi \n\t"
"subl $8, %%ecx \n\t"
"jnz .loop1_pass4 \n\t"
"EMMS \n\t" // DONE
: // output regs (none)
: "S" (sptr), // esi // input regs
"D" (dp), // edi
"c" (width_mmx) // ecx
: "%esi", "%edi", "%ecx" // clobber list
#if 0 /* %mm0, %mm1 not supported by gcc 2.7.2.3 or egcs 1.1 */
, "%mm0", "%mm1"
#endif
);
}
sptr -= width_mmx;
dp -= width_mmx*2;
for (i = width; i; i--)
{
int j;
for (j = 0; j < png_pass_inc[pass]; j++)
*dp-- = *sptr;
--sptr;
}
}
} /* end of pixel_bytes == 1 */
//--------------------------------------------------------------
else if (pixel_bytes == 2)
{
if (((pass == 0) || (pass == 1)) && width)
{
int width_mmx = ((width >> 1) << 1);
width -= width_mmx; // 0,1 pixels => 0,2 bytes
if (width_mmx)
{
__asm__ (
"subl $2, %%esi \n\t"
"subl $30, %%edi \n\t"
".loop2_pass0: \n\t"
"movd (%%esi), %%mm0 \n\t" // x x x x 3 2 1 0
"punpcklwd %%mm0, %%mm0 \n\t" // 3 2 3 2 1 0 1 0
"movq %%mm0, %%mm1 \n\t" // 3 2 3 2 1 0 1 0
"punpckldq %%mm0, %%mm0 \n\t" // 1 0 1 0 1 0 1 0
"punpckhdq %%mm1, %%mm1 \n\t" // 3 2 3 2 3 2 3 2
"movq %%mm0, (%%edi) \n\t"
"movq %%mm0, 8(%%edi) \n\t"
"movq %%mm1, 16(%%edi) \n\t"
"subl $4, %%esi \n\t"
"movq %%mm1, 24(%%edi) \n\t"
"subl $32, %%edi \n\t"
"subl $2, %%ecx \n\t"
"jnz .loop2_pass0 \n\t"
"EMMS \n\t" // DONE
: // output regs (none)
: "S" (sptr), // esi // input regs
"D" (dp), // edi
"c" (width_mmx) // ecx
: "%esi", "%edi", "%ecx" // clobber list
#if 0 /* %mm0, %mm1 not supported by gcc 2.7.2.3 or egcs 1.1 */
, "%mm0", "%mm1"
#endif
);
}
sptr -= (width_mmx*2 - 2); // sign fixed
dp -= (width_mmx*16 - 2); // sign fixed
for (i = width; i; i--)
{
png_byte v[8];
int j;
sptr -= 2;
png_memcpy(v, sptr, 2);
for (j = 0; j < png_pass_inc[pass]; j++)
{
dp -= 2;
png_memcpy(dp, v, 2);
}
}
}
else if (((pass == 2) || (pass == 3)) && width)
{
int width_mmx = ((width >> 1) << 1) ;
width -= width_mmx; // 0,1 pixels => 0,2 bytes
if (width_mmx)
{
__asm__ (
"subl $2, %%esi \n\t"
"subl $14, %%edi \n\t"
".loop2_pass2: \n\t"
"movd (%%esi), %%mm0 \n\t" // x x x x 3 2 1 0
"punpcklwd %%mm0, %%mm0 \n\t" // 3 2 3 2 1 0 1 0
"movq %%mm0, %%mm1 \n\t" // 3 2 3 2 1 0 1 0
"punpckldq %%mm0, %%mm0 \n\t" // 1 0 1 0 1 0 1 0
"punpckhdq %%mm1, %%mm1 \n\t" // 3 2 3 2 3 2 3 2
"movq %%mm0, (%%edi) \n\t"
"subl $4, %%esi \n\t"
"movq %%mm1, 8(%%edi) \n\t"
"subl $16, %%edi \n\t"
"subl $2, %%ecx \n\t"
"jnz .loop2_pass2 \n\t"
"EMMS \n\t" // DONE
: // output regs (none)
: "S" (sptr), // esi // input regs
"D" (dp), // edi
"c" (width_mmx) // ecx
: "%esi", "%edi", "%ecx" // clobber list
#if 0 /* %mm0, %mm1 not supported by gcc 2.7.2.3 or egcs 1.1 */
, "%mm0", "%mm1"
#endif
);
}
sptr -= (width_mmx*2 - 2); // sign fixed
dp -= (width_mmx*8 - 2); // sign fixed
for (i = width; i; i--)
{
png_byte v[8];
int j;
sptr -= 2;
png_memcpy(v, sptr, 2);
for (j = 0; j < png_pass_inc[pass]; j++)
{
dp -= 2;
png_memcpy(dp, v, 2);
}
}
}
else if (width) // pass == 4 or 5
{
int width_mmx = ((width >> 1) << 1) ;
width -= width_mmx; // 0,1 pixels => 0,2 bytes
if (width_mmx)
{
__asm__ (
"subl $2, %%esi \n\t"
"subl $6, %%edi \n\t"
".loop2_pass4: \n\t"
"movd (%%esi), %%mm0 \n\t" // x x x x 3 2 1 0
"punpcklwd %%mm0, %%mm0 \n\t" // 3 2 3 2 1 0 1 0
"subl $4, %%esi \n\t"
"movq %%mm0, (%%edi) \n\t"
"subl $8, %%edi \n\t"
"subl $2, %%ecx \n\t"
"jnz .loop2_pass4 \n\t"
"EMMS \n\t" // DONE
: // output regs (none)
: "S" (sptr), // esi // input regs
"D" (dp), // edi
"c" (width_mmx) // ecx
: "%esi", "%edi", "%ecx" // clobber list
#if 0 /* %mm0 not supported by gcc 2.7.2.3 or egcs 1.1 */
, "%mm0"
#endif
);
}
sptr -= (width_mmx*2 - 2); // sign fixed
dp -= (width_mmx*4 - 2); // sign fixed
for (i = width; i; i--)
{
png_byte v[8];
int j;
sptr -= 2;
png_memcpy(v, sptr, 2);
for (j = 0; j < png_pass_inc[pass]; j++)
{
dp -= 2;
png_memcpy(dp, v, 2);
}
}
}
} /* end of pixel_bytes == 2 */
//--------------------------------------------------------------
else if (pixel_bytes == 4)
{
if (((pass == 0) || (pass == 1)) && width)
{
int width_mmx = ((width >> 1) << 1);
width -= width_mmx; // 0,1 pixels => 0,4 bytes
/*
fprintf(stderr, "GRR DEBUG: png_do_read_interlace() pass = %d, width_mmx = %d, width = %d\n", pass, width_mmx, width);
fprintf(stderr, " sptr = 0x%08lx, dp = 0x%08lx\n", (unsigned long)sptr, (unsigned long)dp);
fflush(stderr);
*/
if (width_mmx)
{
#ifdef GRR_DEBUG
FILE *junk = fopen("junk.4bytes", "wb");
if (junk)
fclose(junk);
#endif /* GRR_DEBUG */
__asm__ (
"subl $4, %%esi \n\t"
"subl $60, %%edi \n\t"
".loop4_pass0: \n\t"
"movq (%%esi), %%mm0 \n\t" // 7 6 5 4 3 2 1 0
"movq %%mm0, %%mm1 \n\t" // 7 6 5 4 3 2 1 0
"punpckldq %%mm0, %%mm0 \n\t" // 3 2 1 0 3 2 1 0
"punpckhdq %%mm1, %%mm1 \n\t" // 7 6 5 4 7 6 5 4
"movq %%mm0, (%%edi) \n\t"
"movq %%mm0, 8(%%edi) \n\t"
"movq %%mm0, 16(%%edi) \n\t"
"movq %%mm0, 24(%%edi) \n\t"
"movq %%mm1, 32(%%edi) \n\t"
"movq %%mm1, 40(%%edi) \n\t"
"movq %%mm1, 48(%%edi) \n\t"
"subl $8, %%esi \n\t"
"movq %%mm1, 56(%%edi) \n\t"
"subl $64, %%edi \n\t"
"subl $2, %%ecx \n\t"
"jnz .loop4_pass0 \n\t"
"EMMS \n\t" // DONE
: // output regs (none)
: "S" (sptr), // esi // input regs
"D" (dp), // edi
"c" (width_mmx) // ecx
: "%esi", "%edi", "%ecx" // clobber list
#if 0 /* %mm0, %mm1 not supported by gcc 2.7.2.3 or egcs 1.1 */
, "%mm0", "%mm1"
#endif
);
}
sptr -= (width_mmx*4 - 4); // sign fixed
dp -= (width_mmx*32 - 4); // sign fixed
for (i = width; i; i--)
{
png_byte v[8];
int j;
sptr -= 4;
png_memcpy(v, sptr, 4);
for (j = 0; j < png_pass_inc[pass]; j++)
{
dp -= 4;
png_memcpy(dp, v, 4);
}
}
}
else if (((pass == 2) || (pass == 3)) && width)
{
int width_mmx = ((width >> 1) << 1);
width -= width_mmx; // 0,1 pixels => 0,4 bytes
if (width_mmx)
{
__asm__ (
"subl $4, %%esi \n\t"
"subl $28, %%edi \n\t"
".loop4_pass2: \n\t"
"movq (%%esi), %%mm0 \n\t" // 7 6 5 4 3 2 1 0
"movq %%mm0, %%mm1 \n\t" // 7 6 5 4 3 2 1 0
"punpckldq %%mm0, %%mm0 \n\t" // 3 2 1 0 3 2 1 0
"punpckhdq %%mm1, %%mm1 \n\t" // 7 6 5 4 7 6 5 4
"movq %%mm0, (%%edi) \n\t"
"movq %%mm0, 8(%%edi) \n\t"
"movq %%mm1, 16(%%edi) \n\t"
"movq %%mm1, 24(%%edi) \n\t"
"subl $8, %%esi \n\t"
"subl $32, %%edi \n\t"
"subl $2, %%ecx \n\t"
"jnz .loop4_pass2 \n\t"
"EMMS \n\t" // DONE
: // output regs (none)
: "S" (sptr), // esi // input regs
"D" (dp), // edi
"c" (width_mmx) // ecx
: "%esi", "%edi", "%ecx" // clobber list
#if 0 /* %mm0, %mm1 not supported by gcc 2.7.2.3 or egcs 1.1 */
, "%mm0", "%mm1"
#endif
);
}
sptr -= (width_mmx*4 - 4); // sign fixed
dp -= (width_mmx*16 - 4); // sign fixed
for (i = width; i; i--)
{
png_byte v[8];
int j;
sptr -= 4;
png_memcpy(v, sptr, 4);
for (j = 0; j < png_pass_inc[pass]; j++)
{
dp -= 4;
png_memcpy(dp, v, 4);
}
}
}
else if (width) // pass == 4 or 5
{
int width_mmx = ((width >> 1) << 1) ;
width -= width_mmx; // 0,1 pixels => 0,4 bytes
if (width_mmx)
{
__asm__ (
"subl $4, %%esi \n\t"
"subl $12, %%edi \n\t"
".loop4_pass4: \n\t"
"movq (%%esi), %%mm0 \n\t" // 7 6 5 4 3 2 1 0
"movq %%mm0, %%mm1 \n\t" // 7 6 5 4 3 2 1 0
"punpckldq %%mm0, %%mm0 \n\t" // 3 2 1 0 3 2 1 0
"punpckhdq %%mm1, %%mm1 \n\t" // 7 6 5 4 7 6 5 4
"movq %%mm0, (%%edi) \n\t"
"subl $8, %%esi \n\t"
"movq %%mm1, 8(%%edi) \n\t"
"subl $16, %%edi \n\t"
"subl $2, %%ecx \n\t"
"jnz .loop4_pass4 \n\t"
"EMMS \n\t" // DONE
: // output regs (none)
: "S" (sptr), // esi // input regs
"D" (dp), // edi
"c" (width_mmx) // ecx
: "%esi", "%edi", "%ecx" // clobber list
#if 0 /* %mm0, %mm1 not supported by gcc 2.7.2.3 or egcs 1.1 */
, "%mm0", "%mm1"
#endif
);
}
sptr -= (width_mmx*4 - 4); // sign fixed
dp -= (width_mmx*8 - 4); // sign fixed
for (i = width; i; i--)
{
png_byte v[8];
int j;
sptr -= 4;
png_memcpy(v, sptr, 4);
for (j = 0; j < png_pass_inc[pass]; j++)
{
dp -= 4;
png_memcpy(dp, v, 4);
}
}
}
} /* end of pixel_bytes == 4 */
#define STILL_WORKING_ON_THIS
#ifdef STILL_WORKING_ON_THIS // GRR: should work, but needs testing
// (special 64-bit version of rpng2)
//--------------------------------------------------------------
else if (pixel_bytes == 8)
{
// GRR NOTE: no need to combine passes here!
if (((pass == 0) || (pass == 1)) && width)
{
// source is 8-byte RRGGBBAA
// dest is 64-byte RRGGBBAA RRGGBBAA RRGGBBAA RRGGBBAA ...
#ifdef GRR_DEBUG
FILE *junk = fopen("junk.8bytes", "wb");
if (junk)
fclose(junk);
#endif /* GRR_DEBUG */
__asm__ (
"subl $56, %%edi \n\t" // start of last block
".loop8_pass0: \n\t"
"movq (%%esi), %%mm0 \n\t" // 7 6 5 4 3 2 1 0
"movq %%mm0, (%%edi) \n\t"
"movq %%mm0, 8(%%edi) \n\t"
"movq %%mm0, 16(%%edi) \n\t"
"movq %%mm0, 24(%%edi) \n\t"
"movq %%mm0, 32(%%edi) \n\t"
"movq %%mm0, 40(%%edi) \n\t"
"movq %%mm0, 48(%%edi) \n\t"
"subl $8, %%esi \n\t"
"movq %%mm0, 56(%%edi) \n\t"
"subl $64, %%edi \n\t"
"decl %%ecx \n\t"
"jnz .loop8_pass0 \n\t"
"EMMS \n\t" // DONE
: // output regs (none)
: "S" (sptr), // esi // input regs
"D" (dp), // edi
"c" (width) // ecx
: "%esi", "%edi", "%ecx" // clobber list
#if 0 /* %mm0 not supported by gcc 2.7.2.3 or egcs 1.1 */
, "%mm0"
#endif
);
}
else if (((pass == 2) || (pass == 3)) && width)
{
// source is 8-byte RRGGBBAA
// dest is 32-byte RRGGBBAA RRGGBBAA RRGGBBAA RRGGBBAA
int width_mmx = ((width >> 1) << 1) ;
width -= width_mmx;
if (width_mmx)
{
__asm__ (
"subl $24, %%edi \n\t" // start of last block
".loop8_pass2: \n\t"
"movq (%%esi), %%mm0 \n\t" // 7 6 5 4 3 2 1 0
"movq %%mm0, (%%edi) \n\t"
"movq %%mm0, 8(%%edi) \n\t"
"movq %%mm0, 16(%%edi) \n\t"
"subl $8, %%esi \n\t"
"movq %%mm0, 24(%%edi) \n\t"
"subl $32, %%edi \n\t"
"decl %%ecx \n\t"
"jnz .loop8_pass2 \n\t"
"EMMS \n\t" // DONE
: // output regs (none)
: "S" (sptr), // esi // input regs
"D" (dp), // edi
"c" (width) // ecx
: "%esi", "%edi", "%ecx" // clobber list
#if 0 /* %mm0 not supported by gcc 2.7.2.3 or egcs 1.1 */
, "%mm0"
#endif
);
}
}
else if (width) // pass == 4 or 5
{
// source is 8-byte RRGGBBAA
// dest is 16-byte RRGGBBAA RRGGBBAA
int width_mmx = ((width >> 1) << 1) ;
width -= width_mmx;
if (width_mmx)
{
__asm__ (
"subl $8, %%edi \n\t" // start of last block
".loop8_pass4: \n\t"
"movq (%%esi), %%mm0 \n\t" // 7 6 5 4 3 2 1 0
"movq %%mm0, (%%edi) \n\t"
"subl $8, %%esi \n\t"
"movq %%mm0, 8(%%edi) \n\t"
"subl $16, %%edi \n\t"
"decl %%ecx \n\t"
"jnz .loop8_pass4 \n\t"
"EMMS \n\t" // DONE
: // output regs (none)
: "S" (sptr), // esi // input regs
"D" (dp), // edi
"c" (width) // ecx
: "%esi", "%edi", "%ecx" // clobber list
#if 0 /* %mm0 not supported by gcc 2.7.2.3 or egcs 1.1 */
, "%mm0"
#endif
);
}
}
} /* end of pixel_bytes == 8 */
#endif /* STILL_WORKING_ON_THIS */
//--------------------------------------------------------------
else if (pixel_bytes == 6)
{
for (i = width; i; i--)
{
png_byte v[8];
int j;
png_memcpy(v, sptr, 6);
for (j = 0; j < png_pass_inc[pass]; j++)
{
png_memcpy(dp, v, 6);
dp -= 6;
}
sptr -= 6;
}
} /* end of pixel_bytes == 6 */
//--------------------------------------------------------------
else
{
for (i = width; i; i--)
{
png_byte v[8];
int j;
png_memcpy(v, sptr, pixel_bytes);
for (j = 0; j < png_pass_inc[pass]; j++)
{
png_memcpy(dp, v, pixel_bytes);
dp -= pixel_bytes;
}
sptr-= pixel_bytes;
}
}
} // end of mmx_supported =========================================
else /* MMX not supported: use modified C code - takes advantage
* of inlining of memcpy for a constant */
/* GRR 19991007: does it? or should pixel_bytes in each
* block be replaced with immediate value (e.g., 1)? */
/* GRR 19991017: replaced with constants in each case */
{
if (pixel_bytes == 1)
{
for (i = width; i; i--)
{
int j;
for (j = 0; j < png_pass_inc[pass]; j++)
*dp-- = *sptr;
--sptr;
}
}
else if (pixel_bytes == 3)
{
for (i = width; i; i--)
{
png_byte v[8];
int j;
png_memcpy(v, sptr, 3);
for (j = 0; j < png_pass_inc[pass]; j++)
{
png_memcpy(dp, v, 3);
dp -= 3;
}
sptr -= 3;
}
}
else if (pixel_bytes == 2)
{
for (i = width; i; i--)
{
png_byte v[8];
int j;
png_memcpy(v, sptr, 2);
for (j = 0; j < png_pass_inc[pass]; j++)
{
png_memcpy(dp, v, 2);
dp -= 2;
}
sptr -= 2;
}
}
else if (pixel_bytes == 4)
{
for (i = width; i; i--)
{
png_byte v[8];
int j;
png_memcpy(v, sptr, 4);
for (j = 0; j < png_pass_inc[pass]; j++)
{
png_memcpy(dp, v, 4);
dp -= 4;
}
sptr -= 4;
}
}
else if (pixel_bytes == 6)
{
for (i = width; i; i--)
{
png_byte v[8];
int j;
png_memcpy(v, sptr, 6);
for (j = 0; j < png_pass_inc[pass]; j++)
{
png_memcpy(dp, v, 6);
dp -= 6;
}
sptr -= 6;
}
}
else if (pixel_bytes == 8)
{
for (i = width; i; i--)
{
png_byte v[8];
int j;
png_memcpy(v, sptr, 8);
for (j = 0; j < png_pass_inc[pass]; j++)
{
png_memcpy(dp, v, 8);
dp -= 8;
}
sptr -= 8;
}
}
else // GRR: should never be reached
{
for (i = width; i; i--)
{
png_byte v[8];
int j;
png_memcpy(v, sptr, pixel_bytes);
for (j = 0; j < png_pass_inc[pass]; j++)
{
png_memcpy(dp, v, pixel_bytes);
dp -= pixel_bytes;
}
sptr -= pixel_bytes;
}
}
} /* end if (MMX not supported) */
break;
}
} /* end switch (row_info->pixel_depth) */
row_info->width = final_width;
row_info->rowbytes = ((final_width *
(png_uint_32)row_info->pixel_depth + 7) >> 3);
}
} /* end png_do_read_interlace() */
#endif /* PNG_HAVE_ASSEMBLER_READ_INTERLACE */
#endif /* PNG_READ_INTERLACING_SUPPORTED */
// These variables are utilized in the functions below. They are declared
// globally here to ensure alignment on 8-byte boundaries.
union uAll {
long long use;
double align;
} LBCarryMask = {0x0101010101010101LL},
HBClearMask = {0x7f7f7f7f7f7f7f7fLL},
ActiveMask, ActiveMask2, ActiveMaskEnd, ShiftBpp, ShiftRem;
// Optimized code for PNG Average filter decoder
void /* PRIVATE */
png_read_filter_row_mmx_avg(png_row_infop row_info, png_bytep row,
png_bytep prev_row)
{
int bpp;
// int diff; GRR: global now (shortened to dif/_dif)
bpp = (row_info->pixel_depth + 7) >> 3; // Get # bytes per pixel
_FullLength = row_info->rowbytes; // # of bytes to filter
__asm__ (
// Init address pointers and offset
//GRR "movl row, %%edi \n\t" // edi ==> Avg(x)
"xorl %%ebx, %%ebx \n\t" // ebx ==> x
"movl %%edi, %%edx \n\t"
//GRR "movl prev_row, %%esi \n\t" // esi ==> Prior(x)
//GRR "subl bpp, %%edx \n\t" // (bpp is preloaded into ecx)
"subl %%ecx, %%edx \n\t" // edx ==> Raw(x-bpp)
"xorl %%eax,%%eax \n\t"
// Compute the Raw value for the first bpp bytes
// Raw(x) = Avg(x) + (Prior(x)/2)
"avg_rlp: \n\t"
"movb (%%esi,%%ebx,),%%al \n\t" // Load al with Prior(x)
"incl %%ebx \n\t"
"shrb %%al \n\t" // divide by 2
"addb -1(%%edi,%%ebx,),%%al \n\t" // add Avg(x); -1 to offset inc ebx
//GRR "cmpl bpp, %%ebx \n\t" // (bpp is preloaded into ecx)
"cmpl %%ecx, %%ebx \n\t"
"movb %%al,-1(%%edi,%%ebx,) \n\t" // write Raw(x); -1 to offset inc ebx
"jb avg_rlp \n\t" // mov does not affect flags
// get # of bytes to alignment
"movl %%edi, _dif \n\t" // take start of row
"addl %%ebx, _dif \n\t" // add bpp
"addl $0xf, _dif \n\t" // add 7+8 to incr past alignment bdry
"andl $0xfffffff8, _dif \n\t" // mask to alignment boundary
"subl %%edi, _dif \n\t" // subtract from start => value ebx at alignment
"jz avg_go \n\t"
// fix alignment
// Compute the Raw value for the bytes up to the alignment boundary
// Raw(x) = Avg(x) + ((Raw(x-bpp) + Prior(x))/2)
"xorl %%ecx, %%ecx \n\t"
"avg_lp1: \n\t"
"xorl %%eax, %%eax \n\t"
"movb (%%esi,%%ebx,), %%cl \n\t" // load cl with Prior(x)
"movb (%%edx,%%ebx,), %%al \n\t" // load al with Raw(x-bpp)
"addw %%cx, %%ax \n\t"
"incl %%ebx \n\t"
"shrw %%ax \n\t" // divide by 2
"addb -1(%%edi,%%ebx,), %%al \n\t" // add Avg(x); -1 to offset inc ebx
"cmpl _dif, %%ebx \n\t" // check if at alignment boundary
"movb %%al, -1(%%edi,%%ebx,) \n\t" // write Raw(x); -1 to offset inc ebx
"jb avg_lp1 \n\t" // repeat until at alignment boundary
"avg_go: \n\t"
"movl _FullLength, %%eax \n\t"
"movl %%eax, %%ecx \n\t"
"subl %%ebx, %%eax \n\t" // subtract alignment fix
"andl $0x00000007, %%eax \n\t" // calc bytes over mult of 8
"subl %%eax, %%ecx \n\t" // drop over bytes from original length
"movl %%ecx, _MMXLength \n\t"
: // output regs/vars here, e.g., "=m" (_MMXLength) instead of final instr
: "S" (prev_row), // esi // input regs
"D" (row), // edi
"c" (bpp) // ecx
: "%eax", "%ebx", "%ecx", // clobber list
"%edx", "%edi", "%esi"
// GRR: INCLUDE "memory" as clobbered? (_dif, _MMXLength) PROBABLY
);
#ifdef GRR_GCC_MMX_CONVERTED
// Now do the math for the rest of the row
switch ( bpp )
{
case 3:
{
ActiveMask.use = 0x0000000000ffffff;
ShiftBpp.use = 24; // == 3 * 8
ShiftRem.use = 40; // == 64 - 24
__asm__ (
// Re-init address pointers and offset
"movq $ActiveMask, %%mm7 \n\t"
"movl _dif, %%ebx \n\t" // ebx ==> x = offset to alignment boundary
"movq $LBCarryMask, %%mm5 \n\t"
"movl row, %%edi \n\t" // edi ==> Avg(x)
"movq $HBClearMask, %%mm4 \n\t"
"movl prev_row, %%esi \n\t" // esi ==> Prior(x)
// PRIME the pump (load the first Raw(x-bpp) data set)
"movq -8(%%edi,%%ebx,), %%mm2 \n\t" // Load previous aligned 8 bytes
// (we correct position in loop below)
"avg_3lp: \n\t"
"movq (%%edi,%%ebx,), %%mm0 \n\t" // Load mm0 with Avg(x)
// Add (Prev_row/2) to Average
"movq %%mm5, %%mm3 \n\t"
"psrlq $ShiftRem, %%mm2 \n\t" // Correct position Raw(x-bpp) data
"movq (%%esi,%%ebx,), %%mm1 \n\t" // Load mm1 with Prior(x)
"movq %%mm7, %%mm6 \n\t"
"pand %%mm1, %%mm3 \n\t" // get lsb for each prev_row byte
"psrlq $1, %%mm1 \n\t" // divide prev_row bytes by 2
"pand %%mm4, %%mm1 \n\t" // clear invalid bit 7 of each byte
"paddb %%mm1, %%mm0 \n\t" // add (Prev_row/2) to Avg for each byte
// Add 1st active group (Raw(x-bpp)/2) to Average with LBCarry
"movq %%mm3, %%mm1 \n\t" // now use mm1 for getting LBCarrys
"pand %%mm2, %%mm1 \n\t" // get LBCarrys for each byte where both
// lsb's were == 1 (Only valid for active group)
"psrlq $1, %%mm2 \n\t" // divide raw bytes by 2
"pand %%mm4, %%mm2 \n\t" // clear invalid bit 7 of each byte
"paddb %%mm1, %%mm2 \n\t" // add LBCarrys to (Raw(x-bpp)/2) for each byte
"pand %%mm6, %%mm2 \n\t" // Leave only Active Group 1 bytes to add to Avg
"paddb %%mm2, %%mm0 \n\t" // add (Raw/2) + LBCarrys to Avg for each Active
// byte
// Add 2nd active group (Raw(x-bpp)/2) to Average with LBCarry
"psllq $ShiftBpp, %%mm6 \n\t" // shift the mm6 mask to cover bytes 3-5
"movq %%mm0, %%mm2 \n\t" // mov updated Raws to mm2
"psllq $ShiftBpp, %%mm2 \n\t" // shift data to position correctly
"movq %%mm3, %%mm1 \n\t" // now use mm1 for getting LBCarrys
"pand %%mm2, %%mm1 \n\t" // get LBCarrys for each byte where both
// lsb's were == 1 (Only valid for active group)
"psrlq $1, %%mm2 \n\t" // divide raw bytes by 2
"pand %%mm4, %%mm2 \n\t" // clear invalid bit 7 of each byte
"paddb %%mm1, %%mm2 \n\t" // add LBCarrys to (Raw(x-bpp)/2) for each byte
"pand %%mm6, %%mm2 \n\t" // Leave only Active Group 2 bytes to add to Avg
"paddb %%mm2, %%mm0 \n\t" // add (Raw/2) + LBCarrys to Avg for each Active
// byte
// Add 3rd active group (Raw(x-bpp)/2) to Average with LBCarry
"psllq $ShiftBpp, %%mm6 \n\t" // shift the mm6 mask to cover the last two
// bytes
"movq %%mm0, %%mm2 \n\t" // mov updated Raws to mm2
"psllq $ShiftBpp, %%mm2 \n\t" // shift data to position correctly
// Data only needs to be shifted once here to
// get the correct x-bpp offset.
"movq %%mm3, %%mm1 \n\t" // now use mm1 for getting LBCarrys
"pand %%mm2, %%mm1 \n\t" // get LBCarrys for each byte where both
// lsb's were == 1 (Only valid for active group)
"psrlq $1, %%mm2 \n\t" // divide raw bytes by 2
"pand %%mm4, %%mm2 \n\t" // clear invalid bit 7 of each byte
"paddb %%mm1, %%mm2 \n\t" // add LBCarrys to (Raw(x-bpp)/2) for each byte
"pand %%mm6, %%mm2 \n\t" // Leave only Active Group 2 bytes to add to Avg
"addl $8, %%ebx \n\t"
"paddb %%mm2, %%mm0 \n\t" // add (Raw/2) + LBCarrys to Avg for each Active
// byte
// Now ready to write back to memory
"movq %%mm0, -8(%%edi,%%ebx,) \n\t"
// Move updated Raw(x) to use as Raw(x-bpp) for next loop
"cmpl _MMXLength, %%ebx \n\t"
"movq %%mm0, %%mm2 \n\t" // mov updated Raw(x) to mm2
"jb avg_3lp \n\t"
: // output regs/vars go here, e.g.: "=m" (memory_var)
: "S" (prev_row), // esi // input regs
"D" (row) // edi
: "%ebx", "%edi", "%esi" // clobber list
// GRR: INCLUDE "memory" as clobbered? (_dif, _MMXLength) PROBABLY
// , "%mm0", "%mm1", "%mm2", "%mm3",
// "%mm4", "%mm5", "%mm6", "%mm7"
);
}
break; // end 3 bpp
case 6:
case 4:
//case 7: // who wrote this? PNG doesn't support 5 or 7 bytes/pixel
//case 5:
{
ActiveMask.use = 0xffffffffffffffff; // use shift below to clear
// appropriate inactive bytes
ShiftBpp.use = bpp << 3;
ShiftRem.use = 64 - ShiftBpp.use;
__asm__ (
"movq $HBClearMask, %%mm4 \n\t"
// Re-init address pointers and offset
"movl _dif, %%ebx \n\t" // ebx ==> x = offset to alignment boundary
// Load ActiveMask and clear all bytes except for 1st active group
"movq $ActiveMask, %%mm7 \n\t"
"movl row, %%edi \n\t" // edi ==> Avg(x)
"psrlq $ShiftRem, %%mm7 \n\t"
"movl prev_row, %%esi \n\t" // esi ==> Prior(x)
"movq %%mm7, %%mm6 \n\t"
"movq $LBCarryMask, %%mm5 \n\t"
"psllq $ShiftBpp, %%mm6 \n\t" // Create mask for 2nd active group
// PRIME the pump (load the first Raw(x-bpp) data set
"movq -8(%%edi,%%ebx,), %%mm2 \n\t" // Load previous aligned 8 bytes
// (we correct position in loop below)
"avg_4lp: \n\t"
"movq (%%edi,%%ebx,), %%mm0 \n\t"
"psrlq $ShiftRem, %%mm2 \n\t" // shift data to position correctly
"movq (%%esi,%%ebx,), %%mm1 \n\t"
// Add (Prev_row/2) to Average
"movq %%mm5, %%mm3 \n\t"
"pand %%mm1, %%mm3 \n\t" // get lsb for each prev_row byte
"psrlq $1, %%mm1 \n\t" // divide prev_row bytes by 2
"pand %%mm4, %%mm1 \n\t" // clear invalid bit 7 of each byte
"paddb %%mm1, %%mm0 \n\t" // add (Prev_row/2) to Avg for each byte
// Add 1st active group (Raw(x-bpp)/2) to Average with LBCarry
"movq %%mm3, %%mm1 \n\t" // now use mm1 for getting LBCarrys
"pand %%mm2, %%mm1 \n\t" // get LBCarrys for each byte where both
// lsb's were == 1 (Only valid for active group)
"psrlq $1, %%mm2 \n\t" // divide raw bytes by 2
"pand %%mm4, %%mm2 \n\t" // clear invalid bit 7 of each byte
"paddb %%mm1, %%mm2 \n\t" // add LBCarrys to (Raw(x-bpp)/2) for each byte
"pand %%mm7, %%mm2 \n\t" // Leave only Active Group 1 bytes to add to Avg
"paddb %%mm2, %%mm0 \n\t" // add (Raw/2) + LBCarrys to Avg for each Active
// byte
// Add 2nd active group (Raw(x-bpp)/2) to Average with LBCarry
"movq %%mm0, %%mm2 \n\t" // mov updated Raws to mm2
"psllq $ShiftBpp, %%mm2 \n\t" // shift data to position correctly
"addl $8, %%ebx \n\t"
"movq %%mm3, %%mm1 \n\t" // now use mm1 for getting LBCarrys
"pand %%mm2, %%mm1 \n\t" // get LBCarrys for each byte where both
// lsb's were == 1 (Only valid for active group)
"psrlq $1, %%mm2 \n\t" // divide raw bytes by 2
"pand %%mm4, %%mm2 \n\t" // clear invalid bit 7 of each byte
"paddb %%mm1, %%mm2 \n\t" // add LBCarrys to (Raw(x-bpp)/2) for each byte
"pand %%mm6, %%mm2 \n\t" // Leave only Active Group 2 bytes to add to Avg
"paddb %%mm2, %%mm0 \n\t" // add (Raw/2) + LBCarrys to Avg for each Active
// byte
"cmpl _MMXLength, %%ebx \n\t"
// Now ready to write back to memory
"movq %%mm0, -8(%%edi,%%ebx,) \n\t"
// Prep Raw(x-bpp) for next loop
"movq %%mm0, %%mm2 \n\t" // mov updated Raws to mm2
"jb avg_4lp \n\t"
: // FIXASM: output regs/vars go here, e.g.: "=m" (memory_var)
: // FIXASM: input regs, e.g.: "c" (count), "S" (src), "D" (dest)
: "%ebx", "%edi", "%esi", "%mm0", "%mm1", "%mm2", "%mm3", "%mm4", "%mm5", "%mm6", "%mm7" // CHECKASM: clobber list
);
}
break; // end 4,6 bpp
case 2:
{
ActiveMask.use = 0x000000000000ffff;
ShiftBpp.use = 24; // == 3 * 8
ShiftRem.use = 40; // == 64 - 24
__asm__ (
// Load ActiveMask
"movq $ActiveMask, %%mm7 \n\t"
// Re-init address pointers and offset
"movl _dif, %%ebx \n\t" // ebx ==> x = offset to alignment boundary
"movq $LBCarryMask, %%mm5 \n\t"
"movl row, %%edi \n\t" // edi ==> Avg(x)
"movq $HBClearMask, %%mm4 \n\t"
"movl prev_row, %%esi \n\t" // esi ==> Prior(x)
// PRIME the pump (load the first Raw(x-bpp) data set
"movq -8(%%edi,%%ebx,), %%mm2 \n\t" // Load previous aligned 8 bytes
// (we correct position in loop below)
"avg_2lp: \n\t"
"movq (%%edi,%%ebx,), %%mm0 \n\t"
"psllq $ShiftRem, %%mm2 \n\t" // shift data to position correctly
"movq (%%esi,%%ebx,), %%mm1 \n\t"
// Add (Prev_row/2) to Average
"movq %%mm5, %%mm3 \n\t"
"pand %%mm1, %%mm3 \n\t" // get lsb for each prev_row byte
"psrlq $1, %%mm1 \n\t" // divide prev_row bytes by 2
"pand %%mm4, %%mm1 \n\t" // clear invalid bit 7 of each byte
"movq %%mm7, %%mm6 \n\t"
"paddb %%mm1, %%mm0 \n\t" // add (Prev_row/2) to Avg for each byte
// Add 1st active group (Raw(x-bpp)/2) to Average with LBCarry
"movq %%mm3, %%mm1 \n\t" // now use mm1 for getting LBCarrys
"pand %%mm2, %%mm1 \n\t" // get LBCarrys for each byte where both
// lsb's were == 1 (Only valid for active group)
"psrlq $1, %%mm2 \n\t" // divide raw bytes by 2
"pand %%mm4, %%mm2 \n\t" // clear invalid bit 7 of each byte
"paddb %%mm1, %%mm2 \n\t" // add LBCarrys to (Raw(x-bpp)/2) for each byte
"pand %%mm6, %%mm2 \n\t" // Leave only Active Group 1 bytes to add to Avg
"paddb %%mm2, %%mm0 \n\t" // add (Raw/2) + LBCarrys to Avg for each Active byte
// Add 2nd active group (Raw(x-bpp)/2) to Average with LBCarry
"psllq $ShiftBpp, %%mm6 \n\t" // shift the mm6 mask to cover bytes 2 & 3
"movq %%mm0, %%mm2 \n\t" // mov updated Raws to mm2
"psllq $ShiftBpp, %%mm2 \n\t" // shift data to position correctly
"movq %%mm3, %%mm1 \n\t" // now use mm1 for getting LBCarrys
"pand %%mm2, %%mm1 \n\t" // get LBCarrys for each byte where both
// lsb's were == 1 (Only valid for active group)
"psrlq $1, %%mm2 \n\t" // divide raw bytes by 2
"pand %%mm4, %%mm2 \n\t" // clear invalid bit 7 of each byte
"paddb %%mm1, %%mm2 \n\t" // add LBCarrys to (Raw(x-bpp)/2) for each byte
"pand %%mm6, %%mm2 \n\t" // Leave only Active Group 2 bytes to add to Avg
"paddb %%mm2, %%mm0 \n\t" // add (Raw/2) + LBCarrys to Avg for each Active byte
// Add rdd active group (Raw(x-bpp)/2) to Average with LBCarry
"psllq $ShiftBpp, %%mm6 \n\t" // shift the mm6 mask to cover bytes 4 & 5
"movq %%mm0, %%mm2 \n\t" // mov updated Raws to mm2
"psllq $ShiftBpp, %%mm2 \n\t" // shift data to position correctly
// Data only needs to be shifted once here to
// get the correct x-bpp offset.
"movq %%mm3, %%mm1 \n\t" // now use mm1 for getting LBCarrys
"pand %%mm2, %%mm1 \n\t" // get LBCarrys for each byte where both
// lsb's were == 1 (Only valid for active group)
"psrlq $1, %%mm2 \n\t" // divide raw bytes by 2
"pand %%mm4, %%mm2 \n\t" // clear invalid bit 7 of each byte
"paddb %%mm1, %%mm2 \n\t" // add LBCarrys to (Raw(x-bpp)/2) for each byte
"pand %%mm6, %%mm2 \n\t" // Leave only Active Group 2 bytes to add to Avg
"paddb %%mm2, %%mm0 \n\t" // add (Raw/2) + LBCarrys to Avg for each Active byte
// Add 4th active group (Raw(x-bpp)/2) to Average with LBCarry
"psllq $ShiftBpp, %%mm6 \n\t" // shift the mm6 mask to cover bytes 6 & 7
"movq %%mm0, %%mm2 \n\t" // mov updated Raws to mm2
"psllq $ShiftBpp, %%mm2 \n\t" // shift data to position correctly
// Data only needs to be shifted once here to
// get the correct x-bpp offset.
"addl $8, %%ebx \n\t"
"movq %%mm3, %%mm1 \n\t" // now use mm1 for getting LBCarrys
"pand %%mm2, %%mm1 \n\t" // get LBCarrys for each byte where both
// lsb's were == 1 (Only valid for active group)
"psrlq $1, %%mm2 \n\t" // divide raw bytes by 2
"pand %%mm4, %%mm2 \n\t" // clear invalid bit 7 of each byte
"paddb %%mm1, %%mm2 \n\t" // add LBCarrys to (Raw(x-bpp)/2) for each byte
"pand %%mm6, %%mm2 \n\t" // Leave only Active Group 2 bytes to add to Avg
"paddb %%mm2, %%mm0 \n\t" // add (Raw/2) + LBCarrys to Avg for each Active byte
"cmpl _MMXLength, %%ebx \n\t"
// Now ready to write back to memory
"movq %%mm0, -8(%%edi,%%ebx,) \n\t"
// Prep Raw(x-bpp) for next loop
"movq %%mm0, %%mm2 \n\t" // mov updated Raws to mm2
"jb avg_2lp \n\t"
: // FIXASM: output regs/vars go here, e.g.: "=m" (memory_var)
: // FIXASM: input regs, e.g.: "c" (count), "S" (src), "D" (dest)
: "%ebx", "%edi", "%esi", "%mm0", "%mm1", "%mm2", "%mm3", "%mm4", "%mm5", "%mm6", "%mm7" // CHECKASM: clobber list
);
}
break; // end 2 bpp
case 1:
{
__asm__ (
// Re-init address pointers and offset
"movl _dif, %%ebx \n\t" // ebx ==> x = offset to alignment boundary
"movl row, %%edi \n\t" // edi ==> Avg(x)
"cmpl _FullLength, %%ebx \n\t" // Test if offset at end of array
"jnb avg_1end \n\t"
// Do Paeth decode for remaining bytes
"movl prev_row, %%esi \n\t" // esi ==> Prior(x)
"movl %%edi, %%edx \n\t"
"xorl %%ecx, %%ecx \n\t" // zero ecx before using cl & cx in loop below
"subl bpp, %%edx \n\t" // edx ==> Raw(x-bpp)
"avg_1lp: \n\t"
// Raw(x) = Avg(x) + ((Raw(x-bpp) + Prior(x))/2)
"xorl %%eax, %%eax \n\t"
"movb (%%esi,%%ebx,), %%cl \n\t" // load cl with Prior(x)
"movb (%%edx,%%ebx,), %%al \n\t" // load al with Raw(x-bpp)
"addw %%cx, %%ax \n\t"
"incl %%ebx \n\t"
"shrw %%ax \n\t" // divide by 2
"addb -1(%%edi,%%ebx,), %%al \n\t" // Add Avg(x); -1 to offset inc ebx
"cmpl _FullLength, %%ebx \n\t" // Check if at end of array
"movb %%al, -1(%%edi,%%ebx,) \n\t" // Write back Raw(x);
// mov does not affect flags; -1 to offset inc ebx
"jb avg_1lp \n\t"
"avg_1end: \n\t"
: // FIXASM: output regs/vars go here, e.g.: "=m" (memory_var)
: // FIXASM: input regs, e.g.: "c" (count), "S" (src), "D" (dest)
: "%eax", "%ebx", "%ecx", "%edx", "%edi", "%esi" // CHECKASM: clobber list
);
}
return; // end 1 bpp
case 8:
{
__asm__ (
// Re-init address pointers and offset
"movl _dif, %%ebx \n\t" // ebx ==> x = offset to alignment boundary
"movq $LBCarryMask, %%mm5 \n\t"
"movl row, %%edi \n\t" // edi ==> Avg(x)
"movq $HBClearMask, %%mm4 \n\t"
"movl prev_row, %%esi \n\t" // esi ==> Prior(x)
// PRIME the pump (load the first Raw(x-bpp) data set
"movq -8(%%edi,%%ebx,), %%mm2 \n\t" // Load previous aligned 8 bytes
// (NO NEED to correct position in loop below)
"avg_8lp: \n\t"
"movq (%%edi,%%ebx,), %%mm0 \n\t"
"movq %%mm5, %%mm3 \n\t"
"movq (%%esi,%%ebx,), %%mm1 \n\t"
"addl $8, %%ebx \n\t"
"pand %%mm1, %%mm3 \n\t" // get lsb for each prev_row byte
"psrlq $1, %%mm1 \n\t" // divide prev_row bytes by 2
"pand %%mm2, %%mm3 \n\t" // get LBCarrys for each byte where both
// lsb's were == 1
"psrlq $1, %%mm2 \n\t" // divide raw bytes by 2
"pand %%mm4, %%mm1 \n\t" // clear invalid bit 7 of each byte
"paddb %%mm3, %%mm0 \n\t" // add LBCarrys to Avg for each byte
"pand %%mm4, %%mm2 \n\t" // clear invalid bit 7 of each byte
"paddb %%mm1, %%mm0 \n\t" // add (Prev_row/2) to Avg for each byte
"paddb %%mm2, %%mm0 \n\t" // add (Raw/2) to Avg for each byte
"cmpl _MMXLength, %%ebx \n\t"
"movq %%mm0, -8(%%edi,%%ebx,) \n\t"
"movq %%mm0, %%mm2 \n\t" // reuse as Raw(x-bpp)
"jb avg_8lp \n\t"
: // FIXASM: output regs/vars go here, e.g.: "=m" (memory_var)
: // FIXASM: input regs, e.g.: "c" (count), "S" (src), "D" (dest)
: "%ebx", "%edi", "%esi", "%mm0", "%mm1", "%mm2", "%mm3", "%mm4", "%mm5" // CHECKASM: clobber list
);
}
break; // end 8 bpp
default: // bpp greater than 8 (!= 1,2,3,4,6,8)
{
GRR: PRINT ERROR HERE: SHOULD NEVER BE REACHED (unless smaller than 1?)
__asm__ (
"movq $LBCarryMask, %%mm5 \n\t"
// Re-init address pointers and offset
"movl _dif, %%ebx \n\t" // ebx ==> x = offset to alignment boundary
"movl row, %%edi \n\t" // edi ==> Avg(x)
"movq $HBClearMask, %%mm4 \n\t"
"movl %%edi, %%edx \n\t"
"movl prev_row, %%esi \n\t" // esi ==> Prior(x)
"subl bpp, %%edx \n\t" // edx ==> Raw(x-bpp)
"avg_Alp: \n\t"
"movq (%%edi,%%ebx,), %%mm0 \n\t"
"movq %%mm5, %%mm3 \n\t"
"movq (%%esi,%%ebx,), %%mm1 \n\t"
"pand %%mm1, %%mm3 \n\t" // get lsb for each prev_row byte
"movq (%%edx,%%ebx,), %%mm2 \n\t"
"psrlq $1, %%mm1 \n\t" // divide prev_row bytes by 2
"pand %%mm2, %%mm3 \n\t" // get LBCarrys for each byte where both
// lsb's were == 1
"psrlq $1, %%mm2 \n\t" // divide raw bytes by 2
"pand %%mm4, %%mm1 \n\t" // clear invalid bit 7 of each byte
"paddb %%mm3, %%mm0 \n\t" // add LBCarrys to Avg for each byte
"pand %%mm4, %%mm2 \n\t" // clear invalid bit 7 of each byte
"paddb %%mm1, %%mm0 \n\t" // add (Prev_row/2) to Avg for each byte
"addl $8, %%ebx \n\t"
"paddb %%mm2, %%mm0 \n\t" // add (Raw/2) to Avg for each byte
"cmpl _MMXLength, %%ebx \n\t"
"movq %%mm0, -8(%%edi,%%ebx,) \n\t"
"jb avg_Alp \n\t"
: // FIXASM: output regs/vars go here, e.g.: "=m" (memory_var)
: // FIXASM: input regs, e.g.: "c" (count), "S" (src), "D" (dest)
: "%ebx", "%edx", "%edi", "%esi", "%mm0", "%mm1", "%mm2", "%mm3", "%mm4", "%mm5" // CHECKASM: clobber list
);
}
break;
} // end switch ( bpp )
__asm__ (
// MMX acceleration complete now do clean-up
// Check if any remaining bytes left to decode
"movl _MMXLength, %%ebx \n\t" // ebx ==> x = offset bytes remaining after MMX
"movl row, %%edi \n\t" // edi ==> Avg(x)
"cmpl _FullLength, %%ebx \n\t" // Test if offset at end of array
"jnb avg_end \n\t"
// Do Paeth decode for remaining bytes
"movl prev_row, %%esi \n\t" // esi ==> Prior(x)
"movl %%edi, %%edx \n\t"
"xorl %%ecx, %%ecx \n\t" // zero ecx before using cl & cx in loop below
"subl bpp, %%edx \n\t" // edx ==> Raw(x-bpp)
"avg_lp2: \n\t"
// Raw(x) = Avg(x) + ((Raw(x-bpp) + Prior(x))/2)
"xorl %%eax, %%eax \n\t"
"movb (%%esi,%%ebx,), %%cl \n\t" // load cl with Prior(x)
"movb (%%edx,%%ebx,), %%al \n\t" // load al with Raw(x-bpp)
"addw %%cx, %%ax \n\t"
"incl %%ebx \n\t"
"shrw %%ax \n\t" // divide by 2
"addb -1(%%edi,%%ebx,), %%al \n\t" // Add Avg(x); -1 to offset inc ebx
"cmpl _FullLength, %%ebx \n\t" // Check if at end of array
"movb %%al, -1(%%edi,%%ebx,) \n\t" // Write back Raw(x);
// mov does not affect flags; -1 to offset inc ebx
"jb avg_lp2 \n\t"
"avg_end: \n\t"
"emms \n\t" // End MMX instructions; prep for possible FP instrs.
: // FIXASM: output regs/vars go here, e.g.: "=m" (memory_var)
: // FIXASM: input regs, e.g.: "c" (count), "S" (src), "D" (dest)
: "%eax", "%ebx", "%ecx", "%edx", "%edi", "%esi" // CHECKASM: clobber list
);
#endif /* GRR_GCC_MMX_CONVERTED */
}
// Optimized code for PNG Paeth filter decoder
void /* PRIVATE */
png_read_filter_row_mmx_paeth(png_row_infop row_info, png_bytep row,
png_bytep prev_row)
{
#ifdef GRR_GCC_MMX_CONVERTED
int bpp;
int patemp, pbtemp, pctemp;
bpp = (row_info->pixel_depth + 7) >> 3; // Get # bytes per pixel
_FullLength = row_info->rowbytes; // # of bytes to filter
__asm__ (
"xorl %%ebx, %%ebx \n\t" // ebx ==> x offset
"movl row, %%edi \n\t"
"xorl %%edx, %%edx \n\t" // edx ==> x-bpp offset
"movl prev_row, %%esi \n\t"
"xorl %%eax, %%eax \n\t"
// Compute the Raw value for the first bpp bytes
// Note: the formula works out to be always
// Paeth(x) = Raw(x) + Prior(x) where x < bpp
"paeth_rlp: \n\t"
"movb (%%edi,%%ebx,), %%al \n\t"
"addb (%%esi,%%ebx,), %%al \n\t"
"incl %%ebx \n\t"
"cmpl bpp, %%ebx \n\t"
"movb %%al, -1(%%edi,%%ebx,) \n\t"
"jb paeth_rlp \n\t"
// get # of bytes to alignment
"movl %%edi, _dif \n\t" // take start of row
"addl %%ebx, _dif \n\t" // add bpp
"xorl %%ecx, %%ecx \n\t"
"addl $0xf, _dif \n\t" // add 7 + 8 to incr past alignment boundary
"andl $0xfffffff8, _dif \n\t" // mask to alignment boundary
"subl %%edi, _dif \n\t" // subtract from start ==> value ebx at alignment
"jz paeth_go \n\t"
// fix alignment
"paeth_lp1: \n\t"
"xorl %%eax, %%eax \n\t"
// pav = p - a = (a + b - c) - a = b - c
"movb (%%esi,%%ebx,), %%al \n\t" // load Prior(x) into al
"movb (%%esi,%%edx,), %%cl \n\t" // load Prior(x-bpp) into cl
"subl %%ecx, %%eax \n\t" // subtract Prior(x-bpp)
"movl %%eax, patemp \n\t" // Save pav for later use
"xorl %%eax, %%eax \n\t"
// pbv = p - b = (a + b - c) - b = a - c
"movb (%%edi,%%edx,), %%al \n\t" // load Raw(x-bpp) into al
"subl %%ecx, %%eax \n\t" // subtract Prior(x-bpp)
"movl %%eax, %%ecx \n\t"
// pcv = p - c = (a + b - c) -c = (a - c) + (b - c) = pav + pbv
"addl patemp, %%eax \n\t" // pcv = pav + pbv
// pc = abs(pcv)
"testl $0x80000000, %%eax \n\t"
"jz paeth_pca \n\t"
"negl %%eax \n\t" // reverse sign of neg values
"paeth_pca: \n\t"
"movl %%eax, pctemp \n\t" // save pc for later use
// pb = abs(pbv)
"testl $0x80000000, %%ecx \n\t"
"jz paeth_pba \n\t"
"negl %%ecx \n\t" // reverse sign of neg values
"paeth_pba: \n\t"
"movl %%ecx, pbtemp \n\t" // save pb for later use
// pa = abs(pav)
"movl patemp, %%eax \n\t"
"testl $0x80000000, %%eax \n\t"
"jz paeth_paa \n\t"
"negl %%eax \n\t" // reverse sign of neg values
"paeth_paa: \n\t"
"movl %%eax, patemp \n\t" // save pa for later use
// test if pa <= pb
"cmpl %%ecx, %%eax \n\t"
"jna paeth_abb \n\t"
// pa > pb; now test if pb <= pc
"cmpl pctemp, %%ecx \n\t"
"jna paeth_bbc \n\t"
// pb > pc; Raw(x) = Paeth(x) + Prior(x-bpp)
"movb (%%esi,%%edx,), %%cl \n\t" // load Prior(x-bpp) into cl
"jmp paeth_paeth \n\t"
"paeth_bbc: \n\t"
// pb <= pc; Raw(x) = Paeth(x) + Prior(x)
"movb (%%esi,%%ebx,), %%cl \n\t" // load Prior(x) into cl
"jmp paeth_paeth \n\t"
"paeth_abb: \n\t"
// pa <= pb; now test if pa <= pc
"cmpl pctemp, %%eax \n\t"
"jna paeth_abc \n\t"
// pa > pc; Raw(x) = Paeth(x) + Prior(x-bpp)
"movb (%%esi,%%edx,), %%cl \n\t" // load Prior(x-bpp) into cl
"jmp paeth_paeth \n\t"
"paeth_abc: \n\t"
// pa <= pc; Raw(x) = Paeth(x) + Raw(x-bpp)
"movb (%%edi,%%edx,), %%cl \n\t" // load Raw(x-bpp) into cl
"paeth_paeth: \n\t"
"incl %%ebx \n\t"
"incl %%edx \n\t"
// Raw(x) = (Paeth(x) + Paeth_Predictor( a, b, c )) mod 256
"addb %%cl, -1(%%edi,%%ebx,) \n\t"
"cmpl _dif, %%ebx \n\t"
"jb paeth_lp1 \n\t"
"paeth_go: \n\t"
"movl _FullLength, %%ecx \n\t"
"movl %%ecx, %%eax \n\t"
"subl %%ebx, %%eax \n\t" // subtract alignment fix
"andl $0x00000007, %%eax \n\t" // calc bytes over mult of 8
"subl %%eax, %%ecx \n\t" // drop over bytes from original length
"movl %%ecx, _MMXLength \n\t"
: // FIXASM: output regs/vars go here, e.g.: "=m" (memory_var)
: // FIXASM: input regs, e.g.: "c" (count), "S" (src), "D" (dest)
: "%eax", "%ebx", "%ecx", "%edx", "%edi", "%esi" // CHECKASM: clobber list
);
// Now do the math for the rest of the row
switch ( bpp )
{
case 3:
{
ActiveMask.use = 0x0000000000ffffff;
ActiveMaskEnd.use = 0xffff000000000000;
ShiftBpp.use = 24; // == bpp(3) * 8
ShiftRem.use = 40; // == 64 - 24
__asm__ (
"movl _dif, %%ebx \n\t"
"movl row, %%edi \n\t"
"movl prev_row, %%esi \n\t"
"pxor %%mm0, %%mm0 \n\t"
// PRIME the pump (load the first Raw(x-bpp) data set
"movq -8(%%edi,%%ebx,), %%mm1 \n\t"
"paeth_3lp: \n\t"
"psrlq $ShiftRem, %%mm1 \n\t" // shift last 3 bytes to 1st 3 bytes
"movq (%%esi,%%ebx,), %%mm2 \n\t" // load b=Prior(x)
"punpcklbw %%mm0, %%mm1 \n\t" // Unpack High bytes of a
"movq -8(%%esi,%%ebx,), %%mm3 \n\t" // Prep c=Prior(x-bpp) bytes
"punpcklbw %%mm0, %%mm2 \n\t" // Unpack High bytes of b
"psrlq $ShiftRem, %%mm3 \n\t" // shift last 3 bytes to 1st 3 bytes
// pav = p - a = (a + b - c) - a = b - c
"movq %%mm2, %%mm4 \n\t"
"punpcklbw %%mm0, %%mm3 \n\t" // Unpack High bytes of c
// pbv = p - b = (a + b - c) - b = a - c
"movq %%mm1, %%mm5 \n\t"
"psubw %%mm3, %%mm4 \n\t"
"pxor %%mm7, %%mm7 \n\t"
// pcv = p - c = (a + b - c) -c = (a - c) + (b - c) = pav + pbv
"movq %%mm4, %%mm6 \n\t"
"psubw %%mm3, %%mm5 \n\t"
// pa = abs(p-a) = abs(pav)
// pb = abs(p-b) = abs(pbv)
// pc = abs(p-c) = abs(pcv)
"pcmpgtw %%mm4, %%mm0 \n\t" // Create mask pav bytes < 0
"paddw %%mm5, %%mm6 \n\t"
"pand %%mm4, %%mm0 \n\t" // Only pav bytes < 0 in mm7
"pcmpgtw %%mm5, %%mm7 \n\t" // Create mask pbv bytes < 0
"psubw %%mm0, %%mm4 \n\t"
"pand %%mm5, %%mm7 \n\t" // Only pbv bytes < 0 in mm0
"psubw %%mm0, %%mm4 \n\t"
"psubw %%mm7, %%mm5 \n\t"
"pxor %%mm0, %%mm0 \n\t"
"pcmpgtw %%mm6, %%mm0 \n\t" // Create mask pcv bytes < 0
"pand %%mm6, %%mm0 \n\t" // Only pav bytes < 0 in mm7
"psubw %%mm7, %%mm5 \n\t"
"psubw %%mm0, %%mm6 \n\t"
// test pa <= pb
"movq %%mm4, %%mm7 \n\t"
"psubw %%mm0, %%mm6 \n\t"
"pcmpgtw %%mm5, %%mm7 \n\t" // pa > pb?
"movq %%mm7, %%mm0 \n\t"
// use mm7 mask to merge pa & pb
"pand %%mm7, %%mm5 \n\t"
// use mm0 mask copy to merge a & b
"pand %%mm0, %%mm2 \n\t"
"pandn %%mm4, %%mm7 \n\t"
"pandn %%mm1, %%mm0 \n\t"
"paddw %%mm5, %%mm7 \n\t"
"paddw %%mm2, %%mm0 \n\t"
// test ((pa <= pb)? pa:pb) <= pc
"pcmpgtw %%mm6, %%mm7 \n\t" // pab > pc?
"pxor %%mm1, %%mm1 \n\t"
"pand %%mm7, %%mm3 \n\t"
"pandn %%mm0, %%mm7 \n\t"
"paddw %%mm3, %%mm7 \n\t"
"pxor %%mm0, %%mm0 \n\t"
"packuswb %%mm1, %%mm7 \n\t"
"movq (%%esi,%%ebx,), %%mm3 \n\t" // load c=Prior(x-bpp)
"pand $ActiveMask, %%mm7 \n\t"
"movq %%mm3, %%mm2 \n\t" // load b=Prior(x) step 1
"paddb (%%edi,%%ebx,), %%mm7 \n\t" // add Paeth predictor with Raw(x)
"punpcklbw %%mm0, %%mm3 \n\t" // Unpack High bytes of c
"movq %%mm7, (%%edi,%%ebx,) \n\t" // write back updated value
"movq %%mm7, %%mm1 \n\t" // Now mm1 will be used as Raw(x-bpp)
// Now do Paeth for 2nd set of bytes (3-5)
"psrlq $ShiftBpp, %%mm2 \n\t" // load b=Prior(x) step 2
"punpcklbw %%mm0, %%mm1 \n\t" // Unpack High bytes of a
"pxor %%mm7, %%mm7 \n\t"
"punpcklbw %%mm0, %%mm2 \n\t" // Unpack High bytes of b
// pbv = p - b = (a + b - c) - b = a - c
"movq %%mm1, %%mm5 \n\t"
// pav = p - a = (a + b - c) - a = b - c
"movq %%mm2, %%mm4 \n\t"
"psubw %%mm3, %%mm5 \n\t"
"psubw %%mm3, %%mm4 \n\t"
// pcv = p - c = (a + b - c) -c = (a - c) + (b - c) =
// pav + pbv = pbv + pav
"movq %%mm5, %%mm6 \n\t"
"paddw %%mm4, %%mm6 \n\t"
// pa = abs(p-a) = abs(pav)
// pb = abs(p-b) = abs(pbv)
// pc = abs(p-c) = abs(pcv)
"pcmpgtw %%mm5, %%mm0 \n\t" // Create mask pbv bytes < 0
"pcmpgtw %%mm4, %%mm7 \n\t" // Create mask pav bytes < 0
"pand %%mm5, %%mm0 \n\t" // Only pbv bytes < 0 in mm0
"pand %%mm4, %%mm7 \n\t" // Only pav bytes < 0 in mm7
"psubw %%mm0, %%mm5 \n\t"
"psubw %%mm7, %%mm4 \n\t"
"psubw %%mm0, %%mm5 \n\t"
"psubw %%mm7, %%mm4 \n\t"
"pxor %%mm0, %%mm0 \n\t"
"pcmpgtw %%mm6, %%mm0 \n\t" // Create mask pcv bytes < 0
"pand %%mm6, %%mm0 \n\t" // Only pav bytes < 0 in mm7
"psubw %%mm0, %%mm6 \n\t"
// test pa <= pb
"movq %%mm4, %%mm7 \n\t"
"psubw %%mm0, %%mm6 \n\t"
"pcmpgtw %%mm5, %%mm7 \n\t" // pa > pb?
"movq %%mm7, %%mm0 \n\t"
// use mm7 mask to merge pa & pb
"pand %%mm7, %%mm5 \n\t"
// use mm0 mask copy to merge a & b
"pand %%mm0, %%mm2 \n\t"
"pandn %%mm4, %%mm7 \n\t"
"pandn %%mm1, %%mm0 \n\t"
"paddw %%mm5, %%mm7 \n\t"
"paddw %%mm2, %%mm0 \n\t"
// test ((pa <= pb)? pa:pb) <= pc
"pcmpgtw %%mm6, %%mm7 \n\t" // pab > pc?
"movq (%%esi,%%ebx,), %%mm2 \n\t" // load b=Prior(x)
"pand %%mm7, %%mm3 \n\t"
"pandn %%mm0, %%mm7 \n\t"
"pxor %%mm1, %%mm1 \n\t"
"paddw %%mm3, %%mm7 \n\t"
"pxor %%mm0, %%mm0 \n\t"
"packuswb %%mm1, %%mm7 \n\t"
"movq %%mm2, %%mm3 \n\t" // load c=Prior(x-bpp) step 1
"pand $ActiveMask, %%mm7 \n\t"
"punpckhbw %%mm0, %%mm2 \n\t" // Unpack High bytes of b
"psllq $ShiftBpp, %%mm7 \n\t" // Shift bytes to 2nd group of 3 bytes
// pav = p - a = (a + b - c) - a = b - c
"movq %%mm2, %%mm4 \n\t"
"paddb (%%edi,%%ebx,), %%mm7 \n\t" // add Paeth predictor with Raw(x)
"psllq $ShiftBpp, %%mm3 \n\t" // load c=Prior(x-bpp) step 2
"movq %%mm7, (%%edi,%%ebx,) \n\t" // write back updated value
"movq %%mm7, %%mm1 \n\t"
"punpckhbw %%mm0, %%mm3 \n\t" // Unpack High bytes of c
"psllq $ShiftBpp, %%mm1 \n\t" // Shift bytes
// Now mm1 will be used as Raw(x-bpp)
// Now do Paeth for 3rd, and final, set of bytes (6-7)
"pxor %%mm7, %%mm7 \n\t"
"punpckhbw %%mm0, %%mm1 \n\t" // Unpack High bytes of a
"psubw %%mm3, %%mm4 \n\t"
// pbv = p - b = (a + b - c) - b = a - c
"movq %%mm1, %%mm5 \n\t"
// pcv = p - c = (a + b - c) -c = (a - c) + (b - c) = pav + pbv
"movq %%mm4, %%mm6 \n\t"
"psubw %%mm3, %%mm5 \n\t"
"pxor %%mm0, %%mm0 \n\t"
"paddw %%mm5, %%mm6 \n\t"
// pa = abs(p-a) = abs(pav)
// pb = abs(p-b) = abs(pbv)
// pc = abs(p-c) = abs(pcv)
"pcmpgtw %%mm4, %%mm0 \n\t" // Create mask pav bytes < 0
"pcmpgtw %%mm5, %%mm7 \n\t" // Create mask pbv bytes < 0
"pand %%mm4, %%mm0 \n\t" // Only pav bytes < 0 in mm7
"pand %%mm5, %%mm7 \n\t" // Only pbv bytes < 0 in mm0
"psubw %%mm0, %%mm4 \n\t"
"psubw %%mm7, %%mm5 \n\t"
"psubw %%mm0, %%mm4 \n\t"
"psubw %%mm7, %%mm5 \n\t"
"pxor %%mm0, %%mm0 \n\t"
"pcmpgtw %%mm6, %%mm0 \n\t" // Create mask pcv bytes < 0
"pand %%mm6, %%mm0 \n\t" // Only pav bytes < 0 in mm7
"psubw %%mm0, %%mm6 \n\t"
// test pa <= pb
"movq %%mm4, %%mm7 \n\t"
"psubw %%mm0, %%mm6 \n\t"
"pcmpgtw %%mm5, %%mm7 \n\t" // pa > pb?
"movq %%mm7, %%mm0 \n\t"
// use mm0 mask copy to merge a & b
"pand %%mm0, %%mm2 \n\t"
// use mm7 mask to merge pa & pb
"pand %%mm7, %%mm5 \n\t"
"pandn %%mm1, %%mm0 \n\t"
"pandn %%mm4, %%mm7 \n\t"
"paddw %%mm2, %%mm0 \n\t"
"paddw %%mm5, %%mm7 \n\t"
// test ((pa <= pb)? pa:pb) <= pc
"pcmpgtw %%mm6, %%mm7 \n\t" // pab > pc?
"pand %%mm7, %%mm3 \n\t"
"pandn %%mm0, %%mm7 \n\t"
"paddw %%mm3, %%mm7 \n\t"
"pxor %%mm1, %%mm1 \n\t"
"packuswb %%mm7, %%mm1 \n\t"
// Step ebx to next set of 8 bytes and repeat loop til done
"addl $8, %%ebx \n\t"
"pand $ActiveMaskEnd, %%mm1 \n\t"
"paddb -8(%%edi,%%ebx,), %%mm1 \n\t" // add Paeth predictor with Raw(x)
"cmpl _MMXLength, %%ebx \n\t"
"pxor %%mm0, %%mm0 \n\t" // pxor does not affect flags
"movq %%mm1, -8(%%edi,%%ebx,) \n\t" // write back updated value
// mm1 will be used as Raw(x-bpp) next loop
// mm3 ready to be used as Prior(x-bpp) next loop
"jb paeth_3lp \n\t"
: // FIXASM: output regs/vars go here, e.g.: "=m" (memory_var)
: // FIXASM: input regs, e.g.: "c" (count), "S" (src), "D" (dest)
: "%ebx", "%edi", "%esi", "%mm0", "%mm1", "%mm2", "%mm3", "%mm4", "%mm5", "%mm6", "%mm7" // CHECKASM: clobber list
);
}
break;
case 6:
//case 7: // GRR BOGUS
//case 5: // GRR BOGUS
{
ActiveMask.use = 0x00000000ffffffff;
ActiveMask2.use = 0xffffffff00000000;
ShiftBpp.use = bpp << 3; // == bpp * 8
ShiftRem.use = 64 - ShiftBpp.use;
__asm__ (
"movl _dif, %%ebx \n\t"
"movl row, %%edi \n\t"
"movl prev_row, %%esi \n\t"
// PRIME the pump (load the first Raw(x-bpp) data set
"movq -8(%%edi,%%ebx,), %%mm1 \n\t"
"pxor %%mm0, %%mm0 \n\t"
"paeth_6lp: \n\t"
// Must shift to position Raw(x-bpp) data
"psrlq $ShiftRem, %%mm1 \n\t"
// Do first set of 4 bytes
"movq -8(%%esi,%%ebx,), %%mm3 \n\t" // read c=Prior(x-bpp) bytes
"punpcklbw %%mm0, %%mm1 \n\t" // Unpack Low bytes of a
"movq (%%esi,%%ebx,), %%mm2 \n\t" // load b=Prior(x)
"punpcklbw %%mm0, %%mm2 \n\t" // Unpack Low bytes of b
// Must shift to position Prior(x-bpp) data
"psrlq $ShiftRem, %%mm3 \n\t"
// pav = p - a = (a + b - c) - a = b - c
"movq %%mm2, %%mm4 \n\t"
"punpcklbw %%mm0, %%mm3 \n\t" // Unpack Low bytes of c
// pbv = p - b = (a + b - c) - b = a - c
"movq %%mm1, %%mm5 \n\t"
"psubw %%mm3, %%mm4 \n\t"
"pxor %%mm7, %%mm7 \n\t"
// pcv = p - c = (a + b - c) -c = (a - c) + (b - c) = pav + pbv
"movq %%mm4, %%mm6 \n\t"
"psubw %%mm3, %%mm5 \n\t"
// pa = abs(p-a) = abs(pav)
// pb = abs(p-b) = abs(pbv)
// pc = abs(p-c) = abs(pcv)
"pcmpgtw %%mm4, %%mm0 \n\t" // Create mask pav bytes < 0
"paddw %%mm5, %%mm6 \n\t"
"pand %%mm4, %%mm0 \n\t" // Only pav bytes < 0 in mm7
"pcmpgtw %%mm5, %%mm7 \n\t" // Create mask pbv bytes < 0
"psubw %%mm0, %%mm4 \n\t"
"pand %%mm5, %%mm7 \n\t" // Only pbv bytes < 0 in mm0
"psubw %%mm0, %%mm4 \n\t"
"psubw %%mm7, %%mm5 \n\t"
"pxor %%mm0, %%mm0 \n\t"
"pcmpgtw %%mm6, %%mm0 \n\t" // Create mask pcv bytes < 0
"pand %%mm6, %%mm0 \n\t" // Only pav bytes < 0 in mm7
"psubw %%mm7, %%mm5 \n\t"
"psubw %%mm0, %%mm6 \n\t"
// test pa <= pb
"movq %%mm4, %%mm7 \n\t"
"psubw %%mm0, %%mm6 \n\t"
"pcmpgtw %%mm5, %%mm7 \n\t" // pa > pb?
"movq %%mm7, %%mm0 \n\t"
// use mm7 mask to merge pa & pb
"pand %%mm7, %%mm5 \n\t"
// use mm0 mask copy to merge a & b
"pand %%mm0, %%mm2 \n\t"
"pandn %%mm4, %%mm7 \n\t"
"pandn %%mm1, %%mm0 \n\t"
"paddw %%mm5, %%mm7 \n\t"
"paddw %%mm2, %%mm0 \n\t"
// test ((pa <= pb)? pa:pb) <= pc
"pcmpgtw %%mm6, %%mm7 \n\t" // pab > pc?
"pxor %%mm1, %%mm1 \n\t"
"pand %%mm7, %%mm3 \n\t"
"pandn %%mm0, %%mm7 \n\t"
"paddw %%mm3, %%mm7 \n\t"
"pxor %%mm0, %%mm0 \n\t"
"packuswb %%mm1, %%mm7 \n\t"
"movq -8(%%esi,%%ebx,), %%mm3 \n\t" // load c=Prior(x-bpp)
"pand $ActiveMask, %%mm7 \n\t"
"psrlq $ShiftRem, %%mm3 \n\t"
"movq (%%esi,%%ebx,), %%mm2 \n\t" // load b=Prior(x) step 1
"paddb (%%edi,%%ebx,), %%mm7 \n\t" // add Paeth predictor with Raw(x)
"movq %%mm2, %%mm6 \n\t"
"movq %%mm7, (%%edi,%%ebx,) \n\t" // write back updated value
"movq -8(%%edi,%%ebx,), %%mm1 \n\t"
"psllq $ShiftBpp, %%mm6 \n\t"
"movq %%mm7, %%mm5 \n\t"
"psrlq $ShiftRem, %%mm1 \n\t"
"por %%mm6, %%mm3 \n\t"
"psllq $ShiftBpp, %%mm5 \n\t"
"punpckhbw %%mm0, %%mm3 \n\t" // Unpack High bytes of c
"por %%mm5, %%mm1 \n\t"
// Do second set of 4 bytes
"punpckhbw %%mm0, %%mm2 \n\t" // Unpack High bytes of b
"punpckhbw %%mm0, %%mm1 \n\t" // Unpack High bytes of a
// pav = p - a = (a + b - c) - a = b - c
"movq %%mm2, %%mm4 \n\t"
// pbv = p - b = (a + b - c) - b = a - c
"movq %%mm1, %%mm5 \n\t"
"psubw %%mm3, %%mm4 \n\t"
"pxor %%mm7, %%mm7 \n\t"
// pcv = p - c = (a + b - c) -c = (a - c) + (b - c) = pav + pbv
"movq %%mm4, %%mm6 \n\t"
"psubw %%mm3, %%mm5 \n\t"
// pa = abs(p-a) = abs(pav)
// pb = abs(p-b) = abs(pbv)
// pc = abs(p-c) = abs(pcv)
"pcmpgtw %%mm4, %%mm0 \n\t" // Create mask pav bytes < 0
"paddw %%mm5, %%mm6 \n\t"
"pand %%mm4, %%mm0 \n\t" // Only pav bytes < 0 in mm7
"pcmpgtw %%mm5, %%mm7 \n\t" // Create mask pbv bytes < 0
"psubw %%mm0, %%mm4 \n\t"
"pand %%mm5, %%mm7 \n\t" // Only pbv bytes < 0 in mm0
"psubw %%mm0, %%mm4 \n\t"
"psubw %%mm7, %%mm5 \n\t"
"pxor %%mm0, %%mm0 \n\t"
"pcmpgtw %%mm6, %%mm0 \n\t" // Create mask pcv bytes < 0
"pand %%mm6, %%mm0 \n\t" // Only pav bytes < 0 in mm7
"psubw %%mm7, %%mm5 \n\t"
"psubw %%mm0, %%mm6 \n\t"
// test pa <= pb
"movq %%mm4, %%mm7 \n\t"
"psubw %%mm0, %%mm6 \n\t"
"pcmpgtw %%mm5, %%mm7 \n\t" // pa > pb?
"movq %%mm7, %%mm0 \n\t"
// use mm7 mask to merge pa & pb
"pand %%mm7, %%mm5 \n\t"
// use mm0 mask copy to merge a & b
"pand %%mm0, %%mm2 \n\t"
"pandn %%mm4, %%mm7 \n\t"
"pandn %%mm1, %%mm0 \n\t"
"paddw %%mm5, %%mm7 \n\t"
"paddw %%mm2, %%mm0 \n\t"
// test ((pa <= pb)? pa:pb) <= pc
"pcmpgtw %%mm6, %%mm7 \n\t" // pab > pc?
"pxor %%mm1, %%mm1 \n\t"
"pand %%mm7, %%mm3 \n\t"
"pandn %%mm0, %%mm7 \n\t"
"pxor %%mm1, %%mm1 \n\t"
"paddw %%mm3, %%mm7 \n\t"
"pxor %%mm0, %%mm0 \n\t"
// Step ex to next set of 8 bytes and repeat loop til done
"addl $8, %%ebx \n\t"
"packuswb %%mm7, %%mm1 \n\t"
"paddb -8(%%edi,%%ebx,), %%mm1 \n\t" // add Paeth predictor with Raw(x)
"cmpl _MMXLength, %%ebx \n\t"
"movq %%mm1, -8(%%edi,%%ebx,) \n\t" // write back updated value
// mm1 will be used as Raw(x-bpp) next loop
"jb paeth_6lp \n\t"
: // FIXASM: output regs/vars go here, e.g.: "=m" (memory_var)
: // FIXASM: input regs, e.g.: "c" (count), "S" (src), "D" (dest)
: "%ebx", "%edi", "%esi", "%mm0", "%mm1", "%mm2", "%mm3", "%mm4", "%mm5", "%mm6", "%mm7" // CHECKASM: clobber list
);
}
break;
case 4:
{
ActiveMask.use = 0x00000000ffffffff;
__asm__ (
"movl _dif, %%ebx \n\t"
"movl row, %%edi \n\t"
"movl prev_row, %%esi \n\t"
"pxor %%mm0, %%mm0 \n\t"
// PRIME the pump (load the first Raw(x-bpp) data set
"movq -8(%%edi,%%ebx,), %%mm1 \n\t" // Only time should need to read
// a=Raw(x-bpp) bytes
"paeth_4lp: \n\t"
// Do first set of 4 bytes
"movq -8(%%esi,%%ebx,), %%mm3 \n\t" // read c=Prior(x-bpp) bytes
"punpckhbw %%mm0, %%mm1 \n\t" // Unpack Low bytes of a
"movq (%%esi,%%ebx,), %%mm2 \n\t" // load b=Prior(x)
"punpcklbw %%mm0, %%mm2 \n\t" // Unpack High bytes of b
// pav = p - a = (a + b - c) - a = b - c
"movq %%mm2, %%mm4 \n\t"
"punpckhbw %%mm0, %%mm3 \n\t" // Unpack High bytes of c
// pbv = p - b = (a + b - c) - b = a - c
"movq %%mm1, %%mm5 \n\t"
"psubw %%mm3, %%mm4 \n\t"
"pxor %%mm7, %%mm7 \n\t"
// pcv = p - c = (a + b - c) -c = (a - c) + (b - c) = pav + pbv
"movq %%mm4, %%mm6 \n\t"
"psubw %%mm3, %%mm5 \n\t"
// pa = abs(p-a) = abs(pav)
// pb = abs(p-b) = abs(pbv)
// pc = abs(p-c) = abs(pcv)
"pcmpgtw %%mm4, %%mm0 \n\t" // Create mask pav bytes < 0
"paddw %%mm5, %%mm6 \n\t"
"pand %%mm4, %%mm0 \n\t" // Only pav bytes < 0 in mm7
"pcmpgtw %%mm5, %%mm7 \n\t" // Create mask pbv bytes < 0
"psubw %%mm0, %%mm4 \n\t"
"pand %%mm5, %%mm7 \n\t" // Only pbv bytes < 0 in mm0
"psubw %%mm0, %%mm4 \n\t"
"psubw %%mm7, %%mm5 \n\t"
"pxor %%mm0, %%mm0 \n\t"
"pcmpgtw %%mm6, %%mm0 \n\t" // Create mask pcv bytes < 0
"pand %%mm6, %%mm0 \n\t" // Only pav bytes < 0 in mm7
"psubw %%mm7, %%mm5 \n\t"
"psubw %%mm0, %%mm6 \n\t"
// test pa <= pb
"movq %%mm4, %%mm7 \n\t"
"psubw %%mm0, %%mm6 \n\t"
"pcmpgtw %%mm5, %%mm7 \n\t" // pa > pb?
"movq %%mm7, %%mm0 \n\t"
// use mm7 mask to merge pa & pb
"pand %%mm7, %%mm5 \n\t"
// use mm0 mask copy to merge a & b
"pand %%mm0, %%mm2 \n\t"
"pandn %%mm4, %%mm7 \n\t"
"pandn %%mm1, %%mm0 \n\t"
"paddw %%mm5, %%mm7 \n\t"
"paddw %%mm2, %%mm0 \n\t"
// test ((pa <= pb)? pa:pb) <= pc
"pcmpgtw %%mm6, %%mm7 \n\t" // pab > pc?
"pxor %%mm1, %%mm1 \n\t"
"pand %%mm7, %%mm3 \n\t"
"pandn %%mm0, %%mm7 \n\t"
"paddw %%mm3, %%mm7 \n\t"
"pxor %%mm0, %%mm0 \n\t"
"packuswb %%mm1, %%mm7 \n\t"
"movq (%%esi,%%ebx,), %%mm3 \n\t" // load c=Prior(x-bpp)
"pand $ActiveMask, %%mm7 \n\t"
"movq %%mm3, %%mm2 \n\t" // load b=Prior(x) step 1
"paddb (%%edi,%%ebx,), %%mm7 \n\t" // add Paeth predictor with Raw(x)
"punpcklbw %%mm0, %%mm3 \n\t" // Unpack High bytes of c
"movq %%mm7, (%%edi,%%ebx,) \n\t" // write back updated value
"movq %%mm7, %%mm1 \n\t" // Now mm1 will be used as Raw(x-bpp)
// Do second set of 4 bytes
"punpckhbw %%mm0, %%mm2 \n\t" // Unpack Low bytes of b
"punpcklbw %%mm0, %%mm1 \n\t" // Unpack Low bytes of a
// pav = p - a = (a + b - c) - a = b - c
"movq %%mm2, %%mm4 \n\t"
// pbv = p - b = (a + b - c) - b = a - c
"movq %%mm1, %%mm5 \n\t"
"psubw %%mm3, %%mm4 \n\t"
"pxor %%mm7, %%mm7 \n\t"
// pcv = p - c = (a + b - c) -c = (a - c) + (b - c) = pav + pbv
"movq %%mm4, %%mm6 \n\t"
"psubw %%mm3, %%mm5 \n\t"
// pa = abs(p-a) = abs(pav)
// pb = abs(p-b) = abs(pbv)
// pc = abs(p-c) = abs(pcv)
"pcmpgtw %%mm4, %%mm0 \n\t" // Create mask pav bytes < 0
"paddw %%mm5, %%mm6 \n\t"
"pand %%mm4, %%mm0 \n\t" // Only pav bytes < 0 in mm7
"pcmpgtw %%mm5, %%mm7 \n\t" // Create mask pbv bytes < 0
"psubw %%mm0, %%mm4 \n\t"
"pand %%mm5, %%mm7 \n\t" // Only pbv bytes < 0 in mm0
"psubw %%mm0, %%mm4 \n\t"
"psubw %%mm7, %%mm5 \n\t"
"pxor %%mm0, %%mm0 \n\t"
"pcmpgtw %%mm6, %%mm0 \n\t" // Create mask pcv bytes < 0
"pand %%mm6, %%mm0 \n\t" // Only pav bytes < 0 in mm7
"psubw %%mm7, %%mm5 \n\t"
"psubw %%mm0, %%mm6 \n\t"
// test pa <= pb
"movq %%mm4, %%mm7 \n\t"
"psubw %%mm0, %%mm6 \n\t"
"pcmpgtw %%mm5, %%mm7 \n\t" // pa > pb?
"movq %%mm7, %%mm0 \n\t"
// use mm7 mask to merge pa & pb
"pand %%mm7, %%mm5 \n\t"
// use mm0 mask copy to merge a & b
"pand %%mm0, %%mm2 \n\t"
"pandn %%mm4, %%mm7 \n\t"
"pandn %%mm1, %%mm0 \n\t"
"paddw %%mm5, %%mm7 \n\t"
"paddw %%mm2, %%mm0 \n\t"
// test ((pa <= pb)? pa:pb) <= pc
"pcmpgtw %%mm6, %%mm7 \n\t" // pab > pc?
"pxor %%mm1, %%mm1 \n\t"
"pand %%mm7, %%mm3 \n\t"
"pandn %%mm0, %%mm7 \n\t"
"pxor %%mm1, %%mm1 \n\t"
"paddw %%mm3, %%mm7 \n\t"
"pxor %%mm0, %%mm0 \n\t"
// Step ex to next set of 8 bytes and repeat loop til done
"addl $8, %%ebx \n\t"
"packuswb %%mm7, %%mm1 \n\t"
"paddb -8(%%edi,%%ebx,), %%mm1 \n\t" // add Paeth predictor with Raw(x)
"cmpl _MMXLength, %%ebx \n\t"
"movq %%mm1, -8(%%edi,%%ebx,) \n\t" // write back updated value
// mm1 will be used as Raw(x-bpp) next loop
"jb paeth_4lp \n\t"
: // FIXASM: output regs/vars go here, e.g.: "=m" (memory_var)
: // FIXASM: input regs, e.g.: "c" (count), "S" (src), "D" (dest)
: "%ebx", "%edi", "%esi", "%mm0", "%mm1", "%mm2", "%mm3", "%mm4", "%mm5", "%mm6", "%mm7" // CHECKASM: clobber list
);
}
break;
case 8: // bpp == 8
{
ActiveMask.use = 0x00000000ffffffff;
__asm__ (
"movl _dif, %%ebx \n\t"
"movl row, %%edi \n\t"
"movl prev_row, %%esi \n\t"
"pxor %%mm0, %%mm0 \n\t"
// PRIME the pump (load the first Raw(x-bpp) data set
"movq -8(%%edi,%%ebx,), %%mm1 \n\t" // Only time should need to read
// a=Raw(x-bpp) bytes
"paeth_8lp: \n\t"
// Do first set of 4 bytes
"movq -8(%%esi,%%ebx,), %%mm3 \n\t" // read c=Prior(x-bpp) bytes
"punpcklbw %%mm0, %%mm1 \n\t" // Unpack Low bytes of a
"movq (%%esi,%%ebx,), %%mm2 \n\t" // load b=Prior(x)
"punpcklbw %%mm0, %%mm2 \n\t" // Unpack Low bytes of b
// pav = p - a = (a + b - c) - a = b - c
"movq %%mm2, %%mm4 \n\t"
"punpcklbw %%mm0, %%mm3 \n\t" // Unpack Low bytes of c
// pbv = p - b = (a + b - c) - b = a - c
"movq %%mm1, %%mm5 \n\t"
"psubw %%mm3, %%mm4 \n\t"
"pxor %%mm7, %%mm7 \n\t"
// pcv = p - c = (a + b - c) -c = (a - c) + (b - c) = pav + pbv
"movq %%mm4, %%mm6 \n\t"
"psubw %%mm3, %%mm5 \n\t"
// pa = abs(p-a) = abs(pav)
// pb = abs(p-b) = abs(pbv)
// pc = abs(p-c) = abs(pcv)
"pcmpgtw %%mm4, %%mm0 \n\t" // Create mask pav bytes < 0
"paddw %%mm5, %%mm6 \n\t"
"pand %%mm4, %%mm0 \n\t" // Only pav bytes < 0 in mm7
"pcmpgtw %%mm5, %%mm7 \n\t" // Create mask pbv bytes < 0
"psubw %%mm0, %%mm4 \n\t"
"pand %%mm5, %%mm7 \n\t" // Only pbv bytes < 0 in mm0
"psubw %%mm0, %%mm4 \n\t"
"psubw %%mm7, %%mm5 \n\t"
"pxor %%mm0, %%mm0 \n\t"
"pcmpgtw %%mm6, %%mm0 \n\t" // Create mask pcv bytes < 0
"pand %%mm6, %%mm0 \n\t" // Only pav bytes < 0 in mm7
"psubw %%mm7, %%mm5 \n\t"
"psubw %%mm0, %%mm6 \n\t"
// test pa <= pb
"movq %%mm4, %%mm7 \n\t"
"psubw %%mm0, %%mm6 \n\t"
"pcmpgtw %%mm5, %%mm7 \n\t" // pa > pb?
"movq %%mm7, %%mm0 \n\t"
// use mm7 mask to merge pa & pb
"pand %%mm7, %%mm5 \n\t"
// use mm0 mask copy to merge a & b
"pand %%mm0, %%mm2 \n\t"
"pandn %%mm4, %%mm7 \n\t"
"pandn %%mm1, %%mm0 \n\t"
"paddw %%mm5, %%mm7 \n\t"
"paddw %%mm2, %%mm0 \n\t"
// test ((pa <= pb)? pa:pb) <= pc
"pcmpgtw %%mm6, %%mm7 \n\t" // pab > pc?
"pxor %%mm1, %%mm1 \n\t"
"pand %%mm7, %%mm3 \n\t"
"pandn %%mm0, %%mm7 \n\t"
"paddw %%mm3, %%mm7 \n\t"
"pxor %%mm0, %%mm0 \n\t"
"packuswb %%mm1, %%mm7 \n\t"
"movq -8(%%esi,%%ebx,), %%mm3 \n\t" // read c=Prior(x-bpp) bytes
"pand $ActiveMask, %%mm7 \n\t"
"movq (%%esi,%%ebx,), %%mm2 \n\t" // load b=Prior(x)
"paddb (%%edi,%%ebx,), %%mm7 \n\t" // add Paeth predictor with Raw(x)
"punpckhbw %%mm0, %%mm3 \n\t" // Unpack High bytes of c
"movq %%mm7, (%%edi,%%ebx,) \n\t" // write back updated value
"movq -8(%%edi,%%ebx,), %%mm1 \n\t" // read a=Raw(x-bpp) bytes
// Do second set of 4 bytes
"punpckhbw %%mm0, %%mm2 \n\t" // Unpack High bytes of b
"punpckhbw %%mm0, %%mm1 \n\t" // Unpack High bytes of a
// pav = p - a = (a + b - c) - a = b - c
"movq %%mm2, %%mm4 \n\t"
// pbv = p - b = (a + b - c) - b = a - c
"movq %%mm1, %%mm5 \n\t"
"psubw %%mm3, %%mm4 \n\t"
"pxor %%mm7, %%mm7 \n\t"
// pcv = p - c = (a + b - c) -c = (a - c) + (b - c) = pav + pbv
"movq %%mm4, %%mm6 \n\t"
"psubw %%mm3, %%mm5 \n\t"
// pa = abs(p-a) = abs(pav)
// pb = abs(p-b) = abs(pbv)
// pc = abs(p-c) = abs(pcv)
"pcmpgtw %%mm4, %%mm0 \n\t" // Create mask pav bytes < 0
"paddw %%mm5, %%mm6 \n\t"
"pand %%mm4, %%mm0 \n\t" // Only pav bytes < 0 in mm7
"pcmpgtw %%mm5, %%mm7 \n\t" // Create mask pbv bytes < 0
"psubw %%mm0, %%mm4 \n\t"
"pand %%mm5, %%mm7 \n\t" // Only pbv bytes < 0 in mm0
"psubw %%mm0, %%mm4 \n\t"
"psubw %%mm7, %%mm5 \n\t"
"pxor %%mm0, %%mm0 \n\t"
"pcmpgtw %%mm6, %%mm0 \n\t" // Create mask pcv bytes < 0
"pand %%mm6, %%mm0 \n\t" // Only pav bytes < 0 in mm7
"psubw %%mm7, %%mm5 \n\t"
"psubw %%mm0, %%mm6 \n\t"
// test pa <= pb
"movq %%mm4, %%mm7 \n\t"
"psubw %%mm0, %%mm6 \n\t"
"pcmpgtw %%mm5, %%mm7 \n\t" // pa > pb?
"movq %%mm7, %%mm0 \n\t"
// use mm7 mask to merge pa & pb
"pand %%mm7, %%mm5 \n\t"
// use mm0 mask copy to merge a & b
"pand %%mm0, %%mm2 \n\t"
"pandn %%mm4, %%mm7 \n\t"
"pandn %%mm1, %%mm0 \n\t"
"paddw %%mm5, %%mm7 \n\t"
"paddw %%mm2, %%mm0 \n\t"
// test ((pa <= pb)? pa:pb) <= pc
"pcmpgtw %%mm6, %%mm7 \n\t" // pab > pc?
"pxor %%mm1, %%mm1 \n\t"
"pand %%mm7, %%mm3 \n\t"
"pandn %%mm0, %%mm7 \n\t"
"pxor %%mm1, %%mm1 \n\t"
"paddw %%mm3, %%mm7 \n\t"
"pxor %%mm0, %%mm0 \n\t"
// Step ex to next set of 8 bytes and repeat loop til done
"addl $8, %%ebx \n\t"
"packuswb %%mm7, %%mm1 \n\t"
"paddb -8(%%edi,%%ebx,), %%mm1 \n\t" // add Paeth predictor with Raw(x)
"cmpl _MMXLength, %%ebx \n\t"
"movq %%mm1, -8(%%edi,%%ebx,) \n\t" // write back updated value
// mm1 will be used as Raw(x-bpp) next loop
"jb paeth_8lp \n\t"
: // FIXASM: output regs/vars go here, e.g.: "=m" (memory_var)
: // FIXASM: input regs, e.g.: "c" (count), "S" (src), "D" (dest)
: "%ebx", "%edi", "%esi", "%mm0", "%mm1", "%mm2", "%mm3", "%mm4", "%mm5", "%mm6", "%mm7" // CHECKASM: clobber list
);
}
break;
case 1: // bpp = 1
case 2: // bpp = 2
default: // bpp > 8
{
__asm__ (
"movl _dif, %%ebx \n\t"
"cmpl _FullLength, %%ebx \n\t"
"jnb paeth_dend \n\t"
"movl row, %%edi \n\t"
"movl prev_row, %%esi \n\t"
// Do Paeth decode for remaining bytes
"movl %%ebx, %%edx \n\t"
"xorl %%ecx, %%ecx \n\t" // zero ecx before using cl & cx in loop below
"subl bpp, %%edx \n\t" // Set edx = ebx - bpp
"paeth_dlp: \n\t"
"xorl %%eax, %%eax \n\t"
// pav = p - a = (a + b - c) - a = b - c
"movb (%%esi,%%ebx,), %%al \n\t" // load Prior(x) into al
"movb (%%esi,%%edx,), %%cl \n\t" // load Prior(x-bpp) into cl
"subl %%ecx, %%eax \n\t" // subtract Prior(x-bpp)
"movl %%eax, patemp \n\t" // Save pav for later use
"xorl %%eax, %%eax \n\t"
// pbv = p - b = (a + b - c) - b = a - c
"movb (%%edi,%%edx,), %%al \n\t" // load Raw(x-bpp) into al
"subl %%ecx, %%eax \n\t" // subtract Prior(x-bpp)
"movl %%eax, %%ecx \n\t"
// pcv = p - c = (a + b - c) -c = (a - c) + (b - c) = pav + pbv
"addl patemp, %%eax \n\t" // pcv = pav + pbv
// pc = abs(pcv)
"testl $0x80000000, %%eax \n\t"
"jz paeth_dpca \n\t"
"negl %%eax \n\t" // reverse sign of neg values
"paeth_dpca: \n\t"
"movl %%eax, pctemp \n\t" // save pc for later use
// pb = abs(pbv)
"testl $0x80000000, %%ecx \n\t"
"jz paeth_dpba \n\t"
"negl %%ecx \n\t" // reverse sign of neg values
"paeth_dpba: \n\t"
"movl %%ecx, pbtemp \n\t" // save pb for later use
// pa = abs(pav)
"movl patemp, %%eax \n\t"
"testl $0x80000000, %%eax \n\t"
"jz paeth_dpaa \n\t"
"negl %%eax \n\t" // reverse sign of neg values
"paeth_dpaa: \n\t"
"movl %%eax, patemp \n\t" // save pa for later use
// test if pa <= pb
"cmpl %%ecx, %%eax \n\t"
"jna paeth_dabb \n\t"
// pa > pb; now test if pb <= pc
"cmpl pctemp, %%ecx \n\t"
"jna paeth_dbbc \n\t"
// pb > pc; Raw(x) = Paeth(x) + Prior(x-bpp)
"movb (%%esi,%%edx,), %%cl \n\t" // load Prior(x-bpp) into cl
"jmp paeth_dpaeth \n\t"
"paeth_dbbc: \n\t"
// pb <= pc; Raw(x) = Paeth(x) + Prior(x)
"movb (%%esi,%%ebx,), %%cl \n\t" // load Prior(x) into cl
"jmp paeth_dpaeth \n\t"
"paeth_dabb: \n\t"
// pa <= pb; now test if pa <= pc
"cmpl pctemp, %%eax \n\t"
"jna paeth_dabc \n\t"
// pa > pc; Raw(x) = Paeth(x) + Prior(x-bpp)
"movb (%%esi,%%edx,), %%cl \n\t" // load Prior(x-bpp) into cl
"jmp paeth_dpaeth \n\t"
"paeth_dabc: \n\t"
// pa <= pc; Raw(x) = Paeth(x) + Raw(x-bpp)
"movb (%%edi,%%edx,), %%cl \n\t" // load Raw(x-bpp) into cl
"paeth_dpaeth: \n\t"
"incl %%ebx \n\t"
"incl %%edx \n\t"
// Raw(x) = (Paeth(x) + Paeth_Predictor( a, b, c )) mod 256
"addb %%cl, -1(%%edi,%%ebx,) \n\t"
"cmpl _FullLength, %%ebx \n\t"
"jb paeth_dlp \n\t"
"paeth_dend: \n\t"
: // FIXASM: output regs/vars go here, e.g.: "=m" (memory_var)
: // FIXASM: input regs, e.g.: "c" (count), "S" (src), "D" (dest)
: "%eax", "%ebx", "%ecx", "%edx", "%edi", "%esi" // CHECKASM: clobber list
);
}
return; // No need to go further with this one
} // end switch ( bpp )
__asm__ (
// MMX acceleration complete now do clean-up
// Check if any remaining bytes left to decode
"movl _MMXLength, %%ebx \n\t"
"cmpl _FullLength, %%ebx \n\t"
"jnb paeth_end \n\t"
"movl row, %%edi \n\t"
"movl prev_row, %%esi \n\t"
// Do Paeth decode for remaining bytes
"movl %%ebx, %%edx \n\t"
"xorl %%ecx, %%ecx \n\t" // zero ecx before using cl & cx in loop below
"subl bpp, %%edx \n\t" // Set edx = ebx - bpp
"paeth_lp2: \n\t"
"xorl %%eax, %%eax \n\t"
// pav = p - a = (a + b - c) - a = b - c
"movb (%%esi,%%ebx,), %%al \n\t" // load Prior(x) into al
"movb (%%esi,%%edx,), %%cl \n\t" // load Prior(x-bpp) into cl
"subl %%ecx, %%eax \n\t" // subtract Prior(x-bpp)
"movl %%eax, patemp \n\t" // Save pav for later use
"xorl %%eax, %%eax \n\t"
// pbv = p - b = (a + b - c) - b = a - c
"movb (%%edi,%%edx,), %%al \n\t" // load Raw(x-bpp) into al
"subl %%ecx, %%eax \n\t" // subtract Prior(x-bpp)
"movl %%eax, %%ecx \n\t"
// pcv = p - c = (a + b - c) -c = (a - c) + (b - c) = pav + pbv
"addl patemp, %%eax \n\t" // pcv = pav + pbv
// pc = abs(pcv)
"testl $0x80000000, %%eax \n\t"
"jz paeth_pca2 \n\t"
"negl %%eax \n\t" // reverse sign of neg values
"paeth_pca2: \n\t"
"movl %%eax, pctemp \n\t" // save pc for later use
// pb = abs(pbv)
"testl $0x80000000, %%ecx \n\t"
"jz paeth_pba2 \n\t"
"negl %%ecx \n\t" // reverse sign of neg values
"paeth_pba2: \n\t"
"movl %%ecx, pbtemp \n\t" // save pb for later use
// pa = abs(pav)
"movl patemp, %%eax \n\t"
"testl $0x80000000, %%eax \n\t"
"jz paeth_paa2 \n\t"
"negl %%eax \n\t" // reverse sign of neg values
"paeth_paa2: \n\t"
"movl %%eax, patemp \n\t" // save pa for later use
// test if pa <= pb
"cmpl %%ecx, %%eax \n\t"
"jna paeth_abb2 \n\t"
// pa > pb; now test if pb <= pc
"cmpl pctemp, %%ecx \n\t"
"jna paeth_bbc2 \n\t"
// pb > pc; Raw(x) = Paeth(x) + Prior(x-bpp)
"movb (%%esi,%%edx,), %%cl \n\t" // load Prior(x-bpp) into cl
"jmp paeth_paeth2 \n\t"
"paeth_bbc2: \n\t"
// pb <= pc; Raw(x) = Paeth(x) + Prior(x)
"movb (%%esi,%%ebx,), %%cl \n\t" // load Prior(x) into cl
"jmp paeth_paeth2 \n\t"
"paeth_abb2: \n\t"
// pa <= pb; now test if pa <= pc
"cmpl pctemp, %%eax \n\t"
"jna paeth_abc2 \n\t"
// pa > pc; Raw(x) = Paeth(x) + Prior(x-bpp)
"movb (%%esi,%%edx,), %%cl \n\t" // load Prior(x-bpp) into cl
"jmp paeth_paeth2 \n\t"
"paeth_abc2: \n\t"
// pa <= pc; Raw(x) = Paeth(x) + Raw(x-bpp)
"movb (%%edi,%%edx,), %%cl \n\t" // load Raw(x-bpp) into cl
"paeth_paeth2: \n\t"
"incl %%ebx \n\t"
"incl %%edx \n\t"
// Raw(x) = (Paeth(x) + Paeth_Predictor( a, b, c )) mod 256
"addb %%cl, -1(%%edi,%%ebx,) \n\t"
"cmpl _FullLength, %%ebx \n\t"
"jb paeth_lp2 \n\t"
"paeth_end: \n\t"
"emms \n\t" // End MMX instructions; prep for possible FP instrs.
: // FIXASM: output regs/vars go here, e.g.: "=m" (memory_var)
: // FIXASM: input regs, e.g.: "c" (count), "S" (src), "D" (dest)
: "%eax", "%ebx", "%ecx", "%edx", "%edi", "%esi" // CHECKASM: clobber list
);
#endif /* GRR_GCC_MMX_CONVERTED */
}
// Optimized code for PNG Sub filter decoder
void /* PRIVATE */
png_read_filter_row_mmx_sub(png_row_infop row_info, png_bytep row)
{
#ifdef GRR_GCC_MMX_CONVERTED
int bpp;
bpp = (row_info->pixel_depth + 7) >> 3; // Get # bytes per pixel
_FullLength = row_info->rowbytes - bpp; // # of bytes to filter
__asm__ (
"movl row, %%edi \n\t"
"movl %%edi, %%esi \n\t" // lp = row
"addl bpp, %%edi \n\t" // rp = row + bpp
"xorl %%eax, %%eax \n\t"
// get # of bytes to alignment
"movl %%edi, _dif \n\t" // take start of row
"addl $0xf, _dif \n\t" // add 7 + 8 to incr past
// alignment boundary
"xorl %%ebx, %%ebx \n\t"
"andl $0xfffffff8, _dif \n\t" // mask to alignment boundary
"subl %%edi, _dif \n\t" // subtract from start ==> value
// ebx at alignment
"jz sub_go \n\t"
// fix alignment
"sub_lp1: \n\t"
"movb (%%esi,%%ebx,), %%al \n\t"
"addb %%al, (%%edi,%%ebx,) \n\t"
"incl %%ebx \n\t"
"cmpl _dif, %%ebx \n\t"
"jb sub_lp1 \n\t"
"sub_go: \n\t"
"movl _FullLength, %%ecx \n\t"
"movl %%ecx, %%edx \n\t"
"subl %%ebx, %%edx \n\t" // subtract alignment fix
"andl $0x00000007, %%edx \n\t" // calc bytes over mult of 8
"subl %%edx, %%ecx \n\t" // drop over bytes from length
"movl %%ecx, _MMXLength \n\t"
: // FIXASM: output regs/vars go here, e.g.: "=m" (memory_var)
: // FIXASM: input regs, e.g.: "c" (count), "S" (src), "D" (dest)
: "%eax", "%ebx", "%ecx", "%edx", "%edi", "%esi" // CHECKASM: clobber list
);
// Now do the math for the rest of the row
switch ( bpp )
{
case 3:
{
ActiveMask.use = 0x0000ffffff000000;
ShiftBpp.use = 24; // == 3 * 8
ShiftRem.use = 40; // == 64 - 24
__asm__ (
"movl row, %%edi \n\t"
"movq $ActiveMask, %%mm7 \n\t" // Load ActiveMask for 2nd active byte group
"movl %%edi, %%esi \n\t" // lp = row
"addl bpp, %%edi \n\t" // rp = row + bpp
"movq %%mm7, %%mm6 \n\t"
"movl _dif, %%ebx \n\t"
"psllq $ShiftBpp, %%mm6 \n\t" // Move mask in mm6 to cover 3rd active
// byte group
// PRIME the pump (load the first Raw(x-bpp) data set
"movq -8(%%edi,%%ebx,), %%mm1 \n\t"
"sub_3lp: \n\t"
"psrlq $ShiftRem, %%mm1 \n\t" // Shift data for adding 1st bpp bytes
// no need for mask; shift clears inactive bytes
// Add 1st active group
"movq (%%edi,%%ebx,), %%mm0 \n\t"
"paddb %%mm1, %%mm0 \n\t"
// Add 2nd active group
"movq %%mm0, %%mm1 \n\t" // mov updated Raws to mm1
"psllq $ShiftBpp, %%mm1 \n\t" // shift data to position correctly
"pand %%mm7, %%mm1 \n\t" // mask to use only 2nd active group
"paddb %%mm1, %%mm0 \n\t"
// Add 3rd active group
"movq %%mm0, %%mm1 \n\t" // mov updated Raws to mm1
"psllq $ShiftBpp, %%mm1 \n\t" // shift data to position correctly
"pand %%mm6, %%mm1 \n\t" // mask to use only 3rd active group
"addl $8, %%ebx \n\t"
"paddb %%mm1, %%mm0 \n\t"
"cmpl _MMXLength, %%ebx \n\t"
"movq %%mm0, -8(%%edi,%%ebx,) \n\t" // Write updated Raws back to array
// Prep for doing 1st add at top of loop
"movq %%mm0, %%mm1 \n\t"
"jb sub_3lp \n\t"
: // FIXASM: output regs/vars go here, e.g.: "=m" (memory_var)
: // FIXASM: input regs, e.g.: "c" (count), "S" (src), "D" (dest)
: "%ebx", "%edi", "%esi", "%mm0", "%mm1", "%mm6", "%mm7" // CHECKASM: clobber list
);
}
break;
case 1:
{
// Placed here just in case this is a duplicate of the
// non-MMX code for the SUB filter in png_read_filter_row above
//
// png_bytep rp;
// png_bytep lp;
// png_uint_32 i;
// bpp = (row_info->pixel_depth + 7) >> 3;
// for (i = (png_uint_32)bpp, rp = row + bpp, lp = row;
// i < row_info->rowbytes; i++, rp++, lp++)
// {
// *rp = (png_byte)(((int)(*rp) + (int)(*lp)) & 0xff);
// }
__asm__ (
"movl _dif, %%ebx \n\t"
"movl row, %%edi \n\t"
"cmpl _FullLength, %%ebx \n\t"
"jnb sub_1end \n\t"
"movl %%edi, %%esi \n\t" // lp = row
"xorl %%eax, %%eax \n\t"
"addl bpp, %%edi \n\t" // rp = row + bpp
"sub_1lp: \n\t"
"movb (%%esi,%%ebx,), %%al \n\t"
"addb %%al, (%%edi,%%ebx,) \n\t"
"incl %%ebx \n\t"
"cmpl _FullLength, %%ebx \n\t"
"jb sub_1lp \n\t"
"sub_1end: \n\t"
: // FIXASM: output regs/vars go here, e.g.: "=m" (memory_var)
: // FIXASM: input regs, e.g.: "c" (count), "S" (src), "D" (dest)
: "%eax", "%ebx", "%edi", "%esi" // CHECKASM: clobber list
);
}
return;
case 6:
case 7:
case 4:
case 5:
{
ShiftBpp.use = bpp << 3;
ShiftRem.use = 64 - ShiftBpp.use;
__asm__ (
"movl row, %%edi \n\t"
"movl _dif, %%ebx \n\t"
"movl %%edi, %%esi \n\t" // lp = row
"addl bpp, %%edi \n\t" // rp = row + bpp
// PRIME the pump (load the first Raw(x-bpp) data set
"movq -8(%%edi,%%ebx,), %%mm1 \n\t"
"sub_4lp: \n\t"
"psrlq $ShiftRem, %%mm1 \n\t" // Shift data for adding 1st bpp bytes
// no need for mask; shift clears inactive bytes
"movq (%%edi,%%ebx,), %%mm0 \n\t"
"paddb %%mm1, %%mm0 \n\t"
// Add 2nd active group
"movq %%mm0, %%mm1 \n\t" // mov updated Raws to mm1
"psllq $ShiftBpp, %%mm1 \n\t" // shift data to position correctly
// there is no need for any mask
// since shift clears inactive bits/bytes
"addl $8, %%ebx \n\t"
"paddb %%mm1, %%mm0 \n\t"
"cmpl _MMXLength, %%ebx \n\t"
"movq %%mm0, -8(%%edi,%%ebx,) \n\t"
"movq %%mm0, %%mm1 \n\t" // Prep for doing 1st add at top of loop
"jb sub_4lp \n\t"
: // FIXASM: output regs/vars go here, e.g.: "=m" (memory_var)
: // FIXASM: input regs, e.g.: "c" (count), "S" (src), "D" (dest)
: "%ebx", "%edi", "%esi", "%mm0", "%mm1" // CHECKASM: clobber list
);
}
break;
case 2:
{
ActiveMask.use = 0x00000000ffff0000;
ShiftBpp.use = 16; // == 2 * 8
ShiftRem.use = 48; // == 64 - 16
__asm__ (
"movq $ActiveMask, %%mm7 \n\t" // Load ActiveMask for 2nd active byte group
"movl _dif, %%ebx \n\t"
"movq %%mm7, %%mm6 \n\t"
"movl row, %%edi \n\t"
"psllq $ShiftBpp, %%mm6 \n\t" // Move mask in mm6 to cover 3rd active
// byte group
"movl %%edi, %%esi \n\t" // lp = row
"movq %%mm6, %%mm5 \n\t"
"addl bpp, %%edi \n\t" // rp = row + bpp
"psllq $ShiftBpp, %%mm5 \n\t" // Move mask in mm5 to cover 4th active
// byte group
// PRIME the pump (load the first Raw(x-bpp) data set
"movq -8(%%edi,%%ebx,), %%mm1 \n\t"
"sub_2lp: \n\t"
// Add 1st active group
"psrlq $ShiftRem, %%mm1 \n\t" // Shift data for adding 1st bpp bytes
// no need for mask; shift clears inactive
// bytes
"movq (%%edi,%%ebx,), %%mm0 \n\t"
"paddb %%mm1, %%mm0 \n\t"
// Add 2nd active group
"movq %%mm0, %%mm1 \n\t" // mov updated Raws to mm1
"psllq $ShiftBpp, %%mm1 \n\t" // shift data to position correctly
"pand %%mm7, %%mm1 \n\t" // mask to use only 2nd active group
"paddb %%mm1, %%mm0 \n\t"
// Add 3rd active group
"movq %%mm0, %%mm1 \n\t" // mov updated Raws to mm1
"psllq $ShiftBpp, %%mm1 \n\t" // shift data to position correctly
"pand %%mm6, %%mm1 \n\t" // mask to use only 3rd active group
"paddb %%mm1, %%mm0 \n\t"
// Add 4th active group
"movq %%mm0, %%mm1 \n\t" // mov updated Raws to mm1
"psllq $ShiftBpp, %%mm1 \n\t" // shift data to position correctly
"pand %%mm5, %%mm1 \n\t" // mask to use only 4th active group
"addl $8, %%ebx \n\t"
"paddb %%mm1, %%mm0 \n\t"
"cmpl _MMXLength, %%ebx \n\t"
"movq %%mm0, -8(%%edi,%%ebx,) \n\t" // Write updated Raws back to array
"movq %%mm0, %%mm1 \n\t" // Prep for doing 1st add at top of loop
"jb sub_2lp \n\t"
: // FIXASM: output regs/vars go here, e.g.: "=m" (memory_var)
: // FIXASM: input regs, e.g.: "c" (count), "S" (src), "D" (dest)
: "%ebx", "%edi", "%esi", "%mm0", "%mm1", "%mm5", "%mm6", "%mm7" // CHECKASM: clobber list
);
}
break;
case 8:
{
__asm__ (
"movl row, %%edi \n\t"
"movl _dif, %%ebx \n\t"
"movl %%edi, %%esi \n\t" // lp = row
"addl bpp, %%edi \n\t" // rp = row + bpp
"movl _MMXLength, %%ecx \n\t"
"movq -8(%%edi,%%ebx,), %%mm7 \n\t" // PRIME the pump (load the first
// Raw(x-bpp) data set
"andl $0x0000003f, %%ecx \n\t" // calc bytes over mult of 64
"sub_8lp: \n\t"
"movq (%%edi,%%ebx,), %%mm0 \n\t" // Load Sub(x) for 1st 8 bytes
"paddb %%mm7, %%mm0 \n\t"
"movq 8(%%edi,%%ebx,), %%mm1 \n\t" // Load Sub(x) for 2nd 8 bytes
"movq %%mm0, (%%edi,%%ebx,) \n\t" // Write Raw(x) for 1st 8 bytes
// Now mm0 will be used as Raw(x-bpp) for
// the 2nd group of 8 bytes. This will be
// repeated for each group of 8 bytes with
// the 8th group being used as the Raw(x-bpp)
// for the 1st group of the next loop.
"paddb %%mm0, %%mm1 \n\t"
"movq 16(%%edi,%%ebx,), %%mm2 \n\t" // Load Sub(x) for 3rd 8 bytes
"movq %%mm1, 8(%%edi,%%ebx,) \n\t" // Write Raw(x) for 2nd 8 bytes
"paddb %%mm1, %%mm2 \n\t"
"movq 24(%%edi,%%ebx,), %%mm3 \n\t" // Load Sub(x) for 4th 8 bytes
"movq %%mm2, 16(%%edi,%%ebx,) \n\t" // Write Raw(x) for 3rd 8 bytes
"paddb %%mm2, %%mm3 \n\t"
"movq 32(%%edi,%%ebx,), %%mm4 \n\t" // Load Sub(x) for 5th 8 bytes
"movq %%mm3, 24(%%edi,%%ebx,) \n\t" // Write Raw(x) for 4th 8 bytes
"paddb %%mm3, %%mm4 \n\t"
"movq 40(%%edi,%%ebx,), %%mm5 \n\t" // Load Sub(x) for 6th 8 bytes
"movq %%mm4, 32(%%edi,%%ebx,) \n\t" // Write Raw(x) for 5th 8 bytes
"paddb %%mm4, %%mm5 \n\t"
"movq 48(%%edi,%%ebx,), %%mm6 \n\t" // Load Sub(x) for 7th 8 bytes
"movq %%mm5, 40(%%edi,%%ebx,) \n\t" // Write Raw(x) for 6th 8 bytes
"paddb %%mm5, %%mm6 \n\t"
"movq 56(%%edi,%%ebx,), %%mm7 \n\t" // Load Sub(x) for 8th 8 bytes
"movq %%mm6, 48(%%edi,%%ebx,) \n\t" // Write Raw(x) for 7th 8 bytes
"addl $64, %%ebx \n\t"
"paddb %%mm6, %%mm7 \n\t"
"cmpl %%ecx, %%ebx \n\t"
"movq %%mm7, -8(%%edi,%%ebx,) \n\t" // Write Raw(x) for 8th 8 bytes
"jb sub_8lp \n\t"
"cmpl _MMXLength, %%ebx \n\t"
"jnb sub_8lt8 \n\t"
"sub_8lpA: \n\t"
"movq (%%edi,%%ebx,), %%mm0 \n\t"
"addl $8, %%ebx \n\t"
"paddb %%mm7, %%mm0 \n\t"
"cmpl _MMXLength, %%ebx \n\t"
"movq %%mm0, -8(%%edi,%%ebx,) \n\t" // use -8 to offset early add to ebx
"movq %%mm0, %%mm7 \n\t" // Move calculated Raw(x) data to mm1 to
// be the new Raw(x-bpp) for the next loop
"jb sub_8lpA \n\t"
"sub_8lt8: \n\t"
: // FIXASM: output regs/vars go here, e.g.: "=m" (memory_var)
: // FIXASM: input regs, e.g.: "c" (count), "S" (src), "D" (dest)
: "%ebx", "%ecx", "%edi", "%esi", "%mm0", "%mm1", "%mm2", "%mm3", "%mm4", "%mm5", "%mm6", "%mm7" // CHECKASM: clobber list
);
}
break;
default: // bpp greater than 8 bytes
{
__asm__ (
"movl _dif, %%ebx \n\t"
"movl row, %%edi \n\t"
"movl %%edi, %%esi \n\t" // lp = row
"addl bpp, %%edi \n\t" // rp = row + bpp
"sub_Alp: \n\t"
"movq (%%edi,%%ebx,), %%mm0 \n\t"
"movq (%%esi,%%ebx,), %%mm1 \n\t"
"addl $8, %%ebx \n\t"
"paddb %%mm1, %%mm0 \n\t"
"cmpl _MMXLength, %%ebx \n\t"
"movq %%mm0, -8(%%edi,%%ebx,) \n\t" // mov does not affect flags; -8 to offset
// add ebx
"jb sub_Alp \n\t"
: // FIXASM: output regs/vars go here, e.g.: "=m" (memory_var)
: // FIXASM: input regs, e.g.: "c" (count), "S" (src), "D" (dest)
: "%ebx", "%edi", "%esi", "%mm0", "%mm1" // CHECKASM: clobber list
);
}
break;
} // end switch ( bpp )
__asm__ (
"movl _MMXLength, %%ebx \n\t"
"movl row, %%edi \n\t"
"cmpl _FullLength, %%ebx \n\t"
"jnb sub_end \n\t"
"movl %%edi, %%esi \n\t" // lp = row
"xorl %%eax, %%eax \n\t"
"addl bpp, %%edi \n\t" // rp = row + bpp
"sub_lp2: \n\t"
"movb (%%esi,%%ebx,), %%al \n\t"
"addb %%al, (%%edi,%%ebx,) \n\t"
"incl %%ebx \n\t"
"cmpl _FullLength, %%ebx \n\t"
"jb sub_lp2 \n\t"
"sub_end: \n\t"
"emms \n\t" // end MMX instructions
: // FIXASM: output regs/vars go here, e.g.: "=m" (memory_var)
: // FIXASM: input regs, e.g.: "c" (count), "S" (src), "D" (dest)
: "%eax", "%ebx", "%edi", "%esi" // CHECKASM: clobber list
);
#endif /* GRR_GCC_MMX_CONVERTED */
}
// Optimized code for PNG Up filter decoder
void /* PRIVATE */
png_read_filter_row_mmx_up(png_row_infop row_info, png_bytep row,
png_bytep prev_row)
{
#ifdef GRR_GCC_MMX_CONVERTED
png_uint_32 len;
len = row_info->rowbytes; // # of bytes to filter
__asm__ (
"movl row, %%edi \n\t"
// get # of bytes to alignment
"movl %%edi, %%ecx \n\t"
"xorl %%ebx, %%ebx \n\t"
"addl $0x7, %%ecx \n\t"
"xorl %%eax, %%eax \n\t"
"andl $0xfffffff8, %%ecx \n\t"
"movl prev_row, %%esi \n\t"
"subl %%edi, %%ecx \n\t"
"jz up_go \n\t"
// fix alignment
"up_lp1: \n\t"
"movb (%%edi,%%ebx,), %%al \n\t"
"addb (%%esi,%%ebx,), %%al \n\t"
"incl %%ebx \n\t"
"cmpl %%ecx, %%ebx \n\t"
"movb %%al, -1(%%edi,%%ebx,) \n\t" // mov does not affect flags; -1 to offset inc ebx
"jb up_lp1 \n\t"
"up_go: \n\t"
"movl len, %%ecx \n\t"
"movl %%ecx, %%edx \n\t"
"subl %%ebx, %%edx \n\t" // subtract alignment fix
"andl $0x0000003f, %%edx \n\t" // calc bytes over mult of 64
"subl %%edx, %%ecx \n\t" // drop over bytes from length
// Unrolled loop - use all MMX registers and interleave to reduce
// number of branch instructions (loops) and reduce partial stalls
"up_loop: \n\t"
"movq (%%esi,%%ebx,), %%mm1 \n\t"
"movq (%%edi,%%ebx,), %%mm0 \n\t"
"movq 8(%%esi,%%ebx,), %%mm3 \n\t"
"paddb %%mm1, %%mm0 \n\t"
"movq 8(%%edi,%%ebx,), %%mm2 \n\t"
"movq %%mm0, (%%edi,%%ebx,) \n\t"
"paddb %%mm3, %%mm2 \n\t"
"movq 16(%%esi,%%ebx,), %%mm5 \n\t"
"movq %%mm2, 8(%%edi,%%ebx,) \n\t"
"movq 16(%%edi,%%ebx,), %%mm4 \n\t"
"movq 24(%%esi,%%ebx,), %%mm7 \n\t"
"paddb %%mm5, %%mm4 \n\t"
"movq 24(%%edi,%%ebx,), %%mm6 \n\t"
"movq %%mm4, 16(%%edi,%%ebx,) \n\t"
"paddb %%mm7, %%mm6 \n\t"
"movq 32(%%esi,%%ebx,), %%mm1 \n\t"
"movq %%mm6, 24(%%edi,%%ebx,) \n\t"
"movq 32(%%edi,%%ebx,), %%mm0 \n\t"
"movq 40(%%esi,%%ebx,), %%mm3 \n\t"
"paddb %%mm1, %%mm0 \n\t"
"movq 40(%%edi,%%ebx,), %%mm2 \n\t"
"movq %%mm0, 32(%%edi,%%ebx,) \n\t"
"paddb %%mm3, %%mm2 \n\t"
"movq 48(%%esi,%%ebx,), %%mm5 \n\t"
"movq %%mm2, 40(%%edi,%%ebx,) \n\t"
"movq 48(%%edi,%%ebx,), %%mm4 \n\t"
"movq 56(%%esi,%%ebx,), %%mm7 \n\t"
"paddb %%mm5, %%mm4 \n\t"
"movq 56(%%edi,%%ebx,), %%mm6 \n\t"
"movq %%mm4, 48(%%edi,%%ebx,) \n\t"
"addl $64, %%ebx \n\t"
"paddb %%mm7, %%mm6 \n\t"
"cmpl %%ecx, %%ebx \n\t"
"movq %%mm6, -8(%%edi,%%ebx,) \n\t" // (+56)movq does not affect flags;
// -8 to offset add ebx
"jb up_loop \n\t"
"cmpl $0, %%edx \n\t" // Test for bytes over mult of 64
"jz up_end \n\t"
// 2 lines added by lcreeve@netins.net
// (mail 11 Jul 98 in png-implement list)
"cmpl $8, %%edx \n\t" //test for less than 8 bytes
"jb up_lt8 \n\t"
"addl %%edx, %%ecx \n\t"
"andl $0x00000007, %%edx \n\t" // calc bytes over mult of 8
"subl %%edx, %%ecx \n\t" // drop over bytes from length
"jz up_lt8 \n\t"
// Loop using MMX registers mm0 & mm1 to update 8 bytes simultaneously
"up_lpA: \n\t"
"movq (%%esi,%%ebx,), %%mm1 \n\t"
"movq (%%edi,%%ebx,), %%mm0 \n\t"
"addl $8, %%ebx \n\t"
"paddb %%mm1, %%mm0 \n\t"
"cmpl %%ecx, %%ebx \n\t"
"movq %%mm0, -8(%%edi,%%ebx,) \n\t" // movq does not affect flags; -8 to offset add ebx
"jb up_lpA \n\t"
"cmpl $0, %%edx \n\t" // Test for bytes over mult of 8
"jz up_end \n\t"
"up_lt8: \n\t"
"xorl %%eax, %%eax \n\t"
"addl %%edx, %%ecx \n\t" // move over byte count into counter
// Loop using x86 registers to update remaining bytes
"up_lp2: \n\t"
"movb (%%edi,%%ebx,), %%al \n\t"
"addb (%%esi,%%ebx,), %%al \n\t"
"incl %%ebx \n\t"
"cmpl %%ecx, %%ebx \n\t"
"movb %%al, -1(%%edi,%%ebx,) \n\t" // mov does not affect flags; -1 to offset inc ebx
"jb up_lp2 \n\t"
"up_end: \n\t"
// Conversion of filtered row completed
"emms \n\t" // End MMX instructions; prep for possible FP instrs.
: // FIXASM: output regs/vars go here, e.g.: "=m" (memory_var)
: // FIXASM: input regs, e.g.: "c" (count), "S" (src), "D" (dest)
: "%eax", "%ebx", "%ecx", "%edx", "%edi", "%esi", "%mm0", "%mm1", "%mm2", "%mm3", "%mm4", "%mm5", "%mm6", "%mm7" // CHECKASM: clobber list
);
#endif /* GRR_GCC_MMX_CONVERTED */
}
#if defined(PNG_HAVE_ASSEMBLER_READ_FILTER_ROW)
// Optimized png_read_filter_row routines
void /* PRIVATE */
png_read_filter_row(png_structp png_ptr, png_row_infop row_info, png_bytep
row, png_bytep prev_row, int filter)
{
#ifdef PNG_DEBUG
char filnm[6];
#endif
#define UseMMX 1
if (mmx_supported == 2)
mmx_supported = mmxsupport();
#ifdef GRR_GCC_MMX_CONVERTED
if (!mmx_supported)
#endif
{
png_read_filter_row_c(png_ptr, row_info, row, prev_row, filter);
return ;
}
#ifdef PNG_DEBUG
png_debug(1, "in png_read_filter_row\n");
#if (UseMMX == 1)
png_debug1(0,"%s, ", "MMX");
#else
png_debug1(0,"%s, ", "x86");
#endif
switch (filter)
{
case 0: sprintf(filnm, "None ");
break;
case 1: sprintf(filnm, "Sub ");
break;
case 2: sprintf(filnm, "Up ");
break;
case 3: sprintf(filnm, "Avg ");
break;
case 4: sprintf(filnm, "Paeth");
break;
default: sprintf(filnm, "Unknw");
break;
}
png_debug2(0,"row=%5d, %s, ", png_ptr->row_number, filnm);
png_debug2(0, "pd=%2d, b=%d, ", (int)row_info->pixel_depth,
(int)((row_info->pixel_depth + 7) >> 3));
png_debug1(0,"len=%8d, ", row_info->rowbytes);
#endif
switch (filter)
{
case PNG_FILTER_VALUE_NONE:
break;
case PNG_FILTER_VALUE_SUB:
#if (UseMMX == 1)
if ((row_info->pixel_depth > 8) && (row_info->rowbytes >= 128))
{
png_read_filter_row_mmx_sub(row_info, row);
}
else
#endif
{
png_uint_32 i;
png_uint_32 istop = row_info->rowbytes;
png_uint_32 bpp = (row_info->pixel_depth + 7) >> 3;
png_bytep rp = row + bpp;
png_bytep lp = row;
for (i = bpp; i < istop; i++)
{
*rp = (png_byte)(((int)(*rp) + (int)(*lp++)) & 0xff);
rp++;
}
} //end !UseMMX
break;
case PNG_FILTER_VALUE_UP:
#if (UseMMX == 1)
if ((row_info->pixel_depth > 8) && (row_info->rowbytes >= 128))
{
png_read_filter_row_mmx_up(row_info, row, prev_row);
}
else
#endif
{
png_bytep rp;
png_bytep pp;
png_uint_32 i;
for (i = 0, rp = row, pp = prev_row;
i < row_info->rowbytes; i++, rp++, pp++)
{
*rp = (png_byte)(((int)(*rp) + (int)(*pp)) & 0xff);
}
} //end !UseMMX
break;
case PNG_FILTER_VALUE_AVG:
#if (UseMMX == 1)
if ((row_info->pixel_depth > 8) && (row_info->rowbytes >= 128))
{
png_read_filter_row_mmx_avg(row_info, row, prev_row);
}
else
#endif
{
png_uint_32 i;
png_bytep rp = row;
png_bytep pp = prev_row;
png_bytep lp = row;
png_uint_32 bpp = (row_info->pixel_depth + 7) >> 3;
png_uint_32 istop = row_info->rowbytes - bpp;
for (i = 0; i < bpp; i++)
{
*rp = (png_byte)(((int)(*rp) +
((int)(*pp++) >> 1)) & 0xff);
rp++;
}
for (i = 0; i < istop; i++)
{
*rp = (png_byte)(((int)(*rp) +
((int)(*pp++ + *lp++) >> 1)) & 0xff);
rp++;
}
} //end !UseMMX
break;
case PNG_FILTER_VALUE_PAETH:
#if (UseMMX == 1)
if ((row_info->pixel_depth > 8) && (row_info->rowbytes >= 128))
{
png_read_filter_row_mmx_paeth(row_info, row, prev_row);
}
else
#endif
{
png_uint_32 i;
png_bytep rp = row;
png_bytep pp = prev_row;
png_bytep lp = row;
png_bytep cp = prev_row;
png_uint_32 bpp = (row_info->pixel_depth + 7) >> 3;
png_uint_32 istop=row_info->rowbytes - bpp;
for (i = 0; i < bpp; i++)
{
*rp = (png_byte)(((int)(*rp) + (int)(*pp++)) & 0xff);
rp++;
}
for (i = 0; i < istop; i++) // use leftover rp,pp
{
int a, b, c, pa, pb, pc, p;
a = *lp++;
b = *pp++;
c = *cp++;
p = b - c;
pc = a - c;
#ifdef PNG_USE_ABS
pa = abs(p);
pb = abs(pc);
pc = abs(p + pc);
#else
pa = p < 0 ? -p : p;
pb = pc < 0 ? -pc : pc;
pc = (p + pc) < 0 ? -(p + pc) : p + pc;
#endif
/*
if (pa <= pb && pa <= pc)
p = a;
else if (pb <= pc)
p = b;
else
p = c;
*/
p = (pa <= pb && pa <=pc) ? a : (pb <= pc) ? b : c;
*rp = (png_byte)(((int)(*rp) + p) & 0xff);
rp++;
}
} //end !UseMMX
break;
default:
png_warning(png_ptr, "Ignoring bad adaptive filter type");
*row=0;
break;
}
}
#endif /* PNG_HAVE_ASSEMBLER_READ_FILTER_ROW */
// GRR NOTES: (1) the following code assumes 386 or better (pushfl/popfl)
// (2) all instructions compile with gcc 2.7.2.3 and later
// (3) the function is moved down here to prevent gcc from
// inlining it in multiple places and then barfing be-
// cause the ".NOT_SUPPORTED" label is multiply defined
// [is there a way to signal that a *single* function should
// not be inlined? is there a way to modify the label for
// each inlined instance, e.g., by appending _1, _2, etc.?
// maybe if don't use leading "." in label name? (not tested)]
#ifdef ORIG_THAT_USED_TO_CLOBBER_EBX
int mmxsupport(void)
{
int mmx_supported_local = 0;
__asm__ (
// ".byte 0x66 \n\t" // convert 16-bit pushf to 32-bit pushfd
// "pushf \n\t" // save Eflag to stack
"pushfl \n\t" // save Eflag to stack
"popl %%eax \n\t" // get Eflag from stack into eax
"movl %%eax, %%ecx \n\t" // make another copy of Eflag in ecx
"xorl $0x200000, %%eax \n\t" // toggle ID bit in Eflag (i.e., bit 21)
"pushl %%eax \n\t" // save modified Eflag back to stack
// ".byte 0x66 \n\t" // convert 16-bit popf to 32-bit popfd
// "popf \n\t" // restore modified value to Eflag reg
"popfl \n\t" // restore modified value to Eflag reg
"pushfl \n\t" // save Eflag to stack
"popl %%eax \n\t" // get Eflag from stack
"xorl %%ecx, %%eax \n\t" // compare new Eflag with original Eflag
"jz .NOT_SUPPORTED \n\t" // if same, CPUID instr. is not supported
"xorl %%eax, %%eax \n\t" // set eax to zero
// ".byte 0x0f, 0xa2 \n\t" // CPUID instruction (two-byte opcode)
"cpuid \n\t" // get the CPU identification info
"cmpl $1, %%eax \n\t" // make sure eax return non-zero value
"jl .NOT_SUPPORTED \n\t" // if eax is zero, MMX is not supported
"xorl %%eax, %%eax \n\t" // set eax to zero and...
"incl %%eax \n\t" // ...increment eax to 1. This pair is
// faster than the instruction "mov eax, 1"
"cpuid \n\t" // get the CPU identification info again
"andl $0x800000, %%edx \n\t" // mask out all bits but MMX bit (23)
"cmpl $0, %%edx \n\t" // 0 = MMX not supported
"jz .NOT_SUPPORTED \n\t" // non-zero = yes, MMX IS supported
"movl $1, %0 \n\t" // set return value to 1 and fall through
".NOT_SUPPORTED: \n\t" // target label for jump instructions
"movl %0, %%eax \n\t" // move return value to eax
// DONE
: "=m" (mmx_supported_local) // %0 (output list: memory only)
: // any variables used on input (none)
: "%eax", "%ebx", // clobber list
"%ecx", "%edx"
// , "memory" // if write to a variable gcc thought was in a reg
// , "cc" // "condition codes" (flag bits)
);
//mmx_supported_local=0; // test code for force don't support MMX
//printf("MMX : %u (1=MMX supported)\n",mmx_supported_local);
return mmx_supported_local;
}
#else /* !ORIG_THAT_USED_TO_CLOBBER_EBX */
int mmxsupport(void)
{
__asm__ (
"pushl %%ebx \n\t" // ebx gets clobbered by CPUID instruction
"pushl %%ecx \n\t" // so does ecx...
"pushl %%edx \n\t" // ...and edx (but ecx & edx safe on Linux)
// ".byte 0x66 \n\t" // convert 16-bit pushf to 32-bit pushfd
// "pushf \n\t" // save Eflag to stack
"pushfl \n\t" // save Eflag to stack
"popl %%eax \n\t" // get Eflag from stack into eax
"movl %%eax, %%ecx \n\t" // make another copy of Eflag in ecx
"xorl $0x200000, %%eax \n\t" // toggle ID bit in Eflag (i.e., bit 21)
"pushl %%eax \n\t" // save modified Eflag back to stack
// ".byte 0x66 \n\t" // convert 16-bit popf to 32-bit popfd
// "popf \n\t" // restore modified value to Eflag reg
"popfl \n\t" // restore modified value to Eflag reg
"pushfl \n\t" // save Eflag to stack
"popl %%eax \n\t" // get Eflag from stack
"xorl %%ecx, %%eax \n\t" // compare new Eflag with original Eflag
"jz .NOT_SUPPORTED \n\t" // if same, CPUID instr. is not supported
"xorl %%eax, %%eax \n\t" // set eax to zero
// ".byte 0x0f, 0xa2 \n\t" // CPUID instruction (two-byte opcode)
"cpuid \n\t" // get the CPU identification info
"cmpl $1, %%eax \n\t" // make sure eax return non-zero value
"jl .NOT_SUPPORTED \n\t" // if eax is zero, MMX is not supported
"xorl %%eax, %%eax \n\t" // set eax to zero and...
"incl %%eax \n\t" // ...increment eax to 1. This pair is
// faster than the instruction "mov eax, 1"
"cpuid \n\t" // get the CPU identification info again
"andl $0x800000, %%edx \n\t" // mask out all bits but MMX bit (23)
"cmpl $0, %%edx \n\t" // 0 = MMX not supported
"jz .NOT_SUPPORTED \n\t" // non-zero = yes, MMX IS supported
"movl $1, %%eax \n\t" // set return value to 1
"popl %%edx \n\t" // restore edx
"popl %%ecx \n\t" // restore ecx
"popl %%ebx \n\t" // restore ebx ("row" in png_do_interlace)
"ret \n\t" // DONE: have MMX support
".NOT_SUPPORTED: \n\t" // target label for jump instructions
"movl $0, %%eax \n\t" // set return value to 0
"popl %%edx \n\t" // restore edx
"popl %%ecx \n\t" // restore ecx
"popl %%ebx \n\t" // restore ebx ("row" in png_do_interlace)
// "ret \n\t" // DONE: no MMX support
// (fall through to standard C "ret")
: // "=m" (mmx_supported_local) // %0 (output list: memory only)
: // any variables used on input (none)
: "%eax" // clobber list
// , "%ebx", "%ecx", "%edx" // GRR: we handle these manually
// , "memory" // if write to a variable gcc thought was in a reg
// , "cc" // "condition codes" (flag bits)
);
//mmx_supported_local=0; // test code for force don't support MMX
//printf("MMX : %u (1=MMX supported)\n",mmx_supported_local);
//return mmx_supported_local;
}
#endif /* ?ORIG_THAT_USED_TO_CLOBBER_EBX */
#endif /* PNG_ASSEMBLER_CODE_SUPPORTED && PNG_USE_PNGGCCRD */
|