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