2 #include <isl_val_private.h>
4 #define xFN(TYPE,NAME) TYPE ## _ ## NAME
5 #define FN(TYPE,NAME) xFN(TYPE,NAME)
6 #define xS(TYPE,NAME) struct TYPE ## _ ## NAME
7 #define S(TYPE,NAME) xS(TYPE,NAME)
10 __isl_give PW *FN(PW,alloc_size)(__isl_take isl_space *dim,
11 enum isl_fold type, int n)
13 __isl_give PW *FN(PW,alloc_size)(__isl_take isl_space *dim, int n)
21 ctx = isl_space_get_ctx(dim);
22 isl_assert(ctx, n >= 0, goto error);
23 pw = isl_alloc(ctx, struct PW,
24 sizeof(struct PW) + (n - 1) * sizeof(S(PW,piece)));
42 __isl_give PW *FN(PW,ZERO)(__isl_take isl_space *dim, enum isl_fold type)
44 return FN(PW,alloc_size)(dim, type, 0);
47 __isl_give PW *FN(PW,ZERO)(__isl_take isl_space *dim)
49 return FN(PW,alloc_size)(dim, 0);
53 __isl_give PW *FN(PW,add_piece)(__isl_take PW *pw,
54 __isl_take isl_set *set, __isl_take EL *el)
57 isl_space *el_dim = NULL;
59 if (!pw || !set || !el)
62 if (isl_set_plain_is_empty(set) || FN(EL,EL_IS_ZERO)(el)) {
68 ctx = isl_set_get_ctx(set);
70 if (pw->type != el->type)
71 isl_die(ctx, isl_error_invalid, "fold types don't match",
74 el_dim = FN(EL,get_space(el));
75 isl_assert(ctx, isl_space_is_equal(pw->dim, el_dim), goto error);
76 isl_assert(ctx, pw->n < pw->size, goto error);
78 pw->p[pw->n].set = set;
79 pw->p[pw->n].FIELD = el;
82 isl_space_free(el_dim);
85 isl_space_free(el_dim);
93 __isl_give PW *FN(PW,alloc)(enum isl_fold type,
94 __isl_take isl_set *set, __isl_take EL *el)
96 __isl_give PW *FN(PW,alloc)(__isl_take isl_set *set, __isl_take EL *el)
105 pw = FN(PW,alloc_size)(FN(EL,get_space)(el), type, 1);
107 pw = FN(PW,alloc_size)(FN(EL,get_space)(el), 1);
110 return FN(PW,add_piece)(pw, set, el);
117 __isl_give PW *FN(PW,dup)(__isl_keep PW *pw)
126 dup = FN(PW,alloc_size)(isl_space_copy(pw->dim), pw->type, pw->n);
128 dup = FN(PW,alloc_size)(isl_space_copy(pw->dim), pw->n);
133 for (i = 0; i < pw->n; ++i)
134 dup = FN(PW,add_piece)(dup, isl_set_copy(pw->p[i].set),
135 FN(EL,copy)(pw->p[i].FIELD));
140 __isl_give PW *FN(PW,cow)(__isl_take PW *pw)
148 return FN(PW,dup)(pw);
151 __isl_give PW *FN(PW,copy)(__isl_keep PW *pw)
160 void *FN(PW,free)(__isl_take PW *pw)
169 for (i = 0; i < pw->n; ++i) {
170 isl_set_free(pw->p[i].set);
171 FN(EL,free)(pw->p[i].FIELD);
173 isl_space_free(pw->dim);
179 const char *FN(PW,get_dim_name)(__isl_keep PW *pw, enum isl_dim_type type,
182 return pw ? isl_space_get_dim_name(pw->dim, type, pos) : NULL;
185 int FN(PW,has_dim_id)(__isl_keep PW *pw, enum isl_dim_type type, unsigned pos)
187 return pw ? isl_space_has_dim_id(pw->dim, type, pos) : -1;
190 __isl_give isl_id *FN(PW,get_dim_id)(__isl_keep PW *pw, enum isl_dim_type type,
193 return pw ? isl_space_get_dim_id(pw->dim, type, pos) : NULL;
196 int FN(PW,has_tuple_name)(__isl_keep PW *pw, enum isl_dim_type type)
198 return pw ? isl_space_has_tuple_name(pw->dim, type) : -1;
201 const char *FN(PW,get_tuple_name)(__isl_keep PW *pw, enum isl_dim_type type)
203 return pw ? isl_space_get_tuple_name(pw->dim, type) : NULL;
206 int FN(PW,has_tuple_id)(__isl_keep PW *pw, enum isl_dim_type type)
208 return pw ? isl_space_has_tuple_id(pw->dim, type) : -1;
211 __isl_give isl_id *FN(PW,get_tuple_id)(__isl_keep PW *pw, enum isl_dim_type type)
213 return pw ? isl_space_get_tuple_id(pw->dim, type) : NULL;
216 int FN(PW,IS_ZERO)(__isl_keep PW *pw)
225 __isl_give PW *FN(PW,realign_domain)(__isl_take PW *pw,
226 __isl_take isl_reordering *exp)
234 for (i = 0; i < pw->n; ++i) {
235 pw->p[i].set = isl_set_realign(pw->p[i].set,
236 isl_reordering_copy(exp));
239 pw->p[i].FIELD = FN(EL,realign_domain)(pw->p[i].FIELD,
240 isl_reordering_copy(exp));
245 pw = FN(PW,reset_domain_space)(pw, isl_space_copy(exp->dim));
247 isl_reordering_free(exp);
250 isl_reordering_free(exp);
255 /* Align the parameters of "pw" to those of "model".
257 __isl_give PW *FN(PW,align_params)(__isl_take PW *pw, __isl_take isl_space *model)
264 ctx = isl_space_get_ctx(model);
265 if (!isl_space_has_named_params(model))
266 isl_die(ctx, isl_error_invalid,
267 "model has unnamed parameters", goto error);
268 if (!isl_space_has_named_params(pw->dim))
269 isl_die(ctx, isl_error_invalid,
270 "input has unnamed parameters", goto error);
271 if (!isl_space_match(pw->dim, isl_dim_param, model, isl_dim_param)) {
274 model = isl_space_drop_dims(model, isl_dim_in,
275 0, isl_space_dim(model, isl_dim_in));
276 model = isl_space_drop_dims(model, isl_dim_out,
277 0, isl_space_dim(model, isl_dim_out));
278 exp = isl_parameter_alignment_reordering(pw->dim, model);
279 exp = isl_reordering_extend_space(exp,
280 FN(PW,get_domain_space)(pw));
281 pw = FN(PW,realign_domain)(pw, exp);
284 isl_space_free(model);
287 isl_space_free(model);
292 static __isl_give PW *FN(PW,align_params_pw_pw_and)(__isl_take PW *pw1,
294 __isl_give PW *(*fn)(__isl_take PW *pw1, __isl_take PW *pw2))
300 if (isl_space_match(pw1->dim, isl_dim_param, pw2->dim, isl_dim_param))
302 ctx = FN(PW,get_ctx)(pw1);
303 if (!isl_space_has_named_params(pw1->dim) ||
304 !isl_space_has_named_params(pw2->dim))
305 isl_die(ctx, isl_error_invalid,
306 "unaligned unnamed parameters", goto error);
307 pw1 = FN(PW,align_params)(pw1, FN(PW,get_space)(pw2));
308 pw2 = FN(PW,align_params)(pw2, FN(PW,get_space)(pw1));
316 static __isl_give PW *FN(PW,align_params_pw_set_and)(__isl_take PW *pw,
317 __isl_take isl_set *set,
318 __isl_give PW *(*fn)(__isl_take PW *pw, __isl_take isl_set *set))
324 if (isl_space_match(pw->dim, isl_dim_param, set->dim, isl_dim_param))
326 ctx = FN(PW,get_ctx)(pw);
327 if (!isl_space_has_named_params(pw->dim) ||
328 !isl_space_has_named_params(set->dim))
329 isl_die(ctx, isl_error_invalid,
330 "unaligned unnamed parameters", goto error);
331 pw = FN(PW,align_params)(pw, isl_set_get_space(set));
332 set = isl_set_align_params(set, FN(PW,get_space)(pw));
341 static __isl_give PW *FN(PW,union_add_aligned)(__isl_take PW *pw1,
352 ctx = isl_space_get_ctx(pw1->dim);
354 if (pw1->type != pw2->type)
355 isl_die(ctx, isl_error_invalid,
356 "fold types don't match", goto error);
358 isl_assert(ctx, isl_space_is_equal(pw1->dim, pw2->dim), goto error);
360 if (FN(PW,IS_ZERO)(pw1)) {
365 if (FN(PW,IS_ZERO)(pw2)) {
370 n = (pw1->n + 1) * (pw2->n + 1);
372 res = FN(PW,alloc_size)(isl_space_copy(pw1->dim), pw1->type, n);
374 res = FN(PW,alloc_size)(isl_space_copy(pw1->dim), n);
377 for (i = 0; i < pw1->n; ++i) {
378 set = isl_set_copy(pw1->p[i].set);
379 for (j = 0; j < pw2->n; ++j) {
380 struct isl_set *common;
382 common = isl_set_intersect(isl_set_copy(pw1->p[i].set),
383 isl_set_copy(pw2->p[j].set));
384 if (isl_set_plain_is_empty(common)) {
385 isl_set_free(common);
388 set = isl_set_subtract(set,
389 isl_set_copy(pw2->p[j].set));
391 sum = FN(EL,add_on_domain)(common,
392 FN(EL,copy)(pw1->p[i].FIELD),
393 FN(EL,copy)(pw2->p[j].FIELD));
395 res = FN(PW,add_piece)(res, common, sum);
397 res = FN(PW,add_piece)(res, set, FN(EL,copy)(pw1->p[i].FIELD));
400 for (j = 0; j < pw2->n; ++j) {
401 set = isl_set_copy(pw2->p[j].set);
402 for (i = 0; i < pw1->n; ++i)
403 set = isl_set_subtract(set,
404 isl_set_copy(pw1->p[i].set));
405 res = FN(PW,add_piece)(res, set, FN(EL,copy)(pw2->p[j].FIELD));
418 /* Private version of "union_add". For isl_pw_qpolynomial and
419 * isl_pw_qpolynomial_fold, we prefer to simply call it "add".
421 static __isl_give PW *FN(PW,union_add_)(__isl_take PW *pw1, __isl_take PW *pw2)
423 return FN(PW,align_params_pw_pw_and)(pw1, pw2,
424 &FN(PW,union_add_aligned));
427 /* Make sure "pw" has room for at least "n" more pieces.
429 * If there is only one reference to pw, we extend it in place.
430 * Otherwise, we create a new PW and copy the pieces.
432 static __isl_give PW *FN(PW,grow)(__isl_take PW *pw, int n)
440 if (pw->n + n <= pw->size)
442 ctx = FN(PW,get_ctx)(pw);
445 res = isl_realloc(ctx, pw, struct PW,
446 sizeof(struct PW) + (n - 1) * sizeof(S(PW,piece)));
448 return FN(PW,free)(pw);
453 res = FN(PW,alloc_size)(isl_space_copy(pw->dim), pw->type, n);
455 res = FN(PW,alloc_size)(isl_space_copy(pw->dim), n);
458 return FN(PW,free)(pw);
459 for (i = 0; i < pw->n; ++i)
460 res = FN(PW,add_piece)(res, isl_set_copy(pw->p[i].set),
461 FN(EL,copy)(pw->p[i].FIELD));
466 static __isl_give PW *FN(PW,add_disjoint_aligned)(__isl_take PW *pw1,
475 if (pw1->size < pw1->n + pw2->n && pw1->n < pw2->n)
476 return FN(PW,add_disjoint_aligned)(pw2, pw1);
478 ctx = isl_space_get_ctx(pw1->dim);
480 if (pw1->type != pw2->type)
481 isl_die(ctx, isl_error_invalid,
482 "fold types don't match", goto error);
484 isl_assert(ctx, isl_space_is_equal(pw1->dim, pw2->dim), goto error);
486 if (FN(PW,IS_ZERO)(pw1)) {
491 if (FN(PW,IS_ZERO)(pw2)) {
496 pw1 = FN(PW,grow)(pw1, pw2->n);
500 for (i = 0; i < pw2->n; ++i)
501 pw1 = FN(PW,add_piece)(pw1,
502 isl_set_copy(pw2->p[i].set),
503 FN(EL,copy)(pw2->p[i].FIELD));
514 __isl_give PW *FN(PW,add_disjoint)(__isl_take PW *pw1, __isl_take PW *pw2)
516 return FN(PW,align_params_pw_pw_and)(pw1, pw2,
517 &FN(PW,add_disjoint_aligned));
520 /* This function is currently only used from isl_aff.c
522 static __isl_give PW *FN(PW,on_shared_domain_in)(__isl_take PW *pw1,
523 __isl_take PW *pw2, __isl_take isl_space *space,
524 __isl_give EL *(*fn)(__isl_take EL *el1, __isl_take EL *el2))
525 __attribute__ ((unused));
527 /* Apply "fn" to pairs of elements from pw1 and pw2 on shared domains.
528 * The result of "fn" (and therefore also of this function) lives in "space".
530 static __isl_give PW *FN(PW,on_shared_domain_in)(__isl_take PW *pw1,
531 __isl_take PW *pw2, __isl_take isl_space *space,
532 __isl_give EL *(*fn)(__isl_take EL *el1, __isl_take EL *el2))
542 res = FN(PW,alloc_size)(isl_space_copy(space), pw1->type, n);
544 res = FN(PW,alloc_size)(isl_space_copy(space), n);
547 for (i = 0; i < pw1->n; ++i) {
548 for (j = 0; j < pw2->n; ++j) {
553 common = isl_set_intersect(
554 isl_set_copy(pw1->p[i].set),
555 isl_set_copy(pw2->p[j].set));
556 empty = isl_set_plain_is_empty(common);
557 if (empty < 0 || empty) {
558 isl_set_free(common);
564 res_ij = fn(FN(EL,copy)(pw1->p[i].FIELD),
565 FN(EL,copy)(pw2->p[j].FIELD));
566 res_ij = FN(EL,gist)(res_ij, isl_set_copy(common));
568 res = FN(PW,add_piece)(res, common, res_ij);
572 isl_space_free(space);
577 isl_space_free(space);
584 /* This function is currently only used from isl_aff.c
586 static __isl_give PW *FN(PW,on_shared_domain)(__isl_take PW *pw1,
588 __isl_give EL *(*fn)(__isl_take EL *el1, __isl_take EL *el2))
589 __attribute__ ((unused));
591 /* Apply "fn" to pairs of elements from pw1 and pw2 on shared domains.
592 * The result of "fn" is assumed to live in the same space as "pw1" and "pw2".
594 static __isl_give PW *FN(PW,on_shared_domain)(__isl_take PW *pw1,
596 __isl_give EL *(*fn)(__isl_take EL *el1, __isl_take EL *el2))
603 space = isl_space_copy(pw1->dim);
604 return FN(PW,on_shared_domain_in)(pw1, pw2, space, fn);
612 __isl_give PW *FN(PW,neg)(__isl_take PW *pw)
619 if (FN(PW,IS_ZERO)(pw))
626 for (i = 0; i < pw->n; ++i) {
627 pw->p[i].FIELD = FN(EL,neg)(pw->p[i].FIELD);
629 return FN(PW,free)(pw);
635 __isl_give PW *FN(PW,sub)(__isl_take PW *pw1, __isl_take PW *pw2)
637 return FN(PW,add)(pw1, FN(PW,neg)(pw2));
642 __isl_give isl_qpolynomial *FN(PW,eval)(__isl_take PW *pw,
643 __isl_take isl_point *pnt)
648 isl_space *pnt_dim = NULL;
653 ctx = isl_point_get_ctx(pnt);
654 pnt_dim = isl_point_get_space(pnt);
655 isl_assert(ctx, isl_space_is_domain_internal(pnt_dim, pw->dim),
658 for (i = 0; i < pw->n; ++i) {
659 found = isl_set_contains_point(pw->p[i].set, pnt);
666 qp = FN(EL,eval)(FN(EL,copy)(pw->p[i].FIELD),
667 isl_point_copy(pnt));
669 qp = isl_qpolynomial_zero_on_domain(FN(PW,get_domain_space)(pw));
671 isl_space_free(pnt_dim);
676 isl_space_free(pnt_dim);
682 __isl_give isl_set *FN(PW,domain)(__isl_take PW *pw)
690 dom = isl_set_empty(FN(PW,get_domain_space)(pw));
691 for (i = 0; i < pw->n; ++i)
692 dom = isl_set_union_disjoint(dom, isl_set_copy(pw->p[i].set));
699 /* Restrict the domain of "pw" by combining each cell
700 * with "set" through a call to "fn", where "fn" may be
701 * isl_set_intersect or isl_set_intersect_params.
703 static __isl_give PW *FN(PW,intersect_aligned)(__isl_take PW *pw,
704 __isl_take isl_set *set,
705 __isl_give isl_set *(*fn)(__isl_take isl_set *set1,
706 __isl_take isl_set *set2))
722 for (i = pw->n - 1; i >= 0; --i) {
724 pw->p[i].set = fn(pw->p[i].set, isl_set_copy(set));
727 aff = isl_set_affine_hull(isl_set_copy(pw->p[i].set));
728 pw->p[i].FIELD = FN(EL,substitute_equalities)(pw->p[i].FIELD,
732 if (isl_set_plain_is_empty(pw->p[i].set)) {
733 isl_set_free(pw->p[i].set);
734 FN(EL,free)(pw->p[i].FIELD);
736 pw->p[i] = pw->p[pw->n - 1];
749 static __isl_give PW *FN(PW,intersect_domain_aligned)(__isl_take PW *pw,
750 __isl_take isl_set *set)
752 return FN(PW,intersect_aligned)(pw, set, &isl_set_intersect);
755 __isl_give PW *FN(PW,intersect_domain)(__isl_take PW *pw,
756 __isl_take isl_set *context)
758 return FN(PW,align_params_pw_set_and)(pw, context,
759 &FN(PW,intersect_domain_aligned));
762 static __isl_give PW *FN(PW,intersect_params_aligned)(__isl_take PW *pw,
763 __isl_take isl_set *set)
765 return FN(PW,intersect_aligned)(pw, set, &isl_set_intersect_params);
768 /* Intersect the domain of "pw" with the parameter domain "context".
770 __isl_give PW *FN(PW,intersect_params)(__isl_take PW *pw,
771 __isl_take isl_set *context)
773 return FN(PW,align_params_pw_set_and)(pw, context,
774 &FN(PW,intersect_params_aligned));
777 static __isl_give PW *FN(PW,gist_aligned)(__isl_take PW *pw,
778 __isl_take isl_set *context,
779 __isl_give EL *(*fn_el)(__isl_take EL *el,
780 __isl_take isl_set *set),
781 __isl_give isl_set *(*fn_dom)(__isl_take isl_set *set,
782 __isl_take isl_basic_set *bset))
785 isl_basic_set *hull = NULL;
791 isl_set_free(context);
795 if (!isl_space_match(pw->dim, isl_dim_param,
796 context->dim, isl_dim_param)) {
797 pw = FN(PW,align_params)(pw, isl_set_get_space(context));
798 context = isl_set_align_params(context, FN(PW,get_space)(pw));
801 context = isl_set_compute_divs(context);
802 hull = isl_set_simple_hull(isl_set_copy(context));
808 for (i = pw->n - 1; i >= 0; --i) {
812 set_i = isl_set_intersect(isl_set_copy(pw->p[i].set),
813 isl_set_copy(context));
814 empty = isl_set_plain_is_empty(set_i);
815 pw->p[i].FIELD = fn_el(pw->p[i].FIELD, set_i);
816 pw->p[i].set = fn_dom(pw->p[i].set, isl_basic_set_copy(hull));
817 if (!pw->p[i].FIELD || !pw->p[i].set)
820 isl_set_free(pw->p[i].set);
821 FN(EL,free)(pw->p[i].FIELD);
823 pw->p[i] = pw->p[pw->n - 1];
828 isl_basic_set_free(hull);
829 isl_set_free(context);
834 isl_basic_set_free(hull);
835 isl_set_free(context);
839 static __isl_give PW *FN(PW,gist_domain_aligned)(__isl_take PW *pw,
840 __isl_take isl_set *set)
842 return FN(PW,gist_aligned)(pw, set, &FN(EL,gist),
843 &isl_set_gist_basic_set);
846 __isl_give PW *FN(PW,gist)(__isl_take PW *pw, __isl_take isl_set *context)
848 return FN(PW,align_params_pw_set_and)(pw, context,
849 &FN(PW,gist_domain_aligned));
852 static __isl_give PW *FN(PW,gist_params_aligned)(__isl_take PW *pw,
853 __isl_take isl_set *set)
855 return FN(PW,gist_aligned)(pw, set, &FN(EL,gist_params),
856 &isl_set_gist_params_basic_set);
859 __isl_give PW *FN(PW,gist_params)(__isl_take PW *pw,
860 __isl_take isl_set *context)
862 return FN(PW,align_params_pw_set_and)(pw, context,
863 &FN(PW,gist_params_aligned));
866 __isl_give PW *FN(PW,coalesce)(__isl_take PW *pw)
875 for (i = pw->n - 1; i >= 0; --i) {
876 for (j = i - 1; j >= 0; --j) {
877 if (!FN(EL,plain_is_equal)(pw->p[i].FIELD,
880 pw->p[j].set = isl_set_union(pw->p[j].set,
882 FN(EL,free)(pw->p[i].FIELD);
884 pw->p[i] = pw->p[pw->n - 1];
890 pw->p[i].set = isl_set_coalesce(pw->p[i].set);
901 isl_ctx *FN(PW,get_ctx)(__isl_keep PW *pw)
903 return pw ? isl_space_get_ctx(pw->dim) : NULL;
906 #ifndef NO_INVOLVES_DIMS
907 int FN(PW,involves_dims)(__isl_keep PW *pw, enum isl_dim_type type,
908 unsigned first, unsigned n)
911 enum isl_dim_type set_type;
915 if (pw->n == 0 || n == 0)
918 set_type = type == isl_dim_in ? isl_dim_set : type;
920 for (i = 0; i < pw->n; ++i) {
921 int involves = FN(EL,involves_dims)(pw->p[i].FIELD,
923 if (involves < 0 || involves)
925 involves = isl_set_involves_dims(pw->p[i].set,
927 if (involves < 0 || involves)
934 __isl_give PW *FN(PW,set_dim_name)(__isl_take PW *pw,
935 enum isl_dim_type type, unsigned pos, const char *s)
938 enum isl_dim_type set_type;
944 set_type = type == isl_dim_in ? isl_dim_set : type;
946 pw->dim = isl_space_set_dim_name(pw->dim, type, pos, s);
950 for (i = 0; i < pw->n; ++i) {
951 pw->p[i].set = isl_set_set_dim_name(pw->p[i].set,
955 pw->p[i].FIELD = FN(EL,set_dim_name)(pw->p[i].FIELD, type, pos, s);
967 __isl_give PW *FN(PW,drop_dims)(__isl_take PW *pw,
968 enum isl_dim_type type, unsigned first, unsigned n)
971 enum isl_dim_type set_type;
975 if (n == 0 && !isl_space_get_tuple_name(pw->dim, type))
978 set_type = type == isl_dim_in ? isl_dim_set : type;
983 pw->dim = isl_space_drop_dims(pw->dim, type, first, n);
986 for (i = 0; i < pw->n; ++i) {
987 pw->p[i].FIELD = FN(EL,drop_dims)(pw->p[i].FIELD, type, first, n);
990 if (type == isl_dim_out)
992 pw->p[i].set = isl_set_drop(pw->p[i].set, set_type, first, n);
1003 /* This function is very similar to drop_dims.
1004 * The only difference is that the cells may still involve
1005 * the specified dimensions. They are removed using
1006 * isl_set_project_out instead of isl_set_drop.
1008 __isl_give PW *FN(PW,project_out)(__isl_take PW *pw,
1009 enum isl_dim_type type, unsigned first, unsigned n)
1012 enum isl_dim_type set_type;
1016 if (n == 0 && !isl_space_get_tuple_name(pw->dim, type))
1019 set_type = type == isl_dim_in ? isl_dim_set : type;
1021 pw = FN(PW,cow)(pw);
1024 pw->dim = isl_space_drop_dims(pw->dim, type, first, n);
1027 for (i = 0; i < pw->n; ++i) {
1028 pw->p[i].set = isl_set_project_out(pw->p[i].set,
1029 set_type, first, n);
1032 pw->p[i].FIELD = FN(EL,drop_dims)(pw->p[i].FIELD, type, first, n);
1033 if (!pw->p[i].FIELD)
1043 /* Project the domain of pw onto its parameter space.
1045 __isl_give PW *FN(PW,project_domain_on_params)(__isl_take PW *pw)
1050 n = FN(PW,dim)(pw, isl_dim_in);
1051 pw = FN(PW,project_out)(pw, isl_dim_in, 0, n);
1052 space = FN(PW,get_domain_space)(pw);
1053 space = isl_space_params(space);
1054 pw = FN(PW,reset_domain_space)(pw, space);
1059 #ifndef NO_INSERT_DIMS
1060 __isl_give PW *FN(PW,insert_dims)(__isl_take PW *pw, enum isl_dim_type type,
1061 unsigned first, unsigned n)
1064 enum isl_dim_type set_type;
1068 if (n == 0 && !isl_space_is_named_or_nested(pw->dim, type))
1071 set_type = type == isl_dim_in ? isl_dim_set : type;
1073 pw = FN(PW,cow)(pw);
1077 pw->dim = isl_space_insert_dims(pw->dim, type, first, n);
1081 for (i = 0; i < pw->n; ++i) {
1082 pw->p[i].set = isl_set_insert_dims(pw->p[i].set,
1083 set_type, first, n);
1086 pw->p[i].FIELD = FN(EL,insert_dims)(pw->p[i].FIELD,
1088 if (!pw->p[i].FIELD)
1099 __isl_give PW *FN(PW,fix_dim)(__isl_take PW *pw,
1100 enum isl_dim_type type, unsigned pos, isl_int v)
1107 if (type == isl_dim_in)
1110 pw = FN(PW,cow)(pw);
1113 for (i = 0; i < pw->n; ++i) {
1114 pw->p[i].set = isl_set_fix(pw->p[i].set, type, pos, v);
1125 /* Fix the value of the variable at position "pos" of type "type" of "pw"
1126 * to be equal to "v".
1128 __isl_give PW *FN(PW,fix_val)(__isl_take PW *pw,
1129 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
1132 return FN(PW,free)(pw);
1133 if (!isl_val_is_int(v))
1134 isl_die(FN(PW,get_ctx)(pw), isl_error_invalid,
1135 "expecting integer value", goto error);
1137 pw = FN(PW,fix_dim)(pw, type, pos, v->n);
1143 return FN(PW,free)(pw);
1146 unsigned FN(PW,dim)(__isl_keep PW *pw, enum isl_dim_type type)
1148 return pw ? isl_space_dim(pw->dim, type) : 0;
1151 __isl_give PW *FN(PW,split_dims)(__isl_take PW *pw,
1152 enum isl_dim_type type, unsigned first, unsigned n)
1161 if (type == isl_dim_in)
1164 pw = FN(PW,cow)(pw);
1169 for (i = 0; i < pw->n; ++i) {
1170 pw->p[i].set = isl_set_split_dims(pw->p[i].set, type, first, n);
1182 /* Compute the maximal value attained by the piecewise quasipolynomial
1183 * on its domain or zero if the domain is empty.
1184 * In the worst case, the domain is scanned completely,
1185 * so the domain is assumed to be bounded.
1187 __isl_give isl_qpolynomial *FN(PW,opt)(__isl_take PW *pw, int max)
1190 isl_qpolynomial *opt;
1196 isl_space *dim = isl_space_copy(pw->dim);
1198 return isl_qpolynomial_zero_on_domain(isl_space_domain(dim));
1201 opt = FN(EL,opt_on_domain)(FN(EL,copy)(pw->p[0].FIELD),
1202 isl_set_copy(pw->p[0].set), max);
1203 for (i = 1; i < pw->n; ++i) {
1204 isl_qpolynomial *opt_i;
1205 opt_i = FN(EL,opt_on_domain)(FN(EL,copy)(pw->p[i].FIELD),
1206 isl_set_copy(pw->p[i].set), max);
1208 opt = isl_qpolynomial_max_cst(opt, opt_i);
1210 opt = isl_qpolynomial_min_cst(opt, opt_i);
1217 __isl_give isl_qpolynomial *FN(PW,max)(__isl_take PW *pw)
1219 return FN(PW,opt)(pw, 1);
1222 __isl_give isl_qpolynomial *FN(PW,min)(__isl_take PW *pw)
1224 return FN(PW,opt)(pw, 0);
1228 __isl_give isl_space *FN(PW,get_space)(__isl_keep PW *pw)
1230 return pw ? isl_space_copy(pw->dim) : NULL;
1233 __isl_give isl_space *FN(PW,get_domain_space)(__isl_keep PW *pw)
1235 return pw ? isl_space_domain(isl_space_copy(pw->dim)) : NULL;
1238 #ifndef NO_RESET_DIM
1239 /* Reset the space of "pw". Since we don't know if the elements
1240 * represent the spaces themselves or their domains, we pass along
1241 * both when we call their reset_space_and_domain.
1243 static __isl_give PW *FN(PW,reset_space_and_domain)(__isl_take PW *pw,
1244 __isl_take isl_space *space, __isl_take isl_space *domain)
1248 pw = FN(PW,cow)(pw);
1249 if (!pw || !space || !domain)
1252 for (i = 0; i < pw->n; ++i) {
1253 pw->p[i].set = isl_set_reset_space(pw->p[i].set,
1254 isl_space_copy(domain));
1257 pw->p[i].FIELD = FN(EL,reset_space_and_domain)(pw->p[i].FIELD,
1258 isl_space_copy(space), isl_space_copy(domain));
1259 if (!pw->p[i].FIELD)
1263 isl_space_free(domain);
1265 isl_space_free(pw->dim);
1270 isl_space_free(domain);
1271 isl_space_free(space);
1276 __isl_give PW *FN(PW,reset_domain_space)(__isl_take PW *pw,
1277 __isl_take isl_space *domain)
1281 space = isl_space_extend_domain_with_range(isl_space_copy(domain),
1282 FN(PW,get_space)(pw));
1283 return FN(PW,reset_space_and_domain)(pw, space, domain);
1286 __isl_give PW *FN(PW,reset_space)(__isl_take PW *pw, __isl_take isl_space *dim)
1290 domain = isl_space_domain(isl_space_copy(dim));
1291 return FN(PW,reset_space_and_domain)(pw, dim, domain);
1294 __isl_give PW *FN(PW,set_tuple_id)(__isl_keep PW *pw, enum isl_dim_type type,
1295 __isl_take isl_id *id)
1299 pw = FN(PW,cow)(pw);
1301 return isl_id_free(id);
1303 space = FN(PW,get_space)(pw);
1304 space = isl_space_set_tuple_id(space, type, id);
1306 return FN(PW,reset_space)(pw, space);
1309 __isl_give PW *FN(PW,set_dim_id)(__isl_take PW *pw,
1310 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
1312 pw = FN(PW,cow)(pw);
1314 return isl_id_free(id);
1315 pw->dim = isl_space_set_dim_id(pw->dim, type, pos, id);
1316 return FN(PW,reset_space)(pw, isl_space_copy(pw->dim));
1320 int FN(PW,has_equal_space)(__isl_keep PW *pw1, __isl_keep PW *pw2)
1325 return isl_space_is_equal(pw1->dim, pw2->dim);
1329 __isl_give PW *FN(PW,morph_domain)(__isl_take PW *pw,
1330 __isl_take isl_morph *morph)
1338 ctx = isl_space_get_ctx(pw->dim);
1339 isl_assert(ctx, isl_space_is_domain_internal(morph->dom->dim, pw->dim),
1342 pw = FN(PW,cow)(pw);
1345 pw->dim = isl_space_extend_domain_with_range(
1346 isl_space_copy(morph->ran->dim), pw->dim);
1350 for (i = 0; i < pw->n; ++i) {
1351 pw->p[i].set = isl_morph_set(isl_morph_copy(morph), pw->p[i].set);
1354 pw->p[i].FIELD = FN(EL,morph_domain)(pw->p[i].FIELD,
1355 isl_morph_copy(morph));
1356 if (!pw->p[i].FIELD)
1360 isl_morph_free(morph);
1365 isl_morph_free(morph);
1370 int FN(PW,n_piece)(__isl_keep PW *pw)
1372 return pw ? pw->n : 0;
1375 int FN(PW,foreach_piece)(__isl_keep PW *pw,
1376 int (*fn)(__isl_take isl_set *set, __isl_take EL *el, void *user),
1384 for (i = 0; i < pw->n; ++i)
1385 if (fn(isl_set_copy(pw->p[i].set),
1386 FN(EL,copy)(pw->p[i].FIELD), user) < 0)
1393 static int any_divs(__isl_keep isl_set *set)
1400 for (i = 0; i < set->n; ++i)
1401 if (set->p[i]->n_div > 0)
1407 static int foreach_lifted_subset(__isl_take isl_set *set, __isl_take EL *el,
1408 int (*fn)(__isl_take isl_set *set, __isl_take EL *el,
1409 void *user), void *user)
1416 for (i = 0; i < set->n; ++i) {
1420 lift = isl_set_from_basic_set(isl_basic_set_copy(set->p[i]));
1421 lift = isl_set_lift(lift);
1423 copy = FN(EL,copy)(el);
1424 copy = FN(EL,lift)(copy, isl_set_get_space(lift));
1426 if (fn(lift, copy, user) < 0)
1440 int FN(PW,foreach_lifted_piece)(__isl_keep PW *pw,
1441 int (*fn)(__isl_take isl_set *set, __isl_take EL *el,
1442 void *user), void *user)
1449 for (i = 0; i < pw->n; ++i) {
1453 set = isl_set_copy(pw->p[i].set);
1454 el = FN(EL,copy)(pw->p[i].FIELD);
1455 if (!any_divs(set)) {
1456 if (fn(set, el, user) < 0)
1460 if (foreach_lifted_subset(set, el, fn, user) < 0)
1468 #ifndef NO_MOVE_DIMS
1469 __isl_give PW *FN(PW,move_dims)(__isl_take PW *pw,
1470 enum isl_dim_type dst_type, unsigned dst_pos,
1471 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
1475 pw = FN(PW,cow)(pw);
1479 pw->dim = isl_space_move_dims(pw->dim, dst_type, dst_pos, src_type, src_pos, n);
1483 for (i = 0; i < pw->n; ++i) {
1484 pw->p[i].FIELD = FN(EL,move_dims)(pw->p[i].FIELD,
1485 dst_type, dst_pos, src_type, src_pos, n);
1486 if (!pw->p[i].FIELD)
1490 if (dst_type == isl_dim_in)
1491 dst_type = isl_dim_set;
1492 if (src_type == isl_dim_in)
1493 src_type = isl_dim_set;
1495 for (i = 0; i < pw->n; ++i) {
1496 pw->p[i].set = isl_set_move_dims(pw->p[i].set,
1498 src_type, src_pos, n);
1510 __isl_give PW *FN(PW,mul_isl_int)(__isl_take PW *pw, isl_int v)
1514 if (isl_int_is_one(v))
1516 if (pw && DEFAULT_IS_ZERO && isl_int_is_zero(v)) {
1518 isl_space *dim = FN(PW,get_space)(pw);
1520 zero = FN(PW,ZERO)(dim, pw->type);
1522 zero = FN(PW,ZERO)(dim);
1527 pw = FN(PW,cow)(pw);
1534 if (isl_int_is_neg(v))
1535 pw->type = isl_fold_type_negate(pw->type);
1537 for (i = 0; i < pw->n; ++i) {
1538 pw->p[i].FIELD = FN(EL,scale)(pw->p[i].FIELD, v);
1539 if (!pw->p[i].FIELD)
1549 /* Multiply the pieces of "pw" by "v" and return the result.
1551 __isl_give PW *FN(PW,scale_val)(__isl_take PW *pw, __isl_take isl_val *v)
1558 if (isl_val_is_one(v)) {
1562 if (pw && DEFAULT_IS_ZERO && isl_val_is_zero(v)) {
1564 isl_space *space = FN(PW,get_space)(pw);
1566 zero = FN(PW,ZERO)(space, pw->type);
1568 zero = FN(PW,ZERO)(space);
1578 pw = FN(PW,cow)(pw);
1583 if (isl_val_is_neg(v))
1584 pw->type = isl_fold_type_negate(pw->type);
1586 for (i = 0; i < pw->n; ++i) {
1587 pw->p[i].FIELD = FN(EL,scale_val)(pw->p[i].FIELD,
1589 if (!pw->p[i].FIELD)
1601 __isl_give PW *FN(PW,scale)(__isl_take PW *pw, isl_int v)
1603 return FN(PW,mul_isl_int)(pw, v);
1606 static int FN(PW,qsort_set_cmp)(const void *p1, const void *p2)
1608 isl_set *set1 = *(isl_set * const *)p1;
1609 isl_set *set2 = *(isl_set * const *)p2;
1611 return isl_set_plain_cmp(set1, set2);
1614 /* We normalize in place, but if anything goes wrong we need
1615 * to return NULL, so we need to make sure we don't change the
1616 * meaning of any possible other copies of map.
1618 __isl_give PW *FN(PW,normalize)(__isl_take PW *pw)
1625 for (i = 0; i < pw->n; ++i) {
1626 set = isl_set_normalize(isl_set_copy(pw->p[i].set));
1628 return FN(PW,free)(pw);
1629 isl_set_free(pw->p[i].set);
1632 qsort(pw->p, pw->n, sizeof(pw->p[0]), &FN(PW,qsort_set_cmp));
1633 for (i = pw->n - 1; i >= 1; --i) {
1634 if (!isl_set_plain_is_equal(pw->p[i - 1].set, pw->p[i].set))
1636 if (!FN(EL,plain_is_equal)(pw->p[i - 1].FIELD, pw->p[i].FIELD))
1638 set = isl_set_union(isl_set_copy(pw->p[i - 1].set),
1639 isl_set_copy(pw->p[i].set));
1641 return FN(PW,free)(pw);
1642 isl_set_free(pw->p[i].set);
1643 FN(EL,free)(pw->p[i].FIELD);
1644 isl_set_free(pw->p[i - 1].set);
1645 pw->p[i - 1].set = set;
1646 for (j = i + 1; j < pw->n; ++j)
1647 pw->p[j - 1] = pw->p[j];
1654 /* Is pw1 obviously equal to pw2?
1655 * That is, do they have obviously identical cells and obviously identical
1656 * elements on each cell?
1658 int FN(PW,plain_is_equal)(__isl_keep PW *pw1, __isl_keep PW *pw2)
1668 if (!isl_space_is_equal(pw1->dim, pw2->dim))
1671 pw1 = FN(PW,copy)(pw1);
1672 pw2 = FN(PW,copy)(pw2);
1673 pw1 = FN(PW,normalize)(pw1);
1674 pw2 = FN(PW,normalize)(pw2);
1678 equal = pw1->n == pw2->n;
1679 for (i = 0; equal && i < pw1->n; ++i) {
1680 equal = isl_set_plain_is_equal(pw1->p[i].set, pw2->p[i].set);
1685 equal = FN(EL,plain_is_equal)(pw1->p[i].FIELD, pw2->p[i].FIELD);
1700 static __isl_give PW *FN(PW,align_params_pw_multi_aff_and)(__isl_take PW *pw,
1701 __isl_take isl_multi_aff *ma,
1702 __isl_give PW *(*fn)(__isl_take PW *pw, __isl_take isl_multi_aff *ma))
1705 isl_space *ma_space;
1707 ma_space = isl_multi_aff_get_space(ma);
1708 if (!pw || !ma || !ma_space)
1710 if (isl_space_match(pw->dim, isl_dim_param, ma_space, isl_dim_param)) {
1711 isl_space_free(ma_space);
1714 ctx = FN(PW,get_ctx)(pw);
1715 if (!isl_space_has_named_params(pw->dim) ||
1716 !isl_space_has_named_params(ma_space))
1717 isl_die(ctx, isl_error_invalid,
1718 "unaligned unnamed parameters", goto error);
1719 pw = FN(PW,align_params)(pw, ma_space);
1720 ma = isl_multi_aff_align_params(ma, FN(PW,get_space)(pw));
1723 isl_space_free(ma_space);
1725 isl_multi_aff_free(ma);
1729 static __isl_give PW *FN(PW,align_params_pw_pw_multi_aff_and)(__isl_take PW *pw,
1730 __isl_take isl_pw_multi_aff *pma,
1731 __isl_give PW *(*fn)(__isl_take PW *pw,
1732 __isl_take isl_pw_multi_aff *ma))
1735 isl_space *pma_space;
1737 pma_space = isl_pw_multi_aff_get_space(pma);
1738 if (!pw || !pma || !pma_space)
1740 if (isl_space_match(pw->dim, isl_dim_param, pma_space, isl_dim_param)) {
1741 isl_space_free(pma_space);
1744 ctx = FN(PW,get_ctx)(pw);
1745 if (!isl_space_has_named_params(pw->dim) ||
1746 !isl_space_has_named_params(pma_space))
1747 isl_die(ctx, isl_error_invalid,
1748 "unaligned unnamed parameters", goto error);
1749 pw = FN(PW,align_params)(pw, pma_space);
1750 pma = isl_pw_multi_aff_align_params(pma, FN(PW,get_space)(pw));
1753 isl_space_free(pma_space);
1755 isl_pw_multi_aff_free(pma);
1759 /* Compute the pullback of "pw" by the function represented by "ma".
1760 * In other words, plug in "ma" in "pw".
1762 static __isl_give PW *FN(PW,pullback_multi_aff_aligned)(__isl_take PW *pw,
1763 __isl_take isl_multi_aff *ma)
1766 isl_space *space = NULL;
1768 ma = isl_multi_aff_align_divs(ma);
1769 pw = FN(PW,cow)(pw);
1773 space = isl_space_join(isl_multi_aff_get_space(ma),
1774 FN(PW,get_space)(pw));
1776 for (i = 0; i < pw->n; ++i) {
1777 pw->p[i].set = isl_set_preimage_multi_aff(pw->p[i].set,
1778 isl_multi_aff_copy(ma));
1781 pw->p[i].FIELD = FN(EL,pullback_multi_aff)(pw->p[i].FIELD,
1782 isl_multi_aff_copy(ma));
1783 if (!pw->p[i].FIELD)
1787 pw = FN(PW,reset_space)(pw, space);
1788 isl_multi_aff_free(ma);
1791 isl_space_free(space);
1792 isl_multi_aff_free(ma);
1797 __isl_give PW *FN(PW,pullback_multi_aff)(__isl_take PW *pw,
1798 __isl_take isl_multi_aff *ma)
1800 return FN(PW,align_params_pw_multi_aff_and)(pw, ma,
1801 &FN(PW,pullback_multi_aff_aligned));
1804 /* Compute the pullback of "pw" by the function represented by "pma".
1805 * In other words, plug in "pma" in "pw".
1807 static __isl_give PW *FN(PW,pullback_pw_multi_aff_aligned)(__isl_take PW *pw,
1808 __isl_take isl_pw_multi_aff *pma)
1817 isl_pw_multi_aff_free(pma);
1818 res = FN(PW,empty)(FN(PW,get_space)(pw));
1823 res = FN(PW,pullback_multi_aff)(FN(PW,copy)(pw),
1824 isl_multi_aff_copy(pma->p[0].maff));
1825 res = FN(PW,intersect_domain)(res, isl_set_copy(pma->p[0].set));
1827 for (i = 1; i < pma->n; ++i) {
1830 res_i = FN(PW,pullback_multi_aff)(FN(PW,copy)(pw),
1831 isl_multi_aff_copy(pma->p[i].maff));
1832 res_i = FN(PW,intersect_domain)(res_i,
1833 isl_set_copy(pma->p[i].set));
1834 res = FN(PW,add_disjoint)(res, res_i);
1837 isl_pw_multi_aff_free(pma);
1841 isl_pw_multi_aff_free(pma);
1846 __isl_give PW *FN(PW,pullback_pw_multi_aff)(__isl_take PW *pw,
1847 __isl_take isl_pw_multi_aff *pma)
1849 return FN(PW,align_params_pw_pw_multi_aff_and)(pw, pma,
1850 &FN(PW,pullback_pw_multi_aff_aligned));