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