2 * Copyright 2011 INRIA Saclay
3 * Copyright 2011 Sven Verdoolaege
4 * Copyright 2012-2014 Ecole Normale Superieure
5 * Copyright 2014 INRIA Rocquencourt
7 * Use of this software is governed by the MIT license
9 * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
10 * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
12 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
13 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
14 * B.P. 105 - 78153 Le Chesnay, France
17 #include <isl_ctx_private.h>
18 #include <isl_map_private.h>
19 #include <isl_union_map_private.h>
20 #include <isl_aff_private.h>
21 #include <isl_space_private.h>
22 #include <isl_local_space_private.h>
23 #include <isl_vec_private.h>
24 #include <isl_mat_private.h>
26 #include <isl/constraint.h>
29 #include <isl_val_private.h>
30 #include <isl_point_private.h>
31 #include <isl_config.h>
36 #include <isl_list_templ.c>
41 #include <isl_list_templ.c>
44 #define BASE pw_multi_aff
46 #include <isl_list_templ.c>
49 #define BASE union_pw_aff
51 #include <isl_list_templ.c>
54 #define BASE union_pw_multi_aff
56 #include <isl_list_templ.c>
58 __isl_give isl_aff
*isl_aff_alloc_vec(__isl_take isl_local_space
*ls
,
59 __isl_take isl_vec
*v
)
66 aff
= isl_calloc_type(v
->ctx
, struct isl_aff
);
76 isl_local_space_free(ls
);
81 __isl_give isl_aff
*isl_aff_alloc(__isl_take isl_local_space
*ls
)
90 ctx
= isl_local_space_get_ctx(ls
);
91 if (!isl_local_space_divs_known(ls
))
92 isl_die(ctx
, isl_error_invalid
, "local space has unknown divs",
94 if (!isl_local_space_is_set(ls
))
95 isl_die(ctx
, isl_error_invalid
,
96 "domain of affine expression should be a set",
99 total
= isl_local_space_dim(ls
, isl_dim_all
);
102 v
= isl_vec_alloc(ctx
, 1 + 1 + total
);
103 return isl_aff_alloc_vec(ls
, v
);
105 isl_local_space_free(ls
);
109 __isl_give isl_aff
*isl_aff_zero_on_domain(__isl_take isl_local_space
*ls
)
113 aff
= isl_aff_alloc(ls
);
117 isl_int_set_si(aff
->v
->el
[0], 1);
118 isl_seq_clr(aff
->v
->el
+ 1, aff
->v
->size
- 1);
123 /* Return a piecewise affine expression defined on the specified domain
124 * that is equal to zero.
126 __isl_give isl_pw_aff
*isl_pw_aff_zero_on_domain(__isl_take isl_local_space
*ls
)
128 return isl_pw_aff_from_aff(isl_aff_zero_on_domain(ls
));
131 /* Return an affine expression defined on the specified domain
132 * that represents NaN.
134 __isl_give isl_aff
*isl_aff_nan_on_domain(__isl_take isl_local_space
*ls
)
138 aff
= isl_aff_alloc(ls
);
142 isl_seq_clr(aff
->v
->el
, aff
->v
->size
);
147 /* Return a piecewise affine expression defined on the specified domain
148 * that represents NaN.
150 __isl_give isl_pw_aff
*isl_pw_aff_nan_on_domain(__isl_take isl_local_space
*ls
)
152 return isl_pw_aff_from_aff(isl_aff_nan_on_domain(ls
));
155 /* Return an affine expression that is equal to "val" on
156 * domain local space "ls".
158 __isl_give isl_aff
*isl_aff_val_on_domain(__isl_take isl_local_space
*ls
,
159 __isl_take isl_val
*val
)
165 if (!isl_val_is_rat(val
))
166 isl_die(isl_val_get_ctx(val
), isl_error_invalid
,
167 "expecting rational value", goto error
);
169 aff
= isl_aff_alloc(isl_local_space_copy(ls
));
173 isl_seq_clr(aff
->v
->el
+ 2, aff
->v
->size
- 2);
174 isl_int_set(aff
->v
->el
[1], val
->n
);
175 isl_int_set(aff
->v
->el
[0], val
->d
);
177 isl_local_space_free(ls
);
181 isl_local_space_free(ls
);
186 /* Return an affine expression that is equal to the specified dimension
189 __isl_give isl_aff
*isl_aff_var_on_domain(__isl_take isl_local_space
*ls
,
190 enum isl_dim_type type
, unsigned pos
)
198 space
= isl_local_space_get_space(ls
);
201 if (isl_space_is_map(space
))
202 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
203 "expecting (parameter) set space", goto error
);
204 if (isl_local_space_check_range(ls
, type
, pos
, 1) < 0)
207 isl_space_free(space
);
208 aff
= isl_aff_alloc(ls
);
212 pos
+= isl_local_space_offset(aff
->ls
, type
);
214 isl_int_set_si(aff
->v
->el
[0], 1);
215 isl_seq_clr(aff
->v
->el
+ 1, aff
->v
->size
- 1);
216 isl_int_set_si(aff
->v
->el
[1 + pos
], 1);
220 isl_local_space_free(ls
);
221 isl_space_free(space
);
225 /* Return a piecewise affine expression that is equal to
226 * the specified dimension in "ls".
228 __isl_give isl_pw_aff
*isl_pw_aff_var_on_domain(__isl_take isl_local_space
*ls
,
229 enum isl_dim_type type
, unsigned pos
)
231 return isl_pw_aff_from_aff(isl_aff_var_on_domain(ls
, type
, pos
));
234 /* Return an affine expression that is equal to the parameter
235 * in the domain space "space" with identifier "id".
237 __isl_give isl_aff
*isl_aff_param_on_domain_space_id(
238 __isl_take isl_space
*space
, __isl_take isl_id
*id
)
245 pos
= isl_space_find_dim_by_id(space
, isl_dim_param
, id
);
247 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
248 "parameter not found in space", goto error
);
250 ls
= isl_local_space_from_space(space
);
251 return isl_aff_var_on_domain(ls
, isl_dim_param
, pos
);
253 isl_space_free(space
);
258 __isl_give isl_aff
*isl_aff_copy(__isl_keep isl_aff
*aff
)
267 __isl_give isl_aff
*isl_aff_dup(__isl_keep isl_aff
*aff
)
272 return isl_aff_alloc_vec(isl_local_space_copy(aff
->ls
),
273 isl_vec_copy(aff
->v
));
276 __isl_give isl_aff
*isl_aff_cow(__isl_take isl_aff
*aff
)
284 return isl_aff_dup(aff
);
287 __isl_null isl_aff
*isl_aff_free(__isl_take isl_aff
*aff
)
295 isl_local_space_free(aff
->ls
);
296 isl_vec_free(aff
->v
);
303 isl_ctx
*isl_aff_get_ctx(__isl_keep isl_aff
*aff
)
305 return aff
? isl_local_space_get_ctx(aff
->ls
) : NULL
;
308 /* Return a hash value that digests "aff".
310 uint32_t isl_aff_get_hash(__isl_keep isl_aff
*aff
)
312 uint32_t hash
, ls_hash
, v_hash
;
317 hash
= isl_hash_init();
318 ls_hash
= isl_local_space_get_hash(aff
->ls
);
319 isl_hash_hash(hash
, ls_hash
);
320 v_hash
= isl_vec_get_hash(aff
->v
);
321 isl_hash_hash(hash
, v_hash
);
326 /* Externally, an isl_aff has a map space, but internally, the
327 * ls field corresponds to the domain of that space.
329 isl_size
isl_aff_dim(__isl_keep isl_aff
*aff
, enum isl_dim_type type
)
332 return isl_size_error
;
333 if (type
== isl_dim_out
)
335 if (type
== isl_dim_in
)
337 return isl_local_space_dim(aff
->ls
, type
);
340 /* Return the position of the dimension of the given type and name
342 * Return -1 if no such dimension can be found.
344 int isl_aff_find_dim_by_name(__isl_keep isl_aff
*aff
, enum isl_dim_type type
,
349 if (type
== isl_dim_out
)
351 if (type
== isl_dim_in
)
353 return isl_local_space_find_dim_by_name(aff
->ls
, type
, name
);
356 /* Return the domain space of "aff".
358 static __isl_keep isl_space
*isl_aff_peek_domain_space(__isl_keep isl_aff
*aff
)
360 return aff
? isl_local_space_peek_space(aff
->ls
) : NULL
;
363 __isl_give isl_space
*isl_aff_get_domain_space(__isl_keep isl_aff
*aff
)
365 return isl_space_copy(isl_aff_peek_domain_space(aff
));
368 __isl_give isl_space
*isl_aff_get_space(__isl_keep isl_aff
*aff
)
373 space
= isl_local_space_get_space(aff
->ls
);
374 space
= isl_space_from_domain(space
);
375 space
= isl_space_add_dims(space
, isl_dim_out
, 1);
379 __isl_give isl_local_space
*isl_aff_get_domain_local_space(
380 __isl_keep isl_aff
*aff
)
382 return aff
? isl_local_space_copy(aff
->ls
) : NULL
;
385 __isl_give isl_local_space
*isl_aff_get_local_space(__isl_keep isl_aff
*aff
)
390 ls
= isl_local_space_copy(aff
->ls
);
391 ls
= isl_local_space_from_domain(ls
);
392 ls
= isl_local_space_add_dims(ls
, isl_dim_out
, 1);
396 /* Return the local space of the domain of "aff".
397 * This may be either a copy or the local space itself
398 * if there is only one reference to "aff".
399 * This allows the local space to be modified inplace
400 * if both the expression and its local space have only a single reference.
401 * The caller is not allowed to modify "aff" between this call and
402 * a subsequent call to isl_aff_restore_domain_local_space.
403 * The only exception is that isl_aff_free can be called instead.
405 __isl_give isl_local_space
*isl_aff_take_domain_local_space(
406 __isl_keep isl_aff
*aff
)
413 return isl_aff_get_domain_local_space(aff
);
419 /* Set the local space of the domain of "aff" to "ls",
420 * where the local space of "aff" may be missing
421 * due to a preceding call to isl_aff_take_domain_local_space.
422 * However, in this case, "aff" only has a single reference and
423 * then the call to isl_aff_cow has no effect.
425 __isl_give isl_aff
*isl_aff_restore_domain_local_space(
426 __isl_keep isl_aff
*aff
, __isl_take isl_local_space
*ls
)
432 isl_local_space_free(ls
);
436 aff
= isl_aff_cow(aff
);
439 isl_local_space_free(aff
->ls
);
445 isl_local_space_free(ls
);
449 /* Externally, an isl_aff has a map space, but internally, the
450 * ls field corresponds to the domain of that space.
452 const char *isl_aff_get_dim_name(__isl_keep isl_aff
*aff
,
453 enum isl_dim_type type
, unsigned pos
)
457 if (type
== isl_dim_out
)
459 if (type
== isl_dim_in
)
461 return isl_local_space_get_dim_name(aff
->ls
, type
, pos
);
464 __isl_give isl_aff
*isl_aff_reset_domain_space(__isl_take isl_aff
*aff
,
465 __isl_take isl_space
*dim
)
467 aff
= isl_aff_cow(aff
);
471 aff
->ls
= isl_local_space_reset_space(aff
->ls
, dim
);
473 return isl_aff_free(aff
);
482 /* Reset the space of "aff". This function is called from isl_pw_templ.c
483 * and doesn't know if the space of an element object is represented
484 * directly or through its domain. It therefore passes along both.
486 __isl_give isl_aff
*isl_aff_reset_space_and_domain(__isl_take isl_aff
*aff
,
487 __isl_take isl_space
*space
, __isl_take isl_space
*domain
)
489 isl_space_free(space
);
490 return isl_aff_reset_domain_space(aff
, domain
);
493 /* Reorder the coefficients of the affine expression based
494 * on the given reordering.
495 * The reordering r is assumed to have been extended with the local
498 static __isl_give isl_vec
*vec_reorder(__isl_take isl_vec
*vec
,
499 __isl_take isl_reordering
*r
, int n_div
)
509 space
= isl_reordering_peek_space(r
);
510 dim
= isl_space_dim(space
, isl_dim_all
);
513 res
= isl_vec_alloc(vec
->ctx
, 2 + dim
+ n_div
);
516 isl_seq_cpy(res
->el
, vec
->el
, 2);
517 isl_seq_clr(res
->el
+ 2, res
->size
- 2);
518 for (i
= 0; i
< r
->len
; ++i
)
519 isl_int_set(res
->el
[2 + r
->pos
[i
]], vec
->el
[2 + i
]);
521 isl_reordering_free(r
);
526 isl_reordering_free(r
);
530 /* Reorder the dimensions of the domain of "aff" according
531 * to the given reordering.
533 __isl_give isl_aff
*isl_aff_realign_domain(__isl_take isl_aff
*aff
,
534 __isl_take isl_reordering
*r
)
536 aff
= isl_aff_cow(aff
);
540 r
= isl_reordering_extend(r
, aff
->ls
->div
->n_row
);
541 aff
->v
= vec_reorder(aff
->v
, isl_reordering_copy(r
),
542 aff
->ls
->div
->n_row
);
543 aff
->ls
= isl_local_space_realign(aff
->ls
, r
);
545 if (!aff
->v
|| !aff
->ls
)
546 return isl_aff_free(aff
);
551 isl_reordering_free(r
);
555 __isl_give isl_aff
*isl_aff_align_params(__isl_take isl_aff
*aff
,
556 __isl_take isl_space
*model
)
558 isl_bool equal_params
;
563 equal_params
= isl_space_has_equal_params(aff
->ls
->dim
, model
);
564 if (equal_params
< 0)
569 exp
= isl_parameter_alignment_reordering(aff
->ls
->dim
, model
);
570 exp
= isl_reordering_extend_space(exp
,
571 isl_aff_get_domain_space(aff
));
572 aff
= isl_aff_realign_domain(aff
, exp
);
575 isl_space_free(model
);
578 isl_space_free(model
);
583 /* Is "aff" obviously equal to zero?
585 * If the denominator is zero, then "aff" is not equal to zero.
587 isl_bool
isl_aff_plain_is_zero(__isl_keep isl_aff
*aff
)
590 return isl_bool_error
;
592 if (isl_int_is_zero(aff
->v
->el
[0]))
593 return isl_bool_false
;
594 return isl_seq_first_non_zero(aff
->v
->el
+ 1, aff
->v
->size
- 1) < 0;
597 /* Does "aff" represent NaN?
599 isl_bool
isl_aff_is_nan(__isl_keep isl_aff
*aff
)
602 return isl_bool_error
;
604 return isl_bool_ok(isl_seq_first_non_zero(aff
->v
->el
, 2) < 0);
607 /* Are "aff1" and "aff2" obviously equal?
609 * NaN is not equal to anything, not even to another NaN.
611 isl_bool
isl_aff_plain_is_equal(__isl_keep isl_aff
*aff1
,
612 __isl_keep isl_aff
*aff2
)
617 return isl_bool_error
;
619 if (isl_aff_is_nan(aff1
) || isl_aff_is_nan(aff2
))
620 return isl_bool_false
;
622 equal
= isl_local_space_is_equal(aff1
->ls
, aff2
->ls
);
623 if (equal
< 0 || !equal
)
626 return isl_vec_is_equal(aff1
->v
, aff2
->v
);
629 /* Return the common denominator of "aff" in "v".
631 * We cannot return anything meaningful in case of a NaN.
633 isl_stat
isl_aff_get_denominator(__isl_keep isl_aff
*aff
, isl_int
*v
)
636 return isl_stat_error
;
637 if (isl_aff_is_nan(aff
))
638 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
639 "cannot get denominator of NaN", return isl_stat_error
);
640 isl_int_set(*v
, aff
->v
->el
[0]);
644 /* Return the common denominator of "aff".
646 __isl_give isl_val
*isl_aff_get_denominator_val(__isl_keep isl_aff
*aff
)
653 ctx
= isl_aff_get_ctx(aff
);
654 if (isl_aff_is_nan(aff
))
655 return isl_val_nan(ctx
);
656 return isl_val_int_from_isl_int(ctx
, aff
->v
->el
[0]);
659 /* Return the constant term of "aff".
661 __isl_give isl_val
*isl_aff_get_constant_val(__isl_keep isl_aff
*aff
)
669 ctx
= isl_aff_get_ctx(aff
);
670 if (isl_aff_is_nan(aff
))
671 return isl_val_nan(ctx
);
672 v
= isl_val_rat_from_isl_int(ctx
, aff
->v
->el
[1], aff
->v
->el
[0]);
673 return isl_val_normalize(v
);
676 /* Return the coefficient of the variable of type "type" at position "pos"
679 __isl_give isl_val
*isl_aff_get_coefficient_val(__isl_keep isl_aff
*aff
,
680 enum isl_dim_type type
, int pos
)
688 ctx
= isl_aff_get_ctx(aff
);
689 if (type
== isl_dim_out
)
690 isl_die(ctx
, isl_error_invalid
,
691 "output/set dimension does not have a coefficient",
693 if (type
== isl_dim_in
)
696 if (isl_local_space_check_range(aff
->ls
, type
, pos
, 1) < 0)
699 if (isl_aff_is_nan(aff
))
700 return isl_val_nan(ctx
);
701 pos
+= isl_local_space_offset(aff
->ls
, type
);
702 v
= isl_val_rat_from_isl_int(ctx
, aff
->v
->el
[1 + pos
], aff
->v
->el
[0]);
703 return isl_val_normalize(v
);
706 /* Return the sign of the coefficient of the variable of type "type"
707 * at position "pos" of "aff".
709 int isl_aff_coefficient_sgn(__isl_keep isl_aff
*aff
, enum isl_dim_type type
,
717 ctx
= isl_aff_get_ctx(aff
);
718 if (type
== isl_dim_out
)
719 isl_die(ctx
, isl_error_invalid
,
720 "output/set dimension does not have a coefficient",
722 if (type
== isl_dim_in
)
725 if (isl_local_space_check_range(aff
->ls
, type
, pos
, 1) < 0)
728 pos
+= isl_local_space_offset(aff
->ls
, type
);
729 return isl_int_sgn(aff
->v
->el
[1 + pos
]);
732 /* Replace the numerator of the constant term of "aff" by "v".
734 * A NaN is unaffected by this operation.
736 __isl_give isl_aff
*isl_aff_set_constant(__isl_take isl_aff
*aff
, isl_int v
)
740 if (isl_aff_is_nan(aff
))
742 aff
= isl_aff_cow(aff
);
746 aff
->v
= isl_vec_cow(aff
->v
);
748 return isl_aff_free(aff
);
750 isl_int_set(aff
->v
->el
[1], v
);
755 /* Replace the constant term of "aff" by "v".
757 * A NaN is unaffected by this operation.
759 __isl_give isl_aff
*isl_aff_set_constant_val(__isl_take isl_aff
*aff
,
760 __isl_take isl_val
*v
)
765 if (isl_aff_is_nan(aff
)) {
770 if (!isl_val_is_rat(v
))
771 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
772 "expecting rational value", goto error
);
774 if (isl_int_eq(aff
->v
->el
[1], v
->n
) &&
775 isl_int_eq(aff
->v
->el
[0], v
->d
)) {
780 aff
= isl_aff_cow(aff
);
783 aff
->v
= isl_vec_cow(aff
->v
);
787 if (isl_int_eq(aff
->v
->el
[0], v
->d
)) {
788 isl_int_set(aff
->v
->el
[1], v
->n
);
789 } else if (isl_int_is_one(v
->d
)) {
790 isl_int_mul(aff
->v
->el
[1], aff
->v
->el
[0], v
->n
);
792 isl_seq_scale(aff
->v
->el
+ 1,
793 aff
->v
->el
+ 1, v
->d
, aff
->v
->size
- 1);
794 isl_int_mul(aff
->v
->el
[1], aff
->v
->el
[0], v
->n
);
795 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], v
->d
);
796 aff
->v
= isl_vec_normalize(aff
->v
);
809 /* Add "v" to the constant term of "aff".
811 * A NaN is unaffected by this operation.
813 __isl_give isl_aff
*isl_aff_add_constant(__isl_take isl_aff
*aff
, isl_int v
)
815 if (isl_int_is_zero(v
))
820 if (isl_aff_is_nan(aff
))
822 aff
= isl_aff_cow(aff
);
826 aff
->v
= isl_vec_cow(aff
->v
);
828 return isl_aff_free(aff
);
830 isl_int_addmul(aff
->v
->el
[1], aff
->v
->el
[0], v
);
835 /* Add "v" to the constant term of "aff".
837 * A NaN is unaffected by this operation.
839 __isl_give isl_aff
*isl_aff_add_constant_val(__isl_take isl_aff
*aff
,
840 __isl_take isl_val
*v
)
845 if (isl_aff_is_nan(aff
) || isl_val_is_zero(v
)) {
850 if (!isl_val_is_rat(v
))
851 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
852 "expecting rational value", goto error
);
854 aff
= isl_aff_cow(aff
);
858 aff
->v
= isl_vec_cow(aff
->v
);
862 if (isl_int_is_one(v
->d
)) {
863 isl_int_addmul(aff
->v
->el
[1], aff
->v
->el
[0], v
->n
);
864 } else if (isl_int_eq(aff
->v
->el
[0], v
->d
)) {
865 isl_int_add(aff
->v
->el
[1], aff
->v
->el
[1], v
->n
);
866 aff
->v
= isl_vec_normalize(aff
->v
);
870 isl_seq_scale(aff
->v
->el
+ 1,
871 aff
->v
->el
+ 1, v
->d
, aff
->v
->size
- 1);
872 isl_int_addmul(aff
->v
->el
[1], aff
->v
->el
[0], v
->n
);
873 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], v
->d
);
874 aff
->v
= isl_vec_normalize(aff
->v
);
887 __isl_give isl_aff
*isl_aff_add_constant_si(__isl_take isl_aff
*aff
, int v
)
892 isl_int_set_si(t
, v
);
893 aff
= isl_aff_add_constant(aff
, t
);
899 /* Add "v" to the numerator of the constant term of "aff".
901 * A NaN is unaffected by this operation.
903 __isl_give isl_aff
*isl_aff_add_constant_num(__isl_take isl_aff
*aff
, isl_int v
)
905 if (isl_int_is_zero(v
))
910 if (isl_aff_is_nan(aff
))
912 aff
= isl_aff_cow(aff
);
916 aff
->v
= isl_vec_cow(aff
->v
);
918 return isl_aff_free(aff
);
920 isl_int_add(aff
->v
->el
[1], aff
->v
->el
[1], v
);
925 /* Add "v" to the numerator of the constant term of "aff".
927 * A NaN is unaffected by this operation.
929 __isl_give isl_aff
*isl_aff_add_constant_num_si(__isl_take isl_aff
*aff
, int v
)
937 isl_int_set_si(t
, v
);
938 aff
= isl_aff_add_constant_num(aff
, t
);
944 /* Replace the numerator of the constant term of "aff" by "v".
946 * A NaN is unaffected by this operation.
948 __isl_give isl_aff
*isl_aff_set_constant_si(__isl_take isl_aff
*aff
, int v
)
952 if (isl_aff_is_nan(aff
))
954 aff
= isl_aff_cow(aff
);
958 aff
->v
= isl_vec_cow(aff
->v
);
960 return isl_aff_free(aff
);
962 isl_int_set_si(aff
->v
->el
[1], v
);
967 /* Replace the numerator of the coefficient of the variable of type "type"
968 * at position "pos" of "aff" by "v".
970 * A NaN is unaffected by this operation.
972 __isl_give isl_aff
*isl_aff_set_coefficient(__isl_take isl_aff
*aff
,
973 enum isl_dim_type type
, int pos
, isl_int v
)
978 if (type
== isl_dim_out
)
979 isl_die(aff
->v
->ctx
, isl_error_invalid
,
980 "output/set dimension does not have a coefficient",
981 return isl_aff_free(aff
));
982 if (type
== isl_dim_in
)
985 if (isl_local_space_check_range(aff
->ls
, type
, pos
, 1) < 0)
986 return isl_aff_free(aff
);
988 if (isl_aff_is_nan(aff
))
990 aff
= isl_aff_cow(aff
);
994 aff
->v
= isl_vec_cow(aff
->v
);
996 return isl_aff_free(aff
);
998 pos
+= isl_local_space_offset(aff
->ls
, type
);
999 isl_int_set(aff
->v
->el
[1 + pos
], v
);
1004 /* Replace the numerator of the coefficient of the variable of type "type"
1005 * at position "pos" of "aff" by "v".
1007 * A NaN is unaffected by this operation.
1009 __isl_give isl_aff
*isl_aff_set_coefficient_si(__isl_take isl_aff
*aff
,
1010 enum isl_dim_type type
, int pos
, int v
)
1015 if (type
== isl_dim_out
)
1016 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1017 "output/set dimension does not have a coefficient",
1018 return isl_aff_free(aff
));
1019 if (type
== isl_dim_in
)
1022 if (isl_local_space_check_range(aff
->ls
, type
, pos
, 1) < 0)
1023 return isl_aff_free(aff
);
1025 if (isl_aff_is_nan(aff
))
1027 pos
+= isl_local_space_offset(aff
->ls
, type
);
1028 if (isl_int_cmp_si(aff
->v
->el
[1 + pos
], v
) == 0)
1031 aff
= isl_aff_cow(aff
);
1035 aff
->v
= isl_vec_cow(aff
->v
);
1037 return isl_aff_free(aff
);
1039 isl_int_set_si(aff
->v
->el
[1 + pos
], v
);
1044 /* Replace the coefficient of the variable of type "type" at position "pos"
1047 * A NaN is unaffected by this operation.
1049 __isl_give isl_aff
*isl_aff_set_coefficient_val(__isl_take isl_aff
*aff
,
1050 enum isl_dim_type type
, int pos
, __isl_take isl_val
*v
)
1055 if (type
== isl_dim_out
)
1056 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1057 "output/set dimension does not have a coefficient",
1059 if (type
== isl_dim_in
)
1062 if (isl_local_space_check_range(aff
->ls
, type
, pos
, 1) < 0)
1063 return isl_aff_free(aff
);
1065 if (isl_aff_is_nan(aff
)) {
1069 if (!isl_val_is_rat(v
))
1070 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1071 "expecting rational value", goto error
);
1073 pos
+= isl_local_space_offset(aff
->ls
, type
);
1074 if (isl_int_eq(aff
->v
->el
[1 + pos
], v
->n
) &&
1075 isl_int_eq(aff
->v
->el
[0], v
->d
)) {
1080 aff
= isl_aff_cow(aff
);
1083 aff
->v
= isl_vec_cow(aff
->v
);
1087 if (isl_int_eq(aff
->v
->el
[0], v
->d
)) {
1088 isl_int_set(aff
->v
->el
[1 + pos
], v
->n
);
1089 } else if (isl_int_is_one(v
->d
)) {
1090 isl_int_mul(aff
->v
->el
[1 + pos
], aff
->v
->el
[0], v
->n
);
1092 isl_seq_scale(aff
->v
->el
+ 1,
1093 aff
->v
->el
+ 1, v
->d
, aff
->v
->size
- 1);
1094 isl_int_mul(aff
->v
->el
[1 + pos
], aff
->v
->el
[0], v
->n
);
1095 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], v
->d
);
1096 aff
->v
= isl_vec_normalize(aff
->v
);
1109 /* Add "v" to the coefficient of the variable of type "type"
1110 * at position "pos" of "aff".
1112 * A NaN is unaffected by this operation.
1114 __isl_give isl_aff
*isl_aff_add_coefficient(__isl_take isl_aff
*aff
,
1115 enum isl_dim_type type
, int pos
, isl_int v
)
1120 if (type
== isl_dim_out
)
1121 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1122 "output/set dimension does not have a coefficient",
1123 return isl_aff_free(aff
));
1124 if (type
== isl_dim_in
)
1127 if (isl_local_space_check_range(aff
->ls
, type
, pos
, 1) < 0)
1128 return isl_aff_free(aff
);
1130 if (isl_aff_is_nan(aff
))
1132 aff
= isl_aff_cow(aff
);
1136 aff
->v
= isl_vec_cow(aff
->v
);
1138 return isl_aff_free(aff
);
1140 pos
+= isl_local_space_offset(aff
->ls
, type
);
1141 isl_int_addmul(aff
->v
->el
[1 + pos
], aff
->v
->el
[0], v
);
1146 /* Add "v" to the coefficient of the variable of type "type"
1147 * at position "pos" of "aff".
1149 * A NaN is unaffected by this operation.
1151 __isl_give isl_aff
*isl_aff_add_coefficient_val(__isl_take isl_aff
*aff
,
1152 enum isl_dim_type type
, int pos
, __isl_take isl_val
*v
)
1157 if (isl_val_is_zero(v
)) {
1162 if (type
== isl_dim_out
)
1163 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1164 "output/set dimension does not have a coefficient",
1166 if (type
== isl_dim_in
)
1169 if (isl_local_space_check_range(aff
->ls
, type
, pos
, 1) < 0)
1172 if (isl_aff_is_nan(aff
)) {
1176 if (!isl_val_is_rat(v
))
1177 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1178 "expecting rational value", goto error
);
1180 aff
= isl_aff_cow(aff
);
1184 aff
->v
= isl_vec_cow(aff
->v
);
1188 pos
+= isl_local_space_offset(aff
->ls
, type
);
1189 if (isl_int_is_one(v
->d
)) {
1190 isl_int_addmul(aff
->v
->el
[1 + pos
], aff
->v
->el
[0], v
->n
);
1191 } else if (isl_int_eq(aff
->v
->el
[0], v
->d
)) {
1192 isl_int_add(aff
->v
->el
[1 + pos
], aff
->v
->el
[1 + pos
], v
->n
);
1193 aff
->v
= isl_vec_normalize(aff
->v
);
1197 isl_seq_scale(aff
->v
->el
+ 1,
1198 aff
->v
->el
+ 1, v
->d
, aff
->v
->size
- 1);
1199 isl_int_addmul(aff
->v
->el
[1 + pos
], aff
->v
->el
[0], v
->n
);
1200 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], v
->d
);
1201 aff
->v
= isl_vec_normalize(aff
->v
);
1214 __isl_give isl_aff
*isl_aff_add_coefficient_si(__isl_take isl_aff
*aff
,
1215 enum isl_dim_type type
, int pos
, int v
)
1220 isl_int_set_si(t
, v
);
1221 aff
= isl_aff_add_coefficient(aff
, type
, pos
, t
);
1227 __isl_give isl_aff
*isl_aff_get_div(__isl_keep isl_aff
*aff
, int pos
)
1232 return isl_local_space_get_div(aff
->ls
, pos
);
1235 /* Return the negation of "aff".
1237 * As a special case, -NaN = NaN.
1239 __isl_give isl_aff
*isl_aff_neg(__isl_take isl_aff
*aff
)
1243 if (isl_aff_is_nan(aff
))
1245 aff
= isl_aff_cow(aff
);
1248 aff
->v
= isl_vec_cow(aff
->v
);
1250 return isl_aff_free(aff
);
1252 isl_seq_neg(aff
->v
->el
+ 1, aff
->v
->el
+ 1, aff
->v
->size
- 1);
1257 /* Remove divs from the local space that do not appear in the affine
1259 * We currently only remove divs at the end.
1260 * Some intermediate divs may also not appear directly in the affine
1261 * expression, but we would also need to check that no other divs are
1262 * defined in terms of them.
1264 __isl_give isl_aff
*isl_aff_remove_unused_divs(__isl_take isl_aff
*aff
)
1273 n
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
1275 return isl_aff_free(aff
);
1276 off
= isl_local_space_offset(aff
->ls
, isl_dim_div
);
1278 pos
= isl_seq_last_non_zero(aff
->v
->el
+ 1 + off
, n
) + 1;
1282 aff
= isl_aff_cow(aff
);
1286 aff
->ls
= isl_local_space_drop_dims(aff
->ls
, isl_dim_div
, pos
, n
- pos
);
1287 aff
->v
= isl_vec_drop_els(aff
->v
, 1 + off
+ pos
, n
- pos
);
1288 if (!aff
->ls
|| !aff
->v
)
1289 return isl_aff_free(aff
);
1294 /* Look for any divs in the aff->ls with a denominator equal to one
1295 * and plug them into the affine expression and any subsequent divs
1296 * that may reference the div.
1298 static __isl_give isl_aff
*plug_in_integral_divs(__isl_take isl_aff
*aff
)
1305 isl_local_space
*ls
;
1311 n
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
1313 return isl_aff_free(aff
);
1315 for (i
= 0; i
< n
; ++i
) {
1316 if (!isl_int_is_one(aff
->ls
->div
->row
[i
][0]))
1318 ls
= isl_local_space_copy(aff
->ls
);
1319 ls
= isl_local_space_substitute_seq(ls
, isl_dim_div
, i
,
1320 aff
->ls
->div
->row
[i
], len
, i
+ 1, n
- (i
+ 1));
1321 vec
= isl_vec_copy(aff
->v
);
1322 vec
= isl_vec_cow(vec
);
1328 pos
= isl_local_space_offset(aff
->ls
, isl_dim_div
) + i
;
1329 isl_seq_substitute(vec
->el
, pos
, aff
->ls
->div
->row
[i
],
1334 isl_vec_free(aff
->v
);
1336 isl_local_space_free(aff
->ls
);
1343 isl_local_space_free(ls
);
1344 return isl_aff_free(aff
);
1347 /* Look for any divs j that appear with a unit coefficient inside
1348 * the definitions of other divs i and plug them into the definitions
1351 * In particular, an expression of the form
1353 * floor((f(..) + floor(g(..)/n))/m)
1357 * floor((n * f(..) + g(..))/(n * m))
1359 * This simplification is correct because we can move the expression
1360 * f(..) into the inner floor in the original expression to obtain
1362 * floor(floor((n * f(..) + g(..))/n)/m)
1364 * from which we can derive the simplified expression.
1366 static __isl_give isl_aff
*plug_in_unit_divs(__isl_take isl_aff
*aff
)
1375 n
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
1377 return isl_aff_free(aff
);
1378 off
= isl_local_space_offset(aff
->ls
, isl_dim_div
);
1379 for (i
= 1; i
< n
; ++i
) {
1380 for (j
= 0; j
< i
; ++j
) {
1381 if (!isl_int_is_one(aff
->ls
->div
->row
[i
][1 + off
+ j
]))
1383 aff
->ls
= isl_local_space_substitute_seq(aff
->ls
,
1384 isl_dim_div
, j
, aff
->ls
->div
->row
[j
],
1385 aff
->v
->size
, i
, 1);
1387 return isl_aff_free(aff
);
1394 /* Swap divs "a" and "b" in "aff", which is assumed to be non-NULL.
1396 * Even though this function is only called on isl_affs with a single
1397 * reference, we are careful to only change aff->v and aff->ls together.
1399 static __isl_give isl_aff
*swap_div(__isl_take isl_aff
*aff
, int a
, int b
)
1401 unsigned off
= isl_local_space_offset(aff
->ls
, isl_dim_div
);
1402 isl_local_space
*ls
;
1405 ls
= isl_local_space_copy(aff
->ls
);
1406 ls
= isl_local_space_swap_div(ls
, a
, b
);
1407 v
= isl_vec_copy(aff
->v
);
1412 isl_int_swap(v
->el
[1 + off
+ a
], v
->el
[1 + off
+ b
]);
1413 isl_vec_free(aff
->v
);
1415 isl_local_space_free(aff
->ls
);
1421 isl_local_space_free(ls
);
1422 return isl_aff_free(aff
);
1425 /* Merge divs "a" and "b" in "aff", which is assumed to be non-NULL.
1427 * We currently do not actually remove div "b", but simply add its
1428 * coefficient to that of "a" and then zero it out.
1430 static __isl_give isl_aff
*merge_divs(__isl_take isl_aff
*aff
, int a
, int b
)
1432 unsigned off
= isl_local_space_offset(aff
->ls
, isl_dim_div
);
1434 if (isl_int_is_zero(aff
->v
->el
[1 + off
+ b
]))
1437 aff
->v
= isl_vec_cow(aff
->v
);
1439 return isl_aff_free(aff
);
1441 isl_int_add(aff
->v
->el
[1 + off
+ a
],
1442 aff
->v
->el
[1 + off
+ a
], aff
->v
->el
[1 + off
+ b
]);
1443 isl_int_set_si(aff
->v
->el
[1 + off
+ b
], 0);
1448 /* Sort the divs in the local space of "aff" according to
1449 * the comparison function "cmp_row" in isl_local_space.c,
1450 * combining the coefficients of identical divs.
1452 * Reordering divs does not change the semantics of "aff",
1453 * so there is no need to call isl_aff_cow.
1454 * Moreover, this function is currently only called on isl_affs
1455 * with a single reference.
1457 static __isl_give isl_aff
*sort_divs(__isl_take isl_aff
*aff
)
1462 n
= isl_aff_dim(aff
, isl_dim_div
);
1464 return isl_aff_free(aff
);
1465 for (i
= 1; i
< n
; ++i
) {
1466 for (j
= i
- 1; j
>= 0; --j
) {
1467 int cmp
= isl_mat_cmp_div(aff
->ls
->div
, j
, j
+ 1);
1471 aff
= merge_divs(aff
, j
, j
+ 1);
1473 aff
= swap_div(aff
, j
, j
+ 1);
1482 /* Normalize the representation of "aff".
1484 * This function should only be called of "new" isl_affs, i.e.,
1485 * with only a single reference. We therefore do not need to
1486 * worry about affecting other instances.
1488 __isl_give isl_aff
*isl_aff_normalize(__isl_take isl_aff
*aff
)
1492 aff
->v
= isl_vec_normalize(aff
->v
);
1494 return isl_aff_free(aff
);
1495 aff
= plug_in_integral_divs(aff
);
1496 aff
= plug_in_unit_divs(aff
);
1497 aff
= sort_divs(aff
);
1498 aff
= isl_aff_remove_unused_divs(aff
);
1502 /* Given f, return floor(f).
1503 * If f is an integer expression, then just return f.
1504 * If f is a constant, then return the constant floor(f).
1505 * Otherwise, if f = g/m, write g = q m + r,
1506 * create a new div d = [r/m] and return the expression q + d.
1507 * The coefficients in r are taken to lie between -m/2 and m/2.
1509 * reduce_div_coefficients performs the same normalization.
1511 * As a special case, floor(NaN) = NaN.
1513 __isl_give isl_aff
*isl_aff_floor(__isl_take isl_aff
*aff
)
1523 if (isl_aff_is_nan(aff
))
1525 if (isl_int_is_one(aff
->v
->el
[0]))
1528 aff
= isl_aff_cow(aff
);
1532 aff
->v
= isl_vec_cow(aff
->v
);
1534 return isl_aff_free(aff
);
1536 if (isl_aff_is_cst(aff
)) {
1537 isl_int_fdiv_q(aff
->v
->el
[1], aff
->v
->el
[1], aff
->v
->el
[0]);
1538 isl_int_set_si(aff
->v
->el
[0], 1);
1542 div
= isl_vec_copy(aff
->v
);
1543 div
= isl_vec_cow(div
);
1545 return isl_aff_free(aff
);
1547 ctx
= isl_aff_get_ctx(aff
);
1548 isl_int_fdiv_q(aff
->v
->el
[0], aff
->v
->el
[0], ctx
->two
);
1549 for (i
= 1; i
< aff
->v
->size
; ++i
) {
1550 isl_int_fdiv_r(div
->el
[i
], div
->el
[i
], div
->el
[0]);
1551 isl_int_fdiv_q(aff
->v
->el
[i
], aff
->v
->el
[i
], div
->el
[0]);
1552 if (isl_int_gt(div
->el
[i
], aff
->v
->el
[0])) {
1553 isl_int_sub(div
->el
[i
], div
->el
[i
], div
->el
[0]);
1554 isl_int_add_ui(aff
->v
->el
[i
], aff
->v
->el
[i
], 1);
1558 aff
->ls
= isl_local_space_add_div(aff
->ls
, div
);
1560 return isl_aff_free(aff
);
1562 size
= aff
->v
->size
;
1563 aff
->v
= isl_vec_extend(aff
->v
, size
+ 1);
1565 return isl_aff_free(aff
);
1566 isl_int_set_si(aff
->v
->el
[0], 1);
1567 isl_int_set_si(aff
->v
->el
[size
], 1);
1569 aff
= isl_aff_normalize(aff
);
1576 * aff mod m = aff - m * floor(aff/m)
1578 * with m an integer value.
1580 __isl_give isl_aff
*isl_aff_mod_val(__isl_take isl_aff
*aff
,
1581 __isl_take isl_val
*m
)
1588 if (!isl_val_is_int(m
))
1589 isl_die(isl_val_get_ctx(m
), isl_error_invalid
,
1590 "expecting integer modulo", goto error
);
1592 res
= isl_aff_copy(aff
);
1593 aff
= isl_aff_scale_down_val(aff
, isl_val_copy(m
));
1594 aff
= isl_aff_floor(aff
);
1595 aff
= isl_aff_scale_val(aff
, m
);
1596 res
= isl_aff_sub(res
, aff
);
1607 * pwaff mod m = pwaff - m * floor(pwaff/m)
1609 __isl_give isl_pw_aff
*isl_pw_aff_mod(__isl_take isl_pw_aff
*pwaff
, isl_int m
)
1613 res
= isl_pw_aff_copy(pwaff
);
1614 pwaff
= isl_pw_aff_scale_down(pwaff
, m
);
1615 pwaff
= isl_pw_aff_floor(pwaff
);
1616 pwaff
= isl_pw_aff_scale(pwaff
, m
);
1617 res
= isl_pw_aff_sub(res
, pwaff
);
1624 * pa mod m = pa - m * floor(pa/m)
1626 * with m an integer value.
1628 __isl_give isl_pw_aff
*isl_pw_aff_mod_val(__isl_take isl_pw_aff
*pa
,
1629 __isl_take isl_val
*m
)
1633 if (!isl_val_is_int(m
))
1634 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
1635 "expecting integer modulo", goto error
);
1636 pa
= isl_pw_aff_mod(pa
, m
->n
);
1640 isl_pw_aff_free(pa
);
1645 /* Given f, return ceil(f).
1646 * If f is an integer expression, then just return f.
1647 * Otherwise, let f be the expression
1653 * floor((e + m - 1)/m)
1655 * As a special case, ceil(NaN) = NaN.
1657 __isl_give isl_aff
*isl_aff_ceil(__isl_take isl_aff
*aff
)
1662 if (isl_aff_is_nan(aff
))
1664 if (isl_int_is_one(aff
->v
->el
[0]))
1667 aff
= isl_aff_cow(aff
);
1670 aff
->v
= isl_vec_cow(aff
->v
);
1672 return isl_aff_free(aff
);
1674 isl_int_add(aff
->v
->el
[1], aff
->v
->el
[1], aff
->v
->el
[0]);
1675 isl_int_sub_ui(aff
->v
->el
[1], aff
->v
->el
[1], 1);
1676 aff
= isl_aff_floor(aff
);
1681 /* Apply the expansion computed by isl_merge_divs.
1682 * The expansion itself is given by "exp" while the resulting
1683 * list of divs is given by "div".
1685 __isl_give isl_aff
*isl_aff_expand_divs(__isl_take isl_aff
*aff
,
1686 __isl_take isl_mat
*div
, int *exp
)
1692 aff
= isl_aff_cow(aff
);
1696 old_n_div
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
1697 new_n_div
= isl_mat_rows(div
);
1698 if (old_n_div
< 0 || new_n_div
< 0)
1700 offset
= 1 + isl_local_space_offset(aff
->ls
, isl_dim_div
);
1702 aff
->v
= isl_vec_expand(aff
->v
, offset
, old_n_div
, exp
, new_n_div
);
1703 aff
->ls
= isl_local_space_replace_divs(aff
->ls
, div
);
1704 if (!aff
->v
|| !aff
->ls
)
1705 return isl_aff_free(aff
);
1713 /* Add two affine expressions that live in the same local space.
1715 static __isl_give isl_aff
*add_expanded(__isl_take isl_aff
*aff1
,
1716 __isl_take isl_aff
*aff2
)
1720 aff1
= isl_aff_cow(aff1
);
1724 aff1
->v
= isl_vec_cow(aff1
->v
);
1730 isl_int_gcd(gcd
, aff1
->v
->el
[0], aff2
->v
->el
[0]);
1731 isl_int_divexact(f
, aff2
->v
->el
[0], gcd
);
1732 isl_seq_scale(aff1
->v
->el
+ 1, aff1
->v
->el
+ 1, f
, aff1
->v
->size
- 1);
1733 isl_int_divexact(f
, aff1
->v
->el
[0], gcd
);
1734 isl_seq_addmul(aff1
->v
->el
+ 1, f
, aff2
->v
->el
+ 1, aff1
->v
->size
- 1);
1735 isl_int_divexact(f
, aff2
->v
->el
[0], gcd
);
1736 isl_int_mul(aff1
->v
->el
[0], aff1
->v
->el
[0], f
);
1748 /* Return the sum of "aff1" and "aff2".
1750 * If either of the two is NaN, then the result is NaN.
1752 __isl_give isl_aff
*isl_aff_add(__isl_take isl_aff
*aff1
,
1753 __isl_take isl_aff
*aff2
)
1759 isl_size n_div1
, n_div2
;
1764 ctx
= isl_aff_get_ctx(aff1
);
1765 if (!isl_space_is_equal(aff1
->ls
->dim
, aff2
->ls
->dim
))
1766 isl_die(ctx
, isl_error_invalid
,
1767 "spaces don't match", goto error
);
1769 if (isl_aff_is_nan(aff1
)) {
1773 if (isl_aff_is_nan(aff2
)) {
1778 n_div1
= isl_aff_dim(aff1
, isl_dim_div
);
1779 n_div2
= isl_aff_dim(aff2
, isl_dim_div
);
1780 if (n_div1
< 0 || n_div2
< 0)
1782 if (n_div1
== 0 && n_div2
== 0)
1783 return add_expanded(aff1
, aff2
);
1785 exp1
= isl_alloc_array(ctx
, int, n_div1
);
1786 exp2
= isl_alloc_array(ctx
, int, n_div2
);
1787 if ((n_div1
&& !exp1
) || (n_div2
&& !exp2
))
1790 div
= isl_merge_divs(aff1
->ls
->div
, aff2
->ls
->div
, exp1
, exp2
);
1791 aff1
= isl_aff_expand_divs(aff1
, isl_mat_copy(div
), exp1
);
1792 aff2
= isl_aff_expand_divs(aff2
, div
, exp2
);
1796 return add_expanded(aff1
, aff2
);
1805 __isl_give isl_aff
*isl_aff_sub(__isl_take isl_aff
*aff1
,
1806 __isl_take isl_aff
*aff2
)
1808 return isl_aff_add(aff1
, isl_aff_neg(aff2
));
1811 /* Return the result of scaling "aff" by a factor of "f".
1813 * As a special case, f * NaN = NaN.
1815 __isl_give isl_aff
*isl_aff_scale(__isl_take isl_aff
*aff
, isl_int f
)
1821 if (isl_aff_is_nan(aff
))
1824 if (isl_int_is_one(f
))
1827 aff
= isl_aff_cow(aff
);
1830 aff
->v
= isl_vec_cow(aff
->v
);
1832 return isl_aff_free(aff
);
1834 if (isl_int_is_pos(f
) && isl_int_is_divisible_by(aff
->v
->el
[0], f
)) {
1835 isl_int_divexact(aff
->v
->el
[0], aff
->v
->el
[0], f
);
1840 isl_int_gcd(gcd
, aff
->v
->el
[0], f
);
1841 isl_int_divexact(aff
->v
->el
[0], aff
->v
->el
[0], gcd
);
1842 isl_int_divexact(gcd
, f
, gcd
);
1843 isl_seq_scale(aff
->v
->el
+ 1, aff
->v
->el
+ 1, gcd
, aff
->v
->size
- 1);
1849 /* Multiple "aff" by "v".
1851 __isl_give isl_aff
*isl_aff_scale_val(__isl_take isl_aff
*aff
,
1852 __isl_take isl_val
*v
)
1857 if (isl_val_is_one(v
)) {
1862 if (!isl_val_is_rat(v
))
1863 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1864 "expecting rational factor", goto error
);
1866 aff
= isl_aff_scale(aff
, v
->n
);
1867 aff
= isl_aff_scale_down(aff
, v
->d
);
1877 /* Return the result of scaling "aff" down by a factor of "f".
1879 * As a special case, NaN/f = NaN.
1881 __isl_give isl_aff
*isl_aff_scale_down(__isl_take isl_aff
*aff
, isl_int f
)
1887 if (isl_aff_is_nan(aff
))
1890 if (isl_int_is_one(f
))
1893 aff
= isl_aff_cow(aff
);
1897 if (isl_int_is_zero(f
))
1898 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1899 "cannot scale down by zero", return isl_aff_free(aff
));
1901 aff
->v
= isl_vec_cow(aff
->v
);
1903 return isl_aff_free(aff
);
1906 isl_seq_gcd(aff
->v
->el
+ 1, aff
->v
->size
- 1, &gcd
);
1907 isl_int_gcd(gcd
, gcd
, f
);
1908 isl_seq_scale_down(aff
->v
->el
+ 1, aff
->v
->el
+ 1, gcd
, aff
->v
->size
- 1);
1909 isl_int_divexact(gcd
, f
, gcd
);
1910 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], gcd
);
1916 /* Divide "aff" by "v".
1918 __isl_give isl_aff
*isl_aff_scale_down_val(__isl_take isl_aff
*aff
,
1919 __isl_take isl_val
*v
)
1924 if (isl_val_is_one(v
)) {
1929 if (!isl_val_is_rat(v
))
1930 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1931 "expecting rational factor", goto error
);
1932 if (!isl_val_is_pos(v
))
1933 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1934 "factor needs to be positive", goto error
);
1936 aff
= isl_aff_scale(aff
, v
->d
);
1937 aff
= isl_aff_scale_down(aff
, v
->n
);
1947 __isl_give isl_aff
*isl_aff_scale_down_ui(__isl_take isl_aff
*aff
, unsigned f
)
1955 isl_int_set_ui(v
, f
);
1956 aff
= isl_aff_scale_down(aff
, v
);
1962 __isl_give isl_aff
*isl_aff_set_dim_name(__isl_take isl_aff
*aff
,
1963 enum isl_dim_type type
, unsigned pos
, const char *s
)
1965 aff
= isl_aff_cow(aff
);
1968 if (type
== isl_dim_out
)
1969 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1970 "cannot set name of output/set dimension",
1971 return isl_aff_free(aff
));
1972 if (type
== isl_dim_in
)
1974 aff
->ls
= isl_local_space_set_dim_name(aff
->ls
, type
, pos
, s
);
1976 return isl_aff_free(aff
);
1981 __isl_give isl_aff
*isl_aff_set_dim_id(__isl_take isl_aff
*aff
,
1982 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
1984 aff
= isl_aff_cow(aff
);
1987 if (type
== isl_dim_out
)
1988 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1989 "cannot set name of output/set dimension",
1991 if (type
== isl_dim_in
)
1993 aff
->ls
= isl_local_space_set_dim_id(aff
->ls
, type
, pos
, id
);
1995 return isl_aff_free(aff
);
2004 /* Replace the identifier of the input tuple of "aff" by "id".
2005 * type is currently required to be equal to isl_dim_in
2007 __isl_give isl_aff
*isl_aff_set_tuple_id(__isl_take isl_aff
*aff
,
2008 enum isl_dim_type type
, __isl_take isl_id
*id
)
2010 aff
= isl_aff_cow(aff
);
2013 if (type
!= isl_dim_in
)
2014 isl_die(aff
->v
->ctx
, isl_error_invalid
,
2015 "cannot only set id of input tuple", goto error
);
2016 aff
->ls
= isl_local_space_set_tuple_id(aff
->ls
, isl_dim_set
, id
);
2018 return isl_aff_free(aff
);
2027 /* Exploit the equalities in "eq" to simplify the affine expression
2028 * and the expressions of the integer divisions in the local space.
2029 * The integer divisions in this local space are assumed to appear
2030 * as regular dimensions in "eq".
2032 static __isl_give isl_aff
*isl_aff_substitute_equalities_lifted(
2033 __isl_take isl_aff
*aff
, __isl_take isl_basic_set
*eq
)
2041 if (eq
->n_eq
== 0) {
2042 isl_basic_set_free(eq
);
2046 aff
= isl_aff_cow(aff
);
2050 aff
->ls
= isl_local_space_substitute_equalities(aff
->ls
,
2051 isl_basic_set_copy(eq
));
2052 aff
->v
= isl_vec_cow(aff
->v
);
2053 if (!aff
->ls
|| !aff
->v
)
2056 o_div
= isl_basic_set_offset(eq
, isl_dim_div
);
2058 for (i
= 0; i
< eq
->n_eq
; ++i
) {
2059 j
= isl_seq_last_non_zero(eq
->eq
[i
], o_div
+ n_div
);
2060 if (j
< 0 || j
== 0 || j
>= o_div
)
2063 isl_seq_elim(aff
->v
->el
+ 1, eq
->eq
[i
], j
, o_div
,
2067 isl_basic_set_free(eq
);
2068 aff
= isl_aff_normalize(aff
);
2071 isl_basic_set_free(eq
);
2076 /* Exploit the equalities in "eq" to simplify the affine expression
2077 * and the expressions of the integer divisions in the local space.
2079 __isl_give isl_aff
*isl_aff_substitute_equalities(__isl_take isl_aff
*aff
,
2080 __isl_take isl_basic_set
*eq
)
2086 n_div
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
2090 eq
= isl_basic_set_add_dims(eq
, isl_dim_set
, n_div
);
2091 return isl_aff_substitute_equalities_lifted(aff
, eq
);
2093 isl_basic_set_free(eq
);
2098 /* Look for equalities among the variables shared by context and aff
2099 * and the integer divisions of aff, if any.
2100 * The equalities are then used to eliminate coefficients and/or integer
2101 * divisions from aff.
2103 __isl_give isl_aff
*isl_aff_gist(__isl_take isl_aff
*aff
,
2104 __isl_take isl_set
*context
)
2106 isl_local_space
*ls
;
2107 isl_basic_set
*hull
;
2109 ls
= isl_aff_get_domain_local_space(aff
);
2110 context
= isl_local_space_lift_set(ls
, context
);
2112 hull
= isl_set_affine_hull(context
);
2113 return isl_aff_substitute_equalities_lifted(aff
, hull
);
2116 __isl_give isl_aff
*isl_aff_gist_params(__isl_take isl_aff
*aff
,
2117 __isl_take isl_set
*context
)
2119 isl_set
*dom_context
= isl_set_universe(isl_aff_get_domain_space(aff
));
2120 dom_context
= isl_set_intersect_params(dom_context
, context
);
2121 return isl_aff_gist(aff
, dom_context
);
2124 /* Return a basic set containing those elements in the space
2125 * of aff where it is positive. "rational" should not be set.
2127 * If "aff" is NaN, then it is not positive.
2129 static __isl_give isl_basic_set
*aff_pos_basic_set(__isl_take isl_aff
*aff
,
2132 isl_constraint
*ineq
;
2133 isl_basic_set
*bset
;
2138 if (isl_aff_is_nan(aff
)) {
2139 isl_space
*space
= isl_aff_get_domain_space(aff
);
2141 return isl_basic_set_empty(space
);
2144 isl_die(isl_aff_get_ctx(aff
), isl_error_unsupported
,
2145 "rational sets not supported", goto error
);
2147 ineq
= isl_inequality_from_aff(aff
);
2148 c
= isl_constraint_get_constant_val(ineq
);
2149 c
= isl_val_sub_ui(c
, 1);
2150 ineq
= isl_constraint_set_constant_val(ineq
, c
);
2152 bset
= isl_basic_set_from_constraint(ineq
);
2153 bset
= isl_basic_set_simplify(bset
);
2160 /* Return a basic set containing those elements in the space
2161 * of aff where it is non-negative.
2162 * If "rational" is set, then return a rational basic set.
2164 * If "aff" is NaN, then it is not non-negative (it's not negative either).
2166 static __isl_give isl_basic_set
*aff_nonneg_basic_set(
2167 __isl_take isl_aff
*aff
, int rational
)
2169 isl_constraint
*ineq
;
2170 isl_basic_set
*bset
;
2174 if (isl_aff_is_nan(aff
)) {
2175 isl_space
*space
= isl_aff_get_domain_space(aff
);
2177 return isl_basic_set_empty(space
);
2180 ineq
= isl_inequality_from_aff(aff
);
2182 bset
= isl_basic_set_from_constraint(ineq
);
2184 bset
= isl_basic_set_set_rational(bset
);
2185 bset
= isl_basic_set_simplify(bset
);
2189 /* Return a basic set containing those elements in the space
2190 * of aff where it is non-negative.
2192 __isl_give isl_basic_set
*isl_aff_nonneg_basic_set(__isl_take isl_aff
*aff
)
2194 return aff_nonneg_basic_set(aff
, 0);
2197 /* Return a basic set containing those elements in the domain space
2198 * of "aff" where it is positive.
2200 __isl_give isl_basic_set
*isl_aff_pos_basic_set(__isl_take isl_aff
*aff
)
2202 aff
= isl_aff_add_constant_num_si(aff
, -1);
2203 return isl_aff_nonneg_basic_set(aff
);
2206 /* Return a basic set containing those elements in the domain space
2207 * of aff where it is negative.
2209 __isl_give isl_basic_set
*isl_aff_neg_basic_set(__isl_take isl_aff
*aff
)
2211 aff
= isl_aff_neg(aff
);
2212 return isl_aff_pos_basic_set(aff
);
2215 /* Return a basic set containing those elements in the space
2216 * of aff where it is zero.
2217 * If "rational" is set, then return a rational basic set.
2219 * If "aff" is NaN, then it is not zero.
2221 static __isl_give isl_basic_set
*aff_zero_basic_set(__isl_take isl_aff
*aff
,
2224 isl_constraint
*ineq
;
2225 isl_basic_set
*bset
;
2229 if (isl_aff_is_nan(aff
)) {
2230 isl_space
*space
= isl_aff_get_domain_space(aff
);
2232 return isl_basic_set_empty(space
);
2235 ineq
= isl_equality_from_aff(aff
);
2237 bset
= isl_basic_set_from_constraint(ineq
);
2239 bset
= isl_basic_set_set_rational(bset
);
2240 bset
= isl_basic_set_simplify(bset
);
2244 /* Return a basic set containing those elements in the space
2245 * of aff where it is zero.
2247 __isl_give isl_basic_set
*isl_aff_zero_basic_set(__isl_take isl_aff
*aff
)
2249 return aff_zero_basic_set(aff
, 0);
2252 /* Return a basic set containing those elements in the shared space
2253 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
2255 __isl_give isl_basic_set
*isl_aff_ge_basic_set(__isl_take isl_aff
*aff1
,
2256 __isl_take isl_aff
*aff2
)
2258 aff1
= isl_aff_sub(aff1
, aff2
);
2260 return isl_aff_nonneg_basic_set(aff1
);
2263 /* Return a basic set containing those elements in the shared domain space
2264 * of "aff1" and "aff2" where "aff1" is greater than "aff2".
2266 __isl_give isl_basic_set
*isl_aff_gt_basic_set(__isl_take isl_aff
*aff1
,
2267 __isl_take isl_aff
*aff2
)
2269 aff1
= isl_aff_sub(aff1
, aff2
);
2271 return isl_aff_pos_basic_set(aff1
);
2274 /* Return a set containing those elements in the shared space
2275 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
2277 __isl_give isl_set
*isl_aff_ge_set(__isl_take isl_aff
*aff1
,
2278 __isl_take isl_aff
*aff2
)
2280 return isl_set_from_basic_set(isl_aff_ge_basic_set(aff1
, aff2
));
2283 /* Return a set containing those elements in the shared domain space
2284 * of aff1 and aff2 where aff1 is greater than aff2.
2286 * If either of the two inputs is NaN, then the result is empty,
2287 * as comparisons with NaN always return false.
2289 __isl_give isl_set
*isl_aff_gt_set(__isl_take isl_aff
*aff1
,
2290 __isl_take isl_aff
*aff2
)
2292 return isl_set_from_basic_set(isl_aff_gt_basic_set(aff1
, aff2
));
2295 /* Return a basic set containing those elements in the shared space
2296 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
2298 __isl_give isl_basic_set
*isl_aff_le_basic_set(__isl_take isl_aff
*aff1
,
2299 __isl_take isl_aff
*aff2
)
2301 return isl_aff_ge_basic_set(aff2
, aff1
);
2304 /* Return a basic set containing those elements in the shared domain space
2305 * of "aff1" and "aff2" where "aff1" is smaller than "aff2".
2307 __isl_give isl_basic_set
*isl_aff_lt_basic_set(__isl_take isl_aff
*aff1
,
2308 __isl_take isl_aff
*aff2
)
2310 return isl_aff_gt_basic_set(aff2
, aff1
);
2313 /* Return a set containing those elements in the shared space
2314 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
2316 __isl_give isl_set
*isl_aff_le_set(__isl_take isl_aff
*aff1
,
2317 __isl_take isl_aff
*aff2
)
2319 return isl_aff_ge_set(aff2
, aff1
);
2322 /* Return a set containing those elements in the shared domain space
2323 * of "aff1" and "aff2" where "aff1" is smaller than "aff2".
2325 __isl_give isl_set
*isl_aff_lt_set(__isl_take isl_aff
*aff1
,
2326 __isl_take isl_aff
*aff2
)
2328 return isl_set_from_basic_set(isl_aff_lt_basic_set(aff1
, aff2
));
2331 /* Return a basic set containing those elements in the shared space
2332 * of aff1 and aff2 where aff1 and aff2 are equal.
2334 __isl_give isl_basic_set
*isl_aff_eq_basic_set(__isl_take isl_aff
*aff1
,
2335 __isl_take isl_aff
*aff2
)
2337 aff1
= isl_aff_sub(aff1
, aff2
);
2339 return isl_aff_zero_basic_set(aff1
);
2342 /* Return a set containing those elements in the shared space
2343 * of aff1 and aff2 where aff1 and aff2 are equal.
2345 __isl_give isl_set
*isl_aff_eq_set(__isl_take isl_aff
*aff1
,
2346 __isl_take isl_aff
*aff2
)
2348 return isl_set_from_basic_set(isl_aff_eq_basic_set(aff1
, aff2
));
2351 /* Return a set containing those elements in the shared domain space
2352 * of aff1 and aff2 where aff1 and aff2 are not equal.
2354 * If either of the two inputs is NaN, then the result is empty,
2355 * as comparisons with NaN always return false.
2357 __isl_give isl_set
*isl_aff_ne_set(__isl_take isl_aff
*aff1
,
2358 __isl_take isl_aff
*aff2
)
2360 isl_set
*set_lt
, *set_gt
;
2362 set_lt
= isl_aff_lt_set(isl_aff_copy(aff1
),
2363 isl_aff_copy(aff2
));
2364 set_gt
= isl_aff_gt_set(aff1
, aff2
);
2365 return isl_set_union_disjoint(set_lt
, set_gt
);
2368 __isl_give isl_aff
*isl_aff_add_on_domain(__isl_keep isl_set
*dom
,
2369 __isl_take isl_aff
*aff1
, __isl_take isl_aff
*aff2
)
2371 aff1
= isl_aff_add(aff1
, aff2
);
2372 aff1
= isl_aff_gist(aff1
, isl_set_copy(dom
));
2376 int isl_aff_is_empty(__isl_keep isl_aff
*aff
)
2385 #define TYPE isl_aff
2387 #include "check_type_range_templ.c"
2389 /* Check whether the given affine expression has non-zero coefficient
2390 * for any dimension in the given range or if any of these dimensions
2391 * appear with non-zero coefficients in any of the integer divisions
2392 * involved in the affine expression.
2394 isl_bool
isl_aff_involves_dims(__isl_keep isl_aff
*aff
,
2395 enum isl_dim_type type
, unsigned first
, unsigned n
)
2399 isl_bool involves
= isl_bool_false
;
2402 return isl_bool_error
;
2404 return isl_bool_false
;
2405 if (isl_aff_check_range(aff
, type
, first
, n
) < 0)
2406 return isl_bool_error
;
2408 active
= isl_local_space_get_active(aff
->ls
, aff
->v
->el
+ 2);
2412 first
+= isl_local_space_offset(aff
->ls
, type
) - 1;
2413 for (i
= 0; i
< n
; ++i
)
2414 if (active
[first
+ i
]) {
2415 involves
= isl_bool_true
;
2424 return isl_bool_error
;
2427 __isl_give isl_aff
*isl_aff_drop_dims(__isl_take isl_aff
*aff
,
2428 enum isl_dim_type type
, unsigned first
, unsigned n
)
2434 if (type
== isl_dim_out
)
2435 isl_die(aff
->v
->ctx
, isl_error_invalid
,
2436 "cannot drop output/set dimension",
2437 return isl_aff_free(aff
));
2438 if (type
== isl_dim_in
)
2440 if (n
== 0 && !isl_local_space_is_named_or_nested(aff
->ls
, type
))
2443 ctx
= isl_aff_get_ctx(aff
);
2444 if (isl_local_space_check_range(aff
->ls
, type
, first
, n
) < 0)
2445 return isl_aff_free(aff
);
2447 aff
= isl_aff_cow(aff
);
2451 aff
->ls
= isl_local_space_drop_dims(aff
->ls
, type
, first
, n
);
2453 return isl_aff_free(aff
);
2455 first
+= 1 + isl_local_space_offset(aff
->ls
, type
);
2456 aff
->v
= isl_vec_drop_els(aff
->v
, first
, n
);
2458 return isl_aff_free(aff
);
2463 /* Drop the "n" domain dimensions starting at "first" from "aff",
2464 * after checking that they do not appear in the affine expression.
2466 static __isl_give isl_aff
*drop_domain(__isl_take isl_aff
*aff
, unsigned first
,
2471 involves
= isl_aff_involves_dims(aff
, isl_dim_in
, first
, n
);
2473 return isl_aff_free(aff
);
2475 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2476 "affine expression involves some of the domain dimensions",
2477 return isl_aff_free(aff
));
2478 return isl_aff_drop_dims(aff
, isl_dim_in
, first
, n
);
2481 /* Project the domain of the affine expression onto its parameter space.
2482 * The affine expression may not involve any of the domain dimensions.
2484 __isl_give isl_aff
*isl_aff_project_domain_on_params(__isl_take isl_aff
*aff
)
2489 n
= isl_aff_dim(aff
, isl_dim_in
);
2491 return isl_aff_free(aff
);
2492 aff
= drop_domain(aff
, 0, n
);
2493 space
= isl_aff_get_domain_space(aff
);
2494 space
= isl_space_params(space
);
2495 aff
= isl_aff_reset_domain_space(aff
, space
);
2499 /* Check that the domain of "aff" is a product.
2501 static isl_stat
check_domain_product(__isl_keep isl_aff
*aff
)
2503 isl_bool is_product
;
2505 is_product
= isl_space_is_product(isl_aff_peek_domain_space(aff
));
2507 return isl_stat_error
;
2509 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2510 "domain is not a product", return isl_stat_error
);
2514 /* Given an affine function with a domain of the form [A -> B] that
2515 * does not depend on B, return the same function on domain A.
2517 __isl_give isl_aff
*isl_aff_domain_factor_domain(__isl_take isl_aff
*aff
)
2522 if (check_domain_product(aff
) < 0)
2523 return isl_aff_free(aff
);
2524 space
= isl_aff_get_domain_space(aff
);
2525 n
= isl_space_dim(space
, isl_dim_set
);
2526 space
= isl_space_factor_domain(space
);
2527 n_in
= isl_space_dim(space
, isl_dim_set
);
2528 if (n
< 0 || n_in
< 0)
2529 aff
= isl_aff_free(aff
);
2531 aff
= drop_domain(aff
, n_in
, n
- n_in
);
2532 aff
= isl_aff_reset_domain_space(aff
, space
);
2536 /* Convert an affine expression defined over a parameter domain
2537 * into one that is defined over a zero-dimensional set.
2539 __isl_give isl_aff
*isl_aff_from_range(__isl_take isl_aff
*aff
)
2541 isl_local_space
*ls
;
2543 ls
= isl_aff_take_domain_local_space(aff
);
2544 ls
= isl_local_space_set_from_params(ls
);
2545 aff
= isl_aff_restore_domain_local_space(aff
, ls
);
2550 __isl_give isl_aff
*isl_aff_insert_dims(__isl_take isl_aff
*aff
,
2551 enum isl_dim_type type
, unsigned first
, unsigned n
)
2557 if (type
== isl_dim_out
)
2558 isl_die(aff
->v
->ctx
, isl_error_invalid
,
2559 "cannot insert output/set dimensions",
2560 return isl_aff_free(aff
));
2561 if (type
== isl_dim_in
)
2563 if (n
== 0 && !isl_local_space_is_named_or_nested(aff
->ls
, type
))
2566 ctx
= isl_aff_get_ctx(aff
);
2567 if (isl_local_space_check_range(aff
->ls
, type
, first
, 0) < 0)
2568 return isl_aff_free(aff
);
2570 aff
= isl_aff_cow(aff
);
2574 aff
->ls
= isl_local_space_insert_dims(aff
->ls
, type
, first
, n
);
2576 return isl_aff_free(aff
);
2578 first
+= 1 + isl_local_space_offset(aff
->ls
, type
);
2579 aff
->v
= isl_vec_insert_zero_els(aff
->v
, first
, n
);
2581 return isl_aff_free(aff
);
2586 __isl_give isl_aff
*isl_aff_add_dims(__isl_take isl_aff
*aff
,
2587 enum isl_dim_type type
, unsigned n
)
2591 pos
= isl_aff_dim(aff
, type
);
2593 return isl_aff_free(aff
);
2595 return isl_aff_insert_dims(aff
, type
, pos
, n
);
2598 __isl_give isl_pw_aff
*isl_pw_aff_add_dims(__isl_take isl_pw_aff
*pwaff
,
2599 enum isl_dim_type type
, unsigned n
)
2603 pos
= isl_pw_aff_dim(pwaff
, type
);
2605 return isl_pw_aff_free(pwaff
);
2607 return isl_pw_aff_insert_dims(pwaff
, type
, pos
, n
);
2610 /* Move the "n" dimensions of "src_type" starting at "src_pos" of "aff"
2611 * to dimensions of "dst_type" at "dst_pos".
2613 * We only support moving input dimensions to parameters and vice versa.
2615 __isl_give isl_aff
*isl_aff_move_dims(__isl_take isl_aff
*aff
,
2616 enum isl_dim_type dst_type
, unsigned dst_pos
,
2617 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
2625 !isl_local_space_is_named_or_nested(aff
->ls
, src_type
) &&
2626 !isl_local_space_is_named_or_nested(aff
->ls
, dst_type
))
2629 if (dst_type
== isl_dim_out
|| src_type
== isl_dim_out
)
2630 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2631 "cannot move output/set dimension",
2632 return isl_aff_free(aff
));
2633 if (dst_type
== isl_dim_div
|| src_type
== isl_dim_div
)
2634 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2635 "cannot move divs", return isl_aff_free(aff
));
2636 if (dst_type
== isl_dim_in
)
2637 dst_type
= isl_dim_set
;
2638 if (src_type
== isl_dim_in
)
2639 src_type
= isl_dim_set
;
2641 if (isl_local_space_check_range(aff
->ls
, src_type
, src_pos
, n
) < 0)
2642 return isl_aff_free(aff
);
2643 if (dst_type
== src_type
)
2644 isl_die(isl_aff_get_ctx(aff
), isl_error_unsupported
,
2645 "moving dims within the same type not supported",
2646 return isl_aff_free(aff
));
2648 aff
= isl_aff_cow(aff
);
2652 g_src_pos
= 1 + isl_local_space_offset(aff
->ls
, src_type
) + src_pos
;
2653 g_dst_pos
= 1 + isl_local_space_offset(aff
->ls
, dst_type
) + dst_pos
;
2654 if (dst_type
> src_type
)
2657 aff
->v
= isl_vec_move_els(aff
->v
, g_dst_pos
, g_src_pos
, n
);
2658 aff
->ls
= isl_local_space_move_dims(aff
->ls
, dst_type
, dst_pos
,
2659 src_type
, src_pos
, n
);
2660 if (!aff
->v
|| !aff
->ls
)
2661 return isl_aff_free(aff
);
2663 aff
= sort_divs(aff
);
2668 __isl_give isl_pw_aff
*isl_pw_aff_from_aff(__isl_take isl_aff
*aff
)
2670 isl_set
*dom
= isl_set_universe(isl_aff_get_domain_space(aff
));
2671 return isl_pw_aff_alloc(dom
, aff
);
2674 #define isl_aff_involves_nan isl_aff_is_nan
2677 #define PW isl_pw_aff
2681 #define EL_IS_ZERO is_empty
2685 #define IS_ZERO is_empty
2688 #undef DEFAULT_IS_ZERO
2689 #define DEFAULT_IS_ZERO 0
2695 #include <isl_pw_templ.c>
2696 #include <isl_pw_eval.c>
2697 #include <isl_pw_hash.c>
2698 #include <isl_pw_union_opt.c>
2703 #include <isl_union_single.c>
2704 #include <isl_union_neg.c>
2706 static __isl_give isl_set
*align_params_pw_pw_set_and(
2707 __isl_take isl_pw_aff
*pwaff1
, __isl_take isl_pw_aff
*pwaff2
,
2708 __isl_give isl_set
*(*fn
)(__isl_take isl_pw_aff
*pwaff1
,
2709 __isl_take isl_pw_aff
*pwaff2
))
2711 isl_bool equal_params
;
2713 if (!pwaff1
|| !pwaff2
)
2715 equal_params
= isl_space_has_equal_params(pwaff1
->dim
, pwaff2
->dim
);
2716 if (equal_params
< 0)
2719 return fn(pwaff1
, pwaff2
);
2720 if (isl_pw_aff_check_named_params(pwaff1
) < 0 ||
2721 isl_pw_aff_check_named_params(pwaff2
) < 0)
2723 pwaff1
= isl_pw_aff_align_params(pwaff1
, isl_pw_aff_get_space(pwaff2
));
2724 pwaff2
= isl_pw_aff_align_params(pwaff2
, isl_pw_aff_get_space(pwaff1
));
2725 return fn(pwaff1
, pwaff2
);
2727 isl_pw_aff_free(pwaff1
);
2728 isl_pw_aff_free(pwaff2
);
2732 /* Align the parameters of the to isl_pw_aff arguments and
2733 * then apply a function "fn" on them that returns an isl_map.
2735 static __isl_give isl_map
*align_params_pw_pw_map_and(
2736 __isl_take isl_pw_aff
*pa1
, __isl_take isl_pw_aff
*pa2
,
2737 __isl_give isl_map
*(*fn
)(__isl_take isl_pw_aff
*pa1
,
2738 __isl_take isl_pw_aff
*pa2
))
2740 isl_bool equal_params
;
2744 equal_params
= isl_space_has_equal_params(pa1
->dim
, pa2
->dim
);
2745 if (equal_params
< 0)
2748 return fn(pa1
, pa2
);
2749 if (isl_pw_aff_check_named_params(pa1
) < 0 ||
2750 isl_pw_aff_check_named_params(pa2
) < 0)
2752 pa1
= isl_pw_aff_align_params(pa1
, isl_pw_aff_get_space(pa2
));
2753 pa2
= isl_pw_aff_align_params(pa2
, isl_pw_aff_get_space(pa1
));
2754 return fn(pa1
, pa2
);
2756 isl_pw_aff_free(pa1
);
2757 isl_pw_aff_free(pa2
);
2761 /* Compute a piecewise quasi-affine expression with a domain that
2762 * is the union of those of pwaff1 and pwaff2 and such that on each
2763 * cell, the quasi-affine expression is the maximum of those of pwaff1
2764 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2765 * cell, then the associated expression is the defined one.
2767 static __isl_give isl_pw_aff
*pw_aff_union_max(__isl_take isl_pw_aff
*pwaff1
,
2768 __isl_take isl_pw_aff
*pwaff2
)
2770 return isl_pw_aff_union_opt_cmp(pwaff1
, pwaff2
, &isl_aff_ge_set
);
2773 __isl_give isl_pw_aff
*isl_pw_aff_union_max(__isl_take isl_pw_aff
*pwaff1
,
2774 __isl_take isl_pw_aff
*pwaff2
)
2776 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
,
2780 /* Compute a piecewise quasi-affine expression with a domain that
2781 * is the union of those of pwaff1 and pwaff2 and such that on each
2782 * cell, the quasi-affine expression is the minimum of those of pwaff1
2783 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2784 * cell, then the associated expression is the defined one.
2786 static __isl_give isl_pw_aff
*pw_aff_union_min(__isl_take isl_pw_aff
*pwaff1
,
2787 __isl_take isl_pw_aff
*pwaff2
)
2789 return isl_pw_aff_union_opt_cmp(pwaff1
, pwaff2
, &isl_aff_le_set
);
2792 __isl_give isl_pw_aff
*isl_pw_aff_union_min(__isl_take isl_pw_aff
*pwaff1
,
2793 __isl_take isl_pw_aff
*pwaff2
)
2795 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
,
2799 __isl_give isl_pw_aff
*isl_pw_aff_union_opt(__isl_take isl_pw_aff
*pwaff1
,
2800 __isl_take isl_pw_aff
*pwaff2
, int max
)
2803 return isl_pw_aff_union_max(pwaff1
, pwaff2
);
2805 return isl_pw_aff_union_min(pwaff1
, pwaff2
);
2808 /* Return a set containing those elements in the domain
2809 * of "pwaff" where it satisfies "fn" (if complement is 0) or
2810 * does not satisfy "fn" (if complement is 1).
2812 * The pieces with a NaN never belong to the result since
2813 * NaN does not satisfy any property.
2815 static __isl_give isl_set
*pw_aff_locus(__isl_take isl_pw_aff
*pwaff
,
2816 __isl_give isl_basic_set
*(*fn
)(__isl_take isl_aff
*aff
, int rational
),
2825 set
= isl_set_empty(isl_pw_aff_get_domain_space(pwaff
));
2827 for (i
= 0; i
< pwaff
->n
; ++i
) {
2828 isl_basic_set
*bset
;
2829 isl_set
*set_i
, *locus
;
2832 if (isl_aff_is_nan(pwaff
->p
[i
].aff
))
2835 rational
= isl_set_has_rational(pwaff
->p
[i
].set
);
2836 bset
= fn(isl_aff_copy(pwaff
->p
[i
].aff
), rational
);
2837 locus
= isl_set_from_basic_set(bset
);
2838 set_i
= isl_set_copy(pwaff
->p
[i
].set
);
2840 set_i
= isl_set_subtract(set_i
, locus
);
2842 set_i
= isl_set_intersect(set_i
, locus
);
2843 set
= isl_set_union_disjoint(set
, set_i
);
2846 isl_pw_aff_free(pwaff
);
2851 /* Return a set containing those elements in the domain
2852 * of "pa" where it is positive.
2854 __isl_give isl_set
*isl_pw_aff_pos_set(__isl_take isl_pw_aff
*pa
)
2856 return pw_aff_locus(pa
, &aff_pos_basic_set
, 0);
2859 /* Return a set containing those elements in the domain
2860 * of pwaff where it is non-negative.
2862 __isl_give isl_set
*isl_pw_aff_nonneg_set(__isl_take isl_pw_aff
*pwaff
)
2864 return pw_aff_locus(pwaff
, &aff_nonneg_basic_set
, 0);
2867 /* Return a set containing those elements in the domain
2868 * of pwaff where it is zero.
2870 __isl_give isl_set
*isl_pw_aff_zero_set(__isl_take isl_pw_aff
*pwaff
)
2872 return pw_aff_locus(pwaff
, &aff_zero_basic_set
, 0);
2875 /* Return a set containing those elements in the domain
2876 * of pwaff where it is not zero.
2878 __isl_give isl_set
*isl_pw_aff_non_zero_set(__isl_take isl_pw_aff
*pwaff
)
2880 return pw_aff_locus(pwaff
, &aff_zero_basic_set
, 1);
2883 /* Return a set containing those elements in the shared domain
2884 * of pwaff1 and pwaff2 where pwaff1 is greater than (or equal) to pwaff2.
2886 * We compute the difference on the shared domain and then construct
2887 * the set of values where this difference is non-negative.
2888 * If strict is set, we first subtract 1 from the difference.
2889 * If equal is set, we only return the elements where pwaff1 and pwaff2
2892 static __isl_give isl_set
*pw_aff_gte_set(__isl_take isl_pw_aff
*pwaff1
,
2893 __isl_take isl_pw_aff
*pwaff2
, int strict
, int equal
)
2895 isl_set
*set1
, *set2
;
2897 set1
= isl_pw_aff_domain(isl_pw_aff_copy(pwaff1
));
2898 set2
= isl_pw_aff_domain(isl_pw_aff_copy(pwaff2
));
2899 set1
= isl_set_intersect(set1
, set2
);
2900 pwaff1
= isl_pw_aff_intersect_domain(pwaff1
, isl_set_copy(set1
));
2901 pwaff2
= isl_pw_aff_intersect_domain(pwaff2
, isl_set_copy(set1
));
2902 pwaff1
= isl_pw_aff_add(pwaff1
, isl_pw_aff_neg(pwaff2
));
2905 isl_space
*space
= isl_set_get_space(set1
);
2907 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(space
));
2908 aff
= isl_aff_add_constant_si(aff
, -1);
2909 pwaff1
= isl_pw_aff_add(pwaff1
, isl_pw_aff_alloc(set1
, aff
));
2914 return isl_pw_aff_zero_set(pwaff1
);
2915 return isl_pw_aff_nonneg_set(pwaff1
);
2918 /* Return a set containing those elements in the shared domain
2919 * of pwaff1 and pwaff2 where pwaff1 is equal to pwaff2.
2921 static __isl_give isl_set
*pw_aff_eq_set(__isl_take isl_pw_aff
*pwaff1
,
2922 __isl_take isl_pw_aff
*pwaff2
)
2924 return pw_aff_gte_set(pwaff1
, pwaff2
, 0, 1);
2927 __isl_give isl_set
*isl_pw_aff_eq_set(__isl_take isl_pw_aff
*pwaff1
,
2928 __isl_take isl_pw_aff
*pwaff2
)
2930 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_eq_set
);
2933 /* Return a set containing those elements in the shared domain
2934 * of pwaff1 and pwaff2 where pwaff1 is greater than or equal to pwaff2.
2936 static __isl_give isl_set
*pw_aff_ge_set(__isl_take isl_pw_aff
*pwaff1
,
2937 __isl_take isl_pw_aff
*pwaff2
)
2939 return pw_aff_gte_set(pwaff1
, pwaff2
, 0, 0);
2942 __isl_give isl_set
*isl_pw_aff_ge_set(__isl_take isl_pw_aff
*pwaff1
,
2943 __isl_take isl_pw_aff
*pwaff2
)
2945 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_ge_set
);
2948 /* Return a set containing those elements in the shared domain
2949 * of pwaff1 and pwaff2 where pwaff1 is strictly greater than pwaff2.
2951 static __isl_give isl_set
*pw_aff_gt_set(__isl_take isl_pw_aff
*pwaff1
,
2952 __isl_take isl_pw_aff
*pwaff2
)
2954 return pw_aff_gte_set(pwaff1
, pwaff2
, 1, 0);
2957 __isl_give isl_set
*isl_pw_aff_gt_set(__isl_take isl_pw_aff
*pwaff1
,
2958 __isl_take isl_pw_aff
*pwaff2
)
2960 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_gt_set
);
2963 __isl_give isl_set
*isl_pw_aff_le_set(__isl_take isl_pw_aff
*pwaff1
,
2964 __isl_take isl_pw_aff
*pwaff2
)
2966 return isl_pw_aff_ge_set(pwaff2
, pwaff1
);
2969 __isl_give isl_set
*isl_pw_aff_lt_set(__isl_take isl_pw_aff
*pwaff1
,
2970 __isl_take isl_pw_aff
*pwaff2
)
2972 return isl_pw_aff_gt_set(pwaff2
, pwaff1
);
2975 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
2976 * where the function values are ordered in the same way as "order",
2977 * which returns a set in the shared domain of its two arguments.
2978 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
2980 * Let "pa1" and "pa2" be defined on domains A and B respectively.
2981 * We first pull back the two functions such that they are defined on
2982 * the domain [A -> B]. Then we apply "order", resulting in a set
2983 * in the space [A -> B]. Finally, we unwrap this set to obtain
2984 * a map in the space A -> B.
2986 static __isl_give isl_map
*isl_pw_aff_order_map_aligned(
2987 __isl_take isl_pw_aff
*pa1
, __isl_take isl_pw_aff
*pa2
,
2988 __isl_give isl_set
*(*order
)(__isl_take isl_pw_aff
*pa1
,
2989 __isl_take isl_pw_aff
*pa2
))
2991 isl_space
*space1
, *space2
;
2995 space1
= isl_space_domain(isl_pw_aff_get_space(pa1
));
2996 space2
= isl_space_domain(isl_pw_aff_get_space(pa2
));
2997 space1
= isl_space_map_from_domain_and_range(space1
, space2
);
2998 ma
= isl_multi_aff_domain_map(isl_space_copy(space1
));
2999 pa1
= isl_pw_aff_pullback_multi_aff(pa1
, ma
);
3000 ma
= isl_multi_aff_range_map(space1
);
3001 pa2
= isl_pw_aff_pullback_multi_aff(pa2
, ma
);
3002 set
= order(pa1
, pa2
);
3004 return isl_set_unwrap(set
);
3007 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3008 * where the function values are equal.
3009 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3011 static __isl_give isl_map
*isl_pw_aff_eq_map_aligned(__isl_take isl_pw_aff
*pa1
,
3012 __isl_take isl_pw_aff
*pa2
)
3014 return isl_pw_aff_order_map_aligned(pa1
, pa2
, &isl_pw_aff_eq_set
);
3017 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3018 * where the function values are equal.
3020 __isl_give isl_map
*isl_pw_aff_eq_map(__isl_take isl_pw_aff
*pa1
,
3021 __isl_take isl_pw_aff
*pa2
)
3023 return align_params_pw_pw_map_and(pa1
, pa2
, &isl_pw_aff_eq_map_aligned
);
3026 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3027 * where the function value of "pa1" is less than the function value of "pa2".
3028 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3030 static __isl_give isl_map
*isl_pw_aff_lt_map_aligned(__isl_take isl_pw_aff
*pa1
,
3031 __isl_take isl_pw_aff
*pa2
)
3033 return isl_pw_aff_order_map_aligned(pa1
, pa2
, &isl_pw_aff_lt_set
);
3036 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3037 * where the function value of "pa1" is less than the function value of "pa2".
3039 __isl_give isl_map
*isl_pw_aff_lt_map(__isl_take isl_pw_aff
*pa1
,
3040 __isl_take isl_pw_aff
*pa2
)
3042 return align_params_pw_pw_map_and(pa1
, pa2
, &isl_pw_aff_lt_map_aligned
);
3045 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3046 * where the function value of "pa1" is greater than the function value
3048 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3050 static __isl_give isl_map
*isl_pw_aff_gt_map_aligned(__isl_take isl_pw_aff
*pa1
,
3051 __isl_take isl_pw_aff
*pa2
)
3053 return isl_pw_aff_order_map_aligned(pa1
, pa2
, &isl_pw_aff_gt_set
);
3056 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3057 * where the function value of "pa1" is greater than the function value
3060 __isl_give isl_map
*isl_pw_aff_gt_map(__isl_take isl_pw_aff
*pa1
,
3061 __isl_take isl_pw_aff
*pa2
)
3063 return align_params_pw_pw_map_and(pa1
, pa2
, &isl_pw_aff_gt_map_aligned
);
3066 /* Return a set containing those elements in the shared domain
3067 * of the elements of list1 and list2 where each element in list1
3068 * has the relation specified by "fn" with each element in list2.
3070 static __isl_give isl_set
*pw_aff_list_set(__isl_take isl_pw_aff_list
*list1
,
3071 __isl_take isl_pw_aff_list
*list2
,
3072 __isl_give isl_set
*(*fn
)(__isl_take isl_pw_aff
*pwaff1
,
3073 __isl_take isl_pw_aff
*pwaff2
))
3079 if (!list1
|| !list2
)
3082 ctx
= isl_pw_aff_list_get_ctx(list1
);
3083 if (list1
->n
< 1 || list2
->n
< 1)
3084 isl_die(ctx
, isl_error_invalid
,
3085 "list should contain at least one element", goto error
);
3087 set
= isl_set_universe(isl_pw_aff_get_domain_space(list1
->p
[0]));
3088 for (i
= 0; i
< list1
->n
; ++i
)
3089 for (j
= 0; j
< list2
->n
; ++j
) {
3092 set_ij
= fn(isl_pw_aff_copy(list1
->p
[i
]),
3093 isl_pw_aff_copy(list2
->p
[j
]));
3094 set
= isl_set_intersect(set
, set_ij
);
3097 isl_pw_aff_list_free(list1
);
3098 isl_pw_aff_list_free(list2
);
3101 isl_pw_aff_list_free(list1
);
3102 isl_pw_aff_list_free(list2
);
3106 /* Return a set containing those elements in the shared domain
3107 * of the elements of list1 and list2 where each element in list1
3108 * is equal to each element in list2.
3110 __isl_give isl_set
*isl_pw_aff_list_eq_set(__isl_take isl_pw_aff_list
*list1
,
3111 __isl_take isl_pw_aff_list
*list2
)
3113 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_eq_set
);
3116 __isl_give isl_set
*isl_pw_aff_list_ne_set(__isl_take isl_pw_aff_list
*list1
,
3117 __isl_take isl_pw_aff_list
*list2
)
3119 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_ne_set
);
3122 /* Return a set containing those elements in the shared domain
3123 * of the elements of list1 and list2 where each element in list1
3124 * is less than or equal to each element in list2.
3126 __isl_give isl_set
*isl_pw_aff_list_le_set(__isl_take isl_pw_aff_list
*list1
,
3127 __isl_take isl_pw_aff_list
*list2
)
3129 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_le_set
);
3132 __isl_give isl_set
*isl_pw_aff_list_lt_set(__isl_take isl_pw_aff_list
*list1
,
3133 __isl_take isl_pw_aff_list
*list2
)
3135 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_lt_set
);
3138 __isl_give isl_set
*isl_pw_aff_list_ge_set(__isl_take isl_pw_aff_list
*list1
,
3139 __isl_take isl_pw_aff_list
*list2
)
3141 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_ge_set
);
3144 __isl_give isl_set
*isl_pw_aff_list_gt_set(__isl_take isl_pw_aff_list
*list1
,
3145 __isl_take isl_pw_aff_list
*list2
)
3147 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_gt_set
);
3151 /* Return a set containing those elements in the shared domain
3152 * of pwaff1 and pwaff2 where pwaff1 is not equal to pwaff2.
3154 static __isl_give isl_set
*pw_aff_ne_set(__isl_take isl_pw_aff
*pwaff1
,
3155 __isl_take isl_pw_aff
*pwaff2
)
3157 isl_set
*set_lt
, *set_gt
;
3159 set_lt
= isl_pw_aff_lt_set(isl_pw_aff_copy(pwaff1
),
3160 isl_pw_aff_copy(pwaff2
));
3161 set_gt
= isl_pw_aff_gt_set(pwaff1
, pwaff2
);
3162 return isl_set_union_disjoint(set_lt
, set_gt
);
3165 __isl_give isl_set
*isl_pw_aff_ne_set(__isl_take isl_pw_aff
*pwaff1
,
3166 __isl_take isl_pw_aff
*pwaff2
)
3168 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_ne_set
);
3171 __isl_give isl_pw_aff
*isl_pw_aff_scale_down(__isl_take isl_pw_aff
*pwaff
,
3176 if (isl_int_is_one(v
))
3178 if (!isl_int_is_pos(v
))
3179 isl_die(isl_pw_aff_get_ctx(pwaff
), isl_error_invalid
,
3180 "factor needs to be positive",
3181 return isl_pw_aff_free(pwaff
));
3182 pwaff
= isl_pw_aff_cow(pwaff
);
3188 for (i
= 0; i
< pwaff
->n
; ++i
) {
3189 pwaff
->p
[i
].aff
= isl_aff_scale_down(pwaff
->p
[i
].aff
, v
);
3190 if (!pwaff
->p
[i
].aff
)
3191 return isl_pw_aff_free(pwaff
);
3197 __isl_give isl_pw_aff
*isl_pw_aff_floor(__isl_take isl_pw_aff
*pwaff
)
3201 pwaff
= isl_pw_aff_cow(pwaff
);
3207 for (i
= 0; i
< pwaff
->n
; ++i
) {
3208 pwaff
->p
[i
].aff
= isl_aff_floor(pwaff
->p
[i
].aff
);
3209 if (!pwaff
->p
[i
].aff
)
3210 return isl_pw_aff_free(pwaff
);
3216 __isl_give isl_pw_aff
*isl_pw_aff_ceil(__isl_take isl_pw_aff
*pwaff
)
3220 pwaff
= isl_pw_aff_cow(pwaff
);
3226 for (i
= 0; i
< pwaff
->n
; ++i
) {
3227 pwaff
->p
[i
].aff
= isl_aff_ceil(pwaff
->p
[i
].aff
);
3228 if (!pwaff
->p
[i
].aff
)
3229 return isl_pw_aff_free(pwaff
);
3235 /* Assuming that "cond1" and "cond2" are disjoint,
3236 * return an affine expression that is equal to pwaff1 on cond1
3237 * and to pwaff2 on cond2.
3239 static __isl_give isl_pw_aff
*isl_pw_aff_select(
3240 __isl_take isl_set
*cond1
, __isl_take isl_pw_aff
*pwaff1
,
3241 __isl_take isl_set
*cond2
, __isl_take isl_pw_aff
*pwaff2
)
3243 pwaff1
= isl_pw_aff_intersect_domain(pwaff1
, cond1
);
3244 pwaff2
= isl_pw_aff_intersect_domain(pwaff2
, cond2
);
3246 return isl_pw_aff_add_disjoint(pwaff1
, pwaff2
);
3249 /* Return an affine expression that is equal to pwaff_true for elements
3250 * where "cond" is non-zero and to pwaff_false for elements where "cond"
3252 * That is, return cond ? pwaff_true : pwaff_false;
3254 * If "cond" involves and NaN, then we conservatively return a NaN
3255 * on its entire domain. In principle, we could consider the pieces
3256 * where it is NaN separately from those where it is not.
3258 * If "pwaff_true" and "pwaff_false" are obviously equal to each other,
3259 * then only use the domain of "cond" to restrict the domain.
3261 __isl_give isl_pw_aff
*isl_pw_aff_cond(__isl_take isl_pw_aff
*cond
,
3262 __isl_take isl_pw_aff
*pwaff_true
, __isl_take isl_pw_aff
*pwaff_false
)
3264 isl_set
*cond_true
, *cond_false
;
3269 if (isl_pw_aff_involves_nan(cond
)) {
3270 isl_space
*space
= isl_pw_aff_get_domain_space(cond
);
3271 isl_local_space
*ls
= isl_local_space_from_space(space
);
3272 isl_pw_aff_free(cond
);
3273 isl_pw_aff_free(pwaff_true
);
3274 isl_pw_aff_free(pwaff_false
);
3275 return isl_pw_aff_nan_on_domain(ls
);
3278 pwaff_true
= isl_pw_aff_align_params(pwaff_true
,
3279 isl_pw_aff_get_space(pwaff_false
));
3280 pwaff_false
= isl_pw_aff_align_params(pwaff_false
,
3281 isl_pw_aff_get_space(pwaff_true
));
3282 equal
= isl_pw_aff_plain_is_equal(pwaff_true
, pwaff_false
);
3288 dom
= isl_set_coalesce(isl_pw_aff_domain(cond
));
3289 isl_pw_aff_free(pwaff_false
);
3290 return isl_pw_aff_intersect_domain(pwaff_true
, dom
);
3293 cond_true
= isl_pw_aff_non_zero_set(isl_pw_aff_copy(cond
));
3294 cond_false
= isl_pw_aff_zero_set(cond
);
3295 return isl_pw_aff_select(cond_true
, pwaff_true
,
3296 cond_false
, pwaff_false
);
3298 isl_pw_aff_free(cond
);
3299 isl_pw_aff_free(pwaff_true
);
3300 isl_pw_aff_free(pwaff_false
);
3304 isl_bool
isl_aff_is_cst(__isl_keep isl_aff
*aff
)
3307 return isl_bool_error
;
3309 return isl_seq_first_non_zero(aff
->v
->el
+ 2, aff
->v
->size
- 2) == -1;
3312 /* Check whether pwaff is a piecewise constant.
3314 isl_bool
isl_pw_aff_is_cst(__isl_keep isl_pw_aff
*pwaff
)
3319 return isl_bool_error
;
3321 for (i
= 0; i
< pwaff
->n
; ++i
) {
3322 isl_bool is_cst
= isl_aff_is_cst(pwaff
->p
[i
].aff
);
3323 if (is_cst
< 0 || !is_cst
)
3327 return isl_bool_true
;
3330 /* Are all elements of "mpa" piecewise constants?
3332 isl_bool
isl_multi_pw_aff_is_cst(__isl_keep isl_multi_pw_aff
*mpa
)
3337 return isl_bool_error
;
3339 for (i
= 0; i
< mpa
->n
; ++i
) {
3340 isl_bool is_cst
= isl_pw_aff_is_cst(mpa
->u
.p
[i
]);
3341 if (is_cst
< 0 || !is_cst
)
3345 return isl_bool_true
;
3348 /* Return the product of "aff1" and "aff2".
3350 * If either of the two is NaN, then the result is NaN.
3352 * Otherwise, at least one of "aff1" or "aff2" needs to be a constant.
3354 __isl_give isl_aff
*isl_aff_mul(__isl_take isl_aff
*aff1
,
3355 __isl_take isl_aff
*aff2
)
3360 if (isl_aff_is_nan(aff1
)) {
3364 if (isl_aff_is_nan(aff2
)) {
3369 if (!isl_aff_is_cst(aff2
) && isl_aff_is_cst(aff1
))
3370 return isl_aff_mul(aff2
, aff1
);
3372 if (!isl_aff_is_cst(aff2
))
3373 isl_die(isl_aff_get_ctx(aff1
), isl_error_invalid
,
3374 "at least one affine expression should be constant",
3377 aff1
= isl_aff_cow(aff1
);
3381 aff1
= isl_aff_scale(aff1
, aff2
->v
->el
[1]);
3382 aff1
= isl_aff_scale_down(aff1
, aff2
->v
->el
[0]);
3392 /* Divide "aff1" by "aff2", assuming "aff2" is a constant.
3394 * If either of the two is NaN, then the result is NaN.
3396 __isl_give isl_aff
*isl_aff_div(__isl_take isl_aff
*aff1
,
3397 __isl_take isl_aff
*aff2
)
3405 if (isl_aff_is_nan(aff1
)) {
3409 if (isl_aff_is_nan(aff2
)) {
3414 is_cst
= isl_aff_is_cst(aff2
);
3418 isl_die(isl_aff_get_ctx(aff2
), isl_error_invalid
,
3419 "second argument should be a constant", goto error
);
3424 neg
= isl_int_is_neg(aff2
->v
->el
[1]);
3426 isl_int_neg(aff2
->v
->el
[0], aff2
->v
->el
[0]);
3427 isl_int_neg(aff2
->v
->el
[1], aff2
->v
->el
[1]);
3430 aff1
= isl_aff_scale(aff1
, aff2
->v
->el
[0]);
3431 aff1
= isl_aff_scale_down(aff1
, aff2
->v
->el
[1]);
3434 isl_int_neg(aff2
->v
->el
[0], aff2
->v
->el
[0]);
3435 isl_int_neg(aff2
->v
->el
[1], aff2
->v
->el
[1]);
3446 static __isl_give isl_pw_aff
*pw_aff_add(__isl_take isl_pw_aff
*pwaff1
,
3447 __isl_take isl_pw_aff
*pwaff2
)
3449 return isl_pw_aff_on_shared_domain(pwaff1
, pwaff2
, &isl_aff_add
);
3452 __isl_give isl_pw_aff
*isl_pw_aff_add(__isl_take isl_pw_aff
*pwaff1
,
3453 __isl_take isl_pw_aff
*pwaff2
)
3455 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
, &pw_aff_add
);
3458 __isl_give isl_pw_aff
*isl_pw_aff_union_add(__isl_take isl_pw_aff
*pwaff1
,
3459 __isl_take isl_pw_aff
*pwaff2
)
3461 return isl_pw_aff_union_add_(pwaff1
, pwaff2
);
3464 static __isl_give isl_pw_aff
*pw_aff_mul(__isl_take isl_pw_aff
*pwaff1
,
3465 __isl_take isl_pw_aff
*pwaff2
)
3467 return isl_pw_aff_on_shared_domain(pwaff1
, pwaff2
, &isl_aff_mul
);
3470 __isl_give isl_pw_aff
*isl_pw_aff_mul(__isl_take isl_pw_aff
*pwaff1
,
3471 __isl_take isl_pw_aff
*pwaff2
)
3473 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
, &pw_aff_mul
);
3476 static __isl_give isl_pw_aff
*pw_aff_div(__isl_take isl_pw_aff
*pa1
,
3477 __isl_take isl_pw_aff
*pa2
)
3479 return isl_pw_aff_on_shared_domain(pa1
, pa2
, &isl_aff_div
);
3482 /* Divide "pa1" by "pa2", assuming "pa2" is a piecewise constant.
3484 __isl_give isl_pw_aff
*isl_pw_aff_div(__isl_take isl_pw_aff
*pa1
,
3485 __isl_take isl_pw_aff
*pa2
)
3489 is_cst
= isl_pw_aff_is_cst(pa2
);
3493 isl_die(isl_pw_aff_get_ctx(pa2
), isl_error_invalid
,
3494 "second argument should be a piecewise constant",
3496 return isl_pw_aff_align_params_pw_pw_and(pa1
, pa2
, &pw_aff_div
);
3498 isl_pw_aff_free(pa1
);
3499 isl_pw_aff_free(pa2
);
3503 /* Compute the quotient of the integer division of "pa1" by "pa2"
3504 * with rounding towards zero.
3505 * "pa2" is assumed to be a piecewise constant.
3507 * In particular, return
3509 * pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2)
3512 __isl_give isl_pw_aff
*isl_pw_aff_tdiv_q(__isl_take isl_pw_aff
*pa1
,
3513 __isl_take isl_pw_aff
*pa2
)
3519 is_cst
= isl_pw_aff_is_cst(pa2
);
3523 isl_die(isl_pw_aff_get_ctx(pa2
), isl_error_invalid
,
3524 "second argument should be a piecewise constant",
3527 pa1
= isl_pw_aff_div(pa1
, pa2
);
3529 cond
= isl_pw_aff_nonneg_set(isl_pw_aff_copy(pa1
));
3530 f
= isl_pw_aff_floor(isl_pw_aff_copy(pa1
));
3531 c
= isl_pw_aff_ceil(pa1
);
3532 return isl_pw_aff_cond(isl_set_indicator_function(cond
), f
, c
);
3534 isl_pw_aff_free(pa1
);
3535 isl_pw_aff_free(pa2
);
3539 /* Compute the remainder of the integer division of "pa1" by "pa2"
3540 * with rounding towards zero.
3541 * "pa2" is assumed to be a piecewise constant.
3543 * In particular, return
3545 * pa1 - pa2 * (pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2))
3548 __isl_give isl_pw_aff
*isl_pw_aff_tdiv_r(__isl_take isl_pw_aff
*pa1
,
3549 __isl_take isl_pw_aff
*pa2
)
3554 is_cst
= isl_pw_aff_is_cst(pa2
);
3558 isl_die(isl_pw_aff_get_ctx(pa2
), isl_error_invalid
,
3559 "second argument should be a piecewise constant",
3561 res
= isl_pw_aff_tdiv_q(isl_pw_aff_copy(pa1
), isl_pw_aff_copy(pa2
));
3562 res
= isl_pw_aff_mul(pa2
, res
);
3563 res
= isl_pw_aff_sub(pa1
, res
);
3566 isl_pw_aff_free(pa1
);
3567 isl_pw_aff_free(pa2
);
3571 /* Does either of "pa1" or "pa2" involve any NaN2?
3573 static isl_bool
either_involves_nan(__isl_keep isl_pw_aff
*pa1
,
3574 __isl_keep isl_pw_aff
*pa2
)
3578 has_nan
= isl_pw_aff_involves_nan(pa1
);
3579 if (has_nan
< 0 || has_nan
)
3581 return isl_pw_aff_involves_nan(pa2
);
3584 /* Replace "pa1" and "pa2" (at least one of which involves a NaN)
3585 * by a NaN on their shared domain.
3587 * In principle, the result could be refined to only being NaN
3588 * on the parts of this domain where at least one of "pa1" or "pa2" is NaN.
3590 static __isl_give isl_pw_aff
*replace_by_nan(__isl_take isl_pw_aff
*pa1
,
3591 __isl_take isl_pw_aff
*pa2
)
3593 isl_local_space
*ls
;
3597 dom
= isl_set_intersect(isl_pw_aff_domain(pa1
), isl_pw_aff_domain(pa2
));
3598 ls
= isl_local_space_from_space(isl_set_get_space(dom
));
3599 pa
= isl_pw_aff_nan_on_domain(ls
);
3600 pa
= isl_pw_aff_intersect_domain(pa
, dom
);
3605 static __isl_give isl_pw_aff
*pw_aff_min(__isl_take isl_pw_aff
*pwaff1
,
3606 __isl_take isl_pw_aff
*pwaff2
)
3611 dom
= isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1
)),
3612 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2
)));
3613 le
= isl_pw_aff_le_set(isl_pw_aff_copy(pwaff1
),
3614 isl_pw_aff_copy(pwaff2
));
3615 dom
= isl_set_subtract(dom
, isl_set_copy(le
));
3616 return isl_pw_aff_select(le
, pwaff1
, dom
, pwaff2
);
3619 static __isl_give isl_pw_aff
*pw_aff_max(__isl_take isl_pw_aff
*pwaff1
,
3620 __isl_take isl_pw_aff
*pwaff2
)
3625 dom
= isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1
)),
3626 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2
)));
3627 ge
= isl_pw_aff_ge_set(isl_pw_aff_copy(pwaff1
),
3628 isl_pw_aff_copy(pwaff2
));
3629 dom
= isl_set_subtract(dom
, isl_set_copy(ge
));
3630 return isl_pw_aff_select(ge
, pwaff1
, dom
, pwaff2
);
3633 /* Return an expression for the minimum (if "max" is not set) or
3634 * the maximum (if "max" is set) of "pa1" and "pa2".
3635 * If either expression involves any NaN, then return a NaN
3636 * on the shared domain as result.
3638 static __isl_give isl_pw_aff
*pw_aff_min_max(__isl_take isl_pw_aff
*pa1
,
3639 __isl_take isl_pw_aff
*pa2
, int max
)
3643 has_nan
= either_involves_nan(pa1
, pa2
);
3645 pa1
= isl_pw_aff_free(pa1
);
3647 return replace_by_nan(pa1
, pa2
);
3650 return isl_pw_aff_align_params_pw_pw_and(pa1
, pa2
, &pw_aff_max
);
3652 return isl_pw_aff_align_params_pw_pw_and(pa1
, pa2
, &pw_aff_min
);
3655 /* Return an expression for the minimum of "pwaff1" and "pwaff2".
3657 __isl_give isl_pw_aff
*isl_pw_aff_min(__isl_take isl_pw_aff
*pwaff1
,
3658 __isl_take isl_pw_aff
*pwaff2
)
3660 return pw_aff_min_max(pwaff1
, pwaff2
, 0);
3663 /* Return an expression for the maximum of "pwaff1" and "pwaff2".
3665 __isl_give isl_pw_aff
*isl_pw_aff_max(__isl_take isl_pw_aff
*pwaff1
,
3666 __isl_take isl_pw_aff
*pwaff2
)
3668 return pw_aff_min_max(pwaff1
, pwaff2
, 1);
3671 static __isl_give isl_pw_aff
*pw_aff_list_reduce(
3672 __isl_take isl_pw_aff_list
*list
,
3673 __isl_give isl_pw_aff
*(*fn
)(__isl_take isl_pw_aff
*pwaff1
,
3674 __isl_take isl_pw_aff
*pwaff2
))
3683 ctx
= isl_pw_aff_list_get_ctx(list
);
3685 isl_die(ctx
, isl_error_invalid
,
3686 "list should contain at least one element", goto error
);
3688 res
= isl_pw_aff_copy(list
->p
[0]);
3689 for (i
= 1; i
< list
->n
; ++i
)
3690 res
= fn(res
, isl_pw_aff_copy(list
->p
[i
]));
3692 isl_pw_aff_list_free(list
);
3695 isl_pw_aff_list_free(list
);
3699 /* Return an isl_pw_aff that maps each element in the intersection of the
3700 * domains of the elements of list to the minimal corresponding affine
3703 __isl_give isl_pw_aff
*isl_pw_aff_list_min(__isl_take isl_pw_aff_list
*list
)
3705 return pw_aff_list_reduce(list
, &isl_pw_aff_min
);
3708 /* Return an isl_pw_aff that maps each element in the intersection of the
3709 * domains of the elements of list to the maximal corresponding affine
3712 __isl_give isl_pw_aff
*isl_pw_aff_list_max(__isl_take isl_pw_aff_list
*list
)
3714 return pw_aff_list_reduce(list
, &isl_pw_aff_max
);
3717 /* Mark the domains of "pwaff" as rational.
3719 __isl_give isl_pw_aff
*isl_pw_aff_set_rational(__isl_take isl_pw_aff
*pwaff
)
3723 pwaff
= isl_pw_aff_cow(pwaff
);
3729 for (i
= 0; i
< pwaff
->n
; ++i
) {
3730 pwaff
->p
[i
].set
= isl_set_set_rational(pwaff
->p
[i
].set
);
3731 if (!pwaff
->p
[i
].set
)
3732 return isl_pw_aff_free(pwaff
);
3738 /* Mark the domains of the elements of "list" as rational.
3740 __isl_give isl_pw_aff_list
*isl_pw_aff_list_set_rational(
3741 __isl_take isl_pw_aff_list
*list
)
3751 for (i
= 0; i
< n
; ++i
) {
3754 pa
= isl_pw_aff_list_get_pw_aff(list
, i
);
3755 pa
= isl_pw_aff_set_rational(pa
);
3756 list
= isl_pw_aff_list_set_pw_aff(list
, i
, pa
);
3762 /* Do the parameters of "aff" match those of "space"?
3764 isl_bool
isl_aff_matching_params(__isl_keep isl_aff
*aff
,
3765 __isl_keep isl_space
*space
)
3767 isl_space
*aff_space
;
3771 return isl_bool_error
;
3773 aff_space
= isl_aff_get_domain_space(aff
);
3775 match
= isl_space_has_equal_params(space
, aff_space
);
3777 isl_space_free(aff_space
);
3781 /* Check that the domain space of "aff" matches "space".
3783 isl_stat
isl_aff_check_match_domain_space(__isl_keep isl_aff
*aff
,
3784 __isl_keep isl_space
*space
)
3786 isl_space
*aff_space
;
3790 return isl_stat_error
;
3792 aff_space
= isl_aff_get_domain_space(aff
);
3794 match
= isl_space_has_equal_params(space
, aff_space
);
3798 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
3799 "parameters don't match", goto error
);
3800 match
= isl_space_tuple_is_equal(space
, isl_dim_in
,
3801 aff_space
, isl_dim_set
);
3805 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
3806 "domains don't match", goto error
);
3807 isl_space_free(aff_space
);
3810 isl_space_free(aff_space
);
3811 return isl_stat_error
;
3819 #include <isl_multi_no_explicit_domain.c>
3820 #include <isl_multi_templ.c>
3821 #include <isl_multi_apply_set.c>
3822 #include <isl_multi_cmp.c>
3823 #include <isl_multi_dims.c>
3824 #include <isl_multi_floor.c>
3825 #include <isl_multi_from_base_templ.c>
3826 #include <isl_multi_gist.c>
3827 #include <isl_multi_identity_templ.c>
3828 #include <isl_multi_move_dims_templ.c>
3829 #include <isl_multi_product_templ.c>
3830 #include <isl_multi_splice_templ.c>
3831 #include <isl_multi_zero_templ.c>
3833 /* Construct an isl_multi_aff living in "space" that corresponds
3834 * to the affine transformation matrix "mat".
3836 __isl_give isl_multi_aff
*isl_multi_aff_from_aff_mat(
3837 __isl_take isl_space
*space
, __isl_take isl_mat
*mat
)
3840 isl_local_space
*ls
= NULL
;
3841 isl_multi_aff
*ma
= NULL
;
3842 isl_size n_row
, n_col
, n_out
, total
;
3848 ctx
= isl_mat_get_ctx(mat
);
3850 n_row
= isl_mat_rows(mat
);
3851 n_col
= isl_mat_cols(mat
);
3852 n_out
= isl_space_dim(space
, isl_dim_out
);
3853 total
= isl_space_dim(space
, isl_dim_all
);
3854 if (n_row
< 0 || n_col
< 0 || n_out
< 0 || total
< 0)
3857 isl_die(ctx
, isl_error_invalid
,
3858 "insufficient number of rows", goto error
);
3860 isl_die(ctx
, isl_error_invalid
,
3861 "insufficient number of columns", goto error
);
3862 if (1 + n_out
!= n_row
|| 2 + total
!= n_row
+ n_col
)
3863 isl_die(ctx
, isl_error_invalid
,
3864 "dimension mismatch", goto error
);
3866 ma
= isl_multi_aff_zero(isl_space_copy(space
));
3867 ls
= isl_local_space_from_space(isl_space_domain(space
));
3869 for (i
= 0; i
< n_row
- 1; ++i
) {
3873 v
= isl_vec_alloc(ctx
, 1 + n_col
);
3876 isl_int_set(v
->el
[0], mat
->row
[0][0]);
3877 isl_seq_cpy(v
->el
+ 1, mat
->row
[1 + i
], n_col
);
3878 v
= isl_vec_normalize(v
);
3879 aff
= isl_aff_alloc_vec(isl_local_space_copy(ls
), v
);
3880 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
3883 isl_local_space_free(ls
);
3887 isl_local_space_free(ls
);
3889 isl_multi_aff_free(ma
);
3893 /* Remove any internal structure of the domain of "ma".
3894 * If there is any such internal structure in the input,
3895 * then the name of the corresponding space is also removed.
3897 __isl_give isl_multi_aff
*isl_multi_aff_flatten_domain(
3898 __isl_take isl_multi_aff
*ma
)
3905 if (!ma
->space
->nested
[0])
3908 space
= isl_multi_aff_get_space(ma
);
3909 space
= isl_space_flatten_domain(space
);
3910 ma
= isl_multi_aff_reset_space(ma
, space
);
3915 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
3916 * of the space to its domain.
3918 __isl_give isl_multi_aff
*isl_multi_aff_domain_map(__isl_take isl_space
*space
)
3922 isl_local_space
*ls
;
3927 if (!isl_space_is_map(space
))
3928 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
3929 "not a map space", goto error
);
3931 n_in
= isl_space_dim(space
, isl_dim_in
);
3934 space
= isl_space_domain_map(space
);
3936 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
3938 isl_space_free(space
);
3942 space
= isl_space_domain(space
);
3943 ls
= isl_local_space_from_space(space
);
3944 for (i
= 0; i
< n_in
; ++i
) {
3947 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
3949 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
3951 isl_local_space_free(ls
);
3954 isl_space_free(space
);
3958 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
3959 * of the space to its range.
3961 __isl_give isl_multi_aff
*isl_multi_aff_range_map(__isl_take isl_space
*space
)
3964 isl_size n_in
, n_out
;
3965 isl_local_space
*ls
;
3970 if (!isl_space_is_map(space
))
3971 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
3972 "not a map space", goto error
);
3974 n_in
= isl_space_dim(space
, isl_dim_in
);
3975 n_out
= isl_space_dim(space
, isl_dim_out
);
3976 if (n_in
< 0 || n_out
< 0)
3978 space
= isl_space_range_map(space
);
3980 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
3982 isl_space_free(space
);
3986 space
= isl_space_domain(space
);
3987 ls
= isl_local_space_from_space(space
);
3988 for (i
= 0; i
< n_out
; ++i
) {
3991 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
3992 isl_dim_set
, n_in
+ i
);
3993 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
3995 isl_local_space_free(ls
);
3998 isl_space_free(space
);
4002 /* Given a map space, return an isl_pw_multi_aff that maps a wrapped copy
4003 * of the space to its range.
4005 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_range_map(
4006 __isl_take isl_space
*space
)
4008 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_range_map(space
));
4011 /* Given the space of a set and a range of set dimensions,
4012 * construct an isl_multi_aff that projects out those dimensions.
4014 __isl_give isl_multi_aff
*isl_multi_aff_project_out_map(
4015 __isl_take isl_space
*space
, enum isl_dim_type type
,
4016 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
);
4031 if (isl_space_check_range(space
, type
, first
, n
) < 0)
4034 dim
= isl_space_dim(space
, isl_dim_set
);
4038 space
= isl_space_from_domain(space
);
4039 space
= isl_space_add_dims(space
, isl_dim_out
, dim
- n
);
4042 return isl_multi_aff_alloc(space
);
4044 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
4045 space
= isl_space_domain(space
);
4046 ls
= isl_local_space_from_space(space
);
4048 for (i
= 0; i
< first
; ++i
) {
4051 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
4053 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
4056 for (i
= 0; i
< dim
- (first
+ n
); ++i
) {
4059 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
4060 isl_dim_set
, first
+ n
+ i
);
4061 ma
= isl_multi_aff_set_aff(ma
, first
+ i
, aff
);
4064 isl_local_space_free(ls
);
4067 isl_space_free(space
);
4071 /* Given the space of a set and a range of set dimensions,
4072 * construct an isl_pw_multi_aff that projects out those dimensions.
4074 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_project_out_map(
4075 __isl_take isl_space
*space
, enum isl_dim_type type
,
4076 unsigned first
, unsigned n
)
4080 ma
= isl_multi_aff_project_out_map(space
, type
, first
, n
);
4081 return isl_pw_multi_aff_from_multi_aff(ma
);
4084 /* Create an isl_pw_multi_aff with the given isl_multi_aff on a universe
4087 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_multi_aff(
4088 __isl_take isl_multi_aff
*ma
)
4090 isl_set
*dom
= isl_set_universe(isl_multi_aff_get_domain_space(ma
));
4091 return isl_pw_multi_aff_alloc(dom
, ma
);
4094 /* Create a piecewise multi-affine expression in the given space that maps each
4095 * input dimension to the corresponding output dimension.
4097 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_identity(
4098 __isl_take isl_space
*space
)
4100 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_identity(space
));
4103 /* Exploit the equalities in "eq" to simplify the affine expressions.
4105 static __isl_give isl_multi_aff
*isl_multi_aff_substitute_equalities(
4106 __isl_take isl_multi_aff
*maff
, __isl_take isl_basic_set
*eq
)
4110 maff
= isl_multi_aff_cow(maff
);
4114 for (i
= 0; i
< maff
->n
; ++i
) {
4115 maff
->u
.p
[i
] = isl_aff_substitute_equalities(maff
->u
.p
[i
],
4116 isl_basic_set_copy(eq
));
4121 isl_basic_set_free(eq
);
4124 isl_basic_set_free(eq
);
4125 isl_multi_aff_free(maff
);
4129 __isl_give isl_multi_aff
*isl_multi_aff_scale(__isl_take isl_multi_aff
*maff
,
4134 maff
= isl_multi_aff_cow(maff
);
4138 for (i
= 0; i
< maff
->n
; ++i
) {
4139 maff
->u
.p
[i
] = isl_aff_scale(maff
->u
.p
[i
], f
);
4141 return isl_multi_aff_free(maff
);
4147 __isl_give isl_multi_aff
*isl_multi_aff_add_on_domain(__isl_keep isl_set
*dom
,
4148 __isl_take isl_multi_aff
*maff1
, __isl_take isl_multi_aff
*maff2
)
4150 maff1
= isl_multi_aff_add(maff1
, maff2
);
4151 maff1
= isl_multi_aff_gist(maff1
, isl_set_copy(dom
));
4155 int isl_multi_aff_is_empty(__isl_keep isl_multi_aff
*maff
)
4163 /* Return the set of domain elements where "ma1" is lexicographically
4164 * smaller than or equal to "ma2".
4166 __isl_give isl_set
*isl_multi_aff_lex_le_set(__isl_take isl_multi_aff
*ma1
,
4167 __isl_take isl_multi_aff
*ma2
)
4169 return isl_multi_aff_lex_ge_set(ma2
, ma1
);
4172 /* Return the set of domain elements where "ma1" is lexicographically
4173 * smaller than "ma2".
4175 __isl_give isl_set
*isl_multi_aff_lex_lt_set(__isl_take isl_multi_aff
*ma1
,
4176 __isl_take isl_multi_aff
*ma2
)
4178 return isl_multi_aff_lex_gt_set(ma2
, ma1
);
4181 /* Return the set of domain elements where "ma1" and "ma2"
4184 static __isl_give isl_set
*isl_multi_aff_order_set(
4185 __isl_take isl_multi_aff
*ma1
, __isl_take isl_multi_aff
*ma2
,
4186 __isl_give isl_map
*order(__isl_take isl_space
*set_space
))
4189 isl_map
*map1
, *map2
;
4192 map1
= isl_map_from_multi_aff_internal(ma1
);
4193 map2
= isl_map_from_multi_aff_internal(ma2
);
4194 map
= isl_map_range_product(map1
, map2
);
4195 space
= isl_space_range(isl_map_get_space(map
));
4196 space
= isl_space_domain(isl_space_unwrap(space
));
4198 map
= isl_map_intersect_range(map
, isl_map_wrap(ge
));
4200 return isl_map_domain(map
);
4203 /* Return the set of domain elements where "ma1" is lexicographically
4204 * greater than or equal to "ma2".
4206 __isl_give isl_set
*isl_multi_aff_lex_ge_set(__isl_take isl_multi_aff
*ma1
,
4207 __isl_take isl_multi_aff
*ma2
)
4209 return isl_multi_aff_order_set(ma1
, ma2
, &isl_map_lex_ge
);
4212 /* Return the set of domain elements where "ma1" is lexicographically
4213 * greater than "ma2".
4215 __isl_give isl_set
*isl_multi_aff_lex_gt_set(__isl_take isl_multi_aff
*ma1
,
4216 __isl_take isl_multi_aff
*ma2
)
4218 return isl_multi_aff_order_set(ma1
, ma2
, &isl_map_lex_gt
);
4222 #define PW isl_pw_multi_aff
4224 #define EL isl_multi_aff
4226 #define EL_IS_ZERO is_empty
4230 #define IS_ZERO is_empty
4233 #undef DEFAULT_IS_ZERO
4234 #define DEFAULT_IS_ZERO 0
4238 #define NO_INSERT_DIMS
4242 #include <isl_pw_templ.c>
4243 #include <isl_pw_union_opt.c>
4248 #define BASE pw_multi_aff
4250 #include <isl_union_multi.c>
4251 #include <isl_union_neg.c>
4253 static __isl_give isl_pw_multi_aff
*pw_multi_aff_union_lexmax(
4254 __isl_take isl_pw_multi_aff
*pma1
,
4255 __isl_take isl_pw_multi_aff
*pma2
)
4257 return isl_pw_multi_aff_union_opt_cmp(pma1
, pma2
,
4258 &isl_multi_aff_lex_ge_set
);
4261 /* Given two piecewise multi affine expressions, return a piecewise
4262 * multi-affine expression defined on the union of the definition domains
4263 * of the inputs that is equal to the lexicographic maximum of the two
4264 * inputs on each cell. If only one of the two inputs is defined on
4265 * a given cell, then it is considered to be the maximum.
4267 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_union_lexmax(
4268 __isl_take isl_pw_multi_aff
*pma1
,
4269 __isl_take isl_pw_multi_aff
*pma2
)
4271 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
4272 &pw_multi_aff_union_lexmax
);
4275 static __isl_give isl_pw_multi_aff
*pw_multi_aff_union_lexmin(
4276 __isl_take isl_pw_multi_aff
*pma1
,
4277 __isl_take isl_pw_multi_aff
*pma2
)
4279 return isl_pw_multi_aff_union_opt_cmp(pma1
, pma2
,
4280 &isl_multi_aff_lex_le_set
);
4283 /* Given two piecewise multi affine expressions, return a piecewise
4284 * multi-affine expression defined on the union of the definition domains
4285 * of the inputs that is equal to the lexicographic minimum of the two
4286 * inputs on each cell. If only one of the two inputs is defined on
4287 * a given cell, then it is considered to be the minimum.
4289 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_union_lexmin(
4290 __isl_take isl_pw_multi_aff
*pma1
,
4291 __isl_take isl_pw_multi_aff
*pma2
)
4293 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
4294 &pw_multi_aff_union_lexmin
);
4297 static __isl_give isl_pw_multi_aff
*pw_multi_aff_add(
4298 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4300 return isl_pw_multi_aff_on_shared_domain(pma1
, pma2
,
4301 &isl_multi_aff_add
);
4304 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_add(
4305 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4307 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
4311 static __isl_give isl_pw_multi_aff
*pw_multi_aff_sub(
4312 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4314 return isl_pw_multi_aff_on_shared_domain(pma1
, pma2
,
4315 &isl_multi_aff_sub
);
4318 /* Subtract "pma2" from "pma1" and return the result.
4320 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_sub(
4321 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4323 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
4327 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_union_add(
4328 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4330 return isl_pw_multi_aff_union_add_(pma1
, pma2
);
4333 /* Compute the sum of "upa1" and "upa2" on the union of their domains,
4334 * with the actual sum on the shared domain and
4335 * the defined expression on the symmetric difference of the domains.
4337 __isl_give isl_union_pw_aff
*isl_union_pw_aff_union_add(
4338 __isl_take isl_union_pw_aff
*upa1
, __isl_take isl_union_pw_aff
*upa2
)
4340 return isl_union_pw_aff_union_add_(upa1
, upa2
);
4343 /* Compute the sum of "upma1" and "upma2" on the union of their domains,
4344 * with the actual sum on the shared domain and
4345 * the defined expression on the symmetric difference of the domains.
4347 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_union_add(
4348 __isl_take isl_union_pw_multi_aff
*upma1
,
4349 __isl_take isl_union_pw_multi_aff
*upma2
)
4351 return isl_union_pw_multi_aff_union_add_(upma1
, upma2
);
4354 /* Given two piecewise multi-affine expressions A -> B and C -> D,
4355 * construct a piecewise multi-affine expression [A -> C] -> [B -> D].
4357 static __isl_give isl_pw_multi_aff
*pw_multi_aff_product(
4358 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4362 isl_pw_multi_aff
*res
;
4367 n
= pma1
->n
* pma2
->n
;
4368 space
= isl_space_product(isl_space_copy(pma1
->dim
),
4369 isl_space_copy(pma2
->dim
));
4370 res
= isl_pw_multi_aff_alloc_size(space
, n
);
4372 for (i
= 0; i
< pma1
->n
; ++i
) {
4373 for (j
= 0; j
< pma2
->n
; ++j
) {
4377 domain
= isl_set_product(isl_set_copy(pma1
->p
[i
].set
),
4378 isl_set_copy(pma2
->p
[j
].set
));
4379 ma
= isl_multi_aff_product(
4380 isl_multi_aff_copy(pma1
->p
[i
].maff
),
4381 isl_multi_aff_copy(pma2
->p
[j
].maff
));
4382 res
= isl_pw_multi_aff_add_piece(res
, domain
, ma
);
4386 isl_pw_multi_aff_free(pma1
);
4387 isl_pw_multi_aff_free(pma2
);
4390 isl_pw_multi_aff_free(pma1
);
4391 isl_pw_multi_aff_free(pma2
);
4395 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_product(
4396 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4398 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
4399 &pw_multi_aff_product
);
4402 /* Subtract the initial "n" elements in "ma" with coefficients in "c" and
4403 * denominator "denom".
4404 * "denom" is allowed to be negative, in which case the actual denominator
4405 * is -denom and the expressions are added instead.
4407 static __isl_give isl_aff
*subtract_initial(__isl_take isl_aff
*aff
,
4408 __isl_keep isl_multi_aff
*ma
, int n
, isl_int
*c
, isl_int denom
)
4414 first
= isl_seq_first_non_zero(c
, n
);
4418 sign
= isl_int_sgn(denom
);
4420 isl_int_abs(d
, denom
);
4421 for (i
= first
; i
< n
; ++i
) {
4424 if (isl_int_is_zero(c
[i
]))
4426 aff_i
= isl_multi_aff_get_aff(ma
, i
);
4427 aff_i
= isl_aff_scale(aff_i
, c
[i
]);
4428 aff_i
= isl_aff_scale_down(aff_i
, d
);
4430 aff
= isl_aff_sub(aff
, aff_i
);
4432 aff
= isl_aff_add(aff
, aff_i
);
4439 /* Extract an affine expression that expresses the output dimension "pos"
4440 * of "bmap" in terms of the parameters and input dimensions from
4442 * Note that this expression may involve integer divisions defined
4443 * in terms of parameters and input dimensions.
4444 * The equality may also involve references to earlier (but not later)
4445 * output dimensions. These are replaced by the corresponding elements
4448 * If the equality is of the form
4450 * f(i) + h(j) + a x + g(i) = 0,
4452 * with f(i) a linear combinations of the parameters and input dimensions,
4453 * g(i) a linear combination of integer divisions defined in terms of the same
4454 * and h(j) a linear combinations of earlier output dimensions,
4455 * then the affine expression is
4457 * (-f(i) - g(i))/a - h(j)/a
4459 * If the equality is of the form
4461 * f(i) + h(j) - a x + g(i) = 0,
4463 * then the affine expression is
4465 * (f(i) + g(i))/a - h(j)/(-a)
4468 * If "div" refers to an integer division (i.e., it is smaller than
4469 * the number of integer divisions), then the equality constraint
4470 * does involve an integer division (the one at position "div") that
4471 * is defined in terms of output dimensions. However, this integer
4472 * division can be eliminated by exploiting a pair of constraints
4473 * x >= l and x <= l + n, with n smaller than the coefficient of "div"
4474 * in the equality constraint. "ineq" refers to inequality x >= l, i.e.,
4476 * In particular, let
4478 * x = e(i) + m floor(...)
4480 * with e(i) the expression derived above and floor(...) the integer
4481 * division involving output dimensions.
4492 * e(i) + m floor(...) - l = (e(i) + m floor(...) - l) mod m
4493 * = (e(i) - l) mod m
4497 * x - l = (e(i) - l) mod m
4501 * x = ((e(i) - l) mod m) + l
4503 * The variable "shift" below contains the expression -l, which may
4504 * also involve a linear combination of earlier output dimensions.
4506 static __isl_give isl_aff
*extract_aff_from_equality(
4507 __isl_keep isl_basic_map
*bmap
, int pos
, int eq
, int div
, int ineq
,
4508 __isl_keep isl_multi_aff
*ma
)
4511 isl_size n_div
, n_out
;
4513 isl_local_space
*ls
;
4514 isl_aff
*aff
, *shift
;
4517 ctx
= isl_basic_map_get_ctx(bmap
);
4518 ls
= isl_basic_map_get_local_space(bmap
);
4519 ls
= isl_local_space_domain(ls
);
4520 aff
= isl_aff_alloc(isl_local_space_copy(ls
));
4523 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
4524 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4525 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4526 if (n_out
< 0 || n_div
< 0)
4528 if (isl_int_is_neg(bmap
->eq
[eq
][o_out
+ pos
])) {
4529 isl_seq_cpy(aff
->v
->el
+ 1, bmap
->eq
[eq
], o_out
);
4530 isl_seq_cpy(aff
->v
->el
+ 1 + o_out
,
4531 bmap
->eq
[eq
] + o_out
+ n_out
, n_div
);
4533 isl_seq_neg(aff
->v
->el
+ 1, bmap
->eq
[eq
], o_out
);
4534 isl_seq_neg(aff
->v
->el
+ 1 + o_out
,
4535 bmap
->eq
[eq
] + o_out
+ n_out
, n_div
);
4538 isl_int_set_si(aff
->v
->el
[1 + o_out
+ div
], 0);
4539 isl_int_abs(aff
->v
->el
[0], bmap
->eq
[eq
][o_out
+ pos
]);
4540 aff
= subtract_initial(aff
, ma
, pos
, bmap
->eq
[eq
] + o_out
,
4541 bmap
->eq
[eq
][o_out
+ pos
]);
4543 shift
= isl_aff_alloc(isl_local_space_copy(ls
));
4546 isl_seq_cpy(shift
->v
->el
+ 1, bmap
->ineq
[ineq
], o_out
);
4547 isl_seq_cpy(shift
->v
->el
+ 1 + o_out
,
4548 bmap
->ineq
[ineq
] + o_out
+ n_out
, n_div
);
4549 isl_int_set_si(shift
->v
->el
[0], 1);
4550 shift
= subtract_initial(shift
, ma
, pos
,
4551 bmap
->ineq
[ineq
] + o_out
, ctx
->negone
);
4552 aff
= isl_aff_add(aff
, isl_aff_copy(shift
));
4553 mod
= isl_val_int_from_isl_int(ctx
,
4554 bmap
->eq
[eq
][o_out
+ n_out
+ div
]);
4555 mod
= isl_val_abs(mod
);
4556 aff
= isl_aff_mod_val(aff
, mod
);
4557 aff
= isl_aff_sub(aff
, shift
);
4560 isl_local_space_free(ls
);
4563 isl_local_space_free(ls
);
4568 /* Given a basic map with output dimensions defined
4569 * in terms of the parameters input dimensions and earlier
4570 * output dimensions using an equality (and possibly a pair on inequalities),
4571 * extract an isl_aff that expresses output dimension "pos" in terms
4572 * of the parameters and input dimensions.
4573 * Note that this expression may involve integer divisions defined
4574 * in terms of parameters and input dimensions.
4575 * "ma" contains the expressions corresponding to earlier output dimensions.
4577 * This function shares some similarities with
4578 * isl_basic_map_has_defining_equality and isl_constraint_get_bound.
4580 static __isl_give isl_aff
*extract_isl_aff_from_basic_map(
4581 __isl_keep isl_basic_map
*bmap
, int pos
, __isl_keep isl_multi_aff
*ma
)
4588 eq
= isl_basic_map_output_defining_equality(bmap
, pos
, &div
, &ineq
);
4589 if (eq
>= bmap
->n_eq
)
4590 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
4591 "unable to find suitable equality", return NULL
);
4592 aff
= extract_aff_from_equality(bmap
, pos
, eq
, div
, ineq
, ma
);
4594 aff
= isl_aff_remove_unused_divs(aff
);
4598 /* Given a basic map where each output dimension is defined
4599 * in terms of the parameters and input dimensions using an equality,
4600 * extract an isl_multi_aff that expresses the output dimensions in terms
4601 * of the parameters and input dimensions.
4603 static __isl_give isl_multi_aff
*extract_isl_multi_aff_from_basic_map(
4604 __isl_take isl_basic_map
*bmap
)
4613 ma
= isl_multi_aff_alloc(isl_basic_map_get_space(bmap
));
4614 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4616 ma
= isl_multi_aff_free(ma
);
4618 for (i
= 0; i
< n_out
; ++i
) {
4621 aff
= extract_isl_aff_from_basic_map(bmap
, i
, ma
);
4622 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
4625 isl_basic_map_free(bmap
);
4630 /* Given a basic set where each set dimension is defined
4631 * in terms of the parameters using an equality,
4632 * extract an isl_multi_aff that expresses the set dimensions in terms
4633 * of the parameters.
4635 __isl_give isl_multi_aff
*isl_multi_aff_from_basic_set_equalities(
4636 __isl_take isl_basic_set
*bset
)
4638 return extract_isl_multi_aff_from_basic_map(bset
);
4641 /* Create an isl_pw_multi_aff that is equivalent to
4642 * isl_map_intersect_domain(isl_map_from_basic_map(bmap), domain).
4643 * The given basic map is such that each output dimension is defined
4644 * in terms of the parameters and input dimensions using an equality.
4646 * Since some applications expect the result of isl_pw_multi_aff_from_map
4647 * to only contain integer affine expressions, we compute the floor
4648 * of the expression before returning.
4650 * Remove all constraints involving local variables without
4651 * an explicit representation (resulting in the removal of those
4652 * local variables) prior to the actual extraction to ensure
4653 * that the local spaces in which the resulting affine expressions
4654 * are created do not contain any unknown local variables.
4655 * Removing such constraints is safe because constraints involving
4656 * unknown local variables are not used to determine whether
4657 * a basic map is obviously single-valued.
4659 static __isl_give isl_pw_multi_aff
*plain_pw_multi_aff_from_map(
4660 __isl_take isl_set
*domain
, __isl_take isl_basic_map
*bmap
)
4664 bmap
= isl_basic_map_drop_constraint_involving_unknown_divs(bmap
);
4665 ma
= extract_isl_multi_aff_from_basic_map(bmap
);
4666 ma
= isl_multi_aff_floor(ma
);
4667 return isl_pw_multi_aff_alloc(domain
, ma
);
4670 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4671 * This obviously only works if the input "map" is single-valued.
4672 * If so, we compute the lexicographic minimum of the image in the form
4673 * of an isl_pw_multi_aff. Since the image is unique, it is equal
4674 * to its lexicographic minimum.
4675 * If the input is not single-valued, we produce an error.
4677 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_base(
4678 __isl_take isl_map
*map
)
4682 isl_pw_multi_aff
*pma
;
4684 sv
= isl_map_is_single_valued(map
);
4688 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
4689 "map is not single-valued", goto error
);
4690 map
= isl_map_make_disjoint(map
);
4694 pma
= isl_pw_multi_aff_empty(isl_map_get_space(map
));
4696 for (i
= 0; i
< map
->n
; ++i
) {
4697 isl_pw_multi_aff
*pma_i
;
4698 isl_basic_map
*bmap
;
4699 bmap
= isl_basic_map_copy(map
->p
[i
]);
4700 pma_i
= isl_basic_map_lexmin_pw_multi_aff(bmap
);
4701 pma
= isl_pw_multi_aff_add_disjoint(pma
, pma_i
);
4711 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
4712 * taking into account that the output dimension at position "d"
4713 * can be represented as
4715 * x = floor((e(...) + c1) / m)
4717 * given that constraint "i" is of the form
4719 * e(...) + c1 - m x >= 0
4722 * Let "map" be of the form
4726 * We construct a mapping
4728 * A -> [A -> x = floor(...)]
4730 * apply that to the map, obtaining
4732 * [A -> x = floor(...)] -> B
4734 * and equate dimension "d" to x.
4735 * We then compute a isl_pw_multi_aff representation of the resulting map
4736 * and plug in the mapping above.
4738 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_div(
4739 __isl_take isl_map
*map
, __isl_take isl_basic_map
*hull
, int d
, int i
)
4742 isl_space
*space
= NULL
;
4743 isl_local_space
*ls
;
4751 isl_pw_multi_aff
*pma
;
4754 is_set
= isl_map_is_set(map
);
4758 offset
= isl_basic_map_offset(hull
, isl_dim_out
);
4759 ctx
= isl_map_get_ctx(map
);
4760 space
= isl_space_domain(isl_map_get_space(map
));
4761 n_in
= isl_space_dim(space
, isl_dim_set
);
4762 n
= isl_space_dim(space
, isl_dim_all
);
4763 if (n_in
< 0 || n
< 0)
4766 v
= isl_vec_alloc(ctx
, 1 + 1 + n
);
4768 isl_int_neg(v
->el
[0], hull
->ineq
[i
][offset
+ d
]);
4769 isl_seq_cpy(v
->el
+ 1, hull
->ineq
[i
], 1 + n
);
4771 isl_basic_map_free(hull
);
4773 ls
= isl_local_space_from_space(isl_space_copy(space
));
4774 aff
= isl_aff_alloc_vec(ls
, v
);
4775 aff
= isl_aff_floor(aff
);
4777 isl_space_free(space
);
4778 ma
= isl_multi_aff_from_aff(aff
);
4780 ma
= isl_multi_aff_identity(isl_space_map_from_set(space
));
4781 ma
= isl_multi_aff_range_product(ma
,
4782 isl_multi_aff_from_aff(aff
));
4785 insert
= isl_map_from_multi_aff_internal(isl_multi_aff_copy(ma
));
4786 map
= isl_map_apply_domain(map
, insert
);
4787 map
= isl_map_equate(map
, isl_dim_in
, n_in
, isl_dim_out
, d
);
4788 pma
= isl_pw_multi_aff_from_map(map
);
4789 pma
= isl_pw_multi_aff_pullback_multi_aff(pma
, ma
);
4793 isl_space_free(space
);
4795 isl_basic_map_free(hull
);
4799 /* Is constraint "c" of the form
4801 * e(...) + c1 - m x >= 0
4805 * -e(...) + c2 + m x >= 0
4807 * where m > 1 and e only depends on parameters and input dimemnsions?
4809 * "offset" is the offset of the output dimensions
4810 * "pos" is the position of output dimension x.
4812 static int is_potential_div_constraint(isl_int
*c
, int offset
, int d
, int total
)
4814 if (isl_int_is_zero(c
[offset
+ d
]))
4816 if (isl_int_is_one(c
[offset
+ d
]))
4818 if (isl_int_is_negone(c
[offset
+ d
]))
4820 if (isl_seq_first_non_zero(c
+ offset
, d
) != -1)
4822 if (isl_seq_first_non_zero(c
+ offset
+ d
+ 1,
4823 total
- (offset
+ d
+ 1)) != -1)
4828 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4830 * As a special case, we first check if there is any pair of constraints,
4831 * shared by all the basic maps in "map" that force a given dimension
4832 * to be equal to the floor of some affine combination of the input dimensions.
4834 * In particular, if we can find two constraints
4836 * e(...) + c1 - m x >= 0 i.e., m x <= e(...) + c1
4840 * -e(...) + c2 + m x >= 0 i.e., m x >= e(...) - c2
4842 * where m > 1 and e only depends on parameters and input dimemnsions,
4845 * c1 + c2 < m i.e., -c2 >= c1 - (m - 1)
4847 * then we know that we can take
4849 * x = floor((e(...) + c1) / m)
4851 * without having to perform any computation.
4853 * Note that we know that
4857 * If c1 + c2 were 0, then we would have detected an equality during
4858 * simplification. If c1 + c2 were negative, then we would have detected
4861 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_check_div(
4862 __isl_take isl_map
*map
)
4870 isl_basic_map
*hull
;
4872 hull
= isl_map_unshifted_simple_hull(isl_map_copy(map
));
4873 dim
= isl_map_dim(map
, isl_dim_out
);
4874 total
= isl_basic_map_dim(hull
, isl_dim_all
);
4875 if (dim
< 0 || total
< 0)
4879 offset
= isl_basic_map_offset(hull
, isl_dim_out
);
4881 for (d
= 0; d
< dim
; ++d
) {
4882 for (i
= 0; i
< n
; ++i
) {
4883 if (!is_potential_div_constraint(hull
->ineq
[i
],
4884 offset
, d
, 1 + total
))
4886 for (j
= i
+ 1; j
< n
; ++j
) {
4887 if (!isl_seq_is_neg(hull
->ineq
[i
] + 1,
4888 hull
->ineq
[j
] + 1, total
))
4890 isl_int_add(sum
, hull
->ineq
[i
][0],
4892 if (isl_int_abs_lt(sum
,
4893 hull
->ineq
[i
][offset
+ d
]))
4900 if (isl_int_is_pos(hull
->ineq
[j
][offset
+ d
]))
4902 return pw_multi_aff_from_map_div(map
, hull
, d
, j
);
4906 isl_basic_map_free(hull
);
4907 return pw_multi_aff_from_map_base(map
);
4910 isl_basic_map_free(hull
);
4914 /* Given an affine expression
4916 * [A -> B] -> f(A,B)
4918 * construct an isl_multi_aff
4922 * such that dimension "d" in B' is set to "aff" and the remaining
4923 * dimensions are set equal to the corresponding dimensions in B.
4924 * "n_in" is the dimension of the space A.
4925 * "n_out" is the dimension of the space B.
4927 * If "is_set" is set, then the affine expression is of the form
4931 * and we construct an isl_multi_aff
4935 static __isl_give isl_multi_aff
*range_map(__isl_take isl_aff
*aff
, int d
,
4936 unsigned n_in
, unsigned n_out
, int is_set
)
4940 isl_space
*space
, *space2
;
4941 isl_local_space
*ls
;
4943 space
= isl_aff_get_domain_space(aff
);
4944 ls
= isl_local_space_from_space(isl_space_copy(space
));
4945 space2
= isl_space_copy(space
);
4947 space2
= isl_space_range(isl_space_unwrap(space2
));
4948 space
= isl_space_map_from_domain_and_range(space
, space2
);
4949 ma
= isl_multi_aff_alloc(space
);
4950 ma
= isl_multi_aff_set_aff(ma
, d
, aff
);
4952 for (i
= 0; i
< n_out
; ++i
) {
4955 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
4956 isl_dim_set
, n_in
+ i
);
4957 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
4960 isl_local_space_free(ls
);
4965 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
4966 * taking into account that the dimension at position "d" can be written as
4968 * x = m a + f(..) (1)
4970 * where m is equal to "gcd".
4971 * "i" is the index of the equality in "hull" that defines f(..).
4972 * In particular, the equality is of the form
4974 * f(..) - x + m g(existentials) = 0
4978 * -f(..) + x + m g(existentials) = 0
4980 * We basically plug (1) into "map", resulting in a map with "a"
4981 * in the range instead of "x". The corresponding isl_pw_multi_aff
4982 * defining "a" is then plugged back into (1) to obtain a definition for "x".
4984 * Specifically, given the input map
4988 * We first wrap it into a set
4992 * and define (1) on top of the corresponding space, resulting in "aff".
4993 * We use this to create an isl_multi_aff that maps the output position "d"
4994 * from "a" to "x", leaving all other (intput and output) dimensions unchanged.
4995 * We plug this into the wrapped map, unwrap the result and compute the
4996 * corresponding isl_pw_multi_aff.
4997 * The result is an expression
5005 * so that we can plug that into "aff", after extending the latter to
5011 * If "map" is actually a set, then there is no "A" space, meaning
5012 * that we do not need to perform any wrapping, and that the result
5013 * of the recursive call is of the form
5017 * which is plugged into a mapping of the form
5021 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_stride(
5022 __isl_take isl_map
*map
, __isl_take isl_basic_map
*hull
, int d
, int i
,
5027 isl_local_space
*ls
;
5030 isl_pw_multi_aff
*pma
, *id
;
5036 is_set
= isl_map_is_set(map
);
5040 n_in
= isl_basic_map_dim(hull
, isl_dim_in
);
5041 n_out
= isl_basic_map_dim(hull
, isl_dim_out
);
5042 if (n_in
< 0 || n_out
< 0)
5044 o_out
= isl_basic_map_offset(hull
, isl_dim_out
);
5049 set
= isl_map_wrap(map
);
5050 space
= isl_space_map_from_set(isl_set_get_space(set
));
5051 ma
= isl_multi_aff_identity(space
);
5052 ls
= isl_local_space_from_space(isl_set_get_space(set
));
5053 aff
= isl_aff_alloc(ls
);
5055 isl_int_set_si(aff
->v
->el
[0], 1);
5056 if (isl_int_is_one(hull
->eq
[i
][o_out
+ d
]))
5057 isl_seq_neg(aff
->v
->el
+ 1, hull
->eq
[i
],
5060 isl_seq_cpy(aff
->v
->el
+ 1, hull
->eq
[i
],
5062 isl_int_set(aff
->v
->el
[1 + o_out
+ d
], gcd
);
5064 ma
= isl_multi_aff_set_aff(ma
, n_in
+ d
, isl_aff_copy(aff
));
5065 set
= isl_set_preimage_multi_aff(set
, ma
);
5067 ma
= range_map(aff
, d
, n_in
, n_out
, is_set
);
5072 map
= isl_set_unwrap(set
);
5073 pma
= isl_pw_multi_aff_from_map(map
);
5076 space
= isl_pw_multi_aff_get_domain_space(pma
);
5077 space
= isl_space_map_from_set(space
);
5078 id
= isl_pw_multi_aff_identity(space
);
5079 pma
= isl_pw_multi_aff_range_product(id
, pma
);
5081 id
= isl_pw_multi_aff_from_multi_aff(ma
);
5082 pma
= isl_pw_multi_aff_pullback_pw_multi_aff(id
, pma
);
5084 isl_basic_map_free(hull
);
5088 isl_basic_map_free(hull
);
5092 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5093 * "hull" contains the equalities valid for "map".
5095 * Check if any of the output dimensions is "strided".
5096 * That is, we check if it can be written as
5100 * with m greater than 1, a some combination of existentially quantified
5101 * variables and f an expression in the parameters and input dimensions.
5102 * If so, we remove the stride in pw_multi_aff_from_map_stride.
5104 * Otherwise, we continue with pw_multi_aff_from_map_check_div for a further
5107 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_check_strides(
5108 __isl_take isl_map
*map
, __isl_take isl_basic_map
*hull
)
5117 n_div
= isl_basic_map_dim(hull
, isl_dim_div
);
5118 n_out
= isl_basic_map_dim(hull
, isl_dim_out
);
5119 if (n_div
< 0 || n_out
< 0)
5123 isl_basic_map_free(hull
);
5124 return pw_multi_aff_from_map_check_div(map
);
5129 o_div
= isl_basic_map_offset(hull
, isl_dim_div
);
5130 o_out
= isl_basic_map_offset(hull
, isl_dim_out
);
5132 for (i
= 0; i
< n_out
; ++i
) {
5133 for (j
= 0; j
< hull
->n_eq
; ++j
) {
5134 isl_int
*eq
= hull
->eq
[j
];
5135 isl_pw_multi_aff
*res
;
5137 if (!isl_int_is_one(eq
[o_out
+ i
]) &&
5138 !isl_int_is_negone(eq
[o_out
+ i
]))
5140 if (isl_seq_first_non_zero(eq
+ o_out
, i
) != -1)
5142 if (isl_seq_first_non_zero(eq
+ o_out
+ i
+ 1,
5143 n_out
- (i
+ 1)) != -1)
5145 isl_seq_gcd(eq
+ o_div
, n_div
, &gcd
);
5146 if (isl_int_is_zero(gcd
))
5148 if (isl_int_is_one(gcd
))
5151 res
= pw_multi_aff_from_map_stride(map
, hull
,
5159 isl_basic_map_free(hull
);
5160 return pw_multi_aff_from_map_check_div(map
);
5163 isl_basic_map_free(hull
);
5167 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5169 * As a special case, we first check if all output dimensions are uniquely
5170 * defined in terms of the parameters and input dimensions over the entire
5171 * domain. If so, we extract the desired isl_pw_multi_aff directly
5172 * from the affine hull of "map" and its domain.
5174 * Otherwise, continue with pw_multi_aff_from_map_check_strides for more
5177 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_map(__isl_take isl_map
*map
)
5181 isl_basic_map
*hull
;
5183 n
= isl_map_n_basic_map(map
);
5188 hull
= isl_map_unshifted_simple_hull(isl_map_copy(map
));
5189 hull
= isl_basic_map_plain_affine_hull(hull
);
5190 sv
= isl_basic_map_plain_is_single_valued(hull
);
5192 return plain_pw_multi_aff_from_map(isl_map_domain(map
),
5194 isl_basic_map_free(hull
);
5196 map
= isl_map_detect_equalities(map
);
5197 hull
= isl_map_unshifted_simple_hull(isl_map_copy(map
));
5198 sv
= isl_basic_map_plain_is_single_valued(hull
);
5200 return plain_pw_multi_aff_from_map(isl_map_domain(map
), hull
);
5202 return pw_multi_aff_from_map_check_strides(map
, hull
);
5203 isl_basic_map_free(hull
);
5209 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_set(__isl_take isl_set
*set
)
5211 return isl_pw_multi_aff_from_map(set
);
5214 /* Convert "map" into an isl_pw_multi_aff (if possible) and
5217 static isl_stat
pw_multi_aff_from_map(__isl_take isl_map
*map
, void *user
)
5219 isl_union_pw_multi_aff
**upma
= user
;
5220 isl_pw_multi_aff
*pma
;
5222 pma
= isl_pw_multi_aff_from_map(map
);
5223 *upma
= isl_union_pw_multi_aff_add_pw_multi_aff(*upma
, pma
);
5225 return *upma
? isl_stat_ok
: isl_stat_error
;
5228 /* Create an isl_union_pw_multi_aff with the given isl_aff on a universe
5231 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_aff(
5232 __isl_take isl_aff
*aff
)
5235 isl_pw_multi_aff
*pma
;
5237 ma
= isl_multi_aff_from_aff(aff
);
5238 pma
= isl_pw_multi_aff_from_multi_aff(ma
);
5239 return isl_union_pw_multi_aff_from_pw_multi_aff(pma
);
5242 /* Try and create an isl_union_pw_multi_aff that is equivalent
5243 * to the given isl_union_map.
5244 * The isl_union_map is required to be single-valued in each space.
5245 * Otherwise, an error is produced.
5247 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_union_map(
5248 __isl_take isl_union_map
*umap
)
5251 isl_union_pw_multi_aff
*upma
;
5253 space
= isl_union_map_get_space(umap
);
5254 upma
= isl_union_pw_multi_aff_empty(space
);
5255 if (isl_union_map_foreach_map(umap
, &pw_multi_aff_from_map
, &upma
) < 0)
5256 upma
= isl_union_pw_multi_aff_free(upma
);
5257 isl_union_map_free(umap
);
5262 /* Try and create an isl_union_pw_multi_aff that is equivalent
5263 * to the given isl_union_set.
5264 * The isl_union_set is required to be a singleton in each space.
5265 * Otherwise, an error is produced.
5267 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_union_set(
5268 __isl_take isl_union_set
*uset
)
5270 return isl_union_pw_multi_aff_from_union_map(uset
);
5273 /* Return the piecewise affine expression "set ? 1 : 0".
5275 __isl_give isl_pw_aff
*isl_set_indicator_function(__isl_take isl_set
*set
)
5278 isl_space
*space
= isl_set_get_space(set
);
5279 isl_local_space
*ls
= isl_local_space_from_space(space
);
5280 isl_aff
*zero
= isl_aff_zero_on_domain(isl_local_space_copy(ls
));
5281 isl_aff
*one
= isl_aff_zero_on_domain(ls
);
5283 one
= isl_aff_add_constant_si(one
, 1);
5284 pa
= isl_pw_aff_alloc(isl_set_copy(set
), one
);
5285 set
= isl_set_complement(set
);
5286 pa
= isl_pw_aff_add_disjoint(pa
, isl_pw_aff_alloc(set
, zero
));
5291 /* Plug in "subs" for dimension "type", "pos" of "aff".
5293 * Let i be the dimension to replace and let "subs" be of the form
5297 * and "aff" of the form
5303 * (a f + d g')/(m d)
5305 * where g' is the result of plugging in "subs" in each of the integer
5308 __isl_give isl_aff
*isl_aff_substitute(__isl_take isl_aff
*aff
,
5309 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
5315 aff
= isl_aff_cow(aff
);
5317 return isl_aff_free(aff
);
5319 ctx
= isl_aff_get_ctx(aff
);
5320 if (!isl_space_is_equal(aff
->ls
->dim
, subs
->ls
->dim
))
5321 isl_die(ctx
, isl_error_invalid
,
5322 "spaces don't match", return isl_aff_free(aff
));
5323 n_div
= isl_local_space_dim(subs
->ls
, isl_dim_div
);
5325 return isl_aff_free(aff
);
5327 isl_die(ctx
, isl_error_unsupported
,
5328 "cannot handle divs yet", return isl_aff_free(aff
));
5330 aff
->ls
= isl_local_space_substitute(aff
->ls
, type
, pos
, subs
);
5332 return isl_aff_free(aff
);
5334 aff
->v
= isl_vec_cow(aff
->v
);
5336 return isl_aff_free(aff
);
5338 pos
+= isl_local_space_offset(aff
->ls
, type
);
5341 isl_seq_substitute(aff
->v
->el
, pos
, subs
->v
->el
,
5342 aff
->v
->size
, subs
->v
->size
, v
);
5348 /* Plug in "subs" for dimension "type", "pos" in each of the affine
5349 * expressions in "maff".
5351 __isl_give isl_multi_aff
*isl_multi_aff_substitute(
5352 __isl_take isl_multi_aff
*maff
, enum isl_dim_type type
, unsigned pos
,
5353 __isl_keep isl_aff
*subs
)
5357 maff
= isl_multi_aff_cow(maff
);
5359 return isl_multi_aff_free(maff
);
5361 if (type
== isl_dim_in
)
5364 for (i
= 0; i
< maff
->n
; ++i
) {
5365 maff
->u
.p
[i
] = isl_aff_substitute(maff
->u
.p
[i
],
5368 return isl_multi_aff_free(maff
);
5374 /* Plug in "subs" for dimension "type", "pos" of "pma".
5376 * pma is of the form
5380 * while subs is of the form
5382 * v' = B_j(v) -> S_j
5384 * Each pair i,j such that C_ij = A_i \cap B_i is non-empty
5385 * has a contribution in the result, in particular
5387 * C_ij(S_j) -> M_i(S_j)
5389 * Note that plugging in S_j in C_ij may also result in an empty set
5390 * and this contribution should simply be discarded.
5392 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_substitute(
5393 __isl_take isl_pw_multi_aff
*pma
, enum isl_dim_type type
, unsigned pos
,
5394 __isl_keep isl_pw_aff
*subs
)
5397 isl_pw_multi_aff
*res
;
5400 return isl_pw_multi_aff_free(pma
);
5402 n
= pma
->n
* subs
->n
;
5403 res
= isl_pw_multi_aff_alloc_size(isl_space_copy(pma
->dim
), n
);
5405 for (i
= 0; i
< pma
->n
; ++i
) {
5406 for (j
= 0; j
< subs
->n
; ++j
) {
5408 isl_multi_aff
*res_ij
;
5411 common
= isl_set_intersect(
5412 isl_set_copy(pma
->p
[i
].set
),
5413 isl_set_copy(subs
->p
[j
].set
));
5414 common
= isl_set_substitute(common
,
5415 type
, pos
, subs
->p
[j
].aff
);
5416 empty
= isl_set_plain_is_empty(common
);
5417 if (empty
< 0 || empty
) {
5418 isl_set_free(common
);
5424 res_ij
= isl_multi_aff_substitute(
5425 isl_multi_aff_copy(pma
->p
[i
].maff
),
5426 type
, pos
, subs
->p
[j
].aff
);
5428 res
= isl_pw_multi_aff_add_piece(res
, common
, res_ij
);
5432 isl_pw_multi_aff_free(pma
);
5435 isl_pw_multi_aff_free(pma
);
5436 isl_pw_multi_aff_free(res
);
5440 /* Compute the preimage of a range of dimensions in the affine expression "src"
5441 * under "ma" and put the result in "dst". The number of dimensions in "src"
5442 * that precede the range is given by "n_before". The number of dimensions
5443 * in the range is given by the number of output dimensions of "ma".
5444 * The number of dimensions that follow the range is given by "n_after".
5445 * If "has_denom" is set (to one),
5446 * then "src" and "dst" have an extra initial denominator.
5447 * "n_div_ma" is the number of existentials in "ma"
5448 * "n_div_bset" is the number of existentials in "src"
5449 * The resulting "dst" (which is assumed to have been allocated by
5450 * the caller) contains coefficients for both sets of existentials,
5451 * first those in "ma" and then those in "src".
5452 * f, c1, c2 and g are temporary objects that have been initialized
5455 * Let src represent the expression
5457 * (a(p) + f_u u + b v + f_w w + c(divs))/d
5459 * and let ma represent the expressions
5461 * v_i = (r_i(p) + s_i(y) + t_i(divs'))/m_i
5463 * We start out with the following expression for dst:
5465 * (a(p) + f_u u + 0 y + f_w w + 0 divs' + c(divs) + f \sum_i b_i v_i)/d
5467 * with the multiplication factor f initially equal to 1
5468 * and f \sum_i b_i v_i kept separately.
5469 * For each x_i that we substitute, we multiply the numerator
5470 * (and denominator) of dst by c_1 = m_i and add the numerator
5471 * of the x_i expression multiplied by c_2 = f b_i,
5472 * after removing the common factors of c_1 and c_2.
5473 * The multiplication factor f also needs to be multiplied by c_1
5474 * for the next x_j, j > i.
5476 isl_stat
isl_seq_preimage(isl_int
*dst
, isl_int
*src
,
5477 __isl_keep isl_multi_aff
*ma
, int n_before
, int n_after
,
5478 int n_div_ma
, int n_div_bmap
,
5479 isl_int f
, isl_int c1
, isl_int c2
, isl_int g
, int has_denom
)
5482 isl_size n_param
, n_in
, n_out
;
5485 n_param
= isl_multi_aff_dim(ma
, isl_dim_param
);
5486 n_in
= isl_multi_aff_dim(ma
, isl_dim_in
);
5487 n_out
= isl_multi_aff_dim(ma
, isl_dim_out
);
5488 if (n_param
< 0 || n_in
< 0 || n_out
< 0)
5489 return isl_stat_error
;
5491 isl_seq_cpy(dst
, src
, has_denom
+ 1 + n_param
+ n_before
);
5492 o_dst
= o_src
= has_denom
+ 1 + n_param
+ n_before
;
5493 isl_seq_clr(dst
+ o_dst
, n_in
);
5496 isl_seq_cpy(dst
+ o_dst
, src
+ o_src
, n_after
);
5499 isl_seq_clr(dst
+ o_dst
, n_div_ma
);
5501 isl_seq_cpy(dst
+ o_dst
, src
+ o_src
, n_div_bmap
);
5503 isl_int_set_si(f
, 1);
5505 for (i
= 0; i
< n_out
; ++i
) {
5506 int offset
= has_denom
+ 1 + n_param
+ n_before
+ i
;
5508 if (isl_int_is_zero(src
[offset
]))
5510 isl_int_set(c1
, ma
->u
.p
[i
]->v
->el
[0]);
5511 isl_int_mul(c2
, f
, src
[offset
]);
5512 isl_int_gcd(g
, c1
, c2
);
5513 isl_int_divexact(c1
, c1
, g
);
5514 isl_int_divexact(c2
, c2
, g
);
5516 isl_int_mul(f
, f
, c1
);
5519 isl_seq_combine(dst
+ o_dst
, c1
, dst
+ o_dst
,
5520 c2
, ma
->u
.p
[i
]->v
->el
+ o_src
, 1 + n_param
);
5521 o_dst
+= 1 + n_param
;
5522 o_src
+= 1 + n_param
;
5523 isl_seq_scale(dst
+ o_dst
, dst
+ o_dst
, c1
, n_before
);
5525 isl_seq_combine(dst
+ o_dst
, c1
, dst
+ o_dst
,
5526 c2
, ma
->u
.p
[i
]->v
->el
+ o_src
, n_in
);
5529 isl_seq_scale(dst
+ o_dst
, dst
+ o_dst
, c1
, n_after
);
5531 isl_seq_combine(dst
+ o_dst
, c1
, dst
+ o_dst
,
5532 c2
, ma
->u
.p
[i
]->v
->el
+ o_src
, n_div_ma
);
5535 isl_seq_scale(dst
+ o_dst
, dst
+ o_dst
, c1
, n_div_bmap
);
5537 isl_int_mul(dst
[0], dst
[0], c1
);
5543 /* Compute the pullback of "aff" by the function represented by "ma".
5544 * In other words, plug in "ma" in "aff". The result is an affine expression
5545 * defined over the domain space of "ma".
5547 * If "aff" is represented by
5549 * (a(p) + b x + c(divs))/d
5551 * and ma is represented by
5553 * x = D(p) + F(y) + G(divs')
5555 * then the result is
5557 * (a(p) + b D(p) + b F(y) + b G(divs') + c(divs))/d
5559 * The divs in the local space of the input are similarly adjusted
5560 * through a call to isl_local_space_preimage_multi_aff.
5562 __isl_give isl_aff
*isl_aff_pullback_multi_aff(__isl_take isl_aff
*aff
,
5563 __isl_take isl_multi_aff
*ma
)
5565 isl_aff
*res
= NULL
;
5566 isl_local_space
*ls
;
5567 isl_size n_div_aff
, n_div_ma
;
5568 isl_int f
, c1
, c2
, g
;
5570 ma
= isl_multi_aff_align_divs(ma
);
5574 n_div_aff
= isl_aff_dim(aff
, isl_dim_div
);
5575 n_div_ma
= ma
->n
? isl_aff_dim(ma
->u
.p
[0], isl_dim_div
) : 0;
5576 if (n_div_aff
< 0 || n_div_ma
< 0)
5579 ls
= isl_aff_get_domain_local_space(aff
);
5580 ls
= isl_local_space_preimage_multi_aff(ls
, isl_multi_aff_copy(ma
));
5581 res
= isl_aff_alloc(ls
);
5590 if (isl_seq_preimage(res
->v
->el
, aff
->v
->el
, ma
, 0, 0,
5591 n_div_ma
, n_div_aff
, f
, c1
, c2
, g
, 1) < 0)
5592 res
= isl_aff_free(res
);
5600 isl_multi_aff_free(ma
);
5601 res
= isl_aff_normalize(res
);
5605 isl_multi_aff_free(ma
);
5610 /* Compute the pullback of "aff1" by the function represented by "aff2".
5611 * In other words, plug in "aff2" in "aff1". The result is an affine expression
5612 * defined over the domain space of "aff1".
5614 * The domain of "aff1" should match the range of "aff2", which means
5615 * that it should be single-dimensional.
5617 __isl_give isl_aff
*isl_aff_pullback_aff(__isl_take isl_aff
*aff1
,
5618 __isl_take isl_aff
*aff2
)
5622 ma
= isl_multi_aff_from_aff(aff2
);
5623 return isl_aff_pullback_multi_aff(aff1
, ma
);
5626 /* Compute the pullback of "ma1" by the function represented by "ma2".
5627 * In other words, plug in "ma2" in "ma1".
5629 * The parameters of "ma1" and "ma2" are assumed to have been aligned.
5631 static __isl_give isl_multi_aff
*isl_multi_aff_pullback_multi_aff_aligned(
5632 __isl_take isl_multi_aff
*ma1
, __isl_take isl_multi_aff
*ma2
)
5635 isl_space
*space
= NULL
;
5637 ma2
= isl_multi_aff_align_divs(ma2
);
5638 ma1
= isl_multi_aff_cow(ma1
);
5642 space
= isl_space_join(isl_multi_aff_get_space(ma2
),
5643 isl_multi_aff_get_space(ma1
));
5645 for (i
= 0; i
< ma1
->n
; ++i
) {
5646 ma1
->u
.p
[i
] = isl_aff_pullback_multi_aff(ma1
->u
.p
[i
],
5647 isl_multi_aff_copy(ma2
));
5652 ma1
= isl_multi_aff_reset_space(ma1
, space
);
5653 isl_multi_aff_free(ma2
);
5656 isl_space_free(space
);
5657 isl_multi_aff_free(ma2
);
5658 isl_multi_aff_free(ma1
);
5662 /* Compute the pullback of "ma1" by the function represented by "ma2".
5663 * In other words, plug in "ma2" in "ma1".
5665 __isl_give isl_multi_aff
*isl_multi_aff_pullback_multi_aff(
5666 __isl_take isl_multi_aff
*ma1
, __isl_take isl_multi_aff
*ma2
)
5668 return isl_multi_aff_align_params_multi_multi_and(ma1
, ma2
,
5669 &isl_multi_aff_pullback_multi_aff_aligned
);
5672 /* Extend the local space of "dst" to include the divs
5673 * in the local space of "src".
5675 * If "src" does not have any divs or if the local spaces of "dst" and
5676 * "src" are the same, then no extension is required.
5678 __isl_give isl_aff
*isl_aff_align_divs(__isl_take isl_aff
*dst
,
5679 __isl_keep isl_aff
*src
)
5682 isl_size src_n_div
, dst_n_div
;
5689 return isl_aff_free(dst
);
5691 ctx
= isl_aff_get_ctx(src
);
5692 equal
= isl_local_space_has_equal_space(src
->ls
, dst
->ls
);
5694 return isl_aff_free(dst
);
5696 isl_die(ctx
, isl_error_invalid
,
5697 "spaces don't match", goto error
);
5699 src_n_div
= isl_local_space_dim(src
->ls
, isl_dim_div
);
5700 dst_n_div
= isl_local_space_dim(dst
->ls
, isl_dim_div
);
5703 equal
= isl_local_space_is_equal(src
->ls
, dst
->ls
);
5704 if (equal
< 0 || src_n_div
< 0 || dst_n_div
< 0)
5705 return isl_aff_free(dst
);
5709 exp1
= isl_alloc_array(ctx
, int, src_n_div
);
5710 exp2
= isl_alloc_array(ctx
, int, dst_n_div
);
5711 if (!exp1
|| (dst_n_div
&& !exp2
))
5714 div
= isl_merge_divs(src
->ls
->div
, dst
->ls
->div
, exp1
, exp2
);
5715 dst
= isl_aff_expand_divs(dst
, div
, exp2
);
5723 return isl_aff_free(dst
);
5726 /* Adjust the local spaces of the affine expressions in "maff"
5727 * such that they all have the save divs.
5729 __isl_give isl_multi_aff
*isl_multi_aff_align_divs(
5730 __isl_take isl_multi_aff
*maff
)
5738 maff
= isl_multi_aff_cow(maff
);
5742 for (i
= 1; i
< maff
->n
; ++i
)
5743 maff
->u
.p
[0] = isl_aff_align_divs(maff
->u
.p
[0], maff
->u
.p
[i
]);
5744 for (i
= 1; i
< maff
->n
; ++i
) {
5745 maff
->u
.p
[i
] = isl_aff_align_divs(maff
->u
.p
[i
], maff
->u
.p
[0]);
5747 return isl_multi_aff_free(maff
);
5753 __isl_give isl_aff
*isl_aff_lift(__isl_take isl_aff
*aff
)
5755 aff
= isl_aff_cow(aff
);
5759 aff
->ls
= isl_local_space_lift(aff
->ls
);
5761 return isl_aff_free(aff
);
5766 /* Lift "maff" to a space with extra dimensions such that the result
5767 * has no more existentially quantified variables.
5768 * If "ls" is not NULL, then *ls is assigned the local space that lies
5769 * at the basis of the lifting applied to "maff".
5771 __isl_give isl_multi_aff
*isl_multi_aff_lift(__isl_take isl_multi_aff
*maff
,
5772 __isl_give isl_local_space
**ls
)
5786 isl_space
*space
= isl_multi_aff_get_domain_space(maff
);
5787 *ls
= isl_local_space_from_space(space
);
5789 return isl_multi_aff_free(maff
);
5794 maff
= isl_multi_aff_cow(maff
);
5795 maff
= isl_multi_aff_align_divs(maff
);
5799 n_div
= isl_aff_dim(maff
->u
.p
[0], isl_dim_div
);
5801 return isl_multi_aff_free(maff
);
5802 space
= isl_multi_aff_get_space(maff
);
5803 space
= isl_space_lift(isl_space_domain(space
), n_div
);
5804 space
= isl_space_extend_domain_with_range(space
,
5805 isl_multi_aff_get_space(maff
));
5807 return isl_multi_aff_free(maff
);
5808 isl_space_free(maff
->space
);
5809 maff
->space
= space
;
5812 *ls
= isl_aff_get_domain_local_space(maff
->u
.p
[0]);
5814 return isl_multi_aff_free(maff
);
5817 for (i
= 0; i
< maff
->n
; ++i
) {
5818 maff
->u
.p
[i
] = isl_aff_lift(maff
->u
.p
[i
]);
5826 isl_local_space_free(*ls
);
5827 return isl_multi_aff_free(maff
);
5831 #define TYPE isl_pw_multi_aff
5833 #include "check_type_range_templ.c"
5835 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma".
5837 __isl_give isl_pw_aff
*isl_pw_multi_aff_get_pw_aff(
5838 __isl_keep isl_pw_multi_aff
*pma
, int pos
)
5845 if (isl_pw_multi_aff_check_range(pma
, isl_dim_out
, pos
, 1) < 0)
5848 n_out
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
5852 space
= isl_pw_multi_aff_get_space(pma
);
5853 space
= isl_space_drop_dims(space
, isl_dim_out
,
5854 pos
+ 1, n_out
- pos
- 1);
5855 space
= isl_space_drop_dims(space
, isl_dim_out
, 0, pos
);
5857 pa
= isl_pw_aff_alloc_size(space
, pma
->n
);
5858 for (i
= 0; i
< pma
->n
; ++i
) {
5860 aff
= isl_multi_aff_get_aff(pma
->p
[i
].maff
, pos
);
5861 pa
= isl_pw_aff_add_piece(pa
, isl_set_copy(pma
->p
[i
].set
), aff
);
5867 /* Return an isl_pw_multi_aff with the given "set" as domain and
5868 * an unnamed zero-dimensional range.
5870 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_domain(
5871 __isl_take isl_set
*set
)
5876 space
= isl_set_get_space(set
);
5877 space
= isl_space_from_domain(space
);
5878 ma
= isl_multi_aff_zero(space
);
5879 return isl_pw_multi_aff_alloc(set
, ma
);
5882 /* Add an isl_pw_multi_aff with the given "set" as domain and
5883 * an unnamed zero-dimensional range to *user.
5885 static isl_stat
add_pw_multi_aff_from_domain(__isl_take isl_set
*set
,
5888 isl_union_pw_multi_aff
**upma
= user
;
5889 isl_pw_multi_aff
*pma
;
5891 pma
= isl_pw_multi_aff_from_domain(set
);
5892 *upma
= isl_union_pw_multi_aff_add_pw_multi_aff(*upma
, pma
);
5897 /* Return an isl_union_pw_multi_aff with the given "uset" as domain and
5898 * an unnamed zero-dimensional range.
5900 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_domain(
5901 __isl_take isl_union_set
*uset
)
5904 isl_union_pw_multi_aff
*upma
;
5909 space
= isl_union_set_get_space(uset
);
5910 upma
= isl_union_pw_multi_aff_empty(space
);
5912 if (isl_union_set_foreach_set(uset
,
5913 &add_pw_multi_aff_from_domain
, &upma
) < 0)
5916 isl_union_set_free(uset
);
5919 isl_union_set_free(uset
);
5920 isl_union_pw_multi_aff_free(upma
);
5924 /* Local data for bin_entry and the callback "fn".
5926 struct isl_union_pw_multi_aff_bin_data
{
5927 isl_union_pw_multi_aff
*upma2
;
5928 isl_union_pw_multi_aff
*res
;
5929 isl_pw_multi_aff
*pma
;
5930 isl_stat (*fn
)(__isl_take isl_pw_multi_aff
*pma
, void *user
);
5933 /* Given an isl_pw_multi_aff from upma1, store it in data->pma
5934 * and call data->fn for each isl_pw_multi_aff in data->upma2.
5936 static isl_stat
bin_entry(__isl_take isl_pw_multi_aff
*pma
, void *user
)
5938 struct isl_union_pw_multi_aff_bin_data
*data
= user
;
5942 r
= isl_union_pw_multi_aff_foreach_pw_multi_aff(data
->upma2
,
5944 isl_pw_multi_aff_free(pma
);
5949 /* Call "fn" on each pair of isl_pw_multi_affs in "upma1" and "upma2".
5950 * The isl_pw_multi_aff from upma1 is stored in data->pma (where data is
5951 * passed as user field) and the isl_pw_multi_aff from upma2 is available
5952 * as *entry. The callback should adjust data->res if desired.
5954 static __isl_give isl_union_pw_multi_aff
*bin_op(
5955 __isl_take isl_union_pw_multi_aff
*upma1
,
5956 __isl_take isl_union_pw_multi_aff
*upma2
,
5957 isl_stat (*fn
)(__isl_take isl_pw_multi_aff
*pma
, void *user
))
5960 struct isl_union_pw_multi_aff_bin_data data
= { NULL
, NULL
, NULL
, fn
};
5962 space
= isl_union_pw_multi_aff_get_space(upma2
);
5963 upma1
= isl_union_pw_multi_aff_align_params(upma1
, space
);
5964 space
= isl_union_pw_multi_aff_get_space(upma1
);
5965 upma2
= isl_union_pw_multi_aff_align_params(upma2
, space
);
5967 if (!upma1
|| !upma2
)
5971 data
.res
= isl_union_pw_multi_aff_alloc_same_size(upma1
);
5972 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma1
,
5973 &bin_entry
, &data
) < 0)
5976 isl_union_pw_multi_aff_free(upma1
);
5977 isl_union_pw_multi_aff_free(upma2
);
5980 isl_union_pw_multi_aff_free(upma1
);
5981 isl_union_pw_multi_aff_free(upma2
);
5982 isl_union_pw_multi_aff_free(data
.res
);
5986 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
5987 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
5989 static __isl_give isl_pw_multi_aff
*pw_multi_aff_range_product(
5990 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
5994 space
= isl_space_range_product(isl_pw_multi_aff_get_space(pma1
),
5995 isl_pw_multi_aff_get_space(pma2
));
5996 return isl_pw_multi_aff_on_shared_domain_in(pma1
, pma2
, space
,
5997 &isl_multi_aff_range_product
);
6000 /* Given two isl_pw_multi_affs A -> B and C -> D,
6001 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
6003 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_range_product(
6004 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
6006 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
6007 &pw_multi_aff_range_product
);
6010 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
6011 * construct an isl_pw_multi_aff (A * C) -> (B, D).
6013 static __isl_give isl_pw_multi_aff
*pw_multi_aff_flat_range_product(
6014 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
6018 space
= isl_space_range_product(isl_pw_multi_aff_get_space(pma1
),
6019 isl_pw_multi_aff_get_space(pma2
));
6020 space
= isl_space_flatten_range(space
);
6021 return isl_pw_multi_aff_on_shared_domain_in(pma1
, pma2
, space
,
6022 &isl_multi_aff_flat_range_product
);
6025 /* Given two isl_pw_multi_affs A -> B and C -> D,
6026 * construct an isl_pw_multi_aff (A * C) -> (B, D).
6028 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_flat_range_product(
6029 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
6031 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
6032 &pw_multi_aff_flat_range_product
);
6035 /* If data->pma and "pma2" have the same domain space, then compute
6036 * their flat range product and the result to data->res.
6038 static isl_stat
flat_range_product_entry(__isl_take isl_pw_multi_aff
*pma2
,
6041 struct isl_union_pw_multi_aff_bin_data
*data
= user
;
6043 if (!isl_space_tuple_is_equal(data
->pma
->dim
, isl_dim_in
,
6044 pma2
->dim
, isl_dim_in
)) {
6045 isl_pw_multi_aff_free(pma2
);
6049 pma2
= isl_pw_multi_aff_flat_range_product(
6050 isl_pw_multi_aff_copy(data
->pma
), pma2
);
6052 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma2
);
6057 /* Given two isl_union_pw_multi_affs A -> B and C -> D,
6058 * construct an isl_union_pw_multi_aff (A * C) -> (B, D).
6060 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_flat_range_product(
6061 __isl_take isl_union_pw_multi_aff
*upma1
,
6062 __isl_take isl_union_pw_multi_aff
*upma2
)
6064 return bin_op(upma1
, upma2
, &flat_range_product_entry
);
6067 /* Replace the affine expressions at position "pos" in "pma" by "pa".
6068 * The parameters are assumed to have been aligned.
6070 * The implementation essentially performs an isl_pw_*_on_shared_domain,
6071 * except that it works on two different isl_pw_* types.
6073 static __isl_give isl_pw_multi_aff
*pw_multi_aff_set_pw_aff(
6074 __isl_take isl_pw_multi_aff
*pma
, unsigned pos
,
6075 __isl_take isl_pw_aff
*pa
)
6078 isl_pw_multi_aff
*res
= NULL
;
6083 if (!isl_space_tuple_is_equal(pma
->dim
, isl_dim_in
,
6084 pa
->dim
, isl_dim_in
))
6085 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
6086 "domains don't match", goto error
);
6087 if (isl_pw_multi_aff_check_range(pma
, isl_dim_out
, pos
, 1) < 0)
6091 res
= isl_pw_multi_aff_alloc_size(isl_pw_multi_aff_get_space(pma
), n
);
6093 for (i
= 0; i
< pma
->n
; ++i
) {
6094 for (j
= 0; j
< pa
->n
; ++j
) {
6096 isl_multi_aff
*res_ij
;
6099 common
= isl_set_intersect(isl_set_copy(pma
->p
[i
].set
),
6100 isl_set_copy(pa
->p
[j
].set
));
6101 empty
= isl_set_plain_is_empty(common
);
6102 if (empty
< 0 || empty
) {
6103 isl_set_free(common
);
6109 res_ij
= isl_multi_aff_set_aff(
6110 isl_multi_aff_copy(pma
->p
[i
].maff
), pos
,
6111 isl_aff_copy(pa
->p
[j
].aff
));
6112 res_ij
= isl_multi_aff_gist(res_ij
,
6113 isl_set_copy(common
));
6115 res
= isl_pw_multi_aff_add_piece(res
, common
, res_ij
);
6119 isl_pw_multi_aff_free(pma
);
6120 isl_pw_aff_free(pa
);
6123 isl_pw_multi_aff_free(pma
);
6124 isl_pw_aff_free(pa
);
6125 return isl_pw_multi_aff_free(res
);
6128 /* Replace the affine expressions at position "pos" in "pma" by "pa".
6130 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_set_pw_aff(
6131 __isl_take isl_pw_multi_aff
*pma
, unsigned pos
,
6132 __isl_take isl_pw_aff
*pa
)
6134 isl_bool equal_params
;
6138 equal_params
= isl_space_has_equal_params(pma
->dim
, pa
->dim
);
6139 if (equal_params
< 0)
6142 return pw_multi_aff_set_pw_aff(pma
, pos
, pa
);
6143 if (isl_pw_multi_aff_check_named_params(pma
) < 0 ||
6144 isl_pw_aff_check_named_params(pa
) < 0)
6146 pma
= isl_pw_multi_aff_align_params(pma
, isl_pw_aff_get_space(pa
));
6147 pa
= isl_pw_aff_align_params(pa
, isl_pw_multi_aff_get_space(pma
));
6148 return pw_multi_aff_set_pw_aff(pma
, pos
, pa
);
6150 isl_pw_multi_aff_free(pma
);
6151 isl_pw_aff_free(pa
);
6155 /* Do the parameters of "pa" match those of "space"?
6157 isl_bool
isl_pw_aff_matching_params(__isl_keep isl_pw_aff
*pa
,
6158 __isl_keep isl_space
*space
)
6160 isl_space
*pa_space
;
6164 return isl_bool_error
;
6166 pa_space
= isl_pw_aff_get_space(pa
);
6168 match
= isl_space_has_equal_params(space
, pa_space
);
6170 isl_space_free(pa_space
);
6174 /* Check that the domain space of "pa" matches "space".
6176 isl_stat
isl_pw_aff_check_match_domain_space(__isl_keep isl_pw_aff
*pa
,
6177 __isl_keep isl_space
*space
)
6179 isl_space
*pa_space
;
6183 return isl_stat_error
;
6185 pa_space
= isl_pw_aff_get_space(pa
);
6187 match
= isl_space_has_equal_params(space
, pa_space
);
6191 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
6192 "parameters don't match", goto error
);
6193 match
= isl_space_tuple_is_equal(space
, isl_dim_in
,
6194 pa_space
, isl_dim_in
);
6198 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
6199 "domains don't match", goto error
);
6200 isl_space_free(pa_space
);
6203 isl_space_free(pa_space
);
6204 return isl_stat_error
;
6212 #include <isl_multi_explicit_domain.c>
6213 #include <isl_multi_pw_aff_explicit_domain.c>
6214 #include <isl_multi_templ.c>
6215 #include <isl_multi_apply_set.c>
6216 #include <isl_multi_coalesce.c>
6217 #include <isl_multi_domain_templ.c>
6218 #include <isl_multi_dims.c>
6219 #include <isl_multi_from_base_templ.c>
6220 #include <isl_multi_gist.c>
6221 #include <isl_multi_hash.c>
6222 #include <isl_multi_identity_templ.c>
6223 #include <isl_multi_align_set.c>
6224 #include <isl_multi_intersect.c>
6225 #include <isl_multi_move_dims_templ.c>
6226 #include <isl_multi_product_templ.c>
6227 #include <isl_multi_splice_templ.c>
6228 #include <isl_multi_zero_templ.c>
6230 /* Does "mpa" have a non-trivial explicit domain?
6232 * The explicit domain, if present, is trivial if it represents
6233 * an (obviously) universe set.
6235 isl_bool
isl_multi_pw_aff_has_non_trivial_domain(
6236 __isl_keep isl_multi_pw_aff
*mpa
)
6239 return isl_bool_error
;
6240 if (!isl_multi_pw_aff_has_explicit_domain(mpa
))
6241 return isl_bool_false
;
6242 return isl_bool_not(isl_set_plain_is_universe(mpa
->u
.dom
));
6245 /* Scale the elements of "pma" by the corresponding elements of "mv".
6247 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_scale_multi_val(
6248 __isl_take isl_pw_multi_aff
*pma
, __isl_take isl_multi_val
*mv
)
6251 isl_bool equal_params
;
6253 pma
= isl_pw_multi_aff_cow(pma
);
6256 if (!isl_space_tuple_is_equal(pma
->dim
, isl_dim_out
,
6257 mv
->space
, isl_dim_set
))
6258 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
6259 "spaces don't match", goto error
);
6260 equal_params
= isl_space_has_equal_params(pma
->dim
, mv
->space
);
6261 if (equal_params
< 0)
6263 if (!equal_params
) {
6264 pma
= isl_pw_multi_aff_align_params(pma
,
6265 isl_multi_val_get_space(mv
));
6266 mv
= isl_multi_val_align_params(mv
,
6267 isl_pw_multi_aff_get_space(pma
));
6272 for (i
= 0; i
< pma
->n
; ++i
) {
6273 pma
->p
[i
].maff
= isl_multi_aff_scale_multi_val(pma
->p
[i
].maff
,
6274 isl_multi_val_copy(mv
));
6275 if (!pma
->p
[i
].maff
)
6279 isl_multi_val_free(mv
);
6282 isl_multi_val_free(mv
);
6283 isl_pw_multi_aff_free(pma
);
6287 /* This function is called for each entry of an isl_union_pw_multi_aff.
6288 * If the space of the entry matches that of data->mv,
6289 * then apply isl_pw_multi_aff_scale_multi_val and return the result.
6290 * Otherwise, return an empty isl_pw_multi_aff.
6292 static __isl_give isl_pw_multi_aff
*union_pw_multi_aff_scale_multi_val_entry(
6293 __isl_take isl_pw_multi_aff
*pma
, void *user
)
6295 isl_multi_val
*mv
= user
;
6299 if (!isl_space_tuple_is_equal(pma
->dim
, isl_dim_out
,
6300 mv
->space
, isl_dim_set
)) {
6301 isl_space
*space
= isl_pw_multi_aff_get_space(pma
);
6302 isl_pw_multi_aff_free(pma
);
6303 return isl_pw_multi_aff_empty(space
);
6306 return isl_pw_multi_aff_scale_multi_val(pma
, isl_multi_val_copy(mv
));
6309 /* Scale the elements of "upma" by the corresponding elements of "mv",
6310 * for those entries that match the space of "mv".
6312 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_scale_multi_val(
6313 __isl_take isl_union_pw_multi_aff
*upma
, __isl_take isl_multi_val
*mv
)
6315 upma
= isl_union_pw_multi_aff_align_params(upma
,
6316 isl_multi_val_get_space(mv
));
6317 mv
= isl_multi_val_align_params(mv
,
6318 isl_union_pw_multi_aff_get_space(upma
));
6322 return isl_union_pw_multi_aff_transform(upma
,
6323 &union_pw_multi_aff_scale_multi_val_entry
, mv
);
6325 isl_multi_val_free(mv
);
6328 isl_multi_val_free(mv
);
6329 isl_union_pw_multi_aff_free(upma
);
6333 /* Construct and return a piecewise multi affine expression
6334 * in the given space with value zero in each of the output dimensions and
6335 * a universe domain.
6337 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_zero(__isl_take isl_space
*space
)
6339 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_zero(space
));
6342 /* Construct and return a piecewise multi affine expression
6343 * that is equal to the given piecewise affine expression.
6345 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_pw_aff(
6346 __isl_take isl_pw_aff
*pa
)
6350 isl_pw_multi_aff
*pma
;
6355 space
= isl_pw_aff_get_space(pa
);
6356 pma
= isl_pw_multi_aff_alloc_size(space
, pa
->n
);
6358 for (i
= 0; i
< pa
->n
; ++i
) {
6362 set
= isl_set_copy(pa
->p
[i
].set
);
6363 ma
= isl_multi_aff_from_aff(isl_aff_copy(pa
->p
[i
].aff
));
6364 pma
= isl_pw_multi_aff_add_piece(pma
, set
, ma
);
6367 isl_pw_aff_free(pa
);
6371 /* Construct and return a piecewise multi affine expression
6372 * that is equal to the given multi piecewise affine expression
6373 * on the shared domain of the piecewise affine expressions,
6374 * in the special case of a 0D multi piecewise affine expression.
6376 * Create a piecewise multi affine expression with the explicit domain of
6377 * the 0D multi piecewise affine expression as domain.
6379 static __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_multi_pw_aff_0D(
6380 __isl_take isl_multi_pw_aff
*mpa
)
6386 space
= isl_multi_pw_aff_get_space(mpa
);
6387 dom
= isl_multi_pw_aff_get_explicit_domain(mpa
);
6388 isl_multi_pw_aff_free(mpa
);
6390 ma
= isl_multi_aff_zero(space
);
6391 return isl_pw_multi_aff_alloc(dom
, ma
);
6394 /* Construct and return a piecewise multi affine expression
6395 * that is equal to the given multi piecewise affine expression
6396 * on the shared domain of the piecewise affine expressions.
6398 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_multi_pw_aff(
6399 __isl_take isl_multi_pw_aff
*mpa
)
6404 isl_pw_multi_aff
*pma
;
6410 return isl_pw_multi_aff_from_multi_pw_aff_0D(mpa
);
6412 space
= isl_multi_pw_aff_get_space(mpa
);
6413 pa
= isl_multi_pw_aff_get_pw_aff(mpa
, 0);
6414 pma
= isl_pw_multi_aff_from_pw_aff(pa
);
6416 for (i
= 1; i
< mpa
->n
; ++i
) {
6417 isl_pw_multi_aff
*pma_i
;
6419 pa
= isl_multi_pw_aff_get_pw_aff(mpa
, i
);
6420 pma_i
= isl_pw_multi_aff_from_pw_aff(pa
);
6421 pma
= isl_pw_multi_aff_range_product(pma
, pma_i
);
6424 pma
= isl_pw_multi_aff_reset_space(pma
, space
);
6426 isl_multi_pw_aff_free(mpa
);
6430 /* Construct and return a multi piecewise affine expression
6431 * that is equal to the given multi affine expression.
6433 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_from_multi_aff(
6434 __isl_take isl_multi_aff
*ma
)
6438 isl_multi_pw_aff
*mpa
;
6440 n
= isl_multi_aff_dim(ma
, isl_dim_out
);
6442 ma
= isl_multi_aff_free(ma
);
6446 mpa
= isl_multi_pw_aff_alloc(isl_multi_aff_get_space(ma
));
6448 for (i
= 0; i
< n
; ++i
) {
6451 pa
= isl_pw_aff_from_aff(isl_multi_aff_get_aff(ma
, i
));
6452 mpa
= isl_multi_pw_aff_set_pw_aff(mpa
, i
, pa
);
6455 isl_multi_aff_free(ma
);
6459 /* Construct and return a multi piecewise affine expression
6460 * that is equal to the given piecewise multi affine expression.
6462 * If the resulting multi piecewise affine expression has
6463 * an explicit domain, then assign it the domain of the input.
6464 * In other cases, the domain is stored in the individual elements.
6466 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_from_pw_multi_aff(
6467 __isl_take isl_pw_multi_aff
*pma
)
6472 isl_multi_pw_aff
*mpa
;
6474 n
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
6476 pma
= isl_pw_multi_aff_free(pma
);
6477 space
= isl_pw_multi_aff_get_space(pma
);
6478 mpa
= isl_multi_pw_aff_alloc(space
);
6480 for (i
= 0; i
< n
; ++i
) {
6483 pa
= isl_pw_multi_aff_get_pw_aff(pma
, i
);
6484 mpa
= isl_multi_pw_aff_set_pw_aff(mpa
, i
, pa
);
6486 if (isl_multi_pw_aff_has_explicit_domain(mpa
)) {
6489 dom
= isl_pw_multi_aff_domain(isl_pw_multi_aff_copy(pma
));
6490 mpa
= isl_multi_pw_aff_intersect_domain(mpa
, dom
);
6493 isl_pw_multi_aff_free(pma
);
6497 /* Do "pa1" and "pa2" represent the same function?
6499 * We first check if they are obviously equal.
6500 * If not, we convert them to maps and check if those are equal.
6502 * If "pa1" or "pa2" contain any NaNs, then they are considered
6503 * not to be the same. A NaN is not equal to anything, not even
6506 isl_bool
isl_pw_aff_is_equal(__isl_keep isl_pw_aff
*pa1
,
6507 __isl_keep isl_pw_aff
*pa2
)
6511 isl_map
*map1
, *map2
;
6514 return isl_bool_error
;
6516 equal
= isl_pw_aff_plain_is_equal(pa1
, pa2
);
6517 if (equal
< 0 || equal
)
6519 has_nan
= either_involves_nan(pa1
, pa2
);
6521 return isl_bool_error
;
6523 return isl_bool_false
;
6525 map1
= isl_map_from_pw_aff_internal(isl_pw_aff_copy(pa1
));
6526 map2
= isl_map_from_pw_aff_internal(isl_pw_aff_copy(pa2
));
6527 equal
= isl_map_is_equal(map1
, map2
);
6534 /* Do "mpa1" and "mpa2" represent the same function?
6536 * Note that we cannot convert the entire isl_multi_pw_aff
6537 * to a map because the domains of the piecewise affine expressions
6538 * may not be the same.
6540 isl_bool
isl_multi_pw_aff_is_equal(__isl_keep isl_multi_pw_aff
*mpa1
,
6541 __isl_keep isl_multi_pw_aff
*mpa2
)
6544 isl_bool equal
, equal_params
;
6547 return isl_bool_error
;
6549 equal_params
= isl_space_has_equal_params(mpa1
->space
, mpa2
->space
);
6550 if (equal_params
< 0)
6551 return isl_bool_error
;
6552 if (!equal_params
) {
6553 if (!isl_space_has_named_params(mpa1
->space
))
6554 return isl_bool_false
;
6555 if (!isl_space_has_named_params(mpa2
->space
))
6556 return isl_bool_false
;
6557 mpa1
= isl_multi_pw_aff_copy(mpa1
);
6558 mpa2
= isl_multi_pw_aff_copy(mpa2
);
6559 mpa1
= isl_multi_pw_aff_align_params(mpa1
,
6560 isl_multi_pw_aff_get_space(mpa2
));
6561 mpa2
= isl_multi_pw_aff_align_params(mpa2
,
6562 isl_multi_pw_aff_get_space(mpa1
));
6563 equal
= isl_multi_pw_aff_is_equal(mpa1
, mpa2
);
6564 isl_multi_pw_aff_free(mpa1
);
6565 isl_multi_pw_aff_free(mpa2
);
6569 equal
= isl_space_is_equal(mpa1
->space
, mpa2
->space
);
6570 if (equal
< 0 || !equal
)
6573 for (i
= 0; i
< mpa1
->n
; ++i
) {
6574 equal
= isl_pw_aff_is_equal(mpa1
->u
.p
[i
], mpa2
->u
.p
[i
]);
6575 if (equal
< 0 || !equal
)
6579 return isl_bool_true
;
6582 /* Do "pma1" and "pma2" represent the same function?
6584 * First check if they are obviously equal.
6585 * If not, then convert them to maps and check if those are equal.
6587 * If "pa1" or "pa2" contain any NaNs, then they are considered
6588 * not to be the same. A NaN is not equal to anything, not even
6591 isl_bool
isl_pw_multi_aff_is_equal(__isl_keep isl_pw_multi_aff
*pma1
,
6592 __isl_keep isl_pw_multi_aff
*pma2
)
6596 isl_map
*map1
, *map2
;
6599 return isl_bool_error
;
6601 equal
= isl_pw_multi_aff_plain_is_equal(pma1
, pma2
);
6602 if (equal
< 0 || equal
)
6604 has_nan
= isl_pw_multi_aff_involves_nan(pma1
);
6605 if (has_nan
>= 0 && !has_nan
)
6606 has_nan
= isl_pw_multi_aff_involves_nan(pma2
);
6607 if (has_nan
< 0 || has_nan
)
6608 return isl_bool_not(has_nan
);
6610 map1
= isl_map_from_pw_multi_aff(isl_pw_multi_aff_copy(pma1
));
6611 map2
= isl_map_from_pw_multi_aff(isl_pw_multi_aff_copy(pma2
));
6612 equal
= isl_map_is_equal(map1
, map2
);
6619 /* Compute the pullback of "mpa" by the function represented by "ma".
6620 * In other words, plug in "ma" in "mpa".
6622 * The parameters of "mpa" and "ma" are assumed to have been aligned.
6624 * If "mpa" has an explicit domain, then it is this domain
6625 * that needs to undergo a pullback, i.e., a preimage.
6627 static __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_pullback_multi_aff_aligned(
6628 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_multi_aff
*ma
)
6631 isl_space
*space
= NULL
;
6633 mpa
= isl_multi_pw_aff_cow(mpa
);
6637 space
= isl_space_join(isl_multi_aff_get_space(ma
),
6638 isl_multi_pw_aff_get_space(mpa
));
6642 for (i
= 0; i
< mpa
->n
; ++i
) {
6643 mpa
->u
.p
[i
] = isl_pw_aff_pullback_multi_aff(mpa
->u
.p
[i
],
6644 isl_multi_aff_copy(ma
));
6648 if (isl_multi_pw_aff_has_explicit_domain(mpa
)) {
6649 mpa
->u
.dom
= isl_set_preimage_multi_aff(mpa
->u
.dom
,
6650 isl_multi_aff_copy(ma
));
6655 isl_multi_aff_free(ma
);
6656 isl_space_free(mpa
->space
);
6660 isl_space_free(space
);
6661 isl_multi_pw_aff_free(mpa
);
6662 isl_multi_aff_free(ma
);
6666 /* Compute the pullback of "mpa" by the function represented by "ma".
6667 * In other words, plug in "ma" in "mpa".
6669 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_pullback_multi_aff(
6670 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_multi_aff
*ma
)
6672 isl_bool equal_params
;
6676 equal_params
= isl_space_has_equal_params(mpa
->space
, ma
->space
);
6677 if (equal_params
< 0)
6680 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa
, ma
);
6681 mpa
= isl_multi_pw_aff_align_params(mpa
, isl_multi_aff_get_space(ma
));
6682 ma
= isl_multi_aff_align_params(ma
, isl_multi_pw_aff_get_space(mpa
));
6683 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa
, ma
);
6685 isl_multi_pw_aff_free(mpa
);
6686 isl_multi_aff_free(ma
);
6690 /* Compute the pullback of "mpa" by the function represented by "pma".
6691 * In other words, plug in "pma" in "mpa".
6693 * The parameters of "mpa" and "mpa" are assumed to have been aligned.
6695 * If "mpa" has an explicit domain, then it is this domain
6696 * that needs to undergo a pullback, i.e., a preimage.
6698 static __isl_give isl_multi_pw_aff
*
6699 isl_multi_pw_aff_pullback_pw_multi_aff_aligned(
6700 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_pw_multi_aff
*pma
)
6703 isl_space
*space
= NULL
;
6705 mpa
= isl_multi_pw_aff_cow(mpa
);
6709 space
= isl_space_join(isl_pw_multi_aff_get_space(pma
),
6710 isl_multi_pw_aff_get_space(mpa
));
6712 for (i
= 0; i
< mpa
->n
; ++i
) {
6713 mpa
->u
.p
[i
] = isl_pw_aff_pullback_pw_multi_aff_aligned(
6714 mpa
->u
.p
[i
], isl_pw_multi_aff_copy(pma
));
6718 if (isl_multi_pw_aff_has_explicit_domain(mpa
)) {
6719 mpa
->u
.dom
= isl_set_preimage_pw_multi_aff(mpa
->u
.dom
,
6720 isl_pw_multi_aff_copy(pma
));
6725 isl_pw_multi_aff_free(pma
);
6726 isl_space_free(mpa
->space
);
6730 isl_space_free(space
);
6731 isl_multi_pw_aff_free(mpa
);
6732 isl_pw_multi_aff_free(pma
);
6736 /* Compute the pullback of "mpa" by the function represented by "pma".
6737 * In other words, plug in "pma" in "mpa".
6739 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_pullback_pw_multi_aff(
6740 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_pw_multi_aff
*pma
)
6742 isl_bool equal_params
;
6746 equal_params
= isl_space_has_equal_params(mpa
->space
, pma
->dim
);
6747 if (equal_params
< 0)
6750 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa
, pma
);
6751 mpa
= isl_multi_pw_aff_align_params(mpa
,
6752 isl_pw_multi_aff_get_space(pma
));
6753 pma
= isl_pw_multi_aff_align_params(pma
,
6754 isl_multi_pw_aff_get_space(mpa
));
6755 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa
, pma
);
6757 isl_multi_pw_aff_free(mpa
);
6758 isl_pw_multi_aff_free(pma
);
6762 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
6763 * with the domain of "aff". The domain of the result is the same
6765 * "mpa" and "aff" are assumed to have been aligned.
6767 * We first extract the parametric constant from "aff", defined
6768 * over the correct domain.
6769 * Then we add the appropriate combinations of the members of "mpa".
6770 * Finally, we add the integer divisions through recursive calls.
6772 static __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_aff_aligned(
6773 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_aff
*aff
)
6776 isl_size n_in
, n_div
, n_mpa_in
;
6782 n_in
= isl_aff_dim(aff
, isl_dim_in
);
6783 n_div
= isl_aff_dim(aff
, isl_dim_div
);
6784 n_mpa_in
= isl_multi_pw_aff_dim(mpa
, isl_dim_in
);
6785 if (n_in
< 0 || n_div
< 0 || n_mpa_in
< 0)
6788 space
= isl_space_domain(isl_multi_pw_aff_get_space(mpa
));
6789 tmp
= isl_aff_copy(aff
);
6790 tmp
= isl_aff_drop_dims(tmp
, isl_dim_div
, 0, n_div
);
6791 tmp
= isl_aff_drop_dims(tmp
, isl_dim_in
, 0, n_in
);
6792 tmp
= isl_aff_add_dims(tmp
, isl_dim_in
, n_mpa_in
);
6793 tmp
= isl_aff_reset_domain_space(tmp
, space
);
6794 pa
= isl_pw_aff_from_aff(tmp
);
6796 for (i
= 0; i
< n_in
; ++i
) {
6799 if (!isl_aff_involves_dims(aff
, isl_dim_in
, i
, 1))
6801 v
= isl_aff_get_coefficient_val(aff
, isl_dim_in
, i
);
6802 pa_i
= isl_multi_pw_aff_get_pw_aff(mpa
, i
);
6803 pa_i
= isl_pw_aff_scale_val(pa_i
, v
);
6804 pa
= isl_pw_aff_add(pa
, pa_i
);
6807 for (i
= 0; i
< n_div
; ++i
) {
6811 if (!isl_aff_involves_dims(aff
, isl_dim_div
, i
, 1))
6813 div
= isl_aff_get_div(aff
, i
);
6814 pa_i
= isl_multi_pw_aff_apply_aff_aligned(
6815 isl_multi_pw_aff_copy(mpa
), div
);
6816 pa_i
= isl_pw_aff_floor(pa_i
);
6817 v
= isl_aff_get_coefficient_val(aff
, isl_dim_div
, i
);
6818 pa_i
= isl_pw_aff_scale_val(pa_i
, v
);
6819 pa
= isl_pw_aff_add(pa
, pa_i
);
6822 isl_multi_pw_aff_free(mpa
);
6827 isl_multi_pw_aff_free(mpa
);
6832 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
6833 * with the domain of "aff". The domain of the result is the same
6836 __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_aff(
6837 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_aff
*aff
)
6839 isl_bool equal_params
;
6843 equal_params
= isl_space_has_equal_params(aff
->ls
->dim
, mpa
->space
);
6844 if (equal_params
< 0)
6847 return isl_multi_pw_aff_apply_aff_aligned(mpa
, aff
);
6849 aff
= isl_aff_align_params(aff
, isl_multi_pw_aff_get_space(mpa
));
6850 mpa
= isl_multi_pw_aff_align_params(mpa
, isl_aff_get_space(aff
));
6852 return isl_multi_pw_aff_apply_aff_aligned(mpa
, aff
);
6855 isl_multi_pw_aff_free(mpa
);
6859 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
6860 * with the domain of "pa". The domain of the result is the same
6862 * "mpa" and "pa" are assumed to have been aligned.
6864 * We consider each piece in turn. Note that the domains of the
6865 * pieces are assumed to be disjoint and they remain disjoint
6866 * after taking the preimage (over the same function).
6868 static __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_pw_aff_aligned(
6869 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_pw_aff
*pa
)
6878 space
= isl_space_join(isl_multi_pw_aff_get_space(mpa
),
6879 isl_pw_aff_get_space(pa
));
6880 res
= isl_pw_aff_empty(space
);
6882 for (i
= 0; i
< pa
->n
; ++i
) {
6886 pa_i
= isl_multi_pw_aff_apply_aff_aligned(
6887 isl_multi_pw_aff_copy(mpa
),
6888 isl_aff_copy(pa
->p
[i
].aff
));
6889 domain
= isl_set_copy(pa
->p
[i
].set
);
6890 domain
= isl_set_preimage_multi_pw_aff(domain
,
6891 isl_multi_pw_aff_copy(mpa
));
6892 pa_i
= isl_pw_aff_intersect_domain(pa_i
, domain
);
6893 res
= isl_pw_aff_add_disjoint(res
, pa_i
);
6896 isl_pw_aff_free(pa
);
6897 isl_multi_pw_aff_free(mpa
);
6900 isl_pw_aff_free(pa
);
6901 isl_multi_pw_aff_free(mpa
);
6905 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
6906 * with the domain of "pa". The domain of the result is the same
6909 __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_pw_aff(
6910 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_pw_aff
*pa
)
6912 isl_bool equal_params
;
6916 equal_params
= isl_space_has_equal_params(pa
->dim
, mpa
->space
);
6917 if (equal_params
< 0)
6920 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa
, pa
);
6922 pa
= isl_pw_aff_align_params(pa
, isl_multi_pw_aff_get_space(mpa
));
6923 mpa
= isl_multi_pw_aff_align_params(mpa
, isl_pw_aff_get_space(pa
));
6925 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa
, pa
);
6927 isl_pw_aff_free(pa
);
6928 isl_multi_pw_aff_free(mpa
);
6932 /* Compute the pullback of "pa" by the function represented by "mpa".
6933 * In other words, plug in "mpa" in "pa".
6934 * "pa" and "mpa" are assumed to have been aligned.
6936 * The pullback is computed by applying "pa" to "mpa".
6938 static __isl_give isl_pw_aff
*isl_pw_aff_pullback_multi_pw_aff_aligned(
6939 __isl_take isl_pw_aff
*pa
, __isl_take isl_multi_pw_aff
*mpa
)
6941 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa
, pa
);
6944 /* Compute the pullback of "pa" by the function represented by "mpa".
6945 * In other words, plug in "mpa" in "pa".
6947 * The pullback is computed by applying "pa" to "mpa".
6949 __isl_give isl_pw_aff
*isl_pw_aff_pullback_multi_pw_aff(
6950 __isl_take isl_pw_aff
*pa
, __isl_take isl_multi_pw_aff
*mpa
)
6952 return isl_multi_pw_aff_apply_pw_aff(mpa
, pa
);
6955 /* Compute the pullback of "mpa1" by the function represented by "mpa2".
6956 * In other words, plug in "mpa2" in "mpa1".
6958 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
6960 * We pullback each member of "mpa1" in turn.
6962 * If "mpa1" has an explicit domain, then it is this domain
6963 * that needs to undergo a pullback instead, i.e., a preimage.
6965 static __isl_give isl_multi_pw_aff
*
6966 isl_multi_pw_aff_pullback_multi_pw_aff_aligned(
6967 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
)
6970 isl_space
*space
= NULL
;
6972 mpa1
= isl_multi_pw_aff_cow(mpa1
);
6976 space
= isl_space_join(isl_multi_pw_aff_get_space(mpa2
),
6977 isl_multi_pw_aff_get_space(mpa1
));
6979 for (i
= 0; i
< mpa1
->n
; ++i
) {
6980 mpa1
->u
.p
[i
] = isl_pw_aff_pullback_multi_pw_aff_aligned(
6981 mpa1
->u
.p
[i
], isl_multi_pw_aff_copy(mpa2
));
6986 if (isl_multi_pw_aff_has_explicit_domain(mpa1
)) {
6987 mpa1
->u
.dom
= isl_set_preimage_multi_pw_aff(mpa1
->u
.dom
,
6988 isl_multi_pw_aff_copy(mpa2
));
6992 mpa1
= isl_multi_pw_aff_reset_space(mpa1
, space
);
6994 isl_multi_pw_aff_free(mpa2
);
6997 isl_space_free(space
);
6998 isl_multi_pw_aff_free(mpa1
);
6999 isl_multi_pw_aff_free(mpa2
);
7003 /* Compute the pullback of "mpa1" by the function represented by "mpa2".
7004 * In other words, plug in "mpa2" in "mpa1".
7006 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_pullback_multi_pw_aff(
7007 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
)
7009 return isl_multi_pw_aff_align_params_multi_multi_and(mpa1
, mpa2
,
7010 &isl_multi_pw_aff_pullback_multi_pw_aff_aligned
);
7013 /* Align the parameters of "mpa1" and "mpa2", check that the ranges
7014 * of "mpa1" and "mpa2" live in the same space, construct map space
7015 * between the domain spaces of "mpa1" and "mpa2" and call "order"
7016 * with this map space as extract argument.
7018 static __isl_give isl_map
*isl_multi_pw_aff_order_map(
7019 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
,
7020 __isl_give isl_map
*(*order
)(__isl_keep isl_multi_pw_aff
*mpa1
,
7021 __isl_keep isl_multi_pw_aff
*mpa2
, __isl_take isl_space
*space
))
7024 isl_space
*space1
, *space2
;
7027 mpa1
= isl_multi_pw_aff_align_params(mpa1
,
7028 isl_multi_pw_aff_get_space(mpa2
));
7029 mpa2
= isl_multi_pw_aff_align_params(mpa2
,
7030 isl_multi_pw_aff_get_space(mpa1
));
7033 match
= isl_space_tuple_is_equal(mpa1
->space
, isl_dim_out
,
7034 mpa2
->space
, isl_dim_out
);
7038 isl_die(isl_multi_pw_aff_get_ctx(mpa1
), isl_error_invalid
,
7039 "range spaces don't match", goto error
);
7040 space1
= isl_space_domain(isl_multi_pw_aff_get_space(mpa1
));
7041 space2
= isl_space_domain(isl_multi_pw_aff_get_space(mpa2
));
7042 space1
= isl_space_map_from_domain_and_range(space1
, space2
);
7044 res
= order(mpa1
, mpa2
, space1
);
7045 isl_multi_pw_aff_free(mpa1
);
7046 isl_multi_pw_aff_free(mpa2
);
7049 isl_multi_pw_aff_free(mpa1
);
7050 isl_multi_pw_aff_free(mpa2
);
7054 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7055 * where the function values are equal. "space" is the space of the result.
7056 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7058 * "mpa1" and "mpa2" are equal when each of the pairs of elements
7059 * in the sequences are equal.
7061 static __isl_give isl_map
*isl_multi_pw_aff_eq_map_on_space(
7062 __isl_keep isl_multi_pw_aff
*mpa1
, __isl_keep isl_multi_pw_aff
*mpa2
,
7063 __isl_take isl_space
*space
)
7069 n
= isl_multi_pw_aff_dim(mpa1
, isl_dim_out
);
7071 space
= isl_space_free(space
);
7072 res
= isl_map_universe(space
);
7074 for (i
= 0; i
< n
; ++i
) {
7075 isl_pw_aff
*pa1
, *pa2
;
7078 pa1
= isl_multi_pw_aff_get_pw_aff(mpa1
, i
);
7079 pa2
= isl_multi_pw_aff_get_pw_aff(mpa2
, i
);
7080 map
= isl_pw_aff_eq_map(pa1
, pa2
);
7081 res
= isl_map_intersect(res
, map
);
7087 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7088 * where the function values are equal.
7090 __isl_give isl_map
*isl_multi_pw_aff_eq_map(__isl_take isl_multi_pw_aff
*mpa1
,
7091 __isl_take isl_multi_pw_aff
*mpa2
)
7093 return isl_multi_pw_aff_order_map(mpa1
, mpa2
,
7094 &isl_multi_pw_aff_eq_map_on_space
);
7097 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7098 * where the function values of "mpa1" is lexicographically satisfies "base"
7099 * compared to that of "mpa2". "space" is the space of the result.
7100 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7102 * "mpa1" lexicographically satisfies "base" compared to "mpa2"
7103 * if its i-th element satisfies "base" when compared to
7104 * the i-th element of "mpa2" while all previous elements are
7107 static __isl_give isl_map
*isl_multi_pw_aff_lex_map_on_space(
7108 __isl_keep isl_multi_pw_aff
*mpa1
, __isl_keep isl_multi_pw_aff
*mpa2
,
7109 __isl_give isl_map
*(*base
)(__isl_take isl_pw_aff
*pa1
,
7110 __isl_take isl_pw_aff
*pa2
),
7111 __isl_take isl_space
*space
)
7115 isl_map
*res
, *rest
;
7117 n
= isl_multi_pw_aff_dim(mpa1
, isl_dim_out
);
7119 space
= isl_space_free(space
);
7120 res
= isl_map_empty(isl_space_copy(space
));
7121 rest
= isl_map_universe(space
);
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
= base(pa1
, pa2
);
7130 map
= isl_map_intersect(map
, isl_map_copy(rest
));
7131 res
= isl_map_union(res
, map
);
7136 pa1
= isl_multi_pw_aff_get_pw_aff(mpa1
, i
);
7137 pa2
= isl_multi_pw_aff_get_pw_aff(mpa2
, i
);
7138 map
= isl_pw_aff_eq_map(pa1
, pa2
);
7139 rest
= isl_map_intersect(rest
, map
);
7146 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7147 * where the function value of "mpa1" is lexicographically less than that
7148 * of "mpa2". "space" is the space of the result.
7149 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7151 * "mpa1" is less than "mpa2" if its i-th element is smaller
7152 * than the i-th element of "mpa2" while all previous elements are
7155 __isl_give isl_map
*isl_multi_pw_aff_lex_lt_map_on_space(
7156 __isl_keep isl_multi_pw_aff
*mpa1
, __isl_keep isl_multi_pw_aff
*mpa2
,
7157 __isl_take isl_space
*space
)
7159 return isl_multi_pw_aff_lex_map_on_space(mpa1
, mpa2
,
7160 &isl_pw_aff_lt_map
, space
);
7163 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7164 * where the function value of "mpa1" is lexicographically less than that
7167 __isl_give isl_map
*isl_multi_pw_aff_lex_lt_map(
7168 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
)
7170 return isl_multi_pw_aff_order_map(mpa1
, mpa2
,
7171 &isl_multi_pw_aff_lex_lt_map_on_space
);
7174 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7175 * where the function value of "mpa1" is lexicographically greater than that
7176 * of "mpa2". "space" is the space of the result.
7177 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7179 * "mpa1" is greater than "mpa2" if its i-th element is greater
7180 * than the i-th element of "mpa2" while all previous elements are
7183 __isl_give isl_map
*isl_multi_pw_aff_lex_gt_map_on_space(
7184 __isl_keep isl_multi_pw_aff
*mpa1
, __isl_keep isl_multi_pw_aff
*mpa2
,
7185 __isl_take isl_space
*space
)
7187 return isl_multi_pw_aff_lex_map_on_space(mpa1
, mpa2
,
7188 &isl_pw_aff_gt_map
, space
);
7191 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7192 * where the function value of "mpa1" is lexicographically greater than that
7195 __isl_give isl_map
*isl_multi_pw_aff_lex_gt_map(
7196 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
)
7198 return isl_multi_pw_aff_order_map(mpa1
, mpa2
,
7199 &isl_multi_pw_aff_lex_gt_map_on_space
);
7202 /* Compare two isl_affs.
7204 * Return -1 if "aff1" is "smaller" than "aff2", 1 if "aff1" is "greater"
7205 * than "aff2" and 0 if they are equal.
7207 * The order is fairly arbitrary. We do consider expressions that only involve
7208 * earlier dimensions as "smaller".
7210 int isl_aff_plain_cmp(__isl_keep isl_aff
*aff1
, __isl_keep isl_aff
*aff2
)
7223 cmp
= isl_local_space_cmp(aff1
->ls
, aff2
->ls
);
7227 last1
= isl_seq_last_non_zero(aff1
->v
->el
+ 1, aff1
->v
->size
- 1);
7228 last2
= isl_seq_last_non_zero(aff2
->v
->el
+ 1, aff1
->v
->size
- 1);
7230 return last1
- last2
;
7232 return isl_seq_cmp(aff1
->v
->el
, aff2
->v
->el
, aff1
->v
->size
);
7235 /* Compare two isl_pw_affs.
7237 * Return -1 if "pa1" is "smaller" than "pa2", 1 if "pa1" is "greater"
7238 * than "pa2" and 0 if they are equal.
7240 * The order is fairly arbitrary. We do consider expressions that only involve
7241 * earlier dimensions as "smaller".
7243 int isl_pw_aff_plain_cmp(__isl_keep isl_pw_aff
*pa1
,
7244 __isl_keep isl_pw_aff
*pa2
)
7257 cmp
= isl_space_cmp(pa1
->dim
, pa2
->dim
);
7261 if (pa1
->n
!= pa2
->n
)
7262 return pa1
->n
- pa2
->n
;
7264 for (i
= 0; i
< pa1
->n
; ++i
) {
7265 cmp
= isl_set_plain_cmp(pa1
->p
[i
].set
, pa2
->p
[i
].set
);
7268 cmp
= isl_aff_plain_cmp(pa1
->p
[i
].aff
, pa2
->p
[i
].aff
);
7276 /* Return a piecewise affine expression that is equal to "v" on "domain".
7278 __isl_give isl_pw_aff
*isl_pw_aff_val_on_domain(__isl_take isl_set
*domain
,
7279 __isl_take isl_val
*v
)
7282 isl_local_space
*ls
;
7285 space
= isl_set_get_space(domain
);
7286 ls
= isl_local_space_from_space(space
);
7287 aff
= isl_aff_val_on_domain(ls
, v
);
7289 return isl_pw_aff_alloc(domain
, aff
);
7292 /* Return a multi affine expression that is equal to "mv" on domain
7295 __isl_give isl_multi_aff
*isl_multi_aff_multi_val_on_space(
7296 __isl_take isl_space
*space
, __isl_take isl_multi_val
*mv
)
7301 isl_local_space
*ls
;
7304 n
= isl_multi_val_dim(mv
, isl_dim_set
);
7305 if (!space
|| n
< 0)
7308 space2
= isl_multi_val_get_space(mv
);
7309 space2
= isl_space_align_params(space2
, isl_space_copy(space
));
7310 space
= isl_space_align_params(space
, isl_space_copy(space2
));
7311 space
= isl_space_map_from_domain_and_range(space
, space2
);
7312 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
7313 ls
= isl_local_space_from_space(isl_space_domain(space
));
7314 for (i
= 0; i
< n
; ++i
) {
7318 v
= isl_multi_val_get_val(mv
, i
);
7319 aff
= isl_aff_val_on_domain(isl_local_space_copy(ls
), v
);
7320 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
7322 isl_local_space_free(ls
);
7324 isl_multi_val_free(mv
);
7327 isl_space_free(space
);
7328 isl_multi_val_free(mv
);
7332 /* Return a piecewise multi-affine expression
7333 * that is equal to "mv" on "domain".
7335 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_multi_val_on_domain(
7336 __isl_take isl_set
*domain
, __isl_take isl_multi_val
*mv
)
7341 space
= isl_set_get_space(domain
);
7342 ma
= isl_multi_aff_multi_val_on_space(space
, mv
);
7344 return isl_pw_multi_aff_alloc(domain
, ma
);
7347 /* Internal data structure for isl_union_pw_multi_aff_multi_val_on_domain.
7348 * mv is the value that should be attained on each domain set
7349 * res collects the results
7351 struct isl_union_pw_multi_aff_multi_val_on_domain_data
{
7353 isl_union_pw_multi_aff
*res
;
7356 /* Create an isl_pw_multi_aff equal to data->mv on "domain"
7357 * and add it to data->res.
7359 static isl_stat
pw_multi_aff_multi_val_on_domain(__isl_take isl_set
*domain
,
7362 struct isl_union_pw_multi_aff_multi_val_on_domain_data
*data
= user
;
7363 isl_pw_multi_aff
*pma
;
7366 mv
= isl_multi_val_copy(data
->mv
);
7367 pma
= isl_pw_multi_aff_multi_val_on_domain(domain
, mv
);
7368 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma
);
7370 return data
->res
? isl_stat_ok
: isl_stat_error
;
7373 /* Return a union piecewise multi-affine expression
7374 * that is equal to "mv" on "domain".
7376 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_multi_val_on_domain(
7377 __isl_take isl_union_set
*domain
, __isl_take isl_multi_val
*mv
)
7379 struct isl_union_pw_multi_aff_multi_val_on_domain_data data
;
7382 space
= isl_union_set_get_space(domain
);
7383 data
.res
= isl_union_pw_multi_aff_empty(space
);
7385 if (isl_union_set_foreach_set(domain
,
7386 &pw_multi_aff_multi_val_on_domain
, &data
) < 0)
7387 data
.res
= isl_union_pw_multi_aff_free(data
.res
);
7388 isl_union_set_free(domain
);
7389 isl_multi_val_free(mv
);
7393 /* Compute the pullback of data->pma by the function represented by "pma2",
7394 * provided the spaces match, and add the results to data->res.
7396 static isl_stat
pullback_entry(__isl_take isl_pw_multi_aff
*pma2
, void *user
)
7398 struct isl_union_pw_multi_aff_bin_data
*data
= user
;
7400 if (!isl_space_tuple_is_equal(data
->pma
->dim
, isl_dim_in
,
7401 pma2
->dim
, isl_dim_out
)) {
7402 isl_pw_multi_aff_free(pma2
);
7406 pma2
= isl_pw_multi_aff_pullback_pw_multi_aff(
7407 isl_pw_multi_aff_copy(data
->pma
), pma2
);
7409 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma2
);
7411 return isl_stat_error
;
7416 /* Compute the pullback of "upma1" by the function represented by "upma2".
7418 __isl_give isl_union_pw_multi_aff
*
7419 isl_union_pw_multi_aff_pullback_union_pw_multi_aff(
7420 __isl_take isl_union_pw_multi_aff
*upma1
,
7421 __isl_take isl_union_pw_multi_aff
*upma2
)
7423 return bin_op(upma1
, upma2
, &pullback_entry
);
7426 /* Check that the domain space of "upa" matches "space".
7428 * This function is called from isl_multi_union_pw_aff_set_union_pw_aff and
7429 * can in principle never fail since the space "space" is that
7430 * of the isl_multi_union_pw_aff and is a set space such that
7431 * there is no domain space to match.
7433 * We check the parameters and double-check that "space" is
7434 * indeed that of a set.
7436 static isl_stat
isl_union_pw_aff_check_match_domain_space(
7437 __isl_keep isl_union_pw_aff
*upa
, __isl_keep isl_space
*space
)
7439 isl_space
*upa_space
;
7443 return isl_stat_error
;
7445 match
= isl_space_is_set(space
);
7447 return isl_stat_error
;
7449 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
7450 "expecting set space", return isl_stat_error
);
7452 upa_space
= isl_union_pw_aff_get_space(upa
);
7453 match
= isl_space_has_equal_params(space
, upa_space
);
7457 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
7458 "parameters don't match", goto error
);
7460 isl_space_free(upa_space
);
7463 isl_space_free(upa_space
);
7464 return isl_stat_error
;
7467 /* Do the parameters of "upa" match those of "space"?
7469 static isl_bool
isl_union_pw_aff_matching_params(
7470 __isl_keep isl_union_pw_aff
*upa
, __isl_keep isl_space
*space
)
7472 isl_space
*upa_space
;
7476 return isl_bool_error
;
7478 upa_space
= isl_union_pw_aff_get_space(upa
);
7480 match
= isl_space_has_equal_params(space
, upa_space
);
7482 isl_space_free(upa_space
);
7486 /* Internal data structure for isl_union_pw_aff_reset_domain_space.
7487 * space represents the new parameters.
7488 * res collects the results.
7490 struct isl_union_pw_aff_reset_params_data
{
7492 isl_union_pw_aff
*res
;
7495 /* Replace the parameters of "pa" by data->space and
7496 * add the result to data->res.
7498 static isl_stat
reset_params(__isl_take isl_pw_aff
*pa
, void *user
)
7500 struct isl_union_pw_aff_reset_params_data
*data
= user
;
7503 space
= isl_pw_aff_get_space(pa
);
7504 space
= isl_space_replace_params(space
, data
->space
);
7505 pa
= isl_pw_aff_reset_space(pa
, space
);
7506 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
7508 return data
->res
? isl_stat_ok
: isl_stat_error
;
7511 /* Replace the domain space of "upa" by "space".
7512 * Since a union expression does not have a (single) domain space,
7513 * "space" is necessarily a parameter space.
7515 * Since the order and the names of the parameters determine
7516 * the hash value, we need to create a new hash table.
7518 static __isl_give isl_union_pw_aff
*isl_union_pw_aff_reset_domain_space(
7519 __isl_take isl_union_pw_aff
*upa
, __isl_take isl_space
*space
)
7521 struct isl_union_pw_aff_reset_params_data data
= { space
};
7524 match
= isl_union_pw_aff_matching_params(upa
, space
);
7526 upa
= isl_union_pw_aff_free(upa
);
7528 isl_space_free(space
);
7532 data
.res
= isl_union_pw_aff_empty(isl_space_copy(space
));
7533 if (isl_union_pw_aff_foreach_pw_aff(upa
, &reset_params
, &data
) < 0)
7534 data
.res
= isl_union_pw_aff_free(data
.res
);
7536 isl_union_pw_aff_free(upa
);
7537 isl_space_free(space
);
7541 /* Return the floor of "pa".
7543 static __isl_give isl_pw_aff
*floor_entry(__isl_take isl_pw_aff
*pa
, void *user
)
7545 return isl_pw_aff_floor(pa
);
7548 /* Given f, return floor(f).
7550 __isl_give isl_union_pw_aff
*isl_union_pw_aff_floor(
7551 __isl_take isl_union_pw_aff
*upa
)
7553 return isl_union_pw_aff_transform_inplace(upa
, &floor_entry
, NULL
);
7558 * upa mod m = upa - m * floor(upa/m)
7560 * with m an integer value.
7562 __isl_give isl_union_pw_aff
*isl_union_pw_aff_mod_val(
7563 __isl_take isl_union_pw_aff
*upa
, __isl_take isl_val
*m
)
7565 isl_union_pw_aff
*res
;
7570 if (!isl_val_is_int(m
))
7571 isl_die(isl_val_get_ctx(m
), isl_error_invalid
,
7572 "expecting integer modulo", goto error
);
7573 if (!isl_val_is_pos(m
))
7574 isl_die(isl_val_get_ctx(m
), isl_error_invalid
,
7575 "expecting positive modulo", goto error
);
7577 res
= isl_union_pw_aff_copy(upa
);
7578 upa
= isl_union_pw_aff_scale_down_val(upa
, isl_val_copy(m
));
7579 upa
= isl_union_pw_aff_floor(upa
);
7580 upa
= isl_union_pw_aff_scale_val(upa
, m
);
7581 res
= isl_union_pw_aff_sub(res
, upa
);
7586 isl_union_pw_aff_free(upa
);
7590 /* Internal data structure for isl_union_pw_multi_aff_get_union_pw_aff.
7591 * pos is the output position that needs to be extracted.
7592 * res collects the results.
7594 struct isl_union_pw_multi_aff_get_union_pw_aff_data
{
7596 isl_union_pw_aff
*res
;
7599 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma"
7600 * (assuming it has such a dimension) and add it to data->res.
7602 static isl_stat
get_union_pw_aff(__isl_take isl_pw_multi_aff
*pma
, void *user
)
7604 struct isl_union_pw_multi_aff_get_union_pw_aff_data
*data
= user
;
7608 n_out
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
7610 return isl_stat_error
;
7611 if (data
->pos
>= n_out
) {
7612 isl_pw_multi_aff_free(pma
);
7616 pa
= isl_pw_multi_aff_get_pw_aff(pma
, data
->pos
);
7617 isl_pw_multi_aff_free(pma
);
7619 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
7621 return data
->res
? isl_stat_ok
: isl_stat_error
;
7624 /* Extract an isl_union_pw_aff corresponding to
7625 * output dimension "pos" of "upma".
7627 __isl_give isl_union_pw_aff
*isl_union_pw_multi_aff_get_union_pw_aff(
7628 __isl_keep isl_union_pw_multi_aff
*upma
, int pos
)
7630 struct isl_union_pw_multi_aff_get_union_pw_aff_data data
;
7637 isl_die(isl_union_pw_multi_aff_get_ctx(upma
), isl_error_invalid
,
7638 "cannot extract at negative position", return NULL
);
7640 space
= isl_union_pw_multi_aff_get_space(upma
);
7641 data
.res
= isl_union_pw_aff_empty(space
);
7643 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma
,
7644 &get_union_pw_aff
, &data
) < 0)
7645 data
.res
= isl_union_pw_aff_free(data
.res
);
7650 /* Return a union piecewise affine expression
7651 * that is equal to "aff" on "domain".
7653 __isl_give isl_union_pw_aff
*isl_union_pw_aff_aff_on_domain(
7654 __isl_take isl_union_set
*domain
, __isl_take isl_aff
*aff
)
7658 pa
= isl_pw_aff_from_aff(aff
);
7659 return isl_union_pw_aff_pw_aff_on_domain(domain
, pa
);
7662 /* Return a union piecewise affine expression
7663 * that is equal to the parameter identified by "id" on "domain".
7665 * Make sure the parameter appears in the space passed to
7666 * isl_aff_param_on_domain_space_id.
7668 __isl_give isl_union_pw_aff
*isl_union_pw_aff_param_on_domain_id(
7669 __isl_take isl_union_set
*domain
, __isl_take isl_id
*id
)
7674 space
= isl_union_set_get_space(domain
);
7675 space
= isl_space_add_param_id(space
, isl_id_copy(id
));
7676 aff
= isl_aff_param_on_domain_space_id(space
, id
);
7677 return isl_union_pw_aff_aff_on_domain(domain
, aff
);
7680 /* Internal data structure for isl_union_pw_aff_pw_aff_on_domain.
7681 * "pa" is the piecewise symbolic value that the resulting isl_union_pw_aff
7683 * "res" collects the results.
7685 struct isl_union_pw_aff_pw_aff_on_domain_data
{
7687 isl_union_pw_aff
*res
;
7690 /* Construct a piecewise affine expression that is equal to data->pa
7691 * on "domain" and add the result to data->res.
7693 static isl_stat
pw_aff_on_domain(__isl_take isl_set
*domain
, void *user
)
7695 struct isl_union_pw_aff_pw_aff_on_domain_data
*data
= user
;
7699 pa
= isl_pw_aff_copy(data
->pa
);
7700 dim
= isl_set_dim(domain
, isl_dim_set
);
7702 pa
= isl_pw_aff_free(pa
);
7703 pa
= isl_pw_aff_from_range(pa
);
7704 pa
= isl_pw_aff_add_dims(pa
, isl_dim_in
, dim
);
7705 pa
= isl_pw_aff_reset_domain_space(pa
, isl_set_get_space(domain
));
7706 pa
= isl_pw_aff_intersect_domain(pa
, domain
);
7707 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
7709 return data
->res
? isl_stat_ok
: isl_stat_error
;
7712 /* Return a union piecewise affine expression
7713 * that is equal to "pa" on "domain", assuming "domain" and "pa"
7714 * have been aligned.
7716 * Construct an isl_pw_aff on each of the sets in "domain" and
7717 * collect the results.
7719 static __isl_give isl_union_pw_aff
*isl_union_pw_aff_pw_aff_on_domain_aligned(
7720 __isl_take isl_union_set
*domain
, __isl_take isl_pw_aff
*pa
)
7722 struct isl_union_pw_aff_pw_aff_on_domain_data data
;
7725 space
= isl_union_set_get_space(domain
);
7726 data
.res
= isl_union_pw_aff_empty(space
);
7728 if (isl_union_set_foreach_set(domain
, &pw_aff_on_domain
, &data
) < 0)
7729 data
.res
= isl_union_pw_aff_free(data
.res
);
7730 isl_union_set_free(domain
);
7731 isl_pw_aff_free(pa
);
7735 /* Return a union piecewise affine expression
7736 * that is equal to "pa" on "domain".
7738 * Check that "pa" is a parametric expression,
7739 * align the parameters if needed and call
7740 * isl_union_pw_aff_pw_aff_on_domain_aligned.
7742 __isl_give isl_union_pw_aff
*isl_union_pw_aff_pw_aff_on_domain(
7743 __isl_take isl_union_set
*domain
, __isl_take isl_pw_aff
*pa
)
7746 isl_bool equal_params
;
7747 isl_space
*domain_space
, *pa_space
;
7749 pa_space
= isl_pw_aff_peek_space(pa
);
7750 is_set
= isl_space_is_set(pa_space
);
7754 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
7755 "expecting parametric expression", goto error
);
7757 domain_space
= isl_union_set_get_space(domain
);
7758 pa_space
= isl_pw_aff_get_space(pa
);
7759 equal_params
= isl_space_has_equal_params(domain_space
, pa_space
);
7760 if (equal_params
>= 0 && !equal_params
) {
7763 space
= isl_space_align_params(domain_space
, pa_space
);
7764 pa
= isl_pw_aff_align_params(pa
, isl_space_copy(space
));
7765 domain
= isl_union_set_align_params(domain
, space
);
7767 isl_space_free(domain_space
);
7768 isl_space_free(pa_space
);
7771 if (equal_params
< 0)
7773 return isl_union_pw_aff_pw_aff_on_domain_aligned(domain
, pa
);
7775 isl_union_set_free(domain
);
7776 isl_pw_aff_free(pa
);
7780 /* Internal data structure for isl_union_pw_aff_val_on_domain.
7781 * "v" is the value that the resulting isl_union_pw_aff needs to attain.
7782 * "res" collects the results.
7784 struct isl_union_pw_aff_val_on_domain_data
{
7786 isl_union_pw_aff
*res
;
7789 /* Construct a piecewise affine expression that is equal to data->v
7790 * on "domain" and add the result to data->res.
7792 static isl_stat
pw_aff_val_on_domain(__isl_take isl_set
*domain
, void *user
)
7794 struct isl_union_pw_aff_val_on_domain_data
*data
= user
;
7798 v
= isl_val_copy(data
->v
);
7799 pa
= isl_pw_aff_val_on_domain(domain
, v
);
7800 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
7802 return data
->res
? isl_stat_ok
: isl_stat_error
;
7805 /* Return a union piecewise affine expression
7806 * that is equal to "v" on "domain".
7808 * Construct an isl_pw_aff on each of the sets in "domain" and
7809 * collect the results.
7811 __isl_give isl_union_pw_aff
*isl_union_pw_aff_val_on_domain(
7812 __isl_take isl_union_set
*domain
, __isl_take isl_val
*v
)
7814 struct isl_union_pw_aff_val_on_domain_data data
;
7817 space
= isl_union_set_get_space(domain
);
7818 data
.res
= isl_union_pw_aff_empty(space
);
7820 if (isl_union_set_foreach_set(domain
, &pw_aff_val_on_domain
, &data
) < 0)
7821 data
.res
= isl_union_pw_aff_free(data
.res
);
7822 isl_union_set_free(domain
);
7827 /* Construct a piecewise multi affine expression
7828 * that is equal to "pa" and add it to upma.
7830 static isl_stat
pw_multi_aff_from_pw_aff_entry(__isl_take isl_pw_aff
*pa
,
7833 isl_union_pw_multi_aff
**upma
= user
;
7834 isl_pw_multi_aff
*pma
;
7836 pma
= isl_pw_multi_aff_from_pw_aff(pa
);
7837 *upma
= isl_union_pw_multi_aff_add_pw_multi_aff(*upma
, pma
);
7839 return *upma
? isl_stat_ok
: isl_stat_error
;
7842 /* Construct and return a union piecewise multi affine expression
7843 * that is equal to the given union piecewise affine expression.
7845 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_union_pw_aff(
7846 __isl_take isl_union_pw_aff
*upa
)
7849 isl_union_pw_multi_aff
*upma
;
7854 space
= isl_union_pw_aff_get_space(upa
);
7855 upma
= isl_union_pw_multi_aff_empty(space
);
7857 if (isl_union_pw_aff_foreach_pw_aff(upa
,
7858 &pw_multi_aff_from_pw_aff_entry
, &upma
) < 0)
7859 upma
= isl_union_pw_multi_aff_free(upma
);
7861 isl_union_pw_aff_free(upa
);
7865 /* Compute the set of elements in the domain of "pa" where it is zero and
7866 * add this set to "uset".
7868 static isl_stat
zero_union_set(__isl_take isl_pw_aff
*pa
, void *user
)
7870 isl_union_set
**uset
= (isl_union_set
**)user
;
7872 *uset
= isl_union_set_add_set(*uset
, isl_pw_aff_zero_set(pa
));
7874 return *uset
? isl_stat_ok
: isl_stat_error
;
7877 /* Return a union set containing those elements in the domain
7878 * of "upa" where it is zero.
7880 __isl_give isl_union_set
*isl_union_pw_aff_zero_union_set(
7881 __isl_take isl_union_pw_aff
*upa
)
7883 isl_union_set
*zero
;
7885 zero
= isl_union_set_empty(isl_union_pw_aff_get_space(upa
));
7886 if (isl_union_pw_aff_foreach_pw_aff(upa
, &zero_union_set
, &zero
) < 0)
7887 zero
= isl_union_set_free(zero
);
7889 isl_union_pw_aff_free(upa
);
7893 /* Internal data structure for isl_union_pw_aff_pullback_union_pw_multi_aff.
7894 * upma is the function that is plugged in.
7895 * pa is the current part of the function in which upma is plugged in.
7896 * res collects the results.
7898 struct isl_union_pw_aff_pullback_upma_data
{
7899 isl_union_pw_multi_aff
*upma
;
7901 isl_union_pw_aff
*res
;
7904 /* Check if "pma" can be plugged into data->pa.
7905 * If so, perform the pullback and add the result to data->res.
7907 static isl_stat
pa_pb_pma(__isl_take isl_pw_multi_aff
*pma
, void *user
)
7909 struct isl_union_pw_aff_pullback_upma_data
*data
= user
;
7912 if (!isl_space_tuple_is_equal(data
->pa
->dim
, isl_dim_in
,
7913 pma
->dim
, isl_dim_out
)) {
7914 isl_pw_multi_aff_free(pma
);
7918 pa
= isl_pw_aff_copy(data
->pa
);
7919 pa
= isl_pw_aff_pullback_pw_multi_aff(pa
, pma
);
7921 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
7923 return data
->res
? isl_stat_ok
: isl_stat_error
;
7926 /* Check if any of the elements of data->upma can be plugged into pa,
7927 * add if so add the result to data->res.
7929 static isl_stat
upa_pb_upma(__isl_take isl_pw_aff
*pa
, void *user
)
7931 struct isl_union_pw_aff_pullback_upma_data
*data
= user
;
7935 r
= isl_union_pw_multi_aff_foreach_pw_multi_aff(data
->upma
,
7937 isl_pw_aff_free(pa
);
7942 /* Compute the pullback of "upa" by the function represented by "upma".
7943 * In other words, plug in "upma" in "upa". The result contains
7944 * expressions defined over the domain space of "upma".
7946 * Run over all pairs of elements in "upa" and "upma", perform
7947 * the pullback when appropriate and collect the results.
7948 * If the hash value were based on the domain space rather than
7949 * the function space, then we could run through all elements
7950 * of "upma" and directly pick out the corresponding element of "upa".
7952 __isl_give isl_union_pw_aff
*isl_union_pw_aff_pullback_union_pw_multi_aff(
7953 __isl_take isl_union_pw_aff
*upa
,
7954 __isl_take isl_union_pw_multi_aff
*upma
)
7956 struct isl_union_pw_aff_pullback_upma_data data
= { NULL
, NULL
};
7959 space
= isl_union_pw_multi_aff_get_space(upma
);
7960 upa
= isl_union_pw_aff_align_params(upa
, space
);
7961 space
= isl_union_pw_aff_get_space(upa
);
7962 upma
= isl_union_pw_multi_aff_align_params(upma
, space
);
7968 data
.res
= isl_union_pw_aff_alloc_same_size(upa
);
7969 if (isl_union_pw_aff_foreach_pw_aff(upa
, &upa_pb_upma
, &data
) < 0)
7970 data
.res
= isl_union_pw_aff_free(data
.res
);
7972 isl_union_pw_aff_free(upa
);
7973 isl_union_pw_multi_aff_free(upma
);
7976 isl_union_pw_aff_free(upa
);
7977 isl_union_pw_multi_aff_free(upma
);
7982 #define BASE union_pw_aff
7984 #define DOMBASE union_set
7986 #include <isl_multi_explicit_domain.c>
7987 #include <isl_multi_union_pw_aff_explicit_domain.c>
7988 #include <isl_multi_templ.c>
7989 #include <isl_multi_apply_set.c>
7990 #include <isl_multi_apply_union_set.c>
7991 #include <isl_multi_coalesce.c>
7992 #include <isl_multi_floor.c>
7993 #include <isl_multi_from_base_templ.c>
7994 #include <isl_multi_gist.c>
7995 #include <isl_multi_align_set.c>
7996 #include <isl_multi_align_union_set.c>
7997 #include <isl_multi_intersect.c>
7999 /* Does "mupa" have a non-trivial explicit domain?
8001 * The explicit domain, if present, is trivial if it represents
8002 * an (obviously) universe parameter set.
8004 isl_bool
isl_multi_union_pw_aff_has_non_trivial_domain(
8005 __isl_keep isl_multi_union_pw_aff
*mupa
)
8007 isl_bool is_params
, trivial
;
8011 return isl_bool_error
;
8012 if (!isl_multi_union_pw_aff_has_explicit_domain(mupa
))
8013 return isl_bool_false
;
8014 is_params
= isl_union_set_is_params(mupa
->u
.dom
);
8015 if (is_params
< 0 || !is_params
)
8016 return isl_bool_not(is_params
);
8017 set
= isl_set_from_union_set(isl_union_set_copy(mupa
->u
.dom
));
8018 trivial
= isl_set_plain_is_universe(set
);
8020 return isl_bool_not(trivial
);
8023 /* Construct a multiple union piecewise affine expression
8024 * in the given space with value zero in each of the output dimensions.
8026 * Since there is no canonical zero value for
8027 * a union piecewise affine expression, we can only construct
8028 * a zero-dimensional "zero" value.
8030 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_zero(
8031 __isl_take isl_space
*space
)
8039 params
= isl_space_is_params(space
);
8043 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
8044 "expecting proper set space", goto error
);
8045 if (!isl_space_is_set(space
))
8046 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
8047 "expecting set space", goto error
);
8048 dim
= isl_space_dim(space
, isl_dim_out
);
8052 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
8053 "expecting 0D space", goto error
);
8055 return isl_multi_union_pw_aff_alloc(space
);
8057 isl_space_free(space
);
8061 /* Compute the sum of "mupa1" and "mupa2" on the union of their domains,
8062 * with the actual sum on the shared domain and
8063 * the defined expression on the symmetric difference of the domains.
8065 * We simply iterate over the elements in both arguments and
8066 * call isl_union_pw_aff_union_add on each of them, if there is
8067 * at least one element.
8069 * Otherwise, the two expressions have an explicit domain and
8070 * the union of these explicit domains is computed.
8071 * This assumes that the explicit domains are either both in terms
8072 * of specific domains elements or both in terms of parameters.
8073 * However, if one of the expressions does not have any constraints
8074 * on its explicit domain, then this is allowed as well and the result
8075 * is the expression with no constraints on its explicit domain.
8077 static __isl_give isl_multi_union_pw_aff
*
8078 isl_multi_union_pw_aff_union_add_aligned(
8079 __isl_take isl_multi_union_pw_aff
*mupa1
,
8080 __isl_take isl_multi_union_pw_aff
*mupa2
)
8082 isl_bool has_domain
, is_params1
, is_params2
;
8084 if (isl_multi_union_pw_aff_check_equal_space(mupa1
, mupa2
) < 0)
8087 return isl_multi_union_pw_aff_bin_op(mupa1
, mupa2
,
8088 &isl_union_pw_aff_union_add
);
8089 if (isl_multi_union_pw_aff_check_has_explicit_domain(mupa1
) < 0 ||
8090 isl_multi_union_pw_aff_check_has_explicit_domain(mupa2
) < 0)
8093 has_domain
= isl_multi_union_pw_aff_has_non_trivial_domain(mupa1
);
8097 isl_multi_union_pw_aff_free(mupa2
);
8100 has_domain
= isl_multi_union_pw_aff_has_non_trivial_domain(mupa2
);
8104 isl_multi_union_pw_aff_free(mupa1
);
8108 is_params1
= isl_union_set_is_params(mupa1
->u
.dom
);
8109 is_params2
= isl_union_set_is_params(mupa2
->u
.dom
);
8110 if (is_params1
< 0 || is_params2
< 0)
8112 if (is_params1
!= is_params2
)
8113 isl_die(isl_multi_union_pw_aff_get_ctx(mupa1
),
8115 "cannot compute union of concrete domain and "
8116 "parameter constraints", goto error
);
8117 mupa1
= isl_multi_union_pw_aff_cow(mupa1
);
8120 mupa1
->u
.dom
= isl_union_set_union(mupa1
->u
.dom
,
8121 isl_union_set_copy(mupa2
->u
.dom
));
8124 isl_multi_union_pw_aff_free(mupa2
);
8127 isl_multi_union_pw_aff_free(mupa1
);
8128 isl_multi_union_pw_aff_free(mupa2
);
8132 /* Compute the sum of "mupa1" and "mupa2" on the union of their domains,
8133 * with the actual sum on the shared domain and
8134 * the defined expression on the symmetric difference of the domains.
8136 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_union_add(
8137 __isl_take isl_multi_union_pw_aff
*mupa1
,
8138 __isl_take isl_multi_union_pw_aff
*mupa2
)
8140 return isl_multi_union_pw_aff_align_params_multi_multi_and(mupa1
, mupa2
,
8141 &isl_multi_union_pw_aff_union_add_aligned
);
8144 /* Construct and return a multi union piecewise affine expression
8145 * that is equal to the given multi affine expression.
8147 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_from_multi_aff(
8148 __isl_take isl_multi_aff
*ma
)
8150 isl_multi_pw_aff
*mpa
;
8152 mpa
= isl_multi_pw_aff_from_multi_aff(ma
);
8153 return isl_multi_union_pw_aff_from_multi_pw_aff(mpa
);
8156 /* Construct and return a multi union piecewise affine expression
8157 * that is equal to the given multi piecewise affine expression.
8159 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_from_multi_pw_aff(
8160 __isl_take isl_multi_pw_aff
*mpa
)
8165 isl_multi_union_pw_aff
*mupa
;
8167 n
= isl_multi_pw_aff_dim(mpa
, isl_dim_out
);
8169 mpa
= isl_multi_pw_aff_free(mpa
);
8173 space
= isl_multi_pw_aff_get_space(mpa
);
8174 space
= isl_space_range(space
);
8175 mupa
= isl_multi_union_pw_aff_alloc(space
);
8177 for (i
= 0; i
< n
; ++i
) {
8179 isl_union_pw_aff
*upa
;
8181 pa
= isl_multi_pw_aff_get_pw_aff(mpa
, i
);
8182 upa
= isl_union_pw_aff_from_pw_aff(pa
);
8183 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
8186 isl_multi_pw_aff_free(mpa
);
8191 /* Extract the range space of "pma" and assign it to *space.
8192 * If *space has already been set (through a previous call to this function),
8193 * then check that the range space is the same.
8195 static isl_stat
extract_space(__isl_take isl_pw_multi_aff
*pma
, void *user
)
8197 isl_space
**space
= user
;
8198 isl_space
*pma_space
;
8201 pma_space
= isl_space_range(isl_pw_multi_aff_get_space(pma
));
8202 isl_pw_multi_aff_free(pma
);
8205 return isl_stat_error
;
8211 equal
= isl_space_is_equal(pma_space
, *space
);
8212 isl_space_free(pma_space
);
8215 return isl_stat_error
;
8217 isl_die(isl_space_get_ctx(*space
), isl_error_invalid
,
8218 "range spaces not the same", return isl_stat_error
);
8222 /* Construct and return a multi union piecewise affine expression
8223 * that is equal to the given union piecewise multi affine expression.
8225 * In order to be able to perform the conversion, the input
8226 * needs to be non-empty and may only involve a single range space.
8228 * If the resulting multi union piecewise affine expression has
8229 * an explicit domain, then assign it the domain of the input.
8230 * In other cases, the domain is stored in the individual elements.
8232 __isl_give isl_multi_union_pw_aff
*
8233 isl_multi_union_pw_aff_from_union_pw_multi_aff(
8234 __isl_take isl_union_pw_multi_aff
*upma
)
8236 isl_space
*space
= NULL
;
8237 isl_multi_union_pw_aff
*mupa
;
8241 n
= isl_union_pw_multi_aff_n_pw_multi_aff(upma
);
8245 isl_die(isl_union_pw_multi_aff_get_ctx(upma
), isl_error_invalid
,
8246 "cannot extract range space from empty input",
8248 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma
, &extract_space
,
8255 n
= isl_space_dim(space
, isl_dim_set
);
8257 space
= isl_space_free(space
);
8258 mupa
= isl_multi_union_pw_aff_alloc(space
);
8260 for (i
= 0; i
< n
; ++i
) {
8261 isl_union_pw_aff
*upa
;
8263 upa
= isl_union_pw_multi_aff_get_union_pw_aff(upma
, i
);
8264 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
8266 if (isl_multi_union_pw_aff_has_explicit_domain(mupa
)) {
8268 isl_union_pw_multi_aff
*copy
;
8270 copy
= isl_union_pw_multi_aff_copy(upma
);
8271 dom
= isl_union_pw_multi_aff_domain(copy
);
8272 mupa
= isl_multi_union_pw_aff_intersect_domain(mupa
, dom
);
8275 isl_union_pw_multi_aff_free(upma
);
8278 isl_space_free(space
);
8279 isl_union_pw_multi_aff_free(upma
);
8283 /* Try and create an isl_multi_union_pw_aff that is equivalent
8284 * to the given isl_union_map.
8285 * The isl_union_map is required to be single-valued in each space.
8286 * Moreover, it cannot be empty and all range spaces need to be the same.
8287 * Otherwise, an error is produced.
8289 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_from_union_map(
8290 __isl_take isl_union_map
*umap
)
8292 isl_union_pw_multi_aff
*upma
;
8294 upma
= isl_union_pw_multi_aff_from_union_map(umap
);
8295 return isl_multi_union_pw_aff_from_union_pw_multi_aff(upma
);
8298 /* Return a multiple union piecewise affine expression
8299 * that is equal to "mv" on "domain", assuming "domain" and "mv"
8300 * have been aligned.
8302 * If the resulting multi union piecewise affine expression has
8303 * an explicit domain, then assign it the input domain.
8304 * In other cases, the domain is stored in the individual elements.
8306 static __isl_give isl_multi_union_pw_aff
*
8307 isl_multi_union_pw_aff_multi_val_on_domain_aligned(
8308 __isl_take isl_union_set
*domain
, __isl_take isl_multi_val
*mv
)
8313 isl_multi_union_pw_aff
*mupa
;
8315 n
= isl_multi_val_dim(mv
, isl_dim_set
);
8316 if (!domain
|| n
< 0)
8319 space
= isl_multi_val_get_space(mv
);
8320 mupa
= isl_multi_union_pw_aff_alloc(space
);
8321 for (i
= 0; i
< n
; ++i
) {
8323 isl_union_pw_aff
*upa
;
8325 v
= isl_multi_val_get_val(mv
, i
);
8326 upa
= isl_union_pw_aff_val_on_domain(isl_union_set_copy(domain
),
8328 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
8330 if (isl_multi_union_pw_aff_has_explicit_domain(mupa
))
8331 mupa
= isl_multi_union_pw_aff_intersect_domain(mupa
,
8332 isl_union_set_copy(domain
));
8334 isl_union_set_free(domain
);
8335 isl_multi_val_free(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 "mv" on "domain".
8346 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_multi_val_on_domain(
8347 __isl_take isl_union_set
*domain
, __isl_take isl_multi_val
*mv
)
8349 isl_bool equal_params
;
8353 equal_params
= isl_space_has_equal_params(domain
->dim
, mv
->space
);
8354 if (equal_params
< 0)
8357 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(
8359 domain
= isl_union_set_align_params(domain
,
8360 isl_multi_val_get_space(mv
));
8361 mv
= isl_multi_val_align_params(mv
, isl_union_set_get_space(domain
));
8362 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(domain
, mv
);
8364 isl_union_set_free(domain
);
8365 isl_multi_val_free(mv
);
8369 /* Return a multiple union piecewise affine expression
8370 * that is equal to "ma" on "domain".
8372 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_multi_aff_on_domain(
8373 __isl_take isl_union_set
*domain
, __isl_take isl_multi_aff
*ma
)
8375 isl_pw_multi_aff
*pma
;
8377 pma
= isl_pw_multi_aff_from_multi_aff(ma
);
8378 return isl_multi_union_pw_aff_pw_multi_aff_on_domain(domain
, pma
);
8381 /* Return a multiple union piecewise affine expression
8382 * that is equal to "pma" on "domain", assuming "domain" and "pma"
8383 * have been aligned.
8385 * If the resulting multi union piecewise affine expression has
8386 * an explicit domain, then assign it the input domain.
8387 * In other cases, the domain is stored in the individual elements.
8389 static __isl_give isl_multi_union_pw_aff
*
8390 isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(
8391 __isl_take isl_union_set
*domain
, __isl_take isl_pw_multi_aff
*pma
)
8396 isl_multi_union_pw_aff
*mupa
;
8398 n
= isl_pw_multi_aff_dim(pma
, isl_dim_set
);
8399 if (!domain
|| n
< 0)
8401 space
= isl_pw_multi_aff_get_space(pma
);
8402 mupa
= isl_multi_union_pw_aff_alloc(space
);
8403 for (i
= 0; i
< n
; ++i
) {
8405 isl_union_pw_aff
*upa
;
8407 pa
= isl_pw_multi_aff_get_pw_aff(pma
, i
);
8408 upa
= isl_union_pw_aff_pw_aff_on_domain(
8409 isl_union_set_copy(domain
), pa
);
8410 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
8412 if (isl_multi_union_pw_aff_has_explicit_domain(mupa
))
8413 mupa
= isl_multi_union_pw_aff_intersect_domain(mupa
,
8414 isl_union_set_copy(domain
));
8416 isl_union_set_free(domain
);
8417 isl_pw_multi_aff_free(pma
);
8420 isl_union_set_free(domain
);
8421 isl_pw_multi_aff_free(pma
);
8425 /* Return a multiple union piecewise affine expression
8426 * that is equal to "pma" on "domain".
8428 __isl_give isl_multi_union_pw_aff
*
8429 isl_multi_union_pw_aff_pw_multi_aff_on_domain(__isl_take isl_union_set
*domain
,
8430 __isl_take isl_pw_multi_aff
*pma
)
8432 isl_bool equal_params
;
8435 space
= isl_pw_multi_aff_peek_space(pma
);
8436 equal_params
= isl_union_set_space_has_equal_params(domain
, space
);
8437 if (equal_params
< 0)
8440 return isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(
8442 domain
= isl_union_set_align_params(domain
,
8443 isl_pw_multi_aff_get_space(pma
));
8444 pma
= isl_pw_multi_aff_align_params(pma
,
8445 isl_union_set_get_space(domain
));
8446 return isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(domain
,
8449 isl_union_set_free(domain
);
8450 isl_pw_multi_aff_free(pma
);
8454 /* Return a union set containing those elements in the domains
8455 * of the elements of "mupa" where they are all zero.
8457 * If there are no elements, then simply return the entire domain.
8459 __isl_give isl_union_set
*isl_multi_union_pw_aff_zero_union_set(
8460 __isl_take isl_multi_union_pw_aff
*mupa
)
8464 isl_union_pw_aff
*upa
;
8465 isl_union_set
*zero
;
8467 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8469 mupa
= isl_multi_union_pw_aff_free(mupa
);
8474 return isl_multi_union_pw_aff_domain(mupa
);
8476 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, 0);
8477 zero
= isl_union_pw_aff_zero_union_set(upa
);
8479 for (i
= 1; i
< n
; ++i
) {
8480 isl_union_set
*zero_i
;
8482 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
8483 zero_i
= isl_union_pw_aff_zero_union_set(upa
);
8485 zero
= isl_union_set_intersect(zero
, zero_i
);
8488 isl_multi_union_pw_aff_free(mupa
);
8492 /* Construct a union map mapping the shared domain
8493 * of the union piecewise affine expressions to the range of "mupa"
8494 * in the special case of a 0D multi union piecewise affine expression.
8496 * Construct a map between the explicit domain of "mupa" and
8498 * Note that this assumes that the domain consists of explicit elements.
8500 static __isl_give isl_union_map
*isl_union_map_from_multi_union_pw_aff_0D(
8501 __isl_take isl_multi_union_pw_aff
*mupa
)
8505 isl_union_set
*dom
, *ran
;
8507 space
= isl_multi_union_pw_aff_get_space(mupa
);
8508 dom
= isl_multi_union_pw_aff_domain(mupa
);
8509 ran
= isl_union_set_from_set(isl_set_universe(space
));
8511 is_params
= isl_union_set_is_params(dom
);
8513 dom
= isl_union_set_free(dom
);
8515 isl_die(isl_union_set_get_ctx(dom
), isl_error_invalid
,
8516 "cannot create union map from expression without "
8517 "explicit domain elements",
8518 dom
= isl_union_set_free(dom
));
8520 return isl_union_map_from_domain_and_range(dom
, ran
);
8523 /* Construct a union map mapping the shared domain
8524 * of the union piecewise affine expressions to the range of "mupa"
8525 * with each dimension in the range equated to the
8526 * corresponding union piecewise affine expression.
8528 * If the input is zero-dimensional, then construct a mapping
8529 * from its explicit domain.
8531 __isl_give isl_union_map
*isl_union_map_from_multi_union_pw_aff(
8532 __isl_take isl_multi_union_pw_aff
*mupa
)
8537 isl_union_map
*umap
;
8538 isl_union_pw_aff
*upa
;
8540 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8542 mupa
= isl_multi_union_pw_aff_free(mupa
);
8547 return isl_union_map_from_multi_union_pw_aff_0D(mupa
);
8549 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, 0);
8550 umap
= isl_union_map_from_union_pw_aff(upa
);
8552 for (i
= 1; i
< n
; ++i
) {
8553 isl_union_map
*umap_i
;
8555 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
8556 umap_i
= isl_union_map_from_union_pw_aff(upa
);
8557 umap
= isl_union_map_flat_range_product(umap
, umap_i
);
8560 space
= isl_multi_union_pw_aff_get_space(mupa
);
8561 umap
= isl_union_map_reset_range_space(umap
, space
);
8563 isl_multi_union_pw_aff_free(mupa
);
8567 /* Internal data structure for isl_union_pw_multi_aff_reset_range_space.
8568 * "range" is the space from which to set the range space.
8569 * "res" collects the results.
8571 struct isl_union_pw_multi_aff_reset_range_space_data
{
8573 isl_union_pw_multi_aff
*res
;
8576 /* Replace the range space of "pma" by the range space of data->range and
8577 * add the result to data->res.
8579 static isl_stat
reset_range_space(__isl_take isl_pw_multi_aff
*pma
, void *user
)
8581 struct isl_union_pw_multi_aff_reset_range_space_data
*data
= user
;
8584 space
= isl_pw_multi_aff_get_space(pma
);
8585 space
= isl_space_domain(space
);
8586 space
= isl_space_extend_domain_with_range(space
,
8587 isl_space_copy(data
->range
));
8588 pma
= isl_pw_multi_aff_reset_space(pma
, space
);
8589 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma
);
8591 return data
->res
? isl_stat_ok
: isl_stat_error
;
8594 /* Replace the range space of all the piecewise affine expressions in "upma" by
8595 * the range space of "space".
8597 * This assumes that all these expressions have the same output dimension.
8599 * Since the spaces of the expressions change, so do their hash values.
8600 * We therefore need to create a new isl_union_pw_multi_aff.
8601 * Note that the hash value is currently computed based on the entire
8602 * space even though there can only be a single expression with a given
8605 static __isl_give isl_union_pw_multi_aff
*
8606 isl_union_pw_multi_aff_reset_range_space(
8607 __isl_take isl_union_pw_multi_aff
*upma
, __isl_take isl_space
*space
)
8609 struct isl_union_pw_multi_aff_reset_range_space_data data
= { space
};
8610 isl_space
*space_upma
;
8612 space_upma
= isl_union_pw_multi_aff_get_space(upma
);
8613 data
.res
= isl_union_pw_multi_aff_empty(space_upma
);
8614 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma
,
8615 &reset_range_space
, &data
) < 0)
8616 data
.res
= isl_union_pw_multi_aff_free(data
.res
);
8618 isl_space_free(space
);
8619 isl_union_pw_multi_aff_free(upma
);
8623 /* Construct and return a union piecewise multi affine expression
8624 * that is equal to the given multi union piecewise affine expression,
8625 * in the special case of a 0D multi union piecewise affine expression.
8627 * Construct a union piecewise multi affine expression
8628 * on top of the explicit domain of the input.
8630 __isl_give isl_union_pw_multi_aff
*
8631 isl_union_pw_multi_aff_from_multi_union_pw_aff_0D(
8632 __isl_take isl_multi_union_pw_aff
*mupa
)
8636 isl_union_set
*domain
;
8638 space
= isl_multi_union_pw_aff_get_space(mupa
);
8639 mv
= isl_multi_val_zero(space
);
8640 domain
= isl_multi_union_pw_aff_domain(mupa
);
8641 return isl_union_pw_multi_aff_multi_val_on_domain(domain
, mv
);
8644 /* Construct and return a union piecewise multi affine expression
8645 * that is equal to the given multi union piecewise affine expression.
8647 * If the input is zero-dimensional, then
8648 * construct a union piecewise multi affine expression
8649 * on top of the explicit domain of the input.
8651 __isl_give isl_union_pw_multi_aff
*
8652 isl_union_pw_multi_aff_from_multi_union_pw_aff(
8653 __isl_take isl_multi_union_pw_aff
*mupa
)
8658 isl_union_pw_multi_aff
*upma
;
8659 isl_union_pw_aff
*upa
;
8661 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8663 mupa
= isl_multi_union_pw_aff_free(mupa
);
8668 return isl_union_pw_multi_aff_from_multi_union_pw_aff_0D(mupa
);
8670 space
= isl_multi_union_pw_aff_get_space(mupa
);
8671 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, 0);
8672 upma
= isl_union_pw_multi_aff_from_union_pw_aff(upa
);
8674 for (i
= 1; i
< n
; ++i
) {
8675 isl_union_pw_multi_aff
*upma_i
;
8677 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
8678 upma_i
= isl_union_pw_multi_aff_from_union_pw_aff(upa
);
8679 upma
= isl_union_pw_multi_aff_flat_range_product(upma
, upma_i
);
8682 upma
= isl_union_pw_multi_aff_reset_range_space(upma
, space
);
8684 isl_multi_union_pw_aff_free(mupa
);
8688 /* Intersect the range of "mupa" with "range",
8689 * in the special case where "mupa" is 0D.
8691 * Intersect the domain of "mupa" with the constraints on the parameters
8694 static __isl_give isl_multi_union_pw_aff
*mupa_intersect_range_0D(
8695 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_set
*range
)
8697 range
= isl_set_params(range
);
8698 mupa
= isl_multi_union_pw_aff_intersect_params(mupa
, range
);
8702 /* Intersect the range of "mupa" with "range".
8703 * That is, keep only those domain elements that have a function value
8706 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_intersect_range(
8707 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_set
*range
)
8709 isl_union_pw_multi_aff
*upma
;
8710 isl_union_set
*domain
;
8715 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8716 if (n
< 0 || !range
)
8719 space
= isl_set_get_space(range
);
8720 match
= isl_space_tuple_is_equal(mupa
->space
, isl_dim_set
,
8721 space
, isl_dim_set
);
8722 isl_space_free(space
);
8726 isl_die(isl_multi_union_pw_aff_get_ctx(mupa
), isl_error_invalid
,
8727 "space don't match", goto error
);
8729 return mupa_intersect_range_0D(mupa
, range
);
8731 upma
= isl_union_pw_multi_aff_from_multi_union_pw_aff(
8732 isl_multi_union_pw_aff_copy(mupa
));
8733 domain
= isl_union_set_from_set(range
);
8734 domain
= isl_union_set_preimage_union_pw_multi_aff(domain
, upma
);
8735 mupa
= isl_multi_union_pw_aff_intersect_domain(mupa
, domain
);
8739 isl_multi_union_pw_aff_free(mupa
);
8740 isl_set_free(range
);
8744 /* Return the shared domain of the elements of "mupa",
8745 * in the special case where "mupa" is zero-dimensional.
8747 * Return the explicit domain of "mupa".
8748 * Note that this domain may be a parameter set, either
8749 * because "mupa" is meant to live in a set space or
8750 * because no explicit domain has been set.
8752 __isl_give isl_union_set
*isl_multi_union_pw_aff_domain_0D(
8753 __isl_take isl_multi_union_pw_aff
*mupa
)
8757 dom
= isl_multi_union_pw_aff_get_explicit_domain(mupa
);
8758 isl_multi_union_pw_aff_free(mupa
);
8763 /* Return the shared domain of the elements of "mupa".
8765 * If "mupa" is zero-dimensional, then return its explicit domain.
8767 __isl_give isl_union_set
*isl_multi_union_pw_aff_domain(
8768 __isl_take isl_multi_union_pw_aff
*mupa
)
8772 isl_union_pw_aff
*upa
;
8775 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8777 mupa
= isl_multi_union_pw_aff_free(mupa
);
8782 return isl_multi_union_pw_aff_domain_0D(mupa
);
8784 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, 0);
8785 dom
= isl_union_pw_aff_domain(upa
);
8786 for (i
= 1; i
< n
; ++i
) {
8787 isl_union_set
*dom_i
;
8789 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
8790 dom_i
= isl_union_pw_aff_domain(upa
);
8791 dom
= isl_union_set_intersect(dom
, dom_i
);
8794 isl_multi_union_pw_aff_free(mupa
);
8798 /* Apply "aff" to "mupa". The space of "mupa" is equal to the domain of "aff".
8799 * In particular, the spaces have been aligned.
8800 * The result is defined over the shared domain of the elements of "mupa"
8802 * We first extract the parametric constant part of "aff" and
8803 * define that over the shared domain.
8804 * Then we iterate over all input dimensions of "aff" and add the corresponding
8805 * multiples of the elements of "mupa".
8806 * Finally, we consider the integer divisions, calling the function
8807 * recursively to obtain an isl_union_pw_aff corresponding to the
8808 * integer division argument.
8810 static __isl_give isl_union_pw_aff
*multi_union_pw_aff_apply_aff(
8811 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_aff
*aff
)
8814 isl_size n_in
, n_div
;
8815 isl_union_pw_aff
*upa
;
8816 isl_union_set
*uset
;
8820 n_in
= isl_aff_dim(aff
, isl_dim_in
);
8821 n_div
= isl_aff_dim(aff
, isl_dim_div
);
8822 if (n_in
< 0 || n_div
< 0)
8825 uset
= isl_multi_union_pw_aff_domain(isl_multi_union_pw_aff_copy(mupa
));
8826 cst
= isl_aff_copy(aff
);
8827 cst
= isl_aff_drop_dims(cst
, isl_dim_div
, 0, n_div
);
8828 cst
= isl_aff_drop_dims(cst
, isl_dim_in
, 0, n_in
);
8829 cst
= isl_aff_project_domain_on_params(cst
);
8830 upa
= isl_union_pw_aff_aff_on_domain(uset
, cst
);
8832 for (i
= 0; i
< n_in
; ++i
) {
8833 isl_union_pw_aff
*upa_i
;
8835 if (!isl_aff_involves_dims(aff
, isl_dim_in
, i
, 1))
8837 v
= isl_aff_get_coefficient_val(aff
, isl_dim_in
, i
);
8838 upa_i
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
8839 upa_i
= isl_union_pw_aff_scale_val(upa_i
, v
);
8840 upa
= isl_union_pw_aff_add(upa
, upa_i
);
8843 for (i
= 0; i
< n_div
; ++i
) {
8845 isl_union_pw_aff
*upa_i
;
8847 if (!isl_aff_involves_dims(aff
, isl_dim_div
, i
, 1))
8849 div
= isl_aff_get_div(aff
, i
);
8850 upa_i
= multi_union_pw_aff_apply_aff(
8851 isl_multi_union_pw_aff_copy(mupa
), div
);
8852 upa_i
= isl_union_pw_aff_floor(upa_i
);
8853 v
= isl_aff_get_coefficient_val(aff
, isl_dim_div
, i
);
8854 upa_i
= isl_union_pw_aff_scale_val(upa_i
, v
);
8855 upa
= isl_union_pw_aff_add(upa
, upa_i
);
8858 isl_multi_union_pw_aff_free(mupa
);
8863 isl_multi_union_pw_aff_free(mupa
);
8868 /* Apply "aff" to "mupa". The space of "mupa" needs to be compatible
8869 * with the domain of "aff".
8870 * Furthermore, the dimension of this space needs to be greater than zero.
8871 * The result is defined over the shared domain of the elements of "mupa"
8873 * We perform these checks and then hand over control to
8874 * multi_union_pw_aff_apply_aff.
8876 __isl_give isl_union_pw_aff
*isl_multi_union_pw_aff_apply_aff(
8877 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_aff
*aff
)
8880 isl_space
*space1
, *space2
;
8883 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
8884 isl_aff_get_space(aff
));
8885 aff
= isl_aff_align_params(aff
, isl_multi_union_pw_aff_get_space(mupa
));
8889 space1
= isl_multi_union_pw_aff_get_space(mupa
);
8890 space2
= isl_aff_get_domain_space(aff
);
8891 equal
= isl_space_is_equal(space1
, space2
);
8892 isl_space_free(space1
);
8893 isl_space_free(space2
);
8897 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
8898 "spaces don't match", goto error
);
8899 dim
= isl_aff_dim(aff
, isl_dim_in
);
8903 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
8904 "cannot determine domains", goto error
);
8906 return multi_union_pw_aff_apply_aff(mupa
, aff
);
8908 isl_multi_union_pw_aff_free(mupa
);
8913 /* Apply "ma" to "mupa", in the special case where "mupa" is 0D.
8914 * The space of "mupa" is known to be compatible with the domain of "ma".
8916 * Construct an isl_multi_union_pw_aff that is equal to "ma"
8917 * on the domain of "mupa".
8919 static __isl_give isl_multi_union_pw_aff
*mupa_apply_multi_aff_0D(
8920 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_multi_aff
*ma
)
8924 dom
= isl_multi_union_pw_aff_domain(mupa
);
8925 ma
= isl_multi_aff_project_domain_on_params(ma
);
8927 return isl_multi_union_pw_aff_multi_aff_on_domain(dom
, ma
);
8930 /* Apply "ma" to "mupa". The space of "mupa" needs to be compatible
8931 * with the domain of "ma".
8932 * The result is defined over the shared domain of the elements of "mupa"
8934 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_apply_multi_aff(
8935 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_multi_aff
*ma
)
8937 isl_space
*space1
, *space2
;
8938 isl_multi_union_pw_aff
*res
;
8941 isl_size n_in
, n_out
;
8943 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
8944 isl_multi_aff_get_space(ma
));
8945 ma
= isl_multi_aff_align_params(ma
,
8946 isl_multi_union_pw_aff_get_space(mupa
));
8947 n_in
= isl_multi_aff_dim(ma
, isl_dim_in
);
8948 n_out
= isl_multi_aff_dim(ma
, isl_dim_out
);
8949 if (!mupa
|| n_in
< 0 || n_out
< 0)
8952 space1
= isl_multi_union_pw_aff_get_space(mupa
);
8953 space2
= isl_multi_aff_get_domain_space(ma
);
8954 equal
= isl_space_is_equal(space1
, space2
);
8955 isl_space_free(space1
);
8956 isl_space_free(space2
);
8960 isl_die(isl_multi_aff_get_ctx(ma
), isl_error_invalid
,
8961 "spaces don't match", goto error
);
8963 return mupa_apply_multi_aff_0D(mupa
, ma
);
8965 space1
= isl_space_range(isl_multi_aff_get_space(ma
));
8966 res
= isl_multi_union_pw_aff_alloc(space1
);
8968 for (i
= 0; i
< n_out
; ++i
) {
8970 isl_union_pw_aff
*upa
;
8972 aff
= isl_multi_aff_get_aff(ma
, i
);
8973 upa
= multi_union_pw_aff_apply_aff(
8974 isl_multi_union_pw_aff_copy(mupa
), aff
);
8975 res
= isl_multi_union_pw_aff_set_union_pw_aff(res
, i
, upa
);
8978 isl_multi_aff_free(ma
);
8979 isl_multi_union_pw_aff_free(mupa
);
8982 isl_multi_union_pw_aff_free(mupa
);
8983 isl_multi_aff_free(ma
);
8987 /* Apply "pa" to "mupa", in the special case where "mupa" is 0D.
8988 * The space of "mupa" is known to be compatible with the domain of "pa".
8990 * Construct an isl_multi_union_pw_aff that is equal to "pa"
8991 * on the domain of "mupa".
8993 static __isl_give isl_union_pw_aff
*isl_multi_union_pw_aff_apply_pw_aff_0D(
8994 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_pw_aff
*pa
)
8998 dom
= isl_multi_union_pw_aff_domain(mupa
);
8999 pa
= isl_pw_aff_project_domain_on_params(pa
);
9001 return isl_union_pw_aff_pw_aff_on_domain(dom
, pa
);
9004 /* Apply "pa" to "mupa". The space of "mupa" needs to be compatible
9005 * with the domain of "pa".
9006 * Furthermore, the dimension of this space needs to be greater than zero.
9007 * The result is defined over the shared domain of the elements of "mupa"
9009 __isl_give isl_union_pw_aff
*isl_multi_union_pw_aff_apply_pw_aff(
9010 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_pw_aff
*pa
)
9015 isl_space
*space
, *space2
;
9016 isl_union_pw_aff
*upa
;
9018 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
9019 isl_pw_aff_get_space(pa
));
9020 pa
= isl_pw_aff_align_params(pa
,
9021 isl_multi_union_pw_aff_get_space(mupa
));
9025 space
= isl_multi_union_pw_aff_get_space(mupa
);
9026 space2
= isl_pw_aff_get_domain_space(pa
);
9027 equal
= isl_space_is_equal(space
, space2
);
9028 isl_space_free(space
);
9029 isl_space_free(space2
);
9033 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
9034 "spaces don't match", goto error
);
9035 n_in
= isl_pw_aff_dim(pa
, isl_dim_in
);
9039 return isl_multi_union_pw_aff_apply_pw_aff_0D(mupa
, pa
);
9041 space
= isl_space_params(isl_multi_union_pw_aff_get_space(mupa
));
9042 upa
= isl_union_pw_aff_empty(space
);
9044 for (i
= 0; i
< pa
->n
; ++i
) {
9047 isl_multi_union_pw_aff
*mupa_i
;
9048 isl_union_pw_aff
*upa_i
;
9050 mupa_i
= isl_multi_union_pw_aff_copy(mupa
);
9051 domain
= isl_set_copy(pa
->p
[i
].set
);
9052 mupa_i
= isl_multi_union_pw_aff_intersect_range(mupa_i
, domain
);
9053 aff
= isl_aff_copy(pa
->p
[i
].aff
);
9054 upa_i
= multi_union_pw_aff_apply_aff(mupa_i
, aff
);
9055 upa
= isl_union_pw_aff_union_add(upa
, upa_i
);
9058 isl_multi_union_pw_aff_free(mupa
);
9059 isl_pw_aff_free(pa
);
9062 isl_multi_union_pw_aff_free(mupa
);
9063 isl_pw_aff_free(pa
);
9067 /* Apply "pma" to "mupa", in the special case where "mupa" is 0D.
9068 * The space of "mupa" is known to be compatible with the domain of "pma".
9070 * Construct an isl_multi_union_pw_aff that is equal to "pma"
9071 * on the domain of "mupa".
9073 static __isl_give isl_multi_union_pw_aff
*mupa_apply_pw_multi_aff_0D(
9074 __isl_take isl_multi_union_pw_aff
*mupa
,
9075 __isl_take isl_pw_multi_aff
*pma
)
9079 dom
= isl_multi_union_pw_aff_domain(mupa
);
9080 pma
= isl_pw_multi_aff_project_domain_on_params(pma
);
9082 return isl_multi_union_pw_aff_pw_multi_aff_on_domain(dom
, pma
);
9085 /* Apply "pma" to "mupa". The space of "mupa" needs to be compatible
9086 * with the domain of "pma".
9087 * The result is defined over the shared domain of the elements of "mupa"
9089 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_apply_pw_multi_aff(
9090 __isl_take isl_multi_union_pw_aff
*mupa
,
9091 __isl_take isl_pw_multi_aff
*pma
)
9093 isl_space
*space1
, *space2
;
9094 isl_multi_union_pw_aff
*res
;
9097 isl_size n_in
, n_out
;
9099 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
9100 isl_pw_multi_aff_get_space(pma
));
9101 pma
= isl_pw_multi_aff_align_params(pma
,
9102 isl_multi_union_pw_aff_get_space(mupa
));
9106 space1
= isl_multi_union_pw_aff_get_space(mupa
);
9107 space2
= isl_pw_multi_aff_get_domain_space(pma
);
9108 equal
= isl_space_is_equal(space1
, space2
);
9109 isl_space_free(space1
);
9110 isl_space_free(space2
);
9114 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
9115 "spaces don't match", goto error
);
9116 n_in
= isl_pw_multi_aff_dim(pma
, isl_dim_in
);
9117 n_out
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
9118 if (n_in
< 0 || n_out
< 0)
9121 return mupa_apply_pw_multi_aff_0D(mupa
, pma
);
9123 space1
= isl_space_range(isl_pw_multi_aff_get_space(pma
));
9124 res
= isl_multi_union_pw_aff_alloc(space1
);
9126 for (i
= 0; i
< n_out
; ++i
) {
9128 isl_union_pw_aff
*upa
;
9130 pa
= isl_pw_multi_aff_get_pw_aff(pma
, i
);
9131 upa
= isl_multi_union_pw_aff_apply_pw_aff(
9132 isl_multi_union_pw_aff_copy(mupa
), pa
);
9133 res
= isl_multi_union_pw_aff_set_union_pw_aff(res
, i
, upa
);
9136 isl_pw_multi_aff_free(pma
);
9137 isl_multi_union_pw_aff_free(mupa
);
9140 isl_multi_union_pw_aff_free(mupa
);
9141 isl_pw_multi_aff_free(pma
);
9145 /* Replace the explicit domain of "mupa" by its preimage under "upma".
9146 * If the explicit domain only keeps track of constraints on the parameters,
9147 * then only update those constraints.
9149 static __isl_give isl_multi_union_pw_aff
*preimage_explicit_domain(
9150 __isl_take isl_multi_union_pw_aff
*mupa
,
9151 __isl_keep isl_union_pw_multi_aff
*upma
)
9155 if (isl_multi_union_pw_aff_check_has_explicit_domain(mupa
) < 0)
9156 return isl_multi_union_pw_aff_free(mupa
);
9158 mupa
= isl_multi_union_pw_aff_cow(mupa
);
9162 is_params
= isl_union_set_is_params(mupa
->u
.dom
);
9164 return isl_multi_union_pw_aff_free(mupa
);
9166 upma
= isl_union_pw_multi_aff_copy(upma
);
9168 mupa
->u
.dom
= isl_union_set_intersect_params(mupa
->u
.dom
,
9169 isl_union_set_params(isl_union_pw_multi_aff_domain(upma
)));
9171 mupa
->u
.dom
= isl_union_set_preimage_union_pw_multi_aff(
9174 return isl_multi_union_pw_aff_free(mupa
);
9178 /* Compute the pullback of "mupa" by the function represented by "upma".
9179 * In other words, plug in "upma" in "mupa". The result contains
9180 * expressions defined over the domain space of "upma".
9182 * Run over all elements of "mupa" and plug in "upma" in each of them.
9184 * If "mupa" has an explicit domain, then it is this domain
9185 * that needs to undergo a pullback instead, i.e., a preimage.
9187 __isl_give isl_multi_union_pw_aff
*
9188 isl_multi_union_pw_aff_pullback_union_pw_multi_aff(
9189 __isl_take isl_multi_union_pw_aff
*mupa
,
9190 __isl_take isl_union_pw_multi_aff
*upma
)
9195 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
9196 isl_union_pw_multi_aff_get_space(upma
));
9197 upma
= isl_union_pw_multi_aff_align_params(upma
,
9198 isl_multi_union_pw_aff_get_space(mupa
));
9199 mupa
= isl_multi_union_pw_aff_cow(mupa
);
9200 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
9204 for (i
= 0; i
< n
; ++i
) {
9205 isl_union_pw_aff
*upa
;
9207 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
9208 upa
= isl_union_pw_aff_pullback_union_pw_multi_aff(upa
,
9209 isl_union_pw_multi_aff_copy(upma
));
9210 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
9213 if (isl_multi_union_pw_aff_has_explicit_domain(mupa
))
9214 mupa
= preimage_explicit_domain(mupa
, upma
);
9216 isl_union_pw_multi_aff_free(upma
);
9219 isl_multi_union_pw_aff_free(mupa
);
9220 isl_union_pw_multi_aff_free(upma
);
9224 /* Extract the sequence of elements in "mupa" with domain space "space"
9225 * (ignoring parameters).
9227 * For the elements of "mupa" that are not defined on the specified space,
9228 * the corresponding element in the result is empty.
9230 __isl_give isl_multi_pw_aff
*isl_multi_union_pw_aff_extract_multi_pw_aff(
9231 __isl_keep isl_multi_union_pw_aff
*mupa
, __isl_take isl_space
*space
)
9235 isl_space
*space_mpa
;
9236 isl_multi_pw_aff
*mpa
;
9238 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
9239 if (n
< 0 || !space
)
9242 space_mpa
= isl_multi_union_pw_aff_get_space(mupa
);
9243 space
= isl_space_replace_params(space
, space_mpa
);
9244 space_mpa
= isl_space_map_from_domain_and_range(isl_space_copy(space
),
9246 mpa
= isl_multi_pw_aff_alloc(space_mpa
);
9248 space
= isl_space_from_domain(space
);
9249 space
= isl_space_add_dims(space
, isl_dim_out
, 1);
9250 for (i
= 0; i
< n
; ++i
) {
9251 isl_union_pw_aff
*upa
;
9254 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
9255 pa
= isl_union_pw_aff_extract_pw_aff(upa
,
9256 isl_space_copy(space
));
9257 mpa
= isl_multi_pw_aff_set_pw_aff(mpa
, i
, pa
);
9258 isl_union_pw_aff_free(upa
);
9261 isl_space_free(space
);
9264 isl_space_free(space
);
9268 /* Evaluate the affine function "aff" in the void point "pnt".
9269 * In particular, return the value NaN.
9271 static __isl_give isl_val
*eval_void(__isl_take isl_aff
*aff
,
9272 __isl_take isl_point
*pnt
)
9276 ctx
= isl_point_get_ctx(pnt
);
9278 isl_point_free(pnt
);
9279 return isl_val_nan(ctx
);
9282 /* Evaluate the affine expression "aff"
9283 * in the coordinates (with denominator) "pnt".
9285 static __isl_give isl_val
*eval(__isl_keep isl_vec
*aff
,
9286 __isl_keep isl_vec
*pnt
)
9295 ctx
= isl_vec_get_ctx(aff
);
9298 isl_seq_inner_product(aff
->el
+ 1, pnt
->el
, pnt
->size
, &n
);
9299 isl_int_mul(d
, aff
->el
[0], pnt
->el
[0]);
9300 v
= isl_val_rat_from_isl_int(ctx
, n
, d
);
9301 v
= isl_val_normalize(v
);
9308 /* Check that the domain space of "aff" is equal to "space".
9310 static isl_stat
isl_aff_check_has_domain_space(__isl_keep isl_aff
*aff
,
9311 __isl_keep isl_space
*space
)
9315 ok
= isl_space_is_equal(isl_aff_peek_domain_space(aff
), space
);
9317 return isl_stat_error
;
9319 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
9320 "incompatible spaces", return isl_stat_error
);
9324 /* Evaluate the affine function "aff" in "pnt".
9326 __isl_give isl_val
*isl_aff_eval(__isl_take isl_aff
*aff
,
9327 __isl_take isl_point
*pnt
)
9331 isl_local_space
*ls
;
9333 if (isl_aff_check_has_domain_space(aff
, isl_point_peek_space(pnt
)) < 0)
9335 is_void
= isl_point_is_void(pnt
);
9339 return eval_void(aff
, pnt
);
9341 ls
= isl_aff_get_domain_local_space(aff
);
9342 pnt
= isl_local_space_lift_point(ls
, pnt
);
9344 v
= eval(aff
->v
, isl_point_peek_vec(pnt
));
9347 isl_point_free(pnt
);
9352 isl_point_free(pnt
);