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

    An header for the GMP library

    See the file COPYING.FPC, included in this distribution,
    for details about the copyright. (LGPL)

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

}

unit gmp;

{$mode objfpc}{$h+}
{$packrecords c}

//todo:windows link error on GMP global vars, reason not yet known
{$ifdef windows}
{$define NO_GMP_GLOBVARS}
{$endif}

{$ifdef darwin}
  {$linklib gmp.3}
{$endif}
{ Unused symbols exported from GMP:

  Marked preliminary in GMP manual
    __gmpn_bdivmod

  Marked obsolete in GMP manual
    __gmpn_divrem
    __gmpz_random
    __gmpz_random2
    __gmp_randinit

  Not documented in GMP manual
    __gmpf_size
    __gmpn_divrem_2
    __gmpn_pow_1
    __gmpn_preinv_mod_1
    __gmpz_millerrabin

  Marked for use only within GDB
    __gmpf_dump
    __gmpz_dump
}

interface

uses
  sysutils;

const
  BASE10 = 10;
  LIB = 'gmp';
  LOG_10_2 = 0.3010299956639812;
  ERROR_NONE = 0;
  ERROR_UNSUPPORTED_ARGUMENT = 1;
  ERROR_DIVISION_BY_ZERO = 2;
  ERROR_SQRT_OF_NEGATIVE = 4;
  ERROR_INVALID_ARGUMENT = 8;
  RAND_ALG_DEFAULT = 0;
  RAND_ALG_LC = RAND_ALG_DEFAULT;

type

  // ---- GMP types ----

  { low level multi precision integer atom = machine size uint }
  mp_limb_t = valuint;
  { ^array of mp_limb_t}
  mpn_ptr = ^mp_limb_t;
  mp_size_t = sizeint;
  mp_exp_t = valsint;
  randalg_t = longint;

  { multi precision integer number record }
  mpz_t = record
    alloc: longint;
    size: longint;
    data: mpn_ptr;
  end;
  mpz_ptr = ^mpz_t;

  { multi precision rational number record }
  mpq_t = record
    num: mpz_t;
    den: mpz_t;
  end;
  mpq_ptr = ^mpq_t;

  { multi precision real number record }
  mpf_t = record
    prec: longint;
    size: longint;
    exp: mp_exp_t;
    data: mpn_ptr;
  end;
  mpf_ptr = ^mpf_t;

  randstate_t = record
    seed: mpz_t;
    alg: randalg_t;
    algdata: record
      case longint of
        0 : (lc : pointer);
    end;
  end;
  randstate_ptr = ^randstate_t;

  { Return a pointer to newly allocated space with at least alloc size bytes }
  alloc_func_t = function(alloc_size: sizeuint): pointer; cdecl;
  { Resize a previously allocated block ptr of old size bytes to be new size bytes }
  reallocate_func_t = function(p: pointer; old_size, new_size: sizeuint): pointer; cdecl;
  { De-allocate the space pointed to by ptr }
  free_proc_t = procedure(p: pointer; size: sizeuint); cdecl;
  palloc_func = ^alloc_func_t;
  preallocate_func = ^reallocate_func_t;
  pfree_proc = ^free_proc_t;

  // ---- ext types with automatic mem mngmt & cow, ~ fpc string type style -----

  IMPBase = interface
    function refs: longint;
  end;

  MPInteger = interface(IMPBase)
    function ptr: mpz_ptr;
  end;

  MPFloat = interface(IMPBase)
    function ptr: mpf_ptr;
  end;

  MPRational = interface(IMPBase)
    function ptr: mpq_ptr;
  end;

  MPRandState = interface(IMPBase)
    function ptr: randstate_ptr;
  end;

  { TMPBase }

  TMPBase = class(TInterfacedObject, IMPBase)
  private
    function refs: longint;  inline;
  end;

  { TMPInteger }

  TMPInteger = class(TMPBase, MPInteger)
  private
    fmpz: mpz_t;
    function ptr: mpz_ptr;  inline;
  public
    destructor destroy; override;
  end;

  { TMPFloat }

  TMPFloat = class(TMPBase, MPFloat)
  private
    fmpf: mpf_t;
    function ptr: mpf_ptr;  inline;
  public
    destructor destroy; override;
  end;

  { TMPRational }

  TMPRational = class(TMPBase, MPRational)
  private
    fmpq: mpq_t;
    function ptr: mpq_ptr;  inline;
  public
    destructor destroy; override;
  end;

  { TMPRandState }

  TMPRandState = class(TMPBase, MPRandState)
  private
    frandstate: randstate_t;
    function ptr: randstate_ptr;  inline;
  public
    destructor destroy; override;
  end;

// ==== GMP bindings ====

// ---- Custom Allocation ----

{ Replace the current allocation functions from the arguments }
procedure mp_set_memory_functions(alloc_func_ptr: alloc_func_t; realloc_func_ptr: reallocate_func_t; free_func_ptr: free_proc_t); cdecl; external LIB name '__gmp_set_memory_functions';
{ Get the current allocation functions, storing function pointers to the locations given by the arguments }
procedure mp_get_memory_functions(alloc_func_ptr: palloc_func; realloc_func_ptr: preallocate_func; free_func_ptr: pfree_proc); cdecl; external LIB name '__gmp_get_memory_functions';

// ---- Random Number Functions ----

{ Obsolete: Initialize state with an algorithm selected by alg }
// procedure randinit(var state: randstate_t; alg: randalg_t; args: array of const); cdecl; external LIB name '__gmp_randinit';

{ Initialize state with a default algorithm }
procedure mp_randinit_default(out state: randstate_t); cdecl; external LIB name '__gmp_randinit_default';
{ Initialize state with a linear congruential algorithm X = (aX + c) mod 2^m2exp }
procedure mp_randinit_lc_2exp(out state: randstate_t; var a: mpz_t; c, m2exp: valuint); cdecl; external LIB name '__gmp_randinit_lc_2exp';
{ Initialize state for a linear congruential algorithm as per gmp_randinit_lc_2exp }
function mp_randinit_lc_2exp_size(out state: randstate_t; size: sizeuint): longint; cdecl; external LIB name '__gmp_randinit_lc_2exp_size';
{ Initialize state for a Mersenne Twister algorithm }
procedure mp_randinit_mt(out state: randstate_t); cdecl; external LIB name '__gmp_randinit_mt';
{ Initialize rop with a copy of the algorithm and state from op }
procedure mp_randinit_set(out rop: randstate_t; var op: randstate_t); cdecl; external LIB name '__gmp_randinit_set';
{ Set an initial seed value into state }
procedure mp_randseed(var state: randstate_t; var seed: mpz_t); cdecl; external LIB name '__gmp_randseed';
{ Set an initial seed value into state }
procedure mp_randseed_ui(var state: randstate_t; seed: valuint); cdecl; external LIB name '__gmp_randseed_ui';
{ Free all memory occupied by state }
procedure mp_randclear(var state: randstate_t); cdecl; external LIB name '__gmp_randclear';
{ Return an uniformly distributed random number of n bits, ie. in the range 0 to 2^n−1 inclusive }
function mp_urandomb_ui(var state: randstate_t; n: valuint): valuint; cdecl; external LIB name '__gmp_urandomb_ui';
{ Return an uniformly distributed random number in the range 0 to n − 1, inclusive }
function mp_urandomm_ui(var state: randstate_t; n: valuint): valuint; cdecl; external LIB name '__gmp_urandomm_ui';

// ---- Formatted Input/Output ----

{ Form a null-terminated string in a block of memory obtained from the current memory allocation function }
function mp_asprintf(out pp: pchar; fmt: pchar; args: array of const): longint; cdecl; external LIB name '__gmp_asprintf';
{ Form a null-terminated string in a block of memory obtained from the current memory allocation function }
function mp_asprintf(out pp: pchar; fmt: pchar): longint; cdecl; varargs; external LIB name '__gmp_asprintf';
{ Print to the standard output stdout. Return the number of characters written, or −1 if an error occurred. }
function mp_printf(fmt: pchar; args: array of const): longint; cdecl; external LIB name '__gmp_printf';
{ Print to the standard output stdout. Return the number of characters written, or −1 if an error occurred. }
function mp_printf(fmt: pchar): longint; cdecl; varargs; external LIB name '__gmp_printf';
{ Form a null-terminated string in buf. No more than size bytes will be written. }
function mp_snprintf(buf: pchar; size: sizeuint; fmt: pchar; args: array of const): longint; cdecl; external LIB name '__gmp_snprintf';
{ Form a null-terminated string in buf. No more than size bytes will be written. }
function mp_snprintf(buf: pchar; size: sizeuint; fmt: pchar): longint; cdecl; varargs; external LIB name '__gmp_snprintf';
{ Form a null-terminated string in buf. Return the number of characters written, excluding the terminating null. }
function mp_sprintf(buf, fmt: pchar; args: array of const): longint; cdecl; external LIB name '__gmp_sprintf';
{ Form a null-terminated string in buf. Return the number of characters written, excluding the terminating null. }
function mp_sprintf(buf, fmt: pchar): longint; cdecl; varargs; external LIB name '__gmp_sprintf';
{ Read from the standard input stdin }
function mp_scanf(fmt: pchar; args: array of const): longint; cdecl; external LIB name '__gmp_scanf';
{ Read from the standard input stdin }
function mp_scanf(fmt: pchar): longint; cdecl; varargs; external LIB name '__gmp_scanf';
{ Read from a null-terminated string s }
function mp_sscanf(s, fmt: pchar; args: array of const): longint; cdecl; external LIB name '__gmp_sscanf';
{ Read from a null-terminated string s }
function mp_sscanf(s, fmt: pchar): longint; cdecl; varargs; external LIB name '__gmp_sscanf';

// ---- integer Functions ----

{ Change the space for integer to new_alloc limbs }
function mpz_realloc(var integer_: mpz_t; new_alloc: mp_size_t): pointer; cdecl; external LIB name '__gmpz_realloc';
{ Set rop to the absolute value of op }
procedure mpz_abs(var rop, op: mpz_t); cdecl; external LIB name '__gmpz_abs';
{ Set rop to op1 + op2 }
procedure mpz_add(var rop, op1, op2: mpz_t); cdecl; external LIB name '__gmpz_add';
{ Set rop to op1 + op2 }
procedure mpz_add_ui(var rop, op1: mpz_t; op2: valuint); cdecl; external LIB name '__gmpz_add_ui';
{ Set rop to rop + op1 × op2 }
procedure mpz_addmul(var rop, op1, op2: mpz_t); cdecl; external LIB name '__gmpz_addmul';
{ Set rop to rop + op1 × op2 }
procedure mpz_addmul_ui(var rop, op1: mpz_t; op2: valuint); cdecl; external LIB name '__gmpz_addmul_ui';
{ Set rop to op1 bitwise-and op2 }
procedure mpz_and(var rop, op1, op2: mpz_t); cdecl; external LIB name '__gmpz_and';
{ _Fixed_ space of fixed_num_bits is allocated to each of the array size integers in integer array }
procedure mpz_array_init(var integer_array: mpz_t; array_size, fixed_num_bits: mp_size_t); cdecl; external LIB name '__gmpz_array_init';
{ Compute the binomial coefficient (n over k) and store the result in rop }
procedure mpz_bin_ui(var rop, n: mpz_t; k: valuint); cdecl; external LIB name '__gmpz_bin_ui';
{ Compute the binomial coefficient (n over k) and store the result in rop }
procedure mpz_bin_uiui(var rop: mpz_t; n, k: valuint); cdecl; external LIB name '__gmpz_bin_uiui';
{ Divide n by d, forming a quotient q. Round mode ceil. }
procedure mpz_cdiv_q(var q, n, d: mpz_t); cdecl; external LIB name '__gmpz_cdiv_q';
{ Divide n by d, forming a quotient q. d = 2^b. Round mode ceil. }
procedure mpz_cdiv_q_2exp(var q, n: mpz_t; b: valuint); cdecl; external LIB name '__gmpz_cdiv_q_2exp';
{ Divide n by d, forming a quotient q. Round mode ceil. }
function mpz_cdiv_q_ui(var q, n: mpz_t; d: valuint): valuint; cdecl; external LIB name '__gmpz_cdiv_q_ui';
{ Divide n by d, forming a quotient q and remainder r. Round mode ceil. }
procedure mpz_cdiv_qr(var q, r, n, d: mpz_t); cdecl; external LIB name '__gmpz_cdiv_qr';
{ Divide n by d, forming a quotient q and remainder r. Round mode ceil. }
function mpz_cdiv_qr_ui(var q, r, n: mpz_t; d: valuint): valuint; cdecl; external LIB name '__gmpz_cdiv_qr_ui';
{ Divide n by d, forming a remainder r. Round mode ceil. }
procedure mpz_cdiv_r(var r, n, d: mpz_t); cdecl; external LIB name '__gmpz_cdiv_r';
{ Divide n by d, forming a remainder r. d = 2^b. Round mode ceil. }
procedure mpz_cdiv_r_2exp(var r, n: mpz_t; b: valuint); cdecl; external LIB name '__gmpz_cdiv_r_2exp';
{ Divide n by d, forming a remainder r. Round mode ceil. }
function mpz_cdiv_r_ui(var r, n: mpz_t; d: valuint): valuint; cdecl; external LIB name '__gmpz_cdiv_r_ui';
{ Divide n by d. Round mode ceil. }
function mpz_cdiv_ui(var n: mpz_t; d: valuint): valuint; cdecl; external LIB name '__gmpz_cdiv_ui';
{ Free the space occupied by integer. Call this function for all mpz_t variables when you are done with them. }
procedure mpz_clear(var integer_: mpz_t); cdecl; external LIB name '__gmpz_clear';
{ Clear bit bit_index in rop }
procedure mpz_clrbit(var rop: mpz_t; bit_index: valuint); cdecl; external LIB name '__gmpz_clrbit';
{ Compare op1 and op2. Return a positive value if op1 > op2, zero if op1 = op2, or a negative value if op1 < op2. }
function mpz_cmp(var op1, op2: mpz_t): longint; cdecl; external LIB name '__gmpz_cmp';
{ Compare op1 and op2. Return a positive value if op1 > op2, zero if op1 = op2, or a negative value if op1 < op2. }
function mpz_cmp_d(var op1: mpz_t; op2: double): longint; cdecl; external LIB name '__gmpz_cmp_d';
{ Compare op1 and op2. Return a positive value if op1 > op2, zero if op1 = op2, or a negative value if op1 < op2. }
function mpz_cmp_si(var op1: mpz_t; op2: valsint): longint; cdecl; external LIB name '__gmpz_cmp_si';
{ Compare op1 and op2. Return a positive value if op1 > op2, zero if op1 = op2, or a negative value if op1 < op2. }
function mpz_cmp_ui(var op1: mpz_t; op2: valuint): longint; cdecl; external LIB name '__gmpz_cmp_ui';
{ Compare the absolute values of op1 and op2. Return a positive value if |op1| > |op2|, zero if |op1| = |op2|, or a negative value if |op1| < |op2|. }
function mpz_cmpabs(var op1, op2: mpz_t): longint; cdecl; external LIB name '__gmpz_cmpabs';
{ Compare the absolute values of op1 and op2. Return a positive value if |op1| > |op2|, zero if |op1| = |op2|, or a negative value if |op1| < |op2|. }
function mpz_cmpabs_d(var op1: mpz_t; op2: double): longint; cdecl; external LIB name '__gmpz_cmpabs_d';
{ Compare the absolute values of op1 and op2. Return a positive value if |op1| > |op2|, zero if |op1| = |op2|, or a negative value if |op1| < |op2|. }
function mpz_cmpabs_ui(var op1: mpz_t; op2: valuint): longint; cdecl; external LIB name '__gmpz_cmpabs_ui';
{ Set rop to the one’s complement of op }
procedure mpz_com(var rop, op: mpz_t); cdecl; external LIB name '__gmpz_com';
{ Complement bit bit_index in rop }
procedure mpz_combit(var rop: mpz_t; bit_index: valuint); cdecl; external LIB name '__gmpz_combit';
{ Return non-zero if n is congruent to c modulo d }
function mpz_congruent_p(var n, c, d: mpz_t): longint; cdecl; external LIB name '__gmpz_congruent_p';
{ Return non-zero if n is congruent to c modulo 2^b }
function mpz_congruent_2exp_p(var n, c: mpz_t; b: valuint): longint; cdecl; external LIB name '__gmpz_congruent_2exp_p';
{ Return non-zero if n is congruent to c modulo d }
function mpz_congruent_ui_p(var n: mpz_t; c, d: valuint): longint; cdecl; external LIB name '__gmpz_congruent_ui_p';
{ Set q to n/d }
procedure mpz_divexact(var q, n, d: mpz_t); cdecl; external LIB name '__gmpz_divexact';
{ Set q to n/d }
procedure mpz_divexact_ui(var q, n: mpz_t; d: valuint); cdecl; external LIB name '__gmpz_divexact_ui';
{ Return non-zero if n is exactly divisible by d }
function mpz_divisible_p(var n, d: mpz_t): longint; cdecl; external LIB name '__gmpz_divisible_p';
{ Return non-zero if n is exactly divisible by d }
function mpz_divisible_ui_p(var n: mpz_t; d: valuint): longint; cdecl; external LIB name '__gmpz_divisible_ui_p';
{ Return non-zero if n is exactly divisible by by 2^b }
function mpz_divisible_2exp_p(var n: mpz_t; b: valuint): longint; cdecl; external LIB name '__gmpz_divisible_2exp_p';

// GDB only: procedure mpz_dump(var _para1: mpz_t); cdecl; external LIB name '__gmpz_dump';

