File: reken.c

package info (click to toggle)
form 5.0.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 8,308 kB
  • sloc: ansic: 110,546; cpp: 20,395; sh: 5,874; makefile: 545
file content (3992 lines) | stat: -rw-r--r-- 100,905 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
/** @file reken.c
 * 
 *  This file contains the numerical routines.
 *  The arithmetic in FORM is normally over the rational numbers.
 *	Hence there are routines for dealing with integers and with rational
 *	of 'arbitrary precision' (within limits)
 *	There are also routines for that calculus modulus an integer.
 *	In addition there are the routines for factorials and Bernoulli numbers.
 *	The random number function is currently only for internal purposes.
 */
/* #[ License : */
/*
 *   Copyright (C) 1984-2026 J.A.M. Vermaseren
 *   When using this file you are requested to refer to the publication
 *   J.A.M.Vermaseren "New features of FORM" math-ph/0010025
 *   This is considered a matter of courtesy as the development was paid
 *   for by FOM the Dutch physics granting agency and we would like to
 *   be able to track its scientific use to convince FOM of its value
 *   for the community.
 *
 *   This file is part of FORM.
 *
 *   FORM is free software: you can redistribute it and/or modify it under the
 *   terms of the GNU General Public License as published by the Free Software
 *   Foundation, either version 3 of the License, or (at your option) any later
 *   version.
 *
 *   FORM is distributed in the hope that it will be useful, but WITHOUT ANY
 *   WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 *   FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
 *   details.
 *
 *   You should have received a copy of the GNU General Public License along
 *   with FORM.  If not, see <http://www.gnu.org/licenses/>.
 */
/* #] License : */ 
/*
  	#[ Includes : reken.c
*/

#include "form3.h"
#include <math.h>

#ifdef WITHGMP
#include <gmp.h>
#define GMPSPREAD (GMP_LIMB_BITS/BITSINWORD)
#endif
 
#define GCDMAX 3

#define NEWTRICK 1
/*
  	#] Includes : 
  	#[ RekenRational :
 		#[ Pack :			void Pack(a,na,b,nb)

	Packs the contents of the numerator a and the denominator b into
	one normalized fraction a.

*/

void Pack(UWORD *a, WORD *na, UWORD *b, WORD nb)
{
	WORD c, sgn = 1, i;
	UWORD *to,*from;
	if ( (c = *na) == 0 ) {
		MLOCK(ErrorMessageLock);
		MesPrint("Caught a zero in Pack");
		MUNLOCK(ErrorMessageLock);
		return;
	}
	if ( nb == 0 ) {
		MLOCK(ErrorMessageLock);
		MesPrint("Division by zero in Pack");
		MUNLOCK(ErrorMessageLock);
		return;
	}
	if ( *na < 0 ) { sgn = -sgn; c = -c; }
	if ( nb < 0 ) { sgn = -sgn; nb = -nb; }
	*na = MaX(c,nb);
	to = a + c;
	i = *na - c;
	while ( --i >= 0 ) *to++ = 0;
	i = *na - nb;
	from = b;
	NCOPY(to,from,nb);
	while ( --i >= 0 ) *to++ = 0;
	if ( sgn < 0 ) *na = -*na;
}

/*
 		#] Pack : 
 		#[ UnPack :			void UnPack(a,na,denom,numer)

	Determines the sizes of the numerator and the denominator in the
	normalized fraction a with length na.

*/

void UnPack(UWORD *a, WORD na, WORD *denom, WORD *numer)
{
	UWORD *pos;
	WORD i, sgn = na;
	if ( na < 0 ) { na = -na; }
	i = na;
	if ( i > 1 ) {		/* Find the respective leading words */
		a += i;
		a--;
		pos = a + i;
		while ( !(*a) ) { i--; a--; }
		while ( !(*pos) ) { na--; pos--; }
	}
	*denom = na;
	if ( sgn < 0 ) i = -i;
	*numer = i;
}

/*
 		#] UnPack : 
 		#[ Mully :			WORD Mully(a,na,b,nb)

	Multiplies the rational a by the Long b.

*/

int Mully(PHEAD UWORD *a, WORD *na, UWORD *b, WORD nb)
{
	GETBIDENTITY
	UWORD *d, *e;
	WORD i, sgn = 1;
	WORD nd, ne, adenom, anumer;
	if ( !nb ) { *na = 0; return(0); }
	else if ( *b == 1 ) {
		if ( nb == 1 ) return(0);
		else if ( nb == -1 ) { *na = -*na; return(0); }
	}
	if ( *na < 0 ) { sgn = -sgn; *na = -*na; }
	if ( nb < 0 ) { sgn = -sgn; nb = -nb; }
	UnPack(a,*na,&adenom,&anumer);
	d = NumberMalloc("Mully"); e = NumberMalloc("Mully");
	for ( i = 0; i < nb; i++ ) { e[i] = *b++; }
	ne = nb;
	if ( Simplify(BHEAD a+*na,&adenom,e,&ne) ) goto MullyEr;
	if ( MulLong(a,anumer,e,ne,d,&nd) ) goto MullyEr;
	b = a+*na;
	for ( i = 0; i < *na; i++ ) { e[i] = *b++; }
	ne = adenom;
	*na = nd;
	b = d;
	*na = nd;
	for ( i = 0; i < *na; i++ ) { a[i] = *b++; }
	Pack(a,na,e,ne);
	if ( sgn < 0 ) *na = -*na;
	NumberFree(d,"Mully"); NumberFree(e,"Mully");
	return(0);
MullyEr:
	MLOCK(ErrorMessageLock);
	MesCall("Mully");
	MUNLOCK(ErrorMessageLock);
	NumberFree(d,"Mully"); NumberFree(e,"Mully");
	SETERROR(-1)
}

/*
 		#] Mully : 
 		#[ Divvy :			WORD Divvy(a,na,b,nb)

	Divides the rational a by the Long b.

*/

int Divvy(PHEAD UWORD *a, WORD *na, UWORD *b, WORD nb)
{
	GETBIDENTITY
	UWORD *d,*e;
	WORD i, sgn = 1;
	WORD nd, ne, adenom, anumer;
	if ( !nb ) {
		MLOCK(ErrorMessageLock);
		MesPrint("Division by zero in Divvy");
		MUNLOCK(ErrorMessageLock);
		return(-1);
	}
	d = NumberMalloc("Divvy"); e = NumberMalloc("Divvy");
	if ( nb < 0 ) { sgn = -sgn; nb = -nb; }
	if ( *na < 0 ) { sgn = -sgn; *na = -*na; }
	UnPack(a,*na,&adenom,&anumer);
	for ( i = 0; i < nb; i++ ) { e[i] = *b++; }
	ne = nb;
	if ( Simplify(BHEAD a,&anumer,e,&ne) ) goto DivvyEr;
	if ( MulLong(a+*na,adenom,e,ne,d,&nd) ) goto DivvyEr;
	*na = anumer;
	Pack(a,na,d,nd);
	if ( sgn < 0 ) *na = -*na;
	NumberFree(d,"Divvy"); NumberFree(e,"Divvy");
	return(0);
DivvyEr:
	MLOCK(ErrorMessageLock);
	MesCall("Divvy");
	MUNLOCK(ErrorMessageLock);
	NumberFree(d,"Divvy"); NumberFree(e,"Divvy");
	SETERROR(-1)
}

/*
 		#] Divvy : 
 		#[ AddRat :			WORD AddRat(a,na,b,nb,c,nc)
*/

int AddRat(PHEAD UWORD *a, WORD na, UWORD *b, WORD nb, UWORD *c, WORD *nc)
{
	GETBIDENTITY
	UWORD *d, *e, *f, *g;
	WORD nd, ne, nf, ng, adenom, anumer, bdenom, bnumer;
	if ( !na ) {
		WORD i;
		*nc = nb;
		if ( nb < 0 ) nb = -nb;
		nb *= 2;
		for ( i = 0; i < nb; i++ ) *c++ = *b++;
		return(0);
	}
	else if ( !nb ) {
		WORD i;
		*nc = na;
		if ( na < 0 ) na = -na;
		na *= 2;
		for ( i = 0; i < na; i++ ) *c++ = *a++;
		return(0);
	}
	else if ( b[1] == 1 && a[1] == 1 ) {
		if ( na == 1 ) {
			if ( nb == 1 ) {
				*c = *a + *b;
				c[1] = 1;
				if ( *c < *a ) { c[2] = 1; c[3] = 0; *nc = 2; }
				else { *nc = 1; }
				return(0);
			}
			else if ( nb == -1 ) {
				if ( *b > *a ) { 
					*c = *b - *a; *nc = -1;
				}
				else if ( *b < *a ) {
					*c = *a - *b; *nc = 1;
				}
				else *nc = 0;
				c[1] = 1;
				return(0);
			}
		}
		else if ( na == -1 ){
			if ( nb == -1 ) {
				c[1] = 1;
				*c = *a + *b;
				if ( *c < *a ) { c[2] = 1; c[3] = 0; *nc = -2; }
				else { *nc = -1; }
				return(0);
			}
			else if ( nb == 1 ) {
				if ( *b > *a ) {
					*c = *b - *a; *nc = 1;
				}
				else if ( *b < *a ) {
					*c = *a - *b; *nc = -1;
				}
				else *nc = 0;
				c[1] = 1;
				return(0);
			}
		}
	}
	UnPack(a,na,&adenom,&anumer);
	UnPack(b,nb,&bdenom,&bnumer);
	if ( na < 0 ) na = -na;
	if ( nb < 0 ) nb = -nb;
	if ( na == 1 && nb == 1 ) {
		RLONG t1, t2, t3;
		t3 = ((RLONG)a[1])*((RLONG)b[1]);
		t1 = ((RLONG)a[0])*((RLONG)b[1]);
		t2 = ((RLONG)a[1])*((RLONG)b[0]);
		if ( ( anumer > 0 && bnumer > 0 ) || ( anumer < 0 && bnumer < 0 ) ) {
			if ( ( t1 = t1 + t2 ) < t2 ) {
				c[2] = 1;
				c[0] = (UWORD)t1;
				c[1] = (UWORD)(t1 >> BITSINWORD);
				*nc = 3;
			}
			else {
				c[0] = (UWORD)t1;
				if ( ( c[1] = (UWORD)(t1 >> BITSINWORD) ) != 0 ) *nc = 2;
				else *nc = 1;
			}
		}
		else {
			if ( t1 == t2 ) { *nc = 0; return(0); }
			if ( t1 > t2 ) {
				t1 -= t2;
			}
			else {
				t1 = t2 - t1;
				anumer = -anumer;
			}
			c[0] = (UWORD)t1;
			if ( ( c[1] = (UWORD)(t1 >> BITSINWORD) ) != 0 ) *nc = 2;
			else *nc = 1;
		}
		if ( anumer < 0 ) *nc = -*nc;
		d = NumberMalloc("AddRat");
		d[0] = (UWORD)t3;
		if ( ( d[1] = (UWORD)(t3 >> BITSINWORD) ) != 0 ) nd = 2;
		else nd = 1;
		if ( Simplify(BHEAD c,nc,d,&nd) ) goto AddRer1;
	}
/*
	else if ( a[na] == 1 && b[nb] == 1 && adenom == 1 && bdenom == 1 ) {
		if ( AddLong(a,na,b,nb,c,&nc) ) goto AddRer2;
		i = ABS(nc); d = c + i; *d++ = 1;
		while ( --i > 0 ) *d++ = 0 ;
		return(0);
	}
*/
	else {
		d = NumberMalloc("AddRat"); e = NumberMalloc("AddRat");
		f = NumberMalloc("AddRat"); g = NumberMalloc("AddRat");
		if ( GcdLong(BHEAD a+na,adenom,b+nb,bdenom,d,&nd) ) goto AddRer;
		if ( *d == 1 && nd == 1 ) nd = 0;
		if ( nd ) {
			if ( DivLong(a+na,adenom,d,nd,e,&ne,c,nc) ) goto AddRer;
			if ( DivLong(b+nb,bdenom,d,nd,f,&nf,c,nc) ) goto AddRer;
			if ( MulLong(a,anumer,f,nf,c,nc) ) goto AddRer;
			if ( MulLong(b,bnumer,e,ne,g,&ng) ) goto AddRer;
		}
		else {
			if ( MulLong(a+na,adenom,b,bnumer,c,nc) ) goto AddRer;
			if ( MulLong(b+nb,bdenom,a,anumer,g,&ng) ) goto AddRer;
		}
		if ( AddLong(c,*nc,g,ng,c,nc) ) goto AddRer;
		if ( !*nc ) {
			NumberFree(g,"AddRat"); NumberFree(f,"AddRat");
			NumberFree(e,"AddRat"); NumberFree(d,"AddRat");
			return(0);
		}
		if ( nd ) {
			if ( Simplify(BHEAD c,nc,d,&nd) ) goto AddRer;
			if ( MulLong(e,ne,d,nd,g,&ng) ) goto AddRer;
			if ( MulLong(g,ng,f,nf,d,&nd) ) goto AddRer;
		}
		else {
			if ( MulLong(a+na,adenom,b+nb,bdenom,d,&nd) ) goto AddRer;
		}
		NumberFree(g,"AddRat"); NumberFree(f,"AddRat"); NumberFree(e,"AddRat");
	}
	Pack(c,nc,d,nd);
	NumberFree(d,"AddRat");
	return(0);
AddRer:
	NumberFree(g,"AddRat"); NumberFree(f,"AddRat"); NumberFree(e,"AddRat");
AddRer1:
	NumberFree(d,"AddRat");
/* AddRer2: */
	MLOCK(ErrorMessageLock);
	MesCall("AddRat");
	MUNLOCK(ErrorMessageLock);
	SETERROR(-1)
}

/*
 		#] AddRat : 
 		#[ MulRat :			WORD MulRat(a,na,b,nb,c,nc)

	Multiplies the rationals a and b. The Gcd of the individual
	pieces is divided out first to minimize the chances of spurious
	overflows.

*/

int MulRat(PHEAD UWORD *a, WORD na, UWORD *b, WORD nb, UWORD *c, WORD *nc)
{
	WORD i;
	WORD sgn = 1;
	if ( *b == 1 && b[1] == 1 ) {
		if ( nb == 1 ) {
			*nc = na;
			i = ABS(na); i *= 2;
			while ( --i >= 0 ) *c++ = *a++;
			return(0);
		}
		else if ( nb == -1 ) {
			*nc = - na;
			i = ABS(na); i *= 2;
			while ( --i >= 0 ) *c++ = *a++;
			return(0);
		}
	}
	if ( *a == 1 && a[1] == 1 ) {
		if ( na == 1 ) {
			*nc = nb;
			i = ABS(nb); i *= 2;
			while ( --i >= 0 ) *c++ = *b++;
			return(0);
		}
		else if ( na == -1 ) {
			*nc = - nb;
			i = ABS(nb); i *= 2;
			while ( --i >= 0 ) *c++ = *b++;
			return(0);
		}
	}
	if ( na < 0 ) { na = -na; sgn = -sgn; }
	if ( nb < 0 ) { nb = -nb; sgn = -sgn; }
	if ( !na || !nb ) { *nc = 0; return(0); }
	if ( na != 1 || nb != 1 ) {
		GETBIDENTITY
		UWORD *xd,*xe, *xf,*xg;
		WORD dden, dnumr, eden, enumr;
		UnPack(a,na,&dden,&dnumr);
		UnPack(b,nb,&eden,&enumr);
		xd = NumberMalloc("MulRat"); xf = NumberMalloc("MulRat");
		for ( i = 0; i < dnumr; i++ ) xd[i] = a[i];
		a += na;
		for ( i = 0; i < dden; i++ ) xf[i] = a[i];
		xe = NumberMalloc("MulRat"); xg = NumberMalloc("MulRat");
		for ( i = 0; i < enumr; i++ ) xe[i] = b[i];
		b += nb;
		for ( i = 0; i < eden; i++ ) xg[i] = b[i];
		if ( Simplify(BHEAD xd,&dnumr,xg,&eden) ||
		     Simplify(BHEAD xe,&enumr,xf,&dden) ||
		     MulLong(xd,dnumr,xe,enumr,c,nc) ||
		     MulLong(xf,dden,xg,eden,xd,&dnumr) ) {
			MLOCK(ErrorMessageLock);
			MesCall("MulRat");
			MUNLOCK(ErrorMessageLock);
			NumberFree(xd,"MulRat"); NumberFree(xe,"MulRat"); NumberFree(xf,"MulRat"); NumberFree(xg,"MulRat");
			SETERROR(-1)
		}
		Pack(c,nc,xd,dnumr);
		NumberFree(xd,"MulRat"); NumberFree(xe,"MulRat"); NumberFree(xf,"MulRat"); NumberFree(xg,"MulRat");
	}
	else {
		UWORD y;
		UWORD a0,a1,b0,b1;
		RLONG xx;
		y = a[0]; b1=b[1];
		do { a0 = y % b1; y = b1; } while ( ( b1 = a0 ) != 0 );
		if ( y != 1 ) {
			a0 = a[0] / y;
			b1 = b[1] / y;
		}
		else {
			a0 = a[0];
			b1 = b[1];
		}
		y=b[0]; a1=a[1];
		do { b0 = y % a1; y = a1; } while ( ( a1 = b0 ) != 0 );
		if ( y != 1 ) {
			a1 = a[1] / y;
			b0 = b[0] / y;
		}
		else {
			a1 = a[1];
			b0 = b[0];
		}
		xx = ((RLONG)a0)*b0;
		if ( xx & AWORDMASK ) {
			*nc = 2;
			c[0] = (UWORD)xx;
			c[1] = (UWORD)(xx >> BITSINWORD);
			xx = ((RLONG)a1)*b1;
			c[2] = (UWORD)xx;
			c[3] = (UWORD)(xx >> BITSINWORD);
		}
		else {
			c[0] = (UWORD)xx;
			xx = ((RLONG)a1)*b1;
			if ( xx & AWORDMASK ) {
				c[1] = 0;
				c[2] = (UWORD)xx;
				c[3] = (UWORD)(xx >> BITSINWORD);
				*nc = 2;
			}
			else {
				c[1] = (UWORD)xx;
				*nc = 1;
			}
		}
	}
	if ( sgn < 0 ) *nc = -*nc;
	return(0);
}

/*
 		#] MulRat : 
 		#[ DivRat :			WORD DivRat(a,na,b,nb,c,nc)

	Divides the rational a by the rational b.

*/

int DivRat(PHEAD UWORD *a, WORD na, UWORD *b, WORD nb, UWORD *c, WORD *nc)
{
	GETBIDENTITY
	WORD i, j;
	int ret;
	UWORD *xd,*xe,xx;
	if ( !nb ) {
		MLOCK(ErrorMessageLock);
		MesPrint("Rational division by zero");
		MUNLOCK(ErrorMessageLock);
		return(-1);
	}
	j = i = (nb >= 0)? nb: -nb;
	xd = b; xe = b + i;
	do { xx = *xd; *xd++ = *xe; *xe++ = xx; } while ( --j > 0 );
	ret = MulRat(BHEAD a,na,b,nb,c,nc);
	xd = b; xe = b + i;
	do { xx = *xd; *xd++ = *xe; *xe++ = xx; } while ( --i > 0 );
	return(ret);
}

