add isl_pw_aff_n_piece
[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)(__isl_take PW *pw1,
515         __isl_take PW *pw2,
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  */
521 static __isl_give PW *FN(PW,on_shared_domain)(__isl_take PW *pw1,
522         __isl_take PW *pw2,
523         __isl_give EL *(*fn)(__isl_take EL *el1, __isl_take EL *el2))
524 {
525         int i, j, n;
526         PW *res;
527
528         if (!pw1 || !pw2)
529                 goto error;
530
531         n = pw1->n * pw2->n;
532 #ifdef HAS_TYPE
533         res = FN(PW,alloc_size)(isl_space_copy(pw1->dim), pw1->type, n);
534 #else
535         res = FN(PW,alloc_size)(isl_space_copy(pw1->dim), n);
536 #endif
537
538         for (i = 0; i < pw1->n; ++i) {
539                 for (j = 0; j < pw2->n; ++j) {
540                         isl_set *common;
541                         EL *res_ij;
542                         common = isl_set_intersect(
543                                         isl_set_copy(pw1->p[i].set),
544                                         isl_set_copy(pw2->p[j].set));
545                         if (isl_set_plain_is_empty(common)) {
546                                 isl_set_free(common);
547                                 continue;
548                         }
549
550                         res_ij = fn(FN(EL,copy)(pw1->p[i].FIELD),
551                                     FN(EL,copy)(pw2->p[j].FIELD));
552                         res_ij = FN(EL,gist)(res_ij, isl_set_copy(common));
553
554                         res = FN(PW,add_piece)(res, common, res_ij);
555                 }
556         }
557
558         FN(PW,free)(pw1);
559         FN(PW,free)(pw2);
560         return res;
561 error:
562         FN(PW,free)(pw1);
563         FN(PW,free)(pw2);
564         return NULL;
565 }
566
567 #ifndef NO_NEG
568 __isl_give PW *FN(PW,neg)(__isl_take PW *pw)
569 {
570         int i;
571
572         if (!pw)
573                 return NULL;
574
575         if (FN(PW,IS_ZERO)(pw))
576                 return pw;
577
578         pw = FN(PW,cow)(pw);
579         if (!pw)
580                 return NULL;
581
582         for (i = 0; i < pw->n; ++i) {
583                 pw->p[i].FIELD = FN(EL,neg)(pw->p[i].FIELD);
584                 if (!pw->p[i].FIELD)
585                         return FN(PW,free)(pw);
586         }
587
588         return pw;
589 }
590
591 __isl_give PW *FN(PW,sub)(__isl_take PW *pw1, __isl_take PW *pw2)
592 {
593         return FN(PW,add)(pw1, FN(PW,neg)(pw2));
594 }
595 #endif
596
597 #ifndef NO_EVAL
598 __isl_give isl_qpolynomial *FN(PW,eval)(__isl_take PW *pw,
599         __isl_take isl_point *pnt)
600 {
601         int i;
602         int found = 0;
603         isl_ctx *ctx;
604         isl_space *pnt_dim = NULL;
605         isl_qpolynomial *qp;
606
607         if (!pw || !pnt)
608                 goto error;
609         ctx = isl_point_get_ctx(pnt);
610         pnt_dim = isl_point_get_space(pnt);
611         isl_assert(ctx, isl_space_is_domain(pnt_dim, pw->dim), goto error);
612
613         for (i = 0; i < pw->n; ++i) {
614                 found = isl_set_contains_point(pw->p[i].set, pnt);
615                 if (found < 0)
616                         goto error;
617                 if (found)
618                         break;
619         }
620         if (found)
621                 qp = FN(EL,eval)(FN(EL,copy)(pw->p[i].FIELD),
622                                             isl_point_copy(pnt));
623         else
624                 qp = isl_qpolynomial_zero_on_domain(FN(PW,get_domain_space)(pw));
625         FN(PW,free)(pw);
626         isl_space_free(pnt_dim);
627         isl_point_free(pnt);
628         return qp;
629 error:
630         FN(PW,free)(pw);
631         isl_space_free(pnt_dim);
632         isl_point_free(pnt);
633         return NULL;
634 }
635 #endif
636
637 __isl_give isl_set *FN(PW,domain)(__isl_take PW *pw)
638 {
639         int i;
640         isl_set *dom;
641
642         if (!pw)
643                 return NULL;
644
645         dom = isl_set_empty(FN(PW,get_domain_space)(pw));
646         for (i = 0; i < pw->n; ++i)
647                 dom = isl_set_union_disjoint(dom, isl_set_copy(pw->p[i].set));
648
649         FN(PW,free)(pw);
650
651         return dom;
652 }
653
654 /* Restrict the domain of "pw" by combining each cell
655  * with "set" through a call to "fn", where "fn" may be
656  * isl_set_intersect or isl_set_intersect_params.
657  */
658 static __isl_give PW *FN(PW,intersect_aligned)(__isl_take PW *pw,
659         __isl_take isl_set *set,
660         __isl_give isl_set *(*fn)(__isl_take isl_set *set1,
661                                     __isl_take isl_set *set2))
662 {
663         int i;
664
665         if (!pw || !set)
666                 goto error;
667
668         if (pw->n == 0) {
669                 isl_set_free(set);
670                 return pw;
671         }
672
673         pw = FN(PW,cow)(pw);
674         if (!pw)
675                 goto error;
676
677         for (i = pw->n - 1; i >= 0; --i) {
678                 isl_basic_set *aff;
679                 pw->p[i].set = fn(pw->p[i].set, isl_set_copy(set));
680                 if (!pw->p[i].set)
681                         goto error;
682                 aff = isl_set_affine_hull(isl_set_copy(pw->p[i].set));
683                 pw->p[i].FIELD = FN(EL,substitute_equalities)(pw->p[i].FIELD,
684                                                                 aff);
685                 if (!pw->p[i].FIELD)
686                         goto error;
687                 if (isl_set_plain_is_empty(pw->p[i].set)) {
688                         isl_set_free(pw->p[i].set);
689                         FN(EL,free)(pw->p[i].FIELD);
690                         if (i != pw->n - 1)
691                                 pw->p[i] = pw->p[pw->n - 1];
692                         pw->n--;
693                 }
694         }
695         
696         isl_set_free(set);
697         return pw;
698 error:
699         isl_set_free(set);
700         FN(PW,free)(pw);
701         return NULL;
702 }
703
704 static __isl_give PW *FN(PW,intersect_domain_aligned)(__isl_take PW *pw,
705         __isl_take isl_set *set)
706 {
707         return FN(PW,intersect_aligned)(pw, set, &isl_set_intersect);
708 }
709
710 __isl_give PW *FN(PW,intersect_domain)(__isl_take PW *pw,
711         __isl_take isl_set *context)
712 {
713         return FN(PW,align_params_pw_set_and)(pw, context,
714                                         &FN(PW,intersect_domain_aligned));
715 }
716
717 static __isl_give PW *FN(PW,intersect_params_aligned)(__isl_take PW *pw,
718         __isl_take isl_set *set)
719 {
720         return FN(PW,intersect_aligned)(pw, set, &isl_set_intersect_params);
721 }
722
723 /* Intersect the domain of "pw" with the parameter domain "context".
724  */
725 __isl_give PW *FN(PW,intersect_params)(__isl_take PW *pw,
726         __isl_take isl_set *context)
727 {
728         return FN(PW,align_params_pw_set_and)(pw, context,
729                                         &FN(PW,intersect_params_aligned));
730 }
731
732 static __isl_give PW *FN(PW,gist_aligned)(__isl_take PW *pw,
733         __isl_take isl_set *context,
734         __isl_give EL *(*fn_el)(__isl_take EL *el,
735                                     __isl_take isl_set *set),
736         __isl_give isl_set *(*fn_dom)(__isl_take isl_set *set,
737                                     __isl_take isl_basic_set *bset))
738 {
739         int i;
740         isl_basic_set *hull = NULL;
741
742         if (!pw || !context)
743                 goto error;
744
745         if (pw->n == 0) {
746                 isl_set_free(context);
747                 return pw;
748         }
749
750         if (!isl_space_match(pw->dim, isl_dim_param,
751                                 context->dim, isl_dim_param)) {
752                 pw = FN(PW,align_params)(pw, isl_set_get_space(context));
753                 context = isl_set_align_params(context, FN(PW,get_space)(pw));
754         }
755
756         context = isl_set_compute_divs(context);
757         hull = isl_set_simple_hull(isl_set_copy(context));
758
759         pw = FN(PW,cow)(pw);
760         if (!pw)
761                 goto error;
762
763         for (i = pw->n - 1; i >= 0; --i) {
764                 pw->p[i].set = isl_set_intersect(pw->p[i].set,
765                                                  isl_set_copy(context));
766                 if (!pw->p[i].set)
767                         goto error;
768                 pw->p[i].FIELD = fn_el(pw->p[i].FIELD,
769                                              isl_set_copy(pw->p[i].set));
770                 pw->p[i].set = fn_dom(pw->p[i].set, isl_basic_set_copy(hull));
771                 if (!pw->p[i].set)
772                         goto error;
773                 if (isl_set_plain_is_empty(pw->p[i].set)) {
774                         isl_set_free(pw->p[i].set);
775                         FN(EL,free)(pw->p[i].FIELD);
776                         if (i != pw->n - 1)
777                                 pw->p[i] = pw->p[pw->n - 1];
778                         pw->n--;
779                 }
780         }
781
782         isl_basic_set_free(hull);
783         isl_set_free(context);
784
785         return pw;
786 error:
787         FN(PW,free)(pw);
788         isl_basic_set_free(hull);
789         isl_set_free(context);
790         return NULL;
791 }
792
793 static __isl_give PW *FN(PW,gist_domain_aligned)(__isl_take PW *pw,
794         __isl_take isl_set *set)
795 {
796         return FN(PW,gist_aligned)(pw, set, &FN(EL,gist),
797                                         &isl_set_gist_basic_set);
798 }
799
800 __isl_give PW *FN(PW,gist)(__isl_take PW *pw, __isl_take isl_set *context)
801 {
802         return FN(PW,align_params_pw_set_and)(pw, context,
803                                                 &FN(PW,gist_domain_aligned));
804 }
805
806 static __isl_give PW *FN(PW,gist_params_aligned)(__isl_take PW *pw,
807         __isl_take isl_set *set)
808 {
809         return FN(PW,gist_aligned)(pw, set, &FN(EL,gist_params),
810                                         &isl_set_gist_params_basic_set);
811 }
812
813 __isl_give PW *FN(PW,gist_params)(__isl_take PW *pw,
814         __isl_take isl_set *context)
815 {
816         return FN(PW,align_params_pw_set_and)(pw, context,
817                                                 &FN(PW,gist_params_aligned));
818 }
819
820 __isl_give PW *FN(PW,coalesce)(__isl_take PW *pw)
821 {
822         int i, j;
823
824         if (!pw)
825                 return NULL;
826         if (pw->n == 0)
827                 return pw;
828
829         for (i = pw->n - 1; i >= 0; --i) {
830                 for (j = i - 1; j >= 0; --j) {
831                         if (!FN(EL,plain_is_equal)(pw->p[i].FIELD,
832                                                         pw->p[j].FIELD))
833                                 continue;
834                         pw->p[j].set = isl_set_union(pw->p[j].set,
835                                                         pw->p[i].set);
836                         FN(EL,free)(pw->p[i].FIELD);
837                         if (i != pw->n - 1)
838                                 pw->p[i] = pw->p[pw->n - 1];
839                         pw->n--;
840                         break;
841                 }
842                 if (j >= 0)
843                         continue;
844                 pw->p[i].set = isl_set_coalesce(pw->p[i].set);
845                 if (!pw->p[i].set)
846                         goto error;
847         }
848
849         return pw;
850 error:
851         FN(PW,free)(pw);
852         return NULL;
853 }
854
855 isl_ctx *FN(PW,get_ctx)(__isl_keep PW *pw)
856 {
857         return pw ? isl_space_get_ctx(pw->dim) : NULL;
858 }
859
860 #ifndef NO_INVOLVES_DIMS
861 int FN(PW,involves_dims)(__isl_keep PW *pw, enum isl_dim_type type,
862         unsigned first, unsigned n)
863 {
864         int i;
865         enum isl_dim_type set_type;
866
867         if (!pw)
868                 return -1;
869         if (pw->n == 0 || n == 0)
870                 return 0;
871
872         set_type = type == isl_dim_in ? isl_dim_set : type;
873
874         for (i = 0; i < pw->n; ++i) {
875                 int involves = FN(EL,involves_dims)(pw->p[i].FIELD,
876                                                         type, first, n);
877                 if (involves < 0 || involves)
878                         return involves;
879                 involves = isl_set_involves_dims(pw->p[i].set,
880                                                         set_type, first, n);
881                 if (involves < 0 || involves)
882                         return involves;
883         }
884         return 0;
885 }
886 #endif
887
888 __isl_give PW *FN(PW,set_dim_name)(__isl_take PW *pw,
889         enum isl_dim_type type, unsigned pos, const char *s)
890 {
891         int i;
892         enum isl_dim_type set_type;
893
894         pw = FN(PW,cow)(pw);
895         if (!pw)
896                 return NULL;
897
898         set_type = type == isl_dim_in ? isl_dim_set : type;
899
900         pw->dim = isl_space_set_dim_name(pw->dim, type, pos, s);
901         if (!pw->dim)
902                 goto error;
903
904         for (i = 0; i < pw->n; ++i) {
905                 pw->p[i].set = isl_set_set_dim_name(pw->p[i].set,
906                                                         set_type, pos, s);
907                 if (!pw->p[i].set)
908                         goto error;
909                 pw->p[i].FIELD = FN(EL,set_dim_name)(pw->p[i].FIELD, type, pos, s);
910                 if (!pw->p[i].FIELD)
911                         goto error;
912         }
913
914         return pw;
915 error:
916         FN(PW,free)(pw);
917         return NULL;
918 }
919
920 #ifndef NO_DROP_DIMS
921 __isl_give PW *FN(PW,drop_dims)(__isl_take PW *pw,
922         enum isl_dim_type type, unsigned first, unsigned n)
923 {
924         int i;
925         enum isl_dim_type set_type;
926
927         if (!pw)
928                 return NULL;
929         if (n == 0 && !isl_space_get_tuple_name(pw->dim, type))
930                 return pw;
931
932         set_type = type == isl_dim_in ? isl_dim_set : type;
933
934         pw = FN(PW,cow)(pw);
935         if (!pw)
936                 return NULL;
937         pw->dim = isl_space_drop_dims(pw->dim, type, first, n);
938         if (!pw->dim)
939                 goto error;
940         for (i = 0; i < pw->n; ++i) {
941                 pw->p[i].set = isl_set_drop(pw->p[i].set, set_type, first, n);
942                 if (!pw->p[i].set)
943                         goto error;
944                 pw->p[i].FIELD = FN(EL,drop_dims)(pw->p[i].FIELD, type, first, n);
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 /* This function is very similar to drop_dims.
956  * The only difference is that the cells may still involve
957  * the specified dimensions.  They are removed using
958  * isl_set_project_out instead of isl_set_drop.
959  */
960 __isl_give PW *FN(PW,project_out)(__isl_take PW *pw,
961         enum isl_dim_type type, unsigned first, unsigned n)
962 {
963         int i;
964         enum isl_dim_type set_type;
965
966         if (!pw)
967                 return NULL;
968         if (n == 0 && !isl_space_get_tuple_name(pw->dim, type))
969                 return pw;
970
971         set_type = type == isl_dim_in ? isl_dim_set : type;
972
973         pw = FN(PW,cow)(pw);
974         if (!pw)
975                 return NULL;
976         pw->dim = isl_space_drop_dims(pw->dim, type, first, n);
977         if (!pw->dim)
978                 goto error;
979         for (i = 0; i < pw->n; ++i) {
980                 pw->p[i].set = isl_set_project_out(pw->p[i].set,
981                                                         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 /* Project the domain of pw onto its parameter space.
996  */
997 __isl_give PW *FN(PW,project_domain_on_params)(__isl_take PW *pw)
998 {
999         isl_space *space;
1000         unsigned n;
1001
1002         n = FN(PW,dim)(pw, isl_dim_in);
1003         pw = FN(PW,project_out)(pw, isl_dim_in, 0, n);
1004         space = FN(PW,get_domain_space)(pw);
1005         space = isl_space_params(space);
1006         pw = FN(PW,reset_domain_space)(pw, space);
1007         return pw;
1008 }
1009 #endif
1010
1011 #ifndef NO_INSERT_DIMS
1012 __isl_give PW *FN(PW,insert_dims)(__isl_take PW *pw, enum isl_dim_type type,
1013         unsigned first, unsigned n)
1014 {
1015         int i;
1016         enum isl_dim_type set_type;
1017
1018         if (!pw)
1019                 return NULL;
1020         if (n == 0 && !isl_space_is_named_or_nested(pw->dim, type))
1021                 return pw;
1022
1023         set_type = type == isl_dim_in ? isl_dim_set : type;
1024
1025         pw = FN(PW,cow)(pw);
1026         if (!pw)
1027                 return NULL;
1028
1029         pw->dim = isl_space_insert_dims(pw->dim, type, first, n);
1030         if (!pw->dim)
1031                 goto error;
1032
1033         for (i = 0; i < pw->n; ++i) {
1034                 pw->p[i].set = isl_set_insert_dims(pw->p[i].set,
1035                                                             set_type, first, n);
1036                 if (!pw->p[i].set)
1037                         goto error;
1038                 pw->p[i].FIELD = FN(EL,insert_dims)(pw->p[i].FIELD,
1039                                                                 type, first, n);
1040                 if (!pw->p[i].FIELD)
1041                         goto error;
1042         }
1043
1044         return pw;
1045 error:
1046         FN(PW,free)(pw);
1047         return NULL;
1048 }
1049 #endif
1050
1051 __isl_give PW *FN(PW,fix_dim)(__isl_take PW *pw,
1052         enum isl_dim_type type, unsigned pos, isl_int v)
1053 {
1054         int i;
1055
1056         if (!pw)
1057                 return NULL;
1058
1059         if (type == isl_dim_in)
1060                 type = isl_dim_set;
1061
1062         pw = FN(PW,cow)(pw);
1063         if (!pw)
1064                 return NULL;
1065         for (i = 0; i < pw->n; ++i) {
1066                 pw->p[i].set = isl_set_fix(pw->p[i].set, type, pos, v);
1067                 if (!pw->p[i].set)
1068                         goto error;
1069         }
1070
1071         return pw;
1072 error:
1073         FN(PW,free)(pw);
1074         return NULL;
1075 }
1076
1077 unsigned FN(PW,dim)(__isl_keep PW *pw, enum isl_dim_type type)
1078 {
1079         return pw ? isl_space_dim(pw->dim, type) : 0;
1080 }
1081
1082 __isl_give PW *FN(PW,split_dims)(__isl_take PW *pw,
1083         enum isl_dim_type type, unsigned first, unsigned n)
1084 {
1085         int i;
1086
1087         if (!pw)
1088                 return NULL;
1089         if (n == 0)
1090                 return pw;
1091
1092         if (type == isl_dim_in)
1093                 type = isl_dim_set;
1094
1095         pw = FN(PW,cow)(pw);
1096         if (!pw)
1097                 return NULL;
1098         if (!pw->dim)
1099                 goto error;
1100         for (i = 0; i < pw->n; ++i) {
1101                 pw->p[i].set = isl_set_split_dims(pw->p[i].set, type, first, n);
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 #ifndef NO_OPT
1113 /* Compute the maximal value attained by the piecewise quasipolynomial
1114  * on its domain or zero if the domain is empty.
1115  * In the worst case, the domain is scanned completely,
1116  * so the domain is assumed to be bounded.
1117  */
1118 __isl_give isl_qpolynomial *FN(PW,opt)(__isl_take PW *pw, int max)
1119 {
1120         int i;
1121         isl_qpolynomial *opt;
1122
1123         if (!pw)
1124                 return NULL;
1125
1126         if (pw->n == 0) {
1127                 isl_space *dim = isl_space_copy(pw->dim);
1128                 FN(PW,free)(pw);
1129                 return isl_qpolynomial_zero_on_domain(isl_space_domain(dim));
1130         }
1131
1132         opt = FN(EL,opt_on_domain)(FN(EL,copy)(pw->p[0].FIELD),
1133                                         isl_set_copy(pw->p[0].set), max);
1134         for (i = 1; i < pw->n; ++i) {
1135                 isl_qpolynomial *opt_i;
1136                 opt_i = FN(EL,opt_on_domain)(FN(EL,copy)(pw->p[i].FIELD),
1137                                                 isl_set_copy(pw->p[i].set), max);
1138                 if (max)
1139                         opt = isl_qpolynomial_max_cst(opt, opt_i);
1140                 else
1141                         opt = isl_qpolynomial_min_cst(opt, opt_i);
1142         }
1143
1144         FN(PW,free)(pw);
1145         return opt;
1146 }
1147
1148 __isl_give isl_qpolynomial *FN(PW,max)(__isl_take PW *pw)
1149 {
1150         return FN(PW,opt)(pw, 1);
1151 }
1152
1153 __isl_give isl_qpolynomial *FN(PW,min)(__isl_take PW *pw)
1154 {
1155         return FN(PW,opt)(pw, 0);
1156 }
1157 #endif
1158
1159 __isl_give isl_space *FN(PW,get_space)(__isl_keep PW *pw)
1160 {
1161         return pw ? isl_space_copy(pw->dim) : NULL;
1162 }
1163
1164 __isl_give isl_space *FN(PW,get_domain_space)(__isl_keep PW *pw)
1165 {
1166         return pw ? isl_space_domain(isl_space_copy(pw->dim)) : NULL;
1167 }
1168
1169 #ifndef NO_RESET_DIM
1170 /* Reset the space of "pw".  Since we don't know if the elements
1171  * represent the spaces themselves or their domains, we pass along
1172  * both when we call their reset_space_and_domain.
1173  */
1174 static __isl_give PW *FN(PW,reset_space_and_domain)(__isl_take PW *pw,
1175         __isl_take isl_space *space, __isl_take isl_space *domain)
1176 {
1177         int i;
1178
1179         pw = FN(PW,cow)(pw);
1180         if (!pw || !space || !domain)
1181                 goto error;
1182
1183         for (i = 0; i < pw->n; ++i) {
1184                 pw->p[i].set = isl_set_reset_space(pw->p[i].set,
1185                                                  isl_space_copy(domain));
1186                 if (!pw->p[i].set)
1187                         goto error;
1188                 pw->p[i].FIELD = FN(EL,reset_space_and_domain)(pw->p[i].FIELD,
1189                               isl_space_copy(space), isl_space_copy(domain));
1190                 if (!pw->p[i].FIELD)
1191                         goto error;
1192         }
1193
1194         isl_space_free(domain);
1195
1196         isl_space_free(pw->dim);
1197         pw->dim = space;
1198
1199         return pw;
1200 error:
1201         isl_space_free(domain);
1202         isl_space_free(space);
1203         FN(PW,free)(pw);
1204         return NULL;
1205 }
1206
1207 __isl_give PW *FN(PW,reset_domain_space)(__isl_take PW *pw,
1208         __isl_take isl_space *domain)
1209 {
1210         isl_space *space;
1211
1212         space = isl_space_extend_domain_with_range(isl_space_copy(domain),
1213                                                    FN(PW,get_space)(pw));
1214         return FN(PW,reset_space_and_domain)(pw, space, domain);
1215 }
1216
1217 __isl_give PW *FN(PW,reset_space)(__isl_take PW *pw, __isl_take isl_space *dim)
1218 {
1219         isl_space *domain;
1220
1221         domain = isl_space_domain(isl_space_copy(dim));
1222         return FN(PW,reset_space_and_domain)(pw, dim, domain);
1223 }
1224
1225 __isl_give PW *FN(PW,set_tuple_id)(__isl_keep PW *pw, enum isl_dim_type type,
1226         __isl_take isl_id *id)
1227 {
1228         isl_space *space;
1229
1230         pw = FN(PW,cow)(pw);
1231         if (!pw)
1232                 return isl_id_free(id);
1233
1234         space = FN(PW,get_space)(pw);
1235         space = isl_space_set_tuple_id(space, type, id);
1236
1237         return FN(PW,reset_space)(pw, space);
1238 }
1239
1240 __isl_give PW *FN(PW,set_dim_id)(__isl_take PW *pw,
1241         enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
1242 {
1243         pw = FN(PW,cow)(pw);
1244         if (!pw)
1245                 return isl_id_free(id);
1246         pw->dim = isl_space_set_dim_id(pw->dim, type, pos, id);
1247         return FN(PW,reset_space)(pw, isl_space_copy(pw->dim));
1248 }
1249 #endif
1250
1251 int FN(PW,has_equal_space)(__isl_keep PW *pw1, __isl_keep PW *pw2)
1252 {
1253         if (!pw1 || !pw2)
1254                 return -1;
1255
1256         return isl_space_is_equal(pw1->dim, pw2->dim);
1257 }
1258
1259 #ifndef NO_MORPH
1260 __isl_give PW *FN(PW,morph_domain)(__isl_take PW *pw,
1261         __isl_take isl_morph *morph)
1262 {
1263         int i;
1264         isl_ctx *ctx;
1265
1266         if (!pw || !morph)
1267                 goto error;
1268
1269         ctx = isl_space_get_ctx(pw->dim);
1270         isl_assert(ctx, isl_space_is_domain(morph->dom->dim, pw->dim),
1271                 goto error);
1272
1273         pw = FN(PW,cow)(pw);
1274         if (!pw)
1275                 goto error;
1276         pw->dim = isl_space_extend_domain_with_range(
1277                         isl_space_copy(morph->ran->dim), pw->dim);
1278         if (!pw->dim)
1279                 goto error;
1280
1281         for (i = 0; i < pw->n; ++i) {
1282                 pw->p[i].set = isl_morph_set(isl_morph_copy(morph), pw->p[i].set);
1283                 if (!pw->p[i].set)
1284                         goto error;
1285                 pw->p[i].FIELD = FN(EL,morph_domain)(pw->p[i].FIELD,
1286                                                 isl_morph_copy(morph));
1287                 if (!pw->p[i].FIELD)
1288                         goto error;
1289         }
1290
1291         isl_morph_free(morph);
1292
1293         return pw;
1294 error:
1295         FN(PW,free)(pw);
1296         isl_morph_free(morph);
1297         return NULL;
1298 }
1299 #endif
1300
1301 int FN(PW,n_piece)(__isl_keep PW *pw)
1302 {
1303         return pw ? pw->n : 0;
1304 }
1305
1306 int FN(PW,foreach_piece)(__isl_keep PW *pw,
1307         int (*fn)(__isl_take isl_set *set, __isl_take EL *el, void *user),
1308         void *user)
1309 {
1310         int i;
1311
1312         if (!pw)
1313                 return -1;
1314
1315         for (i = 0; i < pw->n; ++i)
1316                 if (fn(isl_set_copy(pw->p[i].set),
1317                                 FN(EL,copy)(pw->p[i].FIELD), user) < 0)
1318                         return -1;
1319
1320         return 0;
1321 }
1322
1323 #ifndef NO_LIFT
1324 static int any_divs(__isl_keep isl_set *set)
1325 {
1326         int i;
1327
1328         if (!set)
1329                 return -1;
1330
1331         for (i = 0; i < set->n; ++i)
1332                 if (set->p[i]->n_div > 0)
1333                         return 1;
1334
1335         return 0;
1336 }
1337
1338 static int foreach_lifted_subset(__isl_take isl_set *set, __isl_take EL *el,
1339         int (*fn)(__isl_take isl_set *set, __isl_take EL *el,
1340                     void *user), void *user)
1341 {
1342         int i;
1343
1344         if (!set || !el)
1345                 goto error;
1346
1347         for (i = 0; i < set->n; ++i) {
1348                 isl_set *lift;
1349                 EL *copy;
1350
1351                 lift = isl_set_from_basic_set(isl_basic_set_copy(set->p[i]));
1352                 lift = isl_set_lift(lift);
1353
1354                 copy = FN(EL,copy)(el);
1355                 copy = FN(EL,lift)(copy, isl_set_get_space(lift));
1356
1357                 if (fn(lift, copy, user) < 0)
1358                         goto error;
1359         }
1360
1361         isl_set_free(set);
1362         FN(EL,free)(el);
1363
1364         return 0;
1365 error:
1366         isl_set_free(set);
1367         FN(EL,free)(el);
1368         return -1;
1369 }
1370
1371 int FN(PW,foreach_lifted_piece)(__isl_keep PW *pw,
1372         int (*fn)(__isl_take isl_set *set, __isl_take EL *el,
1373                     void *user), void *user)
1374 {
1375         int i;
1376
1377         if (!pw)
1378                 return -1;
1379
1380         for (i = 0; i < pw->n; ++i) {
1381                 isl_set *set;
1382                 EL *el;
1383
1384                 set = isl_set_copy(pw->p[i].set);
1385                 el = FN(EL,copy)(pw->p[i].FIELD);
1386                 if (!any_divs(set)) {
1387                         if (fn(set, el, user) < 0)
1388                                 return -1;
1389                         continue;
1390                 }
1391                 if (foreach_lifted_subset(set, el, fn, user) < 0)
1392                         return -1;
1393         }
1394
1395         return 0;
1396 }
1397 #endif
1398
1399 #ifndef NO_MOVE_DIMS
1400 __isl_give PW *FN(PW,move_dims)(__isl_take PW *pw,
1401         enum isl_dim_type dst_type, unsigned dst_pos,
1402         enum isl_dim_type src_type, unsigned src_pos, unsigned n)
1403 {
1404         int i;
1405
1406         pw = FN(PW,cow)(pw);
1407         if (!pw)
1408                 return NULL;
1409
1410         pw->dim = isl_space_move_dims(pw->dim, dst_type, dst_pos, src_type, src_pos, n);
1411         if (!pw->dim)
1412                 goto error;
1413
1414         for (i = 0; i < pw->n; ++i) {
1415                 pw->p[i].FIELD = FN(EL,move_dims)(pw->p[i].FIELD,
1416                                         dst_type, dst_pos, src_type, src_pos, n);
1417                 if (!pw->p[i].FIELD)
1418                         goto error;
1419         }
1420
1421         if (dst_type == isl_dim_in)
1422                 dst_type = isl_dim_set;
1423         if (src_type == isl_dim_in)
1424                 src_type = isl_dim_set;
1425
1426         for (i = 0; i < pw->n; ++i) {
1427                 pw->p[i].set = isl_set_move_dims(pw->p[i].set,
1428                                                 dst_type, dst_pos,
1429                                                 src_type, src_pos, n);
1430                 if (!pw->p[i].set)
1431                         goto error;
1432         }
1433
1434         return pw;
1435 error:
1436         FN(PW,free)(pw);
1437         return NULL;
1438 }
1439 #endif
1440
1441 __isl_give PW *FN(PW,mul_isl_int)(__isl_take PW *pw, isl_int v)
1442 {
1443         int i;
1444
1445         if (isl_int_is_one(v))
1446                 return pw;
1447         if (pw && DEFAULT_IS_ZERO && isl_int_is_zero(v)) {
1448                 PW *zero;
1449                 isl_space *dim = FN(PW,get_space)(pw);
1450 #ifdef HAS_TYPE
1451                 zero = FN(PW,ZERO)(dim, pw->type);
1452 #else
1453                 zero = FN(PW,ZERO)(dim);
1454 #endif
1455                 FN(PW,free)(pw);
1456                 return zero;
1457         }
1458         pw = FN(PW,cow)(pw);
1459         if (!pw)
1460                 return NULL;
1461         if (pw->n == 0)
1462                 return pw;
1463
1464 #ifdef HAS_TYPE
1465         if (isl_int_is_neg(v))
1466                 pw->type = isl_fold_type_negate(pw->type);
1467 #endif
1468         for (i = 0; i < pw->n; ++i) {
1469                 pw->p[i].FIELD = FN(EL,scale)(pw->p[i].FIELD, v);
1470                 if (!pw->p[i].FIELD)
1471                         goto error;
1472         }
1473
1474         return pw;
1475 error:
1476         FN(PW,free)(pw);
1477         return NULL;
1478 }
1479
1480 __isl_give PW *FN(PW,scale)(__isl_take PW *pw, isl_int v)
1481 {
1482         return FN(PW,mul_isl_int)(pw, v);
1483 }
1484
1485 static int FN(PW,qsort_set_cmp)(const void *p1, const void *p2)
1486 {
1487         const isl_set *set1 = *(const isl_set **)p1;
1488         const isl_set *set2 = *(const isl_set **)p2;
1489
1490         return isl_set_plain_cmp(set1, set2);
1491 }
1492
1493 /* We normalize in place, but if anything goes wrong we need
1494  * to return NULL, so we need to make sure we don't change the
1495  * meaning of any possible other copies of map.
1496  */
1497 __isl_give PW *FN(PW,normalize)(__isl_take PW *pw)
1498 {
1499         int i, j;
1500         isl_set *set;
1501
1502         if (!pw)
1503                 return NULL;
1504         for (i = 0; i < pw->n; ++i) {
1505                 set = isl_set_normalize(isl_set_copy(pw->p[i].set));
1506                 if (!set)
1507                         return FN(PW,free)(pw);
1508                 isl_set_free(pw->p[i].set);
1509                 pw->p[i].set = set;
1510         }
1511         qsort(pw->p, pw->n, sizeof(pw->p[0]), &FN(PW,qsort_set_cmp));
1512         for (i = pw->n - 1; i >= 1; --i) {
1513                 if (!isl_set_plain_is_equal(pw->p[i - 1].set, pw->p[i].set))
1514                         continue;
1515                 if (!FN(EL,plain_is_equal)(pw->p[i - 1].FIELD, pw->p[i].FIELD))
1516                         continue;
1517                 set = isl_set_union(isl_set_copy(pw->p[i - 1].set),
1518                                     isl_set_copy(pw->p[i].set));
1519                 if (!set)
1520                         return FN(PW,free)(pw);
1521                 isl_set_free(pw->p[i].set);
1522                 FN(EL,free)(pw->p[i].FIELD);
1523                 isl_set_free(pw->p[i - 1].set);
1524                 pw->p[i - 1].set = set;
1525                 for (j = i + 1; j < pw->n; ++j)
1526                         pw->p[j - 1] = pw->p[j];
1527                 pw->n--;
1528         }
1529
1530         return pw;
1531 }
1532
1533 /* Is pw1 obviously equal to pw2?
1534  * That is, do they have obviously identical cells and obviously identical
1535  * elements on each cell?
1536  */
1537 int FN(PW,plain_is_equal)(__isl_keep PW *pw1, __isl_keep PW *pw2)
1538 {
1539         int i;
1540         int equal;
1541
1542         if (!pw1 || !pw2)
1543                 return -1;
1544
1545         if (pw1 == pw2)
1546                 return 1;
1547         if (!isl_space_is_equal(pw1->dim, pw2->dim))
1548                 return 0;
1549
1550         pw1 = FN(PW,copy)(pw1);
1551         pw2 = FN(PW,copy)(pw2);
1552         pw1 = FN(PW,normalize)(pw1);
1553         pw2 = FN(PW,normalize)(pw2);
1554         if (!pw1 || !pw2)
1555                 goto error;
1556
1557         equal = pw1->n == pw2->n;
1558         for (i = 0; equal && i < pw1->n; ++i) {
1559                 equal = isl_set_plain_is_equal(pw1->p[i].set, pw2->p[i].set);
1560                 if (equal < 0)
1561                         goto error;
1562                 if (!equal)
1563                         break;
1564                 equal = FN(EL,plain_is_equal)(pw1->p[i].FIELD, pw2->p[i].FIELD);
1565                 if (equal < 0)
1566                         goto error;
1567         }
1568
1569         FN(PW,free)(pw1);
1570         FN(PW,free)(pw2);
1571         return equal;
1572 error:
1573         FN(PW,free)(pw1);
1574         FN(PW,free)(pw2);
1575         return -1;
1576 }