add isl_space_check_named_params
[isl.git] / isl_fold.c
blob42b9868ed55ae3e62af908692fc01e9a10babace
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;
950 isl_assert(u->space->ctx,
951 isl_space_match(part->dim, isl_dim_param, u->space, isl_dim_param),
952 goto error);
954 entry = isl_union_pw_qpolynomial_fold_find_part_entry(u, part->dim, 1);
955 if (!entry)
956 goto error;
958 if (!entry->data)
959 entry->data = part;
960 else {
961 entry->data = isl_pw_qpolynomial_fold_fold(entry->data,
962 isl_pw_qpolynomial_fold_copy(part));
963 if (!entry->data)
964 goto error;
965 isl_pw_qpolynomial_fold_free(part);
968 return u;
969 error:
970 isl_pw_qpolynomial_fold_free(part);
971 isl_union_pw_qpolynomial_fold_free(u);
972 return NULL;
975 static isl_stat fold_part(__isl_take isl_pw_qpolynomial_fold *part, void *user)
977 isl_union_pw_qpolynomial_fold **u;
978 u = (isl_union_pw_qpolynomial_fold **)user;
980 *u = isl_union_pw_qpolynomial_fold_fold_pw_qpolynomial_fold(*u, part);
982 return isl_stat_ok;
985 __isl_give isl_union_pw_qpolynomial_fold *isl_union_pw_qpolynomial_fold_fold(
986 __isl_take isl_union_pw_qpolynomial_fold *u1,
987 __isl_take isl_union_pw_qpolynomial_fold *u2)
989 u1 = isl_union_pw_qpolynomial_fold_cow(u1);
991 if (!u1 || !u2)
992 goto error;
994 if (isl_union_pw_qpolynomial_fold_foreach_pw_qpolynomial_fold(u2,
995 &fold_part, &u1) < 0)
996 goto error;
998 isl_union_pw_qpolynomial_fold_free(u2);
1000 return u1;
1001 error:
1002 isl_union_pw_qpolynomial_fold_free(u1);
1003 isl_union_pw_qpolynomial_fold_free(u2);
1004 return NULL;
1007 __isl_give isl_pw_qpolynomial_fold *isl_pw_qpolynomial_fold_from_pw_qpolynomial(
1008 enum isl_fold type, __isl_take isl_pw_qpolynomial *pwqp)
1010 int i;
1011 isl_pw_qpolynomial_fold *pwf;
1013 if (!pwqp)
1014 return NULL;
1016 pwf = isl_pw_qpolynomial_fold_alloc_size(isl_space_copy(pwqp->dim),
1017 type, pwqp->n);
1019 for (i = 0; i < pwqp->n; ++i)
1020 pwf = isl_pw_qpolynomial_fold_add_piece(pwf,
1021 isl_set_copy(pwqp->p[i].set),
1022 isl_qpolynomial_fold_alloc(type,
1023 isl_qpolynomial_copy(pwqp->p[i].qp)));
1025 isl_pw_qpolynomial_free(pwqp);
1027 return pwf;
1030 __isl_give isl_pw_qpolynomial_fold *isl_pw_qpolynomial_fold_add(
1031 __isl_take isl_pw_qpolynomial_fold *pwf1,
1032 __isl_take isl_pw_qpolynomial_fold *pwf2)
1034 return isl_pw_qpolynomial_fold_union_add_(pwf1, pwf2);
1037 /* Compare two quasi-polynomial reductions.
1039 * Return -1 if "fold1" is "smaller" than "fold2", 1 if "fold1" is "greater"
1040 * than "fold2" and 0 if they are equal.
1042 int isl_qpolynomial_fold_plain_cmp(__isl_keep isl_qpolynomial_fold *fold1,
1043 __isl_keep isl_qpolynomial_fold *fold2)
1045 int i;
1047 if (fold1 == fold2)
1048 return 0;
1049 if (!fold1)
1050 return -1;
1051 if (!fold2)
1052 return 1;
1054 if (fold1->n != fold2->n)
1055 return fold1->n - fold2->n;
1057 for (i = 0; i < fold1->n; ++i) {
1058 int cmp;
1060 cmp = isl_qpolynomial_plain_cmp(fold1->qp[i], fold2->qp[i]);
1061 if (cmp != 0)
1062 return cmp;
1065 return 0;
1068 int isl_qpolynomial_fold_plain_is_equal(__isl_keep isl_qpolynomial_fold *fold1,
1069 __isl_keep isl_qpolynomial_fold *fold2)
1071 int i;
1073 if (!fold1 || !fold2)
1074 return -1;
1076 if (fold1->n != fold2->n)
1077 return 0;
1079 /* We probably want to sort the qps first... */
1080 for (i = 0; i < fold1->n; ++i) {
1081 int eq = isl_qpolynomial_plain_is_equal(fold1->qp[i], fold2->qp[i]);
1082 if (eq < 0 || !eq)
1083 return eq;
1086 return 1;
1089 __isl_give isl_val *isl_qpolynomial_fold_eval(
1090 __isl_take isl_qpolynomial_fold *fold, __isl_take isl_point *pnt)
1092 isl_ctx *ctx;
1093 isl_val *v;
1095 if (!fold || !pnt)
1096 goto error;
1097 ctx = isl_point_get_ctx(pnt);
1098 isl_assert(pnt->dim->ctx, isl_space_is_equal(pnt->dim, fold->dim), goto error);
1099 isl_assert(pnt->dim->ctx,
1100 fold->type == isl_fold_max || fold->type == isl_fold_min,
1101 goto error);
1103 if (fold->n == 0)
1104 v = isl_val_zero(ctx);
1105 else {
1106 int i;
1107 v = isl_qpolynomial_eval(isl_qpolynomial_copy(fold->qp[0]),
1108 isl_point_copy(pnt));
1109 for (i = 1; i < fold->n; ++i) {
1110 isl_val *v_i;
1111 v_i = isl_qpolynomial_eval(
1112 isl_qpolynomial_copy(fold->qp[i]),
1113 isl_point_copy(pnt));
1114 if (fold->type == isl_fold_max)
1115 v = isl_val_max(v, v_i);
1116 else
1117 v = isl_val_min(v, v_i);
1120 isl_qpolynomial_fold_free(fold);
1121 isl_point_free(pnt);
1123 return v;
1124 error:
1125 isl_qpolynomial_fold_free(fold);
1126 isl_point_free(pnt);
1127 return NULL;
1130 size_t isl_pw_qpolynomial_fold_size(__isl_keep isl_pw_qpolynomial_fold *pwf)
1132 int i;
1133 size_t n = 0;
1135 for (i = 0; i < pwf->n; ++i)
1136 n += pwf->p[i].fold->n;
1138 return n;
1141 __isl_give isl_val *isl_qpolynomial_fold_opt_on_domain(
1142 __isl_take isl_qpolynomial_fold *fold, __isl_take isl_set *set, int max)
1144 int i;
1145 isl_val *opt;
1147 if (!set || !fold)
1148 goto error;
1150 if (fold->n == 0) {
1151 opt = isl_val_zero(isl_set_get_ctx(set));
1152 isl_set_free(set);
1153 isl_qpolynomial_fold_free(fold);
1154 return opt;
1157 opt = isl_qpolynomial_opt_on_domain(isl_qpolynomial_copy(fold->qp[0]),
1158 isl_set_copy(set), max);
1159 for (i = 1; i < fold->n; ++i) {
1160 isl_val *opt_i;
1161 opt_i = isl_qpolynomial_opt_on_domain(
1162 isl_qpolynomial_copy(fold->qp[i]),
1163 isl_set_copy(set), max);
1164 if (max)
1165 opt = isl_val_max(opt, opt_i);
1166 else
1167 opt = isl_val_min(opt, opt_i);
1170 isl_set_free(set);
1171 isl_qpolynomial_fold_free(fold);
1173 return opt;
1174 error:
1175 isl_set_free(set);
1176 isl_qpolynomial_fold_free(fold);
1177 return NULL;
1180 /* Check whether for each quasi-polynomial in "fold2" there is
1181 * a quasi-polynomial in "fold1" that dominates it on "set".
1183 static int qpolynomial_fold_covers_on_domain(__isl_keep isl_set *set,
1184 __isl_keep isl_qpolynomial_fold *fold1,
1185 __isl_keep isl_qpolynomial_fold *fold2)
1187 int i, j;
1188 int covers;
1190 if (!set || !fold1 || !fold2)
1191 return -1;
1193 covers = fold1->type == isl_fold_max ? 1 : -1;
1195 for (i = 0; i < fold2->n; ++i) {
1196 for (j = 0; j < fold1->n; ++j) {
1197 isl_qpolynomial *d;
1198 int sgn;
1200 d = isl_qpolynomial_sub(
1201 isl_qpolynomial_copy(fold1->qp[j]),
1202 isl_qpolynomial_copy(fold2->qp[i]));
1203 sgn = isl_qpolynomial_sign(set, d);
1204 isl_qpolynomial_free(d);
1205 if (sgn == covers)
1206 break;
1208 if (j >= fold1->n)
1209 return 0;
1212 return 1;
1215 /* Check whether "pwf1" dominated "pwf2", i.e., the domain of "pwf1" contains
1216 * that of "pwf2" and on each cell, the corresponding fold from pwf1 dominates
1217 * that of pwf2.
1219 int isl_pw_qpolynomial_fold_covers(__isl_keep isl_pw_qpolynomial_fold *pwf1,
1220 __isl_keep isl_pw_qpolynomial_fold *pwf2)
1222 int i, j;
1223 isl_set *dom1, *dom2;
1224 int is_subset;
1226 if (!pwf1 || !pwf2)
1227 return -1;
1229 if (pwf2->n == 0)
1230 return 1;
1231 if (pwf1->n == 0)
1232 return 0;
1234 dom1 = isl_pw_qpolynomial_fold_domain(isl_pw_qpolynomial_fold_copy(pwf1));
1235 dom2 = isl_pw_qpolynomial_fold_domain(isl_pw_qpolynomial_fold_copy(pwf2));
1236 is_subset = isl_set_is_subset(dom2, dom1);
1237 isl_set_free(dom1);
1238 isl_set_free(dom2);
1240 if (is_subset < 0 || !is_subset)
1241 return is_subset;
1243 for (i = 0; i < pwf2->n; ++i) {
1244 for (j = 0; j < pwf1->n; ++j) {
1245 int is_empty;
1246 isl_set *common;
1247 int covers;
1249 common = isl_set_intersect(isl_set_copy(pwf1->p[j].set),
1250 isl_set_copy(pwf2->p[i].set));
1251 is_empty = isl_set_is_empty(common);
1252 if (is_empty < 0 || is_empty) {
1253 isl_set_free(common);
1254 if (is_empty < 0)
1255 return -1;
1256 continue;
1258 covers = qpolynomial_fold_covers_on_domain(common,
1259 pwf1->p[j].fold, pwf2->p[i].fold);
1260 isl_set_free(common);
1261 if (covers < 0 || !covers)
1262 return covers;
1266 return 1;
1269 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_morph_domain(
1270 __isl_take isl_qpolynomial_fold *fold, __isl_take isl_morph *morph)
1272 int i;
1273 isl_ctx *ctx;
1275 if (!fold || !morph)
1276 goto error;
1278 ctx = fold->dim->ctx;
1279 isl_assert(ctx, isl_space_is_equal(fold->dim, morph->dom->dim), goto error);
1281 fold = isl_qpolynomial_fold_cow(fold);
1282 if (!fold)
1283 goto error;
1285 isl_space_free(fold->dim);
1286 fold->dim = isl_space_copy(morph->ran->dim);
1287 if (!fold->dim)
1288 goto error;
1290 for (i = 0; i < fold->n; ++i) {
1291 fold->qp[i] = isl_qpolynomial_morph_domain(fold->qp[i],
1292 isl_morph_copy(morph));
1293 if (!fold->qp[i])
1294 goto error;
1297 isl_morph_free(morph);
1299 return fold;
1300 error:
1301 isl_qpolynomial_fold_free(fold);
1302 isl_morph_free(morph);
1303 return NULL;
1306 enum isl_fold isl_qpolynomial_fold_get_type(__isl_keep isl_qpolynomial_fold *fold)
1308 if (!fold)
1309 return isl_fold_list;
1310 return fold->type;
1313 enum isl_fold isl_union_pw_qpolynomial_fold_get_type(
1314 __isl_keep isl_union_pw_qpolynomial_fold *upwf)
1316 if (!upwf)
1317 return isl_fold_list;
1318 return upwf->type;
1321 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_lift(
1322 __isl_take isl_qpolynomial_fold *fold, __isl_take isl_space *dim)
1324 int i;
1326 if (!fold || !dim)
1327 goto error;
1329 if (isl_space_is_equal(fold->dim, dim)) {
1330 isl_space_free(dim);
1331 return fold;
1334 fold = isl_qpolynomial_fold_cow(fold);
1335 if (!fold)
1336 goto error;
1338 isl_space_free(fold->dim);
1339 fold->dim = isl_space_copy(dim);
1340 if (!fold->dim)
1341 goto error;
1343 for (i = 0; i < fold->n; ++i) {
1344 fold->qp[i] = isl_qpolynomial_lift(fold->qp[i],
1345 isl_space_copy(dim));
1346 if (!fold->qp[i])
1347 goto error;
1350 isl_space_free(dim);
1352 return fold;
1353 error:
1354 isl_qpolynomial_fold_free(fold);
1355 isl_space_free(dim);
1356 return NULL;
1359 isl_stat isl_qpolynomial_fold_foreach_qpolynomial(
1360 __isl_keep isl_qpolynomial_fold *fold,
1361 isl_stat (*fn)(__isl_take isl_qpolynomial *qp, void *user), void *user)
1363 int i;
1365 if (!fold)
1366 return isl_stat_error;
1368 for (i = 0; i < fold->n; ++i)
1369 if (fn(isl_qpolynomial_copy(fold->qp[i]), user) < 0)
1370 return isl_stat_error;
1372 return isl_stat_ok;
1375 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_move_dims(
1376 __isl_take isl_qpolynomial_fold *fold,
1377 enum isl_dim_type dst_type, unsigned dst_pos,
1378 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
1380 int i;
1381 enum isl_dim_type set_src_type, set_dst_type;
1383 if (n == 0)
1384 return fold;
1386 fold = isl_qpolynomial_fold_cow(fold);
1387 if (!fold)
1388 return NULL;
1390 set_src_type = domain_type(src_type);
1391 set_dst_type = domain_type(dst_type);
1393 fold->dim = isl_space_move_dims(fold->dim, set_dst_type, dst_pos,
1394 set_src_type, src_pos, n);
1395 if (!fold->dim)
1396 goto error;
1398 for (i = 0; i < fold->n; ++i) {
1399 fold->qp[i] = isl_qpolynomial_move_dims(fold->qp[i],
1400 dst_type, dst_pos, src_type, src_pos, n);
1401 if (!fold->qp[i])
1402 goto error;
1405 return fold;
1406 error:
1407 isl_qpolynomial_fold_free(fold);
1408 return NULL;
1411 /* For each 0 <= i < "n", replace variable "first" + i of type "type"
1412 * in fold->qp[k] by subs[i].
1414 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_substitute(
1415 __isl_take isl_qpolynomial_fold *fold,
1416 enum isl_dim_type type, unsigned first, unsigned n,
1417 __isl_keep isl_qpolynomial **subs)
1419 int i;
1421 if (n == 0)
1422 return fold;
1424 fold = isl_qpolynomial_fold_cow(fold);
1425 if (!fold)
1426 return NULL;
1428 for (i = 0; i < fold->n; ++i) {
1429 fold->qp[i] = isl_qpolynomial_substitute(fold->qp[i],
1430 type, first, n, subs);
1431 if (!fold->qp[i])
1432 goto error;
1435 return fold;
1436 error:
1437 isl_qpolynomial_fold_free(fold);
1438 return NULL;
1441 static isl_stat add_pwqp(__isl_take isl_pw_qpolynomial *pwqp, void *user)
1443 isl_pw_qpolynomial_fold *pwf;
1444 isl_union_pw_qpolynomial_fold **upwf;
1445 struct isl_hash_table_entry *entry;
1447 upwf = (isl_union_pw_qpolynomial_fold **)user;
1449 entry = isl_union_pw_qpolynomial_fold_find_part_entry(*upwf,
1450 pwqp->dim, 1);
1451 if (!entry)
1452 goto error;
1454 pwf = isl_pw_qpolynomial_fold_from_pw_qpolynomial((*upwf)->type, pwqp);
1455 if (!entry->data)
1456 entry->data = pwf;
1457 else {
1458 entry->data = isl_pw_qpolynomial_fold_add(entry->data, pwf);
1459 if (!entry->data)
1460 return isl_stat_error;
1461 if (isl_pw_qpolynomial_fold_is_zero(entry->data))
1462 *upwf = isl_union_pw_qpolynomial_fold_remove_part_entry(
1463 *upwf, entry);
1466 return isl_stat_ok;
1467 error:
1468 isl_pw_qpolynomial_free(pwqp);
1469 return isl_stat_error;
1472 __isl_give isl_union_pw_qpolynomial_fold *isl_union_pw_qpolynomial_fold_add_union_pw_qpolynomial(
1473 __isl_take isl_union_pw_qpolynomial_fold *upwf,
1474 __isl_take isl_union_pw_qpolynomial *upwqp)
1476 upwf = isl_union_pw_qpolynomial_fold_align_params(upwf,
1477 isl_union_pw_qpolynomial_get_space(upwqp));
1478 upwqp = isl_union_pw_qpolynomial_align_params(upwqp,
1479 isl_union_pw_qpolynomial_fold_get_space(upwf));
1481 upwf = isl_union_pw_qpolynomial_fold_cow(upwf);
1482 if (!upwf || !upwqp)
1483 goto error;
1485 if (isl_union_pw_qpolynomial_foreach_pw_qpolynomial(upwqp, &add_pwqp,
1486 &upwf) < 0)
1487 goto error;
1489 isl_union_pw_qpolynomial_free(upwqp);
1491 return upwf;
1492 error:
1493 isl_union_pw_qpolynomial_fold_free(upwf);
1494 isl_union_pw_qpolynomial_free(upwqp);
1495 return NULL;
1498 static isl_bool join_compatible(__isl_keep isl_space *space1,
1499 __isl_keep isl_space *space2)
1501 isl_bool m;
1502 m = isl_space_match(space1, isl_dim_param, space2, isl_dim_param);
1503 if (m < 0 || !m)
1504 return m;
1505 return isl_space_tuple_is_equal(space1, isl_dim_out,
1506 space2, isl_dim_in);
1509 /* Compute the intersection of the range of the map and the domain
1510 * of the piecewise quasipolynomial reduction and then compute a bound
1511 * on the associated quasipolynomial reduction over all elements
1512 * in this intersection.
1514 * We first introduce some unconstrained dimensions in the
1515 * piecewise quasipolynomial, intersect the resulting domain
1516 * with the wrapped map and the compute the sum.
1518 __isl_give isl_pw_qpolynomial_fold *isl_map_apply_pw_qpolynomial_fold(
1519 __isl_take isl_map *map, __isl_take isl_pw_qpolynomial_fold *pwf,
1520 int *tight)
1522 isl_ctx *ctx;
1523 isl_set *dom;
1524 isl_space *map_dim;
1525 isl_space *pwf_dim;
1526 unsigned n_in;
1527 isl_bool ok;
1529 ctx = isl_map_get_ctx(map);
1530 if (!ctx)
1531 goto error;
1533 map_dim = isl_map_get_space(map);
1534 pwf_dim = isl_pw_qpolynomial_fold_get_space(pwf);
1535 ok = join_compatible(map_dim, pwf_dim);
1536 isl_space_free(map_dim);
1537 isl_space_free(pwf_dim);
1538 if (ok < 0)
1539 goto error;
1540 if (!ok)
1541 isl_die(ctx, isl_error_invalid, "incompatible dimensions",
1542 goto error);
1544 n_in = isl_map_dim(map, isl_dim_in);
1545 pwf = isl_pw_qpolynomial_fold_insert_dims(pwf, isl_dim_in, 0, n_in);
1547 dom = isl_map_wrap(map);
1548 pwf = isl_pw_qpolynomial_fold_reset_domain_space(pwf,
1549 isl_set_get_space(dom));
1551 pwf = isl_pw_qpolynomial_fold_intersect_domain(pwf, dom);
1552 pwf = isl_pw_qpolynomial_fold_bound(pwf, tight);
1554 return pwf;
1555 error:
1556 isl_map_free(map);
1557 isl_pw_qpolynomial_fold_free(pwf);
1558 return NULL;
1561 __isl_give isl_pw_qpolynomial_fold *isl_set_apply_pw_qpolynomial_fold(
1562 __isl_take isl_set *set, __isl_take isl_pw_qpolynomial_fold *pwf,
1563 int *tight)
1565 return isl_map_apply_pw_qpolynomial_fold(set, pwf, tight);
1568 struct isl_apply_fold_data {
1569 isl_union_pw_qpolynomial_fold *upwf;
1570 isl_union_pw_qpolynomial_fold *res;
1571 isl_map *map;
1572 int tight;
1575 static isl_stat pw_qpolynomial_fold_apply(
1576 __isl_take isl_pw_qpolynomial_fold *pwf, void *user)
1578 isl_space *map_dim;
1579 isl_space *pwf_dim;
1580 struct isl_apply_fold_data *data = user;
1581 isl_bool ok;
1583 map_dim = isl_map_get_space(data->map);
1584 pwf_dim = isl_pw_qpolynomial_fold_get_space(pwf);
1585 ok = join_compatible(map_dim, pwf_dim);
1586 isl_space_free(map_dim);
1587 isl_space_free(pwf_dim);
1589 if (ok < 0)
1590 return isl_stat_error;
1591 if (ok) {
1592 pwf = isl_map_apply_pw_qpolynomial_fold(isl_map_copy(data->map),
1593 pwf, data->tight ? &data->tight : NULL);
1594 data->res = isl_union_pw_qpolynomial_fold_fold_pw_qpolynomial_fold(
1595 data->res, pwf);
1596 } else
1597 isl_pw_qpolynomial_fold_free(pwf);
1599 return isl_stat_ok;
1602 static isl_stat map_apply(__isl_take isl_map *map, void *user)
1604 struct isl_apply_fold_data *data = user;
1605 isl_stat r;
1607 data->map = map;
1608 r = isl_union_pw_qpolynomial_fold_foreach_pw_qpolynomial_fold(
1609 data->upwf, &pw_qpolynomial_fold_apply, data);
1611 isl_map_free(map);
1612 return r;
1615 __isl_give isl_union_pw_qpolynomial_fold *isl_union_map_apply_union_pw_qpolynomial_fold(
1616 __isl_take isl_union_map *umap,
1617 __isl_take isl_union_pw_qpolynomial_fold *upwf, int *tight)
1619 isl_space *dim;
1620 enum isl_fold type;
1621 struct isl_apply_fold_data data;
1623 upwf = isl_union_pw_qpolynomial_fold_align_params(upwf,
1624 isl_union_map_get_space(umap));
1625 umap = isl_union_map_align_params(umap,
1626 isl_union_pw_qpolynomial_fold_get_space(upwf));
1628 data.upwf = upwf;
1629 data.tight = tight ? 1 : 0;
1630 dim = isl_union_pw_qpolynomial_fold_get_space(upwf);
1631 type = isl_union_pw_qpolynomial_fold_get_type(upwf);
1632 data.res = isl_union_pw_qpolynomial_fold_zero(dim, type);
1633 if (isl_union_map_foreach_map(umap, &map_apply, &data) < 0)
1634 goto error;
1636 isl_union_map_free(umap);
1637 isl_union_pw_qpolynomial_fold_free(upwf);
1639 if (tight)
1640 *tight = data.tight;
1642 return data.res;
1643 error:
1644 isl_union_map_free(umap);
1645 isl_union_pw_qpolynomial_fold_free(upwf);
1646 isl_union_pw_qpolynomial_fold_free(data.res);
1647 return NULL;
1650 __isl_give isl_union_pw_qpolynomial_fold *isl_union_set_apply_union_pw_qpolynomial_fold(
1651 __isl_take isl_union_set *uset,
1652 __isl_take isl_union_pw_qpolynomial_fold *upwf, int *tight)
1654 return isl_union_map_apply_union_pw_qpolynomial_fold(uset, upwf, tight);
1657 /* Reorder the dimension of "fold" according to the given reordering.
1659 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_realign_domain(
1660 __isl_take isl_qpolynomial_fold *fold, __isl_take isl_reordering *r)
1662 int i;
1664 fold = isl_qpolynomial_fold_cow(fold);
1665 if (!fold || !r)
1666 goto error;
1668 for (i = 0; i < fold->n; ++i) {
1669 fold->qp[i] = isl_qpolynomial_realign_domain(fold->qp[i],
1670 isl_reordering_copy(r));
1671 if (!fold->qp[i])
1672 goto error;
1675 fold = isl_qpolynomial_fold_reset_domain_space(fold,
1676 isl_space_copy(r->dim));
1678 isl_reordering_free(r);
1680 return fold;
1681 error:
1682 isl_qpolynomial_fold_free(fold);
1683 isl_reordering_free(r);
1684 return NULL;
1687 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_mul_isl_int(
1688 __isl_take isl_qpolynomial_fold *fold, isl_int v)
1690 int i;
1692 if (isl_int_is_one(v))
1693 return fold;
1694 if (fold && isl_int_is_zero(v)) {
1695 isl_qpolynomial_fold *zero;
1696 isl_space *dim = isl_space_copy(fold->dim);
1697 zero = isl_qpolynomial_fold_empty(fold->type, dim);
1698 isl_qpolynomial_fold_free(fold);
1699 return zero;
1702 fold = isl_qpolynomial_fold_cow(fold);
1703 if (!fold)
1704 return NULL;
1706 if (isl_int_is_neg(v))
1707 fold->type = isl_fold_type_negate(fold->type);
1708 for (i = 0; i < fold->n; ++i) {
1709 fold->qp[i] = isl_qpolynomial_mul_isl_int(fold->qp[i], v);
1710 if (!fold->qp[i])
1711 goto error;
1714 return fold;
1715 error:
1716 isl_qpolynomial_fold_free(fold);
1717 return NULL;
1720 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_scale(
1721 __isl_take isl_qpolynomial_fold *fold, isl_int v)
1723 return isl_qpolynomial_fold_mul_isl_int(fold, v);
1726 /* Multiply "fold" by "v".
1728 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_scale_val(
1729 __isl_take isl_qpolynomial_fold *fold, __isl_take isl_val *v)
1731 int i;
1733 if (!fold || !v)
1734 goto error;
1736 if (isl_val_is_one(v)) {
1737 isl_val_free(v);
1738 return fold;
1740 if (isl_val_is_zero(v)) {
1741 isl_qpolynomial_fold *zero;
1742 isl_space *space = isl_qpolynomial_fold_get_domain_space(fold);
1743 zero = isl_qpolynomial_fold_empty(fold->type, space);
1744 isl_qpolynomial_fold_free(fold);
1745 isl_val_free(v);
1746 return zero;
1748 if (!isl_val_is_rat(v))
1749 isl_die(isl_qpolynomial_fold_get_ctx(fold), isl_error_invalid,
1750 "expecting rational factor", goto error);
1752 fold = isl_qpolynomial_fold_cow(fold);
1753 if (!fold)
1754 goto error;
1756 if (isl_val_is_neg(v))
1757 fold->type = isl_fold_type_negate(fold->type);
1758 for (i = 0; i < fold->n; ++i) {
1759 fold->qp[i] = isl_qpolynomial_scale_val(fold->qp[i],
1760 isl_val_copy(v));
1761 if (!fold->qp[i])
1762 goto error;
1765 isl_val_free(v);
1766 return fold;
1767 error:
1768 isl_val_free(v);
1769 isl_qpolynomial_fold_free(fold);
1770 return NULL;
1773 /* Divide "fold" by "v".
1775 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_scale_down_val(
1776 __isl_take isl_qpolynomial_fold *fold, __isl_take isl_val *v)
1778 if (!fold || !v)
1779 goto error;
1781 if (isl_val_is_one(v)) {
1782 isl_val_free(v);
1783 return fold;
1785 if (!isl_val_is_rat(v))
1786 isl_die(isl_qpolynomial_fold_get_ctx(fold), isl_error_invalid,
1787 "expecting rational factor", goto error);
1788 if (isl_val_is_zero(v))
1789 isl_die(isl_val_get_ctx(v), isl_error_invalid,
1790 "cannot scale down by zero", goto error);
1792 return isl_qpolynomial_fold_scale_val(fold, isl_val_inv(v));
1793 error:
1794 isl_val_free(v);
1795 isl_qpolynomial_fold_free(fold);
1796 return NULL;