/usr/share/picolisp/src64/big.l is in picolisp 17.12+20180218-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | # 15jul17abu
# (c) Software Lab. Alexander Burger
### Destructive primitives ###
# Remove leading zeroes
(code 'zapZeroA_A 0)
push A # Save number
ld C S # Short-tail in C
ld E C # Null-tail in E
do
cnt (A BIG) # Last cell?
while z # No
null (A DIG) # Null digit?
if nz # No
ld E C # New null-tail
end
lea C (A BIG) # New short-tail
ld A (C) # Next cell
loop
cmp (A BIG) ZERO # Trailing short zero?
if eq # Yes
ld A (A DIG)
null A # Null digit?
if nz # No
test A (hex "F000000000000000") # Fit in short number?
if z # Yes
shl A 4 # Make short number
or A CNT
ld (C) A # Store in short-tail
end
else
ld A ((E) DIG) # Digit in null-tail
test A (hex "F000000000000000") # Fit in short number?
if nz # No
ld ((E) BIG) ZERO # Trim null-tail
else
shl A 4 # Make short number
or A CNT
ld (E) A # Store in null-tail
end
end
end
pop A # Result
ret
# Multiply (unsigned) number by 2
(code 'twiceA_A 0)
cnt A # A short?
if nz # Yes
xor A 3 # Prepare tag bit
shl A 1 # Shift left
jnx Ret # Done
rxr A 1 # Else normalize
shr A 3
jmp boxNumA_A # Return bignum
end
: twiceBigA_A
push A # Save bignum
ld C (A DIG) # Lowest digit
shl C 1 # Shift left
do
push x # Save x-bit
ld (A DIG) C # Store digit
ld E (A BIG) # Next cell
cnt E # End of bignum?
while z # No
ld A E
ld C (A DIG) # Next digit
pop x
rxl C 1 # Rotate left
loop
shr E 4 # Normalize
pop x
rxl E 1 # Rotate left
test E (hex "F000000000000000") # Fit in short number?
if z # Yes
shl E 4 # Make short number
or E CNT
else
call boxNumE_E # New cell
end
ld (A BIG) E # Store in final cell
pop A # Return bignum
ret
# Divide (unsigned) number by 2
(code 'halfA_A 0)
cnt A # A short?
if nz # Yes
shr A 1 # Shift right
off A 9 # Clear lowest bit and tag
or A CNT # Make short number
ret
end
ld C (A DIG) # Lowest digit
ld E (A BIG) # Next cell
cnt E # Any?
if nz # No
shr E 5 # Normalize and shift right
if nz # Non-empty
rxr C 1 # Rotate right
else
rxr C 1 # Rotate right
test C (hex "F000000000000000") # Fit in short number?
if z # Yes
shl C 4 # Return short number
or C CNT
ld A C
ret
end
end
ld (A DIG) C # Store lowest digit
shl E 4 # Make short number
or E CNT
ld (A BIG) E # Store in the cell
ret
end
push A # Save bignum
do
shr (E DIG) 1 # Shift bit
rxr C 1 # Rotate right with x-bit
shl (E DIG) 1
ld (A DIG) C # Store digit
ld C (E BIG) # More cells?
cnt C
while z # Yes
ld A E # Advance pointers
ld E C
ld C (A DIG) # Next digit
loop
shr C 5 # Normalize and shift right
if nz # Non-empty
rxr (E DIG) 1 # Shift previous digit
shl C 4 # Make short number
or C CNT
else
ld C (E DIG) # Shift previous digit
rxr C 1
test C (hex "F000000000000000") # Fit in short number?
if z # Yes
shl C 4 # Make short number
or C CNT
ld (A BIG) C
pop A # Return bignum
ret
end
ld (E DIG) C
ld C ZERO
end
ld (E BIG) C # Store in the cell
pop A # Return bignum
ret
# Multiply (unsigned) number by 10
(code 'tenfoldA_A 0)
cnt A # A short?
if nz # Yes
shr A 4 # Normalize
mul 10 # Multiply by 10
test A (hex "F000000000000000") # Fit in short number?
jnz boxNumA_A # No: Return bignum
shl A 4 # Make short number
or A CNT
ret
end
push X
push A # Save bignum
ld X A # Bignum in X
ld A (X DIG) # Multiply lowest digit by 10
mul 10
do
ld (X DIG) A # Store lower word
ld E C # Keep upper word in E
ld A (X BIG) # Next cell
cnt A # End of bignum?
while z # No
ld X A
ld A (X DIG) # Next digit
mul 10 # Multiply by 10
add D E # Add previous upper word
loop
shr A 4 # Normalize
mul 10 # Multiply by 10
add A E # Add previous upper word
test A (hex "F000000000000000") # Fit in short number?
if z # Yes
shl A 4 # Make short number
or A CNT
else
call boxNumA_A # Return bignum
end
ld (X BIG) A # Store in final cell
pop A # Return bignum
pop X
ret
### Non-destructive primitives ###
# Multiply (unsigned) number by 2
(code 'shluA_A 0)
cnt A # A short?
if nz # Yes
xor A 3 # Prepare tag bit
shl A 1 # Shift left
jnx Ret # Done
rxr A 1 # Else normalize
shr A 3
jmp boxNumA_A # Return bignum
end
call boxNum_E # Build new head
ld (E DIG) (A DIG) # Lowest digit
link
push E # <L I> Result
link
shl (E DIG) 1 # Shift left
push x # Save x-bit
do
ld A (A BIG) # Next cell
cnt A # End of bignum?
while z # No
call boxNum_C # Build next cell
ld (E BIG) C
ld E (A DIG) # Next digit
pop x
rxl E 1 # Rotate left
push x # Save x-bit
ld (C DIG) E
ld E C
loop
shr A 4 # Normalize
pop x
rxl A 1 # Rotate left
test A (hex "F000000000000000") # Fit in short number?
if z # Yes
shl A 4 # Make short number
or A CNT
else
call boxNumA_A # New cell
end
ld (E BIG) A # Store in final cell
ld A (L I) # Return bignum
drop
ret
# Divide (unsigned) number by 2
(code 'shruA_A 0)
cnt A # A short?
if nz # Yes
shr A 1 # Shift right
off A 9 # Clear lowest bit and tag
or A CNT # Make short number
ret
end
ld E (A BIG) # Next cell
cnt E # Any?
if nz # No
ld C (A DIG) # Lowest digit
shr E 5 # Normalize and shift right
if nz # Non-empty
rxr C 1 # Rotate right
else
rxr C 1 # Rotate right
test C (hex "F000000000000000") # Fit in short number?
if z # Yes
shl C 4 # Return short number
or C CNT
ld A C
ret
end
end
shl E 4 # Make short number
or E CNT
jmp consNumCE_A # Return bignum
end
call boxNum_C # Build new head
ld (C DIG) (A DIG) # Lowest digit
link
push C # <L I> Result
link
do
ld A (E DIG) # Shift bit
shr A 1
rxr (C DIG) 1 # Rotate right with x-bit
cnt (E BIG) # More cells?
while z # Yes
call boxNum_A # Build next digit
ld (A DIG) (E DIG)
ld (C BIG) A
ld E (E BIG) # Advance pointers
ld C A
loop
ld A (E BIG) # Final short number
shr A 5 # Normalize and shift right
if nz # Non-empty
ld E (E DIG) # Shift previous digit
rxr E 1
shl A 4 # Make short number
or A CNT
call consNumEA_E # Last cell
ld (C BIG) E # Store in the cell
else
ld E (E DIG) # Shift previous digit
rxr E 1
test E (hex "F000000000000000") # Fit in short number?
if z # Yes
shl E 4 # Make short number
or E CNT
ld (C BIG) E
ld A (L I) # Return bignum
drop
ret
end
call boxNum_A # New cell
ld (A DIG) E
ld (C BIG) A
end
ld A (L I) # Return bignum
drop
ret
# Bitwise AND of two (unsigned) numbers
(code 'anduAE_A 0)
cnt A # A short?
if nz # Yes
cnt E # E also short?
if z # No
ld E (E DIG) # Get digit
shl E 4 # Make short number
or E CNT
end
and A E # Return short number
ret
end
# A is big
cnt E # E short?
if nz # Yes
ld A (A DIG) # Get digit
shl A 4 # Make short number
or A CNT
and A E # Return short number
ret
end
# Both are big
push X
link
push ZERO # <L I> Result
link
ld C (A DIG) # AND first digits
and C (E DIG)
call boxNum_X # Make bignum
ld (X DIG) C
ld (L I) X # Init result
do
ld A (A BIG) # Get tails
ld E (E BIG)
cnt A # End of A?
if nz # Yes
cnt E # Also end of E?
if z # No
ld E (E DIG) # Get digit
shl E 4 # Make short number
or E CNT
end
and A E # Concat short
ld (X BIG) A
ld A (L I) # Return bignum
drop
pop X
jmp zapZeroA_A # Remove leading zeroes
end
cnt E # End of E?
if nz # Yes
ld A (A DIG) # Get digit
shl A 4 # Make short number
or A CNT
and A E # Concat short
ld (X BIG) A
ld A (L I) # Return bignum
drop
pop X
jmp zapZeroA_A # Remove leading zeroes
end
ld C (A DIG) # AND digits
and C (E DIG)
call consNumCE_C # New bignum cell
ld (X BIG) C # Concat to result
ld X C
loop
# Bitwise OR of two (unsigned) numbers
(code 'oruAE_A 0)
cnt A # A short?
if nz # Yes
cnt E # E also short?
if nz # Yes
or A E # Return short number
ret
end
shr A 4 # Normalize
or A (E DIG) # OR digit
ld E (E BIG) # Rest of E
jmp consNumAE_A # Append rest
end
# A is big
cnt E # E short?
if nz # Yes
shr E 4 # Normalize
or E (A DIG) # OR digit
ld A (A BIG) # Rest of A
jmp consNumEA_A # Append rest
end
# Both are big
push X
link
push ZERO # <L I> Result
link
ld C (A DIG) # OR first digits
or C (E DIG)
call boxNum_X # Make bignum
ld (X DIG) C
ld (L I) X # Init result
do
ld A (A BIG) # Get tails
ld E (E BIG)
cnt A # End of A?
if nz # Yes
cnt E # Also end of E?
if nz # Yes
or A E # Concat short number
else
shr A 4 # Normalize
or A (E DIG) # OR digit
ld E (E BIG) # Rest of E
call consNumAE_A # Append rest
end
ld (X BIG) A
ld A (L I) # Return bignum
drop
pop X
ret
end
cnt E # End of E?
if nz # Yes
shr E 4 # Normalize
or E (A DIG) # OR digit
ld A (A BIG) # Rest of A
call consNumEA_A # Append rest
ld (X BIG) A
ld A (L I) # Return bignum
drop
pop X
ret
end
ld C (A DIG) # OR digits
or C (E DIG)
call consNumCE_C # New bignum cell
ld (X BIG) C # Concat to result
ld X C
loop
# Bitwise XOR of two (unsigned) numbers
(code 'xoruAE_A 0)
cnt A # A short?
if nz # Yes
cnt E # E also short?
if nz # Yes
xor A E # Return short number
or A CNT
ret
end
shr A 4 # Normalize
xor A (E DIG) # XOR digit
ld E (E BIG) # Rest of E
call consNumAE_A # Append rest
jmp zapZeroA_A # Remove leading zeroes
end
# A is big
cnt E # E short?
if nz # Yes
shr E 4 # Normalize
xor E (A DIG) # XOR digit
ld A (A BIG) # Rest of A
call consNumEA_A # Append rest
jmp zapZeroA_A # Remove leading zeroes
end
# Both are big
push X
link
push ZERO # <L I> Result
link
ld C (A DIG) # XOR first digits
xor C (E DIG)
call boxNum_X # Make bignum
ld (X DIG) C
ld (L I) X # Init result
do
ld A (A BIG) # Get tails
ld E (E BIG)
cnt A # End of A?
if nz # Yes
cnt E # Also end of E?
if nz # Yes
xor A E # Concat short number
or A CNT
else
shr A 4 # Normalize
xor A (E DIG) # XOR digit
ld E (E BIG) # Rest of E
call consNumAE_A # Append rest
end
ld (X BIG) A
ld A (L I) # Return bignum
drop
pop X
jmp zapZeroA_A # Remove leading zeroes
end
cnt E # End of E?
if nz # Yes
shr E 4 # Normalize
xor E (A DIG) # XOR digit
ld A (A BIG) # Rest of A
call consNumEA_A # Append rest
ld (X BIG) A
ld A (L I) # Return bignum
drop
pop X
jmp zapZeroA_A # Remove leading zeroes
end
ld C (A DIG) # XOR digits
xor C (E DIG)
call consNumCE_C # New bignum cell
ld (X BIG) C # Concat to result
ld X C
loop
# Add two (unsigned) numbers
(code 'adduAE_A 0)
cnt A # A short?
if nz # Yes
cnt E # E also short?
jz 10 # No: Jump
off E CNT # Else clear tag
add A E # Add short numbers
jnc Ret # Done
addc A 0 # Get top bit
ror A 1
shr A 3 # Normalize
jmp boxNumA_A # Return bignum
end
# A is big
cnt E # E short?
if nz # Yes
xchg A E # Exchange args
10 shr A 4 # Normalize short
add A (E DIG) # Add first digit
ld E (E BIG) # Tail in E
jnc consNumAE_A # Cons new cell if no carry
call consNumAE_A # Else build new head
link
push A # <L I> Result
link
do
cnt E # Short number?
if nz # Yes
add E (hex "10") # Add carry
if nc # No further carry
ld (A BIG) E # Append it
else # Again carry
addc E 0 # Get top bit
ror E 1
shr E 3 # Normalize
call boxNum_C # New cell
ld (C DIG) E
ld (A BIG) C # Append it
end
ld A (L I) # Return bignum
drop
ret
end
ld C (E DIG) # Next digit
ld E (E BIG)
add C 1 # Add carry
if nc # None
call consNumCE_E # New last cell
ld (A BIG) E
ld A (L I) # Return bignum
drop
ret
end
call consNumCE_C # New cell
ld (A BIG) C # Append it
ld A C # Tail of result
loop
end
# Both are big
push X
link
push ZERO # <L I> Result
link
ld C (A DIG) # Add first digits
add C (E DIG)
push zsc # Save carry
call boxNum_X # Make bignum
ld (X DIG) C
ld (L I) X # Init result
do
ld A (A BIG) # Get tails
ld E (E BIG)
cnt A # End of A?
if nz # Yes
cnt E # Also end of E?
jz 20 # No: Jump
shr A 4 # Normalize A
shr E 4 # Normalize E
pop zsc
addc A E # Add final shorts with carry
shl A 4
if nx
or A CNT # Make short number
else # Again carry
add A 1 # Get top bit
ror A 1
shr A 3 # Normalize
call boxNumA_A # Make bignum
end
ld (X BIG) A
ld A (L I) # Return bignum
drop
pop X
ret
end
cnt E # End of E?
if nz # Yes
xchg A E # Exchange args
20 shr A 4 # Normalize A
pop zsc
addc A (E DIG) # Add next digit with carry
do
ld E (E BIG)
if nc # No carry
call consNumAE_A # Append rest
ld (X BIG) A
ld A (L I) # Return bignum
drop
pop X
ret
end
call consNumAE_A # New cell
ld (X BIG) A # Concat to result
ld X A # Pointer to last cell
cnt E # End of E?
if nz # Yes
add E (hex "10") # Add carry
if nc # No further carry
ld (X BIG) E # Append it
else # Again carry
addc E 0 # Get top bit
ror E 1
shr E 3 # Normalize
call boxNum_C # New cell
ld (C DIG) E
ld (X BIG) C # Append it
end
ld A (L I) # Return bignum
drop
pop X
ret
end
ld A (E DIG) # Add carry to next digit
add A 1
loop
end
ld C (A DIG) # Add digits
pop zsc
addc C (E DIG)
push zsc
call consNumCE_C # New bignum cell
ld (X BIG) C # Concat to result
ld X C
loop
# Subtract two (unsigned) numbers
(code 'subuAE_A 0)
cnt A # A short?
if nz # Yes
cnt E # E also short?
if nz # Yes
off E CNT # Clear tag
sub A E # Subtract short numbers
jnb Ret # Done
xor A -16 # 2-complement
add A (hex "18")
ret
end
xchg A E # Exchange args
call subBigShort # Subtract short from big
cmp A ZERO # Zero?
if ne # No
or A SIGN # Set negative
end
ret
end
# A is big
cnt E # E short?
if nz # Yes
: subBigShort
shr E 4 # Normalize short
ld C (A DIG)
sub C E # Subtract from first digit
ld E (A BIG) # Tail in E
if nb # No borrow
cmp E ZERO # Leading zero?
jne consNumCE_A # No: Cons new cell
test C (hex "F000000000000000") # Fit in short number?
jnz consNumCE_A # No: Cons new cell
ld A C # Get digit
shl A 4 # Make short number
or A CNT
ret
end
call consNumCE_A # Else build new head
link
push A # <L I> Result
link
do
cnt E # Short number?
if nz # Yes
sub E (hex "10") # Subtract borrow
if b # Again borrow: Must be the first pass
ld A C # C still has lowest digit
neg A # Negate
shl A 4
or A (| SIGN CNT) # Make short negative number
drop
ret
end
ld (A BIG) E # Append it
ld A (L I) # Return bignum
drop
jmp zapZeroA_A # Remove leading zeroes
end
ld C (E DIG) # Next digit
ld E (E BIG)
sub C 1 # Subtract borrow
if nb # None
call consNumCE_E # New last cell
ld (A BIG) E # Append it
ld A (L I) # Return bignum
drop
jmp zapZeroA_A # Remove leading zeroes
end
call consNumCE_C # New cell
ld (A BIG) C # Append it
ld A C # Tail of result
loop
end
# Both are big
push X
link
push ZERO # <L I> Result
link
ld C (A DIG) # Subtract first digits
sub C (E DIG)
push zsc # Save borrow
ld A (A BIG) # Get tail
call consNumCA_C # First bignum cell
ld (L I) C # Init result
do
ld X C # Keep last cell in X
ld E (E BIG) # Get tail
cnt E # End of E?
if nz # Yes
shr E 4 # Normalize E
do
cnt A # Also end of A?
while z # No
ld C (A DIG) # Subtract final digit with borrow
ld A (A BIG) # Next cell
pop zsc
subb C E # Borrow again?
if nb # No
call consNumCA_C # Final new bignum tail
ld (X BIG) C # Concat to result
20 ld A (L I) # Return bignum
drop
pop X
jmp zapZeroA_A # Remove leading zeroes
end
push zsc # Save borrow
call consNumCA_C # New bignum tail
ld (X BIG) C # Concat to result
ld X C # Keep last cell
ld E 0
loop
shr A 4 # Normalize A
break T
end
cnt A # End of A?
if nz # Yes
shr A 4 # Normalize A
do
pop zsc
subb A (E DIG) # Subtract next digit with borrow
push zsc
call boxNum_C # New bignum tail
ld (C DIG) A
ld (X BIG) C # Concat to result
ld X C # Keep last cell
ld E (E BIG) # Next cell
ld A 0
cnt E # Also end of E?
until nz # Yes
shr E 4 # Normalize E
break T
end
ld C (A DIG) # Subtract digits
pop zsc
subb C (E DIG)
push zsc # Save borrow
ld A (A BIG)
call consNumCA_C # New bignum cell
ld (X BIG) C # Concat to result
loop
pop zsc
subb A E # Subtract final shorts with borrow
push zsc # Save borrow
shl A 4
or A CNT # Make short number
ld (X BIG) A
pop zsc # Borrow?
jnb 20 # No
ld A (L I) # Get result
ld E A # 2-complement
do
not (E DIG) # Invert
ld C (E BIG) # Next digit
cnt C # Done?
while z # No
ld E C # Next digit
loop
xor C -16 # Invert final short
ld (E BIG) C
ld E A # Result again
do
add (E DIG) 1 # Increment
jnc 90 # Skip if no carry
ld C (E BIG) # Next digit
cnt C # Done?
while z # No
ld E C # Next digit
loop
add C (hex "10") # Increment final short
ld (E BIG) C
90 drop
pop X
call zapZeroA_A # Remove leading zeroes
or A SIGN # Set negative
ret
# Multiply two (unsigned) numbers
(code 'muluAE_A 0)
cnt A # A short?
if nz # Yes
cmp A ZERO # Multiply with zero?
jeq ret # Yes: Return zero
shr A 4 # Normalize
cnt E # E also short?
if nz # Yes
xchg A E
shr A 4 # Normalize
mul E # Multiply
null C # Only lower word?
if z # Yes
test A (hex "F000000000000000") # Fit in short number?
if z # Yes
shl A 4 # Make short number
or A CNT
ret
end
end
shl C 4 # Make short number
or C CNT
jmp consNumAC_A # Return bignum
end
10 push X
push Y
push Z
ld Y A # Save digit in Y
mul (E DIG) # Multiply lowest digit
call boxNum_X # First cell
ld (X DIG) A
link
push X # <L I> Safe
link
ld Z C # Keep upper word in Z
do
ld E (E BIG)
cnt E # End of bignum?
while z # No
ld A (E DIG) # Get next digit
mul Y # Multiply digit
add D Z # Add previous upper word
ld Z C
call boxNum_C # Next cell
ld (C DIG) A
ld (X BIG) C
ld X C
loop
ld A Y # Retrieve digit
shr E 4 # Normalize
mul E # Multiply
add D Z # Add previous upper word
if z # Only lower word
test A (hex "F000000000000000") # Fit in short number?
if z # Yes
shl A 4 # Make short number
or A CNT
20 ld (X BIG) A # Store in final cell
ld A (L I) # Return bignum
drop
pop Z
pop Y
pop X
ret
end
end
shl C 4 # Make short number
or C CNT
call consNumAC_A # Return bignum
jmp 20
end
# A is big
cnt E # E short?
if nz # Yes
xchg A E # Exchange args
cmp A ZERO # Multiply with zero?
jeq ret # Yes: Return zero
shr A 4 # Normalize
jmp 10
end
# Both are big
push X
push Y
push Z
ld Y A # Arg1 in Y
ld Z E # Arg2 in Z
call boxNum_X # Zero bignum
ld (X DIG) 0
link
push X # <L I> Safe
link
push X # <L -I> Safe index
push Y # <L -II> Arg1 index
do
ld A (Y DIG) # Multiply digits
mul (Z DIG)
add D (X DIG) # Add lower word to safe
do
ld (X DIG) A # Store lower word
ld E C # Keep upper word in E
ld A (X BIG) # Next safe cell
cnt A # End of safe?
if nz # Yes
call boxNum_A # Extend safe
ld (A DIG) 0
ld (X BIG) A
end
ld X A
ld Y (Y BIG) # Next cell of Arg1
cnt Y # End of bignum?
while z # No
ld A (Y DIG) # Multiply digits
mul (Z DIG)
add D (X DIG) # Add safe
addc D E # plus carry
loop
ld A Y # Final short number
shr A 4 # Normalize
mul (Z DIG)
add D (X DIG) # Add safe
addc D E # plus carry
ld (X DIG) A
if nz # Uppper word
ld A (X BIG) # Next safe cell
cnt A # End of safe?
if nz # Yes
call boxNum_A # Extend safe
ld (A DIG) 0
ld (X BIG) A
end
ld (A DIG) C # Store uppper word
end
ld Y (L -II) # Get Arg1 index
ld X ((L -I) BIG) # Advance safe index
ld (L -I) X
ld Z (Z BIG) # Next cell of Arg2
cnt Z # End of bignum?
until nz # Yes
ld A Z
shr A 4 # Normalize
ld Z A
mul (Y DIG) # Multiply digit
add D (X DIG) # Add lower word to safe
do
ld (X DIG) A # Store lower word
ld E C # Keep upper word in E
ld A (X BIG) # Next safe cell
cnt A # End of safe?
if nz # Yes
call boxNum_A # Extend safe
ld (A DIG) 0
ld (X BIG) A
end
ld X A
ld Y (Y BIG) # Next cell of Arg1
cnt Y # End of bignum?
while z # No
ld A (Y DIG) # Multiply digit
mul Z
add D (X DIG) # Add safe
addc D E # plus carry
loop
ld A Y # Final short number
shr A 4 # Normalize
mul Z # Multiply digit
add D (X DIG) # Add safe
addc D E # plus carry
ld (X DIG) A
if nz # Uppper word
ld A (X BIG) # Next safe cell
cnt A # End of safe?
if nz # Yes
call boxNum_A # Extend safe
ld (A DIG) 0
ld (X BIG) A
end
ld (A DIG) C # Store uppper word
end
ld A (L I) # Return bignum
drop
pop Z
pop Y
pop X
jmp zapZeroA_A # Remove leading zeroes
# Divide two (unsigned) numbers (Knuth Vol.2, p.257)
(code 'divuAE_A 0)
cnt A # A short?
if nz # Yes
cnt E # E also short?
if nz # Yes
shr A 4 # Normalize A
ld C 0
shr E 4 # Normalize E
div E # Divide
shl A 4 # Make short number
or A CNT # Quotient
ret
end
ld A ZERO # Else return zero
ret
end
push X
push Y
push Z
link
push ZERO # <L III> Quotient
push A # <L II> Dividend 'u'
push E # <L I> Divisor 'v'
link
ld E (A DIG) # Copy dividend
call boxNumE_E
ld (L II) E # Save new 'u'
ld X 0 # Calculate 'm'
do
ld A (A BIG) # Next cell of 'u'
cnt A # Last one?
while z # No
call boxNum_C # Copy next digit
ld (C DIG) (A DIG)
ld (E BIG) C
ld E C
inc X # Increment 'm'
loop
cmp A ZERO # Trailing short zero?
if ne # No
shr A 4 # Normalize
call boxNum_C # Append in new cell
ld (C DIG) A
ld (E BIG) C
ld E C
inc X # Increment 'm'
end
ld Z E # Keep last cell in Z
push X # <L -I> 'm'
ld Y 0 # Last cell
ld C 0 # Calculate 'n'
ld A (L I) # Get divisor
cnt A # Short?
if nz # Yes
shr A 4 # Normalize
call boxNumA_A # Make big
ld (L I) A # Save new 'v'
ld X A # Keep in X
inc C # 'n' = 1
else
call boxNum_X # Copy divisor
ld (X DIG) (A DIG)
ld (L I) X # Save new 'v'
do
inc C # Increment 'n'
ld A (A BIG) # Next cell of 'v'
cnt A # Last one?
while z # No
ld E (A DIG) # Copy next digit
call boxNumE_E
ld (X BIG) E # Append to 'v'
ld Y X # Keep last cell
ld X E
dec (L -I) # Decrement 'm'
loop
cmp A ZERO # Trailing short zero?
if ne # No
shr A 4 # Normalize
call boxNumA_A # Append in new cell
ld (X BIG) A # Append to 'v'
ld Y X # Set last cell
ld X A
dec (L -I) # Decrement 'm'
inc C # Increment 'n'
end
null (L -I) # 'm' negative?
js 90 # Yes
end
push C # <L -II> 'n'
ld A 0 # Append additional cell
call boxNumA_A
ld (Z BIG) A
ld Z 0 # Calculate 'd'
do
null (X DIG) # Max left position?
while ns # No
ld A (L II) # Shift left 'u'
call twiceBigA_A
ld A (L I) # and 'v'
call twiceBigA_A
inc Z # Increment 'd'
loop
push Z # <L -III> 'd'
push (X DIG) # <L -IV> 'v1'
null Y # Last cell?
if nz # Yes
ld Y (Y DIG) # Yes: Get digit
end
push Y # <L -V> Last cell 'v2'
push 0 # <S> tmp
do
ld C (L -I) # Get 'm'
ld X (L II) # and 'u'
do
sub C 1
while ge
ld X (X BIG) # Index X -> u
loop
ld E (L -II) # Get 'n' in E
ld Y X
ld C 0 # 'u1' in C
ld A 0 # 'u2' in A
do
ld (S) A # Save 'u3' im tmp
ld A C # Shift words
ld C (Y DIG)
ld Y (Y BIG)
sub E 1
until lt
ld Z C # Keep 'r' = 't' in Z,Y
ld Y A
cmp C (L -IV) # 'u1' = 'v1'?
if ne # No
div (L -IV) # 'q' = 't' / 'v1'
else
ld A -1 # 'q' = MAX
end
ld E A # Save 'q' in E
mul (L -IV) # 'q' * 'v1'
sub Y A # Subtract from 'r'
subb Z C
do
null Z # 'r' <= MAX?
while z # Yes
ld A E # 'q' * 'v2'
mul (L -V)
cmp C Y # > lo(r), 'u3'?
while ge
if eq
cmp A (S) # 'u3' in tmp
break le
end
dec E # Yes: Decrement 'q'
add Y (L -IV) # Increment 'r' by 'v1'
addc Z 0
loop
ld (S) E # Save 'q' in tmp
ld Z X # Get 'x'
ld Y (L I) # 'v'
ld A E # and 'q'
mul (Y DIG) # Multiply lowest digit
sub (Z DIG) A # Subtract from 'x'
ld E 0 # Borrow in E
subb E C
neg E
do
ld Y (Y BIG) # More in 'v'?
cnt Y
while z # Yes
ld Z (Z BIG) # Next 'x'
ld A (S) # Multiply with 'q' in tmp
mul (Y DIG) # 't' in D
sub (Z DIG) E # Subtract borrow
subb E E # New borrow
sub (Z DIG) A # Subtract lo(t)
subb E C # Adjust borrow plus hi(t)
neg E
loop
null E # Borrow?
if nz # Yes
ld Z (Z BIG) # Next 'x'
sub (Z DIG) E # Subtract borrow
if b
dec (S) # Decrement 'q'
null (L -I) # 'm' ?
if nz # Yes
ld Y (L I) # Get 'v'
add (X DIG) (Y DIG) # 'x' += 'v'
push zsc # Save carry
do
ld X (X BIG) # More?
ld Y (Y BIG)
cnt Y
while z # Yes
pop zsc # Get carry
addc (X DIG) (Y DIG) # Add digits
push zsc
loop
pop zsc # Final carry
addc (X DIG) 0
end
end
end
ld A (S) # Get 'q'
ld C (L III) # Quotient so far
call consNumAC_A # Prepend 'q'
ld (L III) A # Store result
sub (L -I) 1 # Decrement 'm'
until lt
ld A (L III) # Return quotient in A
call zapZeroA_A
80 drop # Done
pop Z
pop Y
pop X
ret
90 ld A ZERO # Dividend smaller than divisor
jmp 80 # Return quotient 0
# Remainder of two (unsigned) numbers
(code 'remuAE_A 0)
cnt A # A short?
if nz # Yes
cnt E # E also short?
if nz # Yes
shr A 4 # Normalize A
ld C 0
shr E 4 # Normalize E
div E # Divide
ld A C # Get remainder
shl A 4 # Make short number
or A CNT # Quotient
ret
end
ret # Remainder is in A
end
push X
push Y
push Z
link
push ZERO # <L III> Quotient
push A # <L II> Dividend 'u'
push E # <L I> Divisor 'v'
link
ld E (A DIG) # Copy dividend
call boxNumE_E
ld (L II) E # Save new 'u'
ld X 0 # Calculate 'm'
do
ld A (A BIG) # Next cell of 'u'
cnt A # Last one?
while z # No
call boxNum_C # Copy next digit
ld (C DIG) (A DIG)
ld (E BIG) C
ld E C
inc X # Increment 'm'
loop
cmp A ZERO # Trailing short zero?
if ne # No
shr A 4 # Normalize
call boxNum_C # Append in new cell
ld (C DIG) A
ld (E BIG) C
ld E C
inc X # Increment 'm'
end
ld Z E # Keep last cell in Z
push X # <L -I> 'm'
ld Y 0 # Last cell
ld C 0 # Calculate 'n'
ld A (L I) # Get divisor
cnt A # Short?
if nz # Yes
shr A 4 # Normalize
call boxNumA_A # Make big
ld (L I) A # Save new 'v'
ld X A # Keep in X
inc C # 'n' = 1
else
call boxNum_X # Copy divisor
ld (X DIG) (A DIG)
ld (L I) X # Save new 'v'
do
inc C # Increment 'n'
ld A (A BIG) # Next cell of 'v'
cnt A # Last one?
while z # No
ld E (A DIG) # Copy next digit
call boxNumE_E
ld (X BIG) E # Append to 'v'
ld Y X # Keep last cell
ld X E
dec (L -I) # Decrement 'm'
loop
cmp A ZERO # Trailing short zero?
if ne # No
shr A 4 # Normalize
call boxNumA_A # Append in new cell
ld (X BIG) A # Append to 'v'
ld Y X # Set last cell
ld X A
dec (L -I) # Decrement 'm'
inc C # Increment 'n'
end
null (L -I) # 'm' negative?
js 90 # Yes
end
push C # <L -II> 'n'
ld A 0 # Append additional cell
call boxNumA_A
ld (Z BIG) A
ld Z 0 # Calculate 'd'
do
null (X DIG) # Max left position?
while ns # No
ld A (L II) # Shift left 'u'
call twiceBigA_A
ld A (L I) # and 'v'
call twiceBigA_A
inc Z # Increment 'd'
loop
push Z # <L -III> 'd'
push (X DIG) # <L -IV> 'v1'
null Y # Last cell?
if nz # Yes
ld Y (Y DIG) # Yes: Get digit
end
push Y # <L -V> Last cell 'v2'
push 0 # <S> tmp
do
ld C (L -I) # Get 'm'
ld X (L II) # and 'u'
do
sub C 1
while ge
ld X (X BIG) # Index X -> u
loop
ld E (L -II) # Get 'n' in E
ld Y X
ld C 0 # 'u1' in C
ld A 0 # 'u2' in A
do
ld (S) A # Save 'u3' im tmp
ld A C # Shift words
ld C (Y DIG)
ld Y (Y BIG)
sub E 1
until lt
ld Z C # Keep 'r' = 't' in Z,Y
ld Y A
cmp C (L -IV) # 'u1' = 'v1'?
if ne # No
div (L -IV) # 'q' = 't' / 'v1'
else
ld A -1 # 'q' = MAX
end
ld E A # Save 'q' in E
mul (L -IV) # 'q' * 'v1'
sub Y A # Subtract from 'r'
subb Z C
do
null Z # 'r' <= MAX?
while z # Yes
ld A E # 'q' * 'v2'
mul (L -V)
cmp C Y # > lo(r), 'u3'?
while ge
if eq
cmp A (S) # 'u3' in tmp
break le
end
dec E # Yes: Decrement 'q'
add Y (L -IV) # Increment 'r' by 'v1'
addc Z 0
loop
ld (S) E # Save 'q' in tmp
ld Z X # Get 'x'
ld Y (L I) # 'v'
ld A E # and 'q'
mul (Y DIG) # Multiply lowest digit
sub (Z DIG) A # Subtract from 'x'
ld E 0 # Borrow in E
subb E C
neg E
do
ld Y (Y BIG) # More in 'v'?
cnt Y
while z # Yes
ld Z (Z BIG) # Next 'x'
ld A (S) # Multiply with 'q' in tmp
mul (Y DIG) # 't' in D
sub (Z DIG) E # Subtract borrow
subb E E # New borrow
sub (Z DIG) A # Subtract lo(t)
subb E C # Adjust borrow plus hi(t)
neg E
loop
null E # Borrow?
if nz # Yes
ld Z (Z BIG) # Next 'x'
sub (Z DIG) E # Subtract borrow
if b
dec (S) # Decrement 'q'
ld Y (L I) # Get 'v'
add (X DIG) (Y DIG) # 'x' += 'v'
push zsc # Save carry
do
ld X (X BIG) # More?
ld Y (Y BIG)
cnt Y
while z # Yes
pop zsc # Get carry
addc (X DIG) (Y DIG) # Add digits
push zsc
loop
pop zsc # Final carry
addc (X DIG) 0
end
end
ld A (S) # Get 'q'
ld C (L III) # Quotient so far
call consNumAC_A # Prepend 'q'
ld (L III) A # Store result
sub (L -I) 1 # Decrement 'm'
until lt
ld A (L II) # Get remainder
call zapZeroA_A
do
null (L -III) # 'd'?
while nz # Yes
call halfA_A # Shift right (destructive)
dec (L -III) # Decrement 'd'
loop
80 drop # Done
pop Z
pop Y
pop X
ret
90 ld A (L II) # Dividend smaller than divisor, get remainder
call zapZeroA_A
jmp 80
# Increment a (signed) number
(code 'incE_A 0)
ld A ONE
test E SIGN # Positive?
jz adduAE_A # Increment
off E SIGN # Make positive
call subuAE_A # Subtract
cmp A ZERO # Zero?
if ne # No
or A SIGN # Negate again
end
ret
# Decrement a (signed) number
(code 'decE_A 0)
ld A ONE
test E SIGN # Positive?
if z # Yes
xchg A E
jmp subuAE_A # Decrement
end
off E SIGN # Make positive
call adduAE_A # Add
or A SIGN # Negate again
ret
# Add two (signed) numbers
(code 'addAE_A 0)
test A SIGN # Positive?
if z # Yes
test E SIGN # Arg also positive?
jz adduAE_A # Add [+ A E]
off E SIGN # [+ A -E]
jmp subuAE_A # Sub
end
# Result negatve
test E SIGN # Arg positive?
if z # [+ -A E]
off A SIGN
call subuAE_A # Sub
else # [+ -A -E]
off A SIGN
off E SIGN
call adduAE_A # Add
end
cmp A ZERO # Zero?
if ne # No
xor A SIGN # Negate
end
ret
# Subtract to (signed) numbers
(code 'subAE_A 0)
test A SIGN # Positive?
if z # Yes
test E SIGN # Arg also positive?
jz subuAE_A # Sub [- A E]
off E SIGN # [- A -E]
jmp adduAE_A # Add
end
# Result negatve
test E SIGN # Arg positive?
if z # [- -A E]
off A SIGN
call adduAE_A # Add
else # [- -A -E]
off A SIGN
off E SIGN
call subuAE_A # Sub
end
cmp A ZERO # Zero?
if ne # No
xor A SIGN # Negate
end
ret
### Comparisons ###
(code 'cmpNumAE_F 0)
test A SIGN # A positive?
if z # Yes
test E SIGN # E also positive?
jz cmpuAE_F # Yes [A E]
gt # [A -E]
ret
end
# A negative
test E SIGN # E positive?
if z # Yes
lt # nz [-A E]
ret
end
xchg A E # [-A -E]
off A SIGN
off E SIGN
# Compare two (unsigned) numbers
(code 'cmpuAE_F 0)
cnt A # A short?
if nz # Yes
cnt E # E also short?
if nz # Yes
cmp A E # F
ret
end
lt # nz (E is big)
ret
end
# A is big
cnt E # E short?
if nz # Yes
gt # (E is short)
ret
end
# Both are big
push X
push Y
ld X 0 # Clear reverse pointers
ld Y 0
do
ld C (A BIG) # Tails equal?
cmp C (E BIG)
if eq # Yes
do
ld C (A DIG) # Compare digits
cmp C (E DIG)
while eq
null X # End of reversed list?
if z # Yes
pop Y # eq
pop X
ret
end
ld C (X BIG) # Restore A
ld (X BIG) A
ld A X
ld X C
ld C (Y BIG) # Restore E
ld (Y BIG) E
ld E Y
ld Y C
loop
push zsc
break T
end
cnt C # End of A?
if nz # Yes
cnt (E BIG) # Also end of E?
if nz # Yes
cmp C (E BIG) # F
else
lt # nz (E is bigger)
end
push zsc
break T
end
cnt (E BIG) # End of E?
if nz # Yes
gt
push zsc
break T
end
ld (A BIG) X # Reverse A
ld X A
ld A C
ld C (E BIG) # Reverse E
ld (E BIG) Y
ld Y E
ld E C
loop
do
null X # Reversed?
while nz # Yes
ld C (X BIG) # Restore A
ld (X BIG) A
ld A X
ld X C
ld C (Y BIG) # Restore E
ld (Y BIG) E
ld E Y
ld Y C
loop
pop zsc # Return flags
pop Y
pop X
ret
### Conversions ###
# Make number from symbol
(code 'symToNumXA_FE 0)
link
push ZERO # <L I> Safe
link
push A # <L -I> Scale
push 0 # <L -II> Sign flag
push 0 # <L -III> Fraction flag
ld C 0
call symByteCX_FACX # Get first byte
jz 99 # None
do
cmp B 32 # Skip white space
while le
call symByteCX_FACX # Next byte
jz 99 # None
loop
cmp B (char "+") # Plus sign?
jz 10 # Yes
cmp B (char "-") # Minus sign?
if eq # Yes
or (L -II) 1 # Set Sign
10 call symByteCX_FACX # Next byte
jz 99 # None
end
sub A (char "0") # First digit
cmp A 10 # Too big?
jge 99 # Return NO
shl A 4 # Make short number
or A CNT
ld (L I) A # Save
do
call symCharCX_FACX # More?
while nz # Yes
test (L -III) 1 # Fraction?
if nz # Yes
null (L -I) # Scale?
if z # No
sub A (char "0") # Next digit
cmp A 10 # Too big?
jge 99 # Return NO
cmp A 5 # Round?
if ge # Yes
ld A ONE # Increment
ld E (L I)
push C
call adduAE_A
pop C
ld (L I) A
end
do
call symByteCX_FACX # More?
while nz # Yes
sub A (char "0") # Next digit
cmp A 10 # Too big?
jge 99 # Return NO
loop
break T
end
end
cmp A (Sep0) # Decimal separator?
if eq # Yes
test (L -III) 1 # Fraction?
jnz 99 # Return NO
or (L -III) 1 # Set Fraction
else
cmp A (Sep3) # Thousand separator?
if ne # No
sub A (char "0") # Next digit
cmp A 10 # Too big?
jge 99 # Return NO
push C # Save symByte args
push X
push A # Save digit
ld A (L I) # Multiply number by 10
call tenfoldA_A
ld (L I) A # Save
pop E # Get digit
shl E 4 # Make short number
or E CNT
call adduAE_A # Add to number
ld (L I) A # Save again
pop X # Pop symByte args
pop C
test (L -III) 1 # Fraction?
if nz # Yes
dec (L -I) # Decrement Scale
end
end
end
loop
test (L -III) 1 # Fraction?
if nz # Yes
do
sub (L -I) 1 # Decrement Scale
while nb # >= 0
ld A (L I) # Multiply number by 10
call tenfoldA_A
ld (L I) A # Save
loop
end
ld E (L I) # Get result
test (L -II) 1 # Sign?
if nz # Yes
cmp E ZERO # Zero?
if ne # No
xor E SIGN # Negate
end
end
lt # Return YES
99 drop
ret
# Format number to output, length, or symbol
(code 'fmtNum0AE_E 0)
ld (Sep3) 0 # Thousand separator 0
ld (Sep0) 0 # Decimal separator 0
(code 'fmtNumAE_E)
push C
push X
push Y
push Z
link
push ZERO # <L I> Name
link
push A # <L -I> Scale
ld A E # Get number
cnt A # Short number?
if nz # Yes
push 16 # <L -II> mask
else
push 1 # <L -II> mask
end
shr B 3 # Get sign bit
push A # <L -III> Sign flag
off E SIGN
# Calculate buffer size
ld A 19 # Decimal length of 'cnt' (60 bit)
ld C E # Get number
do
cnt C # Last digit?
while z # No
add A 20 # Add decimal length of 'digit' (64 bit)
ld C (C BIG)
loop
add A 17 # Round up
ld C 0 # Divide by 18
div 18
shl A 3 # Word count
sub S A # Space for incrementor
ld (S) 1 # Init to '1'
ld X S # Keep pointer to incrementor in X
sub S A # <S III> Accumulator
cmp S (StkLimit) # Stack check
jlt stkErr
ld (S) 0 # Init to '0'
ld A S # <S II> Top of accumulator
push A
push X # <S I> Pointer to incrementor
push X # <S> Top of incrementor
do
cnt E # Short number?
ldnz Z E # Yes
if z
ld Z (E DIG) # Digit in Z
end
do
ld A Z # Current digit
test A (L -II) # Test next bit with mask
if nz
# Add incrementor to accumulator
ld C 0 # Carry for BCD addition
lea X (S III) # Accumulator
ld Y (S I) # Incrementor
do
cmp X (S II) # X > Top of accumulator?
if gt # Yes
add (S II) 8 # Extend accumulator
ld (X) 0 # with '0'
end
ld A (X)
add A (Y) # Add BCD
add A C # Add BCD-Carry
ld C 0 # Clear BCD-Carry
cmp A 1000000000000000000 # BCD overflow?
if ge # Yes
sub A 1000000000000000000
ld C 1 # Set BCD-Carry
end
ld (X) A # Store BCD digit in accumulator
add X 8
add Y 8
cmp Y (S) # Reached top of incrementor?
until gt # Yes
null C # BCD-Carry?
if ne # Yes
add (S II) 8 # Extend accumulator
ld (X) 1 # With '1'
end
end
# Shift incrementor left
ld C 0 # Clear BCD-Carry
ld Y (S I) # Incrementor
do
ld A (Y)
add A A # Double
add A C # Add BCD-Carry
ld C 0 # Clear BCD-Carry
cmp A 1000000000000000000 # BCD overflow?
if ge # Yes
sub A 1000000000000000000
ld C 1 # Set BCD-Carry
end
ld (Y) A # Store BCD digit in incrementor
add Y 8
cmp Y (S) # Reached top of incrementor?
until gt # Yes
null C # BCD-Carry?
if ne # Yes
add (S) 8 # Extend incrementor
ld (Y) 1 # With '1'
end
shl (L -II) 1 # Shift bit mask
until z
cnt E # Short number?
while z # No
ld E (E BIG) # Next digit
cnt E # Short number?
if nz # Yes
ld A 16 # Mask
else
ld A 1
end
ld (L -II) A # Set bit mask
loop
ld Y (S II) # Top of accumulator
lea Z (S III) # Accumulator
null (L -I) # Scale negative?
if s # Yes
cmp (L -I) -1 # Direct print?
if eq # Yes
test (L -III) 1 # Sign?
if nz # Yes
ld B (char "-") # Output sign
call (PutB)
end
ld A (Y) # Output highest word
call outWordA
do
sub Y 8 # More?
cmp Y Z
while ge # Yes
ld A (Y) # Output words in reverse order
ld E 100000000000000000 # Digit scale
do
ld C 0 # Divide by digit scale
div E
push C # Save remainder
add B (char "0") # Output next digit
call (PutB)
cmp E 1 # Done?
while ne # No
ld C 0 # Divide digit scale by 10
ld A E
div 10
ld E A
pop A # Get remainder
loop
loop
else # Calculate length
ld A Y # Top of accumulator
sub A Z # Accumulator
shr A 3 # Number of accumulator words
mul 18 # Number of digits
ld E A
ld A (Y) # Length of highest word
do
inc E # Increment length
ld C 0 # Divide by 10
div 10
null A # Done?
until z # Yes
test (L -III) 1 # Sign?
if nz # Yes
inc E # Space for '-'
end
shl E 4 # Make short number
or E CNT
end
drop
else
ld C 4 # Build name
lea X (L I)
test (L -III) 1 # Sign?
if nz # Yes
ld B (char "-") # Insert sign
call byteSymBCX_CX
end
push C # Save name index
ld A Y # Top of accumulator
sub A Z # Accumulator
shr A 3 # Number of accumulator words
mul 18 # Number of digits
ld E A # Calculate length-1
ld A (Y) # Highest word
do
ld C 0 # Divide by 10
div 10
null A # Done?
while nz # No
inc E # Increment length
loop
pop C # Restore name index
sub E (L -I) # Scale
ld (L -I) E # Decrement by Length-1
if lt # Scale < 0
ld B (char "0") # Prepend '0'
call byteSymBCX_CX
ld A (Sep0) # Prepend decimal separator
call charSymACX_CX
do
cmp (L -I) -1 # Scale
while lt
inc (L -I) # Increment scale
ld B (char "0") # Ouput zeroes
call byteSymBCX_CX
loop
end
ld A (Y) # Pack highest word
call fmtWordACX_CX
do
sub Y 8 # More?
cmp Y Z
while ge # Yes
ld A (Y) # Pack words in reverse order
ld E 100000000000000000 # Digit scale
do
push A
call fmtScaleCX_CX # Handle scale character(s)
pop A
push C # Save name index
ld C 0 # Divide by digit scale
div E
xchg C (S) # Save remainder, restore name index
add B (char "0") # Pack next digit
call byteSymBCX_CX
cmp E 1 # Done?
while ne # No
push C # Save name index
ld C 0 # Divide digit scale by 10
ld A E
div 10
pop C # Restore name index
ld E A
pop A # Get remainder
loop
loop
ld X (L I) # Get name
drop
call consSymX_E
end
pop Z
pop Y
pop X
pop C
ret
(code 'fmtWordACX_CX 0)
cmp A 9 # Single digit?
if gt # No
ld E C # Save C
ld C 0 # Divide by 10
div 10
push C # Save remainder
ld C E # Restore C
call fmtWordACX_CX # Recurse
call fmtScaleCX_CX # Handle scale character(s)
pop A
end
add B (char "0") # Make ASCII digit
jmp byteSymBCX_CX
(code 'fmtScaleCX_CX 0)
null (L -I) # Scale null?
if z # Yes
ld A (Sep0) # Output decimal separator
call charSymACX_CX
else
null (Sep3) # Thousand separator?
if nz # Yes
ld A (L -I) # Scale > 0?
null A
if nsz # Yes
push C
ld C 0 # Modulus 3
div 3
null C
pop C
if z
ld A (Sep3) # Output thousand separator
call charSymACX_CX
end
end
end
end
dec (L -I) # Decrement scale
ret
# (format 'num ['cnt ['sym1 ['sym2]]]) -> sym
# (format 'sym|lst ['cnt ['sym1 ['sym2]]]) -> num
(code 'doFormat 2)
push X
push Y
ld X E
ld Y (E CDR) # Y on args
ld E (Y) # Eval first
eval
link
push E # <L I> 'num' | 'sym'
link
ld Y (Y CDR) # Second arg
ld E (Y)
eval # Eval 'cnt'
cmp E Nil # Any?
if eq # No
ld E 0 # Zero
else
call xCntEX_FE # Extract 'cnt'
end
push E # <L -I> Scale
push (char ".") # <L -II> Sep0
push 0 # Sep3
ld Y (Y CDR) # Third arg?
atom Y
if z # Yes
ld E (Y)
eval # Eval 'sym1'
num E # Need symbol
jnz symErrEX
sym E
jz symErrEX
call firstCharE_A
ld (L -II) A # Sep0
ld Y (Y CDR) # Fourth arg?
atom Y
if z # Yes
ld E (Y)
eval # Eval 'sym2'
num E # Need symbol
jnz symErrEX
sym E
jz symErrEX
call firstCharE_A
ld (S) A
end
end
pop (Sep3) # Get Sep3
pop (Sep0) # and Sep0
ld E (L I) # Get 'num' | 'sym'
num E # Number?
if nz # Yes
pop A # Get scale
call fmtNumAE_E # Convert to string
else
sym E # Symbol?
if nz # Yes
ld X (E TAIL)
call nameX_X # Get name
else
link
push ZERO # <L II> Number safe
push ZERO # <L I> Result
ld C 4 # Build name
ld X S
link
call packECX_CX
ld X (L I) # Get result
drop
end
pop A # Get scale
call symToNumXA_FE # Convert to number
if ge # Failed
ld E Nil
end
end
drop
pop Y
pop X
ret
### Arithmetics ###
# (+ 'num ..) -> num
(code 'doAdd 2)
push X
push Y
ld X E
ld Y (E CDR) # Y on args
ld E (Y)
eval # Eval first arg
cmp E Nil
if ne # Non-NIL
num E # Number?
jz numErrEX # No
link
push ZERO # <L II> Safe
push E # <L I> Result
link
do
ld Y (Y CDR) # More args?
atom Y
while z # Yes
ld E (Y)
eval # Eval next arg
cmp E Nil
jz 10 # Abort if NIL
num E # Number?
jz numErrEX # No
ld (L II) E # Save arg
ld A (L I) # Result
call addAE_A # Add
ld (L I) A # Result
loop
ld E (L I) # Result
10 drop
end
pop Y
pop X
ret
# (- 'num ..) -> num
(code 'doSub 2)
push X
push Y
ld X E
ld Y (E CDR) # Y on args
ld E (Y)
eval # Eval first arg
cmp E Nil
if ne # Non-NIL
num E # Number?
jz numErrEX # No
ld Y (Y CDR) # More than one arg?
atom Y
if nz # No: Unary minus
cmp E ZERO # Zero?
if ne # No
xor E SIGN # Negate
end
else
link
push ZERO # <L II> Safe
push E # <L I> Result
link
do
ld E (Y)
eval # Eval next arg
cmp E Nil
jz 10 # Abort if NIL
num E # Number?
jz numErrEX # No
ld (L II) E # Save arg
ld A (L I) # Result
call subAE_A # Subtract
ld (L I) A # Result
ld Y (Y CDR) # More args?
atom Y
until nz # No
ld E (L I) # Result
10 drop
end
end
pop Y
pop X
ret
# (inc 'num) -> num
# (inc 'var ['num]) -> num
(code 'doInc 2)
push X
push Y
ld X E
ld Y (E CDR) # Y on args
ld E (Y)
eval # Eval first arg
cmp E Nil
if ne # Non-NIL
link
push E # <L I/II> First arg
link
num E # Number?
if nz # Yes
call incE_A # Increment it
else
call checkVarEX
sym E # Symbol?
if nz # Yes
sym (E TAIL) # External symbol?
if nz # Yes
call dbTouchEX # Touch it
end
end
ld Y (Y CDR) # Next arg?
atom Y
if nz # No
ld E (E) # Get VAL
cmp E Nil # NIL?
ldz A E
if ne # No
num E # Number?
jz numErrEX # No
call incE_A # Increment it
ld ((L I)) A # Set new value
end
else
ld E (Y)
eval # Eval next arg
tuck E # <L I> Second arg
link
ld A ((L II)) # First arg's VAL
cmp A Nil # NIL?
if ne # No
num A # Number?
jz numErrAX # No
ld E (L I) # Second arg
cmp E Nil # NIL?
ldz A E
if ne # No
num E
jz numErrEX # No
call addAE_A # Add
ld ((L II)) A # Set new value
end
end
end
end
ld E A # Get result
drop
end
pop Y
pop X
ret
# (dec 'num) -> num
# (dec 'var ['num]) -> num
(code 'doDec 2)
push X
push Y
ld X E
ld Y (E CDR) # Y on args
ld E (Y)
eval # Eval first arg
cmp E Nil
if ne # Non-NIL
link
push E # <L I/II> First arg
link
num E # Number?
if nz # Yes
call decE_A # Decrement it
else
call checkVarEX
sym E # Symbol?
if nz # Yes
sym (E TAIL) # External symbol?
if nz # Yes
call dbTouchEX # Touch it
end
end
ld Y (Y CDR) # Next arg?
atom Y
if nz # No
ld E (E) # Get VAL
cmp E Nil # NIL?
ldz A E
if ne # No
num E # Number?
jz numErrEX # No
call decE_A # Decrement it
ld ((L I)) A # Set new value
end
else
ld E (Y)
eval # Eval next arg
tuck E # <L I> Second arg
link
ld A ((L II)) # First arg's VAL
cmp A Nil # NIL?
if ne # No
num A # Number?
jz numErrAX # No
ld E (L I) # Second arg
cmp E Nil # NIL?
ldz A E
if ne # No
num E
jz numErrEX # No
call subAE_A # Subtract
ld ((L II)) A # Set new value
end
end
end
end
ld E A # Get result
drop
end
pop Y
pop X
ret
# (* 'num ..) -> num
(code 'doMul 2)
push X
push Y
ld X E
ld Y (E CDR) # Y on args
ld E (Y)
eval # Eval first arg
cmp E Nil
if ne # Non-NIL
num E # Number?
jz numErrEX # No
ld B 0 # Init sign
test E SIGN
if nz
off E SIGN
inc B
end
link
push ZERO # <L II> Safe
push E # <L I> Result
link
push A # <L -I> Sign flag
do
ld Y (Y CDR) # More args?
atom Y
while z # Yes
ld E (Y)
eval # Eval next arg
cmp E Nil
jz 10 # Abort if NIL
num E # Number?
jz numErrEX # No
test E SIGN # Arg negative?
if nz # Yes
off E SIGN # Make argument positive
xor (L -I) 1 # Toggle result sign
end
ld (L II) E # Save arg
ld A (L I) # Result
call muluAE_A # Multiply
ld (L I) A # Result
loop
ld E (L I) # Result
test (L -I) 1 # Sign?
if nz # Yes
cmp E ZERO # Zero?
if ne # No
or E SIGN # Set negative
end
end
10 drop
end
pop Y
pop X
ret
# (*/ 'num1 ['num2 ..] 'num3) -> num
(code 'doMulDiv 2)
push X
push Y
ld X E
ld Y (E CDR) # Y on args
ld E (Y)
eval # Eval first arg
cmp E Nil
if ne # Non-NIL
num E # Number?
jz numErrEX # No
ld B 0 # Init sign
test E SIGN
if nz
off E SIGN
inc B
end
link
push ZERO # <L III> Safe
push ZERO # <L II> Safe
push E # <L I> Result
link
push A # <L -I> Sign flag
do
ld Y (Y CDR) # Next arg
ld E (Y)
eval # Eval next arg
cmp E Nil
jz 10 # Abort if NIL
num E # Number?
jz numErrEX # No
test E SIGN # Arg negative?
if nz # Yes
off E SIGN # Make argument positive
xor (L -I) 1 # Toggle result sign
end
ld (L II) E # Save arg
atom (Y CDR) # More args?
while z # Yes
ld A (L I) # Result
call muluAE_A # Multiply
ld (L I) A # Result
loop
cmp E ZERO # Zero?
jeq divErrX # Yes
ld A E # Last argument
call shruA_A # / 2
ld (L III) A # Save halved argument
ld E (L I) # Get product, keep in safe
call adduAE_A # Add for rounding
ld (L I) A # Save rounded product
ld E (L II) # Last argument
call divuAE_A # Divide
ld E A # Result
test (L -I) 1 # Sign?
if nz # Yes
cmp E ZERO # Zero?
if ne # No
or E SIGN # Set negative
end
end
10 drop
end
pop Y
pop X
ret
# (/ 'num ..) -> num
(code 'doDiv 2)
push X
push Y
ld X E
ld Y (E CDR) # Y on args
ld E (Y)
eval # Eval first arg
cmp E Nil
if ne # Non-NIL
num E # Number?
jz numErrEX # No
ld B 0 # Init sign
test E SIGN
if nz
off E SIGN
inc B
end
link
push ZERO # <L II> Safe
push E # <L I> Result
link
push A # <L -I> Sign flag
do
ld Y (Y CDR) # More args?
atom Y
while z # Yes
ld E (Y)
eval # Eval next arg
cmp E Nil
jz 10 # Abort if NIL
num E # Number?
jz numErrEX # No
cmp E ZERO # Zero?
jeq divErrX # Yes
test E SIGN # Arg negative?
if nz # Yes
off E SIGN # Make argument positive
xor (L -I) 1 # Toggle result sign
end
ld (L II) E # Save arg
ld A (L I) # Result
call divuAE_A # Divide
ld (L I) A # Result
loop
ld E (L I) # Result
test (L -I) 1 # Sign?
if nz # Yes
cmp E ZERO # Zero?
if ne # No
or E SIGN # Set negative
end
end
10 drop
end
pop Y
pop X
ret
# (% 'num ..) -> num
(code 'doRem 2)
push X
push Y
ld X E
ld Y (E CDR) # Y on args
ld E (Y)
eval # Eval first arg
cmp E Nil
if ne # Non-NIL
num E # Number?
jz numErrEX # No
ld B 0 # Init sign
test E SIGN
if nz
off E SIGN
ld B 1
end
link
push ZERO # <L II> Safe
push E # <L I> Result
link
push A # <L -I> Sign flag
do
ld Y (Y CDR) # More args?
atom Y
while z # Yes
ld E (Y)
eval # Eval next arg
cmp E Nil
jz 10 # Abort if NIL
num E # Number?
jz numErrEX # No
cmp E ZERO # Zero?
jeq divErrX # Yes
off E SIGN # Make argument positive
ld (L II) E # Save arg
ld A (L I) # Result
call remuAE_A # Remainder
ld (L I) A # Result
loop
ld E (L I) # Result
test (L -I) 1 # Sign?
if nz # Yes
cmp E ZERO # Zero?
if ne # No
or E SIGN # Set negative
end
end
10 drop
end
pop Y
pop X
ret
# (>> 'cnt 'num) -> num
(code 'doShift 2)
push X
push Y
ld X E
ld Y (E CDR) # Y on args
call evCntXY_FE # Get shift count
link
push ZERO # <L I> Safe
link
push E # <L -I> Shift count
ld Y (Y CDR) # Second arg
ld E (Y)
eval # Eval number
cmp E Nil # Any?
if nz # Yes
num E # Number?
jz numErrEX # No
cmp E ZERO # Zero?
if ne # No
ld (L I) E # Save
ld A E # Number in A
off A SIGN # Make positive
and E SIGN # Sign bit
push E # <L -II> Sign bit
null (L -I) # Shift count?
if nz # Yes
if ns # Positive
do
cnt A # A short?
while z # No
cmp (L -I) 64 # Large shift count?
while ge # Yes
ld A (A BIG) # Discard 64 bits
jz 90 # Jump if done
sub (L -I) 64 # Decrement count
loop
call shruA_A # Non-destructive
ld (L I) A
do
dec (L -I) # Shift count?
while nz
call halfA_A # Shift right (destructive)
ld (L I) A
loop
else
do
cmp (L -I) -64 # Large shift count?
while le # Yes
ld E 0 # Insert 64 zero-bits
call consNumEA_A
ld (L I) A
add (L -I) 64 # Increment count
jz 90 # Jump if done
loop
call shluA_A # Non-destructive
ld (L I) A
do
inc (L -I) # Shift count?
while nz
call twiceA_A # Shift left (destructive)
ld (L I) A
loop
end
end
90 cmp A ZERO # Result zero?
if ne # No
or A (L -II) # Sign bit
end
ld E A # Get result
end
end
drop
pop Y
pop X
ret
# (lt0 'any) -> num | NIL
(code 'doLt0 2)
ld E ((E CDR)) # Eval arg
eval
num E # Number?
jz retNil
test E SIGN # Negative?
jz retNil
ret # Yes: Return num
# (le0 'any) -> num | NIL
(code 'doLe0 2)
ld E ((E CDR)) # Eval arg
eval
num E # Number?
jz retNil
cmp E ZERO # Zero?
if ne # No
test E SIGN # Negative?
jz retNil
end
ret # Yes: Return num
# (ge0 'any) -> num | NIL
(code 'doGe0 2)
ld E ((E CDR)) # Eval arg
eval
num E # Number?
jz retNil
test E SIGN # Positive?
jnz retNil
ret # Yes: Return num
# (gt0 'any) -> num | NIL
(code 'doGt0 2)
ld E ((E CDR)) # Eval arg
eval
num E # Number?
jz retNil
cmp E ZERO # Zero?
jeq retNil
test E SIGN # Positive?
jnz retNil
ret # Yes: Return num
# (abs 'num) -> num
(code 'doAbs 2)
push X
ld X E
ld E ((E CDR)) # Eval arg
eval
cmp E Nil # Any?
if nz # Yes
num E # Number?
jz numErrEX # No
off E SIGN # Clear sign
end
pop X
ret
### Bit operations ###
# (bit? 'num ..) -> num | NIL
(code 'doBitQ 2)
push X
push Y
ld X E
ld Y (E CDR) # Y on args
ld E (Y)
eval # Eval first arg
num E # Number?
jz numErrEX # No
off E SIGN # Clear sign
link
push E # <L I> Bit mask
link
do
ld Y (Y CDR) # More args?
atom Y
while z # Yes
ld E (Y)
eval # Eval next arg
cmp E Nil
while ne # Abort if NIL
num E # Number?
jz numErrEX # No
off E SIGN # Clear sign
ld C (L I) # Get mask
do
cnt C # C short?
while z # No
cnt E # E short?
jnz 10 # Yes: Return NIL
ld A (E DIG) # Get digit
and A (C DIG) # Match?
cmp A (C DIG)
jne 10 # No: Return NIL
ld C (C BIG)
ld E (E BIG)
loop
cnt E # E also short?
if z # No
shr C 4 # Normalize
ld E (E DIG) # Get digit
end
and E C # Match?
cmp E C
if ne # No
10 ld E Nil # Return NIL
drop
pop Y
pop X
ret
end
loop
ld E (L I) # Return bit mask
drop
pop Y
pop X
ret
# (& 'num ..) -> num
(code 'doBitAnd 2)
push X
push Y
ld X E
ld Y (E CDR) # Y on args
ld E (Y)
eval # Eval first arg
cmp E Nil
if ne # Non-NIL
num E # Number?
jz numErrEX # No
off E SIGN # Clear sign
link
push ZERO # <L II> Safe
push E # <L I> Result
link
do
ld Y (Y CDR) # More args?
atom Y
while z # Yes
ld E (Y)
eval # Eval next arg
cmp E Nil
jeq 10 # Abort if NIL
num E # Number?
jz numErrEX # No
off E SIGN # Clear sign
ld (L II) E # Save arg
ld A (L I) # Result
call anduAE_A # Bitwise AND
ld (L I) A # Result
loop
ld E (L I) # Result
10 drop
end
pop Y
pop X
ret
# (| 'num ..) -> num
(code 'doBitOr 2)
push X
push Y
ld X E
ld Y (E CDR) # Y on args
ld E (Y)
eval # Eval first arg
cmp E Nil
if ne # Non-NIL
num E # Number?
jz numErrEX # No
off E SIGN # Clear sign
link
push ZERO # <L II> Safe
push E # <L I> Result
link
do
ld Y (Y CDR) # More args?
atom Y
while z # Yes
ld E (Y)
eval # Eval next arg
cmp E Nil
jeq 10 # Abort if NIL
num E # Number?
jz numErrEX # No
off E SIGN # Clear sign
ld (L II) E # Save arg
ld A (L I) # Result
call oruAE_A # Bitwise OR
ld (L I) A # Result
loop
ld E (L I) # Result
10 drop
end
pop Y
pop X
ret
# (x| 'num ..) -> num
(code 'doBitXor 2)
push X
push Y
ld X E
ld Y (E CDR) # Y on args
ld E (Y)
eval # Eval first arg
cmp E Nil
if ne # Non-NIL
num E # Number?
jz numErrEX # No
off E SIGN # Clear sign
link
push ZERO # <L II> Safe
push E # <L I> Result
link
do
ld Y (Y CDR) # More args?
atom Y
while z # Yes
ld E (Y)
eval # Eval next arg
cmp E Nil
jeq 10 # Abort if NIL
num E # Number?
jz numErrEX # No
off E SIGN # Clear sign
ld (L II) E # Save arg
ld A (L I) # Result
call xoruAE_A # Bitwise XOR
ld (L I) A # Result
loop
ld E (L I) # Result
10 drop
end
pop Y
pop X
ret
# (sqrt 'num ['flg|num]) -> num
(code 'doSqrt 2)
push X
push Y
ld X E
ld Y (E CDR) # Y on args
ld E (Y)
eval # Eval first arg
cmp E Nil
if ne # Non-NIL
num E # Number?
jz numErrEX # No
test E SIGN # Negative?
jnz argErrEX # Yes
link
push E # <L II/V> First arg
link
ld E ((Y CDR)) # Second arg
eval # flg|num
tuck E # <L I/IV> Second arg
link
ld A (L II) # First arg in A
num E # Second arg numeric?
if nz # Yes
call muluAE_A # Multiply with scale
end
cnt A # Short?
if nz # Yes
shr A 4 # Normalize
ld C (hex "400000000000000") # Mask
ld E 0 # Result
do
add E C # result += mask
cmp E A # > number?
if gt # Yes
sub E C # Undo
else
sub A E # Subtract result
add E C # Add mask to result
end
shr E 1 # Shift result
shr C 2 # Shift mask
until z
cmp (L I) Nil # Second arg?
if ne # Yes
cmp A E # Round?
if gt # Yes
inc E # Increment result
end
end
shl E 4 # Make short number
or E CNT
else
tuck A # <L III> Number
push A # <L II> Mask
push ZERO # <L I> Result
link
ld C 0 # Init mask
ld E ONE
call consNumCE_C
ld (L II) C # Save
ld E (A DIG) # Copy number
call boxNumE_E
ld (L III) E # Save
do
ld A (A BIG) # Next cell
cnt A # Last one?
while z # No
call boxNum_C # Copy next digit
ld (C DIG) (A DIG)
ld (E BIG) C
ld E C
call boxNum_X # Extend mask
ld (X DIG) 0
ld (X BIG) (L II)
ld (L II) X # Save
loop
ld (E BIG) A # Copy trailing short
ld A (L II) # Mask
do
ld E (L III) # Number
call cmpuAE_F # Mask <= number?
while le # Yes
call twiceA_A # Times 4
call twiceA_A
loop
do
ld A (L I) # result += mask
ld E (L II)
call adduAE_A
ld (L I) A
ld E (L III) # > number?
call cmpuAE_F
if gt # Yes
ld E (L II) # Undo
call subuAE_A
else
ld A (L III) # Subtract result
ld E (L I)
call subuAE_A
ld (L III) A
ld A (L I) # Add mask to result
ld E (L II)
call adduAE_A
end
call halfA_A # Shift result
ld (L I) A
ld A (L II) # Shift mask twice
call halfA_A
call halfA_A
ld (L II) A
cmp A ZERO # Zero?
until eq # Yes
ld E (L I) # Get result
cmp (L IV) Nil # Second arg?
if ne # Yes
ld A (L III) # Get number
call cmpuAE_F # Round?
if gt # Yes
ld A ONE # Increment result
call adduAE_A
ld E A
end
end
end
drop
end
pop Y
pop X
ret
### Random generator ###
(code 'initSeedE_E 0)
push C # Counter
ld C 0
do
atom E # Pair?
while z # Yes
push E # Recurse on CAR
ld E (E)
call initSeedE_E
add C E
pop E # Loop on CDR
ld E (E CDR)
loop
cmp E Nil # NIL?
if ne # No
num E # Need number
if z # Must be symbol
ld E (E TAIL)
call nameE_E # Get name
end
cnt E # Short?
if nz # Yes
shr E 3 # Keep sign
else
test E SIGN # E positive?
if nz # Yes
inc C # Add 1
off E SIGN # Make positive
end
do
add C (E DIG) # Add next digit
ld E (E BIG)
cnt E # Done?
until nz # Yes
shr E 4 # Final short
end
add C E # Add
end
ld E C # Return counter
pop C
ret
# (seed 'any) -> cnt
(code 'doSeed 2)
ld E ((E CDR)) # Eval arg
eval
call initSeedE_E # Initialize 'Seed'
ld A 6364136223846793005 # Multiplier
mul E # times 'Seed'
ld (Seed) D # Save
shr A (- 32 3) # Get higher 32 bits
ld E A
off E 7 # Keep sign
or E CNT # Make short number
ret
# (hash 'any) -> cnt
(code 'doHash 2)
push X
ld E ((E CDR)) # Eval arg
eval
call initSeedE_E # Initialize
ld X E # Value in X
ld C 64 # Counter
ld E 0 # Result
do
ld A X # Value XOR Result
xor A E
test A 1 # LSB set?
if nz # Yes
xor E (hex "14002") # CRC Polynom x**16 + x**15 + x**2 + 1
end
shr X 1 # Shift value
shr E 1 # and result
dec C # Done?
until z # Yes
inc E # Plus 1
shl E 4 # Make short number
or E CNT # Make short number
pop X
ret
# (rand ['cnt1 'cnt2] | ['T]) -> cnt | flg
(code 'doRand 2)
push X
push Y
ld X E
ld Y (E CDR) # Y on args
ld A 6364136223846793005 # Multiplier
mul (Seed) # times 'Seed'
add D 1 # plus 1
ld (Seed) D # Save
ld E (Y)
eval # Eval first arg
ld A (Seed) # Random low word
cmp E Nil # Any?
if eq # No
shr A (- 32 3) # Get higher 32 bits
ld E A
off E 7 # Keep sign
or E CNT # Make short number
pop Y
pop X
ret
end
cmp E TSym # Boolean
if eq
add A A # Highest bit?
if nc # No
ld E Nil # Return NIL
end # else return T
pop Y
pop X
ret
end
call xCntEX_FE # Get cnt1
push E # Save it
ld Y (Y CDR) # Second arg
call evCntXY_FE # Get cnt2
inc E # Seed % (cnt2 + 1 - cnt1) + cnt1
sub E (S)
if nz # Skip if zero
ld D (Seed) # Get 'Seed'
shl C 32 # Get middle 64 bits
shr A 32
or A C
ld C 0
div E # Modulus in C
end
pop E # + cnt1
add E C
pop Y
pop X
jmp boxE_E # Return short number
# vi:et:ts=3:sw=3
|