isl_convex_hull.c 84.2 KB
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
/*
 * Copyright 2008-2009 Katholieke Universiteit Leuven
 * Copyright 2014      INRIA Rocquencourt
 *
 * Use of this software is governed by the MIT license
 *
 * Written by Sven Verdoolaege, K.U.Leuven, Departement
 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
 * B.P. 105 - 78153 Le Chesnay, France
 */

#include <isl_ctx_private.h>
#include <isl_map_private.h>
#include <isl_lp_private.h>
#include <isl/map.h>
#include <isl_mat_private.h>
#include <isl_vec_private.h>
#include <isl/set.h>
#include <isl_seq.h>
#include <isl_options_private.h>
#include "isl_equalities.h"
#include "isl_tab.h"
#include <isl_sort.h>

#include <bset_to_bmap.c>
#include <bset_from_bmap.c>
#include <set_to_map.c>

static __isl_give isl_basic_set *uset_convex_hull_wrap_bounded(
	__isl_take isl_set *set);

/* Remove redundant
 * constraints.  If the minimal value along the normal of a constraint
 * is the same if the constraint is removed, then the constraint is redundant.
 *
 * Since some constraints may be mutually redundant, sort the constraints
 * first such that constraints that involve existentially quantified
 * variables are considered for removal before those that do not.
 * The sorting is also needed for the use in map_simple_hull.
 *
 * Note that isl_tab_detect_implicit_equalities may also end up
 * marking some constraints as redundant.  Make sure the constraints
 * are preserved and undo those marking such that isl_tab_detect_redundant
 * can consider the constraints in the sorted order.
 *
 * Alternatively, we could have intersected the basic map with the
 * corresponding equality and then checked if the dimension was that
 * of a facet.
 */
__isl_give isl_basic_map *isl_basic_map_remove_redundancies(
	__isl_take isl_basic_map *bmap)
{
	struct isl_tab *tab;

	if (!bmap)
		return NULL;

	bmap = isl_basic_map_gauss(bmap, NULL);
	if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
		return bmap;
	if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NO_REDUNDANT))
		return bmap;
	if (bmap->n_ineq <= 1)
		return bmap;

	bmap = isl_basic_map_sort_constraints(bmap);
	tab = isl_tab_from_basic_map(bmap, 0);
	if (!tab)
		goto error;
	tab->preserve = 1;
	if (isl_tab_detect_implicit_equalities(tab) < 0)
		goto error;
	if (isl_tab_restore_redundant(tab) < 0)
		goto error;
	tab->preserve = 0;
	if (isl_tab_detect_redundant(tab) < 0)
		goto error;
	bmap = isl_basic_map_update_from_tab(bmap, tab);
	isl_tab_free(tab);
	if (!bmap)
		return NULL;
	ISL_F_SET(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
	ISL_F_SET(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
	return bmap;
error:
	isl_tab_free(tab);
	isl_basic_map_free(bmap);
	return NULL;
}

__isl_give isl_basic_set *isl_basic_set_remove_redundancies(
	__isl_take isl_basic_set *bset)
{
	return bset_from_bmap(
		isl_basic_map_remove_redundancies(bset_to_bmap(bset)));
}

/* Remove redundant constraints in each of the basic maps.
 */
__isl_give isl_map *isl_map_remove_redundancies(__isl_take isl_map *map)
{
	return isl_map_inline_foreach_basic_map(map,
					    &isl_basic_map_remove_redundancies);
}

__isl_give isl_set *isl_set_remove_redundancies(__isl_take isl_set *set)
{
	return isl_map_remove_redundancies(set);
}

/* Check if the set set is bound in the direction of the affine
 * constraint c and if so, set the constant term such that the
 * resulting constraint is a bounding constraint for the set.
 */
static int uset_is_bound(__isl_keep isl_set *set, isl_int *c, unsigned len)
{
	int first;
	int j;
	isl_int opt;
	isl_int opt_denom;

	isl_int_init(opt);
	isl_int_init(opt_denom);
	first = 1;
	for (j = 0; j < set->n; ++j) {
		enum isl_lp_result res;

		if (ISL_F_ISSET(set->p[j], ISL_BASIC_SET_EMPTY))
			continue;

		res = isl_basic_set_solve_lp(set->p[j],
				0, c, set->ctx->one, &opt, &opt_denom, NULL);
		if (res == isl_lp_unbounded)
			break;
		if (res == isl_lp_error)
			goto error;
		if (res == isl_lp_empty) {
			set->p[j] = isl_basic_set_set_to_empty(set->p[j]);
			if (!set->p[j])
				goto error;
			continue;
		}
		if (first || isl_int_is_neg(opt)) {
			if (!isl_int_is_one(opt_denom))
				isl_seq_scale(c, c, opt_denom, len);
			isl_int_sub(c[0], c[0], opt);
		}
		first = 0;
	}
	isl_int_clear(opt);
	isl_int_clear(opt_denom);
	return j >= set->n;
error:
	isl_int_clear(opt);
	isl_int_clear(opt_denom);
	return -1;
}

static struct isl_basic_set *isl_basic_set_add_equality(
	struct isl_basic_set *bset, isl_int *c)
{
	int i;
	unsigned dim;

	if (!bset)
		return NULL;

	if (ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY))
		return bset;

	isl_assert(bset->ctx, isl_basic_set_n_param(bset) == 0, goto error);
	isl_assert(bset->ctx, bset->n_div == 0, goto error);
	dim = isl_basic_set_n_dim(bset);
	bset = isl_basic_set_cow(bset);
	bset = isl_basic_set_extend(bset, 0, dim, 0, 1, 0);
	i = isl_basic_set_alloc_equality(bset);
	if (i < 0)
		goto error;
	isl_seq_cpy(bset->eq[i], c, 1 + dim);
	return bset;
error:
	isl_basic_set_free(bset);
	return NULL;
}

static __isl_give isl_set *isl_set_add_basic_set_equality(
	__isl_take isl_set *set, isl_int *c)
{
	int i;

	set = isl_set_cow(set);
	if (!set)
		return NULL;
	for (i = 0; i < set->n; ++i) {
		set->p[i] = isl_basic_set_add_equality(set->p[i], c);
		if (!set->p[i])
			goto error;
	}
	return set;
error:
	isl_set_free(set);
	return NULL;
}

/* Given a union of basic sets, construct the constraints for wrapping
 * a facet around one of its ridges.
 * In particular, if each of n the d-dimensional basic sets i in "set"
 * contains the origin, satisfies the constraints x_1 >= 0 and x_2 >= 0
 * and is defined by the constraints
 *				    [ 1 ]
 *				A_i [ x ]  >= 0
 *
 * then the resulting set is of dimension n*(1+d) and has as constraints
 *
 *				    [ a_i ]
 *				A_i [ x_i ] >= 0
 *
 *				      a_i   >= 0
 *
 *			\sum_i x_{i,1} = 1
 */
static __isl_give isl_basic_set *wrap_constraints(__isl_keep isl_set *set)
{
	struct isl_basic_set *lp;
	unsigned n_eq;
	unsigned n_ineq;
	int i, j, k;
	unsigned dim, lp_dim;

	if (!set)
		return NULL;

	dim = 1 + isl_set_n_dim(set);
	n_eq = 1;
	n_ineq = set->n;
	for (i = 0; i < set->n; ++i) {
		n_eq += set->p[i]->n_eq;
		n_ineq += set->p[i]->n_ineq;
	}
	lp = isl_basic_set_alloc(set->ctx, 0, dim * set->n, 0, n_eq, n_ineq);
	lp = isl_basic_set_set_rational(lp);
	if (!lp)
		return NULL;
	lp_dim = isl_basic_set_n_dim(lp);
	k = isl_basic_set_alloc_equality(lp);
	isl_int_set_si(lp->eq[k][0], -1);
	for (i = 0; i < set->n; ++i) {
		isl_int_set_si(lp->eq[k][1+dim*i], 0);
		isl_int_set_si(lp->eq[k][1+dim*i+1], 1);
		isl_seq_clr(lp->eq[k]+1+dim*i+2, dim-2);
	}
	for (i = 0; i < set->n; ++i) {
		k = isl_basic_set_alloc_inequality(lp);
		isl_seq_clr(lp->ineq[k], 1+lp_dim);
		isl_int_set_si(lp->ineq[k][1+dim*i], 1);

		for (j = 0; j < set->p[i]->n_eq; ++j) {
			k = isl_basic_set_alloc_equality(lp);
			isl_seq_clr(lp->eq[k], 1+dim*i);
			isl_seq_cpy(lp->eq[k]+1+dim*i, set->p[i]->eq[j], dim);
			isl_seq_clr(lp->eq[k]+1+dim*(i+1), dim*(set->n-i-1));
		}

		for (j = 0; j < set->p[i]->n_ineq; ++j) {
			k = isl_basic_set_alloc_inequality(lp);
			isl_seq_clr(lp->ineq[k], 1+dim*i);
			isl_seq_cpy(lp->ineq[k]+1+dim*i, set->p[i]->ineq[j], dim);
			isl_seq_clr(lp->ineq[k]+1+dim*(i+1), dim*(set->n-i-1));
		}
	}
	return lp;
}

/* Given a facet "facet" of the convex hull of "set" and a facet "ridge"
 * of that facet, compute the other facet of the convex hull that contains
 * the ridge.
 *
 * We first transform the set such that the facet constraint becomes
 *
 *			x_1 >= 0
 *
 * I.e., the facet lies in
 *
 *			x_1 = 0
 *
 * and on that facet, the constraint that defines the ridge is
 *
 *			x_2 >= 0
 *
 * (This transformation is not strictly needed, all that is needed is
 * that the ridge contains the origin.)
 *
 * Since the ridge contains the origin, the cone of the convex hull
 * will be of the form
 *
 *			x_1 >= 0
 *			x_2 >= a x_1
 *
 * with this second constraint defining the new facet.
 * The constant a is obtained by settting x_1 in the cone of the
 * convex hull to 1 and minimizing x_2.
 * Now, each element in the cone of the convex hull is the sum
 * of elements in the cones of the basic sets.
 * If a_i is the dilation factor of basic set i, then the problem
 * we need to solve is
 *
 *			min \sum_i x_{i,2}
 *			st
 *				\sum_i x_{i,1} = 1
 *				    a_i   >= 0
 *				  [ a_i ]
 *				A [ x_i ] >= 0
 *
 * with
 *				    [  1  ]
 *				A_i [ x_i ] >= 0
 *
 * the constraints of each (transformed) basic set.
 * If a = n/d, then the constraint defining the new facet (in the transformed
 * space) is
 *
 *			-n x_1 + d x_2 >= 0
 *
 * In the original space, we need to take the same combination of the
 * corresponding constraints "facet" and "ridge".
 *
 * If a = -infty = "-1/0", then we just return the original facet constraint.
 * This means that the facet is unbounded, but has a bounded intersection
 * with the union of sets.
 */
isl_int *isl_set_wrap_facet(__isl_keep isl_set *set,
	isl_int *facet, isl_int *ridge)
{
	int i;
	isl_ctx *ctx;
	struct isl_mat *T = NULL;
	struct isl_basic_set *lp = NULL;
	struct isl_vec *obj;
	enum isl_lp_result res;
	isl_int num, den;
	unsigned dim;

	if (!set)
		return NULL;
	ctx = set->ctx;
	set = isl_set_copy(set);
	set = isl_set_set_rational(set);

	dim = 1 + isl_set_n_dim(set);
	T = isl_mat_alloc(ctx, 3, dim);
	if (!T)
		goto error;
	isl_int_set_si(T->row[0][0], 1);
	isl_seq_clr(T->row[0]+1, dim - 1);
	isl_seq_cpy(T->row[1], facet, dim);
	isl_seq_cpy(T->row[2], ridge, dim);
	T = isl_mat_right_inverse(T);
	set = isl_set_preimage(set, T);
	T = NULL;
	if (!set)
		goto error;
	lp = wrap_constraints(set);
	obj = isl_vec_alloc(ctx, 1 + dim*set->n);
	if (!obj)
		goto error;
	isl_int_set_si(obj->block.data[0], 0);
	for (i = 0; i < set->n; ++i) {
		isl_seq_clr(obj->block.data + 1 + dim*i, 2);
		isl_int_set_si(obj->block.data[1 + dim*i+2], 1);
		isl_seq_clr(obj->block.data + 1 + dim*i+3, dim-3);
	}
	isl_int_init(num);
	isl_int_init(den);
	res = isl_basic_set_solve_lp(lp, 0,
			    obj->block.data, ctx->one, &num, &den, NULL);
	if (res == isl_lp_ok) {
		isl_int_neg(num, num);
		isl_seq_combine(facet, num, facet, den, ridge, dim);
		isl_seq_normalize(ctx, facet, dim);
	}
	isl_int_clear(num);
	isl_int_clear(den);
	isl_vec_free(obj);
	isl_basic_set_free(lp);
	isl_set_free(set);
	if (res == isl_lp_error)
		return NULL;
	isl_assert(ctx, res == isl_lp_ok || res == isl_lp_unbounded, 
		   return NULL);
	return facet;
error:
	isl_basic_set_free(lp);
	isl_mat_free(T);
	isl_set_free(set);
	return NULL;
}

