1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899 4900 4901 4902 4903 4904 4905 4906 4907 4908 4909 4910 4911 4912 4913 4914 4915 4916 4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927 4928 4929 4930 4931 4932 4933 4934 4935 4936 4937 4938 4939 4940 4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 4956 4957 4958 4959 4960 4961 4962 4963 4964 4965 4966 4967 4968 4969 4970 4971 4972 4973 4974 4975 4976 4977 4978 4979 4980 4981 4982 4983 4984 4985 4986 4987 4988 4989 4990 4991 4992 4993 4994 4995 4996 4997 4998 4999 5000 5001 5002 5003 5004 5005 5006 5007 5008 5009 5010 5011 5012 5013 5014 5015 5016 5017 5018 5019 5020 5021 5022 5023 5024 5025 5026 5027 5028 5029 5030 5031 5032 5033 5034 5035 5036 5037 5038 5039 5040 5041 5042 5043 5044 5045 5046 5047 5048 5049 5050 5051 5052 5053 5054 5055 5056 5057 5058 5059 5060 5061 5062 5063 5064 5065 5066 5067 5068 5069 5070 5071 5072 5073 5074 5075 5076 5077 5078 5079 5080 5081 5082 5083 5084 5085 5086 5087 5088 5089 5090 5091 5092 5093 5094 5095 5096 5097 5098 5099 5100 5101 5102 5103 5104 5105 5106 5107 5108 5109 5110 5111 5112 5113 5114 5115 5116 5117 5118 5119 5120 5121 5122 5123 5124 5125 5126 5127 5128 5129 5130 5131 5132 5133 5134 5135 5136 5137 5138 5139 5140 5141 5142 5143 5144 5145 5146 5147 5148 5149 5150 5151 5152 5153 5154 5155 5156 5157 5158 5159 5160 5161 5162 5163 5164 5165 5166 5167 5168 5169 5170 5171 5172 5173 5174 5175 5176 5177 5178 5179 5180 5181 5182 5183 5184 5185 5186 5187 5188 5189 5190 5191 5192 5193 5194 5195 5196 5197 5198 5199 5200 5201 5202 5203 5204 5205 5206 5207 5208 5209 5210 5211 5212 5213 5214 5215 5216 5217 5218 5219 5220 5221 5222 5223 5224 5225 5226 5227 5228 5229 5230 5231 5232 5233 5234 5235 5236 5237 5238 5239 5240 5241 5242 5243 5244 5245 5246 5247 5248 5249 5250 5251 5252 5253 5254 5255 5256 5257 5258 5259 5260 5261 5262 5263 5264 5265 5266 5267 5268 5269 5270 5271 5272 5273 5274 5275 5276 5277 5278 5279 5280 5281 5282 5283 5284 5285 5286 5287 5288 5289 5290 5291 5292 5293 5294 5295 5296 5297 5298 5299 5300 5301 5302 5303 5304 5305 5306 5307 5308 5309 5310 5311 5312 5313 5314 5315 5316 5317 5318 5319 5320 5321 5322 5323 5324 5325 5326 5327 5328 5329 5330 5331 5332 5333 5334 5335 5336 5337 5338 5339 5340 5341 5342 5343 5344 5345 5346 5347 5348 5349 5350 5351 5352 5353 5354 5355 5356 5357 5358 5359 5360 5361 5362 5363 5364 5365 5366 5367 5368 5369 5370 5371 5372 5373 5374 5375 5376 5377 5378 5379 5380 5381 5382 5383 5384 5385 5386 5387 5388 5389 5390 5391 5392 5393 5394 5395 5396 5397 5398 5399 5400 5401 5402 5403 5404 5405 5406 5407 5408 5409 5410 5411 5412 5413 5414 5415 5416 5417 5418 5419 5420 5421 5422 5423 5424 5425 5426 5427 5428 5429 5430 5431 5432 5433 5434 5435 5436 5437 5438 5439 5440 5441 5442 5443 5444 5445 5446 5447 5448 5449 5450 5451 5452 5453 5454 5455 5456 5457 5458 5459 5460 5461 5462 5463 5464 5465 5466 5467 5468 5469 5470 5471 5472 5473 5474 5475 5476 5477 5478 5479 5480 5481 5482 5483 5484 5485 5486 5487 5488 5489 5490 5491 5492 5493 5494 5495 5496 5497 5498 5499 5500 5501 5502 5503 5504 5505 5506 5507 5508 5509 5510 5511 5512 5513 5514 5515 5516 5517 5518 5519 5520 5521 5522 5523 5524 5525 5526 5527 5528 5529 5530 5531 5532 5533 5534 5535 5536 5537 5538 5539 5540 5541 5542 5543 5544 5545 5546 5547 5548 5549 5550 5551 5552 5553 5554 5555 5556 5557 5558 5559 5560 5561 5562 5563 5564 5565 5566 5567 5568 5569 5570 5571 5572 5573 5574 5575 5576 5577 5578 5579 5580 5581 5582 5583 5584 5585 5586 5587 5588 5589 5590 5591 5592 5593 5594 5595 5596 5597 5598 5599 5600 5601 5602 5603 5604 5605 5606 5607 5608 5609 5610 5611 5612 5613 5614 5615 5616 5617 5618 5619 5620 5621 5622 5623 5624 5625 5626 5627 5628 5629 5630 5631 5632 5633 5634 5635 5636 5637 5638 5639 5640 5641 5642 5643 5644 5645 5646 5647 5648 5649 5650 5651 5652 5653 5654 5655 5656 5657 5658 5659 5660 5661 5662 5663 5664 5665 5666 5667 5668 5669 5670 5671 5672 5673 5674 5675 5676 5677 5678 5679 5680 5681 5682 5683 5684 5685 5686 5687 5688 5689 5690 5691 5692 5693 5694 5695 5696 5697 5698 5699 5700 5701 5702 5703 5704 5705 5706 5707 5708 5709 5710 5711 5712 5713 5714 5715 5716 5717 5718 5719 5720 5721 5722 5723 5724 5725 5726 5727 5728 5729 5730 5731 5732 5733 5734 5735 5736 5737 5738 5739 5740 5741 5742 5743 5744 5745 5746 5747 5748 5749 5750 5751 5752 5753 5754 5755 5756 5757 5758 5759 5760 5761 5762 5763 5764 5765 5766 5767 5768 5769 5770 5771 5772 5773 5774 5775 5776 5777 5778 5779 5780 5781 5782 5783 5784 5785 5786 5787 5788 5789 5790 5791 5792 5793 5794 5795
|
/** @file proces.c
*
* Contains the central terms processor routines. This is the core of
* the virtual machine. All other files are to help these routines.
*/
/* #[ License : */
/*
* Copyright (C) 1984-2026 J.A.M. Vermaseren
* When using this file you are requested to refer to the publication
* J.A.M.Vermaseren "New features of FORM" math-ph/0010025
* This is considered a matter of courtesy as the development was paid
* for by FOM the Dutch physics granting agency and we would like to
* be able to track its scientific use to convince FOM of its value
* for the community.
*
* This file is part of FORM.
*
* FORM is free software: you can redistribute it and/or modify it under the
* terms of the GNU General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any later
* version.
*
* FORM is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along
* with FORM. If not, see <http://www.gnu.org/licenses/>.
*/
/* #] License : */
/*
#define HIDEDEBUG
#[ Includes : proces.c
*/
#include "form3.h"
WORD printscratch[2];
/*
#] Includes :
#[ Processor :
#[ Processor : WORD Processor()
*/
/**
* This is the central processor.
* It accepts a stream of Expressions which is accessed by calls to GetTerm.
* The expressions reside either in AR.infile or AR.hidefile
* The definitions of an expression are seen as an id-statement, so the
* primary Expressions should be written to the system of scratch files
* as single terms with an expression pointer. Each expression is terminated
* with a zero and the whole is terminated by two zeroes.
*
* The routine DoExecute should determine whether results are to be
* printed, should revert the scratch I/O directions etc.
* In principle it is DoExecute that calls Processor.
*
* @return if everything OK: 0. Otherwise error. The preprocessor
* may continue with compilation though. Really fatal errors should
* return on the spot by calling Terminate.
*/
int Processor(void)
{
GETIDENTITY
WORD *term, *t, i, size;
int retval = 0;
EXPRESSIONS e;
POSITION position;
WORD last, LastExpression, fromspectator;
LONG dd = 0;
CBUF *C = cbuf+AC.cbufnum;
int firstterm;
CBUF *CC = cbuf+AT.ebufnum;
WORD **w, *cpo, *cbo;
FILEHANDLE *curfile, *oldoutfile = AR.outfile;
WORD oldBracketOn = AR.BracketOn;
WORD *oldBrackBuf = AT.BrackBuf;
WORD oldbracketindexflag = AT.bracketindexflag;
#ifdef WITHPTHREADS
int OldMultiThreaded = AS.MultiThreaded, Oldmparallelflag = AC.mparallelflag;
#endif
if ( CC->numrhs > 0 || CC->numlhs > 0 ) {
if ( CC->rhs ) {
w = CC->rhs; i = CC->numrhs;
do { *w++ = 0; } while ( --i > 0 );
}
if ( CC->lhs ) {
w = CC->lhs; i = CC->numlhs;
do { *w++ = 0; } while ( --i > 0 );
}
CC->numlhs = CC->numrhs = 0;
ClearTree(AT.ebufnum);
CC->Pointer = CC->Buffer;
}
if ( NumExpressions == 0 ) return(0);
AR.expflags = 0;
AR.CompressPointer = AR.CompressBuffer;
AR.NoCompress = AC.NoCompress;
term = AT.WorkPointer;
if ( ( (WORD *)(((UBYTE *)(AT.WorkPointer)) + AM.MaxTer) ) > AT.WorkTop ) {
MesWork();
}
UpdatePositions();
C->rhs[C->numrhs+1] = C->Pointer;
AR.KeptInHold = 0;
if ( AC.CollectFun ) AR.DeferFlag = 0;
AR.outtohide = 0;
AN.PolyFunTodo = 0;
#ifdef HIDEDEBUG
MesPrint("Status at the start of Processor (HideLevel = %d)",AC.HideLevel);
for ( i = 0; i < NumExpressions; i++ ) {
e = Expressions+i;
ExprStatus(e);
}
#endif
/*
Next determine the last expression. This is used for removing the
input file when the final stage of the sort of this expression is
reached. That can save up to 1/3 in disk space.
*/
for ( i = NumExpressions-1; i >= 0; i-- ) {
e = Expressions+i;
if ( e->status == LOCALEXPRESSION || e->status == GLOBALEXPRESSION
|| e->status == HIDELEXPRESSION || e->status == HIDEGEXPRESSION
|| e->status == SKIPLEXPRESSION || e->status == SKIPGEXPRESSION
|| e->status == UNHIDELEXPRESSION || e->status == UNHIDEGEXPRESSION
|| e->status == INTOHIDELEXPRESSION || e->status == INTOHIDEGEXPRESSION
) break;
}
last = i;
for ( i = NumExpressions-1; i >= 0; i-- ) {
AS.OldOnFile[i] = Expressions[i].onfile;
AS.OldNumFactors[i] = Expressions[i].numfactors;
/* AS.Oldvflags[i] = e[i].vflags; */
AS.Oldvflags[i] = Expressions[i].vflags;
AS.Olduflags[i] = Expressions[i].uflags;
Expressions[i].vflags &= ~(ISUNMODIFIED|ISZERO);
}
#ifdef WITHPTHREADS
/*
When we run with threads we have to make sure that all local input
buffers are pointed correctly. Of course this isn't needed if we
run on a single thread only.
*/
if ( AC.partodoflag && AM.totalnumberofthreads > 1 ) {
AS.MultiThreaded = 1; AC.mparallelflag = PARALLELFLAG;
}
if ( AS.MultiThreaded && AC.mparallelflag == PARALLELFLAG ) {
SetWorkerFiles();
}
/*
We start with running the expressions with expr->partodo in parallel.
The current model is: give each worker an expression. Wait for
workers to finish and tell them where to write.
Then give them a new expression. Workers may have to wait for each
other. This is also the case with the last one.
*/
if ( AS.MultiThreaded && AC.mparallelflag == PARALLELFLAG ) {
if ( InParallelProcessor() ) {
retval = 1;
}
AS.MultiThreaded = OldMultiThreaded;
AC.mparallelflag = Oldmparallelflag;
}
#endif
#ifdef WITHMPI
if ( AC.RhsExprInModuleFlag && PF.rhsInParallel && (AC.mparallelflag == PARALLELFLAG || AC.partodoflag) ) {
if ( PF_BroadcastRHS() ) {
retval = -1;
}
}
PF.exprtodo = -1; /* This means, the slave does not perform inparallel */
if ( AC.partodoflag > 0 ) {
if ( PF_InParallelProcessor() ) {
retval = -1;
}
}
#endif
for ( i = 0; i < NumExpressions; i++ ) {
#ifdef INNERTEST
if ( AC.InnerTest ) {
if ( StrCmp(AC.TestValue,(UBYTE *)INNERTEST) == 0 ) {
MesPrint("Testing(Processor): value = %s",AC.TestValue);
}
}
#endif
e = Expressions+i;
#ifdef WITHPTHREADS
if ( AC.partodoflag > 0 && e->partodo > 0 && AM.totalnumberofthreads > 2 ) {
e->partodo = 0;
continue;
}
#endif
#ifdef WITHMPI
if ( AC.partodoflag > 0 && e->partodo > 0 && PF.numtasks > 2 ) {
e->partodo = 0;
continue;
}
#endif
AS.CollectOverFlag = 0;
AR.expchanged = 0;
if ( i == last ) LastExpression = 1;
else LastExpression = 0;
if ( e->inmem ) {
/*
#[ in memory : Memory allocated by poly.c only thusfar.
Here GetTerm cannot work.
For the moment we ignore this for parallelization.
*/
WORD j;
AR.GetFile = 0;
SetScratch(AR.infile,&(e->onfile));
if ( GetTerm(BHEAD term) <= 0 ) {
MesPrint("(1) Expression %d has problems in scratchfile",i);
retval = -1;
break;
}
term[3] = i;
AR.CurExpr = i;
SeekScratch(AR.outfile,&position);
e->onfile = position;
if ( PutOut(BHEAD term,&position,AR.outfile,0) < 0 ) goto ProcErr;
AR.DeferFlag = AC.ComDefer;
NewSort(BHEAD0);
AN.ninterms = 0;
t = e->inmem;
while ( *t ) {
for ( j = 0; j < *t; j++ ) term[j] = t[j];
t += *t;
AN.ninterms++; dd = AN.deferskipped;
if ( AC.CollectFun && *term <= (AM.MaxTer/(2*(LONG)(sizeof(WORD)))) ) {
if ( GetMoreFromMem(term,&t) ) {
LowerSortLevel(); goto ProcErr;
}
}
AT.WorkPointer = term + *term;
AN.RepPoint = AT.RepCount + 1;
AN.IndDum = AM.IndDum;
AR.CurDum = ReNumber(BHEAD term);
if ( AC.SymChangeFlag ) MarkDirty(term,DIRTYSYMFLAG);
if ( AN.ncmod ) {
if ( ( AC.modmode & ALSOFUNARGS ) != 0 ) MarkDirty(term,DIRTYFLAG);
else if ( AR.PolyFun ) PolyFunDirty(BHEAD term);
}
else if ( AC.PolyRatFunChanged ) PolyFunDirty(BHEAD term);
if ( Generator(BHEAD term,0) ) {
LowerSortLevel(); goto ProcErr;
}
AN.ninterms += dd;
}
AN.ninterms += dd;
if ( EndSort(BHEAD AM.S0->sBuffer,0) < 0 ) goto ProcErr;
if ( AM.S0->TermsLeft ) e->vflags &= ~ISZERO;
else e->vflags |= ISZERO;
if ( AR.expchanged == 0 ) e->vflags |= ISUNMODIFIED;
if ( AM.S0->TermsLeft ) AR.expflags |= ISZERO;
if ( AR.expchanged ) AR.expflags |= ISUNMODIFIED;
AR.GetFile = 0;
/*
#] in memory :
*/
}
else {
AR.CurExpr = i;
switch ( e->status ) {
case UNHIDELEXPRESSION:
case UNHIDEGEXPRESSION:
AR.GetFile = 2;
#ifdef WITHMPI
if ( PF.me == MASTER ) SetScratch(AR.hidefile,&(e->onfile));
#else
SetScratch(AR.hidefile,&(e->onfile));
AR.InHiBuf = AR.hidefile->POfull-AR.hidefile->POfill;
#ifdef HIDEDEBUG
MesPrint("Hidefile: onfile: %15p, POposition: %15p, filesize: %15p",&(e->onfile)
,&(AR.hidefile->POposition),&(AR.hidefile->filesize));
MesPrint("Set hidefile to buffer position %l/%l; AR.InHiBuf = %l"
,(AR.hidefile->POfill-AR.hidefile->PObuffer)*sizeof(WORD)
,(AR.hidefile->POfull-AR.hidefile->PObuffer)*sizeof(WORD)
,AR.InHiBuf
);
#endif
#endif
curfile = AR.hidefile;
goto commonread;
case INTOHIDELEXPRESSION:
case INTOHIDEGEXPRESSION:
AR.outtohide = 1;
/*
BugFix 12-feb-2016
This may not work when the file is open and we move around.
AR.hidefile->POfill = AR.hidefile->POfull;
*/
SetEndHScratch(AR.hidefile,&position);
/* fall through */
case LOCALEXPRESSION:
case GLOBALEXPRESSION:
AR.GetFile = 0;
/*[20oct2009 mt]:*/
#ifdef WITHMPI
if( ( PF.me == MASTER ) || (PF.mkSlaveInfile) )
#endif
SetScratch(AR.infile,&(e->onfile));
/*:[20oct2009 mt]*/
curfile = AR.infile;
commonread:;
#ifdef WITHMPI
if ( PF_Processor(e,i,LastExpression) ) {
MesPrint("Error in PF_Processor");
goto ProcErr;
}
/*[20oct2009 mt]:*/
if ( AC.mparallelflag != PARALLELFLAG ){
if(PF.me != MASTER)
break;
#endif
/*:[20oct2009 mt]*/
if ( GetTerm(BHEAD term) <= 0 ) {
#ifdef HIDEDEBUG
MesPrint("Error condition 1a");
ExprStatus(e);
#endif
MesPrint("(2) Expression %d has problems in scratchfile(process)",i);
retval = -1;
break;
}
term[3] = i;
if ( term[5] < 0 ) { /* Fill with spectator */
fromspectator = -term[5];
PUTZERO(AM.SpectatorFiles[fromspectator-1].readpos);
term[5] = AC.cbufnum;
}
else fromspectator = 0;
if ( AR.outtohide ) {
SeekScratch(AR.hidefile,&position);
e->onfile = position;
if ( PutOut(BHEAD term,&position,AR.hidefile,0) < 0 ) goto ProcErr;
}
else {
SeekScratch(AR.outfile,&position);
e->onfile = position;
if ( PutOut(BHEAD term,&position,AR.outfile,0) < 0 ) goto ProcErr;
}
AR.DeferFlag = AC.ComDefer;
AR.Eside = RHSIDE;
if ( ( e->vflags & ISFACTORIZED ) != 0 ) {
AR.BracketOn = 1;
AT.BrackBuf = AM.BracketFactors;
AT.bracketindexflag = 1;
}
if ( AT.bracketindexflag > 0 ) OpenBracketIndex(i);
#ifdef WITHPTHREADS
if ( AS.MultiThreaded && AC.mparallelflag == PARALLELFLAG ) {
if ( ThreadsProcessor(e,LastExpression,fromspectator) ) {
MesPrint("Error in ThreadsProcessor");
goto ProcErr;
}
if ( AR.outtohide ) {
AR.outfile = oldoutfile;
AR.hidefile->POfull = AR.hidefile->POfill;
}
}
else
#endif
{
NewSort(BHEAD0);
AR.MaxDum = AM.IndDum;
AN.ninterms = 0;
for(;;) {
if ( fromspectator ) size = GetFromSpectator(term,fromspectator-1);
else size = GetTerm(BHEAD term);
if ( size <= 0 ) break;
SeekScratch(curfile,&position);
if ( ( e->vflags & ISFACTORIZED ) != 0 && term[1] == HAAKJE ) {
StoreTerm(BHEAD term);
}
else {
AN.ninterms++; dd = AN.deferskipped;
if ( AC.CollectFun && *term <= (AM.MaxTer/(2*(LONG)(sizeof(WORD)))) ) {
if ( GetMoreTerms(term) < 0 ) {
LowerSortLevel(); goto ProcErr;
}
SeekScratch(curfile,&position);
}
AT.WorkPointer = term + *term;
AN.RepPoint = AT.RepCount + 1;
if ( AR.DeferFlag ) {
AN.IndDum = Expressions[AR.CurExpr].numdummies + AM.IndDum;
AR.CurDum = AN.IndDum;
}
else {
AN.IndDum = AM.IndDum;
AR.CurDum = ReNumber(BHEAD term);
}
if ( AC.SymChangeFlag ) MarkDirty(term,DIRTYSYMFLAG);
if ( AN.ncmod ) {
if ( ( AC.modmode & ALSOFUNARGS ) != 0 ) MarkDirty(term,DIRTYFLAG);
else if ( AR.PolyFun ) PolyFunDirty(BHEAD term);
}
else if ( AC.PolyRatFunChanged ) PolyFunDirty(BHEAD term);
if ( ( AR.PolyFunType == 2 ) && ( AC.PolyRatFunChanged == 0 )
&& ( e->status == LOCALEXPRESSION || e->status == GLOBALEXPRESSION ) ) {
PolyFunClean(BHEAD term);
}
if ( Generator(BHEAD term,0) ) {
LowerSortLevel(); goto ProcErr;
}
AN.ninterms += dd;
}
SetScratch(curfile,&position);
if ( AR.GetFile == 2 ) {
AR.InHiBuf = (curfile->POfull-curfile->PObuffer)
-DIFBASE(position,curfile->POposition)/sizeof(WORD);
}
else {
AR.InInBuf = (curfile->POfull-curfile->PObuffer)
-DIFBASE(position,curfile->POposition)/sizeof(WORD);
}
}
AN.ninterms += dd;
if ( LastExpression ) {
UpdateMaxSize();
if ( AR.infile->handle >= 0 ) {
CloseFile(AR.infile->handle);
AR.infile->handle = -1;
remove(AR.infile->name);
PUTZERO(AR.infile->POposition);
}
AR.infile->POfill = AR.infile->POfull = AR.infile->PObuffer;
}
if ( AR.outtohide ) AR.outfile = AR.hidefile;
if ( EndSort(BHEAD AM.S0->sBuffer,0) < 0 ) goto ProcErr;
if ( AR.outtohide ) {
AR.outfile = oldoutfile;
AR.hidefile->POfull = AR.hidefile->POfill;
}
e->numdummies = AR.MaxDum - AM.IndDum;
UpdateMaxSize();
}
AR.BracketOn = oldBracketOn;
AT.BrackBuf = oldBrackBuf;
if ( ( e->vflags & TOBEFACTORED ) != 0 ) {
poly_factorize_expression(e);
}
else if ( ( ( e->vflags & TOBEUNFACTORED ) != 0 )
&& ( ( e->vflags & ISFACTORIZED ) != 0 ) ) {
poly_unfactorize_expression(e);
}
AT.bracketindexflag = oldbracketindexflag;
if ( AM.S0->TermsLeft ) e->vflags &= ~ISZERO;
else e->vflags |= ISZERO;
if ( AR.expchanged == 0 ) e->vflags |= ISUNMODIFIED;
if ( AM.S0->TermsLeft ) AR.expflags |= ISZERO;
if ( AR.expchanged ) AR.expflags |= ISUNMODIFIED;
AR.GetFile = 0;
AR.outtohide = 0;
/*[20oct2009 mt]:*/
#ifdef WITHMPI
}
#endif
#ifdef WITHPTHREADS
if ( e->status == INTOHIDELEXPRESSION ||
e->status == INTOHIDEGEXPRESSION ) {
SetHideFiles();
}
#endif
break;
case SKIPLEXPRESSION:
case SKIPGEXPRESSION:
/*
This can be greatly improved of course by file-to-file copy.
*/
#ifdef WITHMPI
if ( PF.me != MASTER ) break;
#endif
AR.GetFile = 0;
SetScratch(AR.infile,&(e->onfile));
if ( GetTerm(BHEAD term) <= 0 ) {
#ifdef HIDEDEBUG
MesPrint("Error condition 1b");
ExprStatus(e);
#endif
MesPrint("(3) Expression %d has problems in scratchfile",i);
retval = -1;
break;
}
term[3] = i;
AR.DeferFlag = 0;
SeekScratch(AR.outfile,&position);
e->onfile = position;
*AM.S0->sBuffer = 0; firstterm = -1;
do {
WORD *oldipointer = AR.CompressPointer;
WORD *comprtop = AR.ComprTop;
AR.ComprTop = AM.S0->sTop;
AR.CompressPointer = AM.S0->sBuffer;
if ( firstterm > 0 ) {
if ( PutOut(BHEAD term,&position,AR.outfile,1) < 0 ) goto ProcErr;
}
else if ( firstterm < 0 ) {
if ( PutOut(BHEAD term,&position,AR.outfile,0) < 0 ) goto ProcErr;
firstterm++;
}
else {
if ( PutOut(BHEAD term,&position,AR.outfile,-1) < 0 ) goto ProcErr;
firstterm++;
}
AR.CompressPointer = oldipointer;
AR.ComprTop = comprtop;
} while ( GetTerm(BHEAD term) );
if ( FlushOut(&position,AR.outfile,1) ) goto ProcErr;
UpdateMaxSize();
break;
case HIDELEXPRESSION:
case HIDEGEXPRESSION:
#ifdef WITHMPI
if ( PF.me != MASTER ) break;
#endif
AR.GetFile = 0;
SetScratch(AR.infile,&(e->onfile));
if ( GetTerm(BHEAD term) <= 0 ) {
#ifdef HIDEDEBUG
MesPrint("Error condition 1c");
ExprStatus(e);
#endif
MesPrint("(4) Expression %d has problems in scratchfile",i);
retval = -1;
break;
}
term[3] = i;
AR.DeferFlag = 0;
SetEndHScratch(AR.hidefile,&position);
e->onfile = position;
#ifdef HIDEDEBUG
if ( AR.hidefile->handle >= 0 ) {
POSITION possize,pos;
PUTZERO(possize);
PUTZERO(pos);
SeekFile(AR.hidefile->handle,&pos,SEEK_CUR);
SeekFile(AR.hidefile->handle,&possize,SEEK_END);
SeekFile(AR.hidefile->handle,&pos,SEEK_SET);
MesPrint("Processor Hide1: filesize(th) = %12p, filesize(ex) = %12p",&(position),
&(possize));
MesPrint(" in buffer: %l",(AR.hidefile->POfill-AR.hidefile->PObuffer)*sizeof(WORD));
}
#endif
*AM.S0->sBuffer = 0; firstterm = -1;
cbo = cpo = AM.S0->sBuffer;
do {
WORD *oldipointer = AR.CompressPointer;
WORD *oldibuffer = AR.CompressBuffer;
WORD *comprtop = AR.ComprTop;
AR.ComprTop = AM.S0->sTop;
AR.CompressPointer = cpo;
AR.CompressBuffer = cbo;
if ( firstterm > 0 ) {
if ( PutOut(BHEAD term,&position,AR.hidefile,1) < 0 ) goto ProcErr;
}
else if ( firstterm < 0 ) {
if ( PutOut(BHEAD term,&position,AR.hidefile,0) < 0 ) goto ProcErr;
firstterm++;
}
else {
if ( PutOut(BHEAD term,&position,AR.hidefile,-1) < 0 ) goto ProcErr;
firstterm++;
}
cpo = AR.CompressPointer;
cbo = AR.CompressBuffer;
AR.CompressPointer = oldipointer;
AR.CompressBuffer = oldibuffer;
AR.ComprTop = comprtop;
} while ( GetTerm(BHEAD term) );
#ifdef HIDEDEBUG
if ( AR.hidefile->handle >= 0 ) {
POSITION possize,pos;
PUTZERO(possize);
PUTZERO(pos);
SeekFile(AR.hidefile->handle,&pos,SEEK_CUR);
SeekFile(AR.hidefile->handle,&possize,SEEK_END);
SeekFile(AR.hidefile->handle,&pos,SEEK_SET);
MesPrint("Processor Hide2: filesize(th) = %12p, filesize(ex) = %12p",&(position),
&(possize));
MesPrint(" in buffer: %l",(AR.hidefile->POfill-AR.hidefile->PObuffer)*sizeof(WORD));
}
#endif
if ( FlushOut(&position,AR.hidefile,1) ) goto ProcErr;
AR.hidefile->POfull = AR.hidefile->POfill;
#ifdef HIDEDEBUG
if ( AR.hidefile->handle >= 0 ) {
POSITION possize,pos;
PUTZERO(possize);
PUTZERO(pos);
SeekFile(AR.hidefile->handle,&pos,SEEK_CUR);
SeekFile(AR.hidefile->handle,&possize,SEEK_END);
SeekFile(AR.hidefile->handle,&pos,SEEK_SET);
MesPrint("Processor Hide3: filesize(th) = %12p, filesize(ex) = %12p",&(position),
&(possize));
MesPrint(" in buffer: %l",(AR.hidefile->POfill-AR.hidefile->PObuffer)*sizeof(WORD));
}
#endif
/*
Because we direct the e->onfile already to the hide file, we
need to change the status of the expression. Otherwise the use
of parts (or the whole) of the expression looks in the infile
while the position is that of the hide file.
We choose to get everything from the hide file. On average that
should give least file activity.
*/
if ( e->status == HIDELEXPRESSION ) {
e->status = HIDDENLEXPRESSION;
AS.OldOnFile[i] = e->onfile;
AS.OldNumFactors[i] = Expressions[i].numfactors;
}
if ( e->status == HIDEGEXPRESSION ) {
e->status = HIDDENGEXPRESSION;
AS.OldOnFile[i] = e->onfile;
AS.OldNumFactors[i] = Expressions[i].numfactors;
}
#ifdef WITHPTHREADS
SetHideFiles();
#endif
UpdateMaxSize();
break;
case DROPPEDEXPRESSION:
case DROPLEXPRESSION:
case DROPGEXPRESSION:
case DROPHLEXPRESSION:
case DROPHGEXPRESSION:
case STOREDEXPRESSION:
case HIDDENLEXPRESSION:
case HIDDENGEXPRESSION:
case SPECTATOREXPRESSION:
default:
break;
}
}
AR.KeptInHold = 0;
}
AR.DeferFlag = 0;
AT.WorkPointer = term;
#ifdef HIDEDEBUG
MesPrint("Status at the end of Processor (HideLevel = %d)",AC.HideLevel);
for ( i = 0; i < NumExpressions; i++ ) {
e = Expressions+i;
ExprStatus(e);
}
#endif
return(retval);
ProcErr:
AT.WorkPointer = term;
if ( AM.tracebackflag ) MesCall("Processor");
return(-1);
}
/*
#] Processor :
#[ TestSub : WORD TestSub(term,level)
*/
/**
* TestSub hunts for subexpression pointers.
* If one is found its power is given in AN.TeSuOut.
* and the returnvalue is 'expressionnumber'.
* If the expression number is negative it is an expression on disk.
*
* In addition this routine tries to locate subexpression pointers
* in functions. It also notices that action must be taken with any
* of the special functions.
*
* @param term The term in which TestSub hunts for potential action
* @param level The number of the 'level' in the compiler buffer.
* @return The number of the (sub)expression that was encountered.
*
* Other values that are returned are in AN.TeSuOut, AR.TePos, AT.TMbuff,
* AN.TeInFun, AN.Frozen, AT.TMaddr
*
* The level in the compiler buffer is more or less the number of the
* statement in the module. Hence it refers to the element in the lhs array.
*
* This routine is one of the most important routines in FORM.
*/
#define DONE(x) { retvalue = x; goto Done; }
WORD TestSub(PHEAD WORD *term, WORD level)
{
GETBIDENTITY
WORD *m, *t, *r, retvalue = 0, funflag, j, oldncmod, nexpr, *Tpattern = 0;
WORD *stop, *t1, *t2, funnum, wilds, tbufnum, stilldirty = 0;
NESTING n;
CBUF *C = cbuf+AT.ebufnum;
LONG isp, i;
TABLES T;
COMPARE oldcompareroutine = (COMPARE)(AR.CompareRoutine);
WORD oldsorttype = AR.SortType;
ReStart:
tbufnum = 0; i = 0; retvalue = 0;
AT.TMbuff = AM.rbufnum;
funflag = 0;
t = term;
r = t + *t - 1;
m = r - ABS(*r) + 1;
t++;
if ( t < m ) do {
if ( *t == SUBEXPRESSION ) {
/*
Subexpression encountered
There may be more than one.
The old strategy was to take the last.
A newer strategy was to take the lowest power first.
The current strategy is that we compute the number of terms
generated by this subexpression and take the minimum of that.
*/
#ifdef WHICHSUBEXPRESSION
WORD *tmin = t, AN.nbino;
/* LONG minval = MAXLONG; */
LONG minval = -1;
LONG mm, mnum1 = 1;
if ( AN.BinoScrat == 0 ) {
AN.BinoScrat = (UWORD *)Malloc1((AM.MaxTal+2)*sizeof(UWORD),"GetBinoScrat");
}
#endif
if ( t[3] ) {
r = t + t[1];
while ( AN.subsubveto == 0 &&
*r == SUBEXPRESSION && r < m && r[3] ) {
#ifdef WHICHSUBEXPRESSION
mnum1++;
#endif
if ( r[1] == t[1] && r[2] == t[2] && r[4] == t[4] ) {
j = t[1] - SUBEXPSIZE;
t1 = t + SUBEXPSIZE;
t2 = r + SUBEXPSIZE;
while ( j > 0 && *t1++ == *t2++ ) j--;
if ( j <= 0 ) {
t[3] += r[3];
if ( t[3] == 0 ) {
t1 = r + r[1];
t2 = term + *term;
*term -= r[1]+t[1];
r = t;
while ( t1 < t2 ) *r++ = *t1++;
goto ReStart;
}
else {
t1 = r + r[1];
t2 = term + *term;
*term -= r[1];
m -= r[1];
while ( t1 < t2 ) *r++ = *t1++;
r = t;
}
}
}
#ifdef WHICHSUBEXPRESSION
else if ( t[2] >= 0 ) {
/*
Compute Binom(numterms+power-1,power-1)
We need potentially long arithmetic.
That is why we had to allocate AN.BinoScrat
*/
if ( AN.last1 == t[3] && AN.last2 == cbuf[t[4]].NumTerms[t[2]] + t[3] - 1 ) {
if ( AN.last3 > minval ) {
minval = AN.last3; tmin = t;
}
}
else {
AN.last1 = t[3]; mm = AN.last2 = cbuf[t[4]].NumTerms[t[2]] + t[3] - 1;
if ( t[3] == 1 ) {
if ( mm > minval ) {
minval = mm; tmin = t;
}
}
else if ( t[3] > 0 ) {
if ( mm > MAXPOSITIVE ) goto TooMuch;
GetBinom(AN.BinoScrat,&AN.nbino,(WORD)mm,t[3]);
if ( AN.nbino > 2 ) goto TooMuch;
if ( AN.nbino == 2 ) {
mm = AN.BinoScrat[1];
mm = ( mm << BITSINWORD ) + AN.BinoScrat[0];
}
else if ( AN.nbino == 1 ) mm = AN.BinoScrat[0];
else mm = 0;
if ( mm > minval ) {
minval = mm; tmin = t;
}
}
AN.last3 = mm;
}
}
#endif
t = r;
r += r[1];
}
#ifdef WHICHSUBEXPRESSION
if ( mnum1 > 1 && t[2] >= 0 ) {
/*
To keep the flowcontrol simple we duplicate some code here
*/
if ( AN.last1 == t[3] && AN.last2 == cbuf[t[4]].NumTerms[t[2]] + t[3] - 1 ) {
if ( AN.last3 > minval ) {
minval = AN.last3; tmin = t;
}
}
else {
AN.last1 = t[3]; mm = AN.last2 = cbuf[t[4]].NumTerms[t[2]] + t[3] - 1;
if ( t[3] == 1 ) {
if ( mm > minval ) {
minval = mm; tmin = t;
}
}
else if ( t[3] > 0 ) {
if ( mm > MAXPOSITIVE ) {
/*
We will generate more terms than we can count
*/
TooMuch:;
MLOCK(ErrorMessageLock);
MesPrint("Attempt to generate more terms than FORM can count");
MUNLOCK(ErrorMessageLock);
Terminate(-1);
}
GetBinom(AN.BinoScrat,&AN.nbino,(WORD)mm,t[3]);
if ( AN.nbino > 2 ) goto TooMuch;
if ( AN.nbino == 2 ) {
mm = AN.BinoScrat[1];
mm = ( mm << BITSINWORD ) + AN.BinoScrat[0];
}
else if ( AN.nbino == 1 ) mm = AN.BinoScrat[0];
else mm = 0;
if ( mm > minval ) {
minval = mm; tmin = t;
}
}
AN.last3 = mm;
}
}
t = tmin;
#endif
/* AR.TePos = 0; */
AR.TePos = WORDDIF(t,term);
AT.TMbuff = t[4];
if ( t[4] == AM.dbufnum && (t+t[1]) < m && t[t[1]] == DOLLAREXPR2 ) {
if ( t[t[1]+2] < 0 ) AT.TMdolfac = -t[t[1]+2];
else { /* resolve the element number */
AT.TMdolfac = GetDolNum(BHEAD t+t[1],m)+1;
}
}
else AT.TMdolfac = 0;
if ( t[3] < 0 ) {
AN.TeInFun = 1;
AR.TePos = WORDDIF(t,term);
DONE(t[2])
}
else {
AN.TeInFun = 0;
AN.TeSuOut = t[3];
}
if ( t[2] < 0 ) {
AN.TeSuOut = -t[3];
DONE(-t[2])
}
DONE(t[2])
}
}
else if ( *t == EXPRESSION ) {
WORD *toTMaddr;
i = -t[2] - 1;
if ( t[3] < 0 ) {
AN.TeInFun = 1;
AR.TePos = WORDDIF(t,term);
DONE(i)
}
nexpr = t[3];
toTMaddr = m = AT.WorkPointer;
AN.Frozen = 0;
/*
We have to be very careful with respect to setting variables
like AN.TeInFun, because we may still call Generator and that
may change those variables. That is why we set them at the
last moment only.
*/
j = t[1];
AT.WorkPointer += j;
r = t;
NCOPY(m,r,j);
r = t + t[1];
t += SUBEXPSIZE;
while ( t < r ) {
if ( *t == FROMBRAC ) {
WORD *ttstop,*tttstop;
/*
Note: Convention is that wildcards are done
after the expression has been picked up. So
no wildcard substitutions are needed here.
*/
t += 2;
AN.Frozen = m = AT.WorkPointer;
/*
We should check now for subexpressions and if necessary
we substitute them. Keep in mind: only one term allowed!
In retrospect (26-jan-2010): take also functions that
have a dirty flag on
*/
j = *t; tttstop = t + j;
GETSTOP(t,ttstop);
*m++ = j; t++;
while ( t < ttstop ) {
if ( *t == SUBEXPRESSION ) break;
if ( *t >= FUNCTION && ( ( t[2] & DIRTYFLAG ) == DIRTYFLAG ) ) break;
j = t[1]; NCOPY(m,t,j);
}
if ( t < ttstop ) {
/*
We ran into a subexpression or a function with a
'dirty' argument. It could also be a $ or
just e[(a^2)*b]. In all cases we should evaluate
*/
while ( t < tttstop ) *m++ = *t++;
*AT.WorkPointer = m-AT.WorkPointer;
m = AT.WorkPointer;
AT.WorkPointer = m + *m;
NewSort(BHEAD0);
if ( Generator(BHEAD m,AR.Cnumlhs) ) {
LowerSortLevel(); goto EndTest;
}
if ( EndSort(BHEAD m,0) < 0 ) goto EndTest;
AN.Frozen = m;
if ( *m == 0 ) {
*m++ = 4; *m++ = 1; *m++ = 1; *m++ = 3;
}
else if ( m[*m] != 0 ) {
MLOCK(ErrorMessageLock);
MesPrint("Bracket specification in expression should be one single term");
MUNLOCK(ErrorMessageLock);
Terminate(-1);
}
else {
m += *m;
m -= ABS(m[-1]);
*m++ = 1; *m++ = 1; *m++ = 3;
*AN.Frozen = m - AN.Frozen;
}
}
else {
while ( t < tttstop ) *m++ = *t++;
*AT.WorkPointer = m-AT.WorkPointer;
m = AT.WorkPointer;
AT.WorkPointer = m + *m;
if ( Normalize(BHEAD m) ) {
MLOCK(ErrorMessageLock);
MesPrint("Error while picking up contents of bracket");
MUNLOCK(ErrorMessageLock);
Terminate(-1);
}
if ( !*m ) {
*m++ = 4; *m++ = 1; *m++ = 1; *m++ = 3;
}
else m += *m;
}
AT.WorkPointer = m;
break;
}
t += t[1];
}
AN.TeInFun = 0;
AR.TePos = 0;
AN.TeSuOut = nexpr;
AT.TMaddr = toTMaddr;
DONE(i)
}
else if ( *t >= FUNCTION ) {
if ( t[0] == EXPONENT ) {
if ( t[1] == FUNHEAD+4 && t[FUNHEAD] == -SYMBOL &&
t[FUNHEAD+2] == -SNUMBER && t[FUNHEAD+3] < MAXPOWER
&& t[FUNHEAD+3] > -MAXPOWER ) {
t[0] = SYMBOL;
t[1] = 4;
t[2] = t[FUNHEAD+1];
t[3] = t[FUNHEAD+3];
r = term + *term;
m = t + FUNHEAD+4;
t += 4;
while ( m < r ) *t++ = *m++;
*term = WORDDIF(t,term);
goto ReStart;
}
else if ( t[1] == FUNHEAD+ARGHEAD+11 && t[FUNHEAD] == ARGHEAD+9
&& t[FUNHEAD+ARGHEAD] == 9 && t[FUNHEAD+ARGHEAD+1] == DOTPRODUCT
&& t[FUNHEAD+ARGHEAD+8] == 3
&& t[FUNHEAD+ARGHEAD+7] == 1
&& t[FUNHEAD+ARGHEAD+6] == 1
&& t[FUNHEAD+ARGHEAD+5] == 1
&& t[FUNHEAD+ARGHEAD+9] == -SNUMBER
&& t[FUNHEAD+ARGHEAD+10] < MAXPOWER
&& t[FUNHEAD+ARGHEAD+10] > -MAXPOWER ) {
t[0] = DOTPRODUCT;
t[1] = 5;
t[2] = t[FUNHEAD+ARGHEAD+3];
t[3] = t[FUNHEAD+ARGHEAD+4];
t[4] = t[FUNHEAD+ARGHEAD+10];
r = term + *term;
m = t + FUNHEAD+ARGHEAD+11;
t += 5;
while ( m < r ) *t++ = *m++;
*term = WORDDIF(t,term);
goto ReStart;
}
}
funnum = *t;
if ( *t >= FUNCTION + WILDOFFSET ) funnum -= WILDOFFSET;
if ( *t == EXPONENT ) {
/*
Test whether the second argument is an integer
*/
r = t+FUNHEAD;
NEXTARG(r)
if ( *r == -SNUMBER && r[1] < MAXPOWER && r+2 == t+t[1] &&
t[FUNHEAD] > -FUNCTION && ( t[FUNHEAD] != -SNUMBER
|| t[FUNHEAD+1] != 0 ) && t[FUNHEAD] != ARGHEAD ) {
if ( r[1] == 0 ) {
if ( t[FUNHEAD] == -SNUMBER && t[FUNHEAD+1] == 0 ) {
MLOCK(ErrorMessageLock);
MesPrint("Encountered 0^0. Fatal error.");
MUNLOCK(ErrorMessageLock);
SETERROR(-1);
}
*t = DUMMYFUN;
/*
Now mark it clean to avoid further interference.
Normalize will remove this object.
*/
t[2] = 0;
}
else {
/* Note that the case 0^ is treated in Normalize */
t1 = AddRHS(AT.ebufnum,1);
m = t + FUNHEAD;
if ( *m > 0 ) {
m += ARGHEAD;
i = t[FUNHEAD] - ARGHEAD;
while ( (t1 + i + 10) > C->Top )
t1 = DoubleCbuffer(AT.ebufnum,t1,9);
while ( --i >= 0 ) *t1++ = *m++;
}
else {
if ( (t1 + 20) > C->Top )
t1 = DoubleCbuffer(AT.ebufnum,t1,10);
ToGeneral(m,t1,1);
t1 += *t1;
}
*t1++ = 0;
C->rhs[C->numrhs+1] = t1;
C->Pointer = t1;
/* No provisions yet for commuting objects */
C->CanCommu[C->numrhs] = 1;
*t++ = SUBEXPRESSION;
*t++ = SUBEXPSIZE;
*t++ = C->numrhs;
*t++ = r[1];
*t++ = AT.ebufnum;
#if SUBEXPSIZE > 5
Important: we may not have enough spots here
#endif
FILLSUB(t) /* Important: We have maybe only 5 spots! */
r += 2;
m = term + *term;
do { *t++ = *r++; } while ( r < m );
*term -= WORDDIF(r,t);
goto ReStart;
}
}
}
else if ( *t == SUMF1 || *t == SUMF2 ) {
/*
What we are looking for is:
1-st argument: Single symbol or index.
2-nd argument: Number.
3-rd argument: Number.
(4-th argument):Number.
One more argument.
This would activate the summation procedure.
Note that the initiated recursion here can be done
without upsetting the regular procedures.
*/
WORD *tstop, lcounter, lcmin, lcmax, lcinc;
tstop = t + t[1];
r = t+FUNHEAD;
if ( r+6 < tstop && r[2] == -SNUMBER && r[4] == -SNUMBER
&& ( ( r[0] == -SYMBOL )
|| ( r[0] == -INDEX && r[1] >= AM.OffsetIndex
&& r[3] >= 0 && r[3] < AM.OffsetIndex
&& r[5] >= 0 && r[5] < AM.OffsetIndex ) ) ) {
lcounter = r[0] == -INDEX ? -r[1]: r[1]; /* The loop counter */
lcmin = r[3];
lcmax = r[5];
r += 6;
if ( *r == -SNUMBER && r+2 < tstop ) {
lcinc = r[1];
r += 2;
}
else lcinc = 1;
if ( r < tstop && ( ( *r > 0 && (r+*r) == tstop )
|| ( *r <= -FUNCTION && r+1 == tstop )
|| ( *r > -FUNCTION && *r < 0 && r+2 == tstop ) ) ) {
m = AddRHS(AT.ebufnum,1);
if ( *r > 0 ) {
i = *r - ARGHEAD;
r += ARGHEAD;
while ( (m + i + 10) > C->Top )
m = DoubleCbuffer(AT.ebufnum,m,11);
while ( --i >= 0 ) *m++ = *r++;
}
else {
while ( (m + 20) > C->Top )
m = DoubleCbuffer(AT.ebufnum,m,12);
ToGeneral(r,m,1);
m += *m;
}
*m++ = 0;
C->rhs[C->numrhs+1] = m;
C->Pointer = m;
m = AT.TMout;
*m++ = 6;
if ( *t == SUMF1 ) *m++ = SUMNUM1;
else *m++ = SUMNUM2;
*m++ = lcounter;
*m++ = lcmin;
*m++ = lcmax;
*m++ = lcinc;
m = t + t[1];
r = C->rhs[C->numrhs];
/*
Test now if the argument was already evaluated.
In that case it needs a new subexpression prototype.
In either case we replace the function now by a
subexpression prototype.
*/
if ( *r >= (SUBEXPSIZE+4)
&& ABS(*(r+*r-1)) < (*r - 1)
&& r[1] == SUBEXPRESSION ) {
r++;
i = r[1] - 5;
*t++ = *r++; *t++ = *r++; *t++ = C->numrhs;
r++; *t++ = *r++; *t++ = AT.ebufnum; r++;
while ( --i >= 0 ) *t++ = *r++;
}
else {
*t++ = SUBEXPRESSION;
*t++ = 4+SUBEXPSIZE;
*t++ = C->numrhs;
*t++ = 1;
*t++ = AT.ebufnum;
FILLSUB(t)
if ( lcounter < 0 ) {
*t++ = INDTOIND;
*t++ = 4;
*t++ = -lcounter;
}
else {
*t++ = SYMTONUM;
*t++ = 4;
*t++ = lcounter;
}
*t++ = lcmin;
}
t2 = term + *term;
while ( m < t2 ) *t++ = *m++;
*term = WORDDIF(t,term);
AN.TeInFun = -C->numrhs;
AR.TePos = 0;
AN.TeSuOut = 0;
AT.TMbuff = AT.ebufnum;
DONE(C->numrhs)
}
}
}
else if ( *t == TOPOLOGIES ) {
MesPrint("&The topologies_ function was removed in FORM 5.0.");
MesPrint("&See the TopologiesOnly_ option of diagrams_.");
Terminate(-1);
}
else if ( *t == DIAGRAMS ) {
/*
Syntax:
diagrams_(model,setinparticles,setoutparticles,
setextmomenta,setintmomenta,couplings or loops)
*/
if ( AC.nummodels > 0 ) { /* No model, no diagrams */
t1 = t+FUNHEAD; t2 = t+t[1];
if (
t1[0] == -SETSET && Sets[t1[1]].type == CMODEL &&
t1[2] == -SETSET && ( Sets[t1[3]].type == CFUNCTION
|| ( Sets[t1[3]].type == ANYTYPE && ( Sets[t1[3]].first == Sets[t1[3]].last ) ) ) &&
t1[4] == -SETSET && ( Sets[t1[5]].type == CFUNCTION
|| ( Sets[t1[5]].type == ANYTYPE && ( Sets[t1[5]].first == Sets[t1[5]].last ) ) ) &&
t1[6] == -SETSET && ( Sets[t1[7]].type == CVECTOR
|| ( Sets[t1[7]].type == ANYTYPE && ( Sets[t1[7]].first == Sets[t1[7]].last ) ) ) &&
t1[8] == -SETSET && ( Sets[t1[9]].type == CVECTOR
|| ( Sets[t1[9]].type == ANYTYPE && ( Sets[t1[9]].first == Sets[t1[9]].last ) ) ) &&
t1+12 <= t2 ) {
/*
Test that the sets of particles correspond to particles
of the set model.
*/
MODEL *m = AC.models[SetElements[Sets[t1[1]].first]];
int nn0,nn1,nn2;
for ( nn0 = 3; nn0 <= 5; nn0 += 2 ) {
for ( nn1 = Sets[t1[nn0]].first; nn1 < Sets[t1[nn0]].last; nn1++ ) {
for ( nn2 = 0; nn2 < m->nparticles; nn2++ ) {
if ( m->vertices[nn2]->particles[0].number == SetElements[nn1]
|| m->vertices[nn2]->particles[1].number == SetElements[nn1] ) break;
}
if ( nn2 >= m->nparticles ) goto doesnotwork;
}
}
/*
Now test for a single argument indicating the order
in perturbation theory.
*/
if ( ( t1[10] == -SNUMBER && t1[11] >= 0 && t1+12 == t2 )
|| ( t1[10] == -SYMBOL && t1+12 == t2 )
|| ( t1+10+t1[10] == t2 && t1+10+ARGHEAD+t1[10+ARGHEAD] == t2
&& t1[11+ARGHEAD] == SYMBOL ) ) {
/*
Now test that all symbols are valid coupling constants.
*/
if ( t1+12 > t2 ) {
WORD *tt1 = t1+13+ARGHEAD, im;
t2 -= ABS(t2[-1]);
while ( tt1 < t2 ) {
for ( im = 0; im < m->ncouplings; im++ ) {
if ( *tt1 == m->couplings[im] ) break;
}
if ( im >= m->ncouplings ) goto doesnotwork;
tt1 += 2;
}
}
AN.TeInFun = -15;
AN.TeSuOut = 0;
AR.TePos = -1;
AR.funoffset = t - term;
DONE(1)
}
else if ( ( ( t1[10] == -SNUMBER && t1[11] >= 0 && t1+12 == t2-2 )
|| ( t1[10] == -SYMBOL && t1+12 == t2-2 )
|| ( t1+10+t1[10] == t2-2 && t1+10+ARGHEAD+t1[10+ARGHEAD] == t2-2
&& t1[11+ARGHEAD] == SYMBOL ) ) && t2[-2] == -SNUMBER ) {
/*
With options at t2[-2],t2[-1]
Now test that all symbols are valid coupling constants.
*/
t2 -= 2;
if ( t1+12 > t2 ) {
WORD *tt1 = t1+13+ARGHEAD, im;
t2 -= ABS(t2[-1]);
while ( tt1 < t2 ) {
for ( im = 0; im < m->ncouplings; im++ ) {
if ( *tt1 == m->couplings[im] ) break;
}
if ( im >= m->ncouplings ) goto doesnotwork;
tt1 += 2;
}
}
AN.TeInFun = -15;
AN.TeSuOut = 0;
AR.TePos = -1;
AR.funoffset = t - term;
DONE(1)
}
doesnotwork:;
}
}
}
if ( functions[funnum-FUNCTION].spec <= 0
|| ( t[2] & (DIRTYFLAG|MUSTCLEANPRF) ) != 0 ) {
funflag = 1;
}
if ( *t <= MAXBUILTINFUNCTION ) {
if ( *t <= DELTAP && *t >= THETA ) { /* Speeds up by 2 or 3 compares */
if ( *t == THETA || *t == THETA2 ) {
WORD *tstop, *tt2, kk;
tstop = t + t[1];
tt2 = t + FUNHEAD;
while ( tt2 < tstop ) {
if ( *tt2 > 0 && tt2[1] != 0 ) goto DoSpec;
NEXTARG(tt2)
}
if ( !AT.RecFlag ) {
if ( ( kk = DoTheta(BHEAD t) ) == 0 ) {
*term = 0;
DONE(0)
}
else if ( kk > 0 ) {
m = t + t[1];
r = term + *term;
while ( m < r ) *t++ = *m++;
*term = WORDDIF(t,term);
goto ReStart;
}
}
}
else if ( *t == DELTA2 || *t == DELTAP ) {
WORD *tstop, *tt2, kk;
tstop = t + t[1];
tt2 = t + FUNHEAD;
while ( tt2 < tstop ) {
if ( *tt2 > 0 && tt2[1] != 0 ) goto DoSpec;
NEXTARG(tt2)
}
if ( !AT.RecFlag ) {
if ( ( kk = DoDelta(t) ) == 0 ) {
*term = 0;
DONE(0)
}
else if ( kk > 0 ) {
m = t + t[1];
r = term + *term;
while ( m < r ) *t++ = *m++;
*term = WORDDIF(t,term);
goto ReStart;
}
}
} }
else if ( *t == DISTRIBUTION && t[FUNHEAD] == -SNUMBER
&& t[FUNHEAD+1] >= -2 && t[FUNHEAD+1] <= 2
&& t[FUNHEAD+2] == -SNUMBER
&& t[FUNHEAD+4] <= -FUNCTION
&& t[FUNHEAD+5] <= -FUNCTION ) {
WORD *ttt = t+FUNHEAD+6, *tttstop = t+t[1];
while ( ttt < tttstop ) {
if ( *ttt == -DOLLAREXPRESSION ) break;
NEXTARG(ttt);
}
if ( ttt >= tttstop ) {
AN.TeInFun = -1;
AN.TeSuOut = 0;
AR.TePos = -1;
DONE(1)
}
}
else if ( *t == DELTA3 && ((t[1]-FUNHEAD) & 1 ) == 0 ) {
AN.TeInFun = -2;
AN.TeSuOut = 0;
AR.TePos = -1;
DONE(1)
}
else if ( ( *t == TABLEFUNCTION ) && ( t[FUNHEAD] <= -FUNCTION )
&& ( T = functions[-t[FUNHEAD]-FUNCTION].tabl ) != 0
&& ( t[1] >= FUNHEAD+1+2*ABS(T->numind) )
&& ( t[FUNHEAD+1] == -SYMBOL ) ) {
/*
The case of table_(tab,sym1,...,symn)
*/
for ( isp = 0; isp < ABS(T->numind); isp++ ) {
if ( t[FUNHEAD+1+2*isp] != -SYMBOL ) break;
}
if ( isp >= ABS(T->numind) ) {
AN.TeInFun = -3;
AN.TeSuOut = 0;
AR.TePos = -1;
DONE(1)
}
}
else if ( *t == TABLEFUNCTION && t[FUNHEAD] <= -FUNCTION
&& ( T = functions[-t[FUNHEAD]-FUNCTION].tabl ) != 0
&& ( t[1] == FUNHEAD+2 )
&& ( t[FUNHEAD+1] <= -FUNCTION ) ) {
/*
The case of table_(tab,fun)
*/
AN.TeInFun = -3;
AN.TeSuOut = 0;
AR.TePos = -1;
DONE(1)
}
else if ( *t == FACTORIN ) {
if ( t[1] == FUNHEAD+2 && t[FUNHEAD] == -DOLLAREXPRESSION ) {
AN.TeInFun = -4;
AN.TeSuOut = 0;
AR.TePos = -1;
DONE(1)
}
else if ( t[1] == FUNHEAD+2 && t[FUNHEAD] == -EXPRESSION ) {
AN.TeInFun = -5;
AN.TeSuOut = 0;
AR.TePos = -1;
DONE(1)
}
}
else if ( *t == TERMSINBRACKET ) {
if ( t[1] == FUNHEAD || (
t[1] == FUNHEAD+2
&& t[FUNHEAD] == -SNUMBER
&& t[FUNHEAD+1] == 0
) ) {
AN.TeInFun = -6;
AN.TeSuOut = 0;
AR.TePos = -1;
DONE(1)
}
/*
The other cases have not yet been implemented
We still have to add the case of short arguments
First the different bracket in same expression
else if ( t[1] > FUNHEAD+ARGHEAD
&& t[FUNHEAD] == t[1]-FUNHEAD
&& t[FUNHEAD+ARGHEAD] == t[1]-FUNHEAD-ARGHEAD
&& t[t[1]-1] == 3
&& t[t[1]-2] == 1
&& t[t[1]-3] == 1 ) {
AN.TeInFun = -6;
AN.TeSuOut = 0;
AR.TePos = -1;
DONE(1)
}
Next the bracket in an other expression
else if ( t[1] > FUNHEAD+ARGHEAD+2
&& t[FUNHEAD] == -EXPRESSION
&& t[FUNHEAD+2] == t[1]-FUNHEAD-2
&& t[FUNHEAD+ARGHEAD+2] == t[1]-FUNHEAD-ARGHEAD-2
&& t[t[1]-1] == 3
&& t[t[1]-2] == 1
&& t[t[1]-3] == 1 ) {
AN.TeInFun = -6;
AN.TeSuOut = 0;
AR.TePos = -1;
DONE(1)
}
*/
}
else if ( *t == EXTRASYMFUN ) {
if ( t[1] == FUNHEAD+2 && (
( t[FUNHEAD] == -SNUMBER && t[FUNHEAD+1] <= cbuf[AM.sbufnum].numrhs
&& t[FUNHEAD+1] > 0 ) ||
( t[FUNHEAD] == -SYMBOL && t[FUNHEAD+1] < MAXVARIABLES
&& t[FUNHEAD+1] >= MAXVARIABLES-cbuf[AM.sbufnum].numrhs ) ) ) {
AN.TeInFun = -7;
AN.TeSuOut = 0;
AR.TePos = -1;
DONE(1)
}
else if ( t[1] == FUNHEAD ) {
AN.TeInFun = -7;
AN.TeSuOut = 0;
AR.TePos = -1;
DONE(1)
}
}
else if ( *t == DIVFUNCTION || *t == REMFUNCTION
|| *t == INVERSEFUNCTION || *t == MULFUNCTION
|| *t == GCDFUNCTION ) {
WORD *tf;
int todo = 1, numargs = 0;
tf = t + FUNHEAD;
while ( tf < t + t[1] ) {
DOLLARS d;
if ( *tf == -DOLLAREXPRESSION ) {
d = Dollars + tf[1];
if ( d->type == DOLWILDARGS ) {
WORD *tterm = AT.WorkPointer, *tw;
WORD *ta = term, *tb = tterm, *tc, *td = term + *term;
while ( ta < t ) *tb++ = *ta++;
tc = tb;
while ( ta < tf ) *tb++ = *ta++;
tw = d->where+1;
while ( *tw ) {
if ( *tw < 0 ) {
if ( *tw > -FUNCTION ) *tb++ = *tw++;
*tb++ = *tw++;
}
else {
int ia;
for ( ia = 0; ia < *tw; ia++ ) *tb++ = *tw++;
}
}
NEXTARG(ta)
while ( ta < t+t[1] ) *tb++ = *ta++;
tc[1] = tb-tc;
while ( ta < td ) *tb++ = *ta++;
*tterm = tb - tterm;
{
int ia, na = *tterm;
ta = tterm; tb = term;
for ( ia = 0; ia < na; ia++ ) *tb++ = *ta++;
}
if ( tb > AT.WorkTop ) {
MLOCK(ErrorMessageLock);
MesWork();
goto EndTest2;
}
AT.WorkPointer = tb;
goto ReStart;
}
}
NEXTARG(tf);
}
tf = t + FUNHEAD;
while ( tf < t + t[1] ) {
numargs++;
if ( *tf > 0 && tf[1] != 0 ) todo = 0;
NEXTARG(tf);
}
if ( todo && numargs == 2 ) {
if ( *t == DIVFUNCTION ) AN.TeInFun = -9;
else if ( *t == REMFUNCTION ) AN.TeInFun = -10;
else if ( *t == INVERSEFUNCTION ) AN.TeInFun = -11;
else if ( *t == MULFUNCTION ) AN.TeInFun = -14;
else if ( *t == GCDFUNCTION ) AN.TeInFun = -8;
AN.TeSuOut = 0;
AR.TePos = -1;
DONE(1)
}
else if ( todo && numargs == 3 ) {
if ( *t == DIVFUNCTION ) AN.TeInFun = -9;
else if ( *t == REMFUNCTION ) AN.TeInFun = -10;
else if ( *t == GCDFUNCTION ) AN.TeInFun = -8;
AN.TeSuOut = 0;
AR.TePos = -1;
DONE(1)
}
else if ( todo && *t == GCDFUNCTION ) {
AN.TeInFun = -8;
AN.TeSuOut = 0;
AR.TePos = -1;
DONE(1)
}
}
else if ( *t == PERMUTATIONS && ( ( t[1] >= FUNHEAD+1
&& t[FUNHEAD] <= -FUNCTION ) || ( t[1] >= FUNHEAD+3
&& t[FUNHEAD] == -SNUMBER && t[FUNHEAD+2] <= -FUNCTION ) ) ) {
AN.TeInFun = -12;
AN.TeSuOut = 0;
AR.TePos = -1;
DONE(1)
}
else if ( *t == PARTITIONS ) {
if ( TestPartitions(t,&(AT.partitions)) ) {
AT.partitions.where = t-term;
AN.TeInFun = -13;
AN.TeSuOut = 0;
AR.TePos = -1;
DONE(1)
}
}
}
}
t += t[1];
} while ( t < m );
if ( funflag ) { /* Search in functions */
DoSpec:
t = term;
AT.NestPoin->termsize = t;
if ( AT.NestPoin == AT.Nest ) AN.EndNest = t + *t;
t++;
oldncmod = AN.ncmod;
if ( t < m ) do {
if ( *t < FUNCTION ) {
t += t[1]; continue;
}
if ( AN.ncmod && ( ( AC.modmode & ALSOFUNARGS ) == 0 ) ) {
if ( *t != AR.PolyFun ) AN.ncmod = 0;
else AN.ncmod = oldncmod;
}
r = t + t[1];
funnum = *t;
if ( *t >= FUNCTION + WILDOFFSET ) funnum -= WILDOFFSET;
if ( ( *t == NUMFACTORS || *t == FIRSTTERM || *t == CONTENTTERM )
&& t[1] == FUNHEAD+2 &&
( t[FUNHEAD] == -EXPRESSION || t[FUNHEAD] == -DOLLAREXPRESSION ) ) {
/*
if ( *t == NUMFACTORS ) {
This we leave for Normalize
}
*/
}
else if ( functions[funnum-FUNCTION].spec <= 0 ) {
AT.NestPoin->funsize = t + 1;
t1 = t;
t += FUNHEAD;
while ( t < r ) { /* Sum over arguments */
if ( *t > 0 && t[1] ) { /* Argument is dirty */
AT.NestPoin->argsize = t;
AT.NestPoin++;
/* stop = t + *t; */
t2 = t;
t += ARGHEAD;
while ( t < AT.NestPoin[-1].argsize+*(AT.NestPoin[-1].argsize) ) {
/* Sum over terms */
AT.RecFlag++;
i = *t;
AN.subsubveto = 1;
/*
AN.subsubveto repairs a bug that became apparent
in an example by York Schroeder:
f(k1.k1)*replace_(k1,2*k2)
Is it possible to repair the counting of the various
length indicators? (JV 1-jun-2010)
*/
if ( ( retvalue = TestSub(BHEAD t,level) ) != 0 ) {
/*
Possible size changes:
Note defs at 471,467,460,400,425,328
*/
redosize:
if ( i > *t ) {
/*
i -= *t;
*t2 -= i;
t1[1] -= i;
t += *t;
r = t + i;
m = term + *term;
while ( r < m ) *t++ = *r++;
*term -= i;
*/
i -= *t;
t += *t;
r = t + i;
m = AN.EndNest;
while ( r < m ) *t++ = *r++;
t = AT.NestPoin[-1].argsize + ARGHEAD;
n = AT.Nest;
while ( n < AT.NestPoin ) {
*(n->argsize) -= i;
*(n->funsize) -= i;
*(n->termsize) -= i;
n++;
}
AN.EndNest -= i;
}
AN.subsubveto = 0;
t1[2] = 1;
if ( *t1 == AR.PolyFun && AR.PolyFunType == 2 )
t1[2] |= MUSTCLEANPRF;
AT.RecFlag--;
AT.NestPoin--;
AN.TeInFun++;
AR.TePos = 0;
AN.ncmod = oldncmod;
DONE(retvalue)
}
else {
/*
* Somehow the next line fixes Issue #106.
*/
i = *t;
Normalize(BHEAD t);
/* if ( i > *t ) { retvalue = 1; goto redosize; } */
/*
* Experimentally, the next line fixes Issue #105.
*/
if ( *t == 0 ) { retvalue = 1; goto redosize; }
{
WORD *tend = t + *t, *tt = t+1;
stilldirty = 0;
tend -= ABS(tend[-1]);
while ( tt < tend ) {
if ( *tt == SUBEXPRESSION || *tt == EXPRESSION ) {
stilldirty = 1; break;
}
tt += tt[1];
}
}
if ( i > *t ) {
/*
We should not forget to correct the Nest
stack. That caused trouble in the past.
*/
retvalue = 1;
i -= *t;
t += *t;
r = t + i;
m = AN.EndNest;
while ( r < m ) *t++ = *r++;
t = AT.NestPoin[-1].argsize + ARGHEAD;
n = AT.Nest;
while ( n < AT.NestPoin ) {
*(n->argsize) -= i;
*(n->funsize) -= i;
*(n->termsize) -= i;
n++;
}
AN.EndNest -= i;
}
}
AN.subsubveto = 0;
AT.RecFlag--;
t += *t;
}
AT.NestPoin--;
/*
Argument contains no subexpressions.
It should be normalized and sorted.
The main problem is the storage.
*/
t = AT.NestPoin->argsize;
j = *t;
t += ARGHEAD;
NewSort(BHEAD0);
if ( *t1 == AR.PolyFun && AR.PolyFunType == 2 ) {
AR.CompareRoutine = (COMPAREDUMMY)(&CompareSymbols);
AR.SortType = SORTHIGHFIRST;
}
if ( AT.WorkPointer < term + *term )
AT.WorkPointer = term + *term;
while ( t < AT.NestPoin->argsize+*(AT.NestPoin->argsize) ) {
m = AT.WorkPointer;
r = t + *t;
do { *m++ = *t++; } while ( t < r );
r = AT.WorkPointer;
AT.WorkPointer = r + *r;
if ( Normalize(BHEAD r) ) {
if ( *t1 == AR.PolyFun && AR.PolyFunType == 2 ) {
AR.SortType = oldsorttype;
AR.CompareRoutine = (COMPAREDUMMY)oldcompareroutine;
t1[2] |= MUSTCLEANPRF;
}
LowerSortLevel(); goto EndTest;
}
if ( AN.ncmod != 0 ) {
if ( *r ) {
if ( Modulus(r) ) {
LowerSortLevel();
AT.WorkPointer = r;
if ( *t1 == AR.PolyFun && AR.PolyFunType == 2 ) {
AR.SortType = oldsorttype;
AR.CompareRoutine = (COMPAREDUMMY)oldcompareroutine;
t1[2] |= MUSTCLEANPRF;
}
goto EndTest;
}
}
}
if ( AR.PolyFun > 0 ) {
if ( PrepPoly(BHEAD r,1) != 0 ) goto EndTest;
}
if ( *r ) StoreTerm(BHEAD r);
AT.WorkPointer = r;
}
if ( EndSort(BHEAD AT.WorkPointer+ARGHEAD,1) < 0 ) goto EndTest;
m = AT.WorkPointer+ARGHEAD;
if ( *t1 == AR.PolyFun && AR.PolyFunType == 2 ) {
AR.SortType = oldsorttype;
AR.CompareRoutine = (COMPAREDUMMY)oldcompareroutine;
t1[2] |= MUSTCLEANPRF;
}
while ( *m ) m += *m;
i = WORDDIF(m,AT.WorkPointer);
*AT.WorkPointer = i;
AT.WorkPointer[1] = stilldirty;
if ( ToFast(AT.WorkPointer,AT.WorkPointer) ) {
m = AT.WorkPointer;
if ( *m <= -FUNCTION ) { m++; i = 1; }
else { m += 2; i = 2; }
}
j = i - j;
if ( j > 0 ) {
r = m + j;
if ( r > AT.WorkTop ) {
MLOCK(ErrorMessageLock);
MesWork();
goto EndTest2;
}
do { *--r = *--m; } while ( m > AT.WorkPointer );
AT.WorkPointer = r;
m = AN.EndNest;
r = m + j;
stop = AT.NestPoin->argsize+*(AT.NestPoin->argsize);
do { *--r = *--m; } while ( m >= stop );
}
else if ( j < 0 ) {
m = AT.NestPoin->argsize+*(AT.NestPoin->argsize);
r = m + j;
do { *r++ = *m++; } while ( m < AN.EndNest );
}
m = AT.NestPoin->argsize;
r = AT.WorkPointer;
while ( --i >= 0 ) *m++ = *r++;
n = AT.Nest;
while ( n <= AT.NestPoin ) {
if ( *(n->argsize) > 0 && n != AT.NestPoin )
*(n->argsize) += j;
*(n->funsize) += j;
*(n->termsize) += j;
n++;
}
AN.EndNest += j;
/* (AT.NestPoin->argsize)[1] = 0; */
if ( funnum == DENOMINATOR || funnum == EXPONENT ) {
if ( Normalize(BHEAD term) ) {
/*
In this case something has been substituted
Either a $ or a replace_?????
Originally we had here:
goto EndTest;
It seems better to restart.
*/
AN.ncmod = oldncmod;
goto ReStart;
}
/*
And size changes here?????
*/
}
AN.ncmod = oldncmod;
goto ReStart;
}
else if ( *t == -DOLLAREXPRESSION ) {
if ( ( *t1 == TERMSINEXPR || *t1 == SIZEOFFUNCTION )
&& t1[1] == FUNHEAD+2 ) {}
else {
if ( AR.Eside != LHSIDE ) {
AN.TeInFun = 1; AR.TePos = 0;
AT.TMbuff = AM.dbufnum; t1[2] |= DIRTYFLAG;
AN.ncmod = oldncmod;
DONE(1)
}
AC.lhdollarflag = 1;
}
}
else if ( *t == -TERMSINBRACKET ) {
if ( AR.Eside != LHSIDE ) {
AN.TeInFun = 1; AR.TePos = 0;
t1[2] |= DIRTYFLAG;
AN.ncmod = oldncmod;
DONE(1)
}
}
else if ( AN.ncmod != 0 && *t == -SNUMBER ) {
if ( AN.ncmod == 1 || AN.ncmod == -1 ) {
isp = (UWORD)(AC.cmod[0]);
isp = t[1] % isp;
if ( ( AC.modmode & POSNEG ) != 0 ) {
if ( isp > (UWORD)(AC.cmod[0])/2 ) isp = isp - (UWORD)(AC.cmod[0]);
else if ( -isp > (UWORD)(AC.cmod[0])/2 ) isp = isp + (UWORD)(AC.cmod[0]);
}
else {
if ( isp < 0 ) isp += (UWORD)(AC.cmod[0]);
}
if ( isp <= MAXPOSITIVE && isp >= -MAXPOSITIVE ) {
t[1] = isp;
}
}
}
NEXTARG(t)
}
if ( funnum >= FUNCTION && functions[funnum-FUNCTION].tabl ) {
/*
Test whether the table catches
Test 1: index arguments and range. i will be the number
of the element in the table.
*/
WORD rhsnumber, *oldwork = AT.WorkPointer;
WORD ii, *p, *pp, *ppstop;
MINMAX *mm;
T = functions[funnum-FUNCTION].tabl;
/*
Because of tables with a variable number of indices
we need to make a copy of the pattern.
If we do this in the WorkSpace we get problems with EndNest.
This is why we use TermMalloc.
Now Tpattern is a copy that can be modified.
*/
#ifdef WITHPTHREADS
pp = T->pattern[AT.identity];
#else
pp = T->pattern;
#endif
if ( Tpattern == 0 ) Tpattern = TermMalloc("Tpattern");
p = Tpattern;
ii = pp[1];
for ( i = 0; i < ii; i++ ) *p++ = *pp++;
p = Tpattern + FUNHEAD+1;
mm = T->mm;
if ( T->sparse ) {
WORD xx;
t = t1+FUNHEAD;
if ( T->numind == 0 ) { isp = 0; xx = 0; }
else {
if ( T->numind < 0 ) {
if ( *t != -SNUMBER && t[2] != -SNUMBER ) {
xx = ABS(T->numind);
goto teststrict;
}
xx = t[1]+1;
if ( xx < 2 || xx > -T->numind ) goto teststrict;
}
else xx = T->numind;
for ( i = 0; i < xx; i++, t += 2 ) {
if ( *t != -SNUMBER ) break;
}
if ( i < xx ) goto teststrict;
isp = FindTableTree(T,t1+FUNHEAD,2);
}
if ( isp < 0 ) {
teststrict: if ( T->strict == -2 ) {
rhsnumber = AM.zerorhs;
tbufnum = AM.zbufnum;
}
else if ( T->strict == -3 ) {
rhsnumber = AM.onerhs;
tbufnum = AM.zbufnum;
}
else if ( T->strict < 0 ) goto NextFun;
else {
MLOCK(ErrorMessageLock);
MesPrint("Element in table is undefined");
if ( Tpattern ) {
TermFree(Tpattern,"Tpattern");
Tpattern = 0;
}
goto showtable;
}
/*
Copy the indices;
*/
t = t1+FUNHEAD+1;
for ( i = 0; i < xx; i++ ) {
*p = *t; p+=2; t+=2;
}
}
else {
rhsnumber = T->tablepointers[isp+ABS(T->numind)];
#if ( TABLEEXTENSION == 2 )
tbufnum = T->bufnum;
#else
tbufnum = T->tablepointers[isp+ABS(T->numind)+1];
#endif
t = t1+FUNHEAD+1;
ii = xx;
while ( --ii >= 0 ) {
*p = *t; t += 2; p += 2;
}
}
if ( xx < ABS(T->numind) ) {
p--; ppstop = Tpattern+Tpattern[1];
pp = p+2*(-T->numind-xx);
while ( pp < ppstop ) *p++ = *pp++;
Tpattern[1] = p - Tpattern;
}
goto caughttable;
}
else {
i = 0;
t = t1 + FUNHEAD;
j = T->numind;
while ( --j >= 0 ) {
if ( *t != -SNUMBER ) goto NextFun;
t++;
if ( *t < mm->mini || *t > mm->maxi ) {
if ( T->bounds ) {
MLOCK(ErrorMessageLock);
MesPrint("Table boundary check. Argument %d",
T->numind-j);
showtable: AO.OutFill = AO.OutputLine = (UBYTE *)m;
AO.OutSkip = 8;
IniLine(0);
WriteSubTerm(t1,1);
FiniLine();
MUNLOCK(ErrorMessageLock);
if ( Tpattern ) {
TermFree(Tpattern,"Tpattern");
Tpattern = 0;
}
SETERROR(-1)
}
if ( Tpattern ) {
TermFree(Tpattern,"Tpattern");
Tpattern = 0;
}
goto NextFun;
}
i += ( *t - mm->mini ) * (LONG)(mm->size);
*p = *t++;
p += 2;
mm++;
}
/*
Test now whether the entry exists.
*/
i *= TABLEEXTENSION;
if ( T->tablepointers[i] == -1 ) {
if ( T->strict == -2 ) {
rhsnumber = AM.zerorhs;
tbufnum = AM.zbufnum;
}
else if ( T->strict == -3 ) {
rhsnumber = AM.onerhs;
tbufnum = AM.zbufnum;
}
else if ( T->strict < 0 ) {
if ( Tpattern ) {
TermFree(Tpattern,"Tpattern");
Tpattern = 0;
}
goto NextFun;
}
else {
MLOCK(ErrorMessageLock);
MesPrint("Element in table is undefined");
if ( Tpattern ) {
TermFree(Tpattern,"Tpattern");
Tpattern = 0;
}
goto showtable;
}
}
else {
rhsnumber = T->tablepointers[i];
#if ( TABLEEXTENSION == 2 )
tbufnum = T->bufnum;
#else
tbufnum = T->tablepointers[i+1];
#endif
}
}
/*
If there are more arguments we have to do some
pattern matching. This should be easy. We adapted the
pattern, so that the array indices match already.
Note that if there is no match the program will become
very slow.
*/
caughttable:
#ifdef WITHPTHREADS
AN.FullProto = T->prototype[AT.identity];
#else
AN.FullProto = T->prototype;
#endif
AN.WildValue = AN.FullProto + SUBEXPSIZE;
AN.WildStop = AN.FullProto+AN.FullProto[1];
ClearWild(BHEAD0);
AN.RepFunNum = 0;
AN.RepFunList = AN.EndNest;
AT.WorkPointer = (WORD *)(((UBYTE *)(AN.EndNest)) + AM.MaxTer/2);
if ( AT.WorkPointer >= AT.WorkTop ) {
MLOCK(ErrorMessageLock);
MesWork();
MUNLOCK(ErrorMessageLock);
}
wilds = 0;
if ( MatchFunction(BHEAD Tpattern,t1,&wilds) > 0 ) {
AT.WorkPointer = oldwork;
if ( AT.NestPoin != AT.Nest ) {
AN.ncmod = oldncmod;
if ( Tpattern ) {
TermFree(Tpattern,"Tpattern");
Tpattern = 0;
}
DONE(1)
}
m = AN.FullProto;
retvalue = m[2] = rhsnumber;
m[4] = tbufnum;
t = t1;
j = t[1];
i = m[1];
if ( j > i ) {
j = i - j;
NCOPY(t,m,i);
m = term + *term;
while ( r < m ) *t++ = *r++;
*term += j;
}
else if ( j < i ) {
j = i-j;
t = term + *term;
while ( t >= r ) { t[j] = *t; t--; }
t = t1;
NCOPY(t,m,i);
*term += j;
}
else {
NCOPY(t,m,j);
}
AN.TeInFun = 0;
AR.TePos = 0;
AN.TeSuOut = -1;
if ( AT.WorkPointer < term + *term ) AT.WorkPointer = term + *term;
AT.TMbuff = tbufnum;
AN.ncmod = oldncmod;
DONE(retvalue);
}
AT.WorkPointer = oldwork;
if ( Tpattern ) {
TermFree(Tpattern,"Tpattern");
Tpattern = 0;
}
}
NextFun:;
}
else if ( ( t[2] & DIRTYFLAG ) != 0 ) {
t += FUNHEAD;
while ( t < r ) {
if ( *t == FUNNYDOLLAR ) {
if ( AR.Eside != LHSIDE ) {
AN.TeInFun = 1;
AR.TePos = 0;
AT.TMbuff = AM.dbufnum;
AN.ncmod = oldncmod;
DONE(1)
}
AC.lhdollarflag = 1;
}
t++;
}
}
t = r;
AN.ncmod = oldncmod;
} while ( t < m );
}
Done:
if ( Tpattern ) {
TermFree(Tpattern,"Tpattern");
Tpattern = 0;
}
return(retvalue);
EndTest:;
MLOCK(ErrorMessageLock);
EndTest2:;
MesCall("TestSub");
MUNLOCK(ErrorMessageLock);
SETERROR(-1)
}
/*
#] TestSub :
#[ InFunction : WORD InFunction(term,termout)
*/
/**
* Makes the replacement of the subexpression with the number 'replac'
* in a function argument. Additional information is passed in some
* of the AR, AN, AT variables.
*
* @param term The input term
* @param termout The output term
* @return 0: everything is fine, Negative: fatal, Positive: error.
*
* Special attention should be given to nested functions!
*/
int InFunction(PHEAD WORD *term, WORD *termout)
{
GETBIDENTITY
WORD *m, *t, *r, *rr, sign = 1, oldncmod;
WORD *u, *v, *w, *from, *to,
ipp, olddefer = AR.DeferFlag, oldPolyFun = AR.PolyFun, i, j;
LONG numterms;
from = t = term;
r = t + *t - 1;
m = r - ABS(*r) + 1;
t++;
while ( t < m ) {
if ( *t >= FUNCTION+WILDOFFSET ) ipp = *t - WILDOFFSET;
else ipp = *t;
if ( AR.TePos ) {
if ( ( term + AR.TePos ) == t ) {
m = termout;
while ( from < t ) *m++ = *from++;
*m++ = DENOMINATOR;
*m++ = t[1] + 4 + FUNHEAD + ARGHEAD;
*m++ = DIRTYFLAG;
FILLFUN3(m)
*m++ = t[1] + 4 + ARGHEAD;
*m++ = 1;
FILLARG(m)
*m++ = t[1] + 4;
t[3] = -t[3];
v = t + t[1];
while ( t < v ) *m++ = *t++;
from[3] = -from[3];
*m++ = 1;
*m++ = 1;
*m++ = 3;
r = term + *term;
while ( t < r ) *m++ = *t++;
if ( (m-termout) > (LONG)(AM.MaxTer/sizeof(WORD)) ) {
MLOCK(ErrorMessageLock);
MesPrint("Output term too large (%d words) (MaxTermSize: %d words)", m-termout, AM.MaxTer/sizeof(WORD));
MUNLOCK(ErrorMessageLock);
goto TooLarge;
}
*termout = WORDDIF(m,termout);
return(0);
}
}
else if ( ( *t >= FUNCTION && functions[ipp-FUNCTION].spec <= 0 )
&& ( t[2] & DIRTYFLAG ) == DIRTYFLAG ) {
m = termout;
r = t + t[1];
u = t;
t += FUNHEAD;
oldncmod = AN.ncmod;
while ( t < r ) { /* t points at an argument */
if ( *t > 0 && t[1] ) { /* Argument has been modified */
WORD oldsorttype = AR.SortType;
/* This whole argument must be redone */
if ( ( AN.ncmod != 0 )
&& ( ( AC.modmode & ALSOFUNARGS ) == 0 )
&& ( *u != AR.PolyFun ) ) { AN.ncmod = 0; }
AR.DeferFlag = 0;
v = t + *t;
t += ARGHEAD; /* First term */
w = 0; /* to appease the compilers warning devices */
while ( from < t ) {
if ( from == u ) w = m;
*m++ = *from++;
}
to = m;
NewSort(BHEAD0);
if ( *u == AR.PolyFun && AR.PolyFunType == 2 ) {
AR.CompareRoutine = (COMPAREDUMMY)(&CompareSymbols);
AR.SortType = SORTHIGHFIRST;
}
/*
AR.PolyFun = 0;
*/
while ( t < v ) {
i = *t;
NCOPY(m,t,i);
m = to;
if ( AT.WorkPointer < m+*m ) AT.WorkPointer = m + *m;
if ( Generator(BHEAD m,AR.Cnumlhs) ) {
AN.ncmod = oldncmod;
LowerSortLevel(); goto InFunc;
}
}
/* w = the function */
/* v = the next argument */
/* u = the function */
/* to is new argument */
to -= ARGHEAD;
if ( EndSort(BHEAD m,1) < 0 ) {
AN.ncmod = oldncmod;
goto InFunc;
}
AR.PolyFun = oldPolyFun;
if ( *u == AR.PolyFun && AR.PolyFunType == 2 ) {
AR.CompareRoutine = (COMPAREDUMMY)(&Compare1);
AR.SortType = oldsorttype;
}
while ( *m ) m += *m;
*to = WORDDIF(m,to);
to[1] = 1; /* ??????? or rather 0?. 24-mar-2006 JV */
if ( ToFast(to,to) ) {
if ( *to <= -FUNCTION ) m = to+1;
else m = to+2;
}
w[1] = WORDDIF(m,w) + WORDDIF(r,v);
r = term + *term;
t = v;
while ( t < r ) *m++ = *t++;
if ( (m-termout) > (LONG)(AM.MaxTer/sizeof(WORD)) ) {
MLOCK(ErrorMessageLock);
MesPrint("Output term too large (%d words) (MaxTermSize: %d words)", m-termout, AM.MaxTer/sizeof(WORD));
MUNLOCK(ErrorMessageLock);
goto TooLarge;
}
*termout = WORDDIF(m,termout);
AR.DeferFlag = olddefer;
AN.ncmod = oldncmod;
return(0);
}
else if ( *t == -DOLLAREXPRESSION ) {
if ( AR.Eside == LHSIDE ) {
NEXTARG(t)
AC.lhdollarflag = 1;
}
else {
/*
This whole argument must be redone
*/
DOLLARS d = Dollars + t[1];
#ifdef WITHPTHREADS
int nummodopt, dtype = -1;
if ( AS.MultiThreaded ) {
for ( nummodopt = 0; nummodopt < NumModOptdollars; nummodopt++ ) {
if ( t[1] == ModOptdollars[nummodopt].number ) break;
}
if ( nummodopt < NumModOptdollars ) {
dtype = ModOptdollars[nummodopt].type;
if ( dtype == MODLOCAL ) {
d = ModOptdollars[nummodopt].dstruct+AT.identity;
}
else {
LOCK(d->pthreadslockread);
}
}
}
#endif
oldncmod = AN.ncmod;
if ( ( AN.ncmod != 0 )
&& ( ( AC.modmode & ALSOFUNARGS ) == 0 )
&& ( *u != AR.PolyFun ) ) { AN.ncmod = 0; }
AR.DeferFlag = 0;
v = t + 2;
w = 0; /* to appease the compilers warning devices */
while ( from < t ) {
if ( from == u ) w = m;
*m++ = *from++;
}
to = m;
switch ( d->type ) {
case DOLINDEX:
if ( d->index >= 0 && d->index < AM.OffsetIndex ) {
*m++ = -SNUMBER; *m++ = d->index;
}
else { *m++ = -INDEX; *m++ = d->index; }
break;
case DOLZERO:
*m++ = -SNUMBER; *m++ = 0; break;
case DOLNUMBER:
if ( d->where[0] == 4 &&
( d->where[1] & MAXPOSITIVE ) == d->where[1] ) {
*m++ = -SNUMBER;
if ( d->where[3] >= 0 ) *m++ = d->where[1];
else *m++ = -d->where[1];
break;
}
/* fall through */
case DOLTERMS:
/*
Here we have the special case of the PolyRatFun
That function may have a different sort of the
terms in the argument.
*/
to = m; r = d->where;
*m++ = 0; *m++ = 1;
FILLARG(m)
while ( *r ) {
i = *r; NCOPY(m,r,i)
}
*to = m-to;
if ( ToFast(to,to) ) {
if ( *to <= -FUNCTION ) m = to+1;
else m = to+2;
}
else if ( *u == AR.PolyFun && AR.PolyFunType == 2 ) {
AR.PolyFun = 0;
NewSort(BHEAD0);
AR.CompareRoutine = (COMPAREDUMMY)(&CompareSymbols);
r = to + ARGHEAD;
while ( r < m ) {
rr = r; r += *r;
if ( SymbolNormalize(rr) ) goto InFunc;
if ( StoreTerm(BHEAD rr) ) {
AR.CompareRoutine = (COMPAREDUMMY)(&Compare1);
LowerSortLevel();
Terminate(-1);
}
}
if ( EndSort(BHEAD to+ARGHEAD,1) < 0 ) goto InFunc;
AR.PolyFun = oldPolyFun;
AR.CompareRoutine = (COMPAREDUMMY)(&Compare1);
m = to+ARGHEAD;
if ( *m == 0 ) {
*to = -SNUMBER;
to[1] = 0;
m = to + 2;
}
else {
while ( *m ) m += *m;
*t = m - to;
if ( ToFast(to,to) ) {
if ( *to <= -FUNCTION ) m = to+1;
else m = to+2;
}
}
}
w[1] = w[1] - 2 + (m-to);
break;
case DOLSUBTERM:
to = m; r = d->where;
i = r[1];
*m++ = i+4+ARGHEAD; *m++ = 1;
FILLARG(m)
*m++ = i+4;
while ( --i >= 0 ) *m++ = *r++;
*m++ = 1; *m++ = 1; *m++ = 3;
if ( ToFast(to,to) ) {
if ( *to <= -FUNCTION ) m = to+1;
else m = to+2;
}
w[1] = w[1] - 2 + (m-to);
break;
case DOLARGUMENT:
to = m; r = d->where;
if ( *r > 0 ) {
i = *r - 2;
*m++ = *r++; *m++ = 1; r++;
while ( --i >= 0 ) *m++ = *r++;
}
else if ( *r <= -FUNCTION ) *m++ = *r++;
else { *m++ = *r++; *m++ = *r++; }
w[1] = w[1] - 2 + (m-to);
break;
case DOLWILDARGS:
to = m; r = d->where;
if ( *r > 0 ) { /* Tensor arguments */
i = *r++;
while ( --i >= 0 ) {
if ( *r < 0 ) {
*m++ = -VECTOR; *m++ = *r++;
}
else if ( *r >= AM.OffsetIndex ) {
*m++ = -INDEX; *m++ = *r++;
}
else { *m++ = -SNUMBER; *m++ = *r++; }
}
}
else { /* Regular arguments */
r++;
while ( *r ) {
if ( *r > 0 ) {
i = *r - 2;
*m++ = *r++; *m++ = 1; r++;
while ( --i >= 0 ) *m++ = *r++;
}
else if ( *r <= -FUNCTION ) *m++ = *r++;
else { *m++ = *r++; *m++ = *r++; }
}
}
w[1] = w[1] - 2 + (m-to);
break;
case DOLUNDEFINED:
default:
MLOCK(ErrorMessageLock);
MesPrint("!!!Undefined $-variable: $%s!!!",
AC.dollarnames->namebuffer+d->name);
MUNLOCK(ErrorMessageLock);
#ifdef WITHPTHREADS
if ( dtype > 0 && dtype != MODLOCAL ) { UNLOCK(d->pthreadslockread); }
#endif
Terminate(-1);
}
#ifdef WITHPTHREADS
if ( dtype > 0 && dtype != MODLOCAL ) { UNLOCK(d->pthreadslockread); }
#endif
r = term + *term;
t = v;
while ( t < r ) *m++ = *t++;
if ( (m-termout) > (LONG)(AM.MaxTer/sizeof(WORD)) ) {
MLOCK(ErrorMessageLock);
MesPrint("Output term too large (%d words) (MaxTermSize: %d words)", m-termout, AM.MaxTer/sizeof(WORD));
MUNLOCK(ErrorMessageLock);
goto TooLarge;
}
*termout = WORDDIF(m,termout);
AR.DeferFlag = olddefer;
AN.ncmod = oldncmod;
return(0);
}
}
else if ( *t == -TERMSINBRACKET ) {
if ( AC.ComDefer ) numterms = CountTerms1(BHEAD0);
else numterms = 1;
/*
Compose the output term
First copy the part till this function argument
m points at the output term space
u points at the start of the function
t points at the start of the argument
*/
w = 0;
while ( from < t ) {
if ( from == u ) w = m;
*m++ = *from++;
}
if ( ( numterms & MAXPOSITIVE ) == numterms ) {
*m++ = -SNUMBER; *m++ = numterms & MAXPOSITIVE;
w[1] += 1;
}
else if ( ( i = numterms >> BITSINWORD ) == 0 ) {
*m++ = ARGHEAD+4;
for ( j = 1; j < ARGHEAD; j++ ) *m++ = 0;
*m++ = 4; *m++ = numterms & WORDMASK; *m++ = 1; *m++ = 3;
w[1] += ARGHEAD+3;
}
else {
*m++ = ARGHEAD+6;
for ( j = 1; j < ARGHEAD; j++ ) *m++ = 0;
*m++ = 6; *m++ = numterms & WORDMASK;
*m++ = i; *m++ = 1; *m++ = 0; *m++ = 5;
w[1] += ARGHEAD+5;
}
from++; /* Skip our function */
r = term + *term;
while ( from < r ) *m++ = *from++;
if ( (m-termout) > (LONG)(AM.MaxTer/sizeof(WORD)) ) {
MLOCK(ErrorMessageLock);
MesPrint("Output term too large (%d words) (MaxTermSize: %d words)", m-termout, AM.MaxTer/sizeof(WORD));
MUNLOCK(ErrorMessageLock);
goto TooLarge;
}
*termout = WORDDIF(m,termout);
return(0);
}
else { NEXTARG(t) }
}
t = u;
}
else if ( ( *t >= FUNCTION && functions[ipp-FUNCTION].spec > 0 )
&& ( t[2] & DIRTYFLAG ) == DIRTYFLAG ) { /* Could be FUNNYDOLLAR */
u = t; v = t + t[1];
t += FUNHEAD;
while ( t < v ) {
if ( *t == FUNNYDOLLAR ) {
if ( AR.Eside != LHSIDE ) {
DOLLARS d = Dollars + t[1];
#ifdef WITHPTHREADS
int nummodopt, dtype = -1;
if ( AS.MultiThreaded ) {
for ( nummodopt = 0; nummodopt < NumModOptdollars; nummodopt++ ) {
if ( t[1] == ModOptdollars[nummodopt].number ) break;
}
if ( nummodopt < NumModOptdollars ) {
dtype = ModOptdollars[nummodopt].type;
if ( dtype == MODLOCAL ) {
d = ModOptdollars[nummodopt].dstruct+AT.identity;
}
else {
LOCK(d->pthreadslockread);
}
}
}
#endif
oldncmod = AN.ncmod;
if ( ( AN.ncmod != 0 )
&& ( ( AC.modmode & ALSOFUNARGS ) == 0 )
&& ( *u != AR.PolyFun ) ) { AN.ncmod = 0; }
m = termout; w = 0;
while ( from < t ) {
if ( from == u ) w = m;
*m++ = *from++;
}
to = m;
switch ( d->type ) {
case DOLINDEX:
*m++ = d->index; break;
case DOLZERO:
*m++ = 0; break;
case DOLNUMBER:
case DOLTERMS:
if ( d->where[0] == 4 && d->where[4] == 0
&& d->where[3] == 3 && d->where[2] == 1
&& d->where[1] < AM.OffsetIndex ) {
*m++ = d->where[1];
}
else {
wrongtype:;
#ifdef WITHPTHREADS
if ( dtype > 0 && dtype != MODLOCAL ) { UNLOCK(d->pthreadslockread); }
#endif
MLOCK(ErrorMessageLock);
MesPrint("$%s has wrong type for tensor substitution",
AC.dollarnames->namebuffer+d->name);
MUNLOCK(ErrorMessageLock);
AN.ncmod = oldncmod;
return(-1);
}
break;
case DOLARGUMENT:
if ( d->where[0] == -INDEX ) {
*m++ = d->where[1]; break;
}
else if ( d->where[0] == -VECTOR ) {
*m++ = d->where[1]; break;
}
else if ( d->where[0] == -MINVECTOR ) {
*m++ = d->where[1];
sign = -sign;
break;
}
else if ( d->where[0] == -SNUMBER ) {
if ( d->where[1] >= 0
&& d->where[1] < AM.OffsetIndex ) {
*m++ = d->where[1]; break;
}
}
goto wrongtype;
case DOLWILDARGS:
if ( d->where[0] > 0 ) {
r = d->where; i = *r++;
while ( --i >= 0 ) *m++ = *r++;
}
else {
r = d->where + 1;
while ( *r ) {
if ( *r == -INDEX ) {
*m++ = r[1]; r += 2; continue;
}
else if ( *r == -VECTOR ) {
*m++ = r[1]; r += 2; continue;
}
else if ( *r == -MINVECTOR ) {
*m++ = r[1]; r += 2;
sign = -sign; continue;
}
else if ( *r == -SNUMBER ) {
if ( r[1] >= 0
&& r[1] < AM.OffsetIndex ) {
*m++ = r[1]; r += 2; continue;
}
}
goto wrongtype;
}
}
break;
case DOLSUBTERM:
r = d->where;
if ( *r == INDEX && r[1] == 3 ) {
*m++ = r[2];
}
else goto wrongtype;
break;
case DOLUNDEFINED:
#ifdef WITHPTHREADS
if ( dtype > 0 && dtype != MODLOCAL ) { UNLOCK(d->pthreadslockread); }
#endif
MLOCK(ErrorMessageLock);
MesPrint("$%s is undefined in tensor substitution",
AC.dollarnames->namebuffer+d->name);
MUNLOCK(ErrorMessageLock);
AN.ncmod = oldncmod;
return(-1);
}
#ifdef WITHPTHREADS
if ( dtype > 0 && dtype != MODLOCAL ) { UNLOCK(d->pthreadslockread); }
#endif
w[1] = w[1] - 2 + (m-to);
from += 2;
term += *term;
while ( from < term ) *m++ = *from++;
if ( sign < 0 ) m[-1] = -m[-1];
if ( (m-termout) > (LONG)(AM.MaxTer/sizeof(WORD)) ) {
MLOCK(ErrorMessageLock);
MesPrint("Output term too large (%d words) (MaxTermSize: %d words)", m-termout, AM.MaxTer/sizeof(WORD));
MUNLOCK(ErrorMessageLock);
goto TooLarge;
}
*termout = m - termout;
AN.ncmod = oldncmod;
return(0);
}
else {
AC.lhdollarflag = 1;
}
}
t++;
}
t = u;
}
t += t[1];
}
MLOCK(ErrorMessageLock);
MesPrint("Internal error in InFunction: Function not encountered.");
if ( AM.tracebackflag ) {
MesPrint("%w: AR.TePos = %d",AR.TePos);
MesPrint("%w: AN.TeInFun = %d",AN.TeInFun);
termout = term;
AO.OutFill = AO.OutputLine = (UBYTE *)AT.WorkPointer + AM.MaxTer;
AO.OutSkip = 3;
FiniLine();
i = *termout;
while ( --i >= 0 ) {
TalToLine((UWORD)(*termout++));
TokenToLine((UBYTE *)" ");
}
AO.OutSkip = 0;
FiniLine();
MesCall("InFunction");
}
MUNLOCK(ErrorMessageLock);
return(1);
InFunc:
MLOCK(ErrorMessageLock);
MesCall("InFunction");
MUNLOCK(ErrorMessageLock);
SETERROR(-1)
TooLarge:
MLOCK(ErrorMessageLock);
MesCall("InFunction");
MUNLOCK(ErrorMessageLock);
SETERROR(-1)
}
/*
#] InFunction :
#[ InsertTerm : WORD InsertTerm(term,replac,extractbuff,position,termout)
*/
/**
* Puts the terms 'term' and 'position' together into a single
* legal term in termout. replac is the number of the subexpression
* that should be replaced. It must be a positive term.
* When action is needed in the argument of a function all terms
* in that argument are dealt with recursively. The subexpression
* is sorted. Only one subexpression is done at a time this way.
*
* @param term the input term
* @param replac number of the subexpression pointer to replace
* @param extractbuff number of the compiler buffer replac refers to
* @param position position from where to take the term in the compiler buffer
* @param termout the output term
* @param tepos offset in term where the subexpression is.
* @return Normal conventions (OK = 0).
*/
int InsertTerm(PHEAD WORD *term, WORD replac, WORD extractbuff, WORD *position, WORD *termout,
WORD tepos)
{
GETBIDENTITY
WORD *m, *t, *r, i, l2, j;
WORD *u, *v, l1, *coef;
coef = AT.WorkPointer;
if ( ( AT.WorkPointer = coef + 2*AM.MaxTal ) > AT.WorkTop ) {
MLOCK(ErrorMessageLock);
MesWork();
MUNLOCK(ErrorMessageLock);
return(-1);
}
t = term;
r = t + *t;
l1 = l2 = r[-1];
m = r - ABS(l2);
if ( tepos > 0 ) {
t = term + tepos;
goto foundit;
}
t++;
while ( t < m ) {
if ( *t == SUBEXPRESSION && t[2] == replac && t[3] && t[4] == extractbuff ) {
r = t + t[1];
while ( *r == SUBEXPRESSION && r[2] == replac && r[3] && r < m && r[4] == extractbuff ) {
t = r; r += r[1];
}
foundit:;
u = m;
r = term;
m = termout;
do { *m++ = *r++; } while ( r < t );
if ( t[1] > SUBEXPSIZE ) {
/*
if this is a dollar expression there are no wildcards
*/
i = *--m;
if ( ( l2 = WildFill(BHEAD m,position,t) ) < 0 ) goto InsCall;
*m = i;
m += l2-1;
l2 = *m;
i = ( j = ABS(l2) ) - 1;
r = coef + i;
do { *--r = *--m; } while ( --i > 0 );
}
else {
v = t;
t = position;
r = t + *t;
l2 = r[-1];
r -= ( j = ABS(l2) );
t++;
if ( t < r ) do { *m++ = *t++; } while ( t < r );
t = v;
}
t += t[1];
while ( t < u && *t == DOLLAREXPR2 ) t += t[1];
ComAct: if ( t < u ) do { *m++ = *t++; } while ( t < u );
if ( *r == 1 && r[1] == 1 && j == 3 ) {
if ( l2 < 0 ) l1 = -l1;
i = ABS(l1)-1;
NCOPY(m,t,i);
*m++ = l1;
}
else {
if ( MulRat(BHEAD (UWORD *)u,REDLENG(l1),(UWORD *)r,REDLENG(l2),
(UWORD *)m,&l1) ) goto InsCall;
l2 = l1;
l2 *= 2;
if ( l2 < 0 ) {
m -= l2;
*m++ = l2-1;
}
else {
m += l2;
*m++ = l2+1;
}
}
*termout = WORDDIF(m,termout);
if ( (*termout)*((LONG)sizeof(WORD)) > AM.MaxTer ) {
MLOCK(ErrorMessageLock);
MesPrint("Term too complex during substitution (%d words). MaxTermSize (%l words) is too small.", *termout, AM.MaxTer/(LONG)sizeof(WORD) );
goto InsCall2;
}
AT.WorkPointer = coef;
return(0);
}
t += t[1];
}
/*
The next action is for when there is no subexpression pointer.
We append the extra term. Effectively the routine becomes now a
merge routine for two terms.
*/
v = t;
u = m;
r = term;
m = termout;
do { *m++ = *r++; } while ( r < t );
t = position;
r = t + *t;
l2 = r[-1];
r -= ( j = ABS(l2) );
t++;
if ( t < r ) do { *m++ = *t++; } while ( t < r );
t = v;
goto ComAct;
InsCall:
MLOCK(ErrorMessageLock);
InsCall2:
MesCall("InsertTerm");
MUNLOCK(ErrorMessageLock);
SETERROR(-1)
}
/*
#] InsertTerm :
#[ PasteFile : WORD PasteFile(num,acc,pos,accf,renum,freeze,nexpr)
*/
/**
* Gets a term from stored expression expr and puts it in
* the accumulator at position number. It returns the length of the
* term that came from file.
*
* @param number number of partial terms to skip in accum
* @param accum the accumulator
* @param position file position from where to get the stored term
* @param accfill returns tail position in accum
* @param renumber the renumber struct for the variables in the stored expression
* @param freeze information about if we need only the contents of a bracket
* @param nexpr the number of the stored expression
* @return Normal conventions (OK = 0).
*/
LONG PasteFile(PHEAD WORD number, WORD *accum, POSITION *position, WORD **accfill,
RENUMBER renumber, WORD *freeze, WORD nexpr)
{
GETBIDENTITY
WORD *r, l, *m, i;
WORD *stop, *s1, *s2;
/* POSITION AccPos; bug 12-apr-2008 JV */
WORD InCompState;
WORD *oldipointer;
LONG retlength;
stop = (WORD *)(((UBYTE *)(accum)) + 2*AM.MaxTer);
*accum++ = number;
while ( --number >= 0 ) accum += *accum;
if ( freeze ) {
/* AccPos = *position; bug 12-apr-2008 JV */
oldipointer = AR.CompressPointer;
do {
AR.CompressPointer = oldipointer;
/* if ( ( l = GetFromStore(accum,&AccPos,renumber,&InCompState,nexpr) ) < 0 ) bug 12-apr-2008 JV */
if ( ( l = GetFromStore(accum,position,renumber,&InCompState,nexpr) ) < 0 )
goto PasErr;
if ( !l ) { *accum = 0; return(0); }
r = accum;
m = r + *r;
m -= ABS(m[-1]);
r++;
while ( r < m && *r != HAAKJE ) r += r[1];
if ( r >= m ) {
if ( *freeze != 4 ) l = -1;
}
else {
/*
The algorithm for accepting terms with a given (freeze)
representation outside brackets is rather crude. A refinement
would be to store the part outside the bracket and skip the
term when this part doesn't alter (and is unacceptable).
Once accepting one can keep accepting till the bracket alters
and then one may stop the generation. It is necessary to
set up a struct to remember the bracket and the progress
status.
*/
m = AT.WorkPointer;
s2 = r;
r = accum;
*m++ = WORDDIF(s2,r) + 3;
r++;
while ( r < s2 ) *m++ = *r++;
*m++ = 1; *m++ = 1; *m++ = 3;
m = AT.WorkPointer;
if ( Normalize(BHEAD AT.WorkPointer) ) goto PasErr;
r = freeze;
i = *m;
while ( --i >= 0 && *m++ == *r++ ) {}
if ( i > 0 ) {
l = -1;
}
else { /* Term to be accepted */
r = accum;
s1 = r + *r;
r++;
m = s2;
m += m[1];
do { *r++ = *m++; } while ( m < s1 );
*accum = l = WORDDIF(r,accum);
}
}
} while ( l < 0 );
retlength = InCompState;
/* retlength = DIFBASE(AccPos,*position) / sizeof(WORD); bug 12-apr-2008 JV */
}
else {
if ( ( l = GetFromStore(accum,position,renumber,&InCompState,nexpr) ) < 0 ) {
MLOCK(ErrorMessageLock);
MesCall("PasteFile");
MUNLOCK(ErrorMessageLock);
SETERROR(-1)
}
if ( l == 0 ) { *accum = 0; return(0); }
retlength = InCompState;
}
accum += l;
if ( accum > stop ) {
MLOCK(ErrorMessageLock);
MesPrint("Buffer too small in PasteFile");
MUNLOCK(ErrorMessageLock);
SETERROR(-1)
}
*accum = 0;
*accfill = accum;
return(retlength);
PasErr:
MLOCK(ErrorMessageLock);
MesCall("PasteFile");
MUNLOCK(ErrorMessageLock);
SETERROR(-1)
}
/*
#] PasteFile :
#[ PasteTerm : WORD PasteTerm(number,accum,position,times,divby)
*/
/**
* Puts the term at position in the accumulator accum at position
* 'number+1'. if times > 0 the coefficient of this term is
* multiplied by times/divby.
*
* @param number The number of term fragments in accum that should be skipped
* @param accum The accumulator of term fragments
* @param position A position in (typically) a compiler buffer from where
* a (piece of a) term comes.
* @param times Multiply the result by this
* @param divby Divide the result by this.
*
* This routine is typically used when we have to replace a (sub)expression
* pointer by a power of a (sub)expression. This uses mostly a binomial
* expansion and the new term is the old term multiplied one by one
* by terms of the new expression. The factors times and divby keep track
* of the binomial coefficient.
* Once this is complete, the routine FiniTerm will make the contents
* of the accumulator into a proper term that still needs to be normalized.
*/
WORD *PasteTerm(PHEAD WORD number, WORD *accum, WORD *position, WORD times, WORD divby)
{
GETBIDENTITY
WORD *t, *r, x, y, z;
WORD *m, *u, l1, a[2];
m = (WORD *)(((UBYTE *)(accum)) + AM.MaxTer);
/* m = (WORD *)(((UBYTE *)(accum)) + 2*AM.MaxTer); */
*accum++ = number;
while ( --number >= 0 ) accum += *accum;
if ( times == divby ) {
t = position;
r = t + *t;
if ( t < r ) do { *accum++ = *t++; } while ( t < r );
}
else {
u = accum;
t = position;
r = t + *t - 1;
l1 = *r;
r -= ABS(*r) - 1;
if ( t < r ) do { *accum++ = *t++; } while ( t < r );
if ( divby > times ) { x = divby; y = times; }
else { x = times; y = divby; }
z = x%y;
while ( z ) { x = y; y = z; z = x%y; }
if ( y != 1 ) { divby /= y; times /= y; }
a[1] = divby;
a[0] = times;
if ( MulRat(BHEAD (UWORD *)t,REDLENG(l1),(UWORD *)a,1,(UWORD *)accum,&l1) ) {
MLOCK(ErrorMessageLock);
MesCall("PasteTerm");
MUNLOCK(ErrorMessageLock);
return(0);
}
x = l1;
x *= 2;
if ( x < 0 ) { accum -= x; *accum++ = x - 1; }
else { accum += x; *accum++ = x + 1; }
*u = WORDDIF(accum,u);
}
if ( accum >= m ) {
MLOCK(ErrorMessageLock);
MesPrint("Buffer too small in PasteTerm");
MUNLOCK(ErrorMessageLock);
return(0);
}
*accum = 0;
return(accum);
}
/*
#] PasteTerm :
#[ FiniTerm : WORD FiniTerm(term,accum,termout,number)
*/
/**
* Concatenates the contents of the accumulator into a single
* legal term, which replaces the subexpression pointer
*
* @param term the input term with the (sub)expression subterm
* @param accum the accumulator with the term fragments
* @param termout the location where the output should be written
* @param number the number of term fragments in the accumulator
* @param tepos the position of the subterm in term to be replaced
*/
int FiniTerm(PHEAD WORD *term, WORD *accum, WORD *termout, WORD number, WORD tepos)
{
GETBIDENTITY
WORD *m, *t, *r, i, numacc, l2, ipp;
WORD *u, *v, l1, *coef = AT.WorkPointer, *oldaccum;
if ( ( AT.WorkPointer = coef + 2*AM.MaxTal ) > AT.WorkTop ) {
MLOCK(ErrorMessageLock);
MesWork();
MUNLOCK(ErrorMessageLock);
return(-1);
}
oldaccum = accum;
t = term;
m = t + *t - 1;
l1 = REDLENG(*m);
i = ABS(*m) - 1;
r = coef + i;
do { *--r = *--m; } while ( --i > 0 ); /* Copies coefficient */
if ( tepos > 0 ) {
t = term + tepos;
goto foundit;
}
t++;
if ( t < m ) do {
if ( ( ( *t == SUBEXPRESSION && ( *(r=t+t[1]) != SUBEXPRESSION
|| r >= m || !r[3] ) ) || *t == EXPRESSION ) && t[2] == number && t[3] ) {
foundit:;
u = m;
r = term;
m = termout;
if ( r < t ) do { *m++ = *r++; } while ( r < t );
numacc = *accum++;
if ( numacc >= 0 ) do {
if ( *t == EXPRESSION ) {
v = t + t[1];
r = t + SUBEXPSIZE;
while ( r < v ) {
if ( *r == WILDCARDS ) {
r += 2;
i = *--m;
if ( ( l2 = WildFill(BHEAD m,accum,r) ) < 0 ) goto FiniCall;
goto AllWild;
}
r += r[1];
}
goto NoWild;
}
else if ( t[1] > SUBEXPSIZE && t[SUBEXPSIZE] != FROMBRAC ) {
i = *--m;
if ( ( l2 = WildFill(BHEAD m,accum,t) ) < 0 ) goto FiniCall;
AllWild: *m = i;
m += l2-1;
l2 = *m;
m -= ABS(l2) - 1;
r = m;
}
else {
NoWild: r = accum;
v = r + *r - 1;
l2 = *v;
v -= ABS(l2) - 1;
r++;
if ( r < v ) do { *m++ = *r++; } while ( r < v );
}
if ( *r == 1 && r[1] == 1 && ABS(l2) == 3 ) {
if ( l2 < 0 ) l1 = -l1;
}
else {
l2 = REDLENG(l2);
if ( l2 == 0 ) {
t = oldaccum;
numacc = *t++;
AO.OutSkip = 3;
FiniLine();
while ( --numacc >= 0 ) {
i = *t;
while ( --i >= 0 ) {
TalToLine((UWORD)(*t++));
TokenToLine((UBYTE *)" ");
}
}
AO.OutSkip = 0;
FiniLine();
goto FiniCall;
}
if ( MulRat(BHEAD (UWORD *)coef,l1,(UWORD *)r,l2,(UWORD *)coef,&l1) ) goto FiniCall;
if ( AN.ncmod != 0 && TakeModulus((UWORD *)coef,&l1,AC.cmod,AN.ncmod,UNPACK|AC.modmode) ) goto FiniCall;
}
accum += *accum;
} while ( --numacc >= 0 );
if ( *t == SUBEXPRESSION ) {
while ( t+t[1] < u && t[t[1]] == DOLLAREXPR2 ) t += t[1];
}
t += t[1];
if ( t < u ) do { *m++ = *t++; } while ( t < u );
l2 = l1;
/*
Code to economize when taking x = (a+b)/2
*/
r = termout+1;
while ( r < m ) {
if ( *r == SUBEXPRESSION ) {
t = r + r[1];
l1 = (WORD)(cbuf[r[4]].CanCommu[r[2]]);
while ( t < m ) {
if ( *t == SUBEXPRESSION &&
t[1] == r[1] && t[2] == r[2] && t[4] == r[4] ) {
i = t[1] - SUBEXPSIZE;
u = r + SUBEXPSIZE; v = t + SUBEXPSIZE;
while ( i > 0 ) {
if ( *v++ != *u++ ) break;
i--;
}
if ( i <= 0 ) {
u = r;
r[3] += t[3];
r = t + t[1];
while ( r < m ) *t++ = *r++;
m = t;
r = u;
goto Nextr;
}
if ( l1 && cbuf[t[4]].CanCommu[t[2]] ) break;
while ( t+t[1] < m && t[t[1]] == DOLLAREXPR2 ) t += t[1];
}
else if ( l1 ) {
if ( *t == SUBEXPRESSION && cbuf[t[4]].CanCommu[t[2]] )
break;
if ( *t >= FUNCTION+WILDOFFSET )
ipp = *t - WILDOFFSET;
else ipp = *t;
if ( *t >= FUNCTION
&& functions[ipp-FUNCTION].commute && l1 ) break;
if ( *t == EXPRESSION ) break;
}
t += t[1];
}
r += r[1];
}
else r += r[1];
Nextr:;
}
i = ABS(l2);
i *= 2;
i++;
l2 = ( l2 >= 0 ) ? i: -i;
r = coef;
while ( --i > 0 ) *m++ = *r++;
*m++ = l2;
*termout = WORDDIF(m,termout);
AT.WorkPointer = coef;
return(0);
}
t += t[1];
} while ( t < m );
AT.WorkPointer = coef;
return(1);
FiniCall:
MLOCK(ErrorMessageLock);
MesCall("FiniTerm");
MUNLOCK(ErrorMessageLock);
SETERROR(-1)
}
/*
#] FiniTerm :
#[ Generator : WORD Generator(BHEAD term,level)
*/
static WORD zeroDollar[] = { 0, 0 };
/*
static LONG debugcounter = 0;
*/
/**
* The heart of the program.
* Here the expansion tree is set up in one giant recursion
*
* @param term the input term. may be overwritten
* @param level the level in the compiler buffer (number of statement)
* @return Normal conventions (OK = 0).
*
* The routine looks first whether there are unsubstituted (sub)expressions.
* If so, one of them gets inserted term by term and the new term is
* used in a renewed call to Generator.
* If there are no (sub)expressions, the term is normalized, the
* compiler level is raised (next statement) and the program looks
* what type of statement this is. If this is a special statement it
* is either treated on the spot or the appropriate routine is called.
* If it is a substitution, the pattern matcher is called (TestMatch)
* which tells whether there was a match. If so we need to call
* TestSub again to test for (sub)expressions.
* If we run out of levels, the term receives a final treatment for
* modulus calculus and/or brackets and is then sent off to the
* sorting routines.
*/
int Generator(PHEAD WORD *term, WORD level)
{
GETBIDENTITY
WORD replac, *accum, *termout, *t, i, j, tepos, applyflag = 0, *StartBuf;
WORD *a, power, power1, DumNow = AR.CurDum, oldtoprhs, oldatoprhs, extractbuff;
int ret;
int *RepSto = AN.RepPoint, iscopy = 0;
CBUF *C = cbuf+AM.rbufnum, *CC = cbuf + AT.ebufnum, *CCC = cbuf + AT.aebufnum;
LONG posisub, oldcpointer, oldacpointer;
DOLLARS d = 0;
WORD numfac[5], idfunctionflag;
#ifdef WITHPTHREADS
int nummodopt, dtype = -1, id;
#endif
oldtoprhs = CC->numrhs;
oldcpointer = CC->Pointer - CC->Buffer;
oldatoprhs = CCC->numrhs;
oldacpointer = CCC->Pointer - CCC->Buffer;
ReStart:
if ( ( replac = TestSub(BHEAD term,level) ) == 0 ) {
if ( applyflag ) { TableReset(); applyflag = 0; }
/*
if ( AN.PolyNormFlag > 1 ) {
if ( PolyFunMul(BHEAD term) < 0 ) goto GenCall;
AN.PolyNormFlag = 0;
if ( !*term ) goto Return0;
}
*/
Renormalize:
AN.PolyNormFlag = 0;
AN.idfunctionflag = 0;
if ( ( ret = Normalize(BHEAD term) ) != 0 ) {
if ( ret > 0 ) {
if ( AT.WorkPointer < term + *term ) AT.WorkPointer = term + *term;
goto ReStart;
}
goto GenCall;
}
idfunctionflag = AN.idfunctionflag;
if ( !*term ) { AN.PolyNormFlag = 0; goto Return0; }
if ( AN.PolyNormFlag ) {
if ( AN.PolyFunTodo == 0 ) {
if ( PolyFunMul(BHEAD term) < 0 ) goto GenCall;
if ( !*term ) { AN.PolyNormFlag = 0; goto Return0; }
}
else {
WORD oldPolyFunExp = AR.PolyFunExp;
AR.PolyFunExp = 0;
if ( PolyFunMul(BHEAD term) < 0 ) goto GenCall;
AT.WorkPointer = term+*term;
AR.PolyFunExp = oldPolyFunExp;
if ( !*term ) { AN.PolyNormFlag = 0; goto Return0; }
if ( Normalize(BHEAD term) < 0 ) goto GenCall;
if ( !*term ) { AN.PolyNormFlag = 0; goto Return0; }
AT.WorkPointer = term+*term;
if ( AN.PolyNormFlag ) {
if ( PolyFunMul(BHEAD term) < 0 ) goto GenCall;
if ( !*term ) { AN.PolyNormFlag = 0; goto Return0; }
AT.WorkPointer = term+*term;
}
AN.PolyFunTodo = 0;
}
}
if ( idfunctionflag > 0 ) {
if ( TakeIDfunction(BHEAD term) ) {
AT.WorkPointer = term + *term;
goto ReStart;
}
}
if ( AT.WorkPointer < (WORD *)(((UBYTE *)(term)) + AM.MaxTer) )
AT.WorkPointer = (WORD *)(((UBYTE *)(term)) + AM.MaxTer);
do {
SkipCount: level++;
if ( level > AR.Cnumlhs ) {
if ( AR.DeferFlag && AR.sLevel <= 0 ) {
#ifdef WITHMPI
if ( PF.me != MASTER && AC.mparallelflag == PARALLELFLAG && PF.exprtodo < 0 ) {
if ( PF_Deferred(term,level) ) goto GenCall;
}
else
#endif
{
if ( Deferred(BHEAD term,level) ) goto GenCall;
}
goto Return0;
}
if ( AN.ncmod != 0 ) {
if ( Modulus(term) ) goto GenCall;
if ( !*term ) goto Return0;
}
if ( AR.CurDum > AM.IndDum && AR.sLevel <= 0 ) {
WORD olddummies = AN.IndDum;
AN.IndDum = AM.IndDum;
ReNumber(BHEAD term);
Normalize(BHEAD term);
AN.IndDum = olddummies;
if ( !*term ) goto Return0;
olddummies = DetCurDum(BHEAD term);
if ( olddummies > AR.MaxDum ) AR.MaxDum = olddummies;
}
if ( AR.PolyFun > 0 && ( AR.sLevel <= 0 || AN.FunSorts[AR.sLevel]->PolyFlag > 0 ) ) {
if ( PrepPoly(BHEAD term,0) != 0 ) goto Return0;
}
else if ( AR.PolyFun > 0 ) {
if ( PrepPoly(BHEAD term,1) != 0 ) goto Return0;
}
if ( AR.sLevel <= 0 && AR.BracketOn ) {
if ( AT.WorkPointer < term + *term ) AT.WorkPointer = term + *term;
termout = AT.WorkPointer;
if ( AT.WorkPointer + *term + 3 > AT.WorkTop ) goto OverWork;
if ( PutBracket(BHEAD term) ) return(-1);
AN.RepPoint = RepSto;
*AT.WorkPointer = 0;
ret = StoreTerm(BHEAD termout);
AT.WorkPointer = termout;
CC->numrhs = oldtoprhs;
CC->Pointer = CC->Buffer + oldcpointer;
CCC->numrhs = oldatoprhs;
CCC->Pointer = CCC->Buffer + oldacpointer;
return(ret);
}
else {
if ( AT.WorkPointer < term + *term ) AT.WorkPointer = term + *term;
if ( AT.WorkPointer >= AT.WorkTop ) goto OverWork;
*AT.WorkPointer = 0;
AN.RepPoint = RepSto;
ret = StoreTerm(BHEAD term);
CC->numrhs = oldtoprhs;
CC->Pointer = CC->Buffer + oldcpointer;
CCC->numrhs = oldatoprhs;
CCC->Pointer = CCC->Buffer + oldacpointer;
return(ret);
}
}
i = C->lhs[level][0];
if ( i >= TYPECOUNT ) {
/*
#[ Special action :
*/
switch ( i ) {
case TYPECOUNT:
if ( CountDo(term,C->lhs[level]) < C->lhs[level][2] ) {
AT.WorkPointer = term + *term;
goto Return0;
}
break;
case TYPEMULT:
if ( MultDo(BHEAD term,C->lhs[level]) ) goto GenCall;
goto ReStart;
case TYPEGOTO:
level = AC.Labels[C->lhs[level][2]];
break;
case TYPEDISCARD:
AT.WorkPointer = term + *term;
goto Return0;
case TYPEIF:
#ifdef WITHPTHREADS
{
/*
We may be writing in the space here when wildcards
are involved in a match(). Hence we have to make
a private copy here!!!!
*/
WORD ic, jc, *ifcode, *jfcode;
jfcode = C->lhs[level]; jc = jfcode[1];
ifcode = AT.WorkPointer; AT.WorkPointer += jc;
for ( ic = 0; ic < jc; ic++ ) ifcode[ic] = jfcode[ic];
while ( !DoIfStatement(BHEAD ifcode,term) ) {
level = C->lhs[level][2];
if ( C->lhs[level][0] != TYPEELIF ) break;
}
AT.WorkPointer = ifcode;
}
#else
while ( !DoIfStatement(BHEAD C->lhs[level],term) ) {
level = C->lhs[level][2];
if ( C->lhs[level][0] != TYPEELIF ) break;
}
#endif
break;
case TYPEELIF:
do {
level = C->lhs[level][2];
} while ( C->lhs[level][0] == TYPEELIF );
break;
case TYPEELSE:
case TYPEENDIF:
level = C->lhs[level][2];
break;
case TYPESUMFIX:
{
WORD *cp = AR.CompressPointer, *op = AR.CompressPointer;
WORD *tlhs = C->lhs[level] + 3, *m, jlhs;
WORD theindex = C->lhs[level][2];
if ( theindex < 0 ) { /* $-variable */
#ifdef WITHPTHREADS
int ddtype = -1;
theindex = -theindex;
d = Dollars + theindex;
if ( AS.MultiThreaded ) {
for ( nummodopt = 0; nummodopt < NumModOptdollars; nummodopt++ ) {
if ( theindex == ModOptdollars[nummodopt].number ) break;
}
if ( nummodopt < NumModOptdollars ) {
ddtype = ModOptdollars[nummodopt].type;
if ( ddtype == MODLOCAL ) {
d = ModOptdollars[nummodopt].dstruct+AT.identity;
}
else {
LOCK(d->pthreadslockread);
}
}
}
#else
theindex = -theindex;
d = Dollars + theindex;
#endif
if ( d->type != DOLINDEX
|| d->index < AM.OffsetIndex
|| d->index >= AM.OffsetIndex + WILDOFFSET ) {
MLOCK(ErrorMessageLock);
MesPrint("$%s should have been an index"
,AC.dollarnames->namebuffer+d->name);
AN.currentTerm = term;
MesPrint("Current term: %t");
AN.listinprint = printscratch;
printscratch[0] = DOLLAREXPRESSION;
printscratch[1] = theindex;
MesPrint("$%s = %$"
,AC.dollarnames->namebuffer+d->name);
MUNLOCK(ErrorMessageLock);
#ifdef WITHPTHREADS
if ( ddtype > 0 && ddtype != MODLOCAL ) { UNLOCK(d->pthreadslockread); }
#endif
goto GenCall;
}
theindex = d->index;
#ifdef WITHPTHREADS
if ( ddtype > 0 && ddtype != MODLOCAL ) { UNLOCK(d->pthreadslockread); }
#endif
}
cp[1] = SUBEXPSIZE+4;
cp += SUBEXPSIZE;
*cp++ = INDTOIND;
*cp++ = 4;
*cp++ = theindex;
i = C->lhs[level][1] - 3;
cp++;
AR.CompressPointer = cp;
while ( --i >= 0 ) {
cp[-1] = *tlhs++;
termout = AT.WorkPointer;
if ( ( jlhs = WildFill(BHEAD termout,term,op)) < 0 )
goto GenCall;
m = term;
jlhs = *m;
while ( --jlhs >= 0 ) {
if ( *m++ != *termout++ ) break;
}
if ( jlhs >= 0 ) {
termout = AT.WorkPointer;
AT.WorkPointer = termout + *termout;
if ( Generator(BHEAD termout,level) ) goto GenCall;
AT.WorkPointer = termout;
}
else {
AR.CompressPointer = op;
goto SkipCount;
}
}
AR.CompressPointer = op;
goto CommonEnd;
}
case TYPESUM:
{
WORD *wp, *cp = AR.CompressPointer, *op = AR.CompressPointer;
WORD theindex;
WORD *ow;
/*
At this point it is safest to determine CurDum
*/
AR.CurDum = DetCurDum(BHEAD term);
i = C->lhs[level][1]-2;
wp = C->lhs[level] + 2;
cp[1] = SUBEXPSIZE+4*i;
cp += SUBEXPSIZE;
while ( --i >= 0 ) {
theindex = *wp++;
if ( theindex < 0 ) { /* $-variable */
#ifdef WITHPTHREADS
int ddtype = -1;
theindex = -theindex;
d = Dollars + theindex;
if ( AS.MultiThreaded ) {
for ( nummodopt = 0; nummodopt < NumModOptdollars; nummodopt++ ) {
if ( theindex == ModOptdollars[nummodopt].number ) break;
}
if ( nummodopt < NumModOptdollars ) {
ddtype = ModOptdollars[nummodopt].type;
if ( ddtype == MODLOCAL ) {
d = ModOptdollars[nummodopt].dstruct+AT.identity;
}
else {
LOCK(d->pthreadslockread);
}
}
}
#else
theindex = -theindex;
d = Dollars + theindex;
#endif
if ( d->type != DOLINDEX
|| d->index < AM.OffsetIndex
|| d->index >= AM.OffsetIndex + WILDOFFSET ) {
MLOCK(ErrorMessageLock);
MesPrint("$%s should have been an index"
,AC.dollarnames->namebuffer+d->name);
AN.currentTerm = term;
MesPrint("Current term: %t");
AN.listinprint = printscratch;
printscratch[0] = DOLLAREXPRESSION;
printscratch[1] = theindex;
MesPrint("$%s = %$"
,AC.dollarnames->namebuffer+d->name);
MUNLOCK(ErrorMessageLock);
#ifdef WITHPTHREADS
if ( ddtype > 0 && ddtype != MODLOCAL ) { UNLOCK(d->pthreadslockread); }
#endif
goto GenCall;
}
theindex = d->index;
#ifdef WITHPTHREADS
if ( ddtype > 0 && ddtype != MODLOCAL ) { UNLOCK(d->pthreadslockread); }
#endif
}
*cp++ = INDTOIND;
*cp++ = 4;
*cp++ = theindex;
*cp++ = ++AR.CurDum;
}
ow = AT.WorkPointer;
AR.CompressPointer = cp;
if ( WildFill(BHEAD ow,term,op) < 0 ) goto GenCall;
AR.CompressPointer = op;
i = ow[0];
WORD term_changed = 0;
for ( j = 0; j < i; j++ ) {
if ( term[j] != ow[j] ) term_changed = 1;
term[j] = ow[j];
}
// If the term was modified by WildFill, set RepCount.
if ( term_changed ) *AN.RepPoint = 1;
AT.WorkPointer = ow;
// Most other calls to ReNumber reset AN.IndDum to AM.IndDum first. Here it is
// not done, but doing it is one way to fix Issue #710. The fix that is actually
// implemented is to change a comparison in FunLevel and then a reset of
// AN.IndDum appears unnecessary here. But maybe one day this comment is useful.
ReNumber(BHEAD term);
goto Renormalize;
}
case TYPECHISHOLM:
if ( Chisholm(BHEAD term,level) ) goto GenCall;
CommonEnd:
AT.WorkPointer = term + *term;
goto Return0;
case TYPEARG:
if ( ( i = execarg(BHEAD term,level) ) < 0 ) goto GenCall;
level = C->lhs[level][2];
if ( i > 0 ) goto ReStart;
break;
case TYPENORM:
case TYPENORM2:
case TYPENORM3:
case TYPENORM4:
case TYPESPLITARG:
case TYPESPLITARG2:
case TYPESPLITFIRSTARG:
case TYPESPLITLASTARG:
case TYPEARGTOEXTRASYMBOL:
if ( execarg(BHEAD term,level) < 0 ) goto GenCall;
level = C->lhs[level][2];
break;
case TYPEFACTARG:
case TYPEFACTARG2:
{ WORD jjj;
if ( ( jjj = execarg(BHEAD term,level) ) < 0 ) goto GenCall;
if ( jjj > 0 ) goto ReStart;
level = C->lhs[level][2];
break; }
case TYPEEXIT:
if ( C->lhs[level][2] > 0 ) {
MLOCK(ErrorMessageLock);
MesPrint("%s",C->lhs[level]+3);
MUNLOCK(ErrorMessageLock);
}
Terminate(-1);
goto GenCall;
case TYPESETEXIT:
AM.exitflag = 1; /* no danger of race conditions */
break;
case TYPEPRINT:
AN.currentTerm = term;
AN.numlistinprint = (C->lhs[level][1] - C->lhs[level][4] - 5)/2;
AN.listinprint = C->lhs[level]+5+C->lhs[level][4];
MLOCK(ErrorMessageLock);
AO.ErrorBlock = 1;
MesPrint((char *)(C->lhs[level]+5));
AO.ErrorBlock = 0;
MUNLOCK(ErrorMessageLock);
break;
case TYPEFPRINT:
{
int oldFOflag;
WORD oldPrintType, oldLogHandle = AC.LogHandle;
AC.LogHandle = C->lhs[level][2];
MLOCK(ErrorMessageLock);
oldFOflag = AM.FileOnlyFlag;
oldPrintType = AO.PrintType;
if ( AC.LogHandle >= 0 ) {
AM.FileOnlyFlag = 1;
AO.PrintType |= PRINTLFILE;
}
AO.PrintType |= C->lhs[level][3];
AN.currentTerm = term;
AN.numlistinprint = (C->lhs[level][1] - C->lhs[level][4] - 5)/2;
AN.listinprint = C->lhs[level]+5+C->lhs[level][4];
MesPrint((char *)(C->lhs[level]+5));
AO.PrintType = oldPrintType;
AM.FileOnlyFlag = oldFOflag;
MUNLOCK(ErrorMessageLock);
AC.LogHandle = oldLogHandle;
}
break;
case TYPEREDEFPRE:
j = C->lhs[level][2];
#ifdef WITHMPI
{
/*
* Regardless of parallel/nonparallel switch, we need to set
* AC.inputnumbers[ii], which indicates that the corresponding
* preprocessor variable is redefined and so we need to
* send/broadcast it.
*/
int ii;
for ( ii = 0; ii < AC.numpfirstnum; ii++ ) {
if ( AC.pfirstnum[ii] == j ) break;
}
AC.inputnumbers[ii] = AN.ninterms;
}
#endif
#ifdef WITHPTHREADS
if ( AS.MultiThreaded ) {
int ii;
for ( ii = 0; ii < AC.numpfirstnum; ii++ ) {
if ( AC.pfirstnum[ii] == j ) break;
}
if ( AN.inputnumber < AC.inputnumbers[ii] ) break;
LOCK(AP.PreVarLock);
if ( AN.inputnumber >= AC.inputnumbers[ii] ) {
a = C->lhs[level]+4;
if ( a[a[-1]] == 0 )
PutPreVar(PreVar[j].name,(UBYTE *)(a),0,1);
else
PutPreVar(PreVar[j].name,(UBYTE *)(a)
,(UBYTE *)(a+a[-1]+1),1);
/*
PutPreVar(PreVar[j].name,(UBYTE *)(C->lhs[level]+4),0,1);
*/
AC.inputnumbers[ii] = AN.inputnumber;
}
UNLOCK(AP.PreVarLock);
}
else
#endif
{
a = C->lhs[level]+4;
LOCK(AP.PreVarLock);
if ( a[a[-1]] == 0 )
PutPreVar(PreVar[j].name,(UBYTE *)(a),0,1);
else
PutPreVar(PreVar[j].name,(UBYTE *)(a)
,(UBYTE *)(a+a[-1]+1),1);
UNLOCK(AP.PreVarLock);
}
break;
case TYPERENUMBER:
AT.WorkPointer = term + *term;
if ( FullRenumber(BHEAD term,C->lhs[level][2]) ) goto GenCall;
AT.WorkPointer = term + *term;
if ( *term == 0 ) goto Return0;
break;
case TYPETRY:
if ( TryDo(BHEAD term,C->lhs[level],level) ) goto GenCall;
AT.WorkPointer = term + *term;
goto Return0;
case TYPEASSIGN:
{ WORD onc = AR.NoCompress, oldEside = AR.Eside;
WORD oldrepeat = *AN.RepPoint;
/*
Here we have to assign an expression to a $ variable.
*/
AR.Eside = RHSIDE;
AR.NoCompress = 1;
AN.cTerm = AN.currentTerm = term;
AT.WorkPointer = term + *term;
*AT.WorkPointer++ = 0;
if ( AssignDollar(BHEAD term,level) ) goto GenCall;
AT.WorkPointer = term + *term;
AN.cTerm = 0;
*AN.RepPoint = oldrepeat;
AR.NoCompress = onc;
AR.Eside = oldEside;
break;
}
case TYPEFINDLOOP:
if ( Lus(term,C->lhs[level][3],C->lhs[level][4],
C->lhs[level][5],C->lhs[level][6],C->lhs[level][2]) ) {
AT.WorkPointer = term + *term;
goto Renormalize;
}
break;
case TYPEINSIDE:
if ( InsideDollar(BHEAD C->lhs[level],level) < 0 ) goto GenCall;
level = C->lhs[level][2];
break;
case TYPETERM:
ret = execterm(BHEAD term,level);
AN.RepPoint = RepSto;
AR.CurDum = DumNow;
CC->numrhs = oldtoprhs;
CC->Pointer = CC->Buffer + oldcpointer;
CCC->numrhs = oldatoprhs;
CCC->Pointer = CCC->Buffer + oldacpointer;
return(ret);
case TYPEDETCURDUM:
AT.WorkPointer = term + *term;
AR.CurDum = DetCurDum(BHEAD term);
break;
case TYPEINEXPRESSION:
{WORD *ll = C->lhs[level];
int numexprs = (int)(ll[1]-3);
ll += 3;
while ( numexprs-- >= 0 ) {
if ( *ll == AR.CurExpr ) break;
ll++;
}
if ( numexprs < 0 ) level = C->lhs[level][2];
}
break;
case TYPEMERGE:
AT.WorkPointer = term + *term;
if ( DoShuffle(term,level,C->lhs[level][2],C->lhs[level][3]) )
goto GenCall;
AT.WorkPointer = term + *term;
goto Return0;
case TYPESTUFFLE:
AT.WorkPointer = term + *term;
if ( DoStuffle(term,level,C->lhs[level][2],C->lhs[level][3]) )
goto GenCall;
AT.WorkPointer = term + *term;
goto Return0;
case TYPETESTUSE:
AT.WorkPointer = term + *term;
if ( TestUse(term,level) ) goto GenCall;
AT.WorkPointer = term + *term;
break;
case TYPEAPPLY:
AT.WorkPointer = term + *term;
if ( ApplyExec(term,C->lhs[level][2],level) < C->lhs[level][2] ) {
AT.WorkPointer = term + *term;
*AN.RepPoint = 1;
goto ReStart;
}
AT.WorkPointer = term + *term;
break;
/*
case TYPEAPPLYRESET:
AT.WorkPointer = term + *term;
if ( ApplyReset(level) ) goto GenCall;
AT.WorkPointer = term + *term;
break;
*/
case TYPECHAININ:
{ int lter = *term;
AT.WorkPointer = term + *term;
if ( ChainIn(BHEAD term,C->lhs[level][2]) ) goto GenCall;
AT.WorkPointer = term + *term;
/* Symmetry properties might mean the term has vanished */
if ( *term == 0 ) goto Return0;
if ( *term != lter ) *AN.RepPoint = 1;
}
break;
case TYPECHAINOUT:
{ int lter = *term;
AT.WorkPointer = term + *term;
if ( ChainOut(BHEAD term,C->lhs[level][2]) ) goto GenCall;
AT.WorkPointer = term + *term;
if ( *term != lter ) *AN.RepPoint = 1;
}
break;
case TYPEFACTOR:
AT.WorkPointer = term + *term;
if ( DollarFactorize(BHEAD C->lhs[level][2]) ) goto GenCall;
AT.WorkPointer = term + *term;
break;
case TYPEARGIMPLODE:
AT.WorkPointer = term + *term;
if ( ArgumentImplode(BHEAD term,C->lhs[level]) ) goto GenCall;
AT.WorkPointer = term + *term;
break;
case TYPEARGEXPLODE:
AT.WorkPointer = term + *term;
if ( ArgumentExplode(BHEAD term,C->lhs[level]) ) goto GenCall;
AT.WorkPointer = term + *term;
break;
case TYPEDENOMINATORS:
if ( DenToFunction(term,C->lhs[level][2]) ) goto ReStart;
break;
case TYPEDROPCOEFFICIENT:
DropCoefficient(BHEAD term);
break;
case TYPETRANSFORM:
AT.WorkPointer = term + *term;
if ( RunTransform(BHEAD term,C->lhs[level]+2) ) goto GenCall;
AT.WorkPointer = term + *term;
if ( *term == 0 ) goto Return0;
goto ReStart;
case TYPETOPOLYNOMIAL:
AT.WorkPointer = term + *term;
termout = AT.WorkPointer;
if ( ConvertToPoly(BHEAD term,termout,C->lhs[level],0) < 0 ) goto GenCall;
if ( *termout == 0 ) goto Return0;
i = termout[0]; t = term; NCOPY(t,termout,i);
AT.WorkPointer = term + *term;
break;
case TYPEFROMPOLYNOMIAL:
AT.WorkPointer = term + *term;
termout = AT.WorkPointer;
if ( ConvertFromPoly(BHEAD term,termout,0,numxsymbol,0,0) < 0 ) goto GenCall;
if ( *term == 0 ) goto Return0;
i = termout[0]; t = term; NCOPY(t,termout,i);
AT.WorkPointer = term + *term;
goto ReStart;
case TYPEDOLOOP:
level = TestDoLoop(BHEAD C->lhs[level],level);
if ( level < 0 ) goto GenCall;
break;
case TYPEENDDOLOOP:
level = TestEndDoLoop(BHEAD C->lhs[C->lhs[level][2]],C->lhs[level][2]);
if ( level < 0 ) goto GenCall;
break;
case TYPEDROPSYMBOLS:
DropSymbols(BHEAD term);
break;
case TYPEPUTINSIDE:
AT.WorkPointer = term + *term;
if ( PutInside(BHEAD term,C->lhs[level]) < 0 ) goto GenCall;
AT.WorkPointer = term + *term;
/*
* We need to call Generator() to convert slow notation to
* fast notation, which fixes Issue #30.
*/
if ( Generator(BHEAD term,level) < 0 ) goto GenCall;
goto Return0;
case TYPETOSPECTATOR:
if ( PutInSpectator(term,C->lhs[level][2]) < 0 ) goto GenCall;
goto Return0;
case TYPECANONICALIZE:
AT.WorkPointer = term + *term;
if ( DoCanonicalize(BHEAD term,C->lhs[level]) ) goto GenCall;
AT.WorkPointer = term + *term;
if ( *term == 0 ) goto Return0;
break;
case TYPESWITCH:
AT.WorkPointer = term + *term;
if ( DoSwitch(BHEAD term,C->lhs[level]) ) goto GenCall;
goto Return0;
case TYPEENDSWITCH:
AT.WorkPointer = term + *term;
if ( DoEndSwitch(BHEAD term,C->lhs[level]) ) goto GenCall;
goto Return0;
case TYPESETUSERFLAG:
Expressions[AR.CurExpr].uflags |= 1 << (C->lhs[level][2]);
break;
case TYPECLEARUSERFLAG:
Expressions[AR.CurExpr].uflags &= ~(1 << (C->lhs[level][2]));
break;
case TYPEALLLOOPS:
AT.WorkPointer = term + *term;
if ( AllLoops(BHEAD term,level) ) goto GenCall;
goto Return0;
case TYPEALLPATHS:
AT.WorkPointer = term + *term;
if ( AllPaths(BHEAD term,level) ) goto GenCall;
goto Return0;
#ifdef WITHFLOAT
case TYPEEVALUATE:
AT.WorkPointer = term + *term;
if ( C->lhs[level][2] == MZV
|| C->lhs[level][2] == EULER
|| C->lhs[level][2] == MZVHALF
|| C->lhs[level][2] == ALLMZVFUNCTIONS
) {
if ( EvaluateEuler(BHEAD term,level,C->lhs[level][2]) ) goto GenCall;
}
else {
if ( EvaluateFun(BHEAD term,level,C->lhs[level]) ) goto GenCall;
}
/*
else if ( C->lhs[level][2] == SQRTFUNCTION ) {
if ( EvaluateSqrt(BHEAD term,level,C->lhs[level][2]) ) goto GenCall;
}
else {
MLOCK(ErrorMessageLock);
MesPrint("Illegal function %d in evaluate statement.",C->lhs[level][2]);
MUNLOCK(ErrorMessageLock);
goto GenCall;
}
*/
goto Return0;
case TYPETOFLOAT:
AT.WorkPointer = term + *term;
if ( ToFloat(BHEAD term,level) ) goto GenCall;
goto Return0;
case TYPETORAT:
AT.WorkPointer = term + *term;
if ( ToRat(BHEAD term,level) ) goto GenCall;
goto Return0;
case TYPESTRICTROUNDING:
AT.WorkPointer = term + *term;
if ( StrictRounding(BHEAD term,level,C->lhs[level][2],C->lhs[level][3]) ) goto GenCall;
goto Return0;
case TYPECHOP:
AT.WorkPointer = term + *term;
if ( Chop(BHEAD term,level) ) goto GenCall;
goto Return0;
#endif
}
goto SkipCount;
/*
#] Special action :
*/
}
} while ( ( i = TestMatch(BHEAD term,&level) ) == 0 );
if ( AT.WorkPointer < term + *term ) AT.WorkPointer = term + *term;
if ( i > 0 ) replac = TestSub(BHEAD term,level);
else replac = i;
if ( replac >= 0 || AT.TMout[1] != SYMMETRIZE ) {
*AN.RepPoint = 1;
AR.expchanged = 1;
}
if ( replac < 0 ) { /* Terms come from automatic generation */
AutoGen: i = *AT.TMout;
t = termout = AT.WorkPointer;
if ( ( AT.WorkPointer += i ) > AT.WorkTop ) goto OverWork;
accum = AT.TMout;
while ( --i >= 0 ) *t++ = *accum++;
if ( (*(FG.Operation[termout[1]]))(BHEAD term,termout,replac,level) ) goto GenCall;
AT.WorkPointer = termout;
goto Return0;
}
}
if ( applyflag ) { TableReset(); applyflag = 0; }
if ( AN.TeInFun ) { /* Match in function argument */
if ( AN.TeInFun < 0 && !AN.TeSuOut ) {
if ( AR.TePos >= 0 ) goto AutoGen;
switch ( AN.TeInFun ) {
case -1:
if ( DoDistrib(BHEAD term,level) ) goto GenCall;
break;
case -2:
if ( DoDelta3(BHEAD term,level) ) goto GenCall;
break;
case -3:
if ( DoTableExpansion(term,level) ) goto GenCall;
break;
case -4:
if ( FactorIn(BHEAD term,level) ) goto GenCall;
break;
case -5:
if ( FactorInExpr(BHEAD term,level) ) goto GenCall;
break;
case -6:
if ( TermsInBracket(BHEAD term,level) < 0 ) goto GenCall;
break;
case -7:
if ( ExtraSymFun(BHEAD term,level) < 0 ) goto GenCall;
break;
case -8:
if ( GCDfunction(BHEAD term,level) < 0 ) goto GenCall;
break;
case -9:
if ( DIVfunction(BHEAD term,level,0) < 0 ) goto GenCall;
break;
case -10:
if ( DIVfunction(BHEAD term,level,1) < 0 ) goto GenCall;
break;
case -11:
if ( DIVfunction(BHEAD term,level,2) < 0 ) goto GenCall;
break;
case -12:
if ( DoPermutations(BHEAD term,level) ) goto GenCall;
break;
case -13:
if ( DoPartitions(BHEAD term,level) ) goto GenCall;
break;
case -14:
if ( DIVfunction(BHEAD term,level,3) < 0 ) goto GenCall;
break;
case -15:
if ( GenDiagrams(BHEAD term,level) < 0 ) goto GenCall;
break;
}
}
else {
termout = AT.WorkPointer;
AT.WorkPointer = (WORD *)(((UBYTE *)(AT.WorkPointer)) + AM.MaxTer);
if ( AT.WorkPointer > AT.WorkTop ) goto OverWork;
if ( InFunction(BHEAD term,termout) ) goto GenCall;
AT.WorkPointer = termout + *termout;
*AN.RepPoint = 1;
AR.expchanged = 1;
if ( *termout && Generator(BHEAD termout,level) < 0 ) goto GenCall;
AT.WorkPointer = termout;
}
}
else if ( replac > 0 ) {
power = AN.TeSuOut;
tepos = AR.TePos;
if ( power < 0 ) { /* Table expansion */
power = -power; tepos = 0;
}
extractbuff = AT.TMbuff;
if ( extractbuff == AM.dbufnum ) {
d = DolToTerms(BHEAD replac);
if ( d && d->where != 0 ) {
iscopy = 1;
if ( AT.TMdolfac > 0 ) { /* We need a factor */
if ( AT.TMdolfac == 1 ) {
if ( d->nfactors ) {
numfac[0] = 4;
numfac[1] = d->nfactors;
numfac[2] = 1;
numfac[3] = 3;
numfac[4] = 0;
}
else {
numfac[0] = 0;
}
StartBuf = numfac;
}
else {
if ( (AT.TMdolfac-1) > d->nfactors && d->nfactors > 0 ) {
MLOCK(ErrorMessageLock);
MesPrint("Attempt to use an nonexisting factor %d of a $-variable",(WORD)(AT.TMdolfac-1));
if ( d->nfactors == 1 )
MesPrint("There is only one factor");
else
MesPrint("There are only %d factors",(WORD)(d->nfactors));
MUNLOCK(ErrorMessageLock);
goto GenCall;
}
if ( d->nfactors > 1 ) {
DOLLARS dd;
LONG dsize;
WORD *td1, *td2;
dd = Dollars + replac;
#ifdef WITHPTHREADS
{
int nummodopt, dtype = -1;
if ( AS.MultiThreaded && ( AC.mparallelflag == PARALLELFLAG ) ) {
for ( nummodopt = 0; nummodopt < NumModOptdollars; nummodopt++ ) {
if ( replac == ModOptdollars[nummodopt].number ) break;
}
if ( nummodopt < NumModOptdollars ) {
dtype = ModOptdollars[nummodopt].type;
if ( dtype == MODLOCAL ) {
dd = ModOptdollars[nummodopt].dstruct+AT.identity;
}
}
}
}
#endif
dsize = dd->factors[AT.TMdolfac-2].size;
/*
We copy only the factor we need
*/
if ( dsize == 0 ) {
numfac[0] = 4;
numfac[1] = d->factors[AT.TMdolfac-2].value;
numfac[2] = 1;
numfac[3] = 3;
numfac[4] = 0;
StartBuf = numfac;
if ( numfac[1] < 0 ) {
numfac[1] = -numfac[1];
numfac[3] = -numfac[3];
}
}
else {
d->factors[AT.TMdolfac-2].where = td2 = (WORD *)Malloc1(
(dsize+1)*sizeof(WORD),"Copy of factor");
td1 = dd->factors[AT.TMdolfac-2].where;
StartBuf = td2;
d->size = dsize; d->type = DOLTERMS;
NCOPY(td2,td1,dsize);
*td2 = 0;
}
}
else if ( d->nfactors == 1 ) {
StartBuf = d->where;
}
else {
MLOCK(ErrorMessageLock);
if ( d->nfactors == 0 ) {
MesPrint("Attempt to use factor %d of an unfactored $-variable",(WORD)(AT.TMdolfac-1));
}
else {
MesPrint("Internal error. Illegal number of factors for $-variable");
}
MUNLOCK(ErrorMessageLock);
goto GenCall;
}
}
}
else StartBuf = d->where;
}
else {
d = Dollars + replac;
StartBuf = zeroDollar;
}
posisub = 0;
i = DetCommu(d->where);
#ifdef WITHPTHREADS
if ( AS.MultiThreaded ) {
for ( nummodopt = 0; nummodopt < NumModOptdollars; nummodopt++ ) {
if ( replac == ModOptdollars[nummodopt].number ) break;
}
if ( nummodopt < NumModOptdollars ) {
dtype = ModOptdollars[nummodopt].type;
if ( dtype != MODLOCAL && dtype != MODSUM ) {
if ( StartBuf[0] && StartBuf[StartBuf[0]] ) {
MLOCK(ErrorMessageLock);
MesPrint("A dollar variable with modoption max or min can have only one term");
MUNLOCK(ErrorMessageLock);
goto GenCall;
}
LOCK(d->pthreadslockread);
}
}
}
#endif
}
else {
StartBuf = cbuf[extractbuff].Buffer;
posisub = cbuf[extractbuff].rhs[replac] - StartBuf;
i = (WORD)cbuf[extractbuff].CanCommu[replac];
}
if ( power == 1 ) { /* Just a single power */
termout = AT.WorkPointer;
AT.WorkPointer = (WORD *)(((UBYTE *)(AT.WorkPointer)) + AM.MaxTer);
if ( AT.WorkPointer > AT.WorkTop ) goto OverWork;
while ( StartBuf[posisub] ) {
if ( extractbuff == AT.allbufnum ) WildDollars(BHEAD &(StartBuf[posisub]));
AT.WorkPointer = (WORD *)(((UBYTE *)(termout)) + AM.MaxTer);
if ( InsertTerm(BHEAD term,replac,extractbuff,
&(StartBuf[posisub]),termout,tepos) < 0 ) goto GenCall;
AT.WorkPointer = termout + *termout;
*AN.RepPoint = 1;
AR.expchanged = 1;
posisub += StartBuf[posisub];
/*
For multiple table substitutions it may be better to
do modulus arithmetic right here
Turns out to be not very effective.
if ( AN.ncmod != 0 ) {
if ( Modulus(termout) ) goto GenCall;
if ( !*termout ) goto Return0;
}
*/
#ifdef WITHPTHREADS
if ( dtype > 0 && dtype != MODLOCAL && dtype != MODSUM ) { UNLOCK(d->pthreadslockread); }
if ( ( AS.Balancing && CC->numrhs == 0 ) && StartBuf[posisub] ) {
if ( ( id = ConditionalGetAvailableThread() ) >= 0 ) {
if ( BalanceRunThread(BHEAD id,termout,level) < 0 ) goto GenCall;
}
}
else
#endif
if ( Generator(BHEAD termout,level) < 0 ) goto GenCall;
#ifdef WITHPTHREADS
if ( dtype > 0 && dtype != MODLOCAL ) { dtype = 0; break; }
#endif
if ( iscopy == 0 && ( extractbuff != AM.dbufnum ) ) {
/*
There are cases in which a bigger buffer is created
on the fly, like with wildcard buffers.
We play it safe here. Maybe we can be more selective
in some distant future?
*/
StartBuf = cbuf[extractbuff].Buffer;
}
}
if ( extractbuff == AT.allbufnum ) {
CBUF *Ce = cbuf + extractbuff;
Ce->Pointer = Ce->rhs[Ce->numrhs--];
}
#ifdef WITHPTHREADS
if ( dtype > 0 && dtype != MODLOCAL && dtype != MODSUM ) { UNLOCK(d->pthreadslockread); dtype = 0; }
#endif
if ( iscopy ) {
if ( d->nfactors > 1 ) {
int j;
for ( j = 0; j < d->nfactors; j++ ) {
if ( d->factors[j].where ) M_free(d->factors[j].where,"Copy of factor");
}
M_free(d->factors,"Dollar factors");
}
M_free(d,"Copy of dollar variable");
d = 0; iscopy = 0;
}
AT.WorkPointer = termout;
}
else if ( i <= 1 ) { /* Use binomials */
LONG posit, olw;
WORD *same, *ow = AT.WorkPointer;
LONG olpw = AT.posWorkPointer;
power1 = power+1;
WantAddLongs(power1);
olw = posit = AT.lWorkPointer; AT.lWorkPointer += power1;
same = ++AT.WorkPointer;
a = accum = ( AT.WorkPointer += power1+1 );
AT.WorkPointer = (WORD *)(((UBYTE *)(AT.WorkPointer)) + 2*AM.MaxTer);
if ( AT.WorkPointer > AT.WorkTop ) goto OverWork;
AT.lWorkSpace[posit] = posisub;
same[-1] = 0;
*same = 1;
*accum = 0;
tepos = AR.TePos;
i = 1;
do {
if ( StartBuf[AT.lWorkSpace[posit]] ) {
if ( ( a = PasteTerm(BHEAD i-1,accum,
&(StartBuf[AT.lWorkSpace[posit]]),i,*same) ) == 0 )
goto GenCall;
AT.lWorkSpace[posit+1] = AT.lWorkSpace[posit];
same[1] = *same + 1;
if ( i > 1 && AT.lWorkSpace[posit] < AT.lWorkSpace[posit-1] ) *same = 1;
AT.lWorkSpace[posit] += StartBuf[AT.lWorkSpace[posit]];
i++;
posit++;
same++;
}
else {
i--; posit--; same--;
}
if ( i > power ) {
termout = AT.WorkPointer = a;
AT.WorkPointer = (WORD *)(((UBYTE *)(AT.WorkPointer)) + 2*AM.MaxTer);
if ( AT.WorkPointer > AT.WorkTop )
goto OverWork;
if ( FiniTerm(BHEAD term,accum,termout,replac,tepos) ) goto GenCall;
AT.WorkPointer = termout + *termout;
*AN.RepPoint = 1;
AR.expchanged = 1;
#ifdef WITHPTHREADS
if ( dtype > 0 && dtype != MODLOCAL && dtype != MODSUM ) { UNLOCK(d->pthreadslockread); }
if ( ( AS.Balancing && CC->numrhs == 0 ) && ( i > 0 )
&& ( id = ConditionalGetAvailableThread() ) >= 0 ) {
if ( BalanceRunThread(BHEAD id,termout,level) < 0 ) goto GenCall;
}
else
#endif
if ( Generator(BHEAD termout,level) ) goto GenCall;
#ifdef WITHPTHREADS
if ( dtype > 0 && dtype != MODLOCAL ) { dtype = 0; break; }
#endif
if ( iscopy == 0 && ( extractbuff != AM.dbufnum ) )
StartBuf = cbuf[extractbuff].Buffer;
i--; posit--; same--;
}
} while ( i > 0 );
#ifdef WITHPTHREADS
if ( dtype > 0 && dtype != MODLOCAL && dtype != MODSUM ) { UNLOCK(d->pthreadslockread); dtype = 0; }
#endif
if ( iscopy ) {
if ( d->nfactors > 1 ) {
int j;
for ( j = 0; j < d->nfactors; j++ ) {
if ( d->factors[j].where ) M_free(d->factors[j].where,"Copy of factor");
}
M_free(d->factors,"Dollar factors");
}
M_free(d,"Copy of dollar variable");
d = 0; iscopy = 0;
}
AT.WorkPointer = ow; AT.lWorkPointer = olw; AT.posWorkPointer = olpw;
}
else { /* No binomials */
LONG posit, olw, olpw = AT.posWorkPointer;
WantAddLongs(power);
posit = olw = AT.lWorkPointer; AT.lWorkPointer += power;
a = accum = AT.WorkPointer;
AT.WorkPointer = (WORD *)(((UBYTE *)(AT.WorkPointer)) + 2*AM.MaxTer);
if ( AT.WorkPointer > AT.WorkTop ) goto OverWork;
for ( i = 0; i < power; i++ ) AT.lWorkSpace[posit++] = posisub;
posit = olw;
*accum = 0;
tepos = AR.TePos;
i = 0;
while ( i >= 0 ) {
if ( StartBuf[AT.lWorkSpace[posit]] ) {
if ( ( a = PasteTerm(BHEAD i,accum,
&(StartBuf[AT.lWorkSpace[posit]]),1,1) ) == 0 ) goto GenCall;
AT.lWorkSpace[posit] += StartBuf[AT.lWorkSpace[posit]];
i++; posit++;
}
else {
AT.lWorkSpace[posit--] = posisub;
i--;
}
if ( i >= power ) {
termout = AT.WorkPointer = a;
AT.WorkPointer = (WORD *)(((UBYTE *)(AT.WorkPointer)) + 2*AM.MaxTer);
if ( AT.WorkPointer > AT.WorkTop ) goto OverWork;
if ( FiniTerm(BHEAD term,accum,termout,replac,tepos) ) goto GenCall;
AT.WorkPointer = termout + *termout;
*AN.RepPoint = 1;
AR.expchanged = 1;
#ifdef WITHPTHREADS
if ( dtype > 0 && dtype != MODLOCAL && dtype != MODSUM ) { UNLOCK(d->pthreadslockread); }
if ( ( AS.Balancing && CC->numrhs == 0 ) && ( i > 0 ) && ( id = ConditionalGetAvailableThread() ) >= 0 ) {
if ( BalanceRunThread(BHEAD id,termout,level) < 0 ) goto GenCall;
}
else
#endif
if ( Generator(BHEAD termout,level) ) goto GenCall;
#ifdef WITHPTHREADS
if ( dtype > 0 && dtype != MODLOCAL && dtype != MODSUM ) { dtype = 0; break; }
#endif
if ( iscopy == 0 && ( extractbuff != AM.dbufnum ) )
StartBuf = cbuf[extractbuff].Buffer;
i--; posit--;
}
}
#ifdef WITHPTHREADS
if ( dtype > 0 && dtype != MODLOCAL && dtype != MODSUM ) { UNLOCK(d->pthreadslockread); dtype = 0; }
#endif
if ( iscopy ) {
if ( d->nfactors > 1 ) {
int j;
for ( j = 0; j < d->nfactors; j++ ) {
if ( d->factors[j].where ) M_free(d->factors[j].where,"Copy of factor");
}
M_free(d->factors,"Dollar factors");
}
M_free(d,"Copy of dollar variable");
d = 0; iscopy = 0;
}
AT.WorkPointer = accum;
AT.lWorkPointer = olw;
AT.posWorkPointer = olpw;
}
}
else { /* Expression from disk */
POSITION StartPos;
LONG position, olpw, opw, comprev, extra;
RENUMBER renumber;
WORD *Freeze, *aa, *dummies;
replac = -replac-1;
power = AN.TeSuOut;
Freeze = AN.Frozen;
if ( Expressions[replac].status == STOREDEXPRESSION ) {
POSITION firstpos;
SETSTARTPOS(firstpos);
/* Note that AT.TMaddr is needed for GetTable just once! */
/*
We need space for the previous term in the compression
This is made available in AR.CompressBuffer, although we may get
problems with this sooner or later. Hence we need to keep
a set of pointers in AR.CompressBuffer
Note that after the last call there has been no use made
of AR.CompressPointer, so it points automatically at its original
position!
*/
WantAddPointers(power+1);
comprev = opw = AT.pWorkPointer;
AT.pWorkPointer += power+1;
WantAddPositions(power+1);
position = olpw = AT.posWorkPointer;
AT.posWorkPointer += power + 1;
AT.pWorkSpace[comprev++] = AR.CompressPointer;
for ( i = 0; i < power; i++ ) {
PUTZERO(AT.posWorkSpace[position]); position++;
}
position = olpw;
if ( ( renumber = GetTable(replac,&(AT.posWorkSpace[position]),1) ) == 0 ) goto GenCall;
dummies = AT.WorkPointer;
*dummies++ = AR.CurDum;
AT.WorkPointer += power+2;
accum = AT.WorkPointer;
AT.WorkPointer = (WORD *)(((UBYTE *)(AT.WorkPointer)) + 2*AM.MaxTer);
if ( AT.WorkPointer > AT.WorkTop ) goto OverWork;
aa = AT.WorkPointer;
*accum = 0;
i = 0; StartPos = AT.posWorkSpace[position];
dummies[i] = AR.CurDum;
while ( i >= 0 ) {
skippedfirst:
AR.CompressPointer = AT.pWorkSpace[comprev-1];
if ( ( extra = PasteFile(BHEAD i,accum,&(AT.posWorkSpace[position])
,&a,renumber,Freeze,replac) ) < 0 ) goto GenCall;
if ( Expressions[replac].numdummies > 0 ) {
AR.CurDum = dummies[i] + Expressions[replac].numdummies;
}
if ( NOTSTARTPOS(firstpos) ) {
if ( ISMINPOS(firstpos) || ISEQUALPOS(firstpos,AT.posWorkSpace[position]) ) {
firstpos = AT.posWorkSpace[position];
/*
ADDPOS(AT.posWorkSpace[position],extra * sizeof(WORD));
*/
goto skippedfirst;
}
}
if ( extra ) {
/*
ADDPOS(AT.posWorkSpace[position],extra * sizeof(WORD));
*/
i++; AT.posWorkSpace[++position] = StartPos;
AT.pWorkSpace[comprev++] = AR.CompressPointer;
dummies[i] = AR.CurDum;
}
else {
PUTZERO(AT.posWorkSpace[position]); position--; i--;
AR.CurDum = dummies[i];
comprev--;
}
if ( i >= power ) {
termout = AT.WorkPointer = a;
AT.WorkPointer = (WORD *)(((UBYTE *)(AT.WorkPointer)) + 2*AM.MaxTer);
if ( AT.WorkPointer > AT.WorkTop ) goto OverWork;
if ( FiniTerm(BHEAD term,accum,termout,replac,0) ) goto GenCall;
if ( *termout ) {
AT.WorkPointer = termout + *termout;
*AN.RepPoint = 1;
AR.expchanged = 1;
#ifdef WITHPTHREADS
if ( ( AS.Balancing && CC->numrhs == 0 ) && ( i > 0 ) && ( id = ConditionalGetAvailableThread() ) >= 0 ) {
if ( BalanceRunThread(BHEAD id,termout,level) < 0 ) goto GenCall;
}
else
#endif
if ( Generator(BHEAD termout,level) ) goto GenCall;
}
i--; position--;
AR.CurDum = dummies[i];
comprev--;
}
AT.WorkPointer = aa;
}
AT.WorkPointer = accum;
AT.posWorkPointer = olpw;
AT.pWorkPointer = opw;
/*
Bug fix. See also GetTable
#ifdef WITHPTHREADS
M_free(renumber->symb.lo,"VarSpace");
M_free(renumber,"Renumber");
#endif
*/
if ( renumber->symb.lo != AN.dummyrenumlist )
M_free(renumber->symb.lo,"VarSpace");
M_free(renumber,"Renumber");
}
else { /* Active expression */
aa = accum = AT.WorkPointer;
if ( ( (WORD *)(((UBYTE *)(AT.WorkPointer)) + 2 * AM.MaxTer + sizeof(WORD)) ) > AT.WorkTop )
goto OverWork;
*accum++ = -1; AT.WorkPointer++;
if ( DoOnePow(BHEAD term,power,replac,accum,aa,level,Freeze) ) goto GenCall;
AT.WorkPointer = aa;
}
}
Return0:
AR.CurDum = DumNow;
AN.RepPoint = RepSto;
CC->numrhs = oldtoprhs;
CC->Pointer = CC->Buffer + oldcpointer;
CCC->numrhs = oldatoprhs;
CCC->Pointer = CCC->Buffer + oldacpointer;
return(0);
GenCall:
if ( AM.tracebackflag ) {
termout = term;
MLOCK(ErrorMessageLock);
AO.OutFill = AO.OutputLine = (UBYTE *)AT.WorkPointer;
AO.OutSkip = 3;
FiniLine();
i = *termout;
while ( --i >= 0 ) {
TalToLine((UWORD)(*termout++));
TokenToLine((UBYTE *)" ");
}
AO.OutSkip = 0;
FiniLine();
MesCall("Generator");
MUNLOCK(ErrorMessageLock);
}
CC->numrhs = oldtoprhs;
CC->Pointer = CC->Buffer + oldcpointer;
CCC->numrhs = oldatoprhs;
CCC->Pointer = CCC->Buffer + oldacpointer;
return(-1);
OverWork:
CC->numrhs = oldtoprhs;
CC->Pointer = CC->Buffer + oldcpointer;
CCC->numrhs = oldatoprhs;
CCC->Pointer = CCC->Buffer + oldacpointer;
MLOCK(ErrorMessageLock);
MesWork();
MUNLOCK(ErrorMessageLock);
return(-1);
}
/*
#] Generator :
#[ DoOnePow : WORD DoOnePow(term,power,nexp,accum,aa,level,freeze)
*/
/**
* Routine gets one power of an expression in the scratch system.
* If there are more powers needed there will be a recursion.
*
* No attempt is made to use binomials because we have no
* information about commuting properties.
*
* There is a searching for the contents of brackets if needed.
* This searching may be rather slow because of the single links.
*
* @param term is the term we are adding to.
* @param power is the power of the expression that we need.
* @param nexp is the number of the expression.
* @param accum is the accumulator of terms. It accepts the termfragments
* that are made into a proper term in FiniTerm
* @param aa points to the start of the entire accumulator. In *aa
* we store the number of term fragments that are in the
* accumulator.
* @param level is the current depth in the tree of statements. It is
* needed to continue to the next operation/substitution
* with each generated term
* @param freeze is the pointer to the bracket information that should
* be matched.
*/
#ifdef WITHPTHREADS
char freezestring[] = "freeze<-xxxx";
#endif
int DoOnePow(PHEAD WORD *term, WORD power, WORD nexp, WORD * accum,
WORD *aa, WORD level, WORD *freeze)
{
GETBIDENTITY
POSITION oldposition, startposition;
WORD *acc, *termout, fromfreeze = 0;
WORD *oldipointer = AR.CompressPointer;
FILEHANDLE *fi;
WORD type, retval;
WORD oldGetOneFile = AR.GetOneFile;
WORD olddummies = AR.CurDum;
WORD extradummies = Expressions[nexp].numdummies;
/*
The next code is for some tricky debugging. (5-jan-2010 JV)
Normally it should be disabled.
*/
/*
#ifdef WITHPTHREADS
if ( freeze ) {
MLOCK(ErrorMessageLock);
if ( AT.identity < 10 ) {
freezestring[8] = '0'+AT.identity;
freezestring[9] = '>';
freezestring[10] = 0;
}
else if ( AT.identity < 100 ) {
freezestring[8] = '0'+AT.identity/10;
freezestring[9] = '0'+AT.identity%10;
freezestring[10] = '>';
freezestring[11] = 0;
}
else {
freezestring[8] = 0;
}
PrintTerm(freeze,freezestring);
MUNLOCK(ErrorMessageLock);
}
#else
if ( freeze ) PrintTerm(freeze,"freeze");
#endif
*/
type = Expressions[nexp].status;
if ( type == HIDDENLEXPRESSION || type == HIDDENGEXPRESSION
|| type == DROPHLEXPRESSION || type == DROPHGEXPRESSION
|| type == UNHIDELEXPRESSION || type == UNHIDEGEXPRESSION ) {
AR.GetOneFile = 2; fi = AR.hidefile;
}
else {
AR.GetOneFile = 0; fi = AR.infile;
}
if ( fi->handle >= 0 ) {
PUTZERO(oldposition);
#ifdef WITHSEEK
LOCK(AS.inputslock);
SeekFile(fi->handle,&oldposition,SEEK_CUR);
UNLOCK(AS.inputslock);
#endif
}
else {
SETBASEPOSITION(oldposition,fi->POfill-fi->PObuffer);
}
if ( freeze && ( Expressions[nexp].bracketinfo != 0 ) ) {
POSITION *brapos;
/*
There is a bracket index
AR.CompressPointer = oldipointer;
*/
(*aa)++;
power--;
if ( ( brapos = FindBracket(nexp,freeze) ) == 0 )
goto EndExpr;
startposition = *brapos;
goto doterms;
}
startposition = AS.OldOnFile[nexp];
retval = GetOneTerm(BHEAD accum,fi,&startposition,0);
if ( retval > 0 ) { /* Skip prototype */
(*aa)++;
power--;
doterms:
AR.CompressPointer = oldipointer;
for (;;) {
retval = GetOneTerm(BHEAD accum,fi,&startposition,0);
if ( retval <= 0 ) break;
/*
Here should come the code to test for [].
*/
if ( freeze ) {
WORD *t, *m, *r, *mstop;
WORD *tset;
t = accum;
m = freeze;
m += *m;
m -= ABS(m[-1]);
mstop = m;
m = freeze + 1;
r = t;
r += *t;
r -= ABS(r[-1]);
t++;
tset = t;
while ( t < r && *t != HAAKJE ) t += t[1];
if ( t >= r ) {
if ( m < mstop ) {
if ( fromfreeze ) goto EndExpr;
goto NextTerm;
}
t = tset;
}
else {
r = tset;
while ( r < t && m < mstop ) {
if ( *r == *m ) { m++; r++; }
else {
if ( fromfreeze ) goto EndExpr;
goto NextTerm;
}
}
if ( r < t || m < mstop ) {
if ( fromfreeze ) goto EndExpr;
goto NextTerm;
}
}
fromfreeze = 1;
r = tset;
m = accum;
m += *m;
while ( t < m ) *r++ = *t++;
*accum = WORDDIF(r,accum);
}
if ( extradummies > 0 ) {
if ( olddummies > AM.IndDum ) {
MoveDummies(BHEAD accum,olddummies-AM.IndDum);
}
AR.CurDum = olddummies+extradummies;
}
acc = accum;
acc += *acc;
if ( power <= 0 ) {
termout = acc;
AT.WorkPointer = (WORD *)(((UBYTE *)(acc)) + 2*AM.MaxTer);
if ( AT.WorkPointer > AT.WorkTop ) {
MLOCK(ErrorMessageLock);
MesWork();
MUNLOCK(ErrorMessageLock);
return(-1);
}
if ( FiniTerm(BHEAD term,aa,termout,nexp,0) ) goto PowCall;
if ( *termout ) {
MarkPolyRatFunDirty(termout)
/* PolyFunDirty(BHEAD termout); */
AT.WorkPointer = termout + *termout;
*AN.RepPoint = 1;
AR.expchanged = 1;
if ( Generator(BHEAD termout,level) ) goto PowCall;
}
}
else {
if ( acc > AT.WorkTop ) {
MLOCK(ErrorMessageLock);
MesWork();
MUNLOCK(ErrorMessageLock);
return(-1);
}
if ( DoOnePow(BHEAD term,power,nexp,acc,aa,level,freeze) ) goto PowCall;
}
NextTerm:;
AR.CompressPointer = oldipointer;
}
EndExpr:
(*aa)--;
}
AR.CompressPointer = oldipointer;
if ( fi->handle >= 0 ) {
#ifdef WITHSEEK
LOCK(AS.inputslock);
SeekFile(fi->handle,&oldposition,SEEK_SET);
UNLOCK(AS.inputslock);
if ( ISNEGPOS(oldposition) ) {
MLOCK(ErrorMessageLock);
MesPrint("File error");
goto PowCall2;
}
#endif
}
else {
fi->POfill = fi->PObuffer + BASEPOSITION(oldposition);
}
AR.GetOneFile = oldGetOneFile;
AR.CurDum = olddummies;
return(0);
PowCall:;
MLOCK(ErrorMessageLock);
#ifdef WITHSEEK
PowCall2:;
#endif
MesCall("DoOnePow");
MUNLOCK(ErrorMessageLock);
SETERROR(-1)
}
/*
#] DoOnePow :
#[ Deferred : WORD Deferred(term,level)
*/
/**
* Picks up the deferred brackets.
* These are the bracket contents of which we postpone the reading
* when we use the 'Keep Brackets' statement. These contents are
* multiplying the terms just before they are sent to the sorting
* system.
* Special attention goes to having it thread-safe
* We have to lock positioning the file and reading it in
* a thread specific buffer.
*
* @param term The term that must be multiplied by the contents of the
* current bracket
* @param level The compiler level. This is needed because after
* multiplying term by term we call Generator again.
*/
int Deferred(PHEAD WORD *term, WORD level)
{
GETBIDENTITY
POSITION startposition;
WORD *t, *m, *mstop, *tstart, decr, oldb, *termout, i, *oldwork, retval;
WORD *oldipointer = AR.CompressPointer, *oldPOfill = AR.infile->POfill;
WORD oldGetOneFile = AR.GetOneFile;
AR.GetOneFile = 1;
oldwork = AT.WorkPointer;
AT.WorkPointer = (WORD *)(((UBYTE *)(AT.WorkPointer)) + AM.MaxTer);
termout = AT.WorkPointer;
AR.DeferFlag = 0;
startposition = AR.DefPosition;
/*
Store old position
*/
if ( AR.infile->handle >= 0 ) {
/*
PUTZERO(oldposition);
SeekFile(AR.infile->handle,&oldposition,SEEK_CUR);
*/
}
else {
/*
SETBASEPOSITION(oldposition,AR.infile->POfill-AR.infile->PObuffer);
*/
AR.infile->POfill = (WORD *)((UBYTE *)(AR.infile->PObuffer)
+BASEPOSITION(startposition));
}
/*
Look in the CompressBuffer where the bracket contents start
*/
t = m = AR.CompressBuffer;
t += *t;
mstop = t - ABS(t[-1]);
m++;
while ( *m != HAAKJE && m < mstop ) m += m[1];
if ( m >= mstop ) { /* No deferred action! */
AT.WorkPointer = term + *term;
if ( Generator(BHEAD term,level) ) goto DefCall;
AR.DeferFlag = 1;
AT.WorkPointer = oldwork;
AR.GetOneFile = oldGetOneFile;
return(0);
}
mstop = m + m[1];
decr = WORDDIF(mstop,AR.CompressBuffer)-1;
tstart = AR.CompressPointer + decr;
m = AR.CompressBuffer;
t = AR.CompressPointer;
i = *m;
NCOPY(t,m,i);
oldb = *tstart;
AR.TePos = 0;
AN.TeSuOut = 0;
/*
Status:
First bracket content starts at mstop.
Next term starts at startposition.
Decompression information is in AR.CompressPointer.
The outside of the bracket runs from AR.CompressBuffer+1 to mstop.
*/
for(;;) {
*tstart = *(AR.CompressPointer)-decr;
AR.CompressPointer = AR.CompressPointer+AR.CompressPointer[0];
if ( InsertTerm(BHEAD term,0,AM.rbufnum,tstart,termout,0) < 0 ) {
goto DefCall;
}
*tstart = oldb;
AT.WorkPointer = termout + *termout;
if ( Generator(BHEAD termout,level) ) goto DefCall;
AR.CompressPointer = oldipointer;
AT.WorkPointer = termout;
retval = GetOneTerm(BHEAD AT.WorkPointer,AR.infile,&startposition,0);
if ( retval >= 0 ) AR.CompressPointer = oldipointer;
if ( retval <= 0 ) break;
t = AR.CompressPointer;
if ( *t < (1 + decr + ABS(*(t+*t-1))) ) break;
t++;
m = AR.CompressBuffer+1;
while ( m < mstop ) {
if ( *m != *t ) goto Thatsit;
m++; t++;
}
}
Thatsit:;
/*
Finished. Reposition the file, restore information and return.
*/
if ( AR.infile->handle < 0 ) AR.infile->POfill = oldPOfill;
AR.DeferFlag = 1;
AR.GetOneFile = oldGetOneFile;
AT.WorkPointer = oldwork;
return(0);
DefCall:;
MLOCK(ErrorMessageLock);
MesCall("Deferred");
MUNLOCK(ErrorMessageLock);
SETERROR(-1)
}
/*
#] Deferred :
#[ PrepPoly : WORD PrepPoly(term,par)
*/
/**
* Routine checks whether the count of function AR.PolyFun is zero
* or one. If it is one and it has one scalarlike argument the
* coefficient of the term is pulled inside the argument.
* If the count is zero a new function is made with the coefficient
* as its only argument. The function should be placed at its
* proper position.
*
* When this function is active it places the PolyFun as last
* object before the coefficient. This is needed because otherwise
* the compress algorithm has problems in MergePatches.
*
* The bracket routine should also place the PolyFun at a
* comparable spot.
* The compression should then stop at the PolyFun. It doesn't
* really have to stop when writing the final result but this may
* be too complicated.
*
* The parameter par tells whether we are at groundlevel or
* inside a function or dollar variable.
*/
int PrepPoly(PHEAD WORD *term,WORD par)
{
GETBIDENTITY
WORD count = 0, i, jcoef, ncoef;
WORD *t, *m, *r, *tstop, *poly = 0, *v, *w, *vv, *ww;
WORD *oldworkpointer = AT.WorkPointer;
/*
The problem here is that the function will be forced into 'long'
notation. After this -SNUMBER,1 becomes 6,0,4,1,1,3 and the
pattern matcher cannot match a short 1 with a long 1.
But because this is an undocumented feature for very special
purposes, we don't do anything about it. (30-aug-2011)
*/
if ( AR.PolyFunType == 2 && AR.PolyFunExp != 2 ) {
WORD oldtype = AR.SortType;
AR.SortType = SORTHIGHFIRST;
if ( poly_ratfun_normalize(BHEAD term) != 0 ) Terminate(-1);
/* if ( ReadPolyRatFun(BHEAD term) != 0 ) Terminate(-1); */
oldworkpointer = AT.WorkPointer;
AR.SortType = oldtype;
}
AT.PolyAct = 0;
t = term;
GETSTOP(t,tstop);
t++;
while ( t < tstop ) {
if ( *t == AR.PolyFun ) {
if ( count > 0 ) return(0);
poly = t;
count++;
}
t += t[1];
}
r = m = term + *term;
i = ABS(m[-1]);
if ( par > 0 ) {
if ( count == 0 ) return(0);
else if ( AR.PolyFunType == 1 || (AR.PolyFunType == 2 && AR.PolyFunExp == 2) )
goto DoOne;
else if ( AR.PolyFunType == 2 )
goto DoTwo;
else
goto DoError;
}
else if ( count == 0 ) {
/*
#[ Create a PolyFun :
*/
poly = t = tstop;
if ( i == 3 && m[-2] == 1 && (m[-3]&MAXPOSITIVE) == m[-3] ) {
*m++ = AR.PolyFun;
if ( AR.PolyFunType == 1 || (AR.PolyFunType == 2 && AR.PolyFunExp == 2) ) {
*m++ = FUNHEAD+2;
FILLFUN(m)
*m++ = -SNUMBER;
*m = m[-2-FUNHEAD] < 0 ? -m[-4-FUNHEAD]: m[-4-FUNHEAD];
m++;
}
else if ( AR.PolyFunType == 2 ) {
*m++ = FUNHEAD+4;
FILLFUN(m)
*m++ = -SNUMBER;
*m = m[-2-FUNHEAD] < 0 ? -m[-4-FUNHEAD]: m[-4-FUNHEAD];
m++;
*m++ = -SNUMBER;
*m++ = 1;
}
}
else {
WORD *vm;
r = tstop;
if ( AR.PolyFunType == 1 || (AR.PolyFunType == 2 && AR.PolyFunExp == 2) ) {
*m++ = AR.PolyFun;
*m++ = FUNHEAD+ARGHEAD+i+1;
FILLFUN(m)
*m++ = ARGHEAD+i+1;
*m++ = 0;
FILLARG(m)
*m++ = i+1;
NCOPY(m,r,i);
}
else if ( AR.PolyFunType == 2 ) {
WORD *num, *den, size, sign, sizenum, sizeden;
if ( m[-1] < 0 ) { sign = -1; size = -m[-1]; }
else { sign = 1; size = m[-1]; }
num = m - size; size = (size-1)/2; den = num + size;
sizenum = size; while ( num[sizenum-1] == 0 ) sizenum--;
sizeden = size; while ( den[sizeden-1] == 0 ) sizeden--;
v = m;
AT.PolyAct = WORDDIF(v,term);
*v++ = AR.PolyFun;
v++;
FILLFUN(v);
vm = v;
*v++ = ARGHEAD+2*sizenum+2;
*v++ = 0;
FILLARG(v);
*v++ = 2*sizenum+2;
for ( i = 0; i < sizenum; i++ ) *v++ = num[i];
*v++ = 1;
for ( i = 1; i < sizenum; i++ ) *v++ = 0;
*v++ = sign*(2*sizenum+1);
if ( ToFast(vm,vm) ) v = vm+2;
vm = v;
*v++ = ARGHEAD+2*sizeden+2;
*v++ = 0;
FILLARG(v);
*v++ = 2*sizeden+2;
for ( i = 0; i < sizeden; i++ ) *v++ = den[i];
*v++ = 1;
for ( i = 1; i < sizeden; i++ ) *v++ = 0;
*v++ = 2*sizeden+1;
if ( ToFast(vm,vm) ) v = vm+2;
i = v-m;
m[1] = i;
w = num;
NCOPY(w,m,i);
*w++ = 1; *w++ = 1; *w++ = 3; *term = w - term;
return(0);
}
}
/*
#] Create a PolyFun :
*/
}
else if ( AR.PolyFunType == 1 || (AR.PolyFunType == 2 && AR.PolyFunExp == 2) ) {
DoOne:;
/*
#[ One argument :
*/
m = term + *term;
r = poly + poly[1];
if ( ( poly[1] == FUNHEAD+2 && poly[FUNHEAD+1] == 0
&& poly[FUNHEAD] == -SNUMBER ) || poly[1] == FUNHEAD ) return(1);
t = poly + FUNHEAD;
if ( t >= r ) return(0);
if ( m[-1] == 3 && *tstop == 1 && tstop[1] == 1 ) {
i = poly[1];
t = poly;
NCOPY(m,t,i);
}
else if ( *t <= -FUNCTION ) {
if ( t+1 < r ) return(0); /* More than one argument */
r = tstop;
*m++ = AR.PolyFun;
*m++ = FUNHEAD*2+ARGHEAD+i+1;
FILLFUN(m)
*m++ = FUNHEAD+ARGHEAD+i+1;
*m++ = 0;
FILLARG(m)
*m++ = FUNHEAD+i+1;
*m++ = -*t++;
*m++ = FUNHEAD;
FILLFUN(m)
NCOPY(m,r,i);
}
else if ( *t < 0 ) {
if ( t+2 < r ) return(0); /* More than one argument */
r = tstop;
if ( *t == -SNUMBER ) {
if ( t[1] == 0 ) return(1); /* Term should be zero now */
*m = AR.PolyFun;
w = m+1;
m += FUNHEAD+ARGHEAD;
v = m;
*m++ = 5+i;
*m++ = SNUMBER;
*m++ = 4;
*m++ = t[1];
*m++ = 1;
NCOPY(m,r,i);
if ( m >= AT.WorkSpace && m < AT.WorkTop )
AT.WorkPointer = m;
if ( Normalize(BHEAD v) ) Terminate(-1);
AT.WorkPointer = oldworkpointer;
m = w;
if ( *v == 4 && v[2] == 1 && (v[1]&MAXPOSITIVE) == v[1] ) {
*m++ = FUNHEAD+2;
FILLFUN(m)
*m++ = -SNUMBER;
*m++ = v[3] < 0 ? -v[1] : v[1];
}
else if ( *v == 0 ) return(1);
else {
*m++ = FUNHEAD+ARGHEAD+*v;
FILLFUN(m)
*m++ = ARGHEAD+*v;
*m++ = 0;
FILLARG(m)
m = v + *v;
}
}
else if ( *t == -SYMBOL ) {
*m++ = AR.PolyFun;
*m++ = FUNHEAD+ARGHEAD+5+i;
FILLFUN(m)
*m++ = ARGHEAD+5+i;
*m++ = 0;
FILLARG(m)
*m++ = 5+i;
*m++ = SYMBOL;
*m++ = 4;
*m++ = t[1];
*m++ = 1;
NCOPY(m,r,i);
}
else return(0); /* Not symbol-like */
}
else {
if ( t + *t < r ) return(0); /* More than one argument */
i = m[-1];
*m++ = AR.PolyFun;
w = m;
m += ARGHEAD+FUNHEAD-1;
t += ARGHEAD;
jcoef = i < 0 ? (i+1)>>1:(i-1)>>1;
v = t;
/*
Test now the scalar nature of the argument.
No indices allowed.
*/
while ( t < r ) {
WORD *vstop;
vv = t + *t;
vstop = vv - ABS(vv[-1]);
t++;
while( t < vstop ) {
if ( *t == INDEX ) return(0);
t += t[1];
}
t = vv;
}
/*
Now multiply each term by the coefficient.
*/
t = v;
while ( t < r ) {
ww = m;
v = t + *t;
ncoef = v[-1];
vv = v - ABS(ncoef);
if ( ncoef < 0 ) ncoef++;
else ncoef--;
ncoef >>= 1;
while ( t < vv ) *m++ = *t++;
if ( MulRat(BHEAD (UWORD *)vv,ncoef,(UWORD *)tstop,jcoef,
(UWORD *)m,&ncoef) ) Terminate(-1);
ncoef *= 2;
m += ABS(ncoef);
if ( ncoef < 0 ) ncoef--;
else ncoef++;
*m++ = ncoef;
*ww = WORDDIF(m,ww);
if ( AN.ncmod != 0 ) {
if ( Modulus(ww) ) Terminate(-1);
if ( *ww == 0 ) return(1);
m = ww + *ww;
}
t = v;
}
*w = (WORDDIF(m,w))+1;
w[FUNHEAD-1] = w[0] - FUNHEAD;
w[FUNHEAD] = 0;
w[1] = 0; /* omission survived for years. 23-mar-2006 JV */
w += FUNHEAD-1;
if ( ToFast(w,w) ) {
if ( *w <= -FUNCTION ) { w[-FUNHEAD+1] = FUNHEAD+1; m = w+1; }
else { w[-FUNHEAD+1] = FUNHEAD+2; m = w+2; }
}
}
t = poly + poly[1];
while ( t < tstop ) *poly++ = *t++;
/*
#] One argument :
*/
}
else if ( AR.PolyFunType == 2 ) {
DoTwo:;
/*
#[ Two arguments :
*/
WORD *num, *den, size, sign, sizenum, sizeden;
/*
First make sure that the PolyFun is last
*/
m = term + *term;
if ( poly + poly[1] < tstop ) {
for ( i = 0; i < poly[1]; i++ ) m[i] = poly[i];
t = poly; v = poly + poly[1];
while ( v < tstop ) *t++ = *v++;
poly = t;
for ( i = 0; i < m[1]; i++ ) t[i] = m[i];
t += m[1];
}
AT.PolyAct = WORDDIF(poly,term);
/*
If needed we convert the coefficient into a PolyRatFun and then
we call poly_ratfun_normalize
*/
if ( m[-1] == 3 && m[-2] == 1 && m[-3] == 1 ) return(0);
if ( AR.PolyFunExp != 1 ) {
if ( m[-1] < 0 ) { sign = -1; size = -m[-1]; } else { sign = 1; size = m[-1]; }
num = m - size; size = (size-1)/2; den = num + size;
sizenum = size; while ( num[sizenum-1] == 0 ) sizenum--;
sizeden = size; while ( den[sizeden-1] == 0 ) sizeden--;
v = m;
*v++ = AR.PolyFun;
*v++ = FUNHEAD + 2*(ARGHEAD+sizenum+sizeden+2);
/* *v++ = MUSTCLEANPRF; */
*v++ = 0;
FILLFUN3(v);
*v++ = ARGHEAD+2*sizenum+2;
*v++ = 0;
FILLARG(v);
*v++ = 2*sizenum+2;
for ( i = 0; i < sizenum; i++ ) *v++ = num[i];
*v++ = 1;
for ( i = 1; i < sizenum; i++ ) *v++ = 0;
*v++ = sign*(2*sizenum+1);
*v++ = ARGHEAD+2*sizeden+2;
*v++ = 0;
FILLARG(v);
*v++ = 2*sizeden+2;
for ( i = 0; i < sizeden; i++ ) *v++ = den[i];
*v++ = 1;
for ( i = 1; i < sizeden; i++ ) *v++ = 0;
*v++ = 2*sizeden+1;
w = num;
i = v - m;
NCOPY(w,m,i);
}
else {
w = m-ABS(m[-1]);
}
*w++ = 1; *w++ = 1; *w++ = 3; *term = w - term;
{
WORD oldtype = AR.SortType;
AR.SortType = SORTHIGHFIRST;
/*
if ( count > 0 )
poly_ratfun_normalize(BHEAD term);
else
ReadPolyRatFun(BHEAD term);
*/
poly_ratfun_normalize(BHEAD term);
/* oldworkpointer = AT.WorkPointer; */
AR.SortType = oldtype;
}
goto endofit;
/*
#] Two arguments :
*/
}
else {
DoError:;
MLOCK(ErrorMessageLock);
MesPrint("Illegal value for PolyFunType in PrepPoly");
MUNLOCK(ErrorMessageLock);
Terminate(-1);
}
r = term + *term;
AT.PolyAct = WORDDIF(poly,term);
while ( r < m ) *poly++ = *r++;
*poly++ = 1;
*poly++ = 1;
*poly++ = 3;
*term = WORDDIF(poly,term);
endofit:;
return(0);
}
/*
#] PrepPoly :
#[ PolyFunMul : WORD PolyFunMul(term)
*/
/**
* Multiplies the arguments of multiple occurrences of the polyfun.
* In this routine we do the original PolyFun with one argument only.
* The PolyRatFun (PolyFunType = 2) is done in a dedicated routine
* in the file polywrap.cc
* The new result is written over the old result.
*
* @param term It contains the input term and later the output.
* @return Normal conventions (OK = 0).
*/
int PolyFunMul(PHEAD WORD *term)
{
GETBIDENTITY
WORD *t, *fun1, *fun2, *t1, *t2, *m, *w, *ww, *tt1, *tt2, *tt4, *arg1, *arg2;
WORD *tstop, i, dirty = 0, OldPolyFunPow = AR.PolyFunPow, minp1, minp2;
WORD n1, n2, i1, i2, l1, l2, l3, l4, action = 0, noac = 0;
int retval = 0;
if ( AR.PolyFunType == 2 && AR.PolyFunExp == 1 ) {
WORD pow = 0, pow1;
t = term + 1; t1 = term + *term; t1 -= ABS(t1[-1]);
w = t;
while ( t < t1 ) {
if ( *t != AR.PolyFun ) {
SkipFun:
if ( t == w ) { t += t[1]; w = t; }
else { i = t[1]; NCOPY(w,t,i) }
continue;
}
pow1 = 0;
t2 = t + t[1]; t += FUNHEAD;
if ( *t < 0 ) {
if ( *t == -SYMBOL && t[1] == AR.PolyFunVar ) pow1++;
else if ( *t != -SNUMBER ) goto NoLegal;
t += 2;
}
else if ( t[0] == ARGHEAD+8 && t[ARGHEAD] == 8
&& t[ARGHEAD+1] == SYMBOL && t[ARGHEAD+3] == AR.PolyFunVar
&& t[ARGHEAD+5] == 1 && t[ARGHEAD+6] == 1 && t[ARGHEAD+7] == 3 ) {
pow1 += t[ARGHEAD+4];
t += *t;
}
else {
NoLegal:
MLOCK(ErrorMessageLock);
MesPrint("Illegal term with divergence in PolyRatFun");
MesCall("PolyFunMul");
MUNLOCK(ErrorMessageLock);
Terminate(-1);
}
if ( *t < 0 ) {
if ( *t == -SYMBOL && t[1] == AR.PolyFunVar ) pow1--;
else if ( *t != -SNUMBER ) goto NoLegal;
t += 2;
}
else if ( t[0] == ARGHEAD+8 && t[ARGHEAD] == 8
&& t[ARGHEAD+1] == SYMBOL && t[ARGHEAD+3] == AR.PolyFunVar
&& t[ARGHEAD+5] == 1 && t[ARGHEAD+6] == 1 && t[ARGHEAD+7] == 3 ) {
pow1 -= t[ARGHEAD+4];
t += *t;
}
else goto NoLegal;
if ( t == t2 ) pow += pow1;
else goto SkipFun;
}
m = w;
*w++ = AR.PolyFun; *w++ = 0; FILLFUN(w);
if ( pow > 1 ) {
*w++ = 8+ARGHEAD; *w++ = 0; FILLARG(w);
*w++ = 8; *w++ = SYMBOL; *w++ = 4; *w++ = AR.PolyFunVar; *w++ = pow;
*w++ = 1; *w++ = 1; *w++ = 3; *w++ = -SNUMBER; *w++ = 1;
}
else if ( pow == 1 ) {
*w++ = -SYMBOL; *w++ = AR.PolyFunVar; *w++ = -SNUMBER; *w++ = 1;
}
else if ( pow < -1 ) {
*w++ = -SNUMBER; *w++ = 1; *w++ = 8+ARGHEAD; *w++ = 0; FILLARG(w);
*w++ = 8; *w++ = SYMBOL; *w++ = 4; *w++ = AR.PolyFunVar; *w++ = -pow;
*w++ = 1; *w++ = 1; *w++ = 3;
}
else if ( pow == -1 ) {
*w++ = -SNUMBER; *w++ = 1; *w++ = -SYMBOL; *w++ = AR.PolyFunVar;
}
else {
*w++ = -SNUMBER; *w++ = 1; *w++ = -SNUMBER; *w++ = 1;
}
m[1] = w - m;
*w++ = 1; *w++ = 1; *w++ = 3;
*term = w - term;
if ( w > AT.WorkSpace && w < AT.WorkTop ) AT.WorkPointer = w;
return(0);
}
ReStart:
if ( AR.PolyFunType == 2 && ( ( AR.PolyFunExp != 2 )
|| ( AR.PolyFunExp == 2 && AN.PolyNormFlag > 1 ) ) ) {
WORD count1 = 0, count2 = 0, count3;
WORD oldtype = AR.SortType;
t = term + 1; t1 = term + *term; t1 -= ABS(t1[-1]);
while ( t < t1 ) {
if ( *t == AR.PolyFun ) {
if ( t[2] && dirty == 0 ) { /* Any dirty flag on? */
dirty = 1;
/* ReadPolyRatFun(BHEAD term); */
/* ToPolyFunGeneral(BHEAD term); */
poly_ratfun_normalize(BHEAD term);
if ( term[0] == 0 ) return(0);
count1 = 0;
action++;
goto ReStart;
}
t2 = t + t[1]; tt2 = t+FUNHEAD; count3 = 0;
while ( tt2 < t2 ) { count3++; NEXTARG(tt2); }
if ( count3 == 2 ) {
count1++;
if ( ( t[2] & MUSTCLEANPRF ) != 0 ) { /* Better civilize this guy */
action++;
w = AT.WorkPointer;
AR.SortType = SORTHIGHFIRST;
t2 = t + t[1]; tt2 = t+FUNHEAD;
while ( tt2 < t2 ) {
if ( *tt2 > 0 ) {
tt4 = tt2; tt1 = tt2 + ARGHEAD; tt2 += *tt2;
NewSort(BHEAD0);
while ( tt1 < tt2 ) {
i = *tt1; ww = w; NCOPY(ww,tt1,i);
AT.WorkPointer = ww;
Normalize(BHEAD w);
StoreTerm(BHEAD w);
}
EndSort(BHEAD w,1);
ww = w; while ( *ww ) ww += *ww;
if ( ww-w != *tt4-ARGHEAD ) { /* Little problem */
/*
Solution: brute force copy
Maybe it will never come here????
*/
WORD *r1 = TermMalloc("PolyFunMul");
WORD ii = (ww-w)-(*tt4-ARGHEAD); /* increment */
WORD *r2 = tt4+ARGHEAD, *r3, *r4 = r1;
i = r2 - term; r3 = term; NCOPY(r4,r3,i);
i = ww-w; ww = w; NCOPY(r4,ww,i);
r3 = tt2; i = term+*term-tt2; NCOPY(r4,r3,i);
*r1 = i = r4-r1; r4 = term; r3 = r1;
NCOPY(r4,r3,i);
t[1] += ii; t1 += ii; *tt4 += ii;
tt2 = tt4 + *tt4;
TermFree(r1,"PolyFunMul");
}
else {
i = ww-w; ww = w; tt1 = tt4+ARGHEAD;
NCOPY(tt1,ww,i);
AT.WorkPointer = w;
}
}
else if ( *tt2 <= -FUNCTION ) tt2++;
else tt2 += 2;
}
AR.SortType = oldtype;
}
}
}
t += t[1];
}
if ( count1 <= 1 ) { goto checkaction; }
if ( AR.PolyFunExp == 1 ) {
t = term + *term; t -= ABS(t[-1]);
*t++ = 1; *t++ = 1; *t++ = 3; *term = t - term;
}
{
AR.SortType = SORTHIGHFIRST;
/* retval = ReadPolyRatFun(BHEAD term); */
/* ToPolyFunGeneral(BHEAD term); */
retval = poly_ratfun_normalize(BHEAD term);
if ( *term == 0 ) return(retval);
AR.SortType = oldtype;
}
t = term + 1; t1 = term + *term; t1 -= ABS(t1[-1]);
while ( t < t1 ) {
if ( *t == AR.PolyFun ) {
t2 = t + t[1]; tt2 = t+FUNHEAD; count3 = 0;
while ( tt2 < t2 ) { count3++; NEXTARG(tt2); }
if ( count3 == 2 ) {
count2++;
}
}
t += t[1];
}
if ( count1 >= count2 ) {
t = term + 1;
while ( t < t1 ) {
if ( *t == AR.PolyFun ) {
t2 = t;
t = t + t[1];
t2[2] |= (DIRTYFLAG|MUSTCLEANPRF);
t2 += FUNHEAD;
while ( t2 < t ) {
if ( *t2 > 0 ) t2[1] = DIRTYFLAG;
NEXTARG(t2);
}
}
else t += t[1];
}
}
w = term + *term;
if ( w > AT.WorkSpace && w < AT.WorkTop ) AT.WorkPointer = w;
checkaction:
if ( action ) retval = action;
return(retval);
}
retry:
if ( term >= AT.WorkSpace && term+*term < AT.WorkTop )
AT.WorkPointer = term + *term;
GETSTOP(term,tstop);
t = term+1;
while ( *t != AR.PolyFun && t < tstop ) t += t[1];
while ( t < tstop && *t == AR.PolyFun ) {
if ( t[1] > FUNHEAD ) {
if ( t[FUNHEAD] < 0 ) {
if ( t[FUNHEAD] <= -FUNCTION && t[1] == FUNHEAD+1 ) break;
if ( t[FUNHEAD] > -FUNCTION && t[1] == FUNHEAD+2 ) {
if ( t[FUNHEAD] == -SNUMBER && t[FUNHEAD+1] == 0 ) {
*term = 0;
return(0);
}
break;
}
}
else if ( t[FUNHEAD] == t[1] - FUNHEAD ) break;
}
noac = 1;
t += t[1];
}
if ( *t != AR.PolyFun || t >= tstop ) goto done;
fun1 = t;
t += t[1];
while ( t < tstop && *t == AR.PolyFun ) {
if ( t[1] > FUNHEAD ) {
if ( t[FUNHEAD] < 0 ) {
if ( t[FUNHEAD] <= -FUNCTION && t[1] == FUNHEAD+1 ) break;
if ( t[FUNHEAD] > -FUNCTION && t[1] == FUNHEAD+2 ) {
if ( t[FUNHEAD] == -SNUMBER && t[FUNHEAD+1] == 0 ) {
*term = 0;
return(0);
}
break;
}
}
else if ( t[FUNHEAD] == t[1] - FUNHEAD ) break;
}
noac = 1;
t += t[1];
}
if ( *t != AR.PolyFun || t >= tstop ) goto done;
fun2 = t;
/*
We have two functions of the proper type.
Count terms (needed for the specials)
*/
t = fun1 + FUNHEAD;
if ( *t < 0 ) {
n1 = 1; arg1 = AT.WorkPointer;
ToGeneral(t,arg1,1);
AT.WorkPointer = arg1 + *arg1;
}
else {
t += ARGHEAD;
n1 = 0; t1 = fun1 + fun1[1]; arg1 = t;
while ( t < t1 ) { n1++; t += *t; }
}
t = fun2 + FUNHEAD;
if ( *t < 0 ) {
n2 = 1; arg2 = AT.WorkPointer;
ToGeneral(t,arg2,1);
AT.WorkPointer = arg2 + *arg2;
}
else {
t += ARGHEAD;
n2 = 0; t2 = fun2 + fun2[1]; arg2 = t;
while ( t < t2 ) { n2++; t += *t; }
}
/*
Now we can start the multiplications. We first multiply the terms
without coefficients, then normalize, and finally put the coefficients
in place. This is because one has often truncated series and the
high powers may get killed, while their coefficients are the most
expensive ones.
Note: We may run into fun(-SNUMBER,value)
*/
w = AT.WorkPointer;
NewSort(BHEAD0);
if ( AR.PolyFunType == 2 && AR.PolyFunExp == 2 ) {
AT.TrimPower = 1;
/*
We have to find the lowest power in both polynomials.
This will be needed to temporarily correct the AR.PolyFunPow
*/
minp1 = MAXPOWER;
for ( t1 = arg1, i1 = 0; i1 < n1; i1++, t1 += *t1 ) {
if ( *t1 == 4 ) {
if ( minp1 > 0 ) minp1 = 0;
}
else if ( ABS(t1[*t1-1]) == (*t1-1) ) {
if ( minp1 > 0 ) minp1 = 0;
}
else {
if ( t1[1] == SYMBOL && t1[2] == 4 && t1[3] == AR.PolyFunVar ) {
if ( t1[4] < minp1 ) minp1 = t1[4];
}
else {
MesPrint("Illegal term in expanded polyratfun.");
goto PolyCall;
}
}
}
minp2 = MAXPOWER;
for ( t2 = arg2, i2 = 0; i2 < n2; i2++, t2 += *t2 ) {
if ( *t2 == 4 ) {
if ( minp2 > 0 ) minp2 = 0;
}
else if ( ABS(t2[*t2-1]) == (*t2-1) ) {
if ( minp2 > 0 ) minp2 = 0;
}
else {
if ( t2[1] == SYMBOL && t2[2] == 4 && t2[3] == AR.PolyFunVar ) {
if ( t2[4] < minp2 ) minp2 = t2[4];
}
else {
MesPrint("Illegal term in expanded polyratfun.");
goto PolyCall;
}
}
}
AR.PolyFunPow += minp1+minp2;
}
for ( t1 = arg1, i1 = 0; i1 < n1; i1++, t1 += *t1 ) {
for ( t2 = arg2, i2 = 0; i2 < n2; i2++, t2 += *t2 ) {
m = w;
m++;
GETSTOP(t1,tt1);
t = t1 + 1;
while ( t < tt1 ) *m++ = *t++;
GETSTOP(t2,tt2);
t = t2+1;
while ( t < tt2 ) *m++ = *t++;
*m++ = 1; *m++ = 1; *m++ = 3; *w = WORDDIF(m,w);
AT.WorkPointer = m;
if ( Normalize(BHEAD w) ) { LowerSortLevel(); goto PolyCall; }
if ( *w ) {
m = w + *w;
if ( m[-1] != 3 || m[-2] != 1 || m[-3] != 1 ) {
l3 = REDLENG(m[-1]);
m -= ABS(m[-1]);
t = t1 + *t1 - 1;
l1 = REDLENG(*t);
if ( MulRat(BHEAD (UWORD *)m,l3,(UWORD *)tt1,l1,(UWORD *)m,&l4) ) {
LowerSortLevel(); goto PolyCall; }
if ( AN.ncmod != 0 && TakeModulus((UWORD *)m,&l4,AC.cmod,AN.ncmod,UNPACK|AC.modmode) ) {
LowerSortLevel(); goto PolyCall; }
if ( l4 == 0 ) continue;
t = t2 + *t2 - 1;
l2 = REDLENG(*t);
if ( MulRat(BHEAD (UWORD *)m,l4,(UWORD *)tt2,l2,(UWORD *)m,&l3) ) {
LowerSortLevel(); goto PolyCall; }
if ( AN.ncmod != 0 && TakeModulus((UWORD *)m,&l3,AC.cmod,AN.ncmod,UNPACK|AC.modmode) ) {
LowerSortLevel(); goto PolyCall; }
}
else {
m -= 3;
t = t1 + *t1 - 1;
l1 = REDLENG(*t);
t = t2 + *t2 - 1;
l2 = REDLENG(*t);
if ( MulRat(BHEAD (UWORD *)tt1,l1,(UWORD *)tt2,l2,(UWORD *)m,&l3) ) {
LowerSortLevel(); goto PolyCall; }
if ( AN.ncmod != 0 && TakeModulus((UWORD *)m,&l3,AC.cmod,AN.ncmod,UNPACK|AC.modmode) ) {
LowerSortLevel(); goto PolyCall; }
}
if ( l3 == 0 ) continue;
l3 = INCLENG(l3);
m += ABS(l3);
m[-1] = l3;
*w = WORDDIF(m,w);
AT.WorkPointer = m;
if ( StoreTerm(BHEAD w) ) { LowerSortLevel(); goto PolyCall; }
}
}
}
if ( EndSort(BHEAD w,0) < 0 ) goto PolyCall;
AR.PolyFunPow = OldPolyFunPow;
AT.TrimPower = 0;
if ( *w == 0 ) {
*term = 0;
return(0);
}
t = w;
while ( *t ) t += *t;
AT.WorkPointer = t;
n1 = WORDDIF(t,w);
t1 = term;
while ( t1 < fun1 ) *t++ = *t1++;
t2 = t;
*t++ = AR.PolyFun;
*t++ = FUNHEAD+ARGHEAD+n1;
*t++ = 0;
FILLFUN3(t)
*t++ = ARGHEAD+n1;
*t++ = 0;
FILLARG(t)
NCOPY(t,w,n1);
if ( ToFast(t2+FUNHEAD,t2+FUNHEAD) ) {
if ( t2[FUNHEAD] > -FUNCTION ) t2[1] = FUNHEAD+2;
else t2[FUNHEAD] = FUNHEAD+1;
t = t2 + t2[1];
}
t1 = fun1 + fun1[1];
while ( t1 < fun2 ) *t++ = *t1++;
t1 = fun2 + fun2[1];
t2 = term + *term;
while ( t1 < t2 ) *t++ = *t1++;
*AT.WorkPointer = n1 = WORDDIF(t,AT.WorkPointer);
if ( n1*((LONG)sizeof(WORD)) > AM.MaxTer ) {
MLOCK(ErrorMessageLock);
MesPrint("Term too complex (%d words). MaxTermSize (%l words) is too small.", n1, AM.MaxTer/(LONG)sizeof(WORD) );
goto PolyCall2;
}
m = term; t = AT.WorkPointer;
NCOPY(m,t,n1);
action++;
goto retry;
done:
AT.WorkPointer = term + *term;
if ( action && noac ) {
if ( Normalize(BHEAD term) ) goto PolyCall;
AT.WorkPointer = term + *term;
}
return(0);
PolyCall:;
MLOCK(ErrorMessageLock);
PolyCall2:;
AR.PolyFunPow = OldPolyFunPow;
MesCall("PolyFunMul");
MUNLOCK(ErrorMessageLock);
SETERROR(-1)
}
/*
#] PolyFunMul :
#] Processor :
*/
|