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