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