{ Fill buf with word data from op }
function mpz_export(out buf; out countp: sizeuint; order: longint; size: sizeuint; endian: longint; nails: sizeuint; var op: mpz_t): pointer; cdecl; external LIB name '__gmpz_export';
{ Set rop to op!, the factorial of op }
procedure mpz_fac_ui(var rop: mpz_t; op: valuint); cdecl; external LIB name '__gmpz_fac_ui';
{ Divide n by d, forming a quotient q. Round mode floor. }
procedure mpz_fdiv_q(var q, n, d: mpz_t); cdecl; external LIB name '__gmpz_fdiv_q';
{ Divide n by d, forming a quotient q. d = 2^b. Round mode floor. }
procedure mpz_fdiv_q_2exp(var q, n: mpz_t; b: valuint); cdecl; external LIB name '__gmpz_fdiv_q_2exp';
{ Divide n by d, forming a quotient q. Round mode floor. }
function mpz_fdiv_q_ui(var q, n: mpz_t; d: valuint): valuint; cdecl; external LIB name '__gmpz_fdiv_q_ui';
{ Divide n by d, forming a quotient q and remainder r. Round mode floor. }
procedure mpz_fdiv_qr(var q, r, n, d: mpz_t); cdecl; external LIB name '__gmpz_fdiv_qr';
{ Divide n by d, forming a quotient q and remainder r. Round mode floor. }
function mpz_fdiv_qr_ui(var q, r, n: mpz_t; d: valuint): valuint; cdecl; external LIB name '__gmpz_fdiv_qr_ui';
{ Divide n by d, forming a remainder r. Round mode floor. }
procedure mpz_fdiv_r(var r, n, d: mpz_t); cdecl; external LIB name '__gmpz_fdiv_r';
{ Divide n by d, forming a remainder r. d = 2^b. Round mode floor. }
procedure mpz_fdiv_r_2exp(var r, n: mpz_t; b: valuint); cdecl; external LIB name '__gmpz_fdiv_r_2exp';
{ Divide n by d, forming a remainder r. Round mode floor. }
function mpz_fdiv_r_ui(var r, n: mpz_t; d: valuint): valuint; cdecl; external LIB name '__gmpz_fdiv_r_ui';
{ Divide n by d. Round mode floor. }
function mpz_fdiv_ui(var n: mpz_t; d: valuint): valuint; cdecl; external LIB name '__gmpz_fdiv_ui';
{ Set fn to to Fn, the n’th Fibonacci number }
procedure mpz_fib_ui(var fn: mpz_t; n: valuint); cdecl; external LIB name '__gmpz_fib_ui';
{ Set fn to Fn, and fnsub1 to Fn−1 }
procedure mpz_fib2_ui(var fn, fnsub1: mpz_t; n: valuint); cdecl; external LIB name '__gmpz_fib2_ui';
{ Return non-zero iff the value of op fits in an signed int. Otherwise, return zero. }
function mpz_fits_sint_p(var op: mpz_t): longint; cdecl; external LIB name '__gmpz_fits_sint_p';
{ Return non-zero iff the value of op fits in an signed long int. Otherwise, return zero. }
function mpz_fits_slong_p(var op: mpz_t): longint; cdecl; external LIB name '__gmpz_fits_slong_p';
{ Return non-zero iff the value of op fits in an signed short int. Otherwise, return zero. }
function mpz_fits_sshort_p(var op: mpz_t): longint; cdecl; external LIB name '__gmpz_fits_sshort_p';
{ Return non-zero iff the value of op fits in an unsigned int. Otherwise, return zero. }
function mpz_fits_uint_p(var op: mpz_t): longint; cdecl; external LIB name '__gmpz_fits_uint_p';
{ Return non-zero iff the value of op fits in an unsigned long int. Otherwise, return zero. }
function mpz_fits_ulong_p(var op: mpz_t): longint; cdecl; external LIB name '__gmpz_fits_ulong_p';
{ Return non-zero iff the value of op fits in an unsigned short int. Otherwise, return zero. }
function mpz_fits_ushort_p(var op: mpz_t): longint; cdecl; external LIB name '__gmpz_fits_ushort_p';
{ Set rop to the greatest common divisor of op1 and op2 }
procedure mpz_gcd(var rop, op1, op2: mpz_t); cdecl; external LIB name '__gmpz_gcd';
{ Compute the greatest common divisor of op1 and op2. If rop is not NULL, store the result there. }
function mpz_gcd_ui(var rop, op1: mpz_t; op2: valuint): valuint; cdecl; external LIB name '__gmpz_gcd_ui';
{ Set g to the greatest common divisor of a and b, and in addition set s and t to coefficients satisfying as + bt = g }
procedure mpz_gcdext(var g, s, t, a, b: mpz_t); cdecl; external LIB name '__gmpz_gcdext';
{ Convert op to a double, truncating if necessary (ie. rounding towards zero) }
function mpz_get_d(var op: mpz_t): double; cdecl; external LIB name '__gmpz_get_d';
{ Convert op to a double, truncating if necessary (ie. rounding towards zero), and returning the exponent separately }
function mpz_get_d_2exp(out exp: valsint; var op: mpz_t): double; cdecl; external LIB name '__gmpz_get_d_2exp';
{ Return the value of op as a signed long }
function mpz_get_si(var op: mpz_t): valsint; cdecl; external LIB name '__gmpz_get_si';
{ Convert op to a string of digits in base base. The base argument may vary from 2 to 62 or from −2 to −36. If str is NULL, the result string is allocated using the current allocation function }
function mpz_get_str(str: pchar; base: longint; var op: mpz_t): pchar; cdecl; external LIB name '__gmpz_get_str';
{ Return the value of op as an unsigned long }
function mpz_get_ui(var op: mpz_t): valuint; cdecl; external LIB name '__gmpz_get_ui';
{ Return limb number n from op }
function mpz_getlimbn(var op: mpz_t; n: mp_size_t): mp_limb_t; cdecl; external LIB name '__gmpz_getlimbn';
{ If op1 and op2 are both >= 0 or both < 0, return the hamming distance between the two operands, which is the number of bit positions where op1 and op2 have different bit values }
function mpz_hamdist(var op1, op2: mpz_t): valuint; cdecl; external LIB name '__gmpz_hamdist';
{ Set rop from an array of word data at op }
procedure mpz_import(var rop: mpz_t; count: sizeuint; order: longint; size: sizeuint; endian: longint; nails: sizeuint; const op); cdecl; external LIB name '__gmpz_import';
{ Initialize integer, and set its value to 0 }
procedure mpz_init(out integer_: mpz_t); cdecl; external LIB name '__gmpz_init';
{ Initialize integer, with space for n bits, and set its value to 0 }
procedure mpz_init2(out integer_: mpz_t; n: valuint); cdecl; external LIB name '__gmpz_init2';
{ Initialize rop with limb space and set the initial numeric value from op }
procedure mpz_init_set(out rop: mpz_t; var op: mpz_t); cdecl; external LIB name '__gmpz_init_set';
{ Initialize rop with limb space and set the initial numeric value from op }
procedure mpz_init_set_d(out rop: mpz_t; op: double); cdecl; external LIB name '__gmpz_init_set_d';
{ Initialize rop with limb space and set the initial numeric value from op }
procedure mpz_init_set_si(out rop: mpz_t; op: valsint); cdecl; external LIB name '__gmpz_init_set_si';
{ Initialize rop and set its value like mpz_set_str }
function mpz_init_set_str(out rop: mpz_t; str: pchar; base: longint): longint; cdecl; external LIB name '__gmpz_init_set_str';
{ Initialize rop with limb space and set the initial numeric value from op }
procedure mpz_init_set_ui(out rop: mpz_t; op: valuint); cdecl; external LIB name '__gmpz_init_set_ui';
{ Compute the inverse of op1 modulo op2 and put the result in rop }
function mpz_invert(var rop, op1, op2: mpz_t): longint; cdecl; external LIB name '__gmpz_invert';
{ Set rop to op1 bitwise inclusive-or op2 }
procedure mpz_ior(var rop, op1, op2: mpz_t); cdecl; external LIB name '__gmpz_ior';
{ Calculate the Jacobi symbol. This is defined only for b odd }
function mpz_jacobi(var a, b: mpz_t): longint; cdecl; external LIB name '__gmpz_jacobi';
{ Calculate the Jacobi symbol with the Kronecker extension }
function mpz_kronecker_si(var a: mpz_t; b: valsint): longint; cdecl; external LIB name '__gmpz_kronecker_si';
{ Calculate the Jacobi symbol with the Kronecker extension }
function mpz_kronecker_ui(var a: mpz_t; b: valuint): longint; cdecl; external LIB name '__gmpz_kronecker_ui';
{ Calculate the Jacobi symbol with the Kronecker extension }
function mpz_si_kronecker(a: valsint; var b: mpz_t): longint; cdecl; external LIB name '__gmpz_si_kronecker';
{ Calculate the Jacobi symbol with the Kronecker extension }
function mpz_ui_kronecker(a: valuint; var b: mpz_t): longint; cdecl; external LIB name '__gmpz_ui_kronecker';
{ Set rop to the least common multiple of op1 and op2 }
procedure mpz_lcm(var rop, op1, op2: mpz_t); cdecl; external LIB name '__gmpz_lcm';
{ Set rop to the least common multiple of op1 and op2 }
procedure mpz_lcm_ui(var rop, op1: mpz_t; op2: valuint); cdecl; external LIB name '__gmpz_lcm_ui';
{ Set ln to to Ln, the n’th Lucas number }
procedure mpz_lucnum_ui(var ln: mpz_t; n: valuint); cdecl; external LIB name '__gmpz_lucnum_ui';
{ Set ln to Ln, and lnsub1 to Ln−1 }
procedure mpz_lucnum2_ui(var ln, lnsub1: mpz_t; n: valuint); cdecl; external LIB name '__gmpz_lucnum2_ui';

// No docs: function mpz_millerrabin(var _para1: mpz_t; _para2: longint): longint; cdecl; external LIB name '__gmpz_millerrabin';

{ Set r to n mod d. The sign of the divisor is ignored; the result is always non-negative. }
procedure mpz_mod(var r, n, d: mpz_t); cdecl; external LIB name '__gmpz_mod';
{ Set rop to op1 × op2 }
procedure mpz_mul(var rop, op1, op2: mpz_t); cdecl; external LIB name '__gmpz_mul';
{ Set rop to op1 × 2^op2. This operation can also be defined as a left shift by op2 bits. }
procedure mpz_mul_2exp(var rop, op1: mpz_t; op2: valuint); cdecl; external LIB name '__gmpz_mul_2exp';
{ Set rop to op1 × op2 }
procedure mpz_mul_si(var rop, op1: mpz_t; op2: valsint); cdecl; external LIB name '__gmpz_mul_si';
{ Set rop to op1 × op2 }
procedure mpz_mul_ui(var rop, op1: mpz_t; op2: valuint); cdecl; external LIB name '__gmpz_mul_ui';
{ Set rop to −op }
procedure mpz_neg(var rop, op: mpz_t); cdecl; external LIB name '__gmpz_neg';
{ Set rop to the next prime greater than op }
procedure mpz_nextprime(var rop, op: mpz_t); cdecl; external LIB name '__gmpz_nextprime';
{ Return non-zero if op is a perfect power, i.e., if there exist integers a and b, with b > 1, such that op = a^b }
function mpz_perfect_power_p(var op: mpz_t): longint; cdecl; external LIB name '__gmpz_perfect_power_p';
{ Return non-zero if op is a perfect square, i.e., if the square root of op is an integer }
function mpz_perfect_square_p(var op: mpz_t): longint; cdecl; external LIB name '__gmpz_perfect_square_p';
{ If op >= 0, return the population count of op, which is the number of 1 bits in the binary representation }
function mpz_popcount(var op: mpz_t): valuint; cdecl; external LIB name '__gmpz_popcount';
{ Set rop to base^exp. The case 0^0 yields 1. }
procedure mpz_pow_ui(var rop, base: mpz_t; exp: valuint); cdecl; external LIB name '__gmpz_pow_ui';
{ Set rop to base^exp mod mod_ }
procedure mpz_powm(var rop, base, exp, mod_: mpz_t); cdecl; external LIB name '__gmpz_powm';
{ Set rop to base^exp mod mod_ }
procedure mpz_powm_ui(var rop, base: mpz_t; exp: valuint; var mod_: mpz_t); cdecl; external LIB name '__gmpz_powm_ui';
{ Determine whether n is prime. Return 2 if n is definitely prime, return 1 if n is probably prime (without being certain), or return 0 if n is definitely composite. }
function mpz_probab_prime_p(var n: mpz_t; reps: longint): longint; cdecl; external LIB name '__gmpz_probab_prime_p';

{ Obsolete: Generate a random integer of at most max_size limbs }
// procedure mpz_random(var rop: mpz_t; max_size: mp_size_t); cdecl; external LIB name '__gmpz_random';
{ Obsolete: Generate a random integer of at most max_size limbs, with long strings of zeros and ones in the binary representation }
// procedure mpz_random2(var rop: mpz_t; max_size: mp_size_t); cdecl; external LIB name '__gmpz_random2';

{ Change the space allocated for integer to n bits. The value in integer is preserved if it fits, or is set to 0 if not. }
procedure mpz_realloc2(var integer_: mpz_t; n: valuint); cdecl; external LIB name '__gmpz_realloc2';
{ Remove all occurrences of the factor f from op and store the result in rop }
function mpz_remove(var rop, op, f: mpz_t): valuint; cdecl; external LIB name '__gmpz_remove';
{ Set rop to trunc(op^(1/n)), the truncated integer part of the nth root of op. Return non-zero if the computation was exact, i.e., if op is rop to the nth power. }
function mpz_root(var rop, op: mpz_t; n: valuint): longint; cdecl; external LIB name '__gmpz_root';
{ Set root to trunc(u^(1/n)), the truncated integer part of the nth root of u. Set rem to the remainder, (u − root^n). }
procedure mpz_rootrem(var root, rem, u: mpz_t; n: valuint); cdecl; external LIB name '__gmpz_rootrem';
{ Generate a random integer with long strings of zeros and ones in the binary representation }
procedure mpz_rrandomb(var rop: mpz_t; var state: randstate_t; n: valuint); cdecl; external LIB name '__gmpz_rrandomb';
{ Scan op, starting from bit starting_bit, towards more significant bits, until the first 0 bit is found }
function mpz_scan0(var op: mpz_t; starting_bit: valuint): valuint; cdecl; external LIB name '__gmpz_scan0';
{ Scan op, starting from bit starting_bit, towards more significant bits, until the first 1 bit is found }
function mpz_scan1(var op: mpz_t; starting_bit: valuint): valuint; cdecl; external LIB name '__gmpz_scan1';
{ Set the value of rop from op }
procedure mpz_set(var rop, op: mpz_t); cdecl; external LIB name '__gmpz_set';
{ Set the value of rop from op }
procedure mpz_set_d(var rop: mpz_t; op: double); cdecl; external LIB name '__gmpz_set_d';
{ Set the value of rop from op }
procedure mpz_set_f(var rop: mpz_t; var op: mpf_t); cdecl; external LIB name '__gmpz_set_f';
{ Set the value of rop from op }
procedure mpz_set_q(var rop: mpz_t; var op: mpq_t); cdecl; external LIB name '__gmpz_set_q';
{ Set the value of rop from op }
procedure mpz_set_si(var rop: mpz_t; op: valsint); cdecl; external LIB name '__gmpz_set_si';
{ Set the value of rop from str, a null-terminated C string in base base. White space is allowed in the string, and is simply ignored. }
function mpz_set_str(var rop: mpz_t; str: pchar; base: longint): longint; cdecl; external LIB name '__gmpz_set_str';
{ Set the value of rop from op }
procedure mpz_set_ui(var rop: mpz_t; op: valuint); cdecl; external LIB name '__gmpz_set_ui';
{ Set bit bit_index in rop }
procedure mpz_setbit(var rop: mpz_t; bit_index: valuint); cdecl; external LIB name '__gmpz_setbit';
{ Return the size of op measured in number of limbs }
function mpz_size(var op: mpz_t): sizeuint; cdecl; external LIB name '__gmpz_size';
{ Return the size of op measured in number of digits in the given base }
function mpz_sizeinbase(var op: mpz_t; base: longint): sizeuint; cdecl; external LIB name '__gmpz_sizeinbase';
{ Set rop to trunc(sqrt(op)), the truncated integer part of the square root of op }
procedure mpz_sqrt(var rop, op: mpz_t); cdecl; external LIB name '__gmpz_sqrt';
{ Set rop1 to trunc(sqrt(op)), likempz_sqrt. Set rop2 to the remainder (op − rop1^2), which will be zero if op is a perfect square. }
procedure mpz_sqrtrem(var rop1, rop2, op: mpz_t); cdecl; external LIB name '__gmpz_sqrtrem';
{ Set rop to op1 − op2 }
procedure mpz_sub(var rop, op1, op2: mpz_t); cdecl; external LIB name '__gmpz_sub';
{ Set rop to op1 − op2 }
procedure mpz_sub_ui(var rop, op1: mpz_t; op2: valuint); cdecl; external LIB name '__gmpz_sub_ui';
{ Set rop to op1 − op2 }
procedure mpz_ui_sub(var rop: mpz_t; op1: valuint; var op2: mpz_t); cdecl; external LIB name '__gmpz_ui_sub';
{ Set rop to rop − op1 × op2 }
procedure mpz_submul(var rop, op1, op2: mpz_t); cdecl; external LIB name '__gmpz_submul';
{ Set rop to rop − op1 × op2 }
procedure mpz_submul_ui(var rop, op1: mpz_t; op2: valuint); cdecl; external LIB name '__gmpz_submul_ui';
{ Swap the values rop1 and rop2 efficiently }
procedure mpz_swap(var rop1, rop2: mpz_t); cdecl; external LIB name '__gmpz_swap';
{ Divide n by d, forming a quotient q. Round mode trunc. }
procedure mpz_tdiv_q(var q, n, d: mpz_t); cdecl; external LIB name '__gmpz_tdiv_q';
{ Divide n by d, forming a quotient q. d = 2^b. Round mode trunc. }
procedure mpz_tdiv_q_2exp(var q, n: mpz_t; b: valuint); cdecl; external LIB name '__gmpz_tdiv_q_2exp';
{ Divide n by d, forming a quotient q. Round mode trunc. }
function mpz_tdiv_q_ui(var q, n: mpz_t; d: valuint): valuint; cdecl; external LIB name '__gmpz_tdiv_q_ui';
{ Divide n by d, forming a quotient q and remainder r. Round mode trunc. }
procedure mpz_tdiv_qr(var q, r, n, d: mpz_t); cdecl; external LIB name '__gmpz_tdiv_qr';
{ Divide n by d, forming a quotient q and remainder r. Round mode trunc. }
function mpz_tdiv_qr_ui(var q, r, n: mpz_t; d: valuint): valuint; cdecl; external LIB name '__gmpz_tdiv_qr_ui';
{ Divide n by d, forming a remainder r. Round mode trunc. }
procedure mpz_tdiv_r(var r, n, d: mpz_t); cdecl; external LIB name '__gmpz_tdiv_r';
{ Divide n by d, forming a remainder r. d = 2^b. Round mode trunc. }
procedure mpz_tdiv_r_2exp(var r, n: mpz_t; b: valuint); cdecl; external LIB name '__gmpz_tdiv_r_2exp';
{ Divide n by d, forming a remainder r. Round mode trunc. }
function mpz_tdiv_r_ui(var r, n: mpz_t; d: valuint): valuint; cdecl; external LIB name '__gmpz_tdiv_r_ui';
{ Divide n by d. Round mode trunc. }
function mpz_tdiv_ui(var n: mpz_t; d: valuint): valuint; cdecl; external LIB name '__gmpz_tdiv_ui';
{ Test bit bit_index in op and return 0 or 1 accordingly }
function mpz_tstbit(var rop: mpz_t; bit_index: valuint): longint; cdecl; external LIB name '__gmpz_tstbit';
{ Set rop to base^exp. The case 0^0 yields 1 }
procedure mpz_ui_pow_ui(var rop: mpz_t; base, exp: valuint); cdecl; external LIB name '__gmpz_ui_pow_ui';
{ Generate a uniformly distributed random integer in the range 0 to 2^n − 1, inclusive }
procedure mpz_urandomb(var rop: mpz_t; var state: randstate_t; n: valuint); cdecl; external LIB name '__gmpz_urandomb';
{ Generate a uniform random integer in the range 0 to n − 1, inclusive }
procedure mpz_urandomm(var rop: mpz_t; var state: randstate_t; var n: mpz_t); cdecl; external LIB name '__gmpz_urandomm';
{ Set rop to op1 bitwise exclusive-or op2 }
procedure mpz_xor(var rop, op1, op2: mpz_t); cdecl; external LIB name '__gmpz_xor';

// ---- Rational Number Functions ----

{ Set rop to the absolute value of op }
procedure mpq_abs(var rop, op: mpq_t); cdecl; external LIB name '__gmpq_abs';
{ Set sum to addend1 + addend2 }
procedure mpq_add(var sum, addend1, addend2: mpq_t); cdecl; external LIB name '__gmpq_add';
{ Remove any factors that are common to the numerator and denominator of op, and make the denominator positive }
procedure mpq_canonicalize(var op: mpq_t); cdecl; external LIB name '__gmpq_canonicalize';
{ Free the space occupied by rational number }
procedure mpq_clear(var rational_number: mpq_t); cdecl; external LIB name '__gmpq_clear';
{ Compare op1 and op2. Return a positive value if op1 > op2, zero if op1 = op2, and a negative value if op1 < op2 }
function mpq_cmp(var op1, op2: mpq_t): longint; cdecl; external LIB name '__gmpq_cmp';
{ Compare op1 and num2/den2. Return a positive value if op1 > num2/den2, zero if op1 = num2/den2, and a negative value if op1 < num2/den2 }
function mpq_cmp_si(var op1: mpq_t; num2: valsint; den2: valuint): longint; cdecl; external LIB name '__gmpq_cmp_si';
{ Compare op1 and num2/den2. Return a positive value if op1 > num2/den2, zero if op1 = num2/den2, and a negative value if op1 < num2/den2 }
function mpq_cmp_ui(var op1: mpq_t; num2, den2: valuint): longint; cdecl; external LIB name '__gmpq_cmp_ui';
{ Set quotient to dividend/divisor }
procedure mpq_div(var quotient, dividend, divisor: mpq_t); cdecl; external LIB name '__gmpq_div';
{ Set rop to op1/(2^op2) }
procedure mpq_div_2exp(var rop, op1: mpq_t; op2: valuint); cdecl; external LIB name '__gmpq_div_2exp';
{ Return non-zero if op1 and op2 are equal, zero if they are non-equal }
function mpq_equal(var op1, op2: mpq_t): longint; cdecl; external LIB name '__gmpq_equal';
{ Get the numerator of a rational }
procedure mpq_get_num(var numerator: mpz_t; var rational: mpq_t); cdecl; external LIB name '__gmpq_get_num';
{ Get the denominator of a rational }
procedure mpq_get_den(var denominator: mpz_t; var rational: mpq_t); cdecl; external LIB name '__gmpq_get_den';
{ Convert op to a double, truncating if necessary (ie. rounding towards zero) }
function mpq_get_d(var op: mpq_t): double; cdecl; external LIB name '__gmpq_get_d';
{ Convert op to a string of digits in base base }
function mpq_get_str(str: pchar; base: longint; var op: mpq_t): pchar; cdecl; external LIB name '__gmpq_get_str';
{ Initialize dest rational and set it to 0/1 }
procedure mpq_init(out dest_rational: mpq_t); cdecl; external LIB name '__gmpq_init';
{ Set inverted_number to 1/number }
procedure mpq_inv(var inverted_number, number: mpq_t); cdecl; external LIB name '__gmpq_inv';
{ Set product to multiplier × multiplicand }
procedure mpq_mul(var product, multiplier, multiplicand: mpq_t); cdecl; external LIB name '__gmpq_mul';
{ Set rop to op1 × (2^op2) }
procedure mpq_mul_2exp(var rop, op1: mpq_t; op2: valuint); cdecl; external LIB name '__gmpq_mul_2exp';
{ Set negated_operand to −operand }
procedure mpq_neg(var negated_operand, operand: mpq_t); cdecl; external LIB name '__gmpq_neg';
{ Assign rop from op }
procedure mpq_set(var rop, op: mpq_t); cdecl; external LIB name '__gmpq_set';
{ Set rop to the value of op. There is no rounding, this conversion is exact. }
procedure mpq_set_d(var rop: mpq_t; op: double); cdecl; external LIB name '__gmpq_set_d';
{ Set the denominator of a rational }
procedure mpq_set_den(var rational: mpq_t; var denominator: mpz_t); cdecl; external LIB name '__gmpq_set_den';
{ Set rop to the value of op. There is no rounding, this conversion is exact. }
procedure mpq_set_f(var rop: mpq_t; var op: mpf_t); cdecl; external LIB name '__gmpq_set_f';
{ Set the numerator of a rational }
procedure mpq_set_num(var rational: mpq_t; var numerator: mpz_t); cdecl; external LIB name '__gmpq_set_num';
{ Set the value of rop to op1/op2 }
procedure mpq_set_si(var rop: mpq_t; op1: valsint; op2: valuint); cdecl; external LIB name '__gmpq_set_si';
{ Set rop from a null-terminated string str in the given base }
function mpq_set_str(var rop: mpq_t; str: pchar; base: longint): longint; cdecl; external LIB name '__gmpq_set_str';
{ Set the value of rop to op1/op2 }
procedure mpq_set_ui(var rop: mpq_t; op1, op2: valuint); cdecl; external LIB name '__gmpq_set_ui';
{ Assign rop from op }
procedure mpq_set_z(var rop: mpq_t; var op: mpz_t); cdecl; external LIB name '__gmpq_set_z';
{ Set difference to minuend − subtrahend }
procedure mpq_sub(var difference, minuend, subtrahend: mpq_t); cdecl; external LIB name '__gmpq_sub';
{ Swap the values rop1 and rop2 efficiently }
procedure mpq_swap(var rop1, rop2: mpq_t); cdecl; external LIB name '__gmpq_swap';

