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