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)
18 ctx = isl_dim_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)));
39 __isl_give PW *FN(PW,zero)(__isl_take isl_dim *dim, enum isl_fold type)
41 return FN(PW,alloc_)(dim, type, 0);
44 __isl_give PW *FN(PW,zero)(__isl_take isl_dim *dim)
46 return FN(PW,alloc_)(dim, 0);
50 __isl_give PW *FN(PW,add_piece)(__isl_take PW *pw,
51 __isl_take isl_set *set, __isl_take EL *el)
54 isl_dim *el_dim = NULL;
56 if (!pw || !set || !el)
59 if (isl_set_plain_is_empty(set) || FN(EL,IS_ZERO)(el)) {
65 ctx = isl_set_get_ctx(set);
67 if (pw->type != el->type)
68 isl_die(ctx, isl_error_invalid, "fold types don't match",
71 el_dim = FN(EL,get_dim(el));
72 isl_assert(ctx, isl_dim_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;
90 __isl_give PW *FN(PW,alloc)(enum isl_fold type,
91 __isl_take isl_set *set, __isl_take EL *el)
93 __isl_give PW *FN(PW,alloc)(__isl_take isl_set *set, __isl_take EL *el)
102 pw = FN(PW,alloc_)(isl_set_get_dim(set), type, 1);
104 pw = FN(PW,alloc_)(isl_set_get_dim(set), 1);
107 return FN(PW,add_piece)(pw, set, el);
114 __isl_give PW *FN(PW,dup)(__isl_keep PW *pw)
123 dup = FN(PW,alloc_)(isl_dim_copy(pw->dim), pw->type, pw->n);
125 dup = FN(PW,alloc_)(isl_dim_copy(pw->dim), pw->n);
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));
137 __isl_give PW *FN(PW,cow)(__isl_take PW *pw)
145 return FN(PW,dup)(pw);
148 __isl_give PW *FN(PW,copy)(__isl_keep PW *pw)
157 void FN(PW,free)(__isl_take PW *pw)
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_dim_free(pw->dim);
174 int FN(PW,is_zero)(__isl_keep PW *pw)
182 __isl_give PW *FN(PW,add)(__isl_take PW *pw1, __isl_take PW *pw2)
192 ctx = isl_dim_get_ctx(pw1->dim);
194 if (pw1->type != pw2->type)
195 isl_die(ctx, isl_error_invalid,
196 "fold types don't match", goto error);
198 isl_assert(ctx, isl_dim_equal(pw1->dim, pw2->dim), goto error);
200 if (FN(PW,is_zero)(pw1)) {
205 if (FN(PW,is_zero)(pw2)) {
210 n = (pw1->n + 1) * (pw2->n + 1);
212 res = FN(PW,alloc_)(isl_dim_copy(pw1->dim), pw1->type, n);
214 res = FN(PW,alloc_)(isl_dim_copy(pw1->dim), n);
217 for (i = 0; i < pw1->n; ++i) {
218 set = isl_set_copy(pw1->p[i].set);
219 for (j = 0; j < pw2->n; ++j) {
220 struct isl_set *common;
222 set = isl_set_subtract(set,
223 isl_set_copy(pw2->p[j].set));
224 common = isl_set_intersect(isl_set_copy(pw1->p[i].set),
225 isl_set_copy(pw2->p[j].set));
226 if (isl_set_plain_is_empty(common)) {
227 isl_set_free(common);
231 sum = FN(EL,add_on_domain)(common,
232 FN(EL,copy)(pw1->p[i].FIELD),
233 FN(EL,copy)(pw2->p[j].FIELD));
235 res = FN(PW,add_piece)(res, common, sum);
237 res = FN(PW,add_piece)(res, set, FN(EL,copy)(pw1->p[i].FIELD));
240 for (j = 0; j < pw2->n; ++j) {
241 set = isl_set_copy(pw2->p[j].set);
242 for (i = 0; i < pw1->n; ++i)
243 set = isl_set_subtract(set,
244 isl_set_copy(pw1->p[i].set));
245 res = FN(PW,add_piece)(res, set, FN(EL,copy)(pw2->p[j].FIELD));
258 __isl_give PW *FN(PW,add_disjoint)(__isl_take PW *pw1, __isl_take PW *pw2)
267 ctx = isl_dim_get_ctx(pw1->dim);
269 if (pw1->type != pw2->type)
270 isl_die(ctx, isl_error_invalid,
271 "fold types don't match", goto error);
273 isl_assert(ctx, isl_dim_equal(pw1->dim, pw2->dim), goto error);
275 if (FN(PW,is_zero)(pw1)) {
280 if (FN(PW,is_zero)(pw2)) {
286 res = FN(PW,alloc_)(isl_dim_copy(pw1->dim), pw1->type, pw1->n + pw2->n);
288 res = FN(PW,alloc_)(isl_dim_copy(pw1->dim), pw1->n + pw2->n);
291 for (i = 0; i < pw1->n; ++i)
292 res = FN(PW,add_piece)(res,
293 isl_set_copy(pw1->p[i].set),
294 FN(EL,copy)(pw1->p[i].FIELD));
296 for (i = 0; i < pw2->n; ++i)
297 res = FN(PW,add_piece)(res,
298 isl_set_copy(pw2->p[i].set),
299 FN(EL,copy)(pw2->p[i].FIELD));
311 __isl_give isl_qpolynomial *FN(PW,eval)(__isl_take PW *pw,
312 __isl_take isl_point *pnt)
317 isl_dim *pnt_dim = NULL;
322 ctx = isl_point_get_ctx(pnt);
323 pnt_dim = isl_point_get_dim(pnt);
324 isl_assert(ctx, isl_dim_equal(pnt_dim, pw->dim), goto error);
326 for (i = 0; i < pw->n; ++i) {
327 found = isl_set_contains_point(pw->p[i].set, pnt);
334 qp = FN(EL,eval)(FN(EL,copy)(pw->p[i].FIELD),
335 isl_point_copy(pnt));
337 qp = isl_qpolynomial_zero(isl_dim_copy(pw->dim));
339 isl_dim_free(pnt_dim);
344 isl_dim_free(pnt_dim);
349 __isl_give isl_set *FN(PW,domain)(__isl_take PW *pw)
357 dom = isl_set_empty(isl_dim_copy(pw->dim));
358 for (i = 0; i < pw->n; ++i)
359 dom = isl_set_union_disjoint(dom, isl_set_copy(pw->p[i].set));
366 __isl_give PW *FN(PW,intersect_domain)(__isl_take PW *pw, __isl_take isl_set *set)
382 for (i = pw->n - 1; i >= 0; --i) {
384 pw->p[i].set = isl_set_intersect(pw->p[i].set, isl_set_copy(set));
387 aff = isl_set_affine_hull(isl_set_copy(pw->p[i].set));
388 pw->p[i].FIELD = FN(EL,substitute_equalities)(pw->p[i].FIELD,
390 if (isl_set_plain_is_empty(pw->p[i].set)) {
391 isl_set_free(pw->p[i].set);
392 FN(EL,free)(pw->p[i].FIELD);
394 pw->p[i] = pw->p[pw->n - 1];
407 __isl_give PW *FN(PW,gist)(__isl_take PW *pw, __isl_take isl_set *context)
410 isl_basic_set *hull = NULL;
416 isl_set_free(context);
420 context = isl_set_compute_divs(context);
421 hull = isl_set_simple_hull(isl_set_copy(context));
427 for (i = pw->n - 1; i >= 0; --i) {
428 pw->p[i].set = isl_set_intersect(pw->p[i].set,
429 isl_set_copy(context));
432 pw->p[i].FIELD = FN(EL,gist)(pw->p[i].FIELD,
433 isl_set_copy(pw->p[i].set));
434 pw->p[i].set = isl_set_gist_basic_set(pw->p[i].set,
435 isl_basic_set_copy(hull));
438 if (isl_set_plain_is_empty(pw->p[i].set)) {
439 isl_set_free(pw->p[i].set);
440 FN(EL,free)(pw->p[i].FIELD);
442 pw->p[i] = pw->p[pw->n - 1];
447 isl_basic_set_free(hull);
448 isl_set_free(context);
453 isl_basic_set_free(hull);
454 isl_set_free(context);
458 __isl_give PW *FN(PW,coalesce)(__isl_take PW *pw)
467 for (i = pw->n - 1; i >= 0; --i) {
468 for (j = i - 1; j >= 0; --j) {
469 if (!FN(EL,plain_is_equal)(pw->p[i].FIELD,
472 pw->p[j].set = isl_set_union(pw->p[j].set,
474 FN(EL,free)(pw->p[i].FIELD);
476 pw->p[i] = pw->p[pw->n - 1];
482 pw->p[i].set = isl_set_coalesce(pw->p[i].set);
493 isl_ctx *FN(PW,get_ctx)(__isl_keep PW *pw)
495 return pw ? isl_dim_get_ctx(pw->dim) : NULL;
498 int FN(PW,involves_dims)(__isl_keep PW *pw, enum isl_dim_type type,
499 unsigned first, unsigned n)
505 if (pw->n == 0 || n == 0)
507 for (i = 0; i < pw->n; ++i) {
508 int involves = FN(EL,involves_dims)(pw->p[i].FIELD,
510 if (involves < 0 || involves)
512 involves = isl_set_involves_dims(pw->p[i].set, type, first, n);
513 if (involves < 0 || involves)
519 __isl_give PW *FN(PW,set_dim_name)(__isl_take PW *pw,
520 enum isl_dim_type type, unsigned pos, const char *s)
528 pw->dim = isl_dim_set_name(pw->dim, type, pos, s);
532 for (i = 0; i < pw->n; ++i) {
533 pw->p[i].set = isl_set_set_dim_name(pw->p[i].set, type, pos, s);
536 pw->p[i].FIELD = FN(EL,set_dim_name)(pw->p[i].FIELD, type, pos, s);
547 __isl_give PW *FN(PW,drop_dims)(__isl_take PW *pw,
548 enum isl_dim_type type, unsigned first, unsigned n)
554 if (n == 0 && !isl_dim_get_tuple_name(pw->dim, type))
560 pw->dim = isl_dim_drop(pw->dim, type, first, n);
563 for (i = 0; i < pw->n; ++i) {
564 pw->p[i].set = isl_set_drop(pw->p[i].set, type, first, n);
567 pw->p[i].FIELD = FN(EL,drop_dims)(pw->p[i].FIELD, type, first, n);
578 __isl_give PW *FN(PW,insert_dims)(__isl_take PW *pw, enum isl_dim_type type,
579 unsigned first, unsigned n)
585 if (n == 0 && !isl_dim_is_named_or_nested(pw->dim, type))
592 pw->dim = isl_dim_insert(pw->dim, type, first, n);
596 for (i = 0; i < pw->n; ++i) {
597 pw->p[i].set = isl_set_insert(pw->p[i].set, type, first, n);
600 pw->p[i].FIELD = FN(EL,insert_dims)(pw->p[i].FIELD,
612 __isl_give PW *FN(PW,fix_dim)(__isl_take PW *pw,
613 enum isl_dim_type type, unsigned pos, isl_int v)
623 for (i = 0; i < pw->n; ++i) {
624 pw->p[i].set = isl_set_fix(pw->p[i].set, type, pos, v);
635 unsigned FN(PW,dim)(__isl_keep PW *pw, enum isl_dim_type type)
637 return pw ? isl_dim_size(pw->dim, type) : 0;
640 __isl_give PW *FN(PW,split_dims)(__isl_take PW *pw,
641 enum isl_dim_type type, unsigned first, unsigned n)
655 for (i = 0; i < pw->n; ++i) {
656 pw->p[i].set = isl_set_split_dims(pw->p[i].set, type, first, n);
667 /* Compute the maximal value attained by the piecewise quasipolynomial
668 * on its domain or zero if the domain is empty.
669 * In the worst case, the domain is scanned completely,
670 * so the domain is assumed to be bounded.
672 __isl_give isl_qpolynomial *FN(PW,opt)(__isl_take PW *pw, int max)
675 isl_qpolynomial *opt;
681 isl_dim *dim = isl_dim_copy(pw->dim);
683 return isl_qpolynomial_zero(dim);
686 opt = FN(EL,opt_on_domain)(FN(EL,copy)(pw->p[0].FIELD),
687 isl_set_copy(pw->p[0].set), max);
688 for (i = 1; i < pw->n; ++i) {
689 isl_qpolynomial *opt_i;
690 opt_i = FN(EL,opt_on_domain)(FN(EL,copy)(pw->p[i].FIELD),
691 isl_set_copy(pw->p[i].set), max);
693 opt = isl_qpolynomial_max_cst(opt, opt_i);
695 opt = isl_qpolynomial_min_cst(opt, opt_i);
702 __isl_give isl_qpolynomial *FN(PW,max)(__isl_take PW *pw)
704 return FN(PW,opt)(pw, 1);
707 __isl_give isl_qpolynomial *FN(PW,min)(__isl_take PW *pw)
709 return FN(PW,opt)(pw, 0);
712 __isl_give isl_dim *FN(PW,get_dim)(__isl_keep PW *pw)
714 return pw ? isl_dim_copy(pw->dim) : NULL;
717 __isl_give PW *FN(PW,reset_dim)(__isl_take PW *pw, __isl_take isl_dim *dim)
725 for (i = 0; i < pw->n; ++i) {
726 pw->p[i].set = isl_set_reset_dim(pw->p[i].set,
730 pw->p[i].FIELD = FN(EL,reset_dim)(pw->p[i].FIELD,
735 isl_dim_free(pw->dim);
745 int FN(PW,has_equal_dim)(__isl_keep PW *pw1, __isl_keep PW *pw2)
750 return isl_dim_equal(pw1->dim, pw2->dim);
753 __isl_give PW *FN(PW,morph)(__isl_take PW *pw, __isl_take isl_morph *morph)
761 ctx = isl_dim_get_ctx(pw->dim);
762 isl_assert(ctx, isl_dim_equal(pw->dim, morph->dom->dim),
768 isl_dim_free(pw->dim);
769 pw->dim = isl_dim_copy(morph->ran->dim);
773 for (i = 0; i < pw->n; ++i) {
774 pw->p[i].set = isl_morph_set(isl_morph_copy(morph), pw->p[i].set);
777 pw->p[i].FIELD = FN(EL,morph)(pw->p[i].FIELD,
778 isl_morph_copy(morph));
783 isl_morph_free(morph);
788 isl_morph_free(morph);
792 int FN(PW,foreach_piece)(__isl_keep PW *pw,
793 int (*fn)(__isl_take isl_set *set, __isl_take EL *el, void *user),
801 for (i = 0; i < pw->n; ++i)
802 if (fn(isl_set_copy(pw->p[i].set),
803 FN(EL,copy)(pw->p[i].FIELD), user) < 0)
809 static int any_divs(__isl_keep isl_set *set)
816 for (i = 0; i < set->n; ++i)
817 if (set->p[i]->n_div > 0)
823 static int foreach_lifted_subset(__isl_take isl_set *set, __isl_take EL *el,
824 int (*fn)(__isl_take isl_set *set, __isl_take EL *el,
825 void *user), void *user)
832 for (i = 0; i < set->n; ++i) {
836 lift = isl_set_from_basic_set(isl_basic_set_copy(set->p[i]));
837 lift = isl_set_lift(lift);
839 copy = FN(EL,copy)(el);
840 copy = FN(EL,lift)(copy, isl_set_get_dim(lift));
842 if (fn(lift, copy, user) < 0)
856 int FN(PW,foreach_lifted_piece)(__isl_keep PW *pw,
857 int (*fn)(__isl_take isl_set *set, __isl_take EL *el,
858 void *user), void *user)
865 for (i = 0; i < pw->n; ++i) {
869 set = isl_set_copy(pw->p[i].set);
870 el = FN(EL,copy)(pw->p[i].FIELD);
871 if (!any_divs(set)) {
872 if (fn(set, el, user) < 0)
876 if (foreach_lifted_subset(set, el, fn, user) < 0)
883 __isl_give PW *FN(PW,move_dims)(__isl_take PW *pw,
884 enum isl_dim_type dst_type, unsigned dst_pos,
885 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
893 pw->dim = isl_dim_move(pw->dim, dst_type, dst_pos, src_type, src_pos, n);
897 for (i = 0; i < pw->n; ++i) {
898 pw->p[i].set = isl_set_move_dims(pw->p[i].set,
900 src_type, src_pos, n);
903 pw->p[i].FIELD = FN(EL,move_dims)(pw->p[i].FIELD,
904 dst_type, dst_pos, src_type, src_pos, n);
915 __isl_give PW *FN(PW,realign)(__isl_take PW *pw, __isl_take isl_reordering *exp)
923 for (i = 0; i < pw->n; ++i) {
924 pw->p[i].set = isl_set_realign(pw->p[i].set,
925 isl_reordering_copy(exp));
928 pw->p[i].FIELD = FN(EL,realign)(pw->p[i].FIELD,
929 isl_reordering_copy(exp));
934 pw = FN(PW,reset_dim)(pw, isl_dim_copy(exp->dim));
936 isl_reordering_free(exp);
939 isl_reordering_free(exp);
944 __isl_give PW *FN(PW,mul_isl_int)(__isl_take PW *pw, isl_int v)
948 if (isl_int_is_one(v))
950 if (pw && isl_int_is_zero(v)) {
952 isl_dim *dim = FN(PW,get_dim)(pw);
954 zero = FN(PW,zero)(dim, pw->type);
956 zero = FN(PW,zero)(dim);
968 if (isl_int_is_neg(v))
969 pw->type = isl_fold_type_negate(pw->type);
971 for (i = 0; i < pw->n; ++i) {
972 pw->p[i].FIELD = FN(EL,scale)(pw->p[i].FIELD, v);