2 * Copyright 2011 INRIA Saclay
3 * Copyright 2011 Sven Verdoolaege
4 * Copyright 2012 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 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_add(
3050 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
3052 return isl_pw_multi_aff_union_add_(pma1, pma2);
3055 /* Given two piecewise multi-affine expressions A -> B and C -> D,
3056 * construct a piecewise multi-affine expression [A -> C] -> [B -> D].
3058 static __isl_give isl_pw_multi_aff *pw_multi_aff_product(
3059 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
3063 isl_pw_multi_aff *res;
3068 n = pma1->n * pma2->n;
3069 space = isl_space_product(isl_space_copy(pma1->dim),
3070 isl_space_copy(pma2->dim));
3071 res = isl_pw_multi_aff_alloc_size(space, n);
3073 for (i = 0; i < pma1->n; ++i) {
3074 for (j = 0; j < pma2->n; ++j) {
3078 domain = isl_set_product(isl_set_copy(pma1->p[i].set),
3079 isl_set_copy(pma2->p[j].set));
3080 ma = isl_multi_aff_product(
3081 isl_multi_aff_copy(pma1->p[i].maff),
3082 isl_multi_aff_copy(pma2->p[i].maff));
3083 res = isl_pw_multi_aff_add_piece(res, domain, ma);
3087 isl_pw_multi_aff_free(pma1);
3088 isl_pw_multi_aff_free(pma2);
3091 isl_pw_multi_aff_free(pma1);
3092 isl_pw_multi_aff_free(pma2);
3096 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_product(
3097 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
3099 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
3100 &pw_multi_aff_product);
3103 /* Construct a map mapping the domain of the piecewise multi-affine expression
3104 * to its range, with each dimension in the range equated to the
3105 * corresponding affine expression on its cell.
3107 __isl_give isl_map *isl_map_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma)
3115 map = isl_map_empty(isl_pw_multi_aff_get_space(pma));
3117 for (i = 0; i < pma->n; ++i) {
3118 isl_multi_aff *maff;
3119 isl_basic_map *bmap;
3122 maff = isl_multi_aff_copy(pma->p[i].maff);
3123 bmap = isl_basic_map_from_multi_aff(maff);
3124 map_i = isl_map_from_basic_map(bmap);
3125 map_i = isl_map_intersect_domain(map_i,
3126 isl_set_copy(pma->p[i].set));
3127 map = isl_map_union_disjoint(map, map_i);
3130 isl_pw_multi_aff_free(pma);
3134 __isl_give isl_set *isl_set_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma)
3139 if (!isl_space_is_set(pma->dim))
3140 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
3141 "isl_pw_multi_aff cannot be converted into an isl_set",
3142 return isl_pw_multi_aff_free(pma));
3144 return isl_map_from_pw_multi_aff(pma);
3147 /* Given a basic map with a single output dimension that is defined
3148 * in terms of the parameters and input dimensions using an equality,
3149 * extract an isl_aff that expresses the output dimension in terms
3150 * of the parameters and input dimensions.
3152 * Since some applications expect the result of isl_pw_multi_aff_from_map
3153 * to only contain integer affine expressions, we compute the floor
3154 * of the expression before returning.
3156 * This function shares some similarities with
3157 * isl_basic_map_has_defining_equality and isl_constraint_get_bound.
3159 static __isl_give isl_aff *extract_isl_aff_from_basic_map(
3160 __isl_take isl_basic_map *bmap)
3165 isl_local_space *ls;
3170 if (isl_basic_map_dim(bmap, isl_dim_out) != 1)
3171 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
3172 "basic map should have a single output dimension",
3174 offset = isl_basic_map_offset(bmap, isl_dim_out);
3175 total = isl_basic_map_total_dim(bmap);
3176 for (i = 0; i < bmap->n_eq; ++i) {
3177 if (isl_int_is_zero(bmap->eq[i][offset]))
3179 if (isl_seq_first_non_zero(bmap->eq[i] + offset + 1,
3180 1 + total - (offset + 1)) != -1)
3184 if (i >= bmap->n_eq)
3185 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
3186 "unable to find suitable equality", goto error);
3187 ls = isl_basic_map_get_local_space(bmap);
3188 aff = isl_aff_alloc(isl_local_space_domain(ls));
3191 if (isl_int_is_neg(bmap->eq[i][offset]))
3192 isl_seq_cpy(aff->v->el + 1, bmap->eq[i], offset);
3194 isl_seq_neg(aff->v->el + 1, bmap->eq[i], offset);
3195 isl_seq_clr(aff->v->el + 1 + offset, aff->v->size - (1 + offset));
3196 isl_int_abs(aff->v->el[0], bmap->eq[i][offset]);
3197 isl_basic_map_free(bmap);
3199 aff = isl_aff_remove_unused_divs(aff);
3200 aff = isl_aff_floor(aff);
3203 isl_basic_map_free(bmap);
3207 /* Given a basic map where each output dimension is defined
3208 * in terms of the parameters and input dimensions using an equality,
3209 * extract an isl_multi_aff that expresses the output dimensions in terms
3210 * of the parameters and input dimensions.
3212 static __isl_give isl_multi_aff *extract_isl_multi_aff_from_basic_map(
3213 __isl_take isl_basic_map *bmap)
3222 ma = isl_multi_aff_alloc(isl_basic_map_get_space(bmap));
3223 n_out = isl_basic_map_dim(bmap, isl_dim_out);
3225 for (i = 0; i < n_out; ++i) {
3226 isl_basic_map *bmap_i;
3229 bmap_i = isl_basic_map_copy(bmap);
3230 bmap_i = isl_basic_map_project_out(bmap_i, isl_dim_out,
3231 i + 1, n_out - (1 + i));
3232 bmap_i = isl_basic_map_project_out(bmap_i, isl_dim_out, 0, i);
3233 aff = extract_isl_aff_from_basic_map(bmap_i);
3234 ma = isl_multi_aff_set_aff(ma, i, aff);
3237 isl_basic_map_free(bmap);
3242 /* Create an isl_pw_multi_aff that is equivalent to
3243 * isl_map_intersect_domain(isl_map_from_basic_map(bmap), domain).
3244 * The given basic map is such that each output dimension is defined
3245 * in terms of the parameters and input dimensions using an equality.
3247 static __isl_give isl_pw_multi_aff *plain_pw_multi_aff_from_map(
3248 __isl_take isl_set *domain, __isl_take isl_basic_map *bmap)
3252 ma = extract_isl_multi_aff_from_basic_map(bmap);
3253 return isl_pw_multi_aff_alloc(domain, ma);
3256 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
3257 * This obviously only works if the input "map" is single-valued.
3258 * If so, we compute the lexicographic minimum of the image in the form
3259 * of an isl_pw_multi_aff. Since the image is unique, it is equal
3260 * to its lexicographic minimum.
3261 * If the input is not single-valued, we produce an error.
3263 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_base(
3264 __isl_take isl_map *map)
3268 isl_pw_multi_aff *pma;
3270 sv = isl_map_is_single_valued(map);
3274 isl_die(isl_map_get_ctx(map), isl_error_invalid,
3275 "map is not single-valued", goto error);
3276 map = isl_map_make_disjoint(map);
3280 pma = isl_pw_multi_aff_empty(isl_map_get_space(map));
3282 for (i = 0; i < map->n; ++i) {
3283 isl_pw_multi_aff *pma_i;
3284 isl_basic_map *bmap;
3285 bmap = isl_basic_map_copy(map->p[i]);
3286 pma_i = isl_basic_map_lexmin_pw_multi_aff(bmap);
3287 pma = isl_pw_multi_aff_add_disjoint(pma, pma_i);
3297 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
3298 * taking into account that the output dimension at position "d"
3299 * can be represented as
3301 * x = floor((e(...) + c1) / m)
3303 * given that constraint "i" is of the form
3305 * e(...) + c1 - m x >= 0
3308 * Let "map" be of the form
3312 * We construct a mapping
3314 * A -> [A -> x = floor(...)]
3316 * apply that to the map, obtaining
3318 * [A -> x = floor(...)] -> B
3320 * and equate dimension "d" to x.
3321 * We then compute a isl_pw_multi_aff representation of the resulting map
3322 * and plug in the mapping above.
3324 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_div(
3325 __isl_take isl_map *map, __isl_take isl_basic_map *hull, int d, int i)
3329 isl_local_space *ls;
3337 isl_pw_multi_aff *pma;
3340 is_set = isl_map_is_set(map);
3342 offset = isl_basic_map_offset(hull, isl_dim_out);
3343 ctx = isl_map_get_ctx(map);
3344 space = isl_space_domain(isl_map_get_space(map));
3345 n_in = isl_space_dim(space, isl_dim_set);
3346 n = isl_space_dim(space, isl_dim_all);
3348 v = isl_vec_alloc(ctx, 1 + 1 + n);
3350 isl_int_neg(v->el[0], hull->ineq[i][offset + d]);
3351 isl_seq_cpy(v->el + 1, hull->ineq[i], 1 + n);
3353 isl_basic_map_free(hull);
3355 ls = isl_local_space_from_space(isl_space_copy(space));
3356 aff = isl_aff_alloc_vec(ls, v);
3357 aff = isl_aff_floor(aff);
3359 isl_space_free(space);
3360 ma = isl_multi_aff_from_aff(aff);
3362 ma = isl_multi_aff_identity(isl_space_map_from_set(space));
3363 ma = isl_multi_aff_range_product(ma,
3364 isl_multi_aff_from_aff(aff));
3367 insert = isl_map_from_multi_aff(isl_multi_aff_copy(ma));
3368 map = isl_map_apply_domain(map, insert);
3369 map = isl_map_equate(map, isl_dim_in, n_in, isl_dim_out, d);
3370 pma = isl_pw_multi_aff_from_map(map);
3371 pma = isl_pw_multi_aff_pullback_multi_aff(pma, ma);
3376 /* Is constraint "c" of the form
3378 * e(...) + c1 - m x >= 0
3382 * -e(...) + c2 + m x >= 0
3384 * where m > 1 and e only depends on parameters and input dimemnsions?
3386 * "offset" is the offset of the output dimensions
3387 * "pos" is the position of output dimension x.
3389 static int is_potential_div_constraint(isl_int *c, int offset, int d, int total)
3391 if (isl_int_is_zero(c[offset + d]))
3393 if (isl_int_is_one(c[offset + d]))
3395 if (isl_int_is_negone(c[offset + d]))
3397 if (isl_seq_first_non_zero(c + offset, d) != -1)
3399 if (isl_seq_first_non_zero(c + offset + d + 1,
3400 total - (offset + d + 1)) != -1)
3405 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
3407 * As a special case, we first check if there is any pair of constraints,
3408 * shared by all the basic maps in "map" that force a given dimension
3409 * to be equal to the floor of some affine combination of the input dimensions.
3411 * In particular, if we can find two constraints
3413 * e(...) + c1 - m x >= 0 i.e., m x <= e(...) + c1
3417 * -e(...) + c2 + m x >= 0 i.e., m x >= e(...) - c2
3419 * where m > 1 and e only depends on parameters and input dimemnsions,
3422 * c1 + c2 < m i.e., -c2 >= c1 - (m - 1)
3424 * then we know that we can take
3426 * x = floor((e(...) + c1) / m)
3428 * without having to perform any computation.
3430 * Note that we know that
3434 * If c1 + c2 were 0, then we would have detected an equality during
3435 * simplification. If c1 + c2 were negative, then we would have detected
3438 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_check_div(
3439 __isl_take isl_map *map)
3445 isl_basic_map *hull;
3447 hull = isl_map_unshifted_simple_hull(isl_map_copy(map));
3452 dim = isl_map_dim(map, isl_dim_out);
3453 offset = isl_basic_map_offset(hull, isl_dim_out);
3454 total = 1 + isl_basic_map_total_dim(hull);
3456 for (d = 0; d < dim; ++d) {
3457 for (i = 0; i < n; ++i) {
3458 if (!is_potential_div_constraint(hull->ineq[i],
3461 for (j = i + 1; j < n; ++j) {
3462 if (!isl_seq_is_neg(hull->ineq[i] + 1,
3463 hull->ineq[j] + 1, total - 1))
3465 isl_int_add(sum, hull->ineq[i][0],
3467 if (isl_int_abs_lt(sum,
3468 hull->ineq[i][offset + d]))
3475 if (isl_int_is_pos(hull->ineq[j][offset + d]))
3477 return pw_multi_aff_from_map_div(map, hull, d, j);
3481 isl_basic_map_free(hull);
3482 return pw_multi_aff_from_map_base(map);
3485 isl_basic_map_free(hull);
3489 /* Given an affine expression
3491 * [A -> B] -> f(A,B)
3493 * construct an isl_multi_aff
3497 * such that dimension "d" in B' is set to "aff" and the remaining
3498 * dimensions are set equal to the corresponding dimensions in B.
3499 * "n_in" is the dimension of the space A.
3500 * "n_out" is the dimension of the space B.
3502 * If "is_set" is set, then the affine expression is of the form
3506 * and we construct an isl_multi_aff
3510 static __isl_give isl_multi_aff *range_map(__isl_take isl_aff *aff, int d,
3511 unsigned n_in, unsigned n_out, int is_set)
3515 isl_space *space, *space2;
3516 isl_local_space *ls;
3518 space = isl_aff_get_domain_space(aff);
3519 ls = isl_local_space_from_space(isl_space_copy(space));
3520 space2 = isl_space_copy(space);
3522 space2 = isl_space_range(isl_space_unwrap(space2));
3523 space = isl_space_map_from_domain_and_range(space, space2);
3524 ma = isl_multi_aff_alloc(space);
3525 ma = isl_multi_aff_set_aff(ma, d, aff);
3527 for (i = 0; i < n_out; ++i) {
3530 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
3531 isl_dim_set, n_in + i);
3532 ma = isl_multi_aff_set_aff(ma, i, aff);
3535 isl_local_space_free(ls);
3540 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
3541 * taking into account that the dimension at position "d" can be written as
3543 * x = m a + f(..) (1)
3545 * where m is equal to "gcd".
3546 * "i" is the index of the equality in "hull" that defines f(..).
3547 * In particular, the equality is of the form
3549 * f(..) - x + m g(existentials) = 0
3553 * -f(..) + x + m g(existentials) = 0
3555 * We basically plug (1) into "map", resulting in a map with "a"
3556 * in the range instead of "x". The corresponding isl_pw_multi_aff
3557 * defining "a" is then plugged back into (1) to obtain a definition fro "x".
3559 * Specifically, given the input map
3563 * We first wrap it into a set
3567 * and define (1) on top of the corresponding space, resulting in "aff".
3568 * We use this to create an isl_multi_aff that maps the output position "d"
3569 * from "a" to "x", leaving all other (intput and output) dimensions unchanged.
3570 * We plug this into the wrapped map, unwrap the result and compute the
3571 * corresponding isl_pw_multi_aff.
3572 * The result is an expression
3580 * so that we can plug that into "aff", after extending the latter to
3586 * If "map" is actually a set, then there is no "A" space, meaning
3587 * that we do not need to perform any wrapping, and that the result
3588 * of the recursive call is of the form
3592 * which is plugged into a mapping of the form
3596 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_stride(
3597 __isl_take isl_map *map, __isl_take isl_basic_map *hull, int d, int i,
3602 isl_local_space *ls;
3605 isl_pw_multi_aff *pma, *id;
3611 is_set = isl_map_is_set(map);
3613 n_in = isl_basic_map_dim(hull, isl_dim_in);
3614 n_out = isl_basic_map_dim(hull, isl_dim_out);
3615 o_out = isl_basic_map_offset(hull, isl_dim_out);
3620 set = isl_map_wrap(map);
3621 space = isl_space_map_from_set(isl_set_get_space(set));
3622 ma = isl_multi_aff_identity(space);
3623 ls = isl_local_space_from_space(isl_set_get_space(set));
3624 aff = isl_aff_alloc(ls);
3626 isl_int_set_si(aff->v->el[0], 1);
3627 if (isl_int_is_one(hull->eq[i][o_out + d]))
3628 isl_seq_neg(aff->v->el + 1, hull->eq[i],
3631 isl_seq_cpy(aff->v->el + 1, hull->eq[i],
3633 isl_int_set(aff->v->el[1 + o_out + d], gcd);
3635 ma = isl_multi_aff_set_aff(ma, n_in + d, isl_aff_copy(aff));
3636 set = isl_set_preimage_multi_aff(set, ma);
3638 ma = range_map(aff, d, n_in, n_out, is_set);
3643 map = isl_set_unwrap(set);
3644 pma = isl_pw_multi_aff_from_map(set);
3647 space = isl_pw_multi_aff_get_domain_space(pma);
3648 space = isl_space_map_from_set(space);
3649 id = isl_pw_multi_aff_identity(space);
3650 pma = isl_pw_multi_aff_range_product(id, pma);
3652 id = isl_pw_multi_aff_from_multi_aff(ma);
3653 pma = isl_pw_multi_aff_pullback_pw_multi_aff(id, pma);
3655 isl_basic_map_free(hull);
3659 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
3661 * As a special case, we first check if all output dimensions are uniquely
3662 * defined in terms of the parameters and input dimensions over the entire
3663 * domain. If so, we extract the desired isl_pw_multi_aff directly
3664 * from the affine hull of "map" and its domain.
3666 * Otherwise, we check if any of the output dimensions is "strided".
3667 * That is, we check if can be written as
3671 * with m greater than 1, a some combination of existentiall quantified
3672 * variables and f and expression in the parameters and input dimensions.
3673 * If so, we remove the stride in pw_multi_aff_from_map_stride.
3675 * Otherwise, we continue with pw_multi_aff_from_map_check_div for a further
3678 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_map(__isl_take isl_map *map)
3682 isl_basic_map *hull;
3692 hull = isl_map_affine_hull(isl_map_copy(map));
3693 sv = isl_basic_map_plain_is_single_valued(hull);
3695 return plain_pw_multi_aff_from_map(isl_map_domain(map), hull);
3697 hull = isl_basic_map_free(hull);
3701 n_div = isl_basic_map_dim(hull, isl_dim_div);
3702 o_div = isl_basic_map_offset(hull, isl_dim_div);
3705 isl_basic_map_free(hull);
3706 return pw_multi_aff_from_map_check_div(map);
3711 n_out = isl_basic_map_dim(hull, isl_dim_out);
3712 o_out = isl_basic_map_offset(hull, isl_dim_out);
3714 for (i = 0; i < n_out; ++i) {
3715 for (j = 0; j < hull->n_eq; ++j) {
3716 isl_int *eq = hull->eq[j];
3717 isl_pw_multi_aff *res;
3719 if (!isl_int_is_one(eq[o_out + i]) &&
3720 !isl_int_is_negone(eq[o_out + i]))
3722 if (isl_seq_first_non_zero(eq + o_out, i) != -1)
3724 if (isl_seq_first_non_zero(eq + o_out + i + 1,
3725 n_out - (i + 1)) != -1)
3727 isl_seq_gcd(eq + o_div, n_div, &gcd);
3728 if (isl_int_is_zero(gcd))
3730 if (isl_int_is_one(gcd))
3733 res = pw_multi_aff_from_map_stride(map, hull,
3741 isl_basic_map_free(hull);
3742 return pw_multi_aff_from_map_check_div(map);
3748 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_set(__isl_take isl_set *set)
3750 return isl_pw_multi_aff_from_map(set);
3753 /* Convert "map" into an isl_pw_multi_aff (if possible) and
3756 static int pw_multi_aff_from_map(__isl_take isl_map *map, void *user)
3758 isl_union_pw_multi_aff **upma = user;
3759 isl_pw_multi_aff *pma;
3761 pma = isl_pw_multi_aff_from_map(map);
3762 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
3764 return *upma ? 0 : -1;
3767 /* Try and create an isl_union_pw_multi_aff that is equivalent
3768 * to the given isl_union_map.
3769 * The isl_union_map is required to be single-valued in each space.
3770 * Otherwise, an error is produced.
3772 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_map(
3773 __isl_take isl_union_map *umap)
3776 isl_union_pw_multi_aff *upma;
3778 space = isl_union_map_get_space(umap);
3779 upma = isl_union_pw_multi_aff_empty(space);
3780 if (isl_union_map_foreach_map(umap, &pw_multi_aff_from_map, &upma) < 0)
3781 upma = isl_union_pw_multi_aff_free(upma);
3782 isl_union_map_free(umap);
3787 /* Try and create an isl_union_pw_multi_aff that is equivalent
3788 * to the given isl_union_set.
3789 * The isl_union_set is required to be a singleton in each space.
3790 * Otherwise, an error is produced.
3792 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_set(
3793 __isl_take isl_union_set *uset)
3795 return isl_union_pw_multi_aff_from_union_map(uset);
3798 /* Return the piecewise affine expression "set ? 1 : 0".
3800 __isl_give isl_pw_aff *isl_set_indicator_function(__isl_take isl_set *set)
3803 isl_space *space = isl_set_get_space(set);
3804 isl_local_space *ls = isl_local_space_from_space(space);
3805 isl_aff *zero = isl_aff_zero_on_domain(isl_local_space_copy(ls));
3806 isl_aff *one = isl_aff_zero_on_domain(ls);
3808 one = isl_aff_add_constant_si(one, 1);
3809 pa = isl_pw_aff_alloc(isl_set_copy(set), one);
3810 set = isl_set_complement(set);
3811 pa = isl_pw_aff_add_disjoint(pa, isl_pw_aff_alloc(set, zero));
3816 /* Plug in "subs" for dimension "type", "pos" of "aff".
3818 * Let i be the dimension to replace and let "subs" be of the form
3822 * and "aff" of the form
3828 * (a f + d g')/(m d)
3830 * where g' is the result of plugging in "subs" in each of the integer
3833 __isl_give isl_aff *isl_aff_substitute(__isl_take isl_aff *aff,
3834 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
3839 aff = isl_aff_cow(aff);
3841 return isl_aff_free(aff);
3843 ctx = isl_aff_get_ctx(aff);
3844 if (!isl_space_is_equal(aff->ls->dim, subs->ls->dim))
3845 isl_die(ctx, isl_error_invalid,
3846 "spaces don't match", return isl_aff_free(aff));
3847 if (isl_local_space_dim(subs->ls, isl_dim_div) != 0)
3848 isl_die(ctx, isl_error_unsupported,
3849 "cannot handle divs yet", return isl_aff_free(aff));
3851 aff->ls = isl_local_space_substitute(aff->ls, type, pos, subs);
3853 return isl_aff_free(aff);
3855 aff->v = isl_vec_cow(aff->v);
3857 return isl_aff_free(aff);
3859 pos += isl_local_space_offset(aff->ls, type);
3862 isl_seq_substitute(aff->v->el, pos, subs->v->el,
3863 aff->v->size, subs->v->size, v);
3869 /* Plug in "subs" for dimension "type", "pos" in each of the affine
3870 * expressions in "maff".
3872 __isl_give isl_multi_aff *isl_multi_aff_substitute(
3873 __isl_take isl_multi_aff *maff, enum isl_dim_type type, unsigned pos,
3874 __isl_keep isl_aff *subs)
3878 maff = isl_multi_aff_cow(maff);
3880 return isl_multi_aff_free(maff);
3882 if (type == isl_dim_in)
3885 for (i = 0; i < maff->n; ++i) {
3886 maff->p[i] = isl_aff_substitute(maff->p[i], type, pos, subs);
3888 return isl_multi_aff_free(maff);
3894 /* Plug in "subs" for dimension "type", "pos" of "pma".
3896 * pma is of the form
3900 * while subs is of the form
3902 * v' = B_j(v) -> S_j
3904 * Each pair i,j such that C_ij = A_i \cap B_i is non-empty
3905 * has a contribution in the result, in particular
3907 * C_ij(S_j) -> M_i(S_j)
3909 * Note that plugging in S_j in C_ij may also result in an empty set
3910 * and this contribution should simply be discarded.
3912 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_substitute(
3913 __isl_take isl_pw_multi_aff *pma, enum isl_dim_type type, unsigned pos,
3914 __isl_keep isl_pw_aff *subs)
3917 isl_pw_multi_aff *res;
3920 return isl_pw_multi_aff_free(pma);
3922 n = pma->n * subs->n;
3923 res = isl_pw_multi_aff_alloc_size(isl_space_copy(pma->dim), n);
3925 for (i = 0; i < pma->n; ++i) {
3926 for (j = 0; j < subs->n; ++j) {
3928 isl_multi_aff *res_ij;
3931 common = isl_set_intersect(
3932 isl_set_copy(pma->p[i].set),
3933 isl_set_copy(subs->p[j].set));
3934 common = isl_set_substitute(common,
3935 type, pos, subs->p[j].aff);
3936 empty = isl_set_plain_is_empty(common);
3937 if (empty < 0 || empty) {
3938 isl_set_free(common);
3944 res_ij = isl_multi_aff_substitute(
3945 isl_multi_aff_copy(pma->p[i].maff),
3946 type, pos, subs->p[j].aff);
3948 res = isl_pw_multi_aff_add_piece(res, common, res_ij);
3952 isl_pw_multi_aff_free(pma);
3955 isl_pw_multi_aff_free(pma);
3956 isl_pw_multi_aff_free(res);
3960 /* Compute the preimage of the affine expression "src" under "ma"
3961 * and put the result in "dst". If "has_denom" is set (to one),
3962 * then "src" and "dst" have an extra initial denominator.
3963 * "n_div_ma" is the number of existentials in "ma"
3964 * "n_div_bset" is the number of existentials in "src"
3965 * The resulting "dst" (which is assumed to have been allocated by
3966 * the caller) contains coefficients for both sets of existentials,
3967 * first those in "ma" and then those in "src".
3968 * f, c1, c2 and g are temporary objects that have been initialized
3971 * Let src represent the expression
3973 * (a(p) + b x + c(divs))/d
3975 * and let ma represent the expressions
3977 * x_i = (r_i(p) + s_i(y) + t_i(divs'))/m_i
3979 * We start out with the following expression for dst:
3981 * (a(p) + 0 y + 0 divs' + f \sum_i b_i x_i + c(divs))/d
3983 * with the multiplication factor f initially equal to 1.
3984 * For each x_i that we substitute, we multiply the numerator
3985 * (and denominator) of dst by c_1 = m_i and add the numerator
3986 * of the x_i expression multiplied by c_2 = f b_i,
3987 * after removing the common factors of c_1 and c_2.
3988 * The multiplication factor f also needs to be multiplied by c_1
3989 * for the next x_j, j > i.
3991 void isl_seq_preimage(isl_int *dst, isl_int *src,
3992 __isl_keep isl_multi_aff *ma, int n_div_ma, int n_div_bset,
3993 isl_int f, isl_int c1, isl_int c2, isl_int g, int has_denom)
3996 int n_param, n_in, n_out;
3999 n_param = isl_multi_aff_dim(ma, isl_dim_param);
4000 n_in = isl_multi_aff_dim(ma, isl_dim_in);
4001 n_out = isl_multi_aff_dim(ma, isl_dim_out);
4003 o_div_bset = has_denom + 1 + n_param + n_in + n_div_ma;
4005 isl_seq_cpy(dst, src, has_denom + 1 + n_param);
4006 isl_seq_clr(dst + has_denom + 1 + n_param, n_in + n_div_ma);
4007 isl_seq_cpy(dst + o_div_bset,
4008 src + has_denom + 1 + n_param + n_out, n_div_bset);
4010 isl_int_set_si(f, 1);
4012 for (i = 0; i < n_out; ++i) {
4013 if (isl_int_is_zero(src[has_denom + 1 + n_param + i]))
4015 isl_int_set(c1, ma->p[i]->v->el[0]);
4016 isl_int_mul(c2, f, src[has_denom + 1 + n_param + i]);
4017 isl_int_gcd(g, c1, c2);
4018 isl_int_divexact(c1, c1, g);
4019 isl_int_divexact(c2, c2, g);
4021 isl_int_mul(f, f, c1);
4022 isl_seq_combine(dst + has_denom, c1, dst + has_denom,
4023 c2, ma->p[i]->v->el + 1, ma->p[i]->v->size - 1);
4024 isl_seq_scale(dst + o_div_bset,
4025 dst + o_div_bset, c1, n_div_bset);
4027 isl_int_mul(dst[0], dst[0], c1);
4031 /* Compute the pullback of "aff" by the function represented by "ma".
4032 * In other words, plug in "ma" in "aff". The result is an affine expression
4033 * defined over the domain space of "ma".
4035 * If "aff" is represented by
4037 * (a(p) + b x + c(divs))/d
4039 * and ma is represented by
4041 * x = D(p) + F(y) + G(divs')
4043 * then the result is
4045 * (a(p) + b D(p) + b F(y) + b G(divs') + c(divs))/d
4047 * The divs in the local space of the input are similarly adjusted
4048 * through a call to isl_local_space_preimage_multi_aff.
4050 __isl_give isl_aff *isl_aff_pullback_multi_aff(__isl_take isl_aff *aff,
4051 __isl_take isl_multi_aff *ma)
4053 isl_aff *res = NULL;
4054 isl_local_space *ls;
4055 int n_div_aff, n_div_ma;
4056 isl_int f, c1, c2, g;
4058 ma = isl_multi_aff_align_divs(ma);
4062 n_div_aff = isl_aff_dim(aff, isl_dim_div);
4063 n_div_ma = ma->n ? isl_aff_dim(ma->p[0], isl_dim_div) : 0;
4065 ls = isl_aff_get_domain_local_space(aff);
4066 ls = isl_local_space_preimage_multi_aff(ls, isl_multi_aff_copy(ma));
4067 res = isl_aff_alloc(ls);
4076 isl_seq_preimage(res->v->el, aff->v->el, ma, n_div_ma, n_div_aff,
4085 isl_multi_aff_free(ma);
4086 res = isl_aff_normalize(res);
4090 isl_multi_aff_free(ma);
4095 /* Compute the pullback of "ma1" by the function represented by "ma2".
4096 * In other words, plug in "ma2" in "ma1".
4098 __isl_give isl_multi_aff *isl_multi_aff_pullback_multi_aff(
4099 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2)
4102 isl_space *space = NULL;
4104 ma2 = isl_multi_aff_align_divs(ma2);
4105 ma1 = isl_multi_aff_cow(ma1);
4109 space = isl_space_join(isl_multi_aff_get_space(ma2),
4110 isl_multi_aff_get_space(ma1));
4112 for (i = 0; i < ma1->n; ++i) {
4113 ma1->p[i] = isl_aff_pullback_multi_aff(ma1->p[i],
4114 isl_multi_aff_copy(ma2));
4119 ma1 = isl_multi_aff_reset_space(ma1, space);
4120 isl_multi_aff_free(ma2);
4123 isl_space_free(space);
4124 isl_multi_aff_free(ma2);
4125 isl_multi_aff_free(ma1);
4129 /* Extend the local space of "dst" to include the divs
4130 * in the local space of "src".
4132 __isl_give isl_aff *isl_aff_align_divs(__isl_take isl_aff *dst,
4133 __isl_keep isl_aff *src)
4141 return isl_aff_free(dst);
4143 ctx = isl_aff_get_ctx(src);
4144 if (!isl_space_is_equal(src->ls->dim, dst->ls->dim))
4145 isl_die(ctx, isl_error_invalid,
4146 "spaces don't match", goto error);
4148 if (src->ls->div->n_row == 0)
4151 exp1 = isl_alloc_array(ctx, int, src->ls->div->n_row);
4152 exp2 = isl_alloc_array(ctx, int, dst->ls->div->n_row);
4156 div = isl_merge_divs(src->ls->div, dst->ls->div, exp1, exp2);
4157 dst = isl_aff_expand_divs(dst, div, exp2);
4165 return isl_aff_free(dst);
4168 /* Adjust the local spaces of the affine expressions in "maff"
4169 * such that they all have the save divs.
4171 __isl_give isl_multi_aff *isl_multi_aff_align_divs(
4172 __isl_take isl_multi_aff *maff)
4180 maff = isl_multi_aff_cow(maff);
4184 for (i = 1; i < maff->n; ++i)
4185 maff->p[0] = isl_aff_align_divs(maff->p[0], maff->p[i]);
4186 for (i = 1; i < maff->n; ++i) {
4187 maff->p[i] = isl_aff_align_divs(maff->p[i], maff->p[0]);
4189 return isl_multi_aff_free(maff);
4195 __isl_give isl_aff *isl_aff_lift(__isl_take isl_aff *aff)
4197 aff = isl_aff_cow(aff);
4201 aff->ls = isl_local_space_lift(aff->ls);
4203 return isl_aff_free(aff);
4208 /* Lift "maff" to a space with extra dimensions such that the result
4209 * has no more existentially quantified variables.
4210 * If "ls" is not NULL, then *ls is assigned the local space that lies
4211 * at the basis of the lifting applied to "maff".
4213 __isl_give isl_multi_aff *isl_multi_aff_lift(__isl_take isl_multi_aff *maff,
4214 __isl_give isl_local_space **ls)
4228 isl_space *space = isl_multi_aff_get_domain_space(maff);
4229 *ls = isl_local_space_from_space(space);
4231 return isl_multi_aff_free(maff);
4236 maff = isl_multi_aff_cow(maff);
4237 maff = isl_multi_aff_align_divs(maff);
4241 n_div = isl_aff_dim(maff->p[0], isl_dim_div);
4242 space = isl_multi_aff_get_space(maff);
4243 space = isl_space_lift(isl_space_domain(space), n_div);
4244 space = isl_space_extend_domain_with_range(space,
4245 isl_multi_aff_get_space(maff));
4247 return isl_multi_aff_free(maff);
4248 isl_space_free(maff->space);
4249 maff->space = space;
4252 *ls = isl_aff_get_domain_local_space(maff->p[0]);
4254 return isl_multi_aff_free(maff);
4257 for (i = 0; i < maff->n; ++i) {
4258 maff->p[i] = isl_aff_lift(maff->p[i]);
4266 isl_local_space_free(*ls);
4267 return isl_multi_aff_free(maff);
4271 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma".
4273 __isl_give isl_pw_aff *isl_pw_multi_aff_get_pw_aff(
4274 __isl_keep isl_pw_multi_aff *pma, int pos)
4284 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
4285 if (pos < 0 || pos >= n_out)
4286 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
4287 "index out of bounds", return NULL);
4289 space = isl_pw_multi_aff_get_space(pma);
4290 space = isl_space_drop_dims(space, isl_dim_out,
4291 pos + 1, n_out - pos - 1);
4292 space = isl_space_drop_dims(space, isl_dim_out, 0, pos);
4294 pa = isl_pw_aff_alloc_size(space, pma->n);
4295 for (i = 0; i < pma->n; ++i) {
4297 aff = isl_multi_aff_get_aff(pma->p[i].maff, pos);
4298 pa = isl_pw_aff_add_piece(pa, isl_set_copy(pma->p[i].set), aff);
4304 /* Return an isl_pw_multi_aff with the given "set" as domain and
4305 * an unnamed zero-dimensional range.
4307 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_domain(
4308 __isl_take isl_set *set)
4313 space = isl_set_get_space(set);
4314 space = isl_space_from_domain(space);
4315 ma = isl_multi_aff_zero(space);
4316 return isl_pw_multi_aff_alloc(set, ma);
4319 /* Add an isl_pw_multi_aff with the given "set" as domain and
4320 * an unnamed zero-dimensional range to *user.
4322 static int add_pw_multi_aff_from_domain(__isl_take isl_set *set, void *user)
4324 isl_union_pw_multi_aff **upma = user;
4325 isl_pw_multi_aff *pma;
4327 pma = isl_pw_multi_aff_from_domain(set);
4328 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
4333 /* Return an isl_union_pw_multi_aff with the given "uset" as domain and
4334 * an unnamed zero-dimensional range.
4336 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_domain(
4337 __isl_take isl_union_set *uset)
4340 isl_union_pw_multi_aff *upma;
4345 space = isl_union_set_get_space(uset);
4346 upma = isl_union_pw_multi_aff_empty(space);
4348 if (isl_union_set_foreach_set(uset,
4349 &add_pw_multi_aff_from_domain, &upma) < 0)
4352 isl_union_set_free(uset);
4355 isl_union_set_free(uset);
4356 isl_union_pw_multi_aff_free(upma);
4360 /* Convert "pma" to an isl_map and add it to *umap.
4362 static int map_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma, void *user)
4364 isl_union_map **umap = user;
4367 map = isl_map_from_pw_multi_aff(pma);
4368 *umap = isl_union_map_add_map(*umap, map);
4373 /* Construct a union map mapping the domain of the union
4374 * piecewise multi-affine expression to its range, with each dimension
4375 * in the range equated to the corresponding affine expression on its cell.
4377 __isl_give isl_union_map *isl_union_map_from_union_pw_multi_aff(
4378 __isl_take isl_union_pw_multi_aff *upma)
4381 isl_union_map *umap;
4386 space = isl_union_pw_multi_aff_get_space(upma);
4387 umap = isl_union_map_empty(space);
4389 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
4390 &map_from_pw_multi_aff, &umap) < 0)
4393 isl_union_pw_multi_aff_free(upma);
4396 isl_union_pw_multi_aff_free(upma);
4397 isl_union_map_free(umap);
4401 /* Local data for bin_entry and the callback "fn".
4403 struct isl_union_pw_multi_aff_bin_data {
4404 isl_union_pw_multi_aff *upma2;
4405 isl_union_pw_multi_aff *res;
4406 isl_pw_multi_aff *pma;
4407 int (*fn)(void **entry, void *user);
4410 /* Given an isl_pw_multi_aff from upma1, store it in data->pma
4411 * and call data->fn for each isl_pw_multi_aff in data->upma2.
4413 static int bin_entry(void **entry, void *user)
4415 struct isl_union_pw_multi_aff_bin_data *data = user;
4416 isl_pw_multi_aff *pma = *entry;
4419 if (isl_hash_table_foreach(data->upma2->dim->ctx, &data->upma2->table,
4420 data->fn, data) < 0)
4426 /* Call "fn" on each pair of isl_pw_multi_affs in "upma1" and "upma2".
4427 * The isl_pw_multi_aff from upma1 is stored in data->pma (where data is
4428 * passed as user field) and the isl_pw_multi_aff from upma2 is available
4429 * as *entry. The callback should adjust data->res if desired.
4431 static __isl_give isl_union_pw_multi_aff *bin_op(
4432 __isl_take isl_union_pw_multi_aff *upma1,
4433 __isl_take isl_union_pw_multi_aff *upma2,
4434 int (*fn)(void **entry, void *user))
4437 struct isl_union_pw_multi_aff_bin_data data = { NULL, NULL, NULL, fn };
4439 space = isl_union_pw_multi_aff_get_space(upma2);
4440 upma1 = isl_union_pw_multi_aff_align_params(upma1, space);
4441 space = isl_union_pw_multi_aff_get_space(upma1);
4442 upma2 = isl_union_pw_multi_aff_align_params(upma2, space);
4444 if (!upma1 || !upma2)
4448 data.res = isl_union_pw_multi_aff_alloc(isl_space_copy(upma1->dim),
4450 if (isl_hash_table_foreach(upma1->dim->ctx, &upma1->table,
4451 &bin_entry, &data) < 0)
4454 isl_union_pw_multi_aff_free(upma1);
4455 isl_union_pw_multi_aff_free(upma2);
4458 isl_union_pw_multi_aff_free(upma1);
4459 isl_union_pw_multi_aff_free(upma2);
4460 isl_union_pw_multi_aff_free(data.res);
4464 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
4465 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
4467 static __isl_give isl_pw_multi_aff *pw_multi_aff_range_product(
4468 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4472 space = isl_space_range_product(isl_pw_multi_aff_get_space(pma1),
4473 isl_pw_multi_aff_get_space(pma2));
4474 return isl_pw_multi_aff_on_shared_domain_in(pma1, pma2, space,
4475 &isl_multi_aff_range_product);
4478 /* Given two isl_pw_multi_affs A -> B and C -> D,
4479 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
4481 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_range_product(
4482 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4484 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4485 &pw_multi_aff_range_product);
4488 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
4489 * construct an isl_pw_multi_aff (A * C) -> (B, D).
4491 static __isl_give isl_pw_multi_aff *pw_multi_aff_flat_range_product(
4492 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4496 space = isl_space_range_product(isl_pw_multi_aff_get_space(pma1),
4497 isl_pw_multi_aff_get_space(pma2));
4498 space = isl_space_flatten_range(space);
4499 return isl_pw_multi_aff_on_shared_domain_in(pma1, pma2, space,
4500 &isl_multi_aff_flat_range_product);
4503 /* Given two isl_pw_multi_affs A -> B and C -> D,
4504 * construct an isl_pw_multi_aff (A * C) -> (B, D).
4506 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_flat_range_product(
4507 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4509 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4510 &pw_multi_aff_flat_range_product);
4513 /* If data->pma and *entry have the same domain space, then compute
4514 * their flat range product and the result to data->res.
4516 static int flat_range_product_entry(void **entry, void *user)
4518 struct isl_union_pw_multi_aff_bin_data *data = user;
4519 isl_pw_multi_aff *pma2 = *entry;
4521 if (!isl_space_tuple_match(data->pma->dim, isl_dim_in,
4522 pma2->dim, isl_dim_in))
4525 pma2 = isl_pw_multi_aff_flat_range_product(
4526 isl_pw_multi_aff_copy(data->pma),
4527 isl_pw_multi_aff_copy(pma2));
4529 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma2);
4534 /* Given two isl_union_pw_multi_affs A -> B and C -> D,
4535 * construct an isl_union_pw_multi_aff (A * C) -> (B, D).
4537 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_flat_range_product(
4538 __isl_take isl_union_pw_multi_aff *upma1,
4539 __isl_take isl_union_pw_multi_aff *upma2)
4541 return bin_op(upma1, upma2, &flat_range_product_entry);
4544 /* Replace the affine expressions at position "pos" in "pma" by "pa".
4545 * The parameters are assumed to have been aligned.
4547 * The implementation essentially performs an isl_pw_*_on_shared_domain,
4548 * except that it works on two different isl_pw_* types.
4550 static __isl_give isl_pw_multi_aff *pw_multi_aff_set_pw_aff(
4551 __isl_take isl_pw_multi_aff *pma, unsigned pos,
4552 __isl_take isl_pw_aff *pa)
4555 isl_pw_multi_aff *res = NULL;
4560 if (!isl_space_tuple_match(pma->dim, isl_dim_in, pa->dim, isl_dim_in))
4561 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
4562 "domains don't match", goto error);
4563 if (pos >= isl_pw_multi_aff_dim(pma, isl_dim_out))
4564 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
4565 "index out of bounds", goto error);
4568 res = isl_pw_multi_aff_alloc_size(isl_pw_multi_aff_get_space(pma), n);
4570 for (i = 0; i < pma->n; ++i) {
4571 for (j = 0; j < pa->n; ++j) {
4573 isl_multi_aff *res_ij;
4576 common = isl_set_intersect(isl_set_copy(pma->p[i].set),
4577 isl_set_copy(pa->p[j].set));
4578 empty = isl_set_plain_is_empty(common);
4579 if (empty < 0 || empty) {
4580 isl_set_free(common);
4586 res_ij = isl_multi_aff_set_aff(
4587 isl_multi_aff_copy(pma->p[i].maff), pos,
4588 isl_aff_copy(pa->p[j].aff));
4589 res_ij = isl_multi_aff_gist(res_ij,
4590 isl_set_copy(common));
4592 res = isl_pw_multi_aff_add_piece(res, common, res_ij);
4596 isl_pw_multi_aff_free(pma);
4597 isl_pw_aff_free(pa);
4600 isl_pw_multi_aff_free(pma);
4601 isl_pw_aff_free(pa);
4602 return isl_pw_multi_aff_free(res);
4605 /* Replace the affine expressions at position "pos" in "pma" by "pa".
4607 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_set_pw_aff(
4608 __isl_take isl_pw_multi_aff *pma, unsigned pos,
4609 __isl_take isl_pw_aff *pa)
4613 if (isl_space_match(pma->dim, isl_dim_param, pa->dim, isl_dim_param))
4614 return pw_multi_aff_set_pw_aff(pma, pos, pa);
4615 if (!isl_space_has_named_params(pma->dim) ||
4616 !isl_space_has_named_params(pa->dim))
4617 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
4618 "unaligned unnamed parameters", goto error);
4619 pma = isl_pw_multi_aff_align_params(pma, isl_pw_aff_get_space(pa));
4620 pa = isl_pw_aff_align_params(pa, isl_pw_multi_aff_get_space(pma));
4621 return pw_multi_aff_set_pw_aff(pma, pos, pa);
4623 isl_pw_multi_aff_free(pma);
4624 isl_pw_aff_free(pa);
4631 #include <isl_multi_templ.c>