add isl_qpolynomial_fold_gist_params
[isl.git] / isl_pw_templ.c
blobf254b0c32fd8f47a192eca6743f00f26734a71b2
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)
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
13 isl_ctx *ctx;
14 struct PW *pw;
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;
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;
38 #ifdef HAS_TYPE
39 __isl_give PW *FN(PW,ZERO)(__isl_take isl_space *dim, enum isl_fold type)
41 return FN(PW,alloc_size)(dim, type, 0);
43 #else
44 __isl_give PW *FN(PW,ZERO)(__isl_take isl_space *dim)
46 return FN(PW,alloc_size)(dim, 0);
48 #endif
50 __isl_give PW *FN(PW,add_piece)(__isl_take PW *pw,
51 __isl_take isl_set *set, __isl_take EL *el)
53 isl_ctx *ctx;
54 isl_space *el_dim = NULL;
56 if (!pw || !set || !el)
57 goto error;
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;
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);
75 pw->p[pw->n].set = set;
76 pw->p[pw->n].FIELD = el;
77 pw->n++;
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;
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
96 PW *pw;
98 if (!set || !el)
99 goto error;
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
107 return FN(PW,add_piece)(pw, set, el);
108 error:
109 isl_set_free(set);
110 FN(EL,free)(el);
111 return NULL;
114 __isl_give PW *FN(PW,dup)(__isl_keep PW *pw)
116 int i;
117 PW *dup;
119 if (!pw)
120 return NULL;
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;
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));
134 return dup;
137 __isl_give PW *FN(PW,cow)(__isl_take PW *pw)
139 if (!pw)
140 return NULL;
142 if (pw->ref == 1)
143 return pw;
144 pw->ref--;
145 return FN(PW,dup)(pw);
148 __isl_give PW *FN(PW,copy)(__isl_keep PW *pw)
150 if (!pw)
151 return NULL;
153 pw->ref++;
154 return pw;
157 void *FN(PW,free)(__isl_take PW *pw)
159 int i;
161 if (!pw)
162 return NULL;
163 if (--pw->ref > 0)
164 return NULL;
166 for (i = 0; i < pw->n; ++i) {
167 isl_set_free(pw->p[i].set);
168 FN(EL,free)(pw->p[i].FIELD);
170 isl_space_free(pw->dim);
171 free(pw);
173 return NULL;
176 const char *FN(PW,get_dim_name)(__isl_keep PW *pw, enum isl_dim_type type,
177 unsigned pos)
179 return pw ? isl_space_get_dim_name(pw->dim, type, pos) : NULL;
182 __isl_give isl_id *FN(PW,get_dim_id)(__isl_keep PW *pw, enum isl_dim_type type,
183 unsigned pos)
185 return pw ? isl_space_get_dim_id(pw->dim, type, pos) : NULL;
188 const char *FN(PW,get_tuple_name)(__isl_keep PW *pw, enum isl_dim_type type)
190 return pw ? isl_space_get_tuple_name(pw->dim, type) : NULL;
193 int FN(PW,has_tuple_id)(__isl_keep PW *pw, enum isl_dim_type type)
195 return pw ? isl_space_has_tuple_id(pw->dim, type) : -1;
198 __isl_give isl_id *FN(PW,get_tuple_id)(__isl_keep PW *pw, enum isl_dim_type type)
200 return pw ? isl_space_get_tuple_id(pw->dim, type) : NULL;
203 int FN(PW,IS_ZERO)(__isl_keep PW *pw)
205 if (!pw)
206 return -1;
208 return pw->n == 0;
211 #ifndef NO_REALIGN
212 __isl_give PW *FN(PW,realign_domain)(__isl_take PW *pw,
213 __isl_take isl_reordering *exp)
215 int i;
217 pw = FN(PW,cow)(pw);
218 if (!pw || !exp)
219 return NULL;
221 for (i = 0; i < pw->n; ++i) {
222 pw->p[i].set = isl_set_realign(pw->p[i].set,
223 isl_reordering_copy(exp));
224 if (!pw->p[i].set)
225 goto error;
226 pw->p[i].FIELD = FN(EL,realign_domain)(pw->p[i].FIELD,
227 isl_reordering_copy(exp));
228 if (!pw->p[i].FIELD)
229 goto error;
232 pw = FN(PW,reset_domain_space)(pw, isl_space_copy(exp->dim));
234 isl_reordering_free(exp);
235 return pw;
236 error:
237 isl_reordering_free(exp);
238 FN(PW,free)(pw);
239 return NULL;
242 /* Align the parameters of "pw" to those of "model".
244 __isl_give PW *FN(PW,align_params)(__isl_take PW *pw, __isl_take isl_space *model)
246 isl_ctx *ctx;
248 if (!pw || !model)
249 goto error;
251 ctx = isl_space_get_ctx(model);
252 if (!isl_space_has_named_params(model))
253 isl_die(ctx, isl_error_invalid,
254 "model has unnamed parameters", goto error);
255 if (!isl_space_has_named_params(pw->dim))
256 isl_die(ctx, isl_error_invalid,
257 "input has unnamed parameters", goto error);
258 if (!isl_space_match(pw->dim, isl_dim_param, model, isl_dim_param)) {
259 isl_reordering *exp;
261 model = isl_space_drop_dims(model, isl_dim_in,
262 0, isl_space_dim(model, isl_dim_in));
263 model = isl_space_drop_dims(model, isl_dim_out,
264 0, isl_space_dim(model, isl_dim_out));
265 exp = isl_parameter_alignment_reordering(pw->dim, model);
266 exp = isl_reordering_extend_space(exp,
267 FN(PW,get_domain_space)(pw));
268 pw = FN(PW,realign_domain)(pw, exp);
271 isl_space_free(model);
272 return pw;
273 error:
274 isl_space_free(model);
275 FN(PW,free)(pw);
276 return NULL;
279 static __isl_give PW *FN(PW,align_params_pw_pw_and)(__isl_take PW *pw1,
280 __isl_take PW *pw2,
281 __isl_give PW *(*fn)(__isl_take PW *pw1, __isl_take PW *pw2))
283 isl_ctx *ctx;
285 if (!pw1 || !pw2)
286 goto error;
287 if (isl_space_match(pw1->dim, isl_dim_param, pw2->dim, isl_dim_param))
288 return fn(pw1, pw2);
289 ctx = FN(PW,get_ctx)(pw1);
290 if (!isl_space_has_named_params(pw1->dim) ||
291 !isl_space_has_named_params(pw2->dim))
292 isl_die(ctx, isl_error_invalid,
293 "unaligned unnamed parameters", goto error);
294 pw1 = FN(PW,align_params)(pw1, FN(PW,get_space)(pw2));
295 pw2 = FN(PW,align_params)(pw2, FN(PW,get_space)(pw1));
296 return fn(pw1, pw2);
297 error:
298 FN(PW,free)(pw1);
299 FN(PW,free)(pw2);
300 return NULL;
303 static __isl_give PW *FN(PW,align_params_pw_set_and)(__isl_take PW *pw,
304 __isl_take isl_set *set,
305 __isl_give PW *(*fn)(__isl_take PW *pw, __isl_take isl_set *set))
307 isl_ctx *ctx;
309 if (!pw || !set)
310 goto error;
311 if (isl_space_match(pw->dim, isl_dim_param, set->dim, isl_dim_param))
312 return fn(pw, set);
313 ctx = FN(PW,get_ctx)(pw);
314 if (!isl_space_has_named_params(pw->dim) ||
315 !isl_space_has_named_params(set->dim))
316 isl_die(ctx, isl_error_invalid,
317 "unaligned unnamed parameters", goto error);
318 pw = FN(PW,align_params)(pw, isl_set_get_space(set));
319 set = isl_set_align_params(set, FN(PW,get_space)(pw));
320 return fn(pw, set);
321 error:
322 FN(PW,free)(pw);
323 isl_set_free(set);
324 return NULL;
326 #endif
328 static __isl_give PW *FN(PW,union_add_aligned)(__isl_take PW *pw1,
329 __isl_take PW *pw2)
331 int i, j, n;
332 struct PW *res;
333 isl_ctx *ctx;
334 isl_set *set;
336 if (!pw1 || !pw2)
337 goto error;
339 ctx = isl_space_get_ctx(pw1->dim);
340 #ifdef HAS_TYPE
341 if (pw1->type != pw2->type)
342 isl_die(ctx, isl_error_invalid,
343 "fold types don't match", goto error);
344 #endif
345 isl_assert(ctx, isl_space_is_equal(pw1->dim, pw2->dim), goto error);
347 if (FN(PW,IS_ZERO)(pw1)) {
348 FN(PW,free)(pw1);
349 return pw2;
352 if (FN(PW,IS_ZERO)(pw2)) {
353 FN(PW,free)(pw2);
354 return pw1;
357 n = (pw1->n + 1) * (pw2->n + 1);
358 #ifdef HAS_TYPE
359 res = FN(PW,alloc_size)(isl_space_copy(pw1->dim), pw1->type, n);
360 #else
361 res = FN(PW,alloc_size)(isl_space_copy(pw1->dim), n);
362 #endif
364 for (i = 0; i < pw1->n; ++i) {
365 set = isl_set_copy(pw1->p[i].set);
366 for (j = 0; j < pw2->n; ++j) {
367 struct isl_set *common;
368 EL *sum;
369 set = isl_set_subtract(set,
370 isl_set_copy(pw2->p[j].set));
371 common = isl_set_intersect(isl_set_copy(pw1->p[i].set),
372 isl_set_copy(pw2->p[j].set));
373 if (isl_set_plain_is_empty(common)) {
374 isl_set_free(common);
375 continue;
378 sum = FN(EL,add_on_domain)(common,
379 FN(EL,copy)(pw1->p[i].FIELD),
380 FN(EL,copy)(pw2->p[j].FIELD));
382 res = FN(PW,add_piece)(res, common, sum);
384 res = FN(PW,add_piece)(res, set, FN(EL,copy)(pw1->p[i].FIELD));
387 for (j = 0; j < pw2->n; ++j) {
388 set = isl_set_copy(pw2->p[j].set);
389 for (i = 0; i < pw1->n; ++i)
390 set = isl_set_subtract(set,
391 isl_set_copy(pw1->p[i].set));
392 res = FN(PW,add_piece)(res, set, FN(EL,copy)(pw2->p[j].FIELD));
395 FN(PW,free)(pw1);
396 FN(PW,free)(pw2);
398 return res;
399 error:
400 FN(PW,free)(pw1);
401 FN(PW,free)(pw2);
402 return NULL;
405 /* Private version of "union_add". For isl_pw_qpolynomial and
406 * isl_pw_qpolynomial_fold, we prefer to simply call it "add".
408 static __isl_give PW *FN(PW,union_add_)(__isl_take PW *pw1, __isl_take PW *pw2)
410 return FN(PW,align_params_pw_pw_and)(pw1, pw2,
411 &FN(PW,union_add_aligned));
414 /* Make sure "pw" has room for at least "n" more pieces.
416 * If there is only one reference to pw, we extend it in place.
417 * Otherwise, we create a new PW and copy the pieces.
419 static __isl_give PW *FN(PW,grow)(__isl_take PW *pw, int n)
421 int i;
422 isl_ctx *ctx;
423 PW *res;
425 if (!pw)
426 return NULL;
427 if (pw->n + n <= pw->size)
428 return pw;
429 ctx = FN(PW,get_ctx)(pw);
430 n += pw->n;
431 if (pw->ref == 1) {
432 res = isl_realloc(ctx, pw, struct PW,
433 sizeof(struct PW) + (n - 1) * sizeof(S(PW,piece)));
434 if (!res)
435 return FN(PW,free)(pw);
436 res->size = n;
437 return res;
439 #ifdef HAS_TYPE
440 res = FN(PW,alloc_size)(isl_space_copy(pw->dim), pw->type, n);
441 #else
442 res = FN(PW,alloc_size)(isl_space_copy(pw->dim), n);
443 #endif
444 if (!res)
445 return FN(PW,free)(pw);
446 for (i = 0; i < pw->n; ++i)
447 res = FN(PW,add_piece)(res, isl_set_copy(pw->p[i].set),
448 FN(EL,copy)(pw->p[i].FIELD));
449 FN(PW,free)(pw);
450 return res;
453 static __isl_give PW *FN(PW,add_disjoint_aligned)(__isl_take PW *pw1,
454 __isl_take PW *pw2)
456 int i;
457 isl_ctx *ctx;
459 if (!pw1 || !pw2)
460 goto error;
462 if (pw1->size < pw1->n + pw2->n && pw1->n < pw2->n)
463 return FN(PW,add_disjoint_aligned)(pw2, pw1);
465 ctx = isl_space_get_ctx(pw1->dim);
466 #ifdef HAS_TYPE
467 if (pw1->type != pw2->type)
468 isl_die(ctx, isl_error_invalid,
469 "fold types don't match", goto error);
470 #endif
471 isl_assert(ctx, isl_space_is_equal(pw1->dim, pw2->dim), goto error);
473 if (FN(PW,IS_ZERO)(pw1)) {
474 FN(PW,free)(pw1);
475 return pw2;
478 if (FN(PW,IS_ZERO)(pw2)) {
479 FN(PW,free)(pw2);
480 return pw1;
483 pw1 = FN(PW,grow)(pw1, pw2->n);
484 if (!pw1)
485 goto error;
487 for (i = 0; i < pw2->n; ++i)
488 pw1 = FN(PW,add_piece)(pw1,
489 isl_set_copy(pw2->p[i].set),
490 FN(EL,copy)(pw2->p[i].FIELD));
492 FN(PW,free)(pw2);
494 return pw1;
495 error:
496 FN(PW,free)(pw1);
497 FN(PW,free)(pw2);
498 return NULL;
501 __isl_give PW *FN(PW,add_disjoint)(__isl_take PW *pw1, __isl_take PW *pw2)
503 return FN(PW,align_params_pw_pw_and)(pw1, pw2,
504 &FN(PW,add_disjoint_aligned));
507 /* This function is currently only used from isl_aff.c
509 static __isl_give PW *FN(PW,on_shared_domain)(__isl_take PW *pw1,
510 __isl_take PW *pw2,
511 __isl_give EL *(*fn)(__isl_take EL *el1, __isl_take EL *el2))
512 __attribute__ ((unused));
514 /* Apply "fn" to pairs of elements from pw1 and pw2 on shared domains.
516 static __isl_give PW *FN(PW,on_shared_domain)(__isl_take PW *pw1,
517 __isl_take PW *pw2,
518 __isl_give EL *(*fn)(__isl_take EL *el1, __isl_take EL *el2))
520 int i, j, n;
521 PW *res;
523 if (!pw1 || !pw2)
524 goto error;
526 n = pw1->n * pw2->n;
527 #ifdef HAS_TYPE
528 res = FN(PW,alloc_size)(isl_space_copy(pw1->dim), pw1->type, n);
529 #else
530 res = FN(PW,alloc_size)(isl_space_copy(pw1->dim), n);
531 #endif
533 for (i = 0; i < pw1->n; ++i) {
534 for (j = 0; j < pw2->n; ++j) {
535 isl_set *common;
536 EL *res_ij;
537 common = isl_set_intersect(
538 isl_set_copy(pw1->p[i].set),
539 isl_set_copy(pw2->p[j].set));
540 if (isl_set_plain_is_empty(common)) {
541 isl_set_free(common);
542 continue;
545 res_ij = fn(FN(EL,copy)(pw1->p[i].FIELD),
546 FN(EL,copy)(pw2->p[j].FIELD));
548 res = FN(PW,add_piece)(res, common, res_ij);
552 FN(PW,free)(pw1);
553 FN(PW,free)(pw2);
554 return res;
555 error:
556 FN(PW,free)(pw1);
557 FN(PW,free)(pw2);
558 return NULL;
561 #ifndef NO_NEG
562 __isl_give PW *FN(PW,neg)(__isl_take PW *pw)
564 int i;
566 if (!pw)
567 return NULL;
569 if (FN(PW,IS_ZERO)(pw))
570 return pw;
572 pw = FN(PW,cow)(pw);
573 if (!pw)
574 return NULL;
576 for (i = 0; i < pw->n; ++i) {
577 pw->p[i].FIELD = FN(EL,neg)(pw->p[i].FIELD);
578 if (!pw->p[i].FIELD)
579 return FN(PW,free)(pw);
582 return pw;
585 __isl_give PW *FN(PW,sub)(__isl_take PW *pw1, __isl_take PW *pw2)
587 return FN(PW,add)(pw1, FN(PW,neg)(pw2));
589 #endif
591 #ifndef NO_EVAL
592 __isl_give isl_qpolynomial *FN(PW,eval)(__isl_take PW *pw,
593 __isl_take isl_point *pnt)
595 int i;
596 int found = 0;
597 isl_ctx *ctx;
598 isl_space *pnt_dim = NULL;
599 isl_qpolynomial *qp;
601 if (!pw || !pnt)
602 goto error;
603 ctx = isl_point_get_ctx(pnt);
604 pnt_dim = isl_point_get_space(pnt);
605 isl_assert(ctx, isl_space_is_domain(pnt_dim, pw->dim), goto error);
607 for (i = 0; i < pw->n; ++i) {
608 found = isl_set_contains_point(pw->p[i].set, pnt);
609 if (found < 0)
610 goto error;
611 if (found)
612 break;
614 if (found)
615 qp = FN(EL,eval)(FN(EL,copy)(pw->p[i].FIELD),
616 isl_point_copy(pnt));
617 else
618 qp = isl_qpolynomial_zero_on_domain(FN(PW,get_domain_space)(pw));
619 FN(PW,free)(pw);
620 isl_space_free(pnt_dim);
621 isl_point_free(pnt);
622 return qp;
623 error:
624 FN(PW,free)(pw);
625 isl_space_free(pnt_dim);
626 isl_point_free(pnt);
627 return NULL;
629 #endif
631 __isl_give isl_set *FN(PW,domain)(__isl_take PW *pw)
633 int i;
634 isl_set *dom;
636 if (!pw)
637 return NULL;
639 dom = isl_set_empty(FN(PW,get_domain_space)(pw));
640 for (i = 0; i < pw->n; ++i)
641 dom = isl_set_union_disjoint(dom, isl_set_copy(pw->p[i].set));
643 FN(PW,free)(pw);
645 return dom;
648 /* Restrict the domain of "pw" by combining each cell
649 * with "set" through a call to "fn", where "fn" may be
650 * isl_set_intersect or isl_set_intersect_params.
652 static __isl_give PW *FN(PW,intersect_aligned)(__isl_take PW *pw,
653 __isl_take isl_set *set,
654 __isl_give isl_set *(*fn)(__isl_take isl_set *set1,
655 __isl_take isl_set *set2))
657 int i;
659 if (!pw || !set)
660 goto error;
662 if (pw->n == 0) {
663 isl_set_free(set);
664 return pw;
667 pw = FN(PW,cow)(pw);
668 if (!pw)
669 goto error;
671 for (i = pw->n - 1; i >= 0; --i) {
672 isl_basic_set *aff;
673 pw->p[i].set = fn(pw->p[i].set, isl_set_copy(set));
674 if (!pw->p[i].set)
675 goto error;
676 aff = isl_set_affine_hull(isl_set_copy(pw->p[i].set));
677 pw->p[i].FIELD = FN(EL,substitute_equalities)(pw->p[i].FIELD,
678 aff);
679 if (!pw->p[i].FIELD)
680 goto error;
681 if (isl_set_plain_is_empty(pw->p[i].set)) {
682 isl_set_free(pw->p[i].set);
683 FN(EL,free)(pw->p[i].FIELD);
684 if (i != pw->n - 1)
685 pw->p[i] = pw->p[pw->n - 1];
686 pw->n--;
690 isl_set_free(set);
691 return pw;
692 error:
693 isl_set_free(set);
694 FN(PW,free)(pw);
695 return NULL;
698 static __isl_give PW *FN(PW,intersect_domain_aligned)(__isl_take PW *pw,
699 __isl_take isl_set *set)
701 return FN(PW,intersect_aligned)(pw, set, &isl_set_intersect);
704 __isl_give PW *FN(PW,intersect_domain)(__isl_take PW *pw,
705 __isl_take isl_set *context)
707 return FN(PW,align_params_pw_set_and)(pw, context,
708 &FN(PW,intersect_domain_aligned));
711 static __isl_give PW *FN(PW,intersect_params_aligned)(__isl_take PW *pw,
712 __isl_take isl_set *set)
714 return FN(PW,intersect_aligned)(pw, set, &isl_set_intersect_params);
717 /* Intersect the domain of "pw" with the parameter domain "context".
719 __isl_give PW *FN(PW,intersect_params)(__isl_take PW *pw,
720 __isl_take isl_set *context)
722 return FN(PW,align_params_pw_set_and)(pw, context,
723 &FN(PW,intersect_params_aligned));
726 static __isl_give PW *FN(PW,gist_aligned)(__isl_take PW *pw,
727 __isl_take isl_set *context)
729 int i;
730 isl_basic_set *hull = NULL;
732 if (!pw || !context)
733 goto error;
735 if (pw->n == 0) {
736 isl_set_free(context);
737 return pw;
740 if (!isl_space_match(pw->dim, isl_dim_param,
741 context->dim, isl_dim_param)) {
742 pw = FN(PW,align_params)(pw, isl_set_get_space(context));
743 context = isl_set_align_params(context, FN(PW,get_space)(pw));
746 context = isl_set_compute_divs(context);
747 hull = isl_set_simple_hull(isl_set_copy(context));
749 pw = FN(PW,cow)(pw);
750 if (!pw)
751 goto error;
753 for (i = pw->n - 1; i >= 0; --i) {
754 pw->p[i].set = isl_set_intersect(pw->p[i].set,
755 isl_set_copy(context));
756 if (!pw->p[i].set)
757 goto error;
758 pw->p[i].FIELD = FN(EL,gist)(pw->p[i].FIELD,
759 isl_set_copy(pw->p[i].set));
760 pw->p[i].set = isl_set_gist_basic_set(pw->p[i].set,
761 isl_basic_set_copy(hull));
762 if (!pw->p[i].set)
763 goto error;
764 if (isl_set_plain_is_empty(pw->p[i].set)) {
765 isl_set_free(pw->p[i].set);
766 FN(EL,free)(pw->p[i].FIELD);
767 if (i != pw->n - 1)
768 pw->p[i] = pw->p[pw->n - 1];
769 pw->n--;
773 isl_basic_set_free(hull);
774 isl_set_free(context);
776 return pw;
777 error:
778 FN(PW,free)(pw);
779 isl_basic_set_free(hull);
780 isl_set_free(context);
781 return NULL;
784 __isl_give PW *FN(PW,gist)(__isl_take PW *pw, __isl_take isl_set *context)
786 return FN(PW,align_params_pw_set_and)(pw, context, &FN(PW,gist_aligned));
789 __isl_give PW *FN(PW,coalesce)(__isl_take PW *pw)
791 int i, j;
793 if (!pw)
794 return NULL;
795 if (pw->n == 0)
796 return pw;
798 for (i = pw->n - 1; i >= 0; --i) {
799 for (j = i - 1; j >= 0; --j) {
800 if (!FN(EL,plain_is_equal)(pw->p[i].FIELD,
801 pw->p[j].FIELD))
802 continue;
803 pw->p[j].set = isl_set_union(pw->p[j].set,
804 pw->p[i].set);
805 FN(EL,free)(pw->p[i].FIELD);
806 if (i != pw->n - 1)
807 pw->p[i] = pw->p[pw->n - 1];
808 pw->n--;
809 break;
811 if (j >= 0)
812 continue;
813 pw->p[i].set = isl_set_coalesce(pw->p[i].set);
814 if (!pw->p[i].set)
815 goto error;
818 return pw;
819 error:
820 FN(PW,free)(pw);
821 return NULL;
824 isl_ctx *FN(PW,get_ctx)(__isl_keep PW *pw)
826 return pw ? isl_space_get_ctx(pw->dim) : NULL;
829 #ifndef NO_INVOLVES_DIMS
830 int FN(PW,involves_dims)(__isl_keep PW *pw, enum isl_dim_type type,
831 unsigned first, unsigned n)
833 int i;
834 enum isl_dim_type set_type;
836 if (!pw)
837 return -1;
838 if (pw->n == 0 || n == 0)
839 return 0;
841 set_type = type == isl_dim_in ? isl_dim_set : type;
843 for (i = 0; i < pw->n; ++i) {
844 int involves = FN(EL,involves_dims)(pw->p[i].FIELD,
845 type, first, n);
846 if (involves < 0 || involves)
847 return involves;
848 involves = isl_set_involves_dims(pw->p[i].set,
849 set_type, first, n);
850 if (involves < 0 || involves)
851 return involves;
853 return 0;
855 #endif
857 __isl_give PW *FN(PW,set_dim_name)(__isl_take PW *pw,
858 enum isl_dim_type type, unsigned pos, const char *s)
860 int i;
861 enum isl_dim_type set_type;
863 pw = FN(PW,cow)(pw);
864 if (!pw)
865 return NULL;
867 set_type = type == isl_dim_in ? isl_dim_set : type;
869 pw->dim = isl_space_set_dim_name(pw->dim, type, pos, s);
870 if (!pw->dim)
871 goto error;
873 for (i = 0; i < pw->n; ++i) {
874 pw->p[i].set = isl_set_set_dim_name(pw->p[i].set,
875 set_type, pos, s);
876 if (!pw->p[i].set)
877 goto error;
878 pw->p[i].FIELD = FN(EL,set_dim_name)(pw->p[i].FIELD, type, pos, s);
879 if (!pw->p[i].FIELD)
880 goto error;
883 return pw;
884 error:
885 FN(PW,free)(pw);
886 return NULL;
889 #ifndef NO_DROP_DIMS
890 __isl_give PW *FN(PW,drop_dims)(__isl_take PW *pw,
891 enum isl_dim_type type, unsigned first, unsigned n)
893 int i;
894 enum isl_dim_type set_type;
896 if (!pw)
897 return NULL;
898 if (n == 0 && !isl_space_get_tuple_name(pw->dim, type))
899 return pw;
901 set_type = type == isl_dim_in ? isl_dim_set : type;
903 pw = FN(PW,cow)(pw);
904 if (!pw)
905 return NULL;
906 pw->dim = isl_space_drop_dims(pw->dim, type, first, n);
907 if (!pw->dim)
908 goto error;
909 for (i = 0; i < pw->n; ++i) {
910 pw->p[i].set = isl_set_drop(pw->p[i].set, set_type, first, n);
911 if (!pw->p[i].set)
912 goto error;
913 pw->p[i].FIELD = FN(EL,drop_dims)(pw->p[i].FIELD, type, first, n);
914 if (!pw->p[i].FIELD)
915 goto error;
918 return pw;
919 error:
920 FN(PW,free)(pw);
921 return NULL;
924 /* This function is very similar to drop_dims.
925 * The only difference is that the cells may still involve
926 * the specified dimensions. They are removed using
927 * isl_set_project_out instead of isl_set_drop.
929 __isl_give PW *FN(PW,project_out)(__isl_take PW *pw,
930 enum isl_dim_type type, unsigned first, unsigned n)
932 int i;
933 enum isl_dim_type set_type;
935 if (!pw)
936 return NULL;
937 if (n == 0 && !isl_space_get_tuple_name(pw->dim, type))
938 return pw;
940 set_type = type == isl_dim_in ? isl_dim_set : type;
942 pw = FN(PW,cow)(pw);
943 if (!pw)
944 return NULL;
945 pw->dim = isl_space_drop_dims(pw->dim, type, first, n);
946 if (!pw->dim)
947 goto error;
948 for (i = 0; i < pw->n; ++i) {
949 pw->p[i].set = isl_set_project_out(pw->p[i].set,
950 set_type, first, n);
951 if (!pw->p[i].set)
952 goto error;
953 pw->p[i].FIELD = FN(EL,drop_dims)(pw->p[i].FIELD, type, first, n);
954 if (!pw->p[i].FIELD)
955 goto error;
958 return pw;
959 error:
960 FN(PW,free)(pw);
961 return NULL;
964 /* Project the domain of pw onto its parameter space.
966 __isl_give PW *FN(PW,project_domain_on_params)(__isl_take PW *pw)
968 isl_space *space;
969 unsigned n;
971 n = FN(PW,dim)(pw, isl_dim_in);
972 pw = FN(PW,project_out)(pw, isl_dim_in, 0, n);
973 space = FN(PW,get_domain_space)(pw);
974 space = isl_space_params(space);
975 pw = FN(PW,reset_domain_space)(pw, space);
976 return pw;
978 #endif
980 #ifndef NO_INSERT_DIMS
981 __isl_give PW *FN(PW,insert_dims)(__isl_take PW *pw, enum isl_dim_type type,
982 unsigned first, unsigned n)
984 int i;
985 enum isl_dim_type set_type;
987 if (!pw)
988 return NULL;
989 if (n == 0 && !isl_space_is_named_or_nested(pw->dim, type))
990 return pw;
992 set_type = type == isl_dim_in ? isl_dim_set : type;
994 pw = FN(PW,cow)(pw);
995 if (!pw)
996 return NULL;
998 pw->dim = isl_space_insert_dims(pw->dim, type, first, n);
999 if (!pw->dim)
1000 goto error;
1002 for (i = 0; i < pw->n; ++i) {
1003 pw->p[i].set = isl_set_insert_dims(pw->p[i].set,
1004 set_type, first, n);
1005 if (!pw->p[i].set)
1006 goto error;
1007 pw->p[i].FIELD = FN(EL,insert_dims)(pw->p[i].FIELD,
1008 type, first, n);
1009 if (!pw->p[i].FIELD)
1010 goto error;
1013 return pw;
1014 error:
1015 FN(PW,free)(pw);
1016 return NULL;
1018 #endif
1020 __isl_give PW *FN(PW,fix_dim)(__isl_take PW *pw,
1021 enum isl_dim_type type, unsigned pos, isl_int v)
1023 int i;
1025 if (!pw)
1026 return NULL;
1028 if (type == isl_dim_in)
1029 type = isl_dim_set;
1031 pw = FN(PW,cow)(pw);
1032 if (!pw)
1033 return NULL;
1034 for (i = 0; i < pw->n; ++i) {
1035 pw->p[i].set = isl_set_fix(pw->p[i].set, type, pos, v);
1036 if (!pw->p[i].set)
1037 goto error;
1040 return pw;
1041 error:
1042 FN(PW,free)(pw);
1043 return NULL;
1046 unsigned FN(PW,dim)(__isl_keep PW *pw, enum isl_dim_type type)
1048 return pw ? isl_space_dim(pw->dim, type) : 0;
1051 __isl_give PW *FN(PW,split_dims)(__isl_take PW *pw,
1052 enum isl_dim_type type, unsigned first, unsigned n)
1054 int i;
1056 if (!pw)
1057 return NULL;
1058 if (n == 0)
1059 return pw;
1061 if (type == isl_dim_in)
1062 type = isl_dim_set;
1064 pw = FN(PW,cow)(pw);
1065 if (!pw)
1066 return NULL;
1067 if (!pw->dim)
1068 goto error;
1069 for (i = 0; i < pw->n; ++i) {
1070 pw->p[i].set = isl_set_split_dims(pw->p[i].set, type, first, n);
1071 if (!pw->p[i].set)
1072 goto error;
1075 return pw;
1076 error:
1077 FN(PW,free)(pw);
1078 return NULL;
1081 #ifndef NO_OPT
1082 /* Compute the maximal value attained by the piecewise quasipolynomial
1083 * on its domain or zero if the domain is empty.
1084 * In the worst case, the domain is scanned completely,
1085 * so the domain is assumed to be bounded.
1087 __isl_give isl_qpolynomial *FN(PW,opt)(__isl_take PW *pw, int max)
1089 int i;
1090 isl_qpolynomial *opt;
1092 if (!pw)
1093 return NULL;
1095 if (pw->n == 0) {
1096 isl_space *dim = isl_space_copy(pw->dim);
1097 FN(PW,free)(pw);
1098 return isl_qpolynomial_zero_on_domain(isl_space_domain(dim));
1101 opt = FN(EL,opt_on_domain)(FN(EL,copy)(pw->p[0].FIELD),
1102 isl_set_copy(pw->p[0].set), max);
1103 for (i = 1; i < pw->n; ++i) {
1104 isl_qpolynomial *opt_i;
1105 opt_i = FN(EL,opt_on_domain)(FN(EL,copy)(pw->p[i].FIELD),
1106 isl_set_copy(pw->p[i].set), max);
1107 if (max)
1108 opt = isl_qpolynomial_max_cst(opt, opt_i);
1109 else
1110 opt = isl_qpolynomial_min_cst(opt, opt_i);
1113 FN(PW,free)(pw);
1114 return opt;
1117 __isl_give isl_qpolynomial *FN(PW,max)(__isl_take PW *pw)
1119 return FN(PW,opt)(pw, 1);
1122 __isl_give isl_qpolynomial *FN(PW,min)(__isl_take PW *pw)
1124 return FN(PW,opt)(pw, 0);
1126 #endif
1128 __isl_give isl_space *FN(PW,get_space)(__isl_keep PW *pw)
1130 return pw ? isl_space_copy(pw->dim) : NULL;
1133 __isl_give isl_space *FN(PW,get_domain_space)(__isl_keep PW *pw)
1135 return pw ? isl_space_domain(isl_space_copy(pw->dim)) : NULL;
1138 #ifndef NO_RESET_DIM
1139 /* Reset the space of "pw". Since we don't know if the elements
1140 * represent the spaces themselves or their domains, we pass along
1141 * both when we call their reset_space_and_domain.
1143 static __isl_give PW *FN(PW,reset_space_and_domain)(__isl_take PW *pw,
1144 __isl_take isl_space *space, __isl_take isl_space *domain)
1146 int i;
1148 pw = FN(PW,cow)(pw);
1149 if (!pw || !space || !domain)
1150 goto error;
1152 for (i = 0; i < pw->n; ++i) {
1153 pw->p[i].set = isl_set_reset_space(pw->p[i].set,
1154 isl_space_copy(domain));
1155 if (!pw->p[i].set)
1156 goto error;
1157 pw->p[i].FIELD = FN(EL,reset_space_and_domain)(pw->p[i].FIELD,
1158 isl_space_copy(space), isl_space_copy(domain));
1159 if (!pw->p[i].FIELD)
1160 goto error;
1163 isl_space_free(domain);
1165 isl_space_free(pw->dim);
1166 pw->dim = space;
1168 return pw;
1169 error:
1170 isl_space_free(domain);
1171 isl_space_free(space);
1172 FN(PW,free)(pw);
1173 return NULL;
1176 __isl_give PW *FN(PW,reset_domain_space)(__isl_take PW *pw,
1177 __isl_take isl_space *domain)
1179 isl_space *space;
1181 space = isl_space_extend_domain_with_range(isl_space_copy(domain),
1182 FN(PW,get_space)(pw));
1183 return FN(PW,reset_space_and_domain)(pw, space, domain);
1186 __isl_give PW *FN(PW,reset_space)(__isl_take PW *pw, __isl_take isl_space *dim)
1188 isl_space *domain;
1190 domain = isl_space_domain(isl_space_copy(dim));
1191 return FN(PW,reset_space_and_domain)(pw, dim, domain);
1194 __isl_give PW *FN(PW,set_tuple_id)(__isl_keep PW *pw, enum isl_dim_type type,
1195 __isl_take isl_id *id)
1197 isl_space *space;
1199 pw = FN(PW,cow)(pw);
1200 if (!pw)
1201 return isl_id_free(id);
1203 space = FN(PW,get_space)(pw);
1204 space = isl_space_set_tuple_id(space, type, id);
1206 return FN(PW,reset_space)(pw, space);
1209 __isl_give PW *FN(PW,set_dim_id)(__isl_take PW *pw,
1210 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
1212 pw = FN(PW,cow)(pw);
1213 if (!pw)
1214 return isl_id_free(id);
1215 pw->dim = isl_space_set_dim_id(pw->dim, type, pos, id);
1216 return FN(PW,reset_space)(pw, isl_space_copy(pw->dim));
1218 #endif
1220 int FN(PW,has_equal_space)(__isl_keep PW *pw1, __isl_keep PW *pw2)
1222 if (!pw1 || !pw2)
1223 return -1;
1225 return isl_space_is_equal(pw1->dim, pw2->dim);
1228 #ifndef NO_MORPH
1229 __isl_give PW *FN(PW,morph_domain)(__isl_take PW *pw,
1230 __isl_take isl_morph *morph)
1232 int i;
1233 isl_ctx *ctx;
1235 if (!pw || !morph)
1236 goto error;
1238 ctx = isl_space_get_ctx(pw->dim);
1239 isl_assert(ctx, isl_space_is_domain(morph->dom->dim, pw->dim),
1240 goto error);
1242 pw = FN(PW,cow)(pw);
1243 if (!pw)
1244 goto error;
1245 pw->dim = isl_space_extend_domain_with_range(
1246 isl_space_copy(morph->ran->dim), pw->dim);
1247 if (!pw->dim)
1248 goto error;
1250 for (i = 0; i < pw->n; ++i) {
1251 pw->p[i].set = isl_morph_set(isl_morph_copy(morph), pw->p[i].set);
1252 if (!pw->p[i].set)
1253 goto error;
1254 pw->p[i].FIELD = FN(EL,morph_domain)(pw->p[i].FIELD,
1255 isl_morph_copy(morph));
1256 if (!pw->p[i].FIELD)
1257 goto error;
1260 isl_morph_free(morph);
1262 return pw;
1263 error:
1264 FN(PW,free)(pw);
1265 isl_morph_free(morph);
1266 return NULL;
1268 #endif
1270 int FN(PW,foreach_piece)(__isl_keep PW *pw,
1271 int (*fn)(__isl_take isl_set *set, __isl_take EL *el, void *user),
1272 void *user)
1274 int i;
1276 if (!pw)
1277 return -1;
1279 for (i = 0; i < pw->n; ++i)
1280 if (fn(isl_set_copy(pw->p[i].set),
1281 FN(EL,copy)(pw->p[i].FIELD), user) < 0)
1282 return -1;
1284 return 0;
1287 #ifndef NO_LIFT
1288 static int any_divs(__isl_keep isl_set *set)
1290 int i;
1292 if (!set)
1293 return -1;
1295 for (i = 0; i < set->n; ++i)
1296 if (set->p[i]->n_div > 0)
1297 return 1;
1299 return 0;
1302 static int foreach_lifted_subset(__isl_take isl_set *set, __isl_take EL *el,
1303 int (*fn)(__isl_take isl_set *set, __isl_take EL *el,
1304 void *user), void *user)
1306 int i;
1308 if (!set || !el)
1309 goto error;
1311 for (i = 0; i < set->n; ++i) {
1312 isl_set *lift;
1313 EL *copy;
1315 lift = isl_set_from_basic_set(isl_basic_set_copy(set->p[i]));
1316 lift = isl_set_lift(lift);
1318 copy = FN(EL,copy)(el);
1319 copy = FN(EL,lift)(copy, isl_set_get_space(lift));
1321 if (fn(lift, copy, user) < 0)
1322 goto error;
1325 isl_set_free(set);
1326 FN(EL,free)(el);
1328 return 0;
1329 error:
1330 isl_set_free(set);
1331 FN(EL,free)(el);
1332 return -1;
1335 int FN(PW,foreach_lifted_piece)(__isl_keep PW *pw,
1336 int (*fn)(__isl_take isl_set *set, __isl_take EL *el,
1337 void *user), void *user)
1339 int i;
1341 if (!pw)
1342 return -1;
1344 for (i = 0; i < pw->n; ++i) {
1345 isl_set *set;
1346 EL *el;
1348 set = isl_set_copy(pw->p[i].set);
1349 el = FN(EL,copy)(pw->p[i].FIELD);
1350 if (!any_divs(set)) {
1351 if (fn(set, el, user) < 0)
1352 return -1;
1353 continue;
1355 if (foreach_lifted_subset(set, el, fn, user) < 0)
1356 return -1;
1359 return 0;
1361 #endif
1363 #ifndef NO_MOVE_DIMS
1364 __isl_give PW *FN(PW,move_dims)(__isl_take PW *pw,
1365 enum isl_dim_type dst_type, unsigned dst_pos,
1366 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
1368 int i;
1370 pw = FN(PW,cow)(pw);
1371 if (!pw)
1372 return NULL;
1374 pw->dim = isl_space_move_dims(pw->dim, dst_type, dst_pos, src_type, src_pos, n);
1375 if (!pw->dim)
1376 goto error;
1378 for (i = 0; i < pw->n; ++i) {
1379 pw->p[i].FIELD = FN(EL,move_dims)(pw->p[i].FIELD,
1380 dst_type, dst_pos, src_type, src_pos, n);
1381 if (!pw->p[i].FIELD)
1382 goto error;
1385 if (dst_type == isl_dim_in)
1386 dst_type = isl_dim_set;
1387 if (src_type == isl_dim_in)
1388 src_type = isl_dim_set;
1390 for (i = 0; i < pw->n; ++i) {
1391 pw->p[i].set = isl_set_move_dims(pw->p[i].set,
1392 dst_type, dst_pos,
1393 src_type, src_pos, n);
1394 if (!pw->p[i].set)
1395 goto error;
1398 return pw;
1399 error:
1400 FN(PW,free)(pw);
1401 return NULL;
1403 #endif
1405 __isl_give PW *FN(PW,mul_isl_int)(__isl_take PW *pw, isl_int v)
1407 int i;
1409 if (isl_int_is_one(v))
1410 return pw;
1411 if (pw && isl_int_is_zero(v)) {
1412 PW *zero;
1413 isl_space *dim = FN(PW,get_space)(pw);
1414 #ifdef HAS_TYPE
1415 zero = FN(PW,ZERO)(dim, pw->type);
1416 #else
1417 zero = FN(PW,ZERO)(dim);
1418 #endif
1419 FN(PW,free)(pw);
1420 return zero;
1422 pw = FN(PW,cow)(pw);
1423 if (!pw)
1424 return NULL;
1425 if (pw->n == 0)
1426 return pw;
1428 #ifdef HAS_TYPE
1429 if (isl_int_is_neg(v))
1430 pw->type = isl_fold_type_negate(pw->type);
1431 #endif
1432 for (i = 0; i < pw->n; ++i) {
1433 pw->p[i].FIELD = FN(EL,scale)(pw->p[i].FIELD, v);
1434 if (!pw->p[i].FIELD)
1435 goto error;
1438 return pw;
1439 error:
1440 FN(PW,free)(pw);
1441 return NULL;
1444 __isl_give PW *FN(PW,scale)(__isl_take PW *pw, isl_int v)
1446 return FN(PW,mul_isl_int)(pw, v);
1449 static int FN(PW,qsort_set_cmp)(const void *p1, const void *p2)
1451 const isl_set *set1 = *(const isl_set **)p1;
1452 const isl_set *set2 = *(const isl_set **)p2;
1454 return isl_set_plain_cmp(set1, set2);
1457 /* We normalize in place, but if anything goes wrong we need
1458 * to return NULL, so we need to make sure we don't change the
1459 * meaning of any possible other copies of map.
1461 __isl_give PW *FN(PW,normalize)(__isl_take PW *pw)
1463 int i, j;
1464 isl_set *set;
1466 if (!pw)
1467 return NULL;
1468 for (i = 0; i < pw->n; ++i) {
1469 set = isl_set_normalize(isl_set_copy(pw->p[i].set));
1470 if (!set)
1471 return FN(PW,free)(pw);
1472 isl_set_free(pw->p[i].set);
1473 pw->p[i].set = set;
1475 qsort(pw->p, pw->n, sizeof(pw->p[0]), &FN(PW,qsort_set_cmp));
1476 for (i = pw->n - 1; i >= 1; --i) {
1477 if (!isl_set_plain_is_equal(pw->p[i - 1].set, pw->p[i].set))
1478 continue;
1479 if (!FN(EL,plain_is_equal)(pw->p[i - 1].FIELD, pw->p[i].FIELD))
1480 continue;
1481 set = isl_set_union(isl_set_copy(pw->p[i - 1].set),
1482 isl_set_copy(pw->p[i].set));
1483 if (!set)
1484 return FN(PW,free)(pw);
1485 isl_set_free(pw->p[i].set);
1486 FN(EL,free)(pw->p[i].FIELD);
1487 isl_set_free(pw->p[i - 1].set);
1488 pw->p[i - 1].set = set;
1489 for (j = i + 1; j < pw->n; ++j)
1490 pw->p[j - 1] = pw->p[j];
1491 pw->n--;
1494 return pw;
1497 /* Is pw1 obviously equal to pw2?
1498 * That is, do they have obviously identical cells and obviously identical
1499 * elements on each cell?
1501 int FN(PW,plain_is_equal)(__isl_keep PW *pw1, __isl_keep PW *pw2)
1503 int i;
1504 int equal;
1506 if (!pw1 || !pw2)
1507 return -1;
1509 if (pw1 == pw2)
1510 return 1;
1511 if (!isl_space_is_equal(pw1->dim, pw2->dim))
1512 return 0;
1514 pw1 = FN(PW,copy)(pw1);
1515 pw2 = FN(PW,copy)(pw2);
1516 pw1 = FN(PW,normalize)(pw1);
1517 pw2 = FN(PW,normalize)(pw2);
1518 if (!pw1 || !pw2)
1519 goto error;
1521 equal = pw1->n == pw2->n;
1522 for (i = 0; equal && i < pw1->n; ++i) {
1523 equal = isl_set_plain_is_equal(pw1->p[i].set, pw2->p[i].set);
1524 if (equal < 0)
1525 goto error;
1526 if (!equal)
1527 break;
1528 equal = FN(EL,plain_is_equal)(pw1->p[i].FIELD, pw2->p[i].FIELD);
1529 if (equal < 0)
1530 goto error;
1533 FN(PW,free)(pw1);
1534 FN(PW,free)(pw2);
1535 return equal;
1536 error:
1537 FN(PW,free)(pw1);
1538 FN(PW,free)(pw2);
1539 return -1;