/* Compute the constraint of a facet of "set".
 *
 * We first compute the intersection with a bounding constraint
 * that is orthogonal to one of the coordinate axes.
 * If the affine hull of this intersection has only one equality,
 * we have found a facet.
 * Otherwise, we wrap the current bounding constraint around
 * one of the equalities of the face (one that is not equal to
 * the current bounding constraint).
 * This process continues until we have found a facet.
 * The dimension of the intersection increases by at least
 * one on each iteration, so termination is guaranteed.
 */
static __isl_give isl_mat *initial_facet_constraint(__isl_keep isl_set *set)
{
	struct isl_set *slice = NULL;
	struct isl_basic_set *face = NULL;
	int i;
	unsigned dim = isl_set_n_dim(set);
	int is_bound;
	isl_mat *bounds = NULL;

	isl_assert(set->ctx, set->n > 0, goto error);
	bounds = isl_mat_alloc(set->ctx, 1, 1 + dim);
	if (!bounds)
		return NULL;

	isl_seq_clr(bounds->row[0], dim);
	isl_int_set_si(bounds->row[0][1 + dim - 1], 1);
	is_bound = uset_is_bound(set, bounds->row[0], 1 + dim);
	if (is_bound < 0)
		goto error;
	isl_assert(set->ctx, is_bound, goto error);
	isl_seq_normalize(set->ctx, bounds->row[0], 1 + dim);
	bounds->n_row = 1;

	for (;;) {
		slice = isl_set_copy(set);
		slice = isl_set_add_basic_set_equality(slice, bounds->row[0]);
		face = isl_set_affine_hull(slice);
		if (!face)
			goto error;
		if (face->n_eq == 1) {
			isl_basic_set_free(face);
			break;
		}
		for (i = 0; i < face->n_eq; ++i)
			if (!isl_seq_eq(bounds->row[0], face->eq[i], 1 + dim) &&
			    !isl_seq_is_neg(bounds->row[0],
						face->eq[i], 1 + dim))
				break;
		isl_assert(set->ctx, i < face->n_eq, goto error);
		if (!isl_set_wrap_facet(set, bounds->row[0], face->eq[i]))
			goto error;
		isl_seq_normalize(set->ctx, bounds->row[0], bounds->n_col);
		isl_basic_set_free(face);
	}

	return bounds;
error:
	isl_basic_set_free(face);
	isl_mat_free(bounds);
	return NULL;
}

/* Given the bounding constraint "c" of a facet of the convex hull of "set",
 * compute a hyperplane description of the facet, i.e., compute the facets
 * of the facet.
 *
 * We compute an affine transformation that transforms the constraint
 *
 *			  [ 1 ]
 *			c [ x ] = 0
 *
 * to the constraint
 *
 *			   z_1  = 0
 *
 * by computing the right inverse U of a matrix that starts with the rows
 *
 *			[ 1 0 ]
 *			[  c  ]
 *
 * Then
 *			[ 1 ]     [ 1 ]
 *			[ x ] = U [ z ]
 * and
 *			[ 1 ]     [ 1 ]
 *			[ z ] = Q [ x ]
 *
 * with Q = U^{-1}
 * Since z_1 is zero, we can drop this variable as well as the corresponding
 * column of U to obtain
 *
 *			[ 1 ]      [ 1  ]
 *			[ x ] = U' [ z' ]
 * and
 *			[ 1  ]      [ 1 ]
 *			[ z' ] = Q' [ x ]
 *
 * with Q' equal to Q, but without the corresponding row.
 * After computing the facets of the facet in the z' space,
 * we convert them back to the x space through Q.
 */
static __isl_give isl_basic_set *compute_facet(__isl_keep isl_set *set,
	isl_int *c)
{
	struct isl_mat *m, *U, *Q;
	struct isl_basic_set *facet = NULL;
	struct isl_ctx *ctx;
	unsigned dim;

	ctx = set->ctx;
	set = isl_set_copy(set);
	dim = isl_set_n_dim(set);
	m = isl_mat_alloc(set->ctx, 2, 1 + dim);
	if (!m)
		goto error;
	isl_int_set_si(m->row[0][0], 1);
	isl_seq_clr(m->row[0]+1, dim);
	isl_seq_cpy(m->row[1], c, 1+dim);
	U = isl_mat_right_inverse(m);
	Q = isl_mat_right_inverse(isl_mat_copy(U));
	U = isl_mat_drop_cols(U, 1, 1);
	Q = isl_mat_drop_rows(Q, 1, 1);
	set = isl_set_preimage(set, U);
	facet = uset_convex_hull_wrap_bounded(set);
	facet = isl_basic_set_preimage(facet, Q);
	if (facet && facet->n_eq != 0)
		isl_die(ctx, isl_error_internal, "unexpected equality",
			return isl_basic_set_free(facet));
	return facet;
error:
	isl_basic_set_free(facet);
	isl_set_free(set);
	return NULL;
}

/* Given an initial facet constraint, compute the remaining facets.
 * We do this by running through all facets found so far and computing
 * the adjacent facets through wrapping, adding those facets that we
 * hadn't already found before.
 *
 * For each facet we have found so far, we first compute its facets
 * in the resulting convex hull.  That is, we compute the ridges
 * of the resulting convex hull contained in the facet.
 * We also compute the corresponding facet in the current approximation
 * of the convex hull.  There is no need to wrap around the ridges
 * in this facet since that would result in a facet that is already
 * present in the current approximation.
 *
 * This function can still be significantly optimized by checking which of
 * the facets of the basic sets are also facets of the convex hull and
 * using all the facets so far to help in constructing the facets of the
 * facets
 * and/or
 * using the technique in section "3.1 Ridge Generation" of
 * "Extended Convex Hull" by Fukuda et al.
 */
static __isl_give isl_basic_set *extend(__isl_take isl_basic_set *hull,
	__isl_keep isl_set *set)
{
	int i, j, f;
	int k;
	struct isl_basic_set *facet = NULL;
	struct isl_basic_set *hull_facet = NULL;
	unsigned dim;

	if (!hull)
		return NULL;

	isl_assert(set->ctx, set->n > 0, goto error);

	dim = isl_set_n_dim(set);

	for (i = 0; i < hull->n_ineq; ++i) {
		facet = compute_facet(set, hull->ineq[i]);
		facet = isl_basic_set_add_equality(facet, hull->ineq[i]);
		facet = isl_basic_set_gauss(facet, NULL);
		facet = isl_basic_set_normalize_constraints(facet);
		hull_facet = isl_basic_set_copy(hull);
		hull_facet = isl_basic_set_add_equality(hull_facet, hull->ineq[i]);
		hull_facet = isl_basic_set_gauss(hull_facet, NULL);
		hull_facet = isl_basic_set_normalize_constraints(hull_facet);
		if (!facet || !hull_facet)
			goto error;
		hull = isl_basic_set_cow(hull);
		hull = isl_basic_set_extend_space(hull,
			isl_space_copy(hull->dim), 0, 0, facet->n_ineq);
		if (!hull)
			goto error;
		for (j = 0; j < facet->n_ineq; ++j) {
			for (f = 0; f < hull_facet->n_ineq; ++f)
				if (isl_seq_eq(facet->ineq[j],
						hull_facet->ineq[f], 1 + dim))
					break;
			if (f < hull_facet->n_ineq)
				continue;
			k = isl_basic_set_alloc_inequality(hull);
			if (k < 0)
				goto error;
			isl_seq_cpy(hull->ineq[k], hull->ineq[i], 1+dim);
			if (!isl_set_wrap_facet(set, hull->ineq[k], facet->ineq[j]))
				goto error;
		}
		isl_basic_set_free(hull_facet);
		isl_basic_set_free(facet);
	}
	hull = isl_basic_set_simplify(hull);
	hull = isl_basic_set_finalize(hull);
	return hull;
error:
	isl_basic_set_free(hull_facet);
	isl_basic_set_free(facet);
	isl_basic_set_free(hull);
	return NULL;
}

/* Special case for computing the convex hull of a one dimensional set.
 * We simply collect the lower and upper bounds of each basic set
 * and the biggest of those.
 */
static __isl_give isl_basic_set *convex_hull_1d(__isl_take isl_set *set)
{
	struct isl_mat *c = NULL;
	isl_int *lower = NULL;
	isl_int *upper = NULL;
	int i, j, k;
	isl_int a, b;
	struct isl_basic_set *hull;

	for (i = 0; i < set->n; ++i) {
		set->p[i] = isl_basic_set_simplify(set->p[i]);
		if (!set->p[i])
			goto error;
	}
	set = isl_set_remove_empty_parts(set);
	if (!set)
		goto error;
	isl_assert(set->ctx, set->n > 0, goto error);
	c = isl_mat_alloc(set->ctx, 2, 2);
	if (!c)
		goto error;

	if (set->p[0]->n_eq > 0) {
		isl_assert(set->ctx, set->p[0]->n_eq == 1, goto error);
		lower = c->row[0];
		upper = c->row[1];
		if (isl_int_is_pos(set->p[0]->eq[0][1])) {
			isl_seq_cpy(lower, set->p[0]->eq[0], 2);
			isl_seq_neg(upper, set->p[0]->eq[0], 2);
		} else {
			isl_seq_neg(lower, set->p[0]->eq[0], 2);
			isl_seq_cpy(upper, set->p[0]->eq[0], 2);
		}
	} else {
		for (j = 0; j < set->p[0]->n_ineq; ++j) {
			if (isl_int_is_pos(set->p[0]->ineq[j][1])) {
				lower = c->row[0];
				isl_seq_cpy(lower, set->p[0]->ineq[j], 2);
			} else {
				upper = c->row[1];
				isl_seq_cpy(upper, set->p[0]->ineq[j], 2);
			}
		}
	}

	isl_int_init(a);
	isl_int_init(b);
	for (i = 0; i < set->n; ++i) {
		struct isl_basic_set *bset = set->p[i];
		int has_lower = 0;
		int has_upper = 0;

		for (j = 0; j < bset->n_eq; ++j) {
			has_lower = 1;
			has_upper = 1;
			if (lower) {
				isl_int_mul(a, lower[0], bset->eq[j][1]);
				isl_int_mul(b, lower[1], bset->eq[j][0]);
				if (isl_int_lt(a, b) && isl_int_is_pos(bset->eq[j][1]))
					isl_seq_cpy(lower, bset->eq[j], 2);
				if (isl_int_gt(a, b) && isl_int_is_neg(bset->eq[j][1]))
					isl_seq_neg(lower, bset->eq[j], 2);
			}
			if (upper) {
				isl_int_mul(a, upper[0], bset->eq[j][1]);
				isl_int_mul(b, upper[1], bset->eq[j][0]);
				if (isl_int_lt(a, b) && isl_int_is_pos(bset->eq[j][1]))
					isl_seq_neg(upper, bset->eq[j], 2);
				if (isl_int_gt(a, b) && isl_int_is_neg(bset->eq[j][1]))
					isl_seq_cpy(upper, bset->eq[j], 2);
			}
		}
		for (j = 0; j < bset->n_ineq; ++j) {
			if (isl_int_is_pos(bset->ineq[j][1]))
				has_lower = 1;
			if (isl_int_is_neg(bset->ineq[j][1]))
				has_upper = 1;
			if (lower && isl_int_is_pos(bset->ineq[j][1])) {
				isl_int_mul(a, lower[0], bset->ineq[j][1]);
				isl_int_mul(b, lower[1], bset->ineq[j][0]);
				if (isl_int_lt(a, b))
					isl_seq_cpy(lower, bset->ineq[j], 2);
			}
			if (upper && isl_int_is_neg(bset->ineq[j][1])) {
				isl_int_mul(a, upper[0], bset->ineq[j][1]);
				isl_int_mul(b, upper[1], bset->ineq[j][0]);
				if (isl_int_gt(a, b))
					isl_seq_cpy(upper, bset->ineq[j], 2);
			}
		}
		if (!has_lower)
			lower = NULL;
		if (!has_upper)
			upper = NULL;
	}
	isl_int_clear(a);
	isl_int_clear(b);

	hull = isl_basic_set_alloc(set->ctx, 0, 1, 0, 0, 2);
	hull = isl_basic_set_set_rational(hull);
	if (!hull)
		goto error;
	if (lower) {
		k = isl_basic_set_alloc_inequality(hull);
		isl_seq_cpy(hull->ineq[k], lower, 2);
	}
	if (upper) {
		k = isl_basic_set_alloc_inequality(hull);
		isl_seq_cpy(hull->ineq[k], upper, 2);
	}
	hull = isl_basic_set_finalize(hull);
	isl_set_free(set);
	isl_mat_free(c);
	return hull;
error:
	isl_set_free(set);
	isl_mat_free(c);
	return NULL;
}

static __isl_give isl_basic_set *convex_hull_0d(__isl_take isl_set *set)
{
	struct isl_basic_set *convex_hull;

	if (!set)
		return NULL;

	if (isl_set_is_empty(set))
		convex_hull = isl_basic_set_empty(isl_space_copy(set->dim));
	else
		convex_hull = isl_basic_set_universe(isl_space_copy(set->dim));
	isl_set_free(set);
	return convex_hull;
}

