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
|
/* gmpxx.h -- C++ class wrapper for GMP types. -*- C++ -*-
Copyright 2001, 2002, 2003, 2006, 2008 Free Software Foundation, Inc.
This file is part of the GNU MP Library.
The GNU MP Library is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 3 of the License, or (at your
option) any later version.
The GNU MP Library is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
License for more details.
You should have received a copy of the GNU Lesser General Public License
along with the GNU MP Library. If not, see http://www.gnu.org/licenses/. */
/* the C++ compiler must implement the following features:
- member templates
- partial specialization of templates
- namespace support
for g++, this means version 2.91 or higher
for other compilers, I don't know */
#ifdef __GNUC__
#if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 91)
#error gmpxx.h requires g++ version 2.91 (egcs 1.1.2) or higher
#endif
#endif
#ifndef __GMP_PLUSPLUS__
#define __GMP_PLUSPLUS__
#include <iosfwd>
#include <cstring> /* for strlen */
#include <string>
#include <stdexcept>
#include <cfloat>
#include <gmp.h>
/**************** Function objects ****************/
/* Any evaluation of a __gmp_expr ends up calling one of these functions
all intermediate functions being inline, the evaluation should optimize
to a direct call to the relevant function, thus yielding no overhead
over the C interface. */
struct __gmp_unary_plus
{
static void eval(mpz_ptr z, mpz_srcptr w) { mpz_set(z, w); }
static void eval(mpq_ptr q, mpq_srcptr r) { mpq_set(q, r); }
static void eval(mpf_ptr f, mpf_srcptr g) { mpf_set(f, g); }
};
struct __gmp_unary_minus
{
static void eval(mpz_ptr z, mpz_srcptr w) { mpz_neg(z, w); }
static void eval(mpq_ptr q, mpq_srcptr r) { mpq_neg(q, r); }
static void eval(mpf_ptr f, mpf_srcptr g) { mpf_neg(f, g); }
};
struct __gmp_unary_com
{
static void eval(mpz_ptr z, mpz_srcptr w) { mpz_com(z, w); }
};
struct __gmp_binary_plus
{
static void eval(mpz_ptr z, mpz_srcptr w, mpz_srcptr v)
{ mpz_add(z, w, v); }
static void eval(mpz_ptr z, mpz_srcptr w, unsigned long int l)
{ mpz_add_ui(z, w, l); }
static void eval(mpz_ptr z, unsigned long int l, mpz_srcptr w)
{ mpz_add_ui(z, w, l); }
static void eval(mpz_ptr z, mpz_srcptr w, signed long int l)
{
if (l >= 0)
mpz_add_ui(z, w, l);
else
mpz_sub_ui(z, w, -l);
}
static void eval(mpz_ptr z, signed long int l, mpz_srcptr w)
{
if (l >= 0)
mpz_add_ui(z, w, l);
else
mpz_sub_ui(z, w, -l);
}
static void eval(mpz_ptr z, mpz_srcptr w, double d)
{
mpz_t temp;
mpz_init_set_d(temp, d);
mpz_add(z, w, temp);
mpz_clear(temp);
}
static void eval(mpz_ptr z, double d, mpz_srcptr w)
{
mpz_t temp;
mpz_init_set_d(temp, d);
mpz_add(z, temp, w);
mpz_clear(temp);
}
static void eval(mpq_ptr q, mpq_srcptr r, mpq_srcptr s)
{ mpq_add(q, r, s); }
static void eval(mpq_ptr q, mpq_srcptr r, unsigned long int l)
{ mpq_set(q, r); mpz_addmul_ui(mpq_numref(q), mpq_denref(q), l); }
static void eval(mpq_ptr q, unsigned long int l, mpq_srcptr r)
{ mpq_set(q, r); mpz_addmul_ui(mpq_numref(q), mpq_denref(q), l); }
static void eval(mpq_ptr q, mpq_srcptr r, signed long int l)
{
mpq_set(q, r);
if (l >= 0)
mpz_addmul_ui(mpq_numref(q), mpq_denref(q), l);
else
mpz_submul_ui(mpq_numref(q), mpq_denref(q), -l);
}
static void eval(mpq_ptr q, signed long int l, mpq_srcptr r)
{
mpq_set(q, r);
if (l >= 0)
mpz_addmul_ui(mpq_numref(q), mpq_denref(q), l);
else
mpz_submul_ui(mpq_numref(q), mpq_denref(q), -l);
}
static void eval(mpq_ptr q, mpq_srcptr r, double d)
{
mpq_t temp;
mpq_init(temp);
mpq_set_d(temp, d);
mpq_add(q, r, temp);
mpq_clear(temp);
}
static void eval(mpq_ptr q, double d, mpq_srcptr r)
{
mpq_t temp;
mpq_init(temp);
mpq_set_d(temp, d);
mpq_add(q, temp, r);
mpq_clear(temp);
}
static void eval(mpq_ptr q, mpq_srcptr r, mpz_srcptr z)
{ mpq_set(q, r); mpz_addmul(mpq_numref(q), mpq_denref(q), z); }
static void eval(mpq_ptr q, mpz_srcptr z, mpq_srcptr r)
{ mpq_set(q, r); mpz_addmul(mpq_numref(q), mpq_denref(q), z); }
static void eval(mpf_ptr f, mpf_srcptr g, mpf_srcptr h)
{ mpf_add(f, g, h); }
static void eval(mpf_ptr f, mpf_srcptr g, unsigned long int l)
{ mpf_add_ui(f, g, l); }
static void eval(mpf_ptr f, unsigned long int l, mpf_srcptr g)
{ mpf_add_ui(f, g, l); }
static void eval(mpf_ptr f, mpf_srcptr g, signed long int l)
{
if (l >= 0)
mpf_add_ui(f, g, l);
else
mpf_sub_ui(f, g, -l);
}
static void eval(mpf_ptr f, signed long int l, mpf_srcptr g)
{
if (l >= 0)
mpf_add_ui(f, g, l);
else
mpf_sub_ui(f, g, -l);
}
static void eval(mpf_ptr f, mpf_srcptr g, double d)
{
mpf_t temp;
mpf_init2(temp, 8*sizeof(double));
mpf_set_d(temp, d);
mpf_add(f, g, temp);
mpf_clear(temp);
}
static void eval(mpf_ptr f, double d, mpf_srcptr g)
{
mpf_t temp;
mpf_init2(temp, 8*sizeof(double));
mpf_set_d(temp, d);
mpf_add(f, temp, g);
mpf_clear(temp);
}
};
struct __gmp_binary_minus
{
static void eval(mpz_ptr z, mpz_srcptr w, mpz_srcptr v)
{ mpz_sub(z, w, v); }
static void eval(mpz_ptr z, mpz_srcptr w, unsigned long int l)
{ mpz_sub_ui(z, w, l); }
static void eval(mpz_ptr z, unsigned long int l, mpz_srcptr w)
{ mpz_ui_sub(z, l, w); }
static void eval(mpz_ptr z, mpz_srcptr w, signed long int l)
{
if (l >= 0)
mpz_sub_ui(z, w, l);
else
mpz_add_ui(z, w, -l);
}
static void eval(mpz_ptr z, signed long int l, mpz_srcptr w)
{
if (l >= 0)
mpz_ui_sub(z, l, w);
else
{
mpz_add_ui(z, w, -l);
mpz_neg(z, z);
}
}
static void eval(mpz_ptr z, mpz_srcptr w, double d)
{
mpz_t temp;
mpz_init_set_d(temp, d);
mpz_sub(z, w, temp);
mpz_clear(temp);
}
static void eval(mpz_ptr z, double d, mpz_srcptr w)
{
mpz_t temp;
mpz_init_set_d(temp, d);
mpz_sub(z, temp, w);
mpz_clear(temp);
}
static void eval(mpq_ptr q, mpq_srcptr r, mpq_srcptr s)
{ mpq_sub(q, r, s); }
static void eval(mpq_ptr q, mpq_srcptr r, unsigned long int l)
{ mpq_set(q, r); mpz_submul_ui(mpq_numref(q), mpq_denref(q), l); }
static void eval(mpq_ptr q, unsigned long int l, mpq_srcptr r)
{ mpq_neg(q, r); mpz_addmul_ui(mpq_numref(q), mpq_denref(q), l); }
static void eval(mpq_ptr q, mpq_srcptr r, signed long int l)
{
mpq_set(q, r);
if (l >= 0)
mpz_submul_ui(mpq_numref(q), mpq_denref(q), l);
else
mpz_addmul_ui(mpq_numref(q), mpq_denref(q), -l);
}
static void eval(mpq_ptr q, signed long int l, mpq_srcptr r)
{
mpq_neg(q, r);
if (l >= 0)
mpz_addmul_ui(mpq_numref(q), mpq_denref(q), l);
else
mpz_submul_ui(mpq_numref(q), mpq_denref(q), -l);
}
static void eval(mpq_ptr q, mpq_srcptr r, double d)
{
mpq_t temp;
mpq_init(temp);
mpq_set_d(temp, d);
mpq_sub(q, r, temp);
mpq_clear(temp);
}
static void eval(mpq_ptr q, double d, mpq_srcptr r)
{
mpq_t temp;
mpq_init(temp);
mpq_set_d(temp, d);
mpq_sub(q, temp, r);
mpq_clear(temp);
}
static void eval(mpq_ptr q, mpq_srcptr r, mpz_srcptr z)
{ mpq_set(q, r); mpz_submul(mpq_numref(q), mpq_denref(q), z); }
static void eval(mpq_ptr q, mpz_srcptr z, mpq_srcptr r)
{ mpq_neg(q, r); mpz_addmul(mpq_numref(q), mpq_denref(q), z); }
static void eval(mpf_ptr f, mpf_srcptr g, mpf_srcptr h)
{ mpf_sub(f, g, h); }
static void eval(mpf_ptr f, mpf_srcptr g, unsigned long int l)
{ mpf_sub_ui(f, g, l); }
static void eval(mpf_ptr f, unsigned long int l, mpf_srcptr g)
{ mpf_ui_sub(f, l, g); }
static void eval(mpf_ptr f, mpf_srcptr g, signed long int l)
{
if (l >= 0)
mpf_sub_ui(f, g, l);
else
mpf_add_ui(f, g, -l);
}
static void eval(mpf_ptr f, signed long int l, mpf_srcptr g)
{
if (l >= 0)
mpf_sub_ui(f, g, l);
else
mpf_add_ui(f, g, -l);
mpf_neg(f, f);
}
static void eval(mpf_ptr f, mpf_srcptr g, double d)
{
mpf_t temp;
mpf_init2(temp, 8*sizeof(double));
mpf_set_d(temp, d);
mpf_sub(f, g, temp);
mpf_clear(temp);
}
static void eval(mpf_ptr f, double d, mpf_srcptr g)
{
mpf_t temp;
mpf_init2(temp, 8*sizeof(double));
mpf_set_d(temp, d);
mpf_sub(f, temp, g);
mpf_clear(temp);
}
};
struct __gmp_binary_multiplies
{
static void eval(mpz_ptr z, mpz_srcptr w, mpz_srcptr v)
{ mpz_mul(z, w, v); }
static void eval(mpz_ptr z, mpz_srcptr w, unsigned long int l)
{ mpz_mul_ui(z, w, l); }
static void eval(mpz_ptr z, unsigned long int l, mpz_srcptr w)
{ mpz_mul_ui(z, w, l); }
static void eval(mpz_ptr z, mpz_srcptr w, signed long int l)
{ mpz_mul_si (z, w, l); }
static void eval(mpz_ptr z, signed long int l, mpz_srcptr w)
{ mpz_mul_si (z, w, l); }
static void eval(mpz_ptr z, mpz_srcptr w, double d)
{
mpz_t temp;
mpz_init_set_d(temp, d);
mpz_mul(z, w, temp);
mpz_clear(temp);
}
static void eval(mpz_ptr z, double d, mpz_srcptr w)
{
mpz_t temp;
mpz_init_set_d(temp, d);
mpz_mul(z, temp, w);
mpz_clear(temp);
}
static void eval(mpq_ptr q, mpq_srcptr r, mpq_srcptr s)
{ mpq_mul(q, r, s); }
static void eval(mpq_ptr q, mpq_srcptr r, unsigned long int l)
{
mpq_t temp;
mpq_init(temp);
mpq_set_ui(temp, l, 1);
mpq_mul(q, r, temp);
mpq_clear(temp);
}
static void eval(mpq_ptr q, unsigned long int l, mpq_srcptr r)
{
mpq_t temp;
mpq_init(temp);
mpq_set_ui(temp, l, 1);
mpq_mul(q, temp, r);
mpq_clear(temp);
}
static void eval(mpq_ptr q, mpq_srcptr r, signed long int l)
{
mpq_t temp;
mpq_init(temp);
mpq_set_si(temp, l, 1);
mpq_mul(q, r, temp);
mpq_clear(temp);
}
static void eval(mpq_ptr q, signed long int l, mpq_srcptr r)
{
mpq_t temp;
mpq_init(temp);
mpq_set_si(temp, l, 1);
mpq_mul(q, temp, r);
mpq_clear(temp);
}
static void eval(mpq_ptr q, mpq_srcptr r, double d)
{
mpq_t temp;
mpq_init(temp);
mpq_set_d(temp, d);
mpq_mul(q, r, temp);
mpq_clear(temp);
}
static void eval(mpq_ptr q, double d, mpq_srcptr r)
{
mpq_t temp;
mpq_init(temp);
mpq_set_d(temp, d);
mpq_mul(q, temp, r);
mpq_clear(temp);
}
static void eval(mpf_ptr f, mpf_srcptr g, mpf_srcptr h)
{ mpf_mul(f, g, h); }
static void eval(mpf_ptr f, mpf_srcptr g, unsigned long int l)
{ mpf_mul_ui(f, g, l); }
static void eval(mpf_ptr f, unsigned long int l, mpf_srcptr g)
{ mpf_mul_ui(f, g, l); }
static void eval(mpf_ptr f, mpf_srcptr g, signed long int l)
{
if (l >= 0)
mpf_mul_ui(f, g, l);
else
{
mpf_mul_ui(f, g, -l);
mpf_neg(f, f);
}
}
static void eval(mpf_ptr f, signed long int l, mpf_srcptr g)
{
if (l >= 0)
mpf_mul_ui(f, g, l);
else
{
mpf_mul_ui(f, g, -l);
mpf_neg(f, f);
}
}
static void eval(mpf_ptr f, mpf_srcptr g, double d)
{
mpf_t temp;
mpf_init2(temp, 8*sizeof(double));
mpf_set_d(temp, d);
mpf_mul(f, g, temp);
mpf_clear(temp);
}
static void eval(mpf_ptr f, double d, mpf_srcptr g)
{
mpf_t temp;
mpf_init2(temp, 8*sizeof(double));
mpf_set_d(temp, d);
mpf_mul(f, temp, g);
mpf_clear(temp);
}
};
struct __gmp_binary_divides
{
static void eval(mpz_ptr z, mpz_srcptr w, mpz_srcptr v)
{ mpz_tdiv_q(z, w, v); }
static void eval(mpz_ptr z, mpz_srcptr w, unsigned long int l)
{ mpz_tdiv_q_ui(z, w, l); }
static void eval(mpz_ptr z, unsigned long int l, mpz_srcptr w)
{
if (mpz_sgn(w) >= 0)
{
if (mpz_fits_ulong_p(w))
mpz_set_ui(z, l / mpz_get_ui(w));
else
mpz_set_ui(z, 0);
}
else
{
mpz_neg(z, w);
if (mpz_fits_ulong_p(z))
{
mpz_set_ui(z, l / mpz_get_ui(z));
mpz_neg(z, z);
}
else
mpz_set_ui(z, 0);
}
}
static void eval(mpz_ptr z, mpz_srcptr w, signed long int l)
{
if (l >= 0)
mpz_tdiv_q_ui(z, w, l);
else
{
mpz_tdiv_q_ui(z, w, -l);
mpz_neg(z, z);
}
}
static void eval(mpz_ptr z, signed long int l, mpz_srcptr w)
{
if (mpz_fits_slong_p(w))
mpz_set_si(z, l / mpz_get_si(w));
else
{
/* if w is bigger than a long then the quotient must be zero, unless
l==LONG_MIN and w==-LONG_MIN in which case the quotient is -1 */
mpz_set_si (z, (mpz_cmpabs_ui (w, (l >= 0 ? l : -l)) == 0 ? -1 : 0));
}
}
static void eval(mpz_ptr z, mpz_srcptr w, double d)
{
mpz_t temp;
mpz_init_set_d(temp, d);
mpz_tdiv_q(z, w, temp);
mpz_clear(temp);
}
static void eval(mpz_ptr z, double d, mpz_srcptr w)
{
mpz_t temp;
mpz_init_set_d(temp, d);
mpz_tdiv_q(z, temp, w);
mpz_clear(temp);
}
static void eval(mpq_ptr q, mpq_srcptr r, mpq_srcptr s)
{ mpq_div(q, r, s); }
static void eval(mpq_ptr q, mpq_srcptr r, unsigned long int l)
{
mpq_t temp;
mpq_init(temp);
mpq_set_ui(temp, l, 1);
mpq_div(q, r, temp);
mpq_clear(temp);
}
static void eval(mpq_ptr q, unsigned long int l, mpq_srcptr r)
{
mpq_t temp;
mpq_init(temp);
mpq_set_ui(temp, l, 1);
mpq_div(q, temp, r);
mpq_clear(temp);
}
static void eval(mpq_ptr q, mpq_srcptr r, signed long int l)
{
mpq_t temp;
mpq_init(temp);
mpq_set_si(temp, l, 1);
mpq_div(q, r, temp);
mpq_clear(temp);
}
static void eval(mpq_ptr q, signed long int l, mpq_srcptr r)
{
mpq_t temp;
mpq_init(temp);
mpq_set_si(temp, l, 1);
mpq_div(q, temp, r);
mpq_clear(temp);
}
static void eval(mpq_ptr q, mpq_srcptr r, double d)
{
mpq_t temp;
mpq_init(temp);
mpq_set_d(temp, d);
mpq_div(q, r, temp);
mpq_clear(temp);
}
static void eval(mpq_ptr q, double d, mpq_srcptr r)
{
mpq_t temp;
mpq_init(temp);
mpq_set_d(temp, d);
mpq_div(q, temp, r);
mpq_clear(temp);
}
static void eval(mpf_ptr f, mpf_srcptr g, mpf_srcptr h)
{ mpf_div(f, g, h); }
static void eval(mpf_ptr f, mpf_srcptr g, unsigned long int l)
{ mpf_div_ui(f, g, l); }
static void eval(mpf_ptr f, unsigned long int l, mpf_srcptr g)
{ mpf_ui_div(f, l, g); }
static void eval(mpf_ptr f, mpf_srcptr g, signed long int l)
{
if (l >= 0)
mpf_div_ui(f, g, l);
else
{
mpf_div_ui(f, g, -l);
mpf_neg(f, f);
}
}
static void eval(mpf_ptr f, signed long int l, mpf_srcptr g)
{
if (l >= 0)
mpf_ui_div(f, l, g);
else
{
mpf_ui_div(f, -l, g);
mpf_neg(f, f);
}
}
static void eval(mpf_ptr f, mpf_srcptr g, double d)
{
mpf_t temp;
mpf_init2(temp, 8*sizeof(double));
mpf_set_d(temp, d);
mpf_div(f, g, temp);
mpf_clear(temp);
}
static void eval(mpf_ptr f, double d, mpf_srcptr g)
{
mpf_t temp;
mpf_init2(temp, 8*sizeof(double));
mpf_set_d(temp, d);
mpf_div(f, temp, g);
mpf_clear(temp);
}
};
struct __gmp_binary_modulus
{
static void eval(mpz_ptr z, mpz_srcptr w, mpz_srcptr v)
{ mpz_tdiv_r(z, w, v); }
static void eval(mpz_ptr z, mpz_srcptr w, unsigned long int l)
{ mpz_tdiv_r_ui(z, w, l); }
static void eval(mpz_ptr z, unsigned long int l, mpz_srcptr w)
{
if (mpz_sgn(w) >= 0)
{
if (mpz_fits_ulong_p(w))
mpz_set_ui(z, l % mpz_get_ui(w));
else
mpz_set_ui(z, l);
}
else
{
mpz_neg(z, w);
if (mpz_fits_ulong_p(z))
mpz_set_ui(z, l % mpz_get_ui(z));
else
mpz_set_ui(z, l);
}
}
static void eval(mpz_ptr z, mpz_srcptr w, signed long int l)
{
mpz_tdiv_r_ui (z, w, (l >= 0 ? l : -l));
}
static void eval(mpz_ptr z, signed long int l, mpz_srcptr w)
{
if (mpz_fits_slong_p(w))
mpz_set_si(z, l % mpz_get_si(w));
else
{
/* if w is bigger than a long then the remainder is l unchanged,
unless l==LONG_MIN and w==-LONG_MIN in which case it's 0 */
mpz_set_si (z, mpz_cmpabs_ui (w, (l >= 0 ? l : -l)) == 0 ? 0 : l);
}
}
static void eval(mpz_ptr z, mpz_srcptr w, double d)
{
mpz_t temp;
mpz_init_set_d(temp, d);
mpz_tdiv_r(z, w, temp);
mpz_clear(temp);
}
static void eval(mpz_ptr z, double d, mpz_srcptr w)
{
mpz_t temp;
mpz_init_set_d(temp, d);
mpz_tdiv_r(z, temp, w);
mpz_clear(temp);
}
};
// Max allocations for plain types when converted to mpz_t
#define __GMP_DBL_LIMBS (2 + DBL_MAX_EXP / GMP_NUMB_BITS)
#define __GMP_ULI_LIMBS (1 + (8 * sizeof (long) - 1) / GMP_NUMB_BITS)
#define __GMPXX_TMP_UI \
mpz_t temp; \
mp_limb_t limbs[__GMP_ULI_LIMBS]; \
temp->_mp_d = limbs; \
temp->_mp_alloc = __GMP_ULI_LIMBS; \
mpz_set_ui (temp, l)
#define __GMPXX_TMP_SI \
mpz_t temp; \
mp_limb_t limbs[__GMP_ULI_LIMBS]; \
temp->_mp_d = limbs; \
temp->_mp_alloc = __GMP_ULI_LIMBS; \
mpz_set_si (temp, l)
#define __GMPXX_TMP_D \
mpz_t temp; \
mp_limb_t limbs[__GMP_DBL_LIMBS]; \
temp->_mp_d = limbs; \
temp->_mp_alloc = __GMP_DBL_LIMBS; \
mpz_set_d (temp, d)
struct __gmp_binary_and
{
static void eval(mpz_ptr z, mpz_srcptr w, mpz_srcptr v)
{ mpz_and(z, w, v); }
static void eval(mpz_ptr z, mpz_srcptr w, unsigned long int l)
{ __GMPXX_TMP_UI; mpz_and (z, w, temp); }
static void eval(mpz_ptr z, unsigned long int l, mpz_srcptr w)
{ __GMPXX_TMP_UI; mpz_and (z, w, temp); }
static void eval(mpz_ptr z, mpz_srcptr w, signed long int l)
{ __GMPXX_TMP_SI; mpz_and (z, w, temp); }
static void eval(mpz_ptr z, signed long int l, mpz_srcptr w)
{ __GMPXX_TMP_SI; mpz_and (z, w, temp); }
static void eval(mpz_ptr z, mpz_srcptr w, double d)
{ __GMPXX_TMP_D; mpz_and (z, w, temp); }
static void eval(mpz_ptr z, double d, mpz_srcptr w)
{ __GMPXX_TMP_D; mpz_and (z, w, temp); }
};
struct __gmp_binary_ior
{
static void eval(mpz_ptr z, mpz_srcptr w, mpz_srcptr v)
{ mpz_ior(z, w, v); }
static void eval(mpz_ptr z, mpz_srcptr w, unsigned long int l)
{ __GMPXX_TMP_UI; mpz_ior (z, w, temp); }
static void eval(mpz_ptr z, unsigned long int l, mpz_srcptr w)
{ __GMPXX_TMP_UI; mpz_ior (z, w, temp); }
static void eval(mpz_ptr z, mpz_srcptr w, signed long int l)
{ __GMPXX_TMP_SI; mpz_ior (z, w, temp); }
static void eval(mpz_ptr z, signed long int l, mpz_srcptr w)
{ __GMPXX_TMP_SI; mpz_ior (z, w, temp); }
static void eval(mpz_ptr z, mpz_srcptr w, double d)
{ __GMPXX_TMP_D; mpz_ior (z, w, temp); }
static void eval(mpz_ptr z, double d, mpz_srcptr w)
{ __GMPXX_TMP_D; mpz_ior (z, w, temp); }
};
struct __gmp_binary_xor
{
static void eval(mpz_ptr z, mpz_srcptr w, mpz_srcptr v)
{ mpz_xor(z, w, v); }
static void eval(mpz_ptr z, mpz_srcptr w, unsigned long int l)
{ __GMPXX_TMP_UI; mpz_xor (z, w, temp); }
static void eval(mpz_ptr z, unsigned long int l, mpz_srcptr w)
{ __GMPXX_TMP_UI; mpz_xor (z, w, temp); }
static void eval(mpz_ptr z, mpz_srcptr w, signed long int l)
{ __GMPXX_TMP_SI; mpz_xor (z, w, temp); }
static void eval(mpz_ptr z, signed long int l, mpz_srcptr w)
{ __GMPXX_TMP_SI; mpz_xor (z, w, temp); }
static void eval(mpz_ptr z, mpz_srcptr w, double d)
{ __GMPXX_TMP_D; mpz_xor (z, w, temp); }
static void eval(mpz_ptr z, double d, mpz_srcptr w)
{ __GMPXX_TMP_D; mpz_xor (z, w, temp); }
};
struct __gmp_binary_lshift
{
static void eval(mpz_ptr z, mpz_srcptr w, unsigned long int l)
{ mpz_mul_2exp(z, w, l); }
static void eval(mpq_ptr q, mpq_srcptr r, unsigned long int l)
{ mpq_mul_2exp(q, r, l); }
static void eval(mpf_ptr f, mpf_srcptr g, unsigned long int l)
{ mpf_mul_2exp(f, g, l); }
};
struct __gmp_binary_rshift
{
static void eval(mpz_ptr z, mpz_srcptr w, unsigned long int l)
{ mpz_fdiv_q_2exp(z, w, l); }
static void eval(mpq_ptr q, mpq_srcptr r, unsigned long int l)
{ mpq_div_2exp(q, r, l); }
static void eval(mpf_ptr f, mpf_srcptr g, unsigned long int l)
{ mpf_div_2exp(f, g, l); }
};
struct __gmp_binary_equal
{
static bool eval(mpz_srcptr z, mpz_srcptr w) { return mpz_cmp(z, w) == 0; }
static bool eval(mpz_srcptr z, unsigned long int l)
{ return mpz_cmp_ui(z, l) == 0; }
static bool eval(unsigned long int l, mpz_srcptr z)
{ return mpz_cmp_ui(z, l) == 0; }
static bool eval(mpz_srcptr z, signed long int l)
{ return mpz_cmp_si(z, l) == 0; }
static bool eval(signed long int l, mpz_srcptr z)
{ return mpz_cmp_si(z, l) == 0; }
static bool eval(mpz_srcptr z, double d)
{ return mpz_cmp_d(z, d) == 0; }
static bool eval(double d, mpz_srcptr z)
{ return mpz_cmp_d(z, d) == 0; }
static bool eval(mpq_srcptr q, mpq_srcptr r)
{ return mpq_equal(q, r) != 0; }
static bool eval(mpq_srcptr q, unsigned long int l)
{ return mpq_cmp_ui(q, l, 1) == 0; }
static bool eval(unsigned long int l, mpq_srcptr q)
{ return mpq_cmp_ui(q, l, 1) == 0; }
static bool eval(mpq_srcptr q, signed long int l)
{ return mpq_cmp_si(q, l, 1) == 0; }
static bool eval(signed long int l, mpq_srcptr q)
{ return mpq_cmp_si(q, l, 1) == 0; }
static bool eval(mpq_srcptr q, double d)
{
bool b;
mpq_t temp;
mpq_init(temp);
mpq_set_d(temp, d);
b = (mpq_equal(q, temp) != 0);
mpq_clear(temp);
return b;
}
static bool eval(double d, mpq_srcptr q)
{
bool b;
mpq_t temp;
mpq_init(temp);
mpq_set_d(temp, d);
b = (mpq_equal(temp, q) != 0);
mpq_clear(temp);
return b;
}
static bool eval(mpf_srcptr f, mpf_srcptr g) { return mpf_cmp(f, g) == 0; }
static bool eval(mpf_srcptr f, unsigned long int l)
{ return mpf_cmp_ui(f, l) == 0; }
static bool eval(unsigned long int l, mpf_srcptr f)
{ return mpf_cmp_ui(f, l) == 0; }
static bool eval(mpf_srcptr f, signed long int l)
{ return mpf_cmp_si(f, l) == 0; }
static bool eval(signed long int l, mpf_srcptr f)
{ return mpf_cmp_si(f, l) == 0; }
static bool eval(mpf_srcptr f, double d)
{ return mpf_cmp_d(f, d) == 0; }
static bool eval(double d, mpf_srcptr f)
{ return mpf_cmp_d(f, d) == 0; }
};
struct __gmp_binary_not_equal
{
static bool eval(mpz_srcptr z, mpz_srcptr w) { return mpz_cmp(z, w) != 0; }
static bool eval(mpz_srcptr z, unsigned long int l)
{ return mpz_cmp_ui(z, l) != 0; }
static bool eval(unsigned long int l, mpz_srcptr z)
{ return mpz_cmp_ui(z, l) != 0; }
static bool eval(mpz_srcptr z, signed long int l)
{ return mpz_cmp_si(z, l) != 0; }
static bool eval(signed long int l, mpz_srcptr z)
{ return mpz_cmp_si(z, l) != 0; }
static bool eval(mpz_srcptr z, double d)
{ return mpz_cmp_d(z, d) != 0; }
static bool eval(double d, mpz_srcptr z)
{ return mpz_cmp_d(z, d) != 0; }
static bool eval(mpq_srcptr q, mpq_srcptr r)
{ return mpq_equal(q, r) == 0; }
static bool eval(mpq_srcptr q, unsigned long int l)
{ return mpq_cmp_ui(q, l, 1) != 0; }
static bool eval(unsigned long int l, mpq_srcptr q)
{ return mpq_cmp_ui(q, l, 1) != 0; }
static bool eval(mpq_srcptr q, signed long int l)
{ return mpq_cmp_si(q, l, 1) != 0; }
static bool eval(signed long int l, mpq_srcptr q)
{ return mpq_cmp_si(q, l, 1) != 0; }
static bool eval(mpq_srcptr q, double d)
{
bool b;
mpq_t temp;
mpq_init(temp);
mpq_set_d(temp, d);
b = (mpq_equal(q, temp) == 0);
mpq_clear(temp);
return b;
}
static bool eval(double d, mpq_srcptr q)
{
bool b;
mpq_t temp;
mpq_init(temp);
mpq_set_d(temp, d);
b = (mpq_equal(temp, q) == 0);
mpq_clear(temp);
return b;
}
static bool eval(mpf_srcptr f, mpf_srcptr g) { return mpf_cmp(f, g) != 0; }
static bool eval(mpf_srcptr f, unsigned long int l)
{ return mpf_cmp_ui(f, l) != 0; }
static bool eval(unsigned long int l, mpf_srcptr f)
{ return mpf_cmp_ui(f, l) != 0; }
static bool eval(mpf_srcptr f, signed long int l)
{ return mpf_cmp_si(f, l) != 0; }
static bool eval(signed long int l, mpf_srcptr f)
{ return mpf_cmp_si(f, l) != 0; }
static bool eval(mpf_srcptr f, double d)
{ return mpf_cmp_d(f, d) != 0; }
static bool eval(double d, mpf_srcptr f)
{ return mpf_cmp_d(f, d) != 0; }
};
struct __gmp_binary_less
{
static bool eval(mpz_srcptr z, mpz_srcptr w) { return mpz_cmp(z, w) < 0; }
static bool eval(mpz_srcptr z, unsigned long int l)
{ return mpz_cmp_ui(z, l) < 0; }
static bool eval(unsigned long int l, mpz_srcptr z)
{ return mpz_cmp_ui(z, l) > 0; }
static bool eval(mpz_srcptr z, signed long int l)
{ return mpz_cmp_si(z, l) < 0; }
static bool eval(signed long int l, mpz_srcptr z)
{ return mpz_cmp_si(z, l) > 0; }
static bool eval(mpz_srcptr z, double d)
{ return mpz_cmp_d(z, d) < 0; }
static bool eval(double d, mpz_srcptr z)
{ return mpz_cmp_d(z, d) > 0; }
static bool eval(mpq_srcptr q, mpq_srcptr r) { return mpq_cmp(q, r) < 0; }
static bool eval(mpq_srcptr q, unsigned long int l)
{ return mpq_cmp_ui(q, l, 1) < 0; }
static bool eval(unsigned long int l, mpq_srcptr q)
{ return mpq_cmp_ui(q, l, 1) > 0; }
static bool eval(mpq_srcptr q, signed long int l)
{ return mpq_cmp_si(q, l, 1) < 0; }
static bool eval(signed long int l, mpq_srcptr q)
{ return mpq_cmp_si(q, l, 1) > 0; }
static bool eval(mpq_srcptr q, double d)
{
bool b;
mpq_t temp;
mpq_init(temp);
mpq_set_d(temp, d);
b = (mpq_cmp(q, temp) < 0);
mpq_clear(temp);
return b;
}
static bool eval(double d, mpq_srcptr q)
{
bool b;
mpq_t temp;
mpq_init(temp);
mpq_set_d(temp, d);
b = (mpq_cmp(temp, q) < 0);
mpq_clear(temp);
return b;
}
static bool eval(mpf_srcptr f, mpf_srcptr g) { return mpf_cmp(f, g) < 0; }
static bool eval(mpf_srcptr f, unsigned long int l)
{ return mpf_cmp_ui(f, l) < 0; }
static bool eval(unsigned long int l, mpf_srcptr f)
{ return mpf_cmp_ui(f, l) > 0; }
static bool eval(mpf_srcptr f, signed long int l)
{ return mpf_cmp_si(f, l) < 0; }
static bool eval(signed long int l, mpf_srcptr f)
{ return mpf_cmp_si(f, l) > 0; }
static bool eval(mpf_srcptr f, double d)
{ return mpf_cmp_d(f, d) < 0; }
static bool eval(double d, mpf_srcptr f)
{ return mpf_cmp_d(f, d) > 0; }
};
struct __gmp_binary_less_equal
{
static bool eval(mpz_srcptr z, mpz_srcptr w) { return mpz_cmp(z, w) <= 0; }
static bool eval(mpz_srcptr z, unsigned long int l)
{ return mpz_cmp_ui(z, l) <= 0; }
static bool eval(unsigned long int l, mpz_srcptr z)
{ return mpz_cmp_ui(z, l) >= 0; }
static bool eval(mpz_srcptr z, signed long int l)
{ return mpz_cmp_si(z, l) <= 0; }
static bool eval(signed long int l, mpz_srcptr z)
{ return mpz_cmp_si(z, l) >= 0; }
static bool eval(mpz_srcptr z, double d)
{ return mpz_cmp_d(z, d) <= 0; }
static bool eval(double d, mpz_srcptr z)
{ return mpz_cmp_d(z, d) >= 0; }
static bool eval(mpq_srcptr q, mpq_srcptr r) { return mpq_cmp(q, r) <= 0; }
static bool eval(mpq_srcptr q, unsigned long int l)
{ return mpq_cmp_ui(q, l, 1) <= 0; }
static bool eval(unsigned long int l, mpq_srcptr q)
{ return mpq_cmp_ui(q, l, 1) >= 0; }
static bool eval(mpq_srcptr q, signed long int l)
{ return mpq_cmp_si(q, l, 1) <= 0; }
static bool eval(signed long int l, mpq_srcptr q)
{ return mpq_cmp_si(q, l, 1) >= 0; }
static bool eval(mpq_srcptr q, double d)
{
bool b;
mpq_t temp;
mpq_init(temp);
mpq_set_d(temp, d);
b = (mpq_cmp(q, temp) <= 0);
mpq_clear(temp);
return b;
}
static bool eval(double d, mpq_srcptr q)
{
bool b;
mpq_t temp;
mpq_init(temp);
mpq_set_d(temp, d);
b = (mpq_cmp(temp, q) <= 0);
mpq_clear(temp);
return b;
}
static bool eval(mpf_srcptr f, mpf_srcptr g) { return mpf_cmp(f, g) <= 0; }
static bool eval(mpf_srcptr f, unsigned long int l)
{ return mpf_cmp_ui(f, l) <= 0; }
static bool eval(unsigned long int l, mpf_srcptr f)
{ return mpf_cmp_ui(f, l) >= 0; }
static bool eval(mpf_srcptr f, signed long int l)
{ return mpf_cmp_si(f, l) <= 0; }
static bool eval(signed long int l, mpf_srcptr f)
{ return mpf_cmp_si(f, l) >= 0; }
static bool eval(mpf_srcptr f, double d)
{ return mpf_cmp_d(f, d) <= 0; }
static bool eval(double d, mpf_srcptr f)
{ return mpf_cmp_d(f, d) >= 0; }
};
struct __gmp_binary_greater
{
static bool eval(mpz_srcptr z, mpz_srcptr w) { return mpz_cmp(z, w) > 0; }
static bool eval(mpz_srcptr z, unsigned long int l)
{ return mpz_cmp_ui(z, l) > 0; }
static bool eval(unsigned long int l, mpz_srcptr z)
{ return mpz_cmp_ui(z, l) < 0; }
static bool eval(mpz_srcptr z, signed long int l)
{ return mpz_cmp_si(z, l) > 0; }
static bool eval(signed long int l, mpz_srcptr z)
{ return mpz_cmp_si(z, l) < 0; }
static bool eval(mpz_srcptr z, double d)
{ return mpz_cmp_d(z, d) > 0; }
static bool eval(double d, mpz_srcptr z)
{ return mpz_cmp_d(z, d) < 0; }
static bool eval(mpq_srcptr q, mpq_srcptr r) { return mpq_cmp(q, r) > 0; }
static bool eval(mpq_srcptr q, unsigned long int l)
{ return mpq_cmp_ui(q, l, 1) > 0; }
static bool eval(unsigned long int l, mpq_srcptr q)
{ return mpq_cmp_ui(q, l, 1) < 0; }
static bool eval(mpq_srcptr q, signed long int l)
{ return mpq_cmp_si(q, l, 1) > 0; }
static bool eval(signed long int l, mpq_srcptr q)
{ return mpq_cmp_si(q, l, 1) < 0; }
static bool eval(mpq_srcptr q, double d)
{
bool b;
mpq_t temp;
mpq_init(temp);
mpq_set_d(temp, d);
b = (mpq_cmp(q, temp) > 0);
mpq_clear(temp);
return b;
}
static bool eval(double d, mpq_srcptr q)
{
bool b;
mpq_t temp;
mpq_init(temp);
mpq_set_d(temp, d);
b = (mpq_cmp(temp, q) > 0);
mpq_clear(temp);
return b;
}
static bool eval(mpf_srcptr f, mpf_srcptr g) { return mpf_cmp(f, g) > 0; }
static bool eval(mpf_srcptr f, unsigned long int l)
{ return mpf_cmp_ui(f, l) > 0; }
static bool eval(unsigned long int l, mpf_srcptr f)
{ return mpf_cmp_ui(f, l) < 0; }
static bool eval(mpf_srcptr f, signed long int l)
{ return mpf_cmp_si(f, l) > 0; }
static bool eval(signed long int l, mpf_srcptr f)
{ return mpf_cmp_si(f, l) < 0; }
static bool eval(mpf_srcptr f, double d)
{ return mpf_cmp_d(f, d) > 0; }
static bool eval(double d, mpf_srcptr f)
{ return mpf_cmp_d(f, d) < 0; }
};
struct __gmp_binary_greater_equal
{
static bool eval(mpz_srcptr z, mpz_srcptr w) { return mpz_cmp(z, w) >= 0; }
static bool eval(mpz_srcptr z, unsigned long int l)
{ return mpz_cmp_ui(z, l) >= 0; }
static bool eval(unsigned long int l, mpz_srcptr z)
{ return mpz_cmp_ui(z, l) <= 0; }
static bool eval(mpz_srcptr z, signed long int l)
{ return mpz_cmp_si(z, l) >= 0; }
static bool eval(signed long int l, mpz_srcptr z)
{ return mpz_cmp_si(z, l) <= 0; }
static bool eval(mpz_srcptr z, double d)
{ return mpz_cmp_d(z, d) >= 0; }
static bool eval(double d, mpz_srcptr z)
{ return mpz_cmp_d(z, d) <= 0; }
static bool eval(mpq_srcptr q, mpq_srcptr r) { return mpq_cmp(q, r) >= 0; }
static bool eval(mpq_srcptr q, unsigned long int l)
{ return mpq_cmp_ui(q, l, 1) >= 0; }
static bool eval(unsigned long int l, mpq_srcptr q)
{ return mpq_cmp_ui(q, l, 1) <= 0; }
static bool eval(mpq_srcptr q, signed long int l)
{ return mpq_cmp_si(q, l, 1) >= 0; }
static bool eval(signed long int l, mpq_srcptr q)
{ return mpq_cmp_si(q, l, 1) <= 0; }
static bool eval(mpq_srcptr q, double d)
{
bool b;
mpq_t temp;
mpq_init(temp);
mpq_set_d(temp, d);
b = (mpq_cmp(q, temp) >= 0);
mpq_clear(temp);
return b;
}
static bool eval(double d, mpq_srcptr q)
{
bool b;
mpq_t temp;
mpq_init(temp);
mpq_set_d(temp, d);
b = (mpq_cmp(temp, q) >= 0);
mpq_clear(temp);
return b;
}
static bool eval(mpf_srcptr f, mpf_srcptr g) { return mpf_cmp(f, g) >= 0; }
static bool eval(mpf_srcptr f, unsigned long int l)
{ return mpf_cmp_ui(f, l) >= 0; }
static bool eval(unsigned long int l, mpf_srcptr f)
{ return mpf_cmp_ui(f, l) <= 0; }
static bool eval(mpf_srcptr f, signed long int l)
{ return mpf_cmp_si(f, l) >= 0; }
static bool eval(signed long int l, mpf_srcptr f)
{ return mpf_cmp_si(f, l) <= 0; }
static bool eval(mpf_srcptr f, double d)
{ return mpf_cmp_d(f, d) >= 0; }
static bool eval(double d, mpf_srcptr f)
{ return mpf_cmp_d(f, d) <= 0; }
};
struct __gmp_unary_increment
{
static void eval(mpz_ptr z) { mpz_add_ui(z, z, 1); }
static void eval(mpq_ptr q)
{ mpz_add(mpq_numref(q), mpq_numref(q), mpq_denref(q)); }
static void eval(mpf_ptr f) { mpf_add_ui(f, f, 1); }
};
struct __gmp_unary_decrement
{
static void eval(mpz_ptr z) { mpz_sub_ui(z, z, 1); }
static void eval(mpq_ptr q)
{ mpz_sub(mpq_numref(q), mpq_numref(q), mpq_denref(q)); }
static void eval(mpf_ptr f) { mpf_sub_ui(f, f, 1); }
};
struct __gmp_abs_function
{
static void eval(mpz_ptr z, mpz_srcptr w) { mpz_abs(z, w); }
static void eval(mpq_ptr q, mpq_srcptr r) { mpq_abs(q, r); }
static void eval(mpf_ptr f, mpf_srcptr g) { mpf_abs(f, g); }
};
struct __gmp_trunc_function
{
static void eval(mpf_ptr f, mpf_srcptr g) { mpf_trunc(f, g); }
};
struct __gmp_floor_function
{
static void eval(mpf_ptr f, mpf_srcptr g) { mpf_floor(f, g); }
};
struct __gmp_ceil_function
{
static void eval(mpf_ptr f, mpf_srcptr g) { mpf_ceil(f, g); }
};
struct __gmp_sqrt_function
{
static void eval(mpz_ptr z, mpz_srcptr w) { mpz_sqrt(z, w); }
static void eval(mpf_ptr f, mpf_srcptr g) { mpf_sqrt(f, g); }
};
struct __gmp_hypot_function
{
static void eval(mpf_ptr f, mpf_srcptr g, mpf_srcptr h)
{
mpf_t temp;
mpf_init2(temp, mpf_get_prec(f));
mpf_mul(temp, g, g);
mpf_mul(f, h, h);
mpf_add(f, f, temp);
mpf_sqrt(f, f);
mpf_clear(temp);
}
static void eval(mpf_ptr f, mpf_srcptr g, unsigned long int l)
{
mpf_t temp;
mpf_init2(temp, mpf_get_prec(f));
mpf_mul(temp, g, g);
mpf_set_ui(f, l);
mpf_mul(f, f, f);
mpf_add(f, f, temp);
mpf_sqrt(f, f);
mpf_clear(temp);
}
static void eval(mpf_ptr f, unsigned long int l, mpf_srcptr g)
{
mpf_t temp;
mpf_init2(temp, mpf_get_prec(f));
mpf_mul(temp, g, g);
mpf_set_ui(f, l);
mpf_mul(f, f, f);
mpf_add(f, f, temp);
mpf_sqrt(f, f);
mpf_clear(temp);
}
static void eval(mpf_ptr f, mpf_srcptr g, signed long int l)
{
mpf_t temp;
mpf_init2(temp, mpf_get_prec(f));
mpf_mul(temp, g, g);
mpf_set_si(f, l);
mpf_mul(f, f, f);
mpf_add(f, f, temp);
mpf_sqrt(f, f);
mpf_clear(temp);
}
static void eval(mpf_ptr f, signed long int l, mpf_srcptr g)
{
mpf_t temp;
mpf_init2(temp, mpf_get_prec(f));
mpf_mul(temp, g, g);
mpf_set_si(f, l);
mpf_mul(f, f, f);
mpf_add(f, f, temp);
mpf_sqrt(f, f);
mpf_clear(temp);
}
static void eval(mpf_ptr f, mpf_srcptr g, double d)
{
mpf_t temp;
mpf_init2(temp, mpf_get_prec(f));
mpf_mul(temp, g, g);
mpf_set_d(f, d);
mpf_mul(f, f, f);
mpf_add(f, f, temp);
mpf_sqrt(f, f);
mpf_clear(temp);
}
static void eval(mpf_ptr f, double d, mpf_srcptr g)
{
mpf_t temp;
mpf_init2(temp, mpf_get_prec(f));
mpf_mul(temp, g, g);
mpf_set_d(f, d);
mpf_mul(f, f, f);
mpf_add(f, f, temp);
mpf_sqrt(f, f);
mpf_clear(temp);
}
};
struct __gmp_sgn_function
{
static int eval(mpz_srcptr z) { return mpz_sgn(z); }
static int eval(mpq_srcptr q) { return mpq_sgn(q); }
static int eval(mpf_srcptr f) { return mpf_sgn(f); }
};
struct __gmp_cmp_function
{
static int eval(mpz_srcptr z, mpz_srcptr w) { return mpz_cmp(z, w); }
static int eval(mpz_srcptr z, unsigned long int l)
{ return mpz_cmp_ui(z, l); }
static int eval(unsigned long int l, mpz_srcptr z)
{ return -mpz_cmp_ui(z, l); }
static int eval(mpz_srcptr z, signed long int l)
{ return mpz_cmp_si(z, l); }
static int eval(signed long int l, mpz_srcptr z)
{ return -mpz_cmp_si(z, l); }
static int eval(mpz_srcptr z, double d)
{ return mpz_cmp_d(z, d); }
static int eval(double d, mpz_srcptr z)
{ return -mpz_cmp_d(z, d); }
static int eval(mpq_srcptr q, mpq_srcptr r) { return mpq_cmp(q, r); }
static int eval(mpq_srcptr q, unsigned long int l)
{ return mpq_cmp_ui(q, l, 1); }
static int eval(unsigned long int l, mpq_srcptr q)
{ return -mpq_cmp_ui(q, l, 1); }
static int eval(mpq_srcptr q, signed long int l)
{ return mpq_cmp_si(q, l, 1); }
static int eval(signed long int l, mpq_srcptr q)
{ return -mpq_cmp_si(q, l, 1); }
static int eval(mpq_srcptr q, double d)
{
int i;
mpq_t temp;
mpq_init(temp);
mpq_set_d(temp, d);
i = mpq_cmp(q, temp);
mpq_clear(temp);
return i;
}
static int eval(double d, mpq_srcptr q)
{
int i;
mpq_t temp;
mpq_init(temp);
mpq_set_d(temp, d);
i = mpq_cmp(temp, q);
mpq_clear(temp);
return i;
}
static int eval(mpf_srcptr f, mpf_srcptr g) { return mpf_cmp(f, g); }
static int eval(mpf_srcptr f, unsigned long int l)
{ return mpf_cmp_ui(f, l); }
static int eval(unsigned long int l, mpf_srcptr f)
{ return -mpf_cmp_ui(f, l); }
static int eval(mpf_srcptr f, signed long int l)
{ return mpf_cmp_si(f, l); }
static int eval(signed long int l, mpf_srcptr f)
{ return -mpf_cmp_si(f, l); }
static int eval(mpf_srcptr f, double d)
{ return mpf_cmp_d(f, d); }
static int eval(double d, mpf_srcptr f)
{ return -mpf_cmp_d(f, d); }
};
struct __gmp_rand_function
{
static void eval(mpz_ptr z, gmp_randstate_t s, unsigned long int l)
{ mpz_urandomb(z, s, l); }
static void eval(mpz_ptr z, gmp_randstate_t s, mpz_srcptr w)
{ mpz_urandomm(z, s, w); }
static void eval(mpf_ptr f, gmp_randstate_t s, mp_bitcnt_t prec)
{ mpf_urandomb(f, s, prec); }
};
/**************** Auxiliary classes ****************/
/* this is much the same as gmp_allocated_string in gmp-impl.h
since gmp-impl.h is not publicly available, I redefine it here
I use a different name to avoid possible clashes */
extern "C" {
typedef void (*__gmp_freefunc_t) (void *, size_t);
}
struct __gmp_alloc_cstring
{
char *str;
__gmp_alloc_cstring(char *s) { str = s; }
~__gmp_alloc_cstring()
{
__gmp_freefunc_t freefunc;
mp_get_memory_functions (NULL, NULL, &freefunc);
(*freefunc) (str, std::strlen(str)+1);
}
};
// general expression template class
template <class T, class U>
class __gmp_expr;
// templates for resolving expression types
template <class T>
struct __gmp_resolve_ref
{
typedef T ref_type;
};
template <class T, class U>
struct __gmp_resolve_ref<__gmp_expr<T, U> >
{
typedef const __gmp_expr<T, U> & ref_type;
};
template <class T, class U = T>
struct __gmp_resolve_expr;
template <>
struct __gmp_resolve_expr<mpz_t>
{
typedef mpz_t value_type;
typedef mpz_ptr ptr_type;
};
template <>
struct __gmp_resolve_expr<mpq_t>
{
typedef mpq_t value_type;
typedef mpq_ptr ptr_type;
};
template <>
struct __gmp_resolve_expr<mpf_t>
{
typedef mpf_t value_type;
typedef mpf_ptr ptr_type;
};
template <>
struct __gmp_resolve_expr<mpz_t, mpq_t>
{
typedef mpq_t value_type;
};
template <>
struct __gmp_resolve_expr<mpq_t, mpz_t>
{
typedef mpq_t value_type;
};
template <>
struct __gmp_resolve_expr<mpz_t, mpf_t>
{
typedef mpf_t value_type;
};
template <>
struct __gmp_resolve_expr<mpf_t, mpz_t>
{
typedef mpf_t value_type;
};
template <>
struct __gmp_resolve_expr<mpq_t, mpf_t>
{
typedef mpf_t value_type;
};
template <>
struct __gmp_resolve_expr<mpf_t, mpq_t>
{
typedef mpf_t value_type;
};
template <class T, class U, class V>
struct __gmp_resolve_temp
{
typedef __gmp_expr<T, T> temp_type;
};
template <class T>
struct __gmp_resolve_temp<T, T, T>
{
typedef const __gmp_expr<T, T> & temp_type;
};
// classes for evaluating unary and binary expressions
template <class T, class Op>
struct __gmp_unary_expr
{
const T &val;
__gmp_unary_expr(const T &v) : val(v) { }
private:
__gmp_unary_expr();
};
template <class T, class U, class Op>
struct __gmp_binary_expr
{
typename __gmp_resolve_ref<T>::ref_type val1;
typename __gmp_resolve_ref<U>::ref_type val2;
__gmp_binary_expr(const T &v1, const U &v2) : val1(v1), val2(v2) { }
private:
__gmp_binary_expr();
};
// functions for evaluating expressions
template <class T, class U>
void __gmp_set_expr(mpz_ptr, const __gmp_expr<T, U> &);
template <class T, class U>
void __gmp_set_expr(mpq_ptr, const __gmp_expr<T, U> &);
template <class T, class U>
void __gmp_set_expr(mpf_ptr, const __gmp_expr<T, U> &);
/**************** Macros for in-class declarations ****************/
/* This is just repetitive code that is easier to maintain if it's written
only once */
#define __GMPP_DECLARE_COMPOUND_OPERATOR(fun) \
template <class T, class U> \
__gmp_expr<value_type, value_type> & fun(const __gmp_expr<T, U> &);
#define __GMPN_DECLARE_COMPOUND_OPERATOR(fun) \
__gmp_expr & fun(signed char); \
__gmp_expr & fun(unsigned char); \
__gmp_expr & fun(signed int); \
__gmp_expr & fun(unsigned int); \
__gmp_expr & fun(signed short int); \
__gmp_expr & fun(unsigned short int); \
__gmp_expr & fun(signed long int); \
__gmp_expr & fun(unsigned long int); \
__gmp_expr & fun(float); \
__gmp_expr & fun(double); \
__gmp_expr & fun(long double);
#define __GMP_DECLARE_COMPOUND_OPERATOR(fun) \
__GMPP_DECLARE_COMPOUND_OPERATOR(fun) \
__GMPN_DECLARE_COMPOUND_OPERATOR(fun)
#define __GMP_DECLARE_COMPOUND_OPERATOR_UI(fun) \
__gmp_expr & fun(unsigned long int);
#define __GMP_DECLARE_INCREMENT_OPERATOR(fun) \
inline __gmp_expr & fun(); \
inline __gmp_expr fun(int);
/**************** mpz_class -- wrapper for mpz_t ****************/
template <>
class __gmp_expr<mpz_t, mpz_t>
{
private:
typedef mpz_t value_type;
value_type mp;
public:
mp_bitcnt_t get_prec() const { return mpf_get_default_prec(); }
// constructors and destructor
__gmp_expr() { mpz_init(mp); }
__gmp_expr(const __gmp_expr &z) { mpz_init_set(mp, z.mp); }
template <class T, class U>
__gmp_expr(const __gmp_expr<T, U> &expr)
{ mpz_init(mp); __gmp_set_expr(mp, expr); }
__gmp_expr(signed char c) { mpz_init_set_si(mp, c); }
__gmp_expr(unsigned char c) { mpz_init_set_ui(mp, c); }
__gmp_expr(signed int i) { mpz_init_set_si(mp, i); }
__gmp_expr(unsigned int i) { mpz_init_set_ui(mp, i); }
__gmp_expr(signed short int s) { mpz_init_set_si(mp, s); }
__gmp_expr(unsigned short int s) { mpz_init_set_ui(mp, s); }
__gmp_expr(signed long int l) { mpz_init_set_si(mp, l); }
__gmp_expr(unsigned long int l) { mpz_init_set_ui(mp, l); }
__gmp_expr(float f) { mpz_init_set_d(mp, f); }
__gmp_expr(double d) { mpz_init_set_d(mp, d); }
// __gmp_expr(long double ld) { mpz_init_set_d(mp, ld); }
explicit __gmp_expr(const char *s)
{
if (mpz_init_set_str (mp, s, 0) != 0)
{
mpz_clear (mp);
throw std::invalid_argument ("mpz_set_str");
}
}
__gmp_expr(const char *s, int base)
{
if (mpz_init_set_str (mp, s, base) != 0)
{
mpz_clear (mp);
throw std::invalid_argument ("mpz_set_str");
}
}
explicit __gmp_expr(const std::string &s)
{
if (mpz_init_set_str (mp, s.c_str(), 0) != 0)
{
mpz_clear (mp);
throw std::invalid_argument ("mpz_set_str");
}
}
__gmp_expr(const std::string &s, int base)
{
if (mpz_init_set_str(mp, s.c_str(), base) != 0)
{
mpz_clear (mp);
throw std::invalid_argument ("mpz_set_str");
}
}
explicit __gmp_expr(mpz_srcptr z) { mpz_init_set(mp, z); }
~__gmp_expr() { mpz_clear(mp); }
// assignment operators
__gmp_expr & operator=(const __gmp_expr &z)
{ mpz_set(mp, z.mp); return *this; }
template <class T, class U>
__gmp_expr<value_type, value_type> & operator=(const __gmp_expr<T, U> &expr)
{ __gmp_set_expr(mp, expr); return *this; }
__gmp_expr & operator=(signed char c) { mpz_set_si(mp, c); return *this; }
__gmp_expr & operator=(unsigned char c) { mpz_set_ui(mp, c); return *this; }
__gmp_expr & operator=(signed int i) { mpz_set_si(mp, i); return *this; }
__gmp_expr & operator=(unsigned int i) { mpz_set_ui(mp, i); return *this; }
__gmp_expr & operator=(signed short int s)
{ mpz_set_si(mp, s); return *this; }
__gmp_expr & operator=(unsigned short int s)
{ mpz_set_ui(mp, s); return *this; }
__gmp_expr & operator=(signed long int l)
{ mpz_set_si(mp, l); return *this; }
__gmp_expr & operator=(unsigned long int l)
{ mpz_set_ui(mp, l); return *this; }
__gmp_expr & operator=(float f) { mpz_set_d(mp, f); return *this; }
__gmp_expr & operator=(double d) { mpz_set_d(mp, d); return *this; }
// __gmp_expr & operator=(long double ld)
// { mpz_set_ld(mp, ld); return *this; }
__gmp_expr & operator=(const char *s)
{
if (mpz_set_str (mp, s, 0) != 0)
throw std::invalid_argument ("mpz_set_str");
return *this;
}
__gmp_expr & operator=(const std::string &s)
{
if (mpz_set_str(mp, s.c_str(), 0) != 0)
throw std::invalid_argument ("mpz_set_str");
return *this;
}
// string input/output functions
int set_str(const char *s, int base)
{ return mpz_set_str(mp, s, base); }
int set_str(const std::string &s, int base)
{ return mpz_set_str(mp, s.c_str(), base); }
std::string get_str(int base = 10) const
{
__gmp_alloc_cstring temp(mpz_get_str(0, base, mp));
return std::string(temp.str);
}
// conversion functions
mpz_srcptr __get_mp() const { return mp; }
mpz_ptr __get_mp() { return mp; }
mpz_srcptr get_mpz_t() const { return mp; }
mpz_ptr get_mpz_t() { return mp; }
signed long int get_si() const { return mpz_get_si(mp); }
unsigned long int get_ui() const { return mpz_get_ui(mp); }
double get_d() const { return mpz_get_d(mp); }
// bool fits_schar_p() const { return mpz_fits_schar_p(mp); }
// bool fits_uchar_p() const { return mpz_fits_uchar_p(mp); }
bool fits_sint_p() const { return mpz_fits_sint_p(mp); }
bool fits_uint_p() const { return mpz_fits_uint_p(mp); }
bool fits_sshort_p() const { return mpz_fits_sshort_p(mp); }
bool fits_ushort_p() const { return mpz_fits_ushort_p(mp); }
bool fits_slong_p() const { return mpz_fits_slong_p(mp); }
bool fits_ulong_p() const { return mpz_fits_ulong_p(mp); }
// bool fits_float_p() const { return mpz_fits_float_p(mp); }
// bool fits_double_p() const { return mpz_fits_double_p(mp); }
// bool fits_ldouble_p() const { return mpz_fits_ldouble_p(mp); }
// member operators
__GMP_DECLARE_COMPOUND_OPERATOR(operator+=)
__GMP_DECLARE_COMPOUND_OPERATOR(operator-=)
__GMP_DECLARE_COMPOUND_OPERATOR(operator*=)
__GMP_DECLARE_COMPOUND_OPERATOR(operator/=)
__GMP_DECLARE_COMPOUND_OPERATOR(operator%=)
__GMP_DECLARE_COMPOUND_OPERATOR(operator&=)
__GMP_DECLARE_COMPOUND_OPERATOR(operator|=)
__GMP_DECLARE_COMPOUND_OPERATOR(operator^=)
__GMP_DECLARE_COMPOUND_OPERATOR_UI(operator<<=)
__GMP_DECLARE_COMPOUND_OPERATOR_UI(operator>>=)
__GMP_DECLARE_INCREMENT_OPERATOR(operator++)
__GMP_DECLARE_INCREMENT_OPERATOR(operator--)
};
typedef __gmp_expr<mpz_t, mpz_t> mpz_class;
/**************** mpq_class -- wrapper for mpq_t ****************/
template <>
class __gmp_expr<mpq_t, mpq_t>
{
private:
typedef mpq_t value_type;
value_type mp;
public:
mp_bitcnt_t get_prec() const { return mpf_get_default_prec(); }
void canonicalize() { mpq_canonicalize(mp); }
// constructors and destructor
__gmp_expr() { mpq_init(mp); }
__gmp_expr(const __gmp_expr &q) { mpq_init(mp); mpq_set(mp, q.mp); }
template <class T, class U>
__gmp_expr(const __gmp_expr<T, U> &expr)
{ mpq_init(mp); __gmp_set_expr(mp, expr); }
__gmp_expr(signed char c) { mpq_init(mp); mpq_set_si(mp, c, 1); }
__gmp_expr(unsigned char c) { mpq_init(mp); mpq_set_ui(mp, c, 1); }
__gmp_expr(signed int i) { mpq_init(mp); mpq_set_si(mp, i, 1); }
__gmp_expr(unsigned int i) { mpq_init(mp); mpq_set_ui(mp, i, 1); }
__gmp_expr(signed short int s) { mpq_init(mp); mpq_set_si(mp, s, 1); }
__gmp_expr(unsigned short int s) { mpq_init(mp); mpq_set_ui(mp, s, 1); }
__gmp_expr(signed long int l) { mpq_init(mp); mpq_set_si(mp, l, 1); }
__gmp_expr(unsigned long int l) { mpq_init(mp); mpq_set_ui(mp, l, 1); }
__gmp_expr(float f) { mpq_init(mp); mpq_set_d(mp, f); }
__gmp_expr(double d) { mpq_init(mp); mpq_set_d(mp, d); }
// __gmp_expr(long double ld) { mpq_init(mp); mpq_set_ld(mp, ld); }
explicit __gmp_expr(const char *s)
{
mpq_init (mp);
if (mpq_set_str (mp, s, 0) != 0)
{
mpq_clear (mp);
throw std::invalid_argument ("mpq_set_str");
}
}
__gmp_expr(const char *s, int base)
{
mpq_init (mp);
if (mpq_set_str(mp, s, base) != 0)
{
mpq_clear (mp);
throw std::invalid_argument ("mpq_set_str");
}
}
explicit __gmp_expr(const std::string &s)
{
mpq_init (mp);
if (mpq_set_str (mp, s.c_str(), 0) != 0)
{
mpq_clear (mp);
throw std::invalid_argument ("mpq_set_str");
}
}
__gmp_expr(const std::string &s, int base)
{
mpq_init(mp);
if (mpq_set_str (mp, s.c_str(), base) != 0)
{
mpq_clear (mp);
throw std::invalid_argument ("mpq_set_str");
}
}
explicit __gmp_expr(mpq_srcptr q) { mpq_init(mp); mpq_set(mp, q); }
__gmp_expr(const mpz_class &num, const mpz_class &den)
{
mpq_init(mp);
mpz_set(mpq_numref(mp), num.get_mpz_t());
mpz_set(mpq_denref(mp), den.get_mpz_t());
}
~__gmp_expr() { mpq_clear(mp); }
// assignment operators
__gmp_expr & operator=(const __gmp_expr &q)
{ mpq_set(mp, q.mp); return *this; }
template <class T, class U>
__gmp_expr<value_type, value_type> & operator=(const __gmp_expr<T, U> &expr)
{ __gmp_set_expr(mp, expr); return *this; }
__gmp_expr & operator=(signed char c)
{ mpq_set_si(mp, c, 1); return *this; }
__gmp_expr & operator=(unsigned char c)
{ mpq_set_ui(mp, c, 1); return *this; }
__gmp_expr & operator=(signed int i) { mpq_set_si(mp, i, 1); return *this; }
__gmp_expr & operator=(unsigned int i)
{ mpq_set_ui(mp, i, 1); return *this; }
__gmp_expr & operator=(signed short int s)
{ mpq_set_si(mp, s, 1); return *this; }
__gmp_expr & operator=(unsigned short int s)
{ mpq_set_ui(mp, s, 1); return *this; }
__gmp_expr & operator=(signed long int l)
{ mpq_set_si(mp, l, 1); return *this; }
__gmp_expr & operator=(unsigned long int l)
{ mpq_set_ui(mp, l, 1); return *this; }
__gmp_expr & operator=(float f) { mpq_set_d(mp, f); return *this; }
__gmp_expr & operator=(double d) { mpq_set_d(mp, d); return *this; }
// __gmp_expr & operator=(long double ld)
// { mpq_set_ld(mp, ld); return *this; }
__gmp_expr & operator=(const char *s)
{
if (mpq_set_str (mp, s, 0) != 0)
throw std::invalid_argument ("mpq_set_str");
return *this;
}
__gmp_expr & operator=(const std::string &s)
{
if (mpq_set_str(mp, s.c_str(), 0) != 0)
throw std::invalid_argument ("mpq_set_str");
return *this;
}
// string input/output functions
int set_str(const char *s, int base)
{ return mpq_set_str(mp, s, base); }
int set_str(const std::string &s, int base)
{ return mpq_set_str(mp, s.c_str(), base); }
std::string get_str(int base = 10) const
{
__gmp_alloc_cstring temp(mpq_get_str(0, base, mp));
return std::string(temp.str);
}
// conversion functions
// casting a reference to an mpz_t to mpz_class & is a dirty hack,
// but works because the internal representation of mpz_class is
// exactly an mpz_t
const mpz_class & get_num() const
{ return reinterpret_cast<const mpz_class &>(*mpq_numref(mp)); }
mpz_class & get_num()
{ return reinterpret_cast<mpz_class &>(*mpq_numref(mp)); }
const mpz_class & get_den() const
{ return reinterpret_cast<const mpz_class &>(*mpq_denref(mp)); }
mpz_class & get_den()
{ return reinterpret_cast<mpz_class &>(*mpq_denref(mp)); }
mpq_srcptr __get_mp() const { return mp; }
mpq_ptr __get_mp() { return mp; }
mpq_srcptr get_mpq_t() const { return mp; }
mpq_ptr get_mpq_t() { return mp; }
mpz_srcptr get_num_mpz_t() const { return mpq_numref(mp); }
mpz_ptr get_num_mpz_t() { return mpq_numref(mp); }
mpz_srcptr get_den_mpz_t() const { return mpq_denref(mp); }
mpz_ptr get_den_mpz_t() { return mpq_denref(mp); }
double get_d() const { return mpq_get_d(mp); }
// compound assignments
__GMP_DECLARE_COMPOUND_OPERATOR(operator+=)
__GMP_DECLARE_COMPOUND_OPERATOR(operator-=)
__GMP_DECLARE_COMPOUND_OPERATOR(operator*=)
__GMP_DECLARE_COMPOUND_OPERATOR(operator/=)
__GMP_DECLARE_COMPOUND_OPERATOR_UI(operator<<=)
__GMP_DECLARE_COMPOUND_OPERATOR_UI(operator>>=)
__GMP_DECLARE_INCREMENT_OPERATOR(operator++)
__GMP_DECLARE_INCREMENT_OPERATOR(operator--)
};
typedef __gmp_expr<mpq_t, mpq_t> mpq_class;
/**************** mpf_class -- wrapper for mpf_t ****************/
template <>
class __gmp_expr<mpf_t, mpf_t>
{
private:
typedef mpf_t value_type;
value_type mp;
public:
mp_bitcnt_t get_prec() const { return mpf_get_prec(mp); }
void set_prec(mp_bitcnt_t prec) { mpf_set_prec(mp, prec); }
void set_prec_raw(mp_bitcnt_t prec) { mpf_set_prec_raw(mp, prec); }
// constructors and destructor
__gmp_expr() { mpf_init(mp); }
__gmp_expr(const __gmp_expr &f)
{ mpf_init2(mp, f.get_prec()); mpf_set(mp, f.mp); }
__gmp_expr(const __gmp_expr &f, mp_bitcnt_t prec)
{ mpf_init2(mp, prec); mpf_set(mp, f.mp); }
template <class T, class U>
__gmp_expr(const __gmp_expr<T, U> &expr)
{ mpf_init2(mp, expr.get_prec()); __gmp_set_expr(mp, expr); }
template <class T, class U>
__gmp_expr(const __gmp_expr<T, U> &expr, mp_bitcnt_t prec)
{ mpf_init2(mp, prec); __gmp_set_expr(mp, expr); }
__gmp_expr(signed char c) { mpf_init_set_si(mp, c); }
__gmp_expr(signed char c, mp_bitcnt_t prec)
{ mpf_init2(mp, prec); mpf_set_si(mp, c); }
__gmp_expr(unsigned char c) { mpf_init_set_ui(mp, c); }
__gmp_expr(unsigned char c, mp_bitcnt_t prec)
{ mpf_init2(mp, prec); mpf_set_ui(mp, c); }
__gmp_expr(signed int i) { mpf_init_set_si(mp, i); }
__gmp_expr(signed int i, mp_bitcnt_t prec)
{ mpf_init2(mp, prec); mpf_set_si(mp, i); }
__gmp_expr(unsigned int i) { mpf_init_set_ui(mp, i); }
__gmp_expr(unsigned int i, mp_bitcnt_t prec)
{ mpf_init2(mp, prec); mpf_set_ui(mp, i); }
__gmp_expr(signed short int s) { mpf_init_set_si(mp, s); }
__gmp_expr(signed short int s, mp_bitcnt_t prec)
{ mpf_init2(mp, prec); mpf_set_si(mp, s); }
__gmp_expr(unsigned short int s) { mpf_init_set_ui(mp, s); }
__gmp_expr(unsigned short int s, mp_bitcnt_t prec)
{ mpf_init2(mp, prec); mpf_set_ui(mp, s); }
__gmp_expr(signed long int l) { mpf_init_set_si(mp, l); }
__gmp_expr(signed long int l, mp_bitcnt_t prec)
{ mpf_init2(mp, prec); mpf_set_si(mp, l); }
__gmp_expr(unsigned long int l) { mpf_init_set_ui(mp, l); }
__gmp_expr(unsigned long int l, mp_bitcnt_t prec)
{ mpf_init2(mp, prec); mpf_set_ui(mp, l); }
__gmp_expr(float f) { mpf_init_set_d(mp, f); }
__gmp_expr(float f, mp_bitcnt_t prec)
{ mpf_init2(mp, prec); mpf_set_d(mp, f); }
__gmp_expr(double d) { mpf_init_set_d(mp, d); }
__gmp_expr(double d, mp_bitcnt_t prec)
{ mpf_init2(mp, prec); mpf_set_d(mp, d); }
// __gmp_expr(long double ld) { mpf_init_set_d(mp, ld); }
// __gmp_expr(long double ld, mp_bitcnt_t prec)
// { mpf_init2(mp, prec); mpf_set_d(mp, ld); }
explicit __gmp_expr(const char *s)
{
if (mpf_init_set_str (mp, s, 0) != 0)
{
mpf_clear (mp);
throw std::invalid_argument ("mpf_set_str");
}
}
__gmp_expr(const char *s, mp_bitcnt_t prec, int base = 0)
{
mpf_init2(mp, prec);
if (mpf_set_str(mp, s, base) != 0)
{
mpf_clear (mp);
throw std::invalid_argument ("mpf_set_str");
}
}
explicit __gmp_expr(const std::string &s)
{
if (mpf_init_set_str(mp, s.c_str(), 0) != 0)
{
mpf_clear (mp);
throw std::invalid_argument ("mpf_set_str");
}
}
__gmp_expr(const std::string &s, mp_bitcnt_t prec, int base = 0)
{
mpf_init2(mp, prec);
if (mpf_set_str(mp, s.c_str(), base) != 0)
{
mpf_clear (mp);
throw std::invalid_argument ("mpf_set_str");
}
}
explicit __gmp_expr(mpf_srcptr f)
{ mpf_init2(mp, mpf_get_prec(f)); mpf_set(mp, f); }
__gmp_expr(mpf_srcptr f, mp_bitcnt_t prec)
{ mpf_init2(mp, prec); mpf_set(mp, f); }
~__gmp_expr() { mpf_clear(mp); }
// assignment operators
__gmp_expr & operator=(const __gmp_expr &f)
{ mpf_set(mp, f.mp); return *this; }
template <class T, class U>
__gmp_expr<value_type, value_type> & operator=(const __gmp_expr<T, U> &expr)
{ __gmp_set_expr(mp, expr); return *this; }
__gmp_expr & operator=(signed char c) { mpf_set_si(mp, c); return *this; }
__gmp_expr & operator=(unsigned char c) { mpf_set_ui(mp, c); return *this; }
__gmp_expr & operator=(signed int i) { mpf_set_si(mp, i); return *this; }
__gmp_expr & operator=(unsigned int i) { mpf_set_ui(mp, i); return *this; }
__gmp_expr & operator=(signed short int s)
{ mpf_set_si(mp, s); return *this; }
__gmp_expr & operator=(unsigned short int s)
{ mpf_set_ui(mp, s); return *this; }
__gmp_expr & operator=(signed long int l)
{ mpf_set_si(mp, l); return *this; }
__gmp_expr & operator=(unsigned long int l)
{ mpf_set_ui(mp, l); return *this; }
__gmp_expr & operator=(float f) { mpf_set_d(mp, f); return *this; }
__gmp_expr & operator=(double d) { mpf_set_d(mp, d); return *this; }
// __gmp_expr & operator=(long double ld)
// { mpf_set_ld(mp, ld); return *this; }
__gmp_expr & operator=(const char *s)
{
if (mpf_set_str (mp, s, 0) != 0)
throw std::invalid_argument ("mpf_set_str");
return *this;
}
__gmp_expr & operator=(const std::string &s)
{
if (mpf_set_str(mp, s.c_str(), 0) != 0)
throw std::invalid_argument ("mpf_set_str");
return *this;
}
// string input/output functions
int set_str(const char *s, int base)
{ return mpf_set_str(mp, s, base); }
int set_str(const std::string &s, int base)
{ return mpf_set_str(mp, s.c_str(), base); }
std::string get_str(mp_exp_t &expo, int base = 10, size_t size = 0) const
{
__gmp_alloc_cstring temp(mpf_get_str(0, &expo, base, size, mp));
return std::string(temp.str);
}
// conversion functions
mpf_srcptr __get_mp() const { return mp; }
mpf_ptr __get_mp() { return mp; }
mpf_srcptr get_mpf_t() const { return mp; }
mpf_ptr get_mpf_t() { return mp; }
signed long int get_si() const { return mpf_get_si(mp); }
unsigned long int get_ui() const { return mpf_get_ui(mp); }
double get_d() const { return mpf_get_d(mp); }
// bool fits_schar_p() const { return mpf_fits_schar_p(mp); }
// bool fits_uchar_p() const { return mpf_fits_uchar_p(mp); }
bool fits_sint_p() const { return mpf_fits_sint_p(mp); }
bool fits_uint_p() const { return mpf_fits_uint_p(mp); }
bool fits_sshort_p() const { return mpf_fits_sshort_p(mp); }
bool fits_ushort_p() const { return mpf_fits_ushort_p(mp); }
bool fits_slong_p() const { return mpf_fits_slong_p(mp); }
bool fits_ulong_p() const { return mpf_fits_ulong_p(mp); }
// bool fits_float_p() const { return mpf_fits_float_p(mp); }
// bool fits_double_p() const { return mpf_fits_double_p(mp); }
// bool fits_ldouble_p() const { return mpf_fits_ldouble_p(mp); }
// compound assignments
__GMP_DECLARE_COMPOUND_OPERATOR(operator+=)
__GMP_DECLARE_COMPOUND_OPERATOR(operator-=)
__GMP_DECLARE_COMPOUND_OPERATOR(operator*=)
__GMP_DECLARE_COMPOUND_OPERATOR(operator/=)
__GMP_DECLARE_COMPOUND_OPERATOR_UI(operator<<=)
__GMP_DECLARE_COMPOUND_OPERATOR_UI(operator>>=)
__GMP_DECLARE_INCREMENT_OPERATOR(operator++)
__GMP_DECLARE_INCREMENT_OPERATOR(operator--)
};
typedef __gmp_expr<mpf_t, mpf_t> mpf_class;
/**************** I/O operators ****************/
// these should (and will) be provided separately
template <class T>
inline std::ostream & operator<<
(std::ostream &o, const __gmp_expr<T, T> &expr)
{
return o << expr.__get_mp();
}
template <class T, class U>
inline std::ostream & operator<<
(std::ostream &o, const __gmp_expr<T, U> &expr)
{
__gmp_expr<T, T> temp(expr);
return o << temp.__get_mp();
}
template <class T>
inline std::istream & operator>>(std::istream &i, __gmp_expr<T, T> &expr)
{
return i >> expr.__get_mp();
}
inline std::istream & operator>>(std::istream &i, mpq_class &q)
{
i >> q.get_mpq_t();
// q.canonicalize(); // you might want to uncomment this
return i;
}
/**************** Functions for type conversion ****************/
template <>
inline void __gmp_set_expr(mpz_ptr z, const mpz_class &w)
{
mpz_set(z, w.get_mpz_t());
}
template <class T>
inline void __gmp_set_expr(mpz_ptr z, const __gmp_expr<mpz_t, T> &expr)
{
expr.eval(z);
}
template <>
inline void __gmp_set_expr(mpz_ptr z, const mpq_class &q)
{
mpz_set_q(z, q.get_mpq_t());
}
template <class T>
inline void __gmp_set_expr(mpz_ptr z, const __gmp_expr<mpq_t, T> &expr)
{
mpq_class temp(expr);
mpz_set_q(z, temp.get_mpq_t());
}
template <class T>
inline void __gmp_set_expr(mpz_ptr z, const mpf_class &f)
{
mpz_set_f(z, f.get_mpf_t());
}
template <class T>
inline void __gmp_set_expr(mpz_ptr z, const __gmp_expr<mpf_t, T> &expr)
{
mpf_class temp(expr);
mpz_set_f(z, temp.get_mpf_t());
}
template <>
inline void __gmp_set_expr(mpq_ptr q, const mpz_class &z)
{
mpq_set_z(q, z.get_mpz_t());
}
template <class T>
inline void __gmp_set_expr(mpq_ptr q, const __gmp_expr<mpz_t, T> &expr)
{
mpz_class temp(expr);
mpq_set_z(q, temp.get_mpz_t());
}
template <>
inline void __gmp_set_expr(mpq_ptr q, const mpq_class &r)
{
mpq_set(q, r.get_mpq_t());
}
template <class T>
inline void __gmp_set_expr(mpq_ptr q, const __gmp_expr<mpq_t, T> &expr)
{
expr.eval(q);
}
template <class T>
inline void __gmp_set_expr(mpq_ptr q, const mpf_class &f)
{
mpq_set_f(q, f.get_mpf_t());
}
template <class T>
inline void __gmp_set_expr(mpq_ptr q, const __gmp_expr<mpf_t, T> &expr)
{
mpf_class temp(expr);
mpq_set_f(q, temp.get_mpf_t());
}
template <class T>
inline void __gmp_set_expr(mpf_ptr f, const mpz_class &z)
{
mpf_set_z(f, z.get_mpz_t());
}
template <class T>
inline void __gmp_set_expr(mpf_ptr f, const __gmp_expr<mpz_t, T> &expr)
{
mpz_class temp(expr);
mpf_set_z(f, temp.get_mpz_t());
}
template <class T>
inline void __gmp_set_expr(mpf_ptr f, const mpq_class &q)
{
mpf_set_q(f, q.get_mpq_t());
}
template <class T>
inline void __gmp_set_expr(mpf_ptr f, const __gmp_expr<mpq_t, T> &expr)
{
mpq_class temp(expr);
mpf_set_q(f, temp.get_mpq_t());
}
template <>
inline void __gmp_set_expr(mpf_ptr f, const mpf_class &g)
{
mpf_set(f, g.get_mpf_t());
}
template <class T>
inline void __gmp_set_expr(mpf_ptr f, const __gmp_expr<mpf_t, T> &expr)
{
expr.eval(f, mpf_get_prec(f));
}
/**************** Specializations of __gmp_expr ****************/
/* The eval() method of __gmp_expr<T, U> evaluates the corresponding
expression and assigns the result to its argument, which is either an
mpz_t, mpq_t, or mpf_t as specified by the T argument.
Compound expressions are evaluated recursively (temporaries are created
to hold intermediate values), while for simple expressions the eval()
method of the appropriate function object (available as the Op argument
of either __gmp_unary_expr<T, Op> or __gmp_binary_expr<T, U, Op>) is
called. */
/**************** Unary expressions ****************/
/* cases:
- simple: argument is mp*_class, that is, __gmp_expr<T, T>
- compound: argument is __gmp_expr<T, U> (with U not equal to T) */
// simple expressions
template <class T, class Op>
class __gmp_expr<T, __gmp_unary_expr<__gmp_expr<T, T>, Op> >
{
private:
typedef __gmp_expr<T, T> val_type;
__gmp_unary_expr<val_type, Op> expr;
public:
__gmp_expr(const val_type &val) : expr(val) { }
void eval(typename __gmp_resolve_expr<T>::ptr_type p,
unsigned long int = 0) const
{ Op::eval(p, expr.val.__get_mp()); }
const val_type & get_val() const { return expr.val; }
unsigned long int get_prec() const { return expr.val.get_prec(); }
};
// compound expressions
template <class T, class U, class Op>
class __gmp_expr<T, __gmp_unary_expr<__gmp_expr<T, U>, Op> >
{
private:
typedef __gmp_expr<T, U> val_type;
__gmp_unary_expr<val_type, Op> expr;
public:
__gmp_expr(const val_type &val) : expr(val) { }
void eval(typename __gmp_resolve_expr<T>::ptr_type p) const
{ __gmp_expr<T, T> temp(expr.val); Op::eval(p, temp.__get_mp()); }
void eval(typename __gmp_resolve_expr<T>::ptr_type p,
mp_bitcnt_t prec) const
{ __gmp_expr<T, T> temp(expr.val, prec); Op::eval(p, temp.__get_mp()); }
const val_type & get_val() const { return expr.val; }
unsigned long int get_prec() const { return expr.val.get_prec(); }
};
/**************** Binary expressions ****************/
/* simple:
- arguments are both mp*_class
- one argument is mp*_class, one is a built-in type
compound:
- one is mp*_class, one is __gmp_expr<T, U>
- one is __gmp_expr<T, U>, one is built-in
- both arguments are __gmp_expr<...> */
// simple expressions
template <class T, class Op>
class __gmp_expr
<T, __gmp_binary_expr<__gmp_expr<T, T>, __gmp_expr<T, T>, Op> >
{
private:
typedef __gmp_expr<T, T> val1_type;
typedef __gmp_expr<T, T> val2_type;
__gmp_binary_expr<val1_type, val2_type, Op> expr;
public:
__gmp_expr(const val1_type &val1, const val2_type &val2)
: expr(val1, val2) { }
void eval(typename __gmp_resolve_expr<T>::ptr_type p,
unsigned long int = 0) const
{ Op::eval(p, expr.val1.__get_mp(), expr.val2.__get_mp()); }
const val1_type & get_val1() const { return expr.val1; }
const val2_type & get_val2() const { return expr.val2; }
unsigned long int get_prec() const
{
mp_bitcnt_t prec1 = expr.val1.get_prec(),
prec2 = expr.val2.get_prec();
return (prec1 > prec2) ? prec1 : prec2;
}
};
// simple expressions, T is a built-in numerical type
template <class T, class U, class Op>
class __gmp_expr<T, __gmp_binary_expr<__gmp_expr<T, T>, U, Op> >
{
private:
typedef __gmp_expr<T, T> val1_type;
typedef U val2_type;
__gmp_binary_expr<val1_type, val2_type, Op> expr;
public:
__gmp_expr(const val1_type &val1, const val2_type &val2)
: expr(val1, val2) { }
void eval(typename __gmp_resolve_expr<T>::ptr_type p,
unsigned long int = 0) const
{ Op::eval(p, expr.val1.__get_mp(), expr.val2); }
const val1_type & get_val1() const { return expr.val1; }
const val2_type & get_val2() const { return expr.val2; }
unsigned long int get_prec() const { return expr.val1.get_prec(); }
};
template <class T, class U, class Op>
class __gmp_expr<T, __gmp_binary_expr<U, __gmp_expr<T, T>, Op> >
{
private:
typedef U val1_type;
typedef __gmp_expr<T, T> val2_type;
__gmp_binary_expr<val1_type, val2_type, Op> expr;
public:
__gmp_expr(const val1_type &val1, const val2_type &val2)
: expr(val1, val2) { }
void eval(typename __gmp_resolve_expr<T>::ptr_type p,
unsigned long int = 0) const
{ Op::eval(p, expr.val1, expr.val2.__get_mp()); }
const val1_type & get_val1() const { return expr.val1; }
const val2_type & get_val2() const { return expr.val2; }
unsigned long int get_prec() const { return expr.val2.get_prec(); }
};
// compound expressions, one argument is a subexpression
template <class T, class U, class V, class Op>
class __gmp_expr
<T, __gmp_binary_expr<__gmp_expr<T, T>, __gmp_expr<U, V>, Op> >
{
private:
typedef __gmp_expr<T, T> val1_type;
typedef __gmp_expr<U, V> val2_type;
__gmp_binary_expr<val1_type, val2_type, Op> expr;
public:
__gmp_expr(const val1_type &val1, const val2_type &val2)
: expr(val1, val2) { }
void eval(typename __gmp_resolve_expr<T>::ptr_type p) const
{
__gmp_expr<T, T> temp(expr.val2);
Op::eval(p, expr.val1.__get_mp(), temp.__get_mp());
}
void eval(typename __gmp_resolve_expr<T>::ptr_type p,
mp_bitcnt_t prec) const
{
__gmp_expr<T, T> temp(expr.val2, prec);
Op::eval(p, expr.val1.__get_mp(), temp.__get_mp());
}
const val1_type & get_val1() const { return expr.val1; }
const val2_type & get_val2() const { return expr.val2; }
unsigned long int get_prec() const
{
mp_bitcnt_t prec1 = expr.val1.get_prec(),
prec2 = expr.val2.get_prec();
return (prec1 > prec2) ? prec1 : prec2;
}
};
template <class T, class U, class V, class Op>
class __gmp_expr
<T, __gmp_binary_expr<__gmp_expr<U, V>, __gmp_expr<T, T>, Op> >
{
private:
typedef __gmp_expr<U, V> val1_type;
typedef __gmp_expr<T, T> val2_type;
__gmp_binary_expr<val1_type, val2_type, Op> expr;
public:
__gmp_expr(const val1_type &val1, const val2_type &val2)
: expr(val1, val2) { }
void eval(typename __gmp_resolve_expr<T>::ptr_type p) const
{
__gmp_expr<T, T> temp(expr.val1);
Op::eval(p, temp.__get_mp(), expr.val2.__get_mp());
}
void eval(typename __gmp_resolve_expr<T>::ptr_type p,
mp_bitcnt_t prec) const
{
__gmp_expr<T, T> temp(expr.val1, prec);
Op::eval(p, temp.__get_mp(), expr.val2.__get_mp());
}
const val1_type & get_val1() const { return expr.val1; }
const val2_type & get_val2() const { return expr.val2; }
unsigned long int get_prec() const
{
mp_bitcnt_t prec1 = expr.val1.get_prec(),
prec2 = expr.val2.get_prec();
return (prec1 > prec2) ? prec1 : prec2;
}
};
template <class T, class U, class Op>
class __gmp_expr
<T, __gmp_binary_expr<__gmp_expr<T, T>, __gmp_expr<T, U>, Op> >
{
private:
typedef __gmp_expr<T, T> val1_type;
typedef __gmp_expr<T, U> val2_type;
__gmp_binary_expr<val1_type, val2_type, Op> expr;
public:
__gmp_expr(const val1_type &val1, const val2_type &val2)
: expr(val1, val2) { }
void eval(typename __gmp_resolve_expr<T>::ptr_type p) const
{
__gmp_expr<T, T> temp(expr.val2);
Op::eval(p, expr.val1.__get_mp(), temp.__get_mp());
}
void eval(typename __gmp_resolve_expr<T>::ptr_type p,
mp_bitcnt_t prec) const
{
__gmp_expr<T, T> temp(expr.val2, prec);
Op::eval(p, expr.val1.__get_mp(), temp.__get_mp());
}
const val1_type & get_val1() const { return expr.val1; }
const val2_type & get_val2() const { return expr.val2; }
unsigned long int get_prec() const
{
mp_bitcnt_t prec1 = expr.val1.get_prec(),
prec2 = expr.val2.get_prec();
return (prec1 > prec2) ? prec1 : prec2;
}
};
template <class T, class U, class Op>
class __gmp_expr
<T, __gmp_binary_expr<__gmp_expr<T, U>, __gmp_expr<T, T>, Op> >
{
private:
typedef __gmp_expr<T, U> val1_type;
typedef __gmp_expr<T, T> val2_type;
__gmp_binary_expr<val1_type, val2_type, Op> expr;
public:
__gmp_expr(const val1_type &val1, const val2_type &val2)
: expr(val1, val2) { }
void eval(typename __gmp_resolve_expr<T>::ptr_type p) const
{
__gmp_expr<T, T> temp(expr.val1);
Op::eval(p, temp.__get_mp(), expr.val2.__get_mp());
}
void eval(typename __gmp_resolve_expr<T>::ptr_type p,
mp_bitcnt_t prec) const
{
__gmp_expr<T, T> temp(expr.val1, prec);
Op::eval(p, temp.__get_mp(), expr.val2.__get_mp());
}
const val1_type & get_val1() const { return expr.val1; }
const val2_type & get_val2() const { return expr.val2; }
unsigned long int get_prec() const
{
mp_bitcnt_t prec1 = expr.val1.get_prec(),
prec2 = expr.val2.get_prec();
return (prec1 > prec2) ? prec1 : prec2;
}
};
// one argument is a subexpression, one is a built-in
template <class T, class U, class V, class Op>
class __gmp_expr<T, __gmp_binary_expr<__gmp_expr<T, U>, V, Op> >
{
private:
typedef __gmp_expr<T, U> val1_type;
typedef V val2_type;
__gmp_binary_expr<val1_type, val2_type, Op> expr;
public:
__gmp_expr(const val1_type &val1, const val2_type &val2)
: expr(val1, val2) { }
void eval(typename __gmp_resolve_expr<T>::ptr_type p) const
{
__gmp_expr<T, T> temp(expr.val1);
Op::eval(p, temp.__get_mp(), expr.val2);
}
void eval(typename __gmp_resolve_expr<T>::ptr_type p,
mp_bitcnt_t prec) const
{
__gmp_expr<T, T> temp(expr.val1, prec);
Op::eval(p, temp.__get_mp(), expr.val2);
}
const val1_type & get_val1() const { return expr.val1; }
const val2_type & get_val2() const { return expr.val2; }
unsigned long int get_prec() const { return expr.val1.get_prec(); }
};
template <class T, class U, class V, class Op>
class __gmp_expr<T, __gmp_binary_expr<U, __gmp_expr<T, V>, Op> >
{
private:
typedef U val1_type;
typedef __gmp_expr<T, V> val2_type;
__gmp_binary_expr<val1_type, val2_type, Op> expr;
public:
__gmp_expr(const val1_type &val1, const val2_type &val2)
: expr(val1, val2) { }
void eval(typename __gmp_resolve_expr<T>::ptr_type p) const
{
__gmp_expr<T, T> temp(expr.val2);
Op::eval(p, expr.val1, temp.__get_mp());
}
void eval(typename __gmp_resolve_expr<T>::ptr_type p,
mp_bitcnt_t prec) const
{
__gmp_expr<T, T> temp(expr.val2, prec);
Op::eval(p, expr.val1, temp.__get_mp());
}
const val1_type & get_val1() const { return expr.val1; }
const val2_type & get_val2() const { return expr.val2; }
unsigned long int get_prec() const { return expr.val2.get_prec(); }
};
// both arguments are subexpressions
template <class T, class U, class V, class W, class Op>
class __gmp_expr
<T, __gmp_binary_expr<__gmp_expr<T, U>, __gmp_expr<V, W>, Op> >
{
private:
typedef __gmp_expr<T, U> val1_type;
typedef __gmp_expr<V, W> val2_type;
__gmp_binary_expr<val1_type, val2_type, Op> expr;
public:
__gmp_expr(const val1_type &val1, const val2_type &val2)
: expr(val1, val2) { }
void eval(typename __gmp_resolve_expr<T>::ptr_type p) const
{
__gmp_expr<T, T> temp1(expr.val1), temp2(expr.val2);
Op::eval(p, temp1.__get_mp(), temp2.__get_mp());
}
void eval(typename __gmp_resolve_expr<T>::ptr_type p,
mp_bitcnt_t prec) const
{
__gmp_expr<T, T> temp1(expr.val1, prec), temp2(expr.val2, prec);
Op::eval(p, temp1.__get_mp(), temp2.__get_mp());
}
const val1_type & get_val1() const { return expr.val1; }
const val2_type & get_val2() const { return expr.val2; }
unsigned long int get_prec() const
{
mp_bitcnt_t prec1 = expr.val1.get_prec(),
prec2 = expr.val2.get_prec();
return (prec1 > prec2) ? prec1 : prec2;
}
};
template <class T, class U, class V, class W, class Op>
class __gmp_expr
<T, __gmp_binary_expr<__gmp_expr<U, V>, __gmp_expr<T, W>, Op> >
{
private:
typedef __gmp_expr<U, V> val1_type;
typedef __gmp_expr<T, W> val2_type;
__gmp_binary_expr<val1_type, val2_type, Op> expr;
public:
__gmp_expr(const val1_type &val1, const val2_type &val2)
: expr(val1, val2) { }
void eval(typename __gmp_resolve_expr<T>::ptr_type p) const
{
__gmp_expr<T, T> temp1(expr.val1), temp2(expr.val2);
Op::eval(p, temp1.__get_mp(), temp2.__get_mp());
}
void eval(typename __gmp_resolve_expr<T>::ptr_type p,
mp_bitcnt_t prec) const
{
__gmp_expr<T, T> temp1(expr.val1, prec), temp2(expr.val2, prec);
Op::eval(p, temp1.__get_mp(), temp2.__get_mp());
}
const val1_type & get_val1() const { return expr.val1; }
const val2_type & get_val2() const { return expr.val2; }
unsigned long int get_prec() const
{
mp_bitcnt_t prec1 = expr.val1.get_prec(),
prec2 = expr.val2.get_prec();
return (prec1 > prec2) ? prec1 : prec2;
}
};
template <class T, class U, class V, class Op>
class __gmp_expr
<T, __gmp_binary_expr<__gmp_expr<T, U>, __gmp_expr<T, V>, Op> >
{
private:
typedef __gmp_expr<T, U> val1_type;
typedef __gmp_expr<T, V> val2_type;
__gmp_binary_expr<val1_type, val2_type, Op> expr;
public:
__gmp_expr(const val1_type &val1, const val2_type &val2)
: expr(val1, val2) { }
void eval(typename __gmp_resolve_expr<T>::ptr_type p) const
{
__gmp_expr<T, T> temp1(expr.val1), temp2(expr.val2);
Op::eval(p, temp1.__get_mp(), temp2.__get_mp());
}
void eval(typename __gmp_resolve_expr<T>::ptr_type p,
mp_bitcnt_t prec) const
{
__gmp_expr<T, T> temp1(expr.val1, prec), temp2(expr.val2, prec);
Op::eval(p, temp1.__get_mp(), temp2.__get_mp());
}
const val1_type & get_val1() const { return expr.val1; }
const val2_type & get_val2() const { return expr.val2; }
unsigned long int get_prec() const
{
mp_bitcnt_t prec1 = expr.val1.get_prec(),
prec2 = expr.val2.get_prec();
return (prec1 > prec2) ? prec1 : prec2;
}
};
/**************** Special cases ****************/
/* Some operations (i.e., add and subtract) with mixed mpz/mpq arguments
can be done directly without first converting the mpz to mpq.
Appropriate specializations of __gmp_expr are required. */
#define __GMPZQ_DEFINE_EXPR(eval_fun) \
\
template <> \
class __gmp_expr<mpq_t, __gmp_binary_expr<mpz_class, mpq_class, eval_fun> > \
{ \
private: \
typedef mpz_class val1_type; \
typedef mpq_class val2_type; \
\
__gmp_binary_expr<val1_type, val2_type, eval_fun> expr; \
public: \
__gmp_expr(const val1_type &val1, const val2_type &val2) \
: expr(val1, val2) { } \
void eval(mpq_ptr q) const \
{ eval_fun::eval(q, expr.val1.get_mpz_t(), expr.val2.get_mpq_t()); } \
const val1_type & get_val1() const { return expr.val1; } \
const val2_type & get_val2() const { return expr.val2; } \
unsigned long int get_prec() const { return mpf_get_default_prec(); } \
}; \
\
template <> \
class __gmp_expr<mpq_t, __gmp_binary_expr<mpq_class, mpz_class, eval_fun> > \
{ \
private: \
typedef mpq_class val1_type; \
typedef mpz_class val2_type; \
\
__gmp_binary_expr<val1_type, val2_type, eval_fun> expr; \
public: \
__gmp_expr(const val1_type &val1, const val2_type &val2) \
: expr(val1, val2) { } \
void eval(mpq_ptr q) const \
{ eval_fun::eval(q, expr.val1.get_mpq_t(), expr.val2.get_mpz_t()); } \
const val1_type & get_val1() const { return expr.val1; } \
const val2_type & get_val2() const { return expr.val2; } \
unsigned long int get_prec() const { return mpf_get_default_prec(); } \
}; \
\
template <class T> \
class __gmp_expr \
<mpq_t, __gmp_binary_expr<mpz_class, __gmp_expr<mpq_t, T>, eval_fun> > \
{ \
private: \
typedef mpz_class val1_type; \
typedef __gmp_expr<mpq_t, T> val2_type; \
\
__gmp_binary_expr<val1_type, val2_type, eval_fun> expr; \
public: \
__gmp_expr(const val1_type &val1, const val2_type &val2) \
: expr(val1, val2) { } \
void eval(mpq_ptr q) const \
{ \
mpq_class temp(expr.val2); \
eval_fun::eval(q, expr.val1.get_mpz_t(), temp.get_mpq_t()); \
} \
const val1_type & get_val1() const { return expr.val1; } \
const val2_type & get_val2() const { return expr.val2; } \
unsigned long int get_prec() const { return mpf_get_default_prec(); } \
}; \
\
template <class T> \
class __gmp_expr \
<mpq_t, __gmp_binary_expr<mpq_class, __gmp_expr<mpz_t, T>, eval_fun> > \
{ \
private: \
typedef mpq_class val1_type; \
typedef __gmp_expr<mpz_t, T> val2_type; \
\
__gmp_binary_expr<val1_type, val2_type, eval_fun> expr; \
public: \
__gmp_expr(const val1_type &val1, const val2_type &val2) \
: expr(val1, val2) { } \
void eval(mpq_ptr q) const \
{ \
mpz_class temp(expr.val2); \
eval_fun::eval(q, expr.val1.get_mpq_t(), temp.get_mpz_t()); \
} \
const val1_type & get_val1() const { return expr.val1; } \
const val2_type & get_val2() const { return expr.val2; } \
unsigned long int get_prec() const { return mpf_get_default_prec(); } \
}; \
\
template <class T> \
class __gmp_expr \
<mpq_t, __gmp_binary_expr<__gmp_expr<mpz_t, T>, mpq_class, eval_fun> > \
{ \
private: \
typedef __gmp_expr<mpz_t, T> val1_type; \
typedef mpq_class val2_type; \
\
__gmp_binary_expr<val1_type, val2_type, eval_fun> expr; \
public: \
__gmp_expr(const val1_type &val1, const val2_type &val2) \
: expr(val1, val2) { } \
void eval(mpq_ptr q) const \
{ \
mpz_class temp(expr.val1); \
eval_fun::eval(q, temp.get_mpz_t(), expr.val2.get_mpq_t()); \
} \
const val1_type & get_val1() const { return expr.val1; } \
const val2_type & get_val2() const { return expr.val2; } \
unsigned long int get_prec() const { return mpf_get_default_prec(); } \
}; \
\
template <class T> \
class __gmp_expr \
<mpq_t, __gmp_binary_expr<__gmp_expr<mpq_t, T>, mpz_class, eval_fun> > \
{ \
private: \
typedef __gmp_expr<mpq_t, T> val1_type; \
typedef mpz_class val2_type; \
\
__gmp_binary_expr<val1_type, val2_type, eval_fun> expr; \
public: \
__gmp_expr(const val1_type &val1, const val2_type &val2) \
: expr(val1, val2) { } \
void eval(mpq_ptr q) const \
{ \
mpq_class temp(expr.val1); \
eval_fun::eval(q, temp.get_mpq_t(), expr.val2.get_mpz_t()); \
} \
const val1_type & get_val1() const { return expr.val1; } \
const val2_type & get_val2() const { return expr.val2; } \
unsigned long int get_prec() const { return mpf_get_default_prec(); } \
}; \
\
template <class T, class U> \
class __gmp_expr<mpq_t, __gmp_binary_expr \
<__gmp_expr<mpz_t, T>, __gmp_expr<mpq_t, U>, eval_fun> > \
{ \
private: \
typedef __gmp_expr<mpz_t, T> val1_type; \
typedef __gmp_expr<mpq_t, U> val2_type; \
\
__gmp_binary_expr<val1_type, val2_type, eval_fun> expr; \
public: \
__gmp_expr(const val1_type &val1, const val2_type &val2) \
: expr(val1, val2) { } \
void eval(mpq_ptr q) const \
{ \
mpz_class temp1(expr.val1); \
mpq_class temp2(expr.val2); \
eval_fun::eval(q, temp1.get_mpz_t(), temp2.get_mpq_t()); \
} \
const val1_type & get_val1() const { return expr.val1; } \
const val2_type & get_val2() const { return expr.val2; } \
unsigned long int get_prec() const { return mpf_get_default_prec(); } \
}; \
\
template <class T, class U> \
class __gmp_expr<mpq_t, __gmp_binary_expr \
<__gmp_expr<mpq_t, T>, __gmp_expr<mpz_t, U>, eval_fun> > \
{ \
private: \
typedef __gmp_expr<mpq_t, T> val1_type; \
typedef __gmp_expr<mpz_t, U> val2_type; \
\
__gmp_binary_expr<val1_type, val2_type, eval_fun> expr; \
public: \
__gmp_expr(const val1_type &val1, const val2_type &val2) \
: expr(val1, val2) { } \
void eval(mpq_ptr q) const \
{ \
mpq_class temp1(expr.val1); \
mpz_class temp2(expr.val2); \
eval_fun::eval(q, temp1.get_mpq_t(), temp2.get_mpz_t()); \
} \
const val1_type & get_val1() const { return expr.val1; } \
const val2_type & get_val2() const { return expr.val2; } \
unsigned long int get_prec() const { return mpf_get_default_prec(); } \
};
__GMPZQ_DEFINE_EXPR(__gmp_binary_plus)
__GMPZQ_DEFINE_EXPR(__gmp_binary_minus)
/**************** Macros for defining functions ****************/
/* Results of operators and functions are instances of __gmp_expr<T, U>.
T determines the numerical type of the expression: it can be either
mpz_t, mpq_t, or mpf_t. When the arguments of a binary
expression have different numerical types, __gmp_resolve_expr is used
to determine the "larger" type.
U is either __gmp_unary_expr<V, Op> or __gmp_binary_expr<V, W, Op>,
where V and W are the arguments' types -- they can in turn be
expressions, thus allowing to build compound expressions to any
degree of complexity.
Op is a function object that must have an eval() method accepting
appropriate arguments.
Actual evaluation of a __gmp_expr<T, U> object is done when it gets
assigned to an mp*_class ("lazy" evaluation): this is done by calling
its eval() method. */
// non-member unary operators and functions
#define __GMP_DEFINE_UNARY_FUNCTION(fun, eval_fun) \
\
template <class T, class U> \
inline __gmp_expr<T, __gmp_unary_expr<__gmp_expr<T, U>, eval_fun> > \
fun(const __gmp_expr<T, U> &expr) \
{ \
return __gmp_expr<T, __gmp_unary_expr<__gmp_expr<T, U>, eval_fun> >(expr); \
}
#define __GMP_DEFINE_UNARY_TYPE_FUNCTION(type, fun, eval_fun) \
\
template <class T, class U> \
inline type fun(const __gmp_expr<T, U> &expr) \
{ \
typename __gmp_resolve_temp<T, T, U>::temp_type temp(expr); \
return eval_fun::eval(temp.__get_mp()); \
}
// non-member binary operators and functions
#define __GMPP_DEFINE_BINARY_FUNCTION(fun, eval_fun) \
\
template <class T, class U, class V, class W> \
inline __gmp_expr<typename __gmp_resolve_expr<T, V>::value_type, \
__gmp_binary_expr<__gmp_expr<T, U>, __gmp_expr<V, W>, eval_fun> > \
fun(const __gmp_expr<T, U> &expr1, const __gmp_expr<V, W> &expr2) \
{ \
return __gmp_expr<typename __gmp_resolve_expr<T, V>::value_type, \
__gmp_binary_expr<__gmp_expr<T, U>, __gmp_expr<V, W>, eval_fun> > \
(expr1, expr2); \
}
#define __GMPNN_DEFINE_BINARY_FUNCTION(fun, eval_fun, type, bigtype) \
\
template <class T, class U> \
inline __gmp_expr \
<T, __gmp_binary_expr<__gmp_expr<T, U>, bigtype, eval_fun> > \
fun(const __gmp_expr<T, U> &expr, type t) \
{ \
return __gmp_expr \
<T, __gmp_binary_expr<__gmp_expr<T, U>, bigtype, eval_fun> >(expr, t); \
} \
\
template <class T, class U> \
inline __gmp_expr \
<T, __gmp_binary_expr<bigtype, __gmp_expr<T, U>, eval_fun> > \
fun(type t, const __gmp_expr<T, U> &expr) \
{ \
return __gmp_expr \
<T, __gmp_binary_expr<bigtype, __gmp_expr<T, U>, eval_fun> >(t, expr); \
}
#define __GMPNS_DEFINE_BINARY_FUNCTION(fun, eval_fun, type) \
__GMPNN_DEFINE_BINARY_FUNCTION(fun, eval_fun, type, signed long int)
#define __GMPNU_DEFINE_BINARY_FUNCTION(fun, eval_fun, type) \
__GMPNN_DEFINE_BINARY_FUNCTION(fun, eval_fun, type, unsigned long int)
#define __GMPND_DEFINE_BINARY_FUNCTION(fun, eval_fun, type) \
__GMPNN_DEFINE_BINARY_FUNCTION(fun, eval_fun, type, double)
#define __GMPNLD_DEFINE_BINARY_FUNCTION(fun, eval_fun, type) \
__GMPNN_DEFINE_BINARY_FUNCTION(fun, eval_fun, type, long double)
#define __GMPN_DEFINE_BINARY_FUNCTION(fun, eval_fun) \
__GMPNS_DEFINE_BINARY_FUNCTION(fun, eval_fun, signed char) \
__GMPNU_DEFINE_BINARY_FUNCTION(fun, eval_fun, unsigned char) \
__GMPNS_DEFINE_BINARY_FUNCTION(fun, eval_fun, signed int) \
__GMPNU_DEFINE_BINARY_FUNCTION(fun, eval_fun, unsigned int) \
__GMPNS_DEFINE_BINARY_FUNCTION(fun, eval_fun, signed short int) \
__GMPNU_DEFINE_BINARY_FUNCTION(fun, eval_fun, unsigned short int) \
__GMPNS_DEFINE_BINARY_FUNCTION(fun, eval_fun, signed long int) \
__GMPNU_DEFINE_BINARY_FUNCTION(fun, eval_fun, unsigned long int) \
__GMPND_DEFINE_BINARY_FUNCTION(fun, eval_fun, float) \
__GMPND_DEFINE_BINARY_FUNCTION(fun, eval_fun, double) \
__GMPNLD_DEFINE_BINARY_FUNCTION(fun, eval_fun, long double)
#define __GMP_DEFINE_BINARY_FUNCTION(fun, eval_fun) \
__GMPP_DEFINE_BINARY_FUNCTION(fun, eval_fun) \
__GMPN_DEFINE_BINARY_FUNCTION(fun, eval_fun)
#define __GMP_DEFINE_BINARY_FUNCTION_UI(fun, eval_fun) \
\
template <class T, class U> \
inline __gmp_expr \
<T, __gmp_binary_expr<__gmp_expr<T, U>, unsigned long int, eval_fun> > \
fun(const __gmp_expr<T, U> &expr, unsigned long int l) \
{ \
return __gmp_expr<T, __gmp_binary_expr \
<__gmp_expr<T, U>, unsigned long int, eval_fun> >(expr, l); \
}
#define __GMPP_DEFINE_BINARY_TYPE_FUNCTION(type, fun, eval_fun) \
\
template <class T, class U, class V, class W> \
inline type fun(const __gmp_expr<T, U> &expr1, \
const __gmp_expr<V, W> &expr2) \
{ \
typedef typename __gmp_resolve_expr<T, V>::value_type eval_type; \
typename __gmp_resolve_temp<eval_type, T, U>::temp_type temp1(expr1); \
typename __gmp_resolve_temp<eval_type, V, W>::temp_type temp2(expr2); \
return eval_fun::eval(temp1.__get_mp(), temp2.__get_mp()); \
}
#define __GMPNN_DEFINE_BINARY_TYPE_FUNCTION(type, fun, eval_fun, \
type2, bigtype) \
\
template <class T, class U> \
inline type fun(const __gmp_expr<T, U> &expr, type2 t) \
{ \
typename __gmp_resolve_temp<T, T, U>::temp_type temp(expr); \
return eval_fun::eval(temp.__get_mp(), static_cast<bigtype>(t)); \
} \
\
template <class T, class U> \
inline type fun(type2 t, const __gmp_expr<T, U> &expr) \
{ \
typename __gmp_resolve_temp<T, T, U>::temp_type temp(expr); \
return eval_fun::eval(static_cast<bigtype>(t), temp.__get_mp()); \
}
#define __GMPNS_DEFINE_BINARY_TYPE_FUNCTION(type, fun, eval_fun, type2) \
__GMPNN_DEFINE_BINARY_TYPE_FUNCTION(type, fun, eval_fun, \
type2, signed long int)
#define __GMPNU_DEFINE_BINARY_TYPE_FUNCTION(type, fun, eval_fun, type2) \
__GMPNN_DEFINE_BINARY_TYPE_FUNCTION(type, fun, eval_fun, \
type2, unsigned long int)
#define __GMPND_DEFINE_BINARY_TYPE_FUNCTION(type, fun, eval_fun, type2) \
__GMPNN_DEFINE_BINARY_TYPE_FUNCTION(type, fun, eval_fun, type2, double)
#define __GMPNLD_DEFINE_BINARY_TYPE_FUNCTION(type, fun, eval_fun, type2) \
__GMPNN_DEFINE_BINARY_TYPE_FUNCTION(type, fun, eval_fun, type2, long double)
#define __GMPN_DEFINE_BINARY_TYPE_FUNCTION(type, fun, eval_fun) \
__GMPNS_DEFINE_BINARY_TYPE_FUNCTION(type, fun, eval_fun, signed char) \
__GMPNU_DEFINE_BINARY_TYPE_FUNCTION(type, fun, eval_fun, unsigned char) \
__GMPNS_DEFINE_BINARY_TYPE_FUNCTION(type, fun, eval_fun, signed int) \
__GMPNU_DEFINE_BINARY_TYPE_FUNCTION(type, fun, eval_fun, unsigned int) \
__GMPNS_DEFINE_BINARY_TYPE_FUNCTION(type, fun, eval_fun, signed short int) \
__GMPNU_DEFINE_BINARY_TYPE_FUNCTION(type, fun, eval_fun, unsigned short int) \
__GMPNS_DEFINE_BINARY_TYPE_FUNCTION(type, fun, eval_fun, signed long int) \
__GMPNU_DEFINE_BINARY_TYPE_FUNCTION(type, fun, eval_fun, unsigned long int) \
__GMPND_DEFINE_BINARY_TYPE_FUNCTION(type, fun, eval_fun, float) \
__GMPND_DEFINE_BINARY_TYPE_FUNCTION(type, fun, eval_fun, double) \
__GMPNLD_DEFINE_BINARY_TYPE_FUNCTION(type, fun, eval_fun, long double)
#define __GMP_DEFINE_BINARY_TYPE_FUNCTION(type, fun, eval_fun) \
__GMPP_DEFINE_BINARY_TYPE_FUNCTION(type, fun, eval_fun) \
__GMPN_DEFINE_BINARY_TYPE_FUNCTION(type, fun, eval_fun)
// member operators
#define __GMPP_DEFINE_COMPOUND_OPERATOR(type, fun, eval_fun) \
\
template <class T, class U> \
inline type##_class & type##_class::fun(const __gmp_expr<T, U> &expr) \
{ \
__gmp_set_expr(mp, __gmp_expr<type##_t, __gmp_binary_expr \
<type##_class, __gmp_expr<T, U>, eval_fun> >(*this, expr)); \
return *this; \
}
#define __GMPNN_DEFINE_COMPOUND_OPERATOR(type, fun, eval_fun, \
type2, bigtype) \
\
inline type##_class & type##_class::fun(type2 t) \
{ \
__gmp_set_expr(mp, __gmp_expr<type##_t, __gmp_binary_expr \
<type##_class, bigtype, eval_fun> >(*this, t)); \
return *this; \
}
#define __GMPNS_DEFINE_COMPOUND_OPERATOR(type, fun, eval_fun, type2) \
__GMPNN_DEFINE_COMPOUND_OPERATOR(type, fun, eval_fun, \
type2, signed long int)
#define __GMPNU_DEFINE_COMPOUND_OPERATOR(type, fun, eval_fun, type2) \
__GMPNN_DEFINE_COMPOUND_OPERATOR(type, fun, eval_fun, \
type2, unsigned long int)
#define __GMPND_DEFINE_COMPOUND_OPERATOR(type, fun, eval_fun, type2) \
__GMPNN_DEFINE_COMPOUND_OPERATOR(type, fun, eval_fun, type2, double)
#define __GMPNLD_DEFINE_COMPOUND_OPERATOR(type, fun, eval_fun, type2) \
__GMPNN_DEFINE_COMPOUND_OPERATOR(type, fun, eval_fun, type2, long double)
#define __GMPN_DEFINE_COMPOUND_OPERATOR(type, fun, eval_fun) \
__GMPNS_DEFINE_COMPOUND_OPERATOR(type, fun, eval_fun, signed char) \
__GMPNU_DEFINE_COMPOUND_OPERATOR(type, fun, eval_fun, unsigned char) \
__GMPNS_DEFINE_COMPOUND_OPERATOR(type, fun, eval_fun, signed int) \
__GMPNU_DEFINE_COMPOUND_OPERATOR(type, fun, eval_fun, unsigned int) \
__GMPNS_DEFINE_COMPOUND_OPERATOR(type, fun, eval_fun, signed short int) \
__GMPNU_DEFINE_COMPOUND_OPERATOR(type, fun, eval_fun, unsigned short int) \
__GMPNS_DEFINE_COMPOUND_OPERATOR(type, fun, eval_fun, signed long int) \
__GMPNU_DEFINE_COMPOUND_OPERATOR(type, fun, eval_fun, unsigned long int) \
__GMPND_DEFINE_COMPOUND_OPERATOR(type, fun, eval_fun, float) \
__GMPND_DEFINE_COMPOUND_OPERATOR(type, fun, eval_fun, double) \
/* __GMPNLD_DEFINE_COMPOUND_OPERATOR(type, fun, eval_fun, long double) */
#define __GMP_DEFINE_COMPOUND_OPERATOR(type, fun, eval_fun) \
__GMPP_DEFINE_COMPOUND_OPERATOR(type, fun, eval_fun) \
__GMPN_DEFINE_COMPOUND_OPERATOR(type, fun, eval_fun)
#define __GMPZ_DEFINE_COMPOUND_OPERATOR(fun, eval_fun) \
__GMP_DEFINE_COMPOUND_OPERATOR(mpz, fun, eval_fun)
#define __GMPQ_DEFINE_COMPOUND_OPERATOR(fun, eval_fun) \
__GMP_DEFINE_COMPOUND_OPERATOR(mpq, fun, eval_fun)
#define __GMPF_DEFINE_COMPOUND_OPERATOR(fun, eval_fun) \
__GMP_DEFINE_COMPOUND_OPERATOR(mpf, fun, eval_fun)
#define __GMP_DEFINE_COMPOUND_OPERATOR_UI(type, fun, eval_fun) \
\
inline type##_class & type##_class::fun(unsigned long int l) \
{ \
__gmp_set_expr(mp, __gmp_expr<type##_t, __gmp_binary_expr \
<type##_class, unsigned long int, eval_fun> >(*this, l)); \
return *this; \
}
#define __GMPZ_DEFINE_COMPOUND_OPERATOR_UI(fun, eval_fun) \
__GMP_DEFINE_COMPOUND_OPERATOR_UI(mpz, fun, eval_fun)
#define __GMPQ_DEFINE_COMPOUND_OPERATOR_UI(fun, eval_fun) \
__GMP_DEFINE_COMPOUND_OPERATOR_UI(mpq, fun, eval_fun)
#define __GMPF_DEFINE_COMPOUND_OPERATOR_UI(fun, eval_fun) \
__GMP_DEFINE_COMPOUND_OPERATOR_UI(mpf, fun, eval_fun)
#define __GMP_DEFINE_INCREMENT_OPERATOR(type, fun, eval_fun) \
\
inline type##_class & type##_class::fun() \
{ \
eval_fun::eval(mp); \
return *this; \
} \
\
inline type##_class type##_class::fun(int) \
{ \
type##_class temp(*this); \
eval_fun::eval(mp); \
return temp; \
}
#define __GMPZ_DEFINE_INCREMENT_OPERATOR(fun, eval_fun) \
__GMP_DEFINE_INCREMENT_OPERATOR(mpz, fun, eval_fun)
#define __GMPQ_DEFINE_INCREMENT_OPERATOR(fun, eval_fun) \
__GMP_DEFINE_INCREMENT_OPERATOR(mpq, fun, eval_fun)
#define __GMPF_DEFINE_INCREMENT_OPERATOR(fun, eval_fun) \
__GMP_DEFINE_INCREMENT_OPERATOR(mpf, fun, eval_fun)
/**************** Arithmetic operators and functions ****************/
// non-member operators and functions
__GMP_DEFINE_UNARY_FUNCTION(operator+, __gmp_unary_plus)
__GMP_DEFINE_UNARY_FUNCTION(operator-, __gmp_unary_minus)
__GMP_DEFINE_UNARY_FUNCTION(operator~, __gmp_unary_com)
__GMP_DEFINE_BINARY_FUNCTION(operator+, __gmp_binary_plus)
__GMP_DEFINE_BINARY_FUNCTION(operator-, __gmp_binary_minus)
__GMP_DEFINE_BINARY_FUNCTION(operator*, __gmp_binary_multiplies)
__GMP_DEFINE_BINARY_FUNCTION(operator/, __gmp_binary_divides)
__GMP_DEFINE_BINARY_FUNCTION(operator%, __gmp_binary_modulus)
__GMP_DEFINE_BINARY_FUNCTION(operator&, __gmp_binary_and)
__GMP_DEFINE_BINARY_FUNCTION(operator|, __gmp_binary_ior)
__GMP_DEFINE_BINARY_FUNCTION(operator^, __gmp_binary_xor)
__GMP_DEFINE_BINARY_FUNCTION_UI(operator<<, __gmp_binary_lshift)
__GMP_DEFINE_BINARY_FUNCTION_UI(operator>>, __gmp_binary_rshift)
__GMP_DEFINE_BINARY_TYPE_FUNCTION(bool, operator==, __gmp_binary_equal)
__GMP_DEFINE_BINARY_TYPE_FUNCTION(bool, operator!=, __gmp_binary_not_equal)
__GMP_DEFINE_BINARY_TYPE_FUNCTION(bool, operator<, __gmp_binary_less)
__GMP_DEFINE_BINARY_TYPE_FUNCTION(bool, operator<=, __gmp_binary_less_equal)
__GMP_DEFINE_BINARY_TYPE_FUNCTION(bool, operator>, __gmp_binary_greater)
__GMP_DEFINE_BINARY_TYPE_FUNCTION(bool, operator>=, \
__gmp_binary_greater_equal)
__GMP_DEFINE_UNARY_FUNCTION(abs, __gmp_abs_function)
__GMP_DEFINE_UNARY_FUNCTION(trunc, __gmp_trunc_function)
__GMP_DEFINE_UNARY_FUNCTION(floor, __gmp_floor_function)
__GMP_DEFINE_UNARY_FUNCTION(ceil, __gmp_ceil_function)
__GMP_DEFINE_UNARY_FUNCTION(sqrt, __gmp_sqrt_function)
__GMP_DEFINE_BINARY_FUNCTION(hypot, __gmp_hypot_function)
__GMP_DEFINE_UNARY_TYPE_FUNCTION(int, sgn, __gmp_sgn_function)
__GMP_DEFINE_BINARY_TYPE_FUNCTION(int, cmp, __gmp_cmp_function)
// member operators for mpz_class
__GMPZ_DEFINE_COMPOUND_OPERATOR(operator+=, __gmp_binary_plus)
__GMPZ_DEFINE_COMPOUND_OPERATOR(operator-=, __gmp_binary_minus)
__GMPZ_DEFINE_COMPOUND_OPERATOR(operator*=, __gmp_binary_multiplies)
__GMPZ_DEFINE_COMPOUND_OPERATOR(operator/=, __gmp_binary_divides)
__GMPZ_DEFINE_COMPOUND_OPERATOR(operator%=, __gmp_binary_modulus)
__GMPZ_DEFINE_COMPOUND_OPERATOR(operator&=, __gmp_binary_and)
__GMPZ_DEFINE_COMPOUND_OPERATOR(operator|=, __gmp_binary_ior)
__GMPZ_DEFINE_COMPOUND_OPERATOR(operator^=, __gmp_binary_xor)
__GMPZ_DEFINE_COMPOUND_OPERATOR_UI(operator<<=, __gmp_binary_lshift)
__GMPZ_DEFINE_COMPOUND_OPERATOR_UI(operator>>=, __gmp_binary_rshift)
__GMPZ_DEFINE_INCREMENT_OPERATOR(operator++, __gmp_unary_increment)
__GMPZ_DEFINE_INCREMENT_OPERATOR(operator--, __gmp_unary_decrement)
// member operators for mpq_class
__GMPQ_DEFINE_COMPOUND_OPERATOR(operator+=, __gmp_binary_plus)
__GMPQ_DEFINE_COMPOUND_OPERATOR(operator-=, __gmp_binary_minus)
__GMPQ_DEFINE_COMPOUND_OPERATOR(operator*=, __gmp_binary_multiplies)
__GMPQ_DEFINE_COMPOUND_OPERATOR(operator/=, __gmp_binary_divides)
__GMPQ_DEFINE_COMPOUND_OPERATOR_UI(operator<<=, __gmp_binary_lshift)
__GMPQ_DEFINE_COMPOUND_OPERATOR_UI(operator>>=, __gmp_binary_rshift)
__GMPQ_DEFINE_INCREMENT_OPERATOR(operator++, __gmp_unary_increment)
__GMPQ_DEFINE_INCREMENT_OPERATOR(operator--, __gmp_unary_decrement)
// member operators for mpf_class
__GMPF_DEFINE_COMPOUND_OPERATOR(operator+=, __gmp_binary_plus)
__GMPF_DEFINE_COMPOUND_OPERATOR(operator-=, __gmp_binary_minus)
__GMPF_DEFINE_COMPOUND_OPERATOR(operator*=, __gmp_binary_multiplies)
__GMPF_DEFINE_COMPOUND_OPERATOR(operator/=, __gmp_binary_divides)
__GMPF_DEFINE_COMPOUND_OPERATOR_UI(operator<<=, __gmp_binary_lshift)
__GMPF_DEFINE_COMPOUND_OPERATOR_UI(operator>>=, __gmp_binary_rshift)
__GMPF_DEFINE_INCREMENT_OPERATOR(operator++, __gmp_unary_increment)
__GMPF_DEFINE_INCREMENT_OPERATOR(operator--, __gmp_unary_decrement)
/**************** Class wrapper for gmp_randstate_t ****************/
class __gmp_urandomb_value { };
class __gmp_urandomm_value { };
template <>
class __gmp_expr<mpz_t, __gmp_urandomb_value>
{
private:
__gmp_randstate_struct *state;
unsigned long int bits;
public:
__gmp_expr(gmp_randstate_t s, unsigned long int l) : state(s), bits(l) { }
void eval(mpz_ptr z) const { __gmp_rand_function::eval(z, state, bits); }
unsigned long int get_prec() const { return mpf_get_default_prec(); }
};
template <>
class __gmp_expr<mpz_t, __gmp_urandomm_value>
{
private:
__gmp_randstate_struct *state;
mpz_class range;
public:
__gmp_expr(gmp_randstate_t s, const mpz_class &z) : state(s), range(z) { }
void eval(mpz_ptr z) const
{ __gmp_rand_function::eval(z, state, range.get_mpz_t()); }
unsigned long int get_prec() const { return mpf_get_default_prec(); }
};
template <>
class __gmp_expr<mpf_t, __gmp_urandomb_value>
{
private:
__gmp_randstate_struct *state;
unsigned long int bits;
public:
__gmp_expr(gmp_randstate_t s, unsigned long int l) : state(s), bits(l) { }
void eval(mpf_ptr f, mp_bitcnt_t prec) const
{ __gmp_rand_function::eval(f, state, (bits>0) ? get_prec() : prec); }
unsigned long int get_prec() const
{
if (bits == 0)
return mpf_get_default_prec();
else
return bits;
}
};
extern "C" {
typedef void __gmp_randinit_default_t (gmp_randstate_t);
typedef void __gmp_randinit_lc_2exp_t (gmp_randstate_t, mpz_srcptr, unsigned long int, unsigned long int);
typedef int __gmp_randinit_lc_2exp_size_t (gmp_randstate_t, unsigned long int);
}
class gmp_randclass
{
private:
gmp_randstate_t state;
// copy construction and assignment not allowed
gmp_randclass(const gmp_randclass &);
void operator=(const gmp_randclass &);
public:
// constructors and destructor
gmp_randclass(gmp_randalg_t alg, unsigned long int size)
{
switch (alg)
{
case GMP_RAND_ALG_LC: // no other cases for now
default:
gmp_randinit(state, alg, size);
break;
}
}
// gmp_randinit_default
gmp_randclass(__gmp_randinit_default_t* f) { f(state); }
// gmp_randinit_lc_2exp
gmp_randclass(__gmp_randinit_lc_2exp_t* f,
mpz_class z, unsigned long int l1, unsigned long int l2)
{ f(state, z.get_mpz_t(), l1, l2); }
// gmp_randinit_lc_2exp_size
gmp_randclass(__gmp_randinit_lc_2exp_size_t* f,
unsigned long int size)
{
if (f (state, size) == 0)
throw std::length_error ("gmp_randinit_lc_2exp_size");
}
~gmp_randclass() { gmp_randclear(state); }
// initialize
void seed(); // choose a random seed some way (?)
void seed(unsigned long int s) { gmp_randseed_ui(state, s); }
void seed(const mpz_class &z) { gmp_randseed(state, z.get_mpz_t()); }
// get random number
__gmp_expr<mpz_t, __gmp_urandomb_value> get_z_bits(unsigned long int l)
{ return __gmp_expr<mpz_t, __gmp_urandomb_value>(state, l); }
__gmp_expr<mpz_t, __gmp_urandomb_value> get_z_bits(const mpz_class &z)
{ return get_z_bits(z.get_ui()); }
__gmp_expr<mpz_t, __gmp_urandomm_value> get_z_range(const mpz_class &z)
{ return __gmp_expr<mpz_t, __gmp_urandomm_value>(state, z); }
__gmp_expr<mpf_t, __gmp_urandomb_value> get_f(mp_bitcnt_t prec = 0)
{ return __gmp_expr<mpf_t, __gmp_urandomb_value>(state, prec); }
};
/**************** #undef all private macros ****************/
#undef __GMPP_DECLARE_COMPOUND_OPERATOR
#undef __GMPN_DECLARE_COMPOUND_OPERATOR
#undef __GMP_DECLARE_COMPOUND_OPERATOR
#undef __GMP_DECLARE_COMPOUND_OPERATOR_UI
#undef __GMP_DECLARE_INCREMENT_OPERATOR
#undef __GMPZQ_DEFINE_EXPR
#undef __GMP_DEFINE_UNARY_FUNCTION
#undef __GMP_DEFINE_UNARY_TYPE_FUNCTION
#undef __GMPP_DEFINE_BINARY_FUNCTION
#undef __GMPNN_DEFINE_BINARY_FUNCTION
#undef __GMPNS_DEFINE_BINARY_FUNCTION
#undef __GMPNU_DEFINE_BINARY_FUNCTION
#undef __GMPND_DEFINE_BINARY_FUNCTION
#undef __GMPNLD_DEFINE_BINARY_FUNCTION
#undef __GMPN_DEFINE_BINARY_FUNCTION
#undef __GMP_DEFINE_BINARY_FUNCTION
#undef __GMP_DEFINE_BINARY_FUNCTION_UI
#undef __GMPP_DEFINE_BINARY_TYPE_FUNCTION
#undef __GMPNN_DEFINE_BINARY_TYPE_FUNCTION
#undef __GMPNS_DEFINE_BINARY_TYPE_FUNCTION
#undef __GMPNU_DEFINE_BINARY_TYPE_FUNCTION
#undef __GMPND_DEFINE_BINARY_TYPE_FUNCTION
#undef __GMPNLD_DEFINE_BINARY_TYPE_FUNCTION
#undef __GMPN_DEFINE_BINARY_TYPE_FUNCTION
#undef __GMP_DEFINE_BINARY_TYPE_FUNCTION
#undef __GMPZ_DEFINE_COMPOUND_OPERATOR
#undef __GMPZN_DEFINE_COMPOUND_OPERATOR
#undef __GMPZNN_DEFINE_COMPOUND_OPERATOR
#undef __GMPZNS_DEFINE_COMPOUND_OPERATOR
#undef __GMPZNU_DEFINE_COMPOUND_OPERATOR
#undef __GMPZND_DEFINE_COMPOUND_OPERATOR
#undef __GMPZNLD_DEFINE_COMPOUND_OPERATOR
#undef __GMPP_DEFINE_COMPOUND_OPERATOR
#undef __GMPNN_DEFINE_COMPOUND_OPERATOR
#undef __GMPNS_DEFINE_COMPOUND_OPERATOR
#undef __GMPNU_DEFINE_COMPOUND_OPERATOR
#undef __GMPND_DEFINE_COMPOUND_OPERATOR
#undef __GMPNLD_DEFINE_COMPOUND_OPERATOR
#undef __GMPN_DEFINE_COMPOUND_OPERATOR
#undef __GMP_DEFINE_COMPOUND_OPERATOR
#undef __GMPQ_DEFINE_COMPOUND_OPERATOR
#undef __GMPF_DEFINE_COMPOUND_OPERATOR
#undef __GMP_DEFINE_COMPOUND_OPERATOR_UI
#undef __GMPZ_DEFINE_COMPOUND_OPERATOR_UI
#undef __GMPQ_DEFINE_COMPOUND_OPERATOR_UI
#undef __GMPF_DEFINE_COMPOUND_OPERATOR_UI
#undef __GMP_DEFINE_INCREMENT_OPERATOR
#undef __GMPZ_DEFINE_INCREMENT_OPERATOR
#undef __GMPQ_DEFINE_INCREMENT_OPERATOR
#undef __GMPF_DEFINE_INCREMENT_OPERATOR
#endif /* __GMP_PLUSPLUS__ */
|