// ---- Floating-point Functions ----

{ Set rop to the absolute value of op }
procedure mpf_abs(var rop, op: mpf_t); cdecl; external LIB name '__gmpf_abs';
{ Set rop to op1 + op2 }
procedure mpf_add(var rop, op1, op2: mpf_t); cdecl; external LIB name '__gmpf_add';
{ Set rop to op1 + op2 }
procedure mpf_add_ui(var rop, op1: mpf_t; op2: valuint); cdecl; external LIB name '__gmpf_add_ui';
{ Set rop to op rounded to the next higher integer }
procedure mpf_ceil(var rop, op: mpf_t); cdecl; external LIB name '__gmpf_ceil';
{ Free the space occupied by x }
procedure mpf_clear(var x: mpf_t); cdecl; external LIB name '__gmpf_clear';
{ Compare op1 and op2. Return a positive value if op1 > op2, zero if op1 = op2, and a negative value if op1 < op2. }
function mpf_cmp(var op1, op2: mpf_t): longint; cdecl; external LIB name '__gmpf_cmp';
{ Compare op1 and op2. Return a positive value if op1 > op2, zero if op1 = op2, and a negative value if op1 < op2. }
function mpf_cmp_d(var op1: mpf_t; op2: double): longint; cdecl; external LIB name '__gmpf_cmp_d';
{ Compare op1 and op2. Return a positive value if op1 > op2, zero if op1 = op2, and a negative value if op1 < op2. }
function mpf_cmp_si(var op1: mpf_t; op2: valsint): longint; cdecl; external LIB name '__gmpf_cmp_si';
{ Compare op1 and op2. Return a positive value if op1 > op2, zero if op1 = op2, and a negative value if op1 < op2. }
function mpf_cmp_ui(var op1: mpf_t; op2: valuint): longint; cdecl; external LIB name '__gmpf_cmp_ui';
{ Set rop to op1/op2 }
procedure mpf_div(var rop, op1, op2: mpf_t); cdecl; external LIB name '__gmpf_div';
{ Set rop to op1/(2^op2) }
procedure mpf_div_2exp(var rop, op1: mpf_t; op2: valuint); cdecl; external LIB name '__gmpf_div_2exp';
{ Set rop to op1/op2 }
procedure mpf_div_ui(var rop, op1: mpf_t; op2: valuint); cdecl; external LIB name '__gmpf_div_ui';

// GDB only: procedure mpf_dump(var _para1: mpf_t); cdecl; external LIB name '__gmpf_dump';

{ Return non-zero if the first op3 bits of op1 and op2 are equal, zero otherwise }
function mpf_eq(var op1, op2: mpf_t; op3: valuint): longint; cdecl; external LIB name '__gmpf_eq';
{ Return non-zero if op would fit in the respective C data type, when truncated to an integer }
function mpf_fits_sint_p(var op: mpf_t): longint; cdecl; external LIB name '__gmpf_fits_sint_p';
{ Return non-zero if op would fit in the respective C data type, when truncated to an integer }
function mpf_fits_slong_p(var op: mpf_t): longint; cdecl; external LIB name '__gmpf_fits_slong_p';
{ Return non-zero if op would fit in the respective C data type, when truncated to an integer }
function mpf_fits_sshort_p(var op: mpf_t): longint; cdecl; external LIB name '__gmpf_fits_sshort_p';
{ Return non-zero if op would fit in the respective C data type, when truncated to an integer }
function mpf_fits_uint_p(var op: mpf_t): longint; cdecl; external LIB name '__gmpf_fits_uint_p';
{ Return non-zero if op would fit in the respective C data type, when truncated to an integer }
function mpf_fits_ulong_p(var op: mpf_t): longint; cdecl; external LIB name '__gmpf_fits_ulong_p';
{ Return non-zero if op would fit in the respective C data type, when truncated to an integer }
function mpf_fits_ushort_p(var op: mpf_t): longint; cdecl; external LIB name '__gmpf_fits_ushort_p';
{ Set rop to op rounded to the next lower }
procedure mpf_floor(var rop, op: mpf_t); cdecl; external LIB name '__gmpf_floor';
{ Convert op to a double, truncating if necessary (ie. rounding towards zero) }
function mpf_get_d(var op: mpf_t): double; cdecl; external LIB name '__gmpf_get_d';
{ Convert op to a double, truncating if necessary (ie. rounding towards zero), and with an exponent returned separately }
function mpf_get_d_2exp(out exp: valsint; var op: mpf_t): double; cdecl; external LIB name '__gmpf_get_d_2exp';
{ Return the default precision actually used }
function mpf_get_default_prec: valuint; cdecl; external LIB name '__gmpf_get_default_prec';
{ Return the current precision of op, in bits }
function mpf_get_prec(var op: mpf_t): valuint; cdecl; external LIB name '__gmpf_get_prec';
{ Convert op to a long, truncating any fraction part }
function mpf_get_si(var op: mpf_t): valsint; cdecl; external LIB name '__gmpf_get_si';
{ Convert op to a string of digits in base base }
function mpf_get_str(str: pchar; out exp: mp_exp_t; base: longint; ndigits: sizeuint; var op: mpf_t): pchar; cdecl; external LIB name '__gmpf_get_str';
{ Convert op to a unsigned long, truncating any fraction part }
function mpf_get_ui(var op: mpf_t): valuint; cdecl; external LIB name '__gmpf_get_ui';
{ Initialize x to 0 }
procedure mpf_init(out x: mpf_t); cdecl; external LIB name '__gmpf_init';
{ Initialize x to 0 and set its precision to be at least prec bits }
procedure mpf_init2(out x: mpf_t; prec: valuint); cdecl; external LIB name '__gmpf_init2';
{ Initialize rop and set its value from op }
procedure mpf_init_set(out rop: mpf_t; var op: mpf_t); cdecl; external LIB name '__gmpf_init_set';
{ Initialize rop and set its value from op }
procedure mpf_init_set_d(out rop: mpf_t; op: double); cdecl; external LIB name '__gmpf_init_set_d';
{ Initialize rop and set its value from op }
procedure mpf_init_set_si(out rop: mpf_t; op: valsint); cdecl; external LIB name '__gmpf_init_set_si';
{ Initialize rop and set its value from the string in str }
function mpf_init_set_str(out rop: mpf_t; str: pchar; base: longint): longint; cdecl; external LIB name '__gmpf_init_set_str';
{ Initialize rop and set its value from op }
procedure mpf_init_set_ui(out rop: mpf_t; op: valuint); cdecl; external LIB name '__gmpf_init_set_ui';
{ Return non-zero if op is an integer }
function mpf_integer_p(var op: mpf_t): longint; cdecl; external LIB name '__gmpf_integer_p';
{ Set rop to op1 × op2 }
procedure mpf_mul(var rop, op1, op2: mpf_t); cdecl; external LIB name '__gmpf_mul';
{ Set rop to op1 × (2^op2) }
procedure mpf_mul_2exp(var rop, op1: mpf_t; op2: valuint); cdecl; external LIB name '__gmpf_mul_2exp';
{ Set rop to op1 × op2 }
procedure mpf_mul_ui(var rop, op1: mpf_t; op2: valuint); cdecl; external LIB name '__gmpf_mul_ui';
{ Set rop to −op }
procedure mpf_neg(var rop, op: mpf_t); cdecl; external LIB name '__gmpf_neg';
{ Set rop to op1^op2 }
procedure mpf_pow_ui(var rop, op1: mpf_t; op2: valuint); cdecl; external LIB name '__gmpf_pow_ui';
{ Generate a random float of at most max_size limbs, with long strings of zeros and ones in the binary representation }
procedure mpf_random2(var rop: mpf_t; max_size: mp_size_t; exp: mp_exp_t); cdecl; external LIB name '__gmpf_random2';
{ Compute the relative difference between op1 and op2 and store the result in rop }
procedure mpf_reldiff(var rop, op1, op2: mpf_t); cdecl; external LIB name '__gmpf_reldiff';
{ Set the value of rop from op }
procedure mpf_set(var rop, op: mpf_t); cdecl; external LIB name '__gmpf_set';
{ Set the value of rop from op }
procedure mpf_set_d(var rop: mpf_t; op: double); cdecl; external LIB name '__gmpf_set_d';
{ Set the default precision to be at least prec bits }
procedure mpf_set_default_prec(prec: valuint); cdecl; external LIB name '__gmpf_set_default_prec';
{ Set the precision of rop to be at least prec bits }
procedure mpf_set_prec(var rop: mpf_t; prec: valuint); cdecl; external LIB name '__gmpf_set_prec';
{ Set the precision of rop to be at least prec bits, without changing the memory allocated }
procedure mpf_set_prec_raw(var rop: mpf_t; prec: valuint); cdecl; external LIB name '__gmpf_set_prec_raw';
{ Set the value of rop from op }
procedure mpf_set_q(var rop: mpf_t; var op: mpq_t); cdecl; external LIB name '__gmpf_set_q';
{ Set the value of rop from op }
procedure mpf_set_si(var rop: mpf_t; op: valsint); cdecl; external LIB name '__gmpf_set_si';
{ Set the value of rop from the string in str }
function mpf_set_str(var rop: mpf_t; str: pchar; base: longint): longint; cdecl; external LIB name '__gmpf_set_str';
{ Set the value of rop from op }
procedure mpf_set_ui(var rop: mpf_t; op: valuint); cdecl; external LIB name '__gmpf_set_ui';
{ Set the value of rop from op }
procedure mpf_set_z(var rop: mpf_t; var op: mpz_t); cdecl; external LIB name '__gmpf_set_z';

// No docs: function mpf_size(var _para1: mpf_t): size_t; cdecl; external LIB name '__gmpf_size';

{ Set rop to op^(1/2) }
procedure mpf_sqrt(var rop, op: mpf_t); cdecl; external LIB name '__gmpf_sqrt';
{ Set rop to op^(1/2) }
procedure mpf_sqrt_ui(var rop: mpf_t; op: valuint); cdecl; external LIB name '__gmpf_sqrt_ui';
{ Set rop to op1 − op2 }
procedure mpf_sub(var rop, op1, op2: mpf_t); cdecl; external LIB name '__gmpf_sub';
{ Set rop to op1 − op2 }
procedure mpf_sub_ui(var rop, op1: mpf_t; op2: valuint); cdecl; external LIB name '__gmpf_sub_ui';
{ Swap rop1 and rop2 efficiently }
procedure mpf_swap(var rop1, rop2: mpf_t); cdecl; external LIB name '__gmpf_swap';
{ Set rop to op rounded to the integer towards zero }
procedure mpf_trunc(var rop, op: mpf_t); cdecl; external LIB name '__gmpf_trunc';
{ Set rop to op1/op2 }
procedure mpf_ui_div(var rop: mpf_t; op1: valuint; var op2: mpf_t); cdecl; external LIB name '__gmpf_ui_div';
{ Set rop to op1 − op2 }
procedure mpf_ui_sub(var rop: mpf_t; op1: valuint; var op2: mpf_t); cdecl; external LIB name '__gmpf_ui_sub';
{ Generate a uniformly distributed random float in rop, such that 0 <= rop < 1, with nbits significant bits in the mantissa }
procedure mpf_urandomb(var rop: mpf_t; var state: randstate_t; nbits: valuint); cdecl; external LIB name '__gmpf_urandomb';

// ---- Low-level Functions ----

{ Add [s1p, s1n] and [s2p, s2n], and write the s1n least significant limbs of the result to rp. Return carry, either 0 or 1. }
function mpn_add(rop, s1p: mpn_ptr; s1n: mp_size_t; s2p: mpn_ptr; s2n: mp_size_t): mp_limb_t; cdecl; external LIB name '__gmpn_add';
{ Add [s1p, n] and s2limb, and write the n least significant limbs of the result to rp. Return carry, either 0 or 1. }
function mpn_add_1(rp, s1p: mpn_ptr; n: mp_size_t; s2limb: mp_limb_t): mp_limb_t; cdecl; external LIB name '__gmpn_add_1';
{ Add [s1p, n] and [s2p, n], and write the n least significant limbs of the result to rp. Return carry, either 0 or 1. }
function mpn_add_n(rop, s1p, s2p: mpn_ptr; n: mp_size_t): mp_limb_t; cdecl; external LIB name '__gmpn_add_n';
{ Multiply [s1p, n] and s2limb, and add the n least significant limbs of the product to [rp, n] and write the result to rp. Return the most significant limb of the product, plus carry-out from the addition. }
function mpn_addmul_1(rp, s1p: mpn_ptr; n: mp_size_t; s2limb: mp_limb_t): mp_limb_t; cdecl; external LIB name '__gmpn_addmul_1';

{ Preliminary: This function puts the low floor(d/mp_bits_per_limb) limbs of q = [s1p, s1n]/[s2p, s2n] mod 2^d at rp, and returns the high d mod mp_bits_per_limb bits of q }
// function mpn_bdivmod(rp, s1p: mpn_t; s1n: mp_size_t; s2p: mpn_t; s2n: mp_size_t; d: valuint): mp_limb_t; cdecl; external LIB name '__gmpn_bdivmod';

{ Compare [s1p, n] and [s2p, n] and return a positive value if s1 > s2, 0 if they are equal, or a negative value if s1 < s2 }
function mpn_cmp(s1p, s2p: mpn_ptr; n: mp_size_t): longint; cdecl; external LIB name '__gmpn_cmp';
{ Divide [sp, n] by 3, expecting it to divide exactly, and writing the result to [rp, n] }
function mpn_divexact_by3c(rp, sp: mpn_ptr; n: mp_size_t; carry: mp_limb_t): mp_limb_t; cdecl; external LIB name '__gmpn_divexact_by3c';

{ Obsolete: Divide [rs2p, rs2n] by [s3p, s3n], and write the quotient at r1p, with the exception of the most significant limb, which is returned }
// function mpn_divrem(r1p: mpn_t; qxn: mp_size_t; rs2p: mpn_t; rs2n: mp_size_t; s3p: mpn_t; s3n: mp_size_t): mp_limb_t; cdecl; external LIB name '__gmpn_divrem';

{ Divide [s2p, s2n] by s3limb, and write the quotient at r1p. Return the remainder }
function mpn_divrem_1(r1p: mpn_ptr; qxn: mp_size_t; s2p: mpn_ptr; s2n: mp_size_t; s2limb: mp_limb_t): mp_limb_t; cdecl; external LIB name '__gmpn_divrem_1';

// No docs: function mpn_divrem_2(_para1: mpn_t; _para2: mp_size_t; _para3: mpn_t; _para4: mp_size_t; _para5: mpn_t): mp_limb_t; cdecl; external LIB name '__gmpn_divrem_2';

{ Set [rp, retval] to the greatest common divisor of [s1p, s1n] and [s2p, s2n] }
function mpn_gcd(rp, s1p: mpn_ptr; s1n: mp_size_t; s2p: mpn_ptr; s2n: mp_size_t): mp_size_t; cdecl; external LIB name '__gmpn_gcd';
{ Return the greatest common divisor of [s1p, s1n] and s2limb }
function mpn_gcd_1(s1p: mpn_ptr; s1n: mp_size_t; s2limb: mp_limb_t): mp_limb_t; cdecl; external LIB name '__gmpn_gcd_1';
{ Calculate the greatest common divisor of [s1p, s1n] and [s2p, s2n] }
function mpn_gcdext(r1p, r2p: mpn_ptr; out r2n: mp_size_t; s1p: mpn_ptr; s1n: mp_size_t; s2p: mpn_ptr; s2n: mp_size_t): mp_size_t; cdecl; external LIB name '__gmpn_gcdext';
{ Convert [s1p, s1n] to a raw unsigned char array at str in base base, and return the number of characters produced }
function mpn_get_str(str: pbyte; base: longint; s1p: mpn_ptr; s1n: mp_size_t):sizeuint; cdecl; external LIB name '__gmpn_get_str';
{ Compute the hamming distance between [s1p, n] and [s2p, n], which is the number of bit positions where the two operands have different bit values }
function mpn_hamdist(s1p, s2p: mpn_ptr; n: mp_size_t): valuint; cdecl; external LIB name '__gmpn_hamdist';
{ Shift [sp, n] left by count bits, and write the result to [rp, n] }
function mpn_lshift(rp, sp: mpn_ptr; n: mp_size_t; count: dword): mp_limb_t; cdecl; external LIB name '__gmpn_lshift';
{ Divide [s1p, s1n] by s2limb, and return the remainder. s1n can be zero. }
function mpn_mod_1(s1p: mpn_ptr; s1n: mp_size_t; s2limb: mp_limb_t): mp_limb_t; cdecl; external LIB name '__gmpn_mod_1';
{ Multiply [s1p, s1n] and [s2p, s2n], and write the result to rp. Return the most significant limb of the result. }
function mpn_mul(rp, s1p: mpn_ptr; s1n: mp_size_t; s2p: mpn_ptr; s2n: mp_size_t): mp_limb_t; cdecl; external LIB name '__gmpn_mul';
{ Multiply [s1p, n] by s2limb, and write the n least significant limbs of the product to rp. Return the most significant limb of the product. }
function mpn_mul_1(rp, s1p: mpn_ptr; n: mp_size_t; s2limb: mp_limb_t): mp_limb_t; cdecl; external LIB name '__gmpn_mul_1';
{ Multiply [s1p, n] and [s2p, n], and write the 2*n-limb result to rp }
procedure mpn_mul_n(rp, s1p, s2p: mpn_ptr; n: mp_size_t); cdecl; external LIB name '__gmpn_mul_n';
{ Return non-zero iff [s1p, n] is a perfect square }
function mpn_perfect_square_p(s1p: mpn_ptr; n: mp_size_t): longint; cdecl; external LIB name '__gmpn_perfect_square_p';
{ Count the number of set bits in [s1p, n] }
function mpn_popcount(s1p: mpn_ptr; n: mp_size_t): valuint; cdecl; external LIB name '__gmpn_popcount';

// No docs: function mpn_pow_1(_para1, _para2: mpn_t; _para3: mp_size_t; _para4, _para5: mpn_t): mp_size_t; cdecl; external LIB name '__gmpn_pow_1';
// No docs: function mpn_preinv_mod_1(_para1: mpn_t; _para2: mp_size_t; _para3, _para4: mp_limb_t): mp_limb_t; cdecl; external LIB name '__gmpn_preinv_mod_1';

{ Generate a random number of length r1n and store it at r1p }
procedure mpn_random(r1p: mpn_ptr; r1n: mp_size_t); cdecl; external LIB name '__gmpn_random';
{ Generate a random number of length r1n and store it at r1p }
procedure mpn_random2(r1p: mpn_ptr; r1n: mp_size_t); cdecl; external LIB name '__gmpn_random2';
{ Shift [sp, n] right by count bits, and write the result to [rp, n] }
function mpn_rshift(rp, sp: mpn_ptr; n: mp_size_t; count: dword): mp_limb_t; cdecl; external LIB name '__gmpn_rshift';
{ Scan s1p from bit position bit for the next clear bit }
function mpn_scan0(s1p: mpn_ptr; bit: valuint): valuint; cdecl; external LIB name '__gmpn_scan0';
{ Scan s1p from bit position bit for the next set bit }
function mpn_scan1(s1p: mpn_ptr; bit: valuint): valuint; cdecl; external LIB name '__gmpn_scan1';
{ Convert bytes [str,strsize] in the given base to limbs at rp }
function mpn_set_str(rp: mpn_ptr; str: pbyte; strsize: sizeuint; base:longint): mp_size_t; cdecl; external LIB name '__gmpn_set_str';
{ Compute the square root of [sp, n] and put the result at [r1p, dn/2e] and the remainder at [r2p, retval] }
function mpn_sqrtrem(r1p, r2p, sp: mpn_ptr; n: mp_size_t): mp_size_t; cdecl; external LIB name '__gmpn_sqrtrem';
{ Subtract [s2p, s2n] from [s1p, s1n], and write the s1n least significant limbs of the result to rp. Return borrow, either 0 or 1. }
function mpn_sub(rp, s1p: mpn_ptr; s1n: mp_size_t; s2p: mpn_ptr; s2n: mp_size_t): mp_limb_t; cdecl; external LIB name '__gmpn_sub';
{ Subtract s2limb from [s1p, n], and write the n least significant limbs of the result to rp. Return borrow, either 0 or 1. }
function mpn_sub_1(rp, s1p: mpn_ptr; n: mp_size_t; s2limb: mp_limb_t): mp_limb_t; cdecl; external LIB name '__gmpn_sub_1';
{ Subtract [s2p, n] from [s1p, n], and write the n least significant limbs of the result to rp. Return borrow, either 0 or 1. }
function mpn_sub_n(rp, s1p, s2p: mpn_ptr; n: mp_size_t): mp_limb_t; cdecl; external LIB name '__gmpn_sub_n';
{ Multiply [s1p, n] and s2limb, and subtract the n least significant limbs of the product from [rp, n] and write the result to rp. Return the most significant limb of the product, plus borrow-out from the subtraction. }
function mpn_submul_1(rp, s1p: mpn_ptr; n: mp_size_t; s2limb: mp_limb_t): mp_limb_t; cdecl; external LIB name '__gmpn_submul_1';
{ Divide [np, nn] by [dp, dn] and put the quotient at [qp, nn−dn+1] and the remainder at [rp, dn] }
procedure mpn_tdiv_qr(qp, rp: mpn_ptr; qxn: mp_size_t; np: mpn_ptr; nn: mp_size_t; dp: mpn_ptr; dn: mp_size_t); cdecl; external LIB name '__gmpn_tdiv_qr';

// ---- GMP properties ----

