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_tuple_name)(__isl_keep PW *pw, enum isl_dim_type type)
178 return pw ? isl_space_get_tuple_name(pw->dim, type) : NULL;
181 __isl_give isl_id *FN(PW,get_tuple_id)(__isl_keep PW *pw, enum isl_dim_type type)
183 return pw ? isl_space_get_tuple_id(pw->dim, type) : NULL;
186 int FN(PW,IS_ZERO)(__isl_keep PW *pw)
195 __isl_give PW *FN(PW,realign_domain)(__isl_take PW *pw,
196 __isl_take isl_reordering *exp)
204 for (i = 0; i < pw->n; ++i) {
205 pw->p[i].set = isl_set_realign(pw->p[i].set,
206 isl_reordering_copy(exp));
209 pw->p[i].FIELD = FN(EL,realign_domain)(pw->p[i].FIELD,
210 isl_reordering_copy(exp));
215 pw = FN(PW,reset_domain_space)(pw, isl_space_copy(exp->dim));
217 isl_reordering_free(exp);
220 isl_reordering_free(exp);
225 /* Align the parameters of "pw" to those of "model".
227 __isl_give PW *FN(PW,align_params)(__isl_take PW *pw, __isl_take isl_space *model)
234 ctx = isl_space_get_ctx(model);
235 if (!isl_space_has_named_params(model))
236 isl_die(ctx, isl_error_invalid,
237 "model has unnamed parameters", goto error);
238 if (!isl_space_has_named_params(pw->dim))
239 isl_die(ctx, isl_error_invalid,
240 "input has unnamed parameters", goto error);
241 if (!isl_space_match(pw->dim, isl_dim_param, model, isl_dim_param)) {
244 model = isl_space_drop_dims(model, isl_dim_in,
245 0, isl_space_dim(model, isl_dim_in));
246 model = isl_space_drop_dims(model, isl_dim_out,
247 0, isl_space_dim(model, isl_dim_out));
248 exp = isl_parameter_alignment_reordering(pw->dim, model);
249 exp = isl_reordering_extend_space(exp,
250 FN(PW,get_domain_space)(pw));
251 pw = FN(PW,realign_domain)(pw, exp);
254 isl_space_free(model);
257 isl_space_free(model);
262 static __isl_give PW *FN(PW,align_params_pw_pw_and)(__isl_take PW *pw1,
264 __isl_give PW *(*fn)(__isl_take PW *pw1, __isl_take PW *pw2))
270 if (isl_space_match(pw1->dim, isl_dim_param, pw2->dim, isl_dim_param))
272 ctx = FN(PW,get_ctx)(pw1);
273 if (!isl_space_has_named_params(pw1->dim) ||
274 !isl_space_has_named_params(pw2->dim))
275 isl_die(ctx, isl_error_invalid,
276 "unaligned unnamed parameters", goto error);
277 pw1 = FN(PW,align_params)(pw1, FN(PW,get_space)(pw2));
278 pw2 = FN(PW,align_params)(pw2, FN(PW,get_space)(pw1));
286 static __isl_give PW *FN(PW,align_params_pw_set_and)(__isl_take PW *pw,
287 __isl_take isl_set *set,
288 __isl_give PW *(*fn)(__isl_take PW *pw, __isl_take isl_set *set))
294 if (isl_space_match(pw->dim, isl_dim_param, set->dim, isl_dim_param))
296 ctx = FN(PW,get_ctx)(pw);
297 if (!isl_space_has_named_params(pw->dim) ||
298 !isl_space_has_named_params(set->dim))
299 isl_die(ctx, isl_error_invalid,
300 "unaligned unnamed parameters", goto error);
301 pw = FN(PW,align_params)(pw, isl_set_get_space(set));
302 set = isl_set_align_params(set, FN(PW,get_space)(pw));
311 static __isl_give PW *FN(PW,add_aligned)(__isl_take PW *pw1, __isl_take PW *pw2)
321 ctx = isl_space_get_ctx(pw1->dim);
323 if (pw1->type != pw2->type)
324 isl_die(ctx, isl_error_invalid,
325 "fold types don't match", goto error);
327 isl_assert(ctx, isl_space_is_equal(pw1->dim, pw2->dim), goto error);
329 if (FN(PW,IS_ZERO)(pw1)) {
334 if (FN(PW,IS_ZERO)(pw2)) {
339 n = (pw1->n + 1) * (pw2->n + 1);
341 res = FN(PW,alloc_size)(isl_space_copy(pw1->dim), pw1->type, n);
343 res = FN(PW,alloc_size)(isl_space_copy(pw1->dim), n);
346 for (i = 0; i < pw1->n; ++i) {
347 set = isl_set_copy(pw1->p[i].set);
348 for (j = 0; j < pw2->n; ++j) {
349 struct isl_set *common;
351 set = isl_set_subtract(set,
352 isl_set_copy(pw2->p[j].set));
353 common = isl_set_intersect(isl_set_copy(pw1->p[i].set),
354 isl_set_copy(pw2->p[j].set));
355 if (isl_set_plain_is_empty(common)) {
356 isl_set_free(common);
360 sum = FN(EL,add_on_domain)(common,
361 FN(EL,copy)(pw1->p[i].FIELD),
362 FN(EL,copy)(pw2->p[j].FIELD));
364 res = FN(PW,add_piece)(res, common, sum);
366 res = FN(PW,add_piece)(res, set, FN(EL,copy)(pw1->p[i].FIELD));
369 for (j = 0; j < pw2->n; ++j) {
370 set = isl_set_copy(pw2->p[j].set);
371 for (i = 0; i < pw1->n; ++i)
372 set = isl_set_subtract(set,
373 isl_set_copy(pw1->p[i].set));
374 res = FN(PW,add_piece)(res, set, FN(EL,copy)(pw2->p[j].FIELD));
387 __isl_give PW *FN(PW,add)(__isl_take PW *pw1, __isl_take PW *pw2)
389 return FN(PW,align_params_pw_pw_and)(pw1, pw2, &FN(PW,add_aligned));
392 /* Make sure "pw" has room for at least "n" more pieces.
394 * If there is only one reference to pw, we extend it in place.
395 * Otherwise, we create a new PW and copy the pieces.
397 static __isl_give PW *FN(PW,grow)(__isl_take PW *pw, int n)
405 if (pw->n + n <= pw->size)
407 ctx = FN(PW,get_ctx)(pw);
410 res = isl_realloc(ctx, pw, struct PW,
411 sizeof(struct PW) + (n - 1) * sizeof(S(PW,piece)));
413 return FN(PW,free)(pw);
418 res = FN(PW,alloc_size)(isl_space_copy(pw->dim), pw->type, n);
420 res = FN(PW,alloc_size)(isl_space_copy(pw->dim), n);
423 return FN(PW,free)(pw);
424 for (i = 0; i < pw->n; ++i)
425 res = FN(PW,add_piece)(res, isl_set_copy(pw->p[i].set),
426 FN(EL,copy)(pw->p[i].FIELD));
431 static __isl_give PW *FN(PW,add_disjoint_aligned)(__isl_take PW *pw1,
440 if (pw1->size < pw1->n + pw2->n && pw1->n < pw2->n)
441 return FN(PW,add_disjoint_aligned)(pw2, pw1);
443 ctx = isl_space_get_ctx(pw1->dim);
445 if (pw1->type != pw2->type)
446 isl_die(ctx, isl_error_invalid,
447 "fold types don't match", goto error);
449 isl_assert(ctx, isl_space_is_equal(pw1->dim, pw2->dim), goto error);
451 if (FN(PW,IS_ZERO)(pw1)) {
456 if (FN(PW,IS_ZERO)(pw2)) {
461 pw1 = FN(PW,grow)(pw1, pw2->n);
465 for (i = 0; i < pw2->n; ++i)
466 pw1 = FN(PW,add_piece)(pw1,
467 isl_set_copy(pw2->p[i].set),
468 FN(EL,copy)(pw2->p[i].FIELD));
479 __isl_give PW *FN(PW,add_disjoint)(__isl_take PW *pw1, __isl_take PW *pw2)
481 return FN(PW,align_params_pw_pw_and)(pw1, pw2,
482 &FN(PW,add_disjoint_aligned));
486 __isl_give PW *FN(PW,neg)(__isl_take PW *pw)
493 if (FN(PW,IS_ZERO)(pw))
500 for (i = 0; i < pw->n; ++i) {
501 pw->p[i].FIELD = FN(EL,neg)(pw->p[i].FIELD);
503 return FN(PW,free)(pw);
509 __isl_give PW *FN(PW,sub)(__isl_take PW *pw1, __isl_take PW *pw2)
511 return FN(PW,add)(pw1, FN(PW,neg)(pw2));
516 __isl_give isl_qpolynomial *FN(PW,eval)(__isl_take PW *pw,
517 __isl_take isl_point *pnt)
522 isl_space *pnt_dim = NULL;
527 ctx = isl_point_get_ctx(pnt);
528 pnt_dim = isl_point_get_space(pnt);
529 isl_assert(ctx, isl_space_is_domain(pnt_dim, pw->dim), goto error);
531 for (i = 0; i < pw->n; ++i) {
532 found = isl_set_contains_point(pw->p[i].set, pnt);
539 qp = FN(EL,eval)(FN(EL,copy)(pw->p[i].FIELD),
540 isl_point_copy(pnt));
542 qp = isl_qpolynomial_zero_on_domain(FN(PW,get_domain_space)(pw));
544 isl_space_free(pnt_dim);
549 isl_space_free(pnt_dim);
555 __isl_give isl_set *FN(PW,domain)(__isl_take PW *pw)
563 dom = isl_set_empty(FN(PW,get_domain_space)(pw));
564 for (i = 0; i < pw->n; ++i)
565 dom = isl_set_union_disjoint(dom, isl_set_copy(pw->p[i].set));
572 static __isl_give PW *FN(PW,intersect_domain_aligned)(__isl_take PW *pw,
573 __isl_take isl_set *set)
589 for (i = pw->n - 1; i >= 0; --i) {
591 pw->p[i].set = isl_set_intersect(pw->p[i].set, isl_set_copy(set));
594 aff = isl_set_affine_hull(isl_set_copy(pw->p[i].set));
595 pw->p[i].FIELD = FN(EL,substitute_equalities)(pw->p[i].FIELD,
597 if (isl_set_plain_is_empty(pw->p[i].set)) {
598 isl_set_free(pw->p[i].set);
599 FN(EL,free)(pw->p[i].FIELD);
601 pw->p[i] = pw->p[pw->n - 1];
614 __isl_give PW *FN(PW,intersect_domain)(__isl_take PW *pw,
615 __isl_take isl_set *context)
617 return FN(PW,align_params_pw_set_and)(pw, context,
618 &FN(PW,intersect_domain_aligned));
621 static __isl_give PW *FN(PW,gist_aligned)(__isl_take PW *pw,
622 __isl_take isl_set *context)
625 isl_basic_set *hull = NULL;
631 isl_set_free(context);
635 if (!isl_space_match(pw->dim, isl_dim_param,
636 context->dim, isl_dim_param)) {
637 pw = FN(PW,align_params)(pw, isl_set_get_space(context));
638 context = isl_set_align_params(context, FN(PW,get_space)(pw));
641 context = isl_set_compute_divs(context);
642 hull = isl_set_simple_hull(isl_set_copy(context));
648 for (i = pw->n - 1; i >= 0; --i) {
649 pw->p[i].set = isl_set_intersect(pw->p[i].set,
650 isl_set_copy(context));
653 pw->p[i].FIELD = FN(EL,gist)(pw->p[i].FIELD,
654 isl_set_copy(pw->p[i].set));
655 pw->p[i].set = isl_set_gist_basic_set(pw->p[i].set,
656 isl_basic_set_copy(hull));
659 if (isl_set_plain_is_empty(pw->p[i].set)) {
660 isl_set_free(pw->p[i].set);
661 FN(EL,free)(pw->p[i].FIELD);
663 pw->p[i] = pw->p[pw->n - 1];
668 isl_basic_set_free(hull);
669 isl_set_free(context);
674 isl_basic_set_free(hull);
675 isl_set_free(context);
679 __isl_give PW *FN(PW,gist)(__isl_take PW *pw, __isl_take isl_set *context)
681 return FN(PW,align_params_pw_set_and)(pw, context, &FN(PW,gist_aligned));
684 __isl_give PW *FN(PW,coalesce)(__isl_take PW *pw)
693 for (i = pw->n - 1; i >= 0; --i) {
694 for (j = i - 1; j >= 0; --j) {
695 if (!FN(EL,plain_is_equal)(pw->p[i].FIELD,
698 pw->p[j].set = isl_set_union(pw->p[j].set,
700 FN(EL,free)(pw->p[i].FIELD);
702 pw->p[i] = pw->p[pw->n - 1];
708 pw->p[i].set = isl_set_coalesce(pw->p[i].set);
719 isl_ctx *FN(PW,get_ctx)(__isl_keep PW *pw)
721 return pw ? isl_space_get_ctx(pw->dim) : NULL;
724 #ifndef NO_INVOLVES_DIMS
725 int FN(PW,involves_dims)(__isl_keep PW *pw, enum isl_dim_type type,
726 unsigned first, unsigned n)
729 enum isl_dim_type set_type;
733 if (pw->n == 0 || n == 0)
736 set_type = type == isl_dim_in ? isl_dim_set : type;
738 for (i = 0; i < pw->n; ++i) {
739 int involves = FN(EL,involves_dims)(pw->p[i].FIELD,
741 if (involves < 0 || involves)
743 involves = isl_set_involves_dims(pw->p[i].set,
745 if (involves < 0 || involves)
752 __isl_give PW *FN(PW,set_dim_name)(__isl_take PW *pw,
753 enum isl_dim_type type, unsigned pos, const char *s)
756 enum isl_dim_type set_type;
762 set_type = type == isl_dim_in ? isl_dim_set : type;
764 pw->dim = isl_space_set_dim_name(pw->dim, type, pos, s);
768 for (i = 0; i < pw->n; ++i) {
769 pw->p[i].set = isl_set_set_dim_name(pw->p[i].set,
773 pw->p[i].FIELD = FN(EL,set_dim_name)(pw->p[i].FIELD, type, pos, s);
785 __isl_give PW *FN(PW,drop_dims)(__isl_take PW *pw,
786 enum isl_dim_type type, unsigned first, unsigned n)
789 enum isl_dim_type set_type;
793 if (n == 0 && !isl_space_get_tuple_name(pw->dim, type))
796 set_type = type == isl_dim_in ? isl_dim_set : type;
801 pw->dim = isl_space_drop_dims(pw->dim, type, first, n);
804 for (i = 0; i < pw->n; ++i) {
805 pw->p[i].set = isl_set_drop(pw->p[i].set, set_type, first, n);
808 pw->p[i].FIELD = FN(EL,drop_dims)(pw->p[i].FIELD, type, first, n);
819 /* This function is very similar to drop_dims.
820 * The only difference is that the cells may still involve
821 * the specified dimensions. They are removed using
822 * isl_set_project_out instead of isl_set_drop.
824 __isl_give PW *FN(PW,project_out)(__isl_take PW *pw,
825 enum isl_dim_type type, unsigned first, unsigned n)
828 enum isl_dim_type set_type;
832 if (n == 0 && !isl_space_get_tuple_name(pw->dim, type))
835 set_type = type == isl_dim_in ? isl_dim_set : type;
840 pw->dim = isl_space_drop_dims(pw->dim, type, first, n);
843 for (i = 0; i < pw->n; ++i) {
844 pw->p[i].set = isl_set_project_out(pw->p[i].set,
848 pw->p[i].FIELD = FN(EL,drop_dims)(pw->p[i].FIELD, type, first, n);
859 /* Project the domain of pw onto its parameter space.
861 __isl_give PW *FN(PW,project_domain_on_params)(__isl_take PW *pw)
866 n = FN(PW,dim)(pw, isl_dim_in);
867 pw = FN(PW,project_out)(pw, isl_dim_in, 0, n);
868 space = FN(PW,get_domain_space)(pw);
869 space = isl_space_params(space);
870 pw = FN(PW,reset_domain_space)(pw, space);
875 #ifndef NO_INSERT_DIMS
876 __isl_give PW *FN(PW,insert_dims)(__isl_take PW *pw, enum isl_dim_type type,
877 unsigned first, unsigned n)
880 enum isl_dim_type set_type;
884 if (n == 0 && !isl_space_is_named_or_nested(pw->dim, type))
887 set_type = type == isl_dim_in ? isl_dim_set : type;
893 pw->dim = isl_space_insert_dims(pw->dim, type, first, n);
897 for (i = 0; i < pw->n; ++i) {
898 pw->p[i].set = isl_set_insert_dims(pw->p[i].set,
902 pw->p[i].FIELD = FN(EL,insert_dims)(pw->p[i].FIELD,
915 __isl_give PW *FN(PW,fix_dim)(__isl_take PW *pw,
916 enum isl_dim_type type, unsigned pos, isl_int v)
923 if (type == isl_dim_in)
929 for (i = 0; i < pw->n; ++i) {
930 pw->p[i].set = isl_set_fix(pw->p[i].set, type, pos, v);
941 unsigned FN(PW,dim)(__isl_keep PW *pw, enum isl_dim_type type)
943 return pw ? isl_space_dim(pw->dim, type) : 0;
946 __isl_give PW *FN(PW,split_dims)(__isl_take PW *pw,
947 enum isl_dim_type type, unsigned first, unsigned n)
956 if (type == isl_dim_in)
964 for (i = 0; i < pw->n; ++i) {
965 pw->p[i].set = isl_set_split_dims(pw->p[i].set, type, first, n);
977 /* Compute the maximal value attained by the piecewise quasipolynomial
978 * on its domain or zero if the domain is empty.
979 * In the worst case, the domain is scanned completely,
980 * so the domain is assumed to be bounded.
982 __isl_give isl_qpolynomial *FN(PW,opt)(__isl_take PW *pw, int max)
985 isl_qpolynomial *opt;
991 isl_space *dim = isl_space_copy(pw->dim);
993 return isl_qpolynomial_zero_on_domain(dim);
996 opt = FN(EL,opt_on_domain)(FN(EL,copy)(pw->p[0].FIELD),
997 isl_set_copy(pw->p[0].set), max);
998 for (i = 1; i < pw->n; ++i) {
999 isl_qpolynomial *opt_i;
1000 opt_i = FN(EL,opt_on_domain)(FN(EL,copy)(pw->p[i].FIELD),
1001 isl_set_copy(pw->p[i].set), max);
1003 opt = isl_qpolynomial_max_cst(opt, opt_i);
1005 opt = isl_qpolynomial_min_cst(opt, opt_i);
1012 __isl_give isl_qpolynomial *FN(PW,max)(__isl_take PW *pw)
1014 return FN(PW,opt)(pw, 1);
1017 __isl_give isl_qpolynomial *FN(PW,min)(__isl_take PW *pw)
1019 return FN(PW,opt)(pw, 0);
1023 __isl_give isl_space *FN(PW,get_space)(__isl_keep PW *pw)
1025 return pw ? isl_space_copy(pw->dim) : NULL;
1028 __isl_give isl_space *FN(PW,get_domain_space)(__isl_keep PW *pw)
1030 return pw ? isl_space_domain(isl_space_copy(pw->dim)) : NULL;
1033 #ifndef NO_RESET_DIM
1034 /* Reset the space of "pw". Since we don't know if the elements
1035 * represent the spaces themselves or their domains, we pass along
1036 * both when we call their reset_space_and_domain.
1038 static __isl_give PW *FN(PW,reset_space_and_domain)(__isl_take PW *pw,
1039 __isl_take isl_space *space, __isl_take isl_space *domain)
1043 pw = FN(PW,cow)(pw);
1044 if (!pw || !space || !domain)
1047 for (i = 0; i < pw->n; ++i) {
1048 pw->p[i].set = isl_set_reset_space(pw->p[i].set,
1049 isl_space_copy(domain));
1052 pw->p[i].FIELD = FN(EL,reset_space_and_domain)(pw->p[i].FIELD,
1053 isl_space_copy(space), isl_space_copy(domain));
1054 if (!pw->p[i].FIELD)
1058 isl_space_free(domain);
1060 isl_space_free(pw->dim);
1065 isl_space_free(domain);
1066 isl_space_free(space);
1071 __isl_give PW *FN(PW,reset_domain_space)(__isl_take PW *pw,
1072 __isl_take isl_space *domain)
1076 space = isl_space_extend_domain_with_range(isl_space_copy(domain),
1077 FN(PW,get_space)(pw));
1078 return FN(PW,reset_space_and_domain)(pw, space, domain);
1081 __isl_give PW *FN(PW,reset_space)(__isl_take PW *pw, __isl_take isl_space *dim)
1085 domain = isl_space_domain(isl_space_copy(dim));
1086 return FN(PW,reset_space_and_domain)(pw, dim, domain);
1089 __isl_give PW *FN(PW,set_tuple_id)(__isl_keep PW *pw, enum isl_dim_type type,
1090 __isl_take isl_id *id)
1094 pw = FN(PW,cow)(pw);
1096 return isl_id_free(id);
1098 space = FN(PW,get_space)(pw);
1099 space = isl_space_set_tuple_id(space, type, id);
1101 return FN(PW,reset_space)(pw, space);
1105 int FN(PW,has_equal_space)(__isl_keep PW *pw1, __isl_keep PW *pw2)
1110 return isl_space_is_equal(pw1->dim, pw2->dim);
1114 __isl_give PW *FN(PW,morph_domain)(__isl_take PW *pw,
1115 __isl_take isl_morph *morph)
1123 ctx = isl_space_get_ctx(pw->dim);
1124 isl_assert(ctx, isl_space_is_domain(morph->dom->dim, pw->dim),
1127 pw = FN(PW,cow)(pw);
1130 pw->dim = isl_space_extend_domain_with_range(
1131 isl_space_copy(morph->ran->dim), pw->dim);
1135 for (i = 0; i < pw->n; ++i) {
1136 pw->p[i].set = isl_morph_set(isl_morph_copy(morph), pw->p[i].set);
1139 pw->p[i].FIELD = FN(EL,morph_domain)(pw->p[i].FIELD,
1140 isl_morph_copy(morph));
1141 if (!pw->p[i].FIELD)
1145 isl_morph_free(morph);
1150 isl_morph_free(morph);
1155 int FN(PW,foreach_piece)(__isl_keep PW *pw,
1156 int (*fn)(__isl_take isl_set *set, __isl_take EL *el, void *user),
1164 for (i = 0; i < pw->n; ++i)
1165 if (fn(isl_set_copy(pw->p[i].set),
1166 FN(EL,copy)(pw->p[i].FIELD), user) < 0)
1173 static int any_divs(__isl_keep isl_set *set)
1180 for (i = 0; i < set->n; ++i)
1181 if (set->p[i]->n_div > 0)
1187 static int foreach_lifted_subset(__isl_take isl_set *set, __isl_take EL *el,
1188 int (*fn)(__isl_take isl_set *set, __isl_take EL *el,
1189 void *user), void *user)
1196 for (i = 0; i < set->n; ++i) {
1200 lift = isl_set_from_basic_set(isl_basic_set_copy(set->p[i]));
1201 lift = isl_set_lift(lift);
1203 copy = FN(EL,copy)(el);
1204 copy = FN(EL,lift)(copy, isl_set_get_space(lift));
1206 if (fn(lift, copy, user) < 0)
1220 int FN(PW,foreach_lifted_piece)(__isl_keep PW *pw,
1221 int (*fn)(__isl_take isl_set *set, __isl_take EL *el,
1222 void *user), void *user)
1229 for (i = 0; i < pw->n; ++i) {
1233 set = isl_set_copy(pw->p[i].set);
1234 el = FN(EL,copy)(pw->p[i].FIELD);
1235 if (!any_divs(set)) {
1236 if (fn(set, el, user) < 0)
1240 if (foreach_lifted_subset(set, el, fn, user) < 0)
1248 #ifndef NO_MOVE_DIMS
1249 __isl_give PW *FN(PW,move_dims)(__isl_take PW *pw,
1250 enum isl_dim_type dst_type, unsigned dst_pos,
1251 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
1255 pw = FN(PW,cow)(pw);
1259 pw->dim = isl_space_move_dims(pw->dim, dst_type, dst_pos, src_type, src_pos, n);
1263 for (i = 0; i < pw->n; ++i) {
1264 pw->p[i].FIELD = FN(EL,move_dims)(pw->p[i].FIELD,
1265 dst_type, dst_pos, src_type, src_pos, n);
1266 if (!pw->p[i].FIELD)
1270 if (dst_type == isl_dim_in)
1271 dst_type = isl_dim_set;
1272 if (src_type == isl_dim_in)
1273 src_type = isl_dim_set;
1275 for (i = 0; i < pw->n; ++i) {
1276 pw->p[i].set = isl_set_move_dims(pw->p[i].set,
1278 src_type, src_pos, n);
1290 __isl_give PW *FN(PW,mul_isl_int)(__isl_take PW *pw, isl_int v)
1294 if (isl_int_is_one(v))
1296 if (pw && isl_int_is_zero(v)) {
1298 isl_space *dim = FN(PW,get_space)(pw);
1300 zero = FN(PW,ZERO)(dim, pw->type);
1302 zero = FN(PW,ZERO)(dim);
1307 pw = FN(PW,cow)(pw);
1314 if (isl_int_is_neg(v))
1315 pw->type = isl_fold_type_negate(pw->type);
1317 for (i = 0; i < pw->n; ++i) {
1318 pw->p[i].FIELD = FN(EL,scale)(pw->p[i].FIELD, v);
1319 if (!pw->p[i].FIELD)
1329 __isl_give PW *FN(PW,scale)(__isl_take PW *pw, isl_int v)
1331 return FN(PW,mul_isl_int)(pw, v);
1334 static int FN(PW,qsort_set_cmp)(const void *p1, const void *p2)
1336 const isl_set *set1 = *(const isl_set **)p1;
1337 const isl_set *set2 = *(const isl_set **)p2;
1339 return isl_set_plain_cmp(set1, set2);
1342 /* We normalize in place, but if anything goes wrong we need
1343 * to return NULL, so we need to make sure we don't change the
1344 * meaning of any possible other copies of map.
1346 __isl_give PW *FN(PW,normalize)(__isl_take PW *pw)
1353 for (i = 0; i < pw->n; ++i) {
1354 set = isl_set_normalize(isl_set_copy(pw->p[i].set));
1356 return FN(PW,free)(pw);
1357 isl_set_free(pw->p[i].set);
1360 qsort(pw->p, pw->n, sizeof(pw->p[0]), &FN(PW,qsort_set_cmp));
1361 for (i = pw->n - 1; i >= 1; --i) {
1362 if (!isl_set_plain_is_equal(pw->p[i - 1].set, pw->p[i].set))
1364 if (!FN(EL,plain_is_equal)(pw->p[i - 1].FIELD, pw->p[i].FIELD))
1366 set = isl_set_union(isl_set_copy(pw->p[i - 1].set),
1367 isl_set_copy(pw->p[i].set));
1369 return FN(PW,free)(pw);
1370 isl_set_free(pw->p[i].set);
1371 FN(EL,free)(pw->p[i].FIELD);
1372 isl_set_free(pw->p[i - 1].set);
1373 pw->p[i - 1].set = set;
1374 for (j = i + 1; j < pw->n; ++j)
1375 pw->p[j - 1] = pw->p[j];
1382 /* Is pw1 obviously equal to pw2?
1383 * That is, do they have obviously identical cells and obviously identical
1384 * elements on each cell?
1386 int FN(PW,plain_is_equal)(__isl_keep PW *pw1, __isl_keep PW *pw2)
1396 if (!isl_space_is_equal(pw1->dim, pw2->dim))
1399 pw1 = FN(PW,copy)(pw1);
1400 pw2 = FN(PW,copy)(pw2);
1401 pw1 = FN(PW,normalize)(pw1);
1402 pw2 = FN(PW,normalize)(pw2);
1406 equal = pw1->n == pw2->n;
1407 for (i = 0; equal && i < pw1->n; ++i) {
1408 equal = isl_set_plain_is_equal(pw1->p[i].set, pw2->p[i].set);
1413 equal = FN(EL,plain_is_equal)(pw1->p[i].FIELD, pw2->p[i].FIELD);