aboutsummaryrefslogtreecommitdiff
path: root/plugins/gpu_neon/psx_gpu/psx_gpu_arm_neon.S
blob: 7c820d273cecd041e709df8b0e0ad18a0391774b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
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
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
/*
 * Copyright (C) 2011 Gilead Kutnick "Exophase" <exophase@gmail.com>
 * Copyright (C) 2012 Gražvydas Ignotas "notaz" <notasas@gmail.com>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 */

#define MAX_SPANS                                         512
#define MAX_BLOCKS                                        64
#define MAX_BLOCKS_PER_ROW                                128

#define RENDER_STATE_MASK_EVALUATE                        0x20
#define RENDER_FLAGS_MODULATE_TEXELS                      0x1
#define RENDER_FLAGS_BLEND                                0x2
#define RENDER_INTERLACE_ENABLED                          0x1

#include "psx_gpu_offsets.h"

#define psx_gpu_b_dx_offset (psx_gpu_b_block_span_offset + 4)

#define edge_data_left_x_offset                           0
#define edge_data_num_blocks_offset                       2
#define edge_data_right_mask_offset                       4
#define edge_data_y_offset                                6

.syntax unified
.text

#define psx_gpu                                           r0
#define v_a                                               r1
#define v_b                                               r2
#define v_c                                               r3

#define x0                                                r4
#define x1                                                r5
#define x2                                                r6
#define x0_x1                                             r5
#define x1_x2                                             r6
#define y0                                                r7
#define y1                                                r8
#define y2                                                r9
#define y0_y1                                             r7
#define y1_y2                                             r8
#define b0                                                r9
#define b1                                                r10
#define b2                                                r11
#define b0_b1                                             r10
#define b1_b2                                             r11


#define area_r_s                                          r5

#define g_bx0                                             r2
#define g_bx                                              r3
#define g_bx2                                             r4
#define g_bx3                                             r5
#define b_base                                            r6
#define g_by                                              r8

#define gs_bx                                             r7
#define gs_by                                             r10

#define ga_bx                                             g_bx
#define ga_by                                             g_by

#define gw_bx_h                                           g_bx
#define gw_by_h                                           g_by

#define gw_bx_l                                           r11
#define gw_by_l                                           gw_bx_l

#define store_a                                           r0
#define store_b                                           r1
#define store_inc                                         r5


#define v0                                                q0
#define uvrgb0                                            d0
#define x0_y0                                             d1

#define v1                                                q1
#define uvrgb1                                            d2
#define x1_y1                                             d3

#define v2                                                q2
#define uvrgb2                                            d4
#define x2_y2                                             d5

#define x0_ab                                             q3
#define uvrg_xxxx0                                        q3
#define uvrg0                                             d6
#define xxxx0                                             d7

#define x1_ab                                             q4
#define uvrg_xxxx1                                        q4
#define uvrg1                                             d8
#define xxxx1                                             d9

#define x2_ab                                             q5
#define uvrg_xxxx2                                        q5
#define uvrg2                                             d10
#define xxxx2                                             d11

#define y0_ab                                             q6
#define yyyy_uvrg0                                        q6
#define yyyy0                                             d12
#define uvrg0b                                            d13

#define y1_ab                                             q7
#define yyyy_uvrg1                                        q7
#define yyyy1                                             d14
#define uvrg1b                                            d15

#define y2_ab                                             q8
#define yyyy_uvrg2                                        q8
#define yyyy2                                             d16
#define uvrg2b                                            d17

#define d0_ab                                             q9
#define d0_a                                              d18
#define d0_b                                              d19

#define d1_ab                                             q10
#define d1_a                                              d20
#define d1_b                                              d21

#define d2_ab                                             q11
#define d2_a                                              d22
#define d2_b                                              d23

#define d3_ab                                             q12
#define d3_a                                              d24
#define d3_b                                              d25

#define ga_uvrg_x                                         q1
#define ga_uvrg_y                                         q4

#define dx                                                x0_x1
#define dy                                                y0_y1
#define db                                                b0_b1

#define uvrg_base                                         q11

#define gs_uvrg_x                                         q5
#define gs_uvrg_y                                         q6

#define g_uvrg_x                                          q1
#define ga_uv_x                                           d2
#define g_uv_x                                            d2
#define ga_rg_x                                           d3
#define g_rg_x                                            d3

#define g_uvrg_y                                          q4
#define ga_uv_y                                           d8
#define g_uv_y                                            d8
#define ga_rg_y                                           d9
#define g_rg_y                                            d9

#define gw_uv_x                                           q1
#define gw_rg_x                                           q2
#define gw_uv_y                                           q4
#define gw_rg_y                                           q3

#define w_mask                                            q9
#define w_mask_l                                          d18

#define r_shift                                           q10

#define uvrg_dx0                                          q0
#define uvrg_dx0l                                         d0
#define uvrg_dx0h                                         d1

#define uvrg_dx1                                          q1
#define uvrg_dx1l                                         d2
#define uvrg_dx1h                                         d3

#define uvrg_dx2                                          q2
#define uvrg_dx2l                                         d4
#define uvrg_dx2h                                         d5

#define uvrg_dx3                                          q3
#define uvrg_dx3l                                         d6
#define uvrg_dx3h                                         d7

#define uvrgb_phase                                       q13

.align 4

#include "arm_features.h"

#define function(name) FUNCTION(name):

#ifndef TEXRELS_FORBIDDEN

#define JT_OP_REL(table_label, index_reg, temp)
#define JT_OP(x...) x
#define JTE(start, target) target

#else