/*
 		#] DivRat : 
 		#[ Simplify :		WORD Simplify(a,na,b,nb)

	Determines the greatest common denominator of a and b and
	divides both by it. A possible sign is put in a. This is
	the simplification of the fraction a/b.

*/

int Simplify(PHEAD UWORD *a, WORD *na, UWORD *b, WORD *nb)
{
	GETBIDENTITY
	UWORD *x1,*x2,*x3;
	UWORD *x4;
	WORD n1,n2,n3,n4,sgn = 1;
	WORD i;
	UWORD *Siscrat5, *Siscrat6, *Siscrat7, *Siscrat8;
	if ( *na < 0 ) { *na = -*na; sgn = -sgn; }
	if ( *nb < 0 ) { *nb = -*nb; sgn = -sgn; }
	Siscrat5 = NumberMalloc("Simplify"); Siscrat6 = NumberMalloc("Simplify"); 
	Siscrat7 = NumberMalloc("Simplify"); Siscrat8 = NumberMalloc("Simplify"); 
	x1 = Siscrat8; x2 = Siscrat7;
	if ( *nb == 1 ) {
		x3 = Siscrat6;
		if ( DivLong(a,*na,b,*nb,x1,&n1,x2,&n2) ) goto SimpErr;
		if ( !n2 ) {
			for ( i = 0; i < n1; i++ ) *a++ = *x1++;
			*na = n1;
			*b = 1;
		}
		else {
			UWORD y1, y2, y3;
			y2 = *b;
			y3 = *x2;
			do { y1 = y2 % y3; y2 = y3; } while ( ( y3 = y1 ) != 0 );
			if ( ( *x2 = y2 ) != 1 ) {
				*b /= y2;
				if ( DivLong(a,*na,x2,(WORD)1,x1,&n1,x3,&n3) ) goto SimpErr;
				for ( i = 0; i < n1; i++ ) *a++ = *x1++;
				*na = n1;
			}
		}
	}
#ifdef NEWTRICK
	else if ( *na >= GCDMAX && *nb >= GCDMAX ) {
		n1 = i = *na; x3 = a;
		NCOPY(x1,x3,i);
		x3 = b; n2 = i = *nb;
		NCOPY(x2,x3,i);
		x4 = Siscrat5;
		x2 = Siscrat6;
		x3 = Siscrat7;
		if ( GcdLong(BHEAD Siscrat8,n1,Siscrat7,n2,x2,&n3) ) goto SimpErr;
		n2 = n3;
		if ( *x2 != 1 || n2 != 1 ) {
			DivLong(a,*na,x2,n2,x1,&n1,x4,&n4);
			*na = i = n1;
			NCOPY(a,x1,i);
			DivLong(b,*nb,x2,n2,x3,&n3,x4,&n4);
			*nb = i = n3;
			NCOPY(b,x3,i);
		}
	}
#endif
	else {
		x4 = Siscrat5;
		n1 = i = *na; x3 = a;
		NCOPY(x1,x3,i);
		x3 = b; n2 = i = *nb;
		NCOPY(x2,x3,i);
		x1 = Siscrat8; x2 = Siscrat7; x3 = Siscrat6;
		for(;;){
			if ( DivLong(x1,n1,x2,n2,x4,&n4,x3,&n3) ) goto SimpErr;
			if ( !n3 ) break;
			if ( n2 == 1 ) {
				while ( ( *x1 = (*x2) % (*x3) ) != 0 ) { *x2 = *x3; *x3 = *x1; }
				*x2 = *x3;
				break;
			}
			if ( DivLong(x2,n2,x3,n3,x4,&n4,x1,&n1) ) goto SimpErr;
			if ( !n1 ) { x2 = x3; n2 = n3; x3 = Siscrat7; break; }
			if ( n3 == 1 ) {
				while ( ( *x2 = (*x3) % (*x1) ) != 0 ) { *x3 = *x1; *x1 = *x2; }
				*x2 = *x1;
				n2 = 1;
				break;
			}
			if ( DivLong(x3,n3,x1,n1,x4,&n4,x2,&n2) ) goto SimpErr;
			if ( !n2 ) { x2 = x1; n2 = n1; x1 = Siscrat7; break; }
			if ( n1 == 1 ) {
				while ( ( *x3 = (*x1) % (*x2) ) != 0 ) { *x1 = *x2; *x2 = *x3; }
				break;
			}
		}
		if ( *x2 != 1 || n2 != 1 ) {
			DivLong(a,*na,x2,n2,x1,&n1,x4,&n4);
			*na = i = n1;
			NCOPY(a,x1,i);
			DivLong(b,*nb,x2,n2,x3,&n3,x4,&n4);
			*nb = i = n3;
			NCOPY(b,x3,i);
		}
	}
	if ( sgn < 0 ) *na = -*na;
	NumberFree(Siscrat5,"Simplify"); NumberFree(Siscrat6,"Simplify");
	NumberFree(Siscrat7,"Simplify"); NumberFree(Siscrat8,"Simplify");
	return(0);
SimpErr:
	MLOCK(ErrorMessageLock);
	MesCall("Simplify");
	MUNLOCK(ErrorMessageLock);
	NumberFree(Siscrat5,"Simplify"); NumberFree(Siscrat6,"Simplify");
	NumberFree(Siscrat7,"Simplify"); NumberFree(Siscrat8,"Simplify");
	SETERROR(-1)
}

/*
 		#] Simplify : 
 		#[ AccumGCD :		WORD AccumGCD(PHEAD a,na,b,nb)

		Routine takes the rational GCD of the fractions in a and b and
		replaces a by the GCD of the two.
		The rational GCD is defined as the rational that consists of
		the GCD of the numerators divided by the GCD of the denominators
*/

int AccumGCD(PHEAD UWORD *a, WORD *na, UWORD *b, WORD nb)
{
	GETBIDENTITY
	WORD nna,nnb,numa,numb,dena,denb,numc,denc;
	UWORD *GCDbuffer = NumberMalloc("AccumGCD");
	int i;
	nna = *na; if ( nna < 0 ) nna = -nna; nna = (nna-1)/2;
	nnb = nb;  if ( nnb < 0 ) nnb = -nnb; nnb = (nnb-1)/2;
	UnPack(a,nna,&dena,&numa);
	UnPack(b,nnb,&denb,&numb);
	if ( GcdLong(BHEAD a,numa,b,numb,GCDbuffer,&numc) ) goto AccErr;
	numa = numc;
	for ( i = 0; i < numa; i++ ) a[i] = GCDbuffer[i];
	if ( GcdLong(BHEAD a+nna,dena,b+nnb,denb,GCDbuffer,&denc) ) goto AccErr;
	dena = denc;
	for ( i = 0; i < dena; i++ ) a[i+nna] = GCDbuffer[i];
	Pack(a,&numa,a+nna,dena);
	*na = INCLENG(numa);
	NumberFree(GCDbuffer,"AccumGCD");
	return(0);
AccErr:
	MLOCK(ErrorMessageLock);
	MesCall("AccumGCD");
	MUNLOCK(ErrorMessageLock);
	NumberFree(GCDbuffer,"AccumGCD");
	SETERROR(-1)
}

/*
 		#] AccumGCD : 
 		#[ TakeRatRoot:
*/

int TakeRatRoot(UWORD *a, WORD *n, WORD power)
{
	WORD numer,denom, nn;
	if ( ( power & 1 ) == 0 && *n < 0 ) return(1);
	if ( ABS(*n) == 1 && a[0] == 1 && a[1] == 1 ) return(0);
	nn = ABS(*n);
	UnPack(a,nn,&denom,&numer);
	if ( TakeLongRoot(a+nn,&denom,power) ) return(1);
	if ( TakeLongRoot(a,&numer,power) ) return(1);
	Pack(a,&numer,a+nn,denom);
	if ( *n < 0 ) *n = -numer;
	else          *n = numer;
	return(0);
}

/*
 		#] TakeRatRoot: 
  	#] RekenRational : 
  	#[ RekenLong :
 		#[ AddLong :		WORD AddLong(a,na,b,nb,c,nc)

	Long addition. Uses addition and subtraction of positive numbers.

*/

int AddLong(UWORD *a, WORD na, UWORD *b, WORD nb, UWORD *c, WORD *nc)
{
	WORD sgn, res;
	if ( na < 0 ) {
		if ( nb < 0 ) {
			if ( AddPLon(a,-na,b,-nb,c,nc) ) return(-1);
			*nc = -*nc;
			return(0);
		}
		else {
			na = -na;
			sgn = -1;
		}
	}
	else {
		if ( nb < 0 ) {
			nb = -nb;
			sgn = 1;
		}
		else { return( AddPLon(a,na,b,nb,c,nc) ); }
	}
	if ( ( res = BigLong(a,na,b,nb) ) > 0 ) {
		SubPLon(a,na,b,nb,c,nc);
		if ( sgn < 0 ) *nc = -*nc;
	}
	else if ( res < 0 ) {
		SubPLon(b,nb,a,na,c,nc);
		if ( sgn > 0 ) *nc = -*nc;
	}
	else {
		*nc = 0;
		*c = 0;
	}
	return(0);
}

/*
 		#] AddLong : 
 		#[ AddPLon :		WORD AddPLon(a,na,b,nb,c,nc)

	Adds two long integers a and b and puts the result in c.
	The length of a and b are na and nb. The length of c is returned in nc.
	c can be a or b.
*/

int AddPLon(UWORD *a, WORD na, UWORD *b, WORD nb, UWORD *c, WORD *nc)
{
	UWORD carry = 0, e, nd = 0;
	while ( na && nb ) {
		e = *a;
		*c = e + *b + carry;
		if ( carry ) {
			if ( e < *c ) carry = 0;
		}
		else {
			if ( e > *c ) carry = 1;
		}
		a++; b++; c++; nd++; na--; nb--;
	}
	while ( na ) {
		if ( carry ) {
			*c = *a++ + carry;
			if ( *c++ ) carry = 0;
		}
		else *c++ = *a++;
		nd++; na--;
	}
	while ( nb ) {
		if ( carry ) {
			*c = *b++ + carry;
			if ( *c++ ) carry = 0;
		}
		else *c++ = *b++;
		nd++; nb--;
	}
	if ( carry ) {
		nd++;
		if ( nd > (UWORD)AM.MaxTal ) {
			MLOCK(ErrorMessageLock);
			MesPrint("Overflow in addition");
			MUNLOCK(ErrorMessageLock);
			return(-1);
		}
		*c++ = carry;
	}
	*nc = nd;
	return(0);
}

/*
 		#] AddPLon : 
 		#[ SubPLon :		void SubPLon(a,na,b,nb,c,nc)

	Subtracts b from a. Assumes that a > b. Result in c.
	c can be a or b.

*/

void SubPLon(UWORD *a, WORD na, UWORD *b, WORD nb, UWORD *c, WORD *nc)
{
	UWORD borrow = 0, e, nd = 0;
	while ( nb ) {
		e = *a;
		if ( borrow ) {
			*c = e - *b - borrow;
			if ( *c < e ) borrow = 0;
		}
		else {
			*c = e - *b;
			if ( *c > e ) borrow = 1;
		}
		a++; b++; c++; na--; nb--; nd++;
	}
	while ( na ) {
		if ( borrow ) {
			if ( *a ) { *c++ = *a++ - 1; borrow = 0; }
			else { *c++ = (UWORD)(-1); a++; }
		}
		else *c++ = *a++;
		na--; nd++;
	}
	while ( nd && !*--c ) { nd--; }
	*nc = (WORD)nd;
}

/*
 		#] SubPLon : 
 		#[ MulLong :		WORD MulLong(a,na,b,nb,c,nc)

	Does a Long multiplication. Assumes that WORD is half the size
	of a LONG to work out the scheme! The number of operations is
	the canonical na*nm multiplications.
	c should not overlap with a or b.

*/

int MulLong(UWORD *a, WORD na, UWORD *b, WORD nb, UWORD *c, WORD *nc)
{
	WORD sgn = 1;
	UWORD i, *ic, *ia;
	RLONG t, bb;
	if ( !na || !nb ) { *nc = 0; return(0); }
	if ( na < 0 ) { na = -na; sgn = -sgn; }
	if ( nb < 0 ) { nb = -nb; sgn = -sgn; }
	*nc = i = na + nb;
	if ( i > (UWORD)(AM.MaxTal+1) ) goto MulLov;
	ic = c;
/*
  	#[ GMP stuff :
*/
#ifdef WITHGMP
	if (na > 3 && nb > 3) {
/*		mp_limb_t res;  */
		UWORD *to, *from;
		int j;
		GETIDENTITY
		UWORD *DLscrat9 = NumberMalloc("MulLong"), *DLscratA = NumberMalloc("MulLong"), *DLscratB = NumberMalloc("MulLong");
#if ( GMPSPREAD != 1 )
		if ( na & 1 ) {
			from = a; a = to = DLscrat9; j = na; NCOPY(to, from, j);
			a[na++] = 0;
			++*nc;
		} else
#endif
		if ( (LONG)a & (sizeof(mp_limb_t)-1) ) {
			from = a; a = to = DLscrat9; j = na; NCOPY(to, from, j);
		}

#if ( GMPSPREAD != 1 )
		if ( nb & 1 ) {
			from = b; b = to = DLscratA; j = nb; NCOPY(to, from, j);
			b[nb++] = 0;
			++*nc;
		} else
#endif
		if ( (LONG)b & (sizeof(mp_limb_t)-1) ) {
			from = b; b = to = DLscratA; j = nb; NCOPY(to, from, j);
		}

		if ( ( *nc > (WORD)i ) || ( (LONG)c & (LONG)(sizeof(mp_limb_t)-1) ) ) {
			ic = DLscratB;
		}
		if ( na < nb ) {
			/* res = */
			mpn_mul((mp_ptr)ic, (mp_srcptr)b, nb/GMPSPREAD, (mp_srcptr)a, na/GMPSPREAD);
		} else {
			/* res = */
			mpn_mul((mp_ptr)ic, (mp_srcptr)a, na/GMPSPREAD, (mp_srcptr)b, nb/GMPSPREAD);
		}
		while ( ic[i-1] == 0 ) i--;
		*nc = i;
/*
		if ( res == 0 ) *nc -= GMPSPREAD;
		else if ( res <= WORDMASK ) --*nc;
*/
		if ( ic != c ) {
			j = *nc; NCOPY(c, ic, j);
		}
		if ( sgn < 0 ) *nc = -(*nc);
		NumberFree(DLscrat9,"MulLong"); NumberFree(DLscratA,"MulLong"); NumberFree(DLscratB,"MulLong");
		return(0);
	}
#endif
/*
  	#] GMP stuff : 
*/
	do { *ic++ = 0; } while ( --i > 0 );
	do {
		ia = a;
		ic = c++;
		t = 0;
		i = na;
		bb = (RLONG)(*b++);
		do {
			t = (*ia++) * bb + t + *ic;
			*ic++ = (WORD)t;
			t >>= BITSINWORD;		/* should actually be a swap */
		} while ( --i > 0 );
		if ( t ) *ic = (UWORD)t;
	} while ( --nb > 0 );
	if ( !*ic ) (*nc)--;
	if ( *nc > AM.MaxTal ) goto MulLov;
	if ( sgn < 0 ) *nc = -(*nc);
	return(0);
MulLov:
	MLOCK(ErrorMessageLock);
	MesPrint("Overflow in Multiplication");
	MUNLOCK(ErrorMessageLock);
	return(-1);
}

/*
 		#] MulLong : 
 		#[ BigLong :		WORD BigLong(a,na,b,nb)

	Returns > 0 if a > b, < 0 if b > a and 0 if a == b

*/

int BigLong(UWORD *a, WORD na, UWORD *b, WORD nb)
{
	a += na;
	b += nb;
	while ( na && !*--a ) na--;
	while ( nb && !*--b ) nb--;
	if ( nb < na ) return(1);
	if ( nb > na ) return(-1);
	while ( --na >= 0 ) {
		if ( *a > *b ) return(1);
		else if ( *b > *a ) return(-1);
		a--; b--;
	}
	return(0);
}

/*
 		#] BigLong : 
 		#[ DivLong :		WORD DivLong(a,na,b,nb,c,nc,d,nd)

	This is the long division which knows a couple of exceptions.
	It uses therefore a recursive call for the renormalization.
	The quotient comes in c and the remainder in d.
	d may be overlapping with b. It may also be identical to a.
	c should not overlap with a, but it can overlap with b.

*/

