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 /* Return the common denominator of "aff".
414 __isl_give isl_val *isl_aff_get_denominator_val(__isl_keep isl_aff *aff)
421 ctx = isl_aff_get_ctx(aff);
422 return isl_val_int_from_isl_int(ctx, aff->v->el[0]);
425 int isl_aff_get_constant(__isl_keep isl_aff *aff, isl_int *v)
429 isl_int_set(*v, aff->v->el[1]);
433 /* Return the constant term of "aff".
435 __isl_give isl_val *isl_aff_get_constant_val(__isl_keep isl_aff *aff)
443 ctx = isl_aff_get_ctx(aff);
444 v = isl_val_rat_from_isl_int(ctx, aff->v->el[1], aff->v->el[0]);
445 return isl_val_normalize(v);
448 int isl_aff_get_coefficient(__isl_keep isl_aff *aff,
449 enum isl_dim_type type, int pos, isl_int *v)
454 if (type == isl_dim_out)
455 isl_die(aff->v->ctx, isl_error_invalid,
456 "output/set dimension does not have a coefficient",
458 if (type == isl_dim_in)
461 if (pos >= isl_local_space_dim(aff->ls, type))
462 isl_die(aff->v->ctx, isl_error_invalid,
463 "position out of bounds", return -1);
465 pos += isl_local_space_offset(aff->ls, type);
466 isl_int_set(*v, aff->v->el[1 + pos]);
471 /* Return the coefficient of the variable of type "type" at position "pos"
474 __isl_give isl_val *isl_aff_get_coefficient_val(__isl_keep isl_aff *aff,
475 enum isl_dim_type type, int pos)
483 ctx = isl_aff_get_ctx(aff);
484 if (type == isl_dim_out)
485 isl_die(ctx, isl_error_invalid,
486 "output/set dimension does not have a coefficient",
488 if (type == isl_dim_in)
491 if (pos >= isl_local_space_dim(aff->ls, type))
492 isl_die(ctx, isl_error_invalid,
493 "position out of bounds", return NULL);
495 pos += isl_local_space_offset(aff->ls, type);
496 v = isl_val_rat_from_isl_int(ctx, aff->v->el[1 + pos], aff->v->el[0]);
497 return isl_val_normalize(v);
500 __isl_give isl_aff *isl_aff_set_denominator(__isl_take isl_aff *aff, isl_int v)
502 aff = isl_aff_cow(aff);
506 aff->v = isl_vec_cow(aff->v);
508 return isl_aff_free(aff);
510 isl_int_set(aff->v->el[0], v);
515 __isl_give isl_aff *isl_aff_set_constant(__isl_take isl_aff *aff, isl_int v)
517 aff = isl_aff_cow(aff);
521 aff->v = isl_vec_cow(aff->v);
523 return isl_aff_free(aff);
525 isl_int_set(aff->v->el[1], v);
530 /* Replace the constant term of "aff" by "v".
532 __isl_give isl_aff *isl_aff_set_constant_val(__isl_take isl_aff *aff,
533 __isl_take isl_val *v)
538 if (!isl_val_is_rat(v))
539 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
540 "expecting rational value", goto error);
542 if (isl_int_eq(aff->v->el[1], v->n) &&
543 isl_int_eq(aff->v->el[0], v->d)) {
548 aff = isl_aff_cow(aff);
551 aff->v = isl_vec_cow(aff->v);
555 if (isl_int_eq(aff->v->el[0], v->d)) {
556 isl_int_set(aff->v->el[1], v->n);
557 } else if (isl_int_is_one(v->d)) {
558 isl_int_mul(aff->v->el[1], aff->v->el[0], v->n);
560 isl_seq_scale(aff->v->el + 1,
561 aff->v->el + 1, v->d, aff->v->size - 1);
562 isl_int_mul(aff->v->el[1], aff->v->el[0], v->n);
563 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
564 aff->v = isl_vec_normalize(aff->v);
577 __isl_give isl_aff *isl_aff_add_constant(__isl_take isl_aff *aff, isl_int v)
579 if (isl_int_is_zero(v))
582 aff = isl_aff_cow(aff);
586 aff->v = isl_vec_cow(aff->v);
588 return isl_aff_free(aff);
590 isl_int_addmul(aff->v->el[1], aff->v->el[0], v);
595 /* Add "v" to the constant term of "aff".
597 __isl_give isl_aff *isl_aff_add_constant_val(__isl_take isl_aff *aff,
598 __isl_take isl_val *v)
603 if (isl_val_is_zero(v)) {
608 if (!isl_val_is_rat(v))
609 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
610 "expecting rational value", goto error);
612 aff = isl_aff_cow(aff);
616 aff->v = isl_vec_cow(aff->v);
620 if (isl_int_is_one(v->d)) {
621 isl_int_addmul(aff->v->el[1], aff->v->el[0], v->n);
622 } else if (isl_int_eq(aff->v->el[0], v->d)) {
623 isl_int_add(aff->v->el[1], aff->v->el[1], v->n);
624 aff->v = isl_vec_normalize(aff->v);
628 isl_seq_scale(aff->v->el + 1,
629 aff->v->el + 1, v->d, aff->v->size - 1);
630 isl_int_addmul(aff->v->el[1], aff->v->el[0], v->n);
631 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
632 aff->v = isl_vec_normalize(aff->v);
645 __isl_give isl_aff *isl_aff_add_constant_si(__isl_take isl_aff *aff, int v)
650 isl_int_set_si(t, v);
651 aff = isl_aff_add_constant(aff, t);
657 /* Add "v" to the numerator of the constant term of "aff".
659 __isl_give isl_aff *isl_aff_add_constant_num(__isl_take isl_aff *aff, isl_int v)
661 if (isl_int_is_zero(v))
664 aff = isl_aff_cow(aff);
668 aff->v = isl_vec_cow(aff->v);
670 return isl_aff_free(aff);
672 isl_int_add(aff->v->el[1], aff->v->el[1], v);
677 /* Add "v" to the numerator of the constant term of "aff".
679 __isl_give isl_aff *isl_aff_add_constant_num_si(__isl_take isl_aff *aff, int v)
687 isl_int_set_si(t, v);
688 aff = isl_aff_add_constant_num(aff, t);
694 __isl_give isl_aff *isl_aff_set_constant_si(__isl_take isl_aff *aff, int v)
696 aff = isl_aff_cow(aff);
700 aff->v = isl_vec_cow(aff->v);
702 return isl_aff_free(aff);
704 isl_int_set_si(aff->v->el[1], v);
709 __isl_give isl_aff *isl_aff_set_coefficient(__isl_take isl_aff *aff,
710 enum isl_dim_type type, int pos, isl_int v)
715 if (type == isl_dim_out)
716 isl_die(aff->v->ctx, isl_error_invalid,
717 "output/set dimension does not have a coefficient",
718 return isl_aff_free(aff));
719 if (type == isl_dim_in)
722 if (pos >= isl_local_space_dim(aff->ls, type))
723 isl_die(aff->v->ctx, isl_error_invalid,
724 "position out of bounds", return isl_aff_free(aff));
726 aff = isl_aff_cow(aff);
730 aff->v = isl_vec_cow(aff->v);
732 return isl_aff_free(aff);
734 pos += isl_local_space_offset(aff->ls, type);
735 isl_int_set(aff->v->el[1 + pos], v);
740 __isl_give isl_aff *isl_aff_set_coefficient_si(__isl_take isl_aff *aff,
741 enum isl_dim_type type, int pos, int v)
746 if (type == isl_dim_out)
747 isl_die(aff->v->ctx, isl_error_invalid,
748 "output/set dimension does not have a coefficient",
749 return isl_aff_free(aff));
750 if (type == isl_dim_in)
753 if (pos >= isl_local_space_dim(aff->ls, type))
754 isl_die(aff->v->ctx, isl_error_invalid,
755 "position out of bounds", return isl_aff_free(aff));
757 aff = isl_aff_cow(aff);
761 aff->v = isl_vec_cow(aff->v);
763 return isl_aff_free(aff);
765 pos += isl_local_space_offset(aff->ls, type);
766 isl_int_set_si(aff->v->el[1 + pos], v);
771 /* Replace the coefficient of the variable of type "type" at position "pos"
774 __isl_give isl_aff *isl_aff_set_coefficient_val(__isl_take isl_aff *aff,
775 enum isl_dim_type type, int pos, __isl_take isl_val *v)
780 if (type == isl_dim_out)
781 isl_die(aff->v->ctx, isl_error_invalid,
782 "output/set dimension does not have a coefficient",
784 if (type == isl_dim_in)
787 if (pos >= isl_local_space_dim(aff->ls, type))
788 isl_die(aff->v->ctx, isl_error_invalid,
789 "position out of bounds", goto error);
791 if (!isl_val_is_rat(v))
792 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
793 "expecting rational value", goto error);
795 pos += isl_local_space_offset(aff->ls, type);
796 if (isl_int_eq(aff->v->el[1 + pos], v->n) &&
797 isl_int_eq(aff->v->el[0], v->d)) {
802 aff = isl_aff_cow(aff);
805 aff->v = isl_vec_cow(aff->v);
809 if (isl_int_eq(aff->v->el[0], v->d)) {
810 isl_int_set(aff->v->el[1 + pos], v->n);
811 } else if (isl_int_is_one(v->d)) {
812 isl_int_mul(aff->v->el[1 + pos], aff->v->el[0], v->n);
814 isl_seq_scale(aff->v->el + 1,
815 aff->v->el + 1, v->d, aff->v->size - 1);
816 isl_int_mul(aff->v->el[1 + pos], aff->v->el[0], v->n);
817 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
818 aff->v = isl_vec_normalize(aff->v);
831 __isl_give isl_aff *isl_aff_add_coefficient(__isl_take isl_aff *aff,
832 enum isl_dim_type type, int pos, isl_int v)
837 if (type == isl_dim_out)
838 isl_die(aff->v->ctx, isl_error_invalid,
839 "output/set dimension does not have a coefficient",
840 return isl_aff_free(aff));
841 if (type == isl_dim_in)
844 if (pos >= isl_local_space_dim(aff->ls, type))
845 isl_die(aff->v->ctx, isl_error_invalid,
846 "position out of bounds", return isl_aff_free(aff));
848 aff = isl_aff_cow(aff);
852 aff->v = isl_vec_cow(aff->v);
854 return isl_aff_free(aff);
856 pos += isl_local_space_offset(aff->ls, type);
857 isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v);
862 /* Add "v" to the coefficient of the variable of type "type"
863 * at position "pos" of "aff".
865 __isl_give isl_aff *isl_aff_add_coefficient_val(__isl_take isl_aff *aff,
866 enum isl_dim_type type, int pos, __isl_take isl_val *v)
871 if (isl_val_is_zero(v)) {
876 if (type == isl_dim_out)
877 isl_die(aff->v->ctx, isl_error_invalid,
878 "output/set dimension does not have a coefficient",
880 if (type == isl_dim_in)
883 if (pos >= isl_local_space_dim(aff->ls, type))
884 isl_die(aff->v->ctx, isl_error_invalid,
885 "position out of bounds", goto error);
887 if (!isl_val_is_rat(v))
888 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
889 "expecting rational value", goto error);
891 aff = isl_aff_cow(aff);
895 aff->v = isl_vec_cow(aff->v);
899 pos += isl_local_space_offset(aff->ls, type);
900 if (isl_int_is_one(v->d)) {
901 isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v->n);
902 } else if (isl_int_eq(aff->v->el[0], v->d)) {
903 isl_int_add(aff->v->el[1 + pos], aff->v->el[1 + pos], v->n);
904 aff->v = isl_vec_normalize(aff->v);
908 isl_seq_scale(aff->v->el + 1,
909 aff->v->el + 1, v->d, aff->v->size - 1);
910 isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v->n);
911 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
912 aff->v = isl_vec_normalize(aff->v);
925 __isl_give isl_aff *isl_aff_add_coefficient_si(__isl_take isl_aff *aff,
926 enum isl_dim_type type, int pos, int v)
931 isl_int_set_si(t, v);
932 aff = isl_aff_add_coefficient(aff, type, pos, t);
938 __isl_give isl_aff *isl_aff_get_div(__isl_keep isl_aff *aff, int pos)
943 return isl_local_space_get_div(aff->ls, pos);
946 __isl_give isl_aff *isl_aff_neg(__isl_take isl_aff *aff)
948 aff = isl_aff_cow(aff);
951 aff->v = isl_vec_cow(aff->v);
953 return isl_aff_free(aff);
955 isl_seq_neg(aff->v->el + 1, aff->v->el + 1, aff->v->size - 1);
960 /* Remove divs from the local space that do not appear in the affine
962 * We currently only remove divs at the end.
963 * Some intermediate divs may also not appear directly in the affine
964 * expression, but we would also need to check that no other divs are
965 * defined in terms of them.
967 __isl_give isl_aff *isl_aff_remove_unused_divs( __isl_take isl_aff *aff)
976 n = isl_local_space_dim(aff->ls, isl_dim_div);
977 off = isl_local_space_offset(aff->ls, isl_dim_div);
979 pos = isl_seq_last_non_zero(aff->v->el + 1 + off, n) + 1;
983 aff = isl_aff_cow(aff);
987 aff->ls = isl_local_space_drop_dims(aff->ls, isl_dim_div, pos, n - pos);
988 aff->v = isl_vec_drop_els(aff->v, 1 + off + pos, n - pos);
989 if (!aff->ls || !aff->v)
990 return isl_aff_free(aff);
995 /* Given two affine expressions "p" of length p_len (including the
996 * denominator and the constant term) and "subs" of length subs_len,
997 * plug in "subs" for the variable at position "pos".
998 * The variables of "subs" and "p" are assumed to match up to subs_len,
999 * but "p" may have additional variables.
1000 * "v" is an initialized isl_int that can be used internally.
1002 * In particular, if "p" represents the expression
1006 * with i the variable at position "pos" and "subs" represents the expression
1010 * then the result represents the expression
1015 void isl_seq_substitute(isl_int *p, int pos, isl_int *subs,
1016 int p_len, int subs_len, isl_int v)
1018 isl_int_set(v, p[1 + pos]);
1019 isl_int_set_si(p[1 + pos], 0);
1020 isl_seq_combine(p + 1, subs[0], p + 1, v, subs + 1, subs_len - 1);
1021 isl_seq_scale(p + subs_len, p + subs_len, subs[0], p_len - subs_len);
1022 isl_int_mul(p[0], p[0], subs[0]);
1025 /* Look for any divs in the aff->ls with a denominator equal to one
1026 * and plug them into the affine expression and any subsequent divs
1027 * that may reference the div.
1029 static __isl_give isl_aff *plug_in_integral_divs(__isl_take isl_aff *aff)
1035 isl_local_space *ls;
1041 n = isl_local_space_dim(aff->ls, isl_dim_div);
1043 for (i = 0; i < n; ++i) {
1044 if (!isl_int_is_one(aff->ls->div->row[i][0]))
1046 ls = isl_local_space_copy(aff->ls);
1047 ls = isl_local_space_substitute_seq(ls, isl_dim_div, i,
1048 aff->ls->div->row[i], len, i + 1, n - (i + 1));
1049 vec = isl_vec_copy(aff->v);
1050 vec = isl_vec_cow(vec);
1056 pos = isl_local_space_offset(aff->ls, isl_dim_div) + i;
1057 isl_seq_substitute(vec->el, pos, aff->ls->div->row[i],
1062 isl_vec_free(aff->v);
1064 isl_local_space_free(aff->ls);
1071 isl_local_space_free(ls);
1072 return isl_aff_free(aff);
1075 /* Look for any divs j that appear with a unit coefficient inside
1076 * the definitions of other divs i and plug them into the definitions
1079 * In particular, an expression of the form
1081 * floor((f(..) + floor(g(..)/n))/m)
1085 * floor((n * f(..) + g(..))/(n * m))
1087 * This simplification is correct because we can move the expression
1088 * f(..) into the inner floor in the original expression to obtain
1090 * floor(floor((n * f(..) + g(..))/n)/m)
1092 * from which we can derive the simplified expression.
1094 static __isl_give isl_aff *plug_in_unit_divs(__isl_take isl_aff *aff)
1102 n = isl_local_space_dim(aff->ls, isl_dim_div);
1103 off = isl_local_space_offset(aff->ls, isl_dim_div);
1104 for (i = 1; i < n; ++i) {
1105 for (j = 0; j < i; ++j) {
1106 if (!isl_int_is_one(aff->ls->div->row[i][1 + off + j]))
1108 aff->ls = isl_local_space_substitute_seq(aff->ls,
1109 isl_dim_div, j, aff->ls->div->row[j],
1110 aff->v->size, i, 1);
1112 return isl_aff_free(aff);
1119 /* Swap divs "a" and "b" in "aff", which is assumed to be non-NULL.
1121 * Even though this function is only called on isl_affs with a single
1122 * reference, we are careful to only change aff->v and aff->ls together.
1124 static __isl_give isl_aff *swap_div(__isl_take isl_aff *aff, int a, int b)
1126 unsigned off = isl_local_space_offset(aff->ls, isl_dim_div);
1127 isl_local_space *ls;
1130 ls = isl_local_space_copy(aff->ls);
1131 ls = isl_local_space_swap_div(ls, a, b);
1132 v = isl_vec_copy(aff->v);
1137 isl_int_swap(v->el[1 + off + a], v->el[1 + off + b]);
1138 isl_vec_free(aff->v);
1140 isl_local_space_free(aff->ls);
1146 isl_local_space_free(ls);
1147 return isl_aff_free(aff);
1150 /* Merge divs "a" and "b" in "aff", which is assumed to be non-NULL.
1152 * We currently do not actually remove div "b", but simply add its
1153 * coefficient to that of "a" and then zero it out.
1155 static __isl_give isl_aff *merge_divs(__isl_take isl_aff *aff, int a, int b)
1157 unsigned off = isl_local_space_offset(aff->ls, isl_dim_div);
1159 if (isl_int_is_zero(aff->v->el[1 + off + b]))
1162 aff->v = isl_vec_cow(aff->v);
1164 return isl_aff_free(aff);
1166 isl_int_add(aff->v->el[1 + off + a],
1167 aff->v->el[1 + off + a], aff->v->el[1 + off + b]);
1168 isl_int_set_si(aff->v->el[1 + off + b], 0);
1173 /* Sort the divs in the local space of "aff" according to
1174 * the comparison function "cmp_row" in isl_local_space.c,
1175 * combining the coefficients of identical divs.
1177 * Reordering divs does not change the semantics of "aff",
1178 * so there is no need to call isl_aff_cow.
1179 * Moreover, this function is currently only called on isl_affs
1180 * with a single reference.
1182 static __isl_give isl_aff *sort_divs(__isl_take isl_aff *aff)
1190 off = isl_local_space_offset(aff->ls, isl_dim_div);
1191 n = isl_aff_dim(aff, isl_dim_div);
1192 for (i = 1; i < n; ++i) {
1193 for (j = i - 1; j >= 0; --j) {
1194 int cmp = isl_mat_cmp_div(aff->ls->div, j, j + 1);
1198 aff = merge_divs(aff, j, j + 1);
1200 aff = swap_div(aff, j, j + 1);
1209 /* Normalize the representation of "aff".
1211 * This function should only be called of "new" isl_affs, i.e.,
1212 * with only a single reference. We therefore do not need to
1213 * worry about affecting other instances.
1215 __isl_give isl_aff *isl_aff_normalize(__isl_take isl_aff *aff)
1219 aff->v = isl_vec_normalize(aff->v);
1221 return isl_aff_free(aff);
1222 aff = plug_in_integral_divs(aff);
1223 aff = plug_in_unit_divs(aff);
1224 aff = sort_divs(aff);
1225 aff = isl_aff_remove_unused_divs(aff);
1229 /* Given f, return floor(f).
1230 * If f is an integer expression, then just return f.
1231 * If f is a constant, then return the constant floor(f).
1232 * Otherwise, if f = g/m, write g = q m + r,
1233 * create a new div d = [r/m] and return the expression q + d.
1234 * The coefficients in r are taken to lie between -m/2 and m/2.
1236 __isl_give isl_aff *isl_aff_floor(__isl_take isl_aff *aff)
1246 if (isl_int_is_one(aff->v->el[0]))
1249 aff = isl_aff_cow(aff);
1253 aff->v = isl_vec_cow(aff->v);
1255 return isl_aff_free(aff);
1257 if (isl_aff_is_cst(aff)) {
1258 isl_int_fdiv_q(aff->v->el[1], aff->v->el[1], aff->v->el[0]);
1259 isl_int_set_si(aff->v->el[0], 1);
1263 div = isl_vec_copy(aff->v);
1264 div = isl_vec_cow(div);
1266 return isl_aff_free(aff);
1268 ctx = isl_aff_get_ctx(aff);
1269 isl_int_fdiv_q(aff->v->el[0], aff->v->el[0], ctx->two);
1270 for (i = 1; i < aff->v->size; ++i) {
1271 isl_int_fdiv_r(div->el[i], div->el[i], div->el[0]);
1272 isl_int_fdiv_q(aff->v->el[i], aff->v->el[i], div->el[0]);
1273 if (isl_int_gt(div->el[i], aff->v->el[0])) {
1274 isl_int_sub(div->el[i], div->el[i], div->el[0]);
1275 isl_int_add_ui(aff->v->el[i], aff->v->el[i], 1);
1279 aff->ls = isl_local_space_add_div(aff->ls, div);
1281 return isl_aff_free(aff);
1283 size = aff->v->size;
1284 aff->v = isl_vec_extend(aff->v, size + 1);
1286 return isl_aff_free(aff);
1287 isl_int_set_si(aff->v->el[0], 1);
1288 isl_int_set_si(aff->v->el[size], 1);
1290 aff = isl_aff_normalize(aff);
1297 * aff mod m = aff - m * floor(aff/m)
1299 __isl_give isl_aff *isl_aff_mod(__isl_take isl_aff *aff, isl_int m)
1303 res = isl_aff_copy(aff);
1304 aff = isl_aff_scale_down(aff, m);
1305 aff = isl_aff_floor(aff);
1306 aff = isl_aff_scale(aff, m);
1307 res = isl_aff_sub(res, aff);
1314 * pwaff mod m = pwaff - m * floor(pwaff/m)
1316 __isl_give isl_pw_aff *isl_pw_aff_mod(__isl_take isl_pw_aff *pwaff, isl_int m)
1320 res = isl_pw_aff_copy(pwaff);
1321 pwaff = isl_pw_aff_scale_down(pwaff, m);
1322 pwaff = isl_pw_aff_floor(pwaff);
1323 pwaff = isl_pw_aff_scale(pwaff, m);
1324 res = isl_pw_aff_sub(res, pwaff);
1329 /* Given f, return ceil(f).
1330 * If f is an integer expression, then just return f.
1331 * Otherwise, let f be the expression
1337 * floor((e + m - 1)/m)
1339 __isl_give isl_aff *isl_aff_ceil(__isl_take isl_aff *aff)
1344 if (isl_int_is_one(aff->v->el[0]))
1347 aff = isl_aff_cow(aff);
1350 aff->v = isl_vec_cow(aff->v);
1352 return isl_aff_free(aff);
1354 isl_int_add(aff->v->el[1], aff->v->el[1], aff->v->el[0]);
1355 isl_int_sub_ui(aff->v->el[1], aff->v->el[1], 1);
1356 aff = isl_aff_floor(aff);
1361 /* Apply the expansion computed by isl_merge_divs.
1362 * The expansion itself is given by "exp" while the resulting
1363 * list of divs is given by "div".
1365 __isl_give isl_aff *isl_aff_expand_divs( __isl_take isl_aff *aff,
1366 __isl_take isl_mat *div, int *exp)
1373 aff = isl_aff_cow(aff);
1377 old_n_div = isl_local_space_dim(aff->ls, isl_dim_div);
1378 new_n_div = isl_mat_rows(div);
1379 if (new_n_div < old_n_div)
1380 isl_die(isl_mat_get_ctx(div), isl_error_invalid,
1381 "not an expansion", goto error);
1383 aff->v = isl_vec_extend(aff->v, aff->v->size + new_n_div - old_n_div);
1387 offset = 1 + isl_local_space_offset(aff->ls, isl_dim_div);
1389 for (i = new_n_div - 1; i >= 0; --i) {
1390 if (j >= 0 && exp[j] == i) {
1392 isl_int_swap(aff->v->el[offset + i],
1393 aff->v->el[offset + j]);
1396 isl_int_set_si(aff->v->el[offset + i], 0);
1399 aff->ls = isl_local_space_replace_divs(aff->ls, isl_mat_copy(div));
1410 /* Add two affine expressions that live in the same local space.
1412 static __isl_give isl_aff *add_expanded(__isl_take isl_aff *aff1,
1413 __isl_take isl_aff *aff2)
1417 aff1 = isl_aff_cow(aff1);
1421 aff1->v = isl_vec_cow(aff1->v);
1427 isl_int_gcd(gcd, aff1->v->el[0], aff2->v->el[0]);
1428 isl_int_divexact(f, aff2->v->el[0], gcd);
1429 isl_seq_scale(aff1->v->el + 1, aff1->v->el + 1, f, aff1->v->size - 1);
1430 isl_int_divexact(f, aff1->v->el[0], gcd);
1431 isl_seq_addmul(aff1->v->el + 1, f, aff2->v->el + 1, aff1->v->size - 1);
1432 isl_int_divexact(f, aff2->v->el[0], gcd);
1433 isl_int_mul(aff1->v->el[0], aff1->v->el[0], f);
1445 __isl_give isl_aff *isl_aff_add(__isl_take isl_aff *aff1,
1446 __isl_take isl_aff *aff2)
1456 ctx = isl_aff_get_ctx(aff1);
1457 if (!isl_space_is_equal(aff1->ls->dim, aff2->ls->dim))
1458 isl_die(ctx, isl_error_invalid,
1459 "spaces don't match", goto error);
1461 if (aff1->ls->div->n_row == 0 && aff2->ls->div->n_row == 0)
1462 return add_expanded(aff1, aff2);
1464 exp1 = isl_alloc_array(ctx, int, aff1->ls->div->n_row);
1465 exp2 = isl_alloc_array(ctx, int, aff2->ls->div->n_row);
1469 div = isl_merge_divs(aff1->ls->div, aff2->ls->div, exp1, exp2);
1470 aff1 = isl_aff_expand_divs(aff1, isl_mat_copy(div), exp1);
1471 aff2 = isl_aff_expand_divs(aff2, div, exp2);
1475 return add_expanded(aff1, aff2);
1484 __isl_give isl_aff *isl_aff_sub(__isl_take isl_aff *aff1,
1485 __isl_take isl_aff *aff2)
1487 return isl_aff_add(aff1, isl_aff_neg(aff2));
1490 __isl_give isl_aff *isl_aff_scale(__isl_take isl_aff *aff, isl_int f)
1494 if (isl_int_is_one(f))
1497 aff = isl_aff_cow(aff);
1500 aff->v = isl_vec_cow(aff->v);
1502 return isl_aff_free(aff);
1504 if (isl_int_is_pos(f) && isl_int_is_divisible_by(aff->v->el[0], f)) {
1505 isl_int_divexact(aff->v->el[0], aff->v->el[0], f);
1510 isl_int_gcd(gcd, aff->v->el[0], f);
1511 isl_int_divexact(aff->v->el[0], aff->v->el[0], gcd);
1512 isl_int_divexact(gcd, f, gcd);
1513 isl_seq_scale(aff->v->el + 1, aff->v->el + 1, gcd, aff->v->size - 1);
1519 __isl_give isl_aff *isl_aff_scale_down(__isl_take isl_aff *aff, isl_int f)
1523 if (isl_int_is_one(f))
1526 aff = isl_aff_cow(aff);
1530 if (isl_int_is_zero(f))
1531 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1532 "cannot scale down by zero", return isl_aff_free(aff));
1534 aff->v = isl_vec_cow(aff->v);
1536 return isl_aff_free(aff);
1539 isl_seq_gcd(aff->v->el + 1, aff->v->size - 1, &gcd);
1540 isl_int_gcd(gcd, gcd, f);
1541 isl_seq_scale_down(aff->v->el + 1, aff->v->el + 1, gcd, aff->v->size - 1);
1542 isl_int_divexact(gcd, f, gcd);
1543 isl_int_mul(aff->v->el[0], aff->v->el[0], gcd);
1549 __isl_give isl_aff *isl_aff_scale_down_ui(__isl_take isl_aff *aff, unsigned f)
1557 isl_int_set_ui(v, f);
1558 aff = isl_aff_scale_down(aff, v);
1564 __isl_give isl_aff *isl_aff_set_dim_name(__isl_take isl_aff *aff,
1565 enum isl_dim_type type, unsigned pos, const char *s)
1567 aff = isl_aff_cow(aff);
1570 if (type == isl_dim_out)
1571 isl_die(aff->v->ctx, isl_error_invalid,
1572 "cannot set name of output/set dimension",
1573 return isl_aff_free(aff));
1574 if (type == isl_dim_in)
1576 aff->ls = isl_local_space_set_dim_name(aff->ls, type, pos, s);
1578 return isl_aff_free(aff);
1583 __isl_give isl_aff *isl_aff_set_dim_id(__isl_take isl_aff *aff,
1584 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
1586 aff = isl_aff_cow(aff);
1588 return isl_id_free(id);
1589 if (type == isl_dim_out)
1590 isl_die(aff->v->ctx, isl_error_invalid,
1591 "cannot set name of output/set dimension",
1593 if (type == isl_dim_in)
1595 aff->ls = isl_local_space_set_dim_id(aff->ls, type, pos, id);
1597 return isl_aff_free(aff);
1606 /* Exploit the equalities in "eq" to simplify the affine expression
1607 * and the expressions of the integer divisions in the local space.
1608 * The integer divisions in this local space are assumed to appear
1609 * as regular dimensions in "eq".
1611 static __isl_give isl_aff *isl_aff_substitute_equalities_lifted(
1612 __isl_take isl_aff *aff, __isl_take isl_basic_set *eq)
1620 if (eq->n_eq == 0) {
1621 isl_basic_set_free(eq);
1625 aff = isl_aff_cow(aff);
1629 aff->ls = isl_local_space_substitute_equalities(aff->ls,
1630 isl_basic_set_copy(eq));
1631 aff->v = isl_vec_cow(aff->v);
1632 if (!aff->ls || !aff->v)
1635 total = 1 + isl_space_dim(eq->dim, isl_dim_all);
1637 for (i = 0; i < eq->n_eq; ++i) {
1638 j = isl_seq_last_non_zero(eq->eq[i], total + n_div);
1639 if (j < 0 || j == 0 || j >= total)
1642 isl_seq_elim(aff->v->el + 1, eq->eq[i], j, total,
1646 isl_basic_set_free(eq);
1647 aff = isl_aff_normalize(aff);
1650 isl_basic_set_free(eq);
1655 /* Exploit the equalities in "eq" to simplify the affine expression
1656 * and the expressions of the integer divisions in the local space.
1658 static __isl_give isl_aff *isl_aff_substitute_equalities(
1659 __isl_take isl_aff *aff, __isl_take isl_basic_set *eq)
1665 n_div = isl_local_space_dim(aff->ls, isl_dim_div);
1667 eq = isl_basic_set_add_dims(eq, isl_dim_set, n_div);
1668 return isl_aff_substitute_equalities_lifted(aff, eq);
1670 isl_basic_set_free(eq);
1675 /* Look for equalities among the variables shared by context and aff
1676 * and the integer divisions of aff, if any.
1677 * The equalities are then used to eliminate coefficients and/or integer
1678 * divisions from aff.
1680 __isl_give isl_aff *isl_aff_gist(__isl_take isl_aff *aff,
1681 __isl_take isl_set *context)
1683 isl_basic_set *hull;
1688 n_div = isl_local_space_dim(aff->ls, isl_dim_div);
1690 isl_basic_set *bset;
1691 isl_local_space *ls;
1692 context = isl_set_add_dims(context, isl_dim_set, n_div);
1693 ls = isl_aff_get_domain_local_space(aff);
1694 bset = isl_basic_set_from_local_space(ls);
1695 bset = isl_basic_set_lift(bset);
1696 bset = isl_basic_set_flatten(bset);
1697 context = isl_set_intersect(context,
1698 isl_set_from_basic_set(bset));
1701 hull = isl_set_affine_hull(context);
1702 return isl_aff_substitute_equalities_lifted(aff, hull);
1705 isl_set_free(context);
1709 __isl_give isl_aff *isl_aff_gist_params(__isl_take isl_aff *aff,
1710 __isl_take isl_set *context)
1712 isl_set *dom_context = isl_set_universe(isl_aff_get_domain_space(aff));
1713 dom_context = isl_set_intersect_params(dom_context, context);
1714 return isl_aff_gist(aff, dom_context);
1717 /* Return a basic set containing those elements in the space
1718 * of aff where it is non-negative.
1719 * If "rational" is set, then return a rational basic set.
1721 static __isl_give isl_basic_set *aff_nonneg_basic_set(
1722 __isl_take isl_aff *aff, int rational)
1724 isl_constraint *ineq;
1725 isl_basic_set *bset;
1727 ineq = isl_inequality_from_aff(aff);
1729 bset = isl_basic_set_from_constraint(ineq);
1731 bset = isl_basic_set_set_rational(bset);
1732 bset = isl_basic_set_simplify(bset);
1736 /* Return a basic set containing those elements in the space
1737 * of aff where it is non-negative.
1739 __isl_give isl_basic_set *isl_aff_nonneg_basic_set(__isl_take isl_aff *aff)
1741 return aff_nonneg_basic_set(aff, 0);
1744 /* Return a basic set containing those elements in the domain space
1745 * of aff where it is negative.
1747 __isl_give isl_basic_set *isl_aff_neg_basic_set(__isl_take isl_aff *aff)
1749 aff = isl_aff_neg(aff);
1750 aff = isl_aff_add_constant_num_si(aff, -1);
1751 return isl_aff_nonneg_basic_set(aff);
1754 /* Return a basic set containing those elements in the space
1755 * of aff where it is zero.
1756 * If "rational" is set, then return a rational basic set.
1758 static __isl_give isl_basic_set *aff_zero_basic_set(__isl_take isl_aff *aff,
1761 isl_constraint *ineq;
1762 isl_basic_set *bset;
1764 ineq = isl_equality_from_aff(aff);
1766 bset = isl_basic_set_from_constraint(ineq);
1768 bset = isl_basic_set_set_rational(bset);
1769 bset = isl_basic_set_simplify(bset);
1773 /* Return a basic set containing those elements in the space
1774 * of aff where it is zero.
1776 __isl_give isl_basic_set *isl_aff_zero_basic_set(__isl_take isl_aff *aff)
1778 return aff_zero_basic_set(aff, 0);
1781 /* Return a basic set containing those elements in the shared space
1782 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
1784 __isl_give isl_basic_set *isl_aff_ge_basic_set(__isl_take isl_aff *aff1,
1785 __isl_take isl_aff *aff2)
1787 aff1 = isl_aff_sub(aff1, aff2);
1789 return isl_aff_nonneg_basic_set(aff1);
1792 /* Return a basic set containing those elements in the shared space
1793 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
1795 __isl_give isl_basic_set *isl_aff_le_basic_set(__isl_take isl_aff *aff1,
1796 __isl_take isl_aff *aff2)
1798 return isl_aff_ge_basic_set(aff2, aff1);
1801 __isl_give isl_aff *isl_aff_add_on_domain(__isl_keep isl_set *dom,
1802 __isl_take isl_aff *aff1, __isl_take isl_aff *aff2)
1804 aff1 = isl_aff_add(aff1, aff2);
1805 aff1 = isl_aff_gist(aff1, isl_set_copy(dom));
1809 int isl_aff_is_empty(__isl_keep isl_aff *aff)
1817 /* Check whether the given affine expression has non-zero coefficient
1818 * for any dimension in the given range or if any of these dimensions
1819 * appear with non-zero coefficients in any of the integer divisions
1820 * involved in the affine expression.
1822 int isl_aff_involves_dims(__isl_keep isl_aff *aff,
1823 enum isl_dim_type type, unsigned first, unsigned n)
1835 ctx = isl_aff_get_ctx(aff);
1836 if (first + n > isl_aff_dim(aff, type))
1837 isl_die(ctx, isl_error_invalid,
1838 "range out of bounds", return -1);
1840 active = isl_local_space_get_active(aff->ls, aff->v->el + 2);
1844 first += isl_local_space_offset(aff->ls, type) - 1;
1845 for (i = 0; i < n; ++i)
1846 if (active[first + i]) {
1859 __isl_give isl_aff *isl_aff_drop_dims(__isl_take isl_aff *aff,
1860 enum isl_dim_type type, unsigned first, unsigned n)
1866 if (type == isl_dim_out)
1867 isl_die(aff->v->ctx, isl_error_invalid,
1868 "cannot drop output/set dimension",
1869 return isl_aff_free(aff));
1870 if (type == isl_dim_in)
1872 if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
1875 ctx = isl_aff_get_ctx(aff);
1876 if (first + n > isl_local_space_dim(aff->ls, type))
1877 isl_die(ctx, isl_error_invalid, "range out of bounds",
1878 return isl_aff_free(aff));
1880 aff = isl_aff_cow(aff);
1884 aff->ls = isl_local_space_drop_dims(aff->ls, type, first, n);
1886 return isl_aff_free(aff);
1888 first += 1 + isl_local_space_offset(aff->ls, type);
1889 aff->v = isl_vec_drop_els(aff->v, first, n);
1891 return isl_aff_free(aff);
1896 /* Project the domain of the affine expression onto its parameter space.
1897 * The affine expression may not involve any of the domain dimensions.
1899 __isl_give isl_aff *isl_aff_project_domain_on_params(__isl_take isl_aff *aff)
1905 n = isl_aff_dim(aff, isl_dim_in);
1906 involves = isl_aff_involves_dims(aff, isl_dim_in, 0, n);
1908 return isl_aff_free(aff);
1910 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1911 "affine expression involves some of the domain dimensions",
1912 return isl_aff_free(aff));
1913 aff = isl_aff_drop_dims(aff, isl_dim_in, 0, n);
1914 space = isl_aff_get_domain_space(aff);
1915 space = isl_space_params(space);
1916 aff = isl_aff_reset_domain_space(aff, space);
1920 __isl_give isl_aff *isl_aff_insert_dims(__isl_take isl_aff *aff,
1921 enum isl_dim_type type, unsigned first, unsigned n)
1927 if (type == isl_dim_out)
1928 isl_die(aff->v->ctx, isl_error_invalid,
1929 "cannot insert output/set dimensions",
1930 return isl_aff_free(aff));
1931 if (type == isl_dim_in)
1933 if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
1936 ctx = isl_aff_get_ctx(aff);
1937 if (first > isl_local_space_dim(aff->ls, type))
1938 isl_die(ctx, isl_error_invalid, "position out of bounds",
1939 return isl_aff_free(aff));
1941 aff = isl_aff_cow(aff);
1945 aff->ls = isl_local_space_insert_dims(aff->ls, type, first, n);
1947 return isl_aff_free(aff);
1949 first += 1 + isl_local_space_offset(aff->ls, type);
1950 aff->v = isl_vec_insert_zero_els(aff->v, first, n);
1952 return isl_aff_free(aff);
1957 __isl_give isl_aff *isl_aff_add_dims(__isl_take isl_aff *aff,
1958 enum isl_dim_type type, unsigned n)
1962 pos = isl_aff_dim(aff, type);
1964 return isl_aff_insert_dims(aff, type, pos, n);
1967 __isl_give isl_pw_aff *isl_pw_aff_add_dims(__isl_take isl_pw_aff *pwaff,
1968 enum isl_dim_type type, unsigned n)
1972 pos = isl_pw_aff_dim(pwaff, type);
1974 return isl_pw_aff_insert_dims(pwaff, type, pos, n);
1977 __isl_give isl_pw_aff *isl_pw_aff_from_aff(__isl_take isl_aff *aff)
1979 isl_set *dom = isl_set_universe(isl_aff_get_domain_space(aff));
1980 return isl_pw_aff_alloc(dom, aff);
1984 #define PW isl_pw_aff
1988 #define EL_IS_ZERO is_empty
1992 #define IS_ZERO is_empty
1995 #undef DEFAULT_IS_ZERO
1996 #define DEFAULT_IS_ZERO 0
2000 #define NO_MOVE_DIMS
2004 #include <isl_pw_templ.c>
2006 static __isl_give isl_set *align_params_pw_pw_set_and(
2007 __isl_take isl_pw_aff *pwaff1, __isl_take isl_pw_aff *pwaff2,
2008 __isl_give isl_set *(*fn)(__isl_take isl_pw_aff *pwaff1,
2009 __isl_take isl_pw_aff *pwaff2))
2011 if (!pwaff1 || !pwaff2)
2013 if (isl_space_match(pwaff1->dim, isl_dim_param,
2014 pwaff2->dim, isl_dim_param))
2015 return fn(pwaff1, pwaff2);
2016 if (!isl_space_has_named_params(pwaff1->dim) ||
2017 !isl_space_has_named_params(pwaff2->dim))
2018 isl_die(isl_pw_aff_get_ctx(pwaff1), isl_error_invalid,
2019 "unaligned unnamed parameters", goto error);
2020 pwaff1 = isl_pw_aff_align_params(pwaff1, isl_pw_aff_get_space(pwaff2));
2021 pwaff2 = isl_pw_aff_align_params(pwaff2, isl_pw_aff_get_space(pwaff1));
2022 return fn(pwaff1, pwaff2);
2024 isl_pw_aff_free(pwaff1);
2025 isl_pw_aff_free(pwaff2);
2029 /* Compute a piecewise quasi-affine expression with a domain that
2030 * is the union of those of pwaff1 and pwaff2 and such that on each
2031 * cell, the quasi-affine expression is the better (according to cmp)
2032 * of those of pwaff1 and pwaff2. If only one of pwaff1 or pwaff2
2033 * is defined on a given cell, then the associated expression
2034 * is the defined one.
2036 static __isl_give isl_pw_aff *pw_aff_union_opt(__isl_take isl_pw_aff *pwaff1,
2037 __isl_take isl_pw_aff *pwaff2,
2038 __isl_give isl_basic_set *(*cmp)(__isl_take isl_aff *aff1,
2039 __isl_take isl_aff *aff2))
2046 if (!pwaff1 || !pwaff2)
2049 ctx = isl_space_get_ctx(pwaff1->dim);
2050 if (!isl_space_is_equal(pwaff1->dim, pwaff2->dim))
2051 isl_die(ctx, isl_error_invalid,
2052 "arguments should live in same space", goto error);
2054 if (isl_pw_aff_is_empty(pwaff1)) {
2055 isl_pw_aff_free(pwaff1);
2059 if (isl_pw_aff_is_empty(pwaff2)) {
2060 isl_pw_aff_free(pwaff2);
2064 n = 2 * (pwaff1->n + 1) * (pwaff2->n + 1);
2065 res = isl_pw_aff_alloc_size(isl_space_copy(pwaff1->dim), n);
2067 for (i = 0; i < pwaff1->n; ++i) {
2068 set = isl_set_copy(pwaff1->p[i].set);
2069 for (j = 0; j < pwaff2->n; ++j) {
2070 struct isl_set *common;
2073 common = isl_set_intersect(
2074 isl_set_copy(pwaff1->p[i].set),
2075 isl_set_copy(pwaff2->p[j].set));
2076 better = isl_set_from_basic_set(cmp(
2077 isl_aff_copy(pwaff2->p[j].aff),
2078 isl_aff_copy(pwaff1->p[i].aff)));
2079 better = isl_set_intersect(common, better);
2080 if (isl_set_plain_is_empty(better)) {
2081 isl_set_free(better);
2084 set = isl_set_subtract(set, isl_set_copy(better));
2086 res = isl_pw_aff_add_piece(res, better,
2087 isl_aff_copy(pwaff2->p[j].aff));
2089 res = isl_pw_aff_add_piece(res, set,
2090 isl_aff_copy(pwaff1->p[i].aff));
2093 for (j = 0; j < pwaff2->n; ++j) {
2094 set = isl_set_copy(pwaff2->p[j].set);
2095 for (i = 0; i < pwaff1->n; ++i)
2096 set = isl_set_subtract(set,
2097 isl_set_copy(pwaff1->p[i].set));
2098 res = isl_pw_aff_add_piece(res, set,
2099 isl_aff_copy(pwaff2->p[j].aff));
2102 isl_pw_aff_free(pwaff1);
2103 isl_pw_aff_free(pwaff2);
2107 isl_pw_aff_free(pwaff1);
2108 isl_pw_aff_free(pwaff2);
2112 /* Compute a piecewise quasi-affine expression with a domain that
2113 * is the union of those of pwaff1 and pwaff2 and such that on each
2114 * cell, the quasi-affine expression is the maximum of those of pwaff1
2115 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2116 * cell, then the associated expression is the defined one.
2118 static __isl_give isl_pw_aff *pw_aff_union_max(__isl_take isl_pw_aff *pwaff1,
2119 __isl_take isl_pw_aff *pwaff2)
2121 return pw_aff_union_opt(pwaff1, pwaff2, &isl_aff_ge_basic_set);
2124 __isl_give isl_pw_aff *isl_pw_aff_union_max(__isl_take isl_pw_aff *pwaff1,
2125 __isl_take isl_pw_aff *pwaff2)
2127 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2,
2131 /* Compute a piecewise quasi-affine expression with a domain that
2132 * is the union of those of pwaff1 and pwaff2 and such that on each
2133 * cell, the quasi-affine expression is the minimum of those of pwaff1
2134 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2135 * cell, then the associated expression is the defined one.
2137 static __isl_give isl_pw_aff *pw_aff_union_min(__isl_take isl_pw_aff *pwaff1,
2138 __isl_take isl_pw_aff *pwaff2)
2140 return pw_aff_union_opt(pwaff1, pwaff2, &isl_aff_le_basic_set);
2143 __isl_give isl_pw_aff *isl_pw_aff_union_min(__isl_take isl_pw_aff *pwaff1,
2144 __isl_take isl_pw_aff *pwaff2)
2146 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2,
2150 __isl_give isl_pw_aff *isl_pw_aff_union_opt(__isl_take isl_pw_aff *pwaff1,
2151 __isl_take isl_pw_aff *pwaff2, int max)
2154 return isl_pw_aff_union_max(pwaff1, pwaff2);
2156 return isl_pw_aff_union_min(pwaff1, pwaff2);
2159 /* Construct a map with as domain the domain of pwaff and
2160 * one-dimensional range corresponding to the affine expressions.
2162 static __isl_give isl_map *map_from_pw_aff(__isl_take isl_pw_aff *pwaff)
2171 dim = isl_pw_aff_get_space(pwaff);
2172 map = isl_map_empty(dim);
2174 for (i = 0; i < pwaff->n; ++i) {
2175 isl_basic_map *bmap;
2178 bmap = isl_basic_map_from_aff(isl_aff_copy(pwaff->p[i].aff));
2179 map_i = isl_map_from_basic_map(bmap);
2180 map_i = isl_map_intersect_domain(map_i,
2181 isl_set_copy(pwaff->p[i].set));
2182 map = isl_map_union_disjoint(map, map_i);
2185 isl_pw_aff_free(pwaff);
2190 /* Construct a map with as domain the domain of pwaff and
2191 * one-dimensional range corresponding to the affine expressions.
2193 __isl_give isl_map *isl_map_from_pw_aff(__isl_take isl_pw_aff *pwaff)
2197 if (isl_space_is_set(pwaff->dim))
2198 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
2199 "space of input is not a map",
2200 return isl_pw_aff_free(pwaff));
2201 return map_from_pw_aff(pwaff);
2204 /* Construct a one-dimensional set with as parameter domain
2205 * the domain of pwaff and the single set dimension
2206 * corresponding to the affine expressions.
2208 __isl_give isl_set *isl_set_from_pw_aff(__isl_take isl_pw_aff *pwaff)
2212 if (!isl_space_is_set(pwaff->dim))
2213 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
2214 "space of input is not a set",
2215 return isl_pw_aff_free(pwaff));
2216 return map_from_pw_aff(pwaff);
2219 /* Return a set containing those elements in the domain
2220 * of pwaff where it is non-negative.
2222 __isl_give isl_set *isl_pw_aff_nonneg_set(__isl_take isl_pw_aff *pwaff)
2230 set = isl_set_empty(isl_pw_aff_get_domain_space(pwaff));
2232 for (i = 0; i < pwaff->n; ++i) {
2233 isl_basic_set *bset;
2237 rational = isl_set_has_rational(pwaff->p[i].set);
2238 bset = aff_nonneg_basic_set(isl_aff_copy(pwaff->p[i].aff),
2240 set_i = isl_set_from_basic_set(bset);
2241 set_i = isl_set_intersect(set_i, isl_set_copy(pwaff->p[i].set));
2242 set = isl_set_union_disjoint(set, set_i);
2245 isl_pw_aff_free(pwaff);
2250 /* Return a set containing those elements in the domain
2251 * of pwaff where it is zero (if complement is 0) or not zero
2252 * (if complement is 1).
2254 static __isl_give isl_set *pw_aff_zero_set(__isl_take isl_pw_aff *pwaff,
2263 set = isl_set_empty(isl_pw_aff_get_domain_space(pwaff));
2265 for (i = 0; i < pwaff->n; ++i) {
2266 isl_basic_set *bset;
2267 isl_set *set_i, *zero;
2270 rational = isl_set_has_rational(pwaff->p[i].set);
2271 bset = aff_zero_basic_set(isl_aff_copy(pwaff->p[i].aff),
2273 zero = isl_set_from_basic_set(bset);
2274 set_i = isl_set_copy(pwaff->p[i].set);
2276 set_i = isl_set_subtract(set_i, zero);
2278 set_i = isl_set_intersect(set_i, zero);
2279 set = isl_set_union_disjoint(set, set_i);
2282 isl_pw_aff_free(pwaff);
2287 /* Return a set containing those elements in the domain
2288 * of pwaff where it is zero.
2290 __isl_give isl_set *isl_pw_aff_zero_set(__isl_take isl_pw_aff *pwaff)
2292 return pw_aff_zero_set(pwaff, 0);
2295 /* Return a set containing those elements in the domain
2296 * of pwaff where it is not zero.
2298 __isl_give isl_set *isl_pw_aff_non_zero_set(__isl_take isl_pw_aff *pwaff)
2300 return pw_aff_zero_set(pwaff, 1);
2303 /* Return a set containing those elements in the shared domain
2304 * of pwaff1 and pwaff2 where pwaff1 is greater than (or equal) to pwaff2.
2306 * We compute the difference on the shared domain and then construct
2307 * the set of values where this difference is non-negative.
2308 * If strict is set, we first subtract 1 from the difference.
2309 * If equal is set, we only return the elements where pwaff1 and pwaff2
2312 static __isl_give isl_set *pw_aff_gte_set(__isl_take isl_pw_aff *pwaff1,
2313 __isl_take isl_pw_aff *pwaff2, int strict, int equal)
2315 isl_set *set1, *set2;
2317 set1 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff1));
2318 set2 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff2));
2319 set1 = isl_set_intersect(set1, set2);
2320 pwaff1 = isl_pw_aff_intersect_domain(pwaff1, isl_set_copy(set1));
2321 pwaff2 = isl_pw_aff_intersect_domain(pwaff2, isl_set_copy(set1));
2322 pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_neg(pwaff2));
2325 isl_space *dim = isl_set_get_space(set1);
2327 aff = isl_aff_zero_on_domain(isl_local_space_from_space(dim));
2328 aff = isl_aff_add_constant_si(aff, -1);
2329 pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_alloc(set1, aff));
2334 return isl_pw_aff_zero_set(pwaff1);
2335 return isl_pw_aff_nonneg_set(pwaff1);
2338 /* Return a set containing those elements in the shared domain
2339 * of pwaff1 and pwaff2 where pwaff1 is equal to pwaff2.
2341 static __isl_give isl_set *pw_aff_eq_set(__isl_take isl_pw_aff *pwaff1,
2342 __isl_take isl_pw_aff *pwaff2)
2344 return pw_aff_gte_set(pwaff1, pwaff2, 0, 1);
2347 __isl_give isl_set *isl_pw_aff_eq_set(__isl_take isl_pw_aff *pwaff1,
2348 __isl_take isl_pw_aff *pwaff2)
2350 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_eq_set);
2353 /* Return a set containing those elements in the shared domain
2354 * of pwaff1 and pwaff2 where pwaff1 is greater than or equal to pwaff2.
2356 static __isl_give isl_set *pw_aff_ge_set(__isl_take isl_pw_aff *pwaff1,
2357 __isl_take isl_pw_aff *pwaff2)
2359 return pw_aff_gte_set(pwaff1, pwaff2, 0, 0);
2362 __isl_give isl_set *isl_pw_aff_ge_set(__isl_take isl_pw_aff *pwaff1,
2363 __isl_take isl_pw_aff *pwaff2)
2365 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_ge_set);
2368 /* Return a set containing those elements in the shared domain
2369 * of pwaff1 and pwaff2 where pwaff1 is strictly greater than pwaff2.
2371 static __isl_give isl_set *pw_aff_gt_set(__isl_take isl_pw_aff *pwaff1,
2372 __isl_take isl_pw_aff *pwaff2)
2374 return pw_aff_gte_set(pwaff1, pwaff2, 1, 0);
2377 __isl_give isl_set *isl_pw_aff_gt_set(__isl_take isl_pw_aff *pwaff1,
2378 __isl_take isl_pw_aff *pwaff2)
2380 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_gt_set);
2383 __isl_give isl_set *isl_pw_aff_le_set(__isl_take isl_pw_aff *pwaff1,
2384 __isl_take isl_pw_aff *pwaff2)
2386 return isl_pw_aff_ge_set(pwaff2, pwaff1);
2389 __isl_give isl_set *isl_pw_aff_lt_set(__isl_take isl_pw_aff *pwaff1,
2390 __isl_take isl_pw_aff *pwaff2)
2392 return isl_pw_aff_gt_set(pwaff2, pwaff1);
2395 /* Return a set containing those elements in the shared domain
2396 * of the elements of list1 and list2 where each element in list1
2397 * has the relation specified by "fn" with each element in list2.
2399 static __isl_give isl_set *pw_aff_list_set(__isl_take isl_pw_aff_list *list1,
2400 __isl_take isl_pw_aff_list *list2,
2401 __isl_give isl_set *(*fn)(__isl_take isl_pw_aff *pwaff1,
2402 __isl_take isl_pw_aff *pwaff2))
2408 if (!list1 || !list2)
2411 ctx = isl_pw_aff_list_get_ctx(list1);
2412 if (list1->n < 1 || list2->n < 1)
2413 isl_die(ctx, isl_error_invalid,
2414 "list should contain at least one element", goto error);
2416 set = isl_set_universe(isl_pw_aff_get_domain_space(list1->p[0]));
2417 for (i = 0; i < list1->n; ++i)
2418 for (j = 0; j < list2->n; ++j) {
2421 set_ij = fn(isl_pw_aff_copy(list1->p[i]),
2422 isl_pw_aff_copy(list2->p[j]));
2423 set = isl_set_intersect(set, set_ij);
2426 isl_pw_aff_list_free(list1);
2427 isl_pw_aff_list_free(list2);
2430 isl_pw_aff_list_free(list1);
2431 isl_pw_aff_list_free(list2);
2435 /* Return a set containing those elements in the shared domain
2436 * of the elements of list1 and list2 where each element in list1
2437 * is equal to each element in list2.
2439 __isl_give isl_set *isl_pw_aff_list_eq_set(__isl_take isl_pw_aff_list *list1,
2440 __isl_take isl_pw_aff_list *list2)
2442 return pw_aff_list_set(list1, list2, &isl_pw_aff_eq_set);
2445 __isl_give isl_set *isl_pw_aff_list_ne_set(__isl_take isl_pw_aff_list *list1,
2446 __isl_take isl_pw_aff_list *list2)
2448 return pw_aff_list_set(list1, list2, &isl_pw_aff_ne_set);
2451 /* Return a set containing those elements in the shared domain
2452 * of the elements of list1 and list2 where each element in list1
2453 * is less than or equal to each element in list2.
2455 __isl_give isl_set *isl_pw_aff_list_le_set(__isl_take isl_pw_aff_list *list1,
2456 __isl_take isl_pw_aff_list *list2)
2458 return pw_aff_list_set(list1, list2, &isl_pw_aff_le_set);
2461 __isl_give isl_set *isl_pw_aff_list_lt_set(__isl_take isl_pw_aff_list *list1,
2462 __isl_take isl_pw_aff_list *list2)
2464 return pw_aff_list_set(list1, list2, &isl_pw_aff_lt_set);
2467 __isl_give isl_set *isl_pw_aff_list_ge_set(__isl_take isl_pw_aff_list *list1,
2468 __isl_take isl_pw_aff_list *list2)
2470 return pw_aff_list_set(list1, list2, &isl_pw_aff_ge_set);
2473 __isl_give isl_set *isl_pw_aff_list_gt_set(__isl_take isl_pw_aff_list *list1,
2474 __isl_take isl_pw_aff_list *list2)
2476 return pw_aff_list_set(list1, list2, &isl_pw_aff_gt_set);
2480 /* Return a set containing those elements in the shared domain
2481 * of pwaff1 and pwaff2 where pwaff1 is not equal to pwaff2.
2483 static __isl_give isl_set *pw_aff_ne_set(__isl_take isl_pw_aff *pwaff1,
2484 __isl_take isl_pw_aff *pwaff2)
2486 isl_set *set_lt, *set_gt;
2488 set_lt = isl_pw_aff_lt_set(isl_pw_aff_copy(pwaff1),
2489 isl_pw_aff_copy(pwaff2));
2490 set_gt = isl_pw_aff_gt_set(pwaff1, pwaff2);
2491 return isl_set_union_disjoint(set_lt, set_gt);
2494 __isl_give isl_set *isl_pw_aff_ne_set(__isl_take isl_pw_aff *pwaff1,
2495 __isl_take isl_pw_aff *pwaff2)
2497 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_ne_set);
2500 __isl_give isl_pw_aff *isl_pw_aff_scale_down(__isl_take isl_pw_aff *pwaff,
2505 if (isl_int_is_one(v))
2507 if (!isl_int_is_pos(v))
2508 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
2509 "factor needs to be positive",
2510 return isl_pw_aff_free(pwaff));
2511 pwaff = isl_pw_aff_cow(pwaff);
2517 for (i = 0; i < pwaff->n; ++i) {
2518 pwaff->p[i].aff = isl_aff_scale_down(pwaff->p[i].aff, v);
2519 if (!pwaff->p[i].aff)
2520 return isl_pw_aff_free(pwaff);
2526 __isl_give isl_pw_aff *isl_pw_aff_floor(__isl_take isl_pw_aff *pwaff)
2530 pwaff = isl_pw_aff_cow(pwaff);
2536 for (i = 0; i < pwaff->n; ++i) {
2537 pwaff->p[i].aff = isl_aff_floor(pwaff->p[i].aff);
2538 if (!pwaff->p[i].aff)
2539 return isl_pw_aff_free(pwaff);
2545 __isl_give isl_pw_aff *isl_pw_aff_ceil(__isl_take isl_pw_aff *pwaff)
2549 pwaff = isl_pw_aff_cow(pwaff);
2555 for (i = 0; i < pwaff->n; ++i) {
2556 pwaff->p[i].aff = isl_aff_ceil(pwaff->p[i].aff);
2557 if (!pwaff->p[i].aff)
2558 return isl_pw_aff_free(pwaff);
2564 /* Assuming that "cond1" and "cond2" are disjoint,
2565 * return an affine expression that is equal to pwaff1 on cond1
2566 * and to pwaff2 on cond2.
2568 static __isl_give isl_pw_aff *isl_pw_aff_select(
2569 __isl_take isl_set *cond1, __isl_take isl_pw_aff *pwaff1,
2570 __isl_take isl_set *cond2, __isl_take isl_pw_aff *pwaff2)
2572 pwaff1 = isl_pw_aff_intersect_domain(pwaff1, cond1);
2573 pwaff2 = isl_pw_aff_intersect_domain(pwaff2, cond2);
2575 return isl_pw_aff_add_disjoint(pwaff1, pwaff2);
2578 /* Return an affine expression that is equal to pwaff_true for elements
2579 * where "cond" is non-zero and to pwaff_false for elements where "cond"
2581 * That is, return cond ? pwaff_true : pwaff_false;
2583 __isl_give isl_pw_aff *isl_pw_aff_cond(__isl_take isl_pw_aff *cond,
2584 __isl_take isl_pw_aff *pwaff_true, __isl_take isl_pw_aff *pwaff_false)
2586 isl_set *cond_true, *cond_false;
2588 cond_true = isl_pw_aff_non_zero_set(isl_pw_aff_copy(cond));
2589 cond_false = isl_pw_aff_zero_set(cond);
2590 return isl_pw_aff_select(cond_true, pwaff_true,
2591 cond_false, pwaff_false);
2594 int isl_aff_is_cst(__isl_keep isl_aff *aff)
2599 return isl_seq_first_non_zero(aff->v->el + 2, aff->v->size - 2) == -1;
2602 /* Check whether pwaff is a piecewise constant.
2604 int isl_pw_aff_is_cst(__isl_keep isl_pw_aff *pwaff)
2611 for (i = 0; i < pwaff->n; ++i) {
2612 int is_cst = isl_aff_is_cst(pwaff->p[i].aff);
2613 if (is_cst < 0 || !is_cst)
2620 __isl_give isl_aff *isl_aff_mul(__isl_take isl_aff *aff1,
2621 __isl_take isl_aff *aff2)
2623 if (!isl_aff_is_cst(aff2) && isl_aff_is_cst(aff1))
2624 return isl_aff_mul(aff2, aff1);
2626 if (!isl_aff_is_cst(aff2))
2627 isl_die(isl_aff_get_ctx(aff1), isl_error_invalid,
2628 "at least one affine expression should be constant",
2631 aff1 = isl_aff_cow(aff1);
2635 aff1 = isl_aff_scale(aff1, aff2->v->el[1]);
2636 aff1 = isl_aff_scale_down(aff1, aff2->v->el[0]);
2646 /* Divide "aff1" by "aff2", assuming "aff2" is a piecewise constant.
2648 __isl_give isl_aff *isl_aff_div(__isl_take isl_aff *aff1,
2649 __isl_take isl_aff *aff2)
2654 is_cst = isl_aff_is_cst(aff2);
2658 isl_die(isl_aff_get_ctx(aff2), isl_error_invalid,
2659 "second argument should be a constant", goto error);
2664 neg = isl_int_is_neg(aff2->v->el[1]);
2666 isl_int_neg(aff2->v->el[0], aff2->v->el[0]);
2667 isl_int_neg(aff2->v->el[1], aff2->v->el[1]);
2670 aff1 = isl_aff_scale(aff1, aff2->v->el[0]);
2671 aff1 = isl_aff_scale_down(aff1, aff2->v->el[1]);
2674 isl_int_neg(aff2->v->el[0], aff2->v->el[0]);
2675 isl_int_neg(aff2->v->el[1], aff2->v->el[1]);
2686 static __isl_give isl_pw_aff *pw_aff_add(__isl_take isl_pw_aff *pwaff1,
2687 __isl_take isl_pw_aff *pwaff2)
2689 return isl_pw_aff_on_shared_domain(pwaff1, pwaff2, &isl_aff_add);
2692 __isl_give isl_pw_aff *isl_pw_aff_add(__isl_take isl_pw_aff *pwaff1,
2693 __isl_take isl_pw_aff *pwaff2)
2695 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_add);
2698 __isl_give isl_pw_aff *isl_pw_aff_union_add(__isl_take isl_pw_aff *pwaff1,
2699 __isl_take isl_pw_aff *pwaff2)
2701 return isl_pw_aff_union_add_(pwaff1, pwaff2);
2704 static __isl_give isl_pw_aff *pw_aff_mul(__isl_take isl_pw_aff *pwaff1,
2705 __isl_take isl_pw_aff *pwaff2)
2707 return isl_pw_aff_on_shared_domain(pwaff1, pwaff2, &isl_aff_mul);
2710 __isl_give isl_pw_aff *isl_pw_aff_mul(__isl_take isl_pw_aff *pwaff1,
2711 __isl_take isl_pw_aff *pwaff2)
2713 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_mul);
2716 static __isl_give isl_pw_aff *pw_aff_div(__isl_take isl_pw_aff *pa1,
2717 __isl_take isl_pw_aff *pa2)
2719 return isl_pw_aff_on_shared_domain(pa1, pa2, &isl_aff_div);
2722 /* Divide "pa1" by "pa2", assuming "pa2" is a piecewise constant.
2724 __isl_give isl_pw_aff *isl_pw_aff_div(__isl_take isl_pw_aff *pa1,
2725 __isl_take isl_pw_aff *pa2)
2729 is_cst = isl_pw_aff_is_cst(pa2);
2733 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
2734 "second argument should be a piecewise constant",
2736 return isl_pw_aff_align_params_pw_pw_and(pa1, pa2, &pw_aff_div);
2738 isl_pw_aff_free(pa1);
2739 isl_pw_aff_free(pa2);
2743 /* Compute the quotient of the integer division of "pa1" by "pa2"
2744 * with rounding towards zero.
2745 * "pa2" is assumed to be a piecewise constant.
2747 * In particular, return
2749 * pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2)
2752 __isl_give isl_pw_aff *isl_pw_aff_tdiv_q(__isl_take isl_pw_aff *pa1,
2753 __isl_take isl_pw_aff *pa2)
2759 is_cst = isl_pw_aff_is_cst(pa2);
2763 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
2764 "second argument should be a piecewise constant",
2767 pa1 = isl_pw_aff_div(pa1, pa2);
2769 cond = isl_pw_aff_nonneg_set(isl_pw_aff_copy(pa1));
2770 f = isl_pw_aff_floor(isl_pw_aff_copy(pa1));
2771 c = isl_pw_aff_ceil(pa1);
2772 return isl_pw_aff_cond(isl_set_indicator_function(cond), f, c);
2774 isl_pw_aff_free(pa1);
2775 isl_pw_aff_free(pa2);
2779 /* Compute the remainder of the integer division of "pa1" by "pa2"
2780 * with rounding towards zero.
2781 * "pa2" is assumed to be a piecewise constant.
2783 * In particular, return
2785 * pa1 - pa2 * (pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2))
2788 __isl_give isl_pw_aff *isl_pw_aff_tdiv_r(__isl_take isl_pw_aff *pa1,
2789 __isl_take isl_pw_aff *pa2)
2794 is_cst = isl_pw_aff_is_cst(pa2);
2798 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
2799 "second argument should be a piecewise constant",
2801 res = isl_pw_aff_tdiv_q(isl_pw_aff_copy(pa1), isl_pw_aff_copy(pa2));
2802 res = isl_pw_aff_mul(pa2, res);
2803 res = isl_pw_aff_sub(pa1, res);
2806 isl_pw_aff_free(pa1);
2807 isl_pw_aff_free(pa2);
2811 static __isl_give isl_pw_aff *pw_aff_min(__isl_take isl_pw_aff *pwaff1,
2812 __isl_take isl_pw_aff *pwaff2)
2817 dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1)),
2818 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2)));
2819 le = isl_pw_aff_le_set(isl_pw_aff_copy(pwaff1),
2820 isl_pw_aff_copy(pwaff2));
2821 dom = isl_set_subtract(dom, isl_set_copy(le));
2822 return isl_pw_aff_select(le, pwaff1, dom, pwaff2);
2825 __isl_give isl_pw_aff *isl_pw_aff_min(__isl_take isl_pw_aff *pwaff1,
2826 __isl_take isl_pw_aff *pwaff2)
2828 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_min);
2831 static __isl_give isl_pw_aff *pw_aff_max(__isl_take isl_pw_aff *pwaff1,
2832 __isl_take isl_pw_aff *pwaff2)
2837 dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1)),
2838 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2)));
2839 ge = isl_pw_aff_ge_set(isl_pw_aff_copy(pwaff1),
2840 isl_pw_aff_copy(pwaff2));
2841 dom = isl_set_subtract(dom, isl_set_copy(ge));
2842 return isl_pw_aff_select(ge, pwaff1, dom, pwaff2);
2845 __isl_give isl_pw_aff *isl_pw_aff_max(__isl_take isl_pw_aff *pwaff1,
2846 __isl_take isl_pw_aff *pwaff2)
2848 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_max);
2851 static __isl_give isl_pw_aff *pw_aff_list_reduce(
2852 __isl_take isl_pw_aff_list *list,
2853 __isl_give isl_pw_aff *(*fn)(__isl_take isl_pw_aff *pwaff1,
2854 __isl_take isl_pw_aff *pwaff2))
2863 ctx = isl_pw_aff_list_get_ctx(list);
2865 isl_die(ctx, isl_error_invalid,
2866 "list should contain at least one element",
2867 return isl_pw_aff_list_free(list));
2869 res = isl_pw_aff_copy(list->p[0]);
2870 for (i = 1; i < list->n; ++i)
2871 res = fn(res, isl_pw_aff_copy(list->p[i]));
2873 isl_pw_aff_list_free(list);
2877 /* Return an isl_pw_aff that maps each element in the intersection of the
2878 * domains of the elements of list to the minimal corresponding affine
2881 __isl_give isl_pw_aff *isl_pw_aff_list_min(__isl_take isl_pw_aff_list *list)
2883 return pw_aff_list_reduce(list, &isl_pw_aff_min);
2886 /* Return an isl_pw_aff that maps each element in the intersection of the
2887 * domains of the elements of list to the maximal corresponding affine
2890 __isl_give isl_pw_aff *isl_pw_aff_list_max(__isl_take isl_pw_aff_list *list)
2892 return pw_aff_list_reduce(list, &isl_pw_aff_max);
2895 /* Mark the domains of "pwaff" as rational.
2897 __isl_give isl_pw_aff *isl_pw_aff_set_rational(__isl_take isl_pw_aff *pwaff)
2901 pwaff = isl_pw_aff_cow(pwaff);
2907 for (i = 0; i < pwaff->n; ++i) {
2908 pwaff->p[i].set = isl_set_set_rational(pwaff->p[i].set);
2909 if (!pwaff->p[i].set)
2910 return isl_pw_aff_free(pwaff);
2916 /* Mark the domains of the elements of "list" as rational.
2918 __isl_give isl_pw_aff_list *isl_pw_aff_list_set_rational(
2919 __isl_take isl_pw_aff_list *list)
2929 for (i = 0; i < n; ++i) {
2932 pa = isl_pw_aff_list_get_pw_aff(list, i);
2933 pa = isl_pw_aff_set_rational(pa);
2934 list = isl_pw_aff_list_set_pw_aff(list, i, pa);
2940 /* Check that the domain space of "aff" matches "space".
2942 * Return 0 on success and -1 on error.
2944 int isl_aff_check_match_domain_space(__isl_keep isl_aff *aff,
2945 __isl_keep isl_space *space)
2947 isl_space *aff_space;
2953 aff_space = isl_aff_get_domain_space(aff);
2955 match = isl_space_match(space, isl_dim_param, aff_space, isl_dim_param);
2959 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2960 "parameters don't match", goto error);
2961 match = isl_space_tuple_match(space, isl_dim_in,
2962 aff_space, isl_dim_set);
2966 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2967 "domains don't match", goto error);
2968 isl_space_free(aff_space);
2971 isl_space_free(aff_space);
2978 #include <isl_multi_templ.c>
2980 /* Create an isl_pw_multi_aff with the given isl_multi_aff on a universe
2983 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_multi_aff(
2984 __isl_take isl_multi_aff *ma)
2986 isl_set *dom = isl_set_universe(isl_multi_aff_get_domain_space(ma));
2987 return isl_pw_multi_aff_alloc(dom, ma);
2990 /* Create a piecewise multi-affine expression in the given space that maps each
2991 * input dimension to the corresponding output dimension.
2993 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_identity(
2994 __isl_take isl_space *space)
2996 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_identity(space));
2999 __isl_give isl_multi_aff *isl_multi_aff_add(__isl_take isl_multi_aff *maff1,
3000 __isl_take isl_multi_aff *maff2)
3002 return isl_multi_aff_bin_op(maff1, maff2, &isl_aff_add);
3005 /* Subtract "ma2" from "ma1" and return the result.
3007 __isl_give isl_multi_aff *isl_multi_aff_sub(__isl_take isl_multi_aff *ma1,
3008 __isl_take isl_multi_aff *ma2)
3010 return isl_multi_aff_bin_op(ma1, ma2, &isl_aff_sub);
3013 /* Given two multi-affine expressions A -> B and C -> D,
3014 * construct a multi-affine expression [A -> C] -> [B -> D].
3016 __isl_give isl_multi_aff *isl_multi_aff_product(
3017 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2)
3023 int in1, in2, out1, out2;
3025 in1 = isl_multi_aff_dim(ma1, isl_dim_in);
3026 in2 = isl_multi_aff_dim(ma2, isl_dim_in);
3027 out1 = isl_multi_aff_dim(ma1, isl_dim_out);
3028 out2 = isl_multi_aff_dim(ma2, isl_dim_out);
3029 space = isl_space_product(isl_multi_aff_get_space(ma1),
3030 isl_multi_aff_get_space(ma2));
3031 res = isl_multi_aff_alloc(isl_space_copy(space));
3032 space = isl_space_domain(space);
3034 for (i = 0; i < out1; ++i) {
3035 aff = isl_multi_aff_get_aff(ma1, i);
3036 aff = isl_aff_insert_dims(aff, isl_dim_in, in1, in2);
3037 aff = isl_aff_reset_domain_space(aff, isl_space_copy(space));
3038 res = isl_multi_aff_set_aff(res, i, aff);
3041 for (i = 0; i < out2; ++i) {
3042 aff = isl_multi_aff_get_aff(ma2, i);
3043 aff = isl_aff_insert_dims(aff, isl_dim_in, 0, in1);
3044 aff = isl_aff_reset_domain_space(aff, isl_space_copy(space));
3045 res = isl_multi_aff_set_aff(res, out1 + i, aff);
3048 isl_space_free(space);
3049 isl_multi_aff_free(ma1);
3050 isl_multi_aff_free(ma2);
3054 /* Exploit the equalities in "eq" to simplify the affine expressions.
3056 static __isl_give isl_multi_aff *isl_multi_aff_substitute_equalities(
3057 __isl_take isl_multi_aff *maff, __isl_take isl_basic_set *eq)
3061 maff = isl_multi_aff_cow(maff);
3065 for (i = 0; i < maff->n; ++i) {
3066 maff->p[i] = isl_aff_substitute_equalities(maff->p[i],
3067 isl_basic_set_copy(eq));
3072 isl_basic_set_free(eq);
3075 isl_basic_set_free(eq);
3076 isl_multi_aff_free(maff);
3080 __isl_give isl_multi_aff *isl_multi_aff_scale(__isl_take isl_multi_aff *maff,
3085 maff = isl_multi_aff_cow(maff);
3089 for (i = 0; i < maff->n; ++i) {
3090 maff->p[i] = isl_aff_scale(maff->p[i], f);
3092 return isl_multi_aff_free(maff);
3098 __isl_give isl_multi_aff *isl_multi_aff_add_on_domain(__isl_keep isl_set *dom,
3099 __isl_take isl_multi_aff *maff1, __isl_take isl_multi_aff *maff2)
3101 maff1 = isl_multi_aff_add(maff1, maff2);
3102 maff1 = isl_multi_aff_gist(maff1, isl_set_copy(dom));
3106 int isl_multi_aff_is_empty(__isl_keep isl_multi_aff *maff)
3114 int isl_multi_aff_plain_is_equal(__isl_keep isl_multi_aff *maff1,
3115 __isl_keep isl_multi_aff *maff2)
3120 if (!maff1 || !maff2)
3122 if (maff1->n != maff2->n)
3124 equal = isl_space_is_equal(maff1->space, maff2->space);
3125 if (equal < 0 || !equal)
3128 for (i = 0; i < maff1->n; ++i) {
3129 equal = isl_aff_plain_is_equal(maff1->p[i], maff2->p[i]);
3130 if (equal < 0 || !equal)
3137 /* Return the set of domain elements where "ma1" is lexicographically
3138 * smaller than or equal to "ma2".
3140 __isl_give isl_set *isl_multi_aff_lex_le_set(__isl_take isl_multi_aff *ma1,
3141 __isl_take isl_multi_aff *ma2)
3143 return isl_multi_aff_lex_ge_set(ma2, ma1);
3146 /* Return the set of domain elements where "ma1" is lexicographically
3147 * greater than or equal to "ma2".
3149 __isl_give isl_set *isl_multi_aff_lex_ge_set(__isl_take isl_multi_aff *ma1,
3150 __isl_take isl_multi_aff *ma2)
3153 isl_map *map1, *map2;
3156 map1 = isl_map_from_multi_aff(ma1);
3157 map2 = isl_map_from_multi_aff(ma2);
3158 map = isl_map_range_product(map1, map2);
3159 space = isl_space_range(isl_map_get_space(map));
3160 space = isl_space_domain(isl_space_unwrap(space));
3161 ge = isl_map_lex_ge(space);
3162 map = isl_map_intersect_range(map, isl_map_wrap(ge));
3164 return isl_map_domain(map);
3168 #define PW isl_pw_multi_aff
3170 #define EL isl_multi_aff
3172 #define EL_IS_ZERO is_empty
3176 #define IS_ZERO is_empty
3179 #undef DEFAULT_IS_ZERO
3180 #define DEFAULT_IS_ZERO 0
3185 #define NO_INVOLVES_DIMS
3186 #define NO_MOVE_DIMS
3187 #define NO_INSERT_DIMS
3191 #include <isl_pw_templ.c>
3194 #define UNION isl_union_pw_multi_aff
3196 #define PART isl_pw_multi_aff
3198 #define PARTS pw_multi_aff
3199 #define ALIGN_DOMAIN
3203 #include <isl_union_templ.c>
3205 /* Given a function "cmp" that returns the set of elements where
3206 * "ma1" is "better" than "ma2", return the intersection of this
3207 * set with "dom1" and "dom2".
3209 static __isl_give isl_set *shared_and_better(__isl_keep isl_set *dom1,
3210 __isl_keep isl_set *dom2, __isl_keep isl_multi_aff *ma1,
3211 __isl_keep isl_multi_aff *ma2,
3212 __isl_give isl_set *(*cmp)(__isl_take isl_multi_aff *ma1,
3213 __isl_take isl_multi_aff *ma2))
3219 common = isl_set_intersect(isl_set_copy(dom1), isl_set_copy(dom2));
3220 is_empty = isl_set_plain_is_empty(common);
3221 if (is_empty >= 0 && is_empty)
3224 return isl_set_free(common);
3225 better = cmp(isl_multi_aff_copy(ma1), isl_multi_aff_copy(ma2));
3226 better = isl_set_intersect(common, better);
3231 /* Given a function "cmp" that returns the set of elements where
3232 * "ma1" is "better" than "ma2", return a piecewise multi affine
3233 * expression defined on the union of the definition domains
3234 * of "pma1" and "pma2" that maps to the "best" of "pma1" and
3235 * "pma2" on each cell. If only one of the two input functions
3236 * is defined on a given cell, then it is considered the best.
3238 static __isl_give isl_pw_multi_aff *pw_multi_aff_union_opt(
3239 __isl_take isl_pw_multi_aff *pma1,
3240 __isl_take isl_pw_multi_aff *pma2,
3241 __isl_give isl_set *(*cmp)(__isl_take isl_multi_aff *ma1,
3242 __isl_take isl_multi_aff *ma2))
3245 isl_pw_multi_aff *res = NULL;
3247 isl_set *set = NULL;
3252 ctx = isl_space_get_ctx(pma1->dim);
3253 if (!isl_space_is_equal(pma1->dim, pma2->dim))
3254 isl_die(ctx, isl_error_invalid,
3255 "arguments should live in the same space", goto error);
3257 if (isl_pw_multi_aff_is_empty(pma1)) {
3258 isl_pw_multi_aff_free(pma1);
3262 if (isl_pw_multi_aff_is_empty(pma2)) {
3263 isl_pw_multi_aff_free(pma2);
3267 n = 2 * (pma1->n + 1) * (pma2->n + 1);
3268 res = isl_pw_multi_aff_alloc_size(isl_space_copy(pma1->dim), n);
3270 for (i = 0; i < pma1->n; ++i) {
3271 set = isl_set_copy(pma1->p[i].set);
3272 for (j = 0; j < pma2->n; ++j) {
3276 better = shared_and_better(pma2->p[j].set,
3277 pma1->p[i].set, pma2->p[j].maff,
3278 pma1->p[i].maff, cmp);
3279 is_empty = isl_set_plain_is_empty(better);
3280 if (is_empty < 0 || is_empty) {
3281 isl_set_free(better);
3286 set = isl_set_subtract(set, isl_set_copy(better));
3288 res = isl_pw_multi_aff_add_piece(res, better,
3289 isl_multi_aff_copy(pma2->p[j].maff));
3291 res = isl_pw_multi_aff_add_piece(res, set,
3292 isl_multi_aff_copy(pma1->p[i].maff));
3295 for (j = 0; j < pma2->n; ++j) {
3296 set = isl_set_copy(pma2->p[j].set);
3297 for (i = 0; i < pma1->n; ++i)
3298 set = isl_set_subtract(set,
3299 isl_set_copy(pma1->p[i].set));
3300 res = isl_pw_multi_aff_add_piece(res, set,
3301 isl_multi_aff_copy(pma2->p[j].maff));
3304 isl_pw_multi_aff_free(pma1);
3305 isl_pw_multi_aff_free(pma2);
3309 isl_pw_multi_aff_free(pma1);
3310 isl_pw_multi_aff_free(pma2);
3312 return isl_pw_multi_aff_free(res);
3315 static __isl_give isl_pw_multi_aff *pw_multi_aff_union_lexmax(
3316 __isl_take isl_pw_multi_aff *pma1,
3317 __isl_take isl_pw_multi_aff *pma2)
3319 return pw_multi_aff_union_opt(pma1, pma2, &isl_multi_aff_lex_ge_set);
3322 /* Given two piecewise multi affine expressions, return a piecewise
3323 * multi-affine expression defined on the union of the definition domains
3324 * of the inputs that is equal to the lexicographic maximum of the two
3325 * inputs on each cell. If only one of the two inputs is defined on
3326 * a given cell, then it is considered to be the maximum.
3328 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_lexmax(
3329 __isl_take isl_pw_multi_aff *pma1,
3330 __isl_take isl_pw_multi_aff *pma2)
3332 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
3333 &pw_multi_aff_union_lexmax);
3336 static __isl_give isl_pw_multi_aff *pw_multi_aff_union_lexmin(
3337 __isl_take isl_pw_multi_aff *pma1,
3338 __isl_take isl_pw_multi_aff *pma2)
3340 return pw_multi_aff_union_opt(pma1, pma2, &isl_multi_aff_lex_le_set);
3343 /* Given two piecewise multi affine expressions, return a piecewise
3344 * multi-affine expression defined on the union of the definition domains
3345 * of the inputs that is equal to the lexicographic minimum of the two
3346 * inputs on each cell. If only one of the two inputs is defined on
3347 * a given cell, then it is considered to be the minimum.
3349 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_lexmin(
3350 __isl_take isl_pw_multi_aff *pma1,
3351 __isl_take isl_pw_multi_aff *pma2)
3353 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
3354 &pw_multi_aff_union_lexmin);
3357 static __isl_give isl_pw_multi_aff *pw_multi_aff_add(
3358 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
3360 return isl_pw_multi_aff_on_shared_domain(pma1, pma2,
3361 &isl_multi_aff_add);
3364 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_add(
3365 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
3367 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
3371 static __isl_give isl_pw_multi_aff *pw_multi_aff_sub(
3372 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
3374 return isl_pw_multi_aff_on_shared_domain(pma1, pma2,
3375 &isl_multi_aff_sub);
3378 /* Subtract "pma2" from "pma1" and return the result.
3380 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_sub(
3381 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
3383 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
3387 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_add(
3388 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
3390 return isl_pw_multi_aff_union_add_(pma1, pma2);
3393 /* Given two piecewise multi-affine expressions A -> B and C -> D,
3394 * construct a piecewise multi-affine expression [A -> C] -> [B -> D].
3396 static __isl_give isl_pw_multi_aff *pw_multi_aff_product(
3397 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
3401 isl_pw_multi_aff *res;
3406 n = pma1->n * pma2->n;
3407 space = isl_space_product(isl_space_copy(pma1->dim),
3408 isl_space_copy(pma2->dim));
3409 res = isl_pw_multi_aff_alloc_size(space, n);
3411 for (i = 0; i < pma1->n; ++i) {
3412 for (j = 0; j < pma2->n; ++j) {
3416 domain = isl_set_product(isl_set_copy(pma1->p[i].set),
3417 isl_set_copy(pma2->p[j].set));
3418 ma = isl_multi_aff_product(
3419 isl_multi_aff_copy(pma1->p[i].maff),
3420 isl_multi_aff_copy(pma2->p[i].maff));
3421 res = isl_pw_multi_aff_add_piece(res, domain, ma);
3425 isl_pw_multi_aff_free(pma1);
3426 isl_pw_multi_aff_free(pma2);
3429 isl_pw_multi_aff_free(pma1);
3430 isl_pw_multi_aff_free(pma2);
3434 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_product(
3435 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
3437 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
3438 &pw_multi_aff_product);
3441 /* Construct a map mapping the domain of the piecewise multi-affine expression
3442 * to its range, with each dimension in the range equated to the
3443 * corresponding affine expression on its cell.
3445 __isl_give isl_map *isl_map_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma)
3453 map = isl_map_empty(isl_pw_multi_aff_get_space(pma));
3455 for (i = 0; i < pma->n; ++i) {
3456 isl_multi_aff *maff;
3457 isl_basic_map *bmap;
3460 maff = isl_multi_aff_copy(pma->p[i].maff);
3461 bmap = isl_basic_map_from_multi_aff(maff);
3462 map_i = isl_map_from_basic_map(bmap);
3463 map_i = isl_map_intersect_domain(map_i,
3464 isl_set_copy(pma->p[i].set));
3465 map = isl_map_union_disjoint(map, map_i);
3468 isl_pw_multi_aff_free(pma);
3472 __isl_give isl_set *isl_set_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma)
3477 if (!isl_space_is_set(pma->dim))
3478 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
3479 "isl_pw_multi_aff cannot be converted into an isl_set",
3480 return isl_pw_multi_aff_free(pma));
3482 return isl_map_from_pw_multi_aff(pma);
3485 /* Given a basic map with a single output dimension that is defined
3486 * in terms of the parameters and input dimensions using an equality,
3487 * extract an isl_aff that expresses the output dimension in terms
3488 * of the parameters and input dimensions.
3490 * Since some applications expect the result of isl_pw_multi_aff_from_map
3491 * to only contain integer affine expressions, we compute the floor
3492 * of the expression before returning.
3494 * This function shares some similarities with
3495 * isl_basic_map_has_defining_equality and isl_constraint_get_bound.
3497 static __isl_give isl_aff *extract_isl_aff_from_basic_map(
3498 __isl_take isl_basic_map *bmap)
3503 isl_local_space *ls;
3508 if (isl_basic_map_dim(bmap, isl_dim_out) != 1)
3509 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
3510 "basic map should have a single output dimension",
3512 offset = isl_basic_map_offset(bmap, isl_dim_out);
3513 total = isl_basic_map_total_dim(bmap);
3514 for (i = 0; i < bmap->n_eq; ++i) {
3515 if (isl_int_is_zero(bmap->eq[i][offset]))
3517 if (isl_seq_first_non_zero(bmap->eq[i] + offset + 1,
3518 1 + total - (offset + 1)) != -1)
3522 if (i >= bmap->n_eq)
3523 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
3524 "unable to find suitable equality", goto error);
3525 ls = isl_basic_map_get_local_space(bmap);
3526 aff = isl_aff_alloc(isl_local_space_domain(ls));
3529 if (isl_int_is_neg(bmap->eq[i][offset]))
3530 isl_seq_cpy(aff->v->el + 1, bmap->eq[i], offset);
3532 isl_seq_neg(aff->v->el + 1, bmap->eq[i], offset);
3533 isl_seq_clr(aff->v->el + 1 + offset, aff->v->size - (1 + offset));
3534 isl_int_abs(aff->v->el[0], bmap->eq[i][offset]);
3535 isl_basic_map_free(bmap);
3537 aff = isl_aff_remove_unused_divs(aff);
3538 aff = isl_aff_floor(aff);
3541 isl_basic_map_free(bmap);
3545 /* Given a basic map where each output dimension is defined
3546 * in terms of the parameters and input dimensions using an equality,
3547 * extract an isl_multi_aff that expresses the output dimensions in terms
3548 * of the parameters and input dimensions.
3550 static __isl_give isl_multi_aff *extract_isl_multi_aff_from_basic_map(
3551 __isl_take isl_basic_map *bmap)
3560 ma = isl_multi_aff_alloc(isl_basic_map_get_space(bmap));
3561 n_out = isl_basic_map_dim(bmap, isl_dim_out);
3563 for (i = 0; i < n_out; ++i) {
3564 isl_basic_map *bmap_i;
3567 bmap_i = isl_basic_map_copy(bmap);
3568 bmap_i = isl_basic_map_project_out(bmap_i, isl_dim_out,
3569 i + 1, n_out - (1 + i));
3570 bmap_i = isl_basic_map_project_out(bmap_i, isl_dim_out, 0, i);
3571 aff = extract_isl_aff_from_basic_map(bmap_i);
3572 ma = isl_multi_aff_set_aff(ma, i, aff);
3575 isl_basic_map_free(bmap);
3580 /* Create an isl_pw_multi_aff that is equivalent to
3581 * isl_map_intersect_domain(isl_map_from_basic_map(bmap), domain).
3582 * The given basic map is such that each output dimension is defined
3583 * in terms of the parameters and input dimensions using an equality.
3585 static __isl_give isl_pw_multi_aff *plain_pw_multi_aff_from_map(
3586 __isl_take isl_set *domain, __isl_take isl_basic_map *bmap)
3590 ma = extract_isl_multi_aff_from_basic_map(bmap);
3591 return isl_pw_multi_aff_alloc(domain, ma);
3594 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
3595 * This obviously only works if the input "map" is single-valued.
3596 * If so, we compute the lexicographic minimum of the image in the form
3597 * of an isl_pw_multi_aff. Since the image is unique, it is equal
3598 * to its lexicographic minimum.
3599 * If the input is not single-valued, we produce an error.
3601 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_base(
3602 __isl_take isl_map *map)
3606 isl_pw_multi_aff *pma;
3608 sv = isl_map_is_single_valued(map);
3612 isl_die(isl_map_get_ctx(map), isl_error_invalid,
3613 "map is not single-valued", goto error);
3614 map = isl_map_make_disjoint(map);
3618 pma = isl_pw_multi_aff_empty(isl_map_get_space(map));
3620 for (i = 0; i < map->n; ++i) {
3621 isl_pw_multi_aff *pma_i;
3622 isl_basic_map *bmap;
3623 bmap = isl_basic_map_copy(map->p[i]);
3624 pma_i = isl_basic_map_lexmin_pw_multi_aff(bmap);
3625 pma = isl_pw_multi_aff_add_disjoint(pma, pma_i);
3635 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
3636 * taking into account that the output dimension at position "d"
3637 * can be represented as
3639 * x = floor((e(...) + c1) / m)
3641 * given that constraint "i" is of the form
3643 * e(...) + c1 - m x >= 0
3646 * Let "map" be of the form
3650 * We construct a mapping
3652 * A -> [A -> x = floor(...)]
3654 * apply that to the map, obtaining
3656 * [A -> x = floor(...)] -> B
3658 * and equate dimension "d" to x.
3659 * We then compute a isl_pw_multi_aff representation of the resulting map
3660 * and plug in the mapping above.
3662 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_div(
3663 __isl_take isl_map *map, __isl_take isl_basic_map *hull, int d, int i)
3667 isl_local_space *ls;
3675 isl_pw_multi_aff *pma;
3678 is_set = isl_map_is_set(map);
3680 offset = isl_basic_map_offset(hull, isl_dim_out);
3681 ctx = isl_map_get_ctx(map);
3682 space = isl_space_domain(isl_map_get_space(map));
3683 n_in = isl_space_dim(space, isl_dim_set);
3684 n = isl_space_dim(space, isl_dim_all);
3686 v = isl_vec_alloc(ctx, 1 + 1 + n);
3688 isl_int_neg(v->el[0], hull->ineq[i][offset + d]);
3689 isl_seq_cpy(v->el + 1, hull->ineq[i], 1 + n);
3691 isl_basic_map_free(hull);
3693 ls = isl_local_space_from_space(isl_space_copy(space));
3694 aff = isl_aff_alloc_vec(ls, v);
3695 aff = isl_aff_floor(aff);
3697 isl_space_free(space);
3698 ma = isl_multi_aff_from_aff(aff);
3700 ma = isl_multi_aff_identity(isl_space_map_from_set(space));
3701 ma = isl_multi_aff_range_product(ma,
3702 isl_multi_aff_from_aff(aff));
3705 insert = isl_map_from_multi_aff(isl_multi_aff_copy(ma));
3706 map = isl_map_apply_domain(map, insert);
3707 map = isl_map_equate(map, isl_dim_in, n_in, isl_dim_out, d);
3708 pma = isl_pw_multi_aff_from_map(map);
3709 pma = isl_pw_multi_aff_pullback_multi_aff(pma, ma);
3714 /* Is constraint "c" of the form
3716 * e(...) + c1 - m x >= 0
3720 * -e(...) + c2 + m x >= 0
3722 * where m > 1 and e only depends on parameters and input dimemnsions?
3724 * "offset" is the offset of the output dimensions
3725 * "pos" is the position of output dimension x.
3727 static int is_potential_div_constraint(isl_int *c, int offset, int d, int total)
3729 if (isl_int_is_zero(c[offset + d]))
3731 if (isl_int_is_one(c[offset + d]))
3733 if (isl_int_is_negone(c[offset + d]))
3735 if (isl_seq_first_non_zero(c + offset, d) != -1)
3737 if (isl_seq_first_non_zero(c + offset + d + 1,
3738 total - (offset + d + 1)) != -1)
3743 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
3745 * As a special case, we first check if there is any pair of constraints,
3746 * shared by all the basic maps in "map" that force a given dimension
3747 * to be equal to the floor of some affine combination of the input dimensions.
3749 * In particular, if we can find two constraints
3751 * e(...) + c1 - m x >= 0 i.e., m x <= e(...) + c1
3755 * -e(...) + c2 + m x >= 0 i.e., m x >= e(...) - c2
3757 * where m > 1 and e only depends on parameters and input dimemnsions,
3760 * c1 + c2 < m i.e., -c2 >= c1 - (m - 1)
3762 * then we know that we can take
3764 * x = floor((e(...) + c1) / m)
3766 * without having to perform any computation.
3768 * Note that we know that
3772 * If c1 + c2 were 0, then we would have detected an equality during
3773 * simplification. If c1 + c2 were negative, then we would have detected
3776 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_check_div(
3777 __isl_take isl_map *map)
3783 isl_basic_map *hull;
3785 hull = isl_map_unshifted_simple_hull(isl_map_copy(map));
3790 dim = isl_map_dim(map, isl_dim_out);
3791 offset = isl_basic_map_offset(hull, isl_dim_out);
3792 total = 1 + isl_basic_map_total_dim(hull);
3794 for (d = 0; d < dim; ++d) {
3795 for (i = 0; i < n; ++i) {
3796 if (!is_potential_div_constraint(hull->ineq[i],
3799 for (j = i + 1; j < n; ++j) {
3800 if (!isl_seq_is_neg(hull->ineq[i] + 1,
3801 hull->ineq[j] + 1, total - 1))
3803 isl_int_add(sum, hull->ineq[i][0],
3805 if (isl_int_abs_lt(sum,
3806 hull->ineq[i][offset + d]))
3813 if (isl_int_is_pos(hull->ineq[j][offset + d]))
3815 return pw_multi_aff_from_map_div(map, hull, d, j);
3819 isl_basic_map_free(hull);
3820 return pw_multi_aff_from_map_base(map);
3823 isl_basic_map_free(hull);
3827 /* Given an affine expression
3829 * [A -> B] -> f(A,B)
3831 * construct an isl_multi_aff
3835 * such that dimension "d" in B' is set to "aff" and the remaining
3836 * dimensions are set equal to the corresponding dimensions in B.
3837 * "n_in" is the dimension of the space A.
3838 * "n_out" is the dimension of the space B.
3840 * If "is_set" is set, then the affine expression is of the form
3844 * and we construct an isl_multi_aff
3848 static __isl_give isl_multi_aff *range_map(__isl_take isl_aff *aff, int d,
3849 unsigned n_in, unsigned n_out, int is_set)
3853 isl_space *space, *space2;
3854 isl_local_space *ls;
3856 space = isl_aff_get_domain_space(aff);
3857 ls = isl_local_space_from_space(isl_space_copy(space));
3858 space2 = isl_space_copy(space);
3860 space2 = isl_space_range(isl_space_unwrap(space2));
3861 space = isl_space_map_from_domain_and_range(space, space2);
3862 ma = isl_multi_aff_alloc(space);
3863 ma = isl_multi_aff_set_aff(ma, d, aff);
3865 for (i = 0; i < n_out; ++i) {
3868 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
3869 isl_dim_set, n_in + i);
3870 ma = isl_multi_aff_set_aff(ma, i, aff);
3873 isl_local_space_free(ls);
3878 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
3879 * taking into account that the dimension at position "d" can be written as
3881 * x = m a + f(..) (1)
3883 * where m is equal to "gcd".
3884 * "i" is the index of the equality in "hull" that defines f(..).
3885 * In particular, the equality is of the form
3887 * f(..) - x + m g(existentials) = 0
3891 * -f(..) + x + m g(existentials) = 0
3893 * We basically plug (1) into "map", resulting in a map with "a"
3894 * in the range instead of "x". The corresponding isl_pw_multi_aff
3895 * defining "a" is then plugged back into (1) to obtain a definition fro "x".
3897 * Specifically, given the input map
3901 * We first wrap it into a set
3905 * and define (1) on top of the corresponding space, resulting in "aff".
3906 * We use this to create an isl_multi_aff that maps the output position "d"
3907 * from "a" to "x", leaving all other (intput and output) dimensions unchanged.
3908 * We plug this into the wrapped map, unwrap the result and compute the
3909 * corresponding isl_pw_multi_aff.
3910 * The result is an expression
3918 * so that we can plug that into "aff", after extending the latter to
3924 * If "map" is actually a set, then there is no "A" space, meaning
3925 * that we do not need to perform any wrapping, and that the result
3926 * of the recursive call is of the form
3930 * which is plugged into a mapping of the form
3934 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_stride(
3935 __isl_take isl_map *map, __isl_take isl_basic_map *hull, int d, int i,
3940 isl_local_space *ls;
3943 isl_pw_multi_aff *pma, *id;
3949 is_set = isl_map_is_set(map);
3951 n_in = isl_basic_map_dim(hull, isl_dim_in);
3952 n_out = isl_basic_map_dim(hull, isl_dim_out);
3953 o_out = isl_basic_map_offset(hull, isl_dim_out);
3958 set = isl_map_wrap(map);
3959 space = isl_space_map_from_set(isl_set_get_space(set));
3960 ma = isl_multi_aff_identity(space);
3961 ls = isl_local_space_from_space(isl_set_get_space(set));
3962 aff = isl_aff_alloc(ls);
3964 isl_int_set_si(aff->v->el[0], 1);
3965 if (isl_int_is_one(hull->eq[i][o_out + d]))
3966 isl_seq_neg(aff->v->el + 1, hull->eq[i],
3969 isl_seq_cpy(aff->v->el + 1, hull->eq[i],
3971 isl_int_set(aff->v->el[1 + o_out + d], gcd);
3973 ma = isl_multi_aff_set_aff(ma, n_in + d, isl_aff_copy(aff));
3974 set = isl_set_preimage_multi_aff(set, ma);
3976 ma = range_map(aff, d, n_in, n_out, is_set);
3981 map = isl_set_unwrap(set);
3982 pma = isl_pw_multi_aff_from_map(set);
3985 space = isl_pw_multi_aff_get_domain_space(pma);
3986 space = isl_space_map_from_set(space);
3987 id = isl_pw_multi_aff_identity(space);
3988 pma = isl_pw_multi_aff_range_product(id, pma);
3990 id = isl_pw_multi_aff_from_multi_aff(ma);
3991 pma = isl_pw_multi_aff_pullback_pw_multi_aff(id, pma);
3993 isl_basic_map_free(hull);
3997 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
3999 * As a special case, we first check if all output dimensions are uniquely
4000 * defined in terms of the parameters and input dimensions over the entire
4001 * domain. If so, we extract the desired isl_pw_multi_aff directly
4002 * from the affine hull of "map" and its domain.
4004 * Otherwise, we check if any of the output dimensions is "strided".
4005 * That is, we check if can be written as
4009 * with m greater than 1, a some combination of existentiall quantified
4010 * variables and f and expression in the parameters and input dimensions.
4011 * If so, we remove the stride in pw_multi_aff_from_map_stride.
4013 * Otherwise, we continue with pw_multi_aff_from_map_check_div for a further
4016 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_map(__isl_take isl_map *map)
4020 isl_basic_map *hull;
4030 hull = isl_map_affine_hull(isl_map_copy(map));
4031 sv = isl_basic_map_plain_is_single_valued(hull);
4033 return plain_pw_multi_aff_from_map(isl_map_domain(map), hull);
4035 hull = isl_basic_map_free(hull);
4039 n_div = isl_basic_map_dim(hull, isl_dim_div);
4040 o_div = isl_basic_map_offset(hull, isl_dim_div);
4043 isl_basic_map_free(hull);
4044 return pw_multi_aff_from_map_check_div(map);
4049 n_out = isl_basic_map_dim(hull, isl_dim_out);
4050 o_out = isl_basic_map_offset(hull, isl_dim_out);
4052 for (i = 0; i < n_out; ++i) {
4053 for (j = 0; j < hull->n_eq; ++j) {
4054 isl_int *eq = hull->eq[j];
4055 isl_pw_multi_aff *res;
4057 if (!isl_int_is_one(eq[o_out + i]) &&
4058 !isl_int_is_negone(eq[o_out + i]))
4060 if (isl_seq_first_non_zero(eq + o_out, i) != -1)
4062 if (isl_seq_first_non_zero(eq + o_out + i + 1,
4063 n_out - (i + 1)) != -1)
4065 isl_seq_gcd(eq + o_div, n_div, &gcd);
4066 if (isl_int_is_zero(gcd))
4068 if (isl_int_is_one(gcd))
4071 res = pw_multi_aff_from_map_stride(map, hull,
4079 isl_basic_map_free(hull);
4080 return pw_multi_aff_from_map_check_div(map);
4086 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_set(__isl_take isl_set *set)
4088 return isl_pw_multi_aff_from_map(set);
4091 /* Convert "map" into an isl_pw_multi_aff (if possible) and
4094 static int pw_multi_aff_from_map(__isl_take isl_map *map, void *user)
4096 isl_union_pw_multi_aff **upma = user;
4097 isl_pw_multi_aff *pma;
4099 pma = isl_pw_multi_aff_from_map(map);
4100 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
4102 return *upma ? 0 : -1;
4105 /* Try and create an isl_union_pw_multi_aff that is equivalent
4106 * to the given isl_union_map.
4107 * The isl_union_map is required to be single-valued in each space.
4108 * Otherwise, an error is produced.
4110 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_map(
4111 __isl_take isl_union_map *umap)
4114 isl_union_pw_multi_aff *upma;
4116 space = isl_union_map_get_space(umap);
4117 upma = isl_union_pw_multi_aff_empty(space);
4118 if (isl_union_map_foreach_map(umap, &pw_multi_aff_from_map, &upma) < 0)
4119 upma = isl_union_pw_multi_aff_free(upma);
4120 isl_union_map_free(umap);
4125 /* Try and create an isl_union_pw_multi_aff that is equivalent
4126 * to the given isl_union_set.
4127 * The isl_union_set is required to be a singleton in each space.
4128 * Otherwise, an error is produced.
4130 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_set(
4131 __isl_take isl_union_set *uset)
4133 return isl_union_pw_multi_aff_from_union_map(uset);
4136 /* Return the piecewise affine expression "set ? 1 : 0".
4138 __isl_give isl_pw_aff *isl_set_indicator_function(__isl_take isl_set *set)
4141 isl_space *space = isl_set_get_space(set);
4142 isl_local_space *ls = isl_local_space_from_space(space);
4143 isl_aff *zero = isl_aff_zero_on_domain(isl_local_space_copy(ls));
4144 isl_aff *one = isl_aff_zero_on_domain(ls);
4146 one = isl_aff_add_constant_si(one, 1);
4147 pa = isl_pw_aff_alloc(isl_set_copy(set), one);
4148 set = isl_set_complement(set);
4149 pa = isl_pw_aff_add_disjoint(pa, isl_pw_aff_alloc(set, zero));
4154 /* Plug in "subs" for dimension "type", "pos" of "aff".
4156 * Let i be the dimension to replace and let "subs" be of the form
4160 * and "aff" of the form
4166 * (a f + d g')/(m d)
4168 * where g' is the result of plugging in "subs" in each of the integer
4171 __isl_give isl_aff *isl_aff_substitute(__isl_take isl_aff *aff,
4172 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
4177 aff = isl_aff_cow(aff);
4179 return isl_aff_free(aff);
4181 ctx = isl_aff_get_ctx(aff);
4182 if (!isl_space_is_equal(aff->ls->dim, subs->ls->dim))
4183 isl_die(ctx, isl_error_invalid,
4184 "spaces don't match", return isl_aff_free(aff));
4185 if (isl_local_space_dim(subs->ls, isl_dim_div) != 0)
4186 isl_die(ctx, isl_error_unsupported,
4187 "cannot handle divs yet", return isl_aff_free(aff));
4189 aff->ls = isl_local_space_substitute(aff->ls, type, pos, subs);
4191 return isl_aff_free(aff);
4193 aff->v = isl_vec_cow(aff->v);
4195 return isl_aff_free(aff);
4197 pos += isl_local_space_offset(aff->ls, type);
4200 isl_seq_substitute(aff->v->el, pos, subs->v->el,
4201 aff->v->size, subs->v->size, v);
4207 /* Plug in "subs" for dimension "type", "pos" in each of the affine
4208 * expressions in "maff".
4210 __isl_give isl_multi_aff *isl_multi_aff_substitute(
4211 __isl_take isl_multi_aff *maff, enum isl_dim_type type, unsigned pos,
4212 __isl_keep isl_aff *subs)
4216 maff = isl_multi_aff_cow(maff);
4218 return isl_multi_aff_free(maff);
4220 if (type == isl_dim_in)
4223 for (i = 0; i < maff->n; ++i) {
4224 maff->p[i] = isl_aff_substitute(maff->p[i], type, pos, subs);
4226 return isl_multi_aff_free(maff);
4232 /* Plug in "subs" for dimension "type", "pos" of "pma".
4234 * pma is of the form
4238 * while subs is of the form
4240 * v' = B_j(v) -> S_j
4242 * Each pair i,j such that C_ij = A_i \cap B_i is non-empty
4243 * has a contribution in the result, in particular
4245 * C_ij(S_j) -> M_i(S_j)
4247 * Note that plugging in S_j in C_ij may also result in an empty set
4248 * and this contribution should simply be discarded.
4250 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_substitute(
4251 __isl_take isl_pw_multi_aff *pma, enum isl_dim_type type, unsigned pos,
4252 __isl_keep isl_pw_aff *subs)
4255 isl_pw_multi_aff *res;
4258 return isl_pw_multi_aff_free(pma);
4260 n = pma->n * subs->n;
4261 res = isl_pw_multi_aff_alloc_size(isl_space_copy(pma->dim), n);
4263 for (i = 0; i < pma->n; ++i) {
4264 for (j = 0; j < subs->n; ++j) {
4266 isl_multi_aff *res_ij;
4269 common = isl_set_intersect(
4270 isl_set_copy(pma->p[i].set),
4271 isl_set_copy(subs->p[j].set));
4272 common = isl_set_substitute(common,
4273 type, pos, subs->p[j].aff);
4274 empty = isl_set_plain_is_empty(common);
4275 if (empty < 0 || empty) {
4276 isl_set_free(common);
4282 res_ij = isl_multi_aff_substitute(
4283 isl_multi_aff_copy(pma->p[i].maff),
4284 type, pos, subs->p[j].aff);
4286 res = isl_pw_multi_aff_add_piece(res, common, res_ij);
4290 isl_pw_multi_aff_free(pma);
4293 isl_pw_multi_aff_free(pma);
4294 isl_pw_multi_aff_free(res);
4298 /* Compute the preimage of a range of dimensions in the affine expression "src"
4299 * under "ma" and put the result in "dst". The number of dimensions in "src"
4300 * that precede the range is given by "n_before". The number of dimensions
4301 * in the range is given by the number of output dimensions of "ma".
4302 * The number of dimensions that follow the range is given by "n_after".
4303 * If "has_denom" is set (to one),
4304 * then "src" and "dst" have an extra initial denominator.
4305 * "n_div_ma" is the number of existentials in "ma"
4306 * "n_div_bset" is the number of existentials in "src"
4307 * The resulting "dst" (which is assumed to have been allocated by
4308 * the caller) contains coefficients for both sets of existentials,
4309 * first those in "ma" and then those in "src".
4310 * f, c1, c2 and g are temporary objects that have been initialized
4313 * Let src represent the expression
4315 * (a(p) + f_u u + b v + f_w w + c(divs))/d
4317 * and let ma represent the expressions
4319 * v_i = (r_i(p) + s_i(y) + t_i(divs'))/m_i
4321 * We start out with the following expression for dst:
4323 * (a(p) + f_u u + 0 y + f_w w + 0 divs' + c(divs) + f \sum_i b_i v_i)/d
4325 * with the multiplication factor f initially equal to 1
4326 * and f \sum_i b_i v_i kept separately.
4327 * For each x_i that we substitute, we multiply the numerator
4328 * (and denominator) of dst by c_1 = m_i and add the numerator
4329 * of the x_i expression multiplied by c_2 = f b_i,
4330 * after removing the common factors of c_1 and c_2.
4331 * The multiplication factor f also needs to be multiplied by c_1
4332 * for the next x_j, j > i.
4334 void isl_seq_preimage(isl_int *dst, isl_int *src,
4335 __isl_keep isl_multi_aff *ma, int n_before, int n_after,
4336 int n_div_ma, int n_div_bmap,
4337 isl_int f, isl_int c1, isl_int c2, isl_int g, int has_denom)
4340 int n_param, n_in, n_out;
4343 n_param = isl_multi_aff_dim(ma, isl_dim_param);
4344 n_in = isl_multi_aff_dim(ma, isl_dim_in);
4345 n_out = isl_multi_aff_dim(ma, isl_dim_out);
4347 isl_seq_cpy(dst, src, has_denom + 1 + n_param + n_before);
4348 o_dst = o_src = has_denom + 1 + n_param + n_before;
4349 isl_seq_clr(dst + o_dst, n_in);
4352 isl_seq_cpy(dst + o_dst, src + o_src, n_after);
4355 isl_seq_clr(dst + o_dst, n_div_ma);
4357 isl_seq_cpy(dst + o_dst, src + o_src, n_div_bmap);
4359 isl_int_set_si(f, 1);
4361 for (i = 0; i < n_out; ++i) {
4362 int offset = has_denom + 1 + n_param + n_before + i;
4364 if (isl_int_is_zero(src[offset]))
4366 isl_int_set(c1, ma->p[i]->v->el[0]);
4367 isl_int_mul(c2, f, src[offset]);
4368 isl_int_gcd(g, c1, c2);
4369 isl_int_divexact(c1, c1, g);
4370 isl_int_divexact(c2, c2, g);
4372 isl_int_mul(f, f, c1);
4375 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
4376 c2, ma->p[i]->v->el + o_src, 1 + n_param);
4377 o_dst += 1 + n_param;
4378 o_src += 1 + n_param;
4379 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_before);
4381 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
4382 c2, ma->p[i]->v->el + o_src, n_in);
4385 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_after);
4387 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
4388 c2, ma->p[i]->v->el + o_src, n_div_ma);
4391 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_div_bmap);
4393 isl_int_mul(dst[0], dst[0], c1);
4397 /* Compute the pullback of "aff" by the function represented by "ma".
4398 * In other words, plug in "ma" in "aff". The result is an affine expression
4399 * defined over the domain space of "ma".
4401 * If "aff" is represented by
4403 * (a(p) + b x + c(divs))/d
4405 * and ma is represented by
4407 * x = D(p) + F(y) + G(divs')
4409 * then the result is
4411 * (a(p) + b D(p) + b F(y) + b G(divs') + c(divs))/d
4413 * The divs in the local space of the input are similarly adjusted
4414 * through a call to isl_local_space_preimage_multi_aff.
4416 __isl_give isl_aff *isl_aff_pullback_multi_aff(__isl_take isl_aff *aff,
4417 __isl_take isl_multi_aff *ma)
4419 isl_aff *res = NULL;
4420 isl_local_space *ls;
4421 int n_div_aff, n_div_ma;
4422 isl_int f, c1, c2, g;
4424 ma = isl_multi_aff_align_divs(ma);
4428 n_div_aff = isl_aff_dim(aff, isl_dim_div);
4429 n_div_ma = ma->n ? isl_aff_dim(ma->p[0], isl_dim_div) : 0;
4431 ls = isl_aff_get_domain_local_space(aff);
4432 ls = isl_local_space_preimage_multi_aff(ls, isl_multi_aff_copy(ma));
4433 res = isl_aff_alloc(ls);
4442 isl_seq_preimage(res->v->el, aff->v->el, ma, 0, 0, n_div_ma, n_div_aff,
4451 isl_multi_aff_free(ma);
4452 res = isl_aff_normalize(res);
4456 isl_multi_aff_free(ma);
4461 /* Compute the pullback of "ma1" by the function represented by "ma2".
4462 * In other words, plug in "ma2" in "ma1".
4464 __isl_give isl_multi_aff *isl_multi_aff_pullback_multi_aff(
4465 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2)
4468 isl_space *space = NULL;
4470 ma2 = isl_multi_aff_align_divs(ma2);
4471 ma1 = isl_multi_aff_cow(ma1);
4475 space = isl_space_join(isl_multi_aff_get_space(ma2),
4476 isl_multi_aff_get_space(ma1));
4478 for (i = 0; i < ma1->n; ++i) {
4479 ma1->p[i] = isl_aff_pullback_multi_aff(ma1->p[i],
4480 isl_multi_aff_copy(ma2));
4485 ma1 = isl_multi_aff_reset_space(ma1, space);
4486 isl_multi_aff_free(ma2);
4489 isl_space_free(space);
4490 isl_multi_aff_free(ma2);
4491 isl_multi_aff_free(ma1);
4495 /* Extend the local space of "dst" to include the divs
4496 * in the local space of "src".
4498 __isl_give isl_aff *isl_aff_align_divs(__isl_take isl_aff *dst,
4499 __isl_keep isl_aff *src)
4507 return isl_aff_free(dst);
4509 ctx = isl_aff_get_ctx(src);
4510 if (!isl_space_is_equal(src->ls->dim, dst->ls->dim))
4511 isl_die(ctx, isl_error_invalid,
4512 "spaces don't match", goto error);
4514 if (src->ls->div->n_row == 0)
4517 exp1 = isl_alloc_array(ctx, int, src->ls->div->n_row);
4518 exp2 = isl_alloc_array(ctx, int, dst->ls->div->n_row);
4522 div = isl_merge_divs(src->ls->div, dst->ls->div, exp1, exp2);
4523 dst = isl_aff_expand_divs(dst, div, exp2);
4531 return isl_aff_free(dst);
4534 /* Adjust the local spaces of the affine expressions in "maff"
4535 * such that they all have the save divs.
4537 __isl_give isl_multi_aff *isl_multi_aff_align_divs(
4538 __isl_take isl_multi_aff *maff)
4546 maff = isl_multi_aff_cow(maff);
4550 for (i = 1; i < maff->n; ++i)
4551 maff->p[0] = isl_aff_align_divs(maff->p[0], maff->p[i]);
4552 for (i = 1; i < maff->n; ++i) {
4553 maff->p[i] = isl_aff_align_divs(maff->p[i], maff->p[0]);
4555 return isl_multi_aff_free(maff);
4561 __isl_give isl_aff *isl_aff_lift(__isl_take isl_aff *aff)
4563 aff = isl_aff_cow(aff);
4567 aff->ls = isl_local_space_lift(aff->ls);
4569 return isl_aff_free(aff);
4574 /* Lift "maff" to a space with extra dimensions such that the result
4575 * has no more existentially quantified variables.
4576 * If "ls" is not NULL, then *ls is assigned the local space that lies
4577 * at the basis of the lifting applied to "maff".
4579 __isl_give isl_multi_aff *isl_multi_aff_lift(__isl_take isl_multi_aff *maff,
4580 __isl_give isl_local_space **ls)
4594 isl_space *space = isl_multi_aff_get_domain_space(maff);
4595 *ls = isl_local_space_from_space(space);
4597 return isl_multi_aff_free(maff);
4602 maff = isl_multi_aff_cow(maff);
4603 maff = isl_multi_aff_align_divs(maff);
4607 n_div = isl_aff_dim(maff->p[0], isl_dim_div);
4608 space = isl_multi_aff_get_space(maff);
4609 space = isl_space_lift(isl_space_domain(space), n_div);
4610 space = isl_space_extend_domain_with_range(space,
4611 isl_multi_aff_get_space(maff));
4613 return isl_multi_aff_free(maff);
4614 isl_space_free(maff->space);
4615 maff->space = space;
4618 *ls = isl_aff_get_domain_local_space(maff->p[0]);
4620 return isl_multi_aff_free(maff);
4623 for (i = 0; i < maff->n; ++i) {
4624 maff->p[i] = isl_aff_lift(maff->p[i]);
4632 isl_local_space_free(*ls);
4633 return isl_multi_aff_free(maff);
4637 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma".
4639 __isl_give isl_pw_aff *isl_pw_multi_aff_get_pw_aff(
4640 __isl_keep isl_pw_multi_aff *pma, int pos)
4650 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
4651 if (pos < 0 || pos >= n_out)
4652 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
4653 "index out of bounds", return NULL);
4655 space = isl_pw_multi_aff_get_space(pma);
4656 space = isl_space_drop_dims(space, isl_dim_out,
4657 pos + 1, n_out - pos - 1);
4658 space = isl_space_drop_dims(space, isl_dim_out, 0, pos);
4660 pa = isl_pw_aff_alloc_size(space, pma->n);
4661 for (i = 0; i < pma->n; ++i) {
4663 aff = isl_multi_aff_get_aff(pma->p[i].maff, pos);
4664 pa = isl_pw_aff_add_piece(pa, isl_set_copy(pma->p[i].set), aff);
4670 /* Return an isl_pw_multi_aff with the given "set" as domain and
4671 * an unnamed zero-dimensional range.
4673 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_domain(
4674 __isl_take isl_set *set)
4679 space = isl_set_get_space(set);
4680 space = isl_space_from_domain(space);
4681 ma = isl_multi_aff_zero(space);
4682 return isl_pw_multi_aff_alloc(set, ma);
4685 /* Add an isl_pw_multi_aff with the given "set" as domain and
4686 * an unnamed zero-dimensional range to *user.
4688 static int add_pw_multi_aff_from_domain(__isl_take isl_set *set, void *user)
4690 isl_union_pw_multi_aff **upma = user;
4691 isl_pw_multi_aff *pma;
4693 pma = isl_pw_multi_aff_from_domain(set);
4694 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
4699 /* Return an isl_union_pw_multi_aff with the given "uset" as domain and
4700 * an unnamed zero-dimensional range.
4702 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_domain(
4703 __isl_take isl_union_set *uset)
4706 isl_union_pw_multi_aff *upma;
4711 space = isl_union_set_get_space(uset);
4712 upma = isl_union_pw_multi_aff_empty(space);
4714 if (isl_union_set_foreach_set(uset,
4715 &add_pw_multi_aff_from_domain, &upma) < 0)
4718 isl_union_set_free(uset);
4721 isl_union_set_free(uset);
4722 isl_union_pw_multi_aff_free(upma);
4726 /* Convert "pma" to an isl_map and add it to *umap.
4728 static int map_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma, void *user)
4730 isl_union_map **umap = user;
4733 map = isl_map_from_pw_multi_aff(pma);
4734 *umap = isl_union_map_add_map(*umap, map);
4739 /* Construct a union map mapping the domain of the union
4740 * piecewise multi-affine expression to its range, with each dimension
4741 * in the range equated to the corresponding affine expression on its cell.
4743 __isl_give isl_union_map *isl_union_map_from_union_pw_multi_aff(
4744 __isl_take isl_union_pw_multi_aff *upma)
4747 isl_union_map *umap;
4752 space = isl_union_pw_multi_aff_get_space(upma);
4753 umap = isl_union_map_empty(space);
4755 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
4756 &map_from_pw_multi_aff, &umap) < 0)
4759 isl_union_pw_multi_aff_free(upma);
4762 isl_union_pw_multi_aff_free(upma);
4763 isl_union_map_free(umap);
4767 /* Local data for bin_entry and the callback "fn".
4769 struct isl_union_pw_multi_aff_bin_data {
4770 isl_union_pw_multi_aff *upma2;
4771 isl_union_pw_multi_aff *res;
4772 isl_pw_multi_aff *pma;
4773 int (*fn)(void **entry, void *user);
4776 /* Given an isl_pw_multi_aff from upma1, store it in data->pma
4777 * and call data->fn for each isl_pw_multi_aff in data->upma2.
4779 static int bin_entry(void **entry, void *user)
4781 struct isl_union_pw_multi_aff_bin_data *data = user;
4782 isl_pw_multi_aff *pma = *entry;
4785 if (isl_hash_table_foreach(data->upma2->dim->ctx, &data->upma2->table,
4786 data->fn, data) < 0)
4792 /* Call "fn" on each pair of isl_pw_multi_affs in "upma1" and "upma2".
4793 * The isl_pw_multi_aff from upma1 is stored in data->pma (where data is
4794 * passed as user field) and the isl_pw_multi_aff from upma2 is available
4795 * as *entry. The callback should adjust data->res if desired.
4797 static __isl_give isl_union_pw_multi_aff *bin_op(
4798 __isl_take isl_union_pw_multi_aff *upma1,
4799 __isl_take isl_union_pw_multi_aff *upma2,
4800 int (*fn)(void **entry, void *user))
4803 struct isl_union_pw_multi_aff_bin_data data = { NULL, NULL, NULL, fn };
4805 space = isl_union_pw_multi_aff_get_space(upma2);
4806 upma1 = isl_union_pw_multi_aff_align_params(upma1, space);
4807 space = isl_union_pw_multi_aff_get_space(upma1);
4808 upma2 = isl_union_pw_multi_aff_align_params(upma2, space);
4810 if (!upma1 || !upma2)
4814 data.res = isl_union_pw_multi_aff_alloc(isl_space_copy(upma1->dim),
4816 if (isl_hash_table_foreach(upma1->dim->ctx, &upma1->table,
4817 &bin_entry, &data) < 0)
4820 isl_union_pw_multi_aff_free(upma1);
4821 isl_union_pw_multi_aff_free(upma2);
4824 isl_union_pw_multi_aff_free(upma1);
4825 isl_union_pw_multi_aff_free(upma2);
4826 isl_union_pw_multi_aff_free(data.res);
4830 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
4831 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
4833 static __isl_give isl_pw_multi_aff *pw_multi_aff_range_product(
4834 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4838 space = isl_space_range_product(isl_pw_multi_aff_get_space(pma1),
4839 isl_pw_multi_aff_get_space(pma2));
4840 return isl_pw_multi_aff_on_shared_domain_in(pma1, pma2, space,
4841 &isl_multi_aff_range_product);
4844 /* Given two isl_pw_multi_affs A -> B and C -> D,
4845 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
4847 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_range_product(
4848 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4850 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4851 &pw_multi_aff_range_product);
4854 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
4855 * construct an isl_pw_multi_aff (A * C) -> (B, D).
4857 static __isl_give isl_pw_multi_aff *pw_multi_aff_flat_range_product(
4858 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4862 space = isl_space_range_product(isl_pw_multi_aff_get_space(pma1),
4863 isl_pw_multi_aff_get_space(pma2));
4864 space = isl_space_flatten_range(space);
4865 return isl_pw_multi_aff_on_shared_domain_in(pma1, pma2, space,
4866 &isl_multi_aff_flat_range_product);
4869 /* Given two isl_pw_multi_affs A -> B and C -> D,
4870 * construct an isl_pw_multi_aff (A * C) -> (B, D).
4872 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_flat_range_product(
4873 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4875 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4876 &pw_multi_aff_flat_range_product);
4879 /* If data->pma and *entry have the same domain space, then compute
4880 * their flat range product and the result to data->res.
4882 static int flat_range_product_entry(void **entry, void *user)
4884 struct isl_union_pw_multi_aff_bin_data *data = user;
4885 isl_pw_multi_aff *pma2 = *entry;
4887 if (!isl_space_tuple_match(data->pma->dim, isl_dim_in,
4888 pma2->dim, isl_dim_in))
4891 pma2 = isl_pw_multi_aff_flat_range_product(
4892 isl_pw_multi_aff_copy(data->pma),
4893 isl_pw_multi_aff_copy(pma2));
4895 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma2);
4900 /* Given two isl_union_pw_multi_affs A -> B and C -> D,
4901 * construct an isl_union_pw_multi_aff (A * C) -> (B, D).
4903 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_flat_range_product(
4904 __isl_take isl_union_pw_multi_aff *upma1,
4905 __isl_take isl_union_pw_multi_aff *upma2)
4907 return bin_op(upma1, upma2, &flat_range_product_entry);
4910 /* Replace the affine expressions at position "pos" in "pma" by "pa".
4911 * The parameters are assumed to have been aligned.
4913 * The implementation essentially performs an isl_pw_*_on_shared_domain,
4914 * except that it works on two different isl_pw_* types.
4916 static __isl_give isl_pw_multi_aff *pw_multi_aff_set_pw_aff(
4917 __isl_take isl_pw_multi_aff *pma, unsigned pos,
4918 __isl_take isl_pw_aff *pa)
4921 isl_pw_multi_aff *res = NULL;
4926 if (!isl_space_tuple_match(pma->dim, isl_dim_in, pa->dim, isl_dim_in))
4927 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
4928 "domains don't match", goto error);
4929 if (pos >= isl_pw_multi_aff_dim(pma, isl_dim_out))
4930 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
4931 "index out of bounds", goto error);
4934 res = isl_pw_multi_aff_alloc_size(isl_pw_multi_aff_get_space(pma), n);
4936 for (i = 0; i < pma->n; ++i) {
4937 for (j = 0; j < pa->n; ++j) {
4939 isl_multi_aff *res_ij;
4942 common = isl_set_intersect(isl_set_copy(pma->p[i].set),
4943 isl_set_copy(pa->p[j].set));
4944 empty = isl_set_plain_is_empty(common);
4945 if (empty < 0 || empty) {
4946 isl_set_free(common);
4952 res_ij = isl_multi_aff_set_aff(
4953 isl_multi_aff_copy(pma->p[i].maff), pos,
4954 isl_aff_copy(pa->p[j].aff));
4955 res_ij = isl_multi_aff_gist(res_ij,
4956 isl_set_copy(common));
4958 res = isl_pw_multi_aff_add_piece(res, common, res_ij);
4962 isl_pw_multi_aff_free(pma);
4963 isl_pw_aff_free(pa);
4966 isl_pw_multi_aff_free(pma);
4967 isl_pw_aff_free(pa);
4968 return isl_pw_multi_aff_free(res);
4971 /* Replace the affine expressions at position "pos" in "pma" by "pa".
4973 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_set_pw_aff(
4974 __isl_take isl_pw_multi_aff *pma, unsigned pos,
4975 __isl_take isl_pw_aff *pa)
4979 if (isl_space_match(pma->dim, isl_dim_param, pa->dim, isl_dim_param))
4980 return pw_multi_aff_set_pw_aff(pma, pos, pa);
4981 if (!isl_space_has_named_params(pma->dim) ||
4982 !isl_space_has_named_params(pa->dim))
4983 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
4984 "unaligned unnamed parameters", goto error);
4985 pma = isl_pw_multi_aff_align_params(pma, isl_pw_aff_get_space(pa));
4986 pa = isl_pw_aff_align_params(pa, isl_pw_multi_aff_get_space(pma));
4987 return pw_multi_aff_set_pw_aff(pma, pos, pa);
4989 isl_pw_multi_aff_free(pma);
4990 isl_pw_aff_free(pa);
4994 /* Check that the domain space of "pa" matches "space".
4996 * Return 0 on success and -1 on error.
4998 int isl_pw_aff_check_match_domain_space(__isl_keep isl_pw_aff *pa,
4999 __isl_keep isl_space *space)
5001 isl_space *pa_space;
5007 pa_space = isl_pw_aff_get_space(pa);
5009 match = isl_space_match(space, isl_dim_param, pa_space, isl_dim_param);
5013 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
5014 "parameters don't match", goto error);
5015 match = isl_space_tuple_match(space, isl_dim_in, pa_space, isl_dim_in);
5019 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
5020 "domains don't match", goto error);
5021 isl_space_free(pa_space);
5024 isl_space_free(pa_space);
5031 #include <isl_multi_templ.c>
5033 /* Scale the first elements of "ma" by the corresponding elements of "vec".
5035 __isl_give isl_multi_aff *isl_multi_aff_scale_vec(__isl_take isl_multi_aff *ma,
5036 __isl_take isl_vec *vec)
5044 n = isl_multi_aff_dim(ma, isl_dim_out);
5045 if (isl_vec_size(vec) < n)
5046 n = isl_vec_size(vec);
5049 for (i = 0; i < n; ++i) {
5052 isl_vec_get_element(vec, i, &v);
5054 aff = isl_multi_aff_get_aff(ma, i);
5055 aff = isl_aff_scale(aff, v);
5056 ma = isl_multi_aff_set_aff(ma, i, aff);
5064 isl_multi_aff_free(ma);
5068 /* Scale the first elements of "pma" by the corresponding elements of "vec".
5070 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_scale_vec(
5071 __isl_take isl_pw_multi_aff *pma, __isl_take isl_vec *v)
5075 pma = isl_pw_multi_aff_cow(pma);
5079 for (i = 0; i < pma->n; ++i) {
5080 pma->p[i].maff = isl_multi_aff_scale_vec(pma->p[i].maff,
5082 if (!pma->p[i].maff)
5090 isl_pw_multi_aff_free(pma);
5094 /* This function is called for each entry of an isl_union_pw_multi_aff.
5095 * Replace the entry by the result of applying isl_pw_multi_aff_scale_vec
5096 * to the original entry with the isl_vec in "user" as extra argument.
5098 static int union_pw_multi_aff_scale_vec_entry(void **entry, void *user)
5100 isl_pw_multi_aff **pma = (isl_pw_multi_aff **) entry;
5103 *pma = isl_pw_multi_aff_scale_vec(*pma, isl_vec_copy(v));
5110 /* Scale the first elements of "upma" by the corresponding elements of "vec".
5112 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_scale_vec(
5113 __isl_take isl_union_pw_multi_aff *upma, __isl_take isl_vec *v)
5115 upma = isl_union_pw_multi_aff_cow(upma);
5119 if (isl_hash_table_foreach(upma->dim->ctx, &upma->table,
5120 &union_pw_multi_aff_scale_vec_entry, v) < 0)
5127 isl_union_pw_multi_aff_free(upma);