2 * Copyright 2011 INRIA Saclay
3 * Copyright 2011 Sven Verdoolaege
4 * Copyright 2012-2013 Ecole Normale Superieure
6 * Use of this software is governed by the MIT license
8 * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
9 * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
11 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
14 #include <isl_ctx_private.h>
16 #include <isl_map_private.h>
17 #include <isl_union_map_private.h>
18 #include <isl_aff_private.h>
19 #include <isl_space_private.h>
20 #include <isl_local_space_private.h>
21 #include <isl_mat_private.h>
22 #include <isl/constraint.h>
25 #include <isl_config.h>
30 #include <isl_list_templ.c>
35 #include <isl_list_templ.c>
37 __isl_give isl_aff *isl_aff_alloc_vec(__isl_take isl_local_space *ls,
38 __isl_take isl_vec *v)
45 aff = isl_calloc_type(v->ctx, struct isl_aff);
55 isl_local_space_free(ls);
60 __isl_give isl_aff *isl_aff_alloc(__isl_take isl_local_space *ls)
69 ctx = isl_local_space_get_ctx(ls);
70 if (!isl_local_space_divs_known(ls))
71 isl_die(ctx, isl_error_invalid, "local space has unknown divs",
73 if (!isl_local_space_is_set(ls))
74 isl_die(ctx, isl_error_invalid,
75 "domain of affine expression should be a set",
78 total = isl_local_space_dim(ls, isl_dim_all);
79 v = isl_vec_alloc(ctx, 1 + 1 + total);
80 return isl_aff_alloc_vec(ls, v);
82 isl_local_space_free(ls);
86 __isl_give isl_aff *isl_aff_zero_on_domain(__isl_take isl_local_space *ls)
90 aff = isl_aff_alloc(ls);
94 isl_int_set_si(aff->v->el[0], 1);
95 isl_seq_clr(aff->v->el + 1, aff->v->size - 1);
100 /* Return a piecewise affine expression defined on the specified domain
101 * that is equal to zero.
103 __isl_give isl_pw_aff *isl_pw_aff_zero_on_domain(__isl_take isl_local_space *ls)
105 return isl_pw_aff_from_aff(isl_aff_zero_on_domain(ls));
108 /* Return an affine expression that is equal to the specified dimension
111 __isl_give isl_aff *isl_aff_var_on_domain(__isl_take isl_local_space *ls,
112 enum isl_dim_type type, unsigned pos)
120 space = isl_local_space_get_space(ls);
123 if (isl_space_is_map(space))
124 isl_die(isl_space_get_ctx(space), isl_error_invalid,
125 "expecting (parameter) set space", goto error);
126 if (pos >= isl_local_space_dim(ls, type))
127 isl_die(isl_space_get_ctx(space), isl_error_invalid,
128 "position out of bounds", goto error);
130 isl_space_free(space);
131 aff = isl_aff_alloc(ls);
135 pos += isl_local_space_offset(aff->ls, type);
137 isl_int_set_si(aff->v->el[0], 1);
138 isl_seq_clr(aff->v->el + 1, aff->v->size - 1);
139 isl_int_set_si(aff->v->el[1 + pos], 1);
143 isl_local_space_free(ls);
144 isl_space_free(space);
148 /* Return a piecewise affine expression that is equal to
149 * the specified dimension in "ls".
151 __isl_give isl_pw_aff *isl_pw_aff_var_on_domain(__isl_take isl_local_space *ls,
152 enum isl_dim_type type, unsigned pos)
154 return isl_pw_aff_from_aff(isl_aff_var_on_domain(ls, type, pos));
157 __isl_give isl_aff *isl_aff_copy(__isl_keep isl_aff *aff)
166 __isl_give isl_aff *isl_aff_dup(__isl_keep isl_aff *aff)
171 return isl_aff_alloc_vec(isl_local_space_copy(aff->ls),
172 isl_vec_copy(aff->v));
175 __isl_give isl_aff *isl_aff_cow(__isl_take isl_aff *aff)
183 return isl_aff_dup(aff);
186 void *isl_aff_free(__isl_take isl_aff *aff)
194 isl_local_space_free(aff->ls);
195 isl_vec_free(aff->v);
202 isl_ctx *isl_aff_get_ctx(__isl_keep isl_aff *aff)
204 return aff ? isl_local_space_get_ctx(aff->ls) : NULL;
207 /* Externally, an isl_aff has a map space, but internally, the
208 * ls field corresponds to the domain of that space.
210 int isl_aff_dim(__isl_keep isl_aff *aff, enum isl_dim_type type)
214 if (type == isl_dim_out)
216 if (type == isl_dim_in)
218 return isl_local_space_dim(aff->ls, type);
221 __isl_give isl_space *isl_aff_get_domain_space(__isl_keep isl_aff *aff)
223 return aff ? isl_local_space_get_space(aff->ls) : NULL;
226 __isl_give isl_space *isl_aff_get_space(__isl_keep isl_aff *aff)
231 space = isl_local_space_get_space(aff->ls);
232 space = isl_space_from_domain(space);
233 space = isl_space_add_dims(space, isl_dim_out, 1);
237 __isl_give isl_local_space *isl_aff_get_domain_local_space(
238 __isl_keep isl_aff *aff)
240 return aff ? isl_local_space_copy(aff->ls) : NULL;
243 __isl_give isl_local_space *isl_aff_get_local_space(__isl_keep isl_aff *aff)
248 ls = isl_local_space_copy(aff->ls);
249 ls = isl_local_space_from_domain(ls);
250 ls = isl_local_space_add_dims(ls, isl_dim_out, 1);
254 /* Externally, an isl_aff has a map space, but internally, the
255 * ls field corresponds to the domain of that space.
257 const char *isl_aff_get_dim_name(__isl_keep isl_aff *aff,
258 enum isl_dim_type type, unsigned pos)
262 if (type == isl_dim_out)
264 if (type == isl_dim_in)
266 return isl_local_space_get_dim_name(aff->ls, type, pos);
269 __isl_give isl_aff *isl_aff_reset_domain_space(__isl_take isl_aff *aff,
270 __isl_take isl_space *dim)
272 aff = isl_aff_cow(aff);
276 aff->ls = isl_local_space_reset_space(aff->ls, dim);
278 return isl_aff_free(aff);
287 /* Reset the space of "aff". This function is called from isl_pw_templ.c
288 * and doesn't know if the space of an element object is represented
289 * directly or through its domain. It therefore passes along both.
291 __isl_give isl_aff *isl_aff_reset_space_and_domain(__isl_take isl_aff *aff,
292 __isl_take isl_space *space, __isl_take isl_space *domain)
294 isl_space_free(space);
295 return isl_aff_reset_domain_space(aff, domain);
298 /* Reorder the coefficients of the affine expression based
299 * on the given reodering.
300 * The reordering r is assumed to have been extended with the local
303 static __isl_give isl_vec *vec_reorder(__isl_take isl_vec *vec,
304 __isl_take isl_reordering *r, int n_div)
312 res = isl_vec_alloc(vec->ctx,
313 2 + isl_space_dim(r->dim, isl_dim_all) + n_div);
314 isl_seq_cpy(res->el, vec->el, 2);
315 isl_seq_clr(res->el + 2, res->size - 2);
316 for (i = 0; i < r->len; ++i)
317 isl_int_set(res->el[2 + r->pos[i]], vec->el[2 + i]);
319 isl_reordering_free(r);
324 isl_reordering_free(r);
328 /* Reorder the dimensions of the domain of "aff" according
329 * to the given reordering.
331 __isl_give isl_aff *isl_aff_realign_domain(__isl_take isl_aff *aff,
332 __isl_take isl_reordering *r)
334 aff = isl_aff_cow(aff);
338 r = isl_reordering_extend(r, aff->ls->div->n_row);
339 aff->v = vec_reorder(aff->v, isl_reordering_copy(r),
340 aff->ls->div->n_row);
341 aff->ls = isl_local_space_realign(aff->ls, r);
343 if (!aff->v || !aff->ls)
344 return isl_aff_free(aff);
349 isl_reordering_free(r);
353 __isl_give isl_aff *isl_aff_align_params(__isl_take isl_aff *aff,
354 __isl_take isl_space *model)
359 if (!isl_space_match(aff->ls->dim, isl_dim_param,
360 model, isl_dim_param)) {
363 model = isl_space_drop_dims(model, isl_dim_in,
364 0, isl_space_dim(model, isl_dim_in));
365 model = isl_space_drop_dims(model, isl_dim_out,
366 0, isl_space_dim(model, isl_dim_out));
367 exp = isl_parameter_alignment_reordering(aff->ls->dim, model);
368 exp = isl_reordering_extend_space(exp,
369 isl_aff_get_domain_space(aff));
370 aff = isl_aff_realign_domain(aff, exp);
373 isl_space_free(model);
376 isl_space_free(model);
381 int isl_aff_plain_is_zero(__isl_keep isl_aff *aff)
386 return isl_seq_first_non_zero(aff->v->el + 1, aff->v->size - 1) < 0;
389 int isl_aff_plain_is_equal(__isl_keep isl_aff *aff1, __isl_keep isl_aff *aff2)
396 equal = isl_local_space_is_equal(aff1->ls, aff2->ls);
397 if (equal < 0 || !equal)
400 return isl_vec_is_equal(aff1->v, aff2->v);
403 int isl_aff_get_denominator(__isl_keep isl_aff *aff, isl_int *v)
407 isl_int_set(*v, aff->v->el[0]);
411 int isl_aff_get_constant(__isl_keep isl_aff *aff, isl_int *v)
415 isl_int_set(*v, aff->v->el[1]);
419 int isl_aff_get_coefficient(__isl_keep isl_aff *aff,
420 enum isl_dim_type type, int pos, isl_int *v)
425 if (type == isl_dim_out)
426 isl_die(aff->v->ctx, isl_error_invalid,
427 "output/set dimension does not have a coefficient",
429 if (type == isl_dim_in)
432 if (pos >= isl_local_space_dim(aff->ls, type))
433 isl_die(aff->v->ctx, isl_error_invalid,
434 "position out of bounds", return -1);
436 pos += isl_local_space_offset(aff->ls, type);
437 isl_int_set(*v, aff->v->el[1 + pos]);
442 __isl_give isl_aff *isl_aff_set_denominator(__isl_take isl_aff *aff, isl_int v)
444 aff = isl_aff_cow(aff);
448 aff->v = isl_vec_cow(aff->v);
450 return isl_aff_free(aff);
452 isl_int_set(aff->v->el[0], v);
457 __isl_give isl_aff *isl_aff_set_constant(__isl_take isl_aff *aff, isl_int v)
459 aff = isl_aff_cow(aff);
463 aff->v = isl_vec_cow(aff->v);
465 return isl_aff_free(aff);
467 isl_int_set(aff->v->el[1], v);
472 __isl_give isl_aff *isl_aff_add_constant(__isl_take isl_aff *aff, isl_int v)
474 if (isl_int_is_zero(v))
477 aff = isl_aff_cow(aff);
481 aff->v = isl_vec_cow(aff->v);
483 return isl_aff_free(aff);
485 isl_int_addmul(aff->v->el[1], aff->v->el[0], v);
490 __isl_give isl_aff *isl_aff_add_constant_si(__isl_take isl_aff *aff, int v)
495 isl_int_set_si(t, v);
496 aff = isl_aff_add_constant(aff, t);
502 /* Add "v" to the numerator of the constant term of "aff".
504 __isl_give isl_aff *isl_aff_add_constant_num(__isl_take isl_aff *aff, isl_int v)
506 if (isl_int_is_zero(v))
509 aff = isl_aff_cow(aff);
513 aff->v = isl_vec_cow(aff->v);
515 return isl_aff_free(aff);
517 isl_int_add(aff->v->el[1], aff->v->el[1], v);
522 /* Add "v" to the numerator of the constant term of "aff".
524 __isl_give isl_aff *isl_aff_add_constant_num_si(__isl_take isl_aff *aff, int v)
532 isl_int_set_si(t, v);
533 aff = isl_aff_add_constant_num(aff, t);
539 __isl_give isl_aff *isl_aff_set_constant_si(__isl_take isl_aff *aff, int v)
541 aff = isl_aff_cow(aff);
545 aff->v = isl_vec_cow(aff->v);
547 return isl_aff_free(aff);
549 isl_int_set_si(aff->v->el[1], v);
554 __isl_give isl_aff *isl_aff_set_coefficient(__isl_take isl_aff *aff,
555 enum isl_dim_type type, int pos, isl_int v)
560 if (type == isl_dim_out)
561 isl_die(aff->v->ctx, isl_error_invalid,
562 "output/set dimension does not have a coefficient",
563 return isl_aff_free(aff));
564 if (type == isl_dim_in)
567 if (pos >= isl_local_space_dim(aff->ls, type))
568 isl_die(aff->v->ctx, isl_error_invalid,
569 "position out of bounds", return isl_aff_free(aff));
571 aff = isl_aff_cow(aff);
575 aff->v = isl_vec_cow(aff->v);
577 return isl_aff_free(aff);
579 pos += isl_local_space_offset(aff->ls, type);
580 isl_int_set(aff->v->el[1 + pos], v);
585 __isl_give isl_aff *isl_aff_set_coefficient_si(__isl_take isl_aff *aff,
586 enum isl_dim_type type, int pos, int v)
591 if (type == isl_dim_out)
592 isl_die(aff->v->ctx, isl_error_invalid,
593 "output/set dimension does not have a coefficient",
594 return isl_aff_free(aff));
595 if (type == isl_dim_in)
598 if (pos >= isl_local_space_dim(aff->ls, type))
599 isl_die(aff->v->ctx, isl_error_invalid,
600 "position out of bounds", return isl_aff_free(aff));
602 aff = isl_aff_cow(aff);
606 aff->v = isl_vec_cow(aff->v);
608 return isl_aff_free(aff);
610 pos += isl_local_space_offset(aff->ls, type);
611 isl_int_set_si(aff->v->el[1 + pos], v);
616 __isl_give isl_aff *isl_aff_add_coefficient(__isl_take isl_aff *aff,
617 enum isl_dim_type type, int pos, isl_int v)
622 if (type == isl_dim_out)
623 isl_die(aff->v->ctx, isl_error_invalid,
624 "output/set dimension does not have a coefficient",
625 return isl_aff_free(aff));
626 if (type == isl_dim_in)
629 if (pos >= isl_local_space_dim(aff->ls, type))
630 isl_die(aff->v->ctx, isl_error_invalid,
631 "position out of bounds", return isl_aff_free(aff));
633 aff = isl_aff_cow(aff);
637 aff->v = isl_vec_cow(aff->v);
639 return isl_aff_free(aff);
641 pos += isl_local_space_offset(aff->ls, type);
642 isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v);
647 __isl_give isl_aff *isl_aff_add_coefficient_si(__isl_take isl_aff *aff,
648 enum isl_dim_type type, int pos, int v)
653 isl_int_set_si(t, v);
654 aff = isl_aff_add_coefficient(aff, type, pos, t);
660 __isl_give isl_aff *isl_aff_get_div(__isl_keep isl_aff *aff, int pos)
665 return isl_local_space_get_div(aff->ls, pos);
668 __isl_give isl_aff *isl_aff_neg(__isl_take isl_aff *aff)
670 aff = isl_aff_cow(aff);
673 aff->v = isl_vec_cow(aff->v);
675 return isl_aff_free(aff);
677 isl_seq_neg(aff->v->el + 1, aff->v->el + 1, aff->v->size - 1);
682 /* Remove divs from the local space that do not appear in the affine
684 * We currently only remove divs at the end.
685 * Some intermediate divs may also not appear directly in the affine
686 * expression, but we would also need to check that no other divs are
687 * defined in terms of them.
689 __isl_give isl_aff *isl_aff_remove_unused_divs( __isl_take isl_aff *aff)
698 n = isl_local_space_dim(aff->ls, isl_dim_div);
699 off = isl_local_space_offset(aff->ls, isl_dim_div);
701 pos = isl_seq_last_non_zero(aff->v->el + 1 + off, n) + 1;
705 aff = isl_aff_cow(aff);
709 aff->ls = isl_local_space_drop_dims(aff->ls, isl_dim_div, pos, n - pos);
710 aff->v = isl_vec_drop_els(aff->v, 1 + off + pos, n - pos);
711 if (!aff->ls || !aff->v)
712 return isl_aff_free(aff);
717 /* Given two affine expressions "p" of length p_len (including the
718 * denominator and the constant term) and "subs" of length subs_len,
719 * plug in "subs" for the variable at position "pos".
720 * The variables of "subs" and "p" are assumed to match up to subs_len,
721 * but "p" may have additional variables.
722 * "v" is an initialized isl_int that can be used internally.
724 * In particular, if "p" represents the expression
728 * with i the variable at position "pos" and "subs" represents the expression
732 * then the result represents the expression
737 void isl_seq_substitute(isl_int *p, int pos, isl_int *subs,
738 int p_len, int subs_len, isl_int v)
740 isl_int_set(v, p[1 + pos]);
741 isl_int_set_si(p[1 + pos], 0);
742 isl_seq_combine(p + 1, subs[0], p + 1, v, subs + 1, subs_len - 1);
743 isl_seq_scale(p + subs_len, p + subs_len, subs[0], p_len - subs_len);
744 isl_int_mul(p[0], p[0], subs[0]);
747 /* Look for any divs in the aff->ls with a denominator equal to one
748 * and plug them into the affine expression and any subsequent divs
749 * that may reference the div.
751 static __isl_give isl_aff *plug_in_integral_divs(__isl_take isl_aff *aff)
763 n = isl_local_space_dim(aff->ls, isl_dim_div);
765 for (i = 0; i < n; ++i) {
766 if (!isl_int_is_one(aff->ls->div->row[i][0]))
768 ls = isl_local_space_copy(aff->ls);
769 ls = isl_local_space_substitute_seq(ls, isl_dim_div, i,
770 aff->ls->div->row[i], len, i + 1, n - (i + 1));
771 vec = isl_vec_copy(aff->v);
772 vec = isl_vec_cow(vec);
778 pos = isl_local_space_offset(aff->ls, isl_dim_div) + i;
779 isl_seq_substitute(vec->el, pos, aff->ls->div->row[i],
784 isl_vec_free(aff->v);
786 isl_local_space_free(aff->ls);
793 isl_local_space_free(ls);
794 return isl_aff_free(aff);
797 /* Look for any divs j that appear with a unit coefficient inside
798 * the definitions of other divs i and plug them into the definitions
801 * In particular, an expression of the form
803 * floor((f(..) + floor(g(..)/n))/m)
807 * floor((n * f(..) + g(..))/(n * m))
809 * This simplification is correct because we can move the expression
810 * f(..) into the inner floor in the original expression to obtain
812 * floor(floor((n * f(..) + g(..))/n)/m)
814 * from which we can derive the simplified expression.
816 static __isl_give isl_aff *plug_in_unit_divs(__isl_take isl_aff *aff)
824 n = isl_local_space_dim(aff->ls, isl_dim_div);
825 off = isl_local_space_offset(aff->ls, isl_dim_div);
826 for (i = 1; i < n; ++i) {
827 for (j = 0; j < i; ++j) {
828 if (!isl_int_is_one(aff->ls->div->row[i][1 + off + j]))
830 aff->ls = isl_local_space_substitute_seq(aff->ls,
831 isl_dim_div, j, aff->ls->div->row[j],
834 return isl_aff_free(aff);
841 /* Swap divs "a" and "b" in "aff", which is assumed to be non-NULL.
843 * Even though this function is only called on isl_affs with a single
844 * reference, we are careful to only change aff->v and aff->ls together.
846 static __isl_give isl_aff *swap_div(__isl_take isl_aff *aff, int a, int b)
848 unsigned off = isl_local_space_offset(aff->ls, isl_dim_div);
852 ls = isl_local_space_copy(aff->ls);
853 ls = isl_local_space_swap_div(ls, a, b);
854 v = isl_vec_copy(aff->v);
859 isl_int_swap(v->el[1 + off + a], v->el[1 + off + b]);
860 isl_vec_free(aff->v);
862 isl_local_space_free(aff->ls);
868 isl_local_space_free(ls);
869 return isl_aff_free(aff);
872 /* Merge divs "a" and "b" in "aff", which is assumed to be non-NULL.
874 * We currently do not actually remove div "b", but simply add its
875 * coefficient to that of "a" and then zero it out.
877 static __isl_give isl_aff *merge_divs(__isl_take isl_aff *aff, int a, int b)
879 unsigned off = isl_local_space_offset(aff->ls, isl_dim_div);
881 if (isl_int_is_zero(aff->v->el[1 + off + b]))
884 aff->v = isl_vec_cow(aff->v);
886 return isl_aff_free(aff);
888 isl_int_add(aff->v->el[1 + off + a],
889 aff->v->el[1 + off + a], aff->v->el[1 + off + b]);
890 isl_int_set_si(aff->v->el[1 + off + b], 0);
895 /* Sort the divs in the local space of "aff" according to
896 * the comparison function "cmp_row" in isl_local_space.c,
897 * combining the coefficients of identical divs.
899 * Reordering divs does not change the semantics of "aff",
900 * so there is no need to call isl_aff_cow.
901 * Moreover, this function is currently only called on isl_affs
902 * with a single reference.
904 static __isl_give isl_aff *sort_divs(__isl_take isl_aff *aff)
912 off = isl_local_space_offset(aff->ls, isl_dim_div);
913 n = isl_aff_dim(aff, isl_dim_div);
914 for (i = 1; i < n; ++i) {
915 for (j = i - 1; j >= 0; --j) {
916 int cmp = isl_mat_cmp_div(aff->ls->div, j, j + 1);
920 aff = merge_divs(aff, j, j + 1);
922 aff = swap_div(aff, j, j + 1);
931 /* Normalize the representation of "aff".
933 * This function should only be called of "new" isl_affs, i.e.,
934 * with only a single reference. We therefore do not need to
935 * worry about affecting other instances.
937 __isl_give isl_aff *isl_aff_normalize(__isl_take isl_aff *aff)
941 aff->v = isl_vec_normalize(aff->v);
943 return isl_aff_free(aff);
944 aff = plug_in_integral_divs(aff);
945 aff = plug_in_unit_divs(aff);
946 aff = sort_divs(aff);
947 aff = isl_aff_remove_unused_divs(aff);
951 /* Given f, return floor(f).
952 * If f is an integer expression, then just return f.
953 * If f is a constant, then return the constant floor(f).
954 * Otherwise, if f = g/m, write g = q m + r,
955 * create a new div d = [r/m] and return the expression q + d.
956 * The coefficients in r are taken to lie between -m/2 and m/2.
958 __isl_give isl_aff *isl_aff_floor(__isl_take isl_aff *aff)
968 if (isl_int_is_one(aff->v->el[0]))
971 aff = isl_aff_cow(aff);
975 aff->v = isl_vec_cow(aff->v);
977 return isl_aff_free(aff);
979 if (isl_aff_is_cst(aff)) {
980 isl_int_fdiv_q(aff->v->el[1], aff->v->el[1], aff->v->el[0]);
981 isl_int_set_si(aff->v->el[0], 1);
985 div = isl_vec_copy(aff->v);
986 div = isl_vec_cow(div);
988 return isl_aff_free(aff);
990 ctx = isl_aff_get_ctx(aff);
991 isl_int_fdiv_q(aff->v->el[0], aff->v->el[0], ctx->two);
992 for (i = 1; i < aff->v->size; ++i) {
993 isl_int_fdiv_r(div->el[i], div->el[i], div->el[0]);
994 isl_int_fdiv_q(aff->v->el[i], aff->v->el[i], div->el[0]);
995 if (isl_int_gt(div->el[i], aff->v->el[0])) {
996 isl_int_sub(div->el[i], div->el[i], div->el[0]);
997 isl_int_add_ui(aff->v->el[i], aff->v->el[i], 1);
1001 aff->ls = isl_local_space_add_div(aff->ls, div);
1003 return isl_aff_free(aff);
1005 size = aff->v->size;
1006 aff->v = isl_vec_extend(aff->v, size + 1);
1008 return isl_aff_free(aff);
1009 isl_int_set_si(aff->v->el[0], 1);
1010 isl_int_set_si(aff->v->el[size], 1);
1012 aff = isl_aff_normalize(aff);
1019 * aff mod m = aff - m * floor(aff/m)
1021 __isl_give isl_aff *isl_aff_mod(__isl_take isl_aff *aff, isl_int m)
1025 res = isl_aff_copy(aff);
1026 aff = isl_aff_scale_down(aff, m);
1027 aff = isl_aff_floor(aff);
1028 aff = isl_aff_scale(aff, m);
1029 res = isl_aff_sub(res, aff);
1036 * pwaff mod m = pwaff - m * floor(pwaff/m)
1038 __isl_give isl_pw_aff *isl_pw_aff_mod(__isl_take isl_pw_aff *pwaff, isl_int m)
1042 res = isl_pw_aff_copy(pwaff);
1043 pwaff = isl_pw_aff_scale_down(pwaff, m);
1044 pwaff = isl_pw_aff_floor(pwaff);
1045 pwaff = isl_pw_aff_scale(pwaff, m);
1046 res = isl_pw_aff_sub(res, pwaff);
1051 /* Given f, return ceil(f).
1052 * If f is an integer expression, then just return f.
1053 * Otherwise, let f be the expression
1059 * floor((e + m - 1)/m)
1061 __isl_give isl_aff *isl_aff_ceil(__isl_take isl_aff *aff)
1066 if (isl_int_is_one(aff->v->el[0]))
1069 aff = isl_aff_cow(aff);
1072 aff->v = isl_vec_cow(aff->v);
1074 return isl_aff_free(aff);
1076 isl_int_add(aff->v->el[1], aff->v->el[1], aff->v->el[0]);
1077 isl_int_sub_ui(aff->v->el[1], aff->v->el[1], 1);
1078 aff = isl_aff_floor(aff);
1083 /* Apply the expansion computed by isl_merge_divs.
1084 * The expansion itself is given by "exp" while the resulting
1085 * list of divs is given by "div".
1087 __isl_give isl_aff *isl_aff_expand_divs( __isl_take isl_aff *aff,
1088 __isl_take isl_mat *div, int *exp)
1095 aff = isl_aff_cow(aff);
1099 old_n_div = isl_local_space_dim(aff->ls, isl_dim_div);
1100 new_n_div = isl_mat_rows(div);
1101 if (new_n_div < old_n_div)
1102 isl_die(isl_mat_get_ctx(div), isl_error_invalid,
1103 "not an expansion", goto error);
1105 aff->v = isl_vec_extend(aff->v, aff->v->size + new_n_div - old_n_div);
1109 offset = 1 + isl_local_space_offset(aff->ls, isl_dim_div);
1111 for (i = new_n_div - 1; i >= 0; --i) {
1112 if (j >= 0 && exp[j] == i) {
1114 isl_int_swap(aff->v->el[offset + i],
1115 aff->v->el[offset + j]);
1118 isl_int_set_si(aff->v->el[offset + i], 0);
1121 aff->ls = isl_local_space_replace_divs(aff->ls, isl_mat_copy(div));
1132 /* Add two affine expressions that live in the same local space.
1134 static __isl_give isl_aff *add_expanded(__isl_take isl_aff *aff1,
1135 __isl_take isl_aff *aff2)
1139 aff1 = isl_aff_cow(aff1);
1143 aff1->v = isl_vec_cow(aff1->v);
1149 isl_int_gcd(gcd, aff1->v->el[0], aff2->v->el[0]);
1150 isl_int_divexact(f, aff2->v->el[0], gcd);
1151 isl_seq_scale(aff1->v->el + 1, aff1->v->el + 1, f, aff1->v->size - 1);
1152 isl_int_divexact(f, aff1->v->el[0], gcd);
1153 isl_seq_addmul(aff1->v->el + 1, f, aff2->v->el + 1, aff1->v->size - 1);
1154 isl_int_divexact(f, aff2->v->el[0], gcd);
1155 isl_int_mul(aff1->v->el[0], aff1->v->el[0], f);
1167 __isl_give isl_aff *isl_aff_add(__isl_take isl_aff *aff1,
1168 __isl_take isl_aff *aff2)
1178 ctx = isl_aff_get_ctx(aff1);
1179 if (!isl_space_is_equal(aff1->ls->dim, aff2->ls->dim))
1180 isl_die(ctx, isl_error_invalid,
1181 "spaces don't match", goto error);
1183 if (aff1->ls->div->n_row == 0 && aff2->ls->div->n_row == 0)
1184 return add_expanded(aff1, aff2);
1186 exp1 = isl_alloc_array(ctx, int, aff1->ls->div->n_row);
1187 exp2 = isl_alloc_array(ctx, int, aff2->ls->div->n_row);
1191 div = isl_merge_divs(aff1->ls->div, aff2->ls->div, exp1, exp2);
1192 aff1 = isl_aff_expand_divs(aff1, isl_mat_copy(div), exp1);
1193 aff2 = isl_aff_expand_divs(aff2, div, exp2);
1197 return add_expanded(aff1, aff2);
1206 __isl_give isl_aff *isl_aff_sub(__isl_take isl_aff *aff1,
1207 __isl_take isl_aff *aff2)
1209 return isl_aff_add(aff1, isl_aff_neg(aff2));
1212 __isl_give isl_aff *isl_aff_scale(__isl_take isl_aff *aff, isl_int f)
1216 if (isl_int_is_one(f))
1219 aff = isl_aff_cow(aff);
1222 aff->v = isl_vec_cow(aff->v);
1224 return isl_aff_free(aff);
1226 if (isl_int_is_pos(f) && isl_int_is_divisible_by(aff->v->el[0], f)) {
1227 isl_int_divexact(aff->v->el[0], aff->v->el[0], f);
1232 isl_int_gcd(gcd, aff->v->el[0], f);
1233 isl_int_divexact(aff->v->el[0], aff->v->el[0], gcd);
1234 isl_int_divexact(gcd, f, gcd);
1235 isl_seq_scale(aff->v->el + 1, aff->v->el + 1, gcd, aff->v->size - 1);
1241 __isl_give isl_aff *isl_aff_scale_down(__isl_take isl_aff *aff, isl_int f)
1245 if (isl_int_is_one(f))
1248 aff = isl_aff_cow(aff);
1252 if (isl_int_is_zero(f))
1253 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1254 "cannot scale down by zero", return isl_aff_free(aff));
1256 aff->v = isl_vec_cow(aff->v);
1258 return isl_aff_free(aff);
1261 isl_seq_gcd(aff->v->el + 1, aff->v->size - 1, &gcd);
1262 isl_int_gcd(gcd, gcd, f);
1263 isl_seq_scale_down(aff->v->el + 1, aff->v->el + 1, gcd, aff->v->size - 1);
1264 isl_int_divexact(gcd, f, gcd);
1265 isl_int_mul(aff->v->el[0], aff->v->el[0], gcd);
1271 __isl_give isl_aff *isl_aff_scale_down_ui(__isl_take isl_aff *aff, unsigned f)
1279 isl_int_set_ui(v, f);
1280 aff = isl_aff_scale_down(aff, v);
1286 __isl_give isl_aff *isl_aff_set_dim_name(__isl_take isl_aff *aff,
1287 enum isl_dim_type type, unsigned pos, const char *s)
1289 aff = isl_aff_cow(aff);
1292 if (type == isl_dim_out)
1293 isl_die(aff->v->ctx, isl_error_invalid,
1294 "cannot set name of output/set dimension",
1295 return isl_aff_free(aff));
1296 if (type == isl_dim_in)
1298 aff->ls = isl_local_space_set_dim_name(aff->ls, type, pos, s);
1300 return isl_aff_free(aff);
1305 __isl_give isl_aff *isl_aff_set_dim_id(__isl_take isl_aff *aff,
1306 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
1308 aff = isl_aff_cow(aff);
1310 return isl_id_free(id);
1311 if (type == isl_dim_out)
1312 isl_die(aff->v->ctx, isl_error_invalid,
1313 "cannot set name of output/set dimension",
1315 if (type == isl_dim_in)
1317 aff->ls = isl_local_space_set_dim_id(aff->ls, type, pos, id);
1319 return isl_aff_free(aff);
1328 /* Exploit the equalities in "eq" to simplify the affine expression
1329 * and the expressions of the integer divisions in the local space.
1330 * The integer divisions in this local space are assumed to appear
1331 * as regular dimensions in "eq".
1333 static __isl_give isl_aff *isl_aff_substitute_equalities_lifted(
1334 __isl_take isl_aff *aff, __isl_take isl_basic_set *eq)
1342 if (eq->n_eq == 0) {
1343 isl_basic_set_free(eq);
1347 aff = isl_aff_cow(aff);
1351 aff->ls = isl_local_space_substitute_equalities(aff->ls,
1352 isl_basic_set_copy(eq));
1353 aff->v = isl_vec_cow(aff->v);
1354 if (!aff->ls || !aff->v)
1357 total = 1 + isl_space_dim(eq->dim, isl_dim_all);
1359 for (i = 0; i < eq->n_eq; ++i) {
1360 j = isl_seq_last_non_zero(eq->eq[i], total + n_div);
1361 if (j < 0 || j == 0 || j >= total)
1364 isl_seq_elim(aff->v->el + 1, eq->eq[i], j, total,
1368 isl_basic_set_free(eq);
1369 aff = isl_aff_normalize(aff);
1372 isl_basic_set_free(eq);
1377 /* Exploit the equalities in "eq" to simplify the affine expression
1378 * and the expressions of the integer divisions in the local space.
1380 static __isl_give isl_aff *isl_aff_substitute_equalities(
1381 __isl_take isl_aff *aff, __isl_take isl_basic_set *eq)
1387 n_div = isl_local_space_dim(aff->ls, isl_dim_div);
1389 eq = isl_basic_set_add_dims(eq, isl_dim_set, n_div);
1390 return isl_aff_substitute_equalities_lifted(aff, eq);
1392 isl_basic_set_free(eq);
1397 /* Look for equalities among the variables shared by context and aff
1398 * and the integer divisions of aff, if any.
1399 * The equalities are then used to eliminate coefficients and/or integer
1400 * divisions from aff.
1402 __isl_give isl_aff *isl_aff_gist(__isl_take isl_aff *aff,
1403 __isl_take isl_set *context)
1405 isl_basic_set *hull;
1410 n_div = isl_local_space_dim(aff->ls, isl_dim_div);
1412 isl_basic_set *bset;
1413 isl_local_space *ls;
1414 context = isl_set_add_dims(context, isl_dim_set, n_div);
1415 ls = isl_aff_get_domain_local_space(aff);
1416 bset = isl_basic_set_from_local_space(ls);
1417 bset = isl_basic_set_lift(bset);
1418 bset = isl_basic_set_flatten(bset);
1419 context = isl_set_intersect(context,
1420 isl_set_from_basic_set(bset));
1423 hull = isl_set_affine_hull(context);
1424 return isl_aff_substitute_equalities_lifted(aff, hull);
1427 isl_set_free(context);
1431 __isl_give isl_aff *isl_aff_gist_params(__isl_take isl_aff *aff,
1432 __isl_take isl_set *context)
1434 isl_set *dom_context = isl_set_universe(isl_aff_get_domain_space(aff));
1435 dom_context = isl_set_intersect_params(dom_context, context);
1436 return isl_aff_gist(aff, dom_context);
1439 /* Return a basic set containing those elements in the space
1440 * of aff where it is non-negative.
1441 * If "rational" is set, then return a rational basic set.
1443 static __isl_give isl_basic_set *aff_nonneg_basic_set(
1444 __isl_take isl_aff *aff, int rational)
1446 isl_constraint *ineq;
1447 isl_basic_set *bset;
1449 ineq = isl_inequality_from_aff(aff);
1451 bset = isl_basic_set_from_constraint(ineq);
1453 bset = isl_basic_set_set_rational(bset);
1454 bset = isl_basic_set_simplify(bset);
1458 /* Return a basic set containing those elements in the space
1459 * of aff where it is non-negative.
1461 __isl_give isl_basic_set *isl_aff_nonneg_basic_set(__isl_take isl_aff *aff)
1463 return aff_nonneg_basic_set(aff, 0);
1466 /* Return a basic set containing those elements in the domain space
1467 * of aff where it is negative.
1469 __isl_give isl_basic_set *isl_aff_neg_basic_set(__isl_take isl_aff *aff)
1471 aff = isl_aff_neg(aff);
1472 aff = isl_aff_add_constant_num_si(aff, -1);
1473 return isl_aff_nonneg_basic_set(aff);
1476 /* Return a basic set containing those elements in the space
1477 * of aff where it is zero.
1478 * If "rational" is set, then return a rational basic set.
1480 static __isl_give isl_basic_set *aff_zero_basic_set(__isl_take isl_aff *aff,
1483 isl_constraint *ineq;
1484 isl_basic_set *bset;
1486 ineq = isl_equality_from_aff(aff);
1488 bset = isl_basic_set_from_constraint(ineq);
1490 bset = isl_basic_set_set_rational(bset);
1491 bset = isl_basic_set_simplify(bset);
1495 /* Return a basic set containing those elements in the space
1496 * of aff where it is zero.
1498 __isl_give isl_basic_set *isl_aff_zero_basic_set(__isl_take isl_aff *aff)
1500 return aff_zero_basic_set(aff, 0);
1503 /* Return a basic set containing those elements in the shared space
1504 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
1506 __isl_give isl_basic_set *isl_aff_ge_basic_set(__isl_take isl_aff *aff1,
1507 __isl_take isl_aff *aff2)
1509 aff1 = isl_aff_sub(aff1, aff2);
1511 return isl_aff_nonneg_basic_set(aff1);
1514 /* Return a basic set containing those elements in the shared space
1515 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
1517 __isl_give isl_basic_set *isl_aff_le_basic_set(__isl_take isl_aff *aff1,
1518 __isl_take isl_aff *aff2)
1520 return isl_aff_ge_basic_set(aff2, aff1);
1523 __isl_give isl_aff *isl_aff_add_on_domain(__isl_keep isl_set *dom,
1524 __isl_take isl_aff *aff1, __isl_take isl_aff *aff2)
1526 aff1 = isl_aff_add(aff1, aff2);
1527 aff1 = isl_aff_gist(aff1, isl_set_copy(dom));
1531 int isl_aff_is_empty(__isl_keep isl_aff *aff)
1539 /* Check whether the given affine expression has non-zero coefficient
1540 * for any dimension in the given range or if any of these dimensions
1541 * appear with non-zero coefficients in any of the integer divisions
1542 * involved in the affine expression.
1544 int isl_aff_involves_dims(__isl_keep isl_aff *aff,
1545 enum isl_dim_type type, unsigned first, unsigned n)
1557 ctx = isl_aff_get_ctx(aff);
1558 if (first + n > isl_aff_dim(aff, type))
1559 isl_die(ctx, isl_error_invalid,
1560 "range out of bounds", return -1);
1562 active = isl_local_space_get_active(aff->ls, aff->v->el + 2);
1566 first += isl_local_space_offset(aff->ls, type) - 1;
1567 for (i = 0; i < n; ++i)
1568 if (active[first + i]) {
1581 __isl_give isl_aff *isl_aff_drop_dims(__isl_take isl_aff *aff,
1582 enum isl_dim_type type, unsigned first, unsigned n)
1588 if (type == isl_dim_out)
1589 isl_die(aff->v->ctx, isl_error_invalid,
1590 "cannot drop output/set dimension",
1591 return isl_aff_free(aff));
1592 if (type == isl_dim_in)
1594 if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
1597 ctx = isl_aff_get_ctx(aff);
1598 if (first + n > isl_local_space_dim(aff->ls, type))
1599 isl_die(ctx, isl_error_invalid, "range out of bounds",
1600 return isl_aff_free(aff));
1602 aff = isl_aff_cow(aff);
1606 aff->ls = isl_local_space_drop_dims(aff->ls, type, first, n);
1608 return isl_aff_free(aff);
1610 first += 1 + isl_local_space_offset(aff->ls, type);
1611 aff->v = isl_vec_drop_els(aff->v, first, n);
1613 return isl_aff_free(aff);
1618 /* Project the domain of the affine expression onto its parameter space.
1619 * The affine expression may not involve any of the domain dimensions.
1621 __isl_give isl_aff *isl_aff_project_domain_on_params(__isl_take isl_aff *aff)
1627 n = isl_aff_dim(aff, isl_dim_in);
1628 involves = isl_aff_involves_dims(aff, isl_dim_in, 0, n);
1630 return isl_aff_free(aff);
1632 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1633 "affine expression involves some of the domain dimensions",
1634 return isl_aff_free(aff));
1635 aff = isl_aff_drop_dims(aff, isl_dim_in, 0, n);
1636 space = isl_aff_get_domain_space(aff);
1637 space = isl_space_params(space);
1638 aff = isl_aff_reset_domain_space(aff, space);
1642 __isl_give isl_aff *isl_aff_insert_dims(__isl_take isl_aff *aff,
1643 enum isl_dim_type type, unsigned first, unsigned n)
1649 if (type == isl_dim_out)
1650 isl_die(aff->v->ctx, isl_error_invalid,
1651 "cannot insert output/set dimensions",
1652 return isl_aff_free(aff));
1653 if (type == isl_dim_in)
1655 if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
1658 ctx = isl_aff_get_ctx(aff);
1659 if (first > isl_local_space_dim(aff->ls, type))
1660 isl_die(ctx, isl_error_invalid, "position out of bounds",
1661 return isl_aff_free(aff));
1663 aff = isl_aff_cow(aff);
1667 aff->ls = isl_local_space_insert_dims(aff->ls, type, first, n);
1669 return isl_aff_free(aff);
1671 first += 1 + isl_local_space_offset(aff->ls, type);
1672 aff->v = isl_vec_insert_zero_els(aff->v, first, n);
1674 return isl_aff_free(aff);
1679 __isl_give isl_aff *isl_aff_add_dims(__isl_take isl_aff *aff,
1680 enum isl_dim_type type, unsigned n)
1684 pos = isl_aff_dim(aff, type);
1686 return isl_aff_insert_dims(aff, type, pos, n);
1689 __isl_give isl_pw_aff *isl_pw_aff_add_dims(__isl_take isl_pw_aff *pwaff,
1690 enum isl_dim_type type, unsigned n)
1694 pos = isl_pw_aff_dim(pwaff, type);
1696 return isl_pw_aff_insert_dims(pwaff, type, pos, n);
1699 __isl_give isl_pw_aff *isl_pw_aff_from_aff(__isl_take isl_aff *aff)
1701 isl_set *dom = isl_set_universe(isl_aff_get_domain_space(aff));
1702 return isl_pw_aff_alloc(dom, aff);
1706 #define PW isl_pw_aff
1710 #define EL_IS_ZERO is_empty
1714 #define IS_ZERO is_empty
1717 #undef DEFAULT_IS_ZERO
1718 #define DEFAULT_IS_ZERO 0
1722 #define NO_MOVE_DIMS
1726 #include <isl_pw_templ.c>
1728 static __isl_give isl_set *align_params_pw_pw_set_and(
1729 __isl_take isl_pw_aff *pwaff1, __isl_take isl_pw_aff *pwaff2,
1730 __isl_give isl_set *(*fn)(__isl_take isl_pw_aff *pwaff1,
1731 __isl_take isl_pw_aff *pwaff2))
1733 if (!pwaff1 || !pwaff2)
1735 if (isl_space_match(pwaff1->dim, isl_dim_param,
1736 pwaff2->dim, isl_dim_param))
1737 return fn(pwaff1, pwaff2);
1738 if (!isl_space_has_named_params(pwaff1->dim) ||
1739 !isl_space_has_named_params(pwaff2->dim))
1740 isl_die(isl_pw_aff_get_ctx(pwaff1), isl_error_invalid,
1741 "unaligned unnamed parameters", goto error);
1742 pwaff1 = isl_pw_aff_align_params(pwaff1, isl_pw_aff_get_space(pwaff2));
1743 pwaff2 = isl_pw_aff_align_params(pwaff2, isl_pw_aff_get_space(pwaff1));
1744 return fn(pwaff1, pwaff2);
1746 isl_pw_aff_free(pwaff1);
1747 isl_pw_aff_free(pwaff2);
1751 /* Compute a piecewise quasi-affine expression with a domain that
1752 * is the union of those of pwaff1 and pwaff2 and such that on each
1753 * cell, the quasi-affine expression is the better (according to cmp)
1754 * of those of pwaff1 and pwaff2. If only one of pwaff1 or pwaff2
1755 * is defined on a given cell, then the associated expression
1756 * is the defined one.
1758 static __isl_give isl_pw_aff *pw_aff_union_opt(__isl_take isl_pw_aff *pwaff1,
1759 __isl_take isl_pw_aff *pwaff2,
1760 __isl_give isl_basic_set *(*cmp)(__isl_take isl_aff *aff1,
1761 __isl_take isl_aff *aff2))
1768 if (!pwaff1 || !pwaff2)
1771 ctx = isl_space_get_ctx(pwaff1->dim);
1772 if (!isl_space_is_equal(pwaff1->dim, pwaff2->dim))
1773 isl_die(ctx, isl_error_invalid,
1774 "arguments should live in same space", goto error);
1776 if (isl_pw_aff_is_empty(pwaff1)) {
1777 isl_pw_aff_free(pwaff1);
1781 if (isl_pw_aff_is_empty(pwaff2)) {
1782 isl_pw_aff_free(pwaff2);
1786 n = 2 * (pwaff1->n + 1) * (pwaff2->n + 1);
1787 res = isl_pw_aff_alloc_size(isl_space_copy(pwaff1->dim), n);
1789 for (i = 0; i < pwaff1->n; ++i) {
1790 set = isl_set_copy(pwaff1->p[i].set);
1791 for (j = 0; j < pwaff2->n; ++j) {
1792 struct isl_set *common;
1795 common = isl_set_intersect(
1796 isl_set_copy(pwaff1->p[i].set),
1797 isl_set_copy(pwaff2->p[j].set));
1798 better = isl_set_from_basic_set(cmp(
1799 isl_aff_copy(pwaff2->p[j].aff),
1800 isl_aff_copy(pwaff1->p[i].aff)));
1801 better = isl_set_intersect(common, better);
1802 if (isl_set_plain_is_empty(better)) {
1803 isl_set_free(better);
1806 set = isl_set_subtract(set, isl_set_copy(better));
1808 res = isl_pw_aff_add_piece(res, better,
1809 isl_aff_copy(pwaff2->p[j].aff));
1811 res = isl_pw_aff_add_piece(res, set,
1812 isl_aff_copy(pwaff1->p[i].aff));
1815 for (j = 0; j < pwaff2->n; ++j) {
1816 set = isl_set_copy(pwaff2->p[j].set);
1817 for (i = 0; i < pwaff1->n; ++i)
1818 set = isl_set_subtract(set,
1819 isl_set_copy(pwaff1->p[i].set));
1820 res = isl_pw_aff_add_piece(res, set,
1821 isl_aff_copy(pwaff2->p[j].aff));
1824 isl_pw_aff_free(pwaff1);
1825 isl_pw_aff_free(pwaff2);
1829 isl_pw_aff_free(pwaff1);
1830 isl_pw_aff_free(pwaff2);
1834 /* Compute a piecewise quasi-affine expression with a domain that
1835 * is the union of those of pwaff1 and pwaff2 and such that on each
1836 * cell, the quasi-affine expression is the maximum of those of pwaff1
1837 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
1838 * cell, then the associated expression is the defined one.
1840 static __isl_give isl_pw_aff *pw_aff_union_max(__isl_take isl_pw_aff *pwaff1,
1841 __isl_take isl_pw_aff *pwaff2)
1843 return pw_aff_union_opt(pwaff1, pwaff2, &isl_aff_ge_basic_set);
1846 __isl_give isl_pw_aff *isl_pw_aff_union_max(__isl_take isl_pw_aff *pwaff1,
1847 __isl_take isl_pw_aff *pwaff2)
1849 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2,
1853 /* Compute a piecewise quasi-affine expression with a domain that
1854 * is the union of those of pwaff1 and pwaff2 and such that on each
1855 * cell, the quasi-affine expression is the minimum of those of pwaff1
1856 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
1857 * cell, then the associated expression is the defined one.
1859 static __isl_give isl_pw_aff *pw_aff_union_min(__isl_take isl_pw_aff *pwaff1,
1860 __isl_take isl_pw_aff *pwaff2)
1862 return pw_aff_union_opt(pwaff1, pwaff2, &isl_aff_le_basic_set);
1865 __isl_give isl_pw_aff *isl_pw_aff_union_min(__isl_take isl_pw_aff *pwaff1,
1866 __isl_take isl_pw_aff *pwaff2)
1868 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2,
1872 __isl_give isl_pw_aff *isl_pw_aff_union_opt(__isl_take isl_pw_aff *pwaff1,
1873 __isl_take isl_pw_aff *pwaff2, int max)
1876 return isl_pw_aff_union_max(pwaff1, pwaff2);
1878 return isl_pw_aff_union_min(pwaff1, pwaff2);
1881 /* Construct a map with as domain the domain of pwaff and
1882 * one-dimensional range corresponding to the affine expressions.
1884 static __isl_give isl_map *map_from_pw_aff(__isl_take isl_pw_aff *pwaff)
1893 dim = isl_pw_aff_get_space(pwaff);
1894 map = isl_map_empty(dim);
1896 for (i = 0; i < pwaff->n; ++i) {
1897 isl_basic_map *bmap;
1900 bmap = isl_basic_map_from_aff(isl_aff_copy(pwaff->p[i].aff));
1901 map_i = isl_map_from_basic_map(bmap);
1902 map_i = isl_map_intersect_domain(map_i,
1903 isl_set_copy(pwaff->p[i].set));
1904 map = isl_map_union_disjoint(map, map_i);
1907 isl_pw_aff_free(pwaff);
1912 /* Construct a map with as domain the domain of pwaff and
1913 * one-dimensional range corresponding to the affine expressions.
1915 __isl_give isl_map *isl_map_from_pw_aff(__isl_take isl_pw_aff *pwaff)
1919 if (isl_space_is_set(pwaff->dim))
1920 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
1921 "space of input is not a map",
1922 return isl_pw_aff_free(pwaff));
1923 return map_from_pw_aff(pwaff);
1926 /* Construct a one-dimensional set with as parameter domain
1927 * the domain of pwaff and the single set dimension
1928 * corresponding to the affine expressions.
1930 __isl_give isl_set *isl_set_from_pw_aff(__isl_take isl_pw_aff *pwaff)
1934 if (!isl_space_is_set(pwaff->dim))
1935 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
1936 "space of input is not a set",
1937 return isl_pw_aff_free(pwaff));
1938 return map_from_pw_aff(pwaff);
1941 /* Return a set containing those elements in the domain
1942 * of pwaff where it is non-negative.
1944 __isl_give isl_set *isl_pw_aff_nonneg_set(__isl_take isl_pw_aff *pwaff)
1952 set = isl_set_empty(isl_pw_aff_get_domain_space(pwaff));
1954 for (i = 0; i < pwaff->n; ++i) {
1955 isl_basic_set *bset;
1959 rational = isl_set_has_rational(pwaff->p[i].set);
1960 bset = aff_nonneg_basic_set(isl_aff_copy(pwaff->p[i].aff),
1962 set_i = isl_set_from_basic_set(bset);
1963 set_i = isl_set_intersect(set_i, isl_set_copy(pwaff->p[i].set));
1964 set = isl_set_union_disjoint(set, set_i);
1967 isl_pw_aff_free(pwaff);
1972 /* Return a set containing those elements in the domain
1973 * of pwaff where it is zero (if complement is 0) or not zero
1974 * (if complement is 1).
1976 static __isl_give isl_set *pw_aff_zero_set(__isl_take isl_pw_aff *pwaff,
1985 set = isl_set_empty(isl_pw_aff_get_domain_space(pwaff));
1987 for (i = 0; i < pwaff->n; ++i) {
1988 isl_basic_set *bset;
1989 isl_set *set_i, *zero;
1992 rational = isl_set_has_rational(pwaff->p[i].set);
1993 bset = aff_zero_basic_set(isl_aff_copy(pwaff->p[i].aff),
1995 zero = isl_set_from_basic_set(bset);
1996 set_i = isl_set_copy(pwaff->p[i].set);
1998 set_i = isl_set_subtract(set_i, zero);
2000 set_i = isl_set_intersect(set_i, zero);
2001 set = isl_set_union_disjoint(set, set_i);
2004 isl_pw_aff_free(pwaff);
2009 /* Return a set containing those elements in the domain
2010 * of pwaff where it is zero.
2012 __isl_give isl_set *isl_pw_aff_zero_set(__isl_take isl_pw_aff *pwaff)
2014 return pw_aff_zero_set(pwaff, 0);
2017 /* Return a set containing those elements in the domain
2018 * of pwaff where it is not zero.
2020 __isl_give isl_set *isl_pw_aff_non_zero_set(__isl_take isl_pw_aff *pwaff)
2022 return pw_aff_zero_set(pwaff, 1);
2025 /* Return a set containing those elements in the shared domain
2026 * of pwaff1 and pwaff2 where pwaff1 is greater than (or equal) to pwaff2.
2028 * We compute the difference on the shared domain and then construct
2029 * the set of values where this difference is non-negative.
2030 * If strict is set, we first subtract 1 from the difference.
2031 * If equal is set, we only return the elements where pwaff1 and pwaff2
2034 static __isl_give isl_set *pw_aff_gte_set(__isl_take isl_pw_aff *pwaff1,
2035 __isl_take isl_pw_aff *pwaff2, int strict, int equal)
2037 isl_set *set1, *set2;
2039 set1 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff1));
2040 set2 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff2));
2041 set1 = isl_set_intersect(set1, set2);
2042 pwaff1 = isl_pw_aff_intersect_domain(pwaff1, isl_set_copy(set1));
2043 pwaff2 = isl_pw_aff_intersect_domain(pwaff2, isl_set_copy(set1));
2044 pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_neg(pwaff2));
2047 isl_space *dim = isl_set_get_space(set1);
2049 aff = isl_aff_zero_on_domain(isl_local_space_from_space(dim));
2050 aff = isl_aff_add_constant_si(aff, -1);
2051 pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_alloc(set1, aff));
2056 return isl_pw_aff_zero_set(pwaff1);
2057 return isl_pw_aff_nonneg_set(pwaff1);
2060 /* Return a set containing those elements in the shared domain
2061 * of pwaff1 and pwaff2 where pwaff1 is equal to pwaff2.
2063 static __isl_give isl_set *pw_aff_eq_set(__isl_take isl_pw_aff *pwaff1,
2064 __isl_take isl_pw_aff *pwaff2)
2066 return pw_aff_gte_set(pwaff1, pwaff2, 0, 1);
2069 __isl_give isl_set *isl_pw_aff_eq_set(__isl_take isl_pw_aff *pwaff1,
2070 __isl_take isl_pw_aff *pwaff2)
2072 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_eq_set);
2075 /* Return a set containing those elements in the shared domain
2076 * of pwaff1 and pwaff2 where pwaff1 is greater than or equal to pwaff2.
2078 static __isl_give isl_set *pw_aff_ge_set(__isl_take isl_pw_aff *pwaff1,
2079 __isl_take isl_pw_aff *pwaff2)
2081 return pw_aff_gte_set(pwaff1, pwaff2, 0, 0);
2084 __isl_give isl_set *isl_pw_aff_ge_set(__isl_take isl_pw_aff *pwaff1,
2085 __isl_take isl_pw_aff *pwaff2)
2087 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_ge_set);
2090 /* Return a set containing those elements in the shared domain
2091 * of pwaff1 and pwaff2 where pwaff1 is strictly greater than pwaff2.
2093 static __isl_give isl_set *pw_aff_gt_set(__isl_take isl_pw_aff *pwaff1,
2094 __isl_take isl_pw_aff *pwaff2)
2096 return pw_aff_gte_set(pwaff1, pwaff2, 1, 0);
2099 __isl_give isl_set *isl_pw_aff_gt_set(__isl_take isl_pw_aff *pwaff1,
2100 __isl_take isl_pw_aff *pwaff2)
2102 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_gt_set);
2105 __isl_give isl_set *isl_pw_aff_le_set(__isl_take isl_pw_aff *pwaff1,
2106 __isl_take isl_pw_aff *pwaff2)
2108 return isl_pw_aff_ge_set(pwaff2, pwaff1);
2111 __isl_give isl_set *isl_pw_aff_lt_set(__isl_take isl_pw_aff *pwaff1,
2112 __isl_take isl_pw_aff *pwaff2)
2114 return isl_pw_aff_gt_set(pwaff2, pwaff1);
2117 /* Return a set containing those elements in the shared domain
2118 * of the elements of list1 and list2 where each element in list1
2119 * has the relation specified by "fn" with each element in list2.
2121 static __isl_give isl_set *pw_aff_list_set(__isl_take isl_pw_aff_list *list1,
2122 __isl_take isl_pw_aff_list *list2,
2123 __isl_give isl_set *(*fn)(__isl_take isl_pw_aff *pwaff1,
2124 __isl_take isl_pw_aff *pwaff2))
2130 if (!list1 || !list2)
2133 ctx = isl_pw_aff_list_get_ctx(list1);
2134 if (list1->n < 1 || list2->n < 1)
2135 isl_die(ctx, isl_error_invalid,
2136 "list should contain at least one element", goto error);
2138 set = isl_set_universe(isl_pw_aff_get_domain_space(list1->p[0]));
2139 for (i = 0; i < list1->n; ++i)
2140 for (j = 0; j < list2->n; ++j) {
2143 set_ij = fn(isl_pw_aff_copy(list1->p[i]),
2144 isl_pw_aff_copy(list2->p[j]));
2145 set = isl_set_intersect(set, set_ij);
2148 isl_pw_aff_list_free(list1);
2149 isl_pw_aff_list_free(list2);
2152 isl_pw_aff_list_free(list1);
2153 isl_pw_aff_list_free(list2);
2157 /* Return a set containing those elements in the shared domain
2158 * of the elements of list1 and list2 where each element in list1
2159 * is equal to each element in list2.
2161 __isl_give isl_set *isl_pw_aff_list_eq_set(__isl_take isl_pw_aff_list *list1,
2162 __isl_take isl_pw_aff_list *list2)
2164 return pw_aff_list_set(list1, list2, &isl_pw_aff_eq_set);
2167 __isl_give isl_set *isl_pw_aff_list_ne_set(__isl_take isl_pw_aff_list *list1,
2168 __isl_take isl_pw_aff_list *list2)
2170 return pw_aff_list_set(list1, list2, &isl_pw_aff_ne_set);
2173 /* Return a set containing those elements in the shared domain
2174 * of the elements of list1 and list2 where each element in list1
2175 * is less than or equal to each element in list2.
2177 __isl_give isl_set *isl_pw_aff_list_le_set(__isl_take isl_pw_aff_list *list1,
2178 __isl_take isl_pw_aff_list *list2)
2180 return pw_aff_list_set(list1, list2, &isl_pw_aff_le_set);
2183 __isl_give isl_set *isl_pw_aff_list_lt_set(__isl_take isl_pw_aff_list *list1,
2184 __isl_take isl_pw_aff_list *list2)
2186 return pw_aff_list_set(list1, list2, &isl_pw_aff_lt_set);
2189 __isl_give isl_set *isl_pw_aff_list_ge_set(__isl_take isl_pw_aff_list *list1,
2190 __isl_take isl_pw_aff_list *list2)
2192 return pw_aff_list_set(list1, list2, &isl_pw_aff_ge_set);
2195 __isl_give isl_set *isl_pw_aff_list_gt_set(__isl_take isl_pw_aff_list *list1,
2196 __isl_take isl_pw_aff_list *list2)
2198 return pw_aff_list_set(list1, list2, &isl_pw_aff_gt_set);
2202 /* Return a set containing those elements in the shared domain
2203 * of pwaff1 and pwaff2 where pwaff1 is not equal to pwaff2.
2205 static __isl_give isl_set *pw_aff_ne_set(__isl_take isl_pw_aff *pwaff1,
2206 __isl_take isl_pw_aff *pwaff2)
2208 isl_set *set_lt, *set_gt;
2210 set_lt = isl_pw_aff_lt_set(isl_pw_aff_copy(pwaff1),
2211 isl_pw_aff_copy(pwaff2));
2212 set_gt = isl_pw_aff_gt_set(pwaff1, pwaff2);
2213 return isl_set_union_disjoint(set_lt, set_gt);
2216 __isl_give isl_set *isl_pw_aff_ne_set(__isl_take isl_pw_aff *pwaff1,
2217 __isl_take isl_pw_aff *pwaff2)
2219 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_ne_set);
2222 __isl_give isl_pw_aff *isl_pw_aff_scale_down(__isl_take isl_pw_aff *pwaff,
2227 if (isl_int_is_one(v))
2229 if (!isl_int_is_pos(v))
2230 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
2231 "factor needs to be positive",
2232 return isl_pw_aff_free(pwaff));
2233 pwaff = isl_pw_aff_cow(pwaff);
2239 for (i = 0; i < pwaff->n; ++i) {
2240 pwaff->p[i].aff = isl_aff_scale_down(pwaff->p[i].aff, v);
2241 if (!pwaff->p[i].aff)
2242 return isl_pw_aff_free(pwaff);
2248 __isl_give isl_pw_aff *isl_pw_aff_floor(__isl_take isl_pw_aff *pwaff)
2252 pwaff = isl_pw_aff_cow(pwaff);
2258 for (i = 0; i < pwaff->n; ++i) {
2259 pwaff->p[i].aff = isl_aff_floor(pwaff->p[i].aff);
2260 if (!pwaff->p[i].aff)
2261 return isl_pw_aff_free(pwaff);
2267 __isl_give isl_pw_aff *isl_pw_aff_ceil(__isl_take isl_pw_aff *pwaff)
2271 pwaff = isl_pw_aff_cow(pwaff);
2277 for (i = 0; i < pwaff->n; ++i) {
2278 pwaff->p[i].aff = isl_aff_ceil(pwaff->p[i].aff);
2279 if (!pwaff->p[i].aff)
2280 return isl_pw_aff_free(pwaff);
2286 /* Assuming that "cond1" and "cond2" are disjoint,
2287 * return an affine expression that is equal to pwaff1 on cond1
2288 * and to pwaff2 on cond2.
2290 static __isl_give isl_pw_aff *isl_pw_aff_select(
2291 __isl_take isl_set *cond1, __isl_take isl_pw_aff *pwaff1,
2292 __isl_take isl_set *cond2, __isl_take isl_pw_aff *pwaff2)
2294 pwaff1 = isl_pw_aff_intersect_domain(pwaff1, cond1);
2295 pwaff2 = isl_pw_aff_intersect_domain(pwaff2, cond2);
2297 return isl_pw_aff_add_disjoint(pwaff1, pwaff2);
2300 /* Return an affine expression that is equal to pwaff_true for elements
2301 * where "cond" is non-zero and to pwaff_false for elements where "cond"
2303 * That is, return cond ? pwaff_true : pwaff_false;
2305 __isl_give isl_pw_aff *isl_pw_aff_cond(__isl_take isl_pw_aff *cond,
2306 __isl_take isl_pw_aff *pwaff_true, __isl_take isl_pw_aff *pwaff_false)
2308 isl_set *cond_true, *cond_false;
2310 cond_true = isl_pw_aff_non_zero_set(isl_pw_aff_copy(cond));
2311 cond_false = isl_pw_aff_zero_set(cond);
2312 return isl_pw_aff_select(cond_true, pwaff_true,
2313 cond_false, pwaff_false);
2316 int isl_aff_is_cst(__isl_keep isl_aff *aff)
2321 return isl_seq_first_non_zero(aff->v->el + 2, aff->v->size - 2) == -1;
2324 /* Check whether pwaff is a piecewise constant.
2326 int isl_pw_aff_is_cst(__isl_keep isl_pw_aff *pwaff)
2333 for (i = 0; i < pwaff->n; ++i) {
2334 int is_cst = isl_aff_is_cst(pwaff->p[i].aff);
2335 if (is_cst < 0 || !is_cst)
2342 __isl_give isl_aff *isl_aff_mul(__isl_take isl_aff *aff1,
2343 __isl_take isl_aff *aff2)
2345 if (!isl_aff_is_cst(aff2) && isl_aff_is_cst(aff1))
2346 return isl_aff_mul(aff2, aff1);
2348 if (!isl_aff_is_cst(aff2))
2349 isl_die(isl_aff_get_ctx(aff1), isl_error_invalid,
2350 "at least one affine expression should be constant",
2353 aff1 = isl_aff_cow(aff1);
2357 aff1 = isl_aff_scale(aff1, aff2->v->el[1]);
2358 aff1 = isl_aff_scale_down(aff1, aff2->v->el[0]);
2368 /* Divide "aff1" by "aff2", assuming "aff2" is a piecewise constant.
2370 __isl_give isl_aff *isl_aff_div(__isl_take isl_aff *aff1,
2371 __isl_take isl_aff *aff2)
2376 is_cst = isl_aff_is_cst(aff2);
2380 isl_die(isl_aff_get_ctx(aff2), isl_error_invalid,
2381 "second argument should be a constant", goto error);
2386 neg = isl_int_is_neg(aff2->v->el[1]);
2388 isl_int_neg(aff2->v->el[0], aff2->v->el[0]);
2389 isl_int_neg(aff2->v->el[1], aff2->v->el[1]);
2392 aff1 = isl_aff_scale(aff1, aff2->v->el[0]);
2393 aff1 = isl_aff_scale_down(aff1, aff2->v->el[1]);
2396 isl_int_neg(aff2->v->el[0], aff2->v->el[0]);
2397 isl_int_neg(aff2->v->el[1], aff2->v->el[1]);
2408 static __isl_give isl_pw_aff *pw_aff_add(__isl_take isl_pw_aff *pwaff1,
2409 __isl_take isl_pw_aff *pwaff2)
2411 return isl_pw_aff_on_shared_domain(pwaff1, pwaff2, &isl_aff_add);
2414 __isl_give isl_pw_aff *isl_pw_aff_add(__isl_take isl_pw_aff *pwaff1,
2415 __isl_take isl_pw_aff *pwaff2)
2417 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_add);
2420 __isl_give isl_pw_aff *isl_pw_aff_union_add(__isl_take isl_pw_aff *pwaff1,
2421 __isl_take isl_pw_aff *pwaff2)
2423 return isl_pw_aff_union_add_(pwaff1, pwaff2);
2426 static __isl_give isl_pw_aff *pw_aff_mul(__isl_take isl_pw_aff *pwaff1,
2427 __isl_take isl_pw_aff *pwaff2)
2429 return isl_pw_aff_on_shared_domain(pwaff1, pwaff2, &isl_aff_mul);
2432 __isl_give isl_pw_aff *isl_pw_aff_mul(__isl_take isl_pw_aff *pwaff1,
2433 __isl_take isl_pw_aff *pwaff2)
2435 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_mul);
2438 static __isl_give isl_pw_aff *pw_aff_div(__isl_take isl_pw_aff *pa1,
2439 __isl_take isl_pw_aff *pa2)
2441 return isl_pw_aff_on_shared_domain(pa1, pa2, &isl_aff_div);
2444 /* Divide "pa1" by "pa2", assuming "pa2" is a piecewise constant.
2446 __isl_give isl_pw_aff *isl_pw_aff_div(__isl_take isl_pw_aff *pa1,
2447 __isl_take isl_pw_aff *pa2)
2451 is_cst = isl_pw_aff_is_cst(pa2);
2455 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
2456 "second argument should be a piecewise constant",
2458 return isl_pw_aff_align_params_pw_pw_and(pa1, pa2, &pw_aff_div);
2460 isl_pw_aff_free(pa1);
2461 isl_pw_aff_free(pa2);
2465 /* Compute the quotient of the integer division of "pa1" by "pa2"
2466 * with rounding towards zero.
2467 * "pa2" is assumed to be a piecewise constant.
2469 * In particular, return
2471 * pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2)
2474 __isl_give isl_pw_aff *isl_pw_aff_tdiv_q(__isl_take isl_pw_aff *pa1,
2475 __isl_take isl_pw_aff *pa2)
2481 is_cst = isl_pw_aff_is_cst(pa2);
2485 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
2486 "second argument should be a piecewise constant",
2489 pa1 = isl_pw_aff_div(pa1, pa2);
2491 cond = isl_pw_aff_nonneg_set(isl_pw_aff_copy(pa1));
2492 f = isl_pw_aff_floor(isl_pw_aff_copy(pa1));
2493 c = isl_pw_aff_ceil(pa1);
2494 return isl_pw_aff_cond(isl_set_indicator_function(cond), f, c);
2496 isl_pw_aff_free(pa1);
2497 isl_pw_aff_free(pa2);
2501 /* Compute the remainder of the integer division of "pa1" by "pa2"
2502 * with rounding towards zero.
2503 * "pa2" is assumed to be a piecewise constant.
2505 * In particular, return
2507 * pa1 - pa2 * (pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2))
2510 __isl_give isl_pw_aff *isl_pw_aff_tdiv_r(__isl_take isl_pw_aff *pa1,
2511 __isl_take isl_pw_aff *pa2)
2516 is_cst = isl_pw_aff_is_cst(pa2);
2520 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
2521 "second argument should be a piecewise constant",
2523 res = isl_pw_aff_tdiv_q(isl_pw_aff_copy(pa1), isl_pw_aff_copy(pa2));
2524 res = isl_pw_aff_mul(pa2, res);
2525 res = isl_pw_aff_sub(pa1, res);
2528 isl_pw_aff_free(pa1);
2529 isl_pw_aff_free(pa2);
2533 static __isl_give isl_pw_aff *pw_aff_min(__isl_take isl_pw_aff *pwaff1,
2534 __isl_take isl_pw_aff *pwaff2)
2539 dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1)),
2540 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2)));
2541 le = isl_pw_aff_le_set(isl_pw_aff_copy(pwaff1),
2542 isl_pw_aff_copy(pwaff2));
2543 dom = isl_set_subtract(dom, isl_set_copy(le));
2544 return isl_pw_aff_select(le, pwaff1, dom, pwaff2);
2547 __isl_give isl_pw_aff *isl_pw_aff_min(__isl_take isl_pw_aff *pwaff1,
2548 __isl_take isl_pw_aff *pwaff2)
2550 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_min);
2553 static __isl_give isl_pw_aff *pw_aff_max(__isl_take isl_pw_aff *pwaff1,
2554 __isl_take isl_pw_aff *pwaff2)
2559 dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1)),
2560 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2)));
2561 ge = isl_pw_aff_ge_set(isl_pw_aff_copy(pwaff1),
2562 isl_pw_aff_copy(pwaff2));
2563 dom = isl_set_subtract(dom, isl_set_copy(ge));
2564 return isl_pw_aff_select(ge, pwaff1, dom, pwaff2);
2567 __isl_give isl_pw_aff *isl_pw_aff_max(__isl_take isl_pw_aff *pwaff1,
2568 __isl_take isl_pw_aff *pwaff2)
2570 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_max);
2573 static __isl_give isl_pw_aff *pw_aff_list_reduce(
2574 __isl_take isl_pw_aff_list *list,
2575 __isl_give isl_pw_aff *(*fn)(__isl_take isl_pw_aff *pwaff1,
2576 __isl_take isl_pw_aff *pwaff2))
2585 ctx = isl_pw_aff_list_get_ctx(list);
2587 isl_die(ctx, isl_error_invalid,
2588 "list should contain at least one element",
2589 return isl_pw_aff_list_free(list));
2591 res = isl_pw_aff_copy(list->p[0]);
2592 for (i = 1; i < list->n; ++i)
2593 res = fn(res, isl_pw_aff_copy(list->p[i]));
2595 isl_pw_aff_list_free(list);
2599 /* Return an isl_pw_aff that maps each element in the intersection of the
2600 * domains of the elements of list to the minimal corresponding affine
2603 __isl_give isl_pw_aff *isl_pw_aff_list_min(__isl_take isl_pw_aff_list *list)
2605 return pw_aff_list_reduce(list, &isl_pw_aff_min);
2608 /* Return an isl_pw_aff that maps each element in the intersection of the
2609 * domains of the elements of list to the maximal corresponding affine
2612 __isl_give isl_pw_aff *isl_pw_aff_list_max(__isl_take isl_pw_aff_list *list)
2614 return pw_aff_list_reduce(list, &isl_pw_aff_max);
2617 /* Mark the domains of "pwaff" as rational.
2619 __isl_give isl_pw_aff *isl_pw_aff_set_rational(__isl_take isl_pw_aff *pwaff)
2623 pwaff = isl_pw_aff_cow(pwaff);
2629 for (i = 0; i < pwaff->n; ++i) {
2630 pwaff->p[i].set = isl_set_set_rational(pwaff->p[i].set);
2631 if (!pwaff->p[i].set)
2632 return isl_pw_aff_free(pwaff);
2638 /* Mark the domains of the elements of "list" as rational.
2640 __isl_give isl_pw_aff_list *isl_pw_aff_list_set_rational(
2641 __isl_take isl_pw_aff_list *list)
2651 for (i = 0; i < n; ++i) {
2654 pa = isl_pw_aff_list_get_pw_aff(list, i);
2655 pa = isl_pw_aff_set_rational(pa);
2656 list = isl_pw_aff_list_set_pw_aff(list, i, pa);
2662 /* Check that the domain space of "aff" matches "space".
2664 * Return 0 on success and -1 on error.
2666 int isl_aff_check_match_domain_space(__isl_keep isl_aff *aff,
2667 __isl_keep isl_space *space)
2669 isl_space *aff_space;
2675 aff_space = isl_aff_get_domain_space(aff);
2677 match = isl_space_match(space, isl_dim_param, aff_space, isl_dim_param);
2681 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2682 "parameters don't match", goto error);
2683 match = isl_space_tuple_match(space, isl_dim_in,
2684 aff_space, isl_dim_set);
2688 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2689 "domains don't match", goto error);
2690 isl_space_free(aff_space);
2693 isl_space_free(aff_space);
2700 #include <isl_multi_templ.c>
2702 /* Create an isl_pw_multi_aff with the given isl_multi_aff on a universe
2705 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_multi_aff(
2706 __isl_take isl_multi_aff *ma)
2708 isl_set *dom = isl_set_universe(isl_multi_aff_get_domain_space(ma));
2709 return isl_pw_multi_aff_alloc(dom, ma);
2712 /* Create a piecewise multi-affine expression in the given space that maps each
2713 * input dimension to the corresponding output dimension.
2715 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_identity(
2716 __isl_take isl_space *space)
2718 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_identity(space));
2721 __isl_give isl_multi_aff *isl_multi_aff_add(__isl_take isl_multi_aff *maff1,
2722 __isl_take isl_multi_aff *maff2)
2724 return isl_multi_aff_bin_op(maff1, maff2, &isl_aff_add);
2727 /* Subtract "ma2" from "ma1" and return the result.
2729 __isl_give isl_multi_aff *isl_multi_aff_sub(__isl_take isl_multi_aff *ma1,
2730 __isl_take isl_multi_aff *ma2)
2732 return isl_multi_aff_bin_op(ma1, ma2, &isl_aff_sub);
2735 /* Given two multi-affine expressions A -> B and C -> D,
2736 * construct a multi-affine expression [A -> C] -> [B -> D].
2738 __isl_give isl_multi_aff *isl_multi_aff_product(
2739 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2)
2745 int in1, in2, out1, out2;
2747 in1 = isl_multi_aff_dim(ma1, isl_dim_in);
2748 in2 = isl_multi_aff_dim(ma2, isl_dim_in);
2749 out1 = isl_multi_aff_dim(ma1, isl_dim_out);
2750 out2 = isl_multi_aff_dim(ma2, isl_dim_out);
2751 space = isl_space_product(isl_multi_aff_get_space(ma1),
2752 isl_multi_aff_get_space(ma2));
2753 res = isl_multi_aff_alloc(isl_space_copy(space));
2754 space = isl_space_domain(space);
2756 for (i = 0; i < out1; ++i) {
2757 aff = isl_multi_aff_get_aff(ma1, i);
2758 aff = isl_aff_insert_dims(aff, isl_dim_in, in1, in2);
2759 aff = isl_aff_reset_domain_space(aff, isl_space_copy(space));
2760 res = isl_multi_aff_set_aff(res, i, aff);
2763 for (i = 0; i < out2; ++i) {
2764 aff = isl_multi_aff_get_aff(ma2, i);
2765 aff = isl_aff_insert_dims(aff, isl_dim_in, 0, in1);
2766 aff = isl_aff_reset_domain_space(aff, isl_space_copy(space));
2767 res = isl_multi_aff_set_aff(res, out1 + i, aff);
2770 isl_space_free(space);
2771 isl_multi_aff_free(ma1);
2772 isl_multi_aff_free(ma2);
2776 /* Exploit the equalities in "eq" to simplify the affine expressions.
2778 static __isl_give isl_multi_aff *isl_multi_aff_substitute_equalities(
2779 __isl_take isl_multi_aff *maff, __isl_take isl_basic_set *eq)
2783 maff = isl_multi_aff_cow(maff);
2787 for (i = 0; i < maff->n; ++i) {
2788 maff->p[i] = isl_aff_substitute_equalities(maff->p[i],
2789 isl_basic_set_copy(eq));
2794 isl_basic_set_free(eq);
2797 isl_basic_set_free(eq);
2798 isl_multi_aff_free(maff);
2802 __isl_give isl_multi_aff *isl_multi_aff_scale(__isl_take isl_multi_aff *maff,
2807 maff = isl_multi_aff_cow(maff);
2811 for (i = 0; i < maff->n; ++i) {
2812 maff->p[i] = isl_aff_scale(maff->p[i], f);
2814 return isl_multi_aff_free(maff);
2820 __isl_give isl_multi_aff *isl_multi_aff_add_on_domain(__isl_keep isl_set *dom,
2821 __isl_take isl_multi_aff *maff1, __isl_take isl_multi_aff *maff2)
2823 maff1 = isl_multi_aff_add(maff1, maff2);
2824 maff1 = isl_multi_aff_gist(maff1, isl_set_copy(dom));
2828 int isl_multi_aff_is_empty(__isl_keep isl_multi_aff *maff)
2836 int isl_multi_aff_plain_is_equal(__isl_keep isl_multi_aff *maff1,
2837 __isl_keep isl_multi_aff *maff2)
2842 if (!maff1 || !maff2)
2844 if (maff1->n != maff2->n)
2846 equal = isl_space_is_equal(maff1->space, maff2->space);
2847 if (equal < 0 || !equal)
2850 for (i = 0; i < maff1->n; ++i) {
2851 equal = isl_aff_plain_is_equal(maff1->p[i], maff2->p[i]);
2852 if (equal < 0 || !equal)
2859 /* Return the set of domain elements where "ma1" is lexicographically
2860 * smaller than or equal to "ma2".
2862 __isl_give isl_set *isl_multi_aff_lex_le_set(__isl_take isl_multi_aff *ma1,
2863 __isl_take isl_multi_aff *ma2)
2865 return isl_multi_aff_lex_ge_set(ma2, ma1);
2868 /* Return the set of domain elements where "ma1" is lexicographically
2869 * greater than or equal to "ma2".
2871 __isl_give isl_set *isl_multi_aff_lex_ge_set(__isl_take isl_multi_aff *ma1,
2872 __isl_take isl_multi_aff *ma2)
2875 isl_map *map1, *map2;
2878 map1 = isl_map_from_multi_aff(ma1);
2879 map2 = isl_map_from_multi_aff(ma2);
2880 map = isl_map_range_product(map1, map2);
2881 space = isl_space_range(isl_map_get_space(map));
2882 space = isl_space_domain(isl_space_unwrap(space));
2883 ge = isl_map_lex_ge(space);
2884 map = isl_map_intersect_range(map, isl_map_wrap(ge));
2886 return isl_map_domain(map);
2890 #define PW isl_pw_multi_aff
2892 #define EL isl_multi_aff
2894 #define EL_IS_ZERO is_empty
2898 #define IS_ZERO is_empty
2901 #undef DEFAULT_IS_ZERO
2902 #define DEFAULT_IS_ZERO 0
2907 #define NO_INVOLVES_DIMS
2908 #define NO_MOVE_DIMS
2909 #define NO_INSERT_DIMS
2913 #include <isl_pw_templ.c>
2916 #define UNION isl_union_pw_multi_aff
2918 #define PART isl_pw_multi_aff
2920 #define PARTS pw_multi_aff
2921 #define ALIGN_DOMAIN
2925 #include <isl_union_templ.c>
2927 /* Given a function "cmp" that returns the set of elements where
2928 * "ma1" is "better" than "ma2", return the intersection of this
2929 * set with "dom1" and "dom2".
2931 static __isl_give isl_set *shared_and_better(__isl_keep isl_set *dom1,
2932 __isl_keep isl_set *dom2, __isl_keep isl_multi_aff *ma1,
2933 __isl_keep isl_multi_aff *ma2,
2934 __isl_give isl_set *(*cmp)(__isl_take isl_multi_aff *ma1,
2935 __isl_take isl_multi_aff *ma2))
2941 common = isl_set_intersect(isl_set_copy(dom1), isl_set_copy(dom2));
2942 is_empty = isl_set_plain_is_empty(common);
2943 if (is_empty >= 0 && is_empty)
2946 return isl_set_free(common);
2947 better = cmp(isl_multi_aff_copy(ma1), isl_multi_aff_copy(ma2));
2948 better = isl_set_intersect(common, better);
2953 /* Given a function "cmp" that returns the set of elements where
2954 * "ma1" is "better" than "ma2", return a piecewise multi affine
2955 * expression defined on the union of the definition domains
2956 * of "pma1" and "pma2" that maps to the "best" of "pma1" and
2957 * "pma2" on each cell. If only one of the two input functions
2958 * is defined on a given cell, then it is considered the best.
2960 static __isl_give isl_pw_multi_aff *pw_multi_aff_union_opt(
2961 __isl_take isl_pw_multi_aff *pma1,
2962 __isl_take isl_pw_multi_aff *pma2,
2963 __isl_give isl_set *(*cmp)(__isl_take isl_multi_aff *ma1,
2964 __isl_take isl_multi_aff *ma2))
2967 isl_pw_multi_aff *res = NULL;
2969 isl_set *set = NULL;
2974 ctx = isl_space_get_ctx(pma1->dim);
2975 if (!isl_space_is_equal(pma1->dim, pma2->dim))
2976 isl_die(ctx, isl_error_invalid,
2977 "arguments should live in the same space", goto error);
2979 if (isl_pw_multi_aff_is_empty(pma1)) {
2980 isl_pw_multi_aff_free(pma1);
2984 if (isl_pw_multi_aff_is_empty(pma2)) {
2985 isl_pw_multi_aff_free(pma2);
2989 n = 2 * (pma1->n + 1) * (pma2->n + 1);
2990 res = isl_pw_multi_aff_alloc_size(isl_space_copy(pma1->dim), n);
2992 for (i = 0; i < pma1->n; ++i) {
2993 set = isl_set_copy(pma1->p[i].set);
2994 for (j = 0; j < pma2->n; ++j) {
2998 better = shared_and_better(pma2->p[j].set,
2999 pma1->p[i].set, pma2->p[j].maff,
3000 pma1->p[i].maff, cmp);
3001 is_empty = isl_set_plain_is_empty(better);
3002 if (is_empty < 0 || is_empty) {
3003 isl_set_free(better);
3008 set = isl_set_subtract(set, isl_set_copy(better));
3010 res = isl_pw_multi_aff_add_piece(res, better,
3011 isl_multi_aff_copy(pma2->p[j].maff));
3013 res = isl_pw_multi_aff_add_piece(res, set,
3014 isl_multi_aff_copy(pma1->p[i].maff));
3017 for (j = 0; j < pma2->n; ++j) {
3018 set = isl_set_copy(pma2->p[j].set);
3019 for (i = 0; i < pma1->n; ++i)
3020 set = isl_set_subtract(set,
3021 isl_set_copy(pma1->p[i].set));
3022 res = isl_pw_multi_aff_add_piece(res, set,
3023 isl_multi_aff_copy(pma2->p[j].maff));
3026 isl_pw_multi_aff_free(pma1);
3027 isl_pw_multi_aff_free(pma2);
3031 isl_pw_multi_aff_free(pma1);
3032 isl_pw_multi_aff_free(pma2);
3034 return isl_pw_multi_aff_free(res);
3037 static __isl_give isl_pw_multi_aff *pw_multi_aff_union_lexmax(
3038 __isl_take isl_pw_multi_aff *pma1,
3039 __isl_take isl_pw_multi_aff *pma2)
3041 return pw_multi_aff_union_opt(pma1, pma2, &isl_multi_aff_lex_ge_set);
3044 /* Given two piecewise multi affine expressions, return a piecewise
3045 * multi-affine expression defined on the union of the definition domains
3046 * of the inputs that is equal to the lexicographic maximum of the two
3047 * inputs on each cell. If only one of the two inputs is defined on
3048 * a given cell, then it is considered to be the maximum.
3050 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_lexmax(
3051 __isl_take isl_pw_multi_aff *pma1,
3052 __isl_take isl_pw_multi_aff *pma2)
3054 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
3055 &pw_multi_aff_union_lexmax);
3058 static __isl_give isl_pw_multi_aff *pw_multi_aff_union_lexmin(
3059 __isl_take isl_pw_multi_aff *pma1,
3060 __isl_take isl_pw_multi_aff *pma2)
3062 return pw_multi_aff_union_opt(pma1, pma2, &isl_multi_aff_lex_le_set);
3065 /* Given two piecewise multi affine expressions, return a piecewise
3066 * multi-affine expression defined on the union of the definition domains
3067 * of the inputs that is equal to the lexicographic minimum of the two
3068 * inputs on each cell. If only one of the two inputs is defined on
3069 * a given cell, then it is considered to be the minimum.
3071 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_lexmin(
3072 __isl_take isl_pw_multi_aff *pma1,
3073 __isl_take isl_pw_multi_aff *pma2)
3075 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
3076 &pw_multi_aff_union_lexmin);
3079 static __isl_give isl_pw_multi_aff *pw_multi_aff_add(
3080 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
3082 return isl_pw_multi_aff_on_shared_domain(pma1, pma2,
3083 &isl_multi_aff_add);
3086 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_add(
3087 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
3089 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
3093 static __isl_give isl_pw_multi_aff *pw_multi_aff_sub(
3094 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
3096 return isl_pw_multi_aff_on_shared_domain(pma1, pma2,
3097 &isl_multi_aff_sub);
3100 /* Subtract "pma2" from "pma1" and return the result.
3102 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_sub(
3103 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
3105 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
3109 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_add(
3110 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
3112 return isl_pw_multi_aff_union_add_(pma1, pma2);
3115 /* Given two piecewise multi-affine expressions A -> B and C -> D,
3116 * construct a piecewise multi-affine expression [A -> C] -> [B -> D].
3118 static __isl_give isl_pw_multi_aff *pw_multi_aff_product(
3119 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
3123 isl_pw_multi_aff *res;
3128 n = pma1->n * pma2->n;
3129 space = isl_space_product(isl_space_copy(pma1->dim),
3130 isl_space_copy(pma2->dim));
3131 res = isl_pw_multi_aff_alloc_size(space, n);
3133 for (i = 0; i < pma1->n; ++i) {
3134 for (j = 0; j < pma2->n; ++j) {
3138 domain = isl_set_product(isl_set_copy(pma1->p[i].set),
3139 isl_set_copy(pma2->p[j].set));
3140 ma = isl_multi_aff_product(
3141 isl_multi_aff_copy(pma1->p[i].maff),
3142 isl_multi_aff_copy(pma2->p[i].maff));
3143 res = isl_pw_multi_aff_add_piece(res, domain, ma);
3147 isl_pw_multi_aff_free(pma1);
3148 isl_pw_multi_aff_free(pma2);
3151 isl_pw_multi_aff_free(pma1);
3152 isl_pw_multi_aff_free(pma2);
3156 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_product(
3157 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
3159 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
3160 &pw_multi_aff_product);
3163 /* Construct a map mapping the domain of the piecewise multi-affine expression
3164 * to its range, with each dimension in the range equated to the
3165 * corresponding affine expression on its cell.
3167 __isl_give isl_map *isl_map_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma)
3175 map = isl_map_empty(isl_pw_multi_aff_get_space(pma));
3177 for (i = 0; i < pma->n; ++i) {
3178 isl_multi_aff *maff;
3179 isl_basic_map *bmap;
3182 maff = isl_multi_aff_copy(pma->p[i].maff);
3183 bmap = isl_basic_map_from_multi_aff(maff);
3184 map_i = isl_map_from_basic_map(bmap);
3185 map_i = isl_map_intersect_domain(map_i,
3186 isl_set_copy(pma->p[i].set));
3187 map = isl_map_union_disjoint(map, map_i);
3190 isl_pw_multi_aff_free(pma);
3194 __isl_give isl_set *isl_set_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma)
3199 if (!isl_space_is_set(pma->dim))
3200 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
3201 "isl_pw_multi_aff cannot be converted into an isl_set",
3202 return isl_pw_multi_aff_free(pma));
3204 return isl_map_from_pw_multi_aff(pma);
3207 /* Given a basic map with a single output dimension that is defined
3208 * in terms of the parameters and input dimensions using an equality,
3209 * extract an isl_aff that expresses the output dimension in terms
3210 * of the parameters and input dimensions.
3212 * Since some applications expect the result of isl_pw_multi_aff_from_map
3213 * to only contain integer affine expressions, we compute the floor
3214 * of the expression before returning.
3216 * This function shares some similarities with
3217 * isl_basic_map_has_defining_equality and isl_constraint_get_bound.
3219 static __isl_give isl_aff *extract_isl_aff_from_basic_map(
3220 __isl_take isl_basic_map *bmap)
3225 isl_local_space *ls;
3230 if (isl_basic_map_dim(bmap, isl_dim_out) != 1)
3231 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
3232 "basic map should have a single output dimension",
3234 offset = isl_basic_map_offset(bmap, isl_dim_out);
3235 total = isl_basic_map_total_dim(bmap);
3236 for (i = 0; i < bmap->n_eq; ++i) {
3237 if (isl_int_is_zero(bmap->eq[i][offset]))
3239 if (isl_seq_first_non_zero(bmap->eq[i] + offset + 1,
3240 1 + total - (offset + 1)) != -1)
3244 if (i >= bmap->n_eq)
3245 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
3246 "unable to find suitable equality", goto error);
3247 ls = isl_basic_map_get_local_space(bmap);
3248 aff = isl_aff_alloc(isl_local_space_domain(ls));
3251 if (isl_int_is_neg(bmap->eq[i][offset]))
3252 isl_seq_cpy(aff->v->el + 1, bmap->eq[i], offset);
3254 isl_seq_neg(aff->v->el + 1, bmap->eq[i], offset);
3255 isl_seq_clr(aff->v->el + 1 + offset, aff->v->size - (1 + offset));
3256 isl_int_abs(aff->v->el[0], bmap->eq[i][offset]);
3257 isl_basic_map_free(bmap);
3259 aff = isl_aff_remove_unused_divs(aff);
3260 aff = isl_aff_floor(aff);
3263 isl_basic_map_free(bmap);
3267 /* Given a basic map where each output dimension is defined
3268 * in terms of the parameters and input dimensions using an equality,
3269 * extract an isl_multi_aff that expresses the output dimensions in terms
3270 * of the parameters and input dimensions.
3272 static __isl_give isl_multi_aff *extract_isl_multi_aff_from_basic_map(
3273 __isl_take isl_basic_map *bmap)
3282 ma = isl_multi_aff_alloc(isl_basic_map_get_space(bmap));
3283 n_out = isl_basic_map_dim(bmap, isl_dim_out);
3285 for (i = 0; i < n_out; ++i) {
3286 isl_basic_map *bmap_i;
3289 bmap_i = isl_basic_map_copy(bmap);
3290 bmap_i = isl_basic_map_project_out(bmap_i, isl_dim_out,
3291 i + 1, n_out - (1 + i));
3292 bmap_i = isl_basic_map_project_out(bmap_i, isl_dim_out, 0, i);
3293 aff = extract_isl_aff_from_basic_map(bmap_i);
3294 ma = isl_multi_aff_set_aff(ma, i, aff);
3297 isl_basic_map_free(bmap);
3302 /* Create an isl_pw_multi_aff that is equivalent to
3303 * isl_map_intersect_domain(isl_map_from_basic_map(bmap), domain).
3304 * The given basic map is such that each output dimension is defined
3305 * in terms of the parameters and input dimensions using an equality.
3307 static __isl_give isl_pw_multi_aff *plain_pw_multi_aff_from_map(
3308 __isl_take isl_set *domain, __isl_take isl_basic_map *bmap)
3312 ma = extract_isl_multi_aff_from_basic_map(bmap);
3313 return isl_pw_multi_aff_alloc(domain, ma);
3316 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
3317 * This obviously only works if the input "map" is single-valued.
3318 * If so, we compute the lexicographic minimum of the image in the form
3319 * of an isl_pw_multi_aff. Since the image is unique, it is equal
3320 * to its lexicographic minimum.
3321 * If the input is not single-valued, we produce an error.
3323 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_base(
3324 __isl_take isl_map *map)
3328 isl_pw_multi_aff *pma;
3330 sv = isl_map_is_single_valued(map);
3334 isl_die(isl_map_get_ctx(map), isl_error_invalid,
3335 "map is not single-valued", goto error);
3336 map = isl_map_make_disjoint(map);
3340 pma = isl_pw_multi_aff_empty(isl_map_get_space(map));
3342 for (i = 0; i < map->n; ++i) {
3343 isl_pw_multi_aff *pma_i;
3344 isl_basic_map *bmap;
3345 bmap = isl_basic_map_copy(map->p[i]);
3346 pma_i = isl_basic_map_lexmin_pw_multi_aff(bmap);
3347 pma = isl_pw_multi_aff_add_disjoint(pma, pma_i);
3357 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
3358 * taking into account that the output dimension at position "d"
3359 * can be represented as
3361 * x = floor((e(...) + c1) / m)
3363 * given that constraint "i" is of the form
3365 * e(...) + c1 - m x >= 0
3368 * Let "map" be of the form
3372 * We construct a mapping
3374 * A -> [A -> x = floor(...)]
3376 * apply that to the map, obtaining
3378 * [A -> x = floor(...)] -> B
3380 * and equate dimension "d" to x.
3381 * We then compute a isl_pw_multi_aff representation of the resulting map
3382 * and plug in the mapping above.
3384 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_div(
3385 __isl_take isl_map *map, __isl_take isl_basic_map *hull, int d, int i)
3389 isl_local_space *ls;
3397 isl_pw_multi_aff *pma;
3400 is_set = isl_map_is_set(map);
3402 offset = isl_basic_map_offset(hull, isl_dim_out);
3403 ctx = isl_map_get_ctx(map);
3404 space = isl_space_domain(isl_map_get_space(map));
3405 n_in = isl_space_dim(space, isl_dim_set);
3406 n = isl_space_dim(space, isl_dim_all);
3408 v = isl_vec_alloc(ctx, 1 + 1 + n);
3410 isl_int_neg(v->el[0], hull->ineq[i][offset + d]);
3411 isl_seq_cpy(v->el + 1, hull->ineq[i], 1 + n);
3413 isl_basic_map_free(hull);
3415 ls = isl_local_space_from_space(isl_space_copy(space));
3416 aff = isl_aff_alloc_vec(ls, v);
3417 aff = isl_aff_floor(aff);
3419 isl_space_free(space);
3420 ma = isl_multi_aff_from_aff(aff);
3422 ma = isl_multi_aff_identity(isl_space_map_from_set(space));
3423 ma = isl_multi_aff_range_product(ma,
3424 isl_multi_aff_from_aff(aff));
3427 insert = isl_map_from_multi_aff(isl_multi_aff_copy(ma));
3428 map = isl_map_apply_domain(map, insert);
3429 map = isl_map_equate(map, isl_dim_in, n_in, isl_dim_out, d);
3430 pma = isl_pw_multi_aff_from_map(map);
3431 pma = isl_pw_multi_aff_pullback_multi_aff(pma, ma);
3436 /* Is constraint "c" of the form
3438 * e(...) + c1 - m x >= 0
3442 * -e(...) + c2 + m x >= 0
3444 * where m > 1 and e only depends on parameters and input dimemnsions?
3446 * "offset" is the offset of the output dimensions
3447 * "pos" is the position of output dimension x.
3449 static int is_potential_div_constraint(isl_int *c, int offset, int d, int total)
3451 if (isl_int_is_zero(c[offset + d]))
3453 if (isl_int_is_one(c[offset + d]))
3455 if (isl_int_is_negone(c[offset + d]))
3457 if (isl_seq_first_non_zero(c + offset, d) != -1)
3459 if (isl_seq_first_non_zero(c + offset + d + 1,
3460 total - (offset + d + 1)) != -1)
3465 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
3467 * As a special case, we first check if there is any pair of constraints,
3468 * shared by all the basic maps in "map" that force a given dimension
3469 * to be equal to the floor of some affine combination of the input dimensions.
3471 * In particular, if we can find two constraints
3473 * e(...) + c1 - m x >= 0 i.e., m x <= e(...) + c1
3477 * -e(...) + c2 + m x >= 0 i.e., m x >= e(...) - c2
3479 * where m > 1 and e only depends on parameters and input dimemnsions,
3482 * c1 + c2 < m i.e., -c2 >= c1 - (m - 1)
3484 * then we know that we can take
3486 * x = floor((e(...) + c1) / m)
3488 * without having to perform any computation.
3490 * Note that we know that
3494 * If c1 + c2 were 0, then we would have detected an equality during
3495 * simplification. If c1 + c2 were negative, then we would have detected
3498 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_check_div(
3499 __isl_take isl_map *map)
3505 isl_basic_map *hull;
3507 hull = isl_map_unshifted_simple_hull(isl_map_copy(map));
3512 dim = isl_map_dim(map, isl_dim_out);
3513 offset = isl_basic_map_offset(hull, isl_dim_out);
3514 total = 1 + isl_basic_map_total_dim(hull);
3516 for (d = 0; d < dim; ++d) {
3517 for (i = 0; i < n; ++i) {
3518 if (!is_potential_div_constraint(hull->ineq[i],
3521 for (j = i + 1; j < n; ++j) {
3522 if (!isl_seq_is_neg(hull->ineq[i] + 1,
3523 hull->ineq[j] + 1, total - 1))
3525 isl_int_add(sum, hull->ineq[i][0],
3527 if (isl_int_abs_lt(sum,
3528 hull->ineq[i][offset + d]))
3535 if (isl_int_is_pos(hull->ineq[j][offset + d]))
3537 return pw_multi_aff_from_map_div(map, hull, d, j);
3541 isl_basic_map_free(hull);
3542 return pw_multi_aff_from_map_base(map);
3545 isl_basic_map_free(hull);
3549 /* Given an affine expression
3551 * [A -> B] -> f(A,B)
3553 * construct an isl_multi_aff
3557 * such that dimension "d" in B' is set to "aff" and the remaining
3558 * dimensions are set equal to the corresponding dimensions in B.
3559 * "n_in" is the dimension of the space A.
3560 * "n_out" is the dimension of the space B.
3562 * If "is_set" is set, then the affine expression is of the form
3566 * and we construct an isl_multi_aff
3570 static __isl_give isl_multi_aff *range_map(__isl_take isl_aff *aff, int d,
3571 unsigned n_in, unsigned n_out, int is_set)
3575 isl_space *space, *space2;
3576 isl_local_space *ls;
3578 space = isl_aff_get_domain_space(aff);
3579 ls = isl_local_space_from_space(isl_space_copy(space));
3580 space2 = isl_space_copy(space);
3582 space2 = isl_space_range(isl_space_unwrap(space2));
3583 space = isl_space_map_from_domain_and_range(space, space2);
3584 ma = isl_multi_aff_alloc(space);
3585 ma = isl_multi_aff_set_aff(ma, d, aff);
3587 for (i = 0; i < n_out; ++i) {
3590 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
3591 isl_dim_set, n_in + i);
3592 ma = isl_multi_aff_set_aff(ma, i, aff);
3595 isl_local_space_free(ls);
3600 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
3601 * taking into account that the dimension at position "d" can be written as
3603 * x = m a + f(..) (1)
3605 * where m is equal to "gcd".
3606 * "i" is the index of the equality in "hull" that defines f(..).
3607 * In particular, the equality is of the form
3609 * f(..) - x + m g(existentials) = 0
3613 * -f(..) + x + m g(existentials) = 0
3615 * We basically plug (1) into "map", resulting in a map with "a"
3616 * in the range instead of "x". The corresponding isl_pw_multi_aff
3617 * defining "a" is then plugged back into (1) to obtain a definition fro "x".
3619 * Specifically, given the input map
3623 * We first wrap it into a set
3627 * and define (1) on top of the corresponding space, resulting in "aff".
3628 * We use this to create an isl_multi_aff that maps the output position "d"
3629 * from "a" to "x", leaving all other (intput and output) dimensions unchanged.
3630 * We plug this into the wrapped map, unwrap the result and compute the
3631 * corresponding isl_pw_multi_aff.
3632 * The result is an expression
3640 * so that we can plug that into "aff", after extending the latter to
3646 * If "map" is actually a set, then there is no "A" space, meaning
3647 * that we do not need to perform any wrapping, and that the result
3648 * of the recursive call is of the form
3652 * which is plugged into a mapping of the form
3656 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_stride(
3657 __isl_take isl_map *map, __isl_take isl_basic_map *hull, int d, int i,
3662 isl_local_space *ls;
3665 isl_pw_multi_aff *pma, *id;
3671 is_set = isl_map_is_set(map);
3673 n_in = isl_basic_map_dim(hull, isl_dim_in);
3674 n_out = isl_basic_map_dim(hull, isl_dim_out);
3675 o_out = isl_basic_map_offset(hull, isl_dim_out);
3680 set = isl_map_wrap(map);
3681 space = isl_space_map_from_set(isl_set_get_space(set));
3682 ma = isl_multi_aff_identity(space);
3683 ls = isl_local_space_from_space(isl_set_get_space(set));
3684 aff = isl_aff_alloc(ls);
3686 isl_int_set_si(aff->v->el[0], 1);
3687 if (isl_int_is_one(hull->eq[i][o_out + d]))
3688 isl_seq_neg(aff->v->el + 1, hull->eq[i],
3691 isl_seq_cpy(aff->v->el + 1, hull->eq[i],
3693 isl_int_set(aff->v->el[1 + o_out + d], gcd);
3695 ma = isl_multi_aff_set_aff(ma, n_in + d, isl_aff_copy(aff));
3696 set = isl_set_preimage_multi_aff(set, ma);
3698 ma = range_map(aff, d, n_in, n_out, is_set);
3703 map = isl_set_unwrap(set);
3704 pma = isl_pw_multi_aff_from_map(set);
3707 space = isl_pw_multi_aff_get_domain_space(pma);
3708 space = isl_space_map_from_set(space);
3709 id = isl_pw_multi_aff_identity(space);
3710 pma = isl_pw_multi_aff_range_product(id, pma);
3712 id = isl_pw_multi_aff_from_multi_aff(ma);
3713 pma = isl_pw_multi_aff_pullback_pw_multi_aff(id, pma);
3715 isl_basic_map_free(hull);
3719 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
3721 * As a special case, we first check if all output dimensions are uniquely
3722 * defined in terms of the parameters and input dimensions over the entire
3723 * domain. If so, we extract the desired isl_pw_multi_aff directly
3724 * from the affine hull of "map" and its domain.
3726 * Otherwise, we check if any of the output dimensions is "strided".
3727 * That is, we check if can be written as
3731 * with m greater than 1, a some combination of existentiall quantified
3732 * variables and f and expression in the parameters and input dimensions.
3733 * If so, we remove the stride in pw_multi_aff_from_map_stride.
3735 * Otherwise, we continue with pw_multi_aff_from_map_check_div for a further
3738 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_map(__isl_take isl_map *map)
3742 isl_basic_map *hull;
3752 hull = isl_map_affine_hull(isl_map_copy(map));
3753 sv = isl_basic_map_plain_is_single_valued(hull);
3755 return plain_pw_multi_aff_from_map(isl_map_domain(map), hull);
3757 hull = isl_basic_map_free(hull);
3761 n_div = isl_basic_map_dim(hull, isl_dim_div);
3762 o_div = isl_basic_map_offset(hull, isl_dim_div);
3765 isl_basic_map_free(hull);
3766 return pw_multi_aff_from_map_check_div(map);
3771 n_out = isl_basic_map_dim(hull, isl_dim_out);
3772 o_out = isl_basic_map_offset(hull, isl_dim_out);
3774 for (i = 0; i < n_out; ++i) {
3775 for (j = 0; j < hull->n_eq; ++j) {
3776 isl_int *eq = hull->eq[j];
3777 isl_pw_multi_aff *res;
3779 if (!isl_int_is_one(eq[o_out + i]) &&
3780 !isl_int_is_negone(eq[o_out + i]))
3782 if (isl_seq_first_non_zero(eq + o_out, i) != -1)
3784 if (isl_seq_first_non_zero(eq + o_out + i + 1,
3785 n_out - (i + 1)) != -1)
3787 isl_seq_gcd(eq + o_div, n_div, &gcd);
3788 if (isl_int_is_zero(gcd))
3790 if (isl_int_is_one(gcd))
3793 res = pw_multi_aff_from_map_stride(map, hull,
3801 isl_basic_map_free(hull);
3802 return pw_multi_aff_from_map_check_div(map);
3808 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_set(__isl_take isl_set *set)
3810 return isl_pw_multi_aff_from_map(set);
3813 /* Convert "map" into an isl_pw_multi_aff (if possible) and
3816 static int pw_multi_aff_from_map(__isl_take isl_map *map, void *user)
3818 isl_union_pw_multi_aff **upma = user;
3819 isl_pw_multi_aff *pma;
3821 pma = isl_pw_multi_aff_from_map(map);
3822 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
3824 return *upma ? 0 : -1;
3827 /* Try and create an isl_union_pw_multi_aff that is equivalent
3828 * to the given isl_union_map.
3829 * The isl_union_map is required to be single-valued in each space.
3830 * Otherwise, an error is produced.
3832 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_map(
3833 __isl_take isl_union_map *umap)
3836 isl_union_pw_multi_aff *upma;
3838 space = isl_union_map_get_space(umap);
3839 upma = isl_union_pw_multi_aff_empty(space);
3840 if (isl_union_map_foreach_map(umap, &pw_multi_aff_from_map, &upma) < 0)
3841 upma = isl_union_pw_multi_aff_free(upma);
3842 isl_union_map_free(umap);
3847 /* Try and create an isl_union_pw_multi_aff that is equivalent
3848 * to the given isl_union_set.
3849 * The isl_union_set is required to be a singleton in each space.
3850 * Otherwise, an error is produced.
3852 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_set(
3853 __isl_take isl_union_set *uset)
3855 return isl_union_pw_multi_aff_from_union_map(uset);
3858 /* Return the piecewise affine expression "set ? 1 : 0".
3860 __isl_give isl_pw_aff *isl_set_indicator_function(__isl_take isl_set *set)
3863 isl_space *space = isl_set_get_space(set);
3864 isl_local_space *ls = isl_local_space_from_space(space);
3865 isl_aff *zero = isl_aff_zero_on_domain(isl_local_space_copy(ls));
3866 isl_aff *one = isl_aff_zero_on_domain(ls);
3868 one = isl_aff_add_constant_si(one, 1);
3869 pa = isl_pw_aff_alloc(isl_set_copy(set), one);
3870 set = isl_set_complement(set);
3871 pa = isl_pw_aff_add_disjoint(pa, isl_pw_aff_alloc(set, zero));
3876 /* Plug in "subs" for dimension "type", "pos" of "aff".
3878 * Let i be the dimension to replace and let "subs" be of the form
3882 * and "aff" of the form
3888 * (a f + d g')/(m d)
3890 * where g' is the result of plugging in "subs" in each of the integer
3893 __isl_give isl_aff *isl_aff_substitute(__isl_take isl_aff *aff,
3894 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
3899 aff = isl_aff_cow(aff);
3901 return isl_aff_free(aff);
3903 ctx = isl_aff_get_ctx(aff);
3904 if (!isl_space_is_equal(aff->ls->dim, subs->ls->dim))
3905 isl_die(ctx, isl_error_invalid,
3906 "spaces don't match", return isl_aff_free(aff));
3907 if (isl_local_space_dim(subs->ls, isl_dim_div) != 0)
3908 isl_die(ctx, isl_error_unsupported,
3909 "cannot handle divs yet", return isl_aff_free(aff));
3911 aff->ls = isl_local_space_substitute(aff->ls, type, pos, subs);
3913 return isl_aff_free(aff);
3915 aff->v = isl_vec_cow(aff->v);
3917 return isl_aff_free(aff);
3919 pos += isl_local_space_offset(aff->ls, type);
3922 isl_seq_substitute(aff->v->el, pos, subs->v->el,
3923 aff->v->size, subs->v->size, v);
3929 /* Plug in "subs" for dimension "type", "pos" in each of the affine
3930 * expressions in "maff".
3932 __isl_give isl_multi_aff *isl_multi_aff_substitute(
3933 __isl_take isl_multi_aff *maff, enum isl_dim_type type, unsigned pos,
3934 __isl_keep isl_aff *subs)
3938 maff = isl_multi_aff_cow(maff);
3940 return isl_multi_aff_free(maff);
3942 if (type == isl_dim_in)
3945 for (i = 0; i < maff->n; ++i) {
3946 maff->p[i] = isl_aff_substitute(maff->p[i], type, pos, subs);
3948 return isl_multi_aff_free(maff);
3954 /* Plug in "subs" for dimension "type", "pos" of "pma".
3956 * pma is of the form
3960 * while subs is of the form
3962 * v' = B_j(v) -> S_j
3964 * Each pair i,j such that C_ij = A_i \cap B_i is non-empty
3965 * has a contribution in the result, in particular
3967 * C_ij(S_j) -> M_i(S_j)
3969 * Note that plugging in S_j in C_ij may also result in an empty set
3970 * and this contribution should simply be discarded.
3972 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_substitute(
3973 __isl_take isl_pw_multi_aff *pma, enum isl_dim_type type, unsigned pos,
3974 __isl_keep isl_pw_aff *subs)
3977 isl_pw_multi_aff *res;
3980 return isl_pw_multi_aff_free(pma);
3982 n = pma->n * subs->n;
3983 res = isl_pw_multi_aff_alloc_size(isl_space_copy(pma->dim), n);
3985 for (i = 0; i < pma->n; ++i) {
3986 for (j = 0; j < subs->n; ++j) {
3988 isl_multi_aff *res_ij;
3991 common = isl_set_intersect(
3992 isl_set_copy(pma->p[i].set),
3993 isl_set_copy(subs->p[j].set));
3994 common = isl_set_substitute(common,
3995 type, pos, subs->p[j].aff);
3996 empty = isl_set_plain_is_empty(common);
3997 if (empty < 0 || empty) {
3998 isl_set_free(common);
4004 res_ij = isl_multi_aff_substitute(
4005 isl_multi_aff_copy(pma->p[i].maff),
4006 type, pos, subs->p[j].aff);
4008 res = isl_pw_multi_aff_add_piece(res, common, res_ij);
4012 isl_pw_multi_aff_free(pma);
4015 isl_pw_multi_aff_free(pma);
4016 isl_pw_multi_aff_free(res);
4020 /* Compute the preimage of a range of dimensions in the affine expression "src"
4021 * under "ma" and put the result in "dst". The number of dimensions in "src"
4022 * that precede the range is given by "n_before". The number of dimensions
4023 * in the range is given by the number of output dimensions of "ma".
4024 * The number of dimensions that follow the range is given by "n_after".
4025 * If "has_denom" is set (to one),
4026 * then "src" and "dst" have an extra initial denominator.
4027 * "n_div_ma" is the number of existentials in "ma"
4028 * "n_div_bset" is the number of existentials in "src"
4029 * The resulting "dst" (which is assumed to have been allocated by
4030 * the caller) contains coefficients for both sets of existentials,
4031 * first those in "ma" and then those in "src".
4032 * f, c1, c2 and g are temporary objects that have been initialized
4035 * Let src represent the expression
4037 * (a(p) + f_u u + b v + f_w w + c(divs))/d
4039 * and let ma represent the expressions
4041 * v_i = (r_i(p) + s_i(y) + t_i(divs'))/m_i
4043 * We start out with the following expression for dst:
4045 * (a(p) + f_u u + 0 y + f_w w + 0 divs' + c(divs) + f \sum_i b_i v_i)/d
4047 * with the multiplication factor f initially equal to 1
4048 * and f \sum_i b_i v_i kept separately.
4049 * For each x_i that we substitute, we multiply the numerator
4050 * (and denominator) of dst by c_1 = m_i and add the numerator
4051 * of the x_i expression multiplied by c_2 = f b_i,
4052 * after removing the common factors of c_1 and c_2.
4053 * The multiplication factor f also needs to be multiplied by c_1
4054 * for the next x_j, j > i.
4056 void isl_seq_preimage(isl_int *dst, isl_int *src,
4057 __isl_keep isl_multi_aff *ma, int n_before, int n_after,
4058 int n_div_ma, int n_div_bmap,
4059 isl_int f, isl_int c1, isl_int c2, isl_int g, int has_denom)
4062 int n_param, n_in, n_out;
4065 n_param = isl_multi_aff_dim(ma, isl_dim_param);
4066 n_in = isl_multi_aff_dim(ma, isl_dim_in);
4067 n_out = isl_multi_aff_dim(ma, isl_dim_out);
4069 isl_seq_cpy(dst, src, has_denom + 1 + n_param + n_before);
4070 o_dst = o_src = has_denom + 1 + n_param + n_before;
4071 isl_seq_clr(dst + o_dst, n_in);
4074 isl_seq_cpy(dst + o_dst, src + o_src, n_after);
4077 isl_seq_clr(dst + o_dst, n_div_ma);
4079 isl_seq_cpy(dst + o_dst, src + o_src, n_div_bmap);
4081 isl_int_set_si(f, 1);
4083 for (i = 0; i < n_out; ++i) {
4084 int offset = has_denom + 1 + n_param + n_before + i;
4086 if (isl_int_is_zero(src[offset]))
4088 isl_int_set(c1, ma->p[i]->v->el[0]);
4089 isl_int_mul(c2, f, src[offset]);
4090 isl_int_gcd(g, c1, c2);
4091 isl_int_divexact(c1, c1, g);
4092 isl_int_divexact(c2, c2, g);
4094 isl_int_mul(f, f, c1);
4097 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
4098 c2, ma->p[i]->v->el + o_src, 1 + n_param);
4099 o_dst += 1 + n_param;
4100 o_src += 1 + n_param;
4101 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_before);
4103 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
4104 c2, ma->p[i]->v->el + o_src, n_in);
4107 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_after);
4109 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
4110 c2, ma->p[i]->v->el + o_src, n_div_ma);
4113 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_div_bmap);
4115 isl_int_mul(dst[0], dst[0], c1);
4119 /* Compute the pullback of "aff" by the function represented by "ma".
4120 * In other words, plug in "ma" in "aff". The result is an affine expression
4121 * defined over the domain space of "ma".
4123 * If "aff" is represented by
4125 * (a(p) + b x + c(divs))/d
4127 * and ma is represented by
4129 * x = D(p) + F(y) + G(divs')
4131 * then the result is
4133 * (a(p) + b D(p) + b F(y) + b G(divs') + c(divs))/d
4135 * The divs in the local space of the input are similarly adjusted
4136 * through a call to isl_local_space_preimage_multi_aff.
4138 __isl_give isl_aff *isl_aff_pullback_multi_aff(__isl_take isl_aff *aff,
4139 __isl_take isl_multi_aff *ma)
4141 isl_aff *res = NULL;
4142 isl_local_space *ls;
4143 int n_div_aff, n_div_ma;
4144 isl_int f, c1, c2, g;
4146 ma = isl_multi_aff_align_divs(ma);
4150 n_div_aff = isl_aff_dim(aff, isl_dim_div);
4151 n_div_ma = ma->n ? isl_aff_dim(ma->p[0], isl_dim_div) : 0;
4153 ls = isl_aff_get_domain_local_space(aff);
4154 ls = isl_local_space_preimage_multi_aff(ls, isl_multi_aff_copy(ma));
4155 res = isl_aff_alloc(ls);
4164 isl_seq_preimage(res->v->el, aff->v->el, ma, 0, 0, n_div_ma, n_div_aff,
4173 isl_multi_aff_free(ma);
4174 res = isl_aff_normalize(res);
4178 isl_multi_aff_free(ma);
4183 /* Compute the pullback of "ma1" by the function represented by "ma2".
4184 * In other words, plug in "ma2" in "ma1".
4186 __isl_give isl_multi_aff *isl_multi_aff_pullback_multi_aff(
4187 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2)
4190 isl_space *space = NULL;
4192 ma2 = isl_multi_aff_align_divs(ma2);
4193 ma1 = isl_multi_aff_cow(ma1);
4197 space = isl_space_join(isl_multi_aff_get_space(ma2),
4198 isl_multi_aff_get_space(ma1));
4200 for (i = 0; i < ma1->n; ++i) {
4201 ma1->p[i] = isl_aff_pullback_multi_aff(ma1->p[i],
4202 isl_multi_aff_copy(ma2));
4207 ma1 = isl_multi_aff_reset_space(ma1, space);
4208 isl_multi_aff_free(ma2);
4211 isl_space_free(space);
4212 isl_multi_aff_free(ma2);
4213 isl_multi_aff_free(ma1);
4217 /* Extend the local space of "dst" to include the divs
4218 * in the local space of "src".
4220 __isl_give isl_aff *isl_aff_align_divs(__isl_take isl_aff *dst,
4221 __isl_keep isl_aff *src)
4229 return isl_aff_free(dst);
4231 ctx = isl_aff_get_ctx(src);
4232 if (!isl_space_is_equal(src->ls->dim, dst->ls->dim))
4233 isl_die(ctx, isl_error_invalid,
4234 "spaces don't match", goto error);
4236 if (src->ls->div->n_row == 0)
4239 exp1 = isl_alloc_array(ctx, int, src->ls->div->n_row);
4240 exp2 = isl_alloc_array(ctx, int, dst->ls->div->n_row);
4244 div = isl_merge_divs(src->ls->div, dst->ls->div, exp1, exp2);
4245 dst = isl_aff_expand_divs(dst, div, exp2);
4253 return isl_aff_free(dst);
4256 /* Adjust the local spaces of the affine expressions in "maff"
4257 * such that they all have the save divs.
4259 __isl_give isl_multi_aff *isl_multi_aff_align_divs(
4260 __isl_take isl_multi_aff *maff)
4268 maff = isl_multi_aff_cow(maff);
4272 for (i = 1; i < maff->n; ++i)
4273 maff->p[0] = isl_aff_align_divs(maff->p[0], maff->p[i]);
4274 for (i = 1; i < maff->n; ++i) {
4275 maff->p[i] = isl_aff_align_divs(maff->p[i], maff->p[0]);
4277 return isl_multi_aff_free(maff);
4283 __isl_give isl_aff *isl_aff_lift(__isl_take isl_aff *aff)
4285 aff = isl_aff_cow(aff);
4289 aff->ls = isl_local_space_lift(aff->ls);
4291 return isl_aff_free(aff);
4296 /* Lift "maff" to a space with extra dimensions such that the result
4297 * has no more existentially quantified variables.
4298 * If "ls" is not NULL, then *ls is assigned the local space that lies
4299 * at the basis of the lifting applied to "maff".
4301 __isl_give isl_multi_aff *isl_multi_aff_lift(__isl_take isl_multi_aff *maff,
4302 __isl_give isl_local_space **ls)
4316 isl_space *space = isl_multi_aff_get_domain_space(maff);
4317 *ls = isl_local_space_from_space(space);
4319 return isl_multi_aff_free(maff);
4324 maff = isl_multi_aff_cow(maff);
4325 maff = isl_multi_aff_align_divs(maff);
4329 n_div = isl_aff_dim(maff->p[0], isl_dim_div);
4330 space = isl_multi_aff_get_space(maff);
4331 space = isl_space_lift(isl_space_domain(space), n_div);
4332 space = isl_space_extend_domain_with_range(space,
4333 isl_multi_aff_get_space(maff));
4335 return isl_multi_aff_free(maff);
4336 isl_space_free(maff->space);
4337 maff->space = space;
4340 *ls = isl_aff_get_domain_local_space(maff->p[0]);
4342 return isl_multi_aff_free(maff);
4345 for (i = 0; i < maff->n; ++i) {
4346 maff->p[i] = isl_aff_lift(maff->p[i]);
4354 isl_local_space_free(*ls);
4355 return isl_multi_aff_free(maff);
4359 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma".
4361 __isl_give isl_pw_aff *isl_pw_multi_aff_get_pw_aff(
4362 __isl_keep isl_pw_multi_aff *pma, int pos)
4372 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
4373 if (pos < 0 || pos >= n_out)
4374 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
4375 "index out of bounds", return NULL);
4377 space = isl_pw_multi_aff_get_space(pma);
4378 space = isl_space_drop_dims(space, isl_dim_out,
4379 pos + 1, n_out - pos - 1);
4380 space = isl_space_drop_dims(space, isl_dim_out, 0, pos);
4382 pa = isl_pw_aff_alloc_size(space, pma->n);
4383 for (i = 0; i < pma->n; ++i) {
4385 aff = isl_multi_aff_get_aff(pma->p[i].maff, pos);
4386 pa = isl_pw_aff_add_piece(pa, isl_set_copy(pma->p[i].set), aff);
4392 /* Return an isl_pw_multi_aff with the given "set" as domain and
4393 * an unnamed zero-dimensional range.
4395 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_domain(
4396 __isl_take isl_set *set)
4401 space = isl_set_get_space(set);
4402 space = isl_space_from_domain(space);
4403 ma = isl_multi_aff_zero(space);
4404 return isl_pw_multi_aff_alloc(set, ma);
4407 /* Add an isl_pw_multi_aff with the given "set" as domain and
4408 * an unnamed zero-dimensional range to *user.
4410 static int add_pw_multi_aff_from_domain(__isl_take isl_set *set, void *user)
4412 isl_union_pw_multi_aff **upma = user;
4413 isl_pw_multi_aff *pma;
4415 pma = isl_pw_multi_aff_from_domain(set);
4416 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
4421 /* Return an isl_union_pw_multi_aff with the given "uset" as domain and
4422 * an unnamed zero-dimensional range.
4424 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_domain(
4425 __isl_take isl_union_set *uset)
4428 isl_union_pw_multi_aff *upma;
4433 space = isl_union_set_get_space(uset);
4434 upma = isl_union_pw_multi_aff_empty(space);
4436 if (isl_union_set_foreach_set(uset,
4437 &add_pw_multi_aff_from_domain, &upma) < 0)
4440 isl_union_set_free(uset);
4443 isl_union_set_free(uset);
4444 isl_union_pw_multi_aff_free(upma);
4448 /* Convert "pma" to an isl_map and add it to *umap.
4450 static int map_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma, void *user)
4452 isl_union_map **umap = user;
4455 map = isl_map_from_pw_multi_aff(pma);
4456 *umap = isl_union_map_add_map(*umap, map);
4461 /* Construct a union map mapping the domain of the union
4462 * piecewise multi-affine expression to its range, with each dimension
4463 * in the range equated to the corresponding affine expression on its cell.
4465 __isl_give isl_union_map *isl_union_map_from_union_pw_multi_aff(
4466 __isl_take isl_union_pw_multi_aff *upma)
4469 isl_union_map *umap;
4474 space = isl_union_pw_multi_aff_get_space(upma);
4475 umap = isl_union_map_empty(space);
4477 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
4478 &map_from_pw_multi_aff, &umap) < 0)
4481 isl_union_pw_multi_aff_free(upma);
4484 isl_union_pw_multi_aff_free(upma);
4485 isl_union_map_free(umap);
4489 /* Local data for bin_entry and the callback "fn".
4491 struct isl_union_pw_multi_aff_bin_data {
4492 isl_union_pw_multi_aff *upma2;
4493 isl_union_pw_multi_aff *res;
4494 isl_pw_multi_aff *pma;
4495 int (*fn)(void **entry, void *user);
4498 /* Given an isl_pw_multi_aff from upma1, store it in data->pma
4499 * and call data->fn for each isl_pw_multi_aff in data->upma2.
4501 static int bin_entry(void **entry, void *user)
4503 struct isl_union_pw_multi_aff_bin_data *data = user;
4504 isl_pw_multi_aff *pma = *entry;
4507 if (isl_hash_table_foreach(data->upma2->dim->ctx, &data->upma2->table,
4508 data->fn, data) < 0)
4514 /* Call "fn" on each pair of isl_pw_multi_affs in "upma1" and "upma2".
4515 * The isl_pw_multi_aff from upma1 is stored in data->pma (where data is
4516 * passed as user field) and the isl_pw_multi_aff from upma2 is available
4517 * as *entry. The callback should adjust data->res if desired.
4519 static __isl_give isl_union_pw_multi_aff *bin_op(
4520 __isl_take isl_union_pw_multi_aff *upma1,
4521 __isl_take isl_union_pw_multi_aff *upma2,
4522 int (*fn)(void **entry, void *user))
4525 struct isl_union_pw_multi_aff_bin_data data = { NULL, NULL, NULL, fn };
4527 space = isl_union_pw_multi_aff_get_space(upma2);
4528 upma1 = isl_union_pw_multi_aff_align_params(upma1, space);
4529 space = isl_union_pw_multi_aff_get_space(upma1);
4530 upma2 = isl_union_pw_multi_aff_align_params(upma2, space);
4532 if (!upma1 || !upma2)
4536 data.res = isl_union_pw_multi_aff_alloc(isl_space_copy(upma1->dim),
4538 if (isl_hash_table_foreach(upma1->dim->ctx, &upma1->table,
4539 &bin_entry, &data) < 0)
4542 isl_union_pw_multi_aff_free(upma1);
4543 isl_union_pw_multi_aff_free(upma2);
4546 isl_union_pw_multi_aff_free(upma1);
4547 isl_union_pw_multi_aff_free(upma2);
4548 isl_union_pw_multi_aff_free(data.res);
4552 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
4553 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
4555 static __isl_give isl_pw_multi_aff *pw_multi_aff_range_product(
4556 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4560 space = isl_space_range_product(isl_pw_multi_aff_get_space(pma1),
4561 isl_pw_multi_aff_get_space(pma2));
4562 return isl_pw_multi_aff_on_shared_domain_in(pma1, pma2, space,
4563 &isl_multi_aff_range_product);
4566 /* Given two isl_pw_multi_affs A -> B and C -> D,
4567 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
4569 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_range_product(
4570 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4572 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4573 &pw_multi_aff_range_product);
4576 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
4577 * construct an isl_pw_multi_aff (A * C) -> (B, D).
4579 static __isl_give isl_pw_multi_aff *pw_multi_aff_flat_range_product(
4580 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4584 space = isl_space_range_product(isl_pw_multi_aff_get_space(pma1),
4585 isl_pw_multi_aff_get_space(pma2));
4586 space = isl_space_flatten_range(space);
4587 return isl_pw_multi_aff_on_shared_domain_in(pma1, pma2, space,
4588 &isl_multi_aff_flat_range_product);
4591 /* Given two isl_pw_multi_affs A -> B and C -> D,
4592 * construct an isl_pw_multi_aff (A * C) -> (B, D).
4594 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_flat_range_product(
4595 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4597 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4598 &pw_multi_aff_flat_range_product);
4601 /* If data->pma and *entry have the same domain space, then compute
4602 * their flat range product and the result to data->res.
4604 static int flat_range_product_entry(void **entry, void *user)
4606 struct isl_union_pw_multi_aff_bin_data *data = user;
4607 isl_pw_multi_aff *pma2 = *entry;
4609 if (!isl_space_tuple_match(data->pma->dim, isl_dim_in,
4610 pma2->dim, isl_dim_in))
4613 pma2 = isl_pw_multi_aff_flat_range_product(
4614 isl_pw_multi_aff_copy(data->pma),
4615 isl_pw_multi_aff_copy(pma2));
4617 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma2);
4622 /* Given two isl_union_pw_multi_affs A -> B and C -> D,
4623 * construct an isl_union_pw_multi_aff (A * C) -> (B, D).
4625 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_flat_range_product(
4626 __isl_take isl_union_pw_multi_aff *upma1,
4627 __isl_take isl_union_pw_multi_aff *upma2)
4629 return bin_op(upma1, upma2, &flat_range_product_entry);
4632 /* Replace the affine expressions at position "pos" in "pma" by "pa".
4633 * The parameters are assumed to have been aligned.
4635 * The implementation essentially performs an isl_pw_*_on_shared_domain,
4636 * except that it works on two different isl_pw_* types.
4638 static __isl_give isl_pw_multi_aff *pw_multi_aff_set_pw_aff(
4639 __isl_take isl_pw_multi_aff *pma, unsigned pos,
4640 __isl_take isl_pw_aff *pa)
4643 isl_pw_multi_aff *res = NULL;
4648 if (!isl_space_tuple_match(pma->dim, isl_dim_in, pa->dim, isl_dim_in))
4649 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
4650 "domains don't match", goto error);
4651 if (pos >= isl_pw_multi_aff_dim(pma, isl_dim_out))
4652 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
4653 "index out of bounds", goto error);
4656 res = isl_pw_multi_aff_alloc_size(isl_pw_multi_aff_get_space(pma), n);
4658 for (i = 0; i < pma->n; ++i) {
4659 for (j = 0; j < pa->n; ++j) {
4661 isl_multi_aff *res_ij;
4664 common = isl_set_intersect(isl_set_copy(pma->p[i].set),
4665 isl_set_copy(pa->p[j].set));
4666 empty = isl_set_plain_is_empty(common);
4667 if (empty < 0 || empty) {
4668 isl_set_free(common);
4674 res_ij = isl_multi_aff_set_aff(
4675 isl_multi_aff_copy(pma->p[i].maff), pos,
4676 isl_aff_copy(pa->p[j].aff));
4677 res_ij = isl_multi_aff_gist(res_ij,
4678 isl_set_copy(common));
4680 res = isl_pw_multi_aff_add_piece(res, common, res_ij);
4684 isl_pw_multi_aff_free(pma);
4685 isl_pw_aff_free(pa);
4688 isl_pw_multi_aff_free(pma);
4689 isl_pw_aff_free(pa);
4690 return isl_pw_multi_aff_free(res);
4693 /* Replace the affine expressions at position "pos" in "pma" by "pa".
4695 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_set_pw_aff(
4696 __isl_take isl_pw_multi_aff *pma, unsigned pos,
4697 __isl_take isl_pw_aff *pa)
4701 if (isl_space_match(pma->dim, isl_dim_param, pa->dim, isl_dim_param))
4702 return pw_multi_aff_set_pw_aff(pma, pos, pa);
4703 if (!isl_space_has_named_params(pma->dim) ||
4704 !isl_space_has_named_params(pa->dim))
4705 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
4706 "unaligned unnamed parameters", goto error);
4707 pma = isl_pw_multi_aff_align_params(pma, isl_pw_aff_get_space(pa));
4708 pa = isl_pw_aff_align_params(pa, isl_pw_multi_aff_get_space(pma));
4709 return pw_multi_aff_set_pw_aff(pma, pos, pa);
4711 isl_pw_multi_aff_free(pma);
4712 isl_pw_aff_free(pa);
4716 /* Check that the domain space of "pa" matches "space".
4718 * Return 0 on success and -1 on error.
4720 int isl_pw_aff_check_match_domain_space(__isl_keep isl_pw_aff *pa,
4721 __isl_keep isl_space *space)
4723 isl_space *pa_space;
4729 pa_space = isl_pw_aff_get_space(pa);
4731 match = isl_space_match(space, isl_dim_param, pa_space, isl_dim_param);
4735 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
4736 "parameters don't match", goto error);
4737 match = isl_space_tuple_match(space, isl_dim_in, pa_space, isl_dim_in);
4741 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
4742 "domains don't match", goto error);
4743 isl_space_free(pa_space);
4746 isl_space_free(pa_space);
4753 #include <isl_multi_templ.c>
4755 /* Scale the first elements of "ma" by the corresponding elements of "vec".
4757 __isl_give isl_multi_aff *isl_multi_aff_scale_vec(__isl_take isl_multi_aff *ma,
4758 __isl_take isl_vec *vec)
4766 n = isl_multi_aff_dim(ma, isl_dim_out);
4767 if (isl_vec_size(vec) < n)
4768 n = isl_vec_size(vec);
4771 for (i = 0; i < n; ++i) {
4774 isl_vec_get_element(vec, i, &v);
4776 aff = isl_multi_aff_get_aff(ma, i);
4777 aff = isl_aff_scale(aff, v);
4778 ma = isl_multi_aff_set_aff(ma, i, aff);
4786 isl_multi_aff_free(ma);
4790 /* Scale the first elements of "pma" by the corresponding elements of "vec".
4792 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_scale_vec(
4793 __isl_take isl_pw_multi_aff *pma, __isl_take isl_vec *v)
4797 pma = isl_pw_multi_aff_cow(pma);
4801 for (i = 0; i < pma->n; ++i) {
4802 pma->p[i].maff = isl_multi_aff_scale_vec(pma->p[i].maff,
4804 if (!pma->p[i].maff)
4812 isl_pw_multi_aff_free(pma);
4816 /* This function is called for each entry of an isl_union_pw_multi_aff.
4817 * Replace the entry by the result of applying isl_pw_multi_aff_scale_vec
4818 * to the original entry with the isl_vec in "user" as extra argument.
4820 static int union_pw_multi_aff_scale_vec_entry(void **entry, void *user)
4822 isl_pw_multi_aff **pma = (isl_pw_multi_aff **) entry;
4825 *pma = isl_pw_multi_aff_scale_vec(*pma, isl_vec_copy(v));
4832 /* Scale the first elements of "upma" by the corresponding elements of "vec".
4834 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_scale_vec(
4835 __isl_take isl_union_pw_multi_aff *upma, __isl_take isl_vec *v)
4837 upma = isl_union_pw_multi_aff_cow(upma);
4841 if (isl_hash_table_foreach(upma->dim->ctx, &upma->table,
4842 &union_pw_multi_aff_scale_vec_entry, v) < 0)
4849 isl_union_pw_multi_aff_free(upma);