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
|
/* GMP module external subroutines.
Copyright 2001-2003, 2015 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 either:
* 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.
or
* the GNU General Public License as published by the Free Software
Foundation; either version 2 of the License, or (at your option) any
later version.
or both in parallel, as here.
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 General Public License
for more details.
You should have received copies of the GNU General Public License and the
GNU Lesser General Public License along with the GNU MP Library. If not,
see https://www.gnu.org/licenses/.
/* Notes:
Routines are grouped with the alias feature and a table of function
pointers where possible, since each xsub routine ends up with quite a bit
of code size. Different combinations of arguments and return values have
to be separate though.
The "INTERFACE:" feature isn't available in perl 5.005 and so isn't used.
"ALIAS:" requires a table lookup with CvXSUBANY(cv).any_i32 (which is
"ix") whereas "INTERFACE:" would have CvXSUBANY(cv).any_dptr as the
function pointer immediately.
Mixed-type swapped-order assignments like "$a = 123; $a += mpz(456);"
invoke the plain overloaded "+", not "+=", which makes life easier.
mpz_assume etc types are used with the overloaded operators since such
operators are always called with a class object as the first argument, we
don't need an sv_derived_from() lookup to check. There's assert()s in
MPX_ASSUME() for this though.
The overload_constant routines reached via overload::constant get 4
arguments in perl 5.6, not the 3 as documented. This is apparently a
bug, using "..." lets us ignore the extra one.
There's only a few "si" functions in gmp, so usually SvIV values get
handled with an mpz_set_si into a temporary and then a full precision mpz
routine. This is reasonably efficient.
Argument types are checked, with a view to preserving all bits in the
operand. Perl is a bit looser in its arithmetic, allowing rounding or
truncation to an intended operand type (IV, UV or NV).
Bugs:
The memory leak detection attempted in GMP::END() doesn't work when mpz's
are created as constants because END() is called before they're
destroyed. What's the right place to hook such a check?
See the bugs section of GMP.pm too. */
/* Comment this out to get assertion checking. */
#define NDEBUG
/* Change this to "#define TRACE(x) x" for some diagnostics. */
#define TRACE(x)
#include <assert.h>
#include <float.h>
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"
#include "patchlevel.h"
#include "gmp.h"
/* Perl 5.005 doesn't have SvIsUV, only 5.6 and up.
Perl 5.8 has SvUOK, but not 5.6, so we don't use that. */
#ifndef SvIsUV
#define SvIsUV(sv) 0
#endif
#ifndef SvUVX
#define SvUVX(sv) (croak("GMP: oops, shouldn't be using SvUVX"), 0)
#endif
/* Code which doesn't check anything itself, but exists to support other
assert()s. */
#ifdef NDEBUG
#define assert_support(x)
#else
#define assert_support(x) x
#endif
/* LONG_MAX + 1 and ULONG_MAX + 1, as a doubles */
#define LONG_MAX_P1_AS_DOUBLE ((double) ((unsigned long) LONG_MAX + 1))
#define ULONG_MAX_P1_AS_DOUBLE (2.0 * (double) ((unsigned long) ULONG_MAX/2 + 1))
/* Check for perl version "major.minor".
Perl 5.004 doesn't have PERL_REVISION and PERL_VERSION, but that's ok,
we're only interested in tests above that. */
#if defined (PERL_REVISION) && defined (PERL_VERSION)
#define PERL_GE(major,minor) \
(PERL_REVISION > (major) \
|| ((major) == PERL_REVISION && PERL_VERSION >= (minor)))
#else
#define PERL_GE(major,minor) (0)
#endif
#define PERL_LT(major,minor) (! PERL_GE(major,minor))
/* sv_derived_from etc in 5.005 took "char *" rather than "const char *".
Avoid some compiler warnings by using const only where it works. */
#if PERL_LT (5,6)
#define classconst
#else
#define classconst const
#endif
/* In a MINGW or Cygwin DLL build of gmp, the various gmp functions are
given with dllimport directives, which prevents them being used as
initializers for constant data. We give function tables as
"static_functable const ...", which is normally "static const", but for
mingw expands to just "const" making the table an automatic with a
run-time initializer.
In gcc 3.3.1, the function tables initialized like this end up getting
all the __imp__foo values fetched, even though just one or two will be
used. This is wasteful, but probably not too bad. */
#if defined (__MINGW32__) || defined (__CYGWIN__)
#define static_functable
#else
#define static_functable static
#endif
#define GMP_MALLOC_ID 42
static classconst char mpz_class[] = "GMP::Mpz";
static classconst char mpq_class[] = "GMP::Mpq";
static classconst char mpf_class[] = "GMP::Mpf";
static classconst char rand_class[] = "GMP::Rand";
static HV *mpz_class_hv;
static HV *mpq_class_hv;
static HV *mpf_class_hv;
assert_support (static long mpz_count = 0;)
assert_support (static long mpq_count = 0;)
assert_support (static long mpf_count = 0;)
assert_support (static long rand_count = 0;)
#define TRACE_ACTIVE() \
assert_support \
(TRACE (printf (" active %ld mpz, %ld mpq, %ld mpf, %ld randstate\n", \
mpz_count, mpq_count, mpf_count, rand_count)))
/* Each "struct mpz_elem" etc is an mpz_t with a link field tacked on the
end so they can be held on a linked list. */
#define CREATE_MPX(type) \
\
/* must have mpz_t etc first, for sprintf below */ \
struct type##_elem { \
type##_t m; \
struct type##_elem *next; \
}; \
typedef struct type##_elem *type; \
typedef struct type##_elem *type##_assume; \
typedef type##_ptr type##_coerce; \
\
static type type##_freelist = NULL; \
\
static type \
new_##type (void) \
{ \
type p; \
TRACE (printf ("new %s\n", type##_class)); \
if (type##_freelist != NULL) \
{ \
p = type##_freelist; \
type##_freelist = type##_freelist->next; \
} \
else \
{ \
New (GMP_MALLOC_ID, p, 1, struct type##_elem); \
type##_init (p->m); \
} \
TRACE (printf (" p=%p\n", p)); \
assert_support (type##_count++); \
TRACE_ACTIVE (); \
return p; \
} \
CREATE_MPX (mpz)
CREATE_MPX (mpq)
typedef mpf_ptr mpf;
typedef mpf_ptr mpf_assume;
typedef mpf_ptr mpf_coerce_st0;
typedef mpf_ptr mpf_coerce_def;
static mpf
new_mpf (unsigned long prec)
{
mpf p;
New (GMP_MALLOC_ID, p, 1, __mpf_struct);
mpf_init2 (p, prec);
TRACE (printf (" mpf p=%p\n", p));
assert_support (mpf_count++);
TRACE_ACTIVE ();
return p;
}
/* tmp_mpf_t records an allocated precision with an mpf_t so changes of
precision can be done with just an mpf_set_prec_raw. */
struct tmp_mpf_struct {
mpf_t m;
unsigned long allocated_prec;
};
typedef const struct tmp_mpf_struct *tmp_mpf_srcptr;
typedef struct tmp_mpf_struct *tmp_mpf_ptr;
typedef struct tmp_mpf_struct tmp_mpf_t[1];
#define tmp_mpf_init(f) \
do { \
mpf_init (f->m); \
f->allocated_prec = mpf_get_prec (f->m); \
} while (0)
static void
tmp_mpf_grow (tmp_mpf_ptr f, unsigned long prec)
{
mpf_set_prec_raw (f->m, f->allocated_prec);
mpf_set_prec (f->m, prec);
f->allocated_prec = mpf_get_prec (f->m);
}
#define tmp_mpf_shrink(f) tmp_mpf_grow (f, 1L)
#define tmp_mpf_set_prec(f,prec) \
do { \
if (prec > f->allocated_prec) \
tmp_mpf_grow (f, prec); \
else \
mpf_set_prec_raw (f->m, prec); \
} while (0)
static mpz_t tmp_mpz_0, tmp_mpz_1, tmp_mpz_2;
static mpq_t tmp_mpq_0, tmp_mpq_1;
static tmp_mpf_t tmp_mpf_0, tmp_mpf_1;
/* for GMP::Mpz::export */
#define tmp_mpz_4 tmp_mpz_2
#define FREE_MPX_FREELIST(p,type) \
do { \
TRACE (printf ("free %s\n", type##_class)); \
p->next = type##_freelist; \
type##_freelist = p; \
assert_support (type##_count--); \
TRACE_ACTIVE (); \
assert (type##_count >= 0); \
} while (0)
/* this version for comparison, if desired */
#define FREE_MPX_NOFREELIST(p,type) \
do { \
TRACE (printf ("free %s\n", type##_class)); \
type##_clear (p->m); \
Safefree (p); \
assert_support (type##_count--); \
TRACE_ACTIVE (); \
assert (type##_count >= 0); \
} while (0)
#define free_mpz(z) FREE_MPX_FREELIST (z, mpz)
#define free_mpq(q) FREE_MPX_FREELIST (q, mpq)
/* Return a new mortal SV holding the given mpx_ptr pointer.
class_hv should be one of mpz_class_hv etc. */
#define MPX_NEWMORTAL(mpx_ptr, class_hv) \
sv_bless (sv_setref_pv (sv_newmortal(), NULL, mpx_ptr), class_hv)
/* Aliases for use in typemaps */
typedef char *malloced_string;
typedef const char *const_string;
typedef const char *const_string_assume;
typedef char *string;
typedef SV *order_noswap;
typedef SV *dummy;
typedef SV *SV_copy_0;
typedef unsigned long ulong_coerce;
typedef __gmp_randstate_struct *randstate;
typedef UV gmp_UV;
#define SvMPX(s,type) ((type) SvIV((SV*) SvRV(s)))
#define SvMPZ(s) SvMPX(s,mpz)
#define SvMPQ(s) SvMPX(s,mpq)
#define SvMPF(s) SvMPX(s,mpf)
#define SvRANDSTATE(s) SvMPX(s,randstate)
#define MPX_ASSUME(x,sv,type) \
do { \
assert (sv_derived_from (sv, type##_class)); \
x = SvMPX(sv,type); \
} while (0)
#define MPZ_ASSUME(z,sv) MPX_ASSUME(z,sv,mpz)
#define MPQ_ASSUME(q,sv) MPX_ASSUME(q,sv,mpq)
#define MPF_ASSUME(f,sv) MPX_ASSUME(f,sv,mpf)
#define numberof(x) (sizeof (x) / sizeof ((x)[0]))
#define SGN(x) ((x)<0 ? -1 : (x) != 0)
#define ABS(x) ((x)>=0 ? (x) : -(x))
#define double_integer_p(d) (floor (d) == (d))
#define x_mpq_integer_p(q) \
(mpz_cmp_ui (mpq_denref(q), 1L) == 0)
#define assert_table(ix) assert (ix >= 0 && ix < numberof (table))
#define SV_PTR_SWAP(x,y) \
do { SV *__tmp = (x); (x) = (y); (y) = __tmp; } while (0)
#define MPF_PTR_SWAP(x,y) \
do { mpf_ptr __tmp = (x); (x) = (y); (y) = __tmp; } while (0)
static void
class_or_croak (SV *sv, classconst char *cl)
{
if (! sv_derived_from (sv, cl))
croak("not type %s", cl);
}
/* These are macros, wrap them in functions. */
static int
x_mpz_odd_p (mpz_srcptr z)
{
return mpz_odd_p (z);
}
static int
x_mpz_even_p (mpz_srcptr z)
{
return mpz_even_p (z);
}
static void
x_mpq_pow_ui (mpq_ptr r, mpq_srcptr b, unsigned long e)
{
mpz_pow_ui (mpq_numref(r), mpq_numref(b), e);
mpz_pow_ui (mpq_denref(r), mpq_denref(b), e);
}
static void *
my_gmp_alloc (size_t n)
{
void *p;
TRACE (printf ("my_gmp_alloc %u\n", n));
New (GMP_MALLOC_ID, p, n, char);
TRACE (printf (" p=%p\n", p));
return p;
}
static void *
my_gmp_realloc (void *p, size_t oldsize, size_t newsize)
{
TRACE (printf ("my_gmp_realloc %p, %u to %u\n", p, oldsize, newsize));
Renew (p, newsize, char);
TRACE (printf (" p=%p\n", p));
return p;
}
static void
my_gmp_free (void *p, size_t n)
{
TRACE (printf ("my_gmp_free %p %u\n", p, n));
Safefree (p);
}
#define my_mpx_set_svstr(type) \
static void \
my_##type##_set_svstr (type##_ptr x, SV *sv) \
{ \
const char *str; \
STRLEN len; \
TRACE (printf (" my_" #type "_set_svstr\n")); \
assert (SvPOK(sv) || SvPOKp(sv)); \
str = SvPV (sv, len); \
TRACE (printf (" str \"%s\"\n", str)); \
if (type##_set_str (x, str, 0) != 0) \
croak ("%s: invalid string: %s", type##_class, str); \
}
my_mpx_set_svstr(mpz)
my_mpx_set_svstr(mpq)
my_mpx_set_svstr(mpf)
/* very slack */
static int
x_mpq_cmp_si (mpq_srcptr x, long yn, unsigned long yd)
{
mpq y;
int ret;
y = new_mpq ();
mpq_set_si (y->m, yn, yd);
ret = mpq_cmp (x, y->m);
free_mpq (y);
return ret;
}
static int
x_mpq_fits_slong_p (mpq_srcptr q)
{
return x_mpq_cmp_si (q, LONG_MIN, 1L) >= 0
&& mpq_cmp_ui (q, LONG_MAX, 1L) <= 0;
}
static int
x_mpz_cmp_q (mpz_ptr x, mpq_srcptr y)
{
int ret;
mpz_set_ui (mpq_denref(tmp_mpq_0), 1L);
mpz_swap (mpq_numref(tmp_mpq_0), x);
ret = mpq_cmp (tmp_mpq_0, y);
mpz_swap (mpq_numref(tmp_mpq_0), x);
return ret;
}
static int
x_mpz_cmp_f (mpz_srcptr x, mpf_srcptr y)
{
tmp_mpf_set_prec (tmp_mpf_0, mpz_sizeinbase (x, 2));
mpf_set_z (tmp_mpf_0->m, x);
return mpf_cmp (tmp_mpf_0->m, y);
}
#define USE_UNKNOWN 0
#define USE_IVX 1
#define USE_UVX 2
#define USE_NVX 3
#define USE_PVX 4
#define USE_MPZ 5
#define USE_MPQ 6
#define USE_MPF 7
/* mg_get is called every time we get a value, even if the private flags are
still set from a previous such call. This is the same as as SvIV and
friends do.
When POK, we use the PV, even if there's an IV or NV available. This is
because it's hard to be sure there wasn't any rounding in establishing
the IV and/or NV. Cases of overflow, where the PV should definitely be
used, are easy enough to spot, but rounding is hard. So although IV or
NV would be more efficient, we must use the PV to be sure of getting all
the data. Applications should convert once to mpz, mpq or mpf when using
a value repeatedly.
Zany dual-type scalars like $! where the IV is an error code and the PV
is an error description string won't work with this preference for PV,
but that's too bad. Such scalars should be rare, and unlikely to be used
in bignum calculations.
When IOK and NOK are both set, we would prefer to use the IV since it can
be converted more efficiently, and because on a 64-bit system the NV may
have less bits than the IV. The following rules are applied,
- If the NV is not an integer, then we must use that NV, since clearly
the IV was merely established by rounding and is not the full value.
- In perl prior to 5.8, an NV too big for an IV leaves an overflow value
0xFFFFFFFF. If the NV is too big to fit an IV then clearly it's the NV
which is the true value and must be used.
- In perl 5.8 and up, such an overflow doesn't set IOK, so that test is
unnecessary. However when coming from get-magic, IOKp _is_ set, and we
must check for overflow the same as in older perl.
FIXME:
We'd like to call mg_get just once, but unfortunately sv_derived_from()
will call it for each of our checks. We could do a string compare like
sv_isa ourselves, but that only tests the exact class, it doesn't
recognise subclassing. There doesn't seem to be a public interface to
the subclassing tests (in the internal isa_lookup() function). */
int
use_sv (SV *sv)
{
double d;
if (SvGMAGICAL(sv))
{
mg_get(sv);
if (SvPOKp(sv))
return USE_PVX;
if (SvIOKp(sv))
{
if (SvIsUV(sv))
{
if (SvNOKp(sv))
goto u_or_n;
return USE_UVX;
}
else
{
if (SvNOKp(sv))
goto i_or_n;
return USE_IVX;
}
}
if (SvNOKp(sv))
return USE_NVX;
goto rok_or_unknown;
}
if (SvPOK(sv))
return USE_PVX;
if (SvIOK(sv))
{
if (SvIsUV(sv))
{
if (SvNOK(sv))
{
if (PERL_LT (5, 8))
{
u_or_n:
d = SvNVX(sv);
if (d >= ULONG_MAX_P1_AS_DOUBLE || d < 0.0)
return USE_NVX;
}
d = SvNVX(sv);
if (d != floor (d))
return USE_NVX;
}
return USE_UVX;
}
else
{
if (SvNOK(sv))
{
if (PERL_LT (5, 8))
{
i_or_n:
d = SvNVX(sv);
if (d >= LONG_MAX_P1_AS_DOUBLE || d < (double) LONG_MIN)
return USE_NVX;
}
d = SvNVX(sv);
if (d != floor (d))
return USE_NVX;
}
return USE_IVX;
}
}
if (SvNOK(sv))
return USE_NVX;
rok_or_unknown:
if (SvROK(sv))
{
if (sv_derived_from (sv, mpz_class))
return USE_MPZ;
if (sv_derived_from (sv, mpq_class))
return USE_MPQ;
if (sv_derived_from (sv, mpf_class))
return USE_MPF;
}
return USE_UNKNOWN;
}
/* Coerce sv to an mpz. Use tmp to hold the converted value if sv isn't
already an mpz (or an mpq of which the numerator can be used). Return
the chosen mpz (tmp or the contents of sv). */
static mpz_ptr
coerce_mpz_using (mpz_ptr tmp, SV *sv, int use)
{
switch (use) {
case USE_IVX:
mpz_set_si (tmp, SvIVX(sv));
return tmp;
case USE_UVX:
mpz_set_ui (tmp, SvUVX(sv));
return tmp;
case USE_NVX:
{
double d;
d = SvNVX(sv);
if (! double_integer_p (d))
croak ("cannot coerce non-integer double to mpz");
mpz_set_d (tmp, d);
return tmp;
}
case USE_PVX:
my_mpz_set_svstr (tmp, sv);
return tmp;
case USE_MPZ:
return SvMPZ(sv)->m;
case USE_MPQ:
{
mpq q = SvMPQ(sv);
if (! x_mpq_integer_p (q->m))
croak ("cannot coerce non-integer mpq to mpz");
return mpq_numref(q->m);
}
case USE_MPF:
{
mpf f = SvMPF(sv);
if (! mpf_integer_p (f))
croak ("cannot coerce non-integer mpf to mpz");
mpz_set_f (tmp, f);
return tmp;
}
default:
croak ("cannot coerce to mpz");
}
}
static mpz_ptr
coerce_mpz (mpz_ptr tmp, SV *sv)
{
return coerce_mpz_using (tmp, sv, use_sv (sv));
}
/* Coerce sv to an mpq. If sv is an mpq then just return that, otherwise
use tmp to hold the converted value and return that. */
static mpq_ptr
coerce_mpq_using (mpq_ptr tmp, SV *sv, int use)
{
TRACE (printf ("coerce_mpq_using %p %d\n", tmp, use));
switch (use) {
case USE_IVX:
mpq_set_si (tmp, SvIVX(sv), 1L);
return tmp;
case USE_UVX:
mpq_set_ui (tmp, SvUVX(sv), 1L);
return tmp;
case USE_NVX:
mpq_set_d (tmp, SvNVX(sv));
return tmp;
case USE_PVX:
my_mpq_set_svstr (tmp, sv);
return tmp;
case USE_MPZ:
mpq_set_z (tmp, SvMPZ(sv)->m);
return tmp;
case USE_MPQ:
return SvMPQ(sv)->m;
case USE_MPF:
mpq_set_f (tmp, SvMPF(sv));
return tmp;
default:
croak ("cannot coerce to mpq");
}
}
static mpq_ptr
coerce_mpq (mpq_ptr tmp, SV *sv)
{
return coerce_mpq_using (tmp, sv, use_sv (sv));
}
static void
my_mpf_set_sv_using (mpf_ptr f, SV *sv, int use)
{
switch (use) {
case USE_IVX:
mpf_set_si (f, SvIVX(sv));
break;
case USE_UVX:
mpf_set_ui (f, SvUVX(sv));
break;
case USE_NVX:
mpf_set_d (f, SvNVX(sv));
break;
case USE_PVX:
my_mpf_set_svstr (f, sv);
break;
case USE_MPZ:
mpf_set_z (f, SvMPZ(sv)->m);
break;
case USE_MPQ:
mpf_set_q (f, SvMPQ(sv)->m);
break;
case USE_MPF:
mpf_set (f, SvMPF(sv));
break;
default:
croak ("cannot coerce to mpf");
}
}
/* Coerce sv to an mpf. If sv is an mpf then just return that, otherwise
use tmp to hold the converted value (with prec precision). */
static mpf_ptr
coerce_mpf_using (tmp_mpf_ptr tmp, SV *sv, unsigned long prec, int use)
{
if (use == USE_MPF)
return SvMPF(sv);
tmp_mpf_set_prec (tmp, prec);
my_mpf_set_sv_using (tmp->m, sv, use);
return tmp->m;
}
static mpf_ptr
coerce_mpf (tmp_mpf_ptr tmp, SV *sv, unsigned long prec)
{
return coerce_mpf_using (tmp, sv, prec, use_sv (sv));
}
/* Coerce xv to an mpf and store the pointer in x, ditto for yv to x. If
one of xv or yv is an mpf then use it for the precision, otherwise use
the default precision. */
unsigned long
coerce_mpf_pair (mpf *xp, SV *xv, mpf *yp, SV *yv)
{
int x_use = use_sv (xv);
int y_use = use_sv (yv);
unsigned long prec;
mpf x, y;
if (x_use == USE_MPF)
{
x = SvMPF(xv);
prec = mpf_get_prec (x);
y = coerce_mpf_using (tmp_mpf_0, yv, prec, y_use);
}
else
{
y = coerce_mpf_using (tmp_mpf_0, yv, mpf_get_default_prec(), y_use);
prec = mpf_get_prec (y);
x = coerce_mpf_using (tmp_mpf_1, xv, prec, x_use);
}
*xp = x;
*yp = y;
return prec;
}
/* Note that SvUV is not used, since it merely treats the signed IV as if it
was unsigned. We get an IV and check its sign. */
static unsigned long
coerce_ulong (SV *sv)
{
long n;
switch (use_sv (sv)) {
case USE_IVX:
n = SvIVX(sv);
negative_check:
if (n < 0)
goto range_error;
return n;
case USE_UVX:
return SvUVX(sv);
case USE_NVX:
{
double d;
d = SvNVX(sv);
if (! double_integer_p (d))
goto integer_error;
n = SvIV(sv);
}
goto negative_check;
case USE_PVX:
/* FIXME: Check the string is an integer. */
n = SvIV(sv);
goto negative_check;
case USE_MPZ:
{
mpz z = SvMPZ(sv);
if (! mpz_fits_ulong_p (z->m))
goto range_error;
return mpz_get_ui (z->m);
}
case USE_MPQ:
{
mpq q = SvMPQ(sv);
if (! x_mpq_integer_p (q->m))
goto integer_error;
if (! mpz_fits_ulong_p (mpq_numref (q->m)))
goto range_error;
return mpz_get_ui (mpq_numref (q->m));
}
case USE_MPF:
{
mpf f = SvMPF(sv);
if (! mpf_integer_p (f))
goto integer_error;
if (! mpf_fits_ulong_p (f))
goto range_error;
return mpf_get_ui (f);
}
default:
croak ("cannot coerce to ulong");
}
integer_error:
croak ("not an integer");
range_error:
croak ("out of range for ulong");
}
static long
coerce_long (SV *sv)
{
switch (use_sv (sv)) {
case USE_IVX:
return SvIVX(sv);
case USE_UVX:
{
UV u = SvUVX(sv);
if (u > (UV) LONG_MAX)
goto range_error;
return u;
}
case USE_NVX:
{
double d = SvNVX(sv);
if (! double_integer_p (d))
goto integer_error;
return SvIV(sv);
}
case USE_PVX:
/* FIXME: Check the string is an integer. */
return SvIV(sv);
case USE_MPZ:
{
mpz z = SvMPZ(sv);
if (! mpz_fits_slong_p (z->m))
goto range_error;
return mpz_get_si (z->m);
}
case USE_MPQ:
{
mpq q = SvMPQ(sv);
if (! x_mpq_integer_p (q->m))
goto integer_error;
if (! mpz_fits_slong_p (mpq_numref (q->m)))
goto range_error;
return mpz_get_si (mpq_numref (q->m));
}
case USE_MPF:
{
mpf f = SvMPF(sv);
if (! mpf_integer_p (f))
goto integer_error;
if (! mpf_fits_slong_p (f))
goto range_error;
return mpf_get_si (f);
}
default:
croak ("cannot coerce to long");
}
integer_error:
croak ("not an integer");
range_error:
croak ("out of range for ulong");
}
/* ------------------------------------------------------------------------- */
MODULE = GMP PACKAGE = GMP
BOOT:
TRACE (printf ("GMP boot\n"));
mp_set_memory_functions (my_gmp_alloc, my_gmp_realloc, my_gmp_free);
mpz_init (tmp_mpz_0);
mpz_init (tmp_mpz_1);
mpz_init (tmp_mpz_2);
mpq_init (tmp_mpq_0);
mpq_init (tmp_mpq_1);
tmp_mpf_init (tmp_mpf_0);
tmp_mpf_init (tmp_mpf_1);
mpz_class_hv = gv_stashpv (mpz_class, 1);
mpq_class_hv = gv_stashpv (mpq_class, 1);
mpf_class_hv = gv_stashpv (mpf_class, 1);
void
END()
CODE:
TRACE (printf ("GMP end\n"));
TRACE_ACTIVE ();
/* These are not always true, see Bugs at the top of the file. */
/* assert (mpz_count == 0); */
/* assert (mpq_count == 0); */
/* assert (mpf_count == 0); */
/* assert (rand_count == 0); */
const_string
version()
CODE:
RETVAL = gmp_version;
OUTPUT:
RETVAL
bool
fits_slong_p (sv)
SV *sv
CODE:
switch (use_sv (sv)) {
case USE_IVX:
RETVAL = 1;
break;
case USE_UVX:
{
UV u = SvUVX(sv);
RETVAL = (u <= LONG_MAX);
}
break;
case USE_NVX:
{
double d = SvNVX(sv);
RETVAL = (d >= (double) LONG_MIN && d < LONG_MAX_P1_AS_DOUBLE);
}
break;
case USE_PVX:
{
STRLEN len;
const char *str = SvPV (sv, len);
if (mpq_set_str (tmp_mpq_0, str, 0) == 0)
RETVAL = x_mpq_fits_slong_p (tmp_mpq_0);
else
{
/* enough precision for a long */
tmp_mpf_set_prec (tmp_mpf_0, 2*mp_bits_per_limb);
if (mpf_set_str (tmp_mpf_0->m, str, 10) != 0)
croak ("GMP::fits_slong_p invalid string format");
RETVAL = mpf_fits_slong_p (tmp_mpf_0->m);
}
}
break;
case USE_MPZ:
RETVAL = mpz_fits_slong_p (SvMPZ(sv)->m);
break;
case USE_MPQ:
RETVAL = x_mpq_fits_slong_p (SvMPQ(sv)->m);
break;
case USE_MPF:
RETVAL = mpf_fits_slong_p (SvMPF(sv));
break;
default:
croak ("GMP::fits_slong_p invalid argument");
}
OUTPUT:
RETVAL
double
get_d (sv)
SV *sv
CODE:
switch (use_sv (sv)) {
case USE_IVX:
RETVAL = (double) SvIVX(sv);
break;
case USE_UVX:
RETVAL = (double) SvUVX(sv);
break;
case USE_NVX:
RETVAL = SvNVX(sv);
break;
case USE_PVX:
{
STRLEN len;
RETVAL = atof(SvPV(sv, len));
}
break;
case USE_MPZ:
RETVAL = mpz_get_d (SvMPZ(sv)->m);
break;
case USE_MPQ:
RETVAL = mpq_get_d (SvMPQ(sv)->m);
break;
case USE_MPF:
RETVAL = mpf_get_d (SvMPF(sv));
break;
default:
croak ("GMP::get_d invalid argument");
}
OUTPUT:
RETVAL
void
get_d_2exp (sv)
SV *sv
PREINIT:
double ret;
long exp;
PPCODE:
switch (use_sv (sv)) {
case USE_IVX:
ret = (double) SvIVX(sv);
goto use_frexp;
case USE_UVX:
ret = (double) SvUVX(sv);
goto use_frexp;
case USE_NVX:
{
int i_exp;
ret = SvNVX(sv);
use_frexp:
ret = frexp (ret, &i_exp);
exp = i_exp;
}
break;
case USE_PVX:
/* put strings through mpf to give full exp range */
tmp_mpf_set_prec (tmp_mpf_0, DBL_MANT_DIG);
my_mpf_set_svstr (tmp_mpf_0->m, sv);
ret = mpf_get_d_2exp (&exp, tmp_mpf_0->m);
break;
case USE_MPZ:
ret = mpz_get_d_2exp (&exp, SvMPZ(sv)->m);
break;
case USE_MPQ:
tmp_mpf_set_prec (tmp_mpf_0, DBL_MANT_DIG);
mpf_set_q (tmp_mpf_0->m, SvMPQ(sv)->m);
ret = mpf_get_d_2exp (&exp, tmp_mpf_0->m);
break;
case USE_MPF:
ret = mpf_get_d_2exp (&exp, SvMPF(sv));
break;
default:
croak ("GMP::get_d_2exp invalid argument");
}
PUSHs (sv_2mortal (newSVnv (ret)));
PUSHs (sv_2mortal (newSViv (exp)));
long
get_si (sv)
SV *sv
CODE:
switch (use_sv (sv)) {
case USE_IVX:
RETVAL = SvIVX(sv);
break;
case USE_UVX:
RETVAL = SvUVX(sv);
break;
case USE_NVX:
RETVAL = (long) SvNVX(sv);
break;
case USE_PVX:
RETVAL = SvIV(sv);
break;
case USE_MPZ:
RETVAL = mpz_get_si (SvMPZ(sv)->m);
break;
case USE_MPQ:
mpz_set_q (tmp_mpz_0, SvMPQ(sv)->m);
RETVAL = mpz_get_si (tmp_mpz_0);
break;
case USE_MPF:
RETVAL = mpf_get_si (SvMPF(sv));
break;
default:
croak ("GMP::get_si invalid argument");
}
OUTPUT:
RETVAL
void
get_str (sv, ...)
SV *sv
PREINIT:
char *str;
mp_exp_t exp;
mpz_ptr z;
mpq_ptr q;
mpf f;
int base;
int ndigits;
PPCODE:
TRACE (printf ("GMP::get_str\n"));
if (items >= 2)
base = coerce_long (ST(1));
else
base = 10;
TRACE (printf (" base=%d\n", base));
if (items >= 3)
ndigits = coerce_long (ST(2));
else
ndigits = 10;
TRACE (printf (" ndigits=%d\n", ndigits));
EXTEND (SP, 2);
switch (use_sv (sv)) {
case USE_IVX:
mpz_set_si (tmp_mpz_0, SvIVX(sv));
get_tmp_mpz_0:
z = tmp_mpz_0;
goto get_mpz;
case USE_UVX:
mpz_set_ui (tmp_mpz_0, SvUVX(sv));
goto get_tmp_mpz_0;
case USE_NVX:
/* only digits in the original double, not in the coerced form */
if (ndigits == 0)
ndigits = DBL_DIG;
mpf_set_d (tmp_mpf_0->m, SvNVX(sv));
f = tmp_mpf_0->m;
goto get_mpf;
case USE_PVX:
{
/* get_str on a string is not much more than a base conversion */
STRLEN len;
str = SvPV (sv, len);
if (mpz_set_str (tmp_mpz_0, str, 0) == 0)
{
z = tmp_mpz_0;
goto get_mpz;
}
else if (mpq_set_str (tmp_mpq_0, str, 0) == 0)
{
q = tmp_mpq_0;
goto get_mpq;
}
else
{
/* FIXME: Would like perhaps a precision equivalent to the
number of significant digits of the string, in its given
base. */
tmp_mpf_set_prec (tmp_mpf_0, strlen(str));
if (mpf_set_str (tmp_mpf_0->m, str, 10) == 0)
{
f = tmp_mpf_0->m;
goto get_mpf;
}
else
croak ("GMP::get_str invalid string format");
}
}
break;
case USE_MPZ:
z = SvMPZ(sv)->m;
get_mpz:
str = mpz_get_str (NULL, base, z);
push_str:
PUSHs (sv_2mortal (newSVpv (str, 0)));
break;
case USE_MPQ:
q = SvMPQ(sv)->m;
get_mpq:
str = mpq_get_str (NULL, base, q);
goto push_str;
case USE_MPF:
f = SvMPF(sv);
get_mpf:
str = mpf_get_str (NULL, &exp, base, 0, f);
PUSHs (sv_2mortal (newSVpv (str, 0)));
PUSHs (sv_2mortal (newSViv (exp)));
break;
default:
croak ("GMP::get_str invalid argument");
}
bool
integer_p (sv)
SV *sv
CODE:
switch (use_sv (sv)) {
case USE_IVX:
case USE_UVX:
RETVAL = 1;
break;
case USE_NVX:
RETVAL = double_integer_p (SvNVX(sv));
break;
case USE_PVX:
{
/* FIXME: Maybe this should be done by parsing the string, not by an
actual conversion. */
STRLEN len;
const char *str = SvPV (sv, len);
if (mpq_set_str (tmp_mpq_0, str, 0) == 0)
RETVAL = x_mpq_integer_p (tmp_mpq_0);
else
{
/* enough for all digits of the string */
tmp_mpf_set_prec (tmp_mpf_0, strlen(str)+64);
if (mpf_set_str (tmp_mpf_0->m, str, 10) == 0)
RETVAL = mpf_integer_p (tmp_mpf_0->m);
else
croak ("GMP::integer_p invalid string format");
}
}
break;
case USE_MPZ:
RETVAL = 1;
break;
case USE_MPQ:
RETVAL = x_mpq_integer_p (SvMPQ(sv)->m);
break;
case USE_MPF:
RETVAL = mpf_integer_p (SvMPF(sv));
break;
default:
croak ("GMP::integer_p invalid argument");
}
OUTPUT:
RETVAL
int
sgn (sv)
SV *sv
CODE:
switch (use_sv (sv)) {
case USE_IVX:
RETVAL = SGN (SvIVX(sv));
break;
case USE_UVX:
RETVAL = (SvUVX(sv) > 0);
break;
case USE_NVX:
RETVAL = SGN (SvNVX(sv));
break;
case USE_PVX:
{
/* FIXME: Maybe this should be done by parsing the string, not by an
actual conversion. */
STRLEN len;
const char *str = SvPV (sv, len);
if (mpq_set_str (tmp_mpq_0, str, 0) == 0)
RETVAL = mpq_sgn (tmp_mpq_0);
else
{
/* enough for all digits of the string */
tmp_mpf_set_prec (tmp_mpf_0, strlen(str)+64);
if (mpf_set_str (tmp_mpf_0->m, str, 10) == 0)
RETVAL = mpf_sgn (tmp_mpf_0->m);
else
croak ("GMP::sgn invalid string format");
}
}
break;
case USE_MPZ:
RETVAL = mpz_sgn (SvMPZ(sv)->m);
break;
case USE_MPQ:
RETVAL = mpq_sgn (SvMPQ(sv)->m);
break;
case USE_MPF:
RETVAL = mpf_sgn (SvMPF(sv));
break;
default:
croak ("GMP::sgn invalid argument");
}
OUTPUT:
RETVAL
# currently undocumented
void
shrink ()
CODE:
#define x_mpz_shrink(z) \
mpz_set_ui (z, 0L); _mpz_realloc (z, 1)
#define x_mpq_shrink(q) \
x_mpz_shrink (mpq_numref(q)); x_mpz_shrink (mpq_denref(q))
x_mpz_shrink (tmp_mpz_0);
x_mpz_shrink (tmp_mpz_1);
x_mpz_shrink (tmp_mpz_2);
x_mpq_shrink (tmp_mpq_0);
x_mpq_shrink (tmp_mpq_1);
tmp_mpf_shrink (tmp_mpf_0);
tmp_mpf_shrink (tmp_mpf_1);
malloced_string
sprintf_internal (fmt, sv)
const_string fmt
SV *sv
CODE:
assert (strlen (fmt) >= 3);
assert (SvROK(sv));
assert ((sv_derived_from (sv, mpz_class) && fmt[strlen(fmt)-2] == 'Z')
|| (sv_derived_from (sv, mpq_class) && fmt[strlen(fmt)-2] == 'Q')
|| (sv_derived_from (sv, mpf_class) && fmt[strlen(fmt)-2] == 'F'));
TRACE (printf ("GMP::sprintf_internal\n");
printf (" fmt |%s|\n", fmt);
printf (" sv |%p|\n", SvMPZ(sv)));
/* cheat a bit here, SvMPZ works for mpq and mpf too */
gmp_asprintf (&RETVAL, fmt, SvMPZ(sv));
TRACE (printf (" result |%s|\n", RETVAL));
OUTPUT:
RETVAL
#------------------------------------------------------------------------------
MODULE = GMP PACKAGE = GMP::Mpz
mpz
mpz (...)
ALIAS:
GMP::Mpz::new = 1
PREINIT:
SV *sv;
CODE:
TRACE (printf ("%s new, ix=%ld, items=%d\n", mpz_class, ix, (int) items));
RETVAL = new_mpz();
switch (items) {
case 0:
mpz_set_ui (RETVAL->m, 0L);
break;
case 1:
sv = ST(0);
TRACE (printf (" use %d\n", use_sv (sv)));
switch (use_sv (sv)) {
case USE_IVX:
mpz_set_si (RETVAL->m, SvIVX(sv));
break;
case USE_UVX:
mpz_set_ui (RETVAL->m, SvUVX(sv));
break;
case USE_NVX:
mpz_set_d (RETVAL->m, SvNVX(sv));
break;
case USE_PVX:
my_mpz_set_svstr (RETVAL->m, sv);
break;
case USE_MPZ:
mpz_set (RETVAL->m, SvMPZ(sv)->m);
break;
case USE_MPQ:
mpz_set_q (RETVAL->m, SvMPQ(sv)->m);
break;
case USE_MPF:
mpz_set_f (RETVAL->m, SvMPF(sv));
break;
default:
goto invalid;
}
break;
default:
invalid:
croak ("%s new: invalid arguments", mpz_class);
}
OUTPUT:
RETVAL
void
overload_constant (str, pv, d1, ...)
const_string_assume str
SV *pv
dummy d1
PREINIT:
mpz z;
PPCODE:
TRACE (printf ("%s constant: %s\n", mpz_class, str));
z = new_mpz();
if (mpz_set_str (z->m, str, 0) == 0)
{
PUSHs (MPX_NEWMORTAL (z, mpz_class_hv));
}
else
{
free_mpz (z);
PUSHs(pv);
}
mpz
overload_copy (z, d1, d2)
mpz_assume z
dummy d1
dummy d2
CODE:
RETVAL = new_mpz();
mpz_set (RETVAL->m, z->m);
OUTPUT:
RETVAL
void
DESTROY (z)
mpz_assume z
CODE:
TRACE (printf ("%s DESTROY %p\n", mpz_class, z));
free_mpz (z);
malloced_string
overload_string (z, d1, d2)
mpz_assume z
dummy d1
dummy d2
CODE:
TRACE (printf ("%s overload_string %p\n", mpz_class, z));
RETVAL = mpz_get_str (NULL, 10, z->m);
OUTPUT:
RETVAL
mpz
overload_add (xv, yv, order)
SV *xv
SV *yv
SV *order
ALIAS:
GMP::Mpz::overload_sub = 1
GMP::Mpz::overload_mul = 2
GMP::Mpz::overload_div = 3
GMP::Mpz::overload_rem = 4
GMP::Mpz::overload_and = 5
GMP::Mpz::overload_ior = 6
GMP::Mpz::overload_xor = 7
PREINIT:
static_functable const struct {
void (*op) (mpz_ptr, mpz_srcptr, mpz_srcptr);
} table[] = {
{ mpz_add }, /* 0 */
{ mpz_sub }, /* 1 */
{ mpz_mul }, /* 2 */
{ mpz_tdiv_q }, /* 3 */
{ mpz_tdiv_r }, /* 4 */
{ mpz_and }, /* 5 */
{ mpz_ior }, /* 6 */
{ mpz_xor }, /* 7 */
};
CODE:
assert_table (ix);
if (order == &PL_sv_yes)
SV_PTR_SWAP (xv, yv);
RETVAL = new_mpz();
(*table[ix].op) (RETVAL->m,
coerce_mpz (tmp_mpz_0, xv),
coerce_mpz (tmp_mpz_1, yv));
OUTPUT:
RETVAL
void
overload_addeq (x, y, o)
mpz_assume x
mpz_coerce y
order_noswap o
ALIAS:
GMP::Mpz::overload_subeq = 1
GMP::Mpz::overload_muleq = 2
GMP::Mpz::overload_diveq = 3
GMP::Mpz::overload_remeq = 4
GMP::Mpz::overload_andeq = 5
GMP::Mpz::overload_ioreq = 6
GMP::Mpz::overload_xoreq = 7
PREINIT:
static_functable const struct {
void (*op) (mpz_ptr, mpz_srcptr, mpz_srcptr);
} table[] = {
{ mpz_add }, /* 0 */
{ mpz_sub }, /* 1 */
{ mpz_mul }, /* 2 */
{ mpz_tdiv_q }, /* 3 */
{ mpz_tdiv_r }, /* 4 */
{ mpz_and }, /* 5 */
{ mpz_ior }, /* 6 */
{ mpz_xor }, /* 7 */
};
PPCODE:
assert_table (ix);
(*table[ix].op) (x->m, x->m, y);
XPUSHs (ST(0));
mpz
overload_lshift (zv, nv, order)
SV *zv
SV *nv
SV *order
ALIAS:
GMP::Mpz::overload_rshift = 1
GMP::Mpz::overload_pow = 2
PREINIT:
static_functable const struct {
void (*op) (mpz_ptr, mpz_srcptr, unsigned long);
} table[] = {
{ mpz_mul_2exp }, /* 0 */
{ mpz_fdiv_q_2exp }, /* 1 */
{ mpz_pow_ui }, /* 2 */
};
CODE:
assert_table (ix);
if (order == &PL_sv_yes)
SV_PTR_SWAP (zv, nv);
RETVAL = new_mpz();
(*table[ix].op) (RETVAL->m, coerce_mpz (RETVAL->m, zv), coerce_ulong (nv));
OUTPUT:
RETVAL
void
overload_lshifteq (z, n, o)
mpz_assume z
ulong_coerce n
order_noswap o
ALIAS:
GMP::Mpz::overload_rshifteq = 1
GMP::Mpz::overload_poweq = 2
PREINIT:
static_functable const struct {
void (*op) (mpz_ptr, mpz_srcptr, unsigned long);
} table[] = {
{ mpz_mul_2exp }, /* 0 */
{ mpz_fdiv_q_2exp }, /* 1 */
{ mpz_pow_ui }, /* 2 */
};
PPCODE:
assert_table (ix);
(*table[ix].op) (z->m, z->m, n);
XPUSHs(ST(0));
mpz
overload_abs (z, d1, d2)
mpz_assume z
dummy d1
dummy d2
ALIAS:
GMP::Mpz::overload_neg = 1
GMP::Mpz::overload_com = 2
GMP::Mpz::overload_sqrt = 3
PREINIT:
static_functable const struct {
void (*op) (mpz_ptr w, mpz_srcptr x);
} table[] = {
{ mpz_abs }, /* 0 */
{ mpz_neg }, /* 1 */
{ mpz_com }, /* 2 */
{ mpz_sqrt }, /* 3 */
};
CODE:
assert_table (ix);
RETVAL = new_mpz();
(*table[ix].op) (RETVAL->m, z->m);
OUTPUT:
RETVAL
void
overload_inc (z, d1, d2)
mpz_assume z
dummy d1
dummy d2
ALIAS:
GMP::Mpz::overload_dec = 1
PREINIT:
static_functable const struct {
void (*op) (mpz_ptr w, mpz_srcptr x, unsigned long y);
} table[] = {
{ mpz_add_ui }, /* 0 */
{ mpz_sub_ui }, /* 1 */
};
CODE:
assert_table (ix);
(*table[ix].op) (z->m, z->m, 1L);
int
overload_spaceship (xv, yv, order)
SV *xv
SV *yv
SV *order
PREINIT:
mpz x;
CODE:
TRACE (printf ("%s overload_spaceship\n", mpz_class));
MPZ_ASSUME (x, xv);
switch (use_sv (yv)) {
case USE_IVX:
RETVAL = mpz_cmp_si (x->m, SvIVX(yv));
break;
case USE_UVX:
RETVAL = mpz_cmp_ui (x->m, SvUVX(yv));
break;
case USE_PVX:
RETVAL = mpz_cmp (x->m, coerce_mpz (tmp_mpz_0, yv));
break;
case USE_NVX:
RETVAL = mpz_cmp_d (x->m, SvNVX(yv));
break;
case USE_MPZ:
RETVAL = mpz_cmp (x->m, SvMPZ(yv)->m);
break;
case USE_MPQ:
RETVAL = x_mpz_cmp_q (x->m, SvMPQ(yv)->m);
break;
case USE_MPF:
RETVAL = x_mpz_cmp_f (x->m, SvMPF(yv));
break;
default:
croak ("%s <=>: invalid operand", mpz_class);
}
RETVAL = SGN (RETVAL);
if (order == &PL_sv_yes)
RETVAL = -RETVAL;
OUTPUT:
RETVAL
bool
overload_bool (z, d1, d2)
mpz_assume z
dummy d1
dummy d2
ALIAS:
GMP::Mpz::overload_not = 1
CODE:
RETVAL = (mpz_sgn (z->m) != 0) ^ ix;
OUTPUT:
RETVAL
mpz
bin (n, k)
mpz_coerce n
ulong_coerce k
ALIAS:
GMP::Mpz::root = 1
PREINIT:
/* mpz_root returns an int, hence the cast */
static_functable const struct {
void (*op) (mpz_ptr, mpz_srcptr, unsigned long);
} table[] = {
{ mpz_bin_ui }, /* 0 */
{ (void (*)(mpz_ptr, mpz_srcptr, unsigned long)) mpz_root }, /* 1 */
};
CODE:
assert_table (ix);
RETVAL = new_mpz();
(*table[ix].op) (RETVAL->m, n, k);
OUTPUT:
RETVAL
void
cdiv (a, d)
mpz_coerce a
mpz_coerce d
ALIAS:
GMP::Mpz::fdiv = 1
GMP::Mpz::tdiv = 2
PREINIT:
static_functable const struct {
void (*op) (mpz_ptr, mpz_ptr, mpz_srcptr, mpz_srcptr);
} table[] = {
{ mpz_cdiv_qr }, /* 0 */
{ mpz_fdiv_qr }, /* 1 */
{ mpz_tdiv_qr }, /* 2 */
};
mpz q, r;
PPCODE:
assert_table (ix);
q = new_mpz();
r = new_mpz();
(*table[ix].op) (q->m, r->m, a, d);
EXTEND (SP, 2);
PUSHs (MPX_NEWMORTAL (q, mpz_class_hv));
PUSHs (MPX_NEWMORTAL (r, mpz_class_hv));
void
cdiv_2exp (a, d)
mpz_coerce a
ulong_coerce d
ALIAS:
GMP::Mpz::fdiv_2exp = 1
GMP::Mpz::tdiv_2exp = 2
PREINIT:
static_functable const struct {
void (*q) (mpz_ptr, mpz_srcptr, unsigned long);
void (*r) (mpz_ptr, mpz_srcptr, unsigned long);
} table[] = {
{ mpz_cdiv_q_2exp, mpz_cdiv_r_2exp }, /* 0 */
{ mpz_fdiv_q_2exp, mpz_fdiv_r_2exp }, /* 1 */
{ mpz_tdiv_q_2exp, mpz_tdiv_r_2exp }, /* 2 */
};
mpz q, r;
PPCODE:
assert_table (ix);
q = new_mpz();
r = new_mpz();
(*table[ix].q) (q->m, a, d);
(*table[ix].r) (r->m, a, d);
EXTEND (SP, 2);
PUSHs (MPX_NEWMORTAL (q, mpz_class_hv));
PUSHs (MPX_NEWMORTAL (r, mpz_class_hv));
bool
congruent_p (a, c, d)
mpz_coerce a
mpz_coerce c
mpz_coerce d
PREINIT:
CODE:
RETVAL = mpz_congruent_p (a, c, d);
OUTPUT:
RETVAL
bool
congruent_2exp_p (a, c, d)
mpz_coerce a
mpz_coerce c
ulong_coerce d
PREINIT:
CODE:
RETVAL = mpz_congruent_2exp_p (a, c, d);
OUTPUT:
RETVAL
mpz
divexact (a, d)
mpz_coerce a
mpz_coerce d
ALIAS:
GMP::Mpz::mod = 1
PREINIT:
static_functable const struct {
void (*op) (mpz_ptr, mpz_srcptr, mpz_srcptr);
} table[] = {
{ mpz_divexact }, /* 0 */
{ mpz_mod }, /* 1 */
};
CODE:
assert_table (ix);
RETVAL = new_mpz();
(*table[ix].op) (RETVAL->m, a, d);
OUTPUT:
RETVAL
bool
divisible_p (a, d)
mpz_coerce a
mpz_coerce d
CODE:
RETVAL = mpz_divisible_p (a, d);
OUTPUT:
RETVAL
bool
divisible_2exp_p (a, d)
mpz_coerce a
ulong_coerce d
CODE:
RETVAL = mpz_divisible_2exp_p (a, d);
OUTPUT:
RETVAL
bool
even_p (z)
mpz_coerce z
ALIAS:
GMP::Mpz::odd_p = 1
GMP::Mpz::perfect_square_p = 2
GMP::Mpz::perfect_power_p = 3
PREINIT:
static_functable const struct {
int (*op) (mpz_srcptr z);
} table[] = {
{ x_mpz_even_p }, /* 0 */
{ x_mpz_odd_p }, /* 1 */
{ mpz_perfect_square_p }, /* 2 */
{ mpz_perfect_power_p }, /* 3 */
};
CODE:
assert_table (ix);
RETVAL = (*table[ix].op) (z);
OUTPUT:
RETVAL
mpz
fac (n)
ulong_coerce n
ALIAS:
GMP::Mpz::fib = 1
GMP::Mpz::lucnum = 2
PREINIT:
static_functable const struct {
void (*op) (mpz_ptr r, unsigned long n);
} table[] = {
{ mpz_fac_ui }, /* 0 */
{ mpz_fib_ui }, /* 1 */
{ mpz_lucnum_ui }, /* 2 */
};
CODE:
assert_table (ix);
RETVAL = new_mpz();
(*table[ix].op) (RETVAL->m, n);
OUTPUT:
RETVAL
void
fib2 (n)
ulong_coerce n
ALIAS:
GMP::Mpz::lucnum2 = 1
PREINIT:
static_functable const struct {
void (*op) (mpz_ptr r, mpz_ptr r2, unsigned long n);
} table[] = {
{ mpz_fib2_ui }, /* 0 */
{ mpz_lucnum2_ui }, /* 1 */
};
mpz r, r2;
PPCODE:
assert_table (ix);
r = new_mpz();
r2 = new_mpz();
(*table[ix].op) (r->m, r2->m, n);
EXTEND (SP, 2);
PUSHs (MPX_NEWMORTAL (r, mpz_class_hv));
PUSHs (MPX_NEWMORTAL (r2, mpz_class_hv));
mpz
gcd (x, ...)
mpz_coerce x
ALIAS:
GMP::Mpz::lcm = 1
PREINIT:
static_functable const struct {
void (*op) (mpz_ptr w, mpz_srcptr x, mpz_srcptr y);
void (*op_ui) (mpz_ptr w, mpz_srcptr x, unsigned long y);
} table[] = {
/* cast to ignore ulong return from mpz_gcd_ui */
{ mpz_gcd,
(void (*) (mpz_ptr, mpz_srcptr, unsigned long)) mpz_gcd_ui }, /* 0 */
{ mpz_lcm, mpz_lcm_ui }, /* 1 */
};
int i;
SV *yv;
CODE:
assert_table (ix);
RETVAL = new_mpz();
if (items == 1)
mpz_set (RETVAL->m, x);
else
{
for (i = 1; i < items; i++)
{
yv = ST(i);
if (SvIOK(yv))
(*table[ix].op_ui) (RETVAL->m, x, ABS(SvIVX(yv)));
else
(*table[ix].op) (RETVAL->m, x, coerce_mpz (tmp_mpz_1, yv));
x = RETVAL->m;
}
}
OUTPUT:
RETVAL
void
gcdext (a, b)
mpz_coerce a
mpz_coerce b
PREINIT:
mpz g, x, y;
SV *sv;
PPCODE:
g = new_mpz();
x = new_mpz();
y = new_mpz();
mpz_gcdext (g->m, x->m, y->m, a, b);
EXTEND (SP, 3);
PUSHs (MPX_NEWMORTAL (g, mpz_class_hv));
PUSHs (MPX_NEWMORTAL (x, mpz_class_hv));
PUSHs (MPX_NEWMORTAL (y, mpz_class_hv));
unsigned long
hamdist (x, y)
mpz_coerce x
mpz_coerce y
CODE:
RETVAL = mpz_hamdist (x, y);
OUTPUT:
RETVAL
mpz
invert (a, m)
mpz_coerce a
mpz_coerce m
CODE:
RETVAL = new_mpz();
if (! mpz_invert (RETVAL->m, a, m))
{
free_mpz (RETVAL);
XSRETURN_UNDEF;
}
OUTPUT:
RETVAL
int
jacobi (a, b)
mpz_coerce a
mpz_coerce b
CODE:
RETVAL = mpz_jacobi (a, b);
OUTPUT:
RETVAL
int
kronecker (a, b)
SV *a
SV *b
CODE:
if (SvIOK(b))
RETVAL = mpz_kronecker_si (coerce_mpz(tmp_mpz_0,a), SvIVX(b));
else if (SvIOK(a))
RETVAL = mpz_si_kronecker (SvIVX(a), coerce_mpz(tmp_mpz_0,b));
else
RETVAL = mpz_kronecker (coerce_mpz(tmp_mpz_0,a),
coerce_mpz(tmp_mpz_1,b));
OUTPUT:
RETVAL
void
mpz_export (order, size, endian, nails, z)
int order
size_t size
int endian
size_t nails
mpz_coerce z
PREINIT:
size_t numb, count, bytes, actual_count;
char *data;
SV *sv;
PPCODE:
numb = 8*size - nails;
count = (mpz_sizeinbase (z, 2) + numb-1) / numb;
bytes = count * size;
New (GMP_MALLOC_ID, data, bytes+1, char);
mpz_export (data, &actual_count, order, size, endian, nails, z);
assert (count == actual_count);
data[bytes] = '\0';
sv = sv_newmortal(); sv_usepvn_mg (sv, data, bytes); PUSHs(sv);
mpz
mpz_import (order, size, endian, nails, sv)
int order
size_t size
int endian
size_t nails
SV *sv
PREINIT:
size_t count;
const char *data;
STRLEN len;
CODE:
data = SvPV (sv, len);
if ((len % size) != 0)
croak ("%s mpz_import: string not a multiple of the given size",
mpz_class);
count = len / size;
RETVAL = new_mpz();
mpz_import (RETVAL->m, count, order, size, endian, nails, data);
OUTPUT:
RETVAL
mpz
nextprime (z)
mpz_coerce z
CODE:
RETVAL = new_mpz();
mpz_nextprime (RETVAL->m, z);
OUTPUT:
RETVAL
unsigned long
popcount (x)
mpz_coerce x
CODE:
RETVAL = mpz_popcount (x);
OUTPUT:
RETVAL
mpz
powm (b, e, m)
mpz_coerce b
mpz_coerce e
mpz_coerce m
CODE:
RETVAL = new_mpz();
mpz_powm (RETVAL->m, b, e, m);
OUTPUT:
RETVAL
bool
probab_prime_p (z, n)
mpz_coerce z
ulong_coerce n
CODE:
RETVAL = mpz_probab_prime_p (z, n);
OUTPUT:
RETVAL
# No attempt to coerce here, only an mpz makes sense.
void
realloc (z, limbs)
mpz z
int limbs
CODE:
_mpz_realloc (z->m, limbs);
void
remove (z, f)
mpz_coerce z
mpz_coerce f
PREINIT:
SV *sv;
mpz rem;
unsigned long mult;
PPCODE:
rem = new_mpz();
mult = mpz_remove (rem->m, z, f);
EXTEND (SP, 2);
PUSHs (MPX_NEWMORTAL (rem, mpz_class_hv));
PUSHs (sv_2mortal (newSViv (mult)));
void
roote (z, n)
mpz_coerce z
ulong_coerce n
PREINIT:
SV *sv;
mpz root;
int exact;
PPCODE:
root = new_mpz();
exact = mpz_root (root->m, z, n);
EXTEND (SP, 2);
PUSHs (MPX_NEWMORTAL (root, mpz_class_hv));
sv = (exact ? &PL_sv_yes : &PL_sv_no); sv_2mortal(sv); PUSHs(sv);
void
rootrem (z, n)
mpz_coerce z
ulong_coerce n
PREINIT:
SV *sv;
mpz root;
mpz rem;
PPCODE:
root = new_mpz();
rem = new_mpz();
mpz_rootrem (root->m, rem->m, z, n);
EXTEND (SP, 2);
PUSHs (MPX_NEWMORTAL (root, mpz_class_hv));
PUSHs (MPX_NEWMORTAL (rem, mpz_class_hv));
# In the past scan0 and scan1 were described as returning ULONG_MAX which
# could be obtained in perl with ~0. That wasn't true on 64-bit systems
# (eg. alpha) with perl 5.005, since in that version IV and UV were still
# 32-bits.
#
# We changed in gmp 4.2 to just say ~0 for the not-found return. It's
# likely most people have used ~0 rather than POSIX::ULONG_MAX(), so this
# change should match existing usage. It only actually makes a difference
# in old perl, since recent versions have gone to 64-bits for IV and UV, the
# same as a ulong.
#
# In perl 5.005 we explicitly mask the mpz return down to 32-bits to get ~0.
# UV_MAX is no good, it reflects the size of the UV type (64-bits), rather
# than the size of the values one ought to be storing in an SV (32-bits).
gmp_UV
scan0 (z, start)
mpz_coerce z
ulong_coerce start
ALIAS:
GMP::Mpz::scan1 = 1
PREINIT:
static_functable const struct {
unsigned long (*op) (mpz_srcptr, unsigned long);
} table[] = {
{ mpz_scan0 }, /* 0 */
{ mpz_scan1 }, /* 1 */
};
CODE:
assert_table (ix);
RETVAL = (*table[ix].op) (z, start);
if (PERL_LT (5,6))
RETVAL &= 0xFFFFFFFF;
OUTPUT:
RETVAL
void
setbit (sv, bit)
SV *sv
ulong_coerce bit
ALIAS:
GMP::Mpz::clrbit = 1
GMP::Mpz::combit = 2
PREINIT:
static_functable const struct {
void (*op) (mpz_ptr, unsigned long);
} table[] = {
{ mpz_setbit }, /* 0 */
{ mpz_clrbit }, /* 1 */
{ mpz_combit }, /* 2 */
};
int use;
mpz z;
CODE:
use = use_sv (sv);
if (use == USE_MPZ && SvREFCNT(SvRV(sv)) == 1 && ! SvSMAGICAL(sv))
{
/* our operand is a non-magical mpz with a reference count of 1, so
we can just modify it */
(*table[ix].op) (SvMPZ(sv)->m, bit);
}
else
{
/* otherwise we need to make a new mpz, from whatever we have, and
operate on that, possibly invoking magic when storing back */
SV *new_sv;
mpz z = new_mpz ();
mpz_ptr coerce_ptr = coerce_mpz_using (z->m, sv, use);
if (coerce_ptr != z->m)
mpz_set (z->m, coerce_ptr);
(*table[ix].op) (z->m, bit);
new_sv = sv_bless (sv_setref_pv (sv_newmortal(), NULL, z),
mpz_class_hv);
SvSetMagicSV (sv, new_sv);
}
void
sqrtrem (z)
mpz_coerce z
PREINIT:
SV *sv;
mpz root;
mpz rem;
PPCODE:
root = new_mpz();
rem = new_mpz();
mpz_sqrtrem (root->m, rem->m, z);
EXTEND (SP, 2);
PUSHs (MPX_NEWMORTAL (root, mpz_class_hv));
PUSHs (MPX_NEWMORTAL (rem, mpz_class_hv));
size_t
sizeinbase (z, base)
mpz_coerce z
int base
CODE:
RETVAL = mpz_sizeinbase (z, base);
OUTPUT:
RETVAL
int
tstbit (z, bit)
mpz_coerce z
ulong_coerce bit
CODE:
RETVAL = mpz_tstbit (z, bit);
OUTPUT:
RETVAL
#------------------------------------------------------------------------------
MODULE = GMP PACKAGE = GMP::Mpq
mpq
mpq (...)
ALIAS:
GMP::Mpq::new = 1
CODE:
TRACE (printf ("%s new, ix=%ld, items=%d\n", mpq_class, ix, (int) items));
RETVAL = new_mpq();
switch (items) {
case 0:
mpq_set_ui (RETVAL->m, 0L, 1L);
break;
case 1:
{
mpq_ptr rp = RETVAL->m;
mpq_ptr cp = coerce_mpq (rp, ST(0));
if (cp != rp)
mpq_set (rp, cp);
}
break;
case 2:
{
mpz_ptr rp, cp;
rp = mpq_numref (RETVAL->m);
cp = coerce_mpz (rp, ST(0));
if (cp != rp)
mpz_set (rp, cp);
rp = mpq_denref (RETVAL->m);
cp = coerce_mpz (rp, ST(1));
if (cp != rp)
mpz_set (rp, cp);
}
break;
default:
croak ("%s new: invalid arguments", mpq_class);
}
OUTPUT:
RETVAL
void
overload_constant (str, pv, d1, ...)
const_string_assume str
SV *pv
dummy d1
PREINIT:
SV *sv;
mpq q;
PPCODE:
TRACE (printf ("%s constant: %s\n", mpq_class, str));
q = new_mpq();
if (mpq_set_str (q->m, str, 0) == 0)
{ sv = sv_bless (sv_setref_pv (sv_newmortal(), NULL, q), mpq_class_hv); }
else
{ free_mpq (q); sv = pv; }
XPUSHs(sv);
mpq
overload_copy (q, d1, d2)
mpq_assume q
dummy d1
dummy d2
CODE:
RETVAL = new_mpq();
mpq_set (RETVAL->m, q->m);
OUTPUT:
RETVAL
void
DESTROY (q)
mpq_assume q
CODE:
TRACE (printf ("%s DESTROY %p\n", mpq_class, q));
free_mpq (q);
malloced_string
overload_string (q, d1, d2)
mpq_assume q
dummy d1
dummy d2
CODE:
TRACE (printf ("%s overload_string %p\n", mpq_class, q));
RETVAL = mpq_get_str (NULL, 10, q->m);
OUTPUT:
RETVAL
mpq
overload_add (xv, yv, order)
SV *xv
SV *yv
SV *order
ALIAS:
GMP::Mpq::overload_sub = 1
GMP::Mpq::overload_mul = 2
GMP::Mpq::overload_div = 3
PREINIT:
static_functable const struct {
void (*op) (mpq_ptr, mpq_srcptr, mpq_srcptr);
} table[] = {
{ mpq_add }, /* 0 */
{ mpq_sub }, /* 1 */
{ mpq_mul }, /* 2 */
{ mpq_div }, /* 3 */
};
CODE:
TRACE (printf ("%s binary\n", mpf_class));
assert_table (ix);
if (order == &PL_sv_yes)
SV_PTR_SWAP (xv, yv);
RETVAL = new_mpq();
(*table[ix].op) (RETVAL->m,
coerce_mpq (tmp_mpq_0, xv),
coerce_mpq (tmp_mpq_1, yv));
OUTPUT:
RETVAL
void
overload_addeq (x, y, o)
mpq_assume x
mpq_coerce y
order_noswap o
ALIAS:
GMP::Mpq::overload_subeq = 1
GMP::Mpq::overload_muleq = 2
GMP::Mpq::overload_diveq = 3
PREINIT:
static_functable const struct {
void (*op) (mpq_ptr, mpq_srcptr, mpq_srcptr);
} table[] = {
{ mpq_add }, /* 0 */
{ mpq_sub }, /* 1 */
{ mpq_mul }, /* 2 */
{ mpq_div }, /* 3 */
};
PPCODE:
assert_table (ix);
(*table[ix].op) (x->m, x->m, y);
XPUSHs(ST(0));
mpq
overload_lshift (qv, nv, order)
SV *qv
SV *nv
SV *order
ALIAS:
GMP::Mpq::overload_rshift = 1
GMP::Mpq::overload_pow = 2
PREINIT:
static_functable const struct {
void (*op) (mpq_ptr, mpq_srcptr, unsigned long);
} table[] = {
{ mpq_mul_2exp }, /* 0 */
{ mpq_div_2exp }, /* 1 */
{ x_mpq_pow_ui }, /* 2 */
};
CODE:
assert_table (ix);
if (order == &PL_sv_yes)
SV_PTR_SWAP (qv, nv);
RETVAL = new_mpq();
(*table[ix].op) (RETVAL->m, coerce_mpq (RETVAL->m, qv), coerce_ulong (nv));
OUTPUT:
RETVAL
void
overload_lshifteq (q, n, o)
mpq_assume q
ulong_coerce n
order_noswap o
ALIAS:
GMP::Mpq::overload_rshifteq = 1
GMP::Mpq::overload_poweq = 2
PREINIT:
static_functable const struct {
void (*op) (mpq_ptr, mpq_srcptr, unsigned long);
} table[] = {
{ mpq_mul_2exp }, /* 0 */
{ mpq_div_2exp }, /* 1 */
{ x_mpq_pow_ui }, /* 2 */
};
PPCODE:
assert_table (ix);
(*table[ix].op) (q->m, q->m, n);
XPUSHs(ST(0));
void
overload_inc (q, d1, d2)
mpq_assume q
dummy d1
dummy d2
ALIAS:
GMP::Mpq::overload_dec = 1
PREINIT:
static_functable const struct {
void (*op) (mpz_ptr, mpz_srcptr, mpz_srcptr);
} table[] = {
{ mpz_add }, /* 0 */
{ mpz_sub }, /* 1 */
};
CODE:
assert_table (ix);
(*table[ix].op) (mpq_numref(q->m), mpq_numref(q->m), mpq_denref(q->m));
mpq
overload_abs (q, d1, d2)
mpq_assume q
dummy d1
dummy d2
ALIAS:
GMP::Mpq::overload_neg = 1
PREINIT:
static_functable const struct {
void (*op) (mpq_ptr w, mpq_srcptr x);
} table[] = {
{ mpq_abs }, /* 0 */
{ mpq_neg }, /* 1 */
};
CODE:
assert_table (ix);
RETVAL = new_mpq();
(*table[ix].op) (RETVAL->m, q->m);
OUTPUT:
RETVAL
int
overload_spaceship (x, y, order)
mpq_assume x
mpq_coerce y
SV *order
CODE:
RETVAL = mpq_cmp (x->m, y);
RETVAL = SGN (RETVAL);
if (order == &PL_sv_yes)
RETVAL = -RETVAL;
OUTPUT:
RETVAL
bool
overload_bool (q, d1, d2)
mpq_assume q
dummy d1
dummy d2
ALIAS:
GMP::Mpq::overload_not = 1
CODE:
RETVAL = (mpq_sgn (q->m) != 0) ^ ix;
OUTPUT:
RETVAL
bool
overload_eq (x, yv, d)
mpq_assume x
SV *yv
dummy d
ALIAS:
GMP::Mpq::overload_ne = 1
PREINIT:
int use;
CODE:
use = use_sv (yv);
switch (use) {
case USE_IVX:
case USE_UVX:
case USE_MPZ:
RETVAL = 0;
if (x_mpq_integer_p (x->m))
{
switch (use) {
case USE_IVX:
RETVAL = (mpz_cmp_si (mpq_numref(x->m), SvIVX(yv)) == 0);
break;
case USE_UVX:
RETVAL = (mpz_cmp_ui (mpq_numref(x->m), SvUVX(yv)) == 0);
break;
case USE_MPZ:
RETVAL = (mpz_cmp (mpq_numref(x->m), SvMPZ(yv)->m) == 0);
break;
}
}
break;
case USE_MPQ:
RETVAL = (mpq_equal (x->m, SvMPQ(yv)->m) != 0);
break;
default:
RETVAL = (mpq_equal (x->m, coerce_mpq_using (tmp_mpq_0, yv, use)) != 0);
break;
}
RETVAL ^= ix;
OUTPUT:
RETVAL
void
canonicalize (q)
mpq q
CODE:
mpq_canonicalize (q->m);
mpq
inv (q)
mpq_coerce q
CODE:
RETVAL = new_mpq();
mpq_inv (RETVAL->m, q);
OUTPUT:
RETVAL
mpz
num (q)
mpq q
ALIAS:
GMP::Mpq::den = 1
CODE:
RETVAL = new_mpz();
mpz_set (RETVAL->m, (ix == 0 ? mpq_numref(q->m) : mpq_denref(q->m)));
OUTPUT:
RETVAL
#------------------------------------------------------------------------------
MODULE = GMP PACKAGE = GMP::Mpf
mpf
mpf (...)
ALIAS:
GMP::Mpf::new = 1
PREINIT:
unsigned long prec;
CODE:
TRACE (printf ("%s new\n", mpf_class));
if (items > 2)
croak ("%s new: invalid arguments", mpf_class);
prec = (items == 2 ? coerce_ulong (ST(1)) : mpf_get_default_prec());
RETVAL = new_mpf (prec);
if (items >= 1)
{
SV *sv = ST(0);
my_mpf_set_sv_using (RETVAL, sv, use_sv(sv));
}
OUTPUT:
RETVAL
mpf
overload_constant (sv, d1, d2, ...)
SV *sv
dummy d1
dummy d2
CODE:
assert (SvPOK (sv));
TRACE (printf ("%s constant: %s\n", mpq_class, SvPVX(sv)));
RETVAL = new_mpf (mpf_get_default_prec());
my_mpf_set_svstr (RETVAL, sv);
OUTPUT:
RETVAL
mpf
overload_copy (f, d1, d2)
mpf_assume f
dummy d1
dummy d2
CODE:
TRACE (printf ("%s copy\n", mpf_class));
RETVAL = new_mpf (mpf_get_prec (f));
mpf_set (RETVAL, f);
OUTPUT:
RETVAL
void
DESTROY (f)
mpf_assume f
CODE:
TRACE (printf ("%s DESTROY %p\n", mpf_class, f));
mpf_clear (f);
Safefree (f);
assert_support (mpf_count--);
TRACE_ACTIVE ();
mpf
overload_add (x, y, order)
mpf_assume x
mpf_coerce_st0 y
SV *order
ALIAS:
GMP::Mpf::overload_sub = 1
GMP::Mpf::overload_mul = 2
GMP::Mpf::overload_div = 3
PREINIT:
static_functable const struct {
void (*op) (mpf_ptr, mpf_srcptr, mpf_srcptr);
} table[] = {
{ mpf_add }, /* 0 */
{ mpf_sub }, /* 1 */
{ mpf_mul }, /* 2 */
{ mpf_div }, /* 3 */
};
CODE:
assert_table (ix);
RETVAL = new_mpf (mpf_get_prec (x));
if (order == &PL_sv_yes)
MPF_PTR_SWAP (x, y);
(*table[ix].op) (RETVAL, x, y);
OUTPUT:
RETVAL
void
overload_addeq (x, y, o)
mpf_assume x
mpf_coerce_st0 y
order_noswap o
ALIAS:
GMP::Mpf::overload_subeq = 1
GMP::Mpf::overload_muleq = 2
GMP::Mpf::overload_diveq = 3
PREINIT:
static_functable const struct {
void (*op) (mpf_ptr, mpf_srcptr, mpf_srcptr);
} table[] = {
{ mpf_add }, /* 0 */
{ mpf_sub }, /* 1 */
{ mpf_mul }, /* 2 */
{ mpf_div }, /* 3 */
};
PPCODE:
assert_table (ix);
(*table[ix].op) (x, x, y);
XPUSHs(ST(0));
mpf
overload_lshift (fv, nv, order)
SV *fv
SV *nv
SV *order
ALIAS:
GMP::Mpf::overload_rshift = 1
GMP::Mpf::overload_pow = 2
PREINIT:
static_functable const struct {
void (*op) (mpf_ptr, mpf_srcptr, unsigned long);
} table[] = {
{ mpf_mul_2exp }, /* 0 */
{ mpf_div_2exp }, /* 1 */
{ mpf_pow_ui }, /* 2 */
};
mpf f;
unsigned long prec;
CODE:
assert_table (ix);
MPF_ASSUME (f, fv);
prec = mpf_get_prec (f);
if (order == &PL_sv_yes)
SV_PTR_SWAP (fv, nv);
f = coerce_mpf (tmp_mpf_0, fv, prec);
RETVAL = new_mpf (prec);
(*table[ix].op) (RETVAL, f, coerce_ulong (nv));
OUTPUT:
RETVAL
void
overload_lshifteq (f, n, o)
mpf_assume f
ulong_coerce n
order_noswap o
ALIAS:
GMP::Mpf::overload_rshifteq = 1
GMP::Mpf::overload_poweq = 2
PREINIT:
static_functable const struct {
void (*op) (mpf_ptr, mpf_srcptr, unsigned long);
} table[] = {
{ mpf_mul_2exp }, /* 0 */
{ mpf_div_2exp }, /* 1 */
{ mpf_pow_ui }, /* 2 */
};
PPCODE:
assert_table (ix);
(*table[ix].op) (f, f, n);
XPUSHs(ST(0));
mpf
overload_abs (f, d1, d2)
mpf_assume f
dummy d1
dummy d2
ALIAS:
GMP::Mpf::overload_neg = 1
GMP::Mpf::overload_sqrt = 2
PREINIT:
static_functable const struct {
void (*op) (mpf_ptr w, mpf_srcptr x);
} table[] = {
{ mpf_abs }, /* 0 */
{ mpf_neg }, /* 1 */
{ mpf_sqrt }, /* 2 */
};
CODE:
assert_table (ix);
RETVAL = new_mpf (mpf_get_prec (f));
(*table[ix].op) (RETVAL, f);
OUTPUT:
RETVAL
void
overload_inc (f, d1, d2)
mpf_assume f
dummy d1
dummy d2
ALIAS:
GMP::Mpf::overload_dec = 1
PREINIT:
static_functable const struct {
void (*op) (mpf_ptr w, mpf_srcptr x, unsigned long y);
} table[] = {
{ mpf_add_ui }, /* 0 */
{ mpf_sub_ui }, /* 1 */
};
CODE:
assert_table (ix);
(*table[ix].op) (f, f, 1L);
int
overload_spaceship (xv, yv, order)
SV *xv
SV *yv
SV *order
PREINIT:
mpf x;
CODE:
MPF_ASSUME (x, xv);
switch (use_sv (yv)) {
case USE_IVX:
RETVAL = mpf_cmp_si (x, SvIVX(yv));
break;
case USE_UVX:
RETVAL = mpf_cmp_ui (x, SvUVX(yv));
break;
case USE_NVX:
RETVAL = mpf_cmp_d (x, SvNVX(yv));
break;
case USE_PVX:
{
STRLEN len;
const char *str = SvPV (yv, len);
/* enough for all digits of the string */
tmp_mpf_set_prec (tmp_mpf_0, strlen(str)+64);
if (mpf_set_str (tmp_mpf_0->m, str, 10) != 0)
croak ("%s <=>: invalid string format", mpf_class);
RETVAL = mpf_cmp (x, tmp_mpf_0->m);
}
break;
case USE_MPZ:
RETVAL = - x_mpz_cmp_f (SvMPZ(yv)->m, x);
break;
case USE_MPF:
RETVAL = mpf_cmp (x, SvMPF(yv));
break;
default:
RETVAL = mpq_cmp (coerce_mpq (tmp_mpq_0, xv),
coerce_mpq (tmp_mpq_1, yv));
break;
}
RETVAL = SGN (RETVAL);
if (order == &PL_sv_yes)
RETVAL = -RETVAL;
OUTPUT:
RETVAL
bool
overload_bool (f, d1, d2)
mpf_assume f
dummy d1
dummy d2
ALIAS:
GMP::Mpf::overload_not = 1
CODE:
RETVAL = (mpf_sgn (f) != 0) ^ ix;
OUTPUT:
RETVAL
mpf
ceil (f)
mpf_coerce_def f
ALIAS:
GMP::Mpf::floor = 1
GMP::Mpf::trunc = 2
PREINIT:
static_functable const struct {
void (*op) (mpf_ptr w, mpf_srcptr x);
} table[] = {
{ mpf_ceil }, /* 0 */
{ mpf_floor }, /* 1 */
{ mpf_trunc }, /* 2 */
};
CODE:
assert_table (ix);
RETVAL = new_mpf (mpf_get_prec (f));
(*table[ix].op) (RETVAL, f);
OUTPUT:
RETVAL
unsigned long
get_default_prec ()
CODE:
RETVAL = mpf_get_default_prec();
OUTPUT:
RETVAL
unsigned long
get_prec (f)
mpf_coerce_def f
CODE:
RETVAL = mpf_get_prec (f);
OUTPUT:
RETVAL
bool
mpf_eq (xv, yv, bits)
SV *xv
SV *yv
ulong_coerce bits
PREINIT:
mpf x, y;
CODE:
TRACE (printf ("%s eq\n", mpf_class));
coerce_mpf_pair (&x,xv, &y,yv);
RETVAL = mpf_eq (x, y, bits);
OUTPUT:
RETVAL
mpf
reldiff (xv, yv)
SV *xv
SV *yv
PREINIT:
mpf x, y;
unsigned long prec;
CODE:
TRACE (printf ("%s reldiff\n", mpf_class));
prec = coerce_mpf_pair (&x,xv, &y,yv);
RETVAL = new_mpf (prec);
mpf_reldiff (RETVAL, x, y);
OUTPUT:
RETVAL
void
set_default_prec (prec)
ulong_coerce prec
CODE:
TRACE (printf ("%s set_default_prec %lu\n", mpf_class, prec));
mpf_set_default_prec (prec);
void
set_prec (sv, prec)
SV *sv
ulong_coerce prec
PREINIT:
mpf_ptr old_f, new_f;
int use;
CODE:
TRACE (printf ("%s set_prec to %lu\n", mpf_class, prec));
use = use_sv (sv);
if (use == USE_MPF)
{
old_f = SvMPF(sv);
if (SvREFCNT(SvRV(sv)) == 1)
mpf_set_prec (old_f, prec);
else
{
TRACE (printf (" fork new mpf\n"));
new_f = new_mpf (prec);
mpf_set (new_f, old_f);
goto setref;
}
}
else
{
TRACE (printf (" coerce to mpf\n"));
new_f = new_mpf (prec);
my_mpf_set_sv_using (new_f, sv, use);
setref:
sv_bless (sv_setref_pv (sv, NULL, new_f), mpf_class_hv);
}
#------------------------------------------------------------------------------
MODULE = GMP PACKAGE = GMP::Rand
randstate
new (...)
ALIAS:
GMP::Rand::randstate = 1
CODE:
TRACE (printf ("%s new\n", rand_class));
New (GMP_MALLOC_ID, RETVAL, 1, __gmp_randstate_struct);
TRACE (printf (" RETVAL %p\n", RETVAL));
assert_support (rand_count++);
TRACE_ACTIVE ();
if (items == 0)
{
gmp_randinit_default (RETVAL);
}
else
{
if (SvROK (ST(0)) && sv_derived_from (ST(0), rand_class))
{
if (items != 1)
goto invalid;
gmp_randinit_set (RETVAL, SvRANDSTATE (ST(0)));
}
else
{
STRLEN len;
const char *method = SvPV (ST(0), len);
assert (len == strlen (method));
if (strcmp (method, "lc_2exp") == 0)
{
if (items != 4)
goto invalid;
gmp_randinit_lc_2exp (RETVAL,
coerce_mpz (tmp_mpz_0, ST(1)),
coerce_ulong (ST(2)),
coerce_ulong (ST(3)));
}
else if (strcmp (method, "lc_2exp_size") == 0)
{
if (items != 2)
goto invalid;
if (! gmp_randinit_lc_2exp_size (RETVAL, coerce_ulong (ST(1))))
{
Safefree (RETVAL);
XSRETURN_UNDEF;
}
}
else if (strcmp (method, "mt") == 0)
{
if (items != 1)
goto invalid;
gmp_randinit_mt (RETVAL);
}
else
{
invalid:
croak ("%s new: invalid arguments", rand_class);
}
}
}
OUTPUT:
RETVAL
void
DESTROY (r)
randstate r
CODE:
TRACE (printf ("%s DESTROY\n", rand_class));
gmp_randclear (r);
Safefree (r);
assert_support (rand_count--);
TRACE_ACTIVE ();
void
seed (r, z)
randstate r
mpz_coerce z
CODE:
gmp_randseed (r, z);
mpz
mpz_urandomb (r, bits)
randstate r
ulong_coerce bits
ALIAS:
GMP::Rand::mpz_rrandomb = 1
PREINIT:
static_functable const struct {
void (*fun) (mpz_ptr, gmp_randstate_t r, unsigned long bits);
} table[] = {
{ mpz_urandomb }, /* 0 */
{ mpz_rrandomb }, /* 1 */
};
CODE:
assert_table (ix);
RETVAL = new_mpz();
(*table[ix].fun) (RETVAL->m, r, bits);
OUTPUT:
RETVAL
mpz
mpz_urandomm (r, m)
randstate r
mpz_coerce m
CODE:
RETVAL = new_mpz();
mpz_urandomm (RETVAL->m, r, m);
OUTPUT:
RETVAL
mpf
mpf_urandomb (r, bits)
randstate r
ulong_coerce bits
CODE:
RETVAL = new_mpf (bits);
mpf_urandomb (RETVAL, r, bits);
OUTPUT:
RETVAL
unsigned long
gmp_urandomb_ui (r, bits)
randstate r
ulong_coerce bits
ALIAS:
GMP::Rand::gmp_urandomm_ui = 1
PREINIT:
static_functable const struct {
unsigned long (*fun) (gmp_randstate_t r, unsigned long bits);
} table[] = {
{ gmp_urandomb_ui }, /* 0 */
{ gmp_urandomm_ui }, /* 1 */
};
CODE:
assert_table (ix);
RETVAL = (*table[ix].fun) (r, bits);
OUTPUT:
RETVAL
|