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 __isl_give PW *FN(PW,alloc_size)(__isl_take isl_space *dim,
8 enum isl_fold type, int n)
10 __isl_give PW *FN(PW,alloc_size)(__isl_take isl_space *dim, int n)
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)));
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);
44 __isl_give PW *FN(PW,ZERO)(__isl_take isl_space *dim)
46 return FN(PW,alloc_size)(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_space *el_dim = NULL;
56 if (!pw || !set || !el)
59 if (isl_set_plain_is_empty(set) || FN(EL,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_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;
79 isl_space_free(el_dim);
82 isl_space_free(el_dim);
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_size)(FN(EL,get_space)(el), type, 1);
104 pw = FN(PW,alloc_size)(FN(EL,get_space)(el), 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_size)(isl_space_copy(pw->dim), pw->type, pw->n);
125 dup = FN(PW,alloc_size)(isl_space_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_space_free(pw->dim);
176 const char *FN(PW,get_dim_name)(__isl_keep PW *pw, enum isl_dim_type type,
179 return pw ? isl_space_get_dim_name(pw->dim, type, pos) : NULL;
182 int FN(PW,has_dim_id)(__isl_keep PW *pw, enum isl_dim_type type, unsigned pos)
184 return pw ? isl_space_has_dim_id(pw->dim, type, pos) : -1;
187 __isl_give isl_id *FN(PW,get_dim_id)(__isl_keep PW *pw, enum isl_dim_type type,
190 return pw ? isl_space_get_dim_id(pw->dim, type, pos) : NULL;
193 const char *FN(PW,get_tuple_name)(__isl_keep PW *pw, enum isl_dim_type type)
195 return pw ? isl_space_get_tuple_name(pw->dim, type) : NULL;
198 int FN(PW,has_tuple_id)(__isl_keep PW *pw, enum isl_dim_type type)
200 return pw ? isl_space_has_tuple_id(pw->dim, type) : -1;
203 __isl_give isl_id *FN(PW,get_tuple_id)(__isl_keep PW *pw, enum isl_dim_type type)
205 return pw ? isl_space_get_tuple_id(pw->dim, type) : NULL;
208 int FN(PW,IS_ZERO)(__isl_keep PW *pw)
217 __isl_give PW *FN(PW,realign_domain)(__isl_take PW *pw,
218 __isl_take isl_reordering *exp)
226 for (i = 0; i < pw->n; ++i) {
227 pw->p[i].set = isl_set_realign(pw->p[i].set,
228 isl_reordering_copy(exp));
231 pw->p[i].FIELD = FN(EL,realign_domain)(pw->p[i].FIELD,
232 isl_reordering_copy(exp));
237 pw = FN(PW,reset_domain_space)(pw, isl_space_copy(exp->dim));
239 isl_reordering_free(exp);
242 isl_reordering_free(exp);
247 /* Align the parameters of "pw" to those of "model".
249 __isl_give PW *FN(PW,align_params)(__isl_take PW *pw, __isl_take isl_space *model)
256 ctx = isl_space_get_ctx(model);
257 if (!isl_space_has_named_params(model))
258 isl_die(ctx, isl_error_invalid,
259 "model has unnamed parameters", goto error);
260 if (!isl_space_has_named_params(pw->dim))
261 isl_die(ctx, isl_error_invalid,
262 "input has unnamed parameters", goto error);
263 if (!isl_space_match(pw->dim, isl_dim_param, model, isl_dim_param)) {
266 model = isl_space_drop_dims(model, isl_dim_in,
267 0, isl_space_dim(model, isl_dim_in));
268 model = isl_space_drop_dims(model, isl_dim_out,
269 0, isl_space_dim(model, isl_dim_out));
270 exp = isl_parameter_alignment_reordering(pw->dim, model);
271 exp = isl_reordering_extend_space(exp,
272 FN(PW,get_domain_space)(pw));
273 pw = FN(PW,realign_domain)(pw, exp);
276 isl_space_free(model);
279 isl_space_free(model);
284 static __isl_give PW *FN(PW,align_params_pw_pw_and)(__isl_take PW *pw1,
286 __isl_give PW *(*fn)(__isl_take PW *pw1, __isl_take PW *pw2))
292 if (isl_space_match(pw1->dim, isl_dim_param, pw2->dim, isl_dim_param))
294 ctx = FN(PW,get_ctx)(pw1);
295 if (!isl_space_has_named_params(pw1->dim) ||
296 !isl_space_has_named_params(pw2->dim))
297 isl_die(ctx, isl_error_invalid,
298 "unaligned unnamed parameters", goto error);
299 pw1 = FN(PW,align_params)(pw1, FN(PW,get_space)(pw2));
300 pw2 = FN(PW,align_params)(pw2, FN(PW,get_space)(pw1));
308 static __isl_give PW *FN(PW,align_params_pw_set_and)(__isl_take PW *pw,
309 __isl_take isl_set *set,
310 __isl_give PW *(*fn)(__isl_take PW *pw, __isl_take isl_set *set))
316 if (isl_space_match(pw->dim, isl_dim_param, set->dim, isl_dim_param))
318 ctx = FN(PW,get_ctx)(pw);
319 if (!isl_space_has_named_params(pw->dim) ||
320 !isl_space_has_named_params(set->dim))
321 isl_die(ctx, isl_error_invalid,
322 "unaligned unnamed parameters", goto error);
323 pw = FN(PW,align_params)(pw, isl_set_get_space(set));
324 set = isl_set_align_params(set, FN(PW,get_space)(pw));
333 static __isl_give PW *FN(PW,union_add_aligned)(__isl_take PW *pw1,
344 ctx = isl_space_get_ctx(pw1->dim);
346 if (pw1->type != pw2->type)
347 isl_die(ctx, isl_error_invalid,
348 "fold types don't match", goto error);
350 isl_assert(ctx, isl_space_is_equal(pw1->dim, pw2->dim), goto error);
352 if (FN(PW,IS_ZERO)(pw1)) {
357 if (FN(PW,IS_ZERO)(pw2)) {
362 n = (pw1->n + 1) * (pw2->n + 1);
364 res = FN(PW,alloc_size)(isl_space_copy(pw1->dim), pw1->type, n);
366 res = FN(PW,alloc_size)(isl_space_copy(pw1->dim), n);
369 for (i = 0; i < pw1->n; ++i) {
370 set = isl_set_copy(pw1->p[i].set);
371 for (j = 0; j < pw2->n; ++j) {
372 struct isl_set *common;
374 common = isl_set_intersect(isl_set_copy(pw1->p[i].set),
375 isl_set_copy(pw2->p[j].set));
376 if (isl_set_plain_is_empty(common)) {
377 isl_set_free(common);
380 set = isl_set_subtract(set,
381 isl_set_copy(pw2->p[j].set));
383 sum = FN(EL,add_on_domain)(common,
384 FN(EL,copy)(pw1->p[i].FIELD),
385 FN(EL,copy)(pw2->p[j].FIELD));
387 res = FN(PW,add_piece)(res, common, sum);
389 res = FN(PW,add_piece)(res, set, FN(EL,copy)(pw1->p[i].FIELD));
392 for (j = 0; j < pw2->n; ++j) {
393 set = isl_set_copy(pw2->p[j].set);
394 for (i = 0; i < pw1->n; ++i)
395 set = isl_set_subtract(set,
396 isl_set_copy(pw1->p[i].set));
397 res = FN(PW,add_piece)(res, set, FN(EL,copy)(pw2->p[j].FIELD));
410 /* Private version of "union_add". For isl_pw_qpolynomial and
411 * isl_pw_qpolynomial_fold, we prefer to simply call it "add".
413 static __isl_give PW *FN(PW,union_add_)(__isl_take PW *pw1, __isl_take PW *pw2)
415 return FN(PW,align_params_pw_pw_and)(pw1, pw2,
416 &FN(PW,union_add_aligned));
419 /* Make sure "pw" has room for at least "n" more pieces.
421 * If there is only one reference to pw, we extend it in place.
422 * Otherwise, we create a new PW and copy the pieces.
424 static __isl_give PW *FN(PW,grow)(__isl_take PW *pw, int n)
432 if (pw->n + n <= pw->size)
434 ctx = FN(PW,get_ctx)(pw);
437 res = isl_realloc(ctx, pw, struct PW,
438 sizeof(struct PW) + (n - 1) * sizeof(S(PW,piece)));
440 return FN(PW,free)(pw);
445 res = FN(PW,alloc_size)(isl_space_copy(pw->dim), pw->type, n);
447 res = FN(PW,alloc_size)(isl_space_copy(pw->dim), n);
450 return FN(PW,free)(pw);
451 for (i = 0; i < pw->n; ++i)
452 res = FN(PW,add_piece)(res, isl_set_copy(pw->p[i].set),
453 FN(EL,copy)(pw->p[i].FIELD));
458 static __isl_give PW *FN(PW,add_disjoint_aligned)(__isl_take PW *pw1,
467 if (pw1->size < pw1->n + pw2->n && pw1->n < pw2->n)
468 return FN(PW,add_disjoint_aligned)(pw2, pw1);
470 ctx = isl_space_get_ctx(pw1->dim);
472 if (pw1->type != pw2->type)
473 isl_die(ctx, isl_error_invalid,
474 "fold types don't match", goto error);
476 isl_assert(ctx, isl_space_is_equal(pw1->dim, pw2->dim), goto error);
478 if (FN(PW,IS_ZERO)(pw1)) {
483 if (FN(PW,IS_ZERO)(pw2)) {
488 pw1 = FN(PW,grow)(pw1, pw2->n);
492 for (i = 0; i < pw2->n; ++i)
493 pw1 = FN(PW,add_piece)(pw1,
494 isl_set_copy(pw2->p[i].set),
495 FN(EL,copy)(pw2->p[i].FIELD));
506 __isl_give PW *FN(PW,add_disjoint)(__isl_take PW *pw1, __isl_take PW *pw2)
508 return FN(PW,align_params_pw_pw_and)(pw1, pw2,
509 &FN(PW,add_disjoint_aligned));
512 /* This function is currently only used from isl_aff.c
514 static __isl_give PW *FN(PW,on_shared_domain)(__isl_take PW *pw1,
516 __isl_give EL *(*fn)(__isl_take EL *el1, __isl_take EL *el2))
517 __attribute__ ((unused));
519 /* Apply "fn" to pairs of elements from pw1 and pw2 on shared domains.
521 static __isl_give PW *FN(PW,on_shared_domain)(__isl_take PW *pw1,
523 __isl_give EL *(*fn)(__isl_take EL *el1, __isl_take EL *el2))
533 res = FN(PW,alloc_size)(isl_space_copy(pw1->dim), pw1->type, n);
535 res = FN(PW,alloc_size)(isl_space_copy(pw1->dim), n);
538 for (i = 0; i < pw1->n; ++i) {
539 for (j = 0; j < pw2->n; ++j) {
544 common = isl_set_intersect(
545 isl_set_copy(pw1->p[i].set),
546 isl_set_copy(pw2->p[j].set));
547 empty = isl_set_plain_is_empty(common);
548 if (empty < 0 || empty) {
549 isl_set_free(common);
555 res_ij = fn(FN(EL,copy)(pw1->p[i].FIELD),
556 FN(EL,copy)(pw2->p[j].FIELD));
557 res_ij = FN(EL,gist)(res_ij, isl_set_copy(common));
559 res = FN(PW,add_piece)(res, common, res_ij);
574 __isl_give PW *FN(PW,neg)(__isl_take PW *pw)
581 if (FN(PW,IS_ZERO)(pw))
588 for (i = 0; i < pw->n; ++i) {
589 pw->p[i].FIELD = FN(EL,neg)(pw->p[i].FIELD);
591 return FN(PW,free)(pw);
597 __isl_give PW *FN(PW,sub)(__isl_take PW *pw1, __isl_take PW *pw2)
599 return FN(PW,add)(pw1, FN(PW,neg)(pw2));
604 __isl_give isl_qpolynomial *FN(PW,eval)(__isl_take PW *pw,
605 __isl_take isl_point *pnt)
610 isl_space *pnt_dim = NULL;
615 ctx = isl_point_get_ctx(pnt);
616 pnt_dim = isl_point_get_space(pnt);
617 isl_assert(ctx, isl_space_is_domain(pnt_dim, pw->dim), goto error);
619 for (i = 0; i < pw->n; ++i) {
620 found = isl_set_contains_point(pw->p[i].set, pnt);
627 qp = FN(EL,eval)(FN(EL,copy)(pw->p[i].FIELD),
628 isl_point_copy(pnt));
630 qp = isl_qpolynomial_zero_on_domain(FN(PW,get_domain_space)(pw));
632 isl_space_free(pnt_dim);
637 isl_space_free(pnt_dim);
643 __isl_give isl_set *FN(PW,domain)(__isl_take PW *pw)
651 dom = isl_set_empty(FN(PW,get_domain_space)(pw));
652 for (i = 0; i < pw->n; ++i)
653 dom = isl_set_union_disjoint(dom, isl_set_copy(pw->p[i].set));
660 /* Restrict the domain of "pw" by combining each cell
661 * with "set" through a call to "fn", where "fn" may be
662 * isl_set_intersect or isl_set_intersect_params.
664 static __isl_give PW *FN(PW,intersect_aligned)(__isl_take PW *pw,
665 __isl_take isl_set *set,
666 __isl_give isl_set *(*fn)(__isl_take isl_set *set1,
667 __isl_take isl_set *set2))
683 for (i = pw->n - 1; i >= 0; --i) {
685 pw->p[i].set = fn(pw->p[i].set, isl_set_copy(set));
688 aff = isl_set_affine_hull(isl_set_copy(pw->p[i].set));
689 pw->p[i].FIELD = FN(EL,substitute_equalities)(pw->p[i].FIELD,
693 if (isl_set_plain_is_empty(pw->p[i].set)) {
694 isl_set_free(pw->p[i].set);
695 FN(EL,free)(pw->p[i].FIELD);
697 pw->p[i] = pw->p[pw->n - 1];
710 static __isl_give PW *FN(PW,intersect_domain_aligned)(__isl_take PW *pw,
711 __isl_take isl_set *set)
713 return FN(PW,intersect_aligned)(pw, set, &isl_set_intersect);
716 __isl_give PW *FN(PW,intersect_domain)(__isl_take PW *pw,
717 __isl_take isl_set *context)
719 return FN(PW,align_params_pw_set_and)(pw, context,
720 &FN(PW,intersect_domain_aligned));
723 static __isl_give PW *FN(PW,intersect_params_aligned)(__isl_take PW *pw,
724 __isl_take isl_set *set)
726 return FN(PW,intersect_aligned)(pw, set, &isl_set_intersect_params);
729 /* Intersect the domain of "pw" with the parameter domain "context".
731 __isl_give PW *FN(PW,intersect_params)(__isl_take PW *pw,
732 __isl_take isl_set *context)
734 return FN(PW,align_params_pw_set_and)(pw, context,
735 &FN(PW,intersect_params_aligned));
738 static __isl_give PW *FN(PW,gist_aligned)(__isl_take PW *pw,
739 __isl_take isl_set *context,
740 __isl_give EL *(*fn_el)(__isl_take EL *el,
741 __isl_take isl_set *set),
742 __isl_give isl_set *(*fn_dom)(__isl_take isl_set *set,
743 __isl_take isl_basic_set *bset))
746 isl_basic_set *hull = NULL;
752 isl_set_free(context);
756 if (!isl_space_match(pw->dim, isl_dim_param,
757 context->dim, isl_dim_param)) {
758 pw = FN(PW,align_params)(pw, isl_set_get_space(context));
759 context = isl_set_align_params(context, FN(PW,get_space)(pw));
762 context = isl_set_compute_divs(context);
763 hull = isl_set_simple_hull(isl_set_copy(context));
769 for (i = pw->n - 1; i >= 0; --i) {
770 pw->p[i].set = isl_set_intersect(pw->p[i].set,
771 isl_set_copy(context));
774 pw->p[i].FIELD = fn_el(pw->p[i].FIELD,
775 isl_set_copy(pw->p[i].set));
776 pw->p[i].set = fn_dom(pw->p[i].set, isl_basic_set_copy(hull));
779 if (isl_set_plain_is_empty(pw->p[i].set)) {
780 isl_set_free(pw->p[i].set);
781 FN(EL,free)(pw->p[i].FIELD);
783 pw->p[i] = pw->p[pw->n - 1];
788 isl_basic_set_free(hull);
789 isl_set_free(context);
794 isl_basic_set_free(hull);
795 isl_set_free(context);
799 static __isl_give PW *FN(PW,gist_domain_aligned)(__isl_take PW *pw,
800 __isl_take isl_set *set)
802 return FN(PW,gist_aligned)(pw, set, &FN(EL,gist),
803 &isl_set_gist_basic_set);
806 __isl_give PW *FN(PW,gist)(__isl_take PW *pw, __isl_take isl_set *context)
808 return FN(PW,align_params_pw_set_and)(pw, context,
809 &FN(PW,gist_domain_aligned));
812 static __isl_give PW *FN(PW,gist_params_aligned)(__isl_take PW *pw,
813 __isl_take isl_set *set)
815 return FN(PW,gist_aligned)(pw, set, &FN(EL,gist_params),
816 &isl_set_gist_params_basic_set);
819 __isl_give PW *FN(PW,gist_params)(__isl_take PW *pw,
820 __isl_take isl_set *context)
822 return FN(PW,align_params_pw_set_and)(pw, context,
823 &FN(PW,gist_params_aligned));
826 __isl_give PW *FN(PW,coalesce)(__isl_take PW *pw)
835 for (i = pw->n - 1; i >= 0; --i) {
836 for (j = i - 1; j >= 0; --j) {
837 if (!FN(EL,plain_is_equal)(pw->p[i].FIELD,
840 pw->p[j].set = isl_set_union(pw->p[j].set,
842 FN(EL,free)(pw->p[i].FIELD);
844 pw->p[i] = pw->p[pw->n - 1];
850 pw->p[i].set = isl_set_coalesce(pw->p[i].set);
861 isl_ctx *FN(PW,get_ctx)(__isl_keep PW *pw)
863 return pw ? isl_space_get_ctx(pw->dim) : NULL;
866 #ifndef NO_INVOLVES_DIMS
867 int FN(PW,involves_dims)(__isl_keep PW *pw, enum isl_dim_type type,
868 unsigned first, unsigned n)
871 enum isl_dim_type set_type;
875 if (pw->n == 0 || n == 0)
878 set_type = type == isl_dim_in ? isl_dim_set : type;
880 for (i = 0; i < pw->n; ++i) {
881 int involves = FN(EL,involves_dims)(pw->p[i].FIELD,
883 if (involves < 0 || involves)
885 involves = isl_set_involves_dims(pw->p[i].set,
887 if (involves < 0 || involves)
894 __isl_give PW *FN(PW,set_dim_name)(__isl_take PW *pw,
895 enum isl_dim_type type, unsigned pos, const char *s)
898 enum isl_dim_type set_type;
904 set_type = type == isl_dim_in ? isl_dim_set : type;
906 pw->dim = isl_space_set_dim_name(pw->dim, type, pos, s);
910 for (i = 0; i < pw->n; ++i) {
911 pw->p[i].set = isl_set_set_dim_name(pw->p[i].set,
915 pw->p[i].FIELD = FN(EL,set_dim_name)(pw->p[i].FIELD, type, pos, s);
927 __isl_give PW *FN(PW,drop_dims)(__isl_take PW *pw,
928 enum isl_dim_type type, unsigned first, unsigned n)
931 enum isl_dim_type set_type;
935 if (n == 0 && !isl_space_get_tuple_name(pw->dim, type))
938 set_type = type == isl_dim_in ? isl_dim_set : type;
943 pw->dim = isl_space_drop_dims(pw->dim, type, first, n);
946 for (i = 0; i < pw->n; ++i) {
947 pw->p[i].set = isl_set_drop(pw->p[i].set, set_type, first, n);
950 pw->p[i].FIELD = FN(EL,drop_dims)(pw->p[i].FIELD, type, first, n);
961 /* This function is very similar to drop_dims.
962 * The only difference is that the cells may still involve
963 * the specified dimensions. They are removed using
964 * isl_set_project_out instead of isl_set_drop.
966 __isl_give PW *FN(PW,project_out)(__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_project_out(pw->p[i].set,
990 pw->p[i].FIELD = FN(EL,drop_dims)(pw->p[i].FIELD, type, first, n);
1001 /* Project the domain of pw onto its parameter space.
1003 __isl_give PW *FN(PW,project_domain_on_params)(__isl_take PW *pw)
1008 n = FN(PW,dim)(pw, isl_dim_in);
1009 pw = FN(PW,project_out)(pw, isl_dim_in, 0, n);
1010 space = FN(PW,get_domain_space)(pw);
1011 space = isl_space_params(space);
1012 pw = FN(PW,reset_domain_space)(pw, space);
1017 #ifndef NO_INSERT_DIMS
1018 __isl_give PW *FN(PW,insert_dims)(__isl_take PW *pw, enum isl_dim_type type,
1019 unsigned first, unsigned n)
1022 enum isl_dim_type set_type;
1026 if (n == 0 && !isl_space_is_named_or_nested(pw->dim, type))
1029 set_type = type == isl_dim_in ? isl_dim_set : type;
1031 pw = FN(PW,cow)(pw);
1035 pw->dim = isl_space_insert_dims(pw->dim, type, first, n);
1039 for (i = 0; i < pw->n; ++i) {
1040 pw->p[i].set = isl_set_insert_dims(pw->p[i].set,
1041 set_type, first, n);
1044 pw->p[i].FIELD = FN(EL,insert_dims)(pw->p[i].FIELD,
1046 if (!pw->p[i].FIELD)
1057 __isl_give PW *FN(PW,fix_dim)(__isl_take PW *pw,
1058 enum isl_dim_type type, unsigned pos, isl_int v)
1065 if (type == isl_dim_in)
1068 pw = FN(PW,cow)(pw);
1071 for (i = 0; i < pw->n; ++i) {
1072 pw->p[i].set = isl_set_fix(pw->p[i].set, type, pos, v);
1083 unsigned FN(PW,dim)(__isl_keep PW *pw, enum isl_dim_type type)
1085 return pw ? isl_space_dim(pw->dim, type) : 0;
1088 __isl_give PW *FN(PW,split_dims)(__isl_take PW *pw,
1089 enum isl_dim_type type, unsigned first, unsigned n)
1098 if (type == isl_dim_in)
1101 pw = FN(PW,cow)(pw);
1106 for (i = 0; i < pw->n; ++i) {
1107 pw->p[i].set = isl_set_split_dims(pw->p[i].set, type, first, n);
1119 /* Compute the maximal value attained by the piecewise quasipolynomial
1120 * on its domain or zero if the domain is empty.
1121 * In the worst case, the domain is scanned completely,
1122 * so the domain is assumed to be bounded.
1124 __isl_give isl_qpolynomial *FN(PW,opt)(__isl_take PW *pw, int max)
1127 isl_qpolynomial *opt;
1133 isl_space *dim = isl_space_copy(pw->dim);
1135 return isl_qpolynomial_zero_on_domain(isl_space_domain(dim));
1138 opt = FN(EL,opt_on_domain)(FN(EL,copy)(pw->p[0].FIELD),
1139 isl_set_copy(pw->p[0].set), max);
1140 for (i = 1; i < pw->n; ++i) {
1141 isl_qpolynomial *opt_i;
1142 opt_i = FN(EL,opt_on_domain)(FN(EL,copy)(pw->p[i].FIELD),
1143 isl_set_copy(pw->p[i].set), max);
1145 opt = isl_qpolynomial_max_cst(opt, opt_i);
1147 opt = isl_qpolynomial_min_cst(opt, opt_i);
1154 __isl_give isl_qpolynomial *FN(PW,max)(__isl_take PW *pw)
1156 return FN(PW,opt)(pw, 1);
1159 __isl_give isl_qpolynomial *FN(PW,min)(__isl_take PW *pw)
1161 return FN(PW,opt)(pw, 0);
1165 __isl_give isl_space *FN(PW,get_space)(__isl_keep PW *pw)
1167 return pw ? isl_space_copy(pw->dim) : NULL;
1170 __isl_give isl_space *FN(PW,get_domain_space)(__isl_keep PW *pw)
1172 return pw ? isl_space_domain(isl_space_copy(pw->dim)) : NULL;
1175 #ifndef NO_RESET_DIM
1176 /* Reset the space of "pw". Since we don't know if the elements
1177 * represent the spaces themselves or their domains, we pass along
1178 * both when we call their reset_space_and_domain.
1180 static __isl_give PW *FN(PW,reset_space_and_domain)(__isl_take PW *pw,
1181 __isl_take isl_space *space, __isl_take isl_space *domain)
1185 pw = FN(PW,cow)(pw);
1186 if (!pw || !space || !domain)
1189 for (i = 0; i < pw->n; ++i) {
1190 pw->p[i].set = isl_set_reset_space(pw->p[i].set,
1191 isl_space_copy(domain));
1194 pw->p[i].FIELD = FN(EL,reset_space_and_domain)(pw->p[i].FIELD,
1195 isl_space_copy(space), isl_space_copy(domain));
1196 if (!pw->p[i].FIELD)
1200 isl_space_free(domain);
1202 isl_space_free(pw->dim);
1207 isl_space_free(domain);
1208 isl_space_free(space);
1213 __isl_give PW *FN(PW,reset_domain_space)(__isl_take PW *pw,
1214 __isl_take isl_space *domain)
1218 space = isl_space_extend_domain_with_range(isl_space_copy(domain),
1219 FN(PW,get_space)(pw));
1220 return FN(PW,reset_space_and_domain)(pw, space, domain);
1223 __isl_give PW *FN(PW,reset_space)(__isl_take PW *pw, __isl_take isl_space *dim)
1227 domain = isl_space_domain(isl_space_copy(dim));
1228 return FN(PW,reset_space_and_domain)(pw, dim, domain);
1231 __isl_give PW *FN(PW,set_tuple_id)(__isl_keep PW *pw, enum isl_dim_type type,
1232 __isl_take isl_id *id)
1236 pw = FN(PW,cow)(pw);
1238 return isl_id_free(id);
1240 space = FN(PW,get_space)(pw);
1241 space = isl_space_set_tuple_id(space, type, id);
1243 return FN(PW,reset_space)(pw, space);
1246 __isl_give PW *FN(PW,set_dim_id)(__isl_take PW *pw,
1247 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
1249 pw = FN(PW,cow)(pw);
1251 return isl_id_free(id);
1252 pw->dim = isl_space_set_dim_id(pw->dim, type, pos, id);
1253 return FN(PW,reset_space)(pw, isl_space_copy(pw->dim));
1257 int FN(PW,has_equal_space)(__isl_keep PW *pw1, __isl_keep PW *pw2)
1262 return isl_space_is_equal(pw1->dim, pw2->dim);
1266 __isl_give PW *FN(PW,morph_domain)(__isl_take PW *pw,
1267 __isl_take isl_morph *morph)
1275 ctx = isl_space_get_ctx(pw->dim);
1276 isl_assert(ctx, isl_space_is_domain(morph->dom->dim, pw->dim),
1279 pw = FN(PW,cow)(pw);
1282 pw->dim = isl_space_extend_domain_with_range(
1283 isl_space_copy(morph->ran->dim), pw->dim);
1287 for (i = 0; i < pw->n; ++i) {
1288 pw->p[i].set = isl_morph_set(isl_morph_copy(morph), pw->p[i].set);
1291 pw->p[i].FIELD = FN(EL,morph_domain)(pw->p[i].FIELD,
1292 isl_morph_copy(morph));
1293 if (!pw->p[i].FIELD)
1297 isl_morph_free(morph);
1302 isl_morph_free(morph);
1307 int FN(PW,n_piece)(__isl_keep PW *pw)
1309 return pw ? pw->n : 0;
1312 int FN(PW,foreach_piece)(__isl_keep PW *pw,
1313 int (*fn)(__isl_take isl_set *set, __isl_take EL *el, void *user),
1321 for (i = 0; i < pw->n; ++i)
1322 if (fn(isl_set_copy(pw->p[i].set),
1323 FN(EL,copy)(pw->p[i].FIELD), user) < 0)
1330 static int any_divs(__isl_keep isl_set *set)
1337 for (i = 0; i < set->n; ++i)
1338 if (set->p[i]->n_div > 0)
1344 static int foreach_lifted_subset(__isl_take isl_set *set, __isl_take EL *el,
1345 int (*fn)(__isl_take isl_set *set, __isl_take EL *el,
1346 void *user), void *user)
1353 for (i = 0; i < set->n; ++i) {
1357 lift = isl_set_from_basic_set(isl_basic_set_copy(set->p[i]));
1358 lift = isl_set_lift(lift);
1360 copy = FN(EL,copy)(el);
1361 copy = FN(EL,lift)(copy, isl_set_get_space(lift));
1363 if (fn(lift, copy, user) < 0)
1377 int FN(PW,foreach_lifted_piece)(__isl_keep PW *pw,
1378 int (*fn)(__isl_take isl_set *set, __isl_take EL *el,
1379 void *user), void *user)
1386 for (i = 0; i < pw->n; ++i) {
1390 set = isl_set_copy(pw->p[i].set);
1391 el = FN(EL,copy)(pw->p[i].FIELD);
1392 if (!any_divs(set)) {
1393 if (fn(set, el, user) < 0)
1397 if (foreach_lifted_subset(set, el, fn, user) < 0)
1405 #ifndef NO_MOVE_DIMS
1406 __isl_give PW *FN(PW,move_dims)(__isl_take PW *pw,
1407 enum isl_dim_type dst_type, unsigned dst_pos,
1408 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
1412 pw = FN(PW,cow)(pw);
1416 pw->dim = isl_space_move_dims(pw->dim, dst_type, dst_pos, src_type, src_pos, n);
1420 for (i = 0; i < pw->n; ++i) {
1421 pw->p[i].FIELD = FN(EL,move_dims)(pw->p[i].FIELD,
1422 dst_type, dst_pos, src_type, src_pos, n);
1423 if (!pw->p[i].FIELD)
1427 if (dst_type == isl_dim_in)
1428 dst_type = isl_dim_set;
1429 if (src_type == isl_dim_in)
1430 src_type = isl_dim_set;
1432 for (i = 0; i < pw->n; ++i) {
1433 pw->p[i].set = isl_set_move_dims(pw->p[i].set,
1435 src_type, src_pos, n);
1447 __isl_give PW *FN(PW,mul_isl_int)(__isl_take PW *pw, isl_int v)
1451 if (isl_int_is_one(v))
1453 if (pw && DEFAULT_IS_ZERO && isl_int_is_zero(v)) {
1455 isl_space *dim = FN(PW,get_space)(pw);
1457 zero = FN(PW,ZERO)(dim, pw->type);
1459 zero = FN(PW,ZERO)(dim);
1464 pw = FN(PW,cow)(pw);
1471 if (isl_int_is_neg(v))
1472 pw->type = isl_fold_type_negate(pw->type);
1474 for (i = 0; i < pw->n; ++i) {
1475 pw->p[i].FIELD = FN(EL,scale)(pw->p[i].FIELD, v);
1476 if (!pw->p[i].FIELD)
1486 __isl_give PW *FN(PW,scale)(__isl_take PW *pw, isl_int v)
1488 return FN(PW,mul_isl_int)(pw, v);
1491 static int FN(PW,qsort_set_cmp)(const void *p1, const void *p2)
1493 const isl_set *set1 = *(const isl_set **)p1;
1494 const isl_set *set2 = *(const isl_set **)p2;
1496 return isl_set_plain_cmp(set1, set2);
1499 /* We normalize in place, but if anything goes wrong we need
1500 * to return NULL, so we need to make sure we don't change the
1501 * meaning of any possible other copies of map.
1503 __isl_give PW *FN(PW,normalize)(__isl_take PW *pw)
1510 for (i = 0; i < pw->n; ++i) {
1511 set = isl_set_normalize(isl_set_copy(pw->p[i].set));
1513 return FN(PW,free)(pw);
1514 isl_set_free(pw->p[i].set);
1517 qsort(pw->p, pw->n, sizeof(pw->p[0]), &FN(PW,qsort_set_cmp));
1518 for (i = pw->n - 1; i >= 1; --i) {
1519 if (!isl_set_plain_is_equal(pw->p[i - 1].set, pw->p[i].set))
1521 if (!FN(EL,plain_is_equal)(pw->p[i - 1].FIELD, pw->p[i].FIELD))
1523 set = isl_set_union(isl_set_copy(pw->p[i - 1].set),
1524 isl_set_copy(pw->p[i].set));
1526 return FN(PW,free)(pw);
1527 isl_set_free(pw->p[i].set);
1528 FN(EL,free)(pw->p[i].FIELD);
1529 isl_set_free(pw->p[i - 1].set);
1530 pw->p[i - 1].set = set;
1531 for (j = i + 1; j < pw->n; ++j)
1532 pw->p[j - 1] = pw->p[j];
1539 /* Is pw1 obviously equal to pw2?
1540 * That is, do they have obviously identical cells and obviously identical
1541 * elements on each cell?
1543 int FN(PW,plain_is_equal)(__isl_keep PW *pw1, __isl_keep PW *pw2)
1553 if (!isl_space_is_equal(pw1->dim, pw2->dim))
1556 pw1 = FN(PW,copy)(pw1);
1557 pw2 = FN(PW,copy)(pw2);
1558 pw1 = FN(PW,normalize)(pw1);
1559 pw2 = FN(PW,normalize)(pw2);
1563 equal = pw1->n == pw2->n;
1564 for (i = 0; equal && i < pw1->n; ++i) {
1565 equal = isl_set_plain_is_equal(pw1->p[i].set, pw2->p[i].set);
1570 equal = FN(EL,plain_is_equal)(pw1->p[i].FIELD, pw2->p[i].FIELD);