{ Size of a limb on this machine }
function bits_per_limb: longint;
{ Some GMP functions may set this thread unsafe variable. Better avoid using it. }
function errno: longint;
{ GMP version string a.b.c }
function version: string;

// ==== ext bindings =====

// ---- Random Number Functions ----

{ Initialize state with a default algorithm }
procedure randinit_default(out state: MPRandState);
{ Initialize state with a linear congruential algorithm X = (aX + c) mod 2^m2exp }
procedure randinit_lc_2exp(out state: MPRandState; var a: MPInteger; c, m2exp: valuint);
{ Initialize state for a linear congruential algorithm as per gmp_randinit_lc_2exp }
function randinit_lc_2exp_size(out state: MPRandState; size: sizeuint): boolean;
{ Initialize state for a Mersenne Twister algorithm }
procedure randinit_mt(out state: MPRandState);
{ Initialize rop with a copy of the algorithm and state from op }
procedure randinit_set(out rop: MPRandState; var op: MPRandState);
{ Set an initial seed value into state }
procedure randseed(var state: MPRandState; var seed: MPInteger);
{ Set an initial seed value into state }
procedure randseed_ui(var state: MPRandState; seed: valuint);
{ Free all memory occupied by state }
procedure randclear(var state: MPRandState);
{ Return an uniformly distributed random number of n bits, ie. in the range 0 to 2^n−1 inclusive }
function urandomb_ui(var state: MPRandState; n: valuint): valuint;
{ Return an uniformly distributed random number in the range 0 to n − 1, inclusive }
function urandomm_ui(var state: MPRandState; n: valuint): valuint;

// ---- integer Functions ----

{ Change the space for integer to new_alloc limbs }
function z_realloc(var integer_: MPInteger; new_alloc: mp_size_t): pointer;
{ Set rop to the absolute value of op }
procedure z_abs(var rop, op: MPInteger);
{ Return the absolute value of op }
function z_abs(var op: MPInteger): MPInteger;
{ Set rop to op1 + op2 }
procedure z_add(var rop, op1, op2: MPInteger);
{ Return op1 + op2 }
function z_add(var op1, op2: MPInteger): MPInteger;
{ Set rop to op1 + op2 }
procedure z_add_ui(var rop, op1: MPInteger; op2: valuint);
{ Return op1 + op2 }
function z_add_ui(var op1: MPInteger; op2: valuint): MPInteger;
{ Set rop to rop + op1 × op2 }
procedure z_addmul(var rop, op1, op2: MPInteger);
{ Set rop to rop + op1 × op2 }
procedure z_addmul_ui(var rop, op1: MPInteger; op2: valuint);
{ Set rop to op1 bitwise-and op2 }
procedure z_and(var rop, op1, op2: MPInteger);
{ Return op1 bitwise-and op2 }
function z_and(var op1, op2: MPInteger): MPInteger;
//{ _Fixed_ space of fixed_num_bits is allocated to each of the array size integers in integer array }
//procedure z_array_init(var integer_array: MPInteger; array_size, fixed_num_bits: mp_size_t);
{ Compute the binomial coefficient (n over k) and store the result in rop }
procedure z_bin_ui(var rop, n: MPInteger; k: valuint);
{ Return the binomial coefficient (n over k) }
function z_bin_ui(var n: MPInteger; k: valuint): MPInteger;
{ Compute the binomial coefficient (n over k) and store the result in rop }
procedure z_bin_uiui(var rop: MPInteger; n, k: valuint);
{ Return the binomial coefficient (n over k) }
function z_bin_uiui(n, k: valuint): MPInteger;
{ Divide n by d, forming a quotient q. Round mode ceil. }
procedure z_cdiv_q(var q, n, d: MPInteger);
{ Divide n by d, forming a return quotient. Round mode ceil. }
function z_cdiv_q(var n, d: MPInteger): MPInteger;
{ Divide n by d, forming a quotient q. d = 2^b. Round mode ceil. }
procedure z_cdiv_q_2exp(var q, n: MPInteger; b: valuint);
{ Divide n by d, forming a return quotient. d = 2^b. Round mode ceil. }
function z_cdiv_q_2exp(var n: MPInteger; b: valuint): MPInteger;
{ Divide n by d, forming a quotient q. Round mode ceil. }
function z_cdiv_q_ui(var q, n: MPInteger; d: valuint): valuint;
{ Divide n by d, forming a quotient q and remainder r. Round mode ceil. }
procedure z_cdiv_qr(var q, r, n, d: MPInteger);
{ Divide n by d, forming a quotient q and remainder r. Round mode ceil. }
function z_cdiv_qr_ui(var q, r, n: MPInteger; d: valuint): valuint;
{ Divide n by d, forming a remainder r. Round mode ceil. }
procedure z_cdiv_r(var r, n, d: MPInteger);
{ Divide n by d, forming a return remainder. Round mode ceil. }
function z_cdiv_r(var n, d: MPInteger): MPInteger;
{ Divide n by d, forming a remainder r. d = 2^b. Round mode ceil. }
procedure z_cdiv_r_2exp(var r, n: MPInteger; b: valuint);
{ Divide n by d, forming a return remainder. d = 2^b. Round mode ceil. }
function z_cdiv_r_2exp(var n: MPInteger; b: valuint): MPInteger;
{ Divide n by d, forming a remainder r. Round mode ceil. }
function z_cdiv_r_ui(var r, n: MPInteger; d: valuint): valuint;
{ Divide n by d. Round mode ceil. }
function z_cdiv_ui(var n: MPInteger; d: valuint): valuint;
{ Free the space occupied by integer. Call this function for all MPInteger variables when you are done with them. }
procedure z_clear(var integer_: MPInteger);
{ Clear bit bit_index in rop }
procedure z_clrbit(var rop: MPInteger; bit_index: valuint);
{ Compare op1 and op2. Return a positive value if op1 > op2, zero if op1 = op2, or a negative value if op1 < op2. }
function z_cmp(var op1, op2: MPInteger): longint;
{ Compare op1 and op2. Return a positive value if op1 > op2, zero if op1 = op2, or a negative value if op1 < op2. }
function z_cmp_d(var op1: MPInteger; op2: double): longint;
{ Compare op1 and op2. Return a positive value if op1 > op2, zero if op1 = op2, or a negative value if op1 < op2. }
function z_cmp_si(var op1: MPInteger; op2: valsint): longint;
{ Compare op1 and op2. Return a positive value if op1 > op2, zero if op1 = op2, or a negative value if op1 < op2. }
function z_cmp_ui(var op1: MPInteger; op2: valuint): longint;
{ Compare the absolute values of op1 and op2. Return a positive value if |op1| > |op2|, zero if |op1| = |op2|, or a negative value if |op1| < |op2|. }
function z_cmpabs(var op1, op2: MPInteger): longint;
{ Compare the absolute values of op1 and op2. Return a positive value if |op1| > |op2|, zero if |op1| = |op2|, or a negative value if |op1| < |op2|. }
function z_cmpabs_d(var op1: MPInteger; op2: double): longint;
{ Compare the absolute values of op1 and op2. Return a positive value if |op1| > |op2|, zero if |op1| = |op2|, or a negative value if |op1| < |op2|. }
function z_cmpabs_ui(var op1: MPInteger; op2: valuint): longint;
{ Set rop to the one’s complement of op }
procedure z_com(var rop, op: MPInteger);
{ Return the one’s complement of op }
function z_com(var op: MPInteger): MPInteger;
{ Complement bit bit_index in rop }
procedure z_combit(var rop: MPInteger; bit_index: valuint);
{ Return true if n is congruent to c modulo d }
function z_congruent_p(var n, c, d: MPInteger): boolean;
{ Return true if n is congruent to c modulo 2^b }
function z_congruent_2exp_p(var n, c: MPInteger; b: valuint): boolean;
{ Return true if n is congruent to c modulo d }
function z_congruent_ui_p(var n: MPInteger; c, d: valuint): boolean;
{ Set q to n/d }
procedure z_divexact(var q, n, d: MPInteger);
{ Return n/d }
function z_divexact(var n, d: MPInteger): MPInteger;
{ Set q to n/d }
procedure z_divexact_ui(var q, n: MPInteger; d: valuint);
{ Return n/d }
function z_divexact_ui(var n: MPInteger; d: valuint): MPInteger;
{ Return true if n is exactly divisible by d }
function z_divisible_p(var n, d: MPInteger): boolean;
{ Return true if n is exactly divisible by d }
function z_divisible_ui_p(var n: MPInteger; d: valuint): boolean;
{ Return true if n is exactly divisible by by 2^b }
function z_divisible_2exp_p(var n: MPInteger; b: valuint): boolean;
{ Fill buf with word data from op }
function z_export(out buf; out countp: sizeuint; order: longint; size: sizeuint; endian: longint; nails: sizeuint; var op: MPInteger): pointer;
{ Set rop to op!, the factorial of op }
procedure z_fac_ui(var rop: MPInteger; op: valuint);
{ Return op!, the factorial of op }
function z_fac_ui(op: valuint): MPInteger;
{ Divide n by d, forming a quotient q. Round mode floor. }
procedure z_fdiv_q(var q, n, d: MPInteger);
{ Divide n by d, forming a return quotient. Round mode floor. }
function z_fdiv_q(var n, d: MPInteger): MPInteger;
{ Divide n by d, forming a quotient q. d = 2^b. Round mode floor. }
procedure z_fdiv_q_2exp(var q, n: MPInteger; b: valuint);
{ Divide n by d, forming a return quotient. d = 2^b. Round mode floor. }
function z_fdiv_q_2exp(var n: MPInteger; b: valuint): MPInteger;
{ Divide n by d, forming a quotient q. Round mode floor. }
function z_fdiv_q_ui(var q, n: MPInteger; d: valuint): valuint;
{ Divide n by d, forming a quotient q and remainder r. Round mode floor. }
procedure z_fdiv_qr(var q, r, n, d: MPInteger);
{ Divide n by d, forming a quotient q and remainder r. Round mode floor. }
function z_fdiv_qr_ui(var q, r, n: MPInteger; d: valuint): valuint;
{ Divide n by d, forming a remainder r. Round mode floor. }
procedure z_fdiv_r(var r, n, d: MPInteger);
{ Divide n by d, forming a return remainder. Round mode floor. }
function z_fdiv_r(var n, d: MPInteger): MPInteger;
{ Divide n by d, forming a remainder r. d = 2^b. Round mode floor. }
procedure z_fdiv_r_2exp(var r, n: MPInteger; b: valuint);
{ Divide n by d, forming a return remainder. d = 2^b. Round mode floor. }
function z_fdiv_r_2exp(var n: MPInteger; b: valuint): MPInteger;
{ Divide n by d, forming a remainder r. Round mode floor. }
function z_fdiv_r_ui(var r, n: MPInteger; d: valuint): valuint;
{ Divide n by d. Round mode floor. }
function z_fdiv_ui(var n: MPInteger; d: valuint): valuint;
{ Set fn to to Fn, the n’th Fibonacci number }
procedure z_fib_ui(var fn: MPInteger; n: valuint);
{ Return Fn, the n’th Fibonacci number }
function z_fib_ui(n: valuint): MPInteger;
{ Set fn to Fn, and fnsub1 to Fn−1 }
procedure z_fib2_ui(var fn, fnsub1: MPInteger; n: valuint);
{ Return Fn, and fnsub1 = Fn−1 }
function z_fib2_ui(var fnsub1: MPInteger; n: valuint): MPInteger;
{ Return true iff the value of op fits in an signed int }
function z_fits_sint_p(var op: MPInteger): boolean;
{ Return true iff the value of op fits in an signed long int }
function z_fits_slong_p(var op: MPInteger): boolean;
{ Return true iff the value of op fits in an signed short int }
function z_fits_sshort_p(var op: MPInteger): boolean;
{ Return true iff the value of op fits in an unsigned int }
function z_fits_uint_p(var op: MPInteger): boolean;
{ Return true iff the value of op fits in an unsigned long int }
function z_fits_ulong_p(var op: MPInteger): boolean;
{ Return true iff the value of op fits in an unsigned short int }
function z_fits_ushort_p(var op: MPInteger): boolean;
{ Set rop to the greatest common divisor of op1 and op2 }
procedure z_gcd(var rop, op1, op2: MPInteger);
{ Return the greatest common divisor of op1 and op2 }
function z_gcd(var op1, op2: MPInteger): MPInteger;
{ Compute the greatest common divisor of op1 and op2 }
function z_gcd_ui(var rop, op1: MPInteger; op2: valuint): valuint;
{ Set g to the greatest common divisor of a and b, and in addition set s and t to coefficients satisfying as + bt = g }
procedure z_gcdext(var g, s, t, a, b: MPInteger);
{ Convert op to a double, truncating if necessary (ie. rounding towards zero) }
function z_get_d(var op: MPInteger): double;
{ Convert op to a double, truncating if necessary (ie. rounding towards zero), and returning the exponent separately }
function z_get_d_2exp(out exp: valsint; var op: MPInteger): double;
{ Return the value of op as a signed long }
function z_get_si(op: MPInteger): valsint;
{ Convert op to a string of digits in base base. The base argument may vary from 2 to 62 or from −2 to −36. }
function z_get_str(base: longint; var op: MPInteger): string;
{ Convert op to a string of digits in base base. The base argument may vary from 2 to 62 or from −2 to −36. If str is NULL, the result string is allocated using the current allocation function }
function z_get_str(str: pchar; base: longint; var op: MPInteger): pchar;
{ Return the value of op as an unsigned long }
function z_get_ui(op: MPInteger): valuint;
{ Return limb number n from op }
function z_getlimbn(var op: MPInteger; n: mp_size_t): mp_limb_t;
{ If op1 and op2 are both >= 0 or both < 0, return the hamming distance between the two operands, which is the number of bit positions where op1 and op2 have different bit values }
function z_hamdist(var op1, op2: MPInteger): valuint;
{ Set rop from an array of word data at op }
procedure z_import(var rop: MPInteger; count: sizeuint; order: longint; size: sizeuint; endian: longint; nails: sizeuint; const op);
{ Initialize integer, and set its value to 0 }
procedure z_init(out integer_: MPInteger);
{ Initialize integer, with space for n bits, and set its value to 0 }
procedure z_init2(out integer_: MPInteger; n: valuint);
{ Initialize rop with limb space and set the initial numeric value from op }
procedure z_init_set(out rop: MPInteger; var op: MPInteger);
{ Initialize rop with limb space and set the initial numeric value from op }
procedure z_init_set_d(out rop: MPInteger; op: double);
{ Initialize rop with limb space and set the initial numeric value from op }
procedure z_init_set_si(out rop: MPInteger; op: valsint);
{ Initialize rop and set its value like z_set_str. If the string is a correct base base number, the function returns true. }
function z_init_set_str(out rop: MPInteger; str: string; base: longint): boolean;
{ Initialize rop with limb space and set the initial numeric value from op }
procedure z_init_set_ui(out rop: MPInteger; op: valuint);
{ Compute the inverse of op1 modulo op2 and put the result in rop }
function z_invert(var rop, op1, op2: MPInteger): longint;
{ Set rop to op1 bitwise inclusive-or op2 }
procedure z_ior(var rop, op1, op2: MPInteger);
{ Return bitwise inclusive-or op2 }
function z_ior(var op1, op2: MPInteger): MPInteger;
{ Calculate the Jacobi symbol. This is defined only for b odd }
function z_jacobi(var a, b: MPInteger): longint;
{ Calculate the Jacobi symbol with the Kronecker extension }
function z_kronecker_si(var a: MPInteger; b: valsint): longint;
{ Calculate the Jacobi symbol with the Kronecker extension }
function z_kronecker_ui(var a: MPInteger; b: valuint): longint;
{ Calculate the Jacobi symbol with the Kronecker extension }
function z_si_kronecker(a: valsint; var b: MPInteger): longint;
{ Calculate the Jacobi symbol with the Kronecker extension }
function z_ui_kronecker(a: valuint; var b: MPInteger): longint;
{ Set rop to the least common multiple of op1 and op2 }
procedure z_lcm(var rop, op1, op2: MPInteger);
{ Return the least common multiple of op1 and op2 }
function z_lcm(var op1, op2: MPInteger): MPInteger;
{ Set rop to the least common multiple of op1 and op2 }
procedure z_lcm_ui(var rop, op1: MPInteger; op2: valuint);
{ Return the least common multiple of op1 and op2 }
function z_lcm_ui(var op1: MPInteger; op2: valuint): MPInteger;
{ Set ln to to Ln, the n’th Lucas number }
procedure z_lucnum_ui(var ln: MPInteger; n: valuint);
{ Return Ln, the n’th Lucas number }
function z_lucnum_ui(n: valuint): MPInteger;
{ Set ln to Ln, and lnsub1 to Ln−1 }
procedure z_lucnum2_ui(var ln, lnsub1: MPInteger; n: valuint);
{ Return Ln, and lnsub1 to Ln−1 }
function z_lucnum2_ui(var lnsub1: MPInteger; n: valuint): MPInteger;
{ Set r to n mod d. The sign of the divisor is ignored; the result is always non-negative. }
procedure z_mod(var r, n, d: MPInteger);
{ Return n mod d. The sign of the divisor is ignored; the result is always non-negative. }
function z_mod(var n, d: MPInteger): MPInteger;
{ Set rop to op1 × op2 }
procedure z_mul(var rop, op1, op2: MPInteger);
{ Return op1 × op2 }
function z_mul(var op1, op2: MPInteger): MPInteger;
{ Set rop to op1 × 2^op2. This operation can also be defined as a left shift by op2 bits. }
procedure z_mul_2exp(var rop, op1: MPInteger; op2: valuint);
{ Return op1 × 2^op2. This operation can also be defined as a left shift by op2 bits. }
function z_mul_2exp(var op1: MPInteger; op2: valuint): MPInteger;
{ Set rop to op1 × op2 }
procedure z_mul_si(var rop, op1: MPInteger; op2: valsint);
{ Return op1 × op2 }
function z_mul_si(var op1: MPInteger; op2: valsint): MPInteger;
{ Set rop to op1 × op2 }
procedure z_mul_ui(var rop, op1: MPInteger; op2: valuint);
{ Return op1 × op2 }
function z_mul_ui(var op1: MPInteger; op2: valuint): MPInteger;
{ Set rop to −op }
procedure z_neg(var rop, op: MPInteger);
{ Return −op }
function z_neg(var op: MPInteger): MPInteger;
{ Set rop to the next prime greater than op }
procedure z_nextprime(var rop, op: MPInteger);
{ Return the next prime greater than op }
function z_nextprime(var op: MPInteger): MPInteger;
{ Return true if op is a perfect power, i.e., if there exist integers a and b, with b > 1, such that op = a^b }
function z_perfect_power_p(var op: MPInteger): boolean;
{ Return true if op is a perfect square, i.e., if the square root of op is an integer }
function z_perfect_square_p(var op: MPInteger): boolean;
{ If op >= 0, return the population count of op, which is the number of 1 bits in the binary representation }
function z_popcount(var op: MPInteger): valuint;
{ Set rop to base^exp. The case 0^0 yields 1. }
procedure z_pow_ui(var rop, base: MPInteger; exp: valuint);
{ Return base^exp. The case 0^0 yields 1. }
function z_pow_ui(var base: MPInteger; exp: valuint): MPInteger;
{ Set rop to base^exp mod mod_ }
procedure z_powm(var rop, base, exp, mod_: MPInteger);
{ Return base^exp mod mod_ }
function z_powm(var base, exp, mod_: MPInteger): MPInteger;
{ Set rop to base^exp mod mod_ }
procedure z_powm_ui(var rop, base: MPInteger; exp: valuint; var mod_: MPInteger);
{ Return base^exp mod mod_ }
function z_powm_ui(var base: MPInteger; exp: valuint; var mod_: MPInteger): MPInteger;
{ Determine whether n is prime. Return 2 if n is definitely prime, return 1 if n is probably prime (without being certain), or return 0 if n is definitely composite. }
function z_probab_prime_p(var n: MPInteger; reps: longint): longint;
{ Change the space allocated for integer to n bits. The value in integer is preserved if it fits, or is set to 0 if not. }
procedure z_realloc2(var integer_: MPInteger; n: valuint);
{ Remove all occurrences of the factor f from op and store the result in rop }
function z_remove(var rop, op, f: MPInteger): valuint;
{ Set rop to trunc(op^(1/n)), the truncated integer part of the nth root of op. Return true if the computation was exact, i.e., if op is rop to the nth power. }
function z_root(var rop, op: MPInteger; n: valuint): boolean;
{ Set root to trunc(u^(1/n)), the truncated integer part of the nth root of u. Set rem to the remainder, (u − root^n). }
procedure z_rootrem(var root, rem, u: MPInteger; n: valuint);
{ Generate a random integer with long strings of zeros and ones in the binary representation }
procedure z_rrandomb(var rop: MPInteger; var state: MPRandState; n: valuint);
{ Return a random integer with long strings of zeros and ones in the binary representation }
function z_rrandomb(var state: MPRandState; n: valuint): MPInteger;
{ Scan op, starting from bit starting_bit, towards more significant bits, until the first 0 bit is found }
function z_scan0(var op: MPInteger; starting_bit: valuint): valuint;
{ Scan op, starting from bit starting_bit, towards more significant bits, until the first 1 bit is found }
function z_scan1(var op: MPInteger; starting_bit: valuint): valuint;
{ Set the value of rop from op }
procedure z_set(var rop, op: MPInteger);
{ Set the value of rop from op }
procedure z_set_d(var rop: MPInteger; op: double);
{ Set the value of rop from op }
procedure z_set_f(var rop: MPInteger; var op: MPFloat);
{ Set the value of rop from op }
procedure z_set_q(var rop: MPInteger; var op: MPRational);
{ Set the value of rop from op }
procedure z_set_si(var rop: MPInteger; op: valsint);
{ Set the value of rop from str, a null-terminated C string in base base. If the string is a correct base base number, the function returns true. }
function z_set_str(var rop: MPInteger; str: string; base: longint): boolean;
{ Set the value of rop from op }
procedure z_set_ui(var rop: MPInteger; op: valuint);
{ Set bit bit_index in rop }
procedure z_setbit(var rop: MPInteger; bit_index: valuint);
{ Return the size of op measured in number of limbs }
function z_size(var op: MPInteger): sizeuint;
{ Return the size of op measured in number of digits in the given base }
function z_sizeinbase(var op: MPInteger; base: longint): sizeuint;
{ Set rop to trunc(sqrt(op)), the truncated integer part of the square root of op }
procedure z_sqrt(var rop, op: MPInteger);
{ Return trunc(sqrt(op)), the truncated integer part of the square root of op }
function z_sqrt(var op: MPInteger): MPInteger;
{ Set rop1 to trunc(sqrt(op)), like z_sqrt. Set rop2 to the remainder (op − rop1^2), which will be zero if op is a perfect square. }
procedure z_sqrtrem(var rop1, rop2, op: MPInteger);
{ Set rop to op1 − op2 }
procedure z_sub(var rop, op1, op2: MPInteger);
{ Return op1 − op2 }
function z_sub(var op1, op2: MPInteger): MPInteger;
{ Set rop to op1 − op2 }
procedure z_sub_ui(var rop, op1: MPInteger; op2: valuint);
{ Return op1 − op2 }
function z_sub_ui(var op1: MPInteger; op2: valuint): MPInteger;
{ Set rop to op1 − op2 }
procedure z_ui_sub(var rop: MPInteger; op1: valuint; var op2: MPInteger);
{ Return op1 − op2 }
function z_ui_sub(op1: valuint; var op2: MPInteger): MPInteger;
{ Set rop to rop − op1 × op2 }
procedure z_submul(var rop, op1, op2: MPInteger);
{ Set rop to rop − op1 × op2 }
procedure z_submul_ui(var rop, op1: MPInteger; op2: valuint);
{ Swap the values rop1 and rop2 efficiently }
procedure z_swap(var rop1, rop2: MPInteger);
{ Divide n by d, forming a quotient q. Round mode trunc. }
procedure z_tdiv_q(var q, n, d: MPInteger);
{ Divide n by d, forming a return quotient. Round mode trunc. }
function z_tdiv_q(var n, d: MPInteger): MPInteger;
{ Divide n by d, forming a quotient q. d = 2^b. Round mode trunc. }
procedure z_tdiv_q_2exp(var q, n: MPInteger; b: valuint);
{ Divide n by d, forming a return quotient. d = 2^b. Round mode trunc. }
function z_tdiv_q_2exp(var n: MPInteger; b: valuint): MPInteger;
{ Divide n by d, forming a quotient q. Round mode trunc. }
function z_tdiv_q_ui(var q, n: MPInteger; d: valuint): valuint;
{ Divide n by d, forming a quotient q and remainder r. Round mode trunc. }
procedure z_tdiv_qr(var q, r, n, d: MPInteger);
{ Divide n by d, forming a quotient q and remainder r. Round mode trunc. }
function z_tdiv_qr_ui(var q, r, n: MPInteger; d: valuint): valuint;
{ Divide n by d, forming a remainder r. Round mode trunc. }
procedure z_tdiv_r(var r, n, d: MPInteger);
{ Divide n by d, forming a return remainder. Round mode trunc. }
function z_tdiv_r(var n, d: MPInteger): MPInteger;
{ Divide n by d, forming a remainder r. d = 2^b. Round mode trunc. }
procedure z_tdiv_r_2exp(var r, n: MPInteger; b: valuint);
{ Divide n by d, forming a return remainder. d = 2^b. Round mode trunc. }
function z_tdiv_r_2exp(var n: MPInteger; b: valuint): MPInteger;
{ Divide n by d, forming a remainder r. Round mode trunc. }
function z_tdiv_r_ui(var r, n: MPInteger; d: valuint): valuint;
{ Divide n by d. Round mode trunc. }
function z_tdiv_ui(var n: MPInteger; d: valuint): valuint;
{ Test bit bit_index in op and return true or false accordingly }
function z_tstbit(var rop: MPInteger; bit_index: valuint): boolean;
{ Set rop to base^exp. The case 0^0 yields 1 }
procedure z_ui_pow_ui(var rop: MPInteger; base, exp: valuint);
{ Return base^exp. The case 0^0 yields 1 }
function z_ui_pow_ui(base, exp: valuint): MPInteger;
{ Generate a uniformly distributed random integer in the range 0 to 2^n − 1, inclusive }
procedure z_urandomb(var rop: MPInteger; var state: MPRandState; n: valuint);
{ Return a uniformly distributed random integer in the range 0 to 2^n − 1, inclusive }
function z_urandomb(var state: MPRandState; n: valuint): MPInteger;
{ Generate a uniform random integer in the range 0 to n − 1, inclusive }
procedure z_urandomm(var rop: MPInteger; var state: MPRandState; var n: MPInteger);
{ Return a uniform random integer in the range 0 to n − 1, inclusive }
function z_urandomm(var state: MPRandState; var n: MPInteger): MPInteger;
{ Set rop to op1 bitwise exclusive-or op2 }
procedure z_xor(var rop, op1, op2: MPInteger);
{ Retuen op1 bitwise exclusive-or op2 }
function z_xor(var op1, op2: MPInteger): MPInteger;

