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 /* Add "v" to the constant term of "aff".
540 __isl_give isl_aff *isl_aff_add_constant_val(__isl_take isl_aff *aff,
541 __isl_take isl_val *v)
546 if (isl_val_is_zero(v)) {
551 if (!isl_val_is_rat(v))
552 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
553 "expecting rational value", goto error);
555 aff = isl_aff_cow(aff);
559 aff->v = isl_vec_cow(aff->v);
563 if (isl_int_is_one(v->d)) {
564 isl_int_addmul(aff->v->el[1], aff->v->el[0], v->n);
565 } else if (isl_int_eq(aff->v->el[0], v->d)) {
566 isl_int_add(aff->v->el[1], aff->v->el[1], v->n);
567 aff->v = isl_vec_normalize(aff->v);
571 isl_seq_scale(aff->v->el + 1,
572 aff->v->el + 1, v->d, aff->v->size - 1);
573 isl_int_addmul(aff->v->el[1], aff->v->el[0], v->n);
574 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
575 aff->v = isl_vec_normalize(aff->v);
588 __isl_give isl_aff *isl_aff_add_constant_si(__isl_take isl_aff *aff, int v)
593 isl_int_set_si(t, v);
594 aff = isl_aff_add_constant(aff, t);
600 /* Add "v" to the numerator of the constant term of "aff".
602 __isl_give isl_aff *isl_aff_add_constant_num(__isl_take isl_aff *aff, isl_int v)
604 if (isl_int_is_zero(v))
607 aff = isl_aff_cow(aff);
611 aff->v = isl_vec_cow(aff->v);
613 return isl_aff_free(aff);
615 isl_int_add(aff->v->el[1], aff->v->el[1], v);
620 /* Add "v" to the numerator of the constant term of "aff".
622 __isl_give isl_aff *isl_aff_add_constant_num_si(__isl_take isl_aff *aff, int v)
630 isl_int_set_si(t, v);
631 aff = isl_aff_add_constant_num(aff, t);
637 __isl_give isl_aff *isl_aff_set_constant_si(__isl_take isl_aff *aff, int v)
639 aff = isl_aff_cow(aff);
643 aff->v = isl_vec_cow(aff->v);
645 return isl_aff_free(aff);
647 isl_int_set_si(aff->v->el[1], v);
652 __isl_give isl_aff *isl_aff_set_coefficient(__isl_take isl_aff *aff,
653 enum isl_dim_type type, int pos, isl_int v)
658 if (type == isl_dim_out)
659 isl_die(aff->v->ctx, isl_error_invalid,
660 "output/set dimension does not have a coefficient",
661 return isl_aff_free(aff));
662 if (type == isl_dim_in)
665 if (pos >= isl_local_space_dim(aff->ls, type))
666 isl_die(aff->v->ctx, isl_error_invalid,
667 "position out of bounds", return isl_aff_free(aff));
669 aff = isl_aff_cow(aff);
673 aff->v = isl_vec_cow(aff->v);
675 return isl_aff_free(aff);
677 pos += isl_local_space_offset(aff->ls, type);
678 isl_int_set(aff->v->el[1 + pos], v);
683 __isl_give isl_aff *isl_aff_set_coefficient_si(__isl_take isl_aff *aff,
684 enum isl_dim_type type, int pos, int v)
689 if (type == isl_dim_out)
690 isl_die(aff->v->ctx, isl_error_invalid,
691 "output/set dimension does not have a coefficient",
692 return isl_aff_free(aff));
693 if (type == isl_dim_in)
696 if (pos >= isl_local_space_dim(aff->ls, type))
697 isl_die(aff->v->ctx, isl_error_invalid,
698 "position out of bounds", return isl_aff_free(aff));
700 aff = isl_aff_cow(aff);
704 aff->v = isl_vec_cow(aff->v);
706 return isl_aff_free(aff);
708 pos += isl_local_space_offset(aff->ls, type);
709 isl_int_set_si(aff->v->el[1 + pos], v);
714 /* Replace the coefficient of the variable of type "type" at position "pos"
717 __isl_give isl_aff *isl_aff_set_coefficient_val(__isl_take isl_aff *aff,
718 enum isl_dim_type type, int pos, __isl_take isl_val *v)
723 if (type == isl_dim_out)
724 isl_die(aff->v->ctx, isl_error_invalid,
725 "output/set dimension does not have a coefficient",
727 if (type == isl_dim_in)
730 if (pos >= isl_local_space_dim(aff->ls, type))
731 isl_die(aff->v->ctx, isl_error_invalid,
732 "position out of bounds", goto error);
734 if (!isl_val_is_rat(v))
735 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
736 "expecting rational value", goto error);
738 pos += isl_local_space_offset(aff->ls, type);
739 if (isl_int_eq(aff->v->el[1 + pos], v->n) &&
740 isl_int_eq(aff->v->el[0], v->d)) {
745 aff = isl_aff_cow(aff);
748 aff->v = isl_vec_cow(aff->v);
752 if (isl_int_eq(aff->v->el[0], v->d)) {
753 isl_int_set(aff->v->el[1 + pos], v->n);
754 } else if (isl_int_is_one(v->d)) {
755 isl_int_mul(aff->v->el[1 + pos], aff->v->el[0], v->n);
757 isl_seq_scale(aff->v->el + 1,
758 aff->v->el + 1, v->d, aff->v->size - 1);
759 isl_int_mul(aff->v->el[1 + pos], aff->v->el[0], v->n);
760 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
761 aff->v = isl_vec_normalize(aff->v);
774 __isl_give isl_aff *isl_aff_add_coefficient(__isl_take isl_aff *aff,
775 enum isl_dim_type type, int pos, isl_int v)
780 if (type == isl_dim_out)
781 isl_die(aff->v->ctx, isl_error_invalid,
782 "output/set dimension does not have a coefficient",
783 return isl_aff_free(aff));
784 if (type == isl_dim_in)
787 if (pos >= isl_local_space_dim(aff->ls, type))
788 isl_die(aff->v->ctx, isl_error_invalid,
789 "position out of bounds", return isl_aff_free(aff));
791 aff = isl_aff_cow(aff);
795 aff->v = isl_vec_cow(aff->v);
797 return isl_aff_free(aff);
799 pos += isl_local_space_offset(aff->ls, type);
800 isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v);
805 __isl_give isl_aff *isl_aff_add_coefficient_si(__isl_take isl_aff *aff,
806 enum isl_dim_type type, int pos, int v)
811 isl_int_set_si(t, v);
812 aff = isl_aff_add_coefficient(aff, type, pos, t);
818 __isl_give isl_aff *isl_aff_get_div(__isl_keep isl_aff *aff, int pos)
823 return isl_local_space_get_div(aff->ls, pos);
826 __isl_give isl_aff *isl_aff_neg(__isl_take isl_aff *aff)
828 aff = isl_aff_cow(aff);
831 aff->v = isl_vec_cow(aff->v);
833 return isl_aff_free(aff);
835 isl_seq_neg(aff->v->el + 1, aff->v->el + 1, aff->v->size - 1);
840 /* Remove divs from the local space that do not appear in the affine
842 * We currently only remove divs at the end.
843 * Some intermediate divs may also not appear directly in the affine
844 * expression, but we would also need to check that no other divs are
845 * defined in terms of them.
847 __isl_give isl_aff *isl_aff_remove_unused_divs( __isl_take isl_aff *aff)
856 n = isl_local_space_dim(aff->ls, isl_dim_div);
857 off = isl_local_space_offset(aff->ls, isl_dim_div);
859 pos = isl_seq_last_non_zero(aff->v->el + 1 + off, n) + 1;
863 aff = isl_aff_cow(aff);
867 aff->ls = isl_local_space_drop_dims(aff->ls, isl_dim_div, pos, n - pos);
868 aff->v = isl_vec_drop_els(aff->v, 1 + off + pos, n - pos);
869 if (!aff->ls || !aff->v)
870 return isl_aff_free(aff);
875 /* Given two affine expressions "p" of length p_len (including the
876 * denominator and the constant term) and "subs" of length subs_len,
877 * plug in "subs" for the variable at position "pos".
878 * The variables of "subs" and "p" are assumed to match up to subs_len,
879 * but "p" may have additional variables.
880 * "v" is an initialized isl_int that can be used internally.
882 * In particular, if "p" represents the expression
886 * with i the variable at position "pos" and "subs" represents the expression
890 * then the result represents the expression
895 void isl_seq_substitute(isl_int *p, int pos, isl_int *subs,
896 int p_len, int subs_len, isl_int v)
898 isl_int_set(v, p[1 + pos]);
899 isl_int_set_si(p[1 + pos], 0);
900 isl_seq_combine(p + 1, subs[0], p + 1, v, subs + 1, subs_len - 1);
901 isl_seq_scale(p + subs_len, p + subs_len, subs[0], p_len - subs_len);
902 isl_int_mul(p[0], p[0], subs[0]);
905 /* Look for any divs in the aff->ls with a denominator equal to one
906 * and plug them into the affine expression and any subsequent divs
907 * that may reference the div.
909 static __isl_give isl_aff *plug_in_integral_divs(__isl_take isl_aff *aff)
921 n = isl_local_space_dim(aff->ls, isl_dim_div);
923 for (i = 0; i < n; ++i) {
924 if (!isl_int_is_one(aff->ls->div->row[i][0]))
926 ls = isl_local_space_copy(aff->ls);
927 ls = isl_local_space_substitute_seq(ls, isl_dim_div, i,
928 aff->ls->div->row[i], len, i + 1, n - (i + 1));
929 vec = isl_vec_copy(aff->v);
930 vec = isl_vec_cow(vec);
936 pos = isl_local_space_offset(aff->ls, isl_dim_div) + i;
937 isl_seq_substitute(vec->el, pos, aff->ls->div->row[i],
942 isl_vec_free(aff->v);
944 isl_local_space_free(aff->ls);
951 isl_local_space_free(ls);
952 return isl_aff_free(aff);
955 /* Look for any divs j that appear with a unit coefficient inside
956 * the definitions of other divs i and plug them into the definitions
959 * In particular, an expression of the form
961 * floor((f(..) + floor(g(..)/n))/m)
965 * floor((n * f(..) + g(..))/(n * m))
967 * This simplification is correct because we can move the expression
968 * f(..) into the inner floor in the original expression to obtain
970 * floor(floor((n * f(..) + g(..))/n)/m)
972 * from which we can derive the simplified expression.
974 static __isl_give isl_aff *plug_in_unit_divs(__isl_take isl_aff *aff)
982 n = isl_local_space_dim(aff->ls, isl_dim_div);
983 off = isl_local_space_offset(aff->ls, isl_dim_div);
984 for (i = 1; i < n; ++i) {
985 for (j = 0; j < i; ++j) {
986 if (!isl_int_is_one(aff->ls->div->row[i][1 + off + j]))
988 aff->ls = isl_local_space_substitute_seq(aff->ls,
989 isl_dim_div, j, aff->ls->div->row[j],
992 return isl_aff_free(aff);
999 /* Swap divs "a" and "b" in "aff", which is assumed to be non-NULL.
1001 * Even though this function is only called on isl_affs with a single
1002 * reference, we are careful to only change aff->v and aff->ls together.
1004 static __isl_give isl_aff *swap_div(__isl_take isl_aff *aff, int a, int b)
1006 unsigned off = isl_local_space_offset(aff->ls, isl_dim_div);
1007 isl_local_space *ls;
1010 ls = isl_local_space_copy(aff->ls);
1011 ls = isl_local_space_swap_div(ls, a, b);
1012 v = isl_vec_copy(aff->v);
1017 isl_int_swap(v->el[1 + off + a], v->el[1 + off + b]);
1018 isl_vec_free(aff->v);
1020 isl_local_space_free(aff->ls);
1026 isl_local_space_free(ls);
1027 return isl_aff_free(aff);
1030 /* Merge divs "a" and "b" in "aff", which is assumed to be non-NULL.
1032 * We currently do not actually remove div "b", but simply add its
1033 * coefficient to that of "a" and then zero it out.
1035 static __isl_give isl_aff *merge_divs(__isl_take isl_aff *aff, int a, int b)
1037 unsigned off = isl_local_space_offset(aff->ls, isl_dim_div);
1039 if (isl_int_is_zero(aff->v->el[1 + off + b]))
1042 aff->v = isl_vec_cow(aff->v);
1044 return isl_aff_free(aff);
1046 isl_int_add(aff->v->el[1 + off + a],
1047 aff->v->el[1 + off + a], aff->v->el[1 + off + b]);
1048 isl_int_set_si(aff->v->el[1 + off + b], 0);
1053 /* Sort the divs in the local space of "aff" according to
1054 * the comparison function "cmp_row" in isl_local_space.c,
1055 * combining the coefficients of identical divs.
1057 * Reordering divs does not change the semantics of "aff",
1058 * so there is no need to call isl_aff_cow.
1059 * Moreover, this function is currently only called on isl_affs
1060 * with a single reference.
1062 static __isl_give isl_aff *sort_divs(__isl_take isl_aff *aff)
1070 off = isl_local_space_offset(aff->ls, isl_dim_div);
1071 n = isl_aff_dim(aff, isl_dim_div);
1072 for (i = 1; i < n; ++i) {
1073 for (j = i - 1; j >= 0; --j) {
1074 int cmp = isl_mat_cmp_div(aff->ls->div, j, j + 1);
1078 aff = merge_divs(aff, j, j + 1);
1080 aff = swap_div(aff, j, j + 1);
1089 /* Normalize the representation of "aff".
1091 * This function should only be called of "new" isl_affs, i.e.,
1092 * with only a single reference. We therefore do not need to
1093 * worry about affecting other instances.
1095 __isl_give isl_aff *isl_aff_normalize(__isl_take isl_aff *aff)
1099 aff->v = isl_vec_normalize(aff->v);
1101 return isl_aff_free(aff);
1102 aff = plug_in_integral_divs(aff);
1103 aff = plug_in_unit_divs(aff);
1104 aff = sort_divs(aff);
1105 aff = isl_aff_remove_unused_divs(aff);
1109 /* Given f, return floor(f).
1110 * If f is an integer expression, then just return f.
1111 * If f is a constant, then return the constant floor(f).
1112 * Otherwise, if f = g/m, write g = q m + r,
1113 * create a new div d = [r/m] and return the expression q + d.
1114 * The coefficients in r are taken to lie between -m/2 and m/2.
1116 __isl_give isl_aff *isl_aff_floor(__isl_take isl_aff *aff)
1126 if (isl_int_is_one(aff->v->el[0]))
1129 aff = isl_aff_cow(aff);
1133 aff->v = isl_vec_cow(aff->v);
1135 return isl_aff_free(aff);
1137 if (isl_aff_is_cst(aff)) {
1138 isl_int_fdiv_q(aff->v->el[1], aff->v->el[1], aff->v->el[0]);
1139 isl_int_set_si(aff->v->el[0], 1);
1143 div = isl_vec_copy(aff->v);
1144 div = isl_vec_cow(div);
1146 return isl_aff_free(aff);
1148 ctx = isl_aff_get_ctx(aff);
1149 isl_int_fdiv_q(aff->v->el[0], aff->v->el[0], ctx->two);
1150 for (i = 1; i < aff->v->size; ++i) {
1151 isl_int_fdiv_r(div->el[i], div->el[i], div->el[0]);
1152 isl_int_fdiv_q(aff->v->el[i], aff->v->el[i], div->el[0]);
1153 if (isl_int_gt(div->el[i], aff->v->el[0])) {
1154 isl_int_sub(div->el[i], div->el[i], div->el[0]);
1155 isl_int_add_ui(aff->v->el[i], aff->v->el[i], 1);
1159 aff->ls = isl_local_space_add_div(aff->ls, div);
1161 return isl_aff_free(aff);
1163 size = aff->v->size;
1164 aff->v = isl_vec_extend(aff->v, size + 1);
1166 return isl_aff_free(aff);
1167 isl_int_set_si(aff->v->el[0], 1);
1168 isl_int_set_si(aff->v->el[size], 1);
1170 aff = isl_aff_normalize(aff);
1177 * aff mod m = aff - m * floor(aff/m)
1179 __isl_give isl_aff *isl_aff_mod(__isl_take isl_aff *aff, isl_int m)
1183 res = isl_aff_copy(aff);
1184 aff = isl_aff_scale_down(aff, m);
1185 aff = isl_aff_floor(aff);
1186 aff = isl_aff_scale(aff, m);
1187 res = isl_aff_sub(res, aff);
1194 * pwaff mod m = pwaff - m * floor(pwaff/m)
1196 __isl_give isl_pw_aff *isl_pw_aff_mod(__isl_take isl_pw_aff *pwaff, isl_int m)
1200 res = isl_pw_aff_copy(pwaff);
1201 pwaff = isl_pw_aff_scale_down(pwaff, m);
1202 pwaff = isl_pw_aff_floor(pwaff);
1203 pwaff = isl_pw_aff_scale(pwaff, m);
1204 res = isl_pw_aff_sub(res, pwaff);
1209 /* Given f, return ceil(f).
1210 * If f is an integer expression, then just return f.
1211 * Otherwise, let f be the expression
1217 * floor((e + m - 1)/m)
1219 __isl_give isl_aff *isl_aff_ceil(__isl_take isl_aff *aff)
1224 if (isl_int_is_one(aff->v->el[0]))
1227 aff = isl_aff_cow(aff);
1230 aff->v = isl_vec_cow(aff->v);
1232 return isl_aff_free(aff);
1234 isl_int_add(aff->v->el[1], aff->v->el[1], aff->v->el[0]);
1235 isl_int_sub_ui(aff->v->el[1], aff->v->el[1], 1);
1236 aff = isl_aff_floor(aff);
1241 /* Apply the expansion computed by isl_merge_divs.
1242 * The expansion itself is given by "exp" while the resulting
1243 * list of divs is given by "div".
1245 __isl_give isl_aff *isl_aff_expand_divs( __isl_take isl_aff *aff,
1246 __isl_take isl_mat *div, int *exp)
1253 aff = isl_aff_cow(aff);
1257 old_n_div = isl_local_space_dim(aff->ls, isl_dim_div);
1258 new_n_div = isl_mat_rows(div);
1259 if (new_n_div < old_n_div)
1260 isl_die(isl_mat_get_ctx(div), isl_error_invalid,
1261 "not an expansion", goto error);
1263 aff->v = isl_vec_extend(aff->v, aff->v->size + new_n_div - old_n_div);
1267 offset = 1 + isl_local_space_offset(aff->ls, isl_dim_div);
1269 for (i = new_n_div - 1; i >= 0; --i) {
1270 if (j >= 0 && exp[j] == i) {
1272 isl_int_swap(aff->v->el[offset + i],
1273 aff->v->el[offset + j]);
1276 isl_int_set_si(aff->v->el[offset + i], 0);
1279 aff->ls = isl_local_space_replace_divs(aff->ls, isl_mat_copy(div));
1290 /* Add two affine expressions that live in the same local space.
1292 static __isl_give isl_aff *add_expanded(__isl_take isl_aff *aff1,
1293 __isl_take isl_aff *aff2)
1297 aff1 = isl_aff_cow(aff1);
1301 aff1->v = isl_vec_cow(aff1->v);
1307 isl_int_gcd(gcd, aff1->v->el[0], aff2->v->el[0]);
1308 isl_int_divexact(f, aff2->v->el[0], gcd);
1309 isl_seq_scale(aff1->v->el + 1, aff1->v->el + 1, f, aff1->v->size - 1);
1310 isl_int_divexact(f, aff1->v->el[0], gcd);
1311 isl_seq_addmul(aff1->v->el + 1, f, aff2->v->el + 1, aff1->v->size - 1);
1312 isl_int_divexact(f, aff2->v->el[0], gcd);
1313 isl_int_mul(aff1->v->el[0], aff1->v->el[0], f);
1325 __isl_give isl_aff *isl_aff_add(__isl_take isl_aff *aff1,
1326 __isl_take isl_aff *aff2)
1336 ctx = isl_aff_get_ctx(aff1);
1337 if (!isl_space_is_equal(aff1->ls->dim, aff2->ls->dim))
1338 isl_die(ctx, isl_error_invalid,
1339 "spaces don't match", goto error);
1341 if (aff1->ls->div->n_row == 0 && aff2->ls->div->n_row == 0)
1342 return add_expanded(aff1, aff2);
1344 exp1 = isl_alloc_array(ctx, int, aff1->ls->div->n_row);
1345 exp2 = isl_alloc_array(ctx, int, aff2->ls->div->n_row);
1349 div = isl_merge_divs(aff1->ls->div, aff2->ls->div, exp1, exp2);
1350 aff1 = isl_aff_expand_divs(aff1, isl_mat_copy(div), exp1);
1351 aff2 = isl_aff_expand_divs(aff2, div, exp2);
1355 return add_expanded(aff1, aff2);
1364 __isl_give isl_aff *isl_aff_sub(__isl_take isl_aff *aff1,
1365 __isl_take isl_aff *aff2)
1367 return isl_aff_add(aff1, isl_aff_neg(aff2));
1370 __isl_give isl_aff *isl_aff_scale(__isl_take isl_aff *aff, isl_int f)
1374 if (isl_int_is_one(f))
1377 aff = isl_aff_cow(aff);
1380 aff->v = isl_vec_cow(aff->v);
1382 return isl_aff_free(aff);
1384 if (isl_int_is_pos(f) && isl_int_is_divisible_by(aff->v->el[0], f)) {
1385 isl_int_divexact(aff->v->el[0], aff->v->el[0], f);
1390 isl_int_gcd(gcd, aff->v->el[0], f);
1391 isl_int_divexact(aff->v->el[0], aff->v->el[0], gcd);
1392 isl_int_divexact(gcd, f, gcd);
1393 isl_seq_scale(aff->v->el + 1, aff->v->el + 1, gcd, aff->v->size - 1);
1399 __isl_give isl_aff *isl_aff_scale_down(__isl_take isl_aff *aff, isl_int f)
1403 if (isl_int_is_one(f))
1406 aff = isl_aff_cow(aff);
1410 if (isl_int_is_zero(f))
1411 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1412 "cannot scale down by zero", return isl_aff_free(aff));
1414 aff->v = isl_vec_cow(aff->v);
1416 return isl_aff_free(aff);
1419 isl_seq_gcd(aff->v->el + 1, aff->v->size - 1, &gcd);
1420 isl_int_gcd(gcd, gcd, f);
1421 isl_seq_scale_down(aff->v->el + 1, aff->v->el + 1, gcd, aff->v->size - 1);
1422 isl_int_divexact(gcd, f, gcd);
1423 isl_int_mul(aff->v->el[0], aff->v->el[0], gcd);
1429 __isl_give isl_aff *isl_aff_scale_down_ui(__isl_take isl_aff *aff, unsigned f)
1437 isl_int_set_ui(v, f);
1438 aff = isl_aff_scale_down(aff, v);
1444 __isl_give isl_aff *isl_aff_set_dim_name(__isl_take isl_aff *aff,
1445 enum isl_dim_type type, unsigned pos, const char *s)
1447 aff = isl_aff_cow(aff);
1450 if (type == isl_dim_out)
1451 isl_die(aff->v->ctx, isl_error_invalid,
1452 "cannot set name of output/set dimension",
1453 return isl_aff_free(aff));
1454 if (type == isl_dim_in)
1456 aff->ls = isl_local_space_set_dim_name(aff->ls, type, pos, s);
1458 return isl_aff_free(aff);
1463 __isl_give isl_aff *isl_aff_set_dim_id(__isl_take isl_aff *aff,
1464 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
1466 aff = isl_aff_cow(aff);
1468 return isl_id_free(id);
1469 if (type == isl_dim_out)
1470 isl_die(aff->v->ctx, isl_error_invalid,
1471 "cannot set name of output/set dimension",
1473 if (type == isl_dim_in)
1475 aff->ls = isl_local_space_set_dim_id(aff->ls, type, pos, id);
1477 return isl_aff_free(aff);
1486 /* Exploit the equalities in "eq" to simplify the affine expression
1487 * and the expressions of the integer divisions in the local space.
1488 * The integer divisions in this local space are assumed to appear
1489 * as regular dimensions in "eq".
1491 static __isl_give isl_aff *isl_aff_substitute_equalities_lifted(
1492 __isl_take isl_aff *aff, __isl_take isl_basic_set *eq)
1500 if (eq->n_eq == 0) {
1501 isl_basic_set_free(eq);
1505 aff = isl_aff_cow(aff);
1509 aff->ls = isl_local_space_substitute_equalities(aff->ls,
1510 isl_basic_set_copy(eq));
1511 aff->v = isl_vec_cow(aff->v);
1512 if (!aff->ls || !aff->v)
1515 total = 1 + isl_space_dim(eq->dim, isl_dim_all);
1517 for (i = 0; i < eq->n_eq; ++i) {
1518 j = isl_seq_last_non_zero(eq->eq[i], total + n_div);
1519 if (j < 0 || j == 0 || j >= total)
1522 isl_seq_elim(aff->v->el + 1, eq->eq[i], j, total,
1526 isl_basic_set_free(eq);
1527 aff = isl_aff_normalize(aff);
1530 isl_basic_set_free(eq);
1535 /* Exploit the equalities in "eq" to simplify the affine expression
1536 * and the expressions of the integer divisions in the local space.
1538 static __isl_give isl_aff *isl_aff_substitute_equalities(
1539 __isl_take isl_aff *aff, __isl_take isl_basic_set *eq)
1545 n_div = isl_local_space_dim(aff->ls, isl_dim_div);
1547 eq = isl_basic_set_add_dims(eq, isl_dim_set, n_div);
1548 return isl_aff_substitute_equalities_lifted(aff, eq);
1550 isl_basic_set_free(eq);
1555 /* Look for equalities among the variables shared by context and aff
1556 * and the integer divisions of aff, if any.
1557 * The equalities are then used to eliminate coefficients and/or integer
1558 * divisions from aff.
1560 __isl_give isl_aff *isl_aff_gist(__isl_take isl_aff *aff,
1561 __isl_take isl_set *context)
1563 isl_basic_set *hull;
1568 n_div = isl_local_space_dim(aff->ls, isl_dim_div);
1570 isl_basic_set *bset;
1571 isl_local_space *ls;
1572 context = isl_set_add_dims(context, isl_dim_set, n_div);
1573 ls = isl_aff_get_domain_local_space(aff);
1574 bset = isl_basic_set_from_local_space(ls);
1575 bset = isl_basic_set_lift(bset);
1576 bset = isl_basic_set_flatten(bset);
1577 context = isl_set_intersect(context,
1578 isl_set_from_basic_set(bset));
1581 hull = isl_set_affine_hull(context);
1582 return isl_aff_substitute_equalities_lifted(aff, hull);
1585 isl_set_free(context);
1589 __isl_give isl_aff *isl_aff_gist_params(__isl_take isl_aff *aff,
1590 __isl_take isl_set *context)
1592 isl_set *dom_context = isl_set_universe(isl_aff_get_domain_space(aff));
1593 dom_context = isl_set_intersect_params(dom_context, context);
1594 return isl_aff_gist(aff, dom_context);
1597 /* Return a basic set containing those elements in the space
1598 * of aff where it is non-negative.
1599 * If "rational" is set, then return a rational basic set.
1601 static __isl_give isl_basic_set *aff_nonneg_basic_set(
1602 __isl_take isl_aff *aff, int rational)
1604 isl_constraint *ineq;
1605 isl_basic_set *bset;
1607 ineq = isl_inequality_from_aff(aff);
1609 bset = isl_basic_set_from_constraint(ineq);
1611 bset = isl_basic_set_set_rational(bset);
1612 bset = isl_basic_set_simplify(bset);
1616 /* Return a basic set containing those elements in the space
1617 * of aff where it is non-negative.
1619 __isl_give isl_basic_set *isl_aff_nonneg_basic_set(__isl_take isl_aff *aff)
1621 return aff_nonneg_basic_set(aff, 0);
1624 /* Return a basic set containing those elements in the domain space
1625 * of aff where it is negative.
1627 __isl_give isl_basic_set *isl_aff_neg_basic_set(__isl_take isl_aff *aff)
1629 aff = isl_aff_neg(aff);
1630 aff = isl_aff_add_constant_num_si(aff, -1);
1631 return isl_aff_nonneg_basic_set(aff);
1634 /* Return a basic set containing those elements in the space
1635 * of aff where it is zero.
1636 * If "rational" is set, then return a rational basic set.
1638 static __isl_give isl_basic_set *aff_zero_basic_set(__isl_take isl_aff *aff,
1641 isl_constraint *ineq;
1642 isl_basic_set *bset;
1644 ineq = isl_equality_from_aff(aff);
1646 bset = isl_basic_set_from_constraint(ineq);
1648 bset = isl_basic_set_set_rational(bset);
1649 bset = isl_basic_set_simplify(bset);
1653 /* Return a basic set containing those elements in the space
1654 * of aff where it is zero.
1656 __isl_give isl_basic_set *isl_aff_zero_basic_set(__isl_take isl_aff *aff)
1658 return aff_zero_basic_set(aff, 0);
1661 /* Return a basic set containing those elements in the shared space
1662 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
1664 __isl_give isl_basic_set *isl_aff_ge_basic_set(__isl_take isl_aff *aff1,
1665 __isl_take isl_aff *aff2)
1667 aff1 = isl_aff_sub(aff1, aff2);
1669 return isl_aff_nonneg_basic_set(aff1);
1672 /* Return a basic set containing those elements in the shared space
1673 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
1675 __isl_give isl_basic_set *isl_aff_le_basic_set(__isl_take isl_aff *aff1,
1676 __isl_take isl_aff *aff2)
1678 return isl_aff_ge_basic_set(aff2, aff1);
1681 __isl_give isl_aff *isl_aff_add_on_domain(__isl_keep isl_set *dom,
1682 __isl_take isl_aff *aff1, __isl_take isl_aff *aff2)
1684 aff1 = isl_aff_add(aff1, aff2);
1685 aff1 = isl_aff_gist(aff1, isl_set_copy(dom));
1689 int isl_aff_is_empty(__isl_keep isl_aff *aff)
1697 /* Check whether the given affine expression has non-zero coefficient
1698 * for any dimension in the given range or if any of these dimensions
1699 * appear with non-zero coefficients in any of the integer divisions
1700 * involved in the affine expression.
1702 int isl_aff_involves_dims(__isl_keep isl_aff *aff,
1703 enum isl_dim_type type, unsigned first, unsigned n)
1715 ctx = isl_aff_get_ctx(aff);
1716 if (first + n > isl_aff_dim(aff, type))
1717 isl_die(ctx, isl_error_invalid,
1718 "range out of bounds", return -1);
1720 active = isl_local_space_get_active(aff->ls, aff->v->el + 2);
1724 first += isl_local_space_offset(aff->ls, type) - 1;
1725 for (i = 0; i < n; ++i)
1726 if (active[first + i]) {
1739 __isl_give isl_aff *isl_aff_drop_dims(__isl_take isl_aff *aff,
1740 enum isl_dim_type type, unsigned first, unsigned n)
1746 if (type == isl_dim_out)
1747 isl_die(aff->v->ctx, isl_error_invalid,
1748 "cannot drop output/set dimension",
1749 return isl_aff_free(aff));
1750 if (type == isl_dim_in)
1752 if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
1755 ctx = isl_aff_get_ctx(aff);
1756 if (first + n > isl_local_space_dim(aff->ls, type))
1757 isl_die(ctx, isl_error_invalid, "range out of bounds",
1758 return isl_aff_free(aff));
1760 aff = isl_aff_cow(aff);
1764 aff->ls = isl_local_space_drop_dims(aff->ls, type, first, n);
1766 return isl_aff_free(aff);
1768 first += 1 + isl_local_space_offset(aff->ls, type);
1769 aff->v = isl_vec_drop_els(aff->v, first, n);
1771 return isl_aff_free(aff);
1776 /* Project the domain of the affine expression onto its parameter space.
1777 * The affine expression may not involve any of the domain dimensions.
1779 __isl_give isl_aff *isl_aff_project_domain_on_params(__isl_take isl_aff *aff)
1785 n = isl_aff_dim(aff, isl_dim_in);
1786 involves = isl_aff_involves_dims(aff, isl_dim_in, 0, n);
1788 return isl_aff_free(aff);
1790 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1791 "affine expression involves some of the domain dimensions",
1792 return isl_aff_free(aff));
1793 aff = isl_aff_drop_dims(aff, isl_dim_in, 0, n);
1794 space = isl_aff_get_domain_space(aff);
1795 space = isl_space_params(space);
1796 aff = isl_aff_reset_domain_space(aff, space);
1800 __isl_give isl_aff *isl_aff_insert_dims(__isl_take isl_aff *aff,
1801 enum isl_dim_type type, unsigned first, unsigned n)
1807 if (type == isl_dim_out)
1808 isl_die(aff->v->ctx, isl_error_invalid,
1809 "cannot insert output/set dimensions",
1810 return isl_aff_free(aff));
1811 if (type == isl_dim_in)
1813 if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
1816 ctx = isl_aff_get_ctx(aff);
1817 if (first > isl_local_space_dim(aff->ls, type))
1818 isl_die(ctx, isl_error_invalid, "position out of bounds",
1819 return isl_aff_free(aff));
1821 aff = isl_aff_cow(aff);
1825 aff->ls = isl_local_space_insert_dims(aff->ls, type, first, n);
1827 return isl_aff_free(aff);
1829 first += 1 + isl_local_space_offset(aff->ls, type);
1830 aff->v = isl_vec_insert_zero_els(aff->v, first, n);
1832 return isl_aff_free(aff);
1837 __isl_give isl_aff *isl_aff_add_dims(__isl_take isl_aff *aff,
1838 enum isl_dim_type type, unsigned n)
1842 pos = isl_aff_dim(aff, type);
1844 return isl_aff_insert_dims(aff, type, pos, n);
1847 __isl_give isl_pw_aff *isl_pw_aff_add_dims(__isl_take isl_pw_aff *pwaff,
1848 enum isl_dim_type type, unsigned n)
1852 pos = isl_pw_aff_dim(pwaff, type);
1854 return isl_pw_aff_insert_dims(pwaff, type, pos, n);
1857 __isl_give isl_pw_aff *isl_pw_aff_from_aff(__isl_take isl_aff *aff)
1859 isl_set *dom = isl_set_universe(isl_aff_get_domain_space(aff));
1860 return isl_pw_aff_alloc(dom, aff);
1864 #define PW isl_pw_aff
1868 #define EL_IS_ZERO is_empty
1872 #define IS_ZERO is_empty
1875 #undef DEFAULT_IS_ZERO
1876 #define DEFAULT_IS_ZERO 0
1880 #define NO_MOVE_DIMS
1884 #include <isl_pw_templ.c>
1886 static __isl_give isl_set *align_params_pw_pw_set_and(
1887 __isl_take isl_pw_aff *pwaff1, __isl_take isl_pw_aff *pwaff2,
1888 __isl_give isl_set *(*fn)(__isl_take isl_pw_aff *pwaff1,
1889 __isl_take isl_pw_aff *pwaff2))
1891 if (!pwaff1 || !pwaff2)
1893 if (isl_space_match(pwaff1->dim, isl_dim_param,
1894 pwaff2->dim, isl_dim_param))
1895 return fn(pwaff1, pwaff2);
1896 if (!isl_space_has_named_params(pwaff1->dim) ||
1897 !isl_space_has_named_params(pwaff2->dim))
1898 isl_die(isl_pw_aff_get_ctx(pwaff1), isl_error_invalid,
1899 "unaligned unnamed parameters", goto error);
1900 pwaff1 = isl_pw_aff_align_params(pwaff1, isl_pw_aff_get_space(pwaff2));
1901 pwaff2 = isl_pw_aff_align_params(pwaff2, isl_pw_aff_get_space(pwaff1));
1902 return fn(pwaff1, pwaff2);
1904 isl_pw_aff_free(pwaff1);
1905 isl_pw_aff_free(pwaff2);
1909 /* Compute a piecewise quasi-affine expression with a domain that
1910 * is the union of those of pwaff1 and pwaff2 and such that on each
1911 * cell, the quasi-affine expression is the better (according to cmp)
1912 * of those of pwaff1 and pwaff2. If only one of pwaff1 or pwaff2
1913 * is defined on a given cell, then the associated expression
1914 * is the defined one.
1916 static __isl_give isl_pw_aff *pw_aff_union_opt(__isl_take isl_pw_aff *pwaff1,
1917 __isl_take isl_pw_aff *pwaff2,
1918 __isl_give isl_basic_set *(*cmp)(__isl_take isl_aff *aff1,
1919 __isl_take isl_aff *aff2))
1926 if (!pwaff1 || !pwaff2)
1929 ctx = isl_space_get_ctx(pwaff1->dim);
1930 if (!isl_space_is_equal(pwaff1->dim, pwaff2->dim))
1931 isl_die(ctx, isl_error_invalid,
1932 "arguments should live in same space", goto error);
1934 if (isl_pw_aff_is_empty(pwaff1)) {
1935 isl_pw_aff_free(pwaff1);
1939 if (isl_pw_aff_is_empty(pwaff2)) {
1940 isl_pw_aff_free(pwaff2);
1944 n = 2 * (pwaff1->n + 1) * (pwaff2->n + 1);
1945 res = isl_pw_aff_alloc_size(isl_space_copy(pwaff1->dim), n);
1947 for (i = 0; i < pwaff1->n; ++i) {
1948 set = isl_set_copy(pwaff1->p[i].set);
1949 for (j = 0; j < pwaff2->n; ++j) {
1950 struct isl_set *common;
1953 common = isl_set_intersect(
1954 isl_set_copy(pwaff1->p[i].set),
1955 isl_set_copy(pwaff2->p[j].set));
1956 better = isl_set_from_basic_set(cmp(
1957 isl_aff_copy(pwaff2->p[j].aff),
1958 isl_aff_copy(pwaff1->p[i].aff)));
1959 better = isl_set_intersect(common, better);
1960 if (isl_set_plain_is_empty(better)) {
1961 isl_set_free(better);
1964 set = isl_set_subtract(set, isl_set_copy(better));
1966 res = isl_pw_aff_add_piece(res, better,
1967 isl_aff_copy(pwaff2->p[j].aff));
1969 res = isl_pw_aff_add_piece(res, set,
1970 isl_aff_copy(pwaff1->p[i].aff));
1973 for (j = 0; j < pwaff2->n; ++j) {
1974 set = isl_set_copy(pwaff2->p[j].set);
1975 for (i = 0; i < pwaff1->n; ++i)
1976 set = isl_set_subtract(set,
1977 isl_set_copy(pwaff1->p[i].set));
1978 res = isl_pw_aff_add_piece(res, set,
1979 isl_aff_copy(pwaff2->p[j].aff));
1982 isl_pw_aff_free(pwaff1);
1983 isl_pw_aff_free(pwaff2);
1987 isl_pw_aff_free(pwaff1);
1988 isl_pw_aff_free(pwaff2);
1992 /* Compute a piecewise quasi-affine expression with a domain that
1993 * is the union of those of pwaff1 and pwaff2 and such that on each
1994 * cell, the quasi-affine expression is the maximum of those of pwaff1
1995 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
1996 * cell, then the associated expression is the defined one.
1998 static __isl_give isl_pw_aff *pw_aff_union_max(__isl_take isl_pw_aff *pwaff1,
1999 __isl_take isl_pw_aff *pwaff2)
2001 return pw_aff_union_opt(pwaff1, pwaff2, &isl_aff_ge_basic_set);
2004 __isl_give isl_pw_aff *isl_pw_aff_union_max(__isl_take isl_pw_aff *pwaff1,
2005 __isl_take isl_pw_aff *pwaff2)
2007 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2,
2011 /* Compute a piecewise quasi-affine expression with a domain that
2012 * is the union of those of pwaff1 and pwaff2 and such that on each
2013 * cell, the quasi-affine expression is the minimum of those of pwaff1
2014 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2015 * cell, then the associated expression is the defined one.
2017 static __isl_give isl_pw_aff *pw_aff_union_min(__isl_take isl_pw_aff *pwaff1,
2018 __isl_take isl_pw_aff *pwaff2)
2020 return pw_aff_union_opt(pwaff1, pwaff2, &isl_aff_le_basic_set);
2023 __isl_give isl_pw_aff *isl_pw_aff_union_min(__isl_take isl_pw_aff *pwaff1,
2024 __isl_take isl_pw_aff *pwaff2)
2026 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2,
2030 __isl_give isl_pw_aff *isl_pw_aff_union_opt(__isl_take isl_pw_aff *pwaff1,
2031 __isl_take isl_pw_aff *pwaff2, int max)
2034 return isl_pw_aff_union_max(pwaff1, pwaff2);
2036 return isl_pw_aff_union_min(pwaff1, pwaff2);
2039 /* Construct a map with as domain the domain of pwaff and
2040 * one-dimensional range corresponding to the affine expressions.
2042 static __isl_give isl_map *map_from_pw_aff(__isl_take isl_pw_aff *pwaff)
2051 dim = isl_pw_aff_get_space(pwaff);
2052 map = isl_map_empty(dim);
2054 for (i = 0; i < pwaff->n; ++i) {
2055 isl_basic_map *bmap;
2058 bmap = isl_basic_map_from_aff(isl_aff_copy(pwaff->p[i].aff));
2059 map_i = isl_map_from_basic_map(bmap);
2060 map_i = isl_map_intersect_domain(map_i,
2061 isl_set_copy(pwaff->p[i].set));
2062 map = isl_map_union_disjoint(map, map_i);
2065 isl_pw_aff_free(pwaff);
2070 /* Construct a map with as domain the domain of pwaff and
2071 * one-dimensional range corresponding to the affine expressions.
2073 __isl_give isl_map *isl_map_from_pw_aff(__isl_take isl_pw_aff *pwaff)
2077 if (isl_space_is_set(pwaff->dim))
2078 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
2079 "space of input is not a map",
2080 return isl_pw_aff_free(pwaff));
2081 return map_from_pw_aff(pwaff);
2084 /* Construct a one-dimensional set with as parameter domain
2085 * the domain of pwaff and the single set dimension
2086 * corresponding to the affine expressions.
2088 __isl_give isl_set *isl_set_from_pw_aff(__isl_take isl_pw_aff *pwaff)
2092 if (!isl_space_is_set(pwaff->dim))
2093 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
2094 "space of input is not a set",
2095 return isl_pw_aff_free(pwaff));
2096 return map_from_pw_aff(pwaff);
2099 /* Return a set containing those elements in the domain
2100 * of pwaff where it is non-negative.
2102 __isl_give isl_set *isl_pw_aff_nonneg_set(__isl_take isl_pw_aff *pwaff)
2110 set = isl_set_empty(isl_pw_aff_get_domain_space(pwaff));
2112 for (i = 0; i < pwaff->n; ++i) {
2113 isl_basic_set *bset;
2117 rational = isl_set_has_rational(pwaff->p[i].set);
2118 bset = aff_nonneg_basic_set(isl_aff_copy(pwaff->p[i].aff),
2120 set_i = isl_set_from_basic_set(bset);
2121 set_i = isl_set_intersect(set_i, isl_set_copy(pwaff->p[i].set));
2122 set = isl_set_union_disjoint(set, set_i);
2125 isl_pw_aff_free(pwaff);
2130 /* Return a set containing those elements in the domain
2131 * of pwaff where it is zero (if complement is 0) or not zero
2132 * (if complement is 1).
2134 static __isl_give isl_set *pw_aff_zero_set(__isl_take isl_pw_aff *pwaff,
2143 set = isl_set_empty(isl_pw_aff_get_domain_space(pwaff));
2145 for (i = 0; i < pwaff->n; ++i) {
2146 isl_basic_set *bset;
2147 isl_set *set_i, *zero;
2150 rational = isl_set_has_rational(pwaff->p[i].set);
2151 bset = aff_zero_basic_set(isl_aff_copy(pwaff->p[i].aff),
2153 zero = isl_set_from_basic_set(bset);
2154 set_i = isl_set_copy(pwaff->p[i].set);
2156 set_i = isl_set_subtract(set_i, zero);
2158 set_i = isl_set_intersect(set_i, zero);
2159 set = isl_set_union_disjoint(set, set_i);
2162 isl_pw_aff_free(pwaff);
2167 /* Return a set containing those elements in the domain
2168 * of pwaff where it is zero.
2170 __isl_give isl_set *isl_pw_aff_zero_set(__isl_take isl_pw_aff *pwaff)
2172 return pw_aff_zero_set(pwaff, 0);
2175 /* Return a set containing those elements in the domain
2176 * of pwaff where it is not zero.
2178 __isl_give isl_set *isl_pw_aff_non_zero_set(__isl_take isl_pw_aff *pwaff)
2180 return pw_aff_zero_set(pwaff, 1);
2183 /* Return a set containing those elements in the shared domain
2184 * of pwaff1 and pwaff2 where pwaff1 is greater than (or equal) to pwaff2.
2186 * We compute the difference on the shared domain and then construct
2187 * the set of values where this difference is non-negative.
2188 * If strict is set, we first subtract 1 from the difference.
2189 * If equal is set, we only return the elements where pwaff1 and pwaff2
2192 static __isl_give isl_set *pw_aff_gte_set(__isl_take isl_pw_aff *pwaff1,
2193 __isl_take isl_pw_aff *pwaff2, int strict, int equal)
2195 isl_set *set1, *set2;
2197 set1 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff1));
2198 set2 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff2));
2199 set1 = isl_set_intersect(set1, set2);
2200 pwaff1 = isl_pw_aff_intersect_domain(pwaff1, isl_set_copy(set1));
2201 pwaff2 = isl_pw_aff_intersect_domain(pwaff2, isl_set_copy(set1));
2202 pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_neg(pwaff2));
2205 isl_space *dim = isl_set_get_space(set1);
2207 aff = isl_aff_zero_on_domain(isl_local_space_from_space(dim));
2208 aff = isl_aff_add_constant_si(aff, -1);
2209 pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_alloc(set1, aff));
2214 return isl_pw_aff_zero_set(pwaff1);
2215 return isl_pw_aff_nonneg_set(pwaff1);
2218 /* Return a set containing those elements in the shared domain
2219 * of pwaff1 and pwaff2 where pwaff1 is equal to pwaff2.
2221 static __isl_give isl_set *pw_aff_eq_set(__isl_take isl_pw_aff *pwaff1,
2222 __isl_take isl_pw_aff *pwaff2)
2224 return pw_aff_gte_set(pwaff1, pwaff2, 0, 1);
2227 __isl_give isl_set *isl_pw_aff_eq_set(__isl_take isl_pw_aff *pwaff1,
2228 __isl_take isl_pw_aff *pwaff2)
2230 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_eq_set);
2233 /* Return a set containing those elements in the shared domain
2234 * of pwaff1 and pwaff2 where pwaff1 is greater than or equal to pwaff2.
2236 static __isl_give isl_set *pw_aff_ge_set(__isl_take isl_pw_aff *pwaff1,
2237 __isl_take isl_pw_aff *pwaff2)
2239 return pw_aff_gte_set(pwaff1, pwaff2, 0, 0);
2242 __isl_give isl_set *isl_pw_aff_ge_set(__isl_take isl_pw_aff *pwaff1,
2243 __isl_take isl_pw_aff *pwaff2)
2245 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_ge_set);
2248 /* Return a set containing those elements in the shared domain
2249 * of pwaff1 and pwaff2 where pwaff1 is strictly greater than pwaff2.
2251 static __isl_give isl_set *pw_aff_gt_set(__isl_take isl_pw_aff *pwaff1,
2252 __isl_take isl_pw_aff *pwaff2)
2254 return pw_aff_gte_set(pwaff1, pwaff2, 1, 0);
2257 __isl_give isl_set *isl_pw_aff_gt_set(__isl_take isl_pw_aff *pwaff1,
2258 __isl_take isl_pw_aff *pwaff2)
2260 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_gt_set);
2263 __isl_give isl_set *isl_pw_aff_le_set(__isl_take isl_pw_aff *pwaff1,
2264 __isl_take isl_pw_aff *pwaff2)
2266 return isl_pw_aff_ge_set(pwaff2, pwaff1);
2269 __isl_give isl_set *isl_pw_aff_lt_set(__isl_take isl_pw_aff *pwaff1,
2270 __isl_take isl_pw_aff *pwaff2)
2272 return isl_pw_aff_gt_set(pwaff2, pwaff1);
2275 /* Return a set containing those elements in the shared domain
2276 * of the elements of list1 and list2 where each element in list1
2277 * has the relation specified by "fn" with each element in list2.
2279 static __isl_give isl_set *pw_aff_list_set(__isl_take isl_pw_aff_list *list1,
2280 __isl_take isl_pw_aff_list *list2,
2281 __isl_give isl_set *(*fn)(__isl_take isl_pw_aff *pwaff1,
2282 __isl_take isl_pw_aff *pwaff2))
2288 if (!list1 || !list2)
2291 ctx = isl_pw_aff_list_get_ctx(list1);
2292 if (list1->n < 1 || list2->n < 1)
2293 isl_die(ctx, isl_error_invalid,
2294 "list should contain at least one element", goto error);
2296 set = isl_set_universe(isl_pw_aff_get_domain_space(list1->p[0]));
2297 for (i = 0; i < list1->n; ++i)
2298 for (j = 0; j < list2->n; ++j) {
2301 set_ij = fn(isl_pw_aff_copy(list1->p[i]),
2302 isl_pw_aff_copy(list2->p[j]));
2303 set = isl_set_intersect(set, set_ij);
2306 isl_pw_aff_list_free(list1);
2307 isl_pw_aff_list_free(list2);
2310 isl_pw_aff_list_free(list1);
2311 isl_pw_aff_list_free(list2);
2315 /* Return a set containing those elements in the shared domain
2316 * of the elements of list1 and list2 where each element in list1
2317 * is equal to each element in list2.
2319 __isl_give isl_set *isl_pw_aff_list_eq_set(__isl_take isl_pw_aff_list *list1,
2320 __isl_take isl_pw_aff_list *list2)
2322 return pw_aff_list_set(list1, list2, &isl_pw_aff_eq_set);
2325 __isl_give isl_set *isl_pw_aff_list_ne_set(__isl_take isl_pw_aff_list *list1,
2326 __isl_take isl_pw_aff_list *list2)
2328 return pw_aff_list_set(list1, list2, &isl_pw_aff_ne_set);
2331 /* Return a set containing those elements in the shared domain
2332 * of the elements of list1 and list2 where each element in list1
2333 * is less than or equal to each element in list2.
2335 __isl_give isl_set *isl_pw_aff_list_le_set(__isl_take isl_pw_aff_list *list1,
2336 __isl_take isl_pw_aff_list *list2)
2338 return pw_aff_list_set(list1, list2, &isl_pw_aff_le_set);
2341 __isl_give isl_set *isl_pw_aff_list_lt_set(__isl_take isl_pw_aff_list *list1,
2342 __isl_take isl_pw_aff_list *list2)
2344 return pw_aff_list_set(list1, list2, &isl_pw_aff_lt_set);
2347 __isl_give isl_set *isl_pw_aff_list_ge_set(__isl_take isl_pw_aff_list *list1,
2348 __isl_take isl_pw_aff_list *list2)
2350 return pw_aff_list_set(list1, list2, &isl_pw_aff_ge_set);
2353 __isl_give isl_set *isl_pw_aff_list_gt_set(__isl_take isl_pw_aff_list *list1,
2354 __isl_take isl_pw_aff_list *list2)
2356 return pw_aff_list_set(list1, list2, &isl_pw_aff_gt_set);
2360 /* Return a set containing those elements in the shared domain
2361 * of pwaff1 and pwaff2 where pwaff1 is not equal to pwaff2.
2363 static __isl_give isl_set *pw_aff_ne_set(__isl_take isl_pw_aff *pwaff1,
2364 __isl_take isl_pw_aff *pwaff2)
2366 isl_set *set_lt, *set_gt;
2368 set_lt = isl_pw_aff_lt_set(isl_pw_aff_copy(pwaff1),
2369 isl_pw_aff_copy(pwaff2));
2370 set_gt = isl_pw_aff_gt_set(pwaff1, pwaff2);
2371 return isl_set_union_disjoint(set_lt, set_gt);
2374 __isl_give isl_set *isl_pw_aff_ne_set(__isl_take isl_pw_aff *pwaff1,
2375 __isl_take isl_pw_aff *pwaff2)
2377 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_ne_set);
2380 __isl_give isl_pw_aff *isl_pw_aff_scale_down(__isl_take isl_pw_aff *pwaff,
2385 if (isl_int_is_one(v))
2387 if (!isl_int_is_pos(v))
2388 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
2389 "factor needs to be positive",
2390 return isl_pw_aff_free(pwaff));
2391 pwaff = isl_pw_aff_cow(pwaff);
2397 for (i = 0; i < pwaff->n; ++i) {
2398 pwaff->p[i].aff = isl_aff_scale_down(pwaff->p[i].aff, v);
2399 if (!pwaff->p[i].aff)
2400 return isl_pw_aff_free(pwaff);
2406 __isl_give isl_pw_aff *isl_pw_aff_floor(__isl_take isl_pw_aff *pwaff)
2410 pwaff = isl_pw_aff_cow(pwaff);
2416 for (i = 0; i < pwaff->n; ++i) {
2417 pwaff->p[i].aff = isl_aff_floor(pwaff->p[i].aff);
2418 if (!pwaff->p[i].aff)
2419 return isl_pw_aff_free(pwaff);
2425 __isl_give isl_pw_aff *isl_pw_aff_ceil(__isl_take isl_pw_aff *pwaff)
2429 pwaff = isl_pw_aff_cow(pwaff);
2435 for (i = 0; i < pwaff->n; ++i) {
2436 pwaff->p[i].aff = isl_aff_ceil(pwaff->p[i].aff);
2437 if (!pwaff->p[i].aff)
2438 return isl_pw_aff_free(pwaff);
2444 /* Assuming that "cond1" and "cond2" are disjoint,
2445 * return an affine expression that is equal to pwaff1 on cond1
2446 * and to pwaff2 on cond2.
2448 static __isl_give isl_pw_aff *isl_pw_aff_select(
2449 __isl_take isl_set *cond1, __isl_take isl_pw_aff *pwaff1,
2450 __isl_take isl_set *cond2, __isl_take isl_pw_aff *pwaff2)
2452 pwaff1 = isl_pw_aff_intersect_domain(pwaff1, cond1);
2453 pwaff2 = isl_pw_aff_intersect_domain(pwaff2, cond2);
2455 return isl_pw_aff_add_disjoint(pwaff1, pwaff2);
2458 /* Return an affine expression that is equal to pwaff_true for elements
2459 * where "cond" is non-zero and to pwaff_false for elements where "cond"
2461 * That is, return cond ? pwaff_true : pwaff_false;
2463 __isl_give isl_pw_aff *isl_pw_aff_cond(__isl_take isl_pw_aff *cond,
2464 __isl_take isl_pw_aff *pwaff_true, __isl_take isl_pw_aff *pwaff_false)
2466 isl_set *cond_true, *cond_false;
2468 cond_true = isl_pw_aff_non_zero_set(isl_pw_aff_copy(cond));
2469 cond_false = isl_pw_aff_zero_set(cond);
2470 return isl_pw_aff_select(cond_true, pwaff_true,
2471 cond_false, pwaff_false);
2474 int isl_aff_is_cst(__isl_keep isl_aff *aff)
2479 return isl_seq_first_non_zero(aff->v->el + 2, aff->v->size - 2) == -1;
2482 /* Check whether pwaff is a piecewise constant.
2484 int isl_pw_aff_is_cst(__isl_keep isl_pw_aff *pwaff)
2491 for (i = 0; i < pwaff->n; ++i) {
2492 int is_cst = isl_aff_is_cst(pwaff->p[i].aff);
2493 if (is_cst < 0 || !is_cst)
2500 __isl_give isl_aff *isl_aff_mul(__isl_take isl_aff *aff1,
2501 __isl_take isl_aff *aff2)
2503 if (!isl_aff_is_cst(aff2) && isl_aff_is_cst(aff1))
2504 return isl_aff_mul(aff2, aff1);
2506 if (!isl_aff_is_cst(aff2))
2507 isl_die(isl_aff_get_ctx(aff1), isl_error_invalid,
2508 "at least one affine expression should be constant",
2511 aff1 = isl_aff_cow(aff1);
2515 aff1 = isl_aff_scale(aff1, aff2->v->el[1]);
2516 aff1 = isl_aff_scale_down(aff1, aff2->v->el[0]);
2526 /* Divide "aff1" by "aff2", assuming "aff2" is a piecewise constant.
2528 __isl_give isl_aff *isl_aff_div(__isl_take isl_aff *aff1,
2529 __isl_take isl_aff *aff2)
2534 is_cst = isl_aff_is_cst(aff2);
2538 isl_die(isl_aff_get_ctx(aff2), isl_error_invalid,
2539 "second argument should be a constant", goto error);
2544 neg = isl_int_is_neg(aff2->v->el[1]);
2546 isl_int_neg(aff2->v->el[0], aff2->v->el[0]);
2547 isl_int_neg(aff2->v->el[1], aff2->v->el[1]);
2550 aff1 = isl_aff_scale(aff1, aff2->v->el[0]);
2551 aff1 = isl_aff_scale_down(aff1, aff2->v->el[1]);
2554 isl_int_neg(aff2->v->el[0], aff2->v->el[0]);
2555 isl_int_neg(aff2->v->el[1], aff2->v->el[1]);
2566 static __isl_give isl_pw_aff *pw_aff_add(__isl_take isl_pw_aff *pwaff1,
2567 __isl_take isl_pw_aff *pwaff2)
2569 return isl_pw_aff_on_shared_domain(pwaff1, pwaff2, &isl_aff_add);
2572 __isl_give isl_pw_aff *isl_pw_aff_add(__isl_take isl_pw_aff *pwaff1,
2573 __isl_take isl_pw_aff *pwaff2)
2575 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_add);
2578 __isl_give isl_pw_aff *isl_pw_aff_union_add(__isl_take isl_pw_aff *pwaff1,
2579 __isl_take isl_pw_aff *pwaff2)
2581 return isl_pw_aff_union_add_(pwaff1, pwaff2);
2584 static __isl_give isl_pw_aff *pw_aff_mul(__isl_take isl_pw_aff *pwaff1,
2585 __isl_take isl_pw_aff *pwaff2)
2587 return isl_pw_aff_on_shared_domain(pwaff1, pwaff2, &isl_aff_mul);
2590 __isl_give isl_pw_aff *isl_pw_aff_mul(__isl_take isl_pw_aff *pwaff1,
2591 __isl_take isl_pw_aff *pwaff2)
2593 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_mul);
2596 static __isl_give isl_pw_aff *pw_aff_div(__isl_take isl_pw_aff *pa1,
2597 __isl_take isl_pw_aff *pa2)
2599 return isl_pw_aff_on_shared_domain(pa1, pa2, &isl_aff_div);
2602 /* Divide "pa1" by "pa2", assuming "pa2" is a piecewise constant.
2604 __isl_give isl_pw_aff *isl_pw_aff_div(__isl_take isl_pw_aff *pa1,
2605 __isl_take isl_pw_aff *pa2)
2609 is_cst = isl_pw_aff_is_cst(pa2);
2613 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
2614 "second argument should be a piecewise constant",
2616 return isl_pw_aff_align_params_pw_pw_and(pa1, pa2, &pw_aff_div);
2618 isl_pw_aff_free(pa1);
2619 isl_pw_aff_free(pa2);
2623 /* Compute the quotient of the integer division of "pa1" by "pa2"
2624 * with rounding towards zero.
2625 * "pa2" is assumed to be a piecewise constant.
2627 * In particular, return
2629 * pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2)
2632 __isl_give isl_pw_aff *isl_pw_aff_tdiv_q(__isl_take isl_pw_aff *pa1,
2633 __isl_take isl_pw_aff *pa2)
2639 is_cst = isl_pw_aff_is_cst(pa2);
2643 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
2644 "second argument should be a piecewise constant",
2647 pa1 = isl_pw_aff_div(pa1, pa2);
2649 cond = isl_pw_aff_nonneg_set(isl_pw_aff_copy(pa1));
2650 f = isl_pw_aff_floor(isl_pw_aff_copy(pa1));
2651 c = isl_pw_aff_ceil(pa1);
2652 return isl_pw_aff_cond(isl_set_indicator_function(cond), f, c);
2654 isl_pw_aff_free(pa1);
2655 isl_pw_aff_free(pa2);
2659 /* Compute the remainder of the integer division of "pa1" by "pa2"
2660 * with rounding towards zero.
2661 * "pa2" is assumed to be a piecewise constant.
2663 * In particular, return
2665 * pa1 - pa2 * (pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2))
2668 __isl_give isl_pw_aff *isl_pw_aff_tdiv_r(__isl_take isl_pw_aff *pa1,
2669 __isl_take isl_pw_aff *pa2)
2674 is_cst = isl_pw_aff_is_cst(pa2);
2678 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
2679 "second argument should be a piecewise constant",
2681 res = isl_pw_aff_tdiv_q(isl_pw_aff_copy(pa1), isl_pw_aff_copy(pa2));
2682 res = isl_pw_aff_mul(pa2, res);
2683 res = isl_pw_aff_sub(pa1, res);
2686 isl_pw_aff_free(pa1);
2687 isl_pw_aff_free(pa2);
2691 static __isl_give isl_pw_aff *pw_aff_min(__isl_take isl_pw_aff *pwaff1,
2692 __isl_take isl_pw_aff *pwaff2)
2697 dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1)),
2698 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2)));
2699 le = isl_pw_aff_le_set(isl_pw_aff_copy(pwaff1),
2700 isl_pw_aff_copy(pwaff2));
2701 dom = isl_set_subtract(dom, isl_set_copy(le));
2702 return isl_pw_aff_select(le, pwaff1, dom, pwaff2);
2705 __isl_give isl_pw_aff *isl_pw_aff_min(__isl_take isl_pw_aff *pwaff1,
2706 __isl_take isl_pw_aff *pwaff2)
2708 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_min);
2711 static __isl_give isl_pw_aff *pw_aff_max(__isl_take isl_pw_aff *pwaff1,
2712 __isl_take isl_pw_aff *pwaff2)
2717 dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1)),
2718 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2)));
2719 ge = isl_pw_aff_ge_set(isl_pw_aff_copy(pwaff1),
2720 isl_pw_aff_copy(pwaff2));
2721 dom = isl_set_subtract(dom, isl_set_copy(ge));
2722 return isl_pw_aff_select(ge, pwaff1, dom, pwaff2);
2725 __isl_give isl_pw_aff *isl_pw_aff_max(__isl_take isl_pw_aff *pwaff1,
2726 __isl_take isl_pw_aff *pwaff2)
2728 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_max);
2731 static __isl_give isl_pw_aff *pw_aff_list_reduce(
2732 __isl_take isl_pw_aff_list *list,
2733 __isl_give isl_pw_aff *(*fn)(__isl_take isl_pw_aff *pwaff1,
2734 __isl_take isl_pw_aff *pwaff2))
2743 ctx = isl_pw_aff_list_get_ctx(list);
2745 isl_die(ctx, isl_error_invalid,
2746 "list should contain at least one element",
2747 return isl_pw_aff_list_free(list));
2749 res = isl_pw_aff_copy(list->p[0]);
2750 for (i = 1; i < list->n; ++i)
2751 res = fn(res, isl_pw_aff_copy(list->p[i]));
2753 isl_pw_aff_list_free(list);
2757 /* Return an isl_pw_aff that maps each element in the intersection of the
2758 * domains of the elements of list to the minimal corresponding affine
2761 __isl_give isl_pw_aff *isl_pw_aff_list_min(__isl_take isl_pw_aff_list *list)
2763 return pw_aff_list_reduce(list, &isl_pw_aff_min);
2766 /* Return an isl_pw_aff that maps each element in the intersection of the
2767 * domains of the elements of list to the maximal corresponding affine
2770 __isl_give isl_pw_aff *isl_pw_aff_list_max(__isl_take isl_pw_aff_list *list)
2772 return pw_aff_list_reduce(list, &isl_pw_aff_max);
2775 /* Mark the domains of "pwaff" as rational.
2777 __isl_give isl_pw_aff *isl_pw_aff_set_rational(__isl_take isl_pw_aff *pwaff)
2781 pwaff = isl_pw_aff_cow(pwaff);
2787 for (i = 0; i < pwaff->n; ++i) {
2788 pwaff->p[i].set = isl_set_set_rational(pwaff->p[i].set);
2789 if (!pwaff->p[i].set)
2790 return isl_pw_aff_free(pwaff);
2796 /* Mark the domains of the elements of "list" as rational.
2798 __isl_give isl_pw_aff_list *isl_pw_aff_list_set_rational(
2799 __isl_take isl_pw_aff_list *list)
2809 for (i = 0; i < n; ++i) {
2812 pa = isl_pw_aff_list_get_pw_aff(list, i);
2813 pa = isl_pw_aff_set_rational(pa);
2814 list = isl_pw_aff_list_set_pw_aff(list, i, pa);
2820 /* Check that the domain space of "aff" matches "space".
2822 * Return 0 on success and -1 on error.
2824 int isl_aff_check_match_domain_space(__isl_keep isl_aff *aff,
2825 __isl_keep isl_space *space)
2827 isl_space *aff_space;
2833 aff_space = isl_aff_get_domain_space(aff);
2835 match = isl_space_match(space, isl_dim_param, aff_space, isl_dim_param);
2839 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2840 "parameters don't match", goto error);
2841 match = isl_space_tuple_match(space, isl_dim_in,
2842 aff_space, isl_dim_set);
2846 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2847 "domains don't match", goto error);
2848 isl_space_free(aff_space);
2851 isl_space_free(aff_space);
2858 #include <isl_multi_templ.c>
2860 /* Create an isl_pw_multi_aff with the given isl_multi_aff on a universe
2863 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_multi_aff(
2864 __isl_take isl_multi_aff *ma)
2866 isl_set *dom = isl_set_universe(isl_multi_aff_get_domain_space(ma));
2867 return isl_pw_multi_aff_alloc(dom, ma);
2870 /* Create a piecewise multi-affine expression in the given space that maps each
2871 * input dimension to the corresponding output dimension.
2873 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_identity(
2874 __isl_take isl_space *space)
2876 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_identity(space));
2879 __isl_give isl_multi_aff *isl_multi_aff_add(__isl_take isl_multi_aff *maff1,
2880 __isl_take isl_multi_aff *maff2)
2882 return isl_multi_aff_bin_op(maff1, maff2, &isl_aff_add);
2885 /* Subtract "ma2" from "ma1" and return the result.
2887 __isl_give isl_multi_aff *isl_multi_aff_sub(__isl_take isl_multi_aff *ma1,
2888 __isl_take isl_multi_aff *ma2)
2890 return isl_multi_aff_bin_op(ma1, ma2, &isl_aff_sub);
2893 /* Given two multi-affine expressions A -> B and C -> D,
2894 * construct a multi-affine expression [A -> C] -> [B -> D].
2896 __isl_give isl_multi_aff *isl_multi_aff_product(
2897 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2)
2903 int in1, in2, out1, out2;
2905 in1 = isl_multi_aff_dim(ma1, isl_dim_in);
2906 in2 = isl_multi_aff_dim(ma2, isl_dim_in);
2907 out1 = isl_multi_aff_dim(ma1, isl_dim_out);
2908 out2 = isl_multi_aff_dim(ma2, isl_dim_out);
2909 space = isl_space_product(isl_multi_aff_get_space(ma1),
2910 isl_multi_aff_get_space(ma2));
2911 res = isl_multi_aff_alloc(isl_space_copy(space));
2912 space = isl_space_domain(space);
2914 for (i = 0; i < out1; ++i) {
2915 aff = isl_multi_aff_get_aff(ma1, i);
2916 aff = isl_aff_insert_dims(aff, isl_dim_in, in1, in2);
2917 aff = isl_aff_reset_domain_space(aff, isl_space_copy(space));
2918 res = isl_multi_aff_set_aff(res, i, aff);
2921 for (i = 0; i < out2; ++i) {
2922 aff = isl_multi_aff_get_aff(ma2, i);
2923 aff = isl_aff_insert_dims(aff, isl_dim_in, 0, in1);
2924 aff = isl_aff_reset_domain_space(aff, isl_space_copy(space));
2925 res = isl_multi_aff_set_aff(res, out1 + i, aff);
2928 isl_space_free(space);
2929 isl_multi_aff_free(ma1);
2930 isl_multi_aff_free(ma2);
2934 /* Exploit the equalities in "eq" to simplify the affine expressions.
2936 static __isl_give isl_multi_aff *isl_multi_aff_substitute_equalities(
2937 __isl_take isl_multi_aff *maff, __isl_take isl_basic_set *eq)
2941 maff = isl_multi_aff_cow(maff);
2945 for (i = 0; i < maff->n; ++i) {
2946 maff->p[i] = isl_aff_substitute_equalities(maff->p[i],
2947 isl_basic_set_copy(eq));
2952 isl_basic_set_free(eq);
2955 isl_basic_set_free(eq);
2956 isl_multi_aff_free(maff);
2960 __isl_give isl_multi_aff *isl_multi_aff_scale(__isl_take isl_multi_aff *maff,
2965 maff = isl_multi_aff_cow(maff);
2969 for (i = 0; i < maff->n; ++i) {
2970 maff->p[i] = isl_aff_scale(maff->p[i], f);
2972 return isl_multi_aff_free(maff);
2978 __isl_give isl_multi_aff *isl_multi_aff_add_on_domain(__isl_keep isl_set *dom,
2979 __isl_take isl_multi_aff *maff1, __isl_take isl_multi_aff *maff2)
2981 maff1 = isl_multi_aff_add(maff1, maff2);
2982 maff1 = isl_multi_aff_gist(maff1, isl_set_copy(dom));
2986 int isl_multi_aff_is_empty(__isl_keep isl_multi_aff *maff)
2994 int isl_multi_aff_plain_is_equal(__isl_keep isl_multi_aff *maff1,
2995 __isl_keep isl_multi_aff *maff2)
3000 if (!maff1 || !maff2)
3002 if (maff1->n != maff2->n)
3004 equal = isl_space_is_equal(maff1->space, maff2->space);
3005 if (equal < 0 || !equal)
3008 for (i = 0; i < maff1->n; ++i) {
3009 equal = isl_aff_plain_is_equal(maff1->p[i], maff2->p[i]);
3010 if (equal < 0 || !equal)
3017 /* Return the set of domain elements where "ma1" is lexicographically
3018 * smaller than or equal to "ma2".
3020 __isl_give isl_set *isl_multi_aff_lex_le_set(__isl_take isl_multi_aff *ma1,
3021 __isl_take isl_multi_aff *ma2)
3023 return isl_multi_aff_lex_ge_set(ma2, ma1);
3026 /* Return the set of domain elements where "ma1" is lexicographically
3027 * greater than or equal to "ma2".
3029 __isl_give isl_set *isl_multi_aff_lex_ge_set(__isl_take isl_multi_aff *ma1,
3030 __isl_take isl_multi_aff *ma2)
3033 isl_map *map1, *map2;
3036 map1 = isl_map_from_multi_aff(ma1);
3037 map2 = isl_map_from_multi_aff(ma2);
3038 map = isl_map_range_product(map1, map2);
3039 space = isl_space_range(isl_map_get_space(map));
3040 space = isl_space_domain(isl_space_unwrap(space));
3041 ge = isl_map_lex_ge(space);
3042 map = isl_map_intersect_range(map, isl_map_wrap(ge));
3044 return isl_map_domain(map);
3048 #define PW isl_pw_multi_aff
3050 #define EL isl_multi_aff
3052 #define EL_IS_ZERO is_empty
3056 #define IS_ZERO is_empty
3059 #undef DEFAULT_IS_ZERO
3060 #define DEFAULT_IS_ZERO 0
3065 #define NO_INVOLVES_DIMS
3066 #define NO_MOVE_DIMS
3067 #define NO_INSERT_DIMS
3071 #include <isl_pw_templ.c>
3074 #define UNION isl_union_pw_multi_aff
3076 #define PART isl_pw_multi_aff
3078 #define PARTS pw_multi_aff
3079 #define ALIGN_DOMAIN
3083 #include <isl_union_templ.c>
3085 /* Given a function "cmp" that returns the set of elements where
3086 * "ma1" is "better" than "ma2", return the intersection of this
3087 * set with "dom1" and "dom2".
3089 static __isl_give isl_set *shared_and_better(__isl_keep isl_set *dom1,
3090 __isl_keep isl_set *dom2, __isl_keep isl_multi_aff *ma1,
3091 __isl_keep isl_multi_aff *ma2,
3092 __isl_give isl_set *(*cmp)(__isl_take isl_multi_aff *ma1,
3093 __isl_take isl_multi_aff *ma2))
3099 common = isl_set_intersect(isl_set_copy(dom1), isl_set_copy(dom2));
3100 is_empty = isl_set_plain_is_empty(common);
3101 if (is_empty >= 0 && is_empty)
3104 return isl_set_free(common);
3105 better = cmp(isl_multi_aff_copy(ma1), isl_multi_aff_copy(ma2));
3106 better = isl_set_intersect(common, better);
3111 /* Given a function "cmp" that returns the set of elements where
3112 * "ma1" is "better" than "ma2", return a piecewise multi affine
3113 * expression defined on the union of the definition domains
3114 * of "pma1" and "pma2" that maps to the "best" of "pma1" and
3115 * "pma2" on each cell. If only one of the two input functions
3116 * is defined on a given cell, then it is considered the best.
3118 static __isl_give isl_pw_multi_aff *pw_multi_aff_union_opt(
3119 __isl_take isl_pw_multi_aff *pma1,
3120 __isl_take isl_pw_multi_aff *pma2,
3121 __isl_give isl_set *(*cmp)(__isl_take isl_multi_aff *ma1,
3122 __isl_take isl_multi_aff *ma2))
3125 isl_pw_multi_aff *res = NULL;
3127 isl_set *set = NULL;
3132 ctx = isl_space_get_ctx(pma1->dim);
3133 if (!isl_space_is_equal(pma1->dim, pma2->dim))
3134 isl_die(ctx, isl_error_invalid,
3135 "arguments should live in the same space", goto error);
3137 if (isl_pw_multi_aff_is_empty(pma1)) {
3138 isl_pw_multi_aff_free(pma1);
3142 if (isl_pw_multi_aff_is_empty(pma2)) {
3143 isl_pw_multi_aff_free(pma2);
3147 n = 2 * (pma1->n + 1) * (pma2->n + 1);
3148 res = isl_pw_multi_aff_alloc_size(isl_space_copy(pma1->dim), n);
3150 for (i = 0; i < pma1->n; ++i) {
3151 set = isl_set_copy(pma1->p[i].set);
3152 for (j = 0; j < pma2->n; ++j) {
3156 better = shared_and_better(pma2->p[j].set,
3157 pma1->p[i].set, pma2->p[j].maff,
3158 pma1->p[i].maff, cmp);
3159 is_empty = isl_set_plain_is_empty(better);
3160 if (is_empty < 0 || is_empty) {
3161 isl_set_free(better);
3166 set = isl_set_subtract(set, isl_set_copy(better));
3168 res = isl_pw_multi_aff_add_piece(res, better,
3169 isl_multi_aff_copy(pma2->p[j].maff));
3171 res = isl_pw_multi_aff_add_piece(res, set,
3172 isl_multi_aff_copy(pma1->p[i].maff));
3175 for (j = 0; j < pma2->n; ++j) {
3176 set = isl_set_copy(pma2->p[j].set);
3177 for (i = 0; i < pma1->n; ++i)
3178 set = isl_set_subtract(set,
3179 isl_set_copy(pma1->p[i].set));
3180 res = isl_pw_multi_aff_add_piece(res, set,
3181 isl_multi_aff_copy(pma2->p[j].maff));
3184 isl_pw_multi_aff_free(pma1);
3185 isl_pw_multi_aff_free(pma2);
3189 isl_pw_multi_aff_free(pma1);
3190 isl_pw_multi_aff_free(pma2);
3192 return isl_pw_multi_aff_free(res);
3195 static __isl_give isl_pw_multi_aff *pw_multi_aff_union_lexmax(
3196 __isl_take isl_pw_multi_aff *pma1,
3197 __isl_take isl_pw_multi_aff *pma2)
3199 return pw_multi_aff_union_opt(pma1, pma2, &isl_multi_aff_lex_ge_set);
3202 /* Given two piecewise multi affine expressions, return a piecewise
3203 * multi-affine expression defined on the union of the definition domains
3204 * of the inputs that is equal to the lexicographic maximum of the two
3205 * inputs on each cell. If only one of the two inputs is defined on
3206 * a given cell, then it is considered to be the maximum.
3208 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_lexmax(
3209 __isl_take isl_pw_multi_aff *pma1,
3210 __isl_take isl_pw_multi_aff *pma2)
3212 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
3213 &pw_multi_aff_union_lexmax);
3216 static __isl_give isl_pw_multi_aff *pw_multi_aff_union_lexmin(
3217 __isl_take isl_pw_multi_aff *pma1,
3218 __isl_take isl_pw_multi_aff *pma2)
3220 return pw_multi_aff_union_opt(pma1, pma2, &isl_multi_aff_lex_le_set);
3223 /* Given two piecewise multi affine expressions, return a piecewise
3224 * multi-affine expression defined on the union of the definition domains
3225 * of the inputs that is equal to the lexicographic minimum of the two
3226 * inputs on each cell. If only one of the two inputs is defined on
3227 * a given cell, then it is considered to be the minimum.
3229 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_lexmin(
3230 __isl_take isl_pw_multi_aff *pma1,
3231 __isl_take isl_pw_multi_aff *pma2)
3233 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
3234 &pw_multi_aff_union_lexmin);
3237 static __isl_give isl_pw_multi_aff *pw_multi_aff_add(
3238 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
3240 return isl_pw_multi_aff_on_shared_domain(pma1, pma2,
3241 &isl_multi_aff_add);
3244 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_add(
3245 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
3247 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
3251 static __isl_give isl_pw_multi_aff *pw_multi_aff_sub(
3252 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
3254 return isl_pw_multi_aff_on_shared_domain(pma1, pma2,
3255 &isl_multi_aff_sub);
3258 /* Subtract "pma2" from "pma1" and return the result.
3260 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_sub(
3261 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
3263 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
3267 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_add(
3268 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
3270 return isl_pw_multi_aff_union_add_(pma1, pma2);
3273 /* Given two piecewise multi-affine expressions A -> B and C -> D,
3274 * construct a piecewise multi-affine expression [A -> C] -> [B -> D].
3276 static __isl_give isl_pw_multi_aff *pw_multi_aff_product(
3277 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
3281 isl_pw_multi_aff *res;
3286 n = pma1->n * pma2->n;
3287 space = isl_space_product(isl_space_copy(pma1->dim),
3288 isl_space_copy(pma2->dim));
3289 res = isl_pw_multi_aff_alloc_size(space, n);
3291 for (i = 0; i < pma1->n; ++i) {
3292 for (j = 0; j < pma2->n; ++j) {
3296 domain = isl_set_product(isl_set_copy(pma1->p[i].set),
3297 isl_set_copy(pma2->p[j].set));
3298 ma = isl_multi_aff_product(
3299 isl_multi_aff_copy(pma1->p[i].maff),
3300 isl_multi_aff_copy(pma2->p[i].maff));
3301 res = isl_pw_multi_aff_add_piece(res, domain, ma);
3305 isl_pw_multi_aff_free(pma1);
3306 isl_pw_multi_aff_free(pma2);
3309 isl_pw_multi_aff_free(pma1);
3310 isl_pw_multi_aff_free(pma2);
3314 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_product(
3315 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
3317 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
3318 &pw_multi_aff_product);
3321 /* Construct a map mapping the domain of the piecewise multi-affine expression
3322 * to its range, with each dimension in the range equated to the
3323 * corresponding affine expression on its cell.
3325 __isl_give isl_map *isl_map_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma)
3333 map = isl_map_empty(isl_pw_multi_aff_get_space(pma));
3335 for (i = 0; i < pma->n; ++i) {
3336 isl_multi_aff *maff;
3337 isl_basic_map *bmap;
3340 maff = isl_multi_aff_copy(pma->p[i].maff);
3341 bmap = isl_basic_map_from_multi_aff(maff);
3342 map_i = isl_map_from_basic_map(bmap);
3343 map_i = isl_map_intersect_domain(map_i,
3344 isl_set_copy(pma->p[i].set));
3345 map = isl_map_union_disjoint(map, map_i);
3348 isl_pw_multi_aff_free(pma);
3352 __isl_give isl_set *isl_set_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma)
3357 if (!isl_space_is_set(pma->dim))
3358 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
3359 "isl_pw_multi_aff cannot be converted into an isl_set",
3360 return isl_pw_multi_aff_free(pma));
3362 return isl_map_from_pw_multi_aff(pma);
3365 /* Given a basic map with a single output dimension that is defined
3366 * in terms of the parameters and input dimensions using an equality,
3367 * extract an isl_aff that expresses the output dimension in terms
3368 * of the parameters and input dimensions.
3370 * Since some applications expect the result of isl_pw_multi_aff_from_map
3371 * to only contain integer affine expressions, we compute the floor
3372 * of the expression before returning.
3374 * This function shares some similarities with
3375 * isl_basic_map_has_defining_equality and isl_constraint_get_bound.
3377 static __isl_give isl_aff *extract_isl_aff_from_basic_map(
3378 __isl_take isl_basic_map *bmap)
3383 isl_local_space *ls;
3388 if (isl_basic_map_dim(bmap, isl_dim_out) != 1)
3389 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
3390 "basic map should have a single output dimension",
3392 offset = isl_basic_map_offset(bmap, isl_dim_out);
3393 total = isl_basic_map_total_dim(bmap);
3394 for (i = 0; i < bmap->n_eq; ++i) {
3395 if (isl_int_is_zero(bmap->eq[i][offset]))
3397 if (isl_seq_first_non_zero(bmap->eq[i] + offset + 1,
3398 1 + total - (offset + 1)) != -1)
3402 if (i >= bmap->n_eq)
3403 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
3404 "unable to find suitable equality", goto error);
3405 ls = isl_basic_map_get_local_space(bmap);
3406 aff = isl_aff_alloc(isl_local_space_domain(ls));
3409 if (isl_int_is_neg(bmap->eq[i][offset]))
3410 isl_seq_cpy(aff->v->el + 1, bmap->eq[i], offset);
3412 isl_seq_neg(aff->v->el + 1, bmap->eq[i], offset);
3413 isl_seq_clr(aff->v->el + 1 + offset, aff->v->size - (1 + offset));
3414 isl_int_abs(aff->v->el[0], bmap->eq[i][offset]);
3415 isl_basic_map_free(bmap);
3417 aff = isl_aff_remove_unused_divs(aff);
3418 aff = isl_aff_floor(aff);
3421 isl_basic_map_free(bmap);
3425 /* Given a basic map where each output dimension is defined
3426 * in terms of the parameters and input dimensions using an equality,
3427 * extract an isl_multi_aff that expresses the output dimensions in terms
3428 * of the parameters and input dimensions.
3430 static __isl_give isl_multi_aff *extract_isl_multi_aff_from_basic_map(
3431 __isl_take isl_basic_map *bmap)
3440 ma = isl_multi_aff_alloc(isl_basic_map_get_space(bmap));
3441 n_out = isl_basic_map_dim(bmap, isl_dim_out);
3443 for (i = 0; i < n_out; ++i) {
3444 isl_basic_map *bmap_i;
3447 bmap_i = isl_basic_map_copy(bmap);
3448 bmap_i = isl_basic_map_project_out(bmap_i, isl_dim_out,
3449 i + 1, n_out - (1 + i));
3450 bmap_i = isl_basic_map_project_out(bmap_i, isl_dim_out, 0, i);
3451 aff = extract_isl_aff_from_basic_map(bmap_i);
3452 ma = isl_multi_aff_set_aff(ma, i, aff);
3455 isl_basic_map_free(bmap);
3460 /* Create an isl_pw_multi_aff that is equivalent to
3461 * isl_map_intersect_domain(isl_map_from_basic_map(bmap), domain).
3462 * The given basic map is such that each output dimension is defined
3463 * in terms of the parameters and input dimensions using an equality.
3465 static __isl_give isl_pw_multi_aff *plain_pw_multi_aff_from_map(
3466 __isl_take isl_set *domain, __isl_take isl_basic_map *bmap)
3470 ma = extract_isl_multi_aff_from_basic_map(bmap);
3471 return isl_pw_multi_aff_alloc(domain, ma);
3474 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
3475 * This obviously only works if the input "map" is single-valued.
3476 * If so, we compute the lexicographic minimum of the image in the form
3477 * of an isl_pw_multi_aff. Since the image is unique, it is equal
3478 * to its lexicographic minimum.
3479 * If the input is not single-valued, we produce an error.
3481 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_base(
3482 __isl_take isl_map *map)
3486 isl_pw_multi_aff *pma;
3488 sv = isl_map_is_single_valued(map);
3492 isl_die(isl_map_get_ctx(map), isl_error_invalid,
3493 "map is not single-valued", goto error);
3494 map = isl_map_make_disjoint(map);
3498 pma = isl_pw_multi_aff_empty(isl_map_get_space(map));
3500 for (i = 0; i < map->n; ++i) {
3501 isl_pw_multi_aff *pma_i;
3502 isl_basic_map *bmap;
3503 bmap = isl_basic_map_copy(map->p[i]);
3504 pma_i = isl_basic_map_lexmin_pw_multi_aff(bmap);
3505 pma = isl_pw_multi_aff_add_disjoint(pma, pma_i);
3515 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
3516 * taking into account that the output dimension at position "d"
3517 * can be represented as
3519 * x = floor((e(...) + c1) / m)
3521 * given that constraint "i" is of the form
3523 * e(...) + c1 - m x >= 0
3526 * Let "map" be of the form
3530 * We construct a mapping
3532 * A -> [A -> x = floor(...)]
3534 * apply that to the map, obtaining
3536 * [A -> x = floor(...)] -> B
3538 * and equate dimension "d" to x.
3539 * We then compute a isl_pw_multi_aff representation of the resulting map
3540 * and plug in the mapping above.
3542 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_div(
3543 __isl_take isl_map *map, __isl_take isl_basic_map *hull, int d, int i)
3547 isl_local_space *ls;
3555 isl_pw_multi_aff *pma;
3558 is_set = isl_map_is_set(map);
3560 offset = isl_basic_map_offset(hull, isl_dim_out);
3561 ctx = isl_map_get_ctx(map);
3562 space = isl_space_domain(isl_map_get_space(map));
3563 n_in = isl_space_dim(space, isl_dim_set);
3564 n = isl_space_dim(space, isl_dim_all);
3566 v = isl_vec_alloc(ctx, 1 + 1 + n);
3568 isl_int_neg(v->el[0], hull->ineq[i][offset + d]);
3569 isl_seq_cpy(v->el + 1, hull->ineq[i], 1 + n);
3571 isl_basic_map_free(hull);
3573 ls = isl_local_space_from_space(isl_space_copy(space));
3574 aff = isl_aff_alloc_vec(ls, v);
3575 aff = isl_aff_floor(aff);
3577 isl_space_free(space);
3578 ma = isl_multi_aff_from_aff(aff);
3580 ma = isl_multi_aff_identity(isl_space_map_from_set(space));
3581 ma = isl_multi_aff_range_product(ma,
3582 isl_multi_aff_from_aff(aff));
3585 insert = isl_map_from_multi_aff(isl_multi_aff_copy(ma));
3586 map = isl_map_apply_domain(map, insert);
3587 map = isl_map_equate(map, isl_dim_in, n_in, isl_dim_out, d);
3588 pma = isl_pw_multi_aff_from_map(map);
3589 pma = isl_pw_multi_aff_pullback_multi_aff(pma, ma);
3594 /* Is constraint "c" of the form
3596 * e(...) + c1 - m x >= 0
3600 * -e(...) + c2 + m x >= 0
3602 * where m > 1 and e only depends on parameters and input dimemnsions?
3604 * "offset" is the offset of the output dimensions
3605 * "pos" is the position of output dimension x.
3607 static int is_potential_div_constraint(isl_int *c, int offset, int d, int total)
3609 if (isl_int_is_zero(c[offset + d]))
3611 if (isl_int_is_one(c[offset + d]))
3613 if (isl_int_is_negone(c[offset + d]))
3615 if (isl_seq_first_non_zero(c + offset, d) != -1)
3617 if (isl_seq_first_non_zero(c + offset + d + 1,
3618 total - (offset + d + 1)) != -1)
3623 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
3625 * As a special case, we first check if there is any pair of constraints,
3626 * shared by all the basic maps in "map" that force a given dimension
3627 * to be equal to the floor of some affine combination of the input dimensions.
3629 * In particular, if we can find two constraints
3631 * e(...) + c1 - m x >= 0 i.e., m x <= e(...) + c1
3635 * -e(...) + c2 + m x >= 0 i.e., m x >= e(...) - c2
3637 * where m > 1 and e only depends on parameters and input dimemnsions,
3640 * c1 + c2 < m i.e., -c2 >= c1 - (m - 1)
3642 * then we know that we can take
3644 * x = floor((e(...) + c1) / m)
3646 * without having to perform any computation.
3648 * Note that we know that
3652 * If c1 + c2 were 0, then we would have detected an equality during
3653 * simplification. If c1 + c2 were negative, then we would have detected
3656 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_check_div(
3657 __isl_take isl_map *map)
3663 isl_basic_map *hull;
3665 hull = isl_map_unshifted_simple_hull(isl_map_copy(map));
3670 dim = isl_map_dim(map, isl_dim_out);
3671 offset = isl_basic_map_offset(hull, isl_dim_out);
3672 total = 1 + isl_basic_map_total_dim(hull);
3674 for (d = 0; d < dim; ++d) {
3675 for (i = 0; i < n; ++i) {
3676 if (!is_potential_div_constraint(hull->ineq[i],
3679 for (j = i + 1; j < n; ++j) {
3680 if (!isl_seq_is_neg(hull->ineq[i] + 1,
3681 hull->ineq[j] + 1, total - 1))
3683 isl_int_add(sum, hull->ineq[i][0],
3685 if (isl_int_abs_lt(sum,
3686 hull->ineq[i][offset + d]))
3693 if (isl_int_is_pos(hull->ineq[j][offset + d]))
3695 return pw_multi_aff_from_map_div(map, hull, d, j);
3699 isl_basic_map_free(hull);
3700 return pw_multi_aff_from_map_base(map);
3703 isl_basic_map_free(hull);
3707 /* Given an affine expression
3709 * [A -> B] -> f(A,B)
3711 * construct an isl_multi_aff
3715 * such that dimension "d" in B' is set to "aff" and the remaining
3716 * dimensions are set equal to the corresponding dimensions in B.
3717 * "n_in" is the dimension of the space A.
3718 * "n_out" is the dimension of the space B.
3720 * If "is_set" is set, then the affine expression is of the form
3724 * and we construct an isl_multi_aff
3728 static __isl_give isl_multi_aff *range_map(__isl_take isl_aff *aff, int d,
3729 unsigned n_in, unsigned n_out, int is_set)
3733 isl_space *space, *space2;
3734 isl_local_space *ls;
3736 space = isl_aff_get_domain_space(aff);
3737 ls = isl_local_space_from_space(isl_space_copy(space));
3738 space2 = isl_space_copy(space);
3740 space2 = isl_space_range(isl_space_unwrap(space2));
3741 space = isl_space_map_from_domain_and_range(space, space2);
3742 ma = isl_multi_aff_alloc(space);
3743 ma = isl_multi_aff_set_aff(ma, d, aff);
3745 for (i = 0; i < n_out; ++i) {
3748 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
3749 isl_dim_set, n_in + i);
3750 ma = isl_multi_aff_set_aff(ma, i, aff);
3753 isl_local_space_free(ls);
3758 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
3759 * taking into account that the dimension at position "d" can be written as
3761 * x = m a + f(..) (1)
3763 * where m is equal to "gcd".
3764 * "i" is the index of the equality in "hull" that defines f(..).
3765 * In particular, the equality is of the form
3767 * f(..) - x + m g(existentials) = 0
3771 * -f(..) + x + m g(existentials) = 0
3773 * We basically plug (1) into "map", resulting in a map with "a"
3774 * in the range instead of "x". The corresponding isl_pw_multi_aff
3775 * defining "a" is then plugged back into (1) to obtain a definition fro "x".
3777 * Specifically, given the input map
3781 * We first wrap it into a set
3785 * and define (1) on top of the corresponding space, resulting in "aff".
3786 * We use this to create an isl_multi_aff that maps the output position "d"
3787 * from "a" to "x", leaving all other (intput and output) dimensions unchanged.
3788 * We plug this into the wrapped map, unwrap the result and compute the
3789 * corresponding isl_pw_multi_aff.
3790 * The result is an expression
3798 * so that we can plug that into "aff", after extending the latter to
3804 * If "map" is actually a set, then there is no "A" space, meaning
3805 * that we do not need to perform any wrapping, and that the result
3806 * of the recursive call is of the form
3810 * which is plugged into a mapping of the form
3814 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_stride(
3815 __isl_take isl_map *map, __isl_take isl_basic_map *hull, int d, int i,
3820 isl_local_space *ls;
3823 isl_pw_multi_aff *pma, *id;
3829 is_set = isl_map_is_set(map);
3831 n_in = isl_basic_map_dim(hull, isl_dim_in);
3832 n_out = isl_basic_map_dim(hull, isl_dim_out);
3833 o_out = isl_basic_map_offset(hull, isl_dim_out);
3838 set = isl_map_wrap(map);
3839 space = isl_space_map_from_set(isl_set_get_space(set));
3840 ma = isl_multi_aff_identity(space);
3841 ls = isl_local_space_from_space(isl_set_get_space(set));
3842 aff = isl_aff_alloc(ls);
3844 isl_int_set_si(aff->v->el[0], 1);
3845 if (isl_int_is_one(hull->eq[i][o_out + d]))
3846 isl_seq_neg(aff->v->el + 1, hull->eq[i],
3849 isl_seq_cpy(aff->v->el + 1, hull->eq[i],
3851 isl_int_set(aff->v->el[1 + o_out + d], gcd);
3853 ma = isl_multi_aff_set_aff(ma, n_in + d, isl_aff_copy(aff));
3854 set = isl_set_preimage_multi_aff(set, ma);
3856 ma = range_map(aff, d, n_in, n_out, is_set);
3861 map = isl_set_unwrap(set);
3862 pma = isl_pw_multi_aff_from_map(set);
3865 space = isl_pw_multi_aff_get_domain_space(pma);
3866 space = isl_space_map_from_set(space);
3867 id = isl_pw_multi_aff_identity(space);
3868 pma = isl_pw_multi_aff_range_product(id, pma);
3870 id = isl_pw_multi_aff_from_multi_aff(ma);
3871 pma = isl_pw_multi_aff_pullback_pw_multi_aff(id, pma);
3873 isl_basic_map_free(hull);
3877 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
3879 * As a special case, we first check if all output dimensions are uniquely
3880 * defined in terms of the parameters and input dimensions over the entire
3881 * domain. If so, we extract the desired isl_pw_multi_aff directly
3882 * from the affine hull of "map" and its domain.
3884 * Otherwise, we check if any of the output dimensions is "strided".
3885 * That is, we check if can be written as
3889 * with m greater than 1, a some combination of existentiall quantified
3890 * variables and f and expression in the parameters and input dimensions.
3891 * If so, we remove the stride in pw_multi_aff_from_map_stride.
3893 * Otherwise, we continue with pw_multi_aff_from_map_check_div for a further
3896 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_map(__isl_take isl_map *map)
3900 isl_basic_map *hull;
3910 hull = isl_map_affine_hull(isl_map_copy(map));
3911 sv = isl_basic_map_plain_is_single_valued(hull);
3913 return plain_pw_multi_aff_from_map(isl_map_domain(map), hull);
3915 hull = isl_basic_map_free(hull);
3919 n_div = isl_basic_map_dim(hull, isl_dim_div);
3920 o_div = isl_basic_map_offset(hull, isl_dim_div);
3923 isl_basic_map_free(hull);
3924 return pw_multi_aff_from_map_check_div(map);
3929 n_out = isl_basic_map_dim(hull, isl_dim_out);
3930 o_out = isl_basic_map_offset(hull, isl_dim_out);
3932 for (i = 0; i < n_out; ++i) {
3933 for (j = 0; j < hull->n_eq; ++j) {
3934 isl_int *eq = hull->eq[j];
3935 isl_pw_multi_aff *res;
3937 if (!isl_int_is_one(eq[o_out + i]) &&
3938 !isl_int_is_negone(eq[o_out + i]))
3940 if (isl_seq_first_non_zero(eq + o_out, i) != -1)
3942 if (isl_seq_first_non_zero(eq + o_out + i + 1,
3943 n_out - (i + 1)) != -1)
3945 isl_seq_gcd(eq + o_div, n_div, &gcd);
3946 if (isl_int_is_zero(gcd))
3948 if (isl_int_is_one(gcd))
3951 res = pw_multi_aff_from_map_stride(map, hull,
3959 isl_basic_map_free(hull);
3960 return pw_multi_aff_from_map_check_div(map);
3966 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_set(__isl_take isl_set *set)
3968 return isl_pw_multi_aff_from_map(set);
3971 /* Convert "map" into an isl_pw_multi_aff (if possible) and
3974 static int pw_multi_aff_from_map(__isl_take isl_map *map, void *user)
3976 isl_union_pw_multi_aff **upma = user;
3977 isl_pw_multi_aff *pma;
3979 pma = isl_pw_multi_aff_from_map(map);
3980 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
3982 return *upma ? 0 : -1;
3985 /* Try and create an isl_union_pw_multi_aff that is equivalent
3986 * to the given isl_union_map.
3987 * The isl_union_map is required to be single-valued in each space.
3988 * Otherwise, an error is produced.
3990 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_map(
3991 __isl_take isl_union_map *umap)
3994 isl_union_pw_multi_aff *upma;
3996 space = isl_union_map_get_space(umap);
3997 upma = isl_union_pw_multi_aff_empty(space);
3998 if (isl_union_map_foreach_map(umap, &pw_multi_aff_from_map, &upma) < 0)
3999 upma = isl_union_pw_multi_aff_free(upma);
4000 isl_union_map_free(umap);
4005 /* Try and create an isl_union_pw_multi_aff that is equivalent
4006 * to the given isl_union_set.
4007 * The isl_union_set is required to be a singleton in each space.
4008 * Otherwise, an error is produced.
4010 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_set(
4011 __isl_take isl_union_set *uset)
4013 return isl_union_pw_multi_aff_from_union_map(uset);
4016 /* Return the piecewise affine expression "set ? 1 : 0".
4018 __isl_give isl_pw_aff *isl_set_indicator_function(__isl_take isl_set *set)
4021 isl_space *space = isl_set_get_space(set);
4022 isl_local_space *ls = isl_local_space_from_space(space);
4023 isl_aff *zero = isl_aff_zero_on_domain(isl_local_space_copy(ls));
4024 isl_aff *one = isl_aff_zero_on_domain(ls);
4026 one = isl_aff_add_constant_si(one, 1);
4027 pa = isl_pw_aff_alloc(isl_set_copy(set), one);
4028 set = isl_set_complement(set);
4029 pa = isl_pw_aff_add_disjoint(pa, isl_pw_aff_alloc(set, zero));
4034 /* Plug in "subs" for dimension "type", "pos" of "aff".
4036 * Let i be the dimension to replace and let "subs" be of the form
4040 * and "aff" of the form
4046 * (a f + d g')/(m d)
4048 * where g' is the result of plugging in "subs" in each of the integer
4051 __isl_give isl_aff *isl_aff_substitute(__isl_take isl_aff *aff,
4052 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
4057 aff = isl_aff_cow(aff);
4059 return isl_aff_free(aff);
4061 ctx = isl_aff_get_ctx(aff);
4062 if (!isl_space_is_equal(aff->ls->dim, subs->ls->dim))
4063 isl_die(ctx, isl_error_invalid,
4064 "spaces don't match", return isl_aff_free(aff));
4065 if (isl_local_space_dim(subs->ls, isl_dim_div) != 0)
4066 isl_die(ctx, isl_error_unsupported,
4067 "cannot handle divs yet", return isl_aff_free(aff));
4069 aff->ls = isl_local_space_substitute(aff->ls, type, pos, subs);
4071 return isl_aff_free(aff);
4073 aff->v = isl_vec_cow(aff->v);
4075 return isl_aff_free(aff);
4077 pos += isl_local_space_offset(aff->ls, type);
4080 isl_seq_substitute(aff->v->el, pos, subs->v->el,
4081 aff->v->size, subs->v->size, v);
4087 /* Plug in "subs" for dimension "type", "pos" in each of the affine
4088 * expressions in "maff".
4090 __isl_give isl_multi_aff *isl_multi_aff_substitute(
4091 __isl_take isl_multi_aff *maff, enum isl_dim_type type, unsigned pos,
4092 __isl_keep isl_aff *subs)
4096 maff = isl_multi_aff_cow(maff);
4098 return isl_multi_aff_free(maff);
4100 if (type == isl_dim_in)
4103 for (i = 0; i < maff->n; ++i) {
4104 maff->p[i] = isl_aff_substitute(maff->p[i], type, pos, subs);
4106 return isl_multi_aff_free(maff);
4112 /* Plug in "subs" for dimension "type", "pos" of "pma".
4114 * pma is of the form
4118 * while subs is of the form
4120 * v' = B_j(v) -> S_j
4122 * Each pair i,j such that C_ij = A_i \cap B_i is non-empty
4123 * has a contribution in the result, in particular
4125 * C_ij(S_j) -> M_i(S_j)
4127 * Note that plugging in S_j in C_ij may also result in an empty set
4128 * and this contribution should simply be discarded.
4130 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_substitute(
4131 __isl_take isl_pw_multi_aff *pma, enum isl_dim_type type, unsigned pos,
4132 __isl_keep isl_pw_aff *subs)
4135 isl_pw_multi_aff *res;
4138 return isl_pw_multi_aff_free(pma);
4140 n = pma->n * subs->n;
4141 res = isl_pw_multi_aff_alloc_size(isl_space_copy(pma->dim), n);
4143 for (i = 0; i < pma->n; ++i) {
4144 for (j = 0; j < subs->n; ++j) {
4146 isl_multi_aff *res_ij;
4149 common = isl_set_intersect(
4150 isl_set_copy(pma->p[i].set),
4151 isl_set_copy(subs->p[j].set));
4152 common = isl_set_substitute(common,
4153 type, pos, subs->p[j].aff);
4154 empty = isl_set_plain_is_empty(common);
4155 if (empty < 0 || empty) {
4156 isl_set_free(common);
4162 res_ij = isl_multi_aff_substitute(
4163 isl_multi_aff_copy(pma->p[i].maff),
4164 type, pos, subs->p[j].aff);
4166 res = isl_pw_multi_aff_add_piece(res, common, res_ij);
4170 isl_pw_multi_aff_free(pma);
4173 isl_pw_multi_aff_free(pma);
4174 isl_pw_multi_aff_free(res);
4178 /* Compute the preimage of a range of dimensions in the affine expression "src"
4179 * under "ma" and put the result in "dst". The number of dimensions in "src"
4180 * that precede the range is given by "n_before". The number of dimensions
4181 * in the range is given by the number of output dimensions of "ma".
4182 * The number of dimensions that follow the range is given by "n_after".
4183 * If "has_denom" is set (to one),
4184 * then "src" and "dst" have an extra initial denominator.
4185 * "n_div_ma" is the number of existentials in "ma"
4186 * "n_div_bset" is the number of existentials in "src"
4187 * The resulting "dst" (which is assumed to have been allocated by
4188 * the caller) contains coefficients for both sets of existentials,
4189 * first those in "ma" and then those in "src".
4190 * f, c1, c2 and g are temporary objects that have been initialized
4193 * Let src represent the expression
4195 * (a(p) + f_u u + b v + f_w w + c(divs))/d
4197 * and let ma represent the expressions
4199 * v_i = (r_i(p) + s_i(y) + t_i(divs'))/m_i
4201 * We start out with the following expression for dst:
4203 * (a(p) + f_u u + 0 y + f_w w + 0 divs' + c(divs) + f \sum_i b_i v_i)/d
4205 * with the multiplication factor f initially equal to 1
4206 * and f \sum_i b_i v_i kept separately.
4207 * For each x_i that we substitute, we multiply the numerator
4208 * (and denominator) of dst by c_1 = m_i and add the numerator
4209 * of the x_i expression multiplied by c_2 = f b_i,
4210 * after removing the common factors of c_1 and c_2.
4211 * The multiplication factor f also needs to be multiplied by c_1
4212 * for the next x_j, j > i.
4214 void isl_seq_preimage(isl_int *dst, isl_int *src,
4215 __isl_keep isl_multi_aff *ma, int n_before, int n_after,
4216 int n_div_ma, int n_div_bmap,
4217 isl_int f, isl_int c1, isl_int c2, isl_int g, int has_denom)
4220 int n_param, n_in, n_out;
4223 n_param = isl_multi_aff_dim(ma, isl_dim_param);
4224 n_in = isl_multi_aff_dim(ma, isl_dim_in);
4225 n_out = isl_multi_aff_dim(ma, isl_dim_out);
4227 isl_seq_cpy(dst, src, has_denom + 1 + n_param + n_before);
4228 o_dst = o_src = has_denom + 1 + n_param + n_before;
4229 isl_seq_clr(dst + o_dst, n_in);
4232 isl_seq_cpy(dst + o_dst, src + o_src, n_after);
4235 isl_seq_clr(dst + o_dst, n_div_ma);
4237 isl_seq_cpy(dst + o_dst, src + o_src, n_div_bmap);
4239 isl_int_set_si(f, 1);
4241 for (i = 0; i < n_out; ++i) {
4242 int offset = has_denom + 1 + n_param + n_before + i;
4244 if (isl_int_is_zero(src[offset]))
4246 isl_int_set(c1, ma->p[i]->v->el[0]);
4247 isl_int_mul(c2, f, src[offset]);
4248 isl_int_gcd(g, c1, c2);
4249 isl_int_divexact(c1, c1, g);
4250 isl_int_divexact(c2, c2, g);
4252 isl_int_mul(f, f, c1);
4255 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
4256 c2, ma->p[i]->v->el + o_src, 1 + n_param);
4257 o_dst += 1 + n_param;
4258 o_src += 1 + n_param;
4259 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_before);
4261 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
4262 c2, ma->p[i]->v->el + o_src, n_in);
4265 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_after);
4267 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
4268 c2, ma->p[i]->v->el + o_src, n_div_ma);
4271 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_div_bmap);
4273 isl_int_mul(dst[0], dst[0], c1);
4277 /* Compute the pullback of "aff" by the function represented by "ma".
4278 * In other words, plug in "ma" in "aff". The result is an affine expression
4279 * defined over the domain space of "ma".
4281 * If "aff" is represented by
4283 * (a(p) + b x + c(divs))/d
4285 * and ma is represented by
4287 * x = D(p) + F(y) + G(divs')
4289 * then the result is
4291 * (a(p) + b D(p) + b F(y) + b G(divs') + c(divs))/d
4293 * The divs in the local space of the input are similarly adjusted
4294 * through a call to isl_local_space_preimage_multi_aff.
4296 __isl_give isl_aff *isl_aff_pullback_multi_aff(__isl_take isl_aff *aff,
4297 __isl_take isl_multi_aff *ma)
4299 isl_aff *res = NULL;
4300 isl_local_space *ls;
4301 int n_div_aff, n_div_ma;
4302 isl_int f, c1, c2, g;
4304 ma = isl_multi_aff_align_divs(ma);
4308 n_div_aff = isl_aff_dim(aff, isl_dim_div);
4309 n_div_ma = ma->n ? isl_aff_dim(ma->p[0], isl_dim_div) : 0;
4311 ls = isl_aff_get_domain_local_space(aff);
4312 ls = isl_local_space_preimage_multi_aff(ls, isl_multi_aff_copy(ma));
4313 res = isl_aff_alloc(ls);
4322 isl_seq_preimage(res->v->el, aff->v->el, ma, 0, 0, n_div_ma, n_div_aff,
4331 isl_multi_aff_free(ma);
4332 res = isl_aff_normalize(res);
4336 isl_multi_aff_free(ma);
4341 /* Compute the pullback of "ma1" by the function represented by "ma2".
4342 * In other words, plug in "ma2" in "ma1".
4344 __isl_give isl_multi_aff *isl_multi_aff_pullback_multi_aff(
4345 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2)
4348 isl_space *space = NULL;
4350 ma2 = isl_multi_aff_align_divs(ma2);
4351 ma1 = isl_multi_aff_cow(ma1);
4355 space = isl_space_join(isl_multi_aff_get_space(ma2),
4356 isl_multi_aff_get_space(ma1));
4358 for (i = 0; i < ma1->n; ++i) {
4359 ma1->p[i] = isl_aff_pullback_multi_aff(ma1->p[i],
4360 isl_multi_aff_copy(ma2));
4365 ma1 = isl_multi_aff_reset_space(ma1, space);
4366 isl_multi_aff_free(ma2);
4369 isl_space_free(space);
4370 isl_multi_aff_free(ma2);
4371 isl_multi_aff_free(ma1);
4375 /* Extend the local space of "dst" to include the divs
4376 * in the local space of "src".
4378 __isl_give isl_aff *isl_aff_align_divs(__isl_take isl_aff *dst,
4379 __isl_keep isl_aff *src)
4387 return isl_aff_free(dst);
4389 ctx = isl_aff_get_ctx(src);
4390 if (!isl_space_is_equal(src->ls->dim, dst->ls->dim))
4391 isl_die(ctx, isl_error_invalid,
4392 "spaces don't match", goto error);
4394 if (src->ls->div->n_row == 0)
4397 exp1 = isl_alloc_array(ctx, int, src->ls->div->n_row);
4398 exp2 = isl_alloc_array(ctx, int, dst->ls->div->n_row);
4402 div = isl_merge_divs(src->ls->div, dst->ls->div, exp1, exp2);
4403 dst = isl_aff_expand_divs(dst, div, exp2);
4411 return isl_aff_free(dst);
4414 /* Adjust the local spaces of the affine expressions in "maff"
4415 * such that they all have the save divs.
4417 __isl_give isl_multi_aff *isl_multi_aff_align_divs(
4418 __isl_take isl_multi_aff *maff)
4426 maff = isl_multi_aff_cow(maff);
4430 for (i = 1; i < maff->n; ++i)
4431 maff->p[0] = isl_aff_align_divs(maff->p[0], maff->p[i]);
4432 for (i = 1; i < maff->n; ++i) {
4433 maff->p[i] = isl_aff_align_divs(maff->p[i], maff->p[0]);
4435 return isl_multi_aff_free(maff);
4441 __isl_give isl_aff *isl_aff_lift(__isl_take isl_aff *aff)
4443 aff = isl_aff_cow(aff);
4447 aff->ls = isl_local_space_lift(aff->ls);
4449 return isl_aff_free(aff);
4454 /* Lift "maff" to a space with extra dimensions such that the result
4455 * has no more existentially quantified variables.
4456 * If "ls" is not NULL, then *ls is assigned the local space that lies
4457 * at the basis of the lifting applied to "maff".
4459 __isl_give isl_multi_aff *isl_multi_aff_lift(__isl_take isl_multi_aff *maff,
4460 __isl_give isl_local_space **ls)
4474 isl_space *space = isl_multi_aff_get_domain_space(maff);
4475 *ls = isl_local_space_from_space(space);
4477 return isl_multi_aff_free(maff);
4482 maff = isl_multi_aff_cow(maff);
4483 maff = isl_multi_aff_align_divs(maff);
4487 n_div = isl_aff_dim(maff->p[0], isl_dim_div);
4488 space = isl_multi_aff_get_space(maff);
4489 space = isl_space_lift(isl_space_domain(space), n_div);
4490 space = isl_space_extend_domain_with_range(space,
4491 isl_multi_aff_get_space(maff));
4493 return isl_multi_aff_free(maff);
4494 isl_space_free(maff->space);
4495 maff->space = space;
4498 *ls = isl_aff_get_domain_local_space(maff->p[0]);
4500 return isl_multi_aff_free(maff);
4503 for (i = 0; i < maff->n; ++i) {
4504 maff->p[i] = isl_aff_lift(maff->p[i]);
4512 isl_local_space_free(*ls);
4513 return isl_multi_aff_free(maff);
4517 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma".
4519 __isl_give isl_pw_aff *isl_pw_multi_aff_get_pw_aff(
4520 __isl_keep isl_pw_multi_aff *pma, int pos)
4530 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
4531 if (pos < 0 || pos >= n_out)
4532 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
4533 "index out of bounds", return NULL);
4535 space = isl_pw_multi_aff_get_space(pma);
4536 space = isl_space_drop_dims(space, isl_dim_out,
4537 pos + 1, n_out - pos - 1);
4538 space = isl_space_drop_dims(space, isl_dim_out, 0, pos);
4540 pa = isl_pw_aff_alloc_size(space, pma->n);
4541 for (i = 0; i < pma->n; ++i) {
4543 aff = isl_multi_aff_get_aff(pma->p[i].maff, pos);
4544 pa = isl_pw_aff_add_piece(pa, isl_set_copy(pma->p[i].set), aff);
4550 /* Return an isl_pw_multi_aff with the given "set" as domain and
4551 * an unnamed zero-dimensional range.
4553 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_domain(
4554 __isl_take isl_set *set)
4559 space = isl_set_get_space(set);
4560 space = isl_space_from_domain(space);
4561 ma = isl_multi_aff_zero(space);
4562 return isl_pw_multi_aff_alloc(set, ma);
4565 /* Add an isl_pw_multi_aff with the given "set" as domain and
4566 * an unnamed zero-dimensional range to *user.
4568 static int add_pw_multi_aff_from_domain(__isl_take isl_set *set, void *user)
4570 isl_union_pw_multi_aff **upma = user;
4571 isl_pw_multi_aff *pma;
4573 pma = isl_pw_multi_aff_from_domain(set);
4574 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
4579 /* Return an isl_union_pw_multi_aff with the given "uset" as domain and
4580 * an unnamed zero-dimensional range.
4582 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_domain(
4583 __isl_take isl_union_set *uset)
4586 isl_union_pw_multi_aff *upma;
4591 space = isl_union_set_get_space(uset);
4592 upma = isl_union_pw_multi_aff_empty(space);
4594 if (isl_union_set_foreach_set(uset,
4595 &add_pw_multi_aff_from_domain, &upma) < 0)
4598 isl_union_set_free(uset);
4601 isl_union_set_free(uset);
4602 isl_union_pw_multi_aff_free(upma);
4606 /* Convert "pma" to an isl_map and add it to *umap.
4608 static int map_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma, void *user)
4610 isl_union_map **umap = user;
4613 map = isl_map_from_pw_multi_aff(pma);
4614 *umap = isl_union_map_add_map(*umap, map);
4619 /* Construct a union map mapping the domain of the union
4620 * piecewise multi-affine expression to its range, with each dimension
4621 * in the range equated to the corresponding affine expression on its cell.
4623 __isl_give isl_union_map *isl_union_map_from_union_pw_multi_aff(
4624 __isl_take isl_union_pw_multi_aff *upma)
4627 isl_union_map *umap;
4632 space = isl_union_pw_multi_aff_get_space(upma);
4633 umap = isl_union_map_empty(space);
4635 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
4636 &map_from_pw_multi_aff, &umap) < 0)
4639 isl_union_pw_multi_aff_free(upma);
4642 isl_union_pw_multi_aff_free(upma);
4643 isl_union_map_free(umap);
4647 /* Local data for bin_entry and the callback "fn".
4649 struct isl_union_pw_multi_aff_bin_data {
4650 isl_union_pw_multi_aff *upma2;
4651 isl_union_pw_multi_aff *res;
4652 isl_pw_multi_aff *pma;
4653 int (*fn)(void **entry, void *user);
4656 /* Given an isl_pw_multi_aff from upma1, store it in data->pma
4657 * and call data->fn for each isl_pw_multi_aff in data->upma2.
4659 static int bin_entry(void **entry, void *user)
4661 struct isl_union_pw_multi_aff_bin_data *data = user;
4662 isl_pw_multi_aff *pma = *entry;
4665 if (isl_hash_table_foreach(data->upma2->dim->ctx, &data->upma2->table,
4666 data->fn, data) < 0)
4672 /* Call "fn" on each pair of isl_pw_multi_affs in "upma1" and "upma2".
4673 * The isl_pw_multi_aff from upma1 is stored in data->pma (where data is
4674 * passed as user field) and the isl_pw_multi_aff from upma2 is available
4675 * as *entry. The callback should adjust data->res if desired.
4677 static __isl_give isl_union_pw_multi_aff *bin_op(
4678 __isl_take isl_union_pw_multi_aff *upma1,
4679 __isl_take isl_union_pw_multi_aff *upma2,
4680 int (*fn)(void **entry, void *user))
4683 struct isl_union_pw_multi_aff_bin_data data = { NULL, NULL, NULL, fn };
4685 space = isl_union_pw_multi_aff_get_space(upma2);
4686 upma1 = isl_union_pw_multi_aff_align_params(upma1, space);
4687 space = isl_union_pw_multi_aff_get_space(upma1);
4688 upma2 = isl_union_pw_multi_aff_align_params(upma2, space);
4690 if (!upma1 || !upma2)
4694 data.res = isl_union_pw_multi_aff_alloc(isl_space_copy(upma1->dim),
4696 if (isl_hash_table_foreach(upma1->dim->ctx, &upma1->table,
4697 &bin_entry, &data) < 0)
4700 isl_union_pw_multi_aff_free(upma1);
4701 isl_union_pw_multi_aff_free(upma2);
4704 isl_union_pw_multi_aff_free(upma1);
4705 isl_union_pw_multi_aff_free(upma2);
4706 isl_union_pw_multi_aff_free(data.res);
4710 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
4711 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
4713 static __isl_give isl_pw_multi_aff *pw_multi_aff_range_product(
4714 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4718 space = isl_space_range_product(isl_pw_multi_aff_get_space(pma1),
4719 isl_pw_multi_aff_get_space(pma2));
4720 return isl_pw_multi_aff_on_shared_domain_in(pma1, pma2, space,
4721 &isl_multi_aff_range_product);
4724 /* Given two isl_pw_multi_affs A -> B and C -> D,
4725 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
4727 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_range_product(
4728 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4730 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4731 &pw_multi_aff_range_product);
4734 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
4735 * construct an isl_pw_multi_aff (A * C) -> (B, D).
4737 static __isl_give isl_pw_multi_aff *pw_multi_aff_flat_range_product(
4738 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4742 space = isl_space_range_product(isl_pw_multi_aff_get_space(pma1),
4743 isl_pw_multi_aff_get_space(pma2));
4744 space = isl_space_flatten_range(space);
4745 return isl_pw_multi_aff_on_shared_domain_in(pma1, pma2, space,
4746 &isl_multi_aff_flat_range_product);
4749 /* Given two isl_pw_multi_affs A -> B and C -> D,
4750 * construct an isl_pw_multi_aff (A * C) -> (B, D).
4752 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_flat_range_product(
4753 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4755 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4756 &pw_multi_aff_flat_range_product);
4759 /* If data->pma and *entry have the same domain space, then compute
4760 * their flat range product and the result to data->res.
4762 static int flat_range_product_entry(void **entry, void *user)
4764 struct isl_union_pw_multi_aff_bin_data *data = user;
4765 isl_pw_multi_aff *pma2 = *entry;
4767 if (!isl_space_tuple_match(data->pma->dim, isl_dim_in,
4768 pma2->dim, isl_dim_in))
4771 pma2 = isl_pw_multi_aff_flat_range_product(
4772 isl_pw_multi_aff_copy(data->pma),
4773 isl_pw_multi_aff_copy(pma2));
4775 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma2);
4780 /* Given two isl_union_pw_multi_affs A -> B and C -> D,
4781 * construct an isl_union_pw_multi_aff (A * C) -> (B, D).
4783 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_flat_range_product(
4784 __isl_take isl_union_pw_multi_aff *upma1,
4785 __isl_take isl_union_pw_multi_aff *upma2)
4787 return bin_op(upma1, upma2, &flat_range_product_entry);
4790 /* Replace the affine expressions at position "pos" in "pma" by "pa".
4791 * The parameters are assumed to have been aligned.
4793 * The implementation essentially performs an isl_pw_*_on_shared_domain,
4794 * except that it works on two different isl_pw_* types.
4796 static __isl_give isl_pw_multi_aff *pw_multi_aff_set_pw_aff(
4797 __isl_take isl_pw_multi_aff *pma, unsigned pos,
4798 __isl_take isl_pw_aff *pa)
4801 isl_pw_multi_aff *res = NULL;
4806 if (!isl_space_tuple_match(pma->dim, isl_dim_in, pa->dim, isl_dim_in))
4807 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
4808 "domains don't match", goto error);
4809 if (pos >= isl_pw_multi_aff_dim(pma, isl_dim_out))
4810 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
4811 "index out of bounds", goto error);
4814 res = isl_pw_multi_aff_alloc_size(isl_pw_multi_aff_get_space(pma), n);
4816 for (i = 0; i < pma->n; ++i) {
4817 for (j = 0; j < pa->n; ++j) {
4819 isl_multi_aff *res_ij;
4822 common = isl_set_intersect(isl_set_copy(pma->p[i].set),
4823 isl_set_copy(pa->p[j].set));
4824 empty = isl_set_plain_is_empty(common);
4825 if (empty < 0 || empty) {
4826 isl_set_free(common);
4832 res_ij = isl_multi_aff_set_aff(
4833 isl_multi_aff_copy(pma->p[i].maff), pos,
4834 isl_aff_copy(pa->p[j].aff));
4835 res_ij = isl_multi_aff_gist(res_ij,
4836 isl_set_copy(common));
4838 res = isl_pw_multi_aff_add_piece(res, common, res_ij);
4842 isl_pw_multi_aff_free(pma);
4843 isl_pw_aff_free(pa);
4846 isl_pw_multi_aff_free(pma);
4847 isl_pw_aff_free(pa);
4848 return isl_pw_multi_aff_free(res);
4851 /* Replace the affine expressions at position "pos" in "pma" by "pa".
4853 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_set_pw_aff(
4854 __isl_take isl_pw_multi_aff *pma, unsigned pos,
4855 __isl_take isl_pw_aff *pa)
4859 if (isl_space_match(pma->dim, isl_dim_param, pa->dim, isl_dim_param))
4860 return pw_multi_aff_set_pw_aff(pma, pos, pa);
4861 if (!isl_space_has_named_params(pma->dim) ||
4862 !isl_space_has_named_params(pa->dim))
4863 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
4864 "unaligned unnamed parameters", goto error);
4865 pma = isl_pw_multi_aff_align_params(pma, isl_pw_aff_get_space(pa));
4866 pa = isl_pw_aff_align_params(pa, isl_pw_multi_aff_get_space(pma));
4867 return pw_multi_aff_set_pw_aff(pma, pos, pa);
4869 isl_pw_multi_aff_free(pma);
4870 isl_pw_aff_free(pa);
4874 /* Check that the domain space of "pa" matches "space".
4876 * Return 0 on success and -1 on error.
4878 int isl_pw_aff_check_match_domain_space(__isl_keep isl_pw_aff *pa,
4879 __isl_keep isl_space *space)
4881 isl_space *pa_space;
4887 pa_space = isl_pw_aff_get_space(pa);
4889 match = isl_space_match(space, isl_dim_param, pa_space, isl_dim_param);
4893 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
4894 "parameters don't match", goto error);
4895 match = isl_space_tuple_match(space, isl_dim_in, pa_space, isl_dim_in);
4899 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
4900 "domains don't match", goto error);
4901 isl_space_free(pa_space);
4904 isl_space_free(pa_space);
4911 #include <isl_multi_templ.c>
4913 /* Scale the first elements of "ma" by the corresponding elements of "vec".
4915 __isl_give isl_multi_aff *isl_multi_aff_scale_vec(__isl_take isl_multi_aff *ma,
4916 __isl_take isl_vec *vec)
4924 n = isl_multi_aff_dim(ma, isl_dim_out);
4925 if (isl_vec_size(vec) < n)
4926 n = isl_vec_size(vec);
4929 for (i = 0; i < n; ++i) {
4932 isl_vec_get_element(vec, i, &v);
4934 aff = isl_multi_aff_get_aff(ma, i);
4935 aff = isl_aff_scale(aff, v);
4936 ma = isl_multi_aff_set_aff(ma, i, aff);
4944 isl_multi_aff_free(ma);
4948 /* Scale the first elements of "pma" by the corresponding elements of "vec".
4950 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_scale_vec(
4951 __isl_take isl_pw_multi_aff *pma, __isl_take isl_vec *v)
4955 pma = isl_pw_multi_aff_cow(pma);
4959 for (i = 0; i < pma->n; ++i) {
4960 pma->p[i].maff = isl_multi_aff_scale_vec(pma->p[i].maff,
4962 if (!pma->p[i].maff)
4970 isl_pw_multi_aff_free(pma);
4974 /* This function is called for each entry of an isl_union_pw_multi_aff.
4975 * Replace the entry by the result of applying isl_pw_multi_aff_scale_vec
4976 * to the original entry with the isl_vec in "user" as extra argument.
4978 static int union_pw_multi_aff_scale_vec_entry(void **entry, void *user)
4980 isl_pw_multi_aff **pma = (isl_pw_multi_aff **) entry;
4983 *pma = isl_pw_multi_aff_scale_vec(*pma, isl_vec_copy(v));
4990 /* Scale the first elements of "upma" by the corresponding elements of "vec".
4992 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_scale_vec(
4993 __isl_take isl_union_pw_multi_aff *upma, __isl_take isl_vec *v)
4995 upma = isl_union_pw_multi_aff_cow(upma);
4999 if (isl_hash_table_foreach(upma->dim->ctx, &upma->table,
5000 &union_pw_multi_aff_scale_vec_entry, v) < 0)
5007 isl_union_pw_multi_aff_free(upma);