aboutsummaryrefslogtreecommitdiffstats
path: root/crypto/ec/asm/ecp_nistz256-sparcv9.pl
blob: 0a4def6e2bf62d4d68eb6b961d0cd646178845cc (plain) (blame)
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
#! /usr/bin/env perl
# Copyright 2015-2018 The OpenSSL Project Authors. All Rights Reserved.
#
# Licensed under the OpenSSL license (the "License").  You may not use
# this file except in compliance with the License.  You can obtain a copy
# in the file LICENSE in the source distribution or at
# https://www.openssl.org/source/license.html


# ====================================================================
# Written by Andy Polyakov <appro@openssl.org> for the OpenSSL
# project. The module is, however, dual licensed under OpenSSL and
# CRYPTOGAMS licenses depending on where you obtain it. For further
# details see http://www.openssl.org/~appro/cryptogams/.
# ====================================================================
#
# ECP_NISTZ256 module for SPARCv9.
#
# February 2015.
#
# Original ECP_NISTZ256 submission targeting x86_64 is detailed in
# http://eprint.iacr.org/2013/816. In the process of adaptation
# original .c module was made 32-bit savvy in order to make this
# implementation possible.
#
#			with/without -DECP_NISTZ256_ASM
# UltraSPARC III	+12-18%
# SPARC T4		+99-550% (+66-150% on 32-bit Solaris)
#
# Ranges denote minimum and maximum improvement coefficients depending
# on benchmark. Lower coefficients are for ECDSA sign, server-side
# operation. Keep in mind that +200% means 3x improvement.

$output = pop;
open STDOUT,">$output";

$code.=<<___;
#include "sparc_arch.h"

#define LOCALS	(STACK_BIAS+STACK_FRAME)
#ifdef	__arch64__
.register	%g2,#scratch
.register	%g3,#scratch
# define STACK64_FRAME	STACK_FRAME
# define LOCALS64	LOCALS
#else
# define STACK64_FRAME	(2047+192)
# define LOCALS64	STACK64_FRAME
#endif

.section	".text",#alloc,#execinstr
___
########################################################################
# Convert ecp_nistz256_table.c to layout expected by ecp_nistz_gather_w7
#
$0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
open TABLE,"<ecp_nistz256_table.c"		or
open TABLE,"<${dir}../ecp_nistz256_table.c"	or
die "failed to open ecp_nistz256_table.c:",$!;

use integer;

foreach(<TABLE>) {
	s/TOBN\(\s*(0x[0-9a-f]+),\s*(0x[0-9a-f]+)\s*\)/push @arr,hex($2),hex($1)/geo;
}
close TABLE;

