2 * Copyright 2011 INRIA Saclay
3 * Copyright 2011 Sven Verdoolaege
4 * Copyright 2012-2014 Ecole Normale Superieure
5 * Copyright 2014 INRIA Rocquencourt
7 * Use of this software is governed by the MIT license
9 * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
10 * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
12 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
13 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
14 * B.P. 105 - 78153 Le Chesnay, France
17 #include <isl_ctx_private.h>
18 #include <isl_map_private.h>
19 #include <isl_union_map_private.h>
20 #include <isl_aff_private.h>
21 #include <isl_space_private.h>
22 #include <isl_local_space_private.h>
23 #include <isl_vec_private.h>
24 #include <isl_mat_private.h>
26 #include <isl/constraint.h>
29 #include <isl_val_private.h>
30 #include <isl_point_private.h>
31 #include <isl_config.h>
36 #include <isl_list_templ.c>
41 #include <isl_list_templ.c>
44 #define BASE pw_multi_aff
46 #include <isl_list_templ.c>
49 #define BASE union_pw_aff
51 #include <isl_list_templ.c>
54 #define BASE union_pw_multi_aff
56 #include <isl_list_templ.c>
58 #include <set_from_map.c>
60 __isl_give isl_aff
*isl_aff_alloc_vec(__isl_take isl_local_space
*ls
,
61 __isl_take isl_vec
*v
)
68 aff
= isl_calloc_type(v
->ctx
, struct isl_aff
);
78 isl_local_space_free(ls
);
83 __isl_give isl_aff
*isl_aff_alloc(__isl_take isl_local_space
*ls
)
92 ctx
= isl_local_space_get_ctx(ls
);
93 if (!isl_local_space_divs_known(ls
))
94 isl_die(ctx
, isl_error_invalid
, "local space has unknown divs",
96 if (!isl_local_space_is_set(ls
))
97 isl_die(ctx
, isl_error_invalid
,
98 "domain of affine expression should be a set",
101 total
= isl_local_space_dim(ls
, isl_dim_all
);
102 v
= isl_vec_alloc(ctx
, 1 + 1 + total
);
103 return isl_aff_alloc_vec(ls
, v
);
105 isl_local_space_free(ls
);
109 __isl_give isl_aff
*isl_aff_zero_on_domain(__isl_take isl_local_space
*ls
)
113 aff
= isl_aff_alloc(ls
);
117 isl_int_set_si(aff
->v
->el
[0], 1);
118 isl_seq_clr(aff
->v
->el
+ 1, aff
->v
->size
- 1);
123 /* Return a piecewise affine expression defined on the specified domain
124 * that is equal to zero.
126 __isl_give isl_pw_aff
*isl_pw_aff_zero_on_domain(__isl_take isl_local_space
*ls
)
128 return isl_pw_aff_from_aff(isl_aff_zero_on_domain(ls
));
131 /* Return an affine expression defined on the specified domain
132 * that represents NaN.
134 __isl_give isl_aff
*isl_aff_nan_on_domain(__isl_take isl_local_space
*ls
)
138 aff
= isl_aff_alloc(ls
);
142 isl_seq_clr(aff
->v
->el
, aff
->v
->size
);
147 /* Return a piecewise affine expression defined on the specified domain
148 * that represents NaN.
150 __isl_give isl_pw_aff
*isl_pw_aff_nan_on_domain(__isl_take isl_local_space
*ls
)
152 return isl_pw_aff_from_aff(isl_aff_nan_on_domain(ls
));
155 /* Return an affine expression that is equal to "val" on
156 * domain local space "ls".
158 __isl_give isl_aff
*isl_aff_val_on_domain(__isl_take isl_local_space
*ls
,
159 __isl_take isl_val
*val
)
165 if (!isl_val_is_rat(val
))
166 isl_die(isl_val_get_ctx(val
), isl_error_invalid
,
167 "expecting rational value", goto error
);
169 aff
= isl_aff_alloc(isl_local_space_copy(ls
));
173 isl_seq_clr(aff
->v
->el
+ 2, aff
->v
->size
- 2);
174 isl_int_set(aff
->v
->el
[1], val
->n
);
175 isl_int_set(aff
->v
->el
[0], val
->d
);
177 isl_local_space_free(ls
);
181 isl_local_space_free(ls
);
186 /* Return an affine expression that is equal to the specified dimension
189 __isl_give isl_aff
*isl_aff_var_on_domain(__isl_take isl_local_space
*ls
,
190 enum isl_dim_type type
, unsigned pos
)
198 space
= isl_local_space_get_space(ls
);
201 if (isl_space_is_map(space
))
202 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
203 "expecting (parameter) set space", goto error
);
204 if (pos
>= isl_local_space_dim(ls
, type
))
205 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
206 "position out of bounds", goto error
);
208 isl_space_free(space
);
209 aff
= isl_aff_alloc(ls
);
213 pos
+= isl_local_space_offset(aff
->ls
, type
);
215 isl_int_set_si(aff
->v
->el
[0], 1);
216 isl_seq_clr(aff
->v
->el
+ 1, aff
->v
->size
- 1);
217 isl_int_set_si(aff
->v
->el
[1 + pos
], 1);
221 isl_local_space_free(ls
);
222 isl_space_free(space
);
226 /* Return a piecewise affine expression that is equal to
227 * the specified dimension in "ls".
229 __isl_give isl_pw_aff
*isl_pw_aff_var_on_domain(__isl_take isl_local_space
*ls
,
230 enum isl_dim_type type
, unsigned pos
)
232 return isl_pw_aff_from_aff(isl_aff_var_on_domain(ls
, type
, pos
));
235 /* Return an affine expression that is equal to the parameter
236 * in the domain space "space" with identifier "id".
238 __isl_give isl_aff
*isl_aff_param_on_domain_space_id(
239 __isl_take isl_space
*space
, __isl_take isl_id
*id
)
246 pos
= isl_space_find_dim_by_id(space
, isl_dim_param
, id
);
248 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
249 "parameter not found in space", goto error
);
251 ls
= isl_local_space_from_space(space
);
252 return isl_aff_var_on_domain(ls
, isl_dim_param
, pos
);
254 isl_space_free(space
);
259 __isl_give isl_aff
*isl_aff_copy(__isl_keep isl_aff
*aff
)
268 __isl_give isl_aff
*isl_aff_dup(__isl_keep isl_aff
*aff
)
273 return isl_aff_alloc_vec(isl_local_space_copy(aff
->ls
),
274 isl_vec_copy(aff
->v
));
277 __isl_give isl_aff
*isl_aff_cow(__isl_take isl_aff
*aff
)
285 return isl_aff_dup(aff
);
288 __isl_null isl_aff
*isl_aff_free(__isl_take isl_aff
*aff
)
296 isl_local_space_free(aff
->ls
);
297 isl_vec_free(aff
->v
);
304 isl_ctx
*isl_aff_get_ctx(__isl_keep isl_aff
*aff
)
306 return aff
? isl_local_space_get_ctx(aff
->ls
) : NULL
;
309 /* Return a hash value that digests "aff".
311 uint32_t isl_aff_get_hash(__isl_keep isl_aff
*aff
)
313 uint32_t hash
, ls_hash
, v_hash
;
318 hash
= isl_hash_init();
319 ls_hash
= isl_local_space_get_hash(aff
->ls
);
320 isl_hash_hash(hash
, ls_hash
);
321 v_hash
= isl_vec_get_hash(aff
->v
);
322 isl_hash_hash(hash
, v_hash
);
327 /* Externally, an isl_aff has a map space, but internally, the
328 * ls field corresponds to the domain of that space.
330 int isl_aff_dim(__isl_keep isl_aff
*aff
, enum isl_dim_type type
)
334 if (type
== isl_dim_out
)
336 if (type
== isl_dim_in
)
338 return isl_local_space_dim(aff
->ls
, type
);
341 /* Return the position of the dimension of the given type and name
343 * Return -1 if no such dimension can be found.
345 int isl_aff_find_dim_by_name(__isl_keep isl_aff
*aff
, enum isl_dim_type type
,
350 if (type
== isl_dim_out
)
352 if (type
== isl_dim_in
)
354 return isl_local_space_find_dim_by_name(aff
->ls
, type
, name
);
357 /* Return the domain space of "aff".
359 static __isl_keep isl_space
*isl_aff_peek_domain_space(__isl_keep isl_aff
*aff
)
361 return aff
? isl_local_space_peek_space(aff
->ls
) : NULL
;
364 __isl_give isl_space
*isl_aff_get_domain_space(__isl_keep isl_aff
*aff
)
366 return isl_space_copy(isl_aff_peek_domain_space(aff
));
369 __isl_give isl_space
*isl_aff_get_space(__isl_keep isl_aff
*aff
)
374 space
= isl_local_space_get_space(aff
->ls
);
375 space
= isl_space_from_domain(space
);
376 space
= isl_space_add_dims(space
, isl_dim_out
, 1);
380 __isl_give isl_local_space
*isl_aff_get_domain_local_space(
381 __isl_keep isl_aff
*aff
)
383 return aff
? isl_local_space_copy(aff
->ls
) : NULL
;
386 __isl_give isl_local_space
*isl_aff_get_local_space(__isl_keep isl_aff
*aff
)
391 ls
= isl_local_space_copy(aff
->ls
);
392 ls
= isl_local_space_from_domain(ls
);
393 ls
= isl_local_space_add_dims(ls
, isl_dim_out
, 1);
397 /* Return the local space of the domain of "aff".
398 * This may be either a copy or the local space itself
399 * if there is only one reference to "aff".
400 * This allows the local space to be modified inplace
401 * if both the expression and its local space have only a single reference.
402 * The caller is not allowed to modify "aff" between this call and
403 * a subsequent call to isl_aff_restore_domain_local_space.
404 * The only exception is that isl_aff_free can be called instead.
406 __isl_give isl_local_space
*isl_aff_take_domain_local_space(
407 __isl_keep isl_aff
*aff
)
414 return isl_aff_get_domain_local_space(aff
);
420 /* Set the local space of the domain of "aff" to "ls",
421 * where the local space of "aff" may be missing
422 * due to a preceding call to isl_aff_take_domain_local_space.
423 * However, in this case, "aff" only has a single reference and
424 * then the call to isl_aff_cow has no effect.
426 __isl_give isl_aff
*isl_aff_restore_domain_local_space(
427 __isl_keep isl_aff
*aff
, __isl_take isl_local_space
*ls
)
433 isl_local_space_free(ls
);
437 aff
= isl_aff_cow(aff
);
440 isl_local_space_free(aff
->ls
);
446 isl_local_space_free(ls
);
450 /* Externally, an isl_aff has a map space, but internally, the
451 * ls field corresponds to the domain of that space.
453 const char *isl_aff_get_dim_name(__isl_keep isl_aff
*aff
,
454 enum isl_dim_type type
, unsigned pos
)
458 if (type
== isl_dim_out
)
460 if (type
== isl_dim_in
)
462 return isl_local_space_get_dim_name(aff
->ls
, type
, pos
);
465 __isl_give isl_aff
*isl_aff_reset_domain_space(__isl_take isl_aff
*aff
,
466 __isl_take isl_space
*dim
)
468 aff
= isl_aff_cow(aff
);
472 aff
->ls
= isl_local_space_reset_space(aff
->ls
, dim
);
474 return isl_aff_free(aff
);
483 /* Reset the space of "aff". This function is called from isl_pw_templ.c
484 * and doesn't know if the space of an element object is represented
485 * directly or through its domain. It therefore passes along both.
487 __isl_give isl_aff
*isl_aff_reset_space_and_domain(__isl_take isl_aff
*aff
,
488 __isl_take isl_space
*space
, __isl_take isl_space
*domain
)
490 isl_space_free(space
);
491 return isl_aff_reset_domain_space(aff
, domain
);
494 /* Reorder the coefficients of the affine expression based
495 * on the given reordering.
496 * The reordering r is assumed to have been extended with the local
499 static __isl_give isl_vec
*vec_reorder(__isl_take isl_vec
*vec
,
500 __isl_take isl_reordering
*r
, int n_div
)
509 space
= isl_reordering_peek_space(r
);
510 res
= isl_vec_alloc(vec
->ctx
,
511 2 + isl_space_dim(space
, isl_dim_all
) + n_div
);
514 isl_seq_cpy(res
->el
, vec
->el
, 2);
515 isl_seq_clr(res
->el
+ 2, res
->size
- 2);
516 for (i
= 0; i
< r
->len
; ++i
)
517 isl_int_set(res
->el
[2 + r
->pos
[i
]], vec
->el
[2 + i
]);
519 isl_reordering_free(r
);
524 isl_reordering_free(r
);
528 /* Reorder the dimensions of the domain of "aff" according
529 * to the given reordering.
531 __isl_give isl_aff
*isl_aff_realign_domain(__isl_take isl_aff
*aff
,
532 __isl_take isl_reordering
*r
)
534 aff
= isl_aff_cow(aff
);
538 r
= isl_reordering_extend(r
, aff
->ls
->div
->n_row
);
539 aff
->v
= vec_reorder(aff
->v
, isl_reordering_copy(r
),
540 aff
->ls
->div
->n_row
);
541 aff
->ls
= isl_local_space_realign(aff
->ls
, r
);
543 if (!aff
->v
|| !aff
->ls
)
544 return isl_aff_free(aff
);
549 isl_reordering_free(r
);
553 __isl_give isl_aff
*isl_aff_align_params(__isl_take isl_aff
*aff
,
554 __isl_take isl_space
*model
)
556 isl_bool equal_params
;
561 equal_params
= isl_space_has_equal_params(aff
->ls
->dim
, model
);
562 if (equal_params
< 0)
567 exp
= isl_parameter_alignment_reordering(aff
->ls
->dim
, model
);
568 exp
= isl_reordering_extend_space(exp
,
569 isl_aff_get_domain_space(aff
));
570 aff
= isl_aff_realign_domain(aff
, exp
);
573 isl_space_free(model
);
576 isl_space_free(model
);
581 /* Is "aff" obviously equal to zero?
583 * If the denominator is zero, then "aff" is not equal to zero.
585 isl_bool
isl_aff_plain_is_zero(__isl_keep isl_aff
*aff
)
588 return isl_bool_error
;
590 if (isl_int_is_zero(aff
->v
->el
[0]))
591 return isl_bool_false
;
592 return isl_seq_first_non_zero(aff
->v
->el
+ 1, aff
->v
->size
- 1) < 0;
595 /* Does "aff" represent NaN?
597 isl_bool
isl_aff_is_nan(__isl_keep isl_aff
*aff
)
600 return isl_bool_error
;
602 return isl_seq_first_non_zero(aff
->v
->el
, 2) < 0;
605 /* Are "aff1" and "aff2" obviously equal?
607 * NaN is not equal to anything, not even to another NaN.
609 isl_bool
isl_aff_plain_is_equal(__isl_keep isl_aff
*aff1
,
610 __isl_keep isl_aff
*aff2
)
615 return isl_bool_error
;
617 if (isl_aff_is_nan(aff1
) || isl_aff_is_nan(aff2
))
618 return isl_bool_false
;
620 equal
= isl_local_space_is_equal(aff1
->ls
, aff2
->ls
);
621 if (equal
< 0 || !equal
)
624 return isl_vec_is_equal(aff1
->v
, aff2
->v
);
627 /* Return the common denominator of "aff" in "v".
629 * We cannot return anything meaningful in case of a NaN.
631 isl_stat
isl_aff_get_denominator(__isl_keep isl_aff
*aff
, isl_int
*v
)
634 return isl_stat_error
;
635 if (isl_aff_is_nan(aff
))
636 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
637 "cannot get denominator of NaN", return isl_stat_error
);
638 isl_int_set(*v
, aff
->v
->el
[0]);
642 /* Return the common denominator of "aff".
644 __isl_give isl_val
*isl_aff_get_denominator_val(__isl_keep isl_aff
*aff
)
651 ctx
= isl_aff_get_ctx(aff
);
652 if (isl_aff_is_nan(aff
))
653 return isl_val_nan(ctx
);
654 return isl_val_int_from_isl_int(ctx
, aff
->v
->el
[0]);
657 /* Return the constant term of "aff".
659 __isl_give isl_val
*isl_aff_get_constant_val(__isl_keep isl_aff
*aff
)
667 ctx
= isl_aff_get_ctx(aff
);
668 if (isl_aff_is_nan(aff
))
669 return isl_val_nan(ctx
);
670 v
= isl_val_rat_from_isl_int(ctx
, aff
->v
->el
[1], aff
->v
->el
[0]);
671 return isl_val_normalize(v
);
674 /* Return the coefficient of the variable of type "type" at position "pos"
677 __isl_give isl_val
*isl_aff_get_coefficient_val(__isl_keep isl_aff
*aff
,
678 enum isl_dim_type type
, int pos
)
686 ctx
= isl_aff_get_ctx(aff
);
687 if (type
== isl_dim_out
)
688 isl_die(ctx
, isl_error_invalid
,
689 "output/set dimension does not have a coefficient",
691 if (type
== isl_dim_in
)
694 if (pos
>= isl_local_space_dim(aff
->ls
, type
))
695 isl_die(ctx
, isl_error_invalid
,
696 "position out of bounds", return NULL
);
698 if (isl_aff_is_nan(aff
))
699 return isl_val_nan(ctx
);
700 pos
+= isl_local_space_offset(aff
->ls
, type
);
701 v
= isl_val_rat_from_isl_int(ctx
, aff
->v
->el
[1 + pos
], aff
->v
->el
[0]);
702 return isl_val_normalize(v
);
705 /* Return the sign of the coefficient of the variable of type "type"
706 * at position "pos" of "aff".
708 int isl_aff_coefficient_sgn(__isl_keep isl_aff
*aff
, enum isl_dim_type type
,
716 ctx
= isl_aff_get_ctx(aff
);
717 if (type
== isl_dim_out
)
718 isl_die(ctx
, isl_error_invalid
,
719 "output/set dimension does not have a coefficient",
721 if (type
== isl_dim_in
)
724 if (pos
>= isl_local_space_dim(aff
->ls
, type
))
725 isl_die(ctx
, isl_error_invalid
,
726 "position out of bounds", return 0);
728 pos
+= isl_local_space_offset(aff
->ls
, type
);
729 return isl_int_sgn(aff
->v
->el
[1 + pos
]);
732 /* Replace the numerator of the constant term of "aff" by "v".
734 * A NaN is unaffected by this operation.
736 __isl_give isl_aff
*isl_aff_set_constant(__isl_take isl_aff
*aff
, isl_int v
)
740 if (isl_aff_is_nan(aff
))
742 aff
= isl_aff_cow(aff
);
746 aff
->v
= isl_vec_cow(aff
->v
);
748 return isl_aff_free(aff
);
750 isl_int_set(aff
->v
->el
[1], v
);
755 /* Replace the constant term of "aff" by "v".
757 * A NaN is unaffected by this operation.
759 __isl_give isl_aff
*isl_aff_set_constant_val(__isl_take isl_aff
*aff
,
760 __isl_take isl_val
*v
)
765 if (isl_aff_is_nan(aff
)) {
770 if (!isl_val_is_rat(v
))
771 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
772 "expecting rational value", goto error
);
774 if (isl_int_eq(aff
->v
->el
[1], v
->n
) &&
775 isl_int_eq(aff
->v
->el
[0], v
->d
)) {
780 aff
= isl_aff_cow(aff
);
783 aff
->v
= isl_vec_cow(aff
->v
);
787 if (isl_int_eq(aff
->v
->el
[0], v
->d
)) {
788 isl_int_set(aff
->v
->el
[1], v
->n
);
789 } else if (isl_int_is_one(v
->d
)) {
790 isl_int_mul(aff
->v
->el
[1], aff
->v
->el
[0], v
->n
);
792 isl_seq_scale(aff
->v
->el
+ 1,
793 aff
->v
->el
+ 1, v
->d
, aff
->v
->size
- 1);
794 isl_int_mul(aff
->v
->el
[1], aff
->v
->el
[0], v
->n
);
795 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], v
->d
);
796 aff
->v
= isl_vec_normalize(aff
->v
);
809 /* Add "v" to the constant term of "aff".
811 * A NaN is unaffected by this operation.
813 __isl_give isl_aff
*isl_aff_add_constant(__isl_take isl_aff
*aff
, isl_int v
)
815 if (isl_int_is_zero(v
))
820 if (isl_aff_is_nan(aff
))
822 aff
= isl_aff_cow(aff
);
826 aff
->v
= isl_vec_cow(aff
->v
);
828 return isl_aff_free(aff
);
830 isl_int_addmul(aff
->v
->el
[1], aff
->v
->el
[0], v
);
835 /* Add "v" to the constant term of "aff".
837 * A NaN is unaffected by this operation.
839 __isl_give isl_aff
*isl_aff_add_constant_val(__isl_take isl_aff
*aff
,
840 __isl_take isl_val
*v
)
845 if (isl_aff_is_nan(aff
) || isl_val_is_zero(v
)) {
850 if (!isl_val_is_rat(v
))
851 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
852 "expecting rational value", goto error
);
854 aff
= isl_aff_cow(aff
);
858 aff
->v
= isl_vec_cow(aff
->v
);
862 if (isl_int_is_one(v
->d
)) {
863 isl_int_addmul(aff
->v
->el
[1], aff
->v
->el
[0], v
->n
);
864 } else if (isl_int_eq(aff
->v
->el
[0], v
->d
)) {
865 isl_int_add(aff
->v
->el
[1], aff
->v
->el
[1], v
->n
);
866 aff
->v
= isl_vec_normalize(aff
->v
);
870 isl_seq_scale(aff
->v
->el
+ 1,
871 aff
->v
->el
+ 1, v
->d
, aff
->v
->size
- 1);
872 isl_int_addmul(aff
->v
->el
[1], aff
->v
->el
[0], v
->n
);
873 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], v
->d
);
874 aff
->v
= isl_vec_normalize(aff
->v
);
887 __isl_give isl_aff
*isl_aff_add_constant_si(__isl_take isl_aff
*aff
, int v
)
892 isl_int_set_si(t
, v
);
893 aff
= isl_aff_add_constant(aff
, t
);
899 /* Add "v" to the numerator of the constant term of "aff".
901 * A NaN is unaffected by this operation.
903 __isl_give isl_aff
*isl_aff_add_constant_num(__isl_take isl_aff
*aff
, isl_int v
)
905 if (isl_int_is_zero(v
))
910 if (isl_aff_is_nan(aff
))
912 aff
= isl_aff_cow(aff
);
916 aff
->v
= isl_vec_cow(aff
->v
);
918 return isl_aff_free(aff
);
920 isl_int_add(aff
->v
->el
[1], aff
->v
->el
[1], v
);
925 /* Add "v" to the numerator of the constant term of "aff".
927 * A NaN is unaffected by this operation.
929 __isl_give isl_aff
*isl_aff_add_constant_num_si(__isl_take isl_aff
*aff
, int v
)
937 isl_int_set_si(t
, v
);
938 aff
= isl_aff_add_constant_num(aff
, t
);
944 /* Replace the numerator of the constant term of "aff" by "v".
946 * A NaN is unaffected by this operation.
948 __isl_give isl_aff
*isl_aff_set_constant_si(__isl_take isl_aff
*aff
, int v
)
952 if (isl_aff_is_nan(aff
))
954 aff
= isl_aff_cow(aff
);
958 aff
->v
= isl_vec_cow(aff
->v
);
960 return isl_aff_free(aff
);
962 isl_int_set_si(aff
->v
->el
[1], v
);
967 /* Replace the numerator of the coefficient of the variable of type "type"
968 * at position "pos" of "aff" by "v".
970 * A NaN is unaffected by this operation.
972 __isl_give isl_aff
*isl_aff_set_coefficient(__isl_take isl_aff
*aff
,
973 enum isl_dim_type type
, int pos
, isl_int v
)
978 if (type
== isl_dim_out
)
979 isl_die(aff
->v
->ctx
, isl_error_invalid
,
980 "output/set dimension does not have a coefficient",
981 return isl_aff_free(aff
));
982 if (type
== isl_dim_in
)
985 if (pos
>= isl_local_space_dim(aff
->ls
, type
))
986 isl_die(aff
->v
->ctx
, isl_error_invalid
,
987 "position out of bounds", return isl_aff_free(aff
));
989 if (isl_aff_is_nan(aff
))
991 aff
= isl_aff_cow(aff
);
995 aff
->v
= isl_vec_cow(aff
->v
);
997 return isl_aff_free(aff
);
999 pos
+= isl_local_space_offset(aff
->ls
, type
);
1000 isl_int_set(aff
->v
->el
[1 + pos
], v
);
1005 /* Replace the numerator of the coefficient of the variable of type "type"
1006 * at position "pos" of "aff" by "v".
1008 * A NaN is unaffected by this operation.
1010 __isl_give isl_aff
*isl_aff_set_coefficient_si(__isl_take isl_aff
*aff
,
1011 enum isl_dim_type type
, int pos
, int v
)
1016 if (type
== isl_dim_out
)
1017 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1018 "output/set dimension does not have a coefficient",
1019 return isl_aff_free(aff
));
1020 if (type
== isl_dim_in
)
1023 if (pos
< 0 || pos
>= isl_local_space_dim(aff
->ls
, type
))
1024 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1025 "position out of bounds", return isl_aff_free(aff
));
1027 if (isl_aff_is_nan(aff
))
1029 pos
+= isl_local_space_offset(aff
->ls
, type
);
1030 if (isl_int_cmp_si(aff
->v
->el
[1 + pos
], v
) == 0)
1033 aff
= isl_aff_cow(aff
);
1037 aff
->v
= isl_vec_cow(aff
->v
);
1039 return isl_aff_free(aff
);
1041 isl_int_set_si(aff
->v
->el
[1 + pos
], v
);
1046 /* Replace the coefficient of the variable of type "type" at position "pos"
1049 * A NaN is unaffected by this operation.
1051 __isl_give isl_aff
*isl_aff_set_coefficient_val(__isl_take isl_aff
*aff
,
1052 enum isl_dim_type type
, int pos
, __isl_take isl_val
*v
)
1057 if (type
== isl_dim_out
)
1058 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1059 "output/set dimension does not have a coefficient",
1061 if (type
== isl_dim_in
)
1064 if (pos
>= isl_local_space_dim(aff
->ls
, type
))
1065 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1066 "position out of bounds", goto error
);
1068 if (isl_aff_is_nan(aff
)) {
1072 if (!isl_val_is_rat(v
))
1073 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1074 "expecting rational value", goto error
);
1076 pos
+= isl_local_space_offset(aff
->ls
, type
);
1077 if (isl_int_eq(aff
->v
->el
[1 + pos
], v
->n
) &&
1078 isl_int_eq(aff
->v
->el
[0], v
->d
)) {
1083 aff
= isl_aff_cow(aff
);
1086 aff
->v
= isl_vec_cow(aff
->v
);
1090 if (isl_int_eq(aff
->v
->el
[0], v
->d
)) {
1091 isl_int_set(aff
->v
->el
[1 + pos
], v
->n
);
1092 } else if (isl_int_is_one(v
->d
)) {
1093 isl_int_mul(aff
->v
->el
[1 + pos
], aff
->v
->el
[0], v
->n
);
1095 isl_seq_scale(aff
->v
->el
+ 1,
1096 aff
->v
->el
+ 1, v
->d
, aff
->v
->size
- 1);
1097 isl_int_mul(aff
->v
->el
[1 + pos
], aff
->v
->el
[0], v
->n
);
1098 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], v
->d
);
1099 aff
->v
= isl_vec_normalize(aff
->v
);
1112 /* Add "v" to the coefficient of the variable of type "type"
1113 * at position "pos" of "aff".
1115 * A NaN is unaffected by this operation.
1117 __isl_give isl_aff
*isl_aff_add_coefficient(__isl_take isl_aff
*aff
,
1118 enum isl_dim_type type
, int pos
, isl_int v
)
1123 if (type
== isl_dim_out
)
1124 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1125 "output/set dimension does not have a coefficient",
1126 return isl_aff_free(aff
));
1127 if (type
== isl_dim_in
)
1130 if (pos
>= isl_local_space_dim(aff
->ls
, type
))
1131 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1132 "position out of bounds", return isl_aff_free(aff
));
1134 if (isl_aff_is_nan(aff
))
1136 aff
= isl_aff_cow(aff
);
1140 aff
->v
= isl_vec_cow(aff
->v
);
1142 return isl_aff_free(aff
);
1144 pos
+= isl_local_space_offset(aff
->ls
, type
);
1145 isl_int_addmul(aff
->v
->el
[1 + pos
], aff
->v
->el
[0], v
);
1150 /* Add "v" to the coefficient of the variable of type "type"
1151 * at position "pos" of "aff".
1153 * A NaN is unaffected by this operation.
1155 __isl_give isl_aff
*isl_aff_add_coefficient_val(__isl_take isl_aff
*aff
,
1156 enum isl_dim_type type
, int pos
, __isl_take isl_val
*v
)
1161 if (isl_val_is_zero(v
)) {
1166 if (type
== isl_dim_out
)
1167 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1168 "output/set dimension does not have a coefficient",
1170 if (type
== isl_dim_in
)
1173 if (pos
>= isl_local_space_dim(aff
->ls
, type
))
1174 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1175 "position out of bounds", goto error
);
1177 if (isl_aff_is_nan(aff
)) {
1181 if (!isl_val_is_rat(v
))
1182 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1183 "expecting rational value", goto error
);
1185 aff
= isl_aff_cow(aff
);
1189 aff
->v
= isl_vec_cow(aff
->v
);
1193 pos
+= isl_local_space_offset(aff
->ls
, type
);
1194 if (isl_int_is_one(v
->d
)) {
1195 isl_int_addmul(aff
->v
->el
[1 + pos
], aff
->v
->el
[0], v
->n
);
1196 } else if (isl_int_eq(aff
->v
->el
[0], v
->d
)) {
1197 isl_int_add(aff
->v
->el
[1 + pos
], aff
->v
->el
[1 + pos
], v
->n
);
1198 aff
->v
= isl_vec_normalize(aff
->v
);
1202 isl_seq_scale(aff
->v
->el
+ 1,
1203 aff
->v
->el
+ 1, v
->d
, aff
->v
->size
- 1);
1204 isl_int_addmul(aff
->v
->el
[1 + pos
], aff
->v
->el
[0], v
->n
);
1205 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], v
->d
);
1206 aff
->v
= isl_vec_normalize(aff
->v
);
1219 __isl_give isl_aff
*isl_aff_add_coefficient_si(__isl_take isl_aff
*aff
,
1220 enum isl_dim_type type
, int pos
, int v
)
1225 isl_int_set_si(t
, v
);
1226 aff
= isl_aff_add_coefficient(aff
, type
, pos
, t
);
1232 __isl_give isl_aff
*isl_aff_get_div(__isl_keep isl_aff
*aff
, int pos
)
1237 return isl_local_space_get_div(aff
->ls
, pos
);
1240 /* Return the negation of "aff".
1242 * As a special case, -NaN = NaN.
1244 __isl_give isl_aff
*isl_aff_neg(__isl_take isl_aff
*aff
)
1248 if (isl_aff_is_nan(aff
))
1250 aff
= isl_aff_cow(aff
);
1253 aff
->v
= isl_vec_cow(aff
->v
);
1255 return isl_aff_free(aff
);
1257 isl_seq_neg(aff
->v
->el
+ 1, aff
->v
->el
+ 1, aff
->v
->size
- 1);
1262 /* Remove divs from the local space that do not appear in the affine
1264 * We currently only remove divs at the end.
1265 * Some intermediate divs may also not appear directly in the affine
1266 * expression, but we would also need to check that no other divs are
1267 * defined in terms of them.
1269 __isl_give isl_aff
*isl_aff_remove_unused_divs(__isl_take isl_aff
*aff
)
1278 n
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
1279 off
= isl_local_space_offset(aff
->ls
, isl_dim_div
);
1281 pos
= isl_seq_last_non_zero(aff
->v
->el
+ 1 + off
, n
) + 1;
1285 aff
= isl_aff_cow(aff
);
1289 aff
->ls
= isl_local_space_drop_dims(aff
->ls
, isl_dim_div
, pos
, n
- pos
);
1290 aff
->v
= isl_vec_drop_els(aff
->v
, 1 + off
+ pos
, n
- pos
);
1291 if (!aff
->ls
|| !aff
->v
)
1292 return isl_aff_free(aff
);
1297 /* Look for any divs in the aff->ls with a denominator equal to one
1298 * and plug them into the affine expression and any subsequent divs
1299 * that may reference the div.
1301 static __isl_give isl_aff
*plug_in_integral_divs(__isl_take isl_aff
*aff
)
1307 isl_local_space
*ls
;
1313 n
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
1315 for (i
= 0; i
< n
; ++i
) {
1316 if (!isl_int_is_one(aff
->ls
->div
->row
[i
][0]))
1318 ls
= isl_local_space_copy(aff
->ls
);
1319 ls
= isl_local_space_substitute_seq(ls
, isl_dim_div
, i
,
1320 aff
->ls
->div
->row
[i
], len
, i
+ 1, n
- (i
+ 1));
1321 vec
= isl_vec_copy(aff
->v
);
1322 vec
= isl_vec_cow(vec
);
1328 pos
= isl_local_space_offset(aff
->ls
, isl_dim_div
) + i
;
1329 isl_seq_substitute(vec
->el
, pos
, aff
->ls
->div
->row
[i
],
1334 isl_vec_free(aff
->v
);
1336 isl_local_space_free(aff
->ls
);
1343 isl_local_space_free(ls
);
1344 return isl_aff_free(aff
);
1347 /* Look for any divs j that appear with a unit coefficient inside
1348 * the definitions of other divs i and plug them into the definitions
1351 * In particular, an expression of the form
1353 * floor((f(..) + floor(g(..)/n))/m)
1357 * floor((n * f(..) + g(..))/(n * m))
1359 * This simplification is correct because we can move the expression
1360 * f(..) into the inner floor in the original expression to obtain
1362 * floor(floor((n * f(..) + g(..))/n)/m)
1364 * from which we can derive the simplified expression.
1366 static __isl_give isl_aff
*plug_in_unit_divs(__isl_take isl_aff
*aff
)
1374 n
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
1375 off
= isl_local_space_offset(aff
->ls
, isl_dim_div
);
1376 for (i
= 1; i
< n
; ++i
) {
1377 for (j
= 0; j
< i
; ++j
) {
1378 if (!isl_int_is_one(aff
->ls
->div
->row
[i
][1 + off
+ j
]))
1380 aff
->ls
= isl_local_space_substitute_seq(aff
->ls
,
1381 isl_dim_div
, j
, aff
->ls
->div
->row
[j
],
1382 aff
->v
->size
, i
, 1);
1384 return isl_aff_free(aff
);
1391 /* Swap divs "a" and "b" in "aff", which is assumed to be non-NULL.
1393 * Even though this function is only called on isl_affs with a single
1394 * reference, we are careful to only change aff->v and aff->ls together.
1396 static __isl_give isl_aff
*swap_div(__isl_take isl_aff
*aff
, int a
, int b
)
1398 unsigned off
= isl_local_space_offset(aff
->ls
, isl_dim_div
);
1399 isl_local_space
*ls
;
1402 ls
= isl_local_space_copy(aff
->ls
);
1403 ls
= isl_local_space_swap_div(ls
, a
, b
);
1404 v
= isl_vec_copy(aff
->v
);
1409 isl_int_swap(v
->el
[1 + off
+ a
], v
->el
[1 + off
+ b
]);
1410 isl_vec_free(aff
->v
);
1412 isl_local_space_free(aff
->ls
);
1418 isl_local_space_free(ls
);
1419 return isl_aff_free(aff
);
1422 /* Merge divs "a" and "b" in "aff", which is assumed to be non-NULL.
1424 * We currently do not actually remove div "b", but simply add its
1425 * coefficient to that of "a" and then zero it out.
1427 static __isl_give isl_aff
*merge_divs(__isl_take isl_aff
*aff
, int a
, int b
)
1429 unsigned off
= isl_local_space_offset(aff
->ls
, isl_dim_div
);
1431 if (isl_int_is_zero(aff
->v
->el
[1 + off
+ b
]))
1434 aff
->v
= isl_vec_cow(aff
->v
);
1436 return isl_aff_free(aff
);
1438 isl_int_add(aff
->v
->el
[1 + off
+ a
],
1439 aff
->v
->el
[1 + off
+ a
], aff
->v
->el
[1 + off
+ b
]);
1440 isl_int_set_si(aff
->v
->el
[1 + off
+ b
], 0);
1445 /* Sort the divs in the local space of "aff" according to
1446 * the comparison function "cmp_row" in isl_local_space.c,
1447 * combining the coefficients of identical divs.
1449 * Reordering divs does not change the semantics of "aff",
1450 * so there is no need to call isl_aff_cow.
1451 * Moreover, this function is currently only called on isl_affs
1452 * with a single reference.
1454 static __isl_give isl_aff
*sort_divs(__isl_take isl_aff
*aff
)
1461 n
= isl_aff_dim(aff
, isl_dim_div
);
1462 for (i
= 1; i
< n
; ++i
) {
1463 for (j
= i
- 1; j
>= 0; --j
) {
1464 int cmp
= isl_mat_cmp_div(aff
->ls
->div
, j
, j
+ 1);
1468 aff
= merge_divs(aff
, j
, j
+ 1);
1470 aff
= swap_div(aff
, j
, j
+ 1);
1479 /* Normalize the representation of "aff".
1481 * This function should only be called of "new" isl_affs, i.e.,
1482 * with only a single reference. We therefore do not need to
1483 * worry about affecting other instances.
1485 __isl_give isl_aff
*isl_aff_normalize(__isl_take isl_aff
*aff
)
1489 aff
->v
= isl_vec_normalize(aff
->v
);
1491 return isl_aff_free(aff
);
1492 aff
= plug_in_integral_divs(aff
);
1493 aff
= plug_in_unit_divs(aff
);
1494 aff
= sort_divs(aff
);
1495 aff
= isl_aff_remove_unused_divs(aff
);
1499 /* Given f, return floor(f).
1500 * If f is an integer expression, then just return f.
1501 * If f is a constant, then return the constant floor(f).
1502 * Otherwise, if f = g/m, write g = q m + r,
1503 * create a new div d = [r/m] and return the expression q + d.
1504 * The coefficients in r are taken to lie between -m/2 and m/2.
1506 * reduce_div_coefficients performs the same normalization.
1508 * As a special case, floor(NaN) = NaN.
1510 __isl_give isl_aff
*isl_aff_floor(__isl_take isl_aff
*aff
)
1520 if (isl_aff_is_nan(aff
))
1522 if (isl_int_is_one(aff
->v
->el
[0]))
1525 aff
= isl_aff_cow(aff
);
1529 aff
->v
= isl_vec_cow(aff
->v
);
1531 return isl_aff_free(aff
);
1533 if (isl_aff_is_cst(aff
)) {
1534 isl_int_fdiv_q(aff
->v
->el
[1], aff
->v
->el
[1], aff
->v
->el
[0]);
1535 isl_int_set_si(aff
->v
->el
[0], 1);
1539 div
= isl_vec_copy(aff
->v
);
1540 div
= isl_vec_cow(div
);
1542 return isl_aff_free(aff
);
1544 ctx
= isl_aff_get_ctx(aff
);
1545 isl_int_fdiv_q(aff
->v
->el
[0], aff
->v
->el
[0], ctx
->two
);
1546 for (i
= 1; i
< aff
->v
->size
; ++i
) {
1547 isl_int_fdiv_r(div
->el
[i
], div
->el
[i
], div
->el
[0]);
1548 isl_int_fdiv_q(aff
->v
->el
[i
], aff
->v
->el
[i
], div
->el
[0]);
1549 if (isl_int_gt(div
->el
[i
], aff
->v
->el
[0])) {
1550 isl_int_sub(div
->el
[i
], div
->el
[i
], div
->el
[0]);
1551 isl_int_add_ui(aff
->v
->el
[i
], aff
->v
->el
[i
], 1);
1555 aff
->ls
= isl_local_space_add_div(aff
->ls
, div
);
1557 return isl_aff_free(aff
);
1559 size
= aff
->v
->size
;
1560 aff
->v
= isl_vec_extend(aff
->v
, size
+ 1);
1562 return isl_aff_free(aff
);
1563 isl_int_set_si(aff
->v
->el
[0], 1);
1564 isl_int_set_si(aff
->v
->el
[size
], 1);
1566 aff
= isl_aff_normalize(aff
);
1573 * aff mod m = aff - m * floor(aff/m)
1575 * with m an integer value.
1577 __isl_give isl_aff
*isl_aff_mod_val(__isl_take isl_aff
*aff
,
1578 __isl_take isl_val
*m
)
1585 if (!isl_val_is_int(m
))
1586 isl_die(isl_val_get_ctx(m
), isl_error_invalid
,
1587 "expecting integer modulo", goto error
);
1589 res
= isl_aff_copy(aff
);
1590 aff
= isl_aff_scale_down_val(aff
, isl_val_copy(m
));
1591 aff
= isl_aff_floor(aff
);
1592 aff
= isl_aff_scale_val(aff
, m
);
1593 res
= isl_aff_sub(res
, aff
);
1604 * pwaff mod m = pwaff - m * floor(pwaff/m)
1606 __isl_give isl_pw_aff
*isl_pw_aff_mod(__isl_take isl_pw_aff
*pwaff
, isl_int m
)
1610 res
= isl_pw_aff_copy(pwaff
);
1611 pwaff
= isl_pw_aff_scale_down(pwaff
, m
);
1612 pwaff
= isl_pw_aff_floor(pwaff
);
1613 pwaff
= isl_pw_aff_scale(pwaff
, m
);
1614 res
= isl_pw_aff_sub(res
, pwaff
);
1621 * pa mod m = pa - m * floor(pa/m)
1623 * with m an integer value.
1625 __isl_give isl_pw_aff
*isl_pw_aff_mod_val(__isl_take isl_pw_aff
*pa
,
1626 __isl_take isl_val
*m
)
1630 if (!isl_val_is_int(m
))
1631 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
1632 "expecting integer modulo", goto error
);
1633 pa
= isl_pw_aff_mod(pa
, m
->n
);
1637 isl_pw_aff_free(pa
);
1642 /* Given f, return ceil(f).
1643 * If f is an integer expression, then just return f.
1644 * Otherwise, let f be the expression
1650 * floor((e + m - 1)/m)
1652 * As a special case, ceil(NaN) = NaN.
1654 __isl_give isl_aff
*isl_aff_ceil(__isl_take isl_aff
*aff
)
1659 if (isl_aff_is_nan(aff
))
1661 if (isl_int_is_one(aff
->v
->el
[0]))
1664 aff
= isl_aff_cow(aff
);
1667 aff
->v
= isl_vec_cow(aff
->v
);
1669 return isl_aff_free(aff
);
1671 isl_int_add(aff
->v
->el
[1], aff
->v
->el
[1], aff
->v
->el
[0]);
1672 isl_int_sub_ui(aff
->v
->el
[1], aff
->v
->el
[1], 1);
1673 aff
= isl_aff_floor(aff
);
1678 /* Apply the expansion computed by isl_merge_divs.
1679 * The expansion itself is given by "exp" while the resulting
1680 * list of divs is given by "div".
1682 __isl_give isl_aff
*isl_aff_expand_divs(__isl_take isl_aff
*aff
,
1683 __isl_take isl_mat
*div
, int *exp
)
1689 aff
= isl_aff_cow(aff
);
1693 old_n_div
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
1694 new_n_div
= isl_mat_rows(div
);
1695 offset
= 1 + isl_local_space_offset(aff
->ls
, isl_dim_div
);
1697 aff
->v
= isl_vec_expand(aff
->v
, offset
, old_n_div
, exp
, new_n_div
);
1698 aff
->ls
= isl_local_space_replace_divs(aff
->ls
, div
);
1699 if (!aff
->v
|| !aff
->ls
)
1700 return isl_aff_free(aff
);
1708 /* Add two affine expressions that live in the same local space.
1710 static __isl_give isl_aff
*add_expanded(__isl_take isl_aff
*aff1
,
1711 __isl_take isl_aff
*aff2
)
1715 aff1
= isl_aff_cow(aff1
);
1719 aff1
->v
= isl_vec_cow(aff1
->v
);
1725 isl_int_gcd(gcd
, aff1
->v
->el
[0], aff2
->v
->el
[0]);
1726 isl_int_divexact(f
, aff2
->v
->el
[0], gcd
);
1727 isl_seq_scale(aff1
->v
->el
+ 1, aff1
->v
->el
+ 1, f
, aff1
->v
->size
- 1);
1728 isl_int_divexact(f
, aff1
->v
->el
[0], gcd
);
1729 isl_seq_addmul(aff1
->v
->el
+ 1, f
, aff2
->v
->el
+ 1, aff1
->v
->size
- 1);
1730 isl_int_divexact(f
, aff2
->v
->el
[0], gcd
);
1731 isl_int_mul(aff1
->v
->el
[0], aff1
->v
->el
[0], f
);
1743 /* Return the sum of "aff1" and "aff2".
1745 * If either of the two is NaN, then the result is NaN.
1747 __isl_give isl_aff
*isl_aff_add(__isl_take isl_aff
*aff1
,
1748 __isl_take isl_aff
*aff2
)
1759 ctx
= isl_aff_get_ctx(aff1
);
1760 if (!isl_space_is_equal(aff1
->ls
->dim
, aff2
->ls
->dim
))
1761 isl_die(ctx
, isl_error_invalid
,
1762 "spaces don't match", goto error
);
1764 if (isl_aff_is_nan(aff1
)) {
1768 if (isl_aff_is_nan(aff2
)) {
1773 n_div1
= isl_aff_dim(aff1
, isl_dim_div
);
1774 n_div2
= isl_aff_dim(aff2
, isl_dim_div
);
1775 if (n_div1
== 0 && n_div2
== 0)
1776 return add_expanded(aff1
, aff2
);
1778 exp1
= isl_alloc_array(ctx
, int, n_div1
);
1779 exp2
= isl_alloc_array(ctx
, int, n_div2
);
1780 if ((n_div1
&& !exp1
) || (n_div2
&& !exp2
))
1783 div
= isl_merge_divs(aff1
->ls
->div
, aff2
->ls
->div
, exp1
, exp2
);
1784 aff1
= isl_aff_expand_divs(aff1
, isl_mat_copy(div
), exp1
);
1785 aff2
= isl_aff_expand_divs(aff2
, div
, exp2
);
1789 return add_expanded(aff1
, aff2
);
1798 __isl_give isl_aff
*isl_aff_sub(__isl_take isl_aff
*aff1
,
1799 __isl_take isl_aff
*aff2
)
1801 return isl_aff_add(aff1
, isl_aff_neg(aff2
));
1804 /* Return the result of scaling "aff" by a factor of "f".
1806 * As a special case, f * NaN = NaN.
1808 __isl_give isl_aff
*isl_aff_scale(__isl_take isl_aff
*aff
, isl_int f
)
1814 if (isl_aff_is_nan(aff
))
1817 if (isl_int_is_one(f
))
1820 aff
= isl_aff_cow(aff
);
1823 aff
->v
= isl_vec_cow(aff
->v
);
1825 return isl_aff_free(aff
);
1827 if (isl_int_is_pos(f
) && isl_int_is_divisible_by(aff
->v
->el
[0], f
)) {
1828 isl_int_divexact(aff
->v
->el
[0], aff
->v
->el
[0], f
);
1833 isl_int_gcd(gcd
, aff
->v
->el
[0], f
);
1834 isl_int_divexact(aff
->v
->el
[0], aff
->v
->el
[0], gcd
);
1835 isl_int_divexact(gcd
, f
, gcd
);
1836 isl_seq_scale(aff
->v
->el
+ 1, aff
->v
->el
+ 1, gcd
, aff
->v
->size
- 1);
1842 /* Multiple "aff" by "v".
1844 __isl_give isl_aff
*isl_aff_scale_val(__isl_take isl_aff
*aff
,
1845 __isl_take isl_val
*v
)
1850 if (isl_val_is_one(v
)) {
1855 if (!isl_val_is_rat(v
))
1856 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1857 "expecting rational factor", goto error
);
1859 aff
= isl_aff_scale(aff
, v
->n
);
1860 aff
= isl_aff_scale_down(aff
, v
->d
);
1870 /* Return the result of scaling "aff" down by a factor of "f".
1872 * As a special case, NaN/f = NaN.
1874 __isl_give isl_aff
*isl_aff_scale_down(__isl_take isl_aff
*aff
, isl_int f
)
1880 if (isl_aff_is_nan(aff
))
1883 if (isl_int_is_one(f
))
1886 aff
= isl_aff_cow(aff
);
1890 if (isl_int_is_zero(f
))
1891 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1892 "cannot scale down by zero", return isl_aff_free(aff
));
1894 aff
->v
= isl_vec_cow(aff
->v
);
1896 return isl_aff_free(aff
);
1899 isl_seq_gcd(aff
->v
->el
+ 1, aff
->v
->size
- 1, &gcd
);
1900 isl_int_gcd(gcd
, gcd
, f
);
1901 isl_seq_scale_down(aff
->v
->el
+ 1, aff
->v
->el
+ 1, gcd
, aff
->v
->size
- 1);
1902 isl_int_divexact(gcd
, f
, gcd
);
1903 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], gcd
);
1909 /* Divide "aff" by "v".
1911 __isl_give isl_aff
*isl_aff_scale_down_val(__isl_take isl_aff
*aff
,
1912 __isl_take isl_val
*v
)
1917 if (isl_val_is_one(v
)) {
1922 if (!isl_val_is_rat(v
))
1923 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1924 "expecting rational factor", goto error
);
1925 if (!isl_val_is_pos(v
))
1926 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1927 "factor needs to be positive", goto error
);
1929 aff
= isl_aff_scale(aff
, v
->d
);
1930 aff
= isl_aff_scale_down(aff
, v
->n
);
1940 __isl_give isl_aff
*isl_aff_scale_down_ui(__isl_take isl_aff
*aff
, unsigned f
)
1948 isl_int_set_ui(v
, f
);
1949 aff
= isl_aff_scale_down(aff
, v
);
1955 __isl_give isl_aff
*isl_aff_set_dim_name(__isl_take isl_aff
*aff
,
1956 enum isl_dim_type type
, unsigned pos
, const char *s
)
1958 aff
= isl_aff_cow(aff
);
1961 if (type
== isl_dim_out
)
1962 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1963 "cannot set name of output/set dimension",
1964 return isl_aff_free(aff
));
1965 if (type
== isl_dim_in
)
1967 aff
->ls
= isl_local_space_set_dim_name(aff
->ls
, type
, pos
, s
);
1969 return isl_aff_free(aff
);
1974 __isl_give isl_aff
*isl_aff_set_dim_id(__isl_take isl_aff
*aff
,
1975 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
1977 aff
= isl_aff_cow(aff
);
1980 if (type
== isl_dim_out
)
1981 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1982 "cannot set name of output/set dimension",
1984 if (type
== isl_dim_in
)
1986 aff
->ls
= isl_local_space_set_dim_id(aff
->ls
, type
, pos
, id
);
1988 return isl_aff_free(aff
);
1997 /* Replace the identifier of the input tuple of "aff" by "id".
1998 * type is currently required to be equal to isl_dim_in
2000 __isl_give isl_aff
*isl_aff_set_tuple_id(__isl_take isl_aff
*aff
,
2001 enum isl_dim_type type
, __isl_take isl_id
*id
)
2003 aff
= isl_aff_cow(aff
);
2006 if (type
!= isl_dim_in
)
2007 isl_die(aff
->v
->ctx
, isl_error_invalid
,
2008 "cannot only set id of input tuple", goto error
);
2009 aff
->ls
= isl_local_space_set_tuple_id(aff
->ls
, isl_dim_set
, id
);
2011 return isl_aff_free(aff
);
2020 /* Exploit the equalities in "eq" to simplify the affine expression
2021 * and the expressions of the integer divisions in the local space.
2022 * The integer divisions in this local space are assumed to appear
2023 * as regular dimensions in "eq".
2025 static __isl_give isl_aff
*isl_aff_substitute_equalities_lifted(
2026 __isl_take isl_aff
*aff
, __isl_take isl_basic_set
*eq
)
2034 if (eq
->n_eq
== 0) {
2035 isl_basic_set_free(eq
);
2039 aff
= isl_aff_cow(aff
);
2043 aff
->ls
= isl_local_space_substitute_equalities(aff
->ls
,
2044 isl_basic_set_copy(eq
));
2045 aff
->v
= isl_vec_cow(aff
->v
);
2046 if (!aff
->ls
|| !aff
->v
)
2049 total
= 1 + isl_space_dim(eq
->dim
, isl_dim_all
);
2051 for (i
= 0; i
< eq
->n_eq
; ++i
) {
2052 j
= isl_seq_last_non_zero(eq
->eq
[i
], total
+ n_div
);
2053 if (j
< 0 || j
== 0 || j
>= total
)
2056 isl_seq_elim(aff
->v
->el
+ 1, eq
->eq
[i
], j
, total
,
2060 isl_basic_set_free(eq
);
2061 aff
= isl_aff_normalize(aff
);
2064 isl_basic_set_free(eq
);
2069 /* Exploit the equalities in "eq" to simplify the affine expression
2070 * and the expressions of the integer divisions in the local space.
2072 __isl_give isl_aff
*isl_aff_substitute_equalities(__isl_take isl_aff
*aff
,
2073 __isl_take isl_basic_set
*eq
)
2079 n_div
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
2081 eq
= isl_basic_set_add_dims(eq
, isl_dim_set
, n_div
);
2082 return isl_aff_substitute_equalities_lifted(aff
, eq
);
2084 isl_basic_set_free(eq
);
2089 /* Look for equalities among the variables shared by context and aff
2090 * and the integer divisions of aff, if any.
2091 * The equalities are then used to eliminate coefficients and/or integer
2092 * divisions from aff.
2094 __isl_give isl_aff
*isl_aff_gist(__isl_take isl_aff
*aff
,
2095 __isl_take isl_set
*context
)
2097 isl_basic_set
*hull
;
2102 n_div
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
2104 isl_basic_set
*bset
;
2105 isl_local_space
*ls
;
2106 context
= isl_set_add_dims(context
, isl_dim_set
, n_div
);
2107 ls
= isl_aff_get_domain_local_space(aff
);
2108 bset
= isl_basic_set_from_local_space(ls
);
2109 bset
= isl_basic_set_lift(bset
);
2110 bset
= isl_basic_set_flatten(bset
);
2111 context
= isl_set_intersect(context
,
2112 isl_set_from_basic_set(bset
));
2115 hull
= isl_set_affine_hull(context
);
2116 return isl_aff_substitute_equalities_lifted(aff
, hull
);
2119 isl_set_free(context
);
2123 __isl_give isl_aff
*isl_aff_gist_params(__isl_take isl_aff
*aff
,
2124 __isl_take isl_set
*context
)
2126 isl_set
*dom_context
= isl_set_universe(isl_aff_get_domain_space(aff
));
2127 dom_context
= isl_set_intersect_params(dom_context
, context
);
2128 return isl_aff_gist(aff
, dom_context
);
2131 /* Return a basic set containing those elements in the space
2132 * of aff where it is positive. "rational" should not be set.
2134 * If "aff" is NaN, then it is not positive.
2136 static __isl_give isl_basic_set
*aff_pos_basic_set(__isl_take isl_aff
*aff
,
2139 isl_constraint
*ineq
;
2140 isl_basic_set
*bset
;
2145 if (isl_aff_is_nan(aff
)) {
2146 isl_space
*space
= isl_aff_get_domain_space(aff
);
2148 return isl_basic_set_empty(space
);
2151 isl_die(isl_aff_get_ctx(aff
), isl_error_unsupported
,
2152 "rational sets not supported", goto error
);
2154 ineq
= isl_inequality_from_aff(aff
);
2155 c
= isl_constraint_get_constant_val(ineq
);
2156 c
= isl_val_sub_ui(c
, 1);
2157 ineq
= isl_constraint_set_constant_val(ineq
, c
);
2159 bset
= isl_basic_set_from_constraint(ineq
);
2160 bset
= isl_basic_set_simplify(bset
);
2167 /* Return a basic set containing those elements in the space
2168 * of aff where it is non-negative.
2169 * If "rational" is set, then return a rational basic set.
2171 * If "aff" is NaN, then it is not non-negative (it's not negative either).
2173 static __isl_give isl_basic_set
*aff_nonneg_basic_set(
2174 __isl_take isl_aff
*aff
, int rational
)
2176 isl_constraint
*ineq
;
2177 isl_basic_set
*bset
;
2181 if (isl_aff_is_nan(aff
)) {
2182 isl_space
*space
= isl_aff_get_domain_space(aff
);
2184 return isl_basic_set_empty(space
);
2187 ineq
= isl_inequality_from_aff(aff
);
2189 bset
= isl_basic_set_from_constraint(ineq
);
2191 bset
= isl_basic_set_set_rational(bset
);
2192 bset
= isl_basic_set_simplify(bset
);
2196 /* Return a basic set containing those elements in the space
2197 * of aff where it is non-negative.
2199 __isl_give isl_basic_set
*isl_aff_nonneg_basic_set(__isl_take isl_aff
*aff
)
2201 return aff_nonneg_basic_set(aff
, 0);
2204 /* Return a basic set containing those elements in the domain space
2205 * of "aff" where it is positive.
2207 __isl_give isl_basic_set
*isl_aff_pos_basic_set(__isl_take isl_aff
*aff
)
2209 aff
= isl_aff_add_constant_num_si(aff
, -1);
2210 return isl_aff_nonneg_basic_set(aff
);
2213 /* Return a basic set containing those elements in the domain space
2214 * of aff where it is negative.
2216 __isl_give isl_basic_set
*isl_aff_neg_basic_set(__isl_take isl_aff
*aff
)
2218 aff
= isl_aff_neg(aff
);
2219 return isl_aff_pos_basic_set(aff
);
2222 /* Return a basic set containing those elements in the space
2223 * of aff where it is zero.
2224 * If "rational" is set, then return a rational basic set.
2226 * If "aff" is NaN, then it is not zero.
2228 static __isl_give isl_basic_set
*aff_zero_basic_set(__isl_take isl_aff
*aff
,
2231 isl_constraint
*ineq
;
2232 isl_basic_set
*bset
;
2236 if (isl_aff_is_nan(aff
)) {
2237 isl_space
*space
= isl_aff_get_domain_space(aff
);
2239 return isl_basic_set_empty(space
);
2242 ineq
= isl_equality_from_aff(aff
);
2244 bset
= isl_basic_set_from_constraint(ineq
);
2246 bset
= isl_basic_set_set_rational(bset
);
2247 bset
= isl_basic_set_simplify(bset
);
2251 /* Return a basic set containing those elements in the space
2252 * of aff where it is zero.
2254 __isl_give isl_basic_set
*isl_aff_zero_basic_set(__isl_take isl_aff
*aff
)
2256 return aff_zero_basic_set(aff
, 0);
2259 /* Return a basic set containing those elements in the shared space
2260 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
2262 __isl_give isl_basic_set
*isl_aff_ge_basic_set(__isl_take isl_aff
*aff1
,
2263 __isl_take isl_aff
*aff2
)
2265 aff1
= isl_aff_sub(aff1
, aff2
);
2267 return isl_aff_nonneg_basic_set(aff1
);
2270 /* Return a basic set containing those elements in the shared domain space
2271 * of "aff1" and "aff2" where "aff1" is greater than "aff2".
2273 __isl_give isl_basic_set
*isl_aff_gt_basic_set(__isl_take isl_aff
*aff1
,
2274 __isl_take isl_aff
*aff2
)
2276 aff1
= isl_aff_sub(aff1
, aff2
);
2278 return isl_aff_pos_basic_set(aff1
);
2281 /* Return a set containing those elements in the shared space
2282 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
2284 __isl_give isl_set
*isl_aff_ge_set(__isl_take isl_aff
*aff1
,
2285 __isl_take isl_aff
*aff2
)
2287 return isl_set_from_basic_set(isl_aff_ge_basic_set(aff1
, aff2
));
2290 /* Return a set containing those elements in the shared domain space
2291 * of aff1 and aff2 where aff1 is greater than aff2.
2293 * If either of the two inputs is NaN, then the result is empty,
2294 * as comparisons with NaN always return false.
2296 __isl_give isl_set
*isl_aff_gt_set(__isl_take isl_aff
*aff1
,
2297 __isl_take isl_aff
*aff2
)
2299 return isl_set_from_basic_set(isl_aff_gt_basic_set(aff1
, aff2
));
2302 /* Return a basic set containing those elements in the shared space
2303 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
2305 __isl_give isl_basic_set
*isl_aff_le_basic_set(__isl_take isl_aff
*aff1
,
2306 __isl_take isl_aff
*aff2
)
2308 return isl_aff_ge_basic_set(aff2
, aff1
);
2311 /* Return a basic set containing those elements in the shared domain space
2312 * of "aff1" and "aff2" where "aff1" is smaller than "aff2".
2314 __isl_give isl_basic_set
*isl_aff_lt_basic_set(__isl_take isl_aff
*aff1
,
2315 __isl_take isl_aff
*aff2
)
2317 return isl_aff_gt_basic_set(aff2
, aff1
);
2320 /* Return a set containing those elements in the shared space
2321 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
2323 __isl_give isl_set
*isl_aff_le_set(__isl_take isl_aff
*aff1
,
2324 __isl_take isl_aff
*aff2
)
2326 return isl_aff_ge_set(aff2
, aff1
);
2329 /* Return a set containing those elements in the shared domain space
2330 * of "aff1" and "aff2" where "aff1" is smaller than "aff2".
2332 __isl_give isl_set
*isl_aff_lt_set(__isl_take isl_aff
*aff1
,
2333 __isl_take isl_aff
*aff2
)
2335 return isl_set_from_basic_set(isl_aff_lt_basic_set(aff1
, aff2
));
2338 /* Return a basic set containing those elements in the shared space
2339 * of aff1 and aff2 where aff1 and aff2 are equal.
2341 __isl_give isl_basic_set
*isl_aff_eq_basic_set(__isl_take isl_aff
*aff1
,
2342 __isl_take isl_aff
*aff2
)
2344 aff1
= isl_aff_sub(aff1
, aff2
);
2346 return isl_aff_zero_basic_set(aff1
);
2349 /* Return a set containing those elements in the shared space
2350 * of aff1 and aff2 where aff1 and aff2 are equal.
2352 __isl_give isl_set
*isl_aff_eq_set(__isl_take isl_aff
*aff1
,
2353 __isl_take isl_aff
*aff2
)
2355 return isl_set_from_basic_set(isl_aff_eq_basic_set(aff1
, aff2
));
2358 /* Return a set containing those elements in the shared domain space
2359 * of aff1 and aff2 where aff1 and aff2 are not equal.
2361 * If either of the two inputs is NaN, then the result is empty,
2362 * as comparisons with NaN always return false.
2364 __isl_give isl_set
*isl_aff_ne_set(__isl_take isl_aff
*aff1
,
2365 __isl_take isl_aff
*aff2
)
2367 isl_set
*set_lt
, *set_gt
;
2369 set_lt
= isl_aff_lt_set(isl_aff_copy(aff1
),
2370 isl_aff_copy(aff2
));
2371 set_gt
= isl_aff_gt_set(aff1
, aff2
);
2372 return isl_set_union_disjoint(set_lt
, set_gt
);
2375 __isl_give isl_aff
*isl_aff_add_on_domain(__isl_keep isl_set
*dom
,
2376 __isl_take isl_aff
*aff1
, __isl_take isl_aff
*aff2
)
2378 aff1
= isl_aff_add(aff1
, aff2
);
2379 aff1
= isl_aff_gist(aff1
, isl_set_copy(dom
));
2383 int isl_aff_is_empty(__isl_keep isl_aff
*aff
)
2391 /* Check whether the given affine expression has non-zero coefficient
2392 * for any dimension in the given range or if any of these dimensions
2393 * appear with non-zero coefficients in any of the integer divisions
2394 * involved in the affine expression.
2396 isl_bool
isl_aff_involves_dims(__isl_keep isl_aff
*aff
,
2397 enum isl_dim_type type
, unsigned first
, unsigned n
)
2402 isl_bool involves
= isl_bool_false
;
2405 return isl_bool_error
;
2407 return isl_bool_false
;
2409 ctx
= isl_aff_get_ctx(aff
);
2410 if (first
+ n
> isl_aff_dim(aff
, type
))
2411 isl_die(ctx
, isl_error_invalid
,
2412 "range out of bounds", return isl_bool_error
);
2414 active
= isl_local_space_get_active(aff
->ls
, aff
->v
->el
+ 2);
2418 first
+= isl_local_space_offset(aff
->ls
, type
) - 1;
2419 for (i
= 0; i
< n
; ++i
)
2420 if (active
[first
+ i
]) {
2421 involves
= isl_bool_true
;
2430 return isl_bool_error
;
2433 __isl_give isl_aff
*isl_aff_drop_dims(__isl_take isl_aff
*aff
,
2434 enum isl_dim_type type
, unsigned first
, unsigned n
)
2440 if (type
== isl_dim_out
)
2441 isl_die(aff
->v
->ctx
, isl_error_invalid
,
2442 "cannot drop output/set dimension",
2443 return isl_aff_free(aff
));
2444 if (type
== isl_dim_in
)
2446 if (n
== 0 && !isl_local_space_is_named_or_nested(aff
->ls
, type
))
2449 ctx
= isl_aff_get_ctx(aff
);
2450 if (first
+ n
> isl_local_space_dim(aff
->ls
, type
))
2451 isl_die(ctx
, isl_error_invalid
, "range out of bounds",
2452 return isl_aff_free(aff
));
2454 aff
= isl_aff_cow(aff
);
2458 aff
->ls
= isl_local_space_drop_dims(aff
->ls
, type
, first
, n
);
2460 return isl_aff_free(aff
);
2462 first
+= 1 + isl_local_space_offset(aff
->ls
, type
);
2463 aff
->v
= isl_vec_drop_els(aff
->v
, first
, n
);
2465 return isl_aff_free(aff
);
2470 /* Drop the "n" domain dimensions starting at "first" from "aff",
2471 * after checking that they do not appear in the affine expression.
2473 static __isl_give isl_aff
*drop_domain(__isl_take isl_aff
*aff
, unsigned first
,
2478 involves
= isl_aff_involves_dims(aff
, isl_dim_in
, first
, n
);
2480 return isl_aff_free(aff
);
2482 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2483 "affine expression involves some of the domain dimensions",
2484 return isl_aff_free(aff
));
2485 return isl_aff_drop_dims(aff
, isl_dim_in
, first
, n
);
2488 /* Project the domain of the affine expression onto its parameter space.
2489 * The affine expression may not involve any of the domain dimensions.
2491 __isl_give isl_aff
*isl_aff_project_domain_on_params(__isl_take isl_aff
*aff
)
2496 n
= isl_aff_dim(aff
, isl_dim_in
);
2497 aff
= drop_domain(aff
, 0, n
);
2498 space
= isl_aff_get_domain_space(aff
);
2499 space
= isl_space_params(space
);
2500 aff
= isl_aff_reset_domain_space(aff
, space
);
2504 /* Check that the domain of "aff" is a product.
2506 static isl_stat
check_domain_product(__isl_keep isl_aff
*aff
)
2508 isl_bool is_product
;
2510 is_product
= isl_space_is_product(isl_aff_peek_domain_space(aff
));
2512 return isl_stat_error
;
2514 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2515 "domain is not a product", return isl_stat_error
);
2519 /* Given an affine function with a domain of the form [A -> B] that
2520 * does not depend on B, return the same function on domain A.
2522 __isl_give isl_aff
*isl_aff_domain_factor_domain(__isl_take isl_aff
*aff
)
2527 if (check_domain_product(aff
) < 0)
2528 return isl_aff_free(aff
);
2529 space
= isl_aff_get_domain_space(aff
);
2530 n
= isl_space_dim(space
, isl_dim_set
);
2531 space
= isl_space_factor_domain(space
);
2532 n_in
= isl_space_dim(space
, isl_dim_set
);
2533 aff
= drop_domain(aff
, n_in
, n
- n_in
);
2534 aff
= isl_aff_reset_domain_space(aff
, space
);
2538 /* Convert an affine expression defined over a parameter domain
2539 * into one that is defined over a zero-dimensional set.
2541 __isl_give isl_aff
*isl_aff_from_range(__isl_take isl_aff
*aff
)
2543 isl_local_space
*ls
;
2545 ls
= isl_aff_take_domain_local_space(aff
);
2546 ls
= isl_local_space_set_from_params(ls
);
2547 aff
= isl_aff_restore_domain_local_space(aff
, ls
);
2552 __isl_give isl_aff
*isl_aff_insert_dims(__isl_take isl_aff
*aff
,
2553 enum isl_dim_type type
, unsigned first
, unsigned n
)
2559 if (type
== isl_dim_out
)
2560 isl_die(aff
->v
->ctx
, isl_error_invalid
,
2561 "cannot insert output/set dimensions",
2562 return isl_aff_free(aff
));
2563 if (type
== isl_dim_in
)
2565 if (n
== 0 && !isl_local_space_is_named_or_nested(aff
->ls
, type
))
2568 ctx
= isl_aff_get_ctx(aff
);
2569 if (first
> isl_local_space_dim(aff
->ls
, type
))
2570 isl_die(ctx
, isl_error_invalid
, "position out of bounds",
2571 return isl_aff_free(aff
));
2573 aff
= isl_aff_cow(aff
);
2577 aff
->ls
= isl_local_space_insert_dims(aff
->ls
, type
, first
, n
);
2579 return isl_aff_free(aff
);
2581 first
+= 1 + isl_local_space_offset(aff
->ls
, type
);
2582 aff
->v
= isl_vec_insert_zero_els(aff
->v
, first
, n
);
2584 return isl_aff_free(aff
);
2589 __isl_give isl_aff
*isl_aff_add_dims(__isl_take isl_aff
*aff
,
2590 enum isl_dim_type type
, unsigned n
)
2594 pos
= isl_aff_dim(aff
, type
);
2596 return isl_aff_insert_dims(aff
, type
, pos
, n
);
2599 __isl_give isl_pw_aff
*isl_pw_aff_add_dims(__isl_take isl_pw_aff
*pwaff
,
2600 enum isl_dim_type type
, unsigned n
)
2604 pos
= isl_pw_aff_dim(pwaff
, type
);
2606 return isl_pw_aff_insert_dims(pwaff
, type
, pos
, n
);
2609 /* Move the "n" dimensions of "src_type" starting at "src_pos" of "aff"
2610 * to dimensions of "dst_type" at "dst_pos".
2612 * We only support moving input dimensions to parameters and vice versa.
2614 __isl_give isl_aff
*isl_aff_move_dims(__isl_take isl_aff
*aff
,
2615 enum isl_dim_type dst_type
, unsigned dst_pos
,
2616 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
2624 !isl_local_space_is_named_or_nested(aff
->ls
, src_type
) &&
2625 !isl_local_space_is_named_or_nested(aff
->ls
, dst_type
))
2628 if (dst_type
== isl_dim_out
|| src_type
== isl_dim_out
)
2629 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2630 "cannot move output/set dimension",
2631 return isl_aff_free(aff
));
2632 if (dst_type
== isl_dim_div
|| src_type
== isl_dim_div
)
2633 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2634 "cannot move divs", return isl_aff_free(aff
));
2635 if (dst_type
== isl_dim_in
)
2636 dst_type
= isl_dim_set
;
2637 if (src_type
== isl_dim_in
)
2638 src_type
= isl_dim_set
;
2640 if (src_pos
+ n
> isl_local_space_dim(aff
->ls
, src_type
))
2641 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2642 "range out of bounds", return isl_aff_free(aff
));
2643 if (dst_type
== src_type
)
2644 isl_die(isl_aff_get_ctx(aff
), isl_error_unsupported
,
2645 "moving dims within the same type not supported",
2646 return isl_aff_free(aff
));
2648 aff
= isl_aff_cow(aff
);
2652 g_src_pos
= 1 + isl_local_space_offset(aff
->ls
, src_type
) + src_pos
;
2653 g_dst_pos
= 1 + isl_local_space_offset(aff
->ls
, dst_type
) + dst_pos
;
2654 if (dst_type
> src_type
)
2657 aff
->v
= isl_vec_move_els(aff
->v
, g_dst_pos
, g_src_pos
, n
);
2658 aff
->ls
= isl_local_space_move_dims(aff
->ls
, dst_type
, dst_pos
,
2659 src_type
, src_pos
, n
);
2660 if (!aff
->v
|| !aff
->ls
)
2661 return isl_aff_free(aff
);
2663 aff
= sort_divs(aff
);
2668 __isl_give isl_pw_aff
*isl_pw_aff_from_aff(__isl_take isl_aff
*aff
)
2670 isl_set
*dom
= isl_set_universe(isl_aff_get_domain_space(aff
));
2671 return isl_pw_aff_alloc(dom
, aff
);
2674 #define isl_aff_involves_nan isl_aff_is_nan
2677 #define PW isl_pw_aff
2681 #define EL_IS_ZERO is_empty
2685 #define IS_ZERO is_empty
2688 #undef DEFAULT_IS_ZERO
2689 #define DEFAULT_IS_ZERO 0
2695 #include <isl_pw_templ.c>
2696 #include <isl_pw_eval.c>
2697 #include <isl_pw_hash.c>
2698 #include <isl_pw_union_opt.c>
2703 #include <isl_union_single.c>
2704 #include <isl_union_neg.c>
2706 static __isl_give isl_set
*align_params_pw_pw_set_and(
2707 __isl_take isl_pw_aff
*pwaff1
, __isl_take isl_pw_aff
*pwaff2
,
2708 __isl_give isl_set
*(*fn
)(__isl_take isl_pw_aff
*pwaff1
,
2709 __isl_take isl_pw_aff
*pwaff2
))
2711 isl_bool equal_params
;
2713 if (!pwaff1
|| !pwaff2
)
2715 equal_params
= isl_space_has_equal_params(pwaff1
->dim
, pwaff2
->dim
);
2716 if (equal_params
< 0)
2719 return fn(pwaff1
, pwaff2
);
2720 if (isl_pw_aff_check_named_params(pwaff1
) < 0 ||
2721 isl_pw_aff_check_named_params(pwaff2
) < 0)
2723 pwaff1
= isl_pw_aff_align_params(pwaff1
, isl_pw_aff_get_space(pwaff2
));
2724 pwaff2
= isl_pw_aff_align_params(pwaff2
, isl_pw_aff_get_space(pwaff1
));
2725 return fn(pwaff1
, pwaff2
);
2727 isl_pw_aff_free(pwaff1
);
2728 isl_pw_aff_free(pwaff2
);
2732 /* Align the parameters of the to isl_pw_aff arguments and
2733 * then apply a function "fn" on them that returns an isl_map.
2735 static __isl_give isl_map
*align_params_pw_pw_map_and(
2736 __isl_take isl_pw_aff
*pa1
, __isl_take isl_pw_aff
*pa2
,
2737 __isl_give isl_map
*(*fn
)(__isl_take isl_pw_aff
*pa1
,
2738 __isl_take isl_pw_aff
*pa2
))
2740 isl_bool equal_params
;
2744 equal_params
= isl_space_has_equal_params(pa1
->dim
, pa2
->dim
);
2745 if (equal_params
< 0)
2748 return fn(pa1
, pa2
);
2749 if (isl_pw_aff_check_named_params(pa1
) < 0 ||
2750 isl_pw_aff_check_named_params(pa2
) < 0)
2752 pa1
= isl_pw_aff_align_params(pa1
, isl_pw_aff_get_space(pa2
));
2753 pa2
= isl_pw_aff_align_params(pa2
, isl_pw_aff_get_space(pa1
));
2754 return fn(pa1
, pa2
);
2756 isl_pw_aff_free(pa1
);
2757 isl_pw_aff_free(pa2
);
2761 /* Compute a piecewise quasi-affine expression with a domain that
2762 * is the union of those of pwaff1 and pwaff2 and such that on each
2763 * cell, the quasi-affine expression is the maximum of those of pwaff1
2764 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2765 * cell, then the associated expression is the defined one.
2767 static __isl_give isl_pw_aff
*pw_aff_union_max(__isl_take isl_pw_aff
*pwaff1
,
2768 __isl_take isl_pw_aff
*pwaff2
)
2770 return isl_pw_aff_union_opt_cmp(pwaff1
, pwaff2
, &isl_aff_ge_set
);
2773 __isl_give isl_pw_aff
*isl_pw_aff_union_max(__isl_take isl_pw_aff
*pwaff1
,
2774 __isl_take isl_pw_aff
*pwaff2
)
2776 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
,
2780 /* Compute a piecewise quasi-affine expression with a domain that
2781 * is the union of those of pwaff1 and pwaff2 and such that on each
2782 * cell, the quasi-affine expression is the minimum of those of pwaff1
2783 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2784 * cell, then the associated expression is the defined one.
2786 static __isl_give isl_pw_aff
*pw_aff_union_min(__isl_take isl_pw_aff
*pwaff1
,
2787 __isl_take isl_pw_aff
*pwaff2
)
2789 return isl_pw_aff_union_opt_cmp(pwaff1
, pwaff2
, &isl_aff_le_set
);
2792 __isl_give isl_pw_aff
*isl_pw_aff_union_min(__isl_take isl_pw_aff
*pwaff1
,
2793 __isl_take isl_pw_aff
*pwaff2
)
2795 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
,
2799 __isl_give isl_pw_aff
*isl_pw_aff_union_opt(__isl_take isl_pw_aff
*pwaff1
,
2800 __isl_take isl_pw_aff
*pwaff2
, int max
)
2803 return isl_pw_aff_union_max(pwaff1
, pwaff2
);
2805 return isl_pw_aff_union_min(pwaff1
, pwaff2
);
2808 /* Construct a map with as domain the domain of pwaff and
2809 * one-dimensional range corresponding to the affine expressions.
2811 static __isl_give isl_map
*map_from_pw_aff(__isl_take isl_pw_aff
*pwaff
)
2820 space
= isl_pw_aff_get_space(pwaff
);
2821 map
= isl_map_empty(space
);
2823 for (i
= 0; i
< pwaff
->n
; ++i
) {
2824 isl_basic_map
*bmap
;
2827 bmap
= isl_basic_map_from_aff(isl_aff_copy(pwaff
->p
[i
].aff
));
2828 map_i
= isl_map_from_basic_map(bmap
);
2829 map_i
= isl_map_intersect_domain(map_i
,
2830 isl_set_copy(pwaff
->p
[i
].set
));
2831 map
= isl_map_union_disjoint(map
, map_i
);
2834 isl_pw_aff_free(pwaff
);
2839 /* Construct a map with as domain the domain of pwaff and
2840 * one-dimensional range corresponding to the affine expressions.
2842 __isl_give isl_map
*isl_map_from_pw_aff(__isl_take isl_pw_aff
*pwaff
)
2846 if (isl_space_is_set(pwaff
->dim
))
2847 isl_die(isl_pw_aff_get_ctx(pwaff
), isl_error_invalid
,
2848 "space of input is not a map", goto error
);
2849 return map_from_pw_aff(pwaff
);
2851 isl_pw_aff_free(pwaff
);
2855 /* Construct a one-dimensional set with as parameter domain
2856 * the domain of pwaff and the single set dimension
2857 * corresponding to the affine expressions.
2859 __isl_give isl_set
*isl_set_from_pw_aff(__isl_take isl_pw_aff
*pwaff
)
2863 if (!isl_space_is_set(pwaff
->dim
))
2864 isl_die(isl_pw_aff_get_ctx(pwaff
), isl_error_invalid
,
2865 "space of input is not a set", goto error
);
2866 return set_from_map(map_from_pw_aff(pwaff
));
2868 isl_pw_aff_free(pwaff
);
2872 /* Return a set containing those elements in the domain
2873 * of "pwaff" where it satisfies "fn" (if complement is 0) or
2874 * does not satisfy "fn" (if complement is 1).
2876 * The pieces with a NaN never belong to the result since
2877 * NaN does not satisfy any property.
2879 static __isl_give isl_set
*pw_aff_locus(__isl_take isl_pw_aff
*pwaff
,
2880 __isl_give isl_basic_set
*(*fn
)(__isl_take isl_aff
*aff
, int rational
),
2889 set
= isl_set_empty(isl_pw_aff_get_domain_space(pwaff
));
2891 for (i
= 0; i
< pwaff
->n
; ++i
) {
2892 isl_basic_set
*bset
;
2893 isl_set
*set_i
, *locus
;
2896 if (isl_aff_is_nan(pwaff
->p
[i
].aff
))
2899 rational
= isl_set_has_rational(pwaff
->p
[i
].set
);
2900 bset
= fn(isl_aff_copy(pwaff
->p
[i
].aff
), rational
);
2901 locus
= isl_set_from_basic_set(bset
);
2902 set_i
= isl_set_copy(pwaff
->p
[i
].set
);
2904 set_i
= isl_set_subtract(set_i
, locus
);
2906 set_i
= isl_set_intersect(set_i
, locus
);
2907 set
= isl_set_union_disjoint(set
, set_i
);
2910 isl_pw_aff_free(pwaff
);
2915 /* Return a set containing those elements in the domain
2916 * of "pa" where it is positive.
2918 __isl_give isl_set
*isl_pw_aff_pos_set(__isl_take isl_pw_aff
*pa
)
2920 return pw_aff_locus(pa
, &aff_pos_basic_set
, 0);
2923 /* Return a set containing those elements in the domain
2924 * of pwaff where it is non-negative.
2926 __isl_give isl_set
*isl_pw_aff_nonneg_set(__isl_take isl_pw_aff
*pwaff
)
2928 return pw_aff_locus(pwaff
, &aff_nonneg_basic_set
, 0);
2931 /* Return a set containing those elements in the domain
2932 * of pwaff where it is zero.
2934 __isl_give isl_set
*isl_pw_aff_zero_set(__isl_take isl_pw_aff
*pwaff
)
2936 return pw_aff_locus(pwaff
, &aff_zero_basic_set
, 0);
2939 /* Return a set containing those elements in the domain
2940 * of pwaff where it is not zero.
2942 __isl_give isl_set
*isl_pw_aff_non_zero_set(__isl_take isl_pw_aff
*pwaff
)
2944 return pw_aff_locus(pwaff
, &aff_zero_basic_set
, 1);
2947 /* Return a set containing those elements in the shared domain
2948 * of pwaff1 and pwaff2 where pwaff1 is greater than (or equal) to pwaff2.
2950 * We compute the difference on the shared domain and then construct
2951 * the set of values where this difference is non-negative.
2952 * If strict is set, we first subtract 1 from the difference.
2953 * If equal is set, we only return the elements where pwaff1 and pwaff2
2956 static __isl_give isl_set
*pw_aff_gte_set(__isl_take isl_pw_aff
*pwaff1
,
2957 __isl_take isl_pw_aff
*pwaff2
, int strict
, int equal
)
2959 isl_set
*set1
, *set2
;
2961 set1
= isl_pw_aff_domain(isl_pw_aff_copy(pwaff1
));
2962 set2
= isl_pw_aff_domain(isl_pw_aff_copy(pwaff2
));
2963 set1
= isl_set_intersect(set1
, set2
);
2964 pwaff1
= isl_pw_aff_intersect_domain(pwaff1
, isl_set_copy(set1
));
2965 pwaff2
= isl_pw_aff_intersect_domain(pwaff2
, isl_set_copy(set1
));
2966 pwaff1
= isl_pw_aff_add(pwaff1
, isl_pw_aff_neg(pwaff2
));
2969 isl_space
*dim
= isl_set_get_space(set1
);
2971 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(dim
));
2972 aff
= isl_aff_add_constant_si(aff
, -1);
2973 pwaff1
= isl_pw_aff_add(pwaff1
, isl_pw_aff_alloc(set1
, aff
));
2978 return isl_pw_aff_zero_set(pwaff1
);
2979 return isl_pw_aff_nonneg_set(pwaff1
);
2982 /* Return a set containing those elements in the shared domain
2983 * of pwaff1 and pwaff2 where pwaff1 is equal to pwaff2.
2985 static __isl_give isl_set
*pw_aff_eq_set(__isl_take isl_pw_aff
*pwaff1
,
2986 __isl_take isl_pw_aff
*pwaff2
)
2988 return pw_aff_gte_set(pwaff1
, pwaff2
, 0, 1);
2991 __isl_give isl_set
*isl_pw_aff_eq_set(__isl_take isl_pw_aff
*pwaff1
,
2992 __isl_take isl_pw_aff
*pwaff2
)
2994 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_eq_set
);
2997 /* Return a set containing those elements in the shared domain
2998 * of pwaff1 and pwaff2 where pwaff1 is greater than or equal to pwaff2.
3000 static __isl_give isl_set
*pw_aff_ge_set(__isl_take isl_pw_aff
*pwaff1
,
3001 __isl_take isl_pw_aff
*pwaff2
)
3003 return pw_aff_gte_set(pwaff1
, pwaff2
, 0, 0);
3006 __isl_give isl_set
*isl_pw_aff_ge_set(__isl_take isl_pw_aff
*pwaff1
,
3007 __isl_take isl_pw_aff
*pwaff2
)
3009 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_ge_set
);
3012 /* Return a set containing those elements in the shared domain
3013 * of pwaff1 and pwaff2 where pwaff1 is strictly greater than pwaff2.
3015 static __isl_give isl_set
*pw_aff_gt_set(__isl_take isl_pw_aff
*pwaff1
,
3016 __isl_take isl_pw_aff
*pwaff2
)
3018 return pw_aff_gte_set(pwaff1
, pwaff2
, 1, 0);
3021 __isl_give isl_set
*isl_pw_aff_gt_set(__isl_take isl_pw_aff
*pwaff1
,
3022 __isl_take isl_pw_aff
*pwaff2
)
3024 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_gt_set
);
3027 __isl_give isl_set
*isl_pw_aff_le_set(__isl_take isl_pw_aff
*pwaff1
,
3028 __isl_take isl_pw_aff
*pwaff2
)
3030 return isl_pw_aff_ge_set(pwaff2
, pwaff1
);
3033 __isl_give isl_set
*isl_pw_aff_lt_set(__isl_take isl_pw_aff
*pwaff1
,
3034 __isl_take isl_pw_aff
*pwaff2
)
3036 return isl_pw_aff_gt_set(pwaff2
, pwaff1
);
3039 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3040 * where the function values are ordered in the same way as "order",
3041 * which returns a set in the shared domain of its two arguments.
3042 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3044 * Let "pa1" and "pa2" be defined on domains A and B respectively.
3045 * We first pull back the two functions such that they are defined on
3046 * the domain [A -> B]. Then we apply "order", resulting in a set
3047 * in the space [A -> B]. Finally, we unwrap this set to obtain
3048 * a map in the space A -> B.
3050 static __isl_give isl_map
*isl_pw_aff_order_map_aligned(
3051 __isl_take isl_pw_aff
*pa1
, __isl_take isl_pw_aff
*pa2
,
3052 __isl_give isl_set
*(*order
)(__isl_take isl_pw_aff
*pa1
,
3053 __isl_take isl_pw_aff
*pa2
))
3055 isl_space
*space1
, *space2
;
3059 space1
= isl_space_domain(isl_pw_aff_get_space(pa1
));
3060 space2
= isl_space_domain(isl_pw_aff_get_space(pa2
));
3061 space1
= isl_space_map_from_domain_and_range(space1
, space2
);
3062 ma
= isl_multi_aff_domain_map(isl_space_copy(space1
));
3063 pa1
= isl_pw_aff_pullback_multi_aff(pa1
, ma
);
3064 ma
= isl_multi_aff_range_map(space1
);
3065 pa2
= isl_pw_aff_pullback_multi_aff(pa2
, ma
);
3066 set
= order(pa1
, pa2
);
3068 return isl_set_unwrap(set
);
3071 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3072 * where the function values are equal.
3073 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3075 static __isl_give isl_map
*isl_pw_aff_eq_map_aligned(__isl_take isl_pw_aff
*pa1
,
3076 __isl_take isl_pw_aff
*pa2
)
3078 return isl_pw_aff_order_map_aligned(pa1
, pa2
, &isl_pw_aff_eq_set
);
3081 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3082 * where the function values are equal.
3084 __isl_give isl_map
*isl_pw_aff_eq_map(__isl_take isl_pw_aff
*pa1
,
3085 __isl_take isl_pw_aff
*pa2
)
3087 return align_params_pw_pw_map_and(pa1
, pa2
, &isl_pw_aff_eq_map_aligned
);
3090 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3091 * where the function value of "pa1" is less than the function value of "pa2".
3092 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3094 static __isl_give isl_map
*isl_pw_aff_lt_map_aligned(__isl_take isl_pw_aff
*pa1
,
3095 __isl_take isl_pw_aff
*pa2
)
3097 return isl_pw_aff_order_map_aligned(pa1
, pa2
, &isl_pw_aff_lt_set
);
3100 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3101 * where the function value of "pa1" is less than the function value of "pa2".
3103 __isl_give isl_map
*isl_pw_aff_lt_map(__isl_take isl_pw_aff
*pa1
,
3104 __isl_take isl_pw_aff
*pa2
)
3106 return align_params_pw_pw_map_and(pa1
, pa2
, &isl_pw_aff_lt_map_aligned
);
3109 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3110 * where the function value of "pa1" is greater than the function value
3112 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3114 static __isl_give isl_map
*isl_pw_aff_gt_map_aligned(__isl_take isl_pw_aff
*pa1
,
3115 __isl_take isl_pw_aff
*pa2
)
3117 return isl_pw_aff_order_map_aligned(pa1
, pa2
, &isl_pw_aff_gt_set
);
3120 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3121 * where the function value of "pa1" is greater than the function value
3124 __isl_give isl_map
*isl_pw_aff_gt_map(__isl_take isl_pw_aff
*pa1
,
3125 __isl_take isl_pw_aff
*pa2
)
3127 return align_params_pw_pw_map_and(pa1
, pa2
, &isl_pw_aff_gt_map_aligned
);
3130 /* Return a set containing those elements in the shared domain
3131 * of the elements of list1 and list2 where each element in list1
3132 * has the relation specified by "fn" with each element in list2.
3134 static __isl_give isl_set
*pw_aff_list_set(__isl_take isl_pw_aff_list
*list1
,
3135 __isl_take isl_pw_aff_list
*list2
,
3136 __isl_give isl_set
*(*fn
)(__isl_take isl_pw_aff
*pwaff1
,
3137 __isl_take isl_pw_aff
*pwaff2
))
3143 if (!list1
|| !list2
)
3146 ctx
= isl_pw_aff_list_get_ctx(list1
);
3147 if (list1
->n
< 1 || list2
->n
< 1)
3148 isl_die(ctx
, isl_error_invalid
,
3149 "list should contain at least one element", goto error
);
3151 set
= isl_set_universe(isl_pw_aff_get_domain_space(list1
->p
[0]));
3152 for (i
= 0; i
< list1
->n
; ++i
)
3153 for (j
= 0; j
< list2
->n
; ++j
) {
3156 set_ij
= fn(isl_pw_aff_copy(list1
->p
[i
]),
3157 isl_pw_aff_copy(list2
->p
[j
]));
3158 set
= isl_set_intersect(set
, set_ij
);
3161 isl_pw_aff_list_free(list1
);
3162 isl_pw_aff_list_free(list2
);
3165 isl_pw_aff_list_free(list1
);
3166 isl_pw_aff_list_free(list2
);
3170 /* Return a set containing those elements in the shared domain
3171 * of the elements of list1 and list2 where each element in list1
3172 * is equal to each element in list2.
3174 __isl_give isl_set
*isl_pw_aff_list_eq_set(__isl_take isl_pw_aff_list
*list1
,
3175 __isl_take isl_pw_aff_list
*list2
)
3177 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_eq_set
);
3180 __isl_give isl_set
*isl_pw_aff_list_ne_set(__isl_take isl_pw_aff_list
*list1
,
3181 __isl_take isl_pw_aff_list
*list2
)
3183 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_ne_set
);
3186 /* Return a set containing those elements in the shared domain
3187 * of the elements of list1 and list2 where each element in list1
3188 * is less than or equal to each element in list2.
3190 __isl_give isl_set
*isl_pw_aff_list_le_set(__isl_take isl_pw_aff_list
*list1
,
3191 __isl_take isl_pw_aff_list
*list2
)
3193 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_le_set
);
3196 __isl_give isl_set
*isl_pw_aff_list_lt_set(__isl_take isl_pw_aff_list
*list1
,
3197 __isl_take isl_pw_aff_list
*list2
)
3199 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_lt_set
);
3202 __isl_give isl_set
*isl_pw_aff_list_ge_set(__isl_take isl_pw_aff_list
*list1
,
3203 __isl_take isl_pw_aff_list
*list2
)
3205 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_ge_set
);
3208 __isl_give isl_set
*isl_pw_aff_list_gt_set(__isl_take isl_pw_aff_list
*list1
,
3209 __isl_take isl_pw_aff_list
*list2
)
3211 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_gt_set
);
3215 /* Return a set containing those elements in the shared domain
3216 * of pwaff1 and pwaff2 where pwaff1 is not equal to pwaff2.
3218 static __isl_give isl_set
*pw_aff_ne_set(__isl_take isl_pw_aff
*pwaff1
,
3219 __isl_take isl_pw_aff
*pwaff2
)
3221 isl_set
*set_lt
, *set_gt
;
3223 set_lt
= isl_pw_aff_lt_set(isl_pw_aff_copy(pwaff1
),
3224 isl_pw_aff_copy(pwaff2
));
3225 set_gt
= isl_pw_aff_gt_set(pwaff1
, pwaff2
);
3226 return isl_set_union_disjoint(set_lt
, set_gt
);
3229 __isl_give isl_set
*isl_pw_aff_ne_set(__isl_take isl_pw_aff
*pwaff1
,
3230 __isl_take isl_pw_aff
*pwaff2
)
3232 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_ne_set
);
3235 __isl_give isl_pw_aff
*isl_pw_aff_scale_down(__isl_take isl_pw_aff
*pwaff
,
3240 if (isl_int_is_one(v
))
3242 if (!isl_int_is_pos(v
))
3243 isl_die(isl_pw_aff_get_ctx(pwaff
), isl_error_invalid
,
3244 "factor needs to be positive",
3245 return isl_pw_aff_free(pwaff
));
3246 pwaff
= isl_pw_aff_cow(pwaff
);
3252 for (i
= 0; i
< pwaff
->n
; ++i
) {
3253 pwaff
->p
[i
].aff
= isl_aff_scale_down(pwaff
->p
[i
].aff
, v
);
3254 if (!pwaff
->p
[i
].aff
)
3255 return isl_pw_aff_free(pwaff
);
3261 __isl_give isl_pw_aff
*isl_pw_aff_floor(__isl_take isl_pw_aff
*pwaff
)
3265 pwaff
= isl_pw_aff_cow(pwaff
);
3271 for (i
= 0; i
< pwaff
->n
; ++i
) {
3272 pwaff
->p
[i
].aff
= isl_aff_floor(pwaff
->p
[i
].aff
);
3273 if (!pwaff
->p
[i
].aff
)
3274 return isl_pw_aff_free(pwaff
);
3280 __isl_give isl_pw_aff
*isl_pw_aff_ceil(__isl_take isl_pw_aff
*pwaff
)
3284 pwaff
= isl_pw_aff_cow(pwaff
);
3290 for (i
= 0; i
< pwaff
->n
; ++i
) {
3291 pwaff
->p
[i
].aff
= isl_aff_ceil(pwaff
->p
[i
].aff
);
3292 if (!pwaff
->p
[i
].aff
)
3293 return isl_pw_aff_free(pwaff
);
3299 /* Assuming that "cond1" and "cond2" are disjoint,
3300 * return an affine expression that is equal to pwaff1 on cond1
3301 * and to pwaff2 on cond2.
3303 static __isl_give isl_pw_aff
*isl_pw_aff_select(
3304 __isl_take isl_set
*cond1
, __isl_take isl_pw_aff
*pwaff1
,
3305 __isl_take isl_set
*cond2
, __isl_take isl_pw_aff
*pwaff2
)
3307 pwaff1
= isl_pw_aff_intersect_domain(pwaff1
, cond1
);
3308 pwaff2
= isl_pw_aff_intersect_domain(pwaff2
, cond2
);
3310 return isl_pw_aff_add_disjoint(pwaff1
, pwaff2
);
3313 /* Return an affine expression that is equal to pwaff_true for elements
3314 * where "cond" is non-zero and to pwaff_false for elements where "cond"
3316 * That is, return cond ? pwaff_true : pwaff_false;
3318 * If "cond" involves and NaN, then we conservatively return a NaN
3319 * on its entire domain. In principle, we could consider the pieces
3320 * where it is NaN separately from those where it is not.
3322 * If "pwaff_true" and "pwaff_false" are obviously equal to each other,
3323 * then only use the domain of "cond" to restrict the domain.
3325 __isl_give isl_pw_aff
*isl_pw_aff_cond(__isl_take isl_pw_aff
*cond
,
3326 __isl_take isl_pw_aff
*pwaff_true
, __isl_take isl_pw_aff
*pwaff_false
)
3328 isl_set
*cond_true
, *cond_false
;
3333 if (isl_pw_aff_involves_nan(cond
)) {
3334 isl_space
*space
= isl_pw_aff_get_domain_space(cond
);
3335 isl_local_space
*ls
= isl_local_space_from_space(space
);
3336 isl_pw_aff_free(cond
);
3337 isl_pw_aff_free(pwaff_true
);
3338 isl_pw_aff_free(pwaff_false
);
3339 return isl_pw_aff_nan_on_domain(ls
);
3342 pwaff_true
= isl_pw_aff_align_params(pwaff_true
,
3343 isl_pw_aff_get_space(pwaff_false
));
3344 pwaff_false
= isl_pw_aff_align_params(pwaff_false
,
3345 isl_pw_aff_get_space(pwaff_true
));
3346 equal
= isl_pw_aff_plain_is_equal(pwaff_true
, pwaff_false
);
3352 dom
= isl_set_coalesce(isl_pw_aff_domain(cond
));
3353 isl_pw_aff_free(pwaff_false
);
3354 return isl_pw_aff_intersect_domain(pwaff_true
, dom
);
3357 cond_true
= isl_pw_aff_non_zero_set(isl_pw_aff_copy(cond
));
3358 cond_false
= isl_pw_aff_zero_set(cond
);
3359 return isl_pw_aff_select(cond_true
, pwaff_true
,
3360 cond_false
, pwaff_false
);
3362 isl_pw_aff_free(cond
);
3363 isl_pw_aff_free(pwaff_true
);
3364 isl_pw_aff_free(pwaff_false
);
3368 isl_bool
isl_aff_is_cst(__isl_keep isl_aff
*aff
)
3371 return isl_bool_error
;
3373 return isl_seq_first_non_zero(aff
->v
->el
+ 2, aff
->v
->size
- 2) == -1;
3376 /* Check whether pwaff is a piecewise constant.
3378 isl_bool
isl_pw_aff_is_cst(__isl_keep isl_pw_aff
*pwaff
)
3383 return isl_bool_error
;
3385 for (i
= 0; i
< pwaff
->n
; ++i
) {
3386 isl_bool is_cst
= isl_aff_is_cst(pwaff
->p
[i
].aff
);
3387 if (is_cst
< 0 || !is_cst
)
3391 return isl_bool_true
;
3394 /* Are all elements of "mpa" piecewise constants?
3396 isl_bool
isl_multi_pw_aff_is_cst(__isl_keep isl_multi_pw_aff
*mpa
)
3401 return isl_bool_error
;
3403 for (i
= 0; i
< mpa
->n
; ++i
) {
3404 isl_bool is_cst
= isl_pw_aff_is_cst(mpa
->u
.p
[i
]);
3405 if (is_cst
< 0 || !is_cst
)
3409 return isl_bool_true
;
3412 /* Return the product of "aff1" and "aff2".
3414 * If either of the two is NaN, then the result is NaN.
3416 * Otherwise, at least one of "aff1" or "aff2" needs to be a constant.
3418 __isl_give isl_aff
*isl_aff_mul(__isl_take isl_aff
*aff1
,
3419 __isl_take isl_aff
*aff2
)
3424 if (isl_aff_is_nan(aff1
)) {
3428 if (isl_aff_is_nan(aff2
)) {
3433 if (!isl_aff_is_cst(aff2
) && isl_aff_is_cst(aff1
))
3434 return isl_aff_mul(aff2
, aff1
);
3436 if (!isl_aff_is_cst(aff2
))
3437 isl_die(isl_aff_get_ctx(aff1
), isl_error_invalid
,
3438 "at least one affine expression should be constant",
3441 aff1
= isl_aff_cow(aff1
);
3445 aff1
= isl_aff_scale(aff1
, aff2
->v
->el
[1]);
3446 aff1
= isl_aff_scale_down(aff1
, aff2
->v
->el
[0]);
3456 /* Divide "aff1" by "aff2", assuming "aff2" is a constant.
3458 * If either of the two is NaN, then the result is NaN.
3460 __isl_give isl_aff
*isl_aff_div(__isl_take isl_aff
*aff1
,
3461 __isl_take isl_aff
*aff2
)
3469 if (isl_aff_is_nan(aff1
)) {
3473 if (isl_aff_is_nan(aff2
)) {
3478 is_cst
= isl_aff_is_cst(aff2
);
3482 isl_die(isl_aff_get_ctx(aff2
), isl_error_invalid
,
3483 "second argument should be a constant", goto error
);
3488 neg
= isl_int_is_neg(aff2
->v
->el
[1]);
3490 isl_int_neg(aff2
->v
->el
[0], aff2
->v
->el
[0]);
3491 isl_int_neg(aff2
->v
->el
[1], aff2
->v
->el
[1]);
3494 aff1
= isl_aff_scale(aff1
, aff2
->v
->el
[0]);
3495 aff1
= isl_aff_scale_down(aff1
, aff2
->v
->el
[1]);
3498 isl_int_neg(aff2
->v
->el
[0], aff2
->v
->el
[0]);
3499 isl_int_neg(aff2
->v
->el
[1], aff2
->v
->el
[1]);
3510 static __isl_give isl_pw_aff
*pw_aff_add(__isl_take isl_pw_aff
*pwaff1
,
3511 __isl_take isl_pw_aff
*pwaff2
)
3513 return isl_pw_aff_on_shared_domain(pwaff1
, pwaff2
, &isl_aff_add
);
3516 __isl_give isl_pw_aff
*isl_pw_aff_add(__isl_take isl_pw_aff
*pwaff1
,
3517 __isl_take isl_pw_aff
*pwaff2
)
3519 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
, &pw_aff_add
);
3522 __isl_give isl_pw_aff
*isl_pw_aff_union_add(__isl_take isl_pw_aff
*pwaff1
,
3523 __isl_take isl_pw_aff
*pwaff2
)
3525 return isl_pw_aff_union_add_(pwaff1
, pwaff2
);
3528 static __isl_give isl_pw_aff
*pw_aff_mul(__isl_take isl_pw_aff
*pwaff1
,
3529 __isl_take isl_pw_aff
*pwaff2
)
3531 return isl_pw_aff_on_shared_domain(pwaff1
, pwaff2
, &isl_aff_mul
);
3534 __isl_give isl_pw_aff
*isl_pw_aff_mul(__isl_take isl_pw_aff
*pwaff1
,
3535 __isl_take isl_pw_aff
*pwaff2
)
3537 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
, &pw_aff_mul
);
3540 static __isl_give isl_pw_aff
*pw_aff_div(__isl_take isl_pw_aff
*pa1
,
3541 __isl_take isl_pw_aff
*pa2
)
3543 return isl_pw_aff_on_shared_domain(pa1
, pa2
, &isl_aff_div
);
3546 /* Divide "pa1" by "pa2", assuming "pa2" is a piecewise constant.
3548 __isl_give isl_pw_aff
*isl_pw_aff_div(__isl_take isl_pw_aff
*pa1
,
3549 __isl_take isl_pw_aff
*pa2
)
3553 is_cst
= isl_pw_aff_is_cst(pa2
);
3557 isl_die(isl_pw_aff_get_ctx(pa2
), isl_error_invalid
,
3558 "second argument should be a piecewise constant",
3560 return isl_pw_aff_align_params_pw_pw_and(pa1
, pa2
, &pw_aff_div
);
3562 isl_pw_aff_free(pa1
);
3563 isl_pw_aff_free(pa2
);
3567 /* Compute the quotient of the integer division of "pa1" by "pa2"
3568 * with rounding towards zero.
3569 * "pa2" is assumed to be a piecewise constant.
3571 * In particular, return
3573 * pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2)
3576 __isl_give isl_pw_aff
*isl_pw_aff_tdiv_q(__isl_take isl_pw_aff
*pa1
,
3577 __isl_take isl_pw_aff
*pa2
)
3583 is_cst
= isl_pw_aff_is_cst(pa2
);
3587 isl_die(isl_pw_aff_get_ctx(pa2
), isl_error_invalid
,
3588 "second argument should be a piecewise constant",
3591 pa1
= isl_pw_aff_div(pa1
, pa2
);
3593 cond
= isl_pw_aff_nonneg_set(isl_pw_aff_copy(pa1
));
3594 f
= isl_pw_aff_floor(isl_pw_aff_copy(pa1
));
3595 c
= isl_pw_aff_ceil(pa1
);
3596 return isl_pw_aff_cond(isl_set_indicator_function(cond
), f
, c
);
3598 isl_pw_aff_free(pa1
);
3599 isl_pw_aff_free(pa2
);
3603 /* Compute the remainder of the integer division of "pa1" by "pa2"
3604 * with rounding towards zero.
3605 * "pa2" is assumed to be a piecewise constant.
3607 * In particular, return
3609 * pa1 - pa2 * (pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2))
3612 __isl_give isl_pw_aff
*isl_pw_aff_tdiv_r(__isl_take isl_pw_aff
*pa1
,
3613 __isl_take isl_pw_aff
*pa2
)
3618 is_cst
= isl_pw_aff_is_cst(pa2
);
3622 isl_die(isl_pw_aff_get_ctx(pa2
), isl_error_invalid
,
3623 "second argument should be a piecewise constant",
3625 res
= isl_pw_aff_tdiv_q(isl_pw_aff_copy(pa1
), isl_pw_aff_copy(pa2
));
3626 res
= isl_pw_aff_mul(pa2
, res
);
3627 res
= isl_pw_aff_sub(pa1
, res
);
3630 isl_pw_aff_free(pa1
);
3631 isl_pw_aff_free(pa2
);
3635 /* Does either of "pa1" or "pa2" involve any NaN2?
3637 static isl_bool
either_involves_nan(__isl_keep isl_pw_aff
*pa1
,
3638 __isl_keep isl_pw_aff
*pa2
)
3642 has_nan
= isl_pw_aff_involves_nan(pa1
);
3643 if (has_nan
< 0 || has_nan
)
3645 return isl_pw_aff_involves_nan(pa2
);
3648 /* Replace "pa1" and "pa2" (at least one of which involves a NaN)
3649 * by a NaN on their shared domain.
3651 * In principle, the result could be refined to only being NaN
3652 * on the parts of this domain where at least one of "pa1" or "pa2" is NaN.
3654 static __isl_give isl_pw_aff
*replace_by_nan(__isl_take isl_pw_aff
*pa1
,
3655 __isl_take isl_pw_aff
*pa2
)
3657 isl_local_space
*ls
;
3661 dom
= isl_set_intersect(isl_pw_aff_domain(pa1
), isl_pw_aff_domain(pa2
));
3662 ls
= isl_local_space_from_space(isl_set_get_space(dom
));
3663 pa
= isl_pw_aff_nan_on_domain(ls
);
3664 pa
= isl_pw_aff_intersect_domain(pa
, dom
);
3669 static __isl_give isl_pw_aff
*pw_aff_min(__isl_take isl_pw_aff
*pwaff1
,
3670 __isl_take isl_pw_aff
*pwaff2
)
3675 dom
= isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1
)),
3676 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2
)));
3677 le
= isl_pw_aff_le_set(isl_pw_aff_copy(pwaff1
),
3678 isl_pw_aff_copy(pwaff2
));
3679 dom
= isl_set_subtract(dom
, isl_set_copy(le
));
3680 return isl_pw_aff_select(le
, pwaff1
, dom
, pwaff2
);
3683 static __isl_give isl_pw_aff
*pw_aff_max(__isl_take isl_pw_aff
*pwaff1
,
3684 __isl_take isl_pw_aff
*pwaff2
)
3689 dom
= isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1
)),
3690 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2
)));
3691 ge
= isl_pw_aff_ge_set(isl_pw_aff_copy(pwaff1
),
3692 isl_pw_aff_copy(pwaff2
));
3693 dom
= isl_set_subtract(dom
, isl_set_copy(ge
));
3694 return isl_pw_aff_select(ge
, pwaff1
, dom
, pwaff2
);
3697 /* Return an expression for the minimum (if "max" is not set) or
3698 * the maximum (if "max" is set) of "pa1" and "pa2".
3699 * If either expression involves any NaN, then return a NaN
3700 * on the shared domain as result.
3702 static __isl_give isl_pw_aff
*pw_aff_min_max(__isl_take isl_pw_aff
*pa1
,
3703 __isl_take isl_pw_aff
*pa2
, int max
)
3707 has_nan
= either_involves_nan(pa1
, pa2
);
3709 pa1
= isl_pw_aff_free(pa1
);
3711 return replace_by_nan(pa1
, pa2
);
3714 return isl_pw_aff_align_params_pw_pw_and(pa1
, pa2
, &pw_aff_max
);
3716 return isl_pw_aff_align_params_pw_pw_and(pa1
, pa2
, &pw_aff_min
);
3719 /* Return an expression for the minimum of "pwaff1" and "pwaff2".
3721 __isl_give isl_pw_aff
*isl_pw_aff_min(__isl_take isl_pw_aff
*pwaff1
,
3722 __isl_take isl_pw_aff
*pwaff2
)
3724 return pw_aff_min_max(pwaff1
, pwaff2
, 0);
3727 /* Return an expression for the maximum of "pwaff1" and "pwaff2".
3729 __isl_give isl_pw_aff
*isl_pw_aff_max(__isl_take isl_pw_aff
*pwaff1
,
3730 __isl_take isl_pw_aff
*pwaff2
)
3732 return pw_aff_min_max(pwaff1
, pwaff2
, 1);
3735 static __isl_give isl_pw_aff
*pw_aff_list_reduce(
3736 __isl_take isl_pw_aff_list
*list
,
3737 __isl_give isl_pw_aff
*(*fn
)(__isl_take isl_pw_aff
*pwaff1
,
3738 __isl_take isl_pw_aff
*pwaff2
))
3747 ctx
= isl_pw_aff_list_get_ctx(list
);
3749 isl_die(ctx
, isl_error_invalid
,
3750 "list should contain at least one element", goto error
);
3752 res
= isl_pw_aff_copy(list
->p
[0]);
3753 for (i
= 1; i
< list
->n
; ++i
)
3754 res
= fn(res
, isl_pw_aff_copy(list
->p
[i
]));
3756 isl_pw_aff_list_free(list
);
3759 isl_pw_aff_list_free(list
);
3763 /* Return an isl_pw_aff that maps each element in the intersection of the
3764 * domains of the elements of list to the minimal corresponding affine
3767 __isl_give isl_pw_aff
*isl_pw_aff_list_min(__isl_take isl_pw_aff_list
*list
)
3769 return pw_aff_list_reduce(list
, &isl_pw_aff_min
);
3772 /* Return an isl_pw_aff that maps each element in the intersection of the
3773 * domains of the elements of list to the maximal corresponding affine
3776 __isl_give isl_pw_aff
*isl_pw_aff_list_max(__isl_take isl_pw_aff_list
*list
)
3778 return pw_aff_list_reduce(list
, &isl_pw_aff_max
);
3781 /* Mark the domains of "pwaff" as rational.
3783 __isl_give isl_pw_aff
*isl_pw_aff_set_rational(__isl_take isl_pw_aff
*pwaff
)
3787 pwaff
= isl_pw_aff_cow(pwaff
);
3793 for (i
= 0; i
< pwaff
->n
; ++i
) {
3794 pwaff
->p
[i
].set
= isl_set_set_rational(pwaff
->p
[i
].set
);
3795 if (!pwaff
->p
[i
].set
)
3796 return isl_pw_aff_free(pwaff
);
3802 /* Mark the domains of the elements of "list" as rational.
3804 __isl_give isl_pw_aff_list
*isl_pw_aff_list_set_rational(
3805 __isl_take isl_pw_aff_list
*list
)
3815 for (i
= 0; i
< n
; ++i
) {
3818 pa
= isl_pw_aff_list_get_pw_aff(list
, i
);
3819 pa
= isl_pw_aff_set_rational(pa
);
3820 list
= isl_pw_aff_list_set_pw_aff(list
, i
, pa
);
3826 /* Do the parameters of "aff" match those of "space"?
3828 isl_bool
isl_aff_matching_params(__isl_keep isl_aff
*aff
,
3829 __isl_keep isl_space
*space
)
3831 isl_space
*aff_space
;
3835 return isl_bool_error
;
3837 aff_space
= isl_aff_get_domain_space(aff
);
3839 match
= isl_space_has_equal_params(space
, aff_space
);
3841 isl_space_free(aff_space
);
3845 /* Check that the domain space of "aff" matches "space".
3847 isl_stat
isl_aff_check_match_domain_space(__isl_keep isl_aff
*aff
,
3848 __isl_keep isl_space
*space
)
3850 isl_space
*aff_space
;
3854 return isl_stat_error
;
3856 aff_space
= isl_aff_get_domain_space(aff
);
3858 match
= isl_space_has_equal_params(space
, aff_space
);
3862 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
3863 "parameters don't match", goto error
);
3864 match
= isl_space_tuple_is_equal(space
, isl_dim_in
,
3865 aff_space
, isl_dim_set
);
3869 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
3870 "domains don't match", goto error
);
3871 isl_space_free(aff_space
);
3874 isl_space_free(aff_space
);
3875 return isl_stat_error
;
3884 #include <isl_multi_no_explicit_domain.c>
3885 #include <isl_multi_templ.c>
3886 #include <isl_multi_apply_set.c>
3887 #include <isl_multi_cmp.c>
3888 #include <isl_multi_dims.c>
3889 #include <isl_multi_floor.c>
3890 #include <isl_multi_gist.c>
3894 /* Construct an isl_multi_aff living in "space" that corresponds
3895 * to the affine transformation matrix "mat".
3897 __isl_give isl_multi_aff
*isl_multi_aff_from_aff_mat(
3898 __isl_take isl_space
*space
, __isl_take isl_mat
*mat
)
3901 isl_local_space
*ls
= NULL
;
3902 isl_multi_aff
*ma
= NULL
;
3903 int n_row
, n_col
, n_out
, total
;
3909 ctx
= isl_mat_get_ctx(mat
);
3911 n_row
= isl_mat_rows(mat
);
3912 n_col
= isl_mat_cols(mat
);
3914 isl_die(ctx
, isl_error_invalid
,
3915 "insufficient number of rows", goto error
);
3917 isl_die(ctx
, isl_error_invalid
,
3918 "insufficient number of columns", goto error
);
3919 n_out
= isl_space_dim(space
, isl_dim_out
);
3920 total
= isl_space_dim(space
, isl_dim_all
);
3921 if (1 + n_out
!= n_row
|| 2 + total
!= n_row
+ n_col
)
3922 isl_die(ctx
, isl_error_invalid
,
3923 "dimension mismatch", goto error
);
3925 ma
= isl_multi_aff_zero(isl_space_copy(space
));
3926 ls
= isl_local_space_from_space(isl_space_domain(space
));
3928 for (i
= 0; i
< n_row
- 1; ++i
) {
3932 v
= isl_vec_alloc(ctx
, 1 + n_col
);
3935 isl_int_set(v
->el
[0], mat
->row
[0][0]);
3936 isl_seq_cpy(v
->el
+ 1, mat
->row
[1 + i
], n_col
);
3937 v
= isl_vec_normalize(v
);
3938 aff
= isl_aff_alloc_vec(isl_local_space_copy(ls
), v
);
3939 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
3942 isl_local_space_free(ls
);
3946 isl_local_space_free(ls
);
3948 isl_multi_aff_free(ma
);
3952 /* Remove any internal structure of the domain of "ma".
3953 * If there is any such internal structure in the input,
3954 * then the name of the corresponding space is also removed.
3956 __isl_give isl_multi_aff
*isl_multi_aff_flatten_domain(
3957 __isl_take isl_multi_aff
*ma
)
3964 if (!ma
->space
->nested
[0])
3967 space
= isl_multi_aff_get_space(ma
);
3968 space
= isl_space_flatten_domain(space
);
3969 ma
= isl_multi_aff_reset_space(ma
, space
);
3974 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
3975 * of the space to its domain.
3977 __isl_give isl_multi_aff
*isl_multi_aff_domain_map(__isl_take isl_space
*space
)
3980 isl_local_space
*ls
;
3985 if (!isl_space_is_map(space
))
3986 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
3987 "not a map space", goto error
);
3989 n_in
= isl_space_dim(space
, isl_dim_in
);
3990 space
= isl_space_domain_map(space
);
3992 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
3994 isl_space_free(space
);
3998 space
= isl_space_domain(space
);
3999 ls
= isl_local_space_from_space(space
);
4000 for (i
= 0; i
< n_in
; ++i
) {
4003 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
4005 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
4007 isl_local_space_free(ls
);
4010 isl_space_free(space
);
4014 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
4015 * of the space to its range.
4017 __isl_give isl_multi_aff
*isl_multi_aff_range_map(__isl_take isl_space
*space
)
4020 isl_local_space
*ls
;
4025 if (!isl_space_is_map(space
))
4026 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
4027 "not a map space", goto error
);
4029 n_in
= isl_space_dim(space
, isl_dim_in
);
4030 n_out
= isl_space_dim(space
, isl_dim_out
);
4031 space
= isl_space_range_map(space
);
4033 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
4035 isl_space_free(space
);
4039 space
= isl_space_domain(space
);
4040 ls
= isl_local_space_from_space(space
);
4041 for (i
= 0; i
< n_out
; ++i
) {
4044 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
4045 isl_dim_set
, n_in
+ i
);
4046 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
4048 isl_local_space_free(ls
);
4051 isl_space_free(space
);
4055 /* Given a map space, return an isl_pw_multi_aff that maps a wrapped copy
4056 * of the space to its range.
4058 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_range_map(
4059 __isl_take isl_space
*space
)
4061 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_range_map(space
));
4064 /* Given the space of a set and a range of set dimensions,
4065 * construct an isl_multi_aff that projects out those dimensions.
4067 __isl_give isl_multi_aff
*isl_multi_aff_project_out_map(
4068 __isl_take isl_space
*space
, enum isl_dim_type type
,
4069 unsigned first
, unsigned n
)
4072 isl_local_space
*ls
;
4077 if (!isl_space_is_set(space
))
4078 isl_die(isl_space_get_ctx(space
), isl_error_unsupported
,
4079 "expecting set space", goto error
);
4080 if (type
!= isl_dim_set
)
4081 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
4082 "only set dimensions can be projected out", goto error
);
4084 dim
= isl_space_dim(space
, isl_dim_set
);
4085 if (first
+ n
> dim
)
4086 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
4087 "range out of bounds", goto error
);
4089 space
= isl_space_from_domain(space
);
4090 space
= isl_space_add_dims(space
, isl_dim_out
, dim
- n
);
4093 return isl_multi_aff_alloc(space
);
4095 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
4096 space
= isl_space_domain(space
);
4097 ls
= isl_local_space_from_space(space
);
4099 for (i
= 0; i
< first
; ++i
) {
4102 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
4104 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
4107 for (i
= 0; i
< dim
- (first
+ n
); ++i
) {
4110 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
4111 isl_dim_set
, first
+ n
+ i
);
4112 ma
= isl_multi_aff_set_aff(ma
, first
+ i
, aff
);
4115 isl_local_space_free(ls
);
4118 isl_space_free(space
);
4122 /* Given the space of a set and a range of set dimensions,
4123 * construct an isl_pw_multi_aff that projects out those dimensions.
4125 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_project_out_map(
4126 __isl_take isl_space
*space
, enum isl_dim_type type
,
4127 unsigned first
, unsigned n
)
4131 ma
= isl_multi_aff_project_out_map(space
, type
, first
, n
);
4132 return isl_pw_multi_aff_from_multi_aff(ma
);
4135 /* Create an isl_pw_multi_aff with the given isl_multi_aff on a universe
4138 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_multi_aff(
4139 __isl_take isl_multi_aff
*ma
)
4141 isl_set
*dom
= isl_set_universe(isl_multi_aff_get_domain_space(ma
));
4142 return isl_pw_multi_aff_alloc(dom
, ma
);
4145 /* Create a piecewise multi-affine expression in the given space that maps each
4146 * input dimension to the corresponding output dimension.
4148 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_identity(
4149 __isl_take isl_space
*space
)
4151 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_identity(space
));
4154 /* Exploit the equalities in "eq" to simplify the affine expressions.
4156 static __isl_give isl_multi_aff
*isl_multi_aff_substitute_equalities(
4157 __isl_take isl_multi_aff
*maff
, __isl_take isl_basic_set
*eq
)
4161 maff
= isl_multi_aff_cow(maff
);
4165 for (i
= 0; i
< maff
->n
; ++i
) {
4166 maff
->u
.p
[i
] = isl_aff_substitute_equalities(maff
->u
.p
[i
],
4167 isl_basic_set_copy(eq
));
4172 isl_basic_set_free(eq
);
4175 isl_basic_set_free(eq
);
4176 isl_multi_aff_free(maff
);
4180 __isl_give isl_multi_aff
*isl_multi_aff_scale(__isl_take isl_multi_aff
*maff
,
4185 maff
= isl_multi_aff_cow(maff
);
4189 for (i
= 0; i
< maff
->n
; ++i
) {
4190 maff
->u
.p
[i
] = isl_aff_scale(maff
->u
.p
[i
], f
);
4192 return isl_multi_aff_free(maff
);
4198 __isl_give isl_multi_aff
*isl_multi_aff_add_on_domain(__isl_keep isl_set
*dom
,
4199 __isl_take isl_multi_aff
*maff1
, __isl_take isl_multi_aff
*maff2
)
4201 maff1
= isl_multi_aff_add(maff1
, maff2
);
4202 maff1
= isl_multi_aff_gist(maff1
, isl_set_copy(dom
));
4206 int isl_multi_aff_is_empty(__isl_keep isl_multi_aff
*maff
)
4214 /* Return the set of domain elements where "ma1" is lexicographically
4215 * smaller than or equal to "ma2".
4217 __isl_give isl_set
*isl_multi_aff_lex_le_set(__isl_take isl_multi_aff
*ma1
,
4218 __isl_take isl_multi_aff
*ma2
)
4220 return isl_multi_aff_lex_ge_set(ma2
, ma1
);
4223 /* Return the set of domain elements where "ma1" is lexicographically
4224 * smaller than "ma2".
4226 __isl_give isl_set
*isl_multi_aff_lex_lt_set(__isl_take isl_multi_aff
*ma1
,
4227 __isl_take isl_multi_aff
*ma2
)
4229 return isl_multi_aff_lex_gt_set(ma2
, ma1
);
4232 /* Return the set of domain elements where "ma1" and "ma2"
4235 static __isl_give isl_set
*isl_multi_aff_order_set(
4236 __isl_take isl_multi_aff
*ma1
, __isl_take isl_multi_aff
*ma2
,
4237 __isl_give isl_map
*order(__isl_take isl_space
*set_space
))
4240 isl_map
*map1
, *map2
;
4243 map1
= isl_map_from_multi_aff(ma1
);
4244 map2
= isl_map_from_multi_aff(ma2
);
4245 map
= isl_map_range_product(map1
, map2
);
4246 space
= isl_space_range(isl_map_get_space(map
));
4247 space
= isl_space_domain(isl_space_unwrap(space
));
4249 map
= isl_map_intersect_range(map
, isl_map_wrap(ge
));
4251 return isl_map_domain(map
);
4254 /* Return the set of domain elements where "ma1" is lexicographically
4255 * greater than or equal to "ma2".
4257 __isl_give isl_set
*isl_multi_aff_lex_ge_set(__isl_take isl_multi_aff
*ma1
,
4258 __isl_take isl_multi_aff
*ma2
)
4260 return isl_multi_aff_order_set(ma1
, ma2
, &isl_map_lex_ge
);
4263 /* Return the set of domain elements where "ma1" is lexicographically
4264 * greater than "ma2".
4266 __isl_give isl_set
*isl_multi_aff_lex_gt_set(__isl_take isl_multi_aff
*ma1
,
4267 __isl_take isl_multi_aff
*ma2
)
4269 return isl_multi_aff_order_set(ma1
, ma2
, &isl_map_lex_gt
);
4273 #define PW isl_pw_multi_aff
4275 #define EL isl_multi_aff
4277 #define EL_IS_ZERO is_empty
4281 #define IS_ZERO is_empty
4284 #undef DEFAULT_IS_ZERO
4285 #define DEFAULT_IS_ZERO 0
4289 #define NO_INSERT_DIMS
4293 #include <isl_pw_templ.c>
4294 #include <isl_pw_union_opt.c>
4299 #define BASE pw_multi_aff
4301 #include <isl_union_multi.c>
4302 #include <isl_union_neg.c>
4304 static __isl_give isl_pw_multi_aff
*pw_multi_aff_union_lexmax(
4305 __isl_take isl_pw_multi_aff
*pma1
,
4306 __isl_take isl_pw_multi_aff
*pma2
)
4308 return isl_pw_multi_aff_union_opt_cmp(pma1
, pma2
,
4309 &isl_multi_aff_lex_ge_set
);
4312 /* Given two piecewise multi affine expressions, return a piecewise
4313 * multi-affine expression defined on the union of the definition domains
4314 * of the inputs that is equal to the lexicographic maximum of the two
4315 * inputs on each cell. If only one of the two inputs is defined on
4316 * a given cell, then it is considered to be the maximum.
4318 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_union_lexmax(
4319 __isl_take isl_pw_multi_aff
*pma1
,
4320 __isl_take isl_pw_multi_aff
*pma2
)
4322 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
4323 &pw_multi_aff_union_lexmax
);
4326 static __isl_give isl_pw_multi_aff
*pw_multi_aff_union_lexmin(
4327 __isl_take isl_pw_multi_aff
*pma1
,
4328 __isl_take isl_pw_multi_aff
*pma2
)
4330 return isl_pw_multi_aff_union_opt_cmp(pma1
, pma2
,
4331 &isl_multi_aff_lex_le_set
);
4334 /* Given two piecewise multi affine expressions, return a piecewise
4335 * multi-affine expression defined on the union of the definition domains
4336 * of the inputs that is equal to the lexicographic minimum of the two
4337 * inputs on each cell. If only one of the two inputs is defined on
4338 * a given cell, then it is considered to be the minimum.
4340 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_union_lexmin(
4341 __isl_take isl_pw_multi_aff
*pma1
,
4342 __isl_take isl_pw_multi_aff
*pma2
)
4344 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
4345 &pw_multi_aff_union_lexmin
);
4348 static __isl_give isl_pw_multi_aff
*pw_multi_aff_add(
4349 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4351 return isl_pw_multi_aff_on_shared_domain(pma1
, pma2
,
4352 &isl_multi_aff_add
);
4355 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_add(
4356 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4358 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
4362 static __isl_give isl_pw_multi_aff
*pw_multi_aff_sub(
4363 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4365 return isl_pw_multi_aff_on_shared_domain(pma1
, pma2
,
4366 &isl_multi_aff_sub
);
4369 /* Subtract "pma2" from "pma1" and return the result.
4371 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_sub(
4372 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4374 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
4378 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_union_add(
4379 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4381 return isl_pw_multi_aff_union_add_(pma1
, pma2
);
4384 /* Compute the sum of "upa1" and "upa2" on the union of their domains,
4385 * with the actual sum on the shared domain and
4386 * the defined expression on the symmetric difference of the domains.
4388 __isl_give isl_union_pw_aff
*isl_union_pw_aff_union_add(
4389 __isl_take isl_union_pw_aff
*upa1
, __isl_take isl_union_pw_aff
*upa2
)
4391 return isl_union_pw_aff_union_add_(upa1
, upa2
);
4394 /* Compute the sum of "upma1" and "upma2" on the union of their domains,
4395 * with the actual sum on the shared domain and
4396 * the defined expression on the symmetric difference of the domains.
4398 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_union_add(
4399 __isl_take isl_union_pw_multi_aff
*upma1
,
4400 __isl_take isl_union_pw_multi_aff
*upma2
)
4402 return isl_union_pw_multi_aff_union_add_(upma1
, upma2
);
4405 /* Given two piecewise multi-affine expressions A -> B and C -> D,
4406 * construct a piecewise multi-affine expression [A -> C] -> [B -> D].
4408 static __isl_give isl_pw_multi_aff
*pw_multi_aff_product(
4409 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4413 isl_pw_multi_aff
*res
;
4418 n
= pma1
->n
* pma2
->n
;
4419 space
= isl_space_product(isl_space_copy(pma1
->dim
),
4420 isl_space_copy(pma2
->dim
));
4421 res
= isl_pw_multi_aff_alloc_size(space
, n
);
4423 for (i
= 0; i
< pma1
->n
; ++i
) {
4424 for (j
= 0; j
< pma2
->n
; ++j
) {
4428 domain
= isl_set_product(isl_set_copy(pma1
->p
[i
].set
),
4429 isl_set_copy(pma2
->p
[j
].set
));
4430 ma
= isl_multi_aff_product(
4431 isl_multi_aff_copy(pma1
->p
[i
].maff
),
4432 isl_multi_aff_copy(pma2
->p
[j
].maff
));
4433 res
= isl_pw_multi_aff_add_piece(res
, domain
, ma
);
4437 isl_pw_multi_aff_free(pma1
);
4438 isl_pw_multi_aff_free(pma2
);
4441 isl_pw_multi_aff_free(pma1
);
4442 isl_pw_multi_aff_free(pma2
);
4446 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_product(
4447 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4449 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
4450 &pw_multi_aff_product
);
4453 /* Construct a map mapping the domain of the piecewise multi-affine expression
4454 * to its range, with each dimension in the range equated to the
4455 * corresponding affine expression on its cell.
4457 * If the domain of "pma" is rational, then so is the constructed "map".
4459 __isl_give isl_map
*isl_map_from_pw_multi_aff(__isl_take isl_pw_multi_aff
*pma
)
4467 map
= isl_map_empty(isl_pw_multi_aff_get_space(pma
));
4469 for (i
= 0; i
< pma
->n
; ++i
) {
4471 isl_multi_aff
*maff
;
4472 isl_basic_map
*bmap
;
4475 rational
= isl_set_is_rational(pma
->p
[i
].set
);
4477 map
= isl_map_free(map
);
4478 maff
= isl_multi_aff_copy(pma
->p
[i
].maff
);
4479 bmap
= isl_basic_map_from_multi_aff2(maff
, rational
);
4480 map_i
= isl_map_from_basic_map(bmap
);
4481 map_i
= isl_map_intersect_domain(map_i
,
4482 isl_set_copy(pma
->p
[i
].set
));
4483 map
= isl_map_union_disjoint(map
, map_i
);
4486 isl_pw_multi_aff_free(pma
);
4490 __isl_give isl_set
*isl_set_from_pw_multi_aff(__isl_take isl_pw_multi_aff
*pma
)
4495 if (!isl_space_is_set(pma
->dim
))
4496 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
4497 "isl_pw_multi_aff cannot be converted into an isl_set",
4500 return isl_map_from_pw_multi_aff(pma
);
4502 isl_pw_multi_aff_free(pma
);
4506 /* Subtract the initial "n" elements in "ma" with coefficients in "c" and
4507 * denominator "denom".
4508 * "denom" is allowed to be negative, in which case the actual denominator
4509 * is -denom and the expressions are added instead.
4511 static __isl_give isl_aff
*subtract_initial(__isl_take isl_aff
*aff
,
4512 __isl_keep isl_multi_aff
*ma
, int n
, isl_int
*c
, isl_int denom
)
4518 first
= isl_seq_first_non_zero(c
, n
);
4522 sign
= isl_int_sgn(denom
);
4524 isl_int_abs(d
, denom
);
4525 for (i
= first
; i
< n
; ++i
) {
4528 if (isl_int_is_zero(c
[i
]))
4530 aff_i
= isl_multi_aff_get_aff(ma
, i
);
4531 aff_i
= isl_aff_scale(aff_i
, c
[i
]);
4532 aff_i
= isl_aff_scale_down(aff_i
, d
);
4534 aff
= isl_aff_sub(aff
, aff_i
);
4536 aff
= isl_aff_add(aff
, aff_i
);
4543 /* Extract an affine expression that expresses the output dimension "pos"
4544 * of "bmap" in terms of the parameters and input dimensions from
4546 * Note that this expression may involve integer divisions defined
4547 * in terms of parameters and input dimensions.
4548 * The equality may also involve references to earlier (but not later)
4549 * output dimensions. These are replaced by the corresponding elements
4552 * If the equality is of the form
4554 * f(i) + h(j) + a x + g(i) = 0,
4556 * with f(i) a linear combinations of the parameters and input dimensions,
4557 * g(i) a linear combination of integer divisions defined in terms of the same
4558 * and h(j) a linear combinations of earlier output dimensions,
4559 * then the affine expression is
4561 * (-f(i) - g(i))/a - h(j)/a
4563 * If the equality is of the form
4565 * f(i) + h(j) - a x + g(i) = 0,
4567 * then the affine expression is
4569 * (f(i) + g(i))/a - h(j)/(-a)
4572 * If "div" refers to an integer division (i.e., it is smaller than
4573 * the number of integer divisions), then the equality constraint
4574 * does involve an integer division (the one at position "div") that
4575 * is defined in terms of output dimensions. However, this integer
4576 * division can be eliminated by exploiting a pair of constraints
4577 * x >= l and x <= l + n, with n smaller than the coefficient of "div"
4578 * in the equality constraint. "ineq" refers to inequality x >= l, i.e.,
4580 * In particular, let
4582 * x = e(i) + m floor(...)
4584 * with e(i) the expression derived above and floor(...) the integer
4585 * division involving output dimensions.
4596 * e(i) + m floor(...) - l = (e(i) + m floor(...) - l) mod m
4597 * = (e(i) - l) mod m
4601 * x - l = (e(i) - l) mod m
4605 * x = ((e(i) - l) mod m) + l
4607 * The variable "shift" below contains the expression -l, which may
4608 * also involve a linear combination of earlier output dimensions.
4610 static __isl_give isl_aff
*extract_aff_from_equality(
4611 __isl_keep isl_basic_map
*bmap
, int pos
, int eq
, int div
, int ineq
,
4612 __isl_keep isl_multi_aff
*ma
)
4615 unsigned n_div
, n_out
;
4617 isl_local_space
*ls
;
4618 isl_aff
*aff
, *shift
;
4621 ctx
= isl_basic_map_get_ctx(bmap
);
4622 ls
= isl_basic_map_get_local_space(bmap
);
4623 ls
= isl_local_space_domain(ls
);
4624 aff
= isl_aff_alloc(isl_local_space_copy(ls
));
4627 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
4628 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4629 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4630 if (isl_int_is_neg(bmap
->eq
[eq
][o_out
+ pos
])) {
4631 isl_seq_cpy(aff
->v
->el
+ 1, bmap
->eq
[eq
], o_out
);
4632 isl_seq_cpy(aff
->v
->el
+ 1 + o_out
,
4633 bmap
->eq
[eq
] + o_out
+ n_out
, n_div
);
4635 isl_seq_neg(aff
->v
->el
+ 1, bmap
->eq
[eq
], o_out
);
4636 isl_seq_neg(aff
->v
->el
+ 1 + o_out
,
4637 bmap
->eq
[eq
] + o_out
+ n_out
, n_div
);
4640 isl_int_set_si(aff
->v
->el
[1 + o_out
+ div
], 0);
4641 isl_int_abs(aff
->v
->el
[0], bmap
->eq
[eq
][o_out
+ pos
]);
4642 aff
= subtract_initial(aff
, ma
, pos
, bmap
->eq
[eq
] + o_out
,
4643 bmap
->eq
[eq
][o_out
+ pos
]);
4645 shift
= isl_aff_alloc(isl_local_space_copy(ls
));
4648 isl_seq_cpy(shift
->v
->el
+ 1, bmap
->ineq
[ineq
], o_out
);
4649 isl_seq_cpy(shift
->v
->el
+ 1 + o_out
,
4650 bmap
->ineq
[ineq
] + o_out
+ n_out
, n_div
);
4651 isl_int_set_si(shift
->v
->el
[0], 1);
4652 shift
= subtract_initial(shift
, ma
, pos
,
4653 bmap
->ineq
[ineq
] + o_out
, ctx
->negone
);
4654 aff
= isl_aff_add(aff
, isl_aff_copy(shift
));
4655 mod
= isl_val_int_from_isl_int(ctx
,
4656 bmap
->eq
[eq
][o_out
+ n_out
+ div
]);
4657 mod
= isl_val_abs(mod
);
4658 aff
= isl_aff_mod_val(aff
, mod
);
4659 aff
= isl_aff_sub(aff
, shift
);
4662 isl_local_space_free(ls
);
4665 isl_local_space_free(ls
);
4670 /* Given a basic map with output dimensions defined
4671 * in terms of the parameters input dimensions and earlier
4672 * output dimensions using an equality (and possibly a pair on inequalities),
4673 * extract an isl_aff that expresses output dimension "pos" in terms
4674 * of the parameters and input dimensions.
4675 * Note that this expression may involve integer divisions defined
4676 * in terms of parameters and input dimensions.
4677 * "ma" contains the expressions corresponding to earlier output dimensions.
4679 * This function shares some similarities with
4680 * isl_basic_map_has_defining_equality and isl_constraint_get_bound.
4682 static __isl_give isl_aff
*extract_isl_aff_from_basic_map(
4683 __isl_keep isl_basic_map
*bmap
, int pos
, __isl_keep isl_multi_aff
*ma
)
4690 eq
= isl_basic_map_output_defining_equality(bmap
, pos
, &div
, &ineq
);
4691 if (eq
>= bmap
->n_eq
)
4692 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
4693 "unable to find suitable equality", return NULL
);
4694 aff
= extract_aff_from_equality(bmap
, pos
, eq
, div
, ineq
, ma
);
4696 aff
= isl_aff_remove_unused_divs(aff
);
4700 /* Given a basic map where each output dimension is defined
4701 * in terms of the parameters and input dimensions using an equality,
4702 * extract an isl_multi_aff that expresses the output dimensions in terms
4703 * of the parameters and input dimensions.
4705 static __isl_give isl_multi_aff
*extract_isl_multi_aff_from_basic_map(
4706 __isl_take isl_basic_map
*bmap
)
4715 ma
= isl_multi_aff_alloc(isl_basic_map_get_space(bmap
));
4716 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4718 for (i
= 0; i
< n_out
; ++i
) {
4721 aff
= extract_isl_aff_from_basic_map(bmap
, i
, ma
);
4722 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
4725 isl_basic_map_free(bmap
);
4730 /* Given a basic set where each set dimension is defined
4731 * in terms of the parameters using an equality,
4732 * extract an isl_multi_aff that expresses the set dimensions in terms
4733 * of the parameters.
4735 __isl_give isl_multi_aff
*isl_multi_aff_from_basic_set_equalities(
4736 __isl_take isl_basic_set
*bset
)
4738 return extract_isl_multi_aff_from_basic_map(bset
);
4741 /* Create an isl_pw_multi_aff that is equivalent to
4742 * isl_map_intersect_domain(isl_map_from_basic_map(bmap), domain).
4743 * The given basic map is such that each output dimension is defined
4744 * in terms of the parameters and input dimensions using an equality.
4746 * Since some applications expect the result of isl_pw_multi_aff_from_map
4747 * to only contain integer affine expressions, we compute the floor
4748 * of the expression before returning.
4750 * Remove all constraints involving local variables without
4751 * an explicit representation (resulting in the removal of those
4752 * local variables) prior to the actual extraction to ensure
4753 * that the local spaces in which the resulting affine expressions
4754 * are created do not contain any unknown local variables.
4755 * Removing such constraints is safe because constraints involving
4756 * unknown local variables are not used to determine whether
4757 * a basic map is obviously single-valued.
4759 static __isl_give isl_pw_multi_aff
*plain_pw_multi_aff_from_map(
4760 __isl_take isl_set
*domain
, __isl_take isl_basic_map
*bmap
)
4764 bmap
= isl_basic_map_drop_constraint_involving_unknown_divs(bmap
);
4765 ma
= extract_isl_multi_aff_from_basic_map(bmap
);
4766 ma
= isl_multi_aff_floor(ma
);
4767 return isl_pw_multi_aff_alloc(domain
, ma
);
4770 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4771 * This obviously only works if the input "map" is single-valued.
4772 * If so, we compute the lexicographic minimum of the image in the form
4773 * of an isl_pw_multi_aff. Since the image is unique, it is equal
4774 * to its lexicographic minimum.
4775 * If the input is not single-valued, we produce an error.
4777 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_base(
4778 __isl_take isl_map
*map
)
4782 isl_pw_multi_aff
*pma
;
4784 sv
= isl_map_is_single_valued(map
);
4788 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
4789 "map is not single-valued", goto error
);
4790 map
= isl_map_make_disjoint(map
);
4794 pma
= isl_pw_multi_aff_empty(isl_map_get_space(map
));
4796 for (i
= 0; i
< map
->n
; ++i
) {
4797 isl_pw_multi_aff
*pma_i
;
4798 isl_basic_map
*bmap
;
4799 bmap
= isl_basic_map_copy(map
->p
[i
]);
4800 pma_i
= isl_basic_map_lexmin_pw_multi_aff(bmap
);
4801 pma
= isl_pw_multi_aff_add_disjoint(pma
, pma_i
);
4811 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
4812 * taking into account that the output dimension at position "d"
4813 * can be represented as
4815 * x = floor((e(...) + c1) / m)
4817 * given that constraint "i" is of the form
4819 * e(...) + c1 - m x >= 0
4822 * Let "map" be of the form
4826 * We construct a mapping
4828 * A -> [A -> x = floor(...)]
4830 * apply that to the map, obtaining
4832 * [A -> x = floor(...)] -> B
4834 * and equate dimension "d" to x.
4835 * We then compute a isl_pw_multi_aff representation of the resulting map
4836 * and plug in the mapping above.
4838 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_div(
4839 __isl_take isl_map
*map
, __isl_take isl_basic_map
*hull
, int d
, int i
)
4843 isl_local_space
*ls
;
4851 isl_pw_multi_aff
*pma
;
4854 is_set
= isl_map_is_set(map
);
4858 offset
= isl_basic_map_offset(hull
, isl_dim_out
);
4859 ctx
= isl_map_get_ctx(map
);
4860 space
= isl_space_domain(isl_map_get_space(map
));
4861 n_in
= isl_space_dim(space
, isl_dim_set
);
4862 n
= isl_space_dim(space
, isl_dim_all
);
4864 v
= isl_vec_alloc(ctx
, 1 + 1 + n
);
4866 isl_int_neg(v
->el
[0], hull
->ineq
[i
][offset
+ d
]);
4867 isl_seq_cpy(v
->el
+ 1, hull
->ineq
[i
], 1 + n
);
4869 isl_basic_map_free(hull
);
4871 ls
= isl_local_space_from_space(isl_space_copy(space
));
4872 aff
= isl_aff_alloc_vec(ls
, v
);
4873 aff
= isl_aff_floor(aff
);
4875 isl_space_free(space
);
4876 ma
= isl_multi_aff_from_aff(aff
);
4878 ma
= isl_multi_aff_identity(isl_space_map_from_set(space
));
4879 ma
= isl_multi_aff_range_product(ma
,
4880 isl_multi_aff_from_aff(aff
));
4883 insert
= isl_map_from_multi_aff(isl_multi_aff_copy(ma
));
4884 map
= isl_map_apply_domain(map
, insert
);
4885 map
= isl_map_equate(map
, isl_dim_in
, n_in
, isl_dim_out
, d
);
4886 pma
= isl_pw_multi_aff_from_map(map
);
4887 pma
= isl_pw_multi_aff_pullback_multi_aff(pma
, ma
);
4892 isl_basic_map_free(hull
);
4896 /* Is constraint "c" of the form
4898 * e(...) + c1 - m x >= 0
4902 * -e(...) + c2 + m x >= 0
4904 * where m > 1 and e only depends on parameters and input dimemnsions?
4906 * "offset" is the offset of the output dimensions
4907 * "pos" is the position of output dimension x.
4909 static int is_potential_div_constraint(isl_int
*c
, int offset
, int d
, int total
)
4911 if (isl_int_is_zero(c
[offset
+ d
]))
4913 if (isl_int_is_one(c
[offset
+ d
]))
4915 if (isl_int_is_negone(c
[offset
+ d
]))
4917 if (isl_seq_first_non_zero(c
+ offset
, d
) != -1)
4919 if (isl_seq_first_non_zero(c
+ offset
+ d
+ 1,
4920 total
- (offset
+ d
+ 1)) != -1)
4925 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4927 * As a special case, we first check if there is any pair of constraints,
4928 * shared by all the basic maps in "map" that force a given dimension
4929 * to be equal to the floor of some affine combination of the input dimensions.
4931 * In particular, if we can find two constraints
4933 * e(...) + c1 - m x >= 0 i.e., m x <= e(...) + c1
4937 * -e(...) + c2 + m x >= 0 i.e., m x >= e(...) - c2
4939 * where m > 1 and e only depends on parameters and input dimemnsions,
4942 * c1 + c2 < m i.e., -c2 >= c1 - (m - 1)
4944 * then we know that we can take
4946 * x = floor((e(...) + c1) / m)
4948 * without having to perform any computation.
4950 * Note that we know that
4954 * If c1 + c2 were 0, then we would have detected an equality during
4955 * simplification. If c1 + c2 were negative, then we would have detected
4958 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_check_div(
4959 __isl_take isl_map
*map
)
4965 isl_basic_map
*hull
;
4967 hull
= isl_map_unshifted_simple_hull(isl_map_copy(map
));
4972 dim
= isl_map_dim(map
, isl_dim_out
);
4973 offset
= isl_basic_map_offset(hull
, isl_dim_out
);
4974 total
= 1 + isl_basic_map_total_dim(hull
);
4976 for (d
= 0; d
< dim
; ++d
) {
4977 for (i
= 0; i
< n
; ++i
) {
4978 if (!is_potential_div_constraint(hull
->ineq
[i
],
4981 for (j
= i
+ 1; j
< n
; ++j
) {
4982 if (!isl_seq_is_neg(hull
->ineq
[i
] + 1,
4983 hull
->ineq
[j
] + 1, total
- 1))
4985 isl_int_add(sum
, hull
->ineq
[i
][0],
4987 if (isl_int_abs_lt(sum
,
4988 hull
->ineq
[i
][offset
+ d
]))
4995 if (isl_int_is_pos(hull
->ineq
[j
][offset
+ d
]))
4997 return pw_multi_aff_from_map_div(map
, hull
, d
, j
);
5001 isl_basic_map_free(hull
);
5002 return pw_multi_aff_from_map_base(map
);
5005 isl_basic_map_free(hull
);
5009 /* Given an affine expression
5011 * [A -> B] -> f(A,B)
5013 * construct an isl_multi_aff
5017 * such that dimension "d" in B' is set to "aff" and the remaining
5018 * dimensions are set equal to the corresponding dimensions in B.
5019 * "n_in" is the dimension of the space A.
5020 * "n_out" is the dimension of the space B.
5022 * If "is_set" is set, then the affine expression is of the form
5026 * and we construct an isl_multi_aff
5030 static __isl_give isl_multi_aff
*range_map(__isl_take isl_aff
*aff
, int d
,
5031 unsigned n_in
, unsigned n_out
, int is_set
)
5035 isl_space
*space
, *space2
;
5036 isl_local_space
*ls
;
5038 space
= isl_aff_get_domain_space(aff
);
5039 ls
= isl_local_space_from_space(isl_space_copy(space
));
5040 space2
= isl_space_copy(space
);
5042 space2
= isl_space_range(isl_space_unwrap(space2
));
5043 space
= isl_space_map_from_domain_and_range(space
, space2
);
5044 ma
= isl_multi_aff_alloc(space
);
5045 ma
= isl_multi_aff_set_aff(ma
, d
, aff
);
5047 for (i
= 0; i
< n_out
; ++i
) {
5050 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
5051 isl_dim_set
, n_in
+ i
);
5052 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
5055 isl_local_space_free(ls
);
5060 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
5061 * taking into account that the dimension at position "d" can be written as
5063 * x = m a + f(..) (1)
5065 * where m is equal to "gcd".
5066 * "i" is the index of the equality in "hull" that defines f(..).
5067 * In particular, the equality is of the form
5069 * f(..) - x + m g(existentials) = 0
5073 * -f(..) + x + m g(existentials) = 0
5075 * We basically plug (1) into "map", resulting in a map with "a"
5076 * in the range instead of "x". The corresponding isl_pw_multi_aff
5077 * defining "a" is then plugged back into (1) to obtain a definition for "x".
5079 * Specifically, given the input map
5083 * We first wrap it into a set
5087 * and define (1) on top of the corresponding space, resulting in "aff".
5088 * We use this to create an isl_multi_aff that maps the output position "d"
5089 * from "a" to "x", leaving all other (intput and output) dimensions unchanged.
5090 * We plug this into the wrapped map, unwrap the result and compute the
5091 * corresponding isl_pw_multi_aff.
5092 * The result is an expression
5100 * so that we can plug that into "aff", after extending the latter to
5106 * If "map" is actually a set, then there is no "A" space, meaning
5107 * that we do not need to perform any wrapping, and that the result
5108 * of the recursive call is of the form
5112 * which is plugged into a mapping of the form
5116 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_stride(
5117 __isl_take isl_map
*map
, __isl_take isl_basic_map
*hull
, int d
, int i
,
5122 isl_local_space
*ls
;
5125 isl_pw_multi_aff
*pma
, *id
;
5131 is_set
= isl_map_is_set(map
);
5135 n_in
= isl_basic_map_dim(hull
, isl_dim_in
);
5136 n_out
= isl_basic_map_dim(hull
, isl_dim_out
);
5137 o_out
= isl_basic_map_offset(hull
, isl_dim_out
);
5142 set
= isl_map_wrap(map
);
5143 space
= isl_space_map_from_set(isl_set_get_space(set
));
5144 ma
= isl_multi_aff_identity(space
);
5145 ls
= isl_local_space_from_space(isl_set_get_space(set
));
5146 aff
= isl_aff_alloc(ls
);
5148 isl_int_set_si(aff
->v
->el
[0], 1);
5149 if (isl_int_is_one(hull
->eq
[i
][o_out
+ d
]))
5150 isl_seq_neg(aff
->v
->el
+ 1, hull
->eq
[i
],
5153 isl_seq_cpy(aff
->v
->el
+ 1, hull
->eq
[i
],
5155 isl_int_set(aff
->v
->el
[1 + o_out
+ d
], gcd
);
5157 ma
= isl_multi_aff_set_aff(ma
, n_in
+ d
, isl_aff_copy(aff
));
5158 set
= isl_set_preimage_multi_aff(set
, ma
);
5160 ma
= range_map(aff
, d
, n_in
, n_out
, is_set
);
5165 map
= isl_set_unwrap(set
);
5166 pma
= isl_pw_multi_aff_from_map(map
);
5169 space
= isl_pw_multi_aff_get_domain_space(pma
);
5170 space
= isl_space_map_from_set(space
);
5171 id
= isl_pw_multi_aff_identity(space
);
5172 pma
= isl_pw_multi_aff_range_product(id
, pma
);
5174 id
= isl_pw_multi_aff_from_multi_aff(ma
);
5175 pma
= isl_pw_multi_aff_pullback_pw_multi_aff(id
, pma
);
5177 isl_basic_map_free(hull
);
5181 isl_basic_map_free(hull
);
5185 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5186 * "hull" contains the equalities valid for "map".
5188 * Check if any of the output dimensions is "strided".
5189 * That is, we check if it can be written as
5193 * with m greater than 1, a some combination of existentially quantified
5194 * variables and f an expression in the parameters and input dimensions.
5195 * If so, we remove the stride in pw_multi_aff_from_map_stride.
5197 * Otherwise, we continue with pw_multi_aff_from_map_check_div for a further
5200 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_check_strides(
5201 __isl_take isl_map
*map
, __isl_take isl_basic_map
*hull
)
5210 n_div
= isl_basic_map_dim(hull
, isl_dim_div
);
5211 o_div
= isl_basic_map_offset(hull
, isl_dim_div
);
5214 isl_basic_map_free(hull
);
5215 return pw_multi_aff_from_map_check_div(map
);
5220 n_out
= isl_basic_map_dim(hull
, isl_dim_out
);
5221 o_out
= isl_basic_map_offset(hull
, isl_dim_out
);
5223 for (i
= 0; i
< n_out
; ++i
) {
5224 for (j
= 0; j
< hull
->n_eq
; ++j
) {
5225 isl_int
*eq
= hull
->eq
[j
];
5226 isl_pw_multi_aff
*res
;
5228 if (!isl_int_is_one(eq
[o_out
+ i
]) &&
5229 !isl_int_is_negone(eq
[o_out
+ i
]))
5231 if (isl_seq_first_non_zero(eq
+ o_out
, i
) != -1)
5233 if (isl_seq_first_non_zero(eq
+ o_out
+ i
+ 1,
5234 n_out
- (i
+ 1)) != -1)
5236 isl_seq_gcd(eq
+ o_div
, n_div
, &gcd
);
5237 if (isl_int_is_zero(gcd
))
5239 if (isl_int_is_one(gcd
))
5242 res
= pw_multi_aff_from_map_stride(map
, hull
,
5250 isl_basic_map_free(hull
);
5251 return pw_multi_aff_from_map_check_div(map
);
5254 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5256 * As a special case, we first check if all output dimensions are uniquely
5257 * defined in terms of the parameters and input dimensions over the entire
5258 * domain. If so, we extract the desired isl_pw_multi_aff directly
5259 * from the affine hull of "map" and its domain.
5261 * Otherwise, continue with pw_multi_aff_from_map_check_strides for more
5264 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_map(__isl_take isl_map
*map
)
5267 isl_basic_map
*hull
;
5272 if (isl_map_n_basic_map(map
) == 1) {
5273 hull
= isl_map_unshifted_simple_hull(isl_map_copy(map
));
5274 hull
= isl_basic_map_plain_affine_hull(hull
);
5275 sv
= isl_basic_map_plain_is_single_valued(hull
);
5277 return plain_pw_multi_aff_from_map(isl_map_domain(map
),
5279 isl_basic_map_free(hull
);
5281 map
= isl_map_detect_equalities(map
);
5282 hull
= isl_map_unshifted_simple_hull(isl_map_copy(map
));
5283 sv
= isl_basic_map_plain_is_single_valued(hull
);
5285 return plain_pw_multi_aff_from_map(isl_map_domain(map
), hull
);
5287 return pw_multi_aff_from_map_check_strides(map
, hull
);
5288 isl_basic_map_free(hull
);
5293 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_set(__isl_take isl_set
*set
)
5295 return isl_pw_multi_aff_from_map(set
);
5298 /* Convert "map" into an isl_pw_multi_aff (if possible) and
5301 static isl_stat
pw_multi_aff_from_map(__isl_take isl_map
*map
, void *user
)
5303 isl_union_pw_multi_aff
**upma
= user
;
5304 isl_pw_multi_aff
*pma
;
5306 pma
= isl_pw_multi_aff_from_map(map
);
5307 *upma
= isl_union_pw_multi_aff_add_pw_multi_aff(*upma
, pma
);
5309 return *upma
? isl_stat_ok
: isl_stat_error
;
5312 /* Create an isl_union_pw_multi_aff with the given isl_aff on a universe
5315 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_aff(
5316 __isl_take isl_aff
*aff
)
5319 isl_pw_multi_aff
*pma
;
5321 ma
= isl_multi_aff_from_aff(aff
);
5322 pma
= isl_pw_multi_aff_from_multi_aff(ma
);
5323 return isl_union_pw_multi_aff_from_pw_multi_aff(pma
);
5326 /* Try and create an isl_union_pw_multi_aff that is equivalent
5327 * to the given isl_union_map.
5328 * The isl_union_map is required to be single-valued in each space.
5329 * Otherwise, an error is produced.
5331 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_union_map(
5332 __isl_take isl_union_map
*umap
)
5335 isl_union_pw_multi_aff
*upma
;
5337 space
= isl_union_map_get_space(umap
);
5338 upma
= isl_union_pw_multi_aff_empty(space
);
5339 if (isl_union_map_foreach_map(umap
, &pw_multi_aff_from_map
, &upma
) < 0)
5340 upma
= isl_union_pw_multi_aff_free(upma
);
5341 isl_union_map_free(umap
);
5346 /* Try and create an isl_union_pw_multi_aff that is equivalent
5347 * to the given isl_union_set.
5348 * The isl_union_set is required to be a singleton in each space.
5349 * Otherwise, an error is produced.
5351 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_union_set(
5352 __isl_take isl_union_set
*uset
)
5354 return isl_union_pw_multi_aff_from_union_map(uset
);
5357 /* Return the piecewise affine expression "set ? 1 : 0".
5359 __isl_give isl_pw_aff
*isl_set_indicator_function(__isl_take isl_set
*set
)
5362 isl_space
*space
= isl_set_get_space(set
);
5363 isl_local_space
*ls
= isl_local_space_from_space(space
);
5364 isl_aff
*zero
= isl_aff_zero_on_domain(isl_local_space_copy(ls
));
5365 isl_aff
*one
= isl_aff_zero_on_domain(ls
);
5367 one
= isl_aff_add_constant_si(one
, 1);
5368 pa
= isl_pw_aff_alloc(isl_set_copy(set
), one
);
5369 set
= isl_set_complement(set
);
5370 pa
= isl_pw_aff_add_disjoint(pa
, isl_pw_aff_alloc(set
, zero
));
5375 /* Plug in "subs" for dimension "type", "pos" of "aff".
5377 * Let i be the dimension to replace and let "subs" be of the form
5381 * and "aff" of the form
5387 * (a f + d g')/(m d)
5389 * where g' is the result of plugging in "subs" in each of the integer
5392 __isl_give isl_aff
*isl_aff_substitute(__isl_take isl_aff
*aff
,
5393 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
5398 aff
= isl_aff_cow(aff
);
5400 return isl_aff_free(aff
);
5402 ctx
= isl_aff_get_ctx(aff
);
5403 if (!isl_space_is_equal(aff
->ls
->dim
, subs
->ls
->dim
))
5404 isl_die(ctx
, isl_error_invalid
,
5405 "spaces don't match", return isl_aff_free(aff
));
5406 if (isl_local_space_dim(subs
->ls
, isl_dim_div
) != 0)
5407 isl_die(ctx
, isl_error_unsupported
,
5408 "cannot handle divs yet", return isl_aff_free(aff
));
5410 aff
->ls
= isl_local_space_substitute(aff
->ls
, type
, pos
, subs
);
5412 return isl_aff_free(aff
);
5414 aff
->v
= isl_vec_cow(aff
->v
);
5416 return isl_aff_free(aff
);
5418 pos
+= isl_local_space_offset(aff
->ls
, type
);
5421 isl_seq_substitute(aff
->v
->el
, pos
, subs
->v
->el
,
5422 aff
->v
->size
, subs
->v
->size
, v
);
5428 /* Plug in "subs" for dimension "type", "pos" in each of the affine
5429 * expressions in "maff".
5431 __isl_give isl_multi_aff
*isl_multi_aff_substitute(
5432 __isl_take isl_multi_aff
*maff
, enum isl_dim_type type
, unsigned pos
,
5433 __isl_keep isl_aff
*subs
)
5437 maff
= isl_multi_aff_cow(maff
);
5439 return isl_multi_aff_free(maff
);
5441 if (type
== isl_dim_in
)
5444 for (i
= 0; i
< maff
->n
; ++i
) {
5445 maff
->u
.p
[i
] = isl_aff_substitute(maff
->u
.p
[i
],
5448 return isl_multi_aff_free(maff
);
5454 /* Plug in "subs" for dimension "type", "pos" of "pma".
5456 * pma is of the form
5460 * while subs is of the form
5462 * v' = B_j(v) -> S_j
5464 * Each pair i,j such that C_ij = A_i \cap B_i is non-empty
5465 * has a contribution in the result, in particular
5467 * C_ij(S_j) -> M_i(S_j)
5469 * Note that plugging in S_j in C_ij may also result in an empty set
5470 * and this contribution should simply be discarded.
5472 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_substitute(
5473 __isl_take isl_pw_multi_aff
*pma
, enum isl_dim_type type
, unsigned pos
,
5474 __isl_keep isl_pw_aff
*subs
)
5477 isl_pw_multi_aff
*res
;
5480 return isl_pw_multi_aff_free(pma
);
5482 n
= pma
->n
* subs
->n
;
5483 res
= isl_pw_multi_aff_alloc_size(isl_space_copy(pma
->dim
), n
);
5485 for (i
= 0; i
< pma
->n
; ++i
) {
5486 for (j
= 0; j
< subs
->n
; ++j
) {
5488 isl_multi_aff
*res_ij
;
5491 common
= isl_set_intersect(
5492 isl_set_copy(pma
->p
[i
].set
),
5493 isl_set_copy(subs
->p
[j
].set
));
5494 common
= isl_set_substitute(common
,
5495 type
, pos
, subs
->p
[j
].aff
);
5496 empty
= isl_set_plain_is_empty(common
);
5497 if (empty
< 0 || empty
) {
5498 isl_set_free(common
);
5504 res_ij
= isl_multi_aff_substitute(
5505 isl_multi_aff_copy(pma
->p
[i
].maff
),
5506 type
, pos
, subs
->p
[j
].aff
);
5508 res
= isl_pw_multi_aff_add_piece(res
, common
, res_ij
);
5512 isl_pw_multi_aff_free(pma
);
5515 isl_pw_multi_aff_free(pma
);
5516 isl_pw_multi_aff_free(res
);
5520 /* Compute the preimage of a range of dimensions in the affine expression "src"
5521 * under "ma" and put the result in "dst". The number of dimensions in "src"
5522 * that precede the range is given by "n_before". The number of dimensions
5523 * in the range is given by the number of output dimensions of "ma".
5524 * The number of dimensions that follow the range is given by "n_after".
5525 * If "has_denom" is set (to one),
5526 * then "src" and "dst" have an extra initial denominator.
5527 * "n_div_ma" is the number of existentials in "ma"
5528 * "n_div_bset" is the number of existentials in "src"
5529 * The resulting "dst" (which is assumed to have been allocated by
5530 * the caller) contains coefficients for both sets of existentials,
5531 * first those in "ma" and then those in "src".
5532 * f, c1, c2 and g are temporary objects that have been initialized
5535 * Let src represent the expression
5537 * (a(p) + f_u u + b v + f_w w + c(divs))/d
5539 * and let ma represent the expressions
5541 * v_i = (r_i(p) + s_i(y) + t_i(divs'))/m_i
5543 * We start out with the following expression for dst:
5545 * (a(p) + f_u u + 0 y + f_w w + 0 divs' + c(divs) + f \sum_i b_i v_i)/d
5547 * with the multiplication factor f initially equal to 1
5548 * and f \sum_i b_i v_i kept separately.
5549 * For each x_i that we substitute, we multiply the numerator
5550 * (and denominator) of dst by c_1 = m_i and add the numerator
5551 * of the x_i expression multiplied by c_2 = f b_i,
5552 * after removing the common factors of c_1 and c_2.
5553 * The multiplication factor f also needs to be multiplied by c_1
5554 * for the next x_j, j > i.
5556 void isl_seq_preimage(isl_int
*dst
, isl_int
*src
,
5557 __isl_keep isl_multi_aff
*ma
, int n_before
, int n_after
,
5558 int n_div_ma
, int n_div_bmap
,
5559 isl_int f
, isl_int c1
, isl_int c2
, isl_int g
, int has_denom
)
5562 int n_param
, n_in
, n_out
;
5565 n_param
= isl_multi_aff_dim(ma
, isl_dim_param
);
5566 n_in
= isl_multi_aff_dim(ma
, isl_dim_in
);
5567 n_out
= isl_multi_aff_dim(ma
, isl_dim_out
);
5569 isl_seq_cpy(dst
, src
, has_denom
+ 1 + n_param
+ n_before
);
5570 o_dst
= o_src
= has_denom
+ 1 + n_param
+ n_before
;
5571 isl_seq_clr(dst
+ o_dst
, n_in
);
5574 isl_seq_cpy(dst
+ o_dst
, src
+ o_src
, n_after
);
5577 isl_seq_clr(dst
+ o_dst
, n_div_ma
);
5579 isl_seq_cpy(dst
+ o_dst
, src
+ o_src
, n_div_bmap
);
5581 isl_int_set_si(f
, 1);
5583 for (i
= 0; i
< n_out
; ++i
) {
5584 int offset
= has_denom
+ 1 + n_param
+ n_before
+ i
;
5586 if (isl_int_is_zero(src
[offset
]))
5588 isl_int_set(c1
, ma
->u
.p
[i
]->v
->el
[0]);
5589 isl_int_mul(c2
, f
, src
[offset
]);
5590 isl_int_gcd(g
, c1
, c2
);
5591 isl_int_divexact(c1
, c1
, g
);
5592 isl_int_divexact(c2
, c2
, g
);
5594 isl_int_mul(f
, f
, c1
);
5597 isl_seq_combine(dst
+ o_dst
, c1
, dst
+ o_dst
,
5598 c2
, ma
->u
.p
[i
]->v
->el
+ o_src
, 1 + n_param
);
5599 o_dst
+= 1 + n_param
;
5600 o_src
+= 1 + n_param
;
5601 isl_seq_scale(dst
+ o_dst
, dst
+ o_dst
, c1
, n_before
);
5603 isl_seq_combine(dst
+ o_dst
, c1
, dst
+ o_dst
,
5604 c2
, ma
->u
.p
[i
]->v
->el
+ o_src
, n_in
);
5607 isl_seq_scale(dst
+ o_dst
, dst
+ o_dst
, c1
, n_after
);
5609 isl_seq_combine(dst
+ o_dst
, c1
, dst
+ o_dst
,
5610 c2
, ma
->u
.p
[i
]->v
->el
+ o_src
, n_div_ma
);
5613 isl_seq_scale(dst
+ o_dst
, dst
+ o_dst
, c1
, n_div_bmap
);
5615 isl_int_mul(dst
[0], dst
[0], c1
);
5619 /* Compute the pullback of "aff" by the function represented by "ma".
5620 * In other words, plug in "ma" in "aff". The result is an affine expression
5621 * defined over the domain space of "ma".
5623 * If "aff" is represented by
5625 * (a(p) + b x + c(divs))/d
5627 * and ma is represented by
5629 * x = D(p) + F(y) + G(divs')
5631 * then the result is
5633 * (a(p) + b D(p) + b F(y) + b G(divs') + c(divs))/d
5635 * The divs in the local space of the input are similarly adjusted
5636 * through a call to isl_local_space_preimage_multi_aff.
5638 __isl_give isl_aff
*isl_aff_pullback_multi_aff(__isl_take isl_aff
*aff
,
5639 __isl_take isl_multi_aff
*ma
)
5641 isl_aff
*res
= NULL
;
5642 isl_local_space
*ls
;
5643 int n_div_aff
, n_div_ma
;
5644 isl_int f
, c1
, c2
, g
;
5646 ma
= isl_multi_aff_align_divs(ma
);
5650 n_div_aff
= isl_aff_dim(aff
, isl_dim_div
);
5651 n_div_ma
= ma
->n
? isl_aff_dim(ma
->u
.p
[0], isl_dim_div
) : 0;
5653 ls
= isl_aff_get_domain_local_space(aff
);
5654 ls
= isl_local_space_preimage_multi_aff(ls
, isl_multi_aff_copy(ma
));
5655 res
= isl_aff_alloc(ls
);
5664 isl_seq_preimage(res
->v
->el
, aff
->v
->el
, ma
, 0, 0, n_div_ma
, n_div_aff
,
5673 isl_multi_aff_free(ma
);
5674 res
= isl_aff_normalize(res
);
5678 isl_multi_aff_free(ma
);
5683 /* Compute the pullback of "aff1" by the function represented by "aff2".
5684 * In other words, plug in "aff2" in "aff1". The result is an affine expression
5685 * defined over the domain space of "aff1".
5687 * The domain of "aff1" should match the range of "aff2", which means
5688 * that it should be single-dimensional.
5690 __isl_give isl_aff
*isl_aff_pullback_aff(__isl_take isl_aff
*aff1
,
5691 __isl_take isl_aff
*aff2
)
5695 ma
= isl_multi_aff_from_aff(aff2
);
5696 return isl_aff_pullback_multi_aff(aff1
, ma
);
5699 /* Compute the pullback of "ma1" by the function represented by "ma2".
5700 * In other words, plug in "ma2" in "ma1".
5702 * The parameters of "ma1" and "ma2" are assumed to have been aligned.
5704 static __isl_give isl_multi_aff
*isl_multi_aff_pullback_multi_aff_aligned(
5705 __isl_take isl_multi_aff
*ma1
, __isl_take isl_multi_aff
*ma2
)
5708 isl_space
*space
= NULL
;
5710 ma2
= isl_multi_aff_align_divs(ma2
);
5711 ma1
= isl_multi_aff_cow(ma1
);
5715 space
= isl_space_join(isl_multi_aff_get_space(ma2
),
5716 isl_multi_aff_get_space(ma1
));
5718 for (i
= 0; i
< ma1
->n
; ++i
) {
5719 ma1
->u
.p
[i
] = isl_aff_pullback_multi_aff(ma1
->u
.p
[i
],
5720 isl_multi_aff_copy(ma2
));
5725 ma1
= isl_multi_aff_reset_space(ma1
, space
);
5726 isl_multi_aff_free(ma2
);
5729 isl_space_free(space
);
5730 isl_multi_aff_free(ma2
);
5731 isl_multi_aff_free(ma1
);
5735 /* Compute the pullback of "ma1" by the function represented by "ma2".
5736 * In other words, plug in "ma2" in "ma1".
5738 __isl_give isl_multi_aff
*isl_multi_aff_pullback_multi_aff(
5739 __isl_take isl_multi_aff
*ma1
, __isl_take isl_multi_aff
*ma2
)
5741 return isl_multi_aff_align_params_multi_multi_and(ma1
, ma2
,
5742 &isl_multi_aff_pullback_multi_aff_aligned
);
5745 /* Extend the local space of "dst" to include the divs
5746 * in the local space of "src".
5748 * If "src" does not have any divs or if the local spaces of "dst" and
5749 * "src" are the same, then no extension is required.
5751 __isl_give isl_aff
*isl_aff_align_divs(__isl_take isl_aff
*dst
,
5752 __isl_keep isl_aff
*src
)
5755 int src_n_div
, dst_n_div
;
5762 return isl_aff_free(dst
);
5764 ctx
= isl_aff_get_ctx(src
);
5765 equal
= isl_local_space_has_equal_space(src
->ls
, dst
->ls
);
5767 return isl_aff_free(dst
);
5769 isl_die(ctx
, isl_error_invalid
,
5770 "spaces don't match", goto error
);
5772 src_n_div
= isl_local_space_dim(src
->ls
, isl_dim_div
);
5775 equal
= isl_local_space_is_equal(src
->ls
, dst
->ls
);
5777 return isl_aff_free(dst
);
5781 dst_n_div
= isl_local_space_dim(dst
->ls
, isl_dim_div
);
5782 exp1
= isl_alloc_array(ctx
, int, src_n_div
);
5783 exp2
= isl_alloc_array(ctx
, int, dst_n_div
);
5784 if (!exp1
|| (dst_n_div
&& !exp2
))
5787 div
= isl_merge_divs(src
->ls
->div
, dst
->ls
->div
, exp1
, exp2
);
5788 dst
= isl_aff_expand_divs(dst
, div
, exp2
);
5796 return isl_aff_free(dst
);
5799 /* Adjust the local spaces of the affine expressions in "maff"
5800 * such that they all have the save divs.
5802 __isl_give isl_multi_aff
*isl_multi_aff_align_divs(
5803 __isl_take isl_multi_aff
*maff
)
5811 maff
= isl_multi_aff_cow(maff
);
5815 for (i
= 1; i
< maff
->n
; ++i
)
5816 maff
->u
.p
[0] = isl_aff_align_divs(maff
->u
.p
[0], maff
->u
.p
[i
]);
5817 for (i
= 1; i
< maff
->n
; ++i
) {
5818 maff
->u
.p
[i
] = isl_aff_align_divs(maff
->u
.p
[i
], maff
->u
.p
[0]);
5820 return isl_multi_aff_free(maff
);
5826 __isl_give isl_aff
*isl_aff_lift(__isl_take isl_aff
*aff
)
5828 aff
= isl_aff_cow(aff
);
5832 aff
->ls
= isl_local_space_lift(aff
->ls
);
5834 return isl_aff_free(aff
);
5839 /* Lift "maff" to a space with extra dimensions such that the result
5840 * has no more existentially quantified variables.
5841 * If "ls" is not NULL, then *ls is assigned the local space that lies
5842 * at the basis of the lifting applied to "maff".
5844 __isl_give isl_multi_aff
*isl_multi_aff_lift(__isl_take isl_multi_aff
*maff
,
5845 __isl_give isl_local_space
**ls
)
5859 isl_space
*space
= isl_multi_aff_get_domain_space(maff
);
5860 *ls
= isl_local_space_from_space(space
);
5862 return isl_multi_aff_free(maff
);
5867 maff
= isl_multi_aff_cow(maff
);
5868 maff
= isl_multi_aff_align_divs(maff
);
5872 n_div
= isl_aff_dim(maff
->u
.p
[0], isl_dim_div
);
5873 space
= isl_multi_aff_get_space(maff
);
5874 space
= isl_space_lift(isl_space_domain(space
), n_div
);
5875 space
= isl_space_extend_domain_with_range(space
,
5876 isl_multi_aff_get_space(maff
));
5878 return isl_multi_aff_free(maff
);
5879 isl_space_free(maff
->space
);
5880 maff
->space
= space
;
5883 *ls
= isl_aff_get_domain_local_space(maff
->u
.p
[0]);
5885 return isl_multi_aff_free(maff
);
5888 for (i
= 0; i
< maff
->n
; ++i
) {
5889 maff
->u
.p
[i
] = isl_aff_lift(maff
->u
.p
[i
]);
5897 isl_local_space_free(*ls
);
5898 return isl_multi_aff_free(maff
);
5902 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma".
5904 __isl_give isl_pw_aff
*isl_pw_multi_aff_get_pw_aff(
5905 __isl_keep isl_pw_multi_aff
*pma
, int pos
)
5915 n_out
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
5916 if (pos
< 0 || pos
>= n_out
)
5917 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
5918 "index out of bounds", return NULL
);
5920 space
= isl_pw_multi_aff_get_space(pma
);
5921 space
= isl_space_drop_dims(space
, isl_dim_out
,
5922 pos
+ 1, n_out
- pos
- 1);
5923 space
= isl_space_drop_dims(space
, isl_dim_out
, 0, pos
);
5925 pa
= isl_pw_aff_alloc_size(space
, pma
->n
);
5926 for (i
= 0; i
< pma
->n
; ++i
) {
5928 aff
= isl_multi_aff_get_aff(pma
->p
[i
].maff
, pos
);
5929 pa
= isl_pw_aff_add_piece(pa
, isl_set_copy(pma
->p
[i
].set
), aff
);
5935 /* Return an isl_pw_multi_aff with the given "set" as domain and
5936 * an unnamed zero-dimensional range.
5938 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_domain(
5939 __isl_take isl_set
*set
)
5944 space
= isl_set_get_space(set
);
5945 space
= isl_space_from_domain(space
);
5946 ma
= isl_multi_aff_zero(space
);
5947 return isl_pw_multi_aff_alloc(set
, ma
);
5950 /* Add an isl_pw_multi_aff with the given "set" as domain and
5951 * an unnamed zero-dimensional range to *user.
5953 static isl_stat
add_pw_multi_aff_from_domain(__isl_take isl_set
*set
,
5956 isl_union_pw_multi_aff
**upma
= user
;
5957 isl_pw_multi_aff
*pma
;
5959 pma
= isl_pw_multi_aff_from_domain(set
);
5960 *upma
= isl_union_pw_multi_aff_add_pw_multi_aff(*upma
, pma
);
5965 /* Return an isl_union_pw_multi_aff with the given "uset" as domain and
5966 * an unnamed zero-dimensional range.
5968 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_domain(
5969 __isl_take isl_union_set
*uset
)
5972 isl_union_pw_multi_aff
*upma
;
5977 space
= isl_union_set_get_space(uset
);
5978 upma
= isl_union_pw_multi_aff_empty(space
);
5980 if (isl_union_set_foreach_set(uset
,
5981 &add_pw_multi_aff_from_domain
, &upma
) < 0)
5984 isl_union_set_free(uset
);
5987 isl_union_set_free(uset
);
5988 isl_union_pw_multi_aff_free(upma
);
5992 /* Convert "pma" to an isl_map and add it to *umap.
5994 static isl_stat
map_from_pw_multi_aff(__isl_take isl_pw_multi_aff
*pma
,
5997 isl_union_map
**umap
= user
;
6000 map
= isl_map_from_pw_multi_aff(pma
);
6001 *umap
= isl_union_map_add_map(*umap
, map
);
6006 /* Construct a union map mapping the domain of the union
6007 * piecewise multi-affine expression to its range, with each dimension
6008 * in the range equated to the corresponding affine expression on its cell.
6010 __isl_give isl_union_map
*isl_union_map_from_union_pw_multi_aff(
6011 __isl_take isl_union_pw_multi_aff
*upma
)
6014 isl_union_map
*umap
;
6019 space
= isl_union_pw_multi_aff_get_space(upma
);
6020 umap
= isl_union_map_empty(space
);
6022 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma
,
6023 &map_from_pw_multi_aff
, &umap
) < 0)
6026 isl_union_pw_multi_aff_free(upma
);
6029 isl_union_pw_multi_aff_free(upma
);
6030 isl_union_map_free(umap
);
6034 /* Local data for bin_entry and the callback "fn".
6036 struct isl_union_pw_multi_aff_bin_data
{
6037 isl_union_pw_multi_aff
*upma2
;
6038 isl_union_pw_multi_aff
*res
;
6039 isl_pw_multi_aff
*pma
;
6040 isl_stat (*fn
)(__isl_take isl_pw_multi_aff
*pma
, void *user
);
6043 /* Given an isl_pw_multi_aff from upma1, store it in data->pma
6044 * and call data->fn for each isl_pw_multi_aff in data->upma2.
6046 static isl_stat
bin_entry(__isl_take isl_pw_multi_aff
*pma
, void *user
)
6048 struct isl_union_pw_multi_aff_bin_data
*data
= user
;
6052 r
= isl_union_pw_multi_aff_foreach_pw_multi_aff(data
->upma2
,
6054 isl_pw_multi_aff_free(pma
);
6059 /* Call "fn" on each pair of isl_pw_multi_affs in "upma1" and "upma2".
6060 * The isl_pw_multi_aff from upma1 is stored in data->pma (where data is
6061 * passed as user field) and the isl_pw_multi_aff from upma2 is available
6062 * as *entry. The callback should adjust data->res if desired.
6064 static __isl_give isl_union_pw_multi_aff
*bin_op(
6065 __isl_take isl_union_pw_multi_aff
*upma1
,
6066 __isl_take isl_union_pw_multi_aff
*upma2
,
6067 isl_stat (*fn
)(__isl_take isl_pw_multi_aff
*pma
, void *user
))
6070 struct isl_union_pw_multi_aff_bin_data data
= { NULL
, NULL
, NULL
, fn
};
6072 space
= isl_union_pw_multi_aff_get_space(upma2
);
6073 upma1
= isl_union_pw_multi_aff_align_params(upma1
, space
);
6074 space
= isl_union_pw_multi_aff_get_space(upma1
);
6075 upma2
= isl_union_pw_multi_aff_align_params(upma2
, space
);
6077 if (!upma1
|| !upma2
)
6081 data
.res
= isl_union_pw_multi_aff_alloc_same_size(upma1
);
6082 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma1
,
6083 &bin_entry
, &data
) < 0)
6086 isl_union_pw_multi_aff_free(upma1
);
6087 isl_union_pw_multi_aff_free(upma2
);
6090 isl_union_pw_multi_aff_free(upma1
);
6091 isl_union_pw_multi_aff_free(upma2
);
6092 isl_union_pw_multi_aff_free(data
.res
);
6096 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
6097 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
6099 static __isl_give isl_pw_multi_aff
*pw_multi_aff_range_product(
6100 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
6104 space
= isl_space_range_product(isl_pw_multi_aff_get_space(pma1
),
6105 isl_pw_multi_aff_get_space(pma2
));
6106 return isl_pw_multi_aff_on_shared_domain_in(pma1
, pma2
, space
,
6107 &isl_multi_aff_range_product
);
6110 /* Given two isl_pw_multi_affs A -> B and C -> D,
6111 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
6113 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_range_product(
6114 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
6116 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
6117 &pw_multi_aff_range_product
);
6120 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
6121 * construct an isl_pw_multi_aff (A * C) -> (B, D).
6123 static __isl_give isl_pw_multi_aff
*pw_multi_aff_flat_range_product(
6124 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
6128 space
= isl_space_range_product(isl_pw_multi_aff_get_space(pma1
),
6129 isl_pw_multi_aff_get_space(pma2
));
6130 space
= isl_space_flatten_range(space
);
6131 return isl_pw_multi_aff_on_shared_domain_in(pma1
, pma2
, space
,
6132 &isl_multi_aff_flat_range_product
);
6135 /* Given two isl_pw_multi_affs A -> B and C -> D,
6136 * construct an isl_pw_multi_aff (A * C) -> (B, D).
6138 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_flat_range_product(
6139 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
6141 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
6142 &pw_multi_aff_flat_range_product
);
6145 /* If data->pma and "pma2" have the same domain space, then compute
6146 * their flat range product and the result to data->res.
6148 static isl_stat
flat_range_product_entry(__isl_take isl_pw_multi_aff
*pma2
,
6151 struct isl_union_pw_multi_aff_bin_data
*data
= user
;
6153 if (!isl_space_tuple_is_equal(data
->pma
->dim
, isl_dim_in
,
6154 pma2
->dim
, isl_dim_in
)) {
6155 isl_pw_multi_aff_free(pma2
);
6159 pma2
= isl_pw_multi_aff_flat_range_product(
6160 isl_pw_multi_aff_copy(data
->pma
), pma2
);
6162 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma2
);
6167 /* Given two isl_union_pw_multi_affs A -> B and C -> D,
6168 * construct an isl_union_pw_multi_aff (A * C) -> (B, D).
6170 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_flat_range_product(
6171 __isl_take isl_union_pw_multi_aff
*upma1
,
6172 __isl_take isl_union_pw_multi_aff
*upma2
)
6174 return bin_op(upma1
, upma2
, &flat_range_product_entry
);
6177 /* Replace the affine expressions at position "pos" in "pma" by "pa".
6178 * The parameters are assumed to have been aligned.
6180 * The implementation essentially performs an isl_pw_*_on_shared_domain,
6181 * except that it works on two different isl_pw_* types.
6183 static __isl_give isl_pw_multi_aff
*pw_multi_aff_set_pw_aff(
6184 __isl_take isl_pw_multi_aff
*pma
, unsigned pos
,
6185 __isl_take isl_pw_aff
*pa
)
6188 isl_pw_multi_aff
*res
= NULL
;
6193 if (!isl_space_tuple_is_equal(pma
->dim
, isl_dim_in
,
6194 pa
->dim
, isl_dim_in
))
6195 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
6196 "domains don't match", goto error
);
6197 if (pos
>= isl_pw_multi_aff_dim(pma
, isl_dim_out
))
6198 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
6199 "index out of bounds", goto error
);
6202 res
= isl_pw_multi_aff_alloc_size(isl_pw_multi_aff_get_space(pma
), n
);
6204 for (i
= 0; i
< pma
->n
; ++i
) {
6205 for (j
= 0; j
< pa
->n
; ++j
) {
6207 isl_multi_aff
*res_ij
;
6210 common
= isl_set_intersect(isl_set_copy(pma
->p
[i
].set
),
6211 isl_set_copy(pa
->p
[j
].set
));
6212 empty
= isl_set_plain_is_empty(common
);
6213 if (empty
< 0 || empty
) {
6214 isl_set_free(common
);
6220 res_ij
= isl_multi_aff_set_aff(
6221 isl_multi_aff_copy(pma
->p
[i
].maff
), pos
,
6222 isl_aff_copy(pa
->p
[j
].aff
));
6223 res_ij
= isl_multi_aff_gist(res_ij
,
6224 isl_set_copy(common
));
6226 res
= isl_pw_multi_aff_add_piece(res
, common
, res_ij
);
6230 isl_pw_multi_aff_free(pma
);
6231 isl_pw_aff_free(pa
);
6234 isl_pw_multi_aff_free(pma
);
6235 isl_pw_aff_free(pa
);
6236 return isl_pw_multi_aff_free(res
);
6239 /* Replace the affine expressions at position "pos" in "pma" by "pa".
6241 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_set_pw_aff(
6242 __isl_take isl_pw_multi_aff
*pma
, unsigned pos
,
6243 __isl_take isl_pw_aff
*pa
)
6245 isl_bool equal_params
;
6249 equal_params
= isl_space_has_equal_params(pma
->dim
, pa
->dim
);
6250 if (equal_params
< 0)
6253 return pw_multi_aff_set_pw_aff(pma
, pos
, pa
);
6254 if (isl_pw_multi_aff_check_named_params(pma
) < 0 ||
6255 isl_pw_aff_check_named_params(pa
) < 0)
6257 pma
= isl_pw_multi_aff_align_params(pma
, isl_pw_aff_get_space(pa
));
6258 pa
= isl_pw_aff_align_params(pa
, isl_pw_multi_aff_get_space(pma
));
6259 return pw_multi_aff_set_pw_aff(pma
, pos
, pa
);
6261 isl_pw_multi_aff_free(pma
);
6262 isl_pw_aff_free(pa
);
6266 /* Do the parameters of "pa" match those of "space"?
6268 isl_bool
isl_pw_aff_matching_params(__isl_keep isl_pw_aff
*pa
,
6269 __isl_keep isl_space
*space
)
6271 isl_space
*pa_space
;
6275 return isl_bool_error
;
6277 pa_space
= isl_pw_aff_get_space(pa
);
6279 match
= isl_space_has_equal_params(space
, pa_space
);
6281 isl_space_free(pa_space
);
6285 /* Check that the domain space of "pa" matches "space".
6287 isl_stat
isl_pw_aff_check_match_domain_space(__isl_keep isl_pw_aff
*pa
,
6288 __isl_keep isl_space
*space
)
6290 isl_space
*pa_space
;
6294 return isl_stat_error
;
6296 pa_space
= isl_pw_aff_get_space(pa
);
6298 match
= isl_space_has_equal_params(space
, pa_space
);
6302 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
6303 "parameters don't match", goto error
);
6304 match
= isl_space_tuple_is_equal(space
, isl_dim_in
,
6305 pa_space
, isl_dim_in
);
6309 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
6310 "domains don't match", goto error
);
6311 isl_space_free(pa_space
);
6314 isl_space_free(pa_space
);
6315 return isl_stat_error
;
6323 #include <isl_multi_explicit_domain.c>
6324 #include <isl_multi_pw_aff_explicit_domain.c>
6325 #include <isl_multi_templ.c>
6326 #include <isl_multi_apply_set.c>
6327 #include <isl_multi_coalesce.c>
6328 #include <isl_multi_dims.c>
6329 #include <isl_multi_gist.c>
6330 #include <isl_multi_hash.c>
6331 #include <isl_multi_align_set.c>
6332 #include <isl_multi_intersect.c>
6334 /* Does "mpa" have a non-trivial explicit domain?
6336 * The explicit domain, if present, is trivial if it represents
6337 * an (obviously) universe set.
6339 isl_bool
isl_multi_pw_aff_has_non_trivial_domain(
6340 __isl_keep isl_multi_pw_aff
*mpa
)
6343 return isl_bool_error
;
6344 if (!isl_multi_pw_aff_has_explicit_domain(mpa
))
6345 return isl_bool_false
;
6346 return isl_bool_not(isl_set_plain_is_universe(mpa
->u
.dom
));
6349 /* Scale the elements of "pma" by the corresponding elements of "mv".
6351 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_scale_multi_val(
6352 __isl_take isl_pw_multi_aff
*pma
, __isl_take isl_multi_val
*mv
)
6355 isl_bool equal_params
;
6357 pma
= isl_pw_multi_aff_cow(pma
);
6360 if (!isl_space_tuple_is_equal(pma
->dim
, isl_dim_out
,
6361 mv
->space
, isl_dim_set
))
6362 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
6363 "spaces don't match", goto error
);
6364 equal_params
= isl_space_has_equal_params(pma
->dim
, mv
->space
);
6365 if (equal_params
< 0)
6367 if (!equal_params
) {
6368 pma
= isl_pw_multi_aff_align_params(pma
,
6369 isl_multi_val_get_space(mv
));
6370 mv
= isl_multi_val_align_params(mv
,
6371 isl_pw_multi_aff_get_space(pma
));
6376 for (i
= 0; i
< pma
->n
; ++i
) {
6377 pma
->p
[i
].maff
= isl_multi_aff_scale_multi_val(pma
->p
[i
].maff
,
6378 isl_multi_val_copy(mv
));
6379 if (!pma
->p
[i
].maff
)
6383 isl_multi_val_free(mv
);
6386 isl_multi_val_free(mv
);
6387 isl_pw_multi_aff_free(pma
);
6391 /* This function is called for each entry of an isl_union_pw_multi_aff.
6392 * If the space of the entry matches that of data->mv,
6393 * then apply isl_pw_multi_aff_scale_multi_val and return the result.
6394 * Otherwise, return an empty isl_pw_multi_aff.
6396 static __isl_give isl_pw_multi_aff
*union_pw_multi_aff_scale_multi_val_entry(
6397 __isl_take isl_pw_multi_aff
*pma
, void *user
)
6399 isl_multi_val
*mv
= user
;
6403 if (!isl_space_tuple_is_equal(pma
->dim
, isl_dim_out
,
6404 mv
->space
, isl_dim_set
)) {
6405 isl_space
*space
= isl_pw_multi_aff_get_space(pma
);
6406 isl_pw_multi_aff_free(pma
);
6407 return isl_pw_multi_aff_empty(space
);
6410 return isl_pw_multi_aff_scale_multi_val(pma
, isl_multi_val_copy(mv
));
6413 /* Scale the elements of "upma" by the corresponding elements of "mv",
6414 * for those entries that match the space of "mv".
6416 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_scale_multi_val(
6417 __isl_take isl_union_pw_multi_aff
*upma
, __isl_take isl_multi_val
*mv
)
6419 upma
= isl_union_pw_multi_aff_align_params(upma
,
6420 isl_multi_val_get_space(mv
));
6421 mv
= isl_multi_val_align_params(mv
,
6422 isl_union_pw_multi_aff_get_space(upma
));
6426 return isl_union_pw_multi_aff_transform(upma
,
6427 &union_pw_multi_aff_scale_multi_val_entry
, mv
);
6429 isl_multi_val_free(mv
);
6432 isl_multi_val_free(mv
);
6433 isl_union_pw_multi_aff_free(upma
);
6437 /* Construct and return a piecewise multi affine expression
6438 * in the given space with value zero in each of the output dimensions and
6439 * a universe domain.
6441 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_zero(__isl_take isl_space
*space
)
6443 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_zero(space
));
6446 /* Construct and return a piecewise multi affine expression
6447 * that is equal to the given piecewise affine expression.
6449 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_pw_aff(
6450 __isl_take isl_pw_aff
*pa
)
6454 isl_pw_multi_aff
*pma
;
6459 space
= isl_pw_aff_get_space(pa
);
6460 pma
= isl_pw_multi_aff_alloc_size(space
, pa
->n
);
6462 for (i
= 0; i
< pa
->n
; ++i
) {
6466 set
= isl_set_copy(pa
->p
[i
].set
);
6467 ma
= isl_multi_aff_from_aff(isl_aff_copy(pa
->p
[i
].aff
));
6468 pma
= isl_pw_multi_aff_add_piece(pma
, set
, ma
);
6471 isl_pw_aff_free(pa
);
6475 /* Construct a set or map mapping the shared (parameter) domain
6476 * of the piecewise affine expressions to the range of "mpa"
6477 * with each dimension in the range equated to the
6478 * corresponding piecewise affine expression.
6480 static __isl_give isl_map
*map_from_multi_pw_aff(
6481 __isl_take isl_multi_pw_aff
*mpa
)
6490 if (isl_space_dim(mpa
->space
, isl_dim_out
) != mpa
->n
)
6491 isl_die(isl_multi_pw_aff_get_ctx(mpa
), isl_error_internal
,
6492 "invalid space", goto error
);
6494 space
= isl_multi_pw_aff_get_domain_space(mpa
);
6495 map
= isl_map_universe(isl_space_from_domain(space
));
6497 for (i
= 0; i
< mpa
->n
; ++i
) {
6501 pa
= isl_pw_aff_copy(mpa
->u
.p
[i
]);
6502 map_i
= map_from_pw_aff(pa
);
6504 map
= isl_map_flat_range_product(map
, map_i
);
6507 map
= isl_map_reset_space(map
, isl_multi_pw_aff_get_space(mpa
));
6509 isl_multi_pw_aff_free(mpa
);
6512 isl_multi_pw_aff_free(mpa
);
6516 /* Construct a map mapping the shared domain
6517 * of the piecewise affine expressions to the range of "mpa"
6518 * with each dimension in the range equated to the
6519 * corresponding piecewise affine expression.
6521 __isl_give isl_map
*isl_map_from_multi_pw_aff(__isl_take isl_multi_pw_aff
*mpa
)
6525 if (isl_space_is_set(mpa
->space
))
6526 isl_die(isl_multi_pw_aff_get_ctx(mpa
), isl_error_internal
,
6527 "space of input is not a map", goto error
);
6529 return map_from_multi_pw_aff(mpa
);
6531 isl_multi_pw_aff_free(mpa
);
6535 /* Construct a set mapping the shared parameter domain
6536 * of the piecewise affine expressions to the space of "mpa"
6537 * with each dimension in the range equated to the
6538 * corresponding piecewise affine expression.
6540 __isl_give isl_set
*isl_set_from_multi_pw_aff(__isl_take isl_multi_pw_aff
*mpa
)
6544 if (!isl_space_is_set(mpa
->space
))
6545 isl_die(isl_multi_pw_aff_get_ctx(mpa
), isl_error_internal
,
6546 "space of input is not a set", goto error
);
6548 return map_from_multi_pw_aff(mpa
);
6550 isl_multi_pw_aff_free(mpa
);
6554 /* Construct and return a piecewise multi affine expression
6555 * that is equal to the given multi piecewise affine expression
6556 * on the shared domain of the piecewise affine expressions,
6557 * in the special case of a 0D multi piecewise affine expression.
6559 * Create a piecewise multi affine expression with the explicit domain of
6560 * the 0D multi piecewise affine expression as domain.
6562 static __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_multi_pw_aff_0D(
6563 __isl_take isl_multi_pw_aff
*mpa
)
6569 space
= isl_multi_pw_aff_get_space(mpa
);
6570 dom
= isl_multi_pw_aff_get_explicit_domain(mpa
);
6571 isl_multi_pw_aff_free(mpa
);
6573 ma
= isl_multi_aff_zero(space
);
6574 return isl_pw_multi_aff_alloc(dom
, ma
);
6577 /* Construct and return a piecewise multi affine expression
6578 * that is equal to the given multi piecewise affine expression
6579 * on the shared domain of the piecewise affine expressions.
6581 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_multi_pw_aff(
6582 __isl_take isl_multi_pw_aff
*mpa
)
6587 isl_pw_multi_aff
*pma
;
6593 return isl_pw_multi_aff_from_multi_pw_aff_0D(mpa
);
6595 space
= isl_multi_pw_aff_get_space(mpa
);
6596 pa
= isl_multi_pw_aff_get_pw_aff(mpa
, 0);
6597 pma
= isl_pw_multi_aff_from_pw_aff(pa
);
6599 for (i
= 1; i
< mpa
->n
; ++i
) {
6600 isl_pw_multi_aff
*pma_i
;
6602 pa
= isl_multi_pw_aff_get_pw_aff(mpa
, i
);
6603 pma_i
= isl_pw_multi_aff_from_pw_aff(pa
);
6604 pma
= isl_pw_multi_aff_range_product(pma
, pma_i
);
6607 pma
= isl_pw_multi_aff_reset_space(pma
, space
);
6609 isl_multi_pw_aff_free(mpa
);
6613 /* Construct and return a multi piecewise affine expression
6614 * that is equal to the given multi affine expression.
6616 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_from_multi_aff(
6617 __isl_take isl_multi_aff
*ma
)
6620 isl_multi_pw_aff
*mpa
;
6625 n
= isl_multi_aff_dim(ma
, isl_dim_out
);
6626 mpa
= isl_multi_pw_aff_alloc(isl_multi_aff_get_space(ma
));
6628 for (i
= 0; i
< n
; ++i
) {
6631 pa
= isl_pw_aff_from_aff(isl_multi_aff_get_aff(ma
, i
));
6632 mpa
= isl_multi_pw_aff_set_pw_aff(mpa
, i
, pa
);
6635 isl_multi_aff_free(ma
);
6639 /* Construct and return a multi piecewise affine expression
6640 * that is equal to the given piecewise multi affine expression.
6642 * If the resulting multi piecewise affine expression has
6643 * an explicit domain, then assign it the domain of the input.
6644 * In other cases, the domain is stored in the individual elements.
6646 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_from_pw_multi_aff(
6647 __isl_take isl_pw_multi_aff
*pma
)
6651 isl_multi_pw_aff
*mpa
;
6656 n
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
6657 space
= isl_pw_multi_aff_get_space(pma
);
6658 mpa
= isl_multi_pw_aff_alloc(space
);
6660 for (i
= 0; i
< n
; ++i
) {
6663 pa
= isl_pw_multi_aff_get_pw_aff(pma
, i
);
6664 mpa
= isl_multi_pw_aff_set_pw_aff(mpa
, i
, pa
);
6666 if (isl_multi_pw_aff_has_explicit_domain(mpa
)) {
6669 dom
= isl_pw_multi_aff_domain(isl_pw_multi_aff_copy(pma
));
6670 mpa
= isl_multi_pw_aff_intersect_domain(mpa
, dom
);
6673 isl_pw_multi_aff_free(pma
);
6677 /* Do "pa1" and "pa2" represent the same function?
6679 * We first check if they are obviously equal.
6680 * If not, we convert them to maps and check if those are equal.
6682 * If "pa1" or "pa2" contain any NaNs, then they are considered
6683 * not to be the same. A NaN is not equal to anything, not even
6686 isl_bool
isl_pw_aff_is_equal(__isl_keep isl_pw_aff
*pa1
,
6687 __isl_keep isl_pw_aff
*pa2
)
6691 isl_map
*map1
, *map2
;
6694 return isl_bool_error
;
6696 equal
= isl_pw_aff_plain_is_equal(pa1
, pa2
);
6697 if (equal
< 0 || equal
)
6699 has_nan
= either_involves_nan(pa1
, pa2
);
6701 return isl_bool_error
;
6703 return isl_bool_false
;
6705 map1
= map_from_pw_aff(isl_pw_aff_copy(pa1
));
6706 map2
= map_from_pw_aff(isl_pw_aff_copy(pa2
));
6707 equal
= isl_map_is_equal(map1
, map2
);
6714 /* Do "mpa1" and "mpa2" represent the same function?
6716 * Note that we cannot convert the entire isl_multi_pw_aff
6717 * to a map because the domains of the piecewise affine expressions
6718 * may not be the same.
6720 isl_bool
isl_multi_pw_aff_is_equal(__isl_keep isl_multi_pw_aff
*mpa1
,
6721 __isl_keep isl_multi_pw_aff
*mpa2
)
6724 isl_bool equal
, equal_params
;
6727 return isl_bool_error
;
6729 equal_params
= isl_space_has_equal_params(mpa1
->space
, mpa2
->space
);
6730 if (equal_params
< 0)
6731 return isl_bool_error
;
6732 if (!equal_params
) {
6733 if (!isl_space_has_named_params(mpa1
->space
))
6734 return isl_bool_false
;
6735 if (!isl_space_has_named_params(mpa2
->space
))
6736 return isl_bool_false
;
6737 mpa1
= isl_multi_pw_aff_copy(mpa1
);
6738 mpa2
= isl_multi_pw_aff_copy(mpa2
);
6739 mpa1
= isl_multi_pw_aff_align_params(mpa1
,
6740 isl_multi_pw_aff_get_space(mpa2
));
6741 mpa2
= isl_multi_pw_aff_align_params(mpa2
,
6742 isl_multi_pw_aff_get_space(mpa1
));
6743 equal
= isl_multi_pw_aff_is_equal(mpa1
, mpa2
);
6744 isl_multi_pw_aff_free(mpa1
);
6745 isl_multi_pw_aff_free(mpa2
);
6749 equal
= isl_space_is_equal(mpa1
->space
, mpa2
->space
);
6750 if (equal
< 0 || !equal
)
6753 for (i
= 0; i
< mpa1
->n
; ++i
) {
6754 equal
= isl_pw_aff_is_equal(mpa1
->u
.p
[i
], mpa2
->u
.p
[i
]);
6755 if (equal
< 0 || !equal
)
6759 return isl_bool_true
;
6762 /* Do "pma1" and "pma2" represent the same function?
6764 * First check if they are obviously equal.
6765 * If not, then convert them to maps and check if those are equal.
6767 * If "pa1" or "pa2" contain any NaNs, then they are considered
6768 * not to be the same. A NaN is not equal to anything, not even
6771 isl_bool
isl_pw_multi_aff_is_equal(__isl_keep isl_pw_multi_aff
*pma1
,
6772 __isl_keep isl_pw_multi_aff
*pma2
)
6776 isl_map
*map1
, *map2
;
6779 return isl_bool_error
;
6781 equal
= isl_pw_multi_aff_plain_is_equal(pma1
, pma2
);
6782 if (equal
< 0 || equal
)
6784 has_nan
= isl_pw_multi_aff_involves_nan(pma1
);
6785 if (has_nan
>= 0 && !has_nan
)
6786 has_nan
= isl_pw_multi_aff_involves_nan(pma2
);
6787 if (has_nan
< 0 || has_nan
)
6788 return isl_bool_not(has_nan
);
6790 map1
= isl_map_from_pw_multi_aff(isl_pw_multi_aff_copy(pma1
));
6791 map2
= isl_map_from_pw_multi_aff(isl_pw_multi_aff_copy(pma2
));
6792 equal
= isl_map_is_equal(map1
, map2
);
6799 /* Compute the pullback of "mpa" by the function represented by "ma".
6800 * In other words, plug in "ma" in "mpa".
6802 * The parameters of "mpa" and "ma" are assumed to have been aligned.
6804 * If "mpa" has an explicit domain, then it is this domain
6805 * that needs to undergo a pullback, i.e., a preimage.
6807 static __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_pullback_multi_aff_aligned(
6808 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_multi_aff
*ma
)
6811 isl_space
*space
= NULL
;
6813 mpa
= isl_multi_pw_aff_cow(mpa
);
6817 space
= isl_space_join(isl_multi_aff_get_space(ma
),
6818 isl_multi_pw_aff_get_space(mpa
));
6822 for (i
= 0; i
< mpa
->n
; ++i
) {
6823 mpa
->u
.p
[i
] = isl_pw_aff_pullback_multi_aff(mpa
->u
.p
[i
],
6824 isl_multi_aff_copy(ma
));
6828 if (isl_multi_pw_aff_has_explicit_domain(mpa
)) {
6829 mpa
->u
.dom
= isl_set_preimage_multi_aff(mpa
->u
.dom
,
6830 isl_multi_aff_copy(ma
));
6835 isl_multi_aff_free(ma
);
6836 isl_space_free(mpa
->space
);
6840 isl_space_free(space
);
6841 isl_multi_pw_aff_free(mpa
);
6842 isl_multi_aff_free(ma
);
6846 /* Compute the pullback of "mpa" by the function represented by "ma".
6847 * In other words, plug in "ma" in "mpa".
6849 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_pullback_multi_aff(
6850 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_multi_aff
*ma
)
6852 isl_bool equal_params
;
6856 equal_params
= isl_space_has_equal_params(mpa
->space
, ma
->space
);
6857 if (equal_params
< 0)
6860 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa
, ma
);
6861 mpa
= isl_multi_pw_aff_align_params(mpa
, isl_multi_aff_get_space(ma
));
6862 ma
= isl_multi_aff_align_params(ma
, isl_multi_pw_aff_get_space(mpa
));
6863 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa
, ma
);
6865 isl_multi_pw_aff_free(mpa
);
6866 isl_multi_aff_free(ma
);
6870 /* Compute the pullback of "mpa" by the function represented by "pma".
6871 * In other words, plug in "pma" in "mpa".
6873 * The parameters of "mpa" and "mpa" are assumed to have been aligned.
6875 * If "mpa" has an explicit domain, then it is this domain
6876 * that needs to undergo a pullback, i.e., a preimage.
6878 static __isl_give isl_multi_pw_aff
*
6879 isl_multi_pw_aff_pullback_pw_multi_aff_aligned(
6880 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_pw_multi_aff
*pma
)
6883 isl_space
*space
= NULL
;
6885 mpa
= isl_multi_pw_aff_cow(mpa
);
6889 space
= isl_space_join(isl_pw_multi_aff_get_space(pma
),
6890 isl_multi_pw_aff_get_space(mpa
));
6892 for (i
= 0; i
< mpa
->n
; ++i
) {
6893 mpa
->u
.p
[i
] = isl_pw_aff_pullback_pw_multi_aff_aligned(
6894 mpa
->u
.p
[i
], isl_pw_multi_aff_copy(pma
));
6898 if (isl_multi_pw_aff_has_explicit_domain(mpa
)) {
6899 mpa
->u
.dom
= isl_set_preimage_pw_multi_aff(mpa
->u
.dom
,
6900 isl_pw_multi_aff_copy(pma
));
6905 isl_pw_multi_aff_free(pma
);
6906 isl_space_free(mpa
->space
);
6910 isl_space_free(space
);
6911 isl_multi_pw_aff_free(mpa
);
6912 isl_pw_multi_aff_free(pma
);
6916 /* Compute the pullback of "mpa" by the function represented by "pma".
6917 * In other words, plug in "pma" in "mpa".
6919 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_pullback_pw_multi_aff(
6920 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_pw_multi_aff
*pma
)
6922 isl_bool equal_params
;
6926 equal_params
= isl_space_has_equal_params(mpa
->space
, pma
->dim
);
6927 if (equal_params
< 0)
6930 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa
, pma
);
6931 mpa
= isl_multi_pw_aff_align_params(mpa
,
6932 isl_pw_multi_aff_get_space(pma
));
6933 pma
= isl_pw_multi_aff_align_params(pma
,
6934 isl_multi_pw_aff_get_space(mpa
));
6935 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa
, pma
);
6937 isl_multi_pw_aff_free(mpa
);
6938 isl_pw_multi_aff_free(pma
);
6942 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
6943 * with the domain of "aff". The domain of the result is the same
6945 * "mpa" and "aff" are assumed to have been aligned.
6947 * We first extract the parametric constant from "aff", defined
6948 * over the correct domain.
6949 * Then we add the appropriate combinations of the members of "mpa".
6950 * Finally, we add the integer divisions through recursive calls.
6952 static __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_aff_aligned(
6953 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_aff
*aff
)
6961 n_in
= isl_aff_dim(aff
, isl_dim_in
);
6962 n_div
= isl_aff_dim(aff
, isl_dim_div
);
6964 space
= isl_space_domain(isl_multi_pw_aff_get_space(mpa
));
6965 tmp
= isl_aff_copy(aff
);
6966 tmp
= isl_aff_drop_dims(tmp
, isl_dim_div
, 0, n_div
);
6967 tmp
= isl_aff_drop_dims(tmp
, isl_dim_in
, 0, n_in
);
6968 tmp
= isl_aff_add_dims(tmp
, isl_dim_in
,
6969 isl_space_dim(space
, isl_dim_set
));
6970 tmp
= isl_aff_reset_domain_space(tmp
, space
);
6971 pa
= isl_pw_aff_from_aff(tmp
);
6973 for (i
= 0; i
< n_in
; ++i
) {
6976 if (!isl_aff_involves_dims(aff
, isl_dim_in
, i
, 1))
6978 v
= isl_aff_get_coefficient_val(aff
, isl_dim_in
, i
);
6979 pa_i
= isl_multi_pw_aff_get_pw_aff(mpa
, i
);
6980 pa_i
= isl_pw_aff_scale_val(pa_i
, v
);
6981 pa
= isl_pw_aff_add(pa
, pa_i
);
6984 for (i
= 0; i
< n_div
; ++i
) {
6988 if (!isl_aff_involves_dims(aff
, isl_dim_div
, i
, 1))
6990 div
= isl_aff_get_div(aff
, i
);
6991 pa_i
= isl_multi_pw_aff_apply_aff_aligned(
6992 isl_multi_pw_aff_copy(mpa
), div
);
6993 pa_i
= isl_pw_aff_floor(pa_i
);
6994 v
= isl_aff_get_coefficient_val(aff
, isl_dim_div
, i
);
6995 pa_i
= isl_pw_aff_scale_val(pa_i
, v
);
6996 pa
= isl_pw_aff_add(pa
, pa_i
);
6999 isl_multi_pw_aff_free(mpa
);
7005 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
7006 * with the domain of "aff". The domain of the result is the same
7009 __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_aff(
7010 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_aff
*aff
)
7012 isl_bool equal_params
;
7016 equal_params
= isl_space_has_equal_params(aff
->ls
->dim
, mpa
->space
);
7017 if (equal_params
< 0)
7020 return isl_multi_pw_aff_apply_aff_aligned(mpa
, aff
);
7022 aff
= isl_aff_align_params(aff
, isl_multi_pw_aff_get_space(mpa
));
7023 mpa
= isl_multi_pw_aff_align_params(mpa
, isl_aff_get_space(aff
));
7025 return isl_multi_pw_aff_apply_aff_aligned(mpa
, aff
);
7028 isl_multi_pw_aff_free(mpa
);
7032 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
7033 * with the domain of "pa". The domain of the result is the same
7035 * "mpa" and "pa" are assumed to have been aligned.
7037 * We consider each piece in turn. Note that the domains of the
7038 * pieces are assumed to be disjoint and they remain disjoint
7039 * after taking the preimage (over the same function).
7041 static __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_pw_aff_aligned(
7042 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_pw_aff
*pa
)
7051 space
= isl_space_join(isl_multi_pw_aff_get_space(mpa
),
7052 isl_pw_aff_get_space(pa
));
7053 res
= isl_pw_aff_empty(space
);
7055 for (i
= 0; i
< pa
->n
; ++i
) {
7059 pa_i
= isl_multi_pw_aff_apply_aff_aligned(
7060 isl_multi_pw_aff_copy(mpa
),
7061 isl_aff_copy(pa
->p
[i
].aff
));
7062 domain
= isl_set_copy(pa
->p
[i
].set
);
7063 domain
= isl_set_preimage_multi_pw_aff(domain
,
7064 isl_multi_pw_aff_copy(mpa
));
7065 pa_i
= isl_pw_aff_intersect_domain(pa_i
, domain
);
7066 res
= isl_pw_aff_add_disjoint(res
, pa_i
);
7069 isl_pw_aff_free(pa
);
7070 isl_multi_pw_aff_free(mpa
);
7073 isl_pw_aff_free(pa
);
7074 isl_multi_pw_aff_free(mpa
);
7078 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
7079 * with the domain of "pa". The domain of the result is the same
7082 __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_pw_aff(
7083 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_pw_aff
*pa
)
7085 isl_bool equal_params
;
7089 equal_params
= isl_space_has_equal_params(pa
->dim
, mpa
->space
);
7090 if (equal_params
< 0)
7093 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa
, pa
);
7095 pa
= isl_pw_aff_align_params(pa
, isl_multi_pw_aff_get_space(mpa
));
7096 mpa
= isl_multi_pw_aff_align_params(mpa
, isl_pw_aff_get_space(pa
));
7098 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa
, pa
);
7100 isl_pw_aff_free(pa
);
7101 isl_multi_pw_aff_free(mpa
);
7105 /* Compute the pullback of "pa" by the function represented by "mpa".
7106 * In other words, plug in "mpa" in "pa".
7107 * "pa" and "mpa" are assumed to have been aligned.
7109 * The pullback is computed by applying "pa" to "mpa".
7111 static __isl_give isl_pw_aff
*isl_pw_aff_pullback_multi_pw_aff_aligned(
7112 __isl_take isl_pw_aff
*pa
, __isl_take isl_multi_pw_aff
*mpa
)
7114 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa
, pa
);
7117 /* Compute the pullback of "pa" by the function represented by "mpa".
7118 * In other words, plug in "mpa" in "pa".
7120 * The pullback is computed by applying "pa" to "mpa".
7122 __isl_give isl_pw_aff
*isl_pw_aff_pullback_multi_pw_aff(
7123 __isl_take isl_pw_aff
*pa
, __isl_take isl_multi_pw_aff
*mpa
)
7125 return isl_multi_pw_aff_apply_pw_aff(mpa
, pa
);
7128 /* Compute the pullback of "mpa1" by the function represented by "mpa2".
7129 * In other words, plug in "mpa2" in "mpa1".
7131 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7133 * We pullback each member of "mpa1" in turn.
7135 * If "mpa1" has an explicit domain, then it is this domain
7136 * that needs to undergo a pullback instead, i.e., a preimage.
7138 static __isl_give isl_multi_pw_aff
*
7139 isl_multi_pw_aff_pullback_multi_pw_aff_aligned(
7140 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
)
7143 isl_space
*space
= NULL
;
7145 mpa1
= isl_multi_pw_aff_cow(mpa1
);
7149 space
= isl_space_join(isl_multi_pw_aff_get_space(mpa2
),
7150 isl_multi_pw_aff_get_space(mpa1
));
7152 for (i
= 0; i
< mpa1
->n
; ++i
) {
7153 mpa1
->u
.p
[i
] = isl_pw_aff_pullback_multi_pw_aff_aligned(
7154 mpa1
->u
.p
[i
], isl_multi_pw_aff_copy(mpa2
));
7159 if (isl_multi_pw_aff_has_explicit_domain(mpa1
)) {
7160 mpa1
->u
.dom
= isl_set_preimage_multi_pw_aff(mpa1
->u
.dom
,
7161 isl_multi_pw_aff_copy(mpa2
));
7165 mpa1
= isl_multi_pw_aff_reset_space(mpa1
, space
);
7167 isl_multi_pw_aff_free(mpa2
);
7170 isl_space_free(space
);
7171 isl_multi_pw_aff_free(mpa1
);
7172 isl_multi_pw_aff_free(mpa2
);
7176 /* Compute the pullback of "mpa1" by the function represented by "mpa2".
7177 * In other words, plug in "mpa2" in "mpa1".
7179 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_pullback_multi_pw_aff(
7180 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
)
7182 return isl_multi_pw_aff_align_params_multi_multi_and(mpa1
, mpa2
,
7183 &isl_multi_pw_aff_pullback_multi_pw_aff_aligned
);
7186 /* Align the parameters of "mpa1" and "mpa2", check that the ranges
7187 * of "mpa1" and "mpa2" live in the same space, construct map space
7188 * between the domain spaces of "mpa1" and "mpa2" and call "order"
7189 * with this map space as extract argument.
7191 static __isl_give isl_map
*isl_multi_pw_aff_order_map(
7192 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
,
7193 __isl_give isl_map
*(*order
)(__isl_keep isl_multi_pw_aff
*mpa1
,
7194 __isl_keep isl_multi_pw_aff
*mpa2
, __isl_take isl_space
*space
))
7197 isl_space
*space1
, *space2
;
7200 mpa1
= isl_multi_pw_aff_align_params(mpa1
,
7201 isl_multi_pw_aff_get_space(mpa2
));
7202 mpa2
= isl_multi_pw_aff_align_params(mpa2
,
7203 isl_multi_pw_aff_get_space(mpa1
));
7206 match
= isl_space_tuple_is_equal(mpa1
->space
, isl_dim_out
,
7207 mpa2
->space
, isl_dim_out
);
7211 isl_die(isl_multi_pw_aff_get_ctx(mpa1
), isl_error_invalid
,
7212 "range spaces don't match", goto error
);
7213 space1
= isl_space_domain(isl_multi_pw_aff_get_space(mpa1
));
7214 space2
= isl_space_domain(isl_multi_pw_aff_get_space(mpa2
));
7215 space1
= isl_space_map_from_domain_and_range(space1
, space2
);
7217 res
= order(mpa1
, mpa2
, space1
);
7218 isl_multi_pw_aff_free(mpa1
);
7219 isl_multi_pw_aff_free(mpa2
);
7222 isl_multi_pw_aff_free(mpa1
);
7223 isl_multi_pw_aff_free(mpa2
);
7227 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7228 * where the function values are equal. "space" is the space of the result.
7229 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7231 * "mpa1" and "mpa2" are equal when each of the pairs of elements
7232 * in the sequences are equal.
7234 static __isl_give isl_map
*isl_multi_pw_aff_eq_map_on_space(
7235 __isl_keep isl_multi_pw_aff
*mpa1
, __isl_keep isl_multi_pw_aff
*mpa2
,
7236 __isl_take isl_space
*space
)
7241 res
= isl_map_universe(space
);
7243 n
= isl_multi_pw_aff_dim(mpa1
, isl_dim_out
);
7244 for (i
= 0; i
< n
; ++i
) {
7245 isl_pw_aff
*pa1
, *pa2
;
7248 pa1
= isl_multi_pw_aff_get_pw_aff(mpa1
, i
);
7249 pa2
= isl_multi_pw_aff_get_pw_aff(mpa2
, i
);
7250 map
= isl_pw_aff_eq_map(pa1
, pa2
);
7251 res
= isl_map_intersect(res
, map
);
7257 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7258 * where the function values are equal.
7260 __isl_give isl_map
*isl_multi_pw_aff_eq_map(__isl_take isl_multi_pw_aff
*mpa1
,
7261 __isl_take isl_multi_pw_aff
*mpa2
)
7263 return isl_multi_pw_aff_order_map(mpa1
, mpa2
,
7264 &isl_multi_pw_aff_eq_map_on_space
);
7267 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7268 * where the function values of "mpa1" is lexicographically satisfies "base"
7269 * compared to that of "mpa2". "space" is the space of the result.
7270 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7272 * "mpa1" lexicographically satisfies "base" compared to "mpa2"
7273 * if its i-th element satisfies "base" when compared to
7274 * the i-th element of "mpa2" while all previous elements are
7277 static __isl_give isl_map
*isl_multi_pw_aff_lex_map_on_space(
7278 __isl_keep isl_multi_pw_aff
*mpa1
, __isl_keep isl_multi_pw_aff
*mpa2
,
7279 __isl_give isl_map
*(*base
)(__isl_take isl_pw_aff
*pa1
,
7280 __isl_take isl_pw_aff
*pa2
),
7281 __isl_take isl_space
*space
)
7284 isl_map
*res
, *rest
;
7286 res
= isl_map_empty(isl_space_copy(space
));
7287 rest
= isl_map_universe(space
);
7289 n
= isl_multi_pw_aff_dim(mpa1
, isl_dim_out
);
7290 for (i
= 0; i
< n
; ++i
) {
7291 isl_pw_aff
*pa1
, *pa2
;
7294 pa1
= isl_multi_pw_aff_get_pw_aff(mpa1
, i
);
7295 pa2
= isl_multi_pw_aff_get_pw_aff(mpa2
, i
);
7296 map
= base(pa1
, pa2
);
7297 map
= isl_map_intersect(map
, isl_map_copy(rest
));
7298 res
= isl_map_union(res
, map
);
7303 pa1
= isl_multi_pw_aff_get_pw_aff(mpa1
, i
);
7304 pa2
= isl_multi_pw_aff_get_pw_aff(mpa2
, i
);
7305 map
= isl_pw_aff_eq_map(pa1
, pa2
);
7306 rest
= isl_map_intersect(rest
, map
);
7313 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7314 * where the function value of "mpa1" is lexicographically less than that
7315 * of "mpa2". "space" is the space of the result.
7316 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7318 * "mpa1" is less than "mpa2" if its i-th element is smaller
7319 * than the i-th element of "mpa2" while all previous elements are
7322 __isl_give isl_map
*isl_multi_pw_aff_lex_lt_map_on_space(
7323 __isl_keep isl_multi_pw_aff
*mpa1
, __isl_keep isl_multi_pw_aff
*mpa2
,
7324 __isl_take isl_space
*space
)
7326 return isl_multi_pw_aff_lex_map_on_space(mpa1
, mpa2
,
7327 &isl_pw_aff_lt_map
, space
);
7330 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7331 * where the function value of "mpa1" is lexicographically less than that
7334 __isl_give isl_map
*isl_multi_pw_aff_lex_lt_map(
7335 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
)
7337 return isl_multi_pw_aff_order_map(mpa1
, mpa2
,
7338 &isl_multi_pw_aff_lex_lt_map_on_space
);
7341 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7342 * where the function value of "mpa1" is lexicographically greater than that
7343 * of "mpa2". "space" is the space of the result.
7344 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7346 * "mpa1" is greater than "mpa2" if its i-th element is greater
7347 * than the i-th element of "mpa2" while all previous elements are
7350 __isl_give isl_map
*isl_multi_pw_aff_lex_gt_map_on_space(
7351 __isl_keep isl_multi_pw_aff
*mpa1
, __isl_keep isl_multi_pw_aff
*mpa2
,
7352 __isl_take isl_space
*space
)
7354 return isl_multi_pw_aff_lex_map_on_space(mpa1
, mpa2
,
7355 &isl_pw_aff_gt_map
, space
);
7358 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7359 * where the function value of "mpa1" is lexicographically greater than that
7362 __isl_give isl_map
*isl_multi_pw_aff_lex_gt_map(
7363 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
)
7365 return isl_multi_pw_aff_order_map(mpa1
, mpa2
,
7366 &isl_multi_pw_aff_lex_gt_map_on_space
);
7369 /* Compare two isl_affs.
7371 * Return -1 if "aff1" is "smaller" than "aff2", 1 if "aff1" is "greater"
7372 * than "aff2" and 0 if they are equal.
7374 * The order is fairly arbitrary. We do consider expressions that only involve
7375 * earlier dimensions as "smaller".
7377 int isl_aff_plain_cmp(__isl_keep isl_aff
*aff1
, __isl_keep isl_aff
*aff2
)
7390 cmp
= isl_local_space_cmp(aff1
->ls
, aff2
->ls
);
7394 last1
= isl_seq_last_non_zero(aff1
->v
->el
+ 1, aff1
->v
->size
- 1);
7395 last2
= isl_seq_last_non_zero(aff2
->v
->el
+ 1, aff1
->v
->size
- 1);
7397 return last1
- last2
;
7399 return isl_seq_cmp(aff1
->v
->el
, aff2
->v
->el
, aff1
->v
->size
);
7402 /* Compare two isl_pw_affs.
7404 * Return -1 if "pa1" is "smaller" than "pa2", 1 if "pa1" is "greater"
7405 * than "pa2" and 0 if they are equal.
7407 * The order is fairly arbitrary. We do consider expressions that only involve
7408 * earlier dimensions as "smaller".
7410 int isl_pw_aff_plain_cmp(__isl_keep isl_pw_aff
*pa1
,
7411 __isl_keep isl_pw_aff
*pa2
)
7424 cmp
= isl_space_cmp(pa1
->dim
, pa2
->dim
);
7428 if (pa1
->n
!= pa2
->n
)
7429 return pa1
->n
- pa2
->n
;
7431 for (i
= 0; i
< pa1
->n
; ++i
) {
7432 cmp
= isl_set_plain_cmp(pa1
->p
[i
].set
, pa2
->p
[i
].set
);
7435 cmp
= isl_aff_plain_cmp(pa1
->p
[i
].aff
, pa2
->p
[i
].aff
);
7443 /* Return a piecewise affine expression that is equal to "v" on "domain".
7445 __isl_give isl_pw_aff
*isl_pw_aff_val_on_domain(__isl_take isl_set
*domain
,
7446 __isl_take isl_val
*v
)
7449 isl_local_space
*ls
;
7452 space
= isl_set_get_space(domain
);
7453 ls
= isl_local_space_from_space(space
);
7454 aff
= isl_aff_val_on_domain(ls
, v
);
7456 return isl_pw_aff_alloc(domain
, aff
);
7459 /* Return a multi affine expression that is equal to "mv" on domain
7462 __isl_give isl_multi_aff
*isl_multi_aff_multi_val_on_space(
7463 __isl_take isl_space
*space
, __isl_take isl_multi_val
*mv
)
7467 isl_local_space
*ls
;
7473 n
= isl_multi_val_dim(mv
, isl_dim_set
);
7474 space2
= isl_multi_val_get_space(mv
);
7475 space2
= isl_space_align_params(space2
, isl_space_copy(space
));
7476 space
= isl_space_align_params(space
, isl_space_copy(space2
));
7477 space
= isl_space_map_from_domain_and_range(space
, space2
);
7478 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
7479 ls
= isl_local_space_from_space(isl_space_domain(space
));
7480 for (i
= 0; i
< n
; ++i
) {
7484 v
= isl_multi_val_get_val(mv
, i
);
7485 aff
= isl_aff_val_on_domain(isl_local_space_copy(ls
), v
);
7486 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
7488 isl_local_space_free(ls
);
7490 isl_multi_val_free(mv
);
7493 isl_space_free(space
);
7494 isl_multi_val_free(mv
);
7498 /* Return a piecewise multi-affine expression
7499 * that is equal to "mv" on "domain".
7501 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_multi_val_on_domain(
7502 __isl_take isl_set
*domain
, __isl_take isl_multi_val
*mv
)
7507 space
= isl_set_get_space(domain
);
7508 ma
= isl_multi_aff_multi_val_on_space(space
, mv
);
7510 return isl_pw_multi_aff_alloc(domain
, ma
);
7513 /* Internal data structure for isl_union_pw_multi_aff_multi_val_on_domain.
7514 * mv is the value that should be attained on each domain set
7515 * res collects the results
7517 struct isl_union_pw_multi_aff_multi_val_on_domain_data
{
7519 isl_union_pw_multi_aff
*res
;
7522 /* Create an isl_pw_multi_aff equal to data->mv on "domain"
7523 * and add it to data->res.
7525 static isl_stat
pw_multi_aff_multi_val_on_domain(__isl_take isl_set
*domain
,
7528 struct isl_union_pw_multi_aff_multi_val_on_domain_data
*data
= user
;
7529 isl_pw_multi_aff
*pma
;
7532 mv
= isl_multi_val_copy(data
->mv
);
7533 pma
= isl_pw_multi_aff_multi_val_on_domain(domain
, mv
);
7534 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma
);
7536 return data
->res
? isl_stat_ok
: isl_stat_error
;
7539 /* Return a union piecewise multi-affine expression
7540 * that is equal to "mv" on "domain".
7542 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_multi_val_on_domain(
7543 __isl_take isl_union_set
*domain
, __isl_take isl_multi_val
*mv
)
7545 struct isl_union_pw_multi_aff_multi_val_on_domain_data data
;
7548 space
= isl_union_set_get_space(domain
);
7549 data
.res
= isl_union_pw_multi_aff_empty(space
);
7551 if (isl_union_set_foreach_set(domain
,
7552 &pw_multi_aff_multi_val_on_domain
, &data
) < 0)
7553 data
.res
= isl_union_pw_multi_aff_free(data
.res
);
7554 isl_union_set_free(domain
);
7555 isl_multi_val_free(mv
);
7559 /* Compute the pullback of data->pma by the function represented by "pma2",
7560 * provided the spaces match, and add the results to data->res.
7562 static isl_stat
pullback_entry(__isl_take isl_pw_multi_aff
*pma2
, void *user
)
7564 struct isl_union_pw_multi_aff_bin_data
*data
= user
;
7566 if (!isl_space_tuple_is_equal(data
->pma
->dim
, isl_dim_in
,
7567 pma2
->dim
, isl_dim_out
)) {
7568 isl_pw_multi_aff_free(pma2
);
7572 pma2
= isl_pw_multi_aff_pullback_pw_multi_aff(
7573 isl_pw_multi_aff_copy(data
->pma
), pma2
);
7575 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma2
);
7577 return isl_stat_error
;
7582 /* Compute the pullback of "upma1" by the function represented by "upma2".
7584 __isl_give isl_union_pw_multi_aff
*
7585 isl_union_pw_multi_aff_pullback_union_pw_multi_aff(
7586 __isl_take isl_union_pw_multi_aff
*upma1
,
7587 __isl_take isl_union_pw_multi_aff
*upma2
)
7589 return bin_op(upma1
, upma2
, &pullback_entry
);
7592 /* Check that the domain space of "upa" matches "space".
7594 * This function is called from isl_multi_union_pw_aff_set_union_pw_aff and
7595 * can in principle never fail since the space "space" is that
7596 * of the isl_multi_union_pw_aff and is a set space such that
7597 * there is no domain space to match.
7599 * We check the parameters and double-check that "space" is
7600 * indeed that of a set.
7602 static isl_stat
isl_union_pw_aff_check_match_domain_space(
7603 __isl_keep isl_union_pw_aff
*upa
, __isl_keep isl_space
*space
)
7605 isl_space
*upa_space
;
7609 return isl_stat_error
;
7611 match
= isl_space_is_set(space
);
7613 return isl_stat_error
;
7615 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
7616 "expecting set space", return isl_stat_error
);
7618 upa_space
= isl_union_pw_aff_get_space(upa
);
7619 match
= isl_space_has_equal_params(space
, upa_space
);
7623 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
7624 "parameters don't match", goto error
);
7626 isl_space_free(upa_space
);
7629 isl_space_free(upa_space
);
7630 return isl_stat_error
;
7633 /* Do the parameters of "upa" match those of "space"?
7635 static isl_bool
isl_union_pw_aff_matching_params(
7636 __isl_keep isl_union_pw_aff
*upa
, __isl_keep isl_space
*space
)
7638 isl_space
*upa_space
;
7642 return isl_bool_error
;
7644 upa_space
= isl_union_pw_aff_get_space(upa
);
7646 match
= isl_space_has_equal_params(space
, upa_space
);
7648 isl_space_free(upa_space
);
7652 /* Internal data structure for isl_union_pw_aff_reset_domain_space.
7653 * space represents the new parameters.
7654 * res collects the results.
7656 struct isl_union_pw_aff_reset_params_data
{
7658 isl_union_pw_aff
*res
;
7661 /* Replace the parameters of "pa" by data->space and
7662 * add the result to data->res.
7664 static isl_stat
reset_params(__isl_take isl_pw_aff
*pa
, void *user
)
7666 struct isl_union_pw_aff_reset_params_data
*data
= user
;
7669 space
= isl_pw_aff_get_space(pa
);
7670 space
= isl_space_replace_params(space
, data
->space
);
7671 pa
= isl_pw_aff_reset_space(pa
, space
);
7672 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
7674 return data
->res
? isl_stat_ok
: isl_stat_error
;
7677 /* Replace the domain space of "upa" by "space".
7678 * Since a union expression does not have a (single) domain space,
7679 * "space" is necessarily a parameter space.
7681 * Since the order and the names of the parameters determine
7682 * the hash value, we need to create a new hash table.
7684 static __isl_give isl_union_pw_aff
*isl_union_pw_aff_reset_domain_space(
7685 __isl_take isl_union_pw_aff
*upa
, __isl_take isl_space
*space
)
7687 struct isl_union_pw_aff_reset_params_data data
= { space
};
7690 match
= isl_union_pw_aff_matching_params(upa
, space
);
7692 upa
= isl_union_pw_aff_free(upa
);
7694 isl_space_free(space
);
7698 data
.res
= isl_union_pw_aff_empty(isl_space_copy(space
));
7699 if (isl_union_pw_aff_foreach_pw_aff(upa
, &reset_params
, &data
) < 0)
7700 data
.res
= isl_union_pw_aff_free(data
.res
);
7702 isl_union_pw_aff_free(upa
);
7703 isl_space_free(space
);
7707 /* Return the floor of "pa".
7709 static __isl_give isl_pw_aff
*floor_entry(__isl_take isl_pw_aff
*pa
, void *user
)
7711 return isl_pw_aff_floor(pa
);
7714 /* Given f, return floor(f).
7716 __isl_give isl_union_pw_aff
*isl_union_pw_aff_floor(
7717 __isl_take isl_union_pw_aff
*upa
)
7719 return isl_union_pw_aff_transform_inplace(upa
, &floor_entry
, NULL
);
7724 * upa mod m = upa - m * floor(upa/m)
7726 * with m an integer value.
7728 __isl_give isl_union_pw_aff
*isl_union_pw_aff_mod_val(
7729 __isl_take isl_union_pw_aff
*upa
, __isl_take isl_val
*m
)
7731 isl_union_pw_aff
*res
;
7736 if (!isl_val_is_int(m
))
7737 isl_die(isl_val_get_ctx(m
), isl_error_invalid
,
7738 "expecting integer modulo", goto error
);
7739 if (!isl_val_is_pos(m
))
7740 isl_die(isl_val_get_ctx(m
), isl_error_invalid
,
7741 "expecting positive modulo", goto error
);
7743 res
= isl_union_pw_aff_copy(upa
);
7744 upa
= isl_union_pw_aff_scale_down_val(upa
, isl_val_copy(m
));
7745 upa
= isl_union_pw_aff_floor(upa
);
7746 upa
= isl_union_pw_aff_scale_val(upa
, m
);
7747 res
= isl_union_pw_aff_sub(res
, upa
);
7752 isl_union_pw_aff_free(upa
);
7756 /* Internal data structure for isl_union_pw_multi_aff_get_union_pw_aff.
7757 * pos is the output position that needs to be extracted.
7758 * res collects the results.
7760 struct isl_union_pw_multi_aff_get_union_pw_aff_data
{
7762 isl_union_pw_aff
*res
;
7765 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma"
7766 * (assuming it has such a dimension) and add it to data->res.
7768 static isl_stat
get_union_pw_aff(__isl_take isl_pw_multi_aff
*pma
, void *user
)
7770 struct isl_union_pw_multi_aff_get_union_pw_aff_data
*data
= user
;
7775 return isl_stat_error
;
7777 n_out
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
7778 if (data
->pos
>= n_out
) {
7779 isl_pw_multi_aff_free(pma
);
7783 pa
= isl_pw_multi_aff_get_pw_aff(pma
, data
->pos
);
7784 isl_pw_multi_aff_free(pma
);
7786 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
7788 return data
->res
? isl_stat_ok
: isl_stat_error
;
7791 /* Extract an isl_union_pw_aff corresponding to
7792 * output dimension "pos" of "upma".
7794 __isl_give isl_union_pw_aff
*isl_union_pw_multi_aff_get_union_pw_aff(
7795 __isl_keep isl_union_pw_multi_aff
*upma
, int pos
)
7797 struct isl_union_pw_multi_aff_get_union_pw_aff_data data
;
7804 isl_die(isl_union_pw_multi_aff_get_ctx(upma
), isl_error_invalid
,
7805 "cannot extract at negative position", return NULL
);
7807 space
= isl_union_pw_multi_aff_get_space(upma
);
7808 data
.res
= isl_union_pw_aff_empty(space
);
7810 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma
,
7811 &get_union_pw_aff
, &data
) < 0)
7812 data
.res
= isl_union_pw_aff_free(data
.res
);
7817 /* Return a union piecewise affine expression
7818 * that is equal to "aff" on "domain".
7820 __isl_give isl_union_pw_aff
*isl_union_pw_aff_aff_on_domain(
7821 __isl_take isl_union_set
*domain
, __isl_take isl_aff
*aff
)
7825 pa
= isl_pw_aff_from_aff(aff
);
7826 return isl_union_pw_aff_pw_aff_on_domain(domain
, pa
);
7829 /* Return a union piecewise affine expression
7830 * that is equal to the parameter identified by "id" on "domain".
7832 * Make sure the parameter appears in the space passed to
7833 * isl_aff_param_on_domain_space_id.
7835 __isl_give isl_union_pw_aff
*isl_union_pw_aff_param_on_domain_id(
7836 __isl_take isl_union_set
*domain
, __isl_take isl_id
*id
)
7841 space
= isl_union_set_get_space(domain
);
7842 space
= isl_space_add_param_id(space
, isl_id_copy(id
));
7843 aff
= isl_aff_param_on_domain_space_id(space
, id
);
7844 return isl_union_pw_aff_aff_on_domain(domain
, aff
);
7847 /* Internal data structure for isl_union_pw_aff_pw_aff_on_domain.
7848 * "pa" is the piecewise symbolic value that the resulting isl_union_pw_aff
7850 * "res" collects the results.
7852 struct isl_union_pw_aff_pw_aff_on_domain_data
{
7854 isl_union_pw_aff
*res
;
7857 /* Construct a piecewise affine expression that is equal to data->pa
7858 * on "domain" and add the result to data->res.
7860 static isl_stat
pw_aff_on_domain(__isl_take isl_set
*domain
, void *user
)
7862 struct isl_union_pw_aff_pw_aff_on_domain_data
*data
= user
;
7866 pa
= isl_pw_aff_copy(data
->pa
);
7867 dim
= isl_set_dim(domain
, isl_dim_set
);
7868 pa
= isl_pw_aff_from_range(pa
);
7869 pa
= isl_pw_aff_add_dims(pa
, isl_dim_in
, dim
);
7870 pa
= isl_pw_aff_reset_domain_space(pa
, isl_set_get_space(domain
));
7871 pa
= isl_pw_aff_intersect_domain(pa
, domain
);
7872 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
7874 return data
->res
? isl_stat_ok
: isl_stat_error
;
7877 /* Return a union piecewise affine expression
7878 * that is equal to "pa" on "domain", assuming "domain" and "pa"
7879 * have been aligned.
7881 * Construct an isl_pw_aff on each of the sets in "domain" and
7882 * collect the results.
7884 static __isl_give isl_union_pw_aff
*isl_union_pw_aff_pw_aff_on_domain_aligned(
7885 __isl_take isl_union_set
*domain
, __isl_take isl_pw_aff
*pa
)
7887 struct isl_union_pw_aff_pw_aff_on_domain_data data
;
7890 space
= isl_union_set_get_space(domain
);
7891 data
.res
= isl_union_pw_aff_empty(space
);
7893 if (isl_union_set_foreach_set(domain
, &pw_aff_on_domain
, &data
) < 0)
7894 data
.res
= isl_union_pw_aff_free(data
.res
);
7895 isl_union_set_free(domain
);
7896 isl_pw_aff_free(pa
);
7900 /* Return a union piecewise affine expression
7901 * that is equal to "pa" on "domain".
7903 * Check that "pa" is a parametric expression,
7904 * align the parameters if needed and call
7905 * isl_union_pw_aff_pw_aff_on_domain_aligned.
7907 __isl_give isl_union_pw_aff
*isl_union_pw_aff_pw_aff_on_domain(
7908 __isl_take isl_union_set
*domain
, __isl_take isl_pw_aff
*pa
)
7911 isl_bool equal_params
;
7912 isl_space
*domain_space
, *pa_space
;
7914 pa_space
= isl_pw_aff_peek_space(pa
);
7915 is_set
= isl_space_is_set(pa_space
);
7919 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
7920 "expecting parametric expression", goto error
);
7922 domain_space
= isl_union_set_get_space(domain
);
7923 pa_space
= isl_pw_aff_get_space(pa
);
7924 equal_params
= isl_space_has_equal_params(domain_space
, pa_space
);
7925 if (equal_params
>= 0 && !equal_params
) {
7928 space
= isl_space_align_params(domain_space
, pa_space
);
7929 pa
= isl_pw_aff_align_params(pa
, isl_space_copy(space
));
7930 domain
= isl_union_set_align_params(domain
, space
);
7932 isl_space_free(domain_space
);
7933 isl_space_free(pa_space
);
7936 if (equal_params
< 0)
7938 return isl_union_pw_aff_pw_aff_on_domain_aligned(domain
, pa
);
7940 isl_union_set_free(domain
);
7941 isl_pw_aff_free(pa
);
7945 /* Internal data structure for isl_union_pw_aff_val_on_domain.
7946 * "v" is the value that the resulting isl_union_pw_aff needs to attain.
7947 * "res" collects the results.
7949 struct isl_union_pw_aff_val_on_domain_data
{
7951 isl_union_pw_aff
*res
;
7954 /* Construct a piecewise affine expression that is equal to data->v
7955 * on "domain" and add the result to data->res.
7957 static isl_stat
pw_aff_val_on_domain(__isl_take isl_set
*domain
, void *user
)
7959 struct isl_union_pw_aff_val_on_domain_data
*data
= user
;
7963 v
= isl_val_copy(data
->v
);
7964 pa
= isl_pw_aff_val_on_domain(domain
, v
);
7965 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
7967 return data
->res
? isl_stat_ok
: isl_stat_error
;
7970 /* Return a union piecewise affine expression
7971 * that is equal to "v" on "domain".
7973 * Construct an isl_pw_aff on each of the sets in "domain" and
7974 * collect the results.
7976 __isl_give isl_union_pw_aff
*isl_union_pw_aff_val_on_domain(
7977 __isl_take isl_union_set
*domain
, __isl_take isl_val
*v
)
7979 struct isl_union_pw_aff_val_on_domain_data data
;
7982 space
= isl_union_set_get_space(domain
);
7983 data
.res
= isl_union_pw_aff_empty(space
);
7985 if (isl_union_set_foreach_set(domain
, &pw_aff_val_on_domain
, &data
) < 0)
7986 data
.res
= isl_union_pw_aff_free(data
.res
);
7987 isl_union_set_free(domain
);
7992 /* Construct a piecewise multi affine expression
7993 * that is equal to "pa" and add it to upma.
7995 static isl_stat
pw_multi_aff_from_pw_aff_entry(__isl_take isl_pw_aff
*pa
,
7998 isl_union_pw_multi_aff
**upma
= user
;
7999 isl_pw_multi_aff
*pma
;
8001 pma
= isl_pw_multi_aff_from_pw_aff(pa
);
8002 *upma
= isl_union_pw_multi_aff_add_pw_multi_aff(*upma
, pma
);
8004 return *upma
? isl_stat_ok
: isl_stat_error
;
8007 /* Construct and return a union piecewise multi affine expression
8008 * that is equal to the given union piecewise affine expression.
8010 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_union_pw_aff(
8011 __isl_take isl_union_pw_aff
*upa
)
8014 isl_union_pw_multi_aff
*upma
;
8019 space
= isl_union_pw_aff_get_space(upa
);
8020 upma
= isl_union_pw_multi_aff_empty(space
);
8022 if (isl_union_pw_aff_foreach_pw_aff(upa
,
8023 &pw_multi_aff_from_pw_aff_entry
, &upma
) < 0)
8024 upma
= isl_union_pw_multi_aff_free(upma
);
8026 isl_union_pw_aff_free(upa
);
8030 /* Compute the set of elements in the domain of "pa" where it is zero and
8031 * add this set to "uset".
8033 static isl_stat
zero_union_set(__isl_take isl_pw_aff
*pa
, void *user
)
8035 isl_union_set
**uset
= (isl_union_set
**)user
;
8037 *uset
= isl_union_set_add_set(*uset
, isl_pw_aff_zero_set(pa
));
8039 return *uset
? isl_stat_ok
: isl_stat_error
;
8042 /* Return a union set containing those elements in the domain
8043 * of "upa" where it is zero.
8045 __isl_give isl_union_set
*isl_union_pw_aff_zero_union_set(
8046 __isl_take isl_union_pw_aff
*upa
)
8048 isl_union_set
*zero
;
8050 zero
= isl_union_set_empty(isl_union_pw_aff_get_space(upa
));
8051 if (isl_union_pw_aff_foreach_pw_aff(upa
, &zero_union_set
, &zero
) < 0)
8052 zero
= isl_union_set_free(zero
);
8054 isl_union_pw_aff_free(upa
);
8058 /* Convert "pa" to an isl_map and add it to *umap.
8060 static isl_stat
map_from_pw_aff_entry(__isl_take isl_pw_aff
*pa
, void *user
)
8062 isl_union_map
**umap
= user
;
8065 map
= isl_map_from_pw_aff(pa
);
8066 *umap
= isl_union_map_add_map(*umap
, map
);
8068 return *umap
? isl_stat_ok
: isl_stat_error
;
8071 /* Construct a union map mapping the domain of the union
8072 * piecewise affine expression to its range, with the single output dimension
8073 * equated to the corresponding affine expressions on their cells.
8075 __isl_give isl_union_map
*isl_union_map_from_union_pw_aff(
8076 __isl_take isl_union_pw_aff
*upa
)
8079 isl_union_map
*umap
;
8084 space
= isl_union_pw_aff_get_space(upa
);
8085 umap
= isl_union_map_empty(space
);
8087 if (isl_union_pw_aff_foreach_pw_aff(upa
, &map_from_pw_aff_entry
,
8089 umap
= isl_union_map_free(umap
);
8091 isl_union_pw_aff_free(upa
);
8095 /* Internal data structure for isl_union_pw_aff_pullback_union_pw_multi_aff.
8096 * upma is the function that is plugged in.
8097 * pa is the current part of the function in which upma is plugged in.
8098 * res collects the results.
8100 struct isl_union_pw_aff_pullback_upma_data
{
8101 isl_union_pw_multi_aff
*upma
;
8103 isl_union_pw_aff
*res
;
8106 /* Check if "pma" can be plugged into data->pa.
8107 * If so, perform the pullback and add the result to data->res.
8109 static isl_stat
pa_pb_pma(__isl_take isl_pw_multi_aff
*pma
, void *user
)
8111 struct isl_union_pw_aff_pullback_upma_data
*data
= user
;
8114 if (!isl_space_tuple_is_equal(data
->pa
->dim
, isl_dim_in
,
8115 pma
->dim
, isl_dim_out
)) {
8116 isl_pw_multi_aff_free(pma
);
8120 pa
= isl_pw_aff_copy(data
->pa
);
8121 pa
= isl_pw_aff_pullback_pw_multi_aff(pa
, pma
);
8123 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
8125 return data
->res
? isl_stat_ok
: isl_stat_error
;
8128 /* Check if any of the elements of data->upma can be plugged into pa,
8129 * add if so add the result to data->res.
8131 static isl_stat
upa_pb_upma(__isl_take isl_pw_aff
*pa
, void *user
)
8133 struct isl_union_pw_aff_pullback_upma_data
*data
= user
;
8137 r
= isl_union_pw_multi_aff_foreach_pw_multi_aff(data
->upma
,
8139 isl_pw_aff_free(pa
);
8144 /* Compute the pullback of "upa" by the function represented by "upma".
8145 * In other words, plug in "upma" in "upa". The result contains
8146 * expressions defined over the domain space of "upma".
8148 * Run over all pairs of elements in "upa" and "upma", perform
8149 * the pullback when appropriate and collect the results.
8150 * If the hash value were based on the domain space rather than
8151 * the function space, then we could run through all elements
8152 * of "upma" and directly pick out the corresponding element of "upa".
8154 __isl_give isl_union_pw_aff
*isl_union_pw_aff_pullback_union_pw_multi_aff(
8155 __isl_take isl_union_pw_aff
*upa
,
8156 __isl_take isl_union_pw_multi_aff
*upma
)
8158 struct isl_union_pw_aff_pullback_upma_data data
= { NULL
, NULL
};
8161 space
= isl_union_pw_multi_aff_get_space(upma
);
8162 upa
= isl_union_pw_aff_align_params(upa
, space
);
8163 space
= isl_union_pw_aff_get_space(upa
);
8164 upma
= isl_union_pw_multi_aff_align_params(upma
, space
);
8170 data
.res
= isl_union_pw_aff_alloc_same_size(upa
);
8171 if (isl_union_pw_aff_foreach_pw_aff(upa
, &upa_pb_upma
, &data
) < 0)
8172 data
.res
= isl_union_pw_aff_free(data
.res
);
8174 isl_union_pw_aff_free(upa
);
8175 isl_union_pw_multi_aff_free(upma
);
8178 isl_union_pw_aff_free(upa
);
8179 isl_union_pw_multi_aff_free(upma
);
8184 #define BASE union_pw_aff
8186 #define DOMBASE union_set
8188 #define NO_MOVE_DIMS
8195 #include <isl_multi_explicit_domain.c>
8196 #include <isl_multi_union_pw_aff_explicit_domain.c>
8197 #include <isl_multi_templ.c>
8198 #include <isl_multi_apply_set.c>
8199 #include <isl_multi_apply_union_set.c>
8200 #include <isl_multi_coalesce.c>
8201 #include <isl_multi_floor.c>
8202 #include <isl_multi_gist.c>
8203 #include <isl_multi_align_set.c>
8204 #include <isl_multi_align_union_set.c>
8205 #include <isl_multi_intersect.c>
8207 /* Does "mupa" have a non-trivial explicit domain?
8209 * The explicit domain, if present, is trivial if it represents
8210 * an (obviously) universe parameter set.
8212 isl_bool
isl_multi_union_pw_aff_has_non_trivial_domain(
8213 __isl_keep isl_multi_union_pw_aff
*mupa
)
8215 isl_bool is_params
, trivial
;
8219 return isl_bool_error
;
8220 if (!isl_multi_union_pw_aff_has_explicit_domain(mupa
))
8221 return isl_bool_false
;
8222 is_params
= isl_union_set_is_params(mupa
->u
.dom
);
8223 if (is_params
< 0 || !is_params
)
8224 return isl_bool_not(is_params
);
8225 set
= isl_set_from_union_set(isl_union_set_copy(mupa
->u
.dom
));
8226 trivial
= isl_set_plain_is_universe(set
);
8228 return isl_bool_not(trivial
);
8231 /* Construct a multiple union piecewise affine expression
8232 * in the given space with value zero in each of the output dimensions.
8234 * Since there is no canonical zero value for
8235 * a union piecewise affine expression, we can only construct
8236 * a zero-dimensional "zero" value.
8238 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_zero(
8239 __isl_take isl_space
*space
)
8246 params
= isl_space_is_params(space
);
8250 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
8251 "expecting proper set space", goto error
);
8252 if (!isl_space_is_set(space
))
8253 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
8254 "expecting set space", goto error
);
8255 if (isl_space_dim(space
, isl_dim_out
) != 0)
8256 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
8257 "expecting 0D space", goto error
);
8259 return isl_multi_union_pw_aff_alloc(space
);
8261 isl_space_free(space
);
8265 /* Compute the sum of "mupa1" and "mupa2" on the union of their domains,
8266 * with the actual sum on the shared domain and
8267 * the defined expression on the symmetric difference of the domains.
8269 * We simply iterate over the elements in both arguments and
8270 * call isl_union_pw_aff_union_add on each of them, if there is
8271 * at least one element.
8273 * Otherwise, the two expressions have an explicit domain and
8274 * the union of these explicit domains is computed.
8275 * This assumes that the explicit domains are either both in terms
8276 * of specific domains elements or both in terms of parameters.
8277 * However, if one of the expressions does not have any constraints
8278 * on its explicit domain, then this is allowed as well and the result
8279 * is the expression with no constraints on its explicit domain.
8281 static __isl_give isl_multi_union_pw_aff
*
8282 isl_multi_union_pw_aff_union_add_aligned(
8283 __isl_take isl_multi_union_pw_aff
*mupa1
,
8284 __isl_take isl_multi_union_pw_aff
*mupa2
)
8286 isl_bool has_domain
, is_params1
, is_params2
;
8288 if (isl_multi_union_pw_aff_check_equal_space(mupa1
, mupa2
) < 0)
8291 return isl_multi_union_pw_aff_bin_op(mupa1
, mupa2
,
8292 &isl_union_pw_aff_union_add
);
8293 if (isl_multi_union_pw_aff_check_has_explicit_domain(mupa1
) < 0 ||
8294 isl_multi_union_pw_aff_check_has_explicit_domain(mupa2
) < 0)
8297 has_domain
= isl_multi_union_pw_aff_has_non_trivial_domain(mupa1
);
8301 isl_multi_union_pw_aff_free(mupa2
);
8304 has_domain
= isl_multi_union_pw_aff_has_non_trivial_domain(mupa2
);
8308 isl_multi_union_pw_aff_free(mupa1
);
8312 is_params1
= isl_union_set_is_params(mupa1
->u
.dom
);
8313 is_params2
= isl_union_set_is_params(mupa2
->u
.dom
);
8314 if (is_params1
< 0 || is_params2
< 0)
8316 if (is_params1
!= is_params2
)
8317 isl_die(isl_multi_union_pw_aff_get_ctx(mupa1
),
8319 "cannot compute union of concrete domain and "
8320 "parameter constraints", goto error
);
8321 mupa1
= isl_multi_union_pw_aff_cow(mupa1
);
8324 mupa1
->u
.dom
= isl_union_set_union(mupa1
->u
.dom
,
8325 isl_union_set_copy(mupa2
->u
.dom
));
8328 isl_multi_union_pw_aff_free(mupa2
);
8331 isl_multi_union_pw_aff_free(mupa1
);
8332 isl_multi_union_pw_aff_free(mupa2
);
8336 /* Compute the sum of "mupa1" and "mupa2" on the union of their domains,
8337 * with the actual sum on the shared domain and
8338 * the defined expression on the symmetric difference of the domains.
8340 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_union_add(
8341 __isl_take isl_multi_union_pw_aff
*mupa1
,
8342 __isl_take isl_multi_union_pw_aff
*mupa2
)
8344 return isl_multi_union_pw_aff_align_params_multi_multi_and(mupa1
, mupa2
,
8345 &isl_multi_union_pw_aff_union_add_aligned
);
8348 /* Construct and return a multi union piecewise affine expression
8349 * that is equal to the given multi affine expression.
8351 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_from_multi_aff(
8352 __isl_take isl_multi_aff
*ma
)
8354 isl_multi_pw_aff
*mpa
;
8356 mpa
= isl_multi_pw_aff_from_multi_aff(ma
);
8357 return isl_multi_union_pw_aff_from_multi_pw_aff(mpa
);
8360 /* Construct and return a multi union piecewise affine expression
8361 * that is equal to the given multi piecewise affine expression.
8363 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_from_multi_pw_aff(
8364 __isl_take isl_multi_pw_aff
*mpa
)
8368 isl_multi_union_pw_aff
*mupa
;
8373 space
= isl_multi_pw_aff_get_space(mpa
);
8374 space
= isl_space_range(space
);
8375 mupa
= isl_multi_union_pw_aff_alloc(space
);
8377 n
= isl_multi_pw_aff_dim(mpa
, isl_dim_out
);
8378 for (i
= 0; i
< n
; ++i
) {
8380 isl_union_pw_aff
*upa
;
8382 pa
= isl_multi_pw_aff_get_pw_aff(mpa
, i
);
8383 upa
= isl_union_pw_aff_from_pw_aff(pa
);
8384 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
8387 isl_multi_pw_aff_free(mpa
);
8392 /* Extract the range space of "pma" and assign it to *space.
8393 * If *space has already been set (through a previous call to this function),
8394 * then check that the range space is the same.
8396 static isl_stat
extract_space(__isl_take isl_pw_multi_aff
*pma
, void *user
)
8398 isl_space
**space
= user
;
8399 isl_space
*pma_space
;
8402 pma_space
= isl_space_range(isl_pw_multi_aff_get_space(pma
));
8403 isl_pw_multi_aff_free(pma
);
8406 return isl_stat_error
;
8412 equal
= isl_space_is_equal(pma_space
, *space
);
8413 isl_space_free(pma_space
);
8416 return isl_stat_error
;
8418 isl_die(isl_space_get_ctx(*space
), isl_error_invalid
,
8419 "range spaces not the same", return isl_stat_error
);
8423 /* Construct and return a multi union piecewise affine expression
8424 * that is equal to the given union piecewise multi affine expression.
8426 * In order to be able to perform the conversion, the input
8427 * needs to be non-empty and may only involve a single range space.
8429 * If the resulting multi union piecewise affine expression has
8430 * an explicit domain, then assign it the domain of the input.
8431 * In other cases, the domain is stored in the individual elements.
8433 __isl_give isl_multi_union_pw_aff
*
8434 isl_multi_union_pw_aff_from_union_pw_multi_aff(
8435 __isl_take isl_union_pw_multi_aff
*upma
)
8437 isl_space
*space
= NULL
;
8438 isl_multi_union_pw_aff
*mupa
;
8443 if (isl_union_pw_multi_aff_n_pw_multi_aff(upma
) == 0)
8444 isl_die(isl_union_pw_multi_aff_get_ctx(upma
), isl_error_invalid
,
8445 "cannot extract range space from empty input",
8447 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma
, &extract_space
,
8454 n
= isl_space_dim(space
, isl_dim_set
);
8455 mupa
= isl_multi_union_pw_aff_alloc(space
);
8457 for (i
= 0; i
< n
; ++i
) {
8458 isl_union_pw_aff
*upa
;
8460 upa
= isl_union_pw_multi_aff_get_union_pw_aff(upma
, i
);
8461 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
8463 if (isl_multi_union_pw_aff_has_explicit_domain(mupa
)) {
8465 isl_union_pw_multi_aff
*copy
;
8467 copy
= isl_union_pw_multi_aff_copy(upma
);
8468 dom
= isl_union_pw_multi_aff_domain(copy
);
8469 mupa
= isl_multi_union_pw_aff_intersect_domain(mupa
, dom
);
8472 isl_union_pw_multi_aff_free(upma
);
8475 isl_space_free(space
);
8476 isl_union_pw_multi_aff_free(upma
);
8480 /* Try and create an isl_multi_union_pw_aff that is equivalent
8481 * to the given isl_union_map.
8482 * The isl_union_map is required to be single-valued in each space.
8483 * Moreover, it cannot be empty and all range spaces need to be the same.
8484 * Otherwise, an error is produced.
8486 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_from_union_map(
8487 __isl_take isl_union_map
*umap
)
8489 isl_union_pw_multi_aff
*upma
;
8491 upma
= isl_union_pw_multi_aff_from_union_map(umap
);
8492 return isl_multi_union_pw_aff_from_union_pw_multi_aff(upma
);
8495 /* Return a multiple union piecewise affine expression
8496 * that is equal to "mv" on "domain", assuming "domain" and "mv"
8497 * have been aligned.
8499 * If the resulting multi union piecewise affine expression has
8500 * an explicit domain, then assign it the input domain.
8501 * In other cases, the domain is stored in the individual elements.
8503 static __isl_give isl_multi_union_pw_aff
*
8504 isl_multi_union_pw_aff_multi_val_on_domain_aligned(
8505 __isl_take isl_union_set
*domain
, __isl_take isl_multi_val
*mv
)
8509 isl_multi_union_pw_aff
*mupa
;
8514 n
= isl_multi_val_dim(mv
, isl_dim_set
);
8515 space
= isl_multi_val_get_space(mv
);
8516 mupa
= isl_multi_union_pw_aff_alloc(space
);
8517 for (i
= 0; i
< n
; ++i
) {
8519 isl_union_pw_aff
*upa
;
8521 v
= isl_multi_val_get_val(mv
, i
);
8522 upa
= isl_union_pw_aff_val_on_domain(isl_union_set_copy(domain
),
8524 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
8526 if (isl_multi_union_pw_aff_has_explicit_domain(mupa
))
8527 mupa
= isl_multi_union_pw_aff_intersect_domain(mupa
,
8528 isl_union_set_copy(domain
));
8530 isl_union_set_free(domain
);
8531 isl_multi_val_free(mv
);
8534 isl_union_set_free(domain
);
8535 isl_multi_val_free(mv
);
8539 /* Return a multiple union piecewise affine expression
8540 * that is equal to "mv" on "domain".
8542 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_multi_val_on_domain(
8543 __isl_take isl_union_set
*domain
, __isl_take isl_multi_val
*mv
)
8545 isl_bool equal_params
;
8549 equal_params
= isl_space_has_equal_params(domain
->dim
, mv
->space
);
8550 if (equal_params
< 0)
8553 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(
8555 domain
= isl_union_set_align_params(domain
,
8556 isl_multi_val_get_space(mv
));
8557 mv
= isl_multi_val_align_params(mv
, isl_union_set_get_space(domain
));
8558 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(domain
, mv
);
8560 isl_union_set_free(domain
);
8561 isl_multi_val_free(mv
);
8565 /* Return a multiple union piecewise affine expression
8566 * that is equal to "ma" on "domain".
8568 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_multi_aff_on_domain(
8569 __isl_take isl_union_set
*domain
, __isl_take isl_multi_aff
*ma
)
8571 isl_pw_multi_aff
*pma
;
8573 pma
= isl_pw_multi_aff_from_multi_aff(ma
);
8574 return isl_multi_union_pw_aff_pw_multi_aff_on_domain(domain
, pma
);
8577 /* Return a multiple union piecewise affine expression
8578 * that is equal to "pma" on "domain", assuming "domain" and "pma"
8579 * have been aligned.
8581 * If the resulting multi union piecewise affine expression has
8582 * an explicit domain, then assign it the input domain.
8583 * In other cases, the domain is stored in the individual elements.
8585 static __isl_give isl_multi_union_pw_aff
*
8586 isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(
8587 __isl_take isl_union_set
*domain
, __isl_take isl_pw_multi_aff
*pma
)
8591 isl_multi_union_pw_aff
*mupa
;
8593 if (!domain
|| !pma
)
8596 n
= isl_pw_multi_aff_dim(pma
, isl_dim_set
);
8597 space
= isl_pw_multi_aff_get_space(pma
);
8598 mupa
= isl_multi_union_pw_aff_alloc(space
);
8599 for (i
= 0; i
< n
; ++i
) {
8601 isl_union_pw_aff
*upa
;
8603 pa
= isl_pw_multi_aff_get_pw_aff(pma
, i
);
8604 upa
= isl_union_pw_aff_pw_aff_on_domain(
8605 isl_union_set_copy(domain
), pa
);
8606 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
8608 if (isl_multi_union_pw_aff_has_explicit_domain(mupa
))
8609 mupa
= isl_multi_union_pw_aff_intersect_domain(mupa
,
8610 isl_union_set_copy(domain
));
8612 isl_union_set_free(domain
);
8613 isl_pw_multi_aff_free(pma
);
8616 isl_union_set_free(domain
);
8617 isl_pw_multi_aff_free(pma
);
8621 /* Return a multiple union piecewise affine expression
8622 * that is equal to "pma" on "domain".
8624 __isl_give isl_multi_union_pw_aff
*
8625 isl_multi_union_pw_aff_pw_multi_aff_on_domain(__isl_take isl_union_set
*domain
,
8626 __isl_take isl_pw_multi_aff
*pma
)
8628 isl_bool equal_params
;
8631 space
= isl_pw_multi_aff_peek_space(pma
);
8632 equal_params
= isl_union_set_space_has_equal_params(domain
, space
);
8633 if (equal_params
< 0)
8636 return isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(
8638 domain
= isl_union_set_align_params(domain
,
8639 isl_pw_multi_aff_get_space(pma
));
8640 pma
= isl_pw_multi_aff_align_params(pma
,
8641 isl_union_set_get_space(domain
));
8642 return isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(domain
,
8645 isl_union_set_free(domain
);
8646 isl_pw_multi_aff_free(pma
);
8650 /* Return a union set containing those elements in the domains
8651 * of the elements of "mupa" where they are all zero.
8653 * If there are no elements, then simply return the entire domain.
8655 __isl_give isl_union_set
*isl_multi_union_pw_aff_zero_union_set(
8656 __isl_take isl_multi_union_pw_aff
*mupa
)
8659 isl_union_pw_aff
*upa
;
8660 isl_union_set
*zero
;
8665 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8667 return isl_multi_union_pw_aff_domain(mupa
);
8669 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, 0);
8670 zero
= isl_union_pw_aff_zero_union_set(upa
);
8672 for (i
= 1; i
< n
; ++i
) {
8673 isl_union_set
*zero_i
;
8675 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
8676 zero_i
= isl_union_pw_aff_zero_union_set(upa
);
8678 zero
= isl_union_set_intersect(zero
, zero_i
);
8681 isl_multi_union_pw_aff_free(mupa
);
8685 /* Construct a union map mapping the shared domain
8686 * of the union piecewise affine expressions to the range of "mupa"
8687 * in the special case of a 0D multi union piecewise affine expression.
8689 * Construct a map between the explicit domain of "mupa" and
8691 * Note that this assumes that the domain consists of explicit elements.
8693 static __isl_give isl_union_map
*isl_union_map_from_multi_union_pw_aff_0D(
8694 __isl_take isl_multi_union_pw_aff
*mupa
)
8698 isl_union_set
*dom
, *ran
;
8700 space
= isl_multi_union_pw_aff_get_space(mupa
);
8701 dom
= isl_multi_union_pw_aff_domain(mupa
);
8702 ran
= isl_union_set_from_set(isl_set_universe(space
));
8704 is_params
= isl_union_set_is_params(dom
);
8706 dom
= isl_union_set_free(dom
);
8708 isl_die(isl_union_set_get_ctx(dom
), isl_error_invalid
,
8709 "cannot create union map from expression without "
8710 "explicit domain elements",
8711 dom
= isl_union_set_free(dom
));
8713 return isl_union_map_from_domain_and_range(dom
, ran
);
8716 /* Construct a union map mapping the shared domain
8717 * of the union piecewise affine expressions to the range of "mupa"
8718 * with each dimension in the range equated to the
8719 * corresponding union piecewise affine expression.
8721 * If the input is zero-dimensional, then construct a mapping
8722 * from its explicit domain.
8724 __isl_give isl_union_map
*isl_union_map_from_multi_union_pw_aff(
8725 __isl_take isl_multi_union_pw_aff
*mupa
)
8729 isl_union_map
*umap
;
8730 isl_union_pw_aff
*upa
;
8735 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8737 return isl_union_map_from_multi_union_pw_aff_0D(mupa
);
8739 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, 0);
8740 umap
= isl_union_map_from_union_pw_aff(upa
);
8742 for (i
= 1; i
< n
; ++i
) {
8743 isl_union_map
*umap_i
;
8745 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
8746 umap_i
= isl_union_map_from_union_pw_aff(upa
);
8747 umap
= isl_union_map_flat_range_product(umap
, umap_i
);
8750 space
= isl_multi_union_pw_aff_get_space(mupa
);
8751 umap
= isl_union_map_reset_range_space(umap
, space
);
8753 isl_multi_union_pw_aff_free(mupa
);
8757 /* Internal data structure for isl_union_pw_multi_aff_reset_range_space.
8758 * "range" is the space from which to set the range space.
8759 * "res" collects the results.
8761 struct isl_union_pw_multi_aff_reset_range_space_data
{
8763 isl_union_pw_multi_aff
*res
;
8766 /* Replace the range space of "pma" by the range space of data->range and
8767 * add the result to data->res.
8769 static isl_stat
reset_range_space(__isl_take isl_pw_multi_aff
*pma
, void *user
)
8771 struct isl_union_pw_multi_aff_reset_range_space_data
*data
= user
;
8774 space
= isl_pw_multi_aff_get_space(pma
);
8775 space
= isl_space_domain(space
);
8776 space
= isl_space_extend_domain_with_range(space
,
8777 isl_space_copy(data
->range
));
8778 pma
= isl_pw_multi_aff_reset_space(pma
, space
);
8779 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma
);
8781 return data
->res
? isl_stat_ok
: isl_stat_error
;
8784 /* Replace the range space of all the piecewise affine expressions in "upma" by
8785 * the range space of "space".
8787 * This assumes that all these expressions have the same output dimension.
8789 * Since the spaces of the expressions change, so do their hash values.
8790 * We therefore need to create a new isl_union_pw_multi_aff.
8791 * Note that the hash value is currently computed based on the entire
8792 * space even though there can only be a single expression with a given
8795 static __isl_give isl_union_pw_multi_aff
*
8796 isl_union_pw_multi_aff_reset_range_space(
8797 __isl_take isl_union_pw_multi_aff
*upma
, __isl_take isl_space
*space
)
8799 struct isl_union_pw_multi_aff_reset_range_space_data data
= { space
};
8800 isl_space
*space_upma
;
8802 space_upma
= isl_union_pw_multi_aff_get_space(upma
);
8803 data
.res
= isl_union_pw_multi_aff_empty(space_upma
);
8804 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma
,
8805 &reset_range_space
, &data
) < 0)
8806 data
.res
= isl_union_pw_multi_aff_free(data
.res
);
8808 isl_space_free(space
);
8809 isl_union_pw_multi_aff_free(upma
);
8813 /* Construct and return a union piecewise multi affine expression
8814 * that is equal to the given multi union piecewise affine expression,
8815 * in the special case of a 0D multi union piecewise affine expression.
8817 * Construct a union piecewise multi affine expression
8818 * on top of the explicit domain of the input.
8820 __isl_give isl_union_pw_multi_aff
*
8821 isl_union_pw_multi_aff_from_multi_union_pw_aff_0D(
8822 __isl_take isl_multi_union_pw_aff
*mupa
)
8826 isl_union_set
*domain
;
8828 space
= isl_multi_union_pw_aff_get_space(mupa
);
8829 mv
= isl_multi_val_zero(space
);
8830 domain
= isl_multi_union_pw_aff_domain(mupa
);
8831 return isl_union_pw_multi_aff_multi_val_on_domain(domain
, mv
);
8834 /* Construct and return a union piecewise multi affine expression
8835 * that is equal to the given multi union piecewise affine expression.
8837 * If the input is zero-dimensional, then
8838 * construct a union piecewise multi affine expression
8839 * on top of the explicit domain of the input.
8841 __isl_give isl_union_pw_multi_aff
*
8842 isl_union_pw_multi_aff_from_multi_union_pw_aff(
8843 __isl_take isl_multi_union_pw_aff
*mupa
)
8847 isl_union_pw_multi_aff
*upma
;
8848 isl_union_pw_aff
*upa
;
8853 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8855 return isl_union_pw_multi_aff_from_multi_union_pw_aff_0D(mupa
);
8857 space
= isl_multi_union_pw_aff_get_space(mupa
);
8858 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, 0);
8859 upma
= isl_union_pw_multi_aff_from_union_pw_aff(upa
);
8861 for (i
= 1; i
< n
; ++i
) {
8862 isl_union_pw_multi_aff
*upma_i
;
8864 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
8865 upma_i
= isl_union_pw_multi_aff_from_union_pw_aff(upa
);
8866 upma
= isl_union_pw_multi_aff_flat_range_product(upma
, upma_i
);
8869 upma
= isl_union_pw_multi_aff_reset_range_space(upma
, space
);
8871 isl_multi_union_pw_aff_free(mupa
);
8875 /* Intersect the range of "mupa" with "range",
8876 * in the special case where "mupa" is 0D.
8878 * Intersect the domain of "mupa" with the constraints on the parameters
8881 static __isl_give isl_multi_union_pw_aff
*mupa_intersect_range_0D(
8882 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_set
*range
)
8884 range
= isl_set_params(range
);
8885 mupa
= isl_multi_union_pw_aff_intersect_params(mupa
, range
);
8889 /* Intersect the range of "mupa" with "range".
8890 * That is, keep only those domain elements that have a function value
8893 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_intersect_range(
8894 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_set
*range
)
8896 isl_union_pw_multi_aff
*upma
;
8897 isl_union_set
*domain
;
8902 if (!mupa
|| !range
)
8905 space
= isl_set_get_space(range
);
8906 match
= isl_space_tuple_is_equal(mupa
->space
, isl_dim_set
,
8907 space
, isl_dim_set
);
8908 isl_space_free(space
);
8912 isl_die(isl_multi_union_pw_aff_get_ctx(mupa
), isl_error_invalid
,
8913 "space don't match", goto error
);
8914 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8916 return mupa_intersect_range_0D(mupa
, range
);
8918 upma
= isl_union_pw_multi_aff_from_multi_union_pw_aff(
8919 isl_multi_union_pw_aff_copy(mupa
));
8920 domain
= isl_union_set_from_set(range
);
8921 domain
= isl_union_set_preimage_union_pw_multi_aff(domain
, upma
);
8922 mupa
= isl_multi_union_pw_aff_intersect_domain(mupa
, domain
);
8926 isl_multi_union_pw_aff_free(mupa
);
8927 isl_set_free(range
);
8931 /* Return the shared domain of the elements of "mupa",
8932 * in the special case where "mupa" is zero-dimensional.
8934 * Return the explicit domain of "mupa".
8935 * Note that this domain may be a parameter set, either
8936 * because "mupa" is meant to live in a set space or
8937 * because no explicit domain has been set.
8939 __isl_give isl_union_set
*isl_multi_union_pw_aff_domain_0D(
8940 __isl_take isl_multi_union_pw_aff
*mupa
)
8944 dom
= isl_multi_union_pw_aff_get_explicit_domain(mupa
);
8945 isl_multi_union_pw_aff_free(mupa
);
8950 /* Return the shared domain of the elements of "mupa".
8952 * If "mupa" is zero-dimensional, then return its explicit domain.
8954 __isl_give isl_union_set
*isl_multi_union_pw_aff_domain(
8955 __isl_take isl_multi_union_pw_aff
*mupa
)
8958 isl_union_pw_aff
*upa
;
8964 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8966 return isl_multi_union_pw_aff_domain_0D(mupa
);
8968 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, 0);
8969 dom
= isl_union_pw_aff_domain(upa
);
8970 for (i
= 1; i
< n
; ++i
) {
8971 isl_union_set
*dom_i
;
8973 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
8974 dom_i
= isl_union_pw_aff_domain(upa
);
8975 dom
= isl_union_set_intersect(dom
, dom_i
);
8978 isl_multi_union_pw_aff_free(mupa
);
8982 /* Apply "aff" to "mupa". The space of "mupa" is equal to the domain of "aff".
8983 * In particular, the spaces have been aligned.
8984 * The result is defined over the shared domain of the elements of "mupa"
8986 * We first extract the parametric constant part of "aff" and
8987 * define that over the shared domain.
8988 * Then we iterate over all input dimensions of "aff" and add the corresponding
8989 * multiples of the elements of "mupa".
8990 * Finally, we consider the integer divisions, calling the function
8991 * recursively to obtain an isl_union_pw_aff corresponding to the
8992 * integer division argument.
8994 static __isl_give isl_union_pw_aff
*multi_union_pw_aff_apply_aff(
8995 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_aff
*aff
)
8998 isl_union_pw_aff
*upa
;
8999 isl_union_set
*uset
;
9003 n_in
= isl_aff_dim(aff
, isl_dim_in
);
9004 n_div
= isl_aff_dim(aff
, isl_dim_div
);
9006 uset
= isl_multi_union_pw_aff_domain(isl_multi_union_pw_aff_copy(mupa
));
9007 cst
= isl_aff_copy(aff
);
9008 cst
= isl_aff_drop_dims(cst
, isl_dim_div
, 0, n_div
);
9009 cst
= isl_aff_drop_dims(cst
, isl_dim_in
, 0, n_in
);
9010 cst
= isl_aff_project_domain_on_params(cst
);
9011 upa
= isl_union_pw_aff_aff_on_domain(uset
, cst
);
9013 for (i
= 0; i
< n_in
; ++i
) {
9014 isl_union_pw_aff
*upa_i
;
9016 if (!isl_aff_involves_dims(aff
, isl_dim_in
, i
, 1))
9018 v
= isl_aff_get_coefficient_val(aff
, isl_dim_in
, i
);
9019 upa_i
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
9020 upa_i
= isl_union_pw_aff_scale_val(upa_i
, v
);
9021 upa
= isl_union_pw_aff_add(upa
, upa_i
);
9024 for (i
= 0; i
< n_div
; ++i
) {
9026 isl_union_pw_aff
*upa_i
;
9028 if (!isl_aff_involves_dims(aff
, isl_dim_div
, i
, 1))
9030 div
= isl_aff_get_div(aff
, i
);
9031 upa_i
= multi_union_pw_aff_apply_aff(
9032 isl_multi_union_pw_aff_copy(mupa
), div
);
9033 upa_i
= isl_union_pw_aff_floor(upa_i
);
9034 v
= isl_aff_get_coefficient_val(aff
, isl_dim_div
, i
);
9035 upa_i
= isl_union_pw_aff_scale_val(upa_i
, v
);
9036 upa
= isl_union_pw_aff_add(upa
, upa_i
);
9039 isl_multi_union_pw_aff_free(mupa
);
9045 /* Apply "aff" to "mupa". The space of "mupa" needs to be compatible
9046 * with the domain of "aff".
9047 * Furthermore, the dimension of this space needs to be greater than zero.
9048 * The result is defined over the shared domain of the elements of "mupa"
9050 * We perform these checks and then hand over control to
9051 * multi_union_pw_aff_apply_aff.
9053 __isl_give isl_union_pw_aff
*isl_multi_union_pw_aff_apply_aff(
9054 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_aff
*aff
)
9056 isl_space
*space1
, *space2
;
9059 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
9060 isl_aff_get_space(aff
));
9061 aff
= isl_aff_align_params(aff
, isl_multi_union_pw_aff_get_space(mupa
));
9065 space1
= isl_multi_union_pw_aff_get_space(mupa
);
9066 space2
= isl_aff_get_domain_space(aff
);
9067 equal
= isl_space_is_equal(space1
, space2
);
9068 isl_space_free(space1
);
9069 isl_space_free(space2
);
9073 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
9074 "spaces don't match", goto error
);
9075 if (isl_aff_dim(aff
, isl_dim_in
) == 0)
9076 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
9077 "cannot determine domains", goto error
);
9079 return multi_union_pw_aff_apply_aff(mupa
, aff
);
9081 isl_multi_union_pw_aff_free(mupa
);
9086 /* Apply "ma" to "mupa", in the special case where "mupa" is 0D.
9087 * The space of "mupa" is known to be compatible with the domain of "ma".
9089 * Construct an isl_multi_union_pw_aff that is equal to "ma"
9090 * on the domain of "mupa".
9092 static __isl_give isl_multi_union_pw_aff
*mupa_apply_multi_aff_0D(
9093 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_multi_aff
*ma
)
9097 dom
= isl_multi_union_pw_aff_domain(mupa
);
9098 ma
= isl_multi_aff_project_domain_on_params(ma
);
9100 return isl_multi_union_pw_aff_multi_aff_on_domain(dom
, ma
);
9103 /* Apply "ma" to "mupa". The space of "mupa" needs to be compatible
9104 * with the domain of "ma".
9105 * The result is defined over the shared domain of the elements of "mupa"
9107 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_apply_multi_aff(
9108 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_multi_aff
*ma
)
9110 isl_space
*space1
, *space2
;
9111 isl_multi_union_pw_aff
*res
;
9115 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
9116 isl_multi_aff_get_space(ma
));
9117 ma
= isl_multi_aff_align_params(ma
,
9118 isl_multi_union_pw_aff_get_space(mupa
));
9122 space1
= isl_multi_union_pw_aff_get_space(mupa
);
9123 space2
= isl_multi_aff_get_domain_space(ma
);
9124 equal
= isl_space_is_equal(space1
, space2
);
9125 isl_space_free(space1
);
9126 isl_space_free(space2
);
9130 isl_die(isl_multi_aff_get_ctx(ma
), isl_error_invalid
,
9131 "spaces don't match", goto error
);
9132 n_out
= isl_multi_aff_dim(ma
, isl_dim_out
);
9133 if (isl_multi_aff_dim(ma
, isl_dim_in
) == 0)
9134 return mupa_apply_multi_aff_0D(mupa
, ma
);
9136 space1
= isl_space_range(isl_multi_aff_get_space(ma
));
9137 res
= isl_multi_union_pw_aff_alloc(space1
);
9139 for (i
= 0; i
< n_out
; ++i
) {
9141 isl_union_pw_aff
*upa
;
9143 aff
= isl_multi_aff_get_aff(ma
, i
);
9144 upa
= multi_union_pw_aff_apply_aff(
9145 isl_multi_union_pw_aff_copy(mupa
), aff
);
9146 res
= isl_multi_union_pw_aff_set_union_pw_aff(res
, i
, upa
);
9149 isl_multi_aff_free(ma
);
9150 isl_multi_union_pw_aff_free(mupa
);
9153 isl_multi_union_pw_aff_free(mupa
);
9154 isl_multi_aff_free(ma
);
9158 /* Apply "pa" to "mupa", in the special case where "mupa" is 0D.
9159 * The space of "mupa" is known to be compatible with the domain of "pa".
9161 * Construct an isl_multi_union_pw_aff that is equal to "pa"
9162 * on the domain of "mupa".
9164 static __isl_give isl_union_pw_aff
*isl_multi_union_pw_aff_apply_pw_aff_0D(
9165 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_pw_aff
*pa
)
9169 dom
= isl_multi_union_pw_aff_domain(mupa
);
9170 pa
= isl_pw_aff_project_domain_on_params(pa
);
9172 return isl_union_pw_aff_pw_aff_on_domain(dom
, pa
);
9175 /* Apply "pa" to "mupa". The space of "mupa" needs to be compatible
9176 * with the domain of "pa".
9177 * Furthermore, the dimension of this space needs to be greater than zero.
9178 * The result is defined over the shared domain of the elements of "mupa"
9180 __isl_give isl_union_pw_aff
*isl_multi_union_pw_aff_apply_pw_aff(
9181 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_pw_aff
*pa
)
9185 isl_space
*space
, *space2
;
9186 isl_union_pw_aff
*upa
;
9188 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
9189 isl_pw_aff_get_space(pa
));
9190 pa
= isl_pw_aff_align_params(pa
,
9191 isl_multi_union_pw_aff_get_space(mupa
));
9195 space
= isl_multi_union_pw_aff_get_space(mupa
);
9196 space2
= isl_pw_aff_get_domain_space(pa
);
9197 equal
= isl_space_is_equal(space
, space2
);
9198 isl_space_free(space
);
9199 isl_space_free(space2
);
9203 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
9204 "spaces don't match", goto error
);
9205 if (isl_pw_aff_dim(pa
, isl_dim_in
) == 0)
9206 return isl_multi_union_pw_aff_apply_pw_aff_0D(mupa
, pa
);
9208 space
= isl_space_params(isl_multi_union_pw_aff_get_space(mupa
));
9209 upa
= isl_union_pw_aff_empty(space
);
9211 for (i
= 0; i
< pa
->n
; ++i
) {
9214 isl_multi_union_pw_aff
*mupa_i
;
9215 isl_union_pw_aff
*upa_i
;
9217 mupa_i
= isl_multi_union_pw_aff_copy(mupa
);
9218 domain
= isl_set_copy(pa
->p
[i
].set
);
9219 mupa_i
= isl_multi_union_pw_aff_intersect_range(mupa_i
, domain
);
9220 aff
= isl_aff_copy(pa
->p
[i
].aff
);
9221 upa_i
= multi_union_pw_aff_apply_aff(mupa_i
, aff
);
9222 upa
= isl_union_pw_aff_union_add(upa
, upa_i
);
9225 isl_multi_union_pw_aff_free(mupa
);
9226 isl_pw_aff_free(pa
);
9229 isl_multi_union_pw_aff_free(mupa
);
9230 isl_pw_aff_free(pa
);
9234 /* Apply "pma" to "mupa", in the special case where "mupa" is 0D.
9235 * The space of "mupa" is known to be compatible with the domain of "pma".
9237 * Construct an isl_multi_union_pw_aff that is equal to "pma"
9238 * on the domain of "mupa".
9240 static __isl_give isl_multi_union_pw_aff
*mupa_apply_pw_multi_aff_0D(
9241 __isl_take isl_multi_union_pw_aff
*mupa
,
9242 __isl_take isl_pw_multi_aff
*pma
)
9246 dom
= isl_multi_union_pw_aff_domain(mupa
);
9247 pma
= isl_pw_multi_aff_project_domain_on_params(pma
);
9249 return isl_multi_union_pw_aff_pw_multi_aff_on_domain(dom
, pma
);
9252 /* Apply "pma" to "mupa". The space of "mupa" needs to be compatible
9253 * with the domain of "pma".
9254 * The result is defined over the shared domain of the elements of "mupa"
9256 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_apply_pw_multi_aff(
9257 __isl_take isl_multi_union_pw_aff
*mupa
,
9258 __isl_take isl_pw_multi_aff
*pma
)
9260 isl_space
*space1
, *space2
;
9261 isl_multi_union_pw_aff
*res
;
9265 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
9266 isl_pw_multi_aff_get_space(pma
));
9267 pma
= isl_pw_multi_aff_align_params(pma
,
9268 isl_multi_union_pw_aff_get_space(mupa
));
9272 space1
= isl_multi_union_pw_aff_get_space(mupa
);
9273 space2
= isl_pw_multi_aff_get_domain_space(pma
);
9274 equal
= isl_space_is_equal(space1
, space2
);
9275 isl_space_free(space1
);
9276 isl_space_free(space2
);
9280 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
9281 "spaces don't match", goto error
);
9282 n_out
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
9283 if (isl_pw_multi_aff_dim(pma
, isl_dim_in
) == 0)
9284 return mupa_apply_pw_multi_aff_0D(mupa
, pma
);
9286 space1
= isl_space_range(isl_pw_multi_aff_get_space(pma
));
9287 res
= isl_multi_union_pw_aff_alloc(space1
);
9289 for (i
= 0; i
< n_out
; ++i
) {
9291 isl_union_pw_aff
*upa
;
9293 pa
= isl_pw_multi_aff_get_pw_aff(pma
, i
);
9294 upa
= isl_multi_union_pw_aff_apply_pw_aff(
9295 isl_multi_union_pw_aff_copy(mupa
), pa
);
9296 res
= isl_multi_union_pw_aff_set_union_pw_aff(res
, i
, upa
);
9299 isl_pw_multi_aff_free(pma
);
9300 isl_multi_union_pw_aff_free(mupa
);
9303 isl_multi_union_pw_aff_free(mupa
);
9304 isl_pw_multi_aff_free(pma
);
9308 /* Replace the explicit domain of "mupa" by its preimage under "upma".
9309 * If the explicit domain only keeps track of constraints on the parameters,
9310 * then only update those constraints.
9312 static __isl_give isl_multi_union_pw_aff
*preimage_explicit_domain(
9313 __isl_take isl_multi_union_pw_aff
*mupa
,
9314 __isl_keep isl_union_pw_multi_aff
*upma
)
9318 if (isl_multi_union_pw_aff_check_has_explicit_domain(mupa
) < 0)
9319 return isl_multi_union_pw_aff_free(mupa
);
9321 mupa
= isl_multi_union_pw_aff_cow(mupa
);
9325 is_params
= isl_union_set_is_params(mupa
->u
.dom
);
9327 return isl_multi_union_pw_aff_free(mupa
);
9329 upma
= isl_union_pw_multi_aff_copy(upma
);
9331 mupa
->u
.dom
= isl_union_set_intersect_params(mupa
->u
.dom
,
9332 isl_union_set_params(isl_union_pw_multi_aff_domain(upma
)));
9334 mupa
->u
.dom
= isl_union_set_preimage_union_pw_multi_aff(
9337 return isl_multi_union_pw_aff_free(mupa
);
9341 /* Compute the pullback of "mupa" by the function represented by "upma".
9342 * In other words, plug in "upma" in "mupa". The result contains
9343 * expressions defined over the domain space of "upma".
9345 * Run over all elements of "mupa" and plug in "upma" in each of them.
9347 * If "mupa" has an explicit domain, then it is this domain
9348 * that needs to undergo a pullback instead, i.e., a preimage.
9350 __isl_give isl_multi_union_pw_aff
*
9351 isl_multi_union_pw_aff_pullback_union_pw_multi_aff(
9352 __isl_take isl_multi_union_pw_aff
*mupa
,
9353 __isl_take isl_union_pw_multi_aff
*upma
)
9357 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
9358 isl_union_pw_multi_aff_get_space(upma
));
9359 upma
= isl_union_pw_multi_aff_align_params(upma
,
9360 isl_multi_union_pw_aff_get_space(mupa
));
9361 mupa
= isl_multi_union_pw_aff_cow(mupa
);
9365 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
9366 for (i
= 0; i
< n
; ++i
) {
9367 isl_union_pw_aff
*upa
;
9369 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
9370 upa
= isl_union_pw_aff_pullback_union_pw_multi_aff(upa
,
9371 isl_union_pw_multi_aff_copy(upma
));
9372 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
9375 if (isl_multi_union_pw_aff_has_explicit_domain(mupa
))
9376 mupa
= preimage_explicit_domain(mupa
, upma
);
9378 isl_union_pw_multi_aff_free(upma
);
9381 isl_multi_union_pw_aff_free(mupa
);
9382 isl_union_pw_multi_aff_free(upma
);
9386 /* Extract the sequence of elements in "mupa" with domain space "space"
9387 * (ignoring parameters).
9389 * For the elements of "mupa" that are not defined on the specified space,
9390 * the corresponding element in the result is empty.
9392 __isl_give isl_multi_pw_aff
*isl_multi_union_pw_aff_extract_multi_pw_aff(
9393 __isl_keep isl_multi_union_pw_aff
*mupa
, __isl_take isl_space
*space
)
9396 isl_space
*space_mpa
;
9397 isl_multi_pw_aff
*mpa
;
9399 if (!mupa
|| !space
)
9402 space_mpa
= isl_multi_union_pw_aff_get_space(mupa
);
9403 space
= isl_space_replace_params(space
, space_mpa
);
9404 space_mpa
= isl_space_map_from_domain_and_range(isl_space_copy(space
),
9406 mpa
= isl_multi_pw_aff_alloc(space_mpa
);
9408 space
= isl_space_from_domain(space
);
9409 space
= isl_space_add_dims(space
, isl_dim_out
, 1);
9410 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
9411 for (i
= 0; i
< n
; ++i
) {
9412 isl_union_pw_aff
*upa
;
9415 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
9416 pa
= isl_union_pw_aff_extract_pw_aff(upa
,
9417 isl_space_copy(space
));
9418 mpa
= isl_multi_pw_aff_set_pw_aff(mpa
, i
, pa
);
9419 isl_union_pw_aff_free(upa
);
9422 isl_space_free(space
);
9425 isl_space_free(space
);
9429 /* Evaluate the affine function "aff" in the void point "pnt".
9430 * In particular, return the value NaN.
9432 static __isl_give isl_val
*eval_void(__isl_take isl_aff
*aff
,
9433 __isl_take isl_point
*pnt
)
9437 ctx
= isl_point_get_ctx(pnt
);
9439 isl_point_free(pnt
);
9440 return isl_val_nan(ctx
);
9443 /* Evaluate the affine expression "aff"
9444 * in the coordinates (with denominator) "pnt".
9446 static __isl_give isl_val
*eval(__isl_keep isl_vec
*aff
,
9447 __isl_keep isl_vec
*pnt
)
9456 ctx
= isl_vec_get_ctx(aff
);
9459 isl_seq_inner_product(aff
->el
+ 1, pnt
->el
, pnt
->size
, &n
);
9460 isl_int_mul(d
, aff
->el
[0], pnt
->el
[0]);
9461 v
= isl_val_rat_from_isl_int(ctx
, n
, d
);
9462 v
= isl_val_normalize(v
);
9469 /* Check that the domain space of "aff" is equal to "space".
9471 static isl_stat
isl_aff_check_has_domain_space(__isl_keep isl_aff
*aff
,
9472 __isl_keep isl_space
*space
)
9476 ok
= isl_space_is_equal(isl_aff_peek_domain_space(aff
), space
);
9478 return isl_stat_error
;
9480 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
9481 "incompatible spaces", return isl_stat_error
);
9485 /* Evaluate the affine function "aff" in "pnt".
9487 __isl_give isl_val
*isl_aff_eval(__isl_take isl_aff
*aff
,
9488 __isl_take isl_point
*pnt
)
9492 isl_local_space
*ls
;
9494 if (isl_aff_check_has_domain_space(aff
, isl_point_peek_space(pnt
)) < 0)
9496 is_void
= isl_point_is_void(pnt
);
9500 return eval_void(aff
, pnt
);
9502 ls
= isl_aff_get_domain_local_space(aff
);
9503 pnt
= isl_local_space_lift_point(ls
, pnt
);
9505 v
= eval(aff
->v
, isl_point_peek_vec(pnt
));
9508 isl_point_free(pnt
);
9513 isl_point_free(pnt
);