#define JT_OP_REL(table_label, index_reg, temp)                                \
  adr temp, table_label;                                                       \
  ldr temp, [temp, index_reg, lsl #2];                                         \
  add pc, pc, temp                                                             \

#define JT_OP(x...)
#define JTE(start, target) (target - start)

#endif

#ifdef __MACH__
#define flush_render_block_buffer _flush_render_block_buffer
#define setup_sprite_untextured_simple _setup_sprite_untextured_simple
#define update_texture_8bpp_cache _update_texture_8bpp_cache
#endif

@ r0: psx_gpu
@ r1: v_a
@ r2: v_b
@ r3: v_c

function(compute_all_gradients)
  // First compute the triangle area reciprocal and shift. The division will
  // happen concurrently with much of the work which follows.
  @ r12 = psx_gpu->triangle_area
  ldr r12, [psx_gpu, #psx_gpu_triangle_area_offset]
  stmdb sp!, { r4 - r11, lr }

  @ load exponent of 62 into upper half of double
  movw r4, #0
  clz r14, r12                       @ r14 = shift

  movt r4, #((62 + 1023) << 4)
  mov r12, r12, lsl r14              @ r12 = triangle_area_normalized

  @ load area normalized into lower half of double
  mov r5, r12, lsr #10
  vmov.f64 d30, r5, r4               @ d30 = (1 << 62) + ta_n

  movt r4, #((1022 + 31) << 4)
  mov r5, r12, lsl #20

  add r4, r4, r12, lsr #11
  vmov.f64 d31, r5, r4

  vdiv.f64 d30, d30, d31             @ d30 = ((1 << 62) + ta_n) / ta_n

  // ((x1 - x0) * (y2 - y1)) - ((x2 - x1) * (y1 - y0)) =
  // ( d0       *  d1      ) - ( d2       *  d3      ) =
  // ( m0                  ) - ( m1                  ) = gradient

  // This is split to do 12 elements at a time over three sets: a, b, and c.
  // Technically we only need to do 10 elements (uvrgb_x and uvrgb_y), so
  // two of the slots are unused.

  // Inputs are all 16-bit signed. The m0/m1 results are 32-bit signed, as
  // is g.

  // First type is:  uvrg bxxx xxxx 
  // Second type is: yyyy ybyy uvrg 
  // Since x_a and y_c are the same the same variable is used for both. 

  vld1.u32 { v0 }, [v_a, :128]       @ v0 = { uvrg0, b0, x0, y0 }
  ldrsh x0, [v_a, #8]                @ load x0

  vld1.u32 { v1 }, [v_b, :128]       @ v1 = { uvrg1, b1, x1, y1}
  ldrh x1, [v_b, #8]                 @ load x1

  vld1.u32 { v2 }, [v_c, :128]       @ v2 = { uvrg2, b2, x2, y2 }
  ldrh x2, [v_c, #8]                 @ load x2

  vmovl.u8 uvrg_xxxx0, uvrgb0        @ uvrg_xxxx0 = { uv0, rg0, b0-, -- }
  ldrh y0, [v_a, #10]                @ load y0

  vmovl.u8 uvrg_xxxx1, uvrgb1        @ uvrg_xxxx1 = { uv1, rg1, b1-, -- }
  ldrh y1, [v_b, #10]                @ load y1

  vmovl.u8 uvrg_xxxx2, uvrgb2        @ uvrg_xxxx2 = { uv2, rg2, b2-, -- }
  ldrh y2, [v_c, #10]                @ load y2

  vmov.u8 uvrg0b, uvrg0              @ uvrg0b = { uv0, rg0 }
  vdup.u16 xxxx0, x0_y0[0]           @ xxxx0 = { xx0, xx0 }

  orr x1_x2, x1, x2, lsl #16         @ x1_x2 = { x1, x2 }
  pkhbt x0_x1, x0, x1, lsl #16       @ x0_x1 = { x0, x1 }

  vmov.u8 uvrg1b, uvrg1              @ uvrg1b = { uv1, rg1 }
  vdup.u16 xxxx1, x1_y1[0]           @ xxxx1 = { xx1, xx1 }

  vmov.u8 uvrg2b, uvrg2              @ uvrg2b = { uv2, rg2 }
  vdup.u16 xxxx2, x2_y2[0]           @ xxxx2 = { xx2, xx2 }

  ldrb b2, [v_c, #4]                 @ load b2
  orr y0_y1, y0, y1, lsl #16         @ y0_y1 = { y0, y1 }

  ldrb b1, [v_b, #4]                 @ load b1
  orr y1_y2, y1, y2, lsl #16         @ y1_y2 = { y1, y2 }

  vdup.u16 yyyy0, x0_y0[1]           @ yyyy0 = { yy0, yy0 }
  vsub.s16 d0_ab, x1_ab, x0_ab

  ldrb b0, [v_a, #4]                 @ load b0
  orr b1_b2, b1, b2, lsl #16         @ b1_b2 = { b1, b2 }

  vdup.u16 yyyy1, x1_y1[1]           @ yyyy1 = { yy1, yy1 }
  vsub.s16 d2_ab, x2_ab, x1_ab

  vdup.u16 yyyy2, x2_y2[1]           @ yyyy2 = { yy2, yy2 }
  vsub.s16 d1_ab, y2_ab, y1_ab

  orr b0_b1, b0, b1, lsl #16         @ b1_b2 = { b1, b2 }
  ssub16 dx, x1_x2, x0_x1            @ dx = { x1 - x0, x2 - x1 }

  ssub16 dy, y1_y2, y0_y1            @ dy = { y1 - y0, y2 - y1 }
  ssub16 db, b1_b2, b0_b1            @ db = { b1 - b0, b2 - b1 }

  vsub.s16 d3_ab, y1_ab, y0_ab
  smusdx ga_by, dx, db               @ ga_by = ((x1 - x0) * (b2 - b1)) -
                                     @         ((x2 - X1) * (b1 - b0)) 
  vmull.s16 ga_uvrg_x, d0_a, d1_a
  smusdx ga_bx, db, dy               @ ga_bx = ((b1 - b0) * (y2 - y1)) -
                                     @         ((b2 - b1) * (y1 - y0))
  vmlsl.s16 ga_uvrg_x, d2_a, d3_a
  movs gs_bx, ga_bx, asr #31

  vmull.s16 ga_uvrg_y, d0_b, d1_b
  rsbmi ga_bx, ga_bx, #0

  @ r12 = psx_gpu->uvrgb_phase
  ldr r12, [psx_gpu, #psx_gpu_uvrgb_phase_offset]

  vmlsl.s16 ga_uvrg_y, d2_b, d3_b
  movs gs_by, ga_by, asr #31

  vshr.u64 d0, d30, #22
  add b_base, r12, b0, lsl #16

  vdup.u32 uvrgb_phase, r12

  rsbmi ga_by, ga_by, #0
  vclt.s32 gs_uvrg_x, ga_uvrg_x, #0  @ gs_uvrg_x = ga_uvrg_x < 0

  @ r12 = psx_gpu->triangle_winding_offset
  ldrb r12, [psx_gpu, #psx_gpu_triangle_winding_offset]
  vclt.s32 gs_uvrg_y, ga_uvrg_y, #0  @ gs_uvrg_y = ga_uvrg_y < 0

  rsb r12, r12, #0                   @ r12 = -(triangle->winding)

  vdup.u32 w_mask, r12               @ w_mask = { -w, -w, -w, -w }
  sub r14, r14, #(62 - 12)           @ r14 = shift - (62 - FIXED_BITS)

  vshll.u16 uvrg_base, uvrg0, #16    @ uvrg_base = uvrg0 << 16
  vdup.u32 r_shift, r14              @ r_shift = { shift, shift, shift, shift }

  vadd.u32 uvrg_base, uvrgb_phase
  vabs.s32 ga_uvrg_x, ga_uvrg_x      @ ga_uvrg_x = abs(ga_uvrg_x)

  vmov area_r_s, s0                  @ area_r_s = triangle_reciprocal
  vabs.s32 ga_uvrg_y, ga_uvrg_y      @ ga_uvrg_y = abs(ga_uvrg_y)

  vmull.u32 gw_rg_x, ga_rg_x, d0[0]
  vmull.u32 gw_uv_x, ga_uv_x, d0[0]
  vmull.u32 gw_rg_y, ga_rg_y, d0[0]
  vmull.u32 gw_uv_y, ga_uv_y, d0[0]

  vshl.u64 gw_rg_x, gw_rg_x, r_shift
  vshl.u64 gw_uv_x, gw_uv_x, r_shift
  vshl.u64 gw_rg_y, gw_rg_y, r_shift
  vshl.u64 gw_uv_y, gw_uv_y, r_shift

  veor.u32 gs_uvrg_x, gs_uvrg_x, w_mask
  vmovn.u64 g_uv_x, gw_uv_x

  veor.u32 gs_uvrg_y, gs_uvrg_y, w_mask
  vmovn.u64 g_rg_x, gw_rg_x

  veor.u32 g_uvrg_x, g_uvrg_x, gs_uvrg_x
  vmovn.u64 g_uv_y, gw_uv_y

  vsub.u32 g_uvrg_x, g_uvrg_x, gs_uvrg_x
  vmovn.u64 g_rg_y, gw_rg_y

  veor.u32 g_uvrg_y, g_uvrg_y, gs_uvrg_y
  mov ga_bx, ga_bx, lsl #13

  vsub.u32 g_uvrg_y, g_uvrg_y, gs_uvrg_y
  mov ga_by, ga_by, lsl #13

  vdup.u32 x0_y0, x0
  umull gw_bx_l, gw_bx_h, ga_bx, area_r_s

  vshl.u32 g_uvrg_x, g_uvrg_x, #4
  vshl.u32 g_uvrg_y, g_uvrg_y, #4

  umull gw_by_l, gw_by_h, ga_by, area_r_s
  vmls.s32 uvrg_base, ga_uvrg_x, x0_y0[0]

  eor gs_bx, gs_bx, r12
  vadd.u32 uvrg_dx2, uvrg_dx1, uvrg_dx1

  veor.u32 uvrg_dx0, uvrg_dx0, uvrg_dx0
  eor gs_by, gs_by, r12

  rsb r11, r14, #0                   @ r11 = negative shift for scalar lsr
  add store_a, psx_gpu, #psx_gpu_uvrg_offset

  sub r11, r11, #(32 - 13)

  add store_b, store_a, #16
  mov store_inc, #32

  vadd.u32 uvrg_dx3, uvrg_dx2, uvrg_dx1
  vst1.u32 { uvrg_base }, [store_a, :128], store_inc

  vst1.u32 { uvrg_dx1 }, [store_b, :128], store_inc
  mov g_bx, gw_bx_h, lsr r11

  vst1.u32 { g_uvrg_y }, [store_a, :128], store_inc
  mov g_by, gw_by_h, lsr r11

  vst4.u32 { uvrg_dx0l, uvrg_dx1l, uvrg_dx2l, uvrg_dx3l },                     \
   [store_b, :128], store_inc
  eor g_bx, g_bx, gs_bx

  vst4.u32 { uvrg_dx0h, uvrg_dx1h, uvrg_dx2h, uvrg_dx3h },                     \
   [store_b, :128], store_inc
  sub g_bx, g_bx, gs_bx

  lsl g_bx, g_bx, #4  
  eor g_by, g_by, gs_by

  mls b_base, g_bx, x0, b_base
  sub g_by, g_by, gs_by

  lsl g_by, g_by, #4
  mov g_bx0, #0

  add g_bx2, g_bx, g_bx
  add g_bx3, g_bx, g_bx2

  stmia store_b, { g_bx0, g_bx, g_bx2, g_bx3, b_base, g_by }

  ldmia sp!, { r4 - r11, pc }


#define psx_gpu                                  r0
#define v_a                                      r1
#define v_b                                      r2
#define v_c                                      r3

#define temp                                     r14

#define x_a                                      r4
#define x_b                                      r5
#define x_c                                      r6
#define y_a                                      r1
#define y_b                                      r2
#define y_c                                      r3

#define height_minor_a                           r7
#define height_minor_b                           r8
#define height_major                             r9
#define height                                   r9

#define reciprocal_table_ptr                     r10

#define edge_alt_low                             r4
#define edge_alt_high                            r5
#define edge_dx_dy_alt                           r6
#define edge_shift_alt                           r10

#define edge_dx_dy_alt_low                       r4
#define edge_dx_dy_alt_high                      r5

#define span_edge_data                           r4
#define span_uvrg_offset                         r5
#define span_b_offset                            r6

#define clip                                     r14

#define b                                        r11
#define b_dy                                     r12


#define alternate_x                              q0
#define alternate_dx_dy                          q1
#define alternate_x_32                           q2

#define alternate_x_low                          d0
#define alternate_x_high                         d1
#define alternate_dx_dy_low                      d2
#define alternate_dx_dy_high                     d3
#define alternate_x_32_low                       d4
#define alternate_x_32_high                      d5

#define left_x                                   q3
#define right_x                                  q4
#define left_dx_dy                               q5
#define right_dx_dy                              q6
#define left_edge                                q7
#define right_edge                               q8

#define left_x_low                               d6
#define left_x_high                              d7
#define right_x_low                              d8
#define right_x_high                             d9
#define left_dx_dy_low                           d10
#define left_dx_dy_high                          d11
#define right_dx_dy_low                          d12
#define right_dx_dy_high                         d13
#define left_edge_low                            d14
#define left_edge_high                           d15
#define right_edge_low                           d16
#define right_edge_high                          d17

#define y_mid_point                              d18
#define c_0x0004                                 d19

#define left_right_x_16                          q11
#define span_shifts_y                            q12
#define c_0x0001                                 q13

#define span_shifts                              d24
#define y_x4                                     d25
#define c_0xFFFE                                 d26
#define c_0x0007                                 d27

#define left_right_x_16_low                      d22
#define left_right_x_16_high                     d23

#define uvrg                                     q14
#define uvrg_dy                                  q15

#define alternate_x_16                           d4

#define v_clip                                   q3
#define v_clip_low                               d6

#define right_x_32                               q10
#define left_x_32                                q11
#define alternate_select                         d24

#define right_x_32_low                           d20
#define right_x_32_high                          d21
#define left_x_32_low                            d22
#define left_x_32_high                           d23

#define edges_xy                                 q0
#define edges_dx_dy                              d2
#define edge_shifts                              d3
#define edge_shifts_64                           q2

#define edges_xy_left                            d0
#define edges_xy_right                           d1

#define height_reciprocals                       d6
#define heights                                  d7

#define widths                                   d8
#define c_0x01                                   d9
#define x_starts                                 d10
#define x_ends                                   d11

#define heights_b                                d12
#define edges_dx_dy_64                           q10

#define edges_dx_dy_64_left                      d20
#define edges_dx_dy_64_right                     d21


#define setup_spans_prologue()                                                 \
  stmdb sp!, { r4 - r11, lr };                                                 \
                                                                               \
  ldrsh x_a, [v_a, #8];                                                        \
  ldrsh x_b, [v_b, #8];                                                        \
  ldrsh x_c, [v_c, #8];                                                        \
  ldrsh y_a, [v_a, #10];                                                       \
  ldrsh y_b, [v_b, #10];                                                       \
  ldrsh y_c, [v_c, #10];                                                       \
                                                                               \
  add temp, psx_gpu, #psx_gpu_uvrg_offset;                                     \
  vld1.32 { uvrg }, [temp];                                                    \
  add temp, psx_gpu, #psx_gpu_uvrg_dy_offset;                                  \
  vld1.32 { uvrg_dy }, [temp];                                                 \
  ldr reciprocal_table_ptr, [psx_gpu, #psx_gpu_reciprocal_table_ptr_offset];   \
                                                                               \
  vmov.u32 c_0x01, #0x01                                                       \

#define setup_spans_load_b()                                                   \
  ldr b, [psx_gpu, #psx_gpu_b_offset];                                         \
  ldr b_dy, [psx_gpu, #psx_gpu_b_dy_offset]                                    \

#define setup_spans_prologue_b()                                               \
  add span_uvrg_offset, psx_gpu, #psx_gpu_span_uvrg_offset_offset;             \
  add temp, psx_gpu, #psx_gpu_viewport_start_x_offset;                         \
                                                                               \
  add span_edge_data, psx_gpu, #psx_gpu_span_edge_data_offset;                 \
  vmov.u16 c_0x0004, #0x0004;                                                  \
                                                                               \
  add span_b_offset, psx_gpu, #psx_gpu_span_b_offset_offset;                   \
  vmov.u16 c_0x0001, #0x0001;                                                  \
                                                                               \
  vld1.u16 { left_edge_low[], left_edge_high[] }, [temp];                      \
  add temp, psx_gpu, #psx_gpu_viewport_end_x_offset;                           \
                                                                               \
  vld1.u16 { right_edge_low[], right_edge_high[] }, [temp];                    \
  vadd.u16 right_edge, right_edge, c_0x0001;                                   \
                                                                               \
  vmov.u16 c_0x0007, #0x0007;                                                  \
  vmvn.u16 c_0xFFFE, #0x0001                                                   \


#define compute_edge_delta_x2()                                                \
  ldr temp, [reciprocal_table_ptr, height, lsl #2];                            \
                                                                               \
  vdup.u32 heights, height;                                                    \
  vsub.u32 widths, x_ends, x_starts;                                           \
                                                                               \
  vdup.u32 edge_shifts, temp;                                                  \
  vsub.u32 heights_b, heights, c_0x01;                                         \
  vshr.u32 height_reciprocals, edge_shifts, #10;                               \
                                                                               \
  vmla.s32 heights_b, x_starts, heights;                                       \
  vbic.u16 edge_shifts, #0xE0;                                                 \
  vmul.s32 edges_dx_dy, widths, height_reciprocals;                            \
  vmull.s32 edges_xy, heights_b, height_reciprocals                            \

#define width_alt                 r6
#define height_reciprocal_alt     r11
#define height_b_alt              r12

#define compute_edge_delta_x3(start_c, height_a, height_b)                     \
  vmov heights, height_a, height_b;                                            \
  ldr temp, [reciprocal_table_ptr, height_a, lsl #2];                          \
  vmov.u32 edge_shifts[0], temp;                                               \
  ldr temp, [reciprocal_table_ptr, height_b, lsl #2];                          \
  vmov.u32 edge_shifts[1], temp;                                               \
  ldr edge_shift_alt, [reciprocal_table_ptr, height_minor_b, lsl #2];          \
                                                                               \
  vsub.u32 widths, x_ends, x_starts;                                           \
  sub width_alt, x_c, start_c;                                                 \
                                                                               \
  vsub.u32 heights_b, heights, c_0x01;                                         \
  sub height_b_alt, height_minor_b, #1;                                        \
                                                                               \
  vshr.u32 height_reciprocals, edge_shifts, #10;                               \
  lsr height_reciprocal_alt, edge_shift_alt, #10;                              \
                                                                               \
  vmla.s32 heights_b, x_starts, heights;                                       \
  mla height_b_alt, height_minor_b, start_c, height_b_alt;                     \
                                                                               \
  vbic.u16 edge_shifts, #0xE0;                                                 \
  and edge_shift_alt, edge_shift_alt, #0x1F;                                   \
                                                                               \
  vmul.s32 edges_dx_dy, widths, height_reciprocals;                            \
  mul edge_dx_dy_alt, width_alt, height_reciprocal_alt;                        \
                                                                               \
  vmull.s32 edges_xy, heights_b, height_reciprocals;                           \
  smull edge_alt_low, edge_alt_high, height_b_alt, height_reciprocal_alt       \


#define setup_spans_adjust_y_up()                                              \
  vsub.u32 y_x4, y_x4, c_0x0004                                                \

#define setup_spans_adjust_y_down()                                            \
  vadd.u32 y_x4, y_x4, c_0x0004                                                \

#define setup_spans_adjust_interpolants_up()                                   \
  vsub.u32 uvrg, uvrg, uvrg_dy;                                                \
  sub b, b, b_dy                                                               \

#define setup_spans_adjust_interpolants_down()                                 \
  vadd.u32 uvrg, uvrg, uvrg_dy;                                                \
  add b, b, b_dy                                                               \


#define setup_spans_clip_interpolants_increment()                              \
  mla b, b_dy, clip, b;                                                        \
  vmla.s32 uvrg, uvrg_dy, v_clip                                               \

#define setup_spans_clip_interpolants_decrement()                              \
  mls b, b_dy, clip, b;                                                        \
  vmls.s32 uvrg, uvrg_dy, v_clip                                               \

#define setup_spans_clip_alternate_yes()                                       \
  smlal edge_alt_low, edge_alt_high, edge_dx_dy_alt, clip                      \

#define setup_spans_clip_alternate_no()                                        \

#define setup_spans_clip(direction, alternate_active)                          \
  vdup.u32 v_clip, clip;                                                       \
  setup_spans_clip_alternate_##alternate_active();                             \
  setup_spans_clip_interpolants_##direction();                                 \
  vmlal.s32 edges_xy, edges_dx_dy, v_clip_low                                  \


#define setup_spans_adjust_edges_alternate_no(left_index, right_index)         \
  vmovl.s32 edge_shifts_64, edge_shifts;                                       \
  vmovl.s32 edges_dx_dy_64, edges_dx_dy;                                       \
                                                                               \
  vshl.s64 edges_xy, edges_xy, edge_shifts_64;                                 \
  vshl.s64 edges_dx_dy_64, edges_dx_dy_64, edge_shifts_64;                     \
                                                                               \
  vmov left_x_low, edges_xy_##left_index;                                      \
  vmov right_x_low, edges_xy_##right_index;                                    \
                                                                               \
  vmov left_dx_dy_low, edges_dx_dy_64_##left_index;                            \
  vmov left_dx_dy_high, edges_dx_dy_64_##left_index;                           \
  vmov right_dx_dy_low, edges_dx_dy_64_##right_index;                          \
  vmov right_dx_dy_high, edges_dx_dy_64_##right_index;                         \
                                                                               \
  vadd.u64 left_x_high, left_x_low, left_dx_dy_low;                            \
  vadd.u64 right_x_high, right_x_low, right_dx_dy_low;                         \
                                                                               \
  vadd.u64 left_dx_dy, left_dx_dy, left_dx_dy;                                 \
  vadd.u64 right_dx_dy, right_dx_dy, right_dx_dy                               \


#define setup_spans_adjust_edges_alternate_yes(left_index, right_index)        \
  setup_spans_adjust_edges_alternate_no(left_index, right_index);              \
                                                                               \
  vdup.u16 y_mid_point, y_b;                                                   \
  rsb temp, edge_shift_alt, #32;                                               \
                                                                               \
  lsl edge_alt_high, edge_alt_high, edge_shift_alt;                            \
  orr edge_alt_high, edge_alt_high, edge_alt_low, lsr temp;                    \
  lsl edge_alt_low, edge_alt_low, edge_shift_alt;                              \
  vmov alternate_x_low, edge_alt_low, edge_alt_high;                           \
                                                                               \
  asr edge_dx_dy_alt_high, edge_dx_dy_alt, temp;                               \
  lsl edge_dx_dy_alt_low, edge_dx_dy_alt, edge_shift_alt;                      \
  vmov alternate_dx_dy_low, edge_dx_dy_alt_low, edge_dx_dy_alt_high;           \
  vmov alternate_dx_dy_high, alternate_dx_dy_low;                              \
                                                                               \
  vadd.u64 alternate_x_high, alternate_x_low, alternate_dx_dy_low;             \
  vadd.u64 alternate_dx_dy, alternate_dx_dy, alternate_dx_dy                   \


#define setup_spans_y_select_up()                                              \
  vclt.s16 alternate_select, y_x4, y_mid_point                                 \

#define setup_spans_y_select_down()                                            \
  vcgt.s16 alternate_select, y_x4, y_mid_point                                 \


#define setup_spans_alternate_select_left()                                    \
  vbit.u16 left_right_x_16_low, alternate_x_16, alternate_select               \

#define setup_spans_alternate_select_right()                                   \
  vbit.u16 left_right_x_16_high, alternate_x_16, alternate_select              \


#define setup_spans_set_x4_alternate_yes(alternate, direction)                 \
  vshrn.s64 alternate_x_32_low, alternate_x, #32;                              \
  vshrn.s64 left_x_32_low, left_x, #32;                                        \
  vshrn.s64 right_x_32_low, right_x, #32;                                      \
                                                                               \
  vadd.u64 alternate_x, alternate_x, alternate_dx_dy;                          \
  vadd.u64 left_x, left_x, left_dx_dy;                                         \
  vadd.u64 right_x, right_x, right_dx_dy;                                      \
                                                                               \
  vshrn.s64 alternate_x_32_high, alternate_x, #32;                             \
  vshrn.s64 left_x_32_high, left_x, #32;                                       \
  vshrn.s64 right_x_32_high, right_x, #32;                                     \
                                                                               \
  vadd.u64 alternate_x, alternate_x, alternate_dx_dy;                          \
  vadd.u64 left_x, left_x, left_dx_dy;                                         \
  vadd.u64 right_x, right_x, right_dx_dy;                                      \
                                                                               \
  vmovn.u32 alternate_x_16, alternate_x_32;                                    \
  setup_spans_y_select_##direction();                                          \
  vmovn.u32 left_right_x_16_low, left_x_32;                                    \
                                                                               \
  vmovn.u32 left_right_x_16_high, right_x_32;                                  \
  setup_spans_alternate_select_##alternate();                                  \
                                                                               \
  vst1.u32 { uvrg }, [span_uvrg_offset, :128]!;                                \
  str b, [span_b_offset], #4;                                                  \
  setup_spans_adjust_interpolants_##direction();                               \
                                                                               \
  vmax.s16 left_right_x_16, left_right_x_16, left_edge;                        \
                                                                               \
  vst1.u32 { uvrg }, [span_uvrg_offset, :128]!;                                \
  str b, [span_b_offset], #4;                                                  \
  setup_spans_adjust_interpolants_##direction();                               \
                                                                               \
  vmin.s16 left_right_x_16, left_right_x_16, right_edge;                       \
                                                                               \
  vst1.u32 { uvrg }, [span_uvrg_offset, :128]!;                                \
  str b, [span_b_offset], #4;                                                  \
  setup_spans_adjust_interpolants_##direction();                               \
                                                                               \
  vsub.u16 left_right_x_16_high, left_right_x_16_high, left_right_x_16_low;    \
  vadd.u16 left_right_x_16_high, left_right_x_16_high, c_0x0007;               \
  vand.u16 span_shifts, left_right_x_16_high, c_0x0007;                        \
                                                                               \
  vst1.u32 { uvrg }, [span_uvrg_offset, :128]!;                                \
  str b, [span_b_offset], #4;                                                  \
  setup_spans_adjust_interpolants_##direction();                               \
                                                                               \
  vshr.u16 left_right_x_16_high, left_right_x_16_high, #3;                     \
  vshl.u16 span_shifts, c_0xFFFE, span_shifts;                                 \
                                                                               \
  vst4.u16 { left_right_x_16, span_shifts_y }, [span_edge_data]!;              \
                                                                               \
  setup_spans_adjust_y_##direction()                                           \


#define setup_spans_set_x4_alternate_no(alternate, direction)                  \
  vshrn.s64 left_x_32_low, left_x, #32;                                        \
  vshrn.s64 right_x_32_low, right_x, #32;                                      \
                                                                               \
  vadd.u64 left_x, left_x, left_dx_dy;                                         \
  vadd.u64 right_x, right_x, right_dx_dy;                                      \
                                                                               \
  vshrn.s64 left_x_32_high, left_x, #32;                                       \
  vshrn.s64 right_x_32_high, right_x, #32;                                     \
                                                                               \
  vadd.u64 left_x, left_x, left_dx_dy;                                         \
  vadd.u64 right_x, right_x, right_dx_dy;                                      \
                                                                               \
  vmovn.u32 left_right_x_16_low, left_x_32;                                    \
  vmovn.u32 left_right_x_16_high, right_x_32;                                  \
                                                                               \
  vst1.u32 { uvrg }, [span_uvrg_offset, :128]!;                                \
  str b, [span_b_offset], #4;                                                  \
  setup_spans_adjust_interpolants_##direction();                               \
                                                                               \
  vmax.s16 left_right_x_16, left_right_x_16, left_edge;                        \
                                                                               \
  vst1.u32 { uvrg }, [span_uvrg_offset, :128]!;                                \
  str b, [span_b_offset], #4;                                                  \
  setup_spans_adjust_interpolants_##direction();                               \
                                                                               \
  vmin.s16 left_right_x_16, left_right_x_16, right_edge;                       \
                                                                               \
  vst1.u32 { uvrg }, [span_uvrg_offset, :128]!;                                \
  str b, [span_b_offset], #4;                                                  \
  setup_spans_adjust_interpolants_##direction();                               \
                                                                               \
  vsub.u16 left_right_x_16_high, left_right_x_16_high, left_right_x_16_low;    \
  vadd.u16 left_right_x_16_high, left_right_x_16_high, c_0x0007;               \
  vand.u16 span_shifts, left_right_x_16_high, c_0x0007;                        \
                                                                               \
  vst1.u32 { uvrg }, [span_uvrg_offset, :128]!;                                \
  str b, [span_b_offset], #4;                                                  \
  setup_spans_adjust_interpolants_##direction();                               \
                                                                               \
  vshl.u16 span_shifts, c_0xFFFE, span_shifts;                                 \
  vshr.u16 left_right_x_16_high, left_right_x_16_high, #3;                     \
                                                                               \
  vst4.u16 { left_right_x_16, span_shifts_y }, [span_edge_data]!;              \
                                                                               \
  setup_spans_adjust_y_##direction()                                           \


#define edge_adjust_low           r11
#define edge_adjust_high          r12

#define setup_spans_alternate_adjust_yes()                                     \
  smull edge_adjust_low, edge_adjust_high, edge_dx_dy_alt, height_minor_a;     \
  subs edge_alt_low, edge_alt_low, edge_adjust_low;                            \
  sbc edge_alt_high, edge_alt_high, edge_adjust_high                           \

#define setup_spans_alternate_adjust_no()                                      \


#define setup_spans_down(left_index, right_index, alternate, alternate_active) \
  setup_spans_alternate_adjust_##alternate_active();                           \
  setup_spans_load_b();                                                        \
                                                                               \
  ldrsh temp, [psx_gpu, #psx_gpu_viewport_end_y_offset];                       \
  subs y_c, y_c, temp;                                                         \
  subgt height, height, y_c;                                                   \
  addgt height, height, #1;                                                    \
                                                                               \
  ldrsh temp, [psx_gpu, #psx_gpu_viewport_start_y_offset];                     \
  subs clip, temp, y_a;                                                        \
  ble 0f;                                                                      \
                                                                               \
  sub height, height, clip;                                                    \
  add y_a, y_a, clip;                                                          \
  setup_spans_clip(increment, alternate_active);                               \
                                                                               \
 0:                                                                            \
  cmp height, #0;                                                              \
  ble 1f;                                                                      \
                                                                               \
  orr temp, y_a, y_a, lsl #16;                                                 \
  add temp, temp, #(1 << 16);                                                  \
  add y_a, temp, #2;                                                           \
  add y_a, y_a, #(2 << 16);                                                    \
  vmov y_x4, temp, y_a;                                                        \
                                                                               \
  setup_spans_adjust_edges_alternate_##alternate_active(left_index,            \
   right_index);                                                               \
  setup_spans_prologue_b();                                                    \
                                                                               \
  strh height, [psx_gpu, #psx_gpu_num_spans_offset];                           \
                                                                               \
 2:                                                                            \
  setup_spans_set_x4_alternate_##alternate_active(alternate, down);            \
  subs height, height, #4;                                                     \
  bhi 2b;                                                                      \
                                                                               \
 1:                                                                            \


#define setup_spans_alternate_pre_increment_yes()                              \
  adds edge_alt_low, edge_alt_low, edge_dx_dy_alt;                             \
  adc edge_alt_high, edge_alt_high, edge_dx_dy_alt, asr #31                    \

#define setup_spans_alternate_pre_increment_no()                               \


#define setup_spans_up_decrement_yes()                                         \
  suble height, height, #1                                                     \

#define setup_spans_up_decrement_no()                                          \


#define setup_spans_up(left_index, right_index, alternate, alternate_active)   \
  setup_spans_alternate_adjust_##alternate_active();                           \
  setup_spans_load_b();                                                        \
  sub y_a, y_a, #1;                                                            \
                                                                               \
  ldrh temp, [psx_gpu, #psx_gpu_viewport_start_y_offset];                      \
  subs temp, temp, y_c;                                                        \
  subgt height, height, temp;                                                  \
  setup_spans_up_decrement_##alternate_active();                               \
                                                                               \
  ldrh temp, [psx_gpu, #psx_gpu_viewport_end_y_offset];                        \
  subs clip, y_a, temp;                                                        \
  ble 0f;                                                                      \
                                                                               \
  sub height, height, clip;                                                    \
  sub y_a, y_a, clip;                                                          \
  setup_spans_clip(decrement, alternate_active);                               \
                                                                               \
 0:                                                                            \
  cmp height, #0;                                                              \
  ble 1f;                                                                      \
                                                                               \
  orr temp, y_a, y_a, lsl #16;                                                 \
  sub temp, temp, #(1 << 16);                                                  \
  sub y_a, temp, #2;                                                           \
  sub y_a, y_a, #(2 << 16);                                                    \
  vmov y_x4, temp, y_a;                                                        \
                                                                               \
  vaddw.s32 edges_xy, edges_xy, edges_dx_dy;                                   \
                                                                               \
  setup_spans_alternate_pre_increment_##alternate_active();                    \
  setup_spans_adjust_edges_alternate_##alternate_active(left_index,            \
   right_index);                                                               \
  setup_spans_adjust_interpolants_up();                                        \
  setup_spans_prologue_b();                                                    \
                                                                               \
  strh height, [psx_gpu, #psx_gpu_num_spans_offset];                           \
                                                                               \
 2:                                                                            \
  setup_spans_set_x4_alternate_##alternate_active(alternate, up);              \
  subs height, height, #4;                                                     \
  bhi 2b;                                                                      \
                                                                               \
 1:                                                                            \


#define setup_spans_epilogue()                                                 \
  ldmia sp!, { r4 - r11, pc }                                                  \


#define setup_spans_up_up(minor, major)                                        \
  setup_spans_prologue();                                                      \
  sub height_minor_a, y_a, y_b;                                                \
  sub height_minor_b, y_b, y_c;                                                \
  sub height, y_a, y_c;                                                        \
                                                                               \
  vdup.u32 x_starts, x_a;                                                      \
  vmov x_ends, x_c, x_b;                                                       \
                                                                               \
  compute_edge_delta_x3(x_b, height_major, height_minor_a);                    \
  setup_spans_up(major, minor, minor, yes);                                    \
  setup_spans_epilogue()                                                       \

function(setup_spans_up_left)
  setup_spans_up_up(left, right)

function(setup_spans_up_right)
  setup_spans_up_up(right, left)

#define setup_spans_down_down(minor, major)                                    \
  setup_spans_prologue();                                                      \
  sub height_minor_a, y_b, y_a;                                                \
  sub height_minor_b, y_c, y_b;                                                \
  sub height, y_c, y_a;                                                        \
                                                                               \
  vdup.u32 x_starts, x_a;                                                      \
  vmov x_ends, x_c, x_b;                                                       \
                                                                               \
  compute_edge_delta_x3(x_b, height_major, height_minor_a);                    \
  setup_spans_down(major, minor, minor, yes);                                  \
  setup_spans_epilogue()                                                       \

function(setup_spans_down_left)
  setup_spans_down_down(left, right)

function(setup_spans_down_right)
  setup_spans_down_down(right, left)


#define setup_spans_up_flat()                                                  \
  sub height, y_a, y_c;                                                        \
                                                                               \
  compute_edge_delta_x2();                                                     \
  setup_spans_up(left, right, none, no);                                       \
  setup_spans_epilogue()                                                       \

function(setup_spans_up_a)
  setup_spans_prologue()

  vmov x_starts, x_a, x_b
  vdup.u32 x_ends, x_c

  setup_spans_up_flat()

function(setup_spans_up_b)
  setup_spans_prologue()

  vdup.u32 x_starts, x_a
  vmov x_ends, x_b, x_c

  setup_spans_up_flat()

#define setup_spans_down_flat()                                                \
  sub height, y_c, y_a;                                                        \
                                                                               \
  compute_edge_delta_x2();                                                     \
  setup_spans_down(left, right, none, no);                                     \
  setup_spans_epilogue()                                                       \

function(setup_spans_down_a)
  setup_spans_prologue()

  vmov x_starts, x_a, x_b
  vdup.u32 x_ends, x_c

  setup_spans_down_flat()

function(setup_spans_down_b)
  setup_spans_prologue()

  vdup.u32 x_starts, x_a
  vmov x_ends, x_b, x_c

  setup_spans_down_flat()


#define middle_y                                          r9

#define edges_xy_b                                        q11
#define edges_dx_dy_b                                     d26
#define edge_shifts_b                                     d27
#define edges_dx_dy_and_shifts_b                          q13
#define height_increment                                  d20

#define edges_dx_dy_and_shifts                            q1

#define edges_xy_b_left                                   d22
#define edges_xy_b_right                                  d23

#define setup_spans_up_down_load_edge_set_b()                                  \
  vmov edges_xy, edges_xy_b;                                                   \
  vmov edges_dx_dy_and_shifts, edges_dx_dy_and_shifts_b                        \


function(setup_spans_up_down)
  setup_spans_prologue()

  // s32 middle_y = y_a;
  sub height_minor_a, y_a, y_b
  sub height_minor_b, y_c, y_a
  sub height_major, y_c, y_b

  vmov x_starts, x_a, x_c
  vdup.u32 x_ends, x_b

  compute_edge_delta_x3(x_a, height_minor_a, height_major)

  mov temp, #0
  vmov height_increment, temp, height_minor_b
  vmlal.s32 edges_xy, edges_dx_dy, height_increment

  vmov edges_xy_b_left, edge_alt_low, edge_alt_high
  vmov edges_xy_b_right, edges_xy_right

  vmov edge_shifts_b, edge_shifts
  vmov.u32 edge_shifts_b[0], edge_shift_alt

  vneg.s32 edges_dx_dy_b, edges_dx_dy
  vmov.u32 edges_dx_dy_b[0], edge_dx_dy_alt

  mov middle_y, y_a
  
  setup_spans_load_b()
  sub y_a, y_a, #1

  ldrh temp, [psx_gpu, #psx_gpu_viewport_start_y_offset]
  subs temp, temp, y_b
  subgt height_minor_a, height_minor_a, temp

  ldrh temp, [psx_gpu, #psx_gpu_viewport_end_y_offset]
  subs clip, y_a, temp
  ble 0f

  sub height_minor_a, height_minor_a, clip
  sub y_a, y_a, clip
  setup_spans_clip(decrement, no)

 0:                                                                
  cmp height_minor_a, #0
  ble 3f

  orr temp, y_a, y_a, lsl #16
  sub temp, temp, #(1 << 16)
  sub y_a, temp, #2
  sub y_a, y_a, #(2 << 16)
  vmov y_x4, temp, y_a

  vaddw.s32 edges_xy, edges_xy, edges_dx_dy

  strh height_minor_a, [psx_gpu, #psx_gpu_num_spans_offset]

  setup_spans_adjust_edges_alternate_no(left, right); 
  setup_spans_adjust_interpolants_up()
  setup_spans_up_down_load_edge_set_b()

  setup_spans_prologue_b()


 2: 
  setup_spans_set_x4_alternate_no(none, up)
  subs height_minor_a, height_minor_a, #4
  bhi 2b

  add span_edge_data, span_edge_data, height_minor_a, lsl #3
  add span_uvrg_offset, span_uvrg_offset, height_minor_a, lsl #4
  add span_b_offset, span_b_offset, height_minor_a, lsl #2

 4:
  add temp, psx_gpu, #psx_gpu_uvrg_offset
  vld1.32 { uvrg }, [temp]
  mov y_a, middle_y
  
  setup_spans_load_b()

  ldrh temp, [psx_gpu, #psx_gpu_viewport_end_y_offset]
  subs y_c, y_c, temp
  subgt height_minor_b, height_minor_b, y_c
  addgt height_minor_b, height_minor_b, #1

  ldrh temp, [psx_gpu, #psx_gpu_viewport_start_y_offset]
  subs clip, temp, y_a
  ble 0f

  sub height_minor_b, height_minor_b, clip
  add y_a, y_a, clip
  setup_spans_clip(increment, no)

 0:
  cmp height_minor_b, #0
  ble 1f

  orr temp, y_a, y_a, lsl #16
  add temp, temp, #(1 << 16) 
  add y_a, temp, #2
  add y_a, y_a, #(2 << 16)
  vmov y_x4, temp, y_a

  setup_spans_adjust_edges_alternate_no(left, right)

  ldrh temp, [psx_gpu, #psx_gpu_num_spans_offset]
  add temp, temp, height_minor_b

  cmp temp, #MAX_SPANS
  beq 5f

  strh temp, [psx_gpu, #psx_gpu_num_spans_offset]

 2:                                                     
  setup_spans_set_x4_alternate_no(none, down)
  subs height_minor_b, height_minor_b, #4
  bhi 2b

 1:
  setup_spans_epilogue()

 3:
  setup_spans_up_down_load_edge_set_b()
  setup_spans_prologue_b()
  bal 4b

 5:
  // FIXME: overflow corner case
  sub temp, temp, height_minor_b
  bics height_minor_b, #3
  add temp, temp, height_minor_b
  strh temp, [psx_gpu, #psx_gpu_num_spans_offset]
  bne 2b
  bal 1b

#undef span_uvrg_offset
#undef span_edge_data
#undef span_b_offset
#undef left_x
#undef b

#define psx_gpu                                  r0
#define num_spans                                r1
#define span_uvrg_offset                         r2
#define span_edge_data                           r3
#define span_b_offset                            r4
#define b_dx                                     r5
#define span_num_blocks                          r6
#define y                                        r7
#define left_x                                   r8
#define b                                        r9
#define dither_offset_ptr                        r10
#define block_ptr_a                              r11
#define fb_ptr                                   r12
#define num_blocks                               r14

#define uvrg_dx_ptr                              r2
#define texture_mask_ptr                         r3
#define dither_shift                             r8
#define dither_row                               r10

#define c_32                                     r7
#define b_dx4                                    r8
#define b_dx8                                    r9
#define block_ptr_b                              r10

#define block_span_ptr                           r10
#define right_mask                               r8

#define color                                    r2
#define color_r                                  r3
#define color_g                                  r4
#define color_b                                  r5

#undef uvrg

#define u_block                                  q0
#define v_block                                  q1
#define r_block                                  q2
#define g_block                                  q3
#define b_block                                  q4

#define uv_dx4                                   d10
#define rg_dx4                                   d11
#define uv_dx8                                   d12
#define rg_dx8                                   d13
#define b_whole_8                                d14
#define fb_mask_ptrs                             d15

#define uvrg_dx4                                 q5
#define uvrg_dx8                                 q6
#define uv_dx8                                   d12
#define rg_dx8                                   d13

#define u_whole                                  q8
#define v_whole                                  q9
#define r_whole                                  q10
#define g_whole                                  q11
#define b_whole                                  q12

#define u_whole_low                              d16
#define u_whole_high                             d17
#define v_whole_low                              d18
#define v_whole_high                             d19
#define r_whole_low                              d20
#define r_whole_high                             d21
#define g_whole_low                              d22
#define g_whole_high                             d23
#define b_whole_low                              d24
#define b_whole_high                             d25

#define dx4                                      q13
#define dx8                                      q13

#define u_whole_8                                d26
#define v_whole_8                                d27
#define u_whole_8b                               d24
#define r_whole_8                                d24
#define g_whole_8                                d25

#define uv_whole_8                               q13
#define uv_whole_8b                              q14

#define dither_offsets                           q14
#define texture_mask                             q15
#define texture_mask_u                           d30
#define texture_mask_v                           d31

#define dither_offsets_short                     d28

#define v_left_x                                 q8
#define uvrg                                     q9
#define block_span                               q10

#define uv                                       d18
#define rg                                       d19

#define draw_mask                                q1
#define draw_mask_edge                           q13
#define test_mask                                q0

#define uvrg_dx                                  q3

#define colors                                   q2

#define setup_blocks_texture_swizzled()                                        \
  vand.u8 u_whole_8b, u_whole_8, texture_mask_u;                               \
  vsli.u8 u_whole_8, v_whole_8, #4;                                            \
  vsri.u8 v_whole_8, u_whole_8b, #4                                            \

#define setup_blocks_texture_unswizzled()                                      \


#define setup_blocks_shaded_textured_builder(swizzling)                        \
.align 3;                                                                      \
                                                                               \
function(setup_blocks_shaded_textured_dithered_##swizzling##_indirect)         \
  ldrh num_spans, [psx_gpu, #psx_gpu_num_spans_offset];                        \
  add uvrg_dx_ptr, psx_gpu, #psx_gpu_uvrg_dx_offset;                           \
                                                                               \
  vld1.u32 { uvrg_dx }, [uvrg_dx_ptr, :128];                                   \
  add texture_mask_ptr, psx_gpu, #psx_gpu_texture_mask_width_offset;           \
                                                                               \
  cmp num_spans, #0;                                                           \
  bxeq lr;                                                                     \
                                                                               \
  stmdb sp!, { r4 - r11, r14 };                                                \
  vshl.u32 uvrg_dx4, uvrg_dx, #2;                                              \
                                                                               \
  ldr b_dx, [psx_gpu, #psx_gpu_b_dx_offset];                                   \
  vshl.u32 uvrg_dx8, uvrg_dx, #3;                                              \
                                                                               \
  vld2.u8 { texture_mask_u[], texture_mask_v[] }, [texture_mask_ptr, :16];     \
  add span_uvrg_offset, psx_gpu, #psx_gpu_span_uvrg_offset_offset;             \
                                                                               \
  ldrh num_blocks, [psx_gpu, #psx_gpu_num_blocks_offset];                      \
  add span_edge_data, psx_gpu, #psx_gpu_span_edge_data_offset;                 \
                                                                               \
  add span_b_offset, psx_gpu, #psx_gpu_span_b_offset_offset;                   \
  add block_ptr_a, psx_gpu, #psx_gpu_blocks_offset;                            \
                                                                               \
  add block_ptr_a, block_ptr_a, num_blocks, lsl #6;                            \
                                                                               \
 0:                                                                            \
  vmov.u8 fb_mask_ptrs, #0;                                                    \
                                                                               \
  ldrh span_num_blocks, [span_edge_data, #edge_data_num_blocks_offset];        \
  add dither_offset_ptr, psx_gpu, #psx_gpu_dither_table_offset;                \
                                                                               \
  ldrh y, [span_edge_data, #edge_data_y_offset];                               \
  ldr fb_ptr, [psx_gpu, #psx_gpu_vram_out_ptr_offset];                         \
                                                                               \
  cmp span_num_blocks, #0;                                                     \
  beq 1f;                                                                      \
                                                                               \
  ldrh left_x, [span_edge_data, #edge_data_left_x_offset];                     \
  add num_blocks, span_num_blocks, num_blocks;                                 \
                                                                               \
  cmp num_blocks, #MAX_BLOCKS;                                                 \
  bgt 2f;                                                                      \
                                                                               \
 3:                                                                            \
  ldr b, [span_b_offset];                                                      \
  add fb_ptr, fb_ptr, y, lsl #11;                                              \
                                                                               \
  vdup.u32 v_left_x, left_x;                                                   \
  and y, y, #0x3;                                                              \
                                                                               \
  ldr dither_row, [dither_offset_ptr, y, lsl #2];                              \
  add fb_ptr, fb_ptr, left_x, lsl #1;                                          \
                                                                               \
  mla b, b_dx, left_x, b;                                                      \
  and dither_shift, left_x, #0x03;                                             \
                                                                               \
  vld1.u32 { uvrg }, [span_uvrg_offset, :128];                                 \
  vshr.u32 uvrg_dx, uvrg_dx4, #2;                                              \
                                                                               \
  mov dither_shift, dither_shift, lsl #3;                                      \
  vmla.u32 uvrg, uvrg_dx, v_left_x;                                            \
                                                                               \
  mov c_32, #32;                                                               \
  subs span_num_blocks, span_num_blocks, #1;                                   \
                                                                               \
  mov dither_row, dither_row, ror dither_shift;                                \
  mov b_dx4, b_dx, lsl #2;                                                     \
                                                                               \
  vdup.u32 dither_offsets_short, dither_row;                                   \
  add block_span_ptr, psx_gpu, #psx_gpu_u_block_span_offset;                   \
                                                                               \
  vdup.u32 b_block, b;                                                         \
  vshll.s8 dither_offsets, dither_offsets_short, #4;                           \
                                                                               \
  vdup.u32 u_block, uv[0];                                                     \
  mov b_dx8, b_dx, lsl #3;                                                     \
                                                                               \
  vdup.u32 v_block, uv[1];                                                     \
  vdup.u32 r_block, rg[0];                                                     \
  vdup.u32 g_block, rg[1];                                                     \
                                                                               \
  vld1.u32 { block_span }, [block_span_ptr, :128]!;                            \
                                                                               \
  vadd.u32 u_block, u_block, block_span;                                       \
  vld1.u32 { block_span }, [block_span_ptr, :128]!;                            \
                                                                               \
  vadd.u32 v_block, v_block, block_span;                                       \
  vld1.u32 { block_span }, [block_span_ptr, :128]!;                            \
                                                                               \
  vadd.u32 r_block, r_block, block_span;                                       \
  vld1.u32 { block_span }, [block_span_ptr, :128]!;                            \
                                                                               \
  vadd.u32 g_block, g_block, block_span;                                       \
  vld1.u32 { block_span }, [block_span_ptr, :128];                             \
                                                                               \
  vadd.u32 b_block, b_block, block_span;                                       \
  add block_ptr_b, block_ptr_a, #16;                                           \
                                                                               \
  vshrn.u32 u_whole_low, u_block, #16;                                         \
  vshrn.u32 v_whole_low, v_block, #16;                                         \
  vshrn.u32 r_whole_low, r_block, #16;                                         \
  vshrn.u32 g_whole_low, g_block, #16;                                         \
                                                                               \
  vdup.u32 dx4, uv_dx4[0];                                                     \
  vshrn.u32 b_whole_low, b_block, #16;                                         \
                                                                               \
  vaddhn.u32 u_whole_high, u_block, dx4;                                       \
  vdup.u32 dx4, uv_dx4[1];                                                     \
                                                                               \
  vaddhn.u32 v_whole_high, v_block, dx4;                                       \
  vdup.u32 dx4, rg_dx4[0];                                                     \
                                                                               \
  vaddhn.u32 r_whole_high, r_block, dx4;                                       \
  vdup.u32 dx4, rg_dx4[1];                                                     \
                                                                               \
  vaddhn.u32 g_whole_high, g_block, dx4;                                       \
  vdup.u32 dx4, b_dx4;                                                         \
                                                                               \
  vaddhn.u32 b_whole_high, b_block, dx4;                                       \
  vdup.u32 dx8, uv_dx8[0];                                                     \
                                                                               \
  vadd.u32 u_block, u_block, dx8;                                              \
  vdup.u32 dx8, uv_dx8[1];                                                     \
                                                                               \
  vadd.u32 v_block, v_block, dx8;                                              \
  vdup.u32 dx8, rg_dx8[0];                                                     \
                                                                               \
  vadd.u32 r_block, r_block, dx8;                                              \
  vdup.u32 dx8, rg_dx8[1];                                                     \
                                                                               \
  vadd.u32 g_block, g_block, dx8;                                              \
  vdup.u32 dx8, b_dx8;                                                         \
                                                                               \
  vadd.u32 b_block, b_block, dx8;                                              \
  vmovn.u16 u_whole_8, u_whole;                                                \
                                                                               \
  vmovn.u16 v_whole_8, v_whole;                                                \
                                                                               \
  vmovn.u16 b_whole_8, b_whole;                                                \
  pld [fb_ptr];                                                                \
  vmov.u32 fb_mask_ptrs[1], fb_ptr;                                            \
                                                                               \
  vand.u8 uv_whole_8, uv_whole_8, texture_mask;                                \
  setup_blocks_texture_##swizzling();                                          \
                                                                               \
  vmovn.u16 r_whole_8, r_whole;                                                \
  beq 5f;                                                                      \
                                                                               \
 4:                                                                            \
  vmovn.u16 g_whole_8, g_whole;                                                \
  vshrn.u32 u_whole_low, u_block, #16;                                         \
                                                                               \
  vst2.u8 { u_whole_8, v_whole_8 }, [block_ptr_a, :128], c_32;                 \
  vshrn.u32 v_whole_low, v_block, #16;                                         \
                                                                               \
  vst1.u32 { r_whole_8, g_whole_8 }, [block_ptr_b, :128], c_32;                \
  vshrn.u32 r_whole_low, r_block, #16;                                         \
                                                                               \
  vst1.u32 { b_whole_8, fb_mask_ptrs }, [block_ptr_a, :128], c_32;             \
  vshrn.u32 g_whole_low, g_block, #16;                                         \
                                                                               \
  vdup.u32 dx4, uv_dx4[0];                                                     \
  vshrn.u32 b_whole_low, b_block, #16;                                         \
                                                                               \
  vaddhn.u32 u_whole_high, u_block, dx4;                                       \
  vdup.u32 dx4, uv_dx4[1];                                                     \
                                                                               \
  vaddhn.u32 v_whole_high, v_block, dx4;                                       \
  vdup.u32 dx4, rg_dx4[0];                                                     \
                                                                               \
  vaddhn.u32 r_whole_high, r_block, dx4;                                       \
  vdup.u32 dx4, rg_dx4[1];                                                     \
                                                                               \
  vaddhn.u32 g_whole_high, g_block, dx4;                                       \
  vdup.u32 dx4, b_dx4;                                                         \
                                                                               \
  vaddhn.u32 b_whole_high, b_block, dx4;                                       \
  vdup.u32 dx8, uv_dx8[0];                                                     \
                                                                               \
  vadd.u32 u_block, u_block, dx8;                                              \
  vdup.u32 dx8, uv_dx8[1];                                                     \
                                                                               \
  vadd.u32 v_block, v_block, dx8;                                              \
  vdup.u32 dx8, rg_dx8[0];                                                     \
                                                                               \
  vadd.u32 r_block, r_block, dx8;                                              \
  vdup.u32 dx8, rg_dx8[1];                                                     \
                                                                               \
  vadd.u32 g_block, g_block, dx8;                                              \
  vdup.u32 dx8, b_dx8;                                                         \
                                                                               \
  vadd.u32 b_block, b_block, dx8;                                              \
  vmovn.u16 u_whole_8, u_whole;                                                \
                                                                               \
  add fb_ptr, fb_ptr, #16;                                                     \
  vmovn.u16 v_whole_8, v_whole;                                                \
                                                                               \
  vst1.u32 { dither_offsets }, [block_ptr_b, :128], c_32;                      \
  vmovn.u16 b_whole_8, b_whole;                                                \
                                                                               \
  pld [fb_ptr];                                                                \
                                                                               \
  vmov.u32 fb_mask_ptrs[1], fb_ptr;                                            \
  subs span_num_blocks, span_num_blocks, #1;                                   \
                                                                               \
  vand.u8 uv_whole_8, uv_whole_8, texture_mask;                                \
  setup_blocks_texture_##swizzling();                                          \
                                                                               \
  vmovn.u16 r_whole_8, r_whole;                                                \
  bne 4b;                                                                      \
                                                                               \
 5:                                                                            \
  vmovn.u16 g_whole_8, g_whole;                                                \
  ldrh right_mask, [span_edge_data, #edge_data_right_mask_offset];             \
                                                                               \
  vld1.u32 { test_mask }, [psx_gpu, :128];                                     \
  vdup.u8 draw_mask, right_mask;                                               \
                                                                               \
  vmov.u32 fb_mask_ptrs[0], right_mask;                                        \
  vtst.u16 draw_mask, draw_mask, test_mask;                                    \
  vzip.u8 u_whole_8, v_whole_8;                                                \
                                                                               \
  vbic.u16 uv_whole_8, uv_whole_8, draw_mask;                                  \
  vst1.u32 { r_whole_8, g_whole_8 }, [block_ptr_b, :128], c_32;                \
  vst1.u32 { uv_whole_8 }, [block_ptr_a, :128], c_32;                          \
  vst1.u32 { dither_offsets }, [block_ptr_b, :128], c_32;                      \
  vst1.u32 { b_whole_8, fb_mask_ptrs }, [block_ptr_a, :128], c_32;             \
                                                                               \
 1:                                                                            \
  add span_uvrg_offset, span_uvrg_offset, #16;                                 \
  add span_b_offset, span_b_offset, #4;                                        \
                                                                               \
  add span_edge_data, span_edge_data, #8;                                      \
  subs num_spans, num_spans, #1;                                               \
                                                                               \
  strh num_blocks, [psx_gpu, #psx_gpu_num_blocks_offset];                      \
  bne 0b;                                                                      \
                                                                               \
  ldmia sp!, { r4 - r11, pc };                                                 \
                                                                               \
 2:                                                                            \
  /* TODO: Load from psx_gpu instead of saving/restoring these               */\
  vpush { texture_mask };                                                      \
  vpush { uvrg_dx4 };                                                          \
                                                                               \
  stmdb sp!, { r0 - r3, EXTRA_UNSAVED_REGS r12, r14 };                        \
  bl flush_render_block_buffer;                                                \
  ldmia sp!, { r0 - r3, EXTRA_UNSAVED_REGS r12, r14 };                        \
                                                                               \
  vpop { uvrg_dx4 };                                                           \
  vpop { texture_mask };                                                       \
                                                                               \
  vadd.u32 uvrg_dx8, uvrg_dx4, uvrg_dx4;                                       \
  vmov.u8 fb_mask_ptrs, #0;                                                    \
                                                                               \
  mov num_blocks, span_num_blocks;                                             \
  add block_ptr_a, psx_gpu, #psx_gpu_blocks_offset;                            \
  bal 3b                                                                       \


setup_blocks_shaded_textured_builder(swizzled)
setup_blocks_shaded_textured_builder(unswizzled)


#define setup_blocks_unshaded_textured_builder(swizzling)                      \
.align 3;                                                                      \
                                                                               \
function(setup_blocks_unshaded_textured_dithered_##swizzling##_indirect)       \
  ldrh num_spans, [psx_gpu, #psx_gpu_num_spans_offset];                        \
  add uvrg_dx_ptr, psx_gpu, #psx_gpu_uvrg_dx_offset;                           \
                                                                               \
  vld1.u32 { uvrg_dx }, [uvrg_dx_ptr, :128];                                   \
  add texture_mask_ptr, psx_gpu, #psx_gpu_texture_mask_width_offset;           \
                                                                               \
  cmp num_spans, #0;                                                           \
  bxeq lr;                                                                     \
                                                                               \
  stmdb sp!, { r4 - r11, r14 };                                                \
  vshl.u32 uvrg_dx4, uvrg_dx, #2;                                              \
                                                                               \
  vshl.u32 uvrg_dx8, uvrg_dx, #3;                                              \
                                                                               \
  vld2.u8 { texture_mask_u[], texture_mask_v[] }, [texture_mask_ptr, :16];     \
  add span_uvrg_offset, psx_gpu, #psx_gpu_span_uvrg_offset_offset;             \
                                                                               \
  ldrh num_blocks, [psx_gpu, #psx_gpu_num_blocks_offset];                      \
  add span_edge_data, psx_gpu, #psx_gpu_span_edge_data_offset;                 \
                                                                               \
  add block_ptr_a, psx_gpu, #psx_gpu_blocks_offset;                            \
                                                                               \
  add block_ptr_a, block_ptr_a, num_blocks, lsl #6;                            \
                                                                               \
 0:                                                                            \
  vmov.u8 fb_mask_ptrs, #0;                                                    \
                                                                               \
  ldrh span_num_blocks, [span_edge_data, #edge_data_num_blocks_offset];        \
  add dither_offset_ptr, psx_gpu, #psx_gpu_dither_table_offset;                \
                                                                               \
  ldrh y, [span_edge_data, #edge_data_y_offset];                               \
  ldr fb_ptr, [psx_gpu, #psx_gpu_vram_out_ptr_offset];                         \
                                                                               \
  cmp span_num_blocks, #0;                                                     \
  beq 1f;                                                                      \
                                                                               \
  ldrh left_x, [span_edge_data, #edge_data_left_x_offset];                     \
  add num_blocks, span_num_blocks, num_blocks;                                 \
                                                                               \
  cmp num_blocks, #MAX_BLOCKS;                                                 \
  bgt 2f;                                                                      \
                                                                               \
 3:                                                                            \
  add fb_ptr, fb_ptr, y, lsl #11;                                              \
                                                                               \
  vdup.u32 v_left_x, left_x;                                                   \
  and y, y, #0x3;                                                              \
                                                                               \
  ldr dither_row, [dither_offset_ptr, y, lsl #2];                              \
  add fb_ptr, fb_ptr, left_x, lsl #1;                                          \
                                                                               \
  and dither_shift, left_x, #0x03;                                             \
                                                                               \
  vld1.u32 { uvrg }, [span_uvrg_offset, :128];                                 \
  vshr.u32 uvrg_dx, uvrg_dx4, #2;                                              \
                                                                               \
  mov dither_shift, dither_shift, lsl #3;                                      \
  vmla.u32 uvrg, uvrg_dx, v_left_x;                                            \
                                                                               \
  mov c_32, #32;                                                               \
  subs span_num_blocks, span_num_blocks, #1;                                   \
                                                                               \
  mov dither_row, dither_row, ror dither_shift;                                \
                                                                               \
  vdup.u32 dither_offsets_short, dither_row;                                   \
  add block_span_ptr, psx_gpu, #psx_gpu_u_block_span_offset;                   \
                                                                               \
  vshll.s8 dither_offsets, dither_offsets_short, #4;                           \
                                                                               \
  vdup.u32 u_block, uv[0];                                                     \
                                                                               \
  vdup.u32 v_block, uv[1];                                                     \
  vld1.u32 { block_span }, [block_span_ptr, :128]!;                            \
                                                                               \
  vadd.u32 u_block, u_block, block_span;                                       \
  vld1.u32 { block_span }, [block_span_ptr, :128]!;                            \
                                                                               \
  vadd.u32 v_block, v_block, block_span;                                       \
  add block_ptr_b, block_ptr_a, #16;                                           \
                                                                               \
  vshrn.u32 u_whole_low, u_block, #16;                                         \
  vshrn.u32 v_whole_low, v_block, #16;                                         \
                                                                               \
  vdup.u32 dx4, uv_dx4[0];                                                     \
                                                                               \
  vaddhn.u32 u_whole_high, u_block, dx4;                                       \
  vdup.u32 dx4, uv_dx4[1];                                                     \
                                                                               \
  vaddhn.u32 v_whole_high, v_block, dx4;                                       \
  vdup.u32 dx8, uv_dx8[0];                                                     \
                                                                               \
  vadd.u32 u_block, u_block, dx8;                                              \
  vdup.u32 dx8, uv_dx8[1];                                                     \
                                                                               \
  vadd.u32 v_block, v_block, dx8;                                              \
  vmovn.u16 u_whole_8, u_whole;                                                \
                                                                               \
  vmovn.u16 v_whole_8, v_whole;                                                \
                                                                               \
  pld [fb_ptr];                                                                \
  vmov.u32 fb_mask_ptrs[1], fb_ptr;                                            \
                                                                               \
  vand.u8 uv_whole_8, uv_whole_8, texture_mask;                                \
  setup_blocks_texture_##swizzling();                                          \
                                                                               \
  beq 5f;                                                                      \
                                                                               \
 4:                                                                            \
  vshrn.u32 u_whole_low, u_block, #16;                                         \
                                                                               \
  vst2.u8 { u_whole_8, v_whole_8 }, [block_ptr_a, :128], c_32;                 \
  vshrn.u32 v_whole_low, v_block, #16;                                         \
                                                                               \
  add block_ptr_b, block_ptr_b, #32;                                           \
  vst1.u32 { b_whole_8, fb_mask_ptrs }, [block_ptr_a, :128], c_32;             \
                                                                               \
  vdup.u32 dx4, uv_dx4[0];                                                     \
  vaddhn.u32 u_whole_high, u_block, dx4;                                       \
  vdup.u32 dx4, uv_dx4[1];                                                     \
                                                                               \
  vaddhn.u32 v_whole_high, v_block, dx4;                                       \
  vdup.u32 dx8, uv_dx8[0];                                                     \
                                                                               \
  vadd.u32 u_block, u_block, dx8;                                              \
  vdup.u32 dx8, uv_dx8[1];                                                     \
                                                                               \
  vadd.u32 v_block, v_block, dx8;                                              \
  vmovn.u16 u_whole_8, u_whole;                                                \
                                                                               \
  add fb_ptr, fb_ptr, #16;                                                     \
  vmovn.u16 v_whole_8, v_whole;                                                \
                                                                               \
  vst1.u32 { dither_offsets }, [block_ptr_b, :128], c_32;                      \
  pld [fb_ptr];                                                                \
                                                                               \
  vmov.u32 fb_mask_ptrs[1], fb_ptr;                                            \
  subs span_num_blocks, span_num_blocks, #1;                                   \
                                                                               \
  vand.u8 uv_whole_8, uv_whole_8, texture_mask;                                \
  setup_blocks_texture_##swizzling();                                          \
                                                                               \
  bne 4b;                                                                      \
                                                                               \
 5:                                                                            \
  ldrh right_mask, [span_edge_data, #edge_data_right_mask_offset];             \
                                                                               \
  vld1.u32 { test_mask }, [psx_gpu, :128];                                     \
  vdup.u8 draw_mask, right_mask;                                               \
                                                                               \
  vmov.u32 fb_mask_ptrs[0], right_mask;                                        \
  vtst.u16 draw_mask, draw_mask, test_mask;                                    \
  vzip.u8 u_whole_8, v_whole_8;                                                \
                                                                               \
  vbic.u16 uv_whole_8, uv_whole_8, draw_mask;                                  \
  add block_ptr_b, block_ptr_b, #32;                                           \
  vst1.u32 { uv_whole_8 }, [block_ptr_a, :128], c_32;                          \
  vst1.u32 { dither_offsets }, [block_ptr_b, :128], c_32;                      \
  vst1.u32 { b_whole_8, fb_mask_ptrs }, [block_ptr_a, :128], c_32;             \
                                                                               \
 1:                                                                            \
  add span_uvrg_offset, span_uvrg_offset, #16;                                 \
  add span_edge_data, span_edge_data, #8;                                      \
  subs num_spans, num_spans, #1;                                               \
                                                                               \
  strh num_blocks, [psx_gpu, #psx_gpu_num_blocks_offset];                      \
  bne 0b;                                                                      \
                                                                               \
  ldmia sp!, { r4 - r11, pc };                                                 \
                                                                               \
 2:                                                                            \
  /* TODO: Load from psx_gpu instead of saving/restoring these               */\
  vpush { texture_mask };                                                      \
  vpush { uvrg_dx4 };                                                          \
                                                                               \
  stmdb sp!, { r0 - r3, EXTRA_UNSAVED_REGS r12, r14 };                        \
  bl flush_render_block_buffer;                                                \
  ldmia sp!, { r0 - r3, EXTRA_UNSAVED_REGS r12, r14 };                        \
                                                                               \
  vpop { uvrg_dx4 };                                                           \
  vpop { texture_mask };                                                       \
                                                                               \
  vadd.u32 uvrg_dx8, uvrg_dx4, uvrg_dx4;                                       \
  vmov.u8 fb_mask_ptrs, #0;                                                    \
                                                                               \
  mov num_blocks, span_num_blocks;                                             \
  add block_ptr_a, psx_gpu, #psx_gpu_blocks_offset;                            \
  bal 3b                                                                       \


setup_blocks_unshaded_textured_builder(swizzled)
setup_blocks_unshaded_textured_builder(unswizzled)


.align 3

function(setup_blocks_unshaded_untextured_undithered_unswizzled_indirect)
  ldrh num_spans, [psx_gpu, #psx_gpu_num_spans_offset]
  veor.u32 draw_mask, draw_mask, draw_mask

  cmp num_spans, #0
  bxeq lr

  stmdb sp!, { r4 - r11, r14 }
  vld1.u32 { test_mask }, [psx_gpu, :128]

  ldr color, [psx_gpu, #psx_gpu_triangle_color_offset]

  ubfx color_r, color, #3, #5
  ubfx color_g, color, #11, #5
  ubfx color_b, color, #19, #5

  orr color, color_r, color_b, lsl #10
  orr color, color, color_g, lsl #5

  vdup.u16 colors, color

  ldrh num_blocks, [psx_gpu, #psx_gpu_num_blocks_offset]
  add span_edge_data, psx_gpu, #psx_gpu_span_edge_data_offset

  add block_ptr_a, psx_gpu, #psx_gpu_blocks_offset
  add block_ptr_a, block_ptr_a, num_blocks, lsl #6

 0:
  ldrh span_num_blocks, [span_edge_data, #edge_data_num_blocks_offset]
  ldrh y, [span_edge_data, #edge_data_y_offset]

  ldr fb_ptr, [psx_gpu, #psx_gpu_vram_out_ptr_offset]

  cmp span_num_blocks, #0
  beq 1f

  ldrh left_x, [span_edge_data, #edge_data_left_x_offset]
  add num_blocks, span_num_blocks, num_blocks

  cmp num_blocks, #MAX_BLOCKS
  bgt 2f

 3:
  add fb_ptr, fb_ptr, y, lsl #11
  and y, y, #0x3

  add fb_ptr, fb_ptr, left_x, lsl #1
  mov c_32, #32

  subs span_num_blocks, span_num_blocks, #1

  add block_ptr_b, block_ptr_a, #16
  pld [fb_ptr]

  vmov.u32 fb_mask_ptrs[1], fb_ptr
  beq 5f

 4:
  vst1.u32 { draw_mask }, [block_ptr_a, :128], c_32
  vst1.u32 { colors }, [block_ptr_b, :128], c_32
  vst1.u32 { b_whole_8, fb_mask_ptrs }, [block_ptr_a, :128], c_32

  add fb_ptr, fb_ptr, #16
  add block_ptr_b, block_ptr_b, #32

  pld [fb_ptr]

  vmov.u32 fb_mask_ptrs[1], fb_ptr
  subs span_num_blocks, span_num_blocks, #1

  bne 4b

 5:
  ldrh right_mask, [span_edge_data, #edge_data_right_mask_offset]

  vdup.u8 draw_mask_edge, right_mask
  vtst.u16 draw_mask_edge, draw_mask_edge, test_mask

  vst1.u32 { colors }, [block_ptr_b, :128], c_32
  vst1.u32 { draw_mask_edge }, [block_ptr_a, :128], c_32
  add block_ptr_b, block_ptr_b, #32
  vst1.u32 { b_whole_8, fb_mask_ptrs }, [block_ptr_a, :128], c_32

 1:
  add span_edge_data, span_edge_data, #8
  subs num_spans, num_spans, #1

  strh num_blocks, [psx_gpu, #psx_gpu_num_blocks_offset]
  bne 0b

  ldmia sp!, { r4 - r11, pc }
                                                                           
 2:
  vpush { colors }

  stmdb sp!, { r0 - r3, EXTRA_UNSAVED_REGS r12, r14 }
  bl flush_render_block_buffer
  ldmia sp!, { r0 - r3, EXTRA_UNSAVED_REGS r12, r14 }

  vpop { colors }

  vld1.u32 { test_mask }, [psx_gpu, :128]
  veor.u32 draw_mask, draw_mask, draw_mask

  mov num_blocks, span_num_blocks
  add block_ptr_a, psx_gpu, #psx_gpu_blocks_offset
  bal 3b


#define mask_msb_scalar                                   r14

#define msb_mask                                          q15

#define pixels_low                                        d16

#define msb_mask_low                                      d30
#define msb_mask_high                                     d31


.align 3

function(setup_blocks_unshaded_untextured_undithered_unswizzled_direct)
  ldrh num_spans, [psx_gpu, #psx_gpu_num_spans_offset]

  cmp num_spans, #0
  bxeq lr

  stmdb sp!, { r4 - r11, r14 }

  ldr color, [psx_gpu, #psx_gpu_triangle_color_offset]

  ubfx color_r, color, #3, #5
  ubfx color_g, color, #11, #5

  ldrh mask_msb_scalar, [psx_gpu, #psx_gpu_mask_msb_offset]
  ubfx color_b, color, #19, #5

  orr color, color_r, color_b, lsl #10
  orr color, color, color_g, lsl #5
  orr color, color, mask_msb_scalar

  vdup.u16 colors, color

  add span_edge_data, psx_gpu, #psx_gpu_span_edge_data_offset
  orr color, color, color, lsl #16


 0:
  ldrh span_num_blocks, [span_edge_data, #edge_data_num_blocks_offset]
  ldrh y, [span_edge_data, #edge_data_y_offset]

  ldr fb_ptr, [psx_gpu, #psx_gpu_vram_out_ptr_offset]

  cmp span_num_blocks, #0
  beq 1f

  ldrh left_x, [span_edge_data, #edge_data_left_x_offset]

  add fb_ptr, fb_ptr, y, lsl #11
  subs span_num_blocks, span_num_blocks, #1

  add fb_ptr, fb_ptr, left_x, lsl #1
  beq 3f

 2:
  vst1.u32 { colors }, [fb_ptr]!
  subs span_num_blocks, span_num_blocks, #1

  bne 2b

 3:
  ldrb right_mask, [span_edge_data, #edge_data_right_mask_offset]

  cmp right_mask, #0x0
  beq 5f

  tst right_mask, #0xF
  streq color, [fb_ptr], #4
  moveq right_mask, right_mask, lsr #4
  streq color, [fb_ptr], #4

  tst right_mask, #0x3
  streq color, [fb_ptr], #4
  moveq right_mask, right_mask, lsr #2

  tst right_mask, #0x1
  strheq color, [fb_ptr]

 1:
  add span_edge_data, span_edge_data, #8
  subs num_spans, num_spans, #1
  bne 0b

  ldmia sp!, { r4 - r11, pc }
                                                                           
 5:
  vst1.u32 { colors }, [fb_ptr]
  bal 1b


#undef c_64

#define c_64                                              r7
#define rg_dx_ptr                                         r2


#undef r_block
#undef g_block
#undef b_block
#undef r_whole
#undef g_whole
#undef b_whole
#undef r_whole_low
#undef r_whole_high
#undef g_whole_low
#undef g_whole_high
#undef b_whole_low
#undef b_whole_high
#undef r_whole_8
#undef g_whole_8
#undef b_whole_8
#undef dither_offsets
#undef rg_dx4
#undef rg_dx8
#undef dx4
#undef dx8
#undef v_left_x
#undef uvrg
#undef block_span
#undef rg
#undef draw_mask
#undef test_mask

#define r_block                                           q0
#define g_block                                           q1
#define b_block                                           q2

#define r_whole                                           q3
#define g_whole                                           q4
#define b_whole                                           q5

#define r_whole_low                                       d6
#define r_whole_high                                      d7
#define g_whole_low                                       d8
#define g_whole_high                                      d9
#define b_whole_low                                       d10
#define b_whole_high                                      d11

#define gb_whole_8                                        q6

#define g_whole_8                                         d12
#define b_whole_8                                         d13

#define r_whole_8                                         d14

#define pixels                                            q8

#define rg_dx4                                            d18
#define rg_dx8                                            d19

#define dx4                                               q10
#define dx8                                               q10

#define v_left_x                                          d6
#define uvrg                                              q4
#define block_span                                        q5

#define rg                                                d9

#define d64_1                                             d22
#define d64_128                                           d23

#define d128_4                                            q12
#define d128_0x7                                          q13

#define d64_4                                             d24

#define dither_offsets                                    q14
#define draw_mask                                         q15

#define dither_offsets_low                                d28

#define rg_dx                                             d0
#define test_mask                                         q10


#define setup_blocks_shaded_untextured_dither_a_dithered()                     \
  vqadd.u8 r_whole_8, r_whole_8, dither_offsets_low;                           \
  vqadd.u8 gb_whole_8, gb_whole_8, dither_offsets;                             \

#define setup_blocks_shaded_untextured_dither_b_dithered()                     \
  vqsub.u8 r_whole_8, r_whole_8, d64_4;                                        \
  vqsub.u8 gb_whole_8, gb_whole_8, d128_4                                      \

#define setup_blocks_shaded_untextured_dither_a_undithered()                   \

#define setup_blocks_shaded_untextured_dither_b_undithered()                   \


#define setup_blocks_shaded_untextured_indirect_builder(dithering)             \
.align 3;                                                                      \
                                                                               \
function(setup_blocks_shaded_untextured_##dithering##_unswizzled_indirect)     \
  ldrh num_spans, [psx_gpu, #psx_gpu_num_spans_offset];                        \
  add rg_dx_ptr, psx_gpu, #(psx_gpu_uvrg_dx_offset + 8);                       \
                                                                               \
  vld1.u32 { rg_dx }, [rg_dx_ptr, :64];                                        \
                                                                               \
  cmp num_spans, #0;                                                           \
  bxeq lr;                                                                     \
                                                                               \
  stmdb sp!, { r4 - r11, r14 };                                                \
  vshl.u32 rg_dx4, rg_dx, #2;                                                  \
                                                                               \
  ldr b_dx, [psx_gpu, #psx_gpu_b_dx_offset];                                   \
  vshl.u32 rg_dx8, rg_dx, #3;                                                  \
                                                                               \
  add span_uvrg_offset, psx_gpu, #psx_gpu_span_uvrg_offset_offset;             \
                                                                               \
  ldrh num_blocks, [psx_gpu, #psx_gpu_num_blocks_offset];                      \
  add span_edge_data, psx_gpu, #psx_gpu_span_edge_data_offset;                 \
                                                                               \
  add span_b_offset, psx_gpu, #psx_gpu_span_b_offset_offset;                   \
  add block_ptr_a, psx_gpu, #psx_gpu_blocks_offset;                            \
                                                                               \
  add block_ptr_a, block_ptr_a, num_blocks, lsl #6;                            \
  vmov.u8 d64_1, #1;                                                           \
                                                                               \
  vmov.u8 d128_4, #4;                                                          \
  vmov.u8 d64_128, #128;                                                       \
                                                                               \
  vmov.u8 d128_0x7, #0x7;                                                      \
                                                                               \
 0:                                                                            \
  ldrh span_num_blocks, [span_edge_data, #edge_data_num_blocks_offset];        \
  add dither_offset_ptr, psx_gpu, #psx_gpu_dither_table_offset;                \
                                                                               \
  ldrh y, [span_edge_data, #edge_data_y_offset];                               \
  ldr fb_ptr, [psx_gpu, #psx_gpu_vram_out_ptr_offset];                         \
                                                                               \
  cmp span_num_blocks, #0;                                                     \
  beq 1f;                                                                      \
                                                                               \
  ldrh left_x, [span_edge_data, #edge_data_left_x_offset];                     \
  add num_blocks, span_num_blocks, num_blocks;                                 \
                                                                               \
  cmp num_blocks, #MAX_BLOCKS;                                                 \
  bgt 2f;                                                                      \
                                                                               \
 3:                                                                            \
  ldr b, [span_b_offset];                                                      \
  add fb_ptr, fb_ptr, y, lsl #11;                                              \
                                                                               \
  vdup.u32 v_left_x, left_x;                                                   \
  and y, y, #0x3;                                                              \
                                                                               \
  ldr dither_row, [dither_offset_ptr, y, lsl #2];                              \
  add fb_ptr, fb_ptr, left_x, lsl #1;                                          \
                                                                               \
  mla b, b_dx, left_x, b;                                                      \
  and dither_shift, left_x, #0x03;                                             \
                                                                               \
  vld1.u32 { uvrg }, [span_uvrg_offset, :128];                                 \
  vshr.u32 rg_dx, rg_dx4, #2;                                                  \
                                                                               \
  mov dither_shift, dither_shift, lsl #3;                                      \
  vmla.u32 rg, rg_dx, v_left_x;                                                \
                                                                               \
  mov c_64, #64;                                                               \
  subs span_num_blocks, span_num_blocks, #1;                                   \
                                                                               \
  mov dither_row, dither_row, ror dither_shift;                                \
  mov b_dx4, b_dx, lsl #2;                                                     \
                                                                               \
  vdup.u32 dither_offsets, dither_row;                                         \
  add block_span_ptr, psx_gpu, #psx_gpu_r_block_span_offset;                   \
                                                                               \
  vdup.u32 b_block, b;                                                         \
  vadd.u8 dither_offsets, dither_offsets, d128_4;                              \
                                                                               \
  mov b_dx8, b_dx, lsl #3;                                                     \
  vdup.u32 r_block, rg[0];                                                     \
  vdup.u32 g_block, rg[1];                                                     \
                                                                               \
  vld1.u32 { block_span }, [block_span_ptr, :128]!;                            \
                                                                               \
  vadd.u32 r_block, r_block, block_span;                                       \
  vld1.u32 { block_span }, [block_span_ptr, :128]!;                            \
                                                                               \
  vadd.u32 g_block, g_block, block_span;                                       \
  vld1.u32 { block_span }, [block_span_ptr, :128];                             \
                                                                               \
  vadd.u32 b_block, b_block, block_span;                                       \
  add block_ptr_b, block_ptr_a, #16;                                           \
                                                                               \
  vshrn.u32 r_whole_low, r_block, #16;                                         \
  vshrn.u32 g_whole_low, g_block, #16;                                         \
  vshrn.u32 b_whole_low, b_block, #16;                                         \
  vdup.u32 dx4, rg_dx4[0];                                                     \
                                                                               \
  vaddhn.u32 r_whole_high, r_block, dx4;                                       \
  vdup.u32 dx4, rg_dx4[1];                                                     \
                                                                               \
  vaddhn.u32 g_whole_high, g_block, dx4;                                       \
  vdup.u32 dx4, b_dx4;                                                         \
                                                                               \
  vaddhn.u32 b_whole_high, b_block, dx4;                                       \
  vdup.u32 dx8, rg_dx8[0];                                                     \
                                                                               \
  vadd.u32 r_block, r_block, dx8;                                              \
  vdup.u32 dx8, rg_dx8[1];                                                     \
                                                                               \
  vadd.u32 g_block, g_block, dx8;                                              \
  vdup.u32 dx8, b_dx8;                                                         \
                                                                               \
  vadd.u32 b_block, b_block, dx8;                                              \
                                                                               \
  vmovn.u16 r_whole_8, r_whole;                                                \
  vmovn.u16 g_whole_8, g_whole;                                                \
  vmovn.u16 b_whole_8, b_whole;                                                \
                                                                               \
  beq 5f;                                                                      \
  veor.u32 draw_mask, draw_mask, draw_mask;                                    \
                                                                               \
 4:                                                                            \
  setup_blocks_shaded_untextured_dither_a_##dithering();                       \
  vshrn.u32 r_whole_low, r_block, #16;                                         \
                                                                               \
  setup_blocks_shaded_untextured_dither_b_##dithering();                       \
  vshrn.u32 g_whole_low, g_block, #16;                                         \
                                                                               \
  vshrn.u32 b_whole_low, b_block, #16;                                         \
  str fb_ptr, [block_ptr_a, #44];                                              \
                                                                               \
  vdup.u32 dx4, rg_dx4[0];                                                     \
  vshr.u8 r_whole_8, r_whole_8, #3;                                            \
  vbic.u8 gb_whole_8, gb_whole_8, d128_0x7;                                    \
                                                                               \
  vaddhn.u32 r_whole_high, r_block, dx4;                                       \
  vdup.u32 dx4, rg_dx4[1];                                                     \
                                                                               \
  vaddhn.u32 g_whole_high, g_block, dx4;                                       \
  vdup.u32 dx4, b_dx4;                                                         \
                                                                               \
  vaddhn.u32 b_whole_high, b_block, dx4;                                       \
  vdup.u32 dx8, rg_dx8[0];                                                     \
                                                                               \
  vmull.u8 pixels, r_whole_8, d64_1;                                           \
  vmlal.u8 pixels, g_whole_8, d64_4;                                           \
  vmlal.u8 pixels, b_whole_8, d64_128;                                         \
                                                                               \
  vadd.u32 r_block, r_block, dx8;                                              \
  vdup.u32 dx8, rg_dx8[1];                                                     \
                                                                               \
  vadd.u32 g_block, g_block, dx8;                                              \
  vdup.u32 dx8, b_dx8;                                                         \
                                                                               \
  vadd.u32 b_block, b_block, dx8;                                              \
  add fb_ptr, fb_ptr, #16;                                                     \
                                                                               \
  vmovn.u16 r_whole_8, r_whole;                                                \
  vmovn.u16 g_whole_8, g_whole;                                                \
  vmovn.u16 b_whole_8, b_whole;                                                \
                                                                               \
  vst1.u32 { draw_mask }, [block_ptr_a, :128], c_64;                           \
  vst1.u32 { pixels }, [block_ptr_b, :128], c_64;                              \
                                                                               \
  pld [fb_ptr];                                                                \
                                                                               \
  subs span_num_blocks, span_num_blocks, #1;                                   \
  bne 4b;                                                                      \
                                                                               \
 5:                                                                            \
  str fb_ptr, [block_ptr_a, #44];                                              \
  setup_blocks_shaded_untextured_dither_a_##dithering();                       \
                                                                               \
  ldrh right_mask, [span_edge_data, #edge_data_right_mask_offset];             \
  setup_blocks_shaded_untextured_dither_b_##dithering();                       \
                                                                               \
  vshr.u8 r_whole_8, r_whole_8, #3;                                            \
  vdup.u8 draw_mask, right_mask;                                               \
                                                                               \
  vbic.u8 gb_whole_8, gb_whole_8, d128_0x7;                                    \
  vld1.u32 { test_mask }, [psx_gpu, :128];                                     \
                                                                               \
  vtst.u16 draw_mask, draw_mask, test_mask;                                    \
                                                                               \
  vmull.u8 pixels, r_whole_8, d64_1;                                           \
  vmlal.u8 pixels, g_whole_8, d64_4;                                           \
  vmlal.u8 pixels, b_whole_8, d64_128;                                         \
                                                                               \
  vst1.u32 { draw_mask }, [block_ptr_a, :128], c_64;                           \
  vst1.u32 { pixels }, [block_ptr_b, :128], c_64;                              \
                                                                               \
 1:                                                                            \
  add span_uvrg_offset, span_uvrg_offset, #16;                                 \
  add span_b_offset, span_b_offset, #4;                                        \
                                                                               \
  add span_edge_data, span_edge_data, #8;                                      \
  subs num_spans, num_spans, #1;                                               \
                                                                               \
  strh num_blocks, [psx_gpu, #psx_gpu_num_blocks_offset];                      \
  bne 0b;                                                                      \
                                                                               \
  ldmia sp!, { r4 - r11, pc };                                                 \
                                                                               \
 2:                                                                            \
  /* TODO: Load from psx_gpu instead of saving/restoring these               */\
  vpush { rg_dx4 };                                                            \
                                                                               \
  stmdb sp!, { r0 - r3, EXTRA_UNSAVED_REGS r12, r14 };                        \
  bl flush_render_block_buffer;                                                \
  ldmia sp!, { r0 - r3, EXTRA_UNSAVED_REGS r12, r14 };                        \
                                                                               \
  vpop { rg_dx4 };                                                             \
                                                                               \
  vmov.u8 d64_1, #1;                                                           \
  vmov.u8 d128_4, #4;                                                          \
  vmov.u8 d64_128, #128;                                                       \
  vmov.u8 d128_0x7, #0x7;                                                      \
                                                                               \
  vadd.u32 rg_dx8, rg_dx4, rg_dx4;                                             \
                                                                               \
  mov num_blocks, span_num_blocks;                                             \
  add block_ptr_a, psx_gpu, #psx_gpu_blocks_offset;                            \
  bal 3b                                                                       \


setup_blocks_shaded_untextured_indirect_builder(undithered)
setup_blocks_shaded_untextured_indirect_builder(dithered)


#undef draw_mask

#define mask_msb_ptr                                      r14

#define draw_mask                                         q0
#define pixels_low                                        d16
#define pixels_high                                       d17



#define setup_blocks_shaded_untextured_direct_builder(dithering)               \
.align 3;                                                                      \
                                                                               \
function(setup_blocks_shaded_untextured_##dithering##_unswizzled_direct)       \
  ldrh num_spans, [psx_gpu, #psx_gpu_num_spans_offset];                        \
  add rg_dx_ptr, psx_gpu, #(psx_gpu_uvrg_dx_offset + 8);                       \
                                                                               \
  vld1.u32 { rg_dx }, [rg_dx_ptr, :64];                                        \
                                                                               \
  cmp num_spans, #0;                                                           \
  bxeq lr;                                                                     \
                                                                               \
  stmdb sp!, { r4 - r11, r14 };                                                \
  vshl.u32 rg_dx4, rg_dx, #2;                                                  \
                                                                               \
  ldr b_dx, [psx_gpu, #psx_gpu_b_dx_offset];                                   \
  vshl.u32 rg_dx8, rg_dx, #3;                                                  \
                                                                               \
  add span_uvrg_offset, psx_gpu, #psx_gpu_span_uvrg_offset_offset;             \
  add span_edge_data, psx_gpu, #psx_gpu_span_edge_data_offset;                 \
                                                                               \
  add span_b_offset, psx_gpu, #psx_gpu_span_b_offset_offset;                   \
  vmov.u8 d64_1, #1;                                                           \
                                                                               \
  vmov.u8 d128_4, #4;                                                          \
  vmov.u8 d64_128, #128;                                                       \
                                                                               \
  vmov.u8 d128_0x7, #0x7;                                                      \
  add mask_msb_ptr, psx_gpu, #psx_gpu_mask_msb_offset;                         \
  vld1.u16 { msb_mask_low[], msb_mask_high[] }, [mask_msb_ptr, :16];           \
                                                                               \
 0:                                                                            \
  ldrh span_num_blocks, [span_edge_data, #edge_data_num_blocks_offset];        \
  add dither_offset_ptr, psx_gpu, #psx_gpu_dither_table_offset;                \
                                                                               \
  ldrh y, [span_edge_data, #edge_data_y_offset];                               \
  ldr fb_ptr, [psx_gpu, #psx_gpu_vram_out_ptr_offset];                         \
                                                                               \
  cmp span_num_blocks, #0;                                                     \
  beq 1f;                                                                      \
                                                                               \
  ldrh left_x, [span_edge_data, #edge_data_left_x_offset];                     \
  add fb_ptr, fb_ptr, y, lsl #11;                                              \
                                                                               \
  ldr b, [span_b_offset];                                                      \
  vdup.u32 v_left_x, left_x;                                                   \
  and y, y, #0x3;                                                              \
                                                                               \
  ldr dither_row, [dither_offset_ptr, y, lsl #2];                              \
  add fb_ptr, fb_ptr, left_x, lsl #1;                                          \
                                                                               \
  mla b, b_dx, left_x, b;                                                      \
  and dither_shift, left_x, #0x03;                                             \
                                                                               \
  vld1.u32 { uvrg }, [span_uvrg_offset, :128];                                 \
  vshr.u32 rg_dx, rg_dx4, #2;                                                  \
                                                                               \
  mov dither_shift, dither_shift, lsl #3;                                      \
  vmla.u32 rg, rg_dx, v_left_x;                                                \
                                                                               \
  subs span_num_blocks, span_num_blocks, #1;                                   \
                                                                               \
  mov dither_row, dither_row, ror dither_shift;                                \
  mov b_dx4, b_dx, lsl #2;                                                     \
                                                                               \
  vdup.u32 dither_offsets, dither_row;                                         \
  add block_span_ptr, psx_gpu, #psx_gpu_r_block_span_offset;                   \
                                                                               \
  vdup.u32 b_block, b;                                                         \
  vadd.u8 dither_offsets, dither_offsets, d128_4;                              \
                                                                               \
  mov b_dx8, b_dx, lsl #3;                                                     \
  vdup.u32 r_block, rg[0];                                                     \
  vdup.u32 g_block, rg[1];                                                     \
                                                                               \
  vld1.u32 { block_span }, [block_span_ptr, :128]!;                            \
                                                                               \
  vadd.u32 r_block, r_block, block_span;                                       \
  vld1.u32 { block_span }, [block_span_ptr, :128]!;                            \
                                                                               \
  vadd.u32 g_block, g_block, block_span;                                       \
  vld1.u32 { block_span }, [block_span_ptr, :128];                             \
                                                                               \
  vadd.u32 b_block, b_block, block_span;                                       \
  add block_ptr_b, block_ptr_a, #16;                                           \
                                                                               \
  vshrn.u32 r_whole_low, r_block, #16;                                         \
  vshrn.u32 g_whole_low, g_block, #16;                                         \
  vshrn.u32 b_whole_low, b_block, #16;                                         \
  vdup.u32 dx4, rg_dx4[0];                                                     \
                                                                               \
  vaddhn.u32 r_whole_high, r_block, dx4;                                       \
  vdup.u32 dx4, rg_dx4[1];                                                     \
                                                                               \
  vaddhn.u32 g_whole_high, g_block, dx4;                                       \
  vdup.u32 dx4, b_dx4;                                                         \
                                                                               \
  vaddhn.u32 b_whole_high, b_block, dx4;                                       \
  vdup.u32 dx8, rg_dx8[0];                                                     \
                                                                               \
  vadd.u32 r_block, r_block, dx8;                                              \
  vdup.u32 dx8, rg_dx8[1];                                                     \
                                                                               \
  vadd.u32 g_block, g_block, dx8;                                              \
  vdup.u32 dx8, b_dx8;                                                         \
                                                                               \
  vadd.u32 b_block, b_block, dx8;                                              \
                                                                               \
  vmovn.u16 r_whole_8, r_whole;                                                \
  vmovn.u16 g_whole_8, g_whole;                                                \
  vmovn.u16 b_whole_8, b_whole;                                                \
                                                                               \
  beq 3f;                                                                      \
                                                                               \
 2:                                                                            \
  setup_blocks_shaded_untextured_dither_a_##dithering();                       \
  vshrn.u32 r_whole_low, r_block, #16;                                         \
                                                                               \
  setup_blocks_shaded_untextured_dither_b_##dithering();                       \
  vshrn.u32 g_whole_low, g_block, #16;                                         \
                                                                               \
  vshrn.u32 b_whole_low, b_block, #16;                                         \
                                                                               \
  vdup.u32 dx4, rg_dx4[0];                                                     \
  vshr.u8 r_whole_8, r_whole_8, #3;                                            \
  vbic.u8 gb_whole_8, gb_whole_8, d128_0x7;                                    \
                                                                               \
  vaddhn.u32 r_whole_high, r_block, dx4;                                       \
  vdup.u32 dx4, rg_dx4[1];                                                     \
                                                                               \
  vmov pixels, msb_mask;                                                       \
  vaddhn.u32 g_whole_high, g_block, dx4;                                       \
  vdup.u32 dx4, b_dx4;                                                         \
                                                                               \
  vaddhn.u32 b_whole_high, b_block, dx4;                                       \
  vdup.u32 dx8, rg_dx8[0];                                                     \
                                                                               \
  vmlal.u8 pixels, r_whole_8, d64_1;                                           \
  vmlal.u8 pixels, g_whole_8, d64_4;                                           \
  vmlal.u8 pixels, b_whole_8, d64_128;                                         \
                                                                               \
  vadd.u32 r_block, r_block, dx8;                                              \
  vdup.u32 dx8, rg_dx8[1];                                                     \
                                                                               \
  vadd.u32 g_block, g_block, dx8;                                              \
  vdup.u32 dx8, b_dx8;                                                         \
                                                                               \
  vadd.u32 b_block, b_block, dx8;                                              \
                                                                               \
  vmovn.u16 r_whole_8, r_whole;                                                \
  vmovn.u16 g_whole_8, g_whole;                                                \
  vmovn.u16 b_whole_8, b_whole;                                                \
                                                                               \
  vst1.u32 { pixels }, [fb_ptr]!;                                              \
  subs span_num_blocks, span_num_blocks, #1;                                   \
  bne 2b;                                                                      \
                                                                               \
 3:                                                                            \
  setup_blocks_shaded_untextured_dither_a_##dithering();                       \
                                                                               \
  ldrh right_mask, [span_edge_data, #edge_data_right_mask_offset];             \
  setup_blocks_shaded_untextured_dither_b_##dithering();                       \
                                                                               \
  vshr.u8 r_whole_8, r_whole_8, #3;                                            \
  rbit right_mask, right_mask;                                                 \
  vmov pixels, msb_mask;                                                       \
  vbic.u8 gb_whole_8, gb_whole_8, d128_0x7;                                    \
  clz right_mask, right_mask;                                                  \
                                                                               \
  vmlal.u8 pixels, r_whole_8, d64_1;                                           \
  vmlal.u8 pixels, g_whole_8, d64_4;                                           \
  vmlal.u8 pixels, b_whole_8, d64_128;                                         \
                                                                               \
  JT_OP_REL(100f, right_mask, temp);                                           \
  JT_OP(ldr pc, [pc, right_mask, lsl #2]);                                     \
  nop;                                                                         \
 100:                                                                          \
  nop;                                                                         \
  .word JTE(100b, 4f);                                                         \
  .word JTE(100b, 5f);                                                         \
  .word JTE(100b, 6f);                                                         \
  .word JTE(100b, 7f);                                                         \
  .word JTE(100b, 8f);                                                         \
  .word JTE(100b, 9f);                                                         \
  .word JTE(100b, 10f);                                                        \
  .word JTE(100b, 11f);                                                        \
                                                                               \
 4:                                                                            \
  vst1.u16 { pixels_low[0] }, [fb_ptr];                                        \
  bal 1f;                                                                      \
                                                                               \
 5:                                                                            \
  vst1.u32 { pixels_low[0] }, [fb_ptr];                                        \
  bal 1f;                                                                      \
                                                                               \
 6:                                                                            \
  vst1.u32 { pixels_low[0] }, [fb_ptr]!;                                       \
  vst1.u16 { pixels_low[2] }, [fb_ptr];                                        \
  bal 1f;                                                                      \
                                                                               \
 7:                                                                            \
  vst1.u32 { pixels_low }, [fb_ptr];                                           \
  bal 1f;                                                                      \
                                                                               \
 8:                                                                            \
  vst1.u32 { pixels_low }, [fb_ptr]!;                                          \
  vst1.u16 { pixels_high[0] }, [fb_ptr];                                       \
  bal 1f;                                                                      \
                                                                               \
 9:                                                                            \
  vst1.u32 { pixels_low }, [fb_ptr]!;                                          \
  vst1.u32 { pixels_high[0] }, [fb_ptr]!;                                      \
  bal 1f;                                                                      \
                                                                               \
 10:                                                                           \
  vst1.u32 { pixels_low }, [fb_ptr]!;                                          \
  vst1.u32 { pixels_high[0] }, [fb_ptr]!;                                      \
  vst1.u16 { pixels_high[2] }, [fb_ptr];                                       \
  bal 1f;                                                                      \
                                                                               \
 11:                                                                           \
  vst1.u32 { pixels }, [fb_ptr];                                               \
  bal 1f;                                                                      \
                                                                               \
 1:                                                                            \
  add span_uvrg_offset, span_uvrg_offset, #16;                                 \
  add span_b_offset, span_b_offset, #4;                                        \
                                                                               \
  add span_edge_data, span_edge_data, #8;                                      \
  subs num_spans, num_spans, #1;                                               \
                                                                               \
  bne 0b;                                                                      \
                                                                               \
  ldmia sp!, { r4 - r11, pc }                                                  \

setup_blocks_shaded_untextured_direct_builder(undithered)
setup_blocks_shaded_untextured_direct_builder(dithered)


#undef psx_gpu
#undef num_blocks
#undef triangle
#undef c_64

#define psx_gpu                                  r0
#define block_ptr                                r1
#define num_blocks                               r2
#define uv_01                                    r3
#define uv_23                                    r4
#define uv_45                                    r5
#define uv_67                                    r6
#define uv_0                                     r7
#define uv_1                                     r3
#define uv_2                                     r8
#define uv_3                                     r4
#define uv_4                                     r9
#define uv_5                                     r5
#define uv_6                                     r10
#define uv_7                                     r6
#define texture_ptr                              r11

#define pixel_0                                  r7
#define pixel_1                                  r3
#define pixel_2                                  r8
#define pixel_3                                  r4
#define pixel_4                                  r9
#define pixel_5                                  r5
#define pixel_6                                  r10
#define pixel_7                                  r6

#define pixels_a                                 r7
#define pixels_b                                 r9
#define pixels_c                                 r8
#define pixels_d                                 r10

#define c_64                                     r0

#define clut_ptr                                 r12
#define current_texture_mask                     r5
#define dirty_textures_mask                      r6

#define texels                                   d0

#define clut_low_a                               d2
#define clut_low_b                               d3
#define clut_high_a                              d4
#define clut_high_b                              d5

#define clut_a                                   q1
#define clut_b                                   q2

#define texels_low                               d6
#define texels_high                              d7

.align 3

function(texture_blocks_untextured)
  bx lr


.align 3

function(texture_blocks_4bpp)
  stmdb sp!, { r3 - r11, r14 }
  add block_ptr, psx_gpu, #psx_gpu_blocks_offset

  ldr texture_ptr, [psx_gpu, #psx_gpu_texture_page_ptr_offset]
  ldrh num_blocks, [psx_gpu, #psx_gpu_num_blocks_offset]

  ldr clut_ptr, [psx_gpu, #psx_gpu_clut_ptr_offset]
  vld1.u32 { clut_a, clut_b }, [clut_ptr, :128]

  ldr current_texture_mask, [psx_gpu, #psx_gpu_current_texture_mask_offset]
  vuzp.u8 clut_a, clut_b

  ldr dirty_textures_mask, [psx_gpu, #psx_gpu_dirty_textures_4bpp_mask_offset]
  tst dirty_textures_mask, current_texture_mask

  bne 1f
  mov c_64, #64

0:
  ldm block_ptr, { uv_01, uv_23, uv_45, uv_67 }

  uxtah uv_0, texture_ptr, uv_01
  uxtah uv_1, texture_ptr, uv_01, ror #16

  uxtah uv_2, texture_ptr, uv_23
  uxtah uv_3, texture_ptr, uv_23, ror #16

  uxtah uv_4, texture_ptr, uv_45
  ldrb pixel_0, [uv_0]

  uxtah uv_5, texture_ptr, uv_45, ror #16
  ldrb pixel_1, [uv_1]

  uxtah uv_6, texture_ptr, uv_67
  ldrb pixel_2, [uv_2]

  uxtah uv_7, texture_ptr, uv_67, ror #16
  ldrb pixel_3, [uv_3]

  ldrb pixel_4, [uv_4]
  subs num_blocks, num_blocks, #1

  ldrb pixel_5, [uv_5]
  orr pixels_a, pixel_0, pixel_1, lsl #8

  ldrb pixel_6, [uv_6]
  orr pixels_b, pixel_4, pixel_5, lsl #8

  ldrb pixel_7, [uv_7]
  orr pixels_a, pixels_a, pixel_2, lsl #16

  orr pixels_b, pixels_b, pixel_6, lsl #16
  orr pixels_a, pixels_a, pixel_3, lsl #24

  orr pixels_b, pixels_b, pixel_7, lsl #24
  vmov texels, pixels_a, pixels_b

  vtbl.8 texels_low, { clut_low_a, clut_low_b }, texels
  vtbl.8 texels_high, { clut_high_a, clut_high_b }, texels

  vst2.u8 { texels_low, texels_high }, [block_ptr, :128], c_64
  bne 0b

  ldmia sp!, { r3 - r11, pc }

1:
  stmdb sp!, { r1 - r2 }  
  bl update_texture_4bpp_cache

  mov c_64, #64
  ldmia sp!, { r1 - r2 }
  bal 0b


.align 3

function(texture_blocks_8bpp)
  stmdb sp!, { r3 - r11, r14 }
  add block_ptr, psx_gpu, #psx_gpu_blocks_offset

  ldr texture_ptr, [psx_gpu, #psx_gpu_texture_page_ptr_offset]
  ldrh num_blocks, [psx_gpu, #psx_gpu_num_blocks_offset]

  ldr clut_ptr, [psx_gpu, #psx_gpu_clut_ptr_offset]
  ldr current_texture_mask, [psx_gpu, #psx_gpu_current_texture_mask_offset]

  ldr dirty_textures_mask, [psx_gpu, #psx_gpu_dirty_textures_8bpp_mask_offset]
  tst dirty_textures_mask, current_texture_mask

  bne 1f
  nop

0:
  ldm block_ptr, { uv_01, uv_23, uv_45, uv_67 }

  uxtah uv_0, texture_ptr, uv_01
  uxtah uv_1, texture_ptr, uv_01, ror #16

  uxtah uv_2, texture_ptr, uv_23
  uxtah uv_3, texture_ptr, uv_23, ror #16

  uxtah uv_4, texture_ptr, uv_45
  ldrb pixel_0, [uv_0]

  uxtah uv_5, texture_ptr, uv_45, ror #16
  ldrb pixel_1, [uv_1]

  uxtah uv_6, texture_ptr, uv_67
  ldrb pixel_2, [uv_2]

  uxtah uv_7, texture_ptr, uv_67, ror #16
  ldrb pixel_3, [uv_3]

  ldrb pixel_4, [uv_4]
  add pixel_0, pixel_0, pixel_0

  ldrb pixel_5, [uv_5]
  add pixel_1, pixel_1, pixel_1

  ldrb pixel_6, [uv_6]
  add pixel_2, pixel_2, pixel_2

  ldrb pixel_7, [uv_7]
  add pixel_3, pixel_3, pixel_3

  ldrh pixel_0, [clut_ptr, pixel_0]
  add pixel_4, pixel_4, pixel_4

  ldrh pixel_1, [clut_ptr, pixel_1]
  add pixel_5, pixel_5, pixel_5

  ldrh pixel_2, [clut_ptr, pixel_2]
  add pixel_6, pixel_6, pixel_6

  ldrh pixel_3, [clut_ptr, pixel_3]
  add pixel_7, pixel_7, pixel_7

  ldrh pixel_4, [clut_ptr, pixel_4]
  orr pixels_a, pixel_0, pixel_1, lsl #16

  ldrh pixel_5, [clut_ptr, pixel_5]
  orr pixels_c, pixel_2, pixel_3, lsl #16

  ldrh pixel_6, [clut_ptr, pixel_6]
  subs num_blocks, num_blocks, #1

  ldrh pixel_7, [clut_ptr, pixel_7]
  orr pixels_b, pixel_4, pixel_5, lsl #16

  orr pixels_d, pixel_6, pixel_7, lsl #16
  stm block_ptr, { pixels_a, pixels_c, pixels_b, pixels_d } 

  add block_ptr, block_ptr, #64
  bne 0b

  ldmia sp!, { r3 - r11, pc }

1:
  stmdb sp!, { r1 - r2, EXTRA_UNSAVED_REGS r12 }

  bl update_texture_8bpp_cache

  ldmia sp!, { r1 - r2, EXTRA_UNSAVED_REGS r12 }
  bal 0b


#undef uv_0
#undef uv_1
#undef uv_2
#undef uv_3
#undef uv_4
#undef uv_5
#undef uv_6
#undef uv_7

#undef pixel_0
#undef pixel_1
#undef pixel_2
#undef pixel_3
#undef pixel_4
#undef pixel_5
#undef pixel_6
#undef pixel_7

#undef texture_ptr

#undef pixels_a
#undef pixels_b
#undef pixels_c
#undef pixels_d

#define psx_gpu                                  r0
#define block_ptr                                r1
#define num_blocks                               r2

#define uv_0                                     r3
#define uv_1                                     r4
#define u_0                                      r3
#define u_1                                      r4
#define v_0                                      r5
#define v_1                                      r6

#define uv_2                                     r5
#define uv_3                                     r6
#define u_2                                      r5
#define u_3                                      r6
#define v_2                                      r7
#define v_3                                      r8

#define uv_4                                     r7
#define uv_5                                     r8
#define u_4                                      r7
#define u_5                                      r8
#define v_4                                      r9
#define v_5                                      r10

#define uv_6                                     r9
#define uv_7                                     r10
#define u_6                                      r9
#define u_7                                      r10
#define v_6                                      r11
#define v_7                                      r0

#define pixel_0                                  r3
#define pixel_1                                  r4
#define pixel_2                                  r5
#define pixel_3                                  r6
#define pixel_4                                  r7
#define pixel_5                                  r8
#define pixel_6                                  r9
#define pixel_7                                  r10

#define pixels_a                                 r3
#define pixels_b                                 r5
#define pixels_c                                 r7
#define pixels_d                                 r9

#define texture_ptr                              r12


.align 3

function(texture_blocks_16bpp)
  stmdb sp!, { r3 - r11, r14 }
  add block_ptr, psx_gpu, #psx_gpu_blocks_offset

  ldrh num_blocks, [psx_gpu, #psx_gpu_num_blocks_offset]
  ldr texture_ptr, [psx_gpu, #psx_gpu_texture_page_ptr_offset]

0:
  ldrh uv_0, [block_ptr]
  subs num_blocks, num_blocks, #1

  ldrh uv_1, [block_ptr, #2]

  and v_0, uv_0, #0xFF00
  and v_1, uv_1, #0xFF00

  and u_0, uv_0, #0xFF
  and u_1, uv_1, #0xFF

  add uv_0, u_0, v_0, lsl #2
  ldrh uv_2, [block_ptr, #4]

  add uv_1, u_1, v_1, lsl #2
  ldrh uv_3, [block_ptr, #6]

  add uv_0, uv_0, uv_0
  add uv_1, uv_1, uv_1

  and v_2, uv_2, #0xFF00
  and v_3, uv_3, #0xFF00

  and u_2, uv_2, #0xFF
  and u_3, uv_3, #0xFF

  add uv_2, u_2, v_2, lsl #2
  ldrh uv_4, [block_ptr, #8]

  add uv_3, u_3, v_3, lsl #2
  ldrh uv_5, [block_ptr, #10]

  add uv_2, uv_2, uv_2
  add uv_3, uv_3, uv_3

  and v_4, uv_4, #0xFF00
  and v_5, uv_5, #0xFF00

  and u_4, uv_4, #0xFF
  and u_5, uv_5, #0xFF

  add uv_4, u_4, v_4, lsl #2
  ldrh uv_6, [block_ptr, #12]

  add uv_5, u_5, v_5, lsl #2
  ldrh uv_7, [block_ptr, #14]

  add uv_4, uv_4, uv_4
  ldrh pixel_0, [texture_ptr, uv_0]

  add uv_5, uv_5, uv_5
  ldrh pixel_1, [texture_ptr, uv_1]

  and v_6, uv_6, #0xFF00
  ldrh pixel_2, [texture_ptr, uv_2]

  and v_7, uv_7, #0xFF00
  ldrh pixel_3, [texture_ptr, uv_3]

  and u_6, uv_6, #0xFF
  ldrh pixel_4, [texture_ptr, uv_4]

  and u_7, uv_7, #0xFF
  ldrh pixel_5, [texture_ptr, uv_5]

  add uv_6, u_6, v_6, lsl #2
  add uv_7, u_7, v_7, lsl #2

  add uv_6, uv_6, uv_6
  add uv_7, uv_7, uv_7

  orr pixels_a, pixel_0, pixel_1, lsl #16
  orr pixels_b, pixel_2, pixel_3, lsl #16

  ldrh pixel_6, [texture_ptr, uv_6]
  orr pixels_c, pixel_4, pixel_5, lsl #16

  ldrh pixel_7, [texture_ptr, uv_7]
  orr pixels_d, pixel_6, pixel_7, lsl #16

  stm block_ptr, { pixels_a, pixels_b, pixels_c, pixels_d }
  add block_ptr, block_ptr, #64

  bne 0b

  ldmia sp!, { r3 - r11, pc }


#undef num_blocks

#undef test_mask
#undef texels
#undef pixels_b
#undef pixels
#undef d64_1
#undef d64_4
#undef d64_128
#undef draw_mask
#undef msb_mask
#undef msb_mask_low
#undef msb_mask_high
#undef fb_pixels

#undef c_32
#undef fb_ptr
#undef mask_msb_ptr

#define psx_gpu                                  r0
#define num_blocks                               r1
#define color_ptr                                r2
#define colors_scalar                            r2
#define colors_scalar_compare                    r3
#define mask_msb_ptr                             r2

#define block_ptr_load_a                         r0
#define block_ptr_store                          r3
#define block_ptr_load_b                         r12
#define c_32                                     r2

#define c_48                                     r4
#define fb_ptr                                   r14
#define draw_mask_bits_scalar                    r5

#define d128_0x07                                q0
#define d128_0x1F                                q1
#define d128_0x8000                              q2
#define test_mask                                q3
#define texels                                   q4
#define colors_rg                                q5
#define colors_b_dm_bits                         q6
#define texels_rg                                q7
#define pixels_r                                 q8
#define pixels_g                                 q9
#define pixels_b                                 q10
#define pixels                                   q11
#define zero_mask                                q4
#define draw_mask                                q12
#define msb_mask                                 q13

#define fb_pixels                                q8

#define pixels_gb_low                            q9

#define colors_r                                 d10
#define colors_g                                 d11
#define colors_b                                 d12
#define draw_mask_bits                           d13
#define texels_r                                 d14
#define texels_g                                 d15
#define pixels_r_low                             d16
#define pixels_g_low                             d18
#define pixels_b_low                             d19
#define msb_mask_low                             d26
#define msb_mask_high                            d27

#define d64_1                                    d28
#define d64_4                                    d29
#define d64_128                                  d30
#define texels_b                                 d31

#define shade_blocks_textured_modulated_prologue_indirect()                    \
  mov c_48, #48;                                                               \
  add block_ptr_store, psx_gpu, #psx_gpu_blocks_offset                         \

#define shade_blocks_textured_modulated_prologue_direct()                      \
  add mask_msb_ptr, psx_gpu, #psx_gpu_mask_msb_offset;                         \
  vld1.u16 { msb_mask_low[], msb_mask_high[] }, [mask_msb_ptr, :16]            \


#define shade_blocks_textured_modulated_prologue_shaded(dithering, target)     \
  
#define shade_blocks_textured_false_modulation_check_undithered(target)        \
  ldr colors_scalar, [psx_gpu, #psx_gpu_triangle_color_offset];                \
  movw colors_scalar_compare, #0x8080;                                         \
                                                                               \
  movt colors_scalar_compare, #0x80;                                           \
  cmp colors_scalar, colors_scalar_compare;                                    \
  beq shade_blocks_textured_unmodulated_##target                               \

#define shade_blocks_textured_false_modulation_check_dithered(target)          \

#define shade_blocks_textured_modulated_prologue_unshaded(dithering, target)   \
  shade_blocks_textured_false_modulation_check_##dithering(target);            \
  add color_ptr, psx_gpu, #psx_gpu_triangle_color_offset;                      \
  vld1.u32 { colors_r[] }, [color_ptr, :32];                                   \
  vdup.u8 colors_g, colors_r[1];                                               \
  vdup.u8 colors_b, colors_r[2];                                               \
  vdup.u8 colors_r, colors_r[0]                                                \


#define shade_blocks_textured_modulated_load_dithered(target)                  \
  vld1.u32 { target }, [block_ptr_load_b, :128]                                \

#define shade_blocks_textured_modulated_load_last_dithered(target)             \
  vld1.u32 { target }, [block_ptr_load_b, :128], c_32                          \

#define shade_blocks_textured_modulated_load_undithered(target)                \

#define shade_blocks_textured_modulated_load_last_undithered(target)           \
  add block_ptr_load_b, block_ptr_load_b, #32                                  \

#define shade_blocks_textured_modulate_dithered(channel)                       \
  vmlal.u8 pixels_##channel, texels_##channel, colors_##channel                \

#define shade_blocks_textured_modulate_undithered(channel)                     \
  vmull.u8 pixels_##channel, texels_##channel, colors_##channel                \


#define shade_blocks_textured_modulated_store_draw_mask_indirect(offset)       \
  vst1.u32 { draw_mask }, [block_ptr_store, :128]!                             \

#define shade_blocks_textured_modulated_store_draw_mask_direct(offset)         \
  ldr fb_ptr, [block_ptr_load_b, #(offset - 64)];                              \
  vld1.u32 { fb_pixels }, [fb_ptr];                                            \
  vbit.u16 pixels, fb_pixels, draw_mask                                        \

#define shade_blocks_textured_modulated_store_pixels_indirect()                \
  vst1.u32 { pixels }, [block_ptr_store, :128], c_48                           \

#define shade_blocks_textured_modulated_store_pixels_direct()                  \
  vst1.u32 { pixels }, [fb_ptr]                                                \


#define shade_blocks_textured_modulated_load_rg_shaded()                       \
  vld1.u32 { colors_r, colors_g }, [block_ptr_load_b, :128], c_32              \

#define shade_blocks_textured_modulated_load_rg_unshaded()                     \
  add block_ptr_load_b, block_ptr_load_b, #32                                  \

#define shade_blocks_textured_modulated_load_bdm_shaded()                      \
  vld1.u32 { colors_b, draw_mask_bits }, [block_ptr_load_a, :128], c_32        \

#define shade_blocks_textured_modulated_load_bdm_unshaded()                    \
  ldr draw_mask_bits_scalar, [block_ptr_load_a, #8];                           \
  add block_ptr_load_a, block_ptr_load_a, #32                                  \

#define shade_blocks_textured_modulated_expand_draw_mask_shaded()              \
  vdup.u16 draw_mask, draw_mask_bits[0]                                        \

#define shade_blocks_textured_modulated_expand_draw_mask_unshaded()            \
  vdup.u16 draw_mask, draw_mask_bits_scalar                                    \


#define shade_blocks_textured_modulated_apply_msb_mask_indirect()              \

#define shade_blocks_textured_modulated_apply_msb_mask_direct()                \
  vorr.u16 pixels, pixels, msb_mask                                            \


#define shade_blocks_textured_modulated_builder(shading, dithering, target)    \
.align 3;                                                                      \
                                                                               \
function(shade_blocks_##shading##_textured_modulated_##dithering##_##target)   \
  shade_blocks_textured_modulated_prologue_##shading(dithering, target);       \
  stmdb sp!, { r4 - r5, lr };                                                  \
  ldrh num_blocks, [psx_gpu, #psx_gpu_num_blocks_offset];                      \
                                                                               \
  vld1.u32 { test_mask }, [psx_gpu, :128];                                     \
                                                                               \
  shade_blocks_textured_modulated_prologue_##target();                         \
                                                                               \
  add block_ptr_load_a, psx_gpu, #psx_gpu_blocks_offset;                       \
  mov c_32, #32;                                                               \
                                                                               \
  add block_ptr_load_b, block_ptr_load_a, #16;                                 \
  vmov.u8 d64_1, #1;                                                           \
  vmov.u8 d64_4, #4;                                                           \
  vmov.u8 d64_128, #128;                                                       \
                                                                               \
  vld1.u32 { texels }, [block_ptr_load_a, :128], c_32;                         \
  vmov.u8 d128_0x07, #0x07;                                                    \
                                                                               \
  shade_blocks_textured_modulated_load_rg_##shading();                         \
  vmov.u8 d128_0x1F, #0x1F;                                                    \
                                                                               \
  shade_blocks_textured_modulated_load_bdm_##shading();                        \
  vmov.u16 d128_0x8000, #0x8000;                                               \
                                                                               \
  vmovn.u16 texels_r, texels;                                                  \
  vshrn.u16 texels_g, texels, #5;                                              \
                                                                               \
  vshrn.u16 texels_b, texels, #7;                                              \
  shade_blocks_textured_modulated_expand_draw_mask_##shading();                \
                                                                               \
  shade_blocks_textured_modulated_load_##dithering(pixels_r);                  \
  vtst.u16 draw_mask, draw_mask, test_mask;                                    \
                                                                               \
  shade_blocks_textured_modulated_load_##dithering(pixels_g);                  \
  vand.u8 texels_rg, texels_rg, d128_0x1F;                                     \
                                                                               \
  shade_blocks_textured_modulated_load_last_##dithering(pixels_b);             \
  vshr.u8 texels_b, texels_b, #3;                                              \
                                                                               \
  shade_blocks_textured_modulate_##dithering(r);                               \
  shade_blocks_textured_modulate_##dithering(g);                               \
  shade_blocks_textured_modulate_##dithering(b);                               \
                                                                               \
  vand.u16 pixels, texels, d128_0x8000;                                        \
  vceq.u16 zero_mask, texels, #0;                                              \
                                                                               \
  vqshrun.s16 pixels_r_low, pixels_r, #4;                                      \
  vqshrun.s16 pixels_g_low, pixels_g, #4;                                      \
  vqshrun.s16 pixels_b_low, pixels_b, #4;                                      \
                                                                               \
  shade_blocks_textured_modulated_apply_msb_mask_##target();                   \
  vorr.u16 draw_mask, draw_mask, zero_mask;                                    \
  vshr.u8 pixels_r_low, pixels_r_low, #3;                                      \
  vbic.u8 pixels_gb_low, pixels_gb_low, d128_0x07;                             \
                                                                               \
  subs num_blocks, num_blocks, #1;                                             \
  beq 1f;                                                                      \
                                                                               \
 .align 3;                                                                     \
                                                                               \
 0:                                                                            \
  vld1.u32 { texels }, [block_ptr_load_a, :128], c_32;                         \
  shade_blocks_textured_modulated_load_rg_##shading();                         \
  vshrn.u16 texels_g, texels, #5;                                              \
                                                                               \
  shade_blocks_textured_modulated_load_bdm_##shading();                        \
  vshrn.u16 texels_b, texels, #7;                                              \
                                                                               \
  pld [block_ptr_load_a];                                                      \
  vmovn.u16 texels_r, texels;                                                  \
  vmlal.u8 pixels, pixels_r_low, d64_1;                                        \
                                                                               \
  vmlal.u8 pixels, pixels_g_low, d64_4;                                        \
  vmlal.u8 pixels, pixels_b_low, d64_128;                                      \
  shade_blocks_textured_modulated_store_draw_mask_##target(-4);                \
                                                                               \
  shade_blocks_textured_modulated_load_##dithering(pixels_r);                  \
  shade_blocks_textured_modulated_expand_draw_mask_##shading();                \
                                                                               \
  shade_blocks_textured_modulated_load_##dithering(pixels_g);                  \
  vand.u8 texels_rg, texels_rg, d128_0x1F;                                     \
                                                                               \
  shade_blocks_textured_modulated_load_last_##dithering(pixels_b);             \
  vtst.u16 draw_mask, draw_mask, test_mask;                                    \
                                                                               \
  shade_blocks_textured_modulated_store_pixels_##target();                     \
  vshr.u8 texels_b, texels_b, #3;                                              \
                                                                               \
  shade_blocks_textured_modulate_##dithering(r);                               \
  shade_blocks_textured_modulate_##dithering(g);                               \
  shade_blocks_textured_modulate_##dithering(b);                               \
                                                                               \
  vand.u16 pixels, texels, d128_0x8000;                                        \
  vceq.u16 zero_mask, texels, #0;                                              \
                                                                               \
  subs num_blocks, num_blocks, #1;                                             \
                                                                               \
  vqshrun.s16 pixels_r_low, pixels_r, #4;                                      \
  vqshrun.s16 pixels_g_low, pixels_g, #4;                                      \
  vqshrun.s16 pixels_b_low, pixels_b, #4;                                      \
                                                                               \
  shade_blocks_textured_modulated_apply_msb_mask_##target();                   \
  vorr.u16 draw_mask, draw_mask, zero_mask;                                    \
  vshr.u8 pixels_r_low, pixels_r_low, #3;                                      \
  vbic.u8 pixels_gb_low, pixels_gb_low, d128_0x07;                             \
                                                                               \
  bne 0b;                                                                      \
                                                                               \
 1:                                                                            \
  vmlal.u8 pixels, pixels_r_low, d64_1;                                        \
  vmlal.u8 pixels, pixels_g_low, d64_4;                                        \
  vmlal.u8 pixels, pixels_b_low, d64_128;                                      \
                                                                               \
  shade_blocks_textured_modulated_store_draw_mask_##target(28);                \
  shade_blocks_textured_modulated_store_pixels_##target();                     \
                                                                               \
  ldmia sp!, { r4 - r5, pc }                                                   \


shade_blocks_textured_modulated_builder(shaded, dithered, direct);
shade_blocks_textured_modulated_builder(shaded, undithered, direct);
shade_blocks_textured_modulated_builder(unshaded, dithered, direct);
shade_blocks_textured_modulated_builder(unshaded, undithered, direct);

shade_blocks_textured_modulated_builder(shaded, dithered, indirect);
shade_blocks_textured_modulated_builder(shaded, undithered, indirect);
shade_blocks_textured_modulated_builder(unshaded, dithered, indirect);
shade_blocks_textured_modulated_builder(unshaded, undithered, indirect);


#undef c_64
#undef fb_ptr
#undef color_ptr

#undef color_r
#undef color_g
#undef color_b

#undef test_mask
#undef pixels
#undef draw_mask
#undef zero_mask
#undef fb_pixels
#undef msb_mask
#undef msb_mask_low
#undef msb_mask_high

#define psx_gpu                                  r0
#define num_blocks                               r1
#define mask_msb_ptr                             r2
#define color_ptr                                r3

#define block_ptr_load                           r0
#define draw_mask_store_ptr                      r3
#define draw_mask_bits_ptr                       r12
#define draw_mask_ptr                            r12
#define pixel_store_ptr                          r14

#define fb_ptr_cmp                               r4

#define fb_ptr                                   r3
#define fb_ptr_next                              r14

#define c_64                                     r2

#define test_mask                                q0
#define pixels                                   q1
#define draw_mask                                q2
#define zero_mask                                q3
#define draw_mask_combined                       q4
#define fb_pixels                                q5
#define fb_pixels_next                           q6
#define msb_mask                                 q7

#define draw_mask_low                            d4
#define draw_mask_high                           d5
#define msb_mask_low                             d14
#define msb_mask_high                            d15

.align 3
function(shade_blocks_textured_unmodulated_indirect)
  str r14, [sp, #-4]
  add draw_mask_bits_ptr, psx_gpu, #(psx_gpu_blocks_offset + 40)

  ldrh num_blocks, [psx_gpu, #psx_gpu_num_blocks_offset]
  add pixel_store_ptr, psx_gpu, #(psx_gpu_blocks_offset + 16)

  vld1.u32 { test_mask }, [psx_gpu, :128]
  add draw_mask_store_ptr, psx_gpu, #psx_gpu_blocks_offset

  mov c_64, #64
  add block_ptr_load, psx_gpu, #psx_gpu_blocks_offset

  vld1.u32 { pixels }, [block_ptr_load, :128], c_64
  vld1.u16 { draw_mask_low[], draw_mask_high[] },                              \
   [draw_mask_bits_ptr, :16], c_64
  vceq.u16 zero_mask, pixels, #0

  vtst.u16 draw_mask, draw_mask, test_mask
  vst1.u32 { pixels }, [pixel_store_ptr, :128], c_64

  subs num_blocks, num_blocks, #1
  beq 1f

 0:
  vld1.u32 { pixels }, [block_ptr_load, :128], c_64
  vorr.u16 draw_mask_combined, draw_mask, zero_mask

  vld1.u16 { draw_mask_low[], draw_mask_high[] },                              \
   [draw_mask_bits_ptr, :16], c_64
  vceq.u16 zero_mask, pixels, #0

  vtst.u16 draw_mask, draw_mask, test_mask
  vst1.u32 { pixels }, [pixel_store_ptr, :128], c_64

  vst1.u32 { draw_mask_combined }, [draw_mask_store_ptr, :128], c_64
  subs num_blocks, num_blocks, #1

  bne 0b

 1:
  vorr.u16 draw_mask_combined, draw_mask, zero_mask
  vst1.u32 { draw_mask_combined }, [draw_mask_store_ptr, :128], c_64

  ldr pc, [sp, #-4]


.align 3

function(shade_blocks_textured_unmodulated_direct)
  stmdb sp!, { r4, r14 }
  add draw_mask_bits_ptr, psx_gpu, #(psx_gpu_blocks_offset + 40)

  ldrh num_blocks, [psx_gpu, #psx_gpu_num_blocks_offset]
  add mask_msb_ptr, psx_gpu, #psx_gpu_mask_msb_offset

  vld1.u16 { msb_mask_low[], msb_mask_high[] }, [mask_msb_ptr, :16]
  mov c_64, #64

  vld1.u32 { test_mask }, [psx_gpu, :128]
  add block_ptr_load, psx_gpu, #psx_gpu_blocks_offset

  vld1.u16 { draw_mask_low[], draw_mask_high[] },                              \
   [draw_mask_bits_ptr, :16], c_64
  ldr fb_ptr_next, [block_ptr_load, #44]

  vld1.u32 { pixels }, [block_ptr_load, :128], c_64
  vld1.u16 { fb_pixels_next }, [fb_ptr_next]
  vceq.u16 zero_mask, pixels, #0
  vtst.u16 draw_mask, draw_mask, test_mask

  subs num_blocks, num_blocks, #1
  beq 1f

 0:
  mov fb_ptr, fb_ptr_next
  ldr fb_ptr_next, [block_ptr_load, #44]

  vorr.u16 pixels, pixels, msb_mask

  vorr.u16 draw_mask_combined, draw_mask, zero_mask
  vmov fb_pixels, fb_pixels_next

  vld1.u16 { draw_mask_low[], draw_mask_high[] },                              \
   [draw_mask_bits_ptr, :16], c_64
  vbif.u16 fb_pixels, pixels, draw_mask_combined

  sub fb_ptr_cmp, fb_ptr_next, fb_ptr
  pld [fb_ptr_next, #64]

  add fb_ptr_cmp, fb_ptr_cmp, #14
  vld1.u32 { pixels }, [block_ptr_load, :128], c_64

  cmp fb_ptr_cmp, #28
  bls 4f

  vld1.u16 { fb_pixels_next }, [fb_ptr_next]
  vceq.u16 zero_mask, pixels, #0

  vst1.u16 { fb_pixels }, [fb_ptr]
  vtst.u16 draw_mask, draw_mask, test_mask

 3:
  subs num_blocks, num_blocks, #1
  bne 0b

 1:
  vorr.u16 draw_mask_combined, draw_mask, zero_mask
  vbif.u16 fb_pixels_next, pixels, draw_mask_combined

  vst1.u16 { fb_pixels_next }, [fb_ptr_next]

  ldmia sp!, { r4, pc }

 4:
  vst1.u16 { fb_pixels }, [fb_ptr]
  vceq.u16 zero_mask, pixels, #0

  vld1.u16 { fb_pixels_next }, [fb_ptr_next]
  vtst.u16 draw_mask, draw_mask, test_mask

  bal 3b


function(shade_blocks_unshaded_untextured_indirect)
  bx lr

.align 3

function(shade_blocks_unshaded_untextured_direct)
  stmdb sp!, { r4, r14 }
  add draw_mask_ptr, psx_gpu, #psx_gpu_blocks_offset

  ldrh num_blocks, [psx_gpu, #psx_gpu_num_blocks_offset]
  add mask_msb_ptr, psx_gpu, #psx_gpu_mask_msb_offset

  vld1.u16 { msb_mask_low[], msb_mask_high[] }, [mask_msb_ptr, :16]
  add color_ptr, psx_gpu, #(psx_gpu_blocks_offset + 16)

  add block_ptr_load, psx_gpu, #(psx_gpu_blocks_offset + 44)
  vld1.u16 { pixels }, [color_ptr, :128]

  mov c_64, #64
  vld1.u16 { draw_mask }, [draw_mask_ptr, :128], c_64

  vorr.u16 pixels, pixels, msb_mask
  subs num_blocks, num_blocks, #1

  ldr fb_ptr_next, [block_ptr_load], #64

  vld1.u16 { fb_pixels_next }, [fb_ptr_next]
  beq 1f

 0:
  vmov fb_pixels, fb_pixels_next
  mov fb_ptr, fb_ptr_next
  ldr fb_ptr_next, [block_ptr_load], #64

  vbif.u16 fb_pixels, pixels, draw_mask
  vld1.u16 { draw_mask }, [draw_mask_ptr, :128], c_64

  sub fb_ptr_cmp, fb_ptr_next, fb_ptr
  add fb_ptr_cmp, fb_ptr_cmp, #14
  cmp fb_ptr_cmp, #28
  bls 4f

  vld1.u16 { fb_pixels_next }, [fb_ptr_next]
  vst1.u16 { fb_pixels }, [fb_ptr]

 3:
  subs num_blocks, num_blocks, #1
  bne 0b

 1:
  vbif.u16 fb_pixels_next, pixels, draw_mask
  vst1.u16 { fb_pixels_next }, [fb_ptr_next]

  ldmia sp!, { r4, pc }

 4:
  vst1.u16 { fb_pixels }, [fb_ptr]
  vld1.u16 { fb_pixels_next }, [fb_ptr_next]
  bal 3b


#undef draw_mask_ptr
#undef c_64
#undef fb_ptr
#undef fb_ptr_next
#undef fb_ptr_cmp

#define psx_gpu                                  r0
#define num_blocks                               r1
#define msb_mask_ptr                             r2
#define pixel_ptr                                r3
#define draw_mask_ptr                            r0
#define c_64                                     r2
#define fb_ptr                                   r12
#define fb_ptr_next                              r14
#define fb_ptr_cmp                               r4

#undef msb_mask
#undef draw_mask
#undef pixels
#undef fb_pixels
#undef d128_0x8000
#undef msb_mask_low
#undef msb_mask_high
#undef draw_mask_next
#undef pixels_g
#undef blend_pixels
#undef fb_pixels_next

#define msb_mask                                 q0
#define draw_mask                                q1
#define pixels                                   q2
#define fb_pixels                                q3
#define blend_pixels                             q4
#define pixels_no_msb                            q5
#define blend_mask                               q6
#define fb_pixels_no_msb                         q7
#define d128_0x8000                              q8
#define d128_0x0421                              q9
#define fb_pixels_next                           q10
#define blend_pixels_next                        q11
#define pixels_next                              q12
#define draw_mask_next                           q13
#define write_mask                               q14

#define pixels_rb                                q5
#define pixels_mg                                q7
#define pixels_g                                 q7
#define d128_0x7C1F                              q8
#define d128_0x03E0                              q9
#define fb_pixels_rb                             q10
#define fb_pixels_g                              q11
#define fb_pixels_masked                         q11
#define d128_0x83E0                              q15
#define pixels_fourth                            q7
#define d128_0x1C07                              q12
#define d128_0x00E0                              q13
#define d128_0x80E0                              q13

#define msb_mask_low                             d0
#define msb_mask_high                            d1

#define blend_blocks_average_set_blend_mask_textured(source)                   \
  vclt.s16 blend_mask, source, #0                                              \

#define blend_blocks_average_set_stp_bit_textured()                            \
  vorr.u16 blend_pixels, #0x8000                                               \

#define blend_blocks_average_combine_textured(source)                          \
  vbif.u16 blend_pixels, source, blend_mask                                    \
  
#define blend_blocks_average_set_blend_mask_untextured(source)                 \

#define blend_blocks_average_set_stp_bit_untextured()                          \

#define blend_blocks_average_combine_untextured(source)                        \

#define blend_blocks_average_mask_set_on()                                     \
  vclt.s16 write_mask, fb_pixels_next, #0                                      \

#define blend_blocks_average_mask_copy_on()                                    \
  vorr.u16 draw_mask, draw_mask_next, write_mask                               \

#define blend_blocks_average_mask_copy_b_on()                                  \
  vorr.u16 draw_mask_next, draw_mask_next, write_mask                          \

#define blend_blocks_average_mask_set_off()                                    \

#define blend_blocks_average_mask_copy_off()                                   \
  vmov draw_mask, draw_mask_next                                               \

#define blend_blocks_average_mask_copy_b_off()                                 \

#define blend_blocks_average_builder(texturing, mask_evaluate)                 \
.align 3;                                                                      \
                                                                               \
function(blend_blocks_##texturing##_average_##mask_evaluate)                   \
  stmdb sp!, { r4, r14 };                                                      \
  add mask_msb_ptr, psx_gpu, #psx_gpu_mask_msb_offset;                         \
  ldrh num_blocks, [psx_gpu, #psx_gpu_num_blocks_offset];                      \
                                                                               \
  add pixel_ptr, psx_gpu, #(psx_gpu_blocks_offset + 16);                       \
  vld1.u16 { msb_mask_low[], msb_mask_high[] }, [mask_msb_ptr, :16];           \
                                                                               \
  add draw_mask_ptr, psx_gpu, #psx_gpu_blocks_offset;                          \
  mov c_64, #64;                                                               \
                                                                               \
  vmov.u16 d128_0x8000, #0x8000;                                               \
  vld1.u32 { draw_mask_next }, [draw_mask_ptr, :128], c_64;                    \
  ldr fb_ptr_next, [pixel_ptr, #28];                                           \
                                                                               \
  vmov.u16 d128_0x0421, #0x0400;                                               \
  vld1.u32 { pixels_next }, [pixel_ptr, :128], c_64;                           \
                                                                               \
  vorr.u16 d128_0x0421, #0x0021;                                               \
  vld1.u16 { fb_pixels_next }, [fb_ptr_next];                                  \
                                                                               \
  veor.u16 blend_pixels_next, pixels_next, fb_pixels_next;                     \
  vbic.u16 pixels_no_msb, pixels_next, d128_0x8000;                            \
  vand.u16 blend_pixels_next, blend_pixels_next, d128_0x0421;                  \
  vsub.u16 blend_pixels_next, pixels_no_msb, blend_pixels_next;                \
  blend_blocks_average_mask_set_##mask_evaluate();                             \
  vbic.u16 fb_pixels_no_msb, fb_pixels_next, d128_0x8000;                      \
                                                                               \
  subs num_blocks, num_blocks, #1;                                             \
  beq 1f;                                                                      \
                                                                               \
 0:                                                                            \
  mov fb_ptr, fb_ptr_next;                                                     \
  ldr fb_ptr_next, [pixel_ptr, #28];                                           \
                                                                               \
  vmov pixels, pixels_next;                                                    \
  vld1.u32 { pixels_next }, [pixel_ptr, :128], c_64;                           \
                                                                               \
  vhadd.u16 blend_pixels, fb_pixels_no_msb, blend_pixels_next;                 \
                                                                               \
  blend_blocks_average_mask_copy_##mask_evaluate();                            \
  vld1.u32 { draw_mask_next }, [draw_mask_ptr, :128], c_64;                    \
                                                                               \
  blend_blocks_average_set_blend_mask_##texturing(pixels);                     \
  blend_blocks_average_set_stp_bit_##texturing();                              \
  vmov fb_pixels, fb_pixels_next;                                              \
  blend_blocks_average_combine_##texturing(pixels);                            \
                                                                               \
  sub fb_ptr_cmp, fb_ptr_next, fb_ptr;                                         \
  add fb_ptr_cmp, fb_ptr_cmp, #14;                                             \
  cmp fb_ptr_cmp, #28;                                                         \
  bls 2f;                                                                      \
                                                                               \
  vld1.u16 { fb_pixels_next }, [fb_ptr_next];                                  \
  veor.u16 blend_pixels_next, pixels_next, fb_pixels_next;                     \
                                                                               \
  vorr.u16 blend_pixels, blend_pixels, msb_mask;                               \
  vbic.u16 pixels_no_msb, pixels_next, d128_0x8000;                            \
                                                                               \
  vand.u16 blend_pixels_next, blend_pixels_next, d128_0x0421;                  \
  vbif.u16 fb_pixels, blend_pixels, draw_mask;                                 \
                                                                               \
  vbic.u16 fb_pixels_no_msb, fb_pixels_next, d128_0x8000;                      \
  vsub.u16 blend_pixels_next, pixels_no_msb, blend_pixels_next;                \
  blend_blocks_average_mask_set_##mask_evaluate();                             \
  vst1.u16 { fb_pixels }, [fb_ptr];                                            \
                                                                               \
 3:                                                                            \
  subs num_blocks, num_blocks, #1;                                             \
  bne 0b;                                                                      \
                                                                               \
 1:                                                                            \
  blend_blocks_average_mask_copy_b_##mask_evaluate();                          \
  vhadd.u16 blend_pixels, fb_pixels_no_msb, blend_pixels_next;                 \
                                                                               \
  blend_blocks_average_set_blend_mask_##texturing(pixels_next);                \
  blend_blocks_average_set_stp_bit_##texturing();                              \
  blend_blocks_average_combine_##texturing(pixels_next);                       \
                                                                               \
  vorr.u16 blend_pixels, blend_pixels, msb_mask;                               \
  vbif.u16 fb_pixels_next, blend_pixels, draw_mask_next;                       \
  vst1.u16 { fb_pixels_next }, [fb_ptr_next];                                  \
                                                                               \
  ldmia sp!, { r4, pc };                                                       \
                                                                               \
 2:                                                                            \
  vorr.u16 blend_pixels, blend_pixels, msb_mask;                               \
  vbif.u16 fb_pixels, blend_pixels, draw_mask;                                 \
  vst1.u16 { fb_pixels }, [fb_ptr];                                            \
                                                                               \
  vld1.u16 { fb_pixels_next }, [fb_ptr_next];                                  \
  veor.u16 blend_pixels_next, pixels_next, fb_pixels_next;                     \
  vbic.u16 pixels_no_msb, pixels_next, d128_0x8000;                            \
  vand.u16 blend_pixels_next, blend_pixels_next, d128_0x0421;                  \
  vsub.u16 blend_pixels_next, pixels_no_msb, blend_pixels_next;                \
  vbic.u16 fb_pixels_no_msb, fb_pixels_next, d128_0x8000;                      \
                                                                               \
  bal 3b                                                                       \

blend_blocks_average_builder(textured, off)
blend_blocks_average_builder(untextured, off)
blend_blocks_average_builder(textured, on)
blend_blocks_average_builder(untextured, on)


#define blend_blocks_add_mask_set_on()                                         \
  vclt.s16 write_mask, fb_pixels, #0                                           \

#define blend_blocks_add_mask_copy_on()                                        \
  vorr.u16 draw_mask, draw_mask, write_mask                                    \

#define blend_blocks_add_mask_set_off()                                        \

#define blend_blocks_add_mask_copy_off()                                       \


#define blend_blocks_add_textured_builder(mask_evaluate)                       \
.align 3;                                                                      \
                                                                               \
function(blend_blocks_textured_add_##mask_evaluate)                            \
  stmdb sp!, { r4, r14 };                                                      \
  add mask_msb_ptr, psx_gpu, #psx_gpu_mask_msb_offset;                         \
  ldrh num_blocks, [psx_gpu, #psx_gpu_num_blocks_offset];                      \
                                                                               \
  add pixel_ptr, psx_gpu, #(psx_gpu_blocks_offset + 16);                       \
  vld1.u16 { msb_mask_low[], msb_mask_high[] }, [mask_msb_ptr, :16];           \
                                                                               \
  add draw_mask_ptr, psx_gpu, #psx_gpu_blocks_offset;                          \
  mov c_64, #64;                                                               \
                                                                               \
  vmov.u16 d128_0x7C1F, #0x7C00;                                               \
  vmov.u16 d128_0x03E0, #0x0300;                                               \
  vmov.u16 d128_0x83E0, #0x8000;                                               \
  vorr.u16 d128_0x03E0, #0x00E0;                                               \
  vorr.u16 d128_0x7C1F, #0x001F;                                               \
  vorr.u16 d128_0x83E0, d128_0x83E0, d128_0x03E0;                              \
                                                                               \
  vld1.u32 { draw_mask }, [draw_mask_ptr, :128], c_64;                         \
  ldr fb_ptr_next, [pixel_ptr, #28];                                           \
  vld1.u32 { pixels }, [pixel_ptr, :128], c_64;                                \
  vclt.s16 blend_mask, pixels, #0;                                             \
  vld1.u16 { fb_pixels }, [fb_ptr_next];                                       \
  blend_blocks_add_mask_set_##mask_evaluate();                                 \
  vand.u16 pixels_rb, pixels, d128_0x7C1F;                                     \
                                                                               \
  blend_blocks_add_mask_copy_##mask_evaluate();                                \
  vorr.u16 pixels, pixels, msb_mask;                                           \
  vand.u16 fb_pixels_masked, fb_pixels, blend_mask;                            \
  vand.u16 pixels_mg, pixels, d128_0x83E0;                                     \
  vand.u16 fb_pixels_rb, fb_pixels_masked, d128_0x7C1F;                        \
  vand.u16 fb_pixels_g, fb_pixels_masked, d128_0x03E0;                         \
  vadd.u16 fb_pixels_rb, fb_pixels_rb, pixels_rb;                              \
  vadd.u16 fb_pixels_g, fb_pixels_g, pixels_mg;                                \
  vmin.u8 fb_pixels_rb, fb_pixels_rb, d128_0x7C1F;                             \
  vmin.u16 fb_pixels_g, fb_pixels_g, d128_0x83E0;                              \
                                                                               \
  subs num_blocks, num_blocks, #1;                                             \
  beq 1f;                                                                      \
                                                                               \
 0:                                                                            \
  mov fb_ptr, fb_ptr_next;                                                     \
                                                                               \
  ldr fb_ptr_next, [pixel_ptr, #28];                                           \
                                                                               \
  vld1.u32 { pixels }, [pixel_ptr, :128], c_64;                                \
  vclt.s16 blend_mask, pixels, #0;                                             \
                                                                               \
  vorr.u16 pixels, pixels, msb_mask;                                           \
  vorr.u16 blend_pixels, fb_pixels_rb, fb_pixels_g;                            \
  vand.u16 pixels_mg, pixels, d128_0x83E0;                                     \
                                                                               \
  sub fb_ptr_cmp, fb_ptr_next, fb_ptr;                                         \
  pld [fb_ptr_next, #64];                                                      \
                                                                               \
  sub fb_ptr_cmp, fb_ptr_next, fb_ptr;                                         \
  vbit.u16 blend_pixels, fb_pixels, draw_mask;                                 \
                                                                               \
  add fb_ptr_cmp, fb_ptr_cmp, #14;                                             \
  vld1.u32 { draw_mask }, [draw_mask_ptr, :128], c_64;                         \
                                                                               \
  cmp fb_ptr_cmp, #28;                                                         \
  bls 2f;                                                                      \
                                                                               \
  vld1.u16 { fb_pixels }, [fb_ptr_next];                                       \
  blend_blocks_add_mask_set_##mask_evaluate();                                 \
  vand.u16 fb_pixels_masked, fb_pixels, blend_mask;                            \
  blend_blocks_add_mask_copy_##mask_evaluate();                                \
  vand.u16 pixels_rb, pixels, d128_0x7C1F;                                     \
  vand.u16 fb_pixels_rb, fb_pixels_masked, d128_0x7C1F;                        \
  vst1.u16 { blend_pixels }, [fb_ptr];                                         \
                                                                               \
 3:                                                                            \
  vand.u16 fb_pixels_g, fb_pixels_masked, d128_0x03E0;                         \
  vadd.u16 fb_pixels_rb, fb_pixels_rb, pixels_rb;                              \
  vadd.u16 fb_pixels_g, fb_pixels_g, pixels_mg;                                \
  vmin.u8 fb_pixels_rb, fb_pixels_rb, d128_0x7C1F;                             \
  vmin.u16 fb_pixels_g, fb_pixels_g, d128_0x83E0;                              \
                                                                               \
  subs num_blocks, num_blocks, #1;                                             \
  bne 0b;                                                                      \
                                                                               \
 1:                                                                            \
  vorr.u16 blend_pixels, fb_pixels_rb, fb_pixels_g;                            \
  vbit.u16 blend_pixels, fb_pixels, draw_mask;                                 \
  vst1.u16 { blend_pixels }, [fb_ptr_next];                                    \
                                                                               \
  ldmia sp!, { r4, pc };                                                       \
                                                                               \
 2:                                                                            \
  vst1.u16 { blend_pixels }, [fb_ptr];                                         \
  vand.u16 pixels_rb, pixels, d128_0x7C1F;                                     \
                                                                               \
  vld1.u16 { fb_pixels }, [fb_ptr_next];                                       \
  blend_blocks_add_mask_set_##mask_evaluate();                                 \
  vand.u16 fb_pixels_masked, fb_pixels, blend_mask;                            \
  blend_blocks_add_mask_copy_##mask_evaluate();                                \
  vand.u16 fb_pixels_rb, fb_pixels_masked, d128_0x7C1F;                        \
  bal 3b                                                                       \


#define blend_blocks_add_untextured_builder(mask_evaluate)                     \
.align 3;                                                                      \
                                                                               \
function(blend_blocks_untextured_add_##mask_evaluate)                          \
  stmdb sp!, { r4, r14 };                                                      \
  add mask_msb_ptr, psx_gpu, #psx_gpu_mask_msb_offset;                         \
  ldrh num_blocks, [psx_gpu, #psx_gpu_num_blocks_offset];                      \
                                                                               \
  add pixel_ptr, psx_gpu, #(psx_gpu_blocks_offset + 16);                       \
  vld1.u16 { msb_mask_low[], msb_mask_high[] }, [mask_msb_ptr, :16];           \
                                                                               \
  add draw_mask_ptr, psx_gpu, #psx_gpu_blocks_offset;                          \
  mov c_64, #64;                                                               \
                                                                               \
  vmov.u16 d128_0x7C1F, #0x7C00;                                               \
  vmov.u16 d128_0x03E0, #0x0300;                                               \
  vorr.u16 d128_0x7C1F, #0x001F;                                               \
  vorr.u16 d128_0x03E0, #0x00E0;                                               \
                                                                               \
  vld1.u32 { draw_mask }, [draw_mask_ptr, :128], c_64;                         \
  ldr fb_ptr_next, [pixel_ptr, #28];                                           \
  vld1.u32 { pixels }, [pixel_ptr, :128], c_64;                                \
  vld1.u16 { fb_pixels }, [fb_ptr_next];                                       \
  blend_blocks_add_mask_set_##mask_evaluate();                                 \
  vand.u16 pixels_rb, pixels, d128_0x7C1F;                                     \
                                                                               \
  blend_blocks_add_mask_copy_##mask_evaluate();                                \
  vand.u16 pixels_g, pixels, d128_0x03E0;                                      \
  vand.u16 fb_pixels_rb, fb_pixels, d128_0x7C1F;                               \
  vand.u16 fb_pixels_g, fb_pixels, d128_0x03E0;                                \
  vadd.u16 fb_pixels_rb, fb_pixels_rb, pixels_rb;                              \
  vadd.u16 fb_pixels_g, fb_pixels_g, pixels_g;                                 \
  vmin.u8 fb_pixels_rb, fb_pixels_rb, d128_0x7C1F;                             \
  vmin.u16 fb_pixels_g, fb_pixels_g, d128_0x03E0;                              \
                                                                               \
  subs num_blocks, num_blocks, #1;                                             \
  beq 1f;                                                                      \
                                                                               \
 0:                                                                            \
  mov fb_ptr, fb_ptr_next;                                                     \
                                                                               \
  ldr fb_ptr_next, [pixel_ptr, #28];                                           \
                                                                               \
  vld1.u32 { pixels }, [pixel_ptr, :128], c_64;                                \
                                                                               \
  vorr.u16 blend_pixels, fb_pixels_rb, fb_pixels_g;                            \
  vorr.u16 blend_pixels, blend_pixels, msb_mask;                               \
  vand.u16 pixels_g, pixels, d128_0x03E0;                                      \
                                                                               \
  vbit.u16 blend_pixels, fb_pixels, draw_mask;                                 \
  vld1.u32 { draw_mask }, [draw_mask_ptr, :128], c_64;                         \
                                                                               \
  sub fb_ptr_cmp, fb_ptr_next, fb_ptr;                                         \
  add fb_ptr_cmp, fb_ptr_cmp, #14;                                             \
  cmp fb_ptr_cmp, #28;                                                         \
  bls 2f;                                                                      \
                                                                               \
  vld1.u16 { fb_pixels }, [fb_ptr_next];                                       \
  blend_blocks_add_mask_set_##mask_evaluate();                                 \
  blend_blocks_add_mask_copy_##mask_evaluate();                                \
  vand.u16 pixels_rb, pixels, d128_0x7C1F;                                     \
  vand.u16 fb_pixels_rb, fb_pixels, d128_0x7C1F;                               \
  vst1.u16 { blend_pixels }, [fb_ptr];                                         \
                                                                               \
 3:                                                                            \
  vand.u16 fb_pixels_g, fb_pixels, d128_0x03E0;                                \
  vadd.u16 fb_pixels_rb, fb_pixels_rb, pixels_rb;                              \
  vadd.u16 fb_pixels_g, fb_pixels_g, pixels_g;                                 \
  vmin.u8 fb_pixels_rb, fb_pixels_rb, d128_0x7C1F;                             \
  vmin.u16 fb_pixels_g, fb_pixels_g, d128_0x03E0;                              \
                                                                               \
  subs num_blocks, num_blocks, #1;                                             \
  bne 0b;                                                                      \
                                                                               \
 1:                                                                            \
  vorr.u16 blend_pixels, fb_pixels_rb, fb_pixels_g;                            \
  vorr.u16 blend_pixels, blend_pixels, msb_mask;                               \
  vbit.u16 blend_pixels, fb_pixels, draw_mask;                                 \
  vst1.u16 { blend_pixels }, [fb_ptr_next];                                    \
                                                                               \
  ldmia sp!, { r4, pc };                                                       \
                                                                               \
 2:                                                                            \
  vst1.u16 { blend_pixels }, [fb_ptr];                                         \
  vand.u16 pixels_rb, pixels, d128_0x7C1F;                                     \
                                                                               \
  vld1.u16 { fb_pixels }, [fb_ptr_next];                                       \
  blend_blocks_add_mask_set_##mask_evaluate();                                 \
  blend_blocks_add_mask_copy_##mask_evaluate();                                \
  vand.u16 fb_pixels_rb, fb_pixels, d128_0x7C1F;                               \
  bal 3b                                                                       \


blend_blocks_add_textured_builder(off)
blend_blocks_add_textured_builder(on)
blend_blocks_add_untextured_builder(off)
blend_blocks_add_untextured_builder(on)

#define blend_blocks_subtract_set_blend_mask_textured()                        \
  vclt.s16 blend_mask, pixels_next, #0                                         \

#define blend_blocks_subtract_combine_textured()                               \
  vbif.u16 blend_pixels, pixels, blend_mask                                    \

#define blend_blocks_subtract_set_stb_textured()                               \
  vorr.u16 blend_pixels, #0x8000                                               \

#define blend_blocks_subtract_msb_mask_textured()                              \
  vorr.u16 pixels, pixels_next, msb_mask                                       \

#define blend_blocks_subtract_set_blend_mask_untextured()                      \

#define blend_blocks_subtract_combine_untextured()                             \

#define blend_blocks_subtract_set_stb_untextured()                             \
  vorr.u16 blend_pixels, blend_pixels, msb_mask                                \

#define blend_blocks_subtract_msb_mask_untextured()                            \


#define blend_blocks_subtract_mask_set_on()                                    \
  vclt.s16 write_mask, fb_pixels, #0                                           \

#define blend_blocks_subtract_mask_copy_on()                                   \
  vorr.u16 draw_mask, draw_mask_next, write_mask                               \

#define blend_blocks_subtract_mask_set_off()                                   \

#define blend_blocks_subtract_mask_copy_off()                                  \
  vmov draw_mask, draw_mask_next                                               \


#define blend_blocks_subtract_builder(texturing, mask_evaluate)                \
.align 3;                                                                      \
                                                                               \
function(blend_blocks_##texturing##_subtract_##mask_evaluate)                  \
  stmdb sp!, { r4, r14 };                                                      \
  add mask_msb_ptr, psx_gpu, #psx_gpu_mask_msb_offset;                         \
  ldrh num_blocks, [psx_gpu, #psx_gpu_num_blocks_offset];                      \
                                                                               \
  add pixel_ptr, psx_gpu, #(psx_gpu_blocks_offset + 16);                       \
  vld1.u16 { msb_mask_low[], msb_mask_high[] }, [mask_msb_ptr, :16];           \
                                                                               \
  add draw_mask_ptr, psx_gpu, #psx_gpu_blocks_offset;                          \
  mov c_64, #64;                                                               \
                                                                               \
  vmov.u16 d128_0x7C1F, #0x7C00;                                               \
  vmov.u16 d128_0x03E0, #0x0300;                                               \
  vorr.u16 d128_0x7C1F, #0x001F;                                               \
  vorr.u16 d128_0x03E0, #0x00E0;                                               \
                                                                               \
  vld1.u32 { draw_mask_next }, [draw_mask_ptr, :128], c_64;                    \
  ldr fb_ptr_next, [pixel_ptr, #28];                                           \
  vld1.u32 { pixels_next }, [pixel_ptr, :128], c_64;                           \
  blend_blocks_subtract_set_blend_mask_##texturing();                          \
  vld1.u16 { fb_pixels }, [fb_ptr_next];                                       \
  blend_blocks_subtract_mask_set_##mask_evaluate();                            \
  vand.u16 pixels_rb, pixels_next, d128_0x7C1F;                                \
                                                                               \
  vand.u16 pixels_g, pixels_next, d128_0x03E0;                                 \
  vand.u16 fb_pixels_rb, fb_pixels, d128_0x7C1F;                               \
  vand.u16 fb_pixels_g, fb_pixels, d128_0x03E0;                                \
  vqsub.u8 fb_pixels_rb, fb_pixels_rb, pixels_rb;                              \
  vqsub.u16 fb_pixels_g, fb_pixels_g, pixels_g;                                \
                                                                               \
  subs num_blocks, num_blocks, #1;                                             \
  beq 1f;                                                                      \
                                                                               \
 0:                                                                            \
  blend_blocks_subtract_mask_copy_##mask_evaluate();                           \
  mov fb_ptr, fb_ptr_next;                                                     \
  ldr fb_ptr_next, [pixel_ptr, #28];                                           \
                                                                               \
  vld1.u32 { draw_mask_next }, [draw_mask_ptr, :128], c_64;                    \
  blend_blocks_subtract_msb_mask_##texturing();                                \
                                                                               \
  vld1.u32 { pixels_next }, [pixel_ptr, :128], c_64;                           \
  vorr.u16 blend_pixels, fb_pixels_rb, fb_pixels_g;                            \
  vand.u16 pixels_rb, pixels_next, d128_0x7C1F;                                \
  blend_blocks_subtract_set_stb_##texturing();                                 \
  vand.u16 pixels_g, pixels_next, d128_0x03E0;                                 \
  blend_blocks_subtract_combine_##texturing();                                 \
  blend_blocks_subtract_set_blend_mask_##texturing();                          \
  vbit.u16 blend_pixels, fb_pixels, draw_mask;                                 \
                                                                               \
  sub fb_ptr_cmp, fb_ptr_next, fb_ptr;                                         \
  add fb_ptr_cmp, fb_ptr_cmp, #14;                                             \
  cmp fb_ptr_cmp, #28;                                                         \
  bls 2f;                                                                      \
                                                                               \
  vld1.u16 { fb_pixels }, [fb_ptr_next];                                       \
  blend_blocks_subtract_mask_set_##mask_evaluate();                            \
  vand.u16 fb_pixels_rb, fb_pixels, d128_0x7C1F;                               \
  vand.u16 fb_pixels_g, fb_pixels, d128_0x03E0;                                \
  vqsub.u8 fb_pixels_rb, fb_pixels_rb, pixels_rb;                              \
  vst1.u16 { blend_pixels }, [fb_ptr];                                         \
  vqsub.u16 fb_pixels_g, fb_pixels_g, pixels_g;                                \
                                                                               \
 3:                                                                            \
  subs num_blocks, num_blocks, #1;                                             \
  bne 0b;                                                                      \
                                                                               \
 1:                                                                            \
  blend_blocks_subtract_mask_copy_##mask_evaluate();                           \
                                                                               \
  blend_blocks_subtract_msb_mask_##texturing();                                \
  vorr.u16 blend_pixels, fb_pixels_rb, fb_pixels_g;                            \
  blend_blocks_subtract_set_stb_##texturing();                                 \
  blend_blocks_subtract_combine_##texturing();                                 \
  vbit.u16 blend_pixels, fb_pixels, draw_mask;                                 \
  vst1.u16 { blend_pixels }, [fb_ptr_next];                                    \
                                                                               \
  ldmia sp!, { r4, pc };                                                       \
                                                                               \
 2:                                                                            \
  vst1.u16 { blend_pixels }, [fb_ptr];                                         \
  vld1.u16 { fb_pixels }, [fb_ptr_next];                                       \
  blend_blocks_subtract_mask_set_##mask_evaluate();                            \
  vand.u16 fb_pixels_rb, fb_pixels, d128_0x7C1F;                               \
  vand.u16 fb_pixels_g, fb_pixels, d128_0x03E0;                                \
  vqsub.u8 fb_pixels_rb, fb_pixels_rb, pixels_rb;                              \
  vqsub.u16 fb_pixels_g, fb_pixels_g, pixels_g;                                \
  bal 3b                                                                       \


blend_blocks_subtract_builder(textured, off)
blend_blocks_subtract_builder(textured, on)
blend_blocks_subtract_builder(untextured, off)
blend_blocks_subtract_builder(untextured, on)


#define blend_blocks_add_fourth_textured_builder(mask_evaluate)                \
.align 3;                                                                      \
                                                                               \
function(blend_blocks_textured_add_fourth_##mask_evaluate)                     \
  stmdb sp!, { r4, r14 };                                                      \
  add mask_msb_ptr, psx_gpu, #psx_gpu_mask_msb_offset;                         \
  ldrh num_blocks, [psx_gpu, #psx_gpu_num_blocks_offset];                      \
                                                                               \
  add pixel_ptr, psx_gpu, #(psx_gpu_blocks_offset + 16);                       \
  vld1.u16 { msb_mask_low[], msb_mask_high[] }, [mask_msb_ptr, :16];           \
                                                                               \
  add draw_mask_ptr, psx_gpu, #psx_gpu_blocks_offset;                          \
  mov c_64, #64;                                                               \
                                                                               \
  vmov.u16 d128_0x7C1F, #0x7C00;                                               \
  vmov.u16 d128_0x03E0, #0x0300;                                               \
  vmov.u16 d128_0x1C07, #0x1C00;                                               \
  vmov.u16 d128_0x00E0, #0x00E0;                                               \
  vorr.u16 d128_0x7C1F, #0x001F;                                               \
  vorr.u16 d128_0x03E0, #0x00E0;                                               \
  vorr.u16 d128_0x1C07, #0x0007;                                               \
                                                                               \
  vld1.u32 { draw_mask }, [draw_mask_ptr, :128], c_64;                         \
  ldr fb_ptr_next, [pixel_ptr, #28];                                           \
  vld1.u32 { pixels }, [pixel_ptr, :128], c_64;                                \
  vclt.s16 blend_mask, pixels, #0;                                             \
  vld1.u16 { fb_pixels }, [fb_ptr_next];                                       \
  blend_blocks_add_mask_set_##mask_evaluate();                                 \
  vshr.s16 pixels_fourth, pixels, #2;                                          \
  vand.u16 pixels_rb, pixels_fourth, d128_0x1C07;                              \
                                                                               \
  blend_blocks_add_mask_copy_##mask_evaluate();                                \
  vand.u16 pixels_g, pixels_fourth, d128_0x00E0;                               \
  vand.u16 fb_pixels_rb, fb_pixels, d128_0x7C1F;                               \
  vand.u16 fb_pixels_g, fb_pixels, d128_0x03E0;                                \
  vadd.u16 fb_pixels_rb, fb_pixels_rb, pixels_rb;                              \
  vadd.u16 fb_pixels_g, fb_pixels_g, pixels_g;                                 \
  vmin.u8 fb_pixels_rb, fb_pixels_rb, d128_0x7C1F;                             \
  vmin.u16 fb_pixels_g, fb_pixels_g, d128_0x03E0;                              \
                                                                               \
  subs num_blocks, num_blocks, #1;                                             \
  beq 1f;                                                                      \
                                                                               \
 0:                                                                            \
  mov fb_ptr, fb_ptr_next;                                                     \
  ldr fb_ptr_next, [pixel_ptr, #28];                                           \
                                                                               \
  vorr.u16 blend_pixels, fb_pixels_rb, fb_pixels_g;                            \
  vbif.u16 blend_pixels, pixels, blend_mask;                                   \
                                                                               \
  vld1.u32 { pixels }, [pixel_ptr, :128], c_64;                                \
  vclt.s16 blend_mask, pixels, #0;                                             \
  vshr.s16 pixels_fourth, pixels, #2;                                          \
  vorr.u16 blend_pixels, blend_pixels, msb_mask;                               \
  vand.u16 pixels_rb, pixels_fourth, d128_0x1C07;                              \
                                                                               \
  vbit.u16 blend_pixels, fb_pixels, draw_mask;                                 \
  vld1.u32 { draw_mask }, [draw_mask_ptr, :128], c_64;                         \
                                                                               \
  sub fb_ptr_cmp, fb_ptr_next, fb_ptr;                                         \
  add fb_ptr_cmp, fb_ptr_cmp, #14;                                             \
  cmp fb_ptr_cmp, #28;                                                         \
  bls 2f;                                                                      \
                                                                               \
  vld1.u16 { fb_pixels }, [fb_ptr_next];                                       \
  blend_blocks_add_mask_set_##mask_evaluate();                                 \
  blend_blocks_add_mask_copy_##mask_evaluate();                                \
  vand.u16 pixels_g, pixels_fourth, d128_0x00E0;                               \
  vand.u16 fb_pixels_rb, fb_pixels, d128_0x7C1F;                               \
  vst1.u16 { blend_pixels }, [fb_ptr];                                         \
                                                                               \
 3:                                                                            \
  vand.u16 fb_pixels_g, fb_pixels, d128_0x03E0;                                \
  vadd.u16 fb_pixels_rb, fb_pixels_rb, pixels_rb;                              \
  vadd.u16 fb_pixels_g, fb_pixels_g, pixels_g;                                 \
  vmin.u8 fb_pixels_rb, fb_pixels_rb, d128_0x7C1F;                             \
  vmin.u16 fb_pixels_g, fb_pixels_g, d128_0x03E0;                              \
                                                                               \
  subs num_blocks, num_blocks, #1;                                             \
  bne 0b;                                                                      \
                                                                               \
 1:                                                                            \
  vorr.u16 blend_pixels, fb_pixels_rb, fb_pixels_g;                            \
  vorr.u16 blend_pixels, blend_pixels, msb_mask;                               \
  vbif.u16 blend_pixels, pixels, blend_mask;                                   \
  vbit.u16 blend_pixels, fb_pixels, draw_mask;                                 \
  vst1.u16 { blend_pixels }, [fb_ptr_next];                                    \
                                                                               \
  ldmia sp!, { r4, pc };                                                       \
                                                                               \
 2:                                                                            \
  vst1.u16 { blend_pixels }, [fb_ptr];                                         \
  vand.u16 pixels_g, pixels_fourth, d128_0x00E0;                               \
                                                                               \
  vld1.u16 { fb_pixels }, [fb_ptr_next];                                       \
  blend_blocks_add_mask_set_##mask_evaluate();                                 \
  blend_blocks_add_mask_copy_##mask_evaluate();                                \
  vand.u16 fb_pixels_rb, fb_pixels, d128_0x7C1F;                               \
  bal 3b                                                                       \



#define blend_blocks_add_fourth_untextured_builder(mask_evaluate)              \
.align 3;                                                                      \
                                                                               \
function(blend_blocks_untextured_add_fourth_##mask_evaluate)                   \
  stmdb sp!, { r4, r14 };                                                      \
  add mask_msb_ptr, psx_gpu, #psx_gpu_mask_msb_offset;                         \
  ldrh num_blocks, [psx_gpu, #psx_gpu_num_blocks_offset];                      \
                                                                               \
  add pixel_ptr, psx_gpu, #(psx_gpu_blocks_offset + 16);                       \
  vld1.u16 { msb_mask_low[], msb_mask_high[] }, [mask_msb_ptr, :16];           \
                                                                               \
  add draw_mask_ptr, psx_gpu, #psx_gpu_blocks_offset;                          \
  mov c_64, #64;                                                               \
                                                                               \
  vmov.u16 d128_0x7C1F, #0x7C00;                                               \
  vmov.u16 d128_0x03E0, #0x0300;                                               \
  vmov.u16 d128_0x1C07, #0x1C00;                                               \
  vmov.u16 d128_0x00E0, #0x00E0;                                               \
  vorr.u16 d128_0x7C1F, #0x001F;                                               \
  vorr.u16 d128_0x03E0, #0x00E0;                                               \
  vorr.u16 d128_0x1C07, #0x0007;                                               \
                                                                               \
  vld1.u32 { draw_mask }, [draw_mask_ptr, :128], c_64;                         \
  ldr fb_ptr_next, [pixel_ptr, #28];                                           \
  vld1.u32 { pixels }, [pixel_ptr, :128], c_64;                                \
  vld1.u16 { fb_pixels }, [fb_ptr_next];                                       \
  blend_blocks_add_mask_set_##mask_evaluate();                                 \
  vshr.s16 pixels_fourth, pixels, #2;                                          \
  vand.u16 pixels_rb, pixels_fourth, d128_0x1C07;                              \
                                                                               \
  blend_blocks_add_mask_copy_##mask_evaluate();                                \
  vand.u16 pixels_g, pixels_fourth, d128_0x00E0;                               \
  vand.u16 fb_pixels_rb, fb_pixels, d128_0x7C1F;                               \
  vand.u16 fb_pixels_g, fb_pixels, d128_0x03E0;                                \
  vadd.u16 fb_pixels_rb, fb_pixels_rb, pixels_rb;                              \
  vadd.u16 fb_pixels_g, fb_pixels_g, pixels_g;                                 \
  vmin.u8 fb_pixels_rb, fb_pixels_rb, d128_0x7C1F;                             \
  vmin.u16 fb_pixels_g, fb_pixels_g, d128_0x03E0;                              \
                                                                               \
  subs num_blocks, num_blocks, #1;                                             \
  beq 1f;                                                                      \
                                                                               \
 0:                                                                            \
  mov fb_ptr, fb_ptr_next;                                                     \
  ldr fb_ptr_next, [pixel_ptr, #28];                                           \
                                                                               \
  vld1.u32 { pixels }, [pixel_ptr, :128], c_64;                                \
                                                                               \
  vorr.u16 blend_pixels, fb_pixels_rb, fb_pixels_g;                            \
  vshr.s16 pixels_fourth, pixels, #2;                                          \
  vorr.u16 blend_pixels, blend_pixels, msb_mask;                               \
  vand.u16 pixels_rb, pixels_fourth, d128_0x1C07;                              \
                                                                               \
  vbit.u16 blend_pixels, fb_pixels, draw_mask;                                 \
  vld1.u32 { draw_mask }, [draw_mask_ptr, :128], c_64;                         \
                                                                               \
  sub fb_ptr_cmp, fb_ptr_next, fb_ptr;                                         \
  add fb_ptr_cmp, fb_ptr_cmp, #14;                                             \
  cmp fb_ptr_cmp, #28;                                                         \
  bls 2f;                                                                      \
                                                                               \
  vld1.u16 { fb_pixels }, [fb_ptr_next];                                       \
  blend_blocks_add_mask_set_##mask_evaluate();                                 \
  blend_blocks_add_mask_copy_##mask_evaluate();                                \
  vand.u16 pixels_g, pixels_fourth, d128_0x00E0;                               \
  vand.u16 fb_pixels_rb, fb_pixels, d128_0x7C1F;                               \
  vst1.u16 { blend_pixels }, [fb_ptr];                                         \
                                                                               \
 3:                                                                            \
  vand.u16 fb_pixels_g, fb_pixels, d128_0x03E0;                                \
  vadd.u16 fb_pixels_rb, fb_pixels_rb, pixels_rb;                              \
  vadd.u16 fb_pixels_g, fb_pixels_g, pixels_g;                                 \
  vmin.u8 fb_pixels_rb, fb_pixels_rb, d128_0x7C1F;                             \
  vmin.u16 fb_pixels_g, fb_pixels_g, d128_0x03E0;                              \
                                                                               \
  subs num_blocks, num_blocks, #1;                                             \
  bne 0b;                                                                      \
                                                                               \
 1:                                                                            \
  vorr.u16 blend_pixels, fb_pixels_rb, fb_pixels_g;                            \
  vorr.u16 blend_pixels, blend_pixels, msb_mask;                               \
  vbit.u16 blend_pixels, fb_pixels, draw_mask;                                 \
  vst1.u16 { blend_pixels }, [fb_ptr_next];                                    \
                                                                               \
  ldmia sp!, { r4, pc };                                                       \
                                                                               \
 2:                                                                            \
  vst1.u16 { blend_pixels }, [fb_ptr];                                         \
  vand.u16 pixels_g, pixels_fourth, d128_0x00E0;                               \
                                                                               \
  vld1.u16 { fb_pixels }, [fb_ptr_next];                                       \
  blend_blocks_add_mask_set_##mask_evaluate();                                 \
  blend_blocks_add_mask_copy_##mask_evaluate();                                \
  vand.u16 fb_pixels_rb, fb_pixels, d128_0x7C1F;                               \
  bal 3b                                                                       \


blend_blocks_add_fourth_textured_builder(off)
blend_blocks_add_fourth_textured_builder(on)
blend_blocks_add_fourth_untextured_builder(off)
blend_blocks_add_fourth_untextured_builder(on)

// TODO: Optimize this more. Need a scene that actually uses it for
// confirmation..

.align 3

function(blend_blocks_textured_unblended_on)         
  stmdb sp!, { r4, r14 }
  add mask_msb_ptr, psx_gpu, #psx_gpu_mask_msb_offset
  ldrh num_blocks, [psx_gpu, #psx_gpu_num_blocks_offset]

  add pixel_ptr, psx_gpu, #(psx_gpu_blocks_offset + 16)
  vld1.u16 { msb_mask_low[], msb_mask_high[] }, [mask_msb_ptr, :16]

  add draw_mask_ptr, psx_gpu, #psx_gpu_blocks_offset
  mov c_64, #64

  ldr fb_ptr, [pixel_ptr, #28]
  vld1.u16 { fb_pixels }, [fb_ptr]
  vld1.u32 { draw_mask }, [draw_mask_ptr, :128], c_64
  vclt.s16 write_mask, fb_pixels, #0
  vld1.u32 { pixels }, [pixel_ptr, :128], c_64

  subs num_blocks, num_blocks, #1
  beq 1f

 0:
  vorr.u16 pixels, pixels, msb_mask
  vorr.u16 draw_mask, draw_mask, write_mask
  vbif.u16 fb_pixels, pixels, draw_mask
  vst1.u16 { fb_pixels }, [fb_ptr]

  ldr fb_ptr, [pixel_ptr, #28]
  vld1.u16 { fb_pixels }, [fb_ptr]
  vld1.u32 { draw_mask }, [draw_mask_ptr, :128], c_64
  vclt.s16 write_mask, fb_pixels, #0
  vld1.u32 { pixels }, [pixel_ptr, :128], c_64

  subs num_blocks, num_blocks, #1
  bne 0b
 
 1:
  vorr.u16 pixels, pixels, msb_mask
  vorr.u16 draw_mask, draw_mask, write_mask
  vbif.u16 fb_pixels, pixels, draw_mask
  vst1.u16 { fb_pixels }, [fb_ptr]

  ldmia sp!, { r4, pc }


function(blend_blocks_textured_unblended_off)
  bx lr


function(warmup)
  mov r3, #64
  cmp r0, #0
  bxeq lr

 0:
  vld1.u32 { u_whole_8, v_whole_8 }, [r1, :128], r3

  subs r0, r0, #1
  bne 0b

  bx lr

#undef vram_ptr
#undef color
#undef width
#undef height
#undef pitch

#define vram_ptr                                          r0
#define color                                             r1
#define width                                             r2
#define height                                            r3

#define pitch                                             r1

#define num_width                                         r12

#undef colors_a
#undef colors_b

#define colors_a                                          q0
#define colors_b                                          q1

.align 3

function(render_block_fill_body)
  vdup.u16 colors_a, color
  mov pitch, #2048

  vmov colors_b, colors_a
  sub pitch, pitch, width, lsl #1

  mov num_width, width

 0:  
  vst1.u32 { colors_a, colors_b }, [vram_ptr, :256]!

  subs num_width, num_width, #16
  bne 0b

  add vram_ptr, vram_ptr, pitch
  mov num_width, width

  subs height, height, #1
  bne 0b

  bx lr
 

#undef x
#undef y
#undef width
#undef height
#undef fb_ptr
#undef texture_mask
#undef num_blocks
#undef temp
#undef dirty_textures_mask
#undef clut_ptr
#undef current_texture_mask

#define psx_gpu                                           r0
#define x                                                 r1
#define y                                                 r2
#define u                                                 r3
#define v                                                 r4
#define width                                             r5
#define height                                            r6
#define offset_u                                          r8
#define offset_v                                          r9
#define offset_u_right                                    r10
#define width_rounded                                     r11
#define height_rounded                                    r12

#define texture_offset_base                               r1
#define tile_width                                        r2
#define tile_height                                       r3
#define num_blocks                                        r4
#define block                                             r5
#define sub_tile_height                                   r6
#define fb_ptr                                            r7
#define texture_mask                                      r8
#define column_data                                       r9
#define texture_offset                                    r10
#define tiles_remaining                                   r11
#define fb_ptr_advance_column                             r12
#define texture_block_ptr                                 r14

#define temp                                              r14

#define texture_page_ptr                                  r3
#define left_block_mask                                   r4
#define right_block_mask                                  r5
#define texture_mask_rev                                  r10
#define control_mask                                      r11

#define dirty_textures_mask                               r4
#define clut_ptr                                          r5
#define current_texture_mask                              r6


#undef texels
#undef clut_low_a
#undef clut_low_b
#undef clut_high_a
#undef clut_high_b
#undef clut_a
#undef clut_b
#undef texels_low
#undef texels_high

#define texels                                            d0
#define draw_masks_fb_ptrs                                q1

#define draw_mask_fb_ptr_left                             d2
#define draw_mask_fb_ptr_right                            d3

#define draw_mask_fb_ptr_left_a                           d2
#define draw_mask_fb_ptr_left_b                           d3
#define draw_mask_fb_ptr_right_a                          d10
#define draw_mask_fb_ptr_right_b                          d11
#define draw_masks_fb_ptrs2                               q5

#define clut_low_a                                        d4
#define clut_low_b                                        d5
#define clut_high_a                                       d6
#define clut_high_b                                       d7

#define block_masks                                       d8
#define block_masks_shifted                               d9

#define clut_a                                            q2
#define clut_b                                            q3

#define texels_low                                        d12
#define texels_high                                       d13

#define texels_wide_low                                   d14
#define texels_wide_high                                  d15
#define texels_wide                                       q7


setup_sprite_flush_blocks:
  vpush { q1 - q5 }

  stmdb sp!, { r0 - r3, EXTRA_UNSAVED_REGS r12, r14 }
  bl flush_render_block_buffer
  ldmia sp!, { r0 - r3, EXTRA_UNSAVED_REGS r12, r14 }

  vpop { q1 - q5 }

  add block, psx_gpu, #psx_gpu_blocks_offset
  bx lr


setup_sprite_update_texture_4bpp_cache:
  stmdb sp!, { r0 - r3, r14 }
  bl update_texture_4bpp_cache
  ldmia sp!, { r0 - r3, pc }


setup_sprite_update_texture_8bpp_cache:
  stmdb sp!, { r0 - r3, EXTRA_UNSAVED_REGS r14 }
  bl update_texture_8bpp_cache
  ldmia sp!, { r0 - r3, EXTRA_UNSAVED_REGS pc }


#define setup_sprite_tiled_initialize_4bpp()                                   \
  ldr dirty_textures_mask,                                                     \
   [psx_gpu, #psx_gpu_dirty_textures_4bpp_mask_offset];                        \
  ldr clut_ptr, [psx_gpu, #psx_gpu_clut_ptr_offset];                           \
                                                                               \
  ldr current_texture_mask, [psx_gpu, #psx_gpu_current_texture_mask_offset];   \
  vld1.u32 { clut_a, clut_b }, [clut_ptr, :128];                               \
                                                                               \
  tst current_texture_mask, dirty_textures_mask;                               \
  vuzp.u8 clut_a, clut_b;                                                      \
                                                                               \
  blne setup_sprite_update_texture_4bpp_cache                                  \

#define setup_sprite_tiled_initialize_8bpp()                                   \
  ldr dirty_textures_mask,                                                     \
   [psx_gpu, #psx_gpu_dirty_textures_8bpp_mask_offset];                        \
  ldr current_texture_mask, [psx_gpu, #psx_gpu_current_texture_mask_offset];   \
                                                                               \
  tst current_texture_mask, dirty_textures_mask;                               \
  blne setup_sprite_update_texture_8bpp_cache                                  \


#define setup_sprite_block_count_single()                                      \
  sub_tile_height                                                              \

#define setup_sprite_block_count_double()                                      \
  sub_tile_height, lsl #1                                                      \

#define setup_sprite_tile_add_blocks(type)                                     \
  add num_blocks, num_blocks, setup_sprite_block_count_##type();               \
  cmp num_blocks, #MAX_BLOCKS;                                                 \
                                                                               \
  movgt num_blocks, setup_sprite_block_count_##type();                         \
  blgt setup_sprite_flush_blocks                                               \


#define setup_sprite_tile_full_4bpp(edge)                                      \
  setup_sprite_tile_add_blocks(double);                                        \
                                                                               \
 4:                                                                            \
  and texture_block_ptr, texture_offset, texture_mask;                         \
  vmov.u32 draw_mask_fb_ptr_left[1], fb_ptr;                                   \
                                                                               \
  pld [fb_ptr];                                                                \
  add texture_block_ptr, texture_page_ptr, texture_block_ptr;                  \
  vld1.u32 { texels }, [texture_block_ptr, :64];                               \
                                                                               \
  vtbl.8 texels_low, { clut_low_a, clut_low_b }, texels;                       \
  vtbl.8 texels_high, { clut_high_a, clut_high_b }, texels;                    \
                                                                               \
  vst2.u8 { texels_low, texels_high }, [block, :128];                          \
  add texture_block_ptr, texture_offset, #8;                                   \
                                                                               \
  and texture_block_ptr, texture_block_ptr, texture_mask;                      \
  add block, block, #40;                                                       \
                                                                               \
  add texture_block_ptr, texture_page_ptr, texture_block_ptr;                  \
  add fb_ptr, fb_ptr, #16;                                                     \
                                                                               \
  vst1.u32 { draw_mask_fb_ptr_left }, [block, :64];                            \
  add block, block, #24;                                                       \
                                                                               \
  vld1.u32 { texels }, [texture_block_ptr, :64];                               \
  vtbl.8 texels_low, { clut_low_a, clut_low_b }, texels;                       \
                                                                               \
  pld [fb_ptr];                                                                \
  vmov.u32 draw_mask_fb_ptr_right[1], fb_ptr;                                  \
  vtbl.8 texels_high, { clut_high_a, clut_high_b }, texels;                    \
                                                                               \
  vst2.u8 { texels_low, texels_high }, [block, :128];                          \
  add block, block, #40;                                                       \
                                                                               \
  add texture_offset, texture_offset, #0x10;                                   \
  add fb_ptr, fb_ptr, #(2048 - 16);                                            \
                                                                               \
  vst1.u32 { draw_mask_fb_ptr_right }, [block, :64];                           \
  add block, block, #24;                                                       \
                                                                               \
  subs sub_tile_height, sub_tile_height, #1;                                   \
  bne 4b;                                                                      \
                                                                               \
  add texture_offset, texture_offset, #0xF00;                                  \
  strh num_blocks, [psx_gpu, #psx_gpu_num_blocks_offset]                       \

  
#define setup_sprite_tile_half_4bpp(edge)                                      \
  setup_sprite_tile_add_blocks(single);                                        \
                                                                               \
 4:                                                                            \
  and texture_block_ptr, texture_offset, texture_mask;                         \
  vmov.u32 draw_mask_fb_ptr_##edge[1], fb_ptr;                                 \
                                                                               \
  pld [fb_ptr];                                                                \
  add texture_block_ptr, texture_page_ptr, texture_block_ptr;                  \
  vld1.u32 { texels }, [texture_block_ptr, :64];                               \
                                                                               \
  vtbl.8 texels_low, { clut_low_a, clut_low_b }, texels;                       \
  vtbl.8 texels_high, { clut_high_a, clut_high_b }, texels;                    \
                                                                               \
  vst2.u8 { texels_low, texels_high }, [block, :128];                          \
  add block, block, #40;                                                       \
                                                                               \
  add texture_block_ptr, texture_page_ptr, texture_block_ptr;                  \
  vst1.u32 { draw_mask_fb_ptr_##edge }, [block, :64];                          \
                                                                               \
  add block, block, #24;                                                       \
  add texture_offset, texture_offset, #0x10;                                   \
                                                                               \
  add fb_ptr, fb_ptr, #2048;                                                   \
  subs sub_tile_height, sub_tile_height, #1;                                   \
                                                                               \
  bne 4b;                                                                      \
                                                                               \
  add texture_offset, texture_offset, #0xF00;                                  \
  strh num_blocks, [psx_gpu, #psx_gpu_num_blocks_offset]                       \
 
 
#define setup_sprite_tile_full_8bpp(edge)                                      \
  setup_sprite_tile_add_blocks(double);                                        \
  add block, block, #16;                                                       \
                                                                               \
 4:                                                                            \
  and texture_block_ptr, texture_offset, texture_mask;                         \
  vmov.u32 draw_mask_fb_ptr_left[1], fb_ptr;                                   \
                                                                               \
  pld [fb_ptr];                                                                \
  add texture_block_ptr, texture_page_ptr, texture_block_ptr;                  \
  vld1.u32 { texels }, [texture_block_ptr, :64];                               \
                                                                               \
  add texture_block_ptr, texture_offset, #8;                                   \
  vst1.u32 { texels }, [block, :64];                                           \
                                                                               \
  and texture_block_ptr, texture_block_ptr, texture_mask;                      \
  add block, block, #24;                                                       \
                                                                               \
  add texture_block_ptr, texture_page_ptr, texture_block_ptr;                  \
                                                                               \
  add fb_ptr, fb_ptr, #16;                                                     \
  vst1.u32 { draw_mask_fb_ptr_left }, [block, :64];                            \
                                                                               \
  add block, block, #40;                                                       \
  vld1.u32 { texels }, [texture_block_ptr, :64];                               \
  pld [fb_ptr];                                                                \
                                                                               \
  vmov.u32 draw_mask_fb_ptr_right[1], fb_ptr;                                  \
  vst1.u32 { texels }, [block, :64];                                           \
  add block, block, #24;                                                       \
                                                                               \
  add texture_offset, texture_offset, #0x10;                                   \
  add fb_ptr, fb_ptr, #(2048 - 16);                                            \
                                                                               \
  vst1.u32 { draw_mask_fb_ptr_right }, [block, :64];                           \
  add block, block, #40;                                                       \
                                                                               \
  subs sub_tile_height, sub_tile_height, #1;                                   \
  bne 4b;                                                                      \
                                                                               \
  sub block, block, #16;                                                       \
  add texture_offset, texture_offset, #0xF00;                                  \
  strh num_blocks, [psx_gpu, #psx_gpu_num_blocks_offset]                       \

  
#define setup_sprite_tile_half_8bpp(edge)                                      \
  setup_sprite_tile_add_blocks(single);                                        \
  add block, block, #16;                                                       \
                                                                               \
 4:                                                                            \
  and texture_block_ptr, texture_offset, texture_mask;                         \
  vmov.u32 draw_mask_fb_ptr_##edge[1], fb_ptr;                                 \
  pld [fb_ptr];                                                                \
                                                                               \
  add texture_block_ptr, texture_page_ptr, texture_block_ptr;                  \
  vld1.u32 { texels }, [texture_block_ptr, :64];                               \
                                                                               \
  vst1.u32 { texels }, [block, :64];                                           \
  add block, block, #24;                                                       \
                                                                               \
  vst1.u32 { draw_mask_fb_ptr_##edge }, [block, :64];                          \
  add block, block, #40;                                                       \
                                                                               \
  add texture_offset, texture_offset, #0x10;                                   \
  add fb_ptr, fb_ptr, #2048;                                                   \
                                                                               \
  subs sub_tile_height, sub_tile_height, #1;                                   \
  bne 4b;                                                                      \
                                                                               \
  sub block, block, #16;                                                       \
  add texture_offset, texture_offset, #0xF00;                                  \
  strh num_blocks, [psx_gpu, #psx_gpu_num_blocks_offset]                       \

 
#define setup_sprite_tile_column_edge_pre_adjust_half_right()                  \
  add texture_offset, texture_offset_base, #8;                                 \
  add fb_ptr, fb_ptr, #16                                                      \

#define setup_sprite_tile_column_edge_pre_adjust_half_left()                   \
  mov texture_offset, texture_offset_base                                      \

#define setup_sprite_tile_column_edge_pre_adjust_half(edge)                    \
  setup_sprite_tile_column_edge_pre_adjust_half_##edge()                       \

#define setup_sprite_tile_column_edge_pre_adjust_full(edge)                    \
  mov texture_offset, texture_offset_base                                      \

#define setup_sprite_tile_column_edge_post_adjust_half_right()                 \
  sub fb_ptr, fb_ptr, #16                                                      \

#define setup_sprite_tile_column_edge_post_adjust_half_left()                  \

#define setup_sprite_tile_column_edge_post_adjust_half(edge)                   \
  setup_sprite_tile_column_edge_post_adjust_half_##edge()                      \

#define setup_sprite_tile_column_edge_post_adjust_full(edge)                   \


#define setup_sprite_tile_column_height_single(edge_mode, edge, texture_mode,  \
 x4mode)                                                                       \
  mov sub_tile_height, column_data;                                            \
  setup_sprite_tile_column_edge_pre_adjust_##edge_mode##x4mode(edge);          \
  setup_sprite_tile_##edge_mode##_##texture_mode##x4mode(edge);                \
  setup_sprite_tile_column_edge_post_adjust_##edge_mode##x4mode(edge)          \

#define setup_sprite_tile_column_height_multi(edge_mode, edge, texture_mode,   \
 x4mode)                                                                       \
  and sub_tile_height, column_data, #0xFF;                                     \
  mov tiles_remaining, column_data, lsr #16;                                   \
  setup_sprite_tile_column_edge_pre_adjust_##edge_mode##x4mode(edge);          \
  setup_sprite_tile_##edge_mode##_##texture_mode##x4mode(edge);                \
                                                                               \
  subs tiles_remaining, tiles_remaining, #1;                                   \
  beq 2f;                                                                      \
                                                                               \
 3:                                                                            \
  mov sub_tile_height, #16;                                                    \
  setup_sprite_tile_##edge_mode##_##texture_mode##x4mode(edge);                \
  subs tiles_remaining, tiles_remaining, #1;                                   \
  bne 3b;                                                                      \
                                                                               \
 2:                                                                            \
  uxtb sub_tile_height, column_data, ror #8;                                   \
  setup_sprite_tile_##edge_mode##_##texture_mode##x4mode(edge);                \
  setup_sprite_tile_column_edge_post_adjust_##edge_mode##x4mode(edge)          \


#define setup_sprite_column_data_single()                                      \
  mov column_data, height;                                                     \
  ldr texture_page_ptr, [psx_gpu, #psx_gpu_texture_page_ptr_offset]            \

#define setup_sprite_column_data_multi()                                       \
  and height_rounded, height_rounded, #0xF;                                    \
  rsb column_data, offset_v, #16;                                              \
                                                                               \
  add height_rounded, height_rounded, #1;                                      \
  sub tile_height, tile_height, #1;                                            \
                                                                               \
  orr column_data, column_data, tile_height, lsl #16;                          \
  ldr texture_page_ptr, [psx_gpu, #psx_gpu_texture_page_ptr_offset];           \
                                                                               \
  orr column_data, column_data, height_rounded, lsl #8                         \

#define setup_sprite_setup_left_draw_mask_fb_ptr()                             \
  vdup.u8 draw_mask_fb_ptr_left, block_masks[0];                               \
  vdup.u8 draw_mask_fb_ptr_right, block_masks[1]                               \

#define setup_sprite_setup_left_draw_mask_fb_ptr_advance_column()              \
  mov fb_ptr_advance_column, #32;                                              \
  vdup.u8 draw_mask_fb_ptr_left, block_masks[0];                               \
                                                                               \
  sub fb_ptr_advance_column, fb_ptr_advance_column, height, lsl #11;           \
  vdup.u8 draw_mask_fb_ptr_right, block_masks[1]                               \

#define setup_sprite_setup_right_draw_mask_fb_ptr()                            \
  vdup.u8 draw_mask_fb_ptr_left, block_masks[4];                               \
  vdup.u8 draw_mask_fb_ptr_right, block_masks[5]                               \

#define setup_sprite_tile_column_width_single(tm, multi_height, edge_mode,     \
 edge, x4mode)                                                                 \
 setup_sprite_##tm##_single_##multi_height##_##edge_mode##_##edge##x4mode:     \
  setup_sprite_column_data_##multi_height();                                   \
  vext.32 block_masks_shifted, block_masks, block_masks, #1;                   \
  vorr.u32 block_masks, block_masks, block_masks_shifted;                      \
  setup_sprite_setup_left_draw_mask_fb_ptr##x4mode();                          \
                                                                               \
  setup_sprite_tile_column_height_##multi_height(edge_mode, edge, tm, x4mode); \
  ldmia sp!, { r4 - r11, pc }                                                  \

#define setup_sprite_tiled_advance_column()                                    \
  add texture_offset_base, texture_offset_base, #0x100;                        \
  tst texture_offset_base, #0xF00;                                             \
  subeq texture_offset_base, texture_offset_base, #(0x100 + 0xF00)             \

#define setup_sprite_tile_column_width_multi(tm, multi_height, left_mode,      \
 right_mode, x4mode)                                                           \
 setup_sprite_##tm##_multi_##multi_height##_##left_mode##_##right_mode##x4mode:\
  setup_sprite_column_data_##multi_height();                                   \
                                                                               \
  setup_sprite_setup_left_draw_mask_fb_ptr_advance_column##x4mode();           \
                                                                               \
  setup_sprite_tile_column_height_##multi_height(left_mode, right, tm, x4mode);\
                                                                               \
  subs tile_width, tile_width, #2;                                             \
  add fb_ptr, fb_ptr, fb_ptr_advance_column;                                   \
                                                                               \
  beq 1f;                                                                      \
                                                                               \
  vmov.u8 draw_masks_fb_ptrs, #0;                                              \
  vmov.u8 draw_masks_fb_ptrs2, #0;                                             \
                                                                               \
 0:                                                                            \
  setup_sprite_tiled_advance_column();                                         \
  setup_sprite_tile_column_height_##multi_height(full, none, tm, x4mode);      \
  add fb_ptr, fb_ptr, fb_ptr_advance_column;                                   \
  subs tile_width, tile_width, #1;                                             \
  bne 0b;                                                                      \
                                                                               \
 1:                                                                            \
  setup_sprite_setup_right_draw_mask_fb_ptr##x4mode();                         \
                                                                               \
  setup_sprite_tiled_advance_column();                                         \
  setup_sprite_tile_column_height_##multi_height(right_mode, left, tm, x4mode);\
  ldmia sp!, { r4 - r11, pc }                                                  \


#define setup_sprite_offset_u_adjust()                                         \

#define setup_sprite_get_left_block_mask()                                     \
  and left_block_mask, left_block_mask, #0xFF                                  \

#define setup_sprite_compare_left_block_mask()                                 \
  cmp left_block_mask, #0xFF                                                   \

#define setup_sprite_get_right_block_mask()                                    \
  uxtb right_block_mask, right_block_mask, ror #8                              \

#define setup_sprite_compare_right_block_mask()                                \
  cmp right_block_mask, #0xFF                                                  \



/* 4x stuff */
#define fb_ptr2 column_data

#define setup_sprite_offset_u_adjust_4x()                                      \
  sub fb_ptr, fb_ptr, offset_u, lsl #1;                                        \
  lsl offset_u_right, #1;                                                      \
  lsl offset_u, #1;                                                            \
  add offset_u_right, #1                                                       \

#define setup_sprite_get_left_block_mask_4x()                                  \
  sxth left_block_mask, left_block_mask                                        \

#define setup_sprite_compare_left_block_mask_4x()                              \
  cmp left_block_mask, #0xFFFFFFFF                                             \

#define setup_sprite_get_right_block_mask_4x()                                 \
  sxth right_block_mask, right_block_mask, ror #16                             \

#define setup_sprite_compare_right_block_mask_4x()                             \
  cmp right_block_mask, #0xFFFFFFFF                                            \


#define widen_texels_16bpp(texels_)                                            \
  vmov texels_wide_low, texels_;                                               \
  vmov texels_wide_high, texels_;                                              \
  vzip.16 texels_wide_low, texels_wide_high                                    \

#define widen_texels_8bpp(texels_)                                             \
  vmov texels_wide_low, texels_;                                               \
  vmov texels_wide_high, texels_;                                              \
  vzip.8 texels_wide_low, texels_wide_high                                     \

#define write_block_16bpp(texels_, block_, draw_mask_fb_ptr_, fb_ptr_)         \
  vst1.u32 { texels_ }, [block_, :128];                                        \
  add block_, block_, #40;                                                     \
                                                                               \
  vmov.u32 draw_mask_fb_ptr_[1], fb_ptr_;                                      \
  vst1.u32 { draw_mask_fb_ptr_ }, [block_, :64];                               \
  add block_, block_, #24                                                      \

/* assumes 16-byte offset already added to block_ */
#define write_block_8bpp(texels_, block_, draw_mask_fb_ptr_, fb_ptr_)          \
  vst1.u32 { texels_ }, [block_, :64];                                         \
  add block_, block_, #24;                                                     \
                                                                               \
  vmov.u32 draw_mask_fb_ptr_[1], fb_ptr_;                                      \
  vst1.u32 { draw_mask_fb_ptr_ }, [block_, :64];                               \
  add block_, block_, #40                                                      \

#define do_texture_block_16bpp_4x(fb_ptr_tmp, draw_mask_fb_ptr_a_,             \
 draw_mask_fb_ptr_b_)                                                          \
  widen_texels_16bpp(texels_low);                                              \
  add fb_ptr_tmp, fb_ptr, #1024*2;                                             \
                                                                               \
  write_block_16bpp(texels_wide, block, draw_mask_fb_ptr_a_, fb_ptr);          \
                                                                               \
  write_block_16bpp(texels_wide, block, draw_mask_fb_ptr_a_, fb_ptr_tmp);      \
  widen_texels_16bpp(texels_high);                                             \
                                                                               \
  add fb_ptr_tmp, fb_ptr, #8*2;                                                \
  write_block_16bpp(texels_wide, block, draw_mask_fb_ptr_b_, fb_ptr_tmp);      \
                                                                               \
  add fb_ptr_tmp, fb_ptr_tmp, #1024*2;                                         \
  write_block_16bpp(texels_wide, block, draw_mask_fb_ptr_b_, fb_ptr_tmp)       \

#define do_texture_block_8bpp_4x(fb_ptr_tmp, draw_mask_fb_ptr_a_,              \
 draw_mask_fb_ptr_b_)                                                          \
  widen_texels_8bpp(texels);                                                   \
  add fb_ptr_tmp, fb_ptr, #1024*2;                                             \
                                                                               \
  write_block_8bpp(texels_wide_low, block, draw_mask_fb_ptr_a_, fb_ptr);       \
  write_block_8bpp(texels_wide_low, block, draw_mask_fb_ptr_a_, fb_ptr_tmp);   \
                                                                               \
  add fb_ptr_tmp, fb_ptr, #8*2;                                                \
  write_block_8bpp(texels_wide_high, block, draw_mask_fb_ptr_b_, fb_ptr_tmp);  \
                                                                               \
  add fb_ptr_tmp, fb_ptr_tmp, #1024*2;                                         \
  write_block_8bpp(texels_wide_high, block, draw_mask_fb_ptr_b_, fb_ptr_tmp)   \


#define setup_sprite_tiled_initialize_4bpp_4x()                                \
  ldr clut_ptr, [psx_gpu, #psx_gpu_clut_ptr_offset];                           \
  vld1.u32 { clut_a, clut_b }, [clut_ptr, :128];                               \
                                                                               \
  vuzp.u8 clut_a, clut_b                                                       \

#define setup_sprite_tiled_initialize_8bpp_4x()                                \


#define setup_sprite_block_count_single_4x()                                   \
  sub_tile_height, lsl #2                                                      \

#define setup_sprite_block_count_double_4x()                                   \
  sub_tile_height, lsl #(1+2)                                                  \

#define setup_sprite_tile_full_4bpp_4x(edge)                                   \
  setup_sprite_tile_add_blocks(double_4x);                                     \
  str column_data, [sp, #-8]!; /* fb_ptr2 */                                   \
                                                                               \
 4:                                                                            \
  and texture_block_ptr, texture_offset, texture_mask;                         \
  pld [fb_ptr];                                                                \
                                                                               \
  add texture_block_ptr, texture_page_ptr, texture_block_ptr;                  \
  vld1.u32 { texels }, [texture_block_ptr, :64];                               \
                                                                               \
  add texture_block_ptr, texture_offset, #8;                                   \
  vtbl.8 texels_low, { clut_low_a, clut_low_b }, texels;                       \
                                                                               \
  and texture_block_ptr, texture_block_ptr, texture_mask;                      \
  vtbl.8 texels_high, { clut_high_a, clut_high_b }, texels;                    \
                                                                               \
  vzip.8 texels_low, texels_high;                                              \
  do_texture_block_16bpp_4x(fb_ptr2, draw_mask_fb_ptr_left_a,                  \
   draw_mask_fb_ptr_left_b);                                                   \
                                                                               \
  add texture_block_ptr, texture_page_ptr, texture_block_ptr;                  \
  pld [fb_ptr, #2048];                                                         \
                                                                               \
  vld1.u32 { texels }, [texture_block_ptr, :64];                               \
  add fb_ptr, fb_ptr, #16*2;                                                   \
                                                                               \
  vtbl.8 texels_low, { clut_low_a, clut_low_b }, texels;                       \
  vtbl.8 texels_high, { clut_high_a, clut_high_b }, texels;                    \
                                                                               \
  vzip.8 texels_low, texels_high;                                              \
  do_texture_block_16bpp_4x(fb_ptr2, draw_mask_fb_ptr_right_a,                 \
   draw_mask_fb_ptr_right_b);                                                  \
                                                                               \
  add texture_offset, texture_offset, #0x10;                                   \
  add fb_ptr, fb_ptr, #(2048 - 16) * 2;                                        \
                                                                               \
  subs sub_tile_height, sub_tile_height, #1;                                   \
  bne 4b;                                                                      \
                                                                               \
  ldr column_data, [sp], #8; /* fb_ptr2 */                                     \
  add texture_offset, texture_offset, #0xF00;                                  \
  strh num_blocks, [psx_gpu, #psx_gpu_num_blocks_offset]                       \


#define setup_sprite_tile_half_4bpp_4x(edge)                                   \
  setup_sprite_tile_add_blocks(single_4x);                                     \
  str column_data, [sp, #-8]!; /* fb_ptr2 */                                   \
                                                                               \
 4:                                                                            \
  and texture_block_ptr, texture_offset, texture_mask;                         \
  pld [fb_ptr];                                                                \
                                                                               \
  add texture_block_ptr, texture_page_ptr, texture_block_ptr;                  \
  vld1.u32 { texels }, [texture_block_ptr, :64];                               \
                                                                               \
  add texture_block_ptr, texture_page_ptr, texture_block_ptr;                  \
  vtbl.8 texels_low, { clut_low_a, clut_low_b }, texels;                       \
                                                                               \
  vtbl.8 texels_high, { clut_high_a, clut_high_b }, texels;                    \
  add texture_offset, texture_offset, #0x10;                                   \
                                                                               \
  vzip.8 texels_low, texels_high;                                              \
  do_texture_block_16bpp_4x(fb_ptr2, draw_mask_fb_ptr_##edge##_a,              \
   draw_mask_fb_ptr_##edge##_b);                                               \
                                                                               \
  pld [fb_ptr, #2048];                                                         \
  add fb_ptr, fb_ptr, #2048 * 2;                                               \
                                                                               \
  subs sub_tile_height, sub_tile_height, #1;                                   \
  bne 4b;                                                                      \
                                                                               \
  ldr column_data, [sp], #8; /* fb_ptr2 */                                     \
  add texture_offset, texture_offset, #0xF00;                                  \
  strh num_blocks, [psx_gpu, #psx_gpu_num_blocks_offset]                       \


#define setup_sprite_tile_full_8bpp_4x(edge)                                   \
  setup_sprite_tile_add_blocks(double_4x);                                     \
  add block, block, #16;                                                       \
  str column_data, [sp, #-8]!; /* fb_ptr2 */                                   \
                                                                               \
 4:                                                                            \
  and texture_block_ptr, texture_offset, texture_mask;                         \
  pld [fb_ptr];                                                                \
                                                                               \
  add texture_block_ptr, texture_page_ptr, texture_block_ptr;                  \
  vld1.u32 { texels }, [texture_block_ptr, :64];                               \
                                                                               \
  add texture_block_ptr, texture_offset, #8;                                   \
  do_texture_block_8bpp_4x(fb_ptr2, draw_mask_fb_ptr_left_a,                   \
   draw_mask_fb_ptr_left_b);                                                   \
                                                                               \
  pld [fb_ptr, #2048];                                                         \
  and texture_block_ptr, texture_block_ptr, texture_mask;                      \
                                                                               \
  add fb_ptr, fb_ptr, #16*2;                                                   \
  add texture_block_ptr, texture_page_ptr, texture_block_ptr;                  \
                                                                               \
  vld1.u32 { texels }, [texture_block_ptr, :64];                               \
                                                                               \
  do_texture_block_8bpp_4x(fb_ptr2, draw_mask_fb_ptr_right_a,                  \
   draw_mask_fb_ptr_right_b);                                                  \
                                                                               \
  add texture_offset, texture_offset, #0x10;                                   \
  add fb_ptr, fb_ptr, #(2048 - 16) * 2;                                        \
                                                                               \
  subs sub_tile_height, sub_tile_height, #1;                                   \
  bne 4b;                                                                      \
                                                                               \
  sub block, block, #16;                                                       \
  ldr column_data, [sp], #8; /* fb_ptr2 */                                     \
  add texture_offset, texture_offset, #0xF00;                                  \
  strh num_blocks, [psx_gpu, #psx_gpu_num_blocks_offset]                       \

  
#define setup_sprite_tile_half_8bpp_4x(edge)                                   \
  setup_sprite_tile_add_blocks(single_4x);                                     \
  add block, block, #16;                                                       \
  str column_data, [sp, #-8]!; /* fb_ptr2 */                                   \
                                                                               \
 4:                                                                            \
  and texture_block_ptr, texture_offset, texture_mask;                         \
  pld [fb_ptr];                                                                \
                                                                               \
  add texture_block_ptr, texture_page_ptr, texture_block_ptr;                  \
  vld1.u32 { texels }, [texture_block_ptr, :64];                               \
                                                                               \
  pld [fb_ptr, #2048];                                                         \
  do_texture_block_8bpp_4x(fb_ptr2, draw_mask_fb_ptr_##edge##_a,               \
   draw_mask_fb_ptr_##edge##_b);                                               \
                                                                               \
  add texture_offset, texture_offset, #0x10;                                   \
  add fb_ptr, fb_ptr, #2048 * 2;                                               \
                                                                               \
  subs sub_tile_height, sub_tile_height, #1;                                   \
  bne 4b;                                                                      \
                                                                               \
  sub block, block, #16;                                                       \
  ldr column_data, [sp], #8; /* fb_ptr2 */                                     \
  add texture_offset, texture_offset, #0xF00;                                  \
  strh num_blocks, [psx_gpu, #psx_gpu_num_blocks_offset]                       \

 
#define setup_sprite_tile_column_edge_pre_adjust_half_right_4x()               \
  add texture_offset, texture_offset_base, #8;                                 \
  add fb_ptr, fb_ptr, #16 * 2                                                  \

#define setup_sprite_tile_column_edge_pre_adjust_half_left_4x()                \
  mov texture_offset, texture_offset_base                                      \

#define setup_sprite_tile_column_edge_pre_adjust_half_4x(edge)                 \
  setup_sprite_tile_column_edge_pre_adjust_half_##edge##_4x()                  \

#define setup_sprite_tile_column_edge_pre_adjust_full_4x(edge)                 \
  mov texture_offset, texture_offset_base                                      \

#define setup_sprite_tile_column_edge_post_adjust_half_right_4x()              \
  sub fb_ptr, fb_ptr, #16 * 2                                                  \

#define setup_sprite_tile_column_edge_post_adjust_half_left_4x()               \

#define setup_sprite_tile_column_edge_post_adjust_half_4x(edge)                \
  setup_sprite_tile_column_edge_post_adjust_half_##edge##_4x()                 \

#define setup_sprite_tile_column_edge_post_adjust_full_4x(edge)                \


#define setup_sprite_setup_left_draw_mask_fb_ptr_4x()                          \
  vdup.u8 draw_mask_fb_ptr_left_a, block_masks[0];                             \
  vdup.u8 draw_mask_fb_ptr_left_b, block_masks[1];                             \
  vdup.u8 draw_mask_fb_ptr_right_a, block_masks[2];                            \
  vdup.u8 draw_mask_fb_ptr_right_b, block_masks[3]                             \

#define setup_sprite_setup_left_draw_mask_fb_ptr_advance_column_4x()           \
  mov fb_ptr_advance_column, #32 * 2;                                          \
  vdup.u8 draw_mask_fb_ptr_left_a, block_masks[0];                             \
  vdup.u8 draw_mask_fb_ptr_left_b, block_masks[1];                             \
  sub fb_ptr_advance_column, fb_ptr_advance_column, height, lsl #11 + 1;       \
  vdup.u8 draw_mask_fb_ptr_right_a, block_masks[2];                            \
  vdup.u8 draw_mask_fb_ptr_right_b, block_masks[3]                             \

#define setup_sprite_setup_right_draw_mask_fb_ptr_4x()                         \
  vdup.u8 draw_mask_fb_ptr_left_a, block_masks[4];                             \
  vdup.u8 draw_mask_fb_ptr_left_b, block_masks[5];                             \
  vdup.u8 draw_mask_fb_ptr_right_a, block_masks[6];                            \
  vdup.u8 draw_mask_fb_ptr_right_b, block_masks[7]                             \


// r0: psx_gpu
// r1: x
// r2: y
// r3: u
// [sp]: v
// [sp + 4]: width
// [sp + 8]: height
// [sp + 12]: color (unused)

#define setup_sprite_tiled_builder(texture_mode, x4mode)                       \
                                                                               \
setup_sprite_tile_column_width_multi(texture_mode,  multi,  full, full,        \
  x4mode);                                                                     \
setup_sprite_tile_column_width_single(texture_mode, multi,  full, none,        \
  x4mode);                                                                     \
setup_sprite_tile_column_width_multi(texture_mode,  single, full, full,        \
  x4mode);                                                                     \
setup_sprite_tile_column_width_single(texture_mode, single, full, none,        \
  x4mode);                                                                     \
setup_sprite_tile_column_width_multi(texture_mode,  multi,  half, full,        \
  x4mode);                                                                     \
setup_sprite_tile_column_width_single(texture_mode, multi,  half, right,       \
  x4mode);                                                                     \
setup_sprite_tile_column_width_multi(texture_mode,  single, half, full,        \
  x4mode);                                                                     \
setup_sprite_tile_column_width_single(texture_mode, single, half, right,       \
  x4mode);                                                                     \
setup_sprite_tile_column_width_multi(texture_mode,  multi,  full, half,        \
  x4mode);                                                                     \
setup_sprite_tile_column_width_single(texture_mode, multi,  half, left,        \
  x4mode);                                                                     \
setup_sprite_tile_column_width_multi(texture_mode,  single, full, half,        \
  x4mode);                                                                     \
setup_sprite_tile_column_width_single(texture_mode, single, half, left,        \
  x4mode);                                                                     \
setup_sprite_tile_column_width_multi(texture_mode,  multi,  half, half,        \
  x4mode);                                                                     \
setup_sprite_tile_column_width_multi(texture_mode,  single, half, half,        \
  x4mode);                                                                     \
                                                                               \
.align 4;                                                                      \
                                                                               \
function(setup_sprite_##texture_mode##x4mode)                                  \
  stmdb sp!, { r4 - r11, r14 };                                                \
  setup_sprite_tiled_initialize_##texture_mode##x4mode();                      \
                                                                               \
  ldr v, [sp, #36];                                                            \
  and offset_u, u, #0xF;                                                       \
                                                                               \
  ldr width, [sp, #40];                                                        \
  ldr fb_ptr, [psx_gpu, #psx_gpu_vram_out_ptr_offset];                         \
                                                                               \
  ldr height, [sp, #44];                                                       \
  add fb_ptr, fb_ptr, y, lsl #11;                                              \
                                                                               \
  add fb_ptr, fb_ptr, x, lsl #1;                                               \
  and offset_v, v, #0xF;                                                       \
                                                                               \
  sub fb_ptr, fb_ptr, offset_u, lsl #1;                                        \
  add width_rounded, offset_u, width;                                          \
                                                                               \
  add height_rounded, offset_v, height;                                        \
  add width_rounded, width_rounded, #15;                                       \
                                                                               \
  add height_rounded, height_rounded, #15;                                     \
  mov tile_width, width_rounded, lsr #4;                                       \
                                                                               \
  /* texture_offset_base = VH-VL-00-00                                       */\
  mov texture_offset_base, v, lsl #8;                                          \
  and offset_u_right, width_rounded, #0xF;                                     \
                                                                               \
  /* texture_offset_base = VH-UH-UL-00                                       */\
  bfi texture_offset_base, u, #4, #8;                                          \
  mov right_block_mask, #0xFFFFFFFE;                                           \
                                                                               \
  setup_sprite_offset_u_adjust##x4mode();                                      \
                                                                               \
  /* texture_offset_base = VH-UH-VL-00                                       */\
  bfi texture_offset_base, v, #4, #4;                                          \
  mov left_block_mask, #0xFFFFFFFF;                                            \
                                                                               \
  mov tile_height, height_rounded, lsr #4;                                     \
  mvn left_block_mask, left_block_mask, lsl offset_u;                          \
                                                                               \
  /* texture_mask = HH-HL-WH-WL                                              */\
  ldrh texture_mask, [psx_gpu, #psx_gpu_texture_mask_width_offset];            \
  mov right_block_mask, right_block_mask, lsl offset_u_right;                  \
                                                                               \
  /* texture_mask_rev = WH-WL-HH-HL                                          */\
  rev16 texture_mask_rev, texture_mask;                                        \
  vmov block_masks, left_block_mask, right_block_mask;                         \
                                                                               \
  /* texture_mask = HH-HL-HL-WL                                              */\
  bfi texture_mask, texture_mask_rev, #4, #4;                                  \
  /* texture_mask_rev = 00-00-00-WH                                          */\
  mov texture_mask_rev, texture_mask_rev, lsr #12;                             \
                                                                               \
  /* texture_mask = HH-WH-HL-WL                                              */\
  bfi texture_mask, texture_mask_rev, #8, #4;                                  \
  setup_sprite_get_left_block_mask##x4mode();                                  \
                                                                               \
  mov control_mask, #0;                                                        \
  setup_sprite_compare_left_block_mask##x4mode();                              \
                                                                               \
  setup_sprite_get_right_block_mask##x4mode();                                 \
  orreq control_mask, control_mask, #0x4;                                      \
                                                                               \
  ldrh num_blocks, [psx_gpu, #psx_gpu_num_blocks_offset];                      \
  setup_sprite_compare_right_block_mask##x4mode();                             \
                                                                               \
  orreq control_mask, control_mask, #0x8;                                      \
  cmp tile_width, #1;                                                          \
                                                                               \
  add block, psx_gpu, #psx_gpu_blocks_offset;                                  \
  orreq control_mask, control_mask, #0x1;                                      \
                                                                               \
  cmp tile_height, #1;                                                         \
  add block, block, num_blocks, lsl #6;                                        \
                                                                               \
  orreq control_mask, control_mask, #0x2;                                      \
  JT_OP_REL(9f, control_mask, temp);                                           \
  JT_OP(ldr pc, [pc, control_mask, lsl #2]);                                   \
  nop;                                                                         \
                                                                               \
 9:                                                                            \
 .word JTE(9b, setup_sprite_##texture_mode##_multi_multi_full_full##x4mode);   \
 .word JTE(9b, setup_sprite_##texture_mode##_single_multi_full_none##x4mode);  \
 .word JTE(9b, setup_sprite_##texture_mode##_multi_single_full_full##x4mode);  \
 .word JTE(9b, setup_sprite_##texture_mode##_single_single_full_none##x4mode); \
 .word JTE(9b, setup_sprite_##texture_mode##_multi_multi_half_full##x4mode);   \
 .word JTE(9b, setup_sprite_##texture_mode##_single_multi_half_right##x4mode); \
 .word JTE(9b, setup_sprite_##texture_mode##_multi_single_half_full##x4mode);  \
 .word JTE(9b, setup_sprite_##texture_mode##_single_single_half_right##x4mode);\
 .word JTE(9b, setup_sprite_##texture_mode##_multi_multi_full_half##x4mode);   \
 .word JTE(9b, setup_sprite_##texture_mode##_single_multi_half_left##x4mode);  \
 .word JTE(9b, setup_sprite_##texture_mode##_multi_single_full_half##x4mode);  \
 .word JTE(9b, setup_sprite_##texture_mode##_single_single_half_left##x4mode); \
 .word JTE(9b, setup_sprite_##texture_mode##_multi_multi_half_half##x4mode);   \
 .word 0x00000000;                                                             \
 .word JTE(9b, setup_sprite_##texture_mode##_multi_single_half_half##x4mode);  \


setup_sprite_tiled_builder(4bpp,);
setup_sprite_tiled_builder(8bpp,);

#undef draw_mask_fb_ptr_left
#undef draw_mask_fb_ptr_right

setup_sprite_tiled_builder(4bpp, _4x);
setup_sprite_tiled_builder(8bpp, _4x);


#undef block_ptr
#undef num_blocks
#undef clut_ptr

#define psx_gpu                                           r0
#define block_ptr                                         r0
#define num_blocks                                        r1
#define clut_ptr                                          r2
#define texel_shift_mask                                  r3
#define block_pixels_a                                    r4
#define block_pixels_b                                    r5
#define texel_0                                           r6
#define texel_2                                           r7
#define texel_4                                           r8
#define texel_6                                           r9
#define texel_1                                           r10
#define texel_3                                           r11
#define texel_5                                           r12
#define texel_7                                           r14
#define texels_01                                         r6
#define texels_23                                         r7
#define texels_45                                         r8
#define texels_67                                         r9

function(texture_sprite_blocks_8bpp)
  stmdb sp!, { r4 - r11, r14 }
  movw texel_shift_mask, #(0xFF << 1)

  ldrh num_blocks, [psx_gpu, #psx_gpu_num_blocks_offset]
  ldr clut_ptr, [psx_gpu, #psx_gpu_clut_ptr_offset]

  add block_ptr, psx_gpu, #psx_gpu_blocks_offset
  ldr block_pixels_a, [block_ptr, #16]

 0:
  and texel_0, texel_shift_mask, block_pixels_a, lsl #1
  ldr block_pixels_b, [block_ptr, #20]

  and texel_1, texel_shift_mask, block_pixels_a, lsr #7
  ldrh texel_0, [clut_ptr, texel_0]

  and texel_2, texel_shift_mask, block_pixels_a, lsr #15
  ldrh texel_1, [clut_ptr, texel_1]

  and texel_3, texel_shift_mask, block_pixels_a, lsr #23
  ldr block_pixels_a, [block_ptr, #(64 + 16)]

  ldrh texel_2, [clut_ptr, texel_2]
  and texel_4, texel_shift_mask, block_pixels_b, lsl #1

  ldrh texel_3, [clut_ptr, texel_3]
  and texel_5, texel_shift_mask, block_pixels_b, lsr #7

  ldrh texel_4, [clut_ptr, texel_4]
  and texel_6, texel_shift_mask, block_pixels_b, lsr #15

  ldrh texel_5, [clut_ptr, texel_5]
  and texel_7, texel_shift_mask, block_pixels_b, lsr #23

  ldrh texel_6, [clut_ptr, texel_6]
  orr texels_01, texel_0, texel_1, lsl #16

  ldrh texel_7, [clut_ptr, texel_7]
  orr texels_23, texel_2, texel_3, lsl #16

  orr texels_45, texel_4, texel_5, lsl #16
  str texels_01, [block_ptr, #0]

  orr texels_67, texel_6, texel_7, lsl #16
  str texels_23, [block_ptr, #4]

  subs num_blocks, num_blocks, #1
  str texels_45, [block_ptr, #8]

  str texels_67, [block_ptr, #12]
  add block_ptr, block_ptr, #64

  bne 0b

  ldmia sp!, { r4 - r11, pc }


#undef width_rounded
#undef texture_mask
#undef num_blocks
#undef texture_offset
#undef texels_low
#undef texels_high
#undef texels_wide_low
#undef texels_wide_high
#undef texels_wide
#undef fb_ptr2
#undef temp

#define psx_gpu                                           r0
#define x                                                 r1
#define y                                                 r2
#define u                                                 r3
#define v                                                 r4
#define width                                             r5
#define height                                            r6
#define left_offset                                       r8
#define width_rounded                                     r9
#define right_width                                       r10

#define block_width                                       r11

#define texture_offset_base                               r1
#define texture_mask                                      r2
#define texture_page_ptr                                  r3
#define num_blocks                                        r4
#define block                                             r5
#define fb_ptr                                            r7
#define texture_offset                                    r8
#define blocks_remaining                                  r9
#define fb_ptr2                                           r10
#define fb_ptr_pitch                                      r12
#define texture_block_ptr                                 r14

#define texture_mask_width                                r2
#define texture_mask_height                               r3
#define left_mask_bits                                    r4
#define right_mask_bits                                   r5


#undef block_masks
#undef block_masks_shifted
#undef texels

#define block_masks                                       d0
#define block_masks_shifted                               d1
#define draw_mask_fb_ptr                                  d2
#define texels                                            q2

#define draw_mask_fb_ptr_a                                d2
#define draw_mask_fb_ptr_b                                d3
#define texels_low                                        d4
#define texels_high                                       d5
#define texels_wide_low                                   d6
#define texels_wide_high                                  d7
#define texels_wide                                       q3


setup_sprites_16bpp_flush:
  vpush { d0 - d3 }

  stmdb sp!, { r0 - r3, EXTRA_UNSAVED_REGS r12, r14 }
  bl flush_render_block_buffer
  ldmia sp!, { r0 - r3, EXTRA_UNSAVED_REGS r12, r14 }

  vpop { d0 - d3 }

  add block, psx_gpu, #psx_gpu_blocks_offset
  mov num_blocks, block_width

  bx lr

function(setup_sprite_16bpp)
  stmdb sp!, { r4 - r11, r14 }
  ldr fb_ptr, [psx_gpu, #psx_gpu_vram_out_ptr_offset]

  ldr v, [sp, #36]
  add fb_ptr, fb_ptr, y, lsl #11

  ldr width, [sp, #40]
  add fb_ptr, fb_ptr, x, lsl #1

  ldr height, [sp, #44]
  and left_offset, u, #0x7

  add texture_offset_base, u, u
  add width_rounded, width, #7

  add texture_offset_base, texture_offset_base, v, lsl #11
  mov left_mask_bits, #0xFF
  
  ldrb texture_mask_width, [psx_gpu, #psx_gpu_texture_mask_width_offset]
  add width_rounded, width_rounded, left_offset

  ldrb texture_mask_height, [psx_gpu, #psx_gpu_texture_mask_height_offset]
  sub fb_ptr, fb_ptr, left_offset, lsl #1

  add texture_mask, texture_mask_width, texture_mask_width
  mov right_mask_bits, #0xFE

  and right_width, width_rounded, #0x7
  mvn left_mask_bits, left_mask_bits, lsl left_offset

  add texture_mask, texture_mask, texture_mask_height, lsl #11
  mov block_width, width_rounded, lsr #3

  mov right_mask_bits, right_mask_bits, lsl right_width
  movw fb_ptr_pitch, #(2048 + 16)

  sub fb_ptr_pitch, fb_ptr_pitch, block_width, lsl #4
  vmov block_masks, left_mask_bits, right_mask_bits

  ldrh num_blocks, [psx_gpu, #psx_gpu_num_blocks_offset]
  add block, psx_gpu, #psx_gpu_blocks_offset

  bic texture_offset_base, texture_offset_base, #0xF
  cmp block_width, #1

  ldr texture_page_ptr, [psx_gpu, #psx_gpu_texture_page_ptr_offset]
  add block, block, num_blocks, lsl #6

  bne 0f

  vext.32 block_masks_shifted, block_masks, block_masks, #1
  vorr.u32 block_masks, block_masks, block_masks_shifted
  vdup.u8 draw_mask_fb_ptr, block_masks[0]

 1:
  add num_blocks, num_blocks, #1
  cmp num_blocks, #MAX_BLOCKS
  blgt setup_sprites_16bpp_flush

  and texture_block_ptr, texture_offset_base, texture_mask
  subs height, height, #1

  add texture_block_ptr, texture_page_ptr, texture_block_ptr
  vld1.u32 { texels }, [texture_block_ptr, :128]

  vst1.u32 { texels }, [block, :128]
  add block, block, #40

  vmov.u32 draw_mask_fb_ptr[1], fb_ptr
  pld [fb_ptr]

  vst1.u32 { draw_mask_fb_ptr }, [block, :64]

  add block, block, #24
  add texture_offset_base, texture_offset_base, #2048
  add fb_ptr, fb_ptr, #2048
  strh num_blocks, [psx_gpu, #psx_gpu_num_blocks_offset]
  bne 1b

  ldmia sp!, { r4 - r11, pc }

 0:
  add num_blocks, num_blocks, block_width
  mov texture_offset, texture_offset_base

  cmp num_blocks, #MAX_BLOCKS
  blgt setup_sprites_16bpp_flush

  add texture_offset_base, texture_offset_base, #2048
  and texture_block_ptr, texture_offset, texture_mask

  add texture_block_ptr, texture_page_ptr, texture_block_ptr
  vld1.u32 { texels }, [texture_block_ptr, :128]  

  vst1.u32 { texels }, [block, :128]
  add block, block, #40

  vdup.u8 draw_mask_fb_ptr, block_masks[0]
  vmov.u32 draw_mask_fb_ptr[1], fb_ptr
  pld [fb_ptr]

  vst1.u32 { draw_mask_fb_ptr }, [block, :64]
  subs blocks_remaining, block_width, #2

  add texture_offset, texture_offset, #16
  add fb_ptr, fb_ptr, #16

  vmov.u8 draw_mask_fb_ptr, #0

  add block, block, #24
  beq 2f

 1:
  and texture_block_ptr, texture_offset, texture_mask
  subs blocks_remaining, blocks_remaining, #1

  add texture_block_ptr, texture_page_ptr, texture_block_ptr
  vld1.u32 { texels }, [texture_block_ptr, :128]

  vst1.u32 { texels }, [block, :128]
  add block, block, #40

  vmov.u32 draw_mask_fb_ptr[1], fb_ptr
  pld [fb_ptr]

  vst1.u32 { draw_mask_fb_ptr }, [block, :64]
  
  add texture_offset, texture_offset, #16
  add fb_ptr, fb_ptr, #16

  add block, block, #24
  bne 1b

 2:
  and texture_block_ptr, texture_offset, texture_mask
  add texture_block_ptr, texture_page_ptr, texture_block_ptr

  vld1.u32 { texels }, [texture_block_ptr, :128]
  vdup.u8 draw_mask_fb_ptr, block_masks[4]

  vst1.u32 { texels }, [block, :128]
  add block, block, #40

  vmov.u32 draw_mask_fb_ptr[1], fb_ptr
  vst1.u32 { draw_mask_fb_ptr }, [block, :64]
  
  add block, block, #24
  subs height, height, #1

  add fb_ptr, fb_ptr, fb_ptr_pitch
  strh num_blocks, [psx_gpu, #psx_gpu_num_blocks_offset]

  bne 0b

  ldmia sp!, { r4 - r11, pc }


// 4x version
// FIXME: duplicate code with normal version :(
#undef draw_mask_fb_ptr

function(setup_sprite_16bpp_4x)
  stmdb sp!, { r4 - r11, r14 }
  ldr fb_ptr, [psx_gpu, #psx_gpu_vram_out_ptr_offset]

  ldr v, [sp, #36]
  add fb_ptr, fb_ptr, y, lsl #11

  ldr width, [sp, #40]
  add fb_ptr, fb_ptr, x, lsl #1

  ldr height, [sp, #44]
  and left_offset, u, #0x7

  add texture_offset_base, u, u
  add width_rounded, width, #7

  add texture_offset_base, texture_offset_base, v, lsl #11
  movw left_mask_bits, #0xFFFF
  
  ldrb texture_mask_width, [psx_gpu, #psx_gpu_texture_mask_width_offset]
  add width_rounded, width_rounded, left_offset

  lsl left_offset, #1

  ldrb texture_mask_height, [psx_gpu, #psx_gpu_texture_mask_height_offset]
  sub fb_ptr, fb_ptr, left_offset, lsl #1

  add texture_mask, texture_mask_width, texture_mask_width
  movw right_mask_bits, #0xFFFC

  and right_width, width_rounded, #0x7
  mvn left_mask_bits, left_mask_bits, lsl left_offset

  lsl right_width, #1

  add texture_mask, texture_mask, texture_mask_height, lsl #11
  mov block_width, width_rounded, lsr #3

  mov right_mask_bits, right_mask_bits, lsl right_width
  movw fb_ptr_pitch, #(2048 + 16) * 2

  sub fb_ptr_pitch, fb_ptr_pitch, block_width, lsl #4+1
  vmov block_masks, left_mask_bits, right_mask_bits

  ldrh num_blocks, [psx_gpu, #psx_gpu_num_blocks_offset]
  add block, psx_gpu, #psx_gpu_blocks_offset

  bic texture_offset_base, texture_offset_base, #0xF
  cmp block_width, #1

  ldr texture_page_ptr, [psx_gpu, #psx_gpu_texture_page_ptr_offset]
  add block, block, num_blocks, lsl #6

  lsl block_width, #2
  bne 0f

  vext.32 block_masks_shifted, block_masks, block_masks, #1
  vorr.u32 block_masks, block_masks, block_masks_shifted
  vdup.u8 draw_mask_fb_ptr_a, block_masks[0]
  vdup.u8 draw_mask_fb_ptr_b, block_masks[1]

 1:
  add num_blocks, num_blocks, block_width
  cmp num_blocks, #MAX_BLOCKS
  blgt setup_sprites_16bpp_flush

  and texture_block_ptr, texture_offset_base, texture_mask
  subs height, height, #1

  add texture_block_ptr, texture_page_ptr, texture_block_ptr
  vld1.u32 { texels }, [texture_block_ptr, :128]

  do_texture_block_16bpp_4x(fb_ptr2, draw_mask_fb_ptr_a, draw_mask_fb_ptr_b)

  add texture_offset_base, texture_offset_base, #2048
  add fb_ptr, fb_ptr, #2048*2
  strh num_blocks, [psx_gpu, #psx_gpu_num_blocks_offset]
  bne 1b

  ldmia sp!, { r4 - r11, pc }

 0:
  add num_blocks, num_blocks, block_width
  mov texture_offset, texture_offset_base

  vdup.u8 draw_mask_fb_ptr_a, block_masks[0] // left_mask_bits
  vdup.u8 draw_mask_fb_ptr_b, block_masks[1]

  cmp num_blocks, #MAX_BLOCKS
  blgt setup_sprites_16bpp_flush

  add texture_offset_base, texture_offset_base, #2048
  and texture_block_ptr, texture_offset, texture_mask

  add texture_block_ptr, texture_page_ptr, texture_block_ptr
  vld1.u32 { texels }, [texture_block_ptr, :128]

  do_texture_block_16bpp_4x(fb_ptr2, draw_mask_fb_ptr_a, draw_mask_fb_ptr_b)

  subs blocks_remaining, block_width, #2*4
  add texture_offset, texture_offset, #16

  vmov.u8 draw_mask_fb_ptr_a, #0
  vmov.u8 draw_mask_fb_ptr_b, #0

  add fb_ptr, fb_ptr, #16*2
  beq 2f

 1:
  and texture_block_ptr, texture_offset, texture_mask
  subs blocks_remaining, blocks_remaining, #4

  add texture_block_ptr, texture_page_ptr, texture_block_ptr
  vld1.u32 { texels }, [texture_block_ptr, :128]

  do_texture_block_16bpp_4x(fb_ptr2, draw_mask_fb_ptr_a, draw_mask_fb_ptr_b)
  add texture_offset, texture_offset, #16

  add fb_ptr, fb_ptr, #16*2
  bgt 1b

 2:
  vdup.u8 draw_mask_fb_ptr_a, block_masks[4] // right_mask_bits
  vdup.u8 draw_mask_fb_ptr_b, block_masks[5]

  and texture_block_ptr, texture_offset, texture_mask
  add texture_block_ptr, texture_page_ptr, texture_block_ptr

  vld1.u32 { texels }, [texture_block_ptr, :128]

  do_texture_block_16bpp_4x(fb_ptr2, draw_mask_fb_ptr_a, draw_mask_fb_ptr_b)
  subs height, height, #1

  add fb_ptr, fb_ptr, fb_ptr_pitch
  strh num_blocks, [psx_gpu, #psx_gpu_num_blocks_offset]

  bne 0b

  ldmia sp!, { r4 - r11, pc }


#undef width
#undef right_width
#undef right_mask_bits
#undef color
#undef height
#undef blocks_remaining
#undef colors
#undef right_mask
#undef test_mask
#undef draw_mask

#define psx_gpu                                           r0
#define x                                                 r1
#define y                                                 r2
#define width                                             r3
#define right_width                                       r5
#define right_mask_bits                                   r6
#define fb_ptr                                            r7
#define color                                             r8
#define height                                            r9
#define fb_ptr_pitch                                      r12

// referenced by setup_sprites_16bpp_flush
#define num_blocks                                        r4
#define block                                             r5
#define block_width                                       r11

#define color_r                                           r1
#define color_g                                           r2
#define color_b                                           r8
#define blocks_remaining                                  r6

#define colors                                            q0
#define right_mask                                        q1
#define test_mask                                         q2
#define draw_mask                                         q2
#define draw_mask_bits_fb_ptr                             d6


.align 3

function(setup_sprite_untextured)
  ldrh r12, [psx_gpu, #psx_gpu_render_state_offset]
  tst r12, #(RENDER_STATE_MASK_EVALUATE | RENDER_FLAGS_MODULATE_TEXELS         \
    | RENDER_FLAGS_BLEND)
  ldrbeq r12, [psx_gpu, #psx_gpu_render_mode_offset]
  tsteq r12, #RENDER_INTERLACE_ENABLED
  beq setup_sprite_untextured_simple

  stmdb sp!, { r4 - r11, r14 }

  ldr width, [sp, #40]
  ldr fb_ptr, [psx_gpu, #psx_gpu_vram_out_ptr_offset]

  ldr height, [sp, #44]
  add fb_ptr, fb_ptr, y, lsl #11

  add fb_ptr, fb_ptr, x, lsl #1
  sub right_width, width, #1

  ldr color, [sp, #48]
  and right_width, #7

  add block_width, width, #7
  add right_width, #1

  lsr block_width, #3
  mov right_mask_bits, #0xff

  sub fb_ptr_pitch, block_width, #1
  lsl right_mask_bits, right_width

  lsl fb_ptr_pitch, #3+1
  ubfx color_r, color, #3, #5

  rsb fb_ptr_pitch, #1024*2
  ubfx color_g, color, #11, #5

  vld1.u32 { test_mask }, [psx_gpu, :128]
  ubfx color_b, color, #19, #5

  vdup.u16 right_mask, right_mask_bits
  orr color, color_r, color_b, lsl #10

  ldrh num_blocks, [psx_gpu, #psx_gpu_num_blocks_offset]
  orr color, color, color_g, lsl #5

  vtst.u16 right_mask, right_mask, test_mask
  add block, psx_gpu, #psx_gpu_blocks_offset

  vdup.u16 colors, color
  add block, block, num_blocks, lsl #6


setup_sprite_untextured_height_loop:
  add num_blocks, block_width
  sub blocks_remaining, block_width, #1

  cmp num_blocks, #MAX_BLOCKS
  blgt setup_sprites_16bpp_flush

  cmp blocks_remaining, #0
  ble 1f

  vmov.u8 draw_mask, #0 /* zero_mask */
  vmov.u8 draw_mask_bits_fb_ptr, #0

 0:
  vst1.u32 { draw_mask }, [block, :128]!
  subs blocks_remaining, #1

  vst1.u32 { colors }, [block, :128]
  add block, block, #24

  vmov.u32 draw_mask_bits_fb_ptr[1], fb_ptr
  vst1.u32 { draw_mask_bits_fb_ptr }, [block, :64]
  
  add block, block, #24
  add fb_ptr, #8*2
  bgt 0b

 1:
  vst1.u32 { right_mask }, [block, :128]!
  subs height, #1

  vst1.u32 { colors }, [block, :128]
  add block, block, #24

  vmov.u32 draw_mask_bits_fb_ptr[1], fb_ptr
  vst1.u32 { draw_mask_bits_fb_ptr }, [block, :64]
  
  add block, block, #24
  add fb_ptr, fb_ptr_pitch

  strh num_blocks, [psx_gpu, #psx_gpu_num_blocks_offset]
  bgt setup_sprite_untextured_height_loop

  ldmia sp!, { r4 - r11, pc }



#undef texture_page_ptr
#undef vram_ptr
#undef dirty_textures_mask
#undef current_texture_mask

#define psx_gpu                                           r0
#define current_texture_page                              r1
#define texture_page_ptr                                  r2
#define vram_ptr_a                                        r3
#define current_texture_page_x                            r12
#define current_texture_page_y                            r4
#define dirty_textures_mask                               r5
#define tile_y                                            r6
#define tile_x                                            r7
#define sub_y                                             r8
#define current_texture_mask                              r9
#define c_4096                                            r10
#define vram_ptr_b                                        r11

#define texel_block_a                                     d0
#define texel_block_b                                     d1
#define texel_block_expanded_a                            q1
#define texel_block_expanded_b                            q2
#define texel_block_expanded_ab                           q2
#define texel_block_expanded_c                            q3
#define texel_block_expanded_d                            q4
#define texel_block_expanded_cd                           q3

function(update_texture_4bpp_cache)
  stmdb sp!, { r4 - r11, r14 }
  vpush { q0 - q3 }

  ldrb current_texture_page, [psx_gpu, #psx_gpu_current_texture_page_offset]

  ldr texture_page_ptr, [psx_gpu, #psx_gpu_texture_page_base_offset]
  ldr vram_ptr_a, [psx_gpu, #psx_gpu_vram_ptr_offset]

  and current_texture_page_x, current_texture_page, #0xF
  ldr current_texture_mask, [psx_gpu, #psx_gpu_current_texture_mask_offset]

  mov current_texture_page_y, current_texture_page, lsr #4
  ldr dirty_textures_mask, [psx_gpu, #psx_gpu_dirty_textures_4bpp_mask_offset]

  add vram_ptr_a, vram_ptr_a, current_texture_page_y, lsl #19
  mov tile_y, #16

  add vram_ptr_a, vram_ptr_a, current_texture_page_x, lsl #7  
  bic dirty_textures_mask, current_texture_mask
  
  mov tile_x, #16
  str dirty_textures_mask, [psx_gpu, #psx_gpu_dirty_textures_4bpp_mask_offset]

  mov sub_y, #8
  movw c_4096, #4096

  add vram_ptr_b, vram_ptr_a, #2048

 0:
  vld1.u32 { texel_block_a }, [vram_ptr_a, :64], c_4096
  vld1.u32 { texel_block_b }, [vram_ptr_b, :64], c_4096

  vmovl.u8 texel_block_expanded_a, texel_block_a
  vshll.u8 texel_block_expanded_b, texel_block_a, #4
  vmovl.u8 texel_block_expanded_c, texel_block_b
  vshll.u8 texel_block_expanded_d, texel_block_b, #4

  vbic.u16 texel_block_expanded_a, #0x00F0
  vbic.u16 texel_block_expanded_b, #0x00F0
  vbic.u16 texel_block_expanded_c, #0x00F0
  vbic.u16 texel_block_expanded_d, #0x00F0

  vorr.u16 texel_block_expanded_ab, texel_block_expanded_a,                    \
   texel_block_expanded_b
  vorr.u16 texel_block_expanded_cd, texel_block_expanded_c,                    \
   texel_block_expanded_d

  vst1.u32 { texel_block_expanded_ab, texel_block_expanded_cd },               \
   [texture_page_ptr, :256]!

  subs sub_y, sub_y, #1
  bne 0b

  mov sub_y, #8
  add vram_ptr_a, vram_ptr_a, #8
  add vram_ptr_b, vram_ptr_b, #8

  sub vram_ptr_a, vram_ptr_a, #(16 * 2048)
  sub vram_ptr_b, vram_ptr_b, #(16 * 2048)

  subs tile_x, tile_x, #1
  bne 0b

  mov tile_x, #16
  add vram_ptr_a, vram_ptr_a, #(16 * 2048)
  add vram_ptr_b, vram_ptr_b, #(16 * 2048)

  sub vram_ptr_a, vram_ptr_a, #(8 * 16)
  sub vram_ptr_b, vram_ptr_b, #(8 * 16)

  subs tile_y, tile_y, #1
  bne 0b

  vpop { q0 - q3 }
  ldmia sp!, { r4 - r11, pc }


#undef current_texture_page

#define psx_gpu                                           r0
#define texture_page                                      r1
#define texture_page_ptr                                  r2
#define vram_ptr_a                                        r3
#define texture_page_x                                    r12
#define texture_page_y                                    r4
#define current_texture_page                              r5
#define tile_y                                            r6
#define tile_x                                            r7
#define sub_y                                             r8
#define c_4096                                            r10
#define vram_ptr_b                                        r11


#undef texels_a
#undef texels_b

#define texels_a                                          q0
#define texels_b                                          q1
#define texels_c                                          q2
#define texels_d                                          q3


function(update_texture_8bpp_cache_slice)
  stmdb sp!, { r4 - r11, r14 }
  vpush { q0 - q3 }

  ldrb current_texture_page, [psx_gpu, #psx_gpu_current_texture_page_offset]
  ldr vram_ptr_a, [psx_gpu, #psx_gpu_vram_ptr_offset]

  ldr texture_page_ptr, [psx_gpu, #psx_gpu_texture_page_base_offset]
  mov tile_y, #16

  and texture_page_x, texture_page, #0xF
  mov texture_page_y, texture_page, lsr #4

  add vram_ptr_a, vram_ptr_a, texture_page_x, lsl #7  
  mov tile_x, #8

  add vram_ptr_a, vram_ptr_a, texture_page_y, lsl #19
  eor current_texture_page, current_texture_page, texture_page

  ands current_texture_page, current_texture_page, #0x1
  mov sub_y, #4

  addne texture_page_ptr, texture_page_ptr, #(8 * 16 * 16)
  movw c_4096, #4096

  add vram_ptr_b, vram_ptr_a, #2048

 0:
  vld1.u32 { texels_a }, [vram_ptr_a, :128], c_4096
  vld1.u32 { texels_b }, [vram_ptr_b, :128], c_4096
  vld1.u32 { texels_c }, [vram_ptr_a, :128], c_4096
  vld1.u32 { texels_d }, [vram_ptr_b, :128], c_4096

  vst1.u32 { texels_a, texels_b }, [texture_page_ptr, :256]!
  vst1.u32 { texels_c, texels_d }, [texture_page_ptr, :256]!

  subs sub_y, sub_y, #1
  bne 0b

  mov sub_y, #4

  add vram_ptr_a, vram_ptr_a, #16
  add vram_ptr_b, vram_ptr_b, #16

  sub vram_ptr_a, vram_ptr_a, #(16 * 2048)
  sub vram_ptr_b, vram_ptr_b, #(16 * 2048)

  subs tile_x, tile_x, #1
  bne 0b

  mov tile_x, #8

  add vram_ptr_a, vram_ptr_a, #(16 * 2048)
  add vram_ptr_b, vram_ptr_b, #(16 * 2048)

  sub vram_ptr_a, vram_ptr_a, #(8 * 16)
  sub vram_ptr_b, vram_ptr_b, #(8 * 16)

  subs tile_y, tile_y, #1
  add texture_page_ptr, texture_page_ptr, #(8 * 16 * 16)

  bne 0b

  vpop { q0 - q3 }
  ldmia sp!, { r4 - r11, pc }


/* void scale2x_tiles8(void *dst, const void *src, int w8, int h) */
function(scale2x_tiles8)
  push { r4, r14 }

  mov r4, r1
  add r12, r0, #1024*2
  mov r14, r2

0:
  vld1.u16 { q0 }, [r1, :128]!
  vld1.u16 { q2 }, [r1, :128]!
  vmov q1, q0
  vmov q3, q2
  vzip.16 q0, q1
  vzip.16 q2, q3
  subs r14, #2
  vst1.u16 { q0, q1 }, [r0, :128]!
  vst1.u16 { q0, q1 }, [r12, :128]!
  blt 1f
  vst1.u16 { q2, q3 }, [r0, :128]!
  vst1.u16 { q2, q3 }, [r12, :128]!
  bgt 0b
1:
  subs r3, #1
  mov r14, r2
  add r0, #1024*2*2
  add r4, #1024*2
  sub r0, r0, r2, lsl #4+1
  mov r1, r4
  add r12, r0, #1024*2
  bgt 0b
  nop

  pop { r4, pc }

// vim:filetype=armasm