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 /* Return the constant term of "aff".
422 __isl_give isl_val *isl_aff_get_constant_val(__isl_keep isl_aff *aff)
430 ctx = isl_aff_get_ctx(aff);
431 v = isl_val_rat_from_isl_int(ctx, aff->v->el[1], aff->v->el[0]);
432 return isl_val_normalize(v);
435 int isl_aff_get_coefficient(__isl_keep isl_aff *aff,
436 enum isl_dim_type type, int pos, isl_int *v)
441 if (type == isl_dim_out)
442 isl_die(aff->v->ctx, isl_error_invalid,
443 "output/set dimension does not have a coefficient",
445 if (type == isl_dim_in)
448 if (pos >= isl_local_space_dim(aff->ls, type))
449 isl_die(aff->v->ctx, isl_error_invalid,
450 "position out of bounds", return -1);
452 pos += isl_local_space_offset(aff->ls, type);
453 isl_int_set(*v, aff->v->el[1 + pos]);
458 __isl_give isl_aff *isl_aff_set_denominator(__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[0], v);
473 __isl_give isl_aff *isl_aff_set_constant(__isl_take isl_aff *aff, isl_int v)
475 aff = isl_aff_cow(aff);
479 aff->v = isl_vec_cow(aff->v);
481 return isl_aff_free(aff);
483 isl_int_set(aff->v->el[1], v);
488 /* Replace the constant term of "aff" by "v".
490 __isl_give isl_aff *isl_aff_set_constant_val(__isl_take isl_aff *aff,
491 __isl_take isl_val *v)
496 if (!isl_val_is_rat(v))
497 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
498 "expecting rational value", goto error);
500 if (isl_int_eq(aff->v->el[1], v->n) &&
501 isl_int_eq(aff->v->el[0], v->d)) {
506 aff = isl_aff_cow(aff);
509 aff->v = isl_vec_cow(aff->v);
513 if (isl_int_eq(aff->v->el[0], v->d)) {
514 isl_int_set(aff->v->el[1], v->n);
515 } else if (isl_int_is_one(v->d)) {
516 isl_int_mul(aff->v->el[1], aff->v->el[0], v->n);
518 isl_seq_scale(aff->v->el + 1,
519 aff->v->el + 1, v->d, aff->v->size - 1);
520 isl_int_mul(aff->v->el[1], aff->v->el[0], v->n);
521 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
522 aff->v = isl_vec_normalize(aff->v);
535 __isl_give isl_aff *isl_aff_add_constant(__isl_take isl_aff *aff, isl_int v)
537 if (isl_int_is_zero(v))
540 aff = isl_aff_cow(aff);
544 aff->v = isl_vec_cow(aff->v);
546 return isl_aff_free(aff);
548 isl_int_addmul(aff->v->el[1], aff->v->el[0], v);
553 /* Add "v" to the constant term of "aff".
555 __isl_give isl_aff *isl_aff_add_constant_val(__isl_take isl_aff *aff,
556 __isl_take isl_val *v)
561 if (isl_val_is_zero(v)) {
566 if (!isl_val_is_rat(v))
567 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
568 "expecting rational value", goto error);
570 aff = isl_aff_cow(aff);
574 aff->v = isl_vec_cow(aff->v);
578 if (isl_int_is_one(v->d)) {
579 isl_int_addmul(aff->v->el[1], aff->v->el[0], v->n);
580 } else if (isl_int_eq(aff->v->el[0], v->d)) {
581 isl_int_add(aff->v->el[1], aff->v->el[1], v->n);
582 aff->v = isl_vec_normalize(aff->v);
586 isl_seq_scale(aff->v->el + 1,
587 aff->v->el + 1, v->d, aff->v->size - 1);
588 isl_int_addmul(aff->v->el[1], aff->v->el[0], v->n);
589 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
590 aff->v = isl_vec_normalize(aff->v);
603 __isl_give isl_aff *isl_aff_add_constant_si(__isl_take isl_aff *aff, int v)
608 isl_int_set_si(t, v);
609 aff = isl_aff_add_constant(aff, t);
615 /* Add "v" to the numerator of the constant term of "aff".
617 __isl_give isl_aff *isl_aff_add_constant_num(__isl_take isl_aff *aff, isl_int v)
619 if (isl_int_is_zero(v))
622 aff = isl_aff_cow(aff);
626 aff->v = isl_vec_cow(aff->v);
628 return isl_aff_free(aff);
630 isl_int_add(aff->v->el[1], aff->v->el[1], v);
635 /* Add "v" to the numerator of the constant term of "aff".
637 __isl_give isl_aff *isl_aff_add_constant_num_si(__isl_take isl_aff *aff, int v)
645 isl_int_set_si(t, v);
646 aff = isl_aff_add_constant_num(aff, t);
652 __isl_give isl_aff *isl_aff_set_constant_si(__isl_take isl_aff *aff, int v)
654 aff = isl_aff_cow(aff);
658 aff->v = isl_vec_cow(aff->v);
660 return isl_aff_free(aff);
662 isl_int_set_si(aff->v->el[1], v);
667 __isl_give isl_aff *isl_aff_set_coefficient(__isl_take isl_aff *aff,
668 enum isl_dim_type type, int pos, isl_int v)
673 if (type == isl_dim_out)
674 isl_die(aff->v->ctx, isl_error_invalid,
675 "output/set dimension does not have a coefficient",
676 return isl_aff_free(aff));
677 if (type == isl_dim_in)
680 if (pos >= isl_local_space_dim(aff->ls, type))
681 isl_die(aff->v->ctx, isl_error_invalid,
682 "position out of bounds", return isl_aff_free(aff));
684 aff = isl_aff_cow(aff);
688 aff->v = isl_vec_cow(aff->v);
690 return isl_aff_free(aff);
692 pos += isl_local_space_offset(aff->ls, type);
693 isl_int_set(aff->v->el[1 + pos], v);
698 __isl_give isl_aff *isl_aff_set_coefficient_si(__isl_take isl_aff *aff,
699 enum isl_dim_type type, int pos, int v)
704 if (type == isl_dim_out)
705 isl_die(aff->v->ctx, isl_error_invalid,
706 "output/set dimension does not have a coefficient",
707 return isl_aff_free(aff));
708 if (type == isl_dim_in)
711 if (pos >= isl_local_space_dim(aff->ls, type))
712 isl_die(aff->v->ctx, isl_error_invalid,
713 "position out of bounds", return isl_aff_free(aff));
715 aff = isl_aff_cow(aff);
719 aff->v = isl_vec_cow(aff->v);
721 return isl_aff_free(aff);
723 pos += isl_local_space_offset(aff->ls, type);
724 isl_int_set_si(aff->v->el[1 + pos], v);
729 /* Replace the coefficient of the variable of type "type" at position "pos"
732 __isl_give isl_aff *isl_aff_set_coefficient_val(__isl_take isl_aff *aff,
733 enum isl_dim_type type, int pos, __isl_take isl_val *v)
738 if (type == isl_dim_out)
739 isl_die(aff->v->ctx, isl_error_invalid,
740 "output/set dimension does not have a coefficient",
742 if (type == isl_dim_in)
745 if (pos >= isl_local_space_dim(aff->ls, type))
746 isl_die(aff->v->ctx, isl_error_invalid,
747 "position out of bounds", goto error);
749 if (!isl_val_is_rat(v))
750 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
751 "expecting rational value", goto error);
753 pos += isl_local_space_offset(aff->ls, type);
754 if (isl_int_eq(aff->v->el[1 + pos], v->n) &&
755 isl_int_eq(aff->v->el[0], v->d)) {
760 aff = isl_aff_cow(aff);
763 aff->v = isl_vec_cow(aff->v);
767 if (isl_int_eq(aff->v->el[0], v->d)) {
768 isl_int_set(aff->v->el[1 + pos], v->n);
769 } else if (isl_int_is_one(v->d)) {
770 isl_int_mul(aff->v->el[1 + pos], aff->v->el[0], v->n);
772 isl_seq_scale(aff->v->el + 1,
773 aff->v->el + 1, v->d, aff->v->size - 1);
774 isl_int_mul(aff->v->el[1 + pos], aff->v->el[0], v->n);
775 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
776 aff->v = isl_vec_normalize(aff->v);
789 __isl_give isl_aff *isl_aff_add_coefficient(__isl_take isl_aff *aff,
790 enum isl_dim_type type, int pos, isl_int v)
795 if (type == isl_dim_out)
796 isl_die(aff->v->ctx, isl_error_invalid,
797 "output/set dimension does not have a coefficient",
798 return isl_aff_free(aff));
799 if (type == isl_dim_in)
802 if (pos >= isl_local_space_dim(aff->ls, type))
803 isl_die(aff->v->ctx, isl_error_invalid,
804 "position out of bounds", return isl_aff_free(aff));
806 aff = isl_aff_cow(aff);
810 aff->v = isl_vec_cow(aff->v);
812 return isl_aff_free(aff);
814 pos += isl_local_space_offset(aff->ls, type);
815 isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v);
820 /* Add "v" to the coefficient of the variable of type "type"
821 * at position "pos" of "aff".
823 __isl_give isl_aff *isl_aff_add_coefficient_val(__isl_take isl_aff *aff,
824 enum isl_dim_type type, int pos, __isl_take isl_val *v)
829 if (isl_val_is_zero(v)) {
834 if (type == isl_dim_out)
835 isl_die(aff->v->ctx, isl_error_invalid,
836 "output/set dimension does not have a coefficient",
838 if (type == isl_dim_in)
841 if (pos >= isl_local_space_dim(aff->ls, type))
842 isl_die(aff->v->ctx, isl_error_invalid,
843 "position out of bounds", goto error);
845 if (!isl_val_is_rat(v))
846 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
847 "expecting rational value", goto error);
849 aff = isl_aff_cow(aff);
853 aff->v = isl_vec_cow(aff->v);
857 pos += isl_local_space_offset(aff->ls, type);
858 if (isl_int_is_one(v->d)) {
859 isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v->n);
860 } else if (isl_int_eq(aff->v->el[0], v->d)) {
861 isl_int_add(aff->v->el[1 + pos], aff->v->el[1 + pos], v->n);
862 aff->v = isl_vec_normalize(aff->v);
866 isl_seq_scale(aff->v->el + 1,
867 aff->v->el + 1, v->d, aff->v->size - 1);
868 isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v->n);
869 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
870 aff->v = isl_vec_normalize(aff->v);
883 __isl_give isl_aff *isl_aff_add_coefficient_si(__isl_take isl_aff *aff,
884 enum isl_dim_type type, int pos, int v)
889 isl_int_set_si(t, v);
890 aff = isl_aff_add_coefficient(aff, type, pos, t);
896 __isl_give isl_aff *isl_aff_get_div(__isl_keep isl_aff *aff, int pos)
901 return isl_local_space_get_div(aff->ls, pos);
904 __isl_give isl_aff *isl_aff_neg(__isl_take isl_aff *aff)
906 aff = isl_aff_cow(aff);
909 aff->v = isl_vec_cow(aff->v);
911 return isl_aff_free(aff);
913 isl_seq_neg(aff->v->el + 1, aff->v->el + 1, aff->v->size - 1);
918 /* Remove divs from the local space that do not appear in the affine
920 * We currently only remove divs at the end.
921 * Some intermediate divs may also not appear directly in the affine
922 * expression, but we would also need to check that no other divs are
923 * defined in terms of them.
925 __isl_give isl_aff *isl_aff_remove_unused_divs( __isl_take isl_aff *aff)
934 n = isl_local_space_dim(aff->ls, isl_dim_div);
935 off = isl_local_space_offset(aff->ls, isl_dim_div);
937 pos = isl_seq_last_non_zero(aff->v->el + 1 + off, n) + 1;
941 aff = isl_aff_cow(aff);
945 aff->ls = isl_local_space_drop_dims(aff->ls, isl_dim_div, pos, n - pos);
946 aff->v = isl_vec_drop_els(aff->v, 1 + off + pos, n - pos);
947 if (!aff->ls || !aff->v)
948 return isl_aff_free(aff);
953 /* Given two affine expressions "p" of length p_len (including the
954 * denominator and the constant term) and "subs" of length subs_len,
955 * plug in "subs" for the variable at position "pos".
956 * The variables of "subs" and "p" are assumed to match up to subs_len,
957 * but "p" may have additional variables.
958 * "v" is an initialized isl_int that can be used internally.
960 * In particular, if "p" represents the expression
964 * with i the variable at position "pos" and "subs" represents the expression
968 * then the result represents the expression
973 void isl_seq_substitute(isl_int *p, int pos, isl_int *subs,
974 int p_len, int subs_len, isl_int v)
976 isl_int_set(v, p[1 + pos]);
977 isl_int_set_si(p[1 + pos], 0);
978 isl_seq_combine(p + 1, subs[0], p + 1, v, subs + 1, subs_len - 1);
979 isl_seq_scale(p + subs_len, p + subs_len, subs[0], p_len - subs_len);
980 isl_int_mul(p[0], p[0], subs[0]);
983 /* Look for any divs in the aff->ls with a denominator equal to one
984 * and plug them into the affine expression and any subsequent divs
985 * that may reference the div.
987 static __isl_give isl_aff *plug_in_integral_divs(__isl_take isl_aff *aff)
999 n = isl_local_space_dim(aff->ls, isl_dim_div);
1001 for (i = 0; i < n; ++i) {
1002 if (!isl_int_is_one(aff->ls->div->row[i][0]))
1004 ls = isl_local_space_copy(aff->ls);
1005 ls = isl_local_space_substitute_seq(ls, isl_dim_div, i,
1006 aff->ls->div->row[i], len, i + 1, n - (i + 1));
1007 vec = isl_vec_copy(aff->v);
1008 vec = isl_vec_cow(vec);
1014 pos = isl_local_space_offset(aff->ls, isl_dim_div) + i;
1015 isl_seq_substitute(vec->el, pos, aff->ls->div->row[i],
1020 isl_vec_free(aff->v);
1022 isl_local_space_free(aff->ls);
1029 isl_local_space_free(ls);
1030 return isl_aff_free(aff);
1033 /* Look for any divs j that appear with a unit coefficient inside
1034 * the definitions of other divs i and plug them into the definitions
1037 * In particular, an expression of the form
1039 * floor((f(..) + floor(g(..)/n))/m)
1043 * floor((n * f(..) + g(..))/(n * m))
1045 * This simplification is correct because we can move the expression
1046 * f(..) into the inner floor in the original expression to obtain
1048 * floor(floor((n * f(..) + g(..))/n)/m)
1050 * from which we can derive the simplified expression.
1052 static __isl_give isl_aff *plug_in_unit_divs(__isl_take isl_aff *aff)
1060 n = isl_local_space_dim(aff->ls, isl_dim_div);
1061 off = isl_local_space_offset(aff->ls, isl_dim_div);
1062 for (i = 1; i < n; ++i) {
1063 for (j = 0; j < i; ++j) {
1064 if (!isl_int_is_one(aff->ls->div->row[i][1 + off + j]))
1066 aff->ls = isl_local_space_substitute_seq(aff->ls,
1067 isl_dim_div, j, aff->ls->div->row[j],
1068 aff->v->size, i, 1);
1070 return isl_aff_free(aff);
1077 /* Swap divs "a" and "b" in "aff", which is assumed to be non-NULL.
1079 * Even though this function is only called on isl_affs with a single
1080 * reference, we are careful to only change aff->v and aff->ls together.
1082 static __isl_give isl_aff *swap_div(__isl_take isl_aff *aff, int a, int b)
1084 unsigned off = isl_local_space_offset(aff->ls, isl_dim_div);
1085 isl_local_space *ls;
1088 ls = isl_local_space_copy(aff->ls);
1089 ls = isl_local_space_swap_div(ls, a, b);
1090 v = isl_vec_copy(aff->v);
1095 isl_int_swap(v->el[1 + off + a], v->el[1 + off + b]);
1096 isl_vec_free(aff->v);
1098 isl_local_space_free(aff->ls);
1104 isl_local_space_free(ls);
1105 return isl_aff_free(aff);
1108 /* Merge divs "a" and "b" in "aff", which is assumed to be non-NULL.
1110 * We currently do not actually remove div "b", but simply add its
1111 * coefficient to that of "a" and then zero it out.
1113 static __isl_give isl_aff *merge_divs(__isl_take isl_aff *aff, int a, int b)
1115 unsigned off = isl_local_space_offset(aff->ls, isl_dim_div);
1117 if (isl_int_is_zero(aff->v->el[1 + off + b]))
1120 aff->v = isl_vec_cow(aff->v);
1122 return isl_aff_free(aff);
1124 isl_int_add(aff->v->el[1 + off + a],
1125 aff->v->el[1 + off + a], aff->v->el[1 + off + b]);
1126 isl_int_set_si(aff->v->el[1 + off + b], 0);
1131 /* Sort the divs in the local space of "aff" according to
1132 * the comparison function "cmp_row" in isl_local_space.c,
1133 * combining the coefficients of identical divs.
1135 * Reordering divs does not change the semantics of "aff",
1136 * so there is no need to call isl_aff_cow.
1137 * Moreover, this function is currently only called on isl_affs
1138 * with a single reference.
1140 static __isl_give isl_aff *sort_divs(__isl_take isl_aff *aff)
1148 off = isl_local_space_offset(aff->ls, isl_dim_div);
1149 n = isl_aff_dim(aff, isl_dim_div);
1150 for (i = 1; i < n; ++i) {
1151 for (j = i - 1; j >= 0; --j) {
1152 int cmp = isl_mat_cmp_div(aff->ls->div, j, j + 1);
1156 aff = merge_divs(aff, j, j + 1);
1158 aff = swap_div(aff, j, j + 1);
1167 /* Normalize the representation of "aff".
1169 * This function should only be called of "new" isl_affs, i.e.,
1170 * with only a single reference. We therefore do not need to
1171 * worry about affecting other instances.
1173 __isl_give isl_aff *isl_aff_normalize(__isl_take isl_aff *aff)
1177 aff->v = isl_vec_normalize(aff->v);
1179 return isl_aff_free(aff);
1180 aff = plug_in_integral_divs(aff);
1181 aff = plug_in_unit_divs(aff);
1182 aff = sort_divs(aff);
1183 aff = isl_aff_remove_unused_divs(aff);
1187 /* Given f, return floor(f).
1188 * If f is an integer expression, then just return f.
1189 * If f is a constant, then return the constant floor(f).
1190 * Otherwise, if f = g/m, write g = q m + r,
1191 * create a new div d = [r/m] and return the expression q + d.
1192 * The coefficients in r are taken to lie between -m/2 and m/2.
1194 __isl_give isl_aff *isl_aff_floor(__isl_take isl_aff *aff)
1204 if (isl_int_is_one(aff->v->el[0]))
1207 aff = isl_aff_cow(aff);
1211 aff->v = isl_vec_cow(aff->v);
1213 return isl_aff_free(aff);
1215 if (isl_aff_is_cst(aff)) {
1216 isl_int_fdiv_q(aff->v->el[1], aff->v->el[1], aff->v->el[0]);
1217 isl_int_set_si(aff->v->el[0], 1);
1221 div = isl_vec_copy(aff->v);
1222 div = isl_vec_cow(div);
1224 return isl_aff_free(aff);
1226 ctx = isl_aff_get_ctx(aff);
1227 isl_int_fdiv_q(aff->v->el[0], aff->v->el[0], ctx->two);
1228 for (i = 1; i < aff->v->size; ++i) {
1229 isl_int_fdiv_r(div->el[i], div->el[i], div->el[0]);
1230 isl_int_fdiv_q(aff->v->el[i], aff->v->el[i], div->el[0]);
1231 if (isl_int_gt(div->el[i], aff->v->el[0])) {
1232 isl_int_sub(div->el[i], div->el[i], div->el[0]);
1233 isl_int_add_ui(aff->v->el[i], aff->v->el[i], 1);
1237 aff->ls = isl_local_space_add_div(aff->ls, div);
1239 return isl_aff_free(aff);
1241 size = aff->v->size;
1242 aff->v = isl_vec_extend(aff->v, size + 1);
1244 return isl_aff_free(aff);
1245 isl_int_set_si(aff->v->el[0], 1);
1246 isl_int_set_si(aff->v->el[size], 1);
1248 aff = isl_aff_normalize(aff);
1255 * aff mod m = aff - m * floor(aff/m)
1257 __isl_give isl_aff *isl_aff_mod(__isl_take isl_aff *aff, isl_int m)
1261 res = isl_aff_copy(aff);
1262 aff = isl_aff_scale_down(aff, m);
1263 aff = isl_aff_floor(aff);
1264 aff = isl_aff_scale(aff, m);
1265 res = isl_aff_sub(res, aff);
1272 * pwaff mod m = pwaff - m * floor(pwaff/m)
1274 __isl_give isl_pw_aff *isl_pw_aff_mod(__isl_take isl_pw_aff *pwaff, isl_int m)
1278 res = isl_pw_aff_copy(pwaff);
1279 pwaff = isl_pw_aff_scale_down(pwaff, m);
1280 pwaff = isl_pw_aff_floor(pwaff);
1281 pwaff = isl_pw_aff_scale(pwaff, m);
1282 res = isl_pw_aff_sub(res, pwaff);
1287 /* Given f, return ceil(f).
1288 * If f is an integer expression, then just return f.
1289 * Otherwise, let f be the expression
1295 * floor((e + m - 1)/m)
1297 __isl_give isl_aff *isl_aff_ceil(__isl_take isl_aff *aff)
1302 if (isl_int_is_one(aff->v->el[0]))
1305 aff = isl_aff_cow(aff);
1308 aff->v = isl_vec_cow(aff->v);
1310 return isl_aff_free(aff);
1312 isl_int_add(aff->v->el[1], aff->v->el[1], aff->v->el[0]);
1313 isl_int_sub_ui(aff->v->el[1], aff->v->el[1], 1);
1314 aff = isl_aff_floor(aff);
1319 /* Apply the expansion computed by isl_merge_divs.
1320 * The expansion itself is given by "exp" while the resulting
1321 * list of divs is given by "div".
1323 __isl_give isl_aff *isl_aff_expand_divs( __isl_take isl_aff *aff,
1324 __isl_take isl_mat *div, int *exp)
1331 aff = isl_aff_cow(aff);
1335 old_n_div = isl_local_space_dim(aff->ls, isl_dim_div);
1336 new_n_div = isl_mat_rows(div);
1337 if (new_n_div < old_n_div)
1338 isl_die(isl_mat_get_ctx(div), isl_error_invalid,
1339 "not an expansion", goto error);
1341 aff->v = isl_vec_extend(aff->v, aff->v->size + new_n_div - old_n_div);
1345 offset = 1 + isl_local_space_offset(aff->ls, isl_dim_div);
1347 for (i = new_n_div - 1; i >= 0; --i) {
1348 if (j >= 0 && exp[j] == i) {
1350 isl_int_swap(aff->v->el[offset + i],
1351 aff->v->el[offset + j]);
1354 isl_int_set_si(aff->v->el[offset + i], 0);
1357 aff->ls = isl_local_space_replace_divs(aff->ls, isl_mat_copy(div));
1368 /* Add two affine expressions that live in the same local space.
1370 static __isl_give isl_aff *add_expanded(__isl_take isl_aff *aff1,
1371 __isl_take isl_aff *aff2)
1375 aff1 = isl_aff_cow(aff1);
1379 aff1->v = isl_vec_cow(aff1->v);
1385 isl_int_gcd(gcd, aff1->v->el[0], aff2->v->el[0]);
1386 isl_int_divexact(f, aff2->v->el[0], gcd);
1387 isl_seq_scale(aff1->v->el + 1, aff1->v->el + 1, f, aff1->v->size - 1);
1388 isl_int_divexact(f, aff1->v->el[0], gcd);
1389 isl_seq_addmul(aff1->v->el + 1, f, aff2->v->el + 1, aff1->v->size - 1);
1390 isl_int_divexact(f, aff2->v->el[0], gcd);
1391 isl_int_mul(aff1->v->el[0], aff1->v->el[0], f);
1403 __isl_give isl_aff *isl_aff_add(__isl_take isl_aff *aff1,
1404 __isl_take isl_aff *aff2)
1414 ctx = isl_aff_get_ctx(aff1);
1415 if (!isl_space_is_equal(aff1->ls->dim, aff2->ls->dim))
1416 isl_die(ctx, isl_error_invalid,
1417 "spaces don't match", goto error);
1419 if (aff1->ls->div->n_row == 0 && aff2->ls->div->n_row == 0)
1420 return add_expanded(aff1, aff2);
1422 exp1 = isl_alloc_array(ctx, int, aff1->ls->div->n_row);
1423 exp2 = isl_alloc_array(ctx, int, aff2->ls->div->n_row);
1427 div = isl_merge_divs(aff1->ls->div, aff2->ls->div, exp1, exp2);
1428 aff1 = isl_aff_expand_divs(aff1, isl_mat_copy(div), exp1);
1429 aff2 = isl_aff_expand_divs(aff2, div, exp2);
1433 return add_expanded(aff1, aff2);
1442 __isl_give isl_aff *isl_aff_sub(__isl_take isl_aff *aff1,
1443 __isl_take isl_aff *aff2)
1445 return isl_aff_add(aff1, isl_aff_neg(aff2));
1448 __isl_give isl_aff *isl_aff_scale(__isl_take isl_aff *aff, isl_int f)
1452 if (isl_int_is_one(f))
1455 aff = isl_aff_cow(aff);
1458 aff->v = isl_vec_cow(aff->v);
1460 return isl_aff_free(aff);
1462 if (isl_int_is_pos(f) && isl_int_is_divisible_by(aff->v->el[0], f)) {
1463 isl_int_divexact(aff->v->el[0], aff->v->el[0], f);
1468 isl_int_gcd(gcd, aff->v->el[0], f);
1469 isl_int_divexact(aff->v->el[0], aff->v->el[0], gcd);
1470 isl_int_divexact(gcd, f, gcd);
1471 isl_seq_scale(aff->v->el + 1, aff->v->el + 1, gcd, aff->v->size - 1);
1477 __isl_give isl_aff *isl_aff_scale_down(__isl_take isl_aff *aff, isl_int f)
1481 if (isl_int_is_one(f))
1484 aff = isl_aff_cow(aff);
1488 if (isl_int_is_zero(f))
1489 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1490 "cannot scale down by zero", return isl_aff_free(aff));
1492 aff->v = isl_vec_cow(aff->v);
1494 return isl_aff_free(aff);
1497 isl_seq_gcd(aff->v->el + 1, aff->v->size - 1, &gcd);
1498 isl_int_gcd(gcd, gcd, f);
1499 isl_seq_scale_down(aff->v->el + 1, aff->v->el + 1, gcd, aff->v->size - 1);
1500 isl_int_divexact(gcd, f, gcd);
1501 isl_int_mul(aff->v->el[0], aff->v->el[0], gcd);
1507 __isl_give isl_aff *isl_aff_scale_down_ui(__isl_take isl_aff *aff, unsigned f)
1515 isl_int_set_ui(v, f);
1516 aff = isl_aff_scale_down(aff, v);
1522 __isl_give isl_aff *isl_aff_set_dim_name(__isl_take isl_aff *aff,
1523 enum isl_dim_type type, unsigned pos, const char *s)
1525 aff = isl_aff_cow(aff);
1528 if (type == isl_dim_out)
1529 isl_die(aff->v->ctx, isl_error_invalid,
1530 "cannot set name of output/set dimension",
1531 return isl_aff_free(aff));
1532 if (type == isl_dim_in)
1534 aff->ls = isl_local_space_set_dim_name(aff->ls, type, pos, s);
1536 return isl_aff_free(aff);
1541 __isl_give isl_aff *isl_aff_set_dim_id(__isl_take isl_aff *aff,
1542 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
1544 aff = isl_aff_cow(aff);
1546 return isl_id_free(id);
1547 if (type == isl_dim_out)
1548 isl_die(aff->v->ctx, isl_error_invalid,
1549 "cannot set name of output/set dimension",
1551 if (type == isl_dim_in)
1553 aff->ls = isl_local_space_set_dim_id(aff->ls, type, pos, id);
1555 return isl_aff_free(aff);
1564 /* Exploit the equalities in "eq" to simplify the affine expression
1565 * and the expressions of the integer divisions in the local space.
1566 * The integer divisions in this local space are assumed to appear
1567 * as regular dimensions in "eq".
1569 static __isl_give isl_aff *isl_aff_substitute_equalities_lifted(
1570 __isl_take isl_aff *aff, __isl_take isl_basic_set *eq)
1578 if (eq->n_eq == 0) {
1579 isl_basic_set_free(eq);
1583 aff = isl_aff_cow(aff);
1587 aff->ls = isl_local_space_substitute_equalities(aff->ls,
1588 isl_basic_set_copy(eq));
1589 aff->v = isl_vec_cow(aff->v);
1590 if (!aff->ls || !aff->v)
1593 total = 1 + isl_space_dim(eq->dim, isl_dim_all);
1595 for (i = 0; i < eq->n_eq; ++i) {
1596 j = isl_seq_last_non_zero(eq->eq[i], total + n_div);
1597 if (j < 0 || j == 0 || j >= total)
1600 isl_seq_elim(aff->v->el + 1, eq->eq[i], j, total,
1604 isl_basic_set_free(eq);
1605 aff = isl_aff_normalize(aff);
1608 isl_basic_set_free(eq);
1613 /* Exploit the equalities in "eq" to simplify the affine expression
1614 * and the expressions of the integer divisions in the local space.
1616 static __isl_give isl_aff *isl_aff_substitute_equalities(
1617 __isl_take isl_aff *aff, __isl_take isl_basic_set *eq)
1623 n_div = isl_local_space_dim(aff->ls, isl_dim_div);
1625 eq = isl_basic_set_add_dims(eq, isl_dim_set, n_div);
1626 return isl_aff_substitute_equalities_lifted(aff, eq);
1628 isl_basic_set_free(eq);
1633 /* Look for equalities among the variables shared by context and aff
1634 * and the integer divisions of aff, if any.
1635 * The equalities are then used to eliminate coefficients and/or integer
1636 * divisions from aff.
1638 __isl_give isl_aff *isl_aff_gist(__isl_take isl_aff *aff,
1639 __isl_take isl_set *context)
1641 isl_basic_set *hull;
1646 n_div = isl_local_space_dim(aff->ls, isl_dim_div);
1648 isl_basic_set *bset;
1649 isl_local_space *ls;
1650 context = isl_set_add_dims(context, isl_dim_set, n_div);
1651 ls = isl_aff_get_domain_local_space(aff);
1652 bset = isl_basic_set_from_local_space(ls);
1653 bset = isl_basic_set_lift(bset);
1654 bset = isl_basic_set_flatten(bset);
1655 context = isl_set_intersect(context,
1656 isl_set_from_basic_set(bset));
1659 hull = isl_set_affine_hull(context);
1660 return isl_aff_substitute_equalities_lifted(aff, hull);
1663 isl_set_free(context);
1667 __isl_give isl_aff *isl_aff_gist_params(__isl_take isl_aff *aff,
1668 __isl_take isl_set *context)
1670 isl_set *dom_context = isl_set_universe(isl_aff_get_domain_space(aff));
1671 dom_context = isl_set_intersect_params(dom_context, context);
1672 return isl_aff_gist(aff, dom_context);
1675 /* Return a basic set containing those elements in the space
1676 * of aff where it is non-negative.
1677 * If "rational" is set, then return a rational basic set.
1679 static __isl_give isl_basic_set *aff_nonneg_basic_set(
1680 __isl_take isl_aff *aff, int rational)
1682 isl_constraint *ineq;
1683 isl_basic_set *bset;
1685 ineq = isl_inequality_from_aff(aff);
1687 bset = isl_basic_set_from_constraint(ineq);
1689 bset = isl_basic_set_set_rational(bset);
1690 bset = isl_basic_set_simplify(bset);
1694 /* Return a basic set containing those elements in the space
1695 * of aff where it is non-negative.
1697 __isl_give isl_basic_set *isl_aff_nonneg_basic_set(__isl_take isl_aff *aff)
1699 return aff_nonneg_basic_set(aff, 0);
1702 /* Return a basic set containing those elements in the domain space
1703 * of aff where it is negative.
1705 __isl_give isl_basic_set *isl_aff_neg_basic_set(__isl_take isl_aff *aff)
1707 aff = isl_aff_neg(aff);
1708 aff = isl_aff_add_constant_num_si(aff, -1);
1709 return isl_aff_nonneg_basic_set(aff);
1712 /* Return a basic set containing those elements in the space
1713 * of aff where it is zero.
1714 * If "rational" is set, then return a rational basic set.
1716 static __isl_give isl_basic_set *aff_zero_basic_set(__isl_take isl_aff *aff,
1719 isl_constraint *ineq;
1720 isl_basic_set *bset;
1722 ineq = isl_equality_from_aff(aff);
1724 bset = isl_basic_set_from_constraint(ineq);
1726 bset = isl_basic_set_set_rational(bset);
1727 bset = isl_basic_set_simplify(bset);
1731 /* Return a basic set containing those elements in the space
1732 * of aff where it is zero.
1734 __isl_give isl_basic_set *isl_aff_zero_basic_set(__isl_take isl_aff *aff)
1736 return aff_zero_basic_set(aff, 0);
1739 /* Return a basic set containing those elements in the shared space
1740 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
1742 __isl_give isl_basic_set *isl_aff_ge_basic_set(__isl_take isl_aff *aff1,
1743 __isl_take isl_aff *aff2)
1745 aff1 = isl_aff_sub(aff1, aff2);
1747 return isl_aff_nonneg_basic_set(aff1);
1750 /* Return a basic set containing those elements in the shared space
1751 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
1753 __isl_give isl_basic_set *isl_aff_le_basic_set(__isl_take isl_aff *aff1,
1754 __isl_take isl_aff *aff2)
1756 return isl_aff_ge_basic_set(aff2, aff1);
1759 __isl_give isl_aff *isl_aff_add_on_domain(__isl_keep isl_set *dom,
1760 __isl_take isl_aff *aff1, __isl_take isl_aff *aff2)
1762 aff1 = isl_aff_add(aff1, aff2);
1763 aff1 = isl_aff_gist(aff1, isl_set_copy(dom));
1767 int isl_aff_is_empty(__isl_keep isl_aff *aff)
1775 /* Check whether the given affine expression has non-zero coefficient
1776 * for any dimension in the given range or if any of these dimensions
1777 * appear with non-zero coefficients in any of the integer divisions
1778 * involved in the affine expression.
1780 int isl_aff_involves_dims(__isl_keep isl_aff *aff,
1781 enum isl_dim_type type, unsigned first, unsigned n)
1793 ctx = isl_aff_get_ctx(aff);
1794 if (first + n > isl_aff_dim(aff, type))
1795 isl_die(ctx, isl_error_invalid,
1796 "range out of bounds", return -1);
1798 active = isl_local_space_get_active(aff->ls, aff->v->el + 2);
1802 first += isl_local_space_offset(aff->ls, type) - 1;
1803 for (i = 0; i < n; ++i)
1804 if (active[first + i]) {
1817 __isl_give isl_aff *isl_aff_drop_dims(__isl_take isl_aff *aff,
1818 enum isl_dim_type type, unsigned first, unsigned n)
1824 if (type == isl_dim_out)
1825 isl_die(aff->v->ctx, isl_error_invalid,
1826 "cannot drop output/set dimension",
1827 return isl_aff_free(aff));
1828 if (type == isl_dim_in)
1830 if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
1833 ctx = isl_aff_get_ctx(aff);
1834 if (first + n > isl_local_space_dim(aff->ls, type))
1835 isl_die(ctx, isl_error_invalid, "range out of bounds",
1836 return isl_aff_free(aff));
1838 aff = isl_aff_cow(aff);
1842 aff->ls = isl_local_space_drop_dims(aff->ls, type, first, n);
1844 return isl_aff_free(aff);
1846 first += 1 + isl_local_space_offset(aff->ls, type);
1847 aff->v = isl_vec_drop_els(aff->v, first, n);
1849 return isl_aff_free(aff);
1854 /* Project the domain of the affine expression onto its parameter space.
1855 * The affine expression may not involve any of the domain dimensions.
1857 __isl_give isl_aff *isl_aff_project_domain_on_params(__isl_take isl_aff *aff)
1863 n = isl_aff_dim(aff, isl_dim_in);
1864 involves = isl_aff_involves_dims(aff, isl_dim_in, 0, n);
1866 return isl_aff_free(aff);
1868 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1869 "affine expression involves some of the domain dimensions",
1870 return isl_aff_free(aff));
1871 aff = isl_aff_drop_dims(aff, isl_dim_in, 0, n);
1872 space = isl_aff_get_domain_space(aff);
1873 space = isl_space_params(space);
1874 aff = isl_aff_reset_domain_space(aff, space);
1878 __isl_give isl_aff *isl_aff_insert_dims(__isl_take isl_aff *aff,
1879 enum isl_dim_type type, unsigned first, unsigned n)
1885 if (type == isl_dim_out)
1886 isl_die(aff->v->ctx, isl_error_invalid,
1887 "cannot insert output/set dimensions",
1888 return isl_aff_free(aff));
1889 if (type == isl_dim_in)
1891 if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
1894 ctx = isl_aff_get_ctx(aff);
1895 if (first > isl_local_space_dim(aff->ls, type))
1896 isl_die(ctx, isl_error_invalid, "position out of bounds",
1897 return isl_aff_free(aff));
1899 aff = isl_aff_cow(aff);
1903 aff->ls = isl_local_space_insert_dims(aff->ls, type, first, n);
1905 return isl_aff_free(aff);
1907 first += 1 + isl_local_space_offset(aff->ls, type);
1908 aff->v = isl_vec_insert_zero_els(aff->v, first, n);
1910 return isl_aff_free(aff);
1915 __isl_give isl_aff *isl_aff_add_dims(__isl_take isl_aff *aff,
1916 enum isl_dim_type type, unsigned n)
1920 pos = isl_aff_dim(aff, type);
1922 return isl_aff_insert_dims(aff, type, pos, n);
1925 __isl_give isl_pw_aff *isl_pw_aff_add_dims(__isl_take isl_pw_aff *pwaff,
1926 enum isl_dim_type type, unsigned n)
1930 pos = isl_pw_aff_dim(pwaff, type);
1932 return isl_pw_aff_insert_dims(pwaff, type, pos, n);
1935 __isl_give isl_pw_aff *isl_pw_aff_from_aff(__isl_take isl_aff *aff)
1937 isl_set *dom = isl_set_universe(isl_aff_get_domain_space(aff));
1938 return isl_pw_aff_alloc(dom, aff);
1942 #define PW isl_pw_aff
1946 #define EL_IS_ZERO is_empty
1950 #define IS_ZERO is_empty
1953 #undef DEFAULT_IS_ZERO
1954 #define DEFAULT_IS_ZERO 0
1958 #define NO_MOVE_DIMS
1962 #include <isl_pw_templ.c>
1964 static __isl_give isl_set *align_params_pw_pw_set_and(
1965 __isl_take isl_pw_aff *pwaff1, __isl_take isl_pw_aff *pwaff2,
1966 __isl_give isl_set *(*fn)(__isl_take isl_pw_aff *pwaff1,
1967 __isl_take isl_pw_aff *pwaff2))
1969 if (!pwaff1 || !pwaff2)
1971 if (isl_space_match(pwaff1->dim, isl_dim_param,
1972 pwaff2->dim, isl_dim_param))
1973 return fn(pwaff1, pwaff2);
1974 if (!isl_space_has_named_params(pwaff1->dim) ||
1975 !isl_space_has_named_params(pwaff2->dim))
1976 isl_die(isl_pw_aff_get_ctx(pwaff1), isl_error_invalid,
1977 "unaligned unnamed parameters", goto error);
1978 pwaff1 = isl_pw_aff_align_params(pwaff1, isl_pw_aff_get_space(pwaff2));
1979 pwaff2 = isl_pw_aff_align_params(pwaff2, isl_pw_aff_get_space(pwaff1));
1980 return fn(pwaff1, pwaff2);
1982 isl_pw_aff_free(pwaff1);
1983 isl_pw_aff_free(pwaff2);
1987 /* Compute a piecewise quasi-affine expression with a domain that
1988 * is the union of those of pwaff1 and pwaff2 and such that on each
1989 * cell, the quasi-affine expression is the better (according to cmp)
1990 * of those of pwaff1 and pwaff2. If only one of pwaff1 or pwaff2
1991 * is defined on a given cell, then the associated expression
1992 * is the defined one.
1994 static __isl_give isl_pw_aff *pw_aff_union_opt(__isl_take isl_pw_aff *pwaff1,
1995 __isl_take isl_pw_aff *pwaff2,
1996 __isl_give isl_basic_set *(*cmp)(__isl_take isl_aff *aff1,
1997 __isl_take isl_aff *aff2))
2004 if (!pwaff1 || !pwaff2)
2007 ctx = isl_space_get_ctx(pwaff1->dim);
2008 if (!isl_space_is_equal(pwaff1->dim, pwaff2->dim))
2009 isl_die(ctx, isl_error_invalid,
2010 "arguments should live in same space", goto error);
2012 if (isl_pw_aff_is_empty(pwaff1)) {
2013 isl_pw_aff_free(pwaff1);
2017 if (isl_pw_aff_is_empty(pwaff2)) {
2018 isl_pw_aff_free(pwaff2);
2022 n = 2 * (pwaff1->n + 1) * (pwaff2->n + 1);
2023 res = isl_pw_aff_alloc_size(isl_space_copy(pwaff1->dim), n);
2025 for (i = 0; i < pwaff1->n; ++i) {
2026 set = isl_set_copy(pwaff1->p[i].set);
2027 for (j = 0; j < pwaff2->n; ++j) {
2028 struct isl_set *common;
2031 common = isl_set_intersect(
2032 isl_set_copy(pwaff1->p[i].set),
2033 isl_set_copy(pwaff2->p[j].set));
2034 better = isl_set_from_basic_set(cmp(
2035 isl_aff_copy(pwaff2->p[j].aff),
2036 isl_aff_copy(pwaff1->p[i].aff)));
2037 better = isl_set_intersect(common, better);
2038 if (isl_set_plain_is_empty(better)) {
2039 isl_set_free(better);
2042 set = isl_set_subtract(set, isl_set_copy(better));
2044 res = isl_pw_aff_add_piece(res, better,
2045 isl_aff_copy(pwaff2->p[j].aff));
2047 res = isl_pw_aff_add_piece(res, set,
2048 isl_aff_copy(pwaff1->p[i].aff));
2051 for (j = 0; j < pwaff2->n; ++j) {
2052 set = isl_set_copy(pwaff2->p[j].set);
2053 for (i = 0; i < pwaff1->n; ++i)
2054 set = isl_set_subtract(set,
2055 isl_set_copy(pwaff1->p[i].set));
2056 res = isl_pw_aff_add_piece(res, set,
2057 isl_aff_copy(pwaff2->p[j].aff));
2060 isl_pw_aff_free(pwaff1);
2061 isl_pw_aff_free(pwaff2);
2065 isl_pw_aff_free(pwaff1);
2066 isl_pw_aff_free(pwaff2);
2070 /* Compute a piecewise quasi-affine expression with a domain that
2071 * is the union of those of pwaff1 and pwaff2 and such that on each
2072 * cell, the quasi-affine expression is the maximum of those of pwaff1
2073 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2074 * cell, then the associated expression is the defined one.
2076 static __isl_give isl_pw_aff *pw_aff_union_max(__isl_take isl_pw_aff *pwaff1,
2077 __isl_take isl_pw_aff *pwaff2)
2079 return pw_aff_union_opt(pwaff1, pwaff2, &isl_aff_ge_basic_set);
2082 __isl_give isl_pw_aff *isl_pw_aff_union_max(__isl_take isl_pw_aff *pwaff1,
2083 __isl_take isl_pw_aff *pwaff2)
2085 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2,
2089 /* Compute a piecewise quasi-affine expression with a domain that
2090 * is the union of those of pwaff1 and pwaff2 and such that on each
2091 * cell, the quasi-affine expression is the minimum of those of pwaff1
2092 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2093 * cell, then the associated expression is the defined one.
2095 static __isl_give isl_pw_aff *pw_aff_union_min(__isl_take isl_pw_aff *pwaff1,
2096 __isl_take isl_pw_aff *pwaff2)
2098 return pw_aff_union_opt(pwaff1, pwaff2, &isl_aff_le_basic_set);
2101 __isl_give isl_pw_aff *isl_pw_aff_union_min(__isl_take isl_pw_aff *pwaff1,
2102 __isl_take isl_pw_aff *pwaff2)
2104 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2,
2108 __isl_give isl_pw_aff *isl_pw_aff_union_opt(__isl_take isl_pw_aff *pwaff1,
2109 __isl_take isl_pw_aff *pwaff2, int max)
2112 return isl_pw_aff_union_max(pwaff1, pwaff2);
2114 return isl_pw_aff_union_min(pwaff1, pwaff2);
2117 /* Construct a map with as domain the domain of pwaff and
2118 * one-dimensional range corresponding to the affine expressions.
2120 static __isl_give isl_map *map_from_pw_aff(__isl_take isl_pw_aff *pwaff)
2129 dim = isl_pw_aff_get_space(pwaff);
2130 map = isl_map_empty(dim);
2132 for (i = 0; i < pwaff->n; ++i) {
2133 isl_basic_map *bmap;
2136 bmap = isl_basic_map_from_aff(isl_aff_copy(pwaff->p[i].aff));
2137 map_i = isl_map_from_basic_map(bmap);
2138 map_i = isl_map_intersect_domain(map_i,
2139 isl_set_copy(pwaff->p[i].set));
2140 map = isl_map_union_disjoint(map, map_i);
2143 isl_pw_aff_free(pwaff);
2148 /* Construct a map with as domain the domain of pwaff and
2149 * one-dimensional range corresponding to the affine expressions.
2151 __isl_give isl_map *isl_map_from_pw_aff(__isl_take isl_pw_aff *pwaff)
2155 if (isl_space_is_set(pwaff->dim))
2156 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
2157 "space of input is not a map",
2158 return isl_pw_aff_free(pwaff));
2159 return map_from_pw_aff(pwaff);
2162 /* Construct a one-dimensional set with as parameter domain
2163 * the domain of pwaff and the single set dimension
2164 * corresponding to the affine expressions.
2166 __isl_give isl_set *isl_set_from_pw_aff(__isl_take isl_pw_aff *pwaff)
2170 if (!isl_space_is_set(pwaff->dim))
2171 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
2172 "space of input is not a set",
2173 return isl_pw_aff_free(pwaff));
2174 return map_from_pw_aff(pwaff);
2177 /* Return a set containing those elements in the domain
2178 * of pwaff where it is non-negative.
2180 __isl_give isl_set *isl_pw_aff_nonneg_set(__isl_take isl_pw_aff *pwaff)
2188 set = isl_set_empty(isl_pw_aff_get_domain_space(pwaff));
2190 for (i = 0; i < pwaff->n; ++i) {
2191 isl_basic_set *bset;
2195 rational = isl_set_has_rational(pwaff->p[i].set);
2196 bset = aff_nonneg_basic_set(isl_aff_copy(pwaff->p[i].aff),
2198 set_i = isl_set_from_basic_set(bset);
2199 set_i = isl_set_intersect(set_i, isl_set_copy(pwaff->p[i].set));
2200 set = isl_set_union_disjoint(set, set_i);
2203 isl_pw_aff_free(pwaff);
2208 /* Return a set containing those elements in the domain
2209 * of pwaff where it is zero (if complement is 0) or not zero
2210 * (if complement is 1).
2212 static __isl_give isl_set *pw_aff_zero_set(__isl_take isl_pw_aff *pwaff,
2221 set = isl_set_empty(isl_pw_aff_get_domain_space(pwaff));
2223 for (i = 0; i < pwaff->n; ++i) {
2224 isl_basic_set *bset;
2225 isl_set *set_i, *zero;
2228 rational = isl_set_has_rational(pwaff->p[i].set);
2229 bset = aff_zero_basic_set(isl_aff_copy(pwaff->p[i].aff),
2231 zero = isl_set_from_basic_set(bset);
2232 set_i = isl_set_copy(pwaff->p[i].set);
2234 set_i = isl_set_subtract(set_i, zero);
2236 set_i = isl_set_intersect(set_i, zero);
2237 set = isl_set_union_disjoint(set, set_i);
2240 isl_pw_aff_free(pwaff);
2245 /* Return a set containing those elements in the domain
2246 * of pwaff where it is zero.
2248 __isl_give isl_set *isl_pw_aff_zero_set(__isl_take isl_pw_aff *pwaff)
2250 return pw_aff_zero_set(pwaff, 0);
2253 /* Return a set containing those elements in the domain
2254 * of pwaff where it is not zero.
2256 __isl_give isl_set *isl_pw_aff_non_zero_set(__isl_take isl_pw_aff *pwaff)
2258 return pw_aff_zero_set(pwaff, 1);
2261 /* Return a set containing those elements in the shared domain
2262 * of pwaff1 and pwaff2 where pwaff1 is greater than (or equal) to pwaff2.
2264 * We compute the difference on the shared domain and then construct
2265 * the set of values where this difference is non-negative.
2266 * If strict is set, we first subtract 1 from the difference.
2267 * If equal is set, we only return the elements where pwaff1 and pwaff2
2270 static __isl_give isl_set *pw_aff_gte_set(__isl_take isl_pw_aff *pwaff1,
2271 __isl_take isl_pw_aff *pwaff2, int strict, int equal)
2273 isl_set *set1, *set2;
2275 set1 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff1));
2276 set2 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff2));
2277 set1 = isl_set_intersect(set1, set2);
2278 pwaff1 = isl_pw_aff_intersect_domain(pwaff1, isl_set_copy(set1));
2279 pwaff2 = isl_pw_aff_intersect_domain(pwaff2, isl_set_copy(set1));
2280 pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_neg(pwaff2));
2283 isl_space *dim = isl_set_get_space(set1);
2285 aff = isl_aff_zero_on_domain(isl_local_space_from_space(dim));
2286 aff = isl_aff_add_constant_si(aff, -1);
2287 pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_alloc(set1, aff));
2292 return isl_pw_aff_zero_set(pwaff1);
2293 return isl_pw_aff_nonneg_set(pwaff1);
2296 /* Return a set containing those elements in the shared domain
2297 * of pwaff1 and pwaff2 where pwaff1 is equal to pwaff2.
2299 static __isl_give isl_set *pw_aff_eq_set(__isl_take isl_pw_aff *pwaff1,
2300 __isl_take isl_pw_aff *pwaff2)
2302 return pw_aff_gte_set(pwaff1, pwaff2, 0, 1);
2305 __isl_give isl_set *isl_pw_aff_eq_set(__isl_take isl_pw_aff *pwaff1,
2306 __isl_take isl_pw_aff *pwaff2)
2308 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_eq_set);
2311 /* Return a set containing those elements in the shared domain
2312 * of pwaff1 and pwaff2 where pwaff1 is greater than or equal to pwaff2.
2314 static __isl_give isl_set *pw_aff_ge_set(__isl_take isl_pw_aff *pwaff1,
2315 __isl_take isl_pw_aff *pwaff2)
2317 return pw_aff_gte_set(pwaff1, pwaff2, 0, 0);
2320 __isl_give isl_set *isl_pw_aff_ge_set(__isl_take isl_pw_aff *pwaff1,
2321 __isl_take isl_pw_aff *pwaff2)
2323 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_ge_set);
2326 /* Return a set containing those elements in the shared domain
2327 * of pwaff1 and pwaff2 where pwaff1 is strictly greater than pwaff2.
2329 static __isl_give isl_set *pw_aff_gt_set(__isl_take isl_pw_aff *pwaff1,
2330 __isl_take isl_pw_aff *pwaff2)
2332 return pw_aff_gte_set(pwaff1, pwaff2, 1, 0);
2335 __isl_give isl_set *isl_pw_aff_gt_set(__isl_take isl_pw_aff *pwaff1,
2336 __isl_take isl_pw_aff *pwaff2)
2338 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_gt_set);
2341 __isl_give isl_set *isl_pw_aff_le_set(__isl_take isl_pw_aff *pwaff1,
2342 __isl_take isl_pw_aff *pwaff2)
2344 return isl_pw_aff_ge_set(pwaff2, pwaff1);
2347 __isl_give isl_set *isl_pw_aff_lt_set(__isl_take isl_pw_aff *pwaff1,
2348 __isl_take isl_pw_aff *pwaff2)
2350 return isl_pw_aff_gt_set(pwaff2, pwaff1);
2353 /* Return a set containing those elements in the shared domain
2354 * of the elements of list1 and list2 where each element in list1
2355 * has the relation specified by "fn" with each element in list2.
2357 static __isl_give isl_set *pw_aff_list_set(__isl_take isl_pw_aff_list *list1,
2358 __isl_take isl_pw_aff_list *list2,
2359 __isl_give isl_set *(*fn)(__isl_take isl_pw_aff *pwaff1,
2360 __isl_take isl_pw_aff *pwaff2))
2366 if (!list1 || !list2)
2369 ctx = isl_pw_aff_list_get_ctx(list1);
2370 if (list1->n < 1 || list2->n < 1)
2371 isl_die(ctx, isl_error_invalid,
2372 "list should contain at least one element", goto error);
2374 set = isl_set_universe(isl_pw_aff_get_domain_space(list1->p[0]));
2375 for (i = 0; i < list1->n; ++i)
2376 for (j = 0; j < list2->n; ++j) {
2379 set_ij = fn(isl_pw_aff_copy(list1->p[i]),
2380 isl_pw_aff_copy(list2->p[j]));
2381 set = isl_set_intersect(set, set_ij);
2384 isl_pw_aff_list_free(list1);
2385 isl_pw_aff_list_free(list2);
2388 isl_pw_aff_list_free(list1);
2389 isl_pw_aff_list_free(list2);
2393 /* Return a set containing those elements in the shared domain
2394 * of the elements of list1 and list2 where each element in list1
2395 * is equal to each element in list2.
2397 __isl_give isl_set *isl_pw_aff_list_eq_set(__isl_take isl_pw_aff_list *list1,
2398 __isl_take isl_pw_aff_list *list2)
2400 return pw_aff_list_set(list1, list2, &isl_pw_aff_eq_set);
2403 __isl_give isl_set *isl_pw_aff_list_ne_set(__isl_take isl_pw_aff_list *list1,
2404 __isl_take isl_pw_aff_list *list2)
2406 return pw_aff_list_set(list1, list2, &isl_pw_aff_ne_set);
2409 /* Return a set containing those elements in the shared domain
2410 * of the elements of list1 and list2 where each element in list1
2411 * is less than or equal to each element in list2.
2413 __isl_give isl_set *isl_pw_aff_list_le_set(__isl_take isl_pw_aff_list *list1,
2414 __isl_take isl_pw_aff_list *list2)
2416 return pw_aff_list_set(list1, list2, &isl_pw_aff_le_set);
2419 __isl_give isl_set *isl_pw_aff_list_lt_set(__isl_take isl_pw_aff_list *list1,
2420 __isl_take isl_pw_aff_list *list2)
2422 return pw_aff_list_set(list1, list2, &isl_pw_aff_lt_set);
2425 __isl_give isl_set *isl_pw_aff_list_ge_set(__isl_take isl_pw_aff_list *list1,
2426 __isl_take isl_pw_aff_list *list2)
2428 return pw_aff_list_set(list1, list2, &isl_pw_aff_ge_set);
2431 __isl_give isl_set *isl_pw_aff_list_gt_set(__isl_take isl_pw_aff_list *list1,
2432 __isl_take isl_pw_aff_list *list2)
2434 return pw_aff_list_set(list1, list2, &isl_pw_aff_gt_set);
2438 /* Return a set containing those elements in the shared domain
2439 * of pwaff1 and pwaff2 where pwaff1 is not equal to pwaff2.
2441 static __isl_give isl_set *pw_aff_ne_set(__isl_take isl_pw_aff *pwaff1,
2442 __isl_take isl_pw_aff *pwaff2)
2444 isl_set *set_lt, *set_gt;
2446 set_lt = isl_pw_aff_lt_set(isl_pw_aff_copy(pwaff1),
2447 isl_pw_aff_copy(pwaff2));
2448 set_gt = isl_pw_aff_gt_set(pwaff1, pwaff2);
2449 return isl_set_union_disjoint(set_lt, set_gt);
2452 __isl_give isl_set *isl_pw_aff_ne_set(__isl_take isl_pw_aff *pwaff1,
2453 __isl_take isl_pw_aff *pwaff2)
2455 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_ne_set);
2458 __isl_give isl_pw_aff *isl_pw_aff_scale_down(__isl_take isl_pw_aff *pwaff,
2463 if (isl_int_is_one(v))
2465 if (!isl_int_is_pos(v))
2466 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
2467 "factor needs to be positive",
2468 return isl_pw_aff_free(pwaff));
2469 pwaff = isl_pw_aff_cow(pwaff);
2475 for (i = 0; i < pwaff->n; ++i) {
2476 pwaff->p[i].aff = isl_aff_scale_down(pwaff->p[i].aff, v);
2477 if (!pwaff->p[i].aff)
2478 return isl_pw_aff_free(pwaff);
2484 __isl_give isl_pw_aff *isl_pw_aff_floor(__isl_take isl_pw_aff *pwaff)
2488 pwaff = isl_pw_aff_cow(pwaff);
2494 for (i = 0; i < pwaff->n; ++i) {
2495 pwaff->p[i].aff = isl_aff_floor(pwaff->p[i].aff);
2496 if (!pwaff->p[i].aff)
2497 return isl_pw_aff_free(pwaff);
2503 __isl_give isl_pw_aff *isl_pw_aff_ceil(__isl_take isl_pw_aff *pwaff)
2507 pwaff = isl_pw_aff_cow(pwaff);
2513 for (i = 0; i < pwaff->n; ++i) {
2514 pwaff->p[i].aff = isl_aff_ceil(pwaff->p[i].aff);
2515 if (!pwaff->p[i].aff)
2516 return isl_pw_aff_free(pwaff);
2522 /* Assuming that "cond1" and "cond2" are disjoint,
2523 * return an affine expression that is equal to pwaff1 on cond1
2524 * and to pwaff2 on cond2.
2526 static __isl_give isl_pw_aff *isl_pw_aff_select(
2527 __isl_take isl_set *cond1, __isl_take isl_pw_aff *pwaff1,
2528 __isl_take isl_set *cond2, __isl_take isl_pw_aff *pwaff2)
2530 pwaff1 = isl_pw_aff_intersect_domain(pwaff1, cond1);
2531 pwaff2 = isl_pw_aff_intersect_domain(pwaff2, cond2);
2533 return isl_pw_aff_add_disjoint(pwaff1, pwaff2);
2536 /* Return an affine expression that is equal to pwaff_true for elements
2537 * where "cond" is non-zero and to pwaff_false for elements where "cond"
2539 * That is, return cond ? pwaff_true : pwaff_false;
2541 __isl_give isl_pw_aff *isl_pw_aff_cond(__isl_take isl_pw_aff *cond,
2542 __isl_take isl_pw_aff *pwaff_true, __isl_take isl_pw_aff *pwaff_false)
2544 isl_set *cond_true, *cond_false;
2546 cond_true = isl_pw_aff_non_zero_set(isl_pw_aff_copy(cond));
2547 cond_false = isl_pw_aff_zero_set(cond);
2548 return isl_pw_aff_select(cond_true, pwaff_true,
2549 cond_false, pwaff_false);
2552 int isl_aff_is_cst(__isl_keep isl_aff *aff)
2557 return isl_seq_first_non_zero(aff->v->el + 2, aff->v->size - 2) == -1;
2560 /* Check whether pwaff is a piecewise constant.
2562 int isl_pw_aff_is_cst(__isl_keep isl_pw_aff *pwaff)
2569 for (i = 0; i < pwaff->n; ++i) {
2570 int is_cst = isl_aff_is_cst(pwaff->p[i].aff);
2571 if (is_cst < 0 || !is_cst)
2578 __isl_give isl_aff *isl_aff_mul(__isl_take isl_aff *aff1,
2579 __isl_take isl_aff *aff2)
2581 if (!isl_aff_is_cst(aff2) && isl_aff_is_cst(aff1))
2582 return isl_aff_mul(aff2, aff1);
2584 if (!isl_aff_is_cst(aff2))
2585 isl_die(isl_aff_get_ctx(aff1), isl_error_invalid,
2586 "at least one affine expression should be constant",
2589 aff1 = isl_aff_cow(aff1);
2593 aff1 = isl_aff_scale(aff1, aff2->v->el[1]);
2594 aff1 = isl_aff_scale_down(aff1, aff2->v->el[0]);
2604 /* Divide "aff1" by "aff2", assuming "aff2" is a piecewise constant.
2606 __isl_give isl_aff *isl_aff_div(__isl_take isl_aff *aff1,
2607 __isl_take isl_aff *aff2)
2612 is_cst = isl_aff_is_cst(aff2);
2616 isl_die(isl_aff_get_ctx(aff2), isl_error_invalid,
2617 "second argument should be a constant", goto error);
2622 neg = isl_int_is_neg(aff2->v->el[1]);
2624 isl_int_neg(aff2->v->el[0], aff2->v->el[0]);
2625 isl_int_neg(aff2->v->el[1], aff2->v->el[1]);
2628 aff1 = isl_aff_scale(aff1, aff2->v->el[0]);
2629 aff1 = isl_aff_scale_down(aff1, aff2->v->el[1]);
2632 isl_int_neg(aff2->v->el[0], aff2->v->el[0]);
2633 isl_int_neg(aff2->v->el[1], aff2->v->el[1]);
2644 static __isl_give isl_pw_aff *pw_aff_add(__isl_take isl_pw_aff *pwaff1,
2645 __isl_take isl_pw_aff *pwaff2)
2647 return isl_pw_aff_on_shared_domain(pwaff1, pwaff2, &isl_aff_add);
2650 __isl_give isl_pw_aff *isl_pw_aff_add(__isl_take isl_pw_aff *pwaff1,
2651 __isl_take isl_pw_aff *pwaff2)
2653 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_add);
2656 __isl_give isl_pw_aff *isl_pw_aff_union_add(__isl_take isl_pw_aff *pwaff1,
2657 __isl_take isl_pw_aff *pwaff2)
2659 return isl_pw_aff_union_add_(pwaff1, pwaff2);
2662 static __isl_give isl_pw_aff *pw_aff_mul(__isl_take isl_pw_aff *pwaff1,
2663 __isl_take isl_pw_aff *pwaff2)
2665 return isl_pw_aff_on_shared_domain(pwaff1, pwaff2, &isl_aff_mul);
2668 __isl_give isl_pw_aff *isl_pw_aff_mul(__isl_take isl_pw_aff *pwaff1,
2669 __isl_take isl_pw_aff *pwaff2)
2671 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_mul);
2674 static __isl_give isl_pw_aff *pw_aff_div(__isl_take isl_pw_aff *pa1,
2675 __isl_take isl_pw_aff *pa2)
2677 return isl_pw_aff_on_shared_domain(pa1, pa2, &isl_aff_div);
2680 /* Divide "pa1" by "pa2", assuming "pa2" is a piecewise constant.
2682 __isl_give isl_pw_aff *isl_pw_aff_div(__isl_take isl_pw_aff *pa1,
2683 __isl_take isl_pw_aff *pa2)
2687 is_cst = isl_pw_aff_is_cst(pa2);
2691 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
2692 "second argument should be a piecewise constant",
2694 return isl_pw_aff_align_params_pw_pw_and(pa1, pa2, &pw_aff_div);
2696 isl_pw_aff_free(pa1);
2697 isl_pw_aff_free(pa2);
2701 /* Compute the quotient of the integer division of "pa1" by "pa2"
2702 * with rounding towards zero.
2703 * "pa2" is assumed to be a piecewise constant.
2705 * In particular, return
2707 * pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2)
2710 __isl_give isl_pw_aff *isl_pw_aff_tdiv_q(__isl_take isl_pw_aff *pa1,
2711 __isl_take isl_pw_aff *pa2)
2717 is_cst = isl_pw_aff_is_cst(pa2);
2721 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
2722 "second argument should be a piecewise constant",
2725 pa1 = isl_pw_aff_div(pa1, pa2);
2727 cond = isl_pw_aff_nonneg_set(isl_pw_aff_copy(pa1));
2728 f = isl_pw_aff_floor(isl_pw_aff_copy(pa1));
2729 c = isl_pw_aff_ceil(pa1);
2730 return isl_pw_aff_cond(isl_set_indicator_function(cond), f, c);
2732 isl_pw_aff_free(pa1);
2733 isl_pw_aff_free(pa2);
2737 /* Compute the remainder of the integer division of "pa1" by "pa2"
2738 * with rounding towards zero.
2739 * "pa2" is assumed to be a piecewise constant.
2741 * In particular, return
2743 * pa1 - pa2 * (pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2))
2746 __isl_give isl_pw_aff *isl_pw_aff_tdiv_r(__isl_take isl_pw_aff *pa1,
2747 __isl_take isl_pw_aff *pa2)
2752 is_cst = isl_pw_aff_is_cst(pa2);
2756 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
2757 "second argument should be a piecewise constant",
2759 res = isl_pw_aff_tdiv_q(isl_pw_aff_copy(pa1), isl_pw_aff_copy(pa2));
2760 res = isl_pw_aff_mul(pa2, res);
2761 res = isl_pw_aff_sub(pa1, res);
2764 isl_pw_aff_free(pa1);
2765 isl_pw_aff_free(pa2);
2769 static __isl_give isl_pw_aff *pw_aff_min(__isl_take isl_pw_aff *pwaff1,
2770 __isl_take isl_pw_aff *pwaff2)
2775 dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1)),
2776 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2)));
2777 le = isl_pw_aff_le_set(isl_pw_aff_copy(pwaff1),
2778 isl_pw_aff_copy(pwaff2));
2779 dom = isl_set_subtract(dom, isl_set_copy(le));
2780 return isl_pw_aff_select(le, pwaff1, dom, pwaff2);
2783 __isl_give isl_pw_aff *isl_pw_aff_min(__isl_take isl_pw_aff *pwaff1,
2784 __isl_take isl_pw_aff *pwaff2)
2786 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_min);
2789 static __isl_give isl_pw_aff *pw_aff_max(__isl_take isl_pw_aff *pwaff1,
2790 __isl_take isl_pw_aff *pwaff2)
2795 dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1)),
2796 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2)));
2797 ge = isl_pw_aff_ge_set(isl_pw_aff_copy(pwaff1),
2798 isl_pw_aff_copy(pwaff2));
2799 dom = isl_set_subtract(dom, isl_set_copy(ge));
2800 return isl_pw_aff_select(ge, pwaff1, dom, pwaff2);
2803 __isl_give isl_pw_aff *isl_pw_aff_max(__isl_take isl_pw_aff *pwaff1,
2804 __isl_take isl_pw_aff *pwaff2)
2806 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_max);
2809 static __isl_give isl_pw_aff *pw_aff_list_reduce(
2810 __isl_take isl_pw_aff_list *list,
2811 __isl_give isl_pw_aff *(*fn)(__isl_take isl_pw_aff *pwaff1,
2812 __isl_take isl_pw_aff *pwaff2))
2821 ctx = isl_pw_aff_list_get_ctx(list);
2823 isl_die(ctx, isl_error_invalid,
2824 "list should contain at least one element",
2825 return isl_pw_aff_list_free(list));
2827 res = isl_pw_aff_copy(list->p[0]);
2828 for (i = 1; i < list->n; ++i)
2829 res = fn(res, isl_pw_aff_copy(list->p[i]));
2831 isl_pw_aff_list_free(list);
2835 /* Return an isl_pw_aff that maps each element in the intersection of the
2836 * domains of the elements of list to the minimal corresponding affine
2839 __isl_give isl_pw_aff *isl_pw_aff_list_min(__isl_take isl_pw_aff_list *list)
2841 return pw_aff_list_reduce(list, &isl_pw_aff_min);
2844 /* Return an isl_pw_aff that maps each element in the intersection of the
2845 * domains of the elements of list to the maximal corresponding affine
2848 __isl_give isl_pw_aff *isl_pw_aff_list_max(__isl_take isl_pw_aff_list *list)
2850 return pw_aff_list_reduce(list, &isl_pw_aff_max);
2853 /* Mark the domains of "pwaff" as rational.
2855 __isl_give isl_pw_aff *isl_pw_aff_set_rational(__isl_take isl_pw_aff *pwaff)
2859 pwaff = isl_pw_aff_cow(pwaff);
2865 for (i = 0; i < pwaff->n; ++i) {
2866 pwaff->p[i].set = isl_set_set_rational(pwaff->p[i].set);
2867 if (!pwaff->p[i].set)
2868 return isl_pw_aff_free(pwaff);
2874 /* Mark the domains of the elements of "list" as rational.
2876 __isl_give isl_pw_aff_list *isl_pw_aff_list_set_rational(
2877 __isl_take isl_pw_aff_list *list)
2887 for (i = 0; i < n; ++i) {
2890 pa = isl_pw_aff_list_get_pw_aff(list, i);
2891 pa = isl_pw_aff_set_rational(pa);
2892 list = isl_pw_aff_list_set_pw_aff(list, i, pa);
2898 /* Check that the domain space of "aff" matches "space".
2900 * Return 0 on success and -1 on error.
2902 int isl_aff_check_match_domain_space(__isl_keep isl_aff *aff,
2903 __isl_keep isl_space *space)
2905 isl_space *aff_space;
2911 aff_space = isl_aff_get_domain_space(aff);
2913 match = isl_space_match(space, isl_dim_param, aff_space, isl_dim_param);
2917 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2918 "parameters don't match", goto error);
2919 match = isl_space_tuple_match(space, isl_dim_in,
2920 aff_space, isl_dim_set);
2924 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2925 "domains don't match", goto error);
2926 isl_space_free(aff_space);
2929 isl_space_free(aff_space);
2936 #include <isl_multi_templ.c>
2938 /* Create an isl_pw_multi_aff with the given isl_multi_aff on a universe
2941 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_multi_aff(
2942 __isl_take isl_multi_aff *ma)
2944 isl_set *dom = isl_set_universe(isl_multi_aff_get_domain_space(ma));
2945 return isl_pw_multi_aff_alloc(dom, ma);
2948 /* Create a piecewise multi-affine expression in the given space that maps each
2949 * input dimension to the corresponding output dimension.
2951 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_identity(
2952 __isl_take isl_space *space)
2954 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_identity(space));
2957 __isl_give isl_multi_aff *isl_multi_aff_add(__isl_take isl_multi_aff *maff1,
2958 __isl_take isl_multi_aff *maff2)
2960 return isl_multi_aff_bin_op(maff1, maff2, &isl_aff_add);
2963 /* Subtract "ma2" from "ma1" and return the result.
2965 __isl_give isl_multi_aff *isl_multi_aff_sub(__isl_take isl_multi_aff *ma1,
2966 __isl_take isl_multi_aff *ma2)
2968 return isl_multi_aff_bin_op(ma1, ma2, &isl_aff_sub);
2971 /* Given two multi-affine expressions A -> B and C -> D,
2972 * construct a multi-affine expression [A -> C] -> [B -> D].
2974 __isl_give isl_multi_aff *isl_multi_aff_product(
2975 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2)
2981 int in1, in2, out1, out2;
2983 in1 = isl_multi_aff_dim(ma1, isl_dim_in);
2984 in2 = isl_multi_aff_dim(ma2, isl_dim_in);
2985 out1 = isl_multi_aff_dim(ma1, isl_dim_out);
2986 out2 = isl_multi_aff_dim(ma2, isl_dim_out);
2987 space = isl_space_product(isl_multi_aff_get_space(ma1),
2988 isl_multi_aff_get_space(ma2));
2989 res = isl_multi_aff_alloc(isl_space_copy(space));
2990 space = isl_space_domain(space);
2992 for (i = 0; i < out1; ++i) {
2993 aff = isl_multi_aff_get_aff(ma1, i);
2994 aff = isl_aff_insert_dims(aff, isl_dim_in, in1, in2);
2995 aff = isl_aff_reset_domain_space(aff, isl_space_copy(space));
2996 res = isl_multi_aff_set_aff(res, i, aff);
2999 for (i = 0; i < out2; ++i) {
3000 aff = isl_multi_aff_get_aff(ma2, i);
3001 aff = isl_aff_insert_dims(aff, isl_dim_in, 0, in1);
3002 aff = isl_aff_reset_domain_space(aff, isl_space_copy(space));
3003 res = isl_multi_aff_set_aff(res, out1 + i, aff);
3006 isl_space_free(space);
3007 isl_multi_aff_free(ma1);
3008 isl_multi_aff_free(ma2);
3012 /* Exploit the equalities in "eq" to simplify the affine expressions.
3014 static __isl_give isl_multi_aff *isl_multi_aff_substitute_equalities(
3015 __isl_take isl_multi_aff *maff, __isl_take isl_basic_set *eq)
3019 maff = isl_multi_aff_cow(maff);
3023 for (i = 0; i < maff->n; ++i) {
3024 maff->p[i] = isl_aff_substitute_equalities(maff->p[i],
3025 isl_basic_set_copy(eq));
3030 isl_basic_set_free(eq);
3033 isl_basic_set_free(eq);
3034 isl_multi_aff_free(maff);
3038 __isl_give isl_multi_aff *isl_multi_aff_scale(__isl_take isl_multi_aff *maff,
3043 maff = isl_multi_aff_cow(maff);
3047 for (i = 0; i < maff->n; ++i) {
3048 maff->p[i] = isl_aff_scale(maff->p[i], f);
3050 return isl_multi_aff_free(maff);
3056 __isl_give isl_multi_aff *isl_multi_aff_add_on_domain(__isl_keep isl_set *dom,
3057 __isl_take isl_multi_aff *maff1, __isl_take isl_multi_aff *maff2)
3059 maff1 = isl_multi_aff_add(maff1, maff2);
3060 maff1 = isl_multi_aff_gist(maff1, isl_set_copy(dom));
3064 int isl_multi_aff_is_empty(__isl_keep isl_multi_aff *maff)
3072 int isl_multi_aff_plain_is_equal(__isl_keep isl_multi_aff *maff1,
3073 __isl_keep isl_multi_aff *maff2)
3078 if (!maff1 || !maff2)
3080 if (maff1->n != maff2->n)
3082 equal = isl_space_is_equal(maff1->space, maff2->space);
3083 if (equal < 0 || !equal)
3086 for (i = 0; i < maff1->n; ++i) {
3087 equal = isl_aff_plain_is_equal(maff1->p[i], maff2->p[i]);
3088 if (equal < 0 || !equal)
3095 /* Return the set of domain elements where "ma1" is lexicographically
3096 * smaller than or equal to "ma2".
3098 __isl_give isl_set *isl_multi_aff_lex_le_set(__isl_take isl_multi_aff *ma1,
3099 __isl_take isl_multi_aff *ma2)
3101 return isl_multi_aff_lex_ge_set(ma2, ma1);
3104 /* Return the set of domain elements where "ma1" is lexicographically
3105 * greater than or equal to "ma2".
3107 __isl_give isl_set *isl_multi_aff_lex_ge_set(__isl_take isl_multi_aff *ma1,
3108 __isl_take isl_multi_aff *ma2)
3111 isl_map *map1, *map2;
3114 map1 = isl_map_from_multi_aff(ma1);
3115 map2 = isl_map_from_multi_aff(ma2);
3116 map = isl_map_range_product(map1, map2);
3117 space = isl_space_range(isl_map_get_space(map));
3118 space = isl_space_domain(isl_space_unwrap(space));
3119 ge = isl_map_lex_ge(space);
3120 map = isl_map_intersect_range(map, isl_map_wrap(ge));
3122 return isl_map_domain(map);
3126 #define PW isl_pw_multi_aff
3128 #define EL isl_multi_aff
3130 #define EL_IS_ZERO is_empty
3134 #define IS_ZERO is_empty
3137 #undef DEFAULT_IS_ZERO
3138 #define DEFAULT_IS_ZERO 0
3143 #define NO_INVOLVES_DIMS
3144 #define NO_MOVE_DIMS
3145 #define NO_INSERT_DIMS
3149 #include <isl_pw_templ.c>
3152 #define UNION isl_union_pw_multi_aff
3154 #define PART isl_pw_multi_aff
3156 #define PARTS pw_multi_aff
3157 #define ALIGN_DOMAIN
3161 #include <isl_union_templ.c>
3163 /* Given a function "cmp" that returns the set of elements where
3164 * "ma1" is "better" than "ma2", return the intersection of this
3165 * set with "dom1" and "dom2".
3167 static __isl_give isl_set *shared_and_better(__isl_keep isl_set *dom1,
3168 __isl_keep isl_set *dom2, __isl_keep isl_multi_aff *ma1,
3169 __isl_keep isl_multi_aff *ma2,
3170 __isl_give isl_set *(*cmp)(__isl_take isl_multi_aff *ma1,
3171 __isl_take isl_multi_aff *ma2))
3177 common = isl_set_intersect(isl_set_copy(dom1), isl_set_copy(dom2));
3178 is_empty = isl_set_plain_is_empty(common);
3179 if (is_empty >= 0 && is_empty)
3182 return isl_set_free(common);
3183 better = cmp(isl_multi_aff_copy(ma1), isl_multi_aff_copy(ma2));
3184 better = isl_set_intersect(common, better);
3189 /* Given a function "cmp" that returns the set of elements where
3190 * "ma1" is "better" than "ma2", return a piecewise multi affine
3191 * expression defined on the union of the definition domains
3192 * of "pma1" and "pma2" that maps to the "best" of "pma1" and
3193 * "pma2" on each cell. If only one of the two input functions
3194 * is defined on a given cell, then it is considered the best.
3196 static __isl_give isl_pw_multi_aff *pw_multi_aff_union_opt(
3197 __isl_take isl_pw_multi_aff *pma1,
3198 __isl_take isl_pw_multi_aff *pma2,
3199 __isl_give isl_set *(*cmp)(__isl_take isl_multi_aff *ma1,
3200 __isl_take isl_multi_aff *ma2))
3203 isl_pw_multi_aff *res = NULL;
3205 isl_set *set = NULL;
3210 ctx = isl_space_get_ctx(pma1->dim);
3211 if (!isl_space_is_equal(pma1->dim, pma2->dim))
3212 isl_die(ctx, isl_error_invalid,
3213 "arguments should live in the same space", goto error);
3215 if (isl_pw_multi_aff_is_empty(pma1)) {
3216 isl_pw_multi_aff_free(pma1);
3220 if (isl_pw_multi_aff_is_empty(pma2)) {
3221 isl_pw_multi_aff_free(pma2);
3225 n = 2 * (pma1->n + 1) * (pma2->n + 1);
3226 res = isl_pw_multi_aff_alloc_size(isl_space_copy(pma1->dim), n);
3228 for (i = 0; i < pma1->n; ++i) {
3229 set = isl_set_copy(pma1->p[i].set);
3230 for (j = 0; j < pma2->n; ++j) {
3234 better = shared_and_better(pma2->p[j].set,
3235 pma1->p[i].set, pma2->p[j].maff,
3236 pma1->p[i].maff, cmp);
3237 is_empty = isl_set_plain_is_empty(better);
3238 if (is_empty < 0 || is_empty) {
3239 isl_set_free(better);
3244 set = isl_set_subtract(set, isl_set_copy(better));
3246 res = isl_pw_multi_aff_add_piece(res, better,
3247 isl_multi_aff_copy(pma2->p[j].maff));
3249 res = isl_pw_multi_aff_add_piece(res, set,
3250 isl_multi_aff_copy(pma1->p[i].maff));
3253 for (j = 0; j < pma2->n; ++j) {
3254 set = isl_set_copy(pma2->p[j].set);
3255 for (i = 0; i < pma1->n; ++i)
3256 set = isl_set_subtract(set,
3257 isl_set_copy(pma1->p[i].set));
3258 res = isl_pw_multi_aff_add_piece(res, set,
3259 isl_multi_aff_copy(pma2->p[j].maff));
3262 isl_pw_multi_aff_free(pma1);
3263 isl_pw_multi_aff_free(pma2);
3267 isl_pw_multi_aff_free(pma1);
3268 isl_pw_multi_aff_free(pma2);
3270 return isl_pw_multi_aff_free(res);
3273 static __isl_give isl_pw_multi_aff *pw_multi_aff_union_lexmax(
3274 __isl_take isl_pw_multi_aff *pma1,
3275 __isl_take isl_pw_multi_aff *pma2)
3277 return pw_multi_aff_union_opt(pma1, pma2, &isl_multi_aff_lex_ge_set);
3280 /* Given two piecewise multi affine expressions, return a piecewise
3281 * multi-affine expression defined on the union of the definition domains
3282 * of the inputs that is equal to the lexicographic maximum of the two
3283 * inputs on each cell. If only one of the two inputs is defined on
3284 * a given cell, then it is considered to be the maximum.
3286 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_lexmax(
3287 __isl_take isl_pw_multi_aff *pma1,
3288 __isl_take isl_pw_multi_aff *pma2)
3290 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
3291 &pw_multi_aff_union_lexmax);
3294 static __isl_give isl_pw_multi_aff *pw_multi_aff_union_lexmin(
3295 __isl_take isl_pw_multi_aff *pma1,
3296 __isl_take isl_pw_multi_aff *pma2)
3298 return pw_multi_aff_union_opt(pma1, pma2, &isl_multi_aff_lex_le_set);
3301 /* Given two piecewise multi affine expressions, return a piecewise
3302 * multi-affine expression defined on the union of the definition domains
3303 * of the inputs that is equal to the lexicographic minimum of the two
3304 * inputs on each cell. If only one of the two inputs is defined on
3305 * a given cell, then it is considered to be the minimum.
3307 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_lexmin(
3308 __isl_take isl_pw_multi_aff *pma1,
3309 __isl_take isl_pw_multi_aff *pma2)
3311 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
3312 &pw_multi_aff_union_lexmin);
3315 static __isl_give isl_pw_multi_aff *pw_multi_aff_add(
3316 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
3318 return isl_pw_multi_aff_on_shared_domain(pma1, pma2,
3319 &isl_multi_aff_add);
3322 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_add(
3323 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
3325 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
3329 static __isl_give isl_pw_multi_aff *pw_multi_aff_sub(
3330 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
3332 return isl_pw_multi_aff_on_shared_domain(pma1, pma2,
3333 &isl_multi_aff_sub);
3336 /* Subtract "pma2" from "pma1" and return the result.
3338 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_sub(
3339 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
3341 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
3345 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_add(
3346 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
3348 return isl_pw_multi_aff_union_add_(pma1, pma2);
3351 /* Given two piecewise multi-affine expressions A -> B and C -> D,
3352 * construct a piecewise multi-affine expression [A -> C] -> [B -> D].
3354 static __isl_give isl_pw_multi_aff *pw_multi_aff_product(
3355 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
3359 isl_pw_multi_aff *res;
3364 n = pma1->n * pma2->n;
3365 space = isl_space_product(isl_space_copy(pma1->dim),
3366 isl_space_copy(pma2->dim));
3367 res = isl_pw_multi_aff_alloc_size(space, n);
3369 for (i = 0; i < pma1->n; ++i) {
3370 for (j = 0; j < pma2->n; ++j) {
3374 domain = isl_set_product(isl_set_copy(pma1->p[i].set),
3375 isl_set_copy(pma2->p[j].set));
3376 ma = isl_multi_aff_product(
3377 isl_multi_aff_copy(pma1->p[i].maff),
3378 isl_multi_aff_copy(pma2->p[i].maff));
3379 res = isl_pw_multi_aff_add_piece(res, domain, ma);
3383 isl_pw_multi_aff_free(pma1);
3384 isl_pw_multi_aff_free(pma2);
3387 isl_pw_multi_aff_free(pma1);
3388 isl_pw_multi_aff_free(pma2);
3392 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_product(
3393 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
3395 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
3396 &pw_multi_aff_product);
3399 /* Construct a map mapping the domain of the piecewise multi-affine expression
3400 * to its range, with each dimension in the range equated to the
3401 * corresponding affine expression on its cell.
3403 __isl_give isl_map *isl_map_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma)
3411 map = isl_map_empty(isl_pw_multi_aff_get_space(pma));
3413 for (i = 0; i < pma->n; ++i) {
3414 isl_multi_aff *maff;
3415 isl_basic_map *bmap;
3418 maff = isl_multi_aff_copy(pma->p[i].maff);
3419 bmap = isl_basic_map_from_multi_aff(maff);
3420 map_i = isl_map_from_basic_map(bmap);
3421 map_i = isl_map_intersect_domain(map_i,
3422 isl_set_copy(pma->p[i].set));
3423 map = isl_map_union_disjoint(map, map_i);
3426 isl_pw_multi_aff_free(pma);
3430 __isl_give isl_set *isl_set_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma)
3435 if (!isl_space_is_set(pma->dim))
3436 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
3437 "isl_pw_multi_aff cannot be converted into an isl_set",
3438 return isl_pw_multi_aff_free(pma));
3440 return isl_map_from_pw_multi_aff(pma);
3443 /* Given a basic map with a single output dimension that is defined
3444 * in terms of the parameters and input dimensions using an equality,
3445 * extract an isl_aff that expresses the output dimension in terms
3446 * of the parameters and input dimensions.
3448 * Since some applications expect the result of isl_pw_multi_aff_from_map
3449 * to only contain integer affine expressions, we compute the floor
3450 * of the expression before returning.
3452 * This function shares some similarities with
3453 * isl_basic_map_has_defining_equality and isl_constraint_get_bound.
3455 static __isl_give isl_aff *extract_isl_aff_from_basic_map(
3456 __isl_take isl_basic_map *bmap)
3461 isl_local_space *ls;
3466 if (isl_basic_map_dim(bmap, isl_dim_out) != 1)
3467 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
3468 "basic map should have a single output dimension",
3470 offset = isl_basic_map_offset(bmap, isl_dim_out);
3471 total = isl_basic_map_total_dim(bmap);
3472 for (i = 0; i < bmap->n_eq; ++i) {
3473 if (isl_int_is_zero(bmap->eq[i][offset]))
3475 if (isl_seq_first_non_zero(bmap->eq[i] + offset + 1,
3476 1 + total - (offset + 1)) != -1)
3480 if (i >= bmap->n_eq)
3481 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
3482 "unable to find suitable equality", goto error);
3483 ls = isl_basic_map_get_local_space(bmap);
3484 aff = isl_aff_alloc(isl_local_space_domain(ls));
3487 if (isl_int_is_neg(bmap->eq[i][offset]))
3488 isl_seq_cpy(aff->v->el + 1, bmap->eq[i], offset);
3490 isl_seq_neg(aff->v->el + 1, bmap->eq[i], offset);
3491 isl_seq_clr(aff->v->el + 1 + offset, aff->v->size - (1 + offset));
3492 isl_int_abs(aff->v->el[0], bmap->eq[i][offset]);
3493 isl_basic_map_free(bmap);
3495 aff = isl_aff_remove_unused_divs(aff);
3496 aff = isl_aff_floor(aff);
3499 isl_basic_map_free(bmap);
3503 /* Given a basic map where each output dimension is defined
3504 * in terms of the parameters and input dimensions using an equality,
3505 * extract an isl_multi_aff that expresses the output dimensions in terms
3506 * of the parameters and input dimensions.
3508 static __isl_give isl_multi_aff *extract_isl_multi_aff_from_basic_map(
3509 __isl_take isl_basic_map *bmap)
3518 ma = isl_multi_aff_alloc(isl_basic_map_get_space(bmap));
3519 n_out = isl_basic_map_dim(bmap, isl_dim_out);
3521 for (i = 0; i < n_out; ++i) {
3522 isl_basic_map *bmap_i;
3525 bmap_i = isl_basic_map_copy(bmap);
3526 bmap_i = isl_basic_map_project_out(bmap_i, isl_dim_out,
3527 i + 1, n_out - (1 + i));
3528 bmap_i = isl_basic_map_project_out(bmap_i, isl_dim_out, 0, i);
3529 aff = extract_isl_aff_from_basic_map(bmap_i);
3530 ma = isl_multi_aff_set_aff(ma, i, aff);
3533 isl_basic_map_free(bmap);
3538 /* Create an isl_pw_multi_aff that is equivalent to
3539 * isl_map_intersect_domain(isl_map_from_basic_map(bmap), domain).
3540 * The given basic map is such that each output dimension is defined
3541 * in terms of the parameters and input dimensions using an equality.
3543 static __isl_give isl_pw_multi_aff *plain_pw_multi_aff_from_map(
3544 __isl_take isl_set *domain, __isl_take isl_basic_map *bmap)
3548 ma = extract_isl_multi_aff_from_basic_map(bmap);
3549 return isl_pw_multi_aff_alloc(domain, ma);
3552 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
3553 * This obviously only works if the input "map" is single-valued.
3554 * If so, we compute the lexicographic minimum of the image in the form
3555 * of an isl_pw_multi_aff. Since the image is unique, it is equal
3556 * to its lexicographic minimum.
3557 * If the input is not single-valued, we produce an error.
3559 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_base(
3560 __isl_take isl_map *map)
3564 isl_pw_multi_aff *pma;
3566 sv = isl_map_is_single_valued(map);
3570 isl_die(isl_map_get_ctx(map), isl_error_invalid,
3571 "map is not single-valued", goto error);
3572 map = isl_map_make_disjoint(map);
3576 pma = isl_pw_multi_aff_empty(isl_map_get_space(map));
3578 for (i = 0; i < map->n; ++i) {
3579 isl_pw_multi_aff *pma_i;
3580 isl_basic_map *bmap;
3581 bmap = isl_basic_map_copy(map->p[i]);
3582 pma_i = isl_basic_map_lexmin_pw_multi_aff(bmap);
3583 pma = isl_pw_multi_aff_add_disjoint(pma, pma_i);
3593 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
3594 * taking into account that the output dimension at position "d"
3595 * can be represented as
3597 * x = floor((e(...) + c1) / m)
3599 * given that constraint "i" is of the form
3601 * e(...) + c1 - m x >= 0
3604 * Let "map" be of the form
3608 * We construct a mapping
3610 * A -> [A -> x = floor(...)]
3612 * apply that to the map, obtaining
3614 * [A -> x = floor(...)] -> B
3616 * and equate dimension "d" to x.
3617 * We then compute a isl_pw_multi_aff representation of the resulting map
3618 * and plug in the mapping above.
3620 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_div(
3621 __isl_take isl_map *map, __isl_take isl_basic_map *hull, int d, int i)
3625 isl_local_space *ls;
3633 isl_pw_multi_aff *pma;
3636 is_set = isl_map_is_set(map);
3638 offset = isl_basic_map_offset(hull, isl_dim_out);
3639 ctx = isl_map_get_ctx(map);
3640 space = isl_space_domain(isl_map_get_space(map));
3641 n_in = isl_space_dim(space, isl_dim_set);
3642 n = isl_space_dim(space, isl_dim_all);
3644 v = isl_vec_alloc(ctx, 1 + 1 + n);
3646 isl_int_neg(v->el[0], hull->ineq[i][offset + d]);
3647 isl_seq_cpy(v->el + 1, hull->ineq[i], 1 + n);
3649 isl_basic_map_free(hull);
3651 ls = isl_local_space_from_space(isl_space_copy(space));
3652 aff = isl_aff_alloc_vec(ls, v);
3653 aff = isl_aff_floor(aff);
3655 isl_space_free(space);
3656 ma = isl_multi_aff_from_aff(aff);
3658 ma = isl_multi_aff_identity(isl_space_map_from_set(space));
3659 ma = isl_multi_aff_range_product(ma,
3660 isl_multi_aff_from_aff(aff));
3663 insert = isl_map_from_multi_aff(isl_multi_aff_copy(ma));
3664 map = isl_map_apply_domain(map, insert);
3665 map = isl_map_equate(map, isl_dim_in, n_in, isl_dim_out, d);
3666 pma = isl_pw_multi_aff_from_map(map);
3667 pma = isl_pw_multi_aff_pullback_multi_aff(pma, ma);
3672 /* Is constraint "c" of the form
3674 * e(...) + c1 - m x >= 0
3678 * -e(...) + c2 + m x >= 0
3680 * where m > 1 and e only depends on parameters and input dimemnsions?
3682 * "offset" is the offset of the output dimensions
3683 * "pos" is the position of output dimension x.
3685 static int is_potential_div_constraint(isl_int *c, int offset, int d, int total)
3687 if (isl_int_is_zero(c[offset + d]))
3689 if (isl_int_is_one(c[offset + d]))
3691 if (isl_int_is_negone(c[offset + d]))
3693 if (isl_seq_first_non_zero(c + offset, d) != -1)
3695 if (isl_seq_first_non_zero(c + offset + d + 1,
3696 total - (offset + d + 1)) != -1)
3701 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
3703 * As a special case, we first check if there is any pair of constraints,
3704 * shared by all the basic maps in "map" that force a given dimension
3705 * to be equal to the floor of some affine combination of the input dimensions.
3707 * In particular, if we can find two constraints
3709 * e(...) + c1 - m x >= 0 i.e., m x <= e(...) + c1
3713 * -e(...) + c2 + m x >= 0 i.e., m x >= e(...) - c2
3715 * where m > 1 and e only depends on parameters and input dimemnsions,
3718 * c1 + c2 < m i.e., -c2 >= c1 - (m - 1)
3720 * then we know that we can take
3722 * x = floor((e(...) + c1) / m)
3724 * without having to perform any computation.
3726 * Note that we know that
3730 * If c1 + c2 were 0, then we would have detected an equality during
3731 * simplification. If c1 + c2 were negative, then we would have detected
3734 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_check_div(
3735 __isl_take isl_map *map)
3741 isl_basic_map *hull;
3743 hull = isl_map_unshifted_simple_hull(isl_map_copy(map));
3748 dim = isl_map_dim(map, isl_dim_out);
3749 offset = isl_basic_map_offset(hull, isl_dim_out);
3750 total = 1 + isl_basic_map_total_dim(hull);
3752 for (d = 0; d < dim; ++d) {
3753 for (i = 0; i < n; ++i) {
3754 if (!is_potential_div_constraint(hull->ineq[i],
3757 for (j = i + 1; j < n; ++j) {
3758 if (!isl_seq_is_neg(hull->ineq[i] + 1,
3759 hull->ineq[j] + 1, total - 1))
3761 isl_int_add(sum, hull->ineq[i][0],
3763 if (isl_int_abs_lt(sum,
3764 hull->ineq[i][offset + d]))
3771 if (isl_int_is_pos(hull->ineq[j][offset + d]))
3773 return pw_multi_aff_from_map_div(map, hull, d, j);
3777 isl_basic_map_free(hull);
3778 return pw_multi_aff_from_map_base(map);
3781 isl_basic_map_free(hull);
3785 /* Given an affine expression
3787 * [A -> B] -> f(A,B)
3789 * construct an isl_multi_aff
3793 * such that dimension "d" in B' is set to "aff" and the remaining
3794 * dimensions are set equal to the corresponding dimensions in B.
3795 * "n_in" is the dimension of the space A.
3796 * "n_out" is the dimension of the space B.
3798 * If "is_set" is set, then the affine expression is of the form
3802 * and we construct an isl_multi_aff
3806 static __isl_give isl_multi_aff *range_map(__isl_take isl_aff *aff, int d,
3807 unsigned n_in, unsigned n_out, int is_set)
3811 isl_space *space, *space2;
3812 isl_local_space *ls;
3814 space = isl_aff_get_domain_space(aff);
3815 ls = isl_local_space_from_space(isl_space_copy(space));
3816 space2 = isl_space_copy(space);
3818 space2 = isl_space_range(isl_space_unwrap(space2));
3819 space = isl_space_map_from_domain_and_range(space, space2);
3820 ma = isl_multi_aff_alloc(space);
3821 ma = isl_multi_aff_set_aff(ma, d, aff);
3823 for (i = 0; i < n_out; ++i) {
3826 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
3827 isl_dim_set, n_in + i);
3828 ma = isl_multi_aff_set_aff(ma, i, aff);
3831 isl_local_space_free(ls);
3836 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
3837 * taking into account that the dimension at position "d" can be written as
3839 * x = m a + f(..) (1)
3841 * where m is equal to "gcd".
3842 * "i" is the index of the equality in "hull" that defines f(..).
3843 * In particular, the equality is of the form
3845 * f(..) - x + m g(existentials) = 0
3849 * -f(..) + x + m g(existentials) = 0
3851 * We basically plug (1) into "map", resulting in a map with "a"
3852 * in the range instead of "x". The corresponding isl_pw_multi_aff
3853 * defining "a" is then plugged back into (1) to obtain a definition fro "x".
3855 * Specifically, given the input map
3859 * We first wrap it into a set
3863 * and define (1) on top of the corresponding space, resulting in "aff".
3864 * We use this to create an isl_multi_aff that maps the output position "d"
3865 * from "a" to "x", leaving all other (intput and output) dimensions unchanged.
3866 * We plug this into the wrapped map, unwrap the result and compute the
3867 * corresponding isl_pw_multi_aff.
3868 * The result is an expression
3876 * so that we can plug that into "aff", after extending the latter to
3882 * If "map" is actually a set, then there is no "A" space, meaning
3883 * that we do not need to perform any wrapping, and that the result
3884 * of the recursive call is of the form
3888 * which is plugged into a mapping of the form
3892 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_stride(
3893 __isl_take isl_map *map, __isl_take isl_basic_map *hull, int d, int i,
3898 isl_local_space *ls;
3901 isl_pw_multi_aff *pma, *id;
3907 is_set = isl_map_is_set(map);
3909 n_in = isl_basic_map_dim(hull, isl_dim_in);
3910 n_out = isl_basic_map_dim(hull, isl_dim_out);
3911 o_out = isl_basic_map_offset(hull, isl_dim_out);
3916 set = isl_map_wrap(map);
3917 space = isl_space_map_from_set(isl_set_get_space(set));
3918 ma = isl_multi_aff_identity(space);
3919 ls = isl_local_space_from_space(isl_set_get_space(set));
3920 aff = isl_aff_alloc(ls);
3922 isl_int_set_si(aff->v->el[0], 1);
3923 if (isl_int_is_one(hull->eq[i][o_out + d]))
3924 isl_seq_neg(aff->v->el + 1, hull->eq[i],
3927 isl_seq_cpy(aff->v->el + 1, hull->eq[i],
3929 isl_int_set(aff->v->el[1 + o_out + d], gcd);
3931 ma = isl_multi_aff_set_aff(ma, n_in + d, isl_aff_copy(aff));
3932 set = isl_set_preimage_multi_aff(set, ma);
3934 ma = range_map(aff, d, n_in, n_out, is_set);
3939 map = isl_set_unwrap(set);
3940 pma = isl_pw_multi_aff_from_map(set);
3943 space = isl_pw_multi_aff_get_domain_space(pma);
3944 space = isl_space_map_from_set(space);
3945 id = isl_pw_multi_aff_identity(space);
3946 pma = isl_pw_multi_aff_range_product(id, pma);
3948 id = isl_pw_multi_aff_from_multi_aff(ma);
3949 pma = isl_pw_multi_aff_pullback_pw_multi_aff(id, pma);
3951 isl_basic_map_free(hull);
3955 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
3957 * As a special case, we first check if all output dimensions are uniquely
3958 * defined in terms of the parameters and input dimensions over the entire
3959 * domain. If so, we extract the desired isl_pw_multi_aff directly
3960 * from the affine hull of "map" and its domain.
3962 * Otherwise, we check if any of the output dimensions is "strided".
3963 * That is, we check if can be written as
3967 * with m greater than 1, a some combination of existentiall quantified
3968 * variables and f and expression in the parameters and input dimensions.
3969 * If so, we remove the stride in pw_multi_aff_from_map_stride.
3971 * Otherwise, we continue with pw_multi_aff_from_map_check_div for a further
3974 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_map(__isl_take isl_map *map)
3978 isl_basic_map *hull;
3988 hull = isl_map_affine_hull(isl_map_copy(map));
3989 sv = isl_basic_map_plain_is_single_valued(hull);
3991 return plain_pw_multi_aff_from_map(isl_map_domain(map), hull);
3993 hull = isl_basic_map_free(hull);
3997 n_div = isl_basic_map_dim(hull, isl_dim_div);
3998 o_div = isl_basic_map_offset(hull, isl_dim_div);
4001 isl_basic_map_free(hull);
4002 return pw_multi_aff_from_map_check_div(map);
4007 n_out = isl_basic_map_dim(hull, isl_dim_out);
4008 o_out = isl_basic_map_offset(hull, isl_dim_out);
4010 for (i = 0; i < n_out; ++i) {
4011 for (j = 0; j < hull->n_eq; ++j) {
4012 isl_int *eq = hull->eq[j];
4013 isl_pw_multi_aff *res;
4015 if (!isl_int_is_one(eq[o_out + i]) &&
4016 !isl_int_is_negone(eq[o_out + i]))
4018 if (isl_seq_first_non_zero(eq + o_out, i) != -1)
4020 if (isl_seq_first_non_zero(eq + o_out + i + 1,
4021 n_out - (i + 1)) != -1)
4023 isl_seq_gcd(eq + o_div, n_div, &gcd);
4024 if (isl_int_is_zero(gcd))
4026 if (isl_int_is_one(gcd))
4029 res = pw_multi_aff_from_map_stride(map, hull,
4037 isl_basic_map_free(hull);
4038 return pw_multi_aff_from_map_check_div(map);
4044 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_set(__isl_take isl_set *set)
4046 return isl_pw_multi_aff_from_map(set);
4049 /* Convert "map" into an isl_pw_multi_aff (if possible) and
4052 static int pw_multi_aff_from_map(__isl_take isl_map *map, void *user)
4054 isl_union_pw_multi_aff **upma = user;
4055 isl_pw_multi_aff *pma;
4057 pma = isl_pw_multi_aff_from_map(map);
4058 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
4060 return *upma ? 0 : -1;
4063 /* Try and create an isl_union_pw_multi_aff that is equivalent
4064 * to the given isl_union_map.
4065 * The isl_union_map is required to be single-valued in each space.
4066 * Otherwise, an error is produced.
4068 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_map(
4069 __isl_take isl_union_map *umap)
4072 isl_union_pw_multi_aff *upma;
4074 space = isl_union_map_get_space(umap);
4075 upma = isl_union_pw_multi_aff_empty(space);
4076 if (isl_union_map_foreach_map(umap, &pw_multi_aff_from_map, &upma) < 0)
4077 upma = isl_union_pw_multi_aff_free(upma);
4078 isl_union_map_free(umap);
4083 /* Try and create an isl_union_pw_multi_aff that is equivalent
4084 * to the given isl_union_set.
4085 * The isl_union_set is required to be a singleton in each space.
4086 * Otherwise, an error is produced.
4088 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_set(
4089 __isl_take isl_union_set *uset)
4091 return isl_union_pw_multi_aff_from_union_map(uset);
4094 /* Return the piecewise affine expression "set ? 1 : 0".
4096 __isl_give isl_pw_aff *isl_set_indicator_function(__isl_take isl_set *set)
4099 isl_space *space = isl_set_get_space(set);
4100 isl_local_space *ls = isl_local_space_from_space(space);
4101 isl_aff *zero = isl_aff_zero_on_domain(isl_local_space_copy(ls));
4102 isl_aff *one = isl_aff_zero_on_domain(ls);
4104 one = isl_aff_add_constant_si(one, 1);
4105 pa = isl_pw_aff_alloc(isl_set_copy(set), one);
4106 set = isl_set_complement(set);
4107 pa = isl_pw_aff_add_disjoint(pa, isl_pw_aff_alloc(set, zero));
4112 /* Plug in "subs" for dimension "type", "pos" of "aff".
4114 * Let i be the dimension to replace and let "subs" be of the form
4118 * and "aff" of the form
4124 * (a f + d g')/(m d)
4126 * where g' is the result of plugging in "subs" in each of the integer
4129 __isl_give isl_aff *isl_aff_substitute(__isl_take isl_aff *aff,
4130 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
4135 aff = isl_aff_cow(aff);
4137 return isl_aff_free(aff);
4139 ctx = isl_aff_get_ctx(aff);
4140 if (!isl_space_is_equal(aff->ls->dim, subs->ls->dim))
4141 isl_die(ctx, isl_error_invalid,
4142 "spaces don't match", return isl_aff_free(aff));
4143 if (isl_local_space_dim(subs->ls, isl_dim_div) != 0)
4144 isl_die(ctx, isl_error_unsupported,
4145 "cannot handle divs yet", return isl_aff_free(aff));
4147 aff->ls = isl_local_space_substitute(aff->ls, type, pos, subs);
4149 return isl_aff_free(aff);
4151 aff->v = isl_vec_cow(aff->v);
4153 return isl_aff_free(aff);
4155 pos += isl_local_space_offset(aff->ls, type);
4158 isl_seq_substitute(aff->v->el, pos, subs->v->el,
4159 aff->v->size, subs->v->size, v);
4165 /* Plug in "subs" for dimension "type", "pos" in each of the affine
4166 * expressions in "maff".
4168 __isl_give isl_multi_aff *isl_multi_aff_substitute(
4169 __isl_take isl_multi_aff *maff, enum isl_dim_type type, unsigned pos,
4170 __isl_keep isl_aff *subs)
4174 maff = isl_multi_aff_cow(maff);
4176 return isl_multi_aff_free(maff);
4178 if (type == isl_dim_in)
4181 for (i = 0; i < maff->n; ++i) {
4182 maff->p[i] = isl_aff_substitute(maff->p[i], type, pos, subs);
4184 return isl_multi_aff_free(maff);
4190 /* Plug in "subs" for dimension "type", "pos" of "pma".
4192 * pma is of the form
4196 * while subs is of the form
4198 * v' = B_j(v) -> S_j
4200 * Each pair i,j such that C_ij = A_i \cap B_i is non-empty
4201 * has a contribution in the result, in particular
4203 * C_ij(S_j) -> M_i(S_j)
4205 * Note that plugging in S_j in C_ij may also result in an empty set
4206 * and this contribution should simply be discarded.
4208 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_substitute(
4209 __isl_take isl_pw_multi_aff *pma, enum isl_dim_type type, unsigned pos,
4210 __isl_keep isl_pw_aff *subs)
4213 isl_pw_multi_aff *res;
4216 return isl_pw_multi_aff_free(pma);
4218 n = pma->n * subs->n;
4219 res = isl_pw_multi_aff_alloc_size(isl_space_copy(pma->dim), n);
4221 for (i = 0; i < pma->n; ++i) {
4222 for (j = 0; j < subs->n; ++j) {
4224 isl_multi_aff *res_ij;
4227 common = isl_set_intersect(
4228 isl_set_copy(pma->p[i].set),
4229 isl_set_copy(subs->p[j].set));
4230 common = isl_set_substitute(common,
4231 type, pos, subs->p[j].aff);
4232 empty = isl_set_plain_is_empty(common);
4233 if (empty < 0 || empty) {
4234 isl_set_free(common);
4240 res_ij = isl_multi_aff_substitute(
4241 isl_multi_aff_copy(pma->p[i].maff),
4242 type, pos, subs->p[j].aff);
4244 res = isl_pw_multi_aff_add_piece(res, common, res_ij);
4248 isl_pw_multi_aff_free(pma);
4251 isl_pw_multi_aff_free(pma);
4252 isl_pw_multi_aff_free(res);
4256 /* Compute the preimage of a range of dimensions in the affine expression "src"
4257 * under "ma" and put the result in "dst". The number of dimensions in "src"
4258 * that precede the range is given by "n_before". The number of dimensions
4259 * in the range is given by the number of output dimensions of "ma".
4260 * The number of dimensions that follow the range is given by "n_after".
4261 * If "has_denom" is set (to one),
4262 * then "src" and "dst" have an extra initial denominator.
4263 * "n_div_ma" is the number of existentials in "ma"
4264 * "n_div_bset" is the number of existentials in "src"
4265 * The resulting "dst" (which is assumed to have been allocated by
4266 * the caller) contains coefficients for both sets of existentials,
4267 * first those in "ma" and then those in "src".
4268 * f, c1, c2 and g are temporary objects that have been initialized
4271 * Let src represent the expression
4273 * (a(p) + f_u u + b v + f_w w + c(divs))/d
4275 * and let ma represent the expressions
4277 * v_i = (r_i(p) + s_i(y) + t_i(divs'))/m_i
4279 * We start out with the following expression for dst:
4281 * (a(p) + f_u u + 0 y + f_w w + 0 divs' + c(divs) + f \sum_i b_i v_i)/d
4283 * with the multiplication factor f initially equal to 1
4284 * and f \sum_i b_i v_i kept separately.
4285 * For each x_i that we substitute, we multiply the numerator
4286 * (and denominator) of dst by c_1 = m_i and add the numerator
4287 * of the x_i expression multiplied by c_2 = f b_i,
4288 * after removing the common factors of c_1 and c_2.
4289 * The multiplication factor f also needs to be multiplied by c_1
4290 * for the next x_j, j > i.
4292 void isl_seq_preimage(isl_int *dst, isl_int *src,
4293 __isl_keep isl_multi_aff *ma, int n_before, int n_after,
4294 int n_div_ma, int n_div_bmap,
4295 isl_int f, isl_int c1, isl_int c2, isl_int g, int has_denom)
4298 int n_param, n_in, n_out;
4301 n_param = isl_multi_aff_dim(ma, isl_dim_param);
4302 n_in = isl_multi_aff_dim(ma, isl_dim_in);
4303 n_out = isl_multi_aff_dim(ma, isl_dim_out);
4305 isl_seq_cpy(dst, src, has_denom + 1 + n_param + n_before);
4306 o_dst = o_src = has_denom + 1 + n_param + n_before;
4307 isl_seq_clr(dst + o_dst, n_in);
4310 isl_seq_cpy(dst + o_dst, src + o_src, n_after);
4313 isl_seq_clr(dst + o_dst, n_div_ma);
4315 isl_seq_cpy(dst + o_dst, src + o_src, n_div_bmap);
4317 isl_int_set_si(f, 1);
4319 for (i = 0; i < n_out; ++i) {
4320 int offset = has_denom + 1 + n_param + n_before + i;
4322 if (isl_int_is_zero(src[offset]))
4324 isl_int_set(c1, ma->p[i]->v->el[0]);
4325 isl_int_mul(c2, f, src[offset]);
4326 isl_int_gcd(g, c1, c2);
4327 isl_int_divexact(c1, c1, g);
4328 isl_int_divexact(c2, c2, g);
4330 isl_int_mul(f, f, c1);
4333 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
4334 c2, ma->p[i]->v->el + o_src, 1 + n_param);
4335 o_dst += 1 + n_param;
4336 o_src += 1 + n_param;
4337 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_before);
4339 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
4340 c2, ma->p[i]->v->el + o_src, n_in);
4343 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_after);
4345 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
4346 c2, ma->p[i]->v->el + o_src, n_div_ma);
4349 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_div_bmap);
4351 isl_int_mul(dst[0], dst[0], c1);
4355 /* Compute the pullback of "aff" by the function represented by "ma".
4356 * In other words, plug in "ma" in "aff". The result is an affine expression
4357 * defined over the domain space of "ma".
4359 * If "aff" is represented by
4361 * (a(p) + b x + c(divs))/d
4363 * and ma is represented by
4365 * x = D(p) + F(y) + G(divs')
4367 * then the result is
4369 * (a(p) + b D(p) + b F(y) + b G(divs') + c(divs))/d
4371 * The divs in the local space of the input are similarly adjusted
4372 * through a call to isl_local_space_preimage_multi_aff.
4374 __isl_give isl_aff *isl_aff_pullback_multi_aff(__isl_take isl_aff *aff,
4375 __isl_take isl_multi_aff *ma)
4377 isl_aff *res = NULL;
4378 isl_local_space *ls;
4379 int n_div_aff, n_div_ma;
4380 isl_int f, c1, c2, g;
4382 ma = isl_multi_aff_align_divs(ma);
4386 n_div_aff = isl_aff_dim(aff, isl_dim_div);
4387 n_div_ma = ma->n ? isl_aff_dim(ma->p[0], isl_dim_div) : 0;
4389 ls = isl_aff_get_domain_local_space(aff);
4390 ls = isl_local_space_preimage_multi_aff(ls, isl_multi_aff_copy(ma));
4391 res = isl_aff_alloc(ls);
4400 isl_seq_preimage(res->v->el, aff->v->el, ma, 0, 0, n_div_ma, n_div_aff,
4409 isl_multi_aff_free(ma);
4410 res = isl_aff_normalize(res);
4414 isl_multi_aff_free(ma);
4419 /* Compute the pullback of "ma1" by the function represented by "ma2".
4420 * In other words, plug in "ma2" in "ma1".
4422 __isl_give isl_multi_aff *isl_multi_aff_pullback_multi_aff(
4423 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2)
4426 isl_space *space = NULL;
4428 ma2 = isl_multi_aff_align_divs(ma2);
4429 ma1 = isl_multi_aff_cow(ma1);
4433 space = isl_space_join(isl_multi_aff_get_space(ma2),
4434 isl_multi_aff_get_space(ma1));
4436 for (i = 0; i < ma1->n; ++i) {
4437 ma1->p[i] = isl_aff_pullback_multi_aff(ma1->p[i],
4438 isl_multi_aff_copy(ma2));
4443 ma1 = isl_multi_aff_reset_space(ma1, space);
4444 isl_multi_aff_free(ma2);
4447 isl_space_free(space);
4448 isl_multi_aff_free(ma2);
4449 isl_multi_aff_free(ma1);
4453 /* Extend the local space of "dst" to include the divs
4454 * in the local space of "src".
4456 __isl_give isl_aff *isl_aff_align_divs(__isl_take isl_aff *dst,
4457 __isl_keep isl_aff *src)
4465 return isl_aff_free(dst);
4467 ctx = isl_aff_get_ctx(src);
4468 if (!isl_space_is_equal(src->ls->dim, dst->ls->dim))
4469 isl_die(ctx, isl_error_invalid,
4470 "spaces don't match", goto error);
4472 if (src->ls->div->n_row == 0)
4475 exp1 = isl_alloc_array(ctx, int, src->ls->div->n_row);
4476 exp2 = isl_alloc_array(ctx, int, dst->ls->div->n_row);
4480 div = isl_merge_divs(src->ls->div, dst->ls->div, exp1, exp2);
4481 dst = isl_aff_expand_divs(dst, div, exp2);
4489 return isl_aff_free(dst);
4492 /* Adjust the local spaces of the affine expressions in "maff"
4493 * such that they all have the save divs.
4495 __isl_give isl_multi_aff *isl_multi_aff_align_divs(
4496 __isl_take isl_multi_aff *maff)
4504 maff = isl_multi_aff_cow(maff);
4508 for (i = 1; i < maff->n; ++i)
4509 maff->p[0] = isl_aff_align_divs(maff->p[0], maff->p[i]);
4510 for (i = 1; i < maff->n; ++i) {
4511 maff->p[i] = isl_aff_align_divs(maff->p[i], maff->p[0]);
4513 return isl_multi_aff_free(maff);
4519 __isl_give isl_aff *isl_aff_lift(__isl_take isl_aff *aff)
4521 aff = isl_aff_cow(aff);
4525 aff->ls = isl_local_space_lift(aff->ls);
4527 return isl_aff_free(aff);
4532 /* Lift "maff" to a space with extra dimensions such that the result
4533 * has no more existentially quantified variables.
4534 * If "ls" is not NULL, then *ls is assigned the local space that lies
4535 * at the basis of the lifting applied to "maff".
4537 __isl_give isl_multi_aff *isl_multi_aff_lift(__isl_take isl_multi_aff *maff,
4538 __isl_give isl_local_space **ls)
4552 isl_space *space = isl_multi_aff_get_domain_space(maff);
4553 *ls = isl_local_space_from_space(space);
4555 return isl_multi_aff_free(maff);
4560 maff = isl_multi_aff_cow(maff);
4561 maff = isl_multi_aff_align_divs(maff);
4565 n_div = isl_aff_dim(maff->p[0], isl_dim_div);
4566 space = isl_multi_aff_get_space(maff);
4567 space = isl_space_lift(isl_space_domain(space), n_div);
4568 space = isl_space_extend_domain_with_range(space,
4569 isl_multi_aff_get_space(maff));
4571 return isl_multi_aff_free(maff);
4572 isl_space_free(maff->space);
4573 maff->space = space;
4576 *ls = isl_aff_get_domain_local_space(maff->p[0]);
4578 return isl_multi_aff_free(maff);
4581 for (i = 0; i < maff->n; ++i) {
4582 maff->p[i] = isl_aff_lift(maff->p[i]);
4590 isl_local_space_free(*ls);
4591 return isl_multi_aff_free(maff);
4595 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma".
4597 __isl_give isl_pw_aff *isl_pw_multi_aff_get_pw_aff(
4598 __isl_keep isl_pw_multi_aff *pma, int pos)
4608 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
4609 if (pos < 0 || pos >= n_out)
4610 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
4611 "index out of bounds", return NULL);
4613 space = isl_pw_multi_aff_get_space(pma);
4614 space = isl_space_drop_dims(space, isl_dim_out,
4615 pos + 1, n_out - pos - 1);
4616 space = isl_space_drop_dims(space, isl_dim_out, 0, pos);
4618 pa = isl_pw_aff_alloc_size(space, pma->n);
4619 for (i = 0; i < pma->n; ++i) {
4621 aff = isl_multi_aff_get_aff(pma->p[i].maff, pos);
4622 pa = isl_pw_aff_add_piece(pa, isl_set_copy(pma->p[i].set), aff);
4628 /* Return an isl_pw_multi_aff with the given "set" as domain and
4629 * an unnamed zero-dimensional range.
4631 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_domain(
4632 __isl_take isl_set *set)
4637 space = isl_set_get_space(set);
4638 space = isl_space_from_domain(space);
4639 ma = isl_multi_aff_zero(space);
4640 return isl_pw_multi_aff_alloc(set, ma);
4643 /* Add an isl_pw_multi_aff with the given "set" as domain and
4644 * an unnamed zero-dimensional range to *user.
4646 static int add_pw_multi_aff_from_domain(__isl_take isl_set *set, void *user)
4648 isl_union_pw_multi_aff **upma = user;
4649 isl_pw_multi_aff *pma;
4651 pma = isl_pw_multi_aff_from_domain(set);
4652 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
4657 /* Return an isl_union_pw_multi_aff with the given "uset" as domain and
4658 * an unnamed zero-dimensional range.
4660 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_domain(
4661 __isl_take isl_union_set *uset)
4664 isl_union_pw_multi_aff *upma;
4669 space = isl_union_set_get_space(uset);
4670 upma = isl_union_pw_multi_aff_empty(space);
4672 if (isl_union_set_foreach_set(uset,
4673 &add_pw_multi_aff_from_domain, &upma) < 0)
4676 isl_union_set_free(uset);
4679 isl_union_set_free(uset);
4680 isl_union_pw_multi_aff_free(upma);
4684 /* Convert "pma" to an isl_map and add it to *umap.
4686 static int map_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma, void *user)
4688 isl_union_map **umap = user;
4691 map = isl_map_from_pw_multi_aff(pma);
4692 *umap = isl_union_map_add_map(*umap, map);
4697 /* Construct a union map mapping the domain of the union
4698 * piecewise multi-affine expression to its range, with each dimension
4699 * in the range equated to the corresponding affine expression on its cell.
4701 __isl_give isl_union_map *isl_union_map_from_union_pw_multi_aff(
4702 __isl_take isl_union_pw_multi_aff *upma)
4705 isl_union_map *umap;
4710 space = isl_union_pw_multi_aff_get_space(upma);
4711 umap = isl_union_map_empty(space);
4713 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
4714 &map_from_pw_multi_aff, &umap) < 0)
4717 isl_union_pw_multi_aff_free(upma);
4720 isl_union_pw_multi_aff_free(upma);
4721 isl_union_map_free(umap);
4725 /* Local data for bin_entry and the callback "fn".
4727 struct isl_union_pw_multi_aff_bin_data {
4728 isl_union_pw_multi_aff *upma2;
4729 isl_union_pw_multi_aff *res;
4730 isl_pw_multi_aff *pma;
4731 int (*fn)(void **entry, void *user);
4734 /* Given an isl_pw_multi_aff from upma1, store it in data->pma
4735 * and call data->fn for each isl_pw_multi_aff in data->upma2.
4737 static int bin_entry(void **entry, void *user)
4739 struct isl_union_pw_multi_aff_bin_data *data = user;
4740 isl_pw_multi_aff *pma = *entry;
4743 if (isl_hash_table_foreach(data->upma2->dim->ctx, &data->upma2->table,
4744 data->fn, data) < 0)
4750 /* Call "fn" on each pair of isl_pw_multi_affs in "upma1" and "upma2".
4751 * The isl_pw_multi_aff from upma1 is stored in data->pma (where data is
4752 * passed as user field) and the isl_pw_multi_aff from upma2 is available
4753 * as *entry. The callback should adjust data->res if desired.
4755 static __isl_give isl_union_pw_multi_aff *bin_op(
4756 __isl_take isl_union_pw_multi_aff *upma1,
4757 __isl_take isl_union_pw_multi_aff *upma2,
4758 int (*fn)(void **entry, void *user))
4761 struct isl_union_pw_multi_aff_bin_data data = { NULL, NULL, NULL, fn };
4763 space = isl_union_pw_multi_aff_get_space(upma2);
4764 upma1 = isl_union_pw_multi_aff_align_params(upma1, space);
4765 space = isl_union_pw_multi_aff_get_space(upma1);
4766 upma2 = isl_union_pw_multi_aff_align_params(upma2, space);
4768 if (!upma1 || !upma2)
4772 data.res = isl_union_pw_multi_aff_alloc(isl_space_copy(upma1->dim),
4774 if (isl_hash_table_foreach(upma1->dim->ctx, &upma1->table,
4775 &bin_entry, &data) < 0)
4778 isl_union_pw_multi_aff_free(upma1);
4779 isl_union_pw_multi_aff_free(upma2);
4782 isl_union_pw_multi_aff_free(upma1);
4783 isl_union_pw_multi_aff_free(upma2);
4784 isl_union_pw_multi_aff_free(data.res);
4788 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
4789 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
4791 static __isl_give isl_pw_multi_aff *pw_multi_aff_range_product(
4792 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4796 space = isl_space_range_product(isl_pw_multi_aff_get_space(pma1),
4797 isl_pw_multi_aff_get_space(pma2));
4798 return isl_pw_multi_aff_on_shared_domain_in(pma1, pma2, space,
4799 &isl_multi_aff_range_product);
4802 /* Given two isl_pw_multi_affs A -> B and C -> D,
4803 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
4805 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_range_product(
4806 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4808 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4809 &pw_multi_aff_range_product);
4812 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
4813 * construct an isl_pw_multi_aff (A * C) -> (B, D).
4815 static __isl_give isl_pw_multi_aff *pw_multi_aff_flat_range_product(
4816 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4820 space = isl_space_range_product(isl_pw_multi_aff_get_space(pma1),
4821 isl_pw_multi_aff_get_space(pma2));
4822 space = isl_space_flatten_range(space);
4823 return isl_pw_multi_aff_on_shared_domain_in(pma1, pma2, space,
4824 &isl_multi_aff_flat_range_product);
4827 /* Given two isl_pw_multi_affs A -> B and C -> D,
4828 * construct an isl_pw_multi_aff (A * C) -> (B, D).
4830 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_flat_range_product(
4831 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4833 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4834 &pw_multi_aff_flat_range_product);
4837 /* If data->pma and *entry have the same domain space, then compute
4838 * their flat range product and the result to data->res.
4840 static int flat_range_product_entry(void **entry, void *user)
4842 struct isl_union_pw_multi_aff_bin_data *data = user;
4843 isl_pw_multi_aff *pma2 = *entry;
4845 if (!isl_space_tuple_match(data->pma->dim, isl_dim_in,
4846 pma2->dim, isl_dim_in))
4849 pma2 = isl_pw_multi_aff_flat_range_product(
4850 isl_pw_multi_aff_copy(data->pma),
4851 isl_pw_multi_aff_copy(pma2));
4853 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma2);
4858 /* Given two isl_union_pw_multi_affs A -> B and C -> D,
4859 * construct an isl_union_pw_multi_aff (A * C) -> (B, D).
4861 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_flat_range_product(
4862 __isl_take isl_union_pw_multi_aff *upma1,
4863 __isl_take isl_union_pw_multi_aff *upma2)
4865 return bin_op(upma1, upma2, &flat_range_product_entry);
4868 /* Replace the affine expressions at position "pos" in "pma" by "pa".
4869 * The parameters are assumed to have been aligned.
4871 * The implementation essentially performs an isl_pw_*_on_shared_domain,
4872 * except that it works on two different isl_pw_* types.
4874 static __isl_give isl_pw_multi_aff *pw_multi_aff_set_pw_aff(
4875 __isl_take isl_pw_multi_aff *pma, unsigned pos,
4876 __isl_take isl_pw_aff *pa)
4879 isl_pw_multi_aff *res = NULL;
4884 if (!isl_space_tuple_match(pma->dim, isl_dim_in, pa->dim, isl_dim_in))
4885 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
4886 "domains don't match", goto error);
4887 if (pos >= isl_pw_multi_aff_dim(pma, isl_dim_out))
4888 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
4889 "index out of bounds", goto error);
4892 res = isl_pw_multi_aff_alloc_size(isl_pw_multi_aff_get_space(pma), n);
4894 for (i = 0; i < pma->n; ++i) {
4895 for (j = 0; j < pa->n; ++j) {
4897 isl_multi_aff *res_ij;
4900 common = isl_set_intersect(isl_set_copy(pma->p[i].set),
4901 isl_set_copy(pa->p[j].set));
4902 empty = isl_set_plain_is_empty(common);
4903 if (empty < 0 || empty) {
4904 isl_set_free(common);
4910 res_ij = isl_multi_aff_set_aff(
4911 isl_multi_aff_copy(pma->p[i].maff), pos,
4912 isl_aff_copy(pa->p[j].aff));
4913 res_ij = isl_multi_aff_gist(res_ij,
4914 isl_set_copy(common));
4916 res = isl_pw_multi_aff_add_piece(res, common, res_ij);
4920 isl_pw_multi_aff_free(pma);
4921 isl_pw_aff_free(pa);
4924 isl_pw_multi_aff_free(pma);
4925 isl_pw_aff_free(pa);
4926 return isl_pw_multi_aff_free(res);
4929 /* Replace the affine expressions at position "pos" in "pma" by "pa".
4931 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_set_pw_aff(
4932 __isl_take isl_pw_multi_aff *pma, unsigned pos,
4933 __isl_take isl_pw_aff *pa)
4937 if (isl_space_match(pma->dim, isl_dim_param, pa->dim, isl_dim_param))
4938 return pw_multi_aff_set_pw_aff(pma, pos, pa);
4939 if (!isl_space_has_named_params(pma->dim) ||
4940 !isl_space_has_named_params(pa->dim))
4941 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
4942 "unaligned unnamed parameters", goto error);
4943 pma = isl_pw_multi_aff_align_params(pma, isl_pw_aff_get_space(pa));
4944 pa = isl_pw_aff_align_params(pa, isl_pw_multi_aff_get_space(pma));
4945 return pw_multi_aff_set_pw_aff(pma, pos, pa);
4947 isl_pw_multi_aff_free(pma);
4948 isl_pw_aff_free(pa);
4952 /* Check that the domain space of "pa" matches "space".
4954 * Return 0 on success and -1 on error.
4956 int isl_pw_aff_check_match_domain_space(__isl_keep isl_pw_aff *pa,
4957 __isl_keep isl_space *space)
4959 isl_space *pa_space;
4965 pa_space = isl_pw_aff_get_space(pa);
4967 match = isl_space_match(space, isl_dim_param, pa_space, isl_dim_param);
4971 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
4972 "parameters don't match", goto error);
4973 match = isl_space_tuple_match(space, isl_dim_in, pa_space, isl_dim_in);
4977 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
4978 "domains don't match", goto error);
4979 isl_space_free(pa_space);
4982 isl_space_free(pa_space);
4989 #include <isl_multi_templ.c>
4991 /* Scale the first elements of "ma" by the corresponding elements of "vec".
4993 __isl_give isl_multi_aff *isl_multi_aff_scale_vec(__isl_take isl_multi_aff *ma,
4994 __isl_take isl_vec *vec)
5002 n = isl_multi_aff_dim(ma, isl_dim_out);
5003 if (isl_vec_size(vec) < n)
5004 n = isl_vec_size(vec);
5007 for (i = 0; i < n; ++i) {
5010 isl_vec_get_element(vec, i, &v);
5012 aff = isl_multi_aff_get_aff(ma, i);
5013 aff = isl_aff_scale(aff, v);
5014 ma = isl_multi_aff_set_aff(ma, i, aff);
5022 isl_multi_aff_free(ma);
5026 /* Scale the first elements of "pma" by the corresponding elements of "vec".
5028 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_scale_vec(
5029 __isl_take isl_pw_multi_aff *pma, __isl_take isl_vec *v)
5033 pma = isl_pw_multi_aff_cow(pma);
5037 for (i = 0; i < pma->n; ++i) {
5038 pma->p[i].maff = isl_multi_aff_scale_vec(pma->p[i].maff,
5040 if (!pma->p[i].maff)
5048 isl_pw_multi_aff_free(pma);
5052 /* This function is called for each entry of an isl_union_pw_multi_aff.
5053 * Replace the entry by the result of applying isl_pw_multi_aff_scale_vec
5054 * to the original entry with the isl_vec in "user" as extra argument.
5056 static int union_pw_multi_aff_scale_vec_entry(void **entry, void *user)
5058 isl_pw_multi_aff **pma = (isl_pw_multi_aff **) entry;
5061 *pma = isl_pw_multi_aff_scale_vec(*pma, isl_vec_copy(v));
5068 /* Scale the first elements of "upma" by the corresponding elements of "vec".
5070 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_scale_vec(
5071 __isl_take isl_union_pw_multi_aff *upma, __isl_take isl_vec *v)
5073 upma = isl_union_pw_multi_aff_cow(upma);
5077 if (isl_hash_table_foreach(upma->dim->ctx, &upma->table,
5078 &union_pw_multi_aff_scale_vec_entry, v) < 0)
5085 isl_union_pw_multi_aff_free(upma);