isl 0.08
[isl.git] / isl_fold.c
blobfcafa154affb40e33490eca48e98179daeaf15a4
1 /*
2 * Copyright 2010 INRIA Saclay
4 * Use of this software is governed by the GNU LGPLv2.1 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.h>
18 #include <isl/seq.h>
19 #include <isl_mat_private.h>
20 #include <isl_config.h>
22 enum isl_fold isl_fold_type_negate(enum isl_fold type)
24 switch (type) {
25 case isl_fold_min:
26 return isl_fold_max;
27 case isl_fold_max:
28 return isl_fold_min;
29 case isl_fold_list:
30 return isl_fold_list;
33 isl_die(NULL, isl_error_internal, "unhandled isl_fold type", abort());
36 static __isl_give isl_qpolynomial_fold *qpolynomial_fold_alloc(
37 enum isl_fold type, __isl_take isl_space *dim, int n)
39 isl_qpolynomial_fold *fold;
41 if (!dim)
42 goto error;
44 isl_assert(dim->ctx, n >= 0, goto error);
45 fold = isl_calloc(dim->ctx, struct isl_qpolynomial_fold,
46 sizeof(struct isl_qpolynomial_fold) +
47 (n - 1) * sizeof(struct isl_qpolynomial *));
48 if (!fold)
49 goto error;
51 fold->ref = 1;
52 fold->size = n;
53 fold->n = 0;
54 fold->type = type;
55 fold->dim = dim;
57 return fold;
58 error:
59 isl_space_free(dim);
60 return NULL;
63 isl_ctx *isl_qpolynomial_fold_get_ctx(__isl_keep isl_qpolynomial_fold *fold)
65 return fold ? fold->dim->ctx : NULL;
68 __isl_give isl_space *isl_qpolynomial_fold_get_domain_space(
69 __isl_keep isl_qpolynomial_fold *fold)
71 return fold ? isl_space_copy(fold->dim) : NULL;
74 __isl_give isl_space *isl_qpolynomial_fold_get_space(
75 __isl_keep isl_qpolynomial_fold *fold)
77 isl_space *space;
78 if (!fold)
79 return NULL;
80 space = isl_space_copy(fold->dim);
81 space = isl_space_from_domain(space);
82 space = isl_space_add_dims(space, isl_dim_out, 1);
83 return space;
86 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_reset_domain_space(
87 __isl_take isl_qpolynomial_fold *fold, __isl_take isl_space *dim)
89 int i;
91 fold = isl_qpolynomial_fold_cow(fold);
92 if (!fold || !dim)
93 goto error;
95 for (i = 0; i < fold->n; ++i) {
96 fold->qp[i] = isl_qpolynomial_reset_domain_space(fold->qp[i],
97 isl_space_copy(dim));
98 if (!fold->qp[i])
99 goto error;
102 isl_space_free(fold->dim);
103 fold->dim = dim;
105 return fold;
106 error:
107 isl_qpolynomial_fold_free(fold);
108 isl_space_free(dim);
109 return NULL;
112 /* Reset the space of "fold". This function is called from isl_pw_templ.c
113 * and doesn't know if the space of an element object is represented
114 * directly or through its domain. It therefore passes along both.
116 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_reset_space_and_domain(
117 __isl_take isl_qpolynomial_fold *fold, __isl_take isl_space *space,
118 __isl_take isl_space *domain)
120 isl_space_free(space);
121 return isl_qpolynomial_fold_reset_domain_space(fold, domain);
124 int isl_qpolynomial_fold_involves_dims(__isl_keep isl_qpolynomial_fold *fold,
125 enum isl_dim_type type, unsigned first, unsigned n)
127 int i;
129 if (!fold)
130 return -1;
131 if (fold->n == 0 || n == 0)
132 return 0;
134 for (i = 0; i < fold->n; ++i) {
135 int involves = isl_qpolynomial_involves_dims(fold->qp[i],
136 type, first, n);
137 if (involves < 0 || involves)
138 return involves;
140 return 0;
143 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_set_dim_name(
144 __isl_take isl_qpolynomial_fold *fold,
145 enum isl_dim_type type, unsigned pos, const char *s)
147 int i;
149 fold = isl_qpolynomial_fold_cow(fold);
150 if (!fold)
151 return NULL;
152 fold->dim = isl_space_set_dim_name(fold->dim, type, pos, s);
153 if (!fold->dim)
154 goto error;
156 for (i = 0; i < fold->n; ++i) {
157 fold->qp[i] = isl_qpolynomial_set_dim_name(fold->qp[i],
158 type, pos, s);
159 if (!fold->qp[i])
160 goto error;
163 return fold;
164 error:
165 isl_qpolynomial_fold_free(fold);
166 return NULL;
169 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_drop_dims(
170 __isl_take isl_qpolynomial_fold *fold,
171 enum isl_dim_type type, unsigned first, unsigned n)
173 int i;
174 enum isl_dim_type set_type;
176 if (!fold)
177 return NULL;
178 if (n == 0)
179 return fold;
181 set_type = type == isl_dim_in ? isl_dim_set : type;
183 fold = isl_qpolynomial_fold_cow(fold);
184 if (!fold)
185 return NULL;
186 fold->dim = isl_space_drop_dims(fold->dim, set_type, first, n);
187 if (!fold->dim)
188 goto error;
190 for (i = 0; i < fold->n; ++i) {
191 fold->qp[i] = isl_qpolynomial_drop_dims(fold->qp[i],
192 type, first, n);
193 if (!fold->qp[i])
194 goto error;
197 return fold;
198 error:
199 isl_qpolynomial_fold_free(fold);
200 return NULL;
203 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_insert_dims(
204 __isl_take isl_qpolynomial_fold *fold,
205 enum isl_dim_type type, unsigned first, unsigned n)
207 int i;
209 if (!fold)
210 return NULL;
211 if (n == 0 && !isl_space_is_named_or_nested(fold->dim, type))
212 return fold;
214 fold = isl_qpolynomial_fold_cow(fold);
215 if (!fold)
216 return NULL;
217 fold->dim = isl_space_insert_dims(fold->dim, type, first, n);
218 if (!fold->dim)
219 goto error;
221 for (i = 0; i < fold->n; ++i) {
222 fold->qp[i] = isl_qpolynomial_insert_dims(fold->qp[i],
223 type, first, n);
224 if (!fold->qp[i])
225 goto error;
228 return fold;
229 error:
230 isl_qpolynomial_fold_free(fold);
231 return NULL;
234 static int isl_qpolynomial_cst_sign(__isl_keep isl_qpolynomial *qp)
236 struct isl_upoly_cst *cst;
238 cst = isl_upoly_as_cst(qp->upoly);
239 if (!cst)
240 return 0;
242 return isl_int_sgn(cst->n) < 0 ? -1 : 1;
245 static int isl_qpolynomial_aff_sign(__isl_keep isl_set *set,
246 __isl_keep isl_qpolynomial *qp)
248 enum isl_lp_result res;
249 isl_vec *aff;
250 isl_int opt;
251 int sgn = 0;
253 aff = isl_qpolynomial_extract_affine(qp);
254 if (!aff)
255 return 0;
257 isl_int_init(opt);
259 res = isl_set_solve_lp(set, 0, aff->el + 1, aff->el[0],
260 &opt, NULL, NULL);
261 if (res == isl_lp_error)
262 goto done;
263 if (res == isl_lp_empty ||
264 (res == isl_lp_ok && !isl_int_is_neg(opt))) {
265 sgn = 1;
266 goto done;
269 res = isl_set_solve_lp(set, 1, aff->el + 1, aff->el[0],
270 &opt, NULL, NULL);
271 if (res == isl_lp_ok && !isl_int_is_pos(opt))
272 sgn = -1;
274 done:
275 isl_int_clear(opt);
276 isl_vec_free(aff);
277 return sgn;
280 /* Determine, if possible, the sign of the quasipolynomial "qp" on
281 * the domain "set".
283 * If qp is a constant, then the problem is trivial.
284 * If qp is linear, then we check if the minimum of the corresponding
285 * affine constraint is non-negative or if the maximum is non-positive.
287 * Otherwise, we check if the outermost variable "v" has a lower bound "l"
288 * in "set". If so, we write qp(v,v') as
290 * q(v,v') * (v - l) + r(v')
292 * if q(v,v') and r(v') have the same known sign, then the original
293 * quasipolynomial has the same sign as well.
295 * Return
296 * -1 if qp <= 0
297 * 1 if qp >= 0
298 * 0 if unknown
300 static int isl_qpolynomial_sign(__isl_keep isl_set *set,
301 __isl_keep isl_qpolynomial *qp)
303 int d;
304 int i;
305 int is;
306 struct isl_upoly_rec *rec;
307 isl_vec *v;
308 isl_int l;
309 enum isl_lp_result res;
310 int sgn = 0;
312 is = isl_qpolynomial_is_cst(qp, NULL, NULL);
313 if (is < 0)
314 return 0;
315 if (is)
316 return isl_qpolynomial_cst_sign(qp);
318 is = isl_qpolynomial_is_affine(qp);
319 if (is < 0)
320 return 0;
321 if (is)
322 return isl_qpolynomial_aff_sign(set, qp);
324 if (qp->div->n_row > 0)
325 return 0;
327 rec = isl_upoly_as_rec(qp->upoly);
328 if (!rec)
329 return 0;
331 d = isl_space_dim(qp->dim, isl_dim_all);
332 v = isl_vec_alloc(set->ctx, 2 + d);
333 if (!v)
334 return 0;
336 isl_seq_clr(v->el + 1, 1 + d);
337 isl_int_set_si(v->el[0], 1);
338 isl_int_set_si(v->el[2 + qp->upoly->var], 1);
340 isl_int_init(l);
342 res = isl_set_solve_lp(set, 0, v->el + 1, v->el[0], &l, NULL, NULL);
343 if (res == isl_lp_ok) {
344 isl_qpolynomial *min;
345 isl_qpolynomial *base;
346 isl_qpolynomial *r, *q;
347 isl_qpolynomial *t;
349 min = isl_qpolynomial_cst_on_domain(isl_space_copy(qp->dim), l);
350 base = isl_qpolynomial_var_pow_on_domain(isl_space_copy(qp->dim),
351 qp->upoly->var, 1);
353 r = isl_qpolynomial_alloc(isl_space_copy(qp->dim), 0,
354 isl_upoly_copy(rec->p[rec->n - 1]));
355 q = isl_qpolynomial_copy(r);
357 for (i = rec->n - 2; i >= 0; --i) {
358 r = isl_qpolynomial_mul(r, isl_qpolynomial_copy(min));
359 t = isl_qpolynomial_alloc(isl_space_copy(qp->dim), 0,
360 isl_upoly_copy(rec->p[i]));
361 r = isl_qpolynomial_add(r, t);
362 if (i == 0)
363 break;
364 q = isl_qpolynomial_mul(q, isl_qpolynomial_copy(base));
365 q = isl_qpolynomial_add(q, isl_qpolynomial_copy(r));
368 if (isl_qpolynomial_is_zero(q))
369 sgn = isl_qpolynomial_sign(set, r);
370 else if (isl_qpolynomial_is_zero(r))
371 sgn = isl_qpolynomial_sign(set, q);
372 else {
373 int sgn_q, sgn_r;
374 sgn_r = isl_qpolynomial_sign(set, r);
375 sgn_q = isl_qpolynomial_sign(set, q);
376 if (sgn_r == sgn_q)
377 sgn = sgn_r;
380 isl_qpolynomial_free(min);
381 isl_qpolynomial_free(base);
382 isl_qpolynomial_free(q);
383 isl_qpolynomial_free(r);
386 isl_int_clear(l);
388 isl_vec_free(v);
390 return sgn;
393 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_fold_on_domain(
394 __isl_keep isl_set *set,
395 __isl_take isl_qpolynomial_fold *fold1,
396 __isl_take isl_qpolynomial_fold *fold2)
398 int i, j;
399 int n1;
400 struct isl_qpolynomial_fold *res = NULL;
401 int better;
403 if (!fold1 || !fold2)
404 goto error;
406 isl_assert(fold1->dim->ctx, fold1->type == fold2->type, goto error);
407 isl_assert(fold1->dim->ctx, isl_space_is_equal(fold1->dim, fold2->dim),
408 goto error);
410 better = fold1->type == isl_fold_max ? -1 : 1;
412 if (isl_qpolynomial_fold_is_empty(fold1)) {
413 isl_qpolynomial_fold_free(fold1);
414 return fold2;
417 if (isl_qpolynomial_fold_is_empty(fold2)) {
418 isl_qpolynomial_fold_free(fold2);
419 return fold1;
422 res = qpolynomial_fold_alloc(fold1->type, isl_space_copy(fold1->dim),
423 fold1->n + fold2->n);
424 if (!res)
425 goto error;
427 for (i = 0; i < fold1->n; ++i) {
428 res->qp[res->n] = isl_qpolynomial_copy(fold1->qp[i]);
429 if (!res->qp[res->n])
430 goto error;
431 res->n++;
433 n1 = res->n;
435 for (i = 0; i < fold2->n; ++i) {
436 for (j = n1 - 1; j >= 0; --j) {
437 isl_qpolynomial *d;
438 int sgn;
439 d = isl_qpolynomial_sub(
440 isl_qpolynomial_copy(res->qp[j]),
441 isl_qpolynomial_copy(fold2->qp[i]));
442 sgn = isl_qpolynomial_sign(set, d);
443 isl_qpolynomial_free(d);
444 if (sgn == 0)
445 continue;
446 if (sgn != better)
447 break;
448 isl_qpolynomial_free(res->qp[j]);
449 if (j != n1 - 1)
450 res->qp[j] = res->qp[n1 - 1];
451 n1--;
452 if (n1 != res->n - 1)
453 res->qp[n1] = res->qp[res->n - 1];
454 res->n--;
456 if (j >= 0)
457 continue;
458 res->qp[res->n] = isl_qpolynomial_copy(fold2->qp[i]);
459 if (!res->qp[res->n])
460 goto error;
461 res->n++;
464 isl_qpolynomial_fold_free(fold1);
465 isl_qpolynomial_fold_free(fold2);
467 return res;
468 error:
469 isl_qpolynomial_fold_free(res);
470 isl_qpolynomial_fold_free(fold1);
471 isl_qpolynomial_fold_free(fold2);
472 return NULL;
475 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_add_qpolynomial(
476 __isl_take isl_qpolynomial_fold *fold, __isl_take isl_qpolynomial *qp)
478 int i;
480 if (!fold || !qp)
481 goto error;
483 if (isl_qpolynomial_is_zero(qp)) {
484 isl_qpolynomial_free(qp);
485 return fold;
488 fold = isl_qpolynomial_fold_cow(fold);
489 if (!fold)
490 goto error;
492 for (i = 0; i < fold->n; ++i) {
493 fold->qp[i] = isl_qpolynomial_add(fold->qp[i],
494 isl_qpolynomial_copy(qp));
495 if (!fold->qp[i])
496 goto error;
499 isl_qpolynomial_free(qp);
500 return fold;
501 error:
502 isl_qpolynomial_fold_free(fold);
503 isl_qpolynomial_free(qp);
504 return NULL;
507 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_add_on_domain(
508 __isl_keep isl_set *dom,
509 __isl_take isl_qpolynomial_fold *fold1,
510 __isl_take isl_qpolynomial_fold *fold2)
512 int i;
513 isl_qpolynomial_fold *res = NULL;
515 if (!fold1 || !fold2)
516 goto error;
518 if (isl_qpolynomial_fold_is_empty(fold1)) {
519 isl_qpolynomial_fold_free(fold1);
520 return fold2;
523 if (isl_qpolynomial_fold_is_empty(fold2)) {
524 isl_qpolynomial_fold_free(fold2);
525 return fold1;
528 if (fold1->n == 1 && fold2->n != 1)
529 return isl_qpolynomial_fold_add_on_domain(dom, fold2, fold1);
531 if (fold2->n == 1) {
532 res = isl_qpolynomial_fold_add_qpolynomial(fold1,
533 isl_qpolynomial_copy(fold2->qp[0]));
534 isl_qpolynomial_fold_free(fold2);
535 return res;
538 res = isl_qpolynomial_fold_add_qpolynomial(
539 isl_qpolynomial_fold_copy(fold1),
540 isl_qpolynomial_copy(fold2->qp[0]));
542 for (i = 1; i < fold2->n; ++i) {
543 isl_qpolynomial_fold *res_i;
544 res_i = isl_qpolynomial_fold_add_qpolynomial(
545 isl_qpolynomial_fold_copy(fold1),
546 isl_qpolynomial_copy(fold2->qp[i]));
547 res = isl_qpolynomial_fold_fold_on_domain(dom, res, res_i);
550 isl_qpolynomial_fold_free(fold1);
551 isl_qpolynomial_fold_free(fold2);
552 return res;
553 error:
554 isl_qpolynomial_fold_free(res);
555 isl_qpolynomial_fold_free(fold1);
556 isl_qpolynomial_fold_free(fold2);
557 return NULL;
560 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_substitute_equalities(
561 __isl_take isl_qpolynomial_fold *fold, __isl_take isl_basic_set *eq)
563 int i;
565 if (!fold || !eq)
566 goto error;
568 fold = isl_qpolynomial_fold_cow(fold);
569 if (!fold)
570 return NULL;
572 for (i = 0; i < fold->n; ++i) {
573 fold->qp[i] = isl_qpolynomial_substitute_equalities(fold->qp[i],
574 isl_basic_set_copy(eq));
575 if (!fold->qp[i])
576 goto error;
579 isl_basic_set_free(eq);
580 return fold;
581 error:
582 isl_basic_set_free(eq);
583 isl_qpolynomial_fold_free(fold);
584 return NULL;
587 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_gist(
588 __isl_take isl_qpolynomial_fold *fold, __isl_take isl_set *context)
590 int i;
592 if (!fold || !context)
593 goto error;
595 fold = isl_qpolynomial_fold_cow(fold);
596 if (!fold)
597 return NULL;
599 for (i = 0; i < fold->n; ++i) {
600 fold->qp[i] = isl_qpolynomial_gist(fold->qp[i],
601 isl_set_copy(context));
602 if (!fold->qp[i])
603 goto error;
606 isl_set_free(context);
607 return fold;
608 error:
609 isl_set_free(context);
610 isl_qpolynomial_fold_free(fold);
611 return NULL;
614 #define HAS_TYPE
616 #undef PW
617 #define PW isl_pw_qpolynomial_fold
618 #undef EL
619 #define EL isl_qpolynomial_fold
620 #undef EL_IS_ZERO
621 #define EL_IS_ZERO is_empty
622 #undef ZERO
623 #define ZERO zero
624 #undef IS_ZERO
625 #define IS_ZERO is_zero
626 #undef FIELD
627 #define FIELD fold
629 #define NO_NEG
631 #include <isl_pw_templ.c>
633 #undef UNION
634 #define UNION isl_union_pw_qpolynomial_fold
635 #undef PART
636 #define PART isl_pw_qpolynomial_fold
637 #undef PARTS
638 #define PARTS pw_qpolynomial_fold
639 #define ALIGN_DOMAIN
641 #include <isl_union_templ.c>
643 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_empty(enum isl_fold type,
644 __isl_take isl_space *dim)
646 return qpolynomial_fold_alloc(type, dim, 0);
649 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_alloc(
650 enum isl_fold type, __isl_take isl_qpolynomial *qp)
652 isl_qpolynomial_fold *fold;
654 if (!qp)
655 return NULL;
657 fold = qpolynomial_fold_alloc(type, isl_space_copy(qp->dim), 1);
658 if (!fold)
659 goto error;
661 fold->qp[0] = qp;
662 fold->n++;
664 return fold;
665 error:
666 isl_qpolynomial_fold_free(fold);
667 isl_qpolynomial_free(qp);
668 return NULL;
671 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_copy(
672 __isl_keep isl_qpolynomial_fold *fold)
674 if (!fold)
675 return NULL;
677 fold->ref++;
678 return fold;
681 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_dup(
682 __isl_keep isl_qpolynomial_fold *fold)
684 int i;
685 isl_qpolynomial_fold *dup;
687 if (!fold)
688 return NULL;
689 dup = qpolynomial_fold_alloc(fold->type,
690 isl_space_copy(fold->dim), fold->n);
691 if (!dup)
692 return NULL;
694 dup->n = fold->n;
695 for (i = 0; i < fold->n; ++i) {
696 dup->qp[i] = isl_qpolynomial_copy(fold->qp[i]);
697 if (!dup->qp[i])
698 goto error;
701 return dup;
702 error:
703 isl_qpolynomial_fold_free(dup);
704 return NULL;
707 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_cow(
708 __isl_take isl_qpolynomial_fold *fold)
710 if (!fold)
711 return NULL;
713 if (fold->ref == 1)
714 return fold;
715 fold->ref--;
716 return isl_qpolynomial_fold_dup(fold);
719 void isl_qpolynomial_fold_free(__isl_take isl_qpolynomial_fold *fold)
721 int i;
723 if (!fold)
724 return;
725 if (--fold->ref > 0)
726 return;
728 for (i = 0; i < fold->n; ++i)
729 isl_qpolynomial_free(fold->qp[i]);
730 isl_space_free(fold->dim);
731 free(fold);
734 int isl_qpolynomial_fold_is_empty(__isl_keep isl_qpolynomial_fold *fold)
736 if (!fold)
737 return -1;
739 return fold->n == 0;
742 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_fold(
743 __isl_take isl_qpolynomial_fold *fold1,
744 __isl_take isl_qpolynomial_fold *fold2)
746 int i;
747 struct isl_qpolynomial_fold *res = NULL;
749 if (!fold1 || !fold2)
750 goto error;
752 isl_assert(fold1->dim->ctx, fold1->type == fold2->type, goto error);
753 isl_assert(fold1->dim->ctx, isl_space_is_equal(fold1->dim, fold2->dim),
754 goto error);
756 if (isl_qpolynomial_fold_is_empty(fold1)) {
757 isl_qpolynomial_fold_free(fold1);
758 return fold2;
761 if (isl_qpolynomial_fold_is_empty(fold2)) {
762 isl_qpolynomial_fold_free(fold2);
763 return fold1;
766 res = qpolynomial_fold_alloc(fold1->type, isl_space_copy(fold1->dim),
767 fold1->n + fold2->n);
768 if (!res)
769 goto error;
771 for (i = 0; i < fold1->n; ++i) {
772 res->qp[res->n] = isl_qpolynomial_copy(fold1->qp[i]);
773 if (!res->qp[res->n])
774 goto error;
775 res->n++;
778 for (i = 0; i < fold2->n; ++i) {
779 res->qp[res->n] = isl_qpolynomial_copy(fold2->qp[i]);
780 if (!res->qp[res->n])
781 goto error;
782 res->n++;
785 isl_qpolynomial_fold_free(fold1);
786 isl_qpolynomial_fold_free(fold2);
788 return res;
789 error:
790 isl_qpolynomial_fold_free(res);
791 isl_qpolynomial_fold_free(fold1);
792 isl_qpolynomial_fold_free(fold2);
793 return NULL;
796 __isl_give isl_pw_qpolynomial_fold *isl_pw_qpolynomial_fold_fold(
797 __isl_take isl_pw_qpolynomial_fold *pw1,
798 __isl_take isl_pw_qpolynomial_fold *pw2)
800 int i, j, n;
801 struct isl_pw_qpolynomial_fold *res;
802 isl_set *set;
804 if (!pw1 || !pw2)
805 goto error;
807 isl_assert(pw1->dim->ctx, isl_space_is_equal(pw1->dim, pw2->dim), goto error);
809 if (isl_pw_qpolynomial_fold_is_zero(pw1)) {
810 isl_pw_qpolynomial_fold_free(pw1);
811 return pw2;
814 if (isl_pw_qpolynomial_fold_is_zero(pw2)) {
815 isl_pw_qpolynomial_fold_free(pw2);
816 return pw1;
819 if (pw1->type != pw2->type)
820 isl_die(pw1->dim->ctx, isl_error_invalid,
821 "fold types don't match", goto error);
823 n = (pw1->n + 1) * (pw2->n + 1);
824 res = isl_pw_qpolynomial_fold_alloc_size(isl_space_copy(pw1->dim),
825 pw1->type, n);
827 for (i = 0; i < pw1->n; ++i) {
828 set = isl_set_copy(pw1->p[i].set);
829 for (j = 0; j < pw2->n; ++j) {
830 struct isl_set *common;
831 isl_qpolynomial_fold *sum;
832 set = isl_set_subtract(set,
833 isl_set_copy(pw2->p[j].set));
834 common = isl_set_intersect(isl_set_copy(pw1->p[i].set),
835 isl_set_copy(pw2->p[j].set));
836 if (isl_set_plain_is_empty(common)) {
837 isl_set_free(common);
838 continue;
841 sum = isl_qpolynomial_fold_fold_on_domain(common,
842 isl_qpolynomial_fold_copy(pw1->p[i].fold),
843 isl_qpolynomial_fold_copy(pw2->p[j].fold));
845 res = isl_pw_qpolynomial_fold_add_piece(res, common, sum);
847 res = isl_pw_qpolynomial_fold_add_piece(res, set,
848 isl_qpolynomial_fold_copy(pw1->p[i].fold));
851 for (j = 0; j < pw2->n; ++j) {
852 set = isl_set_copy(pw2->p[j].set);
853 for (i = 0; i < pw1->n; ++i)
854 set = isl_set_subtract(set, isl_set_copy(pw1->p[i].set));
855 res = isl_pw_qpolynomial_fold_add_piece(res, set,
856 isl_qpolynomial_fold_copy(pw2->p[j].fold));
859 isl_pw_qpolynomial_fold_free(pw1);
860 isl_pw_qpolynomial_fold_free(pw2);
862 return res;
863 error:
864 isl_pw_qpolynomial_fold_free(pw1);
865 isl_pw_qpolynomial_fold_free(pw2);
866 return NULL;
869 __isl_give isl_union_pw_qpolynomial_fold *isl_union_pw_qpolynomial_fold_fold_pw_qpolynomial_fold(
870 __isl_take isl_union_pw_qpolynomial_fold *u,
871 __isl_take isl_pw_qpolynomial_fold *part)
873 uint32_t hash;
874 struct isl_hash_table_entry *entry;
876 u = isl_union_pw_qpolynomial_fold_cow(u);
878 if (!part || !u)
879 goto error;
881 isl_assert(u->dim->ctx, isl_space_match(part->dim, isl_dim_param, u->dim,
882 isl_dim_param), goto error);
884 hash = isl_space_get_hash(part->dim);
885 entry = isl_hash_table_find(u->dim->ctx, &u->table, hash,
886 &has_dim, part->dim, 1);
887 if (!entry)
888 goto error;
890 if (!entry->data)
891 entry->data = part;
892 else {
893 entry->data = isl_pw_qpolynomial_fold_fold(entry->data,
894 isl_pw_qpolynomial_fold_copy(part));
895 if (!entry->data)
896 goto error;
897 isl_pw_qpolynomial_fold_free(part);
900 return u;
901 error:
902 isl_pw_qpolynomial_fold_free(part);
903 isl_union_pw_qpolynomial_fold_free(u);
904 return NULL;
907 static int fold_part(__isl_take isl_pw_qpolynomial_fold *part, void *user)
909 isl_union_pw_qpolynomial_fold **u;
910 u = (isl_union_pw_qpolynomial_fold **)user;
912 *u = isl_union_pw_qpolynomial_fold_fold_pw_qpolynomial_fold(*u, part);
914 return 0;
917 __isl_give isl_union_pw_qpolynomial_fold *isl_union_pw_qpolynomial_fold_fold(
918 __isl_take isl_union_pw_qpolynomial_fold *u1,
919 __isl_take isl_union_pw_qpolynomial_fold *u2)
921 u1 = isl_union_pw_qpolynomial_fold_cow(u1);
923 if (!u1 || !u2)
924 goto error;
926 if (isl_union_pw_qpolynomial_fold_foreach_pw_qpolynomial_fold(u2,
927 &fold_part, &u1) < 0)
928 goto error;
930 isl_union_pw_qpolynomial_fold_free(u2);
932 return u1;
933 error:
934 isl_union_pw_qpolynomial_fold_free(u1);
935 isl_union_pw_qpolynomial_fold_free(u2);
936 return NULL;
939 __isl_give isl_pw_qpolynomial_fold *isl_pw_qpolynomial_fold_from_pw_qpolynomial(
940 enum isl_fold type, __isl_take isl_pw_qpolynomial *pwqp)
942 int i;
943 isl_pw_qpolynomial_fold *pwf;
945 if (!pwqp)
946 return NULL;
948 pwf = isl_pw_qpolynomial_fold_alloc_size(isl_space_copy(pwqp->dim),
949 type, pwqp->n);
951 for (i = 0; i < pwqp->n; ++i)
952 pwf = isl_pw_qpolynomial_fold_add_piece(pwf,
953 isl_set_copy(pwqp->p[i].set),
954 isl_qpolynomial_fold_alloc(type,
955 isl_qpolynomial_copy(pwqp->p[i].qp)));
957 isl_pw_qpolynomial_free(pwqp);
959 return pwf;
962 __isl_give isl_pw_qpolynomial_fold *isl_pw_qpolynomial_fold_add(
963 __isl_take isl_pw_qpolynomial_fold *pwf1,
964 __isl_take isl_pw_qpolynomial_fold *pwf2)
966 return isl_pw_qpolynomial_fold_union_add_(pwf1, pwf2);
969 int isl_qpolynomial_fold_plain_is_equal(__isl_keep isl_qpolynomial_fold *fold1,
970 __isl_keep isl_qpolynomial_fold *fold2)
972 int i;
974 if (!fold1 || !fold2)
975 return -1;
977 if (fold1->n != fold2->n)
978 return 0;
980 /* We probably want to sort the qps first... */
981 for (i = 0; i < fold1->n; ++i) {
982 int eq = isl_qpolynomial_plain_is_equal(fold1->qp[i], fold2->qp[i]);
983 if (eq < 0 || !eq)
984 return eq;
987 return 1;
990 __isl_give isl_qpolynomial *isl_qpolynomial_fold_eval(
991 __isl_take isl_qpolynomial_fold *fold, __isl_take isl_point *pnt)
993 isl_qpolynomial *qp;
995 if (!fold || !pnt)
996 goto error;
997 isl_assert(pnt->dim->ctx, isl_space_is_equal(pnt->dim, fold->dim), goto error);
998 isl_assert(pnt->dim->ctx,
999 fold->type == isl_fold_max || fold->type == isl_fold_min,
1000 goto error);
1002 if (fold->n == 0)
1003 qp = isl_qpolynomial_zero_on_domain(isl_space_copy(fold->dim));
1004 else {
1005 int i;
1006 qp = isl_qpolynomial_eval(isl_qpolynomial_copy(fold->qp[0]),
1007 isl_point_copy(pnt));
1008 for (i = 1; i < fold->n; ++i) {
1009 isl_qpolynomial *qp_i;
1010 qp_i = isl_qpolynomial_eval(
1011 isl_qpolynomial_copy(fold->qp[i]),
1012 isl_point_copy(pnt));
1013 if (fold->type == isl_fold_max)
1014 qp = isl_qpolynomial_max_cst(qp, qp_i);
1015 else
1016 qp = isl_qpolynomial_min_cst(qp, qp_i);
1019 isl_qpolynomial_fold_free(fold);
1020 isl_point_free(pnt);
1022 return qp;
1023 error:
1024 isl_qpolynomial_fold_free(fold);
1025 isl_point_free(pnt);
1026 return NULL;
1029 size_t isl_pw_qpolynomial_fold_size(__isl_keep isl_pw_qpolynomial_fold *pwf)
1031 int i;
1032 size_t n = 0;
1034 for (i = 0; i < pwf->n; ++i)
1035 n += pwf->p[i].fold->n;
1037 return n;
1040 __isl_give isl_qpolynomial *isl_qpolynomial_fold_opt_on_domain(
1041 __isl_take isl_qpolynomial_fold *fold, __isl_take isl_set *set, int max)
1043 int i;
1044 isl_qpolynomial *opt;
1046 if (!set || !fold)
1047 goto error;
1049 if (fold->n == 0) {
1050 isl_space *dim = isl_space_copy(fold->dim);
1051 isl_set_free(set);
1052 isl_qpolynomial_fold_free(fold);
1053 return isl_qpolynomial_zero_on_domain(dim);
1056 opt = isl_qpolynomial_opt_on_domain(isl_qpolynomial_copy(fold->qp[0]),
1057 isl_set_copy(set), max);
1058 for (i = 1; i < fold->n; ++i) {
1059 isl_qpolynomial *opt_i;
1060 opt_i = isl_qpolynomial_opt_on_domain(
1061 isl_qpolynomial_copy(fold->qp[i]),
1062 isl_set_copy(set), max);
1063 if (max)
1064 opt = isl_qpolynomial_max_cst(opt, opt_i);
1065 else
1066 opt = isl_qpolynomial_min_cst(opt, opt_i);
1069 isl_set_free(set);
1070 isl_qpolynomial_fold_free(fold);
1072 return opt;
1073 error:
1074 isl_set_free(set);
1075 isl_qpolynomial_fold_free(fold);
1076 return NULL;
1079 /* Check whether for each quasi-polynomial in "fold2" there is
1080 * a quasi-polynomial in "fold1" that dominates it on "set".
1082 static int qpolynomial_fold_covers_on_domain(__isl_keep isl_set *set,
1083 __isl_keep isl_qpolynomial_fold *fold1,
1084 __isl_keep isl_qpolynomial_fold *fold2)
1086 int i, j;
1087 int covers;
1089 if (!set || !fold1 || !fold2)
1090 return -1;
1092 covers = fold1->type == isl_fold_max ? 1 : -1;
1094 for (i = 0; i < fold2->n; ++i) {
1095 for (j = 0; j < fold1->n; ++j) {
1096 isl_qpolynomial *d;
1097 int sgn;
1099 d = isl_qpolynomial_sub(
1100 isl_qpolynomial_copy(fold1->qp[j]),
1101 isl_qpolynomial_copy(fold2->qp[i]));
1102 sgn = isl_qpolynomial_sign(set, d);
1103 isl_qpolynomial_free(d);
1104 if (sgn == covers)
1105 break;
1107 if (j >= fold1->n)
1108 return 0;
1111 return 1;
1114 /* Check whether "pwf1" dominated "pwf2", i.e., the domain of "pwf1" contains
1115 * that of "pwf2" and on each cell, the corresponding fold from pwf1 dominates
1116 * that of pwf2.
1118 int isl_pw_qpolynomial_fold_covers(__isl_keep isl_pw_qpolynomial_fold *pwf1,
1119 __isl_keep isl_pw_qpolynomial_fold *pwf2)
1121 int i, j;
1122 isl_set *dom1, *dom2;
1123 int is_subset;
1125 if (!pwf1 || !pwf2)
1126 return -1;
1128 if (pwf2->n == 0)
1129 return 1;
1130 if (pwf1->n == 0)
1131 return 0;
1133 dom1 = isl_pw_qpolynomial_fold_domain(isl_pw_qpolynomial_fold_copy(pwf1));
1134 dom2 = isl_pw_qpolynomial_fold_domain(isl_pw_qpolynomial_fold_copy(pwf2));
1135 is_subset = isl_set_is_subset(dom2, dom1);
1136 isl_set_free(dom1);
1137 isl_set_free(dom2);
1139 if (is_subset < 0 || !is_subset)
1140 return is_subset;
1142 for (i = 0; i < pwf2->n; ++i) {
1143 for (j = 0; j < pwf1->n; ++j) {
1144 int is_empty;
1145 isl_set *common;
1146 int covers;
1148 common = isl_set_intersect(isl_set_copy(pwf1->p[j].set),
1149 isl_set_copy(pwf2->p[i].set));
1150 is_empty = isl_set_is_empty(common);
1151 if (is_empty < 0 || is_empty) {
1152 isl_set_free(common);
1153 if (is_empty < 0)
1154 return -1;
1155 continue;
1157 covers = qpolynomial_fold_covers_on_domain(common,
1158 pwf1->p[j].fold, pwf2->p[i].fold);
1159 isl_set_free(common);
1160 if (covers < 0 || !covers)
1161 return covers;
1165 return 1;
1168 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_morph_domain(
1169 __isl_take isl_qpolynomial_fold *fold, __isl_take isl_morph *morph)
1171 int i;
1172 isl_ctx *ctx;
1174 if (!fold || !morph)
1175 goto error;
1177 ctx = fold->dim->ctx;
1178 isl_assert(ctx, isl_space_is_equal(fold->dim, morph->dom->dim), goto error);
1180 fold = isl_qpolynomial_fold_cow(fold);
1181 if (!fold)
1182 goto error;
1184 isl_space_free(fold->dim);
1185 fold->dim = isl_space_copy(morph->ran->dim);
1186 if (!fold->dim)
1187 goto error;
1189 for (i = 0; i < fold->n; ++i) {
1190 fold->qp[i] = isl_qpolynomial_morph_domain(fold->qp[i],
1191 isl_morph_copy(morph));
1192 if (!fold->qp[i])
1193 goto error;
1196 isl_morph_free(morph);
1198 return fold;
1199 error:
1200 isl_qpolynomial_fold_free(fold);
1201 isl_morph_free(morph);
1202 return NULL;
1205 enum isl_fold isl_qpolynomial_fold_get_type(__isl_keep isl_qpolynomial_fold *fold)
1207 if (!fold)
1208 return isl_fold_list;
1209 return fold->type;
1212 enum isl_fold isl_union_pw_qpolynomial_fold_get_type(
1213 __isl_keep isl_union_pw_qpolynomial_fold *upwf)
1215 if (!upwf)
1216 return isl_fold_list;
1217 return upwf->type;
1220 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_lift(
1221 __isl_take isl_qpolynomial_fold *fold, __isl_take isl_space *dim)
1223 int i;
1225 if (!fold || !dim)
1226 goto error;
1228 if (isl_space_is_equal(fold->dim, dim)) {
1229 isl_space_free(dim);
1230 return fold;
1233 fold = isl_qpolynomial_fold_cow(fold);
1234 if (!fold)
1235 goto error;
1237 isl_space_free(fold->dim);
1238 fold->dim = isl_space_copy(dim);
1239 if (!fold->dim)
1240 goto error;
1242 for (i = 0; i < fold->n; ++i) {
1243 fold->qp[i] = isl_qpolynomial_lift(fold->qp[i],
1244 isl_space_copy(dim));
1245 if (!fold->qp[i])
1246 goto error;
1249 isl_space_free(dim);
1251 return fold;
1252 error:
1253 isl_qpolynomial_fold_free(fold);
1254 isl_space_free(dim);
1255 return NULL;
1258 int isl_qpolynomial_fold_foreach_qpolynomial(
1259 __isl_keep isl_qpolynomial_fold *fold,
1260 int (*fn)(__isl_take isl_qpolynomial *qp, void *user), void *user)
1262 int i;
1264 if (!fold)
1265 return -1;
1267 for (i = 0; i < fold->n; ++i)
1268 if (fn(isl_qpolynomial_copy(fold->qp[i]), user) < 0)
1269 return -1;
1271 return 0;
1274 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_move_dims(
1275 __isl_take isl_qpolynomial_fold *fold,
1276 enum isl_dim_type dst_type, unsigned dst_pos,
1277 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
1279 int i;
1281 if (n == 0)
1282 return fold;
1284 fold = isl_qpolynomial_fold_cow(fold);
1285 if (!fold)
1286 return NULL;
1288 fold->dim = isl_space_move_dims(fold->dim, dst_type, dst_pos,
1289 src_type, src_pos, n);
1290 if (!fold->dim)
1291 goto error;
1293 for (i = 0; i < fold->n; ++i) {
1294 fold->qp[i] = isl_qpolynomial_move_dims(fold->qp[i],
1295 dst_type, dst_pos, src_type, src_pos, n);
1296 if (!fold->qp[i])
1297 goto error;
1300 return fold;
1301 error:
1302 isl_qpolynomial_fold_free(fold);
1303 return NULL;
1306 /* For each 0 <= i < "n", replace variable "first" + i of type "type"
1307 * in fold->qp[k] by subs[i].
1309 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_substitute(
1310 __isl_take isl_qpolynomial_fold *fold,
1311 enum isl_dim_type type, unsigned first, unsigned n,
1312 __isl_keep isl_qpolynomial **subs)
1314 int i;
1316 if (n == 0)
1317 return fold;
1319 fold = isl_qpolynomial_fold_cow(fold);
1320 if (!fold)
1321 return NULL;
1323 for (i = 0; i < fold->n; ++i) {
1324 fold->qp[i] = isl_qpolynomial_substitute(fold->qp[i],
1325 type, first, n, subs);
1326 if (!fold->qp[i])
1327 goto error;
1330 return fold;
1331 error:
1332 isl_qpolynomial_fold_free(fold);
1333 return NULL;
1336 static int add_pwqp(__isl_take isl_pw_qpolynomial *pwqp, void *user)
1338 isl_ctx *ctx;
1339 isl_pw_qpolynomial_fold *pwf;
1340 isl_union_pw_qpolynomial_fold **upwf;
1341 uint32_t hash;
1342 struct isl_hash_table_entry *entry;
1344 upwf = (isl_union_pw_qpolynomial_fold **)user;
1346 ctx = pwqp->dim->ctx;
1347 hash = isl_space_get_hash(pwqp->dim);
1348 entry = isl_hash_table_find(ctx, &(*upwf)->table,
1349 hash, &has_dim, pwqp->dim, 1);
1350 if (!entry)
1351 goto error;
1353 pwf = isl_pw_qpolynomial_fold_from_pw_qpolynomial((*upwf)->type, pwqp);
1354 if (!entry->data)
1355 entry->data = pwf;
1356 else {
1357 entry->data = isl_pw_qpolynomial_fold_add(entry->data, pwf);
1358 if (!entry->data)
1359 return -1;
1360 if (isl_pw_qpolynomial_fold_is_zero(entry->data)) {
1361 isl_pw_qpolynomial_fold_free(entry->data);
1362 isl_hash_table_remove(ctx, &(*upwf)->table, entry);
1366 return 0;
1367 error:
1368 isl_pw_qpolynomial_free(pwqp);
1369 return -1;
1372 __isl_give isl_union_pw_qpolynomial_fold *isl_union_pw_qpolynomial_fold_add_union_pw_qpolynomial(
1373 __isl_take isl_union_pw_qpolynomial_fold *upwf,
1374 __isl_take isl_union_pw_qpolynomial *upwqp)
1376 upwf = isl_union_pw_qpolynomial_fold_align_params(upwf,
1377 isl_union_pw_qpolynomial_get_space(upwqp));
1378 upwqp = isl_union_pw_qpolynomial_align_params(upwqp,
1379 isl_union_pw_qpolynomial_fold_get_space(upwf));
1381 upwf = isl_union_pw_qpolynomial_fold_cow(upwf);
1382 if (!upwf || !upwqp)
1383 goto error;
1385 if (isl_union_pw_qpolynomial_foreach_pw_qpolynomial(upwqp, &add_pwqp,
1386 &upwf) < 0)
1387 goto error;
1389 isl_union_pw_qpolynomial_free(upwqp);
1391 return upwf;
1392 error:
1393 isl_union_pw_qpolynomial_fold_free(upwf);
1394 isl_union_pw_qpolynomial_free(upwqp);
1395 return NULL;
1398 static int join_compatible(__isl_keep isl_space *dim1, __isl_keep isl_space *dim2)
1400 int m;
1401 m = isl_space_match(dim1, isl_dim_param, dim2, isl_dim_param);
1402 if (m < 0 || !m)
1403 return m;
1404 return isl_space_tuple_match(dim1, isl_dim_out, dim2, isl_dim_in);
1407 /* Compute the intersection of the range of the map and the domain
1408 * of the piecewise quasipolynomial reduction and then compute a bound
1409 * on the associated quasipolynomial reduction over all elements
1410 * in this intersection.
1412 * We first introduce some unconstrained dimensions in the
1413 * piecewise quasipolynomial, intersect the resulting domain
1414 * with the wrapped map and the compute the sum.
1416 __isl_give isl_pw_qpolynomial_fold *isl_map_apply_pw_qpolynomial_fold(
1417 __isl_take isl_map *map, __isl_take isl_pw_qpolynomial_fold *pwf,
1418 int *tight)
1420 isl_ctx *ctx;
1421 isl_set *dom;
1422 isl_space *map_dim;
1423 isl_space *pwf_dim;
1424 unsigned n_in;
1425 int ok;
1427 ctx = isl_map_get_ctx(map);
1428 if (!ctx)
1429 goto error;
1431 map_dim = isl_map_get_space(map);
1432 pwf_dim = isl_pw_qpolynomial_fold_get_space(pwf);
1433 ok = join_compatible(map_dim, pwf_dim);
1434 isl_space_free(map_dim);
1435 isl_space_free(pwf_dim);
1436 if (!ok)
1437 isl_die(ctx, isl_error_invalid, "incompatible dimensions",
1438 goto error);
1440 n_in = isl_map_dim(map, isl_dim_in);
1441 pwf = isl_pw_qpolynomial_fold_insert_dims(pwf, isl_dim_in, 0, n_in);
1443 dom = isl_map_wrap(map);
1444 pwf = isl_pw_qpolynomial_fold_reset_domain_space(pwf,
1445 isl_set_get_space(dom));
1447 pwf = isl_pw_qpolynomial_fold_intersect_domain(pwf, dom);
1448 pwf = isl_pw_qpolynomial_fold_bound(pwf, tight);
1450 return pwf;
1451 error:
1452 isl_map_free(map);
1453 isl_pw_qpolynomial_fold_free(pwf);
1454 return NULL;
1457 __isl_give isl_pw_qpolynomial_fold *isl_set_apply_pw_qpolynomial_fold(
1458 __isl_take isl_set *set, __isl_take isl_pw_qpolynomial_fold *pwf,
1459 int *tight)
1461 return isl_map_apply_pw_qpolynomial_fold(set, pwf, tight);
1464 struct isl_apply_fold_data {
1465 isl_union_pw_qpolynomial_fold *upwf;
1466 isl_union_pw_qpolynomial_fold *res;
1467 isl_map *map;
1468 int tight;
1471 static int pw_qpolynomial_fold_apply(__isl_take isl_pw_qpolynomial_fold *pwf,
1472 void *user)
1474 isl_space *map_dim;
1475 isl_space *pwf_dim;
1476 struct isl_apply_fold_data *data = user;
1477 int ok;
1479 map_dim = isl_map_get_space(data->map);
1480 pwf_dim = isl_pw_qpolynomial_fold_get_space(pwf);
1481 ok = join_compatible(map_dim, pwf_dim);
1482 isl_space_free(map_dim);
1483 isl_space_free(pwf_dim);
1485 if (ok) {
1486 pwf = isl_map_apply_pw_qpolynomial_fold(isl_map_copy(data->map),
1487 pwf, data->tight ? &data->tight : NULL);
1488 data->res = isl_union_pw_qpolynomial_fold_fold_pw_qpolynomial_fold(
1489 data->res, pwf);
1490 } else
1491 isl_pw_qpolynomial_fold_free(pwf);
1493 return 0;
1496 static int map_apply(__isl_take isl_map *map, void *user)
1498 struct isl_apply_fold_data *data = user;
1499 int r;
1501 data->map = map;
1502 r = isl_union_pw_qpolynomial_fold_foreach_pw_qpolynomial_fold(
1503 data->upwf, &pw_qpolynomial_fold_apply, data);
1505 isl_map_free(map);
1506 return r;
1509 __isl_give isl_union_pw_qpolynomial_fold *isl_union_map_apply_union_pw_qpolynomial_fold(
1510 __isl_take isl_union_map *umap,
1511 __isl_take isl_union_pw_qpolynomial_fold *upwf, int *tight)
1513 isl_space *dim;
1514 enum isl_fold type;
1515 struct isl_apply_fold_data data;
1517 upwf = isl_union_pw_qpolynomial_fold_align_params(upwf,
1518 isl_union_map_get_space(umap));
1519 umap = isl_union_map_align_params(umap,
1520 isl_union_pw_qpolynomial_fold_get_space(upwf));
1522 data.upwf = upwf;
1523 data.tight = tight ? 1 : 0;
1524 dim = isl_union_pw_qpolynomial_fold_get_space(upwf);
1525 type = isl_union_pw_qpolynomial_fold_get_type(upwf);
1526 data.res = isl_union_pw_qpolynomial_fold_zero(dim, type);
1527 if (isl_union_map_foreach_map(umap, &map_apply, &data) < 0)
1528 goto error;
1530 isl_union_map_free(umap);
1531 isl_union_pw_qpolynomial_fold_free(upwf);
1533 if (tight)
1534 *tight = data.tight;
1536 return data.res;
1537 error:
1538 isl_union_map_free(umap);
1539 isl_union_pw_qpolynomial_fold_free(upwf);
1540 isl_union_pw_qpolynomial_fold_free(data.res);
1541 return NULL;
1544 __isl_give isl_union_pw_qpolynomial_fold *isl_union_set_apply_union_pw_qpolynomial_fold(
1545 __isl_take isl_union_set *uset,
1546 __isl_take isl_union_pw_qpolynomial_fold *upwf, int *tight)
1548 return isl_union_map_apply_union_pw_qpolynomial_fold(uset, upwf, tight);
1551 /* Reorder the dimension of "fold" according to the given reordering.
1553 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_realign_domain(
1554 __isl_take isl_qpolynomial_fold *fold, __isl_take isl_reordering *r)
1556 int i;
1558 fold = isl_qpolynomial_fold_cow(fold);
1559 if (!fold || !r)
1560 goto error;
1562 for (i = 0; i < fold->n; ++i) {
1563 fold->qp[i] = isl_qpolynomial_realign_domain(fold->qp[i],
1564 isl_reordering_copy(r));
1565 if (!fold->qp[i])
1566 goto error;
1569 fold = isl_qpolynomial_fold_reset_domain_space(fold,
1570 isl_space_copy(r->dim));
1572 isl_reordering_free(r);
1574 return fold;
1575 error:
1576 isl_qpolynomial_fold_free(fold);
1577 isl_reordering_free(r);
1578 return NULL;
1581 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_mul_isl_int(
1582 __isl_take isl_qpolynomial_fold *fold, isl_int v)
1584 int i;
1586 if (isl_int_is_one(v))
1587 return fold;
1588 if (fold && isl_int_is_zero(v)) {
1589 isl_qpolynomial_fold *zero;
1590 isl_space *dim = isl_space_copy(fold->dim);
1591 zero = isl_qpolynomial_fold_empty(fold->type, dim);
1592 isl_qpolynomial_fold_free(fold);
1593 return zero;
1596 fold = isl_qpolynomial_fold_cow(fold);
1597 if (!fold)
1598 return NULL;
1600 if (isl_int_is_neg(v))
1601 fold->type = isl_fold_type_negate(fold->type);
1602 for (i = 0; i < fold->n; ++i) {
1603 fold->qp[i] = isl_qpolynomial_mul_isl_int(fold->qp[i], v);
1604 if (!fold->qp[i])
1605 goto error;
1608 return fold;
1609 error:
1610 isl_qpolynomial_fold_free(fold);
1611 return NULL;
1614 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_scale(
1615 __isl_take isl_qpolynomial_fold *fold, isl_int v)
1617 return isl_qpolynomial_fold_mul_isl_int(fold, v);