int DivLong(UWORD *a, WORD na, UWORD *b, WORD nb, UWORD *c,
            WORD *nc, UWORD *d, WORD *nd)
{
	WORD sgna = 1, sgnb = 1, ne, nf, ng, nh;
	WORD i, ni;
	UWORD *w1, *w2;
	RLONG t, v;
	UWORD *e, *f, *ff, *g, norm, estim;
#ifdef WITHGMP
	UWORD *DLscrat9, *DLscratA, *DLscratB, *DLscratC;
#endif
	RLONG esthelp;
	if ( !nb ) {
		MLOCK(ErrorMessageLock);
		MesPrint("Division by zero");
		MUNLOCK(ErrorMessageLock);
		return(-1);
	}
	if ( !na ) { *nc = *nd = 0; return(0); }
	if ( na < 0 ) { sgna = -sgna; na = -na; }
	if ( nb < 0 ) { sgnb = -sgnb; nb = -nb; }
	if ( na < nb ) {
		for ( i = 0; i < na; i++ ) *d++ = *a++;
		*nd = na;
		*nc = 0;
	}
	else if ( nb == na && ( i = BigLong(b,nb,a,na) ) >= 0 ) {
		if ( i > 0 ) {
			for ( i = 0; i < na; i++ ) *d++ = *a++;
			*nd = na;
			*nc = 0;
		}
		else {
			*c = 1;
			*nc = 1;
			*nd = 0;
		}
	}
	else if ( nb == 1 ) {
		if ( *b == 1 ) {
			for ( i = 0; i < na; i++ ) *c++ = *a++;
			*nc = na;
			*nd = 0;
		}
		else {
			w1 = a+na;
			*nc = ni = na;
			*nd = 1;
			w2 = c+ni;
			v = (RLONG)(*b);
			t = (RLONG)(*--w1);
			while ( --ni >= 0 ) {
				*--w2 = t / v;
				t -= v * (*w2);
				if ( ni ) {
					t <<= BITSINWORD;
					t += *--w1;
				}
			}
			if ( ( *d = (UWORD)t ) == 0 ) *nd = 0;
			if ( !*(c+na-1) ) (*nc)--;
		}
	}
	else {
		GETIDENTITY
/*
 		#[ GMP stuff :

		We start with copying a and b.
		Then we make space for c and d.
		Next we call mpn_tdiv_qr
		We adjust sizes and copy to c and d if needed.
		Finally the signs are settled.
*/
#ifdef WITHGMP
		if ( na > 4 && nb > 3 ) {
		  UWORD *ic, *id, *to, *from;
		  int j = na - nb;
		  DLscrat9 = NumberMalloc("DivLong"); DLscratA = NumberMalloc("DivLong");
		  DLscratB = NumberMalloc("DivLong"); DLscratC = NumberMalloc("DivLong");

#if ( GMPSPREAD != 1 )
		  if ( na & 1 ) {
			from = a; a = to = DLscrat9; i = na; NCOPY(to, from, i);
			a[na++] = 0;
		  } else
#endif
		  if ( (LONG)a & (sizeof(mp_limb_t)-1) ) {
			from = a; a = to = DLscrat9; i = na; NCOPY(to, from, i);
		  }

#if ( GMPSPREAD != 1 )
		  if ( nb & 1 ) {
			from = b; b = to = DLscratA; i = nb; NCOPY(to, from, i);
			b[nb++] = 0;
		  } else
#endif
		  if ( ( (LONG)b & (sizeof(mp_limb_t)-1) ) != 0 ) {
			from = b; b = to = DLscratA; i = nb; NCOPY(to, from, i);
		  }
#if ( GMPSPREAD != 1 )
/*
          Not recognizing this case was a nasty and extremely rare bug.
          In the case that c == a and na is odd and nc == na and b
          still needs to be used, the least significant UWORD of b got
          overwritten by zero. (22-mar-2023)
*/
		  ic = DLscratB; id = DLscratC;
#else
		  if ( ( (LONG)c & (sizeof(mp_limb_t)-1) ) != 0 ) ic = DLscratB;
		  else                                            ic = c;

		  if ( ( (LONG)d & (sizeof(mp_limb_t)-1) ) != 0 ) id = DLscratC;
		  else                                            id = d;
#endif
		  mpn_tdiv_qr((mp_limb_t *)ic,(mp_limb_t *)id,(mp_size_t)0,
			(const mp_limb_t *)a,(mp_size_t)(na/GMPSPREAD),
			(const mp_limb_t *)b,(mp_size_t)(nb/GMPSPREAD));
		  while ( j >= 0 && ic[j] == 0 ) j--;
		  j++; *nc = j;
		  if ( c != ic ) { NCOPY(c,ic,j); }
		  j = nb-1;
		  while ( j >= 0 && id[j] == 0 ) j--;
		  j++; *nd = j;
		  if ( d != id ) { NCOPY(d,id,j); }
		  if ( sgna < 0 ) { *nc = -(*nc); *nd = -(*nd); }
		  if ( sgnb < 0 ) { *nc = -(*nc); }
		  NumberFree(DLscrat9,"DivLong"); NumberFree(DLscratA,"DivLong");
		  NumberFree(DLscratB,"DivLong"); NumberFree(DLscratC,"DivLong");
		  return(0);
		}
#endif
/*
 		#] GMP stuff : 
*/
		/* Start with normalization operation */
 
		e = NumberMalloc("DivLong"); f = NumberMalloc("DivLong"); g = NumberMalloc("DivLong");
		if ( b[nb-1] == (FULLMAX-1) ) norm = 1;
		else {
			norm = (UWORD)(((ULONG)FULLMAX) / (ULONG)((b[nb-1]+1L)));
		}
		f[na] = 0;
		if ( MulLong(b,nb,&norm,1,e,&ne) ||
		     MulLong(a,na,&norm,1,f,&nf) ) {
			NumberFree(e,"DivLong"); NumberFree(f,"DivLong"); NumberFree(g,"DivLong");
			return(-1);
		}
		if ( BigLong(f+nf-ne,ne,e,ne) >= 0 ) {
			SubPLon(f+nf-ne,ne,e,ne,f+nf-ne,&nh);
			w1 = c + (nf-ne);
			*nc = nf-ne+1;
		}
		else {
			nh = ne;
			*nc = nf-ne;
			w1 = 0;
		}
		w2 = c; i = *nc; do { *w2++ = 0; } while ( --i > 0 );
		nf = na;
		ni = nf-ne;
		esthelp = (RLONG)(e[ne-1]) + 1L;
		while ( nf >= ne ) {
			if ( (WORD)esthelp == 0 ) {
				estim = (WORD)(((((RLONG)(f[nf]))<<BITSINWORD)+f[nf-1])>>BITSINWORD);
			}
			else {
				estim = (WORD)(((((RLONG)(f[nf]))<<BITSINWORD)+f[nf-1])/esthelp);
			}
			/* This estimate may be up to two too small */
			if ( estim ) {
				MulLong(e,ne,&estim,1,g,&ng);
				nh = ne + 1; if ( !f[ni+ne] ) nh--;
				SubPLon(f+ni,nh,g,ng,f+ni,&nh);
			}
			else {
				w2 = f+ni+ne; nh = ne+1;
				while ( ( nh > 0 ) && !*w2 ) { nh--; w2--; }
			}
			if ( BigLong(f+ni,nh,e,ne) >= 0 ) {
				estim++;
				SubPLon(f+ni,nh,e,ne,f+ni,&nh);
				if ( BigLong(f+ni,nh,e,ne) >= 0 ) {
					estim++;
					SubPLon(f+ni,nh,e,ne,f+ni,&nh);
					if ( BigLong(f+ni,nh,e,ne) >= 0 ) {
						MLOCK(ErrorMessageLock);
						MesPrint("Problems in DivLong");
						AO.OutSkip = 3;
						FiniLine();
						i = na;
						while ( --i >= 0 ) { TalToLine((UWORD)(*a++)); TokenToLine((UBYTE *)"  "); }
						FiniLine();
						i = nb;
						while ( --i >= 0 ) { TalToLine((UWORD)(*b++)); TokenToLine((UBYTE *)"  "); }
						AO.OutSkip = 0;
						FiniLine();
						MUNLOCK(ErrorMessageLock);
						NumberFree(e,"DivLong"); NumberFree(f,"DivLong"); NumberFree(g,"DivLong");
						return(-1);
					}
				}
			}
			c[ni] = estim;
			nf--;
			ni--;
		}
		if ( w1 ) *w1 = 1;

		/* Finish with the renormalization operation */

		if ( nh > 0 ) {
			if ( norm == 1 ) {
				*nd = i = nh; ff = f;
				NCOPY(d,ff,i);
			}
			else {
				w1 = f+nh;
				*nd = ni = nh;
				w2 = d+ni;
				v = norm;
				t = (RLONG)(*--w1);
				while ( --ni >= 0 ) {
					*--w2 = t / v;
					t -= v * (*w2);
					if ( ni ) {
						t <<= BITSINWORD;
						t += *--w1;
					}
				}
				if ( t ) {
					MLOCK(ErrorMessageLock);
					MesPrint("Error in DivLong");
					MUNLOCK(ErrorMessageLock);
					NumberFree(e,"DivLong"); NumberFree(f,"DivLong"); NumberFree(g,"DivLong");
					return(-1);
				}
				if ( !*(d+nh-1) ) (*nd)--;
			}
		}
		else { *nd = 0; }
		NumberFree(e,"DivLong"); NumberFree(f,"DivLong"); NumberFree(g,"DivLong");
	}
	if ( sgna < 0 ) { *nc = -(*nc); *nd = -(*nd); }
	if ( sgnb < 0 ) { *nc = -(*nc); }
	return(0);
}

/*
 		#] DivLong : 
 		#[ RaisPow :		WORD RaisPow(a,na,b)

	Raises a to the power b. a is a Long integer and b >= 0.
	The method that is used works with a bitdecomposition of b.
*/

int RaisPow(PHEAD UWORD *a, WORD *na, UWORD b)
{
	GETBIDENTITY
	WORD i, nu;
	UWORD *it, *iu, c;
	UWORD *is, *iss;
	WORD ns, nt, nmod;
	nmod = ABS(AN.ncmod);
	if ( !*na || ( ( *na == 1 ) && ( *a == 1 ) ) ) return(0);
	if ( !b ) {	*na=1; *a=1; return(0); }
	is = NumberMalloc("RaisPow");
	it = NumberMalloc("RaisPow");
	for ( i = 0; i < ABS(*na); i++ ) is[i] = a[i];
	ns = *na;
	c = b;
	for ( i = 0; i < BITSINWORD; i++ ) {
		if ( !c ) break;
		c /= 2;
	}
	i--;
	c = 1 << i;
	while ( --i >= 0 ) {
		c /= 2;
		if(MulLong(is,ns,is,ns,it,&nt)) goto RaisOvl;
		if ( b & c ) {
			if ( MulLong(it,nt,a,*na,is,&ns) ) goto RaisOvl;
		}
		else {
			iu = is; is = it; it = iu;
			nu = ns; ns = nt; nt = nu;
		}
		if ( nmod != 0 ) {
			if ( DivLong(is,ns,(UWORD *)AC.cmod,nmod,it,&nt,is,&ns) ) goto RaisOvl;
		}
	}
	if ( ( nmod != 0 ) && ( ( AC.modmode & POSNEG ) != 0 ) ) {
		NormalModulus(is,&ns);
	}
	if ( ( *na = i = ns ) != 0 ) { iss = is; i=ABS(i); NCOPY(a,iss,i); }
	NumberFree(is,"RaisPow"); NumberFree(it,"RaisPow");
	return(0);
RaisOvl:
	MLOCK(ErrorMessageLock);
	MesCall("RaisPow");
	MUNLOCK(ErrorMessageLock);
	NumberFree(is,"RaisPow"); NumberFree(it,"RaisPow");
	SETERROR(-1)
}

/*
 		#] RaisPow : 
 		#[ RaisPowCached :
*/

/**  Computes power x^n and caches the value
 *
 *   Description
 *   ===========
 *   Calculates the power x^n and stores the results for caching
 *   purposes. The pointer c (i.e., the pointer, and not what it
 *   points to) is overwritten. What it points to should not be
 *   overwritten in the calling function.
 *
 *   Notes
 *   =====
 *   - Caching is done in AT.small_power[]. This array is extended
 *     if necessary.
 */
void RaisPowCached (PHEAD WORD x, WORD n, UWORD **c, WORD *nc) {

	int i,j;
	WORD new_small_power_maxx, new_small_power_maxn, ID;
	WORD *new_small_power_n;
	UWORD **new_small_power;
	
	/* check whether to extend the array */
	if (x>=AT.small_power_maxx || n>=AT.small_power_maxn) {

		new_small_power_maxx = AT.small_power_maxx;
		if (x>=AT.small_power_maxx)
			new_small_power_maxx = MaX(2*AT.small_power_maxx, x+1);
		
		new_small_power_maxn = AT.small_power_maxn;
		if (n>=AT.small_power_maxn)
			new_small_power_maxn = MaX(2*AT.small_power_maxn, n+1);
		
		new_small_power_n = (WORD*) Malloc1(new_small_power_maxx*new_small_power_maxn*sizeof(WORD),"RaisPowCached");
		new_small_power = (UWORD **) Malloc1(new_small_power_maxx*new_small_power_maxn*sizeof(UWORD *),"RaisPowCached");

		for (i=0; i<new_small_power_maxx * new_small_power_maxn; i++) {
			new_small_power_n[i] = 0;
			new_small_power  [i] = NULL;
		}
					 
		for (i=0; i<AT.small_power_maxx; i++) 
			for (j=0; j<AT.small_power_maxn; j++) {
				new_small_power_n[i*new_small_power_maxn+j] =	AT.small_power_n[i*AT.small_power_maxn+j];
				new_small_power  [i*new_small_power_maxn+j] = AT.small_power  [i*AT.small_power_maxn+j];
			}

		if (AT.small_power_n != NULL) {
			M_free(AT.small_power_n,"RaisPowCached");
			M_free(AT.small_power,"RaisPowCached");
		}

		AT.small_power_maxx = new_small_power_maxx;
		AT.small_power_maxn = new_small_power_maxn;
		AT.small_power_n    = new_small_power_n;
		AT.small_power      = new_small_power;				
	}

	/* check whether the results is already calculated */
	ID = x * AT.small_power_maxn + n;

	if (AT.small_power[ID] == NULL) {
#ifdef OLDRAISPOWCACHED
        AT.small_power[ID] = NumberMalloc("RaisPowCached");
        AT.small_power_n[ID] = 1;
        AT.small_power[ID][0] = x;
        RaisPow(BHEAD AT.small_power[ID],&AT.small_power_n[ID],n);
#else
        UWORD *c = NumberMalloc("RaisPowCached");
        WORD i, k = 1;
		c[0] = x;
        RaisPow(BHEAD c,&k,n);
/*
        And now get the proper amount.
*/
        if ( AT.InNumMem < k ) {    /* We should start a new buffer */
            AT.InNumMem = 5*AM.MaxTal;
            AT.NumMem = (UWORD *)Malloc1(AT.InNumMem*sizeof(UWORD),"RaisPowCached");
/*
			MesPrint("  Got an extra %l UWORDS in RaisPowCached",AT.InNumMem);
*/
        }
        for ( i = 0; i < k; i++ ) AT.NumMem[i] = c[i];
        AT.small_power[ID] = AT.NumMem;
        AT.small_power_n[ID] = k;
        AT.NumMem += k;
        AT.InNumMem -= k;
        NumberFree(c,"RaisPowCached");
#endif
	}

	/* return the result */
	*c  = AT.small_power[ID];
	*nc = AT.small_power_n[ID];
}

/*
 		#] RaisPowCached : 
	 	#[ RaisPowMod :
		
   Computes the power x^n mod m
 */
WORD RaisPowMod (WORD x, WORD n, WORD m) {
	LONG y=1, z=x;
	while (n) {
		if (n&1) { y*=z; y%=m; }
		z*=z; z%=m;
		n /= 2;
	}
	return (WORD)y;
}

/*
  	#] RaisPowMod : 
 		#[ NormalModulus :  int NormalModulus(UWORD *a,WORD *na)
*/
/**
 *	Brings a modular representation in the range -p/2 to +p/2
 *	The return value tells whether anything was done.
 *	Routine made in the general modulus revamp of July 2008 (JV).
 */

int NormalModulus(UWORD *a,WORD *na)
{
	WORD n;
	if ( AC.halfmod == 0 ) {
	  LOCK(AC.halfmodlock);
	  if ( AC.halfmod == 0 ) {
		UWORD two[1],remain[1];
		WORD dummy;
		two[0] = 2;
		AC.halfmod = (UWORD *)Malloc1((ABS(AC.ncmod))*sizeof(UWORD),"halfmod");
		DivLong((UWORD *)AC.cmod,(ABS(AC.ncmod)),two,1
				,(UWORD *)AC.halfmod,&(AC.nhalfmod),remain,&dummy);
	  }
	  UNLOCK(AC.halfmodlock);
	}
	n = ABS(*na);
	if ( BigLong(a,n,AC.halfmod,AC.nhalfmod) > 0 ) {
		SubPLon((UWORD *)AC.cmod,(ABS(AC.ncmod)),a,n,a,&n);
		if ( *na > 0 ) { *na = -n; }
		else { *na = n; }
		return(1);
	}
	return(0);
}

/*
 		#] NormalModulus : 
 		#[ MakeInverses :
*/
/**
 *	Makes a table of inverses in modular calculus
 *	The modulus is in AC.cmod and AC.ncmod
 *	One should notice that the table of inverses can only be made if
 *	the modulus fits inside a single FORM word. Otherwise the table lookup
 *	becomes too difficult and the table too long.
 */

int MakeInverses(void)
{
	WORD n = AC.cmod[0], i, inv2;
	if ( AC.ncmod != 1 ) return(1);
	if ( AC.modinverses == 0 ) {
	  LOCK(AC.halfmodlock);
	  if ( AC.modinverses == 0 ) {
		AC.modinverses = (UWORD *)Malloc1(n*sizeof(UWORD),"modinverses");
		AC.modinverses[0] = 0;
		AC.modinverses[1] = 1;
		for ( i = 2; i < n; i++ ) {
			if ( GetModInverses(i,n,
						(WORD *)(&(AC.modinverses[i])),&inv2) ) {
				SETERROR(-1)
			}
		}
	  }
	  UNLOCK(AC.halfmodlock);
	}
	return(0);
}

/*
 		#] MakeInverses : 
 		#[ GetModInverses :
*/
/**
 *	Input m1 and m2, which are relative prime.
 *	determines a*m1+b*m2 = 1  (and 1 is the gcd of m1 and m2)
 *	then a*m1 = 1 mod m2 and hence im1 = a.
 *	and  b*m2 = 1 mod m1 and hence im2 = b.
 *	Set m1 = 0*m1+1*m2 = a1*m1+b1*m2
 *	    m2 = 1*m1+0*m2 = a2*m1+b2*m2
 *	If everything is OK, the return value is zero
 */

int GetModInverses(WORD m1, WORD m2, WORD *im1, WORD *im2)
{
	WORD a1, a2, a3;
	WORD b1, b2, b3;
	WORD x = m1, y, c, d = m2;
	if ( x < 1 || d <= 1 ) goto somethingwrong;
	a1 = 0; a2 = 1;
	b1 = 1; b2 = 0;
	for(;;) {
		c = d/x; y = d%x; /* a good compiler makes this faster than y=d-c*x */
		if ( y == 0 ) break;
		a3 = a1-c*a2; a1 = a2; a2 = a3;
		b3 = b1-c*b2; b1 = b2; b2 = b3;
		d = x; x = y;
	}
	if ( x != 1 ) goto somethingwrong;
	if ( a2 < 0 ) a2 += m2;
	if ( b2 < 0 ) b2 += m1;
	if (im1!=NULL) *im1 = a2;
	if (im2!=NULL) *im2 = b2;
	return(0);
somethingwrong:
	MLOCK(ErrorMessageLock);
	MesPrint("Error trying to determine inverses in GetModInverses");
	MUNLOCK(ErrorMessageLock);
	return(-1);
}
/*
 		#] GetModInverses : 
 		#[ GetLongModInverses :
*/

int GetLongModInverses(PHEAD UWORD *a, WORD na, UWORD *b, WORD nb, UWORD *ia, WORD *nia, UWORD *ib, WORD *nib) {

	UWORD *s, *t, *sa, *sb, *ta, *tb, *x, *y, *swap1;
	WORD ns, nt, nsa, nsb, nta, ntb, nx, ny, swap2;
		 
	s = NumberMalloc("GetLongModInverses");
	ns = na;
	WCOPY(s, a, ABS(ns));

	t = NumberMalloc("GetLongModInverses");
	nt = nb;
	WCOPY(t, b, ABS(nt));

	sa = NumberMalloc("GetLongModInverses");
	nsa = 1;
	sa[0] = 1;

	sb = NumberMalloc("GetLongModInverses");
	nsb = 0;

	ta = NumberMalloc("GetLongModInverses");
	nta = 0;

	tb = NumberMalloc("GetLongModInverses");
	ntb = 1;
	tb[0] = 1;

	x = NumberMalloc("GetLongModInverses");
	y = NumberMalloc("GetLongModInverses");

	while (nt != 0) {
		DivLong(s,ns,t,nt,x,&nx,y,&ny);
		swap1=s; s=y; y=swap1;
		ns=ny;
		MulLong(x,nx,ta,nta,y,&ny);
		AddLong(sa,nsa,y,-ny,sa,&nsa);
		MulLong(x,nx,tb,ntb,y,&ny);
		AddLong(sb,nsb,y,-ny,sb,&nsb);

		swap1=s; s=t; t=swap1;
		swap2=ns; ns=nt; nt=swap2;
		swap1=sa; sa=ta; ta=swap1;
		swap2=nsa; nsa=nta; nta=swap2;
		swap1=sb; sb=tb; tb=swap1;
		swap2=nsb; nsb=ntb; ntb=swap2;
	}

	if (ia!=NULL) {
		*nia = nsa*ns;
		WCOPY(ia,sa,ABS(*nia));
	}

	if (ib!=NULL) {
		*nib = nsb*ns;
		WCOPY(ib,sb,ABS(*nib));
	}
	
	NumberFree(s,"GetLongModInverses");
	NumberFree(t,"GetLongModInverses");
	NumberFree(sa,"GetLongModInverses");
	NumberFree(sb,"GetLongModInverses");
	NumberFree(ta,"GetLongModInverses");
	NumberFree(tb,"GetLongModInverses");
	NumberFree(x,"GetLongModInverses");
	NumberFree(y,"GetLongModInverses");

	return 0;
}

