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_map_private.h>
15 #include <isl_aff_private.h>
16 #include <isl_dim_private.h>
17 #include <isl_local_space_private.h>
18 #include <isl_mat_private.h>
19 #include <isl_list_private.h>
20 #include <isl/constraint.h>
24 __isl_give isl_aff *isl_aff_alloc_vec(__isl_take isl_local_space *ls,
25 __isl_take isl_vec *v)
32 aff = isl_calloc_type(v->ctx, struct isl_aff);
42 isl_local_space_free(ls);
47 __isl_give isl_aff *isl_aff_alloc(__isl_take isl_local_space *ls)
56 ctx = isl_local_space_get_ctx(ls);
57 if (!isl_local_space_divs_known(ls))
58 isl_die(ctx, isl_error_invalid, "local space has unknown divs",
61 total = isl_local_space_dim(ls, isl_dim_all);
62 v = isl_vec_alloc(ctx, 1 + 1 + total);
63 return isl_aff_alloc_vec(ls, v);
65 isl_local_space_free(ls);
69 __isl_give isl_aff *isl_aff_zero(__isl_take isl_local_space *ls)
73 aff = isl_aff_alloc(ls);
77 isl_int_set_si(aff->v->el[0], 1);
78 isl_seq_clr(aff->v->el + 1, aff->v->size - 1);
83 __isl_give isl_aff *isl_aff_copy(__isl_keep isl_aff *aff)
92 __isl_give isl_aff *isl_aff_dup(__isl_keep isl_aff *aff)
97 return isl_aff_alloc_vec(isl_local_space_copy(aff->ls),
98 isl_vec_copy(aff->v));
101 __isl_give isl_aff *isl_aff_cow(__isl_take isl_aff *aff)
109 return isl_aff_dup(aff);
112 void *isl_aff_free(__isl_take isl_aff *aff)
120 isl_local_space_free(aff->ls);
121 isl_vec_free(aff->v);
128 isl_ctx *isl_aff_get_ctx(__isl_keep isl_aff *aff)
130 return aff ? isl_local_space_get_ctx(aff->ls) : NULL;
133 int isl_aff_dim(__isl_keep isl_aff *aff, enum isl_dim_type type)
135 return aff ? isl_local_space_dim(aff->ls, type) : 0;
138 __isl_give isl_dim *isl_aff_get_dim(__isl_keep isl_aff *aff)
140 return aff ? isl_local_space_get_dim(aff->ls) : NULL;
143 __isl_give isl_local_space *isl_aff_get_local_space(__isl_keep isl_aff *aff)
145 return aff ? isl_local_space_copy(aff->ls) : NULL;
148 const char *isl_aff_get_dim_name(__isl_keep isl_aff *aff,
149 enum isl_dim_type type, unsigned pos)
151 return aff ? isl_local_space_get_dim_name(aff->ls, type, pos) : 0;
154 __isl_give isl_aff *isl_aff_reset_dim(__isl_take isl_aff *aff,
155 __isl_take isl_dim *dim)
157 aff = isl_aff_cow(aff);
161 aff->ls = isl_local_space_reset_dim(aff->ls, dim);
163 return isl_aff_free(aff);
172 /* Reorder the coefficients of the affine expression based
173 * on the given reodering.
174 * The reordering r is assumed to have been extended with the local
177 static __isl_give isl_vec *vec_reorder(__isl_take isl_vec *vec,
178 __isl_take isl_reordering *r, int n_div)
186 res = isl_vec_alloc(vec->ctx, 2 + isl_dim_total(r->dim) + n_div);
187 isl_seq_cpy(res->el, vec->el, 2);
188 isl_seq_clr(res->el + 2, res->size - 2);
189 for (i = 0; i < r->len; ++i)
190 isl_int_set(res->el[2 + r->pos[i]], vec->el[2 + i]);
192 isl_reordering_free(r);
197 isl_reordering_free(r);
201 /* Reorder the dimensions of "aff" according to the given reordering.
203 __isl_give isl_aff *isl_aff_realign(__isl_take isl_aff *aff,
204 __isl_take isl_reordering *r)
206 aff = isl_aff_cow(aff);
210 r = isl_reordering_extend(r, aff->ls->div->n_row);
211 aff->v = vec_reorder(aff->v, isl_reordering_copy(r),
212 aff->ls->div->n_row);
213 aff->ls = isl_local_space_realign(aff->ls, r);
215 if (!aff->v || !aff->ls)
216 return isl_aff_free(aff);
221 isl_reordering_free(r);
225 int isl_aff_plain_is_zero(__isl_keep isl_aff *aff)
230 return isl_seq_first_non_zero(aff->v->el + 1, aff->v->size - 1) < 0;
233 int isl_aff_plain_is_equal(__isl_keep isl_aff *aff1, __isl_keep isl_aff *aff2)
240 equal = isl_local_space_is_equal(aff1->ls, aff2->ls);
241 if (equal < 0 || !equal)
244 return isl_vec_is_equal(aff1->v, aff2->v);
247 int isl_aff_get_denominator(__isl_keep isl_aff *aff, isl_int *v)
251 isl_int_set(*v, aff->v->el[0]);
255 int isl_aff_get_constant(__isl_keep isl_aff *aff, isl_int *v)
259 isl_int_set(*v, aff->v->el[1]);
263 int isl_aff_get_coefficient(__isl_keep isl_aff *aff,
264 enum isl_dim_type type, int pos, isl_int *v)
269 if (pos >= isl_local_space_dim(aff->ls, type))
270 isl_die(aff->v->ctx, isl_error_invalid,
271 "position out of bounds", return -1);
273 pos += isl_local_space_offset(aff->ls, type);
274 isl_int_set(*v, aff->v->el[1 + pos]);
279 __isl_give isl_aff *isl_aff_set_denominator(__isl_take isl_aff *aff, isl_int v)
281 aff = isl_aff_cow(aff);
285 aff->v = isl_vec_cow(aff->v);
287 return isl_aff_free(aff);
289 isl_int_set(aff->v->el[0], v);
294 __isl_give isl_aff *isl_aff_set_constant(__isl_take isl_aff *aff, isl_int v)
296 aff = isl_aff_cow(aff);
300 aff->v = isl_vec_cow(aff->v);
302 return isl_aff_free(aff);
304 isl_int_set(aff->v->el[1], v);
309 __isl_give isl_aff *isl_aff_add_constant(__isl_take isl_aff *aff, isl_int v)
311 if (isl_int_is_zero(v))
314 aff = isl_aff_cow(aff);
318 aff->v = isl_vec_cow(aff->v);
320 return isl_aff_free(aff);
322 isl_int_addmul(aff->v->el[1], aff->v->el[0], v);
327 __isl_give isl_aff *isl_aff_add_constant_si(__isl_take isl_aff *aff, int v)
332 isl_int_set_si(t, v);
333 aff = isl_aff_add_constant(aff, t);
339 __isl_give isl_aff *isl_aff_set_constant_si(__isl_take isl_aff *aff, int v)
341 aff = isl_aff_cow(aff);
345 aff->v = isl_vec_cow(aff->v);
347 return isl_aff_free(aff);
349 isl_int_set_si(aff->v->el[1], v);
354 __isl_give isl_aff *isl_aff_set_coefficient(__isl_take isl_aff *aff,
355 enum isl_dim_type type, int pos, isl_int v)
360 if (pos >= isl_local_space_dim(aff->ls, type))
361 isl_die(aff->v->ctx, isl_error_invalid,
362 "position out of bounds", return isl_aff_free(aff));
364 aff = isl_aff_cow(aff);
368 aff->v = isl_vec_cow(aff->v);
370 return isl_aff_free(aff);
372 pos += isl_local_space_offset(aff->ls, type);
373 isl_int_set(aff->v->el[1 + pos], v);
378 __isl_give isl_aff *isl_aff_set_coefficient_si(__isl_take isl_aff *aff,
379 enum isl_dim_type type, int pos, int v)
384 if (pos >= isl_local_space_dim(aff->ls, type))
385 isl_die(aff->v->ctx, isl_error_invalid,
386 "position out of bounds", return isl_aff_free(aff));
388 aff = isl_aff_cow(aff);
392 aff->v = isl_vec_cow(aff->v);
394 return isl_aff_free(aff);
396 pos += isl_local_space_offset(aff->ls, type);
397 isl_int_set_si(aff->v->el[1 + pos], v);
402 __isl_give isl_aff *isl_aff_add_coefficient(__isl_take isl_aff *aff,
403 enum isl_dim_type type, int pos, isl_int v)
408 if (pos >= isl_local_space_dim(aff->ls, type))
409 isl_die(aff->v->ctx, isl_error_invalid,
410 "position out of bounds", return isl_aff_free(aff));
412 aff = isl_aff_cow(aff);
416 aff->v = isl_vec_cow(aff->v);
418 return isl_aff_free(aff);
420 pos += isl_local_space_offset(aff->ls, type);
421 isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v);
426 __isl_give isl_aff *isl_aff_add_coefficient_si(__isl_take isl_aff *aff,
427 enum isl_dim_type type, int pos, int v)
432 isl_int_set_si(t, v);
433 aff = isl_aff_add_coefficient(aff, type, pos, t);
439 __isl_give isl_div *isl_aff_get_div(__isl_keep isl_aff *aff, int pos)
444 return isl_local_space_get_div(aff->ls, pos);
447 __isl_give isl_aff *isl_aff_neg(__isl_take isl_aff *aff)
449 aff = isl_aff_cow(aff);
452 aff->v = isl_vec_cow(aff->v);
454 return isl_aff_free(aff);
456 isl_seq_neg(aff->v->el + 1, aff->v->el + 1, aff->v->size - 1);
461 /* Given f, return floor(f).
462 * If f is an integer expression, then just return f.
463 * Otherwise, create a new div d = [f] and return the expression d.
465 __isl_give isl_aff *isl_aff_floor(__isl_take isl_aff *aff)
473 if (isl_int_is_one(aff->v->el[0]))
476 aff = isl_aff_cow(aff);
480 aff->ls = isl_local_space_add_div(aff->ls, isl_vec_copy(aff->v));
482 return isl_aff_free(aff);
484 ctx = isl_aff_get_ctx(aff);
486 isl_vec_free(aff->v);
487 aff->v = isl_vec_alloc(ctx, size + 1);
488 aff->v = isl_vec_clr(aff->v);
490 return isl_aff_free(aff);
491 isl_int_set_si(aff->v->el[0], 1);
492 isl_int_set_si(aff->v->el[size], 1);
499 * aff mod m = aff - m * floor(aff/m)
501 __isl_give isl_aff *isl_aff_mod(__isl_take isl_aff *aff, isl_int m)
505 res = isl_aff_copy(aff);
506 aff = isl_aff_scale_down(aff, m);
507 aff = isl_aff_floor(aff);
508 aff = isl_aff_scale(aff, m);
509 res = isl_aff_sub(res, aff);
516 * pwaff mod m = pwaff - m * floor(pwaff/m)
518 __isl_give isl_pw_aff *isl_pw_aff_mod(__isl_take isl_pw_aff *pwaff, isl_int m)
522 res = isl_pw_aff_copy(pwaff);
523 pwaff = isl_pw_aff_scale_down(pwaff, m);
524 pwaff = isl_pw_aff_floor(pwaff);
525 pwaff = isl_pw_aff_scale(pwaff, m);
526 res = isl_pw_aff_sub(res, pwaff);
531 /* Given f, return ceil(f).
532 * If f is an integer expression, then just return f.
533 * Otherwise, create a new div d = [-f] and return the expression -d.
535 __isl_give isl_aff *isl_aff_ceil(__isl_take isl_aff *aff)
540 if (isl_int_is_one(aff->v->el[0]))
543 aff = isl_aff_neg(aff);
544 aff = isl_aff_floor(aff);
545 aff = isl_aff_neg(aff);
550 /* Apply the expansion computed by isl_merge_divs.
551 * The expansion itself is given by "exp" while the resulting
552 * list of divs is given by "div".
554 __isl_give isl_aff *isl_aff_expand_divs( __isl_take isl_aff *aff,
555 __isl_take isl_mat *div, int *exp)
562 aff = isl_aff_cow(aff);
566 old_n_div = isl_local_space_dim(aff->ls, isl_dim_div);
567 new_n_div = isl_mat_rows(div);
568 if (new_n_div < old_n_div)
569 isl_die(isl_mat_get_ctx(div), isl_error_invalid,
570 "not an expansion", goto error);
572 aff->v = isl_vec_extend(aff->v, aff->v->size + new_n_div - old_n_div);
576 offset = 1 + isl_local_space_offset(aff->ls, isl_dim_div);
578 for (i = new_n_div - 1; i >= 0; --i) {
579 if (j >= 0 && exp[j] == i) {
581 isl_int_swap(aff->v->el[offset + i],
582 aff->v->el[offset + j]);
585 isl_int_set_si(aff->v->el[offset + i], 0);
588 aff->ls = isl_local_space_replace_divs(aff->ls, isl_mat_copy(div));
599 /* Add two affine expressions that live in the same local space.
601 static __isl_give isl_aff *add_expanded(__isl_take isl_aff *aff1,
602 __isl_take isl_aff *aff2)
606 aff1 = isl_aff_cow(aff1);
610 aff1->v = isl_vec_cow(aff1->v);
616 isl_int_gcd(gcd, aff1->v->el[0], aff2->v->el[0]);
617 isl_int_divexact(f, aff2->v->el[0], gcd);
618 isl_seq_scale(aff1->v->el + 1, aff1->v->el + 1, f, aff1->v->size - 1);
619 isl_int_divexact(f, aff1->v->el[0], gcd);
620 isl_seq_addmul(aff1->v->el + 1, f, aff2->v->el + 1, aff1->v->size - 1);
621 isl_int_divexact(f, aff2->v->el[0], gcd);
622 isl_int_mul(aff1->v->el[0], aff1->v->el[0], f);
634 __isl_give isl_aff *isl_aff_add(__isl_take isl_aff *aff1,
635 __isl_take isl_aff *aff2)
645 ctx = isl_aff_get_ctx(aff1);
646 if (!isl_dim_equal(aff1->ls->dim, aff2->ls->dim))
647 isl_die(ctx, isl_error_invalid,
648 "spaces don't match", goto error);
650 if (aff1->ls->div->n_row == 0 && aff2->ls->div->n_row == 0)
651 return add_expanded(aff1, aff2);
653 exp1 = isl_alloc_array(ctx, int, aff1->ls->div->n_row);
654 exp2 = isl_alloc_array(ctx, int, aff2->ls->div->n_row);
658 div = isl_merge_divs(aff1->ls->div, aff2->ls->div, exp1, exp2);
659 aff1 = isl_aff_expand_divs(aff1, isl_mat_copy(div), exp1);
660 aff2 = isl_aff_expand_divs(aff2, div, exp2);
664 return add_expanded(aff1, aff2);
673 __isl_give isl_aff *isl_aff_sub(__isl_take isl_aff *aff1,
674 __isl_take isl_aff *aff2)
676 return isl_aff_add(aff1, isl_aff_neg(aff2));
679 __isl_give isl_aff *isl_aff_scale(__isl_take isl_aff *aff, isl_int f)
683 if (isl_int_is_one(f))
686 aff = isl_aff_cow(aff);
689 aff->v = isl_vec_cow(aff->v);
691 return isl_aff_free(aff);
694 isl_int_gcd(gcd, aff->v->el[0], f);
695 isl_int_divexact(aff->v->el[0], aff->v->el[0], gcd);
696 isl_int_divexact(gcd, f, gcd);
697 isl_seq_scale(aff->v->el + 1, aff->v->el + 1, gcd, aff->v->size - 1);
703 __isl_give isl_aff *isl_aff_scale_down(__isl_take isl_aff *aff, isl_int f)
707 if (isl_int_is_one(f))
710 aff = isl_aff_cow(aff);
713 aff->v = isl_vec_cow(aff->v);
715 return isl_aff_free(aff);
718 isl_seq_gcd(aff->v->el + 1, aff->v->size - 1, &gcd);
719 isl_int_gcd(gcd, gcd, f);
720 isl_seq_scale_down(aff->v->el + 1, aff->v->el + 1, gcd, aff->v->size - 1);
721 isl_int_divexact(gcd, f, gcd);
722 isl_int_mul(aff->v->el[0], aff->v->el[0], gcd);
728 __isl_give isl_aff *isl_aff_scale_down_ui(__isl_take isl_aff *aff, unsigned f)
736 isl_int_set_ui(v, f);
737 aff = isl_aff_scale_down(aff, v);
743 __isl_give isl_aff *isl_aff_set_dim_name(__isl_take isl_aff *aff,
744 enum isl_dim_type type, unsigned pos, const char *s)
746 aff = isl_aff_cow(aff);
749 aff->ls = isl_local_space_set_dim_name(aff->ls, type, pos, s);
751 return isl_aff_free(aff);
756 /* Exploit the equalities in "eq" to simplify the affine expression
757 * and the expressions of the integer divisions in the local space.
758 * The integer divisions in this local space are assumed to appear
759 * as regular dimensions in "eq".
761 static __isl_give isl_aff *isl_aff_substitute_equalities_lifted(
762 __isl_take isl_aff *aff, __isl_take isl_basic_set *eq)
771 isl_basic_set_free(eq);
775 aff = isl_aff_cow(aff);
779 aff->ls = isl_local_space_substitute_equalities(aff->ls,
780 isl_basic_set_copy(eq));
784 total = 1 + isl_dim_total(eq->dim);
786 for (i = 0; i < eq->n_eq; ++i) {
787 j = isl_seq_last_non_zero(eq->eq[i], total + n_div);
788 if (j < 0 || j == 0 || j >= total)
791 isl_seq_elim(aff->v->el + 1, eq->eq[i], j, total,
795 isl_basic_set_free(eq);
798 isl_basic_set_free(eq);
803 /* Exploit the equalities in "eq" to simplify the affine expression
804 * and the expressions of the integer divisions in the local space.
806 static __isl_give isl_aff *isl_aff_substitute_equalities(
807 __isl_take isl_aff *aff, __isl_take isl_basic_set *eq)
813 n_div = isl_local_space_dim(aff->ls, isl_dim_div);
815 eq = isl_basic_set_add(eq, isl_dim_set, n_div);
816 return isl_aff_substitute_equalities_lifted(aff, eq);
818 isl_basic_set_free(eq);
823 /* Look for equalities among the variables shared by context and aff
824 * and the integer divisions of aff, if any.
825 * The equalities are then used to eliminate coefficients and/or integer
826 * divisions from aff.
828 __isl_give isl_aff *isl_aff_gist(__isl_take isl_aff *aff,
829 __isl_take isl_set *context)
836 n_div = isl_local_space_dim(aff->ls, isl_dim_div);
839 context = isl_set_add_dims(context, isl_dim_set, n_div);
840 bset = isl_basic_set_from_local_space(
841 isl_aff_get_local_space(aff));
842 bset = isl_basic_set_lift(bset);
843 bset = isl_basic_set_flatten(bset);
844 context = isl_set_intersect(context,
845 isl_set_from_basic_set(bset));
848 hull = isl_set_affine_hull(context);
849 return isl_aff_substitute_equalities_lifted(aff, hull);
852 isl_set_free(context);
856 /* Return a basic set containing those elements in the space
857 * of aff where it is non-negative.
859 __isl_give isl_basic_set *isl_aff_nonneg_basic_set(__isl_take isl_aff *aff)
861 isl_constraint *ineq;
863 ineq = isl_inequality_from_aff(aff);
865 return isl_basic_set_from_constraint(ineq);
868 /* Return a basic set containing those elements in the space
869 * of aff where it is zero.
871 __isl_give isl_basic_set *isl_aff_zero_basic_set(__isl_take isl_aff *aff)
873 isl_constraint *ineq;
875 ineq = isl_equality_from_aff(aff);
877 return isl_basic_set_from_constraint(ineq);
880 /* Return a basic set containing those elements in the shared space
881 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
883 __isl_give isl_basic_set *isl_aff_ge_basic_set(__isl_take isl_aff *aff1,
884 __isl_take isl_aff *aff2)
886 aff1 = isl_aff_sub(aff1, aff2);
888 return isl_aff_nonneg_basic_set(aff1);
891 __isl_give isl_aff *isl_aff_add_on_domain(__isl_keep isl_set *dom,
892 __isl_take isl_aff *aff1, __isl_take isl_aff *aff2)
894 aff1 = isl_aff_add(aff1, aff2);
895 aff1 = isl_aff_gist(aff1, isl_set_copy(dom));
899 int isl_aff_is_empty(__isl_keep isl_aff *aff)
907 /* Set active[i] to 1 if the dimension at position i is involved
908 * in the affine expression.
910 static int set_active(__isl_keep isl_aff *aff, int *active)
919 total = aff->v->size - 2;
920 for (i = 0; i < total; ++i)
921 active[i] = !isl_int_is_zero(aff->v->el[2 + i]);
923 offset = isl_local_space_offset(aff->ls, isl_dim_div) - 1;
924 for (i = aff->ls->div->n_row - 1; i >= 0; --i) {
925 if (!active[offset + i])
927 for (j = 0; j < total; ++j)
929 !isl_int_is_zero(aff->ls->div->row[i][2 + j]);
935 /* Check whether the given affine expression has non-zero coefficient
936 * for any dimension in the given range or if any of these dimensions
937 * appear with non-zero coefficients in any of the integer divisions
938 * involved in the affine expression.
940 int isl_aff_involves_dims(__isl_keep isl_aff *aff,
941 enum isl_dim_type type, unsigned first, unsigned n)
953 ctx = isl_aff_get_ctx(aff);
954 if (first + n > isl_aff_dim(aff, type))
955 isl_die(ctx, isl_error_invalid,
956 "range out of bounds", return -1);
958 active = isl_calloc_array(ctx, int,
959 isl_local_space_dim(aff->ls, isl_dim_all));
960 if (set_active(aff, active) < 0)
963 first += isl_local_space_offset(aff->ls, type) - 1;
964 for (i = 0; i < n; ++i)
965 if (active[first + i]) {
978 __isl_give isl_aff *isl_aff_drop_dims(__isl_take isl_aff *aff,
979 enum isl_dim_type type, unsigned first, unsigned n)
985 if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
988 ctx = isl_aff_get_ctx(aff);
989 if (first + n > isl_aff_dim(aff, type))
990 isl_die(ctx, isl_error_invalid, "range out of bounds",
991 return isl_aff_free(aff));
993 aff = isl_aff_cow(aff);
997 aff->ls = isl_local_space_drop_dims(aff->ls, type, first, n);
999 return isl_aff_free(aff);
1001 first += 1 + isl_local_space_offset(aff->ls, type);
1002 aff->v = isl_vec_drop_els(aff->v, first, n);
1004 return isl_aff_free(aff);
1009 __isl_give isl_aff *isl_aff_insert_dims(__isl_take isl_aff *aff,
1010 enum isl_dim_type type, unsigned first, unsigned n)
1016 if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
1019 ctx = isl_aff_get_ctx(aff);
1020 if (first > isl_aff_dim(aff, type))
1021 isl_die(ctx, isl_error_invalid, "position out of bounds",
1022 return isl_aff_free(aff));
1024 aff = isl_aff_cow(aff);
1028 aff->ls = isl_local_space_insert_dims(aff->ls, type, first, n);
1030 return isl_aff_free(aff);
1032 first += 1 + isl_local_space_offset(aff->ls, type);
1033 aff->v = isl_vec_insert_zero_els(aff->v, first, n);
1035 return isl_aff_free(aff);
1040 __isl_give isl_aff *isl_aff_add_dims(__isl_take isl_aff *aff,
1041 enum isl_dim_type type, unsigned n)
1045 pos = isl_aff_dim(aff, type);
1047 return isl_aff_insert_dims(aff, type, pos, n);
1050 __isl_give isl_pw_aff *isl_pw_aff_add_dims(__isl_take isl_pw_aff *pwaff,
1051 enum isl_dim_type type, unsigned n)
1055 pos = isl_pw_aff_dim(pwaff, type);
1057 return isl_pw_aff_insert_dims(pwaff, type, pos, n);
1060 __isl_give isl_pw_aff *isl_pw_aff_set_tuple_id(__isl_take isl_pw_aff *pwaff,
1061 __isl_take isl_id *id)
1065 dim = isl_pw_aff_get_dim(pwaff);
1066 dim = isl_dim_set_tuple_id(dim, isl_dim_set, id);
1068 return isl_pw_aff_reset_dim(pwaff, dim);
1071 __isl_give isl_pw_aff *isl_pw_aff_from_aff(__isl_take isl_aff *aff)
1073 isl_set *dom = isl_set_universe(isl_aff_get_dim(aff));
1074 return isl_pw_aff_alloc(dom, aff);
1078 #define PW isl_pw_aff
1082 #define EL_IS_ZERO is_empty
1086 #define IS_ZERO is_empty
1092 #define NO_MOVE_DIMS
1096 #include <isl_pw_templ.c>
1098 static __isl_give isl_set *align_params_pw_pw_set_and(
1099 __isl_take isl_pw_aff *pwaff1, __isl_take isl_pw_aff *pwaff2,
1100 __isl_give isl_set *(*fn)(__isl_take isl_pw_aff *pwaff1,
1101 __isl_take isl_pw_aff *pwaff2))
1103 if (!pwaff1 || !pwaff2)
1105 if (isl_dim_match(pwaff1->dim, isl_dim_param,
1106 pwaff2->dim, isl_dim_param))
1107 return fn(pwaff1, pwaff2);
1108 if (!isl_dim_has_named_params(pwaff1->dim) ||
1109 !isl_dim_has_named_params(pwaff2->dim))
1110 isl_die(isl_pw_aff_get_ctx(pwaff1), isl_error_invalid,
1111 "unaligned unnamed parameters", goto error);
1112 pwaff1 = isl_pw_aff_align_params(pwaff1, isl_pw_aff_get_dim(pwaff2));
1113 pwaff2 = isl_pw_aff_align_params(pwaff2, isl_pw_aff_get_dim(pwaff1));
1114 return fn(pwaff1, pwaff2);
1116 isl_pw_aff_free(pwaff1);
1117 isl_pw_aff_free(pwaff2);
1121 /* Compute a piecewise quasi-affine expression with a domain that
1122 * is the union of those of pwaff1 and pwaff2 and such that on each
1123 * cell, the quasi-affine expression is the maximum of those of pwaff1
1124 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
1125 * cell, then the associated expression is the defined one.
1127 static __isl_give isl_pw_aff *pw_aff_union_max(__isl_take isl_pw_aff *pwaff1,
1128 __isl_take isl_pw_aff *pwaff2)
1135 if (!pwaff1 || !pwaff2)
1138 ctx = isl_dim_get_ctx(pwaff1->dim);
1139 if (!isl_dim_equal(pwaff1->dim, pwaff2->dim))
1140 isl_die(ctx, isl_error_invalid,
1141 "arguments should live in same space", goto error);
1143 if (isl_pw_aff_is_empty(pwaff1)) {
1144 isl_pw_aff_free(pwaff1);
1148 if (isl_pw_aff_is_empty(pwaff2)) {
1149 isl_pw_aff_free(pwaff2);
1153 n = 2 * (pwaff1->n + 1) * (pwaff2->n + 1);
1154 res = isl_pw_aff_alloc_(isl_dim_copy(pwaff1->dim), n);
1156 for (i = 0; i < pwaff1->n; ++i) {
1157 set = isl_set_copy(pwaff1->p[i].set);
1158 for (j = 0; j < pwaff2->n; ++j) {
1159 struct isl_set *common;
1162 common = isl_set_intersect(
1163 isl_set_copy(pwaff1->p[i].set),
1164 isl_set_copy(pwaff2->p[j].set));
1165 ge = isl_set_from_basic_set(isl_aff_ge_basic_set(
1166 isl_aff_copy(pwaff2->p[j].aff),
1167 isl_aff_copy(pwaff1->p[i].aff)));
1168 ge = isl_set_intersect(common, ge);
1169 if (isl_set_plain_is_empty(ge)) {
1173 set = isl_set_subtract(set, isl_set_copy(ge));
1175 res = isl_pw_aff_add_piece(res, ge,
1176 isl_aff_copy(pwaff2->p[j].aff));
1178 res = isl_pw_aff_add_piece(res, set,
1179 isl_aff_copy(pwaff1->p[i].aff));
1182 for (j = 0; j < pwaff2->n; ++j) {
1183 set = isl_set_copy(pwaff2->p[j].set);
1184 for (i = 0; i < pwaff1->n; ++i)
1185 set = isl_set_subtract(set,
1186 isl_set_copy(pwaff1->p[i].set));
1187 res = isl_pw_aff_add_piece(res, set,
1188 isl_aff_copy(pwaff2->p[j].aff));
1191 isl_pw_aff_free(pwaff1);
1192 isl_pw_aff_free(pwaff2);
1196 isl_pw_aff_free(pwaff1);
1197 isl_pw_aff_free(pwaff2);
1201 __isl_give isl_pw_aff *isl_pw_aff_union_max(__isl_take isl_pw_aff *pwaff1,
1202 __isl_take isl_pw_aff *pwaff2)
1204 return align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_union_max);
1207 /* Construct a map with as domain the domain of pwaff and
1208 * one-dimensional range corresponding to the affine expressions.
1210 __isl_give isl_map *isl_map_from_pw_aff(__isl_take isl_pw_aff *pwaff)
1219 dim = isl_pw_aff_get_dim(pwaff);
1220 dim = isl_dim_from_domain(dim);
1221 dim = isl_dim_add(dim, isl_dim_out, 1);
1222 map = isl_map_empty(dim);
1224 for (i = 0; i < pwaff->n; ++i) {
1225 isl_basic_map *bmap;
1228 bmap = isl_basic_map_from_aff(isl_aff_copy(pwaff->p[i].aff));
1229 map_i = isl_map_from_basic_map(bmap);
1230 map_i = isl_map_intersect_domain(map_i,
1231 isl_set_copy(pwaff->p[i].set));
1232 map = isl_map_union_disjoint(map, map_i);
1235 isl_pw_aff_free(pwaff);
1240 /* Return a set containing those elements in the domain
1241 * of pwaff where it is non-negative.
1243 __isl_give isl_set *isl_pw_aff_nonneg_set(__isl_take isl_pw_aff *pwaff)
1251 set = isl_set_empty(isl_pw_aff_get_dim(pwaff));
1253 for (i = 0; i < pwaff->n; ++i) {
1254 isl_basic_set *bset;
1257 bset = isl_aff_nonneg_basic_set(isl_aff_copy(pwaff->p[i].aff));
1258 set_i = isl_set_from_basic_set(bset);
1259 set_i = isl_set_intersect(set_i, isl_set_copy(pwaff->p[i].set));
1260 set = isl_set_union_disjoint(set, set_i);
1263 isl_pw_aff_free(pwaff);
1268 /* Return a set containing those elements in the domain
1269 * of pwaff where it is zero.
1271 __isl_give isl_set *isl_pw_aff_zero_set(__isl_take isl_pw_aff *pwaff)
1279 set = isl_set_empty(isl_pw_aff_get_dim(pwaff));
1281 for (i = 0; i < pwaff->n; ++i) {
1282 isl_basic_set *bset;
1285 bset = isl_aff_zero_basic_set(isl_aff_copy(pwaff->p[i].aff));
1286 set_i = isl_set_from_basic_set(bset);
1287 set_i = isl_set_intersect(set_i, isl_set_copy(pwaff->p[i].set));
1288 set = isl_set_union_disjoint(set, set_i);
1291 isl_pw_aff_free(pwaff);
1296 /* Return a set containing those elements in the domain
1297 * of pwaff where it is not zero.
1299 __isl_give isl_set *isl_pw_aff_non_zero_set(__isl_take isl_pw_aff *pwaff)
1301 return isl_set_complement(isl_pw_aff_zero_set(pwaff));
1304 /* Return a set containing those elements in the shared domain
1305 * of pwaff1 and pwaff2 where pwaff1 is greater than (or equal) to pwaff2.
1307 * We compute the difference on the shared domain and then construct
1308 * the set of values where this difference is non-negative.
1309 * If strict is set, we first subtract 1 from the difference.
1310 * If equal is set, we only return the elements where pwaff1 and pwaff2
1313 static __isl_give isl_set *pw_aff_gte_set(__isl_take isl_pw_aff *pwaff1,
1314 __isl_take isl_pw_aff *pwaff2, int strict, int equal)
1316 isl_set *set1, *set2;
1318 set1 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff1));
1319 set2 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff2));
1320 set1 = isl_set_intersect(set1, set2);
1321 pwaff1 = isl_pw_aff_intersect_domain(pwaff1, isl_set_copy(set1));
1322 pwaff2 = isl_pw_aff_intersect_domain(pwaff2, isl_set_copy(set1));
1323 pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_neg(pwaff2));
1326 isl_dim *dim = isl_set_get_dim(set1);
1328 aff = isl_aff_zero(isl_local_space_from_dim(dim));
1329 aff = isl_aff_add_constant_si(aff, -1);
1330 pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_alloc(set1, aff));
1335 return isl_pw_aff_zero_set(pwaff1);
1336 return isl_pw_aff_nonneg_set(pwaff1);
1339 /* Return a set containing those elements in the shared domain
1340 * of pwaff1 and pwaff2 where pwaff1 is equal to pwaff2.
1342 static __isl_give isl_set *pw_aff_eq_set(__isl_take isl_pw_aff *pwaff1,
1343 __isl_take isl_pw_aff *pwaff2)
1345 return pw_aff_gte_set(pwaff1, pwaff2, 0, 1);
1348 __isl_give isl_set *isl_pw_aff_eq_set(__isl_take isl_pw_aff *pwaff1,
1349 __isl_take isl_pw_aff *pwaff2)
1351 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_eq_set);
1354 /* Return a set containing those elements in the shared domain
1355 * of pwaff1 and pwaff2 where pwaff1 is greater than or equal to pwaff2.
1357 static __isl_give isl_set *pw_aff_ge_set(__isl_take isl_pw_aff *pwaff1,
1358 __isl_take isl_pw_aff *pwaff2)
1360 return pw_aff_gte_set(pwaff1, pwaff2, 0, 0);
1363 __isl_give isl_set *isl_pw_aff_ge_set(__isl_take isl_pw_aff *pwaff1,
1364 __isl_take isl_pw_aff *pwaff2)
1366 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_ge_set);
1369 /* Return a set containing those elements in the shared domain
1370 * of pwaff1 and pwaff2 where pwaff1 is strictly greater than pwaff2.
1372 static __isl_give isl_set *pw_aff_gt_set(__isl_take isl_pw_aff *pwaff1,
1373 __isl_take isl_pw_aff *pwaff2)
1375 return pw_aff_gte_set(pwaff1, pwaff2, 1, 0);
1378 __isl_give isl_set *isl_pw_aff_gt_set(__isl_take isl_pw_aff *pwaff1,
1379 __isl_take isl_pw_aff *pwaff2)
1381 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_gt_set);
1384 __isl_give isl_set *isl_pw_aff_le_set(__isl_take isl_pw_aff *pwaff1,
1385 __isl_take isl_pw_aff *pwaff2)
1387 return isl_pw_aff_ge_set(pwaff2, pwaff1);
1390 __isl_give isl_set *isl_pw_aff_lt_set(__isl_take isl_pw_aff *pwaff1,
1391 __isl_take isl_pw_aff *pwaff2)
1393 return isl_pw_aff_gt_set(pwaff2, pwaff1);
1396 /* Return a set containing those elements in the shared domain
1397 * of the elements of list1 and list2 where each element in list1
1398 * has the relation specified by "fn" with each element in list2.
1400 static __isl_give isl_set *pw_aff_list_set(__isl_take isl_pw_aff_list *list1,
1401 __isl_take isl_pw_aff_list *list2,
1402 __isl_give isl_set *(*fn)(__isl_take isl_pw_aff *pwaff1,
1403 __isl_take isl_pw_aff *pwaff2))
1409 if (!list1 || !list2)
1412 ctx = isl_pw_aff_list_get_ctx(list1);
1413 if (list1->n < 1 || list2->n < 1)
1414 isl_die(ctx, isl_error_invalid,
1415 "list should contain at least one element", goto error);
1417 set = isl_set_universe(isl_pw_aff_get_dim(list1->p[0]));
1418 for (i = 0; i < list1->n; ++i)
1419 for (j = 0; j < list2->n; ++j) {
1422 set_ij = fn(isl_pw_aff_copy(list1->p[i]),
1423 isl_pw_aff_copy(list2->p[j]));
1424 set = isl_set_intersect(set, set_ij);
1427 isl_pw_aff_list_free(list1);
1428 isl_pw_aff_list_free(list2);
1431 isl_pw_aff_list_free(list1);
1432 isl_pw_aff_list_free(list2);
1436 /* Return a set containing those elements in the shared domain
1437 * of the elements of list1 and list2 where each element in list1
1438 * is equal to each element in list2.
1440 __isl_give isl_set *isl_pw_aff_list_eq_set(__isl_take isl_pw_aff_list *list1,
1441 __isl_take isl_pw_aff_list *list2)
1443 return pw_aff_list_set(list1, list2, &isl_pw_aff_eq_set);
1446 __isl_give isl_set *isl_pw_aff_list_ne_set(__isl_take isl_pw_aff_list *list1,
1447 __isl_take isl_pw_aff_list *list2)
1449 return pw_aff_list_set(list1, list2, &isl_pw_aff_ne_set);
1452 /* Return a set containing those elements in the shared domain
1453 * of the elements of list1 and list2 where each element in list1
1454 * is less than or equal to each element in list2.
1456 __isl_give isl_set *isl_pw_aff_list_le_set(__isl_take isl_pw_aff_list *list1,
1457 __isl_take isl_pw_aff_list *list2)
1459 return pw_aff_list_set(list1, list2, &isl_pw_aff_le_set);
1462 __isl_give isl_set *isl_pw_aff_list_lt_set(__isl_take isl_pw_aff_list *list1,
1463 __isl_take isl_pw_aff_list *list2)
1465 return pw_aff_list_set(list1, list2, &isl_pw_aff_lt_set);
1468 __isl_give isl_set *isl_pw_aff_list_ge_set(__isl_take isl_pw_aff_list *list1,
1469 __isl_take isl_pw_aff_list *list2)
1471 return pw_aff_list_set(list1, list2, &isl_pw_aff_ge_set);
1474 __isl_give isl_set *isl_pw_aff_list_gt_set(__isl_take isl_pw_aff_list *list1,
1475 __isl_take isl_pw_aff_list *list2)
1477 return pw_aff_list_set(list1, list2, &isl_pw_aff_gt_set);
1481 /* Return a set containing those elements in the shared domain
1482 * of pwaff1 and pwaff2 where pwaff1 is not equal to pwaff2.
1484 static __isl_give isl_set *pw_aff_ne_set(__isl_take isl_pw_aff *pwaff1,
1485 __isl_take isl_pw_aff *pwaff2)
1487 isl_set *set_lt, *set_gt;
1489 set_lt = isl_pw_aff_lt_set(isl_pw_aff_copy(pwaff1),
1490 isl_pw_aff_copy(pwaff2));
1491 set_gt = isl_pw_aff_gt_set(pwaff1, pwaff2);
1492 return isl_set_union_disjoint(set_lt, set_gt);
1495 __isl_give isl_set *isl_pw_aff_ne_set(__isl_take isl_pw_aff *pwaff1,
1496 __isl_take isl_pw_aff *pwaff2)
1498 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_ne_set);
1501 __isl_give isl_pw_aff *isl_pw_aff_scale_down(__isl_take isl_pw_aff *pwaff,
1506 if (isl_int_is_one(v))
1508 if (!isl_int_is_pos(v))
1509 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
1510 "factor needs to be positive",
1511 return isl_pw_aff_free(pwaff));
1512 pwaff = isl_pw_aff_cow(pwaff);
1518 for (i = 0; i < pwaff->n; ++i) {
1519 pwaff->p[i].aff = isl_aff_scale_down(pwaff->p[i].aff, v);
1520 if (!pwaff->p[i].aff)
1521 return isl_pw_aff_free(pwaff);
1527 __isl_give isl_pw_aff *isl_pw_aff_floor(__isl_take isl_pw_aff *pwaff)
1531 pwaff = isl_pw_aff_cow(pwaff);
1537 for (i = 0; i < pwaff->n; ++i) {
1538 pwaff->p[i].aff = isl_aff_floor(pwaff->p[i].aff);
1539 if (!pwaff->p[i].aff)
1540 return isl_pw_aff_free(pwaff);
1546 __isl_give isl_pw_aff *isl_pw_aff_ceil(__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_ceil(pwaff->p[i].aff);
1558 if (!pwaff->p[i].aff)
1559 return isl_pw_aff_free(pwaff);
1565 /* Return an affine expression that is equal to pwaff_true for elements
1566 * in "cond" and to pwaff_false for elements not in "cond".
1567 * That is, return cond ? pwaff_true : pwaff_false;
1569 __isl_give isl_pw_aff *isl_pw_aff_cond(__isl_take isl_set *cond,
1570 __isl_take isl_pw_aff *pwaff_true, __isl_take isl_pw_aff *pwaff_false)
1574 comp = isl_set_complement(isl_set_copy(cond));
1575 pwaff_true = isl_pw_aff_intersect_domain(pwaff_true, cond);
1576 pwaff_false = isl_pw_aff_intersect_domain(pwaff_false, comp);
1578 return isl_pw_aff_add_disjoint(pwaff_true, pwaff_false);
1581 int isl_aff_is_cst(__isl_keep isl_aff *aff)
1586 return isl_seq_first_non_zero(aff->v->el + 2, aff->v->size - 2) == -1;
1589 /* Check whether pwaff is a piecewise constant.
1591 int isl_pw_aff_is_cst(__isl_keep isl_pw_aff *pwaff)
1598 for (i = 0; i < pwaff->n; ++i) {
1599 int is_cst = isl_aff_is_cst(pwaff->p[i].aff);
1600 if (is_cst < 0 || !is_cst)
1607 __isl_give isl_aff *isl_aff_mul(__isl_take isl_aff *aff1,
1608 __isl_take isl_aff *aff2)
1610 if (!isl_aff_is_cst(aff2) && isl_aff_is_cst(aff1))
1611 return isl_aff_mul(aff2, aff1);
1613 if (!isl_aff_is_cst(aff2))
1614 isl_die(isl_aff_get_ctx(aff1), isl_error_invalid,
1615 "at least one affine expression should be constant",
1618 aff1 = isl_aff_cow(aff1);
1622 aff1 = isl_aff_scale(aff1, aff2->v->el[1]);
1623 aff1 = isl_aff_scale_down(aff1, aff2->v->el[0]);
1633 static __isl_give isl_pw_aff *pw_aff_mul(__isl_take isl_pw_aff *pwaff1,
1634 __isl_take isl_pw_aff *pwaff2)
1639 if (!pwaff1 || !pwaff2)
1642 n = pwaff1->n * pwaff2->n;
1643 res = isl_pw_aff_alloc_(isl_dim_copy(pwaff1->dim), n);
1645 for (i = 0; i < pwaff1->n; ++i) {
1646 for (j = 0; j < pwaff2->n; ++j) {
1649 common = isl_set_intersect(
1650 isl_set_copy(pwaff1->p[i].set),
1651 isl_set_copy(pwaff2->p[j].set));
1652 if (isl_set_plain_is_empty(common)) {
1653 isl_set_free(common);
1657 prod = isl_aff_mul(isl_aff_copy(pwaff1->p[i].aff),
1658 isl_aff_copy(pwaff2->p[j].aff));
1660 res = isl_pw_aff_add_piece(res, common, prod);
1664 isl_pw_aff_free(pwaff1);
1665 isl_pw_aff_free(pwaff2);
1668 isl_pw_aff_free(pwaff1);
1669 isl_pw_aff_free(pwaff2);
1673 __isl_give isl_pw_aff *isl_pw_aff_mul(__isl_take isl_pw_aff *pwaff1,
1674 __isl_take isl_pw_aff *pwaff2)
1676 return align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_mul);
1679 static __isl_give isl_pw_aff *pw_aff_min(__isl_take isl_pw_aff *pwaff1,
1680 __isl_take isl_pw_aff *pwaff2)
1684 le = isl_pw_aff_le_set(isl_pw_aff_copy(pwaff1),
1685 isl_pw_aff_copy(pwaff2));
1686 return isl_pw_aff_cond(le, pwaff1, pwaff2);
1689 __isl_give isl_pw_aff *isl_pw_aff_min(__isl_take isl_pw_aff *pwaff1,
1690 __isl_take isl_pw_aff *pwaff2)
1692 return align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_min);
1695 static __isl_give isl_pw_aff *pw_aff_max(__isl_take isl_pw_aff *pwaff1,
1696 __isl_take isl_pw_aff *pwaff2)
1700 le = isl_pw_aff_ge_set(isl_pw_aff_copy(pwaff1),
1701 isl_pw_aff_copy(pwaff2));
1702 return isl_pw_aff_cond(le, pwaff1, pwaff2);
1705 __isl_give isl_pw_aff *isl_pw_aff_max(__isl_take isl_pw_aff *pwaff1,
1706 __isl_take isl_pw_aff *pwaff2)
1708 return align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_max);
1711 static __isl_give isl_pw_aff *pw_aff_list_reduce(
1712 __isl_take isl_pw_aff_list *list,
1713 __isl_give isl_pw_aff *(*fn)(__isl_take isl_pw_aff *pwaff1,
1714 __isl_take isl_pw_aff *pwaff2))
1723 ctx = isl_pw_aff_list_get_ctx(list);
1725 isl_die(ctx, isl_error_invalid,
1726 "list should contain at least one element",
1727 return isl_pw_aff_list_free(list));
1729 res = isl_pw_aff_copy(list->p[0]);
1730 for (i = 1; i < list->n; ++i)
1731 res = fn(res, isl_pw_aff_copy(list->p[i]));
1733 isl_pw_aff_list_free(list);
1737 /* Return an isl_pw_aff that maps each element in the intersection of the
1738 * domains of the elements of list to the minimal corresponding affine
1741 __isl_give isl_pw_aff *isl_pw_aff_list_min(__isl_take isl_pw_aff_list *list)
1743 return pw_aff_list_reduce(list, &isl_pw_aff_min);
1746 /* Return an isl_pw_aff that maps each element in the intersection of the
1747 * domains of the elements of list to the maximal corresponding affine
1750 __isl_give isl_pw_aff *isl_pw_aff_list_max(__isl_take isl_pw_aff_list *list)
1752 return pw_aff_list_reduce(list, &isl_pw_aff_max);