drop *_fast_* functions
[isl.git] / isl_fold.c
blob939f0c7875f3b180d6eb86df7c04c601f355771f
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 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_fold_on_domain(
410 __isl_keep isl_set *set,
411 __isl_take isl_qpolynomial_fold *fold1,
412 __isl_take isl_qpolynomial_fold *fold2)
414 int i, j;
415 int n1;
416 struct isl_qpolynomial_fold *res = NULL;
417 int better;
419 if (!fold1 || !fold2)
420 goto error;
422 isl_assert(fold1->dim->ctx, fold1->type == fold2->type, goto error);
423 isl_assert(fold1->dim->ctx, isl_space_is_equal(fold1->dim, fold2->dim),
424 goto error);
426 better = fold1->type == isl_fold_max ? -1 : 1;
428 if (isl_qpolynomial_fold_is_empty(fold1)) {
429 isl_qpolynomial_fold_free(fold1);
430 return fold2;
433 if (isl_qpolynomial_fold_is_empty(fold2)) {
434 isl_qpolynomial_fold_free(fold2);
435 return fold1;
438 res = qpolynomial_fold_alloc(fold1->type, isl_space_copy(fold1->dim),
439 fold1->n + fold2->n);
440 if (!res)
441 goto error;
443 for (i = 0; i < fold1->n; ++i) {
444 res->qp[res->n] = isl_qpolynomial_copy(fold1->qp[i]);
445 if (!res->qp[res->n])
446 goto error;
447 res->n++;
449 n1 = res->n;
451 for (i = 0; i < fold2->n; ++i) {
452 for (j = n1 - 1; j >= 0; --j) {
453 isl_qpolynomial *d;
454 int sgn, equal;
455 equal = isl_qpolynomial_plain_is_equal(res->qp[j],
456 fold2->qp[i]);
457 if (equal < 0)
458 goto error;
459 if (equal)
460 break;
461 d = isl_qpolynomial_sub(
462 isl_qpolynomial_copy(res->qp[j]),
463 isl_qpolynomial_copy(fold2->qp[i]));
464 sgn = isl_qpolynomial_sign(set, d);
465 isl_qpolynomial_free(d);
466 if (sgn == 0)
467 continue;
468 if (sgn != better)
469 break;
470 isl_qpolynomial_free(res->qp[j]);
471 if (j != n1 - 1)
472 res->qp[j] = res->qp[n1 - 1];
473 n1--;
474 if (n1 != res->n - 1)
475 res->qp[n1] = res->qp[res->n - 1];
476 res->n--;
478 if (j >= 0)
479 continue;
480 res->qp[res->n] = isl_qpolynomial_copy(fold2->qp[i]);
481 if (!res->qp[res->n])
482 goto error;
483 res->n++;
486 isl_qpolynomial_fold_free(fold1);
487 isl_qpolynomial_fold_free(fold2);
489 return res;
490 error:
491 isl_qpolynomial_fold_free(res);
492 isl_qpolynomial_fold_free(fold1);
493 isl_qpolynomial_fold_free(fold2);
494 return NULL;
497 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_add_qpolynomial(
498 __isl_take isl_qpolynomial_fold *fold, __isl_take isl_qpolynomial *qp)
500 int i;
502 if (!fold || !qp)
503 goto error;
505 if (isl_qpolynomial_is_zero(qp)) {
506 isl_qpolynomial_free(qp);
507 return fold;
510 fold = isl_qpolynomial_fold_cow(fold);
511 if (!fold)
512 goto error;
514 for (i = 0; i < fold->n; ++i) {
515 fold->qp[i] = isl_qpolynomial_add(fold->qp[i],
516 isl_qpolynomial_copy(qp));
517 if (!fold->qp[i])
518 goto error;
521 isl_qpolynomial_free(qp);
522 return fold;
523 error:
524 isl_qpolynomial_fold_free(fold);
525 isl_qpolynomial_free(qp);
526 return NULL;
529 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_add_on_domain(
530 __isl_keep isl_set *dom,
531 __isl_take isl_qpolynomial_fold *fold1,
532 __isl_take isl_qpolynomial_fold *fold2)
534 int i;
535 isl_qpolynomial_fold *res = NULL;
537 if (!fold1 || !fold2)
538 goto error;
540 if (isl_qpolynomial_fold_is_empty(fold1)) {
541 isl_qpolynomial_fold_free(fold1);
542 return fold2;
545 if (isl_qpolynomial_fold_is_empty(fold2)) {
546 isl_qpolynomial_fold_free(fold2);
547 return fold1;
550 if (fold1->n == 1 && fold2->n != 1)
551 return isl_qpolynomial_fold_add_on_domain(dom, fold2, fold1);
553 if (fold2->n == 1) {
554 res = isl_qpolynomial_fold_add_qpolynomial(fold1,
555 isl_qpolynomial_copy(fold2->qp[0]));
556 isl_qpolynomial_fold_free(fold2);
557 return res;
560 res = isl_qpolynomial_fold_add_qpolynomial(
561 isl_qpolynomial_fold_copy(fold1),
562 isl_qpolynomial_copy(fold2->qp[0]));
564 for (i = 1; i < fold2->n; ++i) {
565 isl_qpolynomial_fold *res_i;
566 res_i = isl_qpolynomial_fold_add_qpolynomial(
567 isl_qpolynomial_fold_copy(fold1),
568 isl_qpolynomial_copy(fold2->qp[i]));
569 res = isl_qpolynomial_fold_fold_on_domain(dom, res, res_i);
572 isl_qpolynomial_fold_free(fold1);
573 isl_qpolynomial_fold_free(fold2);
574 return res;
575 error:
576 isl_qpolynomial_fold_free(res);
577 isl_qpolynomial_fold_free(fold1);
578 isl_qpolynomial_fold_free(fold2);
579 return NULL;
582 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_substitute_equalities(
583 __isl_take isl_qpolynomial_fold *fold, __isl_take isl_basic_set *eq)
585 int i;
587 if (!fold || !eq)
588 goto error;
590 fold = isl_qpolynomial_fold_cow(fold);
591 if (!fold)
592 return NULL;
594 for (i = 0; i < fold->n; ++i) {
595 fold->qp[i] = isl_qpolynomial_substitute_equalities(fold->qp[i],
596 isl_basic_set_copy(eq));
597 if (!fold->qp[i])
598 goto error;
601 isl_basic_set_free(eq);
602 return fold;
603 error:
604 isl_basic_set_free(eq);
605 isl_qpolynomial_fold_free(fold);
606 return NULL;
609 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_gist(
610 __isl_take isl_qpolynomial_fold *fold, __isl_take isl_set *context)
612 int i;
614 if (!fold || !context)
615 goto error;
617 fold = isl_qpolynomial_fold_cow(fold);
618 if (!fold)
619 return NULL;
621 for (i = 0; i < fold->n; ++i) {
622 fold->qp[i] = isl_qpolynomial_gist(fold->qp[i],
623 isl_set_copy(context));
624 if (!fold->qp[i])
625 goto error;
628 isl_set_free(context);
629 return fold;
630 error:
631 isl_set_free(context);
632 isl_qpolynomial_fold_free(fold);
633 return NULL;
636 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_gist_params(
637 __isl_take isl_qpolynomial_fold *fold, __isl_take isl_set *context)
639 isl_space *space = isl_qpolynomial_fold_get_domain_space(fold);
640 isl_set *dom_context = isl_set_universe(space);
641 dom_context = isl_set_intersect_params(dom_context, context);
642 return isl_qpolynomial_fold_gist(fold, dom_context);
645 #define HAS_TYPE
647 #undef PW
648 #define PW isl_pw_qpolynomial_fold
649 #undef EL
650 #define EL isl_qpolynomial_fold
651 #undef EL_IS_ZERO
652 #define EL_IS_ZERO is_empty
653 #undef ZERO
654 #define ZERO zero
655 #undef IS_ZERO
656 #define IS_ZERO is_zero
657 #undef FIELD
658 #define FIELD fold
659 #undef DEFAULT_IS_ZERO
660 #define DEFAULT_IS_ZERO 1
662 #define NO_NEG
663 #define NO_SUB
664 #define NO_PULLBACK
666 #include <isl_pw_templ.c>
668 #undef UNION
669 #define UNION isl_union_pw_qpolynomial_fold
670 #undef PART
671 #define PART isl_pw_qpolynomial_fold
672 #undef PARTS
673 #define PARTS pw_qpolynomial_fold
675 #define NO_SUB
677 #include <isl_union_templ.c>
679 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_empty(enum isl_fold type,
680 __isl_take isl_space *dim)
682 return qpolynomial_fold_alloc(type, dim, 0);
685 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_alloc(
686 enum isl_fold type, __isl_take isl_qpolynomial *qp)
688 isl_qpolynomial_fold *fold;
690 if (!qp)
691 return NULL;
693 fold = qpolynomial_fold_alloc(type, isl_space_copy(qp->dim), 1);
694 if (!fold)
695 goto error;
697 fold->qp[0] = qp;
698 fold->n++;
700 return fold;
701 error:
702 isl_qpolynomial_fold_free(fold);
703 isl_qpolynomial_free(qp);
704 return NULL;
707 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_copy(
708 __isl_keep isl_qpolynomial_fold *fold)
710 if (!fold)
711 return NULL;
713 fold->ref++;
714 return fold;
717 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_dup(
718 __isl_keep isl_qpolynomial_fold *fold)
720 int i;
721 isl_qpolynomial_fold *dup;
723 if (!fold)
724 return NULL;
725 dup = qpolynomial_fold_alloc(fold->type,
726 isl_space_copy(fold->dim), fold->n);
727 if (!dup)
728 return NULL;
730 dup->n = fold->n;
731 for (i = 0; i < fold->n; ++i) {
732 dup->qp[i] = isl_qpolynomial_copy(fold->qp[i]);
733 if (!dup->qp[i])
734 goto error;
737 return dup;
738 error:
739 isl_qpolynomial_fold_free(dup);
740 return NULL;
743 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_cow(
744 __isl_take isl_qpolynomial_fold *fold)
746 if (!fold)
747 return NULL;
749 if (fold->ref == 1)
750 return fold;
751 fold->ref--;
752 return isl_qpolynomial_fold_dup(fold);
755 void isl_qpolynomial_fold_free(__isl_take isl_qpolynomial_fold *fold)
757 int i;
759 if (!fold)
760 return;
761 if (--fold->ref > 0)
762 return;
764 for (i = 0; i < fold->n; ++i)
765 isl_qpolynomial_free(fold->qp[i]);
766 isl_space_free(fold->dim);
767 free(fold);
770 int isl_qpolynomial_fold_is_empty(__isl_keep isl_qpolynomial_fold *fold)
772 if (!fold)
773 return -1;
775 return fold->n == 0;
778 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_fold(
779 __isl_take isl_qpolynomial_fold *fold1,
780 __isl_take isl_qpolynomial_fold *fold2)
782 int i;
783 struct isl_qpolynomial_fold *res = NULL;
785 if (!fold1 || !fold2)
786 goto error;
788 isl_assert(fold1->dim->ctx, fold1->type == fold2->type, goto error);
789 isl_assert(fold1->dim->ctx, isl_space_is_equal(fold1->dim, fold2->dim),
790 goto error);
792 if (isl_qpolynomial_fold_is_empty(fold1)) {
793 isl_qpolynomial_fold_free(fold1);
794 return fold2;
797 if (isl_qpolynomial_fold_is_empty(fold2)) {
798 isl_qpolynomial_fold_free(fold2);
799 return fold1;
802 res = qpolynomial_fold_alloc(fold1->type, isl_space_copy(fold1->dim),
803 fold1->n + fold2->n);
804 if (!res)
805 goto error;
807 for (i = 0; i < fold1->n; ++i) {
808 res->qp[res->n] = isl_qpolynomial_copy(fold1->qp[i]);
809 if (!res->qp[res->n])
810 goto error;
811 res->n++;
814 for (i = 0; i < fold2->n; ++i) {
815 res->qp[res->n] = isl_qpolynomial_copy(fold2->qp[i]);
816 if (!res->qp[res->n])
817 goto error;
818 res->n++;
821 isl_qpolynomial_fold_free(fold1);
822 isl_qpolynomial_fold_free(fold2);
824 return res;
825 error:
826 isl_qpolynomial_fold_free(res);
827 isl_qpolynomial_fold_free(fold1);
828 isl_qpolynomial_fold_free(fold2);
829 return NULL;
832 __isl_give isl_pw_qpolynomial_fold *isl_pw_qpolynomial_fold_fold(
833 __isl_take isl_pw_qpolynomial_fold *pw1,
834 __isl_take isl_pw_qpolynomial_fold *pw2)
836 int i, j, n;
837 struct isl_pw_qpolynomial_fold *res;
838 isl_set *set;
840 if (!pw1 || !pw2)
841 goto error;
843 isl_assert(pw1->dim->ctx, isl_space_is_equal(pw1->dim, pw2->dim), goto error);
845 if (isl_pw_qpolynomial_fold_is_zero(pw1)) {
846 isl_pw_qpolynomial_fold_free(pw1);
847 return pw2;
850 if (isl_pw_qpolynomial_fold_is_zero(pw2)) {
851 isl_pw_qpolynomial_fold_free(pw2);
852 return pw1;
855 if (pw1->type != pw2->type)
856 isl_die(pw1->dim->ctx, isl_error_invalid,
857 "fold types don't match", goto error);
859 n = (pw1->n + 1) * (pw2->n + 1);
860 res = isl_pw_qpolynomial_fold_alloc_size(isl_space_copy(pw1->dim),
861 pw1->type, n);
863 for (i = 0; i < pw1->n; ++i) {
864 set = isl_set_copy(pw1->p[i].set);
865 for (j = 0; j < pw2->n; ++j) {
866 struct isl_set *common;
867 isl_qpolynomial_fold *sum;
868 set = isl_set_subtract(set,
869 isl_set_copy(pw2->p[j].set));
870 common = isl_set_intersect(isl_set_copy(pw1->p[i].set),
871 isl_set_copy(pw2->p[j].set));
872 if (isl_set_plain_is_empty(common)) {
873 isl_set_free(common);
874 continue;
877 sum = isl_qpolynomial_fold_fold_on_domain(common,
878 isl_qpolynomial_fold_copy(pw1->p[i].fold),
879 isl_qpolynomial_fold_copy(pw2->p[j].fold));
881 res = isl_pw_qpolynomial_fold_add_piece(res, common, sum);
883 res = isl_pw_qpolynomial_fold_add_piece(res, set,
884 isl_qpolynomial_fold_copy(pw1->p[i].fold));
887 for (j = 0; j < pw2->n; ++j) {
888 set = isl_set_copy(pw2->p[j].set);
889 for (i = 0; i < pw1->n; ++i)
890 set = isl_set_subtract(set, isl_set_copy(pw1->p[i].set));
891 res = isl_pw_qpolynomial_fold_add_piece(res, set,
892 isl_qpolynomial_fold_copy(pw2->p[j].fold));
895 isl_pw_qpolynomial_fold_free(pw1);
896 isl_pw_qpolynomial_fold_free(pw2);
898 return res;
899 error:
900 isl_pw_qpolynomial_fold_free(pw1);
901 isl_pw_qpolynomial_fold_free(pw2);
902 return NULL;
905 __isl_give isl_union_pw_qpolynomial_fold *isl_union_pw_qpolynomial_fold_fold_pw_qpolynomial_fold(
906 __isl_take isl_union_pw_qpolynomial_fold *u,
907 __isl_take isl_pw_qpolynomial_fold *part)
909 uint32_t hash;
910 struct isl_hash_table_entry *entry;
912 u = isl_union_pw_qpolynomial_fold_cow(u);
914 if (!part || !u)
915 goto error;
917 isl_assert(u->space->ctx,
918 isl_space_match(part->dim, isl_dim_param, u->space, isl_dim_param),
919 goto error);
921 hash = isl_space_get_hash(part->dim);
922 entry = isl_hash_table_find(u->space->ctx, &u->table, hash,
923 &isl_union_pw_qpolynomial_fold_has_same_domain_space,
924 part->dim, 1);
925 if (!entry)
926 goto error;
928 if (!entry->data)
929 entry->data = part;
930 else {
931 entry->data = isl_pw_qpolynomial_fold_fold(entry->data,
932 isl_pw_qpolynomial_fold_copy(part));
933 if (!entry->data)
934 goto error;
935 isl_pw_qpolynomial_fold_free(part);
938 return u;
939 error:
940 isl_pw_qpolynomial_fold_free(part);
941 isl_union_pw_qpolynomial_fold_free(u);
942 return NULL;
945 static int fold_part(__isl_take isl_pw_qpolynomial_fold *part, void *user)
947 isl_union_pw_qpolynomial_fold **u;
948 u = (isl_union_pw_qpolynomial_fold **)user;
950 *u = isl_union_pw_qpolynomial_fold_fold_pw_qpolynomial_fold(*u, part);
952 return 0;
955 __isl_give isl_union_pw_qpolynomial_fold *isl_union_pw_qpolynomial_fold_fold(
956 __isl_take isl_union_pw_qpolynomial_fold *u1,
957 __isl_take isl_union_pw_qpolynomial_fold *u2)
959 u1 = isl_union_pw_qpolynomial_fold_cow(u1);
961 if (!u1 || !u2)
962 goto error;
964 if (isl_union_pw_qpolynomial_fold_foreach_pw_qpolynomial_fold(u2,
965 &fold_part, &u1) < 0)
966 goto error;
968 isl_union_pw_qpolynomial_fold_free(u2);
970 return u1;
971 error:
972 isl_union_pw_qpolynomial_fold_free(u1);
973 isl_union_pw_qpolynomial_fold_free(u2);
974 return NULL;
977 __isl_give isl_pw_qpolynomial_fold *isl_pw_qpolynomial_fold_from_pw_qpolynomial(
978 enum isl_fold type, __isl_take isl_pw_qpolynomial *pwqp)
980 int i;
981 isl_pw_qpolynomial_fold *pwf;
983 if (!pwqp)
984 return NULL;
986 pwf = isl_pw_qpolynomial_fold_alloc_size(isl_space_copy(pwqp->dim),
987 type, pwqp->n);
989 for (i = 0; i < pwqp->n; ++i)
990 pwf = isl_pw_qpolynomial_fold_add_piece(pwf,
991 isl_set_copy(pwqp->p[i].set),
992 isl_qpolynomial_fold_alloc(type,
993 isl_qpolynomial_copy(pwqp->p[i].qp)));
995 isl_pw_qpolynomial_free(pwqp);
997 return pwf;
1000 __isl_give isl_pw_qpolynomial_fold *isl_pw_qpolynomial_fold_add(
1001 __isl_take isl_pw_qpolynomial_fold *pwf1,
1002 __isl_take isl_pw_qpolynomial_fold *pwf2)
1004 return isl_pw_qpolynomial_fold_union_add_(pwf1, pwf2);
1007 int isl_qpolynomial_fold_plain_is_equal(__isl_keep isl_qpolynomial_fold *fold1,
1008 __isl_keep isl_qpolynomial_fold *fold2)
1010 int i;
1012 if (!fold1 || !fold2)
1013 return -1;
1015 if (fold1->n != fold2->n)
1016 return 0;
1018 /* We probably want to sort the qps first... */
1019 for (i = 0; i < fold1->n; ++i) {
1020 int eq = isl_qpolynomial_plain_is_equal(fold1->qp[i], fold2->qp[i]);
1021 if (eq < 0 || !eq)
1022 return eq;
1025 return 1;
1028 __isl_give isl_val *isl_qpolynomial_fold_eval(
1029 __isl_take isl_qpolynomial_fold *fold, __isl_take isl_point *pnt)
1031 isl_ctx *ctx;
1032 isl_val *v;
1034 if (!fold || !pnt)
1035 goto error;
1036 ctx = isl_point_get_ctx(pnt);
1037 isl_assert(pnt->dim->ctx, isl_space_is_equal(pnt->dim, fold->dim), goto error);
1038 isl_assert(pnt->dim->ctx,
1039 fold->type == isl_fold_max || fold->type == isl_fold_min,
1040 goto error);
1042 if (fold->n == 0)
1043 v = isl_val_zero(ctx);
1044 else {
1045 int i;
1046 v = isl_qpolynomial_eval(isl_qpolynomial_copy(fold->qp[0]),
1047 isl_point_copy(pnt));
1048 for (i = 1; i < fold->n; ++i) {
1049 isl_val *v_i;
1050 v_i = isl_qpolynomial_eval(
1051 isl_qpolynomial_copy(fold->qp[i]),
1052 isl_point_copy(pnt));
1053 if (fold->type == isl_fold_max)
1054 v = isl_val_max(v, v_i);
1055 else
1056 v = isl_val_min(v, v_i);
1059 isl_qpolynomial_fold_free(fold);
1060 isl_point_free(pnt);
1062 return v;
1063 error:
1064 isl_qpolynomial_fold_free(fold);
1065 isl_point_free(pnt);
1066 return NULL;
1069 size_t isl_pw_qpolynomial_fold_size(__isl_keep isl_pw_qpolynomial_fold *pwf)
1071 int i;
1072 size_t n = 0;
1074 for (i = 0; i < pwf->n; ++i)
1075 n += pwf->p[i].fold->n;
1077 return n;
1080 __isl_give isl_val *isl_qpolynomial_fold_opt_on_domain(
1081 __isl_take isl_qpolynomial_fold *fold, __isl_take isl_set *set, int max)
1083 int i;
1084 isl_val *opt;
1086 if (!set || !fold)
1087 goto error;
1089 if (fold->n == 0) {
1090 opt = isl_val_zero(isl_set_get_ctx(set));
1091 isl_set_free(set);
1092 isl_qpolynomial_fold_free(fold);
1093 return opt;
1096 opt = isl_qpolynomial_opt_on_domain(isl_qpolynomial_copy(fold->qp[0]),
1097 isl_set_copy(set), max);
1098 for (i = 1; i < fold->n; ++i) {
1099 isl_val *opt_i;
1100 opt_i = isl_qpolynomial_opt_on_domain(
1101 isl_qpolynomial_copy(fold->qp[i]),
1102 isl_set_copy(set), max);
1103 if (max)
1104 opt = isl_val_max(opt, opt_i);
1105 else
1106 opt = isl_val_min(opt, opt_i);
1109 isl_set_free(set);
1110 isl_qpolynomial_fold_free(fold);
1112 return opt;
1113 error:
1114 isl_set_free(set);
1115 isl_qpolynomial_fold_free(fold);
1116 return NULL;
1119 /* Check whether for each quasi-polynomial in "fold2" there is
1120 * a quasi-polynomial in "fold1" that dominates it on "set".
1122 static int qpolynomial_fold_covers_on_domain(__isl_keep isl_set *set,
1123 __isl_keep isl_qpolynomial_fold *fold1,
1124 __isl_keep isl_qpolynomial_fold *fold2)
1126 int i, j;
1127 int covers;
1129 if (!set || !fold1 || !fold2)
1130 return -1;
1132 covers = fold1->type == isl_fold_max ? 1 : -1;
1134 for (i = 0; i < fold2->n; ++i) {
1135 for (j = 0; j < fold1->n; ++j) {
1136 isl_qpolynomial *d;
1137 int sgn;
1139 d = isl_qpolynomial_sub(
1140 isl_qpolynomial_copy(fold1->qp[j]),
1141 isl_qpolynomial_copy(fold2->qp[i]));
1142 sgn = isl_qpolynomial_sign(set, d);
1143 isl_qpolynomial_free(d);
1144 if (sgn == covers)
1145 break;
1147 if (j >= fold1->n)
1148 return 0;
1151 return 1;
1154 /* Check whether "pwf1" dominated "pwf2", i.e., the domain of "pwf1" contains
1155 * that of "pwf2" and on each cell, the corresponding fold from pwf1 dominates
1156 * that of pwf2.
1158 int isl_pw_qpolynomial_fold_covers(__isl_keep isl_pw_qpolynomial_fold *pwf1,
1159 __isl_keep isl_pw_qpolynomial_fold *pwf2)
1161 int i, j;
1162 isl_set *dom1, *dom2;
1163 int is_subset;
1165 if (!pwf1 || !pwf2)
1166 return -1;
1168 if (pwf2->n == 0)
1169 return 1;
1170 if (pwf1->n == 0)
1171 return 0;
1173 dom1 = isl_pw_qpolynomial_fold_domain(isl_pw_qpolynomial_fold_copy(pwf1));
1174 dom2 = isl_pw_qpolynomial_fold_domain(isl_pw_qpolynomial_fold_copy(pwf2));
1175 is_subset = isl_set_is_subset(dom2, dom1);
1176 isl_set_free(dom1);
1177 isl_set_free(dom2);
1179 if (is_subset < 0 || !is_subset)
1180 return is_subset;
1182 for (i = 0; i < pwf2->n; ++i) {
1183 for (j = 0; j < pwf1->n; ++j) {
1184 int is_empty;
1185 isl_set *common;
1186 int covers;
1188 common = isl_set_intersect(isl_set_copy(pwf1->p[j].set),
1189 isl_set_copy(pwf2->p[i].set));
1190 is_empty = isl_set_is_empty(common);
1191 if (is_empty < 0 || is_empty) {
1192 isl_set_free(common);
1193 if (is_empty < 0)
1194 return -1;
1195 continue;
1197 covers = qpolynomial_fold_covers_on_domain(common,
1198 pwf1->p[j].fold, pwf2->p[i].fold);
1199 isl_set_free(common);
1200 if (covers < 0 || !covers)
1201 return covers;
1205 return 1;
1208 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_morph_domain(
1209 __isl_take isl_qpolynomial_fold *fold, __isl_take isl_morph *morph)
1211 int i;
1212 isl_ctx *ctx;
1214 if (!fold || !morph)
1215 goto error;
1217 ctx = fold->dim->ctx;
1218 isl_assert(ctx, isl_space_is_equal(fold->dim, morph->dom->dim), goto error);
1220 fold = isl_qpolynomial_fold_cow(fold);
1221 if (!fold)
1222 goto error;
1224 isl_space_free(fold->dim);
1225 fold->dim = isl_space_copy(morph->ran->dim);
1226 if (!fold->dim)
1227 goto error;
1229 for (i = 0; i < fold->n; ++i) {
1230 fold->qp[i] = isl_qpolynomial_morph_domain(fold->qp[i],
1231 isl_morph_copy(morph));
1232 if (!fold->qp[i])
1233 goto error;
1236 isl_morph_free(morph);
1238 return fold;
1239 error:
1240 isl_qpolynomial_fold_free(fold);
1241 isl_morph_free(morph);
1242 return NULL;
1245 enum isl_fold isl_qpolynomial_fold_get_type(__isl_keep isl_qpolynomial_fold *fold)
1247 if (!fold)
1248 return isl_fold_list;
1249 return fold->type;
1252 enum isl_fold isl_union_pw_qpolynomial_fold_get_type(
1253 __isl_keep isl_union_pw_qpolynomial_fold *upwf)
1255 if (!upwf)
1256 return isl_fold_list;
1257 return upwf->type;
1260 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_lift(
1261 __isl_take isl_qpolynomial_fold *fold, __isl_take isl_space *dim)
1263 int i;
1265 if (!fold || !dim)
1266 goto error;
1268 if (isl_space_is_equal(fold->dim, dim)) {
1269 isl_space_free(dim);
1270 return fold;
1273 fold = isl_qpolynomial_fold_cow(fold);
1274 if (!fold)
1275 goto error;
1277 isl_space_free(fold->dim);
1278 fold->dim = isl_space_copy(dim);
1279 if (!fold->dim)
1280 goto error;
1282 for (i = 0; i < fold->n; ++i) {
1283 fold->qp[i] = isl_qpolynomial_lift(fold->qp[i],
1284 isl_space_copy(dim));
1285 if (!fold->qp[i])
1286 goto error;
1289 isl_space_free(dim);
1291 return fold;
1292 error:
1293 isl_qpolynomial_fold_free(fold);
1294 isl_space_free(dim);
1295 return NULL;
1298 int isl_qpolynomial_fold_foreach_qpolynomial(
1299 __isl_keep isl_qpolynomial_fold *fold,
1300 int (*fn)(__isl_take isl_qpolynomial *qp, void *user), void *user)
1302 int i;
1304 if (!fold)
1305 return -1;
1307 for (i = 0; i < fold->n; ++i)
1308 if (fn(isl_qpolynomial_copy(fold->qp[i]), user) < 0)
1309 return -1;
1311 return 0;
1314 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_move_dims(
1315 __isl_take isl_qpolynomial_fold *fold,
1316 enum isl_dim_type dst_type, unsigned dst_pos,
1317 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
1319 int i;
1321 if (n == 0)
1322 return fold;
1324 fold = isl_qpolynomial_fold_cow(fold);
1325 if (!fold)
1326 return NULL;
1328 fold->dim = isl_space_move_dims(fold->dim, dst_type, dst_pos,
1329 src_type, src_pos, n);
1330 if (!fold->dim)
1331 goto error;
1333 for (i = 0; i < fold->n; ++i) {
1334 fold->qp[i] = isl_qpolynomial_move_dims(fold->qp[i],
1335 dst_type, dst_pos, src_type, src_pos, n);
1336 if (!fold->qp[i])
1337 goto error;
1340 return fold;
1341 error:
1342 isl_qpolynomial_fold_free(fold);
1343 return NULL;
1346 /* For each 0 <= i < "n", replace variable "first" + i of type "type"
1347 * in fold->qp[k] by subs[i].
1349 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_substitute(
1350 __isl_take isl_qpolynomial_fold *fold,
1351 enum isl_dim_type type, unsigned first, unsigned n,
1352 __isl_keep isl_qpolynomial **subs)
1354 int i;
1356 if (n == 0)
1357 return fold;
1359 fold = isl_qpolynomial_fold_cow(fold);
1360 if (!fold)
1361 return NULL;
1363 for (i = 0; i < fold->n; ++i) {
1364 fold->qp[i] = isl_qpolynomial_substitute(fold->qp[i],
1365 type, first, n, subs);
1366 if (!fold->qp[i])
1367 goto error;
1370 return fold;
1371 error:
1372 isl_qpolynomial_fold_free(fold);
1373 return NULL;
1376 static int add_pwqp(__isl_take isl_pw_qpolynomial *pwqp, void *user)
1378 isl_ctx *ctx;
1379 isl_pw_qpolynomial_fold *pwf;
1380 isl_union_pw_qpolynomial_fold **upwf;
1381 uint32_t hash;
1382 struct isl_hash_table_entry *entry;
1384 upwf = (isl_union_pw_qpolynomial_fold **)user;
1386 ctx = pwqp->dim->ctx;
1387 hash = isl_space_get_hash(pwqp->dim);
1388 entry = isl_hash_table_find(ctx, &(*upwf)->table, hash,
1389 &isl_union_pw_qpolynomial_fold_has_same_domain_space,
1390 pwqp->dim, 1);
1391 if (!entry)
1392 goto error;
1394 pwf = isl_pw_qpolynomial_fold_from_pw_qpolynomial((*upwf)->type, pwqp);
1395 if (!entry->data)
1396 entry->data = pwf;
1397 else {
1398 entry->data = isl_pw_qpolynomial_fold_add(entry->data, pwf);
1399 if (!entry->data)
1400 return -1;
1401 if (isl_pw_qpolynomial_fold_is_zero(entry->data)) {
1402 isl_pw_qpolynomial_fold_free(entry->data);
1403 isl_hash_table_remove(ctx, &(*upwf)->table, entry);
1407 return 0;
1408 error:
1409 isl_pw_qpolynomial_free(pwqp);
1410 return -1;
1413 __isl_give isl_union_pw_qpolynomial_fold *isl_union_pw_qpolynomial_fold_add_union_pw_qpolynomial(
1414 __isl_take isl_union_pw_qpolynomial_fold *upwf,
1415 __isl_take isl_union_pw_qpolynomial *upwqp)
1417 upwf = isl_union_pw_qpolynomial_fold_align_params(upwf,
1418 isl_union_pw_qpolynomial_get_space(upwqp));
1419 upwqp = isl_union_pw_qpolynomial_align_params(upwqp,
1420 isl_union_pw_qpolynomial_fold_get_space(upwf));
1422 upwf = isl_union_pw_qpolynomial_fold_cow(upwf);
1423 if (!upwf || !upwqp)
1424 goto error;
1426 if (isl_union_pw_qpolynomial_foreach_pw_qpolynomial(upwqp, &add_pwqp,
1427 &upwf) < 0)
1428 goto error;
1430 isl_union_pw_qpolynomial_free(upwqp);
1432 return upwf;
1433 error:
1434 isl_union_pw_qpolynomial_fold_free(upwf);
1435 isl_union_pw_qpolynomial_free(upwqp);
1436 return NULL;
1439 static int join_compatible(__isl_keep isl_space *dim1, __isl_keep isl_space *dim2)
1441 int m;
1442 m = isl_space_match(dim1, isl_dim_param, dim2, isl_dim_param);
1443 if (m < 0 || !m)
1444 return m;
1445 return isl_space_tuple_is_equal(dim1, isl_dim_out, dim2, isl_dim_in);
1448 /* Compute the intersection of the range of the map and the domain
1449 * of the piecewise quasipolynomial reduction and then compute a bound
1450 * on the associated quasipolynomial reduction over all elements
1451 * in this intersection.
1453 * We first introduce some unconstrained dimensions in the
1454 * piecewise quasipolynomial, intersect the resulting domain
1455 * with the wrapped map and the compute the sum.
1457 __isl_give isl_pw_qpolynomial_fold *isl_map_apply_pw_qpolynomial_fold(
1458 __isl_take isl_map *map, __isl_take isl_pw_qpolynomial_fold *pwf,
1459 int *tight)
1461 isl_ctx *ctx;
1462 isl_set *dom;
1463 isl_space *map_dim;
1464 isl_space *pwf_dim;
1465 unsigned n_in;
1466 int ok;
1468 ctx = isl_map_get_ctx(map);
1469 if (!ctx)
1470 goto error;
1472 map_dim = isl_map_get_space(map);
1473 pwf_dim = isl_pw_qpolynomial_fold_get_space(pwf);
1474 ok = join_compatible(map_dim, pwf_dim);
1475 isl_space_free(map_dim);
1476 isl_space_free(pwf_dim);
1477 if (!ok)
1478 isl_die(ctx, isl_error_invalid, "incompatible dimensions",
1479 goto error);
1481 n_in = isl_map_dim(map, isl_dim_in);
1482 pwf = isl_pw_qpolynomial_fold_insert_dims(pwf, isl_dim_in, 0, n_in);
1484 dom = isl_map_wrap(map);
1485 pwf = isl_pw_qpolynomial_fold_reset_domain_space(pwf,
1486 isl_set_get_space(dom));
1488 pwf = isl_pw_qpolynomial_fold_intersect_domain(pwf, dom);
1489 pwf = isl_pw_qpolynomial_fold_bound(pwf, tight);
1491 return pwf;
1492 error:
1493 isl_map_free(map);
1494 isl_pw_qpolynomial_fold_free(pwf);
1495 return NULL;
1498 __isl_give isl_pw_qpolynomial_fold *isl_set_apply_pw_qpolynomial_fold(
1499 __isl_take isl_set *set, __isl_take isl_pw_qpolynomial_fold *pwf,
1500 int *tight)
1502 return isl_map_apply_pw_qpolynomial_fold(set, pwf, tight);
1505 struct isl_apply_fold_data {
1506 isl_union_pw_qpolynomial_fold *upwf;
1507 isl_union_pw_qpolynomial_fold *res;
1508 isl_map *map;
1509 int tight;
1512 static int pw_qpolynomial_fold_apply(__isl_take isl_pw_qpolynomial_fold *pwf,
1513 void *user)
1515 isl_space *map_dim;
1516 isl_space *pwf_dim;
1517 struct isl_apply_fold_data *data = user;
1518 int ok;
1520 map_dim = isl_map_get_space(data->map);
1521 pwf_dim = isl_pw_qpolynomial_fold_get_space(pwf);
1522 ok = join_compatible(map_dim, pwf_dim);
1523 isl_space_free(map_dim);
1524 isl_space_free(pwf_dim);
1526 if (ok) {
1527 pwf = isl_map_apply_pw_qpolynomial_fold(isl_map_copy(data->map),
1528 pwf, data->tight ? &data->tight : NULL);
1529 data->res = isl_union_pw_qpolynomial_fold_fold_pw_qpolynomial_fold(
1530 data->res, pwf);
1531 } else
1532 isl_pw_qpolynomial_fold_free(pwf);
1534 return 0;
1537 static int map_apply(__isl_take isl_map *map, void *user)
1539 struct isl_apply_fold_data *data = user;
1540 int r;
1542 data->map = map;
1543 r = isl_union_pw_qpolynomial_fold_foreach_pw_qpolynomial_fold(
1544 data->upwf, &pw_qpolynomial_fold_apply, data);
1546 isl_map_free(map);
1547 return r;
1550 __isl_give isl_union_pw_qpolynomial_fold *isl_union_map_apply_union_pw_qpolynomial_fold(
1551 __isl_take isl_union_map *umap,
1552 __isl_take isl_union_pw_qpolynomial_fold *upwf, int *tight)
1554 isl_space *dim;
1555 enum isl_fold type;
1556 struct isl_apply_fold_data data;
1558 upwf = isl_union_pw_qpolynomial_fold_align_params(upwf,
1559 isl_union_map_get_space(umap));
1560 umap = isl_union_map_align_params(umap,
1561 isl_union_pw_qpolynomial_fold_get_space(upwf));
1563 data.upwf = upwf;
1564 data.tight = tight ? 1 : 0;
1565 dim = isl_union_pw_qpolynomial_fold_get_space(upwf);
1566 type = isl_union_pw_qpolynomial_fold_get_type(upwf);
1567 data.res = isl_union_pw_qpolynomial_fold_zero(dim, type);
1568 if (isl_union_map_foreach_map(umap, &map_apply, &data) < 0)
1569 goto error;
1571 isl_union_map_free(umap);
1572 isl_union_pw_qpolynomial_fold_free(upwf);
1574 if (tight)
1575 *tight = data.tight;
1577 return data.res;
1578 error:
1579 isl_union_map_free(umap);
1580 isl_union_pw_qpolynomial_fold_free(upwf);
1581 isl_union_pw_qpolynomial_fold_free(data.res);
1582 return NULL;
1585 __isl_give isl_union_pw_qpolynomial_fold *isl_union_set_apply_union_pw_qpolynomial_fold(
1586 __isl_take isl_union_set *uset,
1587 __isl_take isl_union_pw_qpolynomial_fold *upwf, int *tight)
1589 return isl_union_map_apply_union_pw_qpolynomial_fold(uset, upwf, tight);
1592 /* Reorder the dimension of "fold" according to the given reordering.
1594 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_realign_domain(
1595 __isl_take isl_qpolynomial_fold *fold, __isl_take isl_reordering *r)
1597 int i;
1599 fold = isl_qpolynomial_fold_cow(fold);
1600 if (!fold || !r)
1601 goto error;
1603 for (i = 0; i < fold->n; ++i) {
1604 fold->qp[i] = isl_qpolynomial_realign_domain(fold->qp[i],
1605 isl_reordering_copy(r));
1606 if (!fold->qp[i])
1607 goto error;
1610 fold = isl_qpolynomial_fold_reset_domain_space(fold,
1611 isl_space_copy(r->dim));
1613 isl_reordering_free(r);
1615 return fold;
1616 error:
1617 isl_qpolynomial_fold_free(fold);
1618 isl_reordering_free(r);
1619 return NULL;
1622 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_mul_isl_int(
1623 __isl_take isl_qpolynomial_fold *fold, isl_int v)
1625 int i;
1627 if (isl_int_is_one(v))
1628 return fold;
1629 if (fold && isl_int_is_zero(v)) {
1630 isl_qpolynomial_fold *zero;
1631 isl_space *dim = isl_space_copy(fold->dim);
1632 zero = isl_qpolynomial_fold_empty(fold->type, dim);
1633 isl_qpolynomial_fold_free(fold);
1634 return zero;
1637 fold = isl_qpolynomial_fold_cow(fold);
1638 if (!fold)
1639 return NULL;
1641 if (isl_int_is_neg(v))
1642 fold->type = isl_fold_type_negate(fold->type);
1643 for (i = 0; i < fold->n; ++i) {
1644 fold->qp[i] = isl_qpolynomial_mul_isl_int(fold->qp[i], v);
1645 if (!fold->qp[i])
1646 goto error;
1649 return fold;
1650 error:
1651 isl_qpolynomial_fold_free(fold);
1652 return NULL;
1655 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_scale(
1656 __isl_take isl_qpolynomial_fold *fold, isl_int v)
1658 return isl_qpolynomial_fold_mul_isl_int(fold, v);
1661 /* Multiply "fold" by "v".
1663 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_scale_val(
1664 __isl_take isl_qpolynomial_fold *fold, __isl_take isl_val *v)
1666 int i;
1668 if (!fold || !v)
1669 goto error;
1671 if (isl_val_is_one(v)) {
1672 isl_val_free(v);
1673 return fold;
1675 if (isl_val_is_zero(v)) {
1676 isl_qpolynomial_fold *zero;
1677 isl_space *space = isl_qpolynomial_fold_get_domain_space(fold);
1678 zero = isl_qpolynomial_fold_empty(fold->type, space);
1679 isl_qpolynomial_fold_free(fold);
1680 isl_val_free(v);
1681 return zero;
1683 if (!isl_val_is_rat(v))
1684 isl_die(isl_qpolynomial_fold_get_ctx(fold), isl_error_invalid,
1685 "expecting rational factor", goto error);
1687 fold = isl_qpolynomial_fold_cow(fold);
1688 if (!fold)
1689 goto error;
1691 if (isl_val_is_neg(v))
1692 fold->type = isl_fold_type_negate(fold->type);
1693 for (i = 0; i < fold->n; ++i) {
1694 fold->qp[i] = isl_qpolynomial_scale_val(fold->qp[i],
1695 isl_val_copy(v));
1696 if (!fold->qp[i])
1697 goto error;
1700 isl_val_free(v);
1701 return fold;
1702 error:
1703 isl_val_free(v);
1704 isl_qpolynomial_fold_free(fold);
1705 return NULL;
1708 /* Divide "fold" by "v".
1710 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_scale_down_val(
1711 __isl_take isl_qpolynomial_fold *fold, __isl_take isl_val *v)
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_rat(v))
1721 isl_die(isl_qpolynomial_fold_get_ctx(fold), isl_error_invalid,
1722 "expecting rational factor", goto error);
1723 if (isl_val_is_zero(v))
1724 isl_die(isl_val_get_ctx(v), isl_error_invalid,
1725 "cannot scale down by zero", goto error);
1727 return isl_qpolynomial_fold_scale_val(fold, isl_val_inv(v));
1728 error:
1729 isl_val_free(v);
1730 isl_qpolynomial_fold_free(fold);
1731 return NULL;