/*
 		#] GetLongModInverses : 
 		#[ Product :		WORD Product(a,na,b)

	Multiplies the Long number in a with the WORD b.

*/

int Product(UWORD *a, WORD *na, WORD b)
{
	WORD i, sgn = 1;
	RLONG t, u;
	if ( *na < 0 ) { *na = -(*na); sgn = -sgn; }
	if ( b < 0 ) { b = -b; sgn = -sgn; }
	t = 0;
	u = (RLONG)b;
	for ( i = 0; i < *na; i++ ) {
		t += *a * u;
		*a++ = (UWORD)t;
		t >>= BITSINWORD;
	}
	if ( t > 0 ) {
		if ( ++(*na) > AM.MaxTal ) {
			MLOCK(ErrorMessageLock);
			MesPrint("Overflow in Product");
			MUNLOCK(ErrorMessageLock);
			return(-1);
		}
		*a = (UWORD)t;
	}
	if ( sgn < 0 ) *na = -(*na);
	return(0);
}

/*
 		#] Product : 
 		#[ Quotient :		UWORD Quotient(a,na,b)

		Routine divides the long number a by b with the assumption that
		there is no remainder (like while computing binomials).

*/

UWORD Quotient(UWORD *a, WORD *na, WORD b)
{
	RLONG v, t;
	WORD i, j, sgn = 1;
	if ( ( i = *na ) < 0 ) { sgn = -1; i = -i; }
	if ( b < 0 ) { b = -b; sgn = -sgn; }
	if ( i == 1 ) {
		if ( ( *a /= (UWORD)b ) == 0 ) *na = 0;
		if ( sgn < 0 ) *na = -*na;
		return(0);
	}
	a += i;
	j = i;
	v = (RLONG)b;
	t = (RLONG)(*--a);
	while ( --i >= 0 ) {
		*a = t / v;
		t -= v * (*a);
		if ( i ) {
			t <<= BITSINWORD;
			t += *--a;
		}
	}
	a += j - 1;
	if ( !*a ) j--;
	if ( sgn < 0 ) j = -j;
	*na = j;
	return(0);
}

/*
 		#] Quotient : 
 		#[ Remain10 :		WORD Remain10(a,na)

	Routine divides a by 10 and gives the remainder as return value.
	The value of a will be the quotient! a must be positive.

*/

WORD Remain10(UWORD *a, WORD *na)
{
	WORD i;
	RLONG t, u;
	UWORD *b;
	i = *na;
	t = 0;
	b = a + i - 1;
	while ( --i >= 0 ) {
		t += *b;
		*b-- = u = t / 10;
		t -= u * 10;
		if ( i > 0 ) t <<= BITSINWORD;
	}
	if ( ( *na > 0 ) && !a[*na-1] ) (*na)--;
	return((WORD)t);
}

/*
 		#] Remain10 : 
 		#[ Remain4 :		WORD Remain4(a,na)

	Routine divides a by 10000 and gives the remainder as return value.
	The value of a will be the quotient! a must be positive.

*/

WORD Remain4(UWORD *a, WORD *na)
{
	WORD i;
	RLONG t, u;
	UWORD *b;
	i = *na;
	t = 0;
	b = a + i - 1;
	while ( --i >= 0 ) {
		t += *b;
		*b-- = u = t / 10000;
		t -= u * 10000;
		if ( i > 0 ) t <<= BITSINWORD;
	}
	if ( ( *na > 0 ) && !a[*na-1] ) (*na)--;
	return((WORD)t);
}

/*
 		#] Remain4 : 
 		#[ PrtLong :		void PrtLong(a,na,s)

	Puts the long number a in string s.

*/

void PrtLong(UWORD *a, WORD na, UBYTE *s)
{
	GETIDENTITY
	WORD q, i;
	UBYTE *sa, *sb;
	UBYTE c;
	UWORD *bb, *b;

	if ( na < 0 ) {
		*s++ = '-';
		na = -na;
	}

	b = NumberMalloc("PrtLong");
	bb = b;
	i = na; while ( --i >= 0 ) *bb++ = *a++;
	a = b;
	if ( na > 2 ) {
		sa = s;
		do {
			q = Remain4(a,&na);
			*sa++ = (UBYTE)('0' + (q%10));
			q /= 10;
			*sa++ = (UBYTE)('0' + (q%10));
			q /= 10;
			*sa++ = (UBYTE)('0' + (q%10));
			q /= 10;
			*sa++ = (UBYTE)('0' + (q%10));
		} while ( na );
		while ( sa[-1] == '0' ) sa--;
		sb = s;
		s = sa;
		sa--;
		while ( sa > sb ) { c = *sa; *sa = *sb; *sb = c; sa--; sb++; }
	}
	else if ( na ) {
		sa = s;
		do {
			q = Remain10(a,&na);
			*sa++ = (UBYTE)('0' + q);
		} while ( na );
		sb = s;
		s = sa;
		sa--;
		while ( sa > sb ) { c = *sa; *sa = *sb; *sb = c; sa--; sb++; }
	}
	else *s++ = '0';
	*s = '\0';
	NumberFree(b,"PrtLong");
}

/*
 		#] PrtLong : 
 		#[ GetLong :		WORD GetLong(s,a,na)

	Reads a long number from a string.
	The string is zero terminated and contains only digits!

	New algorithm: try to read 4 digits together before the result
	is accumulated.
*/

int GetLong(UBYTE *s, UWORD *a, WORD *na)
{
/*
	UWORD digit;
	*a = 0;
	*na = 0;
	while ( FG.cTable[*s] == 1 ) {
		digit = *s++ - '0';
		if ( *na && Product(a,na,(WORD)10) ) return(-1);
		if ( digit && AddLong(a,*na,&digit,(WORD)1,a,na) ) return(-1);
	}
	return(0);
*/
	UWORD digit, x = 0, y = 0;
	*a = 0;
	*na = 0;
	while ( FG.cTable[*s] == 1 ) {
		x = *s++ - '0';
		if ( FG.cTable[*s] != 1 ) { y = 10; break; }
		x = 10*x + *s++ - '0';
		if ( FG.cTable[*s] != 1 ) { y = 100; break; }
		x = 10*x + *s++ - '0';
		if ( FG.cTable[*s] != 1 ) { y = 1000; break; }
		x = 10*x + *s++ - '0';
		if ( *na && Product(a,na,(WORD)10000) ) return(-1);
		if ( ( digit = x ) != 0 && AddLong(a,*na,&digit,(WORD)1,a,na) )
			return(-1);
		y = 0;
	}
	if ( y ) {
		if ( *na && Product(a,na,(WORD)y) ) return(-1);
		if ( ( digit = x ) != 0 && AddLong(a,*na,&digit,(WORD)1,a,na) )
			return(-1);
	}
	return(0);
}

/*
 		#] GetLong : 
 		#[ GCD :			WORD GCD(a,na,b,nb,c,nc)

	Algorithm to compute the GCD of two long numbers.
	See Knuth, sec 4.5.2 algorithm L.

	We assume that both numbers are positive

	NOTE!!!!!. NumberMalloc gets called and it may not be freed
*/

#ifdef EXTRAGCD

#define Convert(ia,aa,naa) \
	if ( (LONG)ia < 0 ) {        \
		ia = (ULONG)(-(LONG)ia); \
		aa[0] = ia;              \
		if ( ( aa[1] = ia >> BITSINWORD ) != 0 ) naa = -2; \
		else naa = -1;           \
	}                            \
	else if ( ia == 0 ) { aa[0] = 0; naa = 0; } \
	else {                       \
		aa[0] = ia;              \
		if ( ( aa[1] = ia >> BITSINWORD ) != 0 ) naa = 2; \
		else naa = 1;            \
	}

void GCD(UWORD *a, WORD na, UWORD *b, WORD nb, UWORD *c, WORD *nc)
{
	int ja = 0, jb = 0, j;
	UWORD *r,*t;
	UWORD *x1, *x2, *x3;
	WORD nd,naa,nbb;
	ULONG ia,ib,ic,id,u,v,w,q,T;
	UWORD aa[2], bb[2];
/*
	First eliminate easy powers of 2^...
*/
	while ( a[0] == 0 ) { na--; ja++; a++; }
	while ( b[0] == 0 ) { nb--; jb++; b++; }
	if ( ja > jb ) ja = jb;
	if ( ja > 0 ) {
		j = ja;
		do { *c++ = 0; } while ( --j > 0 );
	}
/*
	Now arrange things such that a >= b
*/
	if ( na < nb ) {
		jb = na; na = nb; nb = jb;
exch:
		r = a; a = b; b = r;
	}
	else if ( na == nb ) {
		r = a+na;
		t = b+nb;
		j = na;
		while ( --j >= 0 ) {
			if ( *--r > *--t ) break;
			if ( *r < *t ) goto exch;
		}
		if ( j < 0 ) {
out:
			j = nb;
			NCOPY(c,b,j);
			*nc = nb+ja;
			return;
		}
	}
/*
	{
		MLOCK(ErrorMessageLock);
		MesPrint("Ordered input, ja = %d",(WORD)ja);
		AO.OutSkip = 3;
		FiniLine();
		j = na; r = a;
		while ( --j >= 0 ) { TalToLine((UWORD)(*r++)); TokenToLine((UBYTE *)"  "); }
		FiniLine();
		j = nb; r = b;
		while ( --j >= 0 ) { TalToLine((UWORD)(*r++)); TokenToLine((UBYTE *)"  "); }
		AO.OutSkip = 0;
		FiniLine();
		MUNLOCK(ErrorMessageLock);
	}
*/
/*
	We have now that A > B
	The loop recognizes the case that na-nb >= 1
	In that case we just have to divide!
*/
	r = x1 = NumberMalloc("GCD"); t = x2 = NumberMalloc("GCD"); x3 = NumberMalloc("GCD");
	j = na;
	NCOPY(r,a,j);
	j = nb;
	NCOPY(t,b,j);

	for(;;) {

		while ( na > nb ) {
toobad:
			DivLong(x1,na,x2,nb,c,nc,x3,&nd);
			if ( nd == 0 ) { b = x2; goto out; }
			t = x1; x1 = x2; x2 = x3; x3 = t; na = nb; nb = nd;
			if ( na == 2 ) break;
		}
/*
	Here we can use the shortcut.
*/
	if ( na == 2 ) {
		v = x1[0] + ( ((ULONG)x1[1]) << BITSINWORD );
		w = x2[0];
		if ( nb == 2 ) w += ((ULONG)x2[1]) << BITSINWORD;
#ifdef EXTRAGCD2
		v = GCD2(v,w);
#else
		do { u = v%w; v = w; w = u; } while ( w );
#endif
		c[0] = (UWORD)v;
		if ( ( c[1] = (UWORD)(v >> BITSINWORD) ) != 0 ) *nc = 2+ja;
		else *nc = 1+ja;
		NumberFree(x1,"GCD"); NumberFree(x2,"GCD"); NumberFree(x3,"GCD");
		return;
	}
	if ( na == 1 ) {
		UWORD ui, uj;
		ui = x1[0]; uj = x2[0];
#ifdef EXTRAGCD2
		ui = (UWORD)GCD2((ULONG)ui,(ULONG)uj);
#else
		do { nd = ui%uj; ui = uj; uj = nd; } while ( nd );
#endif
		c[0] = ui;
		*nc = 1 + ja;
		NumberFree(x1,"GCD"); NumberFree(x2,"GCD"); NumberFree(x3,"GCD");
		return;
	}
	ia = 1; ib = 0; ic = 0; id = 1;
	u = ( ((ULONG)x1[na-1]) << BITSINWORD ) + x1[na-2];
	v = ( ((ULONG)x2[nb-1]) << BITSINWORD ) + x2[nb-2];

	while ( v+ic != 0 && v+id != 0 &&
		 ( q = (u+ia)/(v+ic) ) == (u+ib)/(v+id) ) {
		T = ia-q*ic; ia = ic; ic = T;
		T = ib-q*id; ib = id; id = T;
		T = u - q*v; u = v; v = T;
	}
	if ( ib == 0 ) goto toobad;
	Convert(ia,aa,naa);
	Convert(ib,bb,nbb);
	MulLong(x1,na,aa,naa,x3,&nd);
	MulLong(x2,nb,bb,nbb,c,nc);
	AddLong(x3,nd,c,*nc,c,nc);
	Convert(ic,aa,naa);
	Convert(id,bb,nbb);
	MulLong(x1,na,aa,naa,x3,&nd);
	t = c; na = j = *nc; r = x1;
	NCOPY(r,t,j);
	MulLong(x2,nb,bb,nbb,c,nc);
	AddLong(x3,nd,c,*nc,x2,&nb);
	}
}

#endif

/*
 		#] GCD : 
 		#[ GcdLong :		WORD GcdLong(a,na,b,nb,c,nc)

	Returns the Greatest Common Divider of a and b in c.
	If a and or b are zero an error message will be returned.
	The answer is always positive.
	In principle a and c can be the same.
*/

#ifndef NEWTRICK
/*
  	#[ Old Routine :
*/

int GcdLong(PHEAD UWORD *a, WORD na, UWORD *b, WORD nb, UWORD *c, WORD *nc)
{
	GETBIDENTITY
	if ( !na || !nb ) {
		if ( !na && !nb ) {
			MLOCK(ErrorMessageLock);
			MesPrint("Cannot take gcd");
			MUNLOCK(ErrorMessageLock);
			return(-1);
		}
		
		if ( !na ) {
			*nc = abs(nb);
			NCOPY(c,b,*nc);
			*nc = abs(nb);
			return(0);
		}
		
		*nc = abs(na);
		NCOPY(c,a,*nc);
		*nc = abs(na);
		return(0);
	}
	if ( na < 0 ) na = -na;
	if ( nb < 0 ) nb = -nb;
	if ( na == 1 && nb == 1 ) {
#ifdef EXTRAGCD2
		*c = (UWORD)GCD2((ULONG)*a,(ULONG)*b);
#else
		UWORD x,y,z;
		x = *a;
		y = *b;
		do { z = x % y; x = y; } while ( ( y = z ) != 0 );
		*c = x;
#endif
		*nc = 1;
	}
	else if ( na <= 2 && nb <= 2 ) {
		RLONG lx,ly,lz;
		if ( na == 2 ) { lx = (((RLONG)(a[1]))<<BITSINWORD) + *a; }
		else { lx = *a; }
		if ( nb == 2 ) { ly = (((RLONG)(b[1]))<<BITSINWORD) + *b; }
		else { ly = *b; }
#ifdef LONGWORD
#ifdef EXTRAGCD2
		lx = GCD2(lx,ly);
#else
		do { lz = lx % ly; lx = ly; } while ( ( ly = lz ) != 0 );
#endif
#else
          if ( lx < ly ) { lz = lx; lx = ly; ly = lz; }
		do {
			lz = lx % ly; lx = ly;
		} while ( ( ly = lz ) != 0 && ( lx & AWORDMASK ) != 0 );
		if ( ly ) {
			do { *c = ((UWORD)lx)%((UWORD)ly); lx = ly; } while ( ( ly = *c ) != 0 );
			*c = (UWORD)lx;
			*nc = 1;
		}
		else
#endif
		{
			*c++ = (UWORD)lx;
			if ( ( *c = (UWORD)(lx >> BITSINWORD) ) != 0 ) *nc = 2;
			else *nc = 1;
		}
	}
	else {
#ifdef EXTRAGCD
		GCD(a,na,b,nb,c,nc);
#else
#ifdef NEWGCD
		UWORD *x3,*x1,*x2, *GLscrat7, *GLscrat8;
		WORD n1,n2,n3,n4;
		WORD i, j;
		x1 = c; x3 = a; n1 = i = na;
		NCOPY(x1,x3,i);
		GLscrat7 = NumberMalloc("GcdLong"); GLscrat8 = NumberMalloc("GcdLong");
		x2 = GLscrat8; x3 = b; n2 = i = nb;
		NCOPY(x2,x3,i);
		x1 = c; i = 0;
		while ( x1[0] == 0 ) { i += BITSINWORD; x1++; n1--; }
		while ( ( x1[0] & 1 ) == 0 ) { i++; SCHUIF(x1,n1) }
		x2 = GLscrat8; j = 0;
		while ( x2[0] == 0 ) { j += BITSINWORD; x2++; n2--; }
		while ( ( x2[0] & 1 ) == 0 ) { j++; SCHUIF(x2,n2) }
		if ( j > i ) j = i;		/* powers of two in GCD */
		for(;;){
			if ( n1 > n2 ) {
firstbig:
				SubPLon(x1,n1,x2,n2,x1,&n3);
				n1 = n3;
				if ( n1 == 0 ) {
					x1 = c;
					n1 = i = n2; NCOPY(x1,x2,i);
					break;
				}
				while ( ( x1[0] & 1 ) == 0 ) SCHUIF(x1,n1)
				if ( n1 == 1 ) {
					if ( DivLong(x2,n2,x1,n1,GLscrat7,&n3,x2,&n4) ) goto GcdErr;
					n2 = n4;
					if ( n2 == 0 ) {
						i = n1; x2 = c; NCOPY(x2,x1,i);
						break;
					}
#ifdef EXTRAGCD2
					*c = (UWORD)GCD2((ULONG)x1[0],(ULONG)x2[0]);
#else
					{
						UWORD x,y,z;
						x = x1[0];
						y = x2[0];
						do { z = x % y; x = y; } while ( ( y = z ) != 0 );
						*c = x;
					}
#endif
					n1 = 1;
					break;
				}
			}
			else if ( n1 < n2 ) {
lastbig:
				SubPLon(x2,n2,x1,n1,x2,&n3);
				n2 = n3;
				if ( n2 == 0 ) {
					i = n1; x2 = c; NCOPY(x2,x1,i);
					break;
				}
				while ( ( x2[0] & 1 ) == 0 ) SCHUIF(x2,n2)
				if ( n2 == 1 ) {
					if ( DivLong(x1,n1,x2,n2,GLscrat7,&n3,x1,&n4) ) goto GcdErr;
					n1 = n4;
					if ( n1 == 0 ) {
						x1 = c;
						n1 = i = n2; NCOPY(x1,x2,i);
						break;
					}
#ifdef EXTRAGCD2
					*c = (UWORD)GCD2((ULONG)x2[0],(ULONG)x1[0]);
#else
					{
						UWORD x,y,z;
						x = x2[0];
						y = x1[0];
						do { z = x % y; x = y; } while ( ( y = z ) != 0 );
						*c = x;
					}
#endif
					n1 = 1;
					break;
				}
			}
			else {
				for ( i = n1-1; i >= 0; i-- ) {
					if ( x1[i] > x2[i] ) goto firstbig;
					else if ( x1[i] < x2[i] ) goto lastbig;
				}
				i = n1; x2 = c; NCOPY(x2,x1,i);
				break;
			}
		}
/*
		Now the GCD is in c but still needs j powers of 2.
*/
		x1 = c;
		while ( j >= BITSINWORD ) {
			for ( i = n1; i > 0; i-- ) x1[i] = x1[i-1];
			x1[0] = 0; n1++;
			j -= BITSINWORD;
		}
		if ( j > 0 ) {
			ULONG a1,a2 = 0;
			for ( i = 0; i < n1; i++ ) {
				a1 = x1[i]; a1 <<= j;
				a2 += a1;
				x1[i] = a2;
				a2 >>= BITSINWORD;
			}
			if ( a2 != 0 ) {
				x1[n1++] = a2;
			}
		}
		*nc = n1;
		NumberFree(GLscrat7,"GcdLong"); NumberFree(GLscrat8,"GcdLong");
#else
		UWORD *x1,*x2,*x3,*x4,*c1,*c2;
		WORD n1,n2,n3,n4,i;
		x1 = c; x3 = a; n1 = i = na;
		NCOPY(x1,x3,i);
		x1 = c; c1 = x2 = NumberMalloc("GcdLong"); x3 = NumberMalloc("GcdLong"); x4 = NumberMalloc("GcdLong");
		c2 = b; n2 = i = nb;
		NCOPY(c1,c2,i);
		for(;;){
			if ( DivLong(x1,n1,x2,n2,x4,&n4,x3,&n3) ) goto GcdErr;
			if ( !n3 ) { x1 = x2; n1 = n2; break; }
			if ( DivLong(x2,n2,x3,n3,x4,&n4,x1,&n1) ) goto GcdErr;
			if ( !n1 ) { x1 = x3; n1 = n3; break; }
			if ( DivLong(x3,n3,x1,n1,x4,&n4,x2,&n2) ) goto GcdErr;
			if ( !n2 ) {
				*nc = n1;
				NumberFree(x2,"GcdLong"); NumberFree(x3,"GcdLong"); NumberFree(x4,"GcdLong");
				return(0);
			}
		}
		*nc = i = n1;
		NCOPY(c,x1,i);
		NumberFree(x2,"GcdLong"); NumberFree(x3,"GcdLong"); NumberFree(x4,"GcdLong");
#endif
#endif
	}
	return(0);
GcdErr:
	MLOCK(ErrorMessageLock);
	MesCall("GcdLong");
	MUNLOCK(ErrorMessageLock);
	SETERROR(-1)
}
/*
  	#] Old Routine : 
*/
#else

