2 * Copyright 2011 INRIA Saclay
3 * Copyright 2011 Sven Verdoolaege
4 * Copyright 2012-2014 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_vec_private.h>
22 #include <isl_mat_private.h>
23 #include <isl/constraint.h>
26 #include <isl_val_private.h>
27 #include <isl/deprecated/aff_int.h>
28 #include <isl_config.h>
33 #include <isl_list_templ.c>
38 #include <isl_list_templ.c>
41 #define BASE union_pw_multi_aff
43 #include <isl_list_templ.c>
45 __isl_give isl_aff
*isl_aff_alloc_vec(__isl_take isl_local_space
*ls
,
46 __isl_take isl_vec
*v
)
53 aff
= isl_calloc_type(v
->ctx
, struct isl_aff
);
63 isl_local_space_free(ls
);
68 __isl_give isl_aff
*isl_aff_alloc(__isl_take isl_local_space
*ls
)
77 ctx
= isl_local_space_get_ctx(ls
);
78 if (!isl_local_space_divs_known(ls
))
79 isl_die(ctx
, isl_error_invalid
, "local space has unknown divs",
81 if (!isl_local_space_is_set(ls
))
82 isl_die(ctx
, isl_error_invalid
,
83 "domain of affine expression should be a set",
86 total
= isl_local_space_dim(ls
, isl_dim_all
);
87 v
= isl_vec_alloc(ctx
, 1 + 1 + total
);
88 return isl_aff_alloc_vec(ls
, v
);
90 isl_local_space_free(ls
);
94 __isl_give isl_aff
*isl_aff_zero_on_domain(__isl_take isl_local_space
*ls
)
98 aff
= isl_aff_alloc(ls
);
102 isl_int_set_si(aff
->v
->el
[0], 1);
103 isl_seq_clr(aff
->v
->el
+ 1, aff
->v
->size
- 1);
108 /* Return a piecewise affine expression defined on the specified domain
109 * that is equal to zero.
111 __isl_give isl_pw_aff
*isl_pw_aff_zero_on_domain(__isl_take isl_local_space
*ls
)
113 return isl_pw_aff_from_aff(isl_aff_zero_on_domain(ls
));
116 /* Return an affine expression defined on the specified domain
117 * that represents NaN.
119 __isl_give isl_aff
*isl_aff_nan_on_domain(__isl_take isl_local_space
*ls
)
123 aff
= isl_aff_alloc(ls
);
127 isl_seq_clr(aff
->v
->el
, aff
->v
->size
);
132 /* Return a piecewise affine expression defined on the specified domain
133 * that represents NaN.
135 __isl_give isl_pw_aff
*isl_pw_aff_nan_on_domain(__isl_take isl_local_space
*ls
)
137 return isl_pw_aff_from_aff(isl_aff_nan_on_domain(ls
));
140 /* Return an affine expression that is equal to "val" on
141 * domain local space "ls".
143 __isl_give isl_aff
*isl_aff_val_on_domain(__isl_take isl_local_space
*ls
,
144 __isl_take isl_val
*val
)
150 if (!isl_val_is_rat(val
))
151 isl_die(isl_val_get_ctx(val
), isl_error_invalid
,
152 "expecting rational value", goto error
);
154 aff
= isl_aff_alloc(isl_local_space_copy(ls
));
158 isl_seq_clr(aff
->v
->el
+ 2, aff
->v
->size
- 2);
159 isl_int_set(aff
->v
->el
[1], val
->n
);
160 isl_int_set(aff
->v
->el
[0], val
->d
);
162 isl_local_space_free(ls
);
166 isl_local_space_free(ls
);
171 /* Return an affine expression that is equal to the specified dimension
174 __isl_give isl_aff
*isl_aff_var_on_domain(__isl_take isl_local_space
*ls
,
175 enum isl_dim_type type
, unsigned pos
)
183 space
= isl_local_space_get_space(ls
);
186 if (isl_space_is_map(space
))
187 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
188 "expecting (parameter) set space", goto error
);
189 if (pos
>= isl_local_space_dim(ls
, type
))
190 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
191 "position out of bounds", goto error
);
193 isl_space_free(space
);
194 aff
= isl_aff_alloc(ls
);
198 pos
+= isl_local_space_offset(aff
->ls
, type
);
200 isl_int_set_si(aff
->v
->el
[0], 1);
201 isl_seq_clr(aff
->v
->el
+ 1, aff
->v
->size
- 1);
202 isl_int_set_si(aff
->v
->el
[1 + pos
], 1);
206 isl_local_space_free(ls
);
207 isl_space_free(space
);
211 /* Return a piecewise affine expression that is equal to
212 * the specified dimension in "ls".
214 __isl_give isl_pw_aff
*isl_pw_aff_var_on_domain(__isl_take isl_local_space
*ls
,
215 enum isl_dim_type type
, unsigned pos
)
217 return isl_pw_aff_from_aff(isl_aff_var_on_domain(ls
, type
, pos
));
220 __isl_give isl_aff
*isl_aff_copy(__isl_keep isl_aff
*aff
)
229 __isl_give isl_aff
*isl_aff_dup(__isl_keep isl_aff
*aff
)
234 return isl_aff_alloc_vec(isl_local_space_copy(aff
->ls
),
235 isl_vec_copy(aff
->v
));
238 __isl_give isl_aff
*isl_aff_cow(__isl_take isl_aff
*aff
)
246 return isl_aff_dup(aff
);
249 __isl_null isl_aff
*isl_aff_free(__isl_take isl_aff
*aff
)
257 isl_local_space_free(aff
->ls
);
258 isl_vec_free(aff
->v
);
265 isl_ctx
*isl_aff_get_ctx(__isl_keep isl_aff
*aff
)
267 return aff
? isl_local_space_get_ctx(aff
->ls
) : NULL
;
270 /* Externally, an isl_aff has a map space, but internally, the
271 * ls field corresponds to the domain of that space.
273 int isl_aff_dim(__isl_keep isl_aff
*aff
, enum isl_dim_type type
)
277 if (type
== isl_dim_out
)
279 if (type
== isl_dim_in
)
281 return isl_local_space_dim(aff
->ls
, type
);
284 /* Return the position of the dimension of the given type and name
286 * Return -1 if no such dimension can be found.
288 int isl_aff_find_dim_by_name(__isl_keep isl_aff
*aff
, enum isl_dim_type type
,
293 if (type
== isl_dim_out
)
295 if (type
== isl_dim_in
)
297 return isl_local_space_find_dim_by_name(aff
->ls
, type
, name
);
300 __isl_give isl_space
*isl_aff_get_domain_space(__isl_keep isl_aff
*aff
)
302 return aff
? isl_local_space_get_space(aff
->ls
) : NULL
;
305 __isl_give isl_space
*isl_aff_get_space(__isl_keep isl_aff
*aff
)
310 space
= isl_local_space_get_space(aff
->ls
);
311 space
= isl_space_from_domain(space
);
312 space
= isl_space_add_dims(space
, isl_dim_out
, 1);
316 __isl_give isl_local_space
*isl_aff_get_domain_local_space(
317 __isl_keep isl_aff
*aff
)
319 return aff
? isl_local_space_copy(aff
->ls
) : NULL
;
322 __isl_give isl_local_space
*isl_aff_get_local_space(__isl_keep isl_aff
*aff
)
327 ls
= isl_local_space_copy(aff
->ls
);
328 ls
= isl_local_space_from_domain(ls
);
329 ls
= isl_local_space_add_dims(ls
, isl_dim_out
, 1);
333 /* Externally, an isl_aff has a map space, but internally, the
334 * ls field corresponds to the domain of that space.
336 const char *isl_aff_get_dim_name(__isl_keep isl_aff
*aff
,
337 enum isl_dim_type type
, unsigned pos
)
341 if (type
== isl_dim_out
)
343 if (type
== isl_dim_in
)
345 return isl_local_space_get_dim_name(aff
->ls
, type
, pos
);
348 __isl_give isl_aff
*isl_aff_reset_domain_space(__isl_take isl_aff
*aff
,
349 __isl_take isl_space
*dim
)
351 aff
= isl_aff_cow(aff
);
355 aff
->ls
= isl_local_space_reset_space(aff
->ls
, dim
);
357 return isl_aff_free(aff
);
366 /* Reset the space of "aff". This function is called from isl_pw_templ.c
367 * and doesn't know if the space of an element object is represented
368 * directly or through its domain. It therefore passes along both.
370 __isl_give isl_aff
*isl_aff_reset_space_and_domain(__isl_take isl_aff
*aff
,
371 __isl_take isl_space
*space
, __isl_take isl_space
*domain
)
373 isl_space_free(space
);
374 return isl_aff_reset_domain_space(aff
, domain
);
377 /* Reorder the coefficients of the affine expression based
378 * on the given reodering.
379 * The reordering r is assumed to have been extended with the local
382 static __isl_give isl_vec
*vec_reorder(__isl_take isl_vec
*vec
,
383 __isl_take isl_reordering
*r
, int n_div
)
391 res
= isl_vec_alloc(vec
->ctx
,
392 2 + isl_space_dim(r
->dim
, isl_dim_all
) + n_div
);
393 isl_seq_cpy(res
->el
, vec
->el
, 2);
394 isl_seq_clr(res
->el
+ 2, res
->size
- 2);
395 for (i
= 0; i
< r
->len
; ++i
)
396 isl_int_set(res
->el
[2 + r
->pos
[i
]], vec
->el
[2 + i
]);
398 isl_reordering_free(r
);
403 isl_reordering_free(r
);
407 /* Reorder the dimensions of the domain of "aff" according
408 * to the given reordering.
410 __isl_give isl_aff
*isl_aff_realign_domain(__isl_take isl_aff
*aff
,
411 __isl_take isl_reordering
*r
)
413 aff
= isl_aff_cow(aff
);
417 r
= isl_reordering_extend(r
, aff
->ls
->div
->n_row
);
418 aff
->v
= vec_reorder(aff
->v
, isl_reordering_copy(r
),
419 aff
->ls
->div
->n_row
);
420 aff
->ls
= isl_local_space_realign(aff
->ls
, r
);
422 if (!aff
->v
|| !aff
->ls
)
423 return isl_aff_free(aff
);
428 isl_reordering_free(r
);
432 __isl_give isl_aff
*isl_aff_align_params(__isl_take isl_aff
*aff
,
433 __isl_take isl_space
*model
)
438 if (!isl_space_match(aff
->ls
->dim
, isl_dim_param
,
439 model
, isl_dim_param
)) {
442 model
= isl_space_drop_dims(model
, isl_dim_in
,
443 0, isl_space_dim(model
, isl_dim_in
));
444 model
= isl_space_drop_dims(model
, isl_dim_out
,
445 0, isl_space_dim(model
, isl_dim_out
));
446 exp
= isl_parameter_alignment_reordering(aff
->ls
->dim
, model
);
447 exp
= isl_reordering_extend_space(exp
,
448 isl_aff_get_domain_space(aff
));
449 aff
= isl_aff_realign_domain(aff
, exp
);
452 isl_space_free(model
);
455 isl_space_free(model
);
460 /* Is "aff" obviously equal to zero?
462 * If the denominator is zero, then "aff" is not equal to zero.
464 int isl_aff_plain_is_zero(__isl_keep isl_aff
*aff
)
469 if (isl_int_is_zero(aff
->v
->el
[0]))
471 return isl_seq_first_non_zero(aff
->v
->el
+ 1, aff
->v
->size
- 1) < 0;
474 /* Does "aff" represent NaN?
476 int isl_aff_is_nan(__isl_keep isl_aff
*aff
)
481 return isl_seq_first_non_zero(aff
->v
->el
, 2) < 0;
484 /* Does "pa" involve any NaNs?
486 int isl_pw_aff_involves_nan(__isl_keep isl_pw_aff
*pa
)
495 for (i
= 0; i
< pa
->n
; ++i
) {
496 int is_nan
= isl_aff_is_nan(pa
->p
[i
].aff
);
497 if (is_nan
< 0 || is_nan
)
504 /* Are "aff1" and "aff2" obviously equal?
506 * NaN is not equal to anything, not even to another NaN.
508 int isl_aff_plain_is_equal(__isl_keep isl_aff
*aff1
, __isl_keep isl_aff
*aff2
)
515 if (isl_aff_is_nan(aff1
) || isl_aff_is_nan(aff2
))
518 equal
= isl_local_space_is_equal(aff1
->ls
, aff2
->ls
);
519 if (equal
< 0 || !equal
)
522 return isl_vec_is_equal(aff1
->v
, aff2
->v
);
525 /* Return the common denominator of "aff" in "v".
527 * We cannot return anything meaningful in case of a NaN.
529 int isl_aff_get_denominator(__isl_keep isl_aff
*aff
, isl_int
*v
)
533 if (isl_aff_is_nan(aff
))
534 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
535 "cannot get denominator of NaN", return -1);
536 isl_int_set(*v
, aff
->v
->el
[0]);
540 /* Return the common denominator of "aff".
542 __isl_give isl_val
*isl_aff_get_denominator_val(__isl_keep isl_aff
*aff
)
549 ctx
= isl_aff_get_ctx(aff
);
550 if (isl_aff_is_nan(aff
))
551 return isl_val_nan(ctx
);
552 return isl_val_int_from_isl_int(ctx
, aff
->v
->el
[0]);
555 /* Return the constant term of "aff" in "v".
557 * We cannot return anything meaningful in case of a NaN.
559 int isl_aff_get_constant(__isl_keep isl_aff
*aff
, isl_int
*v
)
563 if (isl_aff_is_nan(aff
))
564 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
565 "cannot get constant term of NaN", return -1);
566 isl_int_set(*v
, aff
->v
->el
[1]);
570 /* Return the constant term of "aff".
572 __isl_give isl_val
*isl_aff_get_constant_val(__isl_keep isl_aff
*aff
)
580 ctx
= isl_aff_get_ctx(aff
);
581 if (isl_aff_is_nan(aff
))
582 return isl_val_nan(ctx
);
583 v
= isl_val_rat_from_isl_int(ctx
, aff
->v
->el
[1], aff
->v
->el
[0]);
584 return isl_val_normalize(v
);
587 /* Return the coefficient of the variable of type "type" at position "pos"
590 * We cannot return anything meaningful in case of a NaN.
592 int isl_aff_get_coefficient(__isl_keep isl_aff
*aff
,
593 enum isl_dim_type type
, int pos
, isl_int
*v
)
598 if (type
== isl_dim_out
)
599 isl_die(aff
->v
->ctx
, isl_error_invalid
,
600 "output/set dimension does not have a coefficient",
602 if (type
== isl_dim_in
)
605 if (pos
>= isl_local_space_dim(aff
->ls
, type
))
606 isl_die(aff
->v
->ctx
, isl_error_invalid
,
607 "position out of bounds", return -1);
609 if (isl_aff_is_nan(aff
))
610 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
611 "cannot get coefficient of NaN", return -1);
612 pos
+= isl_local_space_offset(aff
->ls
, type
);
613 isl_int_set(*v
, aff
->v
->el
[1 + pos
]);
618 /* Return the coefficient of the variable of type "type" at position "pos"
621 __isl_give isl_val
*isl_aff_get_coefficient_val(__isl_keep isl_aff
*aff
,
622 enum isl_dim_type type
, int pos
)
630 ctx
= isl_aff_get_ctx(aff
);
631 if (type
== isl_dim_out
)
632 isl_die(ctx
, isl_error_invalid
,
633 "output/set dimension does not have a coefficient",
635 if (type
== isl_dim_in
)
638 if (pos
>= isl_local_space_dim(aff
->ls
, type
))
639 isl_die(ctx
, isl_error_invalid
,
640 "position out of bounds", return NULL
);
642 if (isl_aff_is_nan(aff
))
643 return isl_val_nan(ctx
);
644 pos
+= isl_local_space_offset(aff
->ls
, type
);
645 v
= isl_val_rat_from_isl_int(ctx
, aff
->v
->el
[1 + pos
], aff
->v
->el
[0]);
646 return isl_val_normalize(v
);
649 /* Return the sign of the coefficient of the variable of type "type"
650 * at position "pos" of "aff".
652 int isl_aff_coefficient_sgn(__isl_keep isl_aff
*aff
, enum isl_dim_type type
,
660 ctx
= isl_aff_get_ctx(aff
);
661 if (type
== isl_dim_out
)
662 isl_die(ctx
, isl_error_invalid
,
663 "output/set dimension does not have a coefficient",
665 if (type
== isl_dim_in
)
668 if (pos
>= isl_local_space_dim(aff
->ls
, type
))
669 isl_die(ctx
, isl_error_invalid
,
670 "position out of bounds", return 0);
672 pos
+= isl_local_space_offset(aff
->ls
, type
);
673 return isl_int_sgn(aff
->v
->el
[1 + pos
]);
676 /* Replace the denominator of "aff" by "v".
678 * A NaN is unaffected by this operation.
680 __isl_give isl_aff
*isl_aff_set_denominator(__isl_take isl_aff
*aff
, isl_int v
)
684 if (isl_aff_is_nan(aff
))
686 aff
= isl_aff_cow(aff
);
690 aff
->v
= isl_vec_cow(aff
->v
);
692 return isl_aff_free(aff
);
694 isl_int_set(aff
->v
->el
[0], v
);
699 /* Replace the numerator of the constant term of "aff" by "v".
701 * A NaN is unaffected by this operation.
703 __isl_give isl_aff
*isl_aff_set_constant(__isl_take isl_aff
*aff
, isl_int v
)
707 if (isl_aff_is_nan(aff
))
709 aff
= isl_aff_cow(aff
);
713 aff
->v
= isl_vec_cow(aff
->v
);
715 return isl_aff_free(aff
);
717 isl_int_set(aff
->v
->el
[1], v
);
722 /* Replace the constant term of "aff" by "v".
724 * A NaN is unaffected by this operation.
726 __isl_give isl_aff
*isl_aff_set_constant_val(__isl_take isl_aff
*aff
,
727 __isl_take isl_val
*v
)
732 if (isl_aff_is_nan(aff
)) {
737 if (!isl_val_is_rat(v
))
738 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
739 "expecting rational value", goto error
);
741 if (isl_int_eq(aff
->v
->el
[1], v
->n
) &&
742 isl_int_eq(aff
->v
->el
[0], v
->d
)) {
747 aff
= isl_aff_cow(aff
);
750 aff
->v
= isl_vec_cow(aff
->v
);
754 if (isl_int_eq(aff
->v
->el
[0], v
->d
)) {
755 isl_int_set(aff
->v
->el
[1], v
->n
);
756 } else if (isl_int_is_one(v
->d
)) {
757 isl_int_mul(aff
->v
->el
[1], aff
->v
->el
[0], v
->n
);
759 isl_seq_scale(aff
->v
->el
+ 1,
760 aff
->v
->el
+ 1, v
->d
, aff
->v
->size
- 1);
761 isl_int_mul(aff
->v
->el
[1], aff
->v
->el
[0], v
->n
);
762 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], v
->d
);
763 aff
->v
= isl_vec_normalize(aff
->v
);
776 /* Add "v" to the constant term of "aff".
778 * A NaN is unaffected by this operation.
780 __isl_give isl_aff
*isl_aff_add_constant(__isl_take isl_aff
*aff
, isl_int v
)
782 if (isl_int_is_zero(v
))
787 if (isl_aff_is_nan(aff
))
789 aff
= isl_aff_cow(aff
);
793 aff
->v
= isl_vec_cow(aff
->v
);
795 return isl_aff_free(aff
);
797 isl_int_addmul(aff
->v
->el
[1], aff
->v
->el
[0], v
);
802 /* Add "v" to the constant term of "aff".
804 * A NaN is unaffected by this operation.
806 __isl_give isl_aff
*isl_aff_add_constant_val(__isl_take isl_aff
*aff
,
807 __isl_take isl_val
*v
)
812 if (isl_aff_is_nan(aff
) || isl_val_is_zero(v
)) {
817 if (!isl_val_is_rat(v
))
818 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
819 "expecting rational value", goto error
);
821 aff
= isl_aff_cow(aff
);
825 aff
->v
= isl_vec_cow(aff
->v
);
829 if (isl_int_is_one(v
->d
)) {
830 isl_int_addmul(aff
->v
->el
[1], aff
->v
->el
[0], v
->n
);
831 } else if (isl_int_eq(aff
->v
->el
[0], v
->d
)) {
832 isl_int_add(aff
->v
->el
[1], aff
->v
->el
[1], v
->n
);
833 aff
->v
= isl_vec_normalize(aff
->v
);
837 isl_seq_scale(aff
->v
->el
+ 1,
838 aff
->v
->el
+ 1, v
->d
, aff
->v
->size
- 1);
839 isl_int_addmul(aff
->v
->el
[1], aff
->v
->el
[0], v
->n
);
840 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], v
->d
);
841 aff
->v
= isl_vec_normalize(aff
->v
);
854 __isl_give isl_aff
*isl_aff_add_constant_si(__isl_take isl_aff
*aff
, int v
)
859 isl_int_set_si(t
, v
);
860 aff
= isl_aff_add_constant(aff
, t
);
866 /* Add "v" to the numerator of the constant term of "aff".
868 * A NaN is unaffected by this operation.
870 __isl_give isl_aff
*isl_aff_add_constant_num(__isl_take isl_aff
*aff
, isl_int v
)
872 if (isl_int_is_zero(v
))
877 if (isl_aff_is_nan(aff
))
879 aff
= isl_aff_cow(aff
);
883 aff
->v
= isl_vec_cow(aff
->v
);
885 return isl_aff_free(aff
);
887 isl_int_add(aff
->v
->el
[1], aff
->v
->el
[1], v
);
892 /* Add "v" to the numerator of the constant term of "aff".
894 * A NaN is unaffected by this operation.
896 __isl_give isl_aff
*isl_aff_add_constant_num_si(__isl_take isl_aff
*aff
, int v
)
904 isl_int_set_si(t
, v
);
905 aff
= isl_aff_add_constant_num(aff
, t
);
911 /* Replace the numerator of the constant term of "aff" by "v".
913 * A NaN is unaffected by this operation.
915 __isl_give isl_aff
*isl_aff_set_constant_si(__isl_take isl_aff
*aff
, int v
)
919 if (isl_aff_is_nan(aff
))
921 aff
= isl_aff_cow(aff
);
925 aff
->v
= isl_vec_cow(aff
->v
);
927 return isl_aff_free(aff
);
929 isl_int_set_si(aff
->v
->el
[1], v
);
934 /* Replace the numerator of the coefficient of the variable of type "type"
935 * at position "pos" of "aff" by "v".
937 * A NaN is unaffected by this operation.
939 __isl_give isl_aff
*isl_aff_set_coefficient(__isl_take isl_aff
*aff
,
940 enum isl_dim_type type
, int pos
, isl_int v
)
945 if (type
== isl_dim_out
)
946 isl_die(aff
->v
->ctx
, isl_error_invalid
,
947 "output/set dimension does not have a coefficient",
948 return isl_aff_free(aff
));
949 if (type
== isl_dim_in
)
952 if (pos
>= isl_local_space_dim(aff
->ls
, type
))
953 isl_die(aff
->v
->ctx
, isl_error_invalid
,
954 "position out of bounds", return isl_aff_free(aff
));
956 if (isl_aff_is_nan(aff
))
958 aff
= isl_aff_cow(aff
);
962 aff
->v
= isl_vec_cow(aff
->v
);
964 return isl_aff_free(aff
);
966 pos
+= isl_local_space_offset(aff
->ls
, type
);
967 isl_int_set(aff
->v
->el
[1 + pos
], v
);
972 /* Replace the numerator of the coefficient of the variable of type "type"
973 * at position "pos" of "aff" by "v".
975 * A NaN is unaffected by this operation.
977 __isl_give isl_aff
*isl_aff_set_coefficient_si(__isl_take isl_aff
*aff
,
978 enum isl_dim_type type
, int pos
, int v
)
983 if (type
== isl_dim_out
)
984 isl_die(aff
->v
->ctx
, isl_error_invalid
,
985 "output/set dimension does not have a coefficient",
986 return isl_aff_free(aff
));
987 if (type
== isl_dim_in
)
990 if (pos
< 0 || pos
>= isl_local_space_dim(aff
->ls
, type
))
991 isl_die(aff
->v
->ctx
, isl_error_invalid
,
992 "position out of bounds", return isl_aff_free(aff
));
994 if (isl_aff_is_nan(aff
))
996 pos
+= isl_local_space_offset(aff
->ls
, type
);
997 if (isl_int_cmp_si(aff
->v
->el
[1 + pos
], v
) == 0)
1000 aff
= isl_aff_cow(aff
);
1004 aff
->v
= isl_vec_cow(aff
->v
);
1006 return isl_aff_free(aff
);
1008 isl_int_set_si(aff
->v
->el
[1 + pos
], v
);
1013 /* Replace the coefficient of the variable of type "type" at position "pos"
1016 * A NaN is unaffected by this operation.
1018 __isl_give isl_aff
*isl_aff_set_coefficient_val(__isl_take isl_aff
*aff
,
1019 enum isl_dim_type type
, int pos
, __isl_take isl_val
*v
)
1024 if (type
== isl_dim_out
)
1025 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1026 "output/set dimension does not have a coefficient",
1028 if (type
== isl_dim_in
)
1031 if (pos
>= isl_local_space_dim(aff
->ls
, type
))
1032 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1033 "position out of bounds", goto error
);
1035 if (isl_aff_is_nan(aff
)) {
1039 if (!isl_val_is_rat(v
))
1040 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1041 "expecting rational value", goto error
);
1043 pos
+= isl_local_space_offset(aff
->ls
, type
);
1044 if (isl_int_eq(aff
->v
->el
[1 + pos
], v
->n
) &&
1045 isl_int_eq(aff
->v
->el
[0], v
->d
)) {
1050 aff
= isl_aff_cow(aff
);
1053 aff
->v
= isl_vec_cow(aff
->v
);
1057 if (isl_int_eq(aff
->v
->el
[0], v
->d
)) {
1058 isl_int_set(aff
->v
->el
[1 + pos
], v
->n
);
1059 } else if (isl_int_is_one(v
->d
)) {
1060 isl_int_mul(aff
->v
->el
[1 + pos
], aff
->v
->el
[0], v
->n
);
1062 isl_seq_scale(aff
->v
->el
+ 1,
1063 aff
->v
->el
+ 1, v
->d
, aff
->v
->size
- 1);
1064 isl_int_mul(aff
->v
->el
[1 + pos
], aff
->v
->el
[0], v
->n
);
1065 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], v
->d
);
1066 aff
->v
= isl_vec_normalize(aff
->v
);
1079 /* Add "v" to the coefficient of the variable of type "type"
1080 * at position "pos" of "aff".
1082 * A NaN is unaffected by this operation.
1084 __isl_give isl_aff
*isl_aff_add_coefficient(__isl_take isl_aff
*aff
,
1085 enum isl_dim_type type
, int pos
, isl_int v
)
1090 if (type
== isl_dim_out
)
1091 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1092 "output/set dimension does not have a coefficient",
1093 return isl_aff_free(aff
));
1094 if (type
== isl_dim_in
)
1097 if (pos
>= isl_local_space_dim(aff
->ls
, type
))
1098 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1099 "position out of bounds", return isl_aff_free(aff
));
1101 if (isl_aff_is_nan(aff
))
1103 aff
= isl_aff_cow(aff
);
1107 aff
->v
= isl_vec_cow(aff
->v
);
1109 return isl_aff_free(aff
);
1111 pos
+= isl_local_space_offset(aff
->ls
, type
);
1112 isl_int_addmul(aff
->v
->el
[1 + pos
], aff
->v
->el
[0], v
);
1117 /* Add "v" to the coefficient of the variable of type "type"
1118 * at position "pos" of "aff".
1120 * A NaN is unaffected by this operation.
1122 __isl_give isl_aff
*isl_aff_add_coefficient_val(__isl_take isl_aff
*aff
,
1123 enum isl_dim_type type
, int pos
, __isl_take isl_val
*v
)
1128 if (isl_val_is_zero(v
)) {
1133 if (type
== isl_dim_out
)
1134 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1135 "output/set dimension does not have a coefficient",
1137 if (type
== isl_dim_in
)
1140 if (pos
>= isl_local_space_dim(aff
->ls
, type
))
1141 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1142 "position out of bounds", goto error
);
1144 if (isl_aff_is_nan(aff
)) {
1148 if (!isl_val_is_rat(v
))
1149 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1150 "expecting rational value", goto error
);
1152 aff
= isl_aff_cow(aff
);
1156 aff
->v
= isl_vec_cow(aff
->v
);
1160 pos
+= isl_local_space_offset(aff
->ls
, type
);
1161 if (isl_int_is_one(v
->d
)) {
1162 isl_int_addmul(aff
->v
->el
[1 + pos
], aff
->v
->el
[0], v
->n
);
1163 } else if (isl_int_eq(aff
->v
->el
[0], v
->d
)) {
1164 isl_int_add(aff
->v
->el
[1 + pos
], aff
->v
->el
[1 + pos
], v
->n
);
1165 aff
->v
= isl_vec_normalize(aff
->v
);
1169 isl_seq_scale(aff
->v
->el
+ 1,
1170 aff
->v
->el
+ 1, v
->d
, aff
->v
->size
- 1);
1171 isl_int_addmul(aff
->v
->el
[1 + pos
], aff
->v
->el
[0], v
->n
);
1172 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], v
->d
);
1173 aff
->v
= isl_vec_normalize(aff
->v
);
1186 __isl_give isl_aff
*isl_aff_add_coefficient_si(__isl_take isl_aff
*aff
,
1187 enum isl_dim_type type
, int pos
, int v
)
1192 isl_int_set_si(t
, v
);
1193 aff
= isl_aff_add_coefficient(aff
, type
, pos
, t
);
1199 __isl_give isl_aff
*isl_aff_get_div(__isl_keep isl_aff
*aff
, int pos
)
1204 return isl_local_space_get_div(aff
->ls
, pos
);
1207 /* Return the negation of "aff".
1209 * As a special case, -NaN = NaN.
1211 __isl_give isl_aff
*isl_aff_neg(__isl_take isl_aff
*aff
)
1215 if (isl_aff_is_nan(aff
))
1217 aff
= isl_aff_cow(aff
);
1220 aff
->v
= isl_vec_cow(aff
->v
);
1222 return isl_aff_free(aff
);
1224 isl_seq_neg(aff
->v
->el
+ 1, aff
->v
->el
+ 1, aff
->v
->size
- 1);
1229 /* Remove divs from the local space that do not appear in the affine
1231 * We currently only remove divs at the end.
1232 * Some intermediate divs may also not appear directly in the affine
1233 * expression, but we would also need to check that no other divs are
1234 * defined in terms of them.
1236 __isl_give isl_aff
*isl_aff_remove_unused_divs( __isl_take isl_aff
*aff
)
1245 n
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
1246 off
= isl_local_space_offset(aff
->ls
, isl_dim_div
);
1248 pos
= isl_seq_last_non_zero(aff
->v
->el
+ 1 + off
, n
) + 1;
1252 aff
= isl_aff_cow(aff
);
1256 aff
->ls
= isl_local_space_drop_dims(aff
->ls
, isl_dim_div
, pos
, n
- pos
);
1257 aff
->v
= isl_vec_drop_els(aff
->v
, 1 + off
+ pos
, n
- pos
);
1258 if (!aff
->ls
|| !aff
->v
)
1259 return isl_aff_free(aff
);
1264 /* Given two affine expressions "p" of length p_len (including the
1265 * denominator and the constant term) and "subs" of length subs_len,
1266 * plug in "subs" for the variable at position "pos".
1267 * The variables of "subs" and "p" are assumed to match up to subs_len,
1268 * but "p" may have additional variables.
1269 * "v" is an initialized isl_int that can be used internally.
1271 * In particular, if "p" represents the expression
1275 * with i the variable at position "pos" and "subs" represents the expression
1279 * then the result represents the expression
1284 void isl_seq_substitute(isl_int
*p
, int pos
, isl_int
*subs
,
1285 int p_len
, int subs_len
, isl_int v
)
1287 isl_int_set(v
, p
[1 + pos
]);
1288 isl_int_set_si(p
[1 + pos
], 0);
1289 isl_seq_combine(p
+ 1, subs
[0], p
+ 1, v
, subs
+ 1, subs_len
- 1);
1290 isl_seq_scale(p
+ subs_len
, p
+ subs_len
, subs
[0], p_len
- subs_len
);
1291 isl_int_mul(p
[0], p
[0], subs
[0]);
1294 /* Look for any divs in the aff->ls with a denominator equal to one
1295 * and plug them into the affine expression and any subsequent divs
1296 * that may reference the div.
1298 static __isl_give isl_aff
*plug_in_integral_divs(__isl_take isl_aff
*aff
)
1304 isl_local_space
*ls
;
1310 n
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
1312 for (i
= 0; i
< n
; ++i
) {
1313 if (!isl_int_is_one(aff
->ls
->div
->row
[i
][0]))
1315 ls
= isl_local_space_copy(aff
->ls
);
1316 ls
= isl_local_space_substitute_seq(ls
, isl_dim_div
, i
,
1317 aff
->ls
->div
->row
[i
], len
, i
+ 1, n
- (i
+ 1));
1318 vec
= isl_vec_copy(aff
->v
);
1319 vec
= isl_vec_cow(vec
);
1325 pos
= isl_local_space_offset(aff
->ls
, isl_dim_div
) + i
;
1326 isl_seq_substitute(vec
->el
, pos
, aff
->ls
->div
->row
[i
],
1331 isl_vec_free(aff
->v
);
1333 isl_local_space_free(aff
->ls
);
1340 isl_local_space_free(ls
);
1341 return isl_aff_free(aff
);
1344 /* Look for any divs j that appear with a unit coefficient inside
1345 * the definitions of other divs i and plug them into the definitions
1348 * In particular, an expression of the form
1350 * floor((f(..) + floor(g(..)/n))/m)
1354 * floor((n * f(..) + g(..))/(n * m))
1356 * This simplification is correct because we can move the expression
1357 * f(..) into the inner floor in the original expression to obtain
1359 * floor(floor((n * f(..) + g(..))/n)/m)
1361 * from which we can derive the simplified expression.
1363 static __isl_give isl_aff
*plug_in_unit_divs(__isl_take isl_aff
*aff
)
1371 n
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
1372 off
= isl_local_space_offset(aff
->ls
, isl_dim_div
);
1373 for (i
= 1; i
< n
; ++i
) {
1374 for (j
= 0; j
< i
; ++j
) {
1375 if (!isl_int_is_one(aff
->ls
->div
->row
[i
][1 + off
+ j
]))
1377 aff
->ls
= isl_local_space_substitute_seq(aff
->ls
,
1378 isl_dim_div
, j
, aff
->ls
->div
->row
[j
],
1379 aff
->v
->size
, i
, 1);
1381 return isl_aff_free(aff
);
1388 /* Swap divs "a" and "b" in "aff", which is assumed to be non-NULL.
1390 * Even though this function is only called on isl_affs with a single
1391 * reference, we are careful to only change aff->v and aff->ls together.
1393 static __isl_give isl_aff
*swap_div(__isl_take isl_aff
*aff
, int a
, int b
)
1395 unsigned off
= isl_local_space_offset(aff
->ls
, isl_dim_div
);
1396 isl_local_space
*ls
;
1399 ls
= isl_local_space_copy(aff
->ls
);
1400 ls
= isl_local_space_swap_div(ls
, a
, b
);
1401 v
= isl_vec_copy(aff
->v
);
1406 isl_int_swap(v
->el
[1 + off
+ a
], v
->el
[1 + off
+ b
]);
1407 isl_vec_free(aff
->v
);
1409 isl_local_space_free(aff
->ls
);
1415 isl_local_space_free(ls
);
1416 return isl_aff_free(aff
);
1419 /* Merge divs "a" and "b" in "aff", which is assumed to be non-NULL.
1421 * We currently do not actually remove div "b", but simply add its
1422 * coefficient to that of "a" and then zero it out.
1424 static __isl_give isl_aff
*merge_divs(__isl_take isl_aff
*aff
, int a
, int b
)
1426 unsigned off
= isl_local_space_offset(aff
->ls
, isl_dim_div
);
1428 if (isl_int_is_zero(aff
->v
->el
[1 + off
+ b
]))
1431 aff
->v
= isl_vec_cow(aff
->v
);
1433 return isl_aff_free(aff
);
1435 isl_int_add(aff
->v
->el
[1 + off
+ a
],
1436 aff
->v
->el
[1 + off
+ a
], aff
->v
->el
[1 + off
+ b
]);
1437 isl_int_set_si(aff
->v
->el
[1 + off
+ b
], 0);
1442 /* Sort the divs in the local space of "aff" according to
1443 * the comparison function "cmp_row" in isl_local_space.c,
1444 * combining the coefficients of identical divs.
1446 * Reordering divs does not change the semantics of "aff",
1447 * so there is no need to call isl_aff_cow.
1448 * Moreover, this function is currently only called on isl_affs
1449 * with a single reference.
1451 static __isl_give isl_aff
*sort_divs(__isl_take isl_aff
*aff
)
1459 off
= isl_local_space_offset(aff
->ls
, isl_dim_div
);
1460 n
= isl_aff_dim(aff
, isl_dim_div
);
1461 for (i
= 1; i
< n
; ++i
) {
1462 for (j
= i
- 1; j
>= 0; --j
) {
1463 int cmp
= isl_mat_cmp_div(aff
->ls
->div
, j
, j
+ 1);
1467 aff
= merge_divs(aff
, j
, j
+ 1);
1469 aff
= swap_div(aff
, j
, j
+ 1);
1478 /* Normalize the representation of "aff".
1480 * This function should only be called of "new" isl_affs, i.e.,
1481 * with only a single reference. We therefore do not need to
1482 * worry about affecting other instances.
1484 __isl_give isl_aff
*isl_aff_normalize(__isl_take isl_aff
*aff
)
1488 aff
->v
= isl_vec_normalize(aff
->v
);
1490 return isl_aff_free(aff
);
1491 aff
= plug_in_integral_divs(aff
);
1492 aff
= plug_in_unit_divs(aff
);
1493 aff
= sort_divs(aff
);
1494 aff
= isl_aff_remove_unused_divs(aff
);
1498 /* Given f, return floor(f).
1499 * If f is an integer expression, then just return f.
1500 * If f is a constant, then return the constant floor(f).
1501 * Otherwise, if f = g/m, write g = q m + r,
1502 * create a new div d = [r/m] and return the expression q + d.
1503 * The coefficients in r are taken to lie between -m/2 and m/2.
1505 * As a special case, floor(NaN) = NaN.
1507 __isl_give isl_aff
*isl_aff_floor(__isl_take isl_aff
*aff
)
1517 if (isl_aff_is_nan(aff
))
1519 if (isl_int_is_one(aff
->v
->el
[0]))
1522 aff
= isl_aff_cow(aff
);
1526 aff
->v
= isl_vec_cow(aff
->v
);
1528 return isl_aff_free(aff
);
1530 if (isl_aff_is_cst(aff
)) {
1531 isl_int_fdiv_q(aff
->v
->el
[1], aff
->v
->el
[1], aff
->v
->el
[0]);
1532 isl_int_set_si(aff
->v
->el
[0], 1);
1536 div
= isl_vec_copy(aff
->v
);
1537 div
= isl_vec_cow(div
);
1539 return isl_aff_free(aff
);
1541 ctx
= isl_aff_get_ctx(aff
);
1542 isl_int_fdiv_q(aff
->v
->el
[0], aff
->v
->el
[0], ctx
->two
);
1543 for (i
= 1; i
< aff
->v
->size
; ++i
) {
1544 isl_int_fdiv_r(div
->el
[i
], div
->el
[i
], div
->el
[0]);
1545 isl_int_fdiv_q(aff
->v
->el
[i
], aff
->v
->el
[i
], div
->el
[0]);
1546 if (isl_int_gt(div
->el
[i
], aff
->v
->el
[0])) {
1547 isl_int_sub(div
->el
[i
], div
->el
[i
], div
->el
[0]);
1548 isl_int_add_ui(aff
->v
->el
[i
], aff
->v
->el
[i
], 1);
1552 aff
->ls
= isl_local_space_add_div(aff
->ls
, div
);
1554 return isl_aff_free(aff
);
1556 size
= aff
->v
->size
;
1557 aff
->v
= isl_vec_extend(aff
->v
, size
+ 1);
1559 return isl_aff_free(aff
);
1560 isl_int_set_si(aff
->v
->el
[0], 1);
1561 isl_int_set_si(aff
->v
->el
[size
], 1);
1563 aff
= isl_aff_normalize(aff
);
1570 * aff mod m = aff - m * floor(aff/m)
1572 __isl_give isl_aff
*isl_aff_mod(__isl_take isl_aff
*aff
, isl_int m
)
1576 res
= isl_aff_copy(aff
);
1577 aff
= isl_aff_scale_down(aff
, m
);
1578 aff
= isl_aff_floor(aff
);
1579 aff
= isl_aff_scale(aff
, m
);
1580 res
= isl_aff_sub(res
, aff
);
1587 * aff mod m = aff - m * floor(aff/m)
1589 * with m an integer value.
1591 __isl_give isl_aff
*isl_aff_mod_val(__isl_take isl_aff
*aff
,
1592 __isl_take isl_val
*m
)
1599 if (!isl_val_is_int(m
))
1600 isl_die(isl_val_get_ctx(m
), isl_error_invalid
,
1601 "expecting integer modulo", goto error
);
1603 res
= isl_aff_copy(aff
);
1604 aff
= isl_aff_scale_down_val(aff
, isl_val_copy(m
));
1605 aff
= isl_aff_floor(aff
);
1606 aff
= isl_aff_scale_val(aff
, m
);
1607 res
= isl_aff_sub(res
, aff
);
1618 * pwaff mod m = pwaff - m * floor(pwaff/m)
1620 __isl_give isl_pw_aff
*isl_pw_aff_mod(__isl_take isl_pw_aff
*pwaff
, isl_int m
)
1624 res
= isl_pw_aff_copy(pwaff
);
1625 pwaff
= isl_pw_aff_scale_down(pwaff
, m
);
1626 pwaff
= isl_pw_aff_floor(pwaff
);
1627 pwaff
= isl_pw_aff_scale(pwaff
, m
);
1628 res
= isl_pw_aff_sub(res
, pwaff
);
1635 * pa mod m = pa - m * floor(pa/m)
1637 * with m an integer value.
1639 __isl_give isl_pw_aff
*isl_pw_aff_mod_val(__isl_take isl_pw_aff
*pa
,
1640 __isl_take isl_val
*m
)
1644 if (!isl_val_is_int(m
))
1645 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
1646 "expecting integer modulo", goto error
);
1647 pa
= isl_pw_aff_mod(pa
, m
->n
);
1651 isl_pw_aff_free(pa
);
1656 /* Given f, return ceil(f).
1657 * If f is an integer expression, then just return f.
1658 * Otherwise, let f be the expression
1664 * floor((e + m - 1)/m)
1666 * As a special case, ceil(NaN) = NaN.
1668 __isl_give isl_aff
*isl_aff_ceil(__isl_take isl_aff
*aff
)
1673 if (isl_aff_is_nan(aff
))
1675 if (isl_int_is_one(aff
->v
->el
[0]))
1678 aff
= isl_aff_cow(aff
);
1681 aff
->v
= isl_vec_cow(aff
->v
);
1683 return isl_aff_free(aff
);
1685 isl_int_add(aff
->v
->el
[1], aff
->v
->el
[1], aff
->v
->el
[0]);
1686 isl_int_sub_ui(aff
->v
->el
[1], aff
->v
->el
[1], 1);
1687 aff
= isl_aff_floor(aff
);
1692 /* Apply the expansion computed by isl_merge_divs.
1693 * The expansion itself is given by "exp" while the resulting
1694 * list of divs is given by "div".
1696 __isl_give isl_aff
*isl_aff_expand_divs( __isl_take isl_aff
*aff
,
1697 __isl_take isl_mat
*div
, int *exp
)
1704 aff
= isl_aff_cow(aff
);
1708 old_n_div
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
1709 new_n_div
= isl_mat_rows(div
);
1710 if (new_n_div
< old_n_div
)
1711 isl_die(isl_mat_get_ctx(div
), isl_error_invalid
,
1712 "not an expansion", goto error
);
1714 aff
->v
= isl_vec_extend(aff
->v
, aff
->v
->size
+ new_n_div
- old_n_div
);
1718 offset
= 1 + isl_local_space_offset(aff
->ls
, isl_dim_div
);
1720 for (i
= new_n_div
- 1; i
>= 0; --i
) {
1721 if (j
>= 0 && exp
[j
] == i
) {
1723 isl_int_swap(aff
->v
->el
[offset
+ i
],
1724 aff
->v
->el
[offset
+ j
]);
1727 isl_int_set_si(aff
->v
->el
[offset
+ i
], 0);
1730 aff
->ls
= isl_local_space_replace_divs(aff
->ls
, isl_mat_copy(div
));
1741 /* Add two affine expressions that live in the same local space.
1743 static __isl_give isl_aff
*add_expanded(__isl_take isl_aff
*aff1
,
1744 __isl_take isl_aff
*aff2
)
1748 aff1
= isl_aff_cow(aff1
);
1752 aff1
->v
= isl_vec_cow(aff1
->v
);
1758 isl_int_gcd(gcd
, aff1
->v
->el
[0], aff2
->v
->el
[0]);
1759 isl_int_divexact(f
, aff2
->v
->el
[0], gcd
);
1760 isl_seq_scale(aff1
->v
->el
+ 1, aff1
->v
->el
+ 1, f
, aff1
->v
->size
- 1);
1761 isl_int_divexact(f
, aff1
->v
->el
[0], gcd
);
1762 isl_seq_addmul(aff1
->v
->el
+ 1, f
, aff2
->v
->el
+ 1, aff1
->v
->size
- 1);
1763 isl_int_divexact(f
, aff2
->v
->el
[0], gcd
);
1764 isl_int_mul(aff1
->v
->el
[0], aff1
->v
->el
[0], f
);
1776 /* Return the sum of "aff1" and "aff2".
1778 * If either of the two is NaN, then the result is NaN.
1780 __isl_give isl_aff
*isl_aff_add(__isl_take isl_aff
*aff1
,
1781 __isl_take isl_aff
*aff2
)
1792 ctx
= isl_aff_get_ctx(aff1
);
1793 if (!isl_space_is_equal(aff1
->ls
->dim
, aff2
->ls
->dim
))
1794 isl_die(ctx
, isl_error_invalid
,
1795 "spaces don't match", goto error
);
1797 if (isl_aff_is_nan(aff1
)) {
1801 if (isl_aff_is_nan(aff2
)) {
1806 n_div1
= isl_aff_dim(aff1
, isl_dim_div
);
1807 n_div2
= isl_aff_dim(aff2
, isl_dim_div
);
1808 if (n_div1
== 0 && n_div2
== 0)
1809 return add_expanded(aff1
, aff2
);
1811 exp1
= isl_alloc_array(ctx
, int, n_div1
);
1812 exp2
= isl_alloc_array(ctx
, int, n_div2
);
1813 if ((n_div1
&& !exp1
) || (n_div2
&& !exp2
))
1816 div
= isl_merge_divs(aff1
->ls
->div
, aff2
->ls
->div
, exp1
, exp2
);
1817 aff1
= isl_aff_expand_divs(aff1
, isl_mat_copy(div
), exp1
);
1818 aff2
= isl_aff_expand_divs(aff2
, div
, exp2
);
1822 return add_expanded(aff1
, aff2
);
1831 __isl_give isl_aff
*isl_aff_sub(__isl_take isl_aff
*aff1
,
1832 __isl_take isl_aff
*aff2
)
1834 return isl_aff_add(aff1
, isl_aff_neg(aff2
));
1837 /* Return the result of scaling "aff" by a factor of "f".
1839 * As a special case, f * NaN = NaN.
1841 __isl_give isl_aff
*isl_aff_scale(__isl_take isl_aff
*aff
, isl_int f
)
1847 if (isl_aff_is_nan(aff
))
1850 if (isl_int_is_one(f
))
1853 aff
= isl_aff_cow(aff
);
1856 aff
->v
= isl_vec_cow(aff
->v
);
1858 return isl_aff_free(aff
);
1860 if (isl_int_is_pos(f
) && isl_int_is_divisible_by(aff
->v
->el
[0], f
)) {
1861 isl_int_divexact(aff
->v
->el
[0], aff
->v
->el
[0], f
);
1866 isl_int_gcd(gcd
, aff
->v
->el
[0], f
);
1867 isl_int_divexact(aff
->v
->el
[0], aff
->v
->el
[0], gcd
);
1868 isl_int_divexact(gcd
, f
, gcd
);
1869 isl_seq_scale(aff
->v
->el
+ 1, aff
->v
->el
+ 1, gcd
, aff
->v
->size
- 1);
1875 /* Multiple "aff" by "v".
1877 __isl_give isl_aff
*isl_aff_scale_val(__isl_take isl_aff
*aff
,
1878 __isl_take isl_val
*v
)
1883 if (isl_val_is_one(v
)) {
1888 if (!isl_val_is_rat(v
))
1889 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1890 "expecting rational factor", goto error
);
1892 aff
= isl_aff_scale(aff
, v
->n
);
1893 aff
= isl_aff_scale_down(aff
, v
->d
);
1903 /* Return the result of scaling "aff" down by a factor of "f".
1905 * As a special case, NaN/f = NaN.
1907 __isl_give isl_aff
*isl_aff_scale_down(__isl_take isl_aff
*aff
, isl_int f
)
1913 if (isl_aff_is_nan(aff
))
1916 if (isl_int_is_one(f
))
1919 aff
= isl_aff_cow(aff
);
1923 if (isl_int_is_zero(f
))
1924 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1925 "cannot scale down by zero", return isl_aff_free(aff
));
1927 aff
->v
= isl_vec_cow(aff
->v
);
1929 return isl_aff_free(aff
);
1932 isl_seq_gcd(aff
->v
->el
+ 1, aff
->v
->size
- 1, &gcd
);
1933 isl_int_gcd(gcd
, gcd
, f
);
1934 isl_seq_scale_down(aff
->v
->el
+ 1, aff
->v
->el
+ 1, gcd
, aff
->v
->size
- 1);
1935 isl_int_divexact(gcd
, f
, gcd
);
1936 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], gcd
);
1942 /* Divide "aff" by "v".
1944 __isl_give isl_aff
*isl_aff_scale_down_val(__isl_take isl_aff
*aff
,
1945 __isl_take isl_val
*v
)
1950 if (isl_val_is_one(v
)) {
1955 if (!isl_val_is_rat(v
))
1956 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1957 "expecting rational factor", goto error
);
1958 if (!isl_val_is_pos(v
))
1959 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1960 "factor needs to be positive", goto error
);
1962 aff
= isl_aff_scale(aff
, v
->d
);
1963 aff
= isl_aff_scale_down(aff
, v
->n
);
1973 __isl_give isl_aff
*isl_aff_scale_down_ui(__isl_take isl_aff
*aff
, unsigned f
)
1981 isl_int_set_ui(v
, f
);
1982 aff
= isl_aff_scale_down(aff
, v
);
1988 __isl_give isl_aff
*isl_aff_set_dim_name(__isl_take isl_aff
*aff
,
1989 enum isl_dim_type type
, unsigned pos
, const char *s
)
1991 aff
= isl_aff_cow(aff
);
1994 if (type
== isl_dim_out
)
1995 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1996 "cannot set name of output/set dimension",
1997 return isl_aff_free(aff
));
1998 if (type
== isl_dim_in
)
2000 aff
->ls
= isl_local_space_set_dim_name(aff
->ls
, type
, pos
, s
);
2002 return isl_aff_free(aff
);
2007 __isl_give isl_aff
*isl_aff_set_dim_id(__isl_take isl_aff
*aff
,
2008 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
2010 aff
= isl_aff_cow(aff
);
2013 if (type
== isl_dim_out
)
2014 isl_die(aff
->v
->ctx
, isl_error_invalid
,
2015 "cannot set name of output/set dimension",
2017 if (type
== isl_dim_in
)
2019 aff
->ls
= isl_local_space_set_dim_id(aff
->ls
, type
, pos
, id
);
2021 return isl_aff_free(aff
);
2030 /* Replace the identifier of the input tuple of "aff" by "id".
2031 * type is currently required to be equal to isl_dim_in
2033 __isl_give isl_aff
*isl_aff_set_tuple_id(__isl_take isl_aff
*aff
,
2034 enum isl_dim_type type
, __isl_take isl_id
*id
)
2036 aff
= isl_aff_cow(aff
);
2039 if (type
!= isl_dim_out
)
2040 isl_die(aff
->v
->ctx
, isl_error_invalid
,
2041 "cannot only set id of input tuple", goto error
);
2042 aff
->ls
= isl_local_space_set_tuple_id(aff
->ls
, isl_dim_set
, id
);
2044 return isl_aff_free(aff
);
2053 /* Exploit the equalities in "eq" to simplify the affine expression
2054 * and the expressions of the integer divisions in the local space.
2055 * The integer divisions in this local space are assumed to appear
2056 * as regular dimensions in "eq".
2058 static __isl_give isl_aff
*isl_aff_substitute_equalities_lifted(
2059 __isl_take isl_aff
*aff
, __isl_take isl_basic_set
*eq
)
2067 if (eq
->n_eq
== 0) {
2068 isl_basic_set_free(eq
);
2072 aff
= isl_aff_cow(aff
);
2076 aff
->ls
= isl_local_space_substitute_equalities(aff
->ls
,
2077 isl_basic_set_copy(eq
));
2078 aff
->v
= isl_vec_cow(aff
->v
);
2079 if (!aff
->ls
|| !aff
->v
)
2082 total
= 1 + isl_space_dim(eq
->dim
, isl_dim_all
);
2084 for (i
= 0; i
< eq
->n_eq
; ++i
) {
2085 j
= isl_seq_last_non_zero(eq
->eq
[i
], total
+ n_div
);
2086 if (j
< 0 || j
== 0 || j
>= total
)
2089 isl_seq_elim(aff
->v
->el
+ 1, eq
->eq
[i
], j
, total
,
2093 isl_basic_set_free(eq
);
2094 aff
= isl_aff_normalize(aff
);
2097 isl_basic_set_free(eq
);
2102 /* Exploit the equalities in "eq" to simplify the affine expression
2103 * and the expressions of the integer divisions in the local space.
2105 __isl_give isl_aff
*isl_aff_substitute_equalities(__isl_take isl_aff
*aff
,
2106 __isl_take isl_basic_set
*eq
)
2112 n_div
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
2114 eq
= isl_basic_set_add_dims(eq
, isl_dim_set
, n_div
);
2115 return isl_aff_substitute_equalities_lifted(aff
, eq
);
2117 isl_basic_set_free(eq
);
2122 /* Look for equalities among the variables shared by context and aff
2123 * and the integer divisions of aff, if any.
2124 * The equalities are then used to eliminate coefficients and/or integer
2125 * divisions from aff.
2127 __isl_give isl_aff
*isl_aff_gist(__isl_take isl_aff
*aff
,
2128 __isl_take isl_set
*context
)
2130 isl_basic_set
*hull
;
2135 n_div
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
2137 isl_basic_set
*bset
;
2138 isl_local_space
*ls
;
2139 context
= isl_set_add_dims(context
, isl_dim_set
, n_div
);
2140 ls
= isl_aff_get_domain_local_space(aff
);
2141 bset
= isl_basic_set_from_local_space(ls
);
2142 bset
= isl_basic_set_lift(bset
);
2143 bset
= isl_basic_set_flatten(bset
);
2144 context
= isl_set_intersect(context
,
2145 isl_set_from_basic_set(bset
));
2148 hull
= isl_set_affine_hull(context
);
2149 return isl_aff_substitute_equalities_lifted(aff
, hull
);
2152 isl_set_free(context
);
2156 __isl_give isl_aff
*isl_aff_gist_params(__isl_take isl_aff
*aff
,
2157 __isl_take isl_set
*context
)
2159 isl_set
*dom_context
= isl_set_universe(isl_aff_get_domain_space(aff
));
2160 dom_context
= isl_set_intersect_params(dom_context
, context
);
2161 return isl_aff_gist(aff
, dom_context
);
2164 /* Return a basic set containing those elements in the space
2165 * of aff where it is non-negative.
2166 * If "rational" is set, then return a rational basic set.
2168 * If "aff" is NaN, then it is not non-negative (it's not negative either).
2170 static __isl_give isl_basic_set
*aff_nonneg_basic_set(
2171 __isl_take isl_aff
*aff
, int rational
)
2173 isl_constraint
*ineq
;
2174 isl_basic_set
*bset
;
2178 if (isl_aff_is_nan(aff
)) {
2179 isl_space
*space
= isl_aff_get_domain_space(aff
);
2181 return isl_basic_set_empty(space
);
2184 ineq
= isl_inequality_from_aff(aff
);
2186 bset
= isl_basic_set_from_constraint(ineq
);
2188 bset
= isl_basic_set_set_rational(bset
);
2189 bset
= isl_basic_set_simplify(bset
);
2193 /* Return a basic set containing those elements in the space
2194 * of aff where it is non-negative.
2196 __isl_give isl_basic_set
*isl_aff_nonneg_basic_set(__isl_take isl_aff
*aff
)
2198 return aff_nonneg_basic_set(aff
, 0);
2201 /* Return a basic set containing those elements in the domain space
2202 * of aff where it is negative.
2204 __isl_give isl_basic_set
*isl_aff_neg_basic_set(__isl_take isl_aff
*aff
)
2206 aff
= isl_aff_neg(aff
);
2207 aff
= isl_aff_add_constant_num_si(aff
, -1);
2208 return isl_aff_nonneg_basic_set(aff
);
2211 /* Return a basic set containing those elements in the space
2212 * of aff where it is zero.
2213 * If "rational" is set, then return a rational basic set.
2215 * If "aff" is NaN, then it is not zero.
2217 static __isl_give isl_basic_set
*aff_zero_basic_set(__isl_take isl_aff
*aff
,
2220 isl_constraint
*ineq
;
2221 isl_basic_set
*bset
;
2225 if (isl_aff_is_nan(aff
)) {
2226 isl_space
*space
= isl_aff_get_domain_space(aff
);
2228 return isl_basic_set_empty(space
);
2231 ineq
= isl_equality_from_aff(aff
);
2233 bset
= isl_basic_set_from_constraint(ineq
);
2235 bset
= isl_basic_set_set_rational(bset
);
2236 bset
= isl_basic_set_simplify(bset
);
2240 /* Return a basic set containing those elements in the space
2241 * of aff where it is zero.
2243 __isl_give isl_basic_set
*isl_aff_zero_basic_set(__isl_take isl_aff
*aff
)
2245 return aff_zero_basic_set(aff
, 0);
2248 /* Return a basic set containing those elements in the shared space
2249 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
2251 __isl_give isl_basic_set
*isl_aff_ge_basic_set(__isl_take isl_aff
*aff1
,
2252 __isl_take isl_aff
*aff2
)
2254 aff1
= isl_aff_sub(aff1
, aff2
);
2256 return isl_aff_nonneg_basic_set(aff1
);
2259 /* Return a basic set containing those elements in the shared space
2260 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
2262 __isl_give isl_basic_set
*isl_aff_le_basic_set(__isl_take isl_aff
*aff1
,
2263 __isl_take isl_aff
*aff2
)
2265 return isl_aff_ge_basic_set(aff2
, aff1
);
2268 __isl_give isl_aff
*isl_aff_add_on_domain(__isl_keep isl_set
*dom
,
2269 __isl_take isl_aff
*aff1
, __isl_take isl_aff
*aff2
)
2271 aff1
= isl_aff_add(aff1
, aff2
);
2272 aff1
= isl_aff_gist(aff1
, isl_set_copy(dom
));
2276 int isl_aff_is_empty(__isl_keep isl_aff
*aff
)
2284 /* Check whether the given affine expression has non-zero coefficient
2285 * for any dimension in the given range or if any of these dimensions
2286 * appear with non-zero coefficients in any of the integer divisions
2287 * involved in the affine expression.
2289 int isl_aff_involves_dims(__isl_keep isl_aff
*aff
,
2290 enum isl_dim_type type
, unsigned first
, unsigned n
)
2302 ctx
= isl_aff_get_ctx(aff
);
2303 if (first
+ n
> isl_aff_dim(aff
, type
))
2304 isl_die(ctx
, isl_error_invalid
,
2305 "range out of bounds", return -1);
2307 active
= isl_local_space_get_active(aff
->ls
, aff
->v
->el
+ 2);
2311 first
+= isl_local_space_offset(aff
->ls
, type
) - 1;
2312 for (i
= 0; i
< n
; ++i
)
2313 if (active
[first
+ i
]) {
2326 __isl_give isl_aff
*isl_aff_drop_dims(__isl_take isl_aff
*aff
,
2327 enum isl_dim_type type
, unsigned first
, unsigned n
)
2333 if (type
== isl_dim_out
)
2334 isl_die(aff
->v
->ctx
, isl_error_invalid
,
2335 "cannot drop output/set dimension",
2336 return isl_aff_free(aff
));
2337 if (type
== isl_dim_in
)
2339 if (n
== 0 && !isl_local_space_is_named_or_nested(aff
->ls
, type
))
2342 ctx
= isl_aff_get_ctx(aff
);
2343 if (first
+ n
> isl_local_space_dim(aff
->ls
, type
))
2344 isl_die(ctx
, isl_error_invalid
, "range out of bounds",
2345 return isl_aff_free(aff
));
2347 aff
= isl_aff_cow(aff
);
2351 aff
->ls
= isl_local_space_drop_dims(aff
->ls
, type
, first
, n
);
2353 return isl_aff_free(aff
);
2355 first
+= 1 + isl_local_space_offset(aff
->ls
, type
);
2356 aff
->v
= isl_vec_drop_els(aff
->v
, first
, n
);
2358 return isl_aff_free(aff
);
2363 /* Project the domain of the affine expression onto its parameter space.
2364 * The affine expression may not involve any of the domain dimensions.
2366 __isl_give isl_aff
*isl_aff_project_domain_on_params(__isl_take isl_aff
*aff
)
2372 n
= isl_aff_dim(aff
, isl_dim_in
);
2373 involves
= isl_aff_involves_dims(aff
, isl_dim_in
, 0, n
);
2375 return isl_aff_free(aff
);
2377 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2378 "affine expression involves some of the domain dimensions",
2379 return isl_aff_free(aff
));
2380 aff
= isl_aff_drop_dims(aff
, isl_dim_in
, 0, n
);
2381 space
= isl_aff_get_domain_space(aff
);
2382 space
= isl_space_params(space
);
2383 aff
= isl_aff_reset_domain_space(aff
, space
);
2387 __isl_give isl_aff
*isl_aff_insert_dims(__isl_take isl_aff
*aff
,
2388 enum isl_dim_type type
, unsigned first
, unsigned n
)
2394 if (type
== isl_dim_out
)
2395 isl_die(aff
->v
->ctx
, isl_error_invalid
,
2396 "cannot insert output/set dimensions",
2397 return isl_aff_free(aff
));
2398 if (type
== isl_dim_in
)
2400 if (n
== 0 && !isl_local_space_is_named_or_nested(aff
->ls
, type
))
2403 ctx
= isl_aff_get_ctx(aff
);
2404 if (first
> isl_local_space_dim(aff
->ls
, type
))
2405 isl_die(ctx
, isl_error_invalid
, "position out of bounds",
2406 return isl_aff_free(aff
));
2408 aff
= isl_aff_cow(aff
);
2412 aff
->ls
= isl_local_space_insert_dims(aff
->ls
, type
, first
, n
);
2414 return isl_aff_free(aff
);
2416 first
+= 1 + isl_local_space_offset(aff
->ls
, type
);
2417 aff
->v
= isl_vec_insert_zero_els(aff
->v
, first
, n
);
2419 return isl_aff_free(aff
);
2424 __isl_give isl_aff
*isl_aff_add_dims(__isl_take isl_aff
*aff
,
2425 enum isl_dim_type type
, unsigned n
)
2429 pos
= isl_aff_dim(aff
, type
);
2431 return isl_aff_insert_dims(aff
, type
, pos
, n
);
2434 __isl_give isl_pw_aff
*isl_pw_aff_add_dims(__isl_take isl_pw_aff
*pwaff
,
2435 enum isl_dim_type type
, unsigned n
)
2439 pos
= isl_pw_aff_dim(pwaff
, type
);
2441 return isl_pw_aff_insert_dims(pwaff
, type
, pos
, n
);
2444 /* Move the "n" dimensions of "src_type" starting at "src_pos" of "aff"
2445 * to dimensions of "dst_type" at "dst_pos".
2447 * We only support moving input dimensions to parameters and vice versa.
2449 __isl_give isl_aff
*isl_aff_move_dims(__isl_take isl_aff
*aff
,
2450 enum isl_dim_type dst_type
, unsigned dst_pos
,
2451 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
2459 !isl_local_space_is_named_or_nested(aff
->ls
, src_type
) &&
2460 !isl_local_space_is_named_or_nested(aff
->ls
, dst_type
))
2463 if (dst_type
== isl_dim_out
|| src_type
== isl_dim_out
)
2464 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2465 "cannot move output/set dimension", isl_aff_free(aff
));
2466 if (dst_type
== isl_dim_div
|| src_type
== isl_dim_div
)
2467 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2468 "cannot move divs", isl_aff_free(aff
));
2469 if (dst_type
== isl_dim_in
)
2470 dst_type
= isl_dim_set
;
2471 if (src_type
== isl_dim_in
)
2472 src_type
= isl_dim_set
;
2474 if (src_pos
+ n
> isl_local_space_dim(aff
->ls
, src_type
))
2475 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2476 "range out of bounds", isl_aff_free(aff
));
2477 if (dst_type
== src_type
)
2478 isl_die(isl_aff_get_ctx(aff
), isl_error_unsupported
,
2479 "moving dims within the same type not supported",
2482 aff
= isl_aff_cow(aff
);
2486 g_src_pos
= 1 + isl_local_space_offset(aff
->ls
, src_type
) + src_pos
;
2487 g_dst_pos
= 1 + isl_local_space_offset(aff
->ls
, dst_type
) + dst_pos
;
2488 if (dst_type
> src_type
)
2491 aff
->v
= isl_vec_move_els(aff
->v
, g_dst_pos
, g_src_pos
, n
);
2492 aff
->ls
= isl_local_space_move_dims(aff
->ls
, dst_type
, dst_pos
,
2493 src_type
, src_pos
, n
);
2494 if (!aff
->v
|| !aff
->ls
)
2495 return isl_aff_free(aff
);
2497 aff
= sort_divs(aff
);
2502 __isl_give isl_pw_aff
*isl_pw_aff_from_aff(__isl_take isl_aff
*aff
)
2504 isl_set
*dom
= isl_set_universe(isl_aff_get_domain_space(aff
));
2505 return isl_pw_aff_alloc(dom
, aff
);
2509 #define PW isl_pw_aff
2513 #define EL_IS_ZERO is_empty
2517 #define IS_ZERO is_empty
2520 #undef DEFAULT_IS_ZERO
2521 #define DEFAULT_IS_ZERO 0
2528 #include <isl_pw_templ.c>
2530 static __isl_give isl_set
*align_params_pw_pw_set_and(
2531 __isl_take isl_pw_aff
*pwaff1
, __isl_take isl_pw_aff
*pwaff2
,
2532 __isl_give isl_set
*(*fn
)(__isl_take isl_pw_aff
*pwaff1
,
2533 __isl_take isl_pw_aff
*pwaff2
))
2535 if (!pwaff1
|| !pwaff2
)
2537 if (isl_space_match(pwaff1
->dim
, isl_dim_param
,
2538 pwaff2
->dim
, isl_dim_param
))
2539 return fn(pwaff1
, pwaff2
);
2540 if (!isl_space_has_named_params(pwaff1
->dim
) ||
2541 !isl_space_has_named_params(pwaff2
->dim
))
2542 isl_die(isl_pw_aff_get_ctx(pwaff1
), isl_error_invalid
,
2543 "unaligned unnamed parameters", goto error
);
2544 pwaff1
= isl_pw_aff_align_params(pwaff1
, isl_pw_aff_get_space(pwaff2
));
2545 pwaff2
= isl_pw_aff_align_params(pwaff2
, isl_pw_aff_get_space(pwaff1
));
2546 return fn(pwaff1
, pwaff2
);
2548 isl_pw_aff_free(pwaff1
);
2549 isl_pw_aff_free(pwaff2
);
2553 /* Compute a piecewise quasi-affine expression with a domain that
2554 * is the union of those of pwaff1 and pwaff2 and such that on each
2555 * cell, the quasi-affine expression is the better (according to cmp)
2556 * of those of pwaff1 and pwaff2. If only one of pwaff1 or pwaff2
2557 * is defined on a given cell, then the associated expression
2558 * is the defined one.
2560 static __isl_give isl_pw_aff
*pw_aff_union_opt(__isl_take isl_pw_aff
*pwaff1
,
2561 __isl_take isl_pw_aff
*pwaff2
,
2562 __isl_give isl_basic_set
*(*cmp
)(__isl_take isl_aff
*aff1
,
2563 __isl_take isl_aff
*aff2
))
2570 if (!pwaff1
|| !pwaff2
)
2573 ctx
= isl_space_get_ctx(pwaff1
->dim
);
2574 if (!isl_space_is_equal(pwaff1
->dim
, pwaff2
->dim
))
2575 isl_die(ctx
, isl_error_invalid
,
2576 "arguments should live in same space", goto error
);
2578 if (isl_pw_aff_is_empty(pwaff1
)) {
2579 isl_pw_aff_free(pwaff1
);
2583 if (isl_pw_aff_is_empty(pwaff2
)) {
2584 isl_pw_aff_free(pwaff2
);
2588 n
= 2 * (pwaff1
->n
+ 1) * (pwaff2
->n
+ 1);
2589 res
= isl_pw_aff_alloc_size(isl_space_copy(pwaff1
->dim
), n
);
2591 for (i
= 0; i
< pwaff1
->n
; ++i
) {
2592 set
= isl_set_copy(pwaff1
->p
[i
].set
);
2593 for (j
= 0; j
< pwaff2
->n
; ++j
) {
2594 struct isl_set
*common
;
2597 common
= isl_set_intersect(
2598 isl_set_copy(pwaff1
->p
[i
].set
),
2599 isl_set_copy(pwaff2
->p
[j
].set
));
2600 better
= isl_set_from_basic_set(cmp(
2601 isl_aff_copy(pwaff2
->p
[j
].aff
),
2602 isl_aff_copy(pwaff1
->p
[i
].aff
)));
2603 better
= isl_set_intersect(common
, better
);
2604 if (isl_set_plain_is_empty(better
)) {
2605 isl_set_free(better
);
2608 set
= isl_set_subtract(set
, isl_set_copy(better
));
2610 res
= isl_pw_aff_add_piece(res
, better
,
2611 isl_aff_copy(pwaff2
->p
[j
].aff
));
2613 res
= isl_pw_aff_add_piece(res
, set
,
2614 isl_aff_copy(pwaff1
->p
[i
].aff
));
2617 for (j
= 0; j
< pwaff2
->n
; ++j
) {
2618 set
= isl_set_copy(pwaff2
->p
[j
].set
);
2619 for (i
= 0; i
< pwaff1
->n
; ++i
)
2620 set
= isl_set_subtract(set
,
2621 isl_set_copy(pwaff1
->p
[i
].set
));
2622 res
= isl_pw_aff_add_piece(res
, set
,
2623 isl_aff_copy(pwaff2
->p
[j
].aff
));
2626 isl_pw_aff_free(pwaff1
);
2627 isl_pw_aff_free(pwaff2
);
2631 isl_pw_aff_free(pwaff1
);
2632 isl_pw_aff_free(pwaff2
);
2636 /* Compute a piecewise quasi-affine expression with a domain that
2637 * is the union of those of pwaff1 and pwaff2 and such that on each
2638 * cell, the quasi-affine expression is the maximum of those of pwaff1
2639 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2640 * cell, then the associated expression is the defined one.
2642 static __isl_give isl_pw_aff
*pw_aff_union_max(__isl_take isl_pw_aff
*pwaff1
,
2643 __isl_take isl_pw_aff
*pwaff2
)
2645 return pw_aff_union_opt(pwaff1
, pwaff2
, &isl_aff_ge_basic_set
);
2648 __isl_give isl_pw_aff
*isl_pw_aff_union_max(__isl_take isl_pw_aff
*pwaff1
,
2649 __isl_take isl_pw_aff
*pwaff2
)
2651 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
,
2655 /* Compute a piecewise quasi-affine expression with a domain that
2656 * is the union of those of pwaff1 and pwaff2 and such that on each
2657 * cell, the quasi-affine expression is the minimum of those of pwaff1
2658 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2659 * cell, then the associated expression is the defined one.
2661 static __isl_give isl_pw_aff
*pw_aff_union_min(__isl_take isl_pw_aff
*pwaff1
,
2662 __isl_take isl_pw_aff
*pwaff2
)
2664 return pw_aff_union_opt(pwaff1
, pwaff2
, &isl_aff_le_basic_set
);
2667 __isl_give isl_pw_aff
*isl_pw_aff_union_min(__isl_take isl_pw_aff
*pwaff1
,
2668 __isl_take isl_pw_aff
*pwaff2
)
2670 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
,
2674 __isl_give isl_pw_aff
*isl_pw_aff_union_opt(__isl_take isl_pw_aff
*pwaff1
,
2675 __isl_take isl_pw_aff
*pwaff2
, int max
)
2678 return isl_pw_aff_union_max(pwaff1
, pwaff2
);
2680 return isl_pw_aff_union_min(pwaff1
, pwaff2
);
2683 /* Construct a map with as domain the domain of pwaff and
2684 * one-dimensional range corresponding to the affine expressions.
2686 static __isl_give isl_map
*map_from_pw_aff(__isl_take isl_pw_aff
*pwaff
)
2695 dim
= isl_pw_aff_get_space(pwaff
);
2696 map
= isl_map_empty(dim
);
2698 for (i
= 0; i
< pwaff
->n
; ++i
) {
2699 isl_basic_map
*bmap
;
2702 bmap
= isl_basic_map_from_aff(isl_aff_copy(pwaff
->p
[i
].aff
));
2703 map_i
= isl_map_from_basic_map(bmap
);
2704 map_i
= isl_map_intersect_domain(map_i
,
2705 isl_set_copy(pwaff
->p
[i
].set
));
2706 map
= isl_map_union_disjoint(map
, map_i
);
2709 isl_pw_aff_free(pwaff
);
2714 /* Construct a map with as domain the domain of pwaff and
2715 * one-dimensional range corresponding to the affine expressions.
2717 __isl_give isl_map
*isl_map_from_pw_aff(__isl_take isl_pw_aff
*pwaff
)
2721 if (isl_space_is_set(pwaff
->dim
))
2722 isl_die(isl_pw_aff_get_ctx(pwaff
), isl_error_invalid
,
2723 "space of input is not a map", goto error
);
2724 return map_from_pw_aff(pwaff
);
2726 isl_pw_aff_free(pwaff
);
2730 /* Construct a one-dimensional set with as parameter domain
2731 * the domain of pwaff and the single set dimension
2732 * corresponding to the affine expressions.
2734 __isl_give isl_set
*isl_set_from_pw_aff(__isl_take isl_pw_aff
*pwaff
)
2738 if (!isl_space_is_set(pwaff
->dim
))
2739 isl_die(isl_pw_aff_get_ctx(pwaff
), isl_error_invalid
,
2740 "space of input is not a set", goto error
);
2741 return map_from_pw_aff(pwaff
);
2743 isl_pw_aff_free(pwaff
);
2747 /* Return a set containing those elements in the domain
2748 * of pwaff where it is non-negative.
2750 __isl_give isl_set
*isl_pw_aff_nonneg_set(__isl_take isl_pw_aff
*pwaff
)
2758 set
= isl_set_empty(isl_pw_aff_get_domain_space(pwaff
));
2760 for (i
= 0; i
< pwaff
->n
; ++i
) {
2761 isl_basic_set
*bset
;
2765 rational
= isl_set_has_rational(pwaff
->p
[i
].set
);
2766 bset
= aff_nonneg_basic_set(isl_aff_copy(pwaff
->p
[i
].aff
),
2768 set_i
= isl_set_from_basic_set(bset
);
2769 set_i
= isl_set_intersect(set_i
, isl_set_copy(pwaff
->p
[i
].set
));
2770 set
= isl_set_union_disjoint(set
, set_i
);
2773 isl_pw_aff_free(pwaff
);
2778 /* Return a set containing those elements in the domain
2779 * of pwaff where it is zero (if complement is 0) or not zero
2780 * (if complement is 1).
2782 * The pieces with a NaN never belong to the result since
2783 * NaN is neither zero nor non-zero.
2785 static __isl_give isl_set
*pw_aff_zero_set(__isl_take isl_pw_aff
*pwaff
,
2794 set
= isl_set_empty(isl_pw_aff_get_domain_space(pwaff
));
2796 for (i
= 0; i
< pwaff
->n
; ++i
) {
2797 isl_basic_set
*bset
;
2798 isl_set
*set_i
, *zero
;
2801 if (isl_aff_is_nan(pwaff
->p
[i
].aff
))
2804 rational
= isl_set_has_rational(pwaff
->p
[i
].set
);
2805 bset
= aff_zero_basic_set(isl_aff_copy(pwaff
->p
[i
].aff
),
2807 zero
= isl_set_from_basic_set(bset
);
2808 set_i
= isl_set_copy(pwaff
->p
[i
].set
);
2810 set_i
= isl_set_subtract(set_i
, zero
);
2812 set_i
= isl_set_intersect(set_i
, zero
);
2813 set
= isl_set_union_disjoint(set
, set_i
);
2816 isl_pw_aff_free(pwaff
);
2821 /* Return a set containing those elements in the domain
2822 * of pwaff where it is zero.
2824 __isl_give isl_set
*isl_pw_aff_zero_set(__isl_take isl_pw_aff
*pwaff
)
2826 return pw_aff_zero_set(pwaff
, 0);
2829 /* Return a set containing those elements in the domain
2830 * of pwaff where it is not zero.
2832 __isl_give isl_set
*isl_pw_aff_non_zero_set(__isl_take isl_pw_aff
*pwaff
)
2834 return pw_aff_zero_set(pwaff
, 1);
2837 /* Return a set containing those elements in the shared domain
2838 * of pwaff1 and pwaff2 where pwaff1 is greater than (or equal) to pwaff2.
2840 * We compute the difference on the shared domain and then construct
2841 * the set of values where this difference is non-negative.
2842 * If strict is set, we first subtract 1 from the difference.
2843 * If equal is set, we only return the elements where pwaff1 and pwaff2
2846 static __isl_give isl_set
*pw_aff_gte_set(__isl_take isl_pw_aff
*pwaff1
,
2847 __isl_take isl_pw_aff
*pwaff2
, int strict
, int equal
)
2849 isl_set
*set1
, *set2
;
2851 set1
= isl_pw_aff_domain(isl_pw_aff_copy(pwaff1
));
2852 set2
= isl_pw_aff_domain(isl_pw_aff_copy(pwaff2
));
2853 set1
= isl_set_intersect(set1
, set2
);
2854 pwaff1
= isl_pw_aff_intersect_domain(pwaff1
, isl_set_copy(set1
));
2855 pwaff2
= isl_pw_aff_intersect_domain(pwaff2
, isl_set_copy(set1
));
2856 pwaff1
= isl_pw_aff_add(pwaff1
, isl_pw_aff_neg(pwaff2
));
2859 isl_space
*dim
= isl_set_get_space(set1
);
2861 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(dim
));
2862 aff
= isl_aff_add_constant_si(aff
, -1);
2863 pwaff1
= isl_pw_aff_add(pwaff1
, isl_pw_aff_alloc(set1
, aff
));
2868 return isl_pw_aff_zero_set(pwaff1
);
2869 return isl_pw_aff_nonneg_set(pwaff1
);
2872 /* Return a set containing those elements in the shared domain
2873 * of pwaff1 and pwaff2 where pwaff1 is equal to pwaff2.
2875 static __isl_give isl_set
*pw_aff_eq_set(__isl_take isl_pw_aff
*pwaff1
,
2876 __isl_take isl_pw_aff
*pwaff2
)
2878 return pw_aff_gte_set(pwaff1
, pwaff2
, 0, 1);
2881 __isl_give isl_set
*isl_pw_aff_eq_set(__isl_take isl_pw_aff
*pwaff1
,
2882 __isl_take isl_pw_aff
*pwaff2
)
2884 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_eq_set
);
2887 /* Return a set containing those elements in the shared domain
2888 * of pwaff1 and pwaff2 where pwaff1 is greater than or equal to pwaff2.
2890 static __isl_give isl_set
*pw_aff_ge_set(__isl_take isl_pw_aff
*pwaff1
,
2891 __isl_take isl_pw_aff
*pwaff2
)
2893 return pw_aff_gte_set(pwaff1
, pwaff2
, 0, 0);
2896 __isl_give isl_set
*isl_pw_aff_ge_set(__isl_take isl_pw_aff
*pwaff1
,
2897 __isl_take isl_pw_aff
*pwaff2
)
2899 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_ge_set
);
2902 /* Return a set containing those elements in the shared domain
2903 * of pwaff1 and pwaff2 where pwaff1 is strictly greater than pwaff2.
2905 static __isl_give isl_set
*pw_aff_gt_set(__isl_take isl_pw_aff
*pwaff1
,
2906 __isl_take isl_pw_aff
*pwaff2
)
2908 return pw_aff_gte_set(pwaff1
, pwaff2
, 1, 0);
2911 __isl_give isl_set
*isl_pw_aff_gt_set(__isl_take isl_pw_aff
*pwaff1
,
2912 __isl_take isl_pw_aff
*pwaff2
)
2914 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_gt_set
);
2917 __isl_give isl_set
*isl_pw_aff_le_set(__isl_take isl_pw_aff
*pwaff1
,
2918 __isl_take isl_pw_aff
*pwaff2
)
2920 return isl_pw_aff_ge_set(pwaff2
, pwaff1
);
2923 __isl_give isl_set
*isl_pw_aff_lt_set(__isl_take isl_pw_aff
*pwaff1
,
2924 __isl_take isl_pw_aff
*pwaff2
)
2926 return isl_pw_aff_gt_set(pwaff2
, pwaff1
);
2929 /* Return a set containing those elements in the shared domain
2930 * of the elements of list1 and list2 where each element in list1
2931 * has the relation specified by "fn" with each element in list2.
2933 static __isl_give isl_set
*pw_aff_list_set(__isl_take isl_pw_aff_list
*list1
,
2934 __isl_take isl_pw_aff_list
*list2
,
2935 __isl_give isl_set
*(*fn
)(__isl_take isl_pw_aff
*pwaff1
,
2936 __isl_take isl_pw_aff
*pwaff2
))
2942 if (!list1
|| !list2
)
2945 ctx
= isl_pw_aff_list_get_ctx(list1
);
2946 if (list1
->n
< 1 || list2
->n
< 1)
2947 isl_die(ctx
, isl_error_invalid
,
2948 "list should contain at least one element", goto error
);
2950 set
= isl_set_universe(isl_pw_aff_get_domain_space(list1
->p
[0]));
2951 for (i
= 0; i
< list1
->n
; ++i
)
2952 for (j
= 0; j
< list2
->n
; ++j
) {
2955 set_ij
= fn(isl_pw_aff_copy(list1
->p
[i
]),
2956 isl_pw_aff_copy(list2
->p
[j
]));
2957 set
= isl_set_intersect(set
, set_ij
);
2960 isl_pw_aff_list_free(list1
);
2961 isl_pw_aff_list_free(list2
);
2964 isl_pw_aff_list_free(list1
);
2965 isl_pw_aff_list_free(list2
);
2969 /* Return a set containing those elements in the shared domain
2970 * of the elements of list1 and list2 where each element in list1
2971 * is equal to each element in list2.
2973 __isl_give isl_set
*isl_pw_aff_list_eq_set(__isl_take isl_pw_aff_list
*list1
,
2974 __isl_take isl_pw_aff_list
*list2
)
2976 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_eq_set
);
2979 __isl_give isl_set
*isl_pw_aff_list_ne_set(__isl_take isl_pw_aff_list
*list1
,
2980 __isl_take isl_pw_aff_list
*list2
)
2982 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_ne_set
);
2985 /* Return a set containing those elements in the shared domain
2986 * of the elements of list1 and list2 where each element in list1
2987 * is less than or equal to each element in list2.
2989 __isl_give isl_set
*isl_pw_aff_list_le_set(__isl_take isl_pw_aff_list
*list1
,
2990 __isl_take isl_pw_aff_list
*list2
)
2992 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_le_set
);
2995 __isl_give isl_set
*isl_pw_aff_list_lt_set(__isl_take isl_pw_aff_list
*list1
,
2996 __isl_take isl_pw_aff_list
*list2
)
2998 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_lt_set
);
3001 __isl_give isl_set
*isl_pw_aff_list_ge_set(__isl_take isl_pw_aff_list
*list1
,
3002 __isl_take isl_pw_aff_list
*list2
)
3004 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_ge_set
);
3007 __isl_give isl_set
*isl_pw_aff_list_gt_set(__isl_take isl_pw_aff_list
*list1
,
3008 __isl_take isl_pw_aff_list
*list2
)
3010 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_gt_set
);
3014 /* Return a set containing those elements in the shared domain
3015 * of pwaff1 and pwaff2 where pwaff1 is not equal to pwaff2.
3017 static __isl_give isl_set
*pw_aff_ne_set(__isl_take isl_pw_aff
*pwaff1
,
3018 __isl_take isl_pw_aff
*pwaff2
)
3020 isl_set
*set_lt
, *set_gt
;
3022 set_lt
= isl_pw_aff_lt_set(isl_pw_aff_copy(pwaff1
),
3023 isl_pw_aff_copy(pwaff2
));
3024 set_gt
= isl_pw_aff_gt_set(pwaff1
, pwaff2
);
3025 return isl_set_union_disjoint(set_lt
, set_gt
);
3028 __isl_give isl_set
*isl_pw_aff_ne_set(__isl_take isl_pw_aff
*pwaff1
,
3029 __isl_take isl_pw_aff
*pwaff2
)
3031 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_ne_set
);
3034 __isl_give isl_pw_aff
*isl_pw_aff_scale_down(__isl_take isl_pw_aff
*pwaff
,
3039 if (isl_int_is_one(v
))
3041 if (!isl_int_is_pos(v
))
3042 isl_die(isl_pw_aff_get_ctx(pwaff
), isl_error_invalid
,
3043 "factor needs to be positive",
3044 return isl_pw_aff_free(pwaff
));
3045 pwaff
= isl_pw_aff_cow(pwaff
);
3051 for (i
= 0; i
< pwaff
->n
; ++i
) {
3052 pwaff
->p
[i
].aff
= isl_aff_scale_down(pwaff
->p
[i
].aff
, v
);
3053 if (!pwaff
->p
[i
].aff
)
3054 return isl_pw_aff_free(pwaff
);
3060 __isl_give isl_pw_aff
*isl_pw_aff_floor(__isl_take isl_pw_aff
*pwaff
)
3064 pwaff
= isl_pw_aff_cow(pwaff
);
3070 for (i
= 0; i
< pwaff
->n
; ++i
) {
3071 pwaff
->p
[i
].aff
= isl_aff_floor(pwaff
->p
[i
].aff
);
3072 if (!pwaff
->p
[i
].aff
)
3073 return isl_pw_aff_free(pwaff
);
3079 __isl_give isl_pw_aff
*isl_pw_aff_ceil(__isl_take isl_pw_aff
*pwaff
)
3083 pwaff
= isl_pw_aff_cow(pwaff
);
3089 for (i
= 0; i
< pwaff
->n
; ++i
) {
3090 pwaff
->p
[i
].aff
= isl_aff_ceil(pwaff
->p
[i
].aff
);
3091 if (!pwaff
->p
[i
].aff
)
3092 return isl_pw_aff_free(pwaff
);
3098 /* Assuming that "cond1" and "cond2" are disjoint,
3099 * return an affine expression that is equal to pwaff1 on cond1
3100 * and to pwaff2 on cond2.
3102 static __isl_give isl_pw_aff
*isl_pw_aff_select(
3103 __isl_take isl_set
*cond1
, __isl_take isl_pw_aff
*pwaff1
,
3104 __isl_take isl_set
*cond2
, __isl_take isl_pw_aff
*pwaff2
)
3106 pwaff1
= isl_pw_aff_intersect_domain(pwaff1
, cond1
);
3107 pwaff2
= isl_pw_aff_intersect_domain(pwaff2
, cond2
);
3109 return isl_pw_aff_add_disjoint(pwaff1
, pwaff2
);
3112 /* Return an affine expression that is equal to pwaff_true for elements
3113 * where "cond" is non-zero and to pwaff_false for elements where "cond"
3115 * That is, return cond ? pwaff_true : pwaff_false;
3117 * If "cond" involves and NaN, then we conservatively return a NaN
3118 * on its entire domain. In principle, we could consider the pieces
3119 * where it is NaN separately from those where it is not.
3121 __isl_give isl_pw_aff
*isl_pw_aff_cond(__isl_take isl_pw_aff
*cond
,
3122 __isl_take isl_pw_aff
*pwaff_true
, __isl_take isl_pw_aff
*pwaff_false
)
3124 isl_set
*cond_true
, *cond_false
;
3128 if (isl_pw_aff_involves_nan(cond
)) {
3129 isl_space
*space
= isl_pw_aff_get_domain_space(cond
);
3130 isl_local_space
*ls
= isl_local_space_from_space(space
);
3131 isl_pw_aff_free(cond
);
3132 isl_pw_aff_free(pwaff_true
);
3133 isl_pw_aff_free(pwaff_false
);
3134 return isl_pw_aff_nan_on_domain(ls
);
3137 cond_true
= isl_pw_aff_non_zero_set(isl_pw_aff_copy(cond
));
3138 cond_false
= isl_pw_aff_zero_set(cond
);
3139 return isl_pw_aff_select(cond_true
, pwaff_true
,
3140 cond_false
, pwaff_false
);
3142 isl_pw_aff_free(cond
);
3143 isl_pw_aff_free(pwaff_true
);
3144 isl_pw_aff_free(pwaff_false
);
3148 int isl_aff_is_cst(__isl_keep isl_aff
*aff
)
3153 return isl_seq_first_non_zero(aff
->v
->el
+ 2, aff
->v
->size
- 2) == -1;
3156 /* Check whether pwaff is a piecewise constant.
3158 int isl_pw_aff_is_cst(__isl_keep isl_pw_aff
*pwaff
)
3165 for (i
= 0; i
< pwaff
->n
; ++i
) {
3166 int is_cst
= isl_aff_is_cst(pwaff
->p
[i
].aff
);
3167 if (is_cst
< 0 || !is_cst
)
3174 /* Return the product of "aff1" and "aff2".
3176 * If either of the two is NaN, then the result is NaN.
3178 * Otherwise, at least one of "aff1" or "aff2" needs to be a constant.
3180 __isl_give isl_aff
*isl_aff_mul(__isl_take isl_aff
*aff1
,
3181 __isl_take isl_aff
*aff2
)
3186 if (isl_aff_is_nan(aff1
)) {
3190 if (isl_aff_is_nan(aff2
)) {
3195 if (!isl_aff_is_cst(aff2
) && isl_aff_is_cst(aff1
))
3196 return isl_aff_mul(aff2
, aff1
);
3198 if (!isl_aff_is_cst(aff2
))
3199 isl_die(isl_aff_get_ctx(aff1
), isl_error_invalid
,
3200 "at least one affine expression should be constant",
3203 aff1
= isl_aff_cow(aff1
);
3207 aff1
= isl_aff_scale(aff1
, aff2
->v
->el
[1]);
3208 aff1
= isl_aff_scale_down(aff1
, aff2
->v
->el
[0]);
3218 /* Divide "aff1" by "aff2", assuming "aff2" is a constant.
3220 * If either of the two is NaN, then the result is NaN.
3222 __isl_give isl_aff
*isl_aff_div(__isl_take isl_aff
*aff1
,
3223 __isl_take isl_aff
*aff2
)
3231 if (isl_aff_is_nan(aff1
)) {
3235 if (isl_aff_is_nan(aff2
)) {
3240 is_cst
= isl_aff_is_cst(aff2
);
3244 isl_die(isl_aff_get_ctx(aff2
), isl_error_invalid
,
3245 "second argument should be a constant", goto error
);
3250 neg
= isl_int_is_neg(aff2
->v
->el
[1]);
3252 isl_int_neg(aff2
->v
->el
[0], aff2
->v
->el
[0]);
3253 isl_int_neg(aff2
->v
->el
[1], aff2
->v
->el
[1]);
3256 aff1
= isl_aff_scale(aff1
, aff2
->v
->el
[0]);
3257 aff1
= isl_aff_scale_down(aff1
, aff2
->v
->el
[1]);
3260 isl_int_neg(aff2
->v
->el
[0], aff2
->v
->el
[0]);
3261 isl_int_neg(aff2
->v
->el
[1], aff2
->v
->el
[1]);
3272 static __isl_give isl_pw_aff
*pw_aff_add(__isl_take isl_pw_aff
*pwaff1
,
3273 __isl_take isl_pw_aff
*pwaff2
)
3275 return isl_pw_aff_on_shared_domain(pwaff1
, pwaff2
, &isl_aff_add
);
3278 __isl_give isl_pw_aff
*isl_pw_aff_add(__isl_take isl_pw_aff
*pwaff1
,
3279 __isl_take isl_pw_aff
*pwaff2
)
3281 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
, &pw_aff_add
);
3284 __isl_give isl_pw_aff
*isl_pw_aff_union_add(__isl_take isl_pw_aff
*pwaff1
,
3285 __isl_take isl_pw_aff
*pwaff2
)
3287 return isl_pw_aff_union_add_(pwaff1
, pwaff2
);
3290 static __isl_give isl_pw_aff
*pw_aff_mul(__isl_take isl_pw_aff
*pwaff1
,
3291 __isl_take isl_pw_aff
*pwaff2
)
3293 return isl_pw_aff_on_shared_domain(pwaff1
, pwaff2
, &isl_aff_mul
);
3296 __isl_give isl_pw_aff
*isl_pw_aff_mul(__isl_take isl_pw_aff
*pwaff1
,
3297 __isl_take isl_pw_aff
*pwaff2
)
3299 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
, &pw_aff_mul
);
3302 static __isl_give isl_pw_aff
*pw_aff_div(__isl_take isl_pw_aff
*pa1
,
3303 __isl_take isl_pw_aff
*pa2
)
3305 return isl_pw_aff_on_shared_domain(pa1
, pa2
, &isl_aff_div
);
3308 /* Divide "pa1" by "pa2", assuming "pa2" is a piecewise constant.
3310 __isl_give isl_pw_aff
*isl_pw_aff_div(__isl_take isl_pw_aff
*pa1
,
3311 __isl_take isl_pw_aff
*pa2
)
3315 is_cst
= isl_pw_aff_is_cst(pa2
);
3319 isl_die(isl_pw_aff_get_ctx(pa2
), isl_error_invalid
,
3320 "second argument should be a piecewise constant",
3322 return isl_pw_aff_align_params_pw_pw_and(pa1
, pa2
, &pw_aff_div
);
3324 isl_pw_aff_free(pa1
);
3325 isl_pw_aff_free(pa2
);
3329 /* Compute the quotient of the integer division of "pa1" by "pa2"
3330 * with rounding towards zero.
3331 * "pa2" is assumed to be a piecewise constant.
3333 * In particular, return
3335 * pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2)
3338 __isl_give isl_pw_aff
*isl_pw_aff_tdiv_q(__isl_take isl_pw_aff
*pa1
,
3339 __isl_take isl_pw_aff
*pa2
)
3345 is_cst
= isl_pw_aff_is_cst(pa2
);
3349 isl_die(isl_pw_aff_get_ctx(pa2
), isl_error_invalid
,
3350 "second argument should be a piecewise constant",
3353 pa1
= isl_pw_aff_div(pa1
, pa2
);
3355 cond
= isl_pw_aff_nonneg_set(isl_pw_aff_copy(pa1
));
3356 f
= isl_pw_aff_floor(isl_pw_aff_copy(pa1
));
3357 c
= isl_pw_aff_ceil(pa1
);
3358 return isl_pw_aff_cond(isl_set_indicator_function(cond
), f
, c
);
3360 isl_pw_aff_free(pa1
);
3361 isl_pw_aff_free(pa2
);
3365 /* Compute the remainder of the integer division of "pa1" by "pa2"
3366 * with rounding towards zero.
3367 * "pa2" is assumed to be a piecewise constant.
3369 * In particular, return
3371 * pa1 - pa2 * (pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2))
3374 __isl_give isl_pw_aff
*isl_pw_aff_tdiv_r(__isl_take isl_pw_aff
*pa1
,
3375 __isl_take isl_pw_aff
*pa2
)
3380 is_cst
= isl_pw_aff_is_cst(pa2
);
3384 isl_die(isl_pw_aff_get_ctx(pa2
), isl_error_invalid
,
3385 "second argument should be a piecewise constant",
3387 res
= isl_pw_aff_tdiv_q(isl_pw_aff_copy(pa1
), isl_pw_aff_copy(pa2
));
3388 res
= isl_pw_aff_mul(pa2
, res
);
3389 res
= isl_pw_aff_sub(pa1
, res
);
3392 isl_pw_aff_free(pa1
);
3393 isl_pw_aff_free(pa2
);
3397 static __isl_give isl_pw_aff
*pw_aff_min(__isl_take isl_pw_aff
*pwaff1
,
3398 __isl_take isl_pw_aff
*pwaff2
)
3403 dom
= isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1
)),
3404 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2
)));
3405 le
= isl_pw_aff_le_set(isl_pw_aff_copy(pwaff1
),
3406 isl_pw_aff_copy(pwaff2
));
3407 dom
= isl_set_subtract(dom
, isl_set_copy(le
));
3408 return isl_pw_aff_select(le
, pwaff1
, dom
, pwaff2
);
3411 __isl_give isl_pw_aff
*isl_pw_aff_min(__isl_take isl_pw_aff
*pwaff1
,
3412 __isl_take isl_pw_aff
*pwaff2
)
3414 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
, &pw_aff_min
);
3417 static __isl_give isl_pw_aff
*pw_aff_max(__isl_take isl_pw_aff
*pwaff1
,
3418 __isl_take isl_pw_aff
*pwaff2
)
3423 dom
= isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1
)),
3424 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2
)));
3425 ge
= isl_pw_aff_ge_set(isl_pw_aff_copy(pwaff1
),
3426 isl_pw_aff_copy(pwaff2
));
3427 dom
= isl_set_subtract(dom
, isl_set_copy(ge
));
3428 return isl_pw_aff_select(ge
, pwaff1
, dom
, pwaff2
);
3431 __isl_give isl_pw_aff
*isl_pw_aff_max(__isl_take isl_pw_aff
*pwaff1
,
3432 __isl_take isl_pw_aff
*pwaff2
)
3434 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
, &pw_aff_max
);
3437 static __isl_give isl_pw_aff
*pw_aff_list_reduce(
3438 __isl_take isl_pw_aff_list
*list
,
3439 __isl_give isl_pw_aff
*(*fn
)(__isl_take isl_pw_aff
*pwaff1
,
3440 __isl_take isl_pw_aff
*pwaff2
))
3449 ctx
= isl_pw_aff_list_get_ctx(list
);
3451 isl_die(ctx
, isl_error_invalid
,
3452 "list should contain at least one element", goto error
);
3454 res
= isl_pw_aff_copy(list
->p
[0]);
3455 for (i
= 1; i
< list
->n
; ++i
)
3456 res
= fn(res
, isl_pw_aff_copy(list
->p
[i
]));
3458 isl_pw_aff_list_free(list
);
3461 isl_pw_aff_list_free(list
);
3465 /* Return an isl_pw_aff that maps each element in the intersection of the
3466 * domains of the elements of list to the minimal corresponding affine
3469 __isl_give isl_pw_aff
*isl_pw_aff_list_min(__isl_take isl_pw_aff_list
*list
)
3471 return pw_aff_list_reduce(list
, &isl_pw_aff_min
);
3474 /* Return an isl_pw_aff that maps each element in the intersection of the
3475 * domains of the elements of list to the maximal corresponding affine
3478 __isl_give isl_pw_aff
*isl_pw_aff_list_max(__isl_take isl_pw_aff_list
*list
)
3480 return pw_aff_list_reduce(list
, &isl_pw_aff_max
);
3483 /* Mark the domains of "pwaff" as rational.
3485 __isl_give isl_pw_aff
*isl_pw_aff_set_rational(__isl_take isl_pw_aff
*pwaff
)
3489 pwaff
= isl_pw_aff_cow(pwaff
);
3495 for (i
= 0; i
< pwaff
->n
; ++i
) {
3496 pwaff
->p
[i
].set
= isl_set_set_rational(pwaff
->p
[i
].set
);
3497 if (!pwaff
->p
[i
].set
)
3498 return isl_pw_aff_free(pwaff
);
3504 /* Mark the domains of the elements of "list" as rational.
3506 __isl_give isl_pw_aff_list
*isl_pw_aff_list_set_rational(
3507 __isl_take isl_pw_aff_list
*list
)
3517 for (i
= 0; i
< n
; ++i
) {
3520 pa
= isl_pw_aff_list_get_pw_aff(list
, i
);
3521 pa
= isl_pw_aff_set_rational(pa
);
3522 list
= isl_pw_aff_list_set_pw_aff(list
, i
, pa
);
3528 /* Do the parameters of "aff" match those of "space"?
3530 int isl_aff_matching_params(__isl_keep isl_aff
*aff
,
3531 __isl_keep isl_space
*space
)
3533 isl_space
*aff_space
;
3539 aff_space
= isl_aff_get_domain_space(aff
);
3541 match
= isl_space_match(space
, isl_dim_param
, aff_space
, isl_dim_param
);
3543 isl_space_free(aff_space
);
3547 /* Check that the domain space of "aff" matches "space".
3549 * Return 0 on success and -1 on error.
3551 int isl_aff_check_match_domain_space(__isl_keep isl_aff
*aff
,
3552 __isl_keep isl_space
*space
)
3554 isl_space
*aff_space
;
3560 aff_space
= isl_aff_get_domain_space(aff
);
3562 match
= isl_space_match(space
, isl_dim_param
, aff_space
, isl_dim_param
);
3566 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
3567 "parameters don't match", goto error
);
3568 match
= isl_space_tuple_is_equal(space
, isl_dim_in
,
3569 aff_space
, isl_dim_set
);
3573 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
3574 "domains don't match", goto error
);
3575 isl_space_free(aff_space
);
3578 isl_space_free(aff_space
);
3584 #define NO_INTERSECT_DOMAIN
3587 #include <isl_multi_templ.c>
3590 #undef NO_INTERSECT_DOMAIN
3592 /* Remove any internal structure of the domain of "ma".
3593 * If there is any such internal structure in the input,
3594 * then the name of the corresponding space is also removed.
3596 __isl_give isl_multi_aff
*isl_multi_aff_flatten_domain(
3597 __isl_take isl_multi_aff
*ma
)
3604 if (!ma
->space
->nested
[0])
3607 space
= isl_multi_aff_get_space(ma
);
3608 space
= isl_space_flatten_domain(space
);
3609 ma
= isl_multi_aff_reset_space(ma
, space
);
3614 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
3615 * of the space to its domain.
3617 __isl_give isl_multi_aff
*isl_multi_aff_domain_map(__isl_take isl_space
*space
)
3620 isl_local_space
*ls
;
3625 if (!isl_space_is_map(space
))
3626 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
3627 "not a map space", goto error
);
3629 n_in
= isl_space_dim(space
, isl_dim_in
);
3630 space
= isl_space_domain_map(space
);
3632 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
3634 isl_space_free(space
);
3638 space
= isl_space_domain(space
);
3639 ls
= isl_local_space_from_space(space
);
3640 for (i
= 0; i
< n_in
; ++i
) {
3643 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
3645 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
3647 isl_local_space_free(ls
);
3650 isl_space_free(space
);
3654 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
3655 * of the space to its range.
3657 __isl_give isl_multi_aff
*isl_multi_aff_range_map(__isl_take isl_space
*space
)
3660 isl_local_space
*ls
;
3665 if (!isl_space_is_map(space
))
3666 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
3667 "not a map space", goto error
);
3669 n_in
= isl_space_dim(space
, isl_dim_in
);
3670 n_out
= isl_space_dim(space
, isl_dim_out
);
3671 space
= isl_space_range_map(space
);
3673 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
3675 isl_space_free(space
);
3679 space
= isl_space_domain(space
);
3680 ls
= isl_local_space_from_space(space
);
3681 for (i
= 0; i
< n_out
; ++i
) {
3684 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
3685 isl_dim_set
, n_in
+ i
);
3686 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
3688 isl_local_space_free(ls
);
3691 isl_space_free(space
);
3695 /* Given the space of a set and a range of set dimensions,
3696 * construct an isl_multi_aff that projects out those dimensions.
3698 __isl_give isl_multi_aff
*isl_multi_aff_project_out_map(
3699 __isl_take isl_space
*space
, enum isl_dim_type type
,
3700 unsigned first
, unsigned n
)
3703 isl_local_space
*ls
;
3708 if (!isl_space_is_set(space
))
3709 isl_die(isl_space_get_ctx(space
), isl_error_unsupported
,
3710 "expecting set space", goto error
);
3711 if (type
!= isl_dim_set
)
3712 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
3713 "only set dimensions can be projected out", goto error
);
3715 dim
= isl_space_dim(space
, isl_dim_set
);
3716 if (first
+ n
> dim
)
3717 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
3718 "range out of bounds", goto error
);
3720 space
= isl_space_from_domain(space
);
3721 space
= isl_space_add_dims(space
, isl_dim_out
, dim
- n
);
3724 return isl_multi_aff_alloc(space
);
3726 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
3727 space
= isl_space_domain(space
);
3728 ls
= isl_local_space_from_space(space
);
3730 for (i
= 0; i
< first
; ++i
) {
3733 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
3735 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
3738 for (i
= 0; i
< dim
- (first
+ n
); ++i
) {
3741 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
3742 isl_dim_set
, first
+ n
+ i
);
3743 ma
= isl_multi_aff_set_aff(ma
, first
+ i
, aff
);
3746 isl_local_space_free(ls
);
3749 isl_space_free(space
);
3753 /* Given the space of a set and a range of set dimensions,
3754 * construct an isl_pw_multi_aff that projects out those dimensions.
3756 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_project_out_map(
3757 __isl_take isl_space
*space
, enum isl_dim_type type
,
3758 unsigned first
, unsigned n
)
3762 ma
= isl_multi_aff_project_out_map(space
, type
, first
, n
);
3763 return isl_pw_multi_aff_from_multi_aff(ma
);
3766 /* Create an isl_pw_multi_aff with the given isl_multi_aff on a universe
3769 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_multi_aff(
3770 __isl_take isl_multi_aff
*ma
)
3772 isl_set
*dom
= isl_set_universe(isl_multi_aff_get_domain_space(ma
));
3773 return isl_pw_multi_aff_alloc(dom
, ma
);
3776 /* Create a piecewise multi-affine expression in the given space that maps each
3777 * input dimension to the corresponding output dimension.
3779 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_identity(
3780 __isl_take isl_space
*space
)
3782 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_identity(space
));
3785 /* Add "ma2" to "ma1" and return the result.
3787 * The parameters of "ma1" and "ma2" are assumed to have been aligned.
3789 static __isl_give isl_multi_aff
*isl_multi_aff_add_aligned(
3790 __isl_take isl_multi_aff
*maff1
, __isl_take isl_multi_aff
*maff2
)
3792 return isl_multi_aff_bin_op(maff1
, maff2
, &isl_aff_add
);
3795 /* Add "ma2" to "ma1" and return the result.
3797 __isl_give isl_multi_aff
*isl_multi_aff_add(__isl_take isl_multi_aff
*ma1
,
3798 __isl_take isl_multi_aff
*ma2
)
3800 return isl_multi_aff_align_params_multi_multi_and(ma1
, ma2
,
3801 &isl_multi_aff_add_aligned
);
3804 /* Exploit the equalities in "eq" to simplify the affine expressions.
3806 static __isl_give isl_multi_aff
*isl_multi_aff_substitute_equalities(
3807 __isl_take isl_multi_aff
*maff
, __isl_take isl_basic_set
*eq
)
3811 maff
= isl_multi_aff_cow(maff
);
3815 for (i
= 0; i
< maff
->n
; ++i
) {
3816 maff
->p
[i
] = isl_aff_substitute_equalities(maff
->p
[i
],
3817 isl_basic_set_copy(eq
));
3822 isl_basic_set_free(eq
);
3825 isl_basic_set_free(eq
);
3826 isl_multi_aff_free(maff
);
3830 /* Given f, return floor(f).
3832 __isl_give isl_multi_aff
*isl_multi_aff_floor(__isl_take isl_multi_aff
*ma
)
3836 ma
= isl_multi_aff_cow(ma
);
3840 for (i
= 0; i
< ma
->n
; ++i
) {
3841 ma
->p
[i
] = isl_aff_floor(ma
->p
[i
]);
3843 return isl_multi_aff_free(ma
);
3849 __isl_give isl_multi_aff
*isl_multi_aff_scale(__isl_take isl_multi_aff
*maff
,
3854 maff
= isl_multi_aff_cow(maff
);
3858 for (i
= 0; i
< maff
->n
; ++i
) {
3859 maff
->p
[i
] = isl_aff_scale(maff
->p
[i
], f
);
3861 return isl_multi_aff_free(maff
);
3867 __isl_give isl_multi_aff
*isl_multi_aff_add_on_domain(__isl_keep isl_set
*dom
,
3868 __isl_take isl_multi_aff
*maff1
, __isl_take isl_multi_aff
*maff2
)
3870 maff1
= isl_multi_aff_add(maff1
, maff2
);
3871 maff1
= isl_multi_aff_gist(maff1
, isl_set_copy(dom
));
3875 int isl_multi_aff_is_empty(__isl_keep isl_multi_aff
*maff
)
3883 /* Return the set of domain elements where "ma1" is lexicographically
3884 * smaller than or equal to "ma2".
3886 __isl_give isl_set
*isl_multi_aff_lex_le_set(__isl_take isl_multi_aff
*ma1
,
3887 __isl_take isl_multi_aff
*ma2
)
3889 return isl_multi_aff_lex_ge_set(ma2
, ma1
);
3892 /* Return the set of domain elements where "ma1" is lexicographically
3893 * greater than or equal to "ma2".
3895 __isl_give isl_set
*isl_multi_aff_lex_ge_set(__isl_take isl_multi_aff
*ma1
,
3896 __isl_take isl_multi_aff
*ma2
)
3899 isl_map
*map1
, *map2
;
3902 map1
= isl_map_from_multi_aff(ma1
);
3903 map2
= isl_map_from_multi_aff(ma2
);
3904 map
= isl_map_range_product(map1
, map2
);
3905 space
= isl_space_range(isl_map_get_space(map
));
3906 space
= isl_space_domain(isl_space_unwrap(space
));
3907 ge
= isl_map_lex_ge(space
);
3908 map
= isl_map_intersect_range(map
, isl_map_wrap(ge
));
3910 return isl_map_domain(map
);
3914 #define PW isl_pw_multi_aff
3916 #define EL isl_multi_aff
3918 #define EL_IS_ZERO is_empty
3922 #define IS_ZERO is_empty
3925 #undef DEFAULT_IS_ZERO
3926 #define DEFAULT_IS_ZERO 0
3931 #define NO_INVOLVES_DIMS
3932 #define NO_INSERT_DIMS
3936 #include <isl_pw_templ.c>
3939 #define UNION isl_union_pw_multi_aff
3941 #define PART isl_pw_multi_aff
3943 #define PARTS pw_multi_aff
3944 #define ALIGN_DOMAIN
3948 #include <isl_union_templ.c>
3950 /* Given a function "cmp" that returns the set of elements where
3951 * "ma1" is "better" than "ma2", return the intersection of this
3952 * set with "dom1" and "dom2".
3954 static __isl_give isl_set
*shared_and_better(__isl_keep isl_set
*dom1
,
3955 __isl_keep isl_set
*dom2
, __isl_keep isl_multi_aff
*ma1
,
3956 __isl_keep isl_multi_aff
*ma2
,
3957 __isl_give isl_set
*(*cmp
)(__isl_take isl_multi_aff
*ma1
,
3958 __isl_take isl_multi_aff
*ma2
))
3964 common
= isl_set_intersect(isl_set_copy(dom1
), isl_set_copy(dom2
));
3965 is_empty
= isl_set_plain_is_empty(common
);
3966 if (is_empty
>= 0 && is_empty
)
3969 return isl_set_free(common
);
3970 better
= cmp(isl_multi_aff_copy(ma1
), isl_multi_aff_copy(ma2
));
3971 better
= isl_set_intersect(common
, better
);
3976 /* Given a function "cmp" that returns the set of elements where
3977 * "ma1" is "better" than "ma2", return a piecewise multi affine
3978 * expression defined on the union of the definition domains
3979 * of "pma1" and "pma2" that maps to the "best" of "pma1" and
3980 * "pma2" on each cell. If only one of the two input functions
3981 * is defined on a given cell, then it is considered the best.
3983 static __isl_give isl_pw_multi_aff
*pw_multi_aff_union_opt(
3984 __isl_take isl_pw_multi_aff
*pma1
,
3985 __isl_take isl_pw_multi_aff
*pma2
,
3986 __isl_give isl_set
*(*cmp
)(__isl_take isl_multi_aff
*ma1
,
3987 __isl_take isl_multi_aff
*ma2
))
3990 isl_pw_multi_aff
*res
= NULL
;
3992 isl_set
*set
= NULL
;
3997 ctx
= isl_space_get_ctx(pma1
->dim
);
3998 if (!isl_space_is_equal(pma1
->dim
, pma2
->dim
))
3999 isl_die(ctx
, isl_error_invalid
,
4000 "arguments should live in the same space", goto error
);
4002 if (isl_pw_multi_aff_is_empty(pma1
)) {
4003 isl_pw_multi_aff_free(pma1
);
4007 if (isl_pw_multi_aff_is_empty(pma2
)) {
4008 isl_pw_multi_aff_free(pma2
);
4012 n
= 2 * (pma1
->n
+ 1) * (pma2
->n
+ 1);
4013 res
= isl_pw_multi_aff_alloc_size(isl_space_copy(pma1
->dim
), n
);
4015 for (i
= 0; i
< pma1
->n
; ++i
) {
4016 set
= isl_set_copy(pma1
->p
[i
].set
);
4017 for (j
= 0; j
< pma2
->n
; ++j
) {
4021 better
= shared_and_better(pma2
->p
[j
].set
,
4022 pma1
->p
[i
].set
, pma2
->p
[j
].maff
,
4023 pma1
->p
[i
].maff
, cmp
);
4024 is_empty
= isl_set_plain_is_empty(better
);
4025 if (is_empty
< 0 || is_empty
) {
4026 isl_set_free(better
);
4031 set
= isl_set_subtract(set
, isl_set_copy(better
));
4033 res
= isl_pw_multi_aff_add_piece(res
, better
,
4034 isl_multi_aff_copy(pma2
->p
[j
].maff
));
4036 res
= isl_pw_multi_aff_add_piece(res
, set
,
4037 isl_multi_aff_copy(pma1
->p
[i
].maff
));
4040 for (j
= 0; j
< pma2
->n
; ++j
) {
4041 set
= isl_set_copy(pma2
->p
[j
].set
);
4042 for (i
= 0; i
< pma1
->n
; ++i
)
4043 set
= isl_set_subtract(set
,
4044 isl_set_copy(pma1
->p
[i
].set
));
4045 res
= isl_pw_multi_aff_add_piece(res
, set
,
4046 isl_multi_aff_copy(pma2
->p
[j
].maff
));
4049 isl_pw_multi_aff_free(pma1
);
4050 isl_pw_multi_aff_free(pma2
);
4054 isl_pw_multi_aff_free(pma1
);
4055 isl_pw_multi_aff_free(pma2
);
4057 return isl_pw_multi_aff_free(res
);
4060 static __isl_give isl_pw_multi_aff
*pw_multi_aff_union_lexmax(
4061 __isl_take isl_pw_multi_aff
*pma1
,
4062 __isl_take isl_pw_multi_aff
*pma2
)
4064 return pw_multi_aff_union_opt(pma1
, pma2
, &isl_multi_aff_lex_ge_set
);
4067 /* Given two piecewise multi affine expressions, return a piecewise
4068 * multi-affine expression defined on the union of the definition domains
4069 * of the inputs that is equal to the lexicographic maximum of the two
4070 * inputs on each cell. If only one of the two inputs is defined on
4071 * a given cell, then it is considered to be the maximum.
4073 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_union_lexmax(
4074 __isl_take isl_pw_multi_aff
*pma1
,
4075 __isl_take isl_pw_multi_aff
*pma2
)
4077 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
4078 &pw_multi_aff_union_lexmax
);
4081 static __isl_give isl_pw_multi_aff
*pw_multi_aff_union_lexmin(
4082 __isl_take isl_pw_multi_aff
*pma1
,
4083 __isl_take isl_pw_multi_aff
*pma2
)
4085 return pw_multi_aff_union_opt(pma1
, pma2
, &isl_multi_aff_lex_le_set
);
4088 /* Given two piecewise multi affine expressions, return a piecewise
4089 * multi-affine expression defined on the union of the definition domains
4090 * of the inputs that is equal to the lexicographic minimum of the two
4091 * inputs on each cell. If only one of the two inputs is defined on
4092 * a given cell, then it is considered to be the minimum.
4094 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_union_lexmin(
4095 __isl_take isl_pw_multi_aff
*pma1
,
4096 __isl_take isl_pw_multi_aff
*pma2
)
4098 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
4099 &pw_multi_aff_union_lexmin
);
4102 static __isl_give isl_pw_multi_aff
*pw_multi_aff_add(
4103 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4105 return isl_pw_multi_aff_on_shared_domain(pma1
, pma2
,
4106 &isl_multi_aff_add
);
4109 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_add(
4110 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4112 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
4116 static __isl_give isl_pw_multi_aff
*pw_multi_aff_sub(
4117 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4119 return isl_pw_multi_aff_on_shared_domain(pma1
, pma2
,
4120 &isl_multi_aff_sub
);
4123 /* Subtract "pma2" from "pma1" and return the result.
4125 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_sub(
4126 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4128 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
4132 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_union_add(
4133 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4135 return isl_pw_multi_aff_union_add_(pma1
, pma2
);
4138 /* Compute the sum of "upma1" and "upma2" on the union of their domains,
4139 * with the actual sum on the shared domain and
4140 * the defined expression on the symmetric difference of the domains.
4142 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_union_add(
4143 __isl_take isl_union_pw_multi_aff
*upma1
,
4144 __isl_take isl_union_pw_multi_aff
*upma2
)
4146 return isl_union_pw_multi_aff_union_add_(upma1
, upma2
);
4149 /* Given two piecewise multi-affine expressions A -> B and C -> D,
4150 * construct a piecewise multi-affine expression [A -> C] -> [B -> D].
4152 static __isl_give isl_pw_multi_aff
*pw_multi_aff_product(
4153 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4157 isl_pw_multi_aff
*res
;
4162 n
= pma1
->n
* pma2
->n
;
4163 space
= isl_space_product(isl_space_copy(pma1
->dim
),
4164 isl_space_copy(pma2
->dim
));
4165 res
= isl_pw_multi_aff_alloc_size(space
, n
);
4167 for (i
= 0; i
< pma1
->n
; ++i
) {
4168 for (j
= 0; j
< pma2
->n
; ++j
) {
4172 domain
= isl_set_product(isl_set_copy(pma1
->p
[i
].set
),
4173 isl_set_copy(pma2
->p
[j
].set
));
4174 ma
= isl_multi_aff_product(
4175 isl_multi_aff_copy(pma1
->p
[i
].maff
),
4176 isl_multi_aff_copy(pma2
->p
[j
].maff
));
4177 res
= isl_pw_multi_aff_add_piece(res
, domain
, ma
);
4181 isl_pw_multi_aff_free(pma1
);
4182 isl_pw_multi_aff_free(pma2
);
4185 isl_pw_multi_aff_free(pma1
);
4186 isl_pw_multi_aff_free(pma2
);
4190 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_product(
4191 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4193 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
4194 &pw_multi_aff_product
);
4197 /* Construct a map mapping the domain of the piecewise multi-affine expression
4198 * to its range, with each dimension in the range equated to the
4199 * corresponding affine expression on its cell.
4201 __isl_give isl_map
*isl_map_from_pw_multi_aff(__isl_take isl_pw_multi_aff
*pma
)
4209 map
= isl_map_empty(isl_pw_multi_aff_get_space(pma
));
4211 for (i
= 0; i
< pma
->n
; ++i
) {
4212 isl_multi_aff
*maff
;
4213 isl_basic_map
*bmap
;
4216 maff
= isl_multi_aff_copy(pma
->p
[i
].maff
);
4217 bmap
= isl_basic_map_from_multi_aff(maff
);
4218 map_i
= isl_map_from_basic_map(bmap
);
4219 map_i
= isl_map_intersect_domain(map_i
,
4220 isl_set_copy(pma
->p
[i
].set
));
4221 map
= isl_map_union_disjoint(map
, map_i
);
4224 isl_pw_multi_aff_free(pma
);
4228 __isl_give isl_set
*isl_set_from_pw_multi_aff(__isl_take isl_pw_multi_aff
*pma
)
4233 if (!isl_space_is_set(pma
->dim
))
4234 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
4235 "isl_pw_multi_aff cannot be converted into an isl_set",
4238 return isl_map_from_pw_multi_aff(pma
);
4240 isl_pw_multi_aff_free(pma
);
4244 /* Given a basic map with a single output dimension that is defined
4245 * in terms of the parameters and input dimensions using an equality,
4246 * extract an isl_aff that expresses the output dimension in terms
4247 * of the parameters and input dimensions.
4248 * Note that this expression may involve integer divisions defined
4249 * in terms of parameters and input dimensions.
4251 * This function shares some similarities with
4252 * isl_basic_map_has_defining_equality and isl_constraint_get_bound.
4254 static __isl_give isl_aff
*extract_isl_aff_from_basic_map(
4255 __isl_take isl_basic_map
*bmap
)
4260 isl_local_space
*ls
;
4265 if (isl_basic_map_dim(bmap
, isl_dim_out
) != 1)
4266 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
4267 "basic map should have a single output dimension",
4269 eq
= isl_basic_map_output_defining_equality(bmap
, 0);
4270 if (eq
>= bmap
->n_eq
)
4271 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
4272 "unable to find suitable equality", goto error
);
4273 ls
= isl_basic_map_get_local_space(bmap
);
4274 aff
= isl_aff_alloc(isl_local_space_domain(ls
));
4277 offset
= isl_basic_map_offset(bmap
, isl_dim_out
);
4278 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4279 if (isl_int_is_neg(bmap
->eq
[eq
][offset
])) {
4280 isl_seq_cpy(aff
->v
->el
+ 1, bmap
->eq
[eq
], offset
);
4281 isl_seq_cpy(aff
->v
->el
+ 1 + offset
, bmap
->eq
[eq
] + offset
+ 1,
4284 isl_seq_neg(aff
->v
->el
+ 1, bmap
->eq
[eq
], offset
);
4285 isl_seq_neg(aff
->v
->el
+ 1 + offset
, bmap
->eq
[eq
] + offset
+ 1,
4288 isl_int_abs(aff
->v
->el
[0], bmap
->eq
[eq
][offset
]);
4289 isl_basic_map_free(bmap
);
4291 aff
= isl_aff_remove_unused_divs(aff
);
4294 isl_basic_map_free(bmap
);
4298 /* Given a basic map where each output dimension is defined
4299 * in terms of the parameters and input dimensions using an equality,
4300 * extract an isl_multi_aff that expresses the output dimensions in terms
4301 * of the parameters and input dimensions.
4303 static __isl_give isl_multi_aff
*extract_isl_multi_aff_from_basic_map(
4304 __isl_take isl_basic_map
*bmap
)
4313 ma
= isl_multi_aff_alloc(isl_basic_map_get_space(bmap
));
4314 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4316 for (i
= 0; i
< n_out
; ++i
) {
4317 isl_basic_map
*bmap_i
;
4320 bmap_i
= isl_basic_map_copy(bmap
);
4321 bmap_i
= isl_basic_map_project_out(bmap_i
, isl_dim_out
,
4322 i
+ 1, n_out
- (1 + i
));
4323 bmap_i
= isl_basic_map_project_out(bmap_i
, isl_dim_out
, 0, i
);
4324 aff
= extract_isl_aff_from_basic_map(bmap_i
);
4325 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
4328 isl_basic_map_free(bmap
);
4333 /* Given a basic set where each set dimension is defined
4334 * in terms of the parameters using an equality,
4335 * extract an isl_multi_aff that expresses the set dimensions in terms
4336 * of the parameters.
4338 __isl_give isl_multi_aff
*isl_multi_aff_from_basic_set_equalities(
4339 __isl_take isl_basic_set
*bset
)
4341 return extract_isl_multi_aff_from_basic_map(bset
);
4344 /* Create an isl_pw_multi_aff that is equivalent to
4345 * isl_map_intersect_domain(isl_map_from_basic_map(bmap), domain).
4346 * The given basic map is such that each output dimension is defined
4347 * in terms of the parameters and input dimensions using an equality.
4349 * Since some applications expect the result of isl_pw_multi_aff_from_map
4350 * to only contain integer affine expressions, we compute the floor
4351 * of the expression before returning.
4353 static __isl_give isl_pw_multi_aff
*plain_pw_multi_aff_from_map(
4354 __isl_take isl_set
*domain
, __isl_take isl_basic_map
*bmap
)
4358 ma
= extract_isl_multi_aff_from_basic_map(bmap
);
4359 ma
= isl_multi_aff_floor(ma
);
4360 return isl_pw_multi_aff_alloc(domain
, ma
);
4363 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4364 * This obviously only works if the input "map" is single-valued.
4365 * If so, we compute the lexicographic minimum of the image in the form
4366 * of an isl_pw_multi_aff. Since the image is unique, it is equal
4367 * to its lexicographic minimum.
4368 * If the input is not single-valued, we produce an error.
4370 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_base(
4371 __isl_take isl_map
*map
)
4375 isl_pw_multi_aff
*pma
;
4377 sv
= isl_map_is_single_valued(map
);
4381 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
4382 "map is not single-valued", goto error
);
4383 map
= isl_map_make_disjoint(map
);
4387 pma
= isl_pw_multi_aff_empty(isl_map_get_space(map
));
4389 for (i
= 0; i
< map
->n
; ++i
) {
4390 isl_pw_multi_aff
*pma_i
;
4391 isl_basic_map
*bmap
;
4392 bmap
= isl_basic_map_copy(map
->p
[i
]);
4393 pma_i
= isl_basic_map_lexmin_pw_multi_aff(bmap
);
4394 pma
= isl_pw_multi_aff_add_disjoint(pma
, pma_i
);
4404 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
4405 * taking into account that the output dimension at position "d"
4406 * can be represented as
4408 * x = floor((e(...) + c1) / m)
4410 * given that constraint "i" is of the form
4412 * e(...) + c1 - m x >= 0
4415 * Let "map" be of the form
4419 * We construct a mapping
4421 * A -> [A -> x = floor(...)]
4423 * apply that to the map, obtaining
4425 * [A -> x = floor(...)] -> B
4427 * and equate dimension "d" to x.
4428 * We then compute a isl_pw_multi_aff representation of the resulting map
4429 * and plug in the mapping above.
4431 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_div(
4432 __isl_take isl_map
*map
, __isl_take isl_basic_map
*hull
, int d
, int i
)
4436 isl_local_space
*ls
;
4444 isl_pw_multi_aff
*pma
;
4447 is_set
= isl_map_is_set(map
);
4449 offset
= isl_basic_map_offset(hull
, isl_dim_out
);
4450 ctx
= isl_map_get_ctx(map
);
4451 space
= isl_space_domain(isl_map_get_space(map
));
4452 n_in
= isl_space_dim(space
, isl_dim_set
);
4453 n
= isl_space_dim(space
, isl_dim_all
);
4455 v
= isl_vec_alloc(ctx
, 1 + 1 + n
);
4457 isl_int_neg(v
->el
[0], hull
->ineq
[i
][offset
+ d
]);
4458 isl_seq_cpy(v
->el
+ 1, hull
->ineq
[i
], 1 + n
);
4460 isl_basic_map_free(hull
);
4462 ls
= isl_local_space_from_space(isl_space_copy(space
));
4463 aff
= isl_aff_alloc_vec(ls
, v
);
4464 aff
= isl_aff_floor(aff
);
4466 isl_space_free(space
);
4467 ma
= isl_multi_aff_from_aff(aff
);
4469 ma
= isl_multi_aff_identity(isl_space_map_from_set(space
));
4470 ma
= isl_multi_aff_range_product(ma
,
4471 isl_multi_aff_from_aff(aff
));
4474 insert
= isl_map_from_multi_aff(isl_multi_aff_copy(ma
));
4475 map
= isl_map_apply_domain(map
, insert
);
4476 map
= isl_map_equate(map
, isl_dim_in
, n_in
, isl_dim_out
, d
);
4477 pma
= isl_pw_multi_aff_from_map(map
);
4478 pma
= isl_pw_multi_aff_pullback_multi_aff(pma
, ma
);
4483 /* Is constraint "c" of the form
4485 * e(...) + c1 - m x >= 0
4489 * -e(...) + c2 + m x >= 0
4491 * where m > 1 and e only depends on parameters and input dimemnsions?
4493 * "offset" is the offset of the output dimensions
4494 * "pos" is the position of output dimension x.
4496 static int is_potential_div_constraint(isl_int
*c
, int offset
, int d
, int total
)
4498 if (isl_int_is_zero(c
[offset
+ d
]))
4500 if (isl_int_is_one(c
[offset
+ d
]))
4502 if (isl_int_is_negone(c
[offset
+ d
]))
4504 if (isl_seq_first_non_zero(c
+ offset
, d
) != -1)
4506 if (isl_seq_first_non_zero(c
+ offset
+ d
+ 1,
4507 total
- (offset
+ d
+ 1)) != -1)
4512 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4514 * As a special case, we first check if there is any pair of constraints,
4515 * shared by all the basic maps in "map" that force a given dimension
4516 * to be equal to the floor of some affine combination of the input dimensions.
4518 * In particular, if we can find two constraints
4520 * e(...) + c1 - m x >= 0 i.e., m x <= e(...) + c1
4524 * -e(...) + c2 + m x >= 0 i.e., m x >= e(...) - c2
4526 * where m > 1 and e only depends on parameters and input dimemnsions,
4529 * c1 + c2 < m i.e., -c2 >= c1 - (m - 1)
4531 * then we know that we can take
4533 * x = floor((e(...) + c1) / m)
4535 * without having to perform any computation.
4537 * Note that we know that
4541 * If c1 + c2 were 0, then we would have detected an equality during
4542 * simplification. If c1 + c2 were negative, then we would have detected
4545 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_check_div(
4546 __isl_take isl_map
*map
)
4552 isl_basic_map
*hull
;
4554 hull
= isl_map_unshifted_simple_hull(isl_map_copy(map
));
4559 dim
= isl_map_dim(map
, isl_dim_out
);
4560 offset
= isl_basic_map_offset(hull
, isl_dim_out
);
4561 total
= 1 + isl_basic_map_total_dim(hull
);
4563 for (d
= 0; d
< dim
; ++d
) {
4564 for (i
= 0; i
< n
; ++i
) {
4565 if (!is_potential_div_constraint(hull
->ineq
[i
],
4568 for (j
= i
+ 1; j
< n
; ++j
) {
4569 if (!isl_seq_is_neg(hull
->ineq
[i
] + 1,
4570 hull
->ineq
[j
] + 1, total
- 1))
4572 isl_int_add(sum
, hull
->ineq
[i
][0],
4574 if (isl_int_abs_lt(sum
,
4575 hull
->ineq
[i
][offset
+ d
]))
4582 if (isl_int_is_pos(hull
->ineq
[j
][offset
+ d
]))
4584 return pw_multi_aff_from_map_div(map
, hull
, d
, j
);
4588 isl_basic_map_free(hull
);
4589 return pw_multi_aff_from_map_base(map
);
4592 isl_basic_map_free(hull
);
4596 /* Given an affine expression
4598 * [A -> B] -> f(A,B)
4600 * construct an isl_multi_aff
4604 * such that dimension "d" in B' is set to "aff" and the remaining
4605 * dimensions are set equal to the corresponding dimensions in B.
4606 * "n_in" is the dimension of the space A.
4607 * "n_out" is the dimension of the space B.
4609 * If "is_set" is set, then the affine expression is of the form
4613 * and we construct an isl_multi_aff
4617 static __isl_give isl_multi_aff
*range_map(__isl_take isl_aff
*aff
, int d
,
4618 unsigned n_in
, unsigned n_out
, int is_set
)
4622 isl_space
*space
, *space2
;
4623 isl_local_space
*ls
;
4625 space
= isl_aff_get_domain_space(aff
);
4626 ls
= isl_local_space_from_space(isl_space_copy(space
));
4627 space2
= isl_space_copy(space
);
4629 space2
= isl_space_range(isl_space_unwrap(space2
));
4630 space
= isl_space_map_from_domain_and_range(space
, space2
);
4631 ma
= isl_multi_aff_alloc(space
);
4632 ma
= isl_multi_aff_set_aff(ma
, d
, aff
);
4634 for (i
= 0; i
< n_out
; ++i
) {
4637 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
4638 isl_dim_set
, n_in
+ i
);
4639 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
4642 isl_local_space_free(ls
);
4647 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
4648 * taking into account that the dimension at position "d" can be written as
4650 * x = m a + f(..) (1)
4652 * where m is equal to "gcd".
4653 * "i" is the index of the equality in "hull" that defines f(..).
4654 * In particular, the equality is of the form
4656 * f(..) - x + m g(existentials) = 0
4660 * -f(..) + x + m g(existentials) = 0
4662 * We basically plug (1) into "map", resulting in a map with "a"
4663 * in the range instead of "x". The corresponding isl_pw_multi_aff
4664 * defining "a" is then plugged back into (1) to obtain a definition fro "x".
4666 * Specifically, given the input map
4670 * We first wrap it into a set
4674 * and define (1) on top of the corresponding space, resulting in "aff".
4675 * We use this to create an isl_multi_aff that maps the output position "d"
4676 * from "a" to "x", leaving all other (intput and output) dimensions unchanged.
4677 * We plug this into the wrapped map, unwrap the result and compute the
4678 * corresponding isl_pw_multi_aff.
4679 * The result is an expression
4687 * so that we can plug that into "aff", after extending the latter to
4693 * If "map" is actually a set, then there is no "A" space, meaning
4694 * that we do not need to perform any wrapping, and that the result
4695 * of the recursive call is of the form
4699 * which is plugged into a mapping of the form
4703 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_stride(
4704 __isl_take isl_map
*map
, __isl_take isl_basic_map
*hull
, int d
, int i
,
4709 isl_local_space
*ls
;
4712 isl_pw_multi_aff
*pma
, *id
;
4718 is_set
= isl_map_is_set(map
);
4720 n_in
= isl_basic_map_dim(hull
, isl_dim_in
);
4721 n_out
= isl_basic_map_dim(hull
, isl_dim_out
);
4722 o_out
= isl_basic_map_offset(hull
, isl_dim_out
);
4727 set
= isl_map_wrap(map
);
4728 space
= isl_space_map_from_set(isl_set_get_space(set
));
4729 ma
= isl_multi_aff_identity(space
);
4730 ls
= isl_local_space_from_space(isl_set_get_space(set
));
4731 aff
= isl_aff_alloc(ls
);
4733 isl_int_set_si(aff
->v
->el
[0], 1);
4734 if (isl_int_is_one(hull
->eq
[i
][o_out
+ d
]))
4735 isl_seq_neg(aff
->v
->el
+ 1, hull
->eq
[i
],
4738 isl_seq_cpy(aff
->v
->el
+ 1, hull
->eq
[i
],
4740 isl_int_set(aff
->v
->el
[1 + o_out
+ d
], gcd
);
4742 ma
= isl_multi_aff_set_aff(ma
, n_in
+ d
, isl_aff_copy(aff
));
4743 set
= isl_set_preimage_multi_aff(set
, ma
);
4745 ma
= range_map(aff
, d
, n_in
, n_out
, is_set
);
4750 map
= isl_set_unwrap(set
);
4751 pma
= isl_pw_multi_aff_from_map(set
);
4754 space
= isl_pw_multi_aff_get_domain_space(pma
);
4755 space
= isl_space_map_from_set(space
);
4756 id
= isl_pw_multi_aff_identity(space
);
4757 pma
= isl_pw_multi_aff_range_product(id
, pma
);
4759 id
= isl_pw_multi_aff_from_multi_aff(ma
);
4760 pma
= isl_pw_multi_aff_pullback_pw_multi_aff(id
, pma
);
4762 isl_basic_map_free(hull
);
4766 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4768 * As a special case, we first check if all output dimensions are uniquely
4769 * defined in terms of the parameters and input dimensions over the entire
4770 * domain. If so, we extract the desired isl_pw_multi_aff directly
4771 * from the affine hull of "map" and its domain.
4773 * Otherwise, we check if any of the output dimensions is "strided".
4774 * That is, we check if can be written as
4778 * with m greater than 1, a some combination of existentiall quantified
4779 * variables and f and expression in the parameters and input dimensions.
4780 * If so, we remove the stride in pw_multi_aff_from_map_stride.
4782 * Otherwise, we continue with pw_multi_aff_from_map_check_div for a further
4785 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_map(__isl_take isl_map
*map
)
4789 isl_basic_map
*hull
;
4799 hull
= isl_map_affine_hull(isl_map_copy(map
));
4800 sv
= isl_basic_map_plain_is_single_valued(hull
);
4802 return plain_pw_multi_aff_from_map(isl_map_domain(map
), hull
);
4804 hull
= isl_basic_map_free(hull
);
4808 n_div
= isl_basic_map_dim(hull
, isl_dim_div
);
4809 o_div
= isl_basic_map_offset(hull
, isl_dim_div
);
4812 isl_basic_map_free(hull
);
4813 return pw_multi_aff_from_map_check_div(map
);
4818 n_out
= isl_basic_map_dim(hull
, isl_dim_out
);
4819 o_out
= isl_basic_map_offset(hull
, isl_dim_out
);
4821 for (i
= 0; i
< n_out
; ++i
) {
4822 for (j
= 0; j
< hull
->n_eq
; ++j
) {
4823 isl_int
*eq
= hull
->eq
[j
];
4824 isl_pw_multi_aff
*res
;
4826 if (!isl_int_is_one(eq
[o_out
+ i
]) &&
4827 !isl_int_is_negone(eq
[o_out
+ i
]))
4829 if (isl_seq_first_non_zero(eq
+ o_out
, i
) != -1)
4831 if (isl_seq_first_non_zero(eq
+ o_out
+ i
+ 1,
4832 n_out
- (i
+ 1)) != -1)
4834 isl_seq_gcd(eq
+ o_div
, n_div
, &gcd
);
4835 if (isl_int_is_zero(gcd
))
4837 if (isl_int_is_one(gcd
))
4840 res
= pw_multi_aff_from_map_stride(map
, hull
,
4848 isl_basic_map_free(hull
);
4849 return pw_multi_aff_from_map_check_div(map
);
4855 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_set(__isl_take isl_set
*set
)
4857 return isl_pw_multi_aff_from_map(set
);
4860 /* Convert "map" into an isl_pw_multi_aff (if possible) and
4863 static int pw_multi_aff_from_map(__isl_take isl_map
*map
, void *user
)
4865 isl_union_pw_multi_aff
**upma
= user
;
4866 isl_pw_multi_aff
*pma
;
4868 pma
= isl_pw_multi_aff_from_map(map
);
4869 *upma
= isl_union_pw_multi_aff_add_pw_multi_aff(*upma
, pma
);
4871 return *upma
? 0 : -1;
4874 /* Try and create an isl_union_pw_multi_aff that is equivalent
4875 * to the given isl_union_map.
4876 * The isl_union_map is required to be single-valued in each space.
4877 * Otherwise, an error is produced.
4879 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_union_map(
4880 __isl_take isl_union_map
*umap
)
4883 isl_union_pw_multi_aff
*upma
;
4885 space
= isl_union_map_get_space(umap
);
4886 upma
= isl_union_pw_multi_aff_empty(space
);
4887 if (isl_union_map_foreach_map(umap
, &pw_multi_aff_from_map
, &upma
) < 0)
4888 upma
= isl_union_pw_multi_aff_free(upma
);
4889 isl_union_map_free(umap
);
4894 /* Try and create an isl_union_pw_multi_aff that is equivalent
4895 * to the given isl_union_set.
4896 * The isl_union_set is required to be a singleton in each space.
4897 * Otherwise, an error is produced.
4899 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_union_set(
4900 __isl_take isl_union_set
*uset
)
4902 return isl_union_pw_multi_aff_from_union_map(uset
);
4905 /* Return the piecewise affine expression "set ? 1 : 0".
4907 __isl_give isl_pw_aff
*isl_set_indicator_function(__isl_take isl_set
*set
)
4910 isl_space
*space
= isl_set_get_space(set
);
4911 isl_local_space
*ls
= isl_local_space_from_space(space
);
4912 isl_aff
*zero
= isl_aff_zero_on_domain(isl_local_space_copy(ls
));
4913 isl_aff
*one
= isl_aff_zero_on_domain(ls
);
4915 one
= isl_aff_add_constant_si(one
, 1);
4916 pa
= isl_pw_aff_alloc(isl_set_copy(set
), one
);
4917 set
= isl_set_complement(set
);
4918 pa
= isl_pw_aff_add_disjoint(pa
, isl_pw_aff_alloc(set
, zero
));
4923 /* Plug in "subs" for dimension "type", "pos" of "aff".
4925 * Let i be the dimension to replace and let "subs" be of the form
4929 * and "aff" of the form
4935 * (a f + d g')/(m d)
4937 * where g' is the result of plugging in "subs" in each of the integer
4940 __isl_give isl_aff
*isl_aff_substitute(__isl_take isl_aff
*aff
,
4941 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
4946 aff
= isl_aff_cow(aff
);
4948 return isl_aff_free(aff
);
4950 ctx
= isl_aff_get_ctx(aff
);
4951 if (!isl_space_is_equal(aff
->ls
->dim
, subs
->ls
->dim
))
4952 isl_die(ctx
, isl_error_invalid
,
4953 "spaces don't match", return isl_aff_free(aff
));
4954 if (isl_local_space_dim(subs
->ls
, isl_dim_div
) != 0)
4955 isl_die(ctx
, isl_error_unsupported
,
4956 "cannot handle divs yet", return isl_aff_free(aff
));
4958 aff
->ls
= isl_local_space_substitute(aff
->ls
, type
, pos
, subs
);
4960 return isl_aff_free(aff
);
4962 aff
->v
= isl_vec_cow(aff
->v
);
4964 return isl_aff_free(aff
);
4966 pos
+= isl_local_space_offset(aff
->ls
, type
);
4969 isl_seq_substitute(aff
->v
->el
, pos
, subs
->v
->el
,
4970 aff
->v
->size
, subs
->v
->size
, v
);
4976 /* Plug in "subs" for dimension "type", "pos" in each of the affine
4977 * expressions in "maff".
4979 __isl_give isl_multi_aff
*isl_multi_aff_substitute(
4980 __isl_take isl_multi_aff
*maff
, enum isl_dim_type type
, unsigned pos
,
4981 __isl_keep isl_aff
*subs
)
4985 maff
= isl_multi_aff_cow(maff
);
4987 return isl_multi_aff_free(maff
);
4989 if (type
== isl_dim_in
)
4992 for (i
= 0; i
< maff
->n
; ++i
) {
4993 maff
->p
[i
] = isl_aff_substitute(maff
->p
[i
], type
, pos
, subs
);
4995 return isl_multi_aff_free(maff
);
5001 /* Plug in "subs" for dimension "type", "pos" of "pma".
5003 * pma is of the form
5007 * while subs is of the form
5009 * v' = B_j(v) -> S_j
5011 * Each pair i,j such that C_ij = A_i \cap B_i is non-empty
5012 * has a contribution in the result, in particular
5014 * C_ij(S_j) -> M_i(S_j)
5016 * Note that plugging in S_j in C_ij may also result in an empty set
5017 * and this contribution should simply be discarded.
5019 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_substitute(
5020 __isl_take isl_pw_multi_aff
*pma
, enum isl_dim_type type
, unsigned pos
,
5021 __isl_keep isl_pw_aff
*subs
)
5024 isl_pw_multi_aff
*res
;
5027 return isl_pw_multi_aff_free(pma
);
5029 n
= pma
->n
* subs
->n
;
5030 res
= isl_pw_multi_aff_alloc_size(isl_space_copy(pma
->dim
), n
);
5032 for (i
= 0; i
< pma
->n
; ++i
) {
5033 for (j
= 0; j
< subs
->n
; ++j
) {
5035 isl_multi_aff
*res_ij
;
5038 common
= isl_set_intersect(
5039 isl_set_copy(pma
->p
[i
].set
),
5040 isl_set_copy(subs
->p
[j
].set
));
5041 common
= isl_set_substitute(common
,
5042 type
, pos
, subs
->p
[j
].aff
);
5043 empty
= isl_set_plain_is_empty(common
);
5044 if (empty
< 0 || empty
) {
5045 isl_set_free(common
);
5051 res_ij
= isl_multi_aff_substitute(
5052 isl_multi_aff_copy(pma
->p
[i
].maff
),
5053 type
, pos
, subs
->p
[j
].aff
);
5055 res
= isl_pw_multi_aff_add_piece(res
, common
, res_ij
);
5059 isl_pw_multi_aff_free(pma
);
5062 isl_pw_multi_aff_free(pma
);
5063 isl_pw_multi_aff_free(res
);
5067 /* Compute the preimage of a range of dimensions in the affine expression "src"
5068 * under "ma" and put the result in "dst". The number of dimensions in "src"
5069 * that precede the range is given by "n_before". The number of dimensions
5070 * in the range is given by the number of output dimensions of "ma".
5071 * The number of dimensions that follow the range is given by "n_after".
5072 * If "has_denom" is set (to one),
5073 * then "src" and "dst" have an extra initial denominator.
5074 * "n_div_ma" is the number of existentials in "ma"
5075 * "n_div_bset" is the number of existentials in "src"
5076 * The resulting "dst" (which is assumed to have been allocated by
5077 * the caller) contains coefficients for both sets of existentials,
5078 * first those in "ma" and then those in "src".
5079 * f, c1, c2 and g are temporary objects that have been initialized
5082 * Let src represent the expression
5084 * (a(p) + f_u u + b v + f_w w + c(divs))/d
5086 * and let ma represent the expressions
5088 * v_i = (r_i(p) + s_i(y) + t_i(divs'))/m_i
5090 * We start out with the following expression for dst:
5092 * (a(p) + f_u u + 0 y + f_w w + 0 divs' + c(divs) + f \sum_i b_i v_i)/d
5094 * with the multiplication factor f initially equal to 1
5095 * and f \sum_i b_i v_i kept separately.
5096 * For each x_i that we substitute, we multiply the numerator
5097 * (and denominator) of dst by c_1 = m_i and add the numerator
5098 * of the x_i expression multiplied by c_2 = f b_i,
5099 * after removing the common factors of c_1 and c_2.
5100 * The multiplication factor f also needs to be multiplied by c_1
5101 * for the next x_j, j > i.
5103 void isl_seq_preimage(isl_int
*dst
, isl_int
*src
,
5104 __isl_keep isl_multi_aff
*ma
, int n_before
, int n_after
,
5105 int n_div_ma
, int n_div_bmap
,
5106 isl_int f
, isl_int c1
, isl_int c2
, isl_int g
, int has_denom
)
5109 int n_param
, n_in
, n_out
;
5112 n_param
= isl_multi_aff_dim(ma
, isl_dim_param
);
5113 n_in
= isl_multi_aff_dim(ma
, isl_dim_in
);
5114 n_out
= isl_multi_aff_dim(ma
, isl_dim_out
);
5116 isl_seq_cpy(dst
, src
, has_denom
+ 1 + n_param
+ n_before
);
5117 o_dst
= o_src
= has_denom
+ 1 + n_param
+ n_before
;
5118 isl_seq_clr(dst
+ o_dst
, n_in
);
5121 isl_seq_cpy(dst
+ o_dst
, src
+ o_src
, n_after
);
5124 isl_seq_clr(dst
+ o_dst
, n_div_ma
);
5126 isl_seq_cpy(dst
+ o_dst
, src
+ o_src
, n_div_bmap
);
5128 isl_int_set_si(f
, 1);
5130 for (i
= 0; i
< n_out
; ++i
) {
5131 int offset
= has_denom
+ 1 + n_param
+ n_before
+ i
;
5133 if (isl_int_is_zero(src
[offset
]))
5135 isl_int_set(c1
, ma
->p
[i
]->v
->el
[0]);
5136 isl_int_mul(c2
, f
, src
[offset
]);
5137 isl_int_gcd(g
, c1
, c2
);
5138 isl_int_divexact(c1
, c1
, g
);
5139 isl_int_divexact(c2
, c2
, g
);
5141 isl_int_mul(f
, f
, c1
);
5144 isl_seq_combine(dst
+ o_dst
, c1
, dst
+ o_dst
,
5145 c2
, ma
->p
[i
]->v
->el
+ o_src
, 1 + n_param
);
5146 o_dst
+= 1 + n_param
;
5147 o_src
+= 1 + n_param
;
5148 isl_seq_scale(dst
+ o_dst
, dst
+ o_dst
, c1
, n_before
);
5150 isl_seq_combine(dst
+ o_dst
, c1
, dst
+ o_dst
,
5151 c2
, ma
->p
[i
]->v
->el
+ o_src
, n_in
);
5154 isl_seq_scale(dst
+ o_dst
, dst
+ o_dst
, c1
, n_after
);
5156 isl_seq_combine(dst
+ o_dst
, c1
, dst
+ o_dst
,
5157 c2
, ma
->p
[i
]->v
->el
+ o_src
, n_div_ma
);
5160 isl_seq_scale(dst
+ o_dst
, dst
+ o_dst
, c1
, n_div_bmap
);
5162 isl_int_mul(dst
[0], dst
[0], c1
);
5166 /* Compute the pullback of "aff" by the function represented by "ma".
5167 * In other words, plug in "ma" in "aff". The result is an affine expression
5168 * defined over the domain space of "ma".
5170 * If "aff" is represented by
5172 * (a(p) + b x + c(divs))/d
5174 * and ma is represented by
5176 * x = D(p) + F(y) + G(divs')
5178 * then the result is
5180 * (a(p) + b D(p) + b F(y) + b G(divs') + c(divs))/d
5182 * The divs in the local space of the input are similarly adjusted
5183 * through a call to isl_local_space_preimage_multi_aff.
5185 __isl_give isl_aff
*isl_aff_pullback_multi_aff(__isl_take isl_aff
*aff
,
5186 __isl_take isl_multi_aff
*ma
)
5188 isl_aff
*res
= NULL
;
5189 isl_local_space
*ls
;
5190 int n_div_aff
, n_div_ma
;
5191 isl_int f
, c1
, c2
, g
;
5193 ma
= isl_multi_aff_align_divs(ma
);
5197 n_div_aff
= isl_aff_dim(aff
, isl_dim_div
);
5198 n_div_ma
= ma
->n
? isl_aff_dim(ma
->p
[0], isl_dim_div
) : 0;
5200 ls
= isl_aff_get_domain_local_space(aff
);
5201 ls
= isl_local_space_preimage_multi_aff(ls
, isl_multi_aff_copy(ma
));
5202 res
= isl_aff_alloc(ls
);
5211 isl_seq_preimage(res
->v
->el
, aff
->v
->el
, ma
, 0, 0, n_div_ma
, n_div_aff
,
5220 isl_multi_aff_free(ma
);
5221 res
= isl_aff_normalize(res
);
5225 isl_multi_aff_free(ma
);
5230 /* Compute the pullback of "aff1" by the function represented by "aff2".
5231 * In other words, plug in "aff2" in "aff1". The result is an affine expression
5232 * defined over the domain space of "aff1".
5234 * The domain of "aff1" should match the range of "aff2", which means
5235 * that it should be single-dimensional.
5237 __isl_give isl_aff
*isl_aff_pullback_aff(__isl_take isl_aff
*aff1
,
5238 __isl_take isl_aff
*aff2
)
5242 ma
= isl_multi_aff_from_aff(aff2
);
5243 return isl_aff_pullback_multi_aff(aff1
, ma
);
5246 /* Compute the pullback of "ma1" by the function represented by "ma2".
5247 * In other words, plug in "ma2" in "ma1".
5249 * The parameters of "ma1" and "ma2" are assumed to have been aligned.
5251 static __isl_give isl_multi_aff
*isl_multi_aff_pullback_multi_aff_aligned(
5252 __isl_take isl_multi_aff
*ma1
, __isl_take isl_multi_aff
*ma2
)
5255 isl_space
*space
= NULL
;
5257 ma2
= isl_multi_aff_align_divs(ma2
);
5258 ma1
= isl_multi_aff_cow(ma1
);
5262 space
= isl_space_join(isl_multi_aff_get_space(ma2
),
5263 isl_multi_aff_get_space(ma1
));
5265 for (i
= 0; i
< ma1
->n
; ++i
) {
5266 ma1
->p
[i
] = isl_aff_pullback_multi_aff(ma1
->p
[i
],
5267 isl_multi_aff_copy(ma2
));
5272 ma1
= isl_multi_aff_reset_space(ma1
, space
);
5273 isl_multi_aff_free(ma2
);
5276 isl_space_free(space
);
5277 isl_multi_aff_free(ma2
);
5278 isl_multi_aff_free(ma1
);
5282 /* Compute the pullback of "ma1" by the function represented by "ma2".
5283 * In other words, plug in "ma2" in "ma1".
5285 __isl_give isl_multi_aff
*isl_multi_aff_pullback_multi_aff(
5286 __isl_take isl_multi_aff
*ma1
, __isl_take isl_multi_aff
*ma2
)
5288 return isl_multi_aff_align_params_multi_multi_and(ma1
, ma2
,
5289 &isl_multi_aff_pullback_multi_aff_aligned
);
5292 /* Extend the local space of "dst" to include the divs
5293 * in the local space of "src".
5295 __isl_give isl_aff
*isl_aff_align_divs(__isl_take isl_aff
*dst
,
5296 __isl_keep isl_aff
*src
)
5304 return isl_aff_free(dst
);
5306 ctx
= isl_aff_get_ctx(src
);
5307 if (!isl_space_is_equal(src
->ls
->dim
, dst
->ls
->dim
))
5308 isl_die(ctx
, isl_error_invalid
,
5309 "spaces don't match", goto error
);
5311 if (src
->ls
->div
->n_row
== 0)
5314 exp1
= isl_alloc_array(ctx
, int, src
->ls
->div
->n_row
);
5315 exp2
= isl_alloc_array(ctx
, int, dst
->ls
->div
->n_row
);
5316 if (!exp1
|| (dst
->ls
->div
->n_row
&& !exp2
))
5319 div
= isl_merge_divs(src
->ls
->div
, dst
->ls
->div
, exp1
, exp2
);
5320 dst
= isl_aff_expand_divs(dst
, div
, exp2
);
5328 return isl_aff_free(dst
);
5331 /* Adjust the local spaces of the affine expressions in "maff"
5332 * such that they all have the save divs.
5334 __isl_give isl_multi_aff
*isl_multi_aff_align_divs(
5335 __isl_take isl_multi_aff
*maff
)
5343 maff
= isl_multi_aff_cow(maff
);
5347 for (i
= 1; i
< maff
->n
; ++i
)
5348 maff
->p
[0] = isl_aff_align_divs(maff
->p
[0], maff
->p
[i
]);
5349 for (i
= 1; i
< maff
->n
; ++i
) {
5350 maff
->p
[i
] = isl_aff_align_divs(maff
->p
[i
], maff
->p
[0]);
5352 return isl_multi_aff_free(maff
);
5358 __isl_give isl_aff
*isl_aff_lift(__isl_take isl_aff
*aff
)
5360 aff
= isl_aff_cow(aff
);
5364 aff
->ls
= isl_local_space_lift(aff
->ls
);
5366 return isl_aff_free(aff
);
5371 /* Lift "maff" to a space with extra dimensions such that the result
5372 * has no more existentially quantified variables.
5373 * If "ls" is not NULL, then *ls is assigned the local space that lies
5374 * at the basis of the lifting applied to "maff".
5376 __isl_give isl_multi_aff
*isl_multi_aff_lift(__isl_take isl_multi_aff
*maff
,
5377 __isl_give isl_local_space
**ls
)
5391 isl_space
*space
= isl_multi_aff_get_domain_space(maff
);
5392 *ls
= isl_local_space_from_space(space
);
5394 return isl_multi_aff_free(maff
);
5399 maff
= isl_multi_aff_cow(maff
);
5400 maff
= isl_multi_aff_align_divs(maff
);
5404 n_div
= isl_aff_dim(maff
->p
[0], isl_dim_div
);
5405 space
= isl_multi_aff_get_space(maff
);
5406 space
= isl_space_lift(isl_space_domain(space
), n_div
);
5407 space
= isl_space_extend_domain_with_range(space
,
5408 isl_multi_aff_get_space(maff
));
5410 return isl_multi_aff_free(maff
);
5411 isl_space_free(maff
->space
);
5412 maff
->space
= space
;
5415 *ls
= isl_aff_get_domain_local_space(maff
->p
[0]);
5417 return isl_multi_aff_free(maff
);
5420 for (i
= 0; i
< maff
->n
; ++i
) {
5421 maff
->p
[i
] = isl_aff_lift(maff
->p
[i
]);
5429 isl_local_space_free(*ls
);
5430 return isl_multi_aff_free(maff
);
5434 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma".
5436 __isl_give isl_pw_aff
*isl_pw_multi_aff_get_pw_aff(
5437 __isl_keep isl_pw_multi_aff
*pma
, int pos
)
5447 n_out
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
5448 if (pos
< 0 || pos
>= n_out
)
5449 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
5450 "index out of bounds", return NULL
);
5452 space
= isl_pw_multi_aff_get_space(pma
);
5453 space
= isl_space_drop_dims(space
, isl_dim_out
,
5454 pos
+ 1, n_out
- pos
- 1);
5455 space
= isl_space_drop_dims(space
, isl_dim_out
, 0, pos
);
5457 pa
= isl_pw_aff_alloc_size(space
, pma
->n
);
5458 for (i
= 0; i
< pma
->n
; ++i
) {
5460 aff
= isl_multi_aff_get_aff(pma
->p
[i
].maff
, pos
);
5461 pa
= isl_pw_aff_add_piece(pa
, isl_set_copy(pma
->p
[i
].set
), aff
);
5467 /* Return an isl_pw_multi_aff with the given "set" as domain and
5468 * an unnamed zero-dimensional range.
5470 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_domain(
5471 __isl_take isl_set
*set
)
5476 space
= isl_set_get_space(set
);
5477 space
= isl_space_from_domain(space
);
5478 ma
= isl_multi_aff_zero(space
);
5479 return isl_pw_multi_aff_alloc(set
, ma
);
5482 /* Add an isl_pw_multi_aff with the given "set" as domain and
5483 * an unnamed zero-dimensional range to *user.
5485 static int add_pw_multi_aff_from_domain(__isl_take isl_set
*set
, void *user
)
5487 isl_union_pw_multi_aff
**upma
= user
;
5488 isl_pw_multi_aff
*pma
;
5490 pma
= isl_pw_multi_aff_from_domain(set
);
5491 *upma
= isl_union_pw_multi_aff_add_pw_multi_aff(*upma
, pma
);
5496 /* Return an isl_union_pw_multi_aff with the given "uset" as domain and
5497 * an unnamed zero-dimensional range.
5499 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_domain(
5500 __isl_take isl_union_set
*uset
)
5503 isl_union_pw_multi_aff
*upma
;
5508 space
= isl_union_set_get_space(uset
);
5509 upma
= isl_union_pw_multi_aff_empty(space
);
5511 if (isl_union_set_foreach_set(uset
,
5512 &add_pw_multi_aff_from_domain
, &upma
) < 0)
5515 isl_union_set_free(uset
);
5518 isl_union_set_free(uset
);
5519 isl_union_pw_multi_aff_free(upma
);
5523 /* Convert "pma" to an isl_map and add it to *umap.
5525 static int map_from_pw_multi_aff(__isl_take isl_pw_multi_aff
*pma
, void *user
)
5527 isl_union_map
**umap
= user
;
5530 map
= isl_map_from_pw_multi_aff(pma
);
5531 *umap
= isl_union_map_add_map(*umap
, map
);
5536 /* Construct a union map mapping the domain of the union
5537 * piecewise multi-affine expression to its range, with each dimension
5538 * in the range equated to the corresponding affine expression on its cell.
5540 __isl_give isl_union_map
*isl_union_map_from_union_pw_multi_aff(
5541 __isl_take isl_union_pw_multi_aff
*upma
)
5544 isl_union_map
*umap
;
5549 space
= isl_union_pw_multi_aff_get_space(upma
);
5550 umap
= isl_union_map_empty(space
);
5552 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma
,
5553 &map_from_pw_multi_aff
, &umap
) < 0)
5556 isl_union_pw_multi_aff_free(upma
);
5559 isl_union_pw_multi_aff_free(upma
);
5560 isl_union_map_free(umap
);
5564 /* Local data for bin_entry and the callback "fn".
5566 struct isl_union_pw_multi_aff_bin_data
{
5567 isl_union_pw_multi_aff
*upma2
;
5568 isl_union_pw_multi_aff
*res
;
5569 isl_pw_multi_aff
*pma
;
5570 int (*fn
)(void **entry
, void *user
);
5573 /* Given an isl_pw_multi_aff from upma1, store it in data->pma
5574 * and call data->fn for each isl_pw_multi_aff in data->upma2.
5576 static int bin_entry(void **entry
, void *user
)
5578 struct isl_union_pw_multi_aff_bin_data
*data
= user
;
5579 isl_pw_multi_aff
*pma
= *entry
;
5582 if (isl_hash_table_foreach(data
->upma2
->space
->ctx
, &data
->upma2
->table
,
5583 data
->fn
, data
) < 0)
5589 /* Call "fn" on each pair of isl_pw_multi_affs in "upma1" and "upma2".
5590 * The isl_pw_multi_aff from upma1 is stored in data->pma (where data is
5591 * passed as user field) and the isl_pw_multi_aff from upma2 is available
5592 * as *entry. The callback should adjust data->res if desired.
5594 static __isl_give isl_union_pw_multi_aff
*bin_op(
5595 __isl_take isl_union_pw_multi_aff
*upma1
,
5596 __isl_take isl_union_pw_multi_aff
*upma2
,
5597 int (*fn
)(void **entry
, void *user
))
5600 struct isl_union_pw_multi_aff_bin_data data
= { NULL
, NULL
, NULL
, fn
};
5602 space
= isl_union_pw_multi_aff_get_space(upma2
);
5603 upma1
= isl_union_pw_multi_aff_align_params(upma1
, space
);
5604 space
= isl_union_pw_multi_aff_get_space(upma1
);
5605 upma2
= isl_union_pw_multi_aff_align_params(upma2
, space
);
5607 if (!upma1
|| !upma2
)
5611 data
.res
= isl_union_pw_multi_aff_alloc(isl_space_copy(upma1
->space
),
5613 if (isl_hash_table_foreach(upma1
->space
->ctx
, &upma1
->table
,
5614 &bin_entry
, &data
) < 0)
5617 isl_union_pw_multi_aff_free(upma1
);
5618 isl_union_pw_multi_aff_free(upma2
);
5621 isl_union_pw_multi_aff_free(upma1
);
5622 isl_union_pw_multi_aff_free(upma2
);
5623 isl_union_pw_multi_aff_free(data
.res
);
5627 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
5628 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
5630 static __isl_give isl_pw_multi_aff
*pw_multi_aff_range_product(
5631 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
5635 space
= isl_space_range_product(isl_pw_multi_aff_get_space(pma1
),
5636 isl_pw_multi_aff_get_space(pma2
));
5637 return isl_pw_multi_aff_on_shared_domain_in(pma1
, pma2
, space
,
5638 &isl_multi_aff_range_product
);
5641 /* Given two isl_pw_multi_affs A -> B and C -> D,
5642 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
5644 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_range_product(
5645 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
5647 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
5648 &pw_multi_aff_range_product
);
5651 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
5652 * construct an isl_pw_multi_aff (A * C) -> (B, D).
5654 static __isl_give isl_pw_multi_aff
*pw_multi_aff_flat_range_product(
5655 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
5659 space
= isl_space_range_product(isl_pw_multi_aff_get_space(pma1
),
5660 isl_pw_multi_aff_get_space(pma2
));
5661 space
= isl_space_flatten_range(space
);
5662 return isl_pw_multi_aff_on_shared_domain_in(pma1
, pma2
, space
,
5663 &isl_multi_aff_flat_range_product
);
5666 /* Given two isl_pw_multi_affs A -> B and C -> D,
5667 * construct an isl_pw_multi_aff (A * C) -> (B, D).
5669 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_flat_range_product(
5670 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
5672 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
5673 &pw_multi_aff_flat_range_product
);
5676 /* If data->pma and *entry have the same domain space, then compute
5677 * their flat range product and the result to data->res.
5679 static int flat_range_product_entry(void **entry
, void *user
)
5681 struct isl_union_pw_multi_aff_bin_data
*data
= user
;
5682 isl_pw_multi_aff
*pma2
= *entry
;
5684 if (!isl_space_tuple_is_equal(data
->pma
->dim
, isl_dim_in
,
5685 pma2
->dim
, isl_dim_in
))
5688 pma2
= isl_pw_multi_aff_flat_range_product(
5689 isl_pw_multi_aff_copy(data
->pma
),
5690 isl_pw_multi_aff_copy(pma2
));
5692 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma2
);
5697 /* Given two isl_union_pw_multi_affs A -> B and C -> D,
5698 * construct an isl_union_pw_multi_aff (A * C) -> (B, D).
5700 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_flat_range_product(
5701 __isl_take isl_union_pw_multi_aff
*upma1
,
5702 __isl_take isl_union_pw_multi_aff
*upma2
)
5704 return bin_op(upma1
, upma2
, &flat_range_product_entry
);
5707 /* Replace the affine expressions at position "pos" in "pma" by "pa".
5708 * The parameters are assumed to have been aligned.
5710 * The implementation essentially performs an isl_pw_*_on_shared_domain,
5711 * except that it works on two different isl_pw_* types.
5713 static __isl_give isl_pw_multi_aff
*pw_multi_aff_set_pw_aff(
5714 __isl_take isl_pw_multi_aff
*pma
, unsigned pos
,
5715 __isl_take isl_pw_aff
*pa
)
5718 isl_pw_multi_aff
*res
= NULL
;
5723 if (!isl_space_tuple_is_equal(pma
->dim
, isl_dim_in
,
5724 pa
->dim
, isl_dim_in
))
5725 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
5726 "domains don't match", goto error
);
5727 if (pos
>= isl_pw_multi_aff_dim(pma
, isl_dim_out
))
5728 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
5729 "index out of bounds", goto error
);
5732 res
= isl_pw_multi_aff_alloc_size(isl_pw_multi_aff_get_space(pma
), n
);
5734 for (i
= 0; i
< pma
->n
; ++i
) {
5735 for (j
= 0; j
< pa
->n
; ++j
) {
5737 isl_multi_aff
*res_ij
;
5740 common
= isl_set_intersect(isl_set_copy(pma
->p
[i
].set
),
5741 isl_set_copy(pa
->p
[j
].set
));
5742 empty
= isl_set_plain_is_empty(common
);
5743 if (empty
< 0 || empty
) {
5744 isl_set_free(common
);
5750 res_ij
= isl_multi_aff_set_aff(
5751 isl_multi_aff_copy(pma
->p
[i
].maff
), pos
,
5752 isl_aff_copy(pa
->p
[j
].aff
));
5753 res_ij
= isl_multi_aff_gist(res_ij
,
5754 isl_set_copy(common
));
5756 res
= isl_pw_multi_aff_add_piece(res
, common
, res_ij
);
5760 isl_pw_multi_aff_free(pma
);
5761 isl_pw_aff_free(pa
);
5764 isl_pw_multi_aff_free(pma
);
5765 isl_pw_aff_free(pa
);
5766 return isl_pw_multi_aff_free(res
);
5769 /* Replace the affine expressions at position "pos" in "pma" by "pa".
5771 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_set_pw_aff(
5772 __isl_take isl_pw_multi_aff
*pma
, unsigned pos
,
5773 __isl_take isl_pw_aff
*pa
)
5777 if (isl_space_match(pma
->dim
, isl_dim_param
, pa
->dim
, isl_dim_param
))
5778 return pw_multi_aff_set_pw_aff(pma
, pos
, pa
);
5779 if (!isl_space_has_named_params(pma
->dim
) ||
5780 !isl_space_has_named_params(pa
->dim
))
5781 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
5782 "unaligned unnamed parameters", goto error
);
5783 pma
= isl_pw_multi_aff_align_params(pma
, isl_pw_aff_get_space(pa
));
5784 pa
= isl_pw_aff_align_params(pa
, isl_pw_multi_aff_get_space(pma
));
5785 return pw_multi_aff_set_pw_aff(pma
, pos
, pa
);
5787 isl_pw_multi_aff_free(pma
);
5788 isl_pw_aff_free(pa
);
5792 /* Do the parameters of "pa" match those of "space"?
5794 int isl_pw_aff_matching_params(__isl_keep isl_pw_aff
*pa
,
5795 __isl_keep isl_space
*space
)
5797 isl_space
*pa_space
;
5803 pa_space
= isl_pw_aff_get_space(pa
);
5805 match
= isl_space_match(space
, isl_dim_param
, pa_space
, isl_dim_param
);
5807 isl_space_free(pa_space
);
5811 /* Check that the domain space of "pa" matches "space".
5813 * Return 0 on success and -1 on error.
5815 int isl_pw_aff_check_match_domain_space(__isl_keep isl_pw_aff
*pa
,
5816 __isl_keep isl_space
*space
)
5818 isl_space
*pa_space
;
5824 pa_space
= isl_pw_aff_get_space(pa
);
5826 match
= isl_space_match(space
, isl_dim_param
, pa_space
, isl_dim_param
);
5830 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
5831 "parameters don't match", goto error
);
5832 match
= isl_space_tuple_is_equal(space
, isl_dim_in
,
5833 pa_space
, isl_dim_in
);
5837 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
5838 "domains don't match", goto error
);
5839 isl_space_free(pa_space
);
5842 isl_space_free(pa_space
);
5849 #include <isl_multi_templ.c>
5851 /* Scale the elements of "pma" by the corresponding elements of "mv".
5853 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_scale_multi_val(
5854 __isl_take isl_pw_multi_aff
*pma
, __isl_take isl_multi_val
*mv
)
5858 pma
= isl_pw_multi_aff_cow(pma
);
5861 if (!isl_space_tuple_is_equal(pma
->dim
, isl_dim_out
,
5862 mv
->space
, isl_dim_set
))
5863 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
5864 "spaces don't match", goto error
);
5865 if (!isl_space_match(pma
->dim
, isl_dim_param
,
5866 mv
->space
, isl_dim_param
)) {
5867 pma
= isl_pw_multi_aff_align_params(pma
,
5868 isl_multi_val_get_space(mv
));
5869 mv
= isl_multi_val_align_params(mv
,
5870 isl_pw_multi_aff_get_space(pma
));
5875 for (i
= 0; i
< pma
->n
; ++i
) {
5876 pma
->p
[i
].maff
= isl_multi_aff_scale_multi_val(pma
->p
[i
].maff
,
5877 isl_multi_val_copy(mv
));
5878 if (!pma
->p
[i
].maff
)
5882 isl_multi_val_free(mv
);
5885 isl_multi_val_free(mv
);
5886 isl_pw_multi_aff_free(pma
);
5890 /* Internal data structure for isl_union_pw_multi_aff_scale_multi_val.
5891 * mv contains the mv argument.
5892 * res collects the results.
5894 struct isl_union_pw_multi_aff_scale_multi_val_data
{
5896 isl_union_pw_multi_aff
*res
;
5899 /* This function is called for each entry of an isl_union_pw_multi_aff.
5900 * If the space of the entry matches that of data->mv,
5901 * then apply isl_pw_multi_aff_scale_multi_val and add the result
5904 static int union_pw_multi_aff_scale_multi_val_entry(void **entry
, void *user
)
5906 struct isl_union_pw_multi_aff_scale_multi_val_data
*data
= user
;
5907 isl_pw_multi_aff
*pma
= *entry
;
5911 if (!isl_space_tuple_is_equal(pma
->dim
, isl_dim_out
,
5912 data
->mv
->space
, isl_dim_set
))
5915 pma
= isl_pw_multi_aff_copy(pma
);
5916 pma
= isl_pw_multi_aff_scale_multi_val(pma
,
5917 isl_multi_val_copy(data
->mv
));
5918 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma
);
5925 /* Scale the elements of "upma" by the corresponding elements of "mv",
5926 * for those entries that match the space of "mv".
5928 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_scale_multi_val(
5929 __isl_take isl_union_pw_multi_aff
*upma
, __isl_take isl_multi_val
*mv
)
5931 struct isl_union_pw_multi_aff_scale_multi_val_data data
;
5933 upma
= isl_union_pw_multi_aff_align_params(upma
,
5934 isl_multi_val_get_space(mv
));
5935 mv
= isl_multi_val_align_params(mv
,
5936 isl_union_pw_multi_aff_get_space(upma
));
5941 data
.res
= isl_union_pw_multi_aff_alloc(isl_space_copy(upma
->space
),
5943 if (isl_hash_table_foreach(upma
->space
->ctx
, &upma
->table
,
5944 &union_pw_multi_aff_scale_multi_val_entry
, &data
) < 0)
5947 isl_multi_val_free(mv
);
5948 isl_union_pw_multi_aff_free(upma
);
5951 isl_multi_val_free(mv
);
5952 isl_union_pw_multi_aff_free(upma
);
5956 /* Construct and return a piecewise multi affine expression
5957 * in the given space with value zero in each of the output dimensions and
5958 * a universe domain.
5960 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_zero(__isl_take isl_space
*space
)
5962 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_zero(space
));
5965 /* Construct and return a piecewise multi affine expression
5966 * that is equal to the given piecewise affine expression.
5968 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_pw_aff(
5969 __isl_take isl_pw_aff
*pa
)
5973 isl_pw_multi_aff
*pma
;
5978 space
= isl_pw_aff_get_space(pa
);
5979 pma
= isl_pw_multi_aff_alloc_size(space
, pa
->n
);
5981 for (i
= 0; i
< pa
->n
; ++i
) {
5985 set
= isl_set_copy(pa
->p
[i
].set
);
5986 ma
= isl_multi_aff_from_aff(isl_aff_copy(pa
->p
[i
].aff
));
5987 pma
= isl_pw_multi_aff_add_piece(pma
, set
, ma
);
5990 isl_pw_aff_free(pa
);
5994 /* Construct a set or map mapping the shared (parameter) domain
5995 * of the piecewise affine expressions to the range of "mpa"
5996 * with each dimension in the range equated to the
5997 * corresponding piecewise affine expression.
5999 static __isl_give isl_map
*map_from_multi_pw_aff(
6000 __isl_take isl_multi_pw_aff
*mpa
)
6009 if (isl_space_dim(mpa
->space
, isl_dim_out
) != mpa
->n
)
6010 isl_die(isl_multi_pw_aff_get_ctx(mpa
), isl_error_internal
,
6011 "invalid space", goto error
);
6013 space
= isl_multi_pw_aff_get_domain_space(mpa
);
6014 map
= isl_map_universe(isl_space_from_domain(space
));
6016 for (i
= 0; i
< mpa
->n
; ++i
) {
6020 pa
= isl_pw_aff_copy(mpa
->p
[i
]);
6021 map_i
= map_from_pw_aff(pa
);
6023 map
= isl_map_flat_range_product(map
, map_i
);
6026 map
= isl_map_reset_space(map
, isl_multi_pw_aff_get_space(mpa
));
6028 isl_multi_pw_aff_free(mpa
);
6031 isl_multi_pw_aff_free(mpa
);
6035 /* Construct a map mapping the shared domain
6036 * of the piecewise affine expressions to the range of "mpa"
6037 * with each dimension in the range equated to the
6038 * corresponding piecewise affine expression.
6040 __isl_give isl_map
*isl_map_from_multi_pw_aff(__isl_take isl_multi_pw_aff
*mpa
)
6044 if (isl_space_is_set(mpa
->space
))
6045 isl_die(isl_multi_pw_aff_get_ctx(mpa
), isl_error_internal
,
6046 "space of input is not a map", goto error
);
6048 return map_from_multi_pw_aff(mpa
);
6050 isl_multi_pw_aff_free(mpa
);
6054 /* Construct a set mapping the shared parameter domain
6055 * of the piecewise affine expressions to the space of "mpa"
6056 * with each dimension in the range equated to the
6057 * corresponding piecewise affine expression.
6059 __isl_give isl_set
*isl_set_from_multi_pw_aff(__isl_take isl_multi_pw_aff
*mpa
)
6063 if (!isl_space_is_set(mpa
->space
))
6064 isl_die(isl_multi_pw_aff_get_ctx(mpa
), isl_error_internal
,
6065 "space of input is not a set", goto error
);
6067 return map_from_multi_pw_aff(mpa
);
6069 isl_multi_pw_aff_free(mpa
);
6073 /* Construct and return a piecewise multi affine expression
6074 * that is equal to the given multi piecewise affine expression
6075 * on the shared domain of the piecewise affine expressions.
6077 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_multi_pw_aff(
6078 __isl_take isl_multi_pw_aff
*mpa
)
6083 isl_pw_multi_aff
*pma
;
6088 space
= isl_multi_pw_aff_get_space(mpa
);
6091 isl_multi_pw_aff_free(mpa
);
6092 return isl_pw_multi_aff_zero(space
);
6095 pa
= isl_multi_pw_aff_get_pw_aff(mpa
, 0);
6096 pma
= isl_pw_multi_aff_from_pw_aff(pa
);
6098 for (i
= 1; i
< mpa
->n
; ++i
) {
6099 isl_pw_multi_aff
*pma_i
;
6101 pa
= isl_multi_pw_aff_get_pw_aff(mpa
, i
);
6102 pma_i
= isl_pw_multi_aff_from_pw_aff(pa
);
6103 pma
= isl_pw_multi_aff_range_product(pma
, pma_i
);
6106 pma
= isl_pw_multi_aff_reset_space(pma
, space
);
6108 isl_multi_pw_aff_free(mpa
);
6112 /* Construct and return a multi piecewise affine expression
6113 * that is equal to the given multi affine expression.
6115 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_from_multi_aff(
6116 __isl_take isl_multi_aff
*ma
)
6119 isl_multi_pw_aff
*mpa
;
6124 n
= isl_multi_aff_dim(ma
, isl_dim_out
);
6125 mpa
= isl_multi_pw_aff_alloc(isl_multi_aff_get_space(ma
));
6127 for (i
= 0; i
< n
; ++i
) {
6130 pa
= isl_pw_aff_from_aff(isl_multi_aff_get_aff(ma
, i
));
6131 mpa
= isl_multi_pw_aff_set_pw_aff(mpa
, i
, pa
);
6134 isl_multi_aff_free(ma
);
6138 /* Construct and return a multi piecewise affine expression
6139 * that is equal to the given piecewise multi affine expression.
6141 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_from_pw_multi_aff(
6142 __isl_take isl_pw_multi_aff
*pma
)
6146 isl_multi_pw_aff
*mpa
;
6151 n
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
6152 space
= isl_pw_multi_aff_get_space(pma
);
6153 mpa
= isl_multi_pw_aff_alloc(space
);
6155 for (i
= 0; i
< n
; ++i
) {
6158 pa
= isl_pw_multi_aff_get_pw_aff(pma
, i
);
6159 mpa
= isl_multi_pw_aff_set_pw_aff(mpa
, i
, pa
);
6162 isl_pw_multi_aff_free(pma
);
6166 /* Do "pa1" and "pa2" represent the same function?
6168 * We first check if they are obviously equal.
6169 * If not, we convert them to maps and check if those are equal.
6171 int isl_pw_aff_is_equal(__isl_keep isl_pw_aff
*pa1
, __isl_keep isl_pw_aff
*pa2
)
6174 isl_map
*map1
, *map2
;
6179 equal
= isl_pw_aff_plain_is_equal(pa1
, pa2
);
6180 if (equal
< 0 || equal
)
6183 map1
= map_from_pw_aff(isl_pw_aff_copy(pa1
));
6184 map2
= map_from_pw_aff(isl_pw_aff_copy(pa2
));
6185 equal
= isl_map_is_equal(map1
, map2
);
6192 /* Do "mpa1" and "mpa2" represent the same function?
6194 * Note that we cannot convert the entire isl_multi_pw_aff
6195 * to a map because the domains of the piecewise affine expressions
6196 * may not be the same.
6198 int isl_multi_pw_aff_is_equal(__isl_keep isl_multi_pw_aff
*mpa1
,
6199 __isl_keep isl_multi_pw_aff
*mpa2
)
6207 if (!isl_space_match(mpa1
->space
, isl_dim_param
,
6208 mpa2
->space
, isl_dim_param
)) {
6209 if (!isl_space_has_named_params(mpa1
->space
))
6211 if (!isl_space_has_named_params(mpa2
->space
))
6213 mpa1
= isl_multi_pw_aff_copy(mpa1
);
6214 mpa2
= isl_multi_pw_aff_copy(mpa2
);
6215 mpa1
= isl_multi_pw_aff_align_params(mpa1
,
6216 isl_multi_pw_aff_get_space(mpa2
));
6217 mpa2
= isl_multi_pw_aff_align_params(mpa2
,
6218 isl_multi_pw_aff_get_space(mpa1
));
6219 equal
= isl_multi_pw_aff_is_equal(mpa1
, mpa2
);
6220 isl_multi_pw_aff_free(mpa1
);
6221 isl_multi_pw_aff_free(mpa2
);
6225 equal
= isl_space_is_equal(mpa1
->space
, mpa2
->space
);
6226 if (equal
< 0 || !equal
)
6229 for (i
= 0; i
< mpa1
->n
; ++i
) {
6230 equal
= isl_pw_aff_is_equal(mpa1
->p
[i
], mpa2
->p
[i
]);
6231 if (equal
< 0 || !equal
)
6238 /* Coalesce the elements of "mpa".
6240 * Note that such coalescing does not change the meaning of "mpa"
6241 * so there is no need to cow. We do need to be careful not to
6242 * destroy any other copies of "mpa" in case of failure.
6244 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_coalesce(
6245 __isl_take isl_multi_pw_aff
*mpa
)
6252 for (i
= 0; i
< mpa
->n
; ++i
) {
6253 isl_pw_aff
*pa
= isl_pw_aff_copy(mpa
->p
[i
]);
6254 pa
= isl_pw_aff_coalesce(pa
);
6256 return isl_multi_pw_aff_free(mpa
);
6257 isl_pw_aff_free(mpa
->p
[i
]);
6264 /* Compute the pullback of "mpa" by the function represented by "ma".
6265 * In other words, plug in "ma" in "mpa".
6267 * The parameters of "mpa" and "ma" are assumed to have been aligned.
6269 static __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_pullback_multi_aff_aligned(
6270 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_multi_aff
*ma
)
6273 isl_space
*space
= NULL
;
6275 mpa
= isl_multi_pw_aff_cow(mpa
);
6279 space
= isl_space_join(isl_multi_aff_get_space(ma
),
6280 isl_multi_pw_aff_get_space(mpa
));
6284 for (i
= 0; i
< mpa
->n
; ++i
) {
6285 mpa
->p
[i
] = isl_pw_aff_pullback_multi_aff(mpa
->p
[i
],
6286 isl_multi_aff_copy(ma
));
6291 isl_multi_aff_free(ma
);
6292 isl_space_free(mpa
->space
);
6296 isl_space_free(space
);
6297 isl_multi_pw_aff_free(mpa
);
6298 isl_multi_aff_free(ma
);
6302 /* Compute the pullback of "mpa" by the function represented by "ma".
6303 * In other words, plug in "ma" in "mpa".
6305 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_pullback_multi_aff(
6306 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_multi_aff
*ma
)
6310 if (isl_space_match(mpa
->space
, isl_dim_param
,
6311 ma
->space
, isl_dim_param
))
6312 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa
, ma
);
6313 mpa
= isl_multi_pw_aff_align_params(mpa
, isl_multi_aff_get_space(ma
));
6314 ma
= isl_multi_aff_align_params(ma
, isl_multi_pw_aff_get_space(mpa
));
6315 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa
, ma
);
6317 isl_multi_pw_aff_free(mpa
);
6318 isl_multi_aff_free(ma
);
6322 /* Compute the pullback of "mpa" by the function represented by "pma".
6323 * In other words, plug in "pma" in "mpa".
6325 * The parameters of "mpa" and "mpa" are assumed to have been aligned.
6327 static __isl_give isl_multi_pw_aff
*
6328 isl_multi_pw_aff_pullback_pw_multi_aff_aligned(
6329 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_pw_multi_aff
*pma
)
6332 isl_space
*space
= NULL
;
6334 mpa
= isl_multi_pw_aff_cow(mpa
);
6338 space
= isl_space_join(isl_pw_multi_aff_get_space(pma
),
6339 isl_multi_pw_aff_get_space(mpa
));
6341 for (i
= 0; i
< mpa
->n
; ++i
) {
6342 mpa
->p
[i
] = isl_pw_aff_pullback_pw_multi_aff_aligned(mpa
->p
[i
],
6343 isl_pw_multi_aff_copy(pma
));
6348 isl_pw_multi_aff_free(pma
);
6349 isl_space_free(mpa
->space
);
6353 isl_space_free(space
);
6354 isl_multi_pw_aff_free(mpa
);
6355 isl_pw_multi_aff_free(pma
);
6359 /* Compute the pullback of "mpa" by the function represented by "pma".
6360 * In other words, plug in "pma" in "mpa".
6362 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_pullback_pw_multi_aff(
6363 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_pw_multi_aff
*pma
)
6367 if (isl_space_match(mpa
->space
, isl_dim_param
, pma
->dim
, isl_dim_param
))
6368 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa
, pma
);
6369 mpa
= isl_multi_pw_aff_align_params(mpa
,
6370 isl_pw_multi_aff_get_space(pma
));
6371 pma
= isl_pw_multi_aff_align_params(pma
,
6372 isl_multi_pw_aff_get_space(mpa
));
6373 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa
, pma
);
6375 isl_multi_pw_aff_free(mpa
);
6376 isl_pw_multi_aff_free(pma
);
6380 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
6381 * with the domain of "aff". The domain of the result is the same
6383 * "mpa" and "aff" are assumed to have been aligned.
6385 * We first extract the parametric constant from "aff", defined
6386 * over the correct domain.
6387 * Then we add the appropriate combinations of the members of "mpa".
6388 * Finally, we add the integer divisions through recursive calls.
6390 static __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_aff_aligned(
6391 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_aff
*aff
)
6393 int i
, n_param
, n_in
, n_div
;
6399 n_param
= isl_aff_dim(aff
, isl_dim_param
);
6400 n_in
= isl_aff_dim(aff
, isl_dim_in
);
6401 n_div
= isl_aff_dim(aff
, isl_dim_div
);
6403 space
= isl_space_domain(isl_multi_pw_aff_get_space(mpa
));
6404 tmp
= isl_aff_copy(aff
);
6405 tmp
= isl_aff_drop_dims(tmp
, isl_dim_div
, 0, n_div
);
6406 tmp
= isl_aff_drop_dims(tmp
, isl_dim_in
, 0, n_in
);
6407 tmp
= isl_aff_add_dims(tmp
, isl_dim_in
,
6408 isl_space_dim(space
, isl_dim_set
));
6409 tmp
= isl_aff_reset_domain_space(tmp
, space
);
6410 pa
= isl_pw_aff_from_aff(tmp
);
6412 for (i
= 0; i
< n_in
; ++i
) {
6415 if (!isl_aff_involves_dims(aff
, isl_dim_in
, i
, 1))
6417 v
= isl_aff_get_coefficient_val(aff
, isl_dim_in
, i
);
6418 pa_i
= isl_multi_pw_aff_get_pw_aff(mpa
, i
);
6419 pa_i
= isl_pw_aff_scale_val(pa_i
, v
);
6420 pa
= isl_pw_aff_add(pa
, pa_i
);
6423 for (i
= 0; i
< n_div
; ++i
) {
6427 if (!isl_aff_involves_dims(aff
, isl_dim_div
, i
, 1))
6429 div
= isl_aff_get_div(aff
, i
);
6430 pa_i
= isl_multi_pw_aff_apply_aff_aligned(
6431 isl_multi_pw_aff_copy(mpa
), div
);
6432 pa_i
= isl_pw_aff_floor(pa_i
);
6433 v
= isl_aff_get_coefficient_val(aff
, isl_dim_div
, i
);
6434 pa_i
= isl_pw_aff_scale_val(pa_i
, v
);
6435 pa
= isl_pw_aff_add(pa
, pa_i
);
6438 isl_multi_pw_aff_free(mpa
);
6444 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
6445 * with the domain of "aff". The domain of the result is the same
6448 __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_aff(
6449 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_aff
*aff
)
6453 if (isl_space_match(aff
->ls
->dim
, isl_dim_param
,
6454 mpa
->space
, isl_dim_param
))
6455 return isl_multi_pw_aff_apply_aff_aligned(mpa
, aff
);
6457 aff
= isl_aff_align_params(aff
, isl_multi_pw_aff_get_space(mpa
));
6458 mpa
= isl_multi_pw_aff_align_params(mpa
, isl_aff_get_space(aff
));
6460 return isl_multi_pw_aff_apply_aff_aligned(mpa
, aff
);
6463 isl_multi_pw_aff_free(mpa
);
6467 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
6468 * with the domain of "pa". The domain of the result is the same
6470 * "mpa" and "pa" are assumed to have been aligned.
6472 * We consider each piece in turn. Note that the domains of the
6473 * pieces are assumed to be disjoint and they remain disjoint
6474 * after taking the preimage (over the same function).
6476 static __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_pw_aff_aligned(
6477 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_pw_aff
*pa
)
6486 space
= isl_space_join(isl_multi_pw_aff_get_space(mpa
),
6487 isl_pw_aff_get_space(pa
));
6488 res
= isl_pw_aff_empty(space
);
6490 for (i
= 0; i
< pa
->n
; ++i
) {
6494 pa_i
= isl_multi_pw_aff_apply_aff_aligned(
6495 isl_multi_pw_aff_copy(mpa
),
6496 isl_aff_copy(pa
->p
[i
].aff
));
6497 domain
= isl_set_copy(pa
->p
[i
].set
);
6498 domain
= isl_set_preimage_multi_pw_aff(domain
,
6499 isl_multi_pw_aff_copy(mpa
));
6500 pa_i
= isl_pw_aff_intersect_domain(pa_i
, domain
);
6501 res
= isl_pw_aff_add_disjoint(res
, pa_i
);
6504 isl_pw_aff_free(pa
);
6505 isl_multi_pw_aff_free(mpa
);
6508 isl_pw_aff_free(pa
);
6509 isl_multi_pw_aff_free(mpa
);
6513 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
6514 * with the domain of "pa". The domain of the result is the same
6517 __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_pw_aff(
6518 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_pw_aff
*pa
)
6522 if (isl_space_match(pa
->dim
, isl_dim_param
, mpa
->space
, isl_dim_param
))
6523 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa
, pa
);
6525 pa
= isl_pw_aff_align_params(pa
, isl_multi_pw_aff_get_space(mpa
));
6526 mpa
= isl_multi_pw_aff_align_params(mpa
, isl_pw_aff_get_space(pa
));
6528 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa
, pa
);
6530 isl_pw_aff_free(pa
);
6531 isl_multi_pw_aff_free(mpa
);
6535 /* Compute the pullback of "pa" by the function represented by "mpa".
6536 * In other words, plug in "mpa" in "pa".
6537 * "pa" and "mpa" are assumed to have been aligned.
6539 * The pullback is computed by applying "pa" to "mpa".
6541 static __isl_give isl_pw_aff
*isl_pw_aff_pullback_multi_pw_aff_aligned(
6542 __isl_take isl_pw_aff
*pa
, __isl_take isl_multi_pw_aff
*mpa
)
6544 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa
, pa
);
6547 /* Compute the pullback of "pa" by the function represented by "mpa".
6548 * In other words, plug in "mpa" in "pa".
6550 * The pullback is computed by applying "pa" to "mpa".
6552 __isl_give isl_pw_aff
*isl_pw_aff_pullback_multi_pw_aff(
6553 __isl_take isl_pw_aff
*pa
, __isl_take isl_multi_pw_aff
*mpa
)
6555 return isl_multi_pw_aff_apply_pw_aff(mpa
, pa
);
6558 /* Compute the pullback of "mpa1" by the function represented by "mpa2".
6559 * In other words, plug in "mpa2" in "mpa1".
6561 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
6563 * We pullback each member of "mpa1" in turn.
6565 static __isl_give isl_multi_pw_aff
*
6566 isl_multi_pw_aff_pullback_multi_pw_aff_aligned(
6567 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
)
6570 isl_space
*space
= NULL
;
6572 mpa1
= isl_multi_pw_aff_cow(mpa1
);
6576 space
= isl_space_join(isl_multi_pw_aff_get_space(mpa2
),
6577 isl_multi_pw_aff_get_space(mpa1
));
6579 for (i
= 0; i
< mpa1
->n
; ++i
) {
6580 mpa1
->p
[i
] = isl_pw_aff_pullback_multi_pw_aff_aligned(
6581 mpa1
->p
[i
], isl_multi_pw_aff_copy(mpa2
));
6586 mpa1
= isl_multi_pw_aff_reset_space(mpa1
, space
);
6588 isl_multi_pw_aff_free(mpa2
);
6591 isl_space_free(space
);
6592 isl_multi_pw_aff_free(mpa1
);
6593 isl_multi_pw_aff_free(mpa2
);
6597 /* Compute the pullback of "mpa1" by the function represented by "mpa2".
6598 * In other words, plug in "mpa2" in "mpa1".
6600 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_pullback_multi_pw_aff(
6601 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
)
6603 return isl_multi_pw_aff_align_params_multi_multi_and(mpa1
, mpa2
,
6604 &isl_multi_pw_aff_pullback_multi_pw_aff_aligned
);
6607 /* Compare two isl_affs.
6609 * Return -1 if "aff1" is "smaller" than "aff2", 1 if "aff1" is "greater"
6610 * than "aff2" and 0 if they are equal.
6612 * The order is fairly arbitrary. We do consider expressions that only involve
6613 * earlier dimensions as "smaller".
6615 int isl_aff_plain_cmp(__isl_keep isl_aff
*aff1
, __isl_keep isl_aff
*aff2
)
6628 cmp
= isl_local_space_cmp(aff1
->ls
, aff2
->ls
);
6632 last1
= isl_seq_last_non_zero(aff1
->v
->el
+ 1, aff1
->v
->size
- 1);
6633 last2
= isl_seq_last_non_zero(aff2
->v
->el
+ 1, aff1
->v
->size
- 1);
6635 return last1
- last2
;
6637 return isl_seq_cmp(aff1
->v
->el
, aff2
->v
->el
, aff1
->v
->size
);
6640 /* Compare two isl_pw_affs.
6642 * Return -1 if "pa1" is "smaller" than "pa2", 1 if "pa1" is "greater"
6643 * than "pa2" and 0 if they are equal.
6645 * The order is fairly arbitrary. We do consider expressions that only involve
6646 * earlier dimensions as "smaller".
6648 int isl_pw_aff_plain_cmp(__isl_keep isl_pw_aff
*pa1
,
6649 __isl_keep isl_pw_aff
*pa2
)
6662 cmp
= isl_space_cmp(pa1
->dim
, pa2
->dim
);
6666 if (pa1
->n
!= pa2
->n
)
6667 return pa1
->n
- pa2
->n
;
6669 for (i
= 0; i
< pa1
->n
; ++i
) {
6670 cmp
= isl_set_plain_cmp(pa1
->p
[i
].set
, pa2
->p
[i
].set
);
6673 cmp
= isl_aff_plain_cmp(pa1
->p
[i
].aff
, pa2
->p
[i
].aff
);
6681 /* Return a piecewise affine expression that is equal to "v" on "domain".
6683 __isl_give isl_pw_aff
*isl_pw_aff_val_on_domain(__isl_take isl_set
*domain
,
6684 __isl_take isl_val
*v
)
6687 isl_local_space
*ls
;
6690 space
= isl_set_get_space(domain
);
6691 ls
= isl_local_space_from_space(space
);
6692 aff
= isl_aff_val_on_domain(ls
, v
);
6694 return isl_pw_aff_alloc(domain
, aff
);
6697 /* Return a multi affine expression that is equal to "mv" on domain
6700 __isl_give isl_multi_aff
*isl_multi_aff_multi_val_on_space(
6701 __isl_take isl_space
*space
, __isl_take isl_multi_val
*mv
)
6705 isl_local_space
*ls
;
6711 n
= isl_multi_val_dim(mv
, isl_dim_set
);
6712 space2
= isl_multi_val_get_space(mv
);
6713 space2
= isl_space_align_params(space2
, isl_space_copy(space
));
6714 space
= isl_space_align_params(space
, isl_space_copy(space2
));
6715 space
= isl_space_map_from_domain_and_range(space
, space2
);
6716 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
6717 ls
= isl_local_space_from_space(isl_space_domain(space
));
6718 for (i
= 0; i
< n
; ++i
) {
6722 v
= isl_multi_val_get_val(mv
, i
);
6723 aff
= isl_aff_val_on_domain(isl_local_space_copy(ls
), v
);
6724 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
6726 isl_local_space_free(ls
);
6728 isl_multi_val_free(mv
);
6731 isl_space_free(space
);
6732 isl_multi_val_free(mv
);
6736 /* Return a piecewise multi-affine expression
6737 * that is equal to "mv" on "domain".
6739 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_multi_val_on_domain(
6740 __isl_take isl_set
*domain
, __isl_take isl_multi_val
*mv
)
6745 space
= isl_set_get_space(domain
);
6746 ma
= isl_multi_aff_multi_val_on_space(space
, mv
);
6748 return isl_pw_multi_aff_alloc(domain
, ma
);
6751 /* Internal data structure for isl_union_pw_multi_aff_multi_val_on_domain.
6752 * mv is the value that should be attained on each domain set
6753 * res collects the results
6755 struct isl_union_pw_multi_aff_multi_val_on_domain_data
{
6757 isl_union_pw_multi_aff
*res
;
6760 /* Create an isl_pw_multi_aff equal to data->mv on "domain"
6761 * and add it to data->res.
6763 static int pw_multi_aff_multi_val_on_domain(__isl_take isl_set
*domain
,
6766 struct isl_union_pw_multi_aff_multi_val_on_domain_data
*data
= user
;
6767 isl_pw_multi_aff
*pma
;
6770 mv
= isl_multi_val_copy(data
->mv
);
6771 pma
= isl_pw_multi_aff_multi_val_on_domain(domain
, mv
);
6772 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma
);
6774 return data
->res
? 0 : -1;
6777 /* Return a union piecewise multi-affine expression
6778 * that is equal to "mv" on "domain".
6780 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_multi_val_on_domain(
6781 __isl_take isl_union_set
*domain
, __isl_take isl_multi_val
*mv
)
6783 struct isl_union_pw_multi_aff_multi_val_on_domain_data data
;
6786 space
= isl_union_set_get_space(domain
);
6787 data
.res
= isl_union_pw_multi_aff_empty(space
);
6789 if (isl_union_set_foreach_set(domain
,
6790 &pw_multi_aff_multi_val_on_domain
, &data
) < 0)
6791 data
.res
= isl_union_pw_multi_aff_free(data
.res
);
6792 isl_union_set_free(domain
);
6793 isl_multi_val_free(mv
);