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)
7 static __isl_give PW *FN(PW,alloc_)(__isl_take isl_dim *dim,
8 enum isl_fold type, int n)
10 static __isl_give PW *FN(PW,alloc_)(__isl_take isl_dim *dim, int n)
17 isl_assert(dim->ctx, n >= 0, goto error);
18 pw = isl_alloc(dim->ctx, struct PW,
19 sizeof(struct PW) + (n - 1) * sizeof(S(PW,piece)));
37 __isl_give PW *FN(PW,zero)(__isl_take isl_dim *dim, enum isl_fold type)
39 return FN(PW,alloc_)(dim, type, 0);
42 __isl_give PW *FN(PW,zero)(__isl_take isl_dim *dim)
44 return FN(PW,alloc_)(dim, 0);
48 __isl_give PW *FN(PW,add_piece)(__isl_take PW *pw,
49 __isl_take isl_set *set, __isl_take EL *el)
51 if (!pw || !set || !el)
54 if (isl_set_plain_is_empty(set) || FN(EL,IS_ZERO)(el)) {
61 if (pw->type != el->type)
62 isl_die(set->ctx, isl_error_invalid, "fold types don't match",
65 isl_assert(set->ctx, isl_dim_equal(pw->dim, el->dim), goto error);
66 isl_assert(set->ctx, pw->n < pw->size, goto error);
68 pw->p[pw->n].set = set;
69 pw->p[pw->n].FIELD = el;
81 __isl_give PW *FN(PW,alloc)(enum isl_fold type,
82 __isl_take isl_set *set, __isl_take EL *el)
84 __isl_give PW *FN(PW,alloc)(__isl_take isl_set *set, __isl_take EL *el)
93 pw = FN(PW,alloc_)(isl_set_get_dim(set), type, 1);
95 pw = FN(PW,alloc_)(isl_set_get_dim(set), 1);
98 return FN(PW,add_piece)(pw, set, el);
105 __isl_give PW *FN(PW,dup)(__isl_keep PW *pw)
114 dup = FN(PW,alloc_)(isl_dim_copy(pw->dim), pw->type, pw->n);
116 dup = FN(PW,alloc_)(isl_dim_copy(pw->dim), pw->n);
121 for (i = 0; i < pw->n; ++i)
122 dup = FN(PW,add_piece)(dup, isl_set_copy(pw->p[i].set),
123 FN(EL,copy)(pw->p[i].FIELD));
128 __isl_give PW *FN(PW,cow)(__isl_take PW *pw)
136 return FN(PW,dup)(pw);
139 __isl_give PW *FN(PW,copy)(__isl_keep PW *pw)
148 void FN(PW,free)(__isl_take PW *pw)
157 for (i = 0; i < pw->n; ++i) {
158 isl_set_free(pw->p[i].set);
159 FN(EL,free)(pw->p[i].FIELD);
161 isl_dim_free(pw->dim);
165 int FN(PW,is_zero)(__isl_keep PW *pw)
173 __isl_give PW *FN(PW,add)(__isl_take PW *pw1, __isl_take PW *pw2)
183 if (pw1->type != pw2->type)
184 isl_die(pw1->dim->ctx, isl_error_invalid,
185 "fold types don't match", goto error);
187 isl_assert(pw1->dim->ctx, isl_dim_equal(pw1->dim, pw2->dim), goto error);
189 if (FN(PW,is_zero)(pw1)) {
194 if (FN(PW,is_zero)(pw2)) {
199 n = (pw1->n + 1) * (pw2->n + 1);
201 res = FN(PW,alloc_)(isl_dim_copy(pw1->dim), pw1->type, n);
203 res = FN(PW,alloc_)(isl_dim_copy(pw1->dim), n);
206 for (i = 0; i < pw1->n; ++i) {
207 set = isl_set_copy(pw1->p[i].set);
208 for (j = 0; j < pw2->n; ++j) {
209 struct isl_set *common;
211 set = isl_set_subtract(set,
212 isl_set_copy(pw2->p[j].set));
213 common = isl_set_intersect(isl_set_copy(pw1->p[i].set),
214 isl_set_copy(pw2->p[j].set));
215 if (isl_set_plain_is_empty(common)) {
216 isl_set_free(common);
220 sum = FN(EL,add_on_domain)(common,
221 FN(EL,copy)(pw1->p[i].FIELD),
222 FN(EL,copy)(pw2->p[j].FIELD));
224 res = FN(PW,add_piece)(res, common, sum);
226 res = FN(PW,add_piece)(res, set, FN(EL,copy)(pw1->p[i].FIELD));
229 for (j = 0; j < pw2->n; ++j) {
230 set = isl_set_copy(pw2->p[j].set);
231 for (i = 0; i < pw1->n; ++i)
232 set = isl_set_subtract(set,
233 isl_set_copy(pw1->p[i].set));
234 res = FN(PW,add_piece)(res, set, FN(EL,copy)(pw2->p[j].FIELD));
247 __isl_give PW *FN(PW,add_disjoint)(__isl_take PW *pw1, __isl_take PW *pw2)
256 if (pw1->type != pw2->type)
257 isl_die(pw1->dim->ctx, isl_error_invalid,
258 "fold types don't match", goto error);
260 isl_assert(pw1->dim->ctx, isl_dim_equal(pw1->dim, pw2->dim), goto error);
262 if (FN(PW,is_zero)(pw1)) {
267 if (FN(PW,is_zero)(pw2)) {
273 res = FN(PW,alloc_)(isl_dim_copy(pw1->dim), pw1->type, pw1->n + pw2->n);
275 res = FN(PW,alloc_)(isl_dim_copy(pw1->dim), pw1->n + pw2->n);
278 for (i = 0; i < pw1->n; ++i)
279 res = FN(PW,add_piece)(res,
280 isl_set_copy(pw1->p[i].set),
281 FN(EL,copy)(pw1->p[i].FIELD));
283 for (i = 0; i < pw2->n; ++i)
284 res = FN(PW,add_piece)(res,
285 isl_set_copy(pw2->p[i].set),
286 FN(EL,copy)(pw2->p[i].FIELD));
298 __isl_give isl_qpolynomial *FN(PW,eval)(__isl_take PW *pw,
299 __isl_take isl_point *pnt)
307 isl_assert(pnt->dim->ctx, isl_dim_equal(pnt->dim, pw->dim), goto error);
309 for (i = 0; i < pw->n; ++i) {
310 found = isl_set_contains_point(pw->p[i].set, pnt);
317 qp = FN(EL,eval)(FN(EL,copy)(pw->p[i].FIELD),
318 isl_point_copy(pnt));
320 qp = isl_qpolynomial_zero(isl_dim_copy(pw->dim));
330 __isl_give isl_set *FN(PW,domain)(__isl_take PW *pw)
338 dom = isl_set_empty(isl_dim_copy(pw->dim));
339 for (i = 0; i < pw->n; ++i)
340 dom = isl_set_union_disjoint(dom, isl_set_copy(pw->p[i].set));
347 __isl_give PW *FN(PW,intersect_domain)(__isl_take PW *pw, __isl_take isl_set *set)
363 for (i = pw->n - 1; i >= 0; --i) {
365 pw->p[i].set = isl_set_intersect(pw->p[i].set, isl_set_copy(set));
368 aff = isl_set_affine_hull(isl_set_copy(pw->p[i].set));
369 pw->p[i].FIELD = FN(EL,substitute_equalities)(pw->p[i].FIELD,
371 if (isl_set_plain_is_empty(pw->p[i].set)) {
372 isl_set_free(pw->p[i].set);
373 FN(EL,free)(pw->p[i].FIELD);
375 pw->p[i] = pw->p[pw->n - 1];
388 __isl_give PW *FN(PW,gist)(__isl_take PW *pw, __isl_take isl_set *context)
391 isl_basic_set *hull = NULL;
397 isl_set_free(context);
401 context = isl_set_compute_divs(context);
402 hull = isl_set_simple_hull(isl_set_copy(context));
408 for (i = pw->n - 1; i >= 0; --i) {
409 pw->p[i].set = isl_set_intersect(pw->p[i].set,
410 isl_set_copy(context));
413 pw->p[i].FIELD = FN(EL,gist)(pw->p[i].FIELD,
414 isl_set_copy(pw->p[i].set));
415 pw->p[i].set = isl_set_gist_basic_set(pw->p[i].set,
416 isl_basic_set_copy(hull));
419 if (isl_set_plain_is_empty(pw->p[i].set)) {
420 isl_set_free(pw->p[i].set);
421 FN(EL,free)(pw->p[i].FIELD);
423 pw->p[i] = pw->p[pw->n - 1];
428 isl_basic_set_free(hull);
429 isl_set_free(context);
434 isl_basic_set_free(hull);
435 isl_set_free(context);
439 __isl_give PW *FN(PW,coalesce)(__isl_take PW *pw)
448 for (i = pw->n - 1; i >= 0; --i) {
449 for (j = i - 1; j >= 0; --j) {
450 if (!FN(EL,plain_is_equal)(pw->p[i].FIELD,
453 pw->p[j].set = isl_set_union(pw->p[j].set,
455 FN(EL,free)(pw->p[i].FIELD);
457 pw->p[i] = pw->p[pw->n - 1];
463 pw->p[i].set = isl_set_coalesce(pw->p[i].set);
474 isl_ctx *FN(PW,get_ctx)(__isl_keep PW *pw)
476 return pw ? pw->dim->ctx : NULL;
479 int FN(PW,involves_dims)(__isl_keep PW *pw, enum isl_dim_type type,
480 unsigned first, unsigned n)
486 if (pw->n == 0 || n == 0)
488 for (i = 0; i < pw->n; ++i) {
489 int involves = FN(EL,involves_dims)(pw->p[i].FIELD,
491 if (involves < 0 || involves)
493 involves = isl_set_involves_dims(pw->p[i].set, type, first, n);
494 if (involves < 0 || involves)
500 __isl_give PW *FN(PW,set_dim_name)(__isl_take PW *pw,
501 enum isl_dim_type type, unsigned pos, const char *s)
509 pw->dim = isl_dim_set_name(pw->dim, type, pos, s);
513 for (i = 0; i < pw->n; ++i) {
514 pw->p[i].set = isl_set_set_dim_name(pw->p[i].set, type, pos, s);
517 pw->p[i].FIELD = FN(EL,set_dim_name)(pw->p[i].FIELD, type, pos, s);
528 __isl_give PW *FN(PW,drop_dims)(__isl_take PW *pw,
529 enum isl_dim_type type, unsigned first, unsigned n)
535 if (n == 0 && !isl_dim_get_tuple_name(pw->dim, type))
541 pw->dim = isl_dim_drop(pw->dim, type, first, n);
544 for (i = 0; i < pw->n; ++i) {
545 pw->p[i].set = isl_set_drop(pw->p[i].set, type, first, n);
548 pw->p[i].FIELD = FN(EL,drop_dims)(pw->p[i].FIELD, type, first, n);
559 __isl_give PW *FN(PW,insert_dims)(__isl_take PW *pw, enum isl_dim_type type,
560 unsigned first, unsigned n)
566 if (n == 0 && !isl_dim_is_named_or_nested(pw->dim, type))
573 pw->dim = isl_dim_insert(pw->dim, type, first, n);
577 for (i = 0; i < pw->n; ++i) {
578 pw->p[i].set = isl_set_insert(pw->p[i].set, type, first, n);
581 pw->p[i].FIELD = FN(EL,insert_dims)(pw->p[i].FIELD,
593 __isl_give PW *FN(PW,fix_dim)(__isl_take PW *pw,
594 enum isl_dim_type type, unsigned pos, isl_int v)
604 for (i = 0; i < pw->n; ++i) {
605 pw->p[i].set = isl_set_fix(pw->p[i].set, type, pos, v);
616 unsigned FN(PW,dim)(__isl_keep PW *pw, enum isl_dim_type type)
618 return pw ? isl_dim_size(pw->dim, type) : 0;
621 __isl_give PW *FN(PW,split_dims)(__isl_take PW *pw,
622 enum isl_dim_type type, unsigned first, unsigned n)
636 for (i = 0; i < pw->n; ++i) {
637 pw->p[i].set = isl_set_split_dims(pw->p[i].set, type, first, n);
648 /* Compute the maximal value attained by the piecewise quasipolynomial
649 * on its domain or zero if the domain is empty.
650 * In the worst case, the domain is scanned completely,
651 * so the domain is assumed to be bounded.
653 __isl_give isl_qpolynomial *FN(PW,opt)(__isl_take PW *pw, int max)
656 isl_qpolynomial *opt;
662 isl_dim *dim = isl_dim_copy(pw->dim);
664 return isl_qpolynomial_zero(dim);
667 opt = FN(EL,opt_on_domain)(FN(EL,copy)(pw->p[0].FIELD),
668 isl_set_copy(pw->p[0].set), max);
669 for (i = 1; i < pw->n; ++i) {
670 isl_qpolynomial *opt_i;
671 opt_i = FN(EL,opt_on_domain)(FN(EL,copy)(pw->p[i].FIELD),
672 isl_set_copy(pw->p[i].set), max);
674 opt = isl_qpolynomial_max_cst(opt, opt_i);
676 opt = isl_qpolynomial_min_cst(opt, opt_i);
683 __isl_give isl_qpolynomial *FN(PW,max)(__isl_take PW *pw)
685 return FN(PW,opt)(pw, 1);
688 __isl_give isl_qpolynomial *FN(PW,min)(__isl_take PW *pw)
690 return FN(PW,opt)(pw, 0);
693 __isl_give isl_dim *FN(PW,get_dim)(__isl_keep PW *pw)
695 return pw ? isl_dim_copy(pw->dim) : NULL;
698 __isl_give PW *FN(PW,reset_dim)(__isl_take PW *pw, __isl_take isl_dim *dim)
706 for (i = 0; i < pw->n; ++i) {
707 pw->p[i].set = isl_set_reset_dim(pw->p[i].set,
711 pw->p[i].FIELD = FN(EL,reset_dim)(pw->p[i].FIELD,
716 isl_dim_free(pw->dim);
726 int FN(PW,has_equal_dim)(__isl_keep PW *pw1, __isl_keep PW *pw2)
731 return isl_dim_equal(pw1->dim, pw2->dim);
734 __isl_give PW *FN(PW,morph)(__isl_take PW *pw, __isl_take isl_morph *morph)
741 isl_assert(pw->dim->ctx, isl_dim_equal(pw->dim, morph->dom->dim),
747 isl_dim_free(pw->dim);
748 pw->dim = isl_dim_copy(morph->ran->dim);
752 for (i = 0; i < pw->n; ++i) {
753 pw->p[i].set = isl_morph_set(isl_morph_copy(morph), pw->p[i].set);
756 pw->p[i].FIELD = FN(EL,morph)(pw->p[i].FIELD,
757 isl_morph_copy(morph));
762 isl_morph_free(morph);
767 isl_morph_free(morph);
771 int FN(PW,foreach_piece)(__isl_keep PW *pw,
772 int (*fn)(__isl_take isl_set *set, __isl_take EL *el, void *user),
780 for (i = 0; i < pw->n; ++i)
781 if (fn(isl_set_copy(pw->p[i].set),
782 FN(EL,copy)(pw->p[i].FIELD), user) < 0)
788 static int any_divs(__isl_keep isl_set *set)
795 for (i = 0; i < set->n; ++i)
796 if (set->p[i]->n_div > 0)
802 static int foreach_lifted_subset(__isl_take isl_set *set, __isl_take EL *el,
803 int (*fn)(__isl_take isl_set *set, __isl_take EL *el,
804 void *user), void *user)
811 for (i = 0; i < set->n; ++i) {
815 lift = isl_set_from_basic_set(isl_basic_set_copy(set->p[i]));
816 lift = isl_set_lift(lift);
818 copy = FN(EL,copy)(el);
819 copy = FN(EL,lift)(copy, isl_set_get_dim(lift));
821 if (fn(lift, copy, user) < 0)
835 int FN(PW,foreach_lifted_piece)(__isl_keep PW *pw,
836 int (*fn)(__isl_take isl_set *set, __isl_take EL *el,
837 void *user), void *user)
844 for (i = 0; i < pw->n; ++i) {
848 set = isl_set_copy(pw->p[i].set);
849 el = FN(EL,copy)(pw->p[i].FIELD);
850 if (!any_divs(set)) {
851 if (fn(set, el, user) < 0)
855 if (foreach_lifted_subset(set, el, fn, user) < 0)
862 __isl_give PW *FN(PW,move_dims)(__isl_take PW *pw,
863 enum isl_dim_type dst_type, unsigned dst_pos,
864 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
872 pw->dim = isl_dim_move(pw->dim, dst_type, dst_pos, src_type, src_pos, n);
876 for (i = 0; i < pw->n; ++i) {
877 pw->p[i].set = isl_set_move_dims(pw->p[i].set,
879 src_type, src_pos, n);
882 pw->p[i].FIELD = FN(EL,move_dims)(pw->p[i].FIELD,
883 dst_type, dst_pos, src_type, src_pos, n);
894 __isl_give PW *FN(PW,realign)(__isl_take PW *pw, __isl_take isl_reordering *exp)
902 for (i = 0; i < pw->n; ++i) {
903 pw->p[i].set = isl_set_realign(pw->p[i].set,
904 isl_reordering_copy(exp));
907 pw->p[i].FIELD = FN(EL,realign)(pw->p[i].FIELD,
908 isl_reordering_copy(exp));
913 pw = FN(PW,reset_dim)(pw, isl_dim_copy(exp->dim));
915 isl_reordering_free(exp);
918 isl_reordering_free(exp);
923 __isl_give PW *FN(PW,mul_isl_int)(__isl_take PW *pw, isl_int v)
927 if (isl_int_is_one(v))
929 if (pw && isl_int_is_zero(v)) {
931 isl_dim *dim = FN(PW,get_dim)(pw);
933 zero = FN(PW,zero)(dim, pw->type);
935 zero = FN(PW,zero)(dim);
947 if (isl_int_is_neg(v))
948 pw->type = isl_fold_type_negate(pw->type);
950 for (i = 0; i < pw->n; ++i) {
951 pw->p[i].FIELD = FN(EL,mul_isl_int)(pw->p[i].FIELD, v);