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_list_private.h>
23 #include <isl/constraint.h>
26 #include <isl_config.h>
28 __isl_give isl_aff *isl_aff_alloc_vec(__isl_take isl_local_space *ls,
29 __isl_take isl_vec *v)
36 aff = isl_calloc_type(v->ctx, struct isl_aff);
46 isl_local_space_free(ls);
51 __isl_give isl_aff *isl_aff_alloc(__isl_take isl_local_space *ls)
60 ctx = isl_local_space_get_ctx(ls);
61 if (!isl_local_space_divs_known(ls))
62 isl_die(ctx, isl_error_invalid, "local space has unknown divs",
64 if (!isl_local_space_is_set(ls))
65 isl_die(ctx, isl_error_invalid,
66 "domain of affine expression should be a set",
69 total = isl_local_space_dim(ls, isl_dim_all);
70 v = isl_vec_alloc(ctx, 1 + 1 + total);
71 return isl_aff_alloc_vec(ls, v);
73 isl_local_space_free(ls);
77 __isl_give isl_aff *isl_aff_zero_on_domain(__isl_take isl_local_space *ls)
81 aff = isl_aff_alloc(ls);
85 isl_int_set_si(aff->v->el[0], 1);
86 isl_seq_clr(aff->v->el + 1, aff->v->size - 1);
91 /* Return a piecewise affine expression defined on the specified domain
92 * that is equal to zero.
94 __isl_give isl_pw_aff *isl_pw_aff_zero_on_domain(__isl_take isl_local_space *ls)
96 return isl_pw_aff_from_aff(isl_aff_zero_on_domain(ls));
99 /* Return an affine expression that is equal to the specified dimension
102 __isl_give isl_aff *isl_aff_var_on_domain(__isl_take isl_local_space *ls,
103 enum isl_dim_type type, unsigned pos)
111 space = isl_local_space_get_space(ls);
114 if (isl_space_is_map(space))
115 isl_die(isl_space_get_ctx(space), isl_error_invalid,
116 "expecting (parameter) set space", goto error);
117 if (pos >= isl_local_space_dim(ls, type))
118 isl_die(isl_space_get_ctx(space), isl_error_invalid,
119 "position out of bounds", goto error);
121 isl_space_free(space);
122 aff = isl_aff_alloc(ls);
126 pos += isl_local_space_offset(aff->ls, type);
128 isl_int_set_si(aff->v->el[0], 1);
129 isl_seq_clr(aff->v->el + 1, aff->v->size - 1);
130 isl_int_set_si(aff->v->el[1 + pos], 1);
134 isl_local_space_free(ls);
135 isl_space_free(space);
139 /* Return a piecewise affine expression that is equal to
140 * the specified dimension in "ls".
142 __isl_give isl_pw_aff *isl_pw_aff_var_on_domain(__isl_take isl_local_space *ls,
143 enum isl_dim_type type, unsigned pos)
145 return isl_pw_aff_from_aff(isl_aff_var_on_domain(ls, type, pos));
148 __isl_give isl_aff *isl_aff_copy(__isl_keep isl_aff *aff)
157 __isl_give isl_aff *isl_aff_dup(__isl_keep isl_aff *aff)
162 return isl_aff_alloc_vec(isl_local_space_copy(aff->ls),
163 isl_vec_copy(aff->v));
166 __isl_give isl_aff *isl_aff_cow(__isl_take isl_aff *aff)
174 return isl_aff_dup(aff);
177 void *isl_aff_free(__isl_take isl_aff *aff)
185 isl_local_space_free(aff->ls);
186 isl_vec_free(aff->v);
193 isl_ctx *isl_aff_get_ctx(__isl_keep isl_aff *aff)
195 return aff ? isl_local_space_get_ctx(aff->ls) : NULL;
198 /* Externally, an isl_aff has a map space, but internally, the
199 * ls field corresponds to the domain of that space.
201 int isl_aff_dim(__isl_keep isl_aff *aff, enum isl_dim_type type)
205 if (type == isl_dim_out)
207 if (type == isl_dim_in)
209 return isl_local_space_dim(aff->ls, type);
212 __isl_give isl_space *isl_aff_get_domain_space(__isl_keep isl_aff *aff)
214 return aff ? isl_local_space_get_space(aff->ls) : NULL;
217 __isl_give isl_space *isl_aff_get_space(__isl_keep isl_aff *aff)
222 space = isl_local_space_get_space(aff->ls);
223 space = isl_space_from_domain(space);
224 space = isl_space_add_dims(space, isl_dim_out, 1);
228 __isl_give isl_local_space *isl_aff_get_domain_local_space(
229 __isl_keep isl_aff *aff)
231 return aff ? isl_local_space_copy(aff->ls) : NULL;
234 __isl_give isl_local_space *isl_aff_get_local_space(__isl_keep isl_aff *aff)
239 ls = isl_local_space_copy(aff->ls);
240 ls = isl_local_space_from_domain(ls);
241 ls = isl_local_space_add_dims(ls, isl_dim_out, 1);
245 /* Externally, an isl_aff has a map space, but internally, the
246 * ls field corresponds to the domain of that space.
248 const char *isl_aff_get_dim_name(__isl_keep isl_aff *aff,
249 enum isl_dim_type type, unsigned pos)
253 if (type == isl_dim_out)
255 if (type == isl_dim_in)
257 return isl_local_space_get_dim_name(aff->ls, type, pos);
260 __isl_give isl_aff *isl_aff_reset_domain_space(__isl_take isl_aff *aff,
261 __isl_take isl_space *dim)
263 aff = isl_aff_cow(aff);
267 aff->ls = isl_local_space_reset_space(aff->ls, dim);
269 return isl_aff_free(aff);
278 /* Reset the space of "aff". This function is called from isl_pw_templ.c
279 * and doesn't know if the space of an element object is represented
280 * directly or through its domain. It therefore passes along both.
282 __isl_give isl_aff *isl_aff_reset_space_and_domain(__isl_take isl_aff *aff,
283 __isl_take isl_space *space, __isl_take isl_space *domain)
285 isl_space_free(space);
286 return isl_aff_reset_domain_space(aff, domain);
289 /* Reorder the coefficients of the affine expression based
290 * on the given reodering.
291 * The reordering r is assumed to have been extended with the local
294 static __isl_give isl_vec *vec_reorder(__isl_take isl_vec *vec,
295 __isl_take isl_reordering *r, int n_div)
303 res = isl_vec_alloc(vec->ctx,
304 2 + isl_space_dim(r->dim, isl_dim_all) + n_div);
305 isl_seq_cpy(res->el, vec->el, 2);
306 isl_seq_clr(res->el + 2, res->size - 2);
307 for (i = 0; i < r->len; ++i)
308 isl_int_set(res->el[2 + r->pos[i]], vec->el[2 + i]);
310 isl_reordering_free(r);
315 isl_reordering_free(r);
319 /* Reorder the dimensions of the domain of "aff" according
320 * to the given reordering.
322 __isl_give isl_aff *isl_aff_realign_domain(__isl_take isl_aff *aff,
323 __isl_take isl_reordering *r)
325 aff = isl_aff_cow(aff);
329 r = isl_reordering_extend(r, aff->ls->div->n_row);
330 aff->v = vec_reorder(aff->v, isl_reordering_copy(r),
331 aff->ls->div->n_row);
332 aff->ls = isl_local_space_realign(aff->ls, r);
334 if (!aff->v || !aff->ls)
335 return isl_aff_free(aff);
340 isl_reordering_free(r);
344 __isl_give isl_aff *isl_aff_align_params(__isl_take isl_aff *aff,
345 __isl_take isl_space *model)
350 if (!isl_space_match(aff->ls->dim, isl_dim_param,
351 model, isl_dim_param)) {
354 model = isl_space_drop_dims(model, isl_dim_in,
355 0, isl_space_dim(model, isl_dim_in));
356 model = isl_space_drop_dims(model, isl_dim_out,
357 0, isl_space_dim(model, isl_dim_out));
358 exp = isl_parameter_alignment_reordering(aff->ls->dim, model);
359 exp = isl_reordering_extend_space(exp,
360 isl_aff_get_domain_space(aff));
361 aff = isl_aff_realign_domain(aff, exp);
364 isl_space_free(model);
367 isl_space_free(model);
372 int isl_aff_plain_is_zero(__isl_keep isl_aff *aff)
377 return isl_seq_first_non_zero(aff->v->el + 1, aff->v->size - 1) < 0;
380 int isl_aff_plain_is_equal(__isl_keep isl_aff *aff1, __isl_keep isl_aff *aff2)
387 equal = isl_local_space_is_equal(aff1->ls, aff2->ls);
388 if (equal < 0 || !equal)
391 return isl_vec_is_equal(aff1->v, aff2->v);
394 int isl_aff_get_denominator(__isl_keep isl_aff *aff, isl_int *v)
398 isl_int_set(*v, aff->v->el[0]);
402 int isl_aff_get_constant(__isl_keep isl_aff *aff, isl_int *v)
406 isl_int_set(*v, aff->v->el[1]);
410 int isl_aff_get_coefficient(__isl_keep isl_aff *aff,
411 enum isl_dim_type type, int pos, isl_int *v)
416 if (type == isl_dim_out)
417 isl_die(aff->v->ctx, isl_error_invalid,
418 "output/set dimension does not have a coefficient",
420 if (type == isl_dim_in)
423 if (pos >= isl_local_space_dim(aff->ls, type))
424 isl_die(aff->v->ctx, isl_error_invalid,
425 "position out of bounds", return -1);
427 pos += isl_local_space_offset(aff->ls, type);
428 isl_int_set(*v, aff->v->el[1 + pos]);
433 __isl_give isl_aff *isl_aff_set_denominator(__isl_take isl_aff *aff, isl_int v)
435 aff = isl_aff_cow(aff);
439 aff->v = isl_vec_cow(aff->v);
441 return isl_aff_free(aff);
443 isl_int_set(aff->v->el[0], v);
448 __isl_give isl_aff *isl_aff_set_constant(__isl_take isl_aff *aff, isl_int v)
450 aff = isl_aff_cow(aff);
454 aff->v = isl_vec_cow(aff->v);
456 return isl_aff_free(aff);
458 isl_int_set(aff->v->el[1], v);
463 __isl_give isl_aff *isl_aff_add_constant(__isl_take isl_aff *aff, isl_int v)
465 if (isl_int_is_zero(v))
468 aff = isl_aff_cow(aff);
472 aff->v = isl_vec_cow(aff->v);
474 return isl_aff_free(aff);
476 isl_int_addmul(aff->v->el[1], aff->v->el[0], v);
481 __isl_give isl_aff *isl_aff_add_constant_si(__isl_take isl_aff *aff, int v)
486 isl_int_set_si(t, v);
487 aff = isl_aff_add_constant(aff, t);
493 /* Add "v" to the numerator of the constant term of "aff".
495 __isl_give isl_aff *isl_aff_add_constant_num(__isl_take isl_aff *aff, isl_int v)
497 if (isl_int_is_zero(v))
500 aff = isl_aff_cow(aff);
504 aff->v = isl_vec_cow(aff->v);
506 return isl_aff_free(aff);
508 isl_int_add(aff->v->el[1], aff->v->el[1], v);
513 /* Add "v" to the numerator of the constant term of "aff".
515 __isl_give isl_aff *isl_aff_add_constant_num_si(__isl_take isl_aff *aff, int v)
523 isl_int_set_si(t, v);
524 aff = isl_aff_add_constant_num(aff, t);
530 __isl_give isl_aff *isl_aff_set_constant_si(__isl_take isl_aff *aff, int v)
532 aff = isl_aff_cow(aff);
536 aff->v = isl_vec_cow(aff->v);
538 return isl_aff_free(aff);
540 isl_int_set_si(aff->v->el[1], v);
545 __isl_give isl_aff *isl_aff_set_coefficient(__isl_take isl_aff *aff,
546 enum isl_dim_type type, int pos, isl_int v)
551 if (type == isl_dim_out)
552 isl_die(aff->v->ctx, isl_error_invalid,
553 "output/set dimension does not have a coefficient",
554 return isl_aff_free(aff));
555 if (type == isl_dim_in)
558 if (pos >= isl_local_space_dim(aff->ls, type))
559 isl_die(aff->v->ctx, isl_error_invalid,
560 "position out of bounds", return isl_aff_free(aff));
562 aff = isl_aff_cow(aff);
566 aff->v = isl_vec_cow(aff->v);
568 return isl_aff_free(aff);
570 pos += isl_local_space_offset(aff->ls, type);
571 isl_int_set(aff->v->el[1 + pos], v);
576 __isl_give isl_aff *isl_aff_set_coefficient_si(__isl_take isl_aff *aff,
577 enum isl_dim_type type, int pos, int v)
582 if (type == isl_dim_out)
583 isl_die(aff->v->ctx, isl_error_invalid,
584 "output/set dimension does not have a coefficient",
585 return isl_aff_free(aff));
586 if (type == isl_dim_in)
589 if (pos >= isl_local_space_dim(aff->ls, type))
590 isl_die(aff->v->ctx, isl_error_invalid,
591 "position out of bounds", return isl_aff_free(aff));
593 aff = isl_aff_cow(aff);
597 aff->v = isl_vec_cow(aff->v);
599 return isl_aff_free(aff);
601 pos += isl_local_space_offset(aff->ls, type);
602 isl_int_set_si(aff->v->el[1 + pos], v);
607 __isl_give isl_aff *isl_aff_add_coefficient(__isl_take isl_aff *aff,
608 enum isl_dim_type type, int pos, isl_int v)
613 if (type == isl_dim_out)
614 isl_die(aff->v->ctx, isl_error_invalid,
615 "output/set dimension does not have a coefficient",
616 return isl_aff_free(aff));
617 if (type == isl_dim_in)
620 if (pos >= isl_local_space_dim(aff->ls, type))
621 isl_die(aff->v->ctx, isl_error_invalid,
622 "position out of bounds", return isl_aff_free(aff));
624 aff = isl_aff_cow(aff);
628 aff->v = isl_vec_cow(aff->v);
630 return isl_aff_free(aff);
632 pos += isl_local_space_offset(aff->ls, type);
633 isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v);
638 __isl_give isl_aff *isl_aff_add_coefficient_si(__isl_take isl_aff *aff,
639 enum isl_dim_type type, int pos, int v)
644 isl_int_set_si(t, v);
645 aff = isl_aff_add_coefficient(aff, type, pos, t);
651 __isl_give isl_aff *isl_aff_get_div(__isl_keep isl_aff *aff, int pos)
656 return isl_local_space_get_div(aff->ls, pos);
659 __isl_give isl_aff *isl_aff_neg(__isl_take isl_aff *aff)
661 aff = isl_aff_cow(aff);
664 aff->v = isl_vec_cow(aff->v);
666 return isl_aff_free(aff);
668 isl_seq_neg(aff->v->el + 1, aff->v->el + 1, aff->v->size - 1);
673 /* Remove divs from the local space that do not appear in the affine
675 * We currently only remove divs at the end.
676 * Some intermediate divs may also not appear directly in the affine
677 * expression, but we would also need to check that no other divs are
678 * defined in terms of them.
680 __isl_give isl_aff *isl_aff_remove_unused_divs( __isl_take isl_aff *aff)
689 n = isl_local_space_dim(aff->ls, isl_dim_div);
690 off = isl_local_space_offset(aff->ls, isl_dim_div);
692 pos = isl_seq_last_non_zero(aff->v->el + 1 + off, n) + 1;
696 aff = isl_aff_cow(aff);
700 aff->ls = isl_local_space_drop_dims(aff->ls, isl_dim_div, pos, n - pos);
701 aff->v = isl_vec_drop_els(aff->v, 1 + off + pos, n - pos);
702 if (!aff->ls || !aff->v)
703 return isl_aff_free(aff);
708 /* Given two affine expressions "p" of length p_len (including the
709 * denominator and the constant term) and "subs" of length subs_len,
710 * plug in "subs" for the variable at position "pos".
711 * The variables of "subs" and "p" are assumed to match up to subs_len,
712 * but "p" may have additional variables.
713 * "v" is an initialized isl_int that can be used internally.
715 * In particular, if "p" represents the expression
719 * with i the variable at position "pos" and "subs" represents the expression
723 * then the result represents the expression
728 void isl_seq_substitute(isl_int *p, int pos, isl_int *subs,
729 int p_len, int subs_len, isl_int v)
731 isl_int_set(v, p[1 + pos]);
732 isl_int_set_si(p[1 + pos], 0);
733 isl_seq_combine(p + 1, subs[0], p + 1, v, subs + 1, subs_len - 1);
734 isl_seq_scale(p + subs_len, p + subs_len, subs[0], p_len - subs_len);
735 isl_int_mul(p[0], p[0], subs[0]);
738 /* Look for any divs in the aff->ls with a denominator equal to one
739 * and plug them into the affine expression and any subsequent divs
740 * that may reference the div.
742 static __isl_give isl_aff *plug_in_integral_divs(__isl_take isl_aff *aff)
754 n = isl_local_space_dim(aff->ls, isl_dim_div);
756 for (i = 0; i < n; ++i) {
757 if (!isl_int_is_one(aff->ls->div->row[i][0]))
759 ls = isl_local_space_copy(aff->ls);
760 ls = isl_local_space_substitute_seq(ls, isl_dim_div, i,
761 aff->ls->div->row[i], len, i + 1, n - (i + 1));
762 vec = isl_vec_copy(aff->v);
763 vec = isl_vec_cow(vec);
769 pos = isl_local_space_offset(aff->ls, isl_dim_div) + i;
770 isl_seq_substitute(vec->el, pos, aff->ls->div->row[i],
775 isl_vec_free(aff->v);
777 isl_local_space_free(aff->ls);
784 isl_local_space_free(ls);
785 return isl_aff_free(aff);
788 /* Look for any divs j that appear with a unit coefficient inside
789 * the definitions of other divs i and plug them into the definitions
792 * In particular, an expression of the form
794 * floor((f(..) + floor(g(..)/n))/m)
798 * floor((n * f(..) + g(..))/(n * m))
800 * This simplification is correct because we can move the expression
801 * f(..) into the inner floor in the original expression to obtain
803 * floor(floor((n * f(..) + g(..))/n)/m)
805 * from which we can derive the simplified expression.
807 static __isl_give isl_aff *plug_in_unit_divs(__isl_take isl_aff *aff)
815 n = isl_local_space_dim(aff->ls, isl_dim_div);
816 off = isl_local_space_offset(aff->ls, isl_dim_div);
817 for (i = 1; i < n; ++i) {
818 for (j = 0; j < i; ++j) {
819 if (!isl_int_is_one(aff->ls->div->row[i][1 + off + j]))
821 aff->ls = isl_local_space_substitute_seq(aff->ls,
822 isl_dim_div, j, aff->ls->div->row[j],
825 return isl_aff_free(aff);
832 /* Swap divs "a" and "b" in "aff", which is assumed to be non-NULL.
834 * Even though this function is only called on isl_affs with a single
835 * reference, we are careful to only change aff->v and aff->ls together.
837 static __isl_give isl_aff *swap_div(__isl_take isl_aff *aff, int a, int b)
839 unsigned off = isl_local_space_offset(aff->ls, isl_dim_div);
843 ls = isl_local_space_copy(aff->ls);
844 ls = isl_local_space_swap_div(ls, a, b);
845 v = isl_vec_copy(aff->v);
850 isl_int_swap(v->el[1 + off + a], v->el[1 + off + b]);
851 isl_vec_free(aff->v);
853 isl_local_space_free(aff->ls);
859 isl_local_space_free(ls);
860 return isl_aff_free(aff);
863 /* Merge divs "a" and "b" in "aff", which is assumed to be non-NULL.
865 * We currently do not actually remove div "b", but simply add its
866 * coefficient to that of "a" and then zero it out.
868 static __isl_give isl_aff *merge_divs(__isl_take isl_aff *aff, int a, int b)
870 unsigned off = isl_local_space_offset(aff->ls, isl_dim_div);
872 if (isl_int_is_zero(aff->v->el[1 + off + b]))
875 aff->v = isl_vec_cow(aff->v);
877 return isl_aff_free(aff);
879 isl_int_add(aff->v->el[1 + off + a],
880 aff->v->el[1 + off + a], aff->v->el[1 + off + b]);
881 isl_int_set_si(aff->v->el[1 + off + b], 0);
886 /* Sort the divs in the local space of "aff" according to
887 * the comparison function "cmp_row" in isl_local_space.c,
888 * combining the coefficients of identical divs.
890 * Reordering divs does not change the semantics of "aff",
891 * so there is no need to call isl_aff_cow.
892 * Moreover, this function is currently only called on isl_affs
893 * with a single reference.
895 static __isl_give isl_aff *sort_divs(__isl_take isl_aff *aff)
903 off = isl_local_space_offset(aff->ls, isl_dim_div);
904 n = isl_aff_dim(aff, isl_dim_div);
905 for (i = 1; i < n; ++i) {
906 for (j = i - 1; j >= 0; --j) {
907 int cmp = isl_mat_cmp_div(aff->ls->div, j, j + 1);
911 aff = merge_divs(aff, j, j + 1);
913 aff = swap_div(aff, j, j + 1);
922 /* Normalize the representation of "aff".
924 * This function should only be called of "new" isl_affs, i.e.,
925 * with only a single reference. We therefore do not need to
926 * worry about affecting other instances.
928 __isl_give isl_aff *isl_aff_normalize(__isl_take isl_aff *aff)
932 aff->v = isl_vec_normalize(aff->v);
934 return isl_aff_free(aff);
935 aff = plug_in_integral_divs(aff);
936 aff = plug_in_unit_divs(aff);
937 aff = sort_divs(aff);
938 aff = isl_aff_remove_unused_divs(aff);
942 /* Given f, return floor(f).
943 * If f is an integer expression, then just return f.
944 * If f is a constant, then return the constant floor(f).
945 * Otherwise, if f = g/m, write g = q m + r,
946 * create a new div d = [r/m] and return the expression q + d.
947 * The coefficients in r are taken to lie between -m/2 and m/2.
949 __isl_give isl_aff *isl_aff_floor(__isl_take isl_aff *aff)
959 if (isl_int_is_one(aff->v->el[0]))
962 aff = isl_aff_cow(aff);
966 aff->v = isl_vec_cow(aff->v);
968 return isl_aff_free(aff);
970 if (isl_aff_is_cst(aff)) {
971 isl_int_fdiv_q(aff->v->el[1], aff->v->el[1], aff->v->el[0]);
972 isl_int_set_si(aff->v->el[0], 1);
976 div = isl_vec_copy(aff->v);
977 div = isl_vec_cow(div);
979 return isl_aff_free(aff);
981 ctx = isl_aff_get_ctx(aff);
982 isl_int_fdiv_q(aff->v->el[0], aff->v->el[0], ctx->two);
983 for (i = 1; i < aff->v->size; ++i) {
984 isl_int_fdiv_r(div->el[i], div->el[i], div->el[0]);
985 isl_int_fdiv_q(aff->v->el[i], aff->v->el[i], div->el[0]);
986 if (isl_int_gt(div->el[i], aff->v->el[0])) {
987 isl_int_sub(div->el[i], div->el[i], div->el[0]);
988 isl_int_add_ui(aff->v->el[i], aff->v->el[i], 1);
992 aff->ls = isl_local_space_add_div(aff->ls, div);
994 return isl_aff_free(aff);
997 aff->v = isl_vec_extend(aff->v, size + 1);
999 return isl_aff_free(aff);
1000 isl_int_set_si(aff->v->el[0], 1);
1001 isl_int_set_si(aff->v->el[size], 1);
1003 aff = isl_aff_normalize(aff);
1010 * aff mod m = aff - m * floor(aff/m)
1012 __isl_give isl_aff *isl_aff_mod(__isl_take isl_aff *aff, isl_int m)
1016 res = isl_aff_copy(aff);
1017 aff = isl_aff_scale_down(aff, m);
1018 aff = isl_aff_floor(aff);
1019 aff = isl_aff_scale(aff, m);
1020 res = isl_aff_sub(res, aff);
1027 * pwaff mod m = pwaff - m * floor(pwaff/m)
1029 __isl_give isl_pw_aff *isl_pw_aff_mod(__isl_take isl_pw_aff *pwaff, isl_int m)
1033 res = isl_pw_aff_copy(pwaff);
1034 pwaff = isl_pw_aff_scale_down(pwaff, m);
1035 pwaff = isl_pw_aff_floor(pwaff);
1036 pwaff = isl_pw_aff_scale(pwaff, m);
1037 res = isl_pw_aff_sub(res, pwaff);
1042 /* Given f, return ceil(f).
1043 * If f is an integer expression, then just return f.
1044 * Otherwise, let f be the expression
1050 * floor((e + m - 1)/m)
1052 __isl_give isl_aff *isl_aff_ceil(__isl_take isl_aff *aff)
1057 if (isl_int_is_one(aff->v->el[0]))
1060 aff = isl_aff_cow(aff);
1063 aff->v = isl_vec_cow(aff->v);
1065 return isl_aff_free(aff);
1067 isl_int_add(aff->v->el[1], aff->v->el[1], aff->v->el[0]);
1068 isl_int_sub_ui(aff->v->el[1], aff->v->el[1], 1);
1069 aff = isl_aff_floor(aff);
1074 /* Apply the expansion computed by isl_merge_divs.
1075 * The expansion itself is given by "exp" while the resulting
1076 * list of divs is given by "div".
1078 __isl_give isl_aff *isl_aff_expand_divs( __isl_take isl_aff *aff,
1079 __isl_take isl_mat *div, int *exp)
1086 aff = isl_aff_cow(aff);
1090 old_n_div = isl_local_space_dim(aff->ls, isl_dim_div);
1091 new_n_div = isl_mat_rows(div);
1092 if (new_n_div < old_n_div)
1093 isl_die(isl_mat_get_ctx(div), isl_error_invalid,
1094 "not an expansion", goto error);
1096 aff->v = isl_vec_extend(aff->v, aff->v->size + new_n_div - old_n_div);
1100 offset = 1 + isl_local_space_offset(aff->ls, isl_dim_div);
1102 for (i = new_n_div - 1; i >= 0; --i) {
1103 if (j >= 0 && exp[j] == i) {
1105 isl_int_swap(aff->v->el[offset + i],
1106 aff->v->el[offset + j]);
1109 isl_int_set_si(aff->v->el[offset + i], 0);
1112 aff->ls = isl_local_space_replace_divs(aff->ls, isl_mat_copy(div));
1123 /* Add two affine expressions that live in the same local space.
1125 static __isl_give isl_aff *add_expanded(__isl_take isl_aff *aff1,
1126 __isl_take isl_aff *aff2)
1130 aff1 = isl_aff_cow(aff1);
1134 aff1->v = isl_vec_cow(aff1->v);
1140 isl_int_gcd(gcd, aff1->v->el[0], aff2->v->el[0]);
1141 isl_int_divexact(f, aff2->v->el[0], gcd);
1142 isl_seq_scale(aff1->v->el + 1, aff1->v->el + 1, f, aff1->v->size - 1);
1143 isl_int_divexact(f, aff1->v->el[0], gcd);
1144 isl_seq_addmul(aff1->v->el + 1, f, aff2->v->el + 1, aff1->v->size - 1);
1145 isl_int_divexact(f, aff2->v->el[0], gcd);
1146 isl_int_mul(aff1->v->el[0], aff1->v->el[0], f);
1158 __isl_give isl_aff *isl_aff_add(__isl_take isl_aff *aff1,
1159 __isl_take isl_aff *aff2)
1169 ctx = isl_aff_get_ctx(aff1);
1170 if (!isl_space_is_equal(aff1->ls->dim, aff2->ls->dim))
1171 isl_die(ctx, isl_error_invalid,
1172 "spaces don't match", goto error);
1174 if (aff1->ls->div->n_row == 0 && aff2->ls->div->n_row == 0)
1175 return add_expanded(aff1, aff2);
1177 exp1 = isl_alloc_array(ctx, int, aff1->ls->div->n_row);
1178 exp2 = isl_alloc_array(ctx, int, aff2->ls->div->n_row);
1182 div = isl_merge_divs(aff1->ls->div, aff2->ls->div, exp1, exp2);
1183 aff1 = isl_aff_expand_divs(aff1, isl_mat_copy(div), exp1);
1184 aff2 = isl_aff_expand_divs(aff2, div, exp2);
1188 return add_expanded(aff1, aff2);
1197 __isl_give isl_aff *isl_aff_sub(__isl_take isl_aff *aff1,
1198 __isl_take isl_aff *aff2)
1200 return isl_aff_add(aff1, isl_aff_neg(aff2));
1203 __isl_give isl_aff *isl_aff_scale(__isl_take isl_aff *aff, isl_int f)
1207 if (isl_int_is_one(f))
1210 aff = isl_aff_cow(aff);
1213 aff->v = isl_vec_cow(aff->v);
1215 return isl_aff_free(aff);
1217 if (isl_int_is_pos(f) && isl_int_is_divisible_by(aff->v->el[0], f)) {
1218 isl_int_divexact(aff->v->el[0], aff->v->el[0], f);
1223 isl_int_gcd(gcd, aff->v->el[0], f);
1224 isl_int_divexact(aff->v->el[0], aff->v->el[0], gcd);
1225 isl_int_divexact(gcd, f, gcd);
1226 isl_seq_scale(aff->v->el + 1, aff->v->el + 1, gcd, aff->v->size - 1);
1232 __isl_give isl_aff *isl_aff_scale_down(__isl_take isl_aff *aff, isl_int f)
1236 if (isl_int_is_one(f))
1239 aff = isl_aff_cow(aff);
1243 if (isl_int_is_zero(f))
1244 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1245 "cannot scale down by zero", return isl_aff_free(aff));
1247 aff->v = isl_vec_cow(aff->v);
1249 return isl_aff_free(aff);
1252 isl_seq_gcd(aff->v->el + 1, aff->v->size - 1, &gcd);
1253 isl_int_gcd(gcd, gcd, f);
1254 isl_seq_scale_down(aff->v->el + 1, aff->v->el + 1, gcd, aff->v->size - 1);
1255 isl_int_divexact(gcd, f, gcd);
1256 isl_int_mul(aff->v->el[0], aff->v->el[0], gcd);
1262 __isl_give isl_aff *isl_aff_scale_down_ui(__isl_take isl_aff *aff, unsigned f)
1270 isl_int_set_ui(v, f);
1271 aff = isl_aff_scale_down(aff, v);
1277 __isl_give isl_aff *isl_aff_set_dim_name(__isl_take isl_aff *aff,
1278 enum isl_dim_type type, unsigned pos, const char *s)
1280 aff = isl_aff_cow(aff);
1283 if (type == isl_dim_out)
1284 isl_die(aff->v->ctx, isl_error_invalid,
1285 "cannot set name of output/set dimension",
1286 return isl_aff_free(aff));
1287 if (type == isl_dim_in)
1289 aff->ls = isl_local_space_set_dim_name(aff->ls, type, pos, s);
1291 return isl_aff_free(aff);
1296 __isl_give isl_aff *isl_aff_set_dim_id(__isl_take isl_aff *aff,
1297 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
1299 aff = isl_aff_cow(aff);
1301 return isl_id_free(id);
1302 if (type == isl_dim_out)
1303 isl_die(aff->v->ctx, isl_error_invalid,
1304 "cannot set name of output/set dimension",
1306 if (type == isl_dim_in)
1308 aff->ls = isl_local_space_set_dim_id(aff->ls, type, pos, id);
1310 return isl_aff_free(aff);
1319 /* Exploit the equalities in "eq" to simplify the affine expression
1320 * and the expressions of the integer divisions in the local space.
1321 * The integer divisions in this local space are assumed to appear
1322 * as regular dimensions in "eq".
1324 static __isl_give isl_aff *isl_aff_substitute_equalities_lifted(
1325 __isl_take isl_aff *aff, __isl_take isl_basic_set *eq)
1333 if (eq->n_eq == 0) {
1334 isl_basic_set_free(eq);
1338 aff = isl_aff_cow(aff);
1342 aff->ls = isl_local_space_substitute_equalities(aff->ls,
1343 isl_basic_set_copy(eq));
1344 aff->v = isl_vec_cow(aff->v);
1345 if (!aff->ls || !aff->v)
1348 total = 1 + isl_space_dim(eq->dim, isl_dim_all);
1350 for (i = 0; i < eq->n_eq; ++i) {
1351 j = isl_seq_last_non_zero(eq->eq[i], total + n_div);
1352 if (j < 0 || j == 0 || j >= total)
1355 isl_seq_elim(aff->v->el + 1, eq->eq[i], j, total,
1359 isl_basic_set_free(eq);
1360 aff = isl_aff_normalize(aff);
1363 isl_basic_set_free(eq);
1368 /* Exploit the equalities in "eq" to simplify the affine expression
1369 * and the expressions of the integer divisions in the local space.
1371 static __isl_give isl_aff *isl_aff_substitute_equalities(
1372 __isl_take isl_aff *aff, __isl_take isl_basic_set *eq)
1378 n_div = isl_local_space_dim(aff->ls, isl_dim_div);
1380 eq = isl_basic_set_add_dims(eq, isl_dim_set, n_div);
1381 return isl_aff_substitute_equalities_lifted(aff, eq);
1383 isl_basic_set_free(eq);
1388 /* Look for equalities among the variables shared by context and aff
1389 * and the integer divisions of aff, if any.
1390 * The equalities are then used to eliminate coefficients and/or integer
1391 * divisions from aff.
1393 __isl_give isl_aff *isl_aff_gist(__isl_take isl_aff *aff,
1394 __isl_take isl_set *context)
1396 isl_basic_set *hull;
1401 n_div = isl_local_space_dim(aff->ls, isl_dim_div);
1403 isl_basic_set *bset;
1404 isl_local_space *ls;
1405 context = isl_set_add_dims(context, isl_dim_set, n_div);
1406 ls = isl_aff_get_domain_local_space(aff);
1407 bset = isl_basic_set_from_local_space(ls);
1408 bset = isl_basic_set_lift(bset);
1409 bset = isl_basic_set_flatten(bset);
1410 context = isl_set_intersect(context,
1411 isl_set_from_basic_set(bset));
1414 hull = isl_set_affine_hull(context);
1415 return isl_aff_substitute_equalities_lifted(aff, hull);
1418 isl_set_free(context);
1422 __isl_give isl_aff *isl_aff_gist_params(__isl_take isl_aff *aff,
1423 __isl_take isl_set *context)
1425 isl_set *dom_context = isl_set_universe(isl_aff_get_domain_space(aff));
1426 dom_context = isl_set_intersect_params(dom_context, context);
1427 return isl_aff_gist(aff, dom_context);
1430 /* Return a basic set containing those elements in the space
1431 * of aff where it is non-negative.
1432 * If "rational" is set, then return a rational basic set.
1434 static __isl_give isl_basic_set *aff_nonneg_basic_set(
1435 __isl_take isl_aff *aff, int rational)
1437 isl_constraint *ineq;
1438 isl_basic_set *bset;
1440 ineq = isl_inequality_from_aff(aff);
1442 bset = isl_basic_set_from_constraint(ineq);
1444 bset = isl_basic_set_set_rational(bset);
1445 bset = isl_basic_set_simplify(bset);
1449 /* Return a basic set containing those elements in the space
1450 * of aff where it is non-negative.
1452 __isl_give isl_basic_set *isl_aff_nonneg_basic_set(__isl_take isl_aff *aff)
1454 return aff_nonneg_basic_set(aff, 0);
1457 /* Return a basic set containing those elements in the domain space
1458 * of aff where it is negative.
1460 __isl_give isl_basic_set *isl_aff_neg_basic_set(__isl_take isl_aff *aff)
1462 aff = isl_aff_neg(aff);
1463 aff = isl_aff_add_constant_num_si(aff, -1);
1464 return isl_aff_nonneg_basic_set(aff);
1467 /* Return a basic set containing those elements in the space
1468 * of aff where it is zero.
1469 * If "rational" is set, then return a rational basic set.
1471 static __isl_give isl_basic_set *aff_zero_basic_set(__isl_take isl_aff *aff,
1474 isl_constraint *ineq;
1475 isl_basic_set *bset;
1477 ineq = isl_equality_from_aff(aff);
1479 bset = isl_basic_set_from_constraint(ineq);
1481 bset = isl_basic_set_set_rational(bset);
1482 bset = isl_basic_set_simplify(bset);
1486 /* Return a basic set containing those elements in the space
1487 * of aff where it is zero.
1489 __isl_give isl_basic_set *isl_aff_zero_basic_set(__isl_take isl_aff *aff)
1491 return aff_zero_basic_set(aff, 0);
1494 /* Return a basic set containing those elements in the shared space
1495 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
1497 __isl_give isl_basic_set *isl_aff_ge_basic_set(__isl_take isl_aff *aff1,
1498 __isl_take isl_aff *aff2)
1500 aff1 = isl_aff_sub(aff1, aff2);
1502 return isl_aff_nonneg_basic_set(aff1);
1505 /* Return a basic set containing those elements in the shared space
1506 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
1508 __isl_give isl_basic_set *isl_aff_le_basic_set(__isl_take isl_aff *aff1,
1509 __isl_take isl_aff *aff2)
1511 return isl_aff_ge_basic_set(aff2, aff1);
1514 __isl_give isl_aff *isl_aff_add_on_domain(__isl_keep isl_set *dom,
1515 __isl_take isl_aff *aff1, __isl_take isl_aff *aff2)
1517 aff1 = isl_aff_add(aff1, aff2);
1518 aff1 = isl_aff_gist(aff1, isl_set_copy(dom));
1522 int isl_aff_is_empty(__isl_keep isl_aff *aff)
1530 /* Check whether the given affine expression has non-zero coefficient
1531 * for any dimension in the given range or if any of these dimensions
1532 * appear with non-zero coefficients in any of the integer divisions
1533 * involved in the affine expression.
1535 int isl_aff_involves_dims(__isl_keep isl_aff *aff,
1536 enum isl_dim_type type, unsigned first, unsigned n)
1548 ctx = isl_aff_get_ctx(aff);
1549 if (first + n > isl_aff_dim(aff, type))
1550 isl_die(ctx, isl_error_invalid,
1551 "range out of bounds", return -1);
1553 active = isl_local_space_get_active(aff->ls, aff->v->el + 2);
1557 first += isl_local_space_offset(aff->ls, type) - 1;
1558 for (i = 0; i < n; ++i)
1559 if (active[first + i]) {
1572 __isl_give isl_aff *isl_aff_drop_dims(__isl_take isl_aff *aff,
1573 enum isl_dim_type type, unsigned first, unsigned n)
1579 if (type == isl_dim_out)
1580 isl_die(aff->v->ctx, isl_error_invalid,
1581 "cannot drop output/set dimension",
1582 return isl_aff_free(aff));
1583 if (type == isl_dim_in)
1585 if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
1588 ctx = isl_aff_get_ctx(aff);
1589 if (first + n > isl_local_space_dim(aff->ls, type))
1590 isl_die(ctx, isl_error_invalid, "range out of bounds",
1591 return isl_aff_free(aff));
1593 aff = isl_aff_cow(aff);
1597 aff->ls = isl_local_space_drop_dims(aff->ls, type, first, n);
1599 return isl_aff_free(aff);
1601 first += 1 + isl_local_space_offset(aff->ls, type);
1602 aff->v = isl_vec_drop_els(aff->v, first, n);
1604 return isl_aff_free(aff);
1609 /* Project the domain of the affine expression onto its parameter space.
1610 * The affine expression may not involve any of the domain dimensions.
1612 __isl_give isl_aff *isl_aff_project_domain_on_params(__isl_take isl_aff *aff)
1618 n = isl_aff_dim(aff, isl_dim_in);
1619 involves = isl_aff_involves_dims(aff, isl_dim_in, 0, n);
1621 return isl_aff_free(aff);
1623 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1624 "affine expression involves some of the domain dimensions",
1625 return isl_aff_free(aff));
1626 aff = isl_aff_drop_dims(aff, isl_dim_in, 0, n);
1627 space = isl_aff_get_domain_space(aff);
1628 space = isl_space_params(space);
1629 aff = isl_aff_reset_domain_space(aff, space);
1633 __isl_give isl_aff *isl_aff_insert_dims(__isl_take isl_aff *aff,
1634 enum isl_dim_type type, unsigned first, unsigned n)
1640 if (type == isl_dim_out)
1641 isl_die(aff->v->ctx, isl_error_invalid,
1642 "cannot insert output/set dimensions",
1643 return isl_aff_free(aff));
1644 if (type == isl_dim_in)
1646 if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
1649 ctx = isl_aff_get_ctx(aff);
1650 if (first > isl_local_space_dim(aff->ls, type))
1651 isl_die(ctx, isl_error_invalid, "position out of bounds",
1652 return isl_aff_free(aff));
1654 aff = isl_aff_cow(aff);
1658 aff->ls = isl_local_space_insert_dims(aff->ls, type, first, n);
1660 return isl_aff_free(aff);
1662 first += 1 + isl_local_space_offset(aff->ls, type);
1663 aff->v = isl_vec_insert_zero_els(aff->v, first, n);
1665 return isl_aff_free(aff);
1670 __isl_give isl_aff *isl_aff_add_dims(__isl_take isl_aff *aff,
1671 enum isl_dim_type type, unsigned n)
1675 pos = isl_aff_dim(aff, type);
1677 return isl_aff_insert_dims(aff, type, pos, n);
1680 __isl_give isl_pw_aff *isl_pw_aff_add_dims(__isl_take isl_pw_aff *pwaff,
1681 enum isl_dim_type type, unsigned n)
1685 pos = isl_pw_aff_dim(pwaff, type);
1687 return isl_pw_aff_insert_dims(pwaff, type, pos, n);
1690 __isl_give isl_pw_aff *isl_pw_aff_from_aff(__isl_take isl_aff *aff)
1692 isl_set *dom = isl_set_universe(isl_aff_get_domain_space(aff));
1693 return isl_pw_aff_alloc(dom, aff);
1697 #define PW isl_pw_aff
1701 #define EL_IS_ZERO is_empty
1705 #define IS_ZERO is_empty
1708 #undef DEFAULT_IS_ZERO
1709 #define DEFAULT_IS_ZERO 0
1713 #define NO_MOVE_DIMS
1717 #include <isl_pw_templ.c>
1719 static __isl_give isl_set *align_params_pw_pw_set_and(
1720 __isl_take isl_pw_aff *pwaff1, __isl_take isl_pw_aff *pwaff2,
1721 __isl_give isl_set *(*fn)(__isl_take isl_pw_aff *pwaff1,
1722 __isl_take isl_pw_aff *pwaff2))
1724 if (!pwaff1 || !pwaff2)
1726 if (isl_space_match(pwaff1->dim, isl_dim_param,
1727 pwaff2->dim, isl_dim_param))
1728 return fn(pwaff1, pwaff2);
1729 if (!isl_space_has_named_params(pwaff1->dim) ||
1730 !isl_space_has_named_params(pwaff2->dim))
1731 isl_die(isl_pw_aff_get_ctx(pwaff1), isl_error_invalid,
1732 "unaligned unnamed parameters", goto error);
1733 pwaff1 = isl_pw_aff_align_params(pwaff1, isl_pw_aff_get_space(pwaff2));
1734 pwaff2 = isl_pw_aff_align_params(pwaff2, isl_pw_aff_get_space(pwaff1));
1735 return fn(pwaff1, pwaff2);
1737 isl_pw_aff_free(pwaff1);
1738 isl_pw_aff_free(pwaff2);
1742 /* Compute a piecewise quasi-affine expression with a domain that
1743 * is the union of those of pwaff1 and pwaff2 and such that on each
1744 * cell, the quasi-affine expression is the better (according to cmp)
1745 * of those of pwaff1 and pwaff2. If only one of pwaff1 or pwaff2
1746 * is defined on a given cell, then the associated expression
1747 * is the defined one.
1749 static __isl_give isl_pw_aff *pw_aff_union_opt(__isl_take isl_pw_aff *pwaff1,
1750 __isl_take isl_pw_aff *pwaff2,
1751 __isl_give isl_basic_set *(*cmp)(__isl_take isl_aff *aff1,
1752 __isl_take isl_aff *aff2))
1759 if (!pwaff1 || !pwaff2)
1762 ctx = isl_space_get_ctx(pwaff1->dim);
1763 if (!isl_space_is_equal(pwaff1->dim, pwaff2->dim))
1764 isl_die(ctx, isl_error_invalid,
1765 "arguments should live in same space", goto error);
1767 if (isl_pw_aff_is_empty(pwaff1)) {
1768 isl_pw_aff_free(pwaff1);
1772 if (isl_pw_aff_is_empty(pwaff2)) {
1773 isl_pw_aff_free(pwaff2);
1777 n = 2 * (pwaff1->n + 1) * (pwaff2->n + 1);
1778 res = isl_pw_aff_alloc_size(isl_space_copy(pwaff1->dim), n);
1780 for (i = 0; i < pwaff1->n; ++i) {
1781 set = isl_set_copy(pwaff1->p[i].set);
1782 for (j = 0; j < pwaff2->n; ++j) {
1783 struct isl_set *common;
1786 common = isl_set_intersect(
1787 isl_set_copy(pwaff1->p[i].set),
1788 isl_set_copy(pwaff2->p[j].set));
1789 better = isl_set_from_basic_set(cmp(
1790 isl_aff_copy(pwaff2->p[j].aff),
1791 isl_aff_copy(pwaff1->p[i].aff)));
1792 better = isl_set_intersect(common, better);
1793 if (isl_set_plain_is_empty(better)) {
1794 isl_set_free(better);
1797 set = isl_set_subtract(set, isl_set_copy(better));
1799 res = isl_pw_aff_add_piece(res, better,
1800 isl_aff_copy(pwaff2->p[j].aff));
1802 res = isl_pw_aff_add_piece(res, set,
1803 isl_aff_copy(pwaff1->p[i].aff));
1806 for (j = 0; j < pwaff2->n; ++j) {
1807 set = isl_set_copy(pwaff2->p[j].set);
1808 for (i = 0; i < pwaff1->n; ++i)
1809 set = isl_set_subtract(set,
1810 isl_set_copy(pwaff1->p[i].set));
1811 res = isl_pw_aff_add_piece(res, set,
1812 isl_aff_copy(pwaff2->p[j].aff));
1815 isl_pw_aff_free(pwaff1);
1816 isl_pw_aff_free(pwaff2);
1820 isl_pw_aff_free(pwaff1);
1821 isl_pw_aff_free(pwaff2);
1825 /* Compute a piecewise quasi-affine expression with a domain that
1826 * is the union of those of pwaff1 and pwaff2 and such that on each
1827 * cell, the quasi-affine expression is the maximum of those of pwaff1
1828 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
1829 * cell, then the associated expression is the defined one.
1831 static __isl_give isl_pw_aff *pw_aff_union_max(__isl_take isl_pw_aff *pwaff1,
1832 __isl_take isl_pw_aff *pwaff2)
1834 return pw_aff_union_opt(pwaff1, pwaff2, &isl_aff_ge_basic_set);
1837 __isl_give isl_pw_aff *isl_pw_aff_union_max(__isl_take isl_pw_aff *pwaff1,
1838 __isl_take isl_pw_aff *pwaff2)
1840 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2,
1844 /* Compute a piecewise quasi-affine expression with a domain that
1845 * is the union of those of pwaff1 and pwaff2 and such that on each
1846 * cell, the quasi-affine expression is the minimum of those of pwaff1
1847 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
1848 * cell, then the associated expression is the defined one.
1850 static __isl_give isl_pw_aff *pw_aff_union_min(__isl_take isl_pw_aff *pwaff1,
1851 __isl_take isl_pw_aff *pwaff2)
1853 return pw_aff_union_opt(pwaff1, pwaff2, &isl_aff_le_basic_set);
1856 __isl_give isl_pw_aff *isl_pw_aff_union_min(__isl_take isl_pw_aff *pwaff1,
1857 __isl_take isl_pw_aff *pwaff2)
1859 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2,
1863 __isl_give isl_pw_aff *isl_pw_aff_union_opt(__isl_take isl_pw_aff *pwaff1,
1864 __isl_take isl_pw_aff *pwaff2, int max)
1867 return isl_pw_aff_union_max(pwaff1, pwaff2);
1869 return isl_pw_aff_union_min(pwaff1, pwaff2);
1872 /* Construct a map with as domain the domain of pwaff and
1873 * one-dimensional range corresponding to the affine expressions.
1875 static __isl_give isl_map *map_from_pw_aff(__isl_take isl_pw_aff *pwaff)
1884 dim = isl_pw_aff_get_space(pwaff);
1885 map = isl_map_empty(dim);
1887 for (i = 0; i < pwaff->n; ++i) {
1888 isl_basic_map *bmap;
1891 bmap = isl_basic_map_from_aff(isl_aff_copy(pwaff->p[i].aff));
1892 map_i = isl_map_from_basic_map(bmap);
1893 map_i = isl_map_intersect_domain(map_i,
1894 isl_set_copy(pwaff->p[i].set));
1895 map = isl_map_union_disjoint(map, map_i);
1898 isl_pw_aff_free(pwaff);
1903 /* Construct a map with as domain the domain of pwaff and
1904 * one-dimensional range corresponding to the affine expressions.
1906 __isl_give isl_map *isl_map_from_pw_aff(__isl_take isl_pw_aff *pwaff)
1910 if (isl_space_is_set(pwaff->dim))
1911 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
1912 "space of input is not a map",
1913 return isl_pw_aff_free(pwaff));
1914 return map_from_pw_aff(pwaff);
1917 /* Construct a one-dimensional set with as parameter domain
1918 * the domain of pwaff and the single set dimension
1919 * corresponding to the affine expressions.
1921 __isl_give isl_set *isl_set_from_pw_aff(__isl_take isl_pw_aff *pwaff)
1925 if (!isl_space_is_set(pwaff->dim))
1926 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
1927 "space of input is not a set",
1928 return isl_pw_aff_free(pwaff));
1929 return map_from_pw_aff(pwaff);
1932 /* Return a set containing those elements in the domain
1933 * of pwaff where it is non-negative.
1935 __isl_give isl_set *isl_pw_aff_nonneg_set(__isl_take isl_pw_aff *pwaff)
1943 set = isl_set_empty(isl_pw_aff_get_domain_space(pwaff));
1945 for (i = 0; i < pwaff->n; ++i) {
1946 isl_basic_set *bset;
1950 rational = isl_set_has_rational(pwaff->p[i].set);
1951 bset = aff_nonneg_basic_set(isl_aff_copy(pwaff->p[i].aff),
1953 set_i = isl_set_from_basic_set(bset);
1954 set_i = isl_set_intersect(set_i, isl_set_copy(pwaff->p[i].set));
1955 set = isl_set_union_disjoint(set, set_i);
1958 isl_pw_aff_free(pwaff);
1963 /* Return a set containing those elements in the domain
1964 * of pwaff where it is zero (if complement is 0) or not zero
1965 * (if complement is 1).
1967 static __isl_give isl_set *pw_aff_zero_set(__isl_take isl_pw_aff *pwaff,
1976 set = isl_set_empty(isl_pw_aff_get_domain_space(pwaff));
1978 for (i = 0; i < pwaff->n; ++i) {
1979 isl_basic_set *bset;
1980 isl_set *set_i, *zero;
1983 rational = isl_set_has_rational(pwaff->p[i].set);
1984 bset = aff_zero_basic_set(isl_aff_copy(pwaff->p[i].aff),
1986 zero = isl_set_from_basic_set(bset);
1987 set_i = isl_set_copy(pwaff->p[i].set);
1989 set_i = isl_set_subtract(set_i, zero);
1991 set_i = isl_set_intersect(set_i, zero);
1992 set = isl_set_union_disjoint(set, set_i);
1995 isl_pw_aff_free(pwaff);
2000 /* Return a set containing those elements in the domain
2001 * of pwaff where it is zero.
2003 __isl_give isl_set *isl_pw_aff_zero_set(__isl_take isl_pw_aff *pwaff)
2005 return pw_aff_zero_set(pwaff, 0);
2008 /* Return a set containing those elements in the domain
2009 * of pwaff where it is not zero.
2011 __isl_give isl_set *isl_pw_aff_non_zero_set(__isl_take isl_pw_aff *pwaff)
2013 return pw_aff_zero_set(pwaff, 1);
2016 /* Return a set containing those elements in the shared domain
2017 * of pwaff1 and pwaff2 where pwaff1 is greater than (or equal) to pwaff2.
2019 * We compute the difference on the shared domain and then construct
2020 * the set of values where this difference is non-negative.
2021 * If strict is set, we first subtract 1 from the difference.
2022 * If equal is set, we only return the elements where pwaff1 and pwaff2
2025 static __isl_give isl_set *pw_aff_gte_set(__isl_take isl_pw_aff *pwaff1,
2026 __isl_take isl_pw_aff *pwaff2, int strict, int equal)
2028 isl_set *set1, *set2;
2030 set1 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff1));
2031 set2 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff2));
2032 set1 = isl_set_intersect(set1, set2);
2033 pwaff1 = isl_pw_aff_intersect_domain(pwaff1, isl_set_copy(set1));
2034 pwaff2 = isl_pw_aff_intersect_domain(pwaff2, isl_set_copy(set1));
2035 pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_neg(pwaff2));
2038 isl_space *dim = isl_set_get_space(set1);
2040 aff = isl_aff_zero_on_domain(isl_local_space_from_space(dim));
2041 aff = isl_aff_add_constant_si(aff, -1);
2042 pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_alloc(set1, aff));
2047 return isl_pw_aff_zero_set(pwaff1);
2048 return isl_pw_aff_nonneg_set(pwaff1);
2051 /* Return a set containing those elements in the shared domain
2052 * of pwaff1 and pwaff2 where pwaff1 is equal to pwaff2.
2054 static __isl_give isl_set *pw_aff_eq_set(__isl_take isl_pw_aff *pwaff1,
2055 __isl_take isl_pw_aff *pwaff2)
2057 return pw_aff_gte_set(pwaff1, pwaff2, 0, 1);
2060 __isl_give isl_set *isl_pw_aff_eq_set(__isl_take isl_pw_aff *pwaff1,
2061 __isl_take isl_pw_aff *pwaff2)
2063 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_eq_set);
2066 /* Return a set containing those elements in the shared domain
2067 * of pwaff1 and pwaff2 where pwaff1 is greater than or equal to pwaff2.
2069 static __isl_give isl_set *pw_aff_ge_set(__isl_take isl_pw_aff *pwaff1,
2070 __isl_take isl_pw_aff *pwaff2)
2072 return pw_aff_gte_set(pwaff1, pwaff2, 0, 0);
2075 __isl_give isl_set *isl_pw_aff_ge_set(__isl_take isl_pw_aff *pwaff1,
2076 __isl_take isl_pw_aff *pwaff2)
2078 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_ge_set);
2081 /* Return a set containing those elements in the shared domain
2082 * of pwaff1 and pwaff2 where pwaff1 is strictly greater than pwaff2.
2084 static __isl_give isl_set *pw_aff_gt_set(__isl_take isl_pw_aff *pwaff1,
2085 __isl_take isl_pw_aff *pwaff2)
2087 return pw_aff_gte_set(pwaff1, pwaff2, 1, 0);
2090 __isl_give isl_set *isl_pw_aff_gt_set(__isl_take isl_pw_aff *pwaff1,
2091 __isl_take isl_pw_aff *pwaff2)
2093 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_gt_set);
2096 __isl_give isl_set *isl_pw_aff_le_set(__isl_take isl_pw_aff *pwaff1,
2097 __isl_take isl_pw_aff *pwaff2)
2099 return isl_pw_aff_ge_set(pwaff2, pwaff1);
2102 __isl_give isl_set *isl_pw_aff_lt_set(__isl_take isl_pw_aff *pwaff1,
2103 __isl_take isl_pw_aff *pwaff2)
2105 return isl_pw_aff_gt_set(pwaff2, pwaff1);
2108 /* Return a set containing those elements in the shared domain
2109 * of the elements of list1 and list2 where each element in list1
2110 * has the relation specified by "fn" with each element in list2.
2112 static __isl_give isl_set *pw_aff_list_set(__isl_take isl_pw_aff_list *list1,
2113 __isl_take isl_pw_aff_list *list2,
2114 __isl_give isl_set *(*fn)(__isl_take isl_pw_aff *pwaff1,
2115 __isl_take isl_pw_aff *pwaff2))
2121 if (!list1 || !list2)
2124 ctx = isl_pw_aff_list_get_ctx(list1);
2125 if (list1->n < 1 || list2->n < 1)
2126 isl_die(ctx, isl_error_invalid,
2127 "list should contain at least one element", goto error);
2129 set = isl_set_universe(isl_pw_aff_get_domain_space(list1->p[0]));
2130 for (i = 0; i < list1->n; ++i)
2131 for (j = 0; j < list2->n; ++j) {
2134 set_ij = fn(isl_pw_aff_copy(list1->p[i]),
2135 isl_pw_aff_copy(list2->p[j]));
2136 set = isl_set_intersect(set, set_ij);
2139 isl_pw_aff_list_free(list1);
2140 isl_pw_aff_list_free(list2);
2143 isl_pw_aff_list_free(list1);
2144 isl_pw_aff_list_free(list2);
2148 /* Return a set containing those elements in the shared domain
2149 * of the elements of list1 and list2 where each element in list1
2150 * is equal to each element in list2.
2152 __isl_give isl_set *isl_pw_aff_list_eq_set(__isl_take isl_pw_aff_list *list1,
2153 __isl_take isl_pw_aff_list *list2)
2155 return pw_aff_list_set(list1, list2, &isl_pw_aff_eq_set);
2158 __isl_give isl_set *isl_pw_aff_list_ne_set(__isl_take isl_pw_aff_list *list1,
2159 __isl_take isl_pw_aff_list *list2)
2161 return pw_aff_list_set(list1, list2, &isl_pw_aff_ne_set);
2164 /* Return a set containing those elements in the shared domain
2165 * of the elements of list1 and list2 where each element in list1
2166 * is less than or equal to each element in list2.
2168 __isl_give isl_set *isl_pw_aff_list_le_set(__isl_take isl_pw_aff_list *list1,
2169 __isl_take isl_pw_aff_list *list2)
2171 return pw_aff_list_set(list1, list2, &isl_pw_aff_le_set);
2174 __isl_give isl_set *isl_pw_aff_list_lt_set(__isl_take isl_pw_aff_list *list1,
2175 __isl_take isl_pw_aff_list *list2)
2177 return pw_aff_list_set(list1, list2, &isl_pw_aff_lt_set);
2180 __isl_give isl_set *isl_pw_aff_list_ge_set(__isl_take isl_pw_aff_list *list1,
2181 __isl_take isl_pw_aff_list *list2)
2183 return pw_aff_list_set(list1, list2, &isl_pw_aff_ge_set);
2186 __isl_give isl_set *isl_pw_aff_list_gt_set(__isl_take isl_pw_aff_list *list1,
2187 __isl_take isl_pw_aff_list *list2)
2189 return pw_aff_list_set(list1, list2, &isl_pw_aff_gt_set);
2193 /* Return a set containing those elements in the shared domain
2194 * of pwaff1 and pwaff2 where pwaff1 is not equal to pwaff2.
2196 static __isl_give isl_set *pw_aff_ne_set(__isl_take isl_pw_aff *pwaff1,
2197 __isl_take isl_pw_aff *pwaff2)
2199 isl_set *set_lt, *set_gt;
2201 set_lt = isl_pw_aff_lt_set(isl_pw_aff_copy(pwaff1),
2202 isl_pw_aff_copy(pwaff2));
2203 set_gt = isl_pw_aff_gt_set(pwaff1, pwaff2);
2204 return isl_set_union_disjoint(set_lt, set_gt);
2207 __isl_give isl_set *isl_pw_aff_ne_set(__isl_take isl_pw_aff *pwaff1,
2208 __isl_take isl_pw_aff *pwaff2)
2210 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_ne_set);
2213 __isl_give isl_pw_aff *isl_pw_aff_scale_down(__isl_take isl_pw_aff *pwaff,
2218 if (isl_int_is_one(v))
2220 if (!isl_int_is_pos(v))
2221 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
2222 "factor needs to be positive",
2223 return isl_pw_aff_free(pwaff));
2224 pwaff = isl_pw_aff_cow(pwaff);
2230 for (i = 0; i < pwaff->n; ++i) {
2231 pwaff->p[i].aff = isl_aff_scale_down(pwaff->p[i].aff, v);
2232 if (!pwaff->p[i].aff)
2233 return isl_pw_aff_free(pwaff);
2239 __isl_give isl_pw_aff *isl_pw_aff_floor(__isl_take isl_pw_aff *pwaff)
2243 pwaff = isl_pw_aff_cow(pwaff);
2249 for (i = 0; i < pwaff->n; ++i) {
2250 pwaff->p[i].aff = isl_aff_floor(pwaff->p[i].aff);
2251 if (!pwaff->p[i].aff)
2252 return isl_pw_aff_free(pwaff);
2258 __isl_give isl_pw_aff *isl_pw_aff_ceil(__isl_take isl_pw_aff *pwaff)
2262 pwaff = isl_pw_aff_cow(pwaff);
2268 for (i = 0; i < pwaff->n; ++i) {
2269 pwaff->p[i].aff = isl_aff_ceil(pwaff->p[i].aff);
2270 if (!pwaff->p[i].aff)
2271 return isl_pw_aff_free(pwaff);
2277 /* Assuming that "cond1" and "cond2" are disjoint,
2278 * return an affine expression that is equal to pwaff1 on cond1
2279 * and to pwaff2 on cond2.
2281 static __isl_give isl_pw_aff *isl_pw_aff_select(
2282 __isl_take isl_set *cond1, __isl_take isl_pw_aff *pwaff1,
2283 __isl_take isl_set *cond2, __isl_take isl_pw_aff *pwaff2)
2285 pwaff1 = isl_pw_aff_intersect_domain(pwaff1, cond1);
2286 pwaff2 = isl_pw_aff_intersect_domain(pwaff2, cond2);
2288 return isl_pw_aff_add_disjoint(pwaff1, pwaff2);
2291 /* Return an affine expression that is equal to pwaff_true for elements
2292 * where "cond" is non-zero and to pwaff_false for elements where "cond"
2294 * That is, return cond ? pwaff_true : pwaff_false;
2296 __isl_give isl_pw_aff *isl_pw_aff_cond(__isl_take isl_pw_aff *cond,
2297 __isl_take isl_pw_aff *pwaff_true, __isl_take isl_pw_aff *pwaff_false)
2299 isl_set *cond_true, *cond_false;
2301 cond_true = isl_pw_aff_non_zero_set(isl_pw_aff_copy(cond));
2302 cond_false = isl_pw_aff_zero_set(cond);
2303 return isl_pw_aff_select(cond_true, pwaff_true,
2304 cond_false, pwaff_false);
2307 int isl_aff_is_cst(__isl_keep isl_aff *aff)
2312 return isl_seq_first_non_zero(aff->v->el + 2, aff->v->size - 2) == -1;
2315 /* Check whether pwaff is a piecewise constant.
2317 int isl_pw_aff_is_cst(__isl_keep isl_pw_aff *pwaff)
2324 for (i = 0; i < pwaff->n; ++i) {
2325 int is_cst = isl_aff_is_cst(pwaff->p[i].aff);
2326 if (is_cst < 0 || !is_cst)
2333 __isl_give isl_aff *isl_aff_mul(__isl_take isl_aff *aff1,
2334 __isl_take isl_aff *aff2)
2336 if (!isl_aff_is_cst(aff2) && isl_aff_is_cst(aff1))
2337 return isl_aff_mul(aff2, aff1);
2339 if (!isl_aff_is_cst(aff2))
2340 isl_die(isl_aff_get_ctx(aff1), isl_error_invalid,
2341 "at least one affine expression should be constant",
2344 aff1 = isl_aff_cow(aff1);
2348 aff1 = isl_aff_scale(aff1, aff2->v->el[1]);
2349 aff1 = isl_aff_scale_down(aff1, aff2->v->el[0]);
2359 /* Divide "aff1" by "aff2", assuming "aff2" is a piecewise constant.
2361 __isl_give isl_aff *isl_aff_div(__isl_take isl_aff *aff1,
2362 __isl_take isl_aff *aff2)
2367 is_cst = isl_aff_is_cst(aff2);
2371 isl_die(isl_aff_get_ctx(aff2), isl_error_invalid,
2372 "second argument should be a constant", goto error);
2377 neg = isl_int_is_neg(aff2->v->el[1]);
2379 isl_int_neg(aff2->v->el[0], aff2->v->el[0]);
2380 isl_int_neg(aff2->v->el[1], aff2->v->el[1]);
2383 aff1 = isl_aff_scale(aff1, aff2->v->el[0]);
2384 aff1 = isl_aff_scale_down(aff1, aff2->v->el[1]);
2387 isl_int_neg(aff2->v->el[0], aff2->v->el[0]);
2388 isl_int_neg(aff2->v->el[1], aff2->v->el[1]);
2399 static __isl_give isl_pw_aff *pw_aff_add(__isl_take isl_pw_aff *pwaff1,
2400 __isl_take isl_pw_aff *pwaff2)
2402 return isl_pw_aff_on_shared_domain(pwaff1, pwaff2, &isl_aff_add);
2405 __isl_give isl_pw_aff *isl_pw_aff_add(__isl_take isl_pw_aff *pwaff1,
2406 __isl_take isl_pw_aff *pwaff2)
2408 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_add);
2411 __isl_give isl_pw_aff *isl_pw_aff_union_add(__isl_take isl_pw_aff *pwaff1,
2412 __isl_take isl_pw_aff *pwaff2)
2414 return isl_pw_aff_union_add_(pwaff1, pwaff2);
2417 static __isl_give isl_pw_aff *pw_aff_mul(__isl_take isl_pw_aff *pwaff1,
2418 __isl_take isl_pw_aff *pwaff2)
2420 return isl_pw_aff_on_shared_domain(pwaff1, pwaff2, &isl_aff_mul);
2423 __isl_give isl_pw_aff *isl_pw_aff_mul(__isl_take isl_pw_aff *pwaff1,
2424 __isl_take isl_pw_aff *pwaff2)
2426 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_mul);
2429 static __isl_give isl_pw_aff *pw_aff_div(__isl_take isl_pw_aff *pa1,
2430 __isl_take isl_pw_aff *pa2)
2432 return isl_pw_aff_on_shared_domain(pa1, pa2, &isl_aff_div);
2435 /* Divide "pa1" by "pa2", assuming "pa2" is a piecewise constant.
2437 __isl_give isl_pw_aff *isl_pw_aff_div(__isl_take isl_pw_aff *pa1,
2438 __isl_take isl_pw_aff *pa2)
2442 is_cst = isl_pw_aff_is_cst(pa2);
2446 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
2447 "second argument should be a piecewise constant",
2449 return isl_pw_aff_align_params_pw_pw_and(pa1, pa2, &pw_aff_div);
2451 isl_pw_aff_free(pa1);
2452 isl_pw_aff_free(pa2);
2456 /* Compute the quotient of the integer division of "pa1" by "pa2"
2457 * with rounding towards zero.
2458 * "pa2" is assumed to be a piecewise constant.
2460 * In particular, return
2462 * pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2)
2465 __isl_give isl_pw_aff *isl_pw_aff_tdiv_q(__isl_take isl_pw_aff *pa1,
2466 __isl_take isl_pw_aff *pa2)
2472 is_cst = isl_pw_aff_is_cst(pa2);
2476 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
2477 "second argument should be a piecewise constant",
2480 pa1 = isl_pw_aff_div(pa1, pa2);
2482 cond = isl_pw_aff_nonneg_set(isl_pw_aff_copy(pa1));
2483 f = isl_pw_aff_floor(isl_pw_aff_copy(pa1));
2484 c = isl_pw_aff_ceil(pa1);
2485 return isl_pw_aff_cond(isl_set_indicator_function(cond), f, c);
2487 isl_pw_aff_free(pa1);
2488 isl_pw_aff_free(pa2);
2492 /* Compute the remainder of the integer division of "pa1" by "pa2"
2493 * with rounding towards zero.
2494 * "pa2" is assumed to be a piecewise constant.
2496 * In particular, return
2498 * pa1 - pa2 * (pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2))
2501 __isl_give isl_pw_aff *isl_pw_aff_tdiv_r(__isl_take isl_pw_aff *pa1,
2502 __isl_take isl_pw_aff *pa2)
2507 is_cst = isl_pw_aff_is_cst(pa2);
2511 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
2512 "second argument should be a piecewise constant",
2514 res = isl_pw_aff_tdiv_q(isl_pw_aff_copy(pa1), isl_pw_aff_copy(pa2));
2515 res = isl_pw_aff_mul(pa2, res);
2516 res = isl_pw_aff_sub(pa1, res);
2519 isl_pw_aff_free(pa1);
2520 isl_pw_aff_free(pa2);
2524 static __isl_give isl_pw_aff *pw_aff_min(__isl_take isl_pw_aff *pwaff1,
2525 __isl_take isl_pw_aff *pwaff2)
2530 dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1)),
2531 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2)));
2532 le = isl_pw_aff_le_set(isl_pw_aff_copy(pwaff1),
2533 isl_pw_aff_copy(pwaff2));
2534 dom = isl_set_subtract(dom, isl_set_copy(le));
2535 return isl_pw_aff_select(le, pwaff1, dom, pwaff2);
2538 __isl_give isl_pw_aff *isl_pw_aff_min(__isl_take isl_pw_aff *pwaff1,
2539 __isl_take isl_pw_aff *pwaff2)
2541 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_min);
2544 static __isl_give isl_pw_aff *pw_aff_max(__isl_take isl_pw_aff *pwaff1,
2545 __isl_take isl_pw_aff *pwaff2)
2550 dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1)),
2551 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2)));
2552 ge = isl_pw_aff_ge_set(isl_pw_aff_copy(pwaff1),
2553 isl_pw_aff_copy(pwaff2));
2554 dom = isl_set_subtract(dom, isl_set_copy(ge));
2555 return isl_pw_aff_select(ge, pwaff1, dom, pwaff2);
2558 __isl_give isl_pw_aff *isl_pw_aff_max(__isl_take isl_pw_aff *pwaff1,
2559 __isl_take isl_pw_aff *pwaff2)
2561 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_max);
2564 static __isl_give isl_pw_aff *pw_aff_list_reduce(
2565 __isl_take isl_pw_aff_list *list,
2566 __isl_give isl_pw_aff *(*fn)(__isl_take isl_pw_aff *pwaff1,
2567 __isl_take isl_pw_aff *pwaff2))
2576 ctx = isl_pw_aff_list_get_ctx(list);
2578 isl_die(ctx, isl_error_invalid,
2579 "list should contain at least one element",
2580 return isl_pw_aff_list_free(list));
2582 res = isl_pw_aff_copy(list->p[0]);
2583 for (i = 1; i < list->n; ++i)
2584 res = fn(res, isl_pw_aff_copy(list->p[i]));
2586 isl_pw_aff_list_free(list);
2590 /* Return an isl_pw_aff that maps each element in the intersection of the
2591 * domains of the elements of list to the minimal corresponding affine
2594 __isl_give isl_pw_aff *isl_pw_aff_list_min(__isl_take isl_pw_aff_list *list)
2596 return pw_aff_list_reduce(list, &isl_pw_aff_min);
2599 /* Return an isl_pw_aff that maps each element in the intersection of the
2600 * domains of the elements of list to the maximal corresponding affine
2603 __isl_give isl_pw_aff *isl_pw_aff_list_max(__isl_take isl_pw_aff_list *list)
2605 return pw_aff_list_reduce(list, &isl_pw_aff_max);
2608 /* Mark the domains of "pwaff" as rational.
2610 __isl_give isl_pw_aff *isl_pw_aff_set_rational(__isl_take isl_pw_aff *pwaff)
2614 pwaff = isl_pw_aff_cow(pwaff);
2620 for (i = 0; i < pwaff->n; ++i) {
2621 pwaff->p[i].set = isl_set_set_rational(pwaff->p[i].set);
2622 if (!pwaff->p[i].set)
2623 return isl_pw_aff_free(pwaff);
2629 /* Mark the domains of the elements of "list" as rational.
2631 __isl_give isl_pw_aff_list *isl_pw_aff_list_set_rational(
2632 __isl_take isl_pw_aff_list *list)
2642 for (i = 0; i < n; ++i) {
2645 pa = isl_pw_aff_list_get_pw_aff(list, i);
2646 pa = isl_pw_aff_set_rational(pa);
2647 list = isl_pw_aff_list_set_pw_aff(list, i, pa);
2656 #include <isl_multi_templ.c>
2658 /* Create an isl_pw_multi_aff with the given isl_multi_aff on a universe
2661 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_multi_aff(
2662 __isl_take isl_multi_aff *ma)
2664 isl_set *dom = isl_set_universe(isl_multi_aff_get_domain_space(ma));
2665 return isl_pw_multi_aff_alloc(dom, ma);
2668 /* Create a piecewise multi-affine expression in the given space that maps each
2669 * input dimension to the corresponding output dimension.
2671 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_identity(
2672 __isl_take isl_space *space)
2674 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_identity(space));
2677 __isl_give isl_multi_aff *isl_multi_aff_add(__isl_take isl_multi_aff *maff1,
2678 __isl_take isl_multi_aff *maff2)
2680 return isl_multi_aff_bin_op(maff1, maff2, &isl_aff_add);
2683 /* Subtract "ma2" from "ma1" and return the result.
2685 __isl_give isl_multi_aff *isl_multi_aff_sub(__isl_take isl_multi_aff *ma1,
2686 __isl_take isl_multi_aff *ma2)
2688 return isl_multi_aff_bin_op(ma1, ma2, &isl_aff_sub);
2691 /* Given two multi-affine expressions A -> B and C -> D,
2692 * construct a multi-affine expression [A -> C] -> [B -> D].
2694 __isl_give isl_multi_aff *isl_multi_aff_product(
2695 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2)
2701 int in1, in2, out1, out2;
2703 in1 = isl_multi_aff_dim(ma1, isl_dim_in);
2704 in2 = isl_multi_aff_dim(ma2, isl_dim_in);
2705 out1 = isl_multi_aff_dim(ma1, isl_dim_out);
2706 out2 = isl_multi_aff_dim(ma2, isl_dim_out);
2707 space = isl_space_product(isl_multi_aff_get_space(ma1),
2708 isl_multi_aff_get_space(ma2));
2709 res = isl_multi_aff_alloc(isl_space_copy(space));
2710 space = isl_space_domain(space);
2712 for (i = 0; i < out1; ++i) {
2713 aff = isl_multi_aff_get_aff(ma1, i);
2714 aff = isl_aff_insert_dims(aff, isl_dim_in, in1, in2);
2715 aff = isl_aff_reset_domain_space(aff, isl_space_copy(space));
2716 res = isl_multi_aff_set_aff(res, i, aff);
2719 for (i = 0; i < out2; ++i) {
2720 aff = isl_multi_aff_get_aff(ma2, i);
2721 aff = isl_aff_insert_dims(aff, isl_dim_in, 0, in1);
2722 aff = isl_aff_reset_domain_space(aff, isl_space_copy(space));
2723 res = isl_multi_aff_set_aff(res, out1 + i, aff);
2726 isl_space_free(space);
2727 isl_multi_aff_free(ma1);
2728 isl_multi_aff_free(ma2);
2732 /* Exploit the equalities in "eq" to simplify the affine expressions.
2734 static __isl_give isl_multi_aff *isl_multi_aff_substitute_equalities(
2735 __isl_take isl_multi_aff *maff, __isl_take isl_basic_set *eq)
2739 maff = isl_multi_aff_cow(maff);
2743 for (i = 0; i < maff->n; ++i) {
2744 maff->p[i] = isl_aff_substitute_equalities(maff->p[i],
2745 isl_basic_set_copy(eq));
2750 isl_basic_set_free(eq);
2753 isl_basic_set_free(eq);
2754 isl_multi_aff_free(maff);
2758 __isl_give isl_multi_aff *isl_multi_aff_scale(__isl_take isl_multi_aff *maff,
2763 maff = isl_multi_aff_cow(maff);
2767 for (i = 0; i < maff->n; ++i) {
2768 maff->p[i] = isl_aff_scale(maff->p[i], f);
2770 return isl_multi_aff_free(maff);
2776 __isl_give isl_multi_aff *isl_multi_aff_add_on_domain(__isl_keep isl_set *dom,
2777 __isl_take isl_multi_aff *maff1, __isl_take isl_multi_aff *maff2)
2779 maff1 = isl_multi_aff_add(maff1, maff2);
2780 maff1 = isl_multi_aff_gist(maff1, isl_set_copy(dom));
2784 int isl_multi_aff_is_empty(__isl_keep isl_multi_aff *maff)
2792 int isl_multi_aff_plain_is_equal(__isl_keep isl_multi_aff *maff1,
2793 __isl_keep isl_multi_aff *maff2)
2798 if (!maff1 || !maff2)
2800 if (maff1->n != maff2->n)
2802 equal = isl_space_is_equal(maff1->space, maff2->space);
2803 if (equal < 0 || !equal)
2806 for (i = 0; i < maff1->n; ++i) {
2807 equal = isl_aff_plain_is_equal(maff1->p[i], maff2->p[i]);
2808 if (equal < 0 || !equal)
2815 /* Return the set of domain elements where "ma1" is lexicographically
2816 * smaller than or equal to "ma2".
2818 __isl_give isl_set *isl_multi_aff_lex_le_set(__isl_take isl_multi_aff *ma1,
2819 __isl_take isl_multi_aff *ma2)
2821 return isl_multi_aff_lex_ge_set(ma2, ma1);
2824 /* Return the set of domain elements where "ma1" is lexicographically
2825 * greater than or equal to "ma2".
2827 __isl_give isl_set *isl_multi_aff_lex_ge_set(__isl_take isl_multi_aff *ma1,
2828 __isl_take isl_multi_aff *ma2)
2831 isl_map *map1, *map2;
2834 map1 = isl_map_from_multi_aff(ma1);
2835 map2 = isl_map_from_multi_aff(ma2);
2836 map = isl_map_range_product(map1, map2);
2837 space = isl_space_range(isl_map_get_space(map));
2838 space = isl_space_domain(isl_space_unwrap(space));
2839 ge = isl_map_lex_ge(space);
2840 map = isl_map_intersect_range(map, isl_map_wrap(ge));
2842 return isl_map_domain(map);
2846 #define PW isl_pw_multi_aff
2848 #define EL isl_multi_aff
2850 #define EL_IS_ZERO is_empty
2854 #define IS_ZERO is_empty
2857 #undef DEFAULT_IS_ZERO
2858 #define DEFAULT_IS_ZERO 0
2863 #define NO_INVOLVES_DIMS
2864 #define NO_MOVE_DIMS
2865 #define NO_INSERT_DIMS
2869 #include <isl_pw_templ.c>
2872 #define UNION isl_union_pw_multi_aff
2874 #define PART isl_pw_multi_aff
2876 #define PARTS pw_multi_aff
2877 #define ALIGN_DOMAIN
2881 #include <isl_union_templ.c>
2883 /* Given a function "cmp" that returns the set of elements where
2884 * "ma1" is "better" than "ma2", return the intersection of this
2885 * set with "dom1" and "dom2".
2887 static __isl_give isl_set *shared_and_better(__isl_keep isl_set *dom1,
2888 __isl_keep isl_set *dom2, __isl_keep isl_multi_aff *ma1,
2889 __isl_keep isl_multi_aff *ma2,
2890 __isl_give isl_set *(*cmp)(__isl_take isl_multi_aff *ma1,
2891 __isl_take isl_multi_aff *ma2))
2897 common = isl_set_intersect(isl_set_copy(dom1), isl_set_copy(dom2));
2898 is_empty = isl_set_plain_is_empty(common);
2899 if (is_empty >= 0 && is_empty)
2902 return isl_set_free(common);
2903 better = cmp(isl_multi_aff_copy(ma1), isl_multi_aff_copy(ma2));
2904 better = isl_set_intersect(common, better);
2909 /* Given a function "cmp" that returns the set of elements where
2910 * "ma1" is "better" than "ma2", return a piecewise multi affine
2911 * expression defined on the union of the definition domains
2912 * of "pma1" and "pma2" that maps to the "best" of "pma1" and
2913 * "pma2" on each cell. If only one of the two input functions
2914 * is defined on a given cell, then it is considered the best.
2916 static __isl_give isl_pw_multi_aff *pw_multi_aff_union_opt(
2917 __isl_take isl_pw_multi_aff *pma1,
2918 __isl_take isl_pw_multi_aff *pma2,
2919 __isl_give isl_set *(*cmp)(__isl_take isl_multi_aff *ma1,
2920 __isl_take isl_multi_aff *ma2))
2923 isl_pw_multi_aff *res = NULL;
2925 isl_set *set = NULL;
2930 ctx = isl_space_get_ctx(pma1->dim);
2931 if (!isl_space_is_equal(pma1->dim, pma2->dim))
2932 isl_die(ctx, isl_error_invalid,
2933 "arguments should live in the same space", goto error);
2935 if (isl_pw_multi_aff_is_empty(pma1)) {
2936 isl_pw_multi_aff_free(pma1);
2940 if (isl_pw_multi_aff_is_empty(pma2)) {
2941 isl_pw_multi_aff_free(pma2);
2945 n = 2 * (pma1->n + 1) * (pma2->n + 1);
2946 res = isl_pw_multi_aff_alloc_size(isl_space_copy(pma1->dim), n);
2948 for (i = 0; i < pma1->n; ++i) {
2949 set = isl_set_copy(pma1->p[i].set);
2950 for (j = 0; j < pma2->n; ++j) {
2954 better = shared_and_better(pma2->p[j].set,
2955 pma1->p[i].set, pma2->p[j].maff,
2956 pma1->p[i].maff, cmp);
2957 is_empty = isl_set_plain_is_empty(better);
2958 if (is_empty < 0 || is_empty) {
2959 isl_set_free(better);
2964 set = isl_set_subtract(set, isl_set_copy(better));
2966 res = isl_pw_multi_aff_add_piece(res, better,
2967 isl_multi_aff_copy(pma2->p[j].maff));
2969 res = isl_pw_multi_aff_add_piece(res, set,
2970 isl_multi_aff_copy(pma1->p[i].maff));
2973 for (j = 0; j < pma2->n; ++j) {
2974 set = isl_set_copy(pma2->p[j].set);
2975 for (i = 0; i < pma1->n; ++i)
2976 set = isl_set_subtract(set,
2977 isl_set_copy(pma1->p[i].set));
2978 res = isl_pw_multi_aff_add_piece(res, set,
2979 isl_multi_aff_copy(pma2->p[j].maff));
2982 isl_pw_multi_aff_free(pma1);
2983 isl_pw_multi_aff_free(pma2);
2987 isl_pw_multi_aff_free(pma1);
2988 isl_pw_multi_aff_free(pma2);
2990 return isl_pw_multi_aff_free(res);
2993 static __isl_give isl_pw_multi_aff *pw_multi_aff_union_lexmax(
2994 __isl_take isl_pw_multi_aff *pma1,
2995 __isl_take isl_pw_multi_aff *pma2)
2997 return pw_multi_aff_union_opt(pma1, pma2, &isl_multi_aff_lex_ge_set);
3000 /* Given two piecewise multi affine expressions, return a piecewise
3001 * multi-affine expression defined on the union of the definition domains
3002 * of the inputs that is equal to the lexicographic maximum of the two
3003 * inputs on each cell. If only one of the two inputs is defined on
3004 * a given cell, then it is considered to be the maximum.
3006 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_lexmax(
3007 __isl_take isl_pw_multi_aff *pma1,
3008 __isl_take isl_pw_multi_aff *pma2)
3010 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
3011 &pw_multi_aff_union_lexmax);
3014 static __isl_give isl_pw_multi_aff *pw_multi_aff_union_lexmin(
3015 __isl_take isl_pw_multi_aff *pma1,
3016 __isl_take isl_pw_multi_aff *pma2)
3018 return pw_multi_aff_union_opt(pma1, pma2, &isl_multi_aff_lex_le_set);
3021 /* Given two piecewise multi affine expressions, return a piecewise
3022 * multi-affine expression defined on the union of the definition domains
3023 * of the inputs that is equal to the lexicographic minimum of the two
3024 * inputs on each cell. If only one of the two inputs is defined on
3025 * a given cell, then it is considered to be the minimum.
3027 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_lexmin(
3028 __isl_take isl_pw_multi_aff *pma1,
3029 __isl_take isl_pw_multi_aff *pma2)
3031 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
3032 &pw_multi_aff_union_lexmin);
3035 static __isl_give isl_pw_multi_aff *pw_multi_aff_add(
3036 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
3038 return isl_pw_multi_aff_on_shared_domain(pma1, pma2,
3039 &isl_multi_aff_add);
3042 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_add(
3043 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
3045 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
3049 static __isl_give isl_pw_multi_aff *pw_multi_aff_sub(
3050 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
3052 return isl_pw_multi_aff_on_shared_domain(pma1, pma2,
3053 &isl_multi_aff_sub);
3056 /* Subtract "pma2" from "pma1" and return the result.
3058 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_sub(
3059 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
3061 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
3065 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_add(
3066 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
3068 return isl_pw_multi_aff_union_add_(pma1, pma2);
3071 /* Given two piecewise multi-affine expressions A -> B and C -> D,
3072 * construct a piecewise multi-affine expression [A -> C] -> [B -> D].
3074 static __isl_give isl_pw_multi_aff *pw_multi_aff_product(
3075 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
3079 isl_pw_multi_aff *res;
3084 n = pma1->n * pma2->n;
3085 space = isl_space_product(isl_space_copy(pma1->dim),
3086 isl_space_copy(pma2->dim));
3087 res = isl_pw_multi_aff_alloc_size(space, n);
3089 for (i = 0; i < pma1->n; ++i) {
3090 for (j = 0; j < pma2->n; ++j) {
3094 domain = isl_set_product(isl_set_copy(pma1->p[i].set),
3095 isl_set_copy(pma2->p[j].set));
3096 ma = isl_multi_aff_product(
3097 isl_multi_aff_copy(pma1->p[i].maff),
3098 isl_multi_aff_copy(pma2->p[i].maff));
3099 res = isl_pw_multi_aff_add_piece(res, domain, ma);
3103 isl_pw_multi_aff_free(pma1);
3104 isl_pw_multi_aff_free(pma2);
3107 isl_pw_multi_aff_free(pma1);
3108 isl_pw_multi_aff_free(pma2);
3112 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_product(
3113 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
3115 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
3116 &pw_multi_aff_product);
3119 /* Construct a map mapping the domain of the piecewise multi-affine expression
3120 * to its range, with each dimension in the range equated to the
3121 * corresponding affine expression on its cell.
3123 __isl_give isl_map *isl_map_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma)
3131 map = isl_map_empty(isl_pw_multi_aff_get_space(pma));
3133 for (i = 0; i < pma->n; ++i) {
3134 isl_multi_aff *maff;
3135 isl_basic_map *bmap;
3138 maff = isl_multi_aff_copy(pma->p[i].maff);
3139 bmap = isl_basic_map_from_multi_aff(maff);
3140 map_i = isl_map_from_basic_map(bmap);
3141 map_i = isl_map_intersect_domain(map_i,
3142 isl_set_copy(pma->p[i].set));
3143 map = isl_map_union_disjoint(map, map_i);
3146 isl_pw_multi_aff_free(pma);
3150 __isl_give isl_set *isl_set_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma)
3155 if (!isl_space_is_set(pma->dim))
3156 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
3157 "isl_pw_multi_aff cannot be converted into an isl_set",
3158 return isl_pw_multi_aff_free(pma));
3160 return isl_map_from_pw_multi_aff(pma);
3163 /* Given a basic map with a single output dimension that is defined
3164 * in terms of the parameters and input dimensions using an equality,
3165 * extract an isl_aff that expresses the output dimension in terms
3166 * of the parameters and input dimensions.
3168 * Since some applications expect the result of isl_pw_multi_aff_from_map
3169 * to only contain integer affine expressions, we compute the floor
3170 * of the expression before returning.
3172 * This function shares some similarities with
3173 * isl_basic_map_has_defining_equality and isl_constraint_get_bound.
3175 static __isl_give isl_aff *extract_isl_aff_from_basic_map(
3176 __isl_take isl_basic_map *bmap)
3181 isl_local_space *ls;
3186 if (isl_basic_map_dim(bmap, isl_dim_out) != 1)
3187 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
3188 "basic map should have a single output dimension",
3190 offset = isl_basic_map_offset(bmap, isl_dim_out);
3191 total = isl_basic_map_total_dim(bmap);
3192 for (i = 0; i < bmap->n_eq; ++i) {
3193 if (isl_int_is_zero(bmap->eq[i][offset]))
3195 if (isl_seq_first_non_zero(bmap->eq[i] + offset + 1,
3196 1 + total - (offset + 1)) != -1)
3200 if (i >= bmap->n_eq)
3201 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
3202 "unable to find suitable equality", goto error);
3203 ls = isl_basic_map_get_local_space(bmap);
3204 aff = isl_aff_alloc(isl_local_space_domain(ls));
3207 if (isl_int_is_neg(bmap->eq[i][offset]))
3208 isl_seq_cpy(aff->v->el + 1, bmap->eq[i], offset);
3210 isl_seq_neg(aff->v->el + 1, bmap->eq[i], offset);
3211 isl_seq_clr(aff->v->el + 1 + offset, aff->v->size - (1 + offset));
3212 isl_int_abs(aff->v->el[0], bmap->eq[i][offset]);
3213 isl_basic_map_free(bmap);
3215 aff = isl_aff_remove_unused_divs(aff);
3216 aff = isl_aff_floor(aff);
3219 isl_basic_map_free(bmap);
3223 /* Given a basic map where each output dimension is defined
3224 * in terms of the parameters and input dimensions using an equality,
3225 * extract an isl_multi_aff that expresses the output dimensions in terms
3226 * of the parameters and input dimensions.
3228 static __isl_give isl_multi_aff *extract_isl_multi_aff_from_basic_map(
3229 __isl_take isl_basic_map *bmap)
3238 ma = isl_multi_aff_alloc(isl_basic_map_get_space(bmap));
3239 n_out = isl_basic_map_dim(bmap, isl_dim_out);
3241 for (i = 0; i < n_out; ++i) {
3242 isl_basic_map *bmap_i;
3245 bmap_i = isl_basic_map_copy(bmap);
3246 bmap_i = isl_basic_map_project_out(bmap_i, isl_dim_out,
3247 i + 1, n_out - (1 + i));
3248 bmap_i = isl_basic_map_project_out(bmap_i, isl_dim_out, 0, i);
3249 aff = extract_isl_aff_from_basic_map(bmap_i);
3250 ma = isl_multi_aff_set_aff(ma, i, aff);
3253 isl_basic_map_free(bmap);
3258 /* Create an isl_pw_multi_aff that is equivalent to
3259 * isl_map_intersect_domain(isl_map_from_basic_map(bmap), domain).
3260 * The given basic map is such that each output dimension is defined
3261 * in terms of the parameters and input dimensions using an equality.
3263 static __isl_give isl_pw_multi_aff *plain_pw_multi_aff_from_map(
3264 __isl_take isl_set *domain, __isl_take isl_basic_map *bmap)
3268 ma = extract_isl_multi_aff_from_basic_map(bmap);
3269 return isl_pw_multi_aff_alloc(domain, ma);
3272 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
3273 * This obviously only works if the input "map" is single-valued.
3274 * If so, we compute the lexicographic minimum of the image in the form
3275 * of an isl_pw_multi_aff. Since the image is unique, it is equal
3276 * to its lexicographic minimum.
3277 * If the input is not single-valued, we produce an error.
3279 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_base(
3280 __isl_take isl_map *map)
3284 isl_pw_multi_aff *pma;
3286 sv = isl_map_is_single_valued(map);
3290 isl_die(isl_map_get_ctx(map), isl_error_invalid,
3291 "map is not single-valued", goto error);
3292 map = isl_map_make_disjoint(map);
3296 pma = isl_pw_multi_aff_empty(isl_map_get_space(map));
3298 for (i = 0; i < map->n; ++i) {
3299 isl_pw_multi_aff *pma_i;
3300 isl_basic_map *bmap;
3301 bmap = isl_basic_map_copy(map->p[i]);
3302 pma_i = isl_basic_map_lexmin_pw_multi_aff(bmap);
3303 pma = isl_pw_multi_aff_add_disjoint(pma, pma_i);
3313 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
3314 * taking into account that the output dimension at position "d"
3315 * can be represented as
3317 * x = floor((e(...) + c1) / m)
3319 * given that constraint "i" is of the form
3321 * e(...) + c1 - m x >= 0
3324 * Let "map" be of the form
3328 * We construct a mapping
3330 * A -> [A -> x = floor(...)]
3332 * apply that to the map, obtaining
3334 * [A -> x = floor(...)] -> B
3336 * and equate dimension "d" to x.
3337 * We then compute a isl_pw_multi_aff representation of the resulting map
3338 * and plug in the mapping above.
3340 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_div(
3341 __isl_take isl_map *map, __isl_take isl_basic_map *hull, int d, int i)
3345 isl_local_space *ls;
3353 isl_pw_multi_aff *pma;
3356 is_set = isl_map_is_set(map);
3358 offset = isl_basic_map_offset(hull, isl_dim_out);
3359 ctx = isl_map_get_ctx(map);
3360 space = isl_space_domain(isl_map_get_space(map));
3361 n_in = isl_space_dim(space, isl_dim_set);
3362 n = isl_space_dim(space, isl_dim_all);
3364 v = isl_vec_alloc(ctx, 1 + 1 + n);
3366 isl_int_neg(v->el[0], hull->ineq[i][offset + d]);
3367 isl_seq_cpy(v->el + 1, hull->ineq[i], 1 + n);
3369 isl_basic_map_free(hull);
3371 ls = isl_local_space_from_space(isl_space_copy(space));
3372 aff = isl_aff_alloc_vec(ls, v);
3373 aff = isl_aff_floor(aff);
3375 isl_space_free(space);
3376 ma = isl_multi_aff_from_aff(aff);
3378 ma = isl_multi_aff_identity(isl_space_map_from_set(space));
3379 ma = isl_multi_aff_range_product(ma,
3380 isl_multi_aff_from_aff(aff));
3383 insert = isl_map_from_multi_aff(isl_multi_aff_copy(ma));
3384 map = isl_map_apply_domain(map, insert);
3385 map = isl_map_equate(map, isl_dim_in, n_in, isl_dim_out, d);
3386 pma = isl_pw_multi_aff_from_map(map);
3387 pma = isl_pw_multi_aff_pullback_multi_aff(pma, ma);
3392 /* Is constraint "c" of the form
3394 * e(...) + c1 - m x >= 0
3398 * -e(...) + c2 + m x >= 0
3400 * where m > 1 and e only depends on parameters and input dimemnsions?
3402 * "offset" is the offset of the output dimensions
3403 * "pos" is the position of output dimension x.
3405 static int is_potential_div_constraint(isl_int *c, int offset, int d, int total)
3407 if (isl_int_is_zero(c[offset + d]))
3409 if (isl_int_is_one(c[offset + d]))
3411 if (isl_int_is_negone(c[offset + d]))
3413 if (isl_seq_first_non_zero(c + offset, d) != -1)
3415 if (isl_seq_first_non_zero(c + offset + d + 1,
3416 total - (offset + d + 1)) != -1)
3421 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
3423 * As a special case, we first check if there is any pair of constraints,
3424 * shared by all the basic maps in "map" that force a given dimension
3425 * to be equal to the floor of some affine combination of the input dimensions.
3427 * In particular, if we can find two constraints
3429 * e(...) + c1 - m x >= 0 i.e., m x <= e(...) + c1
3433 * -e(...) + c2 + m x >= 0 i.e., m x >= e(...) - c2
3435 * where m > 1 and e only depends on parameters and input dimemnsions,
3438 * c1 + c2 < m i.e., -c2 >= c1 - (m - 1)
3440 * then we know that we can take
3442 * x = floor((e(...) + c1) / m)
3444 * without having to perform any computation.
3446 * Note that we know that
3450 * If c1 + c2 were 0, then we would have detected an equality during
3451 * simplification. If c1 + c2 were negative, then we would have detected
3454 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_check_div(
3455 __isl_take isl_map *map)
3461 isl_basic_map *hull;
3463 hull = isl_map_unshifted_simple_hull(isl_map_copy(map));
3468 dim = isl_map_dim(map, isl_dim_out);
3469 offset = isl_basic_map_offset(hull, isl_dim_out);
3470 total = 1 + isl_basic_map_total_dim(hull);
3472 for (d = 0; d < dim; ++d) {
3473 for (i = 0; i < n; ++i) {
3474 if (!is_potential_div_constraint(hull->ineq[i],
3477 for (j = i + 1; j < n; ++j) {
3478 if (!isl_seq_is_neg(hull->ineq[i] + 1,
3479 hull->ineq[j] + 1, total - 1))
3481 isl_int_add(sum, hull->ineq[i][0],
3483 if (isl_int_abs_lt(sum,
3484 hull->ineq[i][offset + d]))
3491 if (isl_int_is_pos(hull->ineq[j][offset + d]))
3493 return pw_multi_aff_from_map_div(map, hull, d, j);
3497 isl_basic_map_free(hull);
3498 return pw_multi_aff_from_map_base(map);
3501 isl_basic_map_free(hull);
3505 /* Given an affine expression
3507 * [A -> B] -> f(A,B)
3509 * construct an isl_multi_aff
3513 * such that dimension "d" in B' is set to "aff" and the remaining
3514 * dimensions are set equal to the corresponding dimensions in B.
3515 * "n_in" is the dimension of the space A.
3516 * "n_out" is the dimension of the space B.
3518 * If "is_set" is set, then the affine expression is of the form
3522 * and we construct an isl_multi_aff
3526 static __isl_give isl_multi_aff *range_map(__isl_take isl_aff *aff, int d,
3527 unsigned n_in, unsigned n_out, int is_set)
3531 isl_space *space, *space2;
3532 isl_local_space *ls;
3534 space = isl_aff_get_domain_space(aff);
3535 ls = isl_local_space_from_space(isl_space_copy(space));
3536 space2 = isl_space_copy(space);
3538 space2 = isl_space_range(isl_space_unwrap(space2));
3539 space = isl_space_map_from_domain_and_range(space, space2);
3540 ma = isl_multi_aff_alloc(space);
3541 ma = isl_multi_aff_set_aff(ma, d, aff);
3543 for (i = 0; i < n_out; ++i) {
3546 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
3547 isl_dim_set, n_in + i);
3548 ma = isl_multi_aff_set_aff(ma, i, aff);
3551 isl_local_space_free(ls);
3556 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
3557 * taking into account that the dimension at position "d" can be written as
3559 * x = m a + f(..) (1)
3561 * where m is equal to "gcd".
3562 * "i" is the index of the equality in "hull" that defines f(..).
3563 * In particular, the equality is of the form
3565 * f(..) - x + m g(existentials) = 0
3569 * -f(..) + x + m g(existentials) = 0
3571 * We basically plug (1) into "map", resulting in a map with "a"
3572 * in the range instead of "x". The corresponding isl_pw_multi_aff
3573 * defining "a" is then plugged back into (1) to obtain a definition fro "x".
3575 * Specifically, given the input map
3579 * We first wrap it into a set
3583 * and define (1) on top of the corresponding space, resulting in "aff".
3584 * We use this to create an isl_multi_aff that maps the output position "d"
3585 * from "a" to "x", leaving all other (intput and output) dimensions unchanged.
3586 * We plug this into the wrapped map, unwrap the result and compute the
3587 * corresponding isl_pw_multi_aff.
3588 * The result is an expression
3596 * so that we can plug that into "aff", after extending the latter to
3602 * If "map" is actually a set, then there is no "A" space, meaning
3603 * that we do not need to perform any wrapping, and that the result
3604 * of the recursive call is of the form
3608 * which is plugged into a mapping of the form
3612 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_stride(
3613 __isl_take isl_map *map, __isl_take isl_basic_map *hull, int d, int i,
3618 isl_local_space *ls;
3621 isl_pw_multi_aff *pma, *id;
3627 is_set = isl_map_is_set(map);
3629 n_in = isl_basic_map_dim(hull, isl_dim_in);
3630 n_out = isl_basic_map_dim(hull, isl_dim_out);
3631 o_out = isl_basic_map_offset(hull, isl_dim_out);
3636 set = isl_map_wrap(map);
3637 space = isl_space_map_from_set(isl_set_get_space(set));
3638 ma = isl_multi_aff_identity(space);
3639 ls = isl_local_space_from_space(isl_set_get_space(set));
3640 aff = isl_aff_alloc(ls);
3642 isl_int_set_si(aff->v->el[0], 1);
3643 if (isl_int_is_one(hull->eq[i][o_out + d]))
3644 isl_seq_neg(aff->v->el + 1, hull->eq[i],
3647 isl_seq_cpy(aff->v->el + 1, hull->eq[i],
3649 isl_int_set(aff->v->el[1 + o_out + d], gcd);
3651 ma = isl_multi_aff_set_aff(ma, n_in + d, isl_aff_copy(aff));
3652 set = isl_set_preimage_multi_aff(set, ma);
3654 ma = range_map(aff, d, n_in, n_out, is_set);
3659 map = isl_set_unwrap(set);
3660 pma = isl_pw_multi_aff_from_map(set);
3663 space = isl_pw_multi_aff_get_domain_space(pma);
3664 space = isl_space_map_from_set(space);
3665 id = isl_pw_multi_aff_identity(space);
3666 pma = isl_pw_multi_aff_range_product(id, pma);
3668 id = isl_pw_multi_aff_from_multi_aff(ma);
3669 pma = isl_pw_multi_aff_pullback_pw_multi_aff(id, pma);
3671 isl_basic_map_free(hull);
3675 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
3677 * As a special case, we first check if all output dimensions are uniquely
3678 * defined in terms of the parameters and input dimensions over the entire
3679 * domain. If so, we extract the desired isl_pw_multi_aff directly
3680 * from the affine hull of "map" and its domain.
3682 * Otherwise, we check if any of the output dimensions is "strided".
3683 * That is, we check if can be written as
3687 * with m greater than 1, a some combination of existentiall quantified
3688 * variables and f and expression in the parameters and input dimensions.
3689 * If so, we remove the stride in pw_multi_aff_from_map_stride.
3691 * Otherwise, we continue with pw_multi_aff_from_map_check_div for a further
3694 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_map(__isl_take isl_map *map)
3698 isl_basic_map *hull;
3708 hull = isl_map_affine_hull(isl_map_copy(map));
3709 sv = isl_basic_map_plain_is_single_valued(hull);
3711 return plain_pw_multi_aff_from_map(isl_map_domain(map), hull);
3713 hull = isl_basic_map_free(hull);
3717 n_div = isl_basic_map_dim(hull, isl_dim_div);
3718 o_div = isl_basic_map_offset(hull, isl_dim_div);
3721 isl_basic_map_free(hull);
3722 return pw_multi_aff_from_map_check_div(map);
3727 n_out = isl_basic_map_dim(hull, isl_dim_out);
3728 o_out = isl_basic_map_offset(hull, isl_dim_out);
3730 for (i = 0; i < n_out; ++i) {
3731 for (j = 0; j < hull->n_eq; ++j) {
3732 isl_int *eq = hull->eq[j];
3733 isl_pw_multi_aff *res;
3735 if (!isl_int_is_one(eq[o_out + i]) &&
3736 !isl_int_is_negone(eq[o_out + i]))
3738 if (isl_seq_first_non_zero(eq + o_out, i) != -1)
3740 if (isl_seq_first_non_zero(eq + o_out + i + 1,
3741 n_out - (i + 1)) != -1)
3743 isl_seq_gcd(eq + o_div, n_div, &gcd);
3744 if (isl_int_is_zero(gcd))
3746 if (isl_int_is_one(gcd))
3749 res = pw_multi_aff_from_map_stride(map, hull,
3757 isl_basic_map_free(hull);
3758 return pw_multi_aff_from_map_check_div(map);
3764 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_set(__isl_take isl_set *set)
3766 return isl_pw_multi_aff_from_map(set);
3769 /* Convert "map" into an isl_pw_multi_aff (if possible) and
3772 static int pw_multi_aff_from_map(__isl_take isl_map *map, void *user)
3774 isl_union_pw_multi_aff **upma = user;
3775 isl_pw_multi_aff *pma;
3777 pma = isl_pw_multi_aff_from_map(map);
3778 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
3780 return *upma ? 0 : -1;
3783 /* Try and create an isl_union_pw_multi_aff that is equivalent
3784 * to the given isl_union_map.
3785 * The isl_union_map is required to be single-valued in each space.
3786 * Otherwise, an error is produced.
3788 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_map(
3789 __isl_take isl_union_map *umap)
3792 isl_union_pw_multi_aff *upma;
3794 space = isl_union_map_get_space(umap);
3795 upma = isl_union_pw_multi_aff_empty(space);
3796 if (isl_union_map_foreach_map(umap, &pw_multi_aff_from_map, &upma) < 0)
3797 upma = isl_union_pw_multi_aff_free(upma);
3798 isl_union_map_free(umap);
3803 /* Try and create an isl_union_pw_multi_aff that is equivalent
3804 * to the given isl_union_set.
3805 * The isl_union_set is required to be a singleton in each space.
3806 * Otherwise, an error is produced.
3808 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_set(
3809 __isl_take isl_union_set *uset)
3811 return isl_union_pw_multi_aff_from_union_map(uset);
3814 /* Return the piecewise affine expression "set ? 1 : 0".
3816 __isl_give isl_pw_aff *isl_set_indicator_function(__isl_take isl_set *set)
3819 isl_space *space = isl_set_get_space(set);
3820 isl_local_space *ls = isl_local_space_from_space(space);
3821 isl_aff *zero = isl_aff_zero_on_domain(isl_local_space_copy(ls));
3822 isl_aff *one = isl_aff_zero_on_domain(ls);
3824 one = isl_aff_add_constant_si(one, 1);
3825 pa = isl_pw_aff_alloc(isl_set_copy(set), one);
3826 set = isl_set_complement(set);
3827 pa = isl_pw_aff_add_disjoint(pa, isl_pw_aff_alloc(set, zero));
3832 /* Plug in "subs" for dimension "type", "pos" of "aff".
3834 * Let i be the dimension to replace and let "subs" be of the form
3838 * and "aff" of the form
3844 * (a f + d g')/(m d)
3846 * where g' is the result of plugging in "subs" in each of the integer
3849 __isl_give isl_aff *isl_aff_substitute(__isl_take isl_aff *aff,
3850 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
3855 aff = isl_aff_cow(aff);
3857 return isl_aff_free(aff);
3859 ctx = isl_aff_get_ctx(aff);
3860 if (!isl_space_is_equal(aff->ls->dim, subs->ls->dim))
3861 isl_die(ctx, isl_error_invalid,
3862 "spaces don't match", return isl_aff_free(aff));
3863 if (isl_local_space_dim(subs->ls, isl_dim_div) != 0)
3864 isl_die(ctx, isl_error_unsupported,
3865 "cannot handle divs yet", return isl_aff_free(aff));
3867 aff->ls = isl_local_space_substitute(aff->ls, type, pos, subs);
3869 return isl_aff_free(aff);
3871 aff->v = isl_vec_cow(aff->v);
3873 return isl_aff_free(aff);
3875 pos += isl_local_space_offset(aff->ls, type);
3878 isl_seq_substitute(aff->v->el, pos, subs->v->el,
3879 aff->v->size, subs->v->size, v);
3885 /* Plug in "subs" for dimension "type", "pos" in each of the affine
3886 * expressions in "maff".
3888 __isl_give isl_multi_aff *isl_multi_aff_substitute(
3889 __isl_take isl_multi_aff *maff, enum isl_dim_type type, unsigned pos,
3890 __isl_keep isl_aff *subs)
3894 maff = isl_multi_aff_cow(maff);
3896 return isl_multi_aff_free(maff);
3898 if (type == isl_dim_in)
3901 for (i = 0; i < maff->n; ++i) {
3902 maff->p[i] = isl_aff_substitute(maff->p[i], type, pos, subs);
3904 return isl_multi_aff_free(maff);
3910 /* Plug in "subs" for dimension "type", "pos" of "pma".
3912 * pma is of the form
3916 * while subs is of the form
3918 * v' = B_j(v) -> S_j
3920 * Each pair i,j such that C_ij = A_i \cap B_i is non-empty
3921 * has a contribution in the result, in particular
3923 * C_ij(S_j) -> M_i(S_j)
3925 * Note that plugging in S_j in C_ij may also result in an empty set
3926 * and this contribution should simply be discarded.
3928 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_substitute(
3929 __isl_take isl_pw_multi_aff *pma, enum isl_dim_type type, unsigned pos,
3930 __isl_keep isl_pw_aff *subs)
3933 isl_pw_multi_aff *res;
3936 return isl_pw_multi_aff_free(pma);
3938 n = pma->n * subs->n;
3939 res = isl_pw_multi_aff_alloc_size(isl_space_copy(pma->dim), n);
3941 for (i = 0; i < pma->n; ++i) {
3942 for (j = 0; j < subs->n; ++j) {
3944 isl_multi_aff *res_ij;
3947 common = isl_set_intersect(
3948 isl_set_copy(pma->p[i].set),
3949 isl_set_copy(subs->p[j].set));
3950 common = isl_set_substitute(common,
3951 type, pos, subs->p[j].aff);
3952 empty = isl_set_plain_is_empty(common);
3953 if (empty < 0 || empty) {
3954 isl_set_free(common);
3960 res_ij = isl_multi_aff_substitute(
3961 isl_multi_aff_copy(pma->p[i].maff),
3962 type, pos, subs->p[j].aff);
3964 res = isl_pw_multi_aff_add_piece(res, common, res_ij);
3968 isl_pw_multi_aff_free(pma);
3971 isl_pw_multi_aff_free(pma);
3972 isl_pw_multi_aff_free(res);
3976 /* Compute the preimage of the affine expression "src" under "ma"
3977 * and put the result in "dst". If "has_denom" is set (to one),
3978 * then "src" and "dst" have an extra initial denominator.
3979 * "n_div_ma" is the number of existentials in "ma"
3980 * "n_div_bset" is the number of existentials in "src"
3981 * The resulting "dst" (which is assumed to have been allocated by
3982 * the caller) contains coefficients for both sets of existentials,
3983 * first those in "ma" and then those in "src".
3984 * f, c1, c2 and g are temporary objects that have been initialized
3987 * Let src represent the expression
3989 * (a(p) + b x + c(divs))/d
3991 * and let ma represent the expressions
3993 * x_i = (r_i(p) + s_i(y) + t_i(divs'))/m_i
3995 * We start out with the following expression for dst:
3997 * (a(p) + 0 y + 0 divs' + f \sum_i b_i x_i + c(divs))/d
3999 * with the multiplication factor f initially equal to 1.
4000 * For each x_i that we substitute, we multiply the numerator
4001 * (and denominator) of dst by c_1 = m_i and add the numerator
4002 * of the x_i expression multiplied by c_2 = f b_i,
4003 * after removing the common factors of c_1 and c_2.
4004 * The multiplication factor f also needs to be multiplied by c_1
4005 * for the next x_j, j > i.
4007 void isl_seq_preimage(isl_int *dst, isl_int *src,
4008 __isl_keep isl_multi_aff *ma, int n_div_ma, int n_div_bset,
4009 isl_int f, isl_int c1, isl_int c2, isl_int g, int has_denom)
4012 int n_param, n_in, n_out;
4015 n_param = isl_multi_aff_dim(ma, isl_dim_param);
4016 n_in = isl_multi_aff_dim(ma, isl_dim_in);
4017 n_out = isl_multi_aff_dim(ma, isl_dim_out);
4019 o_div_bset = has_denom + 1 + n_param + n_in + n_div_ma;
4021 isl_seq_cpy(dst, src, has_denom + 1 + n_param);
4022 isl_seq_clr(dst + has_denom + 1 + n_param, n_in + n_div_ma);
4023 isl_seq_cpy(dst + o_div_bset,
4024 src + has_denom + 1 + n_param + n_out, n_div_bset);
4026 isl_int_set_si(f, 1);
4028 for (i = 0; i < n_out; ++i) {
4029 if (isl_int_is_zero(src[has_denom + 1 + n_param + i]))
4031 isl_int_set(c1, ma->p[i]->v->el[0]);
4032 isl_int_mul(c2, f, src[has_denom + 1 + n_param + i]);
4033 isl_int_gcd(g, c1, c2);
4034 isl_int_divexact(c1, c1, g);
4035 isl_int_divexact(c2, c2, g);
4037 isl_int_mul(f, f, c1);
4038 isl_seq_combine(dst + has_denom, c1, dst + has_denom,
4039 c2, ma->p[i]->v->el + 1, ma->p[i]->v->size - 1);
4040 isl_seq_scale(dst + o_div_bset,
4041 dst + o_div_bset, c1, n_div_bset);
4043 isl_int_mul(dst[0], dst[0], c1);
4047 /* Compute the pullback of "aff" by the function represented by "ma".
4048 * In other words, plug in "ma" in "aff". The result is an affine expression
4049 * defined over the domain space of "ma".
4051 * If "aff" is represented by
4053 * (a(p) + b x + c(divs))/d
4055 * and ma is represented by
4057 * x = D(p) + F(y) + G(divs')
4059 * then the result is
4061 * (a(p) + b D(p) + b F(y) + b G(divs') + c(divs))/d
4063 * The divs in the local space of the input are similarly adjusted
4064 * through a call to isl_local_space_preimage_multi_aff.
4066 __isl_give isl_aff *isl_aff_pullback_multi_aff(__isl_take isl_aff *aff,
4067 __isl_take isl_multi_aff *ma)
4069 isl_aff *res = NULL;
4070 isl_local_space *ls;
4071 int n_div_aff, n_div_ma;
4072 isl_int f, c1, c2, g;
4074 ma = isl_multi_aff_align_divs(ma);
4078 n_div_aff = isl_aff_dim(aff, isl_dim_div);
4079 n_div_ma = ma->n ? isl_aff_dim(ma->p[0], isl_dim_div) : 0;
4081 ls = isl_aff_get_domain_local_space(aff);
4082 ls = isl_local_space_preimage_multi_aff(ls, isl_multi_aff_copy(ma));
4083 res = isl_aff_alloc(ls);
4092 isl_seq_preimage(res->v->el, aff->v->el, ma, n_div_ma, n_div_aff,
4101 isl_multi_aff_free(ma);
4102 res = isl_aff_normalize(res);
4106 isl_multi_aff_free(ma);
4111 /* Compute the pullback of "ma1" by the function represented by "ma2".
4112 * In other words, plug in "ma2" in "ma1".
4114 __isl_give isl_multi_aff *isl_multi_aff_pullback_multi_aff(
4115 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2)
4118 isl_space *space = NULL;
4120 ma2 = isl_multi_aff_align_divs(ma2);
4121 ma1 = isl_multi_aff_cow(ma1);
4125 space = isl_space_join(isl_multi_aff_get_space(ma2),
4126 isl_multi_aff_get_space(ma1));
4128 for (i = 0; i < ma1->n; ++i) {
4129 ma1->p[i] = isl_aff_pullback_multi_aff(ma1->p[i],
4130 isl_multi_aff_copy(ma2));
4135 ma1 = isl_multi_aff_reset_space(ma1, space);
4136 isl_multi_aff_free(ma2);
4139 isl_space_free(space);
4140 isl_multi_aff_free(ma2);
4141 isl_multi_aff_free(ma1);
4145 /* Extend the local space of "dst" to include the divs
4146 * in the local space of "src".
4148 __isl_give isl_aff *isl_aff_align_divs(__isl_take isl_aff *dst,
4149 __isl_keep isl_aff *src)
4157 return isl_aff_free(dst);
4159 ctx = isl_aff_get_ctx(src);
4160 if (!isl_space_is_equal(src->ls->dim, dst->ls->dim))
4161 isl_die(ctx, isl_error_invalid,
4162 "spaces don't match", goto error);
4164 if (src->ls->div->n_row == 0)
4167 exp1 = isl_alloc_array(ctx, int, src->ls->div->n_row);
4168 exp2 = isl_alloc_array(ctx, int, dst->ls->div->n_row);
4172 div = isl_merge_divs(src->ls->div, dst->ls->div, exp1, exp2);
4173 dst = isl_aff_expand_divs(dst, div, exp2);
4181 return isl_aff_free(dst);
4184 /* Adjust the local spaces of the affine expressions in "maff"
4185 * such that they all have the save divs.
4187 __isl_give isl_multi_aff *isl_multi_aff_align_divs(
4188 __isl_take isl_multi_aff *maff)
4196 maff = isl_multi_aff_cow(maff);
4200 for (i = 1; i < maff->n; ++i)
4201 maff->p[0] = isl_aff_align_divs(maff->p[0], maff->p[i]);
4202 for (i = 1; i < maff->n; ++i) {
4203 maff->p[i] = isl_aff_align_divs(maff->p[i], maff->p[0]);
4205 return isl_multi_aff_free(maff);
4211 __isl_give isl_aff *isl_aff_lift(__isl_take isl_aff *aff)
4213 aff = isl_aff_cow(aff);
4217 aff->ls = isl_local_space_lift(aff->ls);
4219 return isl_aff_free(aff);
4224 /* Lift "maff" to a space with extra dimensions such that the result
4225 * has no more existentially quantified variables.
4226 * If "ls" is not NULL, then *ls is assigned the local space that lies
4227 * at the basis of the lifting applied to "maff".
4229 __isl_give isl_multi_aff *isl_multi_aff_lift(__isl_take isl_multi_aff *maff,
4230 __isl_give isl_local_space **ls)
4244 isl_space *space = isl_multi_aff_get_domain_space(maff);
4245 *ls = isl_local_space_from_space(space);
4247 return isl_multi_aff_free(maff);
4252 maff = isl_multi_aff_cow(maff);
4253 maff = isl_multi_aff_align_divs(maff);
4257 n_div = isl_aff_dim(maff->p[0], isl_dim_div);
4258 space = isl_multi_aff_get_space(maff);
4259 space = isl_space_lift(isl_space_domain(space), n_div);
4260 space = isl_space_extend_domain_with_range(space,
4261 isl_multi_aff_get_space(maff));
4263 return isl_multi_aff_free(maff);
4264 isl_space_free(maff->space);
4265 maff->space = space;
4268 *ls = isl_aff_get_domain_local_space(maff->p[0]);
4270 return isl_multi_aff_free(maff);
4273 for (i = 0; i < maff->n; ++i) {
4274 maff->p[i] = isl_aff_lift(maff->p[i]);
4282 isl_local_space_free(*ls);
4283 return isl_multi_aff_free(maff);
4287 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma".
4289 __isl_give isl_pw_aff *isl_pw_multi_aff_get_pw_aff(
4290 __isl_keep isl_pw_multi_aff *pma, int pos)
4300 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
4301 if (pos < 0 || pos >= n_out)
4302 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
4303 "index out of bounds", return NULL);
4305 space = isl_pw_multi_aff_get_space(pma);
4306 space = isl_space_drop_dims(space, isl_dim_out,
4307 pos + 1, n_out - pos - 1);
4308 space = isl_space_drop_dims(space, isl_dim_out, 0, pos);
4310 pa = isl_pw_aff_alloc_size(space, pma->n);
4311 for (i = 0; i < pma->n; ++i) {
4313 aff = isl_multi_aff_get_aff(pma->p[i].maff, pos);
4314 pa = isl_pw_aff_add_piece(pa, isl_set_copy(pma->p[i].set), aff);
4320 /* Return an isl_pw_multi_aff with the given "set" as domain and
4321 * an unnamed zero-dimensional range.
4323 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_domain(
4324 __isl_take isl_set *set)
4329 space = isl_set_get_space(set);
4330 space = isl_space_from_domain(space);
4331 ma = isl_multi_aff_zero(space);
4332 return isl_pw_multi_aff_alloc(set, ma);
4335 /* Add an isl_pw_multi_aff with the given "set" as domain and
4336 * an unnamed zero-dimensional range to *user.
4338 static int add_pw_multi_aff_from_domain(__isl_take isl_set *set, void *user)
4340 isl_union_pw_multi_aff **upma = user;
4341 isl_pw_multi_aff *pma;
4343 pma = isl_pw_multi_aff_from_domain(set);
4344 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
4349 /* Return an isl_union_pw_multi_aff with the given "uset" as domain and
4350 * an unnamed zero-dimensional range.
4352 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_domain(
4353 __isl_take isl_union_set *uset)
4356 isl_union_pw_multi_aff *upma;
4361 space = isl_union_set_get_space(uset);
4362 upma = isl_union_pw_multi_aff_empty(space);
4364 if (isl_union_set_foreach_set(uset,
4365 &add_pw_multi_aff_from_domain, &upma) < 0)
4368 isl_union_set_free(uset);
4371 isl_union_set_free(uset);
4372 isl_union_pw_multi_aff_free(upma);
4376 /* Convert "pma" to an isl_map and add it to *umap.
4378 static int map_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma, void *user)
4380 isl_union_map **umap = user;
4383 map = isl_map_from_pw_multi_aff(pma);
4384 *umap = isl_union_map_add_map(*umap, map);
4389 /* Construct a union map mapping the domain of the union
4390 * piecewise multi-affine expression to its range, with each dimension
4391 * in the range equated to the corresponding affine expression on its cell.
4393 __isl_give isl_union_map *isl_union_map_from_union_pw_multi_aff(
4394 __isl_take isl_union_pw_multi_aff *upma)
4397 isl_union_map *umap;
4402 space = isl_union_pw_multi_aff_get_space(upma);
4403 umap = isl_union_map_empty(space);
4405 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
4406 &map_from_pw_multi_aff, &umap) < 0)
4409 isl_union_pw_multi_aff_free(upma);
4412 isl_union_pw_multi_aff_free(upma);
4413 isl_union_map_free(umap);
4417 /* Local data for bin_entry and the callback "fn".
4419 struct isl_union_pw_multi_aff_bin_data {
4420 isl_union_pw_multi_aff *upma2;
4421 isl_union_pw_multi_aff *res;
4422 isl_pw_multi_aff *pma;
4423 int (*fn)(void **entry, void *user);
4426 /* Given an isl_pw_multi_aff from upma1, store it in data->pma
4427 * and call data->fn for each isl_pw_multi_aff in data->upma2.
4429 static int bin_entry(void **entry, void *user)
4431 struct isl_union_pw_multi_aff_bin_data *data = user;
4432 isl_pw_multi_aff *pma = *entry;
4435 if (isl_hash_table_foreach(data->upma2->dim->ctx, &data->upma2->table,
4436 data->fn, data) < 0)
4442 /* Call "fn" on each pair of isl_pw_multi_affs in "upma1" and "upma2".
4443 * The isl_pw_multi_aff from upma1 is stored in data->pma (where data is
4444 * passed as user field) and the isl_pw_multi_aff from upma2 is available
4445 * as *entry. The callback should adjust data->res if desired.
4447 static __isl_give isl_union_pw_multi_aff *bin_op(
4448 __isl_take isl_union_pw_multi_aff *upma1,
4449 __isl_take isl_union_pw_multi_aff *upma2,
4450 int (*fn)(void **entry, void *user))
4453 struct isl_union_pw_multi_aff_bin_data data = { NULL, NULL, NULL, fn };
4455 space = isl_union_pw_multi_aff_get_space(upma2);
4456 upma1 = isl_union_pw_multi_aff_align_params(upma1, space);
4457 space = isl_union_pw_multi_aff_get_space(upma1);
4458 upma2 = isl_union_pw_multi_aff_align_params(upma2, space);
4460 if (!upma1 || !upma2)
4464 data.res = isl_union_pw_multi_aff_alloc(isl_space_copy(upma1->dim),
4466 if (isl_hash_table_foreach(upma1->dim->ctx, &upma1->table,
4467 &bin_entry, &data) < 0)
4470 isl_union_pw_multi_aff_free(upma1);
4471 isl_union_pw_multi_aff_free(upma2);
4474 isl_union_pw_multi_aff_free(upma1);
4475 isl_union_pw_multi_aff_free(upma2);
4476 isl_union_pw_multi_aff_free(data.res);
4480 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
4481 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
4483 static __isl_give isl_pw_multi_aff *pw_multi_aff_range_product(
4484 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4488 space = isl_space_range_product(isl_pw_multi_aff_get_space(pma1),
4489 isl_pw_multi_aff_get_space(pma2));
4490 return isl_pw_multi_aff_on_shared_domain_in(pma1, pma2, space,
4491 &isl_multi_aff_range_product);
4494 /* Given two isl_pw_multi_affs A -> B and C -> D,
4495 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
4497 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_range_product(
4498 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4500 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4501 &pw_multi_aff_range_product);
4504 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
4505 * construct an isl_pw_multi_aff (A * C) -> (B, D).
4507 static __isl_give isl_pw_multi_aff *pw_multi_aff_flat_range_product(
4508 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4512 space = isl_space_range_product(isl_pw_multi_aff_get_space(pma1),
4513 isl_pw_multi_aff_get_space(pma2));
4514 space = isl_space_flatten_range(space);
4515 return isl_pw_multi_aff_on_shared_domain_in(pma1, pma2, space,
4516 &isl_multi_aff_flat_range_product);
4519 /* Given two isl_pw_multi_affs A -> B and C -> D,
4520 * construct an isl_pw_multi_aff (A * C) -> (B, D).
4522 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_flat_range_product(
4523 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4525 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4526 &pw_multi_aff_flat_range_product);
4529 /* If data->pma and *entry have the same domain space, then compute
4530 * their flat range product and the result to data->res.
4532 static int flat_range_product_entry(void **entry, void *user)
4534 struct isl_union_pw_multi_aff_bin_data *data = user;
4535 isl_pw_multi_aff *pma2 = *entry;
4537 if (!isl_space_tuple_match(data->pma->dim, isl_dim_in,
4538 pma2->dim, isl_dim_in))
4541 pma2 = isl_pw_multi_aff_flat_range_product(
4542 isl_pw_multi_aff_copy(data->pma),
4543 isl_pw_multi_aff_copy(pma2));
4545 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma2);
4550 /* Given two isl_union_pw_multi_affs A -> B and C -> D,
4551 * construct an isl_union_pw_multi_aff (A * C) -> (B, D).
4553 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_flat_range_product(
4554 __isl_take isl_union_pw_multi_aff *upma1,
4555 __isl_take isl_union_pw_multi_aff *upma2)
4557 return bin_op(upma1, upma2, &flat_range_product_entry);
4560 /* Replace the affine expressions at position "pos" in "pma" by "pa".
4561 * The parameters are assumed to have been aligned.
4563 * The implementation essentially performs an isl_pw_*_on_shared_domain,
4564 * except that it works on two different isl_pw_* types.
4566 static __isl_give isl_pw_multi_aff *pw_multi_aff_set_pw_aff(
4567 __isl_take isl_pw_multi_aff *pma, unsigned pos,
4568 __isl_take isl_pw_aff *pa)
4571 isl_pw_multi_aff *res = NULL;
4576 if (!isl_space_tuple_match(pma->dim, isl_dim_in, pa->dim, isl_dim_in))
4577 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
4578 "domains don't match", goto error);
4579 if (pos >= isl_pw_multi_aff_dim(pma, isl_dim_out))
4580 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
4581 "index out of bounds", goto error);
4584 res = isl_pw_multi_aff_alloc_size(isl_pw_multi_aff_get_space(pma), n);
4586 for (i = 0; i < pma->n; ++i) {
4587 for (j = 0; j < pa->n; ++j) {
4589 isl_multi_aff *res_ij;
4592 common = isl_set_intersect(isl_set_copy(pma->p[i].set),
4593 isl_set_copy(pa->p[j].set));
4594 empty = isl_set_plain_is_empty(common);
4595 if (empty < 0 || empty) {
4596 isl_set_free(common);
4602 res_ij = isl_multi_aff_set_aff(
4603 isl_multi_aff_copy(pma->p[i].maff), pos,
4604 isl_aff_copy(pa->p[j].aff));
4605 res_ij = isl_multi_aff_gist(res_ij,
4606 isl_set_copy(common));
4608 res = isl_pw_multi_aff_add_piece(res, common, res_ij);
4612 isl_pw_multi_aff_free(pma);
4613 isl_pw_aff_free(pa);
4616 isl_pw_multi_aff_free(pma);
4617 isl_pw_aff_free(pa);
4618 return isl_pw_multi_aff_free(res);
4621 /* Replace the affine expressions at position "pos" in "pma" by "pa".
4623 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_set_pw_aff(
4624 __isl_take isl_pw_multi_aff *pma, unsigned pos,
4625 __isl_take isl_pw_aff *pa)
4629 if (isl_space_match(pma->dim, isl_dim_param, pa->dim, isl_dim_param))
4630 return pw_multi_aff_set_pw_aff(pma, pos, pa);
4631 if (!isl_space_has_named_params(pma->dim) ||
4632 !isl_space_has_named_params(pa->dim))
4633 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
4634 "unaligned unnamed parameters", goto error);
4635 pma = isl_pw_multi_aff_align_params(pma, isl_pw_aff_get_space(pa));
4636 pa = isl_pw_aff_align_params(pa, isl_pw_multi_aff_get_space(pma));
4637 return pw_multi_aff_set_pw_aff(pma, pos, pa);
4639 isl_pw_multi_aff_free(pma);
4640 isl_pw_aff_free(pa);
4647 #include <isl_multi_templ.c>
4649 /* Scale the first elements of "ma" by the corresponding elements of "vec".
4651 __isl_give isl_multi_aff *isl_multi_aff_scale_vec(__isl_take isl_multi_aff *ma,
4652 __isl_take isl_vec *vec)
4660 n = isl_multi_aff_dim(ma, isl_dim_out);
4661 if (isl_vec_size(vec) < n)
4662 n = isl_vec_size(vec);
4665 for (i = 0; i < n; ++i) {
4668 isl_vec_get_element(vec, i, &v);
4670 aff = isl_multi_aff_get_aff(ma, i);
4671 aff = isl_aff_scale(aff, v);
4672 ma = isl_multi_aff_set_aff(ma, i, aff);
4680 isl_multi_aff_free(ma);
4684 /* Scale the first elements of "pma" by the corresponding elements of "vec".
4686 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_scale_vec(
4687 __isl_take isl_pw_multi_aff *pma, __isl_take isl_vec *v)
4691 pma = isl_pw_multi_aff_cow(pma);
4695 for (i = 0; i < pma->n; ++i) {
4696 pma->p[i].maff = isl_multi_aff_scale_vec(pma->p[i].maff,
4698 if (!pma->p[i].maff)
4706 isl_pw_multi_aff_free(pma);
4710 /* This function is called for each entry of an isl_union_pw_multi_aff.
4711 * Replace the entry by the result of applying isl_pw_multi_aff_scale_vec
4712 * to the original entry with the isl_vec in "user" as extra argument.
4714 static int union_pw_multi_aff_scale_vec_entry(void **entry, void *user)
4716 isl_pw_multi_aff **pma = (isl_pw_multi_aff **) entry;
4719 *pma = isl_pw_multi_aff_scale_vec(*pma, isl_vec_copy(v));
4726 /* Scale the first elements of "upma" by the corresponding elements of "vec".
4728 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_scale_vec(
4729 __isl_take isl_union_pw_multi_aff *upma, __isl_take isl_vec *v)
4731 upma = isl_union_pw_multi_aff_cow(upma);
4735 if (isl_hash_table_foreach(upma->dim->ctx, &upma->table,
4736 &union_pw_multi_aff_scale_vec_entry, v) < 0)
4743 isl_union_pw_multi_aff_free(upma);