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 int FN(PW,IS_ZERO)(__isl_keep PW *pw)
190 __isl_give PW *FN(PW,realign_domain)(__isl_take PW *pw,
191 __isl_take isl_reordering *exp)
199 for (i = 0; i < pw->n; ++i) {
200 pw->p[i].set = isl_set_realign(pw->p[i].set,
201 isl_reordering_copy(exp));
204 pw->p[i].FIELD = FN(EL,realign_domain)(pw->p[i].FIELD,
205 isl_reordering_copy(exp));
210 pw = FN(PW,reset_domain_space)(pw, isl_space_copy(exp->dim));
212 isl_reordering_free(exp);
215 isl_reordering_free(exp);
220 /* Align the parameters of "pw" to those of "model".
222 __isl_give PW *FN(PW,align_params)(__isl_take PW *pw, __isl_take isl_space *model)
229 ctx = isl_space_get_ctx(model);
230 if (!isl_space_has_named_params(model))
231 isl_die(ctx, isl_error_invalid,
232 "model has unnamed parameters", goto error);
233 if (!isl_space_has_named_params(pw->dim))
234 isl_die(ctx, isl_error_invalid,
235 "input has unnamed parameters", goto error);
236 if (!isl_space_match(pw->dim, isl_dim_param, model, isl_dim_param)) {
239 model = isl_space_drop_dims(model, isl_dim_in,
240 0, isl_space_dim(model, isl_dim_in));
241 model = isl_space_drop_dims(model, isl_dim_out,
242 0, isl_space_dim(model, isl_dim_out));
243 exp = isl_parameter_alignment_reordering(pw->dim, model);
244 exp = isl_reordering_extend_space(exp,
245 FN(PW,get_domain_space)(pw));
246 pw = FN(PW,realign_domain)(pw, exp);
249 isl_space_free(model);
252 isl_space_free(model);
257 static __isl_give PW *FN(PW,align_params_pw_pw_and)(__isl_take PW *pw1,
259 __isl_give PW *(*fn)(__isl_take PW *pw1, __isl_take PW *pw2))
265 if (isl_space_match(pw1->dim, isl_dim_param, pw2->dim, isl_dim_param))
267 ctx = FN(PW,get_ctx)(pw1);
268 if (!isl_space_has_named_params(pw1->dim) ||
269 !isl_space_has_named_params(pw2->dim))
270 isl_die(ctx, isl_error_invalid,
271 "unaligned unnamed parameters", goto error);
272 pw1 = FN(PW,align_params)(pw1, FN(PW,get_space)(pw2));
273 pw2 = FN(PW,align_params)(pw2, FN(PW,get_space)(pw1));
281 static __isl_give PW *FN(PW,align_params_pw_set_and)(__isl_take PW *pw,
282 __isl_take isl_set *set,
283 __isl_give PW *(*fn)(__isl_take PW *pw, __isl_take isl_set *set))
289 if (isl_space_match(pw->dim, isl_dim_param, set->dim, isl_dim_param))
291 ctx = FN(PW,get_ctx)(pw);
292 if (!isl_space_has_named_params(pw->dim) ||
293 !isl_space_has_named_params(set->dim))
294 isl_die(ctx, isl_error_invalid,
295 "unaligned unnamed parameters", goto error);
296 pw = FN(PW,align_params)(pw, isl_set_get_space(set));
297 set = isl_set_align_params(set, FN(PW,get_space)(pw));
306 static __isl_give PW *FN(PW,add_aligned)(__isl_take PW *pw1, __isl_take PW *pw2)
316 ctx = isl_space_get_ctx(pw1->dim);
318 if (pw1->type != pw2->type)
319 isl_die(ctx, isl_error_invalid,
320 "fold types don't match", goto error);
322 isl_assert(ctx, isl_space_is_equal(pw1->dim, pw2->dim), goto error);
324 if (FN(PW,IS_ZERO)(pw1)) {
329 if (FN(PW,IS_ZERO)(pw2)) {
334 n = (pw1->n + 1) * (pw2->n + 1);
336 res = FN(PW,alloc_size)(isl_space_copy(pw1->dim), pw1->type, n);
338 res = FN(PW,alloc_size)(isl_space_copy(pw1->dim), n);
341 for (i = 0; i < pw1->n; ++i) {
342 set = isl_set_copy(pw1->p[i].set);
343 for (j = 0; j < pw2->n; ++j) {
344 struct isl_set *common;
346 set = isl_set_subtract(set,
347 isl_set_copy(pw2->p[j].set));
348 common = isl_set_intersect(isl_set_copy(pw1->p[i].set),
349 isl_set_copy(pw2->p[j].set));
350 if (isl_set_plain_is_empty(common)) {
351 isl_set_free(common);
355 sum = FN(EL,add_on_domain)(common,
356 FN(EL,copy)(pw1->p[i].FIELD),
357 FN(EL,copy)(pw2->p[j].FIELD));
359 res = FN(PW,add_piece)(res, common, sum);
361 res = FN(PW,add_piece)(res, set, FN(EL,copy)(pw1->p[i].FIELD));
364 for (j = 0; j < pw2->n; ++j) {
365 set = isl_set_copy(pw2->p[j].set);
366 for (i = 0; i < pw1->n; ++i)
367 set = isl_set_subtract(set,
368 isl_set_copy(pw1->p[i].set));
369 res = FN(PW,add_piece)(res, set, FN(EL,copy)(pw2->p[j].FIELD));
382 __isl_give PW *FN(PW,add)(__isl_take PW *pw1, __isl_take PW *pw2)
384 return FN(PW,align_params_pw_pw_and)(pw1, pw2, &FN(PW,add_aligned));
387 /* Make sure "pw" has room for at least "n" more pieces.
389 * If there is only one reference to pw, we extend it in place.
390 * Otherwise, we create a new PW and copy the pieces.
392 static __isl_give PW *FN(PW,grow)(__isl_take PW *pw, int n)
400 if (pw->n + n <= pw->size)
402 ctx = FN(PW,get_ctx)(pw);
405 res = isl_realloc(ctx, pw, struct PW,
406 sizeof(struct PW) + (n - 1) * sizeof(S(PW,piece)));
408 return FN(PW,free)(pw);
413 res = FN(PW,alloc_size)(isl_space_copy(pw->dim), pw->type, n);
415 res = FN(PW,alloc_size)(isl_space_copy(pw->dim), n);
418 return FN(PW,free)(pw);
419 for (i = 0; i < pw->n; ++i)
420 res = FN(PW,add_piece)(res, isl_set_copy(pw->p[i].set),
421 FN(EL,copy)(pw->p[i].FIELD));
426 static __isl_give PW *FN(PW,add_disjoint_aligned)(__isl_take PW *pw1,
435 if (pw1->size < pw1->n + pw2->n && pw1->n < pw2->n)
436 return FN(PW,add_disjoint_aligned)(pw2, pw1);
438 ctx = isl_space_get_ctx(pw1->dim);
440 if (pw1->type != pw2->type)
441 isl_die(ctx, isl_error_invalid,
442 "fold types don't match", goto error);
444 isl_assert(ctx, isl_space_is_equal(pw1->dim, pw2->dim), goto error);
446 if (FN(PW,IS_ZERO)(pw1)) {
451 if (FN(PW,IS_ZERO)(pw2)) {
456 pw1 = FN(PW,grow)(pw1, pw2->n);
460 for (i = 0; i < pw2->n; ++i)
461 pw1 = FN(PW,add_piece)(pw1,
462 isl_set_copy(pw2->p[i].set),
463 FN(EL,copy)(pw2->p[i].FIELD));
474 __isl_give PW *FN(PW,add_disjoint)(__isl_take PW *pw1, __isl_take PW *pw2)
476 return FN(PW,align_params_pw_pw_and)(pw1, pw2,
477 &FN(PW,add_disjoint_aligned));
481 __isl_give PW *FN(PW,neg)(__isl_take PW *pw)
488 if (FN(PW,IS_ZERO)(pw))
495 for (i = 0; i < pw->n; ++i) {
496 pw->p[i].FIELD = FN(EL,neg)(pw->p[i].FIELD);
498 return FN(PW,free)(pw);
504 __isl_give PW *FN(PW,sub)(__isl_take PW *pw1, __isl_take PW *pw2)
506 return FN(PW,add)(pw1, FN(PW,neg)(pw2));
511 __isl_give isl_qpolynomial *FN(PW,eval)(__isl_take PW *pw,
512 __isl_take isl_point *pnt)
517 isl_space *pnt_dim = NULL;
522 ctx = isl_point_get_ctx(pnt);
523 pnt_dim = isl_point_get_space(pnt);
524 isl_assert(ctx, isl_space_is_domain(pnt_dim, pw->dim), goto error);
526 for (i = 0; i < pw->n; ++i) {
527 found = isl_set_contains_point(pw->p[i].set, pnt);
534 qp = FN(EL,eval)(FN(EL,copy)(pw->p[i].FIELD),
535 isl_point_copy(pnt));
537 qp = isl_qpolynomial_zero_on_domain(FN(PW,get_domain_space)(pw));
539 isl_space_free(pnt_dim);
544 isl_space_free(pnt_dim);
550 __isl_give isl_set *FN(PW,domain)(__isl_take PW *pw)
558 dom = isl_set_empty(FN(PW,get_domain_space)(pw));
559 for (i = 0; i < pw->n; ++i)
560 dom = isl_set_union_disjoint(dom, isl_set_copy(pw->p[i].set));
567 static __isl_give PW *FN(PW,intersect_domain_aligned)(__isl_take PW *pw,
568 __isl_take isl_set *set)
584 for (i = pw->n - 1; i >= 0; --i) {
586 pw->p[i].set = isl_set_intersect(pw->p[i].set, isl_set_copy(set));
589 aff = isl_set_affine_hull(isl_set_copy(pw->p[i].set));
590 pw->p[i].FIELD = FN(EL,substitute_equalities)(pw->p[i].FIELD,
592 if (isl_set_plain_is_empty(pw->p[i].set)) {
593 isl_set_free(pw->p[i].set);
594 FN(EL,free)(pw->p[i].FIELD);
596 pw->p[i] = pw->p[pw->n - 1];
609 __isl_give PW *FN(PW,intersect_domain)(__isl_take PW *pw,
610 __isl_take isl_set *context)
612 return FN(PW,align_params_pw_set_and)(pw, context,
613 &FN(PW,intersect_domain_aligned));
616 static __isl_give PW *FN(PW,gist_aligned)(__isl_take PW *pw,
617 __isl_take isl_set *context)
620 isl_basic_set *hull = NULL;
626 isl_set_free(context);
630 if (!isl_space_match(pw->dim, isl_dim_param,
631 context->dim, isl_dim_param)) {
632 pw = FN(PW,align_params)(pw, isl_set_get_space(context));
633 context = isl_set_align_params(context, FN(PW,get_space)(pw));
636 context = isl_set_compute_divs(context);
637 hull = isl_set_simple_hull(isl_set_copy(context));
643 for (i = pw->n - 1; i >= 0; --i) {
644 pw->p[i].set = isl_set_intersect(pw->p[i].set,
645 isl_set_copy(context));
648 pw->p[i].FIELD = FN(EL,gist)(pw->p[i].FIELD,
649 isl_set_copy(pw->p[i].set));
650 pw->p[i].set = isl_set_gist_basic_set(pw->p[i].set,
651 isl_basic_set_copy(hull));
654 if (isl_set_plain_is_empty(pw->p[i].set)) {
655 isl_set_free(pw->p[i].set);
656 FN(EL,free)(pw->p[i].FIELD);
658 pw->p[i] = pw->p[pw->n - 1];
663 isl_basic_set_free(hull);
664 isl_set_free(context);
669 isl_basic_set_free(hull);
670 isl_set_free(context);
674 __isl_give PW *FN(PW,gist)(__isl_take PW *pw, __isl_take isl_set *context)
676 return FN(PW,align_params_pw_set_and)(pw, context, &FN(PW,gist_aligned));
679 __isl_give PW *FN(PW,coalesce)(__isl_take PW *pw)
688 for (i = pw->n - 1; i >= 0; --i) {
689 for (j = i - 1; j >= 0; --j) {
690 if (!FN(EL,plain_is_equal)(pw->p[i].FIELD,
693 pw->p[j].set = isl_set_union(pw->p[j].set,
695 FN(EL,free)(pw->p[i].FIELD);
697 pw->p[i] = pw->p[pw->n - 1];
703 pw->p[i].set = isl_set_coalesce(pw->p[i].set);
714 isl_ctx *FN(PW,get_ctx)(__isl_keep PW *pw)
716 return pw ? isl_space_get_ctx(pw->dim) : NULL;
719 #ifndef NO_INVOLVES_DIMS
720 int FN(PW,involves_dims)(__isl_keep PW *pw, enum isl_dim_type type,
721 unsigned first, unsigned n)
724 enum isl_dim_type set_type;
728 if (pw->n == 0 || n == 0)
731 set_type = type == isl_dim_in ? isl_dim_set : type;
733 for (i = 0; i < pw->n; ++i) {
734 int involves = FN(EL,involves_dims)(pw->p[i].FIELD,
736 if (involves < 0 || involves)
738 involves = isl_set_involves_dims(pw->p[i].set,
740 if (involves < 0 || involves)
747 __isl_give PW *FN(PW,set_dim_name)(__isl_take PW *pw,
748 enum isl_dim_type type, unsigned pos, const char *s)
751 enum isl_dim_type set_type;
757 set_type = type == isl_dim_in ? isl_dim_set : type;
759 pw->dim = isl_space_set_dim_name(pw->dim, type, pos, s);
763 for (i = 0; i < pw->n; ++i) {
764 pw->p[i].set = isl_set_set_dim_name(pw->p[i].set,
768 pw->p[i].FIELD = FN(EL,set_dim_name)(pw->p[i].FIELD, type, pos, s);
780 __isl_give PW *FN(PW,drop_dims)(__isl_take PW *pw,
781 enum isl_dim_type type, unsigned first, unsigned n)
784 enum isl_dim_type set_type;
788 if (n == 0 && !isl_space_get_tuple_name(pw->dim, type))
791 set_type = type == isl_dim_in ? isl_dim_set : type;
796 pw->dim = isl_space_drop_dims(pw->dim, type, first, n);
799 for (i = 0; i < pw->n; ++i) {
800 pw->p[i].set = isl_set_drop(pw->p[i].set, set_type, first, n);
803 pw->p[i].FIELD = FN(EL,drop_dims)(pw->p[i].FIELD, type, first, n);
814 /* This function is very similar to drop_dims.
815 * The only difference is that the cells may still involve
816 * the specified dimensions. They are removed using
817 * isl_set_project_out instead of isl_set_drop.
819 __isl_give PW *FN(PW,project_out)(__isl_take PW *pw,
820 enum isl_dim_type type, unsigned first, unsigned n)
823 enum isl_dim_type set_type;
827 if (n == 0 && !isl_space_get_tuple_name(pw->dim, type))
830 set_type = type == isl_dim_in ? isl_dim_set : type;
835 pw->dim = isl_space_drop_dims(pw->dim, type, first, n);
838 for (i = 0; i < pw->n; ++i) {
839 pw->p[i].set = isl_set_project_out(pw->p[i].set,
843 pw->p[i].FIELD = FN(EL,drop_dims)(pw->p[i].FIELD, type, first, n);
854 /* Project the domain of pw onto its parameter space.
856 __isl_give PW *FN(PW,project_domain_on_params)(__isl_take PW *pw)
861 n = FN(PW,dim)(pw, isl_dim_in);
862 pw = FN(PW,project_out)(pw, isl_dim_in, 0, n);
863 space = FN(PW,get_domain_space)(pw);
864 space = isl_space_params(space);
865 pw = FN(PW,reset_domain_space)(pw, space);
870 #ifndef NO_INSERT_DIMS
871 __isl_give PW *FN(PW,insert_dims)(__isl_take PW *pw, enum isl_dim_type type,
872 unsigned first, unsigned n)
875 enum isl_dim_type set_type;
879 if (n == 0 && !isl_space_is_named_or_nested(pw->dim, type))
882 set_type = type == isl_dim_in ? isl_dim_set : type;
888 pw->dim = isl_space_insert_dims(pw->dim, type, first, n);
892 for (i = 0; i < pw->n; ++i) {
893 pw->p[i].set = isl_set_insert_dims(pw->p[i].set,
897 pw->p[i].FIELD = FN(EL,insert_dims)(pw->p[i].FIELD,
910 __isl_give PW *FN(PW,fix_dim)(__isl_take PW *pw,
911 enum isl_dim_type type, unsigned pos, isl_int v)
918 if (type == isl_dim_in)
924 for (i = 0; i < pw->n; ++i) {
925 pw->p[i].set = isl_set_fix(pw->p[i].set, type, pos, v);
936 unsigned FN(PW,dim)(__isl_keep PW *pw, enum isl_dim_type type)
938 return pw ? isl_space_dim(pw->dim, type) : 0;
941 __isl_give PW *FN(PW,split_dims)(__isl_take PW *pw,
942 enum isl_dim_type type, unsigned first, unsigned n)
951 if (type == isl_dim_in)
959 for (i = 0; i < pw->n; ++i) {
960 pw->p[i].set = isl_set_split_dims(pw->p[i].set, type, first, n);
972 /* Compute the maximal value attained by the piecewise quasipolynomial
973 * on its domain or zero if the domain is empty.
974 * In the worst case, the domain is scanned completely,
975 * so the domain is assumed to be bounded.
977 __isl_give isl_qpolynomial *FN(PW,opt)(__isl_take PW *pw, int max)
980 isl_qpolynomial *opt;
986 isl_space *dim = isl_space_copy(pw->dim);
988 return isl_qpolynomial_zero_on_domain(dim);
991 opt = FN(EL,opt_on_domain)(FN(EL,copy)(pw->p[0].FIELD),
992 isl_set_copy(pw->p[0].set), max);
993 for (i = 1; i < pw->n; ++i) {
994 isl_qpolynomial *opt_i;
995 opt_i = FN(EL,opt_on_domain)(FN(EL,copy)(pw->p[i].FIELD),
996 isl_set_copy(pw->p[i].set), max);
998 opt = isl_qpolynomial_max_cst(opt, opt_i);
1000 opt = isl_qpolynomial_min_cst(opt, opt_i);
1007 __isl_give isl_qpolynomial *FN(PW,max)(__isl_take PW *pw)
1009 return FN(PW,opt)(pw, 1);
1012 __isl_give isl_qpolynomial *FN(PW,min)(__isl_take PW *pw)
1014 return FN(PW,opt)(pw, 0);
1018 __isl_give isl_space *FN(PW,get_space)(__isl_keep PW *pw)
1020 return pw ? isl_space_copy(pw->dim) : NULL;
1023 __isl_give isl_space *FN(PW,get_domain_space)(__isl_keep PW *pw)
1025 return pw ? isl_space_domain(isl_space_copy(pw->dim)) : NULL;
1028 #ifndef NO_RESET_DIM
1029 /* Reset the space of "pw". Since we don't know if the elements
1030 * represent the spaces themselves or their domains, we pass along
1031 * both when we call their reset_space_and_domain.
1033 static __isl_give PW *FN(PW,reset_space_and_domain)(__isl_take PW *pw,
1034 __isl_take isl_space *space, __isl_take isl_space *domain)
1038 pw = FN(PW,cow)(pw);
1039 if (!pw || !space || !domain)
1042 for (i = 0; i < pw->n; ++i) {
1043 pw->p[i].set = isl_set_reset_space(pw->p[i].set,
1044 isl_space_copy(domain));
1047 pw->p[i].FIELD = FN(EL,reset_space_and_domain)(pw->p[i].FIELD,
1048 isl_space_copy(space), isl_space_copy(domain));
1049 if (!pw->p[i].FIELD)
1053 isl_space_free(domain);
1055 isl_space_free(pw->dim);
1060 isl_space_free(domain);
1061 isl_space_free(space);
1066 __isl_give PW *FN(PW,reset_domain_space)(__isl_take PW *pw,
1067 __isl_take isl_space *domain)
1071 space = isl_space_extend_domain_with_range(isl_space_copy(domain),
1072 FN(PW,get_space)(pw));
1073 return FN(PW,reset_space_and_domain)(pw, space, domain);
1076 __isl_give PW *FN(PW,reset_space)(__isl_take PW *pw, __isl_take isl_space *dim)
1080 domain = isl_space_domain(isl_space_copy(dim));
1081 return FN(PW,reset_space_and_domain)(pw, dim, domain);
1084 __isl_give PW *FN(PW,set_tuple_id)(__isl_keep PW *pw, enum isl_dim_type type,
1085 __isl_take isl_id *id)
1089 pw = FN(PW,cow)(pw);
1091 return isl_id_free(id);
1093 space = FN(PW,get_space)(pw);
1094 space = isl_space_set_tuple_id(space, type, id);
1096 return FN(PW,reset_space)(pw, space);
1100 int FN(PW,has_equal_space)(__isl_keep PW *pw1, __isl_keep PW *pw2)
1105 return isl_space_is_equal(pw1->dim, pw2->dim);
1109 __isl_give PW *FN(PW,morph_domain)(__isl_take PW *pw,
1110 __isl_take isl_morph *morph)
1118 ctx = isl_space_get_ctx(pw->dim);
1119 isl_assert(ctx, isl_space_is_domain(morph->dom->dim, pw->dim),
1122 pw = FN(PW,cow)(pw);
1125 pw->dim = isl_space_extend_domain_with_range(
1126 isl_space_copy(morph->ran->dim), pw->dim);
1130 for (i = 0; i < pw->n; ++i) {
1131 pw->p[i].set = isl_morph_set(isl_morph_copy(morph), pw->p[i].set);
1134 pw->p[i].FIELD = FN(EL,morph_domain)(pw->p[i].FIELD,
1135 isl_morph_copy(morph));
1136 if (!pw->p[i].FIELD)
1140 isl_morph_free(morph);
1145 isl_morph_free(morph);
1150 int FN(PW,foreach_piece)(__isl_keep PW *pw,
1151 int (*fn)(__isl_take isl_set *set, __isl_take EL *el, void *user),
1159 for (i = 0; i < pw->n; ++i)
1160 if (fn(isl_set_copy(pw->p[i].set),
1161 FN(EL,copy)(pw->p[i].FIELD), user) < 0)
1168 static int any_divs(__isl_keep isl_set *set)
1175 for (i = 0; i < set->n; ++i)
1176 if (set->p[i]->n_div > 0)
1182 static int foreach_lifted_subset(__isl_take isl_set *set, __isl_take EL *el,
1183 int (*fn)(__isl_take isl_set *set, __isl_take EL *el,
1184 void *user), void *user)
1191 for (i = 0; i < set->n; ++i) {
1195 lift = isl_set_from_basic_set(isl_basic_set_copy(set->p[i]));
1196 lift = isl_set_lift(lift);
1198 copy = FN(EL,copy)(el);
1199 copy = FN(EL,lift)(copy, isl_set_get_space(lift));
1201 if (fn(lift, copy, user) < 0)
1215 int FN(PW,foreach_lifted_piece)(__isl_keep PW *pw,
1216 int (*fn)(__isl_take isl_set *set, __isl_take EL *el,
1217 void *user), void *user)
1224 for (i = 0; i < pw->n; ++i) {
1228 set = isl_set_copy(pw->p[i].set);
1229 el = FN(EL,copy)(pw->p[i].FIELD);
1230 if (!any_divs(set)) {
1231 if (fn(set, el, user) < 0)
1235 if (foreach_lifted_subset(set, el, fn, user) < 0)
1243 #ifndef NO_MOVE_DIMS
1244 __isl_give PW *FN(PW,move_dims)(__isl_take PW *pw,
1245 enum isl_dim_type dst_type, unsigned dst_pos,
1246 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
1250 pw = FN(PW,cow)(pw);
1254 pw->dim = isl_space_move_dims(pw->dim, dst_type, dst_pos, src_type, src_pos, n);
1258 for (i = 0; i < pw->n; ++i) {
1259 pw->p[i].FIELD = FN(EL,move_dims)(pw->p[i].FIELD,
1260 dst_type, dst_pos, src_type, src_pos, n);
1261 if (!pw->p[i].FIELD)
1265 if (dst_type == isl_dim_in)
1266 dst_type = isl_dim_set;
1267 if (src_type == isl_dim_in)
1268 src_type = isl_dim_set;
1270 for (i = 0; i < pw->n; ++i) {
1271 pw->p[i].set = isl_set_move_dims(pw->p[i].set,
1273 src_type, src_pos, n);
1285 __isl_give PW *FN(PW,mul_isl_int)(__isl_take PW *pw, isl_int v)
1289 if (isl_int_is_one(v))
1291 if (pw && isl_int_is_zero(v)) {
1293 isl_space *dim = FN(PW,get_space)(pw);
1295 zero = FN(PW,ZERO)(dim, pw->type);
1297 zero = FN(PW,ZERO)(dim);
1302 pw = FN(PW,cow)(pw);
1309 if (isl_int_is_neg(v))
1310 pw->type = isl_fold_type_negate(pw->type);
1312 for (i = 0; i < pw->n; ++i) {
1313 pw->p[i].FIELD = FN(EL,scale)(pw->p[i].FIELD, v);
1314 if (!pw->p[i].FIELD)
1324 __isl_give PW *FN(PW,scale)(__isl_take PW *pw, isl_int v)
1326 return FN(PW,mul_isl_int)(pw, v);
1329 static int FN(PW,qsort_set_cmp)(const void *p1, const void *p2)
1331 const isl_set *set1 = *(const isl_set **)p1;
1332 const isl_set *set2 = *(const isl_set **)p2;
1334 return isl_set_plain_cmp(set1, set2);
1337 /* We normalize in place, but if anything goes wrong we need
1338 * to return NULL, so we need to make sure we don't change the
1339 * meaning of any possible other copies of map.
1341 __isl_give PW *FN(PW,normalize)(__isl_take PW *pw)
1348 for (i = 0; i < pw->n; ++i) {
1349 set = isl_set_normalize(isl_set_copy(pw->p[i].set));
1351 return FN(PW,free)(pw);
1352 isl_set_free(pw->p[i].set);
1355 qsort(pw->p, pw->n, sizeof(pw->p[0]), &FN(PW,qsort_set_cmp));
1356 for (i = pw->n - 1; i >= 1; --i) {
1357 if (!isl_set_plain_is_equal(pw->p[i - 1].set, pw->p[i].set))
1359 if (!FN(EL,plain_is_equal)(pw->p[i - 1].FIELD, pw->p[i].FIELD))
1361 set = isl_set_union(isl_set_copy(pw->p[i - 1].set),
1362 isl_set_copy(pw->p[i].set));
1364 return FN(PW,free)(pw);
1365 isl_set_free(pw->p[i].set);
1366 FN(EL,free)(pw->p[i].FIELD);
1367 isl_set_free(pw->p[i - 1].set);
1368 pw->p[i - 1].set = set;
1369 for (j = i + 1; j < pw->n; ++j)
1370 pw->p[j - 1] = pw->p[j];
1377 /* Is pw1 obviously equal to pw2?
1378 * That is, do they have obviously identical cells and obviously identical
1379 * elements on each cell?
1381 int FN(PW,plain_is_equal)(__isl_keep PW *pw1, __isl_keep PW *pw2)
1391 if (!isl_space_is_equal(pw1->dim, pw2->dim))
1394 pw1 = FN(PW,copy)(pw1);
1395 pw2 = FN(PW,copy)(pw2);
1396 pw1 = FN(PW,normalize)(pw1);
1397 pw2 = FN(PW,normalize)(pw2);
1401 equal = pw1->n == pw2->n;
1402 for (i = 0; equal && i < pw1->n; ++i) {
1403 equal = isl_set_plain_is_equal(pw1->p[i].set, pw2->p[i].set);
1408 equal = FN(EL,plain_is_equal)(pw1->p[i].FIELD, pw2->p[i].FIELD);