// ---- Rational Number Functions ----

{ Set rop to the absolute value of op }
procedure q_abs(var rop, op: MPRational);
{ Return absolute value of op }
function q_abs(var op: MPRational): MPRational;
{ Set sum to addend1 + addend2 }
procedure q_add(var sum, addend1, addend2: MPRational);
{ Return addend1 + addend2 }
function q_add(var addend1, addend2: MPRational): MPRational;
{ Remove any factors that are common to the numerator and denominator of op, and make the denominator positive }
procedure q_canonicalize(var op: MPRational);
{ Free the space occupied by rational number }
procedure q_clear(var rational_number: MPRational);
{ Compare op1 and op2. Return a positive value if op1 > op2, zero if op1 = op2, and a negative value if op1 < op2 }
function q_cmp(var op1, op2: MPRational): longint;
{ Compare op1 and num2/den2. Return a positive value if op1 > num2/den2, zero if op1 = num2/den2, and a negative value if op1 < num2/den2 }
function q_cmp_si(var op1: MPRational; num2: valsint; den2: valuint): longint;
{ Compare op1 and num2/den2. Return a positive value if op1 > num2/den2, zero if op1 = num2/den2, and a negative value if op1 < num2/den2 }
function q_cmp_ui(var op1: MPRational; num2, den2: valuint): longint;
{ Set quotient to dividend/divisor }
procedure q_div(var quotient, dividend, divisor: MPRational);
{ Return dividend/divisor }
function q_div(var dividend, divisor: MPRational): MPRational;
{ Set rop to op1/(2^op2) }
procedure q_div_2exp(var rop, op1: MPRational; op2: valuint);
{ Return op1/(2^op2) }
function q_div_2exp(var op1: MPRational; op2: valuint): MPRational;
{ Return true if op1 and op2 are equal, false if they are non-equal }
function q_equal(var op1, op2: MPRational): boolean;
{ Get the numerator of a rational }
procedure q_get_num(var numerator: MPInteger; var rational: MPRational);
{ Return the numerator of a rational }
function q_get_num(var rational: MPRational): MPInteger;
{ Get the denominator of a rational }
procedure q_get_den(var denominator: MPInteger; var rational: MPRational);
{ Return the denominator of a rational }
function q_get_den(var rational: MPRational): MPInteger;
{ Convert op to a double, truncating if necessary (ie. rounding towards zero) }
function q_get_d(var op: MPRational): double;
{ Convert op to a string of digits in base base }
function q_get_str(base: longint; var op: MPRational): string;
{ Convert op to a string of digits in base base. If str is NULL, the result string is allocated using the current allocation function. }
function q_get_str(str: pchar; base: longint; var op: MPRational): pchar;
{ Initialize dest rational and set it to 0/1 }
procedure q_init(out dest_rational: MPRational);
{ Set inverted_number to 1/number }
procedure q_inv(var inverted_number, number: MPRational);
{ Return 1/number }
function q_inv(var number: MPRational): MPRational;
{ Set product to multiplier × multiplicand }
procedure q_mul(var product, multiplier, multiplicand: MPRational);
{ Return multiplier × multiplicand }
function q_mul(var multiplier, multiplicand: MPRational): MPRational;
{ Set rop to op1 × (2^op2) }
procedure q_mul_2exp(var rop, op1: MPRational; op2: valuint);
{ Return op1 × (2^op2) }
function q_mul_2exp(var op1: MPRational; op2: valuint): MPRational;
{ Set negated_operand to −operand }
procedure q_neg(var negated_operand, operand: MPRational);
{ Return −operand }
function q_neg(var operand: MPRational): MPRational;
{ Assign rop from op }
procedure q_set(var rop, op: MPRational);
{ Set rop to the value of op. There is no rounding, this conversion is exact. }
procedure q_set_d(var rop: MPRational; op: double);
{ Set the denominator of a rational }
procedure q_set_den(var rational: MPRational; var denominator: MPInteger);
{ Set rop to the value of op. There is no rounding, this conversion is exact. }
procedure q_set_f(var rop: MPRational; var op: MPFloat);
{ Set the numerator of a rational }
procedure q_set_num(var rational: MPRational; var numerator: MPInteger);
{ Set the value of rop to op1/op2 }
procedure q_set_si(var rop: MPRational; op1: valsint; op2: valuint);
{ Set rop from a null-terminated string str in the given base. The return value is true if the entire string is a valid number. }
function q_set_str(var rop: MPRational; str: string; base: longint): boolean;
{ Set the value of rop to op1/op2 }
procedure q_set_ui(var rop: MPRational; op1, op2: valuint);
{ Assign rop from op }
procedure q_set_z(var rop: MPRational; var op: MPInteger);
{ Set difference to minuend − subtrahend }
procedure q_sub(var difference, minuend, subtrahend: MPRational);
{ Return minuend − subtrahend }
function q_sub(var minuend, subtrahend: MPRational): MPRational;
{ Swap the values rop1 and rop2 efficiently }
procedure q_swap(var rop1, rop2: MPRational);

// ---- Floating-point Functions ----

{ Set rop to the absolute value of op }
procedure f_abs(var rop, op: MPFloat);
{ Return the absolute value of op }
function f_abs(var op: MPFloat): MPFloat;
{ Set rop to op1 + op2 }
procedure f_add(var rop, op1, op2: MPFloat);
{ Return op1 + op2 }
function f_add(var op1, op2: MPFloat): MPFloat;
{ Set rop to op1 + op2 }
procedure f_add_ui(var rop, op1: MPFloat; op2: valuint);
{ Return op1 + op2 }
function f_add_ui(var op1: MPFloat; op2: valuint): MPFloat;
{ Set rop to op rounded to the next higher integer }
procedure f_ceil(var rop, op: MPFloat);
{ Return op rounded to the next higher integer }
function f_ceil(var op: MPFloat): MPFloat;
{ Free the space occupied by x }
procedure f_clear(var x: MPFloat);
{ Compare op1 and op2. Return a positive value if op1 > op2, zero if op1 = op2, and a negative value if op1 < op2. }
function f_cmp(var op1, op2: MPFloat): longint;
{ Compare op1 and op2. Return a positive value if op1 > op2, zero if op1 = op2, and a negative value if op1 < op2. }
function f_cmp_d(var op1: MPFloat; op2: double): longint;
{ Compare op1 and op2. Return a positive value if op1 > op2, zero if op1 = op2, and a negative value if op1 < op2. }
function f_cmp_si(var op1: MPFloat; op2: valsint): longint;
{ Compare op1 and op2. Return a positive value if op1 > op2, zero if op1 = op2, and a negative value if op1 < op2. }
function f_cmp_ui(var op1: MPFloat; op2: valuint): longint;
{ Set rop to op1/op2 }
procedure f_div(var rop, op1, op2: MPFloat);
{ Return op1/op2 }
function f_div(var op1, op2: MPFloat): MPFloat;
{ Set rop to op1/(2^op2) }
procedure f_div_2exp(var rop, op1: MPFloat; op2: valuint);
{ Return op1/(2^op2) }
function f_div_2exp(var op1: MPFloat; op2: valuint): MPFloat;
{ Set rop to op1/op2 }
procedure f_div_ui(var rop, op1: MPFloat; op2: valuint);
{ Return op1/op2 }
function f_div_ui(var op1: MPFloat; op2: valuint): MPFloat;
{ Return true if the first op3 bits of op1 and op2 are equal, false otherwise }
function f_eq(var op1, op2: MPFloat; op3: valuint): boolean;
{ Return true if op would fit in the respective C data type, when truncated to an integer }
function f_fits_sint_p(var op: MPFloat): boolean;
{ Return true if op would fit in the respective C data type, when truncated to an integer }
function f_fits_slong_p(var op: MPFloat): boolean;
{ Return true if op would fit in the respective C data type, when truncated to an integer }
function f_fits_sshort_p(var op: MPFloat): boolean;
{ Return true if op would fit in the respective C data type, when truncated to an integer }
function f_fits_uint_p(var op: MPFloat): boolean;
{ Return true if op would fit in the respective C data type, when truncated to an integer }
function f_fits_ulong_p(var op: MPFloat): boolean;
{ Return true if op would fit in the respective C data type, when truncated to an integer }
function f_fits_ushort_p(var op: MPFloat): boolean;
{ Set rop to op rounded to the next lower }
procedure f_floor(var rop, op: MPFloat);
{ Return op rounded to the next lower }
function f_floor(var op: MPFloat): MPFloat;
{ Convert op to a double, truncating if necessary (ie. rounding towards zero) }
function f_get_d(var op: MPFloat): double;
{ Convert op to a double, truncating if necessary (ie. rounding towards zero), and with an exponent returned separately }
function f_get_d_2exp(out exp: valsint; var op: MPFloat): double;
{ Return the default precision actually used }
function f_get_default_prec: valuint;
{ Return the current precision of op, in bits }
function f_get_prec(var op: MPFloat): valuint;
{ Convert op to a long, truncating any fraction part }
function f_get_si(var op: MPFloat): valsint;
{ Convert op to a string of digits in base base }
function f_get_str(out exp: mp_exp_t; base: longint; ndigits: sizeuint; var op: MPFloat): string;
{ Convert op to a string of digits in base base. If str is NULL, the result string is allocated using the current allocation function. }
function f_get_str(str: pchar; out exp: mp_exp_t; base: longint; ndigits: sizeuint; var op: MPFloat): pchar;
{ Convert op to a unsigned long, truncating any fraction part }
function f_get_ui(var op: MPFloat): valuint;
{ Initialize x to 0 }
procedure f_init(out x: MPFloat);
{ Initialize x to 0 and set its precision to be at least prec bits }
procedure f_init2(out x: MPFloat; prec: valuint);
{ Initialize rop and set its value from op }
procedure f_init_set(out rop: MPFloat; var op: MPFloat);
{ Initialize rop and set its value from op }
procedure f_init_set_d(out rop: MPFloat; op: double);
{ Initialize rop and set its value from op }
procedure f_init_set_si(out rop: MPFloat; op: valsint);
{ Initialize rop and set its value from the string in str. Returns true if the entire string is a valid number in base base. }
function f_init_set_str(out rop: MPFloat; str: string; base: longint): boolean;
{ Initialize rop and set its value from op }
procedure f_init_set_ui(out rop: MPFloat; op: valuint);
{ Return true if op is an integer }
function f_integer_p(var op: MPFloat): boolean;
{ Set rop to op1 × op2 }
procedure f_mul(var rop, op1, op2: MPFloat);
{ Return op1 × op2 }
function f_mul(var op1, op2: MPFloat): MPFloat;
{ Set rop to op1 × (2^op2) }
procedure f_mul_2exp(var rop, op1: MPFloat; op2: valuint);
{ Return op1 × (2^op2) }
function f_mul_2exp(var op1: MPFloat; op2: valuint): MPFloat;
{ Set rop to op1 × op2 }
procedure f_mul_ui(var rop, op1: MPFloat; op2: valuint);
{ Return op1 × op2 }
function f_mul_ui(var op1: MPFloat; op2: valuint): MPFloat;
{ Set rop to −op }
procedure f_neg(var rop, op: MPFloat);
{ Return −op }
function f_neg(var op: MPFloat): MPFloat;
{ Set rop to op1^op2 }
procedure f_pow_ui(var rop, op1: MPFloat; op2: valuint);
{ Return op1^op2 }
function f_pow_ui(var op1: MPFloat; op2: valuint): MPFloat;
{ Generate a random float of at most max_size limbs, with long strings of zeros and ones in the binary representation }
procedure f_random2(var rop: MPFloat; max_size: mp_size_t; exp: mp_exp_t);
{ Return a random float of at most max_size limbs, with long strings of zeros and ones in the binary representation }
function f_random2(max_size: mp_size_t; exp: mp_exp_t): MPFloat;
{ Compute the relative difference between op1 and op2 and store the result in rop }
procedure f_reldiff(var rop, op1, op2: MPFloat);
{ Return the relative difference between op1 and op2 }
function f_reldiff(var op1, op2: MPFloat): MPFloat;
{ Set the value of rop from op }
procedure f_set(var rop, op: MPFloat);
{ Set the value of rop from op }
procedure f_set_d(var rop: MPFloat; op: double);
{ Set the default precision to be at least prec bits }
procedure f_set_default_prec(prec: valuint);
{ Set the precision of rop to be at least prec bits }
procedure f_set_prec(var rop: MPFloat; prec: valuint);
{ Set the precision of rop to be at least prec bits, without changing the memory allocated }
procedure f_set_prec_raw(var rop: MPFloat; prec: valuint);
{ Set the value of rop from op }
procedure f_set_q(var rop: MPFloat; var op: MPRational);
{ Set the value of rop from op }
procedure f_set_si(var rop: MPFloat; op: valsint);
{ Set the value of rop from the string in str. Returns true if the entire string is a valid number in base base. }
function f_set_str(var rop: MPFloat; str: string; base: longint): boolean;
{ Set the value of rop from op }
procedure f_set_ui(var rop: MPFloat; op: valuint);
{ Set the value of rop from op }
procedure f_set_z(var rop: MPFloat; var op: MPInteger);
{ Set rop to op^(1/2) }
procedure f_sqrt(var rop, op: MPFloat);
{ Return op^(1/2) }
function f_sqrt(var op: MPFloat): MPFloat;
{ Set rop to op^(1/2) }
procedure f_sqrt_ui(var rop: MPFloat; op: valuint);
{ Return op^(1/2) }
function f_sqrt_ui(op: valuint): MPFloat;
{ Set rop to op1 − op2 }
procedure f_sub(var rop, op1, op2: MPFloat);
{ Return op1 − op2 }
function f_sub(var op1, op2: MPFloat): MPFloat;
{ Set rop to op1 − op2 }
procedure f_sub_ui(var rop, op1: MPFloat; op2: valuint);
{ Return op1 − op2 }
function f_sub_ui(var op1: MPFloat; op2: valuint): MPFloat;
{ Swap rop1 and rop2 efficiently }
procedure f_swap(var rop1, rop2: MPFloat);
{ Set rop to op rounded to the integer towards zero }
procedure f_trunc(var rop, op: MPFloat);
{ Return op rounded to the integer towards zero }
function f_trunc(var op: MPFloat): MPFloat;
{ Set rop to op1/op2 }
procedure f_ui_div(var rop: MPFloat; op1: valuint; var op2: MPFloat);
{ Return op1/op2 }
function f_ui_div(op1: valuint; var op2: MPFloat): MPFloat;
{ Set rop to op1 − op2 }
procedure f_ui_sub(var rop: MPFloat; op1: valuint; var op2: MPFloat);
{ Return op1 − op2 }
function f_ui_sub(op1: valuint; var op2: MPFloat): MPFloat;
{ Generate a uniformly distributed random float in rop, such that 0 <= rop < 1, with nbits significant bits in the mantissa }
procedure f_urandomb(var rop: MPFloat; var state: MPRandState; nbits: valuint);
{ Return a uniformly distributed random float, such that 0 <= result < 1, with nbits significant bits in the mantissa }
function f_urandomb(var state: MPRandState; nbits: valuint): MPFloat;

// ---- operators ----

