isl_constraint.c: use isl_stat enum instead of plain integers
[isl.git] / isl_fold.c
blob8e7779f9a8aa1dfd86107e1d04ac1ade48f13b7d
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 #include <isl_map_private.h>
12 #include <isl_union_map_private.h>
13 #include <isl_polynomial_private.h>
14 #include <isl_point_private.h>
15 #include <isl_space_private.h>
16 #include <isl_lp_private.h>
17 #include <isl_seq.h>
18 #include <isl_mat_private.h>
19 #include <isl_val_private.h>
20 #include <isl_vec_private.h>
21 #include <isl_config.h>
23 enum isl_fold isl_fold_type_negate(enum isl_fold type)
25 switch (type) {
26 case isl_fold_min:
27 return isl_fold_max;
28 case isl_fold_max:
29 return isl_fold_min;
30 case isl_fold_list:
31 return isl_fold_list;
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;
42 if (!dim)
43 goto error;
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 *));
49 if (!fold)
50 goto error;
52 fold->ref = 1;
53 fold->size = n;
54 fold->n = 0;
55 fold->type = type;
56 fold->dim = dim;
58 return fold;
59 error:
60 isl_space_free(dim);
61 return NULL;
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)
78 isl_space *space;
79 if (!fold)
80 return NULL;
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);
84 return space;
87 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_reset_domain_space(
88 __isl_take isl_qpolynomial_fold *fold, __isl_take isl_space *dim)
90 int i;
92 fold = isl_qpolynomial_fold_cow(fold);
93 if (!fold || !dim)
94 goto error;
96 for (i = 0; i < fold->n; ++i) {
97 fold->qp[i] = isl_qpolynomial_reset_domain_space(fold->qp[i],
98 isl_space_copy(dim));
99 if (!fold->qp[i])
100 goto error;
103 isl_space_free(fold->dim);
104 fold->dim = dim;
106 return fold;
107 error:
108 isl_qpolynomial_fold_free(fold);
109 isl_space_free(dim);
110 return NULL;
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)
128 int i;
130 if (!fold)
131 return -1;
132 if (fold->n == 0 || n == 0)
133 return 0;
135 for (i = 0; i < fold->n; ++i) {
136 int involves = isl_qpolynomial_involves_dims(fold->qp[i],
137 type, first, n);
138 if (involves < 0 || involves)
139 return involves;
141 return 0;
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)
148 int i;
150 fold = isl_qpolynomial_fold_cow(fold);
151 if (!fold)
152 return NULL;
153 fold->dim = isl_space_set_dim_name(fold->dim, type, pos, s);
154 if (!fold->dim)
155 goto error;
157 for (i = 0; i < fold->n; ++i) {
158 fold->qp[i] = isl_qpolynomial_set_dim_name(fold->qp[i],
159 type, pos, s);
160 if (!fold->qp[i])
161 goto error;
164 return fold;
165 error:
166 isl_qpolynomial_fold_free(fold);
167 return NULL;
170 /* Given a dimension type for an isl_qpolynomial_fold,
171 * return the corresponding type for the domain.
173 static enum isl_dim_type domain_type(enum isl_dim_type type)
175 if (type == isl_dim_in)
176 return isl_dim_set;
177 return type;
180 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_drop_dims(
181 __isl_take isl_qpolynomial_fold *fold,
182 enum isl_dim_type type, unsigned first, unsigned n)
184 int i;
185 enum isl_dim_type set_type;
187 if (!fold)
188 return NULL;
189 if (n == 0)
190 return fold;
192 set_type = domain_type(type);
194 fold = isl_qpolynomial_fold_cow(fold);
195 if (!fold)
196 return NULL;
197 fold->dim = isl_space_drop_dims(fold->dim, set_type, first, n);
198 if (!fold->dim)
199 goto error;
201 for (i = 0; i < fold->n; ++i) {
202 fold->qp[i] = isl_qpolynomial_drop_dims(fold->qp[i],
203 type, first, n);
204 if (!fold->qp[i])
205 goto error;
208 return fold;
209 error:
210 isl_qpolynomial_fold_free(fold);
211 return NULL;
214 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_insert_dims(
215 __isl_take isl_qpolynomial_fold *fold,
216 enum isl_dim_type type, unsigned first, unsigned n)
218 int i;
220 if (!fold)
221 return NULL;
222 if (n == 0 && !isl_space_is_named_or_nested(fold->dim, type))
223 return fold;
225 fold = isl_qpolynomial_fold_cow(fold);
226 if (!fold)
227 return NULL;
228 fold->dim = isl_space_insert_dims(fold->dim, type, first, n);
229 if (!fold->dim)
230 goto error;
232 for (i = 0; i < fold->n; ++i) {
233 fold->qp[i] = isl_qpolynomial_insert_dims(fold->qp[i],
234 type, first, n);
235 if (!fold->qp[i])
236 goto error;
239 return fold;
240 error:
241 isl_qpolynomial_fold_free(fold);
242 return NULL;
245 /* Determine the sign of the constant quasipolynomial "qp".
247 * Return
248 * -1 if qp <= 0
249 * 1 if qp >= 0
250 * 0 if unknown
252 * For qp == 0, we can return either -1 or 1. In practice, we return 1.
253 * For qp == NaN, the sign is undefined, so we return 0.
255 static int isl_qpolynomial_cst_sign(__isl_keep isl_qpolynomial *qp)
257 struct isl_upoly_cst *cst;
259 if (isl_qpolynomial_is_nan(qp))
260 return 0;
262 cst = isl_upoly_as_cst(qp->upoly);
263 if (!cst)
264 return 0;
266 return isl_int_sgn(cst->n) < 0 ? -1 : 1;
269 static int isl_qpolynomial_aff_sign(__isl_keep isl_set *set,
270 __isl_keep isl_qpolynomial *qp)
272 enum isl_lp_result res;
273 isl_vec *aff;
274 isl_int opt;
275 int sgn = 0;
277 aff = isl_qpolynomial_extract_affine(qp);
278 if (!aff)
279 return 0;
281 isl_int_init(opt);
283 res = isl_set_solve_lp(set, 0, aff->el + 1, aff->el[0],
284 &opt, NULL, NULL);
285 if (res == isl_lp_error)
286 goto done;
287 if (res == isl_lp_empty ||
288 (res == isl_lp_ok && !isl_int_is_neg(opt))) {
289 sgn = 1;
290 goto done;
293 res = isl_set_solve_lp(set, 1, aff->el + 1, aff->el[0],
294 &opt, NULL, NULL);
295 if (res == isl_lp_ok && !isl_int_is_pos(opt))
296 sgn = -1;
298 done:
299 isl_int_clear(opt);
300 isl_vec_free(aff);
301 return sgn;
304 /* Determine, if possible, the sign of the quasipolynomial "qp" on
305 * the domain "set".
307 * If qp is a constant, then the problem is trivial.
308 * If qp is linear, then we check if the minimum of the corresponding
309 * affine constraint is non-negative or if the maximum is non-positive.
311 * Otherwise, we check if the outermost variable "v" has a lower bound "l"
312 * in "set". If so, we write qp(v,v') as
314 * q(v,v') * (v - l) + r(v')
316 * if q(v,v') and r(v') have the same known sign, then the original
317 * quasipolynomial has the same sign as well.
319 * Return
320 * -1 if qp <= 0
321 * 1 if qp >= 0
322 * 0 if unknown
324 static int isl_qpolynomial_sign(__isl_keep isl_set *set,
325 __isl_keep isl_qpolynomial *qp)
327 int d;
328 int i;
329 int is;
330 struct isl_upoly_rec *rec;
331 isl_vec *v;
332 isl_int l;
333 enum isl_lp_result res;
334 int sgn = 0;
336 is = isl_qpolynomial_is_cst(qp, NULL, NULL);
337 if (is < 0)
338 return 0;
339 if (is)
340 return isl_qpolynomial_cst_sign(qp);
342 is = isl_qpolynomial_is_affine(qp);
343 if (is < 0)
344 return 0;
345 if (is)
346 return isl_qpolynomial_aff_sign(set, qp);
348 if (qp->div->n_row > 0)
349 return 0;
351 rec = isl_upoly_as_rec(qp->upoly);
352 if (!rec)
353 return 0;
355 d = isl_space_dim(qp->dim, isl_dim_all);
356 v = isl_vec_alloc(set->ctx, 2 + d);
357 if (!v)
358 return 0;
360 isl_seq_clr(v->el + 1, 1 + d);
361 isl_int_set_si(v->el[0], 1);
362 isl_int_set_si(v->el[2 + qp->upoly->var], 1);
364 isl_int_init(l);
366 res = isl_set_solve_lp(set, 0, v->el + 1, v->el[0], &l, NULL, NULL);
367 if (res == isl_lp_ok) {
368 isl_qpolynomial *min;
369 isl_qpolynomial *base;
370 isl_qpolynomial *r, *q;
371 isl_qpolynomial *t;
373 min = isl_qpolynomial_cst_on_domain(isl_space_copy(qp->dim), l);
374 base = isl_qpolynomial_var_pow_on_domain(isl_space_copy(qp->dim),
375 qp->upoly->var, 1);
377 r = isl_qpolynomial_alloc(isl_space_copy(qp->dim), 0,
378 isl_upoly_copy(rec->p[rec->n - 1]));
379 q = isl_qpolynomial_copy(r);
381 for (i = rec->n - 2; i >= 0; --i) {
382 r = isl_qpolynomial_mul(r, isl_qpolynomial_copy(min));
383 t = isl_qpolynomial_alloc(isl_space_copy(qp->dim), 0,
384 isl_upoly_copy(rec->p[i]));
385 r = isl_qpolynomial_add(r, t);
386 if (i == 0)
387 break;
388 q = isl_qpolynomial_mul(q, isl_qpolynomial_copy(base));
389 q = isl_qpolynomial_add(q, isl_qpolynomial_copy(r));
392 if (isl_qpolynomial_is_zero(q))
393 sgn = isl_qpolynomial_sign(set, r);
394 else if (isl_qpolynomial_is_zero(r))
395 sgn = isl_qpolynomial_sign(set, q);
396 else {
397 int sgn_q, sgn_r;
398 sgn_r = isl_qpolynomial_sign(set, r);
399 sgn_q = isl_qpolynomial_sign(set, q);
400 if (sgn_r == sgn_q)
401 sgn = sgn_r;
404 isl_qpolynomial_free(min);
405 isl_qpolynomial_free(base);
406 isl_qpolynomial_free(q);
407 isl_qpolynomial_free(r);
410 isl_int_clear(l);
412 isl_vec_free(v);
414 return sgn;
417 /* Combine "fold1" and "fold2" into a single reduction, eliminating
418 * those elements of one reduction that are already covered by the other
419 * reduction on "set".
421 * If "fold1" or "fold2" is an empty reduction, then return
422 * the other reduction.
423 * If "fold1" or "fold2" is a NaN, then return this NaN.
425 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_fold_on_domain(
426 __isl_keep isl_set *set,
427 __isl_take isl_qpolynomial_fold *fold1,
428 __isl_take isl_qpolynomial_fold *fold2)
430 int i, j;
431 int n1;
432 struct isl_qpolynomial_fold *res = NULL;
433 int better;
435 if (!fold1 || !fold2)
436 goto error;
438 isl_assert(fold1->dim->ctx, fold1->type == fold2->type, goto error);
439 isl_assert(fold1->dim->ctx, isl_space_is_equal(fold1->dim, fold2->dim),
440 goto error);
442 better = fold1->type == isl_fold_max ? -1 : 1;
444 if (isl_qpolynomial_fold_is_empty(fold1) ||
445 isl_qpolynomial_fold_is_nan(fold2)) {
446 isl_qpolynomial_fold_free(fold1);
447 return fold2;
450 if (isl_qpolynomial_fold_is_empty(fold2) ||
451 isl_qpolynomial_fold_is_nan(fold1)) {
452 isl_qpolynomial_fold_free(fold2);
453 return fold1;
456 res = qpolynomial_fold_alloc(fold1->type, isl_space_copy(fold1->dim),
457 fold1->n + fold2->n);
458 if (!res)
459 goto error;
461 for (i = 0; i < fold1->n; ++i) {
462 res->qp[res->n] = isl_qpolynomial_copy(fold1->qp[i]);
463 if (!res->qp[res->n])
464 goto error;
465 res->n++;
467 n1 = res->n;
469 for (i = 0; i < fold2->n; ++i) {
470 for (j = n1 - 1; j >= 0; --j) {
471 isl_qpolynomial *d;
472 int sgn, equal;
473 equal = isl_qpolynomial_plain_is_equal(res->qp[j],
474 fold2->qp[i]);
475 if (equal < 0)
476 goto error;
477 if (equal)
478 break;
479 d = isl_qpolynomial_sub(
480 isl_qpolynomial_copy(res->qp[j]),
481 isl_qpolynomial_copy(fold2->qp[i]));
482 sgn = isl_qpolynomial_sign(set, d);
483 isl_qpolynomial_free(d);
484 if (sgn == 0)
485 continue;
486 if (sgn != better)
487 break;
488 isl_qpolynomial_free(res->qp[j]);
489 if (j != n1 - 1)
490 res->qp[j] = res->qp[n1 - 1];
491 n1--;
492 if (n1 != res->n - 1)
493 res->qp[n1] = res->qp[res->n - 1];
494 res->n--;
496 if (j >= 0)
497 continue;
498 res->qp[res->n] = isl_qpolynomial_copy(fold2->qp[i]);
499 if (!res->qp[res->n])
500 goto error;
501 res->n++;
504 isl_qpolynomial_fold_free(fold1);
505 isl_qpolynomial_fold_free(fold2);
507 return res;
508 error:
509 isl_qpolynomial_fold_free(res);
510 isl_qpolynomial_fold_free(fold1);
511 isl_qpolynomial_fold_free(fold2);
512 return NULL;
515 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_add_qpolynomial(
516 __isl_take isl_qpolynomial_fold *fold, __isl_take isl_qpolynomial *qp)
518 int i;
520 if (!fold || !qp)
521 goto error;
523 if (isl_qpolynomial_is_zero(qp)) {
524 isl_qpolynomial_free(qp);
525 return fold;
528 fold = isl_qpolynomial_fold_cow(fold);
529 if (!fold)
530 goto error;
532 for (i = 0; i < fold->n; ++i) {
533 fold->qp[i] = isl_qpolynomial_add(fold->qp[i],
534 isl_qpolynomial_copy(qp));
535 if (!fold->qp[i])
536 goto error;
539 isl_qpolynomial_free(qp);
540 return fold;
541 error:
542 isl_qpolynomial_fold_free(fold);
543 isl_qpolynomial_free(qp);
544 return NULL;
547 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_add_on_domain(
548 __isl_keep isl_set *dom,
549 __isl_take isl_qpolynomial_fold *fold1,
550 __isl_take isl_qpolynomial_fold *fold2)
552 int i;
553 isl_qpolynomial_fold *res = NULL;
555 if (!fold1 || !fold2)
556 goto error;
558 if (isl_qpolynomial_fold_is_empty(fold1)) {
559 isl_qpolynomial_fold_free(fold1);
560 return fold2;
563 if (isl_qpolynomial_fold_is_empty(fold2)) {
564 isl_qpolynomial_fold_free(fold2);
565 return fold1;
568 if (fold1->n == 1 && fold2->n != 1)
569 return isl_qpolynomial_fold_add_on_domain(dom, fold2, fold1);
571 if (fold2->n == 1) {
572 res = isl_qpolynomial_fold_add_qpolynomial(fold1,
573 isl_qpolynomial_copy(fold2->qp[0]));
574 isl_qpolynomial_fold_free(fold2);
575 return res;
578 res = isl_qpolynomial_fold_add_qpolynomial(
579 isl_qpolynomial_fold_copy(fold1),
580 isl_qpolynomial_copy(fold2->qp[0]));
582 for (i = 1; i < fold2->n; ++i) {
583 isl_qpolynomial_fold *res_i;
584 res_i = isl_qpolynomial_fold_add_qpolynomial(
585 isl_qpolynomial_fold_copy(fold1),
586 isl_qpolynomial_copy(fold2->qp[i]));
587 res = isl_qpolynomial_fold_fold_on_domain(dom, res, res_i);
590 isl_qpolynomial_fold_free(fold1);
591 isl_qpolynomial_fold_free(fold2);
592 return res;
593 error:
594 isl_qpolynomial_fold_free(res);
595 isl_qpolynomial_fold_free(fold1);
596 isl_qpolynomial_fold_free(fold2);
597 return NULL;
600 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_substitute_equalities(
601 __isl_take isl_qpolynomial_fold *fold, __isl_take isl_basic_set *eq)
603 int i;
605 if (!fold || !eq)
606 goto error;
608 fold = isl_qpolynomial_fold_cow(fold);
609 if (!fold)
610 return NULL;
612 for (i = 0; i < fold->n; ++i) {
613 fold->qp[i] = isl_qpolynomial_substitute_equalities(fold->qp[i],
614 isl_basic_set_copy(eq));
615 if (!fold->qp[i])
616 goto error;
619 isl_basic_set_free(eq);
620 return fold;
621 error:
622 isl_basic_set_free(eq);
623 isl_qpolynomial_fold_free(fold);
624 return NULL;
627 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_gist(
628 __isl_take isl_qpolynomial_fold *fold, __isl_take isl_set *context)
630 int i;
632 if (!fold || !context)
633 goto error;
635 fold = isl_qpolynomial_fold_cow(fold);
636 if (!fold)
637 return NULL;
639 for (i = 0; i < fold->n; ++i) {
640 fold->qp[i] = isl_qpolynomial_gist(fold->qp[i],
641 isl_set_copy(context));
642 if (!fold->qp[i])
643 goto error;
646 isl_set_free(context);
647 return fold;
648 error:
649 isl_set_free(context);
650 isl_qpolynomial_fold_free(fold);
651 return NULL;
654 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_gist_params(
655 __isl_take isl_qpolynomial_fold *fold, __isl_take isl_set *context)
657 isl_space *space = isl_qpolynomial_fold_get_domain_space(fold);
658 isl_set *dom_context = isl_set_universe(space);
659 dom_context = isl_set_intersect_params(dom_context, context);
660 return isl_qpolynomial_fold_gist(fold, dom_context);
663 #define isl_qpolynomial_fold_involves_nan isl_qpolynomial_fold_is_nan
665 #define HAS_TYPE
667 #undef PW
668 #define PW isl_pw_qpolynomial_fold
669 #undef EL
670 #define EL isl_qpolynomial_fold
671 #undef EL_IS_ZERO
672 #define EL_IS_ZERO is_empty
673 #undef ZERO
674 #define ZERO zero
675 #undef IS_ZERO
676 #define IS_ZERO is_zero
677 #undef FIELD
678 #define FIELD fold
679 #undef DEFAULT_IS_ZERO
680 #define DEFAULT_IS_ZERO 1
682 #define NO_NEG
683 #define NO_SUB
684 #define NO_PULLBACK
686 #include <isl_pw_templ.c>
687 #include <isl_pw_eval.c>
689 #undef UNION
690 #define UNION isl_union_pw_qpolynomial_fold
691 #undef PART
692 #define PART isl_pw_qpolynomial_fold
693 #undef PARTS
694 #define PARTS pw_qpolynomial_fold
696 #define NO_SUB
698 #include <isl_union_single.c>
699 #include <isl_union_eval.c>
701 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_empty(enum isl_fold type,
702 __isl_take isl_space *dim)
704 return qpolynomial_fold_alloc(type, dim, 0);
707 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_alloc(
708 enum isl_fold type, __isl_take isl_qpolynomial *qp)
710 isl_qpolynomial_fold *fold;
712 if (!qp)
713 return NULL;
715 fold = qpolynomial_fold_alloc(type, isl_space_copy(qp->dim), 1);
716 if (!fold)
717 goto error;
719 fold->qp[0] = qp;
720 fold->n++;
722 return fold;
723 error:
724 isl_qpolynomial_fold_free(fold);
725 isl_qpolynomial_free(qp);
726 return NULL;
729 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_copy(
730 __isl_keep isl_qpolynomial_fold *fold)
732 if (!fold)
733 return NULL;
735 fold->ref++;
736 return fold;
739 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_dup(
740 __isl_keep isl_qpolynomial_fold *fold)
742 int i;
743 isl_qpolynomial_fold *dup;
745 if (!fold)
746 return NULL;
747 dup = qpolynomial_fold_alloc(fold->type,
748 isl_space_copy(fold->dim), fold->n);
749 if (!dup)
750 return NULL;
752 dup->n = fold->n;
753 for (i = 0; i < fold->n; ++i) {
754 dup->qp[i] = isl_qpolynomial_copy(fold->qp[i]);
755 if (!dup->qp[i])
756 goto error;
759 return dup;
760 error:
761 isl_qpolynomial_fold_free(dup);
762 return NULL;
765 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_cow(
766 __isl_take isl_qpolynomial_fold *fold)
768 if (!fold)
769 return NULL;
771 if (fold->ref == 1)
772 return fold;
773 fold->ref--;
774 return isl_qpolynomial_fold_dup(fold);
777 void isl_qpolynomial_fold_free(__isl_take isl_qpolynomial_fold *fold)
779 int i;
781 if (!fold)
782 return;
783 if (--fold->ref > 0)
784 return;
786 for (i = 0; i < fold->n; ++i)
787 isl_qpolynomial_free(fold->qp[i]);
788 isl_space_free(fold->dim);
789 free(fold);
792 int isl_qpolynomial_fold_is_empty(__isl_keep isl_qpolynomial_fold *fold)
794 if (!fold)
795 return -1;
797 return fold->n == 0;
800 /* Does "fold" represent max(NaN) or min(NaN)?
802 isl_bool isl_qpolynomial_fold_is_nan(__isl_keep isl_qpolynomial_fold *fold)
804 if (!fold)
805 return isl_bool_error;
806 if (fold->n != 1)
807 return isl_bool_false;
808 return isl_qpolynomial_is_nan(fold->qp[0]);
811 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_fold(
812 __isl_take isl_qpolynomial_fold *fold1,
813 __isl_take isl_qpolynomial_fold *fold2)
815 int i;
816 struct isl_qpolynomial_fold *res = NULL;
818 if (!fold1 || !fold2)
819 goto error;
821 isl_assert(fold1->dim->ctx, fold1->type == fold2->type, goto error);
822 isl_assert(fold1->dim->ctx, isl_space_is_equal(fold1->dim, fold2->dim),
823 goto error);
825 if (isl_qpolynomial_fold_is_empty(fold1)) {
826 isl_qpolynomial_fold_free(fold1);
827 return fold2;
830 if (isl_qpolynomial_fold_is_empty(fold2)) {
831 isl_qpolynomial_fold_free(fold2);
832 return fold1;
835 res = qpolynomial_fold_alloc(fold1->type, isl_space_copy(fold1->dim),
836 fold1->n + fold2->n);
837 if (!res)
838 goto error;
840 for (i = 0; i < fold1->n; ++i) {
841 res->qp[res->n] = isl_qpolynomial_copy(fold1->qp[i]);
842 if (!res->qp[res->n])
843 goto error;
844 res->n++;
847 for (i = 0; i < fold2->n; ++i) {
848 res->qp[res->n] = isl_qpolynomial_copy(fold2->qp[i]);
849 if (!res->qp[res->n])
850 goto error;
851 res->n++;
854 isl_qpolynomial_fold_free(fold1);
855 isl_qpolynomial_fold_free(fold2);
857 return res;
858 error:
859 isl_qpolynomial_fold_free(res);
860 isl_qpolynomial_fold_free(fold1);
861 isl_qpolynomial_fold_free(fold2);
862 return NULL;
865 __isl_give isl_pw_qpolynomial_fold *isl_pw_qpolynomial_fold_fold(
866 __isl_take isl_pw_qpolynomial_fold *pw1,
867 __isl_take isl_pw_qpolynomial_fold *pw2)
869 int i, j, n;
870 struct isl_pw_qpolynomial_fold *res;
871 isl_set *set;
873 if (!pw1 || !pw2)
874 goto error;
876 isl_assert(pw1->dim->ctx, isl_space_is_equal(pw1->dim, pw2->dim), goto error);
878 if (isl_pw_qpolynomial_fold_is_zero(pw1)) {
879 isl_pw_qpolynomial_fold_free(pw1);
880 return pw2;
883 if (isl_pw_qpolynomial_fold_is_zero(pw2)) {
884 isl_pw_qpolynomial_fold_free(pw2);
885 return pw1;
888 if (pw1->type != pw2->type)
889 isl_die(pw1->dim->ctx, isl_error_invalid,
890 "fold types don't match", goto error);
892 n = (pw1->n + 1) * (pw2->n + 1);
893 res = isl_pw_qpolynomial_fold_alloc_size(isl_space_copy(pw1->dim),
894 pw1->type, n);
896 for (i = 0; i < pw1->n; ++i) {
897 set = isl_set_copy(pw1->p[i].set);
898 for (j = 0; j < pw2->n; ++j) {
899 struct isl_set *common;
900 isl_qpolynomial_fold *sum;
901 set = isl_set_subtract(set,
902 isl_set_copy(pw2->p[j].set));
903 common = isl_set_intersect(isl_set_copy(pw1->p[i].set),
904 isl_set_copy(pw2->p[j].set));
905 if (isl_set_plain_is_empty(common)) {
906 isl_set_free(common);
907 continue;
910 sum = isl_qpolynomial_fold_fold_on_domain(common,
911 isl_qpolynomial_fold_copy(pw1->p[i].fold),
912 isl_qpolynomial_fold_copy(pw2->p[j].fold));
914 res = isl_pw_qpolynomial_fold_add_piece(res, common, sum);
916 res = isl_pw_qpolynomial_fold_add_piece(res, set,
917 isl_qpolynomial_fold_copy(pw1->p[i].fold));
920 for (j = 0; j < pw2->n; ++j) {
921 set = isl_set_copy(pw2->p[j].set);
922 for (i = 0; i < pw1->n; ++i)
923 set = isl_set_subtract(set, isl_set_copy(pw1->p[i].set));
924 res = isl_pw_qpolynomial_fold_add_piece(res, set,
925 isl_qpolynomial_fold_copy(pw2->p[j].fold));
928 isl_pw_qpolynomial_fold_free(pw1);
929 isl_pw_qpolynomial_fold_free(pw2);
931 return res;
932 error:
933 isl_pw_qpolynomial_fold_free(pw1);
934 isl_pw_qpolynomial_fold_free(pw2);
935 return NULL;
938 __isl_give isl_union_pw_qpolynomial_fold *isl_union_pw_qpolynomial_fold_fold_pw_qpolynomial_fold(
939 __isl_take isl_union_pw_qpolynomial_fold *u,
940 __isl_take isl_pw_qpolynomial_fold *part)
942 struct isl_hash_table_entry *entry;
944 u = isl_union_pw_qpolynomial_fold_cow(u);
946 if (!part || !u)
947 goto error;
948 if (isl_space_check_equal_params(part->dim, u->space) < 0)
949 goto error;
951 entry = isl_union_pw_qpolynomial_fold_find_part_entry(u, part->dim, 1);
952 if (!entry)
953 goto error;
955 if (!entry->data)
956 entry->data = part;
957 else {
958 entry->data = isl_pw_qpolynomial_fold_fold(entry->data,
959 isl_pw_qpolynomial_fold_copy(part));
960 if (!entry->data)
961 goto error;
962 isl_pw_qpolynomial_fold_free(part);
965 return u;
966 error:
967 isl_pw_qpolynomial_fold_free(part);
968 isl_union_pw_qpolynomial_fold_free(u);
969 return NULL;
972 static isl_stat fold_part(__isl_take isl_pw_qpolynomial_fold *part, void *user)
974 isl_union_pw_qpolynomial_fold **u;
975 u = (isl_union_pw_qpolynomial_fold **)user;
977 *u = isl_union_pw_qpolynomial_fold_fold_pw_qpolynomial_fold(*u, part);
979 return isl_stat_ok;
982 __isl_give isl_union_pw_qpolynomial_fold *isl_union_pw_qpolynomial_fold_fold(
983 __isl_take isl_union_pw_qpolynomial_fold *u1,
984 __isl_take isl_union_pw_qpolynomial_fold *u2)
986 u1 = isl_union_pw_qpolynomial_fold_cow(u1);
988 if (!u1 || !u2)
989 goto error;
991 if (isl_union_pw_qpolynomial_fold_foreach_pw_qpolynomial_fold(u2,
992 &fold_part, &u1) < 0)
993 goto error;
995 isl_union_pw_qpolynomial_fold_free(u2);
997 return u1;
998 error:
999 isl_union_pw_qpolynomial_fold_free(u1);
1000 isl_union_pw_qpolynomial_fold_free(u2);
1001 return NULL;
1004 __isl_give isl_pw_qpolynomial_fold *isl_pw_qpolynomial_fold_from_pw_qpolynomial(
1005 enum isl_fold type, __isl_take isl_pw_qpolynomial *pwqp)
1007 int i;
1008 isl_pw_qpolynomial_fold *pwf;
1010 if (!pwqp)
1011 return NULL;
1013 pwf = isl_pw_qpolynomial_fold_alloc_size(isl_space_copy(pwqp->dim),
1014 type, pwqp->n);
1016 for (i = 0; i < pwqp->n; ++i)
1017 pwf = isl_pw_qpolynomial_fold_add_piece(pwf,
1018 isl_set_copy(pwqp->p[i].set),
1019 isl_qpolynomial_fold_alloc(type,
1020 isl_qpolynomial_copy(pwqp->p[i].qp)));
1022 isl_pw_qpolynomial_free(pwqp);
1024 return pwf;
1027 __isl_give isl_pw_qpolynomial_fold *isl_pw_qpolynomial_fold_add(
1028 __isl_take isl_pw_qpolynomial_fold *pwf1,
1029 __isl_take isl_pw_qpolynomial_fold *pwf2)
1031 return isl_pw_qpolynomial_fold_union_add_(pwf1, pwf2);
1034 /* Compare two quasi-polynomial reductions.
1036 * Return -1 if "fold1" is "smaller" than "fold2", 1 if "fold1" is "greater"
1037 * than "fold2" and 0 if they are equal.
1039 int isl_qpolynomial_fold_plain_cmp(__isl_keep isl_qpolynomial_fold *fold1,
1040 __isl_keep isl_qpolynomial_fold *fold2)
1042 int i;
1044 if (fold1 == fold2)
1045 return 0;
1046 if (!fold1)
1047 return -1;
1048 if (!fold2)
1049 return 1;
1051 if (fold1->n != fold2->n)
1052 return fold1->n - fold2->n;
1054 for (i = 0; i < fold1->n; ++i) {
1055 int cmp;
1057 cmp = isl_qpolynomial_plain_cmp(fold1->qp[i], fold2->qp[i]);
1058 if (cmp != 0)
1059 return cmp;
1062 return 0;
1065 int isl_qpolynomial_fold_plain_is_equal(__isl_keep isl_qpolynomial_fold *fold1,
1066 __isl_keep isl_qpolynomial_fold *fold2)
1068 int i;
1070 if (!fold1 || !fold2)
1071 return -1;
1073 if (fold1->n != fold2->n)
1074 return 0;
1076 /* We probably want to sort the qps first... */
1077 for (i = 0; i < fold1->n; ++i) {
1078 int eq = isl_qpolynomial_plain_is_equal(fold1->qp[i], fold2->qp[i]);
1079 if (eq < 0 || !eq)
1080 return eq;
1083 return 1;
1086 __isl_give isl_val *isl_qpolynomial_fold_eval(
1087 __isl_take isl_qpolynomial_fold *fold, __isl_take isl_point *pnt)
1089 isl_ctx *ctx;
1090 isl_val *v;
1092 if (!fold || !pnt)
1093 goto error;
1094 ctx = isl_point_get_ctx(pnt);
1095 isl_assert(pnt->dim->ctx, isl_space_is_equal(pnt->dim, fold->dim), goto error);
1096 isl_assert(pnt->dim->ctx,
1097 fold->type == isl_fold_max || fold->type == isl_fold_min,
1098 goto error);
1100 if (fold->n == 0)
1101 v = isl_val_zero(ctx);
1102 else {
1103 int i;
1104 v = isl_qpolynomial_eval(isl_qpolynomial_copy(fold->qp[0]),
1105 isl_point_copy(pnt));
1106 for (i = 1; i < fold->n; ++i) {
1107 isl_val *v_i;
1108 v_i = isl_qpolynomial_eval(
1109 isl_qpolynomial_copy(fold->qp[i]),
1110 isl_point_copy(pnt));
1111 if (fold->type == isl_fold_max)
1112 v = isl_val_max(v, v_i);
1113 else
1114 v = isl_val_min(v, v_i);
1117 isl_qpolynomial_fold_free(fold);
1118 isl_point_free(pnt);
1120 return v;
1121 error:
1122 isl_qpolynomial_fold_free(fold);
1123 isl_point_free(pnt);
1124 return NULL;
1127 size_t isl_pw_qpolynomial_fold_size(__isl_keep isl_pw_qpolynomial_fold *pwf)
1129 int i;
1130 size_t n = 0;
1132 for (i = 0; i < pwf->n; ++i)
1133 n += pwf->p[i].fold->n;
1135 return n;
1138 __isl_give isl_val *isl_qpolynomial_fold_opt_on_domain(
1139 __isl_take isl_qpolynomial_fold *fold, __isl_take isl_set *set, int max)
1141 int i;
1142 isl_val *opt;
1144 if (!set || !fold)
1145 goto error;
1147 if (fold->n == 0) {
1148 opt = isl_val_zero(isl_set_get_ctx(set));
1149 isl_set_free(set);
1150 isl_qpolynomial_fold_free(fold);
1151 return opt;
1154 opt = isl_qpolynomial_opt_on_domain(isl_qpolynomial_copy(fold->qp[0]),
1155 isl_set_copy(set), max);
1156 for (i = 1; i < fold->n; ++i) {
1157 isl_val *opt_i;
1158 opt_i = isl_qpolynomial_opt_on_domain(
1159 isl_qpolynomial_copy(fold->qp[i]),
1160 isl_set_copy(set), max);
1161 if (max)
1162 opt = isl_val_max(opt, opt_i);
1163 else
1164 opt = isl_val_min(opt, opt_i);
1167 isl_set_free(set);
1168 isl_qpolynomial_fold_free(fold);
1170 return opt;
1171 error:
1172 isl_set_free(set);
1173 isl_qpolynomial_fold_free(fold);
1174 return NULL;
1177 /* Check whether for each quasi-polynomial in "fold2" there is
1178 * a quasi-polynomial in "fold1" that dominates it on "set".
1180 static int qpolynomial_fold_covers_on_domain(__isl_keep isl_set *set,
1181 __isl_keep isl_qpolynomial_fold *fold1,
1182 __isl_keep isl_qpolynomial_fold *fold2)
1184 int i, j;
1185 int covers;
1187 if (!set || !fold1 || !fold2)
1188 return -1;
1190 covers = fold1->type == isl_fold_max ? 1 : -1;
1192 for (i = 0; i < fold2->n; ++i) {
1193 for (j = 0; j < fold1->n; ++j) {
1194 isl_qpolynomial *d;
1195 int sgn;
1197 d = isl_qpolynomial_sub(
1198 isl_qpolynomial_copy(fold1->qp[j]),
1199 isl_qpolynomial_copy(fold2->qp[i]));
1200 sgn = isl_qpolynomial_sign(set, d);
1201 isl_qpolynomial_free(d);
1202 if (sgn == covers)
1203 break;
1205 if (j >= fold1->n)
1206 return 0;
1209 return 1;
1212 /* Check whether "pwf1" dominated "pwf2", i.e., the domain of "pwf1" contains
1213 * that of "pwf2" and on each cell, the corresponding fold from pwf1 dominates
1214 * that of pwf2.
1216 int isl_pw_qpolynomial_fold_covers(__isl_keep isl_pw_qpolynomial_fold *pwf1,
1217 __isl_keep isl_pw_qpolynomial_fold *pwf2)
1219 int i, j;
1220 isl_set *dom1, *dom2;
1221 int is_subset;
1223 if (!pwf1 || !pwf2)
1224 return -1;
1226 if (pwf2->n == 0)
1227 return 1;
1228 if (pwf1->n == 0)
1229 return 0;
1231 dom1 = isl_pw_qpolynomial_fold_domain(isl_pw_qpolynomial_fold_copy(pwf1));
1232 dom2 = isl_pw_qpolynomial_fold_domain(isl_pw_qpolynomial_fold_copy(pwf2));
1233 is_subset = isl_set_is_subset(dom2, dom1);
1234 isl_set_free(dom1);
1235 isl_set_free(dom2);
1237 if (is_subset < 0 || !is_subset)
1238 return is_subset;
1240 for (i = 0; i < pwf2->n; ++i) {
1241 for (j = 0; j < pwf1->n; ++j) {
1242 int is_empty;
1243 isl_set *common;
1244 int covers;
1246 common = isl_set_intersect(isl_set_copy(pwf1->p[j].set),
1247 isl_set_copy(pwf2->p[i].set));
1248 is_empty = isl_set_is_empty(common);
1249 if (is_empty < 0 || is_empty) {
1250 isl_set_free(common);
1251 if (is_empty < 0)
1252 return -1;
1253 continue;
1255 covers = qpolynomial_fold_covers_on_domain(common,
1256 pwf1->p[j].fold, pwf2->p[i].fold);
1257 isl_set_free(common);
1258 if (covers < 0 || !covers)
1259 return covers;
1263 return 1;
1266 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_morph_domain(
1267 __isl_take isl_qpolynomial_fold *fold, __isl_take isl_morph *morph)
1269 int i;
1270 isl_ctx *ctx;
1272 if (!fold || !morph)
1273 goto error;
1275 ctx = fold->dim->ctx;
1276 isl_assert(ctx, isl_space_is_equal(fold->dim, morph->dom->dim), goto error);
1278 fold = isl_qpolynomial_fold_cow(fold);
1279 if (!fold)
1280 goto error;
1282 isl_space_free(fold->dim);
1283 fold->dim = isl_space_copy(morph->ran->dim);
1284 if (!fold->dim)
1285 goto error;
1287 for (i = 0; i < fold->n; ++i) {
1288 fold->qp[i] = isl_qpolynomial_morph_domain(fold->qp[i],
1289 isl_morph_copy(morph));
1290 if (!fold->qp[i])
1291 goto error;
1294 isl_morph_free(morph);
1296 return fold;
1297 error:
1298 isl_qpolynomial_fold_free(fold);
1299 isl_morph_free(morph);
1300 return NULL;
1303 enum isl_fold isl_qpolynomial_fold_get_type(__isl_keep isl_qpolynomial_fold *fold)
1305 if (!fold)
1306 return isl_fold_list;
1307 return fold->type;
1310 enum isl_fold isl_union_pw_qpolynomial_fold_get_type(
1311 __isl_keep isl_union_pw_qpolynomial_fold *upwf)
1313 if (!upwf)
1314 return isl_fold_list;
1315 return upwf->type;
1318 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_lift(
1319 __isl_take isl_qpolynomial_fold *fold, __isl_take isl_space *dim)
1321 int i;
1323 if (!fold || !dim)
1324 goto error;
1326 if (isl_space_is_equal(fold->dim, dim)) {
1327 isl_space_free(dim);
1328 return fold;
1331 fold = isl_qpolynomial_fold_cow(fold);
1332 if (!fold)
1333 goto error;
1335 isl_space_free(fold->dim);
1336 fold->dim = isl_space_copy(dim);
1337 if (!fold->dim)
1338 goto error;
1340 for (i = 0; i < fold->n; ++i) {
1341 fold->qp[i] = isl_qpolynomial_lift(fold->qp[i],
1342 isl_space_copy(dim));
1343 if (!fold->qp[i])
1344 goto error;
1347 isl_space_free(dim);
1349 return fold;
1350 error:
1351 isl_qpolynomial_fold_free(fold);
1352 isl_space_free(dim);
1353 return NULL;
1356 isl_stat isl_qpolynomial_fold_foreach_qpolynomial(
1357 __isl_keep isl_qpolynomial_fold *fold,
1358 isl_stat (*fn)(__isl_take isl_qpolynomial *qp, void *user), void *user)
1360 int i;
1362 if (!fold)
1363 return isl_stat_error;
1365 for (i = 0; i < fold->n; ++i)
1366 if (fn(isl_qpolynomial_copy(fold->qp[i]), user) < 0)
1367 return isl_stat_error;
1369 return isl_stat_ok;
1372 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_move_dims(
1373 __isl_take isl_qpolynomial_fold *fold,
1374 enum isl_dim_type dst_type, unsigned dst_pos,
1375 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
1377 int i;
1378 enum isl_dim_type set_src_type, set_dst_type;
1380 if (n == 0)
1381 return fold;
1383 fold = isl_qpolynomial_fold_cow(fold);
1384 if (!fold)
1385 return NULL;
1387 set_src_type = domain_type(src_type);
1388 set_dst_type = domain_type(dst_type);
1390 fold->dim = isl_space_move_dims(fold->dim, set_dst_type, dst_pos,
1391 set_src_type, src_pos, n);
1392 if (!fold->dim)
1393 goto error;
1395 for (i = 0; i < fold->n; ++i) {
1396 fold->qp[i] = isl_qpolynomial_move_dims(fold->qp[i],
1397 dst_type, dst_pos, src_type, src_pos, n);
1398 if (!fold->qp[i])
1399 goto error;
1402 return fold;
1403 error:
1404 isl_qpolynomial_fold_free(fold);
1405 return NULL;
1408 /* For each 0 <= i < "n", replace variable "first" + i of type "type"
1409 * in fold->qp[k] by subs[i].
1411 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_substitute(
1412 __isl_take isl_qpolynomial_fold *fold,
1413 enum isl_dim_type type, unsigned first, unsigned n,
1414 __isl_keep isl_qpolynomial **subs)
1416 int i;
1418 if (n == 0)
1419 return fold;
1421 fold = isl_qpolynomial_fold_cow(fold);
1422 if (!fold)
1423 return NULL;
1425 for (i = 0; i < fold->n; ++i) {
1426 fold->qp[i] = isl_qpolynomial_substitute(fold->qp[i],
1427 type, first, n, subs);
1428 if (!fold->qp[i])
1429 goto error;
1432 return fold;
1433 error:
1434 isl_qpolynomial_fold_free(fold);
1435 return NULL;
1438 static isl_stat add_pwqp(__isl_take isl_pw_qpolynomial *pwqp, void *user)
1440 isl_pw_qpolynomial_fold *pwf;
1441 isl_union_pw_qpolynomial_fold **upwf;
1442 struct isl_hash_table_entry *entry;
1444 upwf = (isl_union_pw_qpolynomial_fold **)user;
1446 entry = isl_union_pw_qpolynomial_fold_find_part_entry(*upwf,
1447 pwqp->dim, 1);
1448 if (!entry)
1449 goto error;
1451 pwf = isl_pw_qpolynomial_fold_from_pw_qpolynomial((*upwf)->type, pwqp);
1452 if (!entry->data)
1453 entry->data = pwf;
1454 else {
1455 entry->data = isl_pw_qpolynomial_fold_add(entry->data, pwf);
1456 if (!entry->data)
1457 return isl_stat_error;
1458 if (isl_pw_qpolynomial_fold_is_zero(entry->data))
1459 *upwf = isl_union_pw_qpolynomial_fold_remove_part_entry(
1460 *upwf, entry);
1463 return isl_stat_ok;
1464 error:
1465 isl_pw_qpolynomial_free(pwqp);
1466 return isl_stat_error;
1469 __isl_give isl_union_pw_qpolynomial_fold *isl_union_pw_qpolynomial_fold_add_union_pw_qpolynomial(
1470 __isl_take isl_union_pw_qpolynomial_fold *upwf,
1471 __isl_take isl_union_pw_qpolynomial *upwqp)
1473 upwf = isl_union_pw_qpolynomial_fold_align_params(upwf,
1474 isl_union_pw_qpolynomial_get_space(upwqp));
1475 upwqp = isl_union_pw_qpolynomial_align_params(upwqp,
1476 isl_union_pw_qpolynomial_fold_get_space(upwf));
1478 upwf = isl_union_pw_qpolynomial_fold_cow(upwf);
1479 if (!upwf || !upwqp)
1480 goto error;
1482 if (isl_union_pw_qpolynomial_foreach_pw_qpolynomial(upwqp, &add_pwqp,
1483 &upwf) < 0)
1484 goto error;
1486 isl_union_pw_qpolynomial_free(upwqp);
1488 return upwf;
1489 error:
1490 isl_union_pw_qpolynomial_fold_free(upwf);
1491 isl_union_pw_qpolynomial_free(upwqp);
1492 return NULL;
1495 static isl_bool join_compatible(__isl_keep isl_space *space1,
1496 __isl_keep isl_space *space2)
1498 isl_bool m;
1499 m = isl_space_has_equal_params(space1, space2);
1500 if (m < 0 || !m)
1501 return m;
1502 return isl_space_tuple_is_equal(space1, isl_dim_out,
1503 space2, isl_dim_in);
1506 /* Compute the intersection of the range of the map and the domain
1507 * of the piecewise quasipolynomial reduction and then compute a bound
1508 * on the associated quasipolynomial reduction over all elements
1509 * in this intersection.
1511 * We first introduce some unconstrained dimensions in the
1512 * piecewise quasipolynomial, intersect the resulting domain
1513 * with the wrapped map and the compute the sum.
1515 __isl_give isl_pw_qpolynomial_fold *isl_map_apply_pw_qpolynomial_fold(
1516 __isl_take isl_map *map, __isl_take isl_pw_qpolynomial_fold *pwf,
1517 int *tight)
1519 isl_ctx *ctx;
1520 isl_set *dom;
1521 isl_space *map_dim;
1522 isl_space *pwf_dim;
1523 unsigned n_in;
1524 isl_bool ok;
1526 ctx = isl_map_get_ctx(map);
1527 if (!ctx)
1528 goto error;
1530 map_dim = isl_map_get_space(map);
1531 pwf_dim = isl_pw_qpolynomial_fold_get_space(pwf);
1532 ok = join_compatible(map_dim, pwf_dim);
1533 isl_space_free(map_dim);
1534 isl_space_free(pwf_dim);
1535 if (ok < 0)
1536 goto error;
1537 if (!ok)
1538 isl_die(ctx, isl_error_invalid, "incompatible dimensions",
1539 goto error);
1541 n_in = isl_map_dim(map, isl_dim_in);
1542 pwf = isl_pw_qpolynomial_fold_insert_dims(pwf, isl_dim_in, 0, n_in);
1544 dom = isl_map_wrap(map);
1545 pwf = isl_pw_qpolynomial_fold_reset_domain_space(pwf,
1546 isl_set_get_space(dom));
1548 pwf = isl_pw_qpolynomial_fold_intersect_domain(pwf, dom);
1549 pwf = isl_pw_qpolynomial_fold_bound(pwf, tight);
1551 return pwf;
1552 error:
1553 isl_map_free(map);
1554 isl_pw_qpolynomial_fold_free(pwf);
1555 return NULL;
1558 __isl_give isl_pw_qpolynomial_fold *isl_set_apply_pw_qpolynomial_fold(
1559 __isl_take isl_set *set, __isl_take isl_pw_qpolynomial_fold *pwf,
1560 int *tight)
1562 return isl_map_apply_pw_qpolynomial_fold(set, pwf, tight);
1565 struct isl_apply_fold_data {
1566 isl_union_pw_qpolynomial_fold *upwf;
1567 isl_union_pw_qpolynomial_fold *res;
1568 isl_map *map;
1569 int tight;
1572 static isl_stat pw_qpolynomial_fold_apply(
1573 __isl_take isl_pw_qpolynomial_fold *pwf, void *user)
1575 isl_space *map_dim;
1576 isl_space *pwf_dim;
1577 struct isl_apply_fold_data *data = user;
1578 isl_bool ok;
1580 map_dim = isl_map_get_space(data->map);
1581 pwf_dim = isl_pw_qpolynomial_fold_get_space(pwf);
1582 ok = join_compatible(map_dim, pwf_dim);
1583 isl_space_free(map_dim);
1584 isl_space_free(pwf_dim);
1586 if (ok < 0)
1587 return isl_stat_error;
1588 if (ok) {
1589 pwf = isl_map_apply_pw_qpolynomial_fold(isl_map_copy(data->map),
1590 pwf, data->tight ? &data->tight : NULL);
1591 data->res = isl_union_pw_qpolynomial_fold_fold_pw_qpolynomial_fold(
1592 data->res, pwf);
1593 } else
1594 isl_pw_qpolynomial_fold_free(pwf);
1596 return isl_stat_ok;
1599 static isl_stat map_apply(__isl_take isl_map *map, void *user)
1601 struct isl_apply_fold_data *data = user;
1602 isl_stat r;
1604 data->map = map;
1605 r = isl_union_pw_qpolynomial_fold_foreach_pw_qpolynomial_fold(
1606 data->upwf, &pw_qpolynomial_fold_apply, data);
1608 isl_map_free(map);
1609 return r;
1612 __isl_give isl_union_pw_qpolynomial_fold *isl_union_map_apply_union_pw_qpolynomial_fold(
1613 __isl_take isl_union_map *umap,
1614 __isl_take isl_union_pw_qpolynomial_fold *upwf, int *tight)
1616 isl_space *dim;
1617 enum isl_fold type;
1618 struct isl_apply_fold_data data;
1620 upwf = isl_union_pw_qpolynomial_fold_align_params(upwf,
1621 isl_union_map_get_space(umap));
1622 umap = isl_union_map_align_params(umap,
1623 isl_union_pw_qpolynomial_fold_get_space(upwf));
1625 data.upwf = upwf;
1626 data.tight = tight ? 1 : 0;
1627 dim = isl_union_pw_qpolynomial_fold_get_space(upwf);
1628 type = isl_union_pw_qpolynomial_fold_get_type(upwf);
1629 data.res = isl_union_pw_qpolynomial_fold_zero(dim, type);
1630 if (isl_union_map_foreach_map(umap, &map_apply, &data) < 0)
1631 goto error;
1633 isl_union_map_free(umap);
1634 isl_union_pw_qpolynomial_fold_free(upwf);
1636 if (tight)
1637 *tight = data.tight;
1639 return data.res;
1640 error:
1641 isl_union_map_free(umap);
1642 isl_union_pw_qpolynomial_fold_free(upwf);
1643 isl_union_pw_qpolynomial_fold_free(data.res);
1644 return NULL;
1647 __isl_give isl_union_pw_qpolynomial_fold *isl_union_set_apply_union_pw_qpolynomial_fold(
1648 __isl_take isl_union_set *uset,
1649 __isl_take isl_union_pw_qpolynomial_fold *upwf, int *tight)
1651 return isl_union_map_apply_union_pw_qpolynomial_fold(uset, upwf, tight);
1654 /* Reorder the dimension of "fold" according to the given reordering.
1656 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_realign_domain(
1657 __isl_take isl_qpolynomial_fold *fold, __isl_take isl_reordering *r)
1659 int i;
1660 isl_space *space;
1662 fold = isl_qpolynomial_fold_cow(fold);
1663 if (!fold || !r)
1664 goto error;
1666 for (i = 0; i < fold->n; ++i) {
1667 fold->qp[i] = isl_qpolynomial_realign_domain(fold->qp[i],
1668 isl_reordering_copy(r));
1669 if (!fold->qp[i])
1670 goto error;
1673 space = isl_reordering_get_space(r);
1674 fold = isl_qpolynomial_fold_reset_domain_space(fold, space);
1676 isl_reordering_free(r);
1678 return fold;
1679 error:
1680 isl_qpolynomial_fold_free(fold);
1681 isl_reordering_free(r);
1682 return NULL;
1685 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_mul_isl_int(
1686 __isl_take isl_qpolynomial_fold *fold, isl_int v)
1688 int i;
1690 if (isl_int_is_one(v))
1691 return fold;
1692 if (fold && isl_int_is_zero(v)) {
1693 isl_qpolynomial_fold *zero;
1694 isl_space *dim = isl_space_copy(fold->dim);
1695 zero = isl_qpolynomial_fold_empty(fold->type, dim);
1696 isl_qpolynomial_fold_free(fold);
1697 return zero;
1700 fold = isl_qpolynomial_fold_cow(fold);
1701 if (!fold)
1702 return NULL;
1704 if (isl_int_is_neg(v))
1705 fold->type = isl_fold_type_negate(fold->type);
1706 for (i = 0; i < fold->n; ++i) {
1707 fold->qp[i] = isl_qpolynomial_mul_isl_int(fold->qp[i], v);
1708 if (!fold->qp[i])
1709 goto error;
1712 return fold;
1713 error:
1714 isl_qpolynomial_fold_free(fold);
1715 return NULL;
1718 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_scale(
1719 __isl_take isl_qpolynomial_fold *fold, isl_int v)
1721 return isl_qpolynomial_fold_mul_isl_int(fold, v);
1724 /* Multiply "fold" by "v".
1726 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_scale_val(
1727 __isl_take isl_qpolynomial_fold *fold, __isl_take isl_val *v)
1729 int i;
1731 if (!fold || !v)
1732 goto error;
1734 if (isl_val_is_one(v)) {
1735 isl_val_free(v);
1736 return fold;
1738 if (isl_val_is_zero(v)) {
1739 isl_qpolynomial_fold *zero;
1740 isl_space *space = isl_qpolynomial_fold_get_domain_space(fold);
1741 zero = isl_qpolynomial_fold_empty(fold->type, space);
1742 isl_qpolynomial_fold_free(fold);
1743 isl_val_free(v);
1744 return zero;
1746 if (!isl_val_is_rat(v))
1747 isl_die(isl_qpolynomial_fold_get_ctx(fold), isl_error_invalid,
1748 "expecting rational factor", goto error);
1750 fold = isl_qpolynomial_fold_cow(fold);
1751 if (!fold)
1752 goto error;
1754 if (isl_val_is_neg(v))
1755 fold->type = isl_fold_type_negate(fold->type);
1756 for (i = 0; i < fold->n; ++i) {
1757 fold->qp[i] = isl_qpolynomial_scale_val(fold->qp[i],
1758 isl_val_copy(v));
1759 if (!fold->qp[i])
1760 goto error;
1763 isl_val_free(v);
1764 return fold;
1765 error:
1766 isl_val_free(v);
1767 isl_qpolynomial_fold_free(fold);
1768 return NULL;
1771 /* Divide "fold" by "v".
1773 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_scale_down_val(
1774 __isl_take isl_qpolynomial_fold *fold, __isl_take isl_val *v)
1776 if (!fold || !v)
1777 goto error;
1779 if (isl_val_is_one(v)) {
1780 isl_val_free(v);
1781 return fold;
1783 if (!isl_val_is_rat(v))
1784 isl_die(isl_qpolynomial_fold_get_ctx(fold), isl_error_invalid,
1785 "expecting rational factor", goto error);
1786 if (isl_val_is_zero(v))
1787 isl_die(isl_val_get_ctx(v), isl_error_invalid,
1788 "cannot scale down by zero", goto error);
1790 return isl_qpolynomial_fold_scale_val(fold, isl_val_inv(v));
1791 error:
1792 isl_val_free(v);
1793 isl_qpolynomial_fold_free(fold);
1794 return NULL;