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
|
/*
* Copyright (C) 2017 Caio Lima <ticaiolima@gmail.com>
* Copyright (C) 2017-2024 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* Parts of the implementation below:
*
* Copyright 2017 the V8 project authors. All rights reserved.
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*
*
* Copyright (c) 2014 the Dart project authors. Please see the AUTHORS file [1]
* for details. All rights reserved. Use of this source code is governed by a
* BSD-style license that can be found in the LICENSE file [2].
*
* [1] https://github.com/dart-lang/sdk/blob/master/AUTHORS
* [2] https://github.com/dart-lang/sdk/blob/master/LICENSE
*
* Copyright 2009 The Go Authors. All rights reserved.
* Use of this source code is governed by a BSD-style
* license that can be found in the LICENSE file [3].
*
* [3] https://golang.org/LICENSE
*/
#include "config.h"
#include "JSBigInt.h"
#include "BigIntObject.h"
#include "JSCJSValueInlines.h"
#include "JSObjectInlines.h"
#include "MathCommon.h"
#include "ParseInt.h"
#include "StructureInlines.h"
#include <algorithm>
#include <wtf/Hasher.h>
#include <wtf/Int128.h>
#include <wtf/MathExtras.h>
WTF_ALLOW_UNSAFE_BUFFER_USAGE_BEGIN
namespace JSC {
const ClassInfo JSBigInt::s_info = { "BigInt"_s, nullptr, nullptr, nullptr, CREATE_METHOD_TABLE(JSBigInt) };
JSBigInt::JSBigInt(VM& vm, Structure* structure, Digit* data, unsigned length)
: Base(vm, structure)
, m_length(length)
, m_data(vm, this, data)
{ }
template<typename Visitor>
void JSBigInt::visitChildrenImpl(JSCell* cell, Visitor& visitor)
{
auto* thisObject = jsCast<JSBigInt*>(cell);
ASSERT_GC_OBJECT_INHERITS(thisObject, info());
Base::visitChildren(thisObject, visitor);
if (auto* data = thisObject->m_data.getUnsafe())
visitor.markAuxiliary(data);
}
DEFINE_VISIT_CHILDREN(JSBigInt);
void JSBigInt::initialize(InitializationType initType)
{
if (initType == InitializationType::WithZero)
memset(dataStorage(), 0, length() * sizeof(Digit));
}
Structure* JSBigInt::createStructure(VM& vm, JSGlobalObject* globalObject, JSValue prototype)
{
return Structure::create(vm, globalObject, prototype, TypeInfo(HeapBigIntType, StructureFlags), info());
}
inline JSBigInt* JSBigInt::createZero(JSGlobalObject* nullOrGlobalObjectForOOM, VM& vm)
{
return createWithLength(nullOrGlobalObjectForOOM, vm, 0);
}
JSBigInt* JSBigInt::createZero(JSGlobalObject* globalObject)
{
return createZero(globalObject, globalObject->vm());
}
JSBigInt* JSBigInt::tryCreateZero(VM& vm)
{
return createZero(nullptr, vm);
}
inline JSBigInt* JSBigInt::createWithLength(JSGlobalObject* nullOrGlobalObjectForOOM, VM& vm, unsigned length)
{
if (UNLIKELY(length > maxLength)) {
if (nullOrGlobalObjectForOOM) {
auto scope = DECLARE_THROW_SCOPE(vm);
throwOutOfMemoryError(nullOrGlobalObjectForOOM, scope, "BigInt generated from this operation is too big"_s);
}
return nullptr;
}
ASSERT(length <= maxLength);
void* data = vm.primitiveGigacageAuxiliarySpace().allocate(vm, length * sizeof(Digit), nullptr, AllocationFailureMode::ReturnNull);
if (UNLIKELY(!data)) {
if (nullOrGlobalObjectForOOM) {
auto scope = DECLARE_THROW_SCOPE(vm);
throwOutOfMemoryError(nullOrGlobalObjectForOOM, scope);
}
return nullptr;
}
JSBigInt* bigInt = new (NotNull, allocateCell<JSBigInt>(vm)) JSBigInt(vm, vm.bigIntStructure.get(), reinterpret_cast<Digit*>(data), length);
bigInt->finishCreation(vm);
return bigInt;
}
JSBigInt* JSBigInt::tryCreateWithLength(VM& vm, unsigned length)
{
return createWithLength(nullptr, vm, length);
}
JSBigInt* JSBigInt::createWithLength(JSGlobalObject* globalObject, unsigned length)
{
return createWithLength(globalObject, globalObject->vm(), length);
}
inline JSBigInt* JSBigInt::createFrom(JSGlobalObject* nullOrGlobalObjectForOOM, VM& vm, int32_t value)
{
if (!value)
return createZero(nullOrGlobalObjectForOOM, vm);
JSBigInt* bigInt = createWithLength(nullOrGlobalObjectForOOM, vm, 1);
if (UNLIKELY(!bigInt))
return nullptr;
if (value < 0) {
bigInt->setDigit(0, static_cast<Digit>(-1 * static_cast<int64_t>(value)));
bigInt->setSign(true);
} else
bigInt->setDigit(0, static_cast<Digit>(value));
return bigInt;
}
JSBigInt* JSBigInt::createFrom(JSGlobalObject* globalObject, int32_t value)
{
return createFrom(globalObject, globalObject->vm(), value);
}
JSBigInt* JSBigInt::tryCreateFrom(VM& vm, int32_t value)
{
return createFrom(nullptr, vm, value);
}
JSBigInt* JSBigInt::createFrom(JSGlobalObject* globalObject, uint32_t value)
{
VM& vm = globalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
if (!value)
RELEASE_AND_RETURN(scope, createZero(globalObject));
JSBigInt* bigInt = createWithLength(globalObject, 1);
RETURN_IF_EXCEPTION(scope, nullptr);
bigInt->setDigit(0, static_cast<Digit>(value));
return bigInt;
}
inline JSBigInt* JSBigInt::createFromImpl(JSGlobalObject* globalObject, uint64_t value, bool sign)
{
VM& vm = globalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
if (!value)
RELEASE_AND_RETURN(scope, createZero(globalObject));
// This path is not just an optimization: because we do not call rightTrim at the end of this function,
// it would be a bug to create a BigInt with length=2 in this case.
if (sizeof(Digit) == 8 || value <= UINT32_MAX) {
JSBigInt* bigInt = createWithLength(globalObject, 1);
RETURN_IF_EXCEPTION(scope, nullptr);
bigInt->setDigit(0, static_cast<Digit>(value));
bigInt->setSign(sign);
return bigInt;
}
ASSERT(sizeof(Digit) == 4);
JSBigInt* bigInt = createWithLength(globalObject, 2);
RETURN_IF_EXCEPTION(scope, nullptr);
Digit lowBits = static_cast<Digit>(value & 0xffffffff);
Digit highBits = static_cast<Digit>((value >> 32) & 0xffffffff);
ASSERT(highBits);
bigInt->setDigit(0, lowBits);
bigInt->setDigit(1, highBits);
bigInt->setSign(sign);
return bigInt;
}
JSBigInt* JSBigInt::createFrom(JSGlobalObject* globalObject, uint64_t value)
{
return createFromImpl(globalObject, value, false);
}
JSBigInt* JSBigInt::createFrom(JSGlobalObject* globalObject, int64_t value)
{
uint64_t unsignedValue;
bool sign = false;
if (value < 0) {
unsignedValue = static_cast<uint64_t>(-(value + 1)) + 1;
sign = true;
} else
unsignedValue = value;
return createFromImpl(globalObject, unsignedValue, sign);
}
JSBigInt* JSBigInt::createFrom(JSGlobalObject* globalObject, Int128 value)
{
VM& vm = globalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
if (!value)
RELEASE_AND_RETURN(scope, createZero(globalObject));
UInt128 unsignedValue;
bool sign = false;
if (value < 0) {
unsignedValue = static_cast<UInt128>(-(value + 1)) + 1;
sign = true;
} else
unsignedValue = value;
if (unsignedValue <= UINT64_MAX)
RELEASE_AND_RETURN(scope, createFromImpl(globalObject, static_cast<uint64_t>(unsignedValue), sign));
if constexpr (sizeof(Digit) == 8) {
JSBigInt* bigInt = createWithLength(globalObject, 2);
RETURN_IF_EXCEPTION(scope, nullptr);
Digit lowBits = static_cast<Digit>(static_cast<uint64_t>(unsignedValue));
Digit highBits = static_cast<Digit>(static_cast<uint64_t>(unsignedValue >> 64));
ASSERT(highBits);
bigInt->setDigit(0, lowBits);
bigInt->setDigit(1, highBits);
bigInt->setSign(sign);
return bigInt;
}
ASSERT(sizeof(Digit) == 4);
Digit digit0 = static_cast<Digit>(static_cast<uint64_t>(unsignedValue));
Digit digit1 = static_cast<Digit>(static_cast<uint64_t>(unsignedValue >> 32));
Digit digit2 = static_cast<Digit>(static_cast<uint64_t>(unsignedValue >> 64));
Digit digit3 = static_cast<Digit>(static_cast<uint64_t>(unsignedValue >> 96));
ASSERT(digit2 || digit3);
int length = digit3 ? 4 : 3;
JSBigInt* bigInt = createWithLength(globalObject, length);
RETURN_IF_EXCEPTION(scope, nullptr);
bigInt->setDigit(0, digit0);
bigInt->setDigit(1, digit1);
bigInt->setDigit(2, digit2);
if (digit3)
bigInt->setDigit(3, digit3);
bigInt->setSign(sign);
return bigInt;
}
JSBigInt* JSBigInt::createFrom(JSGlobalObject* globalObject, bool value)
{
VM& vm = globalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
if (!value)
RELEASE_AND_RETURN(scope, createZero(globalObject));
JSBigInt* bigInt = createWithLength(globalObject, 1);
RETURN_IF_EXCEPTION(scope, nullptr);
bigInt->setDigit(0, static_cast<Digit>(value));
return bigInt;
}
JSBigInt* JSBigInt::createFrom(JSGlobalObject* globalObject, double value)
{
VM& vm = globalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
ASSERT(isInteger(value));
if (!value)
RELEASE_AND_RETURN(scope, createZero(globalObject));
bool sign = value < 0; // -0 was already handled above.
uint64_t doubleBits = std::bit_cast<uint64_t>(value);
int32_t rawExponent = static_cast<int32_t>(doubleBits >> doublePhysicalMantissaSize) & 0x7ff;
ASSERT(rawExponent != 0x7ff); // Since value is integer, exponent should not be 0x7ff (full bits, used for infinity etc.).
ASSERT(rawExponent >= 0x3ff); // Since value is integer, exponent should be >= 0 + bias (0x3ff).
int32_t exponent = rawExponent - 0x3ff;
int32_t digits = exponent / digitBits + 1;
JSBigInt* result = createWithLength(globalObject, digits);
RETURN_IF_EXCEPTION(scope, nullptr);
ASSERT(result);
result->initialize(InitializationType::WithZero);
result->setSign(sign);
// We construct a BigInt from the double value by shifting its mantissa
// according to its exponent and mapping the bit pattern onto digits.
//
// <----------- bitlength = exponent + 1 ----------->
// <----- 52 ------> <------ trailing zeroes ------>
// mantissa: 1yyyyyyyyyyyyyyyyy 0000000000000000000000000000000
// digits: 0001xxxx xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx
// <--> <------>
// msdTopBit digitBits
//
uint64_t mantissa = (doubleBits & doublePhysicalMantissaMask) | doubleMantissaHiddenBit;
int32_t mantissaTopBit = doubleMantissaSize - 1; // 0-indexed.
// 0-indexed position of most significant bit in the most significant digit.
int32_t msdTopBit = exponent % digitBits;
// Number of unused bits in mantissa. We'll keep them shifted to the
// left (i.e. most significant part) of the underlying uint64_t.
int32_t remainingMantissaBits = 0;
// Next digit under construction.
Digit digit = 0;
// First, build the MSD by shifting the mantissa appropriately.
if (msdTopBit < mantissaTopBit) {
remainingMantissaBits = mantissaTopBit - msdTopBit;
digit = static_cast<Digit>(mantissa >> remainingMantissaBits);
mantissa = mantissa << (64 - remainingMantissaBits);
} else {
ASSERT(msdTopBit >= mantissaTopBit);
digit = static_cast<Digit>(mantissa << (msdTopBit - mantissaTopBit));
mantissa = 0;
}
result->setDigit(digits - 1, digit);
// Then fill in the rest of the digits.
for (int32_t digitIndex = digits - 2; digitIndex >= 0; digitIndex--) {
if (remainingMantissaBits > 0) {
remainingMantissaBits -= digitBits;
if constexpr (sizeof(Digit) == 4) {
digit = mantissa >> 32;
mantissa = mantissa << 32;
} else {
ASSERT(sizeof(Digit) == 8);
digit = mantissa;
mantissa = 0;
}
} else
digit = 0;
result->setDigit(digitIndex, digit);
}
RELEASE_AND_RETURN(scope, result->rightTrim(globalObject));
}
JSValue JSBigInt::toPrimitive(JSGlobalObject*, PreferredPrimitiveType) const
{
return const_cast<JSBigInt*>(this);
}
JSValue JSBigInt::parseInt(JSGlobalObject* globalObject, StringView s, ErrorParseMode parserMode)
{
if (s.is8Bit())
return parseInt(globalObject, s.span8(), parserMode);
return parseInt(globalObject, s.span16(), parserMode);
}
JSValue JSBigInt::parseInt(JSGlobalObject* nullOrGlobalObjectForOOM, VM& vm, StringView s, uint8_t radix, ErrorParseMode parserMode, ParseIntSign sign)
{
if (s.is8Bit())
return parseInt(nullOrGlobalObjectForOOM, vm, s.span8(), 0, radix, parserMode, sign, ParseIntMode::DisallowEmptyString);
return parseInt(nullOrGlobalObjectForOOM, vm, s.span16(), 0, radix, parserMode, sign, ParseIntMode::DisallowEmptyString);
}
JSValue JSBigInt::stringToBigInt(JSGlobalObject* globalObject, StringView s)
{
return parseInt(globalObject, s, ErrorParseMode::IgnoreExceptions);
}
String JSBigInt::toString(JSGlobalObject* globalObject, unsigned radix)
{
if (this->isZero())
return globalObject->vm().smallStrings.singleCharacterStringRep('0');
if (hasOneBitSet(radix))
return toStringBasePowerOfTwo(globalObject->vm(), globalObject, this, radix);
return toStringGeneric(globalObject->vm(), globalObject, this, radix);
}
String JSBigInt::tryGetString(VM& vm, JSBigInt* bigInt, unsigned radix)
{
if (bigInt->isZero())
return vm.smallStrings.singleCharacterStringRep('0');
if (hasOneBitSet(radix))
return toStringBasePowerOfTwo(vm, nullptr, bigInt, radix);
return toStringGeneric(vm, nullptr, bigInt, radix);
}
class HeapBigIntImpl {
public:
explicit HeapBigIntImpl(JSBigInt* bigInt)
: m_bigInt(bigInt)
{ }
ALWAYS_INLINE bool isZero() { return m_bigInt->isZero(); }
ALWAYS_INLINE bool sign() { return m_bigInt->sign(); }
ALWAYS_INLINE unsigned length() { return m_bigInt->length(); }
ALWAYS_INLINE JSBigInt::Digit digit(unsigned i) { return m_bigInt->digit(i); }
ALWAYS_INLINE JSBigInt* toHeapBigInt(JSGlobalObject*, VM&) { return m_bigInt; }
ALWAYS_INLINE JSBigInt* toHeapBigInt(JSGlobalObject*) { return m_bigInt; }
private:
friend struct JSBigInt::ImplResult;
JSBigInt* m_bigInt;
};
class Int32BigIntImpl {
public:
explicit Int32BigIntImpl(int32_t value)
: m_value(value)
{ }
ALWAYS_INLINE bool isZero() { return !m_value; }
ALWAYS_INLINE bool sign() { return m_value < 0; }
ALWAYS_INLINE unsigned length() { return isZero() ? 0 : 1; }
ALWAYS_INLINE JSBigInt::Digit digit(unsigned i)
{
ASSERT(length());
ASSERT_UNUSED(i, i == 0);
if (sign())
return static_cast<JSBigInt::Digit>(WTF::negate(static_cast<int64_t>(m_value)));
return m_value;
}
ALWAYS_INLINE JSBigInt* toHeapBigInt(JSGlobalObject* nullOrGlobalObjectForOOM, VM& vm)
{
return JSBigInt::createFrom(nullOrGlobalObjectForOOM, vm, m_value);
}
ALWAYS_INLINE JSBigInt* toHeapBigInt(JSGlobalObject* globalObject)
{
return JSBigInt::createFrom(globalObject, m_value);
}
private:
friend struct JSBigInt::ImplResult;
int32_t m_value;
};
class Int64BigIntImpl {
public:
static constexpr unsigned numDigits = isRegister64Bit() ? 1 : 2;
explicit Int64BigIntImpl(int64_t value)
: m_value(value)
, m_sign(value < 0)
{
}
explicit Int64BigIntImpl(uint64_t value)
: m_value(value)
, m_sign(false)
{ }
ALWAYS_INLINE bool isZero() { return !m_value; }
ALWAYS_INLINE bool sign() { return m_sign; }
ALWAYS_INLINE unsigned length() { return isZero() ? 0 : numDigits; }
ALWAYS_INLINE JSBigInt::Digit digit(unsigned i)
{
ASSERT_UNUSED(i, i < length());
#if CPU(REGISTER64)
if (sign())
return static_cast<JSBigInt::Digit>(WTF::negate(static_cast<int64_t>(m_value)));
return m_value;
#else
static_assert(sizeof(JSBigInt::Digit) == 4);
if (sign())
return static_cast<JSBigInt::Digit>(WTF::negate(static_cast<int64_t>(m_value)) >> (32 * i));
return static_cast<JSBigInt::Digit>(m_value >> (32 * i));
#endif
}
private:
friend struct JSBigInt::ImplResult;
uint64_t m_value;
bool m_sign;
};
ALWAYS_INLINE JSBigInt::ImplResult::ImplResult(HeapBigIntImpl& heapImpl)
: payload(heapImpl.m_bigInt)
{ }
ALWAYS_INLINE JSBigInt::ImplResult::ImplResult(JSBigInt* heapBigInt)
: payload(heapBigInt)
{ }
#if USE(BIGINT32)
ALWAYS_INLINE JSBigInt::ImplResult::ImplResult(Int32BigIntImpl& int32Impl)
: payload(jsBigInt32(int32Impl.m_value))
{ }
#endif
ALWAYS_INLINE JSBigInt::ImplResult::ImplResult(JSValue value)
: payload(value)
{ }
static ALWAYS_INLINE JSValue tryConvertToBigInt32(JSBigInt::ImplResult implResult)
{
if (!implResult.payload)
return JSValue();
if (implResult.payload.isBigInt32())
return implResult.payload;
return tryConvertToBigInt32(implResult.payload.asHeapBigInt());
}
static ALWAYS_INLINE JSBigInt::ImplResult zeroImpl(JSGlobalObject* globalObject)
{
#if USE(BIGINT32)
UNUSED_PARAM(globalObject);
return jsBigInt32(0);
#else
return JSBigInt::createZero(globalObject);
#endif
}
// Multiplies {this} with {factor} and adds {summand} to the result.
void JSBigInt::inplaceMultiplyAdd(Digit factor, Digit summand)
{
internalMultiplyAdd(HeapBigIntImpl { this }, factor, summand, length(), this);
}
template <typename BigIntImpl1, typename BigIntImpl2>
JSBigInt::ImplResult JSBigInt::exponentiateImpl(JSGlobalObject* globalObject, BigIntImpl1 base, BigIntImpl2 exponent)
{
VM& vm = globalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
if (exponent.sign()) {
throwRangeError(globalObject, scope, "Negative exponent is not allowed"_s);
return nullptr;
}
// 2. If base is 0n and exponent is 0n, return 1n.
if (exponent.isZero())
RELEASE_AND_RETURN(scope, JSBigInt::createFrom(globalObject, 1));
// 3. Return a BigInt representing the mathematical value of base raised
// to the power exponent.
if (base.isZero())
return { base };
if (base.length() == 1 && base.digit(0) == 1) {
// (-1) ** even_number == 1.
if (base.sign() && !(exponent.digit(0) & 1))
RELEASE_AND_RETURN(scope, JSBigInt::unaryMinusImpl(globalObject, base));
// (-1) ** odd_number == -1; 1 ** anything == 1.
return { base };
}
// For all bases >= 2, very large exponents would lead to unrepresentable
// results.
static_assert(maxLengthBits < std::numeric_limits<Digit>::max(), "maxLengthBits needs to be less than digit::max()");
if (exponent.length() > 1) {
throwOutOfMemoryError(globalObject, scope, "BigInt generated from this operation is too big"_s);
return nullptr;
}
Digit expValue = exponent.digit(0);
if (expValue == 1)
return { base };
if (expValue >= maxLengthBits) {
throwOutOfMemoryError(globalObject, scope, "BigInt generated from this operation is too big"_s);
return nullptr;
}
static_assert(maxLengthBits <= maxInt, "maxLengthBits needs to be <= maxInt");
int n = static_cast<int>(expValue);
if (base.length() == 1 && base.digit(0) == 2) {
// Fast path for 2^n.
int neededDigits = 1 + (n / digitBits);
JSBigInt* result = JSBigInt::createWithLength(globalObject, neededDigits);
RETURN_IF_EXCEPTION(scope, nullptr);
result->initialize(InitializationType::WithZero);
// All bits are zero. Now set the n-th bit.
Digit msd = static_cast<Digit>(1) << (n % digitBits);
result->setDigit(neededDigits - 1, msd);
// Result is negative for odd powers of -2n.
if (base.sign())
result->setSign(static_cast<bool>(n & 1));
return { result };
}
JSBigInt* result = nullptr;
JSBigInt* runningSquare = base.toHeapBigInt(globalObject);
RETURN_IF_EXCEPTION(scope, nullptr);
// This implicitly sets the result's sign correctly.
if (n & 1) {
result = base.toHeapBigInt(globalObject);
RETURN_IF_EXCEPTION(scope, nullptr);
}
n >>= 1;
for (; n; n >>= 1) {
ImplResult temp = JSBigInt::multiplyImpl(globalObject, HeapBigIntImpl { runningSquare }, HeapBigIntImpl { runningSquare });
RETURN_IF_EXCEPTION(scope, nullptr);
ASSERT(temp.payload);
ASSERT(temp.payload.isHeapBigInt());
JSBigInt* maybeResult = temp.payload.asHeapBigInt();
runningSquare = maybeResult;
if (n & 1) {
if (!result)
result = runningSquare;
else {
temp = JSBigInt::multiplyImpl(globalObject, HeapBigIntImpl { result }, HeapBigIntImpl { runningSquare });
RETURN_IF_EXCEPTION(scope, nullptr);
ASSERT(temp.payload);
ASSERT(temp.payload.isHeapBigInt());
maybeResult = temp.payload.asHeapBigInt();
result = maybeResult;
}
}
}
return { result };
}
JSValue JSBigInt::exponentiate(JSGlobalObject* globalObject, JSBigInt* base, JSBigInt* exponent)
{
return tryConvertToBigInt32(exponentiateImpl(globalObject, HeapBigIntImpl { base }, HeapBigIntImpl { exponent }));
}
#if USE(BIGINT32)
JSValue JSBigInt::exponentiate(JSGlobalObject* globalObject, JSBigInt* base, int32_t exponent)
{
return tryConvertToBigInt32(exponentiateImpl(globalObject, HeapBigIntImpl { base }, Int32BigIntImpl { exponent }));
}
JSValue JSBigInt::exponentiate(JSGlobalObject* globalObject, int32_t base, JSBigInt* exponent)
{
return tryConvertToBigInt32(exponentiateImpl(globalObject, Int32BigIntImpl { base }, HeapBigIntImpl { exponent }));
}
JSValue JSBigInt::exponentiate(JSGlobalObject* globalObject, int32_t base, int32_t exponent)
{
return tryConvertToBigInt32(exponentiateImpl(globalObject, Int32BigIntImpl { base }, Int32BigIntImpl { exponent }));
}
#endif
template <typename BigIntImpl1, typename BigIntImpl2>
JSBigInt::ImplResult JSBigInt::multiplyImpl(JSGlobalObject* globalObject, BigIntImpl1 x, BigIntImpl2 y)
{
VM& vm = globalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
if (x.isZero())
return { x };
if (y.isZero())
return { y };
unsigned resultLength = x.length() + y.length();
JSBigInt* result = JSBigInt::createWithLength(globalObject, resultLength);
RETURN_IF_EXCEPTION(scope, nullptr);
result->initialize(InitializationType::WithZero);
for (unsigned i = 0; i < x.length(); i++)
multiplyAccumulate(y, x.digit(i), result, i);
result->setSign(x.sign() != y.sign());
RELEASE_AND_RETURN(scope, result->rightTrim(globalObject));
}
JSValue JSBigInt::multiply(JSGlobalObject* globalObject, JSBigInt* x, JSBigInt* y)
{
return tryConvertToBigInt32(multiplyImpl(globalObject, HeapBigIntImpl { x }, HeapBigIntImpl { y }));
}
#if USE(BIGINT32)
JSValue JSBigInt::multiply(JSGlobalObject* globalObject, int32_t x, JSBigInt* y)
{
return tryConvertToBigInt32(multiplyImpl(globalObject, Int32BigIntImpl { x }, HeapBigIntImpl { y }));
}
JSValue JSBigInt::multiply(JSGlobalObject* globalObject, JSBigInt* x, int32_t y)
{
return tryConvertToBigInt32(multiplyImpl(globalObject, HeapBigIntImpl { x }, Int32BigIntImpl { y }));
}
#endif
template <typename BigIntImpl1, typename BigIntImpl2>
JSBigInt::ImplResult JSBigInt::divideImpl(JSGlobalObject* globalObject, BigIntImpl1 x, BigIntImpl2 y)
{
// 1. If y is 0n, throw a RangeError exception.
VM& vm = globalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
if (y.isZero()) {
throwRangeError(globalObject, scope, "0 is an invalid divisor value."_s);
return nullptr;
}
// 2. Let quotient be the mathematical value of x divided by y.
// 3. Return a BigInt representing quotient rounded towards 0 to the next
// integral value.
if (absoluteCompare(x, y) == ComparisonResult::LessThan)
RELEASE_AND_RETURN(scope, zeroImpl(globalObject));
JSBigInt* quotient = nullptr;
bool resultSign = x.sign() != y.sign();
if (y.length() == 1) {
Digit divisor = y.digit(0);
if (divisor == 1) {
if (resultSign == x.sign())
return JSBigInt::ImplResult { x };
RELEASE_AND_RETURN(scope, JSBigInt::unaryMinusImpl(globalObject, x));
}
Digit remainder;
absoluteDivWithDigitDivisor(globalObject, vm, x, divisor, "ient, remainder);
RETURN_IF_EXCEPTION(scope, nullptr);
} else {
JSBigInt* yBigInt = y.toHeapBigInt(globalObject);
RETURN_IF_EXCEPTION(scope, nullptr);
absoluteDivWithBigIntDivisor(globalObject, x, yBigInt, "ient, nullptr);
RETURN_IF_EXCEPTION(scope, nullptr);
}
quotient->setSign(resultSign);
RELEASE_AND_RETURN(scope, quotient->rightTrim(globalObject));
}
JSValue JSBigInt::divide(JSGlobalObject* globalObject, JSBigInt* x, JSBigInt* y)
{
return tryConvertToBigInt32(divideImpl(globalObject, HeapBigIntImpl { x }, HeapBigIntImpl { y }));
}
#if USE(BIGINT32)
JSValue JSBigInt::divide(JSGlobalObject* globalObject, JSBigInt* x, int32_t y)
{
return tryConvertToBigInt32(divideImpl(globalObject, HeapBigIntImpl { x }, Int32BigIntImpl { y }));
}
JSValue JSBigInt::divide(JSGlobalObject* globalObject, int32_t x, JSBigInt* y)
{
return tryConvertToBigInt32(divideImpl(globalObject, Int32BigIntImpl { x }, HeapBigIntImpl { y }));
}
#endif
template <typename BigIntImpl>
JSBigInt* JSBigInt::copy(JSGlobalObject* globalObject, BigIntImpl x)
{
VM& vm = globalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
ASSERT(!x.isZero());
JSBigInt* result = createWithLength(globalObject, x.length());
RETURN_IF_EXCEPTION(scope, nullptr);
for (unsigned i = 0; i < result->length(); ++i)
result->setDigit(i, x.digit(i));
result->setSign(x.sign());
return result;
}
template <typename BigIntImpl>
JSBigInt::ImplResult JSBigInt::unaryMinusImpl(JSGlobalObject* globalObject, BigIntImpl x)
{
VM& vm = globalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
if (x.isZero())
RELEASE_AND_RETURN(scope, zeroImpl(globalObject));
JSBigInt* result = copy(globalObject, x);
RETURN_IF_EXCEPTION(scope, nullptr);
result->setSign(!x.sign());
return result;
}
JSValue JSBigInt::unaryMinus(JSGlobalObject* globalObject, JSBigInt* x)
{
return tryConvertToBigInt32(unaryMinusImpl(globalObject, HeapBigIntImpl { x }));
}
template <typename BigIntImpl1, typename BigIntImpl2>
JSBigInt::ImplResult JSBigInt::remainderImpl(JSGlobalObject* globalObject, BigIntImpl1 x, BigIntImpl2 y)
{
// 1. If y is 0n, throw a RangeError exception.
VM& vm = globalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
if (y.isZero()) {
throwRangeError(globalObject, scope, "0 is an invalid divisor value."_s);
return nullptr;
}
// 2. Return the JSBigInt representing x modulo y.
// See https://github.com/tc39/proposal-bigint/issues/84 though.
if (absoluteCompare(x, y) == ComparisonResult::LessThan)
return { x };
JSBigInt* remainder;
if (y.length() == 1) {
Digit divisor = y.digit(0);
if (divisor == 1)
RELEASE_AND_RETURN(scope, zeroImpl(globalObject));
Digit remainderDigit;
absoluteDivWithDigitDivisor(globalObject, vm, x, divisor, nullptr, remainderDigit);
RETURN_IF_EXCEPTION(scope, nullptr);
if (!remainderDigit)
RELEASE_AND_RETURN(scope, zeroImpl(globalObject));
remainder = createWithLength(globalObject, 1);
RETURN_IF_EXCEPTION(scope, nullptr);
remainder->setDigit(0, remainderDigit);
} else {
JSBigInt* yBigInt = y.toHeapBigInt(globalObject);
RETURN_IF_EXCEPTION(scope, nullptr);
absoluteDivWithBigIntDivisor(globalObject, x, yBigInt, nullptr, &remainder);
RETURN_IF_EXCEPTION(scope, nullptr);
}
remainder->setSign(x.sign());
RELEASE_AND_RETURN(scope, remainder->rightTrim(globalObject));
}
JSValue JSBigInt::remainder(JSGlobalObject* globalObject, JSBigInt* x, JSBigInt* y)
{
return tryConvertToBigInt32(remainderImpl(globalObject, HeapBigIntImpl { x }, HeapBigIntImpl { y }));
}
#if USE(BIGINT32)
JSValue JSBigInt::remainder(JSGlobalObject* globalObject, JSBigInt* x, int32_t y)
{
return tryConvertToBigInt32(remainderImpl(globalObject, HeapBigIntImpl { x }, Int32BigIntImpl { y }));
}
JSValue JSBigInt::remainder(JSGlobalObject* globalObject, int32_t x, JSBigInt* y)
{
return tryConvertToBigInt32(remainderImpl(globalObject, Int32BigIntImpl { x }, HeapBigIntImpl { y }));
}
#endif
template <typename BigIntImpl>
JSBigInt::ImplResult JSBigInt::incImpl(JSGlobalObject* globalObject, BigIntImpl x)
{
VM& vm = globalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
if (!x.sign())
RELEASE_AND_RETURN(scope, absoluteAddOne(globalObject, x, SignOption::Unsigned));
JSBigInt* result = absoluteSubOne(globalObject, x, x.length());
RETURN_IF_EXCEPTION(scope, nullptr);
if (result->isZero())
return result;
result->setSign(true);
return result;
}
JSValue JSBigInt::inc(JSGlobalObject* globalObject, JSBigInt* x)
{
return tryConvertToBigInt32(incImpl(globalObject, HeapBigIntImpl { x }));
}
template <typename BigIntImpl>
JSBigInt::ImplResult JSBigInt::decImpl(JSGlobalObject* globalObject, BigIntImpl x)
{
if (x.isZero()) {
#if USE(BIGINT32)
return jsBigInt32(-1);
#else
return createFrom(globalObject, -1);
#endif
}
if (!x.sign())
return absoluteSubOne(globalObject, x, x.length());
return absoluteAddOne(globalObject, x, SignOption::Signed);
}
JSValue JSBigInt::dec(JSGlobalObject* globalObject, JSBigInt* x)
{
return tryConvertToBigInt32(decImpl(globalObject, HeapBigIntImpl { x }));
}
template <typename BigIntImpl1, typename BigIntImpl2>
JSBigInt::ImplResult JSBigInt::addImpl(JSGlobalObject* globalObject, BigIntImpl1 x, BigIntImpl2 y)
{
bool xSign = x.sign();
// x + y == x + y
// -x + -y == -(x + y)
if (xSign == y.sign())
return absoluteAdd(globalObject, x, y, xSign);
// x + -y == x - y == -(y - x)
// -x + y == y - x == -(x - y)
ComparisonResult comparisonResult = absoluteCompare(x, y);
if (comparisonResult == ComparisonResult::GreaterThan || comparisonResult == ComparisonResult::Equal)
return absoluteSub(globalObject, x, y, xSign);
return absoluteSub(globalObject, y, x, !xSign);
}
JSValue JSBigInt::add(JSGlobalObject* globalObject, JSBigInt* x, JSBigInt* y)
{
return tryConvertToBigInt32(addImpl(globalObject, HeapBigIntImpl { x }, HeapBigIntImpl { y }));
}
#if USE(BIGINT32)
JSValue JSBigInt::add(JSGlobalObject* globalObject, JSBigInt* x, int32_t y)
{
return tryConvertToBigInt32(addImpl(globalObject, HeapBigIntImpl { x }, Int32BigIntImpl { y }));
}
JSValue JSBigInt::add(JSGlobalObject* globalObject, int32_t x, JSBigInt* y)
{
return tryConvertToBigInt32(addImpl(globalObject, Int32BigIntImpl { x }, HeapBigIntImpl { y }));
}
#endif
template <typename BigIntImpl1, typename BigIntImpl2>
JSBigInt::ImplResult JSBigInt::subImpl(JSGlobalObject* globalObject, BigIntImpl1 x, BigIntImpl2 y)
{
bool xSign = x.sign();
if (xSign != y.sign()) {
// x - (-y) == x + y
// (-x) - y == -(x + y)
return absoluteAdd(globalObject, x, y, xSign);
}
// x - y == -(y - x)
// (-x) - (-y) == y - x == -(x - y)
ComparisonResult comparisonResult = absoluteCompare(x, y);
if (comparisonResult == ComparisonResult::GreaterThan || comparisonResult == ComparisonResult::Equal)
return absoluteSub(globalObject, x, y, xSign);
return absoluteSub(globalObject, y, x, !xSign);
}
JSValue JSBigInt::sub(JSGlobalObject* globalObject, JSBigInt* x, JSBigInt* y)
{
return tryConvertToBigInt32(subImpl(globalObject, HeapBigIntImpl { x }, HeapBigIntImpl { y }));
}
#if USE(BIGINT32)
JSValue JSBigInt::sub(JSGlobalObject* globalObject, JSBigInt* x, int32_t y)
{
return tryConvertToBigInt32(subImpl(globalObject, HeapBigIntImpl { x }, Int32BigIntImpl { y }));
}
JSValue JSBigInt::sub(JSGlobalObject* globalObject, int32_t x, JSBigInt* y)
{
return tryConvertToBigInt32(subImpl(globalObject, Int32BigIntImpl { x }, HeapBigIntImpl { y }));
}
#endif
template <typename BigIntImpl1, typename BigIntImpl2>
JSBigInt::ImplResult JSBigInt::bitwiseAndImpl(JSGlobalObject* globalObject, BigIntImpl1 x, BigIntImpl2 y)
{
VM& vm = globalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
if (!x.sign() && !y.sign())
RELEASE_AND_RETURN(scope, absoluteAnd(globalObject, x, y));
if (x.sign() && y.sign()) {
int resultLength = std::max(x.length(), y.length()) + 1;
// (-x) & (-y) == ~(x-1) & ~(y-1) == ~((x-1) | (y-1))
// == -(((x-1) | (y-1)) + 1)
JSBigInt* result = absoluteSubOne(globalObject, x, resultLength);
RETURN_IF_EXCEPTION(scope, nullptr);
JSBigInt* y1 = absoluteSubOne(globalObject, y, y.length());
RETURN_IF_EXCEPTION(scope, nullptr);
result = absoluteOr(globalObject, HeapBigIntImpl { result }, HeapBigIntImpl { y1 });
RETURN_IF_EXCEPTION(scope, nullptr);
RELEASE_AND_RETURN(scope, absoluteAddOne(globalObject, HeapBigIntImpl { result }, SignOption::Signed));
}
ASSERT(x.sign() != y.sign());
// x & (-y) == x & ~(y-1)
auto computeResult = [&] (auto x, auto y) -> JSBigInt* {
ASSERT(!x.sign());
ASSERT(y.sign());
JSBigInt* y1 = absoluteSubOne(globalObject, y, y.length());
RETURN_IF_EXCEPTION(scope, nullptr);
RELEASE_AND_RETURN(scope, absoluteAndNot(globalObject, x, HeapBigIntImpl { y1 }));
};
if (x.sign())
return computeResult(y, x);
return computeResult(x, y);
}
JSValue JSBigInt::bitwiseAnd(JSGlobalObject* globalObject, JSBigInt* x, JSBigInt* y)
{
return tryConvertToBigInt32(bitwiseAndImpl(globalObject, HeapBigIntImpl { x }, HeapBigIntImpl { y }));
}
#if USE(BIGINT32)
JSValue JSBigInt::bitwiseAnd(JSGlobalObject* globalObject, JSBigInt* x, int32_t y)
{
return tryConvertToBigInt32(bitwiseAndImpl(globalObject, HeapBigIntImpl { x }, Int32BigIntImpl { y }));
}
JSValue JSBigInt::bitwiseAnd(JSGlobalObject* globalObject, int32_t x, JSBigInt* y)
{
return tryConvertToBigInt32(bitwiseAndImpl(globalObject, Int32BigIntImpl { x }, HeapBigIntImpl { y }));
}
#endif
template <typename BigIntImpl1, typename BigIntImpl2>
JSBigInt::ImplResult JSBigInt::bitwiseOrImpl(JSGlobalObject* globalObject, BigIntImpl1 x, BigIntImpl2 y)
{
VM& vm = globalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
unsigned resultLength = std::max(x.length(), y.length());
if (!x.sign() && !y.sign())
RELEASE_AND_RETURN(scope, absoluteOr(globalObject, x, y));
if (x.sign() && y.sign()) {
// (-x) | (-y) == ~(x-1) | ~(y-1) == ~((x-1) & (y-1))
// == -(((x-1) & (y-1)) + 1)
JSBigInt* result = absoluteSubOne(globalObject, x, resultLength);
RETURN_IF_EXCEPTION(scope, nullptr);
JSBigInt* y1 = absoluteSubOne(globalObject, y, y.length());
RETURN_IF_EXCEPTION(scope, nullptr);
result = absoluteAnd(globalObject, HeapBigIntImpl { result }, HeapBigIntImpl { y1 });
RETURN_IF_EXCEPTION(scope, nullptr);
RELEASE_AND_RETURN(scope, absoluteAddOne(globalObject, HeapBigIntImpl { result }, SignOption::Signed));
}
ASSERT(x.sign() != y.sign());
// x | (-y) == x | ~(y-1) == ~((y-1) &~ x) == -(((y-1) &~ x) + 1)
auto computeResult = [&] (auto x, auto y) -> JSBigInt* {
ASSERT(!x.sign());
ASSERT(y.sign());
JSBigInt* result = absoluteSubOne(globalObject, y, resultLength);
RETURN_IF_EXCEPTION(scope, nullptr);
result = absoluteAndNot(globalObject, HeapBigIntImpl { result }, x);
RETURN_IF_EXCEPTION(scope, nullptr);
RELEASE_AND_RETURN(scope, absoluteAddOne(globalObject, HeapBigIntImpl { result }, SignOption::Signed));
};
if (x.sign())
return computeResult(y, x);
return computeResult(x, y);
}
JSValue JSBigInt::bitwiseOr(JSGlobalObject* globalObject, JSBigInt* x, JSBigInt* y)
{
return tryConvertToBigInt32(bitwiseOrImpl(globalObject, HeapBigIntImpl { x }, HeapBigIntImpl { y }));
}
#if USE(BIGINT32)
JSValue JSBigInt::bitwiseOr(JSGlobalObject* globalObject, JSBigInt* x, int32_t y)
{
return tryConvertToBigInt32(bitwiseOrImpl(globalObject, HeapBigIntImpl { x }, Int32BigIntImpl { y }));
}
JSValue JSBigInt::bitwiseOr(JSGlobalObject* globalObject, int32_t x, JSBigInt* y)
{
return tryConvertToBigInt32(bitwiseOrImpl(globalObject, Int32BigIntImpl { x }, HeapBigIntImpl { y }));
}
#endif
template <typename BigIntImpl1, typename BigIntImpl2>
JSBigInt::ImplResult JSBigInt::bitwiseXorImpl(JSGlobalObject* globalObject, BigIntImpl1 x, BigIntImpl2 y)
{
VM& vm = globalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
if (!x.sign() && !y.sign())
RELEASE_AND_RETURN(scope, absoluteXor(globalObject, x, y));
if (x.sign() && y.sign()) {
int resultLength = std::max(x.length(), y.length());
// (-x) ^ (-y) == ~(x-1) ^ ~(y-1) == (x-1) ^ (y-1)
JSBigInt* result = absoluteSubOne(globalObject, x, resultLength);
RETURN_IF_EXCEPTION(scope, nullptr);
JSBigInt* y1 = absoluteSubOne(globalObject, y, y.length());
RETURN_IF_EXCEPTION(scope, nullptr);
RELEASE_AND_RETURN(scope, absoluteXor(globalObject, HeapBigIntImpl { result }, HeapBigIntImpl { y1 }));
}
ASSERT(x.sign() != y.sign());
int resultLength = std::max(x.length(), y.length()) + 1;
// x ^ (-y) == x ^ ~(y-1) == ~(x ^ (y-1)) == -((x ^ (y-1)) + 1)
auto computeResult = [&] (auto x, auto y) -> JSBigInt* {
ASSERT(!x.sign());
ASSERT(y.sign());
JSBigInt* result = absoluteSubOne(globalObject, y, resultLength);
RETURN_IF_EXCEPTION(scope, nullptr);
result = absoluteXor(globalObject, HeapBigIntImpl { result }, x);
RETURN_IF_EXCEPTION(scope, nullptr);
RELEASE_AND_RETURN(scope, absoluteAddOne(globalObject, HeapBigIntImpl { result }, SignOption::Signed));
};
// Assume that x is the positive BigInt.
if (x.sign())
return computeResult(y, x);
return computeResult(x, y);
}
JSValue JSBigInt::bitwiseXor(JSGlobalObject* globalObject, JSBigInt* x, JSBigInt* y)
{
return tryConvertToBigInt32(bitwiseXorImpl(globalObject, HeapBigIntImpl { x }, HeapBigIntImpl { y }));
}
#if USE(BIGINT32)
JSValue JSBigInt::bitwiseXor(JSGlobalObject* globalObject, JSBigInt* x, int32_t y)
{
return tryConvertToBigInt32(bitwiseXorImpl(globalObject, HeapBigIntImpl { x }, Int32BigIntImpl { y }));
}
JSValue JSBigInt::bitwiseXor(JSGlobalObject* globalObject, int32_t x, JSBigInt* y)
{
return tryConvertToBigInt32(bitwiseXorImpl(globalObject, Int32BigIntImpl { x }, HeapBigIntImpl { y }));
}
#endif
template <typename BigIntImpl1, typename BigIntImpl2>
JSBigInt::ImplResult JSBigInt::leftShiftImpl(JSGlobalObject* globalObject, BigIntImpl1 x, BigIntImpl2 y)
{
if (x.isZero() || y.isZero())
return { x };
if (y.sign())
return rightShiftByAbsolute(globalObject, x, y);
return leftShiftByAbsolute(globalObject, x, y);
}
JSValue JSBigInt::leftShift(JSGlobalObject* globalObject, JSBigInt* x, JSBigInt* y)
{
return tryConvertToBigInt32(leftShiftImpl(globalObject, HeapBigIntImpl { x }, HeapBigIntImpl { y }));
}
#if USE(BIGINT32)
JSValue JSBigInt::leftShift(JSGlobalObject* globalObject, JSBigInt* x, int32_t y)
{
return tryConvertToBigInt32(leftShiftImpl(globalObject, HeapBigIntImpl { x }, Int32BigIntImpl { y }));
}
JSValue JSBigInt::leftShift(JSGlobalObject* globalObject, int32_t x, JSBigInt* y)
{
return tryConvertToBigInt32(leftShiftImpl(globalObject, Int32BigIntImpl { x }, HeapBigIntImpl { y }));
}
JSValue JSBigInt::leftShiftSlow(JSGlobalObject* globalObject, int32_t x, int32_t y)
{
return tryConvertToBigInt32(leftShiftImpl(globalObject, Int32BigIntImpl { x }, Int32BigIntImpl { y }));
}
#endif
template <typename BigIntImpl1, typename BigIntImpl2>
JSBigInt::ImplResult JSBigInt::signedRightShiftImpl(JSGlobalObject* globalObject, BigIntImpl1 x, BigIntImpl2 y)
{
if (x.isZero() || y.isZero())
return { x };
if (y.sign())
return leftShiftByAbsolute(globalObject, x, y);
return rightShiftByAbsolute(globalObject, x, y);
}
JSValue JSBigInt::signedRightShift(JSGlobalObject* globalObject, JSBigInt* x, JSBigInt* y)
{
return tryConvertToBigInt32(signedRightShiftImpl(globalObject, HeapBigIntImpl { x }, HeapBigIntImpl { y }));
}
#if USE(BIGINT32)
JSValue JSBigInt::signedRightShift(JSGlobalObject* globalObject, JSBigInt* x, int32_t y)
{
return tryConvertToBigInt32(signedRightShiftImpl(globalObject, HeapBigIntImpl { x }, Int32BigIntImpl { y }));
}
JSValue JSBigInt::signedRightShift(JSGlobalObject* globalObject, int32_t x, JSBigInt* y)
{
return tryConvertToBigInt32(signedRightShiftImpl(globalObject, Int32BigIntImpl { x }, HeapBigIntImpl { y }));
}
#endif
template <typename BigIntImpl>
JSBigInt::ImplResult JSBigInt::bitwiseNotImpl(JSGlobalObject* globalObject, BigIntImpl x)
{
if (x.sign()) {
// ~(-x) == ~(~(x-1)) == x-1
return absoluteSubOne(globalObject, x, x.length());
}
// ~x == -x-1 == -(x+1)
return absoluteAddOne(globalObject, x, SignOption::Signed);
}
JSValue JSBigInt::bitwiseNot(JSGlobalObject* globalObject, JSBigInt* x)
{
return tryConvertToBigInt32(bitwiseNotImpl(globalObject, HeapBigIntImpl { x }));
}
#if USE(JSVALUE32_64)
using TwoDigit = uint64_t;
#else
using TwoDigit = UInt128;
#endif
// {carry} must point to an initialized Digit and will either be incremented
// by one or left alone.
inline JSBigInt::Digit JSBigInt::digitAdd(Digit a, Digit b, Digit& carry)
{
Digit result = a + b;
carry += static_cast<bool>(result < a);
return result;
}
// {borrow} must point to an initialized Digit and will either be incremented
// by one or left alone.
inline JSBigInt::Digit JSBigInt::digitSub(Digit a, Digit b, Digit& borrow)
{
Digit result = a - b;
borrow += static_cast<bool>(result > a);
return result;
}
// Returns the low half of the result. High half is in {high}.
inline JSBigInt::Digit JSBigInt::digitMul(Digit a, Digit b, Digit& high)
{
TwoDigit result = static_cast<TwoDigit>(a) * static_cast<TwoDigit>(b);
high = static_cast<Digit>(result >> digitBits);
return static_cast<Digit>(result);
}
// Raises {base} to the power of {exponent}. Does not check for overflow.
inline JSBigInt::Digit JSBigInt::digitPow(Digit base, Digit exponent)
{
Digit result = 1ull;
while (exponent > 0) {
if (exponent & 1)
result *= base;
exponent >>= 1;
base *= base;
}
return result;
}
// Returns the quotient.
// quotient = (high << digitBits + low - remainder) / divisor
inline JSBigInt::Digit JSBigInt::digitDiv(Digit high, Digit low, Digit divisor, Digit& remainder)
{
ASSERT(high < divisor);
#if CPU(X86_64)
Digit quotient;
Digit rem;
__asm__("divq %[divisor]"
// Outputs: {quotient} will be in rax, {rem} in rdx.
: "=a"(quotient), "=d"(rem)
// Inputs: put {high} into rdx, {low} into rax, and {divisor} into
// any register or stack slot.
: "d"(high), "a"(low), [divisor] "rm"(divisor));
remainder = rem;
return quotient;
#elif CPU(X86)
Digit quotient;
Digit rem;
__asm__("divl %[divisor]"
// Outputs: {quotient} will be in eax, {rem} in edx.
: "=a"(quotient), "=d"(rem)
// Inputs: put {high} into edx, {low} into eax, and {divisor} into
// any register or stack slot.
: "d"(high), "a"(low), [divisor] "rm"(divisor));
remainder = rem;
return quotient;
#else
static constexpr Digit halfDigitBase = 1ull << halfDigitBits;
// Adapted from Warren, Hacker's Delight, p. 152.
unsigned s = clz(divisor);
// If {s} is digitBits here, it causes an undefined behavior.
// But {s} is never digitBits since {divisor} is never zero here.
ASSERT(s != digitBits);
divisor <<= s;
Digit vn1 = divisor >> halfDigitBits;
Digit vn0 = divisor & halfDigitMask;
// {sZeroMask} which is 0 if s == 0 and all 1-bits otherwise.
// {s} can be 0. If {s} is 0, performing "low >> (digitBits - s)" must not be done since it causes an undefined behavior
// since `>> digitBits` is undefied in C++. Quoted from C++ spec, "The type of the result is that of the promoted left operand.
// The behavior is undefined if the right operand is negative, or greater than or equal to the length in bits of the promoted
// left operand". We mask the right operand of the shift by {shiftMask} (`digitBits - 1`), which makes `digitBits - 0` zero.
// This shifting produces a value which covers 0 < {s} <= (digitBits - 1) cases. {s} == digitBits never happen as we asserted.
// Since {sZeroMask} clears the value in the case of {s} == 0, {s} == 0 case is also covered.
static_assert(sizeof(CPURegister) == sizeof(Digit));
Digit sZeroMask = static_cast<Digit>((-static_cast<CPURegister>(s)) >> (digitBits - 1));
static constexpr unsigned shiftMask = digitBits - 1;
Digit un32 = (high << s) | ((low >> ((digitBits - s) & shiftMask)) & sZeroMask);
Digit un10 = low << s;
Digit un1 = un10 >> halfDigitBits;
Digit un0 = un10 & halfDigitMask;
Digit q1 = un32 / vn1;
Digit rhat = un32 - q1 * vn1;
while (q1 >= halfDigitBase || q1 * vn0 > rhat * halfDigitBase + un1) {
q1--;
rhat += vn1;
if (rhat >= halfDigitBase)
break;
}
Digit un21 = un32 * halfDigitBase + un1 - q1 * divisor;
Digit q0 = un21 / vn1;
rhat = un21 - q0 * vn1;
while (q0 >= halfDigitBase || q0 * vn0 > rhat * halfDigitBase + un0) {
q0--;
rhat += vn1;
if (rhat >= halfDigitBase)
break;
}
remainder = (un21 * halfDigitBase + un0 - q0 * divisor) >> s;
return q1 * halfDigitBase + q0;
#endif
}
// Multiplies {source} with {factor} and adds {summand} to the result.
// {result} and {source} may be the same BigInt for inplace modification.
template <typename BigIntImpl>
void JSBigInt::internalMultiplyAdd(BigIntImpl source, Digit factor, Digit summand, unsigned n, JSBigInt* result)
{
ASSERT(source.length() >= n);
ASSERT(result->length() >= n);
Digit carry = summand;
Digit high = 0;
for (unsigned i = 0; i < n; i++) {
Digit current = source.digit(i);
Digit newCarry = 0;
// Compute this round's multiplication.
Digit newHigh = 0;
current = digitMul(current, factor, newHigh);
// Add last round's carryovers.
current = digitAdd(current, high, newCarry);
current = digitAdd(current, carry, newCarry);
// Store result and prepare for next round.
result->setDigit(i, current);
carry = newCarry;
high = newHigh;
}
if (result->length() > n) {
result->setDigit(n++, carry + high);
// Current callers don't pass in such large results, but let's be robust.
while (n < result->length())
result->setDigit(n++, 0);
} else
ASSERT(!(carry + high));
}
// Multiplies {multiplicand} with {multiplier} and adds the result to
// {accumulator}, starting at {accumulatorIndex} for the least-significant
// digit.
// Callers must ensure that {accumulator} is big enough to hold the result.
template <typename BigIntImpl>
void JSBigInt::multiplyAccumulate(BigIntImpl multiplicand, Digit multiplier, JSBigInt* accumulator, unsigned accumulatorIndex)
{
ASSERT(accumulator->length() > multiplicand.length() + accumulatorIndex);
if (!multiplier)
return;
Digit carry = 0;
Digit high = 0;
for (unsigned i = 0; i < multiplicand.length(); i++, accumulatorIndex++) {
Digit acc = accumulator->digit(accumulatorIndex);
Digit newCarry = 0;
// Add last round's carryovers.
acc = digitAdd(acc, high, newCarry);
acc = digitAdd(acc, carry, newCarry);
// Compute this round's multiplication.
Digit multiplicandDigit = multiplicand.digit(i);
Digit low = digitMul(multiplier, multiplicandDigit, high);
acc = digitAdd(acc, low, newCarry);
// Store result and prepare for next round.
accumulator->setDigit(accumulatorIndex, acc);
carry = newCarry;
}
while (carry || high) {
ASSERT(accumulatorIndex < accumulator->length());
Digit acc = accumulator->digit(accumulatorIndex);
Digit newCarry = 0;
acc = digitAdd(acc, high, newCarry);
high = 0;
acc = digitAdd(acc, carry, newCarry);
accumulator->setDigit(accumulatorIndex, acc);
carry = newCarry;
accumulatorIndex++;
}
}
bool JSBigInt::equals(JSBigInt* x, JSBigInt* y)
{
if (x->sign() != y->sign())
return false;
if (x->length() != y->length())
return false;
for (unsigned i = 0; i < x->length(); i++) {
if (x->digit(i) != y->digit(i))
return false;
}
return true;
}
template <typename BigIntImpl1, typename BigIntImpl2>
inline JSBigInt::ComparisonResult JSBigInt::absoluteCompare(BigIntImpl1 x, BigIntImpl2 y)
{
ASSERT(!x.length() || x.digit(x.length() - 1));
ASSERT(!y.length() || y.digit(y.length() - 1));
int diff = x.length() - y.length();
if (diff)
return diff < 0 ? ComparisonResult::LessThan : ComparisonResult::GreaterThan;
int i = x.length() - 1;
while (i >= 0 && x.digit(i) == y.digit(i))
i--;
if (i < 0)
return ComparisonResult::Equal;
return x.digit(i) > y.digit(i) ? ComparisonResult::GreaterThan : ComparisonResult::LessThan;
}
template <typename BigIntImpl1, typename BigIntImpl2>
JSBigInt::ComparisonResult JSBigInt::compareImpl(BigIntImpl1 x, BigIntImpl2 y)
{
bool xSign = x.sign();
if (xSign != y.sign())
return xSign ? ComparisonResult::LessThan : ComparisonResult::GreaterThan;
ComparisonResult result = absoluteCompare(x, y);
if (result == ComparisonResult::GreaterThan)
return xSign ? ComparisonResult::LessThan : ComparisonResult::GreaterThan;
if (result == ComparisonResult::LessThan)
return xSign ? ComparisonResult::GreaterThan : ComparisonResult::LessThan;
return ComparisonResult::Equal;
}
JSBigInt::ComparisonResult JSBigInt::compare(JSBigInt* x, JSBigInt* y)
{
return compareImpl(HeapBigIntImpl { x }, HeapBigIntImpl { y });
}
JSBigInt::ComparisonResult JSBigInt::compare(int32_t x, JSBigInt* y)
{
return compareImpl(Int32BigIntImpl { x }, HeapBigIntImpl { y });
}
JSBigInt::ComparisonResult JSBigInt::compare(JSBigInt* x, int32_t y)
{
return compareImpl(HeapBigIntImpl { x }, Int32BigIntImpl { y });
}
JSBigInt::ComparisonResult JSBigInt::compare(JSBigInt* x, int64_t y)
{
return compareImpl(HeapBigIntImpl { x }, Int64BigIntImpl { y });
}
JSBigInt::ComparisonResult JSBigInt::compare(JSValue x, int64_t y)
{
ASSERT(x.isBigInt());
#if USE(BIGINT32)
if (x.isBigInt32())
return compareImpl(Int32BigIntImpl { x.bigInt32AsInt32() }, Int64BigIntImpl { y });
#endif
return compare(x.asHeapBigInt(), y);
}
JSBigInt::ComparisonResult JSBigInt::compare(JSBigInt* x, uint64_t y)
{
return compareImpl(HeapBigIntImpl { x }, Int64BigIntImpl { y });
}
JSBigInt::ComparisonResult JSBigInt::compare(JSValue x, uint64_t y)
{
ASSERT(x.isBigInt());
#if USE(BIGINT32)
if (x.isBigInt32())
return compareImpl(Int32BigIntImpl { x.bigInt32AsInt32() }, Int64BigIntImpl { y });
#endif
return compare(x.asHeapBigInt(), y);
}
JSBigInt::ComparisonResult JSBigInt::compare(JSValue x, JSValue y)
{
ASSERT(x.isBigInt() && y.isBigInt());
#if USE(BIGINT32)
if (x.isBigInt32() && y.isBigInt32()) {
int32_t x1 = x.asBigInt32();
int32_t y1 = y.asBigInt32();
if (x1 == y1)
return JSBigInt::ComparisonResult::Equal;
if (x1 < y1)
return JSBigInt::ComparisonResult::LessThan;
return JSBigInt::ComparisonResult::GreaterThan;
}
if (x.isBigInt32())
return compare(x.bigInt32AsInt32(), y.asHeapBigInt());
if (y.isBigInt32())
return compare(x.asHeapBigInt(), y.bigInt32AsInt32());
#endif
return compare(x.asHeapBigInt(), y.asHeapBigInt());
}
template <typename BigIntImpl1, typename BigIntImpl2>
JSBigInt::ImplResult JSBigInt::absoluteAdd(JSGlobalObject* globalObject, BigIntImpl1 x, BigIntImpl2 y, bool resultSign)
{
VM& vm = globalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
if (x.length() < y.length())
RELEASE_AND_RETURN(scope, absoluteAdd(globalObject, y, x, resultSign));
if (x.isZero()) {
ASSERT(y.isZero());
return { x };
}
if (y.isZero()) {
if (resultSign == x.sign())
return { x };
RELEASE_AND_RETURN(scope, unaryMinusImpl(globalObject, x));
}
JSBigInt* result = createWithLength(globalObject, x.length() + 1);
RETURN_IF_EXCEPTION(scope, nullptr);
ASSERT(result);
Digit carry = 0;
unsigned i = 0;
for (; i < y.length(); i++) {
Digit newCarry = 0;
Digit sum = digitAdd(x.digit(i), y.digit(i), newCarry);
sum = digitAdd(sum, carry, newCarry);
result->setDigit(i, sum);
carry = newCarry;
}
for (; i < x.length(); i++) {
Digit newCarry = 0;
Digit sum = digitAdd(x.digit(i), carry, newCarry);
result->setDigit(i, sum);
carry = newCarry;
}
result->setDigit(i, carry);
result->setSign(resultSign);
RELEASE_AND_RETURN(scope, result->rightTrim(globalObject));
}
template <typename BigIntImpl1, typename BigIntImpl2>
JSBigInt::ImplResult JSBigInt::absoluteSub(JSGlobalObject* globalObject, BigIntImpl1 x, BigIntImpl2 y, bool resultSign)
{
VM& vm = globalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
ComparisonResult comparisonResult = absoluteCompare(x, y);
ASSERT(x.length() >= y.length());
ASSERT(comparisonResult == ComparisonResult::GreaterThan || comparisonResult == ComparisonResult::Equal);
if (x.isZero()) {
ASSERT(y.isZero());
return { x };
}
if (y.isZero()) {
if (resultSign == x.sign())
return ImplResult { x };
RELEASE_AND_RETURN(scope, JSBigInt::unaryMinusImpl(globalObject, x));
}
if (comparisonResult == ComparisonResult::Equal)
RELEASE_AND_RETURN(scope, zeroImpl(globalObject));
JSBigInt* result = createWithLength(globalObject, x.length());
RETURN_IF_EXCEPTION(scope, nullptr);
Digit borrow = 0;
unsigned i = 0;
for (; i < y.length(); i++) {
Digit newBorrow = 0;
Digit difference = digitSub(x.digit(i), y.digit(i), newBorrow);
difference = digitSub(difference, borrow, newBorrow);
result->setDigit(i, difference);
borrow = newBorrow;
}
for (; i < x.length(); i++) {
Digit newBorrow = 0;
Digit difference = digitSub(x.digit(i), borrow, newBorrow);
result->setDigit(i, difference);
borrow = newBorrow;
}
ASSERT(!borrow);
result->setSign(resultSign);
RELEASE_AND_RETURN(scope, result->rightTrim(globalObject));
}
// Divides {x} by {divisor}, returning the result in {quotient} and {remainder}.
// Mathematically, the contract is:
// quotient = (x - remainder) / divisor, with 0 <= remainder < divisor.
// If {quotient} is an empty handle, an appropriately sized BigInt will be
// allocated for it; otherwise the caller must ensure that it is big enough.
// {quotient} can be the same as {x} for an in-place division. {quotient} can
// also be nullptr if the caller is only interested in the remainder.
template <typename BigIntImpl>
bool JSBigInt::absoluteDivWithDigitDivisor(JSGlobalObject* nullOrGlobalObjectForOOM, VM& vm, BigIntImpl x, Digit divisor, JSBigInt** quotient, Digit& remainder)
{
ASSERT(divisor);
ASSERT(!x.isZero());
remainder = 0;
if (divisor == 1) {
if (quotient) {
JSBigInt* result = x.toHeapBigInt(nullOrGlobalObjectForOOM, vm);
if (UNLIKELY(!result))
return false;
*quotient = result;
}
return true;
}
unsigned length = x.length();
if (quotient) {
if (*quotient == nullptr) {
JSBigInt* result = createWithLength(nullOrGlobalObjectForOOM, vm, length);
if (UNLIKELY(!result))
return false;
*quotient = result;
}
for (int i = length - 1; i >= 0; i--) {
Digit q = digitDiv(remainder, x.digit(i), divisor, remainder);
(*quotient)->setDigit(i, q);
}
} else {
for (int i = length - 1; i >= 0; i--)
digitDiv(remainder, x.digit(i), divisor, remainder);
}
return true;
}
// Divides {dividend} by {divisor}, returning the result in {quotient} and
// {remainder}. Mathematically, the contract is:
// quotient = (dividend - remainder) / divisor, with 0 <= remainder < divisor.
// Both {quotient} and {remainder} are optional, for callers that are only
// interested in one of them.
// See Knuth, Volume 2, section 4.3.1, Algorithm D.
template <typename BigIntImpl1>
void JSBigInt::absoluteDivWithBigIntDivisor(JSGlobalObject* globalObject, BigIntImpl1 dividend, JSBigInt* divisor, JSBigInt** quotient, JSBigInt** remainder)
{
ASSERT(divisor->length() >= 2);
ASSERT(dividend.length() >= divisor->length());
VM& vm = globalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
// The unusual variable names inside this function are consistent with
// Knuth's book, as well as with Go's implementation of this algorithm.
// Maintaining this consistency is probably more useful than trying to
// come up with more descriptive names for them.
unsigned n = divisor->length();
unsigned m = dividend.length() - n;
// The quotient to be computed.
JSBigInt* q = nullptr;
if (quotient != nullptr) {
q = createWithLength(globalObject, m + 1);
RETURN_IF_EXCEPTION(scope, void());
}
// In each iteration, {qhatv} holds {divisor} * {current quotient digit}.
// "v" is the book's name for {divisor}, "qhat" the current quotient digit.
JSBigInt* qhatv = createWithLength(globalObject, n + 1);
RETURN_IF_EXCEPTION(scope, void());
// D1.
// Left-shift inputs so that the divisor's MSB is set. This is necessary
// to prevent the digit-wise divisions (see digit_div call below) from
// overflowing (they take a two digits wide input, and return a one digit
// result).
Digit lastDigit = divisor->digit(n - 1);
unsigned shift = clz(lastDigit);
if (shift > 0) {
divisor = absoluteLeftShiftAlwaysCopy(globalObject, HeapBigIntImpl { divisor }, shift, LeftShiftMode::SameSizeResult);
RETURN_IF_EXCEPTION(scope, void());
}
// Holds the (continuously updated) remaining part of the dividend, which
// eventually becomes the remainder.
JSBigInt* u = absoluteLeftShiftAlwaysCopy(globalObject, dividend, shift, LeftShiftMode::AlwaysAddOneDigit);
RETURN_IF_EXCEPTION(scope, void());
// D2.
// Iterate over the dividend's digit (like the "grad school" algorithm).
// {vn1} is the divisor's most significant digit.
Digit vn1 = divisor->digit(n - 1);
for (int j = m; j >= 0; j--) {
// D3.
// Estimate the current iteration's quotient digit (see Knuth for details).
// {qhat} is the current quotient digit.
Digit qhat = std::numeric_limits<Digit>::max();
// {ujn} is the dividend's most significant remaining digit.
Digit ujn = u->digit(j + n);
if (ujn != vn1) {
// {rhat} is the current iteration's remainder.
Digit rhat = 0;
// Estimate the current quotient digit by dividing the most significant
// digits of dividend and divisor. The result will not be too small,
// but could be a bit too large.
qhat = digitDiv(ujn, u->digit(j + n - 1), vn1, rhat);
// Decrement the quotient estimate as needed by looking at the next
// digit, i.e. by testing whether
// qhat * v_{n-2} > (rhat << digitBits) + u_{j+n-2}.
Digit vn2 = divisor->digit(n - 2);
Digit ujn2 = u->digit(j + n - 2);
while (productGreaterThan(qhat, vn2, rhat, ujn2)) {
qhat--;
Digit prevRhat = rhat;
rhat += vn1;
// v[n-1] >= 0, so this tests for overflow.
if (rhat < prevRhat)
break;
}
}
// D4.
// Multiply the divisor with the current quotient digit, and subtract
// it from the dividend. If there was "borrow", then the quotient digit
// was one too high, so we must correct it and undo one subtraction of
// the (shifted) divisor.
internalMultiplyAdd(HeapBigIntImpl { divisor }, qhat, 0, n, qhatv);
Digit c = u->absoluteInplaceSub(qhatv, j);
if (c) {
c = u->absoluteInplaceAdd(divisor, j);
u->setDigit(j + n, u->digit(j + n) + c);
qhat--;
}
if (quotient != nullptr)
q->setDigit(j, qhat);
}
if (quotient != nullptr) {
// Caller will right-trim.
*quotient = q;
}
if (remainder != nullptr) {
u->inplaceRightShift(shift);
*remainder = u;
}
}
// Returns whether (factor1 * factor2) > (high << digitBits) + low.
inline bool JSBigInt::productGreaterThan(Digit factor1, Digit factor2, Digit high, Digit low)
{
Digit resultHigh;
Digit resultLow = digitMul(factor1, factor2, resultHigh);
return resultHigh > high || (resultHigh == high && resultLow > low);
}
// Adds {summand} onto {this}, starting with {summand}'s 0th digit
// at {this}'s {startIndex}'th digit. Returns the "carry" (0 or 1).
JSBigInt::Digit JSBigInt::absoluteInplaceAdd(JSBigInt* summand, unsigned startIndex)
{
Digit carry = 0;
unsigned n = summand->length();
ASSERT(length() >= startIndex + n);
for (unsigned i = 0; i < n; i++) {
Digit newCarry = 0;
Digit sum = digitAdd(digit(startIndex + i), summand->digit(i), newCarry);
sum = digitAdd(sum, carry, newCarry);
setDigit(startIndex + i, sum);
carry = newCarry;
}
return carry;
}
// Subtracts {subtrahend} from {this}, starting with {subtrahend}'s 0th digit
// at {this}'s {startIndex}-th digit. Returns the "borrow" (0 or 1).
JSBigInt::Digit JSBigInt::absoluteInplaceSub(JSBigInt* subtrahend, unsigned startIndex)
{
Digit borrow = 0;
unsigned n = subtrahend->length();
ASSERT(length() >= startIndex + n);
for (unsigned i = 0; i < n; i++) {
Digit newBorrow = 0;
Digit difference = digitSub(digit(startIndex + i), subtrahend->digit(i), newBorrow);
difference = digitSub(difference, borrow, newBorrow);
setDigit(startIndex + i, difference);
borrow = newBorrow;
}
return borrow;
}
void JSBigInt::inplaceRightShift(unsigned shift)
{
ASSERT(shift < digitBits);
ASSERT(!(digit(0) & ((static_cast<Digit>(1) << shift) - 1)));
if (!shift)
return;
Digit carry = digit(0) >> shift;
unsigned last = length() - 1;
for (unsigned i = 0; i < last; i++) {
Digit d = digit(i + 1);
setDigit(i, (d << (digitBits - shift)) | carry);
carry = d >> shift;
}
setDigit(last, carry);
}
// Always copies the input, even when {shift} == 0.
template <typename BigIntImpl>
JSBigInt* JSBigInt::absoluteLeftShiftAlwaysCopy(JSGlobalObject* globalObject, BigIntImpl x, unsigned shift, LeftShiftMode mode)
{
VM& vm = globalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
ASSERT(shift < digitBits);
ASSERT(!x.isZero());
unsigned n = x.length();
unsigned resultLength = mode == LeftShiftMode::AlwaysAddOneDigit ? n + 1 : n;
JSBigInt* result = createWithLength(globalObject, resultLength);
RETURN_IF_EXCEPTION(scope, { });
if (!shift) {
for (unsigned i = 0; i < n; i++)
result->setDigit(i, x.digit(i));
if (mode == LeftShiftMode::AlwaysAddOneDigit)
result->setDigit(n, 0);
return result;
}
Digit carry = 0;
for (unsigned i = 0; i < n; i++) {
Digit d = x.digit(i);
result->setDigit(i, (d << shift) | carry);
carry = d >> (digitBits - shift);
}
if (mode == LeftShiftMode::AlwaysAddOneDigit)
result->setDigit(n, carry);
else {
ASSERT(mode == LeftShiftMode::SameSizeResult);
ASSERT(!carry);
}
return result;
}
// Helper for Absolute{And,AndNot,Or,Xor}.
// Performs the given binary {op} on digit pairs of {x} and {y}; when the
// end of the shorter of the two is reached, {extraDigits} configures how
// remaining digits in the longer input are handled: copied to the result
// or ignored.
// Example:
// y: [ y2 ][ y1 ][ y0 ]
// x: [ x3 ][ x2 ][ x1 ][ x0 ]
// | | | |
// (Copy) (op) (op) (op)
// | | | |
// v v v v
// result: [ 0 ][ x3 ][ r2 ][ r1 ][ r0 ]
template <typename BigIntImpl1, typename BigIntImpl2, typename BitwiseOp>
inline JSBigInt* JSBigInt::absoluteBitwiseOp(JSGlobalObject* globalObject, BigIntImpl1 x, BigIntImpl2 y, ExtraDigitsHandling extraDigits, BitwiseOp&& op)
{
VM& vm = globalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
unsigned xLength = x.length();
unsigned yLength = y.length();
unsigned numPairs = yLength;
unsigned maxLength = xLength;
if (xLength < yLength) {
numPairs = xLength;
maxLength = yLength;
}
ASSERT(numPairs == std::min(xLength, yLength));
ASSERT(maxLength == std::max(xLength, yLength));
unsigned resultLength = extraDigits == ExtraDigitsHandling::Copy ? maxLength : numPairs;
JSBigInt* result = createWithLength(globalObject, resultLength);
RETURN_IF_EXCEPTION(scope, nullptr);
unsigned i = 0;
for (; i < numPairs; i++)
result->setDigit(i, op(x.digit(i), y.digit(i)));
if (extraDigits == ExtraDigitsHandling::Copy) {
if (xLength > yLength) {
for (; i < xLength; i++)
result->setDigit(i, x.digit(i));
} else {
for (; i < yLength; i++)
result->setDigit(i, y.digit(i));
}
}
for (; i < resultLength; i++)
result->setDigit(i, 0);
RELEASE_AND_RETURN(scope, result->rightTrim(globalObject));
}
template <typename BigIntImpl1, typename BigIntImpl2>
JSBigInt* JSBigInt::absoluteAnd(JSGlobalObject* globalObject, BigIntImpl1 x, BigIntImpl2 y)
{
auto digitOperation = [](Digit a, Digit b) {
return a & b;
};
return absoluteBitwiseOp(globalObject, x, y, ExtraDigitsHandling::Skip, digitOperation);
}
template <typename BigIntImpl1, typename BigIntImpl2>
JSBigInt* JSBigInt::absoluteOr(JSGlobalObject* globalObject, BigIntImpl1 x, BigIntImpl2 y)
{
auto digitOperation = [](Digit a, Digit b) {
return a | b;
};
return absoluteBitwiseOp(globalObject, x, y, ExtraDigitsHandling::Copy, digitOperation);
}
template <typename BigIntImpl1, typename BigIntImpl2>
JSBigInt* JSBigInt::absoluteAndNot(JSGlobalObject* globalObject, BigIntImpl1 x, BigIntImpl2 y)
{
// x & ~y
VM& vm = globalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
unsigned xLength = x.length();
unsigned yLength = y.length();
unsigned resultLength = xLength;
JSBigInt* result = createWithLength(globalObject, resultLength);
RETURN_IF_EXCEPTION(scope, nullptr);
unsigned i = 0;
for (; i < std::min(xLength, yLength); i++)
result->setDigit(i, x.digit(i) & ~y.digit(i));
for (; i < resultLength; ++i)
result->setDigit(i, x.digit(i));
RELEASE_AND_RETURN(scope, result->rightTrim(globalObject));
}
template <typename BigIntImpl1, typename BigIntImpl2>
JSBigInt* JSBigInt::absoluteXor(JSGlobalObject* globalObject, BigIntImpl1 x, BigIntImpl2 y)
{
auto digitOperation = [](Digit a, Digit b) {
return a ^ b;
};
return absoluteBitwiseOp(globalObject, x, y, ExtraDigitsHandling::Copy, digitOperation);
}
template <typename BigIntImpl>
JSBigInt* JSBigInt::absoluteAddOne(JSGlobalObject* globalObject, BigIntImpl x, SignOption signOption)
{
VM& vm = globalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
unsigned inputLength = x.length();
// The addition will overflow into a new digit if all existing digits are
// at maximum.
bool willOverflow = true;
for (unsigned i = 0; i < inputLength; i++) {
if (std::numeric_limits<Digit>::max() != x.digit(i)) {
willOverflow = false;
break;
}
}
unsigned resultLength = inputLength + willOverflow;
JSBigInt* result = createWithLength(globalObject, resultLength);
RETURN_IF_EXCEPTION(scope, nullptr);
Digit carry = 1;
for (unsigned i = 0; i < inputLength; i++) {
Digit newCarry = 0;
result->setDigit(i, digitAdd(x.digit(i), carry, newCarry));
carry = newCarry;
}
if (resultLength > inputLength)
result->setDigit(inputLength, carry);
else
ASSERT(!carry);
result->setSign(signOption == SignOption::Signed);
RELEASE_AND_RETURN(scope, result->rightTrim(globalObject));
}
template <typename BigIntImpl>
JSBigInt* JSBigInt::absoluteSubOne(JSGlobalObject* globalObject, BigIntImpl x, unsigned resultLength)
{
ASSERT(!x.isZero());
ASSERT(resultLength >= x.length());
VM& vm = globalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
JSBigInt* result = createWithLength(globalObject, resultLength);
RETURN_IF_EXCEPTION(scope, nullptr);
unsigned length = x.length();
Digit borrow = 1;
for (unsigned i = 0; i < length; i++) {
Digit newBorrow = 0;
result->setDigit(i, digitSub(x.digit(i), borrow, newBorrow));
borrow = newBorrow;
}
ASSERT(!borrow);
for (unsigned i = length; i < resultLength; i++)
result->setDigit(i, borrow);
RELEASE_AND_RETURN(scope, result->rightTrim(globalObject));
}
template <typename BigIntImpl1, typename BigIntImpl2>
JSBigInt::ImplResult JSBigInt::leftShiftByAbsolute(JSGlobalObject* globalObject, BigIntImpl1 x, BigIntImpl2 y)
{
VM& vm = globalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
auto optionalShift = toShiftAmount(y);
if (!optionalShift) {
throwOutOfMemoryError(globalObject, scope, "BigInt generated from this operation is too big"_s);
return nullptr;
}
Digit shift = *optionalShift;
unsigned digitShift = static_cast<unsigned>(shift / digitBits);
unsigned bitsShift = static_cast<unsigned>(shift % digitBits);
unsigned length = x.length();
bool grow = bitsShift && (x.digit(length - 1) >> (digitBits - bitsShift));
int resultLength = length + digitShift + grow;
if (static_cast<unsigned>(resultLength) > maxLength) {
throwOutOfMemoryError(globalObject, scope, "BigInt generated from this operation is too big"_s);
return nullptr;
}
JSBigInt* result = createWithLength(globalObject, resultLength);
RETURN_IF_EXCEPTION(scope, nullptr);
if (!bitsShift) {
unsigned i = 0;
for (; i < digitShift; i++)
result->setDigit(i, 0ul);
for (; i < static_cast<unsigned>(resultLength); i++)
result->setDigit(i, x.digit(i - digitShift));
} else {
Digit carry = 0;
for (unsigned i = 0; i < digitShift; i++)
result->setDigit(i, 0ul);
for (unsigned i = 0; i < length; i++) {
Digit d = x.digit(i);
result->setDigit(i + digitShift, (d << bitsShift) | carry);
carry = d >> (digitBits - bitsShift);
}
if (grow)
result->setDigit(length + digitShift, carry);
else
ASSERT(!carry);
}
result->setSign(x.sign());
RELEASE_AND_RETURN(scope, result->rightTrim(globalObject));
}
template <typename BigIntImpl1, typename BigIntImpl2>
JSBigInt::ImplResult JSBigInt::rightShiftByAbsolute(JSGlobalObject* globalObject, BigIntImpl1 x, BigIntImpl2 y)
{
VM& vm = globalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
unsigned length = x.length();
bool sign = x.sign();
auto optionalShift = toShiftAmount(y);
if (!optionalShift)
RELEASE_AND_RETURN(scope, rightShiftByMaximum(globalObject, sign));
Digit shift = *optionalShift;
unsigned digitalShift = static_cast<unsigned>(shift / digitBits);
unsigned bitsShift = static_cast<unsigned>(shift % digitBits);
int resultLength = length - digitalShift;
if (resultLength <= 0)
RELEASE_AND_RETURN(scope, rightShiftByMaximum(globalObject, sign));
// For negative numbers, round down if any bit was shifted out (so that e.g.
// -5n >> 1n == -3n and not -2n). Check now whether this will happen and
// whether it can cause overflow into a new digit. If we allocate the result
// large enough up front, it avoids having to do a second allocation later.
bool mustRoundDown = false;
if (sign) {
const Digit mask = (static_cast<Digit>(1) << bitsShift) - 1;
if (x.digit(digitalShift) & mask)
mustRoundDown = true;
else {
for (unsigned i = 0; i < digitalShift; i++) {
if (x.digit(i)) {
mustRoundDown = true;
break;
}
}
}
}
// If bitsShift is non-zero, it frees up bits, preventing overflow.
if (mustRoundDown && !bitsShift) {
// Overflow cannot happen if the most significant digit has unset bits.
Digit msd = x.digit(length - 1);
bool roundingCanOverflow = !static_cast<Digit>(~msd);
if (roundingCanOverflow)
resultLength++;
}
ASSERT(static_cast<unsigned>(resultLength) <= length);
JSBigInt* result = createWithLength(globalObject, static_cast<unsigned>(resultLength));
RETURN_IF_EXCEPTION(scope, nullptr);
if (!bitsShift) {
result->setDigit(resultLength - 1, 0);
for (unsigned i = digitalShift; i < length; i++)
result->setDigit(i - digitalShift, x.digit(i));
} else {
Digit carry = x.digit(digitalShift) >> bitsShift;
unsigned last = length - digitalShift - 1;
for (unsigned i = 0; i < last; i++) {
Digit d = x.digit(i + digitalShift + 1);
result->setDigit(i, (d << (digitBits - bitsShift)) | carry);
carry = d >> bitsShift;
}
result->setDigit(last, carry);
}
if (sign) {
result->setSign(true);
if (mustRoundDown) {
// Since the result is negative, rounding down means adding one to
// its absolute value. This cannot overflow.
result = result->rightTrim(globalObject);
RETURN_IF_EXCEPTION(scope, nullptr);
RELEASE_AND_RETURN(scope, absoluteAddOne(globalObject, HeapBigIntImpl { result }, SignOption::Signed));
}
}
RELEASE_AND_RETURN(scope, result->rightTrim(globalObject));
}
JSBigInt::ImplResult JSBigInt::rightShiftByMaximum(JSGlobalObject* globalObject, bool sign)
{
if (sign)
return createFrom(globalObject, -1);
return createZero(globalObject);
}
// Lookup table for the maximum number of bits required per character of a
// base-N string representation of a number. To increase accuracy, the array
// value is the actual value multiplied by 32. To generate this table:
// for (var i = 0; i <= 36; i++) { print(Math.ceil(Math.log2(i) * 32) + ","); }
constexpr uint8_t maxBitsPerCharTable[] = {
0, 0, 32, 51, 64, 75, 83, 90, 96, // 0..8
102, 107, 111, 115, 119, 122, 126, 128, // 9..16
131, 134, 136, 139, 141, 143, 145, 147, // 17..24
149, 151, 153, 154, 156, 158, 159, 160, // 25..32
162, 163, 165, 166, // 33..36
};
static constexpr unsigned bitsPerCharTableShift = 5;
static constexpr size_t bitsPerCharTableMultiplier = 1u << bitsPerCharTableShift;
// Compute (an overapproximation of) the length of the resulting string:
// Divide bit length of the BigInt by bits representable per character.
uint64_t JSBigInt::calculateMaximumCharactersRequired(unsigned length, unsigned radix, Digit lastDigit, bool sign)
{
unsigned leadingZeros = clz(lastDigit);
size_t bitLength = length * digitBits - leadingZeros;
// Maximum number of bits we can represent with one character. We'll use this
// to find an appropriate chunk size below.
uint8_t maxBitsPerChar = maxBitsPerCharTable[radix];
// For estimating result length, we have to be pessimistic and work with
// the minimum number of bits one character can represent.
uint8_t minBitsPerChar = maxBitsPerChar - 1;
// Perform the following computation with uint64_t to avoid overflows.
uint64_t maximumCharactersRequired = bitLength;
maximumCharactersRequired *= bitsPerCharTableMultiplier;
// Round up.
maximumCharactersRequired += minBitsPerChar - 1;
maximumCharactersRequired /= minBitsPerChar;
maximumCharactersRequired += sign;
return maximumCharactersRequired;
}
String JSBigInt::toStringBasePowerOfTwo(VM& vm, JSGlobalObject* nullOrGlobalObjectForOOM, JSBigInt* x, unsigned radix)
{
ASSERT(hasOneBitSet(radix));
ASSERT(radix >= 2 && radix <= 32);
ASSERT(!x->isZero());
const unsigned length = x->length();
const bool sign = x->sign();
const unsigned bitsPerChar = ctz(radix);
const unsigned charMask = radix - 1;
// Compute the length of the resulting string: divide the bit length of the
// BigInt by the number of bits representable per character (rounding up).
const Digit msd = x->digit(length - 1);
const unsigned msdLeadingZeros = clz(msd);
const size_t bitLength = length * digitBits - msdLeadingZeros;
const size_t charsRequired = (bitLength + bitsPerChar - 1) / bitsPerChar + sign;
if (charsRequired > JSString::MaxLength) {
if (nullOrGlobalObjectForOOM) {
auto scope = DECLARE_THROW_SCOPE(vm);
throwOutOfMemoryError(nullOrGlobalObjectForOOM, scope);
}
return String();
}
Vector<LChar> resultString(charsRequired);
Digit digit = 0;
// Keeps track of how many unprocessed bits there are in {digit}.
unsigned availableBits = 0;
int pos = static_cast<int>(charsRequired - 1);
for (unsigned i = 0; i < length - 1; i++) {
Digit newDigit = x->digit(i);
// Take any leftover bits from the last iteration into account.
int current = (digit | (newDigit << availableBits)) & charMask;
resultString[pos--] = radixDigits[current];
int consumedBits = bitsPerChar - availableBits;
digit = newDigit >> consumedBits;
availableBits = digitBits - consumedBits;
while (availableBits >= bitsPerChar) {
resultString[pos--] = radixDigits[digit & charMask];
digit >>= bitsPerChar;
availableBits -= bitsPerChar;
}
}
// Take any leftover bits from the last iteration into account.
int current = (digit | (msd << availableBits)) & charMask;
resultString[pos--] = radixDigits[current];
digit = msd >> (bitsPerChar - availableBits);
while (digit) {
resultString[pos--] = radixDigits[digit & charMask];
digit >>= bitsPerChar;
}
if (sign)
resultString[pos--] = '-';
ASSERT(pos == -1);
return StringImpl::adopt(WTFMove(resultString));
}
String JSBigInt::toStringGeneric(VM& vm, JSGlobalObject* nullOrGlobalObjectForOOM, JSBigInt* x, unsigned radix)
{
// FIXME: [JSC] Revisit usage of Vector into JSBigInt::toString
// https://bugs.webkit.org/show_bug.cgi?id=180671
Vector<LChar> resultString;
ASSERT(radix >= 2 && radix <= 36);
ASSERT(!x->isZero());
unsigned length = x->length();
bool sign = x->sign();
uint8_t maxBitsPerChar = maxBitsPerCharTable[radix];
uint64_t maximumCharactersRequired = calculateMaximumCharactersRequired(length, radix, x->digit(length - 1), sign);
if (maximumCharactersRequired > JSString::MaxLength) {
if (nullOrGlobalObjectForOOM) {
auto scope = DECLARE_THROW_SCOPE(vm);
throwOutOfMemoryError(nullOrGlobalObjectForOOM, scope);
}
return String();
}
Digit lastDigit;
if (length == 1)
lastDigit = x->digit(0);
else {
unsigned chunkChars = digitBits * bitsPerCharTableMultiplier / maxBitsPerChar;
Digit chunkDivisor = digitPow(radix, chunkChars);
// By construction of chunkChars, there can't have been overflow.
ASSERT(chunkDivisor);
unsigned nonZeroDigit = length - 1;
ASSERT(x->digit(nonZeroDigit));
// {rest} holds the part of the BigInt that we haven't looked at yet.
// Not to be confused with "remainder"!
JSBigInt* rest = nullptr;
// In the first round, divide the input, allocating a new BigInt for
// the result == rest; from then on divide the rest in-place.
JSBigInt** dividend = &x;
do {
Digit chunk;
bool success = absoluteDivWithDigitDivisor(nullOrGlobalObjectForOOM, vm, HeapBigIntImpl { *dividend }, chunkDivisor, &rest, chunk);
if (!success)
return String();
dividend = &rest;
for (unsigned i = 0; i < chunkChars; i++) {
resultString.append(radixDigits[chunk % radix]);
chunk /= radix;
}
ASSERT(!chunk);
if (!rest->digit(nonZeroDigit))
nonZeroDigit--;
// We can never clear more than one digit per iteration, because
// chunkDivisor is smaller than max digit value.
ASSERT(rest->digit(nonZeroDigit));
} while (nonZeroDigit > 0);
lastDigit = rest->digit(0);
}
do {
resultString.append(radixDigits[lastDigit % radix]);
lastDigit /= radix;
} while (lastDigit > 0);
ASSERT(resultString.size());
ASSERT(resultString.size() <= static_cast<size_t>(maximumCharactersRequired));
// Remove leading zeroes.
unsigned newSizeNoLeadingZeroes = resultString.size();
while (newSizeNoLeadingZeroes > 1 && resultString[newSizeNoLeadingZeroes - 1] == '0')
newSizeNoLeadingZeroes--;
resultString.shrink(newSizeNoLeadingZeroes);
if (sign)
resultString.append('-');
std::reverse(resultString.begin(), resultString.end());
return StringImpl::adopt(WTFMove(resultString));
}
JSBigInt* JSBigInt::rightTrim(JSGlobalObject* nullOrGlobalObjectForOOM, VM& vm)
{
if (isZero()) {
ASSERT(!sign());
return this;
}
int nonZeroIndex = m_length - 1;
while (nonZeroIndex >= 0 && !digit(nonZeroIndex))
nonZeroIndex--;
if (nonZeroIndex < 0)
return createZero(nullOrGlobalObjectForOOM, vm);
if (nonZeroIndex == static_cast<int>(m_length - 1))
return this;
unsigned newLength = nonZeroIndex + 1;
JSBigInt* trimmedBigInt = createWithLength(nullOrGlobalObjectForOOM, vm, newLength);
if (UNLIKELY(!trimmedBigInt))
return nullptr;
std::copy_n(dataStorage(), newLength, trimmedBigInt->dataStorage());
trimmedBigInt->setSign(this->sign());
ensureStillAliveHere(this);
return trimmedBigInt;
}
JSBigInt* JSBigInt::rightTrim(JSGlobalObject* globalObject)
{
return rightTrim(globalObject, globalObject->vm());
}
JSBigInt* JSBigInt::tryRightTrim(VM& vm)
{
return rightTrim(nullptr, vm);
}
JSBigInt* JSBigInt::allocateFor(JSGlobalObject* nullOrGlobalObjectForOOM, VM& vm, unsigned radix, unsigned charcount)
{
ASSERT(2 <= radix && radix <= 36);
size_t bitsPerChar = maxBitsPerCharTable[radix];
size_t chars = charcount;
const unsigned roundup = bitsPerCharTableMultiplier - 1;
if (chars <= (std::numeric_limits<size_t>::max() - roundup) / bitsPerChar) {
size_t bitsMin = bitsPerChar * chars;
// Divide by 32 (see table), rounding up.
bitsMin = (bitsMin + roundup) >> bitsPerCharTableShift;
if (bitsMin <= static_cast<size_t>(maxInt)) {
// Divide by kDigitsBits, rounding up.
unsigned length = (bitsMin + digitBits - 1) / digitBits;
if (length <= maxLength)
return createWithLength(nullOrGlobalObjectForOOM, vm, length);
}
}
if (nullOrGlobalObjectForOOM) {
auto scope = DECLARE_THROW_SCOPE(vm);
throwOutOfMemoryError(nullOrGlobalObjectForOOM, scope, "BigInt generated from this operation is too big"_s);
}
return nullptr;
}
size_t JSBigInt::estimatedSize(JSCell* cell, VM& vm)
{
return Base::estimatedSize(cell, vm) + jsCast<JSBigInt*>(cell)->m_length * sizeof(Digit);
}
double JSBigInt::toNumber(JSGlobalObject* globalObject) const
{
VM& vm = globalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
throwTypeError(globalObject, scope, "Conversion from 'BigInt' to 'number' is not allowed."_s);
return 0.0;
}
template <typename CharType>
JSValue JSBigInt::parseInt(JSGlobalObject* globalObject, std::span<const CharType> data, ErrorParseMode errorParseMode)
{
VM& vm = globalObject->vm();
size_t p = 0;
while (p < data.size() && isStrWhiteSpace(data[p]))
++p;
// Check Radix from first characters
if (p + 1 < data.size() && data[p] == '0') {
if (isASCIIAlphaCaselessEqual(data[p + 1], 'b'))
return parseInt(globalObject, vm, data, p + 2, 2, errorParseMode, ParseIntSign::Unsigned, ParseIntMode::DisallowEmptyString);
if (isASCIIAlphaCaselessEqual(data[p + 1], 'x'))
return parseInt(globalObject, vm, data, p + 2, 16, errorParseMode, ParseIntSign::Unsigned, ParseIntMode::DisallowEmptyString);
if (isASCIIAlphaCaselessEqual(data[p + 1], 'o'))
return parseInt(globalObject, vm, data, p + 2, 8, errorParseMode, ParseIntSign::Unsigned, ParseIntMode::DisallowEmptyString);
}
ParseIntSign sign = ParseIntSign::Unsigned;
if (p < data.size()) {
if (data[p] == '-') {
sign = ParseIntSign::Signed;
++p;
} else if (data[p] == '+')
++p;
}
return parseInt(globalObject, vm, data, p, 10, errorParseMode, sign);
}
template <typename CharType>
JSValue JSBigInt::parseInt(JSGlobalObject* nullOrGlobalObjectForOOM, VM& vm, std::span<const CharType> data, unsigned startIndex, unsigned radix, ErrorParseMode errorParseMode, ParseIntSign sign, ParseIntMode parseMode)
{
size_t p = startIndex;
if (parseMode != ParseIntMode::AllowEmptyString && startIndex == data.size()) {
ASSERT(nullOrGlobalObjectForOOM);
if (errorParseMode == ErrorParseMode::ThrowExceptions) {
auto scope = DECLARE_THROW_SCOPE(vm);
throwVMError(nullOrGlobalObjectForOOM, scope, createSyntaxError(nullOrGlobalObjectForOOM, "Failed to parse String to BigInt"_s));
}
return JSValue();
}
// Skipping leading zeros
while (p < data.size() && data[p] == '0')
++p;
int endIndex = data.size() - 1;
// Removing trailing spaces
while (endIndex >= static_cast<int>(p) && isStrWhiteSpace(data[endIndex]))
--endIndex;
size_t length = endIndex + 1;
if (p == length) {
#if USE(BIGINT32)
return jsBigInt32(0);
#else
return createZero(nullOrGlobalObjectForOOM, vm);
#endif
}
unsigned lengthLimitForBigInt32;
#if USE(BIGINT32)
static_assert(sizeof(Digit) >= sizeof(uint64_t));
// The idea is to pick the limit such that:
// radix ** lengthLimitForBigInt32 >= INT32_MAX
// radix ** (lengthLimitForBigInt32 - 1) <= INT32_MAX
#if ASSERT_ENABLED
auto limitWorks = [&] {
double lengthLimit = lengthLimitForBigInt32;
double lowerLimit = pow(static_cast<double>(radix), lengthLimit - 1);
double upperLimit = pow(static_cast<double>(radix), lengthLimit);
double target = std::numeric_limits<int32_t>::max();
return lowerLimit <= target && target <= upperLimit && upperLimit <= std::numeric_limits<int64_t>::max();
};
#endif
switch (radix) {
case 2:
lengthLimitForBigInt32 = 31;
ASSERT(limitWorks());
break;
case 8:
lengthLimitForBigInt32 = 11;
ASSERT(limitWorks());
break;
case 10:
lengthLimitForBigInt32 = 10;
ASSERT(limitWorks());
break;
case 16:
lengthLimitForBigInt32 = 8;
ASSERT(limitWorks());
break;
default:
lengthLimitForBigInt32 = 1;
break;
}
#else
// The idea is to pick the largest limit such that:
// radix ** lengthLimitForBigInt32 <= INT32_MAX
#if ASSERT_ENABLED
auto limitWorks = [&] {
double lengthLimit = lengthLimitForBigInt32;
double valueLimit = pow(static_cast<double>(radix), lengthLimit);
double overValueLimit = pow(static_cast<double>(radix), lengthLimit + 1);
double target = std::numeric_limits<int32_t>::max();
return valueLimit <= target && target < overValueLimit;
};
#endif
switch (radix) {
case 2:
lengthLimitForBigInt32 = 30;
ASSERT(limitWorks());
break;
case 8:
lengthLimitForBigInt32 = 10;
ASSERT(limitWorks());
break;
case 10:
lengthLimitForBigInt32 = 9;
ASSERT(limitWorks());
break;
case 16:
lengthLimitForBigInt32 = 7;
ASSERT(limitWorks());
break;
default:
lengthLimitForBigInt32 = 1;
break;
}
#endif // USE(BIGINT32)
JSBigInt* heapResult = nullptr;
unsigned limit0 = '0' + (radix < 10 ? radix : 10);
unsigned limita = 'a' + (static_cast<int32_t>(radix) - 10);
unsigned limitA = 'A' + (static_cast<int32_t>(radix) - 10);
unsigned initialLength = length - p;
while (p < length) {
Checked<uint64_t, CrashOnOverflow> digit = 0;
Checked<uint64_t, CrashOnOverflow> multiplier = 1;
for (unsigned i = 0; i < lengthLimitForBigInt32 && p < length; ++i, ++p) {
digit *= radix;
multiplier *= radix;
if (data[p] >= '0' && data[p] < limit0)
digit += static_cast<uint64_t>(data[p] - '0');
else if (data[p] >= 'a' && data[p] < limita)
digit += static_cast<uint64_t>(data[p] - 'a' + 10);
else if (data[p] >= 'A' && data[p] < limitA)
digit += static_cast<uint64_t>(data[p] - 'A' + 10);
else {
if (errorParseMode == ErrorParseMode::ThrowExceptions) {
auto scope = DECLARE_THROW_SCOPE(vm);
ASSERT(nullOrGlobalObjectForOOM);
throwVMError(nullOrGlobalObjectForOOM, scope, createSyntaxError(nullOrGlobalObjectForOOM, "Failed to parse String to BigInt"_s));
}
return JSValue();
}
}
if (!heapResult) {
if (p == length) {
ASSERT(digit <= static_cast<uint64_t>(std::numeric_limits<int64_t>::max()));
int64_t maybeResult = digit;
ASSERT(maybeResult >= 0);
if (sign == ParseIntSign::Signed)
maybeResult *= -1;
if (static_cast<int64_t>(static_cast<int32_t>(maybeResult)) == maybeResult) {
#if USE(BIGINT32)
return jsBigInt32(static_cast<int32_t>(maybeResult));
#else
return JSBigInt::createFrom(nullOrGlobalObjectForOOM, vm, static_cast<int32_t>(maybeResult));
#endif
}
}
heapResult = allocateFor(nullOrGlobalObjectForOOM, vm, radix, initialLength);
if (UNLIKELY(!heapResult))
return JSValue();
heapResult->initialize(InitializationType::WithZero);
}
ASSERT(static_cast<uint64_t>(static_cast<Digit>(multiplier)) == multiplier);
ASSERT(static_cast<uint64_t>(static_cast<Digit>(digit)) == digit);
heapResult->inplaceMultiplyAdd(static_cast<Digit>(multiplier), static_cast<Digit>(digit));
}
heapResult->setSign(sign == ParseIntSign::Signed);
return heapResult->rightTrim(nullOrGlobalObjectForOOM, vm);
}
JSObject* JSBigInt::toObject(JSGlobalObject* globalObject) const
{
return BigIntObject::create(globalObject->vm(), globalObject, const_cast<JSBigInt*>(this));
}
bool JSBigInt::equalsToNumber(JSValue numValue)
{
ASSERT(numValue.isNumber());
if (numValue.isInt32())
return equalsToInt32(numValue.asInt32());
double value = numValue.asDouble();
return compareToDouble(this, value) == ComparisonResult::Equal;
}
bool JSBigInt::equalsToInt32(int32_t value)
{
if (!value)
return this->isZero();
return (this->length() == 1) && (this->sign() == (value < 0)) && (this->digit(0) == static_cast<Digit>(std::abs(static_cast<int64_t>(value))));
}
JSBigInt::ComparisonResult JSBigInt::compareToDouble(JSBigInt* x, double y)
{
return compareToDouble(HeapBigIntImpl { x }, y);
}
JSBigInt::ComparisonResult JSBigInt::compareToDouble(double x, JSBigInt* y)
{
return compareToDouble(x, HeapBigIntImpl { y });
}
template <typename BigIntImpl>
JSBigInt::ComparisonResult JSBigInt::compareToDouble(BigIntImpl x, double y)
{
// This algorithm expect that the double format is IEEE 754
uint64_t doubleBits = std::bit_cast<uint64_t>(y);
int rawExponent = static_cast<int>(doubleBits >> 52) & 0x7FF;
// Handle finite doubles for {y}.
if (rawExponent == 0x7FF) {
if (std::isnan(y))
return ComparisonResult::Undefined;
return (y == std::numeric_limits<double>::infinity()) ? ComparisonResult::LessThan : ComparisonResult::GreaterThan;
}
bool xSign = x.sign();
// Note that this is different from the double's sign bit for -0. That's
// intentional because -0 must be treated like 0.
bool ySign = y < 0;
if (xSign != ySign)
return xSign ? ComparisonResult::LessThan : ComparisonResult::GreaterThan;
if (!y) {
// If {y} is zero, then ySign is false and xSign must be false.
ASSERT(!xSign);
return x.isZero() ? ComparisonResult::Equal : ComparisonResult::GreaterThan;
}
if (x.isZero()) {
// If {x} is zero, then xSign is false and ySign must be false which indicates that {y} is greater than zero.
ASSERT(!ySign && y > 0);
return ComparisonResult::LessThan;
}
// Right now, only two cases left:
// {x} >= 1 and {y} > 0
// {x} <= -1 and {y} < 0
// Non-finite doubles are handled above.
ASSERT(rawExponent != 0x7FF);
int exponent = rawExponent - 0x3FF;
if (exponent < 0) {
// The absolute value of the double is less than 1. Only 0n has an
// absolute value smaller than that, but we've already covered that case.
// Note that this also handles denormal doubles for {y}.
return xSign ? ComparisonResult::LessThan : ComparisonResult::GreaterThan;
}
int xLength = x.length();
Digit xMSD = x.digit(xLength - 1);
int msdLeadingZeros = clz(xMSD);
int xBitLength = xLength * digitBits - msdLeadingZeros;
int yBitLength = exponent + 1;
if (xBitLength < yBitLength)
return xSign? ComparisonResult::GreaterThan : ComparisonResult::LessThan;
if (xBitLength > yBitLength)
return xSign ? ComparisonResult::LessThan : ComparisonResult::GreaterThan;
// At this point, we know that signs and bit lengths (i.e. position of
// the most significant bit in exponent-free representation) are identical.
// {x} is not zero, {y} is finite and not denormal.
// Now we virtually convert the double to an integer by shifting its
// mantissa according to its exponent, so it will align with the BigInt {x},
// and then we compare them bit for bit until we find a difference or the
// least significant bit.
// <----- 52 ------> <-- virtual trailing zeroes -->
// y / mantissa: 1yyyyyyyyyyyyyyyyy 0000000000000000000000000000000
// x / digits: 0001xxxx xxxxxxxx xxxxxxxx ...
// <--> <------>
// msdTopBit digitBits
//
uint64_t mantissa = doubleBits & 0x000FFFFFFFFFFFFF;
mantissa |= 0x0010000000000000;
const int mantissaTopBit = 52; // 0-indexed.
// 0-indexed position of {x}'s most significant bit within the {msd}.
int msdTopBit = digitBits - 1 - msdLeadingZeros;
ASSERT(msdTopBit == static_cast<int>((xBitLength - 1) % digitBits));
// Shifted chunk of {mantissa} for comparing with {digit}.
Digit compareMantissa;
// Number of unprocessed bits in {mantissa}. We'll keep them shifted to
// the left (i.e. most significant part) of the underlying uint64_t.
int remainingMantissaBits = 0;
// First, compare the most significant digit against the beginning of
// the mantissa and then we align them.
if (msdTopBit < mantissaTopBit) {
remainingMantissaBits = (mantissaTopBit - msdTopBit);
compareMantissa = static_cast<Digit>(mantissa >> remainingMantissaBits);
mantissa = mantissa << (64 - remainingMantissaBits);
} else {
compareMantissa = static_cast<Digit>(mantissa << (msdTopBit - mantissaTopBit));
mantissa = 0;
}
if (xMSD > compareMantissa)
return xSign ? ComparisonResult::LessThan : ComparisonResult::GreaterThan;
if (xMSD < compareMantissa)
return xSign ? ComparisonResult::GreaterThan : ComparisonResult::LessThan;
// Then, compare additional digits against any remaining mantissa bits.
for (int digitIndex = xLength - 2; digitIndex >= 0; digitIndex--) {
if (remainingMantissaBits > 0) {
remainingMantissaBits -= digitBits;
if (sizeof(mantissa) != sizeof(xMSD)) {
compareMantissa = static_cast<Digit>(mantissa >> (64 - digitBits));
// "& 63" to appease compilers. digitBits is 32 here anyway.
mantissa = mantissa << (digitBits & 63);
} else {
compareMantissa = static_cast<Digit>(mantissa);
mantissa = 0;
}
} else
compareMantissa = 0;
Digit digit = x.digit(digitIndex);
if (digit > compareMantissa)
return xSign ? ComparisonResult::LessThan : ComparisonResult::GreaterThan;
if (digit < compareMantissa)
return xSign ? ComparisonResult::GreaterThan : ComparisonResult::LessThan;
}
// Integer parts are equal; check whether {y} has a fractional part.
if (mantissa) {
ASSERT(remainingMantissaBits > 0);
return xSign ? ComparisonResult::GreaterThan : ComparisonResult::LessThan;
}
return ComparisonResult::Equal;
}
JSBigInt::ComparisonResult JSBigInt::compareToDouble(int32_t x, double y)
{
return compareToDouble(Int32BigIntImpl { x }, y);
}
JSBigInt::ComparisonResult JSBigInt::compareToDouble(int64_t x, double y)
{
return compareToDouble(Int64BigIntImpl { x }, y);
}
JSBigInt::ComparisonResult JSBigInt::compareToDouble(uint64_t x, double y)
{
return compareToDouble(Int64BigIntImpl { x }, y);
}
JSBigInt::ComparisonResult JSBigInt::compareToDouble(JSValue x, double y)
{
ASSERT(x.isBigInt());
#if USE(BIGINT32)
if (x.isBigInt32())
return compareToDouble(x.bigInt32AsInt32(), y);
#endif
return compareToDouble(x.asHeapBigInt(), y);
}
template <typename BigIntImpl>
std::optional<JSBigInt::Digit> JSBigInt::toShiftAmount(BigIntImpl x)
{
if (x.length() > 1)
return std::nullopt;
Digit value = x.digit(0);
static_assert(maxLengthBits < std::numeric_limits<Digit>::max(), "maxLengthBits needs to be less than digit");
if (value > maxLengthBits)
return std::nullopt;
return value;
}
JSBigInt::RoundingResult JSBigInt::decideRounding(JSBigInt* bigInt, int32_t mantissaBitsUnset, int32_t digitIndex, uint64_t currentDigit)
{
if (mantissaBitsUnset > 0)
return RoundingResult::RoundDown;
int32_t topUnconsumedBit = 0;
if (mantissaBitsUnset < 0) {
// There are unconsumed bits in currentDigit.
topUnconsumedBit = -mantissaBitsUnset - 1;
} else {
ASSERT(mantissaBitsUnset == 0);
// currentDigit fit the mantissa exactly; look at the next digit.
if (digitIndex == 0)
return RoundingResult::RoundDown;
digitIndex--;
currentDigit = static_cast<uint64_t>(bigInt->digit(digitIndex));
topUnconsumedBit = digitBits - 1;
}
// If the most significant remaining bit is 0, round down.
uint64_t bitmask = static_cast<uint64_t>(1) << topUnconsumedBit;
if ((currentDigit & bitmask) == 0)
return RoundingResult::RoundDown;
// If any other remaining bit is set, round up.
bitmask -= 1;
if ((currentDigit & bitmask) != 0)
return RoundingResult::RoundUp;
while (digitIndex > 0) {
digitIndex--;
if (bigInt->digit(digitIndex) != 0)
return RoundingResult::RoundUp;
}
return RoundingResult::Tie;
}
JSValue JSBigInt::toNumberHeap(JSBigInt* bigInt)
{
if (bigInt->isZero())
return jsNumber(0);
ASSERT(bigInt->length());
// Conversion mechanism is the following.
//
// 1. Get exponent bits.
// 2. Collect mantissa 52 bits.
// 3. Add rounding result of unused bits to mantissa and adjust mantissa & exponent bits.
// 4. Generate double by combining (1) and (3).
const unsigned length = bigInt->length();
const bool sign = bigInt->sign();
const Digit msd = bigInt->digit(length - 1);
const unsigned msdLeadingZeros = clz(msd);
const size_t bitLength = length * digitBits - msdLeadingZeros;
// Double's exponent bits overflow.
if (bitLength > 1024)
return jsDoubleNumber(sign ? -std::numeric_limits<double>::infinity() : std::numeric_limits<double>::infinity());
uint64_t exponent = bitLength - 1;
uint64_t currentDigit = msd;
int32_t digitIndex = length - 1;
int32_t shiftAmount = msdLeadingZeros + 1 + (64 - digitBits);
ASSERT(1 <= shiftAmount);
ASSERT(shiftAmount <= 64);
uint64_t mantissa = (shiftAmount == 64) ? 0 : currentDigit << shiftAmount;
// unsetBits = 64 - setBits - 12 // 12 for non-mantissa bits
// setBits = 64 - (msdLeadingZeros + 1 + bitsNotAvailableDueToDigitSize); // 1 for hidden mantissa bit.
// = 64 - (msdLeadingZeros + 1 + (64 - digitBits))
// = 64 - shiftAmount
// Hence, unsetBits = 64 - (64 - shiftAmount) - 12 = shiftAmount - 12
mantissa >>= 12; // (12 = 64 - 52), we shift 12 bits to put 12 zeros in uint64_t mantissa.
int32_t mantissaBitsUnset = shiftAmount - 12;
// If not all mantissa bits are defined yet, get more digits as needed.
// Collect mantissa 52bits from several digits.
if constexpr (digitBits < 64) {
if (mantissaBitsUnset >= static_cast<int32_t>(digitBits) && digitIndex > 0) {
digitIndex--;
currentDigit = static_cast<uint64_t>(bigInt->digit(digitIndex));
mantissa |= (currentDigit << (mantissaBitsUnset - digitBits));
mantissaBitsUnset -= digitBits;
}
}
if (mantissaBitsUnset > 0 && digitIndex > 0) {
ASSERT(mantissaBitsUnset < static_cast<int32_t>(digitBits));
digitIndex--;
currentDigit = static_cast<uint64_t>(bigInt->digit(digitIndex));
mantissa |= (currentDigit >> (digitBits - mantissaBitsUnset));
mantissaBitsUnset -= digitBits;
}
// If there are unconsumed digits left, we may have to round.
RoundingResult rounding = decideRounding(bigInt, mantissaBitsUnset, digitIndex, currentDigit);
if (rounding == RoundingResult::RoundUp || (rounding == RoundingResult::Tie && (mantissa & 1) == 1)) {
++mantissa;
// Incrementing the mantissa can overflow the mantissa bits. In that case the new mantissa will be all zero (plus hidden bit).
if ((mantissa >> doublePhysicalMantissaSize) != 0) {
mantissa = 0;
exponent++;
// Incrementing the exponent can overflow too.
if (exponent > 1023)
return jsDoubleNumber(sign ? -std::numeric_limits<double>::infinity() : std::numeric_limits<double>::infinity());
}
}
uint64_t signBit = sign ? (static_cast<uint64_t>(1) << 63) : 0;
exponent = (exponent + 0x3ff) << doublePhysicalMantissaSize; // 0x3ff is double exponent bias.
uint64_t doubleBits = signBit | exponent | mantissa;
ASSERT((doubleBits & (static_cast<uint64_t>(1) << 63)) == signBit);
ASSERT((doubleBits & (static_cast<uint64_t>(0x7ff) << 52)) == exponent);
ASSERT((doubleBits & ((static_cast<uint64_t>(1) << 52) - 1)) == mantissa);
return jsNumber(std::bit_cast<double>(doubleBits));
}
template <typename BigIntImpl>
JSBigInt::ImplResult JSBigInt::asIntNImpl(JSGlobalObject* globalObject, uint64_t n, BigIntImpl bigInt)
{
VM& vm = globalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
if (bigInt.isZero())
return { bigInt };
if (n == 0)
RELEASE_AND_RETURN(scope, zeroImpl(globalObject));
uint64_t neededLength = (n + digitBits - 1) / digitBits;
uint64_t length = static_cast<uint64_t>(bigInt.length());
// If bigInt has less than n bits, return it directly.
if (length < neededLength)
return { bigInt };
ASSERT(neededLength <= INT32_MAX);
Digit topDigit = bigInt.digit(static_cast<int32_t>(neededLength) - 1);
Digit compareDigit = static_cast<Digit>(1) << ((n - 1) % digitBits);
if (length == neededLength && topDigit < compareDigit)
return { bigInt };
// Otherwise we have to truncate (which is a no-op in the special case
// of bigInt == -2^(n-1)), and determine the right sign. We also might have
// to subtract from 2^n to simulate having two's complement representation.
// In most cases, the result's sign is bigInt.sign() xor "(n-1)th bit present".
// The only exception is when bigInt is negative, has the (n-1)th bit, and all
// its bits below (n-1) are zero. In that case, the result is the minimum
// n-bit integer (example: asIntN(3, -12n) => -4n).
bool hasBit = (topDigit & compareDigit) == compareDigit;
ASSERT(n <= INT32_MAX);
int32_t N = static_cast<int32_t>(n);
if (!hasBit)
RELEASE_AND_RETURN(scope, truncateToNBits(globalObject, N, bigInt));
if (!bigInt.sign())
RELEASE_AND_RETURN(scope, truncateAndSubFromPowerOfTwo(globalObject, N, bigInt, true));
// Negative numbers must subtract from 2^n, except for the special case
// described above.
if ((topDigit & (compareDigit - 1)) == 0) {
for (int32_t i = static_cast<int32_t>(neededLength) - 2; i >= 0; i--) {
if (bigInt.digit(i) != 0)
RELEASE_AND_RETURN(scope, truncateAndSubFromPowerOfTwo(globalObject, N, bigInt, false));
}
// Truncation is no-op if bigInt == -2^(n-1).
if (length == neededLength && topDigit == compareDigit)
return { bigInt };
RELEASE_AND_RETURN(scope, truncateToNBits(globalObject, N, bigInt));
}
RELEASE_AND_RETURN(scope, truncateAndSubFromPowerOfTwo(globalObject, N, bigInt, false));
}
template <typename BigIntImpl>
JSBigInt::ImplResult JSBigInt::asUintNImpl(JSGlobalObject* globalObject, uint64_t n, BigIntImpl bigInt)
{
VM& vm = globalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
if (bigInt.isZero())
return { bigInt };
if (n == 0)
RELEASE_AND_RETURN(scope, zeroImpl(globalObject));
// If bigInt is negative, simulate two's complement representation.
if (bigInt.sign()) {
if (n > maxLengthBits) {
throwOutOfMemoryError(globalObject, scope, "BigInt generated from this operation is too big"_s);
return nullptr;
}
RELEASE_AND_RETURN(scope, truncateAndSubFromPowerOfTwo(globalObject, static_cast<int32_t>(n), bigInt, false));
}
// If bigInt is positive and has up to n bits, return it directly.
if (n >= maxLengthBits)
return { bigInt };
static_assert(maxLengthBits < INT32_MAX - digitBits);
int32_t neededLength = static_cast<int32_t>((n + digitBits - 1) / digitBits);
if (static_cast<int32_t>(bigInt.length()) < neededLength)
return { bigInt };
int32_t bitsInTopDigit = n % digitBits;
if (static_cast<int32_t>(bigInt.length()) == neededLength) {
if (bitsInTopDigit == 0)
return { bigInt };
Digit topDigit = bigInt.digit(neededLength - 1);
if ((topDigit >> bitsInTopDigit) == 0)
return { bigInt };
}
// Otherwise, truncate.
ASSERT(n <= INT32_MAX);
RELEASE_AND_RETURN(scope, truncateToNBits(globalObject, static_cast<int32_t>(n), bigInt));
}
template <typename BigIntImpl>
JSBigInt::ImplResult JSBigInt::truncateToNBits(JSGlobalObject* globalObject, int32_t n, BigIntImpl bigInt)
{
VM& vm = globalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
ASSERT(n != 0);
ASSERT(bigInt.length() > n / digitBits);
int32_t neededDigits = (n + (digitBits - 1)) / digitBits;
ASSERT(neededDigits <= static_cast<int32_t>(bigInt.length()));
JSBigInt* result = createWithLength(globalObject, neededDigits);
RETURN_IF_EXCEPTION(scope, nullptr);
ASSERT(result);
// Copy all digits except the MSD.
int32_t last = neededDigits - 1;
for (int32_t i = 0; i < last; i++)
result->setDigit(i, bigInt.digit(i));
// The MSD might contain extra bits that we don't want.
Digit msd = bigInt.digit(last);
if (n % digitBits != 0) {
int32_t drop = digitBits - (n % digitBits);
msd = (msd << drop) >> drop;
}
result->setDigit(last, msd);
result->setSign(bigInt.sign());
RELEASE_AND_RETURN(scope, result->rightTrim(globalObject));
}
// Subtracts the least significant n bits of abs(bigInt) from 2^n.
template <typename BigIntImpl>
JSBigInt::ImplResult JSBigInt::truncateAndSubFromPowerOfTwo(JSGlobalObject* globalObject, int32_t n, BigIntImpl bigInt, bool resultSign)
{
VM& vm = globalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
ASSERT(n != 0);
ASSERT(n <= static_cast<int32_t>(maxLengthBits));
int32_t neededDigits = (n + (digitBits - 1)) / digitBits;
ASSERT(neededDigits <= static_cast<int32_t>(maxLength)); // Follows from n <= maxLengthBits.
JSBigInt* result = createWithLength(globalObject, neededDigits);
RETURN_IF_EXCEPTION(scope, nullptr);
ASSERT(result);
// Process all digits except the MSD.
int32_t i = 0;
int32_t last = neededDigits - 1;
int32_t length = bigInt.length();
Digit borrow = 0;
// Take digits from bigInt unless its length is exhausted.
int32_t limit = std::min(last, length);
for (; i < limit; i++) {
Digit newBorrow = 0;
Digit difference = digitSub(0, bigInt.digit(i), newBorrow);
difference = digitSub(difference, borrow, newBorrow);
result->setDigit(i, difference);
borrow = newBorrow;
}
// Then simulate leading zeroes in {bigInt} as needed.
for (; i < last; i++) {
Digit newBorrow = 0;
Digit difference = digitSub(0, borrow, newBorrow);
result->setDigit(i, difference);
borrow = newBorrow;
}
// The MSD might contain extra bits that we don't want.
Digit msd = last < length ? bigInt.digit(last) : 0;
int32_t msdBitsConsumed = n % digitBits;
Digit resultMSD;
if (msdBitsConsumed == 0) {
Digit newBorrow = 0;
resultMSD = digitSub(0, msd, newBorrow);
resultMSD = digitSub(resultMSD, borrow, newBorrow);
} else {
int32_t drop = digitBits - msdBitsConsumed;
msd = (msd << drop) >> drop;
Digit minuendMSD = static_cast<Digit>(1) << (digitBits - drop);
Digit newBorrow = 0;
resultMSD = digitSub(minuendMSD, msd, newBorrow);
resultMSD = digitSub(resultMSD, borrow, newBorrow);
ASSERT(newBorrow == 0); // result < 2^n.
// If all subtracted bits were zero, we have to get rid of the
// materialized minuendMSD again.
resultMSD &= (minuendMSD - 1);
}
result->setDigit(last, resultMSD);
result->setSign(resultSign);
RELEASE_AND_RETURN(scope, result->rightTrim(globalObject));
}
JSValue JSBigInt::asIntN(JSGlobalObject* globalObject, uint64_t n, JSBigInt* bigInt)
{
return tryConvertToBigInt32(asIntNImpl(globalObject, n, HeapBigIntImpl { bigInt }));
}
JSValue JSBigInt::asUintN(JSGlobalObject* globalObject, uint64_t n, JSBigInt* bigInt)
{
return tryConvertToBigInt32(asUintNImpl(globalObject, n, HeapBigIntImpl { bigInt }));
}
#if USE(BIGINT32)
JSValue JSBigInt::asIntN(JSGlobalObject* globalObject, uint64_t n, int32_t bigInt)
{
return tryConvertToBigInt32(asIntNImpl(globalObject, n, Int32BigIntImpl { bigInt }));
}
JSValue JSBigInt::asUintN(JSGlobalObject* globalObject, uint64_t n, int32_t bigInt)
{
return tryConvertToBigInt32(asUintNImpl(globalObject, n, Int32BigIntImpl { bigInt }));
}
#endif
uint64_t JSBigInt::toBigUInt64Heap(JSBigInt* bigInt)
{
auto length = bigInt->length();
if (!length)
return 0;
uint64_t value = 0;
if constexpr (sizeof(Digit) == 4) {
value = static_cast<uint64_t>(bigInt->digit(0));
if (length > 1)
value |= static_cast<uint64_t>(bigInt->digit(1)) << 32;
} else {
ASSERT(sizeof(Digit) == 8);
value = bigInt->digit(0);
}
if (!bigInt->sign())
return value;
return ~(value - 1); // To avoid undefined behavior, we compute two's compliment by hand in C while this is simply `-value`.
}
static ALWAYS_INLINE unsigned computeHash(JSBigInt::Digit* digits, unsigned length, bool sign)
{
Hasher hasher;
WTF::add(hasher, sign);
for (unsigned index = 0; index < length; ++index)
WTF::add(hasher, digits[index]);
return hasher.hash();
}
std::optional<unsigned> JSBigInt::concurrentHash()
{
// FIXME: Implement JSBigInt::concurrentHash by inserting right store barriers.
// https://bugs.webkit.org/show_bug.cgi?id=216801
return std::nullopt;
}
unsigned JSBigInt::hashSlow()
{
ASSERT(!m_hash);
m_hash = computeHash(dataStorage(), length(), m_sign);
return m_hash;
}
} // namespace JSC
WTF_ALLOW_UNSAFE_BUFFER_USAGE_END
|