2 * Copyright 2011 INRIA Saclay
3 * Copyright 2011 Universiteit Leiden
5 * Use of this software is governed by the GNU LGPLv2.1 license
7 * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
8 * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
10 * and Leiden Institute of Advanced Computer Science,
11 * Universiteit Leiden, Niels Bohrweg 1, 2333 CA Leiden, The Netherlands
14 #include <isl_ctx_private.h>
15 #include <isl_map_private.h>
16 #include <isl_aff_private.h>
17 #include <isl_dim_private.h>
18 #include <isl_local_space_private.h>
19 #include <isl_mat_private.h>
20 #include <isl_list_private.h>
21 #include <isl/constraint.h>
25 __isl_give isl_aff *isl_aff_alloc_vec(__isl_take isl_local_space *ls,
26 __isl_take isl_vec *v)
33 aff = isl_calloc_type(v->ctx, struct isl_aff);
43 isl_local_space_free(ls);
48 __isl_give isl_aff *isl_aff_alloc(__isl_take isl_local_space *ls)
57 ctx = isl_local_space_get_ctx(ls);
58 if (!isl_local_space_divs_known(ls))
59 isl_die(ctx, isl_error_invalid, "local space has unknown divs",
62 total = isl_local_space_dim(ls, isl_dim_all);
63 v = isl_vec_alloc(ctx, 1 + 1 + total);
64 return isl_aff_alloc_vec(ls, v);
66 isl_local_space_free(ls);
70 __isl_give isl_aff *isl_aff_zero(__isl_take isl_local_space *ls)
74 aff = isl_aff_alloc(ls);
78 isl_int_set_si(aff->v->el[0], 1);
79 isl_seq_clr(aff->v->el + 1, aff->v->size - 1);
84 __isl_give isl_aff *isl_aff_copy(__isl_keep isl_aff *aff)
93 __isl_give isl_aff *isl_aff_dup(__isl_keep isl_aff *aff)
98 return isl_aff_alloc_vec(isl_local_space_copy(aff->ls),
99 isl_vec_copy(aff->v));
102 __isl_give isl_aff *isl_aff_cow(__isl_take isl_aff *aff)
110 return isl_aff_dup(aff);
113 void *isl_aff_free(__isl_take isl_aff *aff)
121 isl_local_space_free(aff->ls);
122 isl_vec_free(aff->v);
129 isl_ctx *isl_aff_get_ctx(__isl_keep isl_aff *aff)
131 return aff ? isl_local_space_get_ctx(aff->ls) : NULL;
134 int isl_aff_dim(__isl_keep isl_aff *aff, enum isl_dim_type type)
136 return aff ? isl_local_space_dim(aff->ls, type) : 0;
139 __isl_give isl_dim *isl_aff_get_dim(__isl_keep isl_aff *aff)
141 return aff ? isl_local_space_get_dim(aff->ls) : NULL;
144 __isl_give isl_local_space *isl_aff_get_local_space(__isl_keep isl_aff *aff)
146 return aff ? isl_local_space_copy(aff->ls) : NULL;
149 const char *isl_aff_get_dim_name(__isl_keep isl_aff *aff,
150 enum isl_dim_type type, unsigned pos)
152 return aff ? isl_local_space_get_dim_name(aff->ls, type, pos) : 0;
155 __isl_give isl_aff *isl_aff_reset_dim(__isl_take isl_aff *aff,
156 __isl_take isl_dim *dim)
158 aff = isl_aff_cow(aff);
162 aff->ls = isl_local_space_reset_dim(aff->ls, dim);
164 return isl_aff_free(aff);
173 /* Reorder the coefficients of the affine expression based
174 * on the given reodering.
175 * The reordering r is assumed to have been extended with the local
178 static __isl_give isl_vec *vec_reorder(__isl_take isl_vec *vec,
179 __isl_take isl_reordering *r, int n_div)
187 res = isl_vec_alloc(vec->ctx, 2 + isl_dim_total(r->dim) + n_div);
188 isl_seq_cpy(res->el, vec->el, 2);
189 isl_seq_clr(res->el + 2, res->size - 2);
190 for (i = 0; i < r->len; ++i)
191 isl_int_set(res->el[2 + r->pos[i]], vec->el[2 + i]);
193 isl_reordering_free(r);
198 isl_reordering_free(r);
202 /* Reorder the dimensions of "aff" according to the given reordering.
204 __isl_give isl_aff *isl_aff_realign(__isl_take isl_aff *aff,
205 __isl_take isl_reordering *r)
207 aff = isl_aff_cow(aff);
211 r = isl_reordering_extend(r, aff->ls->div->n_row);
212 aff->v = vec_reorder(aff->v, isl_reordering_copy(r),
213 aff->ls->div->n_row);
214 aff->ls = isl_local_space_realign(aff->ls, r);
216 if (!aff->v || !aff->ls)
217 return isl_aff_free(aff);
222 isl_reordering_free(r);
226 int isl_aff_plain_is_zero(__isl_keep isl_aff *aff)
231 return isl_seq_first_non_zero(aff->v->el + 1, aff->v->size - 1) < 0;
234 int isl_aff_plain_is_equal(__isl_keep isl_aff *aff1, __isl_keep isl_aff *aff2)
241 equal = isl_local_space_is_equal(aff1->ls, aff2->ls);
242 if (equal < 0 || !equal)
245 return isl_vec_is_equal(aff1->v, aff2->v);
248 int isl_aff_get_denominator(__isl_keep isl_aff *aff, isl_int *v)
252 isl_int_set(*v, aff->v->el[0]);
256 int isl_aff_get_constant(__isl_keep isl_aff *aff, isl_int *v)
260 isl_int_set(*v, aff->v->el[1]);
264 int isl_aff_get_coefficient(__isl_keep isl_aff *aff,
265 enum isl_dim_type type, int pos, isl_int *v)
270 if (pos >= isl_local_space_dim(aff->ls, type))
271 isl_die(aff->v->ctx, isl_error_invalid,
272 "position out of bounds", return -1);
274 pos += isl_local_space_offset(aff->ls, type);
275 isl_int_set(*v, aff->v->el[1 + pos]);
280 __isl_give isl_aff *isl_aff_set_denominator(__isl_take isl_aff *aff, isl_int v)
282 aff = isl_aff_cow(aff);
286 aff->v = isl_vec_cow(aff->v);
288 return isl_aff_free(aff);
290 isl_int_set(aff->v->el[0], v);
295 __isl_give isl_aff *isl_aff_set_constant(__isl_take isl_aff *aff, isl_int v)
297 aff = isl_aff_cow(aff);
301 aff->v = isl_vec_cow(aff->v);
303 return isl_aff_free(aff);
305 isl_int_set(aff->v->el[1], v);
310 __isl_give isl_aff *isl_aff_add_constant(__isl_take isl_aff *aff, isl_int v)
312 if (isl_int_is_zero(v))
315 aff = isl_aff_cow(aff);
319 aff->v = isl_vec_cow(aff->v);
321 return isl_aff_free(aff);
323 isl_int_addmul(aff->v->el[1], aff->v->el[0], v);
328 __isl_give isl_aff *isl_aff_add_constant_si(__isl_take isl_aff *aff, int v)
333 isl_int_set_si(t, v);
334 aff = isl_aff_add_constant(aff, t);
340 __isl_give isl_aff *isl_aff_set_constant_si(__isl_take isl_aff *aff, int v)
342 aff = isl_aff_cow(aff);
346 aff->v = isl_vec_cow(aff->v);
348 return isl_aff_free(aff);
350 isl_int_set_si(aff->v->el[1], v);
355 __isl_give isl_aff *isl_aff_set_coefficient(__isl_take isl_aff *aff,
356 enum isl_dim_type type, int pos, isl_int v)
361 if (pos >= isl_local_space_dim(aff->ls, type))
362 isl_die(aff->v->ctx, isl_error_invalid,
363 "position out of bounds", return isl_aff_free(aff));
365 aff = isl_aff_cow(aff);
369 aff->v = isl_vec_cow(aff->v);
371 return isl_aff_free(aff);
373 pos += isl_local_space_offset(aff->ls, type);
374 isl_int_set(aff->v->el[1 + pos], v);
379 __isl_give isl_aff *isl_aff_set_coefficient_si(__isl_take isl_aff *aff,
380 enum isl_dim_type type, int pos, int v)
385 if (pos >= isl_local_space_dim(aff->ls, type))
386 isl_die(aff->v->ctx, isl_error_invalid,
387 "position out of bounds", return isl_aff_free(aff));
389 aff = isl_aff_cow(aff);
393 aff->v = isl_vec_cow(aff->v);
395 return isl_aff_free(aff);
397 pos += isl_local_space_offset(aff->ls, type);
398 isl_int_set_si(aff->v->el[1 + pos], v);
403 __isl_give isl_aff *isl_aff_add_coefficient(__isl_take isl_aff *aff,
404 enum isl_dim_type type, int pos, isl_int v)
409 if (pos >= isl_local_space_dim(aff->ls, type))
410 isl_die(aff->v->ctx, isl_error_invalid,
411 "position out of bounds", return isl_aff_free(aff));
413 aff = isl_aff_cow(aff);
417 aff->v = isl_vec_cow(aff->v);
419 return isl_aff_free(aff);
421 pos += isl_local_space_offset(aff->ls, type);
422 isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v);
427 __isl_give isl_aff *isl_aff_add_coefficient_si(__isl_take isl_aff *aff,
428 enum isl_dim_type type, int pos, int v)
433 isl_int_set_si(t, v);
434 aff = isl_aff_add_coefficient(aff, type, pos, t);
440 __isl_give isl_div *isl_aff_get_div(__isl_keep isl_aff *aff, int pos)
445 return isl_local_space_get_div(aff->ls, pos);
448 __isl_give isl_aff *isl_aff_neg(__isl_take isl_aff *aff)
450 aff = isl_aff_cow(aff);
453 aff->v = isl_vec_cow(aff->v);
455 return isl_aff_free(aff);
457 isl_seq_neg(aff->v->el + 1, aff->v->el + 1, aff->v->size - 1);
462 /* Given f, return floor(f).
463 * If f is an integer expression, then just return f.
464 * Otherwise, if f = g/m, write g = q m + r,
465 * create a new div d = [r/m] and return the expression q + d.
466 * The coefficients in r are taken to lie between -m/2 and m/2.
468 __isl_give isl_aff *isl_aff_floor(__isl_take isl_aff *aff)
478 if (isl_int_is_one(aff->v->el[0]))
481 aff = isl_aff_cow(aff);
485 aff->v = isl_vec_cow(aff->v);
486 div = isl_vec_copy(aff->v);
487 div = isl_vec_cow(div);
489 return isl_aff_free(aff);
491 ctx = isl_aff_get_ctx(aff);
492 isl_int_fdiv_q(aff->v->el[0], aff->v->el[0], ctx->two);
493 for (i = 1; i < aff->v->size; ++i) {
494 isl_int_fdiv_r(div->el[i], div->el[i], div->el[0]);
495 isl_int_fdiv_q(aff->v->el[i], aff->v->el[i], div->el[0]);
496 if (isl_int_gt(div->el[i], aff->v->el[0])) {
497 isl_int_sub(div->el[i], div->el[i], div->el[0]);
498 isl_int_add_ui(aff->v->el[i], aff->v->el[i], 1);
502 aff->ls = isl_local_space_add_div(aff->ls, div);
504 return isl_aff_free(aff);
507 aff->v = isl_vec_extend(aff->v, size + 1);
509 return isl_aff_free(aff);
510 isl_int_set_si(aff->v->el[0], 1);
511 isl_int_set_si(aff->v->el[size], 1);
518 * aff mod m = aff - m * floor(aff/m)
520 __isl_give isl_aff *isl_aff_mod(__isl_take isl_aff *aff, isl_int m)
524 res = isl_aff_copy(aff);
525 aff = isl_aff_scale_down(aff, m);
526 aff = isl_aff_floor(aff);
527 aff = isl_aff_scale(aff, m);
528 res = isl_aff_sub(res, aff);
535 * pwaff mod m = pwaff - m * floor(pwaff/m)
537 __isl_give isl_pw_aff *isl_pw_aff_mod(__isl_take isl_pw_aff *pwaff, isl_int m)
541 res = isl_pw_aff_copy(pwaff);
542 pwaff = isl_pw_aff_scale_down(pwaff, m);
543 pwaff = isl_pw_aff_floor(pwaff);
544 pwaff = isl_pw_aff_scale(pwaff, m);
545 res = isl_pw_aff_sub(res, pwaff);
550 /* Given f, return ceil(f).
551 * If f is an integer expression, then just return f.
552 * Otherwise, create a new div d = [-f] and return the expression -d.
554 __isl_give isl_aff *isl_aff_ceil(__isl_take isl_aff *aff)
559 if (isl_int_is_one(aff->v->el[0]))
562 aff = isl_aff_neg(aff);
563 aff = isl_aff_floor(aff);
564 aff = isl_aff_neg(aff);
569 /* Apply the expansion computed by isl_merge_divs.
570 * The expansion itself is given by "exp" while the resulting
571 * list of divs is given by "div".
573 __isl_give isl_aff *isl_aff_expand_divs( __isl_take isl_aff *aff,
574 __isl_take isl_mat *div, int *exp)
581 aff = isl_aff_cow(aff);
585 old_n_div = isl_local_space_dim(aff->ls, isl_dim_div);
586 new_n_div = isl_mat_rows(div);
587 if (new_n_div < old_n_div)
588 isl_die(isl_mat_get_ctx(div), isl_error_invalid,
589 "not an expansion", goto error);
591 aff->v = isl_vec_extend(aff->v, aff->v->size + new_n_div - old_n_div);
595 offset = 1 + isl_local_space_offset(aff->ls, isl_dim_div);
597 for (i = new_n_div - 1; i >= 0; --i) {
598 if (j >= 0 && exp[j] == i) {
600 isl_int_swap(aff->v->el[offset + i],
601 aff->v->el[offset + j]);
604 isl_int_set_si(aff->v->el[offset + i], 0);
607 aff->ls = isl_local_space_replace_divs(aff->ls, isl_mat_copy(div));
618 /* Add two affine expressions that live in the same local space.
620 static __isl_give isl_aff *add_expanded(__isl_take isl_aff *aff1,
621 __isl_take isl_aff *aff2)
625 aff1 = isl_aff_cow(aff1);
629 aff1->v = isl_vec_cow(aff1->v);
635 isl_int_gcd(gcd, aff1->v->el[0], aff2->v->el[0]);
636 isl_int_divexact(f, aff2->v->el[0], gcd);
637 isl_seq_scale(aff1->v->el + 1, aff1->v->el + 1, f, aff1->v->size - 1);
638 isl_int_divexact(f, aff1->v->el[0], gcd);
639 isl_seq_addmul(aff1->v->el + 1, f, aff2->v->el + 1, aff1->v->size - 1);
640 isl_int_divexact(f, aff2->v->el[0], gcd);
641 isl_int_mul(aff1->v->el[0], aff1->v->el[0], f);
653 __isl_give isl_aff *isl_aff_add(__isl_take isl_aff *aff1,
654 __isl_take isl_aff *aff2)
664 ctx = isl_aff_get_ctx(aff1);
665 if (!isl_dim_equal(aff1->ls->dim, aff2->ls->dim))
666 isl_die(ctx, isl_error_invalid,
667 "spaces don't match", goto error);
669 if (aff1->ls->div->n_row == 0 && aff2->ls->div->n_row == 0)
670 return add_expanded(aff1, aff2);
672 exp1 = isl_alloc_array(ctx, int, aff1->ls->div->n_row);
673 exp2 = isl_alloc_array(ctx, int, aff2->ls->div->n_row);
677 div = isl_merge_divs(aff1->ls->div, aff2->ls->div, exp1, exp2);
678 aff1 = isl_aff_expand_divs(aff1, isl_mat_copy(div), exp1);
679 aff2 = isl_aff_expand_divs(aff2, div, exp2);
683 return add_expanded(aff1, aff2);
692 __isl_give isl_aff *isl_aff_sub(__isl_take isl_aff *aff1,
693 __isl_take isl_aff *aff2)
695 return isl_aff_add(aff1, isl_aff_neg(aff2));
698 __isl_give isl_aff *isl_aff_scale(__isl_take isl_aff *aff, isl_int f)
702 if (isl_int_is_one(f))
705 aff = isl_aff_cow(aff);
708 aff->v = isl_vec_cow(aff->v);
710 return isl_aff_free(aff);
713 isl_int_gcd(gcd, aff->v->el[0], f);
714 isl_int_divexact(aff->v->el[0], aff->v->el[0], gcd);
715 isl_int_divexact(gcd, f, gcd);
716 isl_seq_scale(aff->v->el + 1, aff->v->el + 1, gcd, aff->v->size - 1);
722 __isl_give isl_aff *isl_aff_scale_down(__isl_take isl_aff *aff, isl_int f)
726 if (isl_int_is_one(f))
729 aff = isl_aff_cow(aff);
732 aff->v = isl_vec_cow(aff->v);
734 return isl_aff_free(aff);
737 isl_seq_gcd(aff->v->el + 1, aff->v->size - 1, &gcd);
738 isl_int_gcd(gcd, gcd, f);
739 isl_seq_scale_down(aff->v->el + 1, aff->v->el + 1, gcd, aff->v->size - 1);
740 isl_int_divexact(gcd, f, gcd);
741 isl_int_mul(aff->v->el[0], aff->v->el[0], gcd);
747 __isl_give isl_aff *isl_aff_scale_down_ui(__isl_take isl_aff *aff, unsigned f)
755 isl_int_set_ui(v, f);
756 aff = isl_aff_scale_down(aff, v);
762 __isl_give isl_aff *isl_aff_set_dim_name(__isl_take isl_aff *aff,
763 enum isl_dim_type type, unsigned pos, const char *s)
765 aff = isl_aff_cow(aff);
768 aff->ls = isl_local_space_set_dim_name(aff->ls, type, pos, s);
770 return isl_aff_free(aff);
775 /* Exploit the equalities in "eq" to simplify the affine expression
776 * and the expressions of the integer divisions in the local space.
777 * The integer divisions in this local space are assumed to appear
778 * as regular dimensions in "eq".
780 static __isl_give isl_aff *isl_aff_substitute_equalities_lifted(
781 __isl_take isl_aff *aff, __isl_take isl_basic_set *eq)
790 isl_basic_set_free(eq);
794 aff = isl_aff_cow(aff);
798 aff->ls = isl_local_space_substitute_equalities(aff->ls,
799 isl_basic_set_copy(eq));
803 total = 1 + isl_dim_total(eq->dim);
805 for (i = 0; i < eq->n_eq; ++i) {
806 j = isl_seq_last_non_zero(eq->eq[i], total + n_div);
807 if (j < 0 || j == 0 || j >= total)
810 isl_seq_elim(aff->v->el + 1, eq->eq[i], j, total,
814 isl_basic_set_free(eq);
817 isl_basic_set_free(eq);
822 /* Exploit the equalities in "eq" to simplify the affine expression
823 * and the expressions of the integer divisions in the local space.
825 static __isl_give isl_aff *isl_aff_substitute_equalities(
826 __isl_take isl_aff *aff, __isl_take isl_basic_set *eq)
832 n_div = isl_local_space_dim(aff->ls, isl_dim_div);
834 eq = isl_basic_set_add(eq, isl_dim_set, n_div);
835 return isl_aff_substitute_equalities_lifted(aff, eq);
837 isl_basic_set_free(eq);
842 /* Look for equalities among the variables shared by context and aff
843 * and the integer divisions of aff, if any.
844 * The equalities are then used to eliminate coefficients and/or integer
845 * divisions from aff.
847 __isl_give isl_aff *isl_aff_gist(__isl_take isl_aff *aff,
848 __isl_take isl_set *context)
855 n_div = isl_local_space_dim(aff->ls, isl_dim_div);
858 context = isl_set_add_dims(context, isl_dim_set, n_div);
859 bset = isl_basic_set_from_local_space(
860 isl_aff_get_local_space(aff));
861 bset = isl_basic_set_lift(bset);
862 bset = isl_basic_set_flatten(bset);
863 context = isl_set_intersect(context,
864 isl_set_from_basic_set(bset));
867 hull = isl_set_affine_hull(context);
868 return isl_aff_substitute_equalities_lifted(aff, hull);
871 isl_set_free(context);
875 /* Return a basic set containing those elements in the space
876 * of aff where it is non-negative.
878 __isl_give isl_basic_set *isl_aff_nonneg_basic_set(__isl_take isl_aff *aff)
880 isl_constraint *ineq;
882 ineq = isl_inequality_from_aff(aff);
884 return isl_basic_set_from_constraint(ineq);
887 /* Return a basic set containing those elements in the space
888 * of aff where it is zero.
890 __isl_give isl_basic_set *isl_aff_zero_basic_set(__isl_take isl_aff *aff)
892 isl_constraint *ineq;
894 ineq = isl_equality_from_aff(aff);
896 return isl_basic_set_from_constraint(ineq);
899 /* Return a basic set containing those elements in the shared space
900 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
902 __isl_give isl_basic_set *isl_aff_ge_basic_set(__isl_take isl_aff *aff1,
903 __isl_take isl_aff *aff2)
905 aff1 = isl_aff_sub(aff1, aff2);
907 return isl_aff_nonneg_basic_set(aff1);
910 __isl_give isl_aff *isl_aff_add_on_domain(__isl_keep isl_set *dom,
911 __isl_take isl_aff *aff1, __isl_take isl_aff *aff2)
913 aff1 = isl_aff_add(aff1, aff2);
914 aff1 = isl_aff_gist(aff1, isl_set_copy(dom));
918 int isl_aff_is_empty(__isl_keep isl_aff *aff)
926 /* Set active[i] to 1 if the dimension at position i is involved
927 * in the affine expression.
929 static int set_active(__isl_keep isl_aff *aff, int *active)
938 total = aff->v->size - 2;
939 for (i = 0; i < total; ++i)
940 active[i] = !isl_int_is_zero(aff->v->el[2 + i]);
942 offset = isl_local_space_offset(aff->ls, isl_dim_div) - 1;
943 for (i = aff->ls->div->n_row - 1; i >= 0; --i) {
944 if (!active[offset + i])
946 for (j = 0; j < total; ++j)
948 !isl_int_is_zero(aff->ls->div->row[i][2 + j]);
954 /* Check whether the given affine expression has non-zero coefficient
955 * for any dimension in the given range or if any of these dimensions
956 * appear with non-zero coefficients in any of the integer divisions
957 * involved in the affine expression.
959 int isl_aff_involves_dims(__isl_keep isl_aff *aff,
960 enum isl_dim_type type, unsigned first, unsigned n)
972 ctx = isl_aff_get_ctx(aff);
973 if (first + n > isl_aff_dim(aff, type))
974 isl_die(ctx, isl_error_invalid,
975 "range out of bounds", return -1);
977 active = isl_calloc_array(ctx, int,
978 isl_local_space_dim(aff->ls, isl_dim_all));
979 if (set_active(aff, active) < 0)
982 first += isl_local_space_offset(aff->ls, type) - 1;
983 for (i = 0; i < n; ++i)
984 if (active[first + i]) {
997 __isl_give isl_aff *isl_aff_drop_dims(__isl_take isl_aff *aff,
998 enum isl_dim_type type, unsigned first, unsigned n)
1004 if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
1007 ctx = isl_aff_get_ctx(aff);
1008 if (first + n > isl_aff_dim(aff, type))
1009 isl_die(ctx, isl_error_invalid, "range out of bounds",
1010 return isl_aff_free(aff));
1012 aff = isl_aff_cow(aff);
1016 aff->ls = isl_local_space_drop_dims(aff->ls, type, first, n);
1018 return isl_aff_free(aff);
1020 first += 1 + isl_local_space_offset(aff->ls, type);
1021 aff->v = isl_vec_drop_els(aff->v, first, n);
1023 return isl_aff_free(aff);
1028 __isl_give isl_aff *isl_aff_insert_dims(__isl_take isl_aff *aff,
1029 enum isl_dim_type type, unsigned first, unsigned n)
1035 if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
1038 ctx = isl_aff_get_ctx(aff);
1039 if (first > isl_aff_dim(aff, type))
1040 isl_die(ctx, isl_error_invalid, "position out of bounds",
1041 return isl_aff_free(aff));
1043 aff = isl_aff_cow(aff);
1047 aff->ls = isl_local_space_insert_dims(aff->ls, type, first, n);
1049 return isl_aff_free(aff);
1051 first += 1 + isl_local_space_offset(aff->ls, type);
1052 aff->v = isl_vec_insert_zero_els(aff->v, first, n);
1054 return isl_aff_free(aff);
1059 __isl_give isl_aff *isl_aff_add_dims(__isl_take isl_aff *aff,
1060 enum isl_dim_type type, unsigned n)
1064 pos = isl_aff_dim(aff, type);
1066 return isl_aff_insert_dims(aff, type, pos, n);
1069 __isl_give isl_pw_aff *isl_pw_aff_add_dims(__isl_take isl_pw_aff *pwaff,
1070 enum isl_dim_type type, unsigned n)
1074 pos = isl_pw_aff_dim(pwaff, type);
1076 return isl_pw_aff_insert_dims(pwaff, type, pos, n);
1079 __isl_give isl_pw_aff *isl_pw_aff_set_tuple_id(__isl_take isl_pw_aff *pwaff,
1080 __isl_take isl_id *id)
1084 dim = isl_pw_aff_get_dim(pwaff);
1085 dim = isl_dim_set_tuple_id(dim, isl_dim_set, id);
1087 return isl_pw_aff_reset_dim(pwaff, dim);
1090 __isl_give isl_pw_aff *isl_pw_aff_from_aff(__isl_take isl_aff *aff)
1092 isl_set *dom = isl_set_universe(isl_aff_get_dim(aff));
1093 return isl_pw_aff_alloc(dom, aff);
1097 #define PW isl_pw_aff
1101 #define EL_IS_ZERO is_empty
1105 #define IS_ZERO is_empty
1111 #define NO_MOVE_DIMS
1115 #include <isl_pw_templ.c>
1117 static __isl_give isl_set *align_params_pw_pw_set_and(
1118 __isl_take isl_pw_aff *pwaff1, __isl_take isl_pw_aff *pwaff2,
1119 __isl_give isl_set *(*fn)(__isl_take isl_pw_aff *pwaff1,
1120 __isl_take isl_pw_aff *pwaff2))
1122 if (!pwaff1 || !pwaff2)
1124 if (isl_dim_match(pwaff1->dim, isl_dim_param,
1125 pwaff2->dim, isl_dim_param))
1126 return fn(pwaff1, pwaff2);
1127 if (!isl_dim_has_named_params(pwaff1->dim) ||
1128 !isl_dim_has_named_params(pwaff2->dim))
1129 isl_die(isl_pw_aff_get_ctx(pwaff1), isl_error_invalid,
1130 "unaligned unnamed parameters", goto error);
1131 pwaff1 = isl_pw_aff_align_params(pwaff1, isl_pw_aff_get_dim(pwaff2));
1132 pwaff2 = isl_pw_aff_align_params(pwaff2, isl_pw_aff_get_dim(pwaff1));
1133 return fn(pwaff1, pwaff2);
1135 isl_pw_aff_free(pwaff1);
1136 isl_pw_aff_free(pwaff2);
1140 /* Compute a piecewise quasi-affine expression with a domain that
1141 * is the union of those of pwaff1 and pwaff2 and such that on each
1142 * cell, the quasi-affine expression is the maximum of those of pwaff1
1143 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
1144 * cell, then the associated expression is the defined one.
1146 static __isl_give isl_pw_aff *pw_aff_union_max(__isl_take isl_pw_aff *pwaff1,
1147 __isl_take isl_pw_aff *pwaff2)
1154 if (!pwaff1 || !pwaff2)
1157 ctx = isl_dim_get_ctx(pwaff1->dim);
1158 if (!isl_dim_equal(pwaff1->dim, pwaff2->dim))
1159 isl_die(ctx, isl_error_invalid,
1160 "arguments should live in same space", goto error);
1162 if (isl_pw_aff_is_empty(pwaff1)) {
1163 isl_pw_aff_free(pwaff1);
1167 if (isl_pw_aff_is_empty(pwaff2)) {
1168 isl_pw_aff_free(pwaff2);
1172 n = 2 * (pwaff1->n + 1) * (pwaff2->n + 1);
1173 res = isl_pw_aff_alloc_(isl_dim_copy(pwaff1->dim), n);
1175 for (i = 0; i < pwaff1->n; ++i) {
1176 set = isl_set_copy(pwaff1->p[i].set);
1177 for (j = 0; j < pwaff2->n; ++j) {
1178 struct isl_set *common;
1181 common = isl_set_intersect(
1182 isl_set_copy(pwaff1->p[i].set),
1183 isl_set_copy(pwaff2->p[j].set));
1184 ge = isl_set_from_basic_set(isl_aff_ge_basic_set(
1185 isl_aff_copy(pwaff2->p[j].aff),
1186 isl_aff_copy(pwaff1->p[i].aff)));
1187 ge = isl_set_intersect(common, ge);
1188 if (isl_set_plain_is_empty(ge)) {
1192 set = isl_set_subtract(set, isl_set_copy(ge));
1194 res = isl_pw_aff_add_piece(res, ge,
1195 isl_aff_copy(pwaff2->p[j].aff));
1197 res = isl_pw_aff_add_piece(res, set,
1198 isl_aff_copy(pwaff1->p[i].aff));
1201 for (j = 0; j < pwaff2->n; ++j) {
1202 set = isl_set_copy(pwaff2->p[j].set);
1203 for (i = 0; i < pwaff1->n; ++i)
1204 set = isl_set_subtract(set,
1205 isl_set_copy(pwaff1->p[i].set));
1206 res = isl_pw_aff_add_piece(res, set,
1207 isl_aff_copy(pwaff2->p[j].aff));
1210 isl_pw_aff_free(pwaff1);
1211 isl_pw_aff_free(pwaff2);
1215 isl_pw_aff_free(pwaff1);
1216 isl_pw_aff_free(pwaff2);
1220 __isl_give isl_pw_aff *isl_pw_aff_union_max(__isl_take isl_pw_aff *pwaff1,
1221 __isl_take isl_pw_aff *pwaff2)
1223 return align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_union_max);
1226 /* Construct a map with as domain the domain of pwaff and
1227 * one-dimensional range corresponding to the affine expressions.
1229 __isl_give isl_map *isl_map_from_pw_aff(__isl_take isl_pw_aff *pwaff)
1238 dim = isl_pw_aff_get_dim(pwaff);
1239 dim = isl_dim_from_domain(dim);
1240 dim = isl_dim_add(dim, isl_dim_out, 1);
1241 map = isl_map_empty(dim);
1243 for (i = 0; i < pwaff->n; ++i) {
1244 isl_basic_map *bmap;
1247 bmap = isl_basic_map_from_aff(isl_aff_copy(pwaff->p[i].aff));
1248 map_i = isl_map_from_basic_map(bmap);
1249 map_i = isl_map_intersect_domain(map_i,
1250 isl_set_copy(pwaff->p[i].set));
1251 map = isl_map_union_disjoint(map, map_i);
1254 isl_pw_aff_free(pwaff);
1259 /* Return a set containing those elements in the domain
1260 * of pwaff where it is non-negative.
1262 __isl_give isl_set *isl_pw_aff_nonneg_set(__isl_take isl_pw_aff *pwaff)
1270 set = isl_set_empty(isl_pw_aff_get_dim(pwaff));
1272 for (i = 0; i < pwaff->n; ++i) {
1273 isl_basic_set *bset;
1276 bset = isl_aff_nonneg_basic_set(isl_aff_copy(pwaff->p[i].aff));
1277 set_i = isl_set_from_basic_set(bset);
1278 set_i = isl_set_intersect(set_i, isl_set_copy(pwaff->p[i].set));
1279 set = isl_set_union_disjoint(set, set_i);
1282 isl_pw_aff_free(pwaff);
1287 /* Return a set containing those elements in the domain
1288 * of pwaff where it is zero.
1290 __isl_give isl_set *isl_pw_aff_zero_set(__isl_take isl_pw_aff *pwaff)
1298 set = isl_set_empty(isl_pw_aff_get_dim(pwaff));
1300 for (i = 0; i < pwaff->n; ++i) {
1301 isl_basic_set *bset;
1304 bset = isl_aff_zero_basic_set(isl_aff_copy(pwaff->p[i].aff));
1305 set_i = isl_set_from_basic_set(bset);
1306 set_i = isl_set_intersect(set_i, isl_set_copy(pwaff->p[i].set));
1307 set = isl_set_union_disjoint(set, set_i);
1310 isl_pw_aff_free(pwaff);
1315 /* Return a set containing those elements in the domain
1316 * of pwaff where it is not zero.
1318 __isl_give isl_set *isl_pw_aff_non_zero_set(__isl_take isl_pw_aff *pwaff)
1320 return isl_set_complement(isl_pw_aff_zero_set(pwaff));
1323 /* Return a set containing those elements in the shared domain
1324 * of pwaff1 and pwaff2 where pwaff1 is greater than (or equal) to pwaff2.
1326 * We compute the difference on the shared domain and then construct
1327 * the set of values where this difference is non-negative.
1328 * If strict is set, we first subtract 1 from the difference.
1329 * If equal is set, we only return the elements where pwaff1 and pwaff2
1332 static __isl_give isl_set *pw_aff_gte_set(__isl_take isl_pw_aff *pwaff1,
1333 __isl_take isl_pw_aff *pwaff2, int strict, int equal)
1335 isl_set *set1, *set2;
1337 set1 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff1));
1338 set2 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff2));
1339 set1 = isl_set_intersect(set1, set2);
1340 pwaff1 = isl_pw_aff_intersect_domain(pwaff1, isl_set_copy(set1));
1341 pwaff2 = isl_pw_aff_intersect_domain(pwaff2, isl_set_copy(set1));
1342 pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_neg(pwaff2));
1345 isl_dim *dim = isl_set_get_dim(set1);
1347 aff = isl_aff_zero(isl_local_space_from_dim(dim));
1348 aff = isl_aff_add_constant_si(aff, -1);
1349 pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_alloc(set1, aff));
1354 return isl_pw_aff_zero_set(pwaff1);
1355 return isl_pw_aff_nonneg_set(pwaff1);
1358 /* Return a set containing those elements in the shared domain
1359 * of pwaff1 and pwaff2 where pwaff1 is equal to pwaff2.
1361 static __isl_give isl_set *pw_aff_eq_set(__isl_take isl_pw_aff *pwaff1,
1362 __isl_take isl_pw_aff *pwaff2)
1364 return pw_aff_gte_set(pwaff1, pwaff2, 0, 1);
1367 __isl_give isl_set *isl_pw_aff_eq_set(__isl_take isl_pw_aff *pwaff1,
1368 __isl_take isl_pw_aff *pwaff2)
1370 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_eq_set);
1373 /* Return a set containing those elements in the shared domain
1374 * of pwaff1 and pwaff2 where pwaff1 is greater than or equal to pwaff2.
1376 static __isl_give isl_set *pw_aff_ge_set(__isl_take isl_pw_aff *pwaff1,
1377 __isl_take isl_pw_aff *pwaff2)
1379 return pw_aff_gte_set(pwaff1, pwaff2, 0, 0);
1382 __isl_give isl_set *isl_pw_aff_ge_set(__isl_take isl_pw_aff *pwaff1,
1383 __isl_take isl_pw_aff *pwaff2)
1385 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_ge_set);
1388 /* Return a set containing those elements in the shared domain
1389 * of pwaff1 and pwaff2 where pwaff1 is strictly greater than pwaff2.
1391 static __isl_give isl_set *pw_aff_gt_set(__isl_take isl_pw_aff *pwaff1,
1392 __isl_take isl_pw_aff *pwaff2)
1394 return pw_aff_gte_set(pwaff1, pwaff2, 1, 0);
1397 __isl_give isl_set *isl_pw_aff_gt_set(__isl_take isl_pw_aff *pwaff1,
1398 __isl_take isl_pw_aff *pwaff2)
1400 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_gt_set);
1403 __isl_give isl_set *isl_pw_aff_le_set(__isl_take isl_pw_aff *pwaff1,
1404 __isl_take isl_pw_aff *pwaff2)
1406 return isl_pw_aff_ge_set(pwaff2, pwaff1);
1409 __isl_give isl_set *isl_pw_aff_lt_set(__isl_take isl_pw_aff *pwaff1,
1410 __isl_take isl_pw_aff *pwaff2)
1412 return isl_pw_aff_gt_set(pwaff2, pwaff1);
1415 /* Return a set containing those elements in the shared domain
1416 * of the elements of list1 and list2 where each element in list1
1417 * has the relation specified by "fn" with each element in list2.
1419 static __isl_give isl_set *pw_aff_list_set(__isl_take isl_pw_aff_list *list1,
1420 __isl_take isl_pw_aff_list *list2,
1421 __isl_give isl_set *(*fn)(__isl_take isl_pw_aff *pwaff1,
1422 __isl_take isl_pw_aff *pwaff2))
1428 if (!list1 || !list2)
1431 ctx = isl_pw_aff_list_get_ctx(list1);
1432 if (list1->n < 1 || list2->n < 1)
1433 isl_die(ctx, isl_error_invalid,
1434 "list should contain at least one element", goto error);
1436 set = isl_set_universe(isl_pw_aff_get_dim(list1->p[0]));
1437 for (i = 0; i < list1->n; ++i)
1438 for (j = 0; j < list2->n; ++j) {
1441 set_ij = fn(isl_pw_aff_copy(list1->p[i]),
1442 isl_pw_aff_copy(list2->p[j]));
1443 set = isl_set_intersect(set, set_ij);
1446 isl_pw_aff_list_free(list1);
1447 isl_pw_aff_list_free(list2);
1450 isl_pw_aff_list_free(list1);
1451 isl_pw_aff_list_free(list2);
1455 /* Return a set containing those elements in the shared domain
1456 * of the elements of list1 and list2 where each element in list1
1457 * is equal to each element in list2.
1459 __isl_give isl_set *isl_pw_aff_list_eq_set(__isl_take isl_pw_aff_list *list1,
1460 __isl_take isl_pw_aff_list *list2)
1462 return pw_aff_list_set(list1, list2, &isl_pw_aff_eq_set);
1465 __isl_give isl_set *isl_pw_aff_list_ne_set(__isl_take isl_pw_aff_list *list1,
1466 __isl_take isl_pw_aff_list *list2)
1468 return pw_aff_list_set(list1, list2, &isl_pw_aff_ne_set);
1471 /* Return a set containing those elements in the shared domain
1472 * of the elements of list1 and list2 where each element in list1
1473 * is less than or equal to each element in list2.
1475 __isl_give isl_set *isl_pw_aff_list_le_set(__isl_take isl_pw_aff_list *list1,
1476 __isl_take isl_pw_aff_list *list2)
1478 return pw_aff_list_set(list1, list2, &isl_pw_aff_le_set);
1481 __isl_give isl_set *isl_pw_aff_list_lt_set(__isl_take isl_pw_aff_list *list1,
1482 __isl_take isl_pw_aff_list *list2)
1484 return pw_aff_list_set(list1, list2, &isl_pw_aff_lt_set);
1487 __isl_give isl_set *isl_pw_aff_list_ge_set(__isl_take isl_pw_aff_list *list1,
1488 __isl_take isl_pw_aff_list *list2)
1490 return pw_aff_list_set(list1, list2, &isl_pw_aff_ge_set);
1493 __isl_give isl_set *isl_pw_aff_list_gt_set(__isl_take isl_pw_aff_list *list1,
1494 __isl_take isl_pw_aff_list *list2)
1496 return pw_aff_list_set(list1, list2, &isl_pw_aff_gt_set);
1500 /* Return a set containing those elements in the shared domain
1501 * of pwaff1 and pwaff2 where pwaff1 is not equal to pwaff2.
1503 static __isl_give isl_set *pw_aff_ne_set(__isl_take isl_pw_aff *pwaff1,
1504 __isl_take isl_pw_aff *pwaff2)
1506 isl_set *set_lt, *set_gt;
1508 set_lt = isl_pw_aff_lt_set(isl_pw_aff_copy(pwaff1),
1509 isl_pw_aff_copy(pwaff2));
1510 set_gt = isl_pw_aff_gt_set(pwaff1, pwaff2);
1511 return isl_set_union_disjoint(set_lt, set_gt);
1514 __isl_give isl_set *isl_pw_aff_ne_set(__isl_take isl_pw_aff *pwaff1,
1515 __isl_take isl_pw_aff *pwaff2)
1517 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_ne_set);
1520 __isl_give isl_pw_aff *isl_pw_aff_scale_down(__isl_take isl_pw_aff *pwaff,
1525 if (isl_int_is_one(v))
1527 if (!isl_int_is_pos(v))
1528 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
1529 "factor needs to be positive",
1530 return isl_pw_aff_free(pwaff));
1531 pwaff = isl_pw_aff_cow(pwaff);
1537 for (i = 0; i < pwaff->n; ++i) {
1538 pwaff->p[i].aff = isl_aff_scale_down(pwaff->p[i].aff, v);
1539 if (!pwaff->p[i].aff)
1540 return isl_pw_aff_free(pwaff);
1546 __isl_give isl_pw_aff *isl_pw_aff_floor(__isl_take isl_pw_aff *pwaff)
1550 pwaff = isl_pw_aff_cow(pwaff);
1556 for (i = 0; i < pwaff->n; ++i) {
1557 pwaff->p[i].aff = isl_aff_floor(pwaff->p[i].aff);
1558 if (!pwaff->p[i].aff)
1559 return isl_pw_aff_free(pwaff);
1565 __isl_give isl_pw_aff *isl_pw_aff_ceil(__isl_take isl_pw_aff *pwaff)
1569 pwaff = isl_pw_aff_cow(pwaff);
1575 for (i = 0; i < pwaff->n; ++i) {
1576 pwaff->p[i].aff = isl_aff_ceil(pwaff->p[i].aff);
1577 if (!pwaff->p[i].aff)
1578 return isl_pw_aff_free(pwaff);
1584 /* Return an affine expression that is equal to pwaff_true for elements
1585 * in "cond" and to pwaff_false for elements not in "cond".
1586 * That is, return cond ? pwaff_true : pwaff_false;
1588 __isl_give isl_pw_aff *isl_pw_aff_cond(__isl_take isl_set *cond,
1589 __isl_take isl_pw_aff *pwaff_true, __isl_take isl_pw_aff *pwaff_false)
1593 comp = isl_set_complement(isl_set_copy(cond));
1594 pwaff_true = isl_pw_aff_intersect_domain(pwaff_true, cond);
1595 pwaff_false = isl_pw_aff_intersect_domain(pwaff_false, comp);
1597 return isl_pw_aff_add_disjoint(pwaff_true, pwaff_false);
1600 int isl_aff_is_cst(__isl_keep isl_aff *aff)
1605 return isl_seq_first_non_zero(aff->v->el + 2, aff->v->size - 2) == -1;
1608 /* Check whether pwaff is a piecewise constant.
1610 int isl_pw_aff_is_cst(__isl_keep isl_pw_aff *pwaff)
1617 for (i = 0; i < pwaff->n; ++i) {
1618 int is_cst = isl_aff_is_cst(pwaff->p[i].aff);
1619 if (is_cst < 0 || !is_cst)
1626 __isl_give isl_aff *isl_aff_mul(__isl_take isl_aff *aff1,
1627 __isl_take isl_aff *aff2)
1629 if (!isl_aff_is_cst(aff2) && isl_aff_is_cst(aff1))
1630 return isl_aff_mul(aff2, aff1);
1632 if (!isl_aff_is_cst(aff2))
1633 isl_die(isl_aff_get_ctx(aff1), isl_error_invalid,
1634 "at least one affine expression should be constant",
1637 aff1 = isl_aff_cow(aff1);
1641 aff1 = isl_aff_scale(aff1, aff2->v->el[1]);
1642 aff1 = isl_aff_scale_down(aff1, aff2->v->el[0]);
1652 static __isl_give isl_pw_aff *pw_aff_mul(__isl_take isl_pw_aff *pwaff1,
1653 __isl_take isl_pw_aff *pwaff2)
1658 if (!pwaff1 || !pwaff2)
1661 n = pwaff1->n * pwaff2->n;
1662 res = isl_pw_aff_alloc_(isl_dim_copy(pwaff1->dim), n);
1664 for (i = 0; i < pwaff1->n; ++i) {
1665 for (j = 0; j < pwaff2->n; ++j) {
1668 common = isl_set_intersect(
1669 isl_set_copy(pwaff1->p[i].set),
1670 isl_set_copy(pwaff2->p[j].set));
1671 if (isl_set_plain_is_empty(common)) {
1672 isl_set_free(common);
1676 prod = isl_aff_mul(isl_aff_copy(pwaff1->p[i].aff),
1677 isl_aff_copy(pwaff2->p[j].aff));
1679 res = isl_pw_aff_add_piece(res, common, prod);
1683 isl_pw_aff_free(pwaff1);
1684 isl_pw_aff_free(pwaff2);
1687 isl_pw_aff_free(pwaff1);
1688 isl_pw_aff_free(pwaff2);
1692 __isl_give isl_pw_aff *isl_pw_aff_mul(__isl_take isl_pw_aff *pwaff1,
1693 __isl_take isl_pw_aff *pwaff2)
1695 return align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_mul);
1698 static __isl_give isl_pw_aff *pw_aff_min(__isl_take isl_pw_aff *pwaff1,
1699 __isl_take isl_pw_aff *pwaff2)
1703 le = isl_pw_aff_le_set(isl_pw_aff_copy(pwaff1),
1704 isl_pw_aff_copy(pwaff2));
1705 return isl_pw_aff_cond(le, pwaff1, pwaff2);
1708 __isl_give isl_pw_aff *isl_pw_aff_min(__isl_take isl_pw_aff *pwaff1,
1709 __isl_take isl_pw_aff *pwaff2)
1711 return align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_min);
1714 static __isl_give isl_pw_aff *pw_aff_max(__isl_take isl_pw_aff *pwaff1,
1715 __isl_take isl_pw_aff *pwaff2)
1719 le = isl_pw_aff_ge_set(isl_pw_aff_copy(pwaff1),
1720 isl_pw_aff_copy(pwaff2));
1721 return isl_pw_aff_cond(le, pwaff1, pwaff2);
1724 __isl_give isl_pw_aff *isl_pw_aff_max(__isl_take isl_pw_aff *pwaff1,
1725 __isl_take isl_pw_aff *pwaff2)
1727 return align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_max);
1730 static __isl_give isl_pw_aff *pw_aff_list_reduce(
1731 __isl_take isl_pw_aff_list *list,
1732 __isl_give isl_pw_aff *(*fn)(__isl_take isl_pw_aff *pwaff1,
1733 __isl_take isl_pw_aff *pwaff2))
1742 ctx = isl_pw_aff_list_get_ctx(list);
1744 isl_die(ctx, isl_error_invalid,
1745 "list should contain at least one element",
1746 return isl_pw_aff_list_free(list));
1748 res = isl_pw_aff_copy(list->p[0]);
1749 for (i = 1; i < list->n; ++i)
1750 res = fn(res, isl_pw_aff_copy(list->p[i]));
1752 isl_pw_aff_list_free(list);
1756 /* Return an isl_pw_aff that maps each element in the intersection of the
1757 * domains of the elements of list to the minimal corresponding affine
1760 __isl_give isl_pw_aff *isl_pw_aff_list_min(__isl_take isl_pw_aff_list *list)
1762 return pw_aff_list_reduce(list, &isl_pw_aff_min);
1765 /* Return an isl_pw_aff that maps each element in the intersection of the
1766 * domains of the elements of list to the maximal corresponding affine
1769 __isl_give isl_pw_aff *isl_pw_aff_list_max(__isl_take isl_pw_aff_list *list)
1771 return pw_aff_list_reduce(list, &isl_pw_aff_max);