1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
|
/*
* Copyright 2008-2009 Katholieke Universiteit Leuven
* Copyright 2013 Ecole Normale Superieure
* Copyright 2014 INRIA Rocquencourt
* Copyright 2016 Sven Verdoolaege
*
* 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 Ecole Normale Superieure, 45 rue d'Ulm, 75230 Paris, France
* and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
* B.P. 105 - 78153 Le Chesnay, France
*/
#include <isl_ctx_private.h>
#include <isl_mat_private.h>
#include <isl_vec_private.h>
#include "isl_map_private.h"
#include "isl_tab.h"
#include <isl_seq.h>
#include <isl_config.h>
#include <bset_to_bmap.c>
#include <bset_from_bmap.c>
/*
* The implementation of tableaus in this file was inspired by Section 8
* of David Detlefs, Greg Nelson and James B. Saxe, "Simplify: a theorem
* prover for program checking".
*/
struct isl_tab *isl_tab_alloc(struct isl_ctx *ctx,
unsigned n_row, unsigned n_var, unsigned M)
{
int i;
struct isl_tab *tab;
unsigned off = 2 + M;
tab = isl_calloc_type(ctx, struct isl_tab);
if (!tab)
return NULL;
tab->mat = isl_mat_alloc(ctx, n_row, off + n_var);
if (!tab->mat)
goto error;
tab->var = isl_alloc_array(ctx, struct isl_tab_var, n_var);
if (n_var && !tab->var)
goto error;
tab->con = isl_alloc_array(ctx, struct isl_tab_var, n_row);
if (n_row && !tab->con)
goto error;
tab->col_var = isl_alloc_array(ctx, int, n_var);
if (n_var && !tab->col_var)
goto error;
tab->row_var = isl_alloc_array(ctx, int, n_row);
if (n_row && !tab->row_var)
goto error;
for (i = 0; i < n_var; ++i) {
tab->var[i].index = i;
tab->var[i].is_row = 0;
tab->var[i].is_nonneg = 0;
tab->var[i].is_zero = 0;
tab->var[i].is_redundant = 0;
tab->var[i].frozen = 0;
tab->var[i].negated = 0;
tab->col_var[i] = i;
}
tab->n_row = 0;
tab->n_con = 0;
tab->n_eq = 0;
tab->max_con = n_row;
tab->n_col = n_var;
tab->n_var = n_var;
tab->max_var = n_var;
tab->n_param = 0;
tab->n_div = 0;
tab->n_dead = 0;
tab->n_redundant = 0;
tab->strict_redundant = 0;
tab->need_undo = 0;
tab->rational = 0;
tab->empty = 0;
tab->in_undo = 0;
tab->M = M;
tab->cone = 0;
tab->bottom.type = isl_tab_undo_bottom;
tab->bottom.next = NULL;
tab->top = &tab->bottom;
tab->n_zero = 0;
tab->n_unbounded = 0;
tab->basis = NULL;
return tab;
error:
isl_tab_free(tab);
return NULL;
}
isl_ctx *isl_tab_get_ctx(struct isl_tab *tab)
{
return tab ? isl_mat_get_ctx(tab->mat) : NULL;
}
int isl_tab_extend_cons(struct isl_tab *tab, unsigned n_new)
{
unsigned off;
if (!tab)
return -1;
off = 2 + tab->M;
if (tab->max_con < tab->n_con + n_new) {
struct isl_tab_var *con;
con = isl_realloc_array(tab->mat->ctx, tab->con,
struct isl_tab_var, tab->max_con + n_new);
if (!con)
return -1;
tab->con = con;
tab->max_con += n_new;
}
if (tab->mat->n_row < tab->n_row + n_new) {
int *row_var;
tab->mat = isl_mat_extend(tab->mat,
tab->n_row + n_new, off + tab->n_col);
if (!tab->mat)
return -1;
row_var = isl_realloc_array(tab->mat->ctx, tab->row_var,
int, tab->mat->n_row);
if (!row_var)
return -1;
tab->row_var = row_var;
if (tab->row_sign) {
enum isl_tab_row_sign *s;
s = isl_realloc_array(tab->mat->ctx, tab->row_sign,
enum isl_tab_row_sign, tab->mat->n_row);
if (!s)
return -1;
tab->row_sign = s;
}
}
return 0;
}
/* Make room for at least n_new extra variables.
* Return -1 if anything went wrong.
*/
int isl_tab_extend_vars(struct isl_tab *tab, unsigned n_new)
{
struct isl_tab_var *var;
unsigned off = 2 + tab->M;
if (tab->max_var < tab->n_var + n_new) {
var = isl_realloc_array(tab->mat->ctx, tab->var,
struct isl_tab_var, tab->n_var + n_new);
if (!var)
return -1;
tab->var = var;
tab->max_var = tab->n_var + n_new;
}
if (tab->mat->n_col < off + tab->n_col + n_new) {
int *p;
tab->mat = isl_mat_extend(tab->mat,
tab->mat->n_row, off + tab->n_col + n_new);
if (!tab->mat)
return -1;
p = isl_realloc_array(tab->mat->ctx, tab->col_var,
int, tab->n_col + n_new);
if (!p)
return -1;
tab->col_var = p;
}
return 0;
}
static void free_undo_record(struct isl_tab_undo *undo)
{
switch (undo->type) {
case isl_tab_undo_saved_basis:
free(undo->u.col_var);
break;
default:;
}
free(undo);
}
static void free_undo(struct isl_tab *tab)
{
struct isl_tab_undo *undo, *next;
for (undo = tab->top; undo && undo != &tab->bottom; undo = next) {
next = undo->next;
free_undo_record(undo);
}
tab->top = undo;
}
void isl_tab_free(struct isl_tab *tab)
{
if (!tab)
return;
free_undo(tab);
isl_mat_free(tab->mat);
isl_vec_free(tab->dual);
isl_basic_map_free(tab->bmap);
free(tab->var);
free(tab->con);
free(tab->row_var);
free(tab->col_var);
free(tab->row_sign);
isl_mat_free(tab->samples);
free(tab->sample_index);
isl_mat_free(tab->basis);
free(tab);
}
struct isl_tab *isl_tab_dup(struct isl_tab *tab)
{
int i;
struct isl_tab *dup;
unsigned off;
if (!tab)
return NULL;
off = 2 + tab->M;
dup = isl_calloc_type(tab->mat->ctx, struct isl_tab);
if (!dup)
return NULL;
dup->mat = isl_mat_dup(tab->mat);
if (!dup->mat)
goto error;
dup->var = isl_alloc_array(tab->mat->ctx, struct isl_tab_var, tab->max_var);
if (tab->max_var && !dup->var)
goto error;
for (i = 0; i < tab->n_var; ++i)
dup->var[i] = tab->var[i];
dup->con = isl_alloc_array(tab->mat->ctx, struct isl_tab_var, tab->max_con);
if (tab->max_con && !dup->con)
goto error;
for (i = 0; i < tab->n_con; ++i)
dup->con[i] = tab->con[i];
dup->col_var = isl_alloc_array(tab->mat->ctx, int, tab->mat->n_col - off);
if ((tab->mat->n_col - off) && !dup->col_var)
goto error;
for (i = 0; i < tab->n_col; ++i)
dup->col_var[i] = tab->col_var[i];
dup->row_var = isl_alloc_array(tab->mat->ctx, int, tab->mat->n_row);
if (tab->mat->n_row && !dup->row_var)
goto error;
for (i = 0; i < tab->n_row; ++i)
dup->row_var[i] = tab->row_var[i];
if (tab->row_sign) {
dup->row_sign = isl_alloc_array(tab->mat->ctx, enum isl_tab_row_sign,
tab->mat->n_row);
if (tab->mat->n_row && !dup->row_sign)
goto error;
for (i = 0; i < tab->n_row; ++i)
dup->row_sign[i] = tab->row_sign[i];
}
if (tab->samples) {
dup->samples = isl_mat_dup(tab->samples);
if (!dup->samples)
goto error;
dup->sample_index = isl_alloc_array(tab->mat->ctx, int,
tab->samples->n_row);
if (tab->samples->n_row && !dup->sample_index)
goto error;
dup->n_sample = tab->n_sample;
dup->n_outside = tab->n_outside;
}
dup->n_row = tab->n_row;
dup->n_con = tab->n_con;
dup->n_eq = tab->n_eq;
dup->max_con = tab->max_con;
dup->n_col = tab->n_col;
dup->n_var = tab->n_var;
dup->max_var = tab->max_var;
dup->n_param = tab->n_param;
dup->n_div = tab->n_div;
dup->n_dead = tab->n_dead;
dup->n_redundant = tab->n_redundant;
dup->rational = tab->rational;
dup->empty = tab->empty;
dup->strict_redundant = 0;
dup->need_undo = 0;
dup->in_undo = 0;
dup->M = tab->M;
dup->cone = tab->cone;
dup->bottom.type = isl_tab_undo_bottom;
dup->bottom.next = NULL;
dup->top = &dup->bottom;
dup->n_zero = tab->n_zero;
dup->n_unbounded = tab->n_unbounded;
dup->basis = isl_mat_dup(tab->basis);
return dup;
error:
isl_tab_free(dup);
return NULL;
}
/* Construct the coefficient matrix of the product tableau
* of two tableaus.
* mat{1,2} is the coefficient matrix of tableau {1,2}
* row{1,2} is the number of rows in tableau {1,2}
* col{1,2} is the number of columns in tableau {1,2}
* off is the offset to the coefficient column (skipping the
* denominator, the constant term and the big parameter if any)
* r{1,2} is the number of redundant rows in tableau {1,2}
* d{1,2} is the number of dead columns in tableau {1,2}
*
* The order of the rows and columns in the result is as explained
* in isl_tab_product.
*/
static __isl_give isl_mat *tab_mat_product(__isl_keep isl_mat *mat1,
__isl_keep isl_mat *mat2, unsigned row1, unsigned row2,
unsigned col1, unsigned col2,
unsigned off, unsigned r1, unsigned r2, unsigned d1, unsigned d2)
{
int i;
struct isl_mat *prod;
unsigned n;
prod = isl_mat_alloc(mat1->ctx, mat1->n_row + mat2->n_row,
off + col1 + col2);
if (!prod)
return NULL;
n = 0;
for (i = 0; i < r1; ++i) {
isl_seq_cpy(prod->row[n + i], mat1->row[i], off + d1);
isl_seq_clr(prod->row[n + i] + off + d1, d2);
isl_seq_cpy(prod->row[n + i] + off + d1 + d2,
mat1->row[i] + off + d1, col1 - d1);
isl_seq_clr(prod->row[n + i] + off + col1 + d1, col2 - d2);
}
n += r1;
for (i = 0; i < r2; ++i) {
isl_seq_cpy(prod->row[n + i], mat2->row[i], off);
isl_seq_clr(prod->row[n + i] + off, d1);
isl_seq_cpy(prod->row[n + i] + off + d1,
mat2->row[i] + off, d2);
isl_seq_clr(prod->row[n + i] + off + d1 + d2, col1 - d1);
isl_seq_cpy(prod->row[n + i] + off + col1 + d1,
mat2->row[i] + off + d2, col2 - d2);
}
n += r2;
for (i = 0; i < row1 - r1; ++i) {
isl_seq_cpy(prod->row[n + i], mat1->row[r1 + i], off + d1);
isl_seq_clr(prod->row[n + i] + off + d1, d2);
isl_seq_cpy(prod->row[n + i] + off + d1 + d2,
mat1->row[r1 + i] + off + d1, col1 - d1);
isl_seq_clr(prod->row[n + i] + off + col1 + d1, col2 - d2);
}
n += row1 - r1;
for (i = 0; i < row2 - r2; ++i) {
isl_seq_cpy(prod->row[n + i], mat2->row[r2 + i], off);
isl_seq_clr(prod->row[n + i] + off, d1);
isl_seq_cpy(prod->row[n + i] + off + d1,
mat2->row[r2 + i] + off, d2);
isl_seq_clr(prod->row[n + i] + off + d1 + d2, col1 - d1);
isl_seq_cpy(prod->row[n + i] + off + col1 + d1,
mat2->row[r2 + i] + off + d2, col2 - d2);
}
return prod;
}
/* Update the row or column index of a variable that corresponds
* to a variable in the first input tableau.
*/
static void update_index1(struct isl_tab_var *var,
unsigned r1, unsigned r2, unsigned d1, unsigned d2)
{
if (var->index == -1)
return;
if (var->is_row && var->index >= r1)
var->index += r2;
if (!var->is_row && var->index >= d1)
var->index += d2;
}
/* Update the row or column index of a variable that corresponds
* to a variable in the second input tableau.
*/
static void update_index2(struct isl_tab_var *var,
unsigned row1, unsigned col1,
unsigned r1, unsigned r2, unsigned d1, unsigned d2)
{
if (var->index == -1)
return;
if (var->is_row) {
if (var->index < r2)
var->index += r1;
else
var->index += row1;
} else {
if (var->index < d2)
var->index += d1;
else
var->index += col1;
}
}
/* Create a tableau that represents the Cartesian product of the sets
* represented by tableaus tab1 and tab2.
* The order of the rows in the product is
* - redundant rows of tab1
* - redundant rows of tab2
* - non-redundant rows of tab1
* - non-redundant rows of tab2
* The order of the columns is
* - denominator
* - constant term
* - coefficient of big parameter, if any
* - dead columns of tab1
* - dead columns of tab2
* - live columns of tab1
* - live columns of tab2
* The order of the variables and the constraints is a concatenation
* of order in the two input tableaus.
*/
struct isl_tab *isl_tab_product(struct isl_tab *tab1, struct isl_tab *tab2)
{
int i;
struct isl_tab *prod;
unsigned off;
unsigned r1, r2, d1, d2;
if (!tab1 || !tab2)
return NULL;
isl_assert(tab1->mat->ctx, tab1->M == tab2->M, return NULL);
isl_assert(tab1->mat->ctx, tab1->rational == tab2->rational, return NULL);
isl_assert(tab1->mat->ctx, tab1->cone == tab2->cone, return NULL);
isl_assert(tab1->mat->ctx, !tab1->row_sign, return NULL);
isl_assert(tab1->mat->ctx, !tab2->row_sign, return NULL);
isl_assert(tab1->mat->ctx, tab1->n_param == 0, return NULL);
isl_assert(tab1->mat->ctx, tab2->n_param == 0, return NULL);
isl_assert(tab1->mat->ctx, tab1->n_div == 0, return NULL);
isl_assert(tab1->mat->ctx, tab2->n_div == 0, return NULL);
off = 2 + tab1->M;
r1 = tab1->n_redundant;
r2 = tab2->n_redundant;
d1 = tab1->n_dead;
d2 = tab2->n_dead;
prod = isl_calloc_type(tab1->mat->ctx, struct isl_tab);
if (!prod)
return NULL;
prod->mat = tab_mat_product(tab1->mat, tab2->mat,
tab1->n_row, tab2->n_row,
tab1->n_col, tab2->n_col, off, r1, r2, d1, d2);
if (!prod->mat)
goto error;
prod->var = isl_alloc_array(tab1->mat->ctx, struct isl_tab_var,
tab1->max_var + tab2->max_var);
if ((tab1->max_var + tab2->max_var) && !prod->var)
goto error;
for (i = 0; i < tab1->n_var; ++i) {
prod->var[i] = tab1->var[i];
update_index1(&prod->var[i], r1, r2, d1, d2);
}
for (i = 0; i < tab2->n_var; ++i) {
prod->var[tab1->n_var + i] = tab2->var[i];
update_index2(&prod->var[tab1->n_var + i],
tab1->n_row, tab1->n_col,
r1, r2, d1, d2);
}
prod->con = isl_alloc_array(tab1->mat->ctx, struct isl_tab_var,
tab1->max_con + tab2->max_con);
if ((tab1->max_con + tab2->max_con) && !prod->con)
goto error;
for (i = 0; i < tab1->n_con; ++i) {
prod->con[i] = tab1->con[i];
update_index1(&prod->con[i], r1, r2, d1, d2);
}
for (i = 0; i < tab2->n_con; ++i) {
prod->con[tab1->n_con + i] = tab2->con[i];
update_index2(&prod->con[tab1->n_con + i],
tab1->n_row, tab1->n_col,
r1, r2, d1, d2);
}
prod->col_var = isl_alloc_array(tab1->mat->ctx, int,
tab1->n_col + tab2->n_col);
if ((tab1->n_col + tab2->n_col) && !prod->col_var)
goto error;
for (i = 0; i < tab1->n_col; ++i) {
int pos = i < d1 ? i : i + d2;
prod->col_var[pos] = tab1->col_var[i];
}
for (i = 0; i < tab2->n_col; ++i) {
int pos = i < d2 ? d1 + i : tab1->n_col + i;
int t = tab2->col_var[i];
if (t >= 0)
t += tab1->n_var;
else
t -= tab1->n_con;
prod->col_var[pos] = t;
}
prod->row_var = isl_alloc_array(tab1->mat->ctx, int,
tab1->mat->n_row + tab2->mat->n_row);
if ((tab1->mat->n_row + tab2->mat->n_row) && !prod->row_var)
goto error;
for (i = 0; i < tab1->n_row; ++i) {
int pos = i < r1 ? i : i + r2;
prod->row_var[pos] = tab1->row_var[i];
}
for (i = 0; i < tab2->n_row; ++i) {
int pos = i < r2 ? r1 + i : tab1->n_row + i;
int t = tab2->row_var[i];
if (t >= 0)
t += tab1->n_var;
else
t -= tab1->n_con;
prod->row_var[pos] = t;
}
prod->samples = NULL;
prod->sample_index = NULL;
prod->n_row = tab1->n_row + tab2->n_row;
prod->n_con = tab1->n_con + tab2->n_con;
prod->n_eq = 0;
prod->max_con = tab1->max_con + tab2->max_con;
prod->n_col = tab1->n_col + tab2->n_col;
prod->n_var = tab1->n_var + tab2->n_var;
prod->max_var = tab1->max_var + tab2->max_var;
prod->n_param = 0;
prod->n_div = 0;
prod->n_dead = tab1->n_dead + tab2->n_dead;
prod->n_redundant = tab1->n_redundant + tab2->n_redundant;
prod->rational = tab1->rational;
prod->empty = tab1->empty || tab2->empty;
prod->strict_redundant = tab1->strict_redundant || tab2->strict_redundant;
prod->need_undo = 0;
prod->in_undo = 0;
prod->M = tab1->M;
prod->cone = tab1->cone;
prod->bottom.type = isl_tab_undo_bottom;
prod->bottom.next = NULL;
prod->top = &prod->bottom;
prod->n_zero = 0;
prod->n_unbounded = 0;
prod->basis = NULL;
return prod;
error:
isl_tab_free(prod);
return NULL;
}
static struct isl_tab_var *var_from_index(struct isl_tab *tab, int i)
{
if (i >= 0)
return &tab->var[i];
else
return &tab->con[~i];
}
struct isl_tab_var *isl_tab_var_from_row(struct isl_tab *tab, int i)
{
return var_from_index(tab, tab->row_var[i]);
}
static struct isl_tab_var *var_from_col(struct isl_tab *tab, int i)
{
return var_from_index(tab, tab->col_var[i]);
}
/* Check if there are any upper bounds on column variable "var",
* i.e., non-negative rows where var appears with a negative coefficient.
* Return 1 if there are no such bounds.
*/
static int max_is_manifestly_unbounded(struct isl_tab *tab,
struct isl_tab_var *var)
{
int i;
unsigned off = 2 + tab->M;
if (var->is_row)
return 0;
for (i = tab->n_redundant; i < tab->n_row; ++i) {
if (!isl_int_is_neg(tab->mat->row[i][off + var->index]))
continue;
if (isl_tab_var_from_row(tab, i)->is_nonneg)
return 0;
}
return 1;
}
/* Check if there are any lower bounds on column variable "var",
* i.e., non-negative rows where var appears with a positive coefficient.
* Return 1 if there are no such bounds.
*/
static int min_is_manifestly_unbounded(struct isl_tab *tab,
struct isl_tab_var *var)
{
int i;
unsigned off = 2 + tab->M;
if (var->is_row)
return 0;
for (i = tab->n_redundant; i < tab->n_row; ++i) {
if (!isl_int_is_pos(tab->mat->row[i][off + var->index]))
continue;
if (isl_tab_var_from_row(tab, i)->is_nonneg)
return 0;
}
return 1;
}
static int row_cmp(struct isl_tab *tab, int r1, int r2, int c, isl_int *t)
{
unsigned off = 2 + tab->M;
if (tab->M) {
int s;
isl_int_mul(*t, tab->mat->row[r1][2], tab->mat->row[r2][off+c]);
isl_int_submul(*t, tab->mat->row[r2][2], tab->mat->row[r1][off+c]);
s = isl_int_sgn(*t);
if (s)
return s;
}
isl_int_mul(*t, tab->mat->row[r1][1], tab->mat->row[r2][off + c]);
isl_int_submul(*t, tab->mat->row[r2][1], tab->mat->row[r1][off + c]);
return isl_int_sgn(*t);
}
/* Given the index of a column "c", return the index of a row
* that can be used to pivot the column in, with either an increase
* (sgn > 0) or a decrease (sgn < 0) of the corresponding variable.
* If "var" is not NULL, then the row returned will be different from
* the one associated with "var".
*
* Each row in the tableau is of the form
*
* x_r = a_r0 + \sum_i a_ri x_i
*
* Only rows with x_r >= 0 and with the sign of a_ri opposite to "sgn"
* impose any limit on the increase or decrease in the value of x_c
* and this bound is equal to a_r0 / |a_rc|. We are therefore looking
* for the row with the smallest (most stringent) such bound.
* Note that the common denominator of each row drops out of the fraction.
* To check if row j has a smaller bound than row r, i.e.,
* a_j0 / |a_jc| < a_r0 / |a_rc| or a_j0 |a_rc| < a_r0 |a_jc|,
* we check if -sign(a_jc) (a_j0 a_rc - a_r0 a_jc) < 0,
* where -sign(a_jc) is equal to "sgn".
*/
static int pivot_row(struct isl_tab *tab,
struct isl_tab_var *var, int sgn, int c)
{
int j, r, tsgn;
isl_int t;
unsigned off = 2 + tab->M;
isl_int_init(t);
r = -1;
for (j = tab->n_redundant; j < tab->n_row; ++j) {
if (var && j == var->index)
continue;
if (!isl_tab_var_from_row(tab, j)->is_nonneg)
continue;
if (sgn * isl_int_sgn(tab->mat->row[j][off + c]) >= 0)
continue;
if (r < 0) {
r = j;
continue;
}
tsgn = sgn * row_cmp(tab, r, j, c, &t);
if (tsgn < 0 || (tsgn == 0 &&
tab->row_var[j] < tab->row_var[r]))
r = j;
}
isl_int_clear(t);
return r;
}
/* Find a pivot (row and col) that will increase (sgn > 0) or decrease
* (sgn < 0) the value of row variable var.
* If not NULL, then skip_var is a row variable that should be ignored
* while looking for a pivot row. It is usually equal to var.
*
* As the given row in the tableau is of the form
*
* x_r = a_r0 + \sum_i a_ri x_i
*
* we need to find a column such that the sign of a_ri is equal to "sgn"
* (such that an increase in x_i will have the desired effect) or a
* column with a variable that may attain negative values.
* If a_ri is positive, then we need to move x_i in the same direction
* to obtain the desired effect. Otherwise, x_i has to move in the
* opposite direction.
*/
static void find_pivot(struct isl_tab *tab,
struct isl_tab_var *var, struct isl_tab_var *skip_var,
int sgn, int *row, int *col)
{
int j, r, c;
isl_int *tr;
*row = *col = -1;
isl_assert(tab->mat->ctx, var->is_row, return);
tr = tab->mat->row[var->index] + 2 + tab->M;
c = -1;
for (j = tab->n_dead; j < tab->n_col; ++j) {
if (isl_int_is_zero(tr[j]))
continue;
if (isl_int_sgn(tr[j]) != sgn &&
var_from_col(tab, j)->is_nonneg)
continue;
if (c < 0 || tab->col_var[j] < tab->col_var[c])
c = j;
}
if (c < 0)
return;
sgn *= isl_int_sgn(tr[c]);
r = pivot_row(tab, skip_var, sgn, c);
*row = r < 0 ? var->index : r;
*col = c;
}
/* Return 1 if row "row" represents an obviously redundant inequality.
* This means
* - it represents an inequality or a variable
* - that is the sum of a non-negative sample value and a positive
* combination of zero or more non-negative constraints.
*/
int isl_tab_row_is_redundant(struct isl_tab *tab, int row)
{
int i;
unsigned off = 2 + tab->M;
if (tab->row_var[row] < 0 && !isl_tab_var_from_row(tab, row)->is_nonneg)
return 0;
if (isl_int_is_neg(tab->mat->row[row][1]))
return 0;
if (tab->strict_redundant && isl_int_is_zero(tab->mat->row[row][1]))
return 0;
if (tab->M && isl_int_is_neg(tab->mat->row[row][2]))
return 0;
for (i = tab->n_dead; i < tab->n_col; ++i) {
if (isl_int_is_zero(tab->mat->row[row][off + i]))
continue;
if (tab->col_var[i] >= 0)
return 0;
if (isl_int_is_neg(tab->mat->row[row][off + i]))
return 0;
if (!var_from_col(tab, i)->is_nonneg)
return 0;
}
return 1;
}
static void swap_rows(struct isl_tab *tab, int row1, int row2)
{
int t;
enum isl_tab_row_sign s;
t = tab->row_var[row1];
tab->row_var[row1] = tab->row_var[row2];
tab->row_var[row2] = t;
isl_tab_var_from_row(tab, row1)->index = row1;
isl_tab_var_from_row(tab, row2)->index = row2;
tab->mat = isl_mat_swap_rows(tab->mat, row1, row2);
if (!tab->row_sign)
return;
s = tab->row_sign[row1];
tab->row_sign[row1] = tab->row_sign[row2];
tab->row_sign[row2] = s;
}
static isl_stat push_union(struct isl_tab *tab,
enum isl_tab_undo_type type, union isl_tab_undo_val u) WARN_UNUSED;
/* Push record "u" onto the undo stack of "tab", provided "tab"
* keeps track of undo information.
*
* If the record cannot be pushed, then mark the undo stack as invalid
* such that a later rollback attempt will not try to undo earlier
* records without having been able to undo the current record.
*/
static isl_stat push_union(struct isl_tab *tab,
enum isl_tab_undo_type type, union isl_tab_undo_val u)
{
struct isl_tab_undo *undo;
if (!tab)
return isl_stat_error;
if (!tab->need_undo)
return isl_stat_ok;
undo = isl_alloc_type(tab->mat->ctx, struct isl_tab_undo);
if (!undo)
goto error;
undo->type = type;
undo->u = u;
undo->next = tab->top;
tab->top = undo;
return isl_stat_ok;
error:
free_undo(tab);
tab->top = NULL;
return isl_stat_error;
}
isl_stat isl_tab_push_var(struct isl_tab *tab,
enum isl_tab_undo_type type, struct isl_tab_var *var)
{
union isl_tab_undo_val u;
if (var->is_row)
u.var_index = tab->row_var[var->index];
else
u.var_index = tab->col_var[var->index];
return push_union(tab, type, u);
}
isl_stat isl_tab_push(struct isl_tab *tab, enum isl_tab_undo_type type)
{
union isl_tab_undo_val u = { 0 };
return push_union(tab, type, u);
}
/* Push a record on the undo stack describing the current basic
* variables, so that the this state can be restored during rollback.
*/
isl_stat isl_tab_push_basis(struct isl_tab *tab)
{
int i;
union isl_tab_undo_val u;
u.col_var = isl_alloc_array(tab->mat->ctx, int, tab->n_col);
if (tab->n_col && !u.col_var)
return isl_stat_error;
for (i = 0; i < tab->n_col; ++i)
u.col_var[i] = tab->col_var[i];
return push_union(tab, isl_tab_undo_saved_basis, u);
}
isl_stat isl_tab_push_callback(struct isl_tab *tab,
struct isl_tab_callback *callback)
{
union isl_tab_undo_val u;
u.callback = callback;
return push_union(tab, isl_tab_undo_callback, u);
}
struct isl_tab *isl_tab_init_samples(struct isl_tab *tab)
{
if (!tab)
return NULL;
tab->n_sample = 0;
tab->n_outside = 0;
tab->samples = isl_mat_alloc(tab->mat->ctx, 1, 1 + tab->n_var);
if (!tab->samples)
goto error;
tab->sample_index = isl_alloc_array(tab->mat->ctx, int, 1);
if (!tab->sample_index)
goto error;
return tab;
error:
isl_tab_free(tab);
return NULL;
}
int isl_tab_add_sample(struct isl_tab *tab, __isl_take isl_vec *sample)
{
if (!tab || !sample)
goto error;
if (tab->n_sample + 1 > tab->samples->n_row) {
int *t = isl_realloc_array(tab->mat->ctx,
tab->sample_index, int, tab->n_sample + 1);
if (!t)
goto error;
tab->sample_index = t;
}
tab->samples = isl_mat_extend(tab->samples,
tab->n_sample + 1, tab->samples->n_col);
if (!tab->samples)
goto error;
isl_seq_cpy(tab->samples->row[tab->n_sample], sample->el, sample->size);
isl_vec_free(sample);
tab->sample_index[tab->n_sample] = tab->n_sample;
tab->n_sample++;
return 0;
error:
isl_vec_free(sample);
return -1;
}
struct isl_tab *isl_tab_drop_sample(struct isl_tab *tab, int s)
{
if (s != tab->n_outside) {
int t = tab->sample_index[tab->n_outside];
tab->sample_index[tab->n_outside] = tab->sample_index[s];
tab->sample_index[s] = t;
isl_mat_swap_rows(tab->samples, tab->n_outside, s);
}
tab->n_outside++;
if (isl_tab_push(tab, isl_tab_undo_drop_sample) < 0) {
isl_tab_free(tab);
return NULL;
}
return tab;
}
/* Record the current number of samples so that we can remove newer
* samples during a rollback.
*/
isl_stat isl_tab_save_samples(struct isl_tab *tab)
{
union isl_tab_undo_val u;
if (!tab)
return isl_stat_error;
u.n = tab->n_sample;
return push_union(tab, isl_tab_undo_saved_samples, u);
}
/* Mark row with index "row" as being redundant.
* If we may need to undo the operation or if the row represents
* a variable of the original problem, the row is kept,
* but no longer considered when looking for a pivot row.
* Otherwise, the row is simply removed.
*
* The row may be interchanged with some other row. If it
* is interchanged with a later row, return 1. Otherwise return 0.
* If the rows are checked in order in the calling function,
* then a return value of 1 means that the row with the given
* row number may now contain a different row that hasn't been checked yet.
*/
int isl_tab_mark_redundant(struct isl_tab *tab, int row)
{
struct isl_tab_var *var = isl_tab_var_from_row(tab, row);
var->is_redundant = 1;
isl_assert(tab->mat->ctx, row >= tab->n_redundant, return -1);
if (tab->preserve || tab->need_undo || tab->row_var[row] >= 0) {
if (tab->row_var[row] >= 0 && !var->is_nonneg) {
var->is_nonneg = 1;
if (isl_tab_push_var(tab, isl_tab_undo_nonneg, var) < 0)
return -1;
}
if (row != tab->n_redundant)
swap_rows(tab, row, tab->n_redundant);
tab->n_redundant++;
return isl_tab_push_var(tab, isl_tab_undo_redundant, var);
} else {
if (row != tab->n_row - 1)
swap_rows(tab, row, tab->n_row - 1);
isl_tab_var_from_row(tab, tab->n_row - 1)->index = -1;
tab->n_row--;
return 1;
}
}
/* Mark "tab" as a rational tableau.
* If it wasn't marked as a rational tableau already and if we may
* need to undo changes, then arrange for the marking to be undone
* during the undo.
*/
int isl_tab_mark_rational(struct isl_tab *tab)
{
if (!tab)
return -1;
if (!tab->rational && tab->need_undo)
if (isl_tab_push(tab, isl_tab_undo_rational) < 0)
return -1;
tab->rational = 1;
return 0;
}
isl_stat isl_tab_mark_empty(struct isl_tab *tab)
{
if (!tab)
return isl_stat_error;
if (!tab->empty && tab->need_undo)
if (isl_tab_push(tab, isl_tab_undo_empty) < 0)
return isl_stat_error;
tab->empty = 1;
return isl_stat_ok;
}
int isl_tab_freeze_constraint(struct isl_tab *tab, int con)
{
struct isl_tab_var *var;
if (!tab)
return -1;
var = &tab->con[con];
if (var->frozen)
return 0;
if (var->index < 0)
return 0;
var->frozen = 1;
if (tab->need_undo)
return isl_tab_push_var(tab, isl_tab_undo_freeze, var);
return 0;
}
/* Update the rows signs after a pivot of "row" and "col", with "row_sgn"
* the original sign of the pivot element.
* We only keep track of row signs during PILP solving and in this case
* we only pivot a row with negative sign (meaning the value is always
* non-positive) using a positive pivot element.
*
* For each row j, the new value of the parametric constant is equal to
*
* a_j0 - a_jc a_r0/a_rc
*
* where a_j0 is the original parametric constant, a_rc is the pivot element,
* a_r0 is the parametric constant of the pivot row and a_jc is the
* pivot column entry of the row j.
* Since a_r0 is non-positive and a_rc is positive, the sign of row j
* remains the same if a_jc has the same sign as the row j or if
* a_jc is zero. In all other cases, we reset the sign to "unknown".
*/
static void update_row_sign(struct isl_tab *tab, int row, int col, int row_sgn)
{
int i;
struct isl_mat *mat = tab->mat;
unsigned off = 2 + tab->M;
if (!tab->row_sign)
return;
if (tab->row_sign[row] == 0)
return;
isl_assert(mat->ctx, row_sgn > 0, return);
isl_assert(mat->ctx, tab->row_sign[row] == isl_tab_row_neg, return);
tab->row_sign[row] = isl_tab_row_pos;
for (i = 0; i < tab->n_row; ++i) {
int s;
if (i == row)
continue;
s = isl_int_sgn(mat->row[i][off + col]);
if (!s)
continue;
if (!tab->row_sign[i])
continue;
if (s < 0 && tab->row_sign[i] == isl_tab_row_neg)
continue;
if (s > 0 && tab->row_sign[i] == isl_tab_row_pos)
continue;
tab->row_sign[i] = isl_tab_row_unknown;
}
}
/* Given a row number "row" and a column number "col", pivot the tableau
* such that the associated variables are interchanged.
* The given row in the tableau expresses
*
* x_r = a_r0 + \sum_i a_ri x_i
*
* or
*
* x_c = 1/a_rc x_r - a_r0/a_rc + sum_{i \ne r} -a_ri/a_rc
*
* Substituting this equality into the other rows
*
* x_j = a_j0 + \sum_i a_ji x_i
*
* with a_jc \ne 0, we obtain
*
* x_j = a_jc/a_rc x_r + a_j0 - a_jc a_r0/a_rc + sum a_ji - a_jc a_ri/a_rc
*
* The tableau
*
* n_rc/d_r n_ri/d_r
* n_jc/d_j n_ji/d_j
*
* where i is any other column and j is any other row,
* is therefore transformed into
*
* s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
* s(n_rc)d_r n_jc/(|n_rc| d_j) (n_ji |n_rc| - s(n_rc)n_jc n_ri)/(|n_rc| d_j)
*
* The transformation is performed along the following steps
*
* d_r/n_rc n_ri/n_rc
* n_jc/d_j n_ji/d_j
*
* s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
* n_jc/d_j n_ji/d_j
*
* s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
* n_jc/(|n_rc| d_j) n_ji/(|n_rc| d_j)
*
* s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
* n_jc/(|n_rc| d_j) (n_ji |n_rc|)/(|n_rc| d_j)
*
* s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
* n_jc/(|n_rc| d_j) (n_ji |n_rc| - s(n_rc)n_jc n_ri)/(|n_rc| d_j)
*
* s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
* s(n_rc)d_r n_jc/(|n_rc| d_j) (n_ji |n_rc| - s(n_rc)n_jc n_ri)/(|n_rc| d_j)
*
*/
int isl_tab_pivot(struct isl_tab *tab, int row, int col)
{
int i, j;
int sgn;
int t;
isl_ctx *ctx;
struct isl_mat *mat = tab->mat;
struct isl_tab_var *var;
unsigned off = 2 + tab->M;
ctx = isl_tab_get_ctx(tab);
if (isl_ctx_next_operation(ctx) < 0)
return -1;
isl_int_swap(mat->row[row][0], mat->row[row][off + col]);
sgn = isl_int_sgn(mat->row[row][0]);
if (sgn < 0) {
isl_int_neg(mat->row[row][0], mat->row[row][0]);
isl_int_neg(mat->row[row][off + col], mat->row[row][off + col]);
} else
for (j = 0; j < off - 1 + tab->n_col; ++j) {
if (j == off - 1 + col)
continue;
isl_int_neg(mat->row[row][1 + j], mat->row[row][1 + j]);
}
if (!isl_int_is_one(mat->row[row][0]))
isl_seq_normalize(mat->ctx, mat->row[row], off + tab->n_col);
for (i = 0; i < tab->n_row; ++i) {
if (i == row)
continue;
if (isl_int_is_zero(mat->row[i][off + col]))
continue;
isl_int_mul(mat->row[i][0], mat->row[i][0], mat->row[row][0]);
for (j = 0; j < off - 1 + tab->n_col; ++j) {
if (j == off - 1 + col)
continue;
isl_int_mul(mat->row[i][1 + j],
mat->row[i][1 + j], mat->row[row][0]);
isl_int_addmul(mat->row[i][1 + j],
mat->row[i][off + col], mat->row[row][1 + j]);
}
isl_int_mul(mat->row[i][off + col],
mat->row[i][off + col], mat->row[row][off + col]);
if (!isl_int_is_one(mat->row[i][0]))
isl_seq_normalize(mat->ctx, mat->row[i], off + tab->n_col);
}
t = tab->row_var[row];
tab->row_var[row] = tab->col_var[col];
tab->col_var[col] = t;
var = isl_tab_var_from_row(tab, row);
var->is_row = 1;
var->index = row;
var = var_from_col(tab, col);
var->is_row = 0;
var->index = col;
update_row_sign(tab, row, col, sgn);
if (tab->in_undo)
return 0;
for (i = tab->n_redundant; i < tab->n_row; ++i) {
if (isl_int_is_zero(mat->row[i][off + col]))
continue;
if (!isl_tab_var_from_row(tab, i)->frozen &&
isl_tab_row_is_redundant(tab, i)) {
int redo = isl_tab_mark_redundant(tab, i);
if (redo < 0)
return -1;
if (redo)
--i;
}
}
return 0;
}
/* If "var" represents a column variable, then pivot is up (sgn > 0)
* or down (sgn < 0) to a row. The variable is assumed not to be
* unbounded in the specified direction.
* If sgn = 0, then the variable is unbounded in both directions,
* and we pivot with any row we can find.
*/
static int to_row(struct isl_tab *tab, struct isl_tab_var *var, int sign) WARN_UNUSED;
static int to_row(struct isl_tab *tab, struct isl_tab_var *var, int sign)
{
int r;
unsigned off = 2 + tab->M;
if (var->is_row)
return 0;
if (sign == 0) {
for (r = tab->n_redundant; r < tab->n_row; ++r)
if (!isl_int_is_zero(tab->mat->row[r][off+var->index]))
break;
isl_assert(tab->mat->ctx, r < tab->n_row, return -1);
} else {
r = pivot_row(tab, NULL, sign, var->index);
isl_assert(tab->mat->ctx, r >= 0, return -1);
}
return isl_tab_pivot(tab, r, var->index);
}
/* Check whether all variables that are marked as non-negative
* also have a non-negative sample value. This function is not
* called from the current code but is useful during debugging.
*/
static void check_table(struct isl_tab *tab) __attribute__ ((unused));
static void check_table(struct isl_tab *tab)
{
int i;
if (tab->empty)
return;
for (i = tab->n_redundant; i < tab->n_row; ++i) {
struct isl_tab_var *var;
var = isl_tab_var_from_row(tab, i);
if (!var->is_nonneg)
continue;
if (tab->M) {
isl_assert(tab->mat->ctx,
!isl_int_is_neg(tab->mat->row[i][2]), abort());
if (isl_int_is_pos(tab->mat->row[i][2]))
continue;
}
isl_assert(tab->mat->ctx, !isl_int_is_neg(tab->mat->row[i][1]),
abort());
}
}
/* Return the sign of the maximal value of "var".
* If the sign is not negative, then on return from this function,
* the sample value will also be non-negative.
*
* If "var" is manifestly unbounded wrt positive values, we are done.
* Otherwise, we pivot the variable up to a row if needed
* Then we continue pivoting down until either
* - no more down pivots can be performed
* - the sample value is positive
* - the variable is pivoted into a manifestly unbounded column
*/
static int sign_of_max(struct isl_tab *tab, struct isl_tab_var *var)
{
int row, col;
if (max_is_manifestly_unbounded(tab, var))
return 1;
if (to_row(tab, var, 1) < 0)
return -2;
while (!isl_int_is_pos(tab->mat->row[var->index][1])) {
find_pivot(tab, var, var, 1, &row, &col);
if (row == -1)
return isl_int_sgn(tab->mat->row[var->index][1]);
if (isl_tab_pivot(tab, row, col) < 0)
return -2;
if (!var->is_row) /* manifestly unbounded */
return 1;
}
return 1;
}
int isl_tab_sign_of_max(struct isl_tab *tab, int con)
{
struct isl_tab_var *var;
if (!tab)
return -2;
var = &tab->con[con];
isl_assert(tab->mat->ctx, !var->is_redundant, return -2);
isl_assert(tab->mat->ctx, !var->is_zero, return -2);
return sign_of_max(tab, var);
}
static int row_is_neg(struct isl_tab *tab, int row)
{
if (!tab->M)
return isl_int_is_neg(tab->mat->row[row][1]);
if (isl_int_is_pos(tab->mat->row[row][2]))
return 0;
if (isl_int_is_neg(tab->mat->row[row][2]))
return 1;
return isl_int_is_neg(tab->mat->row[row][1]);
}
static int row_sgn(struct isl_tab *tab, int row)
{
if (!tab->M)
return isl_int_sgn(tab->mat->row[row][1]);
if (!isl_int_is_zero(tab->mat->row[row][2]))
return isl_int_sgn(tab->mat->row[row][2]);
else
return isl_int_sgn(tab->mat->row[row][1]);
}
/* Perform pivots until the row variable "var" has a non-negative
* sample value or until no more upward pivots can be performed.
* Return the sign of the sample value after the pivots have been
* performed.
*/
static int restore_row(struct isl_tab *tab, struct isl_tab_var *var)
{
int row, col;
while (row_is_neg(tab, var->index)) {
find_pivot(tab, var, var, 1, &row, &col);
if (row == -1)
break;
if (isl_tab_pivot(tab, row, col) < 0)
return -2;
if (!var->is_row) /* manifestly unbounded */
return 1;
}
return row_sgn(tab, var->index);
}
/* Perform pivots until we are sure that the row variable "var"
* can attain non-negative values. After return from this
* function, "var" is still a row variable, but its sample
* value may not be non-negative, even if the function returns 1.
*/
static int at_least_zero(struct isl_tab *tab, struct isl_tab_var *var)
{
int row, col;
while (isl_int_is_neg(tab->mat->row[var->index][1])) {
find_pivot(tab, var, var, 1, &row, &col);
if (row == -1)
break;
if (row == var->index) /* manifestly unbounded */
return 1;
if (isl_tab_pivot(tab, row, col) < 0)
return -1;
}
return !isl_int_is_neg(tab->mat->row[var->index][1]);
}
/* Return a negative value if "var" can attain negative values.
* Return a non-negative value otherwise.
*
* If "var" is manifestly unbounded wrt negative values, we are done.
* Otherwise, if var is in a column, we can pivot it down to a row.
* Then we continue pivoting down until either
* - the pivot would result in a manifestly unbounded column
* => we don't perform the pivot, but simply return -1
* - no more down pivots can be performed
* - the sample value is negative
* If the sample value becomes negative and the variable is supposed
* to be nonnegative, then we undo the last pivot.
* However, if the last pivot has made the pivoting variable
* obviously redundant, then it may have moved to another row.
* In that case we look for upward pivots until we reach a non-negative
* value again.
*/
static int sign_of_min(struct isl_tab *tab, struct isl_tab_var *var)
{
int row, col;
struct isl_tab_var *pivot_var = NULL;
if (min_is_manifestly_unbounded(tab, var))
return -1;
if (!var->is_row) {
col = var->index;
row = pivot_row(tab, NULL, -1, col);
pivot_var = var_from_col(tab, col);
if (isl_tab_pivot(tab, row, col) < 0)
return -2;
if (var->is_redundant)
return 0;
if (isl_int_is_neg(tab->mat->row[var->index][1])) {
if (var->is_nonneg) {
if (!pivot_var->is_redundant &&
pivot_var->index == row) {
if (isl_tab_pivot(tab, row, col) < 0)
return -2;
} else
if (restore_row(tab, var) < -1)
return -2;
}
return -1;
}
}
if (var->is_redundant)
return 0;
while (!isl_int_is_neg(tab->mat->row[var->index][1])) {
find_pivot(tab, var, var, -1, &row, &col);
if (row == var->index)
return -1;
if (row == -1)
return isl_int_sgn(tab->mat->row[var->index][1]);
pivot_var = var_from_col(tab, col);
if (isl_tab_pivot(tab, row, col) < 0)
return -2;
if (var->is_redundant)
return 0;
}
if (pivot_var && var->is_nonneg) {
/* pivot back to non-negative value */
if (!pivot_var->is_redundant && pivot_var->index == row) {
if (isl_tab_pivot(tab, row, col) < 0)
return -2;
} else
if (restore_row(tab, var) < -1)
return -2;
}
return -1;
}
static int row_at_most_neg_one(struct isl_tab *tab, int row)
{
if (tab->M) {
if (isl_int_is_pos(tab->mat->row[row][2]))
return 0;
if (isl_int_is_neg(tab->mat->row[row][2]))
return 1;
}
return isl_int_is_neg(tab->mat->row[row][1]) &&
isl_int_abs_ge(tab->mat->row[row][1],
tab->mat->row[row][0]);
}
/* Return 1 if "var" can attain values <= -1.
* Return 0 otherwise.
*
* If the variable "var" is supposed to be non-negative (is_nonneg is set),
* then the sample value of "var" is assumed to be non-negative when the
* the function is called. If 1 is returned then the constraint
* is not redundant and the sample value is made non-negative again before
* the function returns.
*/
int isl_tab_min_at_most_neg_one(struct isl_tab *tab, struct isl_tab_var *var)
{
int row, col;
struct isl_tab_var *pivot_var;
if (min_is_manifestly_unbounded(tab, var))
return 1;
if (!var->is_row) {
col = var->index;
row = pivot_row(tab, NULL, -1, col);
pivot_var = var_from_col(tab, col);
if (isl_tab_pivot(tab, row, col) < 0)
return -1;
if (var->is_redundant)
return 0;
if (row_at_most_neg_one(tab, var->index)) {
if (var->is_nonneg) {
if (!pivot_var->is_redundant &&
pivot_var->index == row) {
if (isl_tab_pivot(tab, row, col) < 0)
return -1;
} else
if (restore_row(tab, var) < -1)
return -1;
}
return 1;
}
}
if (var->is_redundant)
return 0;
do {
find_pivot(tab, var, var, -1, &row, &col);
if (row == var->index) {
if (var->is_nonneg && restore_row(tab, var) < -1)
return -1;
return 1;
}
if (row == -1)
return 0;
pivot_var = var_from_col(tab, col);
if (isl_tab_pivot(tab, row, col) < 0)
return -1;
if (var->is_redundant)
return 0;
} while (!row_at_most_neg_one(tab, var->index));
if (var->is_nonneg) {
/* pivot back to non-negative value */
if (!pivot_var->is_redundant && pivot_var->index == row)
if (isl_tab_pivot(tab, row, col) < 0)
return -1;
if (restore_row(tab, var) < -1)
return -1;
}
return 1;
}
/* Return 1 if "var" can attain values >= 1.
* Return 0 otherwise.
*/
static int at_least_one(struct isl_tab *tab, struct isl_tab_var *var)
{
int row, col;
isl_int *r;
if (max_is_manifestly_unbounded(tab, var))
return 1;
if (to_row(tab, var, 1) < 0)
return -1;
r = tab->mat->row[var->index];
while (isl_int_lt(r[1], r[0])) {
find_pivot(tab, var, var, 1, &row, &col);
if (row == -1)
return isl_int_ge(r[1], r[0]);
if (row == var->index) /* manifestly unbounded */
return 1;
if (isl_tab_pivot(tab, row, col) < 0)
return -1;
}
return 1;
}
static void swap_cols(struct isl_tab *tab, int col1, int col2)
{
int t;
unsigned off = 2 + tab->M;
t = tab->col_var[col1];
tab->col_var[col1] = tab->col_var[col2];
tab->col_var[col2] = t;
var_from_col(tab, col1)->index = col1;
var_from_col(tab, col2)->index = col2;
tab->mat = isl_mat_swap_cols(tab->mat, off + col1, off + col2);
}
/* Mark column with index "col" as representing a zero variable.
* If we may need to undo the operation the column is kept,
* but no longer considered.
* Otherwise, the column is simply removed.
*
* The column may be interchanged with some other column. If it
* is interchanged with a later column, return 1. Otherwise return 0.
* If the columns are checked in order in the calling function,
* then a return value of 1 means that the column with the given
* column number may now contain a different column that
* hasn't been checked yet.
*/
int isl_tab_kill_col(struct isl_tab *tab, int col)
{
var_from_col(tab, col)->is_zero = 1;
if (tab->need_undo) {
if (isl_tab_push_var(tab, isl_tab_undo_zero,
var_from_col(tab, col)) < 0)
return -1;
if (col != tab->n_dead)
swap_cols(tab, col, tab->n_dead);
tab->n_dead++;
return 0;
} else {
if (col != tab->n_col - 1)
swap_cols(tab, col, tab->n_col - 1);
var_from_col(tab, tab->n_col - 1)->index = -1;
tab->n_col--;
return 1;
}
}
static int row_is_manifestly_non_integral(struct isl_tab *tab, int row)
{
unsigned off = 2 + tab->M;
if (tab->M && !isl_int_eq(tab->mat->row[row][2],
tab->mat->row[row][0]))
return 0;
if (isl_seq_first_non_zero(tab->mat->row[row] + off + tab->n_dead,
tab->n_col - tab->n_dead) != -1)
return 0;
return !isl_int_is_divisible_by(tab->mat->row[row][1],
tab->mat->row[row][0]);
}
/* For integer tableaus, check if any of the coordinates are stuck
* at a non-integral value.
*/
static int tab_is_manifestly_empty(struct isl_tab *tab)
{
int i;
if (tab->empty)
return 1;
if (tab->rational)
return 0;
for (i = 0; i < tab->n_var; ++i) {
if (!tab->var[i].is_row)
continue;
if (row_is_manifestly_non_integral(tab, tab->var[i].index))
return 1;
}
return 0;
}
/* Row variable "var" is non-negative and cannot attain any values
* larger than zero. This means that the coefficients of the unrestricted
* column variables are zero and that the coefficients of the non-negative
* column variables are zero or negative.
* Each of the non-negative variables with a negative coefficient can
* then also be written as the negative sum of non-negative variables
* and must therefore also be zero.
*
* If "temp_var" is set, then "var" is a temporary variable that
* will be removed after this function returns and for which
* no information is recorded on the undo stack.
* Do not add any undo records involving this variable in this case
* since the variable will have been removed before any future undo
* operations. Also avoid marking the variable as redundant,
* since that either adds an undo record or needlessly removes the row
* (the caller will take care of removing the row).
*/
static isl_stat close_row(struct isl_tab *tab, struct isl_tab_var *var,
int temp_var) WARN_UNUSED;
static isl_stat close_row(struct isl_tab *tab, struct isl_tab_var *var,
int temp_var)
{
int j;
struct isl_mat *mat = tab->mat;
unsigned off = 2 + tab->M;
if (!var->is_nonneg)
isl_die(isl_tab_get_ctx(tab), isl_error_internal,
"expecting non-negative variable",
return isl_stat_error);
var->is_zero = 1;
if (!temp_var && tab->need_undo)
if (isl_tab_push_var(tab, isl_tab_undo_zero, var) < 0)
return isl_stat_error;
for (j = tab->n_dead; j < tab->n_col; ++j) {
int recheck;
if (isl_int_is_zero(mat->row[var->index][off + j]))
continue;
if (isl_int_is_pos(mat->row[var->index][off + j]))
isl_die(isl_tab_get_ctx(tab), isl_error_internal,
"row cannot have positive coefficients",
return isl_stat_error);
recheck = isl_tab_kill_col(tab, j);
if (recheck < 0)
return isl_stat_error;
if (recheck)
--j;
}
if (!temp_var && isl_tab_mark_redundant(tab, var->index) < 0)
return isl_stat_error;
if (tab_is_manifestly_empty(tab) && isl_tab_mark_empty(tab) < 0)
return isl_stat_error;
return isl_stat_ok;
}
/* Add a constraint to the tableau and allocate a row for it.
* Return the index into the constraint array "con".
*
* This function assumes that at least one more row and at least
* one more element in the constraint array are available in the tableau.
*/
int isl_tab_allocate_con(struct isl_tab *tab)
{
int r;
isl_assert(tab->mat->ctx, tab->n_row < tab->mat->n_row, return -1);
isl_assert(tab->mat->ctx, tab->n_con < tab->max_con, return -1);
r = tab->n_con;
tab->con[r].index = tab->n_row;
tab->con[r].is_row = 1;
tab->con[r].is_nonneg = 0;
tab->con[r].is_zero = 0;
tab->con[r].is_redundant = 0;
tab->con[r].frozen = 0;
tab->con[r].negated = 0;
tab->row_var[tab->n_row] = ~r;
tab->n_row++;
tab->n_con++;
if (isl_tab_push_var(tab, isl_tab_undo_allocate, &tab->con[r]) < 0)
return -1;
return r;
}
/* Move the entries in tab->var up one position, starting at "first",
* creating room for an extra entry at position "first".
* Since some of the entries of tab->row_var and tab->col_var contain
* indices into this array, they have to be updated accordingly.
*/
static int var_insert_entry(struct isl_tab *tab, int first)
{
int i;
if (tab->n_var >= tab->max_var)
isl_die(isl_tab_get_ctx(tab), isl_error_internal,
"not enough room for new variable", return -1);
if (first > tab->n_var)
isl_die(isl_tab_get_ctx(tab), isl_error_internal,
"invalid initial position", return -1);
for (i = tab->n_var - 1; i >= first; --i) {
tab->var[i + 1] = tab->var[i];
if (tab->var[i + 1].is_row)
tab->row_var[tab->var[i + 1].index]++;
else
tab->col_var[tab->var[i + 1].index]++;
}
tab->n_var++;
return 0;
}
/* Drop the entry at position "first" in tab->var, moving all
* subsequent entries down.
* Since some of the entries of tab->row_var and tab->col_var contain
* indices into this array, they have to be updated accordingly.
*/
static int var_drop_entry(struct isl_tab *tab, int first)
{
int i;
if (first >= tab->n_var)
isl_die(isl_tab_get_ctx(tab), isl_error_internal,
"invalid initial position", return -1);
tab->n_var--;
for (i = first; i < tab->n_var; ++i) {
tab->var[i] = tab->var[i + 1];
if (tab->var[i + 1].is_row)
tab->row_var[tab->var[i].index]--;
else
tab->col_var[tab->var[i].index]--;
}
return 0;
}
/* Add a variable to the tableau at position "r" and allocate a column for it.
* Return the index into the variable array "var", i.e., "r",
* or -1 on error.
*/
int isl_tab_insert_var(struct isl_tab *tab, int r)
{
int i;
unsigned off = 2 + tab->M;
isl_assert(tab->mat->ctx, tab->n_col < tab->mat->n_col, return -1);
if (var_insert_entry(tab, r) < 0)
return -1;
tab->var[r].index = tab->n_col;
tab->var[r].is_row = 0;
tab->var[r].is_nonneg = 0;
tab->var[r].is_zero = 0;
tab->var[r].is_redundant = 0;
tab->var[r].frozen = 0;
tab->var[r].negated = 0;
tab->col_var[tab->n_col] = r;
for (i = 0; i < tab->n_row; ++i)
isl_int_set_si(tab->mat->row[i][off + tab->n_col], 0);
tab->n_col++;
if (isl_tab_push_var(tab, isl_tab_undo_allocate, &tab->var[r]) < 0)
return -1;
return r;
}
/* Add a row to the tableau. The row is given as an affine combination
* of the original variables and needs to be expressed in terms of the
* column variables.
*
* This function assumes that at least one more row and at least
* one more element in the constraint array are available in the tableau.
*
* We add each term in turn.
* If r = n/d_r is the current sum and we need to add k x, then
* if x is a column variable, we increase the numerator of
* this column by k d_r
* if x = f/d_x is a row variable, then the new representation of r is
*
* n k f d_x/g n + d_r/g k f m/d_r n + m/d_g k f
* --- + --- = ------------------- = -------------------
* d_r d_r d_r d_x/g m
*
* with g the gcd of d_r and d_x and m the lcm of d_r and d_x.
*
* If tab->M is set, then, internally, each variable x is represented
* as x' - M. We then also need no subtract k d_r from the coefficient of M.
*/
int isl_tab_add_row(struct isl_tab *tab, isl_int *line)
{
int i;
int r;
isl_int *row;
isl_int a, b;
unsigned off = 2 + tab->M;
r = isl_tab_allocate_con(tab);
if (r < 0)
return -1;
isl_int_init(a);
isl_int_init(b);
row = tab->mat->row[tab->con[r].index];
isl_int_set_si(row[0], 1);
isl_int_set(row[1], line[0]);
isl_seq_clr(row + 2, tab->M + tab->n_col);
for (i = 0; i < tab->n_var; ++i) {
if (tab->var[i].is_zero)
continue;
if (tab->var[i].is_row) {
isl_int_lcm(a,
row[0], tab->mat->row[tab->var[i].index][0]);
isl_int_swap(a, row[0]);
isl_int_divexact(a, row[0], a);
isl_int_divexact(b,
row[0], tab->mat->row[tab->var[i].index][0]);
isl_int_mul(b, b, line[1 + i]);
isl_seq_combine(row + 1, a, row + 1,
b, tab->mat->row[tab->var[i].index] + 1,
1 + tab->M + tab->n_col);
} else
isl_int_addmul(row[off + tab->var[i].index],
line[1 + i], row[0]);
if (tab->M && i >= tab->n_param && i < tab->n_var - tab->n_div)
isl_int_submul(row[2], line[1 + i], row[0]);
}
isl_seq_normalize(tab->mat->ctx, row, off + tab->n_col);
isl_int_clear(a);
isl_int_clear(b);
if (tab->row_sign)
tab->row_sign[tab->con[r].index] = isl_tab_row_unknown;
return r;
}
static isl_stat drop_row(struct isl_tab *tab, int row)
{
isl_assert(tab->mat->ctx, ~tab->row_var[row] == tab->n_con - 1,
return isl_stat_error);
if (row != tab->n_row - 1)
swap_rows(tab, row, tab->n_row - 1);
tab->n_row--;
tab->n_con--;
return isl_stat_ok;
}
/* Drop the variable in column "col" along with the column.
* The column is removed first because it may need to be moved
* into the last position and this process requires
* the contents of the col_var array in a state
* before the removal of the variable.
*/
static isl_stat drop_col(struct isl_tab *tab, int col)
{
int var;
var = tab->col_var[col];
if (col != tab->n_col - 1)
swap_cols(tab, col, tab->n_col - 1);
tab->n_col--;
if (var_drop_entry(tab, var) < 0)
return isl_stat_error;
return isl_stat_ok;
}
/* Add inequality "ineq" and check if it conflicts with the
* previously added constraints or if it is obviously redundant.
*
* This function assumes that at least one more row and at least
* one more element in the constraint array are available in the tableau.
*/
isl_stat isl_tab_add_ineq(struct isl_tab *tab, isl_int *ineq)
{
int r;
int sgn;
isl_int cst;
if (!tab)
return isl_stat_error;
if (tab->bmap) {
struct isl_basic_map *bmap = tab->bmap;
isl_assert(tab->mat->ctx, tab->n_eq == bmap->n_eq,
return isl_stat_error);
isl_assert(tab->mat->ctx,
tab->n_con == bmap->n_eq + bmap->n_ineq,
return isl_stat_error);
tab->bmap = isl_basic_map_add_ineq(tab->bmap, ineq);
if (isl_tab_push(tab, isl_tab_undo_bmap_ineq) < 0)
return isl_stat_error;
if (!tab->bmap)
return isl_stat_error;
}
if (tab->cone) {
isl_int_init(cst);
isl_int_set_si(cst, 0);
isl_int_swap(ineq[0], cst);
}
r = isl_tab_add_row(tab, ineq);
if (tab->cone) {
isl_int_swap(ineq[0], cst);
isl_int_clear(cst);
}
if (r < 0)
return isl_stat_error;
tab->con[r].is_nonneg = 1;
if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
return isl_stat_error;
if (isl_tab_row_is_redundant(tab, tab->con[r].index)) {
if (isl_tab_mark_redundant(tab, tab->con[r].index) < 0)
return isl_stat_error;
return isl_stat_ok;
}
sgn = restore_row(tab, &tab->con[r]);
if (sgn < -1)
return isl_stat_error;
if (sgn < 0)
return isl_tab_mark_empty(tab);
if (tab->con[r].is_row && isl_tab_row_is_redundant(tab, tab->con[r].index))
if (isl_tab_mark_redundant(tab, tab->con[r].index) < 0)
return isl_stat_error;
return isl_stat_ok;
}
/* Pivot a non-negative variable down until it reaches the value zero
* and then pivot the variable into a column position.
*/
static int to_col(struct isl_tab *tab, struct isl_tab_var *var) WARN_UNUSED;
static int to_col(struct isl_tab *tab, struct isl_tab_var *var)
{
int i;
int row, col;
unsigned off = 2 + tab->M;
if (!var->is_row)
return 0;
while (isl_int_is_pos(tab->mat->row[var->index][1])) {
find_pivot(tab, var, NULL, -1, &row, &col);
isl_assert(tab->mat->ctx, row != -1, return -1);
if (isl_tab_pivot(tab, row, col) < 0)
return -1;
if (!var->is_row)
return 0;
}
for (i = tab->n_dead; i < tab->n_col; ++i)
if (!isl_int_is_zero(tab->mat->row[var->index][off + i]))
break;
isl_assert(tab->mat->ctx, i < tab->n_col, return -1);
if (isl_tab_pivot(tab, var->index, i) < 0)
return -1;
return 0;
}
/* We assume Gaussian elimination has been performed on the equalities.
* The equalities can therefore never conflict.
* Adding the equalities is currently only really useful for a later call
* to isl_tab_ineq_type.
*
* This function assumes that at least one more row and at least
* one more element in the constraint array are available in the tableau.
*/
static struct isl_tab *add_eq(struct isl_tab *tab, isl_int *eq)
{
int i;
int r;
if (!tab)
return NULL;
r = isl_tab_add_row(tab, eq);
if (r < 0)
goto error;
r = tab->con[r].index;
i = isl_seq_first_non_zero(tab->mat->row[r] + 2 + tab->M + tab->n_dead,
tab->n_col - tab->n_dead);
isl_assert(tab->mat->ctx, i >= 0, goto error);
i += tab->n_dead;
if (isl_tab_pivot(tab, r, i) < 0)
goto error;
if (isl_tab_kill_col(tab, i) < 0)
goto error;
tab->n_eq++;
return tab;
error:
isl_tab_free(tab);
return NULL;
}
/* Does the sample value of row "row" of "tab" involve the big parameter,
* if any?
*/
static int row_is_big(struct isl_tab *tab, int row)
{
return tab->M && !isl_int_is_zero(tab->mat->row[row][2]);
}
static int row_is_manifestly_zero(struct isl_tab *tab, int row)
{
unsigned off = 2 + tab->M;
if (!isl_int_is_zero(tab->mat->row[row][1]))
return 0;
if (row_is_big(tab, row))
return 0;
return isl_seq_first_non_zero(tab->mat->row[row] + off + tab->n_dead,
tab->n_col - tab->n_dead) == -1;
}
/* Add an equality that is known to be valid for the given tableau.
*
* This function assumes that at least one more row and at least
* one more element in the constraint array are available in the tableau.
*/
int isl_tab_add_valid_eq(struct isl_tab *tab, isl_int *eq)
{
struct isl_tab_var *var;
int r;
if (!tab)
return -1;
r = isl_tab_add_row(tab, eq);
if (r < 0)
return -1;
var = &tab->con[r];
r = var->index;
if (row_is_manifestly_zero(tab, r)) {
var->is_zero = 1;
if (isl_tab_mark_redundant(tab, r) < 0)
return -1;
return 0;
}
if (isl_int_is_neg(tab->mat->row[r][1])) {
isl_seq_neg(tab->mat->row[r] + 1, tab->mat->row[r] + 1,
1 + tab->n_col);
var->negated = 1;
}
var->is_nonneg = 1;
if (to_col(tab, var) < 0)
return -1;
var->is_nonneg = 0;
if (isl_tab_kill_col(tab, var->index) < 0)
return -1;
return 0;
}
/* Add a zero row to "tab" and return the corresponding index
* in the constraint array.
*
* This function assumes that at least one more row and at least
* one more element in the constraint array are available in the tableau.
*/
static int add_zero_row(struct isl_tab *tab)
{
int r;
isl_int *row;
r = isl_tab_allocate_con(tab);
if (r < 0)
return -1;
row = tab->mat->row[tab->con[r].index];
isl_seq_clr(row + 1, 1 + tab->M + tab->n_col);
isl_int_set_si(row[0], 1);
return r;
}
/* Add equality "eq" and check if it conflicts with the
* previously added constraints or if it is obviously redundant.
*
* This function assumes that at least one more row and at least
* one more element in the constraint array are available in the tableau.
* If tab->bmap is set, then two rows are needed instead of one.
*/
isl_stat isl_tab_add_eq(struct isl_tab *tab, isl_int *eq)
{
struct isl_tab_undo *snap = NULL;
struct isl_tab_var *var;
int r;
int row;
int sgn;
isl_int cst;
if (!tab)
return isl_stat_error;
isl_assert(tab->mat->ctx, !tab->M, return isl_stat_error);
if (tab->need_undo)
snap = isl_tab_snap(tab);
if (tab->cone) {
isl_int_init(cst);
isl_int_set_si(cst, 0);
isl_int_swap(eq[0], cst);
}
r = isl_tab_add_row(tab, eq);
if (tab->cone) {
isl_int_swap(eq[0], cst);
isl_int_clear(cst);
}
if (r < 0)
return isl_stat_error;
var = &tab->con[r];
row = var->index;
if (row_is_manifestly_zero(tab, row)) {
if (snap)
return isl_tab_rollback(tab, snap);
return drop_row(tab, row);
}
if (tab->bmap) {
tab->bmap = isl_basic_map_add_ineq(tab->bmap, eq);
if (isl_tab_push(tab, isl_tab_undo_bmap_ineq) < 0)
return isl_stat_error;
isl_seq_neg(eq, eq, 1 + tab->n_var);
tab->bmap = isl_basic_map_add_ineq(tab->bmap, eq);
isl_seq_neg(eq, eq, 1 + tab->n_var);
if (isl_tab_push(tab, isl_tab_undo_bmap_ineq) < 0)
return isl_stat_error;
if (!tab->bmap)
return isl_stat_error;
if (add_zero_row(tab) < 0)
return isl_stat_error;
}
sgn = isl_int_sgn(tab->mat->row[row][1]);
if (sgn > 0) {
isl_seq_neg(tab->mat->row[row] + 1, tab->mat->row[row] + 1,
1 + tab->n_col);
var->negated = 1;
sgn = -1;
}
if (sgn < 0) {
sgn = sign_of_max(tab, var);
if (sgn < -1)
return isl_stat_error;
if (sgn < 0) {
if (isl_tab_mark_empty(tab) < 0)
return isl_stat_error;
return isl_stat_ok;
}
}
var->is_nonneg = 1;
if (to_col(tab, var) < 0)
return isl_stat_error;
var->is_nonneg = 0;
if (isl_tab_kill_col(tab, var->index) < 0)
return isl_stat_error;
return isl_stat_ok;
}
/* Construct and return an inequality that expresses an upper bound
* on the given div.
* In particular, if the div is given by
*
* d = floor(e/m)
*
* then the inequality expresses
*
* m d <= e
*/
static __isl_give isl_vec *ineq_for_div(__isl_keep isl_basic_map *bmap,
unsigned div)
{
isl_size total;
unsigned div_pos;
struct isl_vec *ineq;
total = isl_basic_map_dim(bmap, isl_dim_all);
if (total < 0)
return NULL;
div_pos = 1 + total - bmap->n_div + div;
ineq = isl_vec_alloc(bmap->ctx, 1 + total);
if (!ineq)
return NULL;
isl_seq_cpy(ineq->el, bmap->div[div] + 1, 1 + total);
isl_int_neg(ineq->el[div_pos], bmap->div[div][0]);
return ineq;
}
/* For a div d = floor(f/m), add the constraints
*
* f - m d >= 0
* -(f-(m-1)) + m d >= 0
*
* Note that the second constraint is the negation of
*
* f - m d >= m
*
* If add_ineq is not NULL, then this function is used
* instead of isl_tab_add_ineq to effectively add the inequalities.
*
* This function assumes that at least two more rows and at least
* two more elements in the constraint array are available in the tableau.
*/
static isl_stat add_div_constraints(struct isl_tab *tab, unsigned div,
isl_stat (*add_ineq)(void *user, isl_int *), void *user)
{
isl_size total;
unsigned div_pos;
struct isl_vec *ineq;
total = isl_basic_map_dim(tab->bmap, isl_dim_all);
if (total < 0)
return isl_stat_error;
div_pos = 1 + total - tab->bmap->n_div + div;
ineq = ineq_for_div(tab->bmap, div);
if (!ineq)
goto error;
if (add_ineq) {
if (add_ineq(user, ineq->el) < 0)
goto error;
} else {
if (isl_tab_add_ineq(tab, ineq->el) < 0)
goto error;
}
isl_seq_neg(ineq->el, tab->bmap->div[div] + 1, 1 + total);
isl_int_set(ineq->el[div_pos], tab->bmap->div[div][0]);
isl_int_add(ineq->el[0], ineq->el[0], ineq->el[div_pos]);
isl_int_sub_ui(ineq->el[0], ineq->el[0], 1);
if (add_ineq) {
if (add_ineq(user, ineq->el) < 0)
goto error;
} else {
if (isl_tab_add_ineq(tab, ineq->el) < 0)
goto error;
}
isl_vec_free(ineq);
return isl_stat_ok;
error:
isl_vec_free(ineq);
return isl_stat_error;
}
/* Check whether the div described by "div" is obviously non-negative.
* If we are using a big parameter, then we will encode the div
* as div' = M + div, which is always non-negative.
* Otherwise, we check whether div is a non-negative affine combination
* of non-negative variables.
*/
static int div_is_nonneg(struct isl_tab *tab, __isl_keep isl_vec *div)
{
int i;
if (tab->M)
return 1;
if (isl_int_is_neg(div->el[1]))
return 0;
for (i = 0; i < tab->n_var; ++i) {
if (isl_int_is_neg(div->el[2 + i]))
return 0;
if (isl_int_is_zero(div->el[2 + i]))
continue;
if (!tab->var[i].is_nonneg)
return 0;
}
return 1;
}
/* Insert an extra div, prescribed by "div", to the tableau and
* the associated bmap (which is assumed to be non-NULL).
* The extra integer division is inserted at (tableau) position "pos".
* Return "pos" or -1 if an error occurred.
*
* If add_ineq is not NULL, then this function is used instead
* of isl_tab_add_ineq to add the div constraints.
* This complication is needed because the code in isl_tab_pip
* wants to perform some extra processing when an inequality
* is added to the tableau.
*/
int isl_tab_insert_div(struct isl_tab *tab, int pos, __isl_keep isl_vec *div,
isl_stat (*add_ineq)(void *user, isl_int *), void *user)
{
int r;
int nonneg;
isl_size n_div;
int o_div;
if (!tab || !div)
return -1;
if (div->size != 1 + 1 + tab->n_var)
isl_die(isl_tab_get_ctx(tab), isl_error_invalid,
"unexpected size", return -1);
n_div = isl_basic_map_dim(tab->bmap, isl_dim_div);
if (n_div < 0)
return -1;
o_div = tab->n_var - n_div;
if (pos < o_div || pos > tab->n_var)
isl_die(isl_tab_get_ctx(tab), isl_error_invalid,
"invalid position", return -1);
nonneg = div_is_nonneg(tab, div);
if (isl_tab_extend_cons(tab, 3) < 0)
return -1;
if (isl_tab_extend_vars(tab, 1) < 0)
return -1;
r = isl_tab_insert_var(tab, pos);
if (r < 0)
return -1;
if (nonneg)
tab->var[r].is_nonneg = 1;
tab->bmap = isl_basic_map_insert_div(tab->bmap, pos - o_div, div);
if (!tab->bmap)
return -1;
if (isl_tab_push_var(tab, isl_tab_undo_bmap_div, &tab->var[r]) < 0)
return -1;
if (add_div_constraints(tab, pos - o_div, add_ineq, user) < 0)
return -1;
return r;
}
/* Add an extra div, prescribed by "div", to the tableau and
* the associated bmap (which is assumed to be non-NULL).
*/
int isl_tab_add_div(struct isl_tab *tab, __isl_keep isl_vec *div)
{
if (!tab)
return -1;
return isl_tab_insert_div(tab, tab->n_var, div, NULL, NULL);
}
/* If "track" is set, then we want to keep track of all constraints in tab
* in its bmap field. This field is initialized from a copy of "bmap",
* so we need to make sure that all constraints in "bmap" also appear
* in the constructed tab.
*/
__isl_give struct isl_tab *isl_tab_from_basic_map(
__isl_keep isl_basic_map *bmap, int track)
{
int i;
struct isl_tab *tab;
isl_size total;
total = isl_basic_map_dim(bmap, isl_dim_all);
if (total < 0)
return NULL;
tab = isl_tab_alloc(bmap->ctx, total + bmap->n_ineq + 1, total, 0);
if (!tab)
return NULL;
tab->preserve = track;
tab->rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY)) {
if (isl_tab_mark_empty(tab) < 0)
goto error;
goto done;
}
for (i = 0; i < bmap->n_eq; ++i) {
tab = add_eq(tab, bmap->eq[i]);
if (!tab)
return tab;
}
for (i = 0; i < bmap->n_ineq; ++i) {
if (isl_tab_add_ineq(tab, bmap->ineq[i]) < 0)
goto error;
if (tab->empty)
goto done;
}
done:
if (track && isl_tab_track_bmap(tab, isl_basic_map_copy(bmap)) < 0)
goto error;
return tab;
error:
isl_tab_free(tab);
return NULL;
}
__isl_give struct isl_tab *isl_tab_from_basic_set(
__isl_keep isl_basic_set *bset, int track)
{
return isl_tab_from_basic_map(bset, track);
}
/* Construct a tableau corresponding to the recession cone of "bset".
*/
struct isl_tab *isl_tab_from_recession_cone(__isl_keep isl_basic_set *bset,
int parametric)
{
isl_int cst;
int i;
struct isl_tab *tab;
isl_size offset = 0;
isl_size total;
total = isl_basic_set_dim(bset, isl_dim_all);
if (parametric)
offset = isl_basic_set_dim(bset, isl_dim_param);
if (total < 0 || offset < 0)
return NULL;
tab = isl_tab_alloc(bset->ctx, bset->n_eq + bset->n_ineq,
total - offset, 0);
if (!tab)
return NULL;
tab->rational = ISL_F_ISSET(bset, ISL_BASIC_SET_RATIONAL);
tab->cone = 1;
isl_int_init(cst);
isl_int_set_si(cst, 0);
for (i = 0; i < bset->n_eq; ++i) {
isl_int_swap(bset->eq[i][offset], cst);
if (offset > 0) {
if (isl_tab_add_eq(tab, bset->eq[i] + offset) < 0)
goto error;
} else
tab = add_eq(tab, bset->eq[i]);
isl_int_swap(bset->eq[i][offset], cst);
if (!tab)
goto done;
}
for (i = 0; i < bset->n_ineq; ++i) {
int r;
isl_int_swap(bset->ineq[i][offset], cst);
r = isl_tab_add_row(tab, bset->ineq[i] + offset);
isl_int_swap(bset->ineq[i][offset], cst);
if (r < 0)
goto error;
tab->con[r].is_nonneg = 1;
if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
goto error;
}
done:
isl_int_clear(cst);
return tab;
error:
isl_int_clear(cst);
isl_tab_free(tab);
return NULL;
}
/* Assuming "tab" is the tableau of a cone, check if the cone is
* bounded, i.e., if it is empty or only contains the origin.
*/
isl_bool isl_tab_cone_is_bounded(struct isl_tab *tab)
{
int i;
if (!tab)
return isl_bool_error;
if (tab->empty)
return isl_bool_true;
if (tab->n_dead == tab->n_col)
return isl_bool_true;
for (;;) {
for (i = tab->n_redundant; i < tab->n_row; ++i) {
struct isl_tab_var *var;
int sgn;
var = isl_tab_var_from_row(tab, i);
if (!var->is_nonneg)
continue;
sgn = sign_of_max(tab, var);
if (sgn < -1)
return isl_bool_error;
if (sgn != 0)
return isl_bool_false;
if (close_row(tab, var, 0) < 0)
return isl_bool_error;
break;
}
if (tab->n_dead == tab->n_col)
return isl_bool_true;
if (i == tab->n_row)
return isl_bool_false;
}
}
int isl_tab_sample_is_integer(struct isl_tab *tab)
{
int i;
if (!tab)
return -1;
for (i = 0; i < tab->n_var; ++i) {
int row;
if (!tab->var[i].is_row)
continue;
row = tab->var[i].index;
if (!isl_int_is_divisible_by(tab->mat->row[row][1],
tab->mat->row[row][0]))
return 0;
}
return 1;
}
static struct isl_vec *extract_integer_sample(struct isl_tab *tab)
{
int i;
struct isl_vec *vec;
vec = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_var);
if (!vec)
return NULL;
isl_int_set_si(vec->block.data[0], 1);
for (i = 0; i < tab->n_var; ++i) {
if (!tab->var[i].is_row)
isl_int_set_si(vec->block.data[1 + i], 0);
else {
int row = tab->var[i].index;
isl_int_divexact(vec->block.data[1 + i],
tab->mat->row[row][1], tab->mat->row[row][0]);
}
}
return vec;
}
__isl_give isl_vec *isl_tab_get_sample_value(struct isl_tab *tab)
{
int i;
struct isl_vec *vec;
isl_int m;
if (!tab)
return NULL;
vec = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_var);
if (!vec)
return NULL;
isl_int_init(m);
isl_int_set_si(vec->block.data[0], 1);
for (i = 0; i < tab->n_var; ++i) {
int row;
if (!tab->var[i].is_row) {
isl_int_set_si(vec->block.data[1 + i], 0);
continue;
}
row = tab->var[i].index;
isl_int_gcd(m, vec->block.data[0], tab->mat->row[row][0]);
isl_int_divexact(m, tab->mat->row[row][0], m);
isl_seq_scale(vec->block.data, vec->block.data, m, 1 + i);
isl_int_divexact(m, vec->block.data[0], tab->mat->row[row][0]);
isl_int_mul(vec->block.data[1 + i], m, tab->mat->row[row][1]);
}
vec = isl_vec_normalize(vec);
isl_int_clear(m);
return vec;
}
/* Store the sample value of "var" of "tab" rounded up (if sgn > 0)
* or down (if sgn < 0) to the nearest integer in *v.
*/
static void get_rounded_sample_value(struct isl_tab *tab,
struct isl_tab_var *var, int sgn, isl_int *v)
{
if (!var->is_row)
isl_int_set_si(*v, 0);
else if (sgn > 0)
isl_int_cdiv_q(*v, tab->mat->row[var->index][1],
tab->mat->row[var->index][0]);
else
isl_int_fdiv_q(*v, tab->mat->row[var->index][1],
tab->mat->row[var->index][0]);
}
/* Update "bmap" based on the results of the tableau "tab".
* In particular, implicit equalities are made explicit, redundant constraints
* are removed and if the sample value happens to be integer, it is stored
* in "bmap" (unless "bmap" already had an integer sample).
*
* The tableau is assumed to have been created from "bmap" using
* isl_tab_from_basic_map.
*/
__isl_give isl_basic_map *isl_basic_map_update_from_tab(
__isl_take isl_basic_map *bmap, struct isl_tab *tab)
{
int i;
unsigned n_eq;
if (!bmap)
return NULL;
if (!tab)
return bmap;
n_eq = tab->n_eq;
if (tab->empty)
bmap = isl_basic_map_set_to_empty(bmap);
else
for (i = bmap->n_ineq - 1; i >= 0; --i) {
if (isl_tab_is_equality(tab, n_eq + i))
isl_basic_map_inequality_to_equality(bmap, i);
else if (isl_tab_is_redundant(tab, n_eq + i))
isl_basic_map_drop_inequality(bmap, i);
}
if (bmap->n_eq != n_eq)
bmap = isl_basic_map_gauss(bmap, NULL);
if (!tab->rational &&
bmap && !bmap->sample && isl_tab_sample_is_integer(tab))
bmap->sample = extract_integer_sample(tab);
return bmap;
}
__isl_give isl_basic_set *isl_basic_set_update_from_tab(
__isl_take isl_basic_set *bset, struct isl_tab *tab)
{
return bset_from_bmap(isl_basic_map_update_from_tab(bset_to_bmap(bset),
tab));
}
/* Drop the last constraint added to "tab" in position "r".
* The constraint is expected to have remained in a row.
*/
static isl_stat drop_last_con_in_row(struct isl_tab *tab, int r)
{
if (!tab->con[r].is_row)
isl_die(isl_tab_get_ctx(tab), isl_error_internal,
"row unexpectedly moved to column",
return isl_stat_error);
if (r + 1 != tab->n_con)
isl_die(isl_tab_get_ctx(tab), isl_error_internal,
"additional constraints added", return isl_stat_error);
if (drop_row(tab, tab->con[r].index) < 0)
return isl_stat_error;
return isl_stat_ok;
}
/* Given a non-negative variable "var", temporarily add a new non-negative
* variable that is the opposite of "var", ensuring that "var" can only attain
* the value zero. The new variable is removed again before this function
* returns. However, the effect of forcing "var" to be zero remains.
* If var = n/d is a row variable, then the new variable = -n/d.
* If var is a column variables, then the new variable = -var.
* If the new variable cannot attain non-negative values, then
* the resulting tableau is empty.
* Otherwise, we know the value will be zero and we close the row.
*/
static isl_stat cut_to_hyperplane(struct isl_tab *tab, struct isl_tab_var *var)
{
unsigned r;
isl_int *row;
int sgn;
unsigned off = 2 + tab->M;
if (var->is_zero)
return isl_stat_ok;
if (var->is_redundant || !var->is_nonneg)
isl_die(isl_tab_get_ctx(tab), isl_error_invalid,
"expecting non-redundant non-negative variable",
return isl_stat_error);
if (isl_tab_extend_cons(tab, 1) < 0)
return isl_stat_error;
r = tab->n_con;
tab->con[r].index = tab->n_row;
tab->con[r].is_row = 1;
tab->con[r].is_nonneg = 0;
tab->con[r].is_zero = 0;
tab->con[r].is_redundant = 0;
tab->con[r].frozen = 0;
tab->con[r].negated = 0;
tab->row_var[tab->n_row] = ~r;
row = tab->mat->row[tab->n_row];
if (var->is_row) {
isl_int_set(row[0], tab->mat->row[var->index][0]);
isl_seq_neg(row + 1,
tab->mat->row[var->index] + 1, 1 + tab->n_col);
} else {
isl_int_set_si(row[0], 1);
isl_seq_clr(row + 1, 1 + tab->n_col);
isl_int_set_si(row[off + var->index], -1);
}
tab->n_row++;
tab->n_con++;
sgn = sign_of_max(tab, &tab->con[r]);
if (sgn < -1)
return isl_stat_error;
if (sgn < 0) {
if (drop_last_con_in_row(tab, r) < 0)
return isl_stat_error;
if (isl_tab_mark_empty(tab) < 0)
return isl_stat_error;
return isl_stat_ok;
}
tab->con[r].is_nonneg = 1;
/* sgn == 0 */
if (close_row(tab, &tab->con[r], 1) < 0)
return isl_stat_error;
if (drop_last_con_in_row(tab, r) < 0)
return isl_stat_error;
return isl_stat_ok;
}
/* Check that "con" is a valid constraint position for "tab".
*/
static isl_stat isl_tab_check_con(struct isl_tab *tab, int con)
{
if (!tab)
return isl_stat_error;
if (con < 0 || con >= tab->n_con)
isl_die(isl_tab_get_ctx(tab), isl_error_invalid,
"position out of bounds", return isl_stat_error);
return isl_stat_ok;
}
/* Given a tableau "tab" and an inequality constraint "con" of the tableau,
* relax the inequality by one. That is, the inequality r >= 0 is replaced
* by r' = r + 1 >= 0.
* If r is a row variable, we simply increase the constant term by one
* (taking into account the denominator).
* If r is a column variable, then we need to modify each row that
* refers to r = r' - 1 by substituting this equality, effectively
* subtracting the coefficient of the column from the constant.
* We should only do this if the minimum is manifestly unbounded,
* however. Otherwise, we may end up with negative sample values
* for non-negative variables.
* So, if r is a column variable with a minimum that is not
* manifestly unbounded, then we need to move it to a row.
* However, the sample value of this row may be negative,
* even after the relaxation, so we need to restore it.
* We therefore prefer to pivot a column up to a row, if possible.
*/
int isl_tab_relax(struct isl_tab *tab, int con)
{
struct isl_tab_var *var;
if (!tab)
return -1;
var = &tab->con[con];
if (var->is_row && (var->index < 0 || var->index < tab->n_redundant))
isl_die(tab->mat->ctx, isl_error_invalid,
"cannot relax redundant constraint", return -1);
if (!var->is_row && (var->index < 0 || var->index < tab->n_dead))
isl_die(tab->mat->ctx, isl_error_invalid,
"cannot relax dead constraint", return -1);
if (!var->is_row && !max_is_manifestly_unbounded(tab, var))
if (to_row(tab, var, 1) < 0)
return -1;
if (!var->is_row && !min_is_manifestly_unbounded(tab, var))
if (to_row(tab, var, -1) < 0)
return -1;
if (var->is_row) {
isl_int_add(tab->mat->row[var->index][1],
tab->mat->row[var->index][1], tab->mat->row[var->index][0]);
if (restore_row(tab, var) < 0)
return -1;
} else {
int i;
unsigned off = 2 + tab->M;
for (i = 0; i < tab->n_row; ++i) {
if (isl_int_is_zero(tab->mat->row[i][off + var->index]))
continue;
isl_int_sub(tab->mat->row[i][1], tab->mat->row[i][1],
tab->mat->row[i][off + var->index]);
}
}
if (isl_tab_push_var(tab, isl_tab_undo_relax, var) < 0)
return -1;
return 0;
}
/* Replace the variable v at position "pos" in the tableau "tab"
* by v' = v + shift.
*
* If the variable is in a column, then we first check if we can
* simply plug in v = v' - shift. The effect on a row with
* coefficient f/d for variable v is that the constant term c/d
* is replaced by (c - f * shift)/d. If shift is positive and
* f is negative for each row that needs to remain non-negative,
* then this is clearly safe. In other words, if the minimum of v
* is manifestly unbounded, then we can keep v in a column position.
* Otherwise, we can pivot it down to a row.
* Similarly, if shift is negative, we need to check if the maximum
* of is manifestly unbounded.
*
* If the variable is in a row (from the start or after pivoting),
* then the constant term c/d is replaced by (c + d * shift)/d.
*/
int isl_tab_shift_var(struct isl_tab *tab, int pos, isl_int shift)
{
struct isl_tab_var *var;
if (!tab)
return -1;
if (isl_int_is_zero(shift))
return 0;
var = &tab->var[pos];
if (!var->is_row) {
if (isl_int_is_neg(shift)) {
if (!max_is_manifestly_unbounded(tab, var))
if (to_row(tab, var, 1) < 0)
return -1;
} else {
if (!min_is_manifestly_unbounded(tab, var))
if (to_row(tab, var, -1) < 0)
return -1;
}
}
if (var->is_row) {
isl_int_addmul(tab->mat->row[var->index][1],
shift, tab->mat->row[var->index][0]);
} else {
int i;
unsigned off = 2 + tab->M;
for (i = 0; i < tab->n_row; ++i) {
if (isl_int_is_zero(tab->mat->row[i][off + var->index]))
continue;
isl_int_submul(tab->mat->row[i][1],
shift, tab->mat->row[i][off + var->index]);
}
}
return 0;
}
/* Remove the sign constraint from constraint "con".
*
* If the constraint variable was originally marked non-negative,
* then we make sure we mark it non-negative again during rollback.
*/
int isl_tab_unrestrict(struct isl_tab *tab, int con)
{
struct isl_tab_var *var;
if (!tab)
return -1;
var = &tab->con[con];
if (!var->is_nonneg)
return 0;
var->is_nonneg = 0;
if (isl_tab_push_var(tab, isl_tab_undo_unrestrict, var) < 0)
return -1;
return 0;
}
int isl_tab_select_facet(struct isl_tab *tab, int con)
{
if (!tab)
return -1;
return cut_to_hyperplane(tab, &tab->con[con]);
}
static int may_be_equality(struct isl_tab *tab, int row)
{
return tab->rational ? isl_int_is_zero(tab->mat->row[row][1])
: isl_int_lt(tab->mat->row[row][1],
tab->mat->row[row][0]);
}
/* Return an isl_tab_var that has been marked or NULL if no such
* variable can be found.
* The marked field has only been set for variables that
* appear in non-redundant rows or non-dead columns.
*
* Pick the last constraint variable that is marked and
* that appears in either a non-redundant row or a non-dead columns.
* Since the returned variable is tested for being a redundant constraint or
* an implicit equality, there is no need to return any tab variable that
* corresponds to a variable.
*/
static struct isl_tab_var *select_marked(struct isl_tab *tab)
{
int i;
struct isl_tab_var *var;
for (i = tab->n_con - 1; i >= 0; --i) {
var = &tab->con[i];
if (var->index < 0)
continue;
if (var->is_row && var->index < tab->n_redundant)
continue;
if (!var->is_row && var->index < tab->n_dead)
continue;
if (var->marked)
return var;
}
return NULL;
}
/* Check for (near) equalities among the constraints.
* A constraint is an equality if it is non-negative and if
* its maximal value is either
* - zero (in case of rational tableaus), or
* - strictly less than 1 (in case of integer tableaus)
*
* We first mark all non-redundant and non-dead variables that
* are not frozen and not obviously not an equality.
* Then we iterate over all marked variables if they can attain
* any values larger than zero or at least one.
* If the maximal value is zero, we mark any column variables
* that appear in the row as being zero and mark the row as being redundant.
* Otherwise, if the maximal value is strictly less than one (and the
* tableau is integer), then we restrict the value to being zero
* by adding an opposite non-negative variable.
* The order in which the variables are considered is not important.
*/
int isl_tab_detect_implicit_equalities(struct isl_tab *tab)
{
int i;
unsigned n_marked;
if (!tab)
return -1;
if (tab->empty)
return 0;
if (tab->n_dead == tab->n_col)
return 0;
n_marked = 0;
for (i = tab->n_redundant; i < tab->n_row; ++i) {
struct isl_tab_var *var = isl_tab_var_from_row(tab, i);
var->marked = !var->frozen && var->is_nonneg &&
may_be_equality(tab, i);
if (var->marked)
n_marked++;
}
for (i = tab->n_dead; i < tab->n_col; ++i) {
struct isl_tab_var *var = var_from_col(tab, i);
var->marked = !var->frozen && var->is_nonneg;
if (var->marked)
n_marked++;
}
while (n_marked) {
struct isl_tab_var *var;
int sgn;
var = select_marked(tab);
if (!var)
break;
var->marked = 0;
n_marked--;
sgn = sign_of_max(tab, var);
if (sgn < 0)
return -1;
if (sgn == 0) {
if (close_row(tab, var, 0) < 0)
return -1;
} else if (!tab->rational && !at_least_one(tab, var)) {
if (cut_to_hyperplane(tab, var) < 0)
return -1;
return isl_tab_detect_implicit_equalities(tab);
}
for (i = tab->n_redundant; i < tab->n_row; ++i) {
var = isl_tab_var_from_row(tab, i);
if (!var->marked)
continue;
if (may_be_equality(tab, i))
continue;
var->marked = 0;
n_marked--;
}
}
return 0;
}
/* Update the element of row_var or col_var that corresponds to
* constraint tab->con[i] to a move from position "old" to position "i".
*/
static int update_con_after_move(struct isl_tab *tab, int i, int old)
{
int *p;
int index;
index = tab->con[i].index;
if (index == -1)
return 0;
p = tab->con[i].is_row ? tab->row_var : tab->col_var;
if (p[index] != ~old)
isl_die(tab->mat->ctx, isl_error_internal,
"broken internal state", return -1);
p[index] = ~i;
return 0;
}
/* Interchange constraints "con1" and "con2" in "tab".
* In particular, interchange the contents of these entries in tab->con.
* Since tab->col_var and tab->row_var point back into this array,
* they need to be updated accordingly.
*/
isl_stat isl_tab_swap_constraints(struct isl_tab *tab, int con1, int con2)
{
struct isl_tab_var var;
if (isl_tab_check_con(tab, con1) < 0 ||
isl_tab_check_con(tab, con2) < 0)
return isl_stat_error;
var = tab->con[con1];
tab->con[con1] = tab->con[con2];
if (update_con_after_move(tab, con1, con2) < 0)
return isl_stat_error;
tab->con[con2] = var;
if (update_con_after_move(tab, con2, con1) < 0)
return isl_stat_error;
return isl_stat_ok;
}
/* Rotate the "n" constraints starting at "first" to the right,
* putting the last constraint in the position of the first constraint.
*/
static int rotate_constraints(struct isl_tab *tab, int first, int n)
{
int i, last;
struct isl_tab_var var;
if (n <= 1)
return 0;
last = first + n - 1;
var = tab->con[last];
for (i = last; i > first; --i) {
tab->con[i] = tab->con[i - 1];
if (update_con_after_move(tab, i, i - 1) < 0)
return -1;
}
tab->con[first] = var;
if (update_con_after_move(tab, first, last) < 0)
return -1;
return 0;
}
/* Drop the "n" entries starting at position "first" in tab->con, moving all
* subsequent entries down.
* Since some of the entries of tab->row_var and tab->col_var contain
* indices into this array, they have to be updated accordingly.
*/
static isl_stat con_drop_entries(struct isl_tab *tab,
unsigned first, unsigned n)
{
int i;
if (first + n > tab->n_con || first + n < first)
isl_die(isl_tab_get_ctx(tab), isl_error_internal,
"invalid range", return isl_stat_error);
tab->n_con -= n;
for (i = first; i < tab->n_con; ++i) {
tab->con[i] = tab->con[i + n];
if (update_con_after_move(tab, i, i + n) < 0)
return isl_stat_error;
}
return isl_stat_ok;
}
/* isl_basic_map_gauss5 callback that gets called when
* two (equality) constraints "a" and "b" get interchanged
* in the basic map. Perform the same interchange in "tab".
*/
static isl_stat swap_eq(unsigned a, unsigned b, void *user)
{
struct isl_tab *tab = user;
return isl_tab_swap_constraints(tab, a, b);
}
/* isl_basic_map_gauss5 callback that gets called when
* the final "n" equality constraints get removed.
* As a special case, if "n" is equal to the total number
* of equality constraints, then this means the basic map
* turned out to be empty.
* Drop the same number of equality constraints from "tab" or
* mark it empty in the special case.
*/
static isl_stat drop_eq(unsigned n, void *user)
{
struct isl_tab *tab = user;
if (tab->n_eq == n)
return isl_tab_mark_empty(tab);
tab->n_eq -= n;
return con_drop_entries(tab, tab->n_eq, n);
}
/* If "bmap" has more than a single reference, then call
* isl_basic_map_gauss on it, updating "tab" accordingly.
*/
static __isl_give isl_basic_map *gauss_if_shared(__isl_take isl_basic_map *bmap,
struct isl_tab *tab)
{
isl_bool single;
single = isl_basic_map_has_single_reference(bmap);
if (single < 0)
return isl_basic_map_free(bmap);
if (single)
return bmap;
return isl_basic_map_gauss5(bmap, NULL, &swap_eq, &drop_eq, tab);
}
/* Make the equalities that are implicit in "bmap" but that have been
* detected in the corresponding "tab" explicit in "bmap" and update
* "tab" to reflect the new order of the constraints.
*
* In particular, if inequality i is an implicit equality then
* isl_basic_map_inequality_to_equality will move the inequality
* in front of the other equality and it will move the last inequality
* in the position of inequality i.
* In the tableau, the inequalities of "bmap" are stored after the equalities
* and so the original order
*
* E E E E E A A A I B B B B L
*
* is changed into
*
* I E E E E E A A A L B B B B
*
* where I is the implicit equality, the E are equalities,
* the A inequalities before I, the B inequalities after I and
* L the last inequality.
* We therefore need to rotate to the right two sets of constraints,
* those up to and including I and those after I.
*
* If "tab" contains any constraints that are not in "bmap" then they
* appear after those in "bmap" and they should be left untouched.
*
* Note that this function only calls isl_basic_map_gauss
* (in case some equality constraints got detected)
* if "bmap" has more than one reference.
* If it only has a single reference, then it is left in a temporary state,
* because the caller may require this state.
* Calling isl_basic_map_gauss is then the responsibility of the caller.
*/
__isl_give isl_basic_map *isl_tab_make_equalities_explicit(struct isl_tab *tab,
__isl_take isl_basic_map *bmap)
{
int i;
unsigned n_eq;
if (!tab || !bmap)
return isl_basic_map_free(bmap);
if (tab->empty)
return bmap;
n_eq = tab->n_eq;
for (i = bmap->n_ineq - 1; i >= 0; --i) {
if (!isl_tab_is_equality(tab, bmap->n_eq + i))
continue;
isl_basic_map_inequality_to_equality(bmap, i);
if (rotate_constraints(tab, 0, tab->n_eq + i + 1) < 0)
return isl_basic_map_free(bmap);
if (rotate_constraints(tab, tab->n_eq + i + 1,
bmap->n_ineq - i) < 0)
return isl_basic_map_free(bmap);
tab->n_eq++;
}
if (n_eq != tab->n_eq)
bmap = gauss_if_shared(bmap, tab);
return bmap;
}
static int con_is_redundant(struct isl_tab *tab, struct isl_tab_var *var)
{
if (!tab)
return -1;
if (tab->rational) {
int sgn = sign_of_min(tab, var);
if (sgn < -1)
return -1;
return sgn >= 0;
} else {
int irred = isl_tab_min_at_most_neg_one(tab, var);
if (irred < 0)
return -1;
return !irred;
}
}
/* Check for (near) redundant constraints.
* A constraint is redundant if it is non-negative and if
* its minimal value (temporarily ignoring the non-negativity) is either
* - zero (in case of rational tableaus), or
* - strictly larger than -1 (in case of integer tableaus)
*
* We first mark all non-redundant and non-dead variables that
* are not frozen and not obviously negatively unbounded.
* Then we iterate over all marked variables if they can attain
* any values smaller than zero or at most negative one.
* If not, we mark the row as being redundant (assuming it hasn't
* been detected as being obviously redundant in the mean time).
*/
int isl_tab_detect_redundant(struct isl_tab *tab)
{
int i;
unsigned n_marked;
if (!tab)
return -1;
if (tab->empty)
return 0;
if (tab->n_redundant == tab->n_row)
return 0;
n_marked = 0;
for (i = tab->n_redundant; i < tab->n_row; ++i) {
struct isl_tab_var *var = isl_tab_var_from_row(tab, i);
var->marked = !var->frozen && var->is_nonneg;
if (var->marked)
n_marked++;
}
for (i = tab->n_dead; i < tab->n_col; ++i) {
struct isl_tab_var *var = var_from_col(tab, i);
var->marked = !var->frozen && var->is_nonneg &&
!min_is_manifestly_unbounded(tab, var);
if (var->marked)
n_marked++;
}
while (n_marked) {
struct isl_tab_var *var;
int red;
var = select_marked(tab);
if (!var)
break;
var->marked = 0;
n_marked--;
red = con_is_redundant(tab, var);
if (red < 0)
return -1;
if (red && !var->is_redundant)
if (isl_tab_mark_redundant(tab, var->index) < 0)
return -1;
for (i = tab->n_dead; i < tab->n_col; ++i) {
var = var_from_col(tab, i);
if (!var->marked)
continue;
if (!min_is_manifestly_unbounded(tab, var))
continue;
var->marked = 0;
n_marked--;
}
}
return 0;
}
int isl_tab_is_equality(struct isl_tab *tab, int con)
{
int row;
unsigned off;
if (!tab)
return -1;
if (tab->con[con].is_zero)
return 1;
if (tab->con[con].is_redundant)
return 0;
if (!tab->con[con].is_row)
return tab->con[con].index < tab->n_dead;
row = tab->con[con].index;
off = 2 + tab->M;
return isl_int_is_zero(tab->mat->row[row][1]) &&
!row_is_big(tab, row) &&
isl_seq_first_non_zero(tab->mat->row[row] + off + tab->n_dead,
tab->n_col - tab->n_dead) == -1;
}
/* Return the minimal value of the affine expression "f" with denominator
* "denom" in *opt, *opt_denom, assuming the tableau is not empty and
* the expression cannot attain arbitrarily small values.
* If opt_denom is NULL, then *opt is rounded up to the nearest integer.
* The return value reflects the nature of the result (empty, unbounded,
* minimal value returned in *opt).
*
* This function assumes that at least one more row and at least
* one more element in the constraint array are available in the tableau.
*/
enum isl_lp_result isl_tab_min(struct isl_tab *tab,
isl_int *f, isl_int denom, isl_int *opt, isl_int *opt_denom,
unsigned flags)
{
int r;
enum isl_lp_result res = isl_lp_ok;
struct isl_tab_var *var;
struct isl_tab_undo *snap;
if (!tab)
return isl_lp_error;
if (tab->empty)
return isl_lp_empty;
snap = isl_tab_snap(tab);
r = isl_tab_add_row(tab, f);
if (r < 0)
return isl_lp_error;
var = &tab->con[r];
for (;;) {
int row, col;
find_pivot(tab, var, var, -1, &row, &col);
if (row == var->index) {
res = isl_lp_unbounded;
break;
}
if (row == -1)
break;
if (isl_tab_pivot(tab, row, col) < 0)
return isl_lp_error;
}
isl_int_mul(tab->mat->row[var->index][0],
tab->mat->row[var->index][0], denom);
if (ISL_FL_ISSET(flags, ISL_TAB_SAVE_DUAL)) {
int i;
isl_vec_free(tab->dual);
tab->dual = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_con);
if (!tab->dual)
return isl_lp_error;
isl_int_set(tab->dual->el[0], tab->mat->row[var->index][0]);
for (i = 0; i < tab->n_con; ++i) {
int pos;
if (tab->con[i].is_row) {
isl_int_set_si(tab->dual->el[1 + i], 0);
continue;
}
pos = 2 + tab->M + tab->con[i].index;
if (tab->con[i].negated)
isl_int_neg(tab->dual->el[1 + i],
tab->mat->row[var->index][pos]);
else
isl_int_set(tab->dual->el[1 + i],
tab->mat->row[var->index][pos]);
}
}
if (opt && res == isl_lp_ok) {
if (opt_denom) {
isl_int_set(*opt, tab->mat->row[var->index][1]);
isl_int_set(*opt_denom, tab->mat->row[var->index][0]);
} else
get_rounded_sample_value(tab, var, 1, opt);
}
if (isl_tab_rollback(tab, snap) < 0)
return isl_lp_error;
return res;
}
/* Is the constraint at position "con" marked as being redundant?
* If it is marked as representing an equality, then it is not
* considered to be redundant.
* Note that isl_tab_mark_redundant marks both the isl_tab_var as
* redundant and moves the corresponding row into the first
* tab->n_redundant positions (or removes the row, assigning it index -1),
* so the final test is actually redundant itself.
*/
int isl_tab_is_redundant(struct isl_tab *tab, int con)
{
if (isl_tab_check_con(tab, con) < 0)
return -1;
if (tab->con[con].is_zero)
return 0;
if (tab->con[con].is_redundant)
return 1;
return tab->con[con].is_row && tab->con[con].index < tab->n_redundant;
}
/* Is variable "var" of "tab" fixed to a constant value by its row
* in the tableau?
* If so and if "value" is not NULL, then store this constant value
* in "value".
*
* That is, is it a row variable that only has non-zero coefficients
* for dead columns?
*/
static isl_bool is_constant(struct isl_tab *tab, struct isl_tab_var *var,
isl_int *value)
{
unsigned off = 2 + tab->M;
isl_mat *mat = tab->mat;
int n;
int row;
int pos;
if (!var->is_row)
return isl_bool_false;
row = var->index;
if (row_is_big(tab, row))
return isl_bool_false;
n = tab->n_col - tab->n_dead;
pos = isl_seq_first_non_zero(mat->row[row] + off + tab->n_dead, n);
if (pos != -1)
return isl_bool_false;
if (value)
isl_int_divexact(*value, mat->row[row][1], mat->row[row][0]);
return isl_bool_true;
}
/* Has the variable "var' of "tab" reached a value that is greater than
* or equal (if sgn > 0) or smaller than or equal (if sgn < 0) to "target"?
* "tmp" has been initialized by the caller and can be used
* to perform local computations.
*
* If the sample value involves the big parameter, then any value
* is reached.
* Otherwise check if n/d >= t, i.e., n >= d * t (if sgn > 0)
* or n/d <= t, i.e., n <= d * t (if sgn < 0).
*/
static int reached(struct isl_tab *tab, struct isl_tab_var *var, int sgn,
isl_int target, isl_int *tmp)
{
if (row_is_big(tab, var->index))
return 1;
isl_int_mul(*tmp, tab->mat->row[var->index][0], target);
if (sgn > 0)
return isl_int_ge(tab->mat->row[var->index][1], *tmp);
else
return isl_int_le(tab->mat->row[var->index][1], *tmp);
}
/* Can variable "var" of "tab" attain the value "target" by
* pivoting up (if sgn > 0) or down (if sgn < 0)?
* If not, then pivot up [down] to the greatest [smallest]
* rational value.
* "tmp" has been initialized by the caller and can be used
* to perform local computations.
*
* If the variable is manifestly unbounded in the desired direction,
* then it can attain any value.
* Otherwise, it can be moved to a row.
* Continue pivoting until the target is reached.
* If no more pivoting can be performed, the maximal [minimal]
* rational value has been reached and the target cannot be reached.
* If the variable would be pivoted into a manifestly unbounded column,
* then the target can be reached.
*/
static isl_bool var_reaches(struct isl_tab *tab, struct isl_tab_var *var,
int sgn, isl_int target, isl_int *tmp)
{
int row, col;
if (sgn < 0 && min_is_manifestly_unbounded(tab, var))
return isl_bool_true;
if (sgn > 0 && max_is_manifestly_unbounded(tab, var))
return isl_bool_true;
if (to_row(tab, var, sgn) < 0)
return isl_bool_error;
while (!reached(tab, var, sgn, target, tmp)) {
find_pivot(tab, var, var, sgn, &row, &col);
if (row == -1)
return isl_bool_false;
if (row == var->index)
return isl_bool_true;
if (isl_tab_pivot(tab, row, col) < 0)
return isl_bool_error;
}
return isl_bool_true;
}
/* Check if variable "var" of "tab" can only attain a single (integer)
* value, and, if so, add an equality constraint to fix the variable
* to this single value and store the result in "target".
* "target" and "tmp" have been initialized by the caller.
*
* Given the current sample value, round it down and check
* whether it is possible to attain a strictly smaller integer value.
* If so, the variable is not restricted to a single integer value.
* Otherwise, the search stops at the smallest rational value.
* Round up this value and check whether it is possible to attain
* a strictly greater integer value.
* If so, the variable is not restricted to a single integer value.
* Otherwise, the search stops at the greatest rational value.
* If rounding down this value yields a value that is different
* from rounding up the smallest rational value, then the variable
* cannot attain any integer value. Mark the tableau empty.
* Otherwise, add an equality constraint that fixes the variable
* to the single integer value found.
*/
static isl_bool detect_constant_with_tmp(struct isl_tab *tab,
struct isl_tab_var *var, isl_int *target, isl_int *tmp)
{
isl_bool reached;
isl_vec *eq;
int pos;
isl_stat r;
get_rounded_sample_value(tab, var, -1, target);
isl_int_sub_ui(*target, *target, 1);
reached = var_reaches(tab, var, -1, *target, tmp);
if (reached < 0 || reached)
return isl_bool_not(reached);
get_rounded_sample_value(tab, var, 1, target);
isl_int_add_ui(*target, *target, 1);
reached = var_reaches(tab, var, 1, *target, tmp);
if (reached < 0 || reached)
return isl_bool_not(reached);
get_rounded_sample_value(tab, var, -1, tmp);
isl_int_sub_ui(*target, *target, 1);
if (isl_int_ne(*target, *tmp)) {
if (isl_tab_mark_empty(tab) < 0)
return isl_bool_error;
return isl_bool_false;
}
if (isl_tab_extend_cons(tab, 1) < 0)
return isl_bool_error;
eq = isl_vec_alloc(isl_tab_get_ctx(tab), 1 + tab->n_var);
if (!eq)
return isl_bool_error;
pos = var - tab->var;
isl_seq_clr(eq->el + 1, tab->n_var);
isl_int_set_si(eq->el[1 + pos], -1);
isl_int_set(eq->el[0], *target);
r = isl_tab_add_eq(tab, eq->el);
isl_vec_free(eq);
return r < 0 ? isl_bool_error : isl_bool_true;
}
/* Check if variable "var" of "tab" can only attain a single (integer)
* value, and, if so, add an equality constraint to fix the variable
* to this single value and store the result in "value" (if "value"
* is not NULL).
*
* If the current sample value involves the big parameter,
* then the variable cannot have a fixed integer value.
* If the variable is already fixed to a single value by its row, then
* there is no need to add another equality constraint.
*
* Otherwise, allocate some temporary variables and continue
* with detect_constant_with_tmp.
*/
static isl_bool get_constant(struct isl_tab *tab, struct isl_tab_var *var,
isl_int *value)
{
isl_int target, tmp;
isl_bool is_cst;
if (var->is_row && row_is_big(tab, var->index))
return isl_bool_false;
is_cst = is_constant(tab, var, value);
if (is_cst < 0 || is_cst)
return is_cst;
if (!value)
isl_int_init(target);
isl_int_init(tmp);
is_cst = detect_constant_with_tmp(tab, var,
value ? value : &target, &tmp);
isl_int_clear(tmp);
if (!value)
isl_int_clear(target);
return is_cst;
}
/* Check if variable "var" of "tab" can only attain a single (integer)
* value, and, if so, add an equality constraint to fix the variable
* to this single value and store the result in "value" (if "value"
* is not NULL).
*
* For rational tableaus, nothing needs to be done.
*/
isl_bool isl_tab_is_constant(struct isl_tab *tab, int var, isl_int *value)
{
if (!tab)
return isl_bool_error;
if (var < 0 || var >= tab->n_var)
isl_die(isl_tab_get_ctx(tab), isl_error_invalid,
"position out of bounds", return isl_bool_error);
if (tab->rational)
return isl_bool_false;
return get_constant(tab, &tab->var[var], value);
}
/* Check if any of the variables of "tab" can only attain a single (integer)
* value, and, if so, add equality constraints to fix those variables
* to these single values.
*
* For rational tableaus, nothing needs to be done.
*/
isl_stat isl_tab_detect_constants(struct isl_tab *tab)
{
int i;
if (!tab)
return isl_stat_error;
if (tab->rational)
return isl_stat_ok;
for (i = 0; i < tab->n_var; ++i) {
if (get_constant(tab, &tab->var[i], NULL) < 0)
return isl_stat_error;
}
return isl_stat_ok;
}
/* Take a snapshot of the tableau that can be restored by a call to
* isl_tab_rollback.
*/
struct isl_tab_undo *isl_tab_snap(struct isl_tab *tab)
{
if (!tab)
return NULL;
tab->need_undo = 1;
return tab->top;
}
/* Does "tab" need to keep track of undo information?
* That is, was a snapshot taken that may need to be restored?
*/
isl_bool isl_tab_need_undo(struct isl_tab *tab)
{
if (!tab)
return isl_bool_error;
return isl_bool_ok(tab->need_undo);
}
/* Remove all tracking of undo information from "tab", invalidating
* any snapshots that may have been taken of the tableau.
* Since all snapshots have been invalidated, there is also
* no need to start keeping track of undo information again.
*/
void isl_tab_clear_undo(struct isl_tab *tab)
{
if (!tab)
return;
free_undo(tab);
tab->need_undo = 0;
}
/* Undo the operation performed by isl_tab_relax.
*/
static isl_stat unrelax(struct isl_tab *tab, struct isl_tab_var *var)
WARN_UNUSED;
static isl_stat unrelax(struct isl_tab *tab, struct isl_tab_var *var)
{
unsigned off = 2 + tab->M;
if (!var->is_row && !max_is_manifestly_unbounded(tab, var))
if (to_row(tab, var, 1) < 0)
return isl_stat_error;
if (var->is_row) {
isl_int_sub(tab->mat->row[var->index][1],
tab->mat->row[var->index][1], tab->mat->row[var->index][0]);
if (var->is_nonneg) {
int sgn = restore_row(tab, var);
isl_assert(tab->mat->ctx, sgn >= 0,
return isl_stat_error);
}
} else {
int i;
for (i = 0; i < tab->n_row; ++i) {
if (isl_int_is_zero(tab->mat->row[i][off + var->index]))
continue;
isl_int_add(tab->mat->row[i][1], tab->mat->row[i][1],
tab->mat->row[i][off + var->index]);
}
}
return isl_stat_ok;
}
/* Undo the operation performed by isl_tab_unrestrict.
*
* In particular, mark the variable as being non-negative and make
* sure the sample value respects this constraint.
*/
static isl_stat ununrestrict(struct isl_tab *tab, struct isl_tab_var *var)
{
var->is_nonneg = 1;
if (var->is_row && restore_row(tab, var) < -1)
return isl_stat_error;
return isl_stat_ok;
}
/* Unmark the last redundant row in "tab" as being redundant.
* This undoes part of the modifications performed by isl_tab_mark_redundant.
* In particular, remove the redundant mark and make
* sure the sample value respects the constraint again.
* A variable that is marked non-negative by isl_tab_mark_redundant
* is covered by a separate undo record.
*/
static isl_stat restore_last_redundant(struct isl_tab *tab)
{
struct isl_tab_var *var;
if (tab->n_redundant < 1)
isl_die(isl_tab_get_ctx(tab), isl_error_internal,
"no redundant rows", return isl_stat_error);
var = isl_tab_var_from_row(tab, tab->n_redundant - 1);
var->is_redundant = 0;
tab->n_redundant--;
restore_row(tab, var);
return isl_stat_ok;
}
static isl_stat perform_undo_var(struct isl_tab *tab, struct isl_tab_undo *undo)
WARN_UNUSED;
static isl_stat perform_undo_var(struct isl_tab *tab, struct isl_tab_undo *undo)
{
struct isl_tab_var *var = var_from_index(tab, undo->u.var_index);
switch (undo->type) {
case isl_tab_undo_nonneg:
var->is_nonneg = 0;
break;
case isl_tab_undo_redundant:
if (!var->is_row || var->index != tab->n_redundant - 1)
isl_die(isl_tab_get_ctx(tab), isl_error_internal,
"not undoing last redundant row",
return isl_stat_error);
return restore_last_redundant(tab);
case isl_tab_undo_freeze:
var->frozen = 0;
break;
case isl_tab_undo_zero:
var->is_zero = 0;
if (!var->is_row)
tab->n_dead--;
break;
case isl_tab_undo_allocate:
if (undo->u.var_index >= 0) {
isl_assert(tab->mat->ctx, !var->is_row,
return isl_stat_error);
return drop_col(tab, var->index);
}
if (!var->is_row) {
if (!max_is_manifestly_unbounded(tab, var)) {
if (to_row(tab, var, 1) < 0)
return isl_stat_error;
} else if (!min_is_manifestly_unbounded(tab, var)) {
if (to_row(tab, var, -1) < 0)
return isl_stat_error;
} else
if (to_row(tab, var, 0) < 0)
return isl_stat_error;
}
return drop_row(tab, var->index);
case isl_tab_undo_relax:
return unrelax(tab, var);
case isl_tab_undo_unrestrict:
return ununrestrict(tab, var);
default:
isl_die(tab->mat->ctx, isl_error_internal,
"perform_undo_var called on invalid undo record",
return isl_stat_error);
}
return isl_stat_ok;
}
/* Restore all rows that have been marked redundant by isl_tab_mark_redundant
* and that have been preserved in the tableau.
* Note that isl_tab_mark_redundant may also have marked some variables
* as being non-negative before marking them redundant. These need
* to be removed as well as otherwise some constraints could end up
* getting marked redundant with respect to the variable.
*/
isl_stat isl_tab_restore_redundant(struct isl_tab *tab)
{
if (!tab)
return isl_stat_error;
if (tab->need_undo)
isl_die(isl_tab_get_ctx(tab), isl_error_invalid,
"manually restoring redundant constraints "
"interferes with undo history",
return isl_stat_error);
while (tab->n_redundant > 0) {
if (tab->row_var[tab->n_redundant - 1] >= 0) {
struct isl_tab_var *var;
var = isl_tab_var_from_row(tab, tab->n_redundant - 1);
var->is_nonneg = 0;
}
restore_last_redundant(tab);
}
return isl_stat_ok;
}
/* Undo the addition of an integer division to the basic map representation
* of "tab" in position "pos".
*/
static isl_stat drop_bmap_div(struct isl_tab *tab, int pos)
{
int off;
isl_size n_div;
n_div = isl_basic_map_dim(tab->bmap, isl_dim_div);
if (n_div < 0)
return isl_stat_error;
off = tab->n_var - n_div;
tab->bmap = isl_basic_map_drop_div(tab->bmap, pos - off);
if (!tab->bmap)
return isl_stat_error;
if (tab->samples) {
tab->samples = isl_mat_drop_cols(tab->samples, 1 + pos, 1);
if (!tab->samples)
return isl_stat_error;
}
return isl_stat_ok;
}
/* Restore the tableau to the state where the basic variables
* are those in "col_var".
* We first construct a list of variables that are currently in
* the basis, but shouldn't. Then we iterate over all variables
* that should be in the basis and for each one that is currently
* not in the basis, we exchange it with one of the elements of the
* list constructed before.
* We can always find an appropriate variable to pivot with because
* the current basis is mapped to the old basis by a non-singular
* matrix and so we can never end up with a zero row.
*/
static int restore_basis(struct isl_tab *tab, int *col_var)
{
int i, j;
int n_extra = 0;
int *extra = NULL; /* current columns that contain bad stuff */
unsigned off = 2 + tab->M;
extra = isl_alloc_array(tab->mat->ctx, int, tab->n_col);
if (tab->n_col && !extra)
goto error;
for (i = 0; i < tab->n_col; ++i) {
for (j = 0; j < tab->n_col; ++j)
if (tab->col_var[i] == col_var[j])
break;
if (j < tab->n_col)
continue;
extra[n_extra++] = i;
}
for (i = 0; i < tab->n_col && n_extra > 0; ++i) {
struct isl_tab_var *var;
int row;
for (j = 0; j < tab->n_col; ++j)
if (col_var[i] == tab->col_var[j])
break;
if (j < tab->n_col)
continue;
var = var_from_index(tab, col_var[i]);
row = var->index;
for (j = 0; j < n_extra; ++j)
if (!isl_int_is_zero(tab->mat->row[row][off+extra[j]]))
break;
isl_assert(tab->mat->ctx, j < n_extra, goto error);
if (isl_tab_pivot(tab, row, extra[j]) < 0)
goto error;
extra[j] = extra[--n_extra];
}
free(extra);
return 0;
error:
free(extra);
return -1;
}
/* Remove all samples with index n or greater, i.e., those samples
* that were added since we saved this number of samples in
* isl_tab_save_samples.
*/
static void drop_samples_since(struct isl_tab *tab, int n)
{
int i;
for (i = tab->n_sample - 1; i >= 0 && tab->n_sample > n; --i) {
if (tab->sample_index[i] < n)
continue;
if (i != tab->n_sample - 1) {
int t = tab->sample_index[tab->n_sample-1];
tab->sample_index[tab->n_sample-1] = tab->sample_index[i];
tab->sample_index[i] = t;
isl_mat_swap_rows(tab->samples, tab->n_sample-1, i);
}
tab->n_sample--;
}
}
static isl_stat perform_undo(struct isl_tab *tab, struct isl_tab_undo *undo)
WARN_UNUSED;
static isl_stat perform_undo(struct isl_tab *tab, struct isl_tab_undo *undo)
{
switch (undo->type) {
case isl_tab_undo_rational:
tab->rational = 0;
break;
case isl_tab_undo_empty:
tab->empty = 0;
break;
case isl_tab_undo_nonneg:
case isl_tab_undo_redundant:
case isl_tab_undo_freeze:
case isl_tab_undo_zero:
case isl_tab_undo_allocate:
case isl_tab_undo_relax:
case isl_tab_undo_unrestrict:
return perform_undo_var(tab, undo);
case isl_tab_undo_bmap_eq:
tab->bmap = isl_basic_map_free_equality(tab->bmap, 1);
return tab->bmap ? isl_stat_ok : isl_stat_error;
case isl_tab_undo_bmap_ineq:
tab->bmap = isl_basic_map_free_inequality(tab->bmap, 1);
return tab->bmap ? isl_stat_ok : isl_stat_error;
case isl_tab_undo_bmap_div:
return drop_bmap_div(tab, undo->u.var_index);
case isl_tab_undo_saved_basis:
if (restore_basis(tab, undo->u.col_var) < 0)
return isl_stat_error;
break;
case isl_tab_undo_drop_sample:
tab->n_outside--;
break;
case isl_tab_undo_saved_samples:
drop_samples_since(tab, undo->u.n);
break;
case isl_tab_undo_callback:
return undo->u.callback->run(undo->u.callback);
default:
isl_assert(tab->mat->ctx, 0, return isl_stat_error);
}
return isl_stat_ok;
}
/* Return the tableau to the state it was in when the snapshot "snap"
* was taken.
*/
isl_stat isl_tab_rollback(struct isl_tab *tab, struct isl_tab_undo *snap)
{
struct isl_tab_undo *undo, *next;
if (!tab)
return isl_stat_error;
tab->in_undo = 1;
for (undo = tab->top; undo && undo != &tab->bottom; undo = next) {
next = undo->next;
if (undo == snap)
break;
if (perform_undo(tab, undo) < 0) {
tab->top = undo;
free_undo(tab);
tab->in_undo = 0;
return isl_stat_error;
}
free_undo_record(undo);
}
tab->in_undo = 0;
tab->top = undo;
if (!undo)
return isl_stat_error;
return isl_stat_ok;
}
/* The given row "row" represents an inequality violated by all
* points in the tableau. Check for some special cases of such
* separating constraints.
* In particular, if the row has been reduced to the constant -1,
* then we know the inequality is adjacent (but opposite) to
* an equality in the tableau.
* If the row has been reduced to r = c*(-1 -r'), with r' an inequality
* of the tableau and c a positive constant, then the inequality
* is adjacent (but opposite) to the inequality r'.
*/
static enum isl_ineq_type separation_type(struct isl_tab *tab, unsigned row)
{
int pos;
unsigned off = 2 + tab->M;
if (tab->rational)
return isl_ineq_separate;
if (!isl_int_is_one(tab->mat->row[row][0]))
return isl_ineq_separate;
pos = isl_seq_first_non_zero(tab->mat->row[row] + off + tab->n_dead,
tab->n_col - tab->n_dead);
if (pos == -1) {
if (isl_int_is_negone(tab->mat->row[row][1]))
return isl_ineq_adj_eq;
else
return isl_ineq_separate;
}
if (!isl_int_eq(tab->mat->row[row][1],
tab->mat->row[row][off + tab->n_dead + pos]))
return isl_ineq_separate;
pos = isl_seq_first_non_zero(
tab->mat->row[row] + off + tab->n_dead + pos + 1,
tab->n_col - tab->n_dead - pos - 1);
return pos == -1 ? isl_ineq_adj_ineq : isl_ineq_separate;
}
/* Check the effect of inequality "ineq" on the tableau "tab".
* The result may be
* isl_ineq_redundant: satisfied by all points in the tableau
* isl_ineq_separate: satisfied by no point in the tableau
* isl_ineq_cut: satisfied by some by not all points
* isl_ineq_adj_eq: adjacent to an equality
* isl_ineq_adj_ineq: adjacent to an inequality.
*/
enum isl_ineq_type isl_tab_ineq_type(struct isl_tab *tab, isl_int *ineq)
{
enum isl_ineq_type type = isl_ineq_error;
struct isl_tab_undo *snap = NULL;
int con;
int row;
if (!tab)
return isl_ineq_error;
if (isl_tab_extend_cons(tab, 1) < 0)
return isl_ineq_error;
snap = isl_tab_snap(tab);
con = isl_tab_add_row(tab, ineq);
if (con < 0)
goto error;
row = tab->con[con].index;
if (isl_tab_row_is_redundant(tab, row))
type = isl_ineq_redundant;
else if (isl_int_is_neg(tab->mat->row[row][1]) &&
(tab->rational ||
isl_int_abs_ge(tab->mat->row[row][1],
tab->mat->row[row][0]))) {
int nonneg = at_least_zero(tab, &tab->con[con]);
if (nonneg < 0)
goto error;
if (nonneg)
type = isl_ineq_cut;
else
type = separation_type(tab, row);
} else {
int red = con_is_redundant(tab, &tab->con[con]);
if (red < 0)
goto error;
if (!red)
type = isl_ineq_cut;
else
type = isl_ineq_redundant;
}
if (isl_tab_rollback(tab, snap))
return isl_ineq_error;
return type;
error:
return isl_ineq_error;
}
isl_stat isl_tab_track_bmap(struct isl_tab *tab, __isl_take isl_basic_map *bmap)
{
bmap = isl_basic_map_cow(bmap);
if (!tab || !bmap)
goto error;
if (tab->empty) {
bmap = isl_basic_map_set_to_empty(bmap);
if (!bmap)
goto error;
tab->bmap = bmap;
return isl_stat_ok;
}
isl_assert(tab->mat->ctx, tab->n_eq == bmap->n_eq, goto error);
isl_assert(tab->mat->ctx,
tab->n_con == bmap->n_eq + bmap->n_ineq, goto error);
tab->bmap = bmap;
return isl_stat_ok;
error:
isl_basic_map_free(bmap);
return isl_stat_error;
}
isl_stat isl_tab_track_bset(struct isl_tab *tab, __isl_take isl_basic_set *bset)
{
return isl_tab_track_bmap(tab, bset_to_bmap(bset));
}
__isl_keep isl_basic_set *isl_tab_peek_bset(struct isl_tab *tab)
{
if (!tab)
return NULL;
return bset_from_bmap(tab->bmap);
}
static void isl_tab_print_internal(__isl_keep struct isl_tab *tab,
FILE *out, int indent)
{
unsigned r, c;
int i;
if (!tab) {
fprintf(out, "%*snull tab\n", indent, "");
return;
}
fprintf(out, "%*sn_redundant: %d, n_dead: %d", indent, "",
tab->n_redundant, tab->n_dead);
if (tab->rational)
fprintf(out, ", rational");
if (tab->empty)
fprintf(out, ", empty");
fprintf(out, "\n");
fprintf(out, "%*s[", indent, "");
for (i = 0; i < tab->n_var; ++i) {
if (i)
fprintf(out, (i == tab->n_param ||
i == tab->n_var - tab->n_div) ? "; "
: ", ");
fprintf(out, "%c%d%s", tab->var[i].is_row ? 'r' : 'c',
tab->var[i].index,
tab->var[i].is_zero ? " [=0]" :
tab->var[i].is_redundant ? " [R]" : "");
}
fprintf(out, "]\n");
fprintf(out, "%*s[", indent, "");
for (i = 0; i < tab->n_con; ++i) {
if (i)
fprintf(out, ", ");
fprintf(out, "%c%d%s", tab->con[i].is_row ? 'r' : 'c',
tab->con[i].index,
tab->con[i].is_zero ? " [=0]" :
tab->con[i].is_redundant ? " [R]" : "");
}
fprintf(out, "]\n");
fprintf(out, "%*s[", indent, "");
for (i = 0; i < tab->n_row; ++i) {
const char *sign = "";
if (i)
fprintf(out, ", ");
if (tab->row_sign) {
if (tab->row_sign[i] == isl_tab_row_unknown)
sign = "?";
else if (tab->row_sign[i] == isl_tab_row_neg)
sign = "-";
else if (tab->row_sign[i] == isl_tab_row_pos)
sign = "+";
else
sign = "+-";
}
fprintf(out, "r%d: %d%s%s", i, tab->row_var[i],
isl_tab_var_from_row(tab, i)->is_nonneg ? " [>=0]" : "", sign);
}
fprintf(out, "]\n");
fprintf(out, "%*s[", indent, "");
for (i = 0; i < tab->n_col; ++i) {
if (i)
fprintf(out, ", ");
fprintf(out, "c%d: %d%s", i, tab->col_var[i],
var_from_col(tab, i)->is_nonneg ? " [>=0]" : "");
}
fprintf(out, "]\n");
r = tab->mat->n_row;
tab->mat->n_row = tab->n_row;
c = tab->mat->n_col;
tab->mat->n_col = 2 + tab->M + tab->n_col;
isl_mat_print_internal(tab->mat, out, indent);
tab->mat->n_row = r;
tab->mat->n_col = c;
if (tab->bmap)
isl_basic_map_print_internal(tab->bmap, out, indent);
}
void isl_tab_dump(__isl_keep struct isl_tab *tab)
{
isl_tab_print_internal(tab, stderr, 0);
}
|