2 * Copyright 2011 INRIA Saclay
3 * Copyright 2011 Sven Verdoolaege
4 * Copyright 2012-2013 Ecole Normale Superieure
6 * Use of this software is governed by the MIT license
8 * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
9 * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
11 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
14 #include <isl_ctx_private.h>
16 #include <isl_map_private.h>
17 #include <isl_union_map_private.h>
18 #include <isl_aff_private.h>
19 #include <isl_space_private.h>
20 #include <isl_local_space_private.h>
21 #include <isl_mat_private.h>
22 #include <isl/constraint.h>
25 #include <isl_val_private.h>
26 #include <isl_config.h>
31 #include <isl_list_templ.c>
36 #include <isl_list_templ.c>
38 __isl_give isl_aff *isl_aff_alloc_vec(__isl_take isl_local_space *ls,
39 __isl_take isl_vec *v)
46 aff = isl_calloc_type(v->ctx, struct isl_aff);
56 isl_local_space_free(ls);
61 __isl_give isl_aff *isl_aff_alloc(__isl_take isl_local_space *ls)
70 ctx = isl_local_space_get_ctx(ls);
71 if (!isl_local_space_divs_known(ls))
72 isl_die(ctx, isl_error_invalid, "local space has unknown divs",
74 if (!isl_local_space_is_set(ls))
75 isl_die(ctx, isl_error_invalid,
76 "domain of affine expression should be a set",
79 total = isl_local_space_dim(ls, isl_dim_all);
80 v = isl_vec_alloc(ctx, 1 + 1 + total);
81 return isl_aff_alloc_vec(ls, v);
83 isl_local_space_free(ls);
87 __isl_give isl_aff *isl_aff_zero_on_domain(__isl_take isl_local_space *ls)
91 aff = isl_aff_alloc(ls);
95 isl_int_set_si(aff->v->el[0], 1);
96 isl_seq_clr(aff->v->el + 1, aff->v->size - 1);
101 /* Return a piecewise affine expression defined on the specified domain
102 * that is equal to zero.
104 __isl_give isl_pw_aff *isl_pw_aff_zero_on_domain(__isl_take isl_local_space *ls)
106 return isl_pw_aff_from_aff(isl_aff_zero_on_domain(ls));
109 /* Return an affine expression that is equal to the specified dimension
112 __isl_give isl_aff *isl_aff_var_on_domain(__isl_take isl_local_space *ls,
113 enum isl_dim_type type, unsigned pos)
121 space = isl_local_space_get_space(ls);
124 if (isl_space_is_map(space))
125 isl_die(isl_space_get_ctx(space), isl_error_invalid,
126 "expecting (parameter) set space", goto error);
127 if (pos >= isl_local_space_dim(ls, type))
128 isl_die(isl_space_get_ctx(space), isl_error_invalid,
129 "position out of bounds", goto error);
131 isl_space_free(space);
132 aff = isl_aff_alloc(ls);
136 pos += isl_local_space_offset(aff->ls, type);
138 isl_int_set_si(aff->v->el[0], 1);
139 isl_seq_clr(aff->v->el + 1, aff->v->size - 1);
140 isl_int_set_si(aff->v->el[1 + pos], 1);
144 isl_local_space_free(ls);
145 isl_space_free(space);
149 /* Return a piecewise affine expression that is equal to
150 * the specified dimension in "ls".
152 __isl_give isl_pw_aff *isl_pw_aff_var_on_domain(__isl_take isl_local_space *ls,
153 enum isl_dim_type type, unsigned pos)
155 return isl_pw_aff_from_aff(isl_aff_var_on_domain(ls, type, pos));
158 __isl_give isl_aff *isl_aff_copy(__isl_keep isl_aff *aff)
167 __isl_give isl_aff *isl_aff_dup(__isl_keep isl_aff *aff)
172 return isl_aff_alloc_vec(isl_local_space_copy(aff->ls),
173 isl_vec_copy(aff->v));
176 __isl_give isl_aff *isl_aff_cow(__isl_take isl_aff *aff)
184 return isl_aff_dup(aff);
187 void *isl_aff_free(__isl_take isl_aff *aff)
195 isl_local_space_free(aff->ls);
196 isl_vec_free(aff->v);
203 isl_ctx *isl_aff_get_ctx(__isl_keep isl_aff *aff)
205 return aff ? isl_local_space_get_ctx(aff->ls) : NULL;
208 /* Externally, an isl_aff has a map space, but internally, the
209 * ls field corresponds to the domain of that space.
211 int isl_aff_dim(__isl_keep isl_aff *aff, enum isl_dim_type type)
215 if (type == isl_dim_out)
217 if (type == isl_dim_in)
219 return isl_local_space_dim(aff->ls, type);
222 __isl_give isl_space *isl_aff_get_domain_space(__isl_keep isl_aff *aff)
224 return aff ? isl_local_space_get_space(aff->ls) : NULL;
227 __isl_give isl_space *isl_aff_get_space(__isl_keep isl_aff *aff)
232 space = isl_local_space_get_space(aff->ls);
233 space = isl_space_from_domain(space);
234 space = isl_space_add_dims(space, isl_dim_out, 1);
238 __isl_give isl_local_space *isl_aff_get_domain_local_space(
239 __isl_keep isl_aff *aff)
241 return aff ? isl_local_space_copy(aff->ls) : NULL;
244 __isl_give isl_local_space *isl_aff_get_local_space(__isl_keep isl_aff *aff)
249 ls = isl_local_space_copy(aff->ls);
250 ls = isl_local_space_from_domain(ls);
251 ls = isl_local_space_add_dims(ls, isl_dim_out, 1);
255 /* Externally, an isl_aff has a map space, but internally, the
256 * ls field corresponds to the domain of that space.
258 const char *isl_aff_get_dim_name(__isl_keep isl_aff *aff,
259 enum isl_dim_type type, unsigned pos)
263 if (type == isl_dim_out)
265 if (type == isl_dim_in)
267 return isl_local_space_get_dim_name(aff->ls, type, pos);
270 __isl_give isl_aff *isl_aff_reset_domain_space(__isl_take isl_aff *aff,
271 __isl_take isl_space *dim)
273 aff = isl_aff_cow(aff);
277 aff->ls = isl_local_space_reset_space(aff->ls, dim);
279 return isl_aff_free(aff);
288 /* Reset the space of "aff". This function is called from isl_pw_templ.c
289 * and doesn't know if the space of an element object is represented
290 * directly or through its domain. It therefore passes along both.
292 __isl_give isl_aff *isl_aff_reset_space_and_domain(__isl_take isl_aff *aff,
293 __isl_take isl_space *space, __isl_take isl_space *domain)
295 isl_space_free(space);
296 return isl_aff_reset_domain_space(aff, domain);
299 /* Reorder the coefficients of the affine expression based
300 * on the given reodering.
301 * The reordering r is assumed to have been extended with the local
304 static __isl_give isl_vec *vec_reorder(__isl_take isl_vec *vec,
305 __isl_take isl_reordering *r, int n_div)
313 res = isl_vec_alloc(vec->ctx,
314 2 + isl_space_dim(r->dim, isl_dim_all) + n_div);
315 isl_seq_cpy(res->el, vec->el, 2);
316 isl_seq_clr(res->el + 2, res->size - 2);
317 for (i = 0; i < r->len; ++i)
318 isl_int_set(res->el[2 + r->pos[i]], vec->el[2 + i]);
320 isl_reordering_free(r);
325 isl_reordering_free(r);
329 /* Reorder the dimensions of the domain of "aff" according
330 * to the given reordering.
332 __isl_give isl_aff *isl_aff_realign_domain(__isl_take isl_aff *aff,
333 __isl_take isl_reordering *r)
335 aff = isl_aff_cow(aff);
339 r = isl_reordering_extend(r, aff->ls->div->n_row);
340 aff->v = vec_reorder(aff->v, isl_reordering_copy(r),
341 aff->ls->div->n_row);
342 aff->ls = isl_local_space_realign(aff->ls, r);
344 if (!aff->v || !aff->ls)
345 return isl_aff_free(aff);
350 isl_reordering_free(r);
354 __isl_give isl_aff *isl_aff_align_params(__isl_take isl_aff *aff,
355 __isl_take isl_space *model)
360 if (!isl_space_match(aff->ls->dim, isl_dim_param,
361 model, isl_dim_param)) {
364 model = isl_space_drop_dims(model, isl_dim_in,
365 0, isl_space_dim(model, isl_dim_in));
366 model = isl_space_drop_dims(model, isl_dim_out,
367 0, isl_space_dim(model, isl_dim_out));
368 exp = isl_parameter_alignment_reordering(aff->ls->dim, model);
369 exp = isl_reordering_extend_space(exp,
370 isl_aff_get_domain_space(aff));
371 aff = isl_aff_realign_domain(aff, exp);
374 isl_space_free(model);
377 isl_space_free(model);
382 int isl_aff_plain_is_zero(__isl_keep isl_aff *aff)
387 return isl_seq_first_non_zero(aff->v->el + 1, aff->v->size - 1) < 0;
390 int isl_aff_plain_is_equal(__isl_keep isl_aff *aff1, __isl_keep isl_aff *aff2)
397 equal = isl_local_space_is_equal(aff1->ls, aff2->ls);
398 if (equal < 0 || !equal)
401 return isl_vec_is_equal(aff1->v, aff2->v);
404 int isl_aff_get_denominator(__isl_keep isl_aff *aff, isl_int *v)
408 isl_int_set(*v, aff->v->el[0]);
412 int isl_aff_get_constant(__isl_keep isl_aff *aff, isl_int *v)
416 isl_int_set(*v, aff->v->el[1]);
420 int isl_aff_get_coefficient(__isl_keep isl_aff *aff,
421 enum isl_dim_type type, int pos, isl_int *v)
426 if (type == isl_dim_out)
427 isl_die(aff->v->ctx, isl_error_invalid,
428 "output/set dimension does not have a coefficient",
430 if (type == isl_dim_in)
433 if (pos >= isl_local_space_dim(aff->ls, type))
434 isl_die(aff->v->ctx, isl_error_invalid,
435 "position out of bounds", return -1);
437 pos += isl_local_space_offset(aff->ls, type);
438 isl_int_set(*v, aff->v->el[1 + pos]);
443 __isl_give isl_aff *isl_aff_set_denominator(__isl_take isl_aff *aff, isl_int v)
445 aff = isl_aff_cow(aff);
449 aff->v = isl_vec_cow(aff->v);
451 return isl_aff_free(aff);
453 isl_int_set(aff->v->el[0], v);
458 __isl_give isl_aff *isl_aff_set_constant(__isl_take isl_aff *aff, isl_int v)
460 aff = isl_aff_cow(aff);
464 aff->v = isl_vec_cow(aff->v);
466 return isl_aff_free(aff);
468 isl_int_set(aff->v->el[1], v);
473 /* Replace the constant term of "aff" by "v".
475 __isl_give isl_aff *isl_aff_set_constant_val(__isl_take isl_aff *aff,
476 __isl_take isl_val *v)
481 if (!isl_val_is_rat(v))
482 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
483 "expecting rational value", goto error);
485 if (isl_int_eq(aff->v->el[1], v->n) &&
486 isl_int_eq(aff->v->el[0], v->d)) {
491 aff = isl_aff_cow(aff);
494 aff->v = isl_vec_cow(aff->v);
498 if (isl_int_eq(aff->v->el[0], v->d)) {
499 isl_int_set(aff->v->el[1], v->n);
500 } else if (isl_int_is_one(v->d)) {
501 isl_int_mul(aff->v->el[1], aff->v->el[0], v->n);
503 isl_seq_scale(aff->v->el + 1,
504 aff->v->el + 1, v->d, aff->v->size - 1);
505 isl_int_mul(aff->v->el[1], aff->v->el[0], v->n);
506 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
507 aff->v = isl_vec_normalize(aff->v);
520 __isl_give isl_aff *isl_aff_add_constant(__isl_take isl_aff *aff, isl_int v)
522 if (isl_int_is_zero(v))
525 aff = isl_aff_cow(aff);
529 aff->v = isl_vec_cow(aff->v);
531 return isl_aff_free(aff);
533 isl_int_addmul(aff->v->el[1], aff->v->el[0], v);
538 __isl_give isl_aff *isl_aff_add_constant_si(__isl_take isl_aff *aff, int v)
543 isl_int_set_si(t, v);
544 aff = isl_aff_add_constant(aff, t);
550 /* Add "v" to the numerator of the constant term of "aff".
552 __isl_give isl_aff *isl_aff_add_constant_num(__isl_take isl_aff *aff, isl_int v)
554 if (isl_int_is_zero(v))
557 aff = isl_aff_cow(aff);
561 aff->v = isl_vec_cow(aff->v);
563 return isl_aff_free(aff);
565 isl_int_add(aff->v->el[1], aff->v->el[1], v);
570 /* Add "v" to the numerator of the constant term of "aff".
572 __isl_give isl_aff *isl_aff_add_constant_num_si(__isl_take isl_aff *aff, int v)
580 isl_int_set_si(t, v);
581 aff = isl_aff_add_constant_num(aff, t);
587 __isl_give isl_aff *isl_aff_set_constant_si(__isl_take isl_aff *aff, int v)
589 aff = isl_aff_cow(aff);
593 aff->v = isl_vec_cow(aff->v);
595 return isl_aff_free(aff);
597 isl_int_set_si(aff->v->el[1], v);
602 __isl_give isl_aff *isl_aff_set_coefficient(__isl_take isl_aff *aff,
603 enum isl_dim_type type, int pos, isl_int v)
608 if (type == isl_dim_out)
609 isl_die(aff->v->ctx, isl_error_invalid,
610 "output/set dimension does not have a coefficient",
611 return isl_aff_free(aff));
612 if (type == isl_dim_in)
615 if (pos >= isl_local_space_dim(aff->ls, type))
616 isl_die(aff->v->ctx, isl_error_invalid,
617 "position out of bounds", return isl_aff_free(aff));
619 aff = isl_aff_cow(aff);
623 aff->v = isl_vec_cow(aff->v);
625 return isl_aff_free(aff);
627 pos += isl_local_space_offset(aff->ls, type);
628 isl_int_set(aff->v->el[1 + pos], v);
633 __isl_give isl_aff *isl_aff_set_coefficient_si(__isl_take isl_aff *aff,
634 enum isl_dim_type type, int pos, int v)
639 if (type == isl_dim_out)
640 isl_die(aff->v->ctx, isl_error_invalid,
641 "output/set dimension does not have a coefficient",
642 return isl_aff_free(aff));
643 if (type == isl_dim_in)
646 if (pos >= isl_local_space_dim(aff->ls, type))
647 isl_die(aff->v->ctx, isl_error_invalid,
648 "position out of bounds", return isl_aff_free(aff));
650 aff = isl_aff_cow(aff);
654 aff->v = isl_vec_cow(aff->v);
656 return isl_aff_free(aff);
658 pos += isl_local_space_offset(aff->ls, type);
659 isl_int_set_si(aff->v->el[1 + pos], v);
664 __isl_give isl_aff *isl_aff_add_coefficient(__isl_take isl_aff *aff,
665 enum isl_dim_type type, int pos, isl_int v)
670 if (type == isl_dim_out)
671 isl_die(aff->v->ctx, isl_error_invalid,
672 "output/set dimension does not have a coefficient",
673 return isl_aff_free(aff));
674 if (type == isl_dim_in)
677 if (pos >= isl_local_space_dim(aff->ls, type))
678 isl_die(aff->v->ctx, isl_error_invalid,
679 "position out of bounds", return isl_aff_free(aff));
681 aff = isl_aff_cow(aff);
685 aff->v = isl_vec_cow(aff->v);
687 return isl_aff_free(aff);
689 pos += isl_local_space_offset(aff->ls, type);
690 isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v);
695 __isl_give isl_aff *isl_aff_add_coefficient_si(__isl_take isl_aff *aff,
696 enum isl_dim_type type, int pos, int v)
701 isl_int_set_si(t, v);
702 aff = isl_aff_add_coefficient(aff, type, pos, t);
708 __isl_give isl_aff *isl_aff_get_div(__isl_keep isl_aff *aff, int pos)
713 return isl_local_space_get_div(aff->ls, pos);
716 __isl_give isl_aff *isl_aff_neg(__isl_take isl_aff *aff)
718 aff = isl_aff_cow(aff);
721 aff->v = isl_vec_cow(aff->v);
723 return isl_aff_free(aff);
725 isl_seq_neg(aff->v->el + 1, aff->v->el + 1, aff->v->size - 1);
730 /* Remove divs from the local space that do not appear in the affine
732 * We currently only remove divs at the end.
733 * Some intermediate divs may also not appear directly in the affine
734 * expression, but we would also need to check that no other divs are
735 * defined in terms of them.
737 __isl_give isl_aff *isl_aff_remove_unused_divs( __isl_take isl_aff *aff)
746 n = isl_local_space_dim(aff->ls, isl_dim_div);
747 off = isl_local_space_offset(aff->ls, isl_dim_div);
749 pos = isl_seq_last_non_zero(aff->v->el + 1 + off, n) + 1;
753 aff = isl_aff_cow(aff);
757 aff->ls = isl_local_space_drop_dims(aff->ls, isl_dim_div, pos, n - pos);
758 aff->v = isl_vec_drop_els(aff->v, 1 + off + pos, n - pos);
759 if (!aff->ls || !aff->v)
760 return isl_aff_free(aff);
765 /* Given two affine expressions "p" of length p_len (including the
766 * denominator and the constant term) and "subs" of length subs_len,
767 * plug in "subs" for the variable at position "pos".
768 * The variables of "subs" and "p" are assumed to match up to subs_len,
769 * but "p" may have additional variables.
770 * "v" is an initialized isl_int that can be used internally.
772 * In particular, if "p" represents the expression
776 * with i the variable at position "pos" and "subs" represents the expression
780 * then the result represents the expression
785 void isl_seq_substitute(isl_int *p, int pos, isl_int *subs,
786 int p_len, int subs_len, isl_int v)
788 isl_int_set(v, p[1 + pos]);
789 isl_int_set_si(p[1 + pos], 0);
790 isl_seq_combine(p + 1, subs[0], p + 1, v, subs + 1, subs_len - 1);
791 isl_seq_scale(p + subs_len, p + subs_len, subs[0], p_len - subs_len);
792 isl_int_mul(p[0], p[0], subs[0]);
795 /* Look for any divs in the aff->ls with a denominator equal to one
796 * and plug them into the affine expression and any subsequent divs
797 * that may reference the div.
799 static __isl_give isl_aff *plug_in_integral_divs(__isl_take isl_aff *aff)
811 n = isl_local_space_dim(aff->ls, isl_dim_div);
813 for (i = 0; i < n; ++i) {
814 if (!isl_int_is_one(aff->ls->div->row[i][0]))
816 ls = isl_local_space_copy(aff->ls);
817 ls = isl_local_space_substitute_seq(ls, isl_dim_div, i,
818 aff->ls->div->row[i], len, i + 1, n - (i + 1));
819 vec = isl_vec_copy(aff->v);
820 vec = isl_vec_cow(vec);
826 pos = isl_local_space_offset(aff->ls, isl_dim_div) + i;
827 isl_seq_substitute(vec->el, pos, aff->ls->div->row[i],
832 isl_vec_free(aff->v);
834 isl_local_space_free(aff->ls);
841 isl_local_space_free(ls);
842 return isl_aff_free(aff);
845 /* Look for any divs j that appear with a unit coefficient inside
846 * the definitions of other divs i and plug them into the definitions
849 * In particular, an expression of the form
851 * floor((f(..) + floor(g(..)/n))/m)
855 * floor((n * f(..) + g(..))/(n * m))
857 * This simplification is correct because we can move the expression
858 * f(..) into the inner floor in the original expression to obtain
860 * floor(floor((n * f(..) + g(..))/n)/m)
862 * from which we can derive the simplified expression.
864 static __isl_give isl_aff *plug_in_unit_divs(__isl_take isl_aff *aff)
872 n = isl_local_space_dim(aff->ls, isl_dim_div);
873 off = isl_local_space_offset(aff->ls, isl_dim_div);
874 for (i = 1; i < n; ++i) {
875 for (j = 0; j < i; ++j) {
876 if (!isl_int_is_one(aff->ls->div->row[i][1 + off + j]))
878 aff->ls = isl_local_space_substitute_seq(aff->ls,
879 isl_dim_div, j, aff->ls->div->row[j],
882 return isl_aff_free(aff);
889 /* Swap divs "a" and "b" in "aff", which is assumed to be non-NULL.
891 * Even though this function is only called on isl_affs with a single
892 * reference, we are careful to only change aff->v and aff->ls together.
894 static __isl_give isl_aff *swap_div(__isl_take isl_aff *aff, int a, int b)
896 unsigned off = isl_local_space_offset(aff->ls, isl_dim_div);
900 ls = isl_local_space_copy(aff->ls);
901 ls = isl_local_space_swap_div(ls, a, b);
902 v = isl_vec_copy(aff->v);
907 isl_int_swap(v->el[1 + off + a], v->el[1 + off + b]);
908 isl_vec_free(aff->v);
910 isl_local_space_free(aff->ls);
916 isl_local_space_free(ls);
917 return isl_aff_free(aff);
920 /* Merge divs "a" and "b" in "aff", which is assumed to be non-NULL.
922 * We currently do not actually remove div "b", but simply add its
923 * coefficient to that of "a" and then zero it out.
925 static __isl_give isl_aff *merge_divs(__isl_take isl_aff *aff, int a, int b)
927 unsigned off = isl_local_space_offset(aff->ls, isl_dim_div);
929 if (isl_int_is_zero(aff->v->el[1 + off + b]))
932 aff->v = isl_vec_cow(aff->v);
934 return isl_aff_free(aff);
936 isl_int_add(aff->v->el[1 + off + a],
937 aff->v->el[1 + off + a], aff->v->el[1 + off + b]);
938 isl_int_set_si(aff->v->el[1 + off + b], 0);
943 /* Sort the divs in the local space of "aff" according to
944 * the comparison function "cmp_row" in isl_local_space.c,
945 * combining the coefficients of identical divs.
947 * Reordering divs does not change the semantics of "aff",
948 * so there is no need to call isl_aff_cow.
949 * Moreover, this function is currently only called on isl_affs
950 * with a single reference.
952 static __isl_give isl_aff *sort_divs(__isl_take isl_aff *aff)
960 off = isl_local_space_offset(aff->ls, isl_dim_div);
961 n = isl_aff_dim(aff, isl_dim_div);
962 for (i = 1; i < n; ++i) {
963 for (j = i - 1; j >= 0; --j) {
964 int cmp = isl_mat_cmp_div(aff->ls->div, j, j + 1);
968 aff = merge_divs(aff, j, j + 1);
970 aff = swap_div(aff, j, j + 1);
979 /* Normalize the representation of "aff".
981 * This function should only be called of "new" isl_affs, i.e.,
982 * with only a single reference. We therefore do not need to
983 * worry about affecting other instances.
985 __isl_give isl_aff *isl_aff_normalize(__isl_take isl_aff *aff)
989 aff->v = isl_vec_normalize(aff->v);
991 return isl_aff_free(aff);
992 aff = plug_in_integral_divs(aff);
993 aff = plug_in_unit_divs(aff);
994 aff = sort_divs(aff);
995 aff = isl_aff_remove_unused_divs(aff);
999 /* Given f, return floor(f).
1000 * If f is an integer expression, then just return f.
1001 * If f is a constant, then return the constant floor(f).
1002 * Otherwise, if f = g/m, write g = q m + r,
1003 * create a new div d = [r/m] and return the expression q + d.
1004 * The coefficients in r are taken to lie between -m/2 and m/2.
1006 __isl_give isl_aff *isl_aff_floor(__isl_take isl_aff *aff)
1016 if (isl_int_is_one(aff->v->el[0]))
1019 aff = isl_aff_cow(aff);
1023 aff->v = isl_vec_cow(aff->v);
1025 return isl_aff_free(aff);
1027 if (isl_aff_is_cst(aff)) {
1028 isl_int_fdiv_q(aff->v->el[1], aff->v->el[1], aff->v->el[0]);
1029 isl_int_set_si(aff->v->el[0], 1);
1033 div = isl_vec_copy(aff->v);
1034 div = isl_vec_cow(div);
1036 return isl_aff_free(aff);
1038 ctx = isl_aff_get_ctx(aff);
1039 isl_int_fdiv_q(aff->v->el[0], aff->v->el[0], ctx->two);
1040 for (i = 1; i < aff->v->size; ++i) {
1041 isl_int_fdiv_r(div->el[i], div->el[i], div->el[0]);
1042 isl_int_fdiv_q(aff->v->el[i], aff->v->el[i], div->el[0]);
1043 if (isl_int_gt(div->el[i], aff->v->el[0])) {
1044 isl_int_sub(div->el[i], div->el[i], div->el[0]);
1045 isl_int_add_ui(aff->v->el[i], aff->v->el[i], 1);
1049 aff->ls = isl_local_space_add_div(aff->ls, div);
1051 return isl_aff_free(aff);
1053 size = aff->v->size;
1054 aff->v = isl_vec_extend(aff->v, size + 1);
1056 return isl_aff_free(aff);
1057 isl_int_set_si(aff->v->el[0], 1);
1058 isl_int_set_si(aff->v->el[size], 1);
1060 aff = isl_aff_normalize(aff);
1067 * aff mod m = aff - m * floor(aff/m)
1069 __isl_give isl_aff *isl_aff_mod(__isl_take isl_aff *aff, isl_int m)
1073 res = isl_aff_copy(aff);
1074 aff = isl_aff_scale_down(aff, m);
1075 aff = isl_aff_floor(aff);
1076 aff = isl_aff_scale(aff, m);
1077 res = isl_aff_sub(res, aff);
1084 * pwaff mod m = pwaff - m * floor(pwaff/m)
1086 __isl_give isl_pw_aff *isl_pw_aff_mod(__isl_take isl_pw_aff *pwaff, isl_int m)
1090 res = isl_pw_aff_copy(pwaff);
1091 pwaff = isl_pw_aff_scale_down(pwaff, m);
1092 pwaff = isl_pw_aff_floor(pwaff);
1093 pwaff = isl_pw_aff_scale(pwaff, m);
1094 res = isl_pw_aff_sub(res, pwaff);
1099 /* Given f, return ceil(f).
1100 * If f is an integer expression, then just return f.
1101 * Otherwise, let f be the expression
1107 * floor((e + m - 1)/m)
1109 __isl_give isl_aff *isl_aff_ceil(__isl_take isl_aff *aff)
1114 if (isl_int_is_one(aff->v->el[0]))
1117 aff = isl_aff_cow(aff);
1120 aff->v = isl_vec_cow(aff->v);
1122 return isl_aff_free(aff);
1124 isl_int_add(aff->v->el[1], aff->v->el[1], aff->v->el[0]);
1125 isl_int_sub_ui(aff->v->el[1], aff->v->el[1], 1);
1126 aff = isl_aff_floor(aff);
1131 /* Apply the expansion computed by isl_merge_divs.
1132 * The expansion itself is given by "exp" while the resulting
1133 * list of divs is given by "div".
1135 __isl_give isl_aff *isl_aff_expand_divs( __isl_take isl_aff *aff,
1136 __isl_take isl_mat *div, int *exp)
1143 aff = isl_aff_cow(aff);
1147 old_n_div = isl_local_space_dim(aff->ls, isl_dim_div);
1148 new_n_div = isl_mat_rows(div);
1149 if (new_n_div < old_n_div)
1150 isl_die(isl_mat_get_ctx(div), isl_error_invalid,
1151 "not an expansion", goto error);
1153 aff->v = isl_vec_extend(aff->v, aff->v->size + new_n_div - old_n_div);
1157 offset = 1 + isl_local_space_offset(aff->ls, isl_dim_div);
1159 for (i = new_n_div - 1; i >= 0; --i) {
1160 if (j >= 0 && exp[j] == i) {
1162 isl_int_swap(aff->v->el[offset + i],
1163 aff->v->el[offset + j]);
1166 isl_int_set_si(aff->v->el[offset + i], 0);
1169 aff->ls = isl_local_space_replace_divs(aff->ls, isl_mat_copy(div));
1180 /* Add two affine expressions that live in the same local space.
1182 static __isl_give isl_aff *add_expanded(__isl_take isl_aff *aff1,
1183 __isl_take isl_aff *aff2)
1187 aff1 = isl_aff_cow(aff1);
1191 aff1->v = isl_vec_cow(aff1->v);
1197 isl_int_gcd(gcd, aff1->v->el[0], aff2->v->el[0]);
1198 isl_int_divexact(f, aff2->v->el[0], gcd);
1199 isl_seq_scale(aff1->v->el + 1, aff1->v->el + 1, f, aff1->v->size - 1);
1200 isl_int_divexact(f, aff1->v->el[0], gcd);
1201 isl_seq_addmul(aff1->v->el + 1, f, aff2->v->el + 1, aff1->v->size - 1);
1202 isl_int_divexact(f, aff2->v->el[0], gcd);
1203 isl_int_mul(aff1->v->el[0], aff1->v->el[0], f);
1215 __isl_give isl_aff *isl_aff_add(__isl_take isl_aff *aff1,
1216 __isl_take isl_aff *aff2)
1226 ctx = isl_aff_get_ctx(aff1);
1227 if (!isl_space_is_equal(aff1->ls->dim, aff2->ls->dim))
1228 isl_die(ctx, isl_error_invalid,
1229 "spaces don't match", goto error);
1231 if (aff1->ls->div->n_row == 0 && aff2->ls->div->n_row == 0)
1232 return add_expanded(aff1, aff2);
1234 exp1 = isl_alloc_array(ctx, int, aff1->ls->div->n_row);
1235 exp2 = isl_alloc_array(ctx, int, aff2->ls->div->n_row);
1239 div = isl_merge_divs(aff1->ls->div, aff2->ls->div, exp1, exp2);
1240 aff1 = isl_aff_expand_divs(aff1, isl_mat_copy(div), exp1);
1241 aff2 = isl_aff_expand_divs(aff2, div, exp2);
1245 return add_expanded(aff1, aff2);
1254 __isl_give isl_aff *isl_aff_sub(__isl_take isl_aff *aff1,
1255 __isl_take isl_aff *aff2)
1257 return isl_aff_add(aff1, isl_aff_neg(aff2));
1260 __isl_give isl_aff *isl_aff_scale(__isl_take isl_aff *aff, isl_int f)
1264 if (isl_int_is_one(f))
1267 aff = isl_aff_cow(aff);
1270 aff->v = isl_vec_cow(aff->v);
1272 return isl_aff_free(aff);
1274 if (isl_int_is_pos(f) && isl_int_is_divisible_by(aff->v->el[0], f)) {
1275 isl_int_divexact(aff->v->el[0], aff->v->el[0], f);
1280 isl_int_gcd(gcd, aff->v->el[0], f);
1281 isl_int_divexact(aff->v->el[0], aff->v->el[0], gcd);
1282 isl_int_divexact(gcd, f, gcd);
1283 isl_seq_scale(aff->v->el + 1, aff->v->el + 1, gcd, aff->v->size - 1);
1289 __isl_give isl_aff *isl_aff_scale_down(__isl_take isl_aff *aff, isl_int f)
1293 if (isl_int_is_one(f))
1296 aff = isl_aff_cow(aff);
1300 if (isl_int_is_zero(f))
1301 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1302 "cannot scale down by zero", return isl_aff_free(aff));
1304 aff->v = isl_vec_cow(aff->v);
1306 return isl_aff_free(aff);
1309 isl_seq_gcd(aff->v->el + 1, aff->v->size - 1, &gcd);
1310 isl_int_gcd(gcd, gcd, f);
1311 isl_seq_scale_down(aff->v->el + 1, aff->v->el + 1, gcd, aff->v->size - 1);
1312 isl_int_divexact(gcd, f, gcd);
1313 isl_int_mul(aff->v->el[0], aff->v->el[0], gcd);
1319 __isl_give isl_aff *isl_aff_scale_down_ui(__isl_take isl_aff *aff, unsigned f)
1327 isl_int_set_ui(v, f);
1328 aff = isl_aff_scale_down(aff, v);
1334 __isl_give isl_aff *isl_aff_set_dim_name(__isl_take isl_aff *aff,
1335 enum isl_dim_type type, unsigned pos, const char *s)
1337 aff = isl_aff_cow(aff);
1340 if (type == isl_dim_out)
1341 isl_die(aff->v->ctx, isl_error_invalid,
1342 "cannot set name of output/set dimension",
1343 return isl_aff_free(aff));
1344 if (type == isl_dim_in)
1346 aff->ls = isl_local_space_set_dim_name(aff->ls, type, pos, s);
1348 return isl_aff_free(aff);
1353 __isl_give isl_aff *isl_aff_set_dim_id(__isl_take isl_aff *aff,
1354 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
1356 aff = isl_aff_cow(aff);
1358 return isl_id_free(id);
1359 if (type == isl_dim_out)
1360 isl_die(aff->v->ctx, isl_error_invalid,
1361 "cannot set name of output/set dimension",
1363 if (type == isl_dim_in)
1365 aff->ls = isl_local_space_set_dim_id(aff->ls, type, pos, id);
1367 return isl_aff_free(aff);
1376 /* Exploit the equalities in "eq" to simplify the affine expression
1377 * and the expressions of the integer divisions in the local space.
1378 * The integer divisions in this local space are assumed to appear
1379 * as regular dimensions in "eq".
1381 static __isl_give isl_aff *isl_aff_substitute_equalities_lifted(
1382 __isl_take isl_aff *aff, __isl_take isl_basic_set *eq)
1390 if (eq->n_eq == 0) {
1391 isl_basic_set_free(eq);
1395 aff = isl_aff_cow(aff);
1399 aff->ls = isl_local_space_substitute_equalities(aff->ls,
1400 isl_basic_set_copy(eq));
1401 aff->v = isl_vec_cow(aff->v);
1402 if (!aff->ls || !aff->v)
1405 total = 1 + isl_space_dim(eq->dim, isl_dim_all);
1407 for (i = 0; i < eq->n_eq; ++i) {
1408 j = isl_seq_last_non_zero(eq->eq[i], total + n_div);
1409 if (j < 0 || j == 0 || j >= total)
1412 isl_seq_elim(aff->v->el + 1, eq->eq[i], j, total,
1416 isl_basic_set_free(eq);
1417 aff = isl_aff_normalize(aff);
1420 isl_basic_set_free(eq);
1425 /* Exploit the equalities in "eq" to simplify the affine expression
1426 * and the expressions of the integer divisions in the local space.
1428 static __isl_give isl_aff *isl_aff_substitute_equalities(
1429 __isl_take isl_aff *aff, __isl_take isl_basic_set *eq)
1435 n_div = isl_local_space_dim(aff->ls, isl_dim_div);
1437 eq = isl_basic_set_add_dims(eq, isl_dim_set, n_div);
1438 return isl_aff_substitute_equalities_lifted(aff, eq);
1440 isl_basic_set_free(eq);
1445 /* Look for equalities among the variables shared by context and aff
1446 * and the integer divisions of aff, if any.
1447 * The equalities are then used to eliminate coefficients and/or integer
1448 * divisions from aff.
1450 __isl_give isl_aff *isl_aff_gist(__isl_take isl_aff *aff,
1451 __isl_take isl_set *context)
1453 isl_basic_set *hull;
1458 n_div = isl_local_space_dim(aff->ls, isl_dim_div);
1460 isl_basic_set *bset;
1461 isl_local_space *ls;
1462 context = isl_set_add_dims(context, isl_dim_set, n_div);
1463 ls = isl_aff_get_domain_local_space(aff);
1464 bset = isl_basic_set_from_local_space(ls);
1465 bset = isl_basic_set_lift(bset);
1466 bset = isl_basic_set_flatten(bset);
1467 context = isl_set_intersect(context,
1468 isl_set_from_basic_set(bset));
1471 hull = isl_set_affine_hull(context);
1472 return isl_aff_substitute_equalities_lifted(aff, hull);
1475 isl_set_free(context);
1479 __isl_give isl_aff *isl_aff_gist_params(__isl_take isl_aff *aff,
1480 __isl_take isl_set *context)
1482 isl_set *dom_context = isl_set_universe(isl_aff_get_domain_space(aff));
1483 dom_context = isl_set_intersect_params(dom_context, context);
1484 return isl_aff_gist(aff, dom_context);
1487 /* Return a basic set containing those elements in the space
1488 * of aff where it is non-negative.
1489 * If "rational" is set, then return a rational basic set.
1491 static __isl_give isl_basic_set *aff_nonneg_basic_set(
1492 __isl_take isl_aff *aff, int rational)
1494 isl_constraint *ineq;
1495 isl_basic_set *bset;
1497 ineq = isl_inequality_from_aff(aff);
1499 bset = isl_basic_set_from_constraint(ineq);
1501 bset = isl_basic_set_set_rational(bset);
1502 bset = isl_basic_set_simplify(bset);
1506 /* Return a basic set containing those elements in the space
1507 * of aff where it is non-negative.
1509 __isl_give isl_basic_set *isl_aff_nonneg_basic_set(__isl_take isl_aff *aff)
1511 return aff_nonneg_basic_set(aff, 0);
1514 /* Return a basic set containing those elements in the domain space
1515 * of aff where it is negative.
1517 __isl_give isl_basic_set *isl_aff_neg_basic_set(__isl_take isl_aff *aff)
1519 aff = isl_aff_neg(aff);
1520 aff = isl_aff_add_constant_num_si(aff, -1);
1521 return isl_aff_nonneg_basic_set(aff);
1524 /* Return a basic set containing those elements in the space
1525 * of aff where it is zero.
1526 * If "rational" is set, then return a rational basic set.
1528 static __isl_give isl_basic_set *aff_zero_basic_set(__isl_take isl_aff *aff,
1531 isl_constraint *ineq;
1532 isl_basic_set *bset;
1534 ineq = isl_equality_from_aff(aff);
1536 bset = isl_basic_set_from_constraint(ineq);
1538 bset = isl_basic_set_set_rational(bset);
1539 bset = isl_basic_set_simplify(bset);
1543 /* Return a basic set containing those elements in the space
1544 * of aff where it is zero.
1546 __isl_give isl_basic_set *isl_aff_zero_basic_set(__isl_take isl_aff *aff)
1548 return aff_zero_basic_set(aff, 0);
1551 /* Return a basic set containing those elements in the shared space
1552 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
1554 __isl_give isl_basic_set *isl_aff_ge_basic_set(__isl_take isl_aff *aff1,
1555 __isl_take isl_aff *aff2)
1557 aff1 = isl_aff_sub(aff1, aff2);
1559 return isl_aff_nonneg_basic_set(aff1);
1562 /* Return a basic set containing those elements in the shared space
1563 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
1565 __isl_give isl_basic_set *isl_aff_le_basic_set(__isl_take isl_aff *aff1,
1566 __isl_take isl_aff *aff2)
1568 return isl_aff_ge_basic_set(aff2, aff1);
1571 __isl_give isl_aff *isl_aff_add_on_domain(__isl_keep isl_set *dom,
1572 __isl_take isl_aff *aff1, __isl_take isl_aff *aff2)
1574 aff1 = isl_aff_add(aff1, aff2);
1575 aff1 = isl_aff_gist(aff1, isl_set_copy(dom));
1579 int isl_aff_is_empty(__isl_keep isl_aff *aff)
1587 /* Check whether the given affine expression has non-zero coefficient
1588 * for any dimension in the given range or if any of these dimensions
1589 * appear with non-zero coefficients in any of the integer divisions
1590 * involved in the affine expression.
1592 int isl_aff_involves_dims(__isl_keep isl_aff *aff,
1593 enum isl_dim_type type, unsigned first, unsigned n)
1605 ctx = isl_aff_get_ctx(aff);
1606 if (first + n > isl_aff_dim(aff, type))
1607 isl_die(ctx, isl_error_invalid,
1608 "range out of bounds", return -1);
1610 active = isl_local_space_get_active(aff->ls, aff->v->el + 2);
1614 first += isl_local_space_offset(aff->ls, type) - 1;
1615 for (i = 0; i < n; ++i)
1616 if (active[first + i]) {
1629 __isl_give isl_aff *isl_aff_drop_dims(__isl_take isl_aff *aff,
1630 enum isl_dim_type type, unsigned first, unsigned n)
1636 if (type == isl_dim_out)
1637 isl_die(aff->v->ctx, isl_error_invalid,
1638 "cannot drop output/set dimension",
1639 return isl_aff_free(aff));
1640 if (type == isl_dim_in)
1642 if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
1645 ctx = isl_aff_get_ctx(aff);
1646 if (first + n > isl_local_space_dim(aff->ls, type))
1647 isl_die(ctx, isl_error_invalid, "range out of bounds",
1648 return isl_aff_free(aff));
1650 aff = isl_aff_cow(aff);
1654 aff->ls = isl_local_space_drop_dims(aff->ls, type, first, n);
1656 return isl_aff_free(aff);
1658 first += 1 + isl_local_space_offset(aff->ls, type);
1659 aff->v = isl_vec_drop_els(aff->v, first, n);
1661 return isl_aff_free(aff);
1666 /* Project the domain of the affine expression onto its parameter space.
1667 * The affine expression may not involve any of the domain dimensions.
1669 __isl_give isl_aff *isl_aff_project_domain_on_params(__isl_take isl_aff *aff)
1675 n = isl_aff_dim(aff, isl_dim_in);
1676 involves = isl_aff_involves_dims(aff, isl_dim_in, 0, n);
1678 return isl_aff_free(aff);
1680 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1681 "affine expression involves some of the domain dimensions",
1682 return isl_aff_free(aff));
1683 aff = isl_aff_drop_dims(aff, isl_dim_in, 0, n);
1684 space = isl_aff_get_domain_space(aff);
1685 space = isl_space_params(space);
1686 aff = isl_aff_reset_domain_space(aff, space);
1690 __isl_give isl_aff *isl_aff_insert_dims(__isl_take isl_aff *aff,
1691 enum isl_dim_type type, unsigned first, unsigned n)
1697 if (type == isl_dim_out)
1698 isl_die(aff->v->ctx, isl_error_invalid,
1699 "cannot insert output/set dimensions",
1700 return isl_aff_free(aff));
1701 if (type == isl_dim_in)
1703 if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
1706 ctx = isl_aff_get_ctx(aff);
1707 if (first > isl_local_space_dim(aff->ls, type))
1708 isl_die(ctx, isl_error_invalid, "position out of bounds",
1709 return isl_aff_free(aff));
1711 aff = isl_aff_cow(aff);
1715 aff->ls = isl_local_space_insert_dims(aff->ls, type, first, n);
1717 return isl_aff_free(aff);
1719 first += 1 + isl_local_space_offset(aff->ls, type);
1720 aff->v = isl_vec_insert_zero_els(aff->v, first, n);
1722 return isl_aff_free(aff);
1727 __isl_give isl_aff *isl_aff_add_dims(__isl_take isl_aff *aff,
1728 enum isl_dim_type type, unsigned n)
1732 pos = isl_aff_dim(aff, type);
1734 return isl_aff_insert_dims(aff, type, pos, n);
1737 __isl_give isl_pw_aff *isl_pw_aff_add_dims(__isl_take isl_pw_aff *pwaff,
1738 enum isl_dim_type type, unsigned n)
1742 pos = isl_pw_aff_dim(pwaff, type);
1744 return isl_pw_aff_insert_dims(pwaff, type, pos, n);
1747 __isl_give isl_pw_aff *isl_pw_aff_from_aff(__isl_take isl_aff *aff)
1749 isl_set *dom = isl_set_universe(isl_aff_get_domain_space(aff));
1750 return isl_pw_aff_alloc(dom, aff);
1754 #define PW isl_pw_aff
1758 #define EL_IS_ZERO is_empty
1762 #define IS_ZERO is_empty
1765 #undef DEFAULT_IS_ZERO
1766 #define DEFAULT_IS_ZERO 0
1770 #define NO_MOVE_DIMS
1774 #include <isl_pw_templ.c>
1776 static __isl_give isl_set *align_params_pw_pw_set_and(
1777 __isl_take isl_pw_aff *pwaff1, __isl_take isl_pw_aff *pwaff2,
1778 __isl_give isl_set *(*fn)(__isl_take isl_pw_aff *pwaff1,
1779 __isl_take isl_pw_aff *pwaff2))
1781 if (!pwaff1 || !pwaff2)
1783 if (isl_space_match(pwaff1->dim, isl_dim_param,
1784 pwaff2->dim, isl_dim_param))
1785 return fn(pwaff1, pwaff2);
1786 if (!isl_space_has_named_params(pwaff1->dim) ||
1787 !isl_space_has_named_params(pwaff2->dim))
1788 isl_die(isl_pw_aff_get_ctx(pwaff1), isl_error_invalid,
1789 "unaligned unnamed parameters", goto error);
1790 pwaff1 = isl_pw_aff_align_params(pwaff1, isl_pw_aff_get_space(pwaff2));
1791 pwaff2 = isl_pw_aff_align_params(pwaff2, isl_pw_aff_get_space(pwaff1));
1792 return fn(pwaff1, pwaff2);
1794 isl_pw_aff_free(pwaff1);
1795 isl_pw_aff_free(pwaff2);
1799 /* Compute a piecewise quasi-affine expression with a domain that
1800 * is the union of those of pwaff1 and pwaff2 and such that on each
1801 * cell, the quasi-affine expression is the better (according to cmp)
1802 * of those of pwaff1 and pwaff2. If only one of pwaff1 or pwaff2
1803 * is defined on a given cell, then the associated expression
1804 * is the defined one.
1806 static __isl_give isl_pw_aff *pw_aff_union_opt(__isl_take isl_pw_aff *pwaff1,
1807 __isl_take isl_pw_aff *pwaff2,
1808 __isl_give isl_basic_set *(*cmp)(__isl_take isl_aff *aff1,
1809 __isl_take isl_aff *aff2))
1816 if (!pwaff1 || !pwaff2)
1819 ctx = isl_space_get_ctx(pwaff1->dim);
1820 if (!isl_space_is_equal(pwaff1->dim, pwaff2->dim))
1821 isl_die(ctx, isl_error_invalid,
1822 "arguments should live in same space", goto error);
1824 if (isl_pw_aff_is_empty(pwaff1)) {
1825 isl_pw_aff_free(pwaff1);
1829 if (isl_pw_aff_is_empty(pwaff2)) {
1830 isl_pw_aff_free(pwaff2);
1834 n = 2 * (pwaff1->n + 1) * (pwaff2->n + 1);
1835 res = isl_pw_aff_alloc_size(isl_space_copy(pwaff1->dim), n);
1837 for (i = 0; i < pwaff1->n; ++i) {
1838 set = isl_set_copy(pwaff1->p[i].set);
1839 for (j = 0; j < pwaff2->n; ++j) {
1840 struct isl_set *common;
1843 common = isl_set_intersect(
1844 isl_set_copy(pwaff1->p[i].set),
1845 isl_set_copy(pwaff2->p[j].set));
1846 better = isl_set_from_basic_set(cmp(
1847 isl_aff_copy(pwaff2->p[j].aff),
1848 isl_aff_copy(pwaff1->p[i].aff)));
1849 better = isl_set_intersect(common, better);
1850 if (isl_set_plain_is_empty(better)) {
1851 isl_set_free(better);
1854 set = isl_set_subtract(set, isl_set_copy(better));
1856 res = isl_pw_aff_add_piece(res, better,
1857 isl_aff_copy(pwaff2->p[j].aff));
1859 res = isl_pw_aff_add_piece(res, set,
1860 isl_aff_copy(pwaff1->p[i].aff));
1863 for (j = 0; j < pwaff2->n; ++j) {
1864 set = isl_set_copy(pwaff2->p[j].set);
1865 for (i = 0; i < pwaff1->n; ++i)
1866 set = isl_set_subtract(set,
1867 isl_set_copy(pwaff1->p[i].set));
1868 res = isl_pw_aff_add_piece(res, set,
1869 isl_aff_copy(pwaff2->p[j].aff));
1872 isl_pw_aff_free(pwaff1);
1873 isl_pw_aff_free(pwaff2);
1877 isl_pw_aff_free(pwaff1);
1878 isl_pw_aff_free(pwaff2);
1882 /* Compute a piecewise quasi-affine expression with a domain that
1883 * is the union of those of pwaff1 and pwaff2 and such that on each
1884 * cell, the quasi-affine expression is the maximum of those of pwaff1
1885 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
1886 * cell, then the associated expression is the defined one.
1888 static __isl_give isl_pw_aff *pw_aff_union_max(__isl_take isl_pw_aff *pwaff1,
1889 __isl_take isl_pw_aff *pwaff2)
1891 return pw_aff_union_opt(pwaff1, pwaff2, &isl_aff_ge_basic_set);
1894 __isl_give isl_pw_aff *isl_pw_aff_union_max(__isl_take isl_pw_aff *pwaff1,
1895 __isl_take isl_pw_aff *pwaff2)
1897 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2,
1901 /* Compute a piecewise quasi-affine expression with a domain that
1902 * is the union of those of pwaff1 and pwaff2 and such that on each
1903 * cell, the quasi-affine expression is the minimum of those of pwaff1
1904 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
1905 * cell, then the associated expression is the defined one.
1907 static __isl_give isl_pw_aff *pw_aff_union_min(__isl_take isl_pw_aff *pwaff1,
1908 __isl_take isl_pw_aff *pwaff2)
1910 return pw_aff_union_opt(pwaff1, pwaff2, &isl_aff_le_basic_set);
1913 __isl_give isl_pw_aff *isl_pw_aff_union_min(__isl_take isl_pw_aff *pwaff1,
1914 __isl_take isl_pw_aff *pwaff2)
1916 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2,
1920 __isl_give isl_pw_aff *isl_pw_aff_union_opt(__isl_take isl_pw_aff *pwaff1,
1921 __isl_take isl_pw_aff *pwaff2, int max)
1924 return isl_pw_aff_union_max(pwaff1, pwaff2);
1926 return isl_pw_aff_union_min(pwaff1, pwaff2);
1929 /* Construct a map with as domain the domain of pwaff and
1930 * one-dimensional range corresponding to the affine expressions.
1932 static __isl_give isl_map *map_from_pw_aff(__isl_take isl_pw_aff *pwaff)
1941 dim = isl_pw_aff_get_space(pwaff);
1942 map = isl_map_empty(dim);
1944 for (i = 0; i < pwaff->n; ++i) {
1945 isl_basic_map *bmap;
1948 bmap = isl_basic_map_from_aff(isl_aff_copy(pwaff->p[i].aff));
1949 map_i = isl_map_from_basic_map(bmap);
1950 map_i = isl_map_intersect_domain(map_i,
1951 isl_set_copy(pwaff->p[i].set));
1952 map = isl_map_union_disjoint(map, map_i);
1955 isl_pw_aff_free(pwaff);
1960 /* Construct a map with as domain the domain of pwaff and
1961 * one-dimensional range corresponding to the affine expressions.
1963 __isl_give isl_map *isl_map_from_pw_aff(__isl_take isl_pw_aff *pwaff)
1967 if (isl_space_is_set(pwaff->dim))
1968 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
1969 "space of input is not a map",
1970 return isl_pw_aff_free(pwaff));
1971 return map_from_pw_aff(pwaff);
1974 /* Construct a one-dimensional set with as parameter domain
1975 * the domain of pwaff and the single set dimension
1976 * corresponding to the affine expressions.
1978 __isl_give isl_set *isl_set_from_pw_aff(__isl_take isl_pw_aff *pwaff)
1982 if (!isl_space_is_set(pwaff->dim))
1983 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
1984 "space of input is not a set",
1985 return isl_pw_aff_free(pwaff));
1986 return map_from_pw_aff(pwaff);
1989 /* Return a set containing those elements in the domain
1990 * of pwaff where it is non-negative.
1992 __isl_give isl_set *isl_pw_aff_nonneg_set(__isl_take isl_pw_aff *pwaff)
2000 set = isl_set_empty(isl_pw_aff_get_domain_space(pwaff));
2002 for (i = 0; i < pwaff->n; ++i) {
2003 isl_basic_set *bset;
2007 rational = isl_set_has_rational(pwaff->p[i].set);
2008 bset = aff_nonneg_basic_set(isl_aff_copy(pwaff->p[i].aff),
2010 set_i = isl_set_from_basic_set(bset);
2011 set_i = isl_set_intersect(set_i, isl_set_copy(pwaff->p[i].set));
2012 set = isl_set_union_disjoint(set, set_i);
2015 isl_pw_aff_free(pwaff);
2020 /* Return a set containing those elements in the domain
2021 * of pwaff where it is zero (if complement is 0) or not zero
2022 * (if complement is 1).
2024 static __isl_give isl_set *pw_aff_zero_set(__isl_take isl_pw_aff *pwaff,
2033 set = isl_set_empty(isl_pw_aff_get_domain_space(pwaff));
2035 for (i = 0; i < pwaff->n; ++i) {
2036 isl_basic_set *bset;
2037 isl_set *set_i, *zero;
2040 rational = isl_set_has_rational(pwaff->p[i].set);
2041 bset = aff_zero_basic_set(isl_aff_copy(pwaff->p[i].aff),
2043 zero = isl_set_from_basic_set(bset);
2044 set_i = isl_set_copy(pwaff->p[i].set);
2046 set_i = isl_set_subtract(set_i, zero);
2048 set_i = isl_set_intersect(set_i, zero);
2049 set = isl_set_union_disjoint(set, set_i);
2052 isl_pw_aff_free(pwaff);
2057 /* Return a set containing those elements in the domain
2058 * of pwaff where it is zero.
2060 __isl_give isl_set *isl_pw_aff_zero_set(__isl_take isl_pw_aff *pwaff)
2062 return pw_aff_zero_set(pwaff, 0);
2065 /* Return a set containing those elements in the domain
2066 * of pwaff where it is not zero.
2068 __isl_give isl_set *isl_pw_aff_non_zero_set(__isl_take isl_pw_aff *pwaff)
2070 return pw_aff_zero_set(pwaff, 1);
2073 /* Return a set containing those elements in the shared domain
2074 * of pwaff1 and pwaff2 where pwaff1 is greater than (or equal) to pwaff2.
2076 * We compute the difference on the shared domain and then construct
2077 * the set of values where this difference is non-negative.
2078 * If strict is set, we first subtract 1 from the difference.
2079 * If equal is set, we only return the elements where pwaff1 and pwaff2
2082 static __isl_give isl_set *pw_aff_gte_set(__isl_take isl_pw_aff *pwaff1,
2083 __isl_take isl_pw_aff *pwaff2, int strict, int equal)
2085 isl_set *set1, *set2;
2087 set1 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff1));
2088 set2 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff2));
2089 set1 = isl_set_intersect(set1, set2);
2090 pwaff1 = isl_pw_aff_intersect_domain(pwaff1, isl_set_copy(set1));
2091 pwaff2 = isl_pw_aff_intersect_domain(pwaff2, isl_set_copy(set1));
2092 pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_neg(pwaff2));
2095 isl_space *dim = isl_set_get_space(set1);
2097 aff = isl_aff_zero_on_domain(isl_local_space_from_space(dim));
2098 aff = isl_aff_add_constant_si(aff, -1);
2099 pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_alloc(set1, aff));
2104 return isl_pw_aff_zero_set(pwaff1);
2105 return isl_pw_aff_nonneg_set(pwaff1);
2108 /* Return a set containing those elements in the shared domain
2109 * of pwaff1 and pwaff2 where pwaff1 is equal to pwaff2.
2111 static __isl_give isl_set *pw_aff_eq_set(__isl_take isl_pw_aff *pwaff1,
2112 __isl_take isl_pw_aff *pwaff2)
2114 return pw_aff_gte_set(pwaff1, pwaff2, 0, 1);
2117 __isl_give isl_set *isl_pw_aff_eq_set(__isl_take isl_pw_aff *pwaff1,
2118 __isl_take isl_pw_aff *pwaff2)
2120 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_eq_set);
2123 /* Return a set containing those elements in the shared domain
2124 * of pwaff1 and pwaff2 where pwaff1 is greater than or equal to pwaff2.
2126 static __isl_give isl_set *pw_aff_ge_set(__isl_take isl_pw_aff *pwaff1,
2127 __isl_take isl_pw_aff *pwaff2)
2129 return pw_aff_gte_set(pwaff1, pwaff2, 0, 0);
2132 __isl_give isl_set *isl_pw_aff_ge_set(__isl_take isl_pw_aff *pwaff1,
2133 __isl_take isl_pw_aff *pwaff2)
2135 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_ge_set);
2138 /* Return a set containing those elements in the shared domain
2139 * of pwaff1 and pwaff2 where pwaff1 is strictly greater than pwaff2.
2141 static __isl_give isl_set *pw_aff_gt_set(__isl_take isl_pw_aff *pwaff1,
2142 __isl_take isl_pw_aff *pwaff2)
2144 return pw_aff_gte_set(pwaff1, pwaff2, 1, 0);
2147 __isl_give isl_set *isl_pw_aff_gt_set(__isl_take isl_pw_aff *pwaff1,
2148 __isl_take isl_pw_aff *pwaff2)
2150 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_gt_set);
2153 __isl_give isl_set *isl_pw_aff_le_set(__isl_take isl_pw_aff *pwaff1,
2154 __isl_take isl_pw_aff *pwaff2)
2156 return isl_pw_aff_ge_set(pwaff2, pwaff1);
2159 __isl_give isl_set *isl_pw_aff_lt_set(__isl_take isl_pw_aff *pwaff1,
2160 __isl_take isl_pw_aff *pwaff2)
2162 return isl_pw_aff_gt_set(pwaff2, pwaff1);
2165 /* Return a set containing those elements in the shared domain
2166 * of the elements of list1 and list2 where each element in list1
2167 * has the relation specified by "fn" with each element in list2.
2169 static __isl_give isl_set *pw_aff_list_set(__isl_take isl_pw_aff_list *list1,
2170 __isl_take isl_pw_aff_list *list2,
2171 __isl_give isl_set *(*fn)(__isl_take isl_pw_aff *pwaff1,
2172 __isl_take isl_pw_aff *pwaff2))
2178 if (!list1 || !list2)
2181 ctx = isl_pw_aff_list_get_ctx(list1);
2182 if (list1->n < 1 || list2->n < 1)
2183 isl_die(ctx, isl_error_invalid,
2184 "list should contain at least one element", goto error);
2186 set = isl_set_universe(isl_pw_aff_get_domain_space(list1->p[0]));
2187 for (i = 0; i < list1->n; ++i)
2188 for (j = 0; j < list2->n; ++j) {
2191 set_ij = fn(isl_pw_aff_copy(list1->p[i]),
2192 isl_pw_aff_copy(list2->p[j]));
2193 set = isl_set_intersect(set, set_ij);
2196 isl_pw_aff_list_free(list1);
2197 isl_pw_aff_list_free(list2);
2200 isl_pw_aff_list_free(list1);
2201 isl_pw_aff_list_free(list2);
2205 /* Return a set containing those elements in the shared domain
2206 * of the elements of list1 and list2 where each element in list1
2207 * is equal to each element in list2.
2209 __isl_give isl_set *isl_pw_aff_list_eq_set(__isl_take isl_pw_aff_list *list1,
2210 __isl_take isl_pw_aff_list *list2)
2212 return pw_aff_list_set(list1, list2, &isl_pw_aff_eq_set);
2215 __isl_give isl_set *isl_pw_aff_list_ne_set(__isl_take isl_pw_aff_list *list1,
2216 __isl_take isl_pw_aff_list *list2)
2218 return pw_aff_list_set(list1, list2, &isl_pw_aff_ne_set);
2221 /* Return a set containing those elements in the shared domain
2222 * of the elements of list1 and list2 where each element in list1
2223 * is less than or equal to each element in list2.
2225 __isl_give isl_set *isl_pw_aff_list_le_set(__isl_take isl_pw_aff_list *list1,
2226 __isl_take isl_pw_aff_list *list2)
2228 return pw_aff_list_set(list1, list2, &isl_pw_aff_le_set);
2231 __isl_give isl_set *isl_pw_aff_list_lt_set(__isl_take isl_pw_aff_list *list1,
2232 __isl_take isl_pw_aff_list *list2)
2234 return pw_aff_list_set(list1, list2, &isl_pw_aff_lt_set);
2237 __isl_give isl_set *isl_pw_aff_list_ge_set(__isl_take isl_pw_aff_list *list1,
2238 __isl_take isl_pw_aff_list *list2)
2240 return pw_aff_list_set(list1, list2, &isl_pw_aff_ge_set);
2243 __isl_give isl_set *isl_pw_aff_list_gt_set(__isl_take isl_pw_aff_list *list1,
2244 __isl_take isl_pw_aff_list *list2)
2246 return pw_aff_list_set(list1, list2, &isl_pw_aff_gt_set);
2250 /* Return a set containing those elements in the shared domain
2251 * of pwaff1 and pwaff2 where pwaff1 is not equal to pwaff2.
2253 static __isl_give isl_set *pw_aff_ne_set(__isl_take isl_pw_aff *pwaff1,
2254 __isl_take isl_pw_aff *pwaff2)
2256 isl_set *set_lt, *set_gt;
2258 set_lt = isl_pw_aff_lt_set(isl_pw_aff_copy(pwaff1),
2259 isl_pw_aff_copy(pwaff2));
2260 set_gt = isl_pw_aff_gt_set(pwaff1, pwaff2);
2261 return isl_set_union_disjoint(set_lt, set_gt);
2264 __isl_give isl_set *isl_pw_aff_ne_set(__isl_take isl_pw_aff *pwaff1,
2265 __isl_take isl_pw_aff *pwaff2)
2267 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_ne_set);
2270 __isl_give isl_pw_aff *isl_pw_aff_scale_down(__isl_take isl_pw_aff *pwaff,
2275 if (isl_int_is_one(v))
2277 if (!isl_int_is_pos(v))
2278 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
2279 "factor needs to be positive",
2280 return isl_pw_aff_free(pwaff));
2281 pwaff = isl_pw_aff_cow(pwaff);
2287 for (i = 0; i < pwaff->n; ++i) {
2288 pwaff->p[i].aff = isl_aff_scale_down(pwaff->p[i].aff, v);
2289 if (!pwaff->p[i].aff)
2290 return isl_pw_aff_free(pwaff);
2296 __isl_give isl_pw_aff *isl_pw_aff_floor(__isl_take isl_pw_aff *pwaff)
2300 pwaff = isl_pw_aff_cow(pwaff);
2306 for (i = 0; i < pwaff->n; ++i) {
2307 pwaff->p[i].aff = isl_aff_floor(pwaff->p[i].aff);
2308 if (!pwaff->p[i].aff)
2309 return isl_pw_aff_free(pwaff);
2315 __isl_give isl_pw_aff *isl_pw_aff_ceil(__isl_take isl_pw_aff *pwaff)
2319 pwaff = isl_pw_aff_cow(pwaff);
2325 for (i = 0; i < pwaff->n; ++i) {
2326 pwaff->p[i].aff = isl_aff_ceil(pwaff->p[i].aff);
2327 if (!pwaff->p[i].aff)
2328 return isl_pw_aff_free(pwaff);
2334 /* Assuming that "cond1" and "cond2" are disjoint,
2335 * return an affine expression that is equal to pwaff1 on cond1
2336 * and to pwaff2 on cond2.
2338 static __isl_give isl_pw_aff *isl_pw_aff_select(
2339 __isl_take isl_set *cond1, __isl_take isl_pw_aff *pwaff1,
2340 __isl_take isl_set *cond2, __isl_take isl_pw_aff *pwaff2)
2342 pwaff1 = isl_pw_aff_intersect_domain(pwaff1, cond1);
2343 pwaff2 = isl_pw_aff_intersect_domain(pwaff2, cond2);
2345 return isl_pw_aff_add_disjoint(pwaff1, pwaff2);
2348 /* Return an affine expression that is equal to pwaff_true for elements
2349 * where "cond" is non-zero and to pwaff_false for elements where "cond"
2351 * That is, return cond ? pwaff_true : pwaff_false;
2353 __isl_give isl_pw_aff *isl_pw_aff_cond(__isl_take isl_pw_aff *cond,
2354 __isl_take isl_pw_aff *pwaff_true, __isl_take isl_pw_aff *pwaff_false)
2356 isl_set *cond_true, *cond_false;
2358 cond_true = isl_pw_aff_non_zero_set(isl_pw_aff_copy(cond));
2359 cond_false = isl_pw_aff_zero_set(cond);
2360 return isl_pw_aff_select(cond_true, pwaff_true,
2361 cond_false, pwaff_false);
2364 int isl_aff_is_cst(__isl_keep isl_aff *aff)
2369 return isl_seq_first_non_zero(aff->v->el + 2, aff->v->size - 2) == -1;
2372 /* Check whether pwaff is a piecewise constant.
2374 int isl_pw_aff_is_cst(__isl_keep isl_pw_aff *pwaff)
2381 for (i = 0; i < pwaff->n; ++i) {
2382 int is_cst = isl_aff_is_cst(pwaff->p[i].aff);
2383 if (is_cst < 0 || !is_cst)
2390 __isl_give isl_aff *isl_aff_mul(__isl_take isl_aff *aff1,
2391 __isl_take isl_aff *aff2)
2393 if (!isl_aff_is_cst(aff2) && isl_aff_is_cst(aff1))
2394 return isl_aff_mul(aff2, aff1);
2396 if (!isl_aff_is_cst(aff2))
2397 isl_die(isl_aff_get_ctx(aff1), isl_error_invalid,
2398 "at least one affine expression should be constant",
2401 aff1 = isl_aff_cow(aff1);
2405 aff1 = isl_aff_scale(aff1, aff2->v->el[1]);
2406 aff1 = isl_aff_scale_down(aff1, aff2->v->el[0]);
2416 /* Divide "aff1" by "aff2", assuming "aff2" is a piecewise constant.
2418 __isl_give isl_aff *isl_aff_div(__isl_take isl_aff *aff1,
2419 __isl_take isl_aff *aff2)
2424 is_cst = isl_aff_is_cst(aff2);
2428 isl_die(isl_aff_get_ctx(aff2), isl_error_invalid,
2429 "second argument should be a constant", goto error);
2434 neg = isl_int_is_neg(aff2->v->el[1]);
2436 isl_int_neg(aff2->v->el[0], aff2->v->el[0]);
2437 isl_int_neg(aff2->v->el[1], aff2->v->el[1]);
2440 aff1 = isl_aff_scale(aff1, aff2->v->el[0]);
2441 aff1 = isl_aff_scale_down(aff1, aff2->v->el[1]);
2444 isl_int_neg(aff2->v->el[0], aff2->v->el[0]);
2445 isl_int_neg(aff2->v->el[1], aff2->v->el[1]);
2456 static __isl_give isl_pw_aff *pw_aff_add(__isl_take isl_pw_aff *pwaff1,
2457 __isl_take isl_pw_aff *pwaff2)
2459 return isl_pw_aff_on_shared_domain(pwaff1, pwaff2, &isl_aff_add);
2462 __isl_give isl_pw_aff *isl_pw_aff_add(__isl_take isl_pw_aff *pwaff1,
2463 __isl_take isl_pw_aff *pwaff2)
2465 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_add);
2468 __isl_give isl_pw_aff *isl_pw_aff_union_add(__isl_take isl_pw_aff *pwaff1,
2469 __isl_take isl_pw_aff *pwaff2)
2471 return isl_pw_aff_union_add_(pwaff1, pwaff2);
2474 static __isl_give isl_pw_aff *pw_aff_mul(__isl_take isl_pw_aff *pwaff1,
2475 __isl_take isl_pw_aff *pwaff2)
2477 return isl_pw_aff_on_shared_domain(pwaff1, pwaff2, &isl_aff_mul);
2480 __isl_give isl_pw_aff *isl_pw_aff_mul(__isl_take isl_pw_aff *pwaff1,
2481 __isl_take isl_pw_aff *pwaff2)
2483 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_mul);
2486 static __isl_give isl_pw_aff *pw_aff_div(__isl_take isl_pw_aff *pa1,
2487 __isl_take isl_pw_aff *pa2)
2489 return isl_pw_aff_on_shared_domain(pa1, pa2, &isl_aff_div);
2492 /* Divide "pa1" by "pa2", assuming "pa2" is a piecewise constant.
2494 __isl_give isl_pw_aff *isl_pw_aff_div(__isl_take isl_pw_aff *pa1,
2495 __isl_take isl_pw_aff *pa2)
2499 is_cst = isl_pw_aff_is_cst(pa2);
2503 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
2504 "second argument should be a piecewise constant",
2506 return isl_pw_aff_align_params_pw_pw_and(pa1, pa2, &pw_aff_div);
2508 isl_pw_aff_free(pa1);
2509 isl_pw_aff_free(pa2);
2513 /* Compute the quotient of the integer division of "pa1" by "pa2"
2514 * with rounding towards zero.
2515 * "pa2" is assumed to be a piecewise constant.
2517 * In particular, return
2519 * pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2)
2522 __isl_give isl_pw_aff *isl_pw_aff_tdiv_q(__isl_take isl_pw_aff *pa1,
2523 __isl_take isl_pw_aff *pa2)
2529 is_cst = isl_pw_aff_is_cst(pa2);
2533 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
2534 "second argument should be a piecewise constant",
2537 pa1 = isl_pw_aff_div(pa1, pa2);
2539 cond = isl_pw_aff_nonneg_set(isl_pw_aff_copy(pa1));
2540 f = isl_pw_aff_floor(isl_pw_aff_copy(pa1));
2541 c = isl_pw_aff_ceil(pa1);
2542 return isl_pw_aff_cond(isl_set_indicator_function(cond), f, c);
2544 isl_pw_aff_free(pa1);
2545 isl_pw_aff_free(pa2);
2549 /* Compute the remainder of the integer division of "pa1" by "pa2"
2550 * with rounding towards zero.
2551 * "pa2" is assumed to be a piecewise constant.
2553 * In particular, return
2555 * pa1 - pa2 * (pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2))
2558 __isl_give isl_pw_aff *isl_pw_aff_tdiv_r(__isl_take isl_pw_aff *pa1,
2559 __isl_take isl_pw_aff *pa2)
2564 is_cst = isl_pw_aff_is_cst(pa2);
2568 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
2569 "second argument should be a piecewise constant",
2571 res = isl_pw_aff_tdiv_q(isl_pw_aff_copy(pa1), isl_pw_aff_copy(pa2));
2572 res = isl_pw_aff_mul(pa2, res);
2573 res = isl_pw_aff_sub(pa1, res);
2576 isl_pw_aff_free(pa1);
2577 isl_pw_aff_free(pa2);
2581 static __isl_give isl_pw_aff *pw_aff_min(__isl_take isl_pw_aff *pwaff1,
2582 __isl_take isl_pw_aff *pwaff2)
2587 dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1)),
2588 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2)));
2589 le = isl_pw_aff_le_set(isl_pw_aff_copy(pwaff1),
2590 isl_pw_aff_copy(pwaff2));
2591 dom = isl_set_subtract(dom, isl_set_copy(le));
2592 return isl_pw_aff_select(le, pwaff1, dom, pwaff2);
2595 __isl_give isl_pw_aff *isl_pw_aff_min(__isl_take isl_pw_aff *pwaff1,
2596 __isl_take isl_pw_aff *pwaff2)
2598 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_min);
2601 static __isl_give isl_pw_aff *pw_aff_max(__isl_take isl_pw_aff *pwaff1,
2602 __isl_take isl_pw_aff *pwaff2)
2607 dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1)),
2608 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2)));
2609 ge = isl_pw_aff_ge_set(isl_pw_aff_copy(pwaff1),
2610 isl_pw_aff_copy(pwaff2));
2611 dom = isl_set_subtract(dom, isl_set_copy(ge));
2612 return isl_pw_aff_select(ge, pwaff1, dom, pwaff2);
2615 __isl_give isl_pw_aff *isl_pw_aff_max(__isl_take isl_pw_aff *pwaff1,
2616 __isl_take isl_pw_aff *pwaff2)
2618 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_max);
2621 static __isl_give isl_pw_aff *pw_aff_list_reduce(
2622 __isl_take isl_pw_aff_list *list,
2623 __isl_give isl_pw_aff *(*fn)(__isl_take isl_pw_aff *pwaff1,
2624 __isl_take isl_pw_aff *pwaff2))
2633 ctx = isl_pw_aff_list_get_ctx(list);
2635 isl_die(ctx, isl_error_invalid,
2636 "list should contain at least one element",
2637 return isl_pw_aff_list_free(list));
2639 res = isl_pw_aff_copy(list->p[0]);
2640 for (i = 1; i < list->n; ++i)
2641 res = fn(res, isl_pw_aff_copy(list->p[i]));
2643 isl_pw_aff_list_free(list);
2647 /* Return an isl_pw_aff that maps each element in the intersection of the
2648 * domains of the elements of list to the minimal corresponding affine
2651 __isl_give isl_pw_aff *isl_pw_aff_list_min(__isl_take isl_pw_aff_list *list)
2653 return pw_aff_list_reduce(list, &isl_pw_aff_min);
2656 /* Return an isl_pw_aff that maps each element in the intersection of the
2657 * domains of the elements of list to the maximal corresponding affine
2660 __isl_give isl_pw_aff *isl_pw_aff_list_max(__isl_take isl_pw_aff_list *list)
2662 return pw_aff_list_reduce(list, &isl_pw_aff_max);
2665 /* Mark the domains of "pwaff" as rational.
2667 __isl_give isl_pw_aff *isl_pw_aff_set_rational(__isl_take isl_pw_aff *pwaff)
2671 pwaff = isl_pw_aff_cow(pwaff);
2677 for (i = 0; i < pwaff->n; ++i) {
2678 pwaff->p[i].set = isl_set_set_rational(pwaff->p[i].set);
2679 if (!pwaff->p[i].set)
2680 return isl_pw_aff_free(pwaff);
2686 /* Mark the domains of the elements of "list" as rational.
2688 __isl_give isl_pw_aff_list *isl_pw_aff_list_set_rational(
2689 __isl_take isl_pw_aff_list *list)
2699 for (i = 0; i < n; ++i) {
2702 pa = isl_pw_aff_list_get_pw_aff(list, i);
2703 pa = isl_pw_aff_set_rational(pa);
2704 list = isl_pw_aff_list_set_pw_aff(list, i, pa);
2710 /* Check that the domain space of "aff" matches "space".
2712 * Return 0 on success and -1 on error.
2714 int isl_aff_check_match_domain_space(__isl_keep isl_aff *aff,
2715 __isl_keep isl_space *space)
2717 isl_space *aff_space;
2723 aff_space = isl_aff_get_domain_space(aff);
2725 match = isl_space_match(space, isl_dim_param, aff_space, isl_dim_param);
2729 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2730 "parameters don't match", goto error);
2731 match = isl_space_tuple_match(space, isl_dim_in,
2732 aff_space, isl_dim_set);
2736 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2737 "domains don't match", goto error);
2738 isl_space_free(aff_space);
2741 isl_space_free(aff_space);
2748 #include <isl_multi_templ.c>
2750 /* Create an isl_pw_multi_aff with the given isl_multi_aff on a universe
2753 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_multi_aff(
2754 __isl_take isl_multi_aff *ma)
2756 isl_set *dom = isl_set_universe(isl_multi_aff_get_domain_space(ma));
2757 return isl_pw_multi_aff_alloc(dom, ma);
2760 /* Create a piecewise multi-affine expression in the given space that maps each
2761 * input dimension to the corresponding output dimension.
2763 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_identity(
2764 __isl_take isl_space *space)
2766 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_identity(space));
2769 __isl_give isl_multi_aff *isl_multi_aff_add(__isl_take isl_multi_aff *maff1,
2770 __isl_take isl_multi_aff *maff2)
2772 return isl_multi_aff_bin_op(maff1, maff2, &isl_aff_add);
2775 /* Subtract "ma2" from "ma1" and return the result.
2777 __isl_give isl_multi_aff *isl_multi_aff_sub(__isl_take isl_multi_aff *ma1,
2778 __isl_take isl_multi_aff *ma2)
2780 return isl_multi_aff_bin_op(ma1, ma2, &isl_aff_sub);
2783 /* Given two multi-affine expressions A -> B and C -> D,
2784 * construct a multi-affine expression [A -> C] -> [B -> D].
2786 __isl_give isl_multi_aff *isl_multi_aff_product(
2787 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2)
2793 int in1, in2, out1, out2;
2795 in1 = isl_multi_aff_dim(ma1, isl_dim_in);
2796 in2 = isl_multi_aff_dim(ma2, isl_dim_in);
2797 out1 = isl_multi_aff_dim(ma1, isl_dim_out);
2798 out2 = isl_multi_aff_dim(ma2, isl_dim_out);
2799 space = isl_space_product(isl_multi_aff_get_space(ma1),
2800 isl_multi_aff_get_space(ma2));
2801 res = isl_multi_aff_alloc(isl_space_copy(space));
2802 space = isl_space_domain(space);
2804 for (i = 0; i < out1; ++i) {
2805 aff = isl_multi_aff_get_aff(ma1, i);
2806 aff = isl_aff_insert_dims(aff, isl_dim_in, in1, in2);
2807 aff = isl_aff_reset_domain_space(aff, isl_space_copy(space));
2808 res = isl_multi_aff_set_aff(res, i, aff);
2811 for (i = 0; i < out2; ++i) {
2812 aff = isl_multi_aff_get_aff(ma2, i);
2813 aff = isl_aff_insert_dims(aff, isl_dim_in, 0, in1);
2814 aff = isl_aff_reset_domain_space(aff, isl_space_copy(space));
2815 res = isl_multi_aff_set_aff(res, out1 + i, aff);
2818 isl_space_free(space);
2819 isl_multi_aff_free(ma1);
2820 isl_multi_aff_free(ma2);
2824 /* Exploit the equalities in "eq" to simplify the affine expressions.
2826 static __isl_give isl_multi_aff *isl_multi_aff_substitute_equalities(
2827 __isl_take isl_multi_aff *maff, __isl_take isl_basic_set *eq)
2831 maff = isl_multi_aff_cow(maff);
2835 for (i = 0; i < maff->n; ++i) {
2836 maff->p[i] = isl_aff_substitute_equalities(maff->p[i],
2837 isl_basic_set_copy(eq));
2842 isl_basic_set_free(eq);
2845 isl_basic_set_free(eq);
2846 isl_multi_aff_free(maff);
2850 __isl_give isl_multi_aff *isl_multi_aff_scale(__isl_take isl_multi_aff *maff,
2855 maff = isl_multi_aff_cow(maff);
2859 for (i = 0; i < maff->n; ++i) {
2860 maff->p[i] = isl_aff_scale(maff->p[i], f);
2862 return isl_multi_aff_free(maff);
2868 __isl_give isl_multi_aff *isl_multi_aff_add_on_domain(__isl_keep isl_set *dom,
2869 __isl_take isl_multi_aff *maff1, __isl_take isl_multi_aff *maff2)
2871 maff1 = isl_multi_aff_add(maff1, maff2);
2872 maff1 = isl_multi_aff_gist(maff1, isl_set_copy(dom));
2876 int isl_multi_aff_is_empty(__isl_keep isl_multi_aff *maff)
2884 int isl_multi_aff_plain_is_equal(__isl_keep isl_multi_aff *maff1,
2885 __isl_keep isl_multi_aff *maff2)
2890 if (!maff1 || !maff2)
2892 if (maff1->n != maff2->n)
2894 equal = isl_space_is_equal(maff1->space, maff2->space);
2895 if (equal < 0 || !equal)
2898 for (i = 0; i < maff1->n; ++i) {
2899 equal = isl_aff_plain_is_equal(maff1->p[i], maff2->p[i]);
2900 if (equal < 0 || !equal)
2907 /* Return the set of domain elements where "ma1" is lexicographically
2908 * smaller than or equal to "ma2".
2910 __isl_give isl_set *isl_multi_aff_lex_le_set(__isl_take isl_multi_aff *ma1,
2911 __isl_take isl_multi_aff *ma2)
2913 return isl_multi_aff_lex_ge_set(ma2, ma1);
2916 /* Return the set of domain elements where "ma1" is lexicographically
2917 * greater than or equal to "ma2".
2919 __isl_give isl_set *isl_multi_aff_lex_ge_set(__isl_take isl_multi_aff *ma1,
2920 __isl_take isl_multi_aff *ma2)
2923 isl_map *map1, *map2;
2926 map1 = isl_map_from_multi_aff(ma1);
2927 map2 = isl_map_from_multi_aff(ma2);
2928 map = isl_map_range_product(map1, map2);
2929 space = isl_space_range(isl_map_get_space(map));
2930 space = isl_space_domain(isl_space_unwrap(space));
2931 ge = isl_map_lex_ge(space);
2932 map = isl_map_intersect_range(map, isl_map_wrap(ge));
2934 return isl_map_domain(map);
2938 #define PW isl_pw_multi_aff
2940 #define EL isl_multi_aff
2942 #define EL_IS_ZERO is_empty
2946 #define IS_ZERO is_empty
2949 #undef DEFAULT_IS_ZERO
2950 #define DEFAULT_IS_ZERO 0
2955 #define NO_INVOLVES_DIMS
2956 #define NO_MOVE_DIMS
2957 #define NO_INSERT_DIMS
2961 #include <isl_pw_templ.c>
2964 #define UNION isl_union_pw_multi_aff
2966 #define PART isl_pw_multi_aff
2968 #define PARTS pw_multi_aff
2969 #define ALIGN_DOMAIN
2973 #include <isl_union_templ.c>
2975 /* Given a function "cmp" that returns the set of elements where
2976 * "ma1" is "better" than "ma2", return the intersection of this
2977 * set with "dom1" and "dom2".
2979 static __isl_give isl_set *shared_and_better(__isl_keep isl_set *dom1,
2980 __isl_keep isl_set *dom2, __isl_keep isl_multi_aff *ma1,
2981 __isl_keep isl_multi_aff *ma2,
2982 __isl_give isl_set *(*cmp)(__isl_take isl_multi_aff *ma1,
2983 __isl_take isl_multi_aff *ma2))
2989 common = isl_set_intersect(isl_set_copy(dom1), isl_set_copy(dom2));
2990 is_empty = isl_set_plain_is_empty(common);
2991 if (is_empty >= 0 && is_empty)
2994 return isl_set_free(common);
2995 better = cmp(isl_multi_aff_copy(ma1), isl_multi_aff_copy(ma2));
2996 better = isl_set_intersect(common, better);
3001 /* Given a function "cmp" that returns the set of elements where
3002 * "ma1" is "better" than "ma2", return a piecewise multi affine
3003 * expression defined on the union of the definition domains
3004 * of "pma1" and "pma2" that maps to the "best" of "pma1" and
3005 * "pma2" on each cell. If only one of the two input functions
3006 * is defined on a given cell, then it is considered the best.
3008 static __isl_give isl_pw_multi_aff *pw_multi_aff_union_opt(
3009 __isl_take isl_pw_multi_aff *pma1,
3010 __isl_take isl_pw_multi_aff *pma2,
3011 __isl_give isl_set *(*cmp)(__isl_take isl_multi_aff *ma1,
3012 __isl_take isl_multi_aff *ma2))
3015 isl_pw_multi_aff *res = NULL;
3017 isl_set *set = NULL;
3022 ctx = isl_space_get_ctx(pma1->dim);
3023 if (!isl_space_is_equal(pma1->dim, pma2->dim))
3024 isl_die(ctx, isl_error_invalid,
3025 "arguments should live in the same space", goto error);
3027 if (isl_pw_multi_aff_is_empty(pma1)) {
3028 isl_pw_multi_aff_free(pma1);
3032 if (isl_pw_multi_aff_is_empty(pma2)) {
3033 isl_pw_multi_aff_free(pma2);
3037 n = 2 * (pma1->n + 1) * (pma2->n + 1);
3038 res = isl_pw_multi_aff_alloc_size(isl_space_copy(pma1->dim), n);
3040 for (i = 0; i < pma1->n; ++i) {
3041 set = isl_set_copy(pma1->p[i].set);
3042 for (j = 0; j < pma2->n; ++j) {
3046 better = shared_and_better(pma2->p[j].set,
3047 pma1->p[i].set, pma2->p[j].maff,
3048 pma1->p[i].maff, cmp);
3049 is_empty = isl_set_plain_is_empty(better);
3050 if (is_empty < 0 || is_empty) {
3051 isl_set_free(better);
3056 set = isl_set_subtract(set, isl_set_copy(better));
3058 res = isl_pw_multi_aff_add_piece(res, better,
3059 isl_multi_aff_copy(pma2->p[j].maff));
3061 res = isl_pw_multi_aff_add_piece(res, set,
3062 isl_multi_aff_copy(pma1->p[i].maff));
3065 for (j = 0; j < pma2->n; ++j) {
3066 set = isl_set_copy(pma2->p[j].set);
3067 for (i = 0; i < pma1->n; ++i)
3068 set = isl_set_subtract(set,
3069 isl_set_copy(pma1->p[i].set));
3070 res = isl_pw_multi_aff_add_piece(res, set,
3071 isl_multi_aff_copy(pma2->p[j].maff));
3074 isl_pw_multi_aff_free(pma1);
3075 isl_pw_multi_aff_free(pma2);
3079 isl_pw_multi_aff_free(pma1);
3080 isl_pw_multi_aff_free(pma2);
3082 return isl_pw_multi_aff_free(res);
3085 static __isl_give isl_pw_multi_aff *pw_multi_aff_union_lexmax(
3086 __isl_take isl_pw_multi_aff *pma1,
3087 __isl_take isl_pw_multi_aff *pma2)
3089 return pw_multi_aff_union_opt(pma1, pma2, &isl_multi_aff_lex_ge_set);
3092 /* Given two piecewise multi affine expressions, return a piecewise
3093 * multi-affine expression defined on the union of the definition domains
3094 * of the inputs that is equal to the lexicographic maximum of the two
3095 * inputs on each cell. If only one of the two inputs is defined on
3096 * a given cell, then it is considered to be the maximum.
3098 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_lexmax(
3099 __isl_take isl_pw_multi_aff *pma1,
3100 __isl_take isl_pw_multi_aff *pma2)
3102 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
3103 &pw_multi_aff_union_lexmax);
3106 static __isl_give isl_pw_multi_aff *pw_multi_aff_union_lexmin(
3107 __isl_take isl_pw_multi_aff *pma1,
3108 __isl_take isl_pw_multi_aff *pma2)
3110 return pw_multi_aff_union_opt(pma1, pma2, &isl_multi_aff_lex_le_set);
3113 /* Given two piecewise multi affine expressions, return a piecewise
3114 * multi-affine expression defined on the union of the definition domains
3115 * of the inputs that is equal to the lexicographic minimum of the two
3116 * inputs on each cell. If only one of the two inputs is defined on
3117 * a given cell, then it is considered to be the minimum.
3119 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_lexmin(
3120 __isl_take isl_pw_multi_aff *pma1,
3121 __isl_take isl_pw_multi_aff *pma2)
3123 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
3124 &pw_multi_aff_union_lexmin);
3127 static __isl_give isl_pw_multi_aff *pw_multi_aff_add(
3128 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
3130 return isl_pw_multi_aff_on_shared_domain(pma1, pma2,
3131 &isl_multi_aff_add);
3134 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_add(
3135 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
3137 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
3141 static __isl_give isl_pw_multi_aff *pw_multi_aff_sub(
3142 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
3144 return isl_pw_multi_aff_on_shared_domain(pma1, pma2,
3145 &isl_multi_aff_sub);
3148 /* Subtract "pma2" from "pma1" and return the result.
3150 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_sub(
3151 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
3153 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
3157 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_add(
3158 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
3160 return isl_pw_multi_aff_union_add_(pma1, pma2);
3163 /* Given two piecewise multi-affine expressions A -> B and C -> D,
3164 * construct a piecewise multi-affine expression [A -> C] -> [B -> D].
3166 static __isl_give isl_pw_multi_aff *pw_multi_aff_product(
3167 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
3171 isl_pw_multi_aff *res;
3176 n = pma1->n * pma2->n;
3177 space = isl_space_product(isl_space_copy(pma1->dim),
3178 isl_space_copy(pma2->dim));
3179 res = isl_pw_multi_aff_alloc_size(space, n);
3181 for (i = 0; i < pma1->n; ++i) {
3182 for (j = 0; j < pma2->n; ++j) {
3186 domain = isl_set_product(isl_set_copy(pma1->p[i].set),
3187 isl_set_copy(pma2->p[j].set));
3188 ma = isl_multi_aff_product(
3189 isl_multi_aff_copy(pma1->p[i].maff),
3190 isl_multi_aff_copy(pma2->p[i].maff));
3191 res = isl_pw_multi_aff_add_piece(res, domain, ma);
3195 isl_pw_multi_aff_free(pma1);
3196 isl_pw_multi_aff_free(pma2);
3199 isl_pw_multi_aff_free(pma1);
3200 isl_pw_multi_aff_free(pma2);
3204 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_product(
3205 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
3207 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
3208 &pw_multi_aff_product);
3211 /* Construct a map mapping the domain of the piecewise multi-affine expression
3212 * to its range, with each dimension in the range equated to the
3213 * corresponding affine expression on its cell.
3215 __isl_give isl_map *isl_map_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma)
3223 map = isl_map_empty(isl_pw_multi_aff_get_space(pma));
3225 for (i = 0; i < pma->n; ++i) {
3226 isl_multi_aff *maff;
3227 isl_basic_map *bmap;
3230 maff = isl_multi_aff_copy(pma->p[i].maff);
3231 bmap = isl_basic_map_from_multi_aff(maff);
3232 map_i = isl_map_from_basic_map(bmap);
3233 map_i = isl_map_intersect_domain(map_i,
3234 isl_set_copy(pma->p[i].set));
3235 map = isl_map_union_disjoint(map, map_i);
3238 isl_pw_multi_aff_free(pma);
3242 __isl_give isl_set *isl_set_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma)
3247 if (!isl_space_is_set(pma->dim))
3248 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
3249 "isl_pw_multi_aff cannot be converted into an isl_set",
3250 return isl_pw_multi_aff_free(pma));
3252 return isl_map_from_pw_multi_aff(pma);
3255 /* Given a basic map with a single output dimension that is defined
3256 * in terms of the parameters and input dimensions using an equality,
3257 * extract an isl_aff that expresses the output dimension in terms
3258 * of the parameters and input dimensions.
3260 * Since some applications expect the result of isl_pw_multi_aff_from_map
3261 * to only contain integer affine expressions, we compute the floor
3262 * of the expression before returning.
3264 * This function shares some similarities with
3265 * isl_basic_map_has_defining_equality and isl_constraint_get_bound.
3267 static __isl_give isl_aff *extract_isl_aff_from_basic_map(
3268 __isl_take isl_basic_map *bmap)
3273 isl_local_space *ls;
3278 if (isl_basic_map_dim(bmap, isl_dim_out) != 1)
3279 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
3280 "basic map should have a single output dimension",
3282 offset = isl_basic_map_offset(bmap, isl_dim_out);
3283 total = isl_basic_map_total_dim(bmap);
3284 for (i = 0; i < bmap->n_eq; ++i) {
3285 if (isl_int_is_zero(bmap->eq[i][offset]))
3287 if (isl_seq_first_non_zero(bmap->eq[i] + offset + 1,
3288 1 + total - (offset + 1)) != -1)
3292 if (i >= bmap->n_eq)
3293 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
3294 "unable to find suitable equality", goto error);
3295 ls = isl_basic_map_get_local_space(bmap);
3296 aff = isl_aff_alloc(isl_local_space_domain(ls));
3299 if (isl_int_is_neg(bmap->eq[i][offset]))
3300 isl_seq_cpy(aff->v->el + 1, bmap->eq[i], offset);
3302 isl_seq_neg(aff->v->el + 1, bmap->eq[i], offset);
3303 isl_seq_clr(aff->v->el + 1 + offset, aff->v->size - (1 + offset));
3304 isl_int_abs(aff->v->el[0], bmap->eq[i][offset]);
3305 isl_basic_map_free(bmap);
3307 aff = isl_aff_remove_unused_divs(aff);
3308 aff = isl_aff_floor(aff);
3311 isl_basic_map_free(bmap);
3315 /* Given a basic map where each output dimension is defined
3316 * in terms of the parameters and input dimensions using an equality,
3317 * extract an isl_multi_aff that expresses the output dimensions in terms
3318 * of the parameters and input dimensions.
3320 static __isl_give isl_multi_aff *extract_isl_multi_aff_from_basic_map(
3321 __isl_take isl_basic_map *bmap)
3330 ma = isl_multi_aff_alloc(isl_basic_map_get_space(bmap));
3331 n_out = isl_basic_map_dim(bmap, isl_dim_out);
3333 for (i = 0; i < n_out; ++i) {
3334 isl_basic_map *bmap_i;
3337 bmap_i = isl_basic_map_copy(bmap);
3338 bmap_i = isl_basic_map_project_out(bmap_i, isl_dim_out,
3339 i + 1, n_out - (1 + i));
3340 bmap_i = isl_basic_map_project_out(bmap_i, isl_dim_out, 0, i);
3341 aff = extract_isl_aff_from_basic_map(bmap_i);
3342 ma = isl_multi_aff_set_aff(ma, i, aff);
3345 isl_basic_map_free(bmap);
3350 /* Create an isl_pw_multi_aff that is equivalent to
3351 * isl_map_intersect_domain(isl_map_from_basic_map(bmap), domain).
3352 * The given basic map is such that each output dimension is defined
3353 * in terms of the parameters and input dimensions using an equality.
3355 static __isl_give isl_pw_multi_aff *plain_pw_multi_aff_from_map(
3356 __isl_take isl_set *domain, __isl_take isl_basic_map *bmap)
3360 ma = extract_isl_multi_aff_from_basic_map(bmap);
3361 return isl_pw_multi_aff_alloc(domain, ma);
3364 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
3365 * This obviously only works if the input "map" is single-valued.
3366 * If so, we compute the lexicographic minimum of the image in the form
3367 * of an isl_pw_multi_aff. Since the image is unique, it is equal
3368 * to its lexicographic minimum.
3369 * If the input is not single-valued, we produce an error.
3371 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_base(
3372 __isl_take isl_map *map)
3376 isl_pw_multi_aff *pma;
3378 sv = isl_map_is_single_valued(map);
3382 isl_die(isl_map_get_ctx(map), isl_error_invalid,
3383 "map is not single-valued", goto error);
3384 map = isl_map_make_disjoint(map);
3388 pma = isl_pw_multi_aff_empty(isl_map_get_space(map));
3390 for (i = 0; i < map->n; ++i) {
3391 isl_pw_multi_aff *pma_i;
3392 isl_basic_map *bmap;
3393 bmap = isl_basic_map_copy(map->p[i]);
3394 pma_i = isl_basic_map_lexmin_pw_multi_aff(bmap);
3395 pma = isl_pw_multi_aff_add_disjoint(pma, pma_i);
3405 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
3406 * taking into account that the output dimension at position "d"
3407 * can be represented as
3409 * x = floor((e(...) + c1) / m)
3411 * given that constraint "i" is of the form
3413 * e(...) + c1 - m x >= 0
3416 * Let "map" be of the form
3420 * We construct a mapping
3422 * A -> [A -> x = floor(...)]
3424 * apply that to the map, obtaining
3426 * [A -> x = floor(...)] -> B
3428 * and equate dimension "d" to x.
3429 * We then compute a isl_pw_multi_aff representation of the resulting map
3430 * and plug in the mapping above.
3432 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_div(
3433 __isl_take isl_map *map, __isl_take isl_basic_map *hull, int d, int i)
3437 isl_local_space *ls;
3445 isl_pw_multi_aff *pma;
3448 is_set = isl_map_is_set(map);
3450 offset = isl_basic_map_offset(hull, isl_dim_out);
3451 ctx = isl_map_get_ctx(map);
3452 space = isl_space_domain(isl_map_get_space(map));
3453 n_in = isl_space_dim(space, isl_dim_set);
3454 n = isl_space_dim(space, isl_dim_all);
3456 v = isl_vec_alloc(ctx, 1 + 1 + n);
3458 isl_int_neg(v->el[0], hull->ineq[i][offset + d]);
3459 isl_seq_cpy(v->el + 1, hull->ineq[i], 1 + n);
3461 isl_basic_map_free(hull);
3463 ls = isl_local_space_from_space(isl_space_copy(space));
3464 aff = isl_aff_alloc_vec(ls, v);
3465 aff = isl_aff_floor(aff);
3467 isl_space_free(space);
3468 ma = isl_multi_aff_from_aff(aff);
3470 ma = isl_multi_aff_identity(isl_space_map_from_set(space));
3471 ma = isl_multi_aff_range_product(ma,
3472 isl_multi_aff_from_aff(aff));
3475 insert = isl_map_from_multi_aff(isl_multi_aff_copy(ma));
3476 map = isl_map_apply_domain(map, insert);
3477 map = isl_map_equate(map, isl_dim_in, n_in, isl_dim_out, d);
3478 pma = isl_pw_multi_aff_from_map(map);
3479 pma = isl_pw_multi_aff_pullback_multi_aff(pma, ma);
3484 /* Is constraint "c" of the form
3486 * e(...) + c1 - m x >= 0
3490 * -e(...) + c2 + m x >= 0
3492 * where m > 1 and e only depends on parameters and input dimemnsions?
3494 * "offset" is the offset of the output dimensions
3495 * "pos" is the position of output dimension x.
3497 static int is_potential_div_constraint(isl_int *c, int offset, int d, int total)
3499 if (isl_int_is_zero(c[offset + d]))
3501 if (isl_int_is_one(c[offset + d]))
3503 if (isl_int_is_negone(c[offset + d]))
3505 if (isl_seq_first_non_zero(c + offset, d) != -1)
3507 if (isl_seq_first_non_zero(c + offset + d + 1,
3508 total - (offset + d + 1)) != -1)
3513 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
3515 * As a special case, we first check if there is any pair of constraints,
3516 * shared by all the basic maps in "map" that force a given dimension
3517 * to be equal to the floor of some affine combination of the input dimensions.
3519 * In particular, if we can find two constraints
3521 * e(...) + c1 - m x >= 0 i.e., m x <= e(...) + c1
3525 * -e(...) + c2 + m x >= 0 i.e., m x >= e(...) - c2
3527 * where m > 1 and e only depends on parameters and input dimemnsions,
3530 * c1 + c2 < m i.e., -c2 >= c1 - (m - 1)
3532 * then we know that we can take
3534 * x = floor((e(...) + c1) / m)
3536 * without having to perform any computation.
3538 * Note that we know that
3542 * If c1 + c2 were 0, then we would have detected an equality during
3543 * simplification. If c1 + c2 were negative, then we would have detected
3546 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_check_div(
3547 __isl_take isl_map *map)
3553 isl_basic_map *hull;
3555 hull = isl_map_unshifted_simple_hull(isl_map_copy(map));
3560 dim = isl_map_dim(map, isl_dim_out);
3561 offset = isl_basic_map_offset(hull, isl_dim_out);
3562 total = 1 + isl_basic_map_total_dim(hull);
3564 for (d = 0; d < dim; ++d) {
3565 for (i = 0; i < n; ++i) {
3566 if (!is_potential_div_constraint(hull->ineq[i],
3569 for (j = i + 1; j < n; ++j) {
3570 if (!isl_seq_is_neg(hull->ineq[i] + 1,
3571 hull->ineq[j] + 1, total - 1))
3573 isl_int_add(sum, hull->ineq[i][0],
3575 if (isl_int_abs_lt(sum,
3576 hull->ineq[i][offset + d]))
3583 if (isl_int_is_pos(hull->ineq[j][offset + d]))
3585 return pw_multi_aff_from_map_div(map, hull, d, j);
3589 isl_basic_map_free(hull);
3590 return pw_multi_aff_from_map_base(map);
3593 isl_basic_map_free(hull);
3597 /* Given an affine expression
3599 * [A -> B] -> f(A,B)
3601 * construct an isl_multi_aff
3605 * such that dimension "d" in B' is set to "aff" and the remaining
3606 * dimensions are set equal to the corresponding dimensions in B.
3607 * "n_in" is the dimension of the space A.
3608 * "n_out" is the dimension of the space B.
3610 * If "is_set" is set, then the affine expression is of the form
3614 * and we construct an isl_multi_aff
3618 static __isl_give isl_multi_aff *range_map(__isl_take isl_aff *aff, int d,
3619 unsigned n_in, unsigned n_out, int is_set)
3623 isl_space *space, *space2;
3624 isl_local_space *ls;
3626 space = isl_aff_get_domain_space(aff);
3627 ls = isl_local_space_from_space(isl_space_copy(space));
3628 space2 = isl_space_copy(space);
3630 space2 = isl_space_range(isl_space_unwrap(space2));
3631 space = isl_space_map_from_domain_and_range(space, space2);
3632 ma = isl_multi_aff_alloc(space);
3633 ma = isl_multi_aff_set_aff(ma, d, aff);
3635 for (i = 0; i < n_out; ++i) {
3638 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
3639 isl_dim_set, n_in + i);
3640 ma = isl_multi_aff_set_aff(ma, i, aff);
3643 isl_local_space_free(ls);
3648 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
3649 * taking into account that the dimension at position "d" can be written as
3651 * x = m a + f(..) (1)
3653 * where m is equal to "gcd".
3654 * "i" is the index of the equality in "hull" that defines f(..).
3655 * In particular, the equality is of the form
3657 * f(..) - x + m g(existentials) = 0
3661 * -f(..) + x + m g(existentials) = 0
3663 * We basically plug (1) into "map", resulting in a map with "a"
3664 * in the range instead of "x". The corresponding isl_pw_multi_aff
3665 * defining "a" is then plugged back into (1) to obtain a definition fro "x".
3667 * Specifically, given the input map
3671 * We first wrap it into a set
3675 * and define (1) on top of the corresponding space, resulting in "aff".
3676 * We use this to create an isl_multi_aff that maps the output position "d"
3677 * from "a" to "x", leaving all other (intput and output) dimensions unchanged.
3678 * We plug this into the wrapped map, unwrap the result and compute the
3679 * corresponding isl_pw_multi_aff.
3680 * The result is an expression
3688 * so that we can plug that into "aff", after extending the latter to
3694 * If "map" is actually a set, then there is no "A" space, meaning
3695 * that we do not need to perform any wrapping, and that the result
3696 * of the recursive call is of the form
3700 * which is plugged into a mapping of the form
3704 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_stride(
3705 __isl_take isl_map *map, __isl_take isl_basic_map *hull, int d, int i,
3710 isl_local_space *ls;
3713 isl_pw_multi_aff *pma, *id;
3719 is_set = isl_map_is_set(map);
3721 n_in = isl_basic_map_dim(hull, isl_dim_in);
3722 n_out = isl_basic_map_dim(hull, isl_dim_out);
3723 o_out = isl_basic_map_offset(hull, isl_dim_out);
3728 set = isl_map_wrap(map);
3729 space = isl_space_map_from_set(isl_set_get_space(set));
3730 ma = isl_multi_aff_identity(space);
3731 ls = isl_local_space_from_space(isl_set_get_space(set));
3732 aff = isl_aff_alloc(ls);
3734 isl_int_set_si(aff->v->el[0], 1);
3735 if (isl_int_is_one(hull->eq[i][o_out + d]))
3736 isl_seq_neg(aff->v->el + 1, hull->eq[i],
3739 isl_seq_cpy(aff->v->el + 1, hull->eq[i],
3741 isl_int_set(aff->v->el[1 + o_out + d], gcd);
3743 ma = isl_multi_aff_set_aff(ma, n_in + d, isl_aff_copy(aff));
3744 set = isl_set_preimage_multi_aff(set, ma);
3746 ma = range_map(aff, d, n_in, n_out, is_set);
3751 map = isl_set_unwrap(set);
3752 pma = isl_pw_multi_aff_from_map(set);
3755 space = isl_pw_multi_aff_get_domain_space(pma);
3756 space = isl_space_map_from_set(space);
3757 id = isl_pw_multi_aff_identity(space);
3758 pma = isl_pw_multi_aff_range_product(id, pma);
3760 id = isl_pw_multi_aff_from_multi_aff(ma);
3761 pma = isl_pw_multi_aff_pullback_pw_multi_aff(id, pma);
3763 isl_basic_map_free(hull);
3767 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
3769 * As a special case, we first check if all output dimensions are uniquely
3770 * defined in terms of the parameters and input dimensions over the entire
3771 * domain. If so, we extract the desired isl_pw_multi_aff directly
3772 * from the affine hull of "map" and its domain.
3774 * Otherwise, we check if any of the output dimensions is "strided".
3775 * That is, we check if can be written as
3779 * with m greater than 1, a some combination of existentiall quantified
3780 * variables and f and expression in the parameters and input dimensions.
3781 * If so, we remove the stride in pw_multi_aff_from_map_stride.
3783 * Otherwise, we continue with pw_multi_aff_from_map_check_div for a further
3786 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_map(__isl_take isl_map *map)
3790 isl_basic_map *hull;
3800 hull = isl_map_affine_hull(isl_map_copy(map));
3801 sv = isl_basic_map_plain_is_single_valued(hull);
3803 return plain_pw_multi_aff_from_map(isl_map_domain(map), hull);
3805 hull = isl_basic_map_free(hull);
3809 n_div = isl_basic_map_dim(hull, isl_dim_div);
3810 o_div = isl_basic_map_offset(hull, isl_dim_div);
3813 isl_basic_map_free(hull);
3814 return pw_multi_aff_from_map_check_div(map);
3819 n_out = isl_basic_map_dim(hull, isl_dim_out);
3820 o_out = isl_basic_map_offset(hull, isl_dim_out);
3822 for (i = 0; i < n_out; ++i) {
3823 for (j = 0; j < hull->n_eq; ++j) {
3824 isl_int *eq = hull->eq[j];
3825 isl_pw_multi_aff *res;
3827 if (!isl_int_is_one(eq[o_out + i]) &&
3828 !isl_int_is_negone(eq[o_out + i]))
3830 if (isl_seq_first_non_zero(eq + o_out, i) != -1)
3832 if (isl_seq_first_non_zero(eq + o_out + i + 1,
3833 n_out - (i + 1)) != -1)
3835 isl_seq_gcd(eq + o_div, n_div, &gcd);
3836 if (isl_int_is_zero(gcd))
3838 if (isl_int_is_one(gcd))
3841 res = pw_multi_aff_from_map_stride(map, hull,
3849 isl_basic_map_free(hull);
3850 return pw_multi_aff_from_map_check_div(map);
3856 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_set(__isl_take isl_set *set)
3858 return isl_pw_multi_aff_from_map(set);
3861 /* Convert "map" into an isl_pw_multi_aff (if possible) and
3864 static int pw_multi_aff_from_map(__isl_take isl_map *map, void *user)
3866 isl_union_pw_multi_aff **upma = user;
3867 isl_pw_multi_aff *pma;
3869 pma = isl_pw_multi_aff_from_map(map);
3870 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
3872 return *upma ? 0 : -1;
3875 /* Try and create an isl_union_pw_multi_aff that is equivalent
3876 * to the given isl_union_map.
3877 * The isl_union_map is required to be single-valued in each space.
3878 * Otherwise, an error is produced.
3880 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_map(
3881 __isl_take isl_union_map *umap)
3884 isl_union_pw_multi_aff *upma;
3886 space = isl_union_map_get_space(umap);
3887 upma = isl_union_pw_multi_aff_empty(space);
3888 if (isl_union_map_foreach_map(umap, &pw_multi_aff_from_map, &upma) < 0)
3889 upma = isl_union_pw_multi_aff_free(upma);
3890 isl_union_map_free(umap);
3895 /* Try and create an isl_union_pw_multi_aff that is equivalent
3896 * to the given isl_union_set.
3897 * The isl_union_set is required to be a singleton in each space.
3898 * Otherwise, an error is produced.
3900 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_set(
3901 __isl_take isl_union_set *uset)
3903 return isl_union_pw_multi_aff_from_union_map(uset);
3906 /* Return the piecewise affine expression "set ? 1 : 0".
3908 __isl_give isl_pw_aff *isl_set_indicator_function(__isl_take isl_set *set)
3911 isl_space *space = isl_set_get_space(set);
3912 isl_local_space *ls = isl_local_space_from_space(space);
3913 isl_aff *zero = isl_aff_zero_on_domain(isl_local_space_copy(ls));
3914 isl_aff *one = isl_aff_zero_on_domain(ls);
3916 one = isl_aff_add_constant_si(one, 1);
3917 pa = isl_pw_aff_alloc(isl_set_copy(set), one);
3918 set = isl_set_complement(set);
3919 pa = isl_pw_aff_add_disjoint(pa, isl_pw_aff_alloc(set, zero));
3924 /* Plug in "subs" for dimension "type", "pos" of "aff".
3926 * Let i be the dimension to replace and let "subs" be of the form
3930 * and "aff" of the form
3936 * (a f + d g')/(m d)
3938 * where g' is the result of plugging in "subs" in each of the integer
3941 __isl_give isl_aff *isl_aff_substitute(__isl_take isl_aff *aff,
3942 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
3947 aff = isl_aff_cow(aff);
3949 return isl_aff_free(aff);
3951 ctx = isl_aff_get_ctx(aff);
3952 if (!isl_space_is_equal(aff->ls->dim, subs->ls->dim))
3953 isl_die(ctx, isl_error_invalid,
3954 "spaces don't match", return isl_aff_free(aff));
3955 if (isl_local_space_dim(subs->ls, isl_dim_div) != 0)
3956 isl_die(ctx, isl_error_unsupported,
3957 "cannot handle divs yet", return isl_aff_free(aff));
3959 aff->ls = isl_local_space_substitute(aff->ls, type, pos, subs);
3961 return isl_aff_free(aff);
3963 aff->v = isl_vec_cow(aff->v);
3965 return isl_aff_free(aff);
3967 pos += isl_local_space_offset(aff->ls, type);
3970 isl_seq_substitute(aff->v->el, pos, subs->v->el,
3971 aff->v->size, subs->v->size, v);
3977 /* Plug in "subs" for dimension "type", "pos" in each of the affine
3978 * expressions in "maff".
3980 __isl_give isl_multi_aff *isl_multi_aff_substitute(
3981 __isl_take isl_multi_aff *maff, enum isl_dim_type type, unsigned pos,
3982 __isl_keep isl_aff *subs)
3986 maff = isl_multi_aff_cow(maff);
3988 return isl_multi_aff_free(maff);
3990 if (type == isl_dim_in)
3993 for (i = 0; i < maff->n; ++i) {
3994 maff->p[i] = isl_aff_substitute(maff->p[i], type, pos, subs);
3996 return isl_multi_aff_free(maff);
4002 /* Plug in "subs" for dimension "type", "pos" of "pma".
4004 * pma is of the form
4008 * while subs is of the form
4010 * v' = B_j(v) -> S_j
4012 * Each pair i,j such that C_ij = A_i \cap B_i is non-empty
4013 * has a contribution in the result, in particular
4015 * C_ij(S_j) -> M_i(S_j)
4017 * Note that plugging in S_j in C_ij may also result in an empty set
4018 * and this contribution should simply be discarded.
4020 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_substitute(
4021 __isl_take isl_pw_multi_aff *pma, enum isl_dim_type type, unsigned pos,
4022 __isl_keep isl_pw_aff *subs)
4025 isl_pw_multi_aff *res;
4028 return isl_pw_multi_aff_free(pma);
4030 n = pma->n * subs->n;
4031 res = isl_pw_multi_aff_alloc_size(isl_space_copy(pma->dim), n);
4033 for (i = 0; i < pma->n; ++i) {
4034 for (j = 0; j < subs->n; ++j) {
4036 isl_multi_aff *res_ij;
4039 common = isl_set_intersect(
4040 isl_set_copy(pma->p[i].set),
4041 isl_set_copy(subs->p[j].set));
4042 common = isl_set_substitute(common,
4043 type, pos, subs->p[j].aff);
4044 empty = isl_set_plain_is_empty(common);
4045 if (empty < 0 || empty) {
4046 isl_set_free(common);
4052 res_ij = isl_multi_aff_substitute(
4053 isl_multi_aff_copy(pma->p[i].maff),
4054 type, pos, subs->p[j].aff);
4056 res = isl_pw_multi_aff_add_piece(res, common, res_ij);
4060 isl_pw_multi_aff_free(pma);
4063 isl_pw_multi_aff_free(pma);
4064 isl_pw_multi_aff_free(res);
4068 /* Compute the preimage of a range of dimensions in the affine expression "src"
4069 * under "ma" and put the result in "dst". The number of dimensions in "src"
4070 * that precede the range is given by "n_before". The number of dimensions
4071 * in the range is given by the number of output dimensions of "ma".
4072 * The number of dimensions that follow the range is given by "n_after".
4073 * If "has_denom" is set (to one),
4074 * then "src" and "dst" have an extra initial denominator.
4075 * "n_div_ma" is the number of existentials in "ma"
4076 * "n_div_bset" is the number of existentials in "src"
4077 * The resulting "dst" (which is assumed to have been allocated by
4078 * the caller) contains coefficients for both sets of existentials,
4079 * first those in "ma" and then those in "src".
4080 * f, c1, c2 and g are temporary objects that have been initialized
4083 * Let src represent the expression
4085 * (a(p) + f_u u + b v + f_w w + c(divs))/d
4087 * and let ma represent the expressions
4089 * v_i = (r_i(p) + s_i(y) + t_i(divs'))/m_i
4091 * We start out with the following expression for dst:
4093 * (a(p) + f_u u + 0 y + f_w w + 0 divs' + c(divs) + f \sum_i b_i v_i)/d
4095 * with the multiplication factor f initially equal to 1
4096 * and f \sum_i b_i v_i kept separately.
4097 * For each x_i that we substitute, we multiply the numerator
4098 * (and denominator) of dst by c_1 = m_i and add the numerator
4099 * of the x_i expression multiplied by c_2 = f b_i,
4100 * after removing the common factors of c_1 and c_2.
4101 * The multiplication factor f also needs to be multiplied by c_1
4102 * for the next x_j, j > i.
4104 void isl_seq_preimage(isl_int *dst, isl_int *src,
4105 __isl_keep isl_multi_aff *ma, int n_before, int n_after,
4106 int n_div_ma, int n_div_bmap,
4107 isl_int f, isl_int c1, isl_int c2, isl_int g, int has_denom)
4110 int n_param, n_in, n_out;
4113 n_param = isl_multi_aff_dim(ma, isl_dim_param);
4114 n_in = isl_multi_aff_dim(ma, isl_dim_in);
4115 n_out = isl_multi_aff_dim(ma, isl_dim_out);
4117 isl_seq_cpy(dst, src, has_denom + 1 + n_param + n_before);
4118 o_dst = o_src = has_denom + 1 + n_param + n_before;
4119 isl_seq_clr(dst + o_dst, n_in);
4122 isl_seq_cpy(dst + o_dst, src + o_src, n_after);
4125 isl_seq_clr(dst + o_dst, n_div_ma);
4127 isl_seq_cpy(dst + o_dst, src + o_src, n_div_bmap);
4129 isl_int_set_si(f, 1);
4131 for (i = 0; i < n_out; ++i) {
4132 int offset = has_denom + 1 + n_param + n_before + i;
4134 if (isl_int_is_zero(src[offset]))
4136 isl_int_set(c1, ma->p[i]->v->el[0]);
4137 isl_int_mul(c2, f, src[offset]);
4138 isl_int_gcd(g, c1, c2);
4139 isl_int_divexact(c1, c1, g);
4140 isl_int_divexact(c2, c2, g);
4142 isl_int_mul(f, f, c1);
4145 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
4146 c2, ma->p[i]->v->el + o_src, 1 + n_param);
4147 o_dst += 1 + n_param;
4148 o_src += 1 + n_param;
4149 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_before);
4151 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
4152 c2, ma->p[i]->v->el + o_src, n_in);
4155 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_after);
4157 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
4158 c2, ma->p[i]->v->el + o_src, n_div_ma);
4161 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_div_bmap);
4163 isl_int_mul(dst[0], dst[0], c1);
4167 /* Compute the pullback of "aff" by the function represented by "ma".
4168 * In other words, plug in "ma" in "aff". The result is an affine expression
4169 * defined over the domain space of "ma".
4171 * If "aff" is represented by
4173 * (a(p) + b x + c(divs))/d
4175 * and ma is represented by
4177 * x = D(p) + F(y) + G(divs')
4179 * then the result is
4181 * (a(p) + b D(p) + b F(y) + b G(divs') + c(divs))/d
4183 * The divs in the local space of the input are similarly adjusted
4184 * through a call to isl_local_space_preimage_multi_aff.
4186 __isl_give isl_aff *isl_aff_pullback_multi_aff(__isl_take isl_aff *aff,
4187 __isl_take isl_multi_aff *ma)
4189 isl_aff *res = NULL;
4190 isl_local_space *ls;
4191 int n_div_aff, n_div_ma;
4192 isl_int f, c1, c2, g;
4194 ma = isl_multi_aff_align_divs(ma);
4198 n_div_aff = isl_aff_dim(aff, isl_dim_div);
4199 n_div_ma = ma->n ? isl_aff_dim(ma->p[0], isl_dim_div) : 0;
4201 ls = isl_aff_get_domain_local_space(aff);
4202 ls = isl_local_space_preimage_multi_aff(ls, isl_multi_aff_copy(ma));
4203 res = isl_aff_alloc(ls);
4212 isl_seq_preimage(res->v->el, aff->v->el, ma, 0, 0, n_div_ma, n_div_aff,
4221 isl_multi_aff_free(ma);
4222 res = isl_aff_normalize(res);
4226 isl_multi_aff_free(ma);
4231 /* Compute the pullback of "ma1" by the function represented by "ma2".
4232 * In other words, plug in "ma2" in "ma1".
4234 __isl_give isl_multi_aff *isl_multi_aff_pullback_multi_aff(
4235 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2)
4238 isl_space *space = NULL;
4240 ma2 = isl_multi_aff_align_divs(ma2);
4241 ma1 = isl_multi_aff_cow(ma1);
4245 space = isl_space_join(isl_multi_aff_get_space(ma2),
4246 isl_multi_aff_get_space(ma1));
4248 for (i = 0; i < ma1->n; ++i) {
4249 ma1->p[i] = isl_aff_pullback_multi_aff(ma1->p[i],
4250 isl_multi_aff_copy(ma2));
4255 ma1 = isl_multi_aff_reset_space(ma1, space);
4256 isl_multi_aff_free(ma2);
4259 isl_space_free(space);
4260 isl_multi_aff_free(ma2);
4261 isl_multi_aff_free(ma1);
4265 /* Extend the local space of "dst" to include the divs
4266 * in the local space of "src".
4268 __isl_give isl_aff *isl_aff_align_divs(__isl_take isl_aff *dst,
4269 __isl_keep isl_aff *src)
4277 return isl_aff_free(dst);
4279 ctx = isl_aff_get_ctx(src);
4280 if (!isl_space_is_equal(src->ls->dim, dst->ls->dim))
4281 isl_die(ctx, isl_error_invalid,
4282 "spaces don't match", goto error);
4284 if (src->ls->div->n_row == 0)
4287 exp1 = isl_alloc_array(ctx, int, src->ls->div->n_row);
4288 exp2 = isl_alloc_array(ctx, int, dst->ls->div->n_row);
4292 div = isl_merge_divs(src->ls->div, dst->ls->div, exp1, exp2);
4293 dst = isl_aff_expand_divs(dst, div, exp2);
4301 return isl_aff_free(dst);
4304 /* Adjust the local spaces of the affine expressions in "maff"
4305 * such that they all have the save divs.
4307 __isl_give isl_multi_aff *isl_multi_aff_align_divs(
4308 __isl_take isl_multi_aff *maff)
4316 maff = isl_multi_aff_cow(maff);
4320 for (i = 1; i < maff->n; ++i)
4321 maff->p[0] = isl_aff_align_divs(maff->p[0], maff->p[i]);
4322 for (i = 1; i < maff->n; ++i) {
4323 maff->p[i] = isl_aff_align_divs(maff->p[i], maff->p[0]);
4325 return isl_multi_aff_free(maff);
4331 __isl_give isl_aff *isl_aff_lift(__isl_take isl_aff *aff)
4333 aff = isl_aff_cow(aff);
4337 aff->ls = isl_local_space_lift(aff->ls);
4339 return isl_aff_free(aff);
4344 /* Lift "maff" to a space with extra dimensions such that the result
4345 * has no more existentially quantified variables.
4346 * If "ls" is not NULL, then *ls is assigned the local space that lies
4347 * at the basis of the lifting applied to "maff".
4349 __isl_give isl_multi_aff *isl_multi_aff_lift(__isl_take isl_multi_aff *maff,
4350 __isl_give isl_local_space **ls)
4364 isl_space *space = isl_multi_aff_get_domain_space(maff);
4365 *ls = isl_local_space_from_space(space);
4367 return isl_multi_aff_free(maff);
4372 maff = isl_multi_aff_cow(maff);
4373 maff = isl_multi_aff_align_divs(maff);
4377 n_div = isl_aff_dim(maff->p[0], isl_dim_div);
4378 space = isl_multi_aff_get_space(maff);
4379 space = isl_space_lift(isl_space_domain(space), n_div);
4380 space = isl_space_extend_domain_with_range(space,
4381 isl_multi_aff_get_space(maff));
4383 return isl_multi_aff_free(maff);
4384 isl_space_free(maff->space);
4385 maff->space = space;
4388 *ls = isl_aff_get_domain_local_space(maff->p[0]);
4390 return isl_multi_aff_free(maff);
4393 for (i = 0; i < maff->n; ++i) {
4394 maff->p[i] = isl_aff_lift(maff->p[i]);
4402 isl_local_space_free(*ls);
4403 return isl_multi_aff_free(maff);
4407 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma".
4409 __isl_give isl_pw_aff *isl_pw_multi_aff_get_pw_aff(
4410 __isl_keep isl_pw_multi_aff *pma, int pos)
4420 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
4421 if (pos < 0 || pos >= n_out)
4422 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
4423 "index out of bounds", return NULL);
4425 space = isl_pw_multi_aff_get_space(pma);
4426 space = isl_space_drop_dims(space, isl_dim_out,
4427 pos + 1, n_out - pos - 1);
4428 space = isl_space_drop_dims(space, isl_dim_out, 0, pos);
4430 pa = isl_pw_aff_alloc_size(space, pma->n);
4431 for (i = 0; i < pma->n; ++i) {
4433 aff = isl_multi_aff_get_aff(pma->p[i].maff, pos);
4434 pa = isl_pw_aff_add_piece(pa, isl_set_copy(pma->p[i].set), aff);
4440 /* Return an isl_pw_multi_aff with the given "set" as domain and
4441 * an unnamed zero-dimensional range.
4443 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_domain(
4444 __isl_take isl_set *set)
4449 space = isl_set_get_space(set);
4450 space = isl_space_from_domain(space);
4451 ma = isl_multi_aff_zero(space);
4452 return isl_pw_multi_aff_alloc(set, ma);
4455 /* Add an isl_pw_multi_aff with the given "set" as domain and
4456 * an unnamed zero-dimensional range to *user.
4458 static int add_pw_multi_aff_from_domain(__isl_take isl_set *set, void *user)
4460 isl_union_pw_multi_aff **upma = user;
4461 isl_pw_multi_aff *pma;
4463 pma = isl_pw_multi_aff_from_domain(set);
4464 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
4469 /* Return an isl_union_pw_multi_aff with the given "uset" as domain and
4470 * an unnamed zero-dimensional range.
4472 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_domain(
4473 __isl_take isl_union_set *uset)
4476 isl_union_pw_multi_aff *upma;
4481 space = isl_union_set_get_space(uset);
4482 upma = isl_union_pw_multi_aff_empty(space);
4484 if (isl_union_set_foreach_set(uset,
4485 &add_pw_multi_aff_from_domain, &upma) < 0)
4488 isl_union_set_free(uset);
4491 isl_union_set_free(uset);
4492 isl_union_pw_multi_aff_free(upma);
4496 /* Convert "pma" to an isl_map and add it to *umap.
4498 static int map_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma, void *user)
4500 isl_union_map **umap = user;
4503 map = isl_map_from_pw_multi_aff(pma);
4504 *umap = isl_union_map_add_map(*umap, map);
4509 /* Construct a union map mapping the domain of the union
4510 * piecewise multi-affine expression to its range, with each dimension
4511 * in the range equated to the corresponding affine expression on its cell.
4513 __isl_give isl_union_map *isl_union_map_from_union_pw_multi_aff(
4514 __isl_take isl_union_pw_multi_aff *upma)
4517 isl_union_map *umap;
4522 space = isl_union_pw_multi_aff_get_space(upma);
4523 umap = isl_union_map_empty(space);
4525 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
4526 &map_from_pw_multi_aff, &umap) < 0)
4529 isl_union_pw_multi_aff_free(upma);
4532 isl_union_pw_multi_aff_free(upma);
4533 isl_union_map_free(umap);
4537 /* Local data for bin_entry and the callback "fn".
4539 struct isl_union_pw_multi_aff_bin_data {
4540 isl_union_pw_multi_aff *upma2;
4541 isl_union_pw_multi_aff *res;
4542 isl_pw_multi_aff *pma;
4543 int (*fn)(void **entry, void *user);
4546 /* Given an isl_pw_multi_aff from upma1, store it in data->pma
4547 * and call data->fn for each isl_pw_multi_aff in data->upma2.
4549 static int bin_entry(void **entry, void *user)
4551 struct isl_union_pw_multi_aff_bin_data *data = user;
4552 isl_pw_multi_aff *pma = *entry;
4555 if (isl_hash_table_foreach(data->upma2->dim->ctx, &data->upma2->table,
4556 data->fn, data) < 0)
4562 /* Call "fn" on each pair of isl_pw_multi_affs in "upma1" and "upma2".
4563 * The isl_pw_multi_aff from upma1 is stored in data->pma (where data is
4564 * passed as user field) and the isl_pw_multi_aff from upma2 is available
4565 * as *entry. The callback should adjust data->res if desired.
4567 static __isl_give isl_union_pw_multi_aff *bin_op(
4568 __isl_take isl_union_pw_multi_aff *upma1,
4569 __isl_take isl_union_pw_multi_aff *upma2,
4570 int (*fn)(void **entry, void *user))
4573 struct isl_union_pw_multi_aff_bin_data data = { NULL, NULL, NULL, fn };
4575 space = isl_union_pw_multi_aff_get_space(upma2);
4576 upma1 = isl_union_pw_multi_aff_align_params(upma1, space);
4577 space = isl_union_pw_multi_aff_get_space(upma1);
4578 upma2 = isl_union_pw_multi_aff_align_params(upma2, space);
4580 if (!upma1 || !upma2)
4584 data.res = isl_union_pw_multi_aff_alloc(isl_space_copy(upma1->dim),
4586 if (isl_hash_table_foreach(upma1->dim->ctx, &upma1->table,
4587 &bin_entry, &data) < 0)
4590 isl_union_pw_multi_aff_free(upma1);
4591 isl_union_pw_multi_aff_free(upma2);
4594 isl_union_pw_multi_aff_free(upma1);
4595 isl_union_pw_multi_aff_free(upma2);
4596 isl_union_pw_multi_aff_free(data.res);
4600 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
4601 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
4603 static __isl_give isl_pw_multi_aff *pw_multi_aff_range_product(
4604 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4608 space = isl_space_range_product(isl_pw_multi_aff_get_space(pma1),
4609 isl_pw_multi_aff_get_space(pma2));
4610 return isl_pw_multi_aff_on_shared_domain_in(pma1, pma2, space,
4611 &isl_multi_aff_range_product);
4614 /* Given two isl_pw_multi_affs A -> B and C -> D,
4615 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
4617 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_range_product(
4618 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4620 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4621 &pw_multi_aff_range_product);
4624 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
4625 * construct an isl_pw_multi_aff (A * C) -> (B, D).
4627 static __isl_give isl_pw_multi_aff *pw_multi_aff_flat_range_product(
4628 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4632 space = isl_space_range_product(isl_pw_multi_aff_get_space(pma1),
4633 isl_pw_multi_aff_get_space(pma2));
4634 space = isl_space_flatten_range(space);
4635 return isl_pw_multi_aff_on_shared_domain_in(pma1, pma2, space,
4636 &isl_multi_aff_flat_range_product);
4639 /* Given two isl_pw_multi_affs A -> B and C -> D,
4640 * construct an isl_pw_multi_aff (A * C) -> (B, D).
4642 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_flat_range_product(
4643 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4645 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4646 &pw_multi_aff_flat_range_product);
4649 /* If data->pma and *entry have the same domain space, then compute
4650 * their flat range product and the result to data->res.
4652 static int flat_range_product_entry(void **entry, void *user)
4654 struct isl_union_pw_multi_aff_bin_data *data = user;
4655 isl_pw_multi_aff *pma2 = *entry;
4657 if (!isl_space_tuple_match(data->pma->dim, isl_dim_in,
4658 pma2->dim, isl_dim_in))
4661 pma2 = isl_pw_multi_aff_flat_range_product(
4662 isl_pw_multi_aff_copy(data->pma),
4663 isl_pw_multi_aff_copy(pma2));
4665 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma2);
4670 /* Given two isl_union_pw_multi_affs A -> B and C -> D,
4671 * construct an isl_union_pw_multi_aff (A * C) -> (B, D).
4673 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_flat_range_product(
4674 __isl_take isl_union_pw_multi_aff *upma1,
4675 __isl_take isl_union_pw_multi_aff *upma2)
4677 return bin_op(upma1, upma2, &flat_range_product_entry);
4680 /* Replace the affine expressions at position "pos" in "pma" by "pa".
4681 * The parameters are assumed to have been aligned.
4683 * The implementation essentially performs an isl_pw_*_on_shared_domain,
4684 * except that it works on two different isl_pw_* types.
4686 static __isl_give isl_pw_multi_aff *pw_multi_aff_set_pw_aff(
4687 __isl_take isl_pw_multi_aff *pma, unsigned pos,
4688 __isl_take isl_pw_aff *pa)
4691 isl_pw_multi_aff *res = NULL;
4696 if (!isl_space_tuple_match(pma->dim, isl_dim_in, pa->dim, isl_dim_in))
4697 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
4698 "domains don't match", goto error);
4699 if (pos >= isl_pw_multi_aff_dim(pma, isl_dim_out))
4700 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
4701 "index out of bounds", goto error);
4704 res = isl_pw_multi_aff_alloc_size(isl_pw_multi_aff_get_space(pma), n);
4706 for (i = 0; i < pma->n; ++i) {
4707 for (j = 0; j < pa->n; ++j) {
4709 isl_multi_aff *res_ij;
4712 common = isl_set_intersect(isl_set_copy(pma->p[i].set),
4713 isl_set_copy(pa->p[j].set));
4714 empty = isl_set_plain_is_empty(common);
4715 if (empty < 0 || empty) {
4716 isl_set_free(common);
4722 res_ij = isl_multi_aff_set_aff(
4723 isl_multi_aff_copy(pma->p[i].maff), pos,
4724 isl_aff_copy(pa->p[j].aff));
4725 res_ij = isl_multi_aff_gist(res_ij,
4726 isl_set_copy(common));
4728 res = isl_pw_multi_aff_add_piece(res, common, res_ij);
4732 isl_pw_multi_aff_free(pma);
4733 isl_pw_aff_free(pa);
4736 isl_pw_multi_aff_free(pma);
4737 isl_pw_aff_free(pa);
4738 return isl_pw_multi_aff_free(res);
4741 /* Replace the affine expressions at position "pos" in "pma" by "pa".
4743 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_set_pw_aff(
4744 __isl_take isl_pw_multi_aff *pma, unsigned pos,
4745 __isl_take isl_pw_aff *pa)
4749 if (isl_space_match(pma->dim, isl_dim_param, pa->dim, isl_dim_param))
4750 return pw_multi_aff_set_pw_aff(pma, pos, pa);
4751 if (!isl_space_has_named_params(pma->dim) ||
4752 !isl_space_has_named_params(pa->dim))
4753 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
4754 "unaligned unnamed parameters", goto error);
4755 pma = isl_pw_multi_aff_align_params(pma, isl_pw_aff_get_space(pa));
4756 pa = isl_pw_aff_align_params(pa, isl_pw_multi_aff_get_space(pma));
4757 return pw_multi_aff_set_pw_aff(pma, pos, pa);
4759 isl_pw_multi_aff_free(pma);
4760 isl_pw_aff_free(pa);
4764 /* Check that the domain space of "pa" matches "space".
4766 * Return 0 on success and -1 on error.
4768 int isl_pw_aff_check_match_domain_space(__isl_keep isl_pw_aff *pa,
4769 __isl_keep isl_space *space)
4771 isl_space *pa_space;
4777 pa_space = isl_pw_aff_get_space(pa);
4779 match = isl_space_match(space, isl_dim_param, pa_space, isl_dim_param);
4783 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
4784 "parameters don't match", goto error);
4785 match = isl_space_tuple_match(space, isl_dim_in, pa_space, isl_dim_in);
4789 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
4790 "domains don't match", goto error);
4791 isl_space_free(pa_space);
4794 isl_space_free(pa_space);
4801 #include <isl_multi_templ.c>
4803 /* Scale the first elements of "ma" by the corresponding elements of "vec".
4805 __isl_give isl_multi_aff *isl_multi_aff_scale_vec(__isl_take isl_multi_aff *ma,
4806 __isl_take isl_vec *vec)
4814 n = isl_multi_aff_dim(ma, isl_dim_out);
4815 if (isl_vec_size(vec) < n)
4816 n = isl_vec_size(vec);
4819 for (i = 0; i < n; ++i) {
4822 isl_vec_get_element(vec, i, &v);
4824 aff = isl_multi_aff_get_aff(ma, i);
4825 aff = isl_aff_scale(aff, v);
4826 ma = isl_multi_aff_set_aff(ma, i, aff);
4834 isl_multi_aff_free(ma);
4838 /* Scale the first elements of "pma" by the corresponding elements of "vec".
4840 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_scale_vec(
4841 __isl_take isl_pw_multi_aff *pma, __isl_take isl_vec *v)
4845 pma = isl_pw_multi_aff_cow(pma);
4849 for (i = 0; i < pma->n; ++i) {
4850 pma->p[i].maff = isl_multi_aff_scale_vec(pma->p[i].maff,
4852 if (!pma->p[i].maff)
4860 isl_pw_multi_aff_free(pma);
4864 /* This function is called for each entry of an isl_union_pw_multi_aff.
4865 * Replace the entry by the result of applying isl_pw_multi_aff_scale_vec
4866 * to the original entry with the isl_vec in "user" as extra argument.
4868 static int union_pw_multi_aff_scale_vec_entry(void **entry, void *user)
4870 isl_pw_multi_aff **pma = (isl_pw_multi_aff **) entry;
4873 *pma = isl_pw_multi_aff_scale_vec(*pma, isl_vec_copy(v));
4880 /* Scale the first elements of "upma" by the corresponding elements of "vec".
4882 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_scale_vec(
4883 __isl_take isl_union_pw_multi_aff *upma, __isl_take isl_vec *v)
4885 upma = isl_union_pw_multi_aff_cow(upma);
4889 if (isl_hash_table_foreach(upma->dim->ctx, &upma->table,
4890 &union_pw_multi_aff_scale_vec_entry, v) < 0)
4897 isl_union_pw_multi_aff_free(upma);