isl_pip: allow existentially quantified variables in the context
[isl.git] / isl_fold.c
blobe05ee101737d64abf5ebabc55f19f38a52b982b1
1 /*
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,
8 * 91893 Orsay, France
9 */
11 #define ISL_DIM_H
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>
17 #include <isl_lp_private.h>
18 #include <isl_seq.h>
19 #include <isl_mat_private.h>
20 #include <isl_val_private.h>
21 #include <isl_vec_private.h>
22 #include <isl_config.h>
23 #include <isl/deprecated/polynomial_int.h>
25 enum isl_fold isl_fold_type_negate(enum isl_fold type)
27 switch (type) {
28 case isl_fold_min:
29 return isl_fold_max;
30 case isl_fold_max:
31 return isl_fold_min;
32 case isl_fold_list:
33 return isl_fold_list;
36 isl_die(NULL, isl_error_internal, "unhandled isl_fold type", abort());
39 static __isl_give isl_qpolynomial_fold *qpolynomial_fold_alloc(
40 enum isl_fold type, __isl_take isl_space *dim, int n)
42 isl_qpolynomial_fold *fold;
44 if (!dim)
45 goto error;
47 isl_assert(dim->ctx, n >= 0, goto error);
48 fold = isl_calloc(dim->ctx, struct isl_qpolynomial_fold,
49 sizeof(struct isl_qpolynomial_fold) +
50 (n - 1) * sizeof(struct isl_qpolynomial *));
51 if (!fold)
52 goto error;
54 fold->ref = 1;
55 fold->size = n;
56 fold->n = 0;
57 fold->type = type;
58 fold->dim = dim;
60 return fold;
61 error:
62 isl_space_free(dim);
63 return NULL;
66 isl_ctx *isl_qpolynomial_fold_get_ctx(__isl_keep isl_qpolynomial_fold *fold)
68 return fold ? fold->dim->ctx : NULL;
71 __isl_give isl_space *isl_qpolynomial_fold_get_domain_space(
72 __isl_keep isl_qpolynomial_fold *fold)
74 return fold ? isl_space_copy(fold->dim) : NULL;
77 __isl_give isl_space *isl_qpolynomial_fold_get_space(
78 __isl_keep isl_qpolynomial_fold *fold)
80 isl_space *space;
81 if (!fold)
82 return NULL;
83 space = isl_space_copy(fold->dim);
84 space = isl_space_from_domain(space);
85 space = isl_space_add_dims(space, isl_dim_out, 1);
86 return space;
89 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_reset_domain_space(
90 __isl_take isl_qpolynomial_fold *fold, __isl_take isl_space *dim)
92 int i;
94 fold = isl_qpolynomial_fold_cow(fold);
95 if (!fold || !dim)
96 goto error;
98 for (i = 0; i < fold->n; ++i) {
99 fold->qp[i] = isl_qpolynomial_reset_domain_space(fold->qp[i],
100 isl_space_copy(dim));
101 if (!fold->qp[i])
102 goto error;
105 isl_space_free(fold->dim);
106 fold->dim = dim;
108 return fold;
109 error:
110 isl_qpolynomial_fold_free(fold);
111 isl_space_free(dim);
112 return NULL;
115 /* Reset the space of "fold". This function is called from isl_pw_templ.c
116 * and doesn't know if the space of an element object is represented
117 * directly or through its domain. It therefore passes along both.
119 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_reset_space_and_domain(
120 __isl_take isl_qpolynomial_fold *fold, __isl_take isl_space *space,
121 __isl_take isl_space *domain)
123 isl_space_free(space);
124 return isl_qpolynomial_fold_reset_domain_space(fold, domain);
127 int isl_qpolynomial_fold_involves_dims(__isl_keep isl_qpolynomial_fold *fold,
128 enum isl_dim_type type, unsigned first, unsigned n)
130 int i;
132 if (!fold)
133 return -1;
134 if (fold->n == 0 || n == 0)
135 return 0;
137 for (i = 0; i < fold->n; ++i) {
138 int involves = isl_qpolynomial_involves_dims(fold->qp[i],
139 type, first, n);
140 if (involves < 0 || involves)
141 return involves;
143 return 0;
146 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_set_dim_name(
147 __isl_take isl_qpolynomial_fold *fold,
148 enum isl_dim_type type, unsigned pos, const char *s)
150 int i;
152 fold = isl_qpolynomial_fold_cow(fold);
153 if (!fold)
154 return NULL;
155 fold->dim = isl_space_set_dim_name(fold->dim, type, pos, s);
156 if (!fold->dim)
157 goto error;
159 for (i = 0; i < fold->n; ++i) {
160 fold->qp[i] = isl_qpolynomial_set_dim_name(fold->qp[i],
161 type, pos, s);
162 if (!fold->qp[i])
163 goto error;
166 return fold;
167 error:
168 isl_qpolynomial_fold_free(fold);
169 return NULL;
172 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_drop_dims(
173 __isl_take isl_qpolynomial_fold *fold,
174 enum isl_dim_type type, unsigned first, unsigned n)
176 int i;
177 enum isl_dim_type set_type;
179 if (!fold)
180 return NULL;
181 if (n == 0)
182 return fold;
184 set_type = type == isl_dim_in ? isl_dim_set : type;
186 fold = isl_qpolynomial_fold_cow(fold);
187 if (!fold)
188 return NULL;
189 fold->dim = isl_space_drop_dims(fold->dim, set_type, first, n);
190 if (!fold->dim)
191 goto error;
193 for (i = 0; i < fold->n; ++i) {
194 fold->qp[i] = isl_qpolynomial_drop_dims(fold->qp[i],
195 type, first, n);
196 if (!fold->qp[i])
197 goto error;
200 return fold;
201 error:
202 isl_qpolynomial_fold_free(fold);
203 return NULL;
206 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_insert_dims(
207 __isl_take isl_qpolynomial_fold *fold,
208 enum isl_dim_type type, unsigned first, unsigned n)
210 int i;
212 if (!fold)
213 return NULL;
214 if (n == 0 && !isl_space_is_named_or_nested(fold->dim, type))
215 return fold;
217 fold = isl_qpolynomial_fold_cow(fold);
218 if (!fold)
219 return NULL;
220 fold->dim = isl_space_insert_dims(fold->dim, type, first, n);
221 if (!fold->dim)
222 goto error;
224 for (i = 0; i < fold->n; ++i) {
225 fold->qp[i] = isl_qpolynomial_insert_dims(fold->qp[i],
226 type, first, n);
227 if (!fold->qp[i])
228 goto error;
231 return fold;
232 error:
233 isl_qpolynomial_fold_free(fold);
234 return NULL;
237 /* Determine the sign of the constant quasipolynomial "qp".
239 * Return
240 * -1 if qp <= 0
241 * 1 if qp >= 0
242 * 0 if unknown
244 * For qp == 0, we can return either -1 or 1. In practice, we return 1.
245 * For qp == NaN, the sign is undefined, so we return 0.
247 static int isl_qpolynomial_cst_sign(__isl_keep isl_qpolynomial *qp)
249 struct isl_upoly_cst *cst;
251 if (isl_qpolynomial_is_nan(qp))
252 return 0;
254 cst = isl_upoly_as_cst(qp->upoly);
255 if (!cst)
256 return 0;
258 return isl_int_sgn(cst->n) < 0 ? -1 : 1;
261 static int isl_qpolynomial_aff_sign(__isl_keep isl_set *set,
262 __isl_keep isl_qpolynomial *qp)
264 enum isl_lp_result res;
265 isl_vec *aff;
266 isl_int opt;
267 int sgn = 0;
269 aff = isl_qpolynomial_extract_affine(qp);
270 if (!aff)
271 return 0;
273 isl_int_init(opt);
275 res = isl_set_solve_lp(set, 0, aff->el + 1, aff->el[0],
276 &opt, NULL, NULL);
277 if (res == isl_lp_error)
278 goto done;
279 if (res == isl_lp_empty ||
280 (res == isl_lp_ok && !isl_int_is_neg(opt))) {
281 sgn = 1;
282 goto done;
285 res = isl_set_solve_lp(set, 1, aff->el + 1, aff->el[0],
286 &opt, NULL, NULL);
287 if (res == isl_lp_ok && !isl_int_is_pos(opt))
288 sgn = -1;
290 done:
291 isl_int_clear(opt);
292 isl_vec_free(aff);
293 return sgn;
296 /* Determine, if possible, the sign of the quasipolynomial "qp" on
297 * the domain "set".
299 * If qp is a constant, then the problem is trivial.
300 * If qp is linear, then we check if the minimum of the corresponding
301 * affine constraint is non-negative or if the maximum is non-positive.
303 * Otherwise, we check if the outermost variable "v" has a lower bound "l"
304 * in "set". If so, we write qp(v,v') as
306 * q(v,v') * (v - l) + r(v')
308 * if q(v,v') and r(v') have the same known sign, then the original
309 * quasipolynomial has the same sign as well.
311 * Return
312 * -1 if qp <= 0
313 * 1 if qp >= 0
314 * 0 if unknown
316 static int isl_qpolynomial_sign(__isl_keep isl_set *set,
317 __isl_keep isl_qpolynomial *qp)
319 int d;
320 int i;
321 int is;
322 struct isl_upoly_rec *rec;
323 isl_vec *v;
324 isl_int l;
325 enum isl_lp_result res;
326 int sgn = 0;
328 is = isl_qpolynomial_is_cst(qp, NULL, NULL);
329 if (is < 0)
330 return 0;
331 if (is)
332 return isl_qpolynomial_cst_sign(qp);
334 is = isl_qpolynomial_is_affine(qp);
335 if (is < 0)
336 return 0;
337 if (is)
338 return isl_qpolynomial_aff_sign(set, qp);
340 if (qp->div->n_row > 0)
341 return 0;
343 rec = isl_upoly_as_rec(qp->upoly);
344 if (!rec)
345 return 0;
347 d = isl_space_dim(qp->dim, isl_dim_all);
348 v = isl_vec_alloc(set->ctx, 2 + d);
349 if (!v)
350 return 0;
352 isl_seq_clr(v->el + 1, 1 + d);
353 isl_int_set_si(v->el[0], 1);
354 isl_int_set_si(v->el[2 + qp->upoly->var], 1);
356 isl_int_init(l);
358 res = isl_set_solve_lp(set, 0, v->el + 1, v->el[0], &l, NULL, NULL);
359 if (res == isl_lp_ok) {
360 isl_qpolynomial *min;
361 isl_qpolynomial *base;
362 isl_qpolynomial *r, *q;
363 isl_qpolynomial *t;
365 min = isl_qpolynomial_cst_on_domain(isl_space_copy(qp->dim), l);
366 base = isl_qpolynomial_var_pow_on_domain(isl_space_copy(qp->dim),
367 qp->upoly->var, 1);
369 r = isl_qpolynomial_alloc(isl_space_copy(qp->dim), 0,
370 isl_upoly_copy(rec->p[rec->n - 1]));
371 q = isl_qpolynomial_copy(r);
373 for (i = rec->n - 2; i >= 0; --i) {
374 r = isl_qpolynomial_mul(r, isl_qpolynomial_copy(min));
375 t = isl_qpolynomial_alloc(isl_space_copy(qp->dim), 0,
376 isl_upoly_copy(rec->p[i]));
377 r = isl_qpolynomial_add(r, t);
378 if (i == 0)
379 break;
380 q = isl_qpolynomial_mul(q, isl_qpolynomial_copy(base));
381 q = isl_qpolynomial_add(q, isl_qpolynomial_copy(r));
384 if (isl_qpolynomial_is_zero(q))
385 sgn = isl_qpolynomial_sign(set, r);
386 else if (isl_qpolynomial_is_zero(r))
387 sgn = isl_qpolynomial_sign(set, q);
388 else {
389 int sgn_q, sgn_r;
390 sgn_r = isl_qpolynomial_sign(set, r);
391 sgn_q = isl_qpolynomial_sign(set, q);
392 if (sgn_r == sgn_q)
393 sgn = sgn_r;
396 isl_qpolynomial_free(min);
397 isl_qpolynomial_free(base);
398 isl_qpolynomial_free(q);
399 isl_qpolynomial_free(r);
402 isl_int_clear(l);
404 isl_vec_free(v);
406 return sgn;
409 /* Combine "fold1" and "fold2" into a single reduction, eliminating
410 * those elements of one reduction that are already covered by the other
411 * reduction on "set".
413 * If "fold1" or "fold2" is an empty reduction, then return
414 * the other reduction.
415 * If "fold1" or "fold2" is a NaN, then return this NaN.
417 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_fold_on_domain(
418 __isl_keep isl_set *set,
419 __isl_take isl_qpolynomial_fold *fold1,
420 __isl_take isl_qpolynomial_fold *fold2)
422 int i, j;
423 int n1;
424 struct isl_qpolynomial_fold *res = NULL;
425 int better;
427 if (!fold1 || !fold2)
428 goto error;
430 isl_assert(fold1->dim->ctx, fold1->type == fold2->type, goto error);
431 isl_assert(fold1->dim->ctx, isl_space_is_equal(fold1->dim, fold2->dim),
432 goto error);
434 better = fold1->type == isl_fold_max ? -1 : 1;
436 if (isl_qpolynomial_fold_is_empty(fold1) ||
437 isl_qpolynomial_fold_is_nan(fold2)) {
438 isl_qpolynomial_fold_free(fold1);
439 return fold2;
442 if (isl_qpolynomial_fold_is_empty(fold2) ||
443 isl_qpolynomial_fold_is_nan(fold1)) {
444 isl_qpolynomial_fold_free(fold2);
445 return fold1;
448 res = qpolynomial_fold_alloc(fold1->type, isl_space_copy(fold1->dim),
449 fold1->n + fold2->n);
450 if (!res)
451 goto error;
453 for (i = 0; i < fold1->n; ++i) {
454 res->qp[res->n] = isl_qpolynomial_copy(fold1->qp[i]);
455 if (!res->qp[res->n])
456 goto error;
457 res->n++;
459 n1 = res->n;
461 for (i = 0; i < fold2->n; ++i) {
462 for (j = n1 - 1; j >= 0; --j) {
463 isl_qpolynomial *d;
464 int sgn, equal;
465 equal = isl_qpolynomial_plain_is_equal(res->qp[j],
466 fold2->qp[i]);
467 if (equal < 0)
468 goto error;
469 if (equal)
470 break;
471 d = isl_qpolynomial_sub(
472 isl_qpolynomial_copy(res->qp[j]),
473 isl_qpolynomial_copy(fold2->qp[i]));
474 sgn = isl_qpolynomial_sign(set, d);
475 isl_qpolynomial_free(d);
476 if (sgn == 0)
477 continue;
478 if (sgn != better)
479 break;
480 isl_qpolynomial_free(res->qp[j]);
481 if (j != n1 - 1)
482 res->qp[j] = res->qp[n1 - 1];
483 n1--;
484 if (n1 != res->n - 1)
485 res->qp[n1] = res->qp[res->n - 1];
486 res->n--;
488 if (j >= 0)
489 continue;
490 res->qp[res->n] = isl_qpolynomial_copy(fold2->qp[i]);
491 if (!res->qp[res->n])
492 goto error;
493 res->n++;
496 isl_qpolynomial_fold_free(fold1);
497 isl_qpolynomial_fold_free(fold2);
499 return res;
500 error:
501 isl_qpolynomial_fold_free(res);
502 isl_qpolynomial_fold_free(fold1);
503 isl_qpolynomial_fold_free(fold2);
504 return NULL;
507 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_add_qpolynomial(
508 __isl_take isl_qpolynomial_fold *fold, __isl_take isl_qpolynomial *qp)
510 int i;
512 if (!fold || !qp)
513 goto error;
515 if (isl_qpolynomial_is_zero(qp)) {
516 isl_qpolynomial_free(qp);
517 return fold;
520 fold = isl_qpolynomial_fold_cow(fold);
521 if (!fold)
522 goto error;
524 for (i = 0; i < fold->n; ++i) {
525 fold->qp[i] = isl_qpolynomial_add(fold->qp[i],
526 isl_qpolynomial_copy(qp));
527 if (!fold->qp[i])
528 goto error;
531 isl_qpolynomial_free(qp);
532 return fold;
533 error:
534 isl_qpolynomial_fold_free(fold);
535 isl_qpolynomial_free(qp);
536 return NULL;
539 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_add_on_domain(
540 __isl_keep isl_set *dom,
541 __isl_take isl_qpolynomial_fold *fold1,
542 __isl_take isl_qpolynomial_fold *fold2)
544 int i;
545 isl_qpolynomial_fold *res = NULL;
547 if (!fold1 || !fold2)
548 goto error;
550 if (isl_qpolynomial_fold_is_empty(fold1)) {
551 isl_qpolynomial_fold_free(fold1);
552 return fold2;
555 if (isl_qpolynomial_fold_is_empty(fold2)) {
556 isl_qpolynomial_fold_free(fold2);
557 return fold1;
560 if (fold1->n == 1 && fold2->n != 1)
561 return isl_qpolynomial_fold_add_on_domain(dom, fold2, fold1);
563 if (fold2->n == 1) {
564 res = isl_qpolynomial_fold_add_qpolynomial(fold1,
565 isl_qpolynomial_copy(fold2->qp[0]));
566 isl_qpolynomial_fold_free(fold2);
567 return res;
570 res = isl_qpolynomial_fold_add_qpolynomial(
571 isl_qpolynomial_fold_copy(fold1),
572 isl_qpolynomial_copy(fold2->qp[0]));
574 for (i = 1; i < fold2->n; ++i) {
575 isl_qpolynomial_fold *res_i;
576 res_i = isl_qpolynomial_fold_add_qpolynomial(
577 isl_qpolynomial_fold_copy(fold1),
578 isl_qpolynomial_copy(fold2->qp[i]));
579 res = isl_qpolynomial_fold_fold_on_domain(dom, res, res_i);
582 isl_qpolynomial_fold_free(fold1);
583 isl_qpolynomial_fold_free(fold2);
584 return res;
585 error:
586 isl_qpolynomial_fold_free(res);
587 isl_qpolynomial_fold_free(fold1);
588 isl_qpolynomial_fold_free(fold2);
589 return NULL;
592 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_substitute_equalities(
593 __isl_take isl_qpolynomial_fold *fold, __isl_take isl_basic_set *eq)
595 int i;
597 if (!fold || !eq)
598 goto error;
600 fold = isl_qpolynomial_fold_cow(fold);
601 if (!fold)
602 return NULL;
604 for (i = 0; i < fold->n; ++i) {
605 fold->qp[i] = isl_qpolynomial_substitute_equalities(fold->qp[i],
606 isl_basic_set_copy(eq));
607 if (!fold->qp[i])
608 goto error;
611 isl_basic_set_free(eq);
612 return fold;
613 error:
614 isl_basic_set_free(eq);
615 isl_qpolynomial_fold_free(fold);
616 return NULL;
619 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_gist(
620 __isl_take isl_qpolynomial_fold *fold, __isl_take isl_set *context)
622 int i;
624 if (!fold || !context)
625 goto error;
627 fold = isl_qpolynomial_fold_cow(fold);
628 if (!fold)
629 return NULL;
631 for (i = 0; i < fold->n; ++i) {
632 fold->qp[i] = isl_qpolynomial_gist(fold->qp[i],
633 isl_set_copy(context));
634 if (!fold->qp[i])
635 goto error;
638 isl_set_free(context);
639 return fold;
640 error:
641 isl_set_free(context);
642 isl_qpolynomial_fold_free(fold);
643 return NULL;
646 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_gist_params(
647 __isl_take isl_qpolynomial_fold *fold, __isl_take isl_set *context)
649 isl_space *space = isl_qpolynomial_fold_get_domain_space(fold);
650 isl_set *dom_context = isl_set_universe(space);
651 dom_context = isl_set_intersect_params(dom_context, context);
652 return isl_qpolynomial_fold_gist(fold, dom_context);
655 #define HAS_TYPE
657 #undef PW
658 #define PW isl_pw_qpolynomial_fold
659 #undef EL
660 #define EL isl_qpolynomial_fold
661 #undef EL_IS_ZERO
662 #define EL_IS_ZERO is_empty
663 #undef ZERO
664 #define ZERO zero
665 #undef IS_ZERO
666 #define IS_ZERO is_zero
667 #undef FIELD
668 #define FIELD fold
669 #undef DEFAULT_IS_ZERO
670 #define DEFAULT_IS_ZERO 1
672 #define NO_NEG
673 #define NO_SUB
674 #define NO_PULLBACK
676 #include <isl_pw_templ.c>
678 #undef UNION
679 #define UNION isl_union_pw_qpolynomial_fold
680 #undef PART
681 #define PART isl_pw_qpolynomial_fold
682 #undef PARTS
683 #define PARTS pw_qpolynomial_fold
685 #define NO_SUB
687 #include <isl_union_single.c>
688 #include <isl_union_eval.c>
690 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_empty(enum isl_fold type,
691 __isl_take isl_space *dim)
693 return qpolynomial_fold_alloc(type, dim, 0);
696 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_alloc(
697 enum isl_fold type, __isl_take isl_qpolynomial *qp)
699 isl_qpolynomial_fold *fold;
701 if (!qp)
702 return NULL;
704 fold = qpolynomial_fold_alloc(type, isl_space_copy(qp->dim), 1);
705 if (!fold)
706 goto error;
708 fold->qp[0] = qp;
709 fold->n++;
711 return fold;
712 error:
713 isl_qpolynomial_fold_free(fold);
714 isl_qpolynomial_free(qp);
715 return NULL;
718 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_copy(
719 __isl_keep isl_qpolynomial_fold *fold)
721 if (!fold)
722 return NULL;
724 fold->ref++;
725 return fold;
728 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_dup(
729 __isl_keep isl_qpolynomial_fold *fold)
731 int i;
732 isl_qpolynomial_fold *dup;
734 if (!fold)
735 return NULL;
736 dup = qpolynomial_fold_alloc(fold->type,
737 isl_space_copy(fold->dim), fold->n);
738 if (!dup)
739 return NULL;
741 dup->n = fold->n;
742 for (i = 0; i < fold->n; ++i) {
743 dup->qp[i] = isl_qpolynomial_copy(fold->qp[i]);
744 if (!dup->qp[i])
745 goto error;
748 return dup;
749 error:
750 isl_qpolynomial_fold_free(dup);
751 return NULL;
754 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_cow(
755 __isl_take isl_qpolynomial_fold *fold)
757 if (!fold)
758 return NULL;
760 if (fold->ref == 1)
761 return fold;
762 fold->ref--;
763 return isl_qpolynomial_fold_dup(fold);
766 void isl_qpolynomial_fold_free(__isl_take isl_qpolynomial_fold *fold)
768 int i;
770 if (!fold)
771 return;
772 if (--fold->ref > 0)
773 return;
775 for (i = 0; i < fold->n; ++i)
776 isl_qpolynomial_free(fold->qp[i]);
777 isl_space_free(fold->dim);
778 free(fold);
781 int isl_qpolynomial_fold_is_empty(__isl_keep isl_qpolynomial_fold *fold)
783 if (!fold)
784 return -1;
786 return fold->n == 0;
789 /* Does "fold" represent max(NaN) or min(NaN)?
791 isl_bool isl_qpolynomial_fold_is_nan(__isl_keep isl_qpolynomial_fold *fold)
793 if (!fold)
794 return isl_bool_error;
795 if (fold->n != 1)
796 return isl_bool_false;
797 return isl_qpolynomial_is_nan(fold->qp[0]);
800 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_fold(
801 __isl_take isl_qpolynomial_fold *fold1,
802 __isl_take isl_qpolynomial_fold *fold2)
804 int i;
805 struct isl_qpolynomial_fold *res = NULL;
807 if (!fold1 || !fold2)
808 goto error;
810 isl_assert(fold1->dim->ctx, fold1->type == fold2->type, goto error);
811 isl_assert(fold1->dim->ctx, isl_space_is_equal(fold1->dim, fold2->dim),
812 goto error);
814 if (isl_qpolynomial_fold_is_empty(fold1)) {
815 isl_qpolynomial_fold_free(fold1);
816 return fold2;
819 if (isl_qpolynomial_fold_is_empty(fold2)) {
820 isl_qpolynomial_fold_free(fold2);
821 return fold1;
824 res = qpolynomial_fold_alloc(fold1->type, isl_space_copy(fold1->dim),
825 fold1->n + fold2->n);
826 if (!res)
827 goto error;
829 for (i = 0; i < fold1->n; ++i) {
830 res->qp[res->n] = isl_qpolynomial_copy(fold1->qp[i]);
831 if (!res->qp[res->n])
832 goto error;
833 res->n++;
836 for (i = 0; i < fold2->n; ++i) {
837 res->qp[res->n] = isl_qpolynomial_copy(fold2->qp[i]);
838 if (!res->qp[res->n])
839 goto error;
840 res->n++;
843 isl_qpolynomial_fold_free(fold1);
844 isl_qpolynomial_fold_free(fold2);
846 return res;
847 error:
848 isl_qpolynomial_fold_free(res);
849 isl_qpolynomial_fold_free(fold1);
850 isl_qpolynomial_fold_free(fold2);
851 return NULL;
854 __isl_give isl_pw_qpolynomial_fold *isl_pw_qpolynomial_fold_fold(
855 __isl_take isl_pw_qpolynomial_fold *pw1,
856 __isl_take isl_pw_qpolynomial_fold *pw2)
858 int i, j, n;
859 struct isl_pw_qpolynomial_fold *res;
860 isl_set *set;
862 if (!pw1 || !pw2)
863 goto error;
865 isl_assert(pw1->dim->ctx, isl_space_is_equal(pw1->dim, pw2->dim), goto error);
867 if (isl_pw_qpolynomial_fold_is_zero(pw1)) {
868 isl_pw_qpolynomial_fold_free(pw1);
869 return pw2;
872 if (isl_pw_qpolynomial_fold_is_zero(pw2)) {
873 isl_pw_qpolynomial_fold_free(pw2);
874 return pw1;
877 if (pw1->type != pw2->type)
878 isl_die(pw1->dim->ctx, isl_error_invalid,
879 "fold types don't match", goto error);
881 n = (pw1->n + 1) * (pw2->n + 1);
882 res = isl_pw_qpolynomial_fold_alloc_size(isl_space_copy(pw1->dim),
883 pw1->type, n);
885 for (i = 0; i < pw1->n; ++i) {
886 set = isl_set_copy(pw1->p[i].set);
887 for (j = 0; j < pw2->n; ++j) {
888 struct isl_set *common;
889 isl_qpolynomial_fold *sum;
890 set = isl_set_subtract(set,
891 isl_set_copy(pw2->p[j].set));
892 common = isl_set_intersect(isl_set_copy(pw1->p[i].set),
893 isl_set_copy(pw2->p[j].set));
894 if (isl_set_plain_is_empty(common)) {
895 isl_set_free(common);
896 continue;
899 sum = isl_qpolynomial_fold_fold_on_domain(common,
900 isl_qpolynomial_fold_copy(pw1->p[i].fold),
901 isl_qpolynomial_fold_copy(pw2->p[j].fold));
903 res = isl_pw_qpolynomial_fold_add_piece(res, common, sum);
905 res = isl_pw_qpolynomial_fold_add_piece(res, set,
906 isl_qpolynomial_fold_copy(pw1->p[i].fold));
909 for (j = 0; j < pw2->n; ++j) {
910 set = isl_set_copy(pw2->p[j].set);
911 for (i = 0; i < pw1->n; ++i)
912 set = isl_set_subtract(set, isl_set_copy(pw1->p[i].set));
913 res = isl_pw_qpolynomial_fold_add_piece(res, set,
914 isl_qpolynomial_fold_copy(pw2->p[j].fold));
917 isl_pw_qpolynomial_fold_free(pw1);
918 isl_pw_qpolynomial_fold_free(pw2);
920 return res;
921 error:
922 isl_pw_qpolynomial_fold_free(pw1);
923 isl_pw_qpolynomial_fold_free(pw2);
924 return NULL;
927 __isl_give isl_union_pw_qpolynomial_fold *isl_union_pw_qpolynomial_fold_fold_pw_qpolynomial_fold(
928 __isl_take isl_union_pw_qpolynomial_fold *u,
929 __isl_take isl_pw_qpolynomial_fold *part)
931 struct isl_hash_table_entry *entry;
933 u = isl_union_pw_qpolynomial_fold_cow(u);
935 if (!part || !u)
936 goto error;
938 isl_assert(u->space->ctx,
939 isl_space_match(part->dim, isl_dim_param, u->space, isl_dim_param),
940 goto error);
942 entry = isl_union_pw_qpolynomial_fold_find_part_entry(u, part->dim, 1);
943 if (!entry)
944 goto error;
946 if (!entry->data)
947 entry->data = part;
948 else {
949 entry->data = isl_pw_qpolynomial_fold_fold(entry->data,
950 isl_pw_qpolynomial_fold_copy(part));
951 if (!entry->data)
952 goto error;
953 isl_pw_qpolynomial_fold_free(part);
956 return u;
957 error:
958 isl_pw_qpolynomial_fold_free(part);
959 isl_union_pw_qpolynomial_fold_free(u);
960 return NULL;
963 static isl_stat fold_part(__isl_take isl_pw_qpolynomial_fold *part, void *user)
965 isl_union_pw_qpolynomial_fold **u;
966 u = (isl_union_pw_qpolynomial_fold **)user;
968 *u = isl_union_pw_qpolynomial_fold_fold_pw_qpolynomial_fold(*u, part);
970 return isl_stat_ok;
973 __isl_give isl_union_pw_qpolynomial_fold *isl_union_pw_qpolynomial_fold_fold(
974 __isl_take isl_union_pw_qpolynomial_fold *u1,
975 __isl_take isl_union_pw_qpolynomial_fold *u2)
977 u1 = isl_union_pw_qpolynomial_fold_cow(u1);
979 if (!u1 || !u2)
980 goto error;
982 if (isl_union_pw_qpolynomial_fold_foreach_pw_qpolynomial_fold(u2,
983 &fold_part, &u1) < 0)
984 goto error;
986 isl_union_pw_qpolynomial_fold_free(u2);
988 return u1;
989 error:
990 isl_union_pw_qpolynomial_fold_free(u1);
991 isl_union_pw_qpolynomial_fold_free(u2);
992 return NULL;
995 __isl_give isl_pw_qpolynomial_fold *isl_pw_qpolynomial_fold_from_pw_qpolynomial(
996 enum isl_fold type, __isl_take isl_pw_qpolynomial *pwqp)
998 int i;
999 isl_pw_qpolynomial_fold *pwf;
1001 if (!pwqp)
1002 return NULL;
1004 pwf = isl_pw_qpolynomial_fold_alloc_size(isl_space_copy(pwqp->dim),
1005 type, pwqp->n);
1007 for (i = 0; i < pwqp->n; ++i)
1008 pwf = isl_pw_qpolynomial_fold_add_piece(pwf,
1009 isl_set_copy(pwqp->p[i].set),
1010 isl_qpolynomial_fold_alloc(type,
1011 isl_qpolynomial_copy(pwqp->p[i].qp)));
1013 isl_pw_qpolynomial_free(pwqp);
1015 return pwf;
1018 __isl_give isl_pw_qpolynomial_fold *isl_pw_qpolynomial_fold_add(
1019 __isl_take isl_pw_qpolynomial_fold *pwf1,
1020 __isl_take isl_pw_qpolynomial_fold *pwf2)
1022 return isl_pw_qpolynomial_fold_union_add_(pwf1, pwf2);
1025 /* Compare two quasi-polynomial reductions.
1027 * Return -1 if "fold1" is "smaller" than "fold2", 1 if "fold1" is "greater"
1028 * than "fold2" and 0 if they are equal.
1030 int isl_qpolynomial_fold_plain_cmp(__isl_keep isl_qpolynomial_fold *fold1,
1031 __isl_keep isl_qpolynomial_fold *fold2)
1033 int i;
1035 if (fold1 == fold2)
1036 return 0;
1037 if (!fold1)
1038 return -1;
1039 if (!fold2)
1040 return 1;
1042 if (fold1->n != fold2->n)
1043 return fold1->n - fold2->n;
1045 for (i = 0; i < fold1->n; ++i) {
1046 int cmp;
1048 cmp = isl_qpolynomial_plain_cmp(fold1->qp[i], fold2->qp[i]);
1049 if (cmp != 0)
1050 return cmp;
1053 return 0;
1056 int isl_qpolynomial_fold_plain_is_equal(__isl_keep isl_qpolynomial_fold *fold1,
1057 __isl_keep isl_qpolynomial_fold *fold2)
1059 int i;
1061 if (!fold1 || !fold2)
1062 return -1;
1064 if (fold1->n != fold2->n)
1065 return 0;
1067 /* We probably want to sort the qps first... */
1068 for (i = 0; i < fold1->n; ++i) {
1069 int eq = isl_qpolynomial_plain_is_equal(fold1->qp[i], fold2->qp[i]);
1070 if (eq < 0 || !eq)
1071 return eq;
1074 return 1;
1077 __isl_give isl_val *isl_qpolynomial_fold_eval(
1078 __isl_take isl_qpolynomial_fold *fold, __isl_take isl_point *pnt)
1080 isl_ctx *ctx;
1081 isl_val *v;
1083 if (!fold || !pnt)
1084 goto error;
1085 ctx = isl_point_get_ctx(pnt);
1086 isl_assert(pnt->dim->ctx, isl_space_is_equal(pnt->dim, fold->dim), goto error);
1087 isl_assert(pnt->dim->ctx,
1088 fold->type == isl_fold_max || fold->type == isl_fold_min,
1089 goto error);
1091 if (fold->n == 0)
1092 v = isl_val_zero(ctx);
1093 else {
1094 int i;
1095 v = isl_qpolynomial_eval(isl_qpolynomial_copy(fold->qp[0]),
1096 isl_point_copy(pnt));
1097 for (i = 1; i < fold->n; ++i) {
1098 isl_val *v_i;
1099 v_i = isl_qpolynomial_eval(
1100 isl_qpolynomial_copy(fold->qp[i]),
1101 isl_point_copy(pnt));
1102 if (fold->type == isl_fold_max)
1103 v = isl_val_max(v, v_i);
1104 else
1105 v = isl_val_min(v, v_i);
1108 isl_qpolynomial_fold_free(fold);
1109 isl_point_free(pnt);
1111 return v;
1112 error:
1113 isl_qpolynomial_fold_free(fold);
1114 isl_point_free(pnt);
1115 return NULL;
1118 size_t isl_pw_qpolynomial_fold_size(__isl_keep isl_pw_qpolynomial_fold *pwf)
1120 int i;
1121 size_t n = 0;
1123 for (i = 0; i < pwf->n; ++i)
1124 n += pwf->p[i].fold->n;
1126 return n;
1129 __isl_give isl_val *isl_qpolynomial_fold_opt_on_domain(
1130 __isl_take isl_qpolynomial_fold *fold, __isl_take isl_set *set, int max)
1132 int i;
1133 isl_val *opt;
1135 if (!set || !fold)
1136 goto error;
1138 if (fold->n == 0) {
1139 opt = isl_val_zero(isl_set_get_ctx(set));
1140 isl_set_free(set);
1141 isl_qpolynomial_fold_free(fold);
1142 return opt;
1145 opt = isl_qpolynomial_opt_on_domain(isl_qpolynomial_copy(fold->qp[0]),
1146 isl_set_copy(set), max);
1147 for (i = 1; i < fold->n; ++i) {
1148 isl_val *opt_i;
1149 opt_i = isl_qpolynomial_opt_on_domain(
1150 isl_qpolynomial_copy(fold->qp[i]),
1151 isl_set_copy(set), max);
1152 if (max)
1153 opt = isl_val_max(opt, opt_i);
1154 else
1155 opt = isl_val_min(opt, opt_i);
1158 isl_set_free(set);
1159 isl_qpolynomial_fold_free(fold);
1161 return opt;
1162 error:
1163 isl_set_free(set);
1164 isl_qpolynomial_fold_free(fold);
1165 return NULL;
1168 /* Check whether for each quasi-polynomial in "fold2" there is
1169 * a quasi-polynomial in "fold1" that dominates it on "set".
1171 static int qpolynomial_fold_covers_on_domain(__isl_keep isl_set *set,
1172 __isl_keep isl_qpolynomial_fold *fold1,
1173 __isl_keep isl_qpolynomial_fold *fold2)
1175 int i, j;
1176 int covers;
1178 if (!set || !fold1 || !fold2)
1179 return -1;
1181 covers = fold1->type == isl_fold_max ? 1 : -1;
1183 for (i = 0; i < fold2->n; ++i) {
1184 for (j = 0; j < fold1->n; ++j) {
1185 isl_qpolynomial *d;
1186 int sgn;
1188 d = isl_qpolynomial_sub(
1189 isl_qpolynomial_copy(fold1->qp[j]),
1190 isl_qpolynomial_copy(fold2->qp[i]));
1191 sgn = isl_qpolynomial_sign(set, d);
1192 isl_qpolynomial_free(d);
1193 if (sgn == covers)
1194 break;
1196 if (j >= fold1->n)
1197 return 0;
1200 return 1;
1203 /* Check whether "pwf1" dominated "pwf2", i.e., the domain of "pwf1" contains
1204 * that of "pwf2" and on each cell, the corresponding fold from pwf1 dominates
1205 * that of pwf2.
1207 int isl_pw_qpolynomial_fold_covers(__isl_keep isl_pw_qpolynomial_fold *pwf1,
1208 __isl_keep isl_pw_qpolynomial_fold *pwf2)
1210 int i, j;
1211 isl_set *dom1, *dom2;
1212 int is_subset;
1214 if (!pwf1 || !pwf2)
1215 return -1;
1217 if (pwf2->n == 0)
1218 return 1;
1219 if (pwf1->n == 0)
1220 return 0;
1222 dom1 = isl_pw_qpolynomial_fold_domain(isl_pw_qpolynomial_fold_copy(pwf1));
1223 dom2 = isl_pw_qpolynomial_fold_domain(isl_pw_qpolynomial_fold_copy(pwf2));
1224 is_subset = isl_set_is_subset(dom2, dom1);
1225 isl_set_free(dom1);
1226 isl_set_free(dom2);
1228 if (is_subset < 0 || !is_subset)
1229 return is_subset;
1231 for (i = 0; i < pwf2->n; ++i) {
1232 for (j = 0; j < pwf1->n; ++j) {
1233 int is_empty;
1234 isl_set *common;
1235 int covers;
1237 common = isl_set_intersect(isl_set_copy(pwf1->p[j].set),
1238 isl_set_copy(pwf2->p[i].set));
1239 is_empty = isl_set_is_empty(common);
1240 if (is_empty < 0 || is_empty) {
1241 isl_set_free(common);
1242 if (is_empty < 0)
1243 return -1;
1244 continue;
1246 covers = qpolynomial_fold_covers_on_domain(common,
1247 pwf1->p[j].fold, pwf2->p[i].fold);
1248 isl_set_free(common);
1249 if (covers < 0 || !covers)
1250 return covers;
1254 return 1;
1257 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_morph_domain(
1258 __isl_take isl_qpolynomial_fold *fold, __isl_take isl_morph *morph)
1260 int i;
1261 isl_ctx *ctx;
1263 if (!fold || !morph)
1264 goto error;
1266 ctx = fold->dim->ctx;
1267 isl_assert(ctx, isl_space_is_equal(fold->dim, morph->dom->dim), goto error);
1269 fold = isl_qpolynomial_fold_cow(fold);
1270 if (!fold)
1271 goto error;
1273 isl_space_free(fold->dim);
1274 fold->dim = isl_space_copy(morph->ran->dim);
1275 if (!fold->dim)
1276 goto error;
1278 for (i = 0; i < fold->n; ++i) {
1279 fold->qp[i] = isl_qpolynomial_morph_domain(fold->qp[i],
1280 isl_morph_copy(morph));
1281 if (!fold->qp[i])
1282 goto error;
1285 isl_morph_free(morph);
1287 return fold;
1288 error:
1289 isl_qpolynomial_fold_free(fold);
1290 isl_morph_free(morph);
1291 return NULL;
1294 enum isl_fold isl_qpolynomial_fold_get_type(__isl_keep isl_qpolynomial_fold *fold)
1296 if (!fold)
1297 return isl_fold_list;
1298 return fold->type;
1301 enum isl_fold isl_union_pw_qpolynomial_fold_get_type(
1302 __isl_keep isl_union_pw_qpolynomial_fold *upwf)
1304 if (!upwf)
1305 return isl_fold_list;
1306 return upwf->type;
1309 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_lift(
1310 __isl_take isl_qpolynomial_fold *fold, __isl_take isl_space *dim)
1312 int i;
1314 if (!fold || !dim)
1315 goto error;
1317 if (isl_space_is_equal(fold->dim, dim)) {
1318 isl_space_free(dim);
1319 return fold;
1322 fold = isl_qpolynomial_fold_cow(fold);
1323 if (!fold)
1324 goto error;
1326 isl_space_free(fold->dim);
1327 fold->dim = isl_space_copy(dim);
1328 if (!fold->dim)
1329 goto error;
1331 for (i = 0; i < fold->n; ++i) {
1332 fold->qp[i] = isl_qpolynomial_lift(fold->qp[i],
1333 isl_space_copy(dim));
1334 if (!fold->qp[i])
1335 goto error;
1338 isl_space_free(dim);
1340 return fold;
1341 error:
1342 isl_qpolynomial_fold_free(fold);
1343 isl_space_free(dim);
1344 return NULL;
1347 isl_stat isl_qpolynomial_fold_foreach_qpolynomial(
1348 __isl_keep isl_qpolynomial_fold *fold,
1349 isl_stat (*fn)(__isl_take isl_qpolynomial *qp, void *user), void *user)
1351 int i;
1353 if (!fold)
1354 return isl_stat_error;
1356 for (i = 0; i < fold->n; ++i)
1357 if (fn(isl_qpolynomial_copy(fold->qp[i]), user) < 0)
1358 return isl_stat_error;
1360 return isl_stat_ok;
1363 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_move_dims(
1364 __isl_take isl_qpolynomial_fold *fold,
1365 enum isl_dim_type dst_type, unsigned dst_pos,
1366 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
1368 int i;
1370 if (n == 0)
1371 return fold;
1373 fold = isl_qpolynomial_fold_cow(fold);
1374 if (!fold)
1375 return NULL;
1377 fold->dim = isl_space_move_dims(fold->dim, dst_type, dst_pos,
1378 src_type, src_pos, n);
1379 if (!fold->dim)
1380 goto error;
1382 for (i = 0; i < fold->n; ++i) {
1383 fold->qp[i] = isl_qpolynomial_move_dims(fold->qp[i],
1384 dst_type, dst_pos, src_type, src_pos, n);
1385 if (!fold->qp[i])
1386 goto error;
1389 return fold;
1390 error:
1391 isl_qpolynomial_fold_free(fold);
1392 return NULL;
1395 /* For each 0 <= i < "n", replace variable "first" + i of type "type"
1396 * in fold->qp[k] by subs[i].
1398 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_substitute(
1399 __isl_take isl_qpolynomial_fold *fold,
1400 enum isl_dim_type type, unsigned first, unsigned n,
1401 __isl_keep isl_qpolynomial **subs)
1403 int i;
1405 if (n == 0)
1406 return fold;
1408 fold = isl_qpolynomial_fold_cow(fold);
1409 if (!fold)
1410 return NULL;
1412 for (i = 0; i < fold->n; ++i) {
1413 fold->qp[i] = isl_qpolynomial_substitute(fold->qp[i],
1414 type, first, n, subs);
1415 if (!fold->qp[i])
1416 goto error;
1419 return fold;
1420 error:
1421 isl_qpolynomial_fold_free(fold);
1422 return NULL;
1425 static isl_stat add_pwqp(__isl_take isl_pw_qpolynomial *pwqp, void *user)
1427 isl_ctx *ctx;
1428 isl_pw_qpolynomial_fold *pwf;
1429 isl_union_pw_qpolynomial_fold **upwf;
1430 struct isl_hash_table_entry *entry;
1432 upwf = (isl_union_pw_qpolynomial_fold **)user;
1434 ctx = pwqp->dim->ctx;
1435 entry = isl_union_pw_qpolynomial_fold_find_part_entry(*upwf,
1436 pwqp->dim, 1);
1437 if (!entry)
1438 goto error;
1440 pwf = isl_pw_qpolynomial_fold_from_pw_qpolynomial((*upwf)->type, pwqp);
1441 if (!entry->data)
1442 entry->data = pwf;
1443 else {
1444 entry->data = isl_pw_qpolynomial_fold_add(entry->data, pwf);
1445 if (!entry->data)
1446 return isl_stat_error;
1447 if (isl_pw_qpolynomial_fold_is_zero(entry->data))
1448 *upwf = isl_union_pw_qpolynomial_fold_remove_part_entry(
1449 *upwf, entry);
1452 return isl_stat_ok;
1453 error:
1454 isl_pw_qpolynomial_free(pwqp);
1455 return isl_stat_error;
1458 __isl_give isl_union_pw_qpolynomial_fold *isl_union_pw_qpolynomial_fold_add_union_pw_qpolynomial(
1459 __isl_take isl_union_pw_qpolynomial_fold *upwf,
1460 __isl_take isl_union_pw_qpolynomial *upwqp)
1462 upwf = isl_union_pw_qpolynomial_fold_align_params(upwf,
1463 isl_union_pw_qpolynomial_get_space(upwqp));
1464 upwqp = isl_union_pw_qpolynomial_align_params(upwqp,
1465 isl_union_pw_qpolynomial_fold_get_space(upwf));
1467 upwf = isl_union_pw_qpolynomial_fold_cow(upwf);
1468 if (!upwf || !upwqp)
1469 goto error;
1471 if (isl_union_pw_qpolynomial_foreach_pw_qpolynomial(upwqp, &add_pwqp,
1472 &upwf) < 0)
1473 goto error;
1475 isl_union_pw_qpolynomial_free(upwqp);
1477 return upwf;
1478 error:
1479 isl_union_pw_qpolynomial_fold_free(upwf);
1480 isl_union_pw_qpolynomial_free(upwqp);
1481 return NULL;
1484 static int join_compatible(__isl_keep isl_space *dim1, __isl_keep isl_space *dim2)
1486 int m;
1487 m = isl_space_match(dim1, isl_dim_param, dim2, isl_dim_param);
1488 if (m < 0 || !m)
1489 return m;
1490 return isl_space_tuple_is_equal(dim1, isl_dim_out, dim2, isl_dim_in);
1493 /* Compute the intersection of the range of the map and the domain
1494 * of the piecewise quasipolynomial reduction and then compute a bound
1495 * on the associated quasipolynomial reduction over all elements
1496 * in this intersection.
1498 * We first introduce some unconstrained dimensions in the
1499 * piecewise quasipolynomial, intersect the resulting domain
1500 * with the wrapped map and the compute the sum.
1502 __isl_give isl_pw_qpolynomial_fold *isl_map_apply_pw_qpolynomial_fold(
1503 __isl_take isl_map *map, __isl_take isl_pw_qpolynomial_fold *pwf,
1504 int *tight)
1506 isl_ctx *ctx;
1507 isl_set *dom;
1508 isl_space *map_dim;
1509 isl_space *pwf_dim;
1510 unsigned n_in;
1511 int ok;
1513 ctx = isl_map_get_ctx(map);
1514 if (!ctx)
1515 goto error;
1517 map_dim = isl_map_get_space(map);
1518 pwf_dim = isl_pw_qpolynomial_fold_get_space(pwf);
1519 ok = join_compatible(map_dim, pwf_dim);
1520 isl_space_free(map_dim);
1521 isl_space_free(pwf_dim);
1522 if (!ok)
1523 isl_die(ctx, isl_error_invalid, "incompatible dimensions",
1524 goto error);
1526 n_in = isl_map_dim(map, isl_dim_in);
1527 pwf = isl_pw_qpolynomial_fold_insert_dims(pwf, isl_dim_in, 0, n_in);
1529 dom = isl_map_wrap(map);
1530 pwf = isl_pw_qpolynomial_fold_reset_domain_space(pwf,
1531 isl_set_get_space(dom));
1533 pwf = isl_pw_qpolynomial_fold_intersect_domain(pwf, dom);
1534 pwf = isl_pw_qpolynomial_fold_bound(pwf, tight);
1536 return pwf;
1537 error:
1538 isl_map_free(map);
1539 isl_pw_qpolynomial_fold_free(pwf);
1540 return NULL;
1543 __isl_give isl_pw_qpolynomial_fold *isl_set_apply_pw_qpolynomial_fold(
1544 __isl_take isl_set *set, __isl_take isl_pw_qpolynomial_fold *pwf,
1545 int *tight)
1547 return isl_map_apply_pw_qpolynomial_fold(set, pwf, tight);
1550 struct isl_apply_fold_data {
1551 isl_union_pw_qpolynomial_fold *upwf;
1552 isl_union_pw_qpolynomial_fold *res;
1553 isl_map *map;
1554 int tight;
1557 static isl_stat pw_qpolynomial_fold_apply(
1558 __isl_take isl_pw_qpolynomial_fold *pwf, void *user)
1560 isl_space *map_dim;
1561 isl_space *pwf_dim;
1562 struct isl_apply_fold_data *data = user;
1563 int ok;
1565 map_dim = isl_map_get_space(data->map);
1566 pwf_dim = isl_pw_qpolynomial_fold_get_space(pwf);
1567 ok = join_compatible(map_dim, pwf_dim);
1568 isl_space_free(map_dim);
1569 isl_space_free(pwf_dim);
1571 if (ok) {
1572 pwf = isl_map_apply_pw_qpolynomial_fold(isl_map_copy(data->map),
1573 pwf, data->tight ? &data->tight : NULL);
1574 data->res = isl_union_pw_qpolynomial_fold_fold_pw_qpolynomial_fold(
1575 data->res, pwf);
1576 } else
1577 isl_pw_qpolynomial_fold_free(pwf);
1579 return isl_stat_ok;
1582 static isl_stat map_apply(__isl_take isl_map *map, void *user)
1584 struct isl_apply_fold_data *data = user;
1585 isl_stat r;
1587 data->map = map;
1588 r = isl_union_pw_qpolynomial_fold_foreach_pw_qpolynomial_fold(
1589 data->upwf, &pw_qpolynomial_fold_apply, data);
1591 isl_map_free(map);
1592 return r;
1595 __isl_give isl_union_pw_qpolynomial_fold *isl_union_map_apply_union_pw_qpolynomial_fold(
1596 __isl_take isl_union_map *umap,
1597 __isl_take isl_union_pw_qpolynomial_fold *upwf, int *tight)
1599 isl_space *dim;
1600 enum isl_fold type;
1601 struct isl_apply_fold_data data;
1603 upwf = isl_union_pw_qpolynomial_fold_align_params(upwf,
1604 isl_union_map_get_space(umap));
1605 umap = isl_union_map_align_params(umap,
1606 isl_union_pw_qpolynomial_fold_get_space(upwf));
1608 data.upwf = upwf;
1609 data.tight = tight ? 1 : 0;
1610 dim = isl_union_pw_qpolynomial_fold_get_space(upwf);
1611 type = isl_union_pw_qpolynomial_fold_get_type(upwf);
1612 data.res = isl_union_pw_qpolynomial_fold_zero(dim, type);
1613 if (isl_union_map_foreach_map(umap, &map_apply, &data) < 0)
1614 goto error;
1616 isl_union_map_free(umap);
1617 isl_union_pw_qpolynomial_fold_free(upwf);
1619 if (tight)
1620 *tight = data.tight;
1622 return data.res;
1623 error:
1624 isl_union_map_free(umap);
1625 isl_union_pw_qpolynomial_fold_free(upwf);
1626 isl_union_pw_qpolynomial_fold_free(data.res);
1627 return NULL;
1630 __isl_give isl_union_pw_qpolynomial_fold *isl_union_set_apply_union_pw_qpolynomial_fold(
1631 __isl_take isl_union_set *uset,
1632 __isl_take isl_union_pw_qpolynomial_fold *upwf, int *tight)
1634 return isl_union_map_apply_union_pw_qpolynomial_fold(uset, upwf, tight);
1637 /* Reorder the dimension of "fold" according to the given reordering.
1639 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_realign_domain(
1640 __isl_take isl_qpolynomial_fold *fold, __isl_take isl_reordering *r)
1642 int i;
1644 fold = isl_qpolynomial_fold_cow(fold);
1645 if (!fold || !r)
1646 goto error;
1648 for (i = 0; i < fold->n; ++i) {
1649 fold->qp[i] = isl_qpolynomial_realign_domain(fold->qp[i],
1650 isl_reordering_copy(r));
1651 if (!fold->qp[i])
1652 goto error;
1655 fold = isl_qpolynomial_fold_reset_domain_space(fold,
1656 isl_space_copy(r->dim));
1658 isl_reordering_free(r);
1660 return fold;
1661 error:
1662 isl_qpolynomial_fold_free(fold);
1663 isl_reordering_free(r);
1664 return NULL;
1667 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_mul_isl_int(
1668 __isl_take isl_qpolynomial_fold *fold, isl_int v)
1670 int i;
1672 if (isl_int_is_one(v))
1673 return fold;
1674 if (fold && isl_int_is_zero(v)) {
1675 isl_qpolynomial_fold *zero;
1676 isl_space *dim = isl_space_copy(fold->dim);
1677 zero = isl_qpolynomial_fold_empty(fold->type, dim);
1678 isl_qpolynomial_fold_free(fold);
1679 return zero;
1682 fold = isl_qpolynomial_fold_cow(fold);
1683 if (!fold)
1684 return NULL;
1686 if (isl_int_is_neg(v))
1687 fold->type = isl_fold_type_negate(fold->type);
1688 for (i = 0; i < fold->n; ++i) {
1689 fold->qp[i] = isl_qpolynomial_mul_isl_int(fold->qp[i], v);
1690 if (!fold->qp[i])
1691 goto error;
1694 return fold;
1695 error:
1696 isl_qpolynomial_fold_free(fold);
1697 return NULL;
1700 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_scale(
1701 __isl_take isl_qpolynomial_fold *fold, isl_int v)
1703 return isl_qpolynomial_fold_mul_isl_int(fold, v);
1706 /* Multiply "fold" by "v".
1708 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_scale_val(
1709 __isl_take isl_qpolynomial_fold *fold, __isl_take isl_val *v)
1711 int i;
1713 if (!fold || !v)
1714 goto error;
1716 if (isl_val_is_one(v)) {
1717 isl_val_free(v);
1718 return fold;
1720 if (isl_val_is_zero(v)) {
1721 isl_qpolynomial_fold *zero;
1722 isl_space *space = isl_qpolynomial_fold_get_domain_space(fold);
1723 zero = isl_qpolynomial_fold_empty(fold->type, space);
1724 isl_qpolynomial_fold_free(fold);
1725 isl_val_free(v);
1726 return zero;
1728 if (!isl_val_is_rat(v))
1729 isl_die(isl_qpolynomial_fold_get_ctx(fold), isl_error_invalid,
1730 "expecting rational factor", goto error);
1732 fold = isl_qpolynomial_fold_cow(fold);
1733 if (!fold)
1734 goto error;
1736 if (isl_val_is_neg(v))
1737 fold->type = isl_fold_type_negate(fold->type);
1738 for (i = 0; i < fold->n; ++i) {
1739 fold->qp[i] = isl_qpolynomial_scale_val(fold->qp[i],
1740 isl_val_copy(v));
1741 if (!fold->qp[i])
1742 goto error;
1745 isl_val_free(v);
1746 return fold;
1747 error:
1748 isl_val_free(v);
1749 isl_qpolynomial_fold_free(fold);
1750 return NULL;
1753 /* Divide "fold" by "v".
1755 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_scale_down_val(
1756 __isl_take isl_qpolynomial_fold *fold, __isl_take isl_val *v)
1758 if (!fold || !v)
1759 goto error;
1761 if (isl_val_is_one(v)) {
1762 isl_val_free(v);
1763 return fold;
1765 if (!isl_val_is_rat(v))
1766 isl_die(isl_qpolynomial_fold_get_ctx(fold), isl_error_invalid,
1767 "expecting rational factor", goto error);
1768 if (isl_val_is_zero(v))
1769 isl_die(isl_val_get_ctx(v), isl_error_invalid,
1770 "cannot scale down by zero", goto error);
1772 return isl_qpolynomial_fold_scale_val(fold, isl_val_inv(v));
1773 error:
1774 isl_val_free(v);
1775 isl_qpolynomial_fold_free(fold);
1776 return NULL;