operator * (op1: MPFloat; op2: MPFloat): MPFloat; inline;
operator * (op1: MPInteger; op2: MPInteger): MPInteger; inline;
operator * (op1: MPRational; op2: MPRational): MPRational; inline;
operator ** (op1: MPFloat; op2: valuint): MPFloat; inline;
operator ** (op1: MPInteger; op2: valuint): MPInteger; inline;
operator + (op1: MPFloat; op2: MPFloat): MPFloat; inline;
operator + (op1: MPInteger; op2: MPInteger): MPInteger; inline;
operator + (op1: MPRational; op2: MPRational): MPRational; inline;
operator - (op: MPFloat): MPFloat; inline;
operator - (op: MPInteger): MPInteger; inline;
operator - (op: MPRational): MPRational; inline;
operator - (op1: MPFloat; op2: MPFloat): MPFloat; inline;
operator - (op1: MPInteger; op2: MPInteger): MPInteger; inline;
operator - (op1: MPRational; op2: MPRational): MPRational; inline;
operator / (op1: MPFloat; op2: MPFloat): MPFloat; inline;
operator / (op1: MPInteger; op2: MPInteger): MPInteger; inline;
operator / (op1: MPRational; op2: MPRational): MPRational; inline;
operator := (op: double): MPFloat; inline;
operator := (op: double): MPInteger; inline;
operator := (op: double): MPRational; inline;
operator := (op: MPFloat): Cardinal; inline;
operator := (op: MPFloat): double; inline;
operator := (op: MPFloat): integer; inline;
operator := (op: MPFloat): mpf_t; inline;
operator := (op: MPFloat): MPInteger; inline;
operator := (op: MPFloat): MPRational; inline;
operator := (op: MPFloat): string; inline;
{$ifdef CPU64}
operator := (op: MPFloat): valsint; inline;
operator := (op: MPFloat): valuint; inline;
{$endif}
operator := (var op: mpf_t): MPFloat; inline;
operator := (op: MPInteger): cardinal; inline;
operator := (op: MPInteger): double; inline;
operator := (op: MPInteger): integer; inline;
operator := (op: MPInteger): MPFloat; inline;
operator := (op: MPInteger): MPRational; inline;
operator := (op: MPInteger): mpz_t; inline;
operator := (op: MPInteger): string; inline;
{$ifdef CPU64}
operator := (op: MPInteger): valsint; inline;
operator := (op: MPInteger): valuint; inline;
{$endif}
operator := (var op: mpq_t): MPRational; inline;
operator := (op: MPRandState): randstate_t; inline;
operator := (op: MPRational): double; inline;
operator := (op: MPRational): MPFloat; inline;
operator := (op: MPRational): MPInteger; inline;
operator := (op: MPRational): mpq_t; inline;
operator := (op: MPRational): string; inline;
operator := (var op: mpz_t): MPInteger; inline;
operator := (var op: randstate_t): MPRandState; inline;
operator := (op: string): MPFloat; inline;
operator := (op: string): MPInteger; inline;
operator := (op: string): MPRational; inline;
operator := (op: valsint): MPFloat; inline;
operator := (op: valsint): MPInteger; inline;
operator := (op: valsint): MPRational; inline;
operator := (op: valuint): MPFloat; inline;
operator := (op: valuint): MPInteger; inline;
operator := (op: valuint): MPRational; inline;
operator < (op1: MPFloat; op2: MPFloat): boolean; inline;
operator < (op1: MPInteger; op2: MPInteger): boolean; inline;
operator < (op1: MPRational; op2: MPRational): boolean; inline;
operator <= (op1: MPFloat; op2: MPFloat): boolean; inline;
operator <= (op1: MPInteger; op2: MPInteger): boolean; inline;
operator <= (op1: MPRational; op2: MPRational): boolean; inline;
operator > (op1: MPFloat; op2: MPFloat): boolean; inline;
operator > (op1: MPInteger; op2: MPInteger): boolean; inline;
operator > (op1: MPRational; op2: MPRational): boolean; inline;
operator >= (op1: MPFloat; op2: MPFloat): boolean; inline;
operator >= (op1: MPInteger; op2: MPInteger): boolean; inline;
operator >= (op1: MPRational; op2: MPRational): boolean; inline;
// compiler doesn't like theese
// operator = (op1: MPFloat; op2: MPFloat): boolean; inline;
// operator = (op1: MPInteger; op2: MPInteger): boolean; inline;
// operator = (op1: MPRational; op2: MPRational): boolean; inline;

implementation

uses
  math;

{$ifndef NO_GMP_GLOBVARS}
var
  __gmp_bits_per_limb: longint; cvar; external;
  __gmp_errno: longint; cvar; external;
  __gmp_version: pchar; cvar; external;

function bits_per_limb: longint;
begin
  result := __gmp_bits_per_limb;
end;

function errno: longint;
begin
  result := __gmp_errno;
end;

function version: string;
begin
  result := __gmp_version;
end;

{$else NO_GMP_GLOBVARS}
function bits_per_limb: longint;
const BITS_PER_BYTE = 8;
begin
  result := sizeof(mp_limb_t) * BITS_PER_BYTE;
end;

function errno: longint;
begin
  result := 0;
end;

function version: string;
const NO_VER = '0.0.0';
begin
  result := NO_VER;
end;
{$endif NO_GMP_GLOBVARS}

// ---- ext types ----

{ TMPBase }

function TMPBase.refs: longint;
begin
  result := frefcount;
end;

{ TMPInteger }

function TMPInteger.ptr: mpz_ptr;
begin
  result := @fmpz
end;

destructor TMPInteger.destroy;
begin
  mpz_clear(fmpz);
  inherited destroy;
end;

{ TMPFloat }

function TMPFloat.ptr: mpf_ptr;
begin
  result := @fmpf;
end;

destructor TMPFloat.destroy;
begin
  mpf_clear(fmpf);
  inherited destroy;
end;

{ TMPRational }

function TMPRational.ptr: mpq_ptr;
begin
  result := @fmpq;
end;

destructor TMPRational.destroy;
begin
  mpq_clear(fmpq);
  inherited destroy;
end;

{ TMPRandState }

function TMPRandState.ptr: randstate_ptr;
begin
  result := @frandstate;
end;

destructor TMPRandState.destroy;
begin
  mp_randclear(frandstate);
  inherited destroy;
end;

// --- helpers ----

function dest(var rop: MPInteger): mpz_ptr;
begin
  if (not assigned(rop)) or (rop.refs > 1) then
    z_init(rop);
  result := rop.ptr;
end;

function dest(var rop: MPFloat): mpf_ptr;
begin
  if (not assigned(rop)) or (rop.refs > 1) then
    f_init(rop);
  result := rop.ptr;
end;

function dest(var rop: MPRational): mpq_ptr;
begin
  if (not assigned(rop)) or (rop.refs > 1) then
    q_init(rop);
  result := rop.ptr;
end;

function dest(var rop: MPRandState): randstate_ptr;
begin
  if (not assigned(rop)) or (rop.refs > 1) then
    randinit_default(rop);
  result := rop.ptr;
end;

function src(var rop: MPInteger): mpz_ptr;
begin
  if not assigned(rop) then
    z_init(rop);
  result := rop.ptr;
end;

function src(var rop: MPFloat): mpf_ptr;
begin
  if not assigned(rop) then
    f_init(rop);
  result := rop.ptr;
end;

function src(var rop: MPRational): mpq_ptr;
begin
  if not assigned(rop) then
    q_init(rop);
  result := rop.ptr;
end;

function src(var rop: MPRandState): randstate_ptr;
begin
  if not assigned(rop) then
    randinit_default(rop);
  result := rop.ptr;
end;

procedure propagate_prec(var result, op: MPFloat);
begin
  f_set_prec(result, f_get_prec(op));
end;

procedure propagate_prec(var result, op1, op2: MPFloat);
begin
  f_set_prec(result, max(valsint(f_get_prec(op1)), f_get_prec(op2)));
end;

// --- ext bindings ----

procedure randinit_default(out state: MPRandState);
begin
  state := TMPRandState.Create;
  mp_randinit_default(state.ptr^);
end;

procedure randinit_lc_2exp(out state: MPRandState; var a: MPInteger; c, m2exp: valuint);
begin
  state := TMPRandState.Create;
  mp_randinit_lc_2exp(state.ptr^, src(a)^, c, m2exp);
end;

function randinit_lc_2exp_size(out state: MPRandState; size: sizeuint): boolean;
begin
  state := TMPRandState.Create;
  result := mp_randinit_lc_2exp_size(state.ptr^, size) <> 0;
end;

procedure randinit_mt(out state: MPRandState);
begin
  state := TMPRandState.Create;
  mp_randinit_mt(state.ptr^);
end;

procedure randinit_set(out rop: MPRandState; var op: MPRandState);
begin
  rop := TMPRandState.Create;
  mp_randinit_set(rop.ptr^, src(op)^);
end;

procedure randseed(var state: MPRandState; var seed: MPInteger);
begin
  mp_randseed(dest(state)^, src(seed)^);
end;

procedure randseed_ui(var state: MPRandState; seed: valuint);
begin
  mp_randseed_ui(dest(state)^, seed);
end;

procedure randclear(var state: MPRandState);
begin
  state := nil;
end;

function urandomb_ui(var state: MPRandState; n: valuint): valuint;
begin
  result := mp_urandomb_ui(dest(state)^, n);
end;

function urandomm_ui(var state: MPRandState; n: valuint): valuint;
begin
  result := mp_urandomb_ui(dest(state)^, n);
end;

function z_realloc(var integer_: MPInteger; new_alloc: mp_size_t): pointer;
begin
  result := mpz_realloc(dest(integer_)^, new_alloc);
end;

procedure z_abs(var rop, op: MPInteger);
begin
  mpz_abs(dest(rop)^, src(op)^);
end;

function z_abs(var op: MPInteger): MPInteger;
begin
  mpz_abs(dest(result)^, src(op)^);
end;

procedure z_add(var rop, op1, op2: MPInteger);
begin
  mpz_add(dest(rop)^, src(op1)^, src(op2)^);
end;

function z_add(var op1, op2: MPInteger): MPInteger;
begin
  mpz_add(dest(result)^, src(op1)^, src(op2)^);
end;

procedure z_add_ui(var rop, op1: MPInteger; op2: valuint);
begin
  mpz_add_ui(dest(rop)^, src(op1)^, op2);
end;

function z_add_ui(var op1: MPInteger; op2: valuint): MPInteger;
begin
  mpz_add_ui(dest(result)^, src(op1)^, op2);
end;

procedure z_addmul(var rop, op1, op2: MPInteger);
begin
  mpz_addmul(dest(rop)^, src(op1)^, src(op2)^);
end;

procedure z_addmul_ui(var rop, op1: MPInteger; op2: valuint);
begin
  mpz_addmul_ui(dest(rop)^, src(op1)^, op2);
end;

procedure z_and(var rop, op1, op2: MPInteger);
begin
  mpz_and(dest(rop)^, src(op1)^, src(op2)^);
end;

function z_and(var op1, op2: MPInteger): MPInteger;
begin
  mpz_and(dest(result)^, src(op1)^, src(op2)^);
end;

procedure z_bin_ui(var rop, n: MPInteger; k: valuint);
begin
  mpz_bin_ui(dest(rop)^, src(n)^, k);
end;

function z_bin_ui(var n: MPInteger; k: valuint): MPInteger;
begin
  mpz_bin_ui(dest(result)^, src(n)^, k);
end;

procedure z_bin_uiui(var rop: MPInteger; n, k: valuint);
begin
  mpz_bin_uiui(dest(rop)^, n, k);
end;

function z_bin_uiui(n, k: valuint): MPInteger;
begin
  mpz_bin_uiui(dest(result)^, n, k);
end;

procedure z_cdiv_q(var q, n, d: MPInteger);
begin
  mpz_cdiv_q(dest(q)^, src(n)^, src(d)^);
end;

function z_cdiv_q(var n, d: MPInteger): MPInteger;
begin
  mpz_cdiv_q(dest(result)^, src(n)^, src(d)^);
end;

procedure z_cdiv_q_2exp(var q, n: MPInteger; b: valuint);
begin
  mpz_cdiv_q_2exp(dest(q)^, src(n)^, b);
end;

function z_cdiv_q_2exp(var n: MPInteger; b: valuint): MPInteger;
begin
  mpz_cdiv_q_2exp(dest(result)^, src(n)^, b);
end;

function z_cdiv_q_ui(var q, n: MPInteger; d: valuint): valuint;
begin
  result := mpz_cdiv_q_ui(dest(q)^, src(n)^, d);
end;

procedure z_cdiv_qr(var q, r, n, d: MPInteger);
begin
  mpz_cdiv_qr(dest(q)^, dest(r)^, src(n)^, src(d)^);
end;

function z_cdiv_qr_ui(var q, r, n: MPInteger; d: valuint): valuint;
begin
  result := mpz_cdiv_qr_ui(dest(q)^, dest(r)^, src(n)^, d);
end;

procedure z_cdiv_r(var r, n, d: MPInteger);
begin
  mpz_cdiv_r(dest(r)^, src(n)^, src(d)^);
end;

function z_cdiv_r(var n, d: MPInteger): MPInteger;
begin
  mpz_cdiv_r(dest(result)^, src(n)^, src(d)^);
end;

procedure z_cdiv_r_2exp(var r, n: MPInteger; b: valuint);
begin
  mpz_cdiv_r_2exp(dest(r)^, src(n)^, b);
end;

function z_cdiv_r_2exp(var n: MPInteger; b: valuint): MPInteger;
begin
  mpz_cdiv_r_2exp(dest(result)^, src(n)^, b);
end;

function z_cdiv_r_ui(var r, n: MPInteger; d: valuint): valuint;
begin
  result := mpz_cdiv_r_ui(dest(r)^, src(n)^, d);
end;

function z_cdiv_ui(var n: MPInteger; d: valuint): valuint;
begin
  result := mpz_cdiv_ui(src(n)^, d);
end;

procedure z_clear(var integer_: MPInteger);
begin
  integer_ := nil;
end;

procedure z_clrbit(var rop: MPInteger; bit_index: valuint);
begin
  mpz_clrbit(dest(rop)^, bit_index);
end;

function z_cmp(var op1, op2: MPInteger): longint;
begin
  result := mpz_cmp(src(op1)^, src(op2)^);
end;

function z_cmp_d(var op1: MPInteger; op2: double): longint;
begin
  result := mpz_cmp_d(src(op1)^, op2);
end;

function z_cmp_si(var op1: MPInteger; op2: valsint): longint;
begin
  result := mpz_cmp_si(src(op1)^, op2);
end;

function z_cmp_ui(var op1: MPInteger; op2: valuint): longint;
begin
  result := mpz_cmp_ui(src(op1)^, op2);
end;

function z_cmpabs(var op1, op2: MPInteger): longint;
begin
  result := mpz_cmpabs(src(op1)^, src(op2)^);
end;

function z_cmpabs_d(var op1: MPInteger; op2: double): longint;
begin
  result := mpz_cmpabs_d(src(op1)^, op2);
end;

function z_cmpabs_ui(var op1: MPInteger; op2: valuint): longint;
begin
  result := mpz_cmpabs_ui(src(op1)^, op2);
end;

procedure z_com(var rop, op: MPInteger);
begin
  mpz_com(dest(rop)^, src(op)^);
end;

function z_com(var op: MPInteger): MPInteger;
begin
  mpz_com(dest(result)^, src(op)^);
end;

procedure z_combit(var rop: MPInteger; bit_index: valuint);
begin
  mpz_combit(dest(rop)^, bit_index);
end;

function z_congruent_p(var n, c, d: MPInteger): boolean;
begin
  result := mpz_congruent_p(src(n)^, src(c)^, src(d)^) <> 0;
end;

function z_congruent_2exp_p(var n, c: MPInteger; b: valuint): boolean;
begin
  result := mpz_congruent_2exp_p(src(n)^, src(c)^, b) <> 0;
end;

function z_congruent_ui_p(var n: MPInteger; c, d: valuint): boolean;
begin
  result := mpz_congruent_ui_p(src(n)^, c, d) <> 0;
end;

procedure z_divexact(var q, n, d: MPInteger);
begin
  mpz_divexact(dest(q)^, src(n)^, src(d)^);
end;

function z_divexact(var n, d: MPInteger): MPInteger;
begin
  mpz_divexact(dest(result)^, src(n)^, src(d)^);
end;

procedure z_divexact_ui(var q, n: MPInteger; d: valuint);
begin
  mpz_divexact_ui(dest(q)^, src(n)^, d);
end;

function z_divexact_ui(var n: MPInteger; d: valuint): MPInteger;
begin
  mpz_divexact_ui(dest(result)^, src(n)^, d);
end;

function z_divisible_p(var n, d: MPInteger): boolean;
begin
  result := mpz_divisible_p(src(n)^, src(d)^) <> 0;
end;

function z_divisible_ui_p(var n: MPInteger; d: valuint): boolean;
begin
  result := mpz_divisible_ui_p(src(n)^, d) <> 0;
end;

function z_divisible_2exp_p(var n: MPInteger; b: valuint): boolean;
begin
  result := mpz_divisible_2exp_p(src(n)^, b) <> 0;
end;

function z_export(out buf; out countp: sizeuint; order: longint; size: sizeuint; endian: longint; nails: sizeuint; var op: MPInteger): pointer;
begin
  result := mpz_export(buf, countp, order, size, endian, nails, src(op)^);
end;

procedure z_fac_ui(var rop: MPInteger; op: valuint);
begin
  mpz_fac_ui(dest(rop)^, op);
end;

function z_fac_ui(op: valuint): MPInteger;
begin
  mpz_fac_ui(dest(result)^, op);
end;

procedure z_fdiv_q(var q, n, d: MPInteger);
begin
  mpz_fdiv_q(dest(q)^, src(n)^, src(d)^);
end;

function z_fdiv_q(var n, d: MPInteger): MPInteger;
begin
  mpz_fdiv_q(dest(result)^, src(n)^, src(d)^);
end;

procedure z_fdiv_q_2exp(var q, n: MPInteger; b: valuint);
begin
  mpz_fdiv_q_2exp(dest(q)^, src(n)^, b);
end;

function z_fdiv_q_2exp(var n: MPInteger; b: valuint): MPInteger;
begin
  mpz_fdiv_q_2exp(dest(result)^, src(n)^, b);
end;

function z_fdiv_q_ui(var q, n: MPInteger; d: valuint): valuint;
begin
  result := mpz_fdiv_q_ui(dest(q)^, src(n)^, d);
end;

procedure z_fdiv_qr(var q, r, n, d: MPInteger);
begin
  mpz_fdiv_qr(dest(q)^, dest(r)^, src(n)^, src(d)^);
end;

function z_fdiv_qr_ui(var q, r, n: MPInteger; d: valuint): valuint;
begin
  result := mpz_fdiv_qr_ui(dest(q)^, dest(r)^, src(n)^, d);
end;

procedure z_fdiv_r(var r, n, d: MPInteger);
begin
  mpz_fdiv_r(dest(r)^, src(n)^, src(d)^);
end;

function z_fdiv_r(var n, d: MPInteger): MPInteger;
begin
  mpz_fdiv_r(dest(result)^, src(n)^, src(d)^);
end;

procedure z_fdiv_r_2exp(var r, n: MPInteger; b: valuint);
begin
  mpz_fdiv_r_2exp(dest(r)^, src(n)^, b);
end;

function z_fdiv_r_2exp(var n: MPInteger; b: valuint): MPInteger;
begin
  mpz_fdiv_r_2exp(dest(result)^, src(n)^, b);
end;

function z_fdiv_r_ui(var r, n: MPInteger; d: valuint): valuint;
begin
  result := mpz_fdiv_r_ui(dest(r)^, src(n)^, d);
end;

function z_fdiv_ui(var n: MPInteger; d: valuint): valuint;
begin
  result := mpz_fdiv_ui(src(n)^, d);
end;

procedure z_fib_ui(var fn: MPInteger; n: valuint);
begin
  mpz_fib_ui(dest(fn)^, n);
end;

function z_fib_ui(n: valuint): MPInteger;
begin
  mpz_fib_ui(dest(result)^, n);
end;

procedure z_fib2_ui(var fn, fnsub1: MPInteger; n: valuint);
begin
  mpz_fib2_ui(dest(fn)^, dest(fnsub1)^, n);
end;

function z_fib2_ui(var fnsub1: MPInteger; n: valuint): MPInteger;
begin
  mpz_fib2_ui(dest(result)^, dest(fnsub1)^, n);
end;

function z_fits_sint_p(var op: MPInteger): boolean;
begin
  result := mpz_fits_sint_p(src(op)^) <> 0;
end;

function z_fits_slong_p(var op: MPInteger): boolean;
begin
  result := mpz_fits_slong_p(src(op)^) <> 0;
end;

function z_fits_sshort_p(var op: MPInteger): boolean;
begin
  result := mpz_fits_sshort_p(src(op)^) <> 0;
end;

function z_fits_uint_p(var op: MPInteger): boolean;
begin
  result := mpz_fits_uint_p(src(op)^) <> 0;
end;

function z_fits_ulong_p(var op: MPInteger): boolean;
begin
  result := mpz_fits_ulong_p(src(op)^) <> 0;
end;

function z_fits_ushort_p(var op: MPInteger): boolean;
begin
  result := mpz_fits_ushort_p(src(op)^) <> 0;
end;

procedure z_gcd(var rop, op1, op2: MPInteger);
begin
  mpz_gcd(dest(rop)^, src(op1)^, src(op2)^);
end;

function z_gcd(var op1, op2: MPInteger): MPInteger;
begin
  mpz_gcd(dest(result)^, src(op1)^, src(op2)^);
end;

function z_gcd_ui(var rop, op1: MPInteger; op2: valuint): valuint;
begin
  result := mpz_gcd_ui(dest(rop)^, src(op1)^, op2)
end;

procedure z_gcdext(var g, s, t, a, b: MPInteger);
begin
  mpz_gcdext(dest(g)^, dest(s)^, dest(t)^, src(a)^, src(b)^);
end;

function z_get_d(var op: MPInteger): double;
begin
  result := mpz_get_d(src(op)^);
end;

function z_get_d_2exp(out exp: valsint; var op: MPInteger): double;
begin
  result := mpz_get_d_2exp(exp, src(op)^);
end;

function z_get_si(op: MPInteger): valsint;
begin
  result := mpz_get_si(src(op)^);
end;

function z_get_str(base: longint; var op: MPInteger): string;
var p: pchar;
begin
  p := mpz_get_str(nil, base, src(op)^);
  result := p;
  freemem(p);
end;

function z_get_str(str: pchar; base: longint; var op: MPInteger): pchar;
begin
  result := mpz_get_str(str, base, src(op)^);
end;

function z_get_ui(op: MPInteger): valuint;
begin
  result := mpz_get_ui(src(op)^);
end;

function z_getlimbn(var op: MPInteger; n: mp_size_t): mp_limb_t;
begin
  result := mpz_getlimbn(src(op)^, n);
end;

function z_hamdist(var op1, op2: MPInteger): valuint;
begin
  result := mpz_hamdist(src(op1)^, src(op2)^);
