isl_qpolynomial_fold_fold_on_domain: eliminate obviously equal entries
[isl.git] / isl_fold.c
blob25e784cd674b4d50fb52d999fb9a7ba679e5409b
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, equal;
442 equal = isl_qpolynomial_plain_is_equal(res->qp[j],
443 fold2->qp[i]);
444 if (equal < 0)
445 goto error;
446 if (equal)
447 break;
448 d = isl_qpolynomial_sub(
449 isl_qpolynomial_copy(res->qp[j]),
450 isl_qpolynomial_copy(fold2->qp[i]));
451 sgn = isl_qpolynomial_sign(set, d);
452 isl_qpolynomial_free(d);
453 if (sgn == 0)
454 continue;
455 if (sgn != better)
456 break;
457 isl_qpolynomial_free(res->qp[j]);
458 if (j != n1 - 1)
459 res->qp[j] = res->qp[n1 - 1];
460 n1--;
461 if (n1 != res->n - 1)
462 res->qp[n1] = res->qp[res->n - 1];
463 res->n--;
465 if (j >= 0)
466 continue;
467 res->qp[res->n] = isl_qpolynomial_copy(fold2->qp[i]);
468 if (!res->qp[res->n])
469 goto error;
470 res->n++;
473 isl_qpolynomial_fold_free(fold1);
474 isl_qpolynomial_fold_free(fold2);
476 return res;
477 error:
478 isl_qpolynomial_fold_free(res);
479 isl_qpolynomial_fold_free(fold1);
480 isl_qpolynomial_fold_free(fold2);
481 return NULL;
484 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_add_qpolynomial(
485 __isl_take isl_qpolynomial_fold *fold, __isl_take isl_qpolynomial *qp)
487 int i;
489 if (!fold || !qp)
490 goto error;
492 if (isl_qpolynomial_is_zero(qp)) {
493 isl_qpolynomial_free(qp);
494 return fold;
497 fold = isl_qpolynomial_fold_cow(fold);
498 if (!fold)
499 goto error;
501 for (i = 0; i < fold->n; ++i) {
502 fold->qp[i] = isl_qpolynomial_add(fold->qp[i],
503 isl_qpolynomial_copy(qp));
504 if (!fold->qp[i])
505 goto error;
508 isl_qpolynomial_free(qp);
509 return fold;
510 error:
511 isl_qpolynomial_fold_free(fold);
512 isl_qpolynomial_free(qp);
513 return NULL;
516 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_add_on_domain(
517 __isl_keep isl_set *dom,
518 __isl_take isl_qpolynomial_fold *fold1,
519 __isl_take isl_qpolynomial_fold *fold2)
521 int i;
522 isl_qpolynomial_fold *res = NULL;
524 if (!fold1 || !fold2)
525 goto error;
527 if (isl_qpolynomial_fold_is_empty(fold1)) {
528 isl_qpolynomial_fold_free(fold1);
529 return fold2;
532 if (isl_qpolynomial_fold_is_empty(fold2)) {
533 isl_qpolynomial_fold_free(fold2);
534 return fold1;
537 if (fold1->n == 1 && fold2->n != 1)
538 return isl_qpolynomial_fold_add_on_domain(dom, fold2, fold1);
540 if (fold2->n == 1) {
541 res = isl_qpolynomial_fold_add_qpolynomial(fold1,
542 isl_qpolynomial_copy(fold2->qp[0]));
543 isl_qpolynomial_fold_free(fold2);
544 return res;
547 res = isl_qpolynomial_fold_add_qpolynomial(
548 isl_qpolynomial_fold_copy(fold1),
549 isl_qpolynomial_copy(fold2->qp[0]));
551 for (i = 1; i < fold2->n; ++i) {
552 isl_qpolynomial_fold *res_i;
553 res_i = isl_qpolynomial_fold_add_qpolynomial(
554 isl_qpolynomial_fold_copy(fold1),
555 isl_qpolynomial_copy(fold2->qp[i]));
556 res = isl_qpolynomial_fold_fold_on_domain(dom, res, res_i);
559 isl_qpolynomial_fold_free(fold1);
560 isl_qpolynomial_fold_free(fold2);
561 return res;
562 error:
563 isl_qpolynomial_fold_free(res);
564 isl_qpolynomial_fold_free(fold1);
565 isl_qpolynomial_fold_free(fold2);
566 return NULL;
569 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_substitute_equalities(
570 __isl_take isl_qpolynomial_fold *fold, __isl_take isl_basic_set *eq)
572 int i;
574 if (!fold || !eq)
575 goto error;
577 fold = isl_qpolynomial_fold_cow(fold);
578 if (!fold)
579 return NULL;
581 for (i = 0; i < fold->n; ++i) {
582 fold->qp[i] = isl_qpolynomial_substitute_equalities(fold->qp[i],
583 isl_basic_set_copy(eq));
584 if (!fold->qp[i])
585 goto error;
588 isl_basic_set_free(eq);
589 return fold;
590 error:
591 isl_basic_set_free(eq);
592 isl_qpolynomial_fold_free(fold);
593 return NULL;
596 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_gist(
597 __isl_take isl_qpolynomial_fold *fold, __isl_take isl_set *context)
599 int i;
601 if (!fold || !context)
602 goto error;
604 fold = isl_qpolynomial_fold_cow(fold);
605 if (!fold)
606 return NULL;
608 for (i = 0; i < fold->n; ++i) {
609 fold->qp[i] = isl_qpolynomial_gist(fold->qp[i],
610 isl_set_copy(context));
611 if (!fold->qp[i])
612 goto error;
615 isl_set_free(context);
616 return fold;
617 error:
618 isl_set_free(context);
619 isl_qpolynomial_fold_free(fold);
620 return NULL;
623 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_gist_params(
624 __isl_take isl_qpolynomial_fold *fold, __isl_take isl_set *context)
626 isl_space *space = isl_qpolynomial_fold_get_domain_space(fold);
627 isl_set *dom_context = isl_set_universe(space);
628 dom_context = isl_set_intersect_params(dom_context, context);
629 return isl_qpolynomial_fold_gist(fold, dom_context);
632 #define HAS_TYPE
634 #undef PW
635 #define PW isl_pw_qpolynomial_fold
636 #undef EL
637 #define EL isl_qpolynomial_fold
638 #undef EL_IS_ZERO
639 #define EL_IS_ZERO is_empty
640 #undef ZERO
641 #define ZERO zero
642 #undef IS_ZERO
643 #define IS_ZERO is_zero
644 #undef FIELD
645 #define FIELD fold
646 #undef DEFAULT_IS_ZERO
647 #define DEFAULT_IS_ZERO 1
649 #define NO_NEG
650 #define NO_PULLBACK
652 #include <isl_pw_templ.c>
654 #undef UNION
655 #define UNION isl_union_pw_qpolynomial_fold
656 #undef PART
657 #define PART isl_pw_qpolynomial_fold
658 #undef PARTS
659 #define PARTS pw_qpolynomial_fold
660 #define ALIGN_DOMAIN
662 #define NO_SUB
664 #include <isl_union_templ.c>
666 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_empty(enum isl_fold type,
667 __isl_take isl_space *dim)
669 return qpolynomial_fold_alloc(type, dim, 0);
672 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_alloc(
673 enum isl_fold type, __isl_take isl_qpolynomial *qp)
675 isl_qpolynomial_fold *fold;
677 if (!qp)
678 return NULL;
680 fold = qpolynomial_fold_alloc(type, isl_space_copy(qp->dim), 1);
681 if (!fold)
682 goto error;
684 fold->qp[0] = qp;
685 fold->n++;
687 return fold;
688 error:
689 isl_qpolynomial_fold_free(fold);
690 isl_qpolynomial_free(qp);
691 return NULL;
694 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_copy(
695 __isl_keep isl_qpolynomial_fold *fold)
697 if (!fold)
698 return NULL;
700 fold->ref++;
701 return fold;
704 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_dup(
705 __isl_keep isl_qpolynomial_fold *fold)
707 int i;
708 isl_qpolynomial_fold *dup;
710 if (!fold)
711 return NULL;
712 dup = qpolynomial_fold_alloc(fold->type,
713 isl_space_copy(fold->dim), fold->n);
714 if (!dup)
715 return NULL;
717 dup->n = fold->n;
718 for (i = 0; i < fold->n; ++i) {
719 dup->qp[i] = isl_qpolynomial_copy(fold->qp[i]);
720 if (!dup->qp[i])
721 goto error;
724 return dup;
725 error:
726 isl_qpolynomial_fold_free(dup);
727 return NULL;
730 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_cow(
731 __isl_take isl_qpolynomial_fold *fold)
733 if (!fold)
734 return NULL;
736 if (fold->ref == 1)
737 return fold;
738 fold->ref--;
739 return isl_qpolynomial_fold_dup(fold);
742 void isl_qpolynomial_fold_free(__isl_take isl_qpolynomial_fold *fold)
744 int i;
746 if (!fold)
747 return;
748 if (--fold->ref > 0)
749 return;
751 for (i = 0; i < fold->n; ++i)
752 isl_qpolynomial_free(fold->qp[i]);
753 isl_space_free(fold->dim);
754 free(fold);
757 int isl_qpolynomial_fold_is_empty(__isl_keep isl_qpolynomial_fold *fold)
759 if (!fold)
760 return -1;
762 return fold->n == 0;
765 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_fold(
766 __isl_take isl_qpolynomial_fold *fold1,
767 __isl_take isl_qpolynomial_fold *fold2)
769 int i;
770 struct isl_qpolynomial_fold *res = NULL;
772 if (!fold1 || !fold2)
773 goto error;
775 isl_assert(fold1->dim->ctx, fold1->type == fold2->type, goto error);
776 isl_assert(fold1->dim->ctx, isl_space_is_equal(fold1->dim, fold2->dim),
777 goto error);
779 if (isl_qpolynomial_fold_is_empty(fold1)) {
780 isl_qpolynomial_fold_free(fold1);
781 return fold2;
784 if (isl_qpolynomial_fold_is_empty(fold2)) {
785 isl_qpolynomial_fold_free(fold2);
786 return fold1;
789 res = qpolynomial_fold_alloc(fold1->type, isl_space_copy(fold1->dim),
790 fold1->n + fold2->n);
791 if (!res)
792 goto error;
794 for (i = 0; i < fold1->n; ++i) {
795 res->qp[res->n] = isl_qpolynomial_copy(fold1->qp[i]);
796 if (!res->qp[res->n])
797 goto error;
798 res->n++;
801 for (i = 0; i < fold2->n; ++i) {
802 res->qp[res->n] = isl_qpolynomial_copy(fold2->qp[i]);
803 if (!res->qp[res->n])
804 goto error;
805 res->n++;
808 isl_qpolynomial_fold_free(fold1);
809 isl_qpolynomial_fold_free(fold2);
811 return res;
812 error:
813 isl_qpolynomial_fold_free(res);
814 isl_qpolynomial_fold_free(fold1);
815 isl_qpolynomial_fold_free(fold2);
816 return NULL;
819 __isl_give isl_pw_qpolynomial_fold *isl_pw_qpolynomial_fold_fold(
820 __isl_take isl_pw_qpolynomial_fold *pw1,
821 __isl_take isl_pw_qpolynomial_fold *pw2)
823 int i, j, n;
824 struct isl_pw_qpolynomial_fold *res;
825 isl_set *set;
827 if (!pw1 || !pw2)
828 goto error;
830 isl_assert(pw1->dim->ctx, isl_space_is_equal(pw1->dim, pw2->dim), goto error);
832 if (isl_pw_qpolynomial_fold_is_zero(pw1)) {
833 isl_pw_qpolynomial_fold_free(pw1);
834 return pw2;
837 if (isl_pw_qpolynomial_fold_is_zero(pw2)) {
838 isl_pw_qpolynomial_fold_free(pw2);
839 return pw1;
842 if (pw1->type != pw2->type)
843 isl_die(pw1->dim->ctx, isl_error_invalid,
844 "fold types don't match", goto error);
846 n = (pw1->n + 1) * (pw2->n + 1);
847 res = isl_pw_qpolynomial_fold_alloc_size(isl_space_copy(pw1->dim),
848 pw1->type, n);
850 for (i = 0; i < pw1->n; ++i) {
851 set = isl_set_copy(pw1->p[i].set);
852 for (j = 0; j < pw2->n; ++j) {
853 struct isl_set *common;
854 isl_qpolynomial_fold *sum;
855 set = isl_set_subtract(set,
856 isl_set_copy(pw2->p[j].set));
857 common = isl_set_intersect(isl_set_copy(pw1->p[i].set),
858 isl_set_copy(pw2->p[j].set));
859 if (isl_set_plain_is_empty(common)) {
860 isl_set_free(common);
861 continue;
864 sum = isl_qpolynomial_fold_fold_on_domain(common,
865 isl_qpolynomial_fold_copy(pw1->p[i].fold),
866 isl_qpolynomial_fold_copy(pw2->p[j].fold));
868 res = isl_pw_qpolynomial_fold_add_piece(res, common, sum);
870 res = isl_pw_qpolynomial_fold_add_piece(res, set,
871 isl_qpolynomial_fold_copy(pw1->p[i].fold));
874 for (j = 0; j < pw2->n; ++j) {
875 set = isl_set_copy(pw2->p[j].set);
876 for (i = 0; i < pw1->n; ++i)
877 set = isl_set_subtract(set, isl_set_copy(pw1->p[i].set));
878 res = isl_pw_qpolynomial_fold_add_piece(res, set,
879 isl_qpolynomial_fold_copy(pw2->p[j].fold));
882 isl_pw_qpolynomial_fold_free(pw1);
883 isl_pw_qpolynomial_fold_free(pw2);
885 return res;
886 error:
887 isl_pw_qpolynomial_fold_free(pw1);
888 isl_pw_qpolynomial_fold_free(pw2);
889 return NULL;
892 __isl_give isl_union_pw_qpolynomial_fold *isl_union_pw_qpolynomial_fold_fold_pw_qpolynomial_fold(
893 __isl_take isl_union_pw_qpolynomial_fold *u,
894 __isl_take isl_pw_qpolynomial_fold *part)
896 uint32_t hash;
897 struct isl_hash_table_entry *entry;
899 u = isl_union_pw_qpolynomial_fold_cow(u);
901 if (!part || !u)
902 goto error;
904 isl_assert(u->dim->ctx, isl_space_match(part->dim, isl_dim_param, u->dim,
905 isl_dim_param), goto error);
907 hash = isl_space_get_hash(part->dim);
908 entry = isl_hash_table_find(u->dim->ctx, &u->table, hash,
909 &has_dim, part->dim, 1);
910 if (!entry)
911 goto error;
913 if (!entry->data)
914 entry->data = part;
915 else {
916 entry->data = isl_pw_qpolynomial_fold_fold(entry->data,
917 isl_pw_qpolynomial_fold_copy(part));
918 if (!entry->data)
919 goto error;
920 isl_pw_qpolynomial_fold_free(part);
923 return u;
924 error:
925 isl_pw_qpolynomial_fold_free(part);
926 isl_union_pw_qpolynomial_fold_free(u);
927 return NULL;
930 static int fold_part(__isl_take isl_pw_qpolynomial_fold *part, void *user)
932 isl_union_pw_qpolynomial_fold **u;
933 u = (isl_union_pw_qpolynomial_fold **)user;
935 *u = isl_union_pw_qpolynomial_fold_fold_pw_qpolynomial_fold(*u, part);
937 return 0;
940 __isl_give isl_union_pw_qpolynomial_fold *isl_union_pw_qpolynomial_fold_fold(
941 __isl_take isl_union_pw_qpolynomial_fold *u1,
942 __isl_take isl_union_pw_qpolynomial_fold *u2)
944 u1 = isl_union_pw_qpolynomial_fold_cow(u1);
946 if (!u1 || !u2)
947 goto error;
949 if (isl_union_pw_qpolynomial_fold_foreach_pw_qpolynomial_fold(u2,
950 &fold_part, &u1) < 0)
951 goto error;
953 isl_union_pw_qpolynomial_fold_free(u2);
955 return u1;
956 error:
957 isl_union_pw_qpolynomial_fold_free(u1);
958 isl_union_pw_qpolynomial_fold_free(u2);
959 return NULL;
962 __isl_give isl_pw_qpolynomial_fold *isl_pw_qpolynomial_fold_from_pw_qpolynomial(
963 enum isl_fold type, __isl_take isl_pw_qpolynomial *pwqp)
965 int i;
966 isl_pw_qpolynomial_fold *pwf;
968 if (!pwqp)
969 return NULL;
971 pwf = isl_pw_qpolynomial_fold_alloc_size(isl_space_copy(pwqp->dim),
972 type, pwqp->n);
974 for (i = 0; i < pwqp->n; ++i)
975 pwf = isl_pw_qpolynomial_fold_add_piece(pwf,
976 isl_set_copy(pwqp->p[i].set),
977 isl_qpolynomial_fold_alloc(type,
978 isl_qpolynomial_copy(pwqp->p[i].qp)));
980 isl_pw_qpolynomial_free(pwqp);
982 return pwf;
985 __isl_give isl_pw_qpolynomial_fold *isl_pw_qpolynomial_fold_add(
986 __isl_take isl_pw_qpolynomial_fold *pwf1,
987 __isl_take isl_pw_qpolynomial_fold *pwf2)
989 return isl_pw_qpolynomial_fold_union_add_(pwf1, pwf2);
992 int isl_qpolynomial_fold_plain_is_equal(__isl_keep isl_qpolynomial_fold *fold1,
993 __isl_keep isl_qpolynomial_fold *fold2)
995 int i;
997 if (!fold1 || !fold2)
998 return -1;
1000 if (fold1->n != fold2->n)
1001 return 0;
1003 /* We probably want to sort the qps first... */
1004 for (i = 0; i < fold1->n; ++i) {
1005 int eq = isl_qpolynomial_plain_is_equal(fold1->qp[i], fold2->qp[i]);
1006 if (eq < 0 || !eq)
1007 return eq;
1010 return 1;
1013 __isl_give isl_val *isl_qpolynomial_fold_eval(
1014 __isl_take isl_qpolynomial_fold *fold, __isl_take isl_point *pnt)
1016 isl_ctx *ctx;
1017 isl_val *v;
1019 if (!fold || !pnt)
1020 goto error;
1021 ctx = isl_point_get_ctx(pnt);
1022 isl_assert(pnt->dim->ctx, isl_space_is_equal(pnt->dim, fold->dim), goto error);
1023 isl_assert(pnt->dim->ctx,
1024 fold->type == isl_fold_max || fold->type == isl_fold_min,
1025 goto error);
1027 if (fold->n == 0)
1028 v = isl_val_zero(ctx);
1029 else {
1030 int i;
1031 v = isl_qpolynomial_eval(isl_qpolynomial_copy(fold->qp[0]),
1032 isl_point_copy(pnt));
1033 for (i = 1; i < fold->n; ++i) {
1034 isl_val *v_i;
1035 v_i = isl_qpolynomial_eval(
1036 isl_qpolynomial_copy(fold->qp[i]),
1037 isl_point_copy(pnt));
1038 if (fold->type == isl_fold_max)
1039 v = isl_val_max(v, v_i);
1040 else
1041 v = isl_val_min(v, v_i);
1044 isl_qpolynomial_fold_free(fold);
1045 isl_point_free(pnt);
1047 return v;
1048 error:
1049 isl_qpolynomial_fold_free(fold);
1050 isl_point_free(pnt);
1051 return NULL;
1054 size_t isl_pw_qpolynomial_fold_size(__isl_keep isl_pw_qpolynomial_fold *pwf)
1056 int i;
1057 size_t n = 0;
1059 for (i = 0; i < pwf->n; ++i)
1060 n += pwf->p[i].fold->n;
1062 return n;
1065 __isl_give isl_val *isl_qpolynomial_fold_opt_on_domain(
1066 __isl_take isl_qpolynomial_fold *fold, __isl_take isl_set *set, int max)
1068 int i;
1069 isl_val *opt;
1071 if (!set || !fold)
1072 goto error;
1074 if (fold->n == 0) {
1075 opt = isl_val_zero(isl_set_get_ctx(set));
1076 isl_set_free(set);
1077 isl_qpolynomial_fold_free(fold);
1078 return opt;
1081 opt = isl_qpolynomial_opt_on_domain(isl_qpolynomial_copy(fold->qp[0]),
1082 isl_set_copy(set), max);
1083 for (i = 1; i < fold->n; ++i) {
1084 isl_val *opt_i;
1085 opt_i = isl_qpolynomial_opt_on_domain(
1086 isl_qpolynomial_copy(fold->qp[i]),
1087 isl_set_copy(set), max);
1088 if (max)
1089 opt = isl_val_max(opt, opt_i);
1090 else
1091 opt = isl_val_min(opt, opt_i);
1094 isl_set_free(set);
1095 isl_qpolynomial_fold_free(fold);
1097 return opt;
1098 error:
1099 isl_set_free(set);
1100 isl_qpolynomial_fold_free(fold);
1101 return NULL;
1104 /* Check whether for each quasi-polynomial in "fold2" there is
1105 * a quasi-polynomial in "fold1" that dominates it on "set".
1107 static int qpolynomial_fold_covers_on_domain(__isl_keep isl_set *set,
1108 __isl_keep isl_qpolynomial_fold *fold1,
1109 __isl_keep isl_qpolynomial_fold *fold2)
1111 int i, j;
1112 int covers;
1114 if (!set || !fold1 || !fold2)
1115 return -1;
1117 covers = fold1->type == isl_fold_max ? 1 : -1;
1119 for (i = 0; i < fold2->n; ++i) {
1120 for (j = 0; j < fold1->n; ++j) {
1121 isl_qpolynomial *d;
1122 int sgn;
1124 d = isl_qpolynomial_sub(
1125 isl_qpolynomial_copy(fold1->qp[j]),
1126 isl_qpolynomial_copy(fold2->qp[i]));
1127 sgn = isl_qpolynomial_sign(set, d);
1128 isl_qpolynomial_free(d);
1129 if (sgn == covers)
1130 break;
1132 if (j >= fold1->n)
1133 return 0;
1136 return 1;
1139 /* Check whether "pwf1" dominated "pwf2", i.e., the domain of "pwf1" contains
1140 * that of "pwf2" and on each cell, the corresponding fold from pwf1 dominates
1141 * that of pwf2.
1143 int isl_pw_qpolynomial_fold_covers(__isl_keep isl_pw_qpolynomial_fold *pwf1,
1144 __isl_keep isl_pw_qpolynomial_fold *pwf2)
1146 int i, j;
1147 isl_set *dom1, *dom2;
1148 int is_subset;
1150 if (!pwf1 || !pwf2)
1151 return -1;
1153 if (pwf2->n == 0)
1154 return 1;
1155 if (pwf1->n == 0)
1156 return 0;
1158 dom1 = isl_pw_qpolynomial_fold_domain(isl_pw_qpolynomial_fold_copy(pwf1));
1159 dom2 = isl_pw_qpolynomial_fold_domain(isl_pw_qpolynomial_fold_copy(pwf2));
1160 is_subset = isl_set_is_subset(dom2, dom1);
1161 isl_set_free(dom1);
1162 isl_set_free(dom2);
1164 if (is_subset < 0 || !is_subset)
1165 return is_subset;
1167 for (i = 0; i < pwf2->n; ++i) {
1168 for (j = 0; j < pwf1->n; ++j) {
1169 int is_empty;
1170 isl_set *common;
1171 int covers;
1173 common = isl_set_intersect(isl_set_copy(pwf1->p[j].set),
1174 isl_set_copy(pwf2->p[i].set));
1175 is_empty = isl_set_is_empty(common);
1176 if (is_empty < 0 || is_empty) {
1177 isl_set_free(common);
1178 if (is_empty < 0)
1179 return -1;
1180 continue;
1182 covers = qpolynomial_fold_covers_on_domain(common,
1183 pwf1->p[j].fold, pwf2->p[i].fold);
1184 isl_set_free(common);
1185 if (covers < 0 || !covers)
1186 return covers;
1190 return 1;
1193 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_morph_domain(
1194 __isl_take isl_qpolynomial_fold *fold, __isl_take isl_morph *morph)
1196 int i;
1197 isl_ctx *ctx;
1199 if (!fold || !morph)
1200 goto error;
1202 ctx = fold->dim->ctx;
1203 isl_assert(ctx, isl_space_is_equal(fold->dim, morph->dom->dim), goto error);
1205 fold = isl_qpolynomial_fold_cow(fold);
1206 if (!fold)
1207 goto error;
1209 isl_space_free(fold->dim);
1210 fold->dim = isl_space_copy(morph->ran->dim);
1211 if (!fold->dim)
1212 goto error;
1214 for (i = 0; i < fold->n; ++i) {
1215 fold->qp[i] = isl_qpolynomial_morph_domain(fold->qp[i],
1216 isl_morph_copy(morph));
1217 if (!fold->qp[i])
1218 goto error;
1221 isl_morph_free(morph);
1223 return fold;
1224 error:
1225 isl_qpolynomial_fold_free(fold);
1226 isl_morph_free(morph);
1227 return NULL;
1230 enum isl_fold isl_qpolynomial_fold_get_type(__isl_keep isl_qpolynomial_fold *fold)
1232 if (!fold)
1233 return isl_fold_list;
1234 return fold->type;
1237 enum isl_fold isl_union_pw_qpolynomial_fold_get_type(
1238 __isl_keep isl_union_pw_qpolynomial_fold *upwf)
1240 if (!upwf)
1241 return isl_fold_list;
1242 return upwf->type;
1245 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_lift(
1246 __isl_take isl_qpolynomial_fold *fold, __isl_take isl_space *dim)
1248 int i;
1250 if (!fold || !dim)
1251 goto error;
1253 if (isl_space_is_equal(fold->dim, dim)) {
1254 isl_space_free(dim);
1255 return fold;
1258 fold = isl_qpolynomial_fold_cow(fold);
1259 if (!fold)
1260 goto error;
1262 isl_space_free(fold->dim);
1263 fold->dim = isl_space_copy(dim);
1264 if (!fold->dim)
1265 goto error;
1267 for (i = 0; i < fold->n; ++i) {
1268 fold->qp[i] = isl_qpolynomial_lift(fold->qp[i],
1269 isl_space_copy(dim));
1270 if (!fold->qp[i])
1271 goto error;
1274 isl_space_free(dim);
1276 return fold;
1277 error:
1278 isl_qpolynomial_fold_free(fold);
1279 isl_space_free(dim);
1280 return NULL;
1283 int isl_qpolynomial_fold_foreach_qpolynomial(
1284 __isl_keep isl_qpolynomial_fold *fold,
1285 int (*fn)(__isl_take isl_qpolynomial *qp, void *user), void *user)
1287 int i;
1289 if (!fold)
1290 return -1;
1292 for (i = 0; i < fold->n; ++i)
1293 if (fn(isl_qpolynomial_copy(fold->qp[i]), user) < 0)
1294 return -1;
1296 return 0;
1299 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_move_dims(
1300 __isl_take isl_qpolynomial_fold *fold,
1301 enum isl_dim_type dst_type, unsigned dst_pos,
1302 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
1304 int i;
1306 if (n == 0)
1307 return fold;
1309 fold = isl_qpolynomial_fold_cow(fold);
1310 if (!fold)
1311 return NULL;
1313 fold->dim = isl_space_move_dims(fold->dim, dst_type, dst_pos,
1314 src_type, src_pos, n);
1315 if (!fold->dim)
1316 goto error;
1318 for (i = 0; i < fold->n; ++i) {
1319 fold->qp[i] = isl_qpolynomial_move_dims(fold->qp[i],
1320 dst_type, dst_pos, src_type, src_pos, n);
1321 if (!fold->qp[i])
1322 goto error;
1325 return fold;
1326 error:
1327 isl_qpolynomial_fold_free(fold);
1328 return NULL;
1331 /* For each 0 <= i < "n", replace variable "first" + i of type "type"
1332 * in fold->qp[k] by subs[i].
1334 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_substitute(
1335 __isl_take isl_qpolynomial_fold *fold,
1336 enum isl_dim_type type, unsigned first, unsigned n,
1337 __isl_keep isl_qpolynomial **subs)
1339 int i;
1341 if (n == 0)
1342 return fold;
1344 fold = isl_qpolynomial_fold_cow(fold);
1345 if (!fold)
1346 return NULL;
1348 for (i = 0; i < fold->n; ++i) {
1349 fold->qp[i] = isl_qpolynomial_substitute(fold->qp[i],
1350 type, first, n, subs);
1351 if (!fold->qp[i])
1352 goto error;
1355 return fold;
1356 error:
1357 isl_qpolynomial_fold_free(fold);
1358 return NULL;
1361 static int add_pwqp(__isl_take isl_pw_qpolynomial *pwqp, void *user)
1363 isl_ctx *ctx;
1364 isl_pw_qpolynomial_fold *pwf;
1365 isl_union_pw_qpolynomial_fold **upwf;
1366 uint32_t hash;
1367 struct isl_hash_table_entry *entry;
1369 upwf = (isl_union_pw_qpolynomial_fold **)user;
1371 ctx = pwqp->dim->ctx;
1372 hash = isl_space_get_hash(pwqp->dim);
1373 entry = isl_hash_table_find(ctx, &(*upwf)->table,
1374 hash, &has_dim, pwqp->dim, 1);
1375 if (!entry)
1376 goto error;
1378 pwf = isl_pw_qpolynomial_fold_from_pw_qpolynomial((*upwf)->type, pwqp);
1379 if (!entry->data)
1380 entry->data = pwf;
1381 else {
1382 entry->data = isl_pw_qpolynomial_fold_add(entry->data, pwf);
1383 if (!entry->data)
1384 return -1;
1385 if (isl_pw_qpolynomial_fold_is_zero(entry->data)) {
1386 isl_pw_qpolynomial_fold_free(entry->data);
1387 isl_hash_table_remove(ctx, &(*upwf)->table, entry);
1391 return 0;
1392 error:
1393 isl_pw_qpolynomial_free(pwqp);
1394 return -1;
1397 __isl_give isl_union_pw_qpolynomial_fold *isl_union_pw_qpolynomial_fold_add_union_pw_qpolynomial(
1398 __isl_take isl_union_pw_qpolynomial_fold *upwf,
1399 __isl_take isl_union_pw_qpolynomial *upwqp)
1401 upwf = isl_union_pw_qpolynomial_fold_align_params(upwf,
1402 isl_union_pw_qpolynomial_get_space(upwqp));
1403 upwqp = isl_union_pw_qpolynomial_align_params(upwqp,
1404 isl_union_pw_qpolynomial_fold_get_space(upwf));
1406 upwf = isl_union_pw_qpolynomial_fold_cow(upwf);
1407 if (!upwf || !upwqp)
1408 goto error;
1410 if (isl_union_pw_qpolynomial_foreach_pw_qpolynomial(upwqp, &add_pwqp,
1411 &upwf) < 0)
1412 goto error;
1414 isl_union_pw_qpolynomial_free(upwqp);
1416 return upwf;
1417 error:
1418 isl_union_pw_qpolynomial_fold_free(upwf);
1419 isl_union_pw_qpolynomial_free(upwqp);
1420 return NULL;
1423 static int join_compatible(__isl_keep isl_space *dim1, __isl_keep isl_space *dim2)
1425 int m;
1426 m = isl_space_match(dim1, isl_dim_param, dim2, isl_dim_param);
1427 if (m < 0 || !m)
1428 return m;
1429 return isl_space_tuple_is_equal(dim1, isl_dim_out, dim2, isl_dim_in);
1432 /* Compute the intersection of the range of the map and the domain
1433 * of the piecewise quasipolynomial reduction and then compute a bound
1434 * on the associated quasipolynomial reduction over all elements
1435 * in this intersection.
1437 * We first introduce some unconstrained dimensions in the
1438 * piecewise quasipolynomial, intersect the resulting domain
1439 * with the wrapped map and the compute the sum.
1441 __isl_give isl_pw_qpolynomial_fold *isl_map_apply_pw_qpolynomial_fold(
1442 __isl_take isl_map *map, __isl_take isl_pw_qpolynomial_fold *pwf,
1443 int *tight)
1445 isl_ctx *ctx;
1446 isl_set *dom;
1447 isl_space *map_dim;
1448 isl_space *pwf_dim;
1449 unsigned n_in;
1450 int ok;
1452 ctx = isl_map_get_ctx(map);
1453 if (!ctx)
1454 goto error;
1456 map_dim = isl_map_get_space(map);
1457 pwf_dim = isl_pw_qpolynomial_fold_get_space(pwf);
1458 ok = join_compatible(map_dim, pwf_dim);
1459 isl_space_free(map_dim);
1460 isl_space_free(pwf_dim);
1461 if (!ok)
1462 isl_die(ctx, isl_error_invalid, "incompatible dimensions",
1463 goto error);
1465 n_in = isl_map_dim(map, isl_dim_in);
1466 pwf = isl_pw_qpolynomial_fold_insert_dims(pwf, isl_dim_in, 0, n_in);
1468 dom = isl_map_wrap(map);
1469 pwf = isl_pw_qpolynomial_fold_reset_domain_space(pwf,
1470 isl_set_get_space(dom));
1472 pwf = isl_pw_qpolynomial_fold_intersect_domain(pwf, dom);
1473 pwf = isl_pw_qpolynomial_fold_bound(pwf, tight);
1475 return pwf;
1476 error:
1477 isl_map_free(map);
1478 isl_pw_qpolynomial_fold_free(pwf);
1479 return NULL;
1482 __isl_give isl_pw_qpolynomial_fold *isl_set_apply_pw_qpolynomial_fold(
1483 __isl_take isl_set *set, __isl_take isl_pw_qpolynomial_fold *pwf,
1484 int *tight)
1486 return isl_map_apply_pw_qpolynomial_fold(set, pwf, tight);
1489 struct isl_apply_fold_data {
1490 isl_union_pw_qpolynomial_fold *upwf;
1491 isl_union_pw_qpolynomial_fold *res;
1492 isl_map *map;
1493 int tight;
1496 static int pw_qpolynomial_fold_apply(__isl_take isl_pw_qpolynomial_fold *pwf,
1497 void *user)
1499 isl_space *map_dim;
1500 isl_space *pwf_dim;
1501 struct isl_apply_fold_data *data = user;
1502 int ok;
1504 map_dim = isl_map_get_space(data->map);
1505 pwf_dim = isl_pw_qpolynomial_fold_get_space(pwf);
1506 ok = join_compatible(map_dim, pwf_dim);
1507 isl_space_free(map_dim);
1508 isl_space_free(pwf_dim);
1510 if (ok) {
1511 pwf = isl_map_apply_pw_qpolynomial_fold(isl_map_copy(data->map),
1512 pwf, data->tight ? &data->tight : NULL);
1513 data->res = isl_union_pw_qpolynomial_fold_fold_pw_qpolynomial_fold(
1514 data->res, pwf);
1515 } else
1516 isl_pw_qpolynomial_fold_free(pwf);
1518 return 0;
1521 static int map_apply(__isl_take isl_map *map, void *user)
1523 struct isl_apply_fold_data *data = user;
1524 int r;
1526 data->map = map;
1527 r = isl_union_pw_qpolynomial_fold_foreach_pw_qpolynomial_fold(
1528 data->upwf, &pw_qpolynomial_fold_apply, data);
1530 isl_map_free(map);
1531 return r;
1534 __isl_give isl_union_pw_qpolynomial_fold *isl_union_map_apply_union_pw_qpolynomial_fold(
1535 __isl_take isl_union_map *umap,
1536 __isl_take isl_union_pw_qpolynomial_fold *upwf, int *tight)
1538 isl_space *dim;
1539 enum isl_fold type;
1540 struct isl_apply_fold_data data;
1542 upwf = isl_union_pw_qpolynomial_fold_align_params(upwf,
1543 isl_union_map_get_space(umap));
1544 umap = isl_union_map_align_params(umap,
1545 isl_union_pw_qpolynomial_fold_get_space(upwf));
1547 data.upwf = upwf;
1548 data.tight = tight ? 1 : 0;
1549 dim = isl_union_pw_qpolynomial_fold_get_space(upwf);
1550 type = isl_union_pw_qpolynomial_fold_get_type(upwf);
1551 data.res = isl_union_pw_qpolynomial_fold_zero(dim, type);
1552 if (isl_union_map_foreach_map(umap, &map_apply, &data) < 0)
1553 goto error;
1555 isl_union_map_free(umap);
1556 isl_union_pw_qpolynomial_fold_free(upwf);
1558 if (tight)
1559 *tight = data.tight;
1561 return data.res;
1562 error:
1563 isl_union_map_free(umap);
1564 isl_union_pw_qpolynomial_fold_free(upwf);
1565 isl_union_pw_qpolynomial_fold_free(data.res);
1566 return NULL;
1569 __isl_give isl_union_pw_qpolynomial_fold *isl_union_set_apply_union_pw_qpolynomial_fold(
1570 __isl_take isl_union_set *uset,
1571 __isl_take isl_union_pw_qpolynomial_fold *upwf, int *tight)
1573 return isl_union_map_apply_union_pw_qpolynomial_fold(uset, upwf, tight);
1576 /* Reorder the dimension of "fold" according to the given reordering.
1578 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_realign_domain(
1579 __isl_take isl_qpolynomial_fold *fold, __isl_take isl_reordering *r)
1581 int i;
1583 fold = isl_qpolynomial_fold_cow(fold);
1584 if (!fold || !r)
1585 goto error;
1587 for (i = 0; i < fold->n; ++i) {
1588 fold->qp[i] = isl_qpolynomial_realign_domain(fold->qp[i],
1589 isl_reordering_copy(r));
1590 if (!fold->qp[i])
1591 goto error;
1594 fold = isl_qpolynomial_fold_reset_domain_space(fold,
1595 isl_space_copy(r->dim));
1597 isl_reordering_free(r);
1599 return fold;
1600 error:
1601 isl_qpolynomial_fold_free(fold);
1602 isl_reordering_free(r);
1603 return NULL;
1606 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_mul_isl_int(
1607 __isl_take isl_qpolynomial_fold *fold, isl_int v)
1609 int i;
1611 if (isl_int_is_one(v))
1612 return fold;
1613 if (fold && isl_int_is_zero(v)) {
1614 isl_qpolynomial_fold *zero;
1615 isl_space *dim = isl_space_copy(fold->dim);
1616 zero = isl_qpolynomial_fold_empty(fold->type, dim);
1617 isl_qpolynomial_fold_free(fold);
1618 return zero;
1621 fold = isl_qpolynomial_fold_cow(fold);
1622 if (!fold)
1623 return NULL;
1625 if (isl_int_is_neg(v))
1626 fold->type = isl_fold_type_negate(fold->type);
1627 for (i = 0; i < fold->n; ++i) {
1628 fold->qp[i] = isl_qpolynomial_mul_isl_int(fold->qp[i], v);
1629 if (!fold->qp[i])
1630 goto error;
1633 return fold;
1634 error:
1635 isl_qpolynomial_fold_free(fold);
1636 return NULL;
1639 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_scale(
1640 __isl_take isl_qpolynomial_fold *fold, isl_int v)
1642 return isl_qpolynomial_fold_mul_isl_int(fold, v);
1645 /* Multiply "fold" by "v".
1647 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_scale_val(
1648 __isl_take isl_qpolynomial_fold *fold, __isl_take isl_val *v)
1650 int i;
1652 if (!fold || !v)
1653 goto error;
1655 if (isl_val_is_one(v)) {
1656 isl_val_free(v);
1657 return fold;
1659 if (isl_val_is_zero(v)) {
1660 isl_qpolynomial_fold *zero;
1661 isl_space *space = isl_qpolynomial_fold_get_domain_space(fold);
1662 zero = isl_qpolynomial_fold_empty(fold->type, space);
1663 isl_qpolynomial_fold_free(fold);
1664 isl_val_free(v);
1665 return zero;
1667 if (!isl_val_is_rat(v))
1668 isl_die(isl_qpolynomial_fold_get_ctx(fold), isl_error_invalid,
1669 "expecting rational factor", goto error);
1671 fold = isl_qpolynomial_fold_cow(fold);
1672 if (!fold)
1673 goto error;
1675 if (isl_val_is_neg(v))
1676 fold->type = isl_fold_type_negate(fold->type);
1677 for (i = 0; i < fold->n; ++i) {
1678 fold->qp[i] = isl_qpolynomial_scale_val(fold->qp[i],
1679 isl_val_copy(v));
1680 if (!fold->qp[i])
1681 goto error;
1684 isl_val_free(v);
1685 return fold;
1686 error:
1687 isl_val_free(v);
1688 isl_qpolynomial_fold_free(fold);
1689 return NULL;