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 /* Return the coefficient of the variable of type "type" at position "pos"
461 __isl_give isl_val *isl_aff_get_coefficient_val(__isl_keep isl_aff *aff,
462 enum isl_dim_type type, int pos)
470 ctx = isl_aff_get_ctx(aff);
471 if (type == isl_dim_out)
472 isl_die(ctx, isl_error_invalid,
473 "output/set dimension does not have a coefficient",
475 if (type == isl_dim_in)
478 if (pos >= isl_local_space_dim(aff->ls, type))
479 isl_die(ctx, isl_error_invalid,
480 "position out of bounds", return NULL);
482 pos += isl_local_space_offset(aff->ls, type);
483 v = isl_val_rat_from_isl_int(ctx, aff->v->el[1 + pos], aff->v->el[0]);
484 return isl_val_normalize(v);
487 __isl_give isl_aff *isl_aff_set_denominator(__isl_take isl_aff *aff, isl_int v)
489 aff = isl_aff_cow(aff);
493 aff->v = isl_vec_cow(aff->v);
495 return isl_aff_free(aff);
497 isl_int_set(aff->v->el[0], v);
502 __isl_give isl_aff *isl_aff_set_constant(__isl_take isl_aff *aff, isl_int v)
504 aff = isl_aff_cow(aff);
508 aff->v = isl_vec_cow(aff->v);
510 return isl_aff_free(aff);
512 isl_int_set(aff->v->el[1], v);
517 /* Replace the constant term of "aff" by "v".
519 __isl_give isl_aff *isl_aff_set_constant_val(__isl_take isl_aff *aff,
520 __isl_take isl_val *v)
525 if (!isl_val_is_rat(v))
526 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
527 "expecting rational value", goto error);
529 if (isl_int_eq(aff->v->el[1], v->n) &&
530 isl_int_eq(aff->v->el[0], v->d)) {
535 aff = isl_aff_cow(aff);
538 aff->v = isl_vec_cow(aff->v);
542 if (isl_int_eq(aff->v->el[0], v->d)) {
543 isl_int_set(aff->v->el[1], v->n);
544 } else if (isl_int_is_one(v->d)) {
545 isl_int_mul(aff->v->el[1], aff->v->el[0], v->n);
547 isl_seq_scale(aff->v->el + 1,
548 aff->v->el + 1, v->d, aff->v->size - 1);
549 isl_int_mul(aff->v->el[1], aff->v->el[0], v->n);
550 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
551 aff->v = isl_vec_normalize(aff->v);
564 __isl_give isl_aff *isl_aff_add_constant(__isl_take isl_aff *aff, isl_int v)
566 if (isl_int_is_zero(v))
569 aff = isl_aff_cow(aff);
573 aff->v = isl_vec_cow(aff->v);
575 return isl_aff_free(aff);
577 isl_int_addmul(aff->v->el[1], aff->v->el[0], v);
582 /* Add "v" to the constant term of "aff".
584 __isl_give isl_aff *isl_aff_add_constant_val(__isl_take isl_aff *aff,
585 __isl_take isl_val *v)
590 if (isl_val_is_zero(v)) {
595 if (!isl_val_is_rat(v))
596 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
597 "expecting rational value", goto error);
599 aff = isl_aff_cow(aff);
603 aff->v = isl_vec_cow(aff->v);
607 if (isl_int_is_one(v->d)) {
608 isl_int_addmul(aff->v->el[1], aff->v->el[0], v->n);
609 } else if (isl_int_eq(aff->v->el[0], v->d)) {
610 isl_int_add(aff->v->el[1], aff->v->el[1], v->n);
611 aff->v = isl_vec_normalize(aff->v);
615 isl_seq_scale(aff->v->el + 1,
616 aff->v->el + 1, v->d, aff->v->size - 1);
617 isl_int_addmul(aff->v->el[1], aff->v->el[0], v->n);
618 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
619 aff->v = isl_vec_normalize(aff->v);
632 __isl_give isl_aff *isl_aff_add_constant_si(__isl_take isl_aff *aff, int v)
637 isl_int_set_si(t, v);
638 aff = isl_aff_add_constant(aff, t);
644 /* Add "v" to the numerator of the constant term of "aff".
646 __isl_give isl_aff *isl_aff_add_constant_num(__isl_take isl_aff *aff, isl_int v)
648 if (isl_int_is_zero(v))
651 aff = isl_aff_cow(aff);
655 aff->v = isl_vec_cow(aff->v);
657 return isl_aff_free(aff);
659 isl_int_add(aff->v->el[1], aff->v->el[1], v);
664 /* Add "v" to the numerator of the constant term of "aff".
666 __isl_give isl_aff *isl_aff_add_constant_num_si(__isl_take isl_aff *aff, int v)
674 isl_int_set_si(t, v);
675 aff = isl_aff_add_constant_num(aff, t);
681 __isl_give isl_aff *isl_aff_set_constant_si(__isl_take isl_aff *aff, int v)
683 aff = isl_aff_cow(aff);
687 aff->v = isl_vec_cow(aff->v);
689 return isl_aff_free(aff);
691 isl_int_set_si(aff->v->el[1], v);
696 __isl_give isl_aff *isl_aff_set_coefficient(__isl_take isl_aff *aff,
697 enum isl_dim_type type, int pos, isl_int v)
702 if (type == isl_dim_out)
703 isl_die(aff->v->ctx, isl_error_invalid,
704 "output/set dimension does not have a coefficient",
705 return isl_aff_free(aff));
706 if (type == isl_dim_in)
709 if (pos >= isl_local_space_dim(aff->ls, type))
710 isl_die(aff->v->ctx, isl_error_invalid,
711 "position out of bounds", return isl_aff_free(aff));
713 aff = isl_aff_cow(aff);
717 aff->v = isl_vec_cow(aff->v);
719 return isl_aff_free(aff);
721 pos += isl_local_space_offset(aff->ls, type);
722 isl_int_set(aff->v->el[1 + pos], v);
727 __isl_give isl_aff *isl_aff_set_coefficient_si(__isl_take isl_aff *aff,
728 enum isl_dim_type type, int pos, int v)
733 if (type == isl_dim_out)
734 isl_die(aff->v->ctx, isl_error_invalid,
735 "output/set dimension does not have a coefficient",
736 return isl_aff_free(aff));
737 if (type == isl_dim_in)
740 if (pos >= isl_local_space_dim(aff->ls, type))
741 isl_die(aff->v->ctx, isl_error_invalid,
742 "position out of bounds", return isl_aff_free(aff));
744 aff = isl_aff_cow(aff);
748 aff->v = isl_vec_cow(aff->v);
750 return isl_aff_free(aff);
752 pos += isl_local_space_offset(aff->ls, type);
753 isl_int_set_si(aff->v->el[1 + pos], v);
758 /* Replace the coefficient of the variable of type "type" at position "pos"
761 __isl_give isl_aff *isl_aff_set_coefficient_val(__isl_take isl_aff *aff,
762 enum isl_dim_type type, int pos, __isl_take isl_val *v)
767 if (type == isl_dim_out)
768 isl_die(aff->v->ctx, isl_error_invalid,
769 "output/set dimension does not have a coefficient",
771 if (type == isl_dim_in)
774 if (pos >= isl_local_space_dim(aff->ls, type))
775 isl_die(aff->v->ctx, isl_error_invalid,
776 "position out of bounds", goto error);
778 if (!isl_val_is_rat(v))
779 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
780 "expecting rational value", goto error);
782 pos += isl_local_space_offset(aff->ls, type);
783 if (isl_int_eq(aff->v->el[1 + pos], v->n) &&
784 isl_int_eq(aff->v->el[0], v->d)) {
789 aff = isl_aff_cow(aff);
792 aff->v = isl_vec_cow(aff->v);
796 if (isl_int_eq(aff->v->el[0], v->d)) {
797 isl_int_set(aff->v->el[1 + pos], v->n);
798 } else if (isl_int_is_one(v->d)) {
799 isl_int_mul(aff->v->el[1 + pos], aff->v->el[0], v->n);
801 isl_seq_scale(aff->v->el + 1,
802 aff->v->el + 1, v->d, aff->v->size - 1);
803 isl_int_mul(aff->v->el[1 + pos], aff->v->el[0], v->n);
804 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
805 aff->v = isl_vec_normalize(aff->v);
818 __isl_give isl_aff *isl_aff_add_coefficient(__isl_take isl_aff *aff,
819 enum isl_dim_type type, int pos, isl_int v)
824 if (type == isl_dim_out)
825 isl_die(aff->v->ctx, isl_error_invalid,
826 "output/set dimension does not have a coefficient",
827 return isl_aff_free(aff));
828 if (type == isl_dim_in)
831 if (pos >= isl_local_space_dim(aff->ls, type))
832 isl_die(aff->v->ctx, isl_error_invalid,
833 "position out of bounds", return isl_aff_free(aff));
835 aff = isl_aff_cow(aff);
839 aff->v = isl_vec_cow(aff->v);
841 return isl_aff_free(aff);
843 pos += isl_local_space_offset(aff->ls, type);
844 isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v);
849 /* Add "v" to the coefficient of the variable of type "type"
850 * at position "pos" of "aff".
852 __isl_give isl_aff *isl_aff_add_coefficient_val(__isl_take isl_aff *aff,
853 enum isl_dim_type type, int pos, __isl_take isl_val *v)
858 if (isl_val_is_zero(v)) {
863 if (type == isl_dim_out)
864 isl_die(aff->v->ctx, isl_error_invalid,
865 "output/set dimension does not have a coefficient",
867 if (type == isl_dim_in)
870 if (pos >= isl_local_space_dim(aff->ls, type))
871 isl_die(aff->v->ctx, isl_error_invalid,
872 "position out of bounds", goto error);
874 if (!isl_val_is_rat(v))
875 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
876 "expecting rational value", goto error);
878 aff = isl_aff_cow(aff);
882 aff->v = isl_vec_cow(aff->v);
886 pos += isl_local_space_offset(aff->ls, type);
887 if (isl_int_is_one(v->d)) {
888 isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v->n);
889 } else if (isl_int_eq(aff->v->el[0], v->d)) {
890 isl_int_add(aff->v->el[1 + pos], aff->v->el[1 + pos], v->n);
891 aff->v = isl_vec_normalize(aff->v);
895 isl_seq_scale(aff->v->el + 1,
896 aff->v->el + 1, v->d, aff->v->size - 1);
897 isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v->n);
898 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
899 aff->v = isl_vec_normalize(aff->v);
912 __isl_give isl_aff *isl_aff_add_coefficient_si(__isl_take isl_aff *aff,
913 enum isl_dim_type type, int pos, int v)
918 isl_int_set_si(t, v);
919 aff = isl_aff_add_coefficient(aff, type, pos, t);
925 __isl_give isl_aff *isl_aff_get_div(__isl_keep isl_aff *aff, int pos)
930 return isl_local_space_get_div(aff->ls, pos);
933 __isl_give isl_aff *isl_aff_neg(__isl_take isl_aff *aff)
935 aff = isl_aff_cow(aff);
938 aff->v = isl_vec_cow(aff->v);
940 return isl_aff_free(aff);
942 isl_seq_neg(aff->v->el + 1, aff->v->el + 1, aff->v->size - 1);
947 /* Remove divs from the local space that do not appear in the affine
949 * We currently only remove divs at the end.
950 * Some intermediate divs may also not appear directly in the affine
951 * expression, but we would also need to check that no other divs are
952 * defined in terms of them.
954 __isl_give isl_aff *isl_aff_remove_unused_divs( __isl_take isl_aff *aff)
963 n = isl_local_space_dim(aff->ls, isl_dim_div);
964 off = isl_local_space_offset(aff->ls, isl_dim_div);
966 pos = isl_seq_last_non_zero(aff->v->el + 1 + off, n) + 1;
970 aff = isl_aff_cow(aff);
974 aff->ls = isl_local_space_drop_dims(aff->ls, isl_dim_div, pos, n - pos);
975 aff->v = isl_vec_drop_els(aff->v, 1 + off + pos, n - pos);
976 if (!aff->ls || !aff->v)
977 return isl_aff_free(aff);
982 /* Given two affine expressions "p" of length p_len (including the
983 * denominator and the constant term) and "subs" of length subs_len,
984 * plug in "subs" for the variable at position "pos".
985 * The variables of "subs" and "p" are assumed to match up to subs_len,
986 * but "p" may have additional variables.
987 * "v" is an initialized isl_int that can be used internally.
989 * In particular, if "p" represents the expression
993 * with i the variable at position "pos" and "subs" represents the expression
997 * then the result represents the expression
1002 void isl_seq_substitute(isl_int *p, int pos, isl_int *subs,
1003 int p_len, int subs_len, isl_int v)
1005 isl_int_set(v, p[1 + pos]);
1006 isl_int_set_si(p[1 + pos], 0);
1007 isl_seq_combine(p + 1, subs[0], p + 1, v, subs + 1, subs_len - 1);
1008 isl_seq_scale(p + subs_len, p + subs_len, subs[0], p_len - subs_len);
1009 isl_int_mul(p[0], p[0], subs[0]);
1012 /* Look for any divs in the aff->ls with a denominator equal to one
1013 * and plug them into the affine expression and any subsequent divs
1014 * that may reference the div.
1016 static __isl_give isl_aff *plug_in_integral_divs(__isl_take isl_aff *aff)
1022 isl_local_space *ls;
1028 n = isl_local_space_dim(aff->ls, isl_dim_div);
1030 for (i = 0; i < n; ++i) {
1031 if (!isl_int_is_one(aff->ls->div->row[i][0]))
1033 ls = isl_local_space_copy(aff->ls);
1034 ls = isl_local_space_substitute_seq(ls, isl_dim_div, i,
1035 aff->ls->div->row[i], len, i + 1, n - (i + 1));
1036 vec = isl_vec_copy(aff->v);
1037 vec = isl_vec_cow(vec);
1043 pos = isl_local_space_offset(aff->ls, isl_dim_div) + i;
1044 isl_seq_substitute(vec->el, pos, aff->ls->div->row[i],
1049 isl_vec_free(aff->v);
1051 isl_local_space_free(aff->ls);
1058 isl_local_space_free(ls);
1059 return isl_aff_free(aff);
1062 /* Look for any divs j that appear with a unit coefficient inside
1063 * the definitions of other divs i and plug them into the definitions
1066 * In particular, an expression of the form
1068 * floor((f(..) + floor(g(..)/n))/m)
1072 * floor((n * f(..) + g(..))/(n * m))
1074 * This simplification is correct because we can move the expression
1075 * f(..) into the inner floor in the original expression to obtain
1077 * floor(floor((n * f(..) + g(..))/n)/m)
1079 * from which we can derive the simplified expression.
1081 static __isl_give isl_aff *plug_in_unit_divs(__isl_take isl_aff *aff)
1089 n = isl_local_space_dim(aff->ls, isl_dim_div);
1090 off = isl_local_space_offset(aff->ls, isl_dim_div);
1091 for (i = 1; i < n; ++i) {
1092 for (j = 0; j < i; ++j) {
1093 if (!isl_int_is_one(aff->ls->div->row[i][1 + off + j]))
1095 aff->ls = isl_local_space_substitute_seq(aff->ls,
1096 isl_dim_div, j, aff->ls->div->row[j],
1097 aff->v->size, i, 1);
1099 return isl_aff_free(aff);
1106 /* Swap divs "a" and "b" in "aff", which is assumed to be non-NULL.
1108 * Even though this function is only called on isl_affs with a single
1109 * reference, we are careful to only change aff->v and aff->ls together.
1111 static __isl_give isl_aff *swap_div(__isl_take isl_aff *aff, int a, int b)
1113 unsigned off = isl_local_space_offset(aff->ls, isl_dim_div);
1114 isl_local_space *ls;
1117 ls = isl_local_space_copy(aff->ls);
1118 ls = isl_local_space_swap_div(ls, a, b);
1119 v = isl_vec_copy(aff->v);
1124 isl_int_swap(v->el[1 + off + a], v->el[1 + off + b]);
1125 isl_vec_free(aff->v);
1127 isl_local_space_free(aff->ls);
1133 isl_local_space_free(ls);
1134 return isl_aff_free(aff);
1137 /* Merge divs "a" and "b" in "aff", which is assumed to be non-NULL.
1139 * We currently do not actually remove div "b", but simply add its
1140 * coefficient to that of "a" and then zero it out.
1142 static __isl_give isl_aff *merge_divs(__isl_take isl_aff *aff, int a, int b)
1144 unsigned off = isl_local_space_offset(aff->ls, isl_dim_div);
1146 if (isl_int_is_zero(aff->v->el[1 + off + b]))
1149 aff->v = isl_vec_cow(aff->v);
1151 return isl_aff_free(aff);
1153 isl_int_add(aff->v->el[1 + off + a],
1154 aff->v->el[1 + off + a], aff->v->el[1 + off + b]);
1155 isl_int_set_si(aff->v->el[1 + off + b], 0);
1160 /* Sort the divs in the local space of "aff" according to
1161 * the comparison function "cmp_row" in isl_local_space.c,
1162 * combining the coefficients of identical divs.
1164 * Reordering divs does not change the semantics of "aff",
1165 * so there is no need to call isl_aff_cow.
1166 * Moreover, this function is currently only called on isl_affs
1167 * with a single reference.
1169 static __isl_give isl_aff *sort_divs(__isl_take isl_aff *aff)
1177 off = isl_local_space_offset(aff->ls, isl_dim_div);
1178 n = isl_aff_dim(aff, isl_dim_div);
1179 for (i = 1; i < n; ++i) {
1180 for (j = i - 1; j >= 0; --j) {
1181 int cmp = isl_mat_cmp_div(aff->ls->div, j, j + 1);
1185 aff = merge_divs(aff, j, j + 1);
1187 aff = swap_div(aff, j, j + 1);
1196 /* Normalize the representation of "aff".
1198 * This function should only be called of "new" isl_affs, i.e.,
1199 * with only a single reference. We therefore do not need to
1200 * worry about affecting other instances.
1202 __isl_give isl_aff *isl_aff_normalize(__isl_take isl_aff *aff)
1206 aff->v = isl_vec_normalize(aff->v);
1208 return isl_aff_free(aff);
1209 aff = plug_in_integral_divs(aff);
1210 aff = plug_in_unit_divs(aff);
1211 aff = sort_divs(aff);
1212 aff = isl_aff_remove_unused_divs(aff);
1216 /* Given f, return floor(f).
1217 * If f is an integer expression, then just return f.
1218 * If f is a constant, then return the constant floor(f).
1219 * Otherwise, if f = g/m, write g = q m + r,
1220 * create a new div d = [r/m] and return the expression q + d.
1221 * The coefficients in r are taken to lie between -m/2 and m/2.
1223 __isl_give isl_aff *isl_aff_floor(__isl_take isl_aff *aff)
1233 if (isl_int_is_one(aff->v->el[0]))
1236 aff = isl_aff_cow(aff);
1240 aff->v = isl_vec_cow(aff->v);
1242 return isl_aff_free(aff);
1244 if (isl_aff_is_cst(aff)) {
1245 isl_int_fdiv_q(aff->v->el[1], aff->v->el[1], aff->v->el[0]);
1246 isl_int_set_si(aff->v->el[0], 1);
1250 div = isl_vec_copy(aff->v);
1251 div = isl_vec_cow(div);
1253 return isl_aff_free(aff);
1255 ctx = isl_aff_get_ctx(aff);
1256 isl_int_fdiv_q(aff->v->el[0], aff->v->el[0], ctx->two);
1257 for (i = 1; i < aff->v->size; ++i) {
1258 isl_int_fdiv_r(div->el[i], div->el[i], div->el[0]);
1259 isl_int_fdiv_q(aff->v->el[i], aff->v->el[i], div->el[0]);
1260 if (isl_int_gt(div->el[i], aff->v->el[0])) {
1261 isl_int_sub(div->el[i], div->el[i], div->el[0]);
1262 isl_int_add_ui(aff->v->el[i], aff->v->el[i], 1);
1266 aff->ls = isl_local_space_add_div(aff->ls, div);
1268 return isl_aff_free(aff);
1270 size = aff->v->size;
1271 aff->v = isl_vec_extend(aff->v, size + 1);
1273 return isl_aff_free(aff);
1274 isl_int_set_si(aff->v->el[0], 1);
1275 isl_int_set_si(aff->v->el[size], 1);
1277 aff = isl_aff_normalize(aff);
1284 * aff mod m = aff - m * floor(aff/m)
1286 __isl_give isl_aff *isl_aff_mod(__isl_take isl_aff *aff, isl_int m)
1290 res = isl_aff_copy(aff);
1291 aff = isl_aff_scale_down(aff, m);
1292 aff = isl_aff_floor(aff);
1293 aff = isl_aff_scale(aff, m);
1294 res = isl_aff_sub(res, aff);
1301 * pwaff mod m = pwaff - m * floor(pwaff/m)
1303 __isl_give isl_pw_aff *isl_pw_aff_mod(__isl_take isl_pw_aff *pwaff, isl_int m)
1307 res = isl_pw_aff_copy(pwaff);
1308 pwaff = isl_pw_aff_scale_down(pwaff, m);
1309 pwaff = isl_pw_aff_floor(pwaff);
1310 pwaff = isl_pw_aff_scale(pwaff, m);
1311 res = isl_pw_aff_sub(res, pwaff);
1316 /* Given f, return ceil(f).
1317 * If f is an integer expression, then just return f.
1318 * Otherwise, let f be the expression
1324 * floor((e + m - 1)/m)
1326 __isl_give isl_aff *isl_aff_ceil(__isl_take isl_aff *aff)
1331 if (isl_int_is_one(aff->v->el[0]))
1334 aff = isl_aff_cow(aff);
1337 aff->v = isl_vec_cow(aff->v);
1339 return isl_aff_free(aff);
1341 isl_int_add(aff->v->el[1], aff->v->el[1], aff->v->el[0]);
1342 isl_int_sub_ui(aff->v->el[1], aff->v->el[1], 1);
1343 aff = isl_aff_floor(aff);
1348 /* Apply the expansion computed by isl_merge_divs.
1349 * The expansion itself is given by "exp" while the resulting
1350 * list of divs is given by "div".
1352 __isl_give isl_aff *isl_aff_expand_divs( __isl_take isl_aff *aff,
1353 __isl_take isl_mat *div, int *exp)
1360 aff = isl_aff_cow(aff);
1364 old_n_div = isl_local_space_dim(aff->ls, isl_dim_div);
1365 new_n_div = isl_mat_rows(div);
1366 if (new_n_div < old_n_div)
1367 isl_die(isl_mat_get_ctx(div), isl_error_invalid,
1368 "not an expansion", goto error);
1370 aff->v = isl_vec_extend(aff->v, aff->v->size + new_n_div - old_n_div);
1374 offset = 1 + isl_local_space_offset(aff->ls, isl_dim_div);
1376 for (i = new_n_div - 1; i >= 0; --i) {
1377 if (j >= 0 && exp[j] == i) {
1379 isl_int_swap(aff->v->el[offset + i],
1380 aff->v->el[offset + j]);
1383 isl_int_set_si(aff->v->el[offset + i], 0);
1386 aff->ls = isl_local_space_replace_divs(aff->ls, isl_mat_copy(div));
1397 /* Add two affine expressions that live in the same local space.
1399 static __isl_give isl_aff *add_expanded(__isl_take isl_aff *aff1,
1400 __isl_take isl_aff *aff2)
1404 aff1 = isl_aff_cow(aff1);
1408 aff1->v = isl_vec_cow(aff1->v);
1414 isl_int_gcd(gcd, aff1->v->el[0], aff2->v->el[0]);
1415 isl_int_divexact(f, aff2->v->el[0], gcd);
1416 isl_seq_scale(aff1->v->el + 1, aff1->v->el + 1, f, aff1->v->size - 1);
1417 isl_int_divexact(f, aff1->v->el[0], gcd);
1418 isl_seq_addmul(aff1->v->el + 1, f, aff2->v->el + 1, aff1->v->size - 1);
1419 isl_int_divexact(f, aff2->v->el[0], gcd);
1420 isl_int_mul(aff1->v->el[0], aff1->v->el[0], f);
1432 __isl_give isl_aff *isl_aff_add(__isl_take isl_aff *aff1,
1433 __isl_take isl_aff *aff2)
1443 ctx = isl_aff_get_ctx(aff1);
1444 if (!isl_space_is_equal(aff1->ls->dim, aff2->ls->dim))
1445 isl_die(ctx, isl_error_invalid,
1446 "spaces don't match", goto error);
1448 if (aff1->ls->div->n_row == 0 && aff2->ls->div->n_row == 0)
1449 return add_expanded(aff1, aff2);
1451 exp1 = isl_alloc_array(ctx, int, aff1->ls->div->n_row);
1452 exp2 = isl_alloc_array(ctx, int, aff2->ls->div->n_row);
1456 div = isl_merge_divs(aff1->ls->div, aff2->ls->div, exp1, exp2);
1457 aff1 = isl_aff_expand_divs(aff1, isl_mat_copy(div), exp1);
1458 aff2 = isl_aff_expand_divs(aff2, div, exp2);
1462 return add_expanded(aff1, aff2);
1471 __isl_give isl_aff *isl_aff_sub(__isl_take isl_aff *aff1,
1472 __isl_take isl_aff *aff2)
1474 return isl_aff_add(aff1, isl_aff_neg(aff2));
1477 __isl_give isl_aff *isl_aff_scale(__isl_take isl_aff *aff, isl_int f)
1481 if (isl_int_is_one(f))
1484 aff = isl_aff_cow(aff);
1487 aff->v = isl_vec_cow(aff->v);
1489 return isl_aff_free(aff);
1491 if (isl_int_is_pos(f) && isl_int_is_divisible_by(aff->v->el[0], f)) {
1492 isl_int_divexact(aff->v->el[0], aff->v->el[0], f);
1497 isl_int_gcd(gcd, aff->v->el[0], f);
1498 isl_int_divexact(aff->v->el[0], aff->v->el[0], gcd);
1499 isl_int_divexact(gcd, f, gcd);
1500 isl_seq_scale(aff->v->el + 1, aff->v->el + 1, gcd, aff->v->size - 1);
1506 __isl_give isl_aff *isl_aff_scale_down(__isl_take isl_aff *aff, isl_int f)
1510 if (isl_int_is_one(f))
1513 aff = isl_aff_cow(aff);
1517 if (isl_int_is_zero(f))
1518 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1519 "cannot scale down by zero", return isl_aff_free(aff));
1521 aff->v = isl_vec_cow(aff->v);
1523 return isl_aff_free(aff);
1526 isl_seq_gcd(aff->v->el + 1, aff->v->size - 1, &gcd);
1527 isl_int_gcd(gcd, gcd, f);
1528 isl_seq_scale_down(aff->v->el + 1, aff->v->el + 1, gcd, aff->v->size - 1);
1529 isl_int_divexact(gcd, f, gcd);
1530 isl_int_mul(aff->v->el[0], aff->v->el[0], gcd);
1536 __isl_give isl_aff *isl_aff_scale_down_ui(__isl_take isl_aff *aff, unsigned f)
1544 isl_int_set_ui(v, f);
1545 aff = isl_aff_scale_down(aff, v);
1551 __isl_give isl_aff *isl_aff_set_dim_name(__isl_take isl_aff *aff,
1552 enum isl_dim_type type, unsigned pos, const char *s)
1554 aff = isl_aff_cow(aff);
1557 if (type == isl_dim_out)
1558 isl_die(aff->v->ctx, isl_error_invalid,
1559 "cannot set name of output/set dimension",
1560 return isl_aff_free(aff));
1561 if (type == isl_dim_in)
1563 aff->ls = isl_local_space_set_dim_name(aff->ls, type, pos, s);
1565 return isl_aff_free(aff);
1570 __isl_give isl_aff *isl_aff_set_dim_id(__isl_take isl_aff *aff,
1571 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
1573 aff = isl_aff_cow(aff);
1575 return isl_id_free(id);
1576 if (type == isl_dim_out)
1577 isl_die(aff->v->ctx, isl_error_invalid,
1578 "cannot set name of output/set dimension",
1580 if (type == isl_dim_in)
1582 aff->ls = isl_local_space_set_dim_id(aff->ls, type, pos, id);
1584 return isl_aff_free(aff);
1593 /* Exploit the equalities in "eq" to simplify the affine expression
1594 * and the expressions of the integer divisions in the local space.
1595 * The integer divisions in this local space are assumed to appear
1596 * as regular dimensions in "eq".
1598 static __isl_give isl_aff *isl_aff_substitute_equalities_lifted(
1599 __isl_take isl_aff *aff, __isl_take isl_basic_set *eq)
1607 if (eq->n_eq == 0) {
1608 isl_basic_set_free(eq);
1612 aff = isl_aff_cow(aff);
1616 aff->ls = isl_local_space_substitute_equalities(aff->ls,
1617 isl_basic_set_copy(eq));
1618 aff->v = isl_vec_cow(aff->v);
1619 if (!aff->ls || !aff->v)
1622 total = 1 + isl_space_dim(eq->dim, isl_dim_all);
1624 for (i = 0; i < eq->n_eq; ++i) {
1625 j = isl_seq_last_non_zero(eq->eq[i], total + n_div);
1626 if (j < 0 || j == 0 || j >= total)
1629 isl_seq_elim(aff->v->el + 1, eq->eq[i], j, total,
1633 isl_basic_set_free(eq);
1634 aff = isl_aff_normalize(aff);
1637 isl_basic_set_free(eq);
1642 /* Exploit the equalities in "eq" to simplify the affine expression
1643 * and the expressions of the integer divisions in the local space.
1645 static __isl_give isl_aff *isl_aff_substitute_equalities(
1646 __isl_take isl_aff *aff, __isl_take isl_basic_set *eq)
1652 n_div = isl_local_space_dim(aff->ls, isl_dim_div);
1654 eq = isl_basic_set_add_dims(eq, isl_dim_set, n_div);
1655 return isl_aff_substitute_equalities_lifted(aff, eq);
1657 isl_basic_set_free(eq);
1662 /* Look for equalities among the variables shared by context and aff
1663 * and the integer divisions of aff, if any.
1664 * The equalities are then used to eliminate coefficients and/or integer
1665 * divisions from aff.
1667 __isl_give isl_aff *isl_aff_gist(__isl_take isl_aff *aff,
1668 __isl_take isl_set *context)
1670 isl_basic_set *hull;
1675 n_div = isl_local_space_dim(aff->ls, isl_dim_div);
1677 isl_basic_set *bset;
1678 isl_local_space *ls;
1679 context = isl_set_add_dims(context, isl_dim_set, n_div);
1680 ls = isl_aff_get_domain_local_space(aff);
1681 bset = isl_basic_set_from_local_space(ls);
1682 bset = isl_basic_set_lift(bset);
1683 bset = isl_basic_set_flatten(bset);
1684 context = isl_set_intersect(context,
1685 isl_set_from_basic_set(bset));
1688 hull = isl_set_affine_hull(context);
1689 return isl_aff_substitute_equalities_lifted(aff, hull);
1692 isl_set_free(context);
1696 __isl_give isl_aff *isl_aff_gist_params(__isl_take isl_aff *aff,
1697 __isl_take isl_set *context)
1699 isl_set *dom_context = isl_set_universe(isl_aff_get_domain_space(aff));
1700 dom_context = isl_set_intersect_params(dom_context, context);
1701 return isl_aff_gist(aff, dom_context);
1704 /* Return a basic set containing those elements in the space
1705 * of aff where it is non-negative.
1706 * If "rational" is set, then return a rational basic set.
1708 static __isl_give isl_basic_set *aff_nonneg_basic_set(
1709 __isl_take isl_aff *aff, int rational)
1711 isl_constraint *ineq;
1712 isl_basic_set *bset;
1714 ineq = isl_inequality_from_aff(aff);
1716 bset = isl_basic_set_from_constraint(ineq);
1718 bset = isl_basic_set_set_rational(bset);
1719 bset = isl_basic_set_simplify(bset);
1723 /* Return a basic set containing those elements in the space
1724 * of aff where it is non-negative.
1726 __isl_give isl_basic_set *isl_aff_nonneg_basic_set(__isl_take isl_aff *aff)
1728 return aff_nonneg_basic_set(aff, 0);
1731 /* Return a basic set containing those elements in the domain space
1732 * of aff where it is negative.
1734 __isl_give isl_basic_set *isl_aff_neg_basic_set(__isl_take isl_aff *aff)
1736 aff = isl_aff_neg(aff);
1737 aff = isl_aff_add_constant_num_si(aff, -1);
1738 return isl_aff_nonneg_basic_set(aff);
1741 /* Return a basic set containing those elements in the space
1742 * of aff where it is zero.
1743 * If "rational" is set, then return a rational basic set.
1745 static __isl_give isl_basic_set *aff_zero_basic_set(__isl_take isl_aff *aff,
1748 isl_constraint *ineq;
1749 isl_basic_set *bset;
1751 ineq = isl_equality_from_aff(aff);
1753 bset = isl_basic_set_from_constraint(ineq);
1755 bset = isl_basic_set_set_rational(bset);
1756 bset = isl_basic_set_simplify(bset);
1760 /* Return a basic set containing those elements in the space
1761 * of aff where it is zero.
1763 __isl_give isl_basic_set *isl_aff_zero_basic_set(__isl_take isl_aff *aff)
1765 return aff_zero_basic_set(aff, 0);
1768 /* Return a basic set containing those elements in the shared space
1769 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
1771 __isl_give isl_basic_set *isl_aff_ge_basic_set(__isl_take isl_aff *aff1,
1772 __isl_take isl_aff *aff2)
1774 aff1 = isl_aff_sub(aff1, aff2);
1776 return isl_aff_nonneg_basic_set(aff1);
1779 /* Return a basic set containing those elements in the shared space
1780 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
1782 __isl_give isl_basic_set *isl_aff_le_basic_set(__isl_take isl_aff *aff1,
1783 __isl_take isl_aff *aff2)
1785 return isl_aff_ge_basic_set(aff2, aff1);
1788 __isl_give isl_aff *isl_aff_add_on_domain(__isl_keep isl_set *dom,
1789 __isl_take isl_aff *aff1, __isl_take isl_aff *aff2)
1791 aff1 = isl_aff_add(aff1, aff2);
1792 aff1 = isl_aff_gist(aff1, isl_set_copy(dom));
1796 int isl_aff_is_empty(__isl_keep isl_aff *aff)
1804 /* Check whether the given affine expression has non-zero coefficient
1805 * for any dimension in the given range or if any of these dimensions
1806 * appear with non-zero coefficients in any of the integer divisions
1807 * involved in the affine expression.
1809 int isl_aff_involves_dims(__isl_keep isl_aff *aff,
1810 enum isl_dim_type type, unsigned first, unsigned n)
1822 ctx = isl_aff_get_ctx(aff);
1823 if (first + n > isl_aff_dim(aff, type))
1824 isl_die(ctx, isl_error_invalid,
1825 "range out of bounds", return -1);
1827 active = isl_local_space_get_active(aff->ls, aff->v->el + 2);
1831 first += isl_local_space_offset(aff->ls, type) - 1;
1832 for (i = 0; i < n; ++i)
1833 if (active[first + i]) {
1846 __isl_give isl_aff *isl_aff_drop_dims(__isl_take isl_aff *aff,
1847 enum isl_dim_type type, unsigned first, unsigned n)
1853 if (type == isl_dim_out)
1854 isl_die(aff->v->ctx, isl_error_invalid,
1855 "cannot drop output/set dimension",
1856 return isl_aff_free(aff));
1857 if (type == isl_dim_in)
1859 if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
1862 ctx = isl_aff_get_ctx(aff);
1863 if (first + n > isl_local_space_dim(aff->ls, type))
1864 isl_die(ctx, isl_error_invalid, "range out of bounds",
1865 return isl_aff_free(aff));
1867 aff = isl_aff_cow(aff);
1871 aff->ls = isl_local_space_drop_dims(aff->ls, type, first, n);
1873 return isl_aff_free(aff);
1875 first += 1 + isl_local_space_offset(aff->ls, type);
1876 aff->v = isl_vec_drop_els(aff->v, first, n);
1878 return isl_aff_free(aff);
1883 /* Project the domain of the affine expression onto its parameter space.
1884 * The affine expression may not involve any of the domain dimensions.
1886 __isl_give isl_aff *isl_aff_project_domain_on_params(__isl_take isl_aff *aff)
1892 n = isl_aff_dim(aff, isl_dim_in);
1893 involves = isl_aff_involves_dims(aff, isl_dim_in, 0, n);
1895 return isl_aff_free(aff);
1897 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1898 "affine expression involves some of the domain dimensions",
1899 return isl_aff_free(aff));
1900 aff = isl_aff_drop_dims(aff, isl_dim_in, 0, n);
1901 space = isl_aff_get_domain_space(aff);
1902 space = isl_space_params(space);
1903 aff = isl_aff_reset_domain_space(aff, space);
1907 __isl_give isl_aff *isl_aff_insert_dims(__isl_take isl_aff *aff,
1908 enum isl_dim_type type, unsigned first, unsigned n)
1914 if (type == isl_dim_out)
1915 isl_die(aff->v->ctx, isl_error_invalid,
1916 "cannot insert output/set dimensions",
1917 return isl_aff_free(aff));
1918 if (type == isl_dim_in)
1920 if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
1923 ctx = isl_aff_get_ctx(aff);
1924 if (first > isl_local_space_dim(aff->ls, type))
1925 isl_die(ctx, isl_error_invalid, "position out of bounds",
1926 return isl_aff_free(aff));
1928 aff = isl_aff_cow(aff);
1932 aff->ls = isl_local_space_insert_dims(aff->ls, type, first, n);
1934 return isl_aff_free(aff);
1936 first += 1 + isl_local_space_offset(aff->ls, type);
1937 aff->v = isl_vec_insert_zero_els(aff->v, first, n);
1939 return isl_aff_free(aff);
1944 __isl_give isl_aff *isl_aff_add_dims(__isl_take isl_aff *aff,
1945 enum isl_dim_type type, unsigned n)
1949 pos = isl_aff_dim(aff, type);
1951 return isl_aff_insert_dims(aff, type, pos, n);
1954 __isl_give isl_pw_aff *isl_pw_aff_add_dims(__isl_take isl_pw_aff *pwaff,
1955 enum isl_dim_type type, unsigned n)
1959 pos = isl_pw_aff_dim(pwaff, type);
1961 return isl_pw_aff_insert_dims(pwaff, type, pos, n);
1964 __isl_give isl_pw_aff *isl_pw_aff_from_aff(__isl_take isl_aff *aff)
1966 isl_set *dom = isl_set_universe(isl_aff_get_domain_space(aff));
1967 return isl_pw_aff_alloc(dom, aff);
1971 #define PW isl_pw_aff
1975 #define EL_IS_ZERO is_empty
1979 #define IS_ZERO is_empty
1982 #undef DEFAULT_IS_ZERO
1983 #define DEFAULT_IS_ZERO 0
1987 #define NO_MOVE_DIMS
1991 #include <isl_pw_templ.c>
1993 static __isl_give isl_set *align_params_pw_pw_set_and(
1994 __isl_take isl_pw_aff *pwaff1, __isl_take isl_pw_aff *pwaff2,
1995 __isl_give isl_set *(*fn)(__isl_take isl_pw_aff *pwaff1,
1996 __isl_take isl_pw_aff *pwaff2))
1998 if (!pwaff1 || !pwaff2)
2000 if (isl_space_match(pwaff1->dim, isl_dim_param,
2001 pwaff2->dim, isl_dim_param))
2002 return fn(pwaff1, pwaff2);
2003 if (!isl_space_has_named_params(pwaff1->dim) ||
2004 !isl_space_has_named_params(pwaff2->dim))
2005 isl_die(isl_pw_aff_get_ctx(pwaff1), isl_error_invalid,
2006 "unaligned unnamed parameters", goto error);
2007 pwaff1 = isl_pw_aff_align_params(pwaff1, isl_pw_aff_get_space(pwaff2));
2008 pwaff2 = isl_pw_aff_align_params(pwaff2, isl_pw_aff_get_space(pwaff1));
2009 return fn(pwaff1, pwaff2);
2011 isl_pw_aff_free(pwaff1);
2012 isl_pw_aff_free(pwaff2);
2016 /* Compute a piecewise quasi-affine expression with a domain that
2017 * is the union of those of pwaff1 and pwaff2 and such that on each
2018 * cell, the quasi-affine expression is the better (according to cmp)
2019 * of those of pwaff1 and pwaff2. If only one of pwaff1 or pwaff2
2020 * is defined on a given cell, then the associated expression
2021 * is the defined one.
2023 static __isl_give isl_pw_aff *pw_aff_union_opt(__isl_take isl_pw_aff *pwaff1,
2024 __isl_take isl_pw_aff *pwaff2,
2025 __isl_give isl_basic_set *(*cmp)(__isl_take isl_aff *aff1,
2026 __isl_take isl_aff *aff2))
2033 if (!pwaff1 || !pwaff2)
2036 ctx = isl_space_get_ctx(pwaff1->dim);
2037 if (!isl_space_is_equal(pwaff1->dim, pwaff2->dim))
2038 isl_die(ctx, isl_error_invalid,
2039 "arguments should live in same space", goto error);
2041 if (isl_pw_aff_is_empty(pwaff1)) {
2042 isl_pw_aff_free(pwaff1);
2046 if (isl_pw_aff_is_empty(pwaff2)) {
2047 isl_pw_aff_free(pwaff2);
2051 n = 2 * (pwaff1->n + 1) * (pwaff2->n + 1);
2052 res = isl_pw_aff_alloc_size(isl_space_copy(pwaff1->dim), n);
2054 for (i = 0; i < pwaff1->n; ++i) {
2055 set = isl_set_copy(pwaff1->p[i].set);
2056 for (j = 0; j < pwaff2->n; ++j) {
2057 struct isl_set *common;
2060 common = isl_set_intersect(
2061 isl_set_copy(pwaff1->p[i].set),
2062 isl_set_copy(pwaff2->p[j].set));
2063 better = isl_set_from_basic_set(cmp(
2064 isl_aff_copy(pwaff2->p[j].aff),
2065 isl_aff_copy(pwaff1->p[i].aff)));
2066 better = isl_set_intersect(common, better);
2067 if (isl_set_plain_is_empty(better)) {
2068 isl_set_free(better);
2071 set = isl_set_subtract(set, isl_set_copy(better));
2073 res = isl_pw_aff_add_piece(res, better,
2074 isl_aff_copy(pwaff2->p[j].aff));
2076 res = isl_pw_aff_add_piece(res, set,
2077 isl_aff_copy(pwaff1->p[i].aff));
2080 for (j = 0; j < pwaff2->n; ++j) {
2081 set = isl_set_copy(pwaff2->p[j].set);
2082 for (i = 0; i < pwaff1->n; ++i)
2083 set = isl_set_subtract(set,
2084 isl_set_copy(pwaff1->p[i].set));
2085 res = isl_pw_aff_add_piece(res, set,
2086 isl_aff_copy(pwaff2->p[j].aff));
2089 isl_pw_aff_free(pwaff1);
2090 isl_pw_aff_free(pwaff2);
2094 isl_pw_aff_free(pwaff1);
2095 isl_pw_aff_free(pwaff2);
2099 /* Compute a piecewise quasi-affine expression with a domain that
2100 * is the union of those of pwaff1 and pwaff2 and such that on each
2101 * cell, the quasi-affine expression is the maximum of those of pwaff1
2102 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2103 * cell, then the associated expression is the defined one.
2105 static __isl_give isl_pw_aff *pw_aff_union_max(__isl_take isl_pw_aff *pwaff1,
2106 __isl_take isl_pw_aff *pwaff2)
2108 return pw_aff_union_opt(pwaff1, pwaff2, &isl_aff_ge_basic_set);
2111 __isl_give isl_pw_aff *isl_pw_aff_union_max(__isl_take isl_pw_aff *pwaff1,
2112 __isl_take isl_pw_aff *pwaff2)
2114 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2,
2118 /* Compute a piecewise quasi-affine expression with a domain that
2119 * is the union of those of pwaff1 and pwaff2 and such that on each
2120 * cell, the quasi-affine expression is the minimum of those of pwaff1
2121 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2122 * cell, then the associated expression is the defined one.
2124 static __isl_give isl_pw_aff *pw_aff_union_min(__isl_take isl_pw_aff *pwaff1,
2125 __isl_take isl_pw_aff *pwaff2)
2127 return pw_aff_union_opt(pwaff1, pwaff2, &isl_aff_le_basic_set);
2130 __isl_give isl_pw_aff *isl_pw_aff_union_min(__isl_take isl_pw_aff *pwaff1,
2131 __isl_take isl_pw_aff *pwaff2)
2133 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2,
2137 __isl_give isl_pw_aff *isl_pw_aff_union_opt(__isl_take isl_pw_aff *pwaff1,
2138 __isl_take isl_pw_aff *pwaff2, int max)
2141 return isl_pw_aff_union_max(pwaff1, pwaff2);
2143 return isl_pw_aff_union_min(pwaff1, pwaff2);
2146 /* Construct a map with as domain the domain of pwaff and
2147 * one-dimensional range corresponding to the affine expressions.
2149 static __isl_give isl_map *map_from_pw_aff(__isl_take isl_pw_aff *pwaff)
2158 dim = isl_pw_aff_get_space(pwaff);
2159 map = isl_map_empty(dim);
2161 for (i = 0; i < pwaff->n; ++i) {
2162 isl_basic_map *bmap;
2165 bmap = isl_basic_map_from_aff(isl_aff_copy(pwaff->p[i].aff));
2166 map_i = isl_map_from_basic_map(bmap);
2167 map_i = isl_map_intersect_domain(map_i,
2168 isl_set_copy(pwaff->p[i].set));
2169 map = isl_map_union_disjoint(map, map_i);
2172 isl_pw_aff_free(pwaff);
2177 /* Construct a map with as domain the domain of pwaff and
2178 * one-dimensional range corresponding to the affine expressions.
2180 __isl_give isl_map *isl_map_from_pw_aff(__isl_take isl_pw_aff *pwaff)
2184 if (isl_space_is_set(pwaff->dim))
2185 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
2186 "space of input is not a map",
2187 return isl_pw_aff_free(pwaff));
2188 return map_from_pw_aff(pwaff);
2191 /* Construct a one-dimensional set with as parameter domain
2192 * the domain of pwaff and the single set dimension
2193 * corresponding to the affine expressions.
2195 __isl_give isl_set *isl_set_from_pw_aff(__isl_take isl_pw_aff *pwaff)
2199 if (!isl_space_is_set(pwaff->dim))
2200 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
2201 "space of input is not a set",
2202 return isl_pw_aff_free(pwaff));
2203 return map_from_pw_aff(pwaff);
2206 /* Return a set containing those elements in the domain
2207 * of pwaff where it is non-negative.
2209 __isl_give isl_set *isl_pw_aff_nonneg_set(__isl_take isl_pw_aff *pwaff)
2217 set = isl_set_empty(isl_pw_aff_get_domain_space(pwaff));
2219 for (i = 0; i < pwaff->n; ++i) {
2220 isl_basic_set *bset;
2224 rational = isl_set_has_rational(pwaff->p[i].set);
2225 bset = aff_nonneg_basic_set(isl_aff_copy(pwaff->p[i].aff),
2227 set_i = isl_set_from_basic_set(bset);
2228 set_i = isl_set_intersect(set_i, isl_set_copy(pwaff->p[i].set));
2229 set = isl_set_union_disjoint(set, set_i);
2232 isl_pw_aff_free(pwaff);
2237 /* Return a set containing those elements in the domain
2238 * of pwaff where it is zero (if complement is 0) or not zero
2239 * (if complement is 1).
2241 static __isl_give isl_set *pw_aff_zero_set(__isl_take isl_pw_aff *pwaff,
2250 set = isl_set_empty(isl_pw_aff_get_domain_space(pwaff));
2252 for (i = 0; i < pwaff->n; ++i) {
2253 isl_basic_set *bset;
2254 isl_set *set_i, *zero;
2257 rational = isl_set_has_rational(pwaff->p[i].set);
2258 bset = aff_zero_basic_set(isl_aff_copy(pwaff->p[i].aff),
2260 zero = isl_set_from_basic_set(bset);
2261 set_i = isl_set_copy(pwaff->p[i].set);
2263 set_i = isl_set_subtract(set_i, zero);
2265 set_i = isl_set_intersect(set_i, zero);
2266 set = isl_set_union_disjoint(set, set_i);
2269 isl_pw_aff_free(pwaff);
2274 /* Return a set containing those elements in the domain
2275 * of pwaff where it is zero.
2277 __isl_give isl_set *isl_pw_aff_zero_set(__isl_take isl_pw_aff *pwaff)
2279 return pw_aff_zero_set(pwaff, 0);
2282 /* Return a set containing those elements in the domain
2283 * of pwaff where it is not zero.
2285 __isl_give isl_set *isl_pw_aff_non_zero_set(__isl_take isl_pw_aff *pwaff)
2287 return pw_aff_zero_set(pwaff, 1);
2290 /* Return a set containing those elements in the shared domain
2291 * of pwaff1 and pwaff2 where pwaff1 is greater than (or equal) to pwaff2.
2293 * We compute the difference on the shared domain and then construct
2294 * the set of values where this difference is non-negative.
2295 * If strict is set, we first subtract 1 from the difference.
2296 * If equal is set, we only return the elements where pwaff1 and pwaff2
2299 static __isl_give isl_set *pw_aff_gte_set(__isl_take isl_pw_aff *pwaff1,
2300 __isl_take isl_pw_aff *pwaff2, int strict, int equal)
2302 isl_set *set1, *set2;
2304 set1 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff1));
2305 set2 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff2));
2306 set1 = isl_set_intersect(set1, set2);
2307 pwaff1 = isl_pw_aff_intersect_domain(pwaff1, isl_set_copy(set1));
2308 pwaff2 = isl_pw_aff_intersect_domain(pwaff2, isl_set_copy(set1));
2309 pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_neg(pwaff2));
2312 isl_space *dim = isl_set_get_space(set1);
2314 aff = isl_aff_zero_on_domain(isl_local_space_from_space(dim));
2315 aff = isl_aff_add_constant_si(aff, -1);
2316 pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_alloc(set1, aff));
2321 return isl_pw_aff_zero_set(pwaff1);
2322 return isl_pw_aff_nonneg_set(pwaff1);
2325 /* Return a set containing those elements in the shared domain
2326 * of pwaff1 and pwaff2 where pwaff1 is equal to pwaff2.
2328 static __isl_give isl_set *pw_aff_eq_set(__isl_take isl_pw_aff *pwaff1,
2329 __isl_take isl_pw_aff *pwaff2)
2331 return pw_aff_gte_set(pwaff1, pwaff2, 0, 1);
2334 __isl_give isl_set *isl_pw_aff_eq_set(__isl_take isl_pw_aff *pwaff1,
2335 __isl_take isl_pw_aff *pwaff2)
2337 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_eq_set);
2340 /* Return a set containing those elements in the shared domain
2341 * of pwaff1 and pwaff2 where pwaff1 is greater than or equal to pwaff2.
2343 static __isl_give isl_set *pw_aff_ge_set(__isl_take isl_pw_aff *pwaff1,
2344 __isl_take isl_pw_aff *pwaff2)
2346 return pw_aff_gte_set(pwaff1, pwaff2, 0, 0);
2349 __isl_give isl_set *isl_pw_aff_ge_set(__isl_take isl_pw_aff *pwaff1,
2350 __isl_take isl_pw_aff *pwaff2)
2352 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_ge_set);
2355 /* Return a set containing those elements in the shared domain
2356 * of pwaff1 and pwaff2 where pwaff1 is strictly greater than pwaff2.
2358 static __isl_give isl_set *pw_aff_gt_set(__isl_take isl_pw_aff *pwaff1,
2359 __isl_take isl_pw_aff *pwaff2)
2361 return pw_aff_gte_set(pwaff1, pwaff2, 1, 0);
2364 __isl_give isl_set *isl_pw_aff_gt_set(__isl_take isl_pw_aff *pwaff1,
2365 __isl_take isl_pw_aff *pwaff2)
2367 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_gt_set);
2370 __isl_give isl_set *isl_pw_aff_le_set(__isl_take isl_pw_aff *pwaff1,
2371 __isl_take isl_pw_aff *pwaff2)
2373 return isl_pw_aff_ge_set(pwaff2, pwaff1);
2376 __isl_give isl_set *isl_pw_aff_lt_set(__isl_take isl_pw_aff *pwaff1,
2377 __isl_take isl_pw_aff *pwaff2)
2379 return isl_pw_aff_gt_set(pwaff2, pwaff1);
2382 /* Return a set containing those elements in the shared domain
2383 * of the elements of list1 and list2 where each element in list1
2384 * has the relation specified by "fn" with each element in list2.
2386 static __isl_give isl_set *pw_aff_list_set(__isl_take isl_pw_aff_list *list1,
2387 __isl_take isl_pw_aff_list *list2,
2388 __isl_give isl_set *(*fn)(__isl_take isl_pw_aff *pwaff1,
2389 __isl_take isl_pw_aff *pwaff2))
2395 if (!list1 || !list2)
2398 ctx = isl_pw_aff_list_get_ctx(list1);
2399 if (list1->n < 1 || list2->n < 1)
2400 isl_die(ctx, isl_error_invalid,
2401 "list should contain at least one element", goto error);
2403 set = isl_set_universe(isl_pw_aff_get_domain_space(list1->p[0]));
2404 for (i = 0; i < list1->n; ++i)
2405 for (j = 0; j < list2->n; ++j) {
2408 set_ij = fn(isl_pw_aff_copy(list1->p[i]),
2409 isl_pw_aff_copy(list2->p[j]));
2410 set = isl_set_intersect(set, set_ij);
2413 isl_pw_aff_list_free(list1);
2414 isl_pw_aff_list_free(list2);
2417 isl_pw_aff_list_free(list1);
2418 isl_pw_aff_list_free(list2);
2422 /* Return a set containing those elements in the shared domain
2423 * of the elements of list1 and list2 where each element in list1
2424 * is equal to each element in list2.
2426 __isl_give isl_set *isl_pw_aff_list_eq_set(__isl_take isl_pw_aff_list *list1,
2427 __isl_take isl_pw_aff_list *list2)
2429 return pw_aff_list_set(list1, list2, &isl_pw_aff_eq_set);
2432 __isl_give isl_set *isl_pw_aff_list_ne_set(__isl_take isl_pw_aff_list *list1,
2433 __isl_take isl_pw_aff_list *list2)
2435 return pw_aff_list_set(list1, list2, &isl_pw_aff_ne_set);
2438 /* Return a set containing those elements in the shared domain
2439 * of the elements of list1 and list2 where each element in list1
2440 * is less than or equal to each element in list2.
2442 __isl_give isl_set *isl_pw_aff_list_le_set(__isl_take isl_pw_aff_list *list1,
2443 __isl_take isl_pw_aff_list *list2)
2445 return pw_aff_list_set(list1, list2, &isl_pw_aff_le_set);
2448 __isl_give isl_set *isl_pw_aff_list_lt_set(__isl_take isl_pw_aff_list *list1,
2449 __isl_take isl_pw_aff_list *list2)
2451 return pw_aff_list_set(list1, list2, &isl_pw_aff_lt_set);
2454 __isl_give isl_set *isl_pw_aff_list_ge_set(__isl_take isl_pw_aff_list *list1,
2455 __isl_take isl_pw_aff_list *list2)
2457 return pw_aff_list_set(list1, list2, &isl_pw_aff_ge_set);
2460 __isl_give isl_set *isl_pw_aff_list_gt_set(__isl_take isl_pw_aff_list *list1,
2461 __isl_take isl_pw_aff_list *list2)
2463 return pw_aff_list_set(list1, list2, &isl_pw_aff_gt_set);
2467 /* Return a set containing those elements in the shared domain
2468 * of pwaff1 and pwaff2 where pwaff1 is not equal to pwaff2.
2470 static __isl_give isl_set *pw_aff_ne_set(__isl_take isl_pw_aff *pwaff1,
2471 __isl_take isl_pw_aff *pwaff2)
2473 isl_set *set_lt, *set_gt;
2475 set_lt = isl_pw_aff_lt_set(isl_pw_aff_copy(pwaff1),
2476 isl_pw_aff_copy(pwaff2));
2477 set_gt = isl_pw_aff_gt_set(pwaff1, pwaff2);
2478 return isl_set_union_disjoint(set_lt, set_gt);
2481 __isl_give isl_set *isl_pw_aff_ne_set(__isl_take isl_pw_aff *pwaff1,
2482 __isl_take isl_pw_aff *pwaff2)
2484 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_ne_set);
2487 __isl_give isl_pw_aff *isl_pw_aff_scale_down(__isl_take isl_pw_aff *pwaff,
2492 if (isl_int_is_one(v))
2494 if (!isl_int_is_pos(v))
2495 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
2496 "factor needs to be positive",
2497 return isl_pw_aff_free(pwaff));
2498 pwaff = isl_pw_aff_cow(pwaff);
2504 for (i = 0; i < pwaff->n; ++i) {
2505 pwaff->p[i].aff = isl_aff_scale_down(pwaff->p[i].aff, v);
2506 if (!pwaff->p[i].aff)
2507 return isl_pw_aff_free(pwaff);
2513 __isl_give isl_pw_aff *isl_pw_aff_floor(__isl_take isl_pw_aff *pwaff)
2517 pwaff = isl_pw_aff_cow(pwaff);
2523 for (i = 0; i < pwaff->n; ++i) {
2524 pwaff->p[i].aff = isl_aff_floor(pwaff->p[i].aff);
2525 if (!pwaff->p[i].aff)
2526 return isl_pw_aff_free(pwaff);
2532 __isl_give isl_pw_aff *isl_pw_aff_ceil(__isl_take isl_pw_aff *pwaff)
2536 pwaff = isl_pw_aff_cow(pwaff);
2542 for (i = 0; i < pwaff->n; ++i) {
2543 pwaff->p[i].aff = isl_aff_ceil(pwaff->p[i].aff);
2544 if (!pwaff->p[i].aff)
2545 return isl_pw_aff_free(pwaff);
2551 /* Assuming that "cond1" and "cond2" are disjoint,
2552 * return an affine expression that is equal to pwaff1 on cond1
2553 * and to pwaff2 on cond2.
2555 static __isl_give isl_pw_aff *isl_pw_aff_select(
2556 __isl_take isl_set *cond1, __isl_take isl_pw_aff *pwaff1,
2557 __isl_take isl_set *cond2, __isl_take isl_pw_aff *pwaff2)
2559 pwaff1 = isl_pw_aff_intersect_domain(pwaff1, cond1);
2560 pwaff2 = isl_pw_aff_intersect_domain(pwaff2, cond2);
2562 return isl_pw_aff_add_disjoint(pwaff1, pwaff2);
2565 /* Return an affine expression that is equal to pwaff_true for elements
2566 * where "cond" is non-zero and to pwaff_false for elements where "cond"
2568 * That is, return cond ? pwaff_true : pwaff_false;
2570 __isl_give isl_pw_aff *isl_pw_aff_cond(__isl_take isl_pw_aff *cond,
2571 __isl_take isl_pw_aff *pwaff_true, __isl_take isl_pw_aff *pwaff_false)
2573 isl_set *cond_true, *cond_false;
2575 cond_true = isl_pw_aff_non_zero_set(isl_pw_aff_copy(cond));
2576 cond_false = isl_pw_aff_zero_set(cond);
2577 return isl_pw_aff_select(cond_true, pwaff_true,
2578 cond_false, pwaff_false);
2581 int isl_aff_is_cst(__isl_keep isl_aff *aff)
2586 return isl_seq_first_non_zero(aff->v->el + 2, aff->v->size - 2) == -1;
2589 /* Check whether pwaff is a piecewise constant.
2591 int isl_pw_aff_is_cst(__isl_keep isl_pw_aff *pwaff)
2598 for (i = 0; i < pwaff->n; ++i) {
2599 int is_cst = isl_aff_is_cst(pwaff->p[i].aff);
2600 if (is_cst < 0 || !is_cst)
2607 __isl_give isl_aff *isl_aff_mul(__isl_take isl_aff *aff1,
2608 __isl_take isl_aff *aff2)
2610 if (!isl_aff_is_cst(aff2) && isl_aff_is_cst(aff1))
2611 return isl_aff_mul(aff2, aff1);
2613 if (!isl_aff_is_cst(aff2))
2614 isl_die(isl_aff_get_ctx(aff1), isl_error_invalid,
2615 "at least one affine expression should be constant",
2618 aff1 = isl_aff_cow(aff1);
2622 aff1 = isl_aff_scale(aff1, aff2->v->el[1]);
2623 aff1 = isl_aff_scale_down(aff1, aff2->v->el[0]);
2633 /* Divide "aff1" by "aff2", assuming "aff2" is a piecewise constant.
2635 __isl_give isl_aff *isl_aff_div(__isl_take isl_aff *aff1,
2636 __isl_take isl_aff *aff2)
2641 is_cst = isl_aff_is_cst(aff2);
2645 isl_die(isl_aff_get_ctx(aff2), isl_error_invalid,
2646 "second argument should be a constant", goto error);
2651 neg = isl_int_is_neg(aff2->v->el[1]);
2653 isl_int_neg(aff2->v->el[0], aff2->v->el[0]);
2654 isl_int_neg(aff2->v->el[1], aff2->v->el[1]);
2657 aff1 = isl_aff_scale(aff1, aff2->v->el[0]);
2658 aff1 = isl_aff_scale_down(aff1, aff2->v->el[1]);
2661 isl_int_neg(aff2->v->el[0], aff2->v->el[0]);
2662 isl_int_neg(aff2->v->el[1], aff2->v->el[1]);
2673 static __isl_give isl_pw_aff *pw_aff_add(__isl_take isl_pw_aff *pwaff1,
2674 __isl_take isl_pw_aff *pwaff2)
2676 return isl_pw_aff_on_shared_domain(pwaff1, pwaff2, &isl_aff_add);
2679 __isl_give isl_pw_aff *isl_pw_aff_add(__isl_take isl_pw_aff *pwaff1,
2680 __isl_take isl_pw_aff *pwaff2)
2682 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_add);
2685 __isl_give isl_pw_aff *isl_pw_aff_union_add(__isl_take isl_pw_aff *pwaff1,
2686 __isl_take isl_pw_aff *pwaff2)
2688 return isl_pw_aff_union_add_(pwaff1, pwaff2);
2691 static __isl_give isl_pw_aff *pw_aff_mul(__isl_take isl_pw_aff *pwaff1,
2692 __isl_take isl_pw_aff *pwaff2)
2694 return isl_pw_aff_on_shared_domain(pwaff1, pwaff2, &isl_aff_mul);
2697 __isl_give isl_pw_aff *isl_pw_aff_mul(__isl_take isl_pw_aff *pwaff1,
2698 __isl_take isl_pw_aff *pwaff2)
2700 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_mul);
2703 static __isl_give isl_pw_aff *pw_aff_div(__isl_take isl_pw_aff *pa1,
2704 __isl_take isl_pw_aff *pa2)
2706 return isl_pw_aff_on_shared_domain(pa1, pa2, &isl_aff_div);
2709 /* Divide "pa1" by "pa2", assuming "pa2" is a piecewise constant.
2711 __isl_give isl_pw_aff *isl_pw_aff_div(__isl_take isl_pw_aff *pa1,
2712 __isl_take isl_pw_aff *pa2)
2716 is_cst = isl_pw_aff_is_cst(pa2);
2720 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
2721 "second argument should be a piecewise constant",
2723 return isl_pw_aff_align_params_pw_pw_and(pa1, pa2, &pw_aff_div);
2725 isl_pw_aff_free(pa1);
2726 isl_pw_aff_free(pa2);
2730 /* Compute the quotient of the integer division of "pa1" by "pa2"
2731 * with rounding towards zero.
2732 * "pa2" is assumed to be a piecewise constant.
2734 * In particular, return
2736 * pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2)
2739 __isl_give isl_pw_aff *isl_pw_aff_tdiv_q(__isl_take isl_pw_aff *pa1,
2740 __isl_take isl_pw_aff *pa2)
2746 is_cst = isl_pw_aff_is_cst(pa2);
2750 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
2751 "second argument should be a piecewise constant",
2754 pa1 = isl_pw_aff_div(pa1, pa2);
2756 cond = isl_pw_aff_nonneg_set(isl_pw_aff_copy(pa1));
2757 f = isl_pw_aff_floor(isl_pw_aff_copy(pa1));
2758 c = isl_pw_aff_ceil(pa1);
2759 return isl_pw_aff_cond(isl_set_indicator_function(cond), f, c);
2761 isl_pw_aff_free(pa1);
2762 isl_pw_aff_free(pa2);
2766 /* Compute the remainder of the integer division of "pa1" by "pa2"
2767 * with rounding towards zero.
2768 * "pa2" is assumed to be a piecewise constant.
2770 * In particular, return
2772 * pa1 - pa2 * (pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2))
2775 __isl_give isl_pw_aff *isl_pw_aff_tdiv_r(__isl_take isl_pw_aff *pa1,
2776 __isl_take isl_pw_aff *pa2)
2781 is_cst = isl_pw_aff_is_cst(pa2);
2785 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
2786 "second argument should be a piecewise constant",
2788 res = isl_pw_aff_tdiv_q(isl_pw_aff_copy(pa1), isl_pw_aff_copy(pa2));
2789 res = isl_pw_aff_mul(pa2, res);
2790 res = isl_pw_aff_sub(pa1, res);
2793 isl_pw_aff_free(pa1);
2794 isl_pw_aff_free(pa2);
2798 static __isl_give isl_pw_aff *pw_aff_min(__isl_take isl_pw_aff *pwaff1,
2799 __isl_take isl_pw_aff *pwaff2)
2804 dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1)),
2805 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2)));
2806 le = isl_pw_aff_le_set(isl_pw_aff_copy(pwaff1),
2807 isl_pw_aff_copy(pwaff2));
2808 dom = isl_set_subtract(dom, isl_set_copy(le));
2809 return isl_pw_aff_select(le, pwaff1, dom, pwaff2);
2812 __isl_give isl_pw_aff *isl_pw_aff_min(__isl_take isl_pw_aff *pwaff1,
2813 __isl_take isl_pw_aff *pwaff2)
2815 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_min);
2818 static __isl_give isl_pw_aff *pw_aff_max(__isl_take isl_pw_aff *pwaff1,
2819 __isl_take isl_pw_aff *pwaff2)
2824 dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1)),
2825 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2)));
2826 ge = isl_pw_aff_ge_set(isl_pw_aff_copy(pwaff1),
2827 isl_pw_aff_copy(pwaff2));
2828 dom = isl_set_subtract(dom, isl_set_copy(ge));
2829 return isl_pw_aff_select(ge, pwaff1, dom, pwaff2);
2832 __isl_give isl_pw_aff *isl_pw_aff_max(__isl_take isl_pw_aff *pwaff1,
2833 __isl_take isl_pw_aff *pwaff2)
2835 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_max);
2838 static __isl_give isl_pw_aff *pw_aff_list_reduce(
2839 __isl_take isl_pw_aff_list *list,
2840 __isl_give isl_pw_aff *(*fn)(__isl_take isl_pw_aff *pwaff1,
2841 __isl_take isl_pw_aff *pwaff2))
2850 ctx = isl_pw_aff_list_get_ctx(list);
2852 isl_die(ctx, isl_error_invalid,
2853 "list should contain at least one element",
2854 return isl_pw_aff_list_free(list));
2856 res = isl_pw_aff_copy(list->p[0]);
2857 for (i = 1; i < list->n; ++i)
2858 res = fn(res, isl_pw_aff_copy(list->p[i]));
2860 isl_pw_aff_list_free(list);
2864 /* Return an isl_pw_aff that maps each element in the intersection of the
2865 * domains of the elements of list to the minimal corresponding affine
2868 __isl_give isl_pw_aff *isl_pw_aff_list_min(__isl_take isl_pw_aff_list *list)
2870 return pw_aff_list_reduce(list, &isl_pw_aff_min);
2873 /* Return an isl_pw_aff that maps each element in the intersection of the
2874 * domains of the elements of list to the maximal corresponding affine
2877 __isl_give isl_pw_aff *isl_pw_aff_list_max(__isl_take isl_pw_aff_list *list)
2879 return pw_aff_list_reduce(list, &isl_pw_aff_max);
2882 /* Mark the domains of "pwaff" as rational.
2884 __isl_give isl_pw_aff *isl_pw_aff_set_rational(__isl_take isl_pw_aff *pwaff)
2888 pwaff = isl_pw_aff_cow(pwaff);
2894 for (i = 0; i < pwaff->n; ++i) {
2895 pwaff->p[i].set = isl_set_set_rational(pwaff->p[i].set);
2896 if (!pwaff->p[i].set)
2897 return isl_pw_aff_free(pwaff);
2903 /* Mark the domains of the elements of "list" as rational.
2905 __isl_give isl_pw_aff_list *isl_pw_aff_list_set_rational(
2906 __isl_take isl_pw_aff_list *list)
2916 for (i = 0; i < n; ++i) {
2919 pa = isl_pw_aff_list_get_pw_aff(list, i);
2920 pa = isl_pw_aff_set_rational(pa);
2921 list = isl_pw_aff_list_set_pw_aff(list, i, pa);
2927 /* Check that the domain space of "aff" matches "space".
2929 * Return 0 on success and -1 on error.
2931 int isl_aff_check_match_domain_space(__isl_keep isl_aff *aff,
2932 __isl_keep isl_space *space)
2934 isl_space *aff_space;
2940 aff_space = isl_aff_get_domain_space(aff);
2942 match = isl_space_match(space, isl_dim_param, aff_space, isl_dim_param);
2946 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2947 "parameters don't match", goto error);
2948 match = isl_space_tuple_match(space, isl_dim_in,
2949 aff_space, isl_dim_set);
2953 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2954 "domains don't match", goto error);
2955 isl_space_free(aff_space);
2958 isl_space_free(aff_space);
2965 #include <isl_multi_templ.c>
2967 /* Create an isl_pw_multi_aff with the given isl_multi_aff on a universe
2970 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_multi_aff(
2971 __isl_take isl_multi_aff *ma)
2973 isl_set *dom = isl_set_universe(isl_multi_aff_get_domain_space(ma));
2974 return isl_pw_multi_aff_alloc(dom, ma);
2977 /* Create a piecewise multi-affine expression in the given space that maps each
2978 * input dimension to the corresponding output dimension.
2980 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_identity(
2981 __isl_take isl_space *space)
2983 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_identity(space));
2986 __isl_give isl_multi_aff *isl_multi_aff_add(__isl_take isl_multi_aff *maff1,
2987 __isl_take isl_multi_aff *maff2)
2989 return isl_multi_aff_bin_op(maff1, maff2, &isl_aff_add);
2992 /* Subtract "ma2" from "ma1" and return the result.
2994 __isl_give isl_multi_aff *isl_multi_aff_sub(__isl_take isl_multi_aff *ma1,
2995 __isl_take isl_multi_aff *ma2)
2997 return isl_multi_aff_bin_op(ma1, ma2, &isl_aff_sub);
3000 /* Given two multi-affine expressions A -> B and C -> D,
3001 * construct a multi-affine expression [A -> C] -> [B -> D].
3003 __isl_give isl_multi_aff *isl_multi_aff_product(
3004 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2)
3010 int in1, in2, out1, out2;
3012 in1 = isl_multi_aff_dim(ma1, isl_dim_in);
3013 in2 = isl_multi_aff_dim(ma2, isl_dim_in);
3014 out1 = isl_multi_aff_dim(ma1, isl_dim_out);
3015 out2 = isl_multi_aff_dim(ma2, isl_dim_out);
3016 space = isl_space_product(isl_multi_aff_get_space(ma1),
3017 isl_multi_aff_get_space(ma2));
3018 res = isl_multi_aff_alloc(isl_space_copy(space));
3019 space = isl_space_domain(space);
3021 for (i = 0; i < out1; ++i) {
3022 aff = isl_multi_aff_get_aff(ma1, i);
3023 aff = isl_aff_insert_dims(aff, isl_dim_in, in1, in2);
3024 aff = isl_aff_reset_domain_space(aff, isl_space_copy(space));
3025 res = isl_multi_aff_set_aff(res, i, aff);
3028 for (i = 0; i < out2; ++i) {
3029 aff = isl_multi_aff_get_aff(ma2, i);
3030 aff = isl_aff_insert_dims(aff, isl_dim_in, 0, in1);
3031 aff = isl_aff_reset_domain_space(aff, isl_space_copy(space));
3032 res = isl_multi_aff_set_aff(res, out1 + i, aff);
3035 isl_space_free(space);
3036 isl_multi_aff_free(ma1);
3037 isl_multi_aff_free(ma2);
3041 /* Exploit the equalities in "eq" to simplify the affine expressions.
3043 static __isl_give isl_multi_aff *isl_multi_aff_substitute_equalities(
3044 __isl_take isl_multi_aff *maff, __isl_take isl_basic_set *eq)
3048 maff = isl_multi_aff_cow(maff);
3052 for (i = 0; i < maff->n; ++i) {
3053 maff->p[i] = isl_aff_substitute_equalities(maff->p[i],
3054 isl_basic_set_copy(eq));
3059 isl_basic_set_free(eq);
3062 isl_basic_set_free(eq);
3063 isl_multi_aff_free(maff);
3067 __isl_give isl_multi_aff *isl_multi_aff_scale(__isl_take isl_multi_aff *maff,
3072 maff = isl_multi_aff_cow(maff);
3076 for (i = 0; i < maff->n; ++i) {
3077 maff->p[i] = isl_aff_scale(maff->p[i], f);
3079 return isl_multi_aff_free(maff);
3085 __isl_give isl_multi_aff *isl_multi_aff_add_on_domain(__isl_keep isl_set *dom,
3086 __isl_take isl_multi_aff *maff1, __isl_take isl_multi_aff *maff2)
3088 maff1 = isl_multi_aff_add(maff1, maff2);
3089 maff1 = isl_multi_aff_gist(maff1, isl_set_copy(dom));
3093 int isl_multi_aff_is_empty(__isl_keep isl_multi_aff *maff)
3101 int isl_multi_aff_plain_is_equal(__isl_keep isl_multi_aff *maff1,
3102 __isl_keep isl_multi_aff *maff2)
3107 if (!maff1 || !maff2)
3109 if (maff1->n != maff2->n)
3111 equal = isl_space_is_equal(maff1->space, maff2->space);
3112 if (equal < 0 || !equal)
3115 for (i = 0; i < maff1->n; ++i) {
3116 equal = isl_aff_plain_is_equal(maff1->p[i], maff2->p[i]);
3117 if (equal < 0 || !equal)
3124 /* Return the set of domain elements where "ma1" is lexicographically
3125 * smaller than or equal to "ma2".
3127 __isl_give isl_set *isl_multi_aff_lex_le_set(__isl_take isl_multi_aff *ma1,
3128 __isl_take isl_multi_aff *ma2)
3130 return isl_multi_aff_lex_ge_set(ma2, ma1);
3133 /* Return the set of domain elements where "ma1" is lexicographically
3134 * greater than or equal to "ma2".
3136 __isl_give isl_set *isl_multi_aff_lex_ge_set(__isl_take isl_multi_aff *ma1,
3137 __isl_take isl_multi_aff *ma2)
3140 isl_map *map1, *map2;
3143 map1 = isl_map_from_multi_aff(ma1);
3144 map2 = isl_map_from_multi_aff(ma2);
3145 map = isl_map_range_product(map1, map2);
3146 space = isl_space_range(isl_map_get_space(map));
3147 space = isl_space_domain(isl_space_unwrap(space));
3148 ge = isl_map_lex_ge(space);
3149 map = isl_map_intersect_range(map, isl_map_wrap(ge));
3151 return isl_map_domain(map);
3155 #define PW isl_pw_multi_aff
3157 #define EL isl_multi_aff
3159 #define EL_IS_ZERO is_empty
3163 #define IS_ZERO is_empty
3166 #undef DEFAULT_IS_ZERO
3167 #define DEFAULT_IS_ZERO 0
3172 #define NO_INVOLVES_DIMS
3173 #define NO_MOVE_DIMS
3174 #define NO_INSERT_DIMS
3178 #include <isl_pw_templ.c>
3181 #define UNION isl_union_pw_multi_aff
3183 #define PART isl_pw_multi_aff
3185 #define PARTS pw_multi_aff
3186 #define ALIGN_DOMAIN
3190 #include <isl_union_templ.c>
3192 /* Given a function "cmp" that returns the set of elements where
3193 * "ma1" is "better" than "ma2", return the intersection of this
3194 * set with "dom1" and "dom2".
3196 static __isl_give isl_set *shared_and_better(__isl_keep isl_set *dom1,
3197 __isl_keep isl_set *dom2, __isl_keep isl_multi_aff *ma1,
3198 __isl_keep isl_multi_aff *ma2,
3199 __isl_give isl_set *(*cmp)(__isl_take isl_multi_aff *ma1,
3200 __isl_take isl_multi_aff *ma2))
3206 common = isl_set_intersect(isl_set_copy(dom1), isl_set_copy(dom2));
3207 is_empty = isl_set_plain_is_empty(common);
3208 if (is_empty >= 0 && is_empty)
3211 return isl_set_free(common);
3212 better = cmp(isl_multi_aff_copy(ma1), isl_multi_aff_copy(ma2));
3213 better = isl_set_intersect(common, better);
3218 /* Given a function "cmp" that returns the set of elements where
3219 * "ma1" is "better" than "ma2", return a piecewise multi affine
3220 * expression defined on the union of the definition domains
3221 * of "pma1" and "pma2" that maps to the "best" of "pma1" and
3222 * "pma2" on each cell. If only one of the two input functions
3223 * is defined on a given cell, then it is considered the best.
3225 static __isl_give isl_pw_multi_aff *pw_multi_aff_union_opt(
3226 __isl_take isl_pw_multi_aff *pma1,
3227 __isl_take isl_pw_multi_aff *pma2,
3228 __isl_give isl_set *(*cmp)(__isl_take isl_multi_aff *ma1,
3229 __isl_take isl_multi_aff *ma2))
3232 isl_pw_multi_aff *res = NULL;
3234 isl_set *set = NULL;
3239 ctx = isl_space_get_ctx(pma1->dim);
3240 if (!isl_space_is_equal(pma1->dim, pma2->dim))
3241 isl_die(ctx, isl_error_invalid,
3242 "arguments should live in the same space", goto error);
3244 if (isl_pw_multi_aff_is_empty(pma1)) {
3245 isl_pw_multi_aff_free(pma1);
3249 if (isl_pw_multi_aff_is_empty(pma2)) {
3250 isl_pw_multi_aff_free(pma2);
3254 n = 2 * (pma1->n + 1) * (pma2->n + 1);
3255 res = isl_pw_multi_aff_alloc_size(isl_space_copy(pma1->dim), n);
3257 for (i = 0; i < pma1->n; ++i) {
3258 set = isl_set_copy(pma1->p[i].set);
3259 for (j = 0; j < pma2->n; ++j) {
3263 better = shared_and_better(pma2->p[j].set,
3264 pma1->p[i].set, pma2->p[j].maff,
3265 pma1->p[i].maff, cmp);
3266 is_empty = isl_set_plain_is_empty(better);
3267 if (is_empty < 0 || is_empty) {
3268 isl_set_free(better);
3273 set = isl_set_subtract(set, isl_set_copy(better));
3275 res = isl_pw_multi_aff_add_piece(res, better,
3276 isl_multi_aff_copy(pma2->p[j].maff));
3278 res = isl_pw_multi_aff_add_piece(res, set,
3279 isl_multi_aff_copy(pma1->p[i].maff));
3282 for (j = 0; j < pma2->n; ++j) {
3283 set = isl_set_copy(pma2->p[j].set);
3284 for (i = 0; i < pma1->n; ++i)
3285 set = isl_set_subtract(set,
3286 isl_set_copy(pma1->p[i].set));
3287 res = isl_pw_multi_aff_add_piece(res, set,
3288 isl_multi_aff_copy(pma2->p[j].maff));
3291 isl_pw_multi_aff_free(pma1);
3292 isl_pw_multi_aff_free(pma2);
3296 isl_pw_multi_aff_free(pma1);
3297 isl_pw_multi_aff_free(pma2);
3299 return isl_pw_multi_aff_free(res);
3302 static __isl_give isl_pw_multi_aff *pw_multi_aff_union_lexmax(
3303 __isl_take isl_pw_multi_aff *pma1,
3304 __isl_take isl_pw_multi_aff *pma2)
3306 return pw_multi_aff_union_opt(pma1, pma2, &isl_multi_aff_lex_ge_set);
3309 /* Given two piecewise multi affine expressions, return a piecewise
3310 * multi-affine expression defined on the union of the definition domains
3311 * of the inputs that is equal to the lexicographic maximum of the two
3312 * inputs on each cell. If only one of the two inputs is defined on
3313 * a given cell, then it is considered to be the maximum.
3315 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_lexmax(
3316 __isl_take isl_pw_multi_aff *pma1,
3317 __isl_take isl_pw_multi_aff *pma2)
3319 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
3320 &pw_multi_aff_union_lexmax);
3323 static __isl_give isl_pw_multi_aff *pw_multi_aff_union_lexmin(
3324 __isl_take isl_pw_multi_aff *pma1,
3325 __isl_take isl_pw_multi_aff *pma2)
3327 return pw_multi_aff_union_opt(pma1, pma2, &isl_multi_aff_lex_le_set);
3330 /* Given two piecewise multi affine expressions, return a piecewise
3331 * multi-affine expression defined on the union of the definition domains
3332 * of the inputs that is equal to the lexicographic minimum of the two
3333 * inputs on each cell. If only one of the two inputs is defined on
3334 * a given cell, then it is considered to be the minimum.
3336 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_lexmin(
3337 __isl_take isl_pw_multi_aff *pma1,
3338 __isl_take isl_pw_multi_aff *pma2)
3340 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
3341 &pw_multi_aff_union_lexmin);
3344 static __isl_give isl_pw_multi_aff *pw_multi_aff_add(
3345 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
3347 return isl_pw_multi_aff_on_shared_domain(pma1, pma2,
3348 &isl_multi_aff_add);
3351 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_add(
3352 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
3354 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
3358 static __isl_give isl_pw_multi_aff *pw_multi_aff_sub(
3359 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
3361 return isl_pw_multi_aff_on_shared_domain(pma1, pma2,
3362 &isl_multi_aff_sub);
3365 /* Subtract "pma2" from "pma1" and return the result.
3367 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_sub(
3368 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
3370 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
3374 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_add(
3375 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
3377 return isl_pw_multi_aff_union_add_(pma1, pma2);
3380 /* Given two piecewise multi-affine expressions A -> B and C -> D,
3381 * construct a piecewise multi-affine expression [A -> C] -> [B -> D].
3383 static __isl_give isl_pw_multi_aff *pw_multi_aff_product(
3384 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
3388 isl_pw_multi_aff *res;
3393 n = pma1->n * pma2->n;
3394 space = isl_space_product(isl_space_copy(pma1->dim),
3395 isl_space_copy(pma2->dim));
3396 res = isl_pw_multi_aff_alloc_size(space, n);
3398 for (i = 0; i < pma1->n; ++i) {
3399 for (j = 0; j < pma2->n; ++j) {
3403 domain = isl_set_product(isl_set_copy(pma1->p[i].set),
3404 isl_set_copy(pma2->p[j].set));
3405 ma = isl_multi_aff_product(
3406 isl_multi_aff_copy(pma1->p[i].maff),
3407 isl_multi_aff_copy(pma2->p[i].maff));
3408 res = isl_pw_multi_aff_add_piece(res, domain, ma);
3412 isl_pw_multi_aff_free(pma1);
3413 isl_pw_multi_aff_free(pma2);
3416 isl_pw_multi_aff_free(pma1);
3417 isl_pw_multi_aff_free(pma2);
3421 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_product(
3422 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
3424 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
3425 &pw_multi_aff_product);
3428 /* Construct a map mapping the domain of the piecewise multi-affine expression
3429 * to its range, with each dimension in the range equated to the
3430 * corresponding affine expression on its cell.
3432 __isl_give isl_map *isl_map_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma)
3440 map = isl_map_empty(isl_pw_multi_aff_get_space(pma));
3442 for (i = 0; i < pma->n; ++i) {
3443 isl_multi_aff *maff;
3444 isl_basic_map *bmap;
3447 maff = isl_multi_aff_copy(pma->p[i].maff);
3448 bmap = isl_basic_map_from_multi_aff(maff);
3449 map_i = isl_map_from_basic_map(bmap);
3450 map_i = isl_map_intersect_domain(map_i,
3451 isl_set_copy(pma->p[i].set));
3452 map = isl_map_union_disjoint(map, map_i);
3455 isl_pw_multi_aff_free(pma);
3459 __isl_give isl_set *isl_set_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma)
3464 if (!isl_space_is_set(pma->dim))
3465 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
3466 "isl_pw_multi_aff cannot be converted into an isl_set",
3467 return isl_pw_multi_aff_free(pma));
3469 return isl_map_from_pw_multi_aff(pma);
3472 /* Given a basic map with a single output dimension that is defined
3473 * in terms of the parameters and input dimensions using an equality,
3474 * extract an isl_aff that expresses the output dimension in terms
3475 * of the parameters and input dimensions.
3477 * Since some applications expect the result of isl_pw_multi_aff_from_map
3478 * to only contain integer affine expressions, we compute the floor
3479 * of the expression before returning.
3481 * This function shares some similarities with
3482 * isl_basic_map_has_defining_equality and isl_constraint_get_bound.
3484 static __isl_give isl_aff *extract_isl_aff_from_basic_map(
3485 __isl_take isl_basic_map *bmap)
3490 isl_local_space *ls;
3495 if (isl_basic_map_dim(bmap, isl_dim_out) != 1)
3496 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
3497 "basic map should have a single output dimension",
3499 offset = isl_basic_map_offset(bmap, isl_dim_out);
3500 total = isl_basic_map_total_dim(bmap);
3501 for (i = 0; i < bmap->n_eq; ++i) {
3502 if (isl_int_is_zero(bmap->eq[i][offset]))
3504 if (isl_seq_first_non_zero(bmap->eq[i] + offset + 1,
3505 1 + total - (offset + 1)) != -1)
3509 if (i >= bmap->n_eq)
3510 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
3511 "unable to find suitable equality", goto error);
3512 ls = isl_basic_map_get_local_space(bmap);
3513 aff = isl_aff_alloc(isl_local_space_domain(ls));
3516 if (isl_int_is_neg(bmap->eq[i][offset]))
3517 isl_seq_cpy(aff->v->el + 1, bmap->eq[i], offset);
3519 isl_seq_neg(aff->v->el + 1, bmap->eq[i], offset);
3520 isl_seq_clr(aff->v->el + 1 + offset, aff->v->size - (1 + offset));
3521 isl_int_abs(aff->v->el[0], bmap->eq[i][offset]);
3522 isl_basic_map_free(bmap);
3524 aff = isl_aff_remove_unused_divs(aff);
3525 aff = isl_aff_floor(aff);
3528 isl_basic_map_free(bmap);
3532 /* Given a basic map where each output dimension is defined
3533 * in terms of the parameters and input dimensions using an equality,
3534 * extract an isl_multi_aff that expresses the output dimensions in terms
3535 * of the parameters and input dimensions.
3537 static __isl_give isl_multi_aff *extract_isl_multi_aff_from_basic_map(
3538 __isl_take isl_basic_map *bmap)
3547 ma = isl_multi_aff_alloc(isl_basic_map_get_space(bmap));
3548 n_out = isl_basic_map_dim(bmap, isl_dim_out);
3550 for (i = 0; i < n_out; ++i) {
3551 isl_basic_map *bmap_i;
3554 bmap_i = isl_basic_map_copy(bmap);
3555 bmap_i = isl_basic_map_project_out(bmap_i, isl_dim_out,
3556 i + 1, n_out - (1 + i));
3557 bmap_i = isl_basic_map_project_out(bmap_i, isl_dim_out, 0, i);
3558 aff = extract_isl_aff_from_basic_map(bmap_i);
3559 ma = isl_multi_aff_set_aff(ma, i, aff);
3562 isl_basic_map_free(bmap);
3567 /* Create an isl_pw_multi_aff that is equivalent to
3568 * isl_map_intersect_domain(isl_map_from_basic_map(bmap), domain).
3569 * The given basic map is such that each output dimension is defined
3570 * in terms of the parameters and input dimensions using an equality.
3572 static __isl_give isl_pw_multi_aff *plain_pw_multi_aff_from_map(
3573 __isl_take isl_set *domain, __isl_take isl_basic_map *bmap)
3577 ma = extract_isl_multi_aff_from_basic_map(bmap);
3578 return isl_pw_multi_aff_alloc(domain, ma);
3581 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
3582 * This obviously only works if the input "map" is single-valued.
3583 * If so, we compute the lexicographic minimum of the image in the form
3584 * of an isl_pw_multi_aff. Since the image is unique, it is equal
3585 * to its lexicographic minimum.
3586 * If the input is not single-valued, we produce an error.
3588 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_base(
3589 __isl_take isl_map *map)
3593 isl_pw_multi_aff *pma;
3595 sv = isl_map_is_single_valued(map);
3599 isl_die(isl_map_get_ctx(map), isl_error_invalid,
3600 "map is not single-valued", goto error);
3601 map = isl_map_make_disjoint(map);
3605 pma = isl_pw_multi_aff_empty(isl_map_get_space(map));
3607 for (i = 0; i < map->n; ++i) {
3608 isl_pw_multi_aff *pma_i;
3609 isl_basic_map *bmap;
3610 bmap = isl_basic_map_copy(map->p[i]);
3611 pma_i = isl_basic_map_lexmin_pw_multi_aff(bmap);
3612 pma = isl_pw_multi_aff_add_disjoint(pma, pma_i);
3622 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
3623 * taking into account that the output dimension at position "d"
3624 * can be represented as
3626 * x = floor((e(...) + c1) / m)
3628 * given that constraint "i" is of the form
3630 * e(...) + c1 - m x >= 0
3633 * Let "map" be of the form
3637 * We construct a mapping
3639 * A -> [A -> x = floor(...)]
3641 * apply that to the map, obtaining
3643 * [A -> x = floor(...)] -> B
3645 * and equate dimension "d" to x.
3646 * We then compute a isl_pw_multi_aff representation of the resulting map
3647 * and plug in the mapping above.
3649 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_div(
3650 __isl_take isl_map *map, __isl_take isl_basic_map *hull, int d, int i)
3654 isl_local_space *ls;
3662 isl_pw_multi_aff *pma;
3665 is_set = isl_map_is_set(map);
3667 offset = isl_basic_map_offset(hull, isl_dim_out);
3668 ctx = isl_map_get_ctx(map);
3669 space = isl_space_domain(isl_map_get_space(map));
3670 n_in = isl_space_dim(space, isl_dim_set);
3671 n = isl_space_dim(space, isl_dim_all);
3673 v = isl_vec_alloc(ctx, 1 + 1 + n);
3675 isl_int_neg(v->el[0], hull->ineq[i][offset + d]);
3676 isl_seq_cpy(v->el + 1, hull->ineq[i], 1 + n);
3678 isl_basic_map_free(hull);
3680 ls = isl_local_space_from_space(isl_space_copy(space));
3681 aff = isl_aff_alloc_vec(ls, v);
3682 aff = isl_aff_floor(aff);
3684 isl_space_free(space);
3685 ma = isl_multi_aff_from_aff(aff);
3687 ma = isl_multi_aff_identity(isl_space_map_from_set(space));
3688 ma = isl_multi_aff_range_product(ma,
3689 isl_multi_aff_from_aff(aff));
3692 insert = isl_map_from_multi_aff(isl_multi_aff_copy(ma));
3693 map = isl_map_apply_domain(map, insert);
3694 map = isl_map_equate(map, isl_dim_in, n_in, isl_dim_out, d);
3695 pma = isl_pw_multi_aff_from_map(map);
3696 pma = isl_pw_multi_aff_pullback_multi_aff(pma, ma);
3701 /* Is constraint "c" of the form
3703 * e(...) + c1 - m x >= 0
3707 * -e(...) + c2 + m x >= 0
3709 * where m > 1 and e only depends on parameters and input dimemnsions?
3711 * "offset" is the offset of the output dimensions
3712 * "pos" is the position of output dimension x.
3714 static int is_potential_div_constraint(isl_int *c, int offset, int d, int total)
3716 if (isl_int_is_zero(c[offset + d]))
3718 if (isl_int_is_one(c[offset + d]))
3720 if (isl_int_is_negone(c[offset + d]))
3722 if (isl_seq_first_non_zero(c + offset, d) != -1)
3724 if (isl_seq_first_non_zero(c + offset + d + 1,
3725 total - (offset + d + 1)) != -1)
3730 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
3732 * As a special case, we first check if there is any pair of constraints,
3733 * shared by all the basic maps in "map" that force a given dimension
3734 * to be equal to the floor of some affine combination of the input dimensions.
3736 * In particular, if we can find two constraints
3738 * e(...) + c1 - m x >= 0 i.e., m x <= e(...) + c1
3742 * -e(...) + c2 + m x >= 0 i.e., m x >= e(...) - c2
3744 * where m > 1 and e only depends on parameters and input dimemnsions,
3747 * c1 + c2 < m i.e., -c2 >= c1 - (m - 1)
3749 * then we know that we can take
3751 * x = floor((e(...) + c1) / m)
3753 * without having to perform any computation.
3755 * Note that we know that
3759 * If c1 + c2 were 0, then we would have detected an equality during
3760 * simplification. If c1 + c2 were negative, then we would have detected
3763 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_check_div(
3764 __isl_take isl_map *map)
3770 isl_basic_map *hull;
3772 hull = isl_map_unshifted_simple_hull(isl_map_copy(map));
3777 dim = isl_map_dim(map, isl_dim_out);
3778 offset = isl_basic_map_offset(hull, isl_dim_out);
3779 total = 1 + isl_basic_map_total_dim(hull);
3781 for (d = 0; d < dim; ++d) {
3782 for (i = 0; i < n; ++i) {
3783 if (!is_potential_div_constraint(hull->ineq[i],
3786 for (j = i + 1; j < n; ++j) {
3787 if (!isl_seq_is_neg(hull->ineq[i] + 1,
3788 hull->ineq[j] + 1, total - 1))
3790 isl_int_add(sum, hull->ineq[i][0],
3792 if (isl_int_abs_lt(sum,
3793 hull->ineq[i][offset + d]))
3800 if (isl_int_is_pos(hull->ineq[j][offset + d]))
3802 return pw_multi_aff_from_map_div(map, hull, d, j);
3806 isl_basic_map_free(hull);
3807 return pw_multi_aff_from_map_base(map);
3810 isl_basic_map_free(hull);
3814 /* Given an affine expression
3816 * [A -> B] -> f(A,B)
3818 * construct an isl_multi_aff
3822 * such that dimension "d" in B' is set to "aff" and the remaining
3823 * dimensions are set equal to the corresponding dimensions in B.
3824 * "n_in" is the dimension of the space A.
3825 * "n_out" is the dimension of the space B.
3827 * If "is_set" is set, then the affine expression is of the form
3831 * and we construct an isl_multi_aff
3835 static __isl_give isl_multi_aff *range_map(__isl_take isl_aff *aff, int d,
3836 unsigned n_in, unsigned n_out, int is_set)
3840 isl_space *space, *space2;
3841 isl_local_space *ls;
3843 space = isl_aff_get_domain_space(aff);
3844 ls = isl_local_space_from_space(isl_space_copy(space));
3845 space2 = isl_space_copy(space);
3847 space2 = isl_space_range(isl_space_unwrap(space2));
3848 space = isl_space_map_from_domain_and_range(space, space2);
3849 ma = isl_multi_aff_alloc(space);
3850 ma = isl_multi_aff_set_aff(ma, d, aff);
3852 for (i = 0; i < n_out; ++i) {
3855 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
3856 isl_dim_set, n_in + i);
3857 ma = isl_multi_aff_set_aff(ma, i, aff);
3860 isl_local_space_free(ls);
3865 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
3866 * taking into account that the dimension at position "d" can be written as
3868 * x = m a + f(..) (1)
3870 * where m is equal to "gcd".
3871 * "i" is the index of the equality in "hull" that defines f(..).
3872 * In particular, the equality is of the form
3874 * f(..) - x + m g(existentials) = 0
3878 * -f(..) + x + m g(existentials) = 0
3880 * We basically plug (1) into "map", resulting in a map with "a"
3881 * in the range instead of "x". The corresponding isl_pw_multi_aff
3882 * defining "a" is then plugged back into (1) to obtain a definition fro "x".
3884 * Specifically, given the input map
3888 * We first wrap it into a set
3892 * and define (1) on top of the corresponding space, resulting in "aff".
3893 * We use this to create an isl_multi_aff that maps the output position "d"
3894 * from "a" to "x", leaving all other (intput and output) dimensions unchanged.
3895 * We plug this into the wrapped map, unwrap the result and compute the
3896 * corresponding isl_pw_multi_aff.
3897 * The result is an expression
3905 * so that we can plug that into "aff", after extending the latter to
3911 * If "map" is actually a set, then there is no "A" space, meaning
3912 * that we do not need to perform any wrapping, and that the result
3913 * of the recursive call is of the form
3917 * which is plugged into a mapping of the form
3921 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_stride(
3922 __isl_take isl_map *map, __isl_take isl_basic_map *hull, int d, int i,
3927 isl_local_space *ls;
3930 isl_pw_multi_aff *pma, *id;
3936 is_set = isl_map_is_set(map);
3938 n_in = isl_basic_map_dim(hull, isl_dim_in);
3939 n_out = isl_basic_map_dim(hull, isl_dim_out);
3940 o_out = isl_basic_map_offset(hull, isl_dim_out);
3945 set = isl_map_wrap(map);
3946 space = isl_space_map_from_set(isl_set_get_space(set));
3947 ma = isl_multi_aff_identity(space);
3948 ls = isl_local_space_from_space(isl_set_get_space(set));
3949 aff = isl_aff_alloc(ls);
3951 isl_int_set_si(aff->v->el[0], 1);
3952 if (isl_int_is_one(hull->eq[i][o_out + d]))
3953 isl_seq_neg(aff->v->el + 1, hull->eq[i],
3956 isl_seq_cpy(aff->v->el + 1, hull->eq[i],
3958 isl_int_set(aff->v->el[1 + o_out + d], gcd);
3960 ma = isl_multi_aff_set_aff(ma, n_in + d, isl_aff_copy(aff));
3961 set = isl_set_preimage_multi_aff(set, ma);
3963 ma = range_map(aff, d, n_in, n_out, is_set);
3968 map = isl_set_unwrap(set);
3969 pma = isl_pw_multi_aff_from_map(set);
3972 space = isl_pw_multi_aff_get_domain_space(pma);
3973 space = isl_space_map_from_set(space);
3974 id = isl_pw_multi_aff_identity(space);
3975 pma = isl_pw_multi_aff_range_product(id, pma);
3977 id = isl_pw_multi_aff_from_multi_aff(ma);
3978 pma = isl_pw_multi_aff_pullback_pw_multi_aff(id, pma);
3980 isl_basic_map_free(hull);
3984 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
3986 * As a special case, we first check if all output dimensions are uniquely
3987 * defined in terms of the parameters and input dimensions over the entire
3988 * domain. If so, we extract the desired isl_pw_multi_aff directly
3989 * from the affine hull of "map" and its domain.
3991 * Otherwise, we check if any of the output dimensions is "strided".
3992 * That is, we check if can be written as
3996 * with m greater than 1, a some combination of existentiall quantified
3997 * variables and f and expression in the parameters and input dimensions.
3998 * If so, we remove the stride in pw_multi_aff_from_map_stride.
4000 * Otherwise, we continue with pw_multi_aff_from_map_check_div for a further
4003 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_map(__isl_take isl_map *map)
4007 isl_basic_map *hull;
4017 hull = isl_map_affine_hull(isl_map_copy(map));
4018 sv = isl_basic_map_plain_is_single_valued(hull);
4020 return plain_pw_multi_aff_from_map(isl_map_domain(map), hull);
4022 hull = isl_basic_map_free(hull);
4026 n_div = isl_basic_map_dim(hull, isl_dim_div);
4027 o_div = isl_basic_map_offset(hull, isl_dim_div);
4030 isl_basic_map_free(hull);
4031 return pw_multi_aff_from_map_check_div(map);
4036 n_out = isl_basic_map_dim(hull, isl_dim_out);
4037 o_out = isl_basic_map_offset(hull, isl_dim_out);
4039 for (i = 0; i < n_out; ++i) {
4040 for (j = 0; j < hull->n_eq; ++j) {
4041 isl_int *eq = hull->eq[j];
4042 isl_pw_multi_aff *res;
4044 if (!isl_int_is_one(eq[o_out + i]) &&
4045 !isl_int_is_negone(eq[o_out + i]))
4047 if (isl_seq_first_non_zero(eq + o_out, i) != -1)
4049 if (isl_seq_first_non_zero(eq + o_out + i + 1,
4050 n_out - (i + 1)) != -1)
4052 isl_seq_gcd(eq + o_div, n_div, &gcd);
4053 if (isl_int_is_zero(gcd))
4055 if (isl_int_is_one(gcd))
4058 res = pw_multi_aff_from_map_stride(map, hull,
4066 isl_basic_map_free(hull);
4067 return pw_multi_aff_from_map_check_div(map);
4073 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_set(__isl_take isl_set *set)
4075 return isl_pw_multi_aff_from_map(set);
4078 /* Convert "map" into an isl_pw_multi_aff (if possible) and
4081 static int pw_multi_aff_from_map(__isl_take isl_map *map, void *user)
4083 isl_union_pw_multi_aff **upma = user;
4084 isl_pw_multi_aff *pma;
4086 pma = isl_pw_multi_aff_from_map(map);
4087 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
4089 return *upma ? 0 : -1;
4092 /* Try and create an isl_union_pw_multi_aff that is equivalent
4093 * to the given isl_union_map.
4094 * The isl_union_map is required to be single-valued in each space.
4095 * Otherwise, an error is produced.
4097 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_map(
4098 __isl_take isl_union_map *umap)
4101 isl_union_pw_multi_aff *upma;
4103 space = isl_union_map_get_space(umap);
4104 upma = isl_union_pw_multi_aff_empty(space);
4105 if (isl_union_map_foreach_map(umap, &pw_multi_aff_from_map, &upma) < 0)
4106 upma = isl_union_pw_multi_aff_free(upma);
4107 isl_union_map_free(umap);
4112 /* Try and create an isl_union_pw_multi_aff that is equivalent
4113 * to the given isl_union_set.
4114 * The isl_union_set is required to be a singleton in each space.
4115 * Otherwise, an error is produced.
4117 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_set(
4118 __isl_take isl_union_set *uset)
4120 return isl_union_pw_multi_aff_from_union_map(uset);
4123 /* Return the piecewise affine expression "set ? 1 : 0".
4125 __isl_give isl_pw_aff *isl_set_indicator_function(__isl_take isl_set *set)
4128 isl_space *space = isl_set_get_space(set);
4129 isl_local_space *ls = isl_local_space_from_space(space);
4130 isl_aff *zero = isl_aff_zero_on_domain(isl_local_space_copy(ls));
4131 isl_aff *one = isl_aff_zero_on_domain(ls);
4133 one = isl_aff_add_constant_si(one, 1);
4134 pa = isl_pw_aff_alloc(isl_set_copy(set), one);
4135 set = isl_set_complement(set);
4136 pa = isl_pw_aff_add_disjoint(pa, isl_pw_aff_alloc(set, zero));
4141 /* Plug in "subs" for dimension "type", "pos" of "aff".
4143 * Let i be the dimension to replace and let "subs" be of the form
4147 * and "aff" of the form
4153 * (a f + d g')/(m d)
4155 * where g' is the result of plugging in "subs" in each of the integer
4158 __isl_give isl_aff *isl_aff_substitute(__isl_take isl_aff *aff,
4159 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
4164 aff = isl_aff_cow(aff);
4166 return isl_aff_free(aff);
4168 ctx = isl_aff_get_ctx(aff);
4169 if (!isl_space_is_equal(aff->ls->dim, subs->ls->dim))
4170 isl_die(ctx, isl_error_invalid,
4171 "spaces don't match", return isl_aff_free(aff));
4172 if (isl_local_space_dim(subs->ls, isl_dim_div) != 0)
4173 isl_die(ctx, isl_error_unsupported,
4174 "cannot handle divs yet", return isl_aff_free(aff));
4176 aff->ls = isl_local_space_substitute(aff->ls, type, pos, subs);
4178 return isl_aff_free(aff);
4180 aff->v = isl_vec_cow(aff->v);
4182 return isl_aff_free(aff);
4184 pos += isl_local_space_offset(aff->ls, type);
4187 isl_seq_substitute(aff->v->el, pos, subs->v->el,
4188 aff->v->size, subs->v->size, v);
4194 /* Plug in "subs" for dimension "type", "pos" in each of the affine
4195 * expressions in "maff".
4197 __isl_give isl_multi_aff *isl_multi_aff_substitute(
4198 __isl_take isl_multi_aff *maff, enum isl_dim_type type, unsigned pos,
4199 __isl_keep isl_aff *subs)
4203 maff = isl_multi_aff_cow(maff);
4205 return isl_multi_aff_free(maff);
4207 if (type == isl_dim_in)
4210 for (i = 0; i < maff->n; ++i) {
4211 maff->p[i] = isl_aff_substitute(maff->p[i], type, pos, subs);
4213 return isl_multi_aff_free(maff);
4219 /* Plug in "subs" for dimension "type", "pos" of "pma".
4221 * pma is of the form
4225 * while subs is of the form
4227 * v' = B_j(v) -> S_j
4229 * Each pair i,j such that C_ij = A_i \cap B_i is non-empty
4230 * has a contribution in the result, in particular
4232 * C_ij(S_j) -> M_i(S_j)
4234 * Note that plugging in S_j in C_ij may also result in an empty set
4235 * and this contribution should simply be discarded.
4237 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_substitute(
4238 __isl_take isl_pw_multi_aff *pma, enum isl_dim_type type, unsigned pos,
4239 __isl_keep isl_pw_aff *subs)
4242 isl_pw_multi_aff *res;
4245 return isl_pw_multi_aff_free(pma);
4247 n = pma->n * subs->n;
4248 res = isl_pw_multi_aff_alloc_size(isl_space_copy(pma->dim), n);
4250 for (i = 0; i < pma->n; ++i) {
4251 for (j = 0; j < subs->n; ++j) {
4253 isl_multi_aff *res_ij;
4256 common = isl_set_intersect(
4257 isl_set_copy(pma->p[i].set),
4258 isl_set_copy(subs->p[j].set));
4259 common = isl_set_substitute(common,
4260 type, pos, subs->p[j].aff);
4261 empty = isl_set_plain_is_empty(common);
4262 if (empty < 0 || empty) {
4263 isl_set_free(common);
4269 res_ij = isl_multi_aff_substitute(
4270 isl_multi_aff_copy(pma->p[i].maff),
4271 type, pos, subs->p[j].aff);
4273 res = isl_pw_multi_aff_add_piece(res, common, res_ij);
4277 isl_pw_multi_aff_free(pma);
4280 isl_pw_multi_aff_free(pma);
4281 isl_pw_multi_aff_free(res);
4285 /* Compute the preimage of a range of dimensions in the affine expression "src"
4286 * under "ma" and put the result in "dst". The number of dimensions in "src"
4287 * that precede the range is given by "n_before". The number of dimensions
4288 * in the range is given by the number of output dimensions of "ma".
4289 * The number of dimensions that follow the range is given by "n_after".
4290 * If "has_denom" is set (to one),
4291 * then "src" and "dst" have an extra initial denominator.
4292 * "n_div_ma" is the number of existentials in "ma"
4293 * "n_div_bset" is the number of existentials in "src"
4294 * The resulting "dst" (which is assumed to have been allocated by
4295 * the caller) contains coefficients for both sets of existentials,
4296 * first those in "ma" and then those in "src".
4297 * f, c1, c2 and g are temporary objects that have been initialized
4300 * Let src represent the expression
4302 * (a(p) + f_u u + b v + f_w w + c(divs))/d
4304 * and let ma represent the expressions
4306 * v_i = (r_i(p) + s_i(y) + t_i(divs'))/m_i
4308 * We start out with the following expression for dst:
4310 * (a(p) + f_u u + 0 y + f_w w + 0 divs' + c(divs) + f \sum_i b_i v_i)/d
4312 * with the multiplication factor f initially equal to 1
4313 * and f \sum_i b_i v_i kept separately.
4314 * For each x_i that we substitute, we multiply the numerator
4315 * (and denominator) of dst by c_1 = m_i and add the numerator
4316 * of the x_i expression multiplied by c_2 = f b_i,
4317 * after removing the common factors of c_1 and c_2.
4318 * The multiplication factor f also needs to be multiplied by c_1
4319 * for the next x_j, j > i.
4321 void isl_seq_preimage(isl_int *dst, isl_int *src,
4322 __isl_keep isl_multi_aff *ma, int n_before, int n_after,
4323 int n_div_ma, int n_div_bmap,
4324 isl_int f, isl_int c1, isl_int c2, isl_int g, int has_denom)
4327 int n_param, n_in, n_out;
4330 n_param = isl_multi_aff_dim(ma, isl_dim_param);
4331 n_in = isl_multi_aff_dim(ma, isl_dim_in);
4332 n_out = isl_multi_aff_dim(ma, isl_dim_out);
4334 isl_seq_cpy(dst, src, has_denom + 1 + n_param + n_before);
4335 o_dst = o_src = has_denom + 1 + n_param + n_before;
4336 isl_seq_clr(dst + o_dst, n_in);
4339 isl_seq_cpy(dst + o_dst, src + o_src, n_after);
4342 isl_seq_clr(dst + o_dst, n_div_ma);
4344 isl_seq_cpy(dst + o_dst, src + o_src, n_div_bmap);
4346 isl_int_set_si(f, 1);
4348 for (i = 0; i < n_out; ++i) {
4349 int offset = has_denom + 1 + n_param + n_before + i;
4351 if (isl_int_is_zero(src[offset]))
4353 isl_int_set(c1, ma->p[i]->v->el[0]);
4354 isl_int_mul(c2, f, src[offset]);
4355 isl_int_gcd(g, c1, c2);
4356 isl_int_divexact(c1, c1, g);
4357 isl_int_divexact(c2, c2, g);
4359 isl_int_mul(f, f, c1);
4362 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
4363 c2, ma->p[i]->v->el + o_src, 1 + n_param);
4364 o_dst += 1 + n_param;
4365 o_src += 1 + n_param;
4366 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_before);
4368 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
4369 c2, ma->p[i]->v->el + o_src, n_in);
4372 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_after);
4374 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
4375 c2, ma->p[i]->v->el + o_src, n_div_ma);
4378 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_div_bmap);
4380 isl_int_mul(dst[0], dst[0], c1);
4384 /* Compute the pullback of "aff" by the function represented by "ma".
4385 * In other words, plug in "ma" in "aff". The result is an affine expression
4386 * defined over the domain space of "ma".
4388 * If "aff" is represented by
4390 * (a(p) + b x + c(divs))/d
4392 * and ma is represented by
4394 * x = D(p) + F(y) + G(divs')
4396 * then the result is
4398 * (a(p) + b D(p) + b F(y) + b G(divs') + c(divs))/d
4400 * The divs in the local space of the input are similarly adjusted
4401 * through a call to isl_local_space_preimage_multi_aff.
4403 __isl_give isl_aff *isl_aff_pullback_multi_aff(__isl_take isl_aff *aff,
4404 __isl_take isl_multi_aff *ma)
4406 isl_aff *res = NULL;
4407 isl_local_space *ls;
4408 int n_div_aff, n_div_ma;
4409 isl_int f, c1, c2, g;
4411 ma = isl_multi_aff_align_divs(ma);
4415 n_div_aff = isl_aff_dim(aff, isl_dim_div);
4416 n_div_ma = ma->n ? isl_aff_dim(ma->p[0], isl_dim_div) : 0;
4418 ls = isl_aff_get_domain_local_space(aff);
4419 ls = isl_local_space_preimage_multi_aff(ls, isl_multi_aff_copy(ma));
4420 res = isl_aff_alloc(ls);
4429 isl_seq_preimage(res->v->el, aff->v->el, ma, 0, 0, n_div_ma, n_div_aff,
4438 isl_multi_aff_free(ma);
4439 res = isl_aff_normalize(res);
4443 isl_multi_aff_free(ma);
4448 /* Compute the pullback of "ma1" by the function represented by "ma2".
4449 * In other words, plug in "ma2" in "ma1".
4451 __isl_give isl_multi_aff *isl_multi_aff_pullback_multi_aff(
4452 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2)
4455 isl_space *space = NULL;
4457 ma2 = isl_multi_aff_align_divs(ma2);
4458 ma1 = isl_multi_aff_cow(ma1);
4462 space = isl_space_join(isl_multi_aff_get_space(ma2),
4463 isl_multi_aff_get_space(ma1));
4465 for (i = 0; i < ma1->n; ++i) {
4466 ma1->p[i] = isl_aff_pullback_multi_aff(ma1->p[i],
4467 isl_multi_aff_copy(ma2));
4472 ma1 = isl_multi_aff_reset_space(ma1, space);
4473 isl_multi_aff_free(ma2);
4476 isl_space_free(space);
4477 isl_multi_aff_free(ma2);
4478 isl_multi_aff_free(ma1);
4482 /* Extend the local space of "dst" to include the divs
4483 * in the local space of "src".
4485 __isl_give isl_aff *isl_aff_align_divs(__isl_take isl_aff *dst,
4486 __isl_keep isl_aff *src)
4494 return isl_aff_free(dst);
4496 ctx = isl_aff_get_ctx(src);
4497 if (!isl_space_is_equal(src->ls->dim, dst->ls->dim))
4498 isl_die(ctx, isl_error_invalid,
4499 "spaces don't match", goto error);
4501 if (src->ls->div->n_row == 0)
4504 exp1 = isl_alloc_array(ctx, int, src->ls->div->n_row);
4505 exp2 = isl_alloc_array(ctx, int, dst->ls->div->n_row);
4509 div = isl_merge_divs(src->ls->div, dst->ls->div, exp1, exp2);
4510 dst = isl_aff_expand_divs(dst, div, exp2);
4518 return isl_aff_free(dst);
4521 /* Adjust the local spaces of the affine expressions in "maff"
4522 * such that they all have the save divs.
4524 __isl_give isl_multi_aff *isl_multi_aff_align_divs(
4525 __isl_take isl_multi_aff *maff)
4533 maff = isl_multi_aff_cow(maff);
4537 for (i = 1; i < maff->n; ++i)
4538 maff->p[0] = isl_aff_align_divs(maff->p[0], maff->p[i]);
4539 for (i = 1; i < maff->n; ++i) {
4540 maff->p[i] = isl_aff_align_divs(maff->p[i], maff->p[0]);
4542 return isl_multi_aff_free(maff);
4548 __isl_give isl_aff *isl_aff_lift(__isl_take isl_aff *aff)
4550 aff = isl_aff_cow(aff);
4554 aff->ls = isl_local_space_lift(aff->ls);
4556 return isl_aff_free(aff);
4561 /* Lift "maff" to a space with extra dimensions such that the result
4562 * has no more existentially quantified variables.
4563 * If "ls" is not NULL, then *ls is assigned the local space that lies
4564 * at the basis of the lifting applied to "maff".
4566 __isl_give isl_multi_aff *isl_multi_aff_lift(__isl_take isl_multi_aff *maff,
4567 __isl_give isl_local_space **ls)
4581 isl_space *space = isl_multi_aff_get_domain_space(maff);
4582 *ls = isl_local_space_from_space(space);
4584 return isl_multi_aff_free(maff);
4589 maff = isl_multi_aff_cow(maff);
4590 maff = isl_multi_aff_align_divs(maff);
4594 n_div = isl_aff_dim(maff->p[0], isl_dim_div);
4595 space = isl_multi_aff_get_space(maff);
4596 space = isl_space_lift(isl_space_domain(space), n_div);
4597 space = isl_space_extend_domain_with_range(space,
4598 isl_multi_aff_get_space(maff));
4600 return isl_multi_aff_free(maff);
4601 isl_space_free(maff->space);
4602 maff->space = space;
4605 *ls = isl_aff_get_domain_local_space(maff->p[0]);
4607 return isl_multi_aff_free(maff);
4610 for (i = 0; i < maff->n; ++i) {
4611 maff->p[i] = isl_aff_lift(maff->p[i]);
4619 isl_local_space_free(*ls);
4620 return isl_multi_aff_free(maff);
4624 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma".
4626 __isl_give isl_pw_aff *isl_pw_multi_aff_get_pw_aff(
4627 __isl_keep isl_pw_multi_aff *pma, int pos)
4637 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
4638 if (pos < 0 || pos >= n_out)
4639 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
4640 "index out of bounds", return NULL);
4642 space = isl_pw_multi_aff_get_space(pma);
4643 space = isl_space_drop_dims(space, isl_dim_out,
4644 pos + 1, n_out - pos - 1);
4645 space = isl_space_drop_dims(space, isl_dim_out, 0, pos);
4647 pa = isl_pw_aff_alloc_size(space, pma->n);
4648 for (i = 0; i < pma->n; ++i) {
4650 aff = isl_multi_aff_get_aff(pma->p[i].maff, pos);
4651 pa = isl_pw_aff_add_piece(pa, isl_set_copy(pma->p[i].set), aff);
4657 /* Return an isl_pw_multi_aff with the given "set" as domain and
4658 * an unnamed zero-dimensional range.
4660 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_domain(
4661 __isl_take isl_set *set)
4666 space = isl_set_get_space(set);
4667 space = isl_space_from_domain(space);
4668 ma = isl_multi_aff_zero(space);
4669 return isl_pw_multi_aff_alloc(set, ma);
4672 /* Add an isl_pw_multi_aff with the given "set" as domain and
4673 * an unnamed zero-dimensional range to *user.
4675 static int add_pw_multi_aff_from_domain(__isl_take isl_set *set, void *user)
4677 isl_union_pw_multi_aff **upma = user;
4678 isl_pw_multi_aff *pma;
4680 pma = isl_pw_multi_aff_from_domain(set);
4681 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
4686 /* Return an isl_union_pw_multi_aff with the given "uset" as domain and
4687 * an unnamed zero-dimensional range.
4689 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_domain(
4690 __isl_take isl_union_set *uset)
4693 isl_union_pw_multi_aff *upma;
4698 space = isl_union_set_get_space(uset);
4699 upma = isl_union_pw_multi_aff_empty(space);
4701 if (isl_union_set_foreach_set(uset,
4702 &add_pw_multi_aff_from_domain, &upma) < 0)
4705 isl_union_set_free(uset);
4708 isl_union_set_free(uset);
4709 isl_union_pw_multi_aff_free(upma);
4713 /* Convert "pma" to an isl_map and add it to *umap.
4715 static int map_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma, void *user)
4717 isl_union_map **umap = user;
4720 map = isl_map_from_pw_multi_aff(pma);
4721 *umap = isl_union_map_add_map(*umap, map);
4726 /* Construct a union map mapping the domain of the union
4727 * piecewise multi-affine expression to its range, with each dimension
4728 * in the range equated to the corresponding affine expression on its cell.
4730 __isl_give isl_union_map *isl_union_map_from_union_pw_multi_aff(
4731 __isl_take isl_union_pw_multi_aff *upma)
4734 isl_union_map *umap;
4739 space = isl_union_pw_multi_aff_get_space(upma);
4740 umap = isl_union_map_empty(space);
4742 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
4743 &map_from_pw_multi_aff, &umap) < 0)
4746 isl_union_pw_multi_aff_free(upma);
4749 isl_union_pw_multi_aff_free(upma);
4750 isl_union_map_free(umap);
4754 /* Local data for bin_entry and the callback "fn".
4756 struct isl_union_pw_multi_aff_bin_data {
4757 isl_union_pw_multi_aff *upma2;
4758 isl_union_pw_multi_aff *res;
4759 isl_pw_multi_aff *pma;
4760 int (*fn)(void **entry, void *user);
4763 /* Given an isl_pw_multi_aff from upma1, store it in data->pma
4764 * and call data->fn for each isl_pw_multi_aff in data->upma2.
4766 static int bin_entry(void **entry, void *user)
4768 struct isl_union_pw_multi_aff_bin_data *data = user;
4769 isl_pw_multi_aff *pma = *entry;
4772 if (isl_hash_table_foreach(data->upma2->dim->ctx, &data->upma2->table,
4773 data->fn, data) < 0)
4779 /* Call "fn" on each pair of isl_pw_multi_affs in "upma1" and "upma2".
4780 * The isl_pw_multi_aff from upma1 is stored in data->pma (where data is
4781 * passed as user field) and the isl_pw_multi_aff from upma2 is available
4782 * as *entry. The callback should adjust data->res if desired.
4784 static __isl_give isl_union_pw_multi_aff *bin_op(
4785 __isl_take isl_union_pw_multi_aff *upma1,
4786 __isl_take isl_union_pw_multi_aff *upma2,
4787 int (*fn)(void **entry, void *user))
4790 struct isl_union_pw_multi_aff_bin_data data = { NULL, NULL, NULL, fn };
4792 space = isl_union_pw_multi_aff_get_space(upma2);
4793 upma1 = isl_union_pw_multi_aff_align_params(upma1, space);
4794 space = isl_union_pw_multi_aff_get_space(upma1);
4795 upma2 = isl_union_pw_multi_aff_align_params(upma2, space);
4797 if (!upma1 || !upma2)
4801 data.res = isl_union_pw_multi_aff_alloc(isl_space_copy(upma1->dim),
4803 if (isl_hash_table_foreach(upma1->dim->ctx, &upma1->table,
4804 &bin_entry, &data) < 0)
4807 isl_union_pw_multi_aff_free(upma1);
4808 isl_union_pw_multi_aff_free(upma2);
4811 isl_union_pw_multi_aff_free(upma1);
4812 isl_union_pw_multi_aff_free(upma2);
4813 isl_union_pw_multi_aff_free(data.res);
4817 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
4818 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
4820 static __isl_give isl_pw_multi_aff *pw_multi_aff_range_product(
4821 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4825 space = isl_space_range_product(isl_pw_multi_aff_get_space(pma1),
4826 isl_pw_multi_aff_get_space(pma2));
4827 return isl_pw_multi_aff_on_shared_domain_in(pma1, pma2, space,
4828 &isl_multi_aff_range_product);
4831 /* Given two isl_pw_multi_affs A -> B and C -> D,
4832 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
4834 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_range_product(
4835 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4837 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4838 &pw_multi_aff_range_product);
4841 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
4842 * construct an isl_pw_multi_aff (A * C) -> (B, D).
4844 static __isl_give isl_pw_multi_aff *pw_multi_aff_flat_range_product(
4845 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4849 space = isl_space_range_product(isl_pw_multi_aff_get_space(pma1),
4850 isl_pw_multi_aff_get_space(pma2));
4851 space = isl_space_flatten_range(space);
4852 return isl_pw_multi_aff_on_shared_domain_in(pma1, pma2, space,
4853 &isl_multi_aff_flat_range_product);
4856 /* Given two isl_pw_multi_affs A -> B and C -> D,
4857 * construct an isl_pw_multi_aff (A * C) -> (B, D).
4859 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_flat_range_product(
4860 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4862 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4863 &pw_multi_aff_flat_range_product);
4866 /* If data->pma and *entry have the same domain space, then compute
4867 * their flat range product and the result to data->res.
4869 static int flat_range_product_entry(void **entry, void *user)
4871 struct isl_union_pw_multi_aff_bin_data *data = user;
4872 isl_pw_multi_aff *pma2 = *entry;
4874 if (!isl_space_tuple_match(data->pma->dim, isl_dim_in,
4875 pma2->dim, isl_dim_in))
4878 pma2 = isl_pw_multi_aff_flat_range_product(
4879 isl_pw_multi_aff_copy(data->pma),
4880 isl_pw_multi_aff_copy(pma2));
4882 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma2);
4887 /* Given two isl_union_pw_multi_affs A -> B and C -> D,
4888 * construct an isl_union_pw_multi_aff (A * C) -> (B, D).
4890 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_flat_range_product(
4891 __isl_take isl_union_pw_multi_aff *upma1,
4892 __isl_take isl_union_pw_multi_aff *upma2)
4894 return bin_op(upma1, upma2, &flat_range_product_entry);
4897 /* Replace the affine expressions at position "pos" in "pma" by "pa".
4898 * The parameters are assumed to have been aligned.
4900 * The implementation essentially performs an isl_pw_*_on_shared_domain,
4901 * except that it works on two different isl_pw_* types.
4903 static __isl_give isl_pw_multi_aff *pw_multi_aff_set_pw_aff(
4904 __isl_take isl_pw_multi_aff *pma, unsigned pos,
4905 __isl_take isl_pw_aff *pa)
4908 isl_pw_multi_aff *res = NULL;
4913 if (!isl_space_tuple_match(pma->dim, isl_dim_in, pa->dim, isl_dim_in))
4914 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
4915 "domains don't match", goto error);
4916 if (pos >= isl_pw_multi_aff_dim(pma, isl_dim_out))
4917 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
4918 "index out of bounds", goto error);
4921 res = isl_pw_multi_aff_alloc_size(isl_pw_multi_aff_get_space(pma), n);
4923 for (i = 0; i < pma->n; ++i) {
4924 for (j = 0; j < pa->n; ++j) {
4926 isl_multi_aff *res_ij;
4929 common = isl_set_intersect(isl_set_copy(pma->p[i].set),
4930 isl_set_copy(pa->p[j].set));
4931 empty = isl_set_plain_is_empty(common);
4932 if (empty < 0 || empty) {
4933 isl_set_free(common);
4939 res_ij = isl_multi_aff_set_aff(
4940 isl_multi_aff_copy(pma->p[i].maff), pos,
4941 isl_aff_copy(pa->p[j].aff));
4942 res_ij = isl_multi_aff_gist(res_ij,
4943 isl_set_copy(common));
4945 res = isl_pw_multi_aff_add_piece(res, common, res_ij);
4949 isl_pw_multi_aff_free(pma);
4950 isl_pw_aff_free(pa);
4953 isl_pw_multi_aff_free(pma);
4954 isl_pw_aff_free(pa);
4955 return isl_pw_multi_aff_free(res);
4958 /* Replace the affine expressions at position "pos" in "pma" by "pa".
4960 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_set_pw_aff(
4961 __isl_take isl_pw_multi_aff *pma, unsigned pos,
4962 __isl_take isl_pw_aff *pa)
4966 if (isl_space_match(pma->dim, isl_dim_param, pa->dim, isl_dim_param))
4967 return pw_multi_aff_set_pw_aff(pma, pos, pa);
4968 if (!isl_space_has_named_params(pma->dim) ||
4969 !isl_space_has_named_params(pa->dim))
4970 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
4971 "unaligned unnamed parameters", goto error);
4972 pma = isl_pw_multi_aff_align_params(pma, isl_pw_aff_get_space(pa));
4973 pa = isl_pw_aff_align_params(pa, isl_pw_multi_aff_get_space(pma));
4974 return pw_multi_aff_set_pw_aff(pma, pos, pa);
4976 isl_pw_multi_aff_free(pma);
4977 isl_pw_aff_free(pa);
4981 /* Check that the domain space of "pa" matches "space".
4983 * Return 0 on success and -1 on error.
4985 int isl_pw_aff_check_match_domain_space(__isl_keep isl_pw_aff *pa,
4986 __isl_keep isl_space *space)
4988 isl_space *pa_space;
4994 pa_space = isl_pw_aff_get_space(pa);
4996 match = isl_space_match(space, isl_dim_param, pa_space, isl_dim_param);
5000 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
5001 "parameters don't match", goto error);
5002 match = isl_space_tuple_match(space, isl_dim_in, pa_space, isl_dim_in);
5006 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
5007 "domains don't match", goto error);
5008 isl_space_free(pa_space);
5011 isl_space_free(pa_space);
5018 #include <isl_multi_templ.c>
5020 /* Scale the first elements of "ma" by the corresponding elements of "vec".
5022 __isl_give isl_multi_aff *isl_multi_aff_scale_vec(__isl_take isl_multi_aff *ma,
5023 __isl_take isl_vec *vec)
5031 n = isl_multi_aff_dim(ma, isl_dim_out);
5032 if (isl_vec_size(vec) < n)
5033 n = isl_vec_size(vec);
5036 for (i = 0; i < n; ++i) {
5039 isl_vec_get_element(vec, i, &v);
5041 aff = isl_multi_aff_get_aff(ma, i);
5042 aff = isl_aff_scale(aff, v);
5043 ma = isl_multi_aff_set_aff(ma, i, aff);
5051 isl_multi_aff_free(ma);
5055 /* Scale the first elements of "pma" by the corresponding elements of "vec".
5057 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_scale_vec(
5058 __isl_take isl_pw_multi_aff *pma, __isl_take isl_vec *v)
5062 pma = isl_pw_multi_aff_cow(pma);
5066 for (i = 0; i < pma->n; ++i) {
5067 pma->p[i].maff = isl_multi_aff_scale_vec(pma->p[i].maff,
5069 if (!pma->p[i].maff)
5077 isl_pw_multi_aff_free(pma);
5081 /* This function is called for each entry of an isl_union_pw_multi_aff.
5082 * Replace the entry by the result of applying isl_pw_multi_aff_scale_vec
5083 * to the original entry with the isl_vec in "user" as extra argument.
5085 static int union_pw_multi_aff_scale_vec_entry(void **entry, void *user)
5087 isl_pw_multi_aff **pma = (isl_pw_multi_aff **) entry;
5090 *pma = isl_pw_multi_aff_scale_vec(*pma, isl_vec_copy(v));
5097 /* Scale the first elements of "upma" by the corresponding elements of "vec".
5099 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_scale_vec(
5100 __isl_take isl_union_pw_multi_aff *upma, __isl_take isl_vec *v)
5102 upma = isl_union_pw_multi_aff_cow(upma);
5106 if (isl_hash_table_foreach(upma->dim->ctx, &upma->table,
5107 &union_pw_multi_aff_scale_vec_entry, v) < 0)
5114 isl_union_pw_multi_aff_free(upma);