/*
	New routine for GcdLong that uses smart shortcuts.
	Algorithm by J. Vermaseren 15-nov-2006.
	It runs faster for very big numbers but only by a fixed factor.
	There is no improvement in the power behaviour.
	Improvement on the whole of hf9 (multiple zeta values at weight 9):
		Better than a factor 2 on a 32 bits architecture and 2.76 on a
		64 bits architecture.
	On hf10 (MZV's at weight 10), 64 bits architecture: factor 7.

	If we have two long numbers (na,nb > GCDMAX) we will work in a
	truncated way. At the moment of writing (15-nov-2006) it isn't
	clear whether this algorithm is an invention or a reinvention.
	A short search on the web didn't show anything.

	31-jul-2007:
	A better search shows that this is an adaptation of the Lehmer-Euclid
	algorithm, already described in Knuth. Here we can work without upper
	and lower limit because we are only interested in the GCD, not the
	extra numbers. Also it takes already some features of the double
	digit Lehmer-Euclid algorithm of Jebelean it seems.

	Maybe this can be programmed slightly better and we can get another
	few percent speed increase. Further improvements for the asymptotic
	case come from splitting the calculation as in Karatsuba and working
	with FFT divisions and multiplications etc. But this is when hundreds
	of words are involved at the least.

	Algorithm

	1: while ( na > nb || nb < GCDMAX ) {
		if ( nb == 0 ) { result in a }
		c = a % b;
		a = b;
		b = c;
	   }
	2: Make the truncated values in which a and b are the combinations
	   of the top two words of a and b. The whole numbers are aa and bb now.
	3: ma1 = 1; ma2 = 0; mb1 = 0; mb2 = 1;
	4: A = a; B = b; m = a/b; c = a - m*b;
	   c = ma1*a+ma2*b-m*(mb1*a+mb2*b) = (ma1-m*mb1)*a+(ma2-m*mb2)*b
	   mc1 = ma1-m*mb1; mc2 = ma2-m*mb2;
	5: a = b; ma1 = mb1; ma2 = mb2;
	   b = c; mb1 = mc1; mb2 = mc2;
	6: if ( b != 0 && nb >= FULLMAX ) goto 4;
	7: Now construct the new quantities
		ma1*aa+ma2*bb and mb1*aa+mb2*bb
	8: goto 1;

	The essence of the above algorithm is that we do the divisions only
	on relatively short numbers. Also usually there are many steps 4&5
	for each step 7. This eliminates many operations.
	The termination at FULLMAX is that we make errors by not considering
	the tail of the number. If we run b down all the way, the errors combine
	in such a way that the new numbers may be of the same order as the old
	numbers. By stopping halfway we don't get the error beyond halfway
	either. Unfortunately this means that a >= FULLMAX and hence na > nb
	which means that next we will have a complete division. But just once.
	Running the steps 4-6 till a < FULLMAX runs already into problems.
	It may be necessary to experiment a bit to obtain the optimum value
	of GCDMAX.
*/

int GcdLong(PHEAD UWORD *a, WORD na, UWORD *b, WORD nb, UWORD *c, WORD *nc)
{
	GETBIDENTITY
	UWORD x,y,z;
	UWORD *x1,*x2,*x3,*x4,*x5,*d;
	UWORD *GLscrat6, *GLscrat7, *GLscrat8, *GLscrat9, *GLscrat10;
	WORD n1,n2,n3,n4,n5,i;
	RLONG lx,ly,lz;
	LONG ma1, ma2, mb1, mb2, mc1, mc2, m;
	if ( !na || !nb ) {
		if ( !na && !nb ) {
			MLOCK(ErrorMessageLock);
			MesPrint("Cannot take gcd");
			MUNLOCK(ErrorMessageLock);
			return(-1);
		}
		
		if ( !na ) {
			*nc = abs(nb);
			NCOPY(c,b,*nc);
			*nc = abs(nb);
			return(0);
		}
		
		*nc = abs(na);
		NCOPY(c,a,*nc);
		*nc = abs(na);
		return(0);
	}
	if ( na < 0 ) na = -na;
	if ( nb < 0 ) nb = -nb;
/*
  	#[ GMP stuff :
*/
#ifdef WITHGMP
	if ( na > 3 && nb > 3 ) {
		int ii;
		mp_limb_t *upa, *upb, *upc, xx;
		UWORD *uw, *u1, *u2;
		unsigned int tcounta, tcountb, tcounta1, tcountb1;
		mp_size_t ana, anb, anc;

		u1 = uw = NumberMalloc("GcdLong");
		upa = (mp_limb_t *)u1;
		ana = na; tcounta1 = 0;
		while ( a[0] == 0 ) { a++; ana--; tcounta1++; }
		for ( ii = 0; ii < ana; ii++ ) { *uw++ = *a++; }
		if ( ( ana & 1 ) != 0 ) { *uw = 0; ana++; }
		ana /= 2;

		u2 = uw = NumberMalloc("GcdLong");
		upb = (mp_limb_t *)u2;
		anb = nb; tcountb1 = 0;
		while ( b[0] == 0 ) { b++; anb--; tcountb1++; }
		for ( ii = 0; ii < anb; ii++ ) { *uw++ = *b++; }
		if ( ( anb & 1 ) != 0 ) { *uw = 0; anb++; }
		anb /= 2;

		xx = upa[0]; tcounta = 0;
		while ( ( xx & 15 ) == 0 ) { tcounta += 4; xx >>= 4; }
		while ( ( xx &  1 ) == 0 ) { tcounta += 1; xx >>= 1; }
		xx = upb[0]; tcountb = 0;
		while ( ( xx & 15 ) == 0 ) { tcountb += 4; xx >>= 4; }
		while ( ( xx &  1 ) == 0 ) { tcountb += 1; xx >>= 1; }

		if ( tcounta ) {
			mpn_rshift(upa,upa,ana,tcounta);
			if ( upa[ana-1] == 0 ) ana--;
		}
		if ( tcountb ) {
			mpn_rshift(upb,upb,anb,tcountb);
			if ( upb[anb-1] == 0 ) anb--;
		}

		upc = (mp_limb_t *)(NumberMalloc("GcdLong"));
		if ( ( ana > anb ) || ( ( ana == anb ) && ( upa[ana-1] >= upb[ana-1] ) ) ) {
			anc = mpn_gcd(upc,upa,ana,upb,anb);
		}
		else {
			anc = mpn_gcd(upc,upb,anb,upa,ana);
		}

		tcounta = tcounta1*BITSINWORD + tcounta;
		tcountb = tcountb1*BITSINWORD + tcountb;
		if ( tcountb > tcounta ) tcountb = tcounta;
		tcounta = tcountb/BITSINWORD;
		tcountb = tcountb%BITSINWORD;

		if ( tcountb ) {
			xx = mpn_lshift(upc,upc,anc,tcountb);
			if ( xx ) { upc[anc] = xx; anc++; }
		}

		uw = (UWORD *)upc; anc *= 2;
		while ( uw[anc-1] == 0 ) anc--;
		for ( ii = 0; ii < (int)tcounta; ii++ ) *c++ = 0;
		for ( ii = 0; ii < anc; ii++ ) *c++ = *uw++;
		*nc = anc + tcounta;
		NumberFree(u1,"GcdLong"); NumberFree(u2,"GcdLong"); NumberFree((UWORD *)(upc),"GcdLong");
		return(0);
	}
#endif
/*
  	#] GMP stuff : 
*/
/*
  	#[ Easy cases :
*/
	if ( na == 1 && nb == 1 ) {
		x = *a;
		y = *b;
		do { z = x % y; x = y; } while ( ( y = z ) != 0 );
		*c = x;
		*nc = 1;
		return(0);
	}
	else if ( na <= 2 && nb <= 2 ) {
		if ( na == 2 ) { lx = (((RLONG)(a[1]))<<BITSINWORD) + *a; }
		else { lx = *a; }
		if ( nb == 2 ) { ly = (((RLONG)(b[1]))<<BITSINWORD) + *b; }
		else { ly = *b; }
		if ( lx < ly ) { lz = lx; lx = ly; ly = lz; }
#if ( BITSINWORD == 16 )
		do {
			lz = lx % ly; lx = ly;
		} while ( ( ly = lz ) != 0 );
#else
		do {
			lz = lx % ly; lx = ly;
		} while ( ( ly = lz ) != 0 && ( lx & AWORDMASK ) != 0 );
		if ( ly ) {
			x = (UWORD)lx; y = (UWORD)ly;
			do { *c = x % y; x = y; } while ( ( y = *c ) != 0 );
			*c = x;
			*nc = 1;
		}
		else
#endif
		{
			*c++ = (UWORD)lx;
			if ( ( *c = (UWORD)(lx >> BITSINWORD) ) != 0 ) *nc = 2;
			else *nc = 1;
		}
		return(0);
	}
/*
  	#] Easy cases : 
*/
	GLscrat6 = NumberMalloc("GcdLong"); GLscrat7 = NumberMalloc("GcdLong");
	GLscrat8 = NumberMalloc("GcdLong");
	GLscrat9 = NumberMalloc("GcdLong"); GLscrat10 = NumberMalloc("GcdLong");
restart:;
/*
  	#[ Easy cases :
*/
	if ( na == 1 && nb == 1 ) {
		x = *a;
		y = *b;
		do { z = x % y; x = y; } while ( ( y = z ) != 0 );
		*c = x;
		*nc = 1;
	}
	else if ( na <= 2 && nb <= 2 ) {
		if ( na == 2 ) { lx = (((RLONG)(a[1]))<<BITSINWORD) + *a; }
		else { lx = *a; }
		if ( nb == 2 ) { ly = (((RLONG)(b[1]))<<BITSINWORD) + *b; }
		else { ly = *b; }
		if ( lx < ly ) { lz = lx; lx = ly; ly = lz; }
#if ( BITSINWORD == 16 )
		do {
			lz = lx % ly; lx = ly;
		} while ( ( ly = lz ) != 0 );
#else
		do {
			lz = lx % ly; lx = ly;
		} while ( ( ly = lz ) != 0 && ( lx & AWORDMASK ) != 0 );
		if ( ly ) {
			x = (UWORD)lx; y = (UWORD)ly;
			do { *c = x % y; x = y; } while ( ( y = *c ) != 0 );
			*c = x;
			*nc = 1;
		}
		else
#endif
		{
			*c++ = (UWORD)lx;
			if ( ( *c = (UWORD)(lx >> BITSINWORD) ) != 0 ) *nc = 2;
			else *nc = 1;
		}
	}
/*
  	#] Easy cases : 
  	#[ Original code :
*/
	else if ( na < GCDMAX || nb < GCDMAX || na != nb ) {
		if ( na < nb ) {
			x2 = GLscrat8; x3 = a; n2 = i = na;
			NCOPY(x2,x3,i);
			x1 = c; x3 = b; n1 = i = nb;
			NCOPY(x1,x3,i);
		}
		else {
			x1 = c; x3 = a; n1 = i = na;
			NCOPY(x1,x3,i);
			x2 = GLscrat8; x3 = b; n2 = i = nb;
			NCOPY(x2,x3,i);
		}
		x1 = c; x2 = GLscrat8; x3 = GLscrat7; x4 = GLscrat6;
		for(;;){
			if ( DivLong(x1,n1,x2,n2,x4,&n4,x3,&n3) ) goto GcdErr;
			if ( !n3 ) { x1 = x2; n1 = n2; break; }
			if ( n2 <= 2 ) { a = x2; b = x3; na = n2; nb = n3; goto restart; }
			if ( n3 >= GCDMAX && n2 == n3 ) {
				a = GLscrat9; b = GLscrat10; na = n2; nb = n3;
				for ( i = 0; i < na; i++ ) a[i] = x2[i];
				for ( i = 0; i < nb; i++ ) b[i] = x3[i];
				goto newtrick;
			}
			if ( DivLong(x2,n2,x3,n3,x4,&n4,x1,&n1) ) goto GcdErr;
			if ( !n1 ) { x1 = x3; n1 = n3; break; }
			if ( n3 <= 2 ) { a = x3; b = x1; na = n3; nb = n1; goto restart; }
			if ( n1 >= GCDMAX && n1 == n3 ) {
				a = GLscrat9; b = GLscrat10; na = n3; nb = n1;
				for ( i = 0; i < na; i++ ) a[i] = x3[i];
				for ( i = 0; i < nb; i++ ) b[i] = x1[i];
				goto newtrick;
			}
			if ( DivLong(x3,n3,x1,n1,x4,&n4,x2,&n2) ) goto GcdErr;
			if ( !n2 ) { *nc = n1; goto normalend; }
			if ( n1 <= 2 ) { a = x1; b = x2; na = n1; nb = n2; goto restart; }
			if ( n2 >= GCDMAX && n2 == n1 ) {
				a = GLscrat9; b = GLscrat10; na = n1; nb = n2;
				for ( i = 0; i < na; i++ ) a[i] = x1[i];
				for ( i = 0; i < nb; i++ ) b[i] = x2[i];
				goto newtrick;
			}
		}
		*nc = i = n1;
		NCOPY(c,x1,i);
	}
/*
  	#] Original code : 
  	#[ New code :
*/
	else {
/*
		This is the new algorithm starting at step 3.

	3: ma1 = 1; ma2 = 0; mb1 = 0; mb2 = 1;
	4: A = a; B = b; m = a/b; c = a - m*b;
	   c = ma1*a+ma2*b-m*(mb1*a+mb2*b) = (ma1-m*mb1)*a+(ma2-m*mb2)*b
	   mc1 = ma1-m*mb1; mc2 = ma2-m*mb2;
	5: a = b; ma1 = mb1; ma2 = mb2;
	   b = c; mb1 = mc1; mb2 = mc2;
	6: if ( b != 0 ) goto 4;
*/
newtrick:;
		ma1 = 1; ma2 = 0; mb1 = 0; mb2 = 1;
		lx = (((RLONG)(a[na-1]))<<BITSINWORD) + a[na-2];
		ly = (((RLONG)(b[nb-1]))<<BITSINWORD) + b[nb-2];
		if ( ly > lx ) { lz = lx; lx = ly; ly = lz; d = a; a = b; b = d; }
		do {
			m = lx/ly;
			mc1 = ma1-m*mb1; mc2 = ma2-m*mb2;
			ma1 = mb1; ma2 = mb2; mb1 = mc1; mb2 = mc2;
			lz = lx - m*ly; lx = ly; ly = lz;
		} while ( ly >= FULLMAX );
/*
		Next the construction of the two new numbers

	7: Now construct the new quantities
		a = ma1*aa+ma2*bb and b = mb1*aa+mb2*bb
*/
		x1 = GLscrat6;
		x2 = GLscrat7;
		x3 = GLscrat8;
		x5 = GLscrat10;
		if ( ma1 < 0 ) {
			ma1 = -ma1;
			x1[0] = (UWORD)ma1;
			x1[1] = (UWORD)(ma1 >> BITSINWORD);
			if ( x1[1] ) n1 = -2;
			else         n1 = -1;
		}
		else {
			x1[0] = (UWORD)ma1;
			x1[1] = (UWORD)(ma1 >> BITSINWORD);
			if ( x1[1] ) n1 = 2;
			else         n1 = 1;
		}
		if ( MulLong(a,na,x1,n1,x2,&n2) ) goto GcdErr;
		if ( ma2 < 0 ) {
			ma2 = -ma2;
			x1[0] = (UWORD)ma2;
			x1[1] = (UWORD)(ma2 >> BITSINWORD);
			if ( x1[1] ) n1 = -2;
			else         n1 = -1;
		}
		else {
			x1[0] = (UWORD)ma2;
			x1[1] = (UWORD)(ma2 >> BITSINWORD);
			if ( x1[1] ) n1 = 2;
			else         n1 = 1;
		}
		if ( MulLong(b,nb,x1,n1,x3,&n3) ) goto GcdErr;
		if ( AddLong(x2,n2,x3,n3,c,&n4) ) goto GcdErr;
		if ( mb1 < 0 ) {
			mb1 = -mb1;
			x1[0] = (UWORD)mb1;
			x1[1] = (UWORD)(mb1 >> BITSINWORD);
			if ( x1[1] ) n1 = -2;
			else         n1 = -1;
		}
		else {
			x1[0] = (UWORD)mb1;
			x1[1] = (UWORD)(mb1 >> BITSINWORD);
			if ( x1[1] ) n1 = 2;
			else         n1 = 1;
		}
		if ( MulLong(a,na,x1,n1,x2,&n2) ) goto GcdErr;
		if ( mb2 < 0 ) {
			mb2 = -mb2;
			x1[0] = (UWORD)mb2;
			x1[1] = (UWORD)(mb2 >> BITSINWORD);
			if ( x1[1] ) n1 = -2;
			else         n1 = -1;
		}
		else {
			x1[0] = (UWORD)mb2;
			x1[1] = (UWORD)(mb2 >> BITSINWORD);
			if ( x1[1] ) n1 = 2;
			else         n1 = 1;
		}
		if ( MulLong(b,nb,x1,n1,x3,&n3) ) goto GcdErr;
		if ( AddLong(x2,n2,x3,n3,x5,&n5) ) goto GcdErr;
		a = c; na = n4; b = x5; nb = n5;
		if ( nb == 0 ) { *nc = n4; goto normalend; }
		x4 = GLscrat9; 
		for ( i = 0; i < na; i++ ) x4[i] = a[i];
		a = x4;
		if ( na < 0 ) na = -na;
		if ( nb < 0 ) nb = -nb;
/*
		The typical case now is that in a we have the last step to go
		to loose the leading word, while in b we have lost the leading word.
		We could go to DivLong now but we can also add an extra step that
		is less wasteful.
		In the case that the new leading word of b is extrememly short (like 1)
		we make a rather large error of course. In the worst case the whole
		will be intercepted by DivLong after all, but that is so rare that
		it shouldn't influence any timing in a measurable way.
*/
		if ( nb >= GCDMAX && na == nb+1 && b[nb-1] >= HALFMAX && b[nb-1] > a[na-1] ) {
			lx = (((RLONG)(a[na-1]))<<BITSINWORD) + a[na-2];
			x1[0] = lx/b[nb-1]; n1 = 1;
			MulLong(b,nb,x1,n1,x2,&n2);
			n2 = -n2;
			AddLong(a,na,x2,n2,x4,&n4);
			if ( n4 == 0 ) {
				*nc = nb;
				for ( i = 0; i < nb; i++ ) c[i] = b[i];
				goto normalend;
			}
			if ( n4 < 0 ) n4 = -n4;
			a = b; na = nb; b = x4; nb = n4;
		}
		goto restart;
/*
  	#] New code : 
*/
	}
normalend:
	NumberFree(GLscrat6,"GcdLong"); NumberFree(GLscrat7,"GcdLong"); NumberFree(GLscrat8,"GcdLong");
	NumberFree(GLscrat9,"GcdLong"); NumberFree(GLscrat10,"GcdLong");
	return(0);
GcdErr:
	MLOCK(ErrorMessageLock);
	MesCall("GcdLong");
	MUNLOCK(ErrorMessageLock);
	NumberFree(GLscrat6,"GcdLong"); NumberFree(GLscrat7,"GcdLong"); NumberFree(GLscrat8,"GcdLong");
	NumberFree(GLscrat9,"GcdLong"); NumberFree(GLscrat10,"GcdLong");
	SETERROR(-1)
}