# See ecp_nistz256_table.c for explanation for why it's 64*16*37.
# 64*16*37-1 is because $#arr returns last valid index or @arr, not
# amount of elements.
die "insane number of elements" if ($#arr != 64*16*37-1);

$code.=<<___;
.globl	ecp_nistz256_precomputed
.align	4096
ecp_nistz256_precomputed:
___
########################################################################
# this conversion smashes P256_POINT_AFFINE by individual bytes with
# 64 byte interval, similar to
#	1111222233334444
#	1234123412341234
for(1..37) {
	@tbl = splice(@arr,0,64*16);
	for($i=0;$i<64;$i++) {
		undef @line;
		for($j=0;$j<64;$j++) {
			push @line,(@tbl[$j*16+$i/4]>>(($i%4)*8))&0xff;
		}
		$code.=".byte\t";
		$code.=join(',',map { sprintf "0x%02x",$_} @line);
		$code.="\n";
	}
}

{{{
my ($rp,$ap,$bp)=map("%i$_",(0..2));
my @acc=map("%l$_",(0..7));
my ($t0,$t1,$t2,$t3,$t4,$t5,$t6,$t7)=(map("%o$_",(0..5)),"%g4","%g5");
my ($bi,$a0,$mask,$carry)=(map("%i$_",(3..5)),"%g1");
my ($rp_real,$ap_real)=("%g2","%g3");

$code.=<<___;
.type	ecp_nistz256_precomputed,#object
.size	ecp_nistz256_precomputed,.-ecp_nistz256_precomputed
.align	64
.LRR:	! 2^512 mod P precomputed for NIST P256 polynomial
.long	0x00000003, 0x00000000, 0xffffffff, 0xfffffffb
.long	0xfffffffe, 0xffffffff, 0xfffffffd, 0x00000004
.Lone:
.long	1,0,0,0,0,0,0,0
.asciz	"ECP_NISTZ256 for SPARCv9, CRYPTOGAMS by <appro\@openssl.org>"

! void	ecp_nistz256_to_mont(BN_ULONG %i0[8],const BN_ULONG %i1[8]);
.globl	ecp_nistz256_to_mont
.align	64
ecp_nistz256_to_mont:
	save	%sp,-STACK_FRAME,%sp
	nop
1:	call	.+8
	add	%o7,.LRR-1b,$bp
	call	__ecp_nistz256_mul_mont
	nop
	ret
	restore
.type	ecp_nistz256_to_mont,#function
.size	ecp_nistz256_to_mont,.-ecp_nistz256_to_mont

! void	ecp_nistz256_from_mont(BN_ULONG %i0[8],const BN_ULONG %i1[8]);
.globl	ecp_nistz256_from_mont
.align	32
ecp_nistz256_from_mont:
	save	%sp,-STACK_FRAME,%sp
	nop
1:	call	.+8
	add	%o7,.Lone-1b,$bp
	call	__ecp_nistz256_mul_mont
	nop
	ret
	restore
.type	ecp_nistz256_from_mont,#function
.size	ecp_nistz256_from_mont,.-ecp_nistz256_from_mont

! void	ecp_nistz256_mul_mont(BN_ULONG %i0[8],const BN_ULONG %i1[8],
!					      const BN_ULONG %i2[8]);
.globl	ecp_nistz256_mul_mont
.align	32
ecp_nistz256_mul_mont:
	save	%sp,-STACK_FRAME,%sp
	nop
	call	__ecp_nistz256_mul_mont
	nop
	ret
	restore
.type	ecp_nistz256_mul_mont,#function
.size	ecp_nistz256_mul_mont,.-ecp_nistz256_mul_mont

! void	ecp_nistz256_sqr_mont(BN_ULONG %i0[8],const BN_ULONG %i2[8]);
.globl	ecp_nistz256_sqr_mont
.align	32
ecp_nistz256_sqr_mont:
	save	%sp,-STACK_FRAME,%sp
	mov	$ap,$bp
	call	__ecp_nistz256_mul_mont
	nop
	ret
	restore
.type	ecp_nistz256_sqr_mont,#function
.size	ecp_nistz256_sqr_mont,.-ecp_nistz256_sqr_mont
___

########################################################################
# Special thing to keep in mind is that $t0-$t7 hold 64-bit values,
# while all others are meant to keep 32. "Meant to" means that additions
# to @acc[0-7] do "contaminate" upper bits, but they are cleared before
# they can affect outcome (follow 'and' with $mask). Also keep in mind
# that addition with carry is addition with 32-bit carry, even though
# CPU is 64-bit. [Addition with 64-bit carry was introduced in T3, see
# below for VIS3 code paths.]

$code.=<<___;
.align	32
__ecp_nistz256_mul_mont:
	ld	[$bp+0],$bi		! b[0]
	mov	-1,$mask
	ld	[$ap+0],$a0
	srl	$mask,0,$mask		! 0xffffffff
	ld	[$ap+4],$t1
	ld	[$ap+8],$t2
	ld	[$ap+12],$t3
	ld	[$ap+16],$t4
	ld	[$ap+20],$t5
	ld	[$ap+24],$t6
	ld	[$ap+28],$t7
	mulx	$a0,$bi,$t0		! a[0-7]*b[0], 64-bit results
	mulx	$t1,$bi,$t1
	mulx	$t2,$bi,$t2
	mulx	$t3,$bi,$t3
	mulx	$t4,$bi,$t4
	mulx	$t5,$bi,$t5
	mulx	$t6,$bi,$t6
	mulx	$t7,$bi,$t7
	srlx	$t0,32,@acc[1]		! extract high parts
	srlx	$t1,32,@acc[2]
	srlx	$t2,32,@acc[3]
	srlx	$t3,32,@acc[4]
	srlx	$t4,32,@acc[5]
	srlx	$t5,32,@acc[6]
	srlx	$t6,32,@acc[7]
	srlx	$t7,32,@acc[0]		! "@acc[8]"
	mov	0,$carry
___
for($i=1;$i<8;$i++) {
$code.=<<___;
	addcc	@acc[1],$t1,@acc[1]	! accumulate high parts
	ld	[$bp+4*$i],$bi		! b[$i]
	ld	[$ap+4],$t1		! re-load a[1-7]
	addccc	@acc[2],$t2,@acc[2]
	addccc	@acc[3],$t3,@acc[3]
	ld	[$ap+8],$t2
	ld	[$ap+12],$t3
	addccc	@acc[4],$t4,@acc[4]
	addccc	@acc[5],$t5,@acc[5]
	ld	[$ap+16],$t4
	ld	[$ap+20],$t5
	addccc	@acc[6],$t6,@acc[6]
	addccc	@acc[7],$t7,@acc[7]
	ld	[$ap+24],$t6
	ld	[$ap+28],$t7
	addccc	@acc[0],$carry,@acc[0]	! "@acc[8]"
	addc	%g0,%g0,$carry
___
	# Reduction iteration is normally performed by accumulating
	# result of multiplication of modulus by "magic" digit [and
	# omitting least significant word, which is guaranteed to
	# be 0], but thanks to special form of modulus and "magic"
	# digit being equal to least significant word, it can be
	# performed with additions and subtractions alone. Indeed:
	#
	#        ffff.0001.0000.0000.0000.ffff.ffff.ffff
	# *                                         abcd
	# + xxxx.xxxx.xxxx.xxxx.xxxx.xxxx.xxxx.xxxx.abcd
	#
	# Now observing that ff..ff*x = (2^n-1)*x = 2^n*x-x, we
	# rewrite above as:
	#
	#   xxxx.xxxx.xxxx.xxxx.xxxx.xxxx.xxxx.xxxx.abcd
	# + abcd.0000.abcd.0000.0000.abcd.0000.0000.0000
	# -      abcd.0000.0000.0000.0000.0000.0000.abcd
	#
	# or marking redundant operations:
	#
	#   xxxx.xxxx.xxxx.xxxx.xxxx.xxxx.xxxx.xxxx.----
	# + abcd.0000.abcd.0000.0000.abcd.----.----.----
	# -      abcd.----.----.----.----.----.----.----

$code.=<<___;
	! multiplication-less reduction
	addcc	@acc[3],$t0,@acc[3]	! r[3]+=r[0]
	addccc	@acc[4],%g0,@acc[4]	! r[4]+=0
	 and	@acc[1],$mask,@acc[1]
	 and	@acc[2],$mask,@acc[2]
	addccc	@acc[5],%g0,@acc[5]	! r[5]+=0
	addccc	@acc[6],$t0,@acc[6]	! r[6]+=r[0]
	 and	@acc[3],$mask,@acc[3]
	 and	@acc[4],$mask,@acc[4]
	addccc	@acc[7],%g0,@acc[7]	! r[7]+=0
	addccc	@acc[0],$t0,@acc[0]	! r[8]+=r[0]	"@acc[8]"
	 and	@acc[5],$mask,@acc[5]
	 and	@acc[6],$mask,@acc[6]
	addc	$carry,%g0,$carry	! top-most carry
	subcc	@acc[7],$t0,@acc[7]	! r[7]-=r[0]
	subccc	@acc[0],%g0,@acc[0]	! r[8]-=0	"@acc[8]"
	subc	$carry,%g0,$carry	! top-most carry
	 and	@acc[7],$mask,@acc[7]
	 and	@acc[0],$mask,@acc[0]	! "@acc[8]"
___
	push(@acc,shift(@acc));		# rotate registers to "omit" acc[0]
$code.=<<___;
	mulx	$a0,$bi,$t0		! a[0-7]*b[$i], 64-bit results
	mulx	$t1,$bi,$t1
	mulx	$t2,$bi,$t2
	mulx	$t3,$bi,$t3
	mulx	$t4,$bi,$t4
	mulx	$t5,$bi,$t5
	mulx	$t6,$bi,$t6
	mulx	$t7,$bi,$t7
	add	@acc[0],$t0,$t0		! accumulate low parts, can't overflow
	add	@acc[1],$t1,$t1
	srlx	$t0,32,@acc[1]		! extract high parts
	add	@acc[2],$t2,$t2
	srlx	$t1,32,@acc[2]
	add	@acc[3],$t3,$t3
	srlx	$t2,32,@acc[3]
	add	@acc[4],$t4,$t4
	srlx	$t3,32,@acc[4]
	add	@acc[5],$t5,$t5
	srlx	$t4,32,@acc[5]
	add	@acc[6],$t6,$t6
	srlx	$t5,32,@acc[6]
	add	@acc[7],$t7,$t7
	srlx	$t6,32,@acc[7]
	srlx	$t7,32,@acc[0]		! "@acc[8]"
___
}
$code.=<<___;
	addcc	@acc[1],$t1,@acc[1]	! accumulate high parts
	addccc	@acc[2],$t2,@acc[2]
	addccc	@acc[3],$t3,@acc[3]
	addccc	@acc[4],$t4,@acc[4]
	addccc	@acc[5],$t5,@acc[5]
	addccc	@acc[6],$t6,@acc[6]
	addccc	@acc[7],$t7,@acc[7]
	addccc	@acc[0],$carry,@acc[0]	! "@acc[8]"
	addc	%g0,%g0,$carry

	addcc	@acc[3],$t0,@acc[3]	! multiplication-less reduction
	addccc	@acc[4],%g0,@acc[4]
	addccc	@acc[5],%g0,@acc[5]
	addccc	@acc[6],$t0,@acc[6]
	addccc	@acc[7],%g0,@acc[7]
	addccc	@acc[0],$t0,@acc[0]	! "@acc[8]"
	addc	$carry,%g0,$carry
	subcc	@acc[7],$t0,@acc[7]
	subccc	@acc[0],%g0,@acc[0]	! "@acc[8]"
	subc	$carry,%g0,$carry	! top-most carry
___
	push(@acc,shift(@acc));		# rotate registers to omit acc[0]
$code.=<<___;
	! Final step is "if result > mod, subtract mod", but we do it
	! "other way around", namely subtract modulus from result
	! and if it borrowed, add modulus back.

	subcc	@acc[0],-1,@acc[0]	! subtract modulus
	subccc	@acc[1],-1,@acc[1]
	subccc	@acc[2],-1,@acc[2]
	subccc	@acc[3],0,@acc[3]
	subccc	@acc[4],0,@acc[4]
	subccc	@acc[5],0,@acc[5]
	subccc	@acc[6],1,@acc[6]
	subccc	@acc[7],-1,@acc[7]
	subc	$carry,0,$carry		! broadcast borrow bit

	! Note that because mod has special form, i.e. consists of
	! 0xffffffff, 1 and 0s, we can conditionally synthesize it by
	! using value of broadcasted borrow and the borrow bit itself.
	! To minimize dependency chain we first broadcast and then
	! extract the bit by negating (follow $bi).

	addcc	@acc[0],$carry,@acc[0]	! add modulus or zero
	addccc	@acc[1],$carry,@acc[1]
	neg	$carry,$bi
	st	@acc[0],[$rp]
	addccc	@acc[2],$carry,@acc[2]
	st	@acc[1],[$rp+4]
	addccc	@acc[3],0,@acc[3]
	st	@acc[2],[$rp+8]
	addccc	@acc[4],0,@acc[4]
	st	@acc[3],[$rp+12]
	addccc	@acc[5],0,@acc[5]
	st	@acc[4],[$rp+16]
	addccc	@acc[6],$bi,@acc[6]
	st	@acc[5],[$rp+20]
	addc	@acc[7],$carry,@acc[7]
	st	@acc[6],[$rp+24]
	retl
	st	@acc[7],[$rp+28]
.type	__ecp_nistz256_mul_mont,#function
.size	__ecp_nistz256_mul_mont,.-__ecp_nistz256_mul_mont

! void	ecp_nistz256_add(BN_ULONG %i0[8],const BN_ULONG %i1[8],
!					 const BN_ULONG %i2[8]);
.globl	ecp_nistz256_add
.align	32
ecp_nistz256_add:
	save	%sp,-STACK_FRAME,%sp
	ld	[$ap],@acc[0]
	ld	[$ap+4],@acc[1]
	ld	[$ap+8],@acc[2]
	ld	[$ap+12],@acc[3]
	ld	[$ap+16],@acc[4]
	ld	[$ap+20],@acc[5]
	ld	[$ap+24],@acc[6]
	call	__ecp_nistz256_add
	ld	[$ap+28],@acc[7]
	ret
	restore
.type	ecp_nistz256_add,#function
.size	ecp_nistz256_add,.-ecp_nistz256_add

.align	32
__ecp_nistz256_add:
	ld	[$bp+0],$t0		! b[0]
	ld	[$bp+4],$t1
	ld	[$bp+8],$t2
	ld	[$bp+12],$t3
	addcc	@acc[0],$t0,@acc[0]
	ld	[$bp+16],$t4
	ld	[$bp+20],$t5
	addccc	@acc[1],$t1,@acc[1]
	ld	[$bp+24],$t6
	ld	[$bp+28],$t7
	addccc	@acc[2],$t2,@acc[2]
	addccc	@acc[3],$t3,@acc[3]
	addccc	@acc[4],$t4,@acc[4]
	addccc	@acc[5],$t5,@acc[5]
	addccc	@acc[6],$t6,@acc[6]
	addccc	@acc[7],$t7,@acc[7]
	addc	%g0,%g0,$carry

.Lreduce_by_sub:

	! if a+b >= modulus, subtract modulus.
	!
	! But since comparison implies subtraction, we subtract
	! modulus and then add it back if subtraction borrowed.

	subcc	@acc[0],-1,@acc[0]
	subccc	@acc[1],-1,@acc[1]
	subccc	@acc[2],-1,@acc[2]
	subccc	@acc[3], 0,@acc[3]
	subccc	@acc[4], 0,@acc[4]
	subccc	@acc[5], 0,@acc[5]
	subccc	@acc[6], 1,@acc[6]
	subccc	@acc[7],-1,@acc[7]
	subc	$carry,0,$carry

	! Note that because mod has special form, i.e. consists of
	! 0xffffffff, 1 and 0s, we can conditionally synthesize it by
	! using value of borrow and its negative.

	addcc	@acc[0],$carry,@acc[0]	! add synthesized modulus
	addccc	@acc[1],$carry,@acc[1]
	neg	$carry,$bi
	st	@acc[0],[$rp]
	addccc	@acc[2],$carry,@acc[2]
	st	@acc[1],[$rp+4]
	addccc	@acc[3],0,@acc[3]
	st	@acc[2],[$rp+8]
	addccc	@acc[4],0,@acc[4]
	st	@acc[3],[$rp+12]
	addccc	@acc[5],0,@acc[5]
	st	@acc[4],[$rp+16]
	addccc	@acc[6],$bi,@acc[6]
	st	@acc[5],[$rp+20]
	addc	@acc[7],$carry,@acc[7]
	st	@acc[6],[$rp+24]
	retl
	st	@acc[7],[$rp+28]
.type	__ecp_nistz256_add,#function
.size	__ecp_nistz256_add,.-__ecp_nistz256_add

! void	ecp_nistz256_mul_by_2(BN_ULONG %i0[8],const BN_ULONG %i1[8]);
.globl	ecp_nistz256_mul_by_2
.align	32
ecp_nistz256_mul_by_2:
	save	%sp,-STACK_FRAME,%sp
	ld	[$ap],@acc[0]
	ld	[$ap+4],@acc[1]
	ld	[$ap+8],@acc[2]
	ld	[$ap+12],@acc[3]
	ld	[$ap+16],@acc[4]
	ld	[$ap+20],@acc[5]
	ld	[$ap+24],@acc[6]
	call	__ecp_nistz256_mul_by_2
	ld	[$ap+28],@acc[7]
	ret
	restore
.type	ecp_nistz256_mul_by_2,#function
.size	ecp_nistz256_mul_by_2,.-ecp_nistz256_mul_by_2

.align	32
__ecp_nistz256_mul_by_2:
	addcc	@acc[0],@acc[0],@acc[0]	! a+a=2*a
	addccc	@acc[1],@acc[1],@acc[1]
	addccc	@acc[2],@acc[2],@acc[2]
	addccc	@acc[3],@acc[3],@acc[3]
	addccc	@acc[4],@acc[4],@acc[4]
	addccc	@acc[5],@acc[5],@acc[5]
	addccc	@acc[6],@acc[6],@acc[6]
	addccc	@acc[7],@acc[7],@acc[7]
	b	.Lreduce_by_sub
	addc	%g0,%g0,$carry
.type	__ecp_nistz256_mul_by_2,#function
.size	__ecp_nistz256_mul_by_2,.-__ecp_nistz256_mul_by_2

! void	ecp_nistz256_mul_by_3(BN_ULONG %i0[8],const BN_ULONG %i1[8]);
.globl	ecp_nistz256_mul_by_3
.align	32
ecp_nistz256_mul_by_3:
	save	%sp,-STACK_FRAME,%sp
	ld	[$ap],@acc[0]
	ld	[$ap+4],@acc[1]
	ld	[$ap+8],@acc[2]
	ld	[$ap+12],@acc[3]
	ld	[$ap+16],@acc[4]
	ld	[$ap+20],@acc[5]
	ld	[$ap+24],@acc[6]
	call	__ecp_nistz256_mul_by_3
	ld	[$ap+28],@acc[7]
	ret
	restore
.type	ecp_nistz256_mul_by_3,#function
.size	ecp_nistz256_mul_by_3,.-ecp_nistz256_mul_by_3

.align	32
__ecp_nistz256_mul_by_3:
	addcc	@acc[0],@acc[0],$t0	! a+a=2*a
	addccc	@acc[1],@acc[1],$t1
	addccc	@acc[2],@acc[2],$t2
	addccc	@acc[3],@acc[3],$t3
	addccc	@acc[4],@acc[4],$t4
	addccc	@acc[5],@acc[5],$t5
	addccc	@acc[6],@acc[6],$t6
	addccc	@acc[7],@acc[7],$t7
	addc	%g0,%g0,$carry

	subcc	$t0,-1,$t0		! .Lreduce_by_sub but without stores
	subccc	$t1,-1,$t1
	subccc	$t2,-1,$t2
	subccc	$t3, 0,$t3
	subccc	$t4, 0,$t4
	subccc	$t5, 0,$t5
	subccc	$t6, 1,$t6
	subccc	$t7,-1,$t7
	subc	$carry,0,$carry

	addcc	$t0,$carry,$t0		! add synthesized modulus
	addccc	$t1,$carry,$t1
	neg	$carry,$bi
	addccc	$t2,$carry,$t2
	addccc	$t3,0,$t3
	addccc	$t4,0,$t4
	addccc	$t5,0,$t5
	addccc	$t6,$bi,$t6
	addc	$t7,$carry,$t7

	addcc	$t0,@acc[0],@acc[0]	! 2*a+a=3*a
	addccc	$t1,@acc[1],@acc[1]
	addccc	$t2,@acc[2],@acc[2]
	addccc	$t3,@acc[3],@acc[3]
	addccc	$t4,@acc[4],@acc[4]
	addccc	$t5,@acc[5],@acc[5]
	addccc	$t6,@acc[6],@acc[6]
	addccc	$t7,@acc[7],@acc[7]
	b	.Lreduce_by_sub
	addc	%g0,%g0,$carry
.type	__ecp_nistz256_mul_by_3,#function
.size	__ecp_nistz256_mul_by_3,.-__ecp_nistz256_mul_by_3

! void	ecp_nistz256_sub(BN_ULONG %i0[8],const BN_ULONG %i1[8],
!				         const BN_ULONG %i2[8]);
.globl	ecp_nistz256_sub
.align	32
ecp_nistz256_sub:
	save	%sp,-STACK_FRAME,%sp
	ld	[$ap],@acc[0]
	ld	[$ap+4],@acc[1]
	ld	[$ap+8],@acc[2]
	ld	[$ap+12],@acc[3]
	ld	[$ap+16],@acc[4]
	ld	[$ap+20],@acc[5]
	ld	[$ap+24],@acc[6]
	call	__ecp_nistz256_sub_from
	ld	[$ap+28],@acc[7]
	ret
	restore
.type	ecp_nistz256_sub,#function
.size	ecp_nistz256_sub,.-ecp_nistz256_sub

! void	ecp_nistz256_neg(BN_ULONG %i0[8],const BN_ULONG %i1[8]);
.globl	ecp_nistz256_neg
.align	32
ecp_nistz256_neg:
	save	%sp,-STACK_FRAME,%sp
	mov	$ap,$bp
	mov	0,@acc[0]
	mov	0,@acc[1]
	mov	0,@acc[2]
	mov	0,@acc[3]
	mov	0,@acc[4]
	mov	0,@acc[5]
	mov	0,@acc[6]
	call	__ecp_nistz256_sub_from
	mov	0,@acc[7]
	ret
	restore
.type	ecp_nistz256_neg,#function
.size	ecp_nistz256_neg,.-ecp_nistz256_neg

.align	32
__ecp_nistz256_sub_from:
	ld	[$bp+0],$t0		! b[0]
	ld	[$bp+4],$t1
	ld	[$bp+8],$t2
	ld	[$bp+12],$t3
	subcc	@acc[0],$t0,@acc[0]
	ld	[$bp+16],$t4
	ld	[$bp+20],$t5
	subccc	@acc[1],$t1,@acc[1]
	subccc	@acc[2],$t2,@acc[2]
	ld	[$bp+24],$t6
	ld	[$bp+28],$t7
	subccc	@acc[3],$t3,@acc[3]
	subccc	@acc[4],$t4,@acc[4]
	subccc	@acc[5],$t5,@acc[5]
	subccc	@acc[6],$t6,@acc[6]
	subccc	@acc[7],$t7,@acc[7]
	subc	%g0,%g0,$carry		! broadcast borrow bit

.Lreduce_by_add:

	! if a-b borrows, add modulus.
	!
	! Note that because mod has special form, i.e. consists of
	! 0xffffffff, 1 and 0s, we can conditionally synthesize it by
	! using value of broadcasted borrow and the borrow bit itself.
	! To minimize dependency chain we first broadcast and then
	! extract the bit by negating (follow $bi).

	addcc	@acc[0],$carry,@acc[0]	! add synthesized modulus
	addccc	@acc[1],$carry,@acc[1]
	neg	$carry,$bi
	st	@acc[0],[$rp]
	addccc	@acc[2],$carry,@acc[2]
	st	@acc[1],[$rp+4]
	addccc	@acc[3],0,@acc[3]
	st	@acc[2],[$rp+8]
	addccc	@acc[4],0,@acc[4]
	st	@acc[3],[$rp+12]
	addccc	@acc[5],0,@acc[5]
	st	@acc[4],[$rp+16]
	addccc	@acc[6],$bi,@acc[6]
	st	@acc[5],[$rp+20]
	addc	@acc[7],$carry,@acc[7]
	st	@acc[6],[$rp+24]
	retl
	st	@acc[7],[$rp+28]
.type	__ecp_nistz256_sub_from,#function
.size	__ecp_nistz256_sub_from,.-__ecp_nistz256_sub_from

.align	32
__ecp_nistz256_sub_morf:
	ld	[$bp+0],$t0		! b[0]
	ld	[$bp+4],$t1
	ld	[$bp+8],$t2
	ld	[$bp+12],$t3
	subcc	$t0,@acc[0],@acc[0]
	ld	[$bp+16],$t4
	ld	[$bp+20],$t5
	subccc	$t1,@acc[1],@acc[1]
	subccc	$t2,@acc[2],@acc[2]
	ld	[$bp+24],$t6
	ld	[$bp+28],$t7
	subccc	$t3,@acc[3],@acc[3]
	subccc	$t4,@acc[4],@acc[4]
	subccc	$t5,@acc[5],@acc[5]
	subccc	$t6,@acc[6],@acc[6]
	subccc	$t7,@acc[7],@acc[7]
	b	.Lreduce_by_add
	subc	%g0,%g0,$carry		! broadcast borrow bit
.type	__ecp_nistz256_sub_morf,#function
.size	__ecp_nistz256_sub_morf,.-__ecp_nistz256_sub_morf

! void	ecp_nistz256_div_by_2(BN_ULONG %i0[8],const BN_ULONG %i1[8]);
.globl	ecp_nistz256_div_by_2
.align	32
ecp_nistz256_div_by_2:
	save	%sp,-STACK_FRAME,%sp
	ld	[$ap],@acc[0]
	ld	[$ap+4],@acc[1]
	ld	[$ap+8],@acc[2]
	ld	[$ap+12],@acc[3]
	ld	[$ap+16],@acc[4]
	ld	[$ap+20],@acc[5]
	ld	[$ap+24],@acc[6]
	call	__ecp_nistz256_div_by_2
	ld	[$ap+28],@acc[7]
	ret
	restore
.type	ecp_nistz256_div_by_2,#function
.size	ecp_nistz256_div_by_2,.-ecp_nistz256_div_by_2

.align	32
__ecp_nistz256_div_by_2:
	! ret = (a is odd ? a+mod : a) >> 1

	and	@acc[0],1,$bi
	neg	$bi,$carry
	addcc	@acc[0],$carry,@acc[0]
	addccc	@acc[1],$carry,@acc[1]
	addccc	@acc[2],$carry,@acc[2]
	addccc	@acc[3],0,@acc[3]
	addccc	@acc[4],0,@acc[4]
	addccc	@acc[5],0,@acc[5]
	addccc	@acc[6],$bi,@acc[6]
	addccc	@acc[7],$carry,@acc[7]
	addc	%g0,%g0,$carry

	! ret >>= 1

	srl	@acc[0],1,@acc[0]
	sll	@acc[1],31,$t0
	srl	@acc[1],1,@acc[1]
	or	@acc[0],$t0,@acc[0]
	sll	@acc[2],31,$t1
	srl	@acc[2],1,@acc[2]
	or	@acc[1],$t1,@acc[1]
	sll	@acc[3],31,$t2
	st	@acc[0],[$rp]
	srl	@acc[3],1,@acc[3]
	or	@acc[2],$t2,@acc[2]
	sll	@acc[4],31,$t3
	st	@acc[1],[$rp+4]
	srl	@acc[4],1,@acc[4]
	or	@acc[3],$t3,@acc[3]
	sll	@acc[5],31,$t4
	st	@acc[2],[$rp+8]
	srl	@acc[5],1,@acc[5]
	or	@acc[4],$t4,@acc[4]
	sll	@acc[6],31,$t5
	st	@acc[3],[$rp+12]
	srl	@acc[6],1,@acc[6]
	or	@acc[5],$t5,@acc[5]
	sll	@acc[7],31,$t6
	st	@acc[4],[$rp+16]
	srl	@acc[7],1,@acc[7]
	or	@acc[6],$t6,@acc[6]
	sll	$carry,31,$t7
	st	@acc[5],[$rp+20]
	or	@acc[7],$t7,@acc[7]
	st	@acc[6],[$rp+24]
	retl
	st	@acc[7],[$rp+28]
.type	__ecp_nistz256_div_by_2,#function
.size	__ecp_nistz256_div_by_2,.-__ecp_nistz256_div_by_2
___

########################################################################
# following subroutines are "literal" implementation of those found in
# ecp_nistz256.c
#
########################################################################
# void ecp_nistz256_point_double(P256_POINT *out,const P256_POINT *inp);
#
{
my ($S,$M,$Zsqr,$tmp0)=map(32*$_,(0..3));
# above map() describes stack layout with 4 temporary
# 256-bit vectors on top.

$code.=<<___;
#ifdef __PIC__
SPARC_PIC_THUNK(%g1)
#endif

.globl	ecp_nistz256_point_double
.align	32
ecp_nistz256_point_double:
	SPARC_LOAD_ADDRESS_LEAF(OPENSSL_sparcv9cap_P,%g1,%g5)
	ld	[%g1],%g1		! OPENSSL_sparcv9cap_P[0]
	and	%g1,(SPARCV9_VIS3|SPARCV9_64BIT_STACK),%g1
	cmp	%g1,(SPARCV9_VIS3|SPARCV9_64BIT_STACK)
	be	ecp_nistz256_point_double_vis3
	nop

	save	%sp,-STACK_FRAME-32*4,%sp

	mov	$rp,$rp_real
	mov	$ap,$ap_real

.Lpoint_double_shortcut:
	ld	[$ap+32],@acc[0]
	ld	[$ap+32+4],@acc[1]
	ld	[$ap+32+8],@acc[2]
	ld	[$ap+32+12],@acc[3]
	ld	[$ap+32+16],@acc[4]
	ld	[$ap+32+20],@acc[5]
	ld	[$ap+32+24],@acc[6]
	ld	[$ap+32+28],@acc[7]
	call	__ecp_nistz256_mul_by_2	! p256_mul_by_2(S, in_y);
	add	%sp,LOCALS+$S,$rp

	add	$ap_real,64,$bp
	add	$ap_real,64,$ap
	call	__ecp_nistz256_mul_mont	! p256_sqr_mont(Zsqr, in_z);
	add	%sp,LOCALS+$Zsqr,$rp

	add	$ap_real,0,$bp
	call	__ecp_nistz256_add	! p256_add(M, Zsqr, in_x);
	add	%sp,LOCALS+$M,$rp

	add	%sp,LOCALS+$S,$bp
	add	%sp,LOCALS+$S,$ap
	call	__ecp_nistz256_mul_mont	! p256_sqr_mont(S, S);
	add	%sp,LOCALS+$S,$rp

	ld	[$ap_real],@acc[0]
	add	%sp,LOCALS+$Zsqr,$bp
	ld	[$ap_real+4],@acc[1]
	ld	[$ap_real+8],@acc[2]
	ld	[$ap_real+12],@acc[3]
	ld	[$ap_real+16],@acc[4]
	ld	[$ap_real+20],@acc[5]
	ld	[$ap_real+24],@acc[6]
	ld	[$ap_real+28],@acc[7]
	call	__ecp_nistz256_sub_from	! p256_sub(Zsqr, in_x, Zsqr);
	add	%sp,LOCALS+$Zsqr,$rp

	add	$ap_real,32,$bp
	add	$ap_real,64,$ap
	call	__ecp_nistz256_mul_mont	! p256_mul_mont(tmp0, in_z, in_y);
	add	%sp,LOCALS+$tmp0,$rp

	call	__ecp_nistz256_mul_by_2	! p256_mul_by_2(res_z, tmp0);
	add	$rp_real,64,$rp

	add	%sp,LOCALS+$Zsqr,$bp
	add	%sp,LOCALS+$M,$ap
	call	__ecp_nistz256_mul_mont	! p256_mul_mont(M, M, Zsqr);
	add	%sp,LOCALS+$M,$rp

	call	__ecp_nistz256_mul_by_3	! p256_mul_by_3(M, M);
	add	%sp,LOCALS+$M,$rp

	add	%sp,LOCALS+$S,$bp
	add	%sp,LOCALS+$S,$ap
	call	__ecp_nistz256_mul_mont	! p256_sqr_mont(tmp0, S);
	add	%sp,LOCALS+$tmp0,$rp

	call	__ecp_nistz256_div_by_2	! p256_div_by_2(res_y, tmp0);
	add	$rp_real,32,$rp

	add	$ap_real,0,$bp
	add	%sp,LOCALS+$S,$ap
	call	__ecp_nistz256_mul_mont	! p256_mul_mont(S, S, in_x);
	add	%sp,LOCALS+$S,$rp

	call	__ecp_nistz256_mul_by_2	! p256_mul_by_2(tmp0, S);
	add	%sp,LOCALS+$tmp0,$rp

	add	%sp,LOCALS+$M,$bp
	add	%sp,LOCALS+$M,$ap
	call	__ecp_nistz256_mul_mont	! p256_sqr_mont(res_x, M);
	add	$rp_real,0,$rp

	add	%sp,LOCALS+$tmp0,$bp
	call	__ecp_nistz256_sub_from	! p256_sub(res_x, res_x, tmp0);
	add	$rp_real,0,$rp

	add	%sp,LOCALS+$S,$bp
	call	__ecp_nistz256_sub_morf	! p256_sub(S, S, res_x);
	add	%sp,LOCALS+$S,$rp

	add	%sp,LOCALS+$M,$bp
	add	%sp,LOCALS+$S,$ap
	call	__ecp_nistz256_mul_mont	! p256_mul_mont(S, S, M);
	add	%sp,LOCALS+$S,$rp

	add	$rp_real,32,$bp
	call	__ecp_nistz256_sub_from	! p256_sub(res_y, S, res_y);
	add	$rp_real,32,$rp

	ret
	restore
.type	ecp_nistz256_point_double,#function
.size	ecp_nistz256_point_double,.-ecp_nistz256_point_double
___
}

########################################################################
# void ecp_nistz256_point_add(P256_POINT *out,const P256_POINT *in1,
#			      const P256_POINT *in2);
{
my ($res_x,$res_y,$res_z,
    $H,$Hsqr,$R,$Rsqr,$Hcub,
    $U1,$U2,$S1,$S2)=map(32*$_,(0..11));
my ($Z1sqr, $Z2sqr) = ($Hsqr, $Rsqr);

# above map() describes stack layout with 12 temporary
# 256-bit vectors on top. Then we reserve some space for
# !in1infty, !in2infty, result of check for zero and return pointer.

my $bp_real=$rp_real;

$code.=<<___;
.globl	ecp_nistz256_point_add
.align	32
ecp_nistz256_point_add:
	SPARC_LOAD_ADDRESS_LEAF(OPENSSL_sparcv9cap_P,%g1,%g5)
	ld	[%g1],%g1		! OPENSSL_sparcv9cap_P[0]
	and	%g1,(SPARCV9_VIS3|SPARCV9_64BIT_STACK),%g1
	cmp	%g1,(SPARCV9_VIS3|SPARCV9_64BIT_STACK)
	be	ecp_nistz256_point_add_vis3
	nop

	save	%sp,-STACK_FRAME-32*12-32,%sp

	stx	$rp,[%fp+STACK_BIAS-8]	! off-load $rp
	mov	$ap,$ap_real
	mov	$bp,$bp_real

	ld	[$bp+64],$t0		! in2_z
	ld	[$bp+64+4],$t1
	ld	[$bp+64+8],$t2
	ld	[$bp+64+12],$t3
	ld	[$bp+64+16],$t4
	ld	[$bp+64+20],$t5
	ld	[$bp+64+24],$t6
	ld	[$bp+64+28],$t7
	or	$t1,$t0,$t0
	or	$t3,$t2,$t2
	or	$t5,$t4,$t4
	or	$t7,$t6,$t6
	or	$t2,$t0,$t0
	or	$t6,$t4,$t4
	or	$t4,$t0,$t0		! !in2infty
	movrnz	$t0,-1,$t0
	st	$t0,[%fp+STACK_BIAS-12]

	ld	[$ap+64],$t0		! in1_z
	ld	[$ap+64+4],$t1
	ld	[$ap+64+8],$t2
	ld	[$ap+64+12],$t3
	ld	[$ap+64+16],$t4
	ld	[$ap+64+20],$t5
	ld	[$ap+64+24],$t6
	ld	[$ap+64+28],$t7
	or	$t1,$t0,$t0
	or	$t3,$t2,$t2
	or	$t5,$t4,$t4
	or	$t7,$t6,$t6
	or	$t2,$t0,$t0
	or	$t6,$t4,$t4
	or	$t4,$t0,$t0		! !in1infty
	movrnz	$t0,-1,$t0
	st	$t0,[%fp+STACK_BIAS-16]

	add	$bp_real,64,$bp
	add	$bp_real,64,$ap
	call	__ecp_nistz256_mul_mont	! p256_sqr_mont(Z2sqr, in2_z);
	add	%sp,LOCALS+$Z2sqr,$rp

	add	$ap_real,64,$bp
	add	$ap_real,64,$ap
	call	__ecp_nistz256_mul_mont	! p256_sqr_mont(Z1sqr, in1_z);
	add	%sp,LOCALS+$Z1sqr,$rp

	add	$bp_real,64,$bp
	add	%sp,LOCALS+$Z2sqr,$ap
	call	__ecp_nistz256_mul_mont	! p256_mul_mont(S1, Z2sqr, in2_z);
	add	%sp,LOCALS+$S1,$rp

	add	$ap_real,64,$bp
	add	%sp,LOCALS+$Z1sqr,$ap
	call	__ecp_nistz256_mul_mont	! p256_mul_mont(S2, Z1sqr, in1_z);
	add	%sp,LOCALS+$S2,$rp

	add	$ap_real,32,$bp
	add	%sp,LOCALS+$S1,$ap
	call	__ecp_nistz256_mul_mont	! p256_mul_mont(S1, S1, in1_y);
	add	%sp,LOCALS+$S1,$rp

	add	$bp_real,32,$bp
	add	%sp,LOCALS+$S2,$ap
	call	__ecp_nistz256_mul_mont	! p256_mul_mont(S2, S2, in2_y);
	add	%sp,LOCALS+$S2,$rp

	add	%sp,LOCALS+$S1,$bp
	call	__ecp_nistz256_sub_from	! p256_sub(R, S2, S1);
	add	%sp,LOCALS+$R,$rp

	or	@acc[1],@acc[0],@acc[0]	! see if result is zero
	or	@acc[3],@acc[2],@acc[2]
	or	@acc[5],@acc[4],@acc[4]
	or	@acc[7],@acc[6],@acc[6]
	or	@acc[2],@acc[0],@acc[0]
	or	@acc[6],@acc[4],@acc[4]
	or	@acc[4],@acc[0],@acc[0]
	st	@acc[0],[%fp+STACK_BIAS-20]

	add	$ap_real,0,$bp
	add	%sp,LOCALS+$Z2sqr,$ap
	call	__ecp_nistz256_mul_mont	! p256_mul_mont(U1, in1_x, Z2sqr);
	add	%sp,LOCALS+$U1,$rp

	add	$bp_real,0,$bp
	add	%sp,LOCALS+$Z1sqr,$ap
	call	__ecp_nistz256_mul_mont	! p256_mul_mont(U2, in2_x, Z1sqr);
	add	%sp,LOCALS+$U2,$rp

	add	%sp,LOCALS+$U1,$bp
	call	__ecp_nistz256_sub_from	! p256_sub(H, U2, U1);
	add	%sp,LOCALS+$H,$rp

	or	@acc[1],@acc[0],@acc[0]	! see if result is zero
	or	@acc[3],@acc[2],@acc[2]
	or	@acc[5],@acc[4],@acc[4]
	or	@acc[7],@acc[6],@acc[6]
	or	@acc[2],@acc[0],@acc[0]
	or	@acc[6],@acc[4],@acc[4]
	orcc	@acc[4],@acc[0],@acc[0]

	bne,pt	%icc,.Ladd_proceed	! is_equal(U1,U2)?
	nop

	ld	[%fp+STACK_BIAS-12],$t0
	ld	[%fp+STACK_BIAS-16],$t1
	ld	[%fp+STACK_BIAS-20],$t2
	andcc	$t0,$t1,%g0
	be,pt	%icc,.Ladd_proceed	! (in1infty || in2infty)?
	nop
	andcc	$t2,$t2,%g0
	be,pt	%icc,.Ladd_double	! is_equal(S1,S2)?
	nop

	ldx	[%fp+STACK_BIAS-8],$rp
	st	%g0,[$rp]
	st	%g0,[$rp+4]
	st	%g0,[$rp+8]
	st	%g0,[$rp+12]
	st	%g0,[$rp+16]
	st	%g0,[$rp+20]
	st	%g0,[$rp+24]
	st	%g0,[$rp+28]
	st	%g0,[$rp+32]
	st	%g0,[$rp+32+4]
	st	%g0,[$rp+32+8]
	st	%g0,[$rp+32+12]
	st	%g0,[$rp+32+16]
	st	%g0,[$rp+32+20]
	st	%g0,[$rp+32+24]
	st	%g0,[$rp+32+28]
	st	%g0,[$rp+64]
	st	%g0,[$rp+64+4]
	st	%g0,[$rp+64+8]
	st	%g0,[$rp+64+12]
	st	%g0,[$rp+64+16]
	st	%g0,[$rp+64+20]
	st	%g0,[$rp+64+24]
	st	%g0,[$rp+64+28]
	b	.Ladd_done
	nop

.align	16
.Ladd_double:
	ldx	[%fp+STACK_BIAS-8],$rp_real
	mov	$ap_real,$ap
	b	.Lpoint_double_shortcut
	add	%sp,32*(12-4)+32,%sp	! difference in frame sizes

.align	16
.Ladd_proceed:
	add	%sp,LOCALS+$R,$bp
	add	%sp,LOCALS+$R,$ap
	call	__ecp_nistz256_mul_mont	! p256_sqr_mont(Rsqr, R);
	add	%sp,LOCALS+$Rsqr,$rp

	add	$ap_real,64,$bp
	add	%sp,LOCALS+$H,$ap
	call	__ecp_nistz256_mul_mont	! p256_mul_mont(res_z, H, in1_z);
	add	%sp,LOCALS+$res_z,$rp

	add	%sp,LOCALS+$H,$bp
	add	%sp,LOCALS+$H,$ap
	call	__ecp_nistz256_mul_mont	! p256_sqr_mont(Hsqr, H);
	add	%sp,LOCALS+$Hsqr,$rp

	add	$bp_real,64,$bp
	add	%sp,LOCALS+$res_z,$ap
	call	__ecp_nistz256_mul_mont	! p256_mul_mont(res_z, res_z, in2_z);
	add	%sp,LOCALS+$res_z,$rp

	add	%sp,LOCALS+$H,$bp
	add	%sp,LOCALS+$Hsqr,$ap
	call	__ecp_nistz256_mul_mont	! p256_mul_mont(Hcub, Hsqr, H);
	add	%sp,LOCALS+$Hcub,$rp

	add	%sp,LOCALS+$U1,$bp
	add	%sp,LOCALS+$Hsqr,$ap
	call	__ecp_nistz256_mul_mont	! p256_mul_mont(U2, U1, Hsqr);
	add	%sp,LOCALS+$U2,$rp

	call	__ecp_nistz256_mul_by_2	! p256_mul_by_2(Hsqr, U2);
	add	%sp,LOCALS+$Hsqr,$rp

	add	%sp,LOCALS+$Rsqr,$bp
	call	__ecp_nistz256_sub_morf	! p256_sub(res_x, Rsqr, Hsqr);
	add	%sp,LOCALS+$res_x,$rp

	add	%sp,LOCALS+$Hcub,$bp
	call	__ecp_nistz256_sub_from	!  p256_sub(res_x, res_x, Hcub);
	add	%sp,LOCALS+$res_x,$rp

	add	%sp,LOCALS+$U2,$bp
	call	__ecp_nistz256_sub_morf	! p256_sub(res_y, U2, res_x);
	add	%sp,LOCALS+$res_y,$rp

	add	%sp,LOCALS+$Hcub,$bp
	add	%sp,LOCALS+$S1,$ap
	call	__ecp_nistz256_mul_mont	! p256_mul_mont(S2, S1, Hcub);
	add	%sp,LOCALS+$S2,$rp

	add	%sp,LOCALS+$R,$bp
	add	%sp,LOCALS+$res_y,$ap
	call	__ecp_nistz256_mul_mont	! p256_mul_mont(res_y, res_y, R);
	add	%sp,LOCALS+$res_y,$rp

	add	%sp,LOCALS+$S2,$bp
	call	__ecp_nistz256_sub_from	! p256_sub(res_y, res_y, S2);
	add	%sp,LOCALS+$res_y,$rp

	ld	[%fp+STACK_BIAS-16],$t1	! !in1infty
	ld	[%fp+STACK_BIAS-12],$t2	! !in2infty
	ldx	[%fp+STACK_BIAS-8],$rp
___
for($i=0;$i<96;$i+=8) {			# conditional moves
$code.=<<___;
	ld	[%sp+LOCALS+$i],@acc[0]		! res
	ld	[%sp+LOCALS+$i+4],@acc[1]
	ld	[$bp_real+$i],@acc[2]		! in2
	ld	[$bp_real+$i+4],@acc[3]
	ld	[$ap_real+$i],@acc[4]		! in1
	ld	[$ap_real+$i+4],@acc[5]
	movrz	$t1,@acc[2],@acc[0]
	movrz	$t1,@acc[3],@acc[1]
	movrz	$t2,@acc[4],@acc[0]
	movrz	$t2,@acc[5],@acc[1]
	st	@acc[0],[$rp+$i]
	st	@acc[1],[$rp+$i+4]
___
}
$code.=<<___;
.Ladd_done:
	ret
	restore
.type	ecp_nistz256_point_add,#function
.size	ecp_nistz256_point_add,.-ecp_nistz256_point_add
___
}

########################################################################
# void ecp_nistz256_point_add_affine(P256_POINT *out,const P256_POINT *in1,
#				     const P256_POINT_AFFINE *in2);
{
my ($res_x,$res_y,$res_z,
    $U2,$S2,$H,$R,$Hsqr,$Hcub,$Rsqr)=map(32*$_,(0..9));
my $Z1sqr = $S2;
# above map() describes stack layout with 10 temporary
# 256-bit vectors on top. Then we reserve some space for
# !in1infty, !in2infty, result of check for zero and return pointer.

my @ONE_mont=(1,0,0,-1,-1,-1,-2,0);
my $bp_real=$rp_real;

$code.=<<___;
.globl	ecp_nistz256_point_add_affine
.align	32
ecp_nistz256_point_add_affine:
	SPARC_LOAD_ADDRESS_LEAF(OPENSSL_sparcv9cap_P,%g1,%g5)
	ld	[%g1],%g1		! OPENSSL_sparcv9cap_P[0]
	and	%g1,(SPARCV9_VIS3|SPARCV9_64BIT_STACK),%g1
	cmp	%g1,(SPARCV9_VIS3|SPARCV9_64BIT_STACK)
	be	ecp_nistz256_point_add_affine_vis3
	nop

	save	%sp,-STACK_FRAME-32*10-32,%sp

	stx	$rp,[%fp+STACK_BIAS-8]	! off-load $rp
	mov	$ap,$ap_real
	mov	$bp,$bp_real

	ld	[$ap+64],$t0		! in1_z
	ld	[$ap+64+4],$t1
	ld	[$ap+64+8],$t2
	ld	[$ap+64+12],$t3
	ld	[$ap+64+16],$t4
	ld	[$ap+64+20],$t5
	ld	[$ap+64+24],$t6
	ld	[$ap+64+28],$t7
	or	$t1,$t0,$t0
	or	$t3,$t2,$t2
	or	$t5,$t4,$t4
	or	$t7,$t6,$t6
	or	$t2,$t0,$t0
	or	$t6,$t4,$t4
	or	$t4,$t0,$t0		! !in1infty
	movrnz	$t0,-1,$t0
	st	$t0,[%fp+STACK_BIAS-16]

	ld	[$bp],@acc[0]		! in2_x
	ld	[$bp+4],@acc[1]
	ld	[$bp+8],@acc[2]
	ld	[$bp+12],@acc[3]
	ld	[$bp+16],@acc[4]
	ld	[$bp+20],@acc[5]
	ld	[$bp+24],@acc[6]
	ld	[$bp+28],@acc[7]
	ld	[$bp+32],$t0		! in2_y
	ld	[$bp+32+4],$t1
	ld	[$bp+32+8],$t2
	ld	[$bp+32+12],$t3
	ld	[$bp+32+16],$t4
	ld	[$bp+32+20],$t5
	ld	[$bp+32+24],$t6
	ld	[$bp+32+28],$t7
	or	@acc[1],@acc[0],@acc[0]
	or	@acc[3],@acc[2],@acc[2]
	or	@acc[5],@acc[4],@acc[4]
	or	@acc[7],@acc[6],@acc[6]
	or	@acc[2],@acc[0],@acc[0]
	or	@acc[6],@acc[4],@acc[4]
	or	@acc[4],@acc[0],@acc[0]
	or	$t1,$t0,$t0
	or	$t3,$t2,$t2
	or	$t5,$t4,$t4
	or	$t7,$t6,$t6
	or	$t2,$t0,$t0
	or	$t6,$t4,$t4
	or	$t4,$t0,$t0
	or	@acc[0],$t0,$t0		! !in2infty
	movrnz	$t0,-1,$t0
	st	$t0,[%fp+STACK_BIAS-12]

	add	$ap_real,64,$bp
	add	$ap_real,64,$ap
	call	__ecp_nistz256_mul_mont	! p256_sqr_mont(Z1sqr, in1_z);
	add	%sp,LOCALS+$Z1sqr,$rp

	add	$bp_real,0,$bp
	add	%sp,LOCALS+$Z1sqr,$ap
	call	__ecp_nistz256_mul_mont	! p256_mul_mont(U2, Z1sqr, in2_x);
	add	%sp,LOCALS+$U2,$rp

	add	$ap_real,0,$bp
	call	__ecp_nistz256_sub_from	! p256_sub(H, U2, in1_x);
	add	%sp,LOCALS+$H,$rp

	add	$ap_real,64,$bp
	add	%sp,LOCALS+$Z1sqr,$ap
	call	__ecp_nistz256_mul_mont	! p256_mul_mont(S2, Z1sqr, in1_z);
	add	%sp,LOCALS+$S2,$rp

	add	$ap_real,64,$bp
	add	%sp,LOCALS+$H,$ap
	call	__ecp_nistz256_mul_mont	! p256_mul_mont(res_z, H, in1_z);
	add	%sp,LOCALS+$res_z,$rp

	add	$bp_real,32,$bp
	add	%sp,LOCALS+$S2,$ap
	call	__ecp_nistz256_mul_mont	! p256_mul_mont(S2, S2, in2_y);
	add	%sp,LOCALS+$S2,$rp

	add	$ap_real,32,$bp
	call	__ecp_nistz256_sub_from	! p256_sub(R, S2, in1_y);
	add	%sp,LOCALS+$R,$rp

	add	%sp,LOCALS+$H,$bp
	add	%sp,LOCALS+$H,$ap
	call	__ecp_nistz256_mul_mont	! p256_sqr_mont(Hsqr, H);
	add	%sp,LOCALS+$Hsqr,$rp

	add	%sp,LOCALS+$R,$bp
	add	%sp,LOCALS+$R,$ap
	call	__ecp_nistz256_mul_mont	! p256_sqr_mont(Rsqr, R);
	add	%sp,LOCALS+$Rsqr,$rp

	add	%sp,LOCALS+$H,$bp
	add	%sp,LOCALS+$Hsqr,$ap
	call	__ecp_nistz256_mul_mont	! p256_mul_mont(Hcub, Hsqr, H);
	add	%sp,LOCALS+$Hcub,$rp

	add	$ap_real,0,$bp
	add	%sp,LOCALS+$Hsqr,$ap
	call	__ecp_nistz256_mul_mont	! p256_mul_mont(U2, in1_x, Hsqr);
	add	%sp,LOCALS+$U2,$rp

	call	__ecp_nistz256_mul_by_2	! p256_mul_by_2(Hsqr, U2);
	add	%sp,LOCALS+$Hsqr,$rp

	add	%sp,LOCALS+$Rsqr,$bp
	call	__ecp_nistz256_sub_morf	! p256_sub(res_x, Rsqr, Hsqr);
	add	%sp,LOCALS+$res_x,$rp

	add	%sp,LOCALS+$Hcub,$bp
	call	__ecp_nistz256_sub_from	!  p256_sub(res_x, res_x, Hcub);
	add	%sp,LOCALS+$res_x,$rp

	add	%sp,LOCALS+$U2,$bp
	call	__ecp_nistz256_sub_morf	! p256_sub(res_y, U2, res_x);
	add	%sp,LOCALS+$res_y,$rp

	add	$ap_real,32,$bp
	add	%sp,LOCALS+$Hcub,$ap
	call	__ecp_nistz256_mul_mont	! p256_mul_mont(S2, in1_y, Hcub);
	add	%sp,LOCALS+$S2,$rp

	add	%sp,LOCALS+$R,$bp
	add	%sp,LOCALS+$res_y,$ap
	call	__ecp_nistz256_mul_mont	! p256_mul_mont(res_y, res_y, R);
	add	%sp,LOCALS+$res_y,$rp

	add	%sp,LOCALS+$S2,$bp
	call	__ecp_nistz256_sub_from	! p256_sub(res_y, res_y, S2);
	add	%sp,LOCALS+$res_y,$rp

	ld	[%fp+STACK_BIAS-16],$t1	! !in1infty
	ld	[%fp+STACK_BIAS-12],$t2	! !in2infty
	ldx	[%fp+STACK_BIAS-8],$rp
___
for($i=0;$i<64;$i+=8) {			# conditional moves
$code.=<<___;
	ld	[%sp+LOCALS+$i],@acc[0]		! res
	ld	[%sp+LOCALS+$i+4],@acc[1]
	ld	[$bp_real+$i],@acc[2]		! in2
	ld	[$bp_real+$i+4],@acc[3]
	ld	[$ap_real+$i],@acc[4]		! in1
	ld	[$ap_real+$i+4],@acc[5]
	movrz	$t1,@acc[2],@acc[0]
	movrz	$t1,@acc[3],@acc[1]
	movrz	$t2,@acc[4],@acc[0]
	movrz	$t2,@acc[5],@acc[1]
	st	@acc[0],[$rp+$i]
	st	@acc[1],[$rp+$i+4]
___
}
for(;$i<96;$i+=8) {
my $j=($i-64)/4;
$code.=<<___;
	ld	[%sp+LOCALS+$i],@acc[0]		! res
	ld	[%sp+LOCALS+$i+4],@acc[1]
	ld	[$ap_real+$i],@acc[4]		! in1
	ld	[$ap_real+$i+4],@acc[5]
	movrz	$t1,@ONE_mont[$j],@acc[0]
	movrz	$t1,@ONE_mont[$j+1],@acc[1]
	movrz	$t2,@acc[4],@acc[0]
	movrz	$t2,@acc[5],@acc[1]
	st	@acc[0],[$rp+$i]
	st	@acc[1],[$rp+$i+4]
___
}
$code.=<<___;
	ret
	restore
.type	ecp_nistz256_point_add_affine,#function
.size	ecp_nistz256_point_add_affine,.-ecp_nistz256_point_add_affine
___
}								}}}
{{{
my ($out,$inp,$index)=map("%i$_",(0..2));
my $mask="%o0";

$code.=<<___;
! void	ecp_nistz256_scatter_w5(void *%i0,const P256_POINT *%i1,
!					  int %i2);
.globl	ecp_nistz256_scatter_w5
.align	32
ecp_nistz256_scatter_w5:
	save	%sp,-STACK_FRAME,%sp

	sll	$index,2,$index
	add	$out,$index,$out

	ld	[$inp],%l0		! X
	ld	[$inp+4],%l1
	ld	[$inp+8],%l2
	ld	[$inp+12],%l3
	ld	[$inp+16],%l4
	ld	[$inp+20],%l5
	ld	[$inp+24],%l6
	ld	[$inp+28],%l7
	add	$inp,32,$inp
	st	%l0,[$out+64*0-4]
	st	%l1,[$out+64*1-4]
	st	%l2,[$out+64*2-4]
	st	%l3,[$out+64*3-4]
	st	%l4,[$out+64*4-4]
	st	%l5,[$out+64*5-4]
	st	%l6,[$out+64*6-4]
	st	%l7,[$out+64*7-4]
	add	$out,64*8,$out

	ld	[$inp],%l0		! Y
	ld	[$inp+4],%l1
	ld	[$inp+8],%l2
	ld	[$inp+12],%l3
	ld	[$inp+16],%l4
	ld	[$inp+20],%l5
	ld	[$inp+24],%l6
	ld	[$inp+28],%l7
	add	$inp,32,$inp
	st	%l0,[$out+64*0-4]
	st	%l1,[$out+64*1-4]
	st	%l2,[$out+64*2-4]
	st	%l3,[$out+64*3-4]
	st	%l4,[$out+64*4-4]
	st	%l5,[$out+64*5-4]
	st	%l6,[$out+64*6-4]
	st	%l7,[$out+64*7-4]
	add	$out,64*8,$out

	ld	[$inp],%l0		! Z
	ld	[$inp+4],%l1
	ld	[$inp+8],%l2
	ld	[$inp+12],%l3
	ld	[$inp+16],%l4
	ld	[$inp+20],%l5
	ld	[$inp+24],%l6
	ld	[$inp+28],%l7
	st	%l0,[$out+64*0-4]
	st	%l1,[$out+64*1-4]
	st	%l2,[$out+64*2-4]
	st	%l3,[$out+64*3-4]
	st	%l4,[$out+64*4-4]
	st	%l5,[$out+64*5-4]
	st	%l6,[$out+64*6-4]
	st	%l7,[$out+64*7-4]

	ret
	restore
.type	ecp_nistz256_scatter_w5,#function
.size	ecp_nistz256_scatter_w5,.-ecp_nistz256_scatter_w5

! void	ecp_nistz256_gather_w5(P256_POINT *%i0,const void *%i1,
!					       int %i2);
.globl	ecp_nistz256_gather_w5
.align	32
ecp_nistz256_gather_w5:
	save	%sp,-STACK_FRAME,%sp

	neg	$index,$mask
	srax	$mask,63,$mask

	add	$index,$mask,$index
	sll	$index,2,$index
	add	$inp,$index,$inp

	ld	[$inp+64*0],%l0
	ld	[$inp+64*1],%l1
	ld	[$inp+64*2],%l2
	ld	[$inp+64*3],%l3
	ld	[$inp+64*4],%l4
	ld	[$inp+64*5],%l5
	ld	[$inp+64*6],%l6
	ld	[$inp+64*7],%l7
	add	$inp,64*8,$inp
	and	%l0,$mask,%l0
	and	%l1,$mask,%l1
	st	%l0,[$out]		! X
	and	%l2,$mask,%l2
	st	%l1,[$out+4]
	and	%l3,$mask,%l3
	st	%l2,[$out+8]
	and	%l4,$mask,%l4
	st	%l3,[$out+12]
	and	%l5,$mask,%l5
	st	%l4,[$out+16]
	and	%l6,$mask,%l6
	st	%l5,[$out+20]
	and	%l7,$mask,%l7
	st	%l6,[$out+24]
	st	%l7,[$out+28]
	add	$out,32,$out

	ld	[$inp+64*0],%l0
	ld	[$inp+64*1],%l1
	ld	[$inp+64*2],%l2
	ld	[$inp+64*3],%l3
	ld	[$inp+64*4],%l4
	ld	[$inp+64*5],%l5
	ld	[$inp+64*6],%l6
	ld	[$inp+64*7],%l7
	add	$inp,64*8,$inp
	and	%l0,$mask,%l0
	and	%l1,$mask,%l1
	st	%l0,[$out]		! Y
	and	%l2,$mask,%l2
	st	%l1,[$out+4]
	and	%l3,$mask,%l3
	st	%l2,[$out+8]
	and	%l4,$mask,%l4
	st	%l3,[$out+12]
	and	%l5,$mask,%l5
	st	%l4,[$out+16]
	and	%l6,$mask,%l6
	st	%l5,[$out+20]
	and	%l7,$mask,%l7
	st	%l6,[$out+24]
	st	%l7,[$out+28]
	add	$out,32,$out

	ld	[$inp+64*0],%l0
	ld	[$inp+64*1],%l1
	ld	[$inp+64*2],%l2
	ld	[$inp+64*3],%l3
	ld	[$inp+64*4],%l4
	ld	[$inp+64*5],%l5
	ld	[$inp+64*6],%l6
	ld	[$inp+64*7],%l7
	and	%l0,$mask,%l0
	and	%l1,$mask,%l1
	st	%l0,[$out]		! Z
	and	%l2,$mask,%l2
	st	%l1,[$out+4]
	and	%l3,$mask,%l3
	st	%l2,[$out+8]
	and	%l4,$mask,%l4
	st	%l3,[$out+12]
	and	%l5,$mask,%l5
	st	%l4,[$out+16]
	and	%l6,$mask,%l6
	st	%l5,[$out+20]
	and	%l7,$mask,%l7
	st	%l6,[$out+24]
	st	%l7,[$out+28]

	ret
	restore
.type	ecp_nistz256_gather_w5,#function
.size	ecp_nistz256_gather_w5,.-ecp_nistz256_gather_w5

! void	ecp_nistz256_scatter_w7(void *%i0,const P256_POINT_AFFINE *%i1,
!					  int %i2);
.globl	ecp_nistz256_scatter_w7
.align	32
ecp_nistz256_scatter_w7:
	save	%sp,-STACK_FRAME,%sp
	nop
	add	$out,$index,$out
	mov	64/4,$index
.Loop_scatter_w7:
	ld	[$inp],%l0
	add	$inp,4,$inp
	subcc	$index,1,$index
	stb	%l0,[$out+64*0]
	srl	%l0,8,%l1
	stb	%l1,[$out+64*1]
	srl	%l0,16,%l2
	stb	%l2,[$out+64*2]
	srl	%l0,24,%l3
	stb	%l3,[$out+64*3]
	bne	.Loop_scatter_w7
	add	$out,64*4,$out

	ret
	restore
.type	ecp_nistz256_scatter_w7,#function
.size	ecp_nistz256_scatter_w7,.-ecp_nistz256_scatter_w7

! void	ecp_nistz256_gather_w7(P256_POINT_AFFINE *%i0,const void *%i1,
!						      int %i2);
.globl	ecp_nistz256_gather_w7
.align	32
ecp_nistz256_gather_w7:
	save	%sp,-STACK_FRAME,%sp

	neg	$index,$mask
	srax	$mask,63,$mask

	add	$index,$mask,$index
	add	$inp,$index,$inp
	mov	64/4,$index

.Loop_gather_w7:
	ldub	[$inp+64*0],%l0
	prefetch [$inp+3840+64*0],1
	subcc	$index,1,$index
	ldub	[$inp+64*1],%l1
	prefetch [$inp+3840+64*1],1
	ldub	[$inp+64*2],%l2
	prefetch [$inp+3840+64*2],1
	ldub	[$inp+64*3],%l3
	prefetch [$inp+3840+64*3],1
	add	$inp,64*4,$inp
	sll	%l1,8,%l1
	sll	%l2,16,%l2
	or	%l0,%l1,%l0
	sll	%l3,24,%l3
	or	%l0,%l2,%l0
	or	%l0,%l3,%l0
	and	%l0,$mask,%l0
	st	%l0,[$out]
	bne	.Loop_gather_w7
	add	$out,4,$out

	ret
	restore
.type	ecp_nistz256_gather_w7,#function
.size	ecp_nistz256_gather_w7,.-ecp_nistz256_gather_w7
___
}}}
{{{
########################################################################
# Following subroutines are VIS3 counterparts of those above that
# implement ones found in ecp_nistz256.c. Key difference is that they
# use 128-bit multiplication and addition with 64-bit carry, and in order
# to do that they perform conversion from uin32_t[8] to uint64_t[4] upon
# entry and vice versa on return.
#
my ($rp,$ap,$bp)=map("%i$_",(0..2));
my ($t0,$t1,$t2,$t3,$a0,$a1,$a2,$a3)=map("%l$_",(0..7));
my ($acc0,$acc1,$acc2,$acc3,$acc4,$acc5)=map("%o$_",(0..5));
my ($bi,$poly1,$poly3,$minus1)=(map("%i$_",(3..5)),"%g1");
my ($rp_real,$ap_real)=("%g2","%g3");
my ($acc6,$acc7)=($bp,$bi);	# used in squaring

$code.=<<___;
.align	32
__ecp_nistz256_mul_by_2_vis3:
	addcc	$acc0,$acc0,$acc0
	addxccc	$acc1,$acc1,$acc1
	addxccc	$acc2,$acc2,$acc2
	addxccc	$acc3,$acc3,$acc3
	b	.Lreduce_by_sub_vis3
	addxc	%g0,%g0,$acc4		! did it carry?
.type	__ecp_nistz256_mul_by_2_vis3,#function
.size	__ecp_nistz256_mul_by_2_vis3,.-__ecp_nistz256_mul_by_2_vis3

.align	32
__ecp_nistz256_add_vis3:
	ldx	[$bp+0],$t0
	ldx	[$bp+8],$t1
	ldx	[$bp+16],$t2
	ldx	[$bp+24],$t3

__ecp_nistz256_add_noload_vis3:

	addcc	$t0,$acc0,$acc0
	addxccc	$t1,$acc1,$acc1
	addxccc	$t2,$acc2,$acc2
	addxccc	$t3,$acc3,$acc3
	addxc	%g0,%g0,$acc4		! did it carry?

.Lreduce_by_sub_vis3:

	addcc	$acc0,1,$t0		! add -modulus, i.e. subtract
	addxccc	$acc1,$poly1,$t1
	addxccc	$acc2,$minus1,$t2
	addxccc	$acc3,$poly3,$t3
	addxc	$acc4,$minus1,$acc4

	movrz	$acc4,$t0,$acc0		! ret = borrow ? ret : ret-modulus
	movrz	$acc4,$t1,$acc1
	stx	$acc0,[$rp]
	movrz	$acc4,$t2,$acc2
	stx	$acc1,[$rp+8]
	movrz	$acc4,$t3,$acc3
	stx	$acc2,[$rp+16]
	retl
	stx	$acc3,[$rp+24]
.type	__ecp_nistz256_add_vis3,#function
.size	__ecp_nistz256_add_vis3,.-__ecp_nistz256_add_vis3

! Trouble with subtraction is that there is no subtraction with 64-bit
! borrow, only with 32-bit one. For this reason we "decompose" 64-bit
! $acc0-$acc3 to 32-bit values and pick b[4] in 32-bit pieces. But
! recall that SPARC is big-endian, which is why you'll observe that
! b[4] is accessed as 4-0-12-8-20-16-28-24. And prior reduction we
! "collect" result back to 64-bit $acc0-$acc3.
.align	32
__ecp_nistz256_sub_from_vis3:
	ld	[$bp+4],$t0
	ld	[$bp+0],$t1
	ld	[$bp+12],$t2
	ld	[$bp+8],$t3

	srlx	$acc0,32,$acc4
	not	$poly1,$poly1
	srlx	$acc1,32,$acc5
	subcc	$acc0,$t0,$acc0
	ld	[$bp+20],$t0
	subccc	$acc4,$t1,$acc4
	ld	[$bp+16],$t1
	subccc	$acc1,$t2,$acc1
	ld	[$bp+28],$t2
	and	$acc0,$poly1,$acc0
	subccc	$acc5,$t3,$acc5
	ld	[$bp+24],$t3
	sllx	$acc4,32,$acc4
	and	$acc1,$poly1,$acc1
	sllx	$acc5,32,$acc5
	or	$acc0,$acc4,$acc0
	srlx	$acc2,32,$acc4
	or	$acc1,$acc5,$acc1
	srlx	$acc3,32,$acc5
	subccc	$acc2,$t0,$acc2
	subccc	$acc4,$t1,$acc4
	subccc	$acc3,$t2,$acc3
	and	$acc2,$poly1,$acc2
	subccc	$acc5,$t3,$acc5
	sllx	$acc4,32,$acc4
	and	$acc3,$poly1,$acc3
	sllx	$acc5,32,$acc5
	or	$acc2,$acc4,$acc2
	subc	%g0,%g0,$acc4		! did it borrow?
	b	.Lreduce_by_add_vis3
	or	$acc3,$acc5,$acc3
.type	__ecp_nistz256_sub_from_vis3,#function
.size	__ecp_nistz256_sub_from_vis3,.-__ecp_nistz256_sub_from_vis3

.align	32
__ecp_nistz256_sub_morf_vis3:
	ld	[$bp+4],$t0
	ld	[$bp+0],$t1
	ld	[$bp+12],$t2
	ld	[$bp+8],$t3

	srlx	$acc0,32,$acc4
	not	$poly1,$poly1
	srlx	$acc1,32,$acc5
	subcc	$t0,$acc0,$acc0
	ld	[$bp+20],$t0
	subccc	$t1,$acc4,$acc4
	ld	[$bp+16],$t1
	subccc	$t2,$acc1,$acc1
	ld	[$bp+28],$t2
	and	$acc0,$poly1,$acc0
	subccc	$t3,$acc5,$acc5
	ld	[$bp+24],$t3
	sllx	$acc4,32,$acc4
	and	$acc1,$poly1,$acc1
	sllx	$acc5,32,$acc5
	or	$acc0,$acc4,$acc0
	srlx	$acc2,32,$acc4
	or	$acc1,$acc5,$acc1
	srlx	$acc3,32,$acc5
	subccc	$t0,$acc2,$acc2
	subccc	$t1,$acc4,$acc4
	subccc	$t2,$acc3,$acc3
	and	$acc2,$poly1,$acc2
	subccc	$t3,$acc5,$acc5
	sllx	$acc4,32,$acc4
	and	$acc3,$poly1,$acc3
	sllx	$acc5,32,$acc5
	or	$acc2,$acc4,$acc2
	subc	%g0,%g0,$acc4		! did it borrow?
	or	$acc3,$acc5,$acc3

.Lreduce_by_add_vis3:

	addcc	$acc0,-1,$t0		! add modulus
	not	$poly3,$t3
	addxccc	$acc1,$poly1,$t1
	not	$poly1,$poly1		! restore $poly1
	addxccc	$acc2,%g0,$t2
	addxc	$acc3,$t3,$t3

	movrnz	$acc4,$t0,$acc0		! if a-b borrowed, ret = ret+mod
	movrnz	$acc4,$t1,$acc1
	stx	$acc0,[$rp]
	movrnz	$acc4,$t2,$acc2
	stx	$acc1,[$rp+8]
	movrnz	$acc4,$t3,$acc3
	stx	$acc2,[$rp+16]
	retl
	stx	$acc3,[$rp+24]
.type	__ecp_nistz256_sub_morf_vis3,#function
.size	__ecp_nistz256_sub_morf_vis3,.-__ecp_nistz256_sub_morf_vis3

.align	32
__ecp_nistz256_div_by_2_vis3:
	! ret = (a is odd ? a+mod : a) >> 1

	not	$poly1,$t1
	not	$poly3,$t3
	and	$acc0,1,$acc5
	addcc	$acc0,-1,$t0		! add modulus
	addxccc	$acc1,$t1,$t1
	addxccc	$acc2,%g0,$t2
	addxccc	$acc3,$t3,$t3
	addxc	%g0,%g0,$acc4		! carry bit

	movrnz	$acc5,$t0,$acc0
	movrnz	$acc5,$t1,$acc1
	movrnz	$acc5,$t2,$acc2
	movrnz	$acc5,$t3,$acc3
	movrz	$acc5,%g0,$acc4

	! ret >>= 1

	srlx	$acc0,1,$acc0
	sllx	$acc1,63,$t0
	srlx	$acc1,1,$acc1
	or	$acc0,$t0,$acc0
	sllx	$acc2,63,$t1
	srlx	$acc2,1,$acc2
	or	$acc1,$t1,$acc1
	sllx	$acc3,63,$t2
	stx	$acc0,[$rp]
	srlx	$acc3,1,$acc3
	or	$acc2,$t2,$acc2
	sllx	$acc4,63,$t3		! don't forget carry bit
	stx	$acc1,[$rp+8]
	or	$acc3,$t3,$acc3
	stx	$acc2,[$rp+16]
	retl
	stx	$acc3,[$rp+24]
.type	__ecp_nistz256_div_by_2_vis3,#function
.size	__ecp_nistz256_div_by_2_vis3,.-__ecp_nistz256_div_by_2_vis3

! compared to __ecp_nistz256_mul_mont it's almost 4x smaller and
! 4x faster [on T4]...
.align	32
__ecp_nistz256_mul_mont_vis3:
	mulx	$a0,$bi,$acc0
	not	$poly3,$poly3		! 0xFFFFFFFF00000001
	umulxhi	$a0,$bi,$t0
	mulx	$a1,$bi,$acc1
	umulxhi	$a1,$bi,$t1
	mulx	$a2,$bi,$acc2
	umulxhi	$a2,$bi,$t2
	mulx	$a3,$bi,$acc3
	umulxhi	$a3,$bi,$t3
	ldx	[$bp+8],$bi		! b[1]

	addcc	$acc1,$t0,$acc1		! accumulate high parts of multiplication
	 sllx	$acc0,32,$t0
	addxccc	$acc2,$t1,$acc2
	 srlx	$acc0,32,$t1
	addxccc	$acc3,$t2,$acc3
	addxc	%g0,$t3,$acc4
	mov	0,$acc5
___
for($i=1;$i<4;$i++) {
	# Reduction iteration is normally performed by accumulating
	# result of multiplication of modulus by "magic" digit [and
	# omitting least significant word, which is guaranteed to
	# be 0], but thanks to special form of modulus and "magic"
	# digit being equal to least significant word, it can be
	# performed with additions and subtractions alone. Indeed:
	#
	#            ffff0001.00000000.0000ffff.ffffffff
	# *                                     abcdefgh
	# + xxxxxxxx.xxxxxxxx.xxxxxxxx.xxxxxxxx.abcdefgh
	#
	# Now observing that ff..ff*x = (2^n-1)*x = 2^n*x-x, we
	# rewrite above as:
	#
	#   xxxxxxxx.xxxxxxxx.xxxxxxxx.xxxxxxxx.abcdefgh
	# + abcdefgh.abcdefgh.0000abcd.efgh0000.00000000
	# - 0000abcd.efgh0000.00000000.00000000.abcdefgh
	#
	# or marking redundant operations:
	#
	#   xxxxxxxx.xxxxxxxx.xxxxxxxx.xxxxxxxx.--------
	# + abcdefgh.abcdefgh.0000abcd.efgh0000.--------
	# - 0000abcd.efgh0000.--------.--------.--------
	#   ^^^^^^^^ but this word is calculated with umulxhi, because
	#            there is no subtract with 64-bit borrow:-(

$code.=<<___;
	sub	$acc0,$t0,$t2		! acc0*0xFFFFFFFF00000001, low part
	umulxhi	$acc0,$poly3,$t3	! acc0*0xFFFFFFFF00000001, high part
	addcc	$acc1,$t0,$acc0		! +=acc[0]<<96 and omit acc[0]
	mulx	$a0,$bi,$t0
	addxccc	$acc2,$t1,$acc1
	mulx	$a1,$bi,$t1
	addxccc	$acc3,$t2,$acc2		! +=acc[0]*0xFFFFFFFF00000001
	mulx	$a2,$bi,$t2
	addxccc	$acc4,$t3,$acc3
	mulx	$a3,$bi,$t3
	addxc	$acc5,%g0,$acc4

	addcc	$acc0,$t0,$acc0		! accumulate low parts of multiplication
	umulxhi	$a0,$bi,$t0
	addxccc	$acc1,$t1,$acc1
	umulxhi	$a1,$bi,$t1
	addxccc	$acc2,$t2,$acc2
	umulxhi	$a2,$bi,$t2
	addxccc	$acc3,$t3,$acc3
	umulxhi	$a3,$bi,$t3
	addxc	$acc4,%g0,$acc4
___
$code.=<<___	if ($i<3);
	ldx	[$bp+8*($i+1)],$bi	! bp[$i+1]
___
$code.=<<___;
	addcc	$acc1,$t0,$acc1		! accumulate high parts of multiplication
	 sllx	$acc0,32,$t0
	addxccc	$acc2,$t1,$acc2
	 srlx	$acc0,32,$t1
	addxccc	$acc3,$t2,$acc3
	addxccc	$acc4,$t3,$acc4
	addxc	%g0,%g0,$acc5
___
}
$code.=<<___;
	sub	$acc0,$t0,$t2		! acc0*0xFFFFFFFF00000001, low part
	umulxhi	$acc0,$poly3,$t3	! acc0*0xFFFFFFFF00000001, high part
	addcc	$acc1,$t0,$acc0		! +=acc[0]<<96 and omit acc[0]
	addxccc	$acc2,$t1,$acc1
	addxccc	$acc3,$t2,$acc2		! +=acc[0]*0xFFFFFFFF00000001
	addxccc	$acc4,$t3,$acc3
	b	.Lmul_final_vis3	! see below
	addxc	$acc5,%g0,$acc4
.type	__ecp_nistz256_mul_mont_vis3,#function
.size	__ecp_nistz256_mul_mont_vis3,.-__ecp_nistz256_mul_mont_vis3

! compared to above __ecp_nistz256_mul_mont_vis3 it's 21% less
! instructions, but only 14% faster [on T4]...
.align	32
__ecp_nistz256_sqr_mont_vis3:
	!  |  |  |  |  |  |a1*a0|  |
	!  |  |  |  |  |a2*a0|  |  |
	!  |  |a3*a2|a3*a0|  |  |  |
	!  |  |  |  |a2*a1|  |  |  |
	!  |  |  |a3*a1|  |  |  |  |
	! *|  |  |  |  |  |  |  | 2|
	! +|a3*a3|a2*a2|a1*a1|a0*a0|
	!  |--+--+--+--+--+--+--+--|
	!  |A7|A6|A5|A4|A3|A2|A1|A0|, where Ax is $accx, i.e. follow $accx
	!
	!  "can't overflow" below mark carrying into high part of
	!  multiplication result, which can't overflow, because it
	!  can never be all ones.

	mulx	$a1,$a0,$acc1		! a[1]*a[0]
	umulxhi	$a1,$a0,$t1
	mulx	$a2,$a0,$acc2		! a[2]*a[0]
	umulxhi	$a2,$a0,$t2
	mulx	$a3,$a0,$acc3		! a[3]*a[0]
	umulxhi	$a3,$a0,$acc4

	addcc	$acc2,$t1,$acc2		! accumulate high parts of multiplication
	mulx	$a2,$a1,$t0		! a[2]*a[1]
	umulxhi	$a2,$a1,$t1
	addxccc	$acc3,$t2,$acc3
	mulx	$a3,$a1,$t2		! a[3]*a[1]
	umulxhi	$a3,$a1,$t3
	addxc	$acc4,%g0,$acc4		! can't overflow

	mulx	$a3,$a2,$acc5		! a[3]*a[2]
	not	$poly3,$poly3		! 0xFFFFFFFF00000001
	umulxhi	$a3,$a2,$acc6

	addcc	$t2,$t1,$t1		! accumulate high parts of multiplication
	mulx	$a0,$a0,$acc0		! a[0]*a[0]
	addxc	$t3,%g0,$t2		! can't overflow

	addcc	$acc3,$t0,$acc3		! accumulate low parts of multiplication
	umulxhi	$a0,$a0,$a0
	addxccc	$acc4,$t1,$acc4
	mulx	$a1,$a1,$t1		! a[1]*a[1]
	addxccc	$acc5,$t2,$acc5
	umulxhi	$a1,$a1,$a1
	addxc	$acc6,%g0,$acc6		! can't overflow

	addcc	$acc1,$acc1,$acc1	! acc[1-6]*=2
	mulx	$a2,$a2,$t2		! a[2]*a[2]
	addxccc	$acc2,$acc2,$acc2
	umulxhi	$a2,$a2,$a2
	addxccc	$acc3,$acc3,$acc3
	mulx	$a3,$a3,$t3		! a[3]*a[3]
	addxccc	$acc4,$acc4,$acc4
	umulxhi	$a3,$a3,$a3
	addxccc	$acc5,$acc5,$acc5
	addxccc	$acc6,$acc6,$acc6
	addxc	%g0,%g0,$acc7

	addcc	$acc1,$a0,$acc1		! +a[i]*a[i]
	addxccc	$acc2,$t1,$acc2
	addxccc	$acc3,$a1,$acc3
	addxccc	$acc4,$t2,$acc4
	 sllx	$acc0,32,$t0
	addxccc	$acc5,$a2,$acc5
	 srlx	$acc0,32,$t1
	addxccc	$acc6,$t3,$acc6
	 sub	$acc0,$t0,$t2		! acc0*0xFFFFFFFF00000001, low part
	addxc	$acc7,$a3,$acc7
___
for($i=0;$i<3;$i++) {			# reductions, see commentary
					# in multiplication for details
$code.=<<___;
	umulxhi	$acc0,$poly3,$t3	! acc0*0xFFFFFFFF00000001, high part
	addcc	$acc1,$t0,$acc0		! +=acc[0]<<96 and omit acc[0]
	 sllx	$acc0,32,$t0
	addxccc	$acc2,$t1,$acc1
	 srlx	$acc0,32,$t1
	addxccc	$acc3,$t2,$acc2		! +=acc[0]*0xFFFFFFFF00000001
	 sub	$acc0,$t0,$t2		! acc0*0xFFFFFFFF00000001, low part
	addxc	%g0,$t3,$acc3		! can't overflow
___
}
$code.=<<___;
	umulxhi	$acc0,$poly3,$t3	! acc0*0xFFFFFFFF00000001, high part
	addcc	$acc1,$t0,$acc0		! +=acc[0]<<96 and omit acc[0]
	addxccc	$acc2,$t1,$acc1
	addxccc	$acc3,$t2,$acc2		! +=acc[0]*0xFFFFFFFF00000001
	addxc	%g0,$t3,$acc3		! can't overflow

	addcc	$acc0,$acc4,$acc0	! accumulate upper half
	addxccc	$acc1,$acc5,$acc1
	addxccc	$acc2,$acc6,$acc2
	addxccc	$acc3,$acc7,$acc3
	addxc	%g0,%g0,$acc4

.Lmul_final_vis3:

	! Final step is "if result > mod, subtract mod", but as comparison
	! means subtraction, we do the subtraction and then copy outcome
	! if it didn't borrow. But note that as we [have to] replace
	! subtraction with addition with negative, carry/borrow logic is
	! inverse.

	addcc	$acc0,1,$t0		! add -modulus, i.e. subtract
	not	$poly3,$poly3		! restore 0x00000000FFFFFFFE
	addxccc	$acc1,$poly1,$t1
	addxccc	$acc2,$minus1,$t2
	addxccc	$acc3,$poly3,$t3
	addxccc	$acc4,$minus1,%g0	! did it carry?

	movcs	%xcc,$t0,$acc0
	movcs	%xcc,$t1,$acc1
	stx	$acc0,[$rp]
	movcs	%xcc,$t2,$acc2
	stx	$acc1,[$rp+8]
	movcs	%xcc,$t3,$acc3
	stx	$acc2,[$rp+16]
	retl
	stx	$acc3,[$rp+24]
.type	__ecp_nistz256_sqr_mont_vis3,#function
.size	__ecp_nistz256_sqr_mont_vis3,.-__ecp_nistz256_sqr_mont_vis3
___

########################################################################
# void ecp_nistz256_point_double(P256_POINT *out,const P256_POINT *inp);
#
{
my ($res_x,$res_y,$res_z,
    $in_x,$in_y,$in_z,
    $S,$M,$Zsqr,$tmp0)=map(32*$_,(0..9));
# above map() describes stack layout with 10 temporary
# 256-bit vectors on top.

$code.=<<___;
.align	32
ecp_nistz256_point_double_vis3:
	save	%sp,-STACK64_FRAME-32*10,%sp

	mov	$rp,$rp_real
.Ldouble_shortcut_vis3:
	mov	-1,$minus1
	mov	-2,$poly3
	sllx	$minus1,32,$poly1		! 0xFFFFFFFF00000000
	srl	$poly3,0,$poly3			! 0x00000000FFFFFFFE

	! convert input to uint64_t[4]
	ld	[$ap],$a0			! in_x
	ld	[$ap+4],$t0
	ld	[$ap+8],$a1
	ld	[$ap+12],$t1
	ld	[$ap+16],$a2
	ld	[$ap+20],$t2
	ld	[$ap+24],$a3
	ld	[$ap+28],$t3
	sllx	$t0,32,$t0
	sllx	$t1,32,$t1
	ld	[$ap+32],$acc0			! in_y
	or	$a0,$t0,$a0
	ld	[$ap+32+4],$t0
	sllx	$t2,32,$t2
	ld	[$ap+32+8],$acc1
	or	$a1,$t1,$a1
	ld	[$ap+32+12],$t1
	sllx	$t3,32,$t3
	ld	[$ap+32+16],$acc2
	or	$a2,$t2,$a2
	ld	[$ap+32+20],$t2
	or	$a3,$t3,$a3
	ld	[$ap+32+24],$acc3
	sllx	$t0,32,$t0
	ld	[$ap+32+28],$t3
	sllx	$t1,32,$t1
	stx	$a0,[%sp+LOCALS64+$in_x]
	sllx	$t2,32,$t2
	stx	$a1,[%sp+LOCALS64+$in_x+8]
	sllx	$t3,32,$t3
	stx	$a2,[%sp+LOCALS64+$in_x+16]
	or	$acc0,$t0,$acc0
	stx	$a3,[%sp+LOCALS64+$in_x+24]
	or	$acc1,$t1,$acc1
	stx	$acc0,[%sp+LOCALS64+$in_y]
	or	$acc2,$t2,$acc2
	stx	$acc1,[%sp+LOCALS64+$in_y+8]
	or	$acc3,$t3,$acc3
	stx	$acc2,[%sp+LOCALS64+$in_y+16]
	stx	$acc3,[%sp+LOCALS64+$in_y+24]

	ld	[$ap+64],$a0			! in_z
	ld	[$ap+64+4],$t0
	ld	[$ap+64+8],$a1
	ld	[$ap+64+12],$t1
	ld	[$ap+64+16],$a2
	ld	[$ap+64+20],$t2
	ld	[$ap+64+24],$a3
	ld	[$ap+64+28],$t3
	sllx	$t0,32,$t0
	sllx	$t1,32,$t1
	or	$a0,$t0,$a0
	sllx	$t2,32,$t2
	or	$a1,$t1,$a1
	sllx	$t3,32,$t3
	or	$a2,$t2,$a2
	or	$a3,$t3,$a3
	sllx	$t0,32,$t0
	sllx	$t1,32,$t1
	stx	$a0,[%sp+LOCALS64+$in_z]
	sllx	$t2,32,$t2
	stx	$a1,[%sp+LOCALS64+$in_z+8]
	sllx	$t3,32,$t3
	stx	$a2,[%sp+LOCALS64+$in_z+16]
	stx	$a3,[%sp+LOCALS64+$in_z+24]

	! in_y is still in $acc0-$acc3
	call	__ecp_nistz256_mul_by_2_vis3	! p256_mul_by_2(S, in_y);
	add	%sp,LOCALS64+$S,$rp

	! in_z is still in $a0-$a3
	call	__ecp_nistz256_sqr_mont_vis3	! p256_sqr_mont(Zsqr, in_z);
	add	%sp,LOCALS64+$Zsqr,$rp

	mov	$acc0,$a0			! put Zsqr aside
	mov	$acc1,$a1
	mov	$acc2,$a2
	mov	$acc3,$a3

	add	%sp,LOCALS64+$in_x,$bp
	call	__ecp_nistz256_add_vis3		! p256_add(M, Zsqr, in_x);
	add	%sp,LOCALS64+$M,$rp

	mov	$a0,$acc0			! restore Zsqr
	ldx	[%sp+LOCALS64+$S],$a0		! forward load
	mov	$a1,$acc1
	ldx	[%sp+LOCALS64+$S+8],$a1
	mov	$a2,$acc2
	ldx	[%sp+LOCALS64+$S+16],$a2
	mov	$a3,$acc3
	ldx	[%sp+LOCALS64+$S+24],$a3

	add	%sp,LOCALS64+$in_x,$bp
	call	__ecp_nistz256_sub_morf_vis3	! p256_sub(Zsqr, in_x, Zsqr);
	add	%sp,LOCALS64+$Zsqr,$rp

	call	__ecp_nistz256_sqr_mont_vis3	! p256_sqr_mont(S, S);
	add	%sp,LOCALS64+$S,$rp

	ldx	[%sp+LOCALS64+$in_z],$bi
	ldx	[%sp+LOCALS64+$in_y],$a0
	ldx	[%sp+LOCALS64+$in_y+8],$a1
	ldx	[%sp+LOCALS64+$in_y+16],$a2
	ldx	[%sp+LOCALS64+$in_y+24],$a3
	add	%sp,LOCALS64+$in_z,$bp
	call	__ecp_nistz256_mul_mont_vis3	! p256_mul_mont(tmp0, in_z, in_y);
	add	%sp,LOCALS64+$tmp0,$rp

	ldx	[%sp+LOCALS64+$M],$bi		! forward load
	ldx	[%sp+LOCALS64+$Zsqr],$a0
	ldx	[%sp+LOCALS64+$Zsqr+8],$a1
	ldx	[%sp+LOCALS64+$Zsqr+16],$a2
	ldx	[%sp+LOCALS64+$Zsqr+24],$a3

	call	__ecp_nistz256_mul_by_2_vis3	! p256_mul_by_2(res_z, tmp0);
	add	%sp,LOCALS64+$res_z,$rp

	add	%sp,LOCALS64+$M,$bp
	call	__ecp_nistz256_mul_mont_vis3	! p256_mul_mont(M, M, Zsqr);
	add	%sp,LOCALS64+$M,$rp

	mov	$acc0,$a0			! put aside M
	mov	$acc1,$a1
	mov	$acc2,$a2
	mov	$acc3,$a3
	call	__ecp_nistz256_mul_by_2_vis3
	add	%sp,LOCALS64+$M,$rp
	mov	$a0,$t0				! copy M
	ldx	[%sp+LOCALS64+$S],$a0		! forward load
	mov	$a1,$t1
	ldx	[%sp+LOCALS64+$S+8],$a1
	mov	$a2,$t2
	ldx	[%sp+LOCALS64+$S+16],$a2
	mov	$a3,$t3
	ldx	[%sp+LOCALS64+$S+24],$a3
	call	__ecp_nistz256_add_noload_vis3	! p256_mul_by_3(M, M);
	add	%sp,LOCALS64+$M,$rp

	call	__ecp_nistz256_sqr_mont_vis3	! p256_sqr_mont(tmp0, S);
	add	%sp,LOCALS64+$tmp0,$rp

	ldx	[%sp+LOCALS64+$S],$bi		! forward load
	ldx	[%sp+LOCALS64+$in_x],$a0
	ldx	[%sp+LOCALS64+$in_x+8],$a1
	ldx	[%sp+LOCALS64+$in_x+16],$a2
	ldx	[%sp+LOCALS64+$in_x+24],$a3

	call	__ecp_nistz256_div_by_2_vis3	! p256_div_by_2(res_y, tmp0);
	add	%sp,LOCALS64+$res_y,$rp

	add	%sp,LOCALS64+$S,$bp
	call	__ecp_nistz256_mul_mont_vis3	! p256_mul_mont(S, S, in_x);
	add	%sp,LOCALS64+$S,$rp

	ldx	[%sp+LOCALS64+$M],$a0		! forward load
	ldx	[%sp+LOCALS64+$M+8],$a1
	ldx	[%sp+LOCALS64+$M+16],$a2
	ldx	[%sp+LOCALS64+$M+24],$a3

	call	__ecp_nistz256_mul_by_2_vis3	! p256_mul_by_2(tmp0, S);
	add	%sp,LOCALS64+$tmp0,$rp

	call	__ecp_nistz256_sqr_mont_vis3	! p256_sqr_mont(res_x, M);
	add	%sp,LOCALS64+$res_x,$rp

	add	%sp,LOCALS64+$tmp0,$bp
	call	__ecp_nistz256_sub_from_vis3	! p256_sub(res_x, res_x, tmp0);
	add	%sp,LOCALS64+$res_x,$rp

	ldx	[%sp+LOCALS64+$M],$a0		! forward load
	ldx	[%sp+LOCALS64+$M+8],$a1
	ldx	[%sp+LOCALS64+$M+16],$a2
	ldx	[%sp+LOCALS64+$M+24],$a3

	add	%sp,LOCALS64+$S,$bp
	call	__ecp_nistz256_sub_morf_vis3	! p256_sub(S, S, res_x);
	add	%sp,LOCALS64+$S,$rp

	mov	$acc0,$bi
	call	__ecp_nistz256_mul_mont_vis3	! p256_mul_mont(S, S, M);
	add	%sp,LOCALS64+$S,$rp

	ldx	[%sp+LOCALS64+$res_x],$a0	! forward load
	ldx	[%sp+LOCALS64+$res_x+8],$a1
	ldx	[%sp+LOCALS64+$res_x+16],$a2
	ldx	[%sp+LOCALS64+$res_x+24],$a3

	add	%sp,LOCALS64+$res_y,$bp
	call	__ecp_nistz256_sub_from_vis3	! p256_sub(res_y, S, res_y);
	add	%sp,LOCALS64+$res_y,$bp

	! convert output to uint_32[8]
	srlx	$a0,32,$t0
	srlx	$a1,32,$t1
	st	$a0,[$rp_real]			! res_x
	srlx	$a2,32,$t2
	st	$t0,[$rp_real+4]
	srlx	$a3,32,$t3
	st	$a1,[$rp_real+8]
	st	$t1,[$rp_real+12]
	st	$a2,[$rp_real+16]
	st	$t2,[$rp_real+20]
	st	$a3,[$rp_real+24]
	st	$t3,[$rp_real+28]

	ldx	[%sp+LOCALS64+$res_z],$a0	! forward load
	srlx	$acc0,32,$t0
	ldx	[%sp+LOCALS64+$res_z+8],$a1
	srlx	$acc1,32,$t1
	ldx	[%sp+LOCALS64+$res_z+16],$a2
	srlx	$acc2,32,$t2
	ldx	[%sp+LOCALS64+$res_z+24],$a3
	srlx	$acc3,32,$t3
	st	$acc0,[$rp_real+32]		! res_y
	st	$t0,  [$rp_real+32+4]
	st	$acc1,[$rp_real+32+8]
	st	$t1,  [$rp_real+32+12]
	st	$acc2,[$rp_real+32+16]
	st	$t2,  [$rp_real+32+20]
	st	$acc3,[$rp_real+32+24]
	st	$t3,  [$rp_real+32+28]

	srlx	$a0,32,$t0
	srlx	$a1,32,$t1
	st	$a0,[$rp_real+64]		! res_z
	srlx	$a2,32,$t2
	st	$t0,[$rp_real+64+4]
	srlx	$a3,32,$t3
	st	$a1,[$rp_real+64+8]
	st	$t1,[$rp_real+64+12]
	st	$a2,[$rp_real+64+16]
	st	$t2,[$rp_real+64+20]
	st	$a3,[$rp_real+64+24]
	st	$t3,[$rp_real+64+28]

	ret
	restore
.type	ecp_nistz256_point_double_vis3,#function
.size	ecp_nistz256_point_double_vis3,.-ecp_nistz256_point_double_vis3
___
}
########################################################################
# void ecp_nistz256_point_add(P256_POINT *out,const P256_POINT *in1,
#			      const P256_POINT *in2);
{
my ($res_x,$res_y,$res_z,
    $in1_x,$in1_y,$in1_z,
    $in2_x,$in2_y,$in2_z,
    $H,$Hsqr,$R,$Rsqr,$Hcub,
    $U1,$U2,$S1,$S2)=map(32*$_,(0..17));
my ($Z1sqr, $Z2sqr) = ($Hsqr, $Rsqr);

# above map() describes stack layout with 18 temporary
# 256-bit vectors on top. Then we reserve some space for
# !in1infty, !in2infty and result of check for zero.

$code.=<<___;
.globl	ecp_nistz256_point_add_vis3
.align	32
ecp_nistz256_point_add_vis3:
	save	%sp,-STACK64_FRAME-32*18-32,%sp

	mov	$rp,$rp_real
	mov	-1,$minus1
	mov	-2,$poly3
	sllx	$minus1,32,$poly1		! 0xFFFFFFFF00000000
	srl	$poly3,0,$poly3			! 0x00000000FFFFFFFE

	! convert input to uint64_t[4]
	ld	[$bp],$a0			! in2_x
	ld	[$bp+4],$t0
	ld	[$bp+8],$a1
	ld	[$bp+12],$t1
	ld	[$bp+16],$a2
	ld	[$bp+20],$t2
	ld	[$bp+24],$a3
	ld	[$bp+28],$t3
	sllx	$t0,32,$t0
	sllx	$t1,32,$t1
	ld	[$bp+32],$acc0			! in2_y
	or	$a0,$t0,$a0
	ld	[$bp+32+4],$t0
	sllx	$t2,32,$t2
	ld	[$bp+32+8],$acc1
	or	$a1,$t1,$a1
	ld	[$bp+32+12],$t1
	sllx	$t3,32,$t3
	ld	[$bp+32+16],$acc2
	or	$a2,$t2,$a2
	ld	[$bp+32+20],$t2
	or	$a3,$t3,$a3
	ld	[$bp+32+24],$acc3
	sllx	$t0,32,$t0
	ld	[$bp+32+28],$t3
	sllx	$t1,32,$t1
	stx	$a0,[%sp+LOCALS64+$in2_x]
	sllx	$t2,32,$t2
	stx	$a1,[%sp+LOCALS64+$in2_x+8]
	sllx	$t3,32,$t3
	stx	$a2,[%sp+LOCALS64+$in2_x+16]
	or	$acc0,$t0,$acc0
	stx	$a3,[%sp+LOCALS64+$in2_x+24]
	or	$acc1,$t1,$acc1
	stx	$acc0,[%sp+LOCALS64+$in2_y]
	or	$acc2,$t2,$acc2
	stx	$acc1,[%sp+LOCALS64+$in2_y+8]
	or	$acc3,$t3,$acc3
	stx	$acc2,[%sp+LOCALS64+$in2_y+16]
	stx	$acc3,[%sp+LOCALS64+$in2_y+24]

	ld	[$bp+64],$acc0			! in2_z
	ld	[$bp+64+4],$t0
	ld	[$bp+64+8],$acc1
	ld	[$bp+64+12],$t1
	ld	[$bp+64+16],$acc2
	ld	[$bp+64+20],$t2
	ld	[$bp+64+24],$acc3
	ld	[$bp+64+28],$t3
	sllx	$t0,32,$t0
	sllx	$t1,32,$t1
	ld	[$ap],$a0			! in1_x
	or	$acc0,$t0,$acc0
	ld	[$ap+4],$t0
	sllx	$t2,32,$t2
	ld	[$ap+8],$a1
	or	$acc1,$t1,$acc1
	ld	[$ap+12],$t1
	sllx	$t3,32,$t3
	ld	[$ap+16],$a2
	or	$acc2,$t2,$acc2
	ld	[$ap+20],$t2
	or	$acc3,$t3,$acc3
	ld	[$ap+24],$a3
	sllx	$t0,32,$t0
	ld	[$ap+28],$t3
	sllx	$t1,32,$t1
	stx	$acc0,[%sp+LOCALS64+$in2_z]
	sllx	$t2,32,$t2
	stx	$acc1,[%sp+LOCALS64+$in2_z+8]
	sllx	$t3,32,$t3
	stx	$acc2,[%sp+LOCALS64+$in2_z+16]
	stx	$acc3,[%sp+LOCALS64+$in2_z+24]

	or	$acc1,$acc0,$acc0
	or	$acc3,$acc2,$acc2
	or	$acc2,$acc0,$acc0
	movrnz	$acc0,-1,$acc0			! !in2infty
	stx	$acc0,[%fp+STACK_BIAS-8]

	or	$a0,$t0,$a0
	ld	[$ap+32],$acc0			! in1_y
	or	$a1,$t1,$a1
	ld	[$ap+32+4],$t0
	or	$a2,$t2,$a2
	ld	[$ap+32+8],$acc1
	or	$a3,$t3,$a3
	ld	[$ap+32+12],$t1
	ld	[$ap+32+16],$acc2
	ld	[$ap+32+20],$t2
	ld	[$ap+32+24],$acc3
	sllx	$t0,32,$t0
	ld	[$ap+32+28],$t3
	sllx	$t1,32,$t1
	stx	$a0,[%sp+LOCALS64+$in1_x]
	sllx	$t2,32,$t2
	stx	$a1,[%sp+LOCALS64+$in1_x+8]
	sllx	$t3,32,$t3
	stx	$a2,[%sp+LOCALS64+$in1_x+16]
	or	$acc0,$t0,$acc0
	stx	$a3,[%sp+LOCALS64+$in1_x+24]
	or	$acc1,$t1,$acc1
	stx	$acc0,[%sp+LOCALS64+$in1_y]
	or	$acc2,$t2,$acc2
	stx	$acc1,[%sp+LOCALS64+$in1_y+8]
	or	$acc3,$t3,$acc3
	stx	$acc2,[%sp+LOCALS64+$in1_y+16]
	stx	$acc3,[%sp+LOCALS64+$in1_y+24]

	ldx	[%sp+LOCALS64+$in2_z],$a0	! forward load
	ldx	[%sp+LOCALS64+$in2_z+8],$a1
	ldx	[%sp+LOCALS64+$in2_z+16],$a2
	ldx	[%sp+LOCALS64+$in2_z+24],$a3

	ld	[$ap+64],$acc0			! in1_z
	ld	[$ap+64+4],$t0
	ld	[$ap+64+8],$acc1
	ld	[$ap+64+12],$t1
	ld	[$ap+64+16],$acc2
	ld	[$ap+64+20],$t2
	ld	[$ap+64+24],$acc3
	ld	[$ap+64+28],$t3
	sllx	$t0,32,$t0
	sllx	$t1,32,$t1
	or	$acc0,$t0,$acc0
	sllx	$t2,32,$t2
	or	$acc1,$t1,$acc1
	sllx	$t3,32,$t3
	stx	$acc0,[%sp+LOCALS64+$in1_z]
	or	$acc2,$t2,$acc2
	stx	$acc1,[%sp+LOCALS64+$in1_z+8]
	or	$acc3,$t3,$acc3
	stx	$acc2,[%sp+LOCALS64+$in1_z+16]
	stx	$acc3,[%sp+LOCALS64+$in1_z+24]

	or	$acc1,$acc0,$acc0
	or	$acc3,$acc2,$acc2
	or	$acc2,$acc0,$acc0
	movrnz	$acc0,-1,$acc0			! !in1infty
	stx	$acc0,[%fp+STACK_BIAS-16]

	call	__ecp_nistz256_sqr_mont_vis3	! p256_sqr_mont(Z2sqr, in2_z);
	add	%sp,LOCALS64+$Z2sqr,$rp

	ldx	[%sp+LOCALS64+$in1_z],$a0
	ldx	[%sp+LOCALS64+$in1_z+8],$a1
	ldx	[%sp+LOCALS64+$in1_z+16],$a2
	ldx	[%sp+LOCALS64+$in1_z+24],$a3
	call	__ecp_nistz256_sqr_mont_vis3	! p256_sqr_mont(Z1sqr, in1_z);
	add	%sp,LOCALS64+$Z1sqr,$rp

	ldx	[%sp+LOCALS64+$Z2sqr],$bi
	ldx	[%sp+LOCALS64+$in2_z],$a0
	ldx	[%sp+LOCALS64+$in2_z+8],$a1
	ldx	[%sp+LOCALS64+$in2_z+16],$a2
	ldx	[%sp+LOCALS64+$in2_z+24],$a3
	add	%sp,LOCALS64+$Z2sqr,$bp
	call	__ecp_nistz256_mul_mont_vis3	! p256_mul_mont(S1, Z2sqr, in2_z);
	add	%sp,LOCALS64+$S1,$rp

	ldx	[%sp+LOCALS64+$Z1sqr],$bi
	ldx	[%sp+LOCALS64+$in1_z],$a0
	ldx	[%sp+LOCALS64+$in1_z+8],$a1
	ldx	[%sp+LOCALS64+$in1_z+16],$a2
	ldx	[%sp+LOCALS64+$in1_z+24],$a3
	add	%sp,LOCALS64+$Z1sqr,$bp
	call	__ecp_nistz256_mul_mont_vis3	! p256_mul_mont(S2, Z1sqr, in1_z);
	add	%sp,LOCALS64+$S2,$rp

	ldx	[%sp+LOCALS64+$S1],$bi
	ldx	[%sp+LOCALS64+$in1_y],$a0
	ldx	[%sp+LOCALS64+$in1_y+8],$a1
	ldx	[%sp+LOCALS64+$in1_y+16],$a2
	ldx	[%sp+LOCALS64+$in1_y+24],$a3
	add	%sp,LOCALS64+$S1,$bp
	call	__ecp_nistz256_mul_mont_vis3	! p256_mul_mont(S1, S1, in1_y);
	add	%sp,LOCALS64+$S1,$rp

	ldx	[%sp+LOCALS64+$S2],$bi
	ldx	[%sp+LOCALS64+$in2_y],$a0
	ldx	[%sp+LOCALS64+$in2_y+8],$a1
	ldx	[%sp+LOCALS64+$in2_y+16],$a2
	ldx	[%sp+LOCALS64+$in2_y+24],$a3
	add	%sp,LOCALS64+$S2,$bp
	call	__ecp_nistz256_mul_mont_vis3	! p256_mul_mont(S2, S2, in2_y);
	add	%sp,LOCALS64+$S2,$rp

	ldx	[%sp+LOCALS64+$Z2sqr],$bi	! forward load
	ldx	[%sp+LOCALS64+$in1_x],$a0
	ldx	[%sp+LOCALS64+$in1_x+8],$a1
	ldx	[%sp+LOCALS64+$in1_x+16],$a2
	ldx	[%sp+LOCALS64+$in1_x+24],$a3

	add	%sp,LOCALS64+$S1,$bp
	call	__ecp_nistz256_sub_from_vis3	! p256_sub(R, S2, S1);
	add	%sp,LOCALS64+$R,$rp

	or	$acc1,$acc0,$acc0		! see if result is zero
	or	$acc3,$acc2,$acc2
	or	$acc2,$acc0,$acc0
	stx	$acc0,[%fp+STACK_BIAS-24]

	add	%sp,LOCALS64+$Z2sqr,$bp
	call	__ecp_nistz256_mul_mont_vis3	! p256_mul_mont(U1, in1_x, Z2sqr);
	add	%sp,LOCALS64+$U1,$rp

	ldx	[%sp+LOCALS64+$Z1sqr],$bi
	ldx	[%sp+LOCALS64+$in2_x],$a0
	ldx	[%sp+LOCALS64+$in2_x+8],$a1
	ldx	[%sp+LOCALS64+$in2_x+16],$a2
	ldx	[%sp+LOCALS64+$in2_x+24],$a3
	add	%sp,LOCALS64+$Z1sqr,$bp
	call	__ecp_nistz256_mul_mont_vis3	! p256_mul_mont(U2, in2_x, Z1sqr);
	add	%sp,LOCALS64+$U2,$rp

	ldx	[%sp+LOCALS64+$R],$a0		! forward load
	ldx	[%sp+LOCALS64+$R+8],$a1
	ldx	[%sp+LOCALS64+$R+16],$a2
	ldx	[%sp+LOCALS64+$R+24],$a3

	add	%sp,LOCALS64+$U1,$bp
	call	__ecp_nistz256_sub_from_vis3	! p256_sub(H, U2, U1);
	add	%sp,LOCALS64+$H,$rp

	or	$acc1,$acc0,$acc0		! see if result is zero
	or	$acc3,$acc2,$acc2
	orcc	$acc2,$acc0,$acc0

	bne,pt	%xcc,.Ladd_proceed_vis3		! is_equal(U1,U2)?
	nop

	ldx	[%fp+STACK_BIAS-8],$t0
	ldx	[%fp+STACK_BIAS-16],$t1
	ldx	[%fp+STACK_BIAS-24],$t2
	andcc	$t0,$t1,%g0
	be,pt	%xcc,.Ladd_proceed_vis3		! (in1infty || in2infty)?
	nop
	andcc	$t2,$t2,%g0
	be,a,pt	%xcc,.Ldouble_shortcut_vis3	! is_equal(S1,S2)?
	add	%sp,32*(12-10)+32,%sp		! difference in frame sizes

	st	%g0,[$rp_real]
	st	%g0,[$rp_real+4]
	st	%g0,[$rp_real+8]
	st	%g0,[$rp_real+12]
	st	%g0,[$rp_real+16]
	st	%g0,[$rp_real+20]
	st	%g0,[$rp_real+24]
	st	%g0,[$rp_real+28]
	st	%g0,[$rp_real+32]
	st	%g0,[$rp_real+32+4]
	st	%g0,[$rp_real+32+8]
	st	%g0,[$rp_real+32+12]
	st	%g0,[$rp_real+32+16]
	st	%g0,[$rp_real+32+20]
	st	%g0,[$rp_real+32+24]
	st	%g0,[$rp_real+32+28]
	st	%g0,[$rp_real+64]
	st	%g0,[$rp_real+64+4]
	st	%g0,[$rp_real+64+8]
	st	%g0,[$rp_real+64+12]
	st	%g0,[$rp_real+64+16]
	st	%g0,[$rp_real+64+20]
	st	%g0,[$rp_real+64+24]
	st	%g0,[$rp_real+64+28]
	b	.Ladd_done_vis3
	nop

.align	16
.Ladd_proceed_vis3:
	call	__ecp_nistz256_sqr_mont_vis3	! p256_sqr_mont(Rsqr, R);
	add	%sp,LOCALS64+$Rsqr,$rp

	ldx	[%sp+LOCALS64+$H],$bi
	ldx	[%sp+LOCALS64+$in1_z],$a0
	ldx	[%sp+LOCALS64+$in1_z+8],$a1
	ldx	[%sp+LOCALS64+$in1_z+16],$a2
	ldx	[%sp+LOCALS64+$in1_z+24],$a3
	add	%sp,LOCALS64+$H,$bp
	call	__ecp_nistz256_mul_mont_vis3	! p256_mul_mont(res_z, H, in1_z);
	add	%sp,LOCALS64+$res_z,$rp

	ldx	[%sp+LOCALS64+$H],$a0
	ldx	[%sp+LOCALS64+$H+8],$a1
	ldx	[%sp+LOCALS64+$H+16],$a2
	ldx	[%sp+LOCALS64+$H+24],$a3
	call	__ecp_nistz256_sqr_mont_vis3	! p256_sqr_mont(Hsqr, H);
	add	%sp,LOCALS64+$Hsqr,$rp

	ldx	[%sp+LOCALS64+$res_z],$bi
	ldx	[%sp+LOCALS64+$in2_z],$a0
	ldx	[%sp+LOCALS64+$in2_z+8],$a1
	ldx	[%sp+LOCALS64+$in2_z+16],$a2
	ldx	[%sp+LOCALS64+$in2_z+24],$a3
	add	%sp,LOCALS64+$res_z,$bp
	call	__ecp_nistz256_mul_mont_vis3	! p256_mul_mont(res_z, res_z, in2_z);
	add	%sp,LOCALS64+$res_z,$rp

	ldx	[%sp+LOCALS64+$H],$bi
	ldx	[%sp+LOCALS64+$Hsqr],$a0
	ldx	[%sp+LOCALS64+$Hsqr+8],$a1
	ldx	[%sp+LOCALS64+$Hsqr+16],$a2
	ldx	[%sp+LOCALS64+$Hsqr+24],$a3
	add	%sp,LOCALS64+$H,$bp
	call	__ecp_nistz256_mul_mont_vis3	! p256_mul_mont(Hcub, Hsqr, H);
	add	%sp,LOCALS64+$Hcub,$rp

	ldx	[%sp+LOCALS64+$U1],$bi
	ldx	[%sp+LOCALS64+$Hsqr],$a0
	ldx	[%sp+LOCALS64+$Hsqr+8],$a1
	ldx	[%sp+LOCALS64+$Hsqr+16],$a2
	ldx	[%sp+LOCALS64+$Hsqr+24],$a3
	add	%sp,LOCALS64+$U1,$bp
	call	__ecp_nistz256_mul_mont_vis3	! p256_mul_mont(U2, U1, Hsqr);
	add	%sp,LOCALS64+$U2,$rp

	call	__ecp_nistz256_mul_by_2_vis3	! p256_mul_by_2(Hsqr, U2);
	add	%sp,LOCALS64+$Hsqr,$rp

	add	%sp,LOCALS64+$Rsqr,$bp
	call	__ecp_nistz256_sub_morf_vis3	! p256_sub(res_x, Rsqr, Hsqr);
	add	%sp,LOCALS64+$res_x,$rp

	add	%sp,LOCALS64+$Hcub,$bp
	call	__ecp_nistz256_sub_from_vis3	!  p256_sub(res_x, res_x, Hcub);
	add	%sp,LOCALS64+$res_x,$rp

	ldx	[%sp+LOCALS64+$S1],$bi		! forward load
	ldx	[%sp+LOCALS64+$Hcub],$a0
	ldx	[%sp+LOCALS64+$Hcub+8],$a1
	ldx	[%sp+LOCALS64+$Hcub+16],$a2
	ldx	[%sp+LOCALS64+$Hcub+24],$a3

	add	%sp,LOCALS64+$U2,$bp
	call	__ecp_nistz256_sub_morf_vis3	! p256_sub(res_y, U2, res_x);
	add	%sp,LOCALS64+$res_y,$rp

	add	%sp,LOCALS64+$S1,$bp
	call	__ecp_nistz256_mul_mont_vis3	! p256_mul_mont(S2, S1, Hcub);
	add	%sp,LOCALS64+$S2,$rp

	ldx	[%sp+LOCALS64+$R],$bi
	ldx	[%sp+LOCALS64+$res_y],$a0
	ldx	[%sp+LOCALS64+$res_y+8],$a1
	ldx	[%sp+LOCALS64+$res_y+16],$a2
	ldx	[%sp+LOCALS64+$res_y+24],$a3
	add	%sp,LOCALS64+$R,$bp
	call	__ecp_nistz256_mul_mont_vis3	! p256_mul_mont(res_y, res_y, R);
	add	%sp,LOCALS64+$res_y,$rp

	add	%sp,LOCALS64+$S2,$bp
	call	__ecp_nistz256_sub_from_vis3	! p256_sub(res_y, res_y, S2);
	add	%sp,LOCALS64+$res_y,$rp

	ldx	[%fp+STACK_BIAS-16],$t1		! !in1infty
	ldx	[%fp+STACK_BIAS-8],$t2		! !in2infty
___
for($i=0;$i<96;$i+=16) {			# conditional moves
$code.=<<___;
	ldx	[%sp+LOCALS64+$res_x+$i],$acc0	! res
	ldx	[%sp+LOCALS64+$res_x+$i+8],$acc1
	ldx	[%sp+LOCALS64+$in2_x+$i],$acc2	! in2
	ldx	[%sp+LOCALS64+$in2_x+$i+8],$acc3
	ldx	[%sp+LOCALS64+$in1_x+$i],$acc4	! in1
	ldx	[%sp+LOCALS64+$in1_x+$i+8],$acc5
	movrz	$t1,$acc2,$acc0
	movrz	$t1,$acc3,$acc1
	movrz	$t2,$acc4,$acc0
	movrz	$t2,$acc5,$acc1
	srlx	$acc0,32,$acc2
	srlx	$acc1,32,$acc3
	st	$acc0,[$rp_real+$i]
	st	$acc2,[$rp_real+$i+4]
	st	$acc1,[$rp_real+$i+8]
	st	$acc3,[$rp_real+$i+12]
___
}
$code.=<<___;
.Ladd_done_vis3:
	ret
	restore
.type	ecp_nistz256_point_add_vis3,#function
.size	ecp_nistz256_point_add_vis3,.-ecp_nistz256_point_add_vis3
___
}
########################################################################
# void ecp_nistz256_point_add_affine(P256_POINT *out,const P256_POINT *in1,
#				     const P256_POINT_AFFINE *in2);
{
my ($res_x,$res_y,$res_z,
    $in1_x,$in1_y,$in1_z,
    $in2_x,$in2_y,
    $U2,$S2,$H,$R,$Hsqr,$Hcub,$Rsqr)=map(32*$_,(0..14));
my $Z1sqr = $S2;
# above map() describes stack layout with 15 temporary
# 256-bit vectors on top. Then we reserve some space for
# !in1infty and !in2infty.

$code.=<<___;
.align	32
ecp_nistz256_point_add_affine_vis3:
	save	%sp,-STACK64_FRAME-32*15-32,%sp

	mov	$rp,$rp_real
	mov	-1,$minus1
	mov	-2,$poly3
	sllx	$minus1,32,$poly1		! 0xFFFFFFFF00000000
	srl	$poly3,0,$poly3			! 0x00000000FFFFFFFE

	! convert input to uint64_t[4]
	ld	[$bp],$a0			! in2_x
	ld	[$bp+4],$t0
	ld	[$bp+8],$a1
	ld	[$bp+12],$t1
	ld	[$bp+16],$a2
	ld	[$bp+20],$t2
	ld	[$bp+24],$a3
	ld	[$bp+28],$t3
	sllx	$t0,32,$t0
	sllx	$t1,32,$t1
	ld	[$bp+32],$acc0			! in2_y
	or	$a0,$t0,$a0
	ld	[$bp+32+4],$t0
	sllx	$t2,32,$t2
	ld	[$bp+32+8],$acc1
	or	$a1,$t1,$a1
	ld	[$bp+32+12],$t1
	sllx	$t3,32,$t3
	ld	[$bp+32+16],$acc2
	or	$a2,$t2,$a2
	ld	[$bp+32+20],$t2
	or	$a3,$t3,$a3
	ld	[$bp+32+24],$acc3
	sllx	$t0,32,$t0
	ld	[$bp+32+28],$t3
	sllx	$t1,32,$t1
	stx	$a0,[%sp+LOCALS64+$in2_x]
	sllx	$t2,32,$t2
	stx	$a1,[%sp+LOCALS64+$in2_x+8]
	sllx	$t3,32,$t3
	stx	$a2,[%sp+LOCALS64+$in2_x+16]
	or	$acc0,$t0,$acc0
	stx	$a3,[%sp+LOCALS64+$in2_x+24]
	or	$acc1,$t1,$acc1
	stx	$acc0,[%sp+LOCALS64+$in2_y]
	or	$acc2,$t2,$acc2
	stx	$acc1,[%sp+LOCALS64+$in2_y+8]
	or	$acc3,$t3,$acc3
	stx	$acc2,[%sp+LOCALS64+$in2_y+16]
	stx	$acc3,[%sp+LOCALS64+$in2_y+24]

	or	$a1,$a0,$a0
	or	$a3,$a2,$a2
	or	$acc1,$acc0,$acc0
	or	$acc3,$acc2,$acc2
	or	$a2,$a0,$a0
	or	$acc2,$acc0,$acc0
	or	$acc0,$a0,$a0
	movrnz	$a0,-1,$a0			! !in2infty
	stx	$a0,[%fp+STACK_BIAS-8]

	ld	[$ap],$a0			! in1_x
	ld	[$ap+4],$t0
	ld	[$ap+8],$a1
	ld	[$ap+12],$t1
	ld	[$ap+16],$a2
	ld	[$ap+20],$t2
	ld	[$ap+24],$a3
	ld	[$ap+28],$t3
	sllx	$t0,32,$t0
	sllx	$t1,32,$t1
	ld	[$ap+32],$acc0			! in1_y
	or	$a0,$t0,$a0
	ld	[$ap+32+4],$t0
	sllx	$t2,32,$t2
	ld	[$ap+32+8],$acc1
	or	$a1,$t1,$a1
	ld	[$ap+32+12],$t1
	sllx	$t3,32,$t3
	ld	[$ap+32+16],$acc2
	or	$a2,$t2,$a2
	ld	[$ap+32+20],$t2
	or	$a3,$t3,$a3
	ld	[$ap+32+24],$acc3
	sllx	$t0,32,$t0
	ld	[$ap+32+28],$t3
	sllx	$t1,32,$t1
	stx	$a0,[%sp+LOCALS64+$in1_x]
	sllx	$t2,32,$t2
	stx	$a1,[%sp+LOCALS64+$in1_x+8]
	sllx	$t3,32,$t3
	stx	$a2,[%sp+LOCALS64+$in1_x+16]
	or	$acc0,$t0,$acc0
	stx	$a3,[%sp+LOCALS64+$in1_x+24]
	or	$acc1,$t1,$acc1
	stx	$acc0,[%sp+LOCALS64+$in1_y]
	or	$acc2,$t2,$acc2
	stx	$acc1,[%sp+LOCALS64+$in1_y+8]
	or	$acc3,$t3,$acc3
	stx	$acc2,[%sp+LOCALS64+$in1_y+16]
	stx	$acc3,[%sp+LOCALS64+$in1_y+24]

	ld	[$ap+64],$a0			! in1_z
	ld	[$ap+64+4],$t0
	ld	[$ap+64+8],$a1
	ld	[$ap+64+12],$t1
	ld	[$ap+64+16],$a2
	ld	[$ap+64+20],$t2
	ld	[$ap+64+24],$a3
	ld	[$ap+64+28],$t3
	sllx	$t0,32,$t0
	sllx	$t1,32,$t1
	or	$a0,$t0,$a0
	sllx	$t2,32,$t2
	or	$a1,$t1,$a1
	sllx	$t3,32,$t3
	stx	$a0,[%sp+LOCALS64+$in1_z]
	or	$a2,$t2,$a2
	stx	$a1,[%sp+LOCALS64+$in1_z+8]
	or	$a3,$t3,$a3
	stx	$a2,[%sp+LOCALS64+$in1_z+16]
	stx	$a3,[%sp+LOCALS64+$in1_z+24]

	or	$a1,$a0,$t0
	or	$a3,$a2,$t2
	or	$t2,$t0,$t0
	movrnz	$t0,-1,$t0			! !in1infty
	stx	$t0,[%fp+STACK_BIAS-16]

	call	__ecp_nistz256_sqr_mont_vis3	! p256_sqr_mont(Z1sqr, in1_z);
	add	%sp,LOCALS64+$Z1sqr,$rp

	ldx	[%sp+LOCALS64+$in2_x],$bi
	mov	$acc0,$a0
	mov	$acc1,$a1
	mov	$acc2,$a2
	mov	$acc3,$a3
	add	%sp,LOCALS64+$in2_x,$bp
	call	__ecp_nistz256_mul_mont_vis3	! p256_mul_mont(U2, Z1sqr, in2_x);
	add	%sp,LOCALS64+$U2,$rp

	ldx	[%sp+LOCALS64+$Z1sqr],$bi	! forward load
	ldx	[%sp+LOCALS64+$in1_z],$a0
	ldx	[%sp+LOCALS64+$in1_z+8],$a1
	ldx	[%sp+LOCALS64+$in1_z+16],$a2
	ldx	[%sp+LOCALS64+$in1_z+24],$a3

	add	%sp,LOCALS64+$in1_x,$bp
	call	__ecp_nistz256_sub_from_vis3	! p256_sub(H, U2, in1_x);
	add	%sp,LOCALS64+$H,$rp

	add	%sp,LOCALS64+$Z1sqr,$bp
	call	__ecp_nistz256_mul_mont_vis3	! p256_mul_mont(S2, Z1sqr, in1_z);
	add	%sp,LOCALS64+$S2,$rp

	ldx	[%sp+LOCALS64+$H],$bi
	ldx	[%sp+LOCALS64+$in1_z],$a0
	ldx	[%sp+LOCALS64+$in1_z+8],$a1
	ldx	[%sp+LOCALS64+$in1_z+16],$a2
	ldx	[%sp+LOCALS64+$in1_z+24],$a3
	add	%sp,LOCALS64+$H,$bp
	call	__ecp_nistz256_mul_mont_vis3	! p256_mul_mont(res_z, H, in1_z);
	add	%sp,LOCALS64+$res_z,$rp

	ldx	[%sp+LOCALS64+$S2],$bi
	ldx	[%sp+LOCALS64+$in2_y],$a0
	ldx	[%sp+LOCALS64+$in2_y+8],$a1
	ldx	[%sp+LOCALS64+$in2_y+16],$a2
	ldx	[%sp+LOCALS64+$in2_y+24],$a3
	add	%sp,LOCALS64+$S2,$bp
	call	__ecp_nistz256_mul_mont_vis3	! p256_mul_mont(S2, S2, in2_y);
	add	%sp,LOCALS64+$S2,$rp

	ldx	[%sp+LOCALS64+$H],$a0		! forward load
	ldx	[%sp+LOCALS64+$H+8],$a1
	ldx	[%sp+LOCALS64+$H+16],$a2
	ldx	[%sp+LOCALS64+$H+24],$a3

	add	%sp,LOCALS64+$in1_y,$bp
	call	__ecp_nistz256_sub_from_vis3	! p256_sub(R, S2, in1_y);
	add	%sp,LOCALS64+$R,$rp

	call	__ecp_nistz256_sqr_mont_vis3	! p256_sqr_mont(Hsqr, H);
	add	%sp,LOCALS64+$Hsqr,$rp

	ldx	[%sp+LOCALS64+$R],$a0
	ldx	[%sp+LOCALS64+$R+8],$a1
	ldx	[%sp+LOCALS64+$R+16],$a2
	ldx	[%sp+LOCALS64+$R+24],$a3
	call	__ecp_nistz256_sqr_mont_vis3	! p256_sqr_mont(Rsqr, R);
	add	%sp,LOCALS64+$Rsqr,$rp

	ldx	[%sp+LOCALS64+$H],$bi
	ldx	[%sp+LOCALS64+$Hsqr],$a0
	ldx	[%sp+LOCALS64+$Hsqr+8],$a1
	ldx	[%sp+LOCALS64+$Hsqr+16],$a2
	ldx	[%sp+LOCALS64+$Hsqr+24],$a3
	add	%sp,LOCALS64+$H,$bp
	call	__ecp_nistz256_mul_mont_vis3	! p256_mul_mont(Hcub, Hsqr, H);
	add	%sp,LOCALS64+$Hcub,$rp

	ldx	[%sp+LOCALS64+$Hsqr],$bi
	ldx	[%sp+LOCALS64+$in1_x],$a0
	ldx	[%sp+LOCALS64+$in1_x+8],$a1
	ldx	[%sp+LOCALS64+$in1_x+16],$a2
	ldx	[%sp+LOCALS64+$in1_x+24],$a3
	add	%sp,LOCALS64+$Hsqr,$bp
	call	__ecp_nistz256_mul_mont_vis3	! p256_mul_mont(U2, in1_x, Hsqr);
	add	%sp,LOCALS64+$U2,$rp

	call	__ecp_nistz256_mul_by_2_vis3	! p256_mul_by_2(Hsqr, U2);
	add	%sp,LOCALS64+$Hsqr,$rp

	add	%sp,LOCALS64+$Rsqr,$bp
	call	__ecp_nistz256_sub_morf_vis3	! p256_sub(res_x, Rsqr, Hsqr);
	add	%sp,LOCALS64+$res_x,$rp

	add	%sp,LOCALS64+$Hcub,$bp
	call	__ecp_nistz256_sub_from_vis3	!  p256_sub(res_x, res_x, Hcub);
	add	%sp,LOCALS64+$res_x,$rp

	ldx	[%sp+LOCALS64+$Hcub],$bi	! forward load
	ldx	[%sp+LOCALS64+$in1_y],$a0
	ldx	[%sp+LOCALS64+$in1_y+8],$a1
	ldx	[%sp+LOCALS64+$in1_y+16],$a2
	ldx	[%sp+LOCALS64+$in1_y+24],$a3

	add	%sp,LOCALS64+$U2,$bp
	call	__ecp_nistz256_sub_morf_vis3	! p256_sub(res_y, U2, res_x);
	add	%sp,LOCALS64+$res_y,$rp

	add	%sp,LOCALS64+$Hcub,$bp
	call	__ecp_nistz256_mul_mont_vis3	! p256_mul_mont(S2, in1_y, Hcub);
	add	%sp,LOCALS64+$S2,$rp

	ldx	[%sp+LOCALS64+$R],$bi
	ldx	[%sp+LOCALS64+$res_y],$a0
	ldx	[%sp+LOCALS64+$res_y+8],$a1
	ldx	[%sp+LOCALS64+$res_y+16],$a2
	ldx	[%sp+LOCALS64+$res_y+24],$a3
	add	%sp,LOCALS64+$R,$bp
	call	__ecp_nistz256_mul_mont_vis3	! p256_mul_mont(res_y, res_y, R);
	add	%sp,LOCALS64+$res_y,$rp

	add	%sp,LOCALS64+$S2,$bp
	call	__ecp_nistz256_sub_from_vis3	! p256_sub(res_y, res_y, S2);
	add	%sp,LOCALS64+$res_y,$rp

	ldx	[%fp+STACK_BIAS-16],$t1		! !in1infty
	ldx	[%fp+STACK_BIAS-8],$t2		! !in2infty
1:	call	.+8
	add	%o7,.Lone_mont_vis3-1b,$bp
___
for($i=0;$i<64;$i+=16) {			# conditional moves
$code.=<<___;
	ldx	[%sp+LOCALS64+$res_x+$i],$acc0	! res
	ldx	[%sp+LOCALS64+$res_x+$i+8],$acc1
	ldx	[%sp+LOCALS64+$in2_x+$i],$acc2	! in2
	ldx	[%sp+LOCALS64+$in2_x+$i+8],$acc3
	ldx	[%sp+LOCALS64+$in1_x+$i],$acc4	! in1
	ldx	[%sp+LOCALS64+$in1_x+$i+8],$acc5
	movrz	$t1,$acc2,$acc0
	movrz	$t1,$acc3,$acc1
	movrz	$t2,$acc4,$acc0
	movrz	$t2,$acc5,$acc1
	srlx	$acc0,32,$acc2
	srlx	$acc1,32,$acc3
	st	$acc0,[$rp_real+$i]
	st	$acc2,[$rp_real+$i+4]
	st	$acc1,[$rp_real+$i+8]
	st	$acc3,[$rp_real+$i+12]
___
}
for(;$i<96;$i+=16) {
$code.=<<___;
	ldx	[%sp+LOCALS64+$res_x+$i],$acc0	! res
	ldx	[%sp+LOCALS64+$res_x+$i+8],$acc1
	ldx	[$bp+$i-64],$acc2		! "in2"
	ldx	[$bp+$i-64+8],$acc3
	ldx	[%sp+LOCALS64+$in1_x+$i],$acc4	! in1
	ldx	[%sp+LOCALS64+$in1_x+$i+8],$acc5
	movrz	$t1,$acc2,$acc0
	movrz	$t1,$acc3,$acc1
	movrz	$t2,$acc4,$acc0
	movrz	$t2,$acc5,$acc1
	srlx	$acc0,32,$acc2
	srlx	$acc1,32,$acc3
	st	$acc0,[$rp_real+$i]
	st	$acc2,[$rp_real+$i+4]
	st	$acc1,[$rp_real+$i+8]
	st	$acc3,[$rp_real+$i+12]
___
}
$code.=<<___;
	ret
	restore
.type	ecp_nistz256_point_add_affine_vis3,#function
.size	ecp_nistz256_point_add_affine_vis3,.-ecp_nistz256_point_add_affine_vis3
.align	64
.Lone_mont_vis3:
.long	0x00000000,0x00000001, 0xffffffff,0x00000000
.long	0xffffffff,0xffffffff, 0x00000000,0xfffffffe
.align	64
___
}								}}}

# Purpose of these subroutines is to explicitly encode VIS instructions,
# so that one can compile the module without having to specify VIS
# extensions on compiler command line, e.g. -xarch=v9 vs. -xarch=v9a.
# Idea is to reserve for option to produce "universal" binary and let
# programmer detect if current CPU is VIS capable at run-time.
sub unvis3 {
my ($mnemonic,$rs1,$rs2,$rd)=@_;
my %bias = ( "g" => 0, "o" => 8, "l" => 16, "i" => 24 );
my ($ref,$opf);
my %visopf = (	"addxc"		=> 0x011,
		"addxccc"	=> 0x013,
		"umulxhi"	=> 0x016	);

    $ref = "$mnemonic\t$rs1,$rs2,$rd";

    if ($opf=$visopf{$mnemonic}) {
	foreach ($rs1,$rs2,$rd) {
	    return $ref if (!/%([goli])([0-9])/);
	    $_=$bias{$1}+$2;
	}

	return	sprintf ".word\t0x%08x !%s",
			0x81b00000|$rd<<25|$rs1<<14|$opf<<5|$rs2,
			$ref;
    } else {
	return $ref;
    }
}

foreach (split("\n",$code)) {
	s/\`([^\`]*)\`/eval $1/ge;

	s/\b(umulxhi|addxc[c]{0,2})\s+(%[goli][0-7]),\s*(%[goli][0-7]),\s*(%[goli][0-7])/
		&unvis3($1,$2,$3,$4)
	 /ge;

	print $_,"\n";
}

close STDOUT;