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