#endif

/*
 		#] GcdLong : 
 		#[ GetBinom :		WORD GetBinom(a,na,i1,i2)
*/

int GetBinom(UWORD *a, WORD *na, WORD i1, WORD i2)
{
	GETIDENTITY
	WORD j, k, l;
	UWORD *GBscrat3, *GBscrat4;
	if ( i1-i2 < i2 ) i2 = i1-i2;
	if ( i2 == 0 ) { *a = 1; *na = 1; return(0); }
	if ( i2 > i1 ) { *a = 0; *na = 0; return(0); }
	*a = i1; *na = 1;
	GBscrat3 = NumberMalloc("GetBinom"); GBscrat4 = NumberMalloc("GetBinom");
	for ( j = 2; j <= i2; j++ ) {
		GBscrat3[0] = i1+1-j;
		if ( MulLong(a,*na,GBscrat3,(WORD)1,GBscrat4,&k) ) goto CalledFrom;
		GBscrat3[0] = j;
		if ( DivLong(GBscrat4,k,GBscrat3,(WORD)1,a,na,GBscrat3,&l) ) goto CalledFrom;
	}
	NumberFree(GBscrat3,"GetBinom"); NumberFree(GBscrat4,"GetBinom");
	return(0);
CalledFrom:
	MLOCK(ErrorMessageLock);
	MesCall("GetBinom");
	MUNLOCK(ErrorMessageLock);
	NumberFree(GBscrat3,"GetBinom"); NumberFree(GBscrat4,"GetBinom");
	SETERROR(-1)
}

/*
 		#] GetBinom : 
 		#[ LcmLong :		WORD LcmLong(a,na,b,nb)

		Computes the LCM of the long numbers a and b and puts the result
		in c. c is allowed to be equal to a.
*/

int LcmLong(PHEAD UWORD *a, WORD na, UWORD *b, WORD nb, UWORD *c, WORD *nc)
{
	int error = 0;
	UWORD *d = NumberMalloc("LcmLong");
	UWORD *e = NumberMalloc("LcmLong");
	UWORD *f = NumberMalloc("LcmLong");
	WORD nd, ne, nf;
	GcdLong(BHEAD a, na, b, nb, d, &nd);
	DivLong(a,na,d,nd,e,&ne,f,&nf);
	if ( MulLong(b,nb,e,ne,c,nc) ) {
		MLOCK(ErrorMessageLock);
		MesCall("LcmLong");
		MUNLOCK(ErrorMessageLock);
		error = -1;
	}
	NumberFree(f,"LcmLong");
	NumberFree(e,"LcmLong");
	NumberFree(d,"LcmLong");
	return(error);
}

/*
 		#] LcmLong : 
 		#[ TakeLongRoot:	int TakeLongRoot(a,n,power)

	Takes the 'power'-root of the long number in a.
	If the root could be taken the return value is zero.
	If the root could not be taken, the return value is 1.
	The root will be in a if it could be taken, otherwise there will be garbage
	Algorithm: (assume b is guess of root, b' better guess)
		b' = (a-(power-1)*b^power)/(n*b^(power-1))
	Note: power should be positive!
*/

int TakeLongRoot(UWORD *a, WORD *n, WORD power)
{
	GETIDENTITY
	int numbits, guessbits, i, retval = 0;
	UWORD x, *b, *c, *d, *e;
	WORD na, nb, nc, nd, ne;
	if ( *n < 0 && ( power & 1 ) == 0 ) return(1);
	if ( power == 1 ) return(0);
	if ( *n < 0 ) { na = -*n; }
	else          { na =  *n; }
	if ( na == 1 ) {
/*			Special cases that are the most frequent */
		if ( a[0] == 1 ) return(0);
		if ( power < BITSINWORD && na == 1 && a[0] == (UWORD)(1<<power) ) {
			a[0] = 2; return(0);
		}
		if ( 2*power < BITSINWORD && na == 1 && a[0] == (UWORD)(1<<(2*power)) ) {
			a[0] = 4; return(0);
		}
	}
/*
	Step 1: make a guess. We count the number of bits.
	numbits will be the 1+2log(a)
*/
	numbits = BITSINWORD*(na-1);
	x = a[na-1];
	while ( ( x >> 8 ) != 0 ) { numbits += 8; x >>= 8; }
	if ( ( x >> 4 ) != 0 ) { numbits += 4; x >>= 4; }
	if ( ( x >> 2 ) != 0 ) { numbits += 2; x >>= 2; }
	if ( ( x >> 1 ) != 0 ) numbits++;
	guessbits = numbits / power;
	if ( guessbits <= 0 ) return(1); /* root < 2 and 1 we did already */
	nb = guessbits/BITSINWORD;
/*
	The recursion is:
	(b'-b) = (a/b^(power-1)-b)/n
	       = (a/c-b)/n
	       = (d-b)/n    (remainder of a/c is e)
	       = c/n  (we reuse the scratch array c)
	Termination can be tricky. When a/c has no remainder and = b we have a root.
	When d = b but the remainder of a/c != 0, there is definitely no root.
*/
	b = NumberMalloc("TakeLongRoot"); c = NumberMalloc("TakeLongRoot");
	d = NumberMalloc("TakeLongRoot"); e = NumberMalloc("TakeLongRoot"); 
	for ( i = 0; i < nb; i++ ) { b[i] = 0; }
	b[nb] = 1 << (guessbits%BITSINWORD);
	nb++;
	for(;;) {
		nc = nb;
		for ( i = 0; i < nb; i++ ) c[i] = b[i];
		if ( RaisPow(BHEAD c,&nc,power-1) ) goto TLcall;
		if ( DivLong(a,na,c,nc,d,&nd,e,&ne) ) goto TLcall;
		nb = -nb;
		if ( AddLong(d,nd,b,nb,c,&nc) ) goto TLcall;
		nb = -nb;
		if ( nc == 0 ) {
			if ( ne == 0 ) break;
			retval = 1; break;
/*
			else {
				NumberFree(b,"TakeLongRoot"); NumberFree(c,"TakeLongRoot");
				NumberFree(d,"TakeLongRoot"); NumberFree(e,"TakeLongRoot");
				return(1);
			}
*/
		}
		DivLong(c,nc,(UWORD *)(&power),1,d,&nd,e,&ne);
		if ( nd == 0 ) {
			retval = 1;
			break;
/*
			NumberFree(b,"TakeLongRoot"); NumberFree(c,"TakeLongRoot");
			NumberFree(d,"TakeLongRoot"); NumberFree(e,"TakeLongRoot");
			return(1);
*/
/*
			This code tries b+1 as a final possibility.
			We believe this is not needed
			UWORD one = 1;
			if ( AddLong(b,nb,&one,1,c,&nc) ) goto TLcall;
			if ( RaisPow(BHEAD c,&nc,power-1) ) goto TLcall;
			if ( DivLong(a,na,c,nc,d,&nd,e,&ne) ) goto TLcall;
			if ( ne != 0 ) return(1);
			nb = -nb;
			if ( SubLong(d,nd,b,nb,c,&nc) ) goto TLcall;
			nb = -nb;
			if ( nc != 0 ) {
				NumberFree(b,"TakeLongRoot"); NumberFree(c,"TakeLongRoot");
				NumberFree(d,"TakeLongRoot"); NumberFree(e,"TakeLongRoot");
				return(1);
			}
			break;
*/
		}
		if ( AddLong(b,nb,d,nd,b,&nb) ) goto TLcall;
	}
	for ( i = 0; i < nb; i++ ) a[i] = b[i];
	if ( *n < 0 ) *n = -nb;
	else          *n =  nb;
	NumberFree(b,"TakeLongRoot"); NumberFree(c,"TakeLongRoot");
	NumberFree(d,"TakeLongRoot"); NumberFree(e,"TakeLongRoot");
	return(retval);
TLcall:
	MLOCK(ErrorMessageLock);
	MesCall("TakeLongRoot");
	MUNLOCK(ErrorMessageLock);
	NumberFree(b,"TakeLongRoot"); NumberFree(c,"TakeLongRoot");
	NumberFree(d,"TakeLongRoot"); NumberFree(e,"TakeLongRoot");
	Terminate(-1);
	return(-1);
}

/*
 		#] TakeLongRoot: 
 		#[ MakeRational:

		Makes the integer a mod m into a traction b/c with |b|,|c| < sqrt(m)
		For the algorithm, see MakeLongRational.
*/

int MakeRational(WORD a,WORD m, WORD *b, WORD *c)
{
	LONG x1,x2,x3,x4,y1,y2;
	if ( a < 0 ) { a = a+m; }
	if ( a <= 1 ) {
		if ( a > m/2 ) a = a-m;
		*b = a; *c = 1; return(0);
	}
	x1 = m; x2 = a;
	if ( x2*x2 >= m ) {
		y1 = x1/x2; y2 = x1%x2; x3 = 1; x4 = -y1; x1 = x2; x2 = y2;
		while ( x2*x2 >= m ) {
			y1 = x1/x2; y2 = x1%x2; x1 = x2; x2 = y2; y2 = x3-y1*x4; x3 = x4; x4 = y2;
		}
	}
	else x4 = 1;
	if ( x2 == 0 ) { return(1); }
	if ( x2 > m/2 ) *b = x2-m;
	else            *b = x2;
	if ( x4 > m/2 ) { *c = x4-m; *c = -*c; *b = -*b; }
	else if ( x4 <= -m/2 ) { x4 += m; *c = x4; }
	else if ( x4 < 0 ) { x4 = -x4; *c = x4; *b = -*b; }
	else            *c = x4;
	return(0);
}

/*
 		#] MakeRational: 
 		#[ MakeLongRational:

		Converts the long number a mod m into the fraction b
		One of the properties of b is that num,den < sqrt(m)
		The algorithm: Start with:   m 0
		                             a 1
		Make now c=m%a, c1=m/a       c c2=0-c1*1
		Make now d=a%c  d1=a/c       d d2=1-d1*c2
		Make now e=c%d  e1=c/d       e e2=1-e1*d2
			etc till in the first column we get a number < sqrt(m)
			We have then f,f2 and the fraction is f/f2.
		If at any moment we get a zero, m contained an unlucky prime.

		Note that this can be made a lot faster when we make the same
		improvements as in the GCD routine. That is something for later.
#ifdef WITHMAKERATIONAL
*/

#define COPYLONG(x1,nx1,x2,nx2) { int i; for(i=0;i<ABS(nx2);i++)x1[i]=x2[i];nx1=nx2; }

int MakeLongRational(PHEAD UWORD *a, WORD na, UWORD *m, WORD nm, UWORD *b, WORD *nb)
{
	UWORD *root = NumberMalloc("MakeRational");
	UWORD *x1 = NumberMalloc("MakeRational");
	UWORD *x2 = NumberMalloc("MakeRational");
	UWORD *x3 = NumberMalloc("MakeRational");
	UWORD *x4 = NumberMalloc("MakeRational");
	UWORD *y1 = NumberMalloc("MakeRational");
	UWORD *y2 = NumberMalloc("MakeRational");
	WORD nroot,nx1,nx2,nx3,nx4,ny1,ny2 = 0,retval = 0;
	WORD sign = 1;
/*
	Step 1: Take the square root of m
*/
	COPYLONG(root,nroot,m,nm)
	TakeLongRoot(root,&nroot,2);
/*
	Step 2: Set the start values
*/
	if ( na < 0 ) { na = -na; sign = -sign; }
	COPYLONG(x1,nx1,m,nm)
	COPYLONG(x2,nx2,a,na)
/*
	x3[0] = 0, nx3 = 0;
	x4[0] = 1, nx4 = 1;
*/
/*
	The start operation needs some special attention because of the zero.
*/
	if ( BigLong(x2,nx2,root,nroot) <= 0 ) {
		x4[0] = 1, nx4 = 1;
		goto gottheanswer;
	}
	DivLong(x1,nx1,x2,nx2,y1,&ny1,y2,&ny2);
	if ( ny2 == 0 ) { retval = 1; goto cleanup; }
	COPYLONG(x1,nx1,x2,nx2)
	COPYLONG(x2,nx2,y2,ny2)
	x3[0] = 1; nx3 = 1;
	COPYLONG(x4,nx4,y1,ny1)
	nx4 = -nx4;
/*
	Now the loop.
*/
	while ( BigLong(x2,nx2,root,nroot) > 0 ) {
		DivLong(x1,nx1,x2,nx2,y1,&ny1,y2,&ny2);
		if ( ny2 == 0 ) { retval = 1; goto cleanup; }
		COPYLONG(x1,nx1,x2,nx2)
		COPYLONG(x2,nx2,y2,ny2)
		MulLong(y1,ny1,x4,nx4,y2,&ny2);
		ny2 = -ny2;
		AddLong(x3,nx3,y2,ny2,y1,&ny1);
		COPYLONG(x3,nx3,x4,nx4)
		COPYLONG(x4,nx4,y1,ny1)
	}
/*
	Now we have the answer. It is x2/x4. It has to be packed into b.
*/
gottheanswer:
	if ( nx4 < 0 ) { sign = -sign; nx4 = -nx4; }
	COPYLONG(b,*nb,x2,nx2)
	Pack(b,nb,x4,nx4);
	if ( sign < 0 ) *nb = -*nb;
cleanup:
	NumberFree(y2,"MakeRational");
	NumberFree(y1,"MakeRational");
	NumberFree(x4,"MakeRational");
	NumberFree(x3,"MakeRational");
	NumberFree(x2,"MakeRational");
	NumberFree(x1,"MakeRational");
	NumberFree(root,"MakeRational");
	return(retval);
}

/*
#endif
 		#] MakeLongRational: 
 		#[ ChineseRemainder:
*/
/**
 *		Routine takes a1 mod m1 and a2 mod m2 and returns a mod m1*m2 with
 *		a mod m1 = a1 and a mod m2 = a2
 *	Chinese remainder:
 *		a%(m1*m2) = q1*m1+a1
 *		a%(m1*m2) = q2*m2+a2
 *	Compute n1 such that (n1*m1)%m2 is one
 *	Compute n2 such that (n2*m2)%m1 is one
 *	Then (a1*n2*m2+a2*n1*m1)%(m1*m2) is a%(m1*m2)
 *
 */
#ifdef WITHCHINESEREMAINDER

int ChineseRemainder(PHEAD MODNUM *a1, MODNUM *a2, MODNUM *a)
{
	UWORD *inv1 = NumberMalloc("ChineseRemainder");
	UWORD *inv2 = NumberMalloc("ChineseRemainder");
	UWORD *fac1 = NumberMalloc("ChineseRemainder");
	UWORD *fac2 = NumberMalloc("ChineseRemainder");
	UWORD two[1];
	WORD ninv1, ninv2, nfac1, nfac2;
	if ( a1->na < 0 ) {
		AddLong(a1->a,a1->na,a1->m,a1->nm,a1->a,&(a1->na));
	}
	if ( a2->na < 0 ) {
		AddLong(a2->a,a2->na,a2->m,a2->nm,a2->a,&(a2->na));
	}
	MulLong(a1->m,a1->nm,a2->m,a2->nm,a->m,&(a->nm));

	GetLongModInverses(BHEAD a1->m,a1->nm,a2->m,a2->nm,inv1,&ninv1,inv2,&ninv2);
	MulLong(inv1,ninv1,a1->m,a1->nm,fac1,&nfac1);
	MulLong(inv2,ninv2,a2->m,a2->nm,fac2,&nfac2);

	MulLong(fac1,nfac1,a2->a,a2->na,inv1,&ninv1);
	MulLong(fac2,nfac2,a1->a,a1->na,inv2,&ninv2);
	AddLong(inv1,ninv1,inv2,ninv2,a->a,&(a->na));

	two[0] = 2;
	MulLong(a->a,a->na,two,1,fac1,&nfac1);
	if ( BigLong(fac1,nfac1,a->m,a->nm) > 0 ) {
		a->nm = -a->nm;
		AddLong(a->a,a->na,a->m,a->nm,a->a,&(a->na));
		a->nm = -a->nm;
	}
	NumberFree(fac2,"ChineseRemainder");
	NumberFree(fac1,"ChineseRemainder");
	NumberFree(inv2,"ChineseRemainder");
	NumberFree(inv1,"ChineseRemainder");
	return(0);
}

