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
| /*
* Copyright 2010-2011 INRIA Saclay
* Copyright 2011 Sven Verdoolaege
* Copyright 2012-2014 Ecole Normale Superieure
*
* Use of this software is governed by the MIT license
*
* Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
* Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
* 91893 Orsay, France
* and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
*/
#include <isl/id.h>
#include <isl/aff.h>
#include <isl_sort.h>
#include <isl_val_private.h>
#include <isl_pw_macro.h>
#ifdef HAS_TYPE
__isl_give PW *FN(PW,alloc_size)(__isl_take isl_space *dim,
enum isl_fold type, int n)
#else
__isl_give PW *FN(PW,alloc_size)(__isl_take isl_space *dim, int n)
#endif
{
isl_ctx *ctx;
struct PW *pw;
if (!dim)
return NULL;
ctx = isl_space_get_ctx(dim);
isl_assert(ctx, n >= 0, goto error);
pw = isl_alloc(ctx, struct PW,
sizeof(struct PW) + (n - 1) * sizeof(S(PW,piece)));
if (!pw)
goto error;
pw->ref = 1;
#ifdef HAS_TYPE
pw->type = type;
#endif
pw->size = n;
pw->n = 0;
pw->dim = dim;
return pw;
error:
isl_space_free(dim);
return NULL;
}
#ifdef HAS_TYPE
__isl_give PW *FN(PW,ZERO)(__isl_take isl_space *dim, enum isl_fold type)
{
return FN(PW,alloc_size)(dim, type, 0);
}
#else
__isl_give PW *FN(PW,ZERO)(__isl_take isl_space *dim)
{
return FN(PW,alloc_size)(dim, 0);
}
#endif
__isl_give PW *FN(PW,add_piece)(__isl_take PW *pw,
__isl_take isl_set *set, __isl_take EL *el)
{
isl_ctx *ctx;
isl_space *el_dim = NULL;
if (!pw || !set || !el)
goto error;
if (isl_set_plain_is_empty(set) || FN(EL,EL_IS_ZERO)(el)) {
isl_set_free(set);
FN(EL,free)(el);
return pw;
}
ctx = isl_set_get_ctx(set);
#ifdef HAS_TYPE
if (pw->type != el->type)
isl_die(ctx, isl_error_invalid, "fold types don't match",
goto error);
#endif
el_dim = FN(EL,get_space(el));
isl_assert(ctx, isl_space_is_equal(pw->dim, el_dim), goto error);
isl_assert(ctx, pw->n < pw->size, goto error);
pw->p[pw->n].set = set;
pw->p[pw->n].FIELD = el;
pw->n++;
isl_space_free(el_dim);
return pw;
error:
isl_space_free(el_dim);
FN(PW,free)(pw);
isl_set_free(set);
FN(EL,free)(el);
return NULL;
}
/* Does the space of "set" correspond to that of the domain of "el".
*/
static isl_bool FN(PW,compatible_domain)(__isl_keep EL *el,
__isl_keep isl_set *set)
{
isl_bool ok;
isl_space *el_space, *set_space;
if (!set || !el)
return isl_bool_error;
set_space = isl_set_get_space(set);
el_space = FN(EL,get_space)(el);
ok = isl_space_is_domain_internal(set_space, el_space);
isl_space_free(el_space);
isl_space_free(set_space);
return ok;
}
/* Check that the space of "set" corresponds to that of the domain of "el".
*/
static isl_stat FN(PW,check_compatible_domain)(__isl_keep EL *el,
__isl_keep isl_set *set)
{
isl_bool ok;
ok = FN(PW,compatible_domain)(el, set);
if (ok < 0)
return isl_stat_error;
if (!ok)
isl_die(isl_set_get_ctx(set), isl_error_invalid,
"incompatible spaces", return isl_stat_error);
return isl_stat_ok;
}
#ifdef HAS_TYPE
__isl_give PW *FN(PW,alloc)(enum isl_fold type,
__isl_take isl_set *set, __isl_take EL *el)
#else
__isl_give PW *FN(PW,alloc)(__isl_take isl_set *set, __isl_take EL *el)
#endif
{
PW *pw;
if (FN(PW,check_compatible_domain)(el, set) < 0)
goto error;
#ifdef HAS_TYPE
pw = FN(PW,alloc_size)(FN(EL,get_space)(el), type, 1);
#else
pw = FN(PW,alloc_size)(FN(EL,get_space)(el), 1);
#endif
return FN(PW,add_piece)(pw, set, el);
error:
isl_set_free(set);
FN(EL,free)(el);
return NULL;
}
__isl_give PW *FN(PW,dup)(__isl_keep PW *pw)
{
int i;
PW *dup;
if (!pw)
return NULL;
#ifdef HAS_TYPE
dup = FN(PW,alloc_size)(isl_space_copy(pw->dim), pw->type, pw->n);
#else
dup = FN(PW,alloc_size)(isl_space_copy(pw->dim), pw->n);
#endif
if (!dup)
return NULL;
for (i = 0; i < pw->n; ++i)
dup = FN(PW,add_piece)(dup, isl_set_copy(pw->p[i].set),
FN(EL,copy)(pw->p[i].FIELD));
return dup;
}
__isl_give PW *FN(PW,cow)(__isl_take PW *pw)
{
if (!pw)
return NULL;
if (pw->ref == 1)
return pw;
pw->ref--;
return FN(PW,dup)(pw);
}
__isl_give PW *FN(PW,copy)(__isl_keep PW *pw)
{
if (!pw)
return NULL;
pw->ref++;
return pw;
}
__isl_null PW *FN(PW,free)(__isl_take PW *pw)
{
int i;
if (!pw)
return NULL;
if (--pw->ref > 0)
return NULL;
for (i = 0; i < pw->n; ++i) {
isl_set_free(pw->p[i].set);
FN(EL,free)(pw->p[i].FIELD);
}
isl_space_free(pw->dim);
free(pw);
return NULL;
}
const char *FN(PW,get_dim_name)(__isl_keep PW *pw, enum isl_dim_type type,
unsigned pos)
{
return pw ? isl_space_get_dim_name(pw->dim, type, pos) : NULL;
}
isl_bool FN(PW,has_dim_id)(__isl_keep PW *pw, enum isl_dim_type type,
unsigned pos)
{
return pw ? isl_space_has_dim_id(pw->dim, type, pos) : isl_bool_error;
}
__isl_give isl_id *FN(PW,get_dim_id)(__isl_keep PW *pw, enum isl_dim_type type,
unsigned pos)
{
return pw ? isl_space_get_dim_id(pw->dim, type, pos) : NULL;
}
isl_bool FN(PW,has_tuple_name)(__isl_keep PW *pw, enum isl_dim_type type)
{
return pw ? isl_space_has_tuple_name(pw->dim, type) : isl_bool_error;
}
const char *FN(PW,get_tuple_name)(__isl_keep PW *pw, enum isl_dim_type type)
{
return pw ? isl_space_get_tuple_name(pw->dim, type) : NULL;
}
isl_bool FN(PW,has_tuple_id)(__isl_keep PW *pw, enum isl_dim_type type)
{
return pw ? isl_space_has_tuple_id(pw->dim, type) : isl_bool_error;
}
__isl_give isl_id *FN(PW,get_tuple_id)(__isl_keep PW *pw, enum isl_dim_type type)
{
return pw ? isl_space_get_tuple_id(pw->dim, type) : NULL;
}
isl_bool FN(PW,IS_ZERO)(__isl_keep PW *pw)
{
if (!pw)
return isl_bool_error;
return pw->n == 0;
}
#ifndef NO_REALIGN
__isl_give PW *FN(PW,realign_domain)(__isl_take PW *pw,
__isl_take isl_reordering *exp)
{
int i;
pw = FN(PW,cow)(pw);
if (!pw || !exp)
goto error;
for (i = 0; i < pw->n; ++i) {
pw->p[i].set = isl_set_realign(pw->p[i].set,
isl_reordering_copy(exp));
if (!pw->p[i].set)
goto error;
pw->p[i].FIELD = FN(EL,realign_domain)(pw->p[i].FIELD,
isl_reordering_copy(exp));
if (!pw->p[i].FIELD)
goto error;
}
pw = FN(PW,reset_domain_space)(pw, isl_reordering_get_space(exp));
isl_reordering_free(exp);
return pw;
error:
isl_reordering_free(exp);
FN(PW,free)(pw);
return NULL;
}
/* Check that "pw" has only named parameters, reporting an error
* if it does not.
*/
isl_stat FN(PW,check_named_params)(__isl_keep PW *pw)
{
return isl_space_check_named_params(FN(PW,peek_space)(pw));
}
/* Align the parameters of "pw" to those of "model".
*/
__isl_give PW *FN(PW,align_params)(__isl_take PW *pw, __isl_take isl_space *model)
{
isl_ctx *ctx;
isl_bool equal_params;
if (!pw || !model)
goto error;
ctx = isl_space_get_ctx(model);
if (!isl_space_has_named_params(model))
isl_die(ctx, isl_error_invalid,
"model has unnamed parameters", goto error);
if (FN(PW,check_named_params)(pw) < 0)
goto error;
equal_params = isl_space_has_equal_params(pw->dim, model);
if (equal_params < 0)
goto error;
if (!equal_params) {
isl_reordering *exp;
exp = isl_parameter_alignment_reordering(pw->dim, model);
exp = isl_reordering_extend_space(exp,
FN(PW,get_domain_space)(pw));
pw = FN(PW,realign_domain)(pw, exp);
}
isl_space_free(model);
return pw;
error:
isl_space_free(model);
FN(PW,free)(pw);
return NULL;
}
static __isl_give PW *FN(PW,align_params_pw_pw_and)(__isl_take PW *pw1,
__isl_take PW *pw2,
__isl_give PW *(*fn)(__isl_take PW *pw1, __isl_take PW *pw2))
{
isl_bool equal_params;
if (!pw1 || !pw2)
goto error;
equal_params = isl_space_has_equal_params(pw1->dim, pw2->dim);
if (equal_params < 0)
goto error;
if (equal_params)
return fn(pw1, pw2);
if (FN(PW,check_named_params)(pw1) < 0 ||
FN(PW,check_named_params)(pw2) < 0)
goto error;
pw1 = FN(PW,align_params)(pw1, FN(PW,get_space)(pw2));
pw2 = FN(PW,align_params)(pw2, FN(PW,get_space)(pw1));
return fn(pw1, pw2);
error:
FN(PW,free)(pw1);
FN(PW,free)(pw2);
return NULL;
}
static __isl_give PW *FN(PW,align_params_pw_set_and)(__isl_take PW *pw,
__isl_take isl_set *set,
__isl_give PW *(*fn)(__isl_take PW *pw, __isl_take isl_set *set))
{
isl_ctx *ctx;
isl_bool aligned;
if (!pw || !set)
goto error;
aligned = isl_set_space_has_equal_params(set, pw->dim);
if (aligned < 0)
goto error;
if (aligned)
return fn(pw, set);
ctx = FN(PW,get_ctx)(pw);
if (FN(PW,check_named_params)(pw) < 0)
goto error;
if (!isl_space_has_named_params(set->dim))
isl_die(ctx, isl_error_invalid,
"unaligned unnamed parameters", goto error);
pw = FN(PW,align_params)(pw, isl_set_get_space(set));
set = isl_set_align_params(set, FN(PW,get_space)(pw));
return fn(pw, set);
error:
FN(PW,free)(pw);
isl_set_free(set);
return NULL;
}
#endif
static __isl_give PW *FN(PW,union_add_aligned)(__isl_take PW *pw1,
__isl_take PW *pw2)
{
int i, j, n;
struct PW *res;
isl_ctx *ctx;
isl_set *set;
if (!pw1 || !pw2)
goto error;
ctx = isl_space_get_ctx(pw1->dim);
#ifdef HAS_TYPE
if (pw1->type != pw2->type)
isl_die(ctx, isl_error_invalid,
"fold types don't match", goto error);
#endif
isl_assert(ctx, isl_space_is_equal(pw1->dim, pw2->dim), goto error);
if (FN(PW,IS_ZERO)(pw1)) {
FN(PW,free)(pw1);
return pw2;
}
if (FN(PW,IS_ZERO)(pw2)) {
FN(PW,free)(pw2);
return pw1;
}
n = (pw1->n + 1) * (pw2->n + 1);
#ifdef HAS_TYPE
res = FN(PW,alloc_size)(isl_space_copy(pw1->dim), pw1->type, n);
#else
res = FN(PW,alloc_size)(isl_space_copy(pw1->dim), n);
#endif
for (i = 0; i < pw1->n; ++i) {
set = isl_set_copy(pw1->p[i].set);
for (j = 0; j < pw2->n; ++j) {
struct isl_set *common;
EL *sum;
common = isl_set_intersect(isl_set_copy(pw1->p[i].set),
isl_set_copy(pw2->p[j].set));
if (isl_set_plain_is_empty(common)) {
isl_set_free(common);
continue;
}
set = isl_set_subtract(set,
isl_set_copy(pw2->p[j].set));
sum = FN(EL,add_on_domain)(common,
FN(EL,copy)(pw1->p[i].FIELD),
FN(EL,copy)(pw2->p[j].FIELD));
res = FN(PW,add_piece)(res, common, sum);
}
res = FN(PW,add_piece)(res, set, FN(EL,copy)(pw1->p[i].FIELD));
}
for (j = 0; j < pw2->n; ++j) {
set = isl_set_copy(pw2->p[j].set);
for (i = 0; i < pw1->n; ++i)
set = isl_set_subtract(set,
isl_set_copy(pw1->p[i].set));
res = FN(PW,add_piece)(res, set, FN(EL,copy)(pw2->p[j].FIELD));
}
FN(PW,free)(pw1);
FN(PW,free)(pw2);
return res;
error:
FN(PW,free)(pw1);
FN(PW,free)(pw2);
return NULL;
}
/* Private version of "union_add". For isl_pw_qpolynomial and
* isl_pw_qpolynomial_fold, we prefer to simply call it "add".
*/
static __isl_give PW *FN(PW,union_add_)(__isl_take PW *pw1, __isl_take PW *pw2)
{
return FN(PW,align_params_pw_pw_and)(pw1, pw2,
&FN(PW,union_add_aligned));
}
/* Make sure "pw" has room for at least "n" more pieces.
*
* If there is only one reference to pw, we extend it in place.
* Otherwise, we create a new PW and copy the pieces.
*/
static __isl_give PW *FN(PW,grow)(__isl_take PW *pw, int n)
{
int i;
isl_ctx *ctx;
PW *res;
if (!pw)
return NULL;
if (pw->n + n <= pw->size)
return pw;
ctx = FN(PW,get_ctx)(pw);
n += pw->n;
if (pw->ref == 1) {
res = isl_realloc(ctx, pw, struct PW,
sizeof(struct PW) + (n - 1) * sizeof(S(PW,piece)));
if (!res)
return FN(PW,free)(pw);
res->size = n;
return res;
}
#ifdef HAS_TYPE
res = FN(PW,alloc_size)(isl_space_copy(pw->dim), pw->type, n);
#else
res = FN(PW,alloc_size)(isl_space_copy(pw->dim), n);
#endif
if (!res)
return FN(PW,free)(pw);
for (i = 0; i < pw->n; ++i)
res = FN(PW,add_piece)(res, isl_set_copy(pw->p[i].set),
FN(EL,copy)(pw->p[i].FIELD));
FN(PW,free)(pw);
return res;
}
static __isl_give PW *FN(PW,add_disjoint_aligned)(__isl_take PW *pw1,
__isl_take PW *pw2)
{
int i;
isl_ctx *ctx;
if (!pw1 || !pw2)
goto error;
if (pw1->size < pw1->n + pw2->n && pw1->n < pw2->n)
return FN(PW,add_disjoint_aligned)(pw2, pw1);
ctx = isl_space_get_ctx(pw1->dim);
#ifdef HAS_TYPE
if (pw1->type != pw2->type)
isl_die(ctx, isl_error_invalid,
"fold types don't match", goto error);
#endif
isl_assert(ctx, isl_space_is_equal(pw1->dim, pw2->dim), goto error);
if (FN(PW,IS_ZERO)(pw1)) {
FN(PW,free)(pw1);
return pw2;
}
if (FN(PW,IS_ZERO)(pw2)) {
FN(PW,free)(pw2);
return pw1;
}
pw1 = FN(PW,grow)(pw1, pw2->n);
if (!pw1)
goto error;
for (i = 0; i < pw2->n; ++i)
pw1 = FN(PW,add_piece)(pw1,
isl_set_copy(pw2->p[i].set),
FN(EL,copy)(pw2->p[i].FIELD));
FN(PW,free)(pw2);
return pw1;
error:
FN(PW,free)(pw1);
FN(PW,free)(pw2);
return NULL;
}
__isl_give PW *FN(PW,add_disjoint)(__isl_take PW *pw1, __isl_take PW *pw2)
{
return FN(PW,align_params_pw_pw_and)(pw1, pw2,
&FN(PW,add_disjoint_aligned));
}
/* This function is currently only used from isl_aff.c
*/
static __isl_give PW *FN(PW,on_shared_domain_in)(__isl_take PW *pw1,
__isl_take PW *pw2, __isl_take isl_space *space,
__isl_give EL *(*fn)(__isl_take EL *el1, __isl_take EL *el2))
__attribute__ ((unused));
/* Apply "fn" to pairs of elements from pw1 and pw2 on shared domains.
* The result of "fn" (and therefore also of this function) lives in "space".
*/
static __isl_give PW *FN(PW,on_shared_domain_in)(__isl_take PW *pw1,
__isl_take PW *pw2, __isl_take isl_space *space,
__isl_give EL *(*fn)(__isl_take EL *el1, __isl_take EL *el2))
{
int i, j, n;
PW *res = NULL;
if (!pw1 || !pw2)
goto error;
n = pw1->n * pw2->n;
#ifdef HAS_TYPE
res = FN(PW,alloc_size)(isl_space_copy(space), pw1->type, n);
#else
res = FN(PW,alloc_size)(isl_space_copy(space), n);
#endif
for (i = 0; i < pw1->n; ++i) {
for (j = 0; j < pw2->n; ++j) {
isl_set *common;
EL *res_ij;
int empty;
common = isl_set_intersect(
isl_set_copy(pw1->p[i].set),
isl_set_copy(pw2->p[j].set));
empty = isl_set_plain_is_empty(common);
if (empty < 0 || empty) {
isl_set_free(common);
if (empty < 0)
goto error;
continue;
}
res_ij = fn(FN(EL,copy)(pw1->p[i].FIELD),
FN(EL,copy)(pw2->p[j].FIELD));
res_ij = FN(EL,gist)(res_ij, isl_set_copy(common));
res = FN(PW,add_piece)(res, common, res_ij);
}
}
isl_space_free(space);
FN(PW,free)(pw1);
FN(PW,free)(pw2);
return res;
error:
isl_space_free(space);
FN(PW,free)(pw1);
FN(PW,free)(pw2);
FN(PW,free)(res);
return NULL;
}
/* This function is currently only used from isl_aff.c
*/
static __isl_give PW *FN(PW,on_shared_domain)(__isl_take PW *pw1,
__isl_take PW *pw2,
__isl_give EL *(*fn)(__isl_take EL *el1, __isl_take EL *el2))
__attribute__ ((unused));
/* Apply "fn" to pairs of elements from pw1 and pw2 on shared domains.
* The result of "fn" is assumed to live in the same space as "pw1" and "pw2".
*/
static __isl_give PW *FN(PW,on_shared_domain)(__isl_take PW *pw1,
__isl_take PW *pw2,
__isl_give EL *(*fn)(__isl_take EL *el1, __isl_take EL *el2))
{
isl_space *space;
if (!pw1 || !pw2)
goto error;
space = isl_space_copy(pw1->dim);
return FN(PW,on_shared_domain_in)(pw1, pw2, space, fn);
error:
FN(PW,free)(pw1);
FN(PW,free)(pw2);
return NULL;
}
#ifndef NO_NEG
__isl_give PW *FN(PW,neg)(__isl_take PW *pw)
{
int i;
if (!pw)
return NULL;
if (FN(PW,IS_ZERO)(pw))
return pw;
pw = FN(PW,cow)(pw);
if (!pw)
return NULL;
for (i = 0; i < pw->n; ++i) {
pw->p[i].FIELD = FN(EL,neg)(pw->p[i].FIELD);
if (!pw->p[i].FIELD)
return FN(PW,free)(pw);
}
return pw;
}
#endif
#ifndef NO_SUB
__isl_give PW *FN(PW,sub)(__isl_take PW *pw1, __isl_take PW *pw2)
{
return FN(PW,add)(pw1, FN(PW,neg)(pw2));
}
#endif
/* Return the parameter domain of "pw".
*/
__isl_give isl_set *FN(PW,params)(__isl_take PW *pw)
{
return isl_set_params(FN(PW,domain)(pw));
}
__isl_give isl_set *FN(PW,domain)(__isl_take PW *pw)
{
int i;
isl_set *dom;
if (!pw)
return NULL;
dom = isl_set_empty(FN(PW,get_domain_space)(pw));
for (i = 0; i < pw->n; ++i)
dom = isl_set_union_disjoint(dom, isl_set_copy(pw->p[i].set));
FN(PW,free)(pw);
return dom;
}
/* Exploit the equalities in the domain of piece "i" of "pw"
* to simplify the associated function.
* If the domain of piece "i" is empty, then remove it entirely,
* replacing it with the final piece.
*/
static int FN(PW,exploit_equalities_and_remove_if_empty)(__isl_keep PW *pw,
int i)
{
isl_basic_set *aff;
int empty = isl_set_plain_is_empty(pw->p[i].set);
if (empty < 0)
return -1;
if (empty) {
isl_set_free(pw->p[i].set);
FN(EL,free)(pw->p[i].FIELD);
if (i != pw->n - 1)
pw->p[i] = pw->p[pw->n - 1];
pw->n--;
return 0;
}
aff = isl_set_affine_hull(isl_set_copy(pw->p[i].set));
pw->p[i].FIELD = FN(EL,substitute_equalities)(pw->p[i].FIELD, aff);
if (!pw->p[i].FIELD)
return -1;
return 0;
}
/* Convert a piecewise expression defined over a parameter domain
* into one that is defined over a zero-dimensional set.
*/
__isl_give PW *FN(PW,from_range)(__isl_take PW *pw)
{
isl_space *space;
if (!pw)
return NULL;
if (!isl_space_is_set(pw->dim))
isl_die(FN(PW,get_ctx)(pw), isl_error_invalid,
"not living in a set space", return FN(PW,free)(pw));
space = FN(PW,get_space)(pw);
space = isl_space_from_range(space);
pw = FN(PW,reset_space)(pw, space);
return pw;
}
/* Fix the value of the given parameter or domain dimension of "pw"
* to be equal to "value".
*/
__isl_give PW *FN(PW,fix_si)(__isl_take PW *pw, enum isl_dim_type type,
unsigned pos, int value)
{
int i;
if (!pw)
return NULL;
if (type == isl_dim_out)
isl_die(FN(PW,get_ctx)(pw), isl_error_invalid,
"cannot fix output dimension", return FN(PW,free)(pw));
if (pw->n == 0)
return pw;
if (type == isl_dim_in)
type = isl_dim_set;
pw = FN(PW,cow)(pw);
if (!pw)
return FN(PW,free)(pw);
for (i = pw->n - 1; i >= 0; --i) {
pw->p[i].set = isl_set_fix_si(pw->p[i].set, type, pos, value);
if (FN(PW,exploit_equalities_and_remove_if_empty)(pw, i) < 0)
return FN(PW,free)(pw);
}
return pw;
}
/* Restrict the domain of "pw" by combining each cell
* with "set" through a call to "fn", where "fn" may be
* isl_set_intersect, isl_set_intersect_params or isl_set_subtract.
*/
static __isl_give PW *FN(PW,restrict_domain_aligned)(__isl_take PW *pw,
__isl_take isl_set *set,
__isl_give isl_set *(*fn)(__isl_take isl_set *set1,
__isl_take isl_set *set2))
{
int i;
if (!pw || !set)
goto error;
if (pw->n == 0) {
isl_set_free(set);
return pw;
}
pw = FN(PW,cow)(pw);
if (!pw)
goto error;
for (i = pw->n - 1; i >= 0; --i) {
pw->p[i].set = fn(pw->p[i].set, isl_set_copy(set));
if (FN(PW,exploit_equalities_and_remove_if_empty)(pw, i) < 0)
goto error;
}
isl_set_free(set);
return pw;
error:
isl_set_free(set);
FN(PW,free)(pw);
return NULL;
}
static __isl_give PW *FN(PW,intersect_domain_aligned)(__isl_take PW *pw,
__isl_take isl_set *set)
{
return FN(PW,restrict_domain_aligned)(pw, set, &isl_set_intersect);
}
__isl_give PW *FN(PW,intersect_domain)(__isl_take PW *pw,
__isl_take isl_set *context)
{
return FN(PW,align_params_pw_set_and)(pw, context,
&FN(PW,intersect_domain_aligned));
}
static __isl_give PW *FN(PW,intersect_params_aligned)(__isl_take PW *pw,
__isl_take isl_set *set)
{
return FN(PW,restrict_domain_aligned)(pw, set,
&isl_set_intersect_params);
}
/* Intersect the domain of "pw" with the parameter domain "context".
*/
__isl_give PW *FN(PW,intersect_params)(__isl_take PW *pw,
__isl_take isl_set *context)
{
return FN(PW,align_params_pw_set_and)(pw, context,
&FN(PW,intersect_params_aligned));
}
/* Subtract "domain' from the domain of "pw", assuming their
* parameters have been aligned.
*/
static __isl_give PW *FN(PW,subtract_domain_aligned)(__isl_take PW *pw,
__isl_take isl_set *domain)
{
return FN(PW,restrict_domain_aligned)(pw, domain, &isl_set_subtract);
}
/* Subtract "domain' from the domain of "pw".
*/
__isl_give PW *FN(PW,subtract_domain)(__isl_take PW *pw,
__isl_take isl_set *domain)
{
return FN(PW,align_params_pw_set_and)(pw, domain,
&FN(PW,subtract_domain_aligned));
}
/* Compute the gist of "pw" with respect to the domain constraints
* of "context" for the case where the domain of the last element
* of "pw" is equal to "context".
* Call "fn_el" to compute the gist of this element, replace
* its domain by the universe and drop all other elements
* as their domains are necessarily disjoint from "context".
*/
static __isl_give PW *FN(PW,gist_last)(__isl_take PW *pw,
__isl_take isl_set *context,
__isl_give EL *(*fn_el)(__isl_take EL *el, __isl_take isl_set *set))
{
int i;
isl_space *space;
for (i = 0; i < pw->n - 1; ++i) {
isl_set_free(pw->p[i].set);
FN(EL,free)(pw->p[i].FIELD);
}
pw->p[0].FIELD = pw->p[pw->n - 1].FIELD;
pw->p[0].set = pw->p[pw->n - 1].set;
pw->n = 1;
space = isl_set_get_space(context);
pw->p[0].FIELD = fn_el(pw->p[0].FIELD, context);
context = isl_set_universe(space);
isl_set_free(pw->p[0].set);
pw->p[0].set = context;
if (!pw->p[0].FIELD || !pw->p[0].set)
return FN(PW,free)(pw);
return pw;
}
/* Compute the gist of "pw" with respect to the domain constraints
* of "context". Call "fn_el" to compute the gist of the elements
* and "fn_dom" to compute the gist of the domains.
*
* If the piecewise expression is empty or the context is the universe,
* then nothing can be simplified.
*/
static __isl_give PW *FN(PW,gist_aligned)(__isl_take PW *pw,
__isl_take isl_set *context,
__isl_give EL *(*fn_el)(__isl_take EL *el,
__isl_take isl_set *set),
__isl_give isl_set *(*fn_dom)(__isl_take isl_set *set,
__isl_take isl_basic_set *bset))
{
int i;
int is_universe;
isl_bool aligned;
isl_basic_set *hull = NULL;
if (!pw || !context)
goto error;
if (pw->n == 0) {
isl_set_free(context);
return pw;
}
is_universe = isl_set_plain_is_universe(context);
if (is_universe < 0)
goto error;
if (is_universe) {
isl_set_free(context);
return pw;
}
aligned = isl_set_space_has_equal_params(context, pw->dim);
if (aligned < 0)
goto error;
if (!aligned) {
pw = FN(PW,align_params)(pw, isl_set_get_space(context));
context = isl_set_align_params(context, FN(PW,get_space)(pw));
}
pw = FN(PW,cow)(pw);
if (!pw)
goto error;
if (pw->n == 1) {
int equal;
equal = isl_set_plain_is_equal(pw->p[0].set, context);
if (equal < 0)
goto error;
if (equal)
return FN(PW,gist_last)(pw, context, fn_el);
}
context = isl_set_compute_divs(context);
hull = isl_set_simple_hull(isl_set_copy(context));
for (i = pw->n - 1; i >= 0; --i) {
isl_set *set_i;
int empty;
if (i == pw->n - 1) {
int equal;
equal = isl_set_plain_is_equal(pw->p[i].set, context);
if (equal < 0)
goto error;
if (equal) {
isl_basic_set_free(hull);
return FN(PW,gist_last)(pw, context, fn_el);
}
}
set_i = isl_set_intersect(isl_set_copy(pw->p[i].set),
isl_set_copy(context));
empty = isl_set_plain_is_empty(set_i);
pw->p[i].FIELD = fn_el(pw->p[i].FIELD, set_i);
pw->p[i].set = fn_dom(pw->p[i].set, isl_basic_set_copy(hull));
if (empty < 0 || !pw->p[i].FIELD || !pw->p[i].set)
goto error;
if (empty) {
isl_set_free(pw->p[i].set);
FN(EL,free)(pw->p[i].FIELD);
if (i != pw->n - 1)
pw->p[i] = pw->p[pw->n - 1];
pw->n--;
}
}
isl_basic_set_free(hull);
isl_set_free(context);
return pw;
error:
FN(PW,free)(pw);
isl_basic_set_free(hull);
isl_set_free(context);
return NULL;
}
static __isl_give PW *FN(PW,gist_domain_aligned)(__isl_take PW *pw,
__isl_take isl_set *set)
{
return FN(PW,gist_aligned)(pw, set, &FN(EL,gist),
&isl_set_gist_basic_set);
}
__isl_give PW *FN(PW,gist)(__isl_take PW *pw, __isl_take isl_set *context)
{
return FN(PW,align_params_pw_set_and)(pw, context,
&FN(PW,gist_domain_aligned));
}
static __isl_give PW *FN(PW,gist_params_aligned)(__isl_take PW *pw,
__isl_take isl_set *set)
{
return FN(PW,gist_aligned)(pw, set, &FN(EL,gist_params),
&isl_set_gist_params_basic_set);
}
__isl_give PW *FN(PW,gist_params)(__isl_take PW *pw,
__isl_take isl_set *context)
{
return FN(PW,align_params_pw_set_and)(pw, context,
&FN(PW,gist_params_aligned));
}
/* Return -1 if the piece "p1" should be sorted before "p2"
* and 1 if it should be sorted after "p2".
* Return 0 if they do not need to be sorted in a specific order.
*
* The two pieces are compared on the basis of their function value expressions.
*/
static int FN(PW,sort_field_cmp)(const void *p1, const void *p2, void *arg)
{
struct FN(PW,piece) const *pc1 = p1;
struct FN(PW,piece) const *pc2 = p2;
return FN(EL,plain_cmp)(pc1->FIELD, pc2->FIELD);
}
/* Sort the pieces of "pw" according to their function value
* expressions and then combine pairs of adjacent pieces with
* the same such expression.
*
* The sorting is performed in place because it does not
* change the meaning of "pw", but care needs to be
* taken not to change any possible other copies of "pw"
* in case anything goes wrong.
*/
__isl_give PW *FN(PW,sort)(__isl_take PW *pw)
{
int i, j;
isl_set *set;
if (!pw)
return NULL;
if (pw->n <= 1)
return pw;
if (isl_sort(pw->p, pw->n, sizeof(pw->p[0]),
&FN(PW,sort_field_cmp), NULL) < 0)
return FN(PW,free)(pw);
for (i = pw->n - 1; i >= 1; --i) {
if (!FN(EL,plain_is_equal)(pw->p[i - 1].FIELD, pw->p[i].FIELD))
continue;
set = isl_set_union(isl_set_copy(pw->p[i - 1].set),
isl_set_copy(pw->p[i].set));
if (!set)
return FN(PW,free)(pw);
isl_set_free(pw->p[i].set);
FN(EL,free)(pw->p[i].FIELD);
isl_set_free(pw->p[i - 1].set);
pw->p[i - 1].set = set;
for (j = i + 1; j < pw->n; ++j)
pw->p[j - 1] = pw->p[j];
pw->n--;
}
return pw;
}
/* Coalesce the domains of "pw".
*
* Prior to the actual coalescing, first sort the pieces such that
* pieces with the same function value expression are combined
* into a single piece, the combined domain of which can then
* be coalesced.
*/
__isl_give PW *FN(PW,coalesce)(__isl_take PW *pw)
{
int i;
pw = FN(PW,sort)(pw);
if (!pw)
return NULL;
for (i = 0; i < pw->n; ++i) {
pw->p[i].set = isl_set_coalesce(pw->p[i].set);
if (!pw->p[i].set)
goto error;
}
return pw;
error:
FN(PW,free)(pw);
return NULL;
}
isl_ctx *FN(PW,get_ctx)(__isl_keep PW *pw)
{
return pw ? isl_space_get_ctx(pw->dim) : NULL;
}
isl_bool FN(PW,involves_dims)(__isl_keep PW *pw, enum isl_dim_type type,
unsigned first, unsigned n)
{
int i;
enum isl_dim_type set_type;
if (!pw)
return isl_bool_error;
if (pw->n == 0 || n == 0)
return isl_bool_false;
set_type = type == isl_dim_in ? isl_dim_set : type;
for (i = 0; i < pw->n; ++i) {
isl_bool involves = FN(EL,involves_dims)(pw->p[i].FIELD,
type, first, n);
if (involves < 0 || involves)
return involves;
involves = isl_set_involves_dims(pw->p[i].set,
set_type, first, n);
if (involves < 0 || involves)
return involves;
}
return isl_bool_false;
}
__isl_give PW *FN(PW,set_dim_name)(__isl_take PW *pw,
enum isl_dim_type type, unsigned pos, const char *s)
{
int i;
enum isl_dim_type set_type;
pw = FN(PW,cow)(pw);
if (!pw)
return NULL;
set_type = type == isl_dim_in ? isl_dim_set : type;
pw->dim = isl_space_set_dim_name(pw->dim, type, pos, s);
if (!pw->dim)
goto error;
for (i = 0; i < pw->n; ++i) {
pw->p[i].set = isl_set_set_dim_name(pw->p[i].set,
set_type, pos, s);
if (!pw->p[i].set)
goto error;
pw->p[i].FIELD = FN(EL,set_dim_name)(pw->p[i].FIELD, type, pos, s);
if (!pw->p[i].FIELD)
goto error;
}
return pw;
error:
FN(PW,free)(pw);
return NULL;
}
__isl_give PW *FN(PW,drop_dims)(__isl_take PW *pw,
enum isl_dim_type type, unsigned first, unsigned n)
{
int i;
enum isl_dim_type set_type;
if (!pw)
return NULL;
if (n == 0 && !isl_space_get_tuple_name(pw->dim, type))
return pw;
set_type = type == isl_dim_in ? isl_dim_set : type;
pw = FN(PW,cow)(pw);
if (!pw)
return NULL;
pw->dim = isl_space_drop_dims(pw->dim, type, first, n);
if (!pw->dim)
goto error;
for (i = 0; i < pw->n; ++i) {
pw->p[i].FIELD = FN(EL,drop_dims)(pw->p[i].FIELD, type, first, n);
if (!pw->p[i].FIELD)
goto error;
if (type == isl_dim_out)
continue;
pw->p[i].set = isl_set_drop(pw->p[i].set, set_type, first, n);
if (!pw->p[i].set)
goto error;
}
return pw;
error:
FN(PW,free)(pw);
return NULL;
}
/* This function is very similar to drop_dims.
* The only difference is that the cells may still involve
* the specified dimensions. They are removed using
* isl_set_project_out instead of isl_set_drop.
*/
__isl_give PW *FN(PW,project_out)(__isl_take PW *pw,
enum isl_dim_type type, unsigned first, unsigned n)
{
int i;
enum isl_dim_type set_type;
if (!pw)
return NULL;
if (n == 0 && !isl_space_get_tuple_name(pw->dim, type))
return pw;
set_type = type == isl_dim_in ? isl_dim_set : type;
pw = FN(PW,cow)(pw);
if (!pw)
return NULL;
pw->dim = isl_space_drop_dims(pw->dim, type, first, n);
if (!pw->dim)
goto error;
for (i = 0; i < pw->n; ++i) {
pw->p[i].set = isl_set_project_out(pw->p[i].set,
set_type, first, n);
if (!pw->p[i].set)
goto error;
pw->p[i].FIELD = FN(EL,drop_dims)(pw->p[i].FIELD, type, first, n);
if (!pw->p[i].FIELD)
goto error;
}
return pw;
error:
FN(PW,free)(pw);
return NULL;
}
/* Project the domain of pw onto its parameter space.
*/
__isl_give PW *FN(PW,project_domain_on_params)(__isl_take PW *pw)
{
isl_space *space;
unsigned n;
n = FN(PW,dim)(pw, isl_dim_in);
pw = FN(PW,project_out)(pw, isl_dim_in, 0, n);
space = FN(PW,get_domain_space)(pw);
space = isl_space_params(space);
pw = FN(PW,reset_domain_space)(pw, space);
return pw;
}
/* Drop all parameters not referenced by "pw".
*/
__isl_give PW *FN(PW,drop_unused_params)(__isl_take PW *pw)
{
int i;
if (FN(PW,check_named_params)(pw) < 0)
return FN(PW,free)(pw);
for (i = FN(PW,dim)(pw, isl_dim_param) - 1; i >= 0; i--) {
isl_bool involves;
involves = FN(PW,involves_dims)(pw, isl_dim_param, i, 1);
if (involves < 0)
return FN(PW,free)(pw);
if (!involves)
pw = FN(PW,drop_dims)(pw, isl_dim_param, i, 1);
}
return pw;
}
#ifndef NO_INSERT_DIMS
__isl_give PW *FN(PW,insert_dims)(__isl_take PW *pw, enum isl_dim_type type,
unsigned first, unsigned n)
{
int i;
enum isl_dim_type set_type;
if (!pw)
return NULL;
if (n == 0 && !isl_space_is_named_or_nested(pw->dim, type))
return pw;
set_type = type == isl_dim_in ? isl_dim_set : type;
pw = FN(PW,cow)(pw);
if (!pw)
return NULL;
pw->dim = isl_space_insert_dims(pw->dim, type, first, n);
if (!pw->dim)
goto error;
for (i = 0; i < pw->n; ++i) {
pw->p[i].set = isl_set_insert_dims(pw->p[i].set,
set_type, first, n);
if (!pw->p[i].set)
goto error;
pw->p[i].FIELD = FN(EL,insert_dims)(pw->p[i].FIELD,
type, first, n);
if (!pw->p[i].FIELD)
goto error;
}
return pw;
error:
FN(PW,free)(pw);
return NULL;
}
#endif
__isl_give PW *FN(PW,fix_dim)(__isl_take PW *pw,
enum isl_dim_type type, unsigned pos, isl_int v)
{
int i;
if (!pw)
return NULL;
if (type == isl_dim_in)
type = isl_dim_set;
pw = FN(PW,cow)(pw);
if (!pw)
return NULL;
for (i = 0; i < pw->n; ++i) {
pw->p[i].set = isl_set_fix(pw->p[i].set, type, pos, v);
if (FN(PW,exploit_equalities_and_remove_if_empty)(pw, i) < 0)
return FN(PW,free)(pw);
}
return pw;
}
/* Fix the value of the variable at position "pos" of type "type" of "pw"
* to be equal to "v".
*/
__isl_give PW *FN(PW,fix_val)(__isl_take PW *pw,
enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
{
if (!v)
return FN(PW,free)(pw);
if (!isl_val_is_int(v))
isl_die(FN(PW,get_ctx)(pw), isl_error_invalid,
"expecting integer value", goto error);
pw = FN(PW,fix_dim)(pw, type, pos, v->n);
isl_val_free(v);
return pw;
error:
isl_val_free(v);
return FN(PW,free)(pw);
}
unsigned FN(PW,dim)(__isl_keep PW *pw, enum isl_dim_type type)
{
return pw ? isl_space_dim(pw->dim, type) : 0;
}
__isl_give PW *FN(PW,split_dims)(__isl_take PW *pw,
enum isl_dim_type type, unsigned first, unsigned n)
{
int i;
if (!pw)
return NULL;
if (n == 0)
return pw;
if (type == isl_dim_in)
type = isl_dim_set;
pw = FN(PW,cow)(pw);
if (!pw)
return NULL;
if (!pw->dim)
goto error;
for (i = 0; i < pw->n; ++i) {
pw->p[i].set = isl_set_split_dims(pw->p[i].set, type, first, n);
if (!pw->p[i].set)
goto error;
}
return pw;
error:
FN(PW,free)(pw);
return NULL;
}
#ifndef NO_OPT
/* Compute the maximal value attained by the piecewise quasipolynomial
* on its domain or zero if the domain is empty.
* In the worst case, the domain is scanned completely,
* so the domain is assumed to be bounded.
*/
__isl_give isl_val *FN(PW,opt)(__isl_take PW *pw, int max)
{
int i;
isl_val *opt;
if (!pw)
return NULL;
if (pw->n == 0) {
opt = isl_val_zero(FN(PW,get_ctx)(pw));
FN(PW,free)(pw);
return opt;
}
opt = FN(EL,opt_on_domain)(FN(EL,copy)(pw->p[0].FIELD),
isl_set_copy(pw->p[0].set), max);
for (i = 1; i < pw->n; ++i) {
isl_val *opt_i;
opt_i = FN(EL,opt_on_domain)(FN(EL,copy)(pw->p[i].FIELD),
isl_set_copy(pw->p[i].set), max);
if (max)
opt = isl_val_max(opt, opt_i);
else
opt = isl_val_min(opt, opt_i);
}
FN(PW,free)(pw);
return opt;
}
__isl_give isl_val *FN(PW,max)(__isl_take PW *pw)
{
return FN(PW,opt)(pw, 1);
}
__isl_give isl_val *FN(PW,min)(__isl_take PW *pw)
{
return FN(PW,opt)(pw, 0);
}
#endif
/* Return the space of "pw".
*/
__isl_keep isl_space *FN(PW,peek_space)(__isl_keep PW *pw)
{
return pw ? pw->dim : NULL;
}
__isl_give isl_space *FN(PW,get_space)(__isl_keep PW *pw)
{
return isl_space_copy(FN(PW,peek_space)(pw));
}
__isl_give isl_space *FN(PW,get_domain_space)(__isl_keep PW *pw)
{
return pw ? isl_space_domain(isl_space_copy(pw->dim)) : NULL;
}
/* Return the position of the dimension of the given type and name
* in "pw".
* Return -1 if no such dimension can be found.
*/
int FN(PW,find_dim_by_name)(__isl_keep PW *pw,
enum isl_dim_type type, const char *name)
{
if (!pw)
return -1;
return isl_space_find_dim_by_name(pw->dim, type, name);
}
#ifndef NO_RESET_DIM
/* Reset the space of "pw". Since we don't know if the elements
* represent the spaces themselves or their domains, we pass along
* both when we call their reset_space_and_domain.
*/
static __isl_give PW *FN(PW,reset_space_and_domain)(__isl_take PW *pw,
__isl_take isl_space *space, __isl_take isl_space *domain)
{
int i;
pw = FN(PW,cow)(pw);
if (!pw || !space || !domain)
goto error;
for (i = 0; i < pw->n; ++i) {
pw->p[i].set = isl_set_reset_space(pw->p[i].set,
isl_space_copy(domain));
if (!pw->p[i].set)
goto error;
pw->p[i].FIELD = FN(EL,reset_space_and_domain)(pw->p[i].FIELD,
isl_space_copy(space), isl_space_copy(domain));
if (!pw->p[i].FIELD)
goto error;
}
isl_space_free(domain);
isl_space_free(pw->dim);
pw->dim = space;
return pw;
error:
isl_space_free(domain);
isl_space_free(space);
FN(PW,free)(pw);
return NULL;
}
__isl_give PW *FN(PW,reset_domain_space)(__isl_take PW *pw,
__isl_take isl_space *domain)
{
isl_space *space;
space = isl_space_extend_domain_with_range(isl_space_copy(domain),
FN(PW,get_space)(pw));
return FN(PW,reset_space_and_domain)(pw, space, domain);
}
__isl_give PW *FN(PW,reset_space)(__isl_take PW *pw, __isl_take isl_space *dim)
{
isl_space *domain;
domain = isl_space_domain(isl_space_copy(dim));
return FN(PW,reset_space_and_domain)(pw, dim, domain);
}
__isl_give PW *FN(PW,set_tuple_id)(__isl_take PW *pw, enum isl_dim_type type,
__isl_take isl_id *id)
{
isl_space *space;
pw = FN(PW,cow)(pw);
if (!pw)
goto error;
space = FN(PW,get_space)(pw);
space = isl_space_set_tuple_id(space, type, id);
return FN(PW,reset_space)(pw, space);
error:
isl_id_free(id);
return FN(PW,free)(pw);
}
/* Drop the id on the specified tuple.
*/
__isl_give PW *FN(PW,reset_tuple_id)(__isl_take PW *pw, enum isl_dim_type type)
{
isl_space *space;
if (!pw)
return NULL;
if (!FN(PW,has_tuple_id)(pw, type))
return pw;
pw = FN(PW,cow)(pw);
if (!pw)
return NULL;
space = FN(PW,get_space)(pw);
space = isl_space_reset_tuple_id(space, type);
return FN(PW,reset_space)(pw, space);
}
__isl_give PW *FN(PW,set_dim_id)(__isl_take PW *pw,
enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
{
pw = FN(PW,cow)(pw);
if (!pw)
goto error;
pw->dim = isl_space_set_dim_id(pw->dim, type, pos, id);
return FN(PW,reset_space)(pw, isl_space_copy(pw->dim));
error:
isl_id_free(id);
return FN(PW,free)(pw);
}
#endif
/* Reset the user pointer on all identifiers of parameters and tuples
* of the space of "pw".
*/
__isl_give PW *FN(PW,reset_user)(__isl_take PW *pw)
{
isl_space *space;
space = FN(PW,get_space)(pw);
space = isl_space_reset_user(space);
return FN(PW,reset_space)(pw, space);
}
isl_bool FN(PW,has_equal_space)(__isl_keep PW *pw1, __isl_keep PW *pw2)
{
if (!pw1 || !pw2)
return isl_bool_error;
return isl_space_is_equal(pw1->dim, pw2->dim);
}
#ifndef NO_MORPH
__isl_give PW *FN(PW,morph_domain)(__isl_take PW *pw,
__isl_take isl_morph *morph)
{
int i;
isl_ctx *ctx;
if (!pw || !morph)
goto error;
ctx = isl_space_get_ctx(pw->dim);
isl_assert(ctx, isl_space_is_domain_internal(morph->dom->dim, pw->dim),
goto error);
pw = FN(PW,cow)(pw);
if (!pw)
goto error;
pw->dim = isl_space_extend_domain_with_range(
isl_space_copy(morph->ran->dim), pw->dim);
if (!pw->dim)
goto error;
for (i = 0; i < pw->n; ++i) {
pw->p[i].set = isl_morph_set(isl_morph_copy(morph), pw->p[i].set);
if (!pw->p[i].set)
goto error;
pw->p[i].FIELD = FN(EL,morph_domain)(pw->p[i].FIELD,
isl_morph_copy(morph));
if (!pw->p[i].FIELD)
goto error;
}
isl_morph_free(morph);
return pw;
error:
FN(PW,free)(pw);
isl_morph_free(morph);
return NULL;
}
#endif
int FN(PW,n_piece)(__isl_keep PW *pw)
{
return pw ? pw->n : 0;
}
isl_stat FN(PW,foreach_piece)(__isl_keep PW *pw,
isl_stat (*fn)(__isl_take isl_set *set, __isl_take EL *el, void *user),
void *user)
{
int i;
if (!pw)
return isl_stat_error;
for (i = 0; i < pw->n; ++i)
if (fn(isl_set_copy(pw->p[i].set),
FN(EL,copy)(pw->p[i].FIELD), user) < 0)
return isl_stat_error;
return isl_stat_ok;
}
#ifndef NO_LIFT
static isl_bool any_divs(__isl_keep isl_set *set)
{
int i;
if (!set)
return isl_bool_error;
for (i = 0; i < set->n; ++i)
if (set->p[i]->n_div > 0)
return isl_bool_true;
return isl_bool_false;
}
static isl_stat foreach_lifted_subset(__isl_take isl_set *set,
__isl_take EL *el,
isl_stat (*fn)(__isl_take isl_set *set, __isl_take EL *el,
void *user), void *user)
{
int i;
if (!set || !el)
goto error;
for (i = 0; i < set->n; ++i) {
isl_set *lift;
EL *copy;
lift = isl_set_from_basic_set(isl_basic_set_copy(set->p[i]));
lift = isl_set_lift(lift);
copy = FN(EL,copy)(el);
copy = FN(EL,lift)(copy, isl_set_get_space(lift));
if (fn(lift, copy, user) < 0)
goto error;
}
isl_set_free(set);
FN(EL,free)(el);
return isl_stat_ok;
error:
isl_set_free(set);
FN(EL,free)(el);
return isl_stat_error;
}
isl_stat FN(PW,foreach_lifted_piece)(__isl_keep PW *pw,
isl_stat (*fn)(__isl_take isl_set *set, __isl_take EL *el,
void *user), void *user)
{
int i;
if (!pw)
return isl_stat_error;
for (i = 0; i < pw->n; ++i) {
isl_bool any;
isl_set *set;
EL *el;
any = any_divs(pw->p[i].set);
if (any < 0)
return isl_stat_error;
set = isl_set_copy(pw->p[i].set);
el = FN(EL,copy)(pw->p[i].FIELD);
if (!any) {
if (fn(set, el, user) < 0)
return isl_stat_error;
continue;
}
if (foreach_lifted_subset(set, el, fn, user) < 0)
return isl_stat_error;
}
return isl_stat_ok;
}
#endif
#ifndef NO_MOVE_DIMS
__isl_give PW *FN(PW,move_dims)(__isl_take PW *pw,
enum isl_dim_type dst_type, unsigned dst_pos,
enum isl_dim_type src_type, unsigned src_pos, unsigned n)
{
int i;
pw = FN(PW,cow)(pw);
if (!pw)
return NULL;
pw->dim = isl_space_move_dims(pw->dim, dst_type, dst_pos, src_type, src_pos, n);
if (!pw->dim)
goto error;
for (i = 0; i < pw->n; ++i) {
pw->p[i].FIELD = FN(EL,move_dims)(pw->p[i].FIELD,
dst_type, dst_pos, src_type, src_pos, n);
if (!pw->p[i].FIELD)
goto error;
}
if (dst_type == isl_dim_in)
dst_type = isl_dim_set;
if (src_type == isl_dim_in)
src_type = isl_dim_set;
for (i = 0; i < pw->n; ++i) {
pw->p[i].set = isl_set_move_dims(pw->p[i].set,
dst_type, dst_pos,
src_type, src_pos, n);
if (!pw->p[i].set)
goto error;
}
return pw;
error:
FN(PW,free)(pw);
return NULL;
}
#endif
__isl_give PW *FN(PW,mul_isl_int)(__isl_take PW *pw, isl_int v)
{
int i;
if (isl_int_is_one(v))
return pw;
if (pw && DEFAULT_IS_ZERO && isl_int_is_zero(v)) {
PW *zero;
isl_space *dim = FN(PW,get_space)(pw);
#ifdef HAS_TYPE
zero = FN(PW,ZERO)(dim, pw->type);
#else
zero = FN(PW,ZERO)(dim);
#endif
FN(PW,free)(pw);
return zero;
}
pw = FN(PW,cow)(pw);
if (!pw)
return NULL;
if (pw->n == 0)
return pw;
#ifdef HAS_TYPE
if (isl_int_is_neg(v))
pw->type = isl_fold_type_negate(pw->type);
#endif
for (i = 0; i < pw->n; ++i) {
pw->p[i].FIELD = FN(EL,scale)(pw->p[i].FIELD, v);
if (!pw->p[i].FIELD)
goto error;
}
return pw;
error:
FN(PW,free)(pw);
return NULL;
}
/* Multiply the pieces of "pw" by "v" and return the result.
*/
__isl_give PW *FN(PW,scale_val)(__isl_take PW *pw, __isl_take isl_val *v)
{
int i;
if (!pw || !v)
goto error;
if (isl_val_is_one(v)) {
isl_val_free(v);
return pw;
}
if (pw && DEFAULT_IS_ZERO && isl_val_is_zero(v)) {
PW *zero;
isl_space *space = FN(PW,get_space)(pw);
#ifdef HAS_TYPE
zero = FN(PW,ZERO)(space, pw->type);
#else
zero = FN(PW,ZERO)(space);
#endif
FN(PW,free)(pw);
isl_val_free(v);
return zero;
}
if (pw->n == 0) {
isl_val_free(v);
return pw;
}
pw = FN(PW,cow)(pw);
if (!pw)
goto error;
#ifdef HAS_TYPE
if (isl_val_is_neg(v))
pw->type = isl_fold_type_negate(pw->type);
#endif
for (i = 0; i < pw->n; ++i) {
pw->p[i].FIELD = FN(EL,scale_val)(pw->p[i].FIELD,
isl_val_copy(v));
if (!pw->p[i].FIELD)
goto error;
}
isl_val_free(v);
return pw;
error:
isl_val_free(v);
FN(PW,free)(pw);
return NULL;
}
/* Divide the pieces of "pw" by "v" and return the result.
*/
__isl_give PW *FN(PW,scale_down_val)(__isl_take PW *pw, __isl_take isl_val *v)
{
int i;
if (!pw || !v)
goto error;
if (isl_val_is_one(v)) {
isl_val_free(v);
return pw;
}
if (!isl_val_is_rat(v))
isl_die(isl_val_get_ctx(v), isl_error_invalid,
"expecting rational factor", goto error);
if (isl_val_is_zero(v))
isl_die(isl_val_get_ctx(v), isl_error_invalid,
"cannot scale down by zero", goto error);
if (pw->n == 0) {
isl_val_free(v);
return pw;
}
pw = FN(PW,cow)(pw);
if (!pw)
goto error;
#ifdef HAS_TYPE
if (isl_val_is_neg(v))
pw->type = isl_fold_type_negate(pw->type);
#endif
for (i = 0; i < pw->n; ++i) {
pw->p[i].FIELD = FN(EL,scale_down_val)(pw->p[i].FIELD,
isl_val_copy(v));
if (!pw->p[i].FIELD)
goto error;
}
isl_val_free(v);
return pw;
error:
isl_val_free(v);
FN(PW,free)(pw);
return NULL;
}
__isl_give PW *FN(PW,scale)(__isl_take PW *pw, isl_int v)
{
return FN(PW,mul_isl_int)(pw, v);
}
/* Apply some normalization to "pw".
* In particular, sort the pieces according to their function value
* expressions, combining pairs of adjacent pieces with
* the same such expression, and then normalize the domains of the pieces.
*
* We normalize in place, but if anything goes wrong we need
* to return NULL, so we need to make sure we don't change the
* meaning of any possible other copies of "pw".
*/
__isl_give PW *FN(PW,normalize)(__isl_take PW *pw)
{
int i;
isl_set *set;
pw = FN(PW,sort)(pw);
if (!pw)
return NULL;
for (i = 0; i < pw->n; ++i) {
set = isl_set_normalize(isl_set_copy(pw->p[i].set));
if (!set)
return FN(PW,free)(pw);
isl_set_free(pw->p[i].set);
pw->p[i].set = set;
}
return pw;
}
/* Is pw1 obviously equal to pw2?
* That is, do they have obviously identical cells and obviously identical
* elements on each cell?
*
* If "pw1" or "pw2" contain any NaNs, then they are considered
* not to be the same. A NaN is not equal to anything, not even
* to another NaN.
*/
isl_bool FN(PW,plain_is_equal)(__isl_keep PW *pw1, __isl_keep PW *pw2)
{
int i;
isl_bool equal, has_nan;
if (!pw1 || !pw2)
return isl_bool_error;
has_nan = FN(PW,involves_nan)(pw1);
if (has_nan >= 0 && !has_nan)
has_nan = FN(PW,involves_nan)(pw2);
if (has_nan < 0 || has_nan)
return isl_bool_not(has_nan);
if (pw1 == pw2)
return isl_bool_true;
if (!isl_space_is_equal(pw1->dim, pw2->dim))
return isl_bool_false;
pw1 = FN(PW,copy)(pw1);
pw2 = FN(PW,copy)(pw2);
pw1 = FN(PW,normalize)(pw1);
pw2 = FN(PW,normalize)(pw2);
if (!pw1 || !pw2)
goto error;
equal = pw1->n == pw2->n;
for (i = 0; equal && i < pw1->n; ++i) {
equal = isl_set_plain_is_equal(pw1->p[i].set, pw2->p[i].set);
if (equal < 0)
goto error;
if (!equal)
break;
equal = FN(EL,plain_is_equal)(pw1->p[i].FIELD, pw2->p[i].FIELD);
if (equal < 0)
goto error;
}
FN(PW,free)(pw1);
FN(PW,free)(pw2);
return equal;
error:
FN(PW,free)(pw1);
FN(PW,free)(pw2);
return isl_bool_error;
}
/* Does "pw" involve any NaNs?
*/
isl_bool FN(PW,involves_nan)(__isl_keep PW *pw)
{
int i;
if (!pw)
return isl_bool_error;
if (pw->n == 0)
return isl_bool_false;
for (i = 0; i < pw->n; ++i) {
isl_bool has_nan = FN(EL,involves_nan)(pw->p[i].FIELD);
if (has_nan < 0 || has_nan)
return has_nan;
}
return isl_bool_false;
}
#ifndef NO_PULLBACK
static __isl_give PW *FN(PW,align_params_pw_multi_aff_and)(__isl_take PW *pw,
__isl_take isl_multi_aff *ma,
__isl_give PW *(*fn)(__isl_take PW *pw, __isl_take isl_multi_aff *ma))
{
isl_ctx *ctx;
isl_bool equal_params;
isl_space *ma_space;
ma_space = isl_multi_aff_get_space(ma);
if (!pw || !ma || !ma_space)
goto error;
equal_params = isl_space_has_equal_params(pw->dim, ma_space);
if (equal_params < 0)
goto error;
if (equal_params) {
isl_space_free(ma_space);
return fn(pw, ma);
}
ctx = FN(PW,get_ctx)(pw);
if (FN(PW,check_named_params)(pw) < 0)
goto error;
if (!isl_space_has_named_params(ma_space))
isl_die(ctx, isl_error_invalid,
"unaligned unnamed parameters", goto error);
pw = FN(PW,align_params)(pw, ma_space);
ma = isl_multi_aff_align_params(ma, FN(PW,get_space)(pw));
return fn(pw, ma);
error:
isl_space_free(ma_space);
FN(PW,free)(pw);
isl_multi_aff_free(ma);
return NULL;
}
static __isl_give PW *FN(PW,align_params_pw_pw_multi_aff_and)(__isl_take PW *pw,
__isl_take isl_pw_multi_aff *pma,
__isl_give PW *(*fn)(__isl_take PW *pw,
__isl_take isl_pw_multi_aff *ma))
{
isl_bool equal_params;
isl_space *pma_space;
pma_space = isl_pw_multi_aff_get_space(pma);
if (!pw || !pma || !pma_space)
goto error;
equal_params = isl_space_has_equal_params(pw->dim, pma_space);
if (equal_params < 0)
goto error;
if (equal_params) {
isl_space_free(pma_space);
return fn(pw, pma);
}
if (FN(PW,check_named_params)(pw) < 0 ||
isl_pw_multi_aff_check_named_params(pma) < 0)
goto error;
pw = FN(PW,align_params)(pw, pma_space);
pma = isl_pw_multi_aff_align_params(pma, FN(PW,get_space)(pw));
return fn(pw, pma);
error:
isl_space_free(pma_space);
FN(PW,free)(pw);
isl_pw_multi_aff_free(pma);
return NULL;
}
/* Compute the pullback of "pw" by the function represented by "ma".
* In other words, plug in "ma" in "pw".
*/
static __isl_give PW *FN(PW,pullback_multi_aff_aligned)(__isl_take PW *pw,
__isl_take isl_multi_aff *ma)
{
int i;
isl_space *space = NULL;
ma = isl_multi_aff_align_divs(ma);
pw = FN(PW,cow)(pw);
if (!pw || !ma)
goto error;
space = isl_space_join(isl_multi_aff_get_space(ma),
FN(PW,get_space)(pw));
for (i = 0; i < pw->n; ++i) {
pw->p[i].set = isl_set_preimage_multi_aff(pw->p[i].set,
isl_multi_aff_copy(ma));
if (!pw->p[i].set)
goto error;
pw->p[i].FIELD = FN(EL,pullback_multi_aff)(pw->p[i].FIELD,
isl_multi_aff_copy(ma));
if (!pw->p[i].FIELD)
goto error;
}
pw = FN(PW,reset_space)(pw, space);
isl_multi_aff_free(ma);
return pw;
error:
isl_space_free(space);
isl_multi_aff_free(ma);
FN(PW,free)(pw);
return NULL;
}
__isl_give PW *FN(PW,pullback_multi_aff)(__isl_take PW *pw,
__isl_take isl_multi_aff *ma)
{
return FN(PW,align_params_pw_multi_aff_and)(pw, ma,
&FN(PW,pullback_multi_aff_aligned));
}
/* Compute the pullback of "pw" by the function represented by "pma".
* In other words, plug in "pma" in "pw".
*/
static __isl_give PW *FN(PW,pullback_pw_multi_aff_aligned)(__isl_take PW *pw,
__isl_take isl_pw_multi_aff *pma)
{
int i;
PW *res;
if (!pma)
goto error;
if (pma->n == 0) {
isl_space *space;
space = isl_space_join(isl_pw_multi_aff_get_space(pma),
FN(PW,get_space)(pw));
isl_pw_multi_aff_free(pma);
res = FN(PW,empty)(space);
FN(PW,free)(pw);
return res;
}
res = FN(PW,pullback_multi_aff)(FN(PW,copy)(pw),
isl_multi_aff_copy(pma->p[0].maff));
res = FN(PW,intersect_domain)(res, isl_set_copy(pma->p[0].set));
for (i = 1; i < pma->n; ++i) {
PW *res_i;
res_i = FN(PW,pullback_multi_aff)(FN(PW,copy)(pw),
isl_multi_aff_copy(pma->p[i].maff));
res_i = FN(PW,intersect_domain)(res_i,
isl_set_copy(pma->p[i].set));
res = FN(PW,add_disjoint)(res, res_i);
}
isl_pw_multi_aff_free(pma);
FN(PW,free)(pw);
return res;
error:
isl_pw_multi_aff_free(pma);
FN(PW,free)(pw);
return NULL;
}
__isl_give PW *FN(PW,pullback_pw_multi_aff)(__isl_take PW *pw,
__isl_take isl_pw_multi_aff *pma)
{
return FN(PW,align_params_pw_pw_multi_aff_and)(pw, pma,
&FN(PW,pullback_pw_multi_aff_aligned));
}
#endif
|