2 * Copyright 2011 INRIA Saclay
3 * Copyright 2011 Sven Verdoolaege
4 * Copyright 2012 Ecole Normale Superieure
6 * Use of this software is governed by the MIT license
8 * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
9 * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
11 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
14 #include <isl_ctx_private.h>
16 #include <isl_map_private.h>
17 #include <isl_union_map_private.h>
18 #include <isl_aff_private.h>
19 #include <isl_space_private.h>
20 #include <isl_local_space_private.h>
21 #include <isl_mat_private.h>
22 #include <isl_list_private.h>
23 #include <isl/constraint.h>
26 #include <isl_config.h>
28 __isl_give isl_aff *isl_aff_alloc_vec(__isl_take isl_local_space *ls,
29 __isl_take isl_vec *v)
36 aff = isl_calloc_type(v->ctx, struct isl_aff);
46 isl_local_space_free(ls);
51 __isl_give isl_aff *isl_aff_alloc(__isl_take isl_local_space *ls)
60 ctx = isl_local_space_get_ctx(ls);
61 if (!isl_local_space_divs_known(ls))
62 isl_die(ctx, isl_error_invalid, "local space has unknown divs",
64 if (!isl_local_space_is_set(ls))
65 isl_die(ctx, isl_error_invalid,
66 "domain of affine expression should be a set",
69 total = isl_local_space_dim(ls, isl_dim_all);
70 v = isl_vec_alloc(ctx, 1 + 1 + total);
71 return isl_aff_alloc_vec(ls, v);
73 isl_local_space_free(ls);
77 __isl_give isl_aff *isl_aff_zero_on_domain(__isl_take isl_local_space *ls)
81 aff = isl_aff_alloc(ls);
85 isl_int_set_si(aff->v->el[0], 1);
86 isl_seq_clr(aff->v->el + 1, aff->v->size - 1);
91 /* Return a piecewise affine expression defined on the specified domain
92 * that is equal to zero.
94 __isl_give isl_pw_aff *isl_pw_aff_zero_on_domain(__isl_take isl_local_space *ls)
96 return isl_pw_aff_from_aff(isl_aff_zero_on_domain(ls));
99 /* Return an affine expression that is equal to the specified dimension
102 __isl_give isl_aff *isl_aff_var_on_domain(__isl_take isl_local_space *ls,
103 enum isl_dim_type type, unsigned pos)
111 space = isl_local_space_get_space(ls);
114 if (isl_space_is_map(space))
115 isl_die(isl_space_get_ctx(space), isl_error_invalid,
116 "expecting (parameter) set space", goto error);
117 if (pos >= isl_local_space_dim(ls, type))
118 isl_die(isl_space_get_ctx(space), isl_error_invalid,
119 "position out of bounds", goto error);
121 isl_space_free(space);
122 aff = isl_aff_alloc(ls);
126 pos += isl_local_space_offset(aff->ls, type);
128 isl_int_set_si(aff->v->el[0], 1);
129 isl_seq_clr(aff->v->el + 1, aff->v->size - 1);
130 isl_int_set_si(aff->v->el[1 + pos], 1);
134 isl_local_space_free(ls);
135 isl_space_free(space);
139 /* Return a piecewise affine expression that is equal to
140 * the specified dimension in "ls".
142 __isl_give isl_pw_aff *isl_pw_aff_var_on_domain(__isl_take isl_local_space *ls,
143 enum isl_dim_type type, unsigned pos)
145 return isl_pw_aff_from_aff(isl_aff_var_on_domain(ls, type, pos));
148 __isl_give isl_aff *isl_aff_copy(__isl_keep isl_aff *aff)
157 __isl_give isl_aff *isl_aff_dup(__isl_keep isl_aff *aff)
162 return isl_aff_alloc_vec(isl_local_space_copy(aff->ls),
163 isl_vec_copy(aff->v));
166 __isl_give isl_aff *isl_aff_cow(__isl_take isl_aff *aff)
174 return isl_aff_dup(aff);
177 void *isl_aff_free(__isl_take isl_aff *aff)
185 isl_local_space_free(aff->ls);
186 isl_vec_free(aff->v);
193 isl_ctx *isl_aff_get_ctx(__isl_keep isl_aff *aff)
195 return aff ? isl_local_space_get_ctx(aff->ls) : NULL;
198 /* Externally, an isl_aff has a map space, but internally, the
199 * ls field corresponds to the domain of that space.
201 int isl_aff_dim(__isl_keep isl_aff *aff, enum isl_dim_type type)
205 if (type == isl_dim_out)
207 if (type == isl_dim_in)
209 return isl_local_space_dim(aff->ls, type);
212 __isl_give isl_space *isl_aff_get_domain_space(__isl_keep isl_aff *aff)
214 return aff ? isl_local_space_get_space(aff->ls) : NULL;
217 __isl_give isl_space *isl_aff_get_space(__isl_keep isl_aff *aff)
222 space = isl_local_space_get_space(aff->ls);
223 space = isl_space_from_domain(space);
224 space = isl_space_add_dims(space, isl_dim_out, 1);
228 __isl_give isl_local_space *isl_aff_get_domain_local_space(
229 __isl_keep isl_aff *aff)
231 return aff ? isl_local_space_copy(aff->ls) : NULL;
234 __isl_give isl_local_space *isl_aff_get_local_space(__isl_keep isl_aff *aff)
239 ls = isl_local_space_copy(aff->ls);
240 ls = isl_local_space_from_domain(ls);
241 ls = isl_local_space_add_dims(ls, isl_dim_out, 1);
245 /* Externally, an isl_aff has a map space, but internally, the
246 * ls field corresponds to the domain of that space.
248 const char *isl_aff_get_dim_name(__isl_keep isl_aff *aff,
249 enum isl_dim_type type, unsigned pos)
253 if (type == isl_dim_out)
255 if (type == isl_dim_in)
257 return isl_local_space_get_dim_name(aff->ls, type, pos);
260 __isl_give isl_aff *isl_aff_reset_domain_space(__isl_take isl_aff *aff,
261 __isl_take isl_space *dim)
263 aff = isl_aff_cow(aff);
267 aff->ls = isl_local_space_reset_space(aff->ls, dim);
269 return isl_aff_free(aff);
278 /* Reset the space of "aff". This function is called from isl_pw_templ.c
279 * and doesn't know if the space of an element object is represented
280 * directly or through its domain. It therefore passes along both.
282 __isl_give isl_aff *isl_aff_reset_space_and_domain(__isl_take isl_aff *aff,
283 __isl_take isl_space *space, __isl_take isl_space *domain)
285 isl_space_free(space);
286 return isl_aff_reset_domain_space(aff, domain);
289 /* Reorder the coefficients of the affine expression based
290 * on the given reodering.
291 * The reordering r is assumed to have been extended with the local
294 static __isl_give isl_vec *vec_reorder(__isl_take isl_vec *vec,
295 __isl_take isl_reordering *r, int n_div)
303 res = isl_vec_alloc(vec->ctx,
304 2 + isl_space_dim(r->dim, isl_dim_all) + n_div);
305 isl_seq_cpy(res->el, vec->el, 2);
306 isl_seq_clr(res->el + 2, res->size - 2);
307 for (i = 0; i < r->len; ++i)
308 isl_int_set(res->el[2 + r->pos[i]], vec->el[2 + i]);
310 isl_reordering_free(r);
315 isl_reordering_free(r);
319 /* Reorder the dimensions of the domain of "aff" according
320 * to the given reordering.
322 __isl_give isl_aff *isl_aff_realign_domain(__isl_take isl_aff *aff,
323 __isl_take isl_reordering *r)
325 aff = isl_aff_cow(aff);
329 r = isl_reordering_extend(r, aff->ls->div->n_row);
330 aff->v = vec_reorder(aff->v, isl_reordering_copy(r),
331 aff->ls->div->n_row);
332 aff->ls = isl_local_space_realign(aff->ls, r);
334 if (!aff->v || !aff->ls)
335 return isl_aff_free(aff);
340 isl_reordering_free(r);
344 __isl_give isl_aff *isl_aff_align_params(__isl_take isl_aff *aff,
345 __isl_take isl_space *model)
350 if (!isl_space_match(aff->ls->dim, isl_dim_param,
351 model, isl_dim_param)) {
354 model = isl_space_drop_dims(model, isl_dim_in,
355 0, isl_space_dim(model, isl_dim_in));
356 model = isl_space_drop_dims(model, isl_dim_out,
357 0, isl_space_dim(model, isl_dim_out));
358 exp = isl_parameter_alignment_reordering(aff->ls->dim, model);
359 exp = isl_reordering_extend_space(exp,
360 isl_aff_get_domain_space(aff));
361 aff = isl_aff_realign_domain(aff, exp);
364 isl_space_free(model);
367 isl_space_free(model);
372 int isl_aff_plain_is_zero(__isl_keep isl_aff *aff)
377 return isl_seq_first_non_zero(aff->v->el + 1, aff->v->size - 1) < 0;
380 int isl_aff_plain_is_equal(__isl_keep isl_aff *aff1, __isl_keep isl_aff *aff2)
387 equal = isl_local_space_is_equal(aff1->ls, aff2->ls);
388 if (equal < 0 || !equal)
391 return isl_vec_is_equal(aff1->v, aff2->v);
394 int isl_aff_get_denominator(__isl_keep isl_aff *aff, isl_int *v)
398 isl_int_set(*v, aff->v->el[0]);
402 int isl_aff_get_constant(__isl_keep isl_aff *aff, isl_int *v)
406 isl_int_set(*v, aff->v->el[1]);
410 int isl_aff_get_coefficient(__isl_keep isl_aff *aff,
411 enum isl_dim_type type, int pos, isl_int *v)
416 if (type == isl_dim_out)
417 isl_die(aff->v->ctx, isl_error_invalid,
418 "output/set dimension does not have a coefficient",
420 if (type == isl_dim_in)
423 if (pos >= isl_local_space_dim(aff->ls, type))
424 isl_die(aff->v->ctx, isl_error_invalid,
425 "position out of bounds", return -1);
427 pos += isl_local_space_offset(aff->ls, type);
428 isl_int_set(*v, aff->v->el[1 + pos]);
433 __isl_give isl_aff *isl_aff_set_denominator(__isl_take isl_aff *aff, isl_int v)
435 aff = isl_aff_cow(aff);
439 aff->v = isl_vec_cow(aff->v);
441 return isl_aff_free(aff);
443 isl_int_set(aff->v->el[0], v);
448 __isl_give isl_aff *isl_aff_set_constant(__isl_take isl_aff *aff, isl_int v)
450 aff = isl_aff_cow(aff);
454 aff->v = isl_vec_cow(aff->v);
456 return isl_aff_free(aff);
458 isl_int_set(aff->v->el[1], v);
463 __isl_give isl_aff *isl_aff_add_constant(__isl_take isl_aff *aff, isl_int v)
465 if (isl_int_is_zero(v))
468 aff = isl_aff_cow(aff);
472 aff->v = isl_vec_cow(aff->v);
474 return isl_aff_free(aff);
476 isl_int_addmul(aff->v->el[1], aff->v->el[0], v);
481 __isl_give isl_aff *isl_aff_add_constant_si(__isl_take isl_aff *aff, int v)
486 isl_int_set_si(t, v);
487 aff = isl_aff_add_constant(aff, t);
493 /* Add "v" to the numerator of the constant term of "aff".
495 __isl_give isl_aff *isl_aff_add_constant_num(__isl_take isl_aff *aff, isl_int v)
497 if (isl_int_is_zero(v))
500 aff = isl_aff_cow(aff);
504 aff->v = isl_vec_cow(aff->v);
506 return isl_aff_free(aff);
508 isl_int_add(aff->v->el[1], aff->v->el[1], v);
513 /* Add "v" to the numerator of the constant term of "aff".
515 __isl_give isl_aff *isl_aff_add_constant_num_si(__isl_take isl_aff *aff, int v)
523 isl_int_set_si(t, v);
524 aff = isl_aff_add_constant_num(aff, t);
530 __isl_give isl_aff *isl_aff_set_constant_si(__isl_take isl_aff *aff, int v)
532 aff = isl_aff_cow(aff);
536 aff->v = isl_vec_cow(aff->v);
538 return isl_aff_free(aff);
540 isl_int_set_si(aff->v->el[1], v);
545 __isl_give isl_aff *isl_aff_set_coefficient(__isl_take isl_aff *aff,
546 enum isl_dim_type type, int pos, isl_int v)
551 if (type == isl_dim_out)
552 isl_die(aff->v->ctx, isl_error_invalid,
553 "output/set dimension does not have a coefficient",
554 return isl_aff_free(aff));
555 if (type == isl_dim_in)
558 if (pos >= isl_local_space_dim(aff->ls, type))
559 isl_die(aff->v->ctx, isl_error_invalid,
560 "position out of bounds", return isl_aff_free(aff));
562 aff = isl_aff_cow(aff);
566 aff->v = isl_vec_cow(aff->v);
568 return isl_aff_free(aff);
570 pos += isl_local_space_offset(aff->ls, type);
571 isl_int_set(aff->v->el[1 + pos], v);
576 __isl_give isl_aff *isl_aff_set_coefficient_si(__isl_take isl_aff *aff,
577 enum isl_dim_type type, int pos, int v)
582 if (type == isl_dim_out)
583 isl_die(aff->v->ctx, isl_error_invalid,
584 "output/set dimension does not have a coefficient",
585 return isl_aff_free(aff));
586 if (type == isl_dim_in)
589 if (pos >= isl_local_space_dim(aff->ls, type))
590 isl_die(aff->v->ctx, isl_error_invalid,
591 "position out of bounds", return isl_aff_free(aff));
593 aff = isl_aff_cow(aff);
597 aff->v = isl_vec_cow(aff->v);
599 return isl_aff_free(aff);
601 pos += isl_local_space_offset(aff->ls, type);
602 isl_int_set_si(aff->v->el[1 + pos], v);
607 __isl_give isl_aff *isl_aff_add_coefficient(__isl_take isl_aff *aff,
608 enum isl_dim_type type, int pos, isl_int v)
613 if (type == isl_dim_out)
614 isl_die(aff->v->ctx, isl_error_invalid,
615 "output/set dimension does not have a coefficient",
616 return isl_aff_free(aff));
617 if (type == isl_dim_in)
620 if (pos >= isl_local_space_dim(aff->ls, type))
621 isl_die(aff->v->ctx, isl_error_invalid,
622 "position out of bounds", return isl_aff_free(aff));
624 aff = isl_aff_cow(aff);
628 aff->v = isl_vec_cow(aff->v);
630 return isl_aff_free(aff);
632 pos += isl_local_space_offset(aff->ls, type);
633 isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v);
638 __isl_give isl_aff *isl_aff_add_coefficient_si(__isl_take isl_aff *aff,
639 enum isl_dim_type type, int pos, int v)
644 isl_int_set_si(t, v);
645 aff = isl_aff_add_coefficient(aff, type, pos, t);
651 __isl_give isl_aff *isl_aff_get_div(__isl_keep isl_aff *aff, int pos)
656 return isl_local_space_get_div(aff->ls, pos);
659 __isl_give isl_aff *isl_aff_neg(__isl_take isl_aff *aff)
661 aff = isl_aff_cow(aff);
664 aff->v = isl_vec_cow(aff->v);
666 return isl_aff_free(aff);
668 isl_seq_neg(aff->v->el + 1, aff->v->el + 1, aff->v->size - 1);
673 /* Remove divs from the local space that do not appear in the affine
675 * We currently only remove divs at the end.
676 * Some intermediate divs may also not appear directly in the affine
677 * expression, but we would also need to check that no other divs are
678 * defined in terms of them.
680 __isl_give isl_aff *isl_aff_remove_unused_divs( __isl_take isl_aff *aff)
689 n = isl_local_space_dim(aff->ls, isl_dim_div);
690 off = isl_local_space_offset(aff->ls, isl_dim_div);
692 pos = isl_seq_last_non_zero(aff->v->el + 1 + off, n) + 1;
696 aff = isl_aff_cow(aff);
700 aff->ls = isl_local_space_drop_dims(aff->ls, isl_dim_div, pos, n - pos);
701 aff->v = isl_vec_drop_els(aff->v, 1 + off + pos, n - pos);
702 if (!aff->ls || !aff->v)
703 return isl_aff_free(aff);
708 /* Given two affine expressions "p" of length p_len (including the
709 * denominator and the constant term) and "subs" of length subs_len,
710 * plug in "subs" for the variable at position "pos".
711 * The variables of "subs" and "p" are assumed to match up to subs_len,
712 * but "p" may have additional variables.
713 * "v" is an initialized isl_int that can be used internally.
715 * In particular, if "p" represents the expression
719 * with i the variable at position "pos" and "subs" represents the expression
723 * then the result represents the expression
728 void isl_seq_substitute(isl_int *p, int pos, isl_int *subs,
729 int p_len, int subs_len, isl_int v)
731 isl_int_set(v, p[1 + pos]);
732 isl_int_set_si(p[1 + pos], 0);
733 isl_seq_combine(p + 1, subs[0], p + 1, v, subs + 1, subs_len - 1);
734 isl_seq_scale(p + subs_len, p + subs_len, subs[0], p_len - subs_len);
735 isl_int_mul(p[0], p[0], subs[0]);
738 /* Look for any divs in the aff->ls with a denominator equal to one
739 * and plug them into the affine expression and any subsequent divs
740 * that may reference the div.
742 static __isl_give isl_aff *plug_in_integral_divs(__isl_take isl_aff *aff)
754 n = isl_local_space_dim(aff->ls, isl_dim_div);
756 for (i = 0; i < n; ++i) {
757 if (!isl_int_is_one(aff->ls->div->row[i][0]))
759 ls = isl_local_space_copy(aff->ls);
760 ls = isl_local_space_substitute_seq(ls, isl_dim_div, i,
761 aff->ls->div->row[i], len, i + 1);
762 vec = isl_vec_copy(aff->v);
763 vec = isl_vec_cow(vec);
769 pos = isl_local_space_offset(aff->ls, isl_dim_div) + i;
770 isl_seq_substitute(vec->el, pos, aff->ls->div->row[i],
775 isl_vec_free(aff->v);
777 isl_local_space_free(aff->ls);
784 isl_local_space_free(ls);
785 return isl_aff_free(aff);
788 /* Swap divs "a" and "b" in "aff", which is assumed to be non-NULL.
790 * Even though this function is only called on isl_affs with a single
791 * reference, we are careful to only change aff->v and aff->ls together.
793 static __isl_give isl_aff *swap_div(__isl_take isl_aff *aff, int a, int b)
795 unsigned off = isl_local_space_offset(aff->ls, isl_dim_div);
799 ls = isl_local_space_copy(aff->ls);
800 ls = isl_local_space_swap_div(ls, a, b);
801 v = isl_vec_copy(aff->v);
806 isl_int_swap(v->el[1 + off + a], v->el[1 + off + b]);
807 isl_vec_free(aff->v);
809 isl_local_space_free(aff->ls);
815 isl_local_space_free(ls);
816 return isl_aff_free(aff);
819 /* Merge divs "a" and "b" in "aff", which is assumed to be non-NULL.
821 * We currently do not actually remove div "b", but simply add its
822 * coefficient to that of "a" and then zero it out.
824 static __isl_give isl_aff *merge_divs(__isl_take isl_aff *aff, int a, int b)
826 unsigned off = isl_local_space_offset(aff->ls, isl_dim_div);
828 if (isl_int_is_zero(aff->v->el[1 + off + b]))
831 aff->v = isl_vec_cow(aff->v);
833 return isl_aff_free(aff);
835 isl_int_add(aff->v->el[1 + off + a],
836 aff->v->el[1 + off + a], aff->v->el[1 + off + b]);
837 isl_int_set_si(aff->v->el[1 + off + b], 0);
842 /* Sort the divs in the local space of "aff" according to
843 * the comparison function "cmp_row" in isl_local_space.c,
844 * combining the coefficients of identical divs.
846 * Reordering divs does not change the semantics of "aff",
847 * so there is no need to call isl_aff_cow.
848 * Moreover, this function is currently only called on isl_affs
849 * with a single reference.
851 static __isl_give isl_aff *sort_divs(__isl_take isl_aff *aff)
859 off = isl_local_space_offset(aff->ls, isl_dim_div);
860 n = isl_aff_dim(aff, isl_dim_div);
861 for (i = 1; i < n; ++i) {
862 for (j = i - 1; j >= 0; --j) {
863 int cmp = isl_mat_cmp_div(aff->ls->div, j, j + 1);
867 aff = merge_divs(aff, j, j + 1);
869 aff = swap_div(aff, j, j + 1);
878 /* Normalize the representation of "aff".
880 * This function should only be called of "new" isl_affs, i.e.,
881 * with only a single reference. We therefore do not need to
882 * worry about affecting other instances.
884 __isl_give isl_aff *isl_aff_normalize(__isl_take isl_aff *aff)
888 aff->v = isl_vec_normalize(aff->v);
890 return isl_aff_free(aff);
891 aff = plug_in_integral_divs(aff);
892 aff = sort_divs(aff);
893 aff = isl_aff_remove_unused_divs(aff);
897 /* Given f, return floor(f).
898 * If f is an integer expression, then just return f.
899 * If f is a constant, then return the constant floor(f).
900 * Otherwise, if f = g/m, write g = q m + r,
901 * create a new div d = [r/m] and return the expression q + d.
902 * The coefficients in r are taken to lie between -m/2 and m/2.
904 __isl_give isl_aff *isl_aff_floor(__isl_take isl_aff *aff)
914 if (isl_int_is_one(aff->v->el[0]))
917 aff = isl_aff_cow(aff);
921 aff->v = isl_vec_cow(aff->v);
923 return isl_aff_free(aff);
925 if (isl_aff_is_cst(aff)) {
926 isl_int_fdiv_q(aff->v->el[1], aff->v->el[1], aff->v->el[0]);
927 isl_int_set_si(aff->v->el[0], 1);
931 div = isl_vec_copy(aff->v);
932 div = isl_vec_cow(div);
934 return isl_aff_free(aff);
936 ctx = isl_aff_get_ctx(aff);
937 isl_int_fdiv_q(aff->v->el[0], aff->v->el[0], ctx->two);
938 for (i = 1; i < aff->v->size; ++i) {
939 isl_int_fdiv_r(div->el[i], div->el[i], div->el[0]);
940 isl_int_fdiv_q(aff->v->el[i], aff->v->el[i], div->el[0]);
941 if (isl_int_gt(div->el[i], aff->v->el[0])) {
942 isl_int_sub(div->el[i], div->el[i], div->el[0]);
943 isl_int_add_ui(aff->v->el[i], aff->v->el[i], 1);
947 aff->ls = isl_local_space_add_div(aff->ls, div);
949 return isl_aff_free(aff);
952 aff->v = isl_vec_extend(aff->v, size + 1);
954 return isl_aff_free(aff);
955 isl_int_set_si(aff->v->el[0], 1);
956 isl_int_set_si(aff->v->el[size], 1);
963 * aff mod m = aff - m * floor(aff/m)
965 __isl_give isl_aff *isl_aff_mod(__isl_take isl_aff *aff, isl_int m)
969 res = isl_aff_copy(aff);
970 aff = isl_aff_scale_down(aff, m);
971 aff = isl_aff_floor(aff);
972 aff = isl_aff_scale(aff, m);
973 res = isl_aff_sub(res, aff);
980 * pwaff mod m = pwaff - m * floor(pwaff/m)
982 __isl_give isl_pw_aff *isl_pw_aff_mod(__isl_take isl_pw_aff *pwaff, isl_int m)
986 res = isl_pw_aff_copy(pwaff);
987 pwaff = isl_pw_aff_scale_down(pwaff, m);
988 pwaff = isl_pw_aff_floor(pwaff);
989 pwaff = isl_pw_aff_scale(pwaff, m);
990 res = isl_pw_aff_sub(res, pwaff);
995 /* Given f, return ceil(f).
996 * If f is an integer expression, then just return f.
997 * Otherwise, create a new div d = [-f] and return the expression -d.
999 __isl_give isl_aff *isl_aff_ceil(__isl_take isl_aff *aff)
1004 if (isl_int_is_one(aff->v->el[0]))
1007 aff = isl_aff_neg(aff);
1008 aff = isl_aff_floor(aff);
1009 aff = isl_aff_neg(aff);
1014 /* Apply the expansion computed by isl_merge_divs.
1015 * The expansion itself is given by "exp" while the resulting
1016 * list of divs is given by "div".
1018 __isl_give isl_aff *isl_aff_expand_divs( __isl_take isl_aff *aff,
1019 __isl_take isl_mat *div, int *exp)
1026 aff = isl_aff_cow(aff);
1030 old_n_div = isl_local_space_dim(aff->ls, isl_dim_div);
1031 new_n_div = isl_mat_rows(div);
1032 if (new_n_div < old_n_div)
1033 isl_die(isl_mat_get_ctx(div), isl_error_invalid,
1034 "not an expansion", goto error);
1036 aff->v = isl_vec_extend(aff->v, aff->v->size + new_n_div - old_n_div);
1040 offset = 1 + isl_local_space_offset(aff->ls, isl_dim_div);
1042 for (i = new_n_div - 1; i >= 0; --i) {
1043 if (j >= 0 && exp[j] == i) {
1045 isl_int_swap(aff->v->el[offset + i],
1046 aff->v->el[offset + j]);
1049 isl_int_set_si(aff->v->el[offset + i], 0);
1052 aff->ls = isl_local_space_replace_divs(aff->ls, isl_mat_copy(div));
1063 /* Add two affine expressions that live in the same local space.
1065 static __isl_give isl_aff *add_expanded(__isl_take isl_aff *aff1,
1066 __isl_take isl_aff *aff2)
1070 aff1 = isl_aff_cow(aff1);
1074 aff1->v = isl_vec_cow(aff1->v);
1080 isl_int_gcd(gcd, aff1->v->el[0], aff2->v->el[0]);
1081 isl_int_divexact(f, aff2->v->el[0], gcd);
1082 isl_seq_scale(aff1->v->el + 1, aff1->v->el + 1, f, aff1->v->size - 1);
1083 isl_int_divexact(f, aff1->v->el[0], gcd);
1084 isl_seq_addmul(aff1->v->el + 1, f, aff2->v->el + 1, aff1->v->size - 1);
1085 isl_int_divexact(f, aff2->v->el[0], gcd);
1086 isl_int_mul(aff1->v->el[0], aff1->v->el[0], f);
1098 __isl_give isl_aff *isl_aff_add(__isl_take isl_aff *aff1,
1099 __isl_take isl_aff *aff2)
1109 ctx = isl_aff_get_ctx(aff1);
1110 if (!isl_space_is_equal(aff1->ls->dim, aff2->ls->dim))
1111 isl_die(ctx, isl_error_invalid,
1112 "spaces don't match", goto error);
1114 if (aff1->ls->div->n_row == 0 && aff2->ls->div->n_row == 0)
1115 return add_expanded(aff1, aff2);
1117 exp1 = isl_alloc_array(ctx, int, aff1->ls->div->n_row);
1118 exp2 = isl_alloc_array(ctx, int, aff2->ls->div->n_row);
1122 div = isl_merge_divs(aff1->ls->div, aff2->ls->div, exp1, exp2);
1123 aff1 = isl_aff_expand_divs(aff1, isl_mat_copy(div), exp1);
1124 aff2 = isl_aff_expand_divs(aff2, div, exp2);
1128 return add_expanded(aff1, aff2);
1137 __isl_give isl_aff *isl_aff_sub(__isl_take isl_aff *aff1,
1138 __isl_take isl_aff *aff2)
1140 return isl_aff_add(aff1, isl_aff_neg(aff2));
1143 __isl_give isl_aff *isl_aff_scale(__isl_take isl_aff *aff, isl_int f)
1147 if (isl_int_is_one(f))
1150 aff = isl_aff_cow(aff);
1153 aff->v = isl_vec_cow(aff->v);
1155 return isl_aff_free(aff);
1158 isl_int_gcd(gcd, aff->v->el[0], f);
1159 isl_int_divexact(aff->v->el[0], aff->v->el[0], gcd);
1160 isl_int_divexact(gcd, f, gcd);
1161 isl_seq_scale(aff->v->el + 1, aff->v->el + 1, gcd, aff->v->size - 1);
1167 __isl_give isl_aff *isl_aff_scale_down(__isl_take isl_aff *aff, isl_int f)
1171 if (isl_int_is_one(f))
1174 aff = isl_aff_cow(aff);
1178 if (isl_int_is_zero(f))
1179 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1180 "cannot scale down by zero", return isl_aff_free(aff));
1182 aff->v = isl_vec_cow(aff->v);
1184 return isl_aff_free(aff);
1187 isl_seq_gcd(aff->v->el + 1, aff->v->size - 1, &gcd);
1188 isl_int_gcd(gcd, gcd, f);
1189 isl_seq_scale_down(aff->v->el + 1, aff->v->el + 1, gcd, aff->v->size - 1);
1190 isl_int_divexact(gcd, f, gcd);
1191 isl_int_mul(aff->v->el[0], aff->v->el[0], gcd);
1197 __isl_give isl_aff *isl_aff_scale_down_ui(__isl_take isl_aff *aff, unsigned f)
1205 isl_int_set_ui(v, f);
1206 aff = isl_aff_scale_down(aff, v);
1212 __isl_give isl_aff *isl_aff_set_dim_name(__isl_take isl_aff *aff,
1213 enum isl_dim_type type, unsigned pos, const char *s)
1215 aff = isl_aff_cow(aff);
1218 if (type == isl_dim_out)
1219 isl_die(aff->v->ctx, isl_error_invalid,
1220 "cannot set name of output/set dimension",
1221 return isl_aff_free(aff));
1222 if (type == isl_dim_in)
1224 aff->ls = isl_local_space_set_dim_name(aff->ls, type, pos, s);
1226 return isl_aff_free(aff);
1231 __isl_give isl_aff *isl_aff_set_dim_id(__isl_take isl_aff *aff,
1232 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
1234 aff = isl_aff_cow(aff);
1236 return isl_id_free(id);
1237 if (type == isl_dim_out)
1238 isl_die(aff->v->ctx, isl_error_invalid,
1239 "cannot set name of output/set dimension",
1241 if (type == isl_dim_in)
1243 aff->ls = isl_local_space_set_dim_id(aff->ls, type, pos, id);
1245 return isl_aff_free(aff);
1254 /* Exploit the equalities in "eq" to simplify the affine expression
1255 * and the expressions of the integer divisions in the local space.
1256 * The integer divisions in this local space are assumed to appear
1257 * as regular dimensions in "eq".
1259 static __isl_give isl_aff *isl_aff_substitute_equalities_lifted(
1260 __isl_take isl_aff *aff, __isl_take isl_basic_set *eq)
1268 if (eq->n_eq == 0) {
1269 isl_basic_set_free(eq);
1273 aff = isl_aff_cow(aff);
1277 aff->ls = isl_local_space_substitute_equalities(aff->ls,
1278 isl_basic_set_copy(eq));
1282 total = 1 + isl_space_dim(eq->dim, isl_dim_all);
1284 for (i = 0; i < eq->n_eq; ++i) {
1285 j = isl_seq_last_non_zero(eq->eq[i], total + n_div);
1286 if (j < 0 || j == 0 || j >= total)
1289 isl_seq_elim(aff->v->el + 1, eq->eq[i], j, total,
1293 isl_basic_set_free(eq);
1294 aff = isl_aff_normalize(aff);
1297 isl_basic_set_free(eq);
1302 /* Exploit the equalities in "eq" to simplify the affine expression
1303 * and the expressions of the integer divisions in the local space.
1305 static __isl_give isl_aff *isl_aff_substitute_equalities(
1306 __isl_take isl_aff *aff, __isl_take isl_basic_set *eq)
1312 n_div = isl_local_space_dim(aff->ls, isl_dim_div);
1314 eq = isl_basic_set_add(eq, isl_dim_set, n_div);
1315 return isl_aff_substitute_equalities_lifted(aff, eq);
1317 isl_basic_set_free(eq);
1322 /* Look for equalities among the variables shared by context and aff
1323 * and the integer divisions of aff, if any.
1324 * The equalities are then used to eliminate coefficients and/or integer
1325 * divisions from aff.
1327 __isl_give isl_aff *isl_aff_gist(__isl_take isl_aff *aff,
1328 __isl_take isl_set *context)
1330 isl_basic_set *hull;
1335 n_div = isl_local_space_dim(aff->ls, isl_dim_div);
1337 isl_basic_set *bset;
1338 isl_local_space *ls;
1339 context = isl_set_add_dims(context, isl_dim_set, n_div);
1340 ls = isl_aff_get_domain_local_space(aff);
1341 bset = isl_basic_set_from_local_space(ls);
1342 bset = isl_basic_set_lift(bset);
1343 bset = isl_basic_set_flatten(bset);
1344 context = isl_set_intersect(context,
1345 isl_set_from_basic_set(bset));
1348 hull = isl_set_affine_hull(context);
1349 return isl_aff_substitute_equalities_lifted(aff, hull);
1352 isl_set_free(context);
1356 __isl_give isl_aff *isl_aff_gist_params(__isl_take isl_aff *aff,
1357 __isl_take isl_set *context)
1359 isl_set *dom_context = isl_set_universe(isl_aff_get_domain_space(aff));
1360 dom_context = isl_set_intersect_params(dom_context, context);
1361 return isl_aff_gist(aff, dom_context);
1364 /* Return a basic set containing those elements in the space
1365 * of aff where it is non-negative.
1367 __isl_give isl_basic_set *isl_aff_nonneg_basic_set(__isl_take isl_aff *aff)
1369 isl_constraint *ineq;
1370 isl_basic_set *bset;
1372 ineq = isl_inequality_from_aff(aff);
1374 bset = isl_basic_set_from_constraint(ineq);
1375 bset = isl_basic_set_simplify(bset);
1379 /* Return a basic set containing those elements in the domain space
1380 * of aff where it is negative.
1382 __isl_give isl_basic_set *isl_aff_neg_basic_set(__isl_take isl_aff *aff)
1384 aff = isl_aff_neg(aff);
1385 aff = isl_aff_add_constant_num_si(aff, -1);
1386 return isl_aff_nonneg_basic_set(aff);
1389 /* Return a basic set containing those elements in the space
1390 * of aff where it is zero.
1392 __isl_give isl_basic_set *isl_aff_zero_basic_set(__isl_take isl_aff *aff)
1394 isl_constraint *ineq;
1395 isl_basic_set *bset;
1397 ineq = isl_equality_from_aff(aff);
1399 bset = isl_basic_set_from_constraint(ineq);
1400 bset = isl_basic_set_simplify(bset);
1404 /* Return a basic set containing those elements in the shared space
1405 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
1407 __isl_give isl_basic_set *isl_aff_ge_basic_set(__isl_take isl_aff *aff1,
1408 __isl_take isl_aff *aff2)
1410 aff1 = isl_aff_sub(aff1, aff2);
1412 return isl_aff_nonneg_basic_set(aff1);
1415 /* Return a basic set containing those elements in the shared space
1416 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
1418 __isl_give isl_basic_set *isl_aff_le_basic_set(__isl_take isl_aff *aff1,
1419 __isl_take isl_aff *aff2)
1421 return isl_aff_ge_basic_set(aff2, aff1);
1424 __isl_give isl_aff *isl_aff_add_on_domain(__isl_keep isl_set *dom,
1425 __isl_take isl_aff *aff1, __isl_take isl_aff *aff2)
1427 aff1 = isl_aff_add(aff1, aff2);
1428 aff1 = isl_aff_gist(aff1, isl_set_copy(dom));
1432 int isl_aff_is_empty(__isl_keep isl_aff *aff)
1440 /* Check whether the given affine expression has non-zero coefficient
1441 * for any dimension in the given range or if any of these dimensions
1442 * appear with non-zero coefficients in any of the integer divisions
1443 * involved in the affine expression.
1445 int isl_aff_involves_dims(__isl_keep isl_aff *aff,
1446 enum isl_dim_type type, unsigned first, unsigned n)
1458 ctx = isl_aff_get_ctx(aff);
1459 if (first + n > isl_aff_dim(aff, type))
1460 isl_die(ctx, isl_error_invalid,
1461 "range out of bounds", return -1);
1463 active = isl_local_space_get_active(aff->ls, aff->v->el + 2);
1467 first += isl_local_space_offset(aff->ls, type) - 1;
1468 for (i = 0; i < n; ++i)
1469 if (active[first + i]) {
1482 __isl_give isl_aff *isl_aff_drop_dims(__isl_take isl_aff *aff,
1483 enum isl_dim_type type, unsigned first, unsigned n)
1489 if (type == isl_dim_out)
1490 isl_die(aff->v->ctx, isl_error_invalid,
1491 "cannot drop output/set dimension",
1492 return isl_aff_free(aff));
1493 if (type == isl_dim_in)
1495 if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
1498 ctx = isl_aff_get_ctx(aff);
1499 if (first + n > isl_local_space_dim(aff->ls, type))
1500 isl_die(ctx, isl_error_invalid, "range out of bounds",
1501 return isl_aff_free(aff));
1503 aff = isl_aff_cow(aff);
1507 aff->ls = isl_local_space_drop_dims(aff->ls, type, first, n);
1509 return isl_aff_free(aff);
1511 first += 1 + isl_local_space_offset(aff->ls, type);
1512 aff->v = isl_vec_drop_els(aff->v, first, n);
1514 return isl_aff_free(aff);
1519 /* Project the domain of the affine expression onto its parameter space.
1520 * The affine expression may not involve any of the domain dimensions.
1522 __isl_give isl_aff *isl_aff_project_domain_on_params(__isl_take isl_aff *aff)
1528 n = isl_aff_dim(aff, isl_dim_in);
1529 involves = isl_aff_involves_dims(aff, isl_dim_in, 0, n);
1531 return isl_aff_free(aff);
1533 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1534 "affine expression involves some of the domain dimensions",
1535 return isl_aff_free(aff));
1536 aff = isl_aff_drop_dims(aff, isl_dim_in, 0, n);
1537 space = isl_aff_get_domain_space(aff);
1538 space = isl_space_params(space);
1539 aff = isl_aff_reset_domain_space(aff, space);
1543 __isl_give isl_aff *isl_aff_insert_dims(__isl_take isl_aff *aff,
1544 enum isl_dim_type type, unsigned first, unsigned n)
1550 if (type == isl_dim_out)
1551 isl_die(aff->v->ctx, isl_error_invalid,
1552 "cannot insert output/set dimensions",
1553 return isl_aff_free(aff));
1554 if (type == isl_dim_in)
1556 if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
1559 ctx = isl_aff_get_ctx(aff);
1560 if (first > isl_local_space_dim(aff->ls, type))
1561 isl_die(ctx, isl_error_invalid, "position out of bounds",
1562 return isl_aff_free(aff));
1564 aff = isl_aff_cow(aff);
1568 aff->ls = isl_local_space_insert_dims(aff->ls, type, first, n);
1570 return isl_aff_free(aff);
1572 first += 1 + isl_local_space_offset(aff->ls, type);
1573 aff->v = isl_vec_insert_zero_els(aff->v, first, n);
1575 return isl_aff_free(aff);
1580 __isl_give isl_aff *isl_aff_add_dims(__isl_take isl_aff *aff,
1581 enum isl_dim_type type, unsigned n)
1585 pos = isl_aff_dim(aff, type);
1587 return isl_aff_insert_dims(aff, type, pos, n);
1590 __isl_give isl_pw_aff *isl_pw_aff_add_dims(__isl_take isl_pw_aff *pwaff,
1591 enum isl_dim_type type, unsigned n)
1595 pos = isl_pw_aff_dim(pwaff, type);
1597 return isl_pw_aff_insert_dims(pwaff, type, pos, n);
1600 __isl_give isl_pw_aff *isl_pw_aff_from_aff(__isl_take isl_aff *aff)
1602 isl_set *dom = isl_set_universe(isl_aff_get_domain_space(aff));
1603 return isl_pw_aff_alloc(dom, aff);
1607 #define PW isl_pw_aff
1611 #define EL_IS_ZERO is_empty
1615 #define IS_ZERO is_empty
1618 #undef DEFAULT_IS_ZERO
1619 #define DEFAULT_IS_ZERO 0
1623 #define NO_MOVE_DIMS
1627 #include <isl_pw_templ.c>
1629 static __isl_give isl_set *align_params_pw_pw_set_and(
1630 __isl_take isl_pw_aff *pwaff1, __isl_take isl_pw_aff *pwaff2,
1631 __isl_give isl_set *(*fn)(__isl_take isl_pw_aff *pwaff1,
1632 __isl_take isl_pw_aff *pwaff2))
1634 if (!pwaff1 || !pwaff2)
1636 if (isl_space_match(pwaff1->dim, isl_dim_param,
1637 pwaff2->dim, isl_dim_param))
1638 return fn(pwaff1, pwaff2);
1639 if (!isl_space_has_named_params(pwaff1->dim) ||
1640 !isl_space_has_named_params(pwaff2->dim))
1641 isl_die(isl_pw_aff_get_ctx(pwaff1), isl_error_invalid,
1642 "unaligned unnamed parameters", goto error);
1643 pwaff1 = isl_pw_aff_align_params(pwaff1, isl_pw_aff_get_space(pwaff2));
1644 pwaff2 = isl_pw_aff_align_params(pwaff2, isl_pw_aff_get_space(pwaff1));
1645 return fn(pwaff1, pwaff2);
1647 isl_pw_aff_free(pwaff1);
1648 isl_pw_aff_free(pwaff2);
1652 /* Compute a piecewise quasi-affine expression with a domain that
1653 * is the union of those of pwaff1 and pwaff2 and such that on each
1654 * cell, the quasi-affine expression is the better (according to cmp)
1655 * of those of pwaff1 and pwaff2. If only one of pwaff1 or pwaff2
1656 * is defined on a given cell, then the associated expression
1657 * is the defined one.
1659 static __isl_give isl_pw_aff *pw_aff_union_opt(__isl_take isl_pw_aff *pwaff1,
1660 __isl_take isl_pw_aff *pwaff2,
1661 __isl_give isl_basic_set *(*cmp)(__isl_take isl_aff *aff1,
1662 __isl_take isl_aff *aff2))
1669 if (!pwaff1 || !pwaff2)
1672 ctx = isl_space_get_ctx(pwaff1->dim);
1673 if (!isl_space_is_equal(pwaff1->dim, pwaff2->dim))
1674 isl_die(ctx, isl_error_invalid,
1675 "arguments should live in same space", goto error);
1677 if (isl_pw_aff_is_empty(pwaff1)) {
1678 isl_pw_aff_free(pwaff1);
1682 if (isl_pw_aff_is_empty(pwaff2)) {
1683 isl_pw_aff_free(pwaff2);
1687 n = 2 * (pwaff1->n + 1) * (pwaff2->n + 1);
1688 res = isl_pw_aff_alloc_size(isl_space_copy(pwaff1->dim), n);
1690 for (i = 0; i < pwaff1->n; ++i) {
1691 set = isl_set_copy(pwaff1->p[i].set);
1692 for (j = 0; j < pwaff2->n; ++j) {
1693 struct isl_set *common;
1696 common = isl_set_intersect(
1697 isl_set_copy(pwaff1->p[i].set),
1698 isl_set_copy(pwaff2->p[j].set));
1699 better = isl_set_from_basic_set(cmp(
1700 isl_aff_copy(pwaff2->p[j].aff),
1701 isl_aff_copy(pwaff1->p[i].aff)));
1702 better = isl_set_intersect(common, better);
1703 if (isl_set_plain_is_empty(better)) {
1704 isl_set_free(better);
1707 set = isl_set_subtract(set, isl_set_copy(better));
1709 res = isl_pw_aff_add_piece(res, better,
1710 isl_aff_copy(pwaff2->p[j].aff));
1712 res = isl_pw_aff_add_piece(res, set,
1713 isl_aff_copy(pwaff1->p[i].aff));
1716 for (j = 0; j < pwaff2->n; ++j) {
1717 set = isl_set_copy(pwaff2->p[j].set);
1718 for (i = 0; i < pwaff1->n; ++i)
1719 set = isl_set_subtract(set,
1720 isl_set_copy(pwaff1->p[i].set));
1721 res = isl_pw_aff_add_piece(res, set,
1722 isl_aff_copy(pwaff2->p[j].aff));
1725 isl_pw_aff_free(pwaff1);
1726 isl_pw_aff_free(pwaff2);
1730 isl_pw_aff_free(pwaff1);
1731 isl_pw_aff_free(pwaff2);
1735 /* Compute a piecewise quasi-affine expression with a domain that
1736 * is the union of those of pwaff1 and pwaff2 and such that on each
1737 * cell, the quasi-affine expression is the maximum of those of pwaff1
1738 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
1739 * cell, then the associated expression is the defined one.
1741 static __isl_give isl_pw_aff *pw_aff_union_max(__isl_take isl_pw_aff *pwaff1,
1742 __isl_take isl_pw_aff *pwaff2)
1744 return pw_aff_union_opt(pwaff1, pwaff2, &isl_aff_ge_basic_set);
1747 __isl_give isl_pw_aff *isl_pw_aff_union_max(__isl_take isl_pw_aff *pwaff1,
1748 __isl_take isl_pw_aff *pwaff2)
1750 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2,
1754 /* Compute a piecewise quasi-affine expression with a domain that
1755 * is the union of those of pwaff1 and pwaff2 and such that on each
1756 * cell, the quasi-affine expression is the minimum of those of pwaff1
1757 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
1758 * cell, then the associated expression is the defined one.
1760 static __isl_give isl_pw_aff *pw_aff_union_min(__isl_take isl_pw_aff *pwaff1,
1761 __isl_take isl_pw_aff *pwaff2)
1763 return pw_aff_union_opt(pwaff1, pwaff2, &isl_aff_le_basic_set);
1766 __isl_give isl_pw_aff *isl_pw_aff_union_min(__isl_take isl_pw_aff *pwaff1,
1767 __isl_take isl_pw_aff *pwaff2)
1769 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2,
1773 __isl_give isl_pw_aff *isl_pw_aff_union_opt(__isl_take isl_pw_aff *pwaff1,
1774 __isl_take isl_pw_aff *pwaff2, int max)
1777 return isl_pw_aff_union_max(pwaff1, pwaff2);
1779 return isl_pw_aff_union_min(pwaff1, pwaff2);
1782 /* Construct a map with as domain the domain of pwaff and
1783 * one-dimensional range corresponding to the affine expressions.
1785 static __isl_give isl_map *map_from_pw_aff(__isl_take isl_pw_aff *pwaff)
1794 dim = isl_pw_aff_get_space(pwaff);
1795 map = isl_map_empty(dim);
1797 for (i = 0; i < pwaff->n; ++i) {
1798 isl_basic_map *bmap;
1801 bmap = isl_basic_map_from_aff(isl_aff_copy(pwaff->p[i].aff));
1802 map_i = isl_map_from_basic_map(bmap);
1803 map_i = isl_map_intersect_domain(map_i,
1804 isl_set_copy(pwaff->p[i].set));
1805 map = isl_map_union_disjoint(map, map_i);
1808 isl_pw_aff_free(pwaff);
1813 /* Construct a map with as domain the domain of pwaff and
1814 * one-dimensional range corresponding to the affine expressions.
1816 __isl_give isl_map *isl_map_from_pw_aff(__isl_take isl_pw_aff *pwaff)
1820 if (isl_space_is_set(pwaff->dim))
1821 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
1822 "space of input is not a map",
1823 return isl_pw_aff_free(pwaff));
1824 return map_from_pw_aff(pwaff);
1827 /* Construct a one-dimensional set with as parameter domain
1828 * the domain of pwaff and the single set dimension
1829 * corresponding to the affine expressions.
1831 __isl_give isl_set *isl_set_from_pw_aff(__isl_take isl_pw_aff *pwaff)
1835 if (!isl_space_is_set(pwaff->dim))
1836 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
1837 "space of input is not a set",
1838 return isl_pw_aff_free(pwaff));
1839 return map_from_pw_aff(pwaff);
1842 /* Return a set containing those elements in the domain
1843 * of pwaff where it is non-negative.
1845 __isl_give isl_set *isl_pw_aff_nonneg_set(__isl_take isl_pw_aff *pwaff)
1853 set = isl_set_empty(isl_pw_aff_get_domain_space(pwaff));
1855 for (i = 0; i < pwaff->n; ++i) {
1856 isl_basic_set *bset;
1859 bset = isl_aff_nonneg_basic_set(isl_aff_copy(pwaff->p[i].aff));
1860 set_i = isl_set_from_basic_set(bset);
1861 set_i = isl_set_intersect(set_i, isl_set_copy(pwaff->p[i].set));
1862 set = isl_set_union_disjoint(set, set_i);
1865 isl_pw_aff_free(pwaff);
1870 /* Return a set containing those elements in the domain
1871 * of pwaff where it is zero (if complement is 0) or not zero
1872 * (if complement is 1).
1874 static __isl_give isl_set *pw_aff_zero_set(__isl_take isl_pw_aff *pwaff,
1883 set = isl_set_empty(isl_pw_aff_get_domain_space(pwaff));
1885 for (i = 0; i < pwaff->n; ++i) {
1886 isl_basic_set *bset;
1887 isl_set *set_i, *zero;
1889 bset = isl_aff_zero_basic_set(isl_aff_copy(pwaff->p[i].aff));
1890 zero = isl_set_from_basic_set(bset);
1891 set_i = isl_set_copy(pwaff->p[i].set);
1893 set_i = isl_set_subtract(set_i, zero);
1895 set_i = isl_set_intersect(set_i, zero);
1896 set = isl_set_union_disjoint(set, set_i);
1899 isl_pw_aff_free(pwaff);
1904 /* Return a set containing those elements in the domain
1905 * of pwaff where it is zero.
1907 __isl_give isl_set *isl_pw_aff_zero_set(__isl_take isl_pw_aff *pwaff)
1909 return pw_aff_zero_set(pwaff, 0);
1912 /* Return a set containing those elements in the domain
1913 * of pwaff where it is not zero.
1915 __isl_give isl_set *isl_pw_aff_non_zero_set(__isl_take isl_pw_aff *pwaff)
1917 return pw_aff_zero_set(pwaff, 1);
1920 /* Return a set containing those elements in the shared domain
1921 * of pwaff1 and pwaff2 where pwaff1 is greater than (or equal) to pwaff2.
1923 * We compute the difference on the shared domain and then construct
1924 * the set of values where this difference is non-negative.
1925 * If strict is set, we first subtract 1 from the difference.
1926 * If equal is set, we only return the elements where pwaff1 and pwaff2
1929 static __isl_give isl_set *pw_aff_gte_set(__isl_take isl_pw_aff *pwaff1,
1930 __isl_take isl_pw_aff *pwaff2, int strict, int equal)
1932 isl_set *set1, *set2;
1934 set1 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff1));
1935 set2 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff2));
1936 set1 = isl_set_intersect(set1, set2);
1937 pwaff1 = isl_pw_aff_intersect_domain(pwaff1, isl_set_copy(set1));
1938 pwaff2 = isl_pw_aff_intersect_domain(pwaff2, isl_set_copy(set1));
1939 pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_neg(pwaff2));
1942 isl_space *dim = isl_set_get_space(set1);
1944 aff = isl_aff_zero_on_domain(isl_local_space_from_space(dim));
1945 aff = isl_aff_add_constant_si(aff, -1);
1946 pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_alloc(set1, aff));
1951 return isl_pw_aff_zero_set(pwaff1);
1952 return isl_pw_aff_nonneg_set(pwaff1);
1955 /* Return a set containing those elements in the shared domain
1956 * of pwaff1 and pwaff2 where pwaff1 is equal to pwaff2.
1958 static __isl_give isl_set *pw_aff_eq_set(__isl_take isl_pw_aff *pwaff1,
1959 __isl_take isl_pw_aff *pwaff2)
1961 return pw_aff_gte_set(pwaff1, pwaff2, 0, 1);
1964 __isl_give isl_set *isl_pw_aff_eq_set(__isl_take isl_pw_aff *pwaff1,
1965 __isl_take isl_pw_aff *pwaff2)
1967 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_eq_set);
1970 /* Return a set containing those elements in the shared domain
1971 * of pwaff1 and pwaff2 where pwaff1 is greater than or equal to pwaff2.
1973 static __isl_give isl_set *pw_aff_ge_set(__isl_take isl_pw_aff *pwaff1,
1974 __isl_take isl_pw_aff *pwaff2)
1976 return pw_aff_gte_set(pwaff1, pwaff2, 0, 0);
1979 __isl_give isl_set *isl_pw_aff_ge_set(__isl_take isl_pw_aff *pwaff1,
1980 __isl_take isl_pw_aff *pwaff2)
1982 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_ge_set);
1985 /* Return a set containing those elements in the shared domain
1986 * of pwaff1 and pwaff2 where pwaff1 is strictly greater than pwaff2.
1988 static __isl_give isl_set *pw_aff_gt_set(__isl_take isl_pw_aff *pwaff1,
1989 __isl_take isl_pw_aff *pwaff2)
1991 return pw_aff_gte_set(pwaff1, pwaff2, 1, 0);
1994 __isl_give isl_set *isl_pw_aff_gt_set(__isl_take isl_pw_aff *pwaff1,
1995 __isl_take isl_pw_aff *pwaff2)
1997 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_gt_set);
2000 __isl_give isl_set *isl_pw_aff_le_set(__isl_take isl_pw_aff *pwaff1,
2001 __isl_take isl_pw_aff *pwaff2)
2003 return isl_pw_aff_ge_set(pwaff2, pwaff1);
2006 __isl_give isl_set *isl_pw_aff_lt_set(__isl_take isl_pw_aff *pwaff1,
2007 __isl_take isl_pw_aff *pwaff2)
2009 return isl_pw_aff_gt_set(pwaff2, pwaff1);
2012 /* Return a set containing those elements in the shared domain
2013 * of the elements of list1 and list2 where each element in list1
2014 * has the relation specified by "fn" with each element in list2.
2016 static __isl_give isl_set *pw_aff_list_set(__isl_take isl_pw_aff_list *list1,
2017 __isl_take isl_pw_aff_list *list2,
2018 __isl_give isl_set *(*fn)(__isl_take isl_pw_aff *pwaff1,
2019 __isl_take isl_pw_aff *pwaff2))
2025 if (!list1 || !list2)
2028 ctx = isl_pw_aff_list_get_ctx(list1);
2029 if (list1->n < 1 || list2->n < 1)
2030 isl_die(ctx, isl_error_invalid,
2031 "list should contain at least one element", goto error);
2033 set = isl_set_universe(isl_pw_aff_get_domain_space(list1->p[0]));
2034 for (i = 0; i < list1->n; ++i)
2035 for (j = 0; j < list2->n; ++j) {
2038 set_ij = fn(isl_pw_aff_copy(list1->p[i]),
2039 isl_pw_aff_copy(list2->p[j]));
2040 set = isl_set_intersect(set, set_ij);
2043 isl_pw_aff_list_free(list1);
2044 isl_pw_aff_list_free(list2);
2047 isl_pw_aff_list_free(list1);
2048 isl_pw_aff_list_free(list2);
2052 /* Return a set containing those elements in the shared domain
2053 * of the elements of list1 and list2 where each element in list1
2054 * is equal to each element in list2.
2056 __isl_give isl_set *isl_pw_aff_list_eq_set(__isl_take isl_pw_aff_list *list1,
2057 __isl_take isl_pw_aff_list *list2)
2059 return pw_aff_list_set(list1, list2, &isl_pw_aff_eq_set);
2062 __isl_give isl_set *isl_pw_aff_list_ne_set(__isl_take isl_pw_aff_list *list1,
2063 __isl_take isl_pw_aff_list *list2)
2065 return pw_aff_list_set(list1, list2, &isl_pw_aff_ne_set);
2068 /* Return a set containing those elements in the shared domain
2069 * of the elements of list1 and list2 where each element in list1
2070 * is less than or equal to each element in list2.
2072 __isl_give isl_set *isl_pw_aff_list_le_set(__isl_take isl_pw_aff_list *list1,
2073 __isl_take isl_pw_aff_list *list2)
2075 return pw_aff_list_set(list1, list2, &isl_pw_aff_le_set);
2078 __isl_give isl_set *isl_pw_aff_list_lt_set(__isl_take isl_pw_aff_list *list1,
2079 __isl_take isl_pw_aff_list *list2)
2081 return pw_aff_list_set(list1, list2, &isl_pw_aff_lt_set);
2084 __isl_give isl_set *isl_pw_aff_list_ge_set(__isl_take isl_pw_aff_list *list1,
2085 __isl_take isl_pw_aff_list *list2)
2087 return pw_aff_list_set(list1, list2, &isl_pw_aff_ge_set);
2090 __isl_give isl_set *isl_pw_aff_list_gt_set(__isl_take isl_pw_aff_list *list1,
2091 __isl_take isl_pw_aff_list *list2)
2093 return pw_aff_list_set(list1, list2, &isl_pw_aff_gt_set);
2097 /* Return a set containing those elements in the shared domain
2098 * of pwaff1 and pwaff2 where pwaff1 is not equal to pwaff2.
2100 static __isl_give isl_set *pw_aff_ne_set(__isl_take isl_pw_aff *pwaff1,
2101 __isl_take isl_pw_aff *pwaff2)
2103 isl_set *set_lt, *set_gt;
2105 set_lt = isl_pw_aff_lt_set(isl_pw_aff_copy(pwaff1),
2106 isl_pw_aff_copy(pwaff2));
2107 set_gt = isl_pw_aff_gt_set(pwaff1, pwaff2);
2108 return isl_set_union_disjoint(set_lt, set_gt);
2111 __isl_give isl_set *isl_pw_aff_ne_set(__isl_take isl_pw_aff *pwaff1,
2112 __isl_take isl_pw_aff *pwaff2)
2114 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_ne_set);
2117 __isl_give isl_pw_aff *isl_pw_aff_scale_down(__isl_take isl_pw_aff *pwaff,
2122 if (isl_int_is_one(v))
2124 if (!isl_int_is_pos(v))
2125 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
2126 "factor needs to be positive",
2127 return isl_pw_aff_free(pwaff));
2128 pwaff = isl_pw_aff_cow(pwaff);
2134 for (i = 0; i < pwaff->n; ++i) {
2135 pwaff->p[i].aff = isl_aff_scale_down(pwaff->p[i].aff, v);
2136 if (!pwaff->p[i].aff)
2137 return isl_pw_aff_free(pwaff);
2143 __isl_give isl_pw_aff *isl_pw_aff_floor(__isl_take isl_pw_aff *pwaff)
2147 pwaff = isl_pw_aff_cow(pwaff);
2153 for (i = 0; i < pwaff->n; ++i) {
2154 pwaff->p[i].aff = isl_aff_floor(pwaff->p[i].aff);
2155 if (!pwaff->p[i].aff)
2156 return isl_pw_aff_free(pwaff);
2162 __isl_give isl_pw_aff *isl_pw_aff_ceil(__isl_take isl_pw_aff *pwaff)
2166 pwaff = isl_pw_aff_cow(pwaff);
2172 for (i = 0; i < pwaff->n; ++i) {
2173 pwaff->p[i].aff = isl_aff_ceil(pwaff->p[i].aff);
2174 if (!pwaff->p[i].aff)
2175 return isl_pw_aff_free(pwaff);
2181 /* Assuming that "cond1" and "cond2" are disjoint,
2182 * return an affine expression that is equal to pwaff1 on cond1
2183 * and to pwaff2 on cond2.
2185 static __isl_give isl_pw_aff *isl_pw_aff_select(
2186 __isl_take isl_set *cond1, __isl_take isl_pw_aff *pwaff1,
2187 __isl_take isl_set *cond2, __isl_take isl_pw_aff *pwaff2)
2189 pwaff1 = isl_pw_aff_intersect_domain(pwaff1, cond1);
2190 pwaff2 = isl_pw_aff_intersect_domain(pwaff2, cond2);
2192 return isl_pw_aff_add_disjoint(pwaff1, pwaff2);
2195 /* Return an affine expression that is equal to pwaff_true for elements
2196 * where "cond" is non-zero and to pwaff_false for elements where "cond"
2198 * That is, return cond ? pwaff_true : pwaff_false;
2200 __isl_give isl_pw_aff *isl_pw_aff_cond(__isl_take isl_pw_aff *cond,
2201 __isl_take isl_pw_aff *pwaff_true, __isl_take isl_pw_aff *pwaff_false)
2203 isl_set *cond_true, *cond_false;
2205 cond_true = isl_pw_aff_non_zero_set(isl_pw_aff_copy(cond));
2206 cond_false = isl_pw_aff_zero_set(cond);
2207 return isl_pw_aff_select(cond_true, pwaff_true,
2208 cond_false, pwaff_false);
2211 int isl_aff_is_cst(__isl_keep isl_aff *aff)
2216 return isl_seq_first_non_zero(aff->v->el + 2, aff->v->size - 2) == -1;
2219 /* Check whether pwaff is a piecewise constant.
2221 int isl_pw_aff_is_cst(__isl_keep isl_pw_aff *pwaff)
2228 for (i = 0; i < pwaff->n; ++i) {
2229 int is_cst = isl_aff_is_cst(pwaff->p[i].aff);
2230 if (is_cst < 0 || !is_cst)
2237 __isl_give isl_aff *isl_aff_mul(__isl_take isl_aff *aff1,
2238 __isl_take isl_aff *aff2)
2240 if (!isl_aff_is_cst(aff2) && isl_aff_is_cst(aff1))
2241 return isl_aff_mul(aff2, aff1);
2243 if (!isl_aff_is_cst(aff2))
2244 isl_die(isl_aff_get_ctx(aff1), isl_error_invalid,
2245 "at least one affine expression should be constant",
2248 aff1 = isl_aff_cow(aff1);
2252 aff1 = isl_aff_scale(aff1, aff2->v->el[1]);
2253 aff1 = isl_aff_scale_down(aff1, aff2->v->el[0]);
2263 /* Divide "aff1" by "aff2", assuming "aff2" is a piecewise constant.
2265 __isl_give isl_aff *isl_aff_div(__isl_take isl_aff *aff1,
2266 __isl_take isl_aff *aff2)
2271 is_cst = isl_aff_is_cst(aff2);
2275 isl_die(isl_aff_get_ctx(aff2), isl_error_invalid,
2276 "second argument should be a constant", goto error);
2281 neg = isl_int_is_neg(aff2->v->el[1]);
2283 isl_int_neg(aff2->v->el[0], aff2->v->el[0]);
2284 isl_int_neg(aff2->v->el[1], aff2->v->el[1]);
2287 aff1 = isl_aff_scale(aff1, aff2->v->el[0]);
2288 aff1 = isl_aff_scale_down(aff1, aff2->v->el[1]);
2291 isl_int_neg(aff2->v->el[0], aff2->v->el[0]);
2292 isl_int_neg(aff2->v->el[1], aff2->v->el[1]);
2303 static __isl_give isl_pw_aff *pw_aff_add(__isl_take isl_pw_aff *pwaff1,
2304 __isl_take isl_pw_aff *pwaff2)
2306 return isl_pw_aff_on_shared_domain(pwaff1, pwaff2, &isl_aff_add);
2309 __isl_give isl_pw_aff *isl_pw_aff_add(__isl_take isl_pw_aff *pwaff1,
2310 __isl_take isl_pw_aff *pwaff2)
2312 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_add);
2315 __isl_give isl_pw_aff *isl_pw_aff_union_add(__isl_take isl_pw_aff *pwaff1,
2316 __isl_take isl_pw_aff *pwaff2)
2318 return isl_pw_aff_union_add_(pwaff1, pwaff2);
2321 static __isl_give isl_pw_aff *pw_aff_mul(__isl_take isl_pw_aff *pwaff1,
2322 __isl_take isl_pw_aff *pwaff2)
2324 return isl_pw_aff_on_shared_domain(pwaff1, pwaff2, &isl_aff_mul);
2327 __isl_give isl_pw_aff *isl_pw_aff_mul(__isl_take isl_pw_aff *pwaff1,
2328 __isl_take isl_pw_aff *pwaff2)
2330 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_mul);
2333 static __isl_give isl_pw_aff *pw_aff_div(__isl_take isl_pw_aff *pa1,
2334 __isl_take isl_pw_aff *pa2)
2336 return isl_pw_aff_on_shared_domain(pa1, pa2, &isl_aff_div);
2339 /* Divide "pa1" by "pa2", assuming "pa2" is a piecewise constant.
2341 __isl_give isl_pw_aff *isl_pw_aff_div(__isl_take isl_pw_aff *pa1,
2342 __isl_take isl_pw_aff *pa2)
2346 is_cst = isl_pw_aff_is_cst(pa2);
2350 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
2351 "second argument should be a piecewise constant",
2353 return isl_pw_aff_align_params_pw_pw_and(pa1, pa2, &pw_aff_div);
2355 isl_pw_aff_free(pa1);
2356 isl_pw_aff_free(pa2);
2360 /* Compute the quotient of the integer division of "pa1" by "pa2"
2361 * with rounding towards zero.
2362 * "pa2" is assumed to be a piecewise constant.
2364 * In particular, return
2366 * pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2)
2369 __isl_give isl_pw_aff *isl_pw_aff_tdiv_q(__isl_take isl_pw_aff *pa1,
2370 __isl_take isl_pw_aff *pa2)
2376 is_cst = isl_pw_aff_is_cst(pa2);
2380 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
2381 "second argument should be a piecewise constant",
2384 pa1 = isl_pw_aff_div(pa1, pa2);
2386 cond = isl_pw_aff_nonneg_set(isl_pw_aff_copy(pa1));
2387 f = isl_pw_aff_floor(isl_pw_aff_copy(pa1));
2388 c = isl_pw_aff_ceil(pa1);
2389 return isl_pw_aff_cond(isl_set_indicator_function(cond), f, c);
2391 isl_pw_aff_free(pa1);
2392 isl_pw_aff_free(pa2);
2396 /* Compute the remainder of the integer division of "pa1" by "pa2"
2397 * with rounding towards zero.
2398 * "pa2" is assumed to be a piecewise constant.
2400 * In particular, return
2402 * pa1 - pa2 * (pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2))
2405 __isl_give isl_pw_aff *isl_pw_aff_tdiv_r(__isl_take isl_pw_aff *pa1,
2406 __isl_take isl_pw_aff *pa2)
2411 is_cst = isl_pw_aff_is_cst(pa2);
2415 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
2416 "second argument should be a piecewise constant",
2418 res = isl_pw_aff_tdiv_q(isl_pw_aff_copy(pa1), isl_pw_aff_copy(pa2));
2419 res = isl_pw_aff_mul(pa2, res);
2420 res = isl_pw_aff_sub(pa1, res);
2423 isl_pw_aff_free(pa1);
2424 isl_pw_aff_free(pa2);
2428 static __isl_give isl_pw_aff *pw_aff_min(__isl_take isl_pw_aff *pwaff1,
2429 __isl_take isl_pw_aff *pwaff2)
2434 dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1)),
2435 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2)));
2436 le = isl_pw_aff_le_set(isl_pw_aff_copy(pwaff1),
2437 isl_pw_aff_copy(pwaff2));
2438 dom = isl_set_subtract(dom, isl_set_copy(le));
2439 return isl_pw_aff_select(le, pwaff1, dom, pwaff2);
2442 __isl_give isl_pw_aff *isl_pw_aff_min(__isl_take isl_pw_aff *pwaff1,
2443 __isl_take isl_pw_aff *pwaff2)
2445 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_min);
2448 static __isl_give isl_pw_aff *pw_aff_max(__isl_take isl_pw_aff *pwaff1,
2449 __isl_take isl_pw_aff *pwaff2)
2454 dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1)),
2455 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2)));
2456 ge = isl_pw_aff_ge_set(isl_pw_aff_copy(pwaff1),
2457 isl_pw_aff_copy(pwaff2));
2458 dom = isl_set_subtract(dom, isl_set_copy(ge));
2459 return isl_pw_aff_select(ge, pwaff1, dom, pwaff2);
2462 __isl_give isl_pw_aff *isl_pw_aff_max(__isl_take isl_pw_aff *pwaff1,
2463 __isl_take isl_pw_aff *pwaff2)
2465 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_max);
2468 static __isl_give isl_pw_aff *pw_aff_list_reduce(
2469 __isl_take isl_pw_aff_list *list,
2470 __isl_give isl_pw_aff *(*fn)(__isl_take isl_pw_aff *pwaff1,
2471 __isl_take isl_pw_aff *pwaff2))
2480 ctx = isl_pw_aff_list_get_ctx(list);
2482 isl_die(ctx, isl_error_invalid,
2483 "list should contain at least one element",
2484 return isl_pw_aff_list_free(list));
2486 res = isl_pw_aff_copy(list->p[0]);
2487 for (i = 1; i < list->n; ++i)
2488 res = fn(res, isl_pw_aff_copy(list->p[i]));
2490 isl_pw_aff_list_free(list);
2494 /* Return an isl_pw_aff that maps each element in the intersection of the
2495 * domains of the elements of list to the minimal corresponding affine
2498 __isl_give isl_pw_aff *isl_pw_aff_list_min(__isl_take isl_pw_aff_list *list)
2500 return pw_aff_list_reduce(list, &isl_pw_aff_min);
2503 /* Return an isl_pw_aff that maps each element in the intersection of the
2504 * domains of the elements of list to the maximal corresponding affine
2507 __isl_give isl_pw_aff *isl_pw_aff_list_max(__isl_take isl_pw_aff_list *list)
2509 return pw_aff_list_reduce(list, &isl_pw_aff_max);
2515 #include <isl_multi_templ.c>
2517 /* Create an isl_pw_multi_aff with the given isl_multi_aff on a universe
2520 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_multi_aff(
2521 __isl_take isl_multi_aff *ma)
2523 isl_set *dom = isl_set_universe(isl_multi_aff_get_domain_space(ma));
2524 return isl_pw_multi_aff_alloc(dom, ma);
2527 /* Create a piecewise multi-affine expression in the given space that maps each
2528 * input dimension to the corresponding output dimension.
2530 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_identity(
2531 __isl_take isl_space *space)
2533 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_identity(space));
2536 __isl_give isl_multi_aff *isl_multi_aff_add(__isl_take isl_multi_aff *maff1,
2537 __isl_take isl_multi_aff *maff2)
2542 maff1 = isl_multi_aff_cow(maff1);
2543 if (!maff1 || !maff2)
2546 ctx = isl_multi_aff_get_ctx(maff1);
2547 if (!isl_space_is_equal(maff1->space, maff2->space))
2548 isl_die(ctx, isl_error_invalid,
2549 "spaces don't match", goto error);
2551 for (i = 0; i < maff1->n; ++i) {
2552 maff1->p[i] = isl_aff_add(maff1->p[i],
2553 isl_aff_copy(maff2->p[i]));
2558 isl_multi_aff_free(maff2);
2561 isl_multi_aff_free(maff1);
2562 isl_multi_aff_free(maff2);
2566 /* Given two multi-affine expressions A -> B and C -> D,
2567 * construct a multi-affine expression [A -> C] -> [B -> D].
2569 __isl_give isl_multi_aff *isl_multi_aff_product(
2570 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2)
2576 int in1, in2, out1, out2;
2578 in1 = isl_multi_aff_dim(ma1, isl_dim_in);
2579 in2 = isl_multi_aff_dim(ma2, isl_dim_in);
2580 out1 = isl_multi_aff_dim(ma1, isl_dim_out);
2581 out2 = isl_multi_aff_dim(ma2, isl_dim_out);
2582 space = isl_space_product(isl_multi_aff_get_space(ma1),
2583 isl_multi_aff_get_space(ma2));
2584 res = isl_multi_aff_alloc(isl_space_copy(space));
2585 space = isl_space_domain(space);
2587 for (i = 0; i < out1; ++i) {
2588 aff = isl_multi_aff_get_aff(ma1, i);
2589 aff = isl_aff_insert_dims(aff, isl_dim_in, in1, in2);
2590 aff = isl_aff_reset_domain_space(aff, isl_space_copy(space));
2591 res = isl_multi_aff_set_aff(res, i, aff);
2594 for (i = 0; i < out2; ++i) {
2595 aff = isl_multi_aff_get_aff(ma2, i);
2596 aff = isl_aff_insert_dims(aff, isl_dim_in, 0, in1);
2597 aff = isl_aff_reset_domain_space(aff, isl_space_copy(space));
2598 res = isl_multi_aff_set_aff(res, out1 + i, aff);
2601 isl_space_free(space);
2602 isl_multi_aff_free(ma1);
2603 isl_multi_aff_free(ma2);
2607 /* Exploit the equalities in "eq" to simplify the affine expressions.
2609 static __isl_give isl_multi_aff *isl_multi_aff_substitute_equalities(
2610 __isl_take isl_multi_aff *maff, __isl_take isl_basic_set *eq)
2614 maff = isl_multi_aff_cow(maff);
2618 for (i = 0; i < maff->n; ++i) {
2619 maff->p[i] = isl_aff_substitute_equalities(maff->p[i],
2620 isl_basic_set_copy(eq));
2625 isl_basic_set_free(eq);
2628 isl_basic_set_free(eq);
2629 isl_multi_aff_free(maff);
2633 __isl_give isl_multi_aff *isl_multi_aff_scale(__isl_take isl_multi_aff *maff,
2638 maff = isl_multi_aff_cow(maff);
2642 for (i = 0; i < maff->n; ++i) {
2643 maff->p[i] = isl_aff_scale(maff->p[i], f);
2645 return isl_multi_aff_free(maff);
2651 __isl_give isl_multi_aff *isl_multi_aff_add_on_domain(__isl_keep isl_set *dom,
2652 __isl_take isl_multi_aff *maff1, __isl_take isl_multi_aff *maff2)
2654 maff1 = isl_multi_aff_add(maff1, maff2);
2655 maff1 = isl_multi_aff_gist(maff1, isl_set_copy(dom));
2659 int isl_multi_aff_is_empty(__isl_keep isl_multi_aff *maff)
2667 int isl_multi_aff_plain_is_equal(__isl_keep isl_multi_aff *maff1,
2668 __isl_keep isl_multi_aff *maff2)
2673 if (!maff1 || !maff2)
2675 if (maff1->n != maff2->n)
2677 equal = isl_space_is_equal(maff1->space, maff2->space);
2678 if (equal < 0 || !equal)
2681 for (i = 0; i < maff1->n; ++i) {
2682 equal = isl_aff_plain_is_equal(maff1->p[i], maff2->p[i]);
2683 if (equal < 0 || !equal)
2690 __isl_give isl_multi_aff *isl_multi_aff_set_dim_name(
2691 __isl_take isl_multi_aff *maff,
2692 enum isl_dim_type type, unsigned pos, const char *s)
2696 maff = isl_multi_aff_cow(maff);
2700 maff->space = isl_space_set_dim_name(maff->space, type, pos, s);
2702 return isl_multi_aff_free(maff);
2704 if (type == isl_dim_out)
2706 for (i = 0; i < maff->n; ++i) {
2707 maff->p[i] = isl_aff_set_dim_name(maff->p[i], type, pos, s);
2709 return isl_multi_aff_free(maff);
2715 __isl_give isl_multi_aff *isl_multi_aff_drop_dims(__isl_take isl_multi_aff *maff,
2716 enum isl_dim_type type, unsigned first, unsigned n)
2720 maff = isl_multi_aff_cow(maff);
2724 maff->space = isl_space_drop_dims(maff->space, type, first, n);
2726 return isl_multi_aff_free(maff);
2728 if (type == isl_dim_out) {
2729 for (i = 0; i < n; ++i)
2730 isl_aff_free(maff->p[first + i]);
2731 for (i = first; i + n < maff->n; ++i)
2732 maff->p[i] = maff->p[i + n];
2737 for (i = 0; i < maff->n; ++i) {
2738 maff->p[i] = isl_aff_drop_dims(maff->p[i], type, first, n);
2740 return isl_multi_aff_free(maff);
2746 /* Return the set of domain elements where "ma1" is lexicographically
2747 * smaller than or equal to "ma2".
2749 __isl_give isl_set *isl_multi_aff_lex_le_set(__isl_take isl_multi_aff *ma1,
2750 __isl_take isl_multi_aff *ma2)
2752 return isl_multi_aff_lex_ge_set(ma2, ma1);
2755 /* Return the set of domain elements where "ma1" is lexicographically
2756 * greater than or equal to "ma2".
2758 __isl_give isl_set *isl_multi_aff_lex_ge_set(__isl_take isl_multi_aff *ma1,
2759 __isl_take isl_multi_aff *ma2)
2762 isl_map *map1, *map2;
2765 map1 = isl_map_from_multi_aff(ma1);
2766 map2 = isl_map_from_multi_aff(ma2);
2767 map = isl_map_range_product(map1, map2);
2768 space = isl_space_range(isl_map_get_space(map));
2769 space = isl_space_domain(isl_space_unwrap(space));
2770 ge = isl_map_lex_ge(space);
2771 map = isl_map_intersect_range(map, isl_map_wrap(ge));
2773 return isl_map_domain(map);
2777 #define PW isl_pw_multi_aff
2779 #define EL isl_multi_aff
2781 #define EL_IS_ZERO is_empty
2785 #define IS_ZERO is_empty
2788 #undef DEFAULT_IS_ZERO
2789 #define DEFAULT_IS_ZERO 0
2794 #define NO_INVOLVES_DIMS
2795 #define NO_MOVE_DIMS
2796 #define NO_INSERT_DIMS
2800 #include <isl_pw_templ.c>
2803 #define UNION isl_union_pw_multi_aff
2805 #define PART isl_pw_multi_aff
2807 #define PARTS pw_multi_aff
2808 #define ALIGN_DOMAIN
2812 #include <isl_union_templ.c>
2814 /* Given a function "cmp" that returns the set of elements where
2815 * "ma1" is "better" than "ma2", return the intersection of this
2816 * set with "dom1" and "dom2".
2818 static __isl_give isl_set *shared_and_better(__isl_keep isl_set *dom1,
2819 __isl_keep isl_set *dom2, __isl_keep isl_multi_aff *ma1,
2820 __isl_keep isl_multi_aff *ma2,
2821 __isl_give isl_set *(*cmp)(__isl_take isl_multi_aff *ma1,
2822 __isl_take isl_multi_aff *ma2))
2828 common = isl_set_intersect(isl_set_copy(dom1), isl_set_copy(dom2));
2829 is_empty = isl_set_plain_is_empty(common);
2830 if (is_empty >= 0 && is_empty)
2833 return isl_set_free(common);
2834 better = cmp(isl_multi_aff_copy(ma1), isl_multi_aff_copy(ma2));
2835 better = isl_set_intersect(common, better);
2840 /* Given a function "cmp" that returns the set of elements where
2841 * "ma1" is "better" than "ma2", return a piecewise multi affine
2842 * expression defined on the union of the definition domains
2843 * of "pma1" and "pma2" that maps to the "best" of "pma1" and
2844 * "pma2" on each cell. If only one of the two input functions
2845 * is defined on a given cell, then it is considered the best.
2847 static __isl_give isl_pw_multi_aff *pw_multi_aff_union_opt(
2848 __isl_take isl_pw_multi_aff *pma1,
2849 __isl_take isl_pw_multi_aff *pma2,
2850 __isl_give isl_set *(*cmp)(__isl_take isl_multi_aff *ma1,
2851 __isl_take isl_multi_aff *ma2))
2854 isl_pw_multi_aff *res = NULL;
2856 isl_set *set = NULL;
2861 ctx = isl_space_get_ctx(pma1->dim);
2862 if (!isl_space_is_equal(pma1->dim, pma2->dim))
2863 isl_die(ctx, isl_error_invalid,
2864 "arguments should live in the same space", goto error);
2866 if (isl_pw_multi_aff_is_empty(pma1)) {
2867 isl_pw_multi_aff_free(pma1);
2871 if (isl_pw_multi_aff_is_empty(pma2)) {
2872 isl_pw_multi_aff_free(pma2);
2876 n = 2 * (pma1->n + 1) * (pma2->n + 1);
2877 res = isl_pw_multi_aff_alloc_size(isl_space_copy(pma1->dim), n);
2879 for (i = 0; i < pma1->n; ++i) {
2880 set = isl_set_copy(pma1->p[i].set);
2881 for (j = 0; j < pma2->n; ++j) {
2885 better = shared_and_better(pma2->p[j].set,
2886 pma1->p[i].set, pma2->p[j].maff,
2887 pma1->p[i].maff, cmp);
2888 is_empty = isl_set_plain_is_empty(better);
2889 if (is_empty < 0 || is_empty) {
2890 isl_set_free(better);
2895 set = isl_set_subtract(set, isl_set_copy(better));
2897 res = isl_pw_multi_aff_add_piece(res, better,
2898 isl_multi_aff_copy(pma2->p[j].maff));
2900 res = isl_pw_multi_aff_add_piece(res, set,
2901 isl_multi_aff_copy(pma1->p[i].maff));
2904 for (j = 0; j < pma2->n; ++j) {
2905 set = isl_set_copy(pma2->p[j].set);
2906 for (i = 0; i < pma1->n; ++i)
2907 set = isl_set_subtract(set,
2908 isl_set_copy(pma1->p[i].set));
2909 res = isl_pw_multi_aff_add_piece(res, set,
2910 isl_multi_aff_copy(pma2->p[j].maff));
2913 isl_pw_multi_aff_free(pma1);
2914 isl_pw_multi_aff_free(pma2);
2918 isl_pw_multi_aff_free(pma1);
2919 isl_pw_multi_aff_free(pma2);
2921 return isl_pw_multi_aff_free(res);
2924 static __isl_give isl_pw_multi_aff *pw_multi_aff_union_lexmax(
2925 __isl_take isl_pw_multi_aff *pma1,
2926 __isl_take isl_pw_multi_aff *pma2)
2928 return pw_multi_aff_union_opt(pma1, pma2, &isl_multi_aff_lex_ge_set);
2931 /* Given two piecewise multi affine expressions, return a piecewise
2932 * multi-affine expression defined on the union of the definition domains
2933 * of the inputs that is equal to the lexicographic maximum of the two
2934 * inputs on each cell. If only one of the two inputs is defined on
2935 * a given cell, then it is considered to be the maximum.
2937 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_lexmax(
2938 __isl_take isl_pw_multi_aff *pma1,
2939 __isl_take isl_pw_multi_aff *pma2)
2941 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
2942 &pw_multi_aff_union_lexmax);
2945 static __isl_give isl_pw_multi_aff *pw_multi_aff_union_lexmin(
2946 __isl_take isl_pw_multi_aff *pma1,
2947 __isl_take isl_pw_multi_aff *pma2)
2949 return pw_multi_aff_union_opt(pma1, pma2, &isl_multi_aff_lex_le_set);
2952 /* Given two piecewise multi affine expressions, return a piecewise
2953 * multi-affine expression defined on the union of the definition domains
2954 * of the inputs that is equal to the lexicographic minimum of the two
2955 * inputs on each cell. If only one of the two inputs is defined on
2956 * a given cell, then it is considered to be the minimum.
2958 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_lexmin(
2959 __isl_take isl_pw_multi_aff *pma1,
2960 __isl_take isl_pw_multi_aff *pma2)
2962 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
2963 &pw_multi_aff_union_lexmin);
2966 static __isl_give isl_pw_multi_aff *pw_multi_aff_add(
2967 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
2969 return isl_pw_multi_aff_on_shared_domain(pma1, pma2,
2970 &isl_multi_aff_add);
2973 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_add(
2974 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
2976 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
2980 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_add(
2981 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
2983 return isl_pw_multi_aff_union_add_(pma1, pma2);
2986 /* Given two piecewise multi-affine expressions A -> B and C -> D,
2987 * construct a piecewise multi-affine expression [A -> C] -> [B -> D].
2989 static __isl_give isl_pw_multi_aff *pw_multi_aff_product(
2990 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
2994 isl_pw_multi_aff *res;
2999 n = pma1->n * pma2->n;
3000 space = isl_space_product(isl_space_copy(pma1->dim),
3001 isl_space_copy(pma2->dim));
3002 res = isl_pw_multi_aff_alloc_size(space, n);
3004 for (i = 0; i < pma1->n; ++i) {
3005 for (j = 0; j < pma2->n; ++j) {
3009 domain = isl_set_product(isl_set_copy(pma1->p[i].set),
3010 isl_set_copy(pma2->p[j].set));
3011 ma = isl_multi_aff_product(
3012 isl_multi_aff_copy(pma1->p[i].maff),
3013 isl_multi_aff_copy(pma2->p[i].maff));
3014 res = isl_pw_multi_aff_add_piece(res, domain, ma);
3018 isl_pw_multi_aff_free(pma1);
3019 isl_pw_multi_aff_free(pma2);
3022 isl_pw_multi_aff_free(pma1);
3023 isl_pw_multi_aff_free(pma2);
3027 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_product(
3028 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
3030 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
3031 &pw_multi_aff_product);
3034 /* Construct a map mapping the domain of the piecewise multi-affine expression
3035 * to its range, with each dimension in the range equated to the
3036 * corresponding affine expression on its cell.
3038 __isl_give isl_map *isl_map_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma)
3046 map = isl_map_empty(isl_pw_multi_aff_get_space(pma));
3048 for (i = 0; i < pma->n; ++i) {
3049 isl_multi_aff *maff;
3050 isl_basic_map *bmap;
3053 maff = isl_multi_aff_copy(pma->p[i].maff);
3054 bmap = isl_basic_map_from_multi_aff(maff);
3055 map_i = isl_map_from_basic_map(bmap);
3056 map_i = isl_map_intersect_domain(map_i,
3057 isl_set_copy(pma->p[i].set));
3058 map = isl_map_union_disjoint(map, map_i);
3061 isl_pw_multi_aff_free(pma);
3065 __isl_give isl_set *isl_set_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma)
3070 if (!isl_space_is_set(pma->dim))
3071 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
3072 "isl_pw_multi_aff cannot be converted into an isl_set",
3073 return isl_pw_multi_aff_free(pma));
3075 return isl_map_from_pw_multi_aff(pma);
3078 /* Given a basic map with a single output dimension that is defined
3079 * in terms of the parameters and input dimensions using an equality,
3080 * extract an isl_aff that expresses the output dimension in terms
3081 * of the parameters and input dimensions.
3083 * Since some applications expect the result of isl_pw_multi_aff_from_map
3084 * to only contain integer affine expressions, we compute the floor
3085 * of the expression before returning.
3087 * This function shares some similarities with
3088 * isl_basic_map_has_defining_equality and isl_constraint_get_bound.
3090 static __isl_give isl_aff *extract_isl_aff_from_basic_map(
3091 __isl_take isl_basic_map *bmap)
3096 isl_local_space *ls;
3101 if (isl_basic_map_dim(bmap, isl_dim_out) != 1)
3102 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
3103 "basic map should have a single output dimension",
3105 offset = isl_basic_map_offset(bmap, isl_dim_out);
3106 total = isl_basic_map_total_dim(bmap);
3107 for (i = 0; i < bmap->n_eq; ++i) {
3108 if (isl_int_is_zero(bmap->eq[i][offset]))
3110 if (isl_seq_first_non_zero(bmap->eq[i] + offset + 1,
3111 1 + total - (offset + 1)) != -1)
3115 if (i >= bmap->n_eq)
3116 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
3117 "unable to find suitable equality", goto error);
3118 ls = isl_basic_map_get_local_space(bmap);
3119 aff = isl_aff_alloc(isl_local_space_domain(ls));
3122 if (isl_int_is_neg(bmap->eq[i][offset]))
3123 isl_seq_cpy(aff->v->el + 1, bmap->eq[i], offset);
3125 isl_seq_neg(aff->v->el + 1, bmap->eq[i], offset);
3126 isl_seq_clr(aff->v->el + 1 + offset, aff->v->size - (1 + offset));
3127 isl_int_abs(aff->v->el[0], bmap->eq[i][offset]);
3128 isl_basic_map_free(bmap);
3130 aff = isl_aff_remove_unused_divs(aff);
3131 aff = isl_aff_floor(aff);
3134 isl_basic_map_free(bmap);
3138 /* Given a basic map where each output dimension is defined
3139 * in terms of the parameters and input dimensions using an equality,
3140 * extract an isl_multi_aff that expresses the output dimensions in terms
3141 * of the parameters and input dimensions.
3143 static __isl_give isl_multi_aff *extract_isl_multi_aff_from_basic_map(
3144 __isl_take isl_basic_map *bmap)
3153 ma = isl_multi_aff_alloc(isl_basic_map_get_space(bmap));
3154 n_out = isl_basic_map_dim(bmap, isl_dim_out);
3156 for (i = 0; i < n_out; ++i) {
3157 isl_basic_map *bmap_i;
3160 bmap_i = isl_basic_map_copy(bmap);
3161 bmap_i = isl_basic_map_project_out(bmap_i, isl_dim_out,
3162 i + 1, n_out - (1 + i));
3163 bmap_i = isl_basic_map_project_out(bmap_i, isl_dim_out, 0, i);
3164 aff = extract_isl_aff_from_basic_map(bmap_i);
3165 ma = isl_multi_aff_set_aff(ma, i, aff);
3168 isl_basic_map_free(bmap);
3173 /* Create an isl_pw_multi_aff that is equivalent to
3174 * isl_map_intersect_domain(isl_map_from_basic_map(bmap), domain).
3175 * The given basic map is such that each output dimension is defined
3176 * in terms of the parameters and input dimensions using an equality.
3178 static __isl_give isl_pw_multi_aff *plain_pw_multi_aff_from_map(
3179 __isl_take isl_set *domain, __isl_take isl_basic_map *bmap)
3183 ma = extract_isl_multi_aff_from_basic_map(bmap);
3184 return isl_pw_multi_aff_alloc(domain, ma);
3187 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
3188 * This obviously only works if the input "map" is single-valued.
3189 * If so, we compute the lexicographic minimum of the image in the form
3190 * of an isl_pw_multi_aff. Since the image is unique, it is equal
3191 * to its lexicographic minimum.
3192 * If the input is not single-valued, we produce an error.
3194 * As a special case, we first check if all output dimensions are uniquely
3195 * defined in terms of the parameters and input dimensions over the entire
3196 * domain. If so, we extract the desired isl_pw_multi_aff directly
3197 * from the affine hull of "map" and its domain.
3199 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_map(__isl_take isl_map *map)
3203 isl_pw_multi_aff *pma;
3204 isl_basic_map *hull;
3209 hull = isl_map_affine_hull(isl_map_copy(map));
3210 sv = isl_basic_map_plain_is_single_valued(hull);
3212 return plain_pw_multi_aff_from_map(isl_map_domain(map), hull);
3213 isl_basic_map_free(hull);
3217 sv = isl_map_is_single_valued(map);
3221 isl_die(isl_map_get_ctx(map), isl_error_invalid,
3222 "map is not single-valued", goto error);
3223 map = isl_map_make_disjoint(map);
3227 pma = isl_pw_multi_aff_empty(isl_map_get_space(map));
3229 for (i = 0; i < map->n; ++i) {
3230 isl_pw_multi_aff *pma_i;
3231 isl_basic_map *bmap;
3232 bmap = isl_basic_map_copy(map->p[i]);
3233 pma_i = isl_basic_map_lexmin_pw_multi_aff(bmap);
3234 pma = isl_pw_multi_aff_add_disjoint(pma, pma_i);
3244 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_set(__isl_take isl_set *set)
3246 return isl_pw_multi_aff_from_map(set);
3249 /* Return the piecewise affine expression "set ? 1 : 0".
3251 __isl_give isl_pw_aff *isl_set_indicator_function(__isl_take isl_set *set)
3254 isl_space *space = isl_set_get_space(set);
3255 isl_local_space *ls = isl_local_space_from_space(space);
3256 isl_aff *zero = isl_aff_zero_on_domain(isl_local_space_copy(ls));
3257 isl_aff *one = isl_aff_zero_on_domain(ls);
3259 one = isl_aff_add_constant_si(one, 1);
3260 pa = isl_pw_aff_alloc(isl_set_copy(set), one);
3261 set = isl_set_complement(set);
3262 pa = isl_pw_aff_add_disjoint(pa, isl_pw_aff_alloc(set, zero));
3267 /* Plug in "subs" for dimension "type", "pos" of "aff".
3269 * Let i be the dimension to replace and let "subs" be of the form
3273 * and "aff" of the form
3279 * (a f + d g')/(m d)
3281 * where g' is the result of plugging in "subs" in each of the integer
3284 __isl_give isl_aff *isl_aff_substitute(__isl_take isl_aff *aff,
3285 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
3290 aff = isl_aff_cow(aff);
3292 return isl_aff_free(aff);
3294 ctx = isl_aff_get_ctx(aff);
3295 if (!isl_space_is_equal(aff->ls->dim, subs->ls->dim))
3296 isl_die(ctx, isl_error_invalid,
3297 "spaces don't match", return isl_aff_free(aff));
3298 if (isl_local_space_dim(subs->ls, isl_dim_div) != 0)
3299 isl_die(ctx, isl_error_unsupported,
3300 "cannot handle divs yet", return isl_aff_free(aff));
3302 aff->ls = isl_local_space_substitute(aff->ls, type, pos, subs);
3304 return isl_aff_free(aff);
3306 aff->v = isl_vec_cow(aff->v);
3308 return isl_aff_free(aff);
3310 pos += isl_local_space_offset(aff->ls, type);
3313 isl_seq_substitute(aff->v->el, pos, subs->v->el,
3314 aff->v->size, subs->v->size, v);
3320 /* Plug in "subs" for dimension "type", "pos" in each of the affine
3321 * expressions in "maff".
3323 __isl_give isl_multi_aff *isl_multi_aff_substitute(
3324 __isl_take isl_multi_aff *maff, enum isl_dim_type type, unsigned pos,
3325 __isl_keep isl_aff *subs)
3329 maff = isl_multi_aff_cow(maff);
3331 return isl_multi_aff_free(maff);
3333 if (type == isl_dim_in)
3336 for (i = 0; i < maff->n; ++i) {
3337 maff->p[i] = isl_aff_substitute(maff->p[i], type, pos, subs);
3339 return isl_multi_aff_free(maff);
3345 /* Plug in "subs" for dimension "type", "pos" of "pma".
3347 * pma is of the form
3351 * while subs is of the form
3353 * v' = B_j(v) -> S_j
3355 * Each pair i,j such that C_ij = A_i \cap B_i is non-empty
3356 * has a contribution in the result, in particular
3358 * C_ij(S_j) -> M_i(S_j)
3360 * Note that plugging in S_j in C_ij may also result in an empty set
3361 * and this contribution should simply be discarded.
3363 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_substitute(
3364 __isl_take isl_pw_multi_aff *pma, enum isl_dim_type type, unsigned pos,
3365 __isl_keep isl_pw_aff *subs)
3368 isl_pw_multi_aff *res;
3371 return isl_pw_multi_aff_free(pma);
3373 n = pma->n * subs->n;
3374 res = isl_pw_multi_aff_alloc_size(isl_space_copy(pma->dim), n);
3376 for (i = 0; i < pma->n; ++i) {
3377 for (j = 0; j < subs->n; ++j) {
3379 isl_multi_aff *res_ij;
3380 common = isl_set_intersect(
3381 isl_set_copy(pma->p[i].set),
3382 isl_set_copy(subs->p[j].set));
3383 common = isl_set_substitute(common,
3384 type, pos, subs->p[j].aff);
3385 if (isl_set_plain_is_empty(common)) {
3386 isl_set_free(common);
3390 res_ij = isl_multi_aff_substitute(
3391 isl_multi_aff_copy(pma->p[i].maff),
3392 type, pos, subs->p[j].aff);
3394 res = isl_pw_multi_aff_add_piece(res, common, res_ij);
3398 isl_pw_multi_aff_free(pma);
3402 /* Extend the local space of "dst" to include the divs
3403 * in the local space of "src".
3405 __isl_give isl_aff *isl_aff_align_divs(__isl_take isl_aff *dst,
3406 __isl_keep isl_aff *src)
3414 return isl_aff_free(dst);
3416 ctx = isl_aff_get_ctx(src);
3417 if (!isl_space_is_equal(src->ls->dim, dst->ls->dim))
3418 isl_die(ctx, isl_error_invalid,
3419 "spaces don't match", goto error);
3421 if (src->ls->div->n_row == 0)
3424 exp1 = isl_alloc_array(ctx, int, src->ls->div->n_row);
3425 exp2 = isl_alloc_array(ctx, int, dst->ls->div->n_row);
3429 div = isl_merge_divs(src->ls->div, dst->ls->div, exp1, exp2);
3430 dst = isl_aff_expand_divs(dst, div, exp2);
3438 return isl_aff_free(dst);
3441 /* Adjust the local spaces of the affine expressions in "maff"
3442 * such that they all have the save divs.
3444 __isl_give isl_multi_aff *isl_multi_aff_align_divs(
3445 __isl_take isl_multi_aff *maff)
3453 maff = isl_multi_aff_cow(maff);
3457 for (i = 1; i < maff->n; ++i)
3458 maff->p[0] = isl_aff_align_divs(maff->p[0], maff->p[i]);
3459 for (i = 1; i < maff->n; ++i) {
3460 maff->p[i] = isl_aff_align_divs(maff->p[i], maff->p[0]);
3462 return isl_multi_aff_free(maff);
3468 __isl_give isl_aff *isl_aff_lift(__isl_take isl_aff *aff)
3470 aff = isl_aff_cow(aff);
3474 aff->ls = isl_local_space_lift(aff->ls);
3476 return isl_aff_free(aff);
3481 /* Lift "maff" to a space with extra dimensions such that the result
3482 * has no more existentially quantified variables.
3483 * If "ls" is not NULL, then *ls is assigned the local space that lies
3484 * at the basis of the lifting applied to "maff".
3486 __isl_give isl_multi_aff *isl_multi_aff_lift(__isl_take isl_multi_aff *maff,
3487 __isl_give isl_local_space **ls)
3501 isl_space *space = isl_multi_aff_get_domain_space(maff);
3502 *ls = isl_local_space_from_space(space);
3504 return isl_multi_aff_free(maff);
3509 maff = isl_multi_aff_cow(maff);
3510 maff = isl_multi_aff_align_divs(maff);
3514 n_div = isl_aff_dim(maff->p[0], isl_dim_div);
3515 space = isl_multi_aff_get_space(maff);
3516 space = isl_space_lift(isl_space_domain(space), n_div);
3517 space = isl_space_extend_domain_with_range(space,
3518 isl_multi_aff_get_space(maff));
3520 return isl_multi_aff_free(maff);
3521 isl_space_free(maff->space);
3522 maff->space = space;
3525 *ls = isl_aff_get_domain_local_space(maff->p[0]);
3527 return isl_multi_aff_free(maff);
3530 for (i = 0; i < maff->n; ++i) {
3531 maff->p[i] = isl_aff_lift(maff->p[i]);
3539 isl_local_space_free(*ls);
3540 return isl_multi_aff_free(maff);
3544 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma".
3546 __isl_give isl_pw_aff *isl_pw_multi_aff_get_pw_aff(
3547 __isl_keep isl_pw_multi_aff *pma, int pos)
3557 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
3558 if (pos < 0 || pos >= n_out)
3559 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
3560 "index out of bounds", return NULL);
3562 space = isl_pw_multi_aff_get_space(pma);
3563 space = isl_space_drop_dims(space, isl_dim_out,
3564 pos + 1, n_out - pos - 1);
3565 space = isl_space_drop_dims(space, isl_dim_out, 0, pos);
3567 pa = isl_pw_aff_alloc_size(space, pma->n);
3568 for (i = 0; i < pma->n; ++i) {
3570 aff = isl_multi_aff_get_aff(pma->p[i].maff, pos);
3571 pa = isl_pw_aff_add_piece(pa, isl_set_copy(pma->p[i].set), aff);
3577 /* Return an isl_pw_multi_aff with the given "set" as domain and
3578 * an unnamed zero-dimensional range.
3580 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_domain(
3581 __isl_take isl_set *set)
3586 space = isl_set_get_space(set);
3587 space = isl_space_from_domain(space);
3588 ma = isl_multi_aff_zero(space);
3589 return isl_pw_multi_aff_alloc(set, ma);
3592 /* Add an isl_pw_multi_aff with the given "set" as domain and
3593 * an unnamed zero-dimensional range to *user.
3595 static int add_pw_multi_aff_from_domain(__isl_take isl_set *set, void *user)
3597 isl_union_pw_multi_aff **upma = user;
3598 isl_pw_multi_aff *pma;
3600 pma = isl_pw_multi_aff_from_domain(set);
3601 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
3606 /* Return an isl_union_pw_multi_aff with the given "uset" as domain and
3607 * an unnamed zero-dimensional range.
3609 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_domain(
3610 __isl_take isl_union_set *uset)
3613 isl_union_pw_multi_aff *upma;
3618 space = isl_union_set_get_space(uset);
3619 upma = isl_union_pw_multi_aff_empty(space);
3621 if (isl_union_set_foreach_set(uset,
3622 &add_pw_multi_aff_from_domain, &upma) < 0)
3625 isl_union_set_free(uset);
3628 isl_union_set_free(uset);
3629 isl_union_pw_multi_aff_free(upma);
3633 /* Convert "pma" to an isl_map and add it to *umap.
3635 static int map_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma, void *user)
3637 isl_union_map **umap = user;
3640 map = isl_map_from_pw_multi_aff(pma);
3641 *umap = isl_union_map_add_map(*umap, map);
3646 /* Construct a union map mapping the domain of the union
3647 * piecewise multi-affine expression to its range, with each dimension
3648 * in the range equated to the corresponding affine expression on its cell.
3650 __isl_give isl_union_map *isl_union_map_from_union_pw_multi_aff(
3651 __isl_take isl_union_pw_multi_aff *upma)
3654 isl_union_map *umap;
3659 space = isl_union_pw_multi_aff_get_space(upma);
3660 umap = isl_union_map_empty(space);
3662 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
3663 &map_from_pw_multi_aff, &umap) < 0)
3666 isl_union_pw_multi_aff_free(upma);
3669 isl_union_pw_multi_aff_free(upma);
3670 isl_union_map_free(umap);
3674 /* Local data for bin_entry and the callback "fn".
3676 struct isl_union_pw_multi_aff_bin_data {
3677 isl_union_pw_multi_aff *upma2;
3678 isl_union_pw_multi_aff *res;
3679 isl_pw_multi_aff *pma;
3680 int (*fn)(void **entry, void *user);
3683 /* Given an isl_pw_multi_aff from upma1, store it in data->pma
3684 * and call data->fn for each isl_pw_multi_aff in data->upma2.
3686 static int bin_entry(void **entry, void *user)
3688 struct isl_union_pw_multi_aff_bin_data *data = user;
3689 isl_pw_multi_aff *pma = *entry;
3692 if (isl_hash_table_foreach(data->upma2->dim->ctx, &data->upma2->table,
3693 data->fn, data) < 0)
3699 /* Call "fn" on each pair of isl_pw_multi_affs in "upma1" and "upma2".
3700 * The isl_pw_multi_aff from upma1 is stored in data->pma (where data is
3701 * passed as user field) and the isl_pw_multi_aff from upma2 is available
3702 * as *entry. The callback should adjust data->res if desired.
3704 static __isl_give isl_union_pw_multi_aff *bin_op(
3705 __isl_take isl_union_pw_multi_aff *upma1,
3706 __isl_take isl_union_pw_multi_aff *upma2,
3707 int (*fn)(void **entry, void *user))
3710 struct isl_union_pw_multi_aff_bin_data data = { NULL, NULL, NULL, fn };
3712 space = isl_union_pw_multi_aff_get_space(upma2);
3713 upma1 = isl_union_pw_multi_aff_align_params(upma1, space);
3714 space = isl_union_pw_multi_aff_get_space(upma1);
3715 upma2 = isl_union_pw_multi_aff_align_params(upma2, space);
3717 if (!upma1 || !upma2)
3721 data.res = isl_union_pw_multi_aff_alloc(isl_space_copy(upma1->dim),
3723 if (isl_hash_table_foreach(upma1->dim->ctx, &upma1->table,
3724 &bin_entry, &data) < 0)
3727 isl_union_pw_multi_aff_free(upma1);
3728 isl_union_pw_multi_aff_free(upma2);
3731 isl_union_pw_multi_aff_free(upma1);
3732 isl_union_pw_multi_aff_free(upma2);
3733 isl_union_pw_multi_aff_free(data.res);
3737 /* Given two isl_multi_affs A -> B and C -> D,
3738 * construct an isl_multi_aff (A * C) -> (B, D).
3740 __isl_give isl_multi_aff *isl_multi_aff_flat_range_product(
3741 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2)
3751 space = isl_space_range_product(isl_multi_aff_get_space(ma1),
3752 isl_multi_aff_get_space(ma2));
3753 space = isl_space_flatten_range(space);
3754 res = isl_multi_aff_alloc(space);
3756 n1 = isl_multi_aff_dim(ma1, isl_dim_out);
3757 n2 = isl_multi_aff_dim(ma2, isl_dim_out);
3759 for (i = 0; i < n1; ++i) {
3760 aff = isl_multi_aff_get_aff(ma1, i);
3761 res = isl_multi_aff_set_aff(res, i, aff);
3764 for (i = 0; i < n2; ++i) {
3765 aff = isl_multi_aff_get_aff(ma2, i);
3766 res = isl_multi_aff_set_aff(res, n1 + i, aff);
3769 isl_multi_aff_free(ma1);
3770 isl_multi_aff_free(ma2);
3773 isl_multi_aff_free(ma1);
3774 isl_multi_aff_free(ma2);
3778 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
3779 * construct an isl_pw_multi_aff (A * C) -> (B, D).
3781 static __isl_give isl_pw_multi_aff *pw_multi_aff_flat_range_product(
3782 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
3786 space = isl_space_range_product(isl_pw_multi_aff_get_space(pma1),
3787 isl_pw_multi_aff_get_space(pma2));
3788 space = isl_space_flatten_range(space);
3789 return isl_pw_multi_aff_on_shared_domain_in(pma1, pma2, space,
3790 &isl_multi_aff_flat_range_product);
3793 /* Given two isl_pw_multi_affs A -> B and C -> D,
3794 * construct an isl_pw_multi_aff (A * C) -> (B, D).
3796 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_flat_range_product(
3797 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
3799 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
3800 &pw_multi_aff_flat_range_product);
3803 /* If data->pma and *entry have the same domain space, then compute
3804 * their flat range product and the result to data->res.
3806 static int flat_range_product_entry(void **entry, void *user)
3808 struct isl_union_pw_multi_aff_bin_data *data = user;
3809 isl_pw_multi_aff *pma2 = *entry;
3811 if (!isl_space_tuple_match(data->pma->dim, isl_dim_in,
3812 pma2->dim, isl_dim_in))
3815 pma2 = isl_pw_multi_aff_flat_range_product(
3816 isl_pw_multi_aff_copy(data->pma),
3817 isl_pw_multi_aff_copy(pma2));
3819 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma2);
3824 /* Given two isl_union_pw_multi_affs A -> B and C -> D,
3825 * construct an isl_union_pw_multi_aff (A * C) -> (B, D).
3827 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_flat_range_product(
3828 __isl_take isl_union_pw_multi_aff *upma1,
3829 __isl_take isl_union_pw_multi_aff *upma2)
3831 return bin_op(upma1, upma2, &flat_range_product_entry);
3834 /* Replace the affine expressions at position "pos" in "pma" by "pa".
3835 * The parameters are assumed to have been aligned.
3837 * The implementation essentially performs an isl_pw_*_on_shared_domain,
3838 * except that it works on two different isl_pw_* types.
3840 static __isl_give isl_pw_multi_aff *pw_multi_aff_set_pw_aff(
3841 __isl_take isl_pw_multi_aff *pma, unsigned pos,
3842 __isl_take isl_pw_aff *pa)
3845 isl_pw_multi_aff *res = NULL;
3850 if (!isl_space_tuple_match(pma->dim, isl_dim_in, pa->dim, isl_dim_in))
3851 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
3852 "domains don't match", goto error);
3853 if (pos >= isl_pw_multi_aff_dim(pma, isl_dim_out))
3854 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
3855 "index out of bounds", goto error);
3858 res = isl_pw_multi_aff_alloc_size(isl_pw_multi_aff_get_space(pma), n);
3860 for (i = 0; i < pma->n; ++i) {
3861 for (j = 0; j < pa->n; ++j) {
3863 isl_multi_aff *res_ij;
3866 common = isl_set_intersect(isl_set_copy(pma->p[i].set),
3867 isl_set_copy(pa->p[j].set));
3868 empty = isl_set_plain_is_empty(common);
3869 if (empty < 0 || empty) {
3870 isl_set_free(common);
3876 res_ij = isl_multi_aff_set_aff(
3877 isl_multi_aff_copy(pma->p[i].maff), pos,
3878 isl_aff_copy(pa->p[j].aff));
3879 res_ij = isl_multi_aff_gist(res_ij,
3880 isl_set_copy(common));
3882 res = isl_pw_multi_aff_add_piece(res, common, res_ij);
3886 isl_pw_multi_aff_free(pma);
3887 isl_pw_aff_free(pa);
3890 isl_pw_multi_aff_free(pma);
3891 isl_pw_aff_free(pa);
3892 return isl_pw_multi_aff_free(res);
3895 /* Replace the affine expressions at position "pos" in "pma" by "pa".
3897 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_set_pw_aff(
3898 __isl_take isl_pw_multi_aff *pma, unsigned pos,
3899 __isl_take isl_pw_aff *pa)
3903 if (isl_space_match(pma->dim, isl_dim_param, pa->dim, isl_dim_param))
3904 return pw_multi_aff_set_pw_aff(pma, pos, pa);
3905 if (!isl_space_has_named_params(pma->dim) ||
3906 !isl_space_has_named_params(pa->dim))
3907 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
3908 "unaligned unnamed parameters", goto error);
3909 pma = isl_pw_multi_aff_align_params(pma, isl_pw_aff_get_space(pa));
3910 pa = isl_pw_aff_align_params(pa, isl_pw_multi_aff_get_space(pma));
3911 return pw_multi_aff_set_pw_aff(pma, pos, pa);
3913 isl_pw_multi_aff_free(pma);
3914 isl_pw_aff_free(pa);