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_space_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>
22 #include <isl_config.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",
60 if (!isl_local_space_is_set(ls))
61 isl_die(ctx, isl_error_invalid,
62 "domain of affine expression should be a set",
65 total = isl_local_space_dim(ls, isl_dim_all);
66 v = isl_vec_alloc(ctx, 1 + 1 + total);
67 return isl_aff_alloc_vec(ls, v);
69 isl_local_space_free(ls);
73 __isl_give isl_aff *isl_aff_zero_on_domain(__isl_take isl_local_space *ls)
77 aff = isl_aff_alloc(ls);
81 isl_int_set_si(aff->v->el[0], 1);
82 isl_seq_clr(aff->v->el + 1, aff->v->size - 1);
87 __isl_give isl_aff *isl_aff_copy(__isl_keep isl_aff *aff)
96 __isl_give isl_aff *isl_aff_dup(__isl_keep isl_aff *aff)
101 return isl_aff_alloc_vec(isl_local_space_copy(aff->ls),
102 isl_vec_copy(aff->v));
105 __isl_give isl_aff *isl_aff_cow(__isl_take isl_aff *aff)
113 return isl_aff_dup(aff);
116 void *isl_aff_free(__isl_take isl_aff *aff)
124 isl_local_space_free(aff->ls);
125 isl_vec_free(aff->v);
132 isl_ctx *isl_aff_get_ctx(__isl_keep isl_aff *aff)
134 return aff ? isl_local_space_get_ctx(aff->ls) : NULL;
137 /* Externally, an isl_aff has a map space, but internally, the
138 * ls field corresponds to the domain of that space.
140 int isl_aff_dim(__isl_keep isl_aff *aff, enum isl_dim_type type)
144 if (type == isl_dim_out)
146 if (type == isl_dim_in)
148 return isl_local_space_dim(aff->ls, type);
151 __isl_give isl_space *isl_aff_get_domain_space(__isl_keep isl_aff *aff)
153 return aff ? isl_local_space_get_space(aff->ls) : NULL;
156 __isl_give isl_space *isl_aff_get_space(__isl_keep isl_aff *aff)
161 space = isl_local_space_get_space(aff->ls);
162 space = isl_space_from_domain(space);
163 space = isl_space_add_dims(space, isl_dim_out, 1);
167 __isl_give isl_local_space *isl_aff_get_domain_local_space(
168 __isl_keep isl_aff *aff)
170 return aff ? isl_local_space_copy(aff->ls) : NULL;
173 __isl_give isl_local_space *isl_aff_get_local_space(__isl_keep isl_aff *aff)
178 ls = isl_local_space_copy(aff->ls);
179 ls = isl_local_space_from_domain(ls);
180 ls = isl_local_space_add_dims(ls, isl_dim_out, 1);
184 /* Externally, an isl_aff has a map space, but internally, the
185 * ls field corresponds to the domain of that space.
187 const char *isl_aff_get_dim_name(__isl_keep isl_aff *aff,
188 enum isl_dim_type type, unsigned pos)
192 if (type == isl_dim_out)
194 if (type == isl_dim_in)
196 return isl_local_space_get_dim_name(aff->ls, type, pos);
199 __isl_give isl_aff *isl_aff_reset_domain_space(__isl_take isl_aff *aff,
200 __isl_take isl_space *dim)
202 aff = isl_aff_cow(aff);
206 aff->ls = isl_local_space_reset_space(aff->ls, dim);
208 return isl_aff_free(aff);
217 /* Reset the space of "aff". This function is called from isl_pw_templ.c
218 * and doesn't know if the space of an element object is represented
219 * directly or through its domain. It therefore passes along both.
221 __isl_give isl_aff *isl_aff_reset_space_and_domain(__isl_take isl_aff *aff,
222 __isl_take isl_space *space, __isl_take isl_space *domain)
224 isl_space_free(space);
225 return isl_aff_reset_domain_space(aff, domain);
228 /* Reorder the coefficients of the affine expression based
229 * on the given reodering.
230 * The reordering r is assumed to have been extended with the local
233 static __isl_give isl_vec *vec_reorder(__isl_take isl_vec *vec,
234 __isl_take isl_reordering *r, int n_div)
242 res = isl_vec_alloc(vec->ctx,
243 2 + isl_space_dim(r->dim, isl_dim_all) + n_div);
244 isl_seq_cpy(res->el, vec->el, 2);
245 isl_seq_clr(res->el + 2, res->size - 2);
246 for (i = 0; i < r->len; ++i)
247 isl_int_set(res->el[2 + r->pos[i]], vec->el[2 + i]);
249 isl_reordering_free(r);
254 isl_reordering_free(r);
258 /* Reorder the dimensions of the domain of "aff" according
259 * to the given reordering.
261 __isl_give isl_aff *isl_aff_realign_domain(__isl_take isl_aff *aff,
262 __isl_take isl_reordering *r)
264 aff = isl_aff_cow(aff);
268 r = isl_reordering_extend(r, aff->ls->div->n_row);
269 aff->v = vec_reorder(aff->v, isl_reordering_copy(r),
270 aff->ls->div->n_row);
271 aff->ls = isl_local_space_realign(aff->ls, r);
273 if (!aff->v || !aff->ls)
274 return isl_aff_free(aff);
279 isl_reordering_free(r);
283 __isl_give isl_aff *isl_aff_align_params(__isl_take isl_aff *aff,
284 __isl_take isl_space *model)
289 if (!isl_space_match(aff->ls->dim, isl_dim_param,
290 model, isl_dim_param)) {
293 model = isl_space_drop_dims(model, isl_dim_in,
294 0, isl_space_dim(model, isl_dim_in));
295 model = isl_space_drop_dims(model, isl_dim_out,
296 0, isl_space_dim(model, isl_dim_out));
297 exp = isl_parameter_alignment_reordering(aff->ls->dim, model);
298 exp = isl_reordering_extend_space(exp,
299 isl_aff_get_domain_space(aff));
300 aff = isl_aff_realign_domain(aff, exp);
303 isl_space_free(model);
306 isl_space_free(model);
311 int isl_aff_plain_is_zero(__isl_keep isl_aff *aff)
316 return isl_seq_first_non_zero(aff->v->el + 1, aff->v->size - 1) < 0;
319 int isl_aff_plain_is_equal(__isl_keep isl_aff *aff1, __isl_keep isl_aff *aff2)
326 equal = isl_local_space_is_equal(aff1->ls, aff2->ls);
327 if (equal < 0 || !equal)
330 return isl_vec_is_equal(aff1->v, aff2->v);
333 int isl_aff_get_denominator(__isl_keep isl_aff *aff, isl_int *v)
337 isl_int_set(*v, aff->v->el[0]);
341 int isl_aff_get_constant(__isl_keep isl_aff *aff, isl_int *v)
345 isl_int_set(*v, aff->v->el[1]);
349 int isl_aff_get_coefficient(__isl_keep isl_aff *aff,
350 enum isl_dim_type type, int pos, isl_int *v)
355 if (type == isl_dim_out)
356 isl_die(aff->v->ctx, isl_error_invalid,
357 "output/set dimension does not have a coefficient",
359 if (type == isl_dim_in)
362 if (pos >= isl_local_space_dim(aff->ls, type))
363 isl_die(aff->v->ctx, isl_error_invalid,
364 "position out of bounds", return -1);
366 pos += isl_local_space_offset(aff->ls, type);
367 isl_int_set(*v, aff->v->el[1 + pos]);
372 __isl_give isl_aff *isl_aff_set_denominator(__isl_take isl_aff *aff, isl_int v)
374 aff = isl_aff_cow(aff);
378 aff->v = isl_vec_cow(aff->v);
380 return isl_aff_free(aff);
382 isl_int_set(aff->v->el[0], v);
387 __isl_give isl_aff *isl_aff_set_constant(__isl_take isl_aff *aff, isl_int v)
389 aff = isl_aff_cow(aff);
393 aff->v = isl_vec_cow(aff->v);
395 return isl_aff_free(aff);
397 isl_int_set(aff->v->el[1], v);
402 __isl_give isl_aff *isl_aff_add_constant(__isl_take isl_aff *aff, isl_int v)
404 if (isl_int_is_zero(v))
407 aff = isl_aff_cow(aff);
411 aff->v = isl_vec_cow(aff->v);
413 return isl_aff_free(aff);
415 isl_int_addmul(aff->v->el[1], aff->v->el[0], v);
420 __isl_give isl_aff *isl_aff_add_constant_si(__isl_take isl_aff *aff, int v)
425 isl_int_set_si(t, v);
426 aff = isl_aff_add_constant(aff, t);
432 __isl_give isl_aff *isl_aff_set_constant_si(__isl_take isl_aff *aff, int v)
434 aff = isl_aff_cow(aff);
438 aff->v = isl_vec_cow(aff->v);
440 return isl_aff_free(aff);
442 isl_int_set_si(aff->v->el[1], v);
447 __isl_give isl_aff *isl_aff_set_coefficient(__isl_take isl_aff *aff,
448 enum isl_dim_type type, int pos, isl_int v)
453 if (type == isl_dim_out)
454 isl_die(aff->v->ctx, isl_error_invalid,
455 "output/set dimension does not have a coefficient",
456 return isl_aff_free(aff));
457 if (type == isl_dim_in)
460 if (pos >= isl_local_space_dim(aff->ls, type))
461 isl_die(aff->v->ctx, isl_error_invalid,
462 "position out of bounds", return isl_aff_free(aff));
464 aff = isl_aff_cow(aff);
468 aff->v = isl_vec_cow(aff->v);
470 return isl_aff_free(aff);
472 pos += isl_local_space_offset(aff->ls, type);
473 isl_int_set(aff->v->el[1 + pos], v);
478 __isl_give isl_aff *isl_aff_set_coefficient_si(__isl_take isl_aff *aff,
479 enum isl_dim_type type, int pos, int v)
484 if (type == isl_dim_out)
485 isl_die(aff->v->ctx, isl_error_invalid,
486 "output/set dimension does not have a coefficient",
487 return isl_aff_free(aff));
488 if (type == isl_dim_in)
491 if (pos >= isl_local_space_dim(aff->ls, type))
492 isl_die(aff->v->ctx, isl_error_invalid,
493 "position out of bounds", return isl_aff_free(aff));
495 aff = isl_aff_cow(aff);
499 aff->v = isl_vec_cow(aff->v);
501 return isl_aff_free(aff);
503 pos += isl_local_space_offset(aff->ls, type);
504 isl_int_set_si(aff->v->el[1 + pos], v);
509 __isl_give isl_aff *isl_aff_add_coefficient(__isl_take isl_aff *aff,
510 enum isl_dim_type type, int pos, isl_int v)
515 if (type == isl_dim_out)
516 isl_die(aff->v->ctx, isl_error_invalid,
517 "output/set dimension does not have a coefficient",
518 return isl_aff_free(aff));
519 if (type == isl_dim_in)
522 if (pos >= isl_local_space_dim(aff->ls, type))
523 isl_die(aff->v->ctx, isl_error_invalid,
524 "position out of bounds", return isl_aff_free(aff));
526 aff = isl_aff_cow(aff);
530 aff->v = isl_vec_cow(aff->v);
532 return isl_aff_free(aff);
534 pos += isl_local_space_offset(aff->ls, type);
535 isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v);
540 __isl_give isl_aff *isl_aff_add_coefficient_si(__isl_take isl_aff *aff,
541 enum isl_dim_type type, int pos, int v)
546 isl_int_set_si(t, v);
547 aff = isl_aff_add_coefficient(aff, type, pos, t);
553 __isl_give isl_aff *isl_aff_get_div(__isl_keep isl_aff *aff, int pos)
558 return isl_local_space_get_div(aff->ls, pos);
561 __isl_give isl_aff *isl_aff_neg(__isl_take isl_aff *aff)
563 aff = isl_aff_cow(aff);
566 aff->v = isl_vec_cow(aff->v);
568 return isl_aff_free(aff);
570 isl_seq_neg(aff->v->el + 1, aff->v->el + 1, aff->v->size - 1);
575 __isl_give isl_aff *isl_aff_normalize(__isl_take isl_aff *aff)
579 aff->v = isl_vec_normalize(aff->v);
581 return isl_aff_free(aff);
585 /* Given f, return floor(f).
586 * If f is an integer expression, then just return f.
587 * Otherwise, if f = g/m, write g = q m + r,
588 * create a new div d = [r/m] and return the expression q + d.
589 * The coefficients in r are taken to lie between -m/2 and m/2.
591 __isl_give isl_aff *isl_aff_floor(__isl_take isl_aff *aff)
601 if (isl_int_is_one(aff->v->el[0]))
604 aff = isl_aff_cow(aff);
608 aff->v = isl_vec_cow(aff->v);
609 div = isl_vec_copy(aff->v);
610 div = isl_vec_cow(div);
612 return isl_aff_free(aff);
614 ctx = isl_aff_get_ctx(aff);
615 isl_int_fdiv_q(aff->v->el[0], aff->v->el[0], ctx->two);
616 for (i = 1; i < aff->v->size; ++i) {
617 isl_int_fdiv_r(div->el[i], div->el[i], div->el[0]);
618 isl_int_fdiv_q(aff->v->el[i], aff->v->el[i], div->el[0]);
619 if (isl_int_gt(div->el[i], aff->v->el[0])) {
620 isl_int_sub(div->el[i], div->el[i], div->el[0]);
621 isl_int_add_ui(aff->v->el[i], aff->v->el[i], 1);
625 aff->ls = isl_local_space_add_div(aff->ls, div);
627 return isl_aff_free(aff);
630 aff->v = isl_vec_extend(aff->v, size + 1);
632 return isl_aff_free(aff);
633 isl_int_set_si(aff->v->el[0], 1);
634 isl_int_set_si(aff->v->el[size], 1);
641 * aff mod m = aff - m * floor(aff/m)
643 __isl_give isl_aff *isl_aff_mod(__isl_take isl_aff *aff, isl_int m)
647 res = isl_aff_copy(aff);
648 aff = isl_aff_scale_down(aff, m);
649 aff = isl_aff_floor(aff);
650 aff = isl_aff_scale(aff, m);
651 res = isl_aff_sub(res, aff);
658 * pwaff mod m = pwaff - m * floor(pwaff/m)
660 __isl_give isl_pw_aff *isl_pw_aff_mod(__isl_take isl_pw_aff *pwaff, isl_int m)
664 res = isl_pw_aff_copy(pwaff);
665 pwaff = isl_pw_aff_scale_down(pwaff, m);
666 pwaff = isl_pw_aff_floor(pwaff);
667 pwaff = isl_pw_aff_scale(pwaff, m);
668 res = isl_pw_aff_sub(res, pwaff);
673 /* Given f, return ceil(f).
674 * If f is an integer expression, then just return f.
675 * Otherwise, create a new div d = [-f] and return the expression -d.
677 __isl_give isl_aff *isl_aff_ceil(__isl_take isl_aff *aff)
682 if (isl_int_is_one(aff->v->el[0]))
685 aff = isl_aff_neg(aff);
686 aff = isl_aff_floor(aff);
687 aff = isl_aff_neg(aff);
692 /* Apply the expansion computed by isl_merge_divs.
693 * The expansion itself is given by "exp" while the resulting
694 * list of divs is given by "div".
696 __isl_give isl_aff *isl_aff_expand_divs( __isl_take isl_aff *aff,
697 __isl_take isl_mat *div, int *exp)
704 aff = isl_aff_cow(aff);
708 old_n_div = isl_local_space_dim(aff->ls, isl_dim_div);
709 new_n_div = isl_mat_rows(div);
710 if (new_n_div < old_n_div)
711 isl_die(isl_mat_get_ctx(div), isl_error_invalid,
712 "not an expansion", goto error);
714 aff->v = isl_vec_extend(aff->v, aff->v->size + new_n_div - old_n_div);
718 offset = 1 + isl_local_space_offset(aff->ls, isl_dim_div);
720 for (i = new_n_div - 1; i >= 0; --i) {
721 if (j >= 0 && exp[j] == i) {
723 isl_int_swap(aff->v->el[offset + i],
724 aff->v->el[offset + j]);
727 isl_int_set_si(aff->v->el[offset + i], 0);
730 aff->ls = isl_local_space_replace_divs(aff->ls, isl_mat_copy(div));
741 /* Add two affine expressions that live in the same local space.
743 static __isl_give isl_aff *add_expanded(__isl_take isl_aff *aff1,
744 __isl_take isl_aff *aff2)
748 aff1 = isl_aff_cow(aff1);
752 aff1->v = isl_vec_cow(aff1->v);
758 isl_int_gcd(gcd, aff1->v->el[0], aff2->v->el[0]);
759 isl_int_divexact(f, aff2->v->el[0], gcd);
760 isl_seq_scale(aff1->v->el + 1, aff1->v->el + 1, f, aff1->v->size - 1);
761 isl_int_divexact(f, aff1->v->el[0], gcd);
762 isl_seq_addmul(aff1->v->el + 1, f, aff2->v->el + 1, aff1->v->size - 1);
763 isl_int_divexact(f, aff2->v->el[0], gcd);
764 isl_int_mul(aff1->v->el[0], aff1->v->el[0], f);
776 __isl_give isl_aff *isl_aff_add(__isl_take isl_aff *aff1,
777 __isl_take isl_aff *aff2)
787 ctx = isl_aff_get_ctx(aff1);
788 if (!isl_space_is_equal(aff1->ls->dim, aff2->ls->dim))
789 isl_die(ctx, isl_error_invalid,
790 "spaces don't match", goto error);
792 if (aff1->ls->div->n_row == 0 && aff2->ls->div->n_row == 0)
793 return add_expanded(aff1, aff2);
795 exp1 = isl_alloc_array(ctx, int, aff1->ls->div->n_row);
796 exp2 = isl_alloc_array(ctx, int, aff2->ls->div->n_row);
800 div = isl_merge_divs(aff1->ls->div, aff2->ls->div, exp1, exp2);
801 aff1 = isl_aff_expand_divs(aff1, isl_mat_copy(div), exp1);
802 aff2 = isl_aff_expand_divs(aff2, div, exp2);
806 return add_expanded(aff1, aff2);
815 __isl_give isl_aff *isl_aff_sub(__isl_take isl_aff *aff1,
816 __isl_take isl_aff *aff2)
818 return isl_aff_add(aff1, isl_aff_neg(aff2));
821 __isl_give isl_aff *isl_aff_scale(__isl_take isl_aff *aff, isl_int f)
825 if (isl_int_is_one(f))
828 aff = isl_aff_cow(aff);
831 aff->v = isl_vec_cow(aff->v);
833 return isl_aff_free(aff);
836 isl_int_gcd(gcd, aff->v->el[0], f);
837 isl_int_divexact(aff->v->el[0], aff->v->el[0], gcd);
838 isl_int_divexact(gcd, f, gcd);
839 isl_seq_scale(aff->v->el + 1, aff->v->el + 1, gcd, aff->v->size - 1);
845 __isl_give isl_aff *isl_aff_scale_down(__isl_take isl_aff *aff, isl_int f)
849 if (isl_int_is_one(f))
852 aff = isl_aff_cow(aff);
855 aff->v = isl_vec_cow(aff->v);
857 return isl_aff_free(aff);
860 isl_seq_gcd(aff->v->el + 1, aff->v->size - 1, &gcd);
861 isl_int_gcd(gcd, gcd, f);
862 isl_seq_scale_down(aff->v->el + 1, aff->v->el + 1, gcd, aff->v->size - 1);
863 isl_int_divexact(gcd, f, gcd);
864 isl_int_mul(aff->v->el[0], aff->v->el[0], gcd);
870 __isl_give isl_aff *isl_aff_scale_down_ui(__isl_take isl_aff *aff, unsigned f)
878 isl_int_set_ui(v, f);
879 aff = isl_aff_scale_down(aff, v);
885 __isl_give isl_aff *isl_aff_set_dim_name(__isl_take isl_aff *aff,
886 enum isl_dim_type type, unsigned pos, const char *s)
888 aff = isl_aff_cow(aff);
891 if (type == isl_dim_out)
892 isl_die(aff->v->ctx, isl_error_invalid,
893 "cannot set name of output/set dimension",
894 return isl_aff_free(aff));
895 if (type == isl_dim_in)
897 aff->ls = isl_local_space_set_dim_name(aff->ls, type, pos, s);
899 return isl_aff_free(aff);
904 __isl_give isl_aff *isl_aff_set_dim_id(__isl_take isl_aff *aff,
905 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
907 aff = isl_aff_cow(aff);
909 return isl_id_free(id);
910 if (type == isl_dim_out)
911 isl_die(aff->v->ctx, isl_error_invalid,
912 "cannot set name of output/set dimension",
914 if (type == isl_dim_in)
916 aff->ls = isl_local_space_set_dim_id(aff->ls, type, pos, id);
918 return isl_aff_free(aff);
927 /* Exploit the equalities in "eq" to simplify the affine expression
928 * and the expressions of the integer divisions in the local space.
929 * The integer divisions in this local space are assumed to appear
930 * as regular dimensions in "eq".
932 static __isl_give isl_aff *isl_aff_substitute_equalities_lifted(
933 __isl_take isl_aff *aff, __isl_take isl_basic_set *eq)
942 isl_basic_set_free(eq);
946 aff = isl_aff_cow(aff);
950 aff->ls = isl_local_space_substitute_equalities(aff->ls,
951 isl_basic_set_copy(eq));
955 total = 1 + isl_space_dim(eq->dim, isl_dim_all);
957 for (i = 0; i < eq->n_eq; ++i) {
958 j = isl_seq_last_non_zero(eq->eq[i], total + n_div);
959 if (j < 0 || j == 0 || j >= total)
962 isl_seq_elim(aff->v->el + 1, eq->eq[i], j, total,
966 isl_basic_set_free(eq);
967 aff = isl_aff_normalize(aff);
970 isl_basic_set_free(eq);
975 /* Exploit the equalities in "eq" to simplify the affine expression
976 * and the expressions of the integer divisions in the local space.
978 static __isl_give isl_aff *isl_aff_substitute_equalities(
979 __isl_take isl_aff *aff, __isl_take isl_basic_set *eq)
985 n_div = isl_local_space_dim(aff->ls, isl_dim_div);
987 eq = isl_basic_set_add(eq, isl_dim_set, n_div);
988 return isl_aff_substitute_equalities_lifted(aff, eq);
990 isl_basic_set_free(eq);
995 /* Look for equalities among the variables shared by context and aff
996 * and the integer divisions of aff, if any.
997 * The equalities are then used to eliminate coefficients and/or integer
998 * divisions from aff.
1000 __isl_give isl_aff *isl_aff_gist(__isl_take isl_aff *aff,
1001 __isl_take isl_set *context)
1003 isl_basic_set *hull;
1008 n_div = isl_local_space_dim(aff->ls, isl_dim_div);
1010 isl_basic_set *bset;
1011 isl_local_space *ls;
1012 context = isl_set_add_dims(context, isl_dim_set, n_div);
1013 ls = isl_aff_get_domain_local_space(aff);
1014 bset = isl_basic_set_from_local_space(ls);
1015 bset = isl_basic_set_lift(bset);
1016 bset = isl_basic_set_flatten(bset);
1017 context = isl_set_intersect(context,
1018 isl_set_from_basic_set(bset));
1021 hull = isl_set_affine_hull(context);
1022 return isl_aff_substitute_equalities_lifted(aff, hull);
1025 isl_set_free(context);
1029 __isl_give isl_aff *isl_aff_gist_params(__isl_take isl_aff *aff,
1030 __isl_take isl_set *context)
1032 isl_set *dom_context = isl_set_universe(isl_aff_get_domain_space(aff));
1033 dom_context = isl_set_intersect_params(dom_context, context);
1034 return isl_aff_gist(aff, dom_context);
1037 /* Return a basic set containing those elements in the space
1038 * of aff where it is non-negative.
1040 __isl_give isl_basic_set *isl_aff_nonneg_basic_set(__isl_take isl_aff *aff)
1042 isl_constraint *ineq;
1044 ineq = isl_inequality_from_aff(aff);
1046 return isl_basic_set_from_constraint(ineq);
1049 /* Return a basic set containing those elements in the space
1050 * of aff where it is zero.
1052 __isl_give isl_basic_set *isl_aff_zero_basic_set(__isl_take isl_aff *aff)
1054 isl_constraint *ineq;
1056 ineq = isl_equality_from_aff(aff);
1058 return isl_basic_set_from_constraint(ineq);
1061 /* Return a basic set containing those elements in the shared space
1062 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
1064 __isl_give isl_basic_set *isl_aff_ge_basic_set(__isl_take isl_aff *aff1,
1065 __isl_take isl_aff *aff2)
1067 aff1 = isl_aff_sub(aff1, aff2);
1069 return isl_aff_nonneg_basic_set(aff1);
1072 /* Return a basic set containing those elements in the shared space
1073 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
1075 __isl_give isl_basic_set *isl_aff_le_basic_set(__isl_take isl_aff *aff1,
1076 __isl_take isl_aff *aff2)
1078 return isl_aff_ge_basic_set(aff2, aff1);
1081 __isl_give isl_aff *isl_aff_add_on_domain(__isl_keep isl_set *dom,
1082 __isl_take isl_aff *aff1, __isl_take isl_aff *aff2)
1084 aff1 = isl_aff_add(aff1, aff2);
1085 aff1 = isl_aff_gist(aff1, isl_set_copy(dom));
1089 int isl_aff_is_empty(__isl_keep isl_aff *aff)
1097 /* Check whether the given affine expression has non-zero coefficient
1098 * for any dimension in the given range or if any of these dimensions
1099 * appear with non-zero coefficients in any of the integer divisions
1100 * involved in the affine expression.
1102 int isl_aff_involves_dims(__isl_keep isl_aff *aff,
1103 enum isl_dim_type type, unsigned first, unsigned n)
1115 ctx = isl_aff_get_ctx(aff);
1116 if (first + n > isl_aff_dim(aff, type))
1117 isl_die(ctx, isl_error_invalid,
1118 "range out of bounds", return -1);
1120 active = isl_local_space_get_active(aff->ls, aff->v->el + 2);
1124 first += isl_local_space_offset(aff->ls, type) - 1;
1125 for (i = 0; i < n; ++i)
1126 if (active[first + i]) {
1139 __isl_give isl_aff *isl_aff_drop_dims(__isl_take isl_aff *aff,
1140 enum isl_dim_type type, unsigned first, unsigned n)
1146 if (type == isl_dim_out)
1147 isl_die(aff->v->ctx, isl_error_invalid,
1148 "cannot drop output/set dimension",
1149 return isl_aff_free(aff));
1150 if (type == isl_dim_in)
1152 if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
1155 ctx = isl_aff_get_ctx(aff);
1156 if (first + n > isl_local_space_dim(aff->ls, type))
1157 isl_die(ctx, isl_error_invalid, "range out of bounds",
1158 return isl_aff_free(aff));
1160 aff = isl_aff_cow(aff);
1164 aff->ls = isl_local_space_drop_dims(aff->ls, type, first, n);
1166 return isl_aff_free(aff);
1168 first += 1 + isl_local_space_offset(aff->ls, type);
1169 aff->v = isl_vec_drop_els(aff->v, first, n);
1171 return isl_aff_free(aff);
1176 /* Project the domain of the affine expression onto its parameter space.
1177 * The affine expression may not involve any of the domain dimensions.
1179 __isl_give isl_aff *isl_aff_project_domain_on_params(__isl_take isl_aff *aff)
1185 n = isl_aff_dim(aff, isl_dim_in);
1186 involves = isl_aff_involves_dims(aff, isl_dim_in, 0, n);
1188 return isl_aff_free(aff);
1190 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1191 "affine expression involves some of the domain dimensions",
1192 return isl_aff_free(aff));
1193 aff = isl_aff_drop_dims(aff, isl_dim_in, 0, n);
1194 space = isl_aff_get_domain_space(aff);
1195 space = isl_space_params(space);
1196 aff = isl_aff_reset_domain_space(aff, space);
1200 __isl_give isl_aff *isl_aff_insert_dims(__isl_take isl_aff *aff,
1201 enum isl_dim_type type, unsigned first, unsigned n)
1207 if (type == isl_dim_out)
1208 isl_die(aff->v->ctx, isl_error_invalid,
1209 "cannot insert output/set dimensions",
1210 return isl_aff_free(aff));
1211 if (type == isl_dim_in)
1213 if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
1216 ctx = isl_aff_get_ctx(aff);
1217 if (first > isl_local_space_dim(aff->ls, type))
1218 isl_die(ctx, isl_error_invalid, "position out of bounds",
1219 return isl_aff_free(aff));
1221 aff = isl_aff_cow(aff);
1225 aff->ls = isl_local_space_insert_dims(aff->ls, type, first, n);
1227 return isl_aff_free(aff);
1229 first += 1 + isl_local_space_offset(aff->ls, type);
1230 aff->v = isl_vec_insert_zero_els(aff->v, first, n);
1232 return isl_aff_free(aff);
1237 __isl_give isl_aff *isl_aff_add_dims(__isl_take isl_aff *aff,
1238 enum isl_dim_type type, unsigned n)
1242 pos = isl_aff_dim(aff, type);
1244 return isl_aff_insert_dims(aff, type, pos, n);
1247 __isl_give isl_pw_aff *isl_pw_aff_add_dims(__isl_take isl_pw_aff *pwaff,
1248 enum isl_dim_type type, unsigned n)
1252 pos = isl_pw_aff_dim(pwaff, type);
1254 return isl_pw_aff_insert_dims(pwaff, type, pos, n);
1257 __isl_give isl_pw_aff *isl_pw_aff_from_aff(__isl_take isl_aff *aff)
1259 isl_set *dom = isl_set_universe(isl_aff_get_domain_space(aff));
1260 return isl_pw_aff_alloc(dom, aff);
1264 #define PW isl_pw_aff
1268 #define EL_IS_ZERO is_empty
1272 #define IS_ZERO is_empty
1275 #undef DEFAULT_IS_ZERO
1276 #define DEFAULT_IS_ZERO 0
1280 #define NO_MOVE_DIMS
1284 #include <isl_pw_templ.c>
1286 static __isl_give isl_set *align_params_pw_pw_set_and(
1287 __isl_take isl_pw_aff *pwaff1, __isl_take isl_pw_aff *pwaff2,
1288 __isl_give isl_set *(*fn)(__isl_take isl_pw_aff *pwaff1,
1289 __isl_take isl_pw_aff *pwaff2))
1291 if (!pwaff1 || !pwaff2)
1293 if (isl_space_match(pwaff1->dim, isl_dim_param,
1294 pwaff2->dim, isl_dim_param))
1295 return fn(pwaff1, pwaff2);
1296 if (!isl_space_has_named_params(pwaff1->dim) ||
1297 !isl_space_has_named_params(pwaff2->dim))
1298 isl_die(isl_pw_aff_get_ctx(pwaff1), isl_error_invalid,
1299 "unaligned unnamed parameters", goto error);
1300 pwaff1 = isl_pw_aff_align_params(pwaff1, isl_pw_aff_get_space(pwaff2));
1301 pwaff2 = isl_pw_aff_align_params(pwaff2, isl_pw_aff_get_space(pwaff1));
1302 return fn(pwaff1, pwaff2);
1304 isl_pw_aff_free(pwaff1);
1305 isl_pw_aff_free(pwaff2);
1309 /* Compute a piecewise quasi-affine expression with a domain that
1310 * is the union of those of pwaff1 and pwaff2 and such that on each
1311 * cell, the quasi-affine expression is the better (according to cmp)
1312 * of those of pwaff1 and pwaff2. If only one of pwaff1 or pwaff2
1313 * is defined on a given cell, then the associated expression
1314 * is the defined one.
1316 static __isl_give isl_pw_aff *pw_aff_union_opt(__isl_take isl_pw_aff *pwaff1,
1317 __isl_take isl_pw_aff *pwaff2,
1318 __isl_give isl_basic_set *(*cmp)(__isl_take isl_aff *aff1,
1319 __isl_take isl_aff *aff2))
1326 if (!pwaff1 || !pwaff2)
1329 ctx = isl_space_get_ctx(pwaff1->dim);
1330 if (!isl_space_is_equal(pwaff1->dim, pwaff2->dim))
1331 isl_die(ctx, isl_error_invalid,
1332 "arguments should live in same space", goto error);
1334 if (isl_pw_aff_is_empty(pwaff1)) {
1335 isl_pw_aff_free(pwaff1);
1339 if (isl_pw_aff_is_empty(pwaff2)) {
1340 isl_pw_aff_free(pwaff2);
1344 n = 2 * (pwaff1->n + 1) * (pwaff2->n + 1);
1345 res = isl_pw_aff_alloc_size(isl_space_copy(pwaff1->dim), n);
1347 for (i = 0; i < pwaff1->n; ++i) {
1348 set = isl_set_copy(pwaff1->p[i].set);
1349 for (j = 0; j < pwaff2->n; ++j) {
1350 struct isl_set *common;
1353 common = isl_set_intersect(
1354 isl_set_copy(pwaff1->p[i].set),
1355 isl_set_copy(pwaff2->p[j].set));
1356 better = isl_set_from_basic_set(cmp(
1357 isl_aff_copy(pwaff2->p[j].aff),
1358 isl_aff_copy(pwaff1->p[i].aff)));
1359 better = isl_set_intersect(common, better);
1360 if (isl_set_plain_is_empty(better)) {
1361 isl_set_free(better);
1364 set = isl_set_subtract(set, isl_set_copy(better));
1366 res = isl_pw_aff_add_piece(res, better,
1367 isl_aff_copy(pwaff2->p[j].aff));
1369 res = isl_pw_aff_add_piece(res, set,
1370 isl_aff_copy(pwaff1->p[i].aff));
1373 for (j = 0; j < pwaff2->n; ++j) {
1374 set = isl_set_copy(pwaff2->p[j].set);
1375 for (i = 0; i < pwaff1->n; ++i)
1376 set = isl_set_subtract(set,
1377 isl_set_copy(pwaff1->p[i].set));
1378 res = isl_pw_aff_add_piece(res, set,
1379 isl_aff_copy(pwaff2->p[j].aff));
1382 isl_pw_aff_free(pwaff1);
1383 isl_pw_aff_free(pwaff2);
1387 isl_pw_aff_free(pwaff1);
1388 isl_pw_aff_free(pwaff2);
1392 /* Compute a piecewise quasi-affine expression with a domain that
1393 * is the union of those of pwaff1 and pwaff2 and such that on each
1394 * cell, the quasi-affine expression is the maximum of those of pwaff1
1395 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
1396 * cell, then the associated expression is the defined one.
1398 static __isl_give isl_pw_aff *pw_aff_union_max(__isl_take isl_pw_aff *pwaff1,
1399 __isl_take isl_pw_aff *pwaff2)
1401 return pw_aff_union_opt(pwaff1, pwaff2, &isl_aff_ge_basic_set);
1404 __isl_give isl_pw_aff *isl_pw_aff_union_max(__isl_take isl_pw_aff *pwaff1,
1405 __isl_take isl_pw_aff *pwaff2)
1407 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2,
1411 /* Compute a piecewise quasi-affine expression with a domain that
1412 * is the union of those of pwaff1 and pwaff2 and such that on each
1413 * cell, the quasi-affine expression is the minimum of those of pwaff1
1414 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
1415 * cell, then the associated expression is the defined one.
1417 static __isl_give isl_pw_aff *pw_aff_union_min(__isl_take isl_pw_aff *pwaff1,
1418 __isl_take isl_pw_aff *pwaff2)
1420 return pw_aff_union_opt(pwaff1, pwaff2, &isl_aff_le_basic_set);
1423 __isl_give isl_pw_aff *isl_pw_aff_union_min(__isl_take isl_pw_aff *pwaff1,
1424 __isl_take isl_pw_aff *pwaff2)
1426 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2,
1430 __isl_give isl_pw_aff *isl_pw_aff_union_opt(__isl_take isl_pw_aff *pwaff1,
1431 __isl_take isl_pw_aff *pwaff2, int max)
1434 return isl_pw_aff_union_max(pwaff1, pwaff2);
1436 return isl_pw_aff_union_min(pwaff1, pwaff2);
1439 /* Construct a map with as domain the domain of pwaff and
1440 * one-dimensional range corresponding to the affine expressions.
1442 static __isl_give isl_map *map_from_pw_aff(__isl_take isl_pw_aff *pwaff)
1451 dim = isl_pw_aff_get_space(pwaff);
1452 map = isl_map_empty(dim);
1454 for (i = 0; i < pwaff->n; ++i) {
1455 isl_basic_map *bmap;
1458 bmap = isl_basic_map_from_aff(isl_aff_copy(pwaff->p[i].aff));
1459 map_i = isl_map_from_basic_map(bmap);
1460 map_i = isl_map_intersect_domain(map_i,
1461 isl_set_copy(pwaff->p[i].set));
1462 map = isl_map_union_disjoint(map, map_i);
1465 isl_pw_aff_free(pwaff);
1470 /* Construct a map with as domain the domain of pwaff and
1471 * one-dimensional range corresponding to the affine expressions.
1473 __isl_give isl_map *isl_map_from_pw_aff(__isl_take isl_pw_aff *pwaff)
1477 if (isl_space_is_set(pwaff->dim))
1478 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
1479 "space of input is not a map",
1480 return isl_pw_aff_free(pwaff));
1481 return map_from_pw_aff(pwaff);
1484 /* Construct a one-dimensional set with as parameter domain
1485 * the domain of pwaff and the single set dimension
1486 * corresponding to the affine expressions.
1488 __isl_give isl_set *isl_set_from_pw_aff(__isl_take isl_pw_aff *pwaff)
1492 if (!isl_space_is_set(pwaff->dim))
1493 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
1494 "space of input is not a set",
1495 return isl_pw_aff_free(pwaff));
1496 return map_from_pw_aff(pwaff);
1499 /* Return a set containing those elements in the domain
1500 * of pwaff where it is non-negative.
1502 __isl_give isl_set *isl_pw_aff_nonneg_set(__isl_take isl_pw_aff *pwaff)
1510 set = isl_set_empty(isl_pw_aff_get_domain_space(pwaff));
1512 for (i = 0; i < pwaff->n; ++i) {
1513 isl_basic_set *bset;
1516 bset = isl_aff_nonneg_basic_set(isl_aff_copy(pwaff->p[i].aff));
1517 set_i = isl_set_from_basic_set(bset);
1518 set_i = isl_set_intersect(set_i, isl_set_copy(pwaff->p[i].set));
1519 set = isl_set_union_disjoint(set, set_i);
1522 isl_pw_aff_free(pwaff);
1527 /* Return a set containing those elements in the domain
1528 * of pwaff where it is zero.
1530 __isl_give isl_set *isl_pw_aff_zero_set(__isl_take isl_pw_aff *pwaff)
1538 set = isl_set_empty(isl_pw_aff_get_domain_space(pwaff));
1540 for (i = 0; i < pwaff->n; ++i) {
1541 isl_basic_set *bset;
1544 bset = isl_aff_zero_basic_set(isl_aff_copy(pwaff->p[i].aff));
1545 set_i = isl_set_from_basic_set(bset);
1546 set_i = isl_set_intersect(set_i, isl_set_copy(pwaff->p[i].set));
1547 set = isl_set_union_disjoint(set, set_i);
1550 isl_pw_aff_free(pwaff);
1555 /* Return a set containing those elements in the domain
1556 * of pwaff where it is not zero.
1558 __isl_give isl_set *isl_pw_aff_non_zero_set(__isl_take isl_pw_aff *pwaff)
1560 return isl_set_complement(isl_pw_aff_zero_set(pwaff));
1563 /* Return a set containing those elements in the shared domain
1564 * of pwaff1 and pwaff2 where pwaff1 is greater than (or equal) to pwaff2.
1566 * We compute the difference on the shared domain and then construct
1567 * the set of values where this difference is non-negative.
1568 * If strict is set, we first subtract 1 from the difference.
1569 * If equal is set, we only return the elements where pwaff1 and pwaff2
1572 static __isl_give isl_set *pw_aff_gte_set(__isl_take isl_pw_aff *pwaff1,
1573 __isl_take isl_pw_aff *pwaff2, int strict, int equal)
1575 isl_set *set1, *set2;
1577 set1 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff1));
1578 set2 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff2));
1579 set1 = isl_set_intersect(set1, set2);
1580 pwaff1 = isl_pw_aff_intersect_domain(pwaff1, isl_set_copy(set1));
1581 pwaff2 = isl_pw_aff_intersect_domain(pwaff2, isl_set_copy(set1));
1582 pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_neg(pwaff2));
1585 isl_space *dim = isl_set_get_space(set1);
1587 aff = isl_aff_zero_on_domain(isl_local_space_from_space(dim));
1588 aff = isl_aff_add_constant_si(aff, -1);
1589 pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_alloc(set1, aff));
1594 return isl_pw_aff_zero_set(pwaff1);
1595 return isl_pw_aff_nonneg_set(pwaff1);
1598 /* Return a set containing those elements in the shared domain
1599 * of pwaff1 and pwaff2 where pwaff1 is equal to pwaff2.
1601 static __isl_give isl_set *pw_aff_eq_set(__isl_take isl_pw_aff *pwaff1,
1602 __isl_take isl_pw_aff *pwaff2)
1604 return pw_aff_gte_set(pwaff1, pwaff2, 0, 1);
1607 __isl_give isl_set *isl_pw_aff_eq_set(__isl_take isl_pw_aff *pwaff1,
1608 __isl_take isl_pw_aff *pwaff2)
1610 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_eq_set);
1613 /* Return a set containing those elements in the shared domain
1614 * of pwaff1 and pwaff2 where pwaff1 is greater than or equal to pwaff2.
1616 static __isl_give isl_set *pw_aff_ge_set(__isl_take isl_pw_aff *pwaff1,
1617 __isl_take isl_pw_aff *pwaff2)
1619 return pw_aff_gte_set(pwaff1, pwaff2, 0, 0);
1622 __isl_give isl_set *isl_pw_aff_ge_set(__isl_take isl_pw_aff *pwaff1,
1623 __isl_take isl_pw_aff *pwaff2)
1625 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_ge_set);
1628 /* Return a set containing those elements in the shared domain
1629 * of pwaff1 and pwaff2 where pwaff1 is strictly greater than pwaff2.
1631 static __isl_give isl_set *pw_aff_gt_set(__isl_take isl_pw_aff *pwaff1,
1632 __isl_take isl_pw_aff *pwaff2)
1634 return pw_aff_gte_set(pwaff1, pwaff2, 1, 0);
1637 __isl_give isl_set *isl_pw_aff_gt_set(__isl_take isl_pw_aff *pwaff1,
1638 __isl_take isl_pw_aff *pwaff2)
1640 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_gt_set);
1643 __isl_give isl_set *isl_pw_aff_le_set(__isl_take isl_pw_aff *pwaff1,
1644 __isl_take isl_pw_aff *pwaff2)
1646 return isl_pw_aff_ge_set(pwaff2, pwaff1);
1649 __isl_give isl_set *isl_pw_aff_lt_set(__isl_take isl_pw_aff *pwaff1,
1650 __isl_take isl_pw_aff *pwaff2)
1652 return isl_pw_aff_gt_set(pwaff2, pwaff1);
1655 /* Return a set containing those elements in the shared domain
1656 * of the elements of list1 and list2 where each element in list1
1657 * has the relation specified by "fn" with each element in list2.
1659 static __isl_give isl_set *pw_aff_list_set(__isl_take isl_pw_aff_list *list1,
1660 __isl_take isl_pw_aff_list *list2,
1661 __isl_give isl_set *(*fn)(__isl_take isl_pw_aff *pwaff1,
1662 __isl_take isl_pw_aff *pwaff2))
1668 if (!list1 || !list2)
1671 ctx = isl_pw_aff_list_get_ctx(list1);
1672 if (list1->n < 1 || list2->n < 1)
1673 isl_die(ctx, isl_error_invalid,
1674 "list should contain at least one element", goto error);
1676 set = isl_set_universe(isl_pw_aff_get_domain_space(list1->p[0]));
1677 for (i = 0; i < list1->n; ++i)
1678 for (j = 0; j < list2->n; ++j) {
1681 set_ij = fn(isl_pw_aff_copy(list1->p[i]),
1682 isl_pw_aff_copy(list2->p[j]));
1683 set = isl_set_intersect(set, set_ij);
1686 isl_pw_aff_list_free(list1);
1687 isl_pw_aff_list_free(list2);
1690 isl_pw_aff_list_free(list1);
1691 isl_pw_aff_list_free(list2);
1695 /* Return a set containing those elements in the shared domain
1696 * of the elements of list1 and list2 where each element in list1
1697 * is equal to each element in list2.
1699 __isl_give isl_set *isl_pw_aff_list_eq_set(__isl_take isl_pw_aff_list *list1,
1700 __isl_take isl_pw_aff_list *list2)
1702 return pw_aff_list_set(list1, list2, &isl_pw_aff_eq_set);
1705 __isl_give isl_set *isl_pw_aff_list_ne_set(__isl_take isl_pw_aff_list *list1,
1706 __isl_take isl_pw_aff_list *list2)
1708 return pw_aff_list_set(list1, list2, &isl_pw_aff_ne_set);
1711 /* Return a set containing those elements in the shared domain
1712 * of the elements of list1 and list2 where each element in list1
1713 * is less than or equal to each element in list2.
1715 __isl_give isl_set *isl_pw_aff_list_le_set(__isl_take isl_pw_aff_list *list1,
1716 __isl_take isl_pw_aff_list *list2)
1718 return pw_aff_list_set(list1, list2, &isl_pw_aff_le_set);
1721 __isl_give isl_set *isl_pw_aff_list_lt_set(__isl_take isl_pw_aff_list *list1,
1722 __isl_take isl_pw_aff_list *list2)
1724 return pw_aff_list_set(list1, list2, &isl_pw_aff_lt_set);
1727 __isl_give isl_set *isl_pw_aff_list_ge_set(__isl_take isl_pw_aff_list *list1,
1728 __isl_take isl_pw_aff_list *list2)
1730 return pw_aff_list_set(list1, list2, &isl_pw_aff_ge_set);
1733 __isl_give isl_set *isl_pw_aff_list_gt_set(__isl_take isl_pw_aff_list *list1,
1734 __isl_take isl_pw_aff_list *list2)
1736 return pw_aff_list_set(list1, list2, &isl_pw_aff_gt_set);
1740 /* Return a set containing those elements in the shared domain
1741 * of pwaff1 and pwaff2 where pwaff1 is not equal to pwaff2.
1743 static __isl_give isl_set *pw_aff_ne_set(__isl_take isl_pw_aff *pwaff1,
1744 __isl_take isl_pw_aff *pwaff2)
1746 isl_set *set_lt, *set_gt;
1748 set_lt = isl_pw_aff_lt_set(isl_pw_aff_copy(pwaff1),
1749 isl_pw_aff_copy(pwaff2));
1750 set_gt = isl_pw_aff_gt_set(pwaff1, pwaff2);
1751 return isl_set_union_disjoint(set_lt, set_gt);
1754 __isl_give isl_set *isl_pw_aff_ne_set(__isl_take isl_pw_aff *pwaff1,
1755 __isl_take isl_pw_aff *pwaff2)
1757 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_ne_set);
1760 __isl_give isl_pw_aff *isl_pw_aff_scale_down(__isl_take isl_pw_aff *pwaff,
1765 if (isl_int_is_one(v))
1767 if (!isl_int_is_pos(v))
1768 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
1769 "factor needs to be positive",
1770 return isl_pw_aff_free(pwaff));
1771 pwaff = isl_pw_aff_cow(pwaff);
1777 for (i = 0; i < pwaff->n; ++i) {
1778 pwaff->p[i].aff = isl_aff_scale_down(pwaff->p[i].aff, v);
1779 if (!pwaff->p[i].aff)
1780 return isl_pw_aff_free(pwaff);
1786 __isl_give isl_pw_aff *isl_pw_aff_floor(__isl_take isl_pw_aff *pwaff)
1790 pwaff = isl_pw_aff_cow(pwaff);
1796 for (i = 0; i < pwaff->n; ++i) {
1797 pwaff->p[i].aff = isl_aff_floor(pwaff->p[i].aff);
1798 if (!pwaff->p[i].aff)
1799 return isl_pw_aff_free(pwaff);
1805 __isl_give isl_pw_aff *isl_pw_aff_ceil(__isl_take isl_pw_aff *pwaff)
1809 pwaff = isl_pw_aff_cow(pwaff);
1815 for (i = 0; i < pwaff->n; ++i) {
1816 pwaff->p[i].aff = isl_aff_ceil(pwaff->p[i].aff);
1817 if (!pwaff->p[i].aff)
1818 return isl_pw_aff_free(pwaff);
1824 /* Return an affine expression that is equal to pwaff_true for elements
1825 * in "cond" and to pwaff_false for elements not in "cond".
1826 * That is, return cond ? pwaff_true : pwaff_false;
1828 __isl_give isl_pw_aff *isl_pw_aff_cond(__isl_take isl_set *cond,
1829 __isl_take isl_pw_aff *pwaff_true, __isl_take isl_pw_aff *pwaff_false)
1833 comp = isl_set_complement(isl_set_copy(cond));
1834 pwaff_true = isl_pw_aff_intersect_domain(pwaff_true, cond);
1835 pwaff_false = isl_pw_aff_intersect_domain(pwaff_false, comp);
1837 return isl_pw_aff_add_disjoint(pwaff_true, pwaff_false);
1840 int isl_aff_is_cst(__isl_keep isl_aff *aff)
1845 return isl_seq_first_non_zero(aff->v->el + 2, aff->v->size - 2) == -1;
1848 /* Check whether pwaff is a piecewise constant.
1850 int isl_pw_aff_is_cst(__isl_keep isl_pw_aff *pwaff)
1857 for (i = 0; i < pwaff->n; ++i) {
1858 int is_cst = isl_aff_is_cst(pwaff->p[i].aff);
1859 if (is_cst < 0 || !is_cst)
1866 __isl_give isl_aff *isl_aff_mul(__isl_take isl_aff *aff1,
1867 __isl_take isl_aff *aff2)
1869 if (!isl_aff_is_cst(aff2) && isl_aff_is_cst(aff1))
1870 return isl_aff_mul(aff2, aff1);
1872 if (!isl_aff_is_cst(aff2))
1873 isl_die(isl_aff_get_ctx(aff1), isl_error_invalid,
1874 "at least one affine expression should be constant",
1877 aff1 = isl_aff_cow(aff1);
1881 aff1 = isl_aff_scale(aff1, aff2->v->el[1]);
1882 aff1 = isl_aff_scale_down(aff1, aff2->v->el[0]);
1892 static __isl_give isl_pw_aff *pw_aff_add(__isl_take isl_pw_aff *pwaff1,
1893 __isl_take isl_pw_aff *pwaff2)
1895 return isl_pw_aff_on_shared_domain(pwaff1, pwaff2, &isl_aff_add);
1898 __isl_give isl_pw_aff *isl_pw_aff_add(__isl_take isl_pw_aff *pwaff1,
1899 __isl_take isl_pw_aff *pwaff2)
1901 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_add);
1904 __isl_give isl_pw_aff *isl_pw_aff_union_add(__isl_take isl_pw_aff *pwaff1,
1905 __isl_take isl_pw_aff *pwaff2)
1907 return isl_pw_aff_union_add_(pwaff1, pwaff2);
1910 static __isl_give isl_pw_aff *pw_aff_mul(__isl_take isl_pw_aff *pwaff1,
1911 __isl_take isl_pw_aff *pwaff2)
1913 return isl_pw_aff_on_shared_domain(pwaff1, pwaff2, &isl_aff_mul);
1916 __isl_give isl_pw_aff *isl_pw_aff_mul(__isl_take isl_pw_aff *pwaff1,
1917 __isl_take isl_pw_aff *pwaff2)
1919 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_mul);
1922 static __isl_give isl_pw_aff *pw_aff_min(__isl_take isl_pw_aff *pwaff1,
1923 __isl_take isl_pw_aff *pwaff2)
1927 le = isl_pw_aff_le_set(isl_pw_aff_copy(pwaff1),
1928 isl_pw_aff_copy(pwaff2));
1929 return isl_pw_aff_cond(le, pwaff1, pwaff2);
1932 __isl_give isl_pw_aff *isl_pw_aff_min(__isl_take isl_pw_aff *pwaff1,
1933 __isl_take isl_pw_aff *pwaff2)
1935 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_min);
1938 static __isl_give isl_pw_aff *pw_aff_max(__isl_take isl_pw_aff *pwaff1,
1939 __isl_take isl_pw_aff *pwaff2)
1943 le = isl_pw_aff_ge_set(isl_pw_aff_copy(pwaff1),
1944 isl_pw_aff_copy(pwaff2));
1945 return isl_pw_aff_cond(le, pwaff1, pwaff2);
1948 __isl_give isl_pw_aff *isl_pw_aff_max(__isl_take isl_pw_aff *pwaff1,
1949 __isl_take isl_pw_aff *pwaff2)
1951 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_max);
1954 static __isl_give isl_pw_aff *pw_aff_list_reduce(
1955 __isl_take isl_pw_aff_list *list,
1956 __isl_give isl_pw_aff *(*fn)(__isl_take isl_pw_aff *pwaff1,
1957 __isl_take isl_pw_aff *pwaff2))
1966 ctx = isl_pw_aff_list_get_ctx(list);
1968 isl_die(ctx, isl_error_invalid,
1969 "list should contain at least one element",
1970 return isl_pw_aff_list_free(list));
1972 res = isl_pw_aff_copy(list->p[0]);
1973 for (i = 1; i < list->n; ++i)
1974 res = fn(res, isl_pw_aff_copy(list->p[i]));
1976 isl_pw_aff_list_free(list);
1980 /* Return an isl_pw_aff that maps each element in the intersection of the
1981 * domains of the elements of list to the minimal corresponding affine
1984 __isl_give isl_pw_aff *isl_pw_aff_list_min(__isl_take isl_pw_aff_list *list)
1986 return pw_aff_list_reduce(list, &isl_pw_aff_min);
1989 /* Return an isl_pw_aff that maps each element in the intersection of the
1990 * domains of the elements of list to the maximal corresponding affine
1993 __isl_give isl_pw_aff *isl_pw_aff_list_max(__isl_take isl_pw_aff_list *list)
1995 return pw_aff_list_reduce(list, &isl_pw_aff_max);
2001 #include <isl_multi_templ.c>
2003 __isl_give isl_multi_aff *isl_multi_aff_add(__isl_take isl_multi_aff *maff1,
2004 __isl_take isl_multi_aff *maff2)
2009 maff1 = isl_multi_aff_cow(maff1);
2010 if (!maff1 || !maff2)
2013 ctx = isl_multi_aff_get_ctx(maff1);
2014 if (!isl_space_is_equal(maff1->space, maff2->space))
2015 isl_die(ctx, isl_error_invalid,
2016 "spaces don't match", goto error);
2018 for (i = 0; i < maff1->n; ++i) {
2019 maff1->p[i] = isl_aff_add(maff1->p[i],
2020 isl_aff_copy(maff2->p[i]));
2025 isl_multi_aff_free(maff2);
2028 isl_multi_aff_free(maff1);
2029 isl_multi_aff_free(maff2);
2033 /* Exploit the equalities in "eq" to simplify the affine expressions.
2035 static __isl_give isl_multi_aff *isl_multi_aff_substitute_equalities(
2036 __isl_take isl_multi_aff *maff, __isl_take isl_basic_set *eq)
2040 maff = isl_multi_aff_cow(maff);
2044 for (i = 0; i < maff->n; ++i) {
2045 maff->p[i] = isl_aff_substitute_equalities(maff->p[i],
2046 isl_basic_set_copy(eq));
2051 isl_basic_set_free(eq);
2054 isl_basic_set_free(eq);
2055 isl_multi_aff_free(maff);
2059 __isl_give isl_multi_aff *isl_multi_aff_scale(__isl_take isl_multi_aff *maff,
2064 maff = isl_multi_aff_cow(maff);
2068 for (i = 0; i < maff->n; ++i) {
2069 maff->p[i] = isl_aff_scale(maff->p[i], f);
2071 return isl_multi_aff_free(maff);
2077 __isl_give isl_multi_aff *isl_multi_aff_add_on_domain(__isl_keep isl_set *dom,
2078 __isl_take isl_multi_aff *maff1, __isl_take isl_multi_aff *maff2)
2080 maff1 = isl_multi_aff_add(maff1, maff2);
2081 maff1 = isl_multi_aff_gist(maff1, isl_set_copy(dom));
2085 int isl_multi_aff_is_empty(__isl_keep isl_multi_aff *maff)
2093 int isl_multi_aff_plain_is_equal(__isl_keep isl_multi_aff *maff1,
2094 __isl_keep isl_multi_aff *maff2)
2099 if (!maff1 || !maff2)
2101 if (maff1->n != maff2->n)
2103 equal = isl_space_is_equal(maff1->space, maff2->space);
2104 if (equal < 0 || !equal)
2107 for (i = 0; i < maff1->n; ++i) {
2108 equal = isl_aff_plain_is_equal(maff1->p[i], maff2->p[i]);
2109 if (equal < 0 || !equal)
2116 __isl_give isl_multi_aff *isl_multi_aff_set_dim_name(
2117 __isl_take isl_multi_aff *maff,
2118 enum isl_dim_type type, unsigned pos, const char *s)
2122 maff = isl_multi_aff_cow(maff);
2126 maff->space = isl_space_set_dim_name(maff->space, type, pos, s);
2128 return isl_multi_aff_free(maff);
2129 for (i = 0; i < maff->n; ++i) {
2130 maff->p[i] = isl_aff_set_dim_name(maff->p[i], type, pos, s);
2132 return isl_multi_aff_free(maff);
2138 __isl_give isl_multi_aff *isl_multi_aff_drop_dims(__isl_take isl_multi_aff *maff,
2139 enum isl_dim_type type, unsigned first, unsigned n)
2143 maff = isl_multi_aff_cow(maff);
2147 maff->space = isl_space_drop_dims(maff->space, type, first, n);
2149 return isl_multi_aff_free(maff);
2151 if (type == isl_dim_out) {
2152 for (i = 0; i < n; ++i)
2153 isl_aff_free(maff->p[first + i]);
2154 for (i = first; i + n < maff->n; ++i)
2155 maff->p[i] = maff->p[i + n];
2160 for (i = 0; i < maff->n; ++i) {
2161 maff->p[i] = isl_aff_drop_dims(maff->p[i], type, first, n);
2163 return isl_multi_aff_free(maff);
2170 #define PW isl_pw_multi_aff
2172 #define EL isl_multi_aff
2174 #define EL_IS_ZERO is_empty
2178 #define IS_ZERO is_empty
2181 #undef DEFAULT_IS_ZERO
2182 #define DEFAULT_IS_ZERO 0
2187 #define NO_INVOLVES_DIMS
2188 #define NO_MOVE_DIMS
2189 #define NO_INSERT_DIMS
2193 #include <isl_pw_templ.c>
2195 static __isl_give isl_pw_multi_aff *pw_multi_aff_add(
2196 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
2198 return isl_pw_multi_aff_on_shared_domain(pma1, pma2,
2199 &isl_multi_aff_add);
2202 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_add(
2203 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
2205 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
2209 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_add(
2210 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
2212 return isl_pw_multi_aff_union_add_(pma1, pma2);
2215 /* Construct a map mapping the domain the piecewise multi-affine expression
2216 * to its range, with each dimension in the range equated to the
2217 * corresponding affine expression on its cell.
2219 __isl_give isl_map *isl_map_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma)
2227 map = isl_map_empty(isl_pw_multi_aff_get_space(pma));
2229 for (i = 0; i < pma->n; ++i) {
2230 isl_multi_aff *maff;
2231 isl_basic_map *bmap;
2234 maff = isl_multi_aff_copy(pma->p[i].maff);
2235 bmap = isl_basic_map_from_multi_aff(maff);
2236 map_i = isl_map_from_basic_map(bmap);
2237 map_i = isl_map_intersect_domain(map_i,
2238 isl_set_copy(pma->p[i].set));
2239 map = isl_map_union_disjoint(map, map_i);
2242 isl_pw_multi_aff_free(pma);
2246 __isl_give isl_set *isl_set_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma)
2248 if (!isl_space_is_set(pma->dim))
2249 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
2250 "isl_pw_multi_aff cannot be converted into an isl_set",
2251 return isl_pw_multi_aff_free(pma));
2253 return isl_map_from_pw_multi_aff(pma);
2256 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
2257 * This obivously only works if the input "map" is single-valued.
2258 * If so, we compute the lexicographic minimum of the image in the form
2259 * of an isl_pw_multi_aff. Since the image is unique, it is equal
2260 * to its lexicographic minimum.
2261 * If the input is not single-valued, we produce an error.
2263 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_map(__isl_take isl_map *map)
2267 isl_pw_multi_aff *pma;
2272 sv = isl_map_is_single_valued(map);
2276 isl_die(isl_map_get_ctx(map), isl_error_invalid,
2277 "map is not single-valued", goto error);
2278 map = isl_map_make_disjoint(map);
2282 pma = isl_pw_multi_aff_empty(isl_map_get_space(map));
2284 for (i = 0; i < map->n; ++i) {
2285 isl_pw_multi_aff *pma_i;
2286 isl_basic_map *bmap;
2287 bmap = isl_basic_map_copy(map->p[i]);
2288 pma_i = isl_basic_map_lexmin_pw_multi_aff(bmap);
2289 pma = isl_pw_multi_aff_add_disjoint(pma, pma_i);
2299 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_set(__isl_take isl_set *set)
2301 return isl_pw_multi_aff_from_map(set);
2304 /* Plug in "subs" for dimension "type", "pos" of "aff".
2306 * Let i be the dimension to replace and let "subs" be of the form
2310 * and "aff" of the form
2316 * floor((a f + d g')/(m d))
2318 * where g' is the result of plugging in "subs" in each of the integer
2321 __isl_give isl_aff *isl_aff_substitute(__isl_take isl_aff *aff,
2322 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
2327 aff = isl_aff_cow(aff);
2329 return isl_aff_free(aff);
2331 ctx = isl_aff_get_ctx(aff);
2332 if (!isl_space_is_equal(aff->ls->dim, subs->ls->dim))
2333 isl_die(ctx, isl_error_invalid,
2334 "spaces don't match", return isl_aff_free(aff));
2335 if (isl_local_space_dim(subs->ls, isl_dim_div) != 0)
2336 isl_die(ctx, isl_error_unsupported,
2337 "cannot handle divs yet", return isl_aff_free(aff));
2339 aff->ls = isl_local_space_substitute(aff->ls, type, pos, subs);
2341 return isl_aff_free(aff);
2343 aff->v = isl_vec_cow(aff->v);
2345 return isl_aff_free(aff);
2347 pos += isl_local_space_offset(aff->ls, type);
2350 isl_int_set(v, aff->v->el[1 + pos]);
2351 isl_int_set_si(aff->v->el[1 + pos], 0);
2352 isl_seq_combine(aff->v->el + 1, subs->v->el[0], aff->v->el + 1,
2353 v, subs->v->el + 1, subs->v->size - 1);
2354 isl_int_mul(aff->v->el[0], aff->v->el[0], subs->v->el[0]);
2360 /* Plug in "subs" for dimension "type", "pos" in each of the affine
2361 * expressions in "maff".
2363 __isl_give isl_multi_aff *isl_multi_aff_substitute(
2364 __isl_take isl_multi_aff *maff, enum isl_dim_type type, unsigned pos,
2365 __isl_keep isl_aff *subs)
2369 maff = isl_multi_aff_cow(maff);
2371 return isl_multi_aff_free(maff);
2373 if (type == isl_dim_in)
2376 for (i = 0; i < maff->n; ++i) {
2377 maff->p[i] = isl_aff_substitute(maff->p[i], type, pos, subs);
2379 return isl_multi_aff_free(maff);
2385 /* Plug in "subs" for dimension "type", "pos" of "pma".
2387 * pma is of the form
2391 * while subs is of the form
2393 * v' = B_j(v) -> S_j
2395 * Each pair i,j such that C_ij = A_i \cap B_i is non-empty
2396 * has a contribution in the result, in particular
2398 * C_ij(S_j) -> M_i(S_j)
2400 * Note that plugging in S_j in C_ij may also result in an empty set
2401 * and this contribution should simply be discarded.
2403 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_substitute(
2404 __isl_take isl_pw_multi_aff *pma, enum isl_dim_type type, unsigned pos,
2405 __isl_keep isl_pw_aff *subs)
2408 isl_pw_multi_aff *res;
2411 return isl_pw_multi_aff_free(pma);
2413 n = pma->n * subs->n;
2414 res = isl_pw_multi_aff_alloc_size(isl_space_copy(pma->dim), n);
2416 for (i = 0; i < pma->n; ++i) {
2417 for (j = 0; j < subs->n; ++j) {
2419 isl_multi_aff *res_ij;
2420 common = isl_set_intersect(
2421 isl_set_copy(pma->p[i].set),
2422 isl_set_copy(subs->p[j].set));
2423 common = isl_set_substitute(common,
2424 type, pos, subs->p[j].aff);
2425 if (isl_set_plain_is_empty(common)) {
2426 isl_set_free(common);
2430 res_ij = isl_multi_aff_substitute(
2431 isl_multi_aff_copy(pma->p[i].maff),
2432 type, pos, subs->p[j].aff);
2434 res = isl_pw_multi_aff_add_piece(res, common, res_ij);
2438 isl_pw_multi_aff_free(pma);
2442 /* Extend the local space of "dst" to include the divs
2443 * in the local space of "src".
2445 __isl_give isl_aff *isl_aff_align_divs(__isl_take isl_aff *dst,
2446 __isl_keep isl_aff *src)
2454 return isl_aff_free(dst);
2456 ctx = isl_aff_get_ctx(src);
2457 if (!isl_space_is_equal(src->ls->dim, dst->ls->dim))
2458 isl_die(ctx, isl_error_invalid,
2459 "spaces don't match", goto error);
2461 if (src->ls->div->n_row == 0)
2464 exp1 = isl_alloc_array(ctx, int, src->ls->div->n_row);
2465 exp2 = isl_alloc_array(ctx, int, dst->ls->div->n_row);
2469 div = isl_merge_divs(src->ls->div, dst->ls->div, exp1, exp2);
2470 dst = isl_aff_expand_divs(dst, div, exp2);
2478 return isl_aff_free(dst);
2481 /* Adjust the local spaces of the affine expressions in "maff"
2482 * such that they all have the save divs.
2484 __isl_give isl_multi_aff *isl_multi_aff_align_divs(
2485 __isl_take isl_multi_aff *maff)
2493 maff = isl_multi_aff_cow(maff);
2497 for (i = 1; i < maff->n; ++i)
2498 maff->p[0] = isl_aff_align_divs(maff->p[0], maff->p[i]);
2499 for (i = 1; i < maff->n; ++i) {
2500 maff->p[i] = isl_aff_align_divs(maff->p[i], maff->p[0]);
2502 return isl_multi_aff_free(maff);
2508 __isl_give isl_aff *isl_aff_lift(__isl_take isl_aff *aff)
2510 aff = isl_aff_cow(aff);
2514 aff->ls = isl_local_space_lift(aff->ls);
2516 return isl_aff_free(aff);
2521 /* Lift "maff" to a space with extra dimensions such that the result
2522 * has no more existentially quantified variables.
2523 * If "ls" is not NULL, then *ls is assigned the local space that lies
2524 * at the basis of the lifting applied to "maff".
2526 __isl_give isl_multi_aff *isl_multi_aff_lift(__isl_take isl_multi_aff *maff,
2527 __isl_give isl_local_space **ls)
2541 isl_space *space = isl_multi_aff_get_domain_space(maff);
2542 *ls = isl_local_space_from_space(space);
2544 return isl_multi_aff_free(maff);
2549 maff = isl_multi_aff_cow(maff);
2550 maff = isl_multi_aff_align_divs(maff);
2554 n_div = isl_aff_dim(maff->p[0], isl_dim_div);
2555 space = isl_multi_aff_get_space(maff);
2556 space = isl_space_lift(isl_space_domain(space), n_div);
2557 space = isl_space_extend_domain_with_range(space,
2558 isl_multi_aff_get_space(maff));
2560 return isl_multi_aff_free(maff);
2561 isl_space_free(maff->space);
2562 maff->space = space;
2565 *ls = isl_aff_get_domain_local_space(maff->p[0]);
2567 return isl_multi_aff_free(maff);
2570 for (i = 0; i < maff->n; ++i) {
2571 maff->p[i] = isl_aff_lift(maff->p[i]);
2579 isl_local_space_free(*ls);
2580 return isl_multi_aff_free(maff);
2584 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma".
2586 __isl_give isl_pw_aff *isl_pw_multi_aff_get_pw_aff(
2587 __isl_keep isl_pw_multi_aff *pma, int pos)
2597 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
2598 if (pos < 0 || pos >= n_out)
2599 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
2600 "index out of bounds", return NULL);
2602 space = isl_pw_multi_aff_get_space(pma);
2603 space = isl_space_drop_dims(space, isl_dim_out,
2604 pos + 1, n_out - pos - 1);
2605 space = isl_space_drop_dims(space, isl_dim_out, 0, pos);
2607 pa = isl_pw_aff_alloc_size(space, pma->n);
2608 for (i = 0; i < pma->n; ++i) {
2610 aff = isl_multi_aff_get_aff(pma->p[i].maff, pos);
2611 pa = isl_pw_aff_add_piece(pa, isl_set_copy(pma->p[i].set), aff);