temporarily make isl_val_int_from_isl_int available
[platform/upstream/isl.git] / isl_pw_templ.c
1 #include <isl/aff.h>
2 #include <isl_val_private.h>
3
4 #define xFN(TYPE,NAME) TYPE ## _ ## NAME
5 #define FN(TYPE,NAME) xFN(TYPE,NAME)
6 #define xS(TYPE,NAME) struct TYPE ## _ ## NAME
7 #define S(TYPE,NAME) xS(TYPE,NAME)
8
9 #ifdef HAS_TYPE
10 __isl_give PW *FN(PW,alloc_size)(__isl_take isl_space *dim,
11         enum isl_fold type, int n)
12 #else
13 __isl_give PW *FN(PW,alloc_size)(__isl_take isl_space *dim, int n)
14 #endif
15 {
16         isl_ctx *ctx;
17         struct PW *pw;
18
19         if (!dim)
20                 return NULL;
21         ctx = isl_space_get_ctx(dim);
22         isl_assert(ctx, n >= 0, goto error);
23         pw = isl_alloc(ctx, struct PW,
24                         sizeof(struct PW) + (n - 1) * sizeof(S(PW,piece)));
25         if (!pw)
26                 goto error;
27
28         pw->ref = 1;
29 #ifdef HAS_TYPE
30         pw->type = type;
31 #endif
32         pw->size = n;
33         pw->n = 0;
34         pw->dim = dim;
35         return pw;
36 error:
37         isl_space_free(dim);
38         return NULL;
39 }
40
41 #ifdef HAS_TYPE
42 __isl_give PW *FN(PW,ZERO)(__isl_take isl_space *dim, enum isl_fold type)
43 {
44         return FN(PW,alloc_size)(dim, type, 0);
45 }
46 #else
47 __isl_give PW *FN(PW,ZERO)(__isl_take isl_space *dim)
48 {
49         return FN(PW,alloc_size)(dim, 0);
50 }
51 #endif
52
53 __isl_give PW *FN(PW,add_piece)(__isl_take PW *pw,
54         __isl_take isl_set *set, __isl_take EL *el)
55 {
56         isl_ctx *ctx;
57         isl_space *el_dim = NULL;
58
59         if (!pw || !set || !el)
60                 goto error;
61
62         if (isl_set_plain_is_empty(set) || FN(EL,EL_IS_ZERO)(el)) {
63                 isl_set_free(set);
64                 FN(EL,free)(el);
65                 return pw;
66         }
67
68         ctx = isl_set_get_ctx(set);
69 #ifdef HAS_TYPE
70         if (pw->type != el->type)
71                 isl_die(ctx, isl_error_invalid, "fold types don't match",
72                         goto error);
73 #endif
74         el_dim = FN(EL,get_space(el));
75         isl_assert(ctx, isl_space_is_equal(pw->dim, el_dim), goto error);
76         isl_assert(ctx, pw->n < pw->size, goto error);
77
78         pw->p[pw->n].set = set;
79         pw->p[pw->n].FIELD = el;
80         pw->n++;
81         
82         isl_space_free(el_dim);
83         return pw;
84 error:
85         isl_space_free(el_dim);
86         FN(PW,free)(pw);
87         isl_set_free(set);
88         FN(EL,free)(el);
89         return NULL;
90 }
91
92 #ifdef HAS_TYPE
93 __isl_give PW *FN(PW,alloc)(enum isl_fold type,
94         __isl_take isl_set *set, __isl_take EL *el)
95 #else
96 __isl_give PW *FN(PW,alloc)(__isl_take isl_set *set, __isl_take EL *el)
97 #endif
98 {
99         PW *pw;
100
101         if (!set || !el)
102                 goto error;
103
104 #ifdef HAS_TYPE
105         pw = FN(PW,alloc_size)(FN(EL,get_space)(el), type, 1);
106 #else
107         pw = FN(PW,alloc_size)(FN(EL,get_space)(el), 1);
108 #endif
109
110         return FN(PW,add_piece)(pw, set, el);
111 error:
112         isl_set_free(set);
113         FN(EL,free)(el);
114         return NULL;
115 }
116
117 __isl_give PW *FN(PW,dup)(__isl_keep PW *pw)
118 {
119         int i;
120         PW *dup;
121
122         if (!pw)
123                 return NULL;
124
125 #ifdef HAS_TYPE
126         dup = FN(PW,alloc_size)(isl_space_copy(pw->dim), pw->type, pw->n);
127 #else
128         dup = FN(PW,alloc_size)(isl_space_copy(pw->dim), pw->n);
129 #endif
130         if (!dup)
131                 return NULL;
132
133         for (i = 0; i < pw->n; ++i)
134                 dup = FN(PW,add_piece)(dup, isl_set_copy(pw->p[i].set),
135                                             FN(EL,copy)(pw->p[i].FIELD));
136
137         return dup;
138 }
139
140 __isl_give PW *FN(PW,cow)(__isl_take PW *pw)
141 {
142         if (!pw)
143                 return NULL;
144
145         if (pw->ref == 1)
146                 return pw;
147         pw->ref--;
148         return FN(PW,dup)(pw);
149 }
150
151 __isl_give PW *FN(PW,copy)(__isl_keep PW *pw)
152 {
153         if (!pw)
154                 return NULL;
155
156         pw->ref++;
157         return pw;
158 }
159
160 void *FN(PW,free)(__isl_take PW *pw)
161 {
162         int i;
163
164         if (!pw)
165                 return NULL;
166         if (--pw->ref > 0)
167                 return NULL;
168
169         for (i = 0; i < pw->n; ++i) {
170                 isl_set_free(pw->p[i].set);
171                 FN(EL,free)(pw->p[i].FIELD);
172         }
173         isl_space_free(pw->dim);
174         free(pw);
175
176         return NULL;
177 }
178
179 const char *FN(PW,get_dim_name)(__isl_keep PW *pw, enum isl_dim_type type,
180         unsigned pos)
181 {
182         return pw ? isl_space_get_dim_name(pw->dim, type, pos) : NULL;
183 }
184
185 int FN(PW,has_dim_id)(__isl_keep PW *pw, enum isl_dim_type type, unsigned pos)
186 {
187         return pw ? isl_space_has_dim_id(pw->dim, type, pos) : -1;
188 }
189
190 __isl_give isl_id *FN(PW,get_dim_id)(__isl_keep PW *pw, enum isl_dim_type type,
191         unsigned pos)
192 {
193         return pw ? isl_space_get_dim_id(pw->dim, type, pos) : NULL;
194 }
195
196 int FN(PW,has_tuple_name)(__isl_keep PW *pw, enum isl_dim_type type)
197 {
198         return pw ? isl_space_has_tuple_name(pw->dim, type) : -1;
199 }
200
201 const char *FN(PW,get_tuple_name)(__isl_keep PW *pw, enum isl_dim_type type)
202 {
203         return pw ? isl_space_get_tuple_name(pw->dim, type) : NULL;
204 }
205
206 int FN(PW,has_tuple_id)(__isl_keep PW *pw, enum isl_dim_type type)
207 {
208         return pw ? isl_space_has_tuple_id(pw->dim, type) : -1;
209 }
210
211 __isl_give isl_id *FN(PW,get_tuple_id)(__isl_keep PW *pw, enum isl_dim_type type)
212 {
213         return pw ? isl_space_get_tuple_id(pw->dim, type) : NULL;
214 }
215
216 int FN(PW,IS_ZERO)(__isl_keep PW *pw)
217 {
218         if (!pw)
219                 return -1;
220
221         return pw->n == 0;
222 }
223
224 #ifndef NO_REALIGN
225 __isl_give PW *FN(PW,realign_domain)(__isl_take PW *pw,
226         __isl_take isl_reordering *exp)
227 {
228         int i;
229
230         pw = FN(PW,cow)(pw);
231         if (!pw || !exp)
232                 goto error;
233
234         for (i = 0; i < pw->n; ++i) {
235                 pw->p[i].set = isl_set_realign(pw->p[i].set,
236                                                     isl_reordering_copy(exp));
237                 if (!pw->p[i].set)
238                         goto error;
239                 pw->p[i].FIELD = FN(EL,realign_domain)(pw->p[i].FIELD,
240                                                     isl_reordering_copy(exp));
241                 if (!pw->p[i].FIELD)
242                         goto error;
243         }
244
245         pw = FN(PW,reset_domain_space)(pw, isl_space_copy(exp->dim));
246
247         isl_reordering_free(exp);
248         return pw;
249 error:
250         isl_reordering_free(exp);
251         FN(PW,free)(pw);
252         return NULL;
253 }
254
255 /* Align the parameters of "pw" to those of "model".
256  */
257 __isl_give PW *FN(PW,align_params)(__isl_take PW *pw, __isl_take isl_space *model)
258 {
259         isl_ctx *ctx;
260
261         if (!pw || !model)
262                 goto error;
263
264         ctx = isl_space_get_ctx(model);
265         if (!isl_space_has_named_params(model))
266                 isl_die(ctx, isl_error_invalid,
267                         "model has unnamed parameters", goto error);
268         if (!isl_space_has_named_params(pw->dim))
269                 isl_die(ctx, isl_error_invalid,
270                         "input has unnamed parameters", goto error);
271         if (!isl_space_match(pw->dim, isl_dim_param, model, isl_dim_param)) {
272                 isl_reordering *exp;
273
274                 model = isl_space_drop_dims(model, isl_dim_in,
275                                         0, isl_space_dim(model, isl_dim_in));
276                 model = isl_space_drop_dims(model, isl_dim_out,
277                                         0, isl_space_dim(model, isl_dim_out));
278                 exp = isl_parameter_alignment_reordering(pw->dim, model);
279                 exp = isl_reordering_extend_space(exp,
280                                         FN(PW,get_domain_space)(pw));
281                 pw = FN(PW,realign_domain)(pw, exp);
282         }
283
284         isl_space_free(model);
285         return pw;
286 error:
287         isl_space_free(model);
288         FN(PW,free)(pw);
289         return NULL;
290 }
291
292 static __isl_give PW *FN(PW,align_params_pw_pw_and)(__isl_take PW *pw1,
293         __isl_take PW *pw2,
294         __isl_give PW *(*fn)(__isl_take PW *pw1, __isl_take PW *pw2))
295 {
296         isl_ctx *ctx;
297
298         if (!pw1 || !pw2)
299                 goto error;
300         if (isl_space_match(pw1->dim, isl_dim_param, pw2->dim, isl_dim_param))
301                 return fn(pw1, pw2);
302         ctx = FN(PW,get_ctx)(pw1);
303         if (!isl_space_has_named_params(pw1->dim) ||
304             !isl_space_has_named_params(pw2->dim))
305                 isl_die(ctx, isl_error_invalid,
306                         "unaligned unnamed parameters", goto error);
307         pw1 = FN(PW,align_params)(pw1, FN(PW,get_space)(pw2));
308         pw2 = FN(PW,align_params)(pw2, FN(PW,get_space)(pw1));
309         return fn(pw1, pw2);
310 error:
311         FN(PW,free)(pw1);
312         FN(PW,free)(pw2);
313         return NULL;
314 }
315
316 static __isl_give PW *FN(PW,align_params_pw_set_and)(__isl_take PW *pw,
317         __isl_take isl_set *set,
318         __isl_give PW *(*fn)(__isl_take PW *pw, __isl_take isl_set *set))
319 {
320         isl_ctx *ctx;
321
322         if (!pw || !set)
323                 goto error;
324         if (isl_space_match(pw->dim, isl_dim_param, set->dim, isl_dim_param))
325                 return fn(pw, set);
326         ctx = FN(PW,get_ctx)(pw);
327         if (!isl_space_has_named_params(pw->dim) ||
328             !isl_space_has_named_params(set->dim))
329                 isl_die(ctx, isl_error_invalid,
330                         "unaligned unnamed parameters", goto error);
331         pw = FN(PW,align_params)(pw, isl_set_get_space(set));
332         set = isl_set_align_params(set, FN(PW,get_space)(pw));
333         return fn(pw, set);
334 error:
335         FN(PW,free)(pw);
336         isl_set_free(set);
337         return NULL;
338 }
339 #endif
340
341 static __isl_give PW *FN(PW,union_add_aligned)(__isl_take PW *pw1,
342         __isl_take PW *pw2)
343 {
344         int i, j, n;
345         struct PW *res;
346         isl_ctx *ctx;
347         isl_set *set;
348
349         if (!pw1 || !pw2)
350                 goto error;
351
352         ctx = isl_space_get_ctx(pw1->dim);
353 #ifdef HAS_TYPE
354         if (pw1->type != pw2->type)
355                 isl_die(ctx, isl_error_invalid,
356                         "fold types don't match", goto error);
357 #endif
358         isl_assert(ctx, isl_space_is_equal(pw1->dim, pw2->dim), goto error);
359
360         if (FN(PW,IS_ZERO)(pw1)) {
361                 FN(PW,free)(pw1);
362                 return pw2;
363         }
364
365         if (FN(PW,IS_ZERO)(pw2)) {
366                 FN(PW,free)(pw2);
367                 return pw1;
368         }
369
370         n = (pw1->n + 1) * (pw2->n + 1);
371 #ifdef HAS_TYPE
372         res = FN(PW,alloc_size)(isl_space_copy(pw1->dim), pw1->type, n);
373 #else
374         res = FN(PW,alloc_size)(isl_space_copy(pw1->dim), n);
375 #endif
376
377         for (i = 0; i < pw1->n; ++i) {
378                 set = isl_set_copy(pw1->p[i].set);
379                 for (j = 0; j < pw2->n; ++j) {
380                         struct isl_set *common;
381                         EL *sum;
382                         common = isl_set_intersect(isl_set_copy(pw1->p[i].set),
383                                                 isl_set_copy(pw2->p[j].set));
384                         if (isl_set_plain_is_empty(common)) {
385                                 isl_set_free(common);
386                                 continue;
387                         }
388                         set = isl_set_subtract(set,
389                                         isl_set_copy(pw2->p[j].set));
390
391                         sum = FN(EL,add_on_domain)(common,
392                                                    FN(EL,copy)(pw1->p[i].FIELD),
393                                                    FN(EL,copy)(pw2->p[j].FIELD));
394
395                         res = FN(PW,add_piece)(res, common, sum);
396                 }
397                 res = FN(PW,add_piece)(res, set, FN(EL,copy)(pw1->p[i].FIELD));
398         }
399
400         for (j = 0; j < pw2->n; ++j) {
401                 set = isl_set_copy(pw2->p[j].set);
402                 for (i = 0; i < pw1->n; ++i)
403                         set = isl_set_subtract(set,
404                                         isl_set_copy(pw1->p[i].set));
405                 res = FN(PW,add_piece)(res, set, FN(EL,copy)(pw2->p[j].FIELD));
406         }
407
408         FN(PW,free)(pw1);
409         FN(PW,free)(pw2);
410
411         return res;
412 error:
413         FN(PW,free)(pw1);
414         FN(PW,free)(pw2);
415         return NULL;
416 }
417
418 /* Private version of "union_add".  For isl_pw_qpolynomial and
419  * isl_pw_qpolynomial_fold, we prefer to simply call it "add".
420  */
421 static __isl_give PW *FN(PW,union_add_)(__isl_take PW *pw1, __isl_take PW *pw2)
422 {
423         return FN(PW,align_params_pw_pw_and)(pw1, pw2,
424                                                 &FN(PW,union_add_aligned));
425 }
426
427 /* Make sure "pw" has room for at least "n" more pieces.
428  *
429  * If there is only one reference to pw, we extend it in place.
430  * Otherwise, we create a new PW and copy the pieces.
431  */
432 static __isl_give PW *FN(PW,grow)(__isl_take PW *pw, int n)
433 {
434         int i;
435         isl_ctx *ctx;
436         PW *res;
437
438         if (!pw)
439                 return NULL;
440         if (pw->n + n <= pw->size)
441                 return pw;
442         ctx = FN(PW,get_ctx)(pw);
443         n += pw->n;
444         if (pw->ref == 1) {
445                 res = isl_realloc(ctx, pw, struct PW,
446                             sizeof(struct PW) + (n - 1) * sizeof(S(PW,piece)));
447                 if (!res)
448                         return FN(PW,free)(pw);
449                 res->size = n;
450                 return res;
451         }
452 #ifdef HAS_TYPE
453         res = FN(PW,alloc_size)(isl_space_copy(pw->dim), pw->type, n);
454 #else
455         res = FN(PW,alloc_size)(isl_space_copy(pw->dim), n);
456 #endif
457         if (!res)
458                 return FN(PW,free)(pw);
459         for (i = 0; i < pw->n; ++i)
460                 res = FN(PW,add_piece)(res, isl_set_copy(pw->p[i].set),
461                                             FN(EL,copy)(pw->p[i].FIELD));
462         FN(PW,free)(pw);
463         return res;
464 }
465
466 static __isl_give PW *FN(PW,add_disjoint_aligned)(__isl_take PW *pw1,
467         __isl_take PW *pw2)
468 {
469         int i;
470         isl_ctx *ctx;
471
472         if (!pw1 || !pw2)
473                 goto error;
474
475         if (pw1->size < pw1->n + pw2->n && pw1->n < pw2->n)
476                 return FN(PW,add_disjoint_aligned)(pw2, pw1);
477
478         ctx = isl_space_get_ctx(pw1->dim);
479 #ifdef HAS_TYPE
480         if (pw1->type != pw2->type)
481                 isl_die(ctx, isl_error_invalid,
482                         "fold types don't match", goto error);
483 #endif
484         isl_assert(ctx, isl_space_is_equal(pw1->dim, pw2->dim), goto error);
485
486         if (FN(PW,IS_ZERO)(pw1)) {
487                 FN(PW,free)(pw1);
488                 return pw2;
489         }
490
491         if (FN(PW,IS_ZERO)(pw2)) {
492                 FN(PW,free)(pw2);
493                 return pw1;
494         }
495
496         pw1 = FN(PW,grow)(pw1, pw2->n);
497         if (!pw1)
498                 goto error;
499
500         for (i = 0; i < pw2->n; ++i)
501                 pw1 = FN(PW,add_piece)(pw1,
502                                 isl_set_copy(pw2->p[i].set),
503                                 FN(EL,copy)(pw2->p[i].FIELD));
504
505         FN(PW,free)(pw2);
506
507         return pw1;
508 error:
509         FN(PW,free)(pw1);
510         FN(PW,free)(pw2);
511         return NULL;
512 }
513
514 __isl_give PW *FN(PW,add_disjoint)(__isl_take PW *pw1, __isl_take PW *pw2)
515 {
516         return FN(PW,align_params_pw_pw_and)(pw1, pw2,
517                                                 &FN(PW,add_disjoint_aligned));
518 }
519
520 /* This function is currently only used from isl_aff.c
521  */
522 static __isl_give PW *FN(PW,on_shared_domain_in)(__isl_take PW *pw1,
523         __isl_take PW *pw2, __isl_take isl_space *space,
524         __isl_give EL *(*fn)(__isl_take EL *el1, __isl_take EL *el2))
525         __attribute__ ((unused));
526
527 /* Apply "fn" to pairs of elements from pw1 and pw2 on shared domains.
528  * The result of "fn" (and therefore also of this function) lives in "space".
529  */
530 static __isl_give PW *FN(PW,on_shared_domain_in)(__isl_take PW *pw1,
531         __isl_take PW *pw2, __isl_take isl_space *space,
532         __isl_give EL *(*fn)(__isl_take EL *el1, __isl_take EL *el2))
533 {
534         int i, j, n;
535         PW *res = NULL;
536
537         if (!pw1 || !pw2)
538                 goto error;
539
540         n = pw1->n * pw2->n;
541 #ifdef HAS_TYPE
542         res = FN(PW,alloc_size)(isl_space_copy(space), pw1->type, n);
543 #else
544         res = FN(PW,alloc_size)(isl_space_copy(space), n);
545 #endif
546
547         for (i = 0; i < pw1->n; ++i) {
548                 for (j = 0; j < pw2->n; ++j) {
549                         isl_set *common;
550                         EL *res_ij;
551                         int empty;
552
553                         common = isl_set_intersect(
554                                         isl_set_copy(pw1->p[i].set),
555                                         isl_set_copy(pw2->p[j].set));
556                         empty = isl_set_plain_is_empty(common);
557                         if (empty < 0 || empty) {
558                                 isl_set_free(common);
559                                 if (empty < 0)
560                                         goto error;
561                                 continue;
562                         }
563
564                         res_ij = fn(FN(EL,copy)(pw1->p[i].FIELD),
565                                     FN(EL,copy)(pw2->p[j].FIELD));
566                         res_ij = FN(EL,gist)(res_ij, isl_set_copy(common));
567
568                         res = FN(PW,add_piece)(res, common, res_ij);
569                 }
570         }
571
572         isl_space_free(space);
573         FN(PW,free)(pw1);
574         FN(PW,free)(pw2);
575         return res;
576 error:
577         isl_space_free(space);
578         FN(PW,free)(pw1);
579         FN(PW,free)(pw2);
580         FN(PW,free)(res);
581         return NULL;
582 }
583
584 /* This function is currently only used from isl_aff.c
585  */
586 static __isl_give PW *FN(PW,on_shared_domain)(__isl_take PW *pw1,
587         __isl_take PW *pw2,
588         __isl_give EL *(*fn)(__isl_take EL *el1, __isl_take EL *el2))
589         __attribute__ ((unused));
590
591 /* Apply "fn" to pairs of elements from pw1 and pw2 on shared domains.
592  * The result of "fn" is assumed to live in the same space as "pw1" and "pw2".
593  */
594 static __isl_give PW *FN(PW,on_shared_domain)(__isl_take PW *pw1,
595         __isl_take PW *pw2,
596         __isl_give EL *(*fn)(__isl_take EL *el1, __isl_take EL *el2))
597 {
598         isl_space *space;
599
600         if (!pw1 || !pw2)
601                 goto error;
602
603         space = isl_space_copy(pw1->dim);
604         return FN(PW,on_shared_domain_in)(pw1, pw2, space, fn);
605 error:
606         FN(PW,free)(pw1);
607         FN(PW,free)(pw2);
608         return NULL;
609 }
610
611 #ifndef NO_NEG
612 __isl_give PW *FN(PW,neg)(__isl_take PW *pw)
613 {
614         int i;
615
616         if (!pw)
617                 return NULL;
618
619         if (FN(PW,IS_ZERO)(pw))
620                 return pw;
621
622         pw = FN(PW,cow)(pw);
623         if (!pw)
624                 return NULL;
625
626         for (i = 0; i < pw->n; ++i) {
627                 pw->p[i].FIELD = FN(EL,neg)(pw->p[i].FIELD);
628                 if (!pw->p[i].FIELD)
629                         return FN(PW,free)(pw);
630         }
631
632         return pw;
633 }
634
635 __isl_give PW *FN(PW,sub)(__isl_take PW *pw1, __isl_take PW *pw2)
636 {
637         return FN(PW,add)(pw1, FN(PW,neg)(pw2));
638 }
639 #endif
640
641 #ifndef NO_EVAL
642 __isl_give isl_qpolynomial *FN(PW,eval)(__isl_take PW *pw,
643         __isl_take isl_point *pnt)
644 {
645         int i;
646         int found = 0;
647         isl_ctx *ctx;
648         isl_space *pnt_dim = NULL;
649         isl_qpolynomial *qp;
650
651         if (!pw || !pnt)
652                 goto error;
653         ctx = isl_point_get_ctx(pnt);
654         pnt_dim = isl_point_get_space(pnt);
655         isl_assert(ctx, isl_space_is_domain_internal(pnt_dim, pw->dim),
656                     goto error);
657
658         for (i = 0; i < pw->n; ++i) {
659                 found = isl_set_contains_point(pw->p[i].set, pnt);
660                 if (found < 0)
661                         goto error;
662                 if (found)
663                         break;
664         }
665         if (found)
666                 qp = FN(EL,eval)(FN(EL,copy)(pw->p[i].FIELD),
667                                             isl_point_copy(pnt));
668         else
669                 qp = isl_qpolynomial_zero_on_domain(FN(PW,get_domain_space)(pw));
670         FN(PW,free)(pw);
671         isl_space_free(pnt_dim);
672         isl_point_free(pnt);
673         return qp;
674 error:
675         FN(PW,free)(pw);
676         isl_space_free(pnt_dim);
677         isl_point_free(pnt);
678         return NULL;
679 }
680 #endif
681
682 __isl_give isl_set *FN(PW,domain)(__isl_take PW *pw)
683 {
684         int i;
685         isl_set *dom;
686
687         if (!pw)
688                 return NULL;
689
690         dom = isl_set_empty(FN(PW,get_domain_space)(pw));
691         for (i = 0; i < pw->n; ++i)
692                 dom = isl_set_union_disjoint(dom, isl_set_copy(pw->p[i].set));
693
694         FN(PW,free)(pw);
695
696         return dom;
697 }
698
699 /* Restrict the domain of "pw" by combining each cell
700  * with "set" through a call to "fn", where "fn" may be
701  * isl_set_intersect or isl_set_intersect_params.
702  */
703 static __isl_give PW *FN(PW,intersect_aligned)(__isl_take PW *pw,
704         __isl_take isl_set *set,
705         __isl_give isl_set *(*fn)(__isl_take isl_set *set1,
706                                     __isl_take isl_set *set2))
707 {
708         int i;
709
710         if (!pw || !set)
711                 goto error;
712
713         if (pw->n == 0) {
714                 isl_set_free(set);
715                 return pw;
716         }
717
718         pw = FN(PW,cow)(pw);
719         if (!pw)
720                 goto error;
721
722         for (i = pw->n - 1; i >= 0; --i) {
723                 isl_basic_set *aff;
724                 pw->p[i].set = fn(pw->p[i].set, isl_set_copy(set));
725                 if (!pw->p[i].set)
726                         goto error;
727                 aff = isl_set_affine_hull(isl_set_copy(pw->p[i].set));
728                 pw->p[i].FIELD = FN(EL,substitute_equalities)(pw->p[i].FIELD,
729                                                                 aff);
730                 if (!pw->p[i].FIELD)
731                         goto error;
732                 if (isl_set_plain_is_empty(pw->p[i].set)) {
733                         isl_set_free(pw->p[i].set);
734                         FN(EL,free)(pw->p[i].FIELD);
735                         if (i != pw->n - 1)
736                                 pw->p[i] = pw->p[pw->n - 1];
737                         pw->n--;
738                 }
739         }
740         
741         isl_set_free(set);
742         return pw;
743 error:
744         isl_set_free(set);
745         FN(PW,free)(pw);
746         return NULL;
747 }
748
749 static __isl_give PW *FN(PW,intersect_domain_aligned)(__isl_take PW *pw,
750         __isl_take isl_set *set)
751 {
752         return FN(PW,intersect_aligned)(pw, set, &isl_set_intersect);
753 }
754
755 __isl_give PW *FN(PW,intersect_domain)(__isl_take PW *pw,
756         __isl_take isl_set *context)
757 {
758         return FN(PW,align_params_pw_set_and)(pw, context,
759                                         &FN(PW,intersect_domain_aligned));
760 }
761
762 static __isl_give PW *FN(PW,intersect_params_aligned)(__isl_take PW *pw,
763         __isl_take isl_set *set)
764 {
765         return FN(PW,intersect_aligned)(pw, set, &isl_set_intersect_params);
766 }
767
768 /* Intersect the domain of "pw" with the parameter domain "context".
769  */
770 __isl_give PW *FN(PW,intersect_params)(__isl_take PW *pw,
771         __isl_take isl_set *context)
772 {
773         return FN(PW,align_params_pw_set_and)(pw, context,
774                                         &FN(PW,intersect_params_aligned));
775 }
776
777 static __isl_give PW *FN(PW,gist_aligned)(__isl_take PW *pw,
778         __isl_take isl_set *context,
779         __isl_give EL *(*fn_el)(__isl_take EL *el,
780                                     __isl_take isl_set *set),
781         __isl_give isl_set *(*fn_dom)(__isl_take isl_set *set,
782                                     __isl_take isl_basic_set *bset))
783 {
784         int i;
785         isl_basic_set *hull = NULL;
786
787         if (!pw || !context)
788                 goto error;
789
790         if (pw->n == 0) {
791                 isl_set_free(context);
792                 return pw;
793         }
794
795         if (!isl_space_match(pw->dim, isl_dim_param,
796                                 context->dim, isl_dim_param)) {
797                 pw = FN(PW,align_params)(pw, isl_set_get_space(context));
798                 context = isl_set_align_params(context, FN(PW,get_space)(pw));
799         }
800
801         context = isl_set_compute_divs(context);
802         hull = isl_set_simple_hull(isl_set_copy(context));
803
804         pw = FN(PW,cow)(pw);
805         if (!pw)
806                 goto error;
807
808         for (i = pw->n - 1; i >= 0; --i) {
809                 isl_set *set_i;
810                 int empty;
811
812                 set_i = isl_set_intersect(isl_set_copy(pw->p[i].set),
813                                                  isl_set_copy(context));
814                 empty = isl_set_plain_is_empty(set_i);
815                 pw->p[i].FIELD = fn_el(pw->p[i].FIELD, set_i);
816                 pw->p[i].set = fn_dom(pw->p[i].set, isl_basic_set_copy(hull));
817                 if (!pw->p[i].FIELD || !pw->p[i].set)
818                         goto error;
819                 if (empty) {
820                         isl_set_free(pw->p[i].set);
821                         FN(EL,free)(pw->p[i].FIELD);
822                         if (i != pw->n - 1)
823                                 pw->p[i] = pw->p[pw->n - 1];
824                         pw->n--;
825                 }
826         }
827
828         isl_basic_set_free(hull);
829         isl_set_free(context);
830
831         return pw;
832 error:
833         FN(PW,free)(pw);
834         isl_basic_set_free(hull);
835         isl_set_free(context);
836         return NULL;
837 }
838
839 static __isl_give PW *FN(PW,gist_domain_aligned)(__isl_take PW *pw,
840         __isl_take isl_set *set)
841 {
842         return FN(PW,gist_aligned)(pw, set, &FN(EL,gist),
843                                         &isl_set_gist_basic_set);
844 }
845
846 __isl_give PW *FN(PW,gist)(__isl_take PW *pw, __isl_take isl_set *context)
847 {
848         return FN(PW,align_params_pw_set_and)(pw, context,
849                                                 &FN(PW,gist_domain_aligned));
850 }
851
852 static __isl_give PW *FN(PW,gist_params_aligned)(__isl_take PW *pw,
853         __isl_take isl_set *set)
854 {
855         return FN(PW,gist_aligned)(pw, set, &FN(EL,gist_params),
856                                         &isl_set_gist_params_basic_set);
857 }
858
859 __isl_give PW *FN(PW,gist_params)(__isl_take PW *pw,
860         __isl_take isl_set *context)
861 {
862         return FN(PW,align_params_pw_set_and)(pw, context,
863                                                 &FN(PW,gist_params_aligned));
864 }
865
866 __isl_give PW *FN(PW,coalesce)(__isl_take PW *pw)
867 {
868         int i, j;
869
870         if (!pw)
871                 return NULL;
872         if (pw->n == 0)
873                 return pw;
874
875         for (i = pw->n - 1; i >= 0; --i) {
876                 for (j = i - 1; j >= 0; --j) {
877                         if (!FN(EL,plain_is_equal)(pw->p[i].FIELD,
878                                                         pw->p[j].FIELD))
879                                 continue;
880                         pw->p[j].set = isl_set_union(pw->p[j].set,
881                                                         pw->p[i].set);
882                         FN(EL,free)(pw->p[i].FIELD);
883                         if (i != pw->n - 1)
884                                 pw->p[i] = pw->p[pw->n - 1];
885                         pw->n--;
886                         break;
887                 }
888                 if (j >= 0)
889                         continue;
890                 pw->p[i].set = isl_set_coalesce(pw->p[i].set);
891                 if (!pw->p[i].set)
892                         goto error;
893         }
894
895         return pw;
896 error:
897         FN(PW,free)(pw);
898         return NULL;
899 }
900
901 isl_ctx *FN(PW,get_ctx)(__isl_keep PW *pw)
902 {
903         return pw ? isl_space_get_ctx(pw->dim) : NULL;
904 }
905
906 #ifndef NO_INVOLVES_DIMS
907 int FN(PW,involves_dims)(__isl_keep PW *pw, enum isl_dim_type type,
908         unsigned first, unsigned n)
909 {
910         int i;
911         enum isl_dim_type set_type;
912
913         if (!pw)
914                 return -1;
915         if (pw->n == 0 || n == 0)
916                 return 0;
917
918         set_type = type == isl_dim_in ? isl_dim_set : type;
919
920         for (i = 0; i < pw->n; ++i) {
921                 int involves = FN(EL,involves_dims)(pw->p[i].FIELD,
922                                                         type, first, n);
923                 if (involves < 0 || involves)
924                         return involves;
925                 involves = isl_set_involves_dims(pw->p[i].set,
926                                                         set_type, first, n);
927                 if (involves < 0 || involves)
928                         return involves;
929         }
930         return 0;
931 }
932 #endif
933
934 __isl_give PW *FN(PW,set_dim_name)(__isl_take PW *pw,
935         enum isl_dim_type type, unsigned pos, const char *s)
936 {
937         int i;
938         enum isl_dim_type set_type;
939
940         pw = FN(PW,cow)(pw);
941         if (!pw)
942                 return NULL;
943
944         set_type = type == isl_dim_in ? isl_dim_set : type;
945
946         pw->dim = isl_space_set_dim_name(pw->dim, type, pos, s);
947         if (!pw->dim)
948                 goto error;
949
950         for (i = 0; i < pw->n; ++i) {
951                 pw->p[i].set = isl_set_set_dim_name(pw->p[i].set,
952                                                         set_type, pos, s);
953                 if (!pw->p[i].set)
954                         goto error;
955                 pw->p[i].FIELD = FN(EL,set_dim_name)(pw->p[i].FIELD, type, pos, s);
956                 if (!pw->p[i].FIELD)
957                         goto error;
958         }
959
960         return pw;
961 error:
962         FN(PW,free)(pw);
963         return NULL;
964 }
965
966 #ifndef NO_DROP_DIMS
967 __isl_give PW *FN(PW,drop_dims)(__isl_take PW *pw,
968         enum isl_dim_type type, unsigned first, unsigned n)
969 {
970         int i;
971         enum isl_dim_type set_type;
972
973         if (!pw)
974                 return NULL;
975         if (n == 0 && !isl_space_get_tuple_name(pw->dim, type))
976                 return pw;
977
978         set_type = type == isl_dim_in ? isl_dim_set : type;
979
980         pw = FN(PW,cow)(pw);
981         if (!pw)
982                 return NULL;
983         pw->dim = isl_space_drop_dims(pw->dim, type, first, n);
984         if (!pw->dim)
985                 goto error;
986         for (i = 0; i < pw->n; ++i) {
987                 pw->p[i].FIELD = FN(EL,drop_dims)(pw->p[i].FIELD, type, first, n);
988                 if (!pw->p[i].FIELD)
989                         goto error;
990                 if (type == isl_dim_out)
991                         continue;
992                 pw->p[i].set = isl_set_drop(pw->p[i].set, set_type, first, n);
993                 if (!pw->p[i].set)
994                         goto error;
995         }
996
997         return pw;
998 error:
999         FN(PW,free)(pw);
1000         return NULL;
1001 }
1002
1003 /* This function is very similar to drop_dims.
1004  * The only difference is that the cells may still involve
1005  * the specified dimensions.  They are removed using
1006  * isl_set_project_out instead of isl_set_drop.
1007  */
1008 __isl_give PW *FN(PW,project_out)(__isl_take PW *pw,
1009         enum isl_dim_type type, unsigned first, unsigned n)
1010 {
1011         int i;
1012         enum isl_dim_type set_type;
1013
1014         if (!pw)
1015                 return NULL;
1016         if (n == 0 && !isl_space_get_tuple_name(pw->dim, type))
1017                 return pw;
1018
1019         set_type = type == isl_dim_in ? isl_dim_set : type;
1020
1021         pw = FN(PW,cow)(pw);
1022         if (!pw)
1023                 return NULL;
1024         pw->dim = isl_space_drop_dims(pw->dim, type, first, n);
1025         if (!pw->dim)
1026                 goto error;
1027         for (i = 0; i < pw->n; ++i) {
1028                 pw->p[i].set = isl_set_project_out(pw->p[i].set,
1029                                                         set_type, first, n);
1030                 if (!pw->p[i].set)
1031                         goto error;
1032                 pw->p[i].FIELD = FN(EL,drop_dims)(pw->p[i].FIELD, type, first, n);
1033                 if (!pw->p[i].FIELD)
1034                         goto error;
1035         }
1036
1037         return pw;
1038 error:
1039         FN(PW,free)(pw);
1040         return NULL;
1041 }
1042
1043 /* Project the domain of pw onto its parameter space.
1044  */
1045 __isl_give PW *FN(PW,project_domain_on_params)(__isl_take PW *pw)
1046 {
1047         isl_space *space;
1048         unsigned n;
1049
1050         n = FN(PW,dim)(pw, isl_dim_in);
1051         pw = FN(PW,project_out)(pw, isl_dim_in, 0, n);
1052         space = FN(PW,get_domain_space)(pw);
1053         space = isl_space_params(space);
1054         pw = FN(PW,reset_domain_space)(pw, space);
1055         return pw;
1056 }
1057 #endif
1058
1059 #ifndef NO_INSERT_DIMS
1060 __isl_give PW *FN(PW,insert_dims)(__isl_take PW *pw, enum isl_dim_type type,
1061         unsigned first, unsigned n)
1062 {
1063         int i;
1064         enum isl_dim_type set_type;
1065
1066         if (!pw)
1067                 return NULL;
1068         if (n == 0 && !isl_space_is_named_or_nested(pw->dim, type))
1069                 return pw;
1070
1071         set_type = type == isl_dim_in ? isl_dim_set : type;
1072
1073         pw = FN(PW,cow)(pw);
1074         if (!pw)
1075                 return NULL;
1076
1077         pw->dim = isl_space_insert_dims(pw->dim, type, first, n);
1078         if (!pw->dim)
1079                 goto error;
1080
1081         for (i = 0; i < pw->n; ++i) {
1082                 pw->p[i].set = isl_set_insert_dims(pw->p[i].set,
1083                                                             set_type, first, n);
1084                 if (!pw->p[i].set)
1085                         goto error;
1086                 pw->p[i].FIELD = FN(EL,insert_dims)(pw->p[i].FIELD,
1087                                                                 type, first, n);
1088                 if (!pw->p[i].FIELD)
1089                         goto error;
1090         }
1091
1092         return pw;
1093 error:
1094         FN(PW,free)(pw);
1095         return NULL;
1096 }
1097 #endif
1098
1099 __isl_give PW *FN(PW,fix_dim)(__isl_take PW *pw,
1100         enum isl_dim_type type, unsigned pos, isl_int v)
1101 {
1102         int i;
1103
1104         if (!pw)
1105                 return NULL;
1106
1107         if (type == isl_dim_in)
1108                 type = isl_dim_set;
1109
1110         pw = FN(PW,cow)(pw);
1111         if (!pw)
1112                 return NULL;
1113         for (i = 0; i < pw->n; ++i) {
1114                 pw->p[i].set = isl_set_fix(pw->p[i].set, type, pos, v);
1115                 if (!pw->p[i].set)
1116                         goto error;
1117         }
1118
1119         return pw;
1120 error:
1121         FN(PW,free)(pw);
1122         return NULL;
1123 }
1124
1125 /* Fix the value of the variable at position "pos" of type "type" of "pw"
1126  * to be equal to "v".
1127  */
1128 __isl_give PW *FN(PW,fix_val)(__isl_take PW *pw,
1129         enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
1130 {
1131         if (!v)
1132                 return FN(PW,free)(pw);
1133         if (!isl_val_is_int(v))
1134                 isl_die(FN(PW,get_ctx)(pw), isl_error_invalid,
1135                         "expecting integer value", goto error);
1136
1137         pw = FN(PW,fix_dim)(pw, type, pos, v->n);
1138         isl_val_free(v);
1139
1140         return pw;
1141 error:
1142         isl_val_free(v);
1143         return FN(PW,free)(pw);
1144 }
1145
1146 unsigned FN(PW,dim)(__isl_keep PW *pw, enum isl_dim_type type)
1147 {
1148         return pw ? isl_space_dim(pw->dim, type) : 0;
1149 }
1150
1151 __isl_give PW *FN(PW,split_dims)(__isl_take PW *pw,
1152         enum isl_dim_type type, unsigned first, unsigned n)
1153 {
1154         int i;
1155
1156         if (!pw)
1157                 return NULL;
1158         if (n == 0)
1159                 return pw;
1160
1161         if (type == isl_dim_in)
1162                 type = isl_dim_set;
1163
1164         pw = FN(PW,cow)(pw);
1165         if (!pw)
1166                 return NULL;
1167         if (!pw->dim)
1168                 goto error;
1169         for (i = 0; i < pw->n; ++i) {
1170                 pw->p[i].set = isl_set_split_dims(pw->p[i].set, type, first, n);
1171                 if (!pw->p[i].set)
1172                         goto error;
1173         }
1174
1175         return pw;
1176 error:
1177         FN(PW,free)(pw);
1178         return NULL;
1179 }
1180
1181 #ifndef NO_OPT
1182 /* Compute the maximal value attained by the piecewise quasipolynomial
1183  * on its domain or zero if the domain is empty.
1184  * In the worst case, the domain is scanned completely,
1185  * so the domain is assumed to be bounded.
1186  */
1187 __isl_give isl_qpolynomial *FN(PW,opt)(__isl_take PW *pw, int max)
1188 {
1189         int i;
1190         isl_qpolynomial *opt;
1191
1192         if (!pw)
1193                 return NULL;
1194
1195         if (pw->n == 0) {
1196                 isl_space *dim = isl_space_copy(pw->dim);
1197                 FN(PW,free)(pw);
1198                 return isl_qpolynomial_zero_on_domain(isl_space_domain(dim));
1199         }
1200
1201         opt = FN(EL,opt_on_domain)(FN(EL,copy)(pw->p[0].FIELD),
1202                                         isl_set_copy(pw->p[0].set), max);
1203         for (i = 1; i < pw->n; ++i) {
1204                 isl_qpolynomial *opt_i;
1205                 opt_i = FN(EL,opt_on_domain)(FN(EL,copy)(pw->p[i].FIELD),
1206                                                 isl_set_copy(pw->p[i].set), max);
1207                 if (max)
1208                         opt = isl_qpolynomial_max_cst(opt, opt_i);
1209                 else
1210                         opt = isl_qpolynomial_min_cst(opt, opt_i);
1211         }
1212
1213         FN(PW,free)(pw);
1214         return opt;
1215 }
1216
1217 __isl_give isl_qpolynomial *FN(PW,max)(__isl_take PW *pw)
1218 {
1219         return FN(PW,opt)(pw, 1);
1220 }
1221
1222 __isl_give isl_qpolynomial *FN(PW,min)(__isl_take PW *pw)
1223 {
1224         return FN(PW,opt)(pw, 0);
1225 }
1226 #endif
1227
1228 __isl_give isl_space *FN(PW,get_space)(__isl_keep PW *pw)
1229 {
1230         return pw ? isl_space_copy(pw->dim) : NULL;
1231 }
1232
1233 __isl_give isl_space *FN(PW,get_domain_space)(__isl_keep PW *pw)
1234 {
1235         return pw ? isl_space_domain(isl_space_copy(pw->dim)) : NULL;
1236 }
1237
1238 #ifndef NO_RESET_DIM
1239 /* Reset the space of "pw".  Since we don't know if the elements
1240  * represent the spaces themselves or their domains, we pass along
1241  * both when we call their reset_space_and_domain.
1242  */
1243 static __isl_give PW *FN(PW,reset_space_and_domain)(__isl_take PW *pw,
1244         __isl_take isl_space *space, __isl_take isl_space *domain)
1245 {
1246         int i;
1247
1248         pw = FN(PW,cow)(pw);
1249         if (!pw || !space || !domain)
1250                 goto error;
1251
1252         for (i = 0; i < pw->n; ++i) {
1253                 pw->p[i].set = isl_set_reset_space(pw->p[i].set,
1254                                                  isl_space_copy(domain));
1255                 if (!pw->p[i].set)
1256                         goto error;
1257                 pw->p[i].FIELD = FN(EL,reset_space_and_domain)(pw->p[i].FIELD,
1258                               isl_space_copy(space), isl_space_copy(domain));
1259                 if (!pw->p[i].FIELD)
1260                         goto error;
1261         }
1262
1263         isl_space_free(domain);
1264
1265         isl_space_free(pw->dim);
1266         pw->dim = space;
1267
1268         return pw;
1269 error:
1270         isl_space_free(domain);
1271         isl_space_free(space);
1272         FN(PW,free)(pw);
1273         return NULL;
1274 }
1275
1276 __isl_give PW *FN(PW,reset_domain_space)(__isl_take PW *pw,
1277         __isl_take isl_space *domain)
1278 {
1279         isl_space *space;
1280
1281         space = isl_space_extend_domain_with_range(isl_space_copy(domain),
1282                                                    FN(PW,get_space)(pw));
1283         return FN(PW,reset_space_and_domain)(pw, space, domain);
1284 }
1285
1286 __isl_give PW *FN(PW,reset_space)(__isl_take PW *pw, __isl_take isl_space *dim)
1287 {
1288         isl_space *domain;
1289
1290         domain = isl_space_domain(isl_space_copy(dim));
1291         return FN(PW,reset_space_and_domain)(pw, dim, domain);
1292 }
1293
1294 __isl_give PW *FN(PW,set_tuple_id)(__isl_keep PW *pw, enum isl_dim_type type,
1295         __isl_take isl_id *id)
1296 {
1297         isl_space *space;
1298
1299         pw = FN(PW,cow)(pw);
1300         if (!pw)
1301                 return isl_id_free(id);
1302
1303         space = FN(PW,get_space)(pw);
1304         space = isl_space_set_tuple_id(space, type, id);
1305
1306         return FN(PW,reset_space)(pw, space);
1307 }
1308
1309 __isl_give PW *FN(PW,set_dim_id)(__isl_take PW *pw,
1310         enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
1311 {
1312         pw = FN(PW,cow)(pw);
1313         if (!pw)
1314                 return isl_id_free(id);
1315         pw->dim = isl_space_set_dim_id(pw->dim, type, pos, id);
1316         return FN(PW,reset_space)(pw, isl_space_copy(pw->dim));
1317 }
1318 #endif
1319
1320 int FN(PW,has_equal_space)(__isl_keep PW *pw1, __isl_keep PW *pw2)
1321 {
1322         if (!pw1 || !pw2)
1323                 return -1;
1324
1325         return isl_space_is_equal(pw1->dim, pw2->dim);
1326 }
1327
1328 #ifndef NO_MORPH
1329 __isl_give PW *FN(PW,morph_domain)(__isl_take PW *pw,
1330         __isl_take isl_morph *morph)
1331 {
1332         int i;
1333         isl_ctx *ctx;
1334
1335         if (!pw || !morph)
1336                 goto error;
1337
1338         ctx = isl_space_get_ctx(pw->dim);
1339         isl_assert(ctx, isl_space_is_domain_internal(morph->dom->dim, pw->dim),
1340                 goto error);
1341
1342         pw = FN(PW,cow)(pw);
1343         if (!pw)
1344                 goto error;
1345         pw->dim = isl_space_extend_domain_with_range(
1346                         isl_space_copy(morph->ran->dim), pw->dim);
1347         if (!pw->dim)
1348                 goto error;
1349
1350         for (i = 0; i < pw->n; ++i) {
1351                 pw->p[i].set = isl_morph_set(isl_morph_copy(morph), pw->p[i].set);
1352                 if (!pw->p[i].set)
1353                         goto error;
1354                 pw->p[i].FIELD = FN(EL,morph_domain)(pw->p[i].FIELD,
1355                                                 isl_morph_copy(morph));
1356                 if (!pw->p[i].FIELD)
1357                         goto error;
1358         }
1359
1360         isl_morph_free(morph);
1361
1362         return pw;
1363 error:
1364         FN(PW,free)(pw);
1365         isl_morph_free(morph);
1366         return NULL;
1367 }
1368 #endif
1369
1370 int FN(PW,n_piece)(__isl_keep PW *pw)
1371 {
1372         return pw ? pw->n : 0;
1373 }
1374
1375 int FN(PW,foreach_piece)(__isl_keep PW *pw,
1376         int (*fn)(__isl_take isl_set *set, __isl_take EL *el, void *user),
1377         void *user)
1378 {
1379         int i;
1380
1381         if (!pw)
1382                 return -1;
1383
1384         for (i = 0; i < pw->n; ++i)
1385                 if (fn(isl_set_copy(pw->p[i].set),
1386                                 FN(EL,copy)(pw->p[i].FIELD), user) < 0)
1387                         return -1;
1388
1389         return 0;
1390 }
1391
1392 #ifndef NO_LIFT
1393 static int any_divs(__isl_keep isl_set *set)
1394 {
1395         int i;
1396
1397         if (!set)
1398                 return -1;
1399
1400         for (i = 0; i < set->n; ++i)
1401                 if (set->p[i]->n_div > 0)
1402                         return 1;
1403
1404         return 0;
1405 }
1406
1407 static int foreach_lifted_subset(__isl_take isl_set *set, __isl_take EL *el,
1408         int (*fn)(__isl_take isl_set *set, __isl_take EL *el,
1409                     void *user), void *user)
1410 {
1411         int i;
1412
1413         if (!set || !el)
1414                 goto error;
1415
1416         for (i = 0; i < set->n; ++i) {
1417                 isl_set *lift;
1418                 EL *copy;
1419
1420                 lift = isl_set_from_basic_set(isl_basic_set_copy(set->p[i]));
1421                 lift = isl_set_lift(lift);
1422
1423                 copy = FN(EL,copy)(el);
1424                 copy = FN(EL,lift)(copy, isl_set_get_space(lift));
1425
1426                 if (fn(lift, copy, user) < 0)
1427                         goto error;
1428         }
1429
1430         isl_set_free(set);
1431         FN(EL,free)(el);
1432
1433         return 0;
1434 error:
1435         isl_set_free(set);
1436         FN(EL,free)(el);
1437         return -1;
1438 }
1439
1440 int FN(PW,foreach_lifted_piece)(__isl_keep PW *pw,
1441         int (*fn)(__isl_take isl_set *set, __isl_take EL *el,
1442                     void *user), void *user)
1443 {
1444         int i;
1445
1446         if (!pw)
1447                 return -1;
1448
1449         for (i = 0; i < pw->n; ++i) {
1450                 isl_set *set;
1451                 EL *el;
1452
1453                 set = isl_set_copy(pw->p[i].set);
1454                 el = FN(EL,copy)(pw->p[i].FIELD);
1455                 if (!any_divs(set)) {
1456                         if (fn(set, el, user) < 0)
1457                                 return -1;
1458                         continue;
1459                 }
1460                 if (foreach_lifted_subset(set, el, fn, user) < 0)
1461                         return -1;
1462         }
1463
1464         return 0;
1465 }
1466 #endif
1467
1468 #ifndef NO_MOVE_DIMS
1469 __isl_give PW *FN(PW,move_dims)(__isl_take PW *pw,
1470         enum isl_dim_type dst_type, unsigned dst_pos,
1471         enum isl_dim_type src_type, unsigned src_pos, unsigned n)
1472 {
1473         int i;
1474
1475         pw = FN(PW,cow)(pw);
1476         if (!pw)
1477                 return NULL;
1478
1479         pw->dim = isl_space_move_dims(pw->dim, dst_type, dst_pos, src_type, src_pos, n);
1480         if (!pw->dim)
1481                 goto error;
1482
1483         for (i = 0; i < pw->n; ++i) {
1484                 pw->p[i].FIELD = FN(EL,move_dims)(pw->p[i].FIELD,
1485                                         dst_type, dst_pos, src_type, src_pos, n);
1486                 if (!pw->p[i].FIELD)
1487                         goto error;
1488         }
1489
1490         if (dst_type == isl_dim_in)
1491                 dst_type = isl_dim_set;
1492         if (src_type == isl_dim_in)
1493                 src_type = isl_dim_set;
1494
1495         for (i = 0; i < pw->n; ++i) {
1496                 pw->p[i].set = isl_set_move_dims(pw->p[i].set,
1497                                                 dst_type, dst_pos,
1498                                                 src_type, src_pos, n);
1499                 if (!pw->p[i].set)
1500                         goto error;
1501         }
1502
1503         return pw;
1504 error:
1505         FN(PW,free)(pw);
1506         return NULL;
1507 }
1508 #endif
1509
1510 __isl_give PW *FN(PW,mul_isl_int)(__isl_take PW *pw, isl_int v)
1511 {
1512         int i;
1513
1514         if (isl_int_is_one(v))
1515                 return pw;
1516         if (pw && DEFAULT_IS_ZERO && isl_int_is_zero(v)) {
1517                 PW *zero;
1518                 isl_space *dim = FN(PW,get_space)(pw);
1519 #ifdef HAS_TYPE
1520                 zero = FN(PW,ZERO)(dim, pw->type);
1521 #else
1522                 zero = FN(PW,ZERO)(dim);
1523 #endif
1524                 FN(PW,free)(pw);
1525                 return zero;
1526         }
1527         pw = FN(PW,cow)(pw);
1528         if (!pw)
1529                 return NULL;
1530         if (pw->n == 0)
1531                 return pw;
1532
1533 #ifdef HAS_TYPE
1534         if (isl_int_is_neg(v))
1535                 pw->type = isl_fold_type_negate(pw->type);
1536 #endif
1537         for (i = 0; i < pw->n; ++i) {
1538                 pw->p[i].FIELD = FN(EL,scale)(pw->p[i].FIELD, v);
1539                 if (!pw->p[i].FIELD)
1540                         goto error;
1541         }
1542
1543         return pw;
1544 error:
1545         FN(PW,free)(pw);
1546         return NULL;
1547 }
1548
1549 /* Multiply the pieces of "pw" by "v" and return the result.
1550  */
1551 __isl_give PW *FN(PW,scale_val)(__isl_take PW *pw, __isl_take isl_val *v)
1552 {
1553         int i;
1554
1555         if (!pw || !v)
1556                 goto error;
1557
1558         if (isl_val_is_one(v)) {
1559                 isl_val_free(v);
1560                 return pw;
1561         }
1562         if (pw && DEFAULT_IS_ZERO && isl_val_is_zero(v)) {
1563                 PW *zero;
1564                 isl_space *space = FN(PW,get_space)(pw);
1565 #ifdef HAS_TYPE
1566                 zero = FN(PW,ZERO)(space, pw->type);
1567 #else
1568                 zero = FN(PW,ZERO)(space);
1569 #endif
1570                 FN(PW,free)(pw);
1571                 isl_val_free(v);
1572                 return zero;
1573         }
1574         if (pw->n == 0) {
1575                 isl_val_free(v);
1576                 return pw;
1577         }
1578         pw = FN(PW,cow)(pw);
1579         if (!pw)
1580                 goto error;
1581
1582 #ifdef HAS_TYPE
1583         if (isl_val_is_neg(v))
1584                 pw->type = isl_fold_type_negate(pw->type);
1585 #endif
1586         for (i = 0; i < pw->n; ++i) {
1587                 pw->p[i].FIELD = FN(EL,scale_val)(pw->p[i].FIELD,
1588                                                     isl_val_copy(v));
1589                 if (!pw->p[i].FIELD)
1590                         goto error;
1591         }
1592
1593         isl_val_free(v);
1594         return pw;
1595 error:
1596         isl_val_free(v);
1597         FN(PW,free)(pw);
1598         return NULL;
1599 }
1600
1601 __isl_give PW *FN(PW,scale)(__isl_take PW *pw, isl_int v)
1602 {
1603         return FN(PW,mul_isl_int)(pw, v);
1604 }
1605
1606 static int FN(PW,qsort_set_cmp)(const void *p1, const void *p2)
1607 {
1608         isl_set *set1 = *(isl_set * const *)p1;
1609         isl_set *set2 = *(isl_set * const *)p2;
1610
1611         return isl_set_plain_cmp(set1, set2);
1612 }
1613
1614 /* We normalize in place, but if anything goes wrong we need
1615  * to return NULL, so we need to make sure we don't change the
1616  * meaning of any possible other copies of map.
1617  */
1618 __isl_give PW *FN(PW,normalize)(__isl_take PW *pw)
1619 {
1620         int i, j;
1621         isl_set *set;
1622
1623         if (!pw)
1624                 return NULL;
1625         for (i = 0; i < pw->n; ++i) {
1626                 set = isl_set_normalize(isl_set_copy(pw->p[i].set));
1627                 if (!set)
1628                         return FN(PW,free)(pw);
1629                 isl_set_free(pw->p[i].set);
1630                 pw->p[i].set = set;
1631         }
1632         qsort(pw->p, pw->n, sizeof(pw->p[0]), &FN(PW,qsort_set_cmp));
1633         for (i = pw->n - 1; i >= 1; --i) {
1634                 if (!isl_set_plain_is_equal(pw->p[i - 1].set, pw->p[i].set))
1635                         continue;
1636                 if (!FN(EL,plain_is_equal)(pw->p[i - 1].FIELD, pw->p[i].FIELD))
1637                         continue;
1638                 set = isl_set_union(isl_set_copy(pw->p[i - 1].set),
1639                                     isl_set_copy(pw->p[i].set));
1640                 if (!set)
1641                         return FN(PW,free)(pw);
1642                 isl_set_free(pw->p[i].set);
1643                 FN(EL,free)(pw->p[i].FIELD);
1644                 isl_set_free(pw->p[i - 1].set);
1645                 pw->p[i - 1].set = set;
1646                 for (j = i + 1; j < pw->n; ++j)
1647                         pw->p[j - 1] = pw->p[j];
1648                 pw->n--;
1649         }
1650
1651         return pw;
1652 }
1653
1654 /* Is pw1 obviously equal to pw2?
1655  * That is, do they have obviously identical cells and obviously identical
1656  * elements on each cell?
1657  */
1658 int FN(PW,plain_is_equal)(__isl_keep PW *pw1, __isl_keep PW *pw2)
1659 {
1660         int i;
1661         int equal;
1662
1663         if (!pw1 || !pw2)
1664                 return -1;
1665
1666         if (pw1 == pw2)
1667                 return 1;
1668         if (!isl_space_is_equal(pw1->dim, pw2->dim))
1669                 return 0;
1670
1671         pw1 = FN(PW,copy)(pw1);
1672         pw2 = FN(PW,copy)(pw2);
1673         pw1 = FN(PW,normalize)(pw1);
1674         pw2 = FN(PW,normalize)(pw2);
1675         if (!pw1 || !pw2)
1676                 goto error;
1677
1678         equal = pw1->n == pw2->n;
1679         for (i = 0; equal && i < pw1->n; ++i) {
1680                 equal = isl_set_plain_is_equal(pw1->p[i].set, pw2->p[i].set);
1681                 if (equal < 0)
1682                         goto error;
1683                 if (!equal)
1684                         break;
1685                 equal = FN(EL,plain_is_equal)(pw1->p[i].FIELD, pw2->p[i].FIELD);
1686                 if (equal < 0)
1687                         goto error;
1688         }
1689
1690         FN(PW,free)(pw1);
1691         FN(PW,free)(pw2);
1692         return equal;
1693 error:
1694         FN(PW,free)(pw1);
1695         FN(PW,free)(pw2);
1696         return -1;
1697 }
1698
1699 #ifndef NO_PULLBACK
1700 static __isl_give PW *FN(PW,align_params_pw_multi_aff_and)(__isl_take PW *pw,
1701         __isl_take isl_multi_aff *ma,
1702         __isl_give PW *(*fn)(__isl_take PW *pw, __isl_take isl_multi_aff *ma))
1703 {
1704         isl_ctx *ctx;
1705         isl_space *ma_space;
1706
1707         ma_space = isl_multi_aff_get_space(ma);
1708         if (!pw || !ma || !ma_space)
1709                 goto error;
1710         if (isl_space_match(pw->dim, isl_dim_param, ma_space, isl_dim_param)) {
1711                 isl_space_free(ma_space);
1712                 return fn(pw, ma);
1713         }
1714         ctx = FN(PW,get_ctx)(pw);
1715         if (!isl_space_has_named_params(pw->dim) ||
1716             !isl_space_has_named_params(ma_space))
1717                 isl_die(ctx, isl_error_invalid,
1718                         "unaligned unnamed parameters", goto error);
1719         pw = FN(PW,align_params)(pw, ma_space);
1720         ma = isl_multi_aff_align_params(ma, FN(PW,get_space)(pw));
1721         return fn(pw, ma);
1722 error:
1723         isl_space_free(ma_space);
1724         FN(PW,free)(pw);
1725         isl_multi_aff_free(ma);
1726         return NULL;
1727 }
1728
1729 static __isl_give PW *FN(PW,align_params_pw_pw_multi_aff_and)(__isl_take PW *pw,
1730         __isl_take isl_pw_multi_aff *pma,
1731         __isl_give PW *(*fn)(__isl_take PW *pw,
1732                 __isl_take isl_pw_multi_aff *ma))
1733 {
1734         isl_ctx *ctx;
1735         isl_space *pma_space;
1736
1737         pma_space = isl_pw_multi_aff_get_space(pma);
1738         if (!pw || !pma || !pma_space)
1739                 goto error;
1740         if (isl_space_match(pw->dim, isl_dim_param, pma_space, isl_dim_param)) {
1741                 isl_space_free(pma_space);
1742                 return fn(pw, pma);
1743         }
1744         ctx = FN(PW,get_ctx)(pw);
1745         if (!isl_space_has_named_params(pw->dim) ||
1746             !isl_space_has_named_params(pma_space))
1747                 isl_die(ctx, isl_error_invalid,
1748                         "unaligned unnamed parameters", goto error);
1749         pw = FN(PW,align_params)(pw, pma_space);
1750         pma = isl_pw_multi_aff_align_params(pma, FN(PW,get_space)(pw));
1751         return fn(pw, pma);
1752 error:
1753         isl_space_free(pma_space);
1754         FN(PW,free)(pw);
1755         isl_pw_multi_aff_free(pma);
1756         return NULL;
1757 }
1758
1759 /* Compute the pullback of "pw" by the function represented by "ma".
1760  * In other words, plug in "ma" in "pw".
1761  */
1762 static __isl_give PW *FN(PW,pullback_multi_aff_aligned)(__isl_take PW *pw,
1763         __isl_take isl_multi_aff *ma)
1764 {
1765         int i;
1766         isl_space *space = NULL;
1767
1768         ma = isl_multi_aff_align_divs(ma);
1769         pw = FN(PW,cow)(pw);
1770         if (!pw || !ma)
1771                 goto error;
1772
1773         space = isl_space_join(isl_multi_aff_get_space(ma),
1774                                 FN(PW,get_space)(pw));
1775
1776         for (i = 0; i < pw->n; ++i) {
1777                 pw->p[i].set = isl_set_preimage_multi_aff(pw->p[i].set,
1778                                                     isl_multi_aff_copy(ma));
1779                 if (!pw->p[i].set)
1780                         goto error;
1781                 pw->p[i].FIELD = FN(EL,pullback_multi_aff)(pw->p[i].FIELD,
1782                                                     isl_multi_aff_copy(ma));
1783                 if (!pw->p[i].FIELD)
1784                         goto error;
1785         }
1786
1787         pw = FN(PW,reset_space)(pw, space);
1788         isl_multi_aff_free(ma);
1789         return pw;
1790 error:
1791         isl_space_free(space);
1792         isl_multi_aff_free(ma);
1793         FN(PW,free)(pw);
1794         return NULL;
1795 }
1796
1797 __isl_give PW *FN(PW,pullback_multi_aff)(__isl_take PW *pw,
1798         __isl_take isl_multi_aff *ma)
1799 {
1800         return FN(PW,align_params_pw_multi_aff_and)(pw, ma,
1801                                         &FN(PW,pullback_multi_aff_aligned));
1802 }
1803
1804 /* Compute the pullback of "pw" by the function represented by "pma".
1805  * In other words, plug in "pma" in "pw".
1806  */
1807 static __isl_give PW *FN(PW,pullback_pw_multi_aff_aligned)(__isl_take PW *pw,
1808         __isl_take isl_pw_multi_aff *pma)
1809 {
1810         int i;
1811         PW *res;
1812
1813         if (!pma)
1814                 goto error;
1815
1816         if (pma->n == 0) {
1817                 isl_pw_multi_aff_free(pma);
1818                 res = FN(PW,empty)(FN(PW,get_space)(pw));
1819                 FN(PW,free)(pw);
1820                 return res;
1821         }
1822
1823         res = FN(PW,pullback_multi_aff)(FN(PW,copy)(pw),
1824                                         isl_multi_aff_copy(pma->p[0].maff));
1825         res = FN(PW,intersect_domain)(res, isl_set_copy(pma->p[0].set));
1826
1827         for (i = 1; i < pma->n; ++i) {
1828                 PW *res_i;
1829
1830                 res_i = FN(PW,pullback_multi_aff)(FN(PW,copy)(pw),
1831                                         isl_multi_aff_copy(pma->p[i].maff));
1832                 res_i = FN(PW,intersect_domain)(res_i,
1833                                         isl_set_copy(pma->p[i].set));
1834                 res = FN(PW,add_disjoint)(res, res_i);
1835         }
1836
1837         isl_pw_multi_aff_free(pma);
1838         FN(PW,free)(pw);
1839         return res;
1840 error:
1841         isl_pw_multi_aff_free(pma);
1842         FN(PW,free)(pw);
1843         return NULL;
1844 }
1845
1846 __isl_give PW *FN(PW,pullback_pw_multi_aff)(__isl_take PW *pw,
1847         __isl_take isl_pw_multi_aff *pma)
1848 {
1849         return FN(PW,align_params_pw_pw_multi_aff_and)(pw, pma,
1850                                         &FN(PW,pullback_pw_multi_aff_aligned));
1851 }
1852 #endif