3 #define xFN(TYPE,NAME) TYPE ## _ ## NAME
4 #define FN(TYPE,NAME) xFN(TYPE,NAME)
5 #define xS(TYPE,NAME) struct TYPE ## _ ## NAME
6 #define S(TYPE,NAME) xS(TYPE,NAME)
9 __isl_give PW *FN(PW,alloc_size)(__isl_take isl_space *dim,
10 enum isl_fold type, int n)
12 __isl_give PW *FN(PW,alloc_size)(__isl_take isl_space *dim, int n)
20 ctx = isl_space_get_ctx(dim);
21 isl_assert(ctx, n >= 0, goto error);
22 pw = isl_alloc(ctx, struct PW,
23 sizeof(struct PW) + (n - 1) * sizeof(S(PW,piece)));
41 __isl_give PW *FN(PW,ZERO)(__isl_take isl_space *dim, enum isl_fold type)
43 return FN(PW,alloc_size)(dim, type, 0);
46 __isl_give PW *FN(PW,ZERO)(__isl_take isl_space *dim)
48 return FN(PW,alloc_size)(dim, 0);
52 __isl_give PW *FN(PW,add_piece)(__isl_take PW *pw,
53 __isl_take isl_set *set, __isl_take EL *el)
56 isl_space *el_dim = NULL;
58 if (!pw || !set || !el)
61 if (isl_set_plain_is_empty(set) || FN(EL,EL_IS_ZERO)(el)) {
67 ctx = isl_set_get_ctx(set);
69 if (pw->type != el->type)
70 isl_die(ctx, isl_error_invalid, "fold types don't match",
73 el_dim = FN(EL,get_space(el));
74 isl_assert(ctx, isl_space_is_equal(pw->dim, el_dim), goto error);
75 isl_assert(ctx, pw->n < pw->size, goto error);
77 pw->p[pw->n].set = set;
78 pw->p[pw->n].FIELD = el;
81 isl_space_free(el_dim);
84 isl_space_free(el_dim);
92 __isl_give PW *FN(PW,alloc)(enum isl_fold type,
93 __isl_take isl_set *set, __isl_take EL *el)
95 __isl_give PW *FN(PW,alloc)(__isl_take isl_set *set, __isl_take EL *el)
104 pw = FN(PW,alloc_size)(FN(EL,get_space)(el), type, 1);
106 pw = FN(PW,alloc_size)(FN(EL,get_space)(el), 1);
109 return FN(PW,add_piece)(pw, set, el);
116 __isl_give PW *FN(PW,dup)(__isl_keep PW *pw)
125 dup = FN(PW,alloc_size)(isl_space_copy(pw->dim), pw->type, pw->n);
127 dup = FN(PW,alloc_size)(isl_space_copy(pw->dim), pw->n);
132 for (i = 0; i < pw->n; ++i)
133 dup = FN(PW,add_piece)(dup, isl_set_copy(pw->p[i].set),
134 FN(EL,copy)(pw->p[i].FIELD));
139 __isl_give PW *FN(PW,cow)(__isl_take PW *pw)
147 return FN(PW,dup)(pw);
150 __isl_give PW *FN(PW,copy)(__isl_keep PW *pw)
159 void *FN(PW,free)(__isl_take PW *pw)
168 for (i = 0; i < pw->n; ++i) {
169 isl_set_free(pw->p[i].set);
170 FN(EL,free)(pw->p[i].FIELD);
172 isl_space_free(pw->dim);
178 const char *FN(PW,get_dim_name)(__isl_keep PW *pw, enum isl_dim_type type,
181 return pw ? isl_space_get_dim_name(pw->dim, type, pos) : NULL;
184 int FN(PW,has_dim_id)(__isl_keep PW *pw, enum isl_dim_type type, unsigned pos)
186 return pw ? isl_space_has_dim_id(pw->dim, type, pos) : -1;
189 __isl_give isl_id *FN(PW,get_dim_id)(__isl_keep PW *pw, enum isl_dim_type type,
192 return pw ? isl_space_get_dim_id(pw->dim, type, pos) : NULL;
195 int FN(PW,has_tuple_name)(__isl_keep PW *pw, enum isl_dim_type type)
197 return pw ? isl_space_has_tuple_name(pw->dim, type) : -1;
200 const char *FN(PW,get_tuple_name)(__isl_keep PW *pw, enum isl_dim_type type)
202 return pw ? isl_space_get_tuple_name(pw->dim, type) : NULL;
205 int FN(PW,has_tuple_id)(__isl_keep PW *pw, enum isl_dim_type type)
207 return pw ? isl_space_has_tuple_id(pw->dim, type) : -1;
210 __isl_give isl_id *FN(PW,get_tuple_id)(__isl_keep PW *pw, enum isl_dim_type type)
212 return pw ? isl_space_get_tuple_id(pw->dim, type) : NULL;
215 int FN(PW,IS_ZERO)(__isl_keep PW *pw)
224 __isl_give PW *FN(PW,realign_domain)(__isl_take PW *pw,
225 __isl_take isl_reordering *exp)
233 for (i = 0; i < pw->n; ++i) {
234 pw->p[i].set = isl_set_realign(pw->p[i].set,
235 isl_reordering_copy(exp));
238 pw->p[i].FIELD = FN(EL,realign_domain)(pw->p[i].FIELD,
239 isl_reordering_copy(exp));
244 pw = FN(PW,reset_domain_space)(pw, isl_space_copy(exp->dim));
246 isl_reordering_free(exp);
249 isl_reordering_free(exp);
254 /* Align the parameters of "pw" to those of "model".
256 __isl_give PW *FN(PW,align_params)(__isl_take PW *pw, __isl_take isl_space *model)
263 ctx = isl_space_get_ctx(model);
264 if (!isl_space_has_named_params(model))
265 isl_die(ctx, isl_error_invalid,
266 "model has unnamed parameters", goto error);
267 if (!isl_space_has_named_params(pw->dim))
268 isl_die(ctx, isl_error_invalid,
269 "input has unnamed parameters", goto error);
270 if (!isl_space_match(pw->dim, isl_dim_param, model, isl_dim_param)) {
273 model = isl_space_drop_dims(model, isl_dim_in,
274 0, isl_space_dim(model, isl_dim_in));
275 model = isl_space_drop_dims(model, isl_dim_out,
276 0, isl_space_dim(model, isl_dim_out));
277 exp = isl_parameter_alignment_reordering(pw->dim, model);
278 exp = isl_reordering_extend_space(exp,
279 FN(PW,get_domain_space)(pw));
280 pw = FN(PW,realign_domain)(pw, exp);
283 isl_space_free(model);
286 isl_space_free(model);
291 static __isl_give PW *FN(PW,align_params_pw_pw_and)(__isl_take PW *pw1,
293 __isl_give PW *(*fn)(__isl_take PW *pw1, __isl_take PW *pw2))
299 if (isl_space_match(pw1->dim, isl_dim_param, pw2->dim, isl_dim_param))
301 ctx = FN(PW,get_ctx)(pw1);
302 if (!isl_space_has_named_params(pw1->dim) ||
303 !isl_space_has_named_params(pw2->dim))
304 isl_die(ctx, isl_error_invalid,
305 "unaligned unnamed parameters", goto error);
306 pw1 = FN(PW,align_params)(pw1, FN(PW,get_space)(pw2));
307 pw2 = FN(PW,align_params)(pw2, FN(PW,get_space)(pw1));
315 static __isl_give PW *FN(PW,align_params_pw_set_and)(__isl_take PW *pw,
316 __isl_take isl_set *set,
317 __isl_give PW *(*fn)(__isl_take PW *pw, __isl_take isl_set *set))
323 if (isl_space_match(pw->dim, isl_dim_param, set->dim, isl_dim_param))
325 ctx = FN(PW,get_ctx)(pw);
326 if (!isl_space_has_named_params(pw->dim) ||
327 !isl_space_has_named_params(set->dim))
328 isl_die(ctx, isl_error_invalid,
329 "unaligned unnamed parameters", goto error);
330 pw = FN(PW,align_params)(pw, isl_set_get_space(set));
331 set = isl_set_align_params(set, FN(PW,get_space)(pw));
340 static __isl_give PW *FN(PW,union_add_aligned)(__isl_take PW *pw1,
351 ctx = isl_space_get_ctx(pw1->dim);
353 if (pw1->type != pw2->type)
354 isl_die(ctx, isl_error_invalid,
355 "fold types don't match", goto error);
357 isl_assert(ctx, isl_space_is_equal(pw1->dim, pw2->dim), goto error);
359 if (FN(PW,IS_ZERO)(pw1)) {
364 if (FN(PW,IS_ZERO)(pw2)) {
369 n = (pw1->n + 1) * (pw2->n + 1);
371 res = FN(PW,alloc_size)(isl_space_copy(pw1->dim), pw1->type, n);
373 res = FN(PW,alloc_size)(isl_space_copy(pw1->dim), n);
376 for (i = 0; i < pw1->n; ++i) {
377 set = isl_set_copy(pw1->p[i].set);
378 for (j = 0; j < pw2->n; ++j) {
379 struct isl_set *common;
381 common = isl_set_intersect(isl_set_copy(pw1->p[i].set),
382 isl_set_copy(pw2->p[j].set));
383 if (isl_set_plain_is_empty(common)) {
384 isl_set_free(common);
387 set = isl_set_subtract(set,
388 isl_set_copy(pw2->p[j].set));
390 sum = FN(EL,add_on_domain)(common,
391 FN(EL,copy)(pw1->p[i].FIELD),
392 FN(EL,copy)(pw2->p[j].FIELD));
394 res = FN(PW,add_piece)(res, common, sum);
396 res = FN(PW,add_piece)(res, set, FN(EL,copy)(pw1->p[i].FIELD));
399 for (j = 0; j < pw2->n; ++j) {
400 set = isl_set_copy(pw2->p[j].set);
401 for (i = 0; i < pw1->n; ++i)
402 set = isl_set_subtract(set,
403 isl_set_copy(pw1->p[i].set));
404 res = FN(PW,add_piece)(res, set, FN(EL,copy)(pw2->p[j].FIELD));
417 /* Private version of "union_add". For isl_pw_qpolynomial and
418 * isl_pw_qpolynomial_fold, we prefer to simply call it "add".
420 static __isl_give PW *FN(PW,union_add_)(__isl_take PW *pw1, __isl_take PW *pw2)
422 return FN(PW,align_params_pw_pw_and)(pw1, pw2,
423 &FN(PW,union_add_aligned));
426 /* Make sure "pw" has room for at least "n" more pieces.
428 * If there is only one reference to pw, we extend it in place.
429 * Otherwise, we create a new PW and copy the pieces.
431 static __isl_give PW *FN(PW,grow)(__isl_take PW *pw, int n)
439 if (pw->n + n <= pw->size)
441 ctx = FN(PW,get_ctx)(pw);
444 res = isl_realloc(ctx, pw, struct PW,
445 sizeof(struct PW) + (n - 1) * sizeof(S(PW,piece)));
447 return FN(PW,free)(pw);
452 res = FN(PW,alloc_size)(isl_space_copy(pw->dim), pw->type, n);
454 res = FN(PW,alloc_size)(isl_space_copy(pw->dim), n);
457 return FN(PW,free)(pw);
458 for (i = 0; i < pw->n; ++i)
459 res = FN(PW,add_piece)(res, isl_set_copy(pw->p[i].set),
460 FN(EL,copy)(pw->p[i].FIELD));
465 static __isl_give PW *FN(PW,add_disjoint_aligned)(__isl_take PW *pw1,
474 if (pw1->size < pw1->n + pw2->n && pw1->n < pw2->n)
475 return FN(PW,add_disjoint_aligned)(pw2, pw1);
477 ctx = isl_space_get_ctx(pw1->dim);
479 if (pw1->type != pw2->type)
480 isl_die(ctx, isl_error_invalid,
481 "fold types don't match", goto error);
483 isl_assert(ctx, isl_space_is_equal(pw1->dim, pw2->dim), goto error);
485 if (FN(PW,IS_ZERO)(pw1)) {
490 if (FN(PW,IS_ZERO)(pw2)) {
495 pw1 = FN(PW,grow)(pw1, pw2->n);
499 for (i = 0; i < pw2->n; ++i)
500 pw1 = FN(PW,add_piece)(pw1,
501 isl_set_copy(pw2->p[i].set),
502 FN(EL,copy)(pw2->p[i].FIELD));
513 __isl_give PW *FN(PW,add_disjoint)(__isl_take PW *pw1, __isl_take PW *pw2)
515 return FN(PW,align_params_pw_pw_and)(pw1, pw2,
516 &FN(PW,add_disjoint_aligned));
519 /* This function is currently only used from isl_aff.c
521 static __isl_give PW *FN(PW,on_shared_domain_in)(__isl_take PW *pw1,
522 __isl_take PW *pw2, __isl_take isl_space *space,
523 __isl_give EL *(*fn)(__isl_take EL *el1, __isl_take EL *el2))
524 __attribute__ ((unused));
526 /* Apply "fn" to pairs of elements from pw1 and pw2 on shared domains.
527 * The result of "fn" (and therefore also of this function) lives in "space".
529 static __isl_give PW *FN(PW,on_shared_domain_in)(__isl_take PW *pw1,
530 __isl_take PW *pw2, __isl_take isl_space *space,
531 __isl_give EL *(*fn)(__isl_take EL *el1, __isl_take EL *el2))
541 res = FN(PW,alloc_size)(isl_space_copy(space), pw1->type, n);
543 res = FN(PW,alloc_size)(isl_space_copy(space), n);
546 for (i = 0; i < pw1->n; ++i) {
547 for (j = 0; j < pw2->n; ++j) {
552 common = isl_set_intersect(
553 isl_set_copy(pw1->p[i].set),
554 isl_set_copy(pw2->p[j].set));
555 empty = isl_set_plain_is_empty(common);
556 if (empty < 0 || empty) {
557 isl_set_free(common);
563 res_ij = fn(FN(EL,copy)(pw1->p[i].FIELD),
564 FN(EL,copy)(pw2->p[j].FIELD));
565 res_ij = FN(EL,gist)(res_ij, isl_set_copy(common));
567 res = FN(PW,add_piece)(res, common, res_ij);
571 isl_space_free(space);
576 isl_space_free(space);
583 /* This function is currently only used from isl_aff.c
585 static __isl_give PW *FN(PW,on_shared_domain)(__isl_take PW *pw1,
587 __isl_give EL *(*fn)(__isl_take EL *el1, __isl_take EL *el2))
588 __attribute__ ((unused));
590 /* Apply "fn" to pairs of elements from pw1 and pw2 on shared domains.
591 * The result of "fn" is assumed to live in the same space as "pw1" and "pw2".
593 static __isl_give PW *FN(PW,on_shared_domain)(__isl_take PW *pw1,
595 __isl_give EL *(*fn)(__isl_take EL *el1, __isl_take EL *el2))
602 space = isl_space_copy(pw1->dim);
603 return FN(PW,on_shared_domain_in)(pw1, pw2, space, fn);
611 __isl_give PW *FN(PW,neg)(__isl_take PW *pw)
618 if (FN(PW,IS_ZERO)(pw))
625 for (i = 0; i < pw->n; ++i) {
626 pw->p[i].FIELD = FN(EL,neg)(pw->p[i].FIELD);
628 return FN(PW,free)(pw);
634 __isl_give PW *FN(PW,sub)(__isl_take PW *pw1, __isl_take PW *pw2)
636 return FN(PW,add)(pw1, FN(PW,neg)(pw2));
641 __isl_give isl_qpolynomial *FN(PW,eval)(__isl_take PW *pw,
642 __isl_take isl_point *pnt)
647 isl_space *pnt_dim = NULL;
652 ctx = isl_point_get_ctx(pnt);
653 pnt_dim = isl_point_get_space(pnt);
654 isl_assert(ctx, isl_space_is_domain_internal(pnt_dim, pw->dim),
657 for (i = 0; i < pw->n; ++i) {
658 found = isl_set_contains_point(pw->p[i].set, pnt);
665 qp = FN(EL,eval)(FN(EL,copy)(pw->p[i].FIELD),
666 isl_point_copy(pnt));
668 qp = isl_qpolynomial_zero_on_domain(FN(PW,get_domain_space)(pw));
670 isl_space_free(pnt_dim);
675 isl_space_free(pnt_dim);
681 __isl_give isl_set *FN(PW,domain)(__isl_take PW *pw)
689 dom = isl_set_empty(FN(PW,get_domain_space)(pw));
690 for (i = 0; i < pw->n; ++i)
691 dom = isl_set_union_disjoint(dom, isl_set_copy(pw->p[i].set));
698 /* Restrict the domain of "pw" by combining each cell
699 * with "set" through a call to "fn", where "fn" may be
700 * isl_set_intersect or isl_set_intersect_params.
702 static __isl_give PW *FN(PW,intersect_aligned)(__isl_take PW *pw,
703 __isl_take isl_set *set,
704 __isl_give isl_set *(*fn)(__isl_take isl_set *set1,
705 __isl_take isl_set *set2))
721 for (i = pw->n - 1; i >= 0; --i) {
723 pw->p[i].set = fn(pw->p[i].set, isl_set_copy(set));
726 aff = isl_set_affine_hull(isl_set_copy(pw->p[i].set));
727 pw->p[i].FIELD = FN(EL,substitute_equalities)(pw->p[i].FIELD,
731 if (isl_set_plain_is_empty(pw->p[i].set)) {
732 isl_set_free(pw->p[i].set);
733 FN(EL,free)(pw->p[i].FIELD);
735 pw->p[i] = pw->p[pw->n - 1];
748 static __isl_give PW *FN(PW,intersect_domain_aligned)(__isl_take PW *pw,
749 __isl_take isl_set *set)
751 return FN(PW,intersect_aligned)(pw, set, &isl_set_intersect);
754 __isl_give PW *FN(PW,intersect_domain)(__isl_take PW *pw,
755 __isl_take isl_set *context)
757 return FN(PW,align_params_pw_set_and)(pw, context,
758 &FN(PW,intersect_domain_aligned));
761 static __isl_give PW *FN(PW,intersect_params_aligned)(__isl_take PW *pw,
762 __isl_take isl_set *set)
764 return FN(PW,intersect_aligned)(pw, set, &isl_set_intersect_params);
767 /* Intersect the domain of "pw" with the parameter domain "context".
769 __isl_give PW *FN(PW,intersect_params)(__isl_take PW *pw,
770 __isl_take isl_set *context)
772 return FN(PW,align_params_pw_set_and)(pw, context,
773 &FN(PW,intersect_params_aligned));
776 static __isl_give PW *FN(PW,gist_aligned)(__isl_take PW *pw,
777 __isl_take isl_set *context,
778 __isl_give EL *(*fn_el)(__isl_take EL *el,
779 __isl_take isl_set *set),
780 __isl_give isl_set *(*fn_dom)(__isl_take isl_set *set,
781 __isl_take isl_basic_set *bset))
784 isl_basic_set *hull = NULL;
790 isl_set_free(context);
794 if (!isl_space_match(pw->dim, isl_dim_param,
795 context->dim, isl_dim_param)) {
796 pw = FN(PW,align_params)(pw, isl_set_get_space(context));
797 context = isl_set_align_params(context, FN(PW,get_space)(pw));
800 context = isl_set_compute_divs(context);
801 hull = isl_set_simple_hull(isl_set_copy(context));
807 for (i = pw->n - 1; i >= 0; --i) {
811 set_i = isl_set_intersect(isl_set_copy(pw->p[i].set),
812 isl_set_copy(context));
813 empty = isl_set_plain_is_empty(set_i);
814 pw->p[i].FIELD = fn_el(pw->p[i].FIELD, set_i);
815 pw->p[i].set = fn_dom(pw->p[i].set, isl_basic_set_copy(hull));
816 if (!pw->p[i].FIELD || !pw->p[i].set)
819 isl_set_free(pw->p[i].set);
820 FN(EL,free)(pw->p[i].FIELD);
822 pw->p[i] = pw->p[pw->n - 1];
827 isl_basic_set_free(hull);
828 isl_set_free(context);
833 isl_basic_set_free(hull);
834 isl_set_free(context);
838 static __isl_give PW *FN(PW,gist_domain_aligned)(__isl_take PW *pw,
839 __isl_take isl_set *set)
841 return FN(PW,gist_aligned)(pw, set, &FN(EL,gist),
842 &isl_set_gist_basic_set);
845 __isl_give PW *FN(PW,gist)(__isl_take PW *pw, __isl_take isl_set *context)
847 return FN(PW,align_params_pw_set_and)(pw, context,
848 &FN(PW,gist_domain_aligned));
851 static __isl_give PW *FN(PW,gist_params_aligned)(__isl_take PW *pw,
852 __isl_take isl_set *set)
854 return FN(PW,gist_aligned)(pw, set, &FN(EL,gist_params),
855 &isl_set_gist_params_basic_set);
858 __isl_give PW *FN(PW,gist_params)(__isl_take PW *pw,
859 __isl_take isl_set *context)
861 return FN(PW,align_params_pw_set_and)(pw, context,
862 &FN(PW,gist_params_aligned));
865 __isl_give PW *FN(PW,coalesce)(__isl_take PW *pw)
874 for (i = pw->n - 1; i >= 0; --i) {
875 for (j = i - 1; j >= 0; --j) {
876 if (!FN(EL,plain_is_equal)(pw->p[i].FIELD,
879 pw->p[j].set = isl_set_union(pw->p[j].set,
881 FN(EL,free)(pw->p[i].FIELD);
883 pw->p[i] = pw->p[pw->n - 1];
889 pw->p[i].set = isl_set_coalesce(pw->p[i].set);
900 isl_ctx *FN(PW,get_ctx)(__isl_keep PW *pw)
902 return pw ? isl_space_get_ctx(pw->dim) : NULL;
905 #ifndef NO_INVOLVES_DIMS
906 int FN(PW,involves_dims)(__isl_keep PW *pw, enum isl_dim_type type,
907 unsigned first, unsigned n)
910 enum isl_dim_type set_type;
914 if (pw->n == 0 || n == 0)
917 set_type = type == isl_dim_in ? isl_dim_set : type;
919 for (i = 0; i < pw->n; ++i) {
920 int involves = FN(EL,involves_dims)(pw->p[i].FIELD,
922 if (involves < 0 || involves)
924 involves = isl_set_involves_dims(pw->p[i].set,
926 if (involves < 0 || involves)
933 __isl_give PW *FN(PW,set_dim_name)(__isl_take PW *pw,
934 enum isl_dim_type type, unsigned pos, const char *s)
937 enum isl_dim_type set_type;
943 set_type = type == isl_dim_in ? isl_dim_set : type;
945 pw->dim = isl_space_set_dim_name(pw->dim, type, pos, s);
949 for (i = 0; i < pw->n; ++i) {
950 pw->p[i].set = isl_set_set_dim_name(pw->p[i].set,
954 pw->p[i].FIELD = FN(EL,set_dim_name)(pw->p[i].FIELD, type, pos, s);
966 __isl_give PW *FN(PW,drop_dims)(__isl_take PW *pw,
967 enum isl_dim_type type, unsigned first, unsigned n)
970 enum isl_dim_type set_type;
974 if (n == 0 && !isl_space_get_tuple_name(pw->dim, type))
977 set_type = type == isl_dim_in ? isl_dim_set : type;
982 pw->dim = isl_space_drop_dims(pw->dim, type, first, n);
985 for (i = 0; i < pw->n; ++i) {
986 pw->p[i].set = isl_set_drop(pw->p[i].set, set_type, first, n);
989 pw->p[i].FIELD = FN(EL,drop_dims)(pw->p[i].FIELD, type, first, n);
1000 /* This function is very similar to drop_dims.
1001 * The only difference is that the cells may still involve
1002 * the specified dimensions. They are removed using
1003 * isl_set_project_out instead of isl_set_drop.
1005 __isl_give PW *FN(PW,project_out)(__isl_take PW *pw,
1006 enum isl_dim_type type, unsigned first, unsigned n)
1009 enum isl_dim_type set_type;
1013 if (n == 0 && !isl_space_get_tuple_name(pw->dim, type))
1016 set_type = type == isl_dim_in ? isl_dim_set : type;
1018 pw = FN(PW,cow)(pw);
1021 pw->dim = isl_space_drop_dims(pw->dim, type, first, n);
1024 for (i = 0; i < pw->n; ++i) {
1025 pw->p[i].set = isl_set_project_out(pw->p[i].set,
1026 set_type, first, n);
1029 pw->p[i].FIELD = FN(EL,drop_dims)(pw->p[i].FIELD, type, first, n);
1030 if (!pw->p[i].FIELD)
1040 /* Project the domain of pw onto its parameter space.
1042 __isl_give PW *FN(PW,project_domain_on_params)(__isl_take PW *pw)
1047 n = FN(PW,dim)(pw, isl_dim_in);
1048 pw = FN(PW,project_out)(pw, isl_dim_in, 0, n);
1049 space = FN(PW,get_domain_space)(pw);
1050 space = isl_space_params(space);
1051 pw = FN(PW,reset_domain_space)(pw, space);
1056 #ifndef NO_INSERT_DIMS
1057 __isl_give PW *FN(PW,insert_dims)(__isl_take PW *pw, enum isl_dim_type type,
1058 unsigned first, unsigned n)
1061 enum isl_dim_type set_type;
1065 if (n == 0 && !isl_space_is_named_or_nested(pw->dim, type))
1068 set_type = type == isl_dim_in ? isl_dim_set : type;
1070 pw = FN(PW,cow)(pw);
1074 pw->dim = isl_space_insert_dims(pw->dim, type, first, n);
1078 for (i = 0; i < pw->n; ++i) {
1079 pw->p[i].set = isl_set_insert_dims(pw->p[i].set,
1080 set_type, first, n);
1083 pw->p[i].FIELD = FN(EL,insert_dims)(pw->p[i].FIELD,
1085 if (!pw->p[i].FIELD)
1096 __isl_give PW *FN(PW,fix_dim)(__isl_take PW *pw,
1097 enum isl_dim_type type, unsigned pos, isl_int v)
1104 if (type == isl_dim_in)
1107 pw = FN(PW,cow)(pw);
1110 for (i = 0; i < pw->n; ++i) {
1111 pw->p[i].set = isl_set_fix(pw->p[i].set, type, pos, v);
1122 unsigned FN(PW,dim)(__isl_keep PW *pw, enum isl_dim_type type)
1124 return pw ? isl_space_dim(pw->dim, type) : 0;
1127 __isl_give PW *FN(PW,split_dims)(__isl_take PW *pw,
1128 enum isl_dim_type type, unsigned first, unsigned n)
1137 if (type == isl_dim_in)
1140 pw = FN(PW,cow)(pw);
1145 for (i = 0; i < pw->n; ++i) {
1146 pw->p[i].set = isl_set_split_dims(pw->p[i].set, type, first, n);
1158 /* Compute the maximal value attained by the piecewise quasipolynomial
1159 * on its domain or zero if the domain is empty.
1160 * In the worst case, the domain is scanned completely,
1161 * so the domain is assumed to be bounded.
1163 __isl_give isl_qpolynomial *FN(PW,opt)(__isl_take PW *pw, int max)
1166 isl_qpolynomial *opt;
1172 isl_space *dim = isl_space_copy(pw->dim);
1174 return isl_qpolynomial_zero_on_domain(isl_space_domain(dim));
1177 opt = FN(EL,opt_on_domain)(FN(EL,copy)(pw->p[0].FIELD),
1178 isl_set_copy(pw->p[0].set), max);
1179 for (i = 1; i < pw->n; ++i) {
1180 isl_qpolynomial *opt_i;
1181 opt_i = FN(EL,opt_on_domain)(FN(EL,copy)(pw->p[i].FIELD),
1182 isl_set_copy(pw->p[i].set), max);
1184 opt = isl_qpolynomial_max_cst(opt, opt_i);
1186 opt = isl_qpolynomial_min_cst(opt, opt_i);
1193 __isl_give isl_qpolynomial *FN(PW,max)(__isl_take PW *pw)
1195 return FN(PW,opt)(pw, 1);
1198 __isl_give isl_qpolynomial *FN(PW,min)(__isl_take PW *pw)
1200 return FN(PW,opt)(pw, 0);
1204 __isl_give isl_space *FN(PW,get_space)(__isl_keep PW *pw)
1206 return pw ? isl_space_copy(pw->dim) : NULL;
1209 __isl_give isl_space *FN(PW,get_domain_space)(__isl_keep PW *pw)
1211 return pw ? isl_space_domain(isl_space_copy(pw->dim)) : NULL;
1214 #ifndef NO_RESET_DIM
1215 /* Reset the space of "pw". Since we don't know if the elements
1216 * represent the spaces themselves or their domains, we pass along
1217 * both when we call their reset_space_and_domain.
1219 static __isl_give PW *FN(PW,reset_space_and_domain)(__isl_take PW *pw,
1220 __isl_take isl_space *space, __isl_take isl_space *domain)
1224 pw = FN(PW,cow)(pw);
1225 if (!pw || !space || !domain)
1228 for (i = 0; i < pw->n; ++i) {
1229 pw->p[i].set = isl_set_reset_space(pw->p[i].set,
1230 isl_space_copy(domain));
1233 pw->p[i].FIELD = FN(EL,reset_space_and_domain)(pw->p[i].FIELD,
1234 isl_space_copy(space), isl_space_copy(domain));
1235 if (!pw->p[i].FIELD)
1239 isl_space_free(domain);
1241 isl_space_free(pw->dim);
1246 isl_space_free(domain);
1247 isl_space_free(space);
1252 __isl_give PW *FN(PW,reset_domain_space)(__isl_take PW *pw,
1253 __isl_take isl_space *domain)
1257 space = isl_space_extend_domain_with_range(isl_space_copy(domain),
1258 FN(PW,get_space)(pw));
1259 return FN(PW,reset_space_and_domain)(pw, space, domain);
1262 __isl_give PW *FN(PW,reset_space)(__isl_take PW *pw, __isl_take isl_space *dim)
1266 domain = isl_space_domain(isl_space_copy(dim));
1267 return FN(PW,reset_space_and_domain)(pw, dim, domain);
1270 __isl_give PW *FN(PW,set_tuple_id)(__isl_keep PW *pw, enum isl_dim_type type,
1271 __isl_take isl_id *id)
1275 pw = FN(PW,cow)(pw);
1277 return isl_id_free(id);
1279 space = FN(PW,get_space)(pw);
1280 space = isl_space_set_tuple_id(space, type, id);
1282 return FN(PW,reset_space)(pw, space);
1285 __isl_give PW *FN(PW,set_dim_id)(__isl_take PW *pw,
1286 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
1288 pw = FN(PW,cow)(pw);
1290 return isl_id_free(id);
1291 pw->dim = isl_space_set_dim_id(pw->dim, type, pos, id);
1292 return FN(PW,reset_space)(pw, isl_space_copy(pw->dim));
1296 int FN(PW,has_equal_space)(__isl_keep PW *pw1, __isl_keep PW *pw2)
1301 return isl_space_is_equal(pw1->dim, pw2->dim);
1305 __isl_give PW *FN(PW,morph_domain)(__isl_take PW *pw,
1306 __isl_take isl_morph *morph)
1314 ctx = isl_space_get_ctx(pw->dim);
1315 isl_assert(ctx, isl_space_is_domain_internal(morph->dom->dim, pw->dim),
1318 pw = FN(PW,cow)(pw);
1321 pw->dim = isl_space_extend_domain_with_range(
1322 isl_space_copy(morph->ran->dim), pw->dim);
1326 for (i = 0; i < pw->n; ++i) {
1327 pw->p[i].set = isl_morph_set(isl_morph_copy(morph), pw->p[i].set);
1330 pw->p[i].FIELD = FN(EL,morph_domain)(pw->p[i].FIELD,
1331 isl_morph_copy(morph));
1332 if (!pw->p[i].FIELD)
1336 isl_morph_free(morph);
1341 isl_morph_free(morph);
1346 int FN(PW,n_piece)(__isl_keep PW *pw)
1348 return pw ? pw->n : 0;
1351 int FN(PW,foreach_piece)(__isl_keep PW *pw,
1352 int (*fn)(__isl_take isl_set *set, __isl_take EL *el, void *user),
1360 for (i = 0; i < pw->n; ++i)
1361 if (fn(isl_set_copy(pw->p[i].set),
1362 FN(EL,copy)(pw->p[i].FIELD), user) < 0)
1369 static int any_divs(__isl_keep isl_set *set)
1376 for (i = 0; i < set->n; ++i)
1377 if (set->p[i]->n_div > 0)
1383 static int foreach_lifted_subset(__isl_take isl_set *set, __isl_take EL *el,
1384 int (*fn)(__isl_take isl_set *set, __isl_take EL *el,
1385 void *user), void *user)
1392 for (i = 0; i < set->n; ++i) {
1396 lift = isl_set_from_basic_set(isl_basic_set_copy(set->p[i]));
1397 lift = isl_set_lift(lift);
1399 copy = FN(EL,copy)(el);
1400 copy = FN(EL,lift)(copy, isl_set_get_space(lift));
1402 if (fn(lift, copy, user) < 0)
1416 int FN(PW,foreach_lifted_piece)(__isl_keep PW *pw,
1417 int (*fn)(__isl_take isl_set *set, __isl_take EL *el,
1418 void *user), void *user)
1425 for (i = 0; i < pw->n; ++i) {
1429 set = isl_set_copy(pw->p[i].set);
1430 el = FN(EL,copy)(pw->p[i].FIELD);
1431 if (!any_divs(set)) {
1432 if (fn(set, el, user) < 0)
1436 if (foreach_lifted_subset(set, el, fn, user) < 0)
1444 #ifndef NO_MOVE_DIMS
1445 __isl_give PW *FN(PW,move_dims)(__isl_take PW *pw,
1446 enum isl_dim_type dst_type, unsigned dst_pos,
1447 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
1451 pw = FN(PW,cow)(pw);
1455 pw->dim = isl_space_move_dims(pw->dim, dst_type, dst_pos, src_type, src_pos, n);
1459 for (i = 0; i < pw->n; ++i) {
1460 pw->p[i].FIELD = FN(EL,move_dims)(pw->p[i].FIELD,
1461 dst_type, dst_pos, src_type, src_pos, n);
1462 if (!pw->p[i].FIELD)
1466 if (dst_type == isl_dim_in)
1467 dst_type = isl_dim_set;
1468 if (src_type == isl_dim_in)
1469 src_type = isl_dim_set;
1471 for (i = 0; i < pw->n; ++i) {
1472 pw->p[i].set = isl_set_move_dims(pw->p[i].set,
1474 src_type, src_pos, n);
1486 __isl_give PW *FN(PW,mul_isl_int)(__isl_take PW *pw, isl_int v)
1490 if (isl_int_is_one(v))
1492 if (pw && DEFAULT_IS_ZERO && isl_int_is_zero(v)) {
1494 isl_space *dim = FN(PW,get_space)(pw);
1496 zero = FN(PW,ZERO)(dim, pw->type);
1498 zero = FN(PW,ZERO)(dim);
1503 pw = FN(PW,cow)(pw);
1510 if (isl_int_is_neg(v))
1511 pw->type = isl_fold_type_negate(pw->type);
1513 for (i = 0; i < pw->n; ++i) {
1514 pw->p[i].FIELD = FN(EL,scale)(pw->p[i].FIELD, v);
1515 if (!pw->p[i].FIELD)
1525 __isl_give PW *FN(PW,scale)(__isl_take PW *pw, isl_int v)
1527 return FN(PW,mul_isl_int)(pw, v);
1530 static int FN(PW,qsort_set_cmp)(const void *p1, const void *p2)
1532 isl_set *set1 = *(isl_set * const *)p1;
1533 isl_set *set2 = *(isl_set * const *)p2;
1535 return isl_set_plain_cmp(set1, set2);
1538 /* We normalize in place, but if anything goes wrong we need
1539 * to return NULL, so we need to make sure we don't change the
1540 * meaning of any possible other copies of map.
1542 __isl_give PW *FN(PW,normalize)(__isl_take PW *pw)
1549 for (i = 0; i < pw->n; ++i) {
1550 set = isl_set_normalize(isl_set_copy(pw->p[i].set));
1552 return FN(PW,free)(pw);
1553 isl_set_free(pw->p[i].set);
1556 qsort(pw->p, pw->n, sizeof(pw->p[0]), &FN(PW,qsort_set_cmp));
1557 for (i = pw->n - 1; i >= 1; --i) {
1558 if (!isl_set_plain_is_equal(pw->p[i - 1].set, pw->p[i].set))
1560 if (!FN(EL,plain_is_equal)(pw->p[i - 1].FIELD, pw->p[i].FIELD))
1562 set = isl_set_union(isl_set_copy(pw->p[i - 1].set),
1563 isl_set_copy(pw->p[i].set));
1565 return FN(PW,free)(pw);
1566 isl_set_free(pw->p[i].set);
1567 FN(EL,free)(pw->p[i].FIELD);
1568 isl_set_free(pw->p[i - 1].set);
1569 pw->p[i - 1].set = set;
1570 for (j = i + 1; j < pw->n; ++j)
1571 pw->p[j - 1] = pw->p[j];
1578 /* Is pw1 obviously equal to pw2?
1579 * That is, do they have obviously identical cells and obviously identical
1580 * elements on each cell?
1582 int FN(PW,plain_is_equal)(__isl_keep PW *pw1, __isl_keep PW *pw2)
1592 if (!isl_space_is_equal(pw1->dim, pw2->dim))
1595 pw1 = FN(PW,copy)(pw1);
1596 pw2 = FN(PW,copy)(pw2);
1597 pw1 = FN(PW,normalize)(pw1);
1598 pw2 = FN(PW,normalize)(pw2);
1602 equal = pw1->n == pw2->n;
1603 for (i = 0; equal && i < pw1->n; ++i) {
1604 equal = isl_set_plain_is_equal(pw1->p[i].set, pw2->p[i].set);
1609 equal = FN(EL,plain_is_equal)(pw1->p[i].FIELD, pw2->p[i].FIELD);
1624 static __isl_give PW *FN(PW,align_params_pw_multi_aff_and)(__isl_take PW *pw,
1625 __isl_take isl_multi_aff *ma,
1626 __isl_give PW *(*fn)(__isl_take PW *pw, __isl_take isl_multi_aff *ma))
1629 isl_space *ma_space;
1631 ma_space = isl_multi_aff_get_space(ma);
1632 if (!pw || !ma || !ma_space)
1634 if (isl_space_match(pw->dim, isl_dim_param, ma_space, isl_dim_param)) {
1635 isl_space_free(ma_space);
1638 ctx = FN(PW,get_ctx)(pw);
1639 if (!isl_space_has_named_params(pw->dim) ||
1640 !isl_space_has_named_params(ma_space))
1641 isl_die(ctx, isl_error_invalid,
1642 "unaligned unnamed parameters", goto error);
1643 pw = FN(PW,align_params)(pw, ma_space);
1644 ma = isl_multi_aff_align_params(ma, FN(PW,get_space)(pw));
1647 isl_space_free(ma_space);
1649 isl_multi_aff_free(ma);
1653 static __isl_give PW *FN(PW,align_params_pw_pw_multi_aff_and)(__isl_take PW *pw,
1654 __isl_take isl_pw_multi_aff *pma,
1655 __isl_give PW *(*fn)(__isl_take PW *pw,
1656 __isl_take isl_pw_multi_aff *ma))
1659 isl_space *pma_space;
1661 pma_space = isl_pw_multi_aff_get_space(pma);
1662 if (!pw || !pma || !pma_space)
1664 if (isl_space_match(pw->dim, isl_dim_param, pma_space, isl_dim_param)) {
1665 isl_space_free(pma_space);
1668 ctx = FN(PW,get_ctx)(pw);
1669 if (!isl_space_has_named_params(pw->dim) ||
1670 !isl_space_has_named_params(pma_space))
1671 isl_die(ctx, isl_error_invalid,
1672 "unaligned unnamed parameters", goto error);
1673 pw = FN(PW,align_params)(pw, pma_space);
1674 pma = isl_pw_multi_aff_align_params(pma, FN(PW,get_space)(pw));
1677 isl_space_free(pma_space);
1679 isl_pw_multi_aff_free(pma);
1683 /* Compute the pullback of "pw" by the function represented by "ma".
1684 * In other words, plug in "ma" in "pw".
1686 static __isl_give PW *FN(PW,pullback_multi_aff_aligned)(__isl_take PW *pw,
1687 __isl_take isl_multi_aff *ma)
1690 isl_space *space = NULL;
1692 ma = isl_multi_aff_align_divs(ma);
1693 pw = FN(PW,cow)(pw);
1697 space = isl_space_join(isl_multi_aff_get_space(ma),
1698 FN(PW,get_space)(pw));
1700 for (i = 0; i < pw->n; ++i) {
1701 pw->p[i].set = isl_set_preimage_multi_aff(pw->p[i].set,
1702 isl_multi_aff_copy(ma));
1705 pw->p[i].FIELD = FN(EL,pullback_multi_aff)(pw->p[i].FIELD,
1706 isl_multi_aff_copy(ma));
1707 if (!pw->p[i].FIELD)
1711 pw = FN(PW,reset_space)(pw, space);
1712 isl_multi_aff_free(ma);
1715 isl_space_free(space);
1716 isl_multi_aff_free(ma);
1721 __isl_give PW *FN(PW,pullback_multi_aff)(__isl_take PW *pw,
1722 __isl_take isl_multi_aff *ma)
1724 return FN(PW,align_params_pw_multi_aff_and)(pw, ma,
1725 &FN(PW,pullback_multi_aff_aligned));
1728 /* Compute the pullback of "pw" by the function represented by "pma".
1729 * In other words, plug in "pma" in "pw".
1731 static __isl_give PW *FN(PW,pullback_pw_multi_aff_aligned)(__isl_take PW *pw,
1732 __isl_take isl_pw_multi_aff *pma)
1741 isl_pw_multi_aff_free(pma);
1742 res = FN(PW,empty)(FN(PW,get_space)(pw));
1747 res = FN(PW,pullback_multi_aff)(FN(PW,copy)(pw),
1748 isl_multi_aff_copy(pma->p[0].maff));
1749 res = FN(PW,intersect_domain)(res, isl_set_copy(pma->p[0].set));
1751 for (i = 1; i < pma->n; ++i) {
1754 res_i = FN(PW,pullback_multi_aff)(FN(PW,copy)(pw),
1755 isl_multi_aff_copy(pma->p[i].maff));
1756 res_i = FN(PW,intersect_domain)(res_i,
1757 isl_set_copy(pma->p[i].set));
1758 res = FN(PW,add_disjoint)(res, res_i);
1761 isl_pw_multi_aff_free(pma);
1765 isl_pw_multi_aff_free(pma);
1770 __isl_give PW *FN(PW,pullback_pw_multi_aff)(__isl_take PW *pw,
1771 __isl_take isl_pw_multi_aff *pma)
1773 return FN(PW,align_params_pw_pw_multi_aff_and)(pw, pma,
1774 &FN(PW,pullback_pw_multi_aff_aligned));