#endif

/*
 		#] ChineseRemainder: 
  	#] RekenLong : 
  	#[ RekenTerms :
 		#[ CompCoef :		WORD CompCoef(term1,term2)

	Compares the coefficients of term1 and term2 by subtracting them.
	This does more work than needed but this routine is only called
	when sorting functions and function arguments.
	(and comparing values 
*/
/* #define 64SAVE */

WORD CompCoef(WORD *term1, WORD *term2)
{
	GETIDENTITY
	UWORD *c;
	WORD n1,n2,n3,*a;
	GETCOEF(term1,n1);
	GETCOEF(term2,n2);
	if ( term1[1] == 0 && n1 == 1 ) {
		if ( term2[1] == 0 && n2 == 1 ) return(0);
		if ( n2 < 0 ) return(1);
		return(-1);
	}
	else if ( term2[1] == 0 && n2 == 1 ) {
		if ( n1 < 0 ) return(-1);
		return(1);
	}
	if ( n1 > 0 ) {
		if ( n2 < 0 ) return(1);
	}
	else {
		if ( n2 > 0 ) return(-1);
		a = term1; term1 = term2; term2 = a;
		n3 = -n1; n1 = -n2; n2 = n3;
	}
	if ( term1[1] == 1 && term2[1] == 1 && n1 == 1 && n2 == 1 ) {
		if ( (UWORD)*term1 > (UWORD)*term2 ) return(1);
		else if ( (UWORD)*term1 < (UWORD)*term2 ) return(-1);
		else return(0);
	}

/*
	The next call should get dedicated code, as AddRat does more than
	strictly needed. Also more attention should be given to overflow.
*/
	c = NumberMalloc("CompCoef");
	if ( AddRat(BHEAD (UWORD *)term1,n1,(UWORD *)term2,-n2,c,&n3) ) {
		MLOCK(ErrorMessageLock);
		MesCall("CompCoef");
		MUNLOCK(ErrorMessageLock);
		NumberFree(c,"CompCoef");
		SETERROR(-1)
	}
	NumberFree(c,"CompCoef");
	return(n3);
}

/*
 		#] CompCoef : 
 		#[ Modulus :		WORD Modulus(term)

	Routine takes the coefficient of term modulus b. The answer
	is in term again and the length of term is adjusted.

*/

int Modulus(WORD *term)
{
	WORD *t;
	WORD n1;
	t = term;
	GETCOEF(t,n1);
	if ( TakeModulus((UWORD *)t,&n1,AC.cmod,AC.ncmod,UNPACK) ) {
		MLOCK(ErrorMessageLock);
		MesCall("Modulus");
		MUNLOCK(ErrorMessageLock);
		SETERROR(-1)
	}
	if ( !n1 ) {
		*term = 0;
		return(0);
	}
	else if ( n1 > 0 ) {
		n1 *= 2;
		t += n1;		/* Note that n1 >= 0 */
		n1++;
	}
	else if ( n1 < 0 ) {
		n1 *= 2;
		t += -n1;
		n1--;
	}
	*t++ = n1;
	*term = WORDDIF(t,term);
	return(0);
}

/*
 		#] Modulus : 
 		#[ TakeModulus :	WORD TakeModulus(a,na,cmodvec,ncmod,par)

		Routine gets the rational number in a with reduced length na.
		It is called when AC.ncmod != 0 and the number in AC.cmod is the
		number wrt which we need the modulus.
		The result is returned in a and na again.

		If par == NOUNPACK we only do a single number, not a fraction.
		In addition we don't do fancy. We want a positive number and
		the input was supposed to be positive.
		We don't pack the result. The calling routine is responsible for that.
		This may not be a good idea. To be checked.
*/

int TakeModulus(UWORD *a, WORD *na, UWORD *cmodvec, WORD ncmod, WORD par)
{
	GETIDENTITY
	UWORD *c, *d, *e, *f, *g, *h;
	UWORD *x4,*x2;
	UWORD *x3,*x1,*x5,*x6,*x7,*x8;
	WORD y3,y1,y5,y6;
	WORD n1, i, y2, y4;
	WORD nh, tdenom, tnumer, nmod;
	LONG x;
	if ( ncmod == 0 ) return(0);		/* No modulus operation */
	nmod = ABS(ncmod);
	n1 = *na;
	if ( ( par & UNPACK ) != 0 ) UnPack(a,n1,&tdenom,&tnumer);
	else { tnumer = n1; }
/*
	We fish out the special case that the coefficient is short as well. 
	There is no need to make lots of calls etc
*/
	if ( ( ( par & UNPACK ) == 0 ) && nmod == 1 && ( n1 == 1 || n1 == -1 ) ) {
		goto simplecase;
	}
	else if ( nmod == 1 && ( n1 == 1 || n1 == -1 ) ) {
		if ( a[1] != 1 ) {
			a[1] = a[1] % cmodvec[0];
			if ( a[1] == 0 ) {
				MesPrint("Division by zero in short modulus arithmetic");
				return(-1);
			}
			y1 = 0;
			if ( ( AC.modinverses != 0 ) && ( ( par & NOINVERSES ) == 0 ) ) {
				y1 = AC.modinverses[a[1]];
			}
			else {
				GetModInverses(a[1],cmodvec[0],&y1,&y2);
			}
			x = a[0];
			a[0] = (x*y1) % cmodvec[0];
			a[1] = 1;
		}
		else {
simplecase:
			a[0] = a[0] % cmodvec[0];
		}
		if ( a[0] == 0 ) { *na = 0; return(0); }
		if ( ( AC.modmode & POSNEG ) != 0 ) {
			if ( a[0] > (UWORD)(cmodvec[0]/2) ) {
				a[0] =  cmodvec[0] - a[0];
				*na = -*na;
			}
		}
		else if ( *na < 0 ) {
			*na = 1; a[0] = cmodvec[0] - a[0];
		}
		return(0);
	}
	c = NumberMalloc("TakeModulus"); d = NumberMalloc("TakeModulus"); e = NumberMalloc("TakeModulus");
	f = NumberMalloc("TakeModulus"); g = NumberMalloc("TakeModulus"); h = NumberMalloc("TakeModulus");
	n1 = ABS(n1);
	if ( DivLong(a,tnumer,(UWORD *)cmodvec,nmod,
		c,&nh,a,&tnumer) ) goto ModErr;
	if ( tnumer == 0 ) { *na = 0; goto normalreturn; }
	if ( ( par & UNPACK ) == 0 ) {
		if ( ( AC.modmode & POSNEG ) != 0 ) {
			NormalModulus(a,&tnumer);
		}
		else if ( tnumer < 0 ) {
			SubPLon((UWORD *)cmodvec,nmod,a,-tnumer,a,&tnumer);
		}
		*na = tnumer;
		goto normalreturn;
	}
	if ( tdenom == 1 && a[n1] == 1 ) {
		if ( ( AC.modmode & POSNEG ) != 0 ) {
			NormalModulus(a,&tnumer);
		}
		else if ( tnumer < 0 ) {
			SubPLon((UWORD *)cmodvec,nmod,a,-tnumer,a,&tnumer);
		}
		*na = tnumer;
		i = ABS(tnumer);
		a += i;
		*a++ = 1;
		while ( --i > 0 ) *a++ = 0;
		goto normalreturn;
	}
	if ( DivLong(a+n1,tdenom,(UWORD *)cmodvec,nmod,c,&nh,a+n1,&tdenom) ) goto ModErr;
	if ( !tdenom ) {
		MLOCK(ErrorMessageLock);
		MesPrint("Division by zero in modulus arithmetic");
		if ( AP.DebugFlag ) {
			AO.OutSkip = 3;
			FiniLine();
			i = *na;
			if ( i < 0 ) i = -i;
			while ( --i >= 0 ) { TalToLine((UWORD)(*a++)); TokenToLine((UBYTE *)"  "); }
			i = *na;
			if ( i < 0 ) i = -i;
			while ( --i >= 0 ) { TalToLine((UWORD)(*a++)); TokenToLine((UBYTE *)"  "); }
			TalToLine((UWORD)(*na));
			AO.OutSkip = 0;
			FiniLine();
		}
		MUNLOCK(ErrorMessageLock);
		NumberFree(c,"TakeModulus"); NumberFree(d,"TakeModulus"); NumberFree(e,"TakeModulus");
		NumberFree(f,"TakeModulus"); NumberFree(g,"TakeModulus"); NumberFree(h,"TakeModulus");
		return(-1);
	}
	if ( ( AC.modinverses != 0 ) && ( ( par & NOINVERSES ) == 0 )
	&& ( tdenom == 1 || tdenom == -1 ) ) {
		*d = AC.modinverses[a[n1]]; y1 = 1; y2 = tdenom;
		if ( MulLong(a,tnumer,d,y1,c,&y3) ) goto ModErr;
		if ( DivLong(c,y3,(UWORD *)cmodvec,nmod,d,&y5,a,&tdenom) ) goto ModErr;
		if ( y2 < 0 ) tdenom = -tdenom;
	}
	else {
	  x2 = (UWORD *)cmodvec; x1 = c; i = nmod; while ( --i >= 0 ) *x1++ = *x2++;
	  x1 = c; x2 = a+n1; x3 = d; x4 = e; x5 = f; x6 = g;
	  y1 = nmod; y2 = tdenom; y4 = 0; y5 = 1; *x5 = 1;
	  for(;;) {
		if ( DivLong(x1,y1,x2,y2,h,&nh,x3,&y3) ) goto ModErr;
		if ( MulLong(x5,y5,h,nh,x6,&y6) ) goto ModErr;
		if ( AddLong(x4,y4,x6,-y6,x6,&y6) ) goto ModErr;
		if ( !y3 ) {
			if ( y2 != 1 || *x2 != 1 ) {
				MLOCK(ErrorMessageLock);
				MesPrint("Inverse in modulus arithmetic doesn't exist");
				MesPrint("Denominator and modulus are not relative prime");
				MUNLOCK(ErrorMessageLock);
				goto ModErr;
			}
			break;
		}
		x7 = x1; x1 = x2; y1 = y2; x2 = x3; y2 = y3; x3 = x7;
		x8 = x4; x4 = x5; y4 = y5; x5 = x6; y5 = y6; x6 = x8;
	  }
	  if ( y5 < 0 && AddLong((UWORD *)cmodvec,nmod,x5,y5,x5,&y5) ) goto ModErr;
	  if ( MulLong(a,tnumer,x5,y5,c,&y3) ) goto ModErr;
	  if ( DivLong(c,y3,(UWORD *)cmodvec,nmod,d,&y5,a,&tdenom) ) goto ModErr;
	}
	if ( !tdenom ) { *na = 0; goto normalreturn; }
	if ( ( ( AC.modmode & POSNEG ) != 0 ) && ( ( par & FROMFUNCTION ) == 0 ) ) {
		NormalModulus(a,&tdenom);
	}
	else if ( tdenom < 0 ) {
		SubPLon((UWORD *)cmodvec,nmod,a,-tdenom,a,&tdenom);
	}
	*na = tdenom;
	i = ABS(tdenom);
	a += i;
	*a++ = 1;
	while ( --i > 0 ) *a++ = 0;
normalreturn:
	NumberFree(c,"TakeModulus"); NumberFree(d,"TakeModulus"); NumberFree(e,"TakeModulus");
	NumberFree(f,"TakeModulus"); NumberFree(g,"TakeModulus"); NumberFree(h,"TakeModulus");
	return(0);
ModErr:
	MLOCK(ErrorMessageLock);
	MesCall("TakeModulus");
	MUNLOCK(ErrorMessageLock);
	NumberFree(c,"TakeModulus"); NumberFree(d,"TakeModulus"); NumberFree(e,"TakeModulus");
	NumberFree(f,"TakeModulus"); NumberFree(g,"TakeModulus"); NumberFree(h,"TakeModulus");
	SETERROR(-1)
}

/*
 		#] TakeModulus : 
 		#[ TakeNormalModulus :  WORD TakeNormalModulus(a,na,par)

		added by Jan [01-09-2010]
*/

int TakeNormalModulus (UWORD *a, WORD *na, UWORD *c, WORD nc, WORD par)
{
	WORD n;
	WORD nhalfc;
	UWORD *halfc;

	GETIDENTITY;
	
	/* determine c/2 by right shifting */
	halfc = NumberMalloc("TakeNormalModulus");
	nhalfc=nc;
	WCOPY(halfc,c,nc);

	for (n=0; n<nhalfc; n++) {
		halfc[n] /= 2;
		if (n+1<nc) halfc[n] |= c[n+1] << (BITSINWORD-1);
	}

	if (halfc[nhalfc-1]==0)
		nhalfc--;
				 
	/* takes care of the number never expanding, e.g., -1(mod 100) -> 99 -> -1 */
	if (BigLong(a,ABS(*na),halfc,nhalfc) > 0) {
	
		TakeModulus(a,na,c,nc,par);
	
		n = ABS(*na);
		if (BigLong(a,n,halfc,nhalfc) > 0) {
			SubPLon(c,nc,a,n,a,&n);
			*na = (*na > 0 ? -n : n);
		}
	}

	NumberFree(halfc,"TakeNormalModulus");
	return(0);
}

/*
 		#] TakeNormalModulus : 
 		#[ MakeModTable :	WORD MakeModTable()
*/

int MakeModTable(void)
{
	LONG size, i, j, n;
	n = ABS(AC.ncmod);
	if ( AC.modpowers ) {
		M_free(AC.modpowers,"AC.modpowers");
		AC.modpowers = NULL;
	}
	if ( n > 2 ) {
		MLOCK(ErrorMessageLock);
		MesPrint("&No memory for modulus generator power table");
		MUNLOCK(ErrorMessageLock);
		Terminate(-1);
	}
	if ( n == 0 ) return(0);
	size = (LONG)(*AC.cmod);
	if ( n == 2 ) size += (((LONG)AC.cmod[1])<<BITSINWORD);
	AC.modpowers = (UWORD *)Malloc1(size*n*sizeof(UWORD),"table for powers of modulus");
	if ( n == 1 ) {
		j = 1;
		for ( i = 0; i < size; i++ ) AC.modpowers[i] = 0;
		for ( i = 0; i < size; i++ ) {
			AC.modpowers[j] = (WORD)i;
			j *= *AC.powmod;
			j %= *AC.cmod;
		}
		for ( i = 2; i < size; i++ ) {
			if ( AC.modpowers[i] == 0 ) {
				MLOCK(ErrorMessageLock);
				MesPrint("&improper generator for this modulus");
				MUNLOCK(ErrorMessageLock);
				M_free(AC.modpowers,"AC.modpowers");
				return(-1);
			}
		}
		AC.modpowers[1] = 0;
	}
	else {
		GETIDENTITY
		WORD nScrat, n2;
		UWORD *MMscrat7 = NumberMalloc("MakeModTable"), *MMscratC = NumberMalloc("MakeModTable");
		*MMscratC = 1;
		nScrat = 1;
		j = size * 2;
		for ( i = 0; i < j; i+=2 ) { AC.modpowers[i] = 0; AC.modpowers[i+1] = 0; }
		for ( i = 0; i < size; i++ ) {
			j = *MMscratC + (((LONG)MMscratC[1])<<BITSINWORD);
			j *= 2;
			AC.modpowers[j] = (WORD)(i & WORDMASK);
			AC.modpowers[j+1] = (WORD)(i >> BITSINWORD);
			MulLong((UWORD *)MMscratC,nScrat,(UWORD *)AC.powmod,
			AC.npowmod,(UWORD *)MMscrat7,&n2);
			TakeModulus(MMscrat7,&n2,AC.cmod,AC.ncmod,NOUNPACK);
			*MMscratC = *MMscrat7; MMscratC[1] = MMscrat7[1]; nScrat = n2;
		}
		NumberFree(MMscrat7,"MakeModTable"); NumberFree(MMscratC,"MakeModTable");
		j = size * 2;
		for ( i = 4; i < j; i+=2 ) {
			if ( AC.modpowers[i] == 0 && AC.modpowers[i+1] == 0 ) {
				MLOCK(ErrorMessageLock);
				MesPrint("&improper generator for this modulus");
				MUNLOCK(ErrorMessageLock);
				M_free(AC.modpowers,"AC.modpowers");
				return(-1);
			}
		}
		AC.modpowers[2] = AC.modpowers[3] = 0;
	}
	return(0);
}

/*
 		#] MakeModTable : 
  	#] RekenTerms : 
  	#[ Functions :
 		#[ Factorial :		WORD Factorial(n,a,na)

	Starts with only the value of fac_(0).
	Builds up what is needed and remembers it for the next time.

	We have:
		AT.nfac: the number of the highest stored factorial
		AT.pfac: the array of locations in the array of stored factorials
		AT.factorials: the array with stored factorials
*/

int Factorial(PHEAD WORD n, UWORD *a, WORD *na)
{
	GETBIDENTITY
	UWORD *b, *c;
	WORD nc;
	int i, j;
	LONG ii;
	if ( n > AT.nfac ) {
		if ( AT.factorials == 0 ) {
			AT.nfac = 0; AT.mfac = 50; AT.sfact = 400;
			AT.pfac = (LONG *)Malloc1((AT.mfac+2)*sizeof(LONG),"factorials");
			AT.factorials = (UWORD *)Malloc1(AT.sfact*sizeof(UWORD),"factorials");
			AT.factorials[0] = 1; AT.pfac[0] = 0; AT.pfac[1] = 1;
		}
		b = a;
		c = AT.factorials+AT.pfac[AT.nfac];
		nc = i = AT.pfac[AT.nfac+1] - AT.pfac[AT.nfac];
		while ( --i >= 0 ) *b++ = *c++;
		for ( j = AT.nfac+1; j <= n; j++ ) {
			Product(a,&nc,j);
			if ( nc > AM.MaxTal ) {
				MLOCK(ErrorMessageLock);
				MesPrint("Overflow in factorial. MaxTal = %d",AM.MaxTal);
				MesPrint("Increase MaxTerm in %s",setupfilename);
				MUNLOCK(ErrorMessageLock);
				return(-1);
			}
			if ( j > AT.mfac ) {  /* double the pfac buffer */
				LONG *p;
				p = (LONG *)Malloc1((AT.mfac*2+2)*sizeof(LONG),"factorials");
				i = AT.mfac;
				for ( i = AT.mfac+1; i >= 0; i-- ) p[i] = AT.pfac[i];
				M_free(AT.pfac,"factorial offsets"); AT.pfac = p; AT.mfac *= 2;
			}
			if ( AT.pfac[j] + nc >= AT.sfact ) { /* double the factorials buffer */
				UWORD *f;
				f = (UWORD *)Malloc1(AT.sfact*2*sizeof(UWORD),"factorials");
				ii = AT.sfact;
				c = AT.factorials; b = f;
				while ( --ii >= 0 ) *b++ = *c++;
				M_free(AT.factorials,"factorials");
				AT.factorials = f;
				AT.sfact *= 2;
			}
			b = a; c = AT.factorials + AT.pfac[j]; i = nc;
			while ( --i >= 0 ) *c++ = *b++;
			AT.pfac[j+1] = AT.pfac[j] + nc;
		}
		*na = nc;
		AT.nfac = n;
	}
	else if ( n == 0 ) {
		*a = 1; *na = 1;
	}
	else {
		*na = i = AT.pfac[n+1] - AT.pfac[n];
		b = AT.factorials + AT.pfac[n];
		while ( --i >= 0 ) *a++ = *b++;
	}
	return(0);
}