end;

procedure z_import(var rop: MPInteger; count: sizeuint; order: longint; size: sizeuint; endian: longint; nails: sizeuint; const op);
begin
  mpz_import(dest(rop)^, count, order, size, endian, nails, op);
end;

procedure z_init(out integer_: MPInteger);
begin
  integer_ := TMPInteger.Create;
  mpz_init(integer_.ptr^);
end;

procedure z_init2(out integer_: MPInteger; n: valuint);
begin
  integer_ := TMPInteger.Create;
  mpz_init2(integer_.ptr^, n);
end;

procedure z_init_set(out rop: MPInteger; var op: MPInteger);
begin
  rop := TMPInteger.Create;
  mpz_init_set(rop.ptr^, src(op)^);
end;

procedure z_init_set_d(out rop: MPInteger; op: double);
begin
  rop := TMPInteger.Create;
  mpz_init_set_d(rop.ptr^, op);
end;

procedure z_init_set_si(out rop: MPInteger; op: valsint);
begin
  rop := TMPInteger.Create;
  mpz_init_set_si(rop.ptr^, op);
end;

function z_init_set_str(out rop: MPInteger; str: string; base: longint): boolean;
begin
  rop := TMPInteger.Create;
  result := mpz_set_str(rop.ptr^, pchar(str), base) = 0;
end;

procedure z_init_set_ui(out rop: MPInteger; op: valuint);
begin
  rop := TMPInteger.Create;
  mpz_init_set_ui(rop.ptr^, op);
end;

function z_invert(var rop, op1, op2: MPInteger): longint;
begin
  result := mpz_invert(dest(rop)^, src(op1)^, src(op2)^);
end;

procedure z_ior(var rop, op1, op2: MPInteger);
begin
  mpz_ior(dest(rop)^, src(op1)^, src(op2)^);
end;

function z_ior(var op1, op2: MPInteger): MPInteger;
begin
  mpz_ior(dest(result)^, src(op1)^, src(op2)^);
end;

function z_jacobi(var a, b: MPInteger): longint;
begin
  result := mpz_jacobi(src(a)^, src(b)^);
end;

function z_kronecker_si(var a: MPInteger; b: valsint): longint;
begin
  result := mpz_kronecker_si(src(a)^, b);
end;

function z_kronecker_ui(var a: MPInteger; b: valuint): longint;
begin
  result := mpz_kronecker_ui(src(a)^, b);
end;

function z_si_kronecker(a: valsint; var b: MPInteger): longint;
begin
  result := mpz_si_kronecker(a, src(b)^);
end;

function z_ui_kronecker(a: valuint; var b: MPInteger): longint;
begin
  result := mpz_ui_kronecker(a, src(b)^);
end;

procedure z_lcm(var rop, op1, op2: MPInteger);
begin
  mpz_lcm(dest(rop)^, src(op1)^, src(op2)^);
end;

function z_lcm(var op1, op2: MPInteger): MPInteger;
begin
  mpz_lcm(dest(result)^, src(op1)^, src(op2)^);
end;

procedure z_lcm_ui(var rop, op1: MPInteger; op2: valuint);
begin
  mpz_lcm_ui(dest(rop)^, src(op1)^, op2);
end;

function z_lcm_ui(var op1: MPInteger; op2: valuint): MPInteger;
begin
  mpz_lcm_ui(dest(result)^, src(op1)^, op2);
end;

procedure z_lucnum_ui(var ln: MPInteger; n: valuint);
begin
  mpz_lucnum_ui(dest(ln)^, n);
end;

function z_lucnum_ui(n: valuint): MPInteger;
begin
  mpz_lucnum_ui(dest(result)^, n);
end;

procedure z_lucnum2_ui(var ln, lnsub1: MPInteger; n: valuint);
begin
  mpz_lucnum2_ui(dest(ln)^, dest(lnsub1)^, n);
end;

function z_lucnum2_ui(var lnsub1: MPInteger; n: valuint): MPInteger;
begin
  mpz_lucnum2_ui(dest(result)^, dest(lnsub1)^, n);
end;

procedure z_mod(var r, n, d: MPInteger);
begin
  mpz_mod(dest(r)^, src(n)^, src(d)^);
end;

function z_mod(var n, d: MPInteger): MPInteger;
begin
  mpz_mod(dest(result)^, src(n)^, src(d)^);
end;

procedure z_mul(var rop, op1, op2: MPInteger);
begin
  mpz_mul(dest(rop)^, src(op1)^, src(op2)^);
end;

function z_mul(var op1, op2: MPInteger): MPInteger;
begin
  mpz_mul(dest(result)^, src(op1)^, src(op2)^);
end;

procedure z_mul_2exp(var rop, op1: MPInteger; op2: valuint);
begin
  mpz_mul_2exp(dest(rop)^, src(op1)^, op2);
end;

function z_mul_2exp(var op1: MPInteger; op2: valuint): MPInteger;
begin
  mpz_mul_2exp(dest(result)^, src(op1)^, op2);
end;

procedure z_mul_si(var rop, op1: MPInteger; op2: valsint);
begin
  mpz_mul_si(dest(rop)^, src(op1)^, op2);
end;

function z_mul_si(var op1: MPInteger; op2: valsint): MPInteger;
begin
  mpz_mul_si(dest(result)^, src(op1)^, op2);
end;

procedure z_mul_ui(var rop, op1: MPInteger; op2: valuint);
begin
  mpz_mul_ui(dest(rop)^, src(op1)^, op2);
end;

function z_mul_ui(var op1: MPInteger; op2: valuint): MPInteger;
begin
  mpz_mul_ui(dest(result)^, src(op1)^, op2);
end;

procedure z_neg(var rop, op: MPInteger);
begin
  mpz_neg(dest(rop)^, src(op)^);
end;

function z_neg(var op: MPInteger): MPInteger;
begin
  mpz_neg(dest(result)^, src(op)^);
end;

procedure z_nextprime(var rop, op: MPInteger);
begin
  mpz_nextprime(dest(rop)^, src(op)^);
end;

function z_nextprime(var op: MPInteger): MPInteger;
begin
  mpz_nextprime(dest(result)^, src(op)^);
end;

function z_perfect_power_p(var op: MPInteger): boolean;
begin
  result := mpz_perfect_power_p(src(op)^) <> 0;
end;

function z_perfect_square_p(var op: MPInteger): boolean;
begin
  result := mpz_perfect_square_p(src(op)^) <> 0;
end;

function z_popcount(var op: MPInteger): valuint;
begin
  result := mpz_popcount(src(op)^);
end;

procedure z_pow_ui(var rop, base: MPInteger; exp: valuint);
begin
  mpz_pow_ui(dest(rop)^, src(base)^, exp);
end;

function z_pow_ui(var base: MPInteger; exp: valuint): MPInteger;
begin
  mpz_pow_ui(dest(result)^, src(base)^, exp);
end;

procedure z_powm(var rop, base, exp, mod_: MPInteger);
begin
  mpz_powm(dest(rop)^, src(base)^, src(exp)^, src(mod_)^);
end;

function z_powm(var base, exp, mod_: MPInteger): MPInteger;
begin
  mpz_powm(dest(result)^, src(base)^, src(exp)^, src(mod_)^);
end;

procedure z_powm_ui(var rop, base: MPInteger; exp: valuint; var mod_: MPInteger);
begin
  mpz_powm_ui(dest(rop)^, src(base)^, exp, src(mod_)^);
end;

function z_powm_ui(var base: MPInteger; exp: valuint; var mod_: MPInteger): MPInteger;
begin
  mpz_powm_ui(dest(result)^, src(base)^, exp, src(mod_)^);
end;

function z_probab_prime_p(var n: MPInteger; reps: longint): longint;
begin
  result := mpz_probab_prime_p(src(n)^, reps);
end;

procedure z_realloc2(var integer_: MPInteger; n: valuint);
begin
  mpz_realloc2(dest(integer_)^, n);
end;

function z_remove(var rop, op, f: MPInteger): valuint;
begin
  result := mpz_remove(dest(rop)^, src(op)^, src(f)^);
end;

function z_root(var rop, op: MPInteger; n: valuint): boolean;
begin
  result := mpz_root(dest(rop)^, src(op)^, n) <> 0;
end;

procedure z_rootrem(var root, rem, u: MPInteger; n: valuint);
begin
  mpz_rootrem(dest(root)^, dest(rem)^, src(u)^, n);
end;

procedure z_rrandomb(var rop: MPInteger; var state: MPRandState; n: valuint);
begin
  mpz_rrandomb(dest(rop)^, src(state)^, n);
end;

function z_rrandomb(var state: MPRandState; n: valuint): MPInteger;
begin
  mpz_rrandomb(dest(result)^, src(state)^, n);
end;

function z_scan0(var op: MPInteger; starting_bit: valuint): valuint;
begin
  result := mpz_scan0(src(op)^, starting_bit);
end;

function z_scan1(var op: MPInteger; starting_bit: valuint): valuint;
begin
  result := mpz_scan1(src(op)^, starting_bit);
end;

procedure z_set(var rop, op: MPInteger);
begin
  mpz_set(dest(rop)^, src(op)^);
end;

procedure z_set_d(var rop: MPInteger; op: double);
begin
  mpz_set_d(dest(rop)^, op);
end;

procedure z_set_f(var rop: MPInteger; var op: MPFloat);
begin
  mpz_set_f(dest(rop)^, src(op)^);
end;

procedure z_set_q(var rop: MPInteger; var op: MPRational);
begin
  mpz_set_q(dest(rop)^, src(op)^);
end;

procedure z_set_si(var rop: MPInteger; op: valsint);
begin
  mpz_set_si(dest(rop)^, op);
end;

function z_set_str(var rop: MPInteger; str: string; base: longint): boolean;
begin
  result := mpz_set_str(dest(rop)^, pchar(str), base) = 0;
end;

procedure z_set_ui(var rop: MPInteger; op: valuint);
begin
  mpz_set_ui(dest(rop)^, op);
end;

procedure z_setbit(var rop: MPInteger; bit_index: valuint);
begin
  mpz_setbit(dest(rop)^, bit_index);
end;

function z_size(var op: MPInteger): sizeuint;
begin
  result := mpz_size(src(op)^);
end;

function z_sizeinbase(var op: MPInteger; base: longint): sizeuint;
begin
  result := mpz_sizeinbase(src(op)^, base);
end;

procedure z_sqrt(var rop, op: MPInteger);
begin
  mpz_sqrt(dest(rop)^, src(op)^);
end;

function z_sqrt(var op: MPInteger): MPInteger;
begin
  mpz_sqrt(dest(result)^, src(op)^);
end;

procedure z_sqrtrem(var rop1, rop2, op: MPInteger);
begin
  mpz_sqrtrem(dest(rop1)^, dest(rop2)^, src(op)^);
end;

procedure z_sub(var rop, op1, op2: MPInteger);
begin
  mpz_sub(dest(rop)^, src(op1)^, src(op2)^);
end;

function z_sub(var op1, op2: MPInteger): MPInteger;
begin
  mpz_sub(dest(result)^, src(op1)^, src(op2)^);
end;

procedure z_sub_ui(var rop, op1: MPInteger; op2: valuint);
begin
  mpz_sub_ui(dest(rop)^, src(op1)^, op2);
end;

function z_sub_ui(var op1: MPInteger; op2: valuint): MPInteger;
begin
  mpz_sub_ui(dest(result)^, src(op1)^, op2);
end;

procedure z_ui_sub(var rop: MPInteger; op1: valuint; var op2: MPInteger);
begin
  mpz_ui_sub(dest(rop)^, op1, src(op2)^);
end;

function z_ui_sub(op1: valuint; var op2: MPInteger): MPInteger;
begin
  mpz_ui_sub(dest(result)^, op1, src(op2)^);
end;

procedure z_submul(var rop, op1, op2: MPInteger);
begin
  mpz_submul(dest(rop)^, src(op1)^, src(op2)^);
end;

procedure z_submul_ui(var rop, op1: MPInteger; op2: valuint);
begin
  mpz_submul_ui(dest(rop)^, src(op1)^, op2);
end;

procedure z_swap(var rop1, rop2: MPInteger);
begin
  mpz_swap(dest(rop1)^, dest(rop2)^);
end;

procedure z_tdiv_q(var q, n, d: MPInteger);
begin
  mpz_tdiv_q(dest(q)^, src(n)^, src(d)^);
end;

function z_tdiv_q(var n, d: MPInteger): MPInteger;
begin
  mpz_tdiv_q(dest(result)^, src(n)^, src(d)^);
end;

procedure z_tdiv_q_2exp(var q, n: MPInteger; b: valuint);
begin
  mpz_tdiv_q_2exp(dest(q)^, src(n)^, b);
end;

function z_tdiv_q_2exp(var n: MPInteger; b: valuint): MPInteger;
begin
  mpz_tdiv_q_2exp(dest(result)^, src(n)^, b);
end;

function z_tdiv_q_ui(var q, n: MPInteger; d: valuint): valuint;
begin
  result := mpz_tdiv_q_ui(dest(q)^, src(n)^, d);
end;

procedure z_tdiv_qr(var q, r, n, d: MPInteger);
begin
  mpz_tdiv_qr(dest(q)^, dest(r)^, src(n)^, src(d)^);
end;

function z_tdiv_qr_ui(var q, r, n: MPInteger; d: valuint): valuint;
begin
  result := mpz_tdiv_qr_ui(dest(q)^, dest(r)^, src(n)^, d);
end;

procedure z_tdiv_r(var r, n, d: MPInteger);
begin
  mpz_tdiv_r(dest(r)^, src(n)^, src(d)^);
end;

function z_tdiv_r(var n, d: MPInteger): MPInteger;
begin
  mpz_tdiv_r(dest(result)^, src(n)^, src(d)^);
end;

procedure z_tdiv_r_2exp(var r, n: MPInteger; b: valuint);
begin
  mpz_tdiv_r_2exp(dest(r)^, src(n)^, b);
end;

function z_tdiv_r_2exp(var n: MPInteger; b: valuint): MPInteger;
begin
  mpz_tdiv_r_2exp(dest(result)^, src(n)^, b);
end;

function z_tdiv_r_ui(var r, n: MPInteger; d: valuint): valuint;
begin
  result := mpz_tdiv_r_ui(dest(r)^, src(n)^, d);
end;

function z_tdiv_ui(var n: MPInteger; d: valuint): valuint;
begin
  result := mpz_tdiv_ui(src(n)^, d);
end;

function z_tstbit(var rop: MPInteger; bit_index: valuint): boolean;
begin
  result := mpz_tstbit(src(rop)^, bit_index) <> 0;
end;

procedure z_ui_pow_ui(var rop: MPInteger; base, exp: valuint);
begin
  mpz_ui_pow_ui(dest(rop)^, base, exp);
end;

function z_ui_pow_ui(base, exp: valuint): MPInteger;
begin
  mpz_ui_pow_ui(dest(result)^, base, exp);
end;

procedure z_urandomb(var rop: MPInteger; var state: MPRandState; n: valuint);
begin
  mpz_urandomb(dest(rop)^, src(state)^, n);
end;

function z_urandomb(var state: MPRandState; n: valuint): MPInteger;
begin
  mpz_urandomb(dest(result)^, src(state)^, n);
end;

procedure z_urandomm(var rop: MPInteger; var state: MPRandState; var n: MPInteger);
begin
  mpz_urandomm(dest(rop)^, src(state)^, src(n)^);
end;

function z_urandomm(var state: MPRandState; var n: MPInteger): MPInteger;
begin
  mpz_urandomm(dest(result)^, src(state)^, src(n)^);
end;

procedure z_xor(var rop, op1, op2: MPInteger);
begin
  mpz_xor(dest(rop)^, src(op1)^, src(op2)^);
end;

function z_xor(var op1, op2: MPInteger): MPInteger;
begin
  mpz_xor(dest(result)^, src(op1)^, src(op2)^);
end;

procedure q_abs(var rop, op: MPRational);
begin
  mpq_abs(dest(rop)^, src(op)^);
end;

function q_abs(var op: MPRational): MPRational;
begin
  mpq_abs(dest(result)^, src(op)^);
end;

procedure q_add(var sum, addend1, addend2: MPRational);
begin
  mpq_add(dest(sum)^, src(addend1)^, src(addend2)^);
end;

function q_add(var addend1, addend2: MPRational): MPRational;
begin
  mpq_add(dest(result)^, src(addend1)^, src(addend2)^);
end;

procedure q_canonicalize(var op: MPRational);
begin
  mpq_canonicalize(dest(op)^);
end;

procedure q_clear(var rational_number: MPRational);
begin
  rational_number := nil;
end;

function q_cmp(var op1, op2: MPRational): longint;
begin
  result := mpq_cmp(src(op1)^, src(op2)^);
end;

function q_cmp_si(var op1: MPRational; num2: valsint; den2: valuint): longint;
begin
  result := mpq_cmp_si(src(op1)^, num2, den2);
end;

function q_cmp_ui(var op1: MPRational; num2, den2: valuint): longint;
begin
  result := mpq_cmp_ui(src(op1)^, num2, den2);
end;

procedure q_div(var quotient, dividend, divisor: MPRational);
begin
  mpq_div(dest(quotient)^, src(dividend)^, src(divisor)^);
end;

function q_div(var dividend, divisor: MPRational): MPRational;
begin
  mpq_div(dest(result)^, src(dividend)^, src(divisor)^);
end;

procedure q_div_2exp(var rop, op1: MPRational; op2: valuint);
begin
  mpq_div_2exp(dest(rop)^, src(op1)^, op2);
end;

function q_div_2exp(var op1: MPRational; op2: valuint): MPRational;
begin
  mpq_div_2exp(dest(result)^, src(op1)^, op2);
end;

function q_equal(var op1, op2: MPRational): boolean;
begin
  result := mpq_equal(src(op1)^, src(op2)^) <> 0;
end;

procedure q_get_num(var numerator: MPInteger; var rational: MPRational);
begin
  mpq_get_num(dest(numerator)^, src(rational)^);
end;

function q_get_num(var rational: MPRational): MPInteger;
begin
  mpq_get_num(dest(result)^, src(rational)^);
end;

procedure q_get_den(var denominator: MPInteger; var rational: MPRational);
begin
  mpq_get_den(dest(denominator)^, src(rational)^);
end;

function q_get_den(var rational: MPRational): MPInteger;
begin
  mpq_get_den(dest(result)^, src(rational)^);
end;

function q_get_d(var op: MPRational): double;
begin
  result := mpq_get_d(src(op)^);
end;

function q_get_str(base: longint; var op: MPRational): string;
var p: pchar;
begin
  p := mpq_get_str(nil, base, src(op)^);
  result := p;
  freemem(p);
end;

function q_get_str(str: pchar; base: longint; var op: MPRational): pchar;
begin
  result := mpq_get_str(str, base, src(op)^);
end;

procedure q_init(out dest_rational: MPRational);
begin
  dest_rational := TMPRational.Create;
  mpq_init(dest_rational.ptr^);
end;

procedure q_inv(var inverted_number, number: MPRational);
begin
  mpq_inv(dest(inverted_number)^, src(number)^);
end;

function q_inv(var number: MPRational): MPRational;
begin
  mpq_inv(dest(result)^, src(number)^);
end;

procedure q_mul(var product, multiplier, multiplicand: MPRational);
begin
  mpq_mul(dest(product)^, src(multiplier)^, src(multiplicand)^);
end;

function q_mul(var multiplier, multiplicand: MPRational): MPRational;
begin
  mpq_mul(dest(result)^, src(multiplier)^, src(multiplicand)^);
end;

procedure q_mul_2exp(var rop, op1: MPRational; op2: valuint);
begin
  mpq_mul_2exp(dest(rop)^, src(op1)^, op2);
end;

function q_mul_2exp(var op1: MPRational; op2: valuint): MPRational;
begin
  mpq_mul_2exp(dest(result)^, src(op1)^, op2);
end;

procedure q_neg(var negated_operand, operand: MPRational);
begin
  mpq_neg(dest(negated_operand)^, src(operand)^);
end;

function q_neg(var operand: MPRational): MPRational;
begin
  mpq_neg(dest(result)^, src(operand)^);
end;

procedure q_set(var rop, op: MPRational);
begin
  mpq_set(dest(rop)^, src(op)^);
end;

procedure q_set_d(var rop: MPRational; op: double);
begin
  mpq_set_d(dest(rop)^, op);
end;

procedure q_set_den(var rational: MPRational; var denominator: MPInteger);
begin
  mpq_set_den(dest(rational)^, src(denominator)^);
end;

procedure q_set_f(var rop: MPRational; var op: MPFloat);
begin
  mpq_set_f(dest(rop)^, src(op)^);
end;

procedure q_set_num(var rational: MPRational; var numerator: MPInteger);
begin
  mpq_set_num(dest(rational)^, src(numerator)^);
end;

procedure q_set_si(var rop: MPRational; op1: valsint; op2: valuint);
begin
  mpq_set_si(dest(rop)^, op1, op2);
end;

function q_set_str(var rop: MPRational; str: string; base: longint): boolean;
begin
  result := mpq_set_str(dest(rop)^, pchar(str), base) = 0;
end;

procedure q_set_ui(var rop: MPRational; op1, op2: valuint);
begin
  mpq_set_ui(dest(rop)^, op1, op2);
end;

procedure q_set_z(var rop: MPRational; var op: MPInteger);
begin
  mpq_set_z(dest(rop)^, src(op)^);
end;

procedure q_sub(var difference, minuend, subtrahend: MPRational);
begin
  mpq_sub(dest(difference)^, src(minuend)^, src(subtrahend)^);
end;

function q_sub(var minuend, subtrahend: MPRational): MPRational;
begin
  mpq_sub(dest(result)^, src(minuend)^, src(subtrahend)^);
end;

