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