deprecate isl_int
[isl.git] / isl_fold.c
blob379223674b6a3233ed2cd2316c971299ea93dbec
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 static int isl_qpolynomial_cst_sign(__isl_keep isl_qpolynomial *qp)
239 struct isl_upoly_cst *cst;
241 cst = isl_upoly_as_cst(qp->upoly);
242 if (!cst)
243 return 0;
245 return isl_int_sgn(cst->n) < 0 ? -1 : 1;
248 static int isl_qpolynomial_aff_sign(__isl_keep isl_set *set,
249 __isl_keep isl_qpolynomial *qp)
251 enum isl_lp_result res;
252 isl_vec *aff;
253 isl_int opt;
254 int sgn = 0;
256 aff = isl_qpolynomial_extract_affine(qp);
257 if (!aff)
258 return 0;
260 isl_int_init(opt);
262 res = isl_set_solve_lp(set, 0, aff->el + 1, aff->el[0],
263 &opt, NULL, NULL);
264 if (res == isl_lp_error)
265 goto done;
266 if (res == isl_lp_empty ||
267 (res == isl_lp_ok && !isl_int_is_neg(opt))) {
268 sgn = 1;
269 goto done;
272 res = isl_set_solve_lp(set, 1, aff->el + 1, aff->el[0],
273 &opt, NULL, NULL);
274 if (res == isl_lp_ok && !isl_int_is_pos(opt))
275 sgn = -1;
277 done:
278 isl_int_clear(opt);
279 isl_vec_free(aff);
280 return sgn;
283 /* Determine, if possible, the sign of the quasipolynomial "qp" on
284 * the domain "set".
286 * If qp is a constant, then the problem is trivial.
287 * If qp is linear, then we check if the minimum of the corresponding
288 * affine constraint is non-negative or if the maximum is non-positive.
290 * Otherwise, we check if the outermost variable "v" has a lower bound "l"
291 * in "set". If so, we write qp(v,v') as
293 * q(v,v') * (v - l) + r(v')
295 * if q(v,v') and r(v') have the same known sign, then the original
296 * quasipolynomial has the same sign as well.
298 * Return
299 * -1 if qp <= 0
300 * 1 if qp >= 0
301 * 0 if unknown
303 static int isl_qpolynomial_sign(__isl_keep isl_set *set,
304 __isl_keep isl_qpolynomial *qp)
306 int d;
307 int i;
308 int is;
309 struct isl_upoly_rec *rec;
310 isl_vec *v;
311 isl_int l;
312 enum isl_lp_result res;
313 int sgn = 0;
315 is = isl_qpolynomial_is_cst(qp, NULL, NULL);
316 if (is < 0)
317 return 0;
318 if (is)
319 return isl_qpolynomial_cst_sign(qp);
321 is = isl_qpolynomial_is_affine(qp);
322 if (is < 0)
323 return 0;
324 if (is)
325 return isl_qpolynomial_aff_sign(set, qp);
327 if (qp->div->n_row > 0)
328 return 0;
330 rec = isl_upoly_as_rec(qp->upoly);
331 if (!rec)
332 return 0;
334 d = isl_space_dim(qp->dim, isl_dim_all);
335 v = isl_vec_alloc(set->ctx, 2 + d);
336 if (!v)
337 return 0;
339 isl_seq_clr(v->el + 1, 1 + d);
340 isl_int_set_si(v->el[0], 1);
341 isl_int_set_si(v->el[2 + qp->upoly->var], 1);
343 isl_int_init(l);
345 res = isl_set_solve_lp(set, 0, v->el + 1, v->el[0], &l, NULL, NULL);
346 if (res == isl_lp_ok) {
347 isl_qpolynomial *min;
348 isl_qpolynomial *base;
349 isl_qpolynomial *r, *q;
350 isl_qpolynomial *t;
352 min = isl_qpolynomial_cst_on_domain(isl_space_copy(qp->dim), l);
353 base = isl_qpolynomial_var_pow_on_domain(isl_space_copy(qp->dim),
354 qp->upoly->var, 1);
356 r = isl_qpolynomial_alloc(isl_space_copy(qp->dim), 0,
357 isl_upoly_copy(rec->p[rec->n - 1]));
358 q = isl_qpolynomial_copy(r);
360 for (i = rec->n - 2; i >= 0; --i) {
361 r = isl_qpolynomial_mul(r, isl_qpolynomial_copy(min));
362 t = isl_qpolynomial_alloc(isl_space_copy(qp->dim), 0,
363 isl_upoly_copy(rec->p[i]));
364 r = isl_qpolynomial_add(r, t);
365 if (i == 0)
366 break;
367 q = isl_qpolynomial_mul(q, isl_qpolynomial_copy(base));
368 q = isl_qpolynomial_add(q, isl_qpolynomial_copy(r));
371 if (isl_qpolynomial_is_zero(q))
372 sgn = isl_qpolynomial_sign(set, r);
373 else if (isl_qpolynomial_is_zero(r))
374 sgn = isl_qpolynomial_sign(set, q);
375 else {
376 int sgn_q, sgn_r;
377 sgn_r = isl_qpolynomial_sign(set, r);
378 sgn_q = isl_qpolynomial_sign(set, q);
379 if (sgn_r == sgn_q)
380 sgn = sgn_r;
383 isl_qpolynomial_free(min);
384 isl_qpolynomial_free(base);
385 isl_qpolynomial_free(q);
386 isl_qpolynomial_free(r);
389 isl_int_clear(l);
391 isl_vec_free(v);
393 return sgn;
396 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_fold_on_domain(
397 __isl_keep isl_set *set,
398 __isl_take isl_qpolynomial_fold *fold1,
399 __isl_take isl_qpolynomial_fold *fold2)
401 int i, j;
402 int n1;
403 struct isl_qpolynomial_fold *res = NULL;
404 int better;
406 if (!fold1 || !fold2)
407 goto error;
409 isl_assert(fold1->dim->ctx, fold1->type == fold2->type, goto error);
410 isl_assert(fold1->dim->ctx, isl_space_is_equal(fold1->dim, fold2->dim),
411 goto error);
413 better = fold1->type == isl_fold_max ? -1 : 1;
415 if (isl_qpolynomial_fold_is_empty(fold1)) {
416 isl_qpolynomial_fold_free(fold1);
417 return fold2;
420 if (isl_qpolynomial_fold_is_empty(fold2)) {
421 isl_qpolynomial_fold_free(fold2);
422 return fold1;
425 res = qpolynomial_fold_alloc(fold1->type, isl_space_copy(fold1->dim),
426 fold1->n + fold2->n);
427 if (!res)
428 goto error;
430 for (i = 0; i < fold1->n; ++i) {
431 res->qp[res->n] = isl_qpolynomial_copy(fold1->qp[i]);
432 if (!res->qp[res->n])
433 goto error;
434 res->n++;
436 n1 = res->n;
438 for (i = 0; i < fold2->n; ++i) {
439 for (j = n1 - 1; j >= 0; --j) {
440 isl_qpolynomial *d;
441 int sgn;
442 d = isl_qpolynomial_sub(
443 isl_qpolynomial_copy(res->qp[j]),
444 isl_qpolynomial_copy(fold2->qp[i]));
445 sgn = isl_qpolynomial_sign(set, d);
446 isl_qpolynomial_free(d);
447 if (sgn == 0)
448 continue;
449 if (sgn != better)
450 break;
451 isl_qpolynomial_free(res->qp[j]);
452 if (j != n1 - 1)
453 res->qp[j] = res->qp[n1 - 1];
454 n1--;
455 if (n1 != res->n - 1)
456 res->qp[n1] = res->qp[res->n - 1];
457 res->n--;
459 if (j >= 0)
460 continue;
461 res->qp[res->n] = isl_qpolynomial_copy(fold2->qp[i]);
462 if (!res->qp[res->n])
463 goto error;
464 res->n++;
467 isl_qpolynomial_fold_free(fold1);
468 isl_qpolynomial_fold_free(fold2);
470 return res;
471 error:
472 isl_qpolynomial_fold_free(res);
473 isl_qpolynomial_fold_free(fold1);
474 isl_qpolynomial_fold_free(fold2);
475 return NULL;
478 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_add_qpolynomial(
479 __isl_take isl_qpolynomial_fold *fold, __isl_take isl_qpolynomial *qp)
481 int i;
483 if (!fold || !qp)
484 goto error;
486 if (isl_qpolynomial_is_zero(qp)) {
487 isl_qpolynomial_free(qp);
488 return fold;
491 fold = isl_qpolynomial_fold_cow(fold);
492 if (!fold)
493 goto error;
495 for (i = 0; i < fold->n; ++i) {
496 fold->qp[i] = isl_qpolynomial_add(fold->qp[i],
497 isl_qpolynomial_copy(qp));
498 if (!fold->qp[i])
499 goto error;
502 isl_qpolynomial_free(qp);
503 return fold;
504 error:
505 isl_qpolynomial_fold_free(fold);
506 isl_qpolynomial_free(qp);
507 return NULL;
510 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_add_on_domain(
511 __isl_keep isl_set *dom,
512 __isl_take isl_qpolynomial_fold *fold1,
513 __isl_take isl_qpolynomial_fold *fold2)
515 int i;
516 isl_qpolynomial_fold *res = NULL;
518 if (!fold1 || !fold2)
519 goto error;
521 if (isl_qpolynomial_fold_is_empty(fold1)) {
522 isl_qpolynomial_fold_free(fold1);
523 return fold2;
526 if (isl_qpolynomial_fold_is_empty(fold2)) {
527 isl_qpolynomial_fold_free(fold2);
528 return fold1;
531 if (fold1->n == 1 && fold2->n != 1)
532 return isl_qpolynomial_fold_add_on_domain(dom, fold2, fold1);
534 if (fold2->n == 1) {
535 res = isl_qpolynomial_fold_add_qpolynomial(fold1,
536 isl_qpolynomial_copy(fold2->qp[0]));
537 isl_qpolynomial_fold_free(fold2);
538 return res;
541 res = isl_qpolynomial_fold_add_qpolynomial(
542 isl_qpolynomial_fold_copy(fold1),
543 isl_qpolynomial_copy(fold2->qp[0]));
545 for (i = 1; i < fold2->n; ++i) {
546 isl_qpolynomial_fold *res_i;
547 res_i = isl_qpolynomial_fold_add_qpolynomial(
548 isl_qpolynomial_fold_copy(fold1),
549 isl_qpolynomial_copy(fold2->qp[i]));
550 res = isl_qpolynomial_fold_fold_on_domain(dom, res, res_i);
553 isl_qpolynomial_fold_free(fold1);
554 isl_qpolynomial_fold_free(fold2);
555 return res;
556 error:
557 isl_qpolynomial_fold_free(res);
558 isl_qpolynomial_fold_free(fold1);
559 isl_qpolynomial_fold_free(fold2);
560 return NULL;
563 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_substitute_equalities(
564 __isl_take isl_qpolynomial_fold *fold, __isl_take isl_basic_set *eq)
566 int i;
568 if (!fold || !eq)
569 goto error;
571 fold = isl_qpolynomial_fold_cow(fold);
572 if (!fold)
573 return NULL;
575 for (i = 0; i < fold->n; ++i) {
576 fold->qp[i] = isl_qpolynomial_substitute_equalities(fold->qp[i],
577 isl_basic_set_copy(eq));
578 if (!fold->qp[i])
579 goto error;
582 isl_basic_set_free(eq);
583 return fold;
584 error:
585 isl_basic_set_free(eq);
586 isl_qpolynomial_fold_free(fold);
587 return NULL;
590 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_gist(
591 __isl_take isl_qpolynomial_fold *fold, __isl_take isl_set *context)
593 int i;
595 if (!fold || !context)
596 goto error;
598 fold = isl_qpolynomial_fold_cow(fold);
599 if (!fold)
600 return NULL;
602 for (i = 0; i < fold->n; ++i) {
603 fold->qp[i] = isl_qpolynomial_gist(fold->qp[i],
604 isl_set_copy(context));
605 if (!fold->qp[i])
606 goto error;
609 isl_set_free(context);
610 return fold;
611 error:
612 isl_set_free(context);
613 isl_qpolynomial_fold_free(fold);
614 return NULL;
617 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_gist_params(
618 __isl_take isl_qpolynomial_fold *fold, __isl_take isl_set *context)
620 isl_space *space = isl_qpolynomial_fold_get_domain_space(fold);
621 isl_set *dom_context = isl_set_universe(space);
622 dom_context = isl_set_intersect_params(dom_context, context);
623 return isl_qpolynomial_fold_gist(fold, dom_context);
626 #define HAS_TYPE
628 #undef PW
629 #define PW isl_pw_qpolynomial_fold
630 #undef EL
631 #define EL isl_qpolynomial_fold
632 #undef EL_IS_ZERO
633 #define EL_IS_ZERO is_empty
634 #undef ZERO
635 #define ZERO zero
636 #undef IS_ZERO
637 #define IS_ZERO is_zero
638 #undef FIELD
639 #define FIELD fold
640 #undef DEFAULT_IS_ZERO
641 #define DEFAULT_IS_ZERO 1
643 #define NO_NEG
644 #define NO_PULLBACK
646 #include <isl_pw_templ.c>
648 #undef UNION
649 #define UNION isl_union_pw_qpolynomial_fold
650 #undef PART
651 #define PART isl_pw_qpolynomial_fold
652 #undef PARTS
653 #define PARTS pw_qpolynomial_fold
654 #define ALIGN_DOMAIN
656 #define NO_SUB
658 #include <isl_union_templ.c>
660 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_empty(enum isl_fold type,
661 __isl_take isl_space *dim)
663 return qpolynomial_fold_alloc(type, dim, 0);
666 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_alloc(
667 enum isl_fold type, __isl_take isl_qpolynomial *qp)
669 isl_qpolynomial_fold *fold;
671 if (!qp)
672 return NULL;
674 fold = qpolynomial_fold_alloc(type, isl_space_copy(qp->dim), 1);
675 if (!fold)
676 goto error;
678 fold->qp[0] = qp;
679 fold->n++;
681 return fold;
682 error:
683 isl_qpolynomial_fold_free(fold);
684 isl_qpolynomial_free(qp);
685 return NULL;
688 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_copy(
689 __isl_keep isl_qpolynomial_fold *fold)
691 if (!fold)
692 return NULL;
694 fold->ref++;
695 return fold;
698 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_dup(
699 __isl_keep isl_qpolynomial_fold *fold)
701 int i;
702 isl_qpolynomial_fold *dup;
704 if (!fold)
705 return NULL;
706 dup = qpolynomial_fold_alloc(fold->type,
707 isl_space_copy(fold->dim), fold->n);
708 if (!dup)
709 return NULL;
711 dup->n = fold->n;
712 for (i = 0; i < fold->n; ++i) {
713 dup->qp[i] = isl_qpolynomial_copy(fold->qp[i]);
714 if (!dup->qp[i])
715 goto error;
718 return dup;
719 error:
720 isl_qpolynomial_fold_free(dup);
721 return NULL;
724 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_cow(
725 __isl_take isl_qpolynomial_fold *fold)
727 if (!fold)
728 return NULL;
730 if (fold->ref == 1)
731 return fold;
732 fold->ref--;
733 return isl_qpolynomial_fold_dup(fold);
736 void isl_qpolynomial_fold_free(__isl_take isl_qpolynomial_fold *fold)
738 int i;
740 if (!fold)
741 return;
742 if (--fold->ref > 0)
743 return;
745 for (i = 0; i < fold->n; ++i)
746 isl_qpolynomial_free(fold->qp[i]);
747 isl_space_free(fold->dim);
748 free(fold);
751 int isl_qpolynomial_fold_is_empty(__isl_keep isl_qpolynomial_fold *fold)
753 if (!fold)
754 return -1;
756 return fold->n == 0;
759 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_fold(
760 __isl_take isl_qpolynomial_fold *fold1,
761 __isl_take isl_qpolynomial_fold *fold2)
763 int i;
764 struct isl_qpolynomial_fold *res = NULL;
766 if (!fold1 || !fold2)
767 goto error;
769 isl_assert(fold1->dim->ctx, fold1->type == fold2->type, goto error);
770 isl_assert(fold1->dim->ctx, isl_space_is_equal(fold1->dim, fold2->dim),
771 goto error);
773 if (isl_qpolynomial_fold_is_empty(fold1)) {
774 isl_qpolynomial_fold_free(fold1);
775 return fold2;
778 if (isl_qpolynomial_fold_is_empty(fold2)) {
779 isl_qpolynomial_fold_free(fold2);
780 return fold1;
783 res = qpolynomial_fold_alloc(fold1->type, isl_space_copy(fold1->dim),
784 fold1->n + fold2->n);
785 if (!res)
786 goto error;
788 for (i = 0; i < fold1->n; ++i) {
789 res->qp[res->n] = isl_qpolynomial_copy(fold1->qp[i]);
790 if (!res->qp[res->n])
791 goto error;
792 res->n++;
795 for (i = 0; i < fold2->n; ++i) {
796 res->qp[res->n] = isl_qpolynomial_copy(fold2->qp[i]);
797 if (!res->qp[res->n])
798 goto error;
799 res->n++;
802 isl_qpolynomial_fold_free(fold1);
803 isl_qpolynomial_fold_free(fold2);
805 return res;
806 error:
807 isl_qpolynomial_fold_free(res);
808 isl_qpolynomial_fold_free(fold1);
809 isl_qpolynomial_fold_free(fold2);
810 return NULL;
813 __isl_give isl_pw_qpolynomial_fold *isl_pw_qpolynomial_fold_fold(
814 __isl_take isl_pw_qpolynomial_fold *pw1,
815 __isl_take isl_pw_qpolynomial_fold *pw2)
817 int i, j, n;
818 struct isl_pw_qpolynomial_fold *res;
819 isl_set *set;
821 if (!pw1 || !pw2)
822 goto error;
824 isl_assert(pw1->dim->ctx, isl_space_is_equal(pw1->dim, pw2->dim), goto error);
826 if (isl_pw_qpolynomial_fold_is_zero(pw1)) {
827 isl_pw_qpolynomial_fold_free(pw1);
828 return pw2;
831 if (isl_pw_qpolynomial_fold_is_zero(pw2)) {
832 isl_pw_qpolynomial_fold_free(pw2);
833 return pw1;
836 if (pw1->type != pw2->type)
837 isl_die(pw1->dim->ctx, isl_error_invalid,
838 "fold types don't match", goto error);
840 n = (pw1->n + 1) * (pw2->n + 1);
841 res = isl_pw_qpolynomial_fold_alloc_size(isl_space_copy(pw1->dim),
842 pw1->type, n);
844 for (i = 0; i < pw1->n; ++i) {
845 set = isl_set_copy(pw1->p[i].set);
846 for (j = 0; j < pw2->n; ++j) {
847 struct isl_set *common;
848 isl_qpolynomial_fold *sum;
849 set = isl_set_subtract(set,
850 isl_set_copy(pw2->p[j].set));
851 common = isl_set_intersect(isl_set_copy(pw1->p[i].set),
852 isl_set_copy(pw2->p[j].set));
853 if (isl_set_plain_is_empty(common)) {
854 isl_set_free(common);
855 continue;
858 sum = isl_qpolynomial_fold_fold_on_domain(common,
859 isl_qpolynomial_fold_copy(pw1->p[i].fold),
860 isl_qpolynomial_fold_copy(pw2->p[j].fold));
862 res = isl_pw_qpolynomial_fold_add_piece(res, common, sum);
864 res = isl_pw_qpolynomial_fold_add_piece(res, set,
865 isl_qpolynomial_fold_copy(pw1->p[i].fold));
868 for (j = 0; j < pw2->n; ++j) {
869 set = isl_set_copy(pw2->p[j].set);
870 for (i = 0; i < pw1->n; ++i)
871 set = isl_set_subtract(set, isl_set_copy(pw1->p[i].set));
872 res = isl_pw_qpolynomial_fold_add_piece(res, set,
873 isl_qpolynomial_fold_copy(pw2->p[j].fold));
876 isl_pw_qpolynomial_fold_free(pw1);
877 isl_pw_qpolynomial_fold_free(pw2);
879 return res;
880 error:
881 isl_pw_qpolynomial_fold_free(pw1);
882 isl_pw_qpolynomial_fold_free(pw2);
883 return NULL;
886 __isl_give isl_union_pw_qpolynomial_fold *isl_union_pw_qpolynomial_fold_fold_pw_qpolynomial_fold(
887 __isl_take isl_union_pw_qpolynomial_fold *u,
888 __isl_take isl_pw_qpolynomial_fold *part)
890 uint32_t hash;
891 struct isl_hash_table_entry *entry;
893 u = isl_union_pw_qpolynomial_fold_cow(u);
895 if (!part || !u)
896 goto error;
898 isl_assert(u->dim->ctx, isl_space_match(part->dim, isl_dim_param, u->dim,
899 isl_dim_param), goto error);
901 hash = isl_space_get_hash(part->dim);
902 entry = isl_hash_table_find(u->dim->ctx, &u->table, hash,
903 &has_dim, part->dim, 1);
904 if (!entry)
905 goto error;
907 if (!entry->data)
908 entry->data = part;
909 else {
910 entry->data = isl_pw_qpolynomial_fold_fold(entry->data,
911 isl_pw_qpolynomial_fold_copy(part));
912 if (!entry->data)
913 goto error;
914 isl_pw_qpolynomial_fold_free(part);
917 return u;
918 error:
919 isl_pw_qpolynomial_fold_free(part);
920 isl_union_pw_qpolynomial_fold_free(u);
921 return NULL;
924 static int fold_part(__isl_take isl_pw_qpolynomial_fold *part, void *user)
926 isl_union_pw_qpolynomial_fold **u;
927 u = (isl_union_pw_qpolynomial_fold **)user;
929 *u = isl_union_pw_qpolynomial_fold_fold_pw_qpolynomial_fold(*u, part);
931 return 0;
934 __isl_give isl_union_pw_qpolynomial_fold *isl_union_pw_qpolynomial_fold_fold(
935 __isl_take isl_union_pw_qpolynomial_fold *u1,
936 __isl_take isl_union_pw_qpolynomial_fold *u2)
938 u1 = isl_union_pw_qpolynomial_fold_cow(u1);
940 if (!u1 || !u2)
941 goto error;
943 if (isl_union_pw_qpolynomial_fold_foreach_pw_qpolynomial_fold(u2,
944 &fold_part, &u1) < 0)
945 goto error;
947 isl_union_pw_qpolynomial_fold_free(u2);
949 return u1;
950 error:
951 isl_union_pw_qpolynomial_fold_free(u1);
952 isl_union_pw_qpolynomial_fold_free(u2);
953 return NULL;
956 __isl_give isl_pw_qpolynomial_fold *isl_pw_qpolynomial_fold_from_pw_qpolynomial(
957 enum isl_fold type, __isl_take isl_pw_qpolynomial *pwqp)
959 int i;
960 isl_pw_qpolynomial_fold *pwf;
962 if (!pwqp)
963 return NULL;
965 pwf = isl_pw_qpolynomial_fold_alloc_size(isl_space_copy(pwqp->dim),
966 type, pwqp->n);
968 for (i = 0; i < pwqp->n; ++i)
969 pwf = isl_pw_qpolynomial_fold_add_piece(pwf,
970 isl_set_copy(pwqp->p[i].set),
971 isl_qpolynomial_fold_alloc(type,
972 isl_qpolynomial_copy(pwqp->p[i].qp)));
974 isl_pw_qpolynomial_free(pwqp);
976 return pwf;
979 __isl_give isl_pw_qpolynomial_fold *isl_pw_qpolynomial_fold_add(
980 __isl_take isl_pw_qpolynomial_fold *pwf1,
981 __isl_take isl_pw_qpolynomial_fold *pwf2)
983 return isl_pw_qpolynomial_fold_union_add_(pwf1, pwf2);
986 int isl_qpolynomial_fold_plain_is_equal(__isl_keep isl_qpolynomial_fold *fold1,
987 __isl_keep isl_qpolynomial_fold *fold2)
989 int i;
991 if (!fold1 || !fold2)
992 return -1;
994 if (fold1->n != fold2->n)
995 return 0;
997 /* We probably want to sort the qps first... */
998 for (i = 0; i < fold1->n; ++i) {
999 int eq = isl_qpolynomial_plain_is_equal(fold1->qp[i], fold2->qp[i]);
1000 if (eq < 0 || !eq)
1001 return eq;
1004 return 1;
1007 __isl_give isl_qpolynomial *isl_qpolynomial_fold_eval(
1008 __isl_take isl_qpolynomial_fold *fold, __isl_take isl_point *pnt)
1010 isl_qpolynomial *qp;
1012 if (!fold || !pnt)
1013 goto error;
1014 isl_assert(pnt->dim->ctx, isl_space_is_equal(pnt->dim, fold->dim), goto error);
1015 isl_assert(pnt->dim->ctx,
1016 fold->type == isl_fold_max || fold->type == isl_fold_min,
1017 goto error);
1019 if (fold->n == 0)
1020 qp = isl_qpolynomial_zero_on_domain(isl_space_copy(fold->dim));
1021 else {
1022 int i;
1023 qp = isl_qpolynomial_eval(isl_qpolynomial_copy(fold->qp[0]),
1024 isl_point_copy(pnt));
1025 for (i = 1; i < fold->n; ++i) {
1026 isl_qpolynomial *qp_i;
1027 qp_i = isl_qpolynomial_eval(
1028 isl_qpolynomial_copy(fold->qp[i]),
1029 isl_point_copy(pnt));
1030 if (fold->type == isl_fold_max)
1031 qp = isl_qpolynomial_max_cst(qp, qp_i);
1032 else
1033 qp = isl_qpolynomial_min_cst(qp, qp_i);
1036 isl_qpolynomial_fold_free(fold);
1037 isl_point_free(pnt);
1039 return qp;
1040 error:
1041 isl_qpolynomial_fold_free(fold);
1042 isl_point_free(pnt);
1043 return NULL;
1046 size_t isl_pw_qpolynomial_fold_size(__isl_keep isl_pw_qpolynomial_fold *pwf)
1048 int i;
1049 size_t n = 0;
1051 for (i = 0; i < pwf->n; ++i)
1052 n += pwf->p[i].fold->n;
1054 return n;
1057 __isl_give isl_qpolynomial *isl_qpolynomial_fold_opt_on_domain(
1058 __isl_take isl_qpolynomial_fold *fold, __isl_take isl_set *set, int max)
1060 int i;
1061 isl_qpolynomial *opt;
1063 if (!set || !fold)
1064 goto error;
1066 if (fold->n == 0) {
1067 isl_space *dim = isl_space_copy(fold->dim);
1068 isl_set_free(set);
1069 isl_qpolynomial_fold_free(fold);
1070 return isl_qpolynomial_zero_on_domain(dim);
1073 opt = isl_qpolynomial_opt_on_domain(isl_qpolynomial_copy(fold->qp[0]),
1074 isl_set_copy(set), max);
1075 for (i = 1; i < fold->n; ++i) {
1076 isl_qpolynomial *opt_i;
1077 opt_i = isl_qpolynomial_opt_on_domain(
1078 isl_qpolynomial_copy(fold->qp[i]),
1079 isl_set_copy(set), max);
1080 if (max)
1081 opt = isl_qpolynomial_max_cst(opt, opt_i);
1082 else
1083 opt = isl_qpolynomial_min_cst(opt, opt_i);
1086 isl_set_free(set);
1087 isl_qpolynomial_fold_free(fold);
1089 return opt;
1090 error:
1091 isl_set_free(set);
1092 isl_qpolynomial_fold_free(fold);
1093 return NULL;
1096 /* Check whether for each quasi-polynomial in "fold2" there is
1097 * a quasi-polynomial in "fold1" that dominates it on "set".
1099 static int qpolynomial_fold_covers_on_domain(__isl_keep isl_set *set,
1100 __isl_keep isl_qpolynomial_fold *fold1,
1101 __isl_keep isl_qpolynomial_fold *fold2)
1103 int i, j;
1104 int covers;
1106 if (!set || !fold1 || !fold2)
1107 return -1;
1109 covers = fold1->type == isl_fold_max ? 1 : -1;
1111 for (i = 0; i < fold2->n; ++i) {
1112 for (j = 0; j < fold1->n; ++j) {
1113 isl_qpolynomial *d;
1114 int sgn;
1116 d = isl_qpolynomial_sub(
1117 isl_qpolynomial_copy(fold1->qp[j]),
1118 isl_qpolynomial_copy(fold2->qp[i]));
1119 sgn = isl_qpolynomial_sign(set, d);
1120 isl_qpolynomial_free(d);
1121 if (sgn == covers)
1122 break;
1124 if (j >= fold1->n)
1125 return 0;
1128 return 1;
1131 /* Check whether "pwf1" dominated "pwf2", i.e., the domain of "pwf1" contains
1132 * that of "pwf2" and on each cell, the corresponding fold from pwf1 dominates
1133 * that of pwf2.
1135 int isl_pw_qpolynomial_fold_covers(__isl_keep isl_pw_qpolynomial_fold *pwf1,
1136 __isl_keep isl_pw_qpolynomial_fold *pwf2)
1138 int i, j;
1139 isl_set *dom1, *dom2;
1140 int is_subset;
1142 if (!pwf1 || !pwf2)
1143 return -1;
1145 if (pwf2->n == 0)
1146 return 1;
1147 if (pwf1->n == 0)
1148 return 0;
1150 dom1 = isl_pw_qpolynomial_fold_domain(isl_pw_qpolynomial_fold_copy(pwf1));
1151 dom2 = isl_pw_qpolynomial_fold_domain(isl_pw_qpolynomial_fold_copy(pwf2));
1152 is_subset = isl_set_is_subset(dom2, dom1);
1153 isl_set_free(dom1);
1154 isl_set_free(dom2);
1156 if (is_subset < 0 || !is_subset)
1157 return is_subset;
1159 for (i = 0; i < pwf2->n; ++i) {
1160 for (j = 0; j < pwf1->n; ++j) {
1161 int is_empty;
1162 isl_set *common;
1163 int covers;
1165 common = isl_set_intersect(isl_set_copy(pwf1->p[j].set),
1166 isl_set_copy(pwf2->p[i].set));
1167 is_empty = isl_set_is_empty(common);
1168 if (is_empty < 0 || is_empty) {
1169 isl_set_free(common);
1170 if (is_empty < 0)
1171 return -1;
1172 continue;
1174 covers = qpolynomial_fold_covers_on_domain(common,
1175 pwf1->p[j].fold, pwf2->p[i].fold);
1176 isl_set_free(common);
1177 if (covers < 0 || !covers)
1178 return covers;
1182 return 1;
1185 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_morph_domain(
1186 __isl_take isl_qpolynomial_fold *fold, __isl_take isl_morph *morph)
1188 int i;
1189 isl_ctx *ctx;
1191 if (!fold || !morph)
1192 goto error;
1194 ctx = fold->dim->ctx;
1195 isl_assert(ctx, isl_space_is_equal(fold->dim, morph->dom->dim), goto error);
1197 fold = isl_qpolynomial_fold_cow(fold);
1198 if (!fold)
1199 goto error;
1201 isl_space_free(fold->dim);
1202 fold->dim = isl_space_copy(morph->ran->dim);
1203 if (!fold->dim)
1204 goto error;
1206 for (i = 0; i < fold->n; ++i) {
1207 fold->qp[i] = isl_qpolynomial_morph_domain(fold->qp[i],
1208 isl_morph_copy(morph));
1209 if (!fold->qp[i])
1210 goto error;
1213 isl_morph_free(morph);
1215 return fold;
1216 error:
1217 isl_qpolynomial_fold_free(fold);
1218 isl_morph_free(morph);
1219 return NULL;
1222 enum isl_fold isl_qpolynomial_fold_get_type(__isl_keep isl_qpolynomial_fold *fold)
1224 if (!fold)
1225 return isl_fold_list;
1226 return fold->type;
1229 enum isl_fold isl_union_pw_qpolynomial_fold_get_type(
1230 __isl_keep isl_union_pw_qpolynomial_fold *upwf)
1232 if (!upwf)
1233 return isl_fold_list;
1234 return upwf->type;
1237 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_lift(
1238 __isl_take isl_qpolynomial_fold *fold, __isl_take isl_space *dim)
1240 int i;
1242 if (!fold || !dim)
1243 goto error;
1245 if (isl_space_is_equal(fold->dim, dim)) {
1246 isl_space_free(dim);
1247 return fold;
1250 fold = isl_qpolynomial_fold_cow(fold);
1251 if (!fold)
1252 goto error;
1254 isl_space_free(fold->dim);
1255 fold->dim = isl_space_copy(dim);
1256 if (!fold->dim)
1257 goto error;
1259 for (i = 0; i < fold->n; ++i) {
1260 fold->qp[i] = isl_qpolynomial_lift(fold->qp[i],
1261 isl_space_copy(dim));
1262 if (!fold->qp[i])
1263 goto error;
1266 isl_space_free(dim);
1268 return fold;
1269 error:
1270 isl_qpolynomial_fold_free(fold);
1271 isl_space_free(dim);
1272 return NULL;
1275 int isl_qpolynomial_fold_foreach_qpolynomial(
1276 __isl_keep isl_qpolynomial_fold *fold,
1277 int (*fn)(__isl_take isl_qpolynomial *qp, void *user), void *user)
1279 int i;
1281 if (!fold)
1282 return -1;
1284 for (i = 0; i < fold->n; ++i)
1285 if (fn(isl_qpolynomial_copy(fold->qp[i]), user) < 0)
1286 return -1;
1288 return 0;
1291 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_move_dims(
1292 __isl_take isl_qpolynomial_fold *fold,
1293 enum isl_dim_type dst_type, unsigned dst_pos,
1294 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
1296 int i;
1298 if (n == 0)
1299 return fold;
1301 fold = isl_qpolynomial_fold_cow(fold);
1302 if (!fold)
1303 return NULL;
1305 fold->dim = isl_space_move_dims(fold->dim, dst_type, dst_pos,
1306 src_type, src_pos, n);
1307 if (!fold->dim)
1308 goto error;
1310 for (i = 0; i < fold->n; ++i) {
1311 fold->qp[i] = isl_qpolynomial_move_dims(fold->qp[i],
1312 dst_type, dst_pos, src_type, src_pos, n);
1313 if (!fold->qp[i])
1314 goto error;
1317 return fold;
1318 error:
1319 isl_qpolynomial_fold_free(fold);
1320 return NULL;
1323 /* For each 0 <= i < "n", replace variable "first" + i of type "type"
1324 * in fold->qp[k] by subs[i].
1326 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_substitute(
1327 __isl_take isl_qpolynomial_fold *fold,
1328 enum isl_dim_type type, unsigned first, unsigned n,
1329 __isl_keep isl_qpolynomial **subs)
1331 int i;
1333 if (n == 0)
1334 return fold;
1336 fold = isl_qpolynomial_fold_cow(fold);
1337 if (!fold)
1338 return NULL;
1340 for (i = 0; i < fold->n; ++i) {
1341 fold->qp[i] = isl_qpolynomial_substitute(fold->qp[i],
1342 type, first, n, subs);
1343 if (!fold->qp[i])
1344 goto error;
1347 return fold;
1348 error:
1349 isl_qpolynomial_fold_free(fold);
1350 return NULL;
1353 static int add_pwqp(__isl_take isl_pw_qpolynomial *pwqp, void *user)
1355 isl_ctx *ctx;
1356 isl_pw_qpolynomial_fold *pwf;
1357 isl_union_pw_qpolynomial_fold **upwf;
1358 uint32_t hash;
1359 struct isl_hash_table_entry *entry;
1361 upwf = (isl_union_pw_qpolynomial_fold **)user;
1363 ctx = pwqp->dim->ctx;
1364 hash = isl_space_get_hash(pwqp->dim);
1365 entry = isl_hash_table_find(ctx, &(*upwf)->table,
1366 hash, &has_dim, pwqp->dim, 1);
1367 if (!entry)
1368 goto error;
1370 pwf = isl_pw_qpolynomial_fold_from_pw_qpolynomial((*upwf)->type, pwqp);
1371 if (!entry->data)
1372 entry->data = pwf;
1373 else {
1374 entry->data = isl_pw_qpolynomial_fold_add(entry->data, pwf);
1375 if (!entry->data)
1376 return -1;
1377 if (isl_pw_qpolynomial_fold_is_zero(entry->data)) {
1378 isl_pw_qpolynomial_fold_free(entry->data);
1379 isl_hash_table_remove(ctx, &(*upwf)->table, entry);
1383 return 0;
1384 error:
1385 isl_pw_qpolynomial_free(pwqp);
1386 return -1;
1389 __isl_give isl_union_pw_qpolynomial_fold *isl_union_pw_qpolynomial_fold_add_union_pw_qpolynomial(
1390 __isl_take isl_union_pw_qpolynomial_fold *upwf,
1391 __isl_take isl_union_pw_qpolynomial *upwqp)
1393 upwf = isl_union_pw_qpolynomial_fold_align_params(upwf,
1394 isl_union_pw_qpolynomial_get_space(upwqp));
1395 upwqp = isl_union_pw_qpolynomial_align_params(upwqp,
1396 isl_union_pw_qpolynomial_fold_get_space(upwf));
1398 upwf = isl_union_pw_qpolynomial_fold_cow(upwf);
1399 if (!upwf || !upwqp)
1400 goto error;
1402 if (isl_union_pw_qpolynomial_foreach_pw_qpolynomial(upwqp, &add_pwqp,
1403 &upwf) < 0)
1404 goto error;
1406 isl_union_pw_qpolynomial_free(upwqp);
1408 return upwf;
1409 error:
1410 isl_union_pw_qpolynomial_fold_free(upwf);
1411 isl_union_pw_qpolynomial_free(upwqp);
1412 return NULL;
1415 static int join_compatible(__isl_keep isl_space *dim1, __isl_keep isl_space *dim2)
1417 int m;
1418 m = isl_space_match(dim1, isl_dim_param, dim2, isl_dim_param);
1419 if (m < 0 || !m)
1420 return m;
1421 return isl_space_tuple_match(dim1, isl_dim_out, dim2, isl_dim_in);
1424 /* Compute the intersection of the range of the map and the domain
1425 * of the piecewise quasipolynomial reduction and then compute a bound
1426 * on the associated quasipolynomial reduction over all elements
1427 * in this intersection.
1429 * We first introduce some unconstrained dimensions in the
1430 * piecewise quasipolynomial, intersect the resulting domain
1431 * with the wrapped map and the compute the sum.
1433 __isl_give isl_pw_qpolynomial_fold *isl_map_apply_pw_qpolynomial_fold(
1434 __isl_take isl_map *map, __isl_take isl_pw_qpolynomial_fold *pwf,
1435 int *tight)
1437 isl_ctx *ctx;
1438 isl_set *dom;
1439 isl_space *map_dim;
1440 isl_space *pwf_dim;
1441 unsigned n_in;
1442 int ok;
1444 ctx = isl_map_get_ctx(map);
1445 if (!ctx)
1446 goto error;
1448 map_dim = isl_map_get_space(map);
1449 pwf_dim = isl_pw_qpolynomial_fold_get_space(pwf);
1450 ok = join_compatible(map_dim, pwf_dim);
1451 isl_space_free(map_dim);
1452 isl_space_free(pwf_dim);
1453 if (!ok)
1454 isl_die(ctx, isl_error_invalid, "incompatible dimensions",
1455 goto error);
1457 n_in = isl_map_dim(map, isl_dim_in);
1458 pwf = isl_pw_qpolynomial_fold_insert_dims(pwf, isl_dim_in, 0, n_in);
1460 dom = isl_map_wrap(map);
1461 pwf = isl_pw_qpolynomial_fold_reset_domain_space(pwf,
1462 isl_set_get_space(dom));
1464 pwf = isl_pw_qpolynomial_fold_intersect_domain(pwf, dom);
1465 pwf = isl_pw_qpolynomial_fold_bound(pwf, tight);
1467 return pwf;
1468 error:
1469 isl_map_free(map);
1470 isl_pw_qpolynomial_fold_free(pwf);
1471 return NULL;
1474 __isl_give isl_pw_qpolynomial_fold *isl_set_apply_pw_qpolynomial_fold(
1475 __isl_take isl_set *set, __isl_take isl_pw_qpolynomial_fold *pwf,
1476 int *tight)
1478 return isl_map_apply_pw_qpolynomial_fold(set, pwf, tight);
1481 struct isl_apply_fold_data {
1482 isl_union_pw_qpolynomial_fold *upwf;
1483 isl_union_pw_qpolynomial_fold *res;
1484 isl_map *map;
1485 int tight;
1488 static int pw_qpolynomial_fold_apply(__isl_take isl_pw_qpolynomial_fold *pwf,
1489 void *user)
1491 isl_space *map_dim;
1492 isl_space *pwf_dim;
1493 struct isl_apply_fold_data *data = user;
1494 int ok;
1496 map_dim = isl_map_get_space(data->map);
1497 pwf_dim = isl_pw_qpolynomial_fold_get_space(pwf);
1498 ok = join_compatible(map_dim, pwf_dim);
1499 isl_space_free(map_dim);
1500 isl_space_free(pwf_dim);
1502 if (ok) {
1503 pwf = isl_map_apply_pw_qpolynomial_fold(isl_map_copy(data->map),
1504 pwf, data->tight ? &data->tight : NULL);
1505 data->res = isl_union_pw_qpolynomial_fold_fold_pw_qpolynomial_fold(
1506 data->res, pwf);
1507 } else
1508 isl_pw_qpolynomial_fold_free(pwf);
1510 return 0;
1513 static int map_apply(__isl_take isl_map *map, void *user)
1515 struct isl_apply_fold_data *data = user;
1516 int r;
1518 data->map = map;
1519 r = isl_union_pw_qpolynomial_fold_foreach_pw_qpolynomial_fold(
1520 data->upwf, &pw_qpolynomial_fold_apply, data);
1522 isl_map_free(map);
1523 return r;
1526 __isl_give isl_union_pw_qpolynomial_fold *isl_union_map_apply_union_pw_qpolynomial_fold(
1527 __isl_take isl_union_map *umap,
1528 __isl_take isl_union_pw_qpolynomial_fold *upwf, int *tight)
1530 isl_space *dim;
1531 enum isl_fold type;
1532 struct isl_apply_fold_data data;
1534 upwf = isl_union_pw_qpolynomial_fold_align_params(upwf,
1535 isl_union_map_get_space(umap));
1536 umap = isl_union_map_align_params(umap,
1537 isl_union_pw_qpolynomial_fold_get_space(upwf));
1539 data.upwf = upwf;
1540 data.tight = tight ? 1 : 0;
1541 dim = isl_union_pw_qpolynomial_fold_get_space(upwf);
1542 type = isl_union_pw_qpolynomial_fold_get_type(upwf);
1543 data.res = isl_union_pw_qpolynomial_fold_zero(dim, type);
1544 if (isl_union_map_foreach_map(umap, &map_apply, &data) < 0)
1545 goto error;
1547 isl_union_map_free(umap);
1548 isl_union_pw_qpolynomial_fold_free(upwf);
1550 if (tight)
1551 *tight = data.tight;
1553 return data.res;
1554 error:
1555 isl_union_map_free(umap);
1556 isl_union_pw_qpolynomial_fold_free(upwf);
1557 isl_union_pw_qpolynomial_fold_free(data.res);
1558 return NULL;
1561 __isl_give isl_union_pw_qpolynomial_fold *isl_union_set_apply_union_pw_qpolynomial_fold(
1562 __isl_take isl_union_set *uset,
1563 __isl_take isl_union_pw_qpolynomial_fold *upwf, int *tight)
1565 return isl_union_map_apply_union_pw_qpolynomial_fold(uset, upwf, tight);
1568 /* Reorder the dimension of "fold" according to the given reordering.
1570 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_realign_domain(
1571 __isl_take isl_qpolynomial_fold *fold, __isl_take isl_reordering *r)
1573 int i;
1575 fold = isl_qpolynomial_fold_cow(fold);
1576 if (!fold || !r)
1577 goto error;
1579 for (i = 0; i < fold->n; ++i) {
1580 fold->qp[i] = isl_qpolynomial_realign_domain(fold->qp[i],
1581 isl_reordering_copy(r));
1582 if (!fold->qp[i])
1583 goto error;
1586 fold = isl_qpolynomial_fold_reset_domain_space(fold,
1587 isl_space_copy(r->dim));
1589 isl_reordering_free(r);
1591 return fold;
1592 error:
1593 isl_qpolynomial_fold_free(fold);
1594 isl_reordering_free(r);
1595 return NULL;
1598 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_mul_isl_int(
1599 __isl_take isl_qpolynomial_fold *fold, isl_int v)
1601 int i;
1603 if (isl_int_is_one(v))
1604 return fold;
1605 if (fold && isl_int_is_zero(v)) {
1606 isl_qpolynomial_fold *zero;
1607 isl_space *dim = isl_space_copy(fold->dim);
1608 zero = isl_qpolynomial_fold_empty(fold->type, dim);
1609 isl_qpolynomial_fold_free(fold);
1610 return zero;
1613 fold = isl_qpolynomial_fold_cow(fold);
1614 if (!fold)
1615 return NULL;
1617 if (isl_int_is_neg(v))
1618 fold->type = isl_fold_type_negate(fold->type);
1619 for (i = 0; i < fold->n; ++i) {
1620 fold->qp[i] = isl_qpolynomial_mul_isl_int(fold->qp[i], v);
1621 if (!fold->qp[i])
1622 goto error;
1625 return fold;
1626 error:
1627 isl_qpolynomial_fold_free(fold);
1628 return NULL;
1631 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_scale(
1632 __isl_take isl_qpolynomial_fold *fold, isl_int v)
1634 return isl_qpolynomial_fold_mul_isl_int(fold, v);
1637 /* Multiply "fold" by "v".
1639 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_scale_val(
1640 __isl_take isl_qpolynomial_fold *fold, __isl_take isl_val *v)
1642 int i;
1644 if (!fold || !v)
1645 goto error;
1647 if (isl_val_is_one(v)) {
1648 isl_val_free(v);
1649 return fold;
1651 if (isl_val_is_zero(v)) {
1652 isl_qpolynomial_fold *zero;
1653 isl_space *space = isl_qpolynomial_fold_get_domain_space(fold);
1654 zero = isl_qpolynomial_fold_empty(fold->type, space);
1655 isl_qpolynomial_fold_free(fold);
1656 isl_val_free(v);
1657 return zero;
1659 if (!isl_val_is_rat(v))
1660 isl_die(isl_qpolynomial_fold_get_ctx(fold), isl_error_invalid,
1661 "expecting rational factor", goto error);
1663 fold = isl_qpolynomial_fold_cow(fold);
1664 if (!fold)
1665 goto error;
1667 if (isl_val_is_neg(v))
1668 fold->type = isl_fold_type_negate(fold->type);
1669 for (i = 0; i < fold->n; ++i) {
1670 fold->qp[i] = isl_qpolynomial_scale_val(fold->qp[i],
1671 isl_val_copy(v));
1672 if (!fold->qp[i])
1673 goto error;
1676 isl_val_free(v);
1677 return fold;
1678 error:
1679 isl_val_free(v);
1680 isl_qpolynomial_fold_free(fold);
1681 return NULL;