2 * Copyright 2011 INRIA Saclay
3 * Copyright 2011 Sven Verdoolaege
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,
12 #include <isl_ctx_private.h>
13 #include <isl_map_private.h>
14 #include <isl_aff_private.h>
15 #include <isl_dim_private.h>
16 #include <isl_local_space_private.h>
17 #include <isl_mat_private.h>
18 #include <isl_list_private.h>
19 #include <isl/constraint.h>
23 __isl_give isl_aff *isl_aff_alloc_vec(__isl_take isl_local_space *ls,
24 __isl_take isl_vec *v)
31 aff = isl_calloc_type(v->ctx, struct isl_aff);
41 isl_local_space_free(ls);
46 __isl_give isl_aff *isl_aff_alloc(__isl_take isl_local_space *ls)
55 ctx = isl_local_space_get_ctx(ls);
56 if (!isl_local_space_divs_known(ls))
57 isl_die(ctx, isl_error_invalid, "local space has unknown divs",
60 total = isl_local_space_dim(ls, isl_dim_all);
61 v = isl_vec_alloc(ctx, 1 + 1 + total);
62 return isl_aff_alloc_vec(ls, v);
64 isl_local_space_free(ls);
68 __isl_give isl_aff *isl_aff_zero(__isl_take isl_local_space *ls)
72 aff = isl_aff_alloc(ls);
76 isl_int_set_si(aff->v->el[0], 1);
77 isl_seq_clr(aff->v->el + 1, aff->v->size - 1);
82 __isl_give isl_aff *isl_aff_copy(__isl_keep isl_aff *aff)
91 __isl_give isl_aff *isl_aff_dup(__isl_keep isl_aff *aff)
96 return isl_aff_alloc_vec(isl_local_space_copy(aff->ls),
97 isl_vec_copy(aff->v));
100 __isl_give isl_aff *isl_aff_cow(__isl_take isl_aff *aff)
108 return isl_aff_dup(aff);
111 void *isl_aff_free(__isl_take isl_aff *aff)
119 isl_local_space_free(aff->ls);
120 isl_vec_free(aff->v);
127 isl_ctx *isl_aff_get_ctx(__isl_keep isl_aff *aff)
129 return aff ? isl_local_space_get_ctx(aff->ls) : NULL;
132 int isl_aff_dim(__isl_keep isl_aff *aff, enum isl_dim_type type)
134 return aff ? isl_local_space_dim(aff->ls, type) : 0;
137 __isl_give isl_dim *isl_aff_get_dim(__isl_keep isl_aff *aff)
139 return aff ? isl_local_space_get_dim(aff->ls) : NULL;
142 __isl_give isl_local_space *isl_aff_get_local_space(__isl_keep isl_aff *aff)
144 return aff ? isl_local_space_copy(aff->ls) : NULL;
147 const char *isl_aff_get_dim_name(__isl_keep isl_aff *aff,
148 enum isl_dim_type type, unsigned pos)
150 return aff ? isl_local_space_get_dim_name(aff->ls, type, pos) : 0;
153 __isl_give isl_aff *isl_aff_reset_dim(__isl_take isl_aff *aff,
154 __isl_take isl_dim *dim)
156 aff = isl_aff_cow(aff);
160 aff->ls = isl_local_space_reset_dim(aff->ls, dim);
162 return isl_aff_free(aff);
171 /* Reorder the coefficients of the affine expression based
172 * on the given reodering.
173 * The reordering r is assumed to have been extended with the local
176 static __isl_give isl_vec *vec_reorder(__isl_take isl_vec *vec,
177 __isl_take isl_reordering *r, int n_div)
185 res = isl_vec_alloc(vec->ctx, 2 + isl_dim_total(r->dim) + n_div);
186 isl_seq_cpy(res->el, vec->el, 2);
187 isl_seq_clr(res->el + 2, res->size - 2);
188 for (i = 0; i < r->len; ++i)
189 isl_int_set(res->el[2 + r->pos[i]], vec->el[2 + i]);
191 isl_reordering_free(r);
196 isl_reordering_free(r);
200 /* Reorder the dimensions of "aff" according to the given reordering.
202 __isl_give isl_aff *isl_aff_realign(__isl_take isl_aff *aff,
203 __isl_take isl_reordering *r)
205 aff = isl_aff_cow(aff);
209 r = isl_reordering_extend(r, aff->ls->div->n_row);
210 aff->v = vec_reorder(aff->v, isl_reordering_copy(r),
211 aff->ls->div->n_row);
212 aff->ls = isl_local_space_realign(aff->ls, r);
214 if (!aff->v || !aff->ls)
215 return isl_aff_free(aff);
220 isl_reordering_free(r);
224 int isl_aff_plain_is_zero(__isl_keep isl_aff *aff)
229 return isl_seq_first_non_zero(aff->v->el + 1, aff->v->size - 1) < 0;
232 int isl_aff_plain_is_equal(__isl_keep isl_aff *aff1, __isl_keep isl_aff *aff2)
239 equal = isl_local_space_is_equal(aff1->ls, aff2->ls);
240 if (equal < 0 || !equal)
243 return isl_vec_is_equal(aff1->v, aff2->v);
246 int isl_aff_get_denominator(__isl_keep isl_aff *aff, isl_int *v)
250 isl_int_set(*v, aff->v->el[0]);
254 int isl_aff_get_constant(__isl_keep isl_aff *aff, isl_int *v)
258 isl_int_set(*v, aff->v->el[1]);
262 int isl_aff_get_coefficient(__isl_keep isl_aff *aff,
263 enum isl_dim_type type, int pos, isl_int *v)
268 if (pos >= isl_local_space_dim(aff->ls, type))
269 isl_die(aff->v->ctx, isl_error_invalid,
270 "position out of bounds", return -1);
272 pos += isl_local_space_offset(aff->ls, type);
273 isl_int_set(*v, aff->v->el[1 + pos]);
278 __isl_give isl_aff *isl_aff_set_denominator(__isl_take isl_aff *aff, isl_int v)
280 aff = isl_aff_cow(aff);
284 aff->v = isl_vec_cow(aff->v);
286 return isl_aff_free(aff);
288 isl_int_set(aff->v->el[0], v);
293 __isl_give isl_aff *isl_aff_set_constant(__isl_take isl_aff *aff, isl_int v)
295 aff = isl_aff_cow(aff);
299 aff->v = isl_vec_cow(aff->v);
301 return isl_aff_free(aff);
303 isl_int_set(aff->v->el[1], v);
308 __isl_give isl_aff *isl_aff_add_constant(__isl_take isl_aff *aff, isl_int v)
310 if (isl_int_is_zero(v))
313 aff = isl_aff_cow(aff);
317 aff->v = isl_vec_cow(aff->v);
319 return isl_aff_free(aff);
321 isl_int_addmul(aff->v->el[1], aff->v->el[0], v);
326 __isl_give isl_aff *isl_aff_add_constant_si(__isl_take isl_aff *aff, int v)
331 isl_int_set_si(t, v);
332 aff = isl_aff_add_constant(aff, t);
338 __isl_give isl_aff *isl_aff_set_constant_si(__isl_take isl_aff *aff, int v)
340 aff = isl_aff_cow(aff);
344 aff->v = isl_vec_cow(aff->v);
346 return isl_aff_free(aff);
348 isl_int_set_si(aff->v->el[1], v);
353 __isl_give isl_aff *isl_aff_set_coefficient(__isl_take isl_aff *aff,
354 enum isl_dim_type type, int pos, isl_int v)
359 if (pos >= isl_local_space_dim(aff->ls, type))
360 isl_die(aff->v->ctx, isl_error_invalid,
361 "position out of bounds", return isl_aff_free(aff));
363 aff = isl_aff_cow(aff);
367 aff->v = isl_vec_cow(aff->v);
369 return isl_aff_free(aff);
371 pos += isl_local_space_offset(aff->ls, type);
372 isl_int_set(aff->v->el[1 + pos], v);
377 __isl_give isl_aff *isl_aff_set_coefficient_si(__isl_take isl_aff *aff,
378 enum isl_dim_type type, int pos, int v)
383 if (pos >= isl_local_space_dim(aff->ls, type))
384 isl_die(aff->v->ctx, isl_error_invalid,
385 "position out of bounds", return isl_aff_free(aff));
387 aff = isl_aff_cow(aff);
391 aff->v = isl_vec_cow(aff->v);
393 return isl_aff_free(aff);
395 pos += isl_local_space_offset(aff->ls, type);
396 isl_int_set_si(aff->v->el[1 + pos], v);
401 __isl_give isl_aff *isl_aff_add_coefficient(__isl_take isl_aff *aff,
402 enum isl_dim_type type, int pos, isl_int v)
407 if (pos >= isl_local_space_dim(aff->ls, type))
408 isl_die(aff->v->ctx, isl_error_invalid,
409 "position out of bounds", return isl_aff_free(aff));
411 aff = isl_aff_cow(aff);
415 aff->v = isl_vec_cow(aff->v);
417 return isl_aff_free(aff);
419 pos += isl_local_space_offset(aff->ls, type);
420 isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v);
425 __isl_give isl_aff *isl_aff_add_coefficient_si(__isl_take isl_aff *aff,
426 enum isl_dim_type type, int pos, int v)
431 isl_int_set_si(t, v);
432 aff = isl_aff_add_coefficient(aff, type, pos, t);
438 __isl_give isl_div *isl_aff_get_div(__isl_keep isl_aff *aff, int pos)
443 return isl_local_space_get_div(aff->ls, pos);
446 __isl_give isl_aff *isl_aff_neg(__isl_take isl_aff *aff)
448 aff = isl_aff_cow(aff);
451 aff->v = isl_vec_cow(aff->v);
453 return isl_aff_free(aff);
455 isl_seq_neg(aff->v->el + 1, aff->v->el + 1, aff->v->size - 1);
460 /* Given f, return floor(f).
461 * If f is an integer expression, then just return f.
462 * Otherwise, if f = g/m, write g = q m + r,
463 * create a new div d = [r/m] and return the expression q + d.
464 * The coefficients in r are taken to lie between -m/2 and m/2.
466 __isl_give isl_aff *isl_aff_floor(__isl_take isl_aff *aff)
476 if (isl_int_is_one(aff->v->el[0]))
479 aff = isl_aff_cow(aff);
483 aff->v = isl_vec_cow(aff->v);
484 div = isl_vec_copy(aff->v);
485 div = isl_vec_cow(div);
487 return isl_aff_free(aff);
489 ctx = isl_aff_get_ctx(aff);
490 isl_int_fdiv_q(aff->v->el[0], aff->v->el[0], ctx->two);
491 for (i = 1; i < aff->v->size; ++i) {
492 isl_int_fdiv_r(div->el[i], div->el[i], div->el[0]);
493 isl_int_fdiv_q(aff->v->el[i], aff->v->el[i], div->el[0]);
494 if (isl_int_gt(div->el[i], aff->v->el[0])) {
495 isl_int_sub(div->el[i], div->el[i], div->el[0]);
496 isl_int_add_ui(aff->v->el[i], aff->v->el[i], 1);
500 aff->ls = isl_local_space_add_div(aff->ls, div);
502 return isl_aff_free(aff);
505 aff->v = isl_vec_extend(aff->v, size + 1);
507 return isl_aff_free(aff);
508 isl_int_set_si(aff->v->el[0], 1);
509 isl_int_set_si(aff->v->el[size], 1);
516 * aff mod m = aff - m * floor(aff/m)
518 __isl_give isl_aff *isl_aff_mod(__isl_take isl_aff *aff, isl_int m)
522 res = isl_aff_copy(aff);
523 aff = isl_aff_scale_down(aff, m);
524 aff = isl_aff_floor(aff);
525 aff = isl_aff_scale(aff, m);
526 res = isl_aff_sub(res, aff);
533 * pwaff mod m = pwaff - m * floor(pwaff/m)
535 __isl_give isl_pw_aff *isl_pw_aff_mod(__isl_take isl_pw_aff *pwaff, isl_int m)
539 res = isl_pw_aff_copy(pwaff);
540 pwaff = isl_pw_aff_scale_down(pwaff, m);
541 pwaff = isl_pw_aff_floor(pwaff);
542 pwaff = isl_pw_aff_scale(pwaff, m);
543 res = isl_pw_aff_sub(res, pwaff);
548 /* Given f, return ceil(f).
549 * If f is an integer expression, then just return f.
550 * Otherwise, create a new div d = [-f] and return the expression -d.
552 __isl_give isl_aff *isl_aff_ceil(__isl_take isl_aff *aff)
557 if (isl_int_is_one(aff->v->el[0]))
560 aff = isl_aff_neg(aff);
561 aff = isl_aff_floor(aff);
562 aff = isl_aff_neg(aff);
567 /* Apply the expansion computed by isl_merge_divs.
568 * The expansion itself is given by "exp" while the resulting
569 * list of divs is given by "div".
571 __isl_give isl_aff *isl_aff_expand_divs( __isl_take isl_aff *aff,
572 __isl_take isl_mat *div, int *exp)
579 aff = isl_aff_cow(aff);
583 old_n_div = isl_local_space_dim(aff->ls, isl_dim_div);
584 new_n_div = isl_mat_rows(div);
585 if (new_n_div < old_n_div)
586 isl_die(isl_mat_get_ctx(div), isl_error_invalid,
587 "not an expansion", goto error);
589 aff->v = isl_vec_extend(aff->v, aff->v->size + new_n_div - old_n_div);
593 offset = 1 + isl_local_space_offset(aff->ls, isl_dim_div);
595 for (i = new_n_div - 1; i >= 0; --i) {
596 if (j >= 0 && exp[j] == i) {
598 isl_int_swap(aff->v->el[offset + i],
599 aff->v->el[offset + j]);
602 isl_int_set_si(aff->v->el[offset + i], 0);
605 aff->ls = isl_local_space_replace_divs(aff->ls, isl_mat_copy(div));
616 /* Add two affine expressions that live in the same local space.
618 static __isl_give isl_aff *add_expanded(__isl_take isl_aff *aff1,
619 __isl_take isl_aff *aff2)
623 aff1 = isl_aff_cow(aff1);
627 aff1->v = isl_vec_cow(aff1->v);
633 isl_int_gcd(gcd, aff1->v->el[0], aff2->v->el[0]);
634 isl_int_divexact(f, aff2->v->el[0], gcd);
635 isl_seq_scale(aff1->v->el + 1, aff1->v->el + 1, f, aff1->v->size - 1);
636 isl_int_divexact(f, aff1->v->el[0], gcd);
637 isl_seq_addmul(aff1->v->el + 1, f, aff2->v->el + 1, aff1->v->size - 1);
638 isl_int_divexact(f, aff2->v->el[0], gcd);
639 isl_int_mul(aff1->v->el[0], aff1->v->el[0], f);
651 __isl_give isl_aff *isl_aff_add(__isl_take isl_aff *aff1,
652 __isl_take isl_aff *aff2)
662 ctx = isl_aff_get_ctx(aff1);
663 if (!isl_dim_equal(aff1->ls->dim, aff2->ls->dim))
664 isl_die(ctx, isl_error_invalid,
665 "spaces don't match", goto error);
667 if (aff1->ls->div->n_row == 0 && aff2->ls->div->n_row == 0)
668 return add_expanded(aff1, aff2);
670 exp1 = isl_alloc_array(ctx, int, aff1->ls->div->n_row);
671 exp2 = isl_alloc_array(ctx, int, aff2->ls->div->n_row);
675 div = isl_merge_divs(aff1->ls->div, aff2->ls->div, exp1, exp2);
676 aff1 = isl_aff_expand_divs(aff1, isl_mat_copy(div), exp1);
677 aff2 = isl_aff_expand_divs(aff2, div, exp2);
681 return add_expanded(aff1, aff2);
690 __isl_give isl_aff *isl_aff_sub(__isl_take isl_aff *aff1,
691 __isl_take isl_aff *aff2)
693 return isl_aff_add(aff1, isl_aff_neg(aff2));
696 __isl_give isl_aff *isl_aff_scale(__isl_take isl_aff *aff, isl_int f)
700 if (isl_int_is_one(f))
703 aff = isl_aff_cow(aff);
706 aff->v = isl_vec_cow(aff->v);
708 return isl_aff_free(aff);
711 isl_int_gcd(gcd, aff->v->el[0], f);
712 isl_int_divexact(aff->v->el[0], aff->v->el[0], gcd);
713 isl_int_divexact(gcd, f, gcd);
714 isl_seq_scale(aff->v->el + 1, aff->v->el + 1, gcd, aff->v->size - 1);
720 __isl_give isl_aff *isl_aff_scale_down(__isl_take isl_aff *aff, isl_int f)
724 if (isl_int_is_one(f))
727 aff = isl_aff_cow(aff);
730 aff->v = isl_vec_cow(aff->v);
732 return isl_aff_free(aff);
735 isl_seq_gcd(aff->v->el + 1, aff->v->size - 1, &gcd);
736 isl_int_gcd(gcd, gcd, f);
737 isl_seq_scale_down(aff->v->el + 1, aff->v->el + 1, gcd, aff->v->size - 1);
738 isl_int_divexact(gcd, f, gcd);
739 isl_int_mul(aff->v->el[0], aff->v->el[0], gcd);
745 __isl_give isl_aff *isl_aff_scale_down_ui(__isl_take isl_aff *aff, unsigned f)
753 isl_int_set_ui(v, f);
754 aff = isl_aff_scale_down(aff, v);
760 __isl_give isl_aff *isl_aff_set_dim_name(__isl_take isl_aff *aff,
761 enum isl_dim_type type, unsigned pos, const char *s)
763 aff = isl_aff_cow(aff);
766 aff->ls = isl_local_space_set_dim_name(aff->ls, type, pos, s);
768 return isl_aff_free(aff);
773 /* Exploit the equalities in "eq" to simplify the affine expression
774 * and the expressions of the integer divisions in the local space.
775 * The integer divisions in this local space are assumed to appear
776 * as regular dimensions in "eq".
778 static __isl_give isl_aff *isl_aff_substitute_equalities_lifted(
779 __isl_take isl_aff *aff, __isl_take isl_basic_set *eq)
788 isl_basic_set_free(eq);
792 aff = isl_aff_cow(aff);
796 aff->ls = isl_local_space_substitute_equalities(aff->ls,
797 isl_basic_set_copy(eq));
801 total = 1 + isl_dim_total(eq->dim);
803 for (i = 0; i < eq->n_eq; ++i) {
804 j = isl_seq_last_non_zero(eq->eq[i], total + n_div);
805 if (j < 0 || j == 0 || j >= total)
808 isl_seq_elim(aff->v->el + 1, eq->eq[i], j, total,
812 isl_basic_set_free(eq);
815 isl_basic_set_free(eq);
820 /* Exploit the equalities in "eq" to simplify the affine expression
821 * and the expressions of the integer divisions in the local space.
823 static __isl_give isl_aff *isl_aff_substitute_equalities(
824 __isl_take isl_aff *aff, __isl_take isl_basic_set *eq)
830 n_div = isl_local_space_dim(aff->ls, isl_dim_div);
832 eq = isl_basic_set_add(eq, isl_dim_set, n_div);
833 return isl_aff_substitute_equalities_lifted(aff, eq);
835 isl_basic_set_free(eq);
840 /* Look for equalities among the variables shared by context and aff
841 * and the integer divisions of aff, if any.
842 * The equalities are then used to eliminate coefficients and/or integer
843 * divisions from aff.
845 __isl_give isl_aff *isl_aff_gist(__isl_take isl_aff *aff,
846 __isl_take isl_set *context)
853 n_div = isl_local_space_dim(aff->ls, isl_dim_div);
856 context = isl_set_add_dims(context, isl_dim_set, n_div);
857 bset = isl_basic_set_from_local_space(
858 isl_aff_get_local_space(aff));
859 bset = isl_basic_set_lift(bset);
860 bset = isl_basic_set_flatten(bset);
861 context = isl_set_intersect(context,
862 isl_set_from_basic_set(bset));
865 hull = isl_set_affine_hull(context);
866 return isl_aff_substitute_equalities_lifted(aff, hull);
869 isl_set_free(context);
873 /* Return a basic set containing those elements in the space
874 * of aff where it is non-negative.
876 __isl_give isl_basic_set *isl_aff_nonneg_basic_set(__isl_take isl_aff *aff)
878 isl_constraint *ineq;
880 ineq = isl_inequality_from_aff(aff);
882 return isl_basic_set_from_constraint(ineq);
885 /* Return a basic set containing those elements in the space
886 * of aff where it is zero.
888 __isl_give isl_basic_set *isl_aff_zero_basic_set(__isl_take isl_aff *aff)
890 isl_constraint *ineq;
892 ineq = isl_equality_from_aff(aff);
894 return isl_basic_set_from_constraint(ineq);
897 /* Return a basic set containing those elements in the shared space
898 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
900 __isl_give isl_basic_set *isl_aff_ge_basic_set(__isl_take isl_aff *aff1,
901 __isl_take isl_aff *aff2)
903 aff1 = isl_aff_sub(aff1, aff2);
905 return isl_aff_nonneg_basic_set(aff1);
908 __isl_give isl_aff *isl_aff_add_on_domain(__isl_keep isl_set *dom,
909 __isl_take isl_aff *aff1, __isl_take isl_aff *aff2)
911 aff1 = isl_aff_add(aff1, aff2);
912 aff1 = isl_aff_gist(aff1, isl_set_copy(dom));
916 int isl_aff_is_empty(__isl_keep isl_aff *aff)
924 /* Set active[i] to 1 if the dimension at position i is involved
925 * in the affine expression.
927 static int set_active(__isl_keep isl_aff *aff, int *active)
936 total = aff->v->size - 2;
937 for (i = 0; i < total; ++i)
938 active[i] = !isl_int_is_zero(aff->v->el[2 + i]);
940 offset = isl_local_space_offset(aff->ls, isl_dim_div) - 1;
941 for (i = aff->ls->div->n_row - 1; i >= 0; --i) {
942 if (!active[offset + i])
944 for (j = 0; j < total; ++j)
946 !isl_int_is_zero(aff->ls->div->row[i][2 + j]);
952 /* Check whether the given affine expression has non-zero coefficient
953 * for any dimension in the given range or if any of these dimensions
954 * appear with non-zero coefficients in any of the integer divisions
955 * involved in the affine expression.
957 int isl_aff_involves_dims(__isl_keep isl_aff *aff,
958 enum isl_dim_type type, unsigned first, unsigned n)
970 ctx = isl_aff_get_ctx(aff);
971 if (first + n > isl_aff_dim(aff, type))
972 isl_die(ctx, isl_error_invalid,
973 "range out of bounds", return -1);
975 active = isl_calloc_array(ctx, int,
976 isl_local_space_dim(aff->ls, isl_dim_all));
977 if (set_active(aff, active) < 0)
980 first += isl_local_space_offset(aff->ls, type) - 1;
981 for (i = 0; i < n; ++i)
982 if (active[first + i]) {
995 __isl_give isl_aff *isl_aff_drop_dims(__isl_take isl_aff *aff,
996 enum isl_dim_type type, unsigned first, unsigned n)
1002 if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
1005 ctx = isl_aff_get_ctx(aff);
1006 if (first + n > isl_aff_dim(aff, type))
1007 isl_die(ctx, isl_error_invalid, "range out of bounds",
1008 return isl_aff_free(aff));
1010 aff = isl_aff_cow(aff);
1014 aff->ls = isl_local_space_drop_dims(aff->ls, type, first, n);
1016 return isl_aff_free(aff);
1018 first += 1 + isl_local_space_offset(aff->ls, type);
1019 aff->v = isl_vec_drop_els(aff->v, first, n);
1021 return isl_aff_free(aff);
1026 __isl_give isl_aff *isl_aff_insert_dims(__isl_take isl_aff *aff,
1027 enum isl_dim_type type, unsigned first, unsigned n)
1033 if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
1036 ctx = isl_aff_get_ctx(aff);
1037 if (first > isl_aff_dim(aff, type))
1038 isl_die(ctx, isl_error_invalid, "position out of bounds",
1039 return isl_aff_free(aff));
1041 aff = isl_aff_cow(aff);
1045 aff->ls = isl_local_space_insert_dims(aff->ls, type, first, n);
1047 return isl_aff_free(aff);
1049 first += 1 + isl_local_space_offset(aff->ls, type);
1050 aff->v = isl_vec_insert_zero_els(aff->v, first, n);
1052 return isl_aff_free(aff);
1057 __isl_give isl_aff *isl_aff_add_dims(__isl_take isl_aff *aff,
1058 enum isl_dim_type type, unsigned n)
1062 pos = isl_aff_dim(aff, type);
1064 return isl_aff_insert_dims(aff, type, pos, n);
1067 __isl_give isl_pw_aff *isl_pw_aff_add_dims(__isl_take isl_pw_aff *pwaff,
1068 enum isl_dim_type type, unsigned n)
1072 pos = isl_pw_aff_dim(pwaff, type);
1074 return isl_pw_aff_insert_dims(pwaff, type, pos, n);
1077 __isl_give isl_pw_aff *isl_pw_aff_set_tuple_id(__isl_take isl_pw_aff *pwaff,
1078 __isl_take isl_id *id)
1082 dim = isl_pw_aff_get_dim(pwaff);
1083 dim = isl_dim_set_tuple_id(dim, isl_dim_set, id);
1085 return isl_pw_aff_reset_dim(pwaff, dim);
1088 __isl_give isl_pw_aff *isl_pw_aff_from_aff(__isl_take isl_aff *aff)
1090 isl_set *dom = isl_set_universe(isl_aff_get_dim(aff));
1091 return isl_pw_aff_alloc(dom, aff);
1095 #define PW isl_pw_aff
1099 #define EL_IS_ZERO is_empty
1103 #define IS_ZERO is_empty
1109 #define NO_MOVE_DIMS
1113 #include <isl_pw_templ.c>
1115 static __isl_give isl_set *align_params_pw_pw_set_and(
1116 __isl_take isl_pw_aff *pwaff1, __isl_take isl_pw_aff *pwaff2,
1117 __isl_give isl_set *(*fn)(__isl_take isl_pw_aff *pwaff1,
1118 __isl_take isl_pw_aff *pwaff2))
1120 if (!pwaff1 || !pwaff2)
1122 if (isl_dim_match(pwaff1->dim, isl_dim_param,
1123 pwaff2->dim, isl_dim_param))
1124 return fn(pwaff1, pwaff2);
1125 if (!isl_dim_has_named_params(pwaff1->dim) ||
1126 !isl_dim_has_named_params(pwaff2->dim))
1127 isl_die(isl_pw_aff_get_ctx(pwaff1), isl_error_invalid,
1128 "unaligned unnamed parameters", goto error);
1129 pwaff1 = isl_pw_aff_align_params(pwaff1, isl_pw_aff_get_dim(pwaff2));
1130 pwaff2 = isl_pw_aff_align_params(pwaff2, isl_pw_aff_get_dim(pwaff1));
1131 return fn(pwaff1, pwaff2);
1133 isl_pw_aff_free(pwaff1);
1134 isl_pw_aff_free(pwaff2);
1138 /* Compute a piecewise quasi-affine expression with a domain that
1139 * is the union of those of pwaff1 and pwaff2 and such that on each
1140 * cell, the quasi-affine expression is the maximum of those of pwaff1
1141 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
1142 * cell, then the associated expression is the defined one.
1144 static __isl_give isl_pw_aff *pw_aff_union_max(__isl_take isl_pw_aff *pwaff1,
1145 __isl_take isl_pw_aff *pwaff2)
1152 if (!pwaff1 || !pwaff2)
1155 ctx = isl_dim_get_ctx(pwaff1->dim);
1156 if (!isl_dim_equal(pwaff1->dim, pwaff2->dim))
1157 isl_die(ctx, isl_error_invalid,
1158 "arguments should live in same space", goto error);
1160 if (isl_pw_aff_is_empty(pwaff1)) {
1161 isl_pw_aff_free(pwaff1);
1165 if (isl_pw_aff_is_empty(pwaff2)) {
1166 isl_pw_aff_free(pwaff2);
1170 n = 2 * (pwaff1->n + 1) * (pwaff2->n + 1);
1171 res = isl_pw_aff_alloc_(isl_dim_copy(pwaff1->dim), n);
1173 for (i = 0; i < pwaff1->n; ++i) {
1174 set = isl_set_copy(pwaff1->p[i].set);
1175 for (j = 0; j < pwaff2->n; ++j) {
1176 struct isl_set *common;
1179 common = isl_set_intersect(
1180 isl_set_copy(pwaff1->p[i].set),
1181 isl_set_copy(pwaff2->p[j].set));
1182 ge = isl_set_from_basic_set(isl_aff_ge_basic_set(
1183 isl_aff_copy(pwaff2->p[j].aff),
1184 isl_aff_copy(pwaff1->p[i].aff)));
1185 ge = isl_set_intersect(common, ge);
1186 if (isl_set_plain_is_empty(ge)) {
1190 set = isl_set_subtract(set, isl_set_copy(ge));
1192 res = isl_pw_aff_add_piece(res, ge,
1193 isl_aff_copy(pwaff2->p[j].aff));
1195 res = isl_pw_aff_add_piece(res, set,
1196 isl_aff_copy(pwaff1->p[i].aff));
1199 for (j = 0; j < pwaff2->n; ++j) {
1200 set = isl_set_copy(pwaff2->p[j].set);
1201 for (i = 0; i < pwaff1->n; ++i)
1202 set = isl_set_subtract(set,
1203 isl_set_copy(pwaff1->p[i].set));
1204 res = isl_pw_aff_add_piece(res, set,
1205 isl_aff_copy(pwaff2->p[j].aff));
1208 isl_pw_aff_free(pwaff1);
1209 isl_pw_aff_free(pwaff2);
1213 isl_pw_aff_free(pwaff1);
1214 isl_pw_aff_free(pwaff2);
1218 __isl_give isl_pw_aff *isl_pw_aff_union_max(__isl_take isl_pw_aff *pwaff1,
1219 __isl_take isl_pw_aff *pwaff2)
1221 return align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_union_max);
1224 /* Construct a map with as domain the domain of pwaff and
1225 * one-dimensional range corresponding to the affine expressions.
1227 __isl_give isl_map *isl_map_from_pw_aff(__isl_take isl_pw_aff *pwaff)
1236 dim = isl_pw_aff_get_dim(pwaff);
1237 dim = isl_dim_from_domain(dim);
1238 dim = isl_dim_add(dim, isl_dim_out, 1);
1239 map = isl_map_empty(dim);
1241 for (i = 0; i < pwaff->n; ++i) {
1242 isl_basic_map *bmap;
1245 bmap = isl_basic_map_from_aff(isl_aff_copy(pwaff->p[i].aff));
1246 map_i = isl_map_from_basic_map(bmap);
1247 map_i = isl_map_intersect_domain(map_i,
1248 isl_set_copy(pwaff->p[i].set));
1249 map = isl_map_union_disjoint(map, map_i);
1252 isl_pw_aff_free(pwaff);
1257 /* Return a set containing those elements in the domain
1258 * of pwaff where it is non-negative.
1260 __isl_give isl_set *isl_pw_aff_nonneg_set(__isl_take isl_pw_aff *pwaff)
1268 set = isl_set_empty(isl_pw_aff_get_dim(pwaff));
1270 for (i = 0; i < pwaff->n; ++i) {
1271 isl_basic_set *bset;
1274 bset = isl_aff_nonneg_basic_set(isl_aff_copy(pwaff->p[i].aff));
1275 set_i = isl_set_from_basic_set(bset);
1276 set_i = isl_set_intersect(set_i, isl_set_copy(pwaff->p[i].set));
1277 set = isl_set_union_disjoint(set, set_i);
1280 isl_pw_aff_free(pwaff);
1285 /* Return a set containing those elements in the domain
1286 * of pwaff where it is zero.
1288 __isl_give isl_set *isl_pw_aff_zero_set(__isl_take isl_pw_aff *pwaff)
1296 set = isl_set_empty(isl_pw_aff_get_dim(pwaff));
1298 for (i = 0; i < pwaff->n; ++i) {
1299 isl_basic_set *bset;
1302 bset = isl_aff_zero_basic_set(isl_aff_copy(pwaff->p[i].aff));
1303 set_i = isl_set_from_basic_set(bset);
1304 set_i = isl_set_intersect(set_i, isl_set_copy(pwaff->p[i].set));
1305 set = isl_set_union_disjoint(set, set_i);
1308 isl_pw_aff_free(pwaff);
1313 /* Return a set containing those elements in the domain
1314 * of pwaff where it is not zero.
1316 __isl_give isl_set *isl_pw_aff_non_zero_set(__isl_take isl_pw_aff *pwaff)
1318 return isl_set_complement(isl_pw_aff_zero_set(pwaff));
1321 /* Return a set containing those elements in the shared domain
1322 * of pwaff1 and pwaff2 where pwaff1 is greater than (or equal) to pwaff2.
1324 * We compute the difference on the shared domain and then construct
1325 * the set of values where this difference is non-negative.
1326 * If strict is set, we first subtract 1 from the difference.
1327 * If equal is set, we only return the elements where pwaff1 and pwaff2
1330 static __isl_give isl_set *pw_aff_gte_set(__isl_take isl_pw_aff *pwaff1,
1331 __isl_take isl_pw_aff *pwaff2, int strict, int equal)
1333 isl_set *set1, *set2;
1335 set1 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff1));
1336 set2 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff2));
1337 set1 = isl_set_intersect(set1, set2);
1338 pwaff1 = isl_pw_aff_intersect_domain(pwaff1, isl_set_copy(set1));
1339 pwaff2 = isl_pw_aff_intersect_domain(pwaff2, isl_set_copy(set1));
1340 pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_neg(pwaff2));
1343 isl_dim *dim = isl_set_get_dim(set1);
1345 aff = isl_aff_zero(isl_local_space_from_dim(dim));
1346 aff = isl_aff_add_constant_si(aff, -1);
1347 pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_alloc(set1, aff));
1352 return isl_pw_aff_zero_set(pwaff1);
1353 return isl_pw_aff_nonneg_set(pwaff1);
1356 /* Return a set containing those elements in the shared domain
1357 * of pwaff1 and pwaff2 where pwaff1 is equal to pwaff2.
1359 static __isl_give isl_set *pw_aff_eq_set(__isl_take isl_pw_aff *pwaff1,
1360 __isl_take isl_pw_aff *pwaff2)
1362 return pw_aff_gte_set(pwaff1, pwaff2, 0, 1);
1365 __isl_give isl_set *isl_pw_aff_eq_set(__isl_take isl_pw_aff *pwaff1,
1366 __isl_take isl_pw_aff *pwaff2)
1368 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_eq_set);
1371 /* Return a set containing those elements in the shared domain
1372 * of pwaff1 and pwaff2 where pwaff1 is greater than or equal to pwaff2.
1374 static __isl_give isl_set *pw_aff_ge_set(__isl_take isl_pw_aff *pwaff1,
1375 __isl_take isl_pw_aff *pwaff2)
1377 return pw_aff_gte_set(pwaff1, pwaff2, 0, 0);
1380 __isl_give isl_set *isl_pw_aff_ge_set(__isl_take isl_pw_aff *pwaff1,
1381 __isl_take isl_pw_aff *pwaff2)
1383 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_ge_set);
1386 /* Return a set containing those elements in the shared domain
1387 * of pwaff1 and pwaff2 where pwaff1 is strictly greater than pwaff2.
1389 static __isl_give isl_set *pw_aff_gt_set(__isl_take isl_pw_aff *pwaff1,
1390 __isl_take isl_pw_aff *pwaff2)
1392 return pw_aff_gte_set(pwaff1, pwaff2, 1, 0);
1395 __isl_give isl_set *isl_pw_aff_gt_set(__isl_take isl_pw_aff *pwaff1,
1396 __isl_take isl_pw_aff *pwaff2)
1398 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_gt_set);
1401 __isl_give isl_set *isl_pw_aff_le_set(__isl_take isl_pw_aff *pwaff1,
1402 __isl_take isl_pw_aff *pwaff2)
1404 return isl_pw_aff_ge_set(pwaff2, pwaff1);
1407 __isl_give isl_set *isl_pw_aff_lt_set(__isl_take isl_pw_aff *pwaff1,
1408 __isl_take isl_pw_aff *pwaff2)
1410 return isl_pw_aff_gt_set(pwaff2, pwaff1);
1413 /* Return a set containing those elements in the shared domain
1414 * of the elements of list1 and list2 where each element in list1
1415 * has the relation specified by "fn" with each element in list2.
1417 static __isl_give isl_set *pw_aff_list_set(__isl_take isl_pw_aff_list *list1,
1418 __isl_take isl_pw_aff_list *list2,
1419 __isl_give isl_set *(*fn)(__isl_take isl_pw_aff *pwaff1,
1420 __isl_take isl_pw_aff *pwaff2))
1426 if (!list1 || !list2)
1429 ctx = isl_pw_aff_list_get_ctx(list1);
1430 if (list1->n < 1 || list2->n < 1)
1431 isl_die(ctx, isl_error_invalid,
1432 "list should contain at least one element", goto error);
1434 set = isl_set_universe(isl_pw_aff_get_dim(list1->p[0]));
1435 for (i = 0; i < list1->n; ++i)
1436 for (j = 0; j < list2->n; ++j) {
1439 set_ij = fn(isl_pw_aff_copy(list1->p[i]),
1440 isl_pw_aff_copy(list2->p[j]));
1441 set = isl_set_intersect(set, set_ij);
1444 isl_pw_aff_list_free(list1);
1445 isl_pw_aff_list_free(list2);
1448 isl_pw_aff_list_free(list1);
1449 isl_pw_aff_list_free(list2);
1453 /* Return a set containing those elements in the shared domain
1454 * of the elements of list1 and list2 where each element in list1
1455 * is equal to each element in list2.
1457 __isl_give isl_set *isl_pw_aff_list_eq_set(__isl_take isl_pw_aff_list *list1,
1458 __isl_take isl_pw_aff_list *list2)
1460 return pw_aff_list_set(list1, list2, &isl_pw_aff_eq_set);
1463 __isl_give isl_set *isl_pw_aff_list_ne_set(__isl_take isl_pw_aff_list *list1,
1464 __isl_take isl_pw_aff_list *list2)
1466 return pw_aff_list_set(list1, list2, &isl_pw_aff_ne_set);
1469 /* Return a set containing those elements in the shared domain
1470 * of the elements of list1 and list2 where each element in list1
1471 * is less than or equal to each element in list2.
1473 __isl_give isl_set *isl_pw_aff_list_le_set(__isl_take isl_pw_aff_list *list1,
1474 __isl_take isl_pw_aff_list *list2)
1476 return pw_aff_list_set(list1, list2, &isl_pw_aff_le_set);
1479 __isl_give isl_set *isl_pw_aff_list_lt_set(__isl_take isl_pw_aff_list *list1,
1480 __isl_take isl_pw_aff_list *list2)
1482 return pw_aff_list_set(list1, list2, &isl_pw_aff_lt_set);
1485 __isl_give isl_set *isl_pw_aff_list_ge_set(__isl_take isl_pw_aff_list *list1,
1486 __isl_take isl_pw_aff_list *list2)
1488 return pw_aff_list_set(list1, list2, &isl_pw_aff_ge_set);
1491 __isl_give isl_set *isl_pw_aff_list_gt_set(__isl_take isl_pw_aff_list *list1,
1492 __isl_take isl_pw_aff_list *list2)
1494 return pw_aff_list_set(list1, list2, &isl_pw_aff_gt_set);
1498 /* Return a set containing those elements in the shared domain
1499 * of pwaff1 and pwaff2 where pwaff1 is not equal to pwaff2.
1501 static __isl_give isl_set *pw_aff_ne_set(__isl_take isl_pw_aff *pwaff1,
1502 __isl_take isl_pw_aff *pwaff2)
1504 isl_set *set_lt, *set_gt;
1506 set_lt = isl_pw_aff_lt_set(isl_pw_aff_copy(pwaff1),
1507 isl_pw_aff_copy(pwaff2));
1508 set_gt = isl_pw_aff_gt_set(pwaff1, pwaff2);
1509 return isl_set_union_disjoint(set_lt, set_gt);
1512 __isl_give isl_set *isl_pw_aff_ne_set(__isl_take isl_pw_aff *pwaff1,
1513 __isl_take isl_pw_aff *pwaff2)
1515 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_ne_set);
1518 __isl_give isl_pw_aff *isl_pw_aff_scale_down(__isl_take isl_pw_aff *pwaff,
1523 if (isl_int_is_one(v))
1525 if (!isl_int_is_pos(v))
1526 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
1527 "factor needs to be positive",
1528 return isl_pw_aff_free(pwaff));
1529 pwaff = isl_pw_aff_cow(pwaff);
1535 for (i = 0; i < pwaff->n; ++i) {
1536 pwaff->p[i].aff = isl_aff_scale_down(pwaff->p[i].aff, v);
1537 if (!pwaff->p[i].aff)
1538 return isl_pw_aff_free(pwaff);
1544 __isl_give isl_pw_aff *isl_pw_aff_floor(__isl_take isl_pw_aff *pwaff)
1548 pwaff = isl_pw_aff_cow(pwaff);
1554 for (i = 0; i < pwaff->n; ++i) {
1555 pwaff->p[i].aff = isl_aff_floor(pwaff->p[i].aff);
1556 if (!pwaff->p[i].aff)
1557 return isl_pw_aff_free(pwaff);
1563 __isl_give isl_pw_aff *isl_pw_aff_ceil(__isl_take isl_pw_aff *pwaff)
1567 pwaff = isl_pw_aff_cow(pwaff);
1573 for (i = 0; i < pwaff->n; ++i) {
1574 pwaff->p[i].aff = isl_aff_ceil(pwaff->p[i].aff);
1575 if (!pwaff->p[i].aff)
1576 return isl_pw_aff_free(pwaff);
1582 /* Return an affine expression that is equal to pwaff_true for elements
1583 * in "cond" and to pwaff_false for elements not in "cond".
1584 * That is, return cond ? pwaff_true : pwaff_false;
1586 __isl_give isl_pw_aff *isl_pw_aff_cond(__isl_take isl_set *cond,
1587 __isl_take isl_pw_aff *pwaff_true, __isl_take isl_pw_aff *pwaff_false)
1591 comp = isl_set_complement(isl_set_copy(cond));
1592 pwaff_true = isl_pw_aff_intersect_domain(pwaff_true, cond);
1593 pwaff_false = isl_pw_aff_intersect_domain(pwaff_false, comp);
1595 return isl_pw_aff_add_disjoint(pwaff_true, pwaff_false);
1598 int isl_aff_is_cst(__isl_keep isl_aff *aff)
1603 return isl_seq_first_non_zero(aff->v->el + 2, aff->v->size - 2) == -1;
1606 /* Check whether pwaff is a piecewise constant.
1608 int isl_pw_aff_is_cst(__isl_keep isl_pw_aff *pwaff)
1615 for (i = 0; i < pwaff->n; ++i) {
1616 int is_cst = isl_aff_is_cst(pwaff->p[i].aff);
1617 if (is_cst < 0 || !is_cst)
1624 __isl_give isl_aff *isl_aff_mul(__isl_take isl_aff *aff1,
1625 __isl_take isl_aff *aff2)
1627 if (!isl_aff_is_cst(aff2) && isl_aff_is_cst(aff1))
1628 return isl_aff_mul(aff2, aff1);
1630 if (!isl_aff_is_cst(aff2))
1631 isl_die(isl_aff_get_ctx(aff1), isl_error_invalid,
1632 "at least one affine expression should be constant",
1635 aff1 = isl_aff_cow(aff1);
1639 aff1 = isl_aff_scale(aff1, aff2->v->el[1]);
1640 aff1 = isl_aff_scale_down(aff1, aff2->v->el[0]);
1650 static __isl_give isl_pw_aff *pw_aff_mul(__isl_take isl_pw_aff *pwaff1,
1651 __isl_take isl_pw_aff *pwaff2)
1656 if (!pwaff1 || !pwaff2)
1659 n = pwaff1->n * pwaff2->n;
1660 res = isl_pw_aff_alloc_(isl_dim_copy(pwaff1->dim), n);
1662 for (i = 0; i < pwaff1->n; ++i) {
1663 for (j = 0; j < pwaff2->n; ++j) {
1666 common = isl_set_intersect(
1667 isl_set_copy(pwaff1->p[i].set),
1668 isl_set_copy(pwaff2->p[j].set));
1669 if (isl_set_plain_is_empty(common)) {
1670 isl_set_free(common);
1674 prod = isl_aff_mul(isl_aff_copy(pwaff1->p[i].aff),
1675 isl_aff_copy(pwaff2->p[j].aff));
1677 res = isl_pw_aff_add_piece(res, common, prod);
1681 isl_pw_aff_free(pwaff1);
1682 isl_pw_aff_free(pwaff2);
1685 isl_pw_aff_free(pwaff1);
1686 isl_pw_aff_free(pwaff2);
1690 __isl_give isl_pw_aff *isl_pw_aff_mul(__isl_take isl_pw_aff *pwaff1,
1691 __isl_take isl_pw_aff *pwaff2)
1693 return align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_mul);
1696 static __isl_give isl_pw_aff *pw_aff_min(__isl_take isl_pw_aff *pwaff1,
1697 __isl_take isl_pw_aff *pwaff2)
1701 le = isl_pw_aff_le_set(isl_pw_aff_copy(pwaff1),
1702 isl_pw_aff_copy(pwaff2));
1703 return isl_pw_aff_cond(le, pwaff1, pwaff2);
1706 __isl_give isl_pw_aff *isl_pw_aff_min(__isl_take isl_pw_aff *pwaff1,
1707 __isl_take isl_pw_aff *pwaff2)
1709 return align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_min);
1712 static __isl_give isl_pw_aff *pw_aff_max(__isl_take isl_pw_aff *pwaff1,
1713 __isl_take isl_pw_aff *pwaff2)
1717 le = isl_pw_aff_ge_set(isl_pw_aff_copy(pwaff1),
1718 isl_pw_aff_copy(pwaff2));
1719 return isl_pw_aff_cond(le, pwaff1, pwaff2);
1722 __isl_give isl_pw_aff *isl_pw_aff_max(__isl_take isl_pw_aff *pwaff1,
1723 __isl_take isl_pw_aff *pwaff2)
1725 return align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_max);
1728 static __isl_give isl_pw_aff *pw_aff_list_reduce(
1729 __isl_take isl_pw_aff_list *list,
1730 __isl_give isl_pw_aff *(*fn)(__isl_take isl_pw_aff *pwaff1,
1731 __isl_take isl_pw_aff *pwaff2))
1740 ctx = isl_pw_aff_list_get_ctx(list);
1742 isl_die(ctx, isl_error_invalid,
1743 "list should contain at least one element",
1744 return isl_pw_aff_list_free(list));
1746 res = isl_pw_aff_copy(list->p[0]);
1747 for (i = 1; i < list->n; ++i)
1748 res = fn(res, isl_pw_aff_copy(list->p[i]));
1750 isl_pw_aff_list_free(list);
1754 /* Return an isl_pw_aff that maps each element in the intersection of the
1755 * domains of the elements of list to the minimal corresponding affine
1758 __isl_give isl_pw_aff *isl_pw_aff_list_min(__isl_take isl_pw_aff_list *list)
1760 return pw_aff_list_reduce(list, &isl_pw_aff_min);
1763 /* Return an isl_pw_aff that maps each element in the intersection of the
1764 * domains of the elements of list to the maximal corresponding affine
1767 __isl_give isl_pw_aff *isl_pw_aff_list_max(__isl_take isl_pw_aff_list *list)
1769 return pw_aff_list_reduce(list, &isl_pw_aff_max);