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>
19 #include <isl_map_private.h>
20 #include <isl_union_map_private.h>
21 #include <isl_aff_private.h>
22 #include <isl_space_private.h>
23 #include <isl_local_space_private.h>
24 #include <isl_vec_private.h>
25 #include <isl_mat_private.h>
26 #include <isl/constraint.h>
29 #include <isl_val_private.h>
30 #include <isl_config.h>
35 #include <isl_list_templ.c>
40 #include <isl_list_templ.c>
43 #define BASE union_pw_aff
45 #include <isl_list_templ.c>
48 #define BASE union_pw_multi_aff
50 #include <isl_list_templ.c>
52 __isl_give isl_aff
*isl_aff_alloc_vec(__isl_take isl_local_space
*ls
,
53 __isl_take isl_vec
*v
)
60 aff
= isl_calloc_type(v
->ctx
, struct isl_aff
);
70 isl_local_space_free(ls
);
75 __isl_give isl_aff
*isl_aff_alloc(__isl_take isl_local_space
*ls
)
84 ctx
= isl_local_space_get_ctx(ls
);
85 if (!isl_local_space_divs_known(ls
))
86 isl_die(ctx
, isl_error_invalid
, "local space has unknown divs",
88 if (!isl_local_space_is_set(ls
))
89 isl_die(ctx
, isl_error_invalid
,
90 "domain of affine expression should be a set",
93 total
= isl_local_space_dim(ls
, isl_dim_all
);
94 v
= isl_vec_alloc(ctx
, 1 + 1 + total
);
95 return isl_aff_alloc_vec(ls
, v
);
97 isl_local_space_free(ls
);
101 __isl_give isl_aff
*isl_aff_zero_on_domain(__isl_take isl_local_space
*ls
)
105 aff
= isl_aff_alloc(ls
);
109 isl_int_set_si(aff
->v
->el
[0], 1);
110 isl_seq_clr(aff
->v
->el
+ 1, aff
->v
->size
- 1);
115 /* Return a piecewise affine expression defined on the specified domain
116 * that is equal to zero.
118 __isl_give isl_pw_aff
*isl_pw_aff_zero_on_domain(__isl_take isl_local_space
*ls
)
120 return isl_pw_aff_from_aff(isl_aff_zero_on_domain(ls
));
123 /* Return an affine expression defined on the specified domain
124 * that represents NaN.
126 __isl_give isl_aff
*isl_aff_nan_on_domain(__isl_take isl_local_space
*ls
)
130 aff
= isl_aff_alloc(ls
);
134 isl_seq_clr(aff
->v
->el
, aff
->v
->size
);
139 /* Return a piecewise affine expression defined on the specified domain
140 * that represents NaN.
142 __isl_give isl_pw_aff
*isl_pw_aff_nan_on_domain(__isl_take isl_local_space
*ls
)
144 return isl_pw_aff_from_aff(isl_aff_nan_on_domain(ls
));
147 /* Return an affine expression that is equal to "val" on
148 * domain local space "ls".
150 __isl_give isl_aff
*isl_aff_val_on_domain(__isl_take isl_local_space
*ls
,
151 __isl_take isl_val
*val
)
157 if (!isl_val_is_rat(val
))
158 isl_die(isl_val_get_ctx(val
), isl_error_invalid
,
159 "expecting rational value", goto error
);
161 aff
= isl_aff_alloc(isl_local_space_copy(ls
));
165 isl_seq_clr(aff
->v
->el
+ 2, aff
->v
->size
- 2);
166 isl_int_set(aff
->v
->el
[1], val
->n
);
167 isl_int_set(aff
->v
->el
[0], val
->d
);
169 isl_local_space_free(ls
);
173 isl_local_space_free(ls
);
178 /* Return an affine expression that is equal to the specified dimension
181 __isl_give isl_aff
*isl_aff_var_on_domain(__isl_take isl_local_space
*ls
,
182 enum isl_dim_type type
, unsigned pos
)
190 space
= isl_local_space_get_space(ls
);
193 if (isl_space_is_map(space
))
194 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
195 "expecting (parameter) set space", goto error
);
196 if (pos
>= isl_local_space_dim(ls
, type
))
197 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
198 "position out of bounds", goto error
);
200 isl_space_free(space
);
201 aff
= isl_aff_alloc(ls
);
205 pos
+= isl_local_space_offset(aff
->ls
, type
);
207 isl_int_set_si(aff
->v
->el
[0], 1);
208 isl_seq_clr(aff
->v
->el
+ 1, aff
->v
->size
- 1);
209 isl_int_set_si(aff
->v
->el
[1 + pos
], 1);
213 isl_local_space_free(ls
);
214 isl_space_free(space
);
218 /* Return a piecewise affine expression that is equal to
219 * the specified dimension in "ls".
221 __isl_give isl_pw_aff
*isl_pw_aff_var_on_domain(__isl_take isl_local_space
*ls
,
222 enum isl_dim_type type
, unsigned pos
)
224 return isl_pw_aff_from_aff(isl_aff_var_on_domain(ls
, type
, pos
));
227 /* Return an affine expression that is equal to the parameter
228 * in the domain space "space" with identifier "id".
230 __isl_give isl_aff
*isl_aff_param_on_domain_space_id(
231 __isl_take isl_space
*space
, __isl_take isl_id
*id
)
238 pos
= isl_space_find_dim_by_id(space
, isl_dim_param
, id
);
240 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
241 "parameter not found in space", goto error
);
243 ls
= isl_local_space_from_space(space
);
244 return isl_aff_var_on_domain(ls
, isl_dim_param
, pos
);
246 isl_space_free(space
);
251 __isl_give isl_aff
*isl_aff_copy(__isl_keep isl_aff
*aff
)
260 __isl_give isl_aff
*isl_aff_dup(__isl_keep isl_aff
*aff
)
265 return isl_aff_alloc_vec(isl_local_space_copy(aff
->ls
),
266 isl_vec_copy(aff
->v
));
269 __isl_give isl_aff
*isl_aff_cow(__isl_take isl_aff
*aff
)
277 return isl_aff_dup(aff
);
280 __isl_null isl_aff
*isl_aff_free(__isl_take isl_aff
*aff
)
288 isl_local_space_free(aff
->ls
);
289 isl_vec_free(aff
->v
);
296 isl_ctx
*isl_aff_get_ctx(__isl_keep isl_aff
*aff
)
298 return aff
? isl_local_space_get_ctx(aff
->ls
) : NULL
;
301 /* Return a hash value that digests "aff".
303 uint32_t isl_aff_get_hash(__isl_keep isl_aff
*aff
)
305 uint32_t hash
, ls_hash
, v_hash
;
310 hash
= isl_hash_init();
311 ls_hash
= isl_local_space_get_hash(aff
->ls
);
312 isl_hash_hash(hash
, ls_hash
);
313 v_hash
= isl_vec_get_hash(aff
->v
);
314 isl_hash_hash(hash
, v_hash
);
319 /* Externally, an isl_aff has a map space, but internally, the
320 * ls field corresponds to the domain of that space.
322 int isl_aff_dim(__isl_keep isl_aff
*aff
, enum isl_dim_type type
)
326 if (type
== isl_dim_out
)
328 if (type
== isl_dim_in
)
330 return isl_local_space_dim(aff
->ls
, type
);
333 /* Return the position of the dimension of the given type and name
335 * Return -1 if no such dimension can be found.
337 int isl_aff_find_dim_by_name(__isl_keep isl_aff
*aff
, enum isl_dim_type type
,
342 if (type
== isl_dim_out
)
344 if (type
== isl_dim_in
)
346 return isl_local_space_find_dim_by_name(aff
->ls
, type
, name
);
349 __isl_give isl_space
*isl_aff_get_domain_space(__isl_keep isl_aff
*aff
)
351 return aff
? isl_local_space_get_space(aff
->ls
) : NULL
;
354 __isl_give isl_space
*isl_aff_get_space(__isl_keep isl_aff
*aff
)
359 space
= isl_local_space_get_space(aff
->ls
);
360 space
= isl_space_from_domain(space
);
361 space
= isl_space_add_dims(space
, isl_dim_out
, 1);
365 __isl_give isl_local_space
*isl_aff_get_domain_local_space(
366 __isl_keep isl_aff
*aff
)
368 return aff
? isl_local_space_copy(aff
->ls
) : NULL
;
371 __isl_give isl_local_space
*isl_aff_get_local_space(__isl_keep isl_aff
*aff
)
376 ls
= isl_local_space_copy(aff
->ls
);
377 ls
= isl_local_space_from_domain(ls
);
378 ls
= isl_local_space_add_dims(ls
, isl_dim_out
, 1);
382 /* Return the local space of the domain of "aff".
383 * This may be either a copy or the local space itself
384 * if there is only one reference to "aff".
385 * This allows the local space to be modified inplace
386 * if both the expression and its local space have only a single reference.
387 * The caller is not allowed to modify "aff" between this call and
388 * a subsequent call to isl_aff_restore_domain_local_space.
389 * The only exception is that isl_aff_free can be called instead.
391 __isl_give isl_local_space
*isl_aff_take_domain_local_space(
392 __isl_keep isl_aff
*aff
)
399 return isl_aff_get_domain_local_space(aff
);
405 /* Set the local space of the domain of "aff" to "ls",
406 * where the local space of "aff" may be missing
407 * due to a preceding call to isl_aff_take_domain_local_space.
408 * However, in this case, "aff" only has a single reference and
409 * then the call to isl_aff_cow has no effect.
411 __isl_give isl_aff
*isl_aff_restore_domain_local_space(
412 __isl_keep isl_aff
*aff
, __isl_take isl_local_space
*ls
)
418 isl_local_space_free(ls
);
422 aff
= isl_aff_cow(aff
);
425 isl_local_space_free(aff
->ls
);
431 isl_local_space_free(ls
);
435 /* Externally, an isl_aff has a map space, but internally, the
436 * ls field corresponds to the domain of that space.
438 const char *isl_aff_get_dim_name(__isl_keep isl_aff
*aff
,
439 enum isl_dim_type type
, unsigned pos
)
443 if (type
== isl_dim_out
)
445 if (type
== isl_dim_in
)
447 return isl_local_space_get_dim_name(aff
->ls
, type
, pos
);
450 __isl_give isl_aff
*isl_aff_reset_domain_space(__isl_take isl_aff
*aff
,
451 __isl_take isl_space
*dim
)
453 aff
= isl_aff_cow(aff
);
457 aff
->ls
= isl_local_space_reset_space(aff
->ls
, dim
);
459 return isl_aff_free(aff
);
468 /* Reset the space of "aff". This function is called from isl_pw_templ.c
469 * and doesn't know if the space of an element object is represented
470 * directly or through its domain. It therefore passes along both.
472 __isl_give isl_aff
*isl_aff_reset_space_and_domain(__isl_take isl_aff
*aff
,
473 __isl_take isl_space
*space
, __isl_take isl_space
*domain
)
475 isl_space_free(space
);
476 return isl_aff_reset_domain_space(aff
, domain
);
479 /* Reorder the coefficients of the affine expression based
480 * on the given reordering.
481 * The reordering r is assumed to have been extended with the local
484 static __isl_give isl_vec
*vec_reorder(__isl_take isl_vec
*vec
,
485 __isl_take isl_reordering
*r
, int n_div
)
493 res
= isl_vec_alloc(vec
->ctx
,
494 2 + isl_space_dim(r
->dim
, isl_dim_all
) + n_div
);
497 isl_seq_cpy(res
->el
, vec
->el
, 2);
498 isl_seq_clr(res
->el
+ 2, res
->size
- 2);
499 for (i
= 0; i
< r
->len
; ++i
)
500 isl_int_set(res
->el
[2 + r
->pos
[i
]], vec
->el
[2 + i
]);
502 isl_reordering_free(r
);
507 isl_reordering_free(r
);
511 /* Reorder the dimensions of the domain of "aff" according
512 * to the given reordering.
514 __isl_give isl_aff
*isl_aff_realign_domain(__isl_take isl_aff
*aff
,
515 __isl_take isl_reordering
*r
)
517 aff
= isl_aff_cow(aff
);
521 r
= isl_reordering_extend(r
, aff
->ls
->div
->n_row
);
522 aff
->v
= vec_reorder(aff
->v
, isl_reordering_copy(r
),
523 aff
->ls
->div
->n_row
);
524 aff
->ls
= isl_local_space_realign(aff
->ls
, r
);
526 if (!aff
->v
|| !aff
->ls
)
527 return isl_aff_free(aff
);
532 isl_reordering_free(r
);
536 __isl_give isl_aff
*isl_aff_align_params(__isl_take isl_aff
*aff
,
537 __isl_take isl_space
*model
)
539 isl_bool equal_params
;
544 equal_params
= isl_space_has_equal_params(aff
->ls
->dim
, model
);
545 if (equal_params
< 0)
550 model
= isl_space_drop_dims(model
, isl_dim_in
,
551 0, isl_space_dim(model
, isl_dim_in
));
552 model
= isl_space_drop_dims(model
, isl_dim_out
,
553 0, isl_space_dim(model
, isl_dim_out
));
554 exp
= isl_parameter_alignment_reordering(aff
->ls
->dim
, model
);
555 exp
= isl_reordering_extend_space(exp
,
556 isl_aff_get_domain_space(aff
));
557 aff
= isl_aff_realign_domain(aff
, exp
);
560 isl_space_free(model
);
563 isl_space_free(model
);
568 /* Is "aff" obviously equal to zero?
570 * If the denominator is zero, then "aff" is not equal to zero.
572 isl_bool
isl_aff_plain_is_zero(__isl_keep isl_aff
*aff
)
575 return isl_bool_error
;
577 if (isl_int_is_zero(aff
->v
->el
[0]))
578 return isl_bool_false
;
579 return isl_seq_first_non_zero(aff
->v
->el
+ 1, aff
->v
->size
- 1) < 0;
582 /* Does "aff" represent NaN?
584 isl_bool
isl_aff_is_nan(__isl_keep isl_aff
*aff
)
587 return isl_bool_error
;
589 return isl_seq_first_non_zero(aff
->v
->el
, 2) < 0;
592 /* Are "aff1" and "aff2" obviously equal?
594 * NaN is not equal to anything, not even to another NaN.
596 isl_bool
isl_aff_plain_is_equal(__isl_keep isl_aff
*aff1
,
597 __isl_keep isl_aff
*aff2
)
602 return isl_bool_error
;
604 if (isl_aff_is_nan(aff1
) || isl_aff_is_nan(aff2
))
605 return isl_bool_false
;
607 equal
= isl_local_space_is_equal(aff1
->ls
, aff2
->ls
);
608 if (equal
< 0 || !equal
)
611 return isl_vec_is_equal(aff1
->v
, aff2
->v
);
614 /* Return the common denominator of "aff" in "v".
616 * We cannot return anything meaningful in case of a NaN.
618 isl_stat
isl_aff_get_denominator(__isl_keep isl_aff
*aff
, isl_int
*v
)
621 return isl_stat_error
;
622 if (isl_aff_is_nan(aff
))
623 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
624 "cannot get denominator of NaN", return isl_stat_error
);
625 isl_int_set(*v
, aff
->v
->el
[0]);
629 /* Return the common denominator of "aff".
631 __isl_give isl_val
*isl_aff_get_denominator_val(__isl_keep isl_aff
*aff
)
638 ctx
= isl_aff_get_ctx(aff
);
639 if (isl_aff_is_nan(aff
))
640 return isl_val_nan(ctx
);
641 return isl_val_int_from_isl_int(ctx
, aff
->v
->el
[0]);
644 /* Return the constant term of "aff".
646 __isl_give isl_val
*isl_aff_get_constant_val(__isl_keep isl_aff
*aff
)
654 ctx
= isl_aff_get_ctx(aff
);
655 if (isl_aff_is_nan(aff
))
656 return isl_val_nan(ctx
);
657 v
= isl_val_rat_from_isl_int(ctx
, aff
->v
->el
[1], aff
->v
->el
[0]);
658 return isl_val_normalize(v
);
661 /* Return the coefficient of the variable of type "type" at position "pos"
664 __isl_give isl_val
*isl_aff_get_coefficient_val(__isl_keep isl_aff
*aff
,
665 enum isl_dim_type type
, int pos
)
673 ctx
= isl_aff_get_ctx(aff
);
674 if (type
== isl_dim_out
)
675 isl_die(ctx
, isl_error_invalid
,
676 "output/set dimension does not have a coefficient",
678 if (type
== isl_dim_in
)
681 if (pos
>= isl_local_space_dim(aff
->ls
, type
))
682 isl_die(ctx
, isl_error_invalid
,
683 "position out of bounds", return NULL
);
685 if (isl_aff_is_nan(aff
))
686 return isl_val_nan(ctx
);
687 pos
+= isl_local_space_offset(aff
->ls
, type
);
688 v
= isl_val_rat_from_isl_int(ctx
, aff
->v
->el
[1 + pos
], aff
->v
->el
[0]);
689 return isl_val_normalize(v
);
692 /* Return the sign of the coefficient of the variable of type "type"
693 * at position "pos" of "aff".
695 int isl_aff_coefficient_sgn(__isl_keep isl_aff
*aff
, enum isl_dim_type type
,
703 ctx
= isl_aff_get_ctx(aff
);
704 if (type
== isl_dim_out
)
705 isl_die(ctx
, isl_error_invalid
,
706 "output/set dimension does not have a coefficient",
708 if (type
== isl_dim_in
)
711 if (pos
>= isl_local_space_dim(aff
->ls
, type
))
712 isl_die(ctx
, isl_error_invalid
,
713 "position out of bounds", return 0);
715 pos
+= isl_local_space_offset(aff
->ls
, type
);
716 return isl_int_sgn(aff
->v
->el
[1 + pos
]);
719 /* Replace the numerator of the constant term of "aff" by "v".
721 * A NaN is unaffected by this operation.
723 __isl_give isl_aff
*isl_aff_set_constant(__isl_take isl_aff
*aff
, isl_int v
)
727 if (isl_aff_is_nan(aff
))
729 aff
= isl_aff_cow(aff
);
733 aff
->v
= isl_vec_cow(aff
->v
);
735 return isl_aff_free(aff
);
737 isl_int_set(aff
->v
->el
[1], v
);
742 /* Replace the constant term of "aff" by "v".
744 * A NaN is unaffected by this operation.
746 __isl_give isl_aff
*isl_aff_set_constant_val(__isl_take isl_aff
*aff
,
747 __isl_take isl_val
*v
)
752 if (isl_aff_is_nan(aff
)) {
757 if (!isl_val_is_rat(v
))
758 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
759 "expecting rational value", goto error
);
761 if (isl_int_eq(aff
->v
->el
[1], v
->n
) &&
762 isl_int_eq(aff
->v
->el
[0], v
->d
)) {
767 aff
= isl_aff_cow(aff
);
770 aff
->v
= isl_vec_cow(aff
->v
);
774 if (isl_int_eq(aff
->v
->el
[0], v
->d
)) {
775 isl_int_set(aff
->v
->el
[1], v
->n
);
776 } else if (isl_int_is_one(v
->d
)) {
777 isl_int_mul(aff
->v
->el
[1], aff
->v
->el
[0], v
->n
);
779 isl_seq_scale(aff
->v
->el
+ 1,
780 aff
->v
->el
+ 1, v
->d
, aff
->v
->size
- 1);
781 isl_int_mul(aff
->v
->el
[1], aff
->v
->el
[0], v
->n
);
782 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], v
->d
);
783 aff
->v
= isl_vec_normalize(aff
->v
);
796 /* Add "v" to the constant term of "aff".
798 * A NaN is unaffected by this operation.
800 __isl_give isl_aff
*isl_aff_add_constant(__isl_take isl_aff
*aff
, isl_int v
)
802 if (isl_int_is_zero(v
))
807 if (isl_aff_is_nan(aff
))
809 aff
= isl_aff_cow(aff
);
813 aff
->v
= isl_vec_cow(aff
->v
);
815 return isl_aff_free(aff
);
817 isl_int_addmul(aff
->v
->el
[1], aff
->v
->el
[0], v
);
822 /* Add "v" to the constant term of "aff".
824 * A NaN is unaffected by this operation.
826 __isl_give isl_aff
*isl_aff_add_constant_val(__isl_take isl_aff
*aff
,
827 __isl_take isl_val
*v
)
832 if (isl_aff_is_nan(aff
) || isl_val_is_zero(v
)) {
837 if (!isl_val_is_rat(v
))
838 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
839 "expecting rational value", goto error
);
841 aff
= isl_aff_cow(aff
);
845 aff
->v
= isl_vec_cow(aff
->v
);
849 if (isl_int_is_one(v
->d
)) {
850 isl_int_addmul(aff
->v
->el
[1], aff
->v
->el
[0], v
->n
);
851 } else if (isl_int_eq(aff
->v
->el
[0], v
->d
)) {
852 isl_int_add(aff
->v
->el
[1], aff
->v
->el
[1], v
->n
);
853 aff
->v
= isl_vec_normalize(aff
->v
);
857 isl_seq_scale(aff
->v
->el
+ 1,
858 aff
->v
->el
+ 1, v
->d
, aff
->v
->size
- 1);
859 isl_int_addmul(aff
->v
->el
[1], aff
->v
->el
[0], v
->n
);
860 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], v
->d
);
861 aff
->v
= isl_vec_normalize(aff
->v
);
874 __isl_give isl_aff
*isl_aff_add_constant_si(__isl_take isl_aff
*aff
, int v
)
879 isl_int_set_si(t
, v
);
880 aff
= isl_aff_add_constant(aff
, t
);
886 /* Add "v" to the numerator of the constant term of "aff".
888 * A NaN is unaffected by this operation.
890 __isl_give isl_aff
*isl_aff_add_constant_num(__isl_take isl_aff
*aff
, isl_int v
)
892 if (isl_int_is_zero(v
))
897 if (isl_aff_is_nan(aff
))
899 aff
= isl_aff_cow(aff
);
903 aff
->v
= isl_vec_cow(aff
->v
);
905 return isl_aff_free(aff
);
907 isl_int_add(aff
->v
->el
[1], aff
->v
->el
[1], v
);
912 /* Add "v" to the numerator of the constant term of "aff".
914 * A NaN is unaffected by this operation.
916 __isl_give isl_aff
*isl_aff_add_constant_num_si(__isl_take isl_aff
*aff
, int v
)
924 isl_int_set_si(t
, v
);
925 aff
= isl_aff_add_constant_num(aff
, t
);
931 /* Replace the numerator of the constant term of "aff" by "v".
933 * A NaN is unaffected by this operation.
935 __isl_give isl_aff
*isl_aff_set_constant_si(__isl_take isl_aff
*aff
, int v
)
939 if (isl_aff_is_nan(aff
))
941 aff
= isl_aff_cow(aff
);
945 aff
->v
= isl_vec_cow(aff
->v
);
947 return isl_aff_free(aff
);
949 isl_int_set_si(aff
->v
->el
[1], v
);
954 /* Replace the numerator of the coefficient of the variable of type "type"
955 * at position "pos" of "aff" by "v".
957 * A NaN is unaffected by this operation.
959 __isl_give isl_aff
*isl_aff_set_coefficient(__isl_take isl_aff
*aff
,
960 enum isl_dim_type type
, int pos
, isl_int v
)
965 if (type
== isl_dim_out
)
966 isl_die(aff
->v
->ctx
, isl_error_invalid
,
967 "output/set dimension does not have a coefficient",
968 return isl_aff_free(aff
));
969 if (type
== isl_dim_in
)
972 if (pos
>= isl_local_space_dim(aff
->ls
, type
))
973 isl_die(aff
->v
->ctx
, isl_error_invalid
,
974 "position out of bounds", return isl_aff_free(aff
));
976 if (isl_aff_is_nan(aff
))
978 aff
= isl_aff_cow(aff
);
982 aff
->v
= isl_vec_cow(aff
->v
);
984 return isl_aff_free(aff
);
986 pos
+= isl_local_space_offset(aff
->ls
, type
);
987 isl_int_set(aff
->v
->el
[1 + pos
], v
);
992 /* Replace the numerator of the coefficient of the variable of type "type"
993 * at position "pos" of "aff" by "v".
995 * A NaN is unaffected by this operation.
997 __isl_give isl_aff
*isl_aff_set_coefficient_si(__isl_take isl_aff
*aff
,
998 enum isl_dim_type type
, int pos
, int v
)
1003 if (type
== isl_dim_out
)
1004 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1005 "output/set dimension does not have a coefficient",
1006 return isl_aff_free(aff
));
1007 if (type
== isl_dim_in
)
1010 if (pos
< 0 || pos
>= isl_local_space_dim(aff
->ls
, type
))
1011 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1012 "position out of bounds", return isl_aff_free(aff
));
1014 if (isl_aff_is_nan(aff
))
1016 pos
+= isl_local_space_offset(aff
->ls
, type
);
1017 if (isl_int_cmp_si(aff
->v
->el
[1 + pos
], v
) == 0)
1020 aff
= isl_aff_cow(aff
);
1024 aff
->v
= isl_vec_cow(aff
->v
);
1026 return isl_aff_free(aff
);
1028 isl_int_set_si(aff
->v
->el
[1 + pos
], v
);
1033 /* Replace the coefficient of the variable of type "type" at position "pos"
1036 * A NaN is unaffected by this operation.
1038 __isl_give isl_aff
*isl_aff_set_coefficient_val(__isl_take isl_aff
*aff
,
1039 enum isl_dim_type type
, int pos
, __isl_take isl_val
*v
)
1044 if (type
== isl_dim_out
)
1045 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1046 "output/set dimension does not have a coefficient",
1048 if (type
== isl_dim_in
)
1051 if (pos
>= isl_local_space_dim(aff
->ls
, type
))
1052 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1053 "position out of bounds", goto error
);
1055 if (isl_aff_is_nan(aff
)) {
1059 if (!isl_val_is_rat(v
))
1060 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1061 "expecting rational value", goto error
);
1063 pos
+= isl_local_space_offset(aff
->ls
, type
);
1064 if (isl_int_eq(aff
->v
->el
[1 + pos
], v
->n
) &&
1065 isl_int_eq(aff
->v
->el
[0], v
->d
)) {
1070 aff
= isl_aff_cow(aff
);
1073 aff
->v
= isl_vec_cow(aff
->v
);
1077 if (isl_int_eq(aff
->v
->el
[0], v
->d
)) {
1078 isl_int_set(aff
->v
->el
[1 + pos
], v
->n
);
1079 } else if (isl_int_is_one(v
->d
)) {
1080 isl_int_mul(aff
->v
->el
[1 + pos
], aff
->v
->el
[0], v
->n
);
1082 isl_seq_scale(aff
->v
->el
+ 1,
1083 aff
->v
->el
+ 1, v
->d
, aff
->v
->size
- 1);
1084 isl_int_mul(aff
->v
->el
[1 + pos
], aff
->v
->el
[0], v
->n
);
1085 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], v
->d
);
1086 aff
->v
= isl_vec_normalize(aff
->v
);
1099 /* Add "v" to the coefficient of the variable of type "type"
1100 * at position "pos" of "aff".
1102 * A NaN is unaffected by this operation.
1104 __isl_give isl_aff
*isl_aff_add_coefficient(__isl_take isl_aff
*aff
,
1105 enum isl_dim_type type
, int pos
, isl_int v
)
1110 if (type
== isl_dim_out
)
1111 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1112 "output/set dimension does not have a coefficient",
1113 return isl_aff_free(aff
));
1114 if (type
== isl_dim_in
)
1117 if (pos
>= isl_local_space_dim(aff
->ls
, type
))
1118 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1119 "position out of bounds", return isl_aff_free(aff
));
1121 if (isl_aff_is_nan(aff
))
1123 aff
= isl_aff_cow(aff
);
1127 aff
->v
= isl_vec_cow(aff
->v
);
1129 return isl_aff_free(aff
);
1131 pos
+= isl_local_space_offset(aff
->ls
, type
);
1132 isl_int_addmul(aff
->v
->el
[1 + pos
], aff
->v
->el
[0], v
);
1137 /* Add "v" to the coefficient of the variable of type "type"
1138 * at position "pos" of "aff".
1140 * A NaN is unaffected by this operation.
1142 __isl_give isl_aff
*isl_aff_add_coefficient_val(__isl_take isl_aff
*aff
,
1143 enum isl_dim_type type
, int pos
, __isl_take isl_val
*v
)
1148 if (isl_val_is_zero(v
)) {
1153 if (type
== isl_dim_out
)
1154 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1155 "output/set dimension does not have a coefficient",
1157 if (type
== isl_dim_in
)
1160 if (pos
>= isl_local_space_dim(aff
->ls
, type
))
1161 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1162 "position out of bounds", goto error
);
1164 if (isl_aff_is_nan(aff
)) {
1168 if (!isl_val_is_rat(v
))
1169 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1170 "expecting rational value", goto error
);
1172 aff
= isl_aff_cow(aff
);
1176 aff
->v
= isl_vec_cow(aff
->v
);
1180 pos
+= isl_local_space_offset(aff
->ls
, type
);
1181 if (isl_int_is_one(v
->d
)) {
1182 isl_int_addmul(aff
->v
->el
[1 + pos
], aff
->v
->el
[0], v
->n
);
1183 } else if (isl_int_eq(aff
->v
->el
[0], v
->d
)) {
1184 isl_int_add(aff
->v
->el
[1 + pos
], aff
->v
->el
[1 + pos
], v
->n
);
1185 aff
->v
= isl_vec_normalize(aff
->v
);
1189 isl_seq_scale(aff
->v
->el
+ 1,
1190 aff
->v
->el
+ 1, v
->d
, aff
->v
->size
- 1);
1191 isl_int_addmul(aff
->v
->el
[1 + pos
], aff
->v
->el
[0], v
->n
);
1192 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], v
->d
);
1193 aff
->v
= isl_vec_normalize(aff
->v
);
1206 __isl_give isl_aff
*isl_aff_add_coefficient_si(__isl_take isl_aff
*aff
,
1207 enum isl_dim_type type
, int pos
, int v
)
1212 isl_int_set_si(t
, v
);
1213 aff
= isl_aff_add_coefficient(aff
, type
, pos
, t
);
1219 __isl_give isl_aff
*isl_aff_get_div(__isl_keep isl_aff
*aff
, int pos
)
1224 return isl_local_space_get_div(aff
->ls
, pos
);
1227 /* Return the negation of "aff".
1229 * As a special case, -NaN = NaN.
1231 __isl_give isl_aff
*isl_aff_neg(__isl_take isl_aff
*aff
)
1235 if (isl_aff_is_nan(aff
))
1237 aff
= isl_aff_cow(aff
);
1240 aff
->v
= isl_vec_cow(aff
->v
);
1242 return isl_aff_free(aff
);
1244 isl_seq_neg(aff
->v
->el
+ 1, aff
->v
->el
+ 1, aff
->v
->size
- 1);
1249 /* Remove divs from the local space that do not appear in the affine
1251 * We currently only remove divs at the end.
1252 * Some intermediate divs may also not appear directly in the affine
1253 * expression, but we would also need to check that no other divs are
1254 * defined in terms of them.
1256 __isl_give isl_aff
*isl_aff_remove_unused_divs(__isl_take isl_aff
*aff
)
1265 n
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
1266 off
= isl_local_space_offset(aff
->ls
, isl_dim_div
);
1268 pos
= isl_seq_last_non_zero(aff
->v
->el
+ 1 + off
, n
) + 1;
1272 aff
= isl_aff_cow(aff
);
1276 aff
->ls
= isl_local_space_drop_dims(aff
->ls
, isl_dim_div
, pos
, n
- pos
);
1277 aff
->v
= isl_vec_drop_els(aff
->v
, 1 + off
+ pos
, n
- pos
);
1278 if (!aff
->ls
|| !aff
->v
)
1279 return isl_aff_free(aff
);
1284 /* Look for any divs in the aff->ls with a denominator equal to one
1285 * and plug them into the affine expression and any subsequent divs
1286 * that may reference the div.
1288 static __isl_give isl_aff
*plug_in_integral_divs(__isl_take isl_aff
*aff
)
1294 isl_local_space
*ls
;
1300 n
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
1302 for (i
= 0; i
< n
; ++i
) {
1303 if (!isl_int_is_one(aff
->ls
->div
->row
[i
][0]))
1305 ls
= isl_local_space_copy(aff
->ls
);
1306 ls
= isl_local_space_substitute_seq(ls
, isl_dim_div
, i
,
1307 aff
->ls
->div
->row
[i
], len
, i
+ 1, n
- (i
+ 1));
1308 vec
= isl_vec_copy(aff
->v
);
1309 vec
= isl_vec_cow(vec
);
1315 pos
= isl_local_space_offset(aff
->ls
, isl_dim_div
) + i
;
1316 isl_seq_substitute(vec
->el
, pos
, aff
->ls
->div
->row
[i
],
1321 isl_vec_free(aff
->v
);
1323 isl_local_space_free(aff
->ls
);
1330 isl_local_space_free(ls
);
1331 return isl_aff_free(aff
);
1334 /* Look for any divs j that appear with a unit coefficient inside
1335 * the definitions of other divs i and plug them into the definitions
1338 * In particular, an expression of the form
1340 * floor((f(..) + floor(g(..)/n))/m)
1344 * floor((n * f(..) + g(..))/(n * m))
1346 * This simplification is correct because we can move the expression
1347 * f(..) into the inner floor in the original expression to obtain
1349 * floor(floor((n * f(..) + g(..))/n)/m)
1351 * from which we can derive the simplified expression.
1353 static __isl_give isl_aff
*plug_in_unit_divs(__isl_take isl_aff
*aff
)
1361 n
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
1362 off
= isl_local_space_offset(aff
->ls
, isl_dim_div
);
1363 for (i
= 1; i
< n
; ++i
) {
1364 for (j
= 0; j
< i
; ++j
) {
1365 if (!isl_int_is_one(aff
->ls
->div
->row
[i
][1 + off
+ j
]))
1367 aff
->ls
= isl_local_space_substitute_seq(aff
->ls
,
1368 isl_dim_div
, j
, aff
->ls
->div
->row
[j
],
1369 aff
->v
->size
, i
, 1);
1371 return isl_aff_free(aff
);
1378 /* Swap divs "a" and "b" in "aff", which is assumed to be non-NULL.
1380 * Even though this function is only called on isl_affs with a single
1381 * reference, we are careful to only change aff->v and aff->ls together.
1383 static __isl_give isl_aff
*swap_div(__isl_take isl_aff
*aff
, int a
, int b
)
1385 unsigned off
= isl_local_space_offset(aff
->ls
, isl_dim_div
);
1386 isl_local_space
*ls
;
1389 ls
= isl_local_space_copy(aff
->ls
);
1390 ls
= isl_local_space_swap_div(ls
, a
, b
);
1391 v
= isl_vec_copy(aff
->v
);
1396 isl_int_swap(v
->el
[1 + off
+ a
], v
->el
[1 + off
+ b
]);
1397 isl_vec_free(aff
->v
);
1399 isl_local_space_free(aff
->ls
);
1405 isl_local_space_free(ls
);
1406 return isl_aff_free(aff
);
1409 /* Merge divs "a" and "b" in "aff", which is assumed to be non-NULL.
1411 * We currently do not actually remove div "b", but simply add its
1412 * coefficient to that of "a" and then zero it out.
1414 static __isl_give isl_aff
*merge_divs(__isl_take isl_aff
*aff
, int a
, int b
)
1416 unsigned off
= isl_local_space_offset(aff
->ls
, isl_dim_div
);
1418 if (isl_int_is_zero(aff
->v
->el
[1 + off
+ b
]))
1421 aff
->v
= isl_vec_cow(aff
->v
);
1423 return isl_aff_free(aff
);
1425 isl_int_add(aff
->v
->el
[1 + off
+ a
],
1426 aff
->v
->el
[1 + off
+ a
], aff
->v
->el
[1 + off
+ b
]);
1427 isl_int_set_si(aff
->v
->el
[1 + off
+ b
], 0);
1432 /* Sort the divs in the local space of "aff" according to
1433 * the comparison function "cmp_row" in isl_local_space.c,
1434 * combining the coefficients of identical divs.
1436 * Reordering divs does not change the semantics of "aff",
1437 * so there is no need to call isl_aff_cow.
1438 * Moreover, this function is currently only called on isl_affs
1439 * with a single reference.
1441 static __isl_give isl_aff
*sort_divs(__isl_take isl_aff
*aff
)
1448 n
= isl_aff_dim(aff
, isl_dim_div
);
1449 for (i
= 1; i
< n
; ++i
) {
1450 for (j
= i
- 1; j
>= 0; --j
) {
1451 int cmp
= isl_mat_cmp_div(aff
->ls
->div
, j
, j
+ 1);
1455 aff
= merge_divs(aff
, j
, j
+ 1);
1457 aff
= swap_div(aff
, j
, j
+ 1);
1466 /* Normalize the representation of "aff".
1468 * This function should only be called of "new" isl_affs, i.e.,
1469 * with only a single reference. We therefore do not need to
1470 * worry about affecting other instances.
1472 __isl_give isl_aff
*isl_aff_normalize(__isl_take isl_aff
*aff
)
1476 aff
->v
= isl_vec_normalize(aff
->v
);
1478 return isl_aff_free(aff
);
1479 aff
= plug_in_integral_divs(aff
);
1480 aff
= plug_in_unit_divs(aff
);
1481 aff
= sort_divs(aff
);
1482 aff
= isl_aff_remove_unused_divs(aff
);
1486 /* Given f, return floor(f).
1487 * If f is an integer expression, then just return f.
1488 * If f is a constant, then return the constant floor(f).
1489 * Otherwise, if f = g/m, write g = q m + r,
1490 * create a new div d = [r/m] and return the expression q + d.
1491 * The coefficients in r are taken to lie between -m/2 and m/2.
1493 * reduce_div_coefficients performs the same normalization.
1495 * As a special case, floor(NaN) = NaN.
1497 __isl_give isl_aff
*isl_aff_floor(__isl_take isl_aff
*aff
)
1507 if (isl_aff_is_nan(aff
))
1509 if (isl_int_is_one(aff
->v
->el
[0]))
1512 aff
= isl_aff_cow(aff
);
1516 aff
->v
= isl_vec_cow(aff
->v
);
1518 return isl_aff_free(aff
);
1520 if (isl_aff_is_cst(aff
)) {
1521 isl_int_fdiv_q(aff
->v
->el
[1], aff
->v
->el
[1], aff
->v
->el
[0]);
1522 isl_int_set_si(aff
->v
->el
[0], 1);
1526 div
= isl_vec_copy(aff
->v
);
1527 div
= isl_vec_cow(div
);
1529 return isl_aff_free(aff
);
1531 ctx
= isl_aff_get_ctx(aff
);
1532 isl_int_fdiv_q(aff
->v
->el
[0], aff
->v
->el
[0], ctx
->two
);
1533 for (i
= 1; i
< aff
->v
->size
; ++i
) {
1534 isl_int_fdiv_r(div
->el
[i
], div
->el
[i
], div
->el
[0]);
1535 isl_int_fdiv_q(aff
->v
->el
[i
], aff
->v
->el
[i
], div
->el
[0]);
1536 if (isl_int_gt(div
->el
[i
], aff
->v
->el
[0])) {
1537 isl_int_sub(div
->el
[i
], div
->el
[i
], div
->el
[0]);
1538 isl_int_add_ui(aff
->v
->el
[i
], aff
->v
->el
[i
], 1);
1542 aff
->ls
= isl_local_space_add_div(aff
->ls
, div
);
1544 return isl_aff_free(aff
);
1546 size
= aff
->v
->size
;
1547 aff
->v
= isl_vec_extend(aff
->v
, size
+ 1);
1549 return isl_aff_free(aff
);
1550 isl_int_set_si(aff
->v
->el
[0], 1);
1551 isl_int_set_si(aff
->v
->el
[size
], 1);
1553 aff
= isl_aff_normalize(aff
);
1560 * aff mod m = aff - m * floor(aff/m)
1562 * with m an integer value.
1564 __isl_give isl_aff
*isl_aff_mod_val(__isl_take isl_aff
*aff
,
1565 __isl_take isl_val
*m
)
1572 if (!isl_val_is_int(m
))
1573 isl_die(isl_val_get_ctx(m
), isl_error_invalid
,
1574 "expecting integer modulo", goto error
);
1576 res
= isl_aff_copy(aff
);
1577 aff
= isl_aff_scale_down_val(aff
, isl_val_copy(m
));
1578 aff
= isl_aff_floor(aff
);
1579 aff
= isl_aff_scale_val(aff
, m
);
1580 res
= isl_aff_sub(res
, aff
);
1591 * pwaff mod m = pwaff - m * floor(pwaff/m)
1593 __isl_give isl_pw_aff
*isl_pw_aff_mod(__isl_take isl_pw_aff
*pwaff
, isl_int m
)
1597 res
= isl_pw_aff_copy(pwaff
);
1598 pwaff
= isl_pw_aff_scale_down(pwaff
, m
);
1599 pwaff
= isl_pw_aff_floor(pwaff
);
1600 pwaff
= isl_pw_aff_scale(pwaff
, m
);
1601 res
= isl_pw_aff_sub(res
, pwaff
);
1608 * pa mod m = pa - m * floor(pa/m)
1610 * with m an integer value.
1612 __isl_give isl_pw_aff
*isl_pw_aff_mod_val(__isl_take isl_pw_aff
*pa
,
1613 __isl_take isl_val
*m
)
1617 if (!isl_val_is_int(m
))
1618 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
1619 "expecting integer modulo", goto error
);
1620 pa
= isl_pw_aff_mod(pa
, m
->n
);
1624 isl_pw_aff_free(pa
);
1629 /* Given f, return ceil(f).
1630 * If f is an integer expression, then just return f.
1631 * Otherwise, let f be the expression
1637 * floor((e + m - 1)/m)
1639 * As a special case, ceil(NaN) = NaN.
1641 __isl_give isl_aff
*isl_aff_ceil(__isl_take isl_aff
*aff
)
1646 if (isl_aff_is_nan(aff
))
1648 if (isl_int_is_one(aff
->v
->el
[0]))
1651 aff
= isl_aff_cow(aff
);
1654 aff
->v
= isl_vec_cow(aff
->v
);
1656 return isl_aff_free(aff
);
1658 isl_int_add(aff
->v
->el
[1], aff
->v
->el
[1], aff
->v
->el
[0]);
1659 isl_int_sub_ui(aff
->v
->el
[1], aff
->v
->el
[1], 1);
1660 aff
= isl_aff_floor(aff
);
1665 /* Apply the expansion computed by isl_merge_divs.
1666 * The expansion itself is given by "exp" while the resulting
1667 * list of divs is given by "div".
1669 __isl_give isl_aff
*isl_aff_expand_divs(__isl_take isl_aff
*aff
,
1670 __isl_take isl_mat
*div
, int *exp
)
1676 aff
= isl_aff_cow(aff
);
1680 old_n_div
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
1681 new_n_div
= isl_mat_rows(div
);
1682 offset
= 1 + isl_local_space_offset(aff
->ls
, isl_dim_div
);
1684 aff
->v
= isl_vec_expand(aff
->v
, offset
, old_n_div
, exp
, new_n_div
);
1685 aff
->ls
= isl_local_space_replace_divs(aff
->ls
, div
);
1686 if (!aff
->v
|| !aff
->ls
)
1687 return isl_aff_free(aff
);
1695 /* Add two affine expressions that live in the same local space.
1697 static __isl_give isl_aff
*add_expanded(__isl_take isl_aff
*aff1
,
1698 __isl_take isl_aff
*aff2
)
1702 aff1
= isl_aff_cow(aff1
);
1706 aff1
->v
= isl_vec_cow(aff1
->v
);
1712 isl_int_gcd(gcd
, aff1
->v
->el
[0], aff2
->v
->el
[0]);
1713 isl_int_divexact(f
, aff2
->v
->el
[0], gcd
);
1714 isl_seq_scale(aff1
->v
->el
+ 1, aff1
->v
->el
+ 1, f
, aff1
->v
->size
- 1);
1715 isl_int_divexact(f
, aff1
->v
->el
[0], gcd
);
1716 isl_seq_addmul(aff1
->v
->el
+ 1, f
, aff2
->v
->el
+ 1, aff1
->v
->size
- 1);
1717 isl_int_divexact(f
, aff2
->v
->el
[0], gcd
);
1718 isl_int_mul(aff1
->v
->el
[0], aff1
->v
->el
[0], f
);
1730 /* Return the sum of "aff1" and "aff2".
1732 * If either of the two is NaN, then the result is NaN.
1734 __isl_give isl_aff
*isl_aff_add(__isl_take isl_aff
*aff1
,
1735 __isl_take isl_aff
*aff2
)
1746 ctx
= isl_aff_get_ctx(aff1
);
1747 if (!isl_space_is_equal(aff1
->ls
->dim
, aff2
->ls
->dim
))
1748 isl_die(ctx
, isl_error_invalid
,
1749 "spaces don't match", goto error
);
1751 if (isl_aff_is_nan(aff1
)) {
1755 if (isl_aff_is_nan(aff2
)) {
1760 n_div1
= isl_aff_dim(aff1
, isl_dim_div
);
1761 n_div2
= isl_aff_dim(aff2
, isl_dim_div
);
1762 if (n_div1
== 0 && n_div2
== 0)
1763 return add_expanded(aff1
, aff2
);
1765 exp1
= isl_alloc_array(ctx
, int, n_div1
);
1766 exp2
= isl_alloc_array(ctx
, int, n_div2
);
1767 if ((n_div1
&& !exp1
) || (n_div2
&& !exp2
))
1770 div
= isl_merge_divs(aff1
->ls
->div
, aff2
->ls
->div
, exp1
, exp2
);
1771 aff1
= isl_aff_expand_divs(aff1
, isl_mat_copy(div
), exp1
);
1772 aff2
= isl_aff_expand_divs(aff2
, div
, exp2
);
1776 return add_expanded(aff1
, aff2
);
1785 __isl_give isl_aff
*isl_aff_sub(__isl_take isl_aff
*aff1
,
1786 __isl_take isl_aff
*aff2
)
1788 return isl_aff_add(aff1
, isl_aff_neg(aff2
));
1791 /* Return the result of scaling "aff" by a factor of "f".
1793 * As a special case, f * NaN = NaN.
1795 __isl_give isl_aff
*isl_aff_scale(__isl_take isl_aff
*aff
, isl_int f
)
1801 if (isl_aff_is_nan(aff
))
1804 if (isl_int_is_one(f
))
1807 aff
= isl_aff_cow(aff
);
1810 aff
->v
= isl_vec_cow(aff
->v
);
1812 return isl_aff_free(aff
);
1814 if (isl_int_is_pos(f
) && isl_int_is_divisible_by(aff
->v
->el
[0], f
)) {
1815 isl_int_divexact(aff
->v
->el
[0], aff
->v
->el
[0], f
);
1820 isl_int_gcd(gcd
, aff
->v
->el
[0], f
);
1821 isl_int_divexact(aff
->v
->el
[0], aff
->v
->el
[0], gcd
);
1822 isl_int_divexact(gcd
, f
, gcd
);
1823 isl_seq_scale(aff
->v
->el
+ 1, aff
->v
->el
+ 1, gcd
, aff
->v
->size
- 1);
1829 /* Multiple "aff" by "v".
1831 __isl_give isl_aff
*isl_aff_scale_val(__isl_take isl_aff
*aff
,
1832 __isl_take isl_val
*v
)
1837 if (isl_val_is_one(v
)) {
1842 if (!isl_val_is_rat(v
))
1843 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1844 "expecting rational factor", goto error
);
1846 aff
= isl_aff_scale(aff
, v
->n
);
1847 aff
= isl_aff_scale_down(aff
, v
->d
);
1857 /* Return the result of scaling "aff" down by a factor of "f".
1859 * As a special case, NaN/f = NaN.
1861 __isl_give isl_aff
*isl_aff_scale_down(__isl_take isl_aff
*aff
, isl_int f
)
1867 if (isl_aff_is_nan(aff
))
1870 if (isl_int_is_one(f
))
1873 aff
= isl_aff_cow(aff
);
1877 if (isl_int_is_zero(f
))
1878 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1879 "cannot scale down by zero", return isl_aff_free(aff
));
1881 aff
->v
= isl_vec_cow(aff
->v
);
1883 return isl_aff_free(aff
);
1886 isl_seq_gcd(aff
->v
->el
+ 1, aff
->v
->size
- 1, &gcd
);
1887 isl_int_gcd(gcd
, gcd
, f
);
1888 isl_seq_scale_down(aff
->v
->el
+ 1, aff
->v
->el
+ 1, gcd
, aff
->v
->size
- 1);
1889 isl_int_divexact(gcd
, f
, gcd
);
1890 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], gcd
);
1896 /* Divide "aff" by "v".
1898 __isl_give isl_aff
*isl_aff_scale_down_val(__isl_take isl_aff
*aff
,
1899 __isl_take isl_val
*v
)
1904 if (isl_val_is_one(v
)) {
1909 if (!isl_val_is_rat(v
))
1910 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1911 "expecting rational factor", goto error
);
1912 if (!isl_val_is_pos(v
))
1913 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1914 "factor needs to be positive", goto error
);
1916 aff
= isl_aff_scale(aff
, v
->d
);
1917 aff
= isl_aff_scale_down(aff
, v
->n
);
1927 __isl_give isl_aff
*isl_aff_scale_down_ui(__isl_take isl_aff
*aff
, unsigned f
)
1935 isl_int_set_ui(v
, f
);
1936 aff
= isl_aff_scale_down(aff
, v
);
1942 __isl_give isl_aff
*isl_aff_set_dim_name(__isl_take isl_aff
*aff
,
1943 enum isl_dim_type type
, unsigned pos
, const char *s
)
1945 aff
= isl_aff_cow(aff
);
1948 if (type
== isl_dim_out
)
1949 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1950 "cannot set name of output/set dimension",
1951 return isl_aff_free(aff
));
1952 if (type
== isl_dim_in
)
1954 aff
->ls
= isl_local_space_set_dim_name(aff
->ls
, type
, pos
, s
);
1956 return isl_aff_free(aff
);
1961 __isl_give isl_aff
*isl_aff_set_dim_id(__isl_take isl_aff
*aff
,
1962 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
1964 aff
= isl_aff_cow(aff
);
1967 if (type
== isl_dim_out
)
1968 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1969 "cannot set name of output/set dimension",
1971 if (type
== isl_dim_in
)
1973 aff
->ls
= isl_local_space_set_dim_id(aff
->ls
, type
, pos
, id
);
1975 return isl_aff_free(aff
);
1984 /* Replace the identifier of the input tuple of "aff" by "id".
1985 * type is currently required to be equal to isl_dim_in
1987 __isl_give isl_aff
*isl_aff_set_tuple_id(__isl_take isl_aff
*aff
,
1988 enum isl_dim_type type
, __isl_take isl_id
*id
)
1990 aff
= isl_aff_cow(aff
);
1993 if (type
!= isl_dim_in
)
1994 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1995 "cannot only set id of input tuple", goto error
);
1996 aff
->ls
= isl_local_space_set_tuple_id(aff
->ls
, isl_dim_set
, id
);
1998 return isl_aff_free(aff
);
2007 /* Exploit the equalities in "eq" to simplify the affine expression
2008 * and the expressions of the integer divisions in the local space.
2009 * The integer divisions in this local space are assumed to appear
2010 * as regular dimensions in "eq".
2012 static __isl_give isl_aff
*isl_aff_substitute_equalities_lifted(
2013 __isl_take isl_aff
*aff
, __isl_take isl_basic_set
*eq
)
2021 if (eq
->n_eq
== 0) {
2022 isl_basic_set_free(eq
);
2026 aff
= isl_aff_cow(aff
);
2030 aff
->ls
= isl_local_space_substitute_equalities(aff
->ls
,
2031 isl_basic_set_copy(eq
));
2032 aff
->v
= isl_vec_cow(aff
->v
);
2033 if (!aff
->ls
|| !aff
->v
)
2036 total
= 1 + isl_space_dim(eq
->dim
, isl_dim_all
);
2038 for (i
= 0; i
< eq
->n_eq
; ++i
) {
2039 j
= isl_seq_last_non_zero(eq
->eq
[i
], total
+ n_div
);
2040 if (j
< 0 || j
== 0 || j
>= total
)
2043 isl_seq_elim(aff
->v
->el
+ 1, eq
->eq
[i
], j
, total
,
2047 isl_basic_set_free(eq
);
2048 aff
= isl_aff_normalize(aff
);
2051 isl_basic_set_free(eq
);
2056 /* Exploit the equalities in "eq" to simplify the affine expression
2057 * and the expressions of the integer divisions in the local space.
2059 __isl_give isl_aff
*isl_aff_substitute_equalities(__isl_take isl_aff
*aff
,
2060 __isl_take isl_basic_set
*eq
)
2066 n_div
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
2068 eq
= isl_basic_set_add_dims(eq
, isl_dim_set
, n_div
);
2069 return isl_aff_substitute_equalities_lifted(aff
, eq
);
2071 isl_basic_set_free(eq
);
2076 /* Look for equalities among the variables shared by context and aff
2077 * and the integer divisions of aff, if any.
2078 * The equalities are then used to eliminate coefficients and/or integer
2079 * divisions from aff.
2081 __isl_give isl_aff
*isl_aff_gist(__isl_take isl_aff
*aff
,
2082 __isl_take isl_set
*context
)
2084 isl_basic_set
*hull
;
2089 n_div
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
2091 isl_basic_set
*bset
;
2092 isl_local_space
*ls
;
2093 context
= isl_set_add_dims(context
, isl_dim_set
, n_div
);
2094 ls
= isl_aff_get_domain_local_space(aff
);
2095 bset
= isl_basic_set_from_local_space(ls
);
2096 bset
= isl_basic_set_lift(bset
);
2097 bset
= isl_basic_set_flatten(bset
);
2098 context
= isl_set_intersect(context
,
2099 isl_set_from_basic_set(bset
));
2102 hull
= isl_set_affine_hull(context
);
2103 return isl_aff_substitute_equalities_lifted(aff
, hull
);
2106 isl_set_free(context
);
2110 __isl_give isl_aff
*isl_aff_gist_params(__isl_take isl_aff
*aff
,
2111 __isl_take isl_set
*context
)
2113 isl_set
*dom_context
= isl_set_universe(isl_aff_get_domain_space(aff
));
2114 dom_context
= isl_set_intersect_params(dom_context
, context
);
2115 return isl_aff_gist(aff
, dom_context
);
2118 /* Return a basic set containing those elements in the space
2119 * of aff where it is positive. "rational" should not be set.
2121 * If "aff" is NaN, then it is not positive.
2123 static __isl_give isl_basic_set
*aff_pos_basic_set(__isl_take isl_aff
*aff
,
2126 isl_constraint
*ineq
;
2127 isl_basic_set
*bset
;
2132 if (isl_aff_is_nan(aff
)) {
2133 isl_space
*space
= isl_aff_get_domain_space(aff
);
2135 return isl_basic_set_empty(space
);
2138 isl_die(isl_aff_get_ctx(aff
), isl_error_unsupported
,
2139 "rational sets not supported", goto error
);
2141 ineq
= isl_inequality_from_aff(aff
);
2142 c
= isl_constraint_get_constant_val(ineq
);
2143 c
= isl_val_sub_ui(c
, 1);
2144 ineq
= isl_constraint_set_constant_val(ineq
, c
);
2146 bset
= isl_basic_set_from_constraint(ineq
);
2147 bset
= isl_basic_set_simplify(bset
);
2154 /* Return a basic set containing those elements in the space
2155 * of aff where it is non-negative.
2156 * If "rational" is set, then return a rational basic set.
2158 * If "aff" is NaN, then it is not non-negative (it's not negative either).
2160 static __isl_give isl_basic_set
*aff_nonneg_basic_set(
2161 __isl_take isl_aff
*aff
, int rational
)
2163 isl_constraint
*ineq
;
2164 isl_basic_set
*bset
;
2168 if (isl_aff_is_nan(aff
)) {
2169 isl_space
*space
= isl_aff_get_domain_space(aff
);
2171 return isl_basic_set_empty(space
);
2174 ineq
= isl_inequality_from_aff(aff
);
2176 bset
= isl_basic_set_from_constraint(ineq
);
2178 bset
= isl_basic_set_set_rational(bset
);
2179 bset
= isl_basic_set_simplify(bset
);
2183 /* Return a basic set containing those elements in the space
2184 * of aff where it is non-negative.
2186 __isl_give isl_basic_set
*isl_aff_nonneg_basic_set(__isl_take isl_aff
*aff
)
2188 return aff_nonneg_basic_set(aff
, 0);
2191 /* Return a basic set containing those elements in the domain space
2192 * of "aff" where it is positive.
2194 __isl_give isl_basic_set
*isl_aff_pos_basic_set(__isl_take isl_aff
*aff
)
2196 aff
= isl_aff_add_constant_num_si(aff
, -1);
2197 return isl_aff_nonneg_basic_set(aff
);
2200 /* Return a basic set containing those elements in the domain space
2201 * of aff where it is negative.
2203 __isl_give isl_basic_set
*isl_aff_neg_basic_set(__isl_take isl_aff
*aff
)
2205 aff
= isl_aff_neg(aff
);
2206 return isl_aff_pos_basic_set(aff
);
2209 /* Return a basic set containing those elements in the space
2210 * of aff where it is zero.
2211 * If "rational" is set, then return a rational basic set.
2213 * If "aff" is NaN, then it is not zero.
2215 static __isl_give isl_basic_set
*aff_zero_basic_set(__isl_take isl_aff
*aff
,
2218 isl_constraint
*ineq
;
2219 isl_basic_set
*bset
;
2223 if (isl_aff_is_nan(aff
)) {
2224 isl_space
*space
= isl_aff_get_domain_space(aff
);
2226 return isl_basic_set_empty(space
);
2229 ineq
= isl_equality_from_aff(aff
);
2231 bset
= isl_basic_set_from_constraint(ineq
);
2233 bset
= isl_basic_set_set_rational(bset
);
2234 bset
= isl_basic_set_simplify(bset
);
2238 /* Return a basic set containing those elements in the space
2239 * of aff where it is zero.
2241 __isl_give isl_basic_set
*isl_aff_zero_basic_set(__isl_take isl_aff
*aff
)
2243 return aff_zero_basic_set(aff
, 0);
2246 /* Return a basic set containing those elements in the shared space
2247 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
2249 __isl_give isl_basic_set
*isl_aff_ge_basic_set(__isl_take isl_aff
*aff1
,
2250 __isl_take isl_aff
*aff2
)
2252 aff1
= isl_aff_sub(aff1
, aff2
);
2254 return isl_aff_nonneg_basic_set(aff1
);
2257 /* Return a basic set containing those elements in the shared domain space
2258 * of "aff1" and "aff2" where "aff1" is greater than "aff2".
2260 __isl_give isl_basic_set
*isl_aff_gt_basic_set(__isl_take isl_aff
*aff1
,
2261 __isl_take isl_aff
*aff2
)
2263 aff1
= isl_aff_sub(aff1
, aff2
);
2265 return isl_aff_pos_basic_set(aff1
);
2268 /* Return a set containing those elements in the shared space
2269 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
2271 __isl_give isl_set
*isl_aff_ge_set(__isl_take isl_aff
*aff1
,
2272 __isl_take isl_aff
*aff2
)
2274 return isl_set_from_basic_set(isl_aff_ge_basic_set(aff1
, aff2
));
2277 /* Return a set containing those elements in the shared domain space
2278 * of aff1 and aff2 where aff1 is greater than aff2.
2280 * If either of the two inputs is NaN, then the result is empty,
2281 * as comparisons with NaN always return false.
2283 __isl_give isl_set
*isl_aff_gt_set(__isl_take isl_aff
*aff1
,
2284 __isl_take isl_aff
*aff2
)
2286 return isl_set_from_basic_set(isl_aff_gt_basic_set(aff1
, aff2
));
2289 /* Return a basic set containing those elements in the shared space
2290 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
2292 __isl_give isl_basic_set
*isl_aff_le_basic_set(__isl_take isl_aff
*aff1
,
2293 __isl_take isl_aff
*aff2
)
2295 return isl_aff_ge_basic_set(aff2
, aff1
);
2298 /* Return a basic set containing those elements in the shared domain space
2299 * of "aff1" and "aff2" where "aff1" is smaller than "aff2".
2301 __isl_give isl_basic_set
*isl_aff_lt_basic_set(__isl_take isl_aff
*aff1
,
2302 __isl_take isl_aff
*aff2
)
2304 return isl_aff_gt_basic_set(aff2
, aff1
);
2307 /* Return a set containing those elements in the shared space
2308 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
2310 __isl_give isl_set
*isl_aff_le_set(__isl_take isl_aff
*aff1
,
2311 __isl_take isl_aff
*aff2
)
2313 return isl_aff_ge_set(aff2
, aff1
);
2316 /* Return a set containing those elements in the shared domain space
2317 * of "aff1" and "aff2" where "aff1" is smaller than "aff2".
2319 __isl_give isl_set
*isl_aff_lt_set(__isl_take isl_aff
*aff1
,
2320 __isl_take isl_aff
*aff2
)
2322 return isl_set_from_basic_set(isl_aff_lt_basic_set(aff1
, aff2
));
2325 /* Return a basic set containing those elements in the shared space
2326 * of aff1 and aff2 where aff1 and aff2 are equal.
2328 __isl_give isl_basic_set
*isl_aff_eq_basic_set(__isl_take isl_aff
*aff1
,
2329 __isl_take isl_aff
*aff2
)
2331 aff1
= isl_aff_sub(aff1
, aff2
);
2333 return isl_aff_zero_basic_set(aff1
);
2336 /* Return a set containing those elements in the shared space
2337 * of aff1 and aff2 where aff1 and aff2 are equal.
2339 __isl_give isl_set
*isl_aff_eq_set(__isl_take isl_aff
*aff1
,
2340 __isl_take isl_aff
*aff2
)
2342 return isl_set_from_basic_set(isl_aff_eq_basic_set(aff1
, aff2
));
2345 /* Return a set containing those elements in the shared domain space
2346 * of aff1 and aff2 where aff1 and aff2 are not equal.
2348 * If either of the two inputs is NaN, then the result is empty,
2349 * as comparisons with NaN always return false.
2351 __isl_give isl_set
*isl_aff_ne_set(__isl_take isl_aff
*aff1
,
2352 __isl_take isl_aff
*aff2
)
2354 isl_set
*set_lt
, *set_gt
;
2356 set_lt
= isl_aff_lt_set(isl_aff_copy(aff1
),
2357 isl_aff_copy(aff2
));
2358 set_gt
= isl_aff_gt_set(aff1
, aff2
);
2359 return isl_set_union_disjoint(set_lt
, set_gt
);
2362 __isl_give isl_aff
*isl_aff_add_on_domain(__isl_keep isl_set
*dom
,
2363 __isl_take isl_aff
*aff1
, __isl_take isl_aff
*aff2
)
2365 aff1
= isl_aff_add(aff1
, aff2
);
2366 aff1
= isl_aff_gist(aff1
, isl_set_copy(dom
));
2370 int isl_aff_is_empty(__isl_keep isl_aff
*aff
)
2378 /* Check whether the given affine expression has non-zero coefficient
2379 * for any dimension in the given range or if any of these dimensions
2380 * appear with non-zero coefficients in any of the integer divisions
2381 * involved in the affine expression.
2383 isl_bool
isl_aff_involves_dims(__isl_keep isl_aff
*aff
,
2384 enum isl_dim_type type
, unsigned first
, unsigned n
)
2389 isl_bool involves
= isl_bool_false
;
2392 return isl_bool_error
;
2394 return isl_bool_false
;
2396 ctx
= isl_aff_get_ctx(aff
);
2397 if (first
+ n
> isl_aff_dim(aff
, type
))
2398 isl_die(ctx
, isl_error_invalid
,
2399 "range out of bounds", return isl_bool_error
);
2401 active
= isl_local_space_get_active(aff
->ls
, aff
->v
->el
+ 2);
2405 first
+= isl_local_space_offset(aff
->ls
, type
) - 1;
2406 for (i
= 0; i
< n
; ++i
)
2407 if (active
[first
+ i
]) {
2408 involves
= isl_bool_true
;
2417 return isl_bool_error
;
2420 __isl_give isl_aff
*isl_aff_drop_dims(__isl_take isl_aff
*aff
,
2421 enum isl_dim_type type
, unsigned first
, unsigned n
)
2427 if (type
== isl_dim_out
)
2428 isl_die(aff
->v
->ctx
, isl_error_invalid
,
2429 "cannot drop output/set dimension",
2430 return isl_aff_free(aff
));
2431 if (type
== isl_dim_in
)
2433 if (n
== 0 && !isl_local_space_is_named_or_nested(aff
->ls
, type
))
2436 ctx
= isl_aff_get_ctx(aff
);
2437 if (first
+ n
> isl_local_space_dim(aff
->ls
, type
))
2438 isl_die(ctx
, isl_error_invalid
, "range out of bounds",
2439 return isl_aff_free(aff
));
2441 aff
= isl_aff_cow(aff
);
2445 aff
->ls
= isl_local_space_drop_dims(aff
->ls
, type
, first
, n
);
2447 return isl_aff_free(aff
);
2449 first
+= 1 + isl_local_space_offset(aff
->ls
, type
);
2450 aff
->v
= isl_vec_drop_els(aff
->v
, first
, n
);
2452 return isl_aff_free(aff
);
2457 /* Project the domain of the affine expression onto its parameter space.
2458 * The affine expression may not involve any of the domain dimensions.
2460 __isl_give isl_aff
*isl_aff_project_domain_on_params(__isl_take isl_aff
*aff
)
2466 n
= isl_aff_dim(aff
, isl_dim_in
);
2467 involves
= isl_aff_involves_dims(aff
, isl_dim_in
, 0, n
);
2469 return isl_aff_free(aff
);
2471 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2472 "affine expression involves some of the domain dimensions",
2473 return isl_aff_free(aff
));
2474 aff
= isl_aff_drop_dims(aff
, isl_dim_in
, 0, n
);
2475 space
= isl_aff_get_domain_space(aff
);
2476 space
= isl_space_params(space
);
2477 aff
= isl_aff_reset_domain_space(aff
, space
);
2481 /* Convert an affine expression defined over a parameter domain
2482 * into one that is defined over a zero-dimensional set.
2484 __isl_give isl_aff
*isl_aff_from_range(__isl_take isl_aff
*aff
)
2486 isl_local_space
*ls
;
2488 ls
= isl_aff_take_domain_local_space(aff
);
2489 ls
= isl_local_space_set_from_params(ls
);
2490 aff
= isl_aff_restore_domain_local_space(aff
, ls
);
2495 __isl_give isl_aff
*isl_aff_insert_dims(__isl_take isl_aff
*aff
,
2496 enum isl_dim_type type
, unsigned first
, unsigned n
)
2502 if (type
== isl_dim_out
)
2503 isl_die(aff
->v
->ctx
, isl_error_invalid
,
2504 "cannot insert output/set dimensions",
2505 return isl_aff_free(aff
));
2506 if (type
== isl_dim_in
)
2508 if (n
== 0 && !isl_local_space_is_named_or_nested(aff
->ls
, type
))
2511 ctx
= isl_aff_get_ctx(aff
);
2512 if (first
> isl_local_space_dim(aff
->ls
, type
))
2513 isl_die(ctx
, isl_error_invalid
, "position out of bounds",
2514 return isl_aff_free(aff
));
2516 aff
= isl_aff_cow(aff
);
2520 aff
->ls
= isl_local_space_insert_dims(aff
->ls
, type
, first
, n
);
2522 return isl_aff_free(aff
);
2524 first
+= 1 + isl_local_space_offset(aff
->ls
, type
);
2525 aff
->v
= isl_vec_insert_zero_els(aff
->v
, first
, n
);
2527 return isl_aff_free(aff
);
2532 __isl_give isl_aff
*isl_aff_add_dims(__isl_take isl_aff
*aff
,
2533 enum isl_dim_type type
, unsigned n
)
2537 pos
= isl_aff_dim(aff
, type
);
2539 return isl_aff_insert_dims(aff
, type
, pos
, n
);
2542 __isl_give isl_pw_aff
*isl_pw_aff_add_dims(__isl_take isl_pw_aff
*pwaff
,
2543 enum isl_dim_type type
, unsigned n
)
2547 pos
= isl_pw_aff_dim(pwaff
, type
);
2549 return isl_pw_aff_insert_dims(pwaff
, type
, pos
, n
);
2552 /* Move the "n" dimensions of "src_type" starting at "src_pos" of "aff"
2553 * to dimensions of "dst_type" at "dst_pos".
2555 * We only support moving input dimensions to parameters and vice versa.
2557 __isl_give isl_aff
*isl_aff_move_dims(__isl_take isl_aff
*aff
,
2558 enum isl_dim_type dst_type
, unsigned dst_pos
,
2559 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
2567 !isl_local_space_is_named_or_nested(aff
->ls
, src_type
) &&
2568 !isl_local_space_is_named_or_nested(aff
->ls
, dst_type
))
2571 if (dst_type
== isl_dim_out
|| src_type
== isl_dim_out
)
2572 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2573 "cannot move output/set dimension",
2574 return isl_aff_free(aff
));
2575 if (dst_type
== isl_dim_div
|| src_type
== isl_dim_div
)
2576 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2577 "cannot move divs", return isl_aff_free(aff
));
2578 if (dst_type
== isl_dim_in
)
2579 dst_type
= isl_dim_set
;
2580 if (src_type
== isl_dim_in
)
2581 src_type
= isl_dim_set
;
2583 if (src_pos
+ n
> isl_local_space_dim(aff
->ls
, src_type
))
2584 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2585 "range out of bounds", return isl_aff_free(aff
));
2586 if (dst_type
== src_type
)
2587 isl_die(isl_aff_get_ctx(aff
), isl_error_unsupported
,
2588 "moving dims within the same type not supported",
2589 return isl_aff_free(aff
));
2591 aff
= isl_aff_cow(aff
);
2595 g_src_pos
= 1 + isl_local_space_offset(aff
->ls
, src_type
) + src_pos
;
2596 g_dst_pos
= 1 + isl_local_space_offset(aff
->ls
, dst_type
) + dst_pos
;
2597 if (dst_type
> src_type
)
2600 aff
->v
= isl_vec_move_els(aff
->v
, g_dst_pos
, g_src_pos
, n
);
2601 aff
->ls
= isl_local_space_move_dims(aff
->ls
, dst_type
, dst_pos
,
2602 src_type
, src_pos
, n
);
2603 if (!aff
->v
|| !aff
->ls
)
2604 return isl_aff_free(aff
);
2606 aff
= sort_divs(aff
);
2611 __isl_give isl_pw_aff
*isl_pw_aff_from_aff(__isl_take isl_aff
*aff
)
2613 isl_set
*dom
= isl_set_universe(isl_aff_get_domain_space(aff
));
2614 return isl_pw_aff_alloc(dom
, aff
);
2617 #define isl_aff_involves_nan isl_aff_is_nan
2620 #define PW isl_pw_aff
2624 #define EL_IS_ZERO is_empty
2628 #define IS_ZERO is_empty
2631 #undef DEFAULT_IS_ZERO
2632 #define DEFAULT_IS_ZERO 0
2639 #include <isl_pw_templ.c>
2640 #include <isl_pw_hash.c>
2641 #include <isl_pw_union_opt.c>
2644 #define UNION isl_union_pw_aff
2646 #define PART isl_pw_aff
2648 #define PARTS pw_aff
2650 #include <isl_union_single.c>
2651 #include <isl_union_neg.c>
2653 static __isl_give isl_set
*align_params_pw_pw_set_and(
2654 __isl_take isl_pw_aff
*pwaff1
, __isl_take isl_pw_aff
*pwaff2
,
2655 __isl_give isl_set
*(*fn
)(__isl_take isl_pw_aff
*pwaff1
,
2656 __isl_take isl_pw_aff
*pwaff2
))
2658 isl_bool equal_params
;
2660 if (!pwaff1
|| !pwaff2
)
2662 equal_params
= isl_space_has_equal_params(pwaff1
->dim
, pwaff2
->dim
);
2663 if (equal_params
< 0)
2666 return fn(pwaff1
, pwaff2
);
2667 if (!isl_space_has_named_params(pwaff1
->dim
) ||
2668 !isl_space_has_named_params(pwaff2
->dim
))
2669 isl_die(isl_pw_aff_get_ctx(pwaff1
), isl_error_invalid
,
2670 "unaligned unnamed parameters", goto error
);
2671 pwaff1
= isl_pw_aff_align_params(pwaff1
, isl_pw_aff_get_space(pwaff2
));
2672 pwaff2
= isl_pw_aff_align_params(pwaff2
, isl_pw_aff_get_space(pwaff1
));
2673 return fn(pwaff1
, pwaff2
);
2675 isl_pw_aff_free(pwaff1
);
2676 isl_pw_aff_free(pwaff2
);
2680 /* Align the parameters of the to isl_pw_aff arguments and
2681 * then apply a function "fn" on them that returns an isl_map.
2683 static __isl_give isl_map
*align_params_pw_pw_map_and(
2684 __isl_take isl_pw_aff
*pa1
, __isl_take isl_pw_aff
*pa2
,
2685 __isl_give isl_map
*(*fn
)(__isl_take isl_pw_aff
*pa1
,
2686 __isl_take isl_pw_aff
*pa2
))
2688 isl_bool equal_params
;
2692 equal_params
= isl_space_has_equal_params(pa1
->dim
, pa2
->dim
);
2693 if (equal_params
< 0)
2696 return fn(pa1
, pa2
);
2697 if (!isl_space_has_named_params(pa1
->dim
) ||
2698 !isl_space_has_named_params(pa2
->dim
))
2699 isl_die(isl_pw_aff_get_ctx(pa1
), isl_error_invalid
,
2700 "unaligned unnamed parameters", goto error
);
2701 pa1
= isl_pw_aff_align_params(pa1
, isl_pw_aff_get_space(pa2
));
2702 pa2
= isl_pw_aff_align_params(pa2
, isl_pw_aff_get_space(pa1
));
2703 return fn(pa1
, pa2
);
2705 isl_pw_aff_free(pa1
);
2706 isl_pw_aff_free(pa2
);
2710 /* Compute a piecewise quasi-affine expression with a domain that
2711 * is the union of those of pwaff1 and pwaff2 and such that on each
2712 * cell, the quasi-affine expression is the maximum of those of pwaff1
2713 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2714 * cell, then the associated expression is the defined one.
2716 static __isl_give isl_pw_aff
*pw_aff_union_max(__isl_take isl_pw_aff
*pwaff1
,
2717 __isl_take isl_pw_aff
*pwaff2
)
2719 return isl_pw_aff_union_opt_cmp(pwaff1
, pwaff2
, &isl_aff_ge_set
);
2722 __isl_give isl_pw_aff
*isl_pw_aff_union_max(__isl_take isl_pw_aff
*pwaff1
,
2723 __isl_take isl_pw_aff
*pwaff2
)
2725 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
,
2729 /* Compute a piecewise quasi-affine expression with a domain that
2730 * is the union of those of pwaff1 and pwaff2 and such that on each
2731 * cell, the quasi-affine expression is the minimum of those of pwaff1
2732 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2733 * cell, then the associated expression is the defined one.
2735 static __isl_give isl_pw_aff
*pw_aff_union_min(__isl_take isl_pw_aff
*pwaff1
,
2736 __isl_take isl_pw_aff
*pwaff2
)
2738 return isl_pw_aff_union_opt_cmp(pwaff1
, pwaff2
, &isl_aff_le_set
);
2741 __isl_give isl_pw_aff
*isl_pw_aff_union_min(__isl_take isl_pw_aff
*pwaff1
,
2742 __isl_take isl_pw_aff
*pwaff2
)
2744 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
,
2748 __isl_give isl_pw_aff
*isl_pw_aff_union_opt(__isl_take isl_pw_aff
*pwaff1
,
2749 __isl_take isl_pw_aff
*pwaff2
, int max
)
2752 return isl_pw_aff_union_max(pwaff1
, pwaff2
);
2754 return isl_pw_aff_union_min(pwaff1
, pwaff2
);
2757 /* Construct a map with as domain the domain of pwaff and
2758 * one-dimensional range corresponding to the affine expressions.
2760 static __isl_give isl_map
*map_from_pw_aff(__isl_take isl_pw_aff
*pwaff
)
2769 dim
= isl_pw_aff_get_space(pwaff
);
2770 map
= isl_map_empty(dim
);
2772 for (i
= 0; i
< pwaff
->n
; ++i
) {
2773 isl_basic_map
*bmap
;
2776 bmap
= isl_basic_map_from_aff(isl_aff_copy(pwaff
->p
[i
].aff
));
2777 map_i
= isl_map_from_basic_map(bmap
);
2778 map_i
= isl_map_intersect_domain(map_i
,
2779 isl_set_copy(pwaff
->p
[i
].set
));
2780 map
= isl_map_union_disjoint(map
, map_i
);
2783 isl_pw_aff_free(pwaff
);
2788 /* Construct a map with as domain the domain of pwaff and
2789 * one-dimensional range corresponding to the affine expressions.
2791 __isl_give isl_map
*isl_map_from_pw_aff(__isl_take isl_pw_aff
*pwaff
)
2795 if (isl_space_is_set(pwaff
->dim
))
2796 isl_die(isl_pw_aff_get_ctx(pwaff
), isl_error_invalid
,
2797 "space of input is not a map", goto error
);
2798 return map_from_pw_aff(pwaff
);
2800 isl_pw_aff_free(pwaff
);
2804 /* Construct a one-dimensional set with as parameter domain
2805 * the domain of pwaff and the single set dimension
2806 * corresponding to the affine expressions.
2808 __isl_give isl_set
*isl_set_from_pw_aff(__isl_take isl_pw_aff
*pwaff
)
2812 if (!isl_space_is_set(pwaff
->dim
))
2813 isl_die(isl_pw_aff_get_ctx(pwaff
), isl_error_invalid
,
2814 "space of input is not a set", goto error
);
2815 return map_from_pw_aff(pwaff
);
2817 isl_pw_aff_free(pwaff
);
2821 /* Return a set containing those elements in the domain
2822 * of "pwaff" where it satisfies "fn" (if complement is 0) or
2823 * does not satisfy "fn" (if complement is 1).
2825 * The pieces with a NaN never belong to the result since
2826 * NaN does not satisfy any property.
2828 static __isl_give isl_set
*pw_aff_locus(__isl_take isl_pw_aff
*pwaff
,
2829 __isl_give isl_basic_set
*(*fn
)(__isl_take isl_aff
*aff
, int rational
),
2838 set
= isl_set_empty(isl_pw_aff_get_domain_space(pwaff
));
2840 for (i
= 0; i
< pwaff
->n
; ++i
) {
2841 isl_basic_set
*bset
;
2842 isl_set
*set_i
, *locus
;
2845 if (isl_aff_is_nan(pwaff
->p
[i
].aff
))
2848 rational
= isl_set_has_rational(pwaff
->p
[i
].set
);
2849 bset
= fn(isl_aff_copy(pwaff
->p
[i
].aff
), rational
);
2850 locus
= isl_set_from_basic_set(bset
);
2851 set_i
= isl_set_copy(pwaff
->p
[i
].set
);
2853 set_i
= isl_set_subtract(set_i
, locus
);
2855 set_i
= isl_set_intersect(set_i
, locus
);
2856 set
= isl_set_union_disjoint(set
, set_i
);
2859 isl_pw_aff_free(pwaff
);
2864 /* Return a set containing those elements in the domain
2865 * of "pa" where it is positive.
2867 __isl_give isl_set
*isl_pw_aff_pos_set(__isl_take isl_pw_aff
*pa
)
2869 return pw_aff_locus(pa
, &aff_pos_basic_set
, 0);
2872 /* Return a set containing those elements in the domain
2873 * of pwaff where it is non-negative.
2875 __isl_give isl_set
*isl_pw_aff_nonneg_set(__isl_take isl_pw_aff
*pwaff
)
2877 return pw_aff_locus(pwaff
, &aff_nonneg_basic_set
, 0);
2880 /* Return a set containing those elements in the domain
2881 * of pwaff where it is zero.
2883 __isl_give isl_set
*isl_pw_aff_zero_set(__isl_take isl_pw_aff
*pwaff
)
2885 return pw_aff_locus(pwaff
, &aff_zero_basic_set
, 0);
2888 /* Return a set containing those elements in the domain
2889 * of pwaff where it is not zero.
2891 __isl_give isl_set
*isl_pw_aff_non_zero_set(__isl_take isl_pw_aff
*pwaff
)
2893 return pw_aff_locus(pwaff
, &aff_zero_basic_set
, 1);
2896 /* Return a set containing those elements in the shared domain
2897 * of pwaff1 and pwaff2 where pwaff1 is greater than (or equal) to pwaff2.
2899 * We compute the difference on the shared domain and then construct
2900 * the set of values where this difference is non-negative.
2901 * If strict is set, we first subtract 1 from the difference.
2902 * If equal is set, we only return the elements where pwaff1 and pwaff2
2905 static __isl_give isl_set
*pw_aff_gte_set(__isl_take isl_pw_aff
*pwaff1
,
2906 __isl_take isl_pw_aff
*pwaff2
, int strict
, int equal
)
2908 isl_set
*set1
, *set2
;
2910 set1
= isl_pw_aff_domain(isl_pw_aff_copy(pwaff1
));
2911 set2
= isl_pw_aff_domain(isl_pw_aff_copy(pwaff2
));
2912 set1
= isl_set_intersect(set1
, set2
);
2913 pwaff1
= isl_pw_aff_intersect_domain(pwaff1
, isl_set_copy(set1
));
2914 pwaff2
= isl_pw_aff_intersect_domain(pwaff2
, isl_set_copy(set1
));
2915 pwaff1
= isl_pw_aff_add(pwaff1
, isl_pw_aff_neg(pwaff2
));
2918 isl_space
*dim
= isl_set_get_space(set1
);
2920 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(dim
));
2921 aff
= isl_aff_add_constant_si(aff
, -1);
2922 pwaff1
= isl_pw_aff_add(pwaff1
, isl_pw_aff_alloc(set1
, aff
));
2927 return isl_pw_aff_zero_set(pwaff1
);
2928 return isl_pw_aff_nonneg_set(pwaff1
);
2931 /* Return a set containing those elements in the shared domain
2932 * of pwaff1 and pwaff2 where pwaff1 is equal to pwaff2.
2934 static __isl_give isl_set
*pw_aff_eq_set(__isl_take isl_pw_aff
*pwaff1
,
2935 __isl_take isl_pw_aff
*pwaff2
)
2937 return pw_aff_gte_set(pwaff1
, pwaff2
, 0, 1);
2940 __isl_give isl_set
*isl_pw_aff_eq_set(__isl_take isl_pw_aff
*pwaff1
,
2941 __isl_take isl_pw_aff
*pwaff2
)
2943 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_eq_set
);
2946 /* Return a set containing those elements in the shared domain
2947 * of pwaff1 and pwaff2 where pwaff1 is greater than or equal to pwaff2.
2949 static __isl_give isl_set
*pw_aff_ge_set(__isl_take isl_pw_aff
*pwaff1
,
2950 __isl_take isl_pw_aff
*pwaff2
)
2952 return pw_aff_gte_set(pwaff1
, pwaff2
, 0, 0);
2955 __isl_give isl_set
*isl_pw_aff_ge_set(__isl_take isl_pw_aff
*pwaff1
,
2956 __isl_take isl_pw_aff
*pwaff2
)
2958 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_ge_set
);
2961 /* Return a set containing those elements in the shared domain
2962 * of pwaff1 and pwaff2 where pwaff1 is strictly greater than pwaff2.
2964 static __isl_give isl_set
*pw_aff_gt_set(__isl_take isl_pw_aff
*pwaff1
,
2965 __isl_take isl_pw_aff
*pwaff2
)
2967 return pw_aff_gte_set(pwaff1
, pwaff2
, 1, 0);
2970 __isl_give isl_set
*isl_pw_aff_gt_set(__isl_take isl_pw_aff
*pwaff1
,
2971 __isl_take isl_pw_aff
*pwaff2
)
2973 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_gt_set
);
2976 __isl_give isl_set
*isl_pw_aff_le_set(__isl_take isl_pw_aff
*pwaff1
,
2977 __isl_take isl_pw_aff
*pwaff2
)
2979 return isl_pw_aff_ge_set(pwaff2
, pwaff1
);
2982 __isl_give isl_set
*isl_pw_aff_lt_set(__isl_take isl_pw_aff
*pwaff1
,
2983 __isl_take isl_pw_aff
*pwaff2
)
2985 return isl_pw_aff_gt_set(pwaff2
, pwaff1
);
2988 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
2989 * where the function values are ordered in the same way as "order",
2990 * which returns a set in the shared domain of its two arguments.
2991 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
2993 * Let "pa1" and "pa2" be defined on domains A and B respectively.
2994 * We first pull back the two functions such that they are defined on
2995 * the domain [A -> B]. Then we apply "order", resulting in a set
2996 * in the space [A -> B]. Finally, we unwrap this set to obtain
2997 * a map in the space A -> B.
2999 static __isl_give isl_map
*isl_pw_aff_order_map_aligned(
3000 __isl_take isl_pw_aff
*pa1
, __isl_take isl_pw_aff
*pa2
,
3001 __isl_give isl_set
*(*order
)(__isl_take isl_pw_aff
*pa1
,
3002 __isl_take isl_pw_aff
*pa2
))
3004 isl_space
*space1
, *space2
;
3008 space1
= isl_space_domain(isl_pw_aff_get_space(pa1
));
3009 space2
= isl_space_domain(isl_pw_aff_get_space(pa2
));
3010 space1
= isl_space_map_from_domain_and_range(space1
, space2
);
3011 ma
= isl_multi_aff_domain_map(isl_space_copy(space1
));
3012 pa1
= isl_pw_aff_pullback_multi_aff(pa1
, ma
);
3013 ma
= isl_multi_aff_range_map(space1
);
3014 pa2
= isl_pw_aff_pullback_multi_aff(pa2
, ma
);
3015 set
= order(pa1
, pa2
);
3017 return isl_set_unwrap(set
);
3020 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3021 * where the function values are equal.
3022 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3024 static __isl_give isl_map
*isl_pw_aff_eq_map_aligned(__isl_take isl_pw_aff
*pa1
,
3025 __isl_take isl_pw_aff
*pa2
)
3027 return isl_pw_aff_order_map_aligned(pa1
, pa2
, &isl_pw_aff_eq_set
);
3030 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3031 * where the function values are equal.
3033 __isl_give isl_map
*isl_pw_aff_eq_map(__isl_take isl_pw_aff
*pa1
,
3034 __isl_take isl_pw_aff
*pa2
)
3036 return align_params_pw_pw_map_and(pa1
, pa2
, &isl_pw_aff_eq_map_aligned
);
3039 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3040 * where the function value of "pa1" is less than the function value of "pa2".
3041 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3043 static __isl_give isl_map
*isl_pw_aff_lt_map_aligned(__isl_take isl_pw_aff
*pa1
,
3044 __isl_take isl_pw_aff
*pa2
)
3046 return isl_pw_aff_order_map_aligned(pa1
, pa2
, &isl_pw_aff_lt_set
);
3049 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3050 * where the function value of "pa1" is less than the function value of "pa2".
3052 __isl_give isl_map
*isl_pw_aff_lt_map(__isl_take isl_pw_aff
*pa1
,
3053 __isl_take isl_pw_aff
*pa2
)
3055 return align_params_pw_pw_map_and(pa1
, pa2
, &isl_pw_aff_lt_map_aligned
);
3058 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3059 * where the function value of "pa1" is greater than the function value
3061 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3063 static __isl_give isl_map
*isl_pw_aff_gt_map_aligned(__isl_take isl_pw_aff
*pa1
,
3064 __isl_take isl_pw_aff
*pa2
)
3066 return isl_pw_aff_order_map_aligned(pa1
, pa2
, &isl_pw_aff_gt_set
);
3069 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3070 * where the function value of "pa1" is greater than the function value
3073 __isl_give isl_map
*isl_pw_aff_gt_map(__isl_take isl_pw_aff
*pa1
,
3074 __isl_take isl_pw_aff
*pa2
)
3076 return align_params_pw_pw_map_and(pa1
, pa2
, &isl_pw_aff_gt_map_aligned
);
3079 /* Return a set containing those elements in the shared domain
3080 * of the elements of list1 and list2 where each element in list1
3081 * has the relation specified by "fn" with each element in list2.
3083 static __isl_give isl_set
*pw_aff_list_set(__isl_take isl_pw_aff_list
*list1
,
3084 __isl_take isl_pw_aff_list
*list2
,
3085 __isl_give isl_set
*(*fn
)(__isl_take isl_pw_aff
*pwaff1
,
3086 __isl_take isl_pw_aff
*pwaff2
))
3092 if (!list1
|| !list2
)
3095 ctx
= isl_pw_aff_list_get_ctx(list1
);
3096 if (list1
->n
< 1 || list2
->n
< 1)
3097 isl_die(ctx
, isl_error_invalid
,
3098 "list should contain at least one element", goto error
);
3100 set
= isl_set_universe(isl_pw_aff_get_domain_space(list1
->p
[0]));
3101 for (i
= 0; i
< list1
->n
; ++i
)
3102 for (j
= 0; j
< list2
->n
; ++j
) {
3105 set_ij
= fn(isl_pw_aff_copy(list1
->p
[i
]),
3106 isl_pw_aff_copy(list2
->p
[j
]));
3107 set
= isl_set_intersect(set
, set_ij
);
3110 isl_pw_aff_list_free(list1
);
3111 isl_pw_aff_list_free(list2
);
3114 isl_pw_aff_list_free(list1
);
3115 isl_pw_aff_list_free(list2
);
3119 /* Return a set containing those elements in the shared domain
3120 * of the elements of list1 and list2 where each element in list1
3121 * is equal to each element in list2.
3123 __isl_give isl_set
*isl_pw_aff_list_eq_set(__isl_take isl_pw_aff_list
*list1
,
3124 __isl_take isl_pw_aff_list
*list2
)
3126 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_eq_set
);
3129 __isl_give isl_set
*isl_pw_aff_list_ne_set(__isl_take isl_pw_aff_list
*list1
,
3130 __isl_take isl_pw_aff_list
*list2
)
3132 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_ne_set
);
3135 /* Return a set containing those elements in the shared domain
3136 * of the elements of list1 and list2 where each element in list1
3137 * is less than or equal to each element in list2.
3139 __isl_give isl_set
*isl_pw_aff_list_le_set(__isl_take isl_pw_aff_list
*list1
,
3140 __isl_take isl_pw_aff_list
*list2
)
3142 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_le_set
);
3145 __isl_give isl_set
*isl_pw_aff_list_lt_set(__isl_take isl_pw_aff_list
*list1
,
3146 __isl_take isl_pw_aff_list
*list2
)
3148 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_lt_set
);
3151 __isl_give isl_set
*isl_pw_aff_list_ge_set(__isl_take isl_pw_aff_list
*list1
,
3152 __isl_take isl_pw_aff_list
*list2
)
3154 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_ge_set
);
3157 __isl_give isl_set
*isl_pw_aff_list_gt_set(__isl_take isl_pw_aff_list
*list1
,
3158 __isl_take isl_pw_aff_list
*list2
)
3160 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_gt_set
);
3164 /* Return a set containing those elements in the shared domain
3165 * of pwaff1 and pwaff2 where pwaff1 is not equal to pwaff2.
3167 static __isl_give isl_set
*pw_aff_ne_set(__isl_take isl_pw_aff
*pwaff1
,
3168 __isl_take isl_pw_aff
*pwaff2
)
3170 isl_set
*set_lt
, *set_gt
;
3172 set_lt
= isl_pw_aff_lt_set(isl_pw_aff_copy(pwaff1
),
3173 isl_pw_aff_copy(pwaff2
));
3174 set_gt
= isl_pw_aff_gt_set(pwaff1
, pwaff2
);
3175 return isl_set_union_disjoint(set_lt
, set_gt
);
3178 __isl_give isl_set
*isl_pw_aff_ne_set(__isl_take isl_pw_aff
*pwaff1
,
3179 __isl_take isl_pw_aff
*pwaff2
)
3181 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_ne_set
);
3184 __isl_give isl_pw_aff
*isl_pw_aff_scale_down(__isl_take isl_pw_aff
*pwaff
,
3189 if (isl_int_is_one(v
))
3191 if (!isl_int_is_pos(v
))
3192 isl_die(isl_pw_aff_get_ctx(pwaff
), isl_error_invalid
,
3193 "factor needs to be positive",
3194 return isl_pw_aff_free(pwaff
));
3195 pwaff
= isl_pw_aff_cow(pwaff
);
3201 for (i
= 0; i
< pwaff
->n
; ++i
) {
3202 pwaff
->p
[i
].aff
= isl_aff_scale_down(pwaff
->p
[i
].aff
, v
);
3203 if (!pwaff
->p
[i
].aff
)
3204 return isl_pw_aff_free(pwaff
);
3210 __isl_give isl_pw_aff
*isl_pw_aff_floor(__isl_take isl_pw_aff
*pwaff
)
3214 pwaff
= isl_pw_aff_cow(pwaff
);
3220 for (i
= 0; i
< pwaff
->n
; ++i
) {
3221 pwaff
->p
[i
].aff
= isl_aff_floor(pwaff
->p
[i
].aff
);
3222 if (!pwaff
->p
[i
].aff
)
3223 return isl_pw_aff_free(pwaff
);
3229 __isl_give isl_pw_aff
*isl_pw_aff_ceil(__isl_take isl_pw_aff
*pwaff
)
3233 pwaff
= isl_pw_aff_cow(pwaff
);
3239 for (i
= 0; i
< pwaff
->n
; ++i
) {
3240 pwaff
->p
[i
].aff
= isl_aff_ceil(pwaff
->p
[i
].aff
);
3241 if (!pwaff
->p
[i
].aff
)
3242 return isl_pw_aff_free(pwaff
);
3248 /* Assuming that "cond1" and "cond2" are disjoint,
3249 * return an affine expression that is equal to pwaff1 on cond1
3250 * and to pwaff2 on cond2.
3252 static __isl_give isl_pw_aff
*isl_pw_aff_select(
3253 __isl_take isl_set
*cond1
, __isl_take isl_pw_aff
*pwaff1
,
3254 __isl_take isl_set
*cond2
, __isl_take isl_pw_aff
*pwaff2
)
3256 pwaff1
= isl_pw_aff_intersect_domain(pwaff1
, cond1
);
3257 pwaff2
= isl_pw_aff_intersect_domain(pwaff2
, cond2
);
3259 return isl_pw_aff_add_disjoint(pwaff1
, pwaff2
);
3262 /* Return an affine expression that is equal to pwaff_true for elements
3263 * where "cond" is non-zero and to pwaff_false for elements where "cond"
3265 * That is, return cond ? pwaff_true : pwaff_false;
3267 * If "cond" involves and NaN, then we conservatively return a NaN
3268 * on its entire domain. In principle, we could consider the pieces
3269 * where it is NaN separately from those where it is not.
3271 * If "pwaff_true" and "pwaff_false" are obviously equal to each other,
3272 * then only use the domain of "cond" to restrict the domain.
3274 __isl_give isl_pw_aff
*isl_pw_aff_cond(__isl_take isl_pw_aff
*cond
,
3275 __isl_take isl_pw_aff
*pwaff_true
, __isl_take isl_pw_aff
*pwaff_false
)
3277 isl_set
*cond_true
, *cond_false
;
3282 if (isl_pw_aff_involves_nan(cond
)) {
3283 isl_space
*space
= isl_pw_aff_get_domain_space(cond
);
3284 isl_local_space
*ls
= isl_local_space_from_space(space
);
3285 isl_pw_aff_free(cond
);
3286 isl_pw_aff_free(pwaff_true
);
3287 isl_pw_aff_free(pwaff_false
);
3288 return isl_pw_aff_nan_on_domain(ls
);
3291 pwaff_true
= isl_pw_aff_align_params(pwaff_true
,
3292 isl_pw_aff_get_space(pwaff_false
));
3293 pwaff_false
= isl_pw_aff_align_params(pwaff_false
,
3294 isl_pw_aff_get_space(pwaff_true
));
3295 equal
= isl_pw_aff_plain_is_equal(pwaff_true
, pwaff_false
);
3301 dom
= isl_set_coalesce(isl_pw_aff_domain(cond
));
3302 isl_pw_aff_free(pwaff_false
);
3303 return isl_pw_aff_intersect_domain(pwaff_true
, dom
);
3306 cond_true
= isl_pw_aff_non_zero_set(isl_pw_aff_copy(cond
));
3307 cond_false
= isl_pw_aff_zero_set(cond
);
3308 return isl_pw_aff_select(cond_true
, pwaff_true
,
3309 cond_false
, pwaff_false
);
3311 isl_pw_aff_free(cond
);
3312 isl_pw_aff_free(pwaff_true
);
3313 isl_pw_aff_free(pwaff_false
);
3317 isl_bool
isl_aff_is_cst(__isl_keep isl_aff
*aff
)
3320 return isl_bool_error
;
3322 return isl_seq_first_non_zero(aff
->v
->el
+ 2, aff
->v
->size
- 2) == -1;
3325 /* Check whether pwaff is a piecewise constant.
3327 isl_bool
isl_pw_aff_is_cst(__isl_keep isl_pw_aff
*pwaff
)
3332 return isl_bool_error
;
3334 for (i
= 0; i
< pwaff
->n
; ++i
) {
3335 isl_bool is_cst
= isl_aff_is_cst(pwaff
->p
[i
].aff
);
3336 if (is_cst
< 0 || !is_cst
)
3340 return isl_bool_true
;
3343 /* Are all elements of "mpa" piecewise constants?
3345 isl_bool
isl_multi_pw_aff_is_cst(__isl_keep isl_multi_pw_aff
*mpa
)
3350 return isl_bool_error
;
3352 for (i
= 0; i
< mpa
->n
; ++i
) {
3353 isl_bool is_cst
= isl_pw_aff_is_cst(mpa
->p
[i
]);
3354 if (is_cst
< 0 || !is_cst
)
3358 return isl_bool_true
;
3361 /* Return the product of "aff1" and "aff2".
3363 * If either of the two is NaN, then the result is NaN.
3365 * Otherwise, at least one of "aff1" or "aff2" needs to be a constant.
3367 __isl_give isl_aff
*isl_aff_mul(__isl_take isl_aff
*aff1
,
3368 __isl_take isl_aff
*aff2
)
3373 if (isl_aff_is_nan(aff1
)) {
3377 if (isl_aff_is_nan(aff2
)) {
3382 if (!isl_aff_is_cst(aff2
) && isl_aff_is_cst(aff1
))
3383 return isl_aff_mul(aff2
, aff1
);
3385 if (!isl_aff_is_cst(aff2
))
3386 isl_die(isl_aff_get_ctx(aff1
), isl_error_invalid
,
3387 "at least one affine expression should be constant",
3390 aff1
= isl_aff_cow(aff1
);
3394 aff1
= isl_aff_scale(aff1
, aff2
->v
->el
[1]);
3395 aff1
= isl_aff_scale_down(aff1
, aff2
->v
->el
[0]);
3405 /* Divide "aff1" by "aff2", assuming "aff2" is a constant.
3407 * If either of the two is NaN, then the result is NaN.
3409 __isl_give isl_aff
*isl_aff_div(__isl_take isl_aff
*aff1
,
3410 __isl_take isl_aff
*aff2
)
3418 if (isl_aff_is_nan(aff1
)) {
3422 if (isl_aff_is_nan(aff2
)) {
3427 is_cst
= isl_aff_is_cst(aff2
);
3431 isl_die(isl_aff_get_ctx(aff2
), isl_error_invalid
,
3432 "second argument should be a constant", goto error
);
3437 neg
= isl_int_is_neg(aff2
->v
->el
[1]);
3439 isl_int_neg(aff2
->v
->el
[0], aff2
->v
->el
[0]);
3440 isl_int_neg(aff2
->v
->el
[1], aff2
->v
->el
[1]);
3443 aff1
= isl_aff_scale(aff1
, aff2
->v
->el
[0]);
3444 aff1
= isl_aff_scale_down(aff1
, aff2
->v
->el
[1]);
3447 isl_int_neg(aff2
->v
->el
[0], aff2
->v
->el
[0]);
3448 isl_int_neg(aff2
->v
->el
[1], aff2
->v
->el
[1]);
3459 static __isl_give isl_pw_aff
*pw_aff_add(__isl_take isl_pw_aff
*pwaff1
,
3460 __isl_take isl_pw_aff
*pwaff2
)
3462 return isl_pw_aff_on_shared_domain(pwaff1
, pwaff2
, &isl_aff_add
);
3465 __isl_give isl_pw_aff
*isl_pw_aff_add(__isl_take isl_pw_aff
*pwaff1
,
3466 __isl_take isl_pw_aff
*pwaff2
)
3468 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
, &pw_aff_add
);
3471 __isl_give isl_pw_aff
*isl_pw_aff_union_add(__isl_take isl_pw_aff
*pwaff1
,
3472 __isl_take isl_pw_aff
*pwaff2
)
3474 return isl_pw_aff_union_add_(pwaff1
, pwaff2
);
3477 static __isl_give isl_pw_aff
*pw_aff_mul(__isl_take isl_pw_aff
*pwaff1
,
3478 __isl_take isl_pw_aff
*pwaff2
)
3480 return isl_pw_aff_on_shared_domain(pwaff1
, pwaff2
, &isl_aff_mul
);
3483 __isl_give isl_pw_aff
*isl_pw_aff_mul(__isl_take isl_pw_aff
*pwaff1
,
3484 __isl_take isl_pw_aff
*pwaff2
)
3486 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
, &pw_aff_mul
);
3489 static __isl_give isl_pw_aff
*pw_aff_div(__isl_take isl_pw_aff
*pa1
,
3490 __isl_take isl_pw_aff
*pa2
)
3492 return isl_pw_aff_on_shared_domain(pa1
, pa2
, &isl_aff_div
);
3495 /* Divide "pa1" by "pa2", assuming "pa2" is a piecewise constant.
3497 __isl_give isl_pw_aff
*isl_pw_aff_div(__isl_take isl_pw_aff
*pa1
,
3498 __isl_take isl_pw_aff
*pa2
)
3502 is_cst
= isl_pw_aff_is_cst(pa2
);
3506 isl_die(isl_pw_aff_get_ctx(pa2
), isl_error_invalid
,
3507 "second argument should be a piecewise constant",
3509 return isl_pw_aff_align_params_pw_pw_and(pa1
, pa2
, &pw_aff_div
);
3511 isl_pw_aff_free(pa1
);
3512 isl_pw_aff_free(pa2
);
3516 /* Compute the quotient of the integer division of "pa1" by "pa2"
3517 * with rounding towards zero.
3518 * "pa2" is assumed to be a piecewise constant.
3520 * In particular, return
3522 * pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2)
3525 __isl_give isl_pw_aff
*isl_pw_aff_tdiv_q(__isl_take isl_pw_aff
*pa1
,
3526 __isl_take isl_pw_aff
*pa2
)
3532 is_cst
= isl_pw_aff_is_cst(pa2
);
3536 isl_die(isl_pw_aff_get_ctx(pa2
), isl_error_invalid
,
3537 "second argument should be a piecewise constant",
3540 pa1
= isl_pw_aff_div(pa1
, pa2
);
3542 cond
= isl_pw_aff_nonneg_set(isl_pw_aff_copy(pa1
));
3543 f
= isl_pw_aff_floor(isl_pw_aff_copy(pa1
));
3544 c
= isl_pw_aff_ceil(pa1
);
3545 return isl_pw_aff_cond(isl_set_indicator_function(cond
), f
, c
);
3547 isl_pw_aff_free(pa1
);
3548 isl_pw_aff_free(pa2
);
3552 /* Compute the remainder of the integer division of "pa1" by "pa2"
3553 * with rounding towards zero.
3554 * "pa2" is assumed to be a piecewise constant.
3556 * In particular, return
3558 * pa1 - pa2 * (pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2))
3561 __isl_give isl_pw_aff
*isl_pw_aff_tdiv_r(__isl_take isl_pw_aff
*pa1
,
3562 __isl_take isl_pw_aff
*pa2
)
3567 is_cst
= isl_pw_aff_is_cst(pa2
);
3571 isl_die(isl_pw_aff_get_ctx(pa2
), isl_error_invalid
,
3572 "second argument should be a piecewise constant",
3574 res
= isl_pw_aff_tdiv_q(isl_pw_aff_copy(pa1
), isl_pw_aff_copy(pa2
));
3575 res
= isl_pw_aff_mul(pa2
, res
);
3576 res
= isl_pw_aff_sub(pa1
, res
);
3579 isl_pw_aff_free(pa1
);
3580 isl_pw_aff_free(pa2
);
3584 /* Does either of "pa1" or "pa2" involve any NaN2?
3586 static isl_bool
either_involves_nan(__isl_keep isl_pw_aff
*pa1
,
3587 __isl_keep isl_pw_aff
*pa2
)
3591 has_nan
= isl_pw_aff_involves_nan(pa1
);
3592 if (has_nan
< 0 || has_nan
)
3594 return isl_pw_aff_involves_nan(pa2
);
3597 /* Replace "pa1" and "pa2" (at least one of which involves a NaN)
3598 * by a NaN on their shared domain.
3600 * In principle, the result could be refined to only being NaN
3601 * on the parts of this domain where at least one of "pa1" or "pa2" is NaN.
3603 static __isl_give isl_pw_aff
*replace_by_nan(__isl_take isl_pw_aff
*pa1
,
3604 __isl_take isl_pw_aff
*pa2
)
3606 isl_local_space
*ls
;
3610 dom
= isl_set_intersect(isl_pw_aff_domain(pa1
), isl_pw_aff_domain(pa2
));
3611 ls
= isl_local_space_from_space(isl_set_get_space(dom
));
3612 pa
= isl_pw_aff_nan_on_domain(ls
);
3613 pa
= isl_pw_aff_intersect_domain(pa
, dom
);
3618 static __isl_give isl_pw_aff
*pw_aff_min(__isl_take isl_pw_aff
*pwaff1
,
3619 __isl_take isl_pw_aff
*pwaff2
)
3624 dom
= isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1
)),
3625 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2
)));
3626 le
= isl_pw_aff_le_set(isl_pw_aff_copy(pwaff1
),
3627 isl_pw_aff_copy(pwaff2
));
3628 dom
= isl_set_subtract(dom
, isl_set_copy(le
));
3629 return isl_pw_aff_select(le
, pwaff1
, dom
, pwaff2
);
3632 static __isl_give isl_pw_aff
*pw_aff_max(__isl_take isl_pw_aff
*pwaff1
,
3633 __isl_take isl_pw_aff
*pwaff2
)
3638 dom
= isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1
)),
3639 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2
)));
3640 ge
= isl_pw_aff_ge_set(isl_pw_aff_copy(pwaff1
),
3641 isl_pw_aff_copy(pwaff2
));
3642 dom
= isl_set_subtract(dom
, isl_set_copy(ge
));
3643 return isl_pw_aff_select(ge
, pwaff1
, dom
, pwaff2
);
3646 /* Return an expression for the minimum (if "max" is not set) or
3647 * the maximum (if "max" is set) of "pa1" and "pa2".
3648 * If either expression involves any NaN, then return a NaN
3649 * on the shared domain as result.
3651 static __isl_give isl_pw_aff
*pw_aff_min_max(__isl_take isl_pw_aff
*pa1
,
3652 __isl_take isl_pw_aff
*pa2
, int max
)
3656 has_nan
= either_involves_nan(pa1
, pa2
);
3658 pa1
= isl_pw_aff_free(pa1
);
3660 return replace_by_nan(pa1
, pa2
);
3663 return isl_pw_aff_align_params_pw_pw_and(pa1
, pa2
, &pw_aff_max
);
3665 return isl_pw_aff_align_params_pw_pw_and(pa1
, pa2
, &pw_aff_min
);
3668 /* Return an expression for the minimum of "pwaff1" and "pwaff2".
3670 __isl_give isl_pw_aff
*isl_pw_aff_min(__isl_take isl_pw_aff
*pwaff1
,
3671 __isl_take isl_pw_aff
*pwaff2
)
3673 return pw_aff_min_max(pwaff1
, pwaff2
, 0);
3676 /* Return an expression for the maximum of "pwaff1" and "pwaff2".
3678 __isl_give isl_pw_aff
*isl_pw_aff_max(__isl_take isl_pw_aff
*pwaff1
,
3679 __isl_take isl_pw_aff
*pwaff2
)
3681 return pw_aff_min_max(pwaff1
, pwaff2
, 1);
3684 static __isl_give isl_pw_aff
*pw_aff_list_reduce(
3685 __isl_take isl_pw_aff_list
*list
,
3686 __isl_give isl_pw_aff
*(*fn
)(__isl_take isl_pw_aff
*pwaff1
,
3687 __isl_take isl_pw_aff
*pwaff2
))
3696 ctx
= isl_pw_aff_list_get_ctx(list
);
3698 isl_die(ctx
, isl_error_invalid
,
3699 "list should contain at least one element", goto error
);
3701 res
= isl_pw_aff_copy(list
->p
[0]);
3702 for (i
= 1; i
< list
->n
; ++i
)
3703 res
= fn(res
, isl_pw_aff_copy(list
->p
[i
]));
3705 isl_pw_aff_list_free(list
);
3708 isl_pw_aff_list_free(list
);
3712 /* Return an isl_pw_aff that maps each element in the intersection of the
3713 * domains of the elements of list to the minimal corresponding affine
3716 __isl_give isl_pw_aff
*isl_pw_aff_list_min(__isl_take isl_pw_aff_list
*list
)
3718 return pw_aff_list_reduce(list
, &isl_pw_aff_min
);
3721 /* Return an isl_pw_aff that maps each element in the intersection of the
3722 * domains of the elements of list to the maximal corresponding affine
3725 __isl_give isl_pw_aff
*isl_pw_aff_list_max(__isl_take isl_pw_aff_list
*list
)
3727 return pw_aff_list_reduce(list
, &isl_pw_aff_max
);
3730 /* Mark the domains of "pwaff" as rational.
3732 __isl_give isl_pw_aff
*isl_pw_aff_set_rational(__isl_take isl_pw_aff
*pwaff
)
3736 pwaff
= isl_pw_aff_cow(pwaff
);
3742 for (i
= 0; i
< pwaff
->n
; ++i
) {
3743 pwaff
->p
[i
].set
= isl_set_set_rational(pwaff
->p
[i
].set
);
3744 if (!pwaff
->p
[i
].set
)
3745 return isl_pw_aff_free(pwaff
);
3751 /* Mark the domains of the elements of "list" as rational.
3753 __isl_give isl_pw_aff_list
*isl_pw_aff_list_set_rational(
3754 __isl_take isl_pw_aff_list
*list
)
3764 for (i
= 0; i
< n
; ++i
) {
3767 pa
= isl_pw_aff_list_get_pw_aff(list
, i
);
3768 pa
= isl_pw_aff_set_rational(pa
);
3769 list
= isl_pw_aff_list_set_pw_aff(list
, i
, pa
);
3775 /* Do the parameters of "aff" match those of "space"?
3777 isl_bool
isl_aff_matching_params(__isl_keep isl_aff
*aff
,
3778 __isl_keep isl_space
*space
)
3780 isl_space
*aff_space
;
3784 return isl_bool_error
;
3786 aff_space
= isl_aff_get_domain_space(aff
);
3788 match
= isl_space_has_equal_params(space
, aff_space
);
3790 isl_space_free(aff_space
);
3794 /* Check that the domain space of "aff" matches "space".
3796 isl_stat
isl_aff_check_match_domain_space(__isl_keep isl_aff
*aff
,
3797 __isl_keep isl_space
*space
)
3799 isl_space
*aff_space
;
3803 return isl_stat_error
;
3805 aff_space
= isl_aff_get_domain_space(aff
);
3807 match
= isl_space_has_equal_params(space
, aff_space
);
3811 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
3812 "parameters don't match", goto error
);
3813 match
= isl_space_tuple_is_equal(space
, isl_dim_in
,
3814 aff_space
, isl_dim_set
);
3818 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
3819 "domains don't match", goto error
);
3820 isl_space_free(aff_space
);
3823 isl_space_free(aff_space
);
3824 return isl_stat_error
;
3833 #include <isl_multi_templ.c>
3834 #include <isl_multi_apply_set.c>
3835 #include <isl_multi_cmp.c>
3836 #include <isl_multi_dims.c>
3837 #include <isl_multi_floor.c>
3838 #include <isl_multi_gist.c>
3842 /* Construct an isl_multi_aff living in "space" that corresponds
3843 * to the affine transformation matrix "mat".
3845 __isl_give isl_multi_aff
*isl_multi_aff_from_aff_mat(
3846 __isl_take isl_space
*space
, __isl_take isl_mat
*mat
)
3849 isl_local_space
*ls
= NULL
;
3850 isl_multi_aff
*ma
= NULL
;
3851 int n_row
, n_col
, n_out
, total
;
3857 ctx
= isl_mat_get_ctx(mat
);
3859 n_row
= isl_mat_rows(mat
);
3860 n_col
= isl_mat_cols(mat
);
3862 isl_die(ctx
, isl_error_invalid
,
3863 "insufficient number of rows", goto error
);
3865 isl_die(ctx
, isl_error_invalid
,
3866 "insufficient number of columns", goto error
);
3867 n_out
= isl_space_dim(space
, isl_dim_out
);
3868 total
= isl_space_dim(space
, isl_dim_all
);
3869 if (1 + n_out
!= n_row
|| 2 + total
!= n_row
+ n_col
)
3870 isl_die(ctx
, isl_error_invalid
,
3871 "dimension mismatch", goto error
);
3873 ma
= isl_multi_aff_zero(isl_space_copy(space
));
3874 ls
= isl_local_space_from_space(isl_space_domain(space
));
3876 for (i
= 0; i
< n_row
- 1; ++i
) {
3880 v
= isl_vec_alloc(ctx
, 1 + n_col
);
3883 isl_int_set(v
->el
[0], mat
->row
[0][0]);
3884 isl_seq_cpy(v
->el
+ 1, mat
->row
[1 + i
], n_col
);
3885 v
= isl_vec_normalize(v
);
3886 aff
= isl_aff_alloc_vec(isl_local_space_copy(ls
), v
);
3887 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
3890 isl_local_space_free(ls
);
3894 isl_local_space_free(ls
);
3896 isl_multi_aff_free(ma
);
3900 /* Remove any internal structure of the domain of "ma".
3901 * If there is any such internal structure in the input,
3902 * then the name of the corresponding space is also removed.
3904 __isl_give isl_multi_aff
*isl_multi_aff_flatten_domain(
3905 __isl_take isl_multi_aff
*ma
)
3912 if (!ma
->space
->nested
[0])
3915 space
= isl_multi_aff_get_space(ma
);
3916 space
= isl_space_flatten_domain(space
);
3917 ma
= isl_multi_aff_reset_space(ma
, space
);
3922 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
3923 * of the space to its domain.
3925 __isl_give isl_multi_aff
*isl_multi_aff_domain_map(__isl_take isl_space
*space
)
3928 isl_local_space
*ls
;
3933 if (!isl_space_is_map(space
))
3934 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
3935 "not a map space", goto error
);
3937 n_in
= isl_space_dim(space
, isl_dim_in
);
3938 space
= isl_space_domain_map(space
);
3940 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
3942 isl_space_free(space
);
3946 space
= isl_space_domain(space
);
3947 ls
= isl_local_space_from_space(space
);
3948 for (i
= 0; i
< n_in
; ++i
) {
3951 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
3953 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
3955 isl_local_space_free(ls
);
3958 isl_space_free(space
);
3962 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
3963 * of the space to its range.
3965 __isl_give isl_multi_aff
*isl_multi_aff_range_map(__isl_take isl_space
*space
)
3968 isl_local_space
*ls
;
3973 if (!isl_space_is_map(space
))
3974 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
3975 "not a map space", goto error
);
3977 n_in
= isl_space_dim(space
, isl_dim_in
);
3978 n_out
= isl_space_dim(space
, isl_dim_out
);
3979 space
= isl_space_range_map(space
);
3981 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
3983 isl_space_free(space
);
3987 space
= isl_space_domain(space
);
3988 ls
= isl_local_space_from_space(space
);
3989 for (i
= 0; i
< n_out
; ++i
) {
3992 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
3993 isl_dim_set
, n_in
+ i
);
3994 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
3996 isl_local_space_free(ls
);
3999 isl_space_free(space
);
4003 /* Given a map space, return an isl_pw_multi_aff that maps a wrapped copy
4004 * of the space to its range.
4006 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_range_map(
4007 __isl_take isl_space
*space
)
4009 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_range_map(space
));
4012 /* Given the space of a set and a range of set dimensions,
4013 * construct an isl_multi_aff that projects out those dimensions.
4015 __isl_give isl_multi_aff
*isl_multi_aff_project_out_map(
4016 __isl_take isl_space
*space
, enum isl_dim_type type
,
4017 unsigned first
, unsigned n
)
4020 isl_local_space
*ls
;
4025 if (!isl_space_is_set(space
))
4026 isl_die(isl_space_get_ctx(space
), isl_error_unsupported
,
4027 "expecting set space", goto error
);
4028 if (type
!= isl_dim_set
)
4029 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
4030 "only set dimensions can be projected out", goto error
);
4032 dim
= isl_space_dim(space
, isl_dim_set
);
4033 if (first
+ n
> dim
)
4034 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
4035 "range out of bounds", goto error
);
4037 space
= isl_space_from_domain(space
);
4038 space
= isl_space_add_dims(space
, isl_dim_out
, dim
- n
);
4041 return isl_multi_aff_alloc(space
);
4043 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
4044 space
= isl_space_domain(space
);
4045 ls
= isl_local_space_from_space(space
);
4047 for (i
= 0; i
< first
; ++i
) {
4050 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
4052 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
4055 for (i
= 0; i
< dim
- (first
+ n
); ++i
) {
4058 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
4059 isl_dim_set
, first
+ n
+ i
);
4060 ma
= isl_multi_aff_set_aff(ma
, first
+ i
, aff
);
4063 isl_local_space_free(ls
);
4066 isl_space_free(space
);
4070 /* Given the space of a set and a range of set dimensions,
4071 * construct an isl_pw_multi_aff that projects out those dimensions.
4073 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_project_out_map(
4074 __isl_take isl_space
*space
, enum isl_dim_type type
,
4075 unsigned first
, unsigned n
)
4079 ma
= isl_multi_aff_project_out_map(space
, type
, first
, n
);
4080 return isl_pw_multi_aff_from_multi_aff(ma
);
4083 /* Create an isl_pw_multi_aff with the given isl_multi_aff on a universe
4086 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_multi_aff(
4087 __isl_take isl_multi_aff
*ma
)
4089 isl_set
*dom
= isl_set_universe(isl_multi_aff_get_domain_space(ma
));
4090 return isl_pw_multi_aff_alloc(dom
, ma
);
4093 /* Create a piecewise multi-affine expression in the given space that maps each
4094 * input dimension to the corresponding output dimension.
4096 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_identity(
4097 __isl_take isl_space
*space
)
4099 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_identity(space
));
4102 /* Exploit the equalities in "eq" to simplify the affine expressions.
4104 static __isl_give isl_multi_aff
*isl_multi_aff_substitute_equalities(
4105 __isl_take isl_multi_aff
*maff
, __isl_take isl_basic_set
*eq
)
4109 maff
= isl_multi_aff_cow(maff
);
4113 for (i
= 0; i
< maff
->n
; ++i
) {
4114 maff
->p
[i
] = isl_aff_substitute_equalities(maff
->p
[i
],
4115 isl_basic_set_copy(eq
));
4120 isl_basic_set_free(eq
);
4123 isl_basic_set_free(eq
);
4124 isl_multi_aff_free(maff
);
4128 __isl_give isl_multi_aff
*isl_multi_aff_scale(__isl_take isl_multi_aff
*maff
,
4133 maff
= isl_multi_aff_cow(maff
);
4137 for (i
= 0; i
< maff
->n
; ++i
) {
4138 maff
->p
[i
] = isl_aff_scale(maff
->p
[i
], f
);
4140 return isl_multi_aff_free(maff
);
4146 __isl_give isl_multi_aff
*isl_multi_aff_add_on_domain(__isl_keep isl_set
*dom
,
4147 __isl_take isl_multi_aff
*maff1
, __isl_take isl_multi_aff
*maff2
)
4149 maff1
= isl_multi_aff_add(maff1
, maff2
);
4150 maff1
= isl_multi_aff_gist(maff1
, isl_set_copy(dom
));
4154 int isl_multi_aff_is_empty(__isl_keep isl_multi_aff
*maff
)
4162 /* Return the set of domain elements where "ma1" is lexicographically
4163 * smaller than or equal to "ma2".
4165 __isl_give isl_set
*isl_multi_aff_lex_le_set(__isl_take isl_multi_aff
*ma1
,
4166 __isl_take isl_multi_aff
*ma2
)
4168 return isl_multi_aff_lex_ge_set(ma2
, ma1
);
4171 /* Return the set of domain elements where "ma1" is lexicographically
4172 * smaller than "ma2".
4174 __isl_give isl_set
*isl_multi_aff_lex_lt_set(__isl_take isl_multi_aff
*ma1
,
4175 __isl_take isl_multi_aff
*ma2
)
4177 return isl_multi_aff_lex_gt_set(ma2
, ma1
);
4180 /* Return the set of domain elements where "ma1" and "ma2"
4183 static __isl_give isl_set
*isl_multi_aff_order_set(
4184 __isl_take isl_multi_aff
*ma1
, __isl_take isl_multi_aff
*ma2
,
4185 __isl_give isl_map
*order(__isl_take isl_space
*set_space
))
4188 isl_map
*map1
, *map2
;
4191 map1
= isl_map_from_multi_aff(ma1
);
4192 map2
= isl_map_from_multi_aff(ma2
);
4193 map
= isl_map_range_product(map1
, map2
);
4194 space
= isl_space_range(isl_map_get_space(map
));
4195 space
= isl_space_domain(isl_space_unwrap(space
));
4197 map
= isl_map_intersect_range(map
, isl_map_wrap(ge
));
4199 return isl_map_domain(map
);
4202 /* Return the set of domain elements where "ma1" is lexicographically
4203 * greater than or equal to "ma2".
4205 __isl_give isl_set
*isl_multi_aff_lex_ge_set(__isl_take isl_multi_aff
*ma1
,
4206 __isl_take isl_multi_aff
*ma2
)
4208 return isl_multi_aff_order_set(ma1
, ma2
, &isl_map_lex_ge
);
4211 /* Return the set of domain elements where "ma1" is lexicographically
4212 * greater than "ma2".
4214 __isl_give isl_set
*isl_multi_aff_lex_gt_set(__isl_take isl_multi_aff
*ma1
,
4215 __isl_take isl_multi_aff
*ma2
)
4217 return isl_multi_aff_order_set(ma1
, ma2
, &isl_map_lex_gt
);
4221 #define PW isl_pw_multi_aff
4223 #define EL isl_multi_aff
4225 #define EL_IS_ZERO is_empty
4229 #define IS_ZERO is_empty
4232 #undef DEFAULT_IS_ZERO
4233 #define DEFAULT_IS_ZERO 0
4238 #define NO_INVOLVES_DIMS
4239 #define NO_INSERT_DIMS
4243 #include <isl_pw_templ.c>
4244 #include <isl_pw_union_opt.c>
4249 #define UNION isl_union_pw_multi_aff
4251 #define PART isl_pw_multi_aff
4253 #define PARTS pw_multi_aff
4255 #include <isl_union_multi.c>
4256 #include <isl_union_neg.c>
4258 static __isl_give isl_pw_multi_aff
*pw_multi_aff_union_lexmax(
4259 __isl_take isl_pw_multi_aff
*pma1
,
4260 __isl_take isl_pw_multi_aff
*pma2
)
4262 return isl_pw_multi_aff_union_opt_cmp(pma1
, pma2
,
4263 &isl_multi_aff_lex_ge_set
);
4266 /* Given two piecewise multi affine expressions, return a piecewise
4267 * multi-affine expression defined on the union of the definition domains
4268 * of the inputs that is equal to the lexicographic maximum of the two
4269 * inputs on each cell. If only one of the two inputs is defined on
4270 * a given cell, then it is considered to be the maximum.
4272 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_union_lexmax(
4273 __isl_take isl_pw_multi_aff
*pma1
,
4274 __isl_take isl_pw_multi_aff
*pma2
)
4276 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
4277 &pw_multi_aff_union_lexmax
);
4280 static __isl_give isl_pw_multi_aff
*pw_multi_aff_union_lexmin(
4281 __isl_take isl_pw_multi_aff
*pma1
,
4282 __isl_take isl_pw_multi_aff
*pma2
)
4284 return isl_pw_multi_aff_union_opt_cmp(pma1
, pma2
,
4285 &isl_multi_aff_lex_le_set
);
4288 /* Given two piecewise multi affine expressions, return a piecewise
4289 * multi-affine expression defined on the union of the definition domains
4290 * of the inputs that is equal to the lexicographic minimum of the two
4291 * inputs on each cell. If only one of the two inputs is defined on
4292 * a given cell, then it is considered to be the minimum.
4294 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_union_lexmin(
4295 __isl_take isl_pw_multi_aff
*pma1
,
4296 __isl_take isl_pw_multi_aff
*pma2
)
4298 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
4299 &pw_multi_aff_union_lexmin
);
4302 static __isl_give isl_pw_multi_aff
*pw_multi_aff_add(
4303 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4305 return isl_pw_multi_aff_on_shared_domain(pma1
, pma2
,
4306 &isl_multi_aff_add
);
4309 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_add(
4310 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4312 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
4316 static __isl_give isl_pw_multi_aff
*pw_multi_aff_sub(
4317 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4319 return isl_pw_multi_aff_on_shared_domain(pma1
, pma2
,
4320 &isl_multi_aff_sub
);
4323 /* Subtract "pma2" from "pma1" and return the result.
4325 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_sub(
4326 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4328 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
4332 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_union_add(
4333 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4335 return isl_pw_multi_aff_union_add_(pma1
, pma2
);
4338 /* Compute the sum of "upa1" and "upa2" on the union of their domains,
4339 * with the actual sum on the shared domain and
4340 * the defined expression on the symmetric difference of the domains.
4342 __isl_give isl_union_pw_aff
*isl_union_pw_aff_union_add(
4343 __isl_take isl_union_pw_aff
*upa1
, __isl_take isl_union_pw_aff
*upa2
)
4345 return isl_union_pw_aff_union_add_(upa1
, upa2
);
4348 /* Compute the sum of "upma1" and "upma2" on the union of their domains,
4349 * with the actual sum on the shared domain and
4350 * the defined expression on the symmetric difference of the domains.
4352 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_union_add(
4353 __isl_take isl_union_pw_multi_aff
*upma1
,
4354 __isl_take isl_union_pw_multi_aff
*upma2
)
4356 return isl_union_pw_multi_aff_union_add_(upma1
, upma2
);
4359 /* Given two piecewise multi-affine expressions A -> B and C -> D,
4360 * construct a piecewise multi-affine expression [A -> C] -> [B -> D].
4362 static __isl_give isl_pw_multi_aff
*pw_multi_aff_product(
4363 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4367 isl_pw_multi_aff
*res
;
4372 n
= pma1
->n
* pma2
->n
;
4373 space
= isl_space_product(isl_space_copy(pma1
->dim
),
4374 isl_space_copy(pma2
->dim
));
4375 res
= isl_pw_multi_aff_alloc_size(space
, n
);
4377 for (i
= 0; i
< pma1
->n
; ++i
) {
4378 for (j
= 0; j
< pma2
->n
; ++j
) {
4382 domain
= isl_set_product(isl_set_copy(pma1
->p
[i
].set
),
4383 isl_set_copy(pma2
->p
[j
].set
));
4384 ma
= isl_multi_aff_product(
4385 isl_multi_aff_copy(pma1
->p
[i
].maff
),
4386 isl_multi_aff_copy(pma2
->p
[j
].maff
));
4387 res
= isl_pw_multi_aff_add_piece(res
, domain
, ma
);
4391 isl_pw_multi_aff_free(pma1
);
4392 isl_pw_multi_aff_free(pma2
);
4395 isl_pw_multi_aff_free(pma1
);
4396 isl_pw_multi_aff_free(pma2
);
4400 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_product(
4401 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4403 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
4404 &pw_multi_aff_product
);
4407 /* Construct a map mapping the domain of the piecewise multi-affine expression
4408 * to its range, with each dimension in the range equated to the
4409 * corresponding affine expression on its cell.
4411 * If the domain of "pma" is rational, then so is the constructed "map".
4413 __isl_give isl_map
*isl_map_from_pw_multi_aff(__isl_take isl_pw_multi_aff
*pma
)
4421 map
= isl_map_empty(isl_pw_multi_aff_get_space(pma
));
4423 for (i
= 0; i
< pma
->n
; ++i
) {
4425 isl_multi_aff
*maff
;
4426 isl_basic_map
*bmap
;
4429 rational
= isl_set_is_rational(pma
->p
[i
].set
);
4431 map
= isl_map_free(map
);
4432 maff
= isl_multi_aff_copy(pma
->p
[i
].maff
);
4433 bmap
= isl_basic_map_from_multi_aff2(maff
, rational
);
4434 map_i
= isl_map_from_basic_map(bmap
);
4435 map_i
= isl_map_intersect_domain(map_i
,
4436 isl_set_copy(pma
->p
[i
].set
));
4437 map
= isl_map_union_disjoint(map
, map_i
);
4440 isl_pw_multi_aff_free(pma
);
4444 __isl_give isl_set
*isl_set_from_pw_multi_aff(__isl_take isl_pw_multi_aff
*pma
)
4449 if (!isl_space_is_set(pma
->dim
))
4450 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
4451 "isl_pw_multi_aff cannot be converted into an isl_set",
4454 return isl_map_from_pw_multi_aff(pma
);
4456 isl_pw_multi_aff_free(pma
);
4460 /* Subtract the initial "n" elements in "ma" with coefficients in "c" and
4461 * denominator "denom".
4462 * "denom" is allowed to be negative, in which case the actual denominator
4463 * is -denom and the expressions are added instead.
4465 static __isl_give isl_aff
*subtract_initial(__isl_take isl_aff
*aff
,
4466 __isl_keep isl_multi_aff
*ma
, int n
, isl_int
*c
, isl_int denom
)
4472 first
= isl_seq_first_non_zero(c
, n
);
4476 sign
= isl_int_sgn(denom
);
4478 isl_int_abs(d
, denom
);
4479 for (i
= first
; i
< n
; ++i
) {
4482 if (isl_int_is_zero(c
[i
]))
4484 aff_i
= isl_multi_aff_get_aff(ma
, i
);
4485 aff_i
= isl_aff_scale(aff_i
, c
[i
]);
4486 aff_i
= isl_aff_scale_down(aff_i
, d
);
4488 aff
= isl_aff_sub(aff
, aff_i
);
4490 aff
= isl_aff_add(aff
, aff_i
);
4497 /* Extract an affine expression that expresses the output dimension "pos"
4498 * of "bmap" in terms of the parameters and input dimensions from
4500 * Note that this expression may involve integer divisions defined
4501 * in terms of parameters and input dimensions.
4502 * The equality may also involve references to earlier (but not later)
4503 * output dimensions. These are replaced by the corresponding elements
4506 * If the equality is of the form
4508 * f(i) + h(j) + a x + g(i) = 0,
4510 * with f(i) a linear combinations of the parameters and input dimensions,
4511 * g(i) a linear combination of integer divisions defined in terms of the same
4512 * and h(j) a linear combinations of earlier output dimensions,
4513 * then the affine expression is
4515 * (-f(i) - g(i))/a - h(j)/a
4517 * If the equality is of the form
4519 * f(i) + h(j) - a x + g(i) = 0,
4521 * then the affine expression is
4523 * (f(i) + g(i))/a - h(j)/(-a)
4526 * If "div" refers to an integer division (i.e., it is smaller than
4527 * the number of integer divisions), then the equality constraint
4528 * does involve an integer division (the one at position "div") that
4529 * is defined in terms of output dimensions. However, this integer
4530 * division can be eliminated by exploiting a pair of constraints
4531 * x >= l and x <= l + n, with n smaller than the coefficient of "div"
4532 * in the equality constraint. "ineq" refers to inequality x >= l, i.e.,
4534 * In particular, let
4536 * x = e(i) + m floor(...)
4538 * with e(i) the expression derived above and floor(...) the integer
4539 * division involving output dimensions.
4550 * e(i) + m floor(...) - l = (e(i) + m floor(...) - l) mod m
4551 * = (e(i) - l) mod m
4555 * x - l = (e(i) - l) mod m
4559 * x = ((e(i) - l) mod m) + l
4561 * The variable "shift" below contains the expression -l, which may
4562 * also involve a linear combination of earlier output dimensions.
4564 static __isl_give isl_aff
*extract_aff_from_equality(
4565 __isl_keep isl_basic_map
*bmap
, int pos
, int eq
, int div
, int ineq
,
4566 __isl_keep isl_multi_aff
*ma
)
4569 unsigned n_div
, n_out
;
4571 isl_local_space
*ls
;
4572 isl_aff
*aff
, *shift
;
4575 ctx
= isl_basic_map_get_ctx(bmap
);
4576 ls
= isl_basic_map_get_local_space(bmap
);
4577 ls
= isl_local_space_domain(ls
);
4578 aff
= isl_aff_alloc(isl_local_space_copy(ls
));
4581 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
4582 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4583 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4584 if (isl_int_is_neg(bmap
->eq
[eq
][o_out
+ pos
])) {
4585 isl_seq_cpy(aff
->v
->el
+ 1, bmap
->eq
[eq
], o_out
);
4586 isl_seq_cpy(aff
->v
->el
+ 1 + o_out
,
4587 bmap
->eq
[eq
] + o_out
+ n_out
, n_div
);
4589 isl_seq_neg(aff
->v
->el
+ 1, bmap
->eq
[eq
], o_out
);
4590 isl_seq_neg(aff
->v
->el
+ 1 + o_out
,
4591 bmap
->eq
[eq
] + o_out
+ n_out
, n_div
);
4594 isl_int_set_si(aff
->v
->el
[1 + o_out
+ div
], 0);
4595 isl_int_abs(aff
->v
->el
[0], bmap
->eq
[eq
][o_out
+ pos
]);
4596 aff
= subtract_initial(aff
, ma
, pos
, bmap
->eq
[eq
] + o_out
,
4597 bmap
->eq
[eq
][o_out
+ pos
]);
4599 shift
= isl_aff_alloc(isl_local_space_copy(ls
));
4602 isl_seq_cpy(shift
->v
->el
+ 1, bmap
->ineq
[ineq
], o_out
);
4603 isl_seq_cpy(shift
->v
->el
+ 1 + o_out
,
4604 bmap
->ineq
[ineq
] + o_out
+ n_out
, n_div
);
4605 isl_int_set_si(shift
->v
->el
[0], 1);
4606 shift
= subtract_initial(shift
, ma
, pos
,
4607 bmap
->ineq
[ineq
] + o_out
, ctx
->negone
);
4608 aff
= isl_aff_add(aff
, isl_aff_copy(shift
));
4609 mod
= isl_val_int_from_isl_int(ctx
,
4610 bmap
->eq
[eq
][o_out
+ n_out
+ div
]);
4611 mod
= isl_val_abs(mod
);
4612 aff
= isl_aff_mod_val(aff
, mod
);
4613 aff
= isl_aff_sub(aff
, shift
);
4616 isl_local_space_free(ls
);
4619 isl_local_space_free(ls
);
4624 /* Given a basic map with output dimensions defined
4625 * in terms of the parameters input dimensions and earlier
4626 * output dimensions using an equality (and possibly a pair on inequalities),
4627 * extract an isl_aff that expresses output dimension "pos" in terms
4628 * of the parameters and input dimensions.
4629 * Note that this expression may involve integer divisions defined
4630 * in terms of parameters and input dimensions.
4631 * "ma" contains the expressions corresponding to earlier output dimensions.
4633 * This function shares some similarities with
4634 * isl_basic_map_has_defining_equality and isl_constraint_get_bound.
4636 static __isl_give isl_aff
*extract_isl_aff_from_basic_map(
4637 __isl_keep isl_basic_map
*bmap
, int pos
, __isl_keep isl_multi_aff
*ma
)
4644 eq
= isl_basic_map_output_defining_equality(bmap
, pos
, &div
, &ineq
);
4645 if (eq
>= bmap
->n_eq
)
4646 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
4647 "unable to find suitable equality", return NULL
);
4648 aff
= extract_aff_from_equality(bmap
, pos
, eq
, div
, ineq
, ma
);
4650 aff
= isl_aff_remove_unused_divs(aff
);
4654 /* Given a basic map where each output dimension is defined
4655 * in terms of the parameters and input dimensions using an equality,
4656 * extract an isl_multi_aff that expresses the output dimensions in terms
4657 * of the parameters and input dimensions.
4659 static __isl_give isl_multi_aff
*extract_isl_multi_aff_from_basic_map(
4660 __isl_take isl_basic_map
*bmap
)
4669 ma
= isl_multi_aff_alloc(isl_basic_map_get_space(bmap
));
4670 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4672 for (i
= 0; i
< n_out
; ++i
) {
4675 aff
= extract_isl_aff_from_basic_map(bmap
, i
, ma
);
4676 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
4679 isl_basic_map_free(bmap
);
4684 /* Given a basic set where each set dimension is defined
4685 * in terms of the parameters using an equality,
4686 * extract an isl_multi_aff that expresses the set dimensions in terms
4687 * of the parameters.
4689 __isl_give isl_multi_aff
*isl_multi_aff_from_basic_set_equalities(
4690 __isl_take isl_basic_set
*bset
)
4692 return extract_isl_multi_aff_from_basic_map(bset
);
4695 /* Create an isl_pw_multi_aff that is equivalent to
4696 * isl_map_intersect_domain(isl_map_from_basic_map(bmap), domain).
4697 * The given basic map is such that each output dimension is defined
4698 * in terms of the parameters and input dimensions using an equality.
4700 * Since some applications expect the result of isl_pw_multi_aff_from_map
4701 * to only contain integer affine expressions, we compute the floor
4702 * of the expression before returning.
4704 * Remove all constraints involving local variables without
4705 * an explicit representation (resulting in the removal of those
4706 * local variables) prior to the actual extraction to ensure
4707 * that the local spaces in which the resulting affine expressions
4708 * are created do not contain any unknown local variables.
4709 * Removing such constraints is safe because constraints involving
4710 * unknown local variables are not used to determine whether
4711 * a basic map is obviously single-valued.
4713 static __isl_give isl_pw_multi_aff
*plain_pw_multi_aff_from_map(
4714 __isl_take isl_set
*domain
, __isl_take isl_basic_map
*bmap
)
4718 bmap
= isl_basic_map_drop_constraint_involving_unknown_divs(bmap
);
4719 ma
= extract_isl_multi_aff_from_basic_map(bmap
);
4720 ma
= isl_multi_aff_floor(ma
);
4721 return isl_pw_multi_aff_alloc(domain
, ma
);
4724 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4725 * This obviously only works if the input "map" is single-valued.
4726 * If so, we compute the lexicographic minimum of the image in the form
4727 * of an isl_pw_multi_aff. Since the image is unique, it is equal
4728 * to its lexicographic minimum.
4729 * If the input is not single-valued, we produce an error.
4731 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_base(
4732 __isl_take isl_map
*map
)
4736 isl_pw_multi_aff
*pma
;
4738 sv
= isl_map_is_single_valued(map
);
4742 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
4743 "map is not single-valued", goto error
);
4744 map
= isl_map_make_disjoint(map
);
4748 pma
= isl_pw_multi_aff_empty(isl_map_get_space(map
));
4750 for (i
= 0; i
< map
->n
; ++i
) {
4751 isl_pw_multi_aff
*pma_i
;
4752 isl_basic_map
*bmap
;
4753 bmap
= isl_basic_map_copy(map
->p
[i
]);
4754 pma_i
= isl_basic_map_lexmin_pw_multi_aff(bmap
);
4755 pma
= isl_pw_multi_aff_add_disjoint(pma
, pma_i
);
4765 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
4766 * taking into account that the output dimension at position "d"
4767 * can be represented as
4769 * x = floor((e(...) + c1) / m)
4771 * given that constraint "i" is of the form
4773 * e(...) + c1 - m x >= 0
4776 * Let "map" be of the form
4780 * We construct a mapping
4782 * A -> [A -> x = floor(...)]
4784 * apply that to the map, obtaining
4786 * [A -> x = floor(...)] -> B
4788 * and equate dimension "d" to x.
4789 * We then compute a isl_pw_multi_aff representation of the resulting map
4790 * and plug in the mapping above.
4792 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_div(
4793 __isl_take isl_map
*map
, __isl_take isl_basic_map
*hull
, int d
, int i
)
4797 isl_local_space
*ls
;
4805 isl_pw_multi_aff
*pma
;
4808 is_set
= isl_map_is_set(map
);
4812 offset
= isl_basic_map_offset(hull
, isl_dim_out
);
4813 ctx
= isl_map_get_ctx(map
);
4814 space
= isl_space_domain(isl_map_get_space(map
));
4815 n_in
= isl_space_dim(space
, isl_dim_set
);
4816 n
= isl_space_dim(space
, isl_dim_all
);
4818 v
= isl_vec_alloc(ctx
, 1 + 1 + n
);
4820 isl_int_neg(v
->el
[0], hull
->ineq
[i
][offset
+ d
]);
4821 isl_seq_cpy(v
->el
+ 1, hull
->ineq
[i
], 1 + n
);
4823 isl_basic_map_free(hull
);
4825 ls
= isl_local_space_from_space(isl_space_copy(space
));
4826 aff
= isl_aff_alloc_vec(ls
, v
);
4827 aff
= isl_aff_floor(aff
);
4829 isl_space_free(space
);
4830 ma
= isl_multi_aff_from_aff(aff
);
4832 ma
= isl_multi_aff_identity(isl_space_map_from_set(space
));
4833 ma
= isl_multi_aff_range_product(ma
,
4834 isl_multi_aff_from_aff(aff
));
4837 insert
= isl_map_from_multi_aff(isl_multi_aff_copy(ma
));
4838 map
= isl_map_apply_domain(map
, insert
);
4839 map
= isl_map_equate(map
, isl_dim_in
, n_in
, isl_dim_out
, d
);
4840 pma
= isl_pw_multi_aff_from_map(map
);
4841 pma
= isl_pw_multi_aff_pullback_multi_aff(pma
, ma
);
4846 isl_basic_map_free(hull
);
4850 /* Is constraint "c" of the form
4852 * e(...) + c1 - m x >= 0
4856 * -e(...) + c2 + m x >= 0
4858 * where m > 1 and e only depends on parameters and input dimemnsions?
4860 * "offset" is the offset of the output dimensions
4861 * "pos" is the position of output dimension x.
4863 static int is_potential_div_constraint(isl_int
*c
, int offset
, int d
, int total
)
4865 if (isl_int_is_zero(c
[offset
+ d
]))
4867 if (isl_int_is_one(c
[offset
+ d
]))
4869 if (isl_int_is_negone(c
[offset
+ d
]))
4871 if (isl_seq_first_non_zero(c
+ offset
, d
) != -1)
4873 if (isl_seq_first_non_zero(c
+ offset
+ d
+ 1,
4874 total
- (offset
+ d
+ 1)) != -1)
4879 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4881 * As a special case, we first check if there is any pair of constraints,
4882 * shared by all the basic maps in "map" that force a given dimension
4883 * to be equal to the floor of some affine combination of the input dimensions.
4885 * In particular, if we can find two constraints
4887 * e(...) + c1 - m x >= 0 i.e., m x <= e(...) + c1
4891 * -e(...) + c2 + m x >= 0 i.e., m x >= e(...) - c2
4893 * where m > 1 and e only depends on parameters and input dimemnsions,
4896 * c1 + c2 < m i.e., -c2 >= c1 - (m - 1)
4898 * then we know that we can take
4900 * x = floor((e(...) + c1) / m)
4902 * without having to perform any computation.
4904 * Note that we know that
4908 * If c1 + c2 were 0, then we would have detected an equality during
4909 * simplification. If c1 + c2 were negative, then we would have detected
4912 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_check_div(
4913 __isl_take isl_map
*map
)
4919 isl_basic_map
*hull
;
4921 hull
= isl_map_unshifted_simple_hull(isl_map_copy(map
));
4926 dim
= isl_map_dim(map
, isl_dim_out
);
4927 offset
= isl_basic_map_offset(hull
, isl_dim_out
);
4928 total
= 1 + isl_basic_map_total_dim(hull
);
4930 for (d
= 0; d
< dim
; ++d
) {
4931 for (i
= 0; i
< n
; ++i
) {
4932 if (!is_potential_div_constraint(hull
->ineq
[i
],
4935 for (j
= i
+ 1; j
< n
; ++j
) {
4936 if (!isl_seq_is_neg(hull
->ineq
[i
] + 1,
4937 hull
->ineq
[j
] + 1, total
- 1))
4939 isl_int_add(sum
, hull
->ineq
[i
][0],
4941 if (isl_int_abs_lt(sum
,
4942 hull
->ineq
[i
][offset
+ d
]))
4949 if (isl_int_is_pos(hull
->ineq
[j
][offset
+ d
]))
4951 return pw_multi_aff_from_map_div(map
, hull
, d
, j
);
4955 isl_basic_map_free(hull
);
4956 return pw_multi_aff_from_map_base(map
);
4959 isl_basic_map_free(hull
);
4963 /* Given an affine expression
4965 * [A -> B] -> f(A,B)
4967 * construct an isl_multi_aff
4971 * such that dimension "d" in B' is set to "aff" and the remaining
4972 * dimensions are set equal to the corresponding dimensions in B.
4973 * "n_in" is the dimension of the space A.
4974 * "n_out" is the dimension of the space B.
4976 * If "is_set" is set, then the affine expression is of the form
4980 * and we construct an isl_multi_aff
4984 static __isl_give isl_multi_aff
*range_map(__isl_take isl_aff
*aff
, int d
,
4985 unsigned n_in
, unsigned n_out
, int is_set
)
4989 isl_space
*space
, *space2
;
4990 isl_local_space
*ls
;
4992 space
= isl_aff_get_domain_space(aff
);
4993 ls
= isl_local_space_from_space(isl_space_copy(space
));
4994 space2
= isl_space_copy(space
);
4996 space2
= isl_space_range(isl_space_unwrap(space2
));
4997 space
= isl_space_map_from_domain_and_range(space
, space2
);
4998 ma
= isl_multi_aff_alloc(space
);
4999 ma
= isl_multi_aff_set_aff(ma
, d
, aff
);
5001 for (i
= 0; i
< n_out
; ++i
) {
5004 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
5005 isl_dim_set
, n_in
+ i
);
5006 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
5009 isl_local_space_free(ls
);
5014 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
5015 * taking into account that the dimension at position "d" can be written as
5017 * x = m a + f(..) (1)
5019 * where m is equal to "gcd".
5020 * "i" is the index of the equality in "hull" that defines f(..).
5021 * In particular, the equality is of the form
5023 * f(..) - x + m g(existentials) = 0
5027 * -f(..) + x + m g(existentials) = 0
5029 * We basically plug (1) into "map", resulting in a map with "a"
5030 * in the range instead of "x". The corresponding isl_pw_multi_aff
5031 * defining "a" is then plugged back into (1) to obtain a definition for "x".
5033 * Specifically, given the input map
5037 * We first wrap it into a set
5041 * and define (1) on top of the corresponding space, resulting in "aff".
5042 * We use this to create an isl_multi_aff that maps the output position "d"
5043 * from "a" to "x", leaving all other (intput and output) dimensions unchanged.
5044 * We plug this into the wrapped map, unwrap the result and compute the
5045 * corresponding isl_pw_multi_aff.
5046 * The result is an expression
5054 * so that we can plug that into "aff", after extending the latter to
5060 * If "map" is actually a set, then there is no "A" space, meaning
5061 * that we do not need to perform any wrapping, and that the result
5062 * of the recursive call is of the form
5066 * which is plugged into a mapping of the form
5070 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_stride(
5071 __isl_take isl_map
*map
, __isl_take isl_basic_map
*hull
, int d
, int i
,
5076 isl_local_space
*ls
;
5079 isl_pw_multi_aff
*pma
, *id
;
5085 is_set
= isl_map_is_set(map
);
5089 n_in
= isl_basic_map_dim(hull
, isl_dim_in
);
5090 n_out
= isl_basic_map_dim(hull
, isl_dim_out
);
5091 o_out
= isl_basic_map_offset(hull
, isl_dim_out
);
5096 set
= isl_map_wrap(map
);
5097 space
= isl_space_map_from_set(isl_set_get_space(set
));
5098 ma
= isl_multi_aff_identity(space
);
5099 ls
= isl_local_space_from_space(isl_set_get_space(set
));
5100 aff
= isl_aff_alloc(ls
);
5102 isl_int_set_si(aff
->v
->el
[0], 1);
5103 if (isl_int_is_one(hull
->eq
[i
][o_out
+ d
]))
5104 isl_seq_neg(aff
->v
->el
+ 1, hull
->eq
[i
],
5107 isl_seq_cpy(aff
->v
->el
+ 1, hull
->eq
[i
],
5109 isl_int_set(aff
->v
->el
[1 + o_out
+ d
], gcd
);
5111 ma
= isl_multi_aff_set_aff(ma
, n_in
+ d
, isl_aff_copy(aff
));
5112 set
= isl_set_preimage_multi_aff(set
, ma
);
5114 ma
= range_map(aff
, d
, n_in
, n_out
, is_set
);
5119 map
= isl_set_unwrap(set
);
5120 pma
= isl_pw_multi_aff_from_map(map
);
5123 space
= isl_pw_multi_aff_get_domain_space(pma
);
5124 space
= isl_space_map_from_set(space
);
5125 id
= isl_pw_multi_aff_identity(space
);
5126 pma
= isl_pw_multi_aff_range_product(id
, pma
);
5128 id
= isl_pw_multi_aff_from_multi_aff(ma
);
5129 pma
= isl_pw_multi_aff_pullback_pw_multi_aff(id
, pma
);
5131 isl_basic_map_free(hull
);
5135 isl_basic_map_free(hull
);
5139 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5140 * "hull" contains the equalities valid for "map".
5142 * Check if any of the output dimensions is "strided".
5143 * That is, we check if it can be written as
5147 * with m greater than 1, a some combination of existentially quantified
5148 * variables and f an expression in the parameters and input dimensions.
5149 * If so, we remove the stride in pw_multi_aff_from_map_stride.
5151 * Otherwise, we continue with pw_multi_aff_from_map_check_div for a further
5154 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_check_strides(
5155 __isl_take isl_map
*map
, __isl_take isl_basic_map
*hull
)
5164 n_div
= isl_basic_map_dim(hull
, isl_dim_div
);
5165 o_div
= isl_basic_map_offset(hull
, isl_dim_div
);
5168 isl_basic_map_free(hull
);
5169 return pw_multi_aff_from_map_check_div(map
);
5174 n_out
= isl_basic_map_dim(hull
, isl_dim_out
);
5175 o_out
= isl_basic_map_offset(hull
, isl_dim_out
);
5177 for (i
= 0; i
< n_out
; ++i
) {
5178 for (j
= 0; j
< hull
->n_eq
; ++j
) {
5179 isl_int
*eq
= hull
->eq
[j
];
5180 isl_pw_multi_aff
*res
;
5182 if (!isl_int_is_one(eq
[o_out
+ i
]) &&
5183 !isl_int_is_negone(eq
[o_out
+ i
]))
5185 if (isl_seq_first_non_zero(eq
+ o_out
, i
) != -1)
5187 if (isl_seq_first_non_zero(eq
+ o_out
+ i
+ 1,
5188 n_out
- (i
+ 1)) != -1)
5190 isl_seq_gcd(eq
+ o_div
, n_div
, &gcd
);
5191 if (isl_int_is_zero(gcd
))
5193 if (isl_int_is_one(gcd
))
5196 res
= pw_multi_aff_from_map_stride(map
, hull
,
5204 isl_basic_map_free(hull
);
5205 return pw_multi_aff_from_map_check_div(map
);
5208 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5210 * As a special case, we first check if all output dimensions are uniquely
5211 * defined in terms of the parameters and input dimensions over the entire
5212 * domain. If so, we extract the desired isl_pw_multi_aff directly
5213 * from the affine hull of "map" and its domain.
5215 * Otherwise, continue with pw_multi_aff_from_map_check_strides for more
5218 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_map(__isl_take isl_map
*map
)
5221 isl_basic_map
*hull
;
5226 if (isl_map_n_basic_map(map
) == 1) {
5227 hull
= isl_map_unshifted_simple_hull(isl_map_copy(map
));
5228 hull
= isl_basic_map_plain_affine_hull(hull
);
5229 sv
= isl_basic_map_plain_is_single_valued(hull
);
5231 return plain_pw_multi_aff_from_map(isl_map_domain(map
),
5233 isl_basic_map_free(hull
);
5235 map
= isl_map_detect_equalities(map
);
5236 hull
= isl_map_unshifted_simple_hull(isl_map_copy(map
));
5237 sv
= isl_basic_map_plain_is_single_valued(hull
);
5239 return plain_pw_multi_aff_from_map(isl_map_domain(map
), hull
);
5241 return pw_multi_aff_from_map_check_strides(map
, hull
);
5242 isl_basic_map_free(hull
);
5247 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_set(__isl_take isl_set
*set
)
5249 return isl_pw_multi_aff_from_map(set
);
5252 /* Convert "map" into an isl_pw_multi_aff (if possible) and
5255 static isl_stat
pw_multi_aff_from_map(__isl_take isl_map
*map
, void *user
)
5257 isl_union_pw_multi_aff
**upma
= user
;
5258 isl_pw_multi_aff
*pma
;
5260 pma
= isl_pw_multi_aff_from_map(map
);
5261 *upma
= isl_union_pw_multi_aff_add_pw_multi_aff(*upma
, pma
);
5263 return *upma
? isl_stat_ok
: isl_stat_error
;
5266 /* Create an isl_union_pw_multi_aff with the given isl_aff on a universe
5269 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_aff(
5270 __isl_take isl_aff
*aff
)
5273 isl_pw_multi_aff
*pma
;
5275 ma
= isl_multi_aff_from_aff(aff
);
5276 pma
= isl_pw_multi_aff_from_multi_aff(ma
);
5277 return isl_union_pw_multi_aff_from_pw_multi_aff(pma
);
5280 /* Try and create an isl_union_pw_multi_aff that is equivalent
5281 * to the given isl_union_map.
5282 * The isl_union_map is required to be single-valued in each space.
5283 * Otherwise, an error is produced.
5285 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_union_map(
5286 __isl_take isl_union_map
*umap
)
5289 isl_union_pw_multi_aff
*upma
;
5291 space
= isl_union_map_get_space(umap
);
5292 upma
= isl_union_pw_multi_aff_empty(space
);
5293 if (isl_union_map_foreach_map(umap
, &pw_multi_aff_from_map
, &upma
) < 0)
5294 upma
= isl_union_pw_multi_aff_free(upma
);
5295 isl_union_map_free(umap
);
5300 /* Try and create an isl_union_pw_multi_aff that is equivalent
5301 * to the given isl_union_set.
5302 * The isl_union_set is required to be a singleton in each space.
5303 * Otherwise, an error is produced.
5305 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_union_set(
5306 __isl_take isl_union_set
*uset
)
5308 return isl_union_pw_multi_aff_from_union_map(uset
);
5311 /* Return the piecewise affine expression "set ? 1 : 0".
5313 __isl_give isl_pw_aff
*isl_set_indicator_function(__isl_take isl_set
*set
)
5316 isl_space
*space
= isl_set_get_space(set
);
5317 isl_local_space
*ls
= isl_local_space_from_space(space
);
5318 isl_aff
*zero
= isl_aff_zero_on_domain(isl_local_space_copy(ls
));
5319 isl_aff
*one
= isl_aff_zero_on_domain(ls
);
5321 one
= isl_aff_add_constant_si(one
, 1);
5322 pa
= isl_pw_aff_alloc(isl_set_copy(set
), one
);
5323 set
= isl_set_complement(set
);
5324 pa
= isl_pw_aff_add_disjoint(pa
, isl_pw_aff_alloc(set
, zero
));
5329 /* Plug in "subs" for dimension "type", "pos" of "aff".
5331 * Let i be the dimension to replace and let "subs" be of the form
5335 * and "aff" of the form
5341 * (a f + d g')/(m d)
5343 * where g' is the result of plugging in "subs" in each of the integer
5346 __isl_give isl_aff
*isl_aff_substitute(__isl_take isl_aff
*aff
,
5347 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
5352 aff
= isl_aff_cow(aff
);
5354 return isl_aff_free(aff
);
5356 ctx
= isl_aff_get_ctx(aff
);
5357 if (!isl_space_is_equal(aff
->ls
->dim
, subs
->ls
->dim
))
5358 isl_die(ctx
, isl_error_invalid
,
5359 "spaces don't match", return isl_aff_free(aff
));
5360 if (isl_local_space_dim(subs
->ls
, isl_dim_div
) != 0)
5361 isl_die(ctx
, isl_error_unsupported
,
5362 "cannot handle divs yet", return isl_aff_free(aff
));
5364 aff
->ls
= isl_local_space_substitute(aff
->ls
, type
, pos
, subs
);
5366 return isl_aff_free(aff
);
5368 aff
->v
= isl_vec_cow(aff
->v
);
5370 return isl_aff_free(aff
);
5372 pos
+= isl_local_space_offset(aff
->ls
, type
);
5375 isl_seq_substitute(aff
->v
->el
, pos
, subs
->v
->el
,
5376 aff
->v
->size
, subs
->v
->size
, v
);
5382 /* Plug in "subs" for dimension "type", "pos" in each of the affine
5383 * expressions in "maff".
5385 __isl_give isl_multi_aff
*isl_multi_aff_substitute(
5386 __isl_take isl_multi_aff
*maff
, enum isl_dim_type type
, unsigned pos
,
5387 __isl_keep isl_aff
*subs
)
5391 maff
= isl_multi_aff_cow(maff
);
5393 return isl_multi_aff_free(maff
);
5395 if (type
== isl_dim_in
)
5398 for (i
= 0; i
< maff
->n
; ++i
) {
5399 maff
->p
[i
] = isl_aff_substitute(maff
->p
[i
], type
, pos
, subs
);
5401 return isl_multi_aff_free(maff
);
5407 /* Plug in "subs" for dimension "type", "pos" of "pma".
5409 * pma is of the form
5413 * while subs is of the form
5415 * v' = B_j(v) -> S_j
5417 * Each pair i,j such that C_ij = A_i \cap B_i is non-empty
5418 * has a contribution in the result, in particular
5420 * C_ij(S_j) -> M_i(S_j)
5422 * Note that plugging in S_j in C_ij may also result in an empty set
5423 * and this contribution should simply be discarded.
5425 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_substitute(
5426 __isl_take isl_pw_multi_aff
*pma
, enum isl_dim_type type
, unsigned pos
,
5427 __isl_keep isl_pw_aff
*subs
)
5430 isl_pw_multi_aff
*res
;
5433 return isl_pw_multi_aff_free(pma
);
5435 n
= pma
->n
* subs
->n
;
5436 res
= isl_pw_multi_aff_alloc_size(isl_space_copy(pma
->dim
), n
);
5438 for (i
= 0; i
< pma
->n
; ++i
) {
5439 for (j
= 0; j
< subs
->n
; ++j
) {
5441 isl_multi_aff
*res_ij
;
5444 common
= isl_set_intersect(
5445 isl_set_copy(pma
->p
[i
].set
),
5446 isl_set_copy(subs
->p
[j
].set
));
5447 common
= isl_set_substitute(common
,
5448 type
, pos
, subs
->p
[j
].aff
);
5449 empty
= isl_set_plain_is_empty(common
);
5450 if (empty
< 0 || empty
) {
5451 isl_set_free(common
);
5457 res_ij
= isl_multi_aff_substitute(
5458 isl_multi_aff_copy(pma
->p
[i
].maff
),
5459 type
, pos
, subs
->p
[j
].aff
);
5461 res
= isl_pw_multi_aff_add_piece(res
, common
, res_ij
);
5465 isl_pw_multi_aff_free(pma
);
5468 isl_pw_multi_aff_free(pma
);
5469 isl_pw_multi_aff_free(res
);
5473 /* Compute the preimage of a range of dimensions in the affine expression "src"
5474 * under "ma" and put the result in "dst". The number of dimensions in "src"
5475 * that precede the range is given by "n_before". The number of dimensions
5476 * in the range is given by the number of output dimensions of "ma".
5477 * The number of dimensions that follow the range is given by "n_after".
5478 * If "has_denom" is set (to one),
5479 * then "src" and "dst" have an extra initial denominator.
5480 * "n_div_ma" is the number of existentials in "ma"
5481 * "n_div_bset" is the number of existentials in "src"
5482 * The resulting "dst" (which is assumed to have been allocated by
5483 * the caller) contains coefficients for both sets of existentials,
5484 * first those in "ma" and then those in "src".
5485 * f, c1, c2 and g are temporary objects that have been initialized
5488 * Let src represent the expression
5490 * (a(p) + f_u u + b v + f_w w + c(divs))/d
5492 * and let ma represent the expressions
5494 * v_i = (r_i(p) + s_i(y) + t_i(divs'))/m_i
5496 * We start out with the following expression for dst:
5498 * (a(p) + f_u u + 0 y + f_w w + 0 divs' + c(divs) + f \sum_i b_i v_i)/d
5500 * with the multiplication factor f initially equal to 1
5501 * and f \sum_i b_i v_i kept separately.
5502 * For each x_i that we substitute, we multiply the numerator
5503 * (and denominator) of dst by c_1 = m_i and add the numerator
5504 * of the x_i expression multiplied by c_2 = f b_i,
5505 * after removing the common factors of c_1 and c_2.
5506 * The multiplication factor f also needs to be multiplied by c_1
5507 * for the next x_j, j > i.
5509 void isl_seq_preimage(isl_int
*dst
, isl_int
*src
,
5510 __isl_keep isl_multi_aff
*ma
, int n_before
, int n_after
,
5511 int n_div_ma
, int n_div_bmap
,
5512 isl_int f
, isl_int c1
, isl_int c2
, isl_int g
, int has_denom
)
5515 int n_param
, n_in
, n_out
;
5518 n_param
= isl_multi_aff_dim(ma
, isl_dim_param
);
5519 n_in
= isl_multi_aff_dim(ma
, isl_dim_in
);
5520 n_out
= isl_multi_aff_dim(ma
, isl_dim_out
);
5522 isl_seq_cpy(dst
, src
, has_denom
+ 1 + n_param
+ n_before
);
5523 o_dst
= o_src
= has_denom
+ 1 + n_param
+ n_before
;
5524 isl_seq_clr(dst
+ o_dst
, n_in
);
5527 isl_seq_cpy(dst
+ o_dst
, src
+ o_src
, n_after
);
5530 isl_seq_clr(dst
+ o_dst
, n_div_ma
);
5532 isl_seq_cpy(dst
+ o_dst
, src
+ o_src
, n_div_bmap
);
5534 isl_int_set_si(f
, 1);
5536 for (i
= 0; i
< n_out
; ++i
) {
5537 int offset
= has_denom
+ 1 + n_param
+ n_before
+ i
;
5539 if (isl_int_is_zero(src
[offset
]))
5541 isl_int_set(c1
, ma
->p
[i
]->v
->el
[0]);
5542 isl_int_mul(c2
, f
, src
[offset
]);
5543 isl_int_gcd(g
, c1
, c2
);
5544 isl_int_divexact(c1
, c1
, g
);
5545 isl_int_divexact(c2
, c2
, g
);
5547 isl_int_mul(f
, f
, c1
);
5550 isl_seq_combine(dst
+ o_dst
, c1
, dst
+ o_dst
,
5551 c2
, ma
->p
[i
]->v
->el
+ o_src
, 1 + n_param
);
5552 o_dst
+= 1 + n_param
;
5553 o_src
+= 1 + n_param
;
5554 isl_seq_scale(dst
+ o_dst
, dst
+ o_dst
, c1
, n_before
);
5556 isl_seq_combine(dst
+ o_dst
, c1
, dst
+ o_dst
,
5557 c2
, ma
->p
[i
]->v
->el
+ o_src
, n_in
);
5560 isl_seq_scale(dst
+ o_dst
, dst
+ o_dst
, c1
, n_after
);
5562 isl_seq_combine(dst
+ o_dst
, c1
, dst
+ o_dst
,
5563 c2
, ma
->p
[i
]->v
->el
+ o_src
, n_div_ma
);
5566 isl_seq_scale(dst
+ o_dst
, dst
+ o_dst
, c1
, n_div_bmap
);
5568 isl_int_mul(dst
[0], dst
[0], c1
);
5572 /* Compute the pullback of "aff" by the function represented by "ma".
5573 * In other words, plug in "ma" in "aff". The result is an affine expression
5574 * defined over the domain space of "ma".
5576 * If "aff" is represented by
5578 * (a(p) + b x + c(divs))/d
5580 * and ma is represented by
5582 * x = D(p) + F(y) + G(divs')
5584 * then the result is
5586 * (a(p) + b D(p) + b F(y) + b G(divs') + c(divs))/d
5588 * The divs in the local space of the input are similarly adjusted
5589 * through a call to isl_local_space_preimage_multi_aff.
5591 __isl_give isl_aff
*isl_aff_pullback_multi_aff(__isl_take isl_aff
*aff
,
5592 __isl_take isl_multi_aff
*ma
)
5594 isl_aff
*res
= NULL
;
5595 isl_local_space
*ls
;
5596 int n_div_aff
, n_div_ma
;
5597 isl_int f
, c1
, c2
, g
;
5599 ma
= isl_multi_aff_align_divs(ma
);
5603 n_div_aff
= isl_aff_dim(aff
, isl_dim_div
);
5604 n_div_ma
= ma
->n
? isl_aff_dim(ma
->p
[0], isl_dim_div
) : 0;
5606 ls
= isl_aff_get_domain_local_space(aff
);
5607 ls
= isl_local_space_preimage_multi_aff(ls
, isl_multi_aff_copy(ma
));
5608 res
= isl_aff_alloc(ls
);
5617 isl_seq_preimage(res
->v
->el
, aff
->v
->el
, ma
, 0, 0, n_div_ma
, n_div_aff
,
5626 isl_multi_aff_free(ma
);
5627 res
= isl_aff_normalize(res
);
5631 isl_multi_aff_free(ma
);
5636 /* Compute the pullback of "aff1" by the function represented by "aff2".
5637 * In other words, plug in "aff2" in "aff1". The result is an affine expression
5638 * defined over the domain space of "aff1".
5640 * The domain of "aff1" should match the range of "aff2", which means
5641 * that it should be single-dimensional.
5643 __isl_give isl_aff
*isl_aff_pullback_aff(__isl_take isl_aff
*aff1
,
5644 __isl_take isl_aff
*aff2
)
5648 ma
= isl_multi_aff_from_aff(aff2
);
5649 return isl_aff_pullback_multi_aff(aff1
, ma
);
5652 /* Compute the pullback of "ma1" by the function represented by "ma2".
5653 * In other words, plug in "ma2" in "ma1".
5655 * The parameters of "ma1" and "ma2" are assumed to have been aligned.
5657 static __isl_give isl_multi_aff
*isl_multi_aff_pullback_multi_aff_aligned(
5658 __isl_take isl_multi_aff
*ma1
, __isl_take isl_multi_aff
*ma2
)
5661 isl_space
*space
= NULL
;
5663 ma2
= isl_multi_aff_align_divs(ma2
);
5664 ma1
= isl_multi_aff_cow(ma1
);
5668 space
= isl_space_join(isl_multi_aff_get_space(ma2
),
5669 isl_multi_aff_get_space(ma1
));
5671 for (i
= 0; i
< ma1
->n
; ++i
) {
5672 ma1
->p
[i
] = isl_aff_pullback_multi_aff(ma1
->p
[i
],
5673 isl_multi_aff_copy(ma2
));
5678 ma1
= isl_multi_aff_reset_space(ma1
, space
);
5679 isl_multi_aff_free(ma2
);
5682 isl_space_free(space
);
5683 isl_multi_aff_free(ma2
);
5684 isl_multi_aff_free(ma1
);
5688 /* Compute the pullback of "ma1" by the function represented by "ma2".
5689 * In other words, plug in "ma2" in "ma1".
5691 __isl_give isl_multi_aff
*isl_multi_aff_pullback_multi_aff(
5692 __isl_take isl_multi_aff
*ma1
, __isl_take isl_multi_aff
*ma2
)
5694 return isl_multi_aff_align_params_multi_multi_and(ma1
, ma2
,
5695 &isl_multi_aff_pullback_multi_aff_aligned
);
5698 /* Extend the local space of "dst" to include the divs
5699 * in the local space of "src".
5701 * If "src" does not have any divs or if the local spaces of "dst" and
5702 * "src" are the same, then no extension is required.
5704 __isl_give isl_aff
*isl_aff_align_divs(__isl_take isl_aff
*dst
,
5705 __isl_keep isl_aff
*src
)
5708 int src_n_div
, dst_n_div
;
5715 return isl_aff_free(dst
);
5717 ctx
= isl_aff_get_ctx(src
);
5718 equal
= isl_local_space_has_equal_space(src
->ls
, dst
->ls
);
5720 return isl_aff_free(dst
);
5722 isl_die(ctx
, isl_error_invalid
,
5723 "spaces don't match", goto error
);
5725 src_n_div
= isl_local_space_dim(src
->ls
, isl_dim_div
);
5728 equal
= isl_local_space_is_equal(src
->ls
, dst
->ls
);
5730 return isl_aff_free(dst
);
5734 dst_n_div
= isl_local_space_dim(dst
->ls
, isl_dim_div
);
5735 exp1
= isl_alloc_array(ctx
, int, src_n_div
);
5736 exp2
= isl_alloc_array(ctx
, int, dst_n_div
);
5737 if (!exp1
|| (dst_n_div
&& !exp2
))
5740 div
= isl_merge_divs(src
->ls
->div
, dst
->ls
->div
, exp1
, exp2
);
5741 dst
= isl_aff_expand_divs(dst
, div
, exp2
);
5749 return isl_aff_free(dst
);
5752 /* Adjust the local spaces of the affine expressions in "maff"
5753 * such that they all have the save divs.
5755 __isl_give isl_multi_aff
*isl_multi_aff_align_divs(
5756 __isl_take isl_multi_aff
*maff
)
5764 maff
= isl_multi_aff_cow(maff
);
5768 for (i
= 1; i
< maff
->n
; ++i
)
5769 maff
->p
[0] = isl_aff_align_divs(maff
->p
[0], maff
->p
[i
]);
5770 for (i
= 1; i
< maff
->n
; ++i
) {
5771 maff
->p
[i
] = isl_aff_align_divs(maff
->p
[i
], maff
->p
[0]);
5773 return isl_multi_aff_free(maff
);
5779 __isl_give isl_aff
*isl_aff_lift(__isl_take isl_aff
*aff
)
5781 aff
= isl_aff_cow(aff
);
5785 aff
->ls
= isl_local_space_lift(aff
->ls
);
5787 return isl_aff_free(aff
);
5792 /* Lift "maff" to a space with extra dimensions such that the result
5793 * has no more existentially quantified variables.
5794 * If "ls" is not NULL, then *ls is assigned the local space that lies
5795 * at the basis of the lifting applied to "maff".
5797 __isl_give isl_multi_aff
*isl_multi_aff_lift(__isl_take isl_multi_aff
*maff
,
5798 __isl_give isl_local_space
**ls
)
5812 isl_space
*space
= isl_multi_aff_get_domain_space(maff
);
5813 *ls
= isl_local_space_from_space(space
);
5815 return isl_multi_aff_free(maff
);
5820 maff
= isl_multi_aff_cow(maff
);
5821 maff
= isl_multi_aff_align_divs(maff
);
5825 n_div
= isl_aff_dim(maff
->p
[0], isl_dim_div
);
5826 space
= isl_multi_aff_get_space(maff
);
5827 space
= isl_space_lift(isl_space_domain(space
), n_div
);
5828 space
= isl_space_extend_domain_with_range(space
,
5829 isl_multi_aff_get_space(maff
));
5831 return isl_multi_aff_free(maff
);
5832 isl_space_free(maff
->space
);
5833 maff
->space
= space
;
5836 *ls
= isl_aff_get_domain_local_space(maff
->p
[0]);
5838 return isl_multi_aff_free(maff
);
5841 for (i
= 0; i
< maff
->n
; ++i
) {
5842 maff
->p
[i
] = isl_aff_lift(maff
->p
[i
]);
5850 isl_local_space_free(*ls
);
5851 return isl_multi_aff_free(maff
);
5855 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma".
5857 __isl_give isl_pw_aff
*isl_pw_multi_aff_get_pw_aff(
5858 __isl_keep isl_pw_multi_aff
*pma
, int pos
)
5868 n_out
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
5869 if (pos
< 0 || pos
>= n_out
)
5870 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
5871 "index out of bounds", return NULL
);
5873 space
= isl_pw_multi_aff_get_space(pma
);
5874 space
= isl_space_drop_dims(space
, isl_dim_out
,
5875 pos
+ 1, n_out
- pos
- 1);
5876 space
= isl_space_drop_dims(space
, isl_dim_out
, 0, pos
);
5878 pa
= isl_pw_aff_alloc_size(space
, pma
->n
);
5879 for (i
= 0; i
< pma
->n
; ++i
) {
5881 aff
= isl_multi_aff_get_aff(pma
->p
[i
].maff
, pos
);
5882 pa
= isl_pw_aff_add_piece(pa
, isl_set_copy(pma
->p
[i
].set
), aff
);
5888 /* Return an isl_pw_multi_aff with the given "set" as domain and
5889 * an unnamed zero-dimensional range.
5891 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_domain(
5892 __isl_take isl_set
*set
)
5897 space
= isl_set_get_space(set
);
5898 space
= isl_space_from_domain(space
);
5899 ma
= isl_multi_aff_zero(space
);
5900 return isl_pw_multi_aff_alloc(set
, ma
);
5903 /* Add an isl_pw_multi_aff with the given "set" as domain and
5904 * an unnamed zero-dimensional range to *user.
5906 static isl_stat
add_pw_multi_aff_from_domain(__isl_take isl_set
*set
,
5909 isl_union_pw_multi_aff
**upma
= user
;
5910 isl_pw_multi_aff
*pma
;
5912 pma
= isl_pw_multi_aff_from_domain(set
);
5913 *upma
= isl_union_pw_multi_aff_add_pw_multi_aff(*upma
, pma
);
5918 /* Return an isl_union_pw_multi_aff with the given "uset" as domain and
5919 * an unnamed zero-dimensional range.
5921 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_domain(
5922 __isl_take isl_union_set
*uset
)
5925 isl_union_pw_multi_aff
*upma
;
5930 space
= isl_union_set_get_space(uset
);
5931 upma
= isl_union_pw_multi_aff_empty(space
);
5933 if (isl_union_set_foreach_set(uset
,
5934 &add_pw_multi_aff_from_domain
, &upma
) < 0)
5937 isl_union_set_free(uset
);
5940 isl_union_set_free(uset
);
5941 isl_union_pw_multi_aff_free(upma
);
5945 /* Convert "pma" to an isl_map and add it to *umap.
5947 static isl_stat
map_from_pw_multi_aff(__isl_take isl_pw_multi_aff
*pma
,
5950 isl_union_map
**umap
= user
;
5953 map
= isl_map_from_pw_multi_aff(pma
);
5954 *umap
= isl_union_map_add_map(*umap
, map
);
5959 /* Construct a union map mapping the domain of the union
5960 * piecewise multi-affine expression to its range, with each dimension
5961 * in the range equated to the corresponding affine expression on its cell.
5963 __isl_give isl_union_map
*isl_union_map_from_union_pw_multi_aff(
5964 __isl_take isl_union_pw_multi_aff
*upma
)
5967 isl_union_map
*umap
;
5972 space
= isl_union_pw_multi_aff_get_space(upma
);
5973 umap
= isl_union_map_empty(space
);
5975 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma
,
5976 &map_from_pw_multi_aff
, &umap
) < 0)
5979 isl_union_pw_multi_aff_free(upma
);
5982 isl_union_pw_multi_aff_free(upma
);
5983 isl_union_map_free(umap
);
5987 /* Local data for bin_entry and the callback "fn".
5989 struct isl_union_pw_multi_aff_bin_data
{
5990 isl_union_pw_multi_aff
*upma2
;
5991 isl_union_pw_multi_aff
*res
;
5992 isl_pw_multi_aff
*pma
;
5993 isl_stat (*fn
)(__isl_take isl_pw_multi_aff
*pma
, void *user
);
5996 /* Given an isl_pw_multi_aff from upma1, store it in data->pma
5997 * and call data->fn for each isl_pw_multi_aff in data->upma2.
5999 static isl_stat
bin_entry(__isl_take isl_pw_multi_aff
*pma
, void *user
)
6001 struct isl_union_pw_multi_aff_bin_data
*data
= user
;
6005 r
= isl_union_pw_multi_aff_foreach_pw_multi_aff(data
->upma2
,
6007 isl_pw_multi_aff_free(pma
);
6012 /* Call "fn" on each pair of isl_pw_multi_affs in "upma1" and "upma2".
6013 * The isl_pw_multi_aff from upma1 is stored in data->pma (where data is
6014 * passed as user field) and the isl_pw_multi_aff from upma2 is available
6015 * as *entry. The callback should adjust data->res if desired.
6017 static __isl_give isl_union_pw_multi_aff
*bin_op(
6018 __isl_take isl_union_pw_multi_aff
*upma1
,
6019 __isl_take isl_union_pw_multi_aff
*upma2
,
6020 isl_stat (*fn
)(__isl_take isl_pw_multi_aff
*pma
, void *user
))
6023 struct isl_union_pw_multi_aff_bin_data data
= { NULL
, NULL
, NULL
, fn
};
6025 space
= isl_union_pw_multi_aff_get_space(upma2
);
6026 upma1
= isl_union_pw_multi_aff_align_params(upma1
, space
);
6027 space
= isl_union_pw_multi_aff_get_space(upma1
);
6028 upma2
= isl_union_pw_multi_aff_align_params(upma2
, space
);
6030 if (!upma1
|| !upma2
)
6034 data
.res
= isl_union_pw_multi_aff_alloc_same_size(upma1
);
6035 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma1
,
6036 &bin_entry
, &data
) < 0)
6039 isl_union_pw_multi_aff_free(upma1
);
6040 isl_union_pw_multi_aff_free(upma2
);
6043 isl_union_pw_multi_aff_free(upma1
);
6044 isl_union_pw_multi_aff_free(upma2
);
6045 isl_union_pw_multi_aff_free(data
.res
);
6049 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
6050 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
6052 static __isl_give isl_pw_multi_aff
*pw_multi_aff_range_product(
6053 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
6057 space
= isl_space_range_product(isl_pw_multi_aff_get_space(pma1
),
6058 isl_pw_multi_aff_get_space(pma2
));
6059 return isl_pw_multi_aff_on_shared_domain_in(pma1
, pma2
, space
,
6060 &isl_multi_aff_range_product
);
6063 /* Given two isl_pw_multi_affs A -> B and C -> D,
6064 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
6066 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_range_product(
6067 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
6069 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
6070 &pw_multi_aff_range_product
);
6073 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
6074 * construct an isl_pw_multi_aff (A * C) -> (B, D).
6076 static __isl_give isl_pw_multi_aff
*pw_multi_aff_flat_range_product(
6077 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
6081 space
= isl_space_range_product(isl_pw_multi_aff_get_space(pma1
),
6082 isl_pw_multi_aff_get_space(pma2
));
6083 space
= isl_space_flatten_range(space
);
6084 return isl_pw_multi_aff_on_shared_domain_in(pma1
, pma2
, space
,
6085 &isl_multi_aff_flat_range_product
);
6088 /* Given two isl_pw_multi_affs A -> B and C -> D,
6089 * construct an isl_pw_multi_aff (A * C) -> (B, D).
6091 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_flat_range_product(
6092 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
6094 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
6095 &pw_multi_aff_flat_range_product
);
6098 /* If data->pma and "pma2" have the same domain space, then compute
6099 * their flat range product and the result to data->res.
6101 static isl_stat
flat_range_product_entry(__isl_take isl_pw_multi_aff
*pma2
,
6104 struct isl_union_pw_multi_aff_bin_data
*data
= user
;
6106 if (!isl_space_tuple_is_equal(data
->pma
->dim
, isl_dim_in
,
6107 pma2
->dim
, isl_dim_in
)) {
6108 isl_pw_multi_aff_free(pma2
);
6112 pma2
= isl_pw_multi_aff_flat_range_product(
6113 isl_pw_multi_aff_copy(data
->pma
), pma2
);
6115 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma2
);
6120 /* Given two isl_union_pw_multi_affs A -> B and C -> D,
6121 * construct an isl_union_pw_multi_aff (A * C) -> (B, D).
6123 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_flat_range_product(
6124 __isl_take isl_union_pw_multi_aff
*upma1
,
6125 __isl_take isl_union_pw_multi_aff
*upma2
)
6127 return bin_op(upma1
, upma2
, &flat_range_product_entry
);
6130 /* Replace the affine expressions at position "pos" in "pma" by "pa".
6131 * The parameters are assumed to have been aligned.
6133 * The implementation essentially performs an isl_pw_*_on_shared_domain,
6134 * except that it works on two different isl_pw_* types.
6136 static __isl_give isl_pw_multi_aff
*pw_multi_aff_set_pw_aff(
6137 __isl_take isl_pw_multi_aff
*pma
, unsigned pos
,
6138 __isl_take isl_pw_aff
*pa
)
6141 isl_pw_multi_aff
*res
= NULL
;
6146 if (!isl_space_tuple_is_equal(pma
->dim
, isl_dim_in
,
6147 pa
->dim
, isl_dim_in
))
6148 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
6149 "domains don't match", goto error
);
6150 if (pos
>= isl_pw_multi_aff_dim(pma
, isl_dim_out
))
6151 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
6152 "index out of bounds", goto error
);
6155 res
= isl_pw_multi_aff_alloc_size(isl_pw_multi_aff_get_space(pma
), n
);
6157 for (i
= 0; i
< pma
->n
; ++i
) {
6158 for (j
= 0; j
< pa
->n
; ++j
) {
6160 isl_multi_aff
*res_ij
;
6163 common
= isl_set_intersect(isl_set_copy(pma
->p
[i
].set
),
6164 isl_set_copy(pa
->p
[j
].set
));
6165 empty
= isl_set_plain_is_empty(common
);
6166 if (empty
< 0 || empty
) {
6167 isl_set_free(common
);
6173 res_ij
= isl_multi_aff_set_aff(
6174 isl_multi_aff_copy(pma
->p
[i
].maff
), pos
,
6175 isl_aff_copy(pa
->p
[j
].aff
));
6176 res_ij
= isl_multi_aff_gist(res_ij
,
6177 isl_set_copy(common
));
6179 res
= isl_pw_multi_aff_add_piece(res
, common
, res_ij
);
6183 isl_pw_multi_aff_free(pma
);
6184 isl_pw_aff_free(pa
);
6187 isl_pw_multi_aff_free(pma
);
6188 isl_pw_aff_free(pa
);
6189 return isl_pw_multi_aff_free(res
);
6192 /* Replace the affine expressions at position "pos" in "pma" by "pa".
6194 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_set_pw_aff(
6195 __isl_take isl_pw_multi_aff
*pma
, unsigned pos
,
6196 __isl_take isl_pw_aff
*pa
)
6198 isl_bool equal_params
;
6202 equal_params
= isl_space_has_equal_params(pma
->dim
, pa
->dim
);
6203 if (equal_params
< 0)
6206 return pw_multi_aff_set_pw_aff(pma
, pos
, pa
);
6207 if (!isl_space_has_named_params(pma
->dim
) ||
6208 !isl_space_has_named_params(pa
->dim
))
6209 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
6210 "unaligned unnamed parameters", goto error
);
6211 pma
= isl_pw_multi_aff_align_params(pma
, isl_pw_aff_get_space(pa
));
6212 pa
= isl_pw_aff_align_params(pa
, isl_pw_multi_aff_get_space(pma
));
6213 return pw_multi_aff_set_pw_aff(pma
, pos
, pa
);
6215 isl_pw_multi_aff_free(pma
);
6216 isl_pw_aff_free(pa
);
6220 /* Do the parameters of "pa" match those of "space"?
6222 isl_bool
isl_pw_aff_matching_params(__isl_keep isl_pw_aff
*pa
,
6223 __isl_keep isl_space
*space
)
6225 isl_space
*pa_space
;
6229 return isl_bool_error
;
6231 pa_space
= isl_pw_aff_get_space(pa
);
6233 match
= isl_space_has_equal_params(space
, pa_space
);
6235 isl_space_free(pa_space
);
6239 /* Check that the domain space of "pa" matches "space".
6241 isl_stat
isl_pw_aff_check_match_domain_space(__isl_keep isl_pw_aff
*pa
,
6242 __isl_keep isl_space
*space
)
6244 isl_space
*pa_space
;
6248 return isl_stat_error
;
6250 pa_space
= isl_pw_aff_get_space(pa
);
6252 match
= isl_space_has_equal_params(space
, pa_space
);
6256 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
6257 "parameters don't match", goto error
);
6258 match
= isl_space_tuple_is_equal(space
, isl_dim_in
,
6259 pa_space
, isl_dim_in
);
6263 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
6264 "domains don't match", goto error
);
6265 isl_space_free(pa_space
);
6268 isl_space_free(pa_space
);
6269 return isl_stat_error
;
6277 #include <isl_multi_templ.c>
6278 #include <isl_multi_apply_set.c>
6279 #include <isl_multi_coalesce.c>
6280 #include <isl_multi_dims.c>
6281 #include <isl_multi_gist.c>
6282 #include <isl_multi_hash.c>
6283 #include <isl_multi_intersect.c>
6285 /* Scale the elements of "pma" by the corresponding elements of "mv".
6287 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_scale_multi_val(
6288 __isl_take isl_pw_multi_aff
*pma
, __isl_take isl_multi_val
*mv
)
6291 isl_bool equal_params
;
6293 pma
= isl_pw_multi_aff_cow(pma
);
6296 if (!isl_space_tuple_is_equal(pma
->dim
, isl_dim_out
,
6297 mv
->space
, isl_dim_set
))
6298 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
6299 "spaces don't match", goto error
);
6300 equal_params
= isl_space_has_equal_params(pma
->dim
, mv
->space
);
6301 if (equal_params
< 0)
6303 if (!equal_params
) {
6304 pma
= isl_pw_multi_aff_align_params(pma
,
6305 isl_multi_val_get_space(mv
));
6306 mv
= isl_multi_val_align_params(mv
,
6307 isl_pw_multi_aff_get_space(pma
));
6312 for (i
= 0; i
< pma
->n
; ++i
) {
6313 pma
->p
[i
].maff
= isl_multi_aff_scale_multi_val(pma
->p
[i
].maff
,
6314 isl_multi_val_copy(mv
));
6315 if (!pma
->p
[i
].maff
)
6319 isl_multi_val_free(mv
);
6322 isl_multi_val_free(mv
);
6323 isl_pw_multi_aff_free(pma
);
6327 /* This function is called for each entry of an isl_union_pw_multi_aff.
6328 * If the space of the entry matches that of data->mv,
6329 * then apply isl_pw_multi_aff_scale_multi_val and return the result.
6330 * Otherwise, return an empty isl_pw_multi_aff.
6332 static __isl_give isl_pw_multi_aff
*union_pw_multi_aff_scale_multi_val_entry(
6333 __isl_take isl_pw_multi_aff
*pma
, void *user
)
6335 isl_multi_val
*mv
= user
;
6339 if (!isl_space_tuple_is_equal(pma
->dim
, isl_dim_out
,
6340 mv
->space
, isl_dim_set
)) {
6341 isl_space
*space
= isl_pw_multi_aff_get_space(pma
);
6342 isl_pw_multi_aff_free(pma
);
6343 return isl_pw_multi_aff_empty(space
);
6346 return isl_pw_multi_aff_scale_multi_val(pma
, isl_multi_val_copy(mv
));
6349 /* Scale the elements of "upma" by the corresponding elements of "mv",
6350 * for those entries that match the space of "mv".
6352 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_scale_multi_val(
6353 __isl_take isl_union_pw_multi_aff
*upma
, __isl_take isl_multi_val
*mv
)
6355 upma
= isl_union_pw_multi_aff_align_params(upma
,
6356 isl_multi_val_get_space(mv
));
6357 mv
= isl_multi_val_align_params(mv
,
6358 isl_union_pw_multi_aff_get_space(upma
));
6362 return isl_union_pw_multi_aff_transform(upma
,
6363 &union_pw_multi_aff_scale_multi_val_entry
, mv
);
6365 isl_multi_val_free(mv
);
6368 isl_multi_val_free(mv
);
6369 isl_union_pw_multi_aff_free(upma
);
6373 /* Construct and return a piecewise multi affine expression
6374 * in the given space with value zero in each of the output dimensions and
6375 * a universe domain.
6377 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_zero(__isl_take isl_space
*space
)
6379 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_zero(space
));
6382 /* Construct and return a piecewise multi affine expression
6383 * that is equal to the given piecewise affine expression.
6385 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_pw_aff(
6386 __isl_take isl_pw_aff
*pa
)
6390 isl_pw_multi_aff
*pma
;
6395 space
= isl_pw_aff_get_space(pa
);
6396 pma
= isl_pw_multi_aff_alloc_size(space
, pa
->n
);
6398 for (i
= 0; i
< pa
->n
; ++i
) {
6402 set
= isl_set_copy(pa
->p
[i
].set
);
6403 ma
= isl_multi_aff_from_aff(isl_aff_copy(pa
->p
[i
].aff
));
6404 pma
= isl_pw_multi_aff_add_piece(pma
, set
, ma
);
6407 isl_pw_aff_free(pa
);
6411 /* Construct a set or map mapping the shared (parameter) domain
6412 * of the piecewise affine expressions to the range of "mpa"
6413 * with each dimension in the range equated to the
6414 * corresponding piecewise affine expression.
6416 static __isl_give isl_map
*map_from_multi_pw_aff(
6417 __isl_take isl_multi_pw_aff
*mpa
)
6426 if (isl_space_dim(mpa
->space
, isl_dim_out
) != mpa
->n
)
6427 isl_die(isl_multi_pw_aff_get_ctx(mpa
), isl_error_internal
,
6428 "invalid space", goto error
);
6430 space
= isl_multi_pw_aff_get_domain_space(mpa
);
6431 map
= isl_map_universe(isl_space_from_domain(space
));
6433 for (i
= 0; i
< mpa
->n
; ++i
) {
6437 pa
= isl_pw_aff_copy(mpa
->p
[i
]);
6438 map_i
= map_from_pw_aff(pa
);
6440 map
= isl_map_flat_range_product(map
, map_i
);
6443 map
= isl_map_reset_space(map
, isl_multi_pw_aff_get_space(mpa
));
6445 isl_multi_pw_aff_free(mpa
);
6448 isl_multi_pw_aff_free(mpa
);
6452 /* Construct a map mapping the shared domain
6453 * of the piecewise affine expressions to the range of "mpa"
6454 * with each dimension in the range equated to the
6455 * corresponding piecewise affine expression.
6457 __isl_give isl_map
*isl_map_from_multi_pw_aff(__isl_take isl_multi_pw_aff
*mpa
)
6461 if (isl_space_is_set(mpa
->space
))
6462 isl_die(isl_multi_pw_aff_get_ctx(mpa
), isl_error_internal
,
6463 "space of input is not a map", goto error
);
6465 return map_from_multi_pw_aff(mpa
);
6467 isl_multi_pw_aff_free(mpa
);
6471 /* Construct a set mapping the shared parameter domain
6472 * of the piecewise affine expressions to the space of "mpa"
6473 * with each dimension in the range equated to the
6474 * corresponding piecewise affine expression.
6476 __isl_give isl_set
*isl_set_from_multi_pw_aff(__isl_take isl_multi_pw_aff
*mpa
)
6480 if (!isl_space_is_set(mpa
->space
))
6481 isl_die(isl_multi_pw_aff_get_ctx(mpa
), isl_error_internal
,
6482 "space of input is not a set", goto error
);
6484 return map_from_multi_pw_aff(mpa
);
6486 isl_multi_pw_aff_free(mpa
);
6490 /* Construct and return a piecewise multi affine expression
6491 * that is equal to the given multi piecewise affine expression
6492 * on the shared domain of the piecewise affine expressions.
6494 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_multi_pw_aff(
6495 __isl_take isl_multi_pw_aff
*mpa
)
6500 isl_pw_multi_aff
*pma
;
6505 space
= isl_multi_pw_aff_get_space(mpa
);
6508 isl_multi_pw_aff_free(mpa
);
6509 return isl_pw_multi_aff_zero(space
);
6512 pa
= isl_multi_pw_aff_get_pw_aff(mpa
, 0);
6513 pma
= isl_pw_multi_aff_from_pw_aff(pa
);
6515 for (i
= 1; i
< mpa
->n
; ++i
) {
6516 isl_pw_multi_aff
*pma_i
;
6518 pa
= isl_multi_pw_aff_get_pw_aff(mpa
, i
);
6519 pma_i
= isl_pw_multi_aff_from_pw_aff(pa
);
6520 pma
= isl_pw_multi_aff_range_product(pma
, pma_i
);
6523 pma
= isl_pw_multi_aff_reset_space(pma
, space
);
6525 isl_multi_pw_aff_free(mpa
);
6529 /* Construct and return a multi piecewise affine expression
6530 * that is equal to the given multi affine expression.
6532 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_from_multi_aff(
6533 __isl_take isl_multi_aff
*ma
)
6536 isl_multi_pw_aff
*mpa
;
6541 n
= isl_multi_aff_dim(ma
, isl_dim_out
);
6542 mpa
= isl_multi_pw_aff_alloc(isl_multi_aff_get_space(ma
));
6544 for (i
= 0; i
< n
; ++i
) {
6547 pa
= isl_pw_aff_from_aff(isl_multi_aff_get_aff(ma
, i
));
6548 mpa
= isl_multi_pw_aff_set_pw_aff(mpa
, i
, pa
);
6551 isl_multi_aff_free(ma
);
6555 /* Construct and return a multi piecewise affine expression
6556 * that is equal to the given piecewise multi affine expression.
6558 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_from_pw_multi_aff(
6559 __isl_take isl_pw_multi_aff
*pma
)
6563 isl_multi_pw_aff
*mpa
;
6568 n
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
6569 space
= isl_pw_multi_aff_get_space(pma
);
6570 mpa
= isl_multi_pw_aff_alloc(space
);
6572 for (i
= 0; i
< n
; ++i
) {
6575 pa
= isl_pw_multi_aff_get_pw_aff(pma
, i
);
6576 mpa
= isl_multi_pw_aff_set_pw_aff(mpa
, i
, pa
);
6579 isl_pw_multi_aff_free(pma
);
6583 /* Do "pa1" and "pa2" represent the same function?
6585 * We first check if they are obviously equal.
6586 * If not, we convert them to maps and check if those are equal.
6588 * If "pa1" or "pa2" contain any NaNs, then they are considered
6589 * not to be the same. A NaN is not equal to anything, not even
6592 isl_bool
isl_pw_aff_is_equal(__isl_keep isl_pw_aff
*pa1
,
6593 __isl_keep isl_pw_aff
*pa2
)
6597 isl_map
*map1
, *map2
;
6600 return isl_bool_error
;
6602 equal
= isl_pw_aff_plain_is_equal(pa1
, pa2
);
6603 if (equal
< 0 || equal
)
6605 has_nan
= either_involves_nan(pa1
, pa2
);
6607 return isl_bool_error
;
6609 return isl_bool_false
;
6611 map1
= map_from_pw_aff(isl_pw_aff_copy(pa1
));
6612 map2
= map_from_pw_aff(isl_pw_aff_copy(pa2
));
6613 equal
= isl_map_is_equal(map1
, map2
);
6620 /* Do "mpa1" and "mpa2" represent the same function?
6622 * Note that we cannot convert the entire isl_multi_pw_aff
6623 * to a map because the domains of the piecewise affine expressions
6624 * may not be the same.
6626 isl_bool
isl_multi_pw_aff_is_equal(__isl_keep isl_multi_pw_aff
*mpa1
,
6627 __isl_keep isl_multi_pw_aff
*mpa2
)
6630 isl_bool equal
, equal_params
;
6633 return isl_bool_error
;
6635 equal_params
= isl_space_has_equal_params(mpa1
->space
, mpa2
->space
);
6636 if (equal_params
< 0)
6637 return isl_bool_error
;
6638 if (!equal_params
) {
6639 if (!isl_space_has_named_params(mpa1
->space
))
6640 return isl_bool_false
;
6641 if (!isl_space_has_named_params(mpa2
->space
))
6642 return isl_bool_false
;
6643 mpa1
= isl_multi_pw_aff_copy(mpa1
);
6644 mpa2
= isl_multi_pw_aff_copy(mpa2
);
6645 mpa1
= isl_multi_pw_aff_align_params(mpa1
,
6646 isl_multi_pw_aff_get_space(mpa2
));
6647 mpa2
= isl_multi_pw_aff_align_params(mpa2
,
6648 isl_multi_pw_aff_get_space(mpa1
));
6649 equal
= isl_multi_pw_aff_is_equal(mpa1
, mpa2
);
6650 isl_multi_pw_aff_free(mpa1
);
6651 isl_multi_pw_aff_free(mpa2
);
6655 equal
= isl_space_is_equal(mpa1
->space
, mpa2
->space
);
6656 if (equal
< 0 || !equal
)
6659 for (i
= 0; i
< mpa1
->n
; ++i
) {
6660 equal
= isl_pw_aff_is_equal(mpa1
->p
[i
], mpa2
->p
[i
]);
6661 if (equal
< 0 || !equal
)
6665 return isl_bool_true
;
6668 /* Do "pma1" and "pma2" represent the same function?
6670 * First check if they are obviously equal.
6671 * If not, then convert them to maps and check if those are equal.
6673 * If "pa1" or "pa2" contain any NaNs, then they are considered
6674 * not to be the same. A NaN is not equal to anything, not even
6677 isl_bool
isl_pw_multi_aff_is_equal(__isl_keep isl_pw_multi_aff
*pma1
,
6678 __isl_keep isl_pw_multi_aff
*pma2
)
6682 isl_map
*map1
, *map2
;
6685 return isl_bool_error
;
6687 equal
= isl_pw_multi_aff_plain_is_equal(pma1
, pma2
);
6688 if (equal
< 0 || equal
)
6690 has_nan
= isl_pw_multi_aff_involves_nan(pma1
);
6691 if (has_nan
>= 0 && !has_nan
)
6692 has_nan
= isl_pw_multi_aff_involves_nan(pma2
);
6693 if (has_nan
< 0 || has_nan
)
6694 return isl_bool_not(has_nan
);
6696 map1
= isl_map_from_pw_multi_aff(isl_pw_multi_aff_copy(pma1
));
6697 map2
= isl_map_from_pw_multi_aff(isl_pw_multi_aff_copy(pma2
));
6698 equal
= isl_map_is_equal(map1
, map2
);
6705 /* Compute the pullback of "mpa" by the function represented by "ma".
6706 * In other words, plug in "ma" in "mpa".
6708 * The parameters of "mpa" and "ma" are assumed to have been aligned.
6710 static __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_pullback_multi_aff_aligned(
6711 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_multi_aff
*ma
)
6714 isl_space
*space
= NULL
;
6716 mpa
= isl_multi_pw_aff_cow(mpa
);
6720 space
= isl_space_join(isl_multi_aff_get_space(ma
),
6721 isl_multi_pw_aff_get_space(mpa
));
6725 for (i
= 0; i
< mpa
->n
; ++i
) {
6726 mpa
->p
[i
] = isl_pw_aff_pullback_multi_aff(mpa
->p
[i
],
6727 isl_multi_aff_copy(ma
));
6732 isl_multi_aff_free(ma
);
6733 isl_space_free(mpa
->space
);
6737 isl_space_free(space
);
6738 isl_multi_pw_aff_free(mpa
);
6739 isl_multi_aff_free(ma
);
6743 /* Compute the pullback of "mpa" by the function represented by "ma".
6744 * In other words, plug in "ma" in "mpa".
6746 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_pullback_multi_aff(
6747 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_multi_aff
*ma
)
6749 isl_bool equal_params
;
6753 equal_params
= isl_space_has_equal_params(mpa
->space
, ma
->space
);
6754 if (equal_params
< 0)
6757 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa
, ma
);
6758 mpa
= isl_multi_pw_aff_align_params(mpa
, isl_multi_aff_get_space(ma
));
6759 ma
= isl_multi_aff_align_params(ma
, isl_multi_pw_aff_get_space(mpa
));
6760 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa
, ma
);
6762 isl_multi_pw_aff_free(mpa
);
6763 isl_multi_aff_free(ma
);
6767 /* Compute the pullback of "mpa" by the function represented by "pma".
6768 * In other words, plug in "pma" in "mpa".
6770 * The parameters of "mpa" and "mpa" are assumed to have been aligned.
6772 static __isl_give isl_multi_pw_aff
*
6773 isl_multi_pw_aff_pullback_pw_multi_aff_aligned(
6774 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_pw_multi_aff
*pma
)
6777 isl_space
*space
= NULL
;
6779 mpa
= isl_multi_pw_aff_cow(mpa
);
6783 space
= isl_space_join(isl_pw_multi_aff_get_space(pma
),
6784 isl_multi_pw_aff_get_space(mpa
));
6786 for (i
= 0; i
< mpa
->n
; ++i
) {
6787 mpa
->p
[i
] = isl_pw_aff_pullback_pw_multi_aff_aligned(mpa
->p
[i
],
6788 isl_pw_multi_aff_copy(pma
));
6793 isl_pw_multi_aff_free(pma
);
6794 isl_space_free(mpa
->space
);
6798 isl_space_free(space
);
6799 isl_multi_pw_aff_free(mpa
);
6800 isl_pw_multi_aff_free(pma
);
6804 /* Compute the pullback of "mpa" by the function represented by "pma".
6805 * In other words, plug in "pma" in "mpa".
6807 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_pullback_pw_multi_aff(
6808 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_pw_multi_aff
*pma
)
6810 isl_bool equal_params
;
6814 equal_params
= isl_space_has_equal_params(mpa
->space
, pma
->dim
);
6815 if (equal_params
< 0)
6818 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa
, pma
);
6819 mpa
= isl_multi_pw_aff_align_params(mpa
,
6820 isl_pw_multi_aff_get_space(pma
));
6821 pma
= isl_pw_multi_aff_align_params(pma
,
6822 isl_multi_pw_aff_get_space(mpa
));
6823 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa
, pma
);
6825 isl_multi_pw_aff_free(mpa
);
6826 isl_pw_multi_aff_free(pma
);
6830 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
6831 * with the domain of "aff". The domain of the result is the same
6833 * "mpa" and "aff" are assumed to have been aligned.
6835 * We first extract the parametric constant from "aff", defined
6836 * over the correct domain.
6837 * Then we add the appropriate combinations of the members of "mpa".
6838 * Finally, we add the integer divisions through recursive calls.
6840 static __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_aff_aligned(
6841 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_aff
*aff
)
6849 n_in
= isl_aff_dim(aff
, isl_dim_in
);
6850 n_div
= isl_aff_dim(aff
, isl_dim_div
);
6852 space
= isl_space_domain(isl_multi_pw_aff_get_space(mpa
));
6853 tmp
= isl_aff_copy(aff
);
6854 tmp
= isl_aff_drop_dims(tmp
, isl_dim_div
, 0, n_div
);
6855 tmp
= isl_aff_drop_dims(tmp
, isl_dim_in
, 0, n_in
);
6856 tmp
= isl_aff_add_dims(tmp
, isl_dim_in
,
6857 isl_space_dim(space
, isl_dim_set
));
6858 tmp
= isl_aff_reset_domain_space(tmp
, space
);
6859 pa
= isl_pw_aff_from_aff(tmp
);
6861 for (i
= 0; i
< n_in
; ++i
) {
6864 if (!isl_aff_involves_dims(aff
, isl_dim_in
, i
, 1))
6866 v
= isl_aff_get_coefficient_val(aff
, isl_dim_in
, i
);
6867 pa_i
= isl_multi_pw_aff_get_pw_aff(mpa
, i
);
6868 pa_i
= isl_pw_aff_scale_val(pa_i
, v
);
6869 pa
= isl_pw_aff_add(pa
, pa_i
);
6872 for (i
= 0; i
< n_div
; ++i
) {
6876 if (!isl_aff_involves_dims(aff
, isl_dim_div
, i
, 1))
6878 div
= isl_aff_get_div(aff
, i
);
6879 pa_i
= isl_multi_pw_aff_apply_aff_aligned(
6880 isl_multi_pw_aff_copy(mpa
), div
);
6881 pa_i
= isl_pw_aff_floor(pa_i
);
6882 v
= isl_aff_get_coefficient_val(aff
, isl_dim_div
, i
);
6883 pa_i
= isl_pw_aff_scale_val(pa_i
, v
);
6884 pa
= isl_pw_aff_add(pa
, pa_i
);
6887 isl_multi_pw_aff_free(mpa
);
6893 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
6894 * with the domain of "aff". The domain of the result is the same
6897 __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_aff(
6898 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_aff
*aff
)
6900 isl_bool equal_params
;
6904 equal_params
= isl_space_has_equal_params(aff
->ls
->dim
, mpa
->space
);
6905 if (equal_params
< 0)
6908 return isl_multi_pw_aff_apply_aff_aligned(mpa
, aff
);
6910 aff
= isl_aff_align_params(aff
, isl_multi_pw_aff_get_space(mpa
));
6911 mpa
= isl_multi_pw_aff_align_params(mpa
, isl_aff_get_space(aff
));
6913 return isl_multi_pw_aff_apply_aff_aligned(mpa
, aff
);
6916 isl_multi_pw_aff_free(mpa
);
6920 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
6921 * with the domain of "pa". The domain of the result is the same
6923 * "mpa" and "pa" are assumed to have been aligned.
6925 * We consider each piece in turn. Note that the domains of the
6926 * pieces are assumed to be disjoint and they remain disjoint
6927 * after taking the preimage (over the same function).
6929 static __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_pw_aff_aligned(
6930 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_pw_aff
*pa
)
6939 space
= isl_space_join(isl_multi_pw_aff_get_space(mpa
),
6940 isl_pw_aff_get_space(pa
));
6941 res
= isl_pw_aff_empty(space
);
6943 for (i
= 0; i
< pa
->n
; ++i
) {
6947 pa_i
= isl_multi_pw_aff_apply_aff_aligned(
6948 isl_multi_pw_aff_copy(mpa
),
6949 isl_aff_copy(pa
->p
[i
].aff
));
6950 domain
= isl_set_copy(pa
->p
[i
].set
);
6951 domain
= isl_set_preimage_multi_pw_aff(domain
,
6952 isl_multi_pw_aff_copy(mpa
));
6953 pa_i
= isl_pw_aff_intersect_domain(pa_i
, domain
);
6954 res
= isl_pw_aff_add_disjoint(res
, pa_i
);
6957 isl_pw_aff_free(pa
);
6958 isl_multi_pw_aff_free(mpa
);
6961 isl_pw_aff_free(pa
);
6962 isl_multi_pw_aff_free(mpa
);
6966 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
6967 * with the domain of "pa". The domain of the result is the same
6970 __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_pw_aff(
6971 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_pw_aff
*pa
)
6973 isl_bool equal_params
;
6977 equal_params
= isl_space_has_equal_params(pa
->dim
, mpa
->space
);
6978 if (equal_params
< 0)
6981 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa
, pa
);
6983 pa
= isl_pw_aff_align_params(pa
, isl_multi_pw_aff_get_space(mpa
));
6984 mpa
= isl_multi_pw_aff_align_params(mpa
, isl_pw_aff_get_space(pa
));
6986 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa
, pa
);
6988 isl_pw_aff_free(pa
);
6989 isl_multi_pw_aff_free(mpa
);
6993 /* Compute the pullback of "pa" by the function represented by "mpa".
6994 * In other words, plug in "mpa" in "pa".
6995 * "pa" and "mpa" are assumed to have been aligned.
6997 * The pullback is computed by applying "pa" to "mpa".
6999 static __isl_give isl_pw_aff
*isl_pw_aff_pullback_multi_pw_aff_aligned(
7000 __isl_take isl_pw_aff
*pa
, __isl_take isl_multi_pw_aff
*mpa
)
7002 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa
, pa
);
7005 /* Compute the pullback of "pa" by the function represented by "mpa".
7006 * In other words, plug in "mpa" in "pa".
7008 * The pullback is computed by applying "pa" to "mpa".
7010 __isl_give isl_pw_aff
*isl_pw_aff_pullback_multi_pw_aff(
7011 __isl_take isl_pw_aff
*pa
, __isl_take isl_multi_pw_aff
*mpa
)
7013 return isl_multi_pw_aff_apply_pw_aff(mpa
, pa
);
7016 /* Compute the pullback of "mpa1" by the function represented by "mpa2".
7017 * In other words, plug in "mpa2" in "mpa1".
7019 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7021 * We pullback each member of "mpa1" in turn.
7023 static __isl_give isl_multi_pw_aff
*
7024 isl_multi_pw_aff_pullback_multi_pw_aff_aligned(
7025 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
)
7028 isl_space
*space
= NULL
;
7030 mpa1
= isl_multi_pw_aff_cow(mpa1
);
7034 space
= isl_space_join(isl_multi_pw_aff_get_space(mpa2
),
7035 isl_multi_pw_aff_get_space(mpa1
));
7037 for (i
= 0; i
< mpa1
->n
; ++i
) {
7038 mpa1
->p
[i
] = isl_pw_aff_pullback_multi_pw_aff_aligned(
7039 mpa1
->p
[i
], isl_multi_pw_aff_copy(mpa2
));
7044 mpa1
= isl_multi_pw_aff_reset_space(mpa1
, space
);
7046 isl_multi_pw_aff_free(mpa2
);
7049 isl_space_free(space
);
7050 isl_multi_pw_aff_free(mpa1
);
7051 isl_multi_pw_aff_free(mpa2
);
7055 /* Compute the pullback of "mpa1" by the function represented by "mpa2".
7056 * In other words, plug in "mpa2" in "mpa1".
7058 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_pullback_multi_pw_aff(
7059 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
)
7061 return isl_multi_pw_aff_align_params_multi_multi_and(mpa1
, mpa2
,
7062 &isl_multi_pw_aff_pullback_multi_pw_aff_aligned
);
7065 /* Align the parameters of "mpa1" and "mpa2", check that the ranges
7066 * of "mpa1" and "mpa2" live in the same space, construct map space
7067 * between the domain spaces of "mpa1" and "mpa2" and call "order"
7068 * with this map space as extract argument.
7070 static __isl_give isl_map
*isl_multi_pw_aff_order_map(
7071 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
,
7072 __isl_give isl_map
*(*order
)(__isl_keep isl_multi_pw_aff
*mpa1
,
7073 __isl_keep isl_multi_pw_aff
*mpa2
, __isl_take isl_space
*space
))
7076 isl_space
*space1
, *space2
;
7079 mpa1
= isl_multi_pw_aff_align_params(mpa1
,
7080 isl_multi_pw_aff_get_space(mpa2
));
7081 mpa2
= isl_multi_pw_aff_align_params(mpa2
,
7082 isl_multi_pw_aff_get_space(mpa1
));
7085 match
= isl_space_tuple_is_equal(mpa1
->space
, isl_dim_out
,
7086 mpa2
->space
, isl_dim_out
);
7090 isl_die(isl_multi_pw_aff_get_ctx(mpa1
), isl_error_invalid
,
7091 "range spaces don't match", goto error
);
7092 space1
= isl_space_domain(isl_multi_pw_aff_get_space(mpa1
));
7093 space2
= isl_space_domain(isl_multi_pw_aff_get_space(mpa2
));
7094 space1
= isl_space_map_from_domain_and_range(space1
, space2
);
7096 res
= order(mpa1
, mpa2
, space1
);
7097 isl_multi_pw_aff_free(mpa1
);
7098 isl_multi_pw_aff_free(mpa2
);
7101 isl_multi_pw_aff_free(mpa1
);
7102 isl_multi_pw_aff_free(mpa2
);
7106 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7107 * where the function values are equal. "space" is the space of the result.
7108 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7110 * "mpa1" and "mpa2" are equal when each of the pairs of elements
7111 * in the sequences are equal.
7113 static __isl_give isl_map
*isl_multi_pw_aff_eq_map_on_space(
7114 __isl_keep isl_multi_pw_aff
*mpa1
, __isl_keep isl_multi_pw_aff
*mpa2
,
7115 __isl_take isl_space
*space
)
7120 res
= isl_map_universe(space
);
7122 n
= isl_multi_pw_aff_dim(mpa1
, isl_dim_out
);
7123 for (i
= 0; i
< n
; ++i
) {
7124 isl_pw_aff
*pa1
, *pa2
;
7127 pa1
= isl_multi_pw_aff_get_pw_aff(mpa1
, i
);
7128 pa2
= isl_multi_pw_aff_get_pw_aff(mpa2
, i
);
7129 map
= isl_pw_aff_eq_map(pa1
, pa2
);
7130 res
= isl_map_intersect(res
, map
);
7136 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7137 * where the function values are equal.
7139 __isl_give isl_map
*isl_multi_pw_aff_eq_map(__isl_take isl_multi_pw_aff
*mpa1
,
7140 __isl_take isl_multi_pw_aff
*mpa2
)
7142 return isl_multi_pw_aff_order_map(mpa1
, mpa2
,
7143 &isl_multi_pw_aff_eq_map_on_space
);
7146 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7147 * where the function values of "mpa1" is lexicographically satisfies "base"
7148 * compared to that of "mpa2". "space" is the space of the result.
7149 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7151 * "mpa1" lexicographically satisfies "base" compared to "mpa2"
7152 * if its i-th element satisfies "base" when compared to
7153 * the i-th element of "mpa2" while all previous elements are
7156 static __isl_give isl_map
*isl_multi_pw_aff_lex_map_on_space(
7157 __isl_keep isl_multi_pw_aff
*mpa1
, __isl_keep isl_multi_pw_aff
*mpa2
,
7158 __isl_give isl_map
*(*base
)(__isl_take isl_pw_aff
*pa1
,
7159 __isl_take isl_pw_aff
*pa2
),
7160 __isl_take isl_space
*space
)
7163 isl_map
*res
, *rest
;
7165 res
= isl_map_empty(isl_space_copy(space
));
7166 rest
= isl_map_universe(space
);
7168 n
= isl_multi_pw_aff_dim(mpa1
, isl_dim_out
);
7169 for (i
= 0; i
< n
; ++i
) {
7170 isl_pw_aff
*pa1
, *pa2
;
7173 pa1
= isl_multi_pw_aff_get_pw_aff(mpa1
, i
);
7174 pa2
= isl_multi_pw_aff_get_pw_aff(mpa2
, i
);
7175 map
= base(pa1
, pa2
);
7176 map
= isl_map_intersect(map
, isl_map_copy(rest
));
7177 res
= isl_map_union(res
, map
);
7182 pa1
= isl_multi_pw_aff_get_pw_aff(mpa1
, i
);
7183 pa2
= isl_multi_pw_aff_get_pw_aff(mpa2
, i
);
7184 map
= isl_pw_aff_eq_map(pa1
, pa2
);
7185 rest
= isl_map_intersect(rest
, map
);
7192 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7193 * where the function value of "mpa1" is lexicographically less than that
7194 * of "mpa2". "space" is the space of the result.
7195 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7197 * "mpa1" is less than "mpa2" if its i-th element is smaller
7198 * than the i-th element of "mpa2" while all previous elements are
7201 __isl_give isl_map
*isl_multi_pw_aff_lex_lt_map_on_space(
7202 __isl_keep isl_multi_pw_aff
*mpa1
, __isl_keep isl_multi_pw_aff
*mpa2
,
7203 __isl_take isl_space
*space
)
7205 return isl_multi_pw_aff_lex_map_on_space(mpa1
, mpa2
,
7206 &isl_pw_aff_lt_map
, space
);
7209 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7210 * where the function value of "mpa1" is lexicographically less than that
7213 __isl_give isl_map
*isl_multi_pw_aff_lex_lt_map(
7214 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
)
7216 return isl_multi_pw_aff_order_map(mpa1
, mpa2
,
7217 &isl_multi_pw_aff_lex_lt_map_on_space
);
7220 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7221 * where the function value of "mpa1" is lexicographically greater than that
7222 * of "mpa2". "space" is the space of the result.
7223 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7225 * "mpa1" is greater than "mpa2" if its i-th element is greater
7226 * than the i-th element of "mpa2" while all previous elements are
7229 __isl_give isl_map
*isl_multi_pw_aff_lex_gt_map_on_space(
7230 __isl_keep isl_multi_pw_aff
*mpa1
, __isl_keep isl_multi_pw_aff
*mpa2
,
7231 __isl_take isl_space
*space
)
7233 return isl_multi_pw_aff_lex_map_on_space(mpa1
, mpa2
,
7234 &isl_pw_aff_gt_map
, space
);
7237 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7238 * where the function value of "mpa1" is lexicographically greater than that
7241 __isl_give isl_map
*isl_multi_pw_aff_lex_gt_map(
7242 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
)
7244 return isl_multi_pw_aff_order_map(mpa1
, mpa2
,
7245 &isl_multi_pw_aff_lex_gt_map_on_space
);
7248 /* Compare two isl_affs.
7250 * Return -1 if "aff1" is "smaller" than "aff2", 1 if "aff1" is "greater"
7251 * than "aff2" and 0 if they are equal.
7253 * The order is fairly arbitrary. We do consider expressions that only involve
7254 * earlier dimensions as "smaller".
7256 int isl_aff_plain_cmp(__isl_keep isl_aff
*aff1
, __isl_keep isl_aff
*aff2
)
7269 cmp
= isl_local_space_cmp(aff1
->ls
, aff2
->ls
);
7273 last1
= isl_seq_last_non_zero(aff1
->v
->el
+ 1, aff1
->v
->size
- 1);
7274 last2
= isl_seq_last_non_zero(aff2
->v
->el
+ 1, aff1
->v
->size
- 1);
7276 return last1
- last2
;
7278 return isl_seq_cmp(aff1
->v
->el
, aff2
->v
->el
, aff1
->v
->size
);
7281 /* Compare two isl_pw_affs.
7283 * Return -1 if "pa1" is "smaller" than "pa2", 1 if "pa1" is "greater"
7284 * than "pa2" and 0 if they are equal.
7286 * The order is fairly arbitrary. We do consider expressions that only involve
7287 * earlier dimensions as "smaller".
7289 int isl_pw_aff_plain_cmp(__isl_keep isl_pw_aff
*pa1
,
7290 __isl_keep isl_pw_aff
*pa2
)
7303 cmp
= isl_space_cmp(pa1
->dim
, pa2
->dim
);
7307 if (pa1
->n
!= pa2
->n
)
7308 return pa1
->n
- pa2
->n
;
7310 for (i
= 0; i
< pa1
->n
; ++i
) {
7311 cmp
= isl_set_plain_cmp(pa1
->p
[i
].set
, pa2
->p
[i
].set
);
7314 cmp
= isl_aff_plain_cmp(pa1
->p
[i
].aff
, pa2
->p
[i
].aff
);
7322 /* Return a piecewise affine expression that is equal to "v" on "domain".
7324 __isl_give isl_pw_aff
*isl_pw_aff_val_on_domain(__isl_take isl_set
*domain
,
7325 __isl_take isl_val
*v
)
7328 isl_local_space
*ls
;
7331 space
= isl_set_get_space(domain
);
7332 ls
= isl_local_space_from_space(space
);
7333 aff
= isl_aff_val_on_domain(ls
, v
);
7335 return isl_pw_aff_alloc(domain
, aff
);
7338 /* Return a multi affine expression that is equal to "mv" on domain
7341 __isl_give isl_multi_aff
*isl_multi_aff_multi_val_on_space(
7342 __isl_take isl_space
*space
, __isl_take isl_multi_val
*mv
)
7346 isl_local_space
*ls
;
7352 n
= isl_multi_val_dim(mv
, isl_dim_set
);
7353 space2
= isl_multi_val_get_space(mv
);
7354 space2
= isl_space_align_params(space2
, isl_space_copy(space
));
7355 space
= isl_space_align_params(space
, isl_space_copy(space2
));
7356 space
= isl_space_map_from_domain_and_range(space
, space2
);
7357 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
7358 ls
= isl_local_space_from_space(isl_space_domain(space
));
7359 for (i
= 0; i
< n
; ++i
) {
7363 v
= isl_multi_val_get_val(mv
, i
);
7364 aff
= isl_aff_val_on_domain(isl_local_space_copy(ls
), v
);
7365 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
7367 isl_local_space_free(ls
);
7369 isl_multi_val_free(mv
);
7372 isl_space_free(space
);
7373 isl_multi_val_free(mv
);
7377 /* Return a piecewise multi-affine expression
7378 * that is equal to "mv" on "domain".
7380 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_multi_val_on_domain(
7381 __isl_take isl_set
*domain
, __isl_take isl_multi_val
*mv
)
7386 space
= isl_set_get_space(domain
);
7387 ma
= isl_multi_aff_multi_val_on_space(space
, mv
);
7389 return isl_pw_multi_aff_alloc(domain
, ma
);
7392 /* Internal data structure for isl_union_pw_multi_aff_multi_val_on_domain.
7393 * mv is the value that should be attained on each domain set
7394 * res collects the results
7396 struct isl_union_pw_multi_aff_multi_val_on_domain_data
{
7398 isl_union_pw_multi_aff
*res
;
7401 /* Create an isl_pw_multi_aff equal to data->mv on "domain"
7402 * and add it to data->res.
7404 static isl_stat
pw_multi_aff_multi_val_on_domain(__isl_take isl_set
*domain
,
7407 struct isl_union_pw_multi_aff_multi_val_on_domain_data
*data
= user
;
7408 isl_pw_multi_aff
*pma
;
7411 mv
= isl_multi_val_copy(data
->mv
);
7412 pma
= isl_pw_multi_aff_multi_val_on_domain(domain
, mv
);
7413 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma
);
7415 return data
->res
? isl_stat_ok
: isl_stat_error
;
7418 /* Return a union piecewise multi-affine expression
7419 * that is equal to "mv" on "domain".
7421 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_multi_val_on_domain(
7422 __isl_take isl_union_set
*domain
, __isl_take isl_multi_val
*mv
)
7424 struct isl_union_pw_multi_aff_multi_val_on_domain_data data
;
7427 space
= isl_union_set_get_space(domain
);
7428 data
.res
= isl_union_pw_multi_aff_empty(space
);
7430 if (isl_union_set_foreach_set(domain
,
7431 &pw_multi_aff_multi_val_on_domain
, &data
) < 0)
7432 data
.res
= isl_union_pw_multi_aff_free(data
.res
);
7433 isl_union_set_free(domain
);
7434 isl_multi_val_free(mv
);
7438 /* Compute the pullback of data->pma by the function represented by "pma2",
7439 * provided the spaces match, and add the results to data->res.
7441 static isl_stat
pullback_entry(__isl_take isl_pw_multi_aff
*pma2
, void *user
)
7443 struct isl_union_pw_multi_aff_bin_data
*data
= user
;
7445 if (!isl_space_tuple_is_equal(data
->pma
->dim
, isl_dim_in
,
7446 pma2
->dim
, isl_dim_out
)) {
7447 isl_pw_multi_aff_free(pma2
);
7451 pma2
= isl_pw_multi_aff_pullback_pw_multi_aff(
7452 isl_pw_multi_aff_copy(data
->pma
), pma2
);
7454 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma2
);
7456 return isl_stat_error
;
7461 /* Compute the pullback of "upma1" by the function represented by "upma2".
7463 __isl_give isl_union_pw_multi_aff
*
7464 isl_union_pw_multi_aff_pullback_union_pw_multi_aff(
7465 __isl_take isl_union_pw_multi_aff
*upma1
,
7466 __isl_take isl_union_pw_multi_aff
*upma2
)
7468 return bin_op(upma1
, upma2
, &pullback_entry
);
7471 /* Check that the domain space of "upa" matches "space".
7473 * This function is called from isl_multi_union_pw_aff_set_union_pw_aff and
7474 * can in principle never fail since the space "space" is that
7475 * of the isl_multi_union_pw_aff and is a set space such that
7476 * there is no domain space to match.
7478 * We check the parameters and double-check that "space" is
7479 * indeed that of a set.
7481 static isl_stat
isl_union_pw_aff_check_match_domain_space(
7482 __isl_keep isl_union_pw_aff
*upa
, __isl_keep isl_space
*space
)
7484 isl_space
*upa_space
;
7488 return isl_stat_error
;
7490 match
= isl_space_is_set(space
);
7492 return isl_stat_error
;
7494 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
7495 "expecting set space", return -1);
7497 upa_space
= isl_union_pw_aff_get_space(upa
);
7498 match
= isl_space_has_equal_params(space
, upa_space
);
7502 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
7503 "parameters don't match", goto error
);
7505 isl_space_free(upa_space
);
7508 isl_space_free(upa_space
);
7509 return isl_stat_error
;
7512 /* Do the parameters of "upa" match those of "space"?
7514 static isl_bool
isl_union_pw_aff_matching_params(
7515 __isl_keep isl_union_pw_aff
*upa
, __isl_keep isl_space
*space
)
7517 isl_space
*upa_space
;
7521 return isl_bool_error
;
7523 upa_space
= isl_union_pw_aff_get_space(upa
);
7525 match
= isl_space_has_equal_params(space
, upa_space
);
7527 isl_space_free(upa_space
);
7531 /* Internal data structure for isl_union_pw_aff_reset_domain_space.
7532 * space represents the new parameters.
7533 * res collects the results.
7535 struct isl_union_pw_aff_reset_params_data
{
7537 isl_union_pw_aff
*res
;
7540 /* Replace the parameters of "pa" by data->space and
7541 * add the result to data->res.
7543 static isl_stat
reset_params(__isl_take isl_pw_aff
*pa
, void *user
)
7545 struct isl_union_pw_aff_reset_params_data
*data
= user
;
7548 space
= isl_pw_aff_get_space(pa
);
7549 space
= isl_space_replace_params(space
, data
->space
);
7550 pa
= isl_pw_aff_reset_space(pa
, space
);
7551 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
7553 return data
->res
? isl_stat_ok
: isl_stat_error
;
7556 /* Replace the domain space of "upa" by "space".
7557 * Since a union expression does not have a (single) domain space,
7558 * "space" is necessarily a parameter space.
7560 * Since the order and the names of the parameters determine
7561 * the hash value, we need to create a new hash table.
7563 static __isl_give isl_union_pw_aff
*isl_union_pw_aff_reset_domain_space(
7564 __isl_take isl_union_pw_aff
*upa
, __isl_take isl_space
*space
)
7566 struct isl_union_pw_aff_reset_params_data data
= { space
};
7569 match
= isl_union_pw_aff_matching_params(upa
, space
);
7571 upa
= isl_union_pw_aff_free(upa
);
7573 isl_space_free(space
);
7577 data
.res
= isl_union_pw_aff_empty(isl_space_copy(space
));
7578 if (isl_union_pw_aff_foreach_pw_aff(upa
, &reset_params
, &data
) < 0)
7579 data
.res
= isl_union_pw_aff_free(data
.res
);
7581 isl_union_pw_aff_free(upa
);
7582 isl_space_free(space
);
7586 /* Return the floor of "pa".
7588 static __isl_give isl_pw_aff
*floor_entry(__isl_take isl_pw_aff
*pa
, void *user
)
7590 return isl_pw_aff_floor(pa
);
7593 /* Given f, return floor(f).
7595 __isl_give isl_union_pw_aff
*isl_union_pw_aff_floor(
7596 __isl_take isl_union_pw_aff
*upa
)
7598 return isl_union_pw_aff_transform_inplace(upa
, &floor_entry
, NULL
);
7603 * upa mod m = upa - m * floor(upa/m)
7605 * with m an integer value.
7607 __isl_give isl_union_pw_aff
*isl_union_pw_aff_mod_val(
7608 __isl_take isl_union_pw_aff
*upa
, __isl_take isl_val
*m
)
7610 isl_union_pw_aff
*res
;
7615 if (!isl_val_is_int(m
))
7616 isl_die(isl_val_get_ctx(m
), isl_error_invalid
,
7617 "expecting integer modulo", goto error
);
7618 if (!isl_val_is_pos(m
))
7619 isl_die(isl_val_get_ctx(m
), isl_error_invalid
,
7620 "expecting positive modulo", goto error
);
7622 res
= isl_union_pw_aff_copy(upa
);
7623 upa
= isl_union_pw_aff_scale_down_val(upa
, isl_val_copy(m
));
7624 upa
= isl_union_pw_aff_floor(upa
);
7625 upa
= isl_union_pw_aff_scale_val(upa
, m
);
7626 res
= isl_union_pw_aff_sub(res
, upa
);
7631 isl_union_pw_aff_free(upa
);
7635 /* Internal data structure for isl_union_pw_multi_aff_get_union_pw_aff.
7636 * pos is the output position that needs to be extracted.
7637 * res collects the results.
7639 struct isl_union_pw_multi_aff_get_union_pw_aff_data
{
7641 isl_union_pw_aff
*res
;
7644 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma"
7645 * (assuming it has such a dimension) and add it to data->res.
7647 static isl_stat
get_union_pw_aff(__isl_take isl_pw_multi_aff
*pma
, void *user
)
7649 struct isl_union_pw_multi_aff_get_union_pw_aff_data
*data
= user
;
7654 return isl_stat_error
;
7656 n_out
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
7657 if (data
->pos
>= n_out
) {
7658 isl_pw_multi_aff_free(pma
);
7662 pa
= isl_pw_multi_aff_get_pw_aff(pma
, data
->pos
);
7663 isl_pw_multi_aff_free(pma
);
7665 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
7667 return data
->res
? isl_stat_ok
: isl_stat_error
;
7670 /* Extract an isl_union_pw_aff corresponding to
7671 * output dimension "pos" of "upma".
7673 __isl_give isl_union_pw_aff
*isl_union_pw_multi_aff_get_union_pw_aff(
7674 __isl_keep isl_union_pw_multi_aff
*upma
, int pos
)
7676 struct isl_union_pw_multi_aff_get_union_pw_aff_data data
;
7683 isl_die(isl_union_pw_multi_aff_get_ctx(upma
), isl_error_invalid
,
7684 "cannot extract at negative position", return NULL
);
7686 space
= isl_union_pw_multi_aff_get_space(upma
);
7687 data
.res
= isl_union_pw_aff_empty(space
);
7689 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma
,
7690 &get_union_pw_aff
, &data
) < 0)
7691 data
.res
= isl_union_pw_aff_free(data
.res
);
7696 /* Return a union piecewise affine expression
7697 * that is equal to "aff" on "domain".
7699 __isl_give isl_union_pw_aff
*isl_union_pw_aff_aff_on_domain(
7700 __isl_take isl_union_set
*domain
, __isl_take isl_aff
*aff
)
7704 pa
= isl_pw_aff_from_aff(aff
);
7705 return isl_union_pw_aff_pw_aff_on_domain(domain
, pa
);
7708 /* Return a union piecewise affine expression
7709 * that is equal to the parameter identified by "id" on "domain".
7711 * Make sure the parameter appears in the space passed to
7712 * isl_aff_param_on_domain_space_id.
7714 __isl_give isl_union_pw_aff
*isl_union_pw_aff_param_on_domain_id(
7715 __isl_take isl_union_set
*domain
, __isl_take isl_id
*id
)
7720 space
= isl_union_set_get_space(domain
);
7721 space
= isl_space_add_param_id(space
, isl_id_copy(id
));
7722 aff
= isl_aff_param_on_domain_space_id(space
, id
);
7723 return isl_union_pw_aff_aff_on_domain(domain
, aff
);
7726 /* Internal data structure for isl_union_pw_aff_pw_aff_on_domain.
7727 * "pa" is the piecewise symbolic value that the resulting isl_union_pw_aff
7729 * "res" collects the results.
7731 struct isl_union_pw_aff_pw_aff_on_domain_data
{
7733 isl_union_pw_aff
*res
;
7736 /* Construct a piecewise affine expression that is equal to data->pa
7737 * on "domain" and add the result to data->res.
7739 static isl_stat
pw_aff_on_domain(__isl_take isl_set
*domain
, void *user
)
7741 struct isl_union_pw_aff_pw_aff_on_domain_data
*data
= user
;
7745 pa
= isl_pw_aff_copy(data
->pa
);
7746 dim
= isl_set_dim(domain
, isl_dim_set
);
7747 pa
= isl_pw_aff_from_range(pa
);
7748 pa
= isl_pw_aff_add_dims(pa
, isl_dim_in
, dim
);
7749 pa
= isl_pw_aff_reset_domain_space(pa
, isl_set_get_space(domain
));
7750 pa
= isl_pw_aff_intersect_domain(pa
, domain
);
7751 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
7753 return data
->res
? isl_stat_ok
: isl_stat_error
;
7756 /* Return a union piecewise affine expression
7757 * that is equal to "pa" on "domain", assuming "domain" and "pa"
7758 * have been aligned.
7760 * Construct an isl_pw_aff on each of the sets in "domain" and
7761 * collect the results.
7763 static __isl_give isl_union_pw_aff
*isl_union_pw_aff_pw_aff_on_domain_aligned(
7764 __isl_take isl_union_set
*domain
, __isl_take isl_pw_aff
*pa
)
7766 struct isl_union_pw_aff_pw_aff_on_domain_data data
;
7769 space
= isl_union_set_get_space(domain
);
7770 data
.res
= isl_union_pw_aff_empty(space
);
7772 if (isl_union_set_foreach_set(domain
, &pw_aff_on_domain
, &data
) < 0)
7773 data
.res
= isl_union_pw_aff_free(data
.res
);
7774 isl_union_set_free(domain
);
7775 isl_pw_aff_free(pa
);
7779 /* Return a union piecewise affine expression
7780 * that is equal to "pa" on "domain".
7782 * Check that "pa" is a parametric expression,
7783 * align the parameters if needed and call
7784 * isl_union_pw_aff_pw_aff_on_domain_aligned.
7786 __isl_give isl_union_pw_aff
*isl_union_pw_aff_pw_aff_on_domain(
7787 __isl_take isl_union_set
*domain
, __isl_take isl_pw_aff
*pa
)
7790 isl_bool equal_params
;
7791 isl_space
*domain_space
, *pa_space
;
7793 pa_space
= isl_pw_aff_peek_space(pa
);
7794 is_set
= isl_space_is_set(pa_space
);
7798 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
7799 "expecting parametric expression", goto error
);
7801 domain_space
= isl_union_set_get_space(domain
);
7802 pa_space
= isl_pw_aff_get_space(pa
);
7803 equal_params
= isl_space_has_equal_params(domain_space
, pa_space
);
7804 if (equal_params
>= 0 && !equal_params
) {
7807 space
= isl_space_align_params(domain_space
, pa_space
);
7808 pa
= isl_pw_aff_align_params(pa
, isl_space_copy(space
));
7809 domain
= isl_union_set_align_params(domain
, space
);
7811 isl_space_free(domain_space
);
7812 isl_space_free(pa_space
);
7815 if (equal_params
< 0)
7817 return isl_union_pw_aff_pw_aff_on_domain_aligned(domain
, pa
);
7819 isl_union_set_free(domain
);
7820 isl_pw_aff_free(pa
);
7824 /* Internal data structure for isl_union_pw_aff_val_on_domain.
7825 * "v" is the value that the resulting isl_union_pw_aff needs to attain.
7826 * "res" collects the results.
7828 struct isl_union_pw_aff_val_on_domain_data
{
7830 isl_union_pw_aff
*res
;
7833 /* Construct a piecewise affine expression that is equal to data->v
7834 * on "domain" and add the result to data->res.
7836 static isl_stat
pw_aff_val_on_domain(__isl_take isl_set
*domain
, void *user
)
7838 struct isl_union_pw_aff_val_on_domain_data
*data
= user
;
7842 v
= isl_val_copy(data
->v
);
7843 pa
= isl_pw_aff_val_on_domain(domain
, v
);
7844 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
7846 return data
->res
? isl_stat_ok
: isl_stat_error
;
7849 /* Return a union piecewise affine expression
7850 * that is equal to "v" on "domain".
7852 * Construct an isl_pw_aff on each of the sets in "domain" and
7853 * collect the results.
7855 __isl_give isl_union_pw_aff
*isl_union_pw_aff_val_on_domain(
7856 __isl_take isl_union_set
*domain
, __isl_take isl_val
*v
)
7858 struct isl_union_pw_aff_val_on_domain_data data
;
7861 space
= isl_union_set_get_space(domain
);
7862 data
.res
= isl_union_pw_aff_empty(space
);
7864 if (isl_union_set_foreach_set(domain
, &pw_aff_val_on_domain
, &data
) < 0)
7865 data
.res
= isl_union_pw_aff_free(data
.res
);
7866 isl_union_set_free(domain
);
7871 /* Construct a piecewise multi affine expression
7872 * that is equal to "pa" and add it to upma.
7874 static isl_stat
pw_multi_aff_from_pw_aff_entry(__isl_take isl_pw_aff
*pa
,
7877 isl_union_pw_multi_aff
**upma
= user
;
7878 isl_pw_multi_aff
*pma
;
7880 pma
= isl_pw_multi_aff_from_pw_aff(pa
);
7881 *upma
= isl_union_pw_multi_aff_add_pw_multi_aff(*upma
, pma
);
7883 return *upma
? isl_stat_ok
: isl_stat_error
;
7886 /* Construct and return a union piecewise multi affine expression
7887 * that is equal to the given union piecewise affine expression.
7889 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_union_pw_aff(
7890 __isl_take isl_union_pw_aff
*upa
)
7893 isl_union_pw_multi_aff
*upma
;
7898 space
= isl_union_pw_aff_get_space(upa
);
7899 upma
= isl_union_pw_multi_aff_empty(space
);
7901 if (isl_union_pw_aff_foreach_pw_aff(upa
,
7902 &pw_multi_aff_from_pw_aff_entry
, &upma
) < 0)
7903 upma
= isl_union_pw_multi_aff_free(upma
);
7905 isl_union_pw_aff_free(upa
);
7909 /* Compute the set of elements in the domain of "pa" where it is zero and
7910 * add this set to "uset".
7912 static isl_stat
zero_union_set(__isl_take isl_pw_aff
*pa
, void *user
)
7914 isl_union_set
**uset
= (isl_union_set
**)user
;
7916 *uset
= isl_union_set_add_set(*uset
, isl_pw_aff_zero_set(pa
));
7918 return *uset
? isl_stat_ok
: isl_stat_error
;
7921 /* Return a union set containing those elements in the domain
7922 * of "upa" where it is zero.
7924 __isl_give isl_union_set
*isl_union_pw_aff_zero_union_set(
7925 __isl_take isl_union_pw_aff
*upa
)
7927 isl_union_set
*zero
;
7929 zero
= isl_union_set_empty(isl_union_pw_aff_get_space(upa
));
7930 if (isl_union_pw_aff_foreach_pw_aff(upa
, &zero_union_set
, &zero
) < 0)
7931 zero
= isl_union_set_free(zero
);
7933 isl_union_pw_aff_free(upa
);
7937 /* Convert "pa" to an isl_map and add it to *umap.
7939 static isl_stat
map_from_pw_aff_entry(__isl_take isl_pw_aff
*pa
, void *user
)
7941 isl_union_map
**umap
= user
;
7944 map
= isl_map_from_pw_aff(pa
);
7945 *umap
= isl_union_map_add_map(*umap
, map
);
7947 return *umap
? isl_stat_ok
: isl_stat_error
;
7950 /* Construct a union map mapping the domain of the union
7951 * piecewise affine expression to its range, with the single output dimension
7952 * equated to the corresponding affine expressions on their cells.
7954 __isl_give isl_union_map
*isl_union_map_from_union_pw_aff(
7955 __isl_take isl_union_pw_aff
*upa
)
7958 isl_union_map
*umap
;
7963 space
= isl_union_pw_aff_get_space(upa
);
7964 umap
= isl_union_map_empty(space
);
7966 if (isl_union_pw_aff_foreach_pw_aff(upa
, &map_from_pw_aff_entry
,
7968 umap
= isl_union_map_free(umap
);
7970 isl_union_pw_aff_free(upa
);
7974 /* Internal data structure for isl_union_pw_aff_pullback_union_pw_multi_aff.
7975 * upma is the function that is plugged in.
7976 * pa is the current part of the function in which upma is plugged in.
7977 * res collects the results.
7979 struct isl_union_pw_aff_pullback_upma_data
{
7980 isl_union_pw_multi_aff
*upma
;
7982 isl_union_pw_aff
*res
;
7985 /* Check if "pma" can be plugged into data->pa.
7986 * If so, perform the pullback and add the result to data->res.
7988 static isl_stat
pa_pb_pma(__isl_take isl_pw_multi_aff
*pma
, void *user
)
7990 struct isl_union_pw_aff_pullback_upma_data
*data
= user
;
7993 if (!isl_space_tuple_is_equal(data
->pa
->dim
, isl_dim_in
,
7994 pma
->dim
, isl_dim_out
)) {
7995 isl_pw_multi_aff_free(pma
);
7999 pa
= isl_pw_aff_copy(data
->pa
);
8000 pa
= isl_pw_aff_pullback_pw_multi_aff(pa
, pma
);
8002 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
8004 return data
->res
? isl_stat_ok
: isl_stat_error
;
8007 /* Check if any of the elements of data->upma can be plugged into pa,
8008 * add if so add the result to data->res.
8010 static isl_stat
upa_pb_upma(__isl_take isl_pw_aff
*pa
, void *user
)
8012 struct isl_union_pw_aff_pullback_upma_data
*data
= user
;
8016 r
= isl_union_pw_multi_aff_foreach_pw_multi_aff(data
->upma
,
8018 isl_pw_aff_free(pa
);
8023 /* Compute the pullback of "upa" by the function represented by "upma".
8024 * In other words, plug in "upma" in "upa". The result contains
8025 * expressions defined over the domain space of "upma".
8027 * Run over all pairs of elements in "upa" and "upma", perform
8028 * the pullback when appropriate and collect the results.
8029 * If the hash value were based on the domain space rather than
8030 * the function space, then we could run through all elements
8031 * of "upma" and directly pick out the corresponding element of "upa".
8033 __isl_give isl_union_pw_aff
*isl_union_pw_aff_pullback_union_pw_multi_aff(
8034 __isl_take isl_union_pw_aff
*upa
,
8035 __isl_take isl_union_pw_multi_aff
*upma
)
8037 struct isl_union_pw_aff_pullback_upma_data data
= { NULL
, NULL
};
8040 space
= isl_union_pw_multi_aff_get_space(upma
);
8041 upa
= isl_union_pw_aff_align_params(upa
, space
);
8042 space
= isl_union_pw_aff_get_space(upa
);
8043 upma
= isl_union_pw_multi_aff_align_params(upma
, space
);
8049 data
.res
= isl_union_pw_aff_alloc_same_size(upa
);
8050 if (isl_union_pw_aff_foreach_pw_aff(upa
, &upa_pb_upma
, &data
) < 0)
8051 data
.res
= isl_union_pw_aff_free(data
.res
);
8053 isl_union_pw_aff_free(upa
);
8054 isl_union_pw_multi_aff_free(upma
);
8057 isl_union_pw_aff_free(upa
);
8058 isl_union_pw_multi_aff_free(upma
);
8063 #define BASE union_pw_aff
8065 #define DOMBASE union_set
8067 #define NO_MOVE_DIMS
8075 #include <isl_multi_templ.c>
8076 #include <isl_multi_apply_set.c>
8077 #include <isl_multi_apply_union_set.c>
8078 #include <isl_multi_coalesce.c>
8079 #include <isl_multi_floor.c>
8080 #include <isl_multi_gist.c>
8081 #include <isl_multi_intersect.c>
8083 /* Construct a multiple union piecewise affine expression
8084 * in the given space with value zero in each of the output dimensions.
8086 * Since there is no canonical zero value for
8087 * a union piecewise affine expression, we can only construct
8088 * a zero-dimensional "zero" value.
8090 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_zero(
8091 __isl_take isl_space
*space
)
8098 params
= isl_space_is_params(space
);
8102 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
8103 "expecting proper set space", goto error
);
8104 if (!isl_space_is_set(space
))
8105 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
8106 "expecting set space", goto error
);
8107 if (isl_space_dim(space
, isl_dim_out
) != 0)
8108 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
8109 "expecting 0D space", goto error
);
8111 return isl_multi_union_pw_aff_alloc(space
);
8113 isl_space_free(space
);
8117 /* Compute the sum of "mupa1" and "mupa2" on the union of their domains,
8118 * with the actual sum on the shared domain and
8119 * the defined expression on the symmetric difference of the domains.
8121 * We simply iterate over the elements in both arguments and
8122 * call isl_union_pw_aff_union_add on each of them.
8124 static __isl_give isl_multi_union_pw_aff
*
8125 isl_multi_union_pw_aff_union_add_aligned(
8126 __isl_take isl_multi_union_pw_aff
*mupa1
,
8127 __isl_take isl_multi_union_pw_aff
*mupa2
)
8129 return isl_multi_union_pw_aff_bin_op(mupa1
, mupa2
,
8130 &isl_union_pw_aff_union_add
);
8133 /* Compute the sum of "mupa1" and "mupa2" on the union of their domains,
8134 * with the actual sum on the shared domain and
8135 * the defined expression on the symmetric difference of the domains.
8137 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_union_add(
8138 __isl_take isl_multi_union_pw_aff
*mupa1
,
8139 __isl_take isl_multi_union_pw_aff
*mupa2
)
8141 return isl_multi_union_pw_aff_align_params_multi_multi_and(mupa1
, mupa2
,
8142 &isl_multi_union_pw_aff_union_add_aligned
);
8145 /* Construct and return a multi union piecewise affine expression
8146 * that is equal to the given multi affine expression.
8148 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_from_multi_aff(
8149 __isl_take isl_multi_aff
*ma
)
8151 isl_multi_pw_aff
*mpa
;
8153 mpa
= isl_multi_pw_aff_from_multi_aff(ma
);
8154 return isl_multi_union_pw_aff_from_multi_pw_aff(mpa
);
8157 /* Construct and return a multi union piecewise affine expression
8158 * that is equal to the given multi piecewise affine expression.
8160 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_from_multi_pw_aff(
8161 __isl_take isl_multi_pw_aff
*mpa
)
8165 isl_multi_union_pw_aff
*mupa
;
8170 space
= isl_multi_pw_aff_get_space(mpa
);
8171 space
= isl_space_range(space
);
8172 mupa
= isl_multi_union_pw_aff_alloc(space
);
8174 n
= isl_multi_pw_aff_dim(mpa
, isl_dim_out
);
8175 for (i
= 0; i
< n
; ++i
) {
8177 isl_union_pw_aff
*upa
;
8179 pa
= isl_multi_pw_aff_get_pw_aff(mpa
, i
);
8180 upa
= isl_union_pw_aff_from_pw_aff(pa
);
8181 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
8184 isl_multi_pw_aff_free(mpa
);
8189 /* Extract the range space of "pma" and assign it to *space.
8190 * If *space has already been set (through a previous call to this function),
8191 * then check that the range space is the same.
8193 static isl_stat
extract_space(__isl_take isl_pw_multi_aff
*pma
, void *user
)
8195 isl_space
**space
= user
;
8196 isl_space
*pma_space
;
8199 pma_space
= isl_space_range(isl_pw_multi_aff_get_space(pma
));
8200 isl_pw_multi_aff_free(pma
);
8203 return isl_stat_error
;
8209 equal
= isl_space_is_equal(pma_space
, *space
);
8210 isl_space_free(pma_space
);
8213 return isl_stat_error
;
8215 isl_die(isl_space_get_ctx(*space
), isl_error_invalid
,
8216 "range spaces not the same", return isl_stat_error
);
8220 /* Construct and return a multi union piecewise affine expression
8221 * that is equal to the given union piecewise multi affine expression.
8223 * In order to be able to perform the conversion, the input
8224 * needs to be non-empty and may only involve a single range space.
8226 __isl_give isl_multi_union_pw_aff
*
8227 isl_multi_union_pw_aff_from_union_pw_multi_aff(
8228 __isl_take isl_union_pw_multi_aff
*upma
)
8230 isl_space
*space
= NULL
;
8231 isl_multi_union_pw_aff
*mupa
;
8236 if (isl_union_pw_multi_aff_n_pw_multi_aff(upma
) == 0)
8237 isl_die(isl_union_pw_multi_aff_get_ctx(upma
), isl_error_invalid
,
8238 "cannot extract range space from empty input",
8240 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma
, &extract_space
,
8247 n
= isl_space_dim(space
, isl_dim_set
);
8248 mupa
= isl_multi_union_pw_aff_alloc(space
);
8250 for (i
= 0; i
< n
; ++i
) {
8251 isl_union_pw_aff
*upa
;
8253 upa
= isl_union_pw_multi_aff_get_union_pw_aff(upma
, i
);
8254 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
8257 isl_union_pw_multi_aff_free(upma
);
8260 isl_space_free(space
);
8261 isl_union_pw_multi_aff_free(upma
);
8265 /* Try and create an isl_multi_union_pw_aff that is equivalent
8266 * to the given isl_union_map.
8267 * The isl_union_map is required to be single-valued in each space.
8268 * Moreover, it cannot be empty and all range spaces need to be the same.
8269 * Otherwise, an error is produced.
8271 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_from_union_map(
8272 __isl_take isl_union_map
*umap
)
8274 isl_union_pw_multi_aff
*upma
;
8276 upma
= isl_union_pw_multi_aff_from_union_map(umap
);
8277 return isl_multi_union_pw_aff_from_union_pw_multi_aff(upma
);
8280 /* Return a multiple union piecewise affine expression
8281 * that is equal to "mv" on "domain", assuming "domain" and "mv"
8282 * have been aligned.
8284 static __isl_give isl_multi_union_pw_aff
*
8285 isl_multi_union_pw_aff_multi_val_on_domain_aligned(
8286 __isl_take isl_union_set
*domain
, __isl_take isl_multi_val
*mv
)
8290 isl_multi_union_pw_aff
*mupa
;
8295 n
= isl_multi_val_dim(mv
, isl_dim_set
);
8296 space
= isl_multi_val_get_space(mv
);
8297 mupa
= isl_multi_union_pw_aff_alloc(space
);
8298 for (i
= 0; i
< n
; ++i
) {
8300 isl_union_pw_aff
*upa
;
8302 v
= isl_multi_val_get_val(mv
, i
);
8303 upa
= isl_union_pw_aff_val_on_domain(isl_union_set_copy(domain
),
8305 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
8308 isl_union_set_free(domain
);
8309 isl_multi_val_free(mv
);
8312 isl_union_set_free(domain
);
8313 isl_multi_val_free(mv
);
8317 /* Return a multiple union piecewise affine expression
8318 * that is equal to "mv" on "domain".
8320 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_multi_val_on_domain(
8321 __isl_take isl_union_set
*domain
, __isl_take isl_multi_val
*mv
)
8323 isl_bool equal_params
;
8327 equal_params
= isl_space_has_equal_params(domain
->dim
, mv
->space
);
8328 if (equal_params
< 0)
8331 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(
8333 domain
= isl_union_set_align_params(domain
,
8334 isl_multi_val_get_space(mv
));
8335 mv
= isl_multi_val_align_params(mv
, isl_union_set_get_space(domain
));
8336 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(domain
, mv
);
8338 isl_union_set_free(domain
);
8339 isl_multi_val_free(mv
);
8343 /* Return a multiple union piecewise affine expression
8344 * that is equal to "ma" on "domain".
8346 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_multi_aff_on_domain(
8347 __isl_take isl_union_set
*domain
, __isl_take isl_multi_aff
*ma
)
8349 isl_pw_multi_aff
*pma
;
8351 pma
= isl_pw_multi_aff_from_multi_aff(ma
);
8352 return isl_multi_union_pw_aff_pw_multi_aff_on_domain(domain
, pma
);
8355 /* Return a multiple union piecewise affine expression
8356 * that is equal to "pma" on "domain", assuming "domain" and "pma"
8357 * have been aligned.
8359 static __isl_give isl_multi_union_pw_aff
*
8360 isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(
8361 __isl_take isl_union_set
*domain
, __isl_take isl_pw_multi_aff
*pma
)
8365 isl_multi_union_pw_aff
*mupa
;
8367 if (!domain
|| !pma
)
8370 n
= isl_pw_multi_aff_dim(pma
, isl_dim_set
);
8371 space
= isl_pw_multi_aff_get_space(pma
);
8372 mupa
= isl_multi_union_pw_aff_alloc(space
);
8373 for (i
= 0; i
< n
; ++i
) {
8375 isl_union_pw_aff
*upa
;
8377 pa
= isl_pw_multi_aff_get_pw_aff(pma
, i
);
8378 upa
= isl_union_pw_aff_pw_aff_on_domain(
8379 isl_union_set_copy(domain
), pa
);
8380 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
8383 isl_union_set_free(domain
);
8384 isl_pw_multi_aff_free(pma
);
8387 isl_union_set_free(domain
);
8388 isl_pw_multi_aff_free(pma
);
8392 /* Return a multiple union piecewise affine expression
8393 * that is equal to "pma" on "domain".
8395 __isl_give isl_multi_union_pw_aff
*
8396 isl_multi_union_pw_aff_pw_multi_aff_on_domain(__isl_take isl_union_set
*domain
,
8397 __isl_take isl_pw_multi_aff
*pma
)
8399 isl_bool equal_params
;
8402 space
= isl_pw_multi_aff_peek_space(pma
);
8403 equal_params
= isl_union_set_space_has_equal_params(domain
, space
);
8404 if (equal_params
< 0)
8407 return isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(
8409 domain
= isl_union_set_align_params(domain
,
8410 isl_pw_multi_aff_get_space(pma
));
8411 pma
= isl_pw_multi_aff_align_params(pma
,
8412 isl_union_set_get_space(domain
));
8413 return isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(domain
,
8416 isl_union_set_free(domain
);
8417 isl_pw_multi_aff_free(pma
);
8421 /* Return a union set containing those elements in the domains
8422 * of the elements of "mupa" where they are all zero.
8424 __isl_give isl_union_set
*isl_multi_union_pw_aff_zero_union_set(
8425 __isl_take isl_multi_union_pw_aff
*mupa
)
8428 isl_union_pw_aff
*upa
;
8429 isl_union_set
*zero
;
8434 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8436 isl_die(isl_multi_union_pw_aff_get_ctx(mupa
), isl_error_invalid
,
8437 "cannot determine zero set "
8438 "of zero-dimensional function", goto error
);
8440 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, 0);
8441 zero
= isl_union_pw_aff_zero_union_set(upa
);
8443 for (i
= 1; i
< n
; ++i
) {
8444 isl_union_set
*zero_i
;
8446 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
8447 zero_i
= isl_union_pw_aff_zero_union_set(upa
);
8449 zero
= isl_union_set_intersect(zero
, zero_i
);
8452 isl_multi_union_pw_aff_free(mupa
);
8455 isl_multi_union_pw_aff_free(mupa
);
8459 /* Construct a union map mapping the shared domain
8460 * of the union piecewise affine expressions to the range of "mupa"
8461 * with each dimension in the range equated to the
8462 * corresponding union piecewise affine expression.
8464 * The input cannot be zero-dimensional as there is
8465 * no way to extract a domain from a zero-dimensional isl_multi_union_pw_aff.
8467 __isl_give isl_union_map
*isl_union_map_from_multi_union_pw_aff(
8468 __isl_take isl_multi_union_pw_aff
*mupa
)
8472 isl_union_map
*umap
;
8473 isl_union_pw_aff
*upa
;
8478 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8480 isl_die(isl_multi_union_pw_aff_get_ctx(mupa
), isl_error_invalid
,
8481 "cannot determine domain of zero-dimensional "
8482 "isl_multi_union_pw_aff", goto error
);
8484 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, 0);
8485 umap
= isl_union_map_from_union_pw_aff(upa
);
8487 for (i
= 1; i
< n
; ++i
) {
8488 isl_union_map
*umap_i
;
8490 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
8491 umap_i
= isl_union_map_from_union_pw_aff(upa
);
8492 umap
= isl_union_map_flat_range_product(umap
, umap_i
);
8495 space
= isl_multi_union_pw_aff_get_space(mupa
);
8496 umap
= isl_union_map_reset_range_space(umap
, space
);
8498 isl_multi_union_pw_aff_free(mupa
);
8501 isl_multi_union_pw_aff_free(mupa
);
8505 /* Internal data structure for isl_union_pw_multi_aff_reset_range_space.
8506 * "range" is the space from which to set the range space.
8507 * "res" collects the results.
8509 struct isl_union_pw_multi_aff_reset_range_space_data
{
8511 isl_union_pw_multi_aff
*res
;
8514 /* Replace the range space of "pma" by the range space of data->range and
8515 * add the result to data->res.
8517 static isl_stat
reset_range_space(__isl_take isl_pw_multi_aff
*pma
, void *user
)
8519 struct isl_union_pw_multi_aff_reset_range_space_data
*data
= user
;
8522 space
= isl_pw_multi_aff_get_space(pma
);
8523 space
= isl_space_domain(space
);
8524 space
= isl_space_extend_domain_with_range(space
,
8525 isl_space_copy(data
->range
));
8526 pma
= isl_pw_multi_aff_reset_space(pma
, space
);
8527 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma
);
8529 return data
->res
? isl_stat_ok
: isl_stat_error
;
8532 /* Replace the range space of all the piecewise affine expressions in "upma" by
8533 * the range space of "space".
8535 * This assumes that all these expressions have the same output dimension.
8537 * Since the spaces of the expressions change, so do their hash values.
8538 * We therefore need to create a new isl_union_pw_multi_aff.
8539 * Note that the hash value is currently computed based on the entire
8540 * space even though there can only be a single expression with a given
8543 static __isl_give isl_union_pw_multi_aff
*
8544 isl_union_pw_multi_aff_reset_range_space(
8545 __isl_take isl_union_pw_multi_aff
*upma
, __isl_take isl_space
*space
)
8547 struct isl_union_pw_multi_aff_reset_range_space_data data
= { space
};
8548 isl_space
*space_upma
;
8550 space_upma
= isl_union_pw_multi_aff_get_space(upma
);
8551 data
.res
= isl_union_pw_multi_aff_empty(space_upma
);
8552 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma
,
8553 &reset_range_space
, &data
) < 0)
8554 data
.res
= isl_union_pw_multi_aff_free(data
.res
);
8556 isl_space_free(space
);
8557 isl_union_pw_multi_aff_free(upma
);
8561 /* Construct and return a union piecewise multi affine expression
8562 * that is equal to the given multi union piecewise affine expression.
8564 * In order to be able to perform the conversion, the input
8565 * needs to have a least one output dimension.
8567 __isl_give isl_union_pw_multi_aff
*
8568 isl_union_pw_multi_aff_from_multi_union_pw_aff(
8569 __isl_take isl_multi_union_pw_aff
*mupa
)
8573 isl_union_pw_multi_aff
*upma
;
8574 isl_union_pw_aff
*upa
;
8579 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8581 isl_die(isl_multi_union_pw_aff_get_ctx(mupa
), isl_error_invalid
,
8582 "cannot determine domain of zero-dimensional "
8583 "isl_multi_union_pw_aff", goto error
);
8585 space
= isl_multi_union_pw_aff_get_space(mupa
);
8586 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, 0);
8587 upma
= isl_union_pw_multi_aff_from_union_pw_aff(upa
);
8589 for (i
= 1; i
< n
; ++i
) {
8590 isl_union_pw_multi_aff
*upma_i
;
8592 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
8593 upma_i
= isl_union_pw_multi_aff_from_union_pw_aff(upa
);
8594 upma
= isl_union_pw_multi_aff_flat_range_product(upma
, upma_i
);
8597 upma
= isl_union_pw_multi_aff_reset_range_space(upma
, space
);
8599 isl_multi_union_pw_aff_free(mupa
);
8602 isl_multi_union_pw_aff_free(mupa
);
8606 /* Intersect the range of "mupa" with "range".
8607 * That is, keep only those domain elements that have a function value
8610 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_intersect_range(
8611 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_set
*range
)
8613 isl_union_pw_multi_aff
*upma
;
8614 isl_union_set
*domain
;
8619 if (!mupa
|| !range
)
8622 space
= isl_set_get_space(range
);
8623 match
= isl_space_tuple_is_equal(mupa
->space
, isl_dim_set
,
8624 space
, isl_dim_set
);
8625 isl_space_free(space
);
8629 isl_die(isl_multi_union_pw_aff_get_ctx(mupa
), isl_error_invalid
,
8630 "space don't match", goto error
);
8631 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8633 isl_die(isl_multi_union_pw_aff_get_ctx(mupa
), isl_error_invalid
,
8634 "cannot intersect range of zero-dimensional "
8635 "isl_multi_union_pw_aff", goto error
);
8637 upma
= isl_union_pw_multi_aff_from_multi_union_pw_aff(
8638 isl_multi_union_pw_aff_copy(mupa
));
8639 domain
= isl_union_set_from_set(range
);
8640 domain
= isl_union_set_preimage_union_pw_multi_aff(domain
, upma
);
8641 mupa
= isl_multi_union_pw_aff_intersect_domain(mupa
, domain
);
8645 isl_multi_union_pw_aff_free(mupa
);
8646 isl_set_free(range
);
8650 /* Return the shared domain of the elements of "mupa".
8652 __isl_give isl_union_set
*isl_multi_union_pw_aff_domain(
8653 __isl_take isl_multi_union_pw_aff
*mupa
)
8656 isl_union_pw_aff
*upa
;
8662 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8664 isl_die(isl_multi_union_pw_aff_get_ctx(mupa
), isl_error_invalid
,
8665 "cannot determine domain", goto error
);
8667 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, 0);
8668 dom
= isl_union_pw_aff_domain(upa
);
8669 for (i
= 1; i
< n
; ++i
) {
8670 isl_union_set
*dom_i
;
8672 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
8673 dom_i
= isl_union_pw_aff_domain(upa
);
8674 dom
= isl_union_set_intersect(dom
, dom_i
);
8677 isl_multi_union_pw_aff_free(mupa
);
8680 isl_multi_union_pw_aff_free(mupa
);
8684 /* Apply "aff" to "mupa". The space of "mupa" is equal to the domain of "aff".
8685 * In particular, the spaces have been aligned.
8686 * The result is defined over the shared domain of the elements of "mupa"
8688 * We first extract the parametric constant part of "aff" and
8689 * define that over the shared domain.
8690 * Then we iterate over all input dimensions of "aff" and add the corresponding
8691 * multiples of the elements of "mupa".
8692 * Finally, we consider the integer divisions, calling the function
8693 * recursively to obtain an isl_union_pw_aff corresponding to the
8694 * integer division argument.
8696 static __isl_give isl_union_pw_aff
*multi_union_pw_aff_apply_aff(
8697 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_aff
*aff
)
8700 isl_union_pw_aff
*upa
;
8701 isl_union_set
*uset
;
8705 n_in
= isl_aff_dim(aff
, isl_dim_in
);
8706 n_div
= isl_aff_dim(aff
, isl_dim_div
);
8708 uset
= isl_multi_union_pw_aff_domain(isl_multi_union_pw_aff_copy(mupa
));
8709 cst
= isl_aff_copy(aff
);
8710 cst
= isl_aff_drop_dims(cst
, isl_dim_div
, 0, n_div
);
8711 cst
= isl_aff_drop_dims(cst
, isl_dim_in
, 0, n_in
);
8712 cst
= isl_aff_project_domain_on_params(cst
);
8713 upa
= isl_union_pw_aff_aff_on_domain(uset
, cst
);
8715 for (i
= 0; i
< n_in
; ++i
) {
8716 isl_union_pw_aff
*upa_i
;
8718 if (!isl_aff_involves_dims(aff
, isl_dim_in
, i
, 1))
8720 v
= isl_aff_get_coefficient_val(aff
, isl_dim_in
, i
);
8721 upa_i
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
8722 upa_i
= isl_union_pw_aff_scale_val(upa_i
, v
);
8723 upa
= isl_union_pw_aff_add(upa
, upa_i
);
8726 for (i
= 0; i
< n_div
; ++i
) {
8728 isl_union_pw_aff
*upa_i
;
8730 if (!isl_aff_involves_dims(aff
, isl_dim_div
, i
, 1))
8732 div
= isl_aff_get_div(aff
, i
);
8733 upa_i
= multi_union_pw_aff_apply_aff(
8734 isl_multi_union_pw_aff_copy(mupa
), div
);
8735 upa_i
= isl_union_pw_aff_floor(upa_i
);
8736 v
= isl_aff_get_coefficient_val(aff
, isl_dim_div
, i
);
8737 upa_i
= isl_union_pw_aff_scale_val(upa_i
, v
);
8738 upa
= isl_union_pw_aff_add(upa
, upa_i
);
8741 isl_multi_union_pw_aff_free(mupa
);
8747 /* Apply "aff" to "mupa". The space of "mupa" needs to be compatible
8748 * with the domain of "aff".
8749 * Furthermore, the dimension of this space needs to be greater than zero.
8750 * The result is defined over the shared domain of the elements of "mupa"
8752 * We perform these checks and then hand over control to
8753 * multi_union_pw_aff_apply_aff.
8755 __isl_give isl_union_pw_aff
*isl_multi_union_pw_aff_apply_aff(
8756 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_aff
*aff
)
8758 isl_space
*space1
, *space2
;
8761 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
8762 isl_aff_get_space(aff
));
8763 aff
= isl_aff_align_params(aff
, isl_multi_union_pw_aff_get_space(mupa
));
8767 space1
= isl_multi_union_pw_aff_get_space(mupa
);
8768 space2
= isl_aff_get_domain_space(aff
);
8769 equal
= isl_space_is_equal(space1
, space2
);
8770 isl_space_free(space1
);
8771 isl_space_free(space2
);
8775 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
8776 "spaces don't match", goto error
);
8777 if (isl_aff_dim(aff
, isl_dim_in
) == 0)
8778 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
8779 "cannot determine domains", goto error
);
8781 return multi_union_pw_aff_apply_aff(mupa
, aff
);
8783 isl_multi_union_pw_aff_free(mupa
);
8788 /* Apply "ma" to "mupa". The space of "mupa" needs to be compatible
8789 * with the domain of "ma".
8790 * Furthermore, the dimension of this space needs to be greater than zero,
8791 * unless the dimension of the target space of "ma" is also zero.
8792 * The result is defined over the shared domain of the elements of "mupa"
8794 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_apply_multi_aff(
8795 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_multi_aff
*ma
)
8797 isl_space
*space1
, *space2
;
8798 isl_multi_union_pw_aff
*res
;
8802 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
8803 isl_multi_aff_get_space(ma
));
8804 ma
= isl_multi_aff_align_params(ma
,
8805 isl_multi_union_pw_aff_get_space(mupa
));
8809 space1
= isl_multi_union_pw_aff_get_space(mupa
);
8810 space2
= isl_multi_aff_get_domain_space(ma
);
8811 equal
= isl_space_is_equal(space1
, space2
);
8812 isl_space_free(space1
);
8813 isl_space_free(space2
);
8817 isl_die(isl_multi_aff_get_ctx(ma
), isl_error_invalid
,
8818 "spaces don't match", goto error
);
8819 n_out
= isl_multi_aff_dim(ma
, isl_dim_out
);
8820 if (isl_multi_aff_dim(ma
, isl_dim_in
) == 0 && n_out
!= 0)
8821 isl_die(isl_multi_aff_get_ctx(ma
), isl_error_invalid
,
8822 "cannot determine domains", goto error
);
8824 space1
= isl_space_range(isl_multi_aff_get_space(ma
));
8825 res
= isl_multi_union_pw_aff_alloc(space1
);
8827 for (i
= 0; i
< n_out
; ++i
) {
8829 isl_union_pw_aff
*upa
;
8831 aff
= isl_multi_aff_get_aff(ma
, i
);
8832 upa
= multi_union_pw_aff_apply_aff(
8833 isl_multi_union_pw_aff_copy(mupa
), aff
);
8834 res
= isl_multi_union_pw_aff_set_union_pw_aff(res
, i
, upa
);
8837 isl_multi_aff_free(ma
);
8838 isl_multi_union_pw_aff_free(mupa
);
8841 isl_multi_union_pw_aff_free(mupa
);
8842 isl_multi_aff_free(ma
);
8846 /* Apply "pa" to "mupa". The space of "mupa" needs to be compatible
8847 * with the domain of "pa".
8848 * Furthermore, the dimension of this space needs to be greater than zero.
8849 * The result is defined over the shared domain of the elements of "mupa"
8851 __isl_give isl_union_pw_aff
*isl_multi_union_pw_aff_apply_pw_aff(
8852 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_pw_aff
*pa
)
8856 isl_space
*space
, *space2
;
8857 isl_union_pw_aff
*upa
;
8859 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
8860 isl_pw_aff_get_space(pa
));
8861 pa
= isl_pw_aff_align_params(pa
,
8862 isl_multi_union_pw_aff_get_space(mupa
));
8866 space
= isl_multi_union_pw_aff_get_space(mupa
);
8867 space2
= isl_pw_aff_get_domain_space(pa
);
8868 equal
= isl_space_is_equal(space
, space2
);
8869 isl_space_free(space
);
8870 isl_space_free(space2
);
8874 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
8875 "spaces don't match", goto error
);
8876 if (isl_pw_aff_dim(pa
, isl_dim_in
) == 0)
8877 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
8878 "cannot determine domains", goto error
);
8880 space
= isl_space_params(isl_multi_union_pw_aff_get_space(mupa
));
8881 upa
= isl_union_pw_aff_empty(space
);
8883 for (i
= 0; i
< pa
->n
; ++i
) {
8886 isl_multi_union_pw_aff
*mupa_i
;
8887 isl_union_pw_aff
*upa_i
;
8889 mupa_i
= isl_multi_union_pw_aff_copy(mupa
);
8890 domain
= isl_set_copy(pa
->p
[i
].set
);
8891 mupa_i
= isl_multi_union_pw_aff_intersect_range(mupa_i
, domain
);
8892 aff
= isl_aff_copy(pa
->p
[i
].aff
);
8893 upa_i
= multi_union_pw_aff_apply_aff(mupa_i
, aff
);
8894 upa
= isl_union_pw_aff_union_add(upa
, upa_i
);
8897 isl_multi_union_pw_aff_free(mupa
);
8898 isl_pw_aff_free(pa
);
8901 isl_multi_union_pw_aff_free(mupa
);
8902 isl_pw_aff_free(pa
);
8906 /* Apply "pma" to "mupa". The space of "mupa" needs to be compatible
8907 * with the domain of "pma".
8908 * Furthermore, the dimension of this space needs to be greater than zero,
8909 * unless the dimension of the target space of "pma" is also zero.
8910 * The result is defined over the shared domain of the elements of "mupa"
8912 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_apply_pw_multi_aff(
8913 __isl_take isl_multi_union_pw_aff
*mupa
,
8914 __isl_take isl_pw_multi_aff
*pma
)
8916 isl_space
*space1
, *space2
;
8917 isl_multi_union_pw_aff
*res
;
8921 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
8922 isl_pw_multi_aff_get_space(pma
));
8923 pma
= isl_pw_multi_aff_align_params(pma
,
8924 isl_multi_union_pw_aff_get_space(mupa
));
8928 space1
= isl_multi_union_pw_aff_get_space(mupa
);
8929 space2
= isl_pw_multi_aff_get_domain_space(pma
);
8930 equal
= isl_space_is_equal(space1
, space2
);
8931 isl_space_free(space1
);
8932 isl_space_free(space2
);
8936 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
8937 "spaces don't match", goto error
);
8938 n_out
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
8939 if (isl_pw_multi_aff_dim(pma
, isl_dim_in
) == 0 && n_out
!= 0)
8940 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
8941 "cannot determine domains", goto error
);
8943 space1
= isl_space_range(isl_pw_multi_aff_get_space(pma
));
8944 res
= isl_multi_union_pw_aff_alloc(space1
);
8946 for (i
= 0; i
< n_out
; ++i
) {
8948 isl_union_pw_aff
*upa
;
8950 pa
= isl_pw_multi_aff_get_pw_aff(pma
, i
);
8951 upa
= isl_multi_union_pw_aff_apply_pw_aff(
8952 isl_multi_union_pw_aff_copy(mupa
), pa
);
8953 res
= isl_multi_union_pw_aff_set_union_pw_aff(res
, i
, upa
);
8956 isl_pw_multi_aff_free(pma
);
8957 isl_multi_union_pw_aff_free(mupa
);
8960 isl_multi_union_pw_aff_free(mupa
);
8961 isl_pw_multi_aff_free(pma
);
8965 /* Compute the pullback of "mupa" by the function represented by "upma".
8966 * In other words, plug in "upma" in "mupa". The result contains
8967 * expressions defined over the domain space of "upma".
8969 * Run over all elements of "mupa" and plug in "upma" in each of them.
8971 __isl_give isl_multi_union_pw_aff
*
8972 isl_multi_union_pw_aff_pullback_union_pw_multi_aff(
8973 __isl_take isl_multi_union_pw_aff
*mupa
,
8974 __isl_take isl_union_pw_multi_aff
*upma
)
8978 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
8979 isl_union_pw_multi_aff_get_space(upma
));
8980 upma
= isl_union_pw_multi_aff_align_params(upma
,
8981 isl_multi_union_pw_aff_get_space(mupa
));
8985 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8986 for (i
= 0; i
< n
; ++i
) {
8987 isl_union_pw_aff
*upa
;
8989 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
8990 upa
= isl_union_pw_aff_pullback_union_pw_multi_aff(upa
,
8991 isl_union_pw_multi_aff_copy(upma
));
8992 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
8995 isl_union_pw_multi_aff_free(upma
);
8998 isl_multi_union_pw_aff_free(mupa
);
8999 isl_union_pw_multi_aff_free(upma
);
9003 /* Extract the sequence of elements in "mupa" with domain space "space"
9004 * (ignoring parameters).
9006 * For the elements of "mupa" that are not defined on the specified space,
9007 * the corresponding element in the result is empty.
9009 __isl_give isl_multi_pw_aff
*isl_multi_union_pw_aff_extract_multi_pw_aff(
9010 __isl_keep isl_multi_union_pw_aff
*mupa
, __isl_take isl_space
*space
)
9013 isl_bool equal_params
;
9014 isl_space
*space_mpa
= NULL
;
9015 isl_multi_pw_aff
*mpa
;
9017 if (!mupa
|| !space
)
9020 space_mpa
= isl_multi_union_pw_aff_get_space(mupa
);
9021 equal_params
= isl_space_has_equal_params(space_mpa
, space
);
9022 if (equal_params
< 0)
9024 if (!equal_params
) {
9025 space
= isl_space_drop_dims(space
, isl_dim_param
,
9026 0, isl_space_dim(space
, isl_dim_param
));
9027 space
= isl_space_align_params(space
,
9028 isl_space_copy(space_mpa
));
9032 space_mpa
= isl_space_map_from_domain_and_range(isl_space_copy(space
),
9034 mpa
= isl_multi_pw_aff_alloc(space_mpa
);
9036 space
= isl_space_from_domain(space
);
9037 space
= isl_space_add_dims(space
, isl_dim_out
, 1);
9038 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
9039 for (i
= 0; i
< n
; ++i
) {
9040 isl_union_pw_aff
*upa
;
9043 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
9044 pa
= isl_union_pw_aff_extract_pw_aff(upa
,
9045 isl_space_copy(space
));
9046 mpa
= isl_multi_pw_aff_set_pw_aff(mpa
, i
, pa
);
9047 isl_union_pw_aff_free(upa
);
9050 isl_space_free(space
);
9053 isl_space_free(space_mpa
);
9054 isl_space_free(space
);