isl_basic_set_dims_get_sign: return isl_stat
[isl.git] / isl_fold.c
blob27c92a6c8a4ceb3e0cb99db9184201b748c8a56b
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 isl_qpolynomial_fold_involves_nan isl_qpolynomial_fold_is_nan
657 #define HAS_TYPE
659 #undef PW
660 #define PW isl_pw_qpolynomial_fold
661 #undef EL
662 #define EL isl_qpolynomial_fold
663 #undef EL_IS_ZERO
664 #define EL_IS_ZERO is_empty
665 #undef ZERO
666 #define ZERO zero
667 #undef IS_ZERO
668 #define IS_ZERO is_zero
669 #undef FIELD
670 #define FIELD fold
671 #undef DEFAULT_IS_ZERO
672 #define DEFAULT_IS_ZERO 1
674 #define NO_NEG
675 #define NO_SUB
676 #define NO_PULLBACK
678 #include <isl_pw_templ.c>
680 #undef UNION
681 #define UNION isl_union_pw_qpolynomial_fold
682 #undef PART
683 #define PART isl_pw_qpolynomial_fold
684 #undef PARTS
685 #define PARTS pw_qpolynomial_fold
687 #define NO_SUB
689 #include <isl_union_single.c>
690 #include <isl_union_eval.c>
692 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_empty(enum isl_fold type,
693 __isl_take isl_space *dim)
695 return qpolynomial_fold_alloc(type, dim, 0);
698 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_alloc(
699 enum isl_fold type, __isl_take isl_qpolynomial *qp)
701 isl_qpolynomial_fold *fold;
703 if (!qp)
704 return NULL;
706 fold = qpolynomial_fold_alloc(type, isl_space_copy(qp->dim), 1);
707 if (!fold)
708 goto error;
710 fold->qp[0] = qp;
711 fold->n++;
713 return fold;
714 error:
715 isl_qpolynomial_fold_free(fold);
716 isl_qpolynomial_free(qp);
717 return NULL;
720 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_copy(
721 __isl_keep isl_qpolynomial_fold *fold)
723 if (!fold)
724 return NULL;
726 fold->ref++;
727 return fold;
730 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_dup(
731 __isl_keep isl_qpolynomial_fold *fold)
733 int i;
734 isl_qpolynomial_fold *dup;
736 if (!fold)
737 return NULL;
738 dup = qpolynomial_fold_alloc(fold->type,
739 isl_space_copy(fold->dim), fold->n);
740 if (!dup)
741 return NULL;
743 dup->n = fold->n;
744 for (i = 0; i < fold->n; ++i) {
745 dup->qp[i] = isl_qpolynomial_copy(fold->qp[i]);
746 if (!dup->qp[i])
747 goto error;
750 return dup;
751 error:
752 isl_qpolynomial_fold_free(dup);
753 return NULL;
756 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_cow(
757 __isl_take isl_qpolynomial_fold *fold)
759 if (!fold)
760 return NULL;
762 if (fold->ref == 1)
763 return fold;
764 fold->ref--;
765 return isl_qpolynomial_fold_dup(fold);
768 void isl_qpolynomial_fold_free(__isl_take isl_qpolynomial_fold *fold)
770 int i;
772 if (!fold)
773 return;
774 if (--fold->ref > 0)
775 return;
777 for (i = 0; i < fold->n; ++i)
778 isl_qpolynomial_free(fold->qp[i]);
779 isl_space_free(fold->dim);
780 free(fold);
783 int isl_qpolynomial_fold_is_empty(__isl_keep isl_qpolynomial_fold *fold)
785 if (!fold)
786 return -1;
788 return fold->n == 0;
791 /* Does "fold" represent max(NaN) or min(NaN)?
793 isl_bool isl_qpolynomial_fold_is_nan(__isl_keep isl_qpolynomial_fold *fold)
795 if (!fold)
796 return isl_bool_error;
797 if (fold->n != 1)
798 return isl_bool_false;
799 return isl_qpolynomial_is_nan(fold->qp[0]);
802 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_fold(
803 __isl_take isl_qpolynomial_fold *fold1,
804 __isl_take isl_qpolynomial_fold *fold2)
806 int i;
807 struct isl_qpolynomial_fold *res = NULL;
809 if (!fold1 || !fold2)
810 goto error;
812 isl_assert(fold1->dim->ctx, fold1->type == fold2->type, goto error);
813 isl_assert(fold1->dim->ctx, isl_space_is_equal(fold1->dim, fold2->dim),
814 goto error);
816 if (isl_qpolynomial_fold_is_empty(fold1)) {
817 isl_qpolynomial_fold_free(fold1);
818 return fold2;
821 if (isl_qpolynomial_fold_is_empty(fold2)) {
822 isl_qpolynomial_fold_free(fold2);
823 return fold1;
826 res = qpolynomial_fold_alloc(fold1->type, isl_space_copy(fold1->dim),
827 fold1->n + fold2->n);
828 if (!res)
829 goto error;
831 for (i = 0; i < fold1->n; ++i) {
832 res->qp[res->n] = isl_qpolynomial_copy(fold1->qp[i]);
833 if (!res->qp[res->n])
834 goto error;
835 res->n++;
838 for (i = 0; i < fold2->n; ++i) {
839 res->qp[res->n] = isl_qpolynomial_copy(fold2->qp[i]);
840 if (!res->qp[res->n])
841 goto error;
842 res->n++;
845 isl_qpolynomial_fold_free(fold1);
846 isl_qpolynomial_fold_free(fold2);
848 return res;
849 error:
850 isl_qpolynomial_fold_free(res);
851 isl_qpolynomial_fold_free(fold1);
852 isl_qpolynomial_fold_free(fold2);
853 return NULL;
856 __isl_give isl_pw_qpolynomial_fold *isl_pw_qpolynomial_fold_fold(
857 __isl_take isl_pw_qpolynomial_fold *pw1,
858 __isl_take isl_pw_qpolynomial_fold *pw2)
860 int i, j, n;
861 struct isl_pw_qpolynomial_fold *res;
862 isl_set *set;
864 if (!pw1 || !pw2)
865 goto error;
867 isl_assert(pw1->dim->ctx, isl_space_is_equal(pw1->dim, pw2->dim), goto error);
869 if (isl_pw_qpolynomial_fold_is_zero(pw1)) {
870 isl_pw_qpolynomial_fold_free(pw1);
871 return pw2;
874 if (isl_pw_qpolynomial_fold_is_zero(pw2)) {
875 isl_pw_qpolynomial_fold_free(pw2);
876 return pw1;
879 if (pw1->type != pw2->type)
880 isl_die(pw1->dim->ctx, isl_error_invalid,
881 "fold types don't match", goto error);
883 n = (pw1->n + 1) * (pw2->n + 1);
884 res = isl_pw_qpolynomial_fold_alloc_size(isl_space_copy(pw1->dim),
885 pw1->type, n);
887 for (i = 0; i < pw1->n; ++i) {
888 set = isl_set_copy(pw1->p[i].set);
889 for (j = 0; j < pw2->n; ++j) {
890 struct isl_set *common;
891 isl_qpolynomial_fold *sum;
892 set = isl_set_subtract(set,
893 isl_set_copy(pw2->p[j].set));
894 common = isl_set_intersect(isl_set_copy(pw1->p[i].set),
895 isl_set_copy(pw2->p[j].set));
896 if (isl_set_plain_is_empty(common)) {
897 isl_set_free(common);
898 continue;
901 sum = isl_qpolynomial_fold_fold_on_domain(common,
902 isl_qpolynomial_fold_copy(pw1->p[i].fold),
903 isl_qpolynomial_fold_copy(pw2->p[j].fold));
905 res = isl_pw_qpolynomial_fold_add_piece(res, common, sum);
907 res = isl_pw_qpolynomial_fold_add_piece(res, set,
908 isl_qpolynomial_fold_copy(pw1->p[i].fold));
911 for (j = 0; j < pw2->n; ++j) {
912 set = isl_set_copy(pw2->p[j].set);
913 for (i = 0; i < pw1->n; ++i)
914 set = isl_set_subtract(set, isl_set_copy(pw1->p[i].set));
915 res = isl_pw_qpolynomial_fold_add_piece(res, set,
916 isl_qpolynomial_fold_copy(pw2->p[j].fold));
919 isl_pw_qpolynomial_fold_free(pw1);
920 isl_pw_qpolynomial_fold_free(pw2);
922 return res;
923 error:
924 isl_pw_qpolynomial_fold_free(pw1);
925 isl_pw_qpolynomial_fold_free(pw2);
926 return NULL;
929 __isl_give isl_union_pw_qpolynomial_fold *isl_union_pw_qpolynomial_fold_fold_pw_qpolynomial_fold(
930 __isl_take isl_union_pw_qpolynomial_fold *u,
931 __isl_take isl_pw_qpolynomial_fold *part)
933 struct isl_hash_table_entry *entry;
935 u = isl_union_pw_qpolynomial_fold_cow(u);
937 if (!part || !u)
938 goto error;
940 isl_assert(u->space->ctx,
941 isl_space_match(part->dim, isl_dim_param, u->space, isl_dim_param),
942 goto error);
944 entry = isl_union_pw_qpolynomial_fold_find_part_entry(u, part->dim, 1);
945 if (!entry)
946 goto error;
948 if (!entry->data)
949 entry->data = part;
950 else {
951 entry->data = isl_pw_qpolynomial_fold_fold(entry->data,
952 isl_pw_qpolynomial_fold_copy(part));
953 if (!entry->data)
954 goto error;
955 isl_pw_qpolynomial_fold_free(part);
958 return u;
959 error:
960 isl_pw_qpolynomial_fold_free(part);
961 isl_union_pw_qpolynomial_fold_free(u);
962 return NULL;
965 static isl_stat fold_part(__isl_take isl_pw_qpolynomial_fold *part, void *user)
967 isl_union_pw_qpolynomial_fold **u;
968 u = (isl_union_pw_qpolynomial_fold **)user;
970 *u = isl_union_pw_qpolynomial_fold_fold_pw_qpolynomial_fold(*u, part);
972 return isl_stat_ok;
975 __isl_give isl_union_pw_qpolynomial_fold *isl_union_pw_qpolynomial_fold_fold(
976 __isl_take isl_union_pw_qpolynomial_fold *u1,
977 __isl_take isl_union_pw_qpolynomial_fold *u2)
979 u1 = isl_union_pw_qpolynomial_fold_cow(u1);
981 if (!u1 || !u2)
982 goto error;
984 if (isl_union_pw_qpolynomial_fold_foreach_pw_qpolynomial_fold(u2,
985 &fold_part, &u1) < 0)
986 goto error;
988 isl_union_pw_qpolynomial_fold_free(u2);
990 return u1;
991 error:
992 isl_union_pw_qpolynomial_fold_free(u1);
993 isl_union_pw_qpolynomial_fold_free(u2);
994 return NULL;
997 __isl_give isl_pw_qpolynomial_fold *isl_pw_qpolynomial_fold_from_pw_qpolynomial(
998 enum isl_fold type, __isl_take isl_pw_qpolynomial *pwqp)
1000 int i;
1001 isl_pw_qpolynomial_fold *pwf;
1003 if (!pwqp)
1004 return NULL;
1006 pwf = isl_pw_qpolynomial_fold_alloc_size(isl_space_copy(pwqp->dim),
1007 type, pwqp->n);
1009 for (i = 0; i < pwqp->n; ++i)
1010 pwf = isl_pw_qpolynomial_fold_add_piece(pwf,
1011 isl_set_copy(pwqp->p[i].set),
1012 isl_qpolynomial_fold_alloc(type,
1013 isl_qpolynomial_copy(pwqp->p[i].qp)));
1015 isl_pw_qpolynomial_free(pwqp);
1017 return pwf;
1020 __isl_give isl_pw_qpolynomial_fold *isl_pw_qpolynomial_fold_add(
1021 __isl_take isl_pw_qpolynomial_fold *pwf1,
1022 __isl_take isl_pw_qpolynomial_fold *pwf2)
1024 return isl_pw_qpolynomial_fold_union_add_(pwf1, pwf2);
1027 /* Compare two quasi-polynomial reductions.
1029 * Return -1 if "fold1" is "smaller" than "fold2", 1 if "fold1" is "greater"
1030 * than "fold2" and 0 if they are equal.
1032 int isl_qpolynomial_fold_plain_cmp(__isl_keep isl_qpolynomial_fold *fold1,
1033 __isl_keep isl_qpolynomial_fold *fold2)
1035 int i;
1037 if (fold1 == fold2)
1038 return 0;
1039 if (!fold1)
1040 return -1;
1041 if (!fold2)
1042 return 1;
1044 if (fold1->n != fold2->n)
1045 return fold1->n - fold2->n;
1047 for (i = 0; i < fold1->n; ++i) {
1048 int cmp;
1050 cmp = isl_qpolynomial_plain_cmp(fold1->qp[i], fold2->qp[i]);
1051 if (cmp != 0)
1052 return cmp;
1055 return 0;
1058 int isl_qpolynomial_fold_plain_is_equal(__isl_keep isl_qpolynomial_fold *fold1,
1059 __isl_keep isl_qpolynomial_fold *fold2)
1061 int i;
1063 if (!fold1 || !fold2)
1064 return -1;
1066 if (fold1->n != fold2->n)
1067 return 0;
1069 /* We probably want to sort the qps first... */
1070 for (i = 0; i < fold1->n; ++i) {
1071 int eq = isl_qpolynomial_plain_is_equal(fold1->qp[i], fold2->qp[i]);
1072 if (eq < 0 || !eq)
1073 return eq;
1076 return 1;
1079 __isl_give isl_val *isl_qpolynomial_fold_eval(
1080 __isl_take isl_qpolynomial_fold *fold, __isl_take isl_point *pnt)
1082 isl_ctx *ctx;
1083 isl_val *v;
1085 if (!fold || !pnt)
1086 goto error;
1087 ctx = isl_point_get_ctx(pnt);
1088 isl_assert(pnt->dim->ctx, isl_space_is_equal(pnt->dim, fold->dim), goto error);
1089 isl_assert(pnt->dim->ctx,
1090 fold->type == isl_fold_max || fold->type == isl_fold_min,
1091 goto error);
1093 if (fold->n == 0)
1094 v = isl_val_zero(ctx);
1095 else {
1096 int i;
1097 v = isl_qpolynomial_eval(isl_qpolynomial_copy(fold->qp[0]),
1098 isl_point_copy(pnt));
1099 for (i = 1; i < fold->n; ++i) {
1100 isl_val *v_i;
1101 v_i = isl_qpolynomial_eval(
1102 isl_qpolynomial_copy(fold->qp[i]),
1103 isl_point_copy(pnt));
1104 if (fold->type == isl_fold_max)
1105 v = isl_val_max(v, v_i);
1106 else
1107 v = isl_val_min(v, v_i);
1110 isl_qpolynomial_fold_free(fold);
1111 isl_point_free(pnt);
1113 return v;
1114 error:
1115 isl_qpolynomial_fold_free(fold);
1116 isl_point_free(pnt);
1117 return NULL;
1120 size_t isl_pw_qpolynomial_fold_size(__isl_keep isl_pw_qpolynomial_fold *pwf)
1122 int i;
1123 size_t n = 0;
1125 for (i = 0; i < pwf->n; ++i)
1126 n += pwf->p[i].fold->n;
1128 return n;
1131 __isl_give isl_val *isl_qpolynomial_fold_opt_on_domain(
1132 __isl_take isl_qpolynomial_fold *fold, __isl_take isl_set *set, int max)
1134 int i;
1135 isl_val *opt;
1137 if (!set || !fold)
1138 goto error;
1140 if (fold->n == 0) {
1141 opt = isl_val_zero(isl_set_get_ctx(set));
1142 isl_set_free(set);
1143 isl_qpolynomial_fold_free(fold);
1144 return opt;
1147 opt = isl_qpolynomial_opt_on_domain(isl_qpolynomial_copy(fold->qp[0]),
1148 isl_set_copy(set), max);
1149 for (i = 1; i < fold->n; ++i) {
1150 isl_val *opt_i;
1151 opt_i = isl_qpolynomial_opt_on_domain(
1152 isl_qpolynomial_copy(fold->qp[i]),
1153 isl_set_copy(set), max);
1154 if (max)
1155 opt = isl_val_max(opt, opt_i);
1156 else
1157 opt = isl_val_min(opt, opt_i);
1160 isl_set_free(set);
1161 isl_qpolynomial_fold_free(fold);
1163 return opt;
1164 error:
1165 isl_set_free(set);
1166 isl_qpolynomial_fold_free(fold);
1167 return NULL;
1170 /* Check whether for each quasi-polynomial in "fold2" there is
1171 * a quasi-polynomial in "fold1" that dominates it on "set".
1173 static int qpolynomial_fold_covers_on_domain(__isl_keep isl_set *set,
1174 __isl_keep isl_qpolynomial_fold *fold1,
1175 __isl_keep isl_qpolynomial_fold *fold2)
1177 int i, j;
1178 int covers;
1180 if (!set || !fold1 || !fold2)
1181 return -1;
1183 covers = fold1->type == isl_fold_max ? 1 : -1;
1185 for (i = 0; i < fold2->n; ++i) {
1186 for (j = 0; j < fold1->n; ++j) {
1187 isl_qpolynomial *d;
1188 int sgn;
1190 d = isl_qpolynomial_sub(
1191 isl_qpolynomial_copy(fold1->qp[j]),
1192 isl_qpolynomial_copy(fold2->qp[i]));
1193 sgn = isl_qpolynomial_sign(set, d);
1194 isl_qpolynomial_free(d);
1195 if (sgn == covers)
1196 break;
1198 if (j >= fold1->n)
1199 return 0;
1202 return 1;
1205 /* Check whether "pwf1" dominated "pwf2", i.e., the domain of "pwf1" contains
1206 * that of "pwf2" and on each cell, the corresponding fold from pwf1 dominates
1207 * that of pwf2.
1209 int isl_pw_qpolynomial_fold_covers(__isl_keep isl_pw_qpolynomial_fold *pwf1,
1210 __isl_keep isl_pw_qpolynomial_fold *pwf2)
1212 int i, j;
1213 isl_set *dom1, *dom2;
1214 int is_subset;
1216 if (!pwf1 || !pwf2)
1217 return -1;
1219 if (pwf2->n == 0)
1220 return 1;
1221 if (pwf1->n == 0)
1222 return 0;
1224 dom1 = isl_pw_qpolynomial_fold_domain(isl_pw_qpolynomial_fold_copy(pwf1));
1225 dom2 = isl_pw_qpolynomial_fold_domain(isl_pw_qpolynomial_fold_copy(pwf2));
1226 is_subset = isl_set_is_subset(dom2, dom1);
1227 isl_set_free(dom1);
1228 isl_set_free(dom2);
1230 if (is_subset < 0 || !is_subset)
1231 return is_subset;
1233 for (i = 0; i < pwf2->n; ++i) {
1234 for (j = 0; j < pwf1->n; ++j) {
1235 int is_empty;
1236 isl_set *common;
1237 int covers;
1239 common = isl_set_intersect(isl_set_copy(pwf1->p[j].set),
1240 isl_set_copy(pwf2->p[i].set));
1241 is_empty = isl_set_is_empty(common);
1242 if (is_empty < 0 || is_empty) {
1243 isl_set_free(common);
1244 if (is_empty < 0)
1245 return -1;
1246 continue;
1248 covers = qpolynomial_fold_covers_on_domain(common,
1249 pwf1->p[j].fold, pwf2->p[i].fold);
1250 isl_set_free(common);
1251 if (covers < 0 || !covers)
1252 return covers;
1256 return 1;
1259 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_morph_domain(
1260 __isl_take isl_qpolynomial_fold *fold, __isl_take isl_morph *morph)
1262 int i;
1263 isl_ctx *ctx;
1265 if (!fold || !morph)
1266 goto error;
1268 ctx = fold->dim->ctx;
1269 isl_assert(ctx, isl_space_is_equal(fold->dim, morph->dom->dim), goto error);
1271 fold = isl_qpolynomial_fold_cow(fold);
1272 if (!fold)
1273 goto error;
1275 isl_space_free(fold->dim);
1276 fold->dim = isl_space_copy(morph->ran->dim);
1277 if (!fold->dim)
1278 goto error;
1280 for (i = 0; i < fold->n; ++i) {
1281 fold->qp[i] = isl_qpolynomial_morph_domain(fold->qp[i],
1282 isl_morph_copy(morph));
1283 if (!fold->qp[i])
1284 goto error;
1287 isl_morph_free(morph);
1289 return fold;
1290 error:
1291 isl_qpolynomial_fold_free(fold);
1292 isl_morph_free(morph);
1293 return NULL;
1296 enum isl_fold isl_qpolynomial_fold_get_type(__isl_keep isl_qpolynomial_fold *fold)
1298 if (!fold)
1299 return isl_fold_list;
1300 return fold->type;
1303 enum isl_fold isl_union_pw_qpolynomial_fold_get_type(
1304 __isl_keep isl_union_pw_qpolynomial_fold *upwf)
1306 if (!upwf)
1307 return isl_fold_list;
1308 return upwf->type;
1311 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_lift(
1312 __isl_take isl_qpolynomial_fold *fold, __isl_take isl_space *dim)
1314 int i;
1316 if (!fold || !dim)
1317 goto error;
1319 if (isl_space_is_equal(fold->dim, dim)) {
1320 isl_space_free(dim);
1321 return fold;
1324 fold = isl_qpolynomial_fold_cow(fold);
1325 if (!fold)
1326 goto error;
1328 isl_space_free(fold->dim);
1329 fold->dim = isl_space_copy(dim);
1330 if (!fold->dim)
1331 goto error;
1333 for (i = 0; i < fold->n; ++i) {
1334 fold->qp[i] = isl_qpolynomial_lift(fold->qp[i],
1335 isl_space_copy(dim));
1336 if (!fold->qp[i])
1337 goto error;
1340 isl_space_free(dim);
1342 return fold;
1343 error:
1344 isl_qpolynomial_fold_free(fold);
1345 isl_space_free(dim);
1346 return NULL;
1349 isl_stat isl_qpolynomial_fold_foreach_qpolynomial(
1350 __isl_keep isl_qpolynomial_fold *fold,
1351 isl_stat (*fn)(__isl_take isl_qpolynomial *qp, void *user), void *user)
1353 int i;
1355 if (!fold)
1356 return isl_stat_error;
1358 for (i = 0; i < fold->n; ++i)
1359 if (fn(isl_qpolynomial_copy(fold->qp[i]), user) < 0)
1360 return isl_stat_error;
1362 return isl_stat_ok;
1365 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_move_dims(
1366 __isl_take isl_qpolynomial_fold *fold,
1367 enum isl_dim_type dst_type, unsigned dst_pos,
1368 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
1370 int i;
1372 if (n == 0)
1373 return fold;
1375 fold = isl_qpolynomial_fold_cow(fold);
1376 if (!fold)
1377 return NULL;
1379 fold->dim = isl_space_move_dims(fold->dim, dst_type, dst_pos,
1380 src_type, src_pos, n);
1381 if (!fold->dim)
1382 goto error;
1384 for (i = 0; i < fold->n; ++i) {
1385 fold->qp[i] = isl_qpolynomial_move_dims(fold->qp[i],
1386 dst_type, dst_pos, src_type, src_pos, n);
1387 if (!fold->qp[i])
1388 goto error;
1391 return fold;
1392 error:
1393 isl_qpolynomial_fold_free(fold);
1394 return NULL;
1397 /* For each 0 <= i < "n", replace variable "first" + i of type "type"
1398 * in fold->qp[k] by subs[i].
1400 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_substitute(
1401 __isl_take isl_qpolynomial_fold *fold,
1402 enum isl_dim_type type, unsigned first, unsigned n,
1403 __isl_keep isl_qpolynomial **subs)
1405 int i;
1407 if (n == 0)
1408 return fold;
1410 fold = isl_qpolynomial_fold_cow(fold);
1411 if (!fold)
1412 return NULL;
1414 for (i = 0; i < fold->n; ++i) {
1415 fold->qp[i] = isl_qpolynomial_substitute(fold->qp[i],
1416 type, first, n, subs);
1417 if (!fold->qp[i])
1418 goto error;
1421 return fold;
1422 error:
1423 isl_qpolynomial_fold_free(fold);
1424 return NULL;
1427 static isl_stat add_pwqp(__isl_take isl_pw_qpolynomial *pwqp, void *user)
1429 isl_ctx *ctx;
1430 isl_pw_qpolynomial_fold *pwf;
1431 isl_union_pw_qpolynomial_fold **upwf;
1432 struct isl_hash_table_entry *entry;
1434 upwf = (isl_union_pw_qpolynomial_fold **)user;
1436 ctx = pwqp->dim->ctx;
1437 entry = isl_union_pw_qpolynomial_fold_find_part_entry(*upwf,
1438 pwqp->dim, 1);
1439 if (!entry)
1440 goto error;
1442 pwf = isl_pw_qpolynomial_fold_from_pw_qpolynomial((*upwf)->type, pwqp);
1443 if (!entry->data)
1444 entry->data = pwf;
1445 else {
1446 entry->data = isl_pw_qpolynomial_fold_add(entry->data, pwf);
1447 if (!entry->data)
1448 return isl_stat_error;
1449 if (isl_pw_qpolynomial_fold_is_zero(entry->data))
1450 *upwf = isl_union_pw_qpolynomial_fold_remove_part_entry(
1451 *upwf, entry);
1454 return isl_stat_ok;
1455 error:
1456 isl_pw_qpolynomial_free(pwqp);
1457 return isl_stat_error;
1460 __isl_give isl_union_pw_qpolynomial_fold *isl_union_pw_qpolynomial_fold_add_union_pw_qpolynomial(
1461 __isl_take isl_union_pw_qpolynomial_fold *upwf,
1462 __isl_take isl_union_pw_qpolynomial *upwqp)
1464 upwf = isl_union_pw_qpolynomial_fold_align_params(upwf,
1465 isl_union_pw_qpolynomial_get_space(upwqp));
1466 upwqp = isl_union_pw_qpolynomial_align_params(upwqp,
1467 isl_union_pw_qpolynomial_fold_get_space(upwf));
1469 upwf = isl_union_pw_qpolynomial_fold_cow(upwf);
1470 if (!upwf || !upwqp)
1471 goto error;
1473 if (isl_union_pw_qpolynomial_foreach_pw_qpolynomial(upwqp, &add_pwqp,
1474 &upwf) < 0)
1475 goto error;
1477 isl_union_pw_qpolynomial_free(upwqp);
1479 return upwf;
1480 error:
1481 isl_union_pw_qpolynomial_fold_free(upwf);
1482 isl_union_pw_qpolynomial_free(upwqp);
1483 return NULL;
1486 static isl_bool join_compatible(__isl_keep isl_space *space1,
1487 __isl_keep isl_space *space2)
1489 isl_bool m;
1490 m = isl_space_match(space1, isl_dim_param, space2, isl_dim_param);
1491 if (m < 0 || !m)
1492 return m;
1493 return isl_space_tuple_is_equal(space1, isl_dim_out,
1494 space2, isl_dim_in);
1497 /* Compute the intersection of the range of the map and the domain
1498 * of the piecewise quasipolynomial reduction and then compute a bound
1499 * on the associated quasipolynomial reduction over all elements
1500 * in this intersection.
1502 * We first introduce some unconstrained dimensions in the
1503 * piecewise quasipolynomial, intersect the resulting domain
1504 * with the wrapped map and the compute the sum.
1506 __isl_give isl_pw_qpolynomial_fold *isl_map_apply_pw_qpolynomial_fold(
1507 __isl_take isl_map *map, __isl_take isl_pw_qpolynomial_fold *pwf,
1508 int *tight)
1510 isl_ctx *ctx;
1511 isl_set *dom;
1512 isl_space *map_dim;
1513 isl_space *pwf_dim;
1514 unsigned n_in;
1515 isl_bool ok;
1517 ctx = isl_map_get_ctx(map);
1518 if (!ctx)
1519 goto error;
1521 map_dim = isl_map_get_space(map);
1522 pwf_dim = isl_pw_qpolynomial_fold_get_space(pwf);
1523 ok = join_compatible(map_dim, pwf_dim);
1524 isl_space_free(map_dim);
1525 isl_space_free(pwf_dim);
1526 if (ok < 0)
1527 goto error;
1528 if (!ok)
1529 isl_die(ctx, isl_error_invalid, "incompatible dimensions",
1530 goto error);
1532 n_in = isl_map_dim(map, isl_dim_in);
1533 pwf = isl_pw_qpolynomial_fold_insert_dims(pwf, isl_dim_in, 0, n_in);
1535 dom = isl_map_wrap(map);
1536 pwf = isl_pw_qpolynomial_fold_reset_domain_space(pwf,
1537 isl_set_get_space(dom));
1539 pwf = isl_pw_qpolynomial_fold_intersect_domain(pwf, dom);
1540 pwf = isl_pw_qpolynomial_fold_bound(pwf, tight);
1542 return pwf;
1543 error:
1544 isl_map_free(map);
1545 isl_pw_qpolynomial_fold_free(pwf);
1546 return NULL;
1549 __isl_give isl_pw_qpolynomial_fold *isl_set_apply_pw_qpolynomial_fold(
1550 __isl_take isl_set *set, __isl_take isl_pw_qpolynomial_fold *pwf,
1551 int *tight)
1553 return isl_map_apply_pw_qpolynomial_fold(set, pwf, tight);
1556 struct isl_apply_fold_data {
1557 isl_union_pw_qpolynomial_fold *upwf;
1558 isl_union_pw_qpolynomial_fold *res;
1559 isl_map *map;
1560 int tight;
1563 static isl_stat pw_qpolynomial_fold_apply(
1564 __isl_take isl_pw_qpolynomial_fold *pwf, void *user)
1566 isl_space *map_dim;
1567 isl_space *pwf_dim;
1568 struct isl_apply_fold_data *data = user;
1569 isl_bool ok;
1571 map_dim = isl_map_get_space(data->map);
1572 pwf_dim = isl_pw_qpolynomial_fold_get_space(pwf);
1573 ok = join_compatible(map_dim, pwf_dim);
1574 isl_space_free(map_dim);
1575 isl_space_free(pwf_dim);
1577 if (ok < 0)
1578 return isl_stat_error;
1579 if (ok) {
1580 pwf = isl_map_apply_pw_qpolynomial_fold(isl_map_copy(data->map),
1581 pwf, data->tight ? &data->tight : NULL);
1582 data->res = isl_union_pw_qpolynomial_fold_fold_pw_qpolynomial_fold(
1583 data->res, pwf);
1584 } else
1585 isl_pw_qpolynomial_fold_free(pwf);
1587 return isl_stat_ok;
1590 static isl_stat map_apply(__isl_take isl_map *map, void *user)
1592 struct isl_apply_fold_data *data = user;
1593 isl_stat r;
1595 data->map = map;
1596 r = isl_union_pw_qpolynomial_fold_foreach_pw_qpolynomial_fold(
1597 data->upwf, &pw_qpolynomial_fold_apply, data);
1599 isl_map_free(map);
1600 return r;
1603 __isl_give isl_union_pw_qpolynomial_fold *isl_union_map_apply_union_pw_qpolynomial_fold(
1604 __isl_take isl_union_map *umap,
1605 __isl_take isl_union_pw_qpolynomial_fold *upwf, int *tight)
1607 isl_space *dim;
1608 enum isl_fold type;
1609 struct isl_apply_fold_data data;
1611 upwf = isl_union_pw_qpolynomial_fold_align_params(upwf,
1612 isl_union_map_get_space(umap));
1613 umap = isl_union_map_align_params(umap,
1614 isl_union_pw_qpolynomial_fold_get_space(upwf));
1616 data.upwf = upwf;
1617 data.tight = tight ? 1 : 0;
1618 dim = isl_union_pw_qpolynomial_fold_get_space(upwf);
1619 type = isl_union_pw_qpolynomial_fold_get_type(upwf);
1620 data.res = isl_union_pw_qpolynomial_fold_zero(dim, type);
1621 if (isl_union_map_foreach_map(umap, &map_apply, &data) < 0)
1622 goto error;
1624 isl_union_map_free(umap);
1625 isl_union_pw_qpolynomial_fold_free(upwf);
1627 if (tight)
1628 *tight = data.tight;
1630 return data.res;
1631 error:
1632 isl_union_map_free(umap);
1633 isl_union_pw_qpolynomial_fold_free(upwf);
1634 isl_union_pw_qpolynomial_fold_free(data.res);
1635 return NULL;
1638 __isl_give isl_union_pw_qpolynomial_fold *isl_union_set_apply_union_pw_qpolynomial_fold(
1639 __isl_take isl_union_set *uset,
1640 __isl_take isl_union_pw_qpolynomial_fold *upwf, int *tight)
1642 return isl_union_map_apply_union_pw_qpolynomial_fold(uset, upwf, tight);
1645 /* Reorder the dimension of "fold" according to the given reordering.
1647 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_realign_domain(
1648 __isl_take isl_qpolynomial_fold *fold, __isl_take isl_reordering *r)
1650 int i;
1652 fold = isl_qpolynomial_fold_cow(fold);
1653 if (!fold || !r)
1654 goto error;
1656 for (i = 0; i < fold->n; ++i) {
1657 fold->qp[i] = isl_qpolynomial_realign_domain(fold->qp[i],
1658 isl_reordering_copy(r));
1659 if (!fold->qp[i])
1660 goto error;
1663 fold = isl_qpolynomial_fold_reset_domain_space(fold,
1664 isl_space_copy(r->dim));
1666 isl_reordering_free(r);
1668 return fold;
1669 error:
1670 isl_qpolynomial_fold_free(fold);
1671 isl_reordering_free(r);
1672 return NULL;
1675 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_mul_isl_int(
1676 __isl_take isl_qpolynomial_fold *fold, isl_int v)
1678 int i;
1680 if (isl_int_is_one(v))
1681 return fold;
1682 if (fold && isl_int_is_zero(v)) {
1683 isl_qpolynomial_fold *zero;
1684 isl_space *dim = isl_space_copy(fold->dim);
1685 zero = isl_qpolynomial_fold_empty(fold->type, dim);
1686 isl_qpolynomial_fold_free(fold);
1687 return zero;
1690 fold = isl_qpolynomial_fold_cow(fold);
1691 if (!fold)
1692 return NULL;
1694 if (isl_int_is_neg(v))
1695 fold->type = isl_fold_type_negate(fold->type);
1696 for (i = 0; i < fold->n; ++i) {
1697 fold->qp[i] = isl_qpolynomial_mul_isl_int(fold->qp[i], v);
1698 if (!fold->qp[i])
1699 goto error;
1702 return fold;
1703 error:
1704 isl_qpolynomial_fold_free(fold);
1705 return NULL;
1708 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_scale(
1709 __isl_take isl_qpolynomial_fold *fold, isl_int v)
1711 return isl_qpolynomial_fold_mul_isl_int(fold, v);
1714 /* Multiply "fold" by "v".
1716 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_scale_val(
1717 __isl_take isl_qpolynomial_fold *fold, __isl_take isl_val *v)
1719 int i;
1721 if (!fold || !v)
1722 goto error;
1724 if (isl_val_is_one(v)) {
1725 isl_val_free(v);
1726 return fold;
1728 if (isl_val_is_zero(v)) {
1729 isl_qpolynomial_fold *zero;
1730 isl_space *space = isl_qpolynomial_fold_get_domain_space(fold);
1731 zero = isl_qpolynomial_fold_empty(fold->type, space);
1732 isl_qpolynomial_fold_free(fold);
1733 isl_val_free(v);
1734 return zero;
1736 if (!isl_val_is_rat(v))
1737 isl_die(isl_qpolynomial_fold_get_ctx(fold), isl_error_invalid,
1738 "expecting rational factor", goto error);
1740 fold = isl_qpolynomial_fold_cow(fold);
1741 if (!fold)
1742 goto error;
1744 if (isl_val_is_neg(v))
1745 fold->type = isl_fold_type_negate(fold->type);
1746 for (i = 0; i < fold->n; ++i) {
1747 fold->qp[i] = isl_qpolynomial_scale_val(fold->qp[i],
1748 isl_val_copy(v));
1749 if (!fold->qp[i])
1750 goto error;
1753 isl_val_free(v);
1754 return fold;
1755 error:
1756 isl_val_free(v);
1757 isl_qpolynomial_fold_free(fold);
1758 return NULL;
1761 /* Divide "fold" by "v".
1763 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_scale_down_val(
1764 __isl_take isl_qpolynomial_fold *fold, __isl_take isl_val *v)
1766 if (!fold || !v)
1767 goto error;
1769 if (isl_val_is_one(v)) {
1770 isl_val_free(v);
1771 return fold;
1773 if (!isl_val_is_rat(v))
1774 isl_die(isl_qpolynomial_fold_get_ctx(fold), isl_error_invalid,
1775 "expecting rational factor", goto error);
1776 if (isl_val_is_zero(v))
1777 isl_die(isl_val_get_ctx(v), isl_error_invalid,
1778 "cannot scale down by zero", goto error);
1780 return isl_qpolynomial_fold_scale_val(fold, isl_val_inv(v));
1781 error:
1782 isl_val_free(v);
1783 isl_qpolynomial_fold_free(fold);
1784 return NULL;