isl_sample.c: plug_in: add memory management annotations
[isl.git] / isl_fold.c
blob1ba881503783a0850dfd16e0bac881c5b260240e
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 /* Given a dimension type for an isl_qpolynomial_fold,
173 * return the corresponding type for the domain.
175 static enum isl_dim_type domain_type(enum isl_dim_type type)
177 if (type == isl_dim_in)
178 return isl_dim_set;
179 return type;
182 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_drop_dims(
183 __isl_take isl_qpolynomial_fold *fold,
184 enum isl_dim_type type, unsigned first, unsigned n)
186 int i;
187 enum isl_dim_type set_type;
189 if (!fold)
190 return NULL;
191 if (n == 0)
192 return fold;
194 set_type = domain_type(type);
196 fold = isl_qpolynomial_fold_cow(fold);
197 if (!fold)
198 return NULL;
199 fold->dim = isl_space_drop_dims(fold->dim, set_type, first, n);
200 if (!fold->dim)
201 goto error;
203 for (i = 0; i < fold->n; ++i) {
204 fold->qp[i] = isl_qpolynomial_drop_dims(fold->qp[i],
205 type, first, n);
206 if (!fold->qp[i])
207 goto error;
210 return fold;
211 error:
212 isl_qpolynomial_fold_free(fold);
213 return NULL;
216 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_insert_dims(
217 __isl_take isl_qpolynomial_fold *fold,
218 enum isl_dim_type type, unsigned first, unsigned n)
220 int i;
222 if (!fold)
223 return NULL;
224 if (n == 0 && !isl_space_is_named_or_nested(fold->dim, type))
225 return fold;
227 fold = isl_qpolynomial_fold_cow(fold);
228 if (!fold)
229 return NULL;
230 fold->dim = isl_space_insert_dims(fold->dim, type, first, n);
231 if (!fold->dim)
232 goto error;
234 for (i = 0; i < fold->n; ++i) {
235 fold->qp[i] = isl_qpolynomial_insert_dims(fold->qp[i],
236 type, first, n);
237 if (!fold->qp[i])
238 goto error;
241 return fold;
242 error:
243 isl_qpolynomial_fold_free(fold);
244 return NULL;
247 /* Determine the sign of the constant quasipolynomial "qp".
249 * Return
250 * -1 if qp <= 0
251 * 1 if qp >= 0
252 * 0 if unknown
254 * For qp == 0, we can return either -1 or 1. In practice, we return 1.
255 * For qp == NaN, the sign is undefined, so we return 0.
257 static int isl_qpolynomial_cst_sign(__isl_keep isl_qpolynomial *qp)
259 struct isl_upoly_cst *cst;
261 if (isl_qpolynomial_is_nan(qp))
262 return 0;
264 cst = isl_upoly_as_cst(qp->upoly);
265 if (!cst)
266 return 0;
268 return isl_int_sgn(cst->n) < 0 ? -1 : 1;
271 static int isl_qpolynomial_aff_sign(__isl_keep isl_set *set,
272 __isl_keep isl_qpolynomial *qp)
274 enum isl_lp_result res;
275 isl_vec *aff;
276 isl_int opt;
277 int sgn = 0;
279 aff = isl_qpolynomial_extract_affine(qp);
280 if (!aff)
281 return 0;
283 isl_int_init(opt);
285 res = isl_set_solve_lp(set, 0, aff->el + 1, aff->el[0],
286 &opt, NULL, NULL);
287 if (res == isl_lp_error)
288 goto done;
289 if (res == isl_lp_empty ||
290 (res == isl_lp_ok && !isl_int_is_neg(opt))) {
291 sgn = 1;
292 goto done;
295 res = isl_set_solve_lp(set, 1, aff->el + 1, aff->el[0],
296 &opt, NULL, NULL);
297 if (res == isl_lp_ok && !isl_int_is_pos(opt))
298 sgn = -1;
300 done:
301 isl_int_clear(opt);
302 isl_vec_free(aff);
303 return sgn;
306 /* Determine, if possible, the sign of the quasipolynomial "qp" on
307 * the domain "set".
309 * If qp is a constant, then the problem is trivial.
310 * If qp is linear, then we check if the minimum of the corresponding
311 * affine constraint is non-negative or if the maximum is non-positive.
313 * Otherwise, we check if the outermost variable "v" has a lower bound "l"
314 * in "set". If so, we write qp(v,v') as
316 * q(v,v') * (v - l) + r(v')
318 * if q(v,v') and r(v') have the same known sign, then the original
319 * quasipolynomial has the same sign as well.
321 * Return
322 * -1 if qp <= 0
323 * 1 if qp >= 0
324 * 0 if unknown
326 static int isl_qpolynomial_sign(__isl_keep isl_set *set,
327 __isl_keep isl_qpolynomial *qp)
329 int d;
330 int i;
331 int is;
332 struct isl_upoly_rec *rec;
333 isl_vec *v;
334 isl_int l;
335 enum isl_lp_result res;
336 int sgn = 0;
338 is = isl_qpolynomial_is_cst(qp, NULL, NULL);
339 if (is < 0)
340 return 0;
341 if (is)
342 return isl_qpolynomial_cst_sign(qp);
344 is = isl_qpolynomial_is_affine(qp);
345 if (is < 0)
346 return 0;
347 if (is)
348 return isl_qpolynomial_aff_sign(set, qp);
350 if (qp->div->n_row > 0)
351 return 0;
353 rec = isl_upoly_as_rec(qp->upoly);
354 if (!rec)
355 return 0;
357 d = isl_space_dim(qp->dim, isl_dim_all);
358 v = isl_vec_alloc(set->ctx, 2 + d);
359 if (!v)
360 return 0;
362 isl_seq_clr(v->el + 1, 1 + d);
363 isl_int_set_si(v->el[0], 1);
364 isl_int_set_si(v->el[2 + qp->upoly->var], 1);
366 isl_int_init(l);
368 res = isl_set_solve_lp(set, 0, v->el + 1, v->el[0], &l, NULL, NULL);
369 if (res == isl_lp_ok) {
370 isl_qpolynomial *min;
371 isl_qpolynomial *base;
372 isl_qpolynomial *r, *q;
373 isl_qpolynomial *t;
375 min = isl_qpolynomial_cst_on_domain(isl_space_copy(qp->dim), l);
376 base = isl_qpolynomial_var_pow_on_domain(isl_space_copy(qp->dim),
377 qp->upoly->var, 1);
379 r = isl_qpolynomial_alloc(isl_space_copy(qp->dim), 0,
380 isl_upoly_copy(rec->p[rec->n - 1]));
381 q = isl_qpolynomial_copy(r);
383 for (i = rec->n - 2; i >= 0; --i) {
384 r = isl_qpolynomial_mul(r, isl_qpolynomial_copy(min));
385 t = isl_qpolynomial_alloc(isl_space_copy(qp->dim), 0,
386 isl_upoly_copy(rec->p[i]));
387 r = isl_qpolynomial_add(r, t);
388 if (i == 0)
389 break;
390 q = isl_qpolynomial_mul(q, isl_qpolynomial_copy(base));
391 q = isl_qpolynomial_add(q, isl_qpolynomial_copy(r));
394 if (isl_qpolynomial_is_zero(q))
395 sgn = isl_qpolynomial_sign(set, r);
396 else if (isl_qpolynomial_is_zero(r))
397 sgn = isl_qpolynomial_sign(set, q);
398 else {
399 int sgn_q, sgn_r;
400 sgn_r = isl_qpolynomial_sign(set, r);
401 sgn_q = isl_qpolynomial_sign(set, q);
402 if (sgn_r == sgn_q)
403 sgn = sgn_r;
406 isl_qpolynomial_free(min);
407 isl_qpolynomial_free(base);
408 isl_qpolynomial_free(q);
409 isl_qpolynomial_free(r);
412 isl_int_clear(l);
414 isl_vec_free(v);
416 return sgn;
419 /* Combine "fold1" and "fold2" into a single reduction, eliminating
420 * those elements of one reduction that are already covered by the other
421 * reduction on "set".
423 * If "fold1" or "fold2" is an empty reduction, then return
424 * the other reduction.
425 * If "fold1" or "fold2" is a NaN, then return this NaN.
427 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_fold_on_domain(
428 __isl_keep isl_set *set,
429 __isl_take isl_qpolynomial_fold *fold1,
430 __isl_take isl_qpolynomial_fold *fold2)
432 int i, j;
433 int n1;
434 struct isl_qpolynomial_fold *res = NULL;
435 int better;
437 if (!fold1 || !fold2)
438 goto error;
440 isl_assert(fold1->dim->ctx, fold1->type == fold2->type, goto error);
441 isl_assert(fold1->dim->ctx, isl_space_is_equal(fold1->dim, fold2->dim),
442 goto error);
444 better = fold1->type == isl_fold_max ? -1 : 1;
446 if (isl_qpolynomial_fold_is_empty(fold1) ||
447 isl_qpolynomial_fold_is_nan(fold2)) {
448 isl_qpolynomial_fold_free(fold1);
449 return fold2;
452 if (isl_qpolynomial_fold_is_empty(fold2) ||
453 isl_qpolynomial_fold_is_nan(fold1)) {
454 isl_qpolynomial_fold_free(fold2);
455 return fold1;
458 res = qpolynomial_fold_alloc(fold1->type, isl_space_copy(fold1->dim),
459 fold1->n + fold2->n);
460 if (!res)
461 goto error;
463 for (i = 0; i < fold1->n; ++i) {
464 res->qp[res->n] = isl_qpolynomial_copy(fold1->qp[i]);
465 if (!res->qp[res->n])
466 goto error;
467 res->n++;
469 n1 = res->n;
471 for (i = 0; i < fold2->n; ++i) {
472 for (j = n1 - 1; j >= 0; --j) {
473 isl_qpolynomial *d;
474 int sgn, equal;
475 equal = isl_qpolynomial_plain_is_equal(res->qp[j],
476 fold2->qp[i]);
477 if (equal < 0)
478 goto error;
479 if (equal)
480 break;
481 d = isl_qpolynomial_sub(
482 isl_qpolynomial_copy(res->qp[j]),
483 isl_qpolynomial_copy(fold2->qp[i]));
484 sgn = isl_qpolynomial_sign(set, d);
485 isl_qpolynomial_free(d);
486 if (sgn == 0)
487 continue;
488 if (sgn != better)
489 break;
490 isl_qpolynomial_free(res->qp[j]);
491 if (j != n1 - 1)
492 res->qp[j] = res->qp[n1 - 1];
493 n1--;
494 if (n1 != res->n - 1)
495 res->qp[n1] = res->qp[res->n - 1];
496 res->n--;
498 if (j >= 0)
499 continue;
500 res->qp[res->n] = isl_qpolynomial_copy(fold2->qp[i]);
501 if (!res->qp[res->n])
502 goto error;
503 res->n++;
506 isl_qpolynomial_fold_free(fold1);
507 isl_qpolynomial_fold_free(fold2);
509 return res;
510 error:
511 isl_qpolynomial_fold_free(res);
512 isl_qpolynomial_fold_free(fold1);
513 isl_qpolynomial_fold_free(fold2);
514 return NULL;
517 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_add_qpolynomial(
518 __isl_take isl_qpolynomial_fold *fold, __isl_take isl_qpolynomial *qp)
520 int i;
522 if (!fold || !qp)
523 goto error;
525 if (isl_qpolynomial_is_zero(qp)) {
526 isl_qpolynomial_free(qp);
527 return fold;
530 fold = isl_qpolynomial_fold_cow(fold);
531 if (!fold)
532 goto error;
534 for (i = 0; i < fold->n; ++i) {
535 fold->qp[i] = isl_qpolynomial_add(fold->qp[i],
536 isl_qpolynomial_copy(qp));
537 if (!fold->qp[i])
538 goto error;
541 isl_qpolynomial_free(qp);
542 return fold;
543 error:
544 isl_qpolynomial_fold_free(fold);
545 isl_qpolynomial_free(qp);
546 return NULL;
549 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_add_on_domain(
550 __isl_keep isl_set *dom,
551 __isl_take isl_qpolynomial_fold *fold1,
552 __isl_take isl_qpolynomial_fold *fold2)
554 int i;
555 isl_qpolynomial_fold *res = NULL;
557 if (!fold1 || !fold2)
558 goto error;
560 if (isl_qpolynomial_fold_is_empty(fold1)) {
561 isl_qpolynomial_fold_free(fold1);
562 return fold2;
565 if (isl_qpolynomial_fold_is_empty(fold2)) {
566 isl_qpolynomial_fold_free(fold2);
567 return fold1;
570 if (fold1->n == 1 && fold2->n != 1)
571 return isl_qpolynomial_fold_add_on_domain(dom, fold2, fold1);
573 if (fold2->n == 1) {
574 res = isl_qpolynomial_fold_add_qpolynomial(fold1,
575 isl_qpolynomial_copy(fold2->qp[0]));
576 isl_qpolynomial_fold_free(fold2);
577 return res;
580 res = isl_qpolynomial_fold_add_qpolynomial(
581 isl_qpolynomial_fold_copy(fold1),
582 isl_qpolynomial_copy(fold2->qp[0]));
584 for (i = 1; i < fold2->n; ++i) {
585 isl_qpolynomial_fold *res_i;
586 res_i = isl_qpolynomial_fold_add_qpolynomial(
587 isl_qpolynomial_fold_copy(fold1),
588 isl_qpolynomial_copy(fold2->qp[i]));
589 res = isl_qpolynomial_fold_fold_on_domain(dom, res, res_i);
592 isl_qpolynomial_fold_free(fold1);
593 isl_qpolynomial_fold_free(fold2);
594 return res;
595 error:
596 isl_qpolynomial_fold_free(res);
597 isl_qpolynomial_fold_free(fold1);
598 isl_qpolynomial_fold_free(fold2);
599 return NULL;
602 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_substitute_equalities(
603 __isl_take isl_qpolynomial_fold *fold, __isl_take isl_basic_set *eq)
605 int i;
607 if (!fold || !eq)
608 goto error;
610 fold = isl_qpolynomial_fold_cow(fold);
611 if (!fold)
612 return NULL;
614 for (i = 0; i < fold->n; ++i) {
615 fold->qp[i] = isl_qpolynomial_substitute_equalities(fold->qp[i],
616 isl_basic_set_copy(eq));
617 if (!fold->qp[i])
618 goto error;
621 isl_basic_set_free(eq);
622 return fold;
623 error:
624 isl_basic_set_free(eq);
625 isl_qpolynomial_fold_free(fold);
626 return NULL;
629 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_gist(
630 __isl_take isl_qpolynomial_fold *fold, __isl_take isl_set *context)
632 int i;
634 if (!fold || !context)
635 goto error;
637 fold = isl_qpolynomial_fold_cow(fold);
638 if (!fold)
639 return NULL;
641 for (i = 0; i < fold->n; ++i) {
642 fold->qp[i] = isl_qpolynomial_gist(fold->qp[i],
643 isl_set_copy(context));
644 if (!fold->qp[i])
645 goto error;
648 isl_set_free(context);
649 return fold;
650 error:
651 isl_set_free(context);
652 isl_qpolynomial_fold_free(fold);
653 return NULL;
656 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_gist_params(
657 __isl_take isl_qpolynomial_fold *fold, __isl_take isl_set *context)
659 isl_space *space = isl_qpolynomial_fold_get_domain_space(fold);
660 isl_set *dom_context = isl_set_universe(space);
661 dom_context = isl_set_intersect_params(dom_context, context);
662 return isl_qpolynomial_fold_gist(fold, dom_context);
665 #define isl_qpolynomial_fold_involves_nan isl_qpolynomial_fold_is_nan
667 #define HAS_TYPE
669 #undef PW
670 #define PW isl_pw_qpolynomial_fold
671 #undef EL
672 #define EL isl_qpolynomial_fold
673 #undef EL_IS_ZERO
674 #define EL_IS_ZERO is_empty
675 #undef ZERO
676 #define ZERO zero
677 #undef IS_ZERO
678 #define IS_ZERO is_zero
679 #undef FIELD
680 #define FIELD fold
681 #undef DEFAULT_IS_ZERO
682 #define DEFAULT_IS_ZERO 1
684 #define NO_NEG
685 #define NO_SUB
686 #define NO_PULLBACK
688 #include <isl_pw_templ.c>
690 #undef UNION
691 #define UNION isl_union_pw_qpolynomial_fold
692 #undef PART
693 #define PART isl_pw_qpolynomial_fold
694 #undef PARTS
695 #define PARTS pw_qpolynomial_fold
697 #define NO_SUB
699 #include <isl_union_single.c>
700 #include <isl_union_eval.c>
702 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_empty(enum isl_fold type,
703 __isl_take isl_space *dim)
705 return qpolynomial_fold_alloc(type, dim, 0);
708 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_alloc(
709 enum isl_fold type, __isl_take isl_qpolynomial *qp)
711 isl_qpolynomial_fold *fold;
713 if (!qp)
714 return NULL;
716 fold = qpolynomial_fold_alloc(type, isl_space_copy(qp->dim), 1);
717 if (!fold)
718 goto error;
720 fold->qp[0] = qp;
721 fold->n++;
723 return fold;
724 error:
725 isl_qpolynomial_fold_free(fold);
726 isl_qpolynomial_free(qp);
727 return NULL;
730 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_copy(
731 __isl_keep isl_qpolynomial_fold *fold)
733 if (!fold)
734 return NULL;
736 fold->ref++;
737 return fold;
740 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_dup(
741 __isl_keep isl_qpolynomial_fold *fold)
743 int i;
744 isl_qpolynomial_fold *dup;
746 if (!fold)
747 return NULL;
748 dup = qpolynomial_fold_alloc(fold->type,
749 isl_space_copy(fold->dim), fold->n);
750 if (!dup)
751 return NULL;
753 dup->n = fold->n;
754 for (i = 0; i < fold->n; ++i) {
755 dup->qp[i] = isl_qpolynomial_copy(fold->qp[i]);
756 if (!dup->qp[i])
757 goto error;
760 return dup;
761 error:
762 isl_qpolynomial_fold_free(dup);
763 return NULL;
766 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_cow(
767 __isl_take isl_qpolynomial_fold *fold)
769 if (!fold)
770 return NULL;
772 if (fold->ref == 1)
773 return fold;
774 fold->ref--;
775 return isl_qpolynomial_fold_dup(fold);
778 void isl_qpolynomial_fold_free(__isl_take isl_qpolynomial_fold *fold)
780 int i;
782 if (!fold)
783 return;
784 if (--fold->ref > 0)
785 return;
787 for (i = 0; i < fold->n; ++i)
788 isl_qpolynomial_free(fold->qp[i]);
789 isl_space_free(fold->dim);
790 free(fold);
793 int isl_qpolynomial_fold_is_empty(__isl_keep isl_qpolynomial_fold *fold)
795 if (!fold)
796 return -1;
798 return fold->n == 0;
801 /* Does "fold" represent max(NaN) or min(NaN)?
803 isl_bool isl_qpolynomial_fold_is_nan(__isl_keep isl_qpolynomial_fold *fold)
805 if (!fold)
806 return isl_bool_error;
807 if (fold->n != 1)
808 return isl_bool_false;
809 return isl_qpolynomial_is_nan(fold->qp[0]);
812 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_fold(
813 __isl_take isl_qpolynomial_fold *fold1,
814 __isl_take isl_qpolynomial_fold *fold2)
816 int i;
817 struct isl_qpolynomial_fold *res = NULL;
819 if (!fold1 || !fold2)
820 goto error;
822 isl_assert(fold1->dim->ctx, fold1->type == fold2->type, goto error);
823 isl_assert(fold1->dim->ctx, isl_space_is_equal(fold1->dim, fold2->dim),
824 goto error);
826 if (isl_qpolynomial_fold_is_empty(fold1)) {
827 isl_qpolynomial_fold_free(fold1);
828 return fold2;
831 if (isl_qpolynomial_fold_is_empty(fold2)) {
832 isl_qpolynomial_fold_free(fold2);
833 return fold1;
836 res = qpolynomial_fold_alloc(fold1->type, isl_space_copy(fold1->dim),
837 fold1->n + fold2->n);
838 if (!res)
839 goto error;
841 for (i = 0; i < fold1->n; ++i) {
842 res->qp[res->n] = isl_qpolynomial_copy(fold1->qp[i]);
843 if (!res->qp[res->n])
844 goto error;
845 res->n++;
848 for (i = 0; i < fold2->n; ++i) {
849 res->qp[res->n] = isl_qpolynomial_copy(fold2->qp[i]);
850 if (!res->qp[res->n])
851 goto error;
852 res->n++;
855 isl_qpolynomial_fold_free(fold1);
856 isl_qpolynomial_fold_free(fold2);
858 return res;
859 error:
860 isl_qpolynomial_fold_free(res);
861 isl_qpolynomial_fold_free(fold1);
862 isl_qpolynomial_fold_free(fold2);
863 return NULL;
866 __isl_give isl_pw_qpolynomial_fold *isl_pw_qpolynomial_fold_fold(
867 __isl_take isl_pw_qpolynomial_fold *pw1,
868 __isl_take isl_pw_qpolynomial_fold *pw2)
870 int i, j, n;
871 struct isl_pw_qpolynomial_fold *res;
872 isl_set *set;
874 if (!pw1 || !pw2)
875 goto error;
877 isl_assert(pw1->dim->ctx, isl_space_is_equal(pw1->dim, pw2->dim), goto error);
879 if (isl_pw_qpolynomial_fold_is_zero(pw1)) {
880 isl_pw_qpolynomial_fold_free(pw1);
881 return pw2;
884 if (isl_pw_qpolynomial_fold_is_zero(pw2)) {
885 isl_pw_qpolynomial_fold_free(pw2);
886 return pw1;
889 if (pw1->type != pw2->type)
890 isl_die(pw1->dim->ctx, isl_error_invalid,
891 "fold types don't match", goto error);
893 n = (pw1->n + 1) * (pw2->n + 1);
894 res = isl_pw_qpolynomial_fold_alloc_size(isl_space_copy(pw1->dim),
895 pw1->type, n);
897 for (i = 0; i < pw1->n; ++i) {
898 set = isl_set_copy(pw1->p[i].set);
899 for (j = 0; j < pw2->n; ++j) {
900 struct isl_set *common;
901 isl_qpolynomial_fold *sum;
902 set = isl_set_subtract(set,
903 isl_set_copy(pw2->p[j].set));
904 common = isl_set_intersect(isl_set_copy(pw1->p[i].set),
905 isl_set_copy(pw2->p[j].set));
906 if (isl_set_plain_is_empty(common)) {
907 isl_set_free(common);
908 continue;
911 sum = isl_qpolynomial_fold_fold_on_domain(common,
912 isl_qpolynomial_fold_copy(pw1->p[i].fold),
913 isl_qpolynomial_fold_copy(pw2->p[j].fold));
915 res = isl_pw_qpolynomial_fold_add_piece(res, common, sum);
917 res = isl_pw_qpolynomial_fold_add_piece(res, set,
918 isl_qpolynomial_fold_copy(pw1->p[i].fold));
921 for (j = 0; j < pw2->n; ++j) {
922 set = isl_set_copy(pw2->p[j].set);
923 for (i = 0; i < pw1->n; ++i)
924 set = isl_set_subtract(set, isl_set_copy(pw1->p[i].set));
925 res = isl_pw_qpolynomial_fold_add_piece(res, set,
926 isl_qpolynomial_fold_copy(pw2->p[j].fold));
929 isl_pw_qpolynomial_fold_free(pw1);
930 isl_pw_qpolynomial_fold_free(pw2);
932 return res;
933 error:
934 isl_pw_qpolynomial_fold_free(pw1);
935 isl_pw_qpolynomial_fold_free(pw2);
936 return NULL;
939 __isl_give isl_union_pw_qpolynomial_fold *isl_union_pw_qpolynomial_fold_fold_pw_qpolynomial_fold(
940 __isl_take isl_union_pw_qpolynomial_fold *u,
941 __isl_take isl_pw_qpolynomial_fold *part)
943 struct isl_hash_table_entry *entry;
945 u = isl_union_pw_qpolynomial_fold_cow(u);
947 if (!part || !u)
948 goto error;
949 if (isl_space_check_equal_params(part->dim, u->space) < 0)
950 goto error;
952 entry = isl_union_pw_qpolynomial_fold_find_part_entry(u, part->dim, 1);
953 if (!entry)
954 goto error;
956 if (!entry->data)
957 entry->data = part;
958 else {
959 entry->data = isl_pw_qpolynomial_fold_fold(entry->data,
960 isl_pw_qpolynomial_fold_copy(part));
961 if (!entry->data)
962 goto error;
963 isl_pw_qpolynomial_fold_free(part);
966 return u;
967 error:
968 isl_pw_qpolynomial_fold_free(part);
969 isl_union_pw_qpolynomial_fold_free(u);
970 return NULL;
973 static isl_stat fold_part(__isl_take isl_pw_qpolynomial_fold *part, void *user)
975 isl_union_pw_qpolynomial_fold **u;
976 u = (isl_union_pw_qpolynomial_fold **)user;
978 *u = isl_union_pw_qpolynomial_fold_fold_pw_qpolynomial_fold(*u, part);
980 return isl_stat_ok;
983 __isl_give isl_union_pw_qpolynomial_fold *isl_union_pw_qpolynomial_fold_fold(
984 __isl_take isl_union_pw_qpolynomial_fold *u1,
985 __isl_take isl_union_pw_qpolynomial_fold *u2)
987 u1 = isl_union_pw_qpolynomial_fold_cow(u1);
989 if (!u1 || !u2)
990 goto error;
992 if (isl_union_pw_qpolynomial_fold_foreach_pw_qpolynomial_fold(u2,
993 &fold_part, &u1) < 0)
994 goto error;
996 isl_union_pw_qpolynomial_fold_free(u2);
998 return u1;
999 error:
1000 isl_union_pw_qpolynomial_fold_free(u1);
1001 isl_union_pw_qpolynomial_fold_free(u2);
1002 return NULL;
1005 __isl_give isl_pw_qpolynomial_fold *isl_pw_qpolynomial_fold_from_pw_qpolynomial(
1006 enum isl_fold type, __isl_take isl_pw_qpolynomial *pwqp)
1008 int i;
1009 isl_pw_qpolynomial_fold *pwf;
1011 if (!pwqp)
1012 return NULL;
1014 pwf = isl_pw_qpolynomial_fold_alloc_size(isl_space_copy(pwqp->dim),
1015 type, pwqp->n);
1017 for (i = 0; i < pwqp->n; ++i)
1018 pwf = isl_pw_qpolynomial_fold_add_piece(pwf,
1019 isl_set_copy(pwqp->p[i].set),
1020 isl_qpolynomial_fold_alloc(type,
1021 isl_qpolynomial_copy(pwqp->p[i].qp)));
1023 isl_pw_qpolynomial_free(pwqp);
1025 return pwf;
1028 __isl_give isl_pw_qpolynomial_fold *isl_pw_qpolynomial_fold_add(
1029 __isl_take isl_pw_qpolynomial_fold *pwf1,
1030 __isl_take isl_pw_qpolynomial_fold *pwf2)
1032 return isl_pw_qpolynomial_fold_union_add_(pwf1, pwf2);
1035 /* Compare two quasi-polynomial reductions.
1037 * Return -1 if "fold1" is "smaller" than "fold2", 1 if "fold1" is "greater"
1038 * than "fold2" and 0 if they are equal.
1040 int isl_qpolynomial_fold_plain_cmp(__isl_keep isl_qpolynomial_fold *fold1,
1041 __isl_keep isl_qpolynomial_fold *fold2)
1043 int i;
1045 if (fold1 == fold2)
1046 return 0;
1047 if (!fold1)
1048 return -1;
1049 if (!fold2)
1050 return 1;
1052 if (fold1->n != fold2->n)
1053 return fold1->n - fold2->n;
1055 for (i = 0; i < fold1->n; ++i) {
1056 int cmp;
1058 cmp = isl_qpolynomial_plain_cmp(fold1->qp[i], fold2->qp[i]);
1059 if (cmp != 0)
1060 return cmp;
1063 return 0;
1066 int isl_qpolynomial_fold_plain_is_equal(__isl_keep isl_qpolynomial_fold *fold1,
1067 __isl_keep isl_qpolynomial_fold *fold2)
1069 int i;
1071 if (!fold1 || !fold2)
1072 return -1;
1074 if (fold1->n != fold2->n)
1075 return 0;
1077 /* We probably want to sort the qps first... */
1078 for (i = 0; i < fold1->n; ++i) {
1079 int eq = isl_qpolynomial_plain_is_equal(fold1->qp[i], fold2->qp[i]);
1080 if (eq < 0 || !eq)
1081 return eq;
1084 return 1;
1087 __isl_give isl_val *isl_qpolynomial_fold_eval(
1088 __isl_take isl_qpolynomial_fold *fold, __isl_take isl_point *pnt)
1090 isl_ctx *ctx;
1091 isl_val *v;
1093 if (!fold || !pnt)
1094 goto error;
1095 ctx = isl_point_get_ctx(pnt);
1096 isl_assert(pnt->dim->ctx, isl_space_is_equal(pnt->dim, fold->dim), goto error);
1097 isl_assert(pnt->dim->ctx,
1098 fold->type == isl_fold_max || fold->type == isl_fold_min,
1099 goto error);
1101 if (fold->n == 0)
1102 v = isl_val_zero(ctx);
1103 else {
1104 int i;
1105 v = isl_qpolynomial_eval(isl_qpolynomial_copy(fold->qp[0]),
1106 isl_point_copy(pnt));
1107 for (i = 1; i < fold->n; ++i) {
1108 isl_val *v_i;
1109 v_i = isl_qpolynomial_eval(
1110 isl_qpolynomial_copy(fold->qp[i]),
1111 isl_point_copy(pnt));
1112 if (fold->type == isl_fold_max)
1113 v = isl_val_max(v, v_i);
1114 else
1115 v = isl_val_min(v, v_i);
1118 isl_qpolynomial_fold_free(fold);
1119 isl_point_free(pnt);
1121 return v;
1122 error:
1123 isl_qpolynomial_fold_free(fold);
1124 isl_point_free(pnt);
1125 return NULL;
1128 size_t isl_pw_qpolynomial_fold_size(__isl_keep isl_pw_qpolynomial_fold *pwf)
1130 int i;
1131 size_t n = 0;
1133 for (i = 0; i < pwf->n; ++i)
1134 n += pwf->p[i].fold->n;
1136 return n;
1139 __isl_give isl_val *isl_qpolynomial_fold_opt_on_domain(
1140 __isl_take isl_qpolynomial_fold *fold, __isl_take isl_set *set, int max)
1142 int i;
1143 isl_val *opt;
1145 if (!set || !fold)
1146 goto error;
1148 if (fold->n == 0) {
1149 opt = isl_val_zero(isl_set_get_ctx(set));
1150 isl_set_free(set);
1151 isl_qpolynomial_fold_free(fold);
1152 return opt;
1155 opt = isl_qpolynomial_opt_on_domain(isl_qpolynomial_copy(fold->qp[0]),
1156 isl_set_copy(set), max);
1157 for (i = 1; i < fold->n; ++i) {
1158 isl_val *opt_i;
1159 opt_i = isl_qpolynomial_opt_on_domain(
1160 isl_qpolynomial_copy(fold->qp[i]),
1161 isl_set_copy(set), max);
1162 if (max)
1163 opt = isl_val_max(opt, opt_i);
1164 else
1165 opt = isl_val_min(opt, opt_i);
1168 isl_set_free(set);
1169 isl_qpolynomial_fold_free(fold);
1171 return opt;
1172 error:
1173 isl_set_free(set);
1174 isl_qpolynomial_fold_free(fold);
1175 return NULL;
1178 /* Check whether for each quasi-polynomial in "fold2" there is
1179 * a quasi-polynomial in "fold1" that dominates it on "set".
1181 static int qpolynomial_fold_covers_on_domain(__isl_keep isl_set *set,
1182 __isl_keep isl_qpolynomial_fold *fold1,
1183 __isl_keep isl_qpolynomial_fold *fold2)
1185 int i, j;
1186 int covers;
1188 if (!set || !fold1 || !fold2)
1189 return -1;
1191 covers = fold1->type == isl_fold_max ? 1 : -1;
1193 for (i = 0; i < fold2->n; ++i) {
1194 for (j = 0; j < fold1->n; ++j) {
1195 isl_qpolynomial *d;
1196 int sgn;
1198 d = isl_qpolynomial_sub(
1199 isl_qpolynomial_copy(fold1->qp[j]),
1200 isl_qpolynomial_copy(fold2->qp[i]));
1201 sgn = isl_qpolynomial_sign(set, d);
1202 isl_qpolynomial_free(d);
1203 if (sgn == covers)
1204 break;
1206 if (j >= fold1->n)
1207 return 0;
1210 return 1;
1213 /* Check whether "pwf1" dominated "pwf2", i.e., the domain of "pwf1" contains
1214 * that of "pwf2" and on each cell, the corresponding fold from pwf1 dominates
1215 * that of pwf2.
1217 int isl_pw_qpolynomial_fold_covers(__isl_keep isl_pw_qpolynomial_fold *pwf1,
1218 __isl_keep isl_pw_qpolynomial_fold *pwf2)
1220 int i, j;
1221 isl_set *dom1, *dom2;
1222 int is_subset;
1224 if (!pwf1 || !pwf2)
1225 return -1;
1227 if (pwf2->n == 0)
1228 return 1;
1229 if (pwf1->n == 0)
1230 return 0;
1232 dom1 = isl_pw_qpolynomial_fold_domain(isl_pw_qpolynomial_fold_copy(pwf1));
1233 dom2 = isl_pw_qpolynomial_fold_domain(isl_pw_qpolynomial_fold_copy(pwf2));
1234 is_subset = isl_set_is_subset(dom2, dom1);
1235 isl_set_free(dom1);
1236 isl_set_free(dom2);
1238 if (is_subset < 0 || !is_subset)
1239 return is_subset;
1241 for (i = 0; i < pwf2->n; ++i) {
1242 for (j = 0; j < pwf1->n; ++j) {
1243 int is_empty;
1244 isl_set *common;
1245 int covers;
1247 common = isl_set_intersect(isl_set_copy(pwf1->p[j].set),
1248 isl_set_copy(pwf2->p[i].set));
1249 is_empty = isl_set_is_empty(common);
1250 if (is_empty < 0 || is_empty) {
1251 isl_set_free(common);
1252 if (is_empty < 0)
1253 return -1;
1254 continue;
1256 covers = qpolynomial_fold_covers_on_domain(common,
1257 pwf1->p[j].fold, pwf2->p[i].fold);
1258 isl_set_free(common);
1259 if (covers < 0 || !covers)
1260 return covers;
1264 return 1;
1267 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_morph_domain(
1268 __isl_take isl_qpolynomial_fold *fold, __isl_take isl_morph *morph)
1270 int i;
1271 isl_ctx *ctx;
1273 if (!fold || !morph)
1274 goto error;
1276 ctx = fold->dim->ctx;
1277 isl_assert(ctx, isl_space_is_equal(fold->dim, morph->dom->dim), goto error);
1279 fold = isl_qpolynomial_fold_cow(fold);
1280 if (!fold)
1281 goto error;
1283 isl_space_free(fold->dim);
1284 fold->dim = isl_space_copy(morph->ran->dim);
1285 if (!fold->dim)
1286 goto error;
1288 for (i = 0; i < fold->n; ++i) {
1289 fold->qp[i] = isl_qpolynomial_morph_domain(fold->qp[i],
1290 isl_morph_copy(morph));
1291 if (!fold->qp[i])
1292 goto error;
1295 isl_morph_free(morph);
1297 return fold;
1298 error:
1299 isl_qpolynomial_fold_free(fold);
1300 isl_morph_free(morph);
1301 return NULL;
1304 enum isl_fold isl_qpolynomial_fold_get_type(__isl_keep isl_qpolynomial_fold *fold)
1306 if (!fold)
1307 return isl_fold_list;
1308 return fold->type;
1311 enum isl_fold isl_union_pw_qpolynomial_fold_get_type(
1312 __isl_keep isl_union_pw_qpolynomial_fold *upwf)
1314 if (!upwf)
1315 return isl_fold_list;
1316 return upwf->type;
1319 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_lift(
1320 __isl_take isl_qpolynomial_fold *fold, __isl_take isl_space *dim)
1322 int i;
1324 if (!fold || !dim)
1325 goto error;
1327 if (isl_space_is_equal(fold->dim, dim)) {
1328 isl_space_free(dim);
1329 return fold;
1332 fold = isl_qpolynomial_fold_cow(fold);
1333 if (!fold)
1334 goto error;
1336 isl_space_free(fold->dim);
1337 fold->dim = isl_space_copy(dim);
1338 if (!fold->dim)
1339 goto error;
1341 for (i = 0; i < fold->n; ++i) {
1342 fold->qp[i] = isl_qpolynomial_lift(fold->qp[i],
1343 isl_space_copy(dim));
1344 if (!fold->qp[i])
1345 goto error;
1348 isl_space_free(dim);
1350 return fold;
1351 error:
1352 isl_qpolynomial_fold_free(fold);
1353 isl_space_free(dim);
1354 return NULL;
1357 isl_stat isl_qpolynomial_fold_foreach_qpolynomial(
1358 __isl_keep isl_qpolynomial_fold *fold,
1359 isl_stat (*fn)(__isl_take isl_qpolynomial *qp, void *user), void *user)
1361 int i;
1363 if (!fold)
1364 return isl_stat_error;
1366 for (i = 0; i < fold->n; ++i)
1367 if (fn(isl_qpolynomial_copy(fold->qp[i]), user) < 0)
1368 return isl_stat_error;
1370 return isl_stat_ok;
1373 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_move_dims(
1374 __isl_take isl_qpolynomial_fold *fold,
1375 enum isl_dim_type dst_type, unsigned dst_pos,
1376 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
1378 int i;
1379 enum isl_dim_type set_src_type, set_dst_type;
1381 if (n == 0)
1382 return fold;
1384 fold = isl_qpolynomial_fold_cow(fold);
1385 if (!fold)
1386 return NULL;
1388 set_src_type = domain_type(src_type);
1389 set_dst_type = domain_type(dst_type);
1391 fold->dim = isl_space_move_dims(fold->dim, set_dst_type, dst_pos,
1392 set_src_type, src_pos, n);
1393 if (!fold->dim)
1394 goto error;
1396 for (i = 0; i < fold->n; ++i) {
1397 fold->qp[i] = isl_qpolynomial_move_dims(fold->qp[i],
1398 dst_type, dst_pos, src_type, src_pos, n);
1399 if (!fold->qp[i])
1400 goto error;
1403 return fold;
1404 error:
1405 isl_qpolynomial_fold_free(fold);
1406 return NULL;
1409 /* For each 0 <= i < "n", replace variable "first" + i of type "type"
1410 * in fold->qp[k] by subs[i].
1412 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_substitute(
1413 __isl_take isl_qpolynomial_fold *fold,
1414 enum isl_dim_type type, unsigned first, unsigned n,
1415 __isl_keep isl_qpolynomial **subs)
1417 int i;
1419 if (n == 0)
1420 return fold;
1422 fold = isl_qpolynomial_fold_cow(fold);
1423 if (!fold)
1424 return NULL;
1426 for (i = 0; i < fold->n; ++i) {
1427 fold->qp[i] = isl_qpolynomial_substitute(fold->qp[i],
1428 type, first, n, subs);
1429 if (!fold->qp[i])
1430 goto error;
1433 return fold;
1434 error:
1435 isl_qpolynomial_fold_free(fold);
1436 return NULL;
1439 static isl_stat add_pwqp(__isl_take isl_pw_qpolynomial *pwqp, void *user)
1441 isl_pw_qpolynomial_fold *pwf;
1442 isl_union_pw_qpolynomial_fold **upwf;
1443 struct isl_hash_table_entry *entry;
1445 upwf = (isl_union_pw_qpolynomial_fold **)user;
1447 entry = isl_union_pw_qpolynomial_fold_find_part_entry(*upwf,
1448 pwqp->dim, 1);
1449 if (!entry)
1450 goto error;
1452 pwf = isl_pw_qpolynomial_fold_from_pw_qpolynomial((*upwf)->type, pwqp);
1453 if (!entry->data)
1454 entry->data = pwf;
1455 else {
1456 entry->data = isl_pw_qpolynomial_fold_add(entry->data, pwf);
1457 if (!entry->data)
1458 return isl_stat_error;
1459 if (isl_pw_qpolynomial_fold_is_zero(entry->data))
1460 *upwf = isl_union_pw_qpolynomial_fold_remove_part_entry(
1461 *upwf, entry);
1464 return isl_stat_ok;
1465 error:
1466 isl_pw_qpolynomial_free(pwqp);
1467 return isl_stat_error;
1470 __isl_give isl_union_pw_qpolynomial_fold *isl_union_pw_qpolynomial_fold_add_union_pw_qpolynomial(
1471 __isl_take isl_union_pw_qpolynomial_fold *upwf,
1472 __isl_take isl_union_pw_qpolynomial *upwqp)
1474 upwf = isl_union_pw_qpolynomial_fold_align_params(upwf,
1475 isl_union_pw_qpolynomial_get_space(upwqp));
1476 upwqp = isl_union_pw_qpolynomial_align_params(upwqp,
1477 isl_union_pw_qpolynomial_fold_get_space(upwf));
1479 upwf = isl_union_pw_qpolynomial_fold_cow(upwf);
1480 if (!upwf || !upwqp)
1481 goto error;
1483 if (isl_union_pw_qpolynomial_foreach_pw_qpolynomial(upwqp, &add_pwqp,
1484 &upwf) < 0)
1485 goto error;
1487 isl_union_pw_qpolynomial_free(upwqp);
1489 return upwf;
1490 error:
1491 isl_union_pw_qpolynomial_fold_free(upwf);
1492 isl_union_pw_qpolynomial_free(upwqp);
1493 return NULL;
1496 static isl_bool join_compatible(__isl_keep isl_space *space1,
1497 __isl_keep isl_space *space2)
1499 isl_bool m;
1500 m = isl_space_has_equal_params(space1, space2);
1501 if (m < 0 || !m)
1502 return m;
1503 return isl_space_tuple_is_equal(space1, isl_dim_out,
1504 space2, isl_dim_in);
1507 /* Compute the intersection of the range of the map and the domain
1508 * of the piecewise quasipolynomial reduction and then compute a bound
1509 * on the associated quasipolynomial reduction over all elements
1510 * in this intersection.
1512 * We first introduce some unconstrained dimensions in the
1513 * piecewise quasipolynomial, intersect the resulting domain
1514 * with the wrapped map and the compute the sum.
1516 __isl_give isl_pw_qpolynomial_fold *isl_map_apply_pw_qpolynomial_fold(
1517 __isl_take isl_map *map, __isl_take isl_pw_qpolynomial_fold *pwf,
1518 int *tight)
1520 isl_ctx *ctx;
1521 isl_set *dom;
1522 isl_space *map_dim;
1523 isl_space *pwf_dim;
1524 unsigned n_in;
1525 isl_bool ok;
1527 ctx = isl_map_get_ctx(map);
1528 if (!ctx)
1529 goto error;
1531 map_dim = isl_map_get_space(map);
1532 pwf_dim = isl_pw_qpolynomial_fold_get_space(pwf);
1533 ok = join_compatible(map_dim, pwf_dim);
1534 isl_space_free(map_dim);
1535 isl_space_free(pwf_dim);
1536 if (ok < 0)
1537 goto error;
1538 if (!ok)
1539 isl_die(ctx, isl_error_invalid, "incompatible dimensions",
1540 goto error);
1542 n_in = isl_map_dim(map, isl_dim_in);
1543 pwf = isl_pw_qpolynomial_fold_insert_dims(pwf, isl_dim_in, 0, n_in);
1545 dom = isl_map_wrap(map);
1546 pwf = isl_pw_qpolynomial_fold_reset_domain_space(pwf,
1547 isl_set_get_space(dom));
1549 pwf = isl_pw_qpolynomial_fold_intersect_domain(pwf, dom);
1550 pwf = isl_pw_qpolynomial_fold_bound(pwf, tight);
1552 return pwf;
1553 error:
1554 isl_map_free(map);
1555 isl_pw_qpolynomial_fold_free(pwf);
1556 return NULL;
1559 __isl_give isl_pw_qpolynomial_fold *isl_set_apply_pw_qpolynomial_fold(
1560 __isl_take isl_set *set, __isl_take isl_pw_qpolynomial_fold *pwf,
1561 int *tight)
1563 return isl_map_apply_pw_qpolynomial_fold(set, pwf, tight);
1566 struct isl_apply_fold_data {
1567 isl_union_pw_qpolynomial_fold *upwf;
1568 isl_union_pw_qpolynomial_fold *res;
1569 isl_map *map;
1570 int tight;
1573 static isl_stat pw_qpolynomial_fold_apply(
1574 __isl_take isl_pw_qpolynomial_fold *pwf, void *user)
1576 isl_space *map_dim;
1577 isl_space *pwf_dim;
1578 struct isl_apply_fold_data *data = user;
1579 isl_bool ok;
1581 map_dim = isl_map_get_space(data->map);
1582 pwf_dim = isl_pw_qpolynomial_fold_get_space(pwf);
1583 ok = join_compatible(map_dim, pwf_dim);
1584 isl_space_free(map_dim);
1585 isl_space_free(pwf_dim);
1587 if (ok < 0)
1588 return isl_stat_error;
1589 if (ok) {
1590 pwf = isl_map_apply_pw_qpolynomial_fold(isl_map_copy(data->map),
1591 pwf, data->tight ? &data->tight : NULL);
1592 data->res = isl_union_pw_qpolynomial_fold_fold_pw_qpolynomial_fold(
1593 data->res, pwf);
1594 } else
1595 isl_pw_qpolynomial_fold_free(pwf);
1597 return isl_stat_ok;
1600 static isl_stat map_apply(__isl_take isl_map *map, void *user)
1602 struct isl_apply_fold_data *data = user;
1603 isl_stat r;
1605 data->map = map;
1606 r = isl_union_pw_qpolynomial_fold_foreach_pw_qpolynomial_fold(
1607 data->upwf, &pw_qpolynomial_fold_apply, data);
1609 isl_map_free(map);
1610 return r;
1613 __isl_give isl_union_pw_qpolynomial_fold *isl_union_map_apply_union_pw_qpolynomial_fold(
1614 __isl_take isl_union_map *umap,
1615 __isl_take isl_union_pw_qpolynomial_fold *upwf, int *tight)
1617 isl_space *dim;
1618 enum isl_fold type;
1619 struct isl_apply_fold_data data;
1621 upwf = isl_union_pw_qpolynomial_fold_align_params(upwf,
1622 isl_union_map_get_space(umap));
1623 umap = isl_union_map_align_params(umap,
1624 isl_union_pw_qpolynomial_fold_get_space(upwf));
1626 data.upwf = upwf;
1627 data.tight = tight ? 1 : 0;
1628 dim = isl_union_pw_qpolynomial_fold_get_space(upwf);
1629 type = isl_union_pw_qpolynomial_fold_get_type(upwf);
1630 data.res = isl_union_pw_qpolynomial_fold_zero(dim, type);
1631 if (isl_union_map_foreach_map(umap, &map_apply, &data) < 0)
1632 goto error;
1634 isl_union_map_free(umap);
1635 isl_union_pw_qpolynomial_fold_free(upwf);
1637 if (tight)
1638 *tight = data.tight;
1640 return data.res;
1641 error:
1642 isl_union_map_free(umap);
1643 isl_union_pw_qpolynomial_fold_free(upwf);
1644 isl_union_pw_qpolynomial_fold_free(data.res);
1645 return NULL;
1648 __isl_give isl_union_pw_qpolynomial_fold *isl_union_set_apply_union_pw_qpolynomial_fold(
1649 __isl_take isl_union_set *uset,
1650 __isl_take isl_union_pw_qpolynomial_fold *upwf, int *tight)
1652 return isl_union_map_apply_union_pw_qpolynomial_fold(uset, upwf, tight);
1655 /* Reorder the dimension of "fold" according to the given reordering.
1657 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_realign_domain(
1658 __isl_take isl_qpolynomial_fold *fold, __isl_take isl_reordering *r)
1660 int i;
1662 fold = isl_qpolynomial_fold_cow(fold);
1663 if (!fold || !r)
1664 goto error;
1666 for (i = 0; i < fold->n; ++i) {
1667 fold->qp[i] = isl_qpolynomial_realign_domain(fold->qp[i],
1668 isl_reordering_copy(r));
1669 if (!fold->qp[i])
1670 goto error;
1673 fold = isl_qpolynomial_fold_reset_domain_space(fold,
1674 isl_space_copy(r->dim));
1676 isl_reordering_free(r);
1678 return fold;
1679 error:
1680 isl_qpolynomial_fold_free(fold);
1681 isl_reordering_free(r);
1682 return NULL;
1685 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_mul_isl_int(
1686 __isl_take isl_qpolynomial_fold *fold, isl_int v)
1688 int i;
1690 if (isl_int_is_one(v))
1691 return fold;
1692 if (fold && isl_int_is_zero(v)) {
1693 isl_qpolynomial_fold *zero;
1694 isl_space *dim = isl_space_copy(fold->dim);
1695 zero = isl_qpolynomial_fold_empty(fold->type, dim);
1696 isl_qpolynomial_fold_free(fold);
1697 return zero;
1700 fold = isl_qpolynomial_fold_cow(fold);
1701 if (!fold)
1702 return NULL;
1704 if (isl_int_is_neg(v))
1705 fold->type = isl_fold_type_negate(fold->type);
1706 for (i = 0; i < fold->n; ++i) {
1707 fold->qp[i] = isl_qpolynomial_mul_isl_int(fold->qp[i], v);
1708 if (!fold->qp[i])
1709 goto error;
1712 return fold;
1713 error:
1714 isl_qpolynomial_fold_free(fold);
1715 return NULL;
1718 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_scale(
1719 __isl_take isl_qpolynomial_fold *fold, isl_int v)
1721 return isl_qpolynomial_fold_mul_isl_int(fold, v);
1724 /* Multiply "fold" by "v".
1726 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_scale_val(
1727 __isl_take isl_qpolynomial_fold *fold, __isl_take isl_val *v)
1729 int i;
1731 if (!fold || !v)
1732 goto error;
1734 if (isl_val_is_one(v)) {
1735 isl_val_free(v);
1736 return fold;
1738 if (isl_val_is_zero(v)) {
1739 isl_qpolynomial_fold *zero;
1740 isl_space *space = isl_qpolynomial_fold_get_domain_space(fold);
1741 zero = isl_qpolynomial_fold_empty(fold->type, space);
1742 isl_qpolynomial_fold_free(fold);
1743 isl_val_free(v);
1744 return zero;
1746 if (!isl_val_is_rat(v))
1747 isl_die(isl_qpolynomial_fold_get_ctx(fold), isl_error_invalid,
1748 "expecting rational factor", goto error);
1750 fold = isl_qpolynomial_fold_cow(fold);
1751 if (!fold)
1752 goto error;
1754 if (isl_val_is_neg(v))
1755 fold->type = isl_fold_type_negate(fold->type);
1756 for (i = 0; i < fold->n; ++i) {
1757 fold->qp[i] = isl_qpolynomial_scale_val(fold->qp[i],
1758 isl_val_copy(v));
1759 if (!fold->qp[i])
1760 goto error;
1763 isl_val_free(v);
1764 return fold;
1765 error:
1766 isl_val_free(v);
1767 isl_qpolynomial_fold_free(fold);
1768 return NULL;
1771 /* Divide "fold" by "v".
1773 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_scale_down_val(
1774 __isl_take isl_qpolynomial_fold *fold, __isl_take isl_val *v)
1776 if (!fold || !v)
1777 goto error;
1779 if (isl_val_is_one(v)) {
1780 isl_val_free(v);
1781 return fold;
1783 if (!isl_val_is_rat(v))
1784 isl_die(isl_qpolynomial_fold_get_ctx(fold), isl_error_invalid,
1785 "expecting rational factor", goto error);
1786 if (isl_val_is_zero(v))
1787 isl_die(isl_val_get_ctx(v), isl_error_invalid,
1788 "cannot scale down by zero", goto error);
1790 return isl_qpolynomial_fold_scale_val(fold, isl_val_inv(v));
1791 error:
1792 isl_val_free(v);
1793 isl_qpolynomial_fold_free(fold);
1794 return NULL;