/*
 		#] Factorial : 
 		#[ Bernoulli :		WORD Bernoulli(n,a,na)

	Starts with only the value of bernoulli_(0).
	Builds up what is needed and remembers it for the next time.
	b_0 = 1
	(n+1)*b_n = -b_{n-1}-sum_(i,1,n-1,b_i*b_{n-i})
	The n-1 plays only a role for b_2.
	We have hard coded b_0,b_1,b_2 and b_odd. After that:
	(2n+1)*b_2n = -sum_(i,1,n-1,b_2i*b_{2n-2i})

	We have:
		AT.nBer: the number of the highest stored Bernoulli number
		AT.pBer: the array of locations in the array of stored Bernoulli numbers
		AT.bernoullis: the array with stored Bernoulli numbers
*/

int Bernoulli(WORD n, UWORD *a, WORD *na)
{
	GETIDENTITY
	UWORD *b, *c, *scrib, *ntop, *ntop1;
	WORD i, i1, i2, nhalf, nqua, nscrib, nntop, nntop1, *oldworkpointer;
	UWORD twee = 2, twonplus1;
	int j;
	LONG ii;
	if ( n <= 1 ) {
		if ( n == 0 ) { a[0] = a[1] = 1; *na = 3; }
		else if ( n == 1 ) { a[0] = 1; a[1] = 2; *na = 3; }
		return(0);
	}
	if ( ( n & 1 ) != 0 ) { a[0] = a[1] = 0; *na = 0; return(0); }
	nhalf = n/2;
	if ( nhalf > AT.nBer ) {
		oldworkpointer = AT.WorkPointer;
		if ( AT.bernoullis == 0 ) {
			AT.nBer = 1; AT.mBer = 50; AT.sBer = 400;
			AT.pBer = (LONG *)Malloc1((AT.mBer+2)*sizeof(LONG),"bernoullis");
			AT.bernoullis = (UWORD *)Malloc1(AT.sBer*sizeof(UWORD),"bernoullis");
			AT.pBer[1] = 0; AT.pBer[2] = 3;
			AT.bernoullis[0] = 3; AT.bernoullis[1] = 1; AT.bernoullis[2] = 12;
			if ( nhalf == 1 ) {
				a[0] = 1; a[1] = 12; *na = 3; return(0);
			}
		}
		while ( nhalf > AT.mBer ) {
			LONG *p;
			p = (LONG *)Malloc1((AT.mBer*2+1)*sizeof(LONG),"bernoullis");
			i = AT.mBer;
			for ( i = AT.mBer; i >= 0; i-- ) p[i] = AT.pBer[i];
			M_free(AT.pBer,"factorial pointers"); AT.pBer = p; AT.mBer *= 2;
		}
		for ( n = AT.nBer+1; n <= nhalf; n++ ) {
			scrib = (UWORD *)(AT.WorkPointer);
			nqua = n/2;
			if ( ( n & 1 ) == 1 ) {
				nscrib = 0; ntop = scrib;
			}
			else {
				b = AT.bernoullis + AT.pBer[nqua];
				nscrib = *b++;
				i = (WORD)(REDLENG(nscrib));
				MulRat(BHEAD b,i,b,i,scrib,&nscrib);
				ntop = scrib + 2*nscrib;
				nqua--;
			}
			for ( j = 1; j <= nqua; j++ ) {
				b = AT.bernoullis + AT.pBer[j];
				c = AT.bernoullis + AT.pBer[n-j];
				i1 = (WORD)(*b); i2 = (WORD)(*c);
				i1 = REDLENG(i1);
				i2 = REDLENG(i2);
				MulRat(BHEAD b+1,i1,c+1,i2,ntop,&nntop);
				Mully(BHEAD ntop,&nntop,&twee,1);
				if ( nscrib ) {
					i = (WORD)nntop; if ( i < 0 ) i = -i;
					ntop1 = ntop + 2*i;
					AddRat(BHEAD ntop,nntop,scrib,nscrib,ntop1,&nntop1);
				}
				else {
					ntop1 = ntop; nntop1 = nntop;
				}
				nscrib = i1 = (WORD)nntop1;
				if ( i1 < 0 ) i1 = - i1;
				i1 = 2*i1;
				for ( i = 0; i < i1; i++ ) scrib[i] = ntop1[i];
				ntop = scrib + i1; 
			}
			twonplus1 = 2*n+1;
			Divvy(BHEAD scrib,&nscrib,&twonplus1,-1);
			i1 = INCLENG(nscrib);
			i2 = i1; if ( i2 < 0 ) i2 = -i2;
			i = (WORD)(AT.bernoullis[AT.pBer[n-1]]);
			if ( i < 0 ) i = -i;
			AT.pBer[n] = AT.pBer[n-1]+i;
			if ( AT.pBer[n] + i2 >= AT.sBer ) {
				UWORD *f;
				f = (UWORD *)Malloc1(AT.sBer*2*sizeof(UWORD),"bernoullis");
				ii = AT.sBer;
				c = AT.bernoullis; b = f;
				while ( --ii >= 0 ) *b++ = *c++;
				M_free(AT.bernoullis,"bernoullis");
				AT.bernoullis = f;
				AT.sBer *= 2;
			}
			c = AT.bernoullis + AT.pBer[n]; b = scrib;
			*c++ = i1;
			for ( i = 1; i < i2; i++ ) *c++ = *b++;
		}
		AT.nBer = nhalf;
		AT.WorkPointer = oldworkpointer;
	}
	b = AT.bernoullis + AT.pBer[nhalf];
	*na = i = (WORD)(*b++);
	if ( i < 0 ) i = -i;
	i--;
	while ( --i >= 0 ) *a++ = *b++;
	return(0);
}

/*
 		#] Bernoulli : 
 		#[ NextPrime :
*/
/**
 *	Gives the next prime number in the list of prime numbers.
 *
 *	If the list isn't long enough we expand it.
 *	For ease in ParForm and because these lists shouldn't be very big
 *	we let each worker keep its own list.
 *
 *	The list is cut off at MAXPOWER, because we don't want to get into
 *	trouble that the power of a variable gets larger than the prime number.
 */

#if ( BITSINWORD == 32 )

void StartPrimeList(PHEAD0)
{
	int i, j;
	AR.PrimeList[AR.numinprimelist++] = 3;
	for ( i = 5; i < 46340; i += 2 ) {
		for ( j = 0; j < AR.numinprimelist && AR.PrimeList[j]*AR.PrimeList[j] <= i; j++ ) {
			if ( i % AR.PrimeList[j] == 0 ) goto nexti;
		}
		AR.PrimeList[AR.numinprimelist++] = i;
nexti:;
	}
	AR.notfirstprime = 1;
}

#endif

WORD NextPrime(PHEAD WORD num)
{
	int i, j;
	WORD *newpl;
	LONG newsize, x;
#if ( BITSINWORD == 32 )
	if ( AR.notfirstprime == 0 ) StartPrimeList(BHEAD0);
#endif
	if ( num > AT.inprimelist ) {
		while ( AT.inprimelist < num ) {
			if ( num >= AT.sizeprimelist ) {
				if ( AT.sizeprimelist == 0 ) newsize = 32;
				else newsize = 2*AT.sizeprimelist;
				while ( num >= newsize ) newsize = newsize*2;
				newpl = (WORD *)Malloc1(newsize*sizeof(WORD),"NextPrime");
				for ( i = 0; i < AT.sizeprimelist; i++ ) {
					newpl[i] = AT.primelist[i];
				}
				if ( AT.sizeprimelist > 0 ) {
					M_free(AT.primelist,"NextPrime");
				}
				AT.sizeprimelist = newsize;
				AT.primelist = newpl;
			}
			if ( AT.inprimelist < 0 ) { i = MAXPOSITIVE; }
			else { i = AT.primelist[AT.inprimelist]; }
			while ( i > MAXPOWER ) {
				i -= 2; x = i;
#if ( BITSINWORD == 32 )
				for ( j = 0; j < AR.numinprimelist && AR.PrimeList[j]*(LONG)(AR.PrimeList[j]) <= x; j++ ) {
					if ( x % AR.PrimeList[j] == 0 ) goto nexti;
				}
#else
				for ( j = 3; j*((LONG)j) <= x; j += 2 ) {
					if ( x % j == 0 ) goto nexti;
				}
#endif
				AT.inprimelist++;
				AT.primelist[AT.inprimelist] = i;
				break;
nexti:;
			}
			if ( i < MAXPOWER ) {
				MLOCK(ErrorMessageLock);
				MesPrint("There are not enough short prime numbers for this calculation");
				MesPrint("Try to use a computer with a %d-bits architecture",
					(int)(BITSINWORD*4));
				MUNLOCK(ErrorMessageLock);
				Terminate(-1);
			}
		}
	}
	return(AT.primelist[num]);
}
 
/*
 		#] NextPrime : 
 		#[ Moebius :

		Returns the value of the Moebius function if n fits inside
		a WORD.
		The method we use is a bit like the sieve of Erathostenes.
*/

WORD Moebius(PHEAD WORD nn)
{
	WORD i,n = nn, x;
	LONG newsize;
	SBYTE *newtable, mu;
#if ( BITSINWORD == 32 )
	if ( AR.notfirstprime == 0 ) StartPrimeList(BHEAD0);
#endif
/*
	First we make sure that:
		a: the table is big enough.
		b: the number is not already in the table.
*/
	if ( nn >= AR.moebiustablesize ) {
		if ( AR.moebiustablesize <= 0 ) { newsize = (LONG)nn + 20; }
		else { newsize = (LONG)nn*2; }
		if ( newsize > MAXPOSITIVE ) newsize = MAXPOSITIVE;
		newtable = (SBYTE *)Malloc1(newsize*sizeof(SBYTE),"Moebius");
		for ( i = 0; i < AR.moebiustablesize; i++ ) newtable[i] = AR.moebiustable[i];
		for ( ; i < newsize; i++ ) newtable[i] = 2;
		if ( AR.moebiustablesize > 0 ) M_free(AR.moebiustable,"Moebius");
		AR.moebiustable = newtable;			
		AR.moebiustablesize = newsize;
	}
	/* NOTE: nn == MAXPOSITIVE never fits in moebiustable. */
	if ( nn != MAXPOSITIVE && AR.moebiustable[nn] != 2 ) return((WORD)AR.moebiustable[nn]);
	mu = 1;
	if ( n == 1 ) goto putvalue;
	if ( n % 2 == 0 ) {
		n /= 2;
		if ( n % 2 == 0 ) { mu = 0; goto putvalue; }
		if ( AR.moebiustable[n] != 2 ) { mu = -AR.moebiustable[n]; goto putvalue; }
		mu = -mu;
		if ( n == 1 ) goto putvalue;
	}
#if ( BITSINWORD == 32 )
	for ( i = 0; i < AR.numinprimelist; i++ ) {
		x = AR.PrimeList[i];
#else
	for ( x = 3; x < MAXPOSITIVE; x += 2 ) {
#endif
		if ( n % x == 0 ) {
			n /= x;
			if ( n % x == 0 ) { mu = 0; goto putvalue; }
			if ( AR.moebiustable[n] != 2 ) { mu = -AR.moebiustable[n]; goto putvalue; }
			mu = -mu;
			if ( n == 1 ) goto putvalue;
		}
		if ( n < x*x ) break; /* notice that x*x always fits inside a WORD */
	}
	mu = -mu;
putvalue:
	if ( nn != MAXPOSITIVE ) AR.moebiustable[nn] = mu;
	return((WORD)mu);
}

/*
 		#] Moebius : 
 		#[ wranf :

		A random number generator that generates random WORDs with a very
		long sequence. It is based on the Knuth generator.

		We take some care that each thread can run its own, but each
		uses its own startup. Hence the seed includes the identity of
		the thread.

		For NPAIR1, NPAIR2 we can use any pair from the table on page 28.
		Candidates are 24,55 (the example on the pages 171,172)
		or (33,97) or (38,89)
		These values are defined in fsizes.h and used in startup.c and threads.c
*/

#define WARMUP 6

static void wranfnew(PHEAD0)
{
	int i;
	LONG j;
	for ( i = 0; i < AR.wranfnpair1; i++ ) {
		j = AR.wranfia[i] - AR.wranfia[i+(AR.wranfnpair2-AR.wranfnpair1)];
		if ( j < 0 ) j += (LONG)1 << (2*BITSINWORD-2);
		AR.wranfia[i] = j;
	}
	for ( i = AR.wranfnpair1; i < AR.wranfnpair2; i++ ) {
		j = AR.wranfia[i] - AR.wranfia[i-AR.wranfnpair1];
		if ( j < 0 ) j += (LONG)1 << (2*BITSINWORD-2);
		AR.wranfia[i] = j;
	}
}

void iniwranf(PHEAD0)
{
	int imax = AR.wranfnpair2-1;
	ULONG i, ii, seed = AR.wranfseed;
	LONG j, k;
	ULONG offset = 12345;
#ifdef PARALLELCODE
	int id;
#if defined(WITHPTHREADS)
	id = AT.identity;
#elif defined(WITHMPI)
	id = PF.me;
#endif
	seed += id;
	i = id + 1;
	if ( i > 1 ) {
		ULONG pow, accu;
		pow = offset; accu = 1;
		while ( i ) {
			if ( ( i & 1 ) != 0 ) accu *= pow;
			i /= 2; pow = pow*pow;
		}
		offset = accu;
	}
#endif
	if ( seed < ((LONG)1<<(BITSINWORD-1)) ) {
		j = ( (seed+31459L) << (BITSINWORD-2))+offset;
	}
	else if ( seed < ((LONG)1<<(BITSINWORD+10-1)) ) {
		j = ( (seed+31459L) << (BITSINWORD-10-2))+offset;
	}
	else {
		j = ( (seed+31459L) << 1)+offset;
	}
	if ( ( seed & 1 ) == 1 ) seed++;
	j += seed;
	AR.wranfia[imax] = j;
	k = 1;
	for ( i = 0; i <= (ULONG)(imax); i++ ) {
		ii = (AR.wranfnpair1*i)%AR.wranfnpair2;
		AR.wranfia[ii] = k;
		k = ULongToLong((ULONG)j - (ULONG)k);
		if ( k < 0 ) k += (LONG)1 << (2*BITSINWORD-2);
		j = AR.wranfia[ii];
	}
	for ( i = 0; i < WARMUP; i++ ) wranfnew(BHEAD0);
	AR.wranfcall = 0;
}

UWORD wranf(PHEAD0)
{
	UWORD wval;
	if ( AR.wranfia == 0 ) {
		AR.wranfia = (ULONG *)Malloc1(AR.wranfnpair2*sizeof(ULONG),"wranf");
		iniwranf(BHEAD0);
	}
	if ( AR.wranfcall >= AR.wranfnpair2) {
		wranfnew(BHEAD0);
		AR.wranfcall = 0;
	}
	wval = (UWORD)(AR.wranfia[AR.wranfcall++]>>(BITSINWORD-1));
	return(wval);
}

/*
	Returns a random UWORD in the range (0,...,imax-1)
*/

UWORD iranf(PHEAD UWORD imax)
{
	UWORD i;
	ULONG x, xmax;
	if (imax < 2) return 0;
	x = (LONG)1 << BITSINWORD;
	xmax = x - x%imax;
	while ( ( i = wranf(BHEAD0) ) >= xmax ) {}
	return(i%imax);
}

/*
 		#] wranf : 
 		#[ PreRandom :

		The random number generator of the preprocessor.
		This one is completely different from the execution time generator
		random_(number). In the preprocessor we generate a floating point
		number in a string according to a distribution.
		Currently allowed are:
			RANDOM_(log,min,max)
			RANDOM_(lin,min,max)
		The return value is a string with the floating point number.
*/

UBYTE *PreRandom(UBYTE *s)
{
	GETIDENTITY
	UBYTE *mode,*mins = 0,*maxs = 0, *outval;
	float num;
	double minval, maxval, value = 0;
	int linlog = -1;
	mode = s;
	while ( FG.cTable[*s] <= 1 ) s++;
	if ( *s == ',' ) { *s = 0; s++; }
	mins = s;
	while ( *s && *s != ',' ) s++;
	if ( *s == ',' ) { *s = 0; s++; }
	maxs = s;
	while ( *s && *s != ',' ) s++;
	if ( *s || *maxs == 0 || *mins == 0 ) {
		MesPrint("@Illegal arguments in macro RANDOM_");
		Terminate(-1);
	}
	if ( StrICmp(mode,(UBYTE *)"lin") == 0 ) {
		linlog = 0;
	}
	else if ( StrICmp(mode,(UBYTE *)"log") == 0 ) {
		linlog = 1;
	}
	else {
		MesPrint("@Illegal mode argument in macro RANDOM_");
		Terminate(-1);
	}

	sscanf((char *)mins,"%f",&num); minval = num;
	sscanf((char *)maxs,"%f",&num); maxval = num;

	/*
	 * Note on ParFORM: we should use the same random number on all the
	 * processes in the complication phase. The random number is generated
	 * on the master and broadcast to the other processes.
	 */
	{
		UWORD x;
		double xx;
#ifdef WITHMPI
		x = 0;
		if ( PF.me == MASTER ) {
			x = wranf(BHEAD0);
		}
		x = (UWORD)PF_BroadcastNumber((LONG)x);
#else
		x = wranf(BHEAD0);
#endif
		xx = x/pow(2.0,(double)(BITSINWORD-1));
		if ( linlog == 0 ) {
			value = minval + (maxval-minval)*xx;
		}
		else if ( linlog == 1 ) {
			value = minval * pow(maxval/minval,xx);
		}
	}

	outval = (UBYTE *)Malloc1(64,"PreRandom");
	if ( ABS(value) < 0.00001 || ABS(value) > 1000000. ) {
		snprintf((char *)outval,64,"%e",value);
	}
	else if ( ABS(value) < 0.0001 ) { snprintf((char *)outval,64,"%10f",value); }
	else if ( ABS(value) < 0.001 ) { snprintf((char *)outval,64,"%9f",value); }
	else if ( ABS(value) < 0.01 ) { snprintf((char *)outval,64,"%8f",value); }
	else if ( ABS(value) < 0.1 ) { snprintf((char *)outval,64,"%7f",value); }
	else if ( ABS(value) < 1. ) { snprintf((char *)outval,64,"%6f",value); }
	else if ( ABS(value) < 10. ) { snprintf((char *)outval,64,"%5f",value); }
	else if ( ABS(value) < 100. ) { snprintf((char *)outval,64,"%4f",value); }
	else if ( ABS(value) < 1000. ) { snprintf((char *)outval,64,"%3f",value); }
	else if ( ABS(value) < 10000. ) { snprintf((char *)outval,64,"%2f",value); }
	else { snprintf((char *)outval,64,"%1f",value); }
	return(outval);
}

/*
 		#] PreRandom : 
  	#] Functions : 
*/