declare isl_*_list together with isl_*
[isl.git] / isl_fold.c
blob82cad6f48f4b890da45416f351a3b977a5f1ed89
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.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 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_gist_params(
615 __isl_take isl_qpolynomial_fold *fold, __isl_take isl_set *context)
617 isl_space *space = isl_qpolynomial_fold_get_domain_space(fold);
618 isl_set *dom_context = isl_set_universe(space);
619 dom_context = isl_set_intersect_params(dom_context, context);
620 return isl_qpolynomial_fold_gist(fold, dom_context);
623 #define HAS_TYPE
625 #undef PW
626 #define PW isl_pw_qpolynomial_fold
627 #undef EL
628 #define EL isl_qpolynomial_fold
629 #undef EL_IS_ZERO
630 #define EL_IS_ZERO is_empty
631 #undef ZERO
632 #define ZERO zero
633 #undef IS_ZERO
634 #define IS_ZERO is_zero
635 #undef FIELD
636 #define FIELD fold
637 #undef DEFAULT_IS_ZERO
638 #define DEFAULT_IS_ZERO 1
640 #define NO_NEG
641 #define NO_PULLBACK
643 #include <isl_pw_templ.c>
645 #undef UNION
646 #define UNION isl_union_pw_qpolynomial_fold
647 #undef PART
648 #define PART isl_pw_qpolynomial_fold
649 #undef PARTS
650 #define PARTS pw_qpolynomial_fold
651 #define ALIGN_DOMAIN
653 #define NO_SUB
655 #include <isl_union_templ.c>
657 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_empty(enum isl_fold type,
658 __isl_take isl_space *dim)
660 return qpolynomial_fold_alloc(type, dim, 0);
663 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_alloc(
664 enum isl_fold type, __isl_take isl_qpolynomial *qp)
666 isl_qpolynomial_fold *fold;
668 if (!qp)
669 return NULL;
671 fold = qpolynomial_fold_alloc(type, isl_space_copy(qp->dim), 1);
672 if (!fold)
673 goto error;
675 fold->qp[0] = qp;
676 fold->n++;
678 return fold;
679 error:
680 isl_qpolynomial_fold_free(fold);
681 isl_qpolynomial_free(qp);
682 return NULL;
685 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_copy(
686 __isl_keep isl_qpolynomial_fold *fold)
688 if (!fold)
689 return NULL;
691 fold->ref++;
692 return fold;
695 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_dup(
696 __isl_keep isl_qpolynomial_fold *fold)
698 int i;
699 isl_qpolynomial_fold *dup;
701 if (!fold)
702 return NULL;
703 dup = qpolynomial_fold_alloc(fold->type,
704 isl_space_copy(fold->dim), fold->n);
705 if (!dup)
706 return NULL;
708 dup->n = fold->n;
709 for (i = 0; i < fold->n; ++i) {
710 dup->qp[i] = isl_qpolynomial_copy(fold->qp[i]);
711 if (!dup->qp[i])
712 goto error;
715 return dup;
716 error:
717 isl_qpolynomial_fold_free(dup);
718 return NULL;
721 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_cow(
722 __isl_take isl_qpolynomial_fold *fold)
724 if (!fold)
725 return NULL;
727 if (fold->ref == 1)
728 return fold;
729 fold->ref--;
730 return isl_qpolynomial_fold_dup(fold);
733 void isl_qpolynomial_fold_free(__isl_take isl_qpolynomial_fold *fold)
735 int i;
737 if (!fold)
738 return;
739 if (--fold->ref > 0)
740 return;
742 for (i = 0; i < fold->n; ++i)
743 isl_qpolynomial_free(fold->qp[i]);
744 isl_space_free(fold->dim);
745 free(fold);
748 int isl_qpolynomial_fold_is_empty(__isl_keep isl_qpolynomial_fold *fold)
750 if (!fold)
751 return -1;
753 return fold->n == 0;
756 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_fold(
757 __isl_take isl_qpolynomial_fold *fold1,
758 __isl_take isl_qpolynomial_fold *fold2)
760 int i;
761 struct isl_qpolynomial_fold *res = NULL;
763 if (!fold1 || !fold2)
764 goto error;
766 isl_assert(fold1->dim->ctx, fold1->type == fold2->type, goto error);
767 isl_assert(fold1->dim->ctx, isl_space_is_equal(fold1->dim, fold2->dim),
768 goto error);
770 if (isl_qpolynomial_fold_is_empty(fold1)) {
771 isl_qpolynomial_fold_free(fold1);
772 return fold2;
775 if (isl_qpolynomial_fold_is_empty(fold2)) {
776 isl_qpolynomial_fold_free(fold2);
777 return fold1;
780 res = qpolynomial_fold_alloc(fold1->type, isl_space_copy(fold1->dim),
781 fold1->n + fold2->n);
782 if (!res)
783 goto error;
785 for (i = 0; i < fold1->n; ++i) {
786 res->qp[res->n] = isl_qpolynomial_copy(fold1->qp[i]);
787 if (!res->qp[res->n])
788 goto error;
789 res->n++;
792 for (i = 0; i < fold2->n; ++i) {
793 res->qp[res->n] = isl_qpolynomial_copy(fold2->qp[i]);
794 if (!res->qp[res->n])
795 goto error;
796 res->n++;
799 isl_qpolynomial_fold_free(fold1);
800 isl_qpolynomial_fold_free(fold2);
802 return res;
803 error:
804 isl_qpolynomial_fold_free(res);
805 isl_qpolynomial_fold_free(fold1);
806 isl_qpolynomial_fold_free(fold2);
807 return NULL;
810 __isl_give isl_pw_qpolynomial_fold *isl_pw_qpolynomial_fold_fold(
811 __isl_take isl_pw_qpolynomial_fold *pw1,
812 __isl_take isl_pw_qpolynomial_fold *pw2)
814 int i, j, n;
815 struct isl_pw_qpolynomial_fold *res;
816 isl_set *set;
818 if (!pw1 || !pw2)
819 goto error;
821 isl_assert(pw1->dim->ctx, isl_space_is_equal(pw1->dim, pw2->dim), goto error);
823 if (isl_pw_qpolynomial_fold_is_zero(pw1)) {
824 isl_pw_qpolynomial_fold_free(pw1);
825 return pw2;
828 if (isl_pw_qpolynomial_fold_is_zero(pw2)) {
829 isl_pw_qpolynomial_fold_free(pw2);
830 return pw1;
833 if (pw1->type != pw2->type)
834 isl_die(pw1->dim->ctx, isl_error_invalid,
835 "fold types don't match", goto error);
837 n = (pw1->n + 1) * (pw2->n + 1);
838 res = isl_pw_qpolynomial_fold_alloc_size(isl_space_copy(pw1->dim),
839 pw1->type, n);
841 for (i = 0; i < pw1->n; ++i) {
842 set = isl_set_copy(pw1->p[i].set);
843 for (j = 0; j < pw2->n; ++j) {
844 struct isl_set *common;
845 isl_qpolynomial_fold *sum;
846 set = isl_set_subtract(set,
847 isl_set_copy(pw2->p[j].set));
848 common = isl_set_intersect(isl_set_copy(pw1->p[i].set),
849 isl_set_copy(pw2->p[j].set));
850 if (isl_set_plain_is_empty(common)) {
851 isl_set_free(common);
852 continue;
855 sum = isl_qpolynomial_fold_fold_on_domain(common,
856 isl_qpolynomial_fold_copy(pw1->p[i].fold),
857 isl_qpolynomial_fold_copy(pw2->p[j].fold));
859 res = isl_pw_qpolynomial_fold_add_piece(res, common, sum);
861 res = isl_pw_qpolynomial_fold_add_piece(res, set,
862 isl_qpolynomial_fold_copy(pw1->p[i].fold));
865 for (j = 0; j < pw2->n; ++j) {
866 set = isl_set_copy(pw2->p[j].set);
867 for (i = 0; i < pw1->n; ++i)
868 set = isl_set_subtract(set, isl_set_copy(pw1->p[i].set));
869 res = isl_pw_qpolynomial_fold_add_piece(res, set,
870 isl_qpolynomial_fold_copy(pw2->p[j].fold));
873 isl_pw_qpolynomial_fold_free(pw1);
874 isl_pw_qpolynomial_fold_free(pw2);
876 return res;
877 error:
878 isl_pw_qpolynomial_fold_free(pw1);
879 isl_pw_qpolynomial_fold_free(pw2);
880 return NULL;
883 __isl_give isl_union_pw_qpolynomial_fold *isl_union_pw_qpolynomial_fold_fold_pw_qpolynomial_fold(
884 __isl_take isl_union_pw_qpolynomial_fold *u,
885 __isl_take isl_pw_qpolynomial_fold *part)
887 uint32_t hash;
888 struct isl_hash_table_entry *entry;
890 u = isl_union_pw_qpolynomial_fold_cow(u);
892 if (!part || !u)
893 goto error;
895 isl_assert(u->dim->ctx, isl_space_match(part->dim, isl_dim_param, u->dim,
896 isl_dim_param), goto error);
898 hash = isl_space_get_hash(part->dim);
899 entry = isl_hash_table_find(u->dim->ctx, &u->table, hash,
900 &has_dim, part->dim, 1);
901 if (!entry)
902 goto error;
904 if (!entry->data)
905 entry->data = part;
906 else {
907 entry->data = isl_pw_qpolynomial_fold_fold(entry->data,
908 isl_pw_qpolynomial_fold_copy(part));
909 if (!entry->data)
910 goto error;
911 isl_pw_qpolynomial_fold_free(part);
914 return u;
915 error:
916 isl_pw_qpolynomial_fold_free(part);
917 isl_union_pw_qpolynomial_fold_free(u);
918 return NULL;
921 static int fold_part(__isl_take isl_pw_qpolynomial_fold *part, void *user)
923 isl_union_pw_qpolynomial_fold **u;
924 u = (isl_union_pw_qpolynomial_fold **)user;
926 *u = isl_union_pw_qpolynomial_fold_fold_pw_qpolynomial_fold(*u, part);
928 return 0;
931 __isl_give isl_union_pw_qpolynomial_fold *isl_union_pw_qpolynomial_fold_fold(
932 __isl_take isl_union_pw_qpolynomial_fold *u1,
933 __isl_take isl_union_pw_qpolynomial_fold *u2)
935 u1 = isl_union_pw_qpolynomial_fold_cow(u1);
937 if (!u1 || !u2)
938 goto error;
940 if (isl_union_pw_qpolynomial_fold_foreach_pw_qpolynomial_fold(u2,
941 &fold_part, &u1) < 0)
942 goto error;
944 isl_union_pw_qpolynomial_fold_free(u2);
946 return u1;
947 error:
948 isl_union_pw_qpolynomial_fold_free(u1);
949 isl_union_pw_qpolynomial_fold_free(u2);
950 return NULL;
953 __isl_give isl_pw_qpolynomial_fold *isl_pw_qpolynomial_fold_from_pw_qpolynomial(
954 enum isl_fold type, __isl_take isl_pw_qpolynomial *pwqp)
956 int i;
957 isl_pw_qpolynomial_fold *pwf;
959 if (!pwqp)
960 return NULL;
962 pwf = isl_pw_qpolynomial_fold_alloc_size(isl_space_copy(pwqp->dim),
963 type, pwqp->n);
965 for (i = 0; i < pwqp->n; ++i)
966 pwf = isl_pw_qpolynomial_fold_add_piece(pwf,
967 isl_set_copy(pwqp->p[i].set),
968 isl_qpolynomial_fold_alloc(type,
969 isl_qpolynomial_copy(pwqp->p[i].qp)));
971 isl_pw_qpolynomial_free(pwqp);
973 return pwf;
976 __isl_give isl_pw_qpolynomial_fold *isl_pw_qpolynomial_fold_add(
977 __isl_take isl_pw_qpolynomial_fold *pwf1,
978 __isl_take isl_pw_qpolynomial_fold *pwf2)
980 return isl_pw_qpolynomial_fold_union_add_(pwf1, pwf2);
983 int isl_qpolynomial_fold_plain_is_equal(__isl_keep isl_qpolynomial_fold *fold1,
984 __isl_keep isl_qpolynomial_fold *fold2)
986 int i;
988 if (!fold1 || !fold2)
989 return -1;
991 if (fold1->n != fold2->n)
992 return 0;
994 /* We probably want to sort the qps first... */
995 for (i = 0; i < fold1->n; ++i) {
996 int eq = isl_qpolynomial_plain_is_equal(fold1->qp[i], fold2->qp[i]);
997 if (eq < 0 || !eq)
998 return eq;
1001 return 1;
1004 __isl_give isl_qpolynomial *isl_qpolynomial_fold_eval(
1005 __isl_take isl_qpolynomial_fold *fold, __isl_take isl_point *pnt)
1007 isl_qpolynomial *qp;
1009 if (!fold || !pnt)
1010 goto error;
1011 isl_assert(pnt->dim->ctx, isl_space_is_equal(pnt->dim, fold->dim), goto error);
1012 isl_assert(pnt->dim->ctx,
1013 fold->type == isl_fold_max || fold->type == isl_fold_min,
1014 goto error);
1016 if (fold->n == 0)
1017 qp = isl_qpolynomial_zero_on_domain(isl_space_copy(fold->dim));
1018 else {
1019 int i;
1020 qp = isl_qpolynomial_eval(isl_qpolynomial_copy(fold->qp[0]),
1021 isl_point_copy(pnt));
1022 for (i = 1; i < fold->n; ++i) {
1023 isl_qpolynomial *qp_i;
1024 qp_i = isl_qpolynomial_eval(
1025 isl_qpolynomial_copy(fold->qp[i]),
1026 isl_point_copy(pnt));
1027 if (fold->type == isl_fold_max)
1028 qp = isl_qpolynomial_max_cst(qp, qp_i);
1029 else
1030 qp = isl_qpolynomial_min_cst(qp, qp_i);
1033 isl_qpolynomial_fold_free(fold);
1034 isl_point_free(pnt);
1036 return qp;
1037 error:
1038 isl_qpolynomial_fold_free(fold);
1039 isl_point_free(pnt);
1040 return NULL;
1043 size_t isl_pw_qpolynomial_fold_size(__isl_keep isl_pw_qpolynomial_fold *pwf)
1045 int i;
1046 size_t n = 0;
1048 for (i = 0; i < pwf->n; ++i)
1049 n += pwf->p[i].fold->n;
1051 return n;
1054 __isl_give isl_qpolynomial *isl_qpolynomial_fold_opt_on_domain(
1055 __isl_take isl_qpolynomial_fold *fold, __isl_take isl_set *set, int max)
1057 int i;
1058 isl_qpolynomial *opt;
1060 if (!set || !fold)
1061 goto error;
1063 if (fold->n == 0) {
1064 isl_space *dim = isl_space_copy(fold->dim);
1065 isl_set_free(set);
1066 isl_qpolynomial_fold_free(fold);
1067 return isl_qpolynomial_zero_on_domain(dim);
1070 opt = isl_qpolynomial_opt_on_domain(isl_qpolynomial_copy(fold->qp[0]),
1071 isl_set_copy(set), max);
1072 for (i = 1; i < fold->n; ++i) {
1073 isl_qpolynomial *opt_i;
1074 opt_i = isl_qpolynomial_opt_on_domain(
1075 isl_qpolynomial_copy(fold->qp[i]),
1076 isl_set_copy(set), max);
1077 if (max)
1078 opt = isl_qpolynomial_max_cst(opt, opt_i);
1079 else
1080 opt = isl_qpolynomial_min_cst(opt, opt_i);
1083 isl_set_free(set);
1084 isl_qpolynomial_fold_free(fold);
1086 return opt;
1087 error:
1088 isl_set_free(set);
1089 isl_qpolynomial_fold_free(fold);
1090 return NULL;
1093 /* Check whether for each quasi-polynomial in "fold2" there is
1094 * a quasi-polynomial in "fold1" that dominates it on "set".
1096 static int qpolynomial_fold_covers_on_domain(__isl_keep isl_set *set,
1097 __isl_keep isl_qpolynomial_fold *fold1,
1098 __isl_keep isl_qpolynomial_fold *fold2)
1100 int i, j;
1101 int covers;
1103 if (!set || !fold1 || !fold2)
1104 return -1;
1106 covers = fold1->type == isl_fold_max ? 1 : -1;
1108 for (i = 0; i < fold2->n; ++i) {
1109 for (j = 0; j < fold1->n; ++j) {
1110 isl_qpolynomial *d;
1111 int sgn;
1113 d = isl_qpolynomial_sub(
1114 isl_qpolynomial_copy(fold1->qp[j]),
1115 isl_qpolynomial_copy(fold2->qp[i]));
1116 sgn = isl_qpolynomial_sign(set, d);
1117 isl_qpolynomial_free(d);
1118 if (sgn == covers)
1119 break;
1121 if (j >= fold1->n)
1122 return 0;
1125 return 1;
1128 /* Check whether "pwf1" dominated "pwf2", i.e., the domain of "pwf1" contains
1129 * that of "pwf2" and on each cell, the corresponding fold from pwf1 dominates
1130 * that of pwf2.
1132 int isl_pw_qpolynomial_fold_covers(__isl_keep isl_pw_qpolynomial_fold *pwf1,
1133 __isl_keep isl_pw_qpolynomial_fold *pwf2)
1135 int i, j;
1136 isl_set *dom1, *dom2;
1137 int is_subset;
1139 if (!pwf1 || !pwf2)
1140 return -1;
1142 if (pwf2->n == 0)
1143 return 1;
1144 if (pwf1->n == 0)
1145 return 0;
1147 dom1 = isl_pw_qpolynomial_fold_domain(isl_pw_qpolynomial_fold_copy(pwf1));
1148 dom2 = isl_pw_qpolynomial_fold_domain(isl_pw_qpolynomial_fold_copy(pwf2));
1149 is_subset = isl_set_is_subset(dom2, dom1);
1150 isl_set_free(dom1);
1151 isl_set_free(dom2);
1153 if (is_subset < 0 || !is_subset)
1154 return is_subset;
1156 for (i = 0; i < pwf2->n; ++i) {
1157 for (j = 0; j < pwf1->n; ++j) {
1158 int is_empty;
1159 isl_set *common;
1160 int covers;
1162 common = isl_set_intersect(isl_set_copy(pwf1->p[j].set),
1163 isl_set_copy(pwf2->p[i].set));
1164 is_empty = isl_set_is_empty(common);
1165 if (is_empty < 0 || is_empty) {
1166 isl_set_free(common);
1167 if (is_empty < 0)
1168 return -1;
1169 continue;
1171 covers = qpolynomial_fold_covers_on_domain(common,
1172 pwf1->p[j].fold, pwf2->p[i].fold);
1173 isl_set_free(common);
1174 if (covers < 0 || !covers)
1175 return covers;
1179 return 1;
1182 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_morph_domain(
1183 __isl_take isl_qpolynomial_fold *fold, __isl_take isl_morph *morph)
1185 int i;
1186 isl_ctx *ctx;
1188 if (!fold || !morph)
1189 goto error;
1191 ctx = fold->dim->ctx;
1192 isl_assert(ctx, isl_space_is_equal(fold->dim, morph->dom->dim), goto error);
1194 fold = isl_qpolynomial_fold_cow(fold);
1195 if (!fold)
1196 goto error;
1198 isl_space_free(fold->dim);
1199 fold->dim = isl_space_copy(morph->ran->dim);
1200 if (!fold->dim)
1201 goto error;
1203 for (i = 0; i < fold->n; ++i) {
1204 fold->qp[i] = isl_qpolynomial_morph_domain(fold->qp[i],
1205 isl_morph_copy(morph));
1206 if (!fold->qp[i])
1207 goto error;
1210 isl_morph_free(morph);
1212 return fold;
1213 error:
1214 isl_qpolynomial_fold_free(fold);
1215 isl_morph_free(morph);
1216 return NULL;
1219 enum isl_fold isl_qpolynomial_fold_get_type(__isl_keep isl_qpolynomial_fold *fold)
1221 if (!fold)
1222 return isl_fold_list;
1223 return fold->type;
1226 enum isl_fold isl_union_pw_qpolynomial_fold_get_type(
1227 __isl_keep isl_union_pw_qpolynomial_fold *upwf)
1229 if (!upwf)
1230 return isl_fold_list;
1231 return upwf->type;
1234 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_lift(
1235 __isl_take isl_qpolynomial_fold *fold, __isl_take isl_space *dim)
1237 int i;
1239 if (!fold || !dim)
1240 goto error;
1242 if (isl_space_is_equal(fold->dim, dim)) {
1243 isl_space_free(dim);
1244 return fold;
1247 fold = isl_qpolynomial_fold_cow(fold);
1248 if (!fold)
1249 goto error;
1251 isl_space_free(fold->dim);
1252 fold->dim = isl_space_copy(dim);
1253 if (!fold->dim)
1254 goto error;
1256 for (i = 0; i < fold->n; ++i) {
1257 fold->qp[i] = isl_qpolynomial_lift(fold->qp[i],
1258 isl_space_copy(dim));
1259 if (!fold->qp[i])
1260 goto error;
1263 isl_space_free(dim);
1265 return fold;
1266 error:
1267 isl_qpolynomial_fold_free(fold);
1268 isl_space_free(dim);
1269 return NULL;
1272 int isl_qpolynomial_fold_foreach_qpolynomial(
1273 __isl_keep isl_qpolynomial_fold *fold,
1274 int (*fn)(__isl_take isl_qpolynomial *qp, void *user), void *user)
1276 int i;
1278 if (!fold)
1279 return -1;
1281 for (i = 0; i < fold->n; ++i)
1282 if (fn(isl_qpolynomial_copy(fold->qp[i]), user) < 0)
1283 return -1;
1285 return 0;
1288 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_move_dims(
1289 __isl_take isl_qpolynomial_fold *fold,
1290 enum isl_dim_type dst_type, unsigned dst_pos,
1291 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
1293 int i;
1295 if (n == 0)
1296 return fold;
1298 fold = isl_qpolynomial_fold_cow(fold);
1299 if (!fold)
1300 return NULL;
1302 fold->dim = isl_space_move_dims(fold->dim, dst_type, dst_pos,
1303 src_type, src_pos, n);
1304 if (!fold->dim)
1305 goto error;
1307 for (i = 0; i < fold->n; ++i) {
1308 fold->qp[i] = isl_qpolynomial_move_dims(fold->qp[i],
1309 dst_type, dst_pos, src_type, src_pos, n);
1310 if (!fold->qp[i])
1311 goto error;
1314 return fold;
1315 error:
1316 isl_qpolynomial_fold_free(fold);
1317 return NULL;
1320 /* For each 0 <= i < "n", replace variable "first" + i of type "type"
1321 * in fold->qp[k] by subs[i].
1323 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_substitute(
1324 __isl_take isl_qpolynomial_fold *fold,
1325 enum isl_dim_type type, unsigned first, unsigned n,
1326 __isl_keep isl_qpolynomial **subs)
1328 int i;
1330 if (n == 0)
1331 return fold;
1333 fold = isl_qpolynomial_fold_cow(fold);
1334 if (!fold)
1335 return NULL;
1337 for (i = 0; i < fold->n; ++i) {
1338 fold->qp[i] = isl_qpolynomial_substitute(fold->qp[i],
1339 type, first, n, subs);
1340 if (!fold->qp[i])
1341 goto error;
1344 return fold;
1345 error:
1346 isl_qpolynomial_fold_free(fold);
1347 return NULL;
1350 static int add_pwqp(__isl_take isl_pw_qpolynomial *pwqp, void *user)
1352 isl_ctx *ctx;
1353 isl_pw_qpolynomial_fold *pwf;
1354 isl_union_pw_qpolynomial_fold **upwf;
1355 uint32_t hash;
1356 struct isl_hash_table_entry *entry;
1358 upwf = (isl_union_pw_qpolynomial_fold **)user;
1360 ctx = pwqp->dim->ctx;
1361 hash = isl_space_get_hash(pwqp->dim);
1362 entry = isl_hash_table_find(ctx, &(*upwf)->table,
1363 hash, &has_dim, pwqp->dim, 1);
1364 if (!entry)
1365 goto error;
1367 pwf = isl_pw_qpolynomial_fold_from_pw_qpolynomial((*upwf)->type, pwqp);
1368 if (!entry->data)
1369 entry->data = pwf;
1370 else {
1371 entry->data = isl_pw_qpolynomial_fold_add(entry->data, pwf);
1372 if (!entry->data)
1373 return -1;
1374 if (isl_pw_qpolynomial_fold_is_zero(entry->data)) {
1375 isl_pw_qpolynomial_fold_free(entry->data);
1376 isl_hash_table_remove(ctx, &(*upwf)->table, entry);
1380 return 0;
1381 error:
1382 isl_pw_qpolynomial_free(pwqp);
1383 return -1;
1386 __isl_give isl_union_pw_qpolynomial_fold *isl_union_pw_qpolynomial_fold_add_union_pw_qpolynomial(
1387 __isl_take isl_union_pw_qpolynomial_fold *upwf,
1388 __isl_take isl_union_pw_qpolynomial *upwqp)
1390 upwf = isl_union_pw_qpolynomial_fold_align_params(upwf,
1391 isl_union_pw_qpolynomial_get_space(upwqp));
1392 upwqp = isl_union_pw_qpolynomial_align_params(upwqp,
1393 isl_union_pw_qpolynomial_fold_get_space(upwf));
1395 upwf = isl_union_pw_qpolynomial_fold_cow(upwf);
1396 if (!upwf || !upwqp)
1397 goto error;
1399 if (isl_union_pw_qpolynomial_foreach_pw_qpolynomial(upwqp, &add_pwqp,
1400 &upwf) < 0)
1401 goto error;
1403 isl_union_pw_qpolynomial_free(upwqp);
1405 return upwf;
1406 error:
1407 isl_union_pw_qpolynomial_fold_free(upwf);
1408 isl_union_pw_qpolynomial_free(upwqp);
1409 return NULL;
1412 static int join_compatible(__isl_keep isl_space *dim1, __isl_keep isl_space *dim2)
1414 int m;
1415 m = isl_space_match(dim1, isl_dim_param, dim2, isl_dim_param);
1416 if (m < 0 || !m)
1417 return m;
1418 return isl_space_tuple_match(dim1, isl_dim_out, dim2, isl_dim_in);
1421 /* Compute the intersection of the range of the map and the domain
1422 * of the piecewise quasipolynomial reduction and then compute a bound
1423 * on the associated quasipolynomial reduction over all elements
1424 * in this intersection.
1426 * We first introduce some unconstrained dimensions in the
1427 * piecewise quasipolynomial, intersect the resulting domain
1428 * with the wrapped map and the compute the sum.
1430 __isl_give isl_pw_qpolynomial_fold *isl_map_apply_pw_qpolynomial_fold(
1431 __isl_take isl_map *map, __isl_take isl_pw_qpolynomial_fold *pwf,
1432 int *tight)
1434 isl_ctx *ctx;
1435 isl_set *dom;
1436 isl_space *map_dim;
1437 isl_space *pwf_dim;
1438 unsigned n_in;
1439 int ok;
1441 ctx = isl_map_get_ctx(map);
1442 if (!ctx)
1443 goto error;
1445 map_dim = isl_map_get_space(map);
1446 pwf_dim = isl_pw_qpolynomial_fold_get_space(pwf);
1447 ok = join_compatible(map_dim, pwf_dim);
1448 isl_space_free(map_dim);
1449 isl_space_free(pwf_dim);
1450 if (!ok)
1451 isl_die(ctx, isl_error_invalid, "incompatible dimensions",
1452 goto error);
1454 n_in = isl_map_dim(map, isl_dim_in);
1455 pwf = isl_pw_qpolynomial_fold_insert_dims(pwf, isl_dim_in, 0, n_in);
1457 dom = isl_map_wrap(map);
1458 pwf = isl_pw_qpolynomial_fold_reset_domain_space(pwf,
1459 isl_set_get_space(dom));
1461 pwf = isl_pw_qpolynomial_fold_intersect_domain(pwf, dom);
1462 pwf = isl_pw_qpolynomial_fold_bound(pwf, tight);
1464 return pwf;
1465 error:
1466 isl_map_free(map);
1467 isl_pw_qpolynomial_fold_free(pwf);
1468 return NULL;
1471 __isl_give isl_pw_qpolynomial_fold *isl_set_apply_pw_qpolynomial_fold(
1472 __isl_take isl_set *set, __isl_take isl_pw_qpolynomial_fold *pwf,
1473 int *tight)
1475 return isl_map_apply_pw_qpolynomial_fold(set, pwf, tight);
1478 struct isl_apply_fold_data {
1479 isl_union_pw_qpolynomial_fold *upwf;
1480 isl_union_pw_qpolynomial_fold *res;
1481 isl_map *map;
1482 int tight;
1485 static int pw_qpolynomial_fold_apply(__isl_take isl_pw_qpolynomial_fold *pwf,
1486 void *user)
1488 isl_space *map_dim;
1489 isl_space *pwf_dim;
1490 struct isl_apply_fold_data *data = user;
1491 int ok;
1493 map_dim = isl_map_get_space(data->map);
1494 pwf_dim = isl_pw_qpolynomial_fold_get_space(pwf);
1495 ok = join_compatible(map_dim, pwf_dim);
1496 isl_space_free(map_dim);
1497 isl_space_free(pwf_dim);
1499 if (ok) {
1500 pwf = isl_map_apply_pw_qpolynomial_fold(isl_map_copy(data->map),
1501 pwf, data->tight ? &data->tight : NULL);
1502 data->res = isl_union_pw_qpolynomial_fold_fold_pw_qpolynomial_fold(
1503 data->res, pwf);
1504 } else
1505 isl_pw_qpolynomial_fold_free(pwf);
1507 return 0;
1510 static int map_apply(__isl_take isl_map *map, void *user)
1512 struct isl_apply_fold_data *data = user;
1513 int r;
1515 data->map = map;
1516 r = isl_union_pw_qpolynomial_fold_foreach_pw_qpolynomial_fold(
1517 data->upwf, &pw_qpolynomial_fold_apply, data);
1519 isl_map_free(map);
1520 return r;
1523 __isl_give isl_union_pw_qpolynomial_fold *isl_union_map_apply_union_pw_qpolynomial_fold(
1524 __isl_take isl_union_map *umap,
1525 __isl_take isl_union_pw_qpolynomial_fold *upwf, int *tight)
1527 isl_space *dim;
1528 enum isl_fold type;
1529 struct isl_apply_fold_data data;
1531 upwf = isl_union_pw_qpolynomial_fold_align_params(upwf,
1532 isl_union_map_get_space(umap));
1533 umap = isl_union_map_align_params(umap,
1534 isl_union_pw_qpolynomial_fold_get_space(upwf));
1536 data.upwf = upwf;
1537 data.tight = tight ? 1 : 0;
1538 dim = isl_union_pw_qpolynomial_fold_get_space(upwf);
1539 type = isl_union_pw_qpolynomial_fold_get_type(upwf);
1540 data.res = isl_union_pw_qpolynomial_fold_zero(dim, type);
1541 if (isl_union_map_foreach_map(umap, &map_apply, &data) < 0)
1542 goto error;
1544 isl_union_map_free(umap);
1545 isl_union_pw_qpolynomial_fold_free(upwf);
1547 if (tight)
1548 *tight = data.tight;
1550 return data.res;
1551 error:
1552 isl_union_map_free(umap);
1553 isl_union_pw_qpolynomial_fold_free(upwf);
1554 isl_union_pw_qpolynomial_fold_free(data.res);
1555 return NULL;
1558 __isl_give isl_union_pw_qpolynomial_fold *isl_union_set_apply_union_pw_qpolynomial_fold(
1559 __isl_take isl_union_set *uset,
1560 __isl_take isl_union_pw_qpolynomial_fold *upwf, int *tight)
1562 return isl_union_map_apply_union_pw_qpolynomial_fold(uset, upwf, tight);
1565 /* Reorder the dimension of "fold" according to the given reordering.
1567 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_realign_domain(
1568 __isl_take isl_qpolynomial_fold *fold, __isl_take isl_reordering *r)
1570 int i;
1572 fold = isl_qpolynomial_fold_cow(fold);
1573 if (!fold || !r)
1574 goto error;
1576 for (i = 0; i < fold->n; ++i) {
1577 fold->qp[i] = isl_qpolynomial_realign_domain(fold->qp[i],
1578 isl_reordering_copy(r));
1579 if (!fold->qp[i])
1580 goto error;
1583 fold = isl_qpolynomial_fold_reset_domain_space(fold,
1584 isl_space_copy(r->dim));
1586 isl_reordering_free(r);
1588 return fold;
1589 error:
1590 isl_qpolynomial_fold_free(fold);
1591 isl_reordering_free(r);
1592 return NULL;
1595 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_mul_isl_int(
1596 __isl_take isl_qpolynomial_fold *fold, isl_int v)
1598 int i;
1600 if (isl_int_is_one(v))
1601 return fold;
1602 if (fold && isl_int_is_zero(v)) {
1603 isl_qpolynomial_fold *zero;
1604 isl_space *dim = isl_space_copy(fold->dim);
1605 zero = isl_qpolynomial_fold_empty(fold->type, dim);
1606 isl_qpolynomial_fold_free(fold);
1607 return zero;
1610 fold = isl_qpolynomial_fold_cow(fold);
1611 if (!fold)
1612 return NULL;
1614 if (isl_int_is_neg(v))
1615 fold->type = isl_fold_type_negate(fold->type);
1616 for (i = 0; i < fold->n; ++i) {
1617 fold->qp[i] = isl_qpolynomial_mul_isl_int(fold->qp[i], v);
1618 if (!fold->qp[i])
1619 goto error;
1622 return fold;
1623 error:
1624 isl_qpolynomial_fold_free(fold);
1625 return NULL;
1628 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_scale(
1629 __isl_take isl_qpolynomial_fold *fold, isl_int v)
1631 return isl_qpolynomial_fold_mul_isl_int(fold, v);