/* Compute the convex hull of a pair of basic sets without any parameters or
 * integer divisions using Fourier-Motzkin elimination.
 * The convex hull is the set of all points that can be written as
 * the sum of points from both basic sets (in homogeneous coordinates).
 * We set up the constraints in a space with dimensions for each of
 * the three sets and then project out the dimensions corresponding
 * to the two original basic sets, retaining only those corresponding
 * to the convex hull.
 */
static __isl_give isl_basic_set *convex_hull_pair_elim(
	__isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
{
	int i, j, k;
	struct isl_basic_set *bset[2];
	struct isl_basic_set *hull = NULL;
	unsigned dim;

	if (!bset1 || !bset2)
		goto error;

	dim = isl_basic_set_n_dim(bset1);
	hull = isl_basic_set_alloc(bset1->ctx, 0, 2 + 3 * dim, 0,
				1 + dim + bset1->n_eq + bset2->n_eq,
				2 + bset1->n_ineq + bset2->n_ineq);
	bset[0] = bset1;
	bset[1] = bset2;
	for (i = 0; i < 2; ++i) {
		for (j = 0; j < bset[i]->n_eq; ++j) {
			k = isl_basic_set_alloc_equality(hull);
			if (k < 0)
				goto error;
			isl_seq_clr(hull->eq[k], (i+1) * (1+dim));
			isl_seq_clr(hull->eq[k]+(i+2)*(1+dim), (1-i)*(1+dim));
			isl_seq_cpy(hull->eq[k]+(i+1)*(1+dim), bset[i]->eq[j],
					1+dim);
		}
		for (j = 0; j < bset[i]->n_ineq; ++j) {
			k = isl_basic_set_alloc_inequality(hull);
			if (k < 0)
				goto error;
			isl_seq_clr(hull->ineq[k], (i+1) * (1+dim));
			isl_seq_clr(hull->ineq[k]+(i+2)*(1+dim), (1-i)*(1+dim));
			isl_seq_cpy(hull->ineq[k]+(i+1)*(1+dim),
					bset[i]->ineq[j], 1+dim);
		}
		k = isl_basic_set_alloc_inequality(hull);
		if (k < 0)
			goto error;
		isl_seq_clr(hull->ineq[k], 1+2+3*dim);
		isl_int_set_si(hull->ineq[k][(i+1)*(1+dim)], 1);
	}
	for (j = 0; j < 1+dim; ++j) {
		k = isl_basic_set_alloc_equality(hull);
		if (k < 0)
			goto error;
		isl_seq_clr(hull->eq[k], 1+2+3*dim);
		isl_int_set_si(hull->eq[k][j], -1);
		isl_int_set_si(hull->eq[k][1+dim+j], 1);
		isl_int_set_si(hull->eq[k][2*(1+dim)+j], 1);
	}
	hull = isl_basic_set_set_rational(hull);
	hull = isl_basic_set_remove_dims(hull, isl_dim_set, dim, 2*(1+dim));
	hull = isl_basic_set_remove_redundancies(hull);
	isl_basic_set_free(bset1);
	isl_basic_set_free(bset2);
	return hull;
error:
	isl_basic_set_free(bset1);
	isl_basic_set_free(bset2);
	isl_basic_set_free(hull);
	return NULL;
}

/* Is the set bounded for each value of the parameters?
 */
isl_bool isl_basic_set_is_bounded(__isl_keep isl_basic_set *bset)
{
	struct isl_tab *tab;
	isl_bool bounded;

	if (!bset)
		return isl_bool_error;
	if (isl_basic_set_plain_is_empty(bset))
		return isl_bool_true;

	tab = isl_tab_from_recession_cone(bset, 1);
	bounded = isl_tab_cone_is_bounded(tab);
	isl_tab_free(tab);
	return bounded;
}

/* Is the image bounded for each value of the parameters and
 * the domain variables?
 */
isl_bool isl_basic_map_image_is_bounded(__isl_keep isl_basic_map *bmap)
{
	unsigned nparam = isl_basic_map_dim(bmap, isl_dim_param);
	unsigned n_in = isl_basic_map_dim(bmap, isl_dim_in);
	isl_bool bounded;

	bmap = isl_basic_map_copy(bmap);
	bmap = isl_basic_map_cow(bmap);
	bmap = isl_basic_map_move_dims(bmap, isl_dim_param, nparam,
					isl_dim_in, 0, n_in);
	bounded = isl_basic_set_is_bounded(bset_from_bmap(bmap));
	isl_basic_map_free(bmap);

	return bounded;
}

/* Is the set bounded for each value of the parameters?
 */
isl_bool isl_set_is_bounded(__isl_keep isl_set *set)
{
	int i;

	if (!set)
		return isl_bool_error;

	for (i = 0; i < set->n; ++i) {
		isl_bool bounded = isl_basic_set_is_bounded(set->p[i]);
		if (!bounded || bounded < 0)
			return bounded;
	}
	return isl_bool_true;
}

/* Compute the lineality space of the convex hull of bset1 and bset2.
 *
 * We first compute the intersection of the recession cone of bset1
 * with the negative of the recession cone of bset2 and then compute
 * the linear hull of the resulting cone.
 */
static __isl_give isl_basic_set *induced_lineality_space(
	__isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
{
	int i, k;
	struct isl_basic_set *lin = NULL;
	unsigned dim;

	if (!bset1 || !bset2)
		goto error;

	dim = isl_basic_set_total_dim(bset1);
	lin = isl_basic_set_alloc_space(isl_basic_set_get_space(bset1), 0,
					bset1->n_eq + bset2->n_eq,
					bset1->n_ineq + bset2->n_ineq);
	lin = isl_basic_set_set_rational(lin);
	if (!lin)
		goto error;
	for (i = 0; i < bset1->n_eq; ++i) {
		k = isl_basic_set_alloc_equality(lin);
		if (k < 0)
			goto error;
		isl_int_set_si(lin->eq[k][0], 0);
		isl_seq_cpy(lin->eq[k] + 1, bset1->eq[i] + 1, dim);
	}
	for (i = 0; i < bset1->n_ineq; ++i) {
		k = isl_basic_set_alloc_inequality(lin);
		if (k < 0)
			goto error;
		isl_int_set_si(lin->ineq[k][0], 0);
		isl_seq_cpy(lin->ineq[k] + 1, bset1->ineq[i] + 1, dim);
	}
	for (i = 0; i < bset2->n_eq; ++i) {
		k = isl_basic_set_alloc_equality(lin);
		if (k < 0)
			goto error;
		isl_int_set_si(lin->eq[k][0], 0);
		isl_seq_neg(lin->eq[k] + 1, bset2->eq[i] + 1, dim);
	}
	for (i = 0; i < bset2->n_ineq; ++i) {
		k = isl_basic_set_alloc_inequality(lin);
		if (k < 0)
			goto error;
		isl_int_set_si(lin->ineq[k][0], 0);
		isl_seq_neg(lin->ineq[k] + 1, bset2->ineq[i] + 1, dim);
	}

	isl_basic_set_free(bset1);
	isl_basic_set_free(bset2);
	return isl_basic_set_affine_hull(lin);
error:
	isl_basic_set_free(lin);
	isl_basic_set_free(bset1);
	isl_basic_set_free(bset2);
	return NULL;
}

static __isl_give isl_basic_set *uset_convex_hull(__isl_take isl_set *set);

/* Given a set and a linear space "lin" of dimension n > 0,
 * project the linear space from the set, compute the convex hull
 * and then map the set back to the original space.
 *
 * Let
 *
 *	M x = 0
 *
 * describe the linear space.  We first compute the Hermite normal
 * form H = M U of M = H Q, to obtain
 *
 *	H Q x = 0
 *
 * The last n rows of H will be zero, so the last n variables of x' = Q x
 * are the one we want to project out.  We do this by transforming each
 * basic set A x >= b to A U x' >= b and then removing the last n dimensions.
 * After computing the convex hull in x'_1, i.e., A' x'_1 >= b',
 * we transform the hull back to the original space as A' Q_1 x >= b',
 * with Q_1 all but the last n rows of Q.
 */
static __isl_give isl_basic_set *modulo_lineality(__isl_take isl_set *set,
	__isl_take isl_basic_set *lin)
{
	unsigned total = isl_basic_set_total_dim(lin);
	unsigned lin_dim;
	struct isl_basic_set *hull;
	struct isl_mat *M, *U, *Q;

	if (!set || !lin)
		goto error;
	lin_dim = total - lin->n_eq;
	M = isl_mat_sub_alloc6(set->ctx, lin->eq, 0, lin->n_eq, 1, total);
	M = isl_mat_left_hermite(M, 0, &U, &Q);
	if (!M)
		goto error;
	isl_mat_free(M);
	isl_basic_set_free(lin);

	Q = isl_mat_drop_rows(Q, Q->n_row - lin_dim, lin_dim);

	U = isl_mat_lin_to_aff(U);
	Q = isl_mat_lin_to_aff(Q);

	set = isl_set_preimage(set, U);
	set = isl_set_remove_dims(set, isl_dim_set, total - lin_dim, lin_dim);
	hull = uset_convex_hull(set);
	hull = isl_basic_set_preimage(hull, Q);

	return hull;
error:
	isl_basic_set_free(lin);
	isl_set_free(set);
	return NULL;
}

/* Given two polyhedra with as constraints h_{ij} x >= 0 in homegeneous space,
 * set up an LP for solving
 *
 *	\sum_j \alpha_{1j} h_{1j} = \sum_j \alpha_{2j} h_{2j}
 *
 * \alpha{i0} corresponds to the (implicit) positivity constraint 1 >= 0
 * The next \alpha{ij} correspond to the equalities and come in pairs.
 * The final \alpha{ij} correspond to the inequalities.
 */
static __isl_give isl_basic_set *valid_direction_lp(
	__isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
{
	isl_space *dim;
	struct isl_basic_set *lp;
	unsigned d;
	int n;
	int i, j, k;

	if (!bset1 || !bset2)
		goto error;
	d = 1 + isl_basic_set_total_dim(bset1);
	n = 2 +
	    2 * bset1->n_eq + bset1->n_ineq + 2 * bset2->n_eq + bset2->n_ineq;
	dim = isl_space_set_alloc(bset1->ctx, 0, n);
	lp = isl_basic_set_alloc_space(dim, 0, d, n);
	if (!lp)
		goto error;
	for (i = 0; i < n; ++i) {
		k = isl_basic_set_alloc_inequality(lp);
		if (k < 0)
			goto error;
		isl_seq_clr(lp->ineq[k] + 1, n);
		isl_int_set_si(lp->ineq[k][0], -1);
		isl_int_set_si(lp->ineq[k][1 + i], 1);
	}
	for (i = 0; i < d; ++i) {
		k = isl_basic_set_alloc_equality(lp);
		if (k < 0)
			goto error;
		n = 0;
		isl_int_set_si(lp->eq[k][n], 0); n++;
		/* positivity constraint 1 >= 0 */
		isl_int_set_si(lp->eq[k][n], i == 0); n++;
		for (j = 0; j < bset1->n_eq; ++j) {
			isl_int_set(lp->eq[k][n], bset1->eq[j][i]); n++;
			isl_int_neg(lp->eq[k][n], bset1->eq[j][i]); n++;
		}
		for (j = 0; j < bset1->n_ineq; ++j) {
			isl_int_set(lp->eq[k][n], bset1->ineq[j][i]); n++;
		}
		/* positivity constraint 1 >= 0 */
		isl_int_set_si(lp->eq[k][n], -(i == 0)); n++;
		for (j = 0; j < bset2->n_eq; ++j) {
			isl_int_neg(lp->eq[k][n], bset2->eq[j][i]); n++;
			isl_int_set(lp->eq[k][n], bset2->eq[j][i]); n++;
		}
		for (j = 0; j < bset2->n_ineq; ++j) {
			isl_int_neg(lp->eq[k][n], bset2->ineq[j][i]); n++;
		}
	}
	lp = isl_basic_set_gauss(lp, NULL);
	isl_basic_set_free(bset1);
	isl_basic_set_free(bset2);
	return lp;
error:
	isl_basic_set_free(bset1);
	isl_basic_set_free(bset2);
	return NULL;
}

/* Compute a vector s in the homogeneous space such that <s, r> > 0
 * for all rays in the homogeneous space of the two cones that correspond
 * to the input polyhedra bset1 and bset2.
 *
 * We compute s as a vector that satisfies
 *
 *	s = \sum_j \alpha_{ij} h_{ij}	for i = 1,2			(*)
 *
 * with h_{ij} the normals of the facets of polyhedron i
 * (including the "positivity constraint" 1 >= 0) and \alpha_{ij}
 * strictly positive numbers.  For simplicity we impose \alpha_{ij} >= 1.
 * We first set up an LP with as variables the \alpha{ij}.
 * In this formulation, for each polyhedron i,
 * the first constraint is the positivity constraint, followed by pairs
 * of variables for the equalities, followed by variables for the inequalities.
 * We then simply pick a feasible solution and compute s using (*).
 *
 * Note that we simply pick any valid direction and make no attempt
 * to pick a "good" or even the "best" valid direction.
 */
static __isl_give isl_vec *valid_direction(
	__isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
{
	struct isl_basic_set *lp;
	struct isl_tab *tab;
	struct isl_vec *sample = NULL;
	struct isl_vec *dir;
	unsigned d;
	int i;
	int n;

	if (!bset1 || !bset2)
		goto error;
	lp = valid_direction_lp(isl_basic_set_copy(bset1),
				isl_basic_set_copy(bset2));
	tab = isl_tab_from_basic_set(lp, 0);
	sample = isl_tab_get_sample_value(tab);
	isl_tab_free(tab);
	isl_basic_set_free(lp);
	if (!sample)
		goto error;
	d = isl_basic_set_total_dim(bset1);
	dir = isl_vec_alloc(bset1->ctx, 1 + d);
	if (!dir)
		goto error;
	isl_seq_clr(dir->block.data + 1, dir->size - 1);
	n = 1;
	/* positivity constraint 1 >= 0 */
	isl_int_set(dir->block.data[0], sample->block.data[n]); n++;
	for (i = 0; i < bset1->n_eq; ++i) {
		isl_int_sub(sample->block.data[n],
			    sample->block.data[n], sample->block.data[n+1]);
		isl_seq_combine(dir->block.data,
				bset1->ctx->one, dir->block.data,
				sample->block.data[n], bset1->eq[i], 1 + d);

		n += 2;
	}
	for (i = 0; i < bset1->n_ineq; ++i)
		isl_seq_combine(dir->block.data,
				bset1->ctx->one, dir->block.data,
				sample->block.data[n++], bset1->ineq[i], 1 + d);
	isl_vec_free(sample);
	isl_seq_normalize(bset1->ctx, dir->el, dir->size);
	isl_basic_set_free(bset1);
	isl_basic_set_free(bset2);
	return dir;
error:
	isl_vec_free(sample);
	isl_basic_set_free(bset1);
	isl_basic_set_free(bset2);
	return NULL;
}

/* Given a polyhedron b_i + A_i x >= 0 and a map T = S^{-1},
 * compute b_i' + A_i' x' >= 0, with
 *
 *	[ b_i A_i ]        [ y' ]		              [ y' ]
 *	[  1   0  ] S^{-1} [ x' ] >= 0	or	[ b_i' A_i' ] [ x' ] >= 0
 *
 * In particular, add the "positivity constraint" and then perform
 * the mapping.
 */
static __isl_give isl_basic_set *homogeneous_map(__isl_take isl_basic_set *bset,
	__isl_take isl_mat *T)
{
	int k;

	if (!bset)
		goto error;
	bset = isl_basic_set_extend_constraints(bset, 0, 1);
	k = isl_basic_set_alloc_inequality(bset);
	if (k < 0)
		goto error;
	isl_seq_clr(bset->ineq[k] + 1, isl_basic_set_total_dim(bset));
	isl_int_set_si(bset->ineq[k][0], 1);
	bset = isl_basic_set_preimage(bset, T);
	return bset;
error:
	isl_mat_free(T);
	isl_basic_set_free(bset);
	return NULL;
}

/* Compute the convex hull of a pair of basic sets without any parameters or
 * integer divisions, where the convex hull is known to be pointed,
 * but the basic sets may be unbounded.
 *
 * We turn this problem into the computation of a convex hull of a pair
 * _bounded_ polyhedra by "changing the direction of the homogeneous
 * dimension".  This idea is due to Matthias Koeppe.
 *
 * Consider the cones in homogeneous space that correspond to the
 * input polyhedra.  The rays of these cones are also rays of the
 * polyhedra if the coordinate that corresponds to the homogeneous
 * dimension is zero.  That is, if the inner product of the rays
 * with the homogeneous direction is zero.
 * The cones in the homogeneous space can also be considered to
 * correspond to other pairs of polyhedra by chosing a different
 * homogeneous direction.  To ensure that both of these polyhedra
 * are bounded, we need to make sure that all rays of the cones
 * correspond to vertices and not to rays.
 * Let s be a direction such that <s, r> > 0 for all rays r of both cones.
 * Then using s as a homogeneous direction, we obtain a pair of polytopes.
 * The vector s is computed in valid_direction.
 *
 * Note that we need to consider _all_ rays of the cones and not just
 * the rays that correspond to rays in the polyhedra.  If we were to
 * only consider those rays and turn them into vertices, then we
 * may inadvertently turn some vertices into rays.
 *
 * The standard homogeneous direction is the unit vector in the 0th coordinate.
 * We therefore transform the two polyhedra such that the selected
 * direction is mapped onto this standard direction and then proceed
 * with the normal computation.
 * Let S be a non-singular square matrix with s as its first row,
 * then we want to map the polyhedra to the space
 *
 *	[ y' ]     [ y ]		[ y ]          [ y' ]
 *	[ x' ] = S [ x ]	i.e.,	[ x ] = S^{-1} [ x' ]
 *
 * We take S to be the unimodular completion of s to limit the growth
 * of the coefficients in the following computations.
 *
 * Let b_i + A_i x >= 0 be the constraints of polyhedron i.
 * We first move to the homogeneous dimension
 *
 *	b_i y + A_i x >= 0		[ b_i A_i ] [ y ]    [ 0 ]
 *	    y         >= 0	or	[  1   0  ] [ x ] >= [ 0 ]
 *
 * Then we change directoin
 *
 *	[ b_i A_i ]        [ y' ]		              [ y' ]
 *	[  1   0  ] S^{-1} [ x' ] >= 0	or	[ b_i' A_i' ] [ x' ] >= 0
 *
 * Then we compute the convex hull of the polytopes b_i' + A_i' x' >= 0
 * resulting in b' + A' x' >= 0, which we then convert back
 *
 *	            [ y ]		        [ y ]
 *	[ b' A' ] S [ x ] >= 0	or	[ b A ] [ x ] >= 0
 *
 * The polyhedron b + A x >= 0 is then the convex hull of the input polyhedra.
 */
static __isl_give isl_basic_set *convex_hull_pair_pointed(
	__isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
{
	struct isl_ctx *ctx = NULL;
	struct isl_vec *dir = NULL;
	struct isl_mat *T = NULL;
	struct isl_mat *T2 = NULL;
	struct isl_basic_set *hull;
	struct isl_set *set;

	if (!bset1 || !bset2)
		goto error;
	ctx = isl_basic_set_get_ctx(bset1);
	dir = valid_direction(isl_basic_set_copy(bset1),
				isl_basic_set_copy(bset2));
	if (!dir)
		goto error;
	T = isl_mat_alloc(ctx, dir->size, dir->size);
	if (!T)
		goto error;
	isl_seq_cpy(T->row[0], dir->block.data, dir->size);
	T = isl_mat_unimodular_complete(T, 1);
	T2 = isl_mat_right_inverse(isl_mat_copy(T));

	bset1 = homogeneous_map(bset1, isl_mat_copy(T2));
	bset2 = homogeneous_map(bset2, T2);
	set = isl_set_alloc_space(isl_basic_set_get_space(bset1), 2, 0);
	set = isl_set_add_basic_set(set, bset1);
	set = isl_set_add_basic_set(set, bset2);
	hull = uset_convex_hull(set);
	hull = isl_basic_set_preimage(hull, T);
	 
	isl_vec_free(dir);

	return hull;
error:
	isl_vec_free(dir);
	isl_basic_set_free(bset1);
	isl_basic_set_free(bset2);
	return NULL;
}

static __isl_give isl_basic_set *uset_convex_hull_wrap(__isl_take isl_set *set);
static __isl_give isl_basic_set *modulo_affine_hull(
	__isl_take isl_set *set, __isl_take isl_basic_set *affine_hull);

/* Compute the convex hull of a pair of basic sets without any parameters or
 * integer divisions.
 *
 * This function is called from uset_convex_hull_unbounded, which
 * means that the complete convex hull is unbounded.  Some pairs
 * of basic sets may still be bounded, though.
 * They may even lie inside a lower dimensional space, in which
 * case they need to be handled inside their affine hull since
 * the main algorithm assumes that the result is full-dimensional.
 *
 * If the convex hull of the two basic sets would have a non-trivial
 * lineality space, we first project out this lineality space.
 */
static __isl_give isl_basic_set *convex_hull_pair(
	__isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
{
	isl_basic_set *lin, *aff;
	int bounded1, bounded2;

	if (bset1->ctx->opt->convex == ISL_CONVEX_HULL_FM)
		return convex_hull_pair_elim(bset1, bset2);

	aff = isl_set_affine_hull(isl_basic_set_union(isl_basic_set_copy(bset1),
						    isl_basic_set_copy(bset2)));
	if (!aff)
		goto error;
	if (aff->n_eq != 0) 
		return modulo_affine_hull(isl_basic_set_union(bset1, bset2), aff);
	isl_basic_set_free(aff);

	bounded1 = isl_basic_set_is_bounded(bset1);
	bounded2 = isl_basic_set_is_bounded(bset2);

	if (bounded1 < 0 || bounded2 < 0)
		goto error;

	if (bounded1 && bounded2)
		return uset_convex_hull_wrap(isl_basic_set_union(bset1, bset2));

	if (bounded1 || bounded2)
		return convex_hull_pair_pointed(bset1, bset2);

	lin = induced_lineality_space(isl_basic_set_copy(bset1),
				      isl_basic_set_copy(bset2));
	if (!lin)
		goto error;
	if (isl_basic_set_plain_is_universe(lin)) {
		isl_basic_set_free(bset1);
		isl_basic_set_free(bset2);
		return lin;
	}
	if (lin->n_eq < isl_basic_set_total_dim(lin)) {
		struct isl_set *set;
		set = isl_set_alloc_space(isl_basic_set_get_space(bset1), 2, 0);
		set = isl_set_add_basic_set(set, bset1);
		set = isl_set_add_basic_set(set, bset2);
		return modulo_lineality(set, lin);
	}
	isl_basic_set_free(lin);

	return convex_hull_pair_pointed(bset1, bset2);
error:
	isl_basic_set_free(bset1);
	isl_basic_set_free(bset2);
	return NULL;
}

/* Compute the lineality space of a basic set.
 * We basically just drop the constants and turn every inequality
 * into an equality.
 * Any explicit representations of local variables are removed
 * because they may no longer be valid representations
 * in the lineality space.
 */
__isl_give isl_basic_set *isl_basic_set_lineality_space(
	__isl_take isl_basic_set *bset)
{
	int i, k;
	struct isl_basic_set *lin = NULL;
	unsigned n_div, dim;

	if (!bset)
		goto error;
	n_div = isl_basic_set_dim(bset, isl_dim_div);
	dim = isl_basic_set_total_dim(bset);

	lin = isl_basic_set_alloc_space(isl_basic_set_get_space(bset),
					n_div, dim, 0);
	for (i = 0; i < n_div; ++i)
		if (isl_basic_set_alloc_div(lin) < 0)
			goto error;
	if (!lin)
		goto error;
	for (i = 0; i < bset->n_eq; ++i) {
		k = isl_basic_set_alloc_equality(lin);
		if (k < 0)
			goto error;
		isl_int_set_si(lin->eq[k][0], 0);
		isl_seq_cpy(lin->eq[k] + 1, bset->eq[i] + 1, dim);
	}
	lin = isl_basic_set_gauss(lin, NULL);
	if (!lin)
		goto error;
	for (i = 0; i < bset->n_ineq && lin->n_eq < dim; ++i) {
		k = isl_basic_set_alloc_equality(lin);
		if (k < 0)
			goto error;
		isl_int_set_si(lin->eq[k][0], 0);
		isl_seq_cpy(lin->eq[k] + 1, bset->ineq[i] + 1, dim);
		lin = isl_basic_set_gauss(lin, NULL);
		if (!lin)
			goto error;
	}
	isl_basic_set_free(bset);
	return lin;
error:
	isl_basic_set_free(lin);
	isl_basic_set_free(bset);
	return NULL;
}

/* Compute the (linear) hull of the lineality spaces of the basic sets in the
 * set "set".
 */
__isl_give isl_basic_set *isl_set_combined_lineality_space(
	__isl_take isl_set *set)
{
	int i;
	struct isl_set *lin = NULL;

	if (!set)
		return NULL;
	if (set->n == 0) {
		isl_space *space = isl_set_get_space(set);
		isl_set_free(set);
		return isl_basic_set_empty(space);
	}

	lin = isl_set_alloc_space(isl_set_get_space(set), set->n, 0);
	for (i = 0; i < set->n; ++i)
		lin = isl_set_add_basic_set(lin,
		    isl_basic_set_lineality_space(isl_basic_set_copy(set->p[i])));
	isl_set_free(set);
	return isl_set_affine_hull(lin);
}

/* Compute the convex hull of a set without any parameters or
 * integer divisions.
 * In each step, we combined two basic sets until only one
 * basic set is left.
 * The input basic sets are assumed not to have a non-trivial
 * lineality space.  If any of the intermediate results has
 * a non-trivial lineality space, it is projected out.
 */
static __isl_give isl_basic_set *uset_convex_hull_unbounded(
	__isl_take isl_set *set)
{
	isl_basic_set_list *list;

	list = isl_set_get_basic_set_list(set);
	isl_set_free(set);

	while (list) {
		int n;
		struct isl_basic_set *t;
		isl_basic_set *bset1, *bset2;

		n = isl_basic_set_list_n_basic_set(list);
		if (n < 2)
			isl_die(isl_basic_set_list_get_ctx(list),
				isl_error_internal,
				"expecting at least two elements", goto error);
		bset1 = isl_basic_set_list_get_basic_set(list, n - 1);
		bset2 = isl_basic_set_list_get_basic_set(list, n - 2);
		bset1 = convex_hull_pair(bset1, bset2);
		if (n == 2) {
			isl_basic_set_list_free(list);
			return bset1;
		}
		bset1 = isl_basic_set_underlying_set(bset1);
		list = isl_basic_set_list_drop(list, n - 2, 2);
		list = isl_basic_set_list_add(list, bset1);

		t = isl_basic_set_list_get_basic_set(list, n - 2);
		t = isl_basic_set_lineality_space(t);
		if (!t)
			goto error;
		if (isl_basic_set_plain_is_universe(t)) {
			isl_basic_set_list_free(list);
			return t;
		}
		if (t->n_eq < isl_basic_set_total_dim(t)) {
			set = isl_basic_set_list_union(list);
			return modulo_lineality(set, t);
		}
		isl_basic_set_free(t);
	}

	return NULL;
error:
	isl_basic_set_list_free(list);
	return NULL;
}

/* Compute an initial hull for wrapping containing a single initial
 * facet.
 * This function assumes that the given set is bounded.
 */
static __isl_give isl_basic_set *initial_hull(__isl_take isl_basic_set *hull,
	__isl_keep isl_set *set)
{
	struct isl_mat *bounds = NULL;
	unsigned dim;
	int k;

	if (!hull)
		goto error;
	bounds = initial_facet_constraint(set);
	if (!bounds)
		goto error;
	k = isl_basic_set_alloc_inequality(hull);
	if (k < 0)
		goto error;
	dim = isl_set_n_dim(set);
	isl_assert(set->ctx, 1 + dim == bounds->n_col, goto error);
	isl_seq_cpy(hull->ineq[k], bounds->row[0], bounds->n_col);
	isl_mat_free(bounds);

	return hull;
error:
	isl_basic_set_free(hull);
	isl_mat_free(bounds);
	return NULL;
}

struct max_constraint {
	struct isl_mat *c;
	int	 	count;
	int		ineq;
};

static int max_constraint_equal(const void *entry, const void *val)
{
	struct max_constraint *a = (struct max_constraint *)entry;
	isl_int *b = (isl_int *)val;

	return isl_seq_eq(a->c->row[0] + 1, b, a->c->n_col - 1);
}

static void update_constraint(struct isl_ctx *ctx, struct isl_hash_table *table,
	isl_int *con, unsigned len, int n, int ineq)
{
	struct isl_hash_table_entry *entry;
	struct max_constraint *c;
	uint32_t c_hash;

	c_hash = isl_seq_get_hash(con + 1, len);
	entry = isl_hash_table_find(ctx, table, c_hash, max_constraint_equal,
			con + 1, 0);
	if (!entry)
		return;
	c = entry->data;
	if (c->count < n) {
		isl_hash_table_remove(ctx, table, entry);
		return;
	}
	c->count++;
	if (isl_int_gt(c->c->row[0][0], con[0]))
		return;
	if (isl_int_eq(c->c->row[0][0], con[0])) {
		if (ineq)
			c->ineq = ineq;
		return;
	}
	c->c = isl_mat_cow(c->c);
	isl_int_set(c->c->row[0][0], con[0]);
	c->ineq = ineq;
}

/* Check whether the constraint hash table "table" contains the constraint
 * "con".
 */
static int has_constraint(struct isl_ctx *ctx, struct isl_hash_table *table,
	isl_int *con, unsigned len, int n)
{
	struct isl_hash_table_entry *entry;
	struct max_constraint *c;
	uint32_t c_hash;

	c_hash = isl_seq_get_hash(con + 1, len);
	entry = isl_hash_table_find(ctx, table, c_hash, max_constraint_equal,
			con + 1, 0);
	if (!entry)
		return 0;
	c = entry->data;
	if (c->count < n)
		return 0;
	return isl_int_eq(c->c->row[0][0], con[0]);
}

/* Check for inequality constraints of a basic set without equalities
 * such that the same or more stringent copies of the constraint appear
 * in all of the basic sets.  Such constraints are necessarily facet
 * constraints of the convex hull.
 *
 * If the resulting basic set is by chance identical to one of
 * the basic sets in "set", then we know that this basic set contains
 * all other basic sets and is therefore the convex hull of set.
 * In this case we set *is_hull to 1.
 */
static __isl_give isl_basic_set *common_constraints(
	__isl_take isl_basic_set *hull, __isl_keep isl_set *set, int *is_hull)
{
	int i, j, s, n;
	int min_constraints;
	int best;
	struct max_constraint *constraints = NULL;
	struct isl_hash_table *table = NULL;
	unsigned total;

	*is_hull = 0;

	for (i = 0; i < set->n; ++i)
		if (set->p[i]->n_eq == 0)
			break;
	if (i >= set->n)
		return hull;
	min_constraints = set->p[i]->n_ineq;
	best = i;
	for (i = best + 1; i < set->n; ++i) {
		if (set->p[i]->n_eq != 0)
			continue;
		if (set->p[i]->n_ineq >= min_constraints)
			continue;
		min_constraints = set->p[i]->n_ineq;
		best = i;
	}
	constraints = isl_calloc_array(hull->ctx, struct max_constraint,
					min_constraints);
	if (!constraints)
		return hull;
	table = isl_alloc_type(hull->ctx, struct isl_hash_table);
	if (isl_hash_table_init(hull->ctx, table, min_constraints))
		goto error;

	total = isl_space_dim(set->dim, isl_dim_all);
	for (i = 0; i < set->p[best]->n_ineq; ++i) {
		constraints[i].c = isl_mat_sub_alloc6(hull->ctx,
			set->p[best]->ineq + i, 0, 1, 0, 1 + total);
		if (!constraints[i].c)
			goto error;
		constraints[i].ineq = 1;
	}
	for (i = 0; i < min_constraints; ++i) {
		struct isl_hash_table_entry *entry;
		uint32_t c_hash;
		c_hash = isl_seq_get_hash(constraints[i].c->row[0] + 1, total);
		entry = isl_hash_table_find(hull->ctx, table, c_hash,
			max_constraint_equal, constraints[i].c->row[0] + 1, 1);
		if (!entry)
			goto error;
		isl_assert(hull->ctx, !entry->data, goto error);
		entry->data = &constraints[i];
	}

	n = 0;
	for (s = 0; s < set->n; ++s) {
		if (s == best)
			continue;

		for (i = 0; i < set->p[s]->n_eq; ++i) {
			isl_int *eq = set->p[s]->eq[i];
			for (j = 0; j < 2; ++j) {
				isl_seq_neg(eq, eq, 1 + total);
				update_constraint(hull->ctx, table,
							    eq, total, n, 0);
			}
		}
		for (i = 0; i < set->p[s]->n_ineq; ++i) {
			isl_int *ineq = set->p[s]->ineq[i];
			update_constraint(hull->ctx, table, ineq, total, n,
				set->p[s]->n_eq == 0);
		}
		++n;
	}

	for (i = 0; i < min_constraints; ++i) {
		if (constraints[i].count < n)
			continue;
		if (!constraints[i].ineq)
			continue;
		j = isl_basic_set_alloc_inequality(hull);
		if (j < 0)
			goto error;
		isl_seq_cpy(hull->ineq[j], constraints[i].c->row[0], 1 + total);
	}

	for (s = 0; s < set->n; ++s) {
		if (set->p[s]->n_eq)
			continue;
		if (set->p[s]->n_ineq != hull->n_ineq)
			continue;
		for (i = 0; i < set->p[s]->n_ineq; ++i) {
			isl_int *ineq = set->p[s]->ineq[i];
			if (!has_constraint(hull->ctx, table, ineq, total, n))
				break;
		}
		if (i == set->p[s]->n_ineq)
			*is_hull = 1;
	}

	isl_hash_table_clear(table);
	for (i = 0; i < min_constraints; ++i)
		isl_mat_free(constraints[i].c);
	free(constraints);
	free(table);
	return hull;
error:
	isl_hash_table_clear(table);
	free(table);
	if (constraints)
		for (i = 0; i < min_constraints; ++i)
			isl_mat_free(constraints[i].c);
	free(constraints);
	return hull;
}

/* Create a template for the convex hull of "set" and fill it up
 * obvious facet constraints, if any.  If the result happens to
 * be the convex hull of "set" then *is_hull is set to 1.
 */
static __isl_give isl_basic_set *proto_hull(__isl_keep isl_set *set,
	int *is_hull)
{
	struct isl_basic_set *hull;
	unsigned n_ineq;
	int i;

	n_ineq = 1;
	for (i = 0; i < set->n; ++i) {
		n_ineq += set->p[i]->n_eq;
		n_ineq += set->p[i]->n_ineq;
	}
	hull = isl_basic_set_alloc_space(isl_space_copy(set->dim), 0, 0, n_ineq);
	hull = isl_basic_set_set_rational(hull);
	if (!hull)
		return NULL;
	return common_constraints(hull, set, is_hull);
}

static __isl_give isl_basic_set *uset_convex_hull_wrap(__isl_take isl_set *set)
{
	struct isl_basic_set *hull;
	int is_hull;

	hull = proto_hull(set, &is_hull);
	if (hull && !is_hull) {
		if (hull->n_ineq == 0)
			hull = initial_hull(hull, set);
		hull = extend(hull, set);
	}
	isl_set_free(set);

	return hull;
}

/* Compute the convex hull of a set without any parameters or
 * integer divisions.  Depending on whether the set is bounded,
 * we pass control to the wrapping based convex hull or
 * the Fourier-Motzkin elimination based convex hull.
 * We also handle a few special cases before checking the boundedness.
 */
static __isl_give isl_basic_set *uset_convex_hull(__isl_take isl_set *set)
{
	isl_bool bounded;
	struct isl_basic_set *convex_hull = NULL;
	struct isl_basic_set *lin;

	if (isl_set_n_dim(set) == 0)
		return convex_hull_0d(set);

	set = isl_set_coalesce(set);
	set = isl_set_set_rational(set);

	if (!set)
		return NULL;
	if (set->n == 1) {
		convex_hull = isl_basic_set_copy(set->p[0]);
		isl_set_free(set);
		return convex_hull;
	}
	if (isl_set_n_dim(set) == 1)
		return convex_hull_1d(set);

	bounded = isl_set_is_bounded(set);
	if (bounded < 0)
		goto error;
	if (bounded && set->ctx->opt->convex == ISL_CONVEX_HULL_WRAP)
		return uset_convex_hull_wrap(set);

	lin = isl_set_combined_lineality_space(isl_set_copy(set));
	if (!lin)
		goto error;
	if (isl_basic_set_plain_is_universe(lin)) {
		isl_set_free(set);
		return lin;
	}
	if (lin->n_eq < isl_basic_set_total_dim(lin))
		return modulo_lineality(set, lin);
	isl_basic_set_free(lin);

	return uset_convex_hull_unbounded(set);
error:
	isl_set_free(set);
	isl_basic_set_free(convex_hull);
	return NULL;
}

/* This is the core procedure, where "set" is a "pure" set, i.e.,
 * without parameters or divs and where the convex hull of set is
 * known to be full-dimensional.
 */
static __isl_give isl_basic_set *uset_convex_hull_wrap_bounded(
	__isl_take isl_set *set)
{
	struct isl_basic_set *convex_hull = NULL;

	if (!set)
		goto error;

	if (isl_set_n_dim(set) == 0) {
		convex_hull = isl_basic_set_universe(isl_space_copy(set->dim));
		isl_set_free(set);
		convex_hull = isl_basic_set_set_rational(convex_hull);
		return convex_hull;
	}

	set = isl_set_set_rational(set);
	set = isl_set_coalesce(set);
	if (!set)
		goto error;
	if (set->n == 1) {
		convex_hull = isl_basic_set_copy(set->p[0]);
		isl_set_free(set);
		convex_hull = isl_basic_map_remove_redundancies(convex_hull);
		return convex_hull;
	}
	if (isl_set_n_dim(set) == 1)
		return convex_hull_1d(set);

	return uset_convex_hull_wrap(set);
error:
	isl_set_free(set);
	return NULL;
}

/* Compute the convex hull of set "set" with affine hull "affine_hull",
 * We first remove the equalities (transforming the set), compute the
 * convex hull of the transformed set and then add the equalities back
 * (after performing the inverse transformation.
 */
static __isl_give isl_basic_set *modulo_affine_hull(
	__isl_take isl_set *set, __isl_take isl_basic_set *affine_hull)
{
	struct isl_mat *T;
	struct isl_mat *T2;
	struct isl_basic_set *dummy;
	struct isl_basic_set *convex_hull;

	dummy = isl_basic_set_remove_equalities(
			isl_basic_set_copy(affine_hull), &T, &T2);
	if (!dummy)
		goto error;
	isl_basic_set_free(dummy);
	set = isl_set_preimage(set, T);
	convex_hull = uset_convex_hull(set);
	convex_hull = isl_basic_set_preimage(convex_hull, T2);
	convex_hull = isl_basic_set_intersect(convex_hull, affine_hull);
	return convex_hull;
error:
	isl_mat_free(T);
	isl_mat_free(T2);
	isl_basic_set_free(affine_hull);
	isl_set_free(set);
	return NULL;
}

/* Return an empty basic map living in the same space as "map".
 */
static __isl_give isl_basic_map *replace_map_by_empty_basic_map(
	__isl_take isl_map *map)
{
	isl_space *space;

	space = isl_map_get_space(map);
	isl_map_free(map);
	return isl_basic_map_empty(space);
}

/* Compute the convex hull of a map.
 *
 * The implementation was inspired by "Extended Convex Hull" by Fukuda et al.,
 * specifically, the wrapping of facets to obtain new facets.
 */
__isl_give isl_basic_map *isl_map_convex_hull(__isl_take isl_map *map)
{
	struct isl_basic_set *bset;
	struct isl_basic_map *model = NULL;
	struct isl_basic_set *affine_hull = NULL;
	struct isl_basic_map *convex_hull = NULL;
	struct isl_set *set = NULL;

	map = isl_map_detect_equalities(map);
	map = isl_map_align_divs_internal(map);
	if (!map)
		goto error;

	if (map->n == 0)
		return replace_map_by_empty_basic_map(map);

	model = isl_basic_map_copy(map->p[0]);
	set = isl_map_underlying_set(map);
	if (!set)
		goto error;

	affine_hull = isl_set_affine_hull(isl_set_copy(set));
	if (!affine_hull)
		goto error;
	if (affine_hull->n_eq != 0)
		bset = modulo_affine_hull(set, affine_hull);
	else {
		isl_basic_set_free(affine_hull);
		bset = uset_convex_hull(set);
	}

	convex_hull = isl_basic_map_overlying_set(bset, model);
	if (!convex_hull)
		return NULL;

	ISL_F_SET(convex_hull, ISL_BASIC_MAP_NO_IMPLICIT);
	ISL_F_SET(convex_hull, ISL_BASIC_MAP_ALL_EQUALITIES);
	ISL_F_CLR(convex_hull, ISL_BASIC_MAP_RATIONAL);
	return convex_hull;
error:
	isl_set_free(set);
	isl_basic_map_free(model);
	return NULL;
}

struct isl_basic_set *isl_set_convex_hull(struct isl_set *set)
{
	return bset_from_bmap(isl_map_convex_hull(set_to_map(set)));
}

__isl_give isl_basic_map *isl_map_polyhedral_hull(__isl_take isl_map *map)
{
	isl_basic_map *hull;

	hull = isl_map_convex_hull(map);
	return isl_basic_map_remove_divs(hull);
}

__isl_give isl_basic_set *isl_set_polyhedral_hull(__isl_take isl_set *set)
{
	return bset_from_bmap(isl_map_polyhedral_hull(set_to_map(set)));
}

struct sh_data_entry {
	struct isl_hash_table	*table;
	struct isl_tab		*tab;
};

/* Holds the data needed during the simple hull computation.
 * In particular,
 *	n		the number of basic sets in the original set
 *	hull_table	a hash table of already computed constraints
 *			in the simple hull
 *	p		for each basic set,
 *		table		a hash table of the constraints
 *		tab		the tableau corresponding to the basic set
 */
struct sh_data {
	struct isl_ctx		*ctx;
	unsigned		n;
	struct isl_hash_table	*hull_table;
	struct sh_data_entry	p[1];
};

static void sh_data_free(struct sh_data *data)
{
	int i;

	if (!data)
		return;
	isl_hash_table_free(data->ctx, data->hull_table);
	for (i = 0; i < data->n; ++i) {
		isl_hash_table_free(data->ctx, data->p[i].table);
		isl_tab_free(data->p[i].tab);
	}
	free(data);
}

struct ineq_cmp_data {
	unsigned	len;
	isl_int		*p;
};

static int has_ineq(const void *entry, const void *val)
{
	isl_int *row = (isl_int *)entry;
	struct ineq_cmp_data *v = (struct ineq_cmp_data *)val;

	return isl_seq_eq(row + 1, v->p + 1, v->len) ||
	       isl_seq_is_neg(row + 1, v->p + 1, v->len);
}

static int hash_ineq(struct isl_ctx *ctx, struct isl_hash_table *table,
			isl_int *ineq, unsigned len)
{
	uint32_t c_hash;
	struct ineq_cmp_data v;
	struct isl_hash_table_entry *entry;

	v.len = len;
	v.p = ineq;
	c_hash = isl_seq_get_hash(ineq + 1, len);
	entry = isl_hash_table_find(ctx, table, c_hash, has_ineq, &v, 1);
	if (!entry)
		return - 1;
	entry->data = ineq;
	return 0;
}

/* Fill hash table "table" with the constraints of "bset".
 * Equalities are added as two inequalities.
 * The value in the hash table is a pointer to the (in)equality of "bset".
 */
static int hash_basic_set(struct isl_hash_table *table,
	__isl_keep isl_basic_set *bset)
{
	int i, j;
	unsigned dim = isl_basic_set_total_dim(bset);

	for (i = 0; i < bset->n_eq; ++i) {
		for (j = 0; j < 2; ++j) {
			isl_seq_neg(bset->eq[i], bset->eq[i], 1 + dim);
			if (hash_ineq(bset->ctx, table, bset->eq[i], dim) < 0)
				return -1;
		}
	}
	for (i = 0; i < bset->n_ineq; ++i) {
		if (hash_ineq(bset->ctx, table, bset->ineq[i], dim) < 0)
			return -1;
	}
	return 0;
}

static struct sh_data *sh_data_alloc(__isl_keep isl_set *set, unsigned n_ineq)
{
	struct sh_data *data;
	int i;

	data = isl_calloc(set->ctx, struct sh_data,
		sizeof(struct sh_data) +
		(set->n - 1) * sizeof(struct sh_data_entry));
	if (!data)
		return NULL;
	data->ctx = set->ctx;
	data->n = set->n;
	data->hull_table = isl_hash_table_alloc(set->ctx, n_ineq);
	if (!data->hull_table)
		goto error;
	for (i = 0; i < set->n; ++i) {
		data->p[i].table = isl_hash_table_alloc(set->ctx,
				    2 * set->p[i]->n_eq + set->p[i]->n_ineq);
		if (!data->p[i].table)
			goto error;
		if (hash_basic_set(data->p[i].table, set->p[i]) < 0)
			goto error;
	}
	return data;
error:
	sh_data_free(data);
	return NULL;
}

/* Check if inequality "ineq" is a bound for basic set "j" or if
 * it can be relaxed (by increasing the constant term) to become
 * a bound for that basic set.  In the latter case, the constant
 * term is updated.
 * Relaxation of the constant term is only allowed if "shift" is set.
 *
 * Return 1 if "ineq" is a bound
 *	  0 if "ineq" may attain arbitrarily small values on basic set "j"
 *	 -1 if some error occurred
 */
static int is_bound(struct sh_data *data, __isl_keep isl_set *set, int j,
	isl_int *ineq, int shift)
{
	enum isl_lp_result res;
	isl_int opt;

	if (!data->p[j].tab) {
		data->p[j].tab = isl_tab_from_basic_set(set->p[j], 0);
		if (!data->p[j].tab)
			return -1;
	}

	isl_int_init(opt);

	res = isl_tab_min(data->p[j].tab, ineq, data->ctx->one,
				&opt, NULL, 0);
	if (res == isl_lp_ok && isl_int_is_neg(opt)) {
		if (shift)
			isl_int_sub(ineq[0], ineq[0], opt);
		else
			res = isl_lp_unbounded;
	}

	isl_int_clear(opt);

	return (res == isl_lp_ok || res == isl_lp_empty) ? 1 :
	       res == isl_lp_unbounded ? 0 : -1;
}

/* Set the constant term of "ineq" to the maximum of those of the constraints
 * in the basic sets of "set" following "i" that are parallel to "ineq".
 * That is, if any of the basic sets of "set" following "i" have a more
 * relaxed copy of "ineq", then replace "ineq" by the most relaxed copy.
 * "c_hash" is the hash value of the linear part of "ineq".
 * "v" has been set up for use by has_ineq.
 *
 * Note that the two inequality constraints corresponding to an equality are
 * represented by the same inequality constraint in data->p[j].table
 * (but with different hash values).  This means the constraint (or at
 * least its constant term) may need to be temporarily negated to get
 * the actually hashed constraint.
 */
static void set_max_constant_term(struct sh_data *data, __isl_keep isl_set *set,
	int i, isl_int *ineq, uint32_t c_hash, struct ineq_cmp_data *v)
{
	int j;
	isl_ctx *ctx;
	struct isl_hash_table_entry *entry;

	ctx = isl_set_get_ctx(set);
	for (j = i + 1; j < set->n; ++j) {
		int neg;
		isl_int *ineq_j;

		entry = isl_hash_table_find(ctx, data->p[j].table,
						c_hash, &has_ineq, v, 0);
		if (!entry)
			continue;

		ineq_j = entry->data;
		neg = isl_seq_is_neg(ineq_j + 1, ineq + 1, v->len);
		if (neg)
			isl_int_neg(ineq_j[0], ineq_j[0]);
		if (isl_int_gt(ineq_j[0], ineq[0]))
			isl_int_set(ineq[0], ineq_j[0]);
		if (neg)
			isl_int_neg(ineq_j[0], ineq_j[0]);
	}
}

/* Check if inequality "ineq" from basic set "i" is or can be relaxed to
 * become a bound on the whole set.  If so, add the (relaxed) inequality
 * to "hull".  Relaxation is only allowed if "shift" is set.
 *
 * We first check if "hull" already contains a translate of the inequality.
 * If so, we are done.
 * Then, we check if any of the previous basic sets contains a translate
 * of the inequality.  If so, then we have already considered this
 * inequality and we are done.
 * Otherwise, for each basic set other than "i", we check if the inequality
 * is a bound on the basic set, but first replace the constant term
 * by the maximal value of any translate of the inequality in any
 * of the following basic sets.
 * For previous basic sets, we know that they do not contain a translate
 * of the inequality, so we directly call is_bound.
 * For following basic sets, we first check if a translate of the
 * inequality appears in its description.  If so, the constant term
 * of the inequality has already been updated with respect to this
 * translate and the inequality is therefore known to be a bound
 * of this basic set.
 */
static __isl_give isl_basic_set *add_bound(__isl_take isl_basic_set *hull,
	struct sh_data *data, __isl_keep isl_set *set, int i, isl_int *ineq,
	int shift)
{
	uint32_t c_hash;
	struct ineq_cmp_data v;
	struct isl_hash_table_entry *entry;
	int j, k;

	if (!hull)
		return NULL;

	v.len = isl_basic_set_total_dim(hull);
	v.p = ineq;
	c_hash = isl_seq_get_hash(ineq + 1, v.len);

	entry = isl_hash_table_find(hull->ctx, data->hull_table, c_hash,
					has_ineq, &v, 0);
	if (entry)
		return hull;

	for (j = 0; j < i; ++j) {
		entry = isl_hash_table_find(hull->ctx, data->p[j].table,
						c_hash, has_ineq, &v, 0);
		if (entry)
			break;
	}
	if (j < i)
		return hull;

	k = isl_basic_set_alloc_inequality(hull);
	if (k < 0)
		goto error;
	isl_seq_cpy(hull->ineq[k], ineq, 1 + v.len);

	set_max_constant_term(data, set, i, hull->ineq[k], c_hash, &v);
	for (j = 0; j < i; ++j) {
		int bound;
		bound = is_bound(data, set, j, hull->ineq[k], shift);
		if (bound < 0)
			goto error;
		if (!bound)
			break;
	}
	if (j < i) {
		isl_basic_set_free_inequality(hull, 1);
		return hull;
	}

	for (j = i + 1; j < set->n; ++j) {
		int bound;
		entry = isl_hash_table_find(hull->ctx, data->p[j].table,
						c_hash, has_ineq, &v, 0);
		if (entry)
			continue;
		bound = is_bound(data, set, j, hull->ineq[k], shift);
		if (bound < 0)
			goto error;
		if (!bound)
			break;
	}
	if (j < set->n) {
		isl_basic_set_free_inequality(hull, 1);
		return hull;
	}

	entry = isl_hash_table_find(hull->ctx, data->hull_table, c_hash,
					has_ineq, &v, 1);
	if (!entry)
		goto error;
	entry->data = hull->ineq[k];

	return hull;
error:
	isl_basic_set_free(hull);
	return NULL;
}

/* Check if any inequality from basic set "i" is or can be relaxed to
 * become a bound on the whole set.  If so, add the (relaxed) inequality
 * to "hull".  Relaxation is only allowed if "shift" is set.
 */
static __isl_give isl_basic_set *add_bounds(__isl_take isl_basic_set *bset,
	struct sh_data *data, __isl_keep isl_set *set, int i, int shift)
{
	int j, k;
	unsigned dim = isl_basic_set_total_dim(bset);

	for (j = 0; j < set->p[i]->n_eq; ++j) {
		for (k = 0; k < 2; ++k) {
			isl_seq_neg(set->p[i]->eq[j], set->p[i]->eq[j], 1+dim);
			bset = add_bound(bset, data, set, i, set->p[i]->eq[j],
					    shift);
		}
	}
	for (j = 0; j < set->p[i]->n_ineq; ++j)
		bset = add_bound(bset, data, set, i, set->p[i]->ineq[j], shift);
	return bset;
}

/* Compute a superset of the convex hull of set that is described
 * by only (translates of) the constraints in the constituents of set.
 * Translation is only allowed if "shift" is set.
 */
static __isl_give isl_basic_set *uset_simple_hull(__isl_take isl_set *set,
	int shift)
{
	struct sh_data *data = NULL;
	struct isl_basic_set *hull = NULL;
	unsigned n_ineq;
	int i;

	if (!set)
		return NULL;

	n_ineq = 0;
	for (i = 0; i < set->n; ++i) {
		if (!set->p[i])
			goto error;
		n_ineq += 2 * set->p[i]->n_eq + set->p[i]->n_ineq;
	}

	hull = isl_basic_set_alloc_space(isl_space_copy(set->dim), 0, 0, n_ineq);
	if (!hull)
		goto error;

	data = sh_data_alloc(set, n_ineq);
	if (!data)
		goto error;

	for (i = 0; i < set->n; ++i)
		hull = add_bounds(hull, data, set, i, shift);

	sh_data_free(data);
	isl_set_free(set);

	return hull;
error:
	sh_data_free(data);
	isl_basic_set_free(hull);
	isl_set_free(set);
	return NULL;
}

/* Compute a superset of the convex hull of map that is described
 * by only (translates of) the constraints in the constituents of map.
 * Handle trivial cases where map is NULL or contains at most one disjunct.
 */
static __isl_give isl_basic_map *map_simple_hull_trivial(
	__isl_take isl_map *map)
{
	isl_basic_map *hull;

	if (!map)
		return NULL;
	if (map->n == 0)
		return replace_map_by_empty_basic_map(map);

	hull = isl_basic_map_copy(map->p[0]);
	isl_map_free(map);
	return hull;
}

/* Return a copy of the simple hull cached inside "map".
 * "shift" determines whether to return the cached unshifted or shifted
 * simple hull.
 */
static __isl_give isl_basic_map *cached_simple_hull(__isl_take isl_map *map,
	int shift)
{
	isl_basic_map *hull;

	hull = isl_basic_map_copy(map->cached_simple_hull[shift]);
	isl_map_free(map);

	return hull;
}

/* Compute a superset of the convex hull of map that is described
 * by only (translates of) the constraints in the constituents of map.
 * Translation is only allowed if "shift" is set.
 *
 * The constraints are sorted while removing redundant constraints
 * in order to indicate a preference of which constraints should
 * be preserved.  In particular, pairs of constraints that are
 * sorted together are preferred to either both be preserved
 * or both be removed.  The sorting is performed inside
 * isl_basic_map_remove_redundancies.
 *
 * The result of the computation is stored in map->cached_simple_hull[shift]
 * such that it can be reused in subsequent calls.  The cache is cleared
 * whenever the map is modified (in isl_map_cow).
 * Note that the results need to be stored in the input map for there
 * to be any chance that they may get reused.  In particular, they
 * are stored in a copy of the input map that is saved before
 * the integer division alignment.
 */
static __isl_give isl_basic_map *map_simple_hull(__isl_take isl_map *map,
	int shift)
{
	struct isl_set *set = NULL;
	struct isl_basic_map *model = NULL;
	struct isl_basic_map *hull;
	struct isl_basic_map *affine_hull;
	struct isl_basic_set *bset = NULL;
	isl_map *input;

	if (!map || map->n <= 1)
		return map_simple_hull_trivial(map);

	if (map->cached_simple_hull[shift])
		return cached_simple_hull(map, shift);

	map = isl_map_detect_equalities(map);
	if (!map || map->n <= 1)
		return map_simple_hull_trivial(map);
	affine_hull = isl_map_affine_hull(isl_map_copy(map));
	input = isl_map_copy(map);
	map = isl_map_align_divs_internal(map);
	model = map ? isl_basic_map_copy(map->p[0]) : NULL;

	set = isl_map_underlying_set(map);

	bset = uset_simple_hull(set, shift);

	hull = isl_basic_map_overlying_set(bset, model);

	hull = isl_basic_map_intersect(hull, affine_hull);
	hull = isl_basic_map_remove_redundancies(hull);

	if (hull) {
		ISL_F_SET(hull, ISL_BASIC_MAP_NO_IMPLICIT);
		ISL_F_SET(hull, ISL_BASIC_MAP_ALL_EQUALITIES);
	}

	hull = isl_basic_map_finalize(hull);
	if (input)
		input->cached_simple_hull[shift] = isl_basic_map_copy(hull);
	isl_map_free(input);

	return hull;
}

/* Compute a superset of the convex hull of map that is described
 * by only translates of the constraints in the constituents of map.
 */
__isl_give isl_basic_map *isl_map_simple_hull(__isl_take isl_map *map)
{
	return map_simple_hull(map, 1);
}

struct isl_basic_set *isl_set_simple_hull(struct isl_set *set)
{
	return bset_from_bmap(isl_map_simple_hull(set_to_map(set)));
}

/* Compute a superset of the convex hull of map that is described
 * by only the constraints in the constituents of map.
 */
__isl_give isl_basic_map *isl_map_unshifted_simple_hull(
	__isl_take isl_map *map)
{
	return map_simple_hull(map, 0);
}

__isl_give isl_basic_set *isl_set_unshifted_simple_hull(
	__isl_take isl_set *set)
{
	return isl_map_unshifted_simple_hull(set);
}

/* Drop all inequalities from "bmap1" that do not also appear in "bmap2".
 * A constraint that appears with different constant terms
 * in "bmap1" and "bmap2" is also kept, with the least restrictive
 * (i.e., greatest) constant term.
 * "bmap1" and "bmap2" are assumed to have the same (known)
 * integer divisions.
 * The constraints of both "bmap1" and "bmap2" are assumed
 * to have been sorted using isl_basic_map_sort_constraints.
 *
 * Run through the inequality constraints of "bmap1" and "bmap2"
 * in sorted order.
 * Each constraint of "bmap1" without a matching constraint in "bmap2"
 * is removed.
 * If a match is found, the constraint is kept.  If needed, the constant
 * term of the constraint is adjusted.
 */
static __isl_give isl_basic_map *select_shared_inequalities(
	__isl_take isl_basic_map *bmap1, __isl_keep isl_basic_map *bmap2)
{
	int i1, i2;

	bmap1 = isl_basic_map_cow(bmap1);
	if (!bmap1 || !bmap2)
		return isl_basic_map_free(bmap1);

	i1 = bmap1->n_ineq - 1;
	i2 = bmap2->n_ineq - 1;
	while (bmap1 && i1 >= 0 && i2 >= 0) {
		int cmp;

		cmp = isl_basic_map_constraint_cmp(bmap1, bmap1->ineq[i1],
							bmap2->ineq[i2]);
		if (cmp < 0) {
			--i2;
			continue;
		}
		if (cmp > 0) {
			if (isl_basic_map_drop_inequality(bmap1, i1) < 0)
				bmap1 = isl_basic_map_free(bmap1);
			--i1;
			continue;
		}
		if (isl_int_lt(bmap1->ineq[i1][0], bmap2->ineq[i2][0]))
			isl_int_set(bmap1->ineq[i1][0], bmap2->ineq[i2][0]);
		--i1;
		--i2;
	}
	for (; i1 >= 0; --i1)
		if (isl_basic_map_drop_inequality(bmap1, i1) < 0)
			bmap1 = isl_basic_map_free(bmap1);

	return bmap1;
}

/* Drop all equalities from "bmap1" that do not also appear in "bmap2".
 * "bmap1" and "bmap2" are assumed to have the same (known)
 * integer divisions.
 *
 * Run through the equality constraints of "bmap1" and "bmap2".
 * Each constraint of "bmap1" without a matching constraint in "bmap2"
 * is removed.
 */
static __isl_give isl_basic_map *select_shared_equalities(
	__isl_take isl_basic_map *bmap1, __isl_keep isl_basic_map *bmap2)
{
	int i1, i2;
	unsigned total;

	bmap1 = isl_basic_map_cow(bmap1);
	if (!bmap1 || !bmap2)
		return isl_basic_map_free(bmap1);

	total = isl_basic_map_total_dim(bmap1);

	i1 = bmap1->n_eq - 1;
	i2 = bmap2->n_eq - 1;
	while (bmap1 && i1 >= 0 && i2 >= 0) {
		int last1, last2;

		last1 = isl_seq_last_non_zero(bmap1->eq[i1] + 1, total);
		last2 = isl_seq_last_non_zero(bmap2->eq[i2] + 1, total);
		if (last1 > last2) {
			--i2;
			continue;
		}
		if (last1 < last2) {
			if (isl_basic_map_drop_equality(bmap1, i1) < 0)
				bmap1 = isl_basic_map_free(bmap1);
			--i1;
			continue;
		}
		if (!isl_seq_eq(bmap1->eq[i1], bmap2->eq[i2], 1 + total)) {
			if (isl_basic_map_drop_equality(bmap1, i1) < 0)
				bmap1 = isl_basic_map_free(bmap1);
		}
		--i1;
		--i2;
	}
	for (; i1 >= 0; --i1)
		if (isl_basic_map_drop_equality(bmap1, i1) < 0)
			bmap1 = isl_basic_map_free(bmap1);

	return bmap1;
}

/* Compute a superset of "bmap1" and "bmap2" that is described
 * by only the constraints that appear in both "bmap1" and "bmap2".
 *
 * First drop constraints that involve unknown integer divisions
 * since it is not trivial to check whether two such integer divisions
 * in different basic maps are the same.
 * Then align the remaining (known) divs and sort the constraints.
 * Finally drop all inequalities and equalities from "bmap1" that
 * do not also appear in "bmap2".
 */
__isl_give isl_basic_map *isl_basic_map_plain_unshifted_simple_hull(
	__isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
{
	bmap1 = isl_basic_map_drop_constraint_involving_unknown_divs(bmap1);
	bmap2 = isl_basic_map_drop_constraint_involving_unknown_divs(bmap2);
	bmap2 = isl_basic_map_align_divs(bmap2, bmap1);
	bmap1 = isl_basic_map_align_divs(bmap1, bmap2);
	bmap1 = isl_basic_map_gauss(bmap1, NULL);
	bmap2 = isl_basic_map_gauss(bmap2, NULL);
	bmap1 = isl_basic_map_sort_constraints(bmap1);
	bmap2 = isl_basic_map_sort_constraints(bmap2);

	bmap1 = select_shared_inequalities(bmap1, bmap2);
	bmap1 = select_shared_equalities(bmap1, bmap2);

	isl_basic_map_free(bmap2);
	bmap1 = isl_basic_map_finalize(bmap1);
	return bmap1;
}

/* Compute a superset of the convex hull of "map" that is described
 * by only the constraints in the constituents of "map".
 * In particular, the result is composed of constraints that appear
 * in each of the basic maps of "map"
 *
 * Constraints that involve unknown integer divisions are dropped
 * since it is not trivial to check whether two such integer divisions
 * in different basic maps are the same.
 *
 * The hull is initialized from the first basic map and then
 * updated with respect to the other basic maps in turn.
 */
__isl_give isl_basic_map *isl_map_plain_unshifted_simple_hull(
	__isl_take isl_map *map)
{
	int i;
	isl_basic_map *hull;

	if (!map)
		return NULL;
	if (map->n <= 1)
		return map_simple_hull_trivial(map);
	map = isl_map_drop_constraint_involving_unknown_divs(map);
	hull = isl_basic_map_copy(map->p[0]);
	for (i = 1; i < map->n; ++i) {
		isl_basic_map *bmap_i;

		bmap_i = isl_basic_map_copy(map->p[i]);
		hull = isl_basic_map_plain_unshifted_simple_hull(hull, bmap_i);
	}

	isl_map_free(map);
	return hull;
}

/* Compute a superset of the convex hull of "set" that is described
 * by only the constraints in the constituents of "set".
 * In particular, the result is composed of constraints that appear
 * in each of the basic sets of "set"
 */
__isl_give isl_basic_set *isl_set_plain_unshifted_simple_hull(
	__isl_take isl_set *set)
{
	return isl_map_plain_unshifted_simple_hull(set);
}

/* Check if "ineq" is a bound on "set" and, if so, add it to "hull".
 *
 * For each basic set in "set", we first check if the basic set
 * contains a translate of "ineq".  If this translate is more relaxed,
 * then we assume that "ineq" is not a bound on this basic set.
 * Otherwise, we know that it is a bound.
 * If the basic set does not contain a translate of "ineq", then
 * we call is_bound to perform the test.
 */
static __isl_give isl_basic_set *add_bound_from_constraint(
	__isl_take isl_basic_set *hull, struct sh_data *data,
	__isl_keep isl_set *set, isl_int *ineq)
{
	int i, k;
	isl_ctx *ctx;
	uint32_t c_hash;
	struct ineq_cmp_data v;

	if (!hull || !set)
		return isl_basic_set_free(hull);

	v.len = isl_basic_set_total_dim(hull);
	v.p = ineq;
	c_hash = isl_seq_get_hash(ineq + 1, v.len);

	ctx = isl_basic_set_get_ctx(hull);
	for (i = 0; i < set->n; ++i) {
		int bound;
		struct isl_hash_table_entry *entry;

		entry = isl_hash_table_find(ctx, data->p[i].table,
						c_hash, &has_ineq, &v, 0);
		if (entry) {
			isl_int *ineq_i = entry->data;
			int neg, more_relaxed;

			neg = isl_seq_is_neg(ineq_i + 1, ineq + 1, v.len);
			if (neg)
				isl_int_neg(ineq_i[0], ineq_i[0]);
			more_relaxed = isl_int_gt(ineq_i[0], ineq[0]);
			if (neg)
				isl_int_neg(ineq_i[0], ineq_i[0]);
			if (more_relaxed)
				break;
			else
				continue;
		}
		bound = is_bound(data, set, i, ineq, 0);
		if (bound < 0)
			return isl_basic_set_free(hull);
		if (!bound)
			break;
	}
	if (i < set->n)
		return hull;

	k = isl_basic_set_alloc_inequality(hull);
	if (k < 0)
		return isl_basic_set_free(hull);
	isl_seq_cpy(hull->ineq[k], ineq, 1 + v.len);

	return hull;
}

/* Compute a superset of the convex hull of "set" that is described
 * by only some of the "n_ineq" constraints in the list "ineq", where "set"
 * has no parameters or integer divisions.
 *
 * The inequalities in "ineq" are assumed to have been sorted such
 * that constraints with the same linear part appear together and
 * that among constraints with the same linear part, those with
 * smaller constant term appear first.
 *
 * We reuse the same data structure that is used by uset_simple_hull,
 * but we do not need the hull table since we will not consider the
 * same constraint more than once.  We therefore allocate it with zero size.
 *
 * We run through the constraints and try to add them one by one,
 * skipping identical constraints.  If we have added a constraint and
 * the next constraint is a more relaxed translate, then we skip this
 * next constraint as well.
 */
static __isl_give isl_basic_set *uset_unshifted_simple_hull_from_constraints(
	__isl_take isl_set *set, int n_ineq, isl_int **ineq)
{
	int i;
	int last_added = 0;
	struct sh_data *data = NULL;
	isl_basic_set *hull = NULL;
	unsigned dim;

	hull = isl_basic_set_alloc_space(isl_set_get_space(set), 0, 0, n_ineq);
	if (!hull)
		goto error;

	data = sh_data_alloc(set, 0);
	if (!data)
		goto error;

	dim = isl_set_dim(set, isl_dim_set);
	for (i = 0; i < n_ineq; ++i) {
		int hull_n_ineq = hull->n_ineq;
		int parallel;

		parallel = i > 0 && isl_seq_eq(ineq[i - 1] + 1, ineq[i] + 1,
						dim);
		if (parallel &&
		    (last_added || isl_int_eq(ineq[i - 1][0], ineq[i][0])))
			continue;
		hull = add_bound_from_constraint(hull, data, set, ineq[i]);
		if (!hull)
			goto error;
		last_added = hull->n_ineq > hull_n_ineq;
	}

	sh_data_free(data);
	isl_set_free(set);
	return hull;
error:
	sh_data_free(data);
	isl_set_free(set);
	isl_basic_set_free(hull);
	return NULL;
}

/* Collect pointers to all the inequalities in the elements of "list"
 * in "ineq".  For equalities, store both a pointer to the equality and
 * a pointer to its opposite, which is first copied to "mat".
 * "ineq" and "mat" are assumed to have been preallocated to the right size
 * (the number of inequalities + 2 times the number of equalites and
 * the number of equalities, respectively).
 */
static __isl_give isl_mat *collect_inequalities(__isl_take isl_mat *mat,
	__isl_keep isl_basic_set_list *list, isl_int **ineq)
{
	int i, j, n, n_eq, n_ineq;

	if (!mat)
		return NULL;

	n_eq = 0;
	n_ineq = 0;
	n = isl_basic_set_list_n_basic_set(list);
	for (i = 0; i < n; ++i) {
		isl_basic_set *bset;
		bset = isl_basic_set_list_get_basic_set(list, i);
		if (!bset)
			return isl_mat_free(mat);
		for (j = 0; j < bset->n_eq; ++j) {
			ineq[n_ineq++] = mat->row[n_eq];
			ineq[n_ineq++] = bset->eq[j];
			isl_seq_neg(mat->row[n_eq++], bset->eq[j], mat->n_col);
		}
		for (j = 0; j < bset->n_ineq; ++j)
			ineq[n_ineq++] = bset->ineq[j];
		isl_basic_set_free(bset);
	}

	return mat;
}

/* Comparison routine for use as an isl_sort callback.
 *
 * Constraints with the same linear part are sorted together and
 * among constraints with the same linear part, those with smaller
 * constant term are sorted first.
 */
static int cmp_ineq(const void *a, const void *b, void *arg)
{
	unsigned dim = *(unsigned *) arg;
	isl_int * const *ineq1 = a;
	isl_int * const *ineq2 = b;
	int cmp;

	cmp = isl_seq_cmp((*ineq1) + 1, (*ineq2) + 1, dim);
	if (cmp != 0)
		return cmp;
	return isl_int_cmp((*ineq1)[0], (*ineq2)[0]);
}

/* Compute a superset of the convex hull of "set" that is described
 * by only constraints in the elements of "list", where "set" has
 * no parameters or integer divisions.
 *
 * We collect all the constraints in those elements and then
 * sort the constraints such that constraints with the same linear part
 * are sorted together and that those with smaller constant term are
 * sorted first.
 */
static __isl_give isl_basic_set *uset_unshifted_simple_hull_from_basic_set_list(
	__isl_take isl_set *set, __isl_take isl_basic_set_list *list)
{
	int i, n, n_eq, n_ineq;
	unsigned dim;
	isl_ctx *ctx;
	isl_mat *mat = NULL;
	isl_int **ineq = NULL;
	isl_basic_set *hull;

	if (!set)
		goto error;
	ctx = isl_set_get_ctx(set);

	n_eq = 0;
	n_ineq = 0;
	n = isl_basic_set_list_n_basic_set(list);
	for (i = 0; i < n; ++i) {
		isl_basic_set *bset;
		bset = isl_basic_set_list_get_basic_set(list, i);
		if (!bset)
			goto error;
		n_eq += bset->n_eq;
		n_ineq += 2 * bset->n_eq + bset->n_ineq;
		isl_basic_set_free(bset);
	}

	ineq = isl_alloc_array(ctx, isl_int *, n_ineq);
	if (n_ineq > 0 && !ineq)
		goto error;

	dim = isl_set_dim(set, isl_dim_set);
	mat = isl_mat_alloc(ctx, n_eq, 1 + dim);
	mat = collect_inequalities(mat, list, ineq);
	if (!mat)
		goto error;

	if (isl_sort(ineq, n_ineq, sizeof(ineq[0]), &cmp_ineq, &dim) < 0)
		goto error;

	hull = uset_unshifted_simple_hull_from_constraints(set, n_ineq, ineq);

	isl_mat_free(mat);
	free(ineq);
	isl_basic_set_list_free(list);
	return hull;
error:
	isl_mat_free(mat);
	free(ineq);
	isl_set_free(set);
	isl_basic_set_list_free(list);
	return NULL;
}

/* Compute a superset of the convex hull of "map" that is described
 * by only constraints in the elements of "list".
 *
 * If the list is empty, then we can only describe the universe set.
 * If the input map is empty, then all constraints are valid, so
 * we return the intersection of the elements in "list".
 *
 * Otherwise, we align all divs and temporarily treat them
 * as regular variables, computing the unshifted simple hull in
 * uset_unshifted_simple_hull_from_basic_set_list.
 */
static __isl_give isl_basic_map *map_unshifted_simple_hull_from_basic_map_list(
	__isl_take isl_map *map, __isl_take isl_basic_map_list *list)
{
	isl_basic_map *model;
	isl_basic_map *hull;
	isl_set *set;
	isl_basic_set_list *bset_list;

	if (!map || !list)
		goto error;

	if (isl_basic_map_list_n_basic_map(list) == 0) {
		isl_space *space;

		space = isl_map_get_space(map);
		isl_map_free(map);
		isl_basic_map_list_free(list);
		return isl_basic_map_universe(space);
	}
	if (isl_map_plain_is_empty(map)) {
		isl_map_free(map);
		return isl_basic_map_list_intersect(list);
	}

	map = isl_map_align_divs_to_basic_map_list(map, list);
	if (!map)
		goto error;
	list = isl_basic_map_list_align_divs_to_basic_map(list, map->p[0]);

	model = isl_basic_map_list_get_basic_map(list, 0);

	set = isl_map_underlying_set(map);
	bset_list = isl_basic_map_list_underlying_set(list);

	hull = uset_unshifted_simple_hull_from_basic_set_list(set, bset_list);
	hull = isl_basic_map_overlying_set(hull, model);

	return hull;
error:
	isl_map_free(map);
	isl_basic_map_list_free(list);
	return NULL;
}

/* Return a sequence of the basic maps that make up the maps in "list".
 */
static __isl_give isl_basic_map_list *collect_basic_maps(
	__isl_take isl_map_list *list)
{
	int i, n;
	isl_ctx *ctx;
	isl_basic_map_list *bmap_list;

	if (!list)
		return NULL;
	n = isl_map_list_n_map(list);
	ctx = isl_map_list_get_ctx(list);
	bmap_list = isl_basic_map_list_alloc(ctx, 0);

	for (i = 0; i < n; ++i) {
		isl_map *map;
		isl_basic_map_list *list_i;

		map = isl_map_list_get_map(list, i);
		map = isl_map_compute_divs(map);
		list_i = isl_map_get_basic_map_list(map);
		isl_map_free(map);
		bmap_list = isl_basic_map_list_concat(bmap_list, list_i);
	}

	isl_map_list_free(list);
	return bmap_list;
}

/* Compute a superset of the convex hull of "map" that is described
 * by only constraints in the elements of "list".
 *
 * If "map" is the universe, then the convex hull (and therefore
 * any superset of the convexhull) is the universe as well.
 *
 * Otherwise, we collect all the basic maps in the map list and
 * continue with map_unshifted_simple_hull_from_basic_map_list.
 */
__isl_give isl_basic_map *isl_map_unshifted_simple_hull_from_map_list(
	__isl_take isl_map *map, __isl_take isl_map_list *list)
{
	isl_basic_map_list *bmap_list;
	int is_universe;

	is_universe = isl_map_plain_is_universe(map);
	if (is_universe < 0)
		map = isl_map_free(map);
	if (is_universe < 0 || is_universe) {
		isl_map_list_free(list);
		return isl_map_unshifted_simple_hull(map);
	}

	bmap_list = collect_basic_maps(list);
	return map_unshifted_simple_hull_from_basic_map_list(map, bmap_list);
}

/* Compute a superset of the convex hull of "set" that is described
 * by only constraints in the elements of "list".
 */
__isl_give isl_basic_set *isl_set_unshifted_simple_hull_from_set_list(
	__isl_take isl_set *set, __isl_take isl_set_list *list)
{
	return isl_map_unshifted_simple_hull_from_map_list(set, list);
}

/* Given a set "set", return parametric bounds on the dimension "dim".
 */
static struct isl_basic_set *set_bounds(struct isl_set *set, int dim)
{
	unsigned set_dim = isl_set_dim(set, isl_dim_set);
	set = isl_set_copy(set);
	set = isl_set_eliminate_dims(set, dim + 1, set_dim - (dim + 1));
	set = isl_set_eliminate_dims(set, 0, dim);
	return isl_set_convex_hull(set);
}

/* Computes a "simple hull" and then check if each dimension in the
 * resulting hull is bounded by a symbolic constant.  If not, the
 * hull is intersected with the corresponding bounds on the whole set.
 */
__isl_give isl_basic_set *isl_set_bounded_simple_hull(__isl_take isl_set *set)
{
	int i, j;
	struct isl_basic_set *hull;
	unsigned nparam, left;
	int removed_divs = 0;

	hull = isl_set_simple_hull(isl_set_copy(set));
	if (!hull)
		goto error;

	nparam = isl_basic_set_dim(hull, isl_dim_param);
	for (i = 0; i < isl_basic_set_dim(hull, isl_dim_set); ++i) {
		int lower = 0, upper = 0;
		struct isl_basic_set *bounds;

		left = isl_basic_set_total_dim(hull) - nparam - i - 1;
		for (j = 0; j < hull->n_eq; ++j) {
			if (isl_int_is_zero(hull->eq[j][1 + nparam + i]))
				continue;
			if (isl_seq_first_non_zero(hull->eq[j]+1+nparam+i+1,
						    left) == -1)
				break;
		}
		if (j < hull->n_eq)
			continue;

		for (j = 0; j < hull->n_ineq; ++j) {
			if (isl_int_is_zero(hull->ineq[j][1 + nparam + i]))
				continue;
			if (isl_seq_first_non_zero(hull->ineq[j]+1+nparam+i+1,
						    left) != -1 ||
			    isl_seq_first_non_zero(hull->ineq[j]+1+nparam,
						    i) != -1)
				continue;
			if (isl_int_is_pos(hull->ineq[j][1 + nparam + i]))
				lower = 1;
			else
				upper = 1;
			if (lower && upper)
				break;
		}

		if (lower && upper)
			continue;

		if (!removed_divs) {
			set = isl_set_remove_divs(set);
			if (!set)
				goto error;
			removed_divs = 1;
		}
		bounds = set_bounds(set, i);
		hull = isl_basic_set_intersect(hull, bounds);
		if (!hull)
			goto error;
	}

	isl_set_free(set);
	return hull;
error:
	isl_set_free(set);
	isl_basic_set_free(hull);
	return NULL;
}