procedure q_swap(var rop1, rop2: MPRational);
begin
  mpq_swap(dest(rop1)^, dest(rop2)^);
end;

procedure f_abs(var rop, op: MPFloat);
begin
  mpf_abs(dest(rop)^, src(op)^);
end;

function f_abs(var op: MPFloat): MPFloat;
begin
  mpf_abs(dest(result)^, src(op)^);
end;

procedure f_add(var rop, op1, op2: MPFloat);
begin
  mpf_add(dest(rop)^, src(op1)^, src(op2)^);
end;

function f_add(var op1, op2: MPFloat): MPFloat;
begin
  mpf_add(dest(result)^, src(op1)^, src(op2)^);
end;

procedure f_add_ui(var rop, op1: MPFloat; op2: valuint);
begin
  mpf_add_ui(dest(rop)^, src(op1)^, op2);
end;

function f_add_ui(var op1: MPFloat; op2: valuint): MPFloat;
begin
  mpf_add_ui(dest(result)^, src(op1)^, op2);
end;

procedure f_ceil(var rop, op: MPFloat);
begin
  mpf_ceil(dest(rop)^, src(op)^);
end;

function f_ceil(var op: MPFloat): MPFloat;
begin
  mpf_ceil(dest(result)^, src(op)^);
end;

procedure f_clear(var x: MPFloat);
begin
  x := nil;
end;

function f_cmp(var op1, op2: MPFloat): longint;
begin
  result := mpf_cmp(src(op1)^, src(op2)^);
end;

function f_cmp_d(var op1: MPFloat; op2: double): longint;
begin
  result := mpf_cmp_d(src(op1)^, op2);
end;

function f_cmp_si(var op1: MPFloat; op2: valsint): longint;
begin
  result := mpf_cmp_si(src(op1)^, op2);
end;

function f_cmp_ui(var op1: MPFloat; op2: valuint): longint;
begin
  result := mpf_cmp_ui(src(op1)^, op2);
end;

procedure f_div(var rop, op1, op2: MPFloat);
begin
  mpf_div(dest(rop)^, src(op1)^, src(op2)^);
end;

function f_div(var op1, op2: MPFloat): MPFloat;
begin
  mpf_div(dest(result)^, src(op1)^, src(op2)^);
end;

procedure f_div_2exp(var rop, op1: MPFloat; op2: valuint);
begin
  mpf_div_2exp(dest(rop)^, src(op1)^, op2);
end;

function f_div_2exp(var op1: MPFloat; op2: valuint): MPFloat;
begin
  mpf_div_2exp(dest(result)^, src(op1)^, op2);
end;

procedure f_div_ui(var rop, op1: MPFloat; op2: valuint);
begin
  mpf_div_ui(dest(rop)^, src(op1)^, op2);
end;

function f_div_ui(var op1: MPFloat; op2: valuint): MPFloat;
begin
  mpf_div_ui(dest(result)^, src(op1)^, op2);
end;

function f_eq(var op1, op2: MPFloat; op3: valuint): boolean;
begin
  result := mpf_eq(src(op1)^, src(op2)^, op3) <> 0;
end;

function f_fits_sint_p(var op: MPFloat): boolean;
begin
  result := mpf_fits_sint_p(src(op)^) <> 0;
end;

function f_fits_slong_p(var op: MPFloat): boolean;
begin
  result := mpf_fits_slong_p(src(op)^) <> 0;
end;

function f_fits_sshort_p(var op: MPFloat): boolean;
begin
  result := mpf_fits_sshort_p(src(op)^) <> 0;
end;

function f_fits_uint_p(var op: MPFloat): boolean;
begin
  result := mpf_fits_uint_p(src(op)^) <> 0;
end;

function f_fits_ulong_p(var op: MPFloat): boolean;
begin
  result := mpf_fits_ulong_p(src(op)^) <> 0;
end;

function f_fits_ushort_p(var op: MPFloat): boolean;
begin
  result := mpf_fits_ushort_p(src(op)^) <> 0;
end;

procedure f_floor(var rop, op: MPFloat);
begin
  mpf_floor(dest(rop)^, src(op)^);
end;

function f_floor(var op: MPFloat): MPFloat;
begin
  mpf_floor(dest(result)^, src(op)^);
end;

function f_get_d(var op: MPFloat): double;
begin
  result := mpf_get_d(src(op)^);
end;

function f_get_d_2exp(out exp: valsint; var op: MPFloat): double;
begin
  result := mpf_get_d_2exp(exp, src(op)^);
end;

function f_get_default_prec: valuint;
begin
  result := mpf_get_default_prec;
end;

function f_get_prec(var op: MPFloat): valuint;
begin
  result := mpf_get_prec(src(op)^);
end;

function f_get_si(var op: MPFloat): valsint;
begin
  result := mpf_get_si(src(op)^);
end;

function f_get_str(out exp: mp_exp_t; base: longint; ndigits: sizeuint; var op: MPFloat): string;
var p: pchar;
begin
  p := mpf_get_str(nil, exp, base, ndigits, src(op)^);
  result := p;
  freemem(p);
end;

function f_get_str(str: pchar; out exp: mp_exp_t; base: longint; ndigits: sizeuint; var op: MPFloat): pchar;
begin
  result := mpf_get_str(str, exp, base, ndigits, src(op)^);
end;

function f_get_ui(var op: MPFloat): valuint;
begin
  result := mpf_get_ui(src(op)^);
end;

procedure f_init(out x: MPFloat);
begin
  x := TMPFloat.Create;
  mpf_init(x.ptr^);
end;

procedure f_init2(out x: MPFloat; prec: valuint);
begin
  x := TMPFloat.Create;
  mpf_init2(x.ptr^, prec);
end;

procedure f_init_set(out rop: MPFloat; var op: MPFloat);
begin
  rop := TMPFloat.Create;
  mpf_init_set(rop.ptr^, src(op)^);
end;

procedure f_init_set_d(out rop: MPFloat; op: double);
begin
  rop := TMPFloat.Create;
  mpf_init_set_d(rop.ptr^, op);
end;

procedure f_init_set_si(out rop: MPFloat; op: valsint);
begin
  rop := TMPFloat.Create;
  mpf_init_set_si(rop.ptr^, op);
end;

function f_init_set_str(out rop: MPFloat; str: string; base: longint): boolean;
begin
  rop := TMPFloat.Create;
  result := mpf_init_set_str(rop.ptr^, pchar(str), base) = 0;
end;

procedure f_init_set_ui(out rop: MPFloat; op: valuint);
begin
  rop := TMPFloat.Create;
  mpf_init_set_ui(rop.ptr^, op);
end;

function f_integer_p(var op: MPFloat): boolean;
begin
  result := mpf_integer_p(src(op)^) <> 0;
end;

procedure f_mul(var rop, op1, op2: MPFloat);
begin
  mpf_mul(dest(rop)^, src(op1)^, src(op2)^);
end;

function f_mul(var op1, op2: MPFloat): MPFloat;
begin
  mpf_mul(dest(result)^, src(op1)^, src(op2)^);
end;

procedure f_mul_2exp(var rop, op1: MPFloat; op2: valuint);
begin
  mpf_mul_2exp(dest(rop)^, src(op1)^, op2);
end;

function f_mul_2exp(var op1: MPFloat; op2: valuint): MPFloat;
begin
  mpf_mul_2exp(dest(result)^, src(op1)^, op2);
end;

procedure f_mul_ui(var rop, op1: MPFloat; op2: valuint);
begin
  mpf_mul_ui(dest(rop)^, src(op1)^, op2);
end;

function f_mul_ui(var op1: MPFloat; op2: valuint): MPFloat;
begin
  mpf_mul_ui(dest(result)^, src(op1)^, op2);
end;

procedure f_neg(var rop, op: MPFloat);
begin
  mpf_neg(dest(rop)^, src(op)^);
end;

function f_neg(var op: MPFloat): MPFloat;
begin
  mpf_neg(dest(result)^, src(op)^);
end;

procedure f_pow_ui(var rop, op1: MPFloat; op2: valuint);
begin
  mpf_pow_ui(dest(rop)^, src(op1)^, op2);
end;

function f_pow_ui(var op1: MPFloat; op2: valuint): MPFloat;
begin
  mpf_pow_ui(dest(result)^, src(op1)^, op2);
end;

procedure f_random2(var rop: MPFloat; max_size: mp_size_t; exp: mp_exp_t);
begin
  mpf_random2(dest(rop)^, max_size, exp);
end;

function f_random2(max_size: mp_size_t; exp: mp_exp_t): MPFloat;
begin
  mpf_random2(dest(result)^, max_size, exp);
end;

procedure f_reldiff(var rop, op1, op2: MPFloat);
begin
  mpf_reldiff(dest(rop)^, src(op1)^, src(op2)^);
end;

function f_reldiff(var op1, op2: MPFloat): MPFloat;
begin
  mpf_reldiff(dest(result)^, src(op1)^, src(op2)^);
end;

procedure f_set(var rop, op: MPFloat);
begin
  mpf_set(dest(rop)^, src(op)^);
end;

procedure f_set_d(var rop: MPFloat; op: double);
begin
  mpf_set_d(dest(rop)^, op);
end;

procedure f_set_default_prec(prec: valuint);
begin
  mpf_set_default_prec(prec);
end;

procedure f_set_prec(var rop: MPFloat; prec: valuint);
begin
  mpf_set_prec(dest(rop)^, prec);
end;

procedure f_set_prec_raw(var rop: MPFloat; prec: valuint);
begin
  mpf_set_prec_raw(dest(rop)^, prec);
end;

procedure f_set_q(var rop: MPFloat; var op: MPRational);
begin
  mpf_set_q(dest(rop)^, src(op)^);
end;

procedure f_set_si(var rop: MPFloat; op: valsint);
begin
  mpf_set_si(dest(rop)^, op);
end;

function f_set_str(var rop: MPFloat; str: string; base: longint): boolean;
begin
  result := mpf_set_str(dest(rop)^, pchar(str), base) = 0;
end;

procedure f_set_ui(var rop: MPFloat; op: valuint);
begin
  mpf_set_ui(dest(rop)^, op);
end;

procedure f_set_z(var rop: MPFloat; var op: MPInteger);
begin
  mpf_set_z(dest(rop)^, src(op)^);
end;

procedure f_sqrt(var rop, op: MPFloat);
begin
  mpf_sqrt(dest(rop)^, src(op)^);
end;

function f_sqrt(var op: MPFloat): MPFloat;
begin
  mpf_sqrt(dest(result)^, src(op)^);
end;

procedure f_sqrt_ui(var rop: MPFloat; op: valuint);
begin
  mpf_sqrt_ui(dest(rop)^, op);
end;

function f_sqrt_ui(op: valuint): MPFloat;
begin
  mpf_sqrt_ui(dest(result)^, op);
end;

procedure f_sub(var rop, op1, op2: MPFloat);
begin
  mpf_sub(dest(rop)^, src(op1)^, src(op2)^);
end;

function f_sub(var op1, op2: MPFloat): MPFloat;
begin
  mpf_sub(dest(result)^, src(op1)^, src(op2)^);
end;

procedure f_sub_ui(var rop, op1: MPFloat; op2: valuint);
begin
  mpf_sub_ui(dest(rop)^, src(op1)^, op2);
end;

function f_sub_ui(var op1: MPFloat; op2: valuint): MPFloat;
begin
  mpf_sub_ui(dest(result)^, src(op1)^, op2);
end;

procedure f_swap(var rop1, rop2: MPFloat);
begin
  mpf_swap(dest(rop1)^, dest(rop2)^);
end;

procedure f_trunc(var rop, op: MPFloat);
begin
  mpf_trunc(dest(rop)^, src(op)^);
end;

function f_trunc(var op: MPFloat): MPFloat;
begin
  mpf_trunc(dest(result)^, src(op)^);
end;

procedure f_ui_div(var rop: MPFloat; op1: valuint; var op2: MPFloat);
begin
  mpf_ui_div(dest(rop)^, op1, src(op2)^);
end;

function f_ui_div(op1: valuint; var op2: MPFloat): MPFloat;
begin
  mpf_ui_div(dest(result)^, op1, src(op2)^);
end;

procedure f_ui_sub(var rop: MPFloat; op1: valuint; var op2: MPFloat);
begin
  mpf_ui_sub(dest(rop)^, op1, src(op2)^);
end;

function f_ui_sub(op1: valuint; var op2: MPFloat): MPFloat;
begin
  mpf_ui_sub(dest(result)^, op1, src(op2)^);
end;

procedure f_urandomb(var rop: MPFloat; var state: MPRandState; nbits: valuint);
begin
  mpf_urandomb(dest(rop)^, src(state)^, nbits);
end;

function f_urandomb(var state: MPRandState; nbits: valuint): MPFloat;
begin
  mpf_urandomb(dest(result)^, src(state)^, nbits);
end;

// ---- operators ----

operator * (op1: MPFloat; op2: MPFloat): MPFloat;
begin
  propagate_prec(result, op1, op2);
  f_mul(result, op1, op2);
end;

operator * (op1: MPInteger; op2: MPInteger): MPInteger;
begin
  z_mul(result, op1, op2);
end;

operator * (op1: MPRational; op2: MPRational): MPRational;
begin
  q_mul(result, op1, op2);
end;

operator ** (op1: MPFloat; op2: valuint): MPFloat;
begin
  propagate_prec(result, op1);
  f_pow_ui(result, op1, op2);
end;

operator ** (op1: MPInteger; op2: valuint): MPInteger;
begin
  z_pow_ui(result, op1, op2);
end;

operator + (op1: MPFloat; op2: MPFloat): MPFloat;
begin
  propagate_prec(result, op1, op2);
  f_add(result, op1, op2);
end;

operator + (op1: MPInteger; op2: MPInteger): MPInteger;
begin
  z_add(result, op1, op2);
end;

operator + (op1: MPRational; op2: MPRational): MPRational;
begin
  q_add(result, op1, op2);
end;

operator - (op: MPFloat): MPFloat;
begin
  propagate_prec(result, op);
  f_neg(result, op);
end;

operator - (op: MPInteger): MPInteger;
begin
  z_neg(result, op);
end;

operator - (op: MPRational): MPRational;
begin
  q_neg(result, op);
end;

operator - (op1: MPFloat; op2: MPFloat): MPFloat;
begin
  propagate_prec(result, op1, op2);
  f_sub(result, op1, op2);
end;

operator - (op1: MPInteger; op2: MPInteger): MPInteger;
begin
  z_sub(result, op1, op2);
end;

operator - (op1: MPRational; op2: MPRational): MPRational;
begin
  q_sub(result, op1, op2);
end;

operator / (op1: MPFloat; op2: MPFloat): MPFloat;
begin
  propagate_prec(result, op1, op2);
  f_div(result, op1, op2);
end;

operator / (op1: MPInteger; op2: MPInteger): MPInteger;
begin
  z_tdiv_q(result, op1, op2);
end;

operator / (op1: MPRational; op2: MPRational): MPRational;
begin
  q_div(result, op1, op2);
end;

operator := (op: double): MPFloat;
begin
  f_set_d(result, op);
end;

operator := (op: double): MPInteger;
begin
  z_set_d(result, op);
end;

operator := (op: double): MPRational;
begin
  q_set_d(result, op);
end;

operator := (op: MPFloat): cardinal;
begin
  result := f_get_ui(op);
end;

operator := (op: MPFloat): double;
begin
  result := f_get_d(op);
end;

operator := (op: MPFloat): integer;
begin
  result := f_get_si(op);
end;

operator := (op: MPFloat): mpf_t;
begin
  mpf_init_set(result, src(op)^);
end;

operator := (op: MPFloat): MPInteger;
begin
  z_set_f(result, op);
end;

operator := (op: MPFloat): MPRational;
begin
  q_set_f(result, op);
end;

operator := (op: MPFloat): string;
const FMT = '%.*Fg';
var p: pchar;
begin
  mp_asprintf(p, FMT, floor(f_get_prec(op) * LOG_10_2), src(op));
  result := p;
  freemem(p);
end;

{$ifdef CPU64}
operator := (op: MPFloat): valsint;
begin
  result := f_get_si(op);
end;

operator := (op: MPFloat): valuint;
begin
  result := f_get_ui(op);
end;
{$endif}

operator := (var op: mpf_t): MPFloat;
begin
  mpf_set(dest(result)^, op);
end;

operator := (op: MPInteger): cardinal;
begin
  result := z_get_ui(op);
end;

operator := (op: MPInteger): double;
begin
  result := z_get_d(op);
end;

operator := (op: MPInteger): integer;
begin
  result := z_get_si(op);
end;

operator := (op: MPInteger): MPFloat;
begin
  f_set_z(result, op);
end;

operator := (op: MPInteger): MPRational;
begin
  q_set_z(result, op);
end;

operator := (op: MPInteger): mpz_t;
begin
  mpz_init_set(result, src(op)^);
end;

operator := (op: MPInteger): string;
const FMT = '%Zd';
var p: pchar;
begin
  mp_asprintf(p, FMT, src(op));
  result := p;
  freemem(p);
end;

{$ifdef CPU64}
operator := (op: MPInteger): valsint;
begin
  result := z_get_si(op);
end;

operator := (op: MPInteger): valuint;
begin
  result := z_get_ui(op);
end;
{$endif}

operator := (var op: mpq_t): MPRational;
begin
  mpq_set(dest(result)^, op);
end;

operator := (op: MPRandState): randstate_t;
begin
  mp_randinit_set(result, src(op)^);
end;

operator := (op: MPRational): double;
begin
  result := q_get_d(op);
end;

operator := (op: MPRational): MPFloat;
begin
  f_set_q(result, op);
end;

operator := (op: MPRational): MPInteger;
begin
  z_set_q(result, op);
end;

operator := (op: MPRational): mpq_t;
begin
  mpq_init(result);
  mpq_set(result, src(op)^);
end;

operator := (op: MPRational): string;
const FMT = '%Qd';
var p: pchar;
begin
  mp_asprintf(p, FMT, src(op));
  result := p;
  freemem(p);
end;

operator := (var op: mpz_t): MPInteger;
begin
  mpz_set(dest(result)^, op);
end;

operator := (var op: randstate_t): MPRandState;
begin
  result := TMPRandState.Create;
  mp_randinit_set(result.ptr^, op);
end;

operator := (op: string): MPFloat;
begin
  f_set_prec(result, ceil(length(op) / LOG_10_2));
  f_set_str(result, op, BASE10);
end;

operator := (op: string): MPInteger;
begin
  z_set_str(result, op, BASE10);
end;

operator := (op: string): MPRational;
begin
  q_set_str(result, op, BASE10);
end;

operator := (op: valsint): MPFloat;
begin
  f_set_si(result, op);
end;

operator := (op: valsint): MPInteger;
begin
  z_set_si(result, op);
end;

operator := (op: valsint): MPRational;
begin
  q_set_si(result, op, 1);
end;

operator := (op: valuint): MPFloat;
begin
  f_set_ui(result, op);
end;

operator := (op: valuint): MPInteger;
begin
  z_set_ui(result, op);
end;

operator := (op: valuint): MPRational;
begin
  q_set_ui(result, op, 1);
end;

operator < (op1: MPFloat; op2: MPFloat): boolean;
begin
  result := f_cmp(op1, op2) < 0;
end;

operator < (op1: MPInteger; op2: MPInteger): boolean;
begin
  result := z_cmp(op1, op2) < 0;
end;

operator < (op1: MPRational; op2: MPRational): boolean;
begin
  result := q_cmp(op1, op2) < 0;
end;

operator <= (op1: MPFloat; op2: MPFloat): boolean;
begin
  result := f_cmp(op1, op2) <= 0;
end;

operator <= (op1: MPInteger; op2: MPInteger): boolean;
begin
  result := z_cmp(op1, op2) <= 0;
end;

operator <= (op1: MPRational; op2: MPRational): boolean;
begin
  result := q_cmp(op1, op2) <= 0;
end;

//operator = (op1: MPFloat; op2: MPFloat): boolean;
//begin
//  result := f_cmp(op1, op2) = 0;
//end;

//operator = (op1: MPInteger; op2: MPInteger): boolean;
//begin
//  result := z_cmp(op1, op2) = 0;
//end;

//operator = (op1: MPRational; op2: MPRational): boolean;
//begin
//  result := q_cmp(op1, op2) = 0;
//end;

operator > (op1: MPFloat; op2: MPFloat): boolean;
begin
  result := f_cmp(op1, op2) > 0;
end;

operator > (op1: MPInteger; op2: MPInteger): boolean;
begin
  result := z_cmp(op1, op2) > 0;
end;

operator > (op1: MPRational; op2: MPRational): boolean;
begin
  result := q_cmp(op1, op2) > 0;
end;

operator >= (op1: MPFloat; op2: MPFloat): boolean;
begin
  result := f_cmp(op1, op2) >= 0;
end;

operator >= (op1: MPInteger; op2: MPInteger): boolean;
begin
  result := z_cmp(op1, op2) >= 0;
end;

operator >= (op1: MPRational; op2: MPRational): boolean;
begin
  result := q_cmp(op1, op2) >= 0;
end;

// ==== init stuff ====

function alloc_func(alloc_size: sizeuint): pointer; cdecl;
begin
  result := getmem(alloc_size);
end;

procedure free_proc(p: pointer; size: sizeuint); cdecl;
begin
  assert(size = size); // hint off
  freemem(p);
end;

function reallocate_func(p: pointer; old_size, new_size: sizeuint): pointer; cdecl;
begin
  assert(old_size = old_size); // hint off
  result := reallocmem(p, new_size);
end;

var r1: mp_limb_t;

initialization
  // prealloc the GMP's global PRNG state, get rid of the (pseudo) mem leak report
  mpn_random(@r1, 1);
  mp_set_memory_functions(@alloc_func, @reallocate_func, @free_proc);

end.