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);
1218 isl_int_gcd(gcd, aff->v->el[0], f);
1219 isl_int_divexact(aff->v->el[0], aff->v->el[0], gcd);
1220 isl_int_divexact(gcd, f, gcd);
1221 isl_seq_scale(aff->v->el + 1, aff->v->el + 1, gcd, aff->v->size - 1);
1227 __isl_give isl_aff *isl_aff_scale_down(__isl_take isl_aff *aff, isl_int f)
1231 if (isl_int_is_one(f))
1234 aff = isl_aff_cow(aff);
1238 if (isl_int_is_zero(f))
1239 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1240 "cannot scale down by zero", return isl_aff_free(aff));
1242 aff->v = isl_vec_cow(aff->v);
1244 return isl_aff_free(aff);
1247 isl_seq_gcd(aff->v->el + 1, aff->v->size - 1, &gcd);
1248 isl_int_gcd(gcd, gcd, f);
1249 isl_seq_scale_down(aff->v->el + 1, aff->v->el + 1, gcd, aff->v->size - 1);
1250 isl_int_divexact(gcd, f, gcd);
1251 isl_int_mul(aff->v->el[0], aff->v->el[0], gcd);
1257 __isl_give isl_aff *isl_aff_scale_down_ui(__isl_take isl_aff *aff, unsigned f)
1265 isl_int_set_ui(v, f);
1266 aff = isl_aff_scale_down(aff, v);
1272 __isl_give isl_aff *isl_aff_set_dim_name(__isl_take isl_aff *aff,
1273 enum isl_dim_type type, unsigned pos, const char *s)
1275 aff = isl_aff_cow(aff);
1278 if (type == isl_dim_out)
1279 isl_die(aff->v->ctx, isl_error_invalid,
1280 "cannot set name of output/set dimension",
1281 return isl_aff_free(aff));
1282 if (type == isl_dim_in)
1284 aff->ls = isl_local_space_set_dim_name(aff->ls, type, pos, s);
1286 return isl_aff_free(aff);
1291 __isl_give isl_aff *isl_aff_set_dim_id(__isl_take isl_aff *aff,
1292 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
1294 aff = isl_aff_cow(aff);
1296 return isl_id_free(id);
1297 if (type == isl_dim_out)
1298 isl_die(aff->v->ctx, isl_error_invalid,
1299 "cannot set name of output/set dimension",
1301 if (type == isl_dim_in)
1303 aff->ls = isl_local_space_set_dim_id(aff->ls, type, pos, id);
1305 return isl_aff_free(aff);
1314 /* Exploit the equalities in "eq" to simplify the affine expression
1315 * and the expressions of the integer divisions in the local space.
1316 * The integer divisions in this local space are assumed to appear
1317 * as regular dimensions in "eq".
1319 static __isl_give isl_aff *isl_aff_substitute_equalities_lifted(
1320 __isl_take isl_aff *aff, __isl_take isl_basic_set *eq)
1328 if (eq->n_eq == 0) {
1329 isl_basic_set_free(eq);
1333 aff = isl_aff_cow(aff);
1337 aff->ls = isl_local_space_substitute_equalities(aff->ls,
1338 isl_basic_set_copy(eq));
1339 aff->v = isl_vec_cow(aff->v);
1340 if (!aff->ls || !aff->v)
1343 total = 1 + isl_space_dim(eq->dim, isl_dim_all);
1345 for (i = 0; i < eq->n_eq; ++i) {
1346 j = isl_seq_last_non_zero(eq->eq[i], total + n_div);
1347 if (j < 0 || j == 0 || j >= total)
1350 isl_seq_elim(aff->v->el + 1, eq->eq[i], j, total,
1354 isl_basic_set_free(eq);
1355 aff = isl_aff_normalize(aff);
1358 isl_basic_set_free(eq);
1363 /* Exploit the equalities in "eq" to simplify the affine expression
1364 * and the expressions of the integer divisions in the local space.
1366 static __isl_give isl_aff *isl_aff_substitute_equalities(
1367 __isl_take isl_aff *aff, __isl_take isl_basic_set *eq)
1373 n_div = isl_local_space_dim(aff->ls, isl_dim_div);
1375 eq = isl_basic_set_add_dims(eq, isl_dim_set, n_div);
1376 return isl_aff_substitute_equalities_lifted(aff, eq);
1378 isl_basic_set_free(eq);
1383 /* Look for equalities among the variables shared by context and aff
1384 * and the integer divisions of aff, if any.
1385 * The equalities are then used to eliminate coefficients and/or integer
1386 * divisions from aff.
1388 __isl_give isl_aff *isl_aff_gist(__isl_take isl_aff *aff,
1389 __isl_take isl_set *context)
1391 isl_basic_set *hull;
1396 n_div = isl_local_space_dim(aff->ls, isl_dim_div);
1398 isl_basic_set *bset;
1399 isl_local_space *ls;
1400 context = isl_set_add_dims(context, isl_dim_set, n_div);
1401 ls = isl_aff_get_domain_local_space(aff);
1402 bset = isl_basic_set_from_local_space(ls);
1403 bset = isl_basic_set_lift(bset);
1404 bset = isl_basic_set_flatten(bset);
1405 context = isl_set_intersect(context,
1406 isl_set_from_basic_set(bset));
1409 hull = isl_set_affine_hull(context);
1410 return isl_aff_substitute_equalities_lifted(aff, hull);
1413 isl_set_free(context);
1417 __isl_give isl_aff *isl_aff_gist_params(__isl_take isl_aff *aff,
1418 __isl_take isl_set *context)
1420 isl_set *dom_context = isl_set_universe(isl_aff_get_domain_space(aff));
1421 dom_context = isl_set_intersect_params(dom_context, context);
1422 return isl_aff_gist(aff, dom_context);
1425 /* Return a basic set containing those elements in the space
1426 * of aff where it is non-negative.
1427 * If "rational" is set, then return a rational basic set.
1429 static __isl_give isl_basic_set *aff_nonneg_basic_set(
1430 __isl_take isl_aff *aff, int rational)
1432 isl_constraint *ineq;
1433 isl_basic_set *bset;
1435 ineq = isl_inequality_from_aff(aff);
1437 bset = isl_basic_set_from_constraint(ineq);
1439 bset = isl_basic_set_set_rational(bset);
1440 bset = isl_basic_set_simplify(bset);
1444 /* Return a basic set containing those elements in the space
1445 * of aff where it is non-negative.
1447 __isl_give isl_basic_set *isl_aff_nonneg_basic_set(__isl_take isl_aff *aff)
1449 return aff_nonneg_basic_set(aff, 0);
1452 /* Return a basic set containing those elements in the domain space
1453 * of aff where it is negative.
1455 __isl_give isl_basic_set *isl_aff_neg_basic_set(__isl_take isl_aff *aff)
1457 aff = isl_aff_neg(aff);
1458 aff = isl_aff_add_constant_num_si(aff, -1);
1459 return isl_aff_nonneg_basic_set(aff);
1462 /* Return a basic set containing those elements in the space
1463 * of aff where it is zero.
1464 * If "rational" is set, then return a rational basic set.
1466 static __isl_give isl_basic_set *aff_zero_basic_set(__isl_take isl_aff *aff,
1469 isl_constraint *ineq;
1470 isl_basic_set *bset;
1472 ineq = isl_equality_from_aff(aff);
1474 bset = isl_basic_set_from_constraint(ineq);
1476 bset = isl_basic_set_set_rational(bset);
1477 bset = isl_basic_set_simplify(bset);
1481 /* Return a basic set containing those elements in the space
1482 * of aff where it is zero.
1484 __isl_give isl_basic_set *isl_aff_zero_basic_set(__isl_take isl_aff *aff)
1486 return aff_zero_basic_set(aff, 0);
1489 /* Return a basic set containing those elements in the shared space
1490 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
1492 __isl_give isl_basic_set *isl_aff_ge_basic_set(__isl_take isl_aff *aff1,
1493 __isl_take isl_aff *aff2)
1495 aff1 = isl_aff_sub(aff1, aff2);
1497 return isl_aff_nonneg_basic_set(aff1);
1500 /* Return a basic set containing those elements in the shared space
1501 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
1503 __isl_give isl_basic_set *isl_aff_le_basic_set(__isl_take isl_aff *aff1,
1504 __isl_take isl_aff *aff2)
1506 return isl_aff_ge_basic_set(aff2, aff1);
1509 __isl_give isl_aff *isl_aff_add_on_domain(__isl_keep isl_set *dom,
1510 __isl_take isl_aff *aff1, __isl_take isl_aff *aff2)
1512 aff1 = isl_aff_add(aff1, aff2);
1513 aff1 = isl_aff_gist(aff1, isl_set_copy(dom));
1517 int isl_aff_is_empty(__isl_keep isl_aff *aff)
1525 /* Check whether the given affine expression has non-zero coefficient
1526 * for any dimension in the given range or if any of these dimensions
1527 * appear with non-zero coefficients in any of the integer divisions
1528 * involved in the affine expression.
1530 int isl_aff_involves_dims(__isl_keep isl_aff *aff,
1531 enum isl_dim_type type, unsigned first, unsigned n)
1543 ctx = isl_aff_get_ctx(aff);
1544 if (first + n > isl_aff_dim(aff, type))
1545 isl_die(ctx, isl_error_invalid,
1546 "range out of bounds", return -1);
1548 active = isl_local_space_get_active(aff->ls, aff->v->el + 2);
1552 first += isl_local_space_offset(aff->ls, type) - 1;
1553 for (i = 0; i < n; ++i)
1554 if (active[first + i]) {
1567 __isl_give isl_aff *isl_aff_drop_dims(__isl_take isl_aff *aff,
1568 enum isl_dim_type type, unsigned first, unsigned n)
1574 if (type == isl_dim_out)
1575 isl_die(aff->v->ctx, isl_error_invalid,
1576 "cannot drop output/set dimension",
1577 return isl_aff_free(aff));
1578 if (type == isl_dim_in)
1580 if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
1583 ctx = isl_aff_get_ctx(aff);
1584 if (first + n > isl_local_space_dim(aff->ls, type))
1585 isl_die(ctx, isl_error_invalid, "range out of bounds",
1586 return isl_aff_free(aff));
1588 aff = isl_aff_cow(aff);
1592 aff->ls = isl_local_space_drop_dims(aff->ls, type, first, n);
1594 return isl_aff_free(aff);
1596 first += 1 + isl_local_space_offset(aff->ls, type);
1597 aff->v = isl_vec_drop_els(aff->v, first, n);
1599 return isl_aff_free(aff);
1604 /* Project the domain of the affine expression onto its parameter space.
1605 * The affine expression may not involve any of the domain dimensions.
1607 __isl_give isl_aff *isl_aff_project_domain_on_params(__isl_take isl_aff *aff)
1613 n = isl_aff_dim(aff, isl_dim_in);
1614 involves = isl_aff_involves_dims(aff, isl_dim_in, 0, n);
1616 return isl_aff_free(aff);
1618 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1619 "affine expression involves some of the domain dimensions",
1620 return isl_aff_free(aff));
1621 aff = isl_aff_drop_dims(aff, isl_dim_in, 0, n);
1622 space = isl_aff_get_domain_space(aff);
1623 space = isl_space_params(space);
1624 aff = isl_aff_reset_domain_space(aff, space);
1628 __isl_give isl_aff *isl_aff_insert_dims(__isl_take isl_aff *aff,
1629 enum isl_dim_type type, unsigned first, unsigned n)
1635 if (type == isl_dim_out)
1636 isl_die(aff->v->ctx, isl_error_invalid,
1637 "cannot insert output/set dimensions",
1638 return isl_aff_free(aff));
1639 if (type == isl_dim_in)
1641 if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
1644 ctx = isl_aff_get_ctx(aff);
1645 if (first > isl_local_space_dim(aff->ls, type))
1646 isl_die(ctx, isl_error_invalid, "position out of bounds",
1647 return isl_aff_free(aff));
1649 aff = isl_aff_cow(aff);
1653 aff->ls = isl_local_space_insert_dims(aff->ls, type, first, n);
1655 return isl_aff_free(aff);
1657 first += 1 + isl_local_space_offset(aff->ls, type);
1658 aff->v = isl_vec_insert_zero_els(aff->v, first, n);
1660 return isl_aff_free(aff);
1665 __isl_give isl_aff *isl_aff_add_dims(__isl_take isl_aff *aff,
1666 enum isl_dim_type type, unsigned n)
1670 pos = isl_aff_dim(aff, type);
1672 return isl_aff_insert_dims(aff, type, pos, n);
1675 __isl_give isl_pw_aff *isl_pw_aff_add_dims(__isl_take isl_pw_aff *pwaff,
1676 enum isl_dim_type type, unsigned n)
1680 pos = isl_pw_aff_dim(pwaff, type);
1682 return isl_pw_aff_insert_dims(pwaff, type, pos, n);
1685 __isl_give isl_pw_aff *isl_pw_aff_from_aff(__isl_take isl_aff *aff)
1687 isl_set *dom = isl_set_universe(isl_aff_get_domain_space(aff));
1688 return isl_pw_aff_alloc(dom, aff);
1692 #define PW isl_pw_aff
1696 #define EL_IS_ZERO is_empty
1700 #define IS_ZERO is_empty
1703 #undef DEFAULT_IS_ZERO
1704 #define DEFAULT_IS_ZERO 0
1708 #define NO_MOVE_DIMS
1712 #include <isl_pw_templ.c>
1714 static __isl_give isl_set *align_params_pw_pw_set_and(
1715 __isl_take isl_pw_aff *pwaff1, __isl_take isl_pw_aff *pwaff2,
1716 __isl_give isl_set *(*fn)(__isl_take isl_pw_aff *pwaff1,
1717 __isl_take isl_pw_aff *pwaff2))
1719 if (!pwaff1 || !pwaff2)
1721 if (isl_space_match(pwaff1->dim, isl_dim_param,
1722 pwaff2->dim, isl_dim_param))
1723 return fn(pwaff1, pwaff2);
1724 if (!isl_space_has_named_params(pwaff1->dim) ||
1725 !isl_space_has_named_params(pwaff2->dim))
1726 isl_die(isl_pw_aff_get_ctx(pwaff1), isl_error_invalid,
1727 "unaligned unnamed parameters", goto error);
1728 pwaff1 = isl_pw_aff_align_params(pwaff1, isl_pw_aff_get_space(pwaff2));
1729 pwaff2 = isl_pw_aff_align_params(pwaff2, isl_pw_aff_get_space(pwaff1));
1730 return fn(pwaff1, pwaff2);
1732 isl_pw_aff_free(pwaff1);
1733 isl_pw_aff_free(pwaff2);
1737 /* Compute a piecewise quasi-affine expression with a domain that
1738 * is the union of those of pwaff1 and pwaff2 and such that on each
1739 * cell, the quasi-affine expression is the better (according to cmp)
1740 * of those of pwaff1 and pwaff2. If only one of pwaff1 or pwaff2
1741 * is defined on a given cell, then the associated expression
1742 * is the defined one.
1744 static __isl_give isl_pw_aff *pw_aff_union_opt(__isl_take isl_pw_aff *pwaff1,
1745 __isl_take isl_pw_aff *pwaff2,
1746 __isl_give isl_basic_set *(*cmp)(__isl_take isl_aff *aff1,
1747 __isl_take isl_aff *aff2))
1754 if (!pwaff1 || !pwaff2)
1757 ctx = isl_space_get_ctx(pwaff1->dim);
1758 if (!isl_space_is_equal(pwaff1->dim, pwaff2->dim))
1759 isl_die(ctx, isl_error_invalid,
1760 "arguments should live in same space", goto error);
1762 if (isl_pw_aff_is_empty(pwaff1)) {
1763 isl_pw_aff_free(pwaff1);
1767 if (isl_pw_aff_is_empty(pwaff2)) {
1768 isl_pw_aff_free(pwaff2);
1772 n = 2 * (pwaff1->n + 1) * (pwaff2->n + 1);
1773 res = isl_pw_aff_alloc_size(isl_space_copy(pwaff1->dim), n);
1775 for (i = 0; i < pwaff1->n; ++i) {
1776 set = isl_set_copy(pwaff1->p[i].set);
1777 for (j = 0; j < pwaff2->n; ++j) {
1778 struct isl_set *common;
1781 common = isl_set_intersect(
1782 isl_set_copy(pwaff1->p[i].set),
1783 isl_set_copy(pwaff2->p[j].set));
1784 better = isl_set_from_basic_set(cmp(
1785 isl_aff_copy(pwaff2->p[j].aff),
1786 isl_aff_copy(pwaff1->p[i].aff)));
1787 better = isl_set_intersect(common, better);
1788 if (isl_set_plain_is_empty(better)) {
1789 isl_set_free(better);
1792 set = isl_set_subtract(set, isl_set_copy(better));
1794 res = isl_pw_aff_add_piece(res, better,
1795 isl_aff_copy(pwaff2->p[j].aff));
1797 res = isl_pw_aff_add_piece(res, set,
1798 isl_aff_copy(pwaff1->p[i].aff));
1801 for (j = 0; j < pwaff2->n; ++j) {
1802 set = isl_set_copy(pwaff2->p[j].set);
1803 for (i = 0; i < pwaff1->n; ++i)
1804 set = isl_set_subtract(set,
1805 isl_set_copy(pwaff1->p[i].set));
1806 res = isl_pw_aff_add_piece(res, set,
1807 isl_aff_copy(pwaff2->p[j].aff));
1810 isl_pw_aff_free(pwaff1);
1811 isl_pw_aff_free(pwaff2);
1815 isl_pw_aff_free(pwaff1);
1816 isl_pw_aff_free(pwaff2);
1820 /* Compute a piecewise quasi-affine expression with a domain that
1821 * is the union of those of pwaff1 and pwaff2 and such that on each
1822 * cell, the quasi-affine expression is the maximum of those of pwaff1
1823 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
1824 * cell, then the associated expression is the defined one.
1826 static __isl_give isl_pw_aff *pw_aff_union_max(__isl_take isl_pw_aff *pwaff1,
1827 __isl_take isl_pw_aff *pwaff2)
1829 return pw_aff_union_opt(pwaff1, pwaff2, &isl_aff_ge_basic_set);
1832 __isl_give isl_pw_aff *isl_pw_aff_union_max(__isl_take isl_pw_aff *pwaff1,
1833 __isl_take isl_pw_aff *pwaff2)
1835 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2,
1839 /* Compute a piecewise quasi-affine expression with a domain that
1840 * is the union of those of pwaff1 and pwaff2 and such that on each
1841 * cell, the quasi-affine expression is the minimum of those of pwaff1
1842 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
1843 * cell, then the associated expression is the defined one.
1845 static __isl_give isl_pw_aff *pw_aff_union_min(__isl_take isl_pw_aff *pwaff1,
1846 __isl_take isl_pw_aff *pwaff2)
1848 return pw_aff_union_opt(pwaff1, pwaff2, &isl_aff_le_basic_set);
1851 __isl_give isl_pw_aff *isl_pw_aff_union_min(__isl_take isl_pw_aff *pwaff1,
1852 __isl_take isl_pw_aff *pwaff2)
1854 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2,
1858 __isl_give isl_pw_aff *isl_pw_aff_union_opt(__isl_take isl_pw_aff *pwaff1,
1859 __isl_take isl_pw_aff *pwaff2, int max)
1862 return isl_pw_aff_union_max(pwaff1, pwaff2);
1864 return isl_pw_aff_union_min(pwaff1, pwaff2);
1867 /* Construct a map with as domain the domain of pwaff and
1868 * one-dimensional range corresponding to the affine expressions.
1870 static __isl_give isl_map *map_from_pw_aff(__isl_take isl_pw_aff *pwaff)
1879 dim = isl_pw_aff_get_space(pwaff);
1880 map = isl_map_empty(dim);
1882 for (i = 0; i < pwaff->n; ++i) {
1883 isl_basic_map *bmap;
1886 bmap = isl_basic_map_from_aff(isl_aff_copy(pwaff->p[i].aff));
1887 map_i = isl_map_from_basic_map(bmap);
1888 map_i = isl_map_intersect_domain(map_i,
1889 isl_set_copy(pwaff->p[i].set));
1890 map = isl_map_union_disjoint(map, map_i);
1893 isl_pw_aff_free(pwaff);
1898 /* Construct a map with as domain the domain of pwaff and
1899 * one-dimensional range corresponding to the affine expressions.
1901 __isl_give isl_map *isl_map_from_pw_aff(__isl_take isl_pw_aff *pwaff)
1905 if (isl_space_is_set(pwaff->dim))
1906 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
1907 "space of input is not a map",
1908 return isl_pw_aff_free(pwaff));
1909 return map_from_pw_aff(pwaff);
1912 /* Construct a one-dimensional set with as parameter domain
1913 * the domain of pwaff and the single set dimension
1914 * corresponding to the affine expressions.
1916 __isl_give isl_set *isl_set_from_pw_aff(__isl_take isl_pw_aff *pwaff)
1920 if (!isl_space_is_set(pwaff->dim))
1921 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
1922 "space of input is not a set",
1923 return isl_pw_aff_free(pwaff));
1924 return map_from_pw_aff(pwaff);
1927 /* Return a set containing those elements in the domain
1928 * of pwaff where it is non-negative.
1930 __isl_give isl_set *isl_pw_aff_nonneg_set(__isl_take isl_pw_aff *pwaff)
1938 set = isl_set_empty(isl_pw_aff_get_domain_space(pwaff));
1940 for (i = 0; i < pwaff->n; ++i) {
1941 isl_basic_set *bset;
1945 rational = isl_set_has_rational(pwaff->p[i].set);
1946 bset = aff_nonneg_basic_set(isl_aff_copy(pwaff->p[i].aff),
1948 set_i = isl_set_from_basic_set(bset);
1949 set_i = isl_set_intersect(set_i, isl_set_copy(pwaff->p[i].set));
1950 set = isl_set_union_disjoint(set, set_i);
1953 isl_pw_aff_free(pwaff);
1958 /* Return a set containing those elements in the domain
1959 * of pwaff where it is zero (if complement is 0) or not zero
1960 * (if complement is 1).
1962 static __isl_give isl_set *pw_aff_zero_set(__isl_take isl_pw_aff *pwaff,
1971 set = isl_set_empty(isl_pw_aff_get_domain_space(pwaff));
1973 for (i = 0; i < pwaff->n; ++i) {
1974 isl_basic_set *bset;
1975 isl_set *set_i, *zero;
1978 rational = isl_set_has_rational(pwaff->p[i].set);
1979 bset = aff_zero_basic_set(isl_aff_copy(pwaff->p[i].aff),
1981 zero = isl_set_from_basic_set(bset);
1982 set_i = isl_set_copy(pwaff->p[i].set);
1984 set_i = isl_set_subtract(set_i, zero);
1986 set_i = isl_set_intersect(set_i, zero);
1987 set = isl_set_union_disjoint(set, set_i);
1990 isl_pw_aff_free(pwaff);
1995 /* Return a set containing those elements in the domain
1996 * of pwaff where it is zero.
1998 __isl_give isl_set *isl_pw_aff_zero_set(__isl_take isl_pw_aff *pwaff)
2000 return pw_aff_zero_set(pwaff, 0);
2003 /* Return a set containing those elements in the domain
2004 * of pwaff where it is not zero.
2006 __isl_give isl_set *isl_pw_aff_non_zero_set(__isl_take isl_pw_aff *pwaff)
2008 return pw_aff_zero_set(pwaff, 1);
2011 /* Return a set containing those elements in the shared domain
2012 * of pwaff1 and pwaff2 where pwaff1 is greater than (or equal) to pwaff2.
2014 * We compute the difference on the shared domain and then construct
2015 * the set of values where this difference is non-negative.
2016 * If strict is set, we first subtract 1 from the difference.
2017 * If equal is set, we only return the elements where pwaff1 and pwaff2
2020 static __isl_give isl_set *pw_aff_gte_set(__isl_take isl_pw_aff *pwaff1,
2021 __isl_take isl_pw_aff *pwaff2, int strict, int equal)
2023 isl_set *set1, *set2;
2025 set1 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff1));
2026 set2 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff2));
2027 set1 = isl_set_intersect(set1, set2);
2028 pwaff1 = isl_pw_aff_intersect_domain(pwaff1, isl_set_copy(set1));
2029 pwaff2 = isl_pw_aff_intersect_domain(pwaff2, isl_set_copy(set1));
2030 pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_neg(pwaff2));
2033 isl_space *dim = isl_set_get_space(set1);
2035 aff = isl_aff_zero_on_domain(isl_local_space_from_space(dim));
2036 aff = isl_aff_add_constant_si(aff, -1);
2037 pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_alloc(set1, aff));
2042 return isl_pw_aff_zero_set(pwaff1);
2043 return isl_pw_aff_nonneg_set(pwaff1);
2046 /* Return a set containing those elements in the shared domain
2047 * of pwaff1 and pwaff2 where pwaff1 is equal to pwaff2.
2049 static __isl_give isl_set *pw_aff_eq_set(__isl_take isl_pw_aff *pwaff1,
2050 __isl_take isl_pw_aff *pwaff2)
2052 return pw_aff_gte_set(pwaff1, pwaff2, 0, 1);
2055 __isl_give isl_set *isl_pw_aff_eq_set(__isl_take isl_pw_aff *pwaff1,
2056 __isl_take isl_pw_aff *pwaff2)
2058 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_eq_set);
2061 /* Return a set containing those elements in the shared domain
2062 * of pwaff1 and pwaff2 where pwaff1 is greater than or equal to pwaff2.
2064 static __isl_give isl_set *pw_aff_ge_set(__isl_take isl_pw_aff *pwaff1,
2065 __isl_take isl_pw_aff *pwaff2)
2067 return pw_aff_gte_set(pwaff1, pwaff2, 0, 0);
2070 __isl_give isl_set *isl_pw_aff_ge_set(__isl_take isl_pw_aff *pwaff1,
2071 __isl_take isl_pw_aff *pwaff2)
2073 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_ge_set);
2076 /* Return a set containing those elements in the shared domain
2077 * of pwaff1 and pwaff2 where pwaff1 is strictly greater than pwaff2.
2079 static __isl_give isl_set *pw_aff_gt_set(__isl_take isl_pw_aff *pwaff1,
2080 __isl_take isl_pw_aff *pwaff2)
2082 return pw_aff_gte_set(pwaff1, pwaff2, 1, 0);
2085 __isl_give isl_set *isl_pw_aff_gt_set(__isl_take isl_pw_aff *pwaff1,
2086 __isl_take isl_pw_aff *pwaff2)
2088 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_gt_set);
2091 __isl_give isl_set *isl_pw_aff_le_set(__isl_take isl_pw_aff *pwaff1,
2092 __isl_take isl_pw_aff *pwaff2)
2094 return isl_pw_aff_ge_set(pwaff2, pwaff1);
2097 __isl_give isl_set *isl_pw_aff_lt_set(__isl_take isl_pw_aff *pwaff1,
2098 __isl_take isl_pw_aff *pwaff2)
2100 return isl_pw_aff_gt_set(pwaff2, pwaff1);
2103 /* Return a set containing those elements in the shared domain
2104 * of the elements of list1 and list2 where each element in list1
2105 * has the relation specified by "fn" with each element in list2.
2107 static __isl_give isl_set *pw_aff_list_set(__isl_take isl_pw_aff_list *list1,
2108 __isl_take isl_pw_aff_list *list2,
2109 __isl_give isl_set *(*fn)(__isl_take isl_pw_aff *pwaff1,
2110 __isl_take isl_pw_aff *pwaff2))
2116 if (!list1 || !list2)
2119 ctx = isl_pw_aff_list_get_ctx(list1);
2120 if (list1->n < 1 || list2->n < 1)
2121 isl_die(ctx, isl_error_invalid,
2122 "list should contain at least one element", goto error);
2124 set = isl_set_universe(isl_pw_aff_get_domain_space(list1->p[0]));
2125 for (i = 0; i < list1->n; ++i)
2126 for (j = 0; j < list2->n; ++j) {
2129 set_ij = fn(isl_pw_aff_copy(list1->p[i]),
2130 isl_pw_aff_copy(list2->p[j]));
2131 set = isl_set_intersect(set, set_ij);
2134 isl_pw_aff_list_free(list1);
2135 isl_pw_aff_list_free(list2);
2138 isl_pw_aff_list_free(list1);
2139 isl_pw_aff_list_free(list2);
2143 /* Return a set containing those elements in the shared domain
2144 * of the elements of list1 and list2 where each element in list1
2145 * is equal to each element in list2.
2147 __isl_give isl_set *isl_pw_aff_list_eq_set(__isl_take isl_pw_aff_list *list1,
2148 __isl_take isl_pw_aff_list *list2)
2150 return pw_aff_list_set(list1, list2, &isl_pw_aff_eq_set);
2153 __isl_give isl_set *isl_pw_aff_list_ne_set(__isl_take isl_pw_aff_list *list1,
2154 __isl_take isl_pw_aff_list *list2)
2156 return pw_aff_list_set(list1, list2, &isl_pw_aff_ne_set);
2159 /* Return a set containing those elements in the shared domain
2160 * of the elements of list1 and list2 where each element in list1
2161 * is less than or equal to each element in list2.
2163 __isl_give isl_set *isl_pw_aff_list_le_set(__isl_take isl_pw_aff_list *list1,
2164 __isl_take isl_pw_aff_list *list2)
2166 return pw_aff_list_set(list1, list2, &isl_pw_aff_le_set);
2169 __isl_give isl_set *isl_pw_aff_list_lt_set(__isl_take isl_pw_aff_list *list1,
2170 __isl_take isl_pw_aff_list *list2)
2172 return pw_aff_list_set(list1, list2, &isl_pw_aff_lt_set);
2175 __isl_give isl_set *isl_pw_aff_list_ge_set(__isl_take isl_pw_aff_list *list1,
2176 __isl_take isl_pw_aff_list *list2)
2178 return pw_aff_list_set(list1, list2, &isl_pw_aff_ge_set);
2181 __isl_give isl_set *isl_pw_aff_list_gt_set(__isl_take isl_pw_aff_list *list1,
2182 __isl_take isl_pw_aff_list *list2)
2184 return pw_aff_list_set(list1, list2, &isl_pw_aff_gt_set);
2188 /* Return a set containing those elements in the shared domain
2189 * of pwaff1 and pwaff2 where pwaff1 is not equal to pwaff2.
2191 static __isl_give isl_set *pw_aff_ne_set(__isl_take isl_pw_aff *pwaff1,
2192 __isl_take isl_pw_aff *pwaff2)
2194 isl_set *set_lt, *set_gt;
2196 set_lt = isl_pw_aff_lt_set(isl_pw_aff_copy(pwaff1),
2197 isl_pw_aff_copy(pwaff2));
2198 set_gt = isl_pw_aff_gt_set(pwaff1, pwaff2);
2199 return isl_set_union_disjoint(set_lt, set_gt);
2202 __isl_give isl_set *isl_pw_aff_ne_set(__isl_take isl_pw_aff *pwaff1,
2203 __isl_take isl_pw_aff *pwaff2)
2205 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_ne_set);
2208 __isl_give isl_pw_aff *isl_pw_aff_scale_down(__isl_take isl_pw_aff *pwaff,
2213 if (isl_int_is_one(v))
2215 if (!isl_int_is_pos(v))
2216 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
2217 "factor needs to be positive",
2218 return isl_pw_aff_free(pwaff));
2219 pwaff = isl_pw_aff_cow(pwaff);
2225 for (i = 0; i < pwaff->n; ++i) {
2226 pwaff->p[i].aff = isl_aff_scale_down(pwaff->p[i].aff, v);
2227 if (!pwaff->p[i].aff)
2228 return isl_pw_aff_free(pwaff);
2234 __isl_give isl_pw_aff *isl_pw_aff_floor(__isl_take isl_pw_aff *pwaff)
2238 pwaff = isl_pw_aff_cow(pwaff);
2244 for (i = 0; i < pwaff->n; ++i) {
2245 pwaff->p[i].aff = isl_aff_floor(pwaff->p[i].aff);
2246 if (!pwaff->p[i].aff)
2247 return isl_pw_aff_free(pwaff);
2253 __isl_give isl_pw_aff *isl_pw_aff_ceil(__isl_take isl_pw_aff *pwaff)
2257 pwaff = isl_pw_aff_cow(pwaff);
2263 for (i = 0; i < pwaff->n; ++i) {
2264 pwaff->p[i].aff = isl_aff_ceil(pwaff->p[i].aff);
2265 if (!pwaff->p[i].aff)
2266 return isl_pw_aff_free(pwaff);
2272 /* Assuming that "cond1" and "cond2" are disjoint,
2273 * return an affine expression that is equal to pwaff1 on cond1
2274 * and to pwaff2 on cond2.
2276 static __isl_give isl_pw_aff *isl_pw_aff_select(
2277 __isl_take isl_set *cond1, __isl_take isl_pw_aff *pwaff1,
2278 __isl_take isl_set *cond2, __isl_take isl_pw_aff *pwaff2)
2280 pwaff1 = isl_pw_aff_intersect_domain(pwaff1, cond1);
2281 pwaff2 = isl_pw_aff_intersect_domain(pwaff2, cond2);
2283 return isl_pw_aff_add_disjoint(pwaff1, pwaff2);
2286 /* Return an affine expression that is equal to pwaff_true for elements
2287 * where "cond" is non-zero and to pwaff_false for elements where "cond"
2289 * That is, return cond ? pwaff_true : pwaff_false;
2291 __isl_give isl_pw_aff *isl_pw_aff_cond(__isl_take isl_pw_aff *cond,
2292 __isl_take isl_pw_aff *pwaff_true, __isl_take isl_pw_aff *pwaff_false)
2294 isl_set *cond_true, *cond_false;
2296 cond_true = isl_pw_aff_non_zero_set(isl_pw_aff_copy(cond));
2297 cond_false = isl_pw_aff_zero_set(cond);
2298 return isl_pw_aff_select(cond_true, pwaff_true,
2299 cond_false, pwaff_false);
2302 int isl_aff_is_cst(__isl_keep isl_aff *aff)
2307 return isl_seq_first_non_zero(aff->v->el + 2, aff->v->size - 2) == -1;
2310 /* Check whether pwaff is a piecewise constant.
2312 int isl_pw_aff_is_cst(__isl_keep isl_pw_aff *pwaff)
2319 for (i = 0; i < pwaff->n; ++i) {
2320 int is_cst = isl_aff_is_cst(pwaff->p[i].aff);
2321 if (is_cst < 0 || !is_cst)
2328 __isl_give isl_aff *isl_aff_mul(__isl_take isl_aff *aff1,
2329 __isl_take isl_aff *aff2)
2331 if (!isl_aff_is_cst(aff2) && isl_aff_is_cst(aff1))
2332 return isl_aff_mul(aff2, aff1);
2334 if (!isl_aff_is_cst(aff2))
2335 isl_die(isl_aff_get_ctx(aff1), isl_error_invalid,
2336 "at least one affine expression should be constant",
2339 aff1 = isl_aff_cow(aff1);
2343 aff1 = isl_aff_scale(aff1, aff2->v->el[1]);
2344 aff1 = isl_aff_scale_down(aff1, aff2->v->el[0]);
2354 /* Divide "aff1" by "aff2", assuming "aff2" is a piecewise constant.
2356 __isl_give isl_aff *isl_aff_div(__isl_take isl_aff *aff1,
2357 __isl_take isl_aff *aff2)
2362 is_cst = isl_aff_is_cst(aff2);
2366 isl_die(isl_aff_get_ctx(aff2), isl_error_invalid,
2367 "second argument should be a constant", goto error);
2372 neg = isl_int_is_neg(aff2->v->el[1]);
2374 isl_int_neg(aff2->v->el[0], aff2->v->el[0]);
2375 isl_int_neg(aff2->v->el[1], aff2->v->el[1]);
2378 aff1 = isl_aff_scale(aff1, aff2->v->el[0]);
2379 aff1 = isl_aff_scale_down(aff1, aff2->v->el[1]);
2382 isl_int_neg(aff2->v->el[0], aff2->v->el[0]);
2383 isl_int_neg(aff2->v->el[1], aff2->v->el[1]);
2394 static __isl_give isl_pw_aff *pw_aff_add(__isl_take isl_pw_aff *pwaff1,
2395 __isl_take isl_pw_aff *pwaff2)
2397 return isl_pw_aff_on_shared_domain(pwaff1, pwaff2, &isl_aff_add);
2400 __isl_give isl_pw_aff *isl_pw_aff_add(__isl_take isl_pw_aff *pwaff1,
2401 __isl_take isl_pw_aff *pwaff2)
2403 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_add);
2406 __isl_give isl_pw_aff *isl_pw_aff_union_add(__isl_take isl_pw_aff *pwaff1,
2407 __isl_take isl_pw_aff *pwaff2)
2409 return isl_pw_aff_union_add_(pwaff1, pwaff2);
2412 static __isl_give isl_pw_aff *pw_aff_mul(__isl_take isl_pw_aff *pwaff1,
2413 __isl_take isl_pw_aff *pwaff2)
2415 return isl_pw_aff_on_shared_domain(pwaff1, pwaff2, &isl_aff_mul);
2418 __isl_give isl_pw_aff *isl_pw_aff_mul(__isl_take isl_pw_aff *pwaff1,
2419 __isl_take isl_pw_aff *pwaff2)
2421 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_mul);
2424 static __isl_give isl_pw_aff *pw_aff_div(__isl_take isl_pw_aff *pa1,
2425 __isl_take isl_pw_aff *pa2)
2427 return isl_pw_aff_on_shared_domain(pa1, pa2, &isl_aff_div);
2430 /* Divide "pa1" by "pa2", assuming "pa2" is a piecewise constant.
2432 __isl_give isl_pw_aff *isl_pw_aff_div(__isl_take isl_pw_aff *pa1,
2433 __isl_take isl_pw_aff *pa2)
2437 is_cst = isl_pw_aff_is_cst(pa2);
2441 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
2442 "second argument should be a piecewise constant",
2444 return isl_pw_aff_align_params_pw_pw_and(pa1, pa2, &pw_aff_div);
2446 isl_pw_aff_free(pa1);
2447 isl_pw_aff_free(pa2);
2451 /* Compute the quotient of the integer division of "pa1" by "pa2"
2452 * with rounding towards zero.
2453 * "pa2" is assumed to be a piecewise constant.
2455 * In particular, return
2457 * pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2)
2460 __isl_give isl_pw_aff *isl_pw_aff_tdiv_q(__isl_take isl_pw_aff *pa1,
2461 __isl_take isl_pw_aff *pa2)
2467 is_cst = isl_pw_aff_is_cst(pa2);
2471 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
2472 "second argument should be a piecewise constant",
2475 pa1 = isl_pw_aff_div(pa1, pa2);
2477 cond = isl_pw_aff_nonneg_set(isl_pw_aff_copy(pa1));
2478 f = isl_pw_aff_floor(isl_pw_aff_copy(pa1));
2479 c = isl_pw_aff_ceil(pa1);
2480 return isl_pw_aff_cond(isl_set_indicator_function(cond), f, c);
2482 isl_pw_aff_free(pa1);
2483 isl_pw_aff_free(pa2);
2487 /* Compute the remainder of the integer division of "pa1" by "pa2"
2488 * with rounding towards zero.
2489 * "pa2" is assumed to be a piecewise constant.
2491 * In particular, return
2493 * pa1 - pa2 * (pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2))
2496 __isl_give isl_pw_aff *isl_pw_aff_tdiv_r(__isl_take isl_pw_aff *pa1,
2497 __isl_take isl_pw_aff *pa2)
2502 is_cst = isl_pw_aff_is_cst(pa2);
2506 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
2507 "second argument should be a piecewise constant",
2509 res = isl_pw_aff_tdiv_q(isl_pw_aff_copy(pa1), isl_pw_aff_copy(pa2));
2510 res = isl_pw_aff_mul(pa2, res);
2511 res = isl_pw_aff_sub(pa1, res);
2514 isl_pw_aff_free(pa1);
2515 isl_pw_aff_free(pa2);
2519 static __isl_give isl_pw_aff *pw_aff_min(__isl_take isl_pw_aff *pwaff1,
2520 __isl_take isl_pw_aff *pwaff2)
2525 dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1)),
2526 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2)));
2527 le = isl_pw_aff_le_set(isl_pw_aff_copy(pwaff1),
2528 isl_pw_aff_copy(pwaff2));
2529 dom = isl_set_subtract(dom, isl_set_copy(le));
2530 return isl_pw_aff_select(le, pwaff1, dom, pwaff2);
2533 __isl_give isl_pw_aff *isl_pw_aff_min(__isl_take isl_pw_aff *pwaff1,
2534 __isl_take isl_pw_aff *pwaff2)
2536 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_min);
2539 static __isl_give isl_pw_aff *pw_aff_max(__isl_take isl_pw_aff *pwaff1,
2540 __isl_take isl_pw_aff *pwaff2)
2545 dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1)),
2546 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2)));
2547 ge = isl_pw_aff_ge_set(isl_pw_aff_copy(pwaff1),
2548 isl_pw_aff_copy(pwaff2));
2549 dom = isl_set_subtract(dom, isl_set_copy(ge));
2550 return isl_pw_aff_select(ge, pwaff1, dom, pwaff2);
2553 __isl_give isl_pw_aff *isl_pw_aff_max(__isl_take isl_pw_aff *pwaff1,
2554 __isl_take isl_pw_aff *pwaff2)
2556 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_max);
2559 static __isl_give isl_pw_aff *pw_aff_list_reduce(
2560 __isl_take isl_pw_aff_list *list,
2561 __isl_give isl_pw_aff *(*fn)(__isl_take isl_pw_aff *pwaff1,
2562 __isl_take isl_pw_aff *pwaff2))
2571 ctx = isl_pw_aff_list_get_ctx(list);
2573 isl_die(ctx, isl_error_invalid,
2574 "list should contain at least one element",
2575 return isl_pw_aff_list_free(list));
2577 res = isl_pw_aff_copy(list->p[0]);
2578 for (i = 1; i < list->n; ++i)
2579 res = fn(res, isl_pw_aff_copy(list->p[i]));
2581 isl_pw_aff_list_free(list);
2585 /* Return an isl_pw_aff that maps each element in the intersection of the
2586 * domains of the elements of list to the minimal corresponding affine
2589 __isl_give isl_pw_aff *isl_pw_aff_list_min(__isl_take isl_pw_aff_list *list)
2591 return pw_aff_list_reduce(list, &isl_pw_aff_min);
2594 /* Return an isl_pw_aff that maps each element in the intersection of the
2595 * domains of the elements of list to the maximal corresponding affine
2598 __isl_give isl_pw_aff *isl_pw_aff_list_max(__isl_take isl_pw_aff_list *list)
2600 return pw_aff_list_reduce(list, &isl_pw_aff_max);
2603 /* Mark the domains of "pwaff" as rational.
2605 __isl_give isl_pw_aff *isl_pw_aff_set_rational(__isl_take isl_pw_aff *pwaff)
2609 pwaff = isl_pw_aff_cow(pwaff);
2615 for (i = 0; i < pwaff->n; ++i) {
2616 pwaff->p[i].set = isl_set_set_rational(pwaff->p[i].set);
2617 if (!pwaff->p[i].set)
2618 return isl_pw_aff_free(pwaff);
2624 /* Mark the domains of the elements of "list" as rational.
2626 __isl_give isl_pw_aff_list *isl_pw_aff_list_set_rational(
2627 __isl_take isl_pw_aff_list *list)
2637 for (i = 0; i < n; ++i) {
2640 pa = isl_pw_aff_list_get_pw_aff(list, i);
2641 pa = isl_pw_aff_set_rational(pa);
2642 list = isl_pw_aff_list_set_pw_aff(list, i, pa);
2651 #include <isl_multi_templ.c>
2653 /* Create an isl_pw_multi_aff with the given isl_multi_aff on a universe
2656 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_multi_aff(
2657 __isl_take isl_multi_aff *ma)
2659 isl_set *dom = isl_set_universe(isl_multi_aff_get_domain_space(ma));
2660 return isl_pw_multi_aff_alloc(dom, ma);
2663 /* Create a piecewise multi-affine expression in the given space that maps each
2664 * input dimension to the corresponding output dimension.
2666 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_identity(
2667 __isl_take isl_space *space)
2669 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_identity(space));
2672 __isl_give isl_multi_aff *isl_multi_aff_add(__isl_take isl_multi_aff *maff1,
2673 __isl_take isl_multi_aff *maff2)
2678 maff1 = isl_multi_aff_cow(maff1);
2679 if (!maff1 || !maff2)
2682 ctx = isl_multi_aff_get_ctx(maff1);
2683 if (!isl_space_is_equal(maff1->space, maff2->space))
2684 isl_die(ctx, isl_error_invalid,
2685 "spaces don't match", goto error);
2687 for (i = 0; i < maff1->n; ++i) {
2688 maff1->p[i] = isl_aff_add(maff1->p[i],
2689 isl_aff_copy(maff2->p[i]));
2694 isl_multi_aff_free(maff2);
2697 isl_multi_aff_free(maff1);
2698 isl_multi_aff_free(maff2);
2702 /* Given two multi-affine expressions A -> B and C -> D,
2703 * construct a multi-affine expression [A -> C] -> [B -> D].
2705 __isl_give isl_multi_aff *isl_multi_aff_product(
2706 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2)
2712 int in1, in2, out1, out2;
2714 in1 = isl_multi_aff_dim(ma1, isl_dim_in);
2715 in2 = isl_multi_aff_dim(ma2, isl_dim_in);
2716 out1 = isl_multi_aff_dim(ma1, isl_dim_out);
2717 out2 = isl_multi_aff_dim(ma2, isl_dim_out);
2718 space = isl_space_product(isl_multi_aff_get_space(ma1),
2719 isl_multi_aff_get_space(ma2));
2720 res = isl_multi_aff_alloc(isl_space_copy(space));
2721 space = isl_space_domain(space);
2723 for (i = 0; i < out1; ++i) {
2724 aff = isl_multi_aff_get_aff(ma1, i);
2725 aff = isl_aff_insert_dims(aff, isl_dim_in, in1, in2);
2726 aff = isl_aff_reset_domain_space(aff, isl_space_copy(space));
2727 res = isl_multi_aff_set_aff(res, i, aff);
2730 for (i = 0; i < out2; ++i) {
2731 aff = isl_multi_aff_get_aff(ma2, i);
2732 aff = isl_aff_insert_dims(aff, isl_dim_in, 0, in1);
2733 aff = isl_aff_reset_domain_space(aff, isl_space_copy(space));
2734 res = isl_multi_aff_set_aff(res, out1 + i, aff);
2737 isl_space_free(space);
2738 isl_multi_aff_free(ma1);
2739 isl_multi_aff_free(ma2);
2743 /* Exploit the equalities in "eq" to simplify the affine expressions.
2745 static __isl_give isl_multi_aff *isl_multi_aff_substitute_equalities(
2746 __isl_take isl_multi_aff *maff, __isl_take isl_basic_set *eq)
2750 maff = isl_multi_aff_cow(maff);
2754 for (i = 0; i < maff->n; ++i) {
2755 maff->p[i] = isl_aff_substitute_equalities(maff->p[i],
2756 isl_basic_set_copy(eq));
2761 isl_basic_set_free(eq);
2764 isl_basic_set_free(eq);
2765 isl_multi_aff_free(maff);
2769 __isl_give isl_multi_aff *isl_multi_aff_scale(__isl_take isl_multi_aff *maff,
2774 maff = isl_multi_aff_cow(maff);
2778 for (i = 0; i < maff->n; ++i) {
2779 maff->p[i] = isl_aff_scale(maff->p[i], f);
2781 return isl_multi_aff_free(maff);
2787 __isl_give isl_multi_aff *isl_multi_aff_add_on_domain(__isl_keep isl_set *dom,
2788 __isl_take isl_multi_aff *maff1, __isl_take isl_multi_aff *maff2)
2790 maff1 = isl_multi_aff_add(maff1, maff2);
2791 maff1 = isl_multi_aff_gist(maff1, isl_set_copy(dom));
2795 int isl_multi_aff_is_empty(__isl_keep isl_multi_aff *maff)
2803 int isl_multi_aff_plain_is_equal(__isl_keep isl_multi_aff *maff1,
2804 __isl_keep isl_multi_aff *maff2)
2809 if (!maff1 || !maff2)
2811 if (maff1->n != maff2->n)
2813 equal = isl_space_is_equal(maff1->space, maff2->space);
2814 if (equal < 0 || !equal)
2817 for (i = 0; i < maff1->n; ++i) {
2818 equal = isl_aff_plain_is_equal(maff1->p[i], maff2->p[i]);
2819 if (equal < 0 || !equal)
2826 /* Return the set of domain elements where "ma1" is lexicographically
2827 * smaller than or equal to "ma2".
2829 __isl_give isl_set *isl_multi_aff_lex_le_set(__isl_take isl_multi_aff *ma1,
2830 __isl_take isl_multi_aff *ma2)
2832 return isl_multi_aff_lex_ge_set(ma2, ma1);
2835 /* Return the set of domain elements where "ma1" is lexicographically
2836 * greater than or equal to "ma2".
2838 __isl_give isl_set *isl_multi_aff_lex_ge_set(__isl_take isl_multi_aff *ma1,
2839 __isl_take isl_multi_aff *ma2)
2842 isl_map *map1, *map2;
2845 map1 = isl_map_from_multi_aff(ma1);
2846 map2 = isl_map_from_multi_aff(ma2);
2847 map = isl_map_range_product(map1, map2);
2848 space = isl_space_range(isl_map_get_space(map));
2849 space = isl_space_domain(isl_space_unwrap(space));
2850 ge = isl_map_lex_ge(space);
2851 map = isl_map_intersect_range(map, isl_map_wrap(ge));
2853 return isl_map_domain(map);
2857 #define PW isl_pw_multi_aff
2859 #define EL isl_multi_aff
2861 #define EL_IS_ZERO is_empty
2865 #define IS_ZERO is_empty
2868 #undef DEFAULT_IS_ZERO
2869 #define DEFAULT_IS_ZERO 0
2874 #define NO_INVOLVES_DIMS
2875 #define NO_MOVE_DIMS
2876 #define NO_INSERT_DIMS
2880 #include <isl_pw_templ.c>
2883 #define UNION isl_union_pw_multi_aff
2885 #define PART isl_pw_multi_aff
2887 #define PARTS pw_multi_aff
2888 #define ALIGN_DOMAIN
2892 #include <isl_union_templ.c>
2894 /* Given a function "cmp" that returns the set of elements where
2895 * "ma1" is "better" than "ma2", return the intersection of this
2896 * set with "dom1" and "dom2".
2898 static __isl_give isl_set *shared_and_better(__isl_keep isl_set *dom1,
2899 __isl_keep isl_set *dom2, __isl_keep isl_multi_aff *ma1,
2900 __isl_keep isl_multi_aff *ma2,
2901 __isl_give isl_set *(*cmp)(__isl_take isl_multi_aff *ma1,
2902 __isl_take isl_multi_aff *ma2))
2908 common = isl_set_intersect(isl_set_copy(dom1), isl_set_copy(dom2));
2909 is_empty = isl_set_plain_is_empty(common);
2910 if (is_empty >= 0 && is_empty)
2913 return isl_set_free(common);
2914 better = cmp(isl_multi_aff_copy(ma1), isl_multi_aff_copy(ma2));
2915 better = isl_set_intersect(common, better);
2920 /* Given a function "cmp" that returns the set of elements where
2921 * "ma1" is "better" than "ma2", return a piecewise multi affine
2922 * expression defined on the union of the definition domains
2923 * of "pma1" and "pma2" that maps to the "best" of "pma1" and
2924 * "pma2" on each cell. If only one of the two input functions
2925 * is defined on a given cell, then it is considered the best.
2927 static __isl_give isl_pw_multi_aff *pw_multi_aff_union_opt(
2928 __isl_take isl_pw_multi_aff *pma1,
2929 __isl_take isl_pw_multi_aff *pma2,
2930 __isl_give isl_set *(*cmp)(__isl_take isl_multi_aff *ma1,
2931 __isl_take isl_multi_aff *ma2))
2934 isl_pw_multi_aff *res = NULL;
2936 isl_set *set = NULL;
2941 ctx = isl_space_get_ctx(pma1->dim);
2942 if (!isl_space_is_equal(pma1->dim, pma2->dim))
2943 isl_die(ctx, isl_error_invalid,
2944 "arguments should live in the same space", goto error);
2946 if (isl_pw_multi_aff_is_empty(pma1)) {
2947 isl_pw_multi_aff_free(pma1);
2951 if (isl_pw_multi_aff_is_empty(pma2)) {
2952 isl_pw_multi_aff_free(pma2);
2956 n = 2 * (pma1->n + 1) * (pma2->n + 1);
2957 res = isl_pw_multi_aff_alloc_size(isl_space_copy(pma1->dim), n);
2959 for (i = 0; i < pma1->n; ++i) {
2960 set = isl_set_copy(pma1->p[i].set);
2961 for (j = 0; j < pma2->n; ++j) {
2965 better = shared_and_better(pma2->p[j].set,
2966 pma1->p[i].set, pma2->p[j].maff,
2967 pma1->p[i].maff, cmp);
2968 is_empty = isl_set_plain_is_empty(better);
2969 if (is_empty < 0 || is_empty) {
2970 isl_set_free(better);
2975 set = isl_set_subtract(set, isl_set_copy(better));
2977 res = isl_pw_multi_aff_add_piece(res, better,
2978 isl_multi_aff_copy(pma2->p[j].maff));
2980 res = isl_pw_multi_aff_add_piece(res, set,
2981 isl_multi_aff_copy(pma1->p[i].maff));
2984 for (j = 0; j < pma2->n; ++j) {
2985 set = isl_set_copy(pma2->p[j].set);
2986 for (i = 0; i < pma1->n; ++i)
2987 set = isl_set_subtract(set,
2988 isl_set_copy(pma1->p[i].set));
2989 res = isl_pw_multi_aff_add_piece(res, set,
2990 isl_multi_aff_copy(pma2->p[j].maff));
2993 isl_pw_multi_aff_free(pma1);
2994 isl_pw_multi_aff_free(pma2);
2998 isl_pw_multi_aff_free(pma1);
2999 isl_pw_multi_aff_free(pma2);
3001 return isl_pw_multi_aff_free(res);
3004 static __isl_give isl_pw_multi_aff *pw_multi_aff_union_lexmax(
3005 __isl_take isl_pw_multi_aff *pma1,
3006 __isl_take isl_pw_multi_aff *pma2)
3008 return pw_multi_aff_union_opt(pma1, pma2, &isl_multi_aff_lex_ge_set);
3011 /* Given two piecewise multi affine expressions, return a piecewise
3012 * multi-affine expression defined on the union of the definition domains
3013 * of the inputs that is equal to the lexicographic maximum of the two
3014 * inputs on each cell. If only one of the two inputs is defined on
3015 * a given cell, then it is considered to be the maximum.
3017 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_lexmax(
3018 __isl_take isl_pw_multi_aff *pma1,
3019 __isl_take isl_pw_multi_aff *pma2)
3021 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
3022 &pw_multi_aff_union_lexmax);
3025 static __isl_give isl_pw_multi_aff *pw_multi_aff_union_lexmin(
3026 __isl_take isl_pw_multi_aff *pma1,
3027 __isl_take isl_pw_multi_aff *pma2)
3029 return pw_multi_aff_union_opt(pma1, pma2, &isl_multi_aff_lex_le_set);
3032 /* Given two piecewise multi affine expressions, return a piecewise
3033 * multi-affine expression defined on the union of the definition domains
3034 * of the inputs that is equal to the lexicographic minimum of the two
3035 * inputs on each cell. If only one of the two inputs is defined on
3036 * a given cell, then it is considered to be the minimum.
3038 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_lexmin(
3039 __isl_take isl_pw_multi_aff *pma1,
3040 __isl_take isl_pw_multi_aff *pma2)
3042 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
3043 &pw_multi_aff_union_lexmin);
3046 static __isl_give isl_pw_multi_aff *pw_multi_aff_add(
3047 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
3049 return isl_pw_multi_aff_on_shared_domain(pma1, pma2,
3050 &isl_multi_aff_add);
3053 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_add(
3054 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
3056 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
3060 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_add(
3061 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
3063 return isl_pw_multi_aff_union_add_(pma1, pma2);
3066 /* Given two piecewise multi-affine expressions A -> B and C -> D,
3067 * construct a piecewise multi-affine expression [A -> C] -> [B -> D].
3069 static __isl_give isl_pw_multi_aff *pw_multi_aff_product(
3070 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
3074 isl_pw_multi_aff *res;
3079 n = pma1->n * pma2->n;
3080 space = isl_space_product(isl_space_copy(pma1->dim),
3081 isl_space_copy(pma2->dim));
3082 res = isl_pw_multi_aff_alloc_size(space, n);
3084 for (i = 0; i < pma1->n; ++i) {
3085 for (j = 0; j < pma2->n; ++j) {
3089 domain = isl_set_product(isl_set_copy(pma1->p[i].set),
3090 isl_set_copy(pma2->p[j].set));
3091 ma = isl_multi_aff_product(
3092 isl_multi_aff_copy(pma1->p[i].maff),
3093 isl_multi_aff_copy(pma2->p[i].maff));
3094 res = isl_pw_multi_aff_add_piece(res, domain, ma);
3098 isl_pw_multi_aff_free(pma1);
3099 isl_pw_multi_aff_free(pma2);
3102 isl_pw_multi_aff_free(pma1);
3103 isl_pw_multi_aff_free(pma2);
3107 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_product(
3108 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
3110 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
3111 &pw_multi_aff_product);
3114 /* Construct a map mapping the domain of the piecewise multi-affine expression
3115 * to its range, with each dimension in the range equated to the
3116 * corresponding affine expression on its cell.
3118 __isl_give isl_map *isl_map_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma)
3126 map = isl_map_empty(isl_pw_multi_aff_get_space(pma));
3128 for (i = 0; i < pma->n; ++i) {
3129 isl_multi_aff *maff;
3130 isl_basic_map *bmap;
3133 maff = isl_multi_aff_copy(pma->p[i].maff);
3134 bmap = isl_basic_map_from_multi_aff(maff);
3135 map_i = isl_map_from_basic_map(bmap);
3136 map_i = isl_map_intersect_domain(map_i,
3137 isl_set_copy(pma->p[i].set));
3138 map = isl_map_union_disjoint(map, map_i);
3141 isl_pw_multi_aff_free(pma);
3145 __isl_give isl_set *isl_set_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma)
3150 if (!isl_space_is_set(pma->dim))
3151 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
3152 "isl_pw_multi_aff cannot be converted into an isl_set",
3153 return isl_pw_multi_aff_free(pma));
3155 return isl_map_from_pw_multi_aff(pma);
3158 /* Given a basic map with a single output dimension that is defined
3159 * in terms of the parameters and input dimensions using an equality,
3160 * extract an isl_aff that expresses the output dimension in terms
3161 * of the parameters and input dimensions.
3163 * Since some applications expect the result of isl_pw_multi_aff_from_map
3164 * to only contain integer affine expressions, we compute the floor
3165 * of the expression before returning.
3167 * This function shares some similarities with
3168 * isl_basic_map_has_defining_equality and isl_constraint_get_bound.
3170 static __isl_give isl_aff *extract_isl_aff_from_basic_map(
3171 __isl_take isl_basic_map *bmap)
3176 isl_local_space *ls;
3181 if (isl_basic_map_dim(bmap, isl_dim_out) != 1)
3182 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
3183 "basic map should have a single output dimension",
3185 offset = isl_basic_map_offset(bmap, isl_dim_out);
3186 total = isl_basic_map_total_dim(bmap);
3187 for (i = 0; i < bmap->n_eq; ++i) {
3188 if (isl_int_is_zero(bmap->eq[i][offset]))
3190 if (isl_seq_first_non_zero(bmap->eq[i] + offset + 1,
3191 1 + total - (offset + 1)) != -1)
3195 if (i >= bmap->n_eq)
3196 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
3197 "unable to find suitable equality", goto error);
3198 ls = isl_basic_map_get_local_space(bmap);
3199 aff = isl_aff_alloc(isl_local_space_domain(ls));
3202 if (isl_int_is_neg(bmap->eq[i][offset]))
3203 isl_seq_cpy(aff->v->el + 1, bmap->eq[i], offset);
3205 isl_seq_neg(aff->v->el + 1, bmap->eq[i], offset);
3206 isl_seq_clr(aff->v->el + 1 + offset, aff->v->size - (1 + offset));
3207 isl_int_abs(aff->v->el[0], bmap->eq[i][offset]);
3208 isl_basic_map_free(bmap);
3210 aff = isl_aff_remove_unused_divs(aff);
3211 aff = isl_aff_floor(aff);
3214 isl_basic_map_free(bmap);
3218 /* Given a basic map where each output dimension is defined
3219 * in terms of the parameters and input dimensions using an equality,
3220 * extract an isl_multi_aff that expresses the output dimensions in terms
3221 * of the parameters and input dimensions.
3223 static __isl_give isl_multi_aff *extract_isl_multi_aff_from_basic_map(
3224 __isl_take isl_basic_map *bmap)
3233 ma = isl_multi_aff_alloc(isl_basic_map_get_space(bmap));
3234 n_out = isl_basic_map_dim(bmap, isl_dim_out);
3236 for (i = 0; i < n_out; ++i) {
3237 isl_basic_map *bmap_i;
3240 bmap_i = isl_basic_map_copy(bmap);
3241 bmap_i = isl_basic_map_project_out(bmap_i, isl_dim_out,
3242 i + 1, n_out - (1 + i));
3243 bmap_i = isl_basic_map_project_out(bmap_i, isl_dim_out, 0, i);
3244 aff = extract_isl_aff_from_basic_map(bmap_i);
3245 ma = isl_multi_aff_set_aff(ma, i, aff);
3248 isl_basic_map_free(bmap);
3253 /* Create an isl_pw_multi_aff that is equivalent to
3254 * isl_map_intersect_domain(isl_map_from_basic_map(bmap), domain).
3255 * The given basic map is such that each output dimension is defined
3256 * in terms of the parameters and input dimensions using an equality.
3258 static __isl_give isl_pw_multi_aff *plain_pw_multi_aff_from_map(
3259 __isl_take isl_set *domain, __isl_take isl_basic_map *bmap)
3263 ma = extract_isl_multi_aff_from_basic_map(bmap);
3264 return isl_pw_multi_aff_alloc(domain, ma);
3267 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
3268 * This obviously only works if the input "map" is single-valued.
3269 * If so, we compute the lexicographic minimum of the image in the form
3270 * of an isl_pw_multi_aff. Since the image is unique, it is equal
3271 * to its lexicographic minimum.
3272 * If the input is not single-valued, we produce an error.
3274 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_base(
3275 __isl_take isl_map *map)
3279 isl_pw_multi_aff *pma;
3281 sv = isl_map_is_single_valued(map);
3285 isl_die(isl_map_get_ctx(map), isl_error_invalid,
3286 "map is not single-valued", goto error);
3287 map = isl_map_make_disjoint(map);
3291 pma = isl_pw_multi_aff_empty(isl_map_get_space(map));
3293 for (i = 0; i < map->n; ++i) {
3294 isl_pw_multi_aff *pma_i;
3295 isl_basic_map *bmap;
3296 bmap = isl_basic_map_copy(map->p[i]);
3297 pma_i = isl_basic_map_lexmin_pw_multi_aff(bmap);
3298 pma = isl_pw_multi_aff_add_disjoint(pma, pma_i);
3308 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
3309 * taking into account that the output dimension at position "d"
3310 * can be represented as
3312 * x = floor((e(...) + c1) / m)
3314 * given that constraint "i" is of the form
3316 * e(...) + c1 - m x >= 0
3319 * Let "map" be of the form
3323 * We construct a mapping
3325 * A -> [A -> x = floor(...)]
3327 * apply that to the map, obtaining
3329 * [A -> x = floor(...)] -> B
3331 * and equate dimension "d" to x.
3332 * We then compute a isl_pw_multi_aff representation of the resulting map
3333 * and plug in the mapping above.
3335 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_div(
3336 __isl_take isl_map *map, __isl_take isl_basic_map *hull, int d, int i)
3340 isl_local_space *ls;
3348 isl_pw_multi_aff *pma;
3351 is_set = isl_map_is_set(map);
3353 offset = isl_basic_map_offset(hull, isl_dim_out);
3354 ctx = isl_map_get_ctx(map);
3355 space = isl_space_domain(isl_map_get_space(map));
3356 n_in = isl_space_dim(space, isl_dim_set);
3357 n = isl_space_dim(space, isl_dim_all);
3359 v = isl_vec_alloc(ctx, 1 + 1 + n);
3361 isl_int_neg(v->el[0], hull->ineq[i][offset + d]);
3362 isl_seq_cpy(v->el + 1, hull->ineq[i], 1 + n);
3364 isl_basic_map_free(hull);
3366 ls = isl_local_space_from_space(isl_space_copy(space));
3367 aff = isl_aff_alloc_vec(ls, v);
3368 aff = isl_aff_floor(aff);
3370 isl_space_free(space);
3371 ma = isl_multi_aff_from_aff(aff);
3373 ma = isl_multi_aff_identity(isl_space_map_from_set(space));
3374 ma = isl_multi_aff_range_product(ma,
3375 isl_multi_aff_from_aff(aff));
3378 insert = isl_map_from_multi_aff(isl_multi_aff_copy(ma));
3379 map = isl_map_apply_domain(map, insert);
3380 map = isl_map_equate(map, isl_dim_in, n_in, isl_dim_out, d);
3381 pma = isl_pw_multi_aff_from_map(map);
3382 pma = isl_pw_multi_aff_pullback_multi_aff(pma, ma);
3387 /* Is constraint "c" of the form
3389 * e(...) + c1 - m x >= 0
3393 * -e(...) + c2 + m x >= 0
3395 * where m > 1 and e only depends on parameters and input dimemnsions?
3397 * "offset" is the offset of the output dimensions
3398 * "pos" is the position of output dimension x.
3400 static int is_potential_div_constraint(isl_int *c, int offset, int d, int total)
3402 if (isl_int_is_zero(c[offset + d]))
3404 if (isl_int_is_one(c[offset + d]))
3406 if (isl_int_is_negone(c[offset + d]))
3408 if (isl_seq_first_non_zero(c + offset, d) != -1)
3410 if (isl_seq_first_non_zero(c + offset + d + 1,
3411 total - (offset + d + 1)) != -1)
3416 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
3418 * As a special case, we first check if there is any pair of constraints,
3419 * shared by all the basic maps in "map" that force a given dimension
3420 * to be equal to the floor of some affine combination of the input dimensions.
3422 * In particular, if we can find two constraints
3424 * e(...) + c1 - m x >= 0 i.e., m x <= e(...) + c1
3428 * -e(...) + c2 + m x >= 0 i.e., m x >= e(...) - c2
3430 * where m > 1 and e only depends on parameters and input dimemnsions,
3433 * c1 + c2 < m i.e., -c2 >= c1 - (m - 1)
3435 * then we know that we can take
3437 * x = floor((e(...) + c1) / m)
3439 * without having to perform any computation.
3441 * Note that we know that
3445 * If c1 + c2 were 0, then we would have detected an equality during
3446 * simplification. If c1 + c2 were negative, then we would have detected
3449 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_check_div(
3450 __isl_take isl_map *map)
3456 isl_basic_map *hull;
3458 hull = isl_map_unshifted_simple_hull(isl_map_copy(map));
3463 dim = isl_map_dim(map, isl_dim_out);
3464 offset = isl_basic_map_offset(hull, isl_dim_out);
3465 total = 1 + isl_basic_map_total_dim(hull);
3467 for (d = 0; d < dim; ++d) {
3468 for (i = 0; i < n; ++i) {
3469 if (!is_potential_div_constraint(hull->ineq[i],
3472 for (j = i + 1; j < n; ++j) {
3473 if (!isl_seq_is_neg(hull->ineq[i] + 1,
3474 hull->ineq[j] + 1, total - 1))
3476 isl_int_add(sum, hull->ineq[i][0],
3478 if (isl_int_abs_lt(sum,
3479 hull->ineq[i][offset + d]))
3486 if (isl_int_is_pos(hull->ineq[j][offset + d]))
3488 return pw_multi_aff_from_map_div(map, hull, d, j);
3492 isl_basic_map_free(hull);
3493 return pw_multi_aff_from_map_base(map);
3496 isl_basic_map_free(hull);
3500 /* Given an affine expression
3502 * [A -> B] -> f(A,B)
3504 * construct an isl_multi_aff
3508 * such that dimension "d" in B' is set to "aff" and the remaining
3509 * dimensions are set equal to the corresponding dimensions in B.
3510 * "n_in" is the dimension of the space A.
3511 * "n_out" is the dimension of the space B.
3513 * If "is_set" is set, then the affine expression is of the form
3517 * and we construct an isl_multi_aff
3521 static __isl_give isl_multi_aff *range_map(__isl_take isl_aff *aff, int d,
3522 unsigned n_in, unsigned n_out, int is_set)
3526 isl_space *space, *space2;
3527 isl_local_space *ls;
3529 space = isl_aff_get_domain_space(aff);
3530 ls = isl_local_space_from_space(isl_space_copy(space));
3531 space2 = isl_space_copy(space);
3533 space2 = isl_space_range(isl_space_unwrap(space2));
3534 space = isl_space_map_from_domain_and_range(space, space2);
3535 ma = isl_multi_aff_alloc(space);
3536 ma = isl_multi_aff_set_aff(ma, d, aff);
3538 for (i = 0; i < n_out; ++i) {
3541 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
3542 isl_dim_set, n_in + i);
3543 ma = isl_multi_aff_set_aff(ma, i, aff);
3546 isl_local_space_free(ls);
3551 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
3552 * taking into account that the dimension at position "d" can be written as
3554 * x = m a + f(..) (1)
3556 * where m is equal to "gcd".
3557 * "i" is the index of the equality in "hull" that defines f(..).
3558 * In particular, the equality is of the form
3560 * f(..) - x + m g(existentials) = 0
3564 * -f(..) + x + m g(existentials) = 0
3566 * We basically plug (1) into "map", resulting in a map with "a"
3567 * in the range instead of "x". The corresponding isl_pw_multi_aff
3568 * defining "a" is then plugged back into (1) to obtain a definition fro "x".
3570 * Specifically, given the input map
3574 * We first wrap it into a set
3578 * and define (1) on top of the corresponding space, resulting in "aff".
3579 * We use this to create an isl_multi_aff that maps the output position "d"
3580 * from "a" to "x", leaving all other (intput and output) dimensions unchanged.
3581 * We plug this into the wrapped map, unwrap the result and compute the
3582 * corresponding isl_pw_multi_aff.
3583 * The result is an expression
3591 * so that we can plug that into "aff", after extending the latter to
3597 * If "map" is actually a set, then there is no "A" space, meaning
3598 * that we do not need to perform any wrapping, and that the result
3599 * of the recursive call is of the form
3603 * which is plugged into a mapping of the form
3607 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_stride(
3608 __isl_take isl_map *map, __isl_take isl_basic_map *hull, int d, int i,
3613 isl_local_space *ls;
3616 isl_pw_multi_aff *pma, *id;
3622 is_set = isl_map_is_set(map);
3624 n_in = isl_basic_map_dim(hull, isl_dim_in);
3625 n_out = isl_basic_map_dim(hull, isl_dim_out);
3626 o_out = isl_basic_map_offset(hull, isl_dim_out);
3631 set = isl_map_wrap(map);
3632 space = isl_space_map_from_set(isl_set_get_space(set));
3633 ma = isl_multi_aff_identity(space);
3634 ls = isl_local_space_from_space(isl_set_get_space(set));
3635 aff = isl_aff_alloc(ls);
3637 isl_int_set_si(aff->v->el[0], 1);
3638 if (isl_int_is_one(hull->eq[i][o_out + d]))
3639 isl_seq_neg(aff->v->el + 1, hull->eq[i],
3642 isl_seq_cpy(aff->v->el + 1, hull->eq[i],
3644 isl_int_set(aff->v->el[1 + o_out + d], gcd);
3646 ma = isl_multi_aff_set_aff(ma, n_in + d, isl_aff_copy(aff));
3647 set = isl_set_preimage_multi_aff(set, ma);
3649 ma = range_map(aff, d, n_in, n_out, is_set);
3654 map = isl_set_unwrap(set);
3655 pma = isl_pw_multi_aff_from_map(set);
3658 space = isl_pw_multi_aff_get_domain_space(pma);
3659 space = isl_space_map_from_set(space);
3660 id = isl_pw_multi_aff_identity(space);
3661 pma = isl_pw_multi_aff_range_product(id, pma);
3663 id = isl_pw_multi_aff_from_multi_aff(ma);
3664 pma = isl_pw_multi_aff_pullback_pw_multi_aff(id, pma);
3666 isl_basic_map_free(hull);
3670 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
3672 * As a special case, we first check if all output dimensions are uniquely
3673 * defined in terms of the parameters and input dimensions over the entire
3674 * domain. If so, we extract the desired isl_pw_multi_aff directly
3675 * from the affine hull of "map" and its domain.
3677 * Otherwise, we check if any of the output dimensions is "strided".
3678 * That is, we check if can be written as
3682 * with m greater than 1, a some combination of existentiall quantified
3683 * variables and f and expression in the parameters and input dimensions.
3684 * If so, we remove the stride in pw_multi_aff_from_map_stride.
3686 * Otherwise, we continue with pw_multi_aff_from_map_check_div for a further
3689 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_map(__isl_take isl_map *map)
3693 isl_basic_map *hull;
3703 hull = isl_map_affine_hull(isl_map_copy(map));
3704 sv = isl_basic_map_plain_is_single_valued(hull);
3706 return plain_pw_multi_aff_from_map(isl_map_domain(map), hull);
3708 hull = isl_basic_map_free(hull);
3712 n_div = isl_basic_map_dim(hull, isl_dim_div);
3713 o_div = isl_basic_map_offset(hull, isl_dim_div);
3716 isl_basic_map_free(hull);
3717 return pw_multi_aff_from_map_check_div(map);
3722 n_out = isl_basic_map_dim(hull, isl_dim_out);
3723 o_out = isl_basic_map_offset(hull, isl_dim_out);
3725 for (i = 0; i < n_out; ++i) {
3726 for (j = 0; j < hull->n_eq; ++j) {
3727 isl_int *eq = hull->eq[j];
3728 isl_pw_multi_aff *res;
3730 if (!isl_int_is_one(eq[o_out + i]) &&
3731 !isl_int_is_negone(eq[o_out + i]))
3733 if (isl_seq_first_non_zero(eq + o_out, i) != -1)
3735 if (isl_seq_first_non_zero(eq + o_out + i + 1,
3736 n_out - (i + 1)) != -1)
3738 isl_seq_gcd(eq + o_div, n_div, &gcd);
3739 if (isl_int_is_zero(gcd))
3741 if (isl_int_is_one(gcd))
3744 res = pw_multi_aff_from_map_stride(map, hull,
3752 isl_basic_map_free(hull);
3753 return pw_multi_aff_from_map_check_div(map);
3759 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_set(__isl_take isl_set *set)
3761 return isl_pw_multi_aff_from_map(set);
3764 /* Return the piecewise affine expression "set ? 1 : 0".
3766 __isl_give isl_pw_aff *isl_set_indicator_function(__isl_take isl_set *set)
3769 isl_space *space = isl_set_get_space(set);
3770 isl_local_space *ls = isl_local_space_from_space(space);
3771 isl_aff *zero = isl_aff_zero_on_domain(isl_local_space_copy(ls));
3772 isl_aff *one = isl_aff_zero_on_domain(ls);
3774 one = isl_aff_add_constant_si(one, 1);
3775 pa = isl_pw_aff_alloc(isl_set_copy(set), one);
3776 set = isl_set_complement(set);
3777 pa = isl_pw_aff_add_disjoint(pa, isl_pw_aff_alloc(set, zero));
3782 /* Plug in "subs" for dimension "type", "pos" of "aff".
3784 * Let i be the dimension to replace and let "subs" be of the form
3788 * and "aff" of the form
3794 * (a f + d g')/(m d)
3796 * where g' is the result of plugging in "subs" in each of the integer
3799 __isl_give isl_aff *isl_aff_substitute(__isl_take isl_aff *aff,
3800 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
3805 aff = isl_aff_cow(aff);
3807 return isl_aff_free(aff);
3809 ctx = isl_aff_get_ctx(aff);
3810 if (!isl_space_is_equal(aff->ls->dim, subs->ls->dim))
3811 isl_die(ctx, isl_error_invalid,
3812 "spaces don't match", return isl_aff_free(aff));
3813 if (isl_local_space_dim(subs->ls, isl_dim_div) != 0)
3814 isl_die(ctx, isl_error_unsupported,
3815 "cannot handle divs yet", return isl_aff_free(aff));
3817 aff->ls = isl_local_space_substitute(aff->ls, type, pos, subs);
3819 return isl_aff_free(aff);
3821 aff->v = isl_vec_cow(aff->v);
3823 return isl_aff_free(aff);
3825 pos += isl_local_space_offset(aff->ls, type);
3828 isl_seq_substitute(aff->v->el, pos, subs->v->el,
3829 aff->v->size, subs->v->size, v);
3835 /* Plug in "subs" for dimension "type", "pos" in each of the affine
3836 * expressions in "maff".
3838 __isl_give isl_multi_aff *isl_multi_aff_substitute(
3839 __isl_take isl_multi_aff *maff, enum isl_dim_type type, unsigned pos,
3840 __isl_keep isl_aff *subs)
3844 maff = isl_multi_aff_cow(maff);
3846 return isl_multi_aff_free(maff);
3848 if (type == isl_dim_in)
3851 for (i = 0; i < maff->n; ++i) {
3852 maff->p[i] = isl_aff_substitute(maff->p[i], type, pos, subs);
3854 return isl_multi_aff_free(maff);
3860 /* Plug in "subs" for dimension "type", "pos" of "pma".
3862 * pma is of the form
3866 * while subs is of the form
3868 * v' = B_j(v) -> S_j
3870 * Each pair i,j such that C_ij = A_i \cap B_i is non-empty
3871 * has a contribution in the result, in particular
3873 * C_ij(S_j) -> M_i(S_j)
3875 * Note that plugging in S_j in C_ij may also result in an empty set
3876 * and this contribution should simply be discarded.
3878 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_substitute(
3879 __isl_take isl_pw_multi_aff *pma, enum isl_dim_type type, unsigned pos,
3880 __isl_keep isl_pw_aff *subs)
3883 isl_pw_multi_aff *res;
3886 return isl_pw_multi_aff_free(pma);
3888 n = pma->n * subs->n;
3889 res = isl_pw_multi_aff_alloc_size(isl_space_copy(pma->dim), n);
3891 for (i = 0; i < pma->n; ++i) {
3892 for (j = 0; j < subs->n; ++j) {
3894 isl_multi_aff *res_ij;
3897 common = isl_set_intersect(
3898 isl_set_copy(pma->p[i].set),
3899 isl_set_copy(subs->p[j].set));
3900 common = isl_set_substitute(common,
3901 type, pos, subs->p[j].aff);
3902 empty = isl_set_plain_is_empty(common);
3903 if (empty < 0 || empty) {
3904 isl_set_free(common);
3910 res_ij = isl_multi_aff_substitute(
3911 isl_multi_aff_copy(pma->p[i].maff),
3912 type, pos, subs->p[j].aff);
3914 res = isl_pw_multi_aff_add_piece(res, common, res_ij);
3918 isl_pw_multi_aff_free(pma);
3921 isl_pw_multi_aff_free(pma);
3922 isl_pw_multi_aff_free(res);
3926 /* Compute the preimage of the affine expression "src" under "ma"
3927 * and put the result in "dst". If "has_denom" is set (to one),
3928 * then "src" and "dst" have an extra initial denominator.
3929 * "n_div_ma" is the number of existentials in "ma"
3930 * "n_div_bset" is the number of existentials in "src"
3931 * The resulting "dst" (which is assumed to have been allocated by
3932 * the caller) contains coefficients for both sets of existentials,
3933 * first those in "ma" and then those in "src".
3934 * f, c1, c2 and g are temporary objects that have been initialized
3937 * Let src represent the expression
3939 * (a(p) + b x + c(divs))/d
3941 * and let ma represent the expressions
3943 * x_i = (r_i(p) + s_i(y) + t_i(divs'))/m_i
3945 * We start out with the following expression for dst:
3947 * (a(p) + 0 y + 0 divs' + f \sum_i b_i x_i + c(divs))/d
3949 * with the multiplication factor f initially equal to 1.
3950 * For each x_i that we substitute, we multiply the numerator
3951 * (and denominator) of dst by c_1 = m_i and add the numerator
3952 * of the x_i expression multiplied by c_2 = f b_i,
3953 * after removing the common factors of c_1 and c_2.
3954 * The multiplication factor f also needs to be multiplied by c_1
3955 * for the next x_j, j > i.
3957 void isl_seq_preimage(isl_int *dst, isl_int *src,
3958 __isl_keep isl_multi_aff *ma, int n_div_ma, int n_div_bset,
3959 isl_int f, isl_int c1, isl_int c2, isl_int g, int has_denom)
3962 int n_param, n_in, n_out;
3965 n_param = isl_multi_aff_dim(ma, isl_dim_param);
3966 n_in = isl_multi_aff_dim(ma, isl_dim_in);
3967 n_out = isl_multi_aff_dim(ma, isl_dim_out);
3969 o_div_bset = has_denom + 1 + n_param + n_in + n_div_ma;
3971 isl_seq_cpy(dst, src, has_denom + 1 + n_param);
3972 isl_seq_clr(dst + has_denom + 1 + n_param, n_in + n_div_ma);
3973 isl_seq_cpy(dst + o_div_bset,
3974 src + has_denom + 1 + n_param + n_out, n_div_bset);
3976 isl_int_set_si(f, 1);
3978 for (i = 0; i < n_out; ++i) {
3979 if (isl_int_is_zero(src[has_denom + 1 + n_param + i]))
3981 isl_int_set(c1, ma->p[i]->v->el[0]);
3982 isl_int_mul(c2, f, src[has_denom + 1 + n_param + i]);
3983 isl_int_gcd(g, c1, c2);
3984 isl_int_divexact(c1, c1, g);
3985 isl_int_divexact(c2, c2, g);
3987 isl_int_mul(f, f, c1);
3988 isl_seq_combine(dst + has_denom, c1, dst + has_denom,
3989 c2, ma->p[i]->v->el + 1, ma->p[i]->v->size - 1);
3990 isl_seq_scale(dst + o_div_bset,
3991 dst + o_div_bset, c1, n_div_bset);
3993 isl_int_mul(dst[0], dst[0], c1);
3997 /* Compute the pullback of "aff" by the function represented by "ma".
3998 * In other words, plug in "ma" in "aff". The result is an affine expression
3999 * defined over the domain space of "ma".
4001 * If "aff" is represented by
4003 * (a(p) + b x + c(divs))/d
4005 * and ma is represented by
4007 * x = D(p) + F(y) + G(divs')
4009 * then the result is
4011 * (a(p) + b D(p) + b F(y) + b G(divs') + c(divs))/d
4013 * The divs in the local space of the input are similarly adjusted
4014 * through a call to isl_local_space_preimage_multi_aff.
4016 __isl_give isl_aff *isl_aff_pullback_multi_aff(__isl_take isl_aff *aff,
4017 __isl_take isl_multi_aff *ma)
4019 isl_aff *res = NULL;
4020 isl_local_space *ls;
4021 int n_div_aff, n_div_ma;
4022 isl_int f, c1, c2, g;
4024 ma = isl_multi_aff_align_divs(ma);
4028 n_div_aff = isl_aff_dim(aff, isl_dim_div);
4029 n_div_ma = ma->n ? isl_aff_dim(ma->p[0], isl_dim_div) : 0;
4031 ls = isl_aff_get_domain_local_space(aff);
4032 ls = isl_local_space_preimage_multi_aff(ls, isl_multi_aff_copy(ma));
4033 res = isl_aff_alloc(ls);
4042 isl_seq_preimage(res->v->el, aff->v->el, ma, n_div_ma, n_div_aff,
4051 isl_multi_aff_free(ma);
4052 res = isl_aff_normalize(res);
4056 isl_multi_aff_free(ma);
4061 /* Compute the pullback of "ma1" by the function represented by "ma2".
4062 * In other words, plug in "ma2" in "ma1".
4064 __isl_give isl_multi_aff *isl_multi_aff_pullback_multi_aff(
4065 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2)
4068 isl_space *space = NULL;
4070 ma2 = isl_multi_aff_align_divs(ma2);
4071 ma1 = isl_multi_aff_cow(ma1);
4075 space = isl_space_join(isl_multi_aff_get_space(ma2),
4076 isl_multi_aff_get_space(ma1));
4078 for (i = 0; i < ma1->n; ++i) {
4079 ma1->p[i] = isl_aff_pullback_multi_aff(ma1->p[i],
4080 isl_multi_aff_copy(ma2));
4085 ma1 = isl_multi_aff_reset_space(ma1, space);
4086 isl_multi_aff_free(ma2);
4089 isl_space_free(space);
4090 isl_multi_aff_free(ma2);
4091 isl_multi_aff_free(ma1);
4095 /* Extend the local space of "dst" to include the divs
4096 * in the local space of "src".
4098 __isl_give isl_aff *isl_aff_align_divs(__isl_take isl_aff *dst,
4099 __isl_keep isl_aff *src)
4107 return isl_aff_free(dst);
4109 ctx = isl_aff_get_ctx(src);
4110 if (!isl_space_is_equal(src->ls->dim, dst->ls->dim))
4111 isl_die(ctx, isl_error_invalid,
4112 "spaces don't match", goto error);
4114 if (src->ls->div->n_row == 0)
4117 exp1 = isl_alloc_array(ctx, int, src->ls->div->n_row);
4118 exp2 = isl_alloc_array(ctx, int, dst->ls->div->n_row);
4122 div = isl_merge_divs(src->ls->div, dst->ls->div, exp1, exp2);
4123 dst = isl_aff_expand_divs(dst, div, exp2);
4131 return isl_aff_free(dst);
4134 /* Adjust the local spaces of the affine expressions in "maff"
4135 * such that they all have the save divs.
4137 __isl_give isl_multi_aff *isl_multi_aff_align_divs(
4138 __isl_take isl_multi_aff *maff)
4146 maff = isl_multi_aff_cow(maff);
4150 for (i = 1; i < maff->n; ++i)
4151 maff->p[0] = isl_aff_align_divs(maff->p[0], maff->p[i]);
4152 for (i = 1; i < maff->n; ++i) {
4153 maff->p[i] = isl_aff_align_divs(maff->p[i], maff->p[0]);
4155 return isl_multi_aff_free(maff);
4161 __isl_give isl_aff *isl_aff_lift(__isl_take isl_aff *aff)
4163 aff = isl_aff_cow(aff);
4167 aff->ls = isl_local_space_lift(aff->ls);
4169 return isl_aff_free(aff);
4174 /* Lift "maff" to a space with extra dimensions such that the result
4175 * has no more existentially quantified variables.
4176 * If "ls" is not NULL, then *ls is assigned the local space that lies
4177 * at the basis of the lifting applied to "maff".
4179 __isl_give isl_multi_aff *isl_multi_aff_lift(__isl_take isl_multi_aff *maff,
4180 __isl_give isl_local_space **ls)
4194 isl_space *space = isl_multi_aff_get_domain_space(maff);
4195 *ls = isl_local_space_from_space(space);
4197 return isl_multi_aff_free(maff);
4202 maff = isl_multi_aff_cow(maff);
4203 maff = isl_multi_aff_align_divs(maff);
4207 n_div = isl_aff_dim(maff->p[0], isl_dim_div);
4208 space = isl_multi_aff_get_space(maff);
4209 space = isl_space_lift(isl_space_domain(space), n_div);
4210 space = isl_space_extend_domain_with_range(space,
4211 isl_multi_aff_get_space(maff));
4213 return isl_multi_aff_free(maff);
4214 isl_space_free(maff->space);
4215 maff->space = space;
4218 *ls = isl_aff_get_domain_local_space(maff->p[0]);
4220 return isl_multi_aff_free(maff);
4223 for (i = 0; i < maff->n; ++i) {
4224 maff->p[i] = isl_aff_lift(maff->p[i]);
4232 isl_local_space_free(*ls);
4233 return isl_multi_aff_free(maff);
4237 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma".
4239 __isl_give isl_pw_aff *isl_pw_multi_aff_get_pw_aff(
4240 __isl_keep isl_pw_multi_aff *pma, int pos)
4250 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
4251 if (pos < 0 || pos >= n_out)
4252 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
4253 "index out of bounds", return NULL);
4255 space = isl_pw_multi_aff_get_space(pma);
4256 space = isl_space_drop_dims(space, isl_dim_out,
4257 pos + 1, n_out - pos - 1);
4258 space = isl_space_drop_dims(space, isl_dim_out, 0, pos);
4260 pa = isl_pw_aff_alloc_size(space, pma->n);
4261 for (i = 0; i < pma->n; ++i) {
4263 aff = isl_multi_aff_get_aff(pma->p[i].maff, pos);
4264 pa = isl_pw_aff_add_piece(pa, isl_set_copy(pma->p[i].set), aff);
4270 /* Return an isl_pw_multi_aff with the given "set" as domain and
4271 * an unnamed zero-dimensional range.
4273 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_domain(
4274 __isl_take isl_set *set)
4279 space = isl_set_get_space(set);
4280 space = isl_space_from_domain(space);
4281 ma = isl_multi_aff_zero(space);
4282 return isl_pw_multi_aff_alloc(set, ma);
4285 /* Add an isl_pw_multi_aff with the given "set" as domain and
4286 * an unnamed zero-dimensional range to *user.
4288 static int add_pw_multi_aff_from_domain(__isl_take isl_set *set, void *user)
4290 isl_union_pw_multi_aff **upma = user;
4291 isl_pw_multi_aff *pma;
4293 pma = isl_pw_multi_aff_from_domain(set);
4294 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
4299 /* Return an isl_union_pw_multi_aff with the given "uset" as domain and
4300 * an unnamed zero-dimensional range.
4302 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_domain(
4303 __isl_take isl_union_set *uset)
4306 isl_union_pw_multi_aff *upma;
4311 space = isl_union_set_get_space(uset);
4312 upma = isl_union_pw_multi_aff_empty(space);
4314 if (isl_union_set_foreach_set(uset,
4315 &add_pw_multi_aff_from_domain, &upma) < 0)
4318 isl_union_set_free(uset);
4321 isl_union_set_free(uset);
4322 isl_union_pw_multi_aff_free(upma);
4326 /* Convert "pma" to an isl_map and add it to *umap.
4328 static int map_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma, void *user)
4330 isl_union_map **umap = user;
4333 map = isl_map_from_pw_multi_aff(pma);
4334 *umap = isl_union_map_add_map(*umap, map);
4339 /* Construct a union map mapping the domain of the union
4340 * piecewise multi-affine expression to its range, with each dimension
4341 * in the range equated to the corresponding affine expression on its cell.
4343 __isl_give isl_union_map *isl_union_map_from_union_pw_multi_aff(
4344 __isl_take isl_union_pw_multi_aff *upma)
4347 isl_union_map *umap;
4352 space = isl_union_pw_multi_aff_get_space(upma);
4353 umap = isl_union_map_empty(space);
4355 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
4356 &map_from_pw_multi_aff, &umap) < 0)
4359 isl_union_pw_multi_aff_free(upma);
4362 isl_union_pw_multi_aff_free(upma);
4363 isl_union_map_free(umap);
4367 /* Local data for bin_entry and the callback "fn".
4369 struct isl_union_pw_multi_aff_bin_data {
4370 isl_union_pw_multi_aff *upma2;
4371 isl_union_pw_multi_aff *res;
4372 isl_pw_multi_aff *pma;
4373 int (*fn)(void **entry, void *user);
4376 /* Given an isl_pw_multi_aff from upma1, store it in data->pma
4377 * and call data->fn for each isl_pw_multi_aff in data->upma2.
4379 static int bin_entry(void **entry, void *user)
4381 struct isl_union_pw_multi_aff_bin_data *data = user;
4382 isl_pw_multi_aff *pma = *entry;
4385 if (isl_hash_table_foreach(data->upma2->dim->ctx, &data->upma2->table,
4386 data->fn, data) < 0)
4392 /* Call "fn" on each pair of isl_pw_multi_affs in "upma1" and "upma2".
4393 * The isl_pw_multi_aff from upma1 is stored in data->pma (where data is
4394 * passed as user field) and the isl_pw_multi_aff from upma2 is available
4395 * as *entry. The callback should adjust data->res if desired.
4397 static __isl_give isl_union_pw_multi_aff *bin_op(
4398 __isl_take isl_union_pw_multi_aff *upma1,
4399 __isl_take isl_union_pw_multi_aff *upma2,
4400 int (*fn)(void **entry, void *user))
4403 struct isl_union_pw_multi_aff_bin_data data = { NULL, NULL, NULL, fn };
4405 space = isl_union_pw_multi_aff_get_space(upma2);
4406 upma1 = isl_union_pw_multi_aff_align_params(upma1, space);
4407 space = isl_union_pw_multi_aff_get_space(upma1);
4408 upma2 = isl_union_pw_multi_aff_align_params(upma2, space);
4410 if (!upma1 || !upma2)
4414 data.res = isl_union_pw_multi_aff_alloc(isl_space_copy(upma1->dim),
4416 if (isl_hash_table_foreach(upma1->dim->ctx, &upma1->table,
4417 &bin_entry, &data) < 0)
4420 isl_union_pw_multi_aff_free(upma1);
4421 isl_union_pw_multi_aff_free(upma2);
4424 isl_union_pw_multi_aff_free(upma1);
4425 isl_union_pw_multi_aff_free(upma2);
4426 isl_union_pw_multi_aff_free(data.res);
4430 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
4431 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
4433 static __isl_give isl_pw_multi_aff *pw_multi_aff_range_product(
4434 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4438 space = isl_space_range_product(isl_pw_multi_aff_get_space(pma1),
4439 isl_pw_multi_aff_get_space(pma2));
4440 return isl_pw_multi_aff_on_shared_domain_in(pma1, pma2, space,
4441 &isl_multi_aff_range_product);
4444 /* Given two isl_pw_multi_affs A -> B and C -> D,
4445 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
4447 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_range_product(
4448 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4450 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4451 &pw_multi_aff_range_product);
4454 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
4455 * construct an isl_pw_multi_aff (A * C) -> (B, D).
4457 static __isl_give isl_pw_multi_aff *pw_multi_aff_flat_range_product(
4458 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4462 space = isl_space_range_product(isl_pw_multi_aff_get_space(pma1),
4463 isl_pw_multi_aff_get_space(pma2));
4464 space = isl_space_flatten_range(space);
4465 return isl_pw_multi_aff_on_shared_domain_in(pma1, pma2, space,
4466 &isl_multi_aff_flat_range_product);
4469 /* Given two isl_pw_multi_affs A -> B and C -> D,
4470 * construct an isl_pw_multi_aff (A * C) -> (B, D).
4472 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_flat_range_product(
4473 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4475 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4476 &pw_multi_aff_flat_range_product);
4479 /* If data->pma and *entry have the same domain space, then compute
4480 * their flat range product and the result to data->res.
4482 static int flat_range_product_entry(void **entry, void *user)
4484 struct isl_union_pw_multi_aff_bin_data *data = user;
4485 isl_pw_multi_aff *pma2 = *entry;
4487 if (!isl_space_tuple_match(data->pma->dim, isl_dim_in,
4488 pma2->dim, isl_dim_in))
4491 pma2 = isl_pw_multi_aff_flat_range_product(
4492 isl_pw_multi_aff_copy(data->pma),
4493 isl_pw_multi_aff_copy(pma2));
4495 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma2);
4500 /* Given two isl_union_pw_multi_affs A -> B and C -> D,
4501 * construct an isl_union_pw_multi_aff (A * C) -> (B, D).
4503 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_flat_range_product(
4504 __isl_take isl_union_pw_multi_aff *upma1,
4505 __isl_take isl_union_pw_multi_aff *upma2)
4507 return bin_op(upma1, upma2, &flat_range_product_entry);
4510 /* Replace the affine expressions at position "pos" in "pma" by "pa".
4511 * The parameters are assumed to have been aligned.
4513 * The implementation essentially performs an isl_pw_*_on_shared_domain,
4514 * except that it works on two different isl_pw_* types.
4516 static __isl_give isl_pw_multi_aff *pw_multi_aff_set_pw_aff(
4517 __isl_take isl_pw_multi_aff *pma, unsigned pos,
4518 __isl_take isl_pw_aff *pa)
4521 isl_pw_multi_aff *res = NULL;
4526 if (!isl_space_tuple_match(pma->dim, isl_dim_in, pa->dim, isl_dim_in))
4527 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
4528 "domains don't match", goto error);
4529 if (pos >= isl_pw_multi_aff_dim(pma, isl_dim_out))
4530 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
4531 "index out of bounds", goto error);
4534 res = isl_pw_multi_aff_alloc_size(isl_pw_multi_aff_get_space(pma), n);
4536 for (i = 0; i < pma->n; ++i) {
4537 for (j = 0; j < pa->n; ++j) {
4539 isl_multi_aff *res_ij;
4542 common = isl_set_intersect(isl_set_copy(pma->p[i].set),
4543 isl_set_copy(pa->p[j].set));
4544 empty = isl_set_plain_is_empty(common);
4545 if (empty < 0 || empty) {
4546 isl_set_free(common);
4552 res_ij = isl_multi_aff_set_aff(
4553 isl_multi_aff_copy(pma->p[i].maff), pos,
4554 isl_aff_copy(pa->p[j].aff));
4555 res_ij = isl_multi_aff_gist(res_ij,
4556 isl_set_copy(common));
4558 res = isl_pw_multi_aff_add_piece(res, common, res_ij);
4562 isl_pw_multi_aff_free(pma);
4563 isl_pw_aff_free(pa);
4566 isl_pw_multi_aff_free(pma);
4567 isl_pw_aff_free(pa);
4568 return isl_pw_multi_aff_free(res);
4571 /* Replace the affine expressions at position "pos" in "pma" by "pa".
4573 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_set_pw_aff(
4574 __isl_take isl_pw_multi_aff *pma, unsigned pos,
4575 __isl_take isl_pw_aff *pa)
4579 if (isl_space_match(pma->dim, isl_dim_param, pa->dim, isl_dim_param))
4580 return pw_multi_aff_set_pw_aff(pma, pos, pa);
4581 if (!isl_space_has_named_params(pma->dim) ||
4582 !isl_space_has_named_params(pa->dim))
4583 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
4584 "unaligned unnamed parameters", goto error);
4585 pma = isl_pw_multi_aff_align_params(pma, isl_pw_aff_get_space(pa));
4586 pa = isl_pw_aff_align_params(pa, isl_pw_multi_aff_get_space(pma));
4587 return pw_multi_aff_set_pw_aff(pma, pos, pa);
4589 isl_pw_multi_aff_free(pma);
4590 isl_pw_aff_free(pa);
4597 #include <isl_multi_templ.c>