2 * Copyright 2010 INRIA Saclay
4 * Use of this software is governed by the MIT license
6 * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
7 * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
12 #include <isl_map_private.h>
13 #include <isl_union_map_private.h>
14 #include <isl_polynomial_private.h>
15 #include <isl_point_private.h>
16 #include <isl_space_private.h>
19 #include <isl_mat_private.h>
20 #include <isl_val_private.h>
21 #include <isl_config.h>
23 enum isl_fold isl_fold_type_negate(enum isl_fold type)
34 isl_die(NULL, isl_error_internal, "unhandled isl_fold type", abort());
37 static __isl_give isl_qpolynomial_fold *qpolynomial_fold_alloc(
38 enum isl_fold type, __isl_take isl_space *dim, int n)
40 isl_qpolynomial_fold *fold;
45 isl_assert(dim->ctx, n >= 0, goto error);
46 fold = isl_calloc(dim->ctx, struct isl_qpolynomial_fold,
47 sizeof(struct isl_qpolynomial_fold) +
48 (n - 1) * sizeof(struct isl_qpolynomial *));
64 isl_ctx *isl_qpolynomial_fold_get_ctx(__isl_keep isl_qpolynomial_fold *fold)
66 return fold ? fold->dim->ctx : NULL;
69 __isl_give isl_space *isl_qpolynomial_fold_get_domain_space(
70 __isl_keep isl_qpolynomial_fold *fold)
72 return fold ? isl_space_copy(fold->dim) : NULL;
75 __isl_give isl_space *isl_qpolynomial_fold_get_space(
76 __isl_keep isl_qpolynomial_fold *fold)
81 space = isl_space_copy(fold->dim);
82 space = isl_space_from_domain(space);
83 space = isl_space_add_dims(space, isl_dim_out, 1);
87 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_reset_domain_space(
88 __isl_take isl_qpolynomial_fold *fold, __isl_take isl_space *dim)
92 fold = isl_qpolynomial_fold_cow(fold);
96 for (i = 0; i < fold->n; ++i) {
97 fold->qp[i] = isl_qpolynomial_reset_domain_space(fold->qp[i],
103 isl_space_free(fold->dim);
108 isl_qpolynomial_fold_free(fold);
113 /* Reset the space of "fold". This function is called from isl_pw_templ.c
114 * and doesn't know if the space of an element object is represented
115 * directly or through its domain. It therefore passes along both.
117 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_reset_space_and_domain(
118 __isl_take isl_qpolynomial_fold *fold, __isl_take isl_space *space,
119 __isl_take isl_space *domain)
121 isl_space_free(space);
122 return isl_qpolynomial_fold_reset_domain_space(fold, domain);
125 int isl_qpolynomial_fold_involves_dims(__isl_keep isl_qpolynomial_fold *fold,
126 enum isl_dim_type type, unsigned first, unsigned n)
132 if (fold->n == 0 || n == 0)
135 for (i = 0; i < fold->n; ++i) {
136 int involves = isl_qpolynomial_involves_dims(fold->qp[i],
138 if (involves < 0 || involves)
144 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_set_dim_name(
145 __isl_take isl_qpolynomial_fold *fold,
146 enum isl_dim_type type, unsigned pos, const char *s)
150 fold = isl_qpolynomial_fold_cow(fold);
153 fold->dim = isl_space_set_dim_name(fold->dim, type, pos, s);
157 for (i = 0; i < fold->n; ++i) {
158 fold->qp[i] = isl_qpolynomial_set_dim_name(fold->qp[i],
166 isl_qpolynomial_fold_free(fold);
170 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_drop_dims(
171 __isl_take isl_qpolynomial_fold *fold,
172 enum isl_dim_type type, unsigned first, unsigned n)
175 enum isl_dim_type set_type;
182 set_type = type == isl_dim_in ? isl_dim_set : type;
184 fold = isl_qpolynomial_fold_cow(fold);
187 fold->dim = isl_space_drop_dims(fold->dim, set_type, first, n);
191 for (i = 0; i < fold->n; ++i) {
192 fold->qp[i] = isl_qpolynomial_drop_dims(fold->qp[i],
200 isl_qpolynomial_fold_free(fold);
204 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_insert_dims(
205 __isl_take isl_qpolynomial_fold *fold,
206 enum isl_dim_type type, unsigned first, unsigned n)
212 if (n == 0 && !isl_space_is_named_or_nested(fold->dim, type))
215 fold = isl_qpolynomial_fold_cow(fold);
218 fold->dim = isl_space_insert_dims(fold->dim, type, first, n);
222 for (i = 0; i < fold->n; ++i) {
223 fold->qp[i] = isl_qpolynomial_insert_dims(fold->qp[i],
231 isl_qpolynomial_fold_free(fold);
235 static int isl_qpolynomial_cst_sign(__isl_keep isl_qpolynomial *qp)
237 struct isl_upoly_cst *cst;
239 cst = isl_upoly_as_cst(qp->upoly);
243 return isl_int_sgn(cst->n) < 0 ? -1 : 1;
246 static int isl_qpolynomial_aff_sign(__isl_keep isl_set *set,
247 __isl_keep isl_qpolynomial *qp)
249 enum isl_lp_result res;
254 aff = isl_qpolynomial_extract_affine(qp);
260 res = isl_set_solve_lp(set, 0, aff->el + 1, aff->el[0],
262 if (res == isl_lp_error)
264 if (res == isl_lp_empty ||
265 (res == isl_lp_ok && !isl_int_is_neg(opt))) {
270 res = isl_set_solve_lp(set, 1, aff->el + 1, aff->el[0],
272 if (res == isl_lp_ok && !isl_int_is_pos(opt))
281 /* Determine, if possible, the sign of the quasipolynomial "qp" on
284 * If qp is a constant, then the problem is trivial.
285 * If qp is linear, then we check if the minimum of the corresponding
286 * affine constraint is non-negative or if the maximum is non-positive.
288 * Otherwise, we check if the outermost variable "v" has a lower bound "l"
289 * in "set". If so, we write qp(v,v') as
291 * q(v,v') * (v - l) + r(v')
293 * if q(v,v') and r(v') have the same known sign, then the original
294 * quasipolynomial has the same sign as well.
301 static int isl_qpolynomial_sign(__isl_keep isl_set *set,
302 __isl_keep isl_qpolynomial *qp)
307 struct isl_upoly_rec *rec;
310 enum isl_lp_result res;
313 is = isl_qpolynomial_is_cst(qp, NULL, NULL);
317 return isl_qpolynomial_cst_sign(qp);
319 is = isl_qpolynomial_is_affine(qp);
323 return isl_qpolynomial_aff_sign(set, qp);
325 if (qp->div->n_row > 0)
328 rec = isl_upoly_as_rec(qp->upoly);
332 d = isl_space_dim(qp->dim, isl_dim_all);
333 v = isl_vec_alloc(set->ctx, 2 + d);
337 isl_seq_clr(v->el + 1, 1 + d);
338 isl_int_set_si(v->el[0], 1);
339 isl_int_set_si(v->el[2 + qp->upoly->var], 1);
343 res = isl_set_solve_lp(set, 0, v->el + 1, v->el[0], &l, NULL, NULL);
344 if (res == isl_lp_ok) {
345 isl_qpolynomial *min;
346 isl_qpolynomial *base;
347 isl_qpolynomial *r, *q;
350 min = isl_qpolynomial_cst_on_domain(isl_space_copy(qp->dim), l);
351 base = isl_qpolynomial_var_pow_on_domain(isl_space_copy(qp->dim),
354 r = isl_qpolynomial_alloc(isl_space_copy(qp->dim), 0,
355 isl_upoly_copy(rec->p[rec->n - 1]));
356 q = isl_qpolynomial_copy(r);
358 for (i = rec->n - 2; i >= 0; --i) {
359 r = isl_qpolynomial_mul(r, isl_qpolynomial_copy(min));
360 t = isl_qpolynomial_alloc(isl_space_copy(qp->dim), 0,
361 isl_upoly_copy(rec->p[i]));
362 r = isl_qpolynomial_add(r, t);
365 q = isl_qpolynomial_mul(q, isl_qpolynomial_copy(base));
366 q = isl_qpolynomial_add(q, isl_qpolynomial_copy(r));
369 if (isl_qpolynomial_is_zero(q))
370 sgn = isl_qpolynomial_sign(set, r);
371 else if (isl_qpolynomial_is_zero(r))
372 sgn = isl_qpolynomial_sign(set, q);
375 sgn_r = isl_qpolynomial_sign(set, r);
376 sgn_q = isl_qpolynomial_sign(set, q);
381 isl_qpolynomial_free(min);
382 isl_qpolynomial_free(base);
383 isl_qpolynomial_free(q);
384 isl_qpolynomial_free(r);
394 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_fold_on_domain(
395 __isl_keep isl_set *set,
396 __isl_take isl_qpolynomial_fold *fold1,
397 __isl_take isl_qpolynomial_fold *fold2)
401 struct isl_qpolynomial_fold *res = NULL;
404 if (!fold1 || !fold2)
407 isl_assert(fold1->dim->ctx, fold1->type == fold2->type, goto error);
408 isl_assert(fold1->dim->ctx, isl_space_is_equal(fold1->dim, fold2->dim),
411 better = fold1->type == isl_fold_max ? -1 : 1;
413 if (isl_qpolynomial_fold_is_empty(fold1)) {
414 isl_qpolynomial_fold_free(fold1);
418 if (isl_qpolynomial_fold_is_empty(fold2)) {
419 isl_qpolynomial_fold_free(fold2);
423 res = qpolynomial_fold_alloc(fold1->type, isl_space_copy(fold1->dim),
424 fold1->n + fold2->n);
428 for (i = 0; i < fold1->n; ++i) {
429 res->qp[res->n] = isl_qpolynomial_copy(fold1->qp[i]);
430 if (!res->qp[res->n])
436 for (i = 0; i < fold2->n; ++i) {
437 for (j = n1 - 1; j >= 0; --j) {
440 d = isl_qpolynomial_sub(
441 isl_qpolynomial_copy(res->qp[j]),
442 isl_qpolynomial_copy(fold2->qp[i]));
443 sgn = isl_qpolynomial_sign(set, d);
444 isl_qpolynomial_free(d);
449 isl_qpolynomial_free(res->qp[j]);
451 res->qp[j] = res->qp[n1 - 1];
453 if (n1 != res->n - 1)
454 res->qp[n1] = res->qp[res->n - 1];
459 res->qp[res->n] = isl_qpolynomial_copy(fold2->qp[i]);
460 if (!res->qp[res->n])
465 isl_qpolynomial_fold_free(fold1);
466 isl_qpolynomial_fold_free(fold2);
470 isl_qpolynomial_fold_free(res);
471 isl_qpolynomial_fold_free(fold1);
472 isl_qpolynomial_fold_free(fold2);
476 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_add_qpolynomial(
477 __isl_take isl_qpolynomial_fold *fold, __isl_take isl_qpolynomial *qp)
484 if (isl_qpolynomial_is_zero(qp)) {
485 isl_qpolynomial_free(qp);
489 fold = isl_qpolynomial_fold_cow(fold);
493 for (i = 0; i < fold->n; ++i) {
494 fold->qp[i] = isl_qpolynomial_add(fold->qp[i],
495 isl_qpolynomial_copy(qp));
500 isl_qpolynomial_free(qp);
503 isl_qpolynomial_fold_free(fold);
504 isl_qpolynomial_free(qp);
508 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_add_on_domain(
509 __isl_keep isl_set *dom,
510 __isl_take isl_qpolynomial_fold *fold1,
511 __isl_take isl_qpolynomial_fold *fold2)
514 isl_qpolynomial_fold *res = NULL;
516 if (!fold1 || !fold2)
519 if (isl_qpolynomial_fold_is_empty(fold1)) {
520 isl_qpolynomial_fold_free(fold1);
524 if (isl_qpolynomial_fold_is_empty(fold2)) {
525 isl_qpolynomial_fold_free(fold2);
529 if (fold1->n == 1 && fold2->n != 1)
530 return isl_qpolynomial_fold_add_on_domain(dom, fold2, fold1);
533 res = isl_qpolynomial_fold_add_qpolynomial(fold1,
534 isl_qpolynomial_copy(fold2->qp[0]));
535 isl_qpolynomial_fold_free(fold2);
539 res = isl_qpolynomial_fold_add_qpolynomial(
540 isl_qpolynomial_fold_copy(fold1),
541 isl_qpolynomial_copy(fold2->qp[0]));
543 for (i = 1; i < fold2->n; ++i) {
544 isl_qpolynomial_fold *res_i;
545 res_i = isl_qpolynomial_fold_add_qpolynomial(
546 isl_qpolynomial_fold_copy(fold1),
547 isl_qpolynomial_copy(fold2->qp[i]));
548 res = isl_qpolynomial_fold_fold_on_domain(dom, res, res_i);
551 isl_qpolynomial_fold_free(fold1);
552 isl_qpolynomial_fold_free(fold2);
555 isl_qpolynomial_fold_free(res);
556 isl_qpolynomial_fold_free(fold1);
557 isl_qpolynomial_fold_free(fold2);
561 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_substitute_equalities(
562 __isl_take isl_qpolynomial_fold *fold, __isl_take isl_basic_set *eq)
569 fold = isl_qpolynomial_fold_cow(fold);
573 for (i = 0; i < fold->n; ++i) {
574 fold->qp[i] = isl_qpolynomial_substitute_equalities(fold->qp[i],
575 isl_basic_set_copy(eq));
580 isl_basic_set_free(eq);
583 isl_basic_set_free(eq);
584 isl_qpolynomial_fold_free(fold);
588 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_gist(
589 __isl_take isl_qpolynomial_fold *fold, __isl_take isl_set *context)
593 if (!fold || !context)
596 fold = isl_qpolynomial_fold_cow(fold);
600 for (i = 0; i < fold->n; ++i) {
601 fold->qp[i] = isl_qpolynomial_gist(fold->qp[i],
602 isl_set_copy(context));
607 isl_set_free(context);
610 isl_set_free(context);
611 isl_qpolynomial_fold_free(fold);
615 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_gist_params(
616 __isl_take isl_qpolynomial_fold *fold, __isl_take isl_set *context)
618 isl_space *space = isl_qpolynomial_fold_get_domain_space(fold);
619 isl_set *dom_context = isl_set_universe(space);
620 dom_context = isl_set_intersect_params(dom_context, context);
621 return isl_qpolynomial_fold_gist(fold, dom_context);
627 #define PW isl_pw_qpolynomial_fold
629 #define EL isl_qpolynomial_fold
631 #define EL_IS_ZERO is_empty
635 #define IS_ZERO is_zero
638 #undef DEFAULT_IS_ZERO
639 #define DEFAULT_IS_ZERO 1
644 #include <isl_pw_templ.c>
647 #define UNION isl_union_pw_qpolynomial_fold
649 #define PART isl_pw_qpolynomial_fold
651 #define PARTS pw_qpolynomial_fold
656 #include <isl_union_templ.c>
658 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_empty(enum isl_fold type,
659 __isl_take isl_space *dim)
661 return qpolynomial_fold_alloc(type, dim, 0);
664 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_alloc(
665 enum isl_fold type, __isl_take isl_qpolynomial *qp)
667 isl_qpolynomial_fold *fold;
672 fold = qpolynomial_fold_alloc(type, isl_space_copy(qp->dim), 1);
681 isl_qpolynomial_fold_free(fold);
682 isl_qpolynomial_free(qp);
686 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_copy(
687 __isl_keep isl_qpolynomial_fold *fold)
696 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_dup(
697 __isl_keep isl_qpolynomial_fold *fold)
700 isl_qpolynomial_fold *dup;
704 dup = qpolynomial_fold_alloc(fold->type,
705 isl_space_copy(fold->dim), fold->n);
710 for (i = 0; i < fold->n; ++i) {
711 dup->qp[i] = isl_qpolynomial_copy(fold->qp[i]);
718 isl_qpolynomial_fold_free(dup);
722 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_cow(
723 __isl_take isl_qpolynomial_fold *fold)
731 return isl_qpolynomial_fold_dup(fold);
734 void isl_qpolynomial_fold_free(__isl_take isl_qpolynomial_fold *fold)
743 for (i = 0; i < fold->n; ++i)
744 isl_qpolynomial_free(fold->qp[i]);
745 isl_space_free(fold->dim);
749 int isl_qpolynomial_fold_is_empty(__isl_keep isl_qpolynomial_fold *fold)
757 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_fold(
758 __isl_take isl_qpolynomial_fold *fold1,
759 __isl_take isl_qpolynomial_fold *fold2)
762 struct isl_qpolynomial_fold *res = NULL;
764 if (!fold1 || !fold2)
767 isl_assert(fold1->dim->ctx, fold1->type == fold2->type, goto error);
768 isl_assert(fold1->dim->ctx, isl_space_is_equal(fold1->dim, fold2->dim),
771 if (isl_qpolynomial_fold_is_empty(fold1)) {
772 isl_qpolynomial_fold_free(fold1);
776 if (isl_qpolynomial_fold_is_empty(fold2)) {
777 isl_qpolynomial_fold_free(fold2);
781 res = qpolynomial_fold_alloc(fold1->type, isl_space_copy(fold1->dim),
782 fold1->n + fold2->n);
786 for (i = 0; i < fold1->n; ++i) {
787 res->qp[res->n] = isl_qpolynomial_copy(fold1->qp[i]);
788 if (!res->qp[res->n])
793 for (i = 0; i < fold2->n; ++i) {
794 res->qp[res->n] = isl_qpolynomial_copy(fold2->qp[i]);
795 if (!res->qp[res->n])
800 isl_qpolynomial_fold_free(fold1);
801 isl_qpolynomial_fold_free(fold2);
805 isl_qpolynomial_fold_free(res);
806 isl_qpolynomial_fold_free(fold1);
807 isl_qpolynomial_fold_free(fold2);
811 __isl_give isl_pw_qpolynomial_fold *isl_pw_qpolynomial_fold_fold(
812 __isl_take isl_pw_qpolynomial_fold *pw1,
813 __isl_take isl_pw_qpolynomial_fold *pw2)
816 struct isl_pw_qpolynomial_fold *res;
822 isl_assert(pw1->dim->ctx, isl_space_is_equal(pw1->dim, pw2->dim), goto error);
824 if (isl_pw_qpolynomial_fold_is_zero(pw1)) {
825 isl_pw_qpolynomial_fold_free(pw1);
829 if (isl_pw_qpolynomial_fold_is_zero(pw2)) {
830 isl_pw_qpolynomial_fold_free(pw2);
834 if (pw1->type != pw2->type)
835 isl_die(pw1->dim->ctx, isl_error_invalid,
836 "fold types don't match", goto error);
838 n = (pw1->n + 1) * (pw2->n + 1);
839 res = isl_pw_qpolynomial_fold_alloc_size(isl_space_copy(pw1->dim),
842 for (i = 0; i < pw1->n; ++i) {
843 set = isl_set_copy(pw1->p[i].set);
844 for (j = 0; j < pw2->n; ++j) {
845 struct isl_set *common;
846 isl_qpolynomial_fold *sum;
847 set = isl_set_subtract(set,
848 isl_set_copy(pw2->p[j].set));
849 common = isl_set_intersect(isl_set_copy(pw1->p[i].set),
850 isl_set_copy(pw2->p[j].set));
851 if (isl_set_plain_is_empty(common)) {
852 isl_set_free(common);
856 sum = isl_qpolynomial_fold_fold_on_domain(common,
857 isl_qpolynomial_fold_copy(pw1->p[i].fold),
858 isl_qpolynomial_fold_copy(pw2->p[j].fold));
860 res = isl_pw_qpolynomial_fold_add_piece(res, common, sum);
862 res = isl_pw_qpolynomial_fold_add_piece(res, set,
863 isl_qpolynomial_fold_copy(pw1->p[i].fold));
866 for (j = 0; j < pw2->n; ++j) {
867 set = isl_set_copy(pw2->p[j].set);
868 for (i = 0; i < pw1->n; ++i)
869 set = isl_set_subtract(set, isl_set_copy(pw1->p[i].set));
870 res = isl_pw_qpolynomial_fold_add_piece(res, set,
871 isl_qpolynomial_fold_copy(pw2->p[j].fold));
874 isl_pw_qpolynomial_fold_free(pw1);
875 isl_pw_qpolynomial_fold_free(pw2);
879 isl_pw_qpolynomial_fold_free(pw1);
880 isl_pw_qpolynomial_fold_free(pw2);
884 __isl_give isl_union_pw_qpolynomial_fold *isl_union_pw_qpolynomial_fold_fold_pw_qpolynomial_fold(
885 __isl_take isl_union_pw_qpolynomial_fold *u,
886 __isl_take isl_pw_qpolynomial_fold *part)
889 struct isl_hash_table_entry *entry;
891 u = isl_union_pw_qpolynomial_fold_cow(u);
896 isl_assert(u->dim->ctx, isl_space_match(part->dim, isl_dim_param, u->dim,
897 isl_dim_param), goto error);
899 hash = isl_space_get_hash(part->dim);
900 entry = isl_hash_table_find(u->dim->ctx, &u->table, hash,
901 &has_dim, part->dim, 1);
908 entry->data = isl_pw_qpolynomial_fold_fold(entry->data,
909 isl_pw_qpolynomial_fold_copy(part));
912 isl_pw_qpolynomial_fold_free(part);
917 isl_pw_qpolynomial_fold_free(part);
918 isl_union_pw_qpolynomial_fold_free(u);
922 static int fold_part(__isl_take isl_pw_qpolynomial_fold *part, void *user)
924 isl_union_pw_qpolynomial_fold **u;
925 u = (isl_union_pw_qpolynomial_fold **)user;
927 *u = isl_union_pw_qpolynomial_fold_fold_pw_qpolynomial_fold(*u, part);
932 __isl_give isl_union_pw_qpolynomial_fold *isl_union_pw_qpolynomial_fold_fold(
933 __isl_take isl_union_pw_qpolynomial_fold *u1,
934 __isl_take isl_union_pw_qpolynomial_fold *u2)
936 u1 = isl_union_pw_qpolynomial_fold_cow(u1);
941 if (isl_union_pw_qpolynomial_fold_foreach_pw_qpolynomial_fold(u2,
942 &fold_part, &u1) < 0)
945 isl_union_pw_qpolynomial_fold_free(u2);
949 isl_union_pw_qpolynomial_fold_free(u1);
950 isl_union_pw_qpolynomial_fold_free(u2);
954 __isl_give isl_pw_qpolynomial_fold *isl_pw_qpolynomial_fold_from_pw_qpolynomial(
955 enum isl_fold type, __isl_take isl_pw_qpolynomial *pwqp)
958 isl_pw_qpolynomial_fold *pwf;
963 pwf = isl_pw_qpolynomial_fold_alloc_size(isl_space_copy(pwqp->dim),
966 for (i = 0; i < pwqp->n; ++i)
967 pwf = isl_pw_qpolynomial_fold_add_piece(pwf,
968 isl_set_copy(pwqp->p[i].set),
969 isl_qpolynomial_fold_alloc(type,
970 isl_qpolynomial_copy(pwqp->p[i].qp)));
972 isl_pw_qpolynomial_free(pwqp);
977 __isl_give isl_pw_qpolynomial_fold *isl_pw_qpolynomial_fold_add(
978 __isl_take isl_pw_qpolynomial_fold *pwf1,
979 __isl_take isl_pw_qpolynomial_fold *pwf2)
981 return isl_pw_qpolynomial_fold_union_add_(pwf1, pwf2);
984 int isl_qpolynomial_fold_plain_is_equal(__isl_keep isl_qpolynomial_fold *fold1,
985 __isl_keep isl_qpolynomial_fold *fold2)
989 if (!fold1 || !fold2)
992 if (fold1->n != fold2->n)
995 /* We probably want to sort the qps first... */
996 for (i = 0; i < fold1->n; ++i) {
997 int eq = isl_qpolynomial_plain_is_equal(fold1->qp[i], fold2->qp[i]);
1005 __isl_give isl_qpolynomial *isl_qpolynomial_fold_eval(
1006 __isl_take isl_qpolynomial_fold *fold, __isl_take isl_point *pnt)
1008 isl_qpolynomial *qp;
1012 isl_assert(pnt->dim->ctx, isl_space_is_equal(pnt->dim, fold->dim), goto error);
1013 isl_assert(pnt->dim->ctx,
1014 fold->type == isl_fold_max || fold->type == isl_fold_min,
1018 qp = isl_qpolynomial_zero_on_domain(isl_space_copy(fold->dim));
1021 qp = isl_qpolynomial_eval(isl_qpolynomial_copy(fold->qp[0]),
1022 isl_point_copy(pnt));
1023 for (i = 1; i < fold->n; ++i) {
1024 isl_qpolynomial *qp_i;
1025 qp_i = isl_qpolynomial_eval(
1026 isl_qpolynomial_copy(fold->qp[i]),
1027 isl_point_copy(pnt));
1028 if (fold->type == isl_fold_max)
1029 qp = isl_qpolynomial_max_cst(qp, qp_i);
1031 qp = isl_qpolynomial_min_cst(qp, qp_i);
1034 isl_qpolynomial_fold_free(fold);
1035 isl_point_free(pnt);
1039 isl_qpolynomial_fold_free(fold);
1040 isl_point_free(pnt);
1044 size_t isl_pw_qpolynomial_fold_size(__isl_keep isl_pw_qpolynomial_fold *pwf)
1049 for (i = 0; i < pwf->n; ++i)
1050 n += pwf->p[i].fold->n;
1055 __isl_give isl_qpolynomial *isl_qpolynomial_fold_opt_on_domain(
1056 __isl_take isl_qpolynomial_fold *fold, __isl_take isl_set *set, int max)
1059 isl_qpolynomial *opt;
1065 isl_space *dim = isl_space_copy(fold->dim);
1067 isl_qpolynomial_fold_free(fold);
1068 return isl_qpolynomial_zero_on_domain(dim);
1071 opt = isl_qpolynomial_opt_on_domain(isl_qpolynomial_copy(fold->qp[0]),
1072 isl_set_copy(set), max);
1073 for (i = 1; i < fold->n; ++i) {
1074 isl_qpolynomial *opt_i;
1075 opt_i = isl_qpolynomial_opt_on_domain(
1076 isl_qpolynomial_copy(fold->qp[i]),
1077 isl_set_copy(set), max);
1079 opt = isl_qpolynomial_max_cst(opt, opt_i);
1081 opt = isl_qpolynomial_min_cst(opt, opt_i);
1085 isl_qpolynomial_fold_free(fold);
1090 isl_qpolynomial_fold_free(fold);
1094 /* Check whether for each quasi-polynomial in "fold2" there is
1095 * a quasi-polynomial in "fold1" that dominates it on "set".
1097 static int qpolynomial_fold_covers_on_domain(__isl_keep isl_set *set,
1098 __isl_keep isl_qpolynomial_fold *fold1,
1099 __isl_keep isl_qpolynomial_fold *fold2)
1104 if (!set || !fold1 || !fold2)
1107 covers = fold1->type == isl_fold_max ? 1 : -1;
1109 for (i = 0; i < fold2->n; ++i) {
1110 for (j = 0; j < fold1->n; ++j) {
1114 d = isl_qpolynomial_sub(
1115 isl_qpolynomial_copy(fold1->qp[j]),
1116 isl_qpolynomial_copy(fold2->qp[i]));
1117 sgn = isl_qpolynomial_sign(set, d);
1118 isl_qpolynomial_free(d);
1129 /* Check whether "pwf1" dominated "pwf2", i.e., the domain of "pwf1" contains
1130 * that of "pwf2" and on each cell, the corresponding fold from pwf1 dominates
1133 int isl_pw_qpolynomial_fold_covers(__isl_keep isl_pw_qpolynomial_fold *pwf1,
1134 __isl_keep isl_pw_qpolynomial_fold *pwf2)
1137 isl_set *dom1, *dom2;
1148 dom1 = isl_pw_qpolynomial_fold_domain(isl_pw_qpolynomial_fold_copy(pwf1));
1149 dom2 = isl_pw_qpolynomial_fold_domain(isl_pw_qpolynomial_fold_copy(pwf2));
1150 is_subset = isl_set_is_subset(dom2, dom1);
1154 if (is_subset < 0 || !is_subset)
1157 for (i = 0; i < pwf2->n; ++i) {
1158 for (j = 0; j < pwf1->n; ++j) {
1163 common = isl_set_intersect(isl_set_copy(pwf1->p[j].set),
1164 isl_set_copy(pwf2->p[i].set));
1165 is_empty = isl_set_is_empty(common);
1166 if (is_empty < 0 || is_empty) {
1167 isl_set_free(common);
1172 covers = qpolynomial_fold_covers_on_domain(common,
1173 pwf1->p[j].fold, pwf2->p[i].fold);
1174 isl_set_free(common);
1175 if (covers < 0 || !covers)
1183 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_morph_domain(
1184 __isl_take isl_qpolynomial_fold *fold, __isl_take isl_morph *morph)
1189 if (!fold || !morph)
1192 ctx = fold->dim->ctx;
1193 isl_assert(ctx, isl_space_is_equal(fold->dim, morph->dom->dim), goto error);
1195 fold = isl_qpolynomial_fold_cow(fold);
1199 isl_space_free(fold->dim);
1200 fold->dim = isl_space_copy(morph->ran->dim);
1204 for (i = 0; i < fold->n; ++i) {
1205 fold->qp[i] = isl_qpolynomial_morph_domain(fold->qp[i],
1206 isl_morph_copy(morph));
1211 isl_morph_free(morph);
1215 isl_qpolynomial_fold_free(fold);
1216 isl_morph_free(morph);
1220 enum isl_fold isl_qpolynomial_fold_get_type(__isl_keep isl_qpolynomial_fold *fold)
1223 return isl_fold_list;
1227 enum isl_fold isl_union_pw_qpolynomial_fold_get_type(
1228 __isl_keep isl_union_pw_qpolynomial_fold *upwf)
1231 return isl_fold_list;
1235 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_lift(
1236 __isl_take isl_qpolynomial_fold *fold, __isl_take isl_space *dim)
1243 if (isl_space_is_equal(fold->dim, dim)) {
1244 isl_space_free(dim);
1248 fold = isl_qpolynomial_fold_cow(fold);
1252 isl_space_free(fold->dim);
1253 fold->dim = isl_space_copy(dim);
1257 for (i = 0; i < fold->n; ++i) {
1258 fold->qp[i] = isl_qpolynomial_lift(fold->qp[i],
1259 isl_space_copy(dim));
1264 isl_space_free(dim);
1268 isl_qpolynomial_fold_free(fold);
1269 isl_space_free(dim);
1273 int isl_qpolynomial_fold_foreach_qpolynomial(
1274 __isl_keep isl_qpolynomial_fold *fold,
1275 int (*fn)(__isl_take isl_qpolynomial *qp, void *user), void *user)
1282 for (i = 0; i < fold->n; ++i)
1283 if (fn(isl_qpolynomial_copy(fold->qp[i]), user) < 0)
1289 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_move_dims(
1290 __isl_take isl_qpolynomial_fold *fold,
1291 enum isl_dim_type dst_type, unsigned dst_pos,
1292 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
1299 fold = isl_qpolynomial_fold_cow(fold);
1303 fold->dim = isl_space_move_dims(fold->dim, dst_type, dst_pos,
1304 src_type, src_pos, n);
1308 for (i = 0; i < fold->n; ++i) {
1309 fold->qp[i] = isl_qpolynomial_move_dims(fold->qp[i],
1310 dst_type, dst_pos, src_type, src_pos, n);
1317 isl_qpolynomial_fold_free(fold);
1321 /* For each 0 <= i < "n", replace variable "first" + i of type "type"
1322 * in fold->qp[k] by subs[i].
1324 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_substitute(
1325 __isl_take isl_qpolynomial_fold *fold,
1326 enum isl_dim_type type, unsigned first, unsigned n,
1327 __isl_keep isl_qpolynomial **subs)
1334 fold = isl_qpolynomial_fold_cow(fold);
1338 for (i = 0; i < fold->n; ++i) {
1339 fold->qp[i] = isl_qpolynomial_substitute(fold->qp[i],
1340 type, first, n, subs);
1347 isl_qpolynomial_fold_free(fold);
1351 static int add_pwqp(__isl_take isl_pw_qpolynomial *pwqp, void *user)
1354 isl_pw_qpolynomial_fold *pwf;
1355 isl_union_pw_qpolynomial_fold **upwf;
1357 struct isl_hash_table_entry *entry;
1359 upwf = (isl_union_pw_qpolynomial_fold **)user;
1361 ctx = pwqp->dim->ctx;
1362 hash = isl_space_get_hash(pwqp->dim);
1363 entry = isl_hash_table_find(ctx, &(*upwf)->table,
1364 hash, &has_dim, pwqp->dim, 1);
1368 pwf = isl_pw_qpolynomial_fold_from_pw_qpolynomial((*upwf)->type, pwqp);
1372 entry->data = isl_pw_qpolynomial_fold_add(entry->data, pwf);
1375 if (isl_pw_qpolynomial_fold_is_zero(entry->data)) {
1376 isl_pw_qpolynomial_fold_free(entry->data);
1377 isl_hash_table_remove(ctx, &(*upwf)->table, entry);
1383 isl_pw_qpolynomial_free(pwqp);
1387 __isl_give isl_union_pw_qpolynomial_fold *isl_union_pw_qpolynomial_fold_add_union_pw_qpolynomial(
1388 __isl_take isl_union_pw_qpolynomial_fold *upwf,
1389 __isl_take isl_union_pw_qpolynomial *upwqp)
1391 upwf = isl_union_pw_qpolynomial_fold_align_params(upwf,
1392 isl_union_pw_qpolynomial_get_space(upwqp));
1393 upwqp = isl_union_pw_qpolynomial_align_params(upwqp,
1394 isl_union_pw_qpolynomial_fold_get_space(upwf));
1396 upwf = isl_union_pw_qpolynomial_fold_cow(upwf);
1397 if (!upwf || !upwqp)
1400 if (isl_union_pw_qpolynomial_foreach_pw_qpolynomial(upwqp, &add_pwqp,
1404 isl_union_pw_qpolynomial_free(upwqp);
1408 isl_union_pw_qpolynomial_fold_free(upwf);
1409 isl_union_pw_qpolynomial_free(upwqp);
1413 static int join_compatible(__isl_keep isl_space *dim1, __isl_keep isl_space *dim2)
1416 m = isl_space_match(dim1, isl_dim_param, dim2, isl_dim_param);
1419 return isl_space_tuple_match(dim1, isl_dim_out, dim2, isl_dim_in);
1422 /* Compute the intersection of the range of the map and the domain
1423 * of the piecewise quasipolynomial reduction and then compute a bound
1424 * on the associated quasipolynomial reduction over all elements
1425 * in this intersection.
1427 * We first introduce some unconstrained dimensions in the
1428 * piecewise quasipolynomial, intersect the resulting domain
1429 * with the wrapped map and the compute the sum.
1431 __isl_give isl_pw_qpolynomial_fold *isl_map_apply_pw_qpolynomial_fold(
1432 __isl_take isl_map *map, __isl_take isl_pw_qpolynomial_fold *pwf,
1442 ctx = isl_map_get_ctx(map);
1446 map_dim = isl_map_get_space(map);
1447 pwf_dim = isl_pw_qpolynomial_fold_get_space(pwf);
1448 ok = join_compatible(map_dim, pwf_dim);
1449 isl_space_free(map_dim);
1450 isl_space_free(pwf_dim);
1452 isl_die(ctx, isl_error_invalid, "incompatible dimensions",
1455 n_in = isl_map_dim(map, isl_dim_in);
1456 pwf = isl_pw_qpolynomial_fold_insert_dims(pwf, isl_dim_in, 0, n_in);
1458 dom = isl_map_wrap(map);
1459 pwf = isl_pw_qpolynomial_fold_reset_domain_space(pwf,
1460 isl_set_get_space(dom));
1462 pwf = isl_pw_qpolynomial_fold_intersect_domain(pwf, dom);
1463 pwf = isl_pw_qpolynomial_fold_bound(pwf, tight);
1468 isl_pw_qpolynomial_fold_free(pwf);
1472 __isl_give isl_pw_qpolynomial_fold *isl_set_apply_pw_qpolynomial_fold(
1473 __isl_take isl_set *set, __isl_take isl_pw_qpolynomial_fold *pwf,
1476 return isl_map_apply_pw_qpolynomial_fold(set, pwf, tight);
1479 struct isl_apply_fold_data {
1480 isl_union_pw_qpolynomial_fold *upwf;
1481 isl_union_pw_qpolynomial_fold *res;
1486 static int pw_qpolynomial_fold_apply(__isl_take isl_pw_qpolynomial_fold *pwf,
1491 struct isl_apply_fold_data *data = user;
1494 map_dim = isl_map_get_space(data->map);
1495 pwf_dim = isl_pw_qpolynomial_fold_get_space(pwf);
1496 ok = join_compatible(map_dim, pwf_dim);
1497 isl_space_free(map_dim);
1498 isl_space_free(pwf_dim);
1501 pwf = isl_map_apply_pw_qpolynomial_fold(isl_map_copy(data->map),
1502 pwf, data->tight ? &data->tight : NULL);
1503 data->res = isl_union_pw_qpolynomial_fold_fold_pw_qpolynomial_fold(
1506 isl_pw_qpolynomial_fold_free(pwf);
1511 static int map_apply(__isl_take isl_map *map, void *user)
1513 struct isl_apply_fold_data *data = user;
1517 r = isl_union_pw_qpolynomial_fold_foreach_pw_qpolynomial_fold(
1518 data->upwf, &pw_qpolynomial_fold_apply, data);
1524 __isl_give isl_union_pw_qpolynomial_fold *isl_union_map_apply_union_pw_qpolynomial_fold(
1525 __isl_take isl_union_map *umap,
1526 __isl_take isl_union_pw_qpolynomial_fold *upwf, int *tight)
1530 struct isl_apply_fold_data data;
1532 upwf = isl_union_pw_qpolynomial_fold_align_params(upwf,
1533 isl_union_map_get_space(umap));
1534 umap = isl_union_map_align_params(umap,
1535 isl_union_pw_qpolynomial_fold_get_space(upwf));
1538 data.tight = tight ? 1 : 0;
1539 dim = isl_union_pw_qpolynomial_fold_get_space(upwf);
1540 type = isl_union_pw_qpolynomial_fold_get_type(upwf);
1541 data.res = isl_union_pw_qpolynomial_fold_zero(dim, type);
1542 if (isl_union_map_foreach_map(umap, &map_apply, &data) < 0)
1545 isl_union_map_free(umap);
1546 isl_union_pw_qpolynomial_fold_free(upwf);
1549 *tight = data.tight;
1553 isl_union_map_free(umap);
1554 isl_union_pw_qpolynomial_fold_free(upwf);
1555 isl_union_pw_qpolynomial_fold_free(data.res);
1559 __isl_give isl_union_pw_qpolynomial_fold *isl_union_set_apply_union_pw_qpolynomial_fold(
1560 __isl_take isl_union_set *uset,
1561 __isl_take isl_union_pw_qpolynomial_fold *upwf, int *tight)
1563 return isl_union_map_apply_union_pw_qpolynomial_fold(uset, upwf, tight);
1566 /* Reorder the dimension of "fold" according to the given reordering.
1568 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_realign_domain(
1569 __isl_take isl_qpolynomial_fold *fold, __isl_take isl_reordering *r)
1573 fold = isl_qpolynomial_fold_cow(fold);
1577 for (i = 0; i < fold->n; ++i) {
1578 fold->qp[i] = isl_qpolynomial_realign_domain(fold->qp[i],
1579 isl_reordering_copy(r));
1584 fold = isl_qpolynomial_fold_reset_domain_space(fold,
1585 isl_space_copy(r->dim));
1587 isl_reordering_free(r);
1591 isl_qpolynomial_fold_free(fold);
1592 isl_reordering_free(r);
1596 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_mul_isl_int(
1597 __isl_take isl_qpolynomial_fold *fold, isl_int v)
1601 if (isl_int_is_one(v))
1603 if (fold && isl_int_is_zero(v)) {
1604 isl_qpolynomial_fold *zero;
1605 isl_space *dim = isl_space_copy(fold->dim);
1606 zero = isl_qpolynomial_fold_empty(fold->type, dim);
1607 isl_qpolynomial_fold_free(fold);
1611 fold = isl_qpolynomial_fold_cow(fold);
1615 if (isl_int_is_neg(v))
1616 fold->type = isl_fold_type_negate(fold->type);
1617 for (i = 0; i < fold->n; ++i) {
1618 fold->qp[i] = isl_qpolynomial_mul_isl_int(fold->qp[i], v);
1625 isl_qpolynomial_fold_free(fold);
1629 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_scale(
1630 __isl_take isl_qpolynomial_fold *fold, isl_int v)
1632 return isl_qpolynomial_fold_mul_isl_int(fold, v);
1635 /* Multiply "fold" by "v".
1637 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_scale_val(
1638 __isl_take isl_qpolynomial_fold *fold, __isl_take isl_val *v)
1645 if (isl_val_is_one(v)) {
1649 if (isl_val_is_zero(v)) {
1650 isl_qpolynomial_fold *zero;
1651 isl_space *space = isl_qpolynomial_fold_get_domain_space(fold);
1652 zero = isl_qpolynomial_fold_empty(fold->type, space);
1653 isl_qpolynomial_fold_free(fold);
1657 if (!isl_val_is_rat(v))
1658 isl_die(isl_qpolynomial_fold_get_ctx(fold), isl_error_invalid,
1659 "expecting rational factor", goto error);
1661 fold = isl_qpolynomial_fold_cow(fold);
1665 if (isl_val_is_neg(v))
1666 fold->type = isl_fold_type_negate(fold->type);
1667 for (i = 0; i < fold->n; ++i) {
1668 fold->qp[i] = isl_qpolynomial_scale_val(fold->qp[i],
1678 isl_qpolynomial_fold_free(fold);