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
)
592 return isl_bool_error
;
594 if (isl_int_is_zero(aff
->v
->el
[0]))
595 return isl_bool_false
;
596 pos
= isl_seq_first_non_zero(aff
->v
->el
+ 1, aff
->v
->size
- 1);
597 return isl_bool_ok(pos
< 0);
600 /* Does "aff" represent NaN?
602 isl_bool
isl_aff_is_nan(__isl_keep isl_aff
*aff
)
605 return isl_bool_error
;
607 return isl_bool_ok(isl_seq_first_non_zero(aff
->v
->el
, 2) < 0);
610 /* Are "aff1" and "aff2" obviously equal?
612 * NaN is not equal to anything, not even to another NaN.
614 isl_bool
isl_aff_plain_is_equal(__isl_keep isl_aff
*aff1
,
615 __isl_keep isl_aff
*aff2
)
620 return isl_bool_error
;
622 if (isl_aff_is_nan(aff1
) || isl_aff_is_nan(aff2
))
623 return isl_bool_false
;
625 equal
= isl_local_space_is_equal(aff1
->ls
, aff2
->ls
);
626 if (equal
< 0 || !equal
)
629 return isl_vec_is_equal(aff1
->v
, aff2
->v
);
632 /* Return the common denominator of "aff" in "v".
634 * We cannot return anything meaningful in case of a NaN.
636 isl_stat
isl_aff_get_denominator(__isl_keep isl_aff
*aff
, isl_int
*v
)
639 return isl_stat_error
;
640 if (isl_aff_is_nan(aff
))
641 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
642 "cannot get denominator of NaN", return isl_stat_error
);
643 isl_int_set(*v
, aff
->v
->el
[0]);
647 /* Return the common denominator of "aff".
649 __isl_give isl_val
*isl_aff_get_denominator_val(__isl_keep isl_aff
*aff
)
656 ctx
= isl_aff_get_ctx(aff
);
657 if (isl_aff_is_nan(aff
))
658 return isl_val_nan(ctx
);
659 return isl_val_int_from_isl_int(ctx
, aff
->v
->el
[0]);
662 /* Return the constant term of "aff".
664 __isl_give isl_val
*isl_aff_get_constant_val(__isl_keep isl_aff
*aff
)
672 ctx
= isl_aff_get_ctx(aff
);
673 if (isl_aff_is_nan(aff
))
674 return isl_val_nan(ctx
);
675 v
= isl_val_rat_from_isl_int(ctx
, aff
->v
->el
[1], aff
->v
->el
[0]);
676 return isl_val_normalize(v
);
679 /* Return the coefficient of the variable of type "type" at position "pos"
682 __isl_give isl_val
*isl_aff_get_coefficient_val(__isl_keep isl_aff
*aff
,
683 enum isl_dim_type type
, int pos
)
691 ctx
= isl_aff_get_ctx(aff
);
692 if (type
== isl_dim_out
)
693 isl_die(ctx
, isl_error_invalid
,
694 "output/set dimension does not have a coefficient",
696 if (type
== isl_dim_in
)
699 if (isl_local_space_check_range(aff
->ls
, type
, pos
, 1) < 0)
702 if (isl_aff_is_nan(aff
))
703 return isl_val_nan(ctx
);
704 pos
+= isl_local_space_offset(aff
->ls
, type
);
705 v
= isl_val_rat_from_isl_int(ctx
, aff
->v
->el
[1 + pos
], aff
->v
->el
[0]);
706 return isl_val_normalize(v
);
709 /* Return the sign of the coefficient of the variable of type "type"
710 * at position "pos" of "aff".
712 int isl_aff_coefficient_sgn(__isl_keep isl_aff
*aff
, enum isl_dim_type type
,
720 ctx
= isl_aff_get_ctx(aff
);
721 if (type
== isl_dim_out
)
722 isl_die(ctx
, isl_error_invalid
,
723 "output/set dimension does not have a coefficient",
725 if (type
== isl_dim_in
)
728 if (isl_local_space_check_range(aff
->ls
, type
, pos
, 1) < 0)
731 pos
+= isl_local_space_offset(aff
->ls
, type
);
732 return isl_int_sgn(aff
->v
->el
[1 + pos
]);
735 /* Replace the numerator of the constant term of "aff" by "v".
737 * A NaN is unaffected by this operation.
739 __isl_give isl_aff
*isl_aff_set_constant(__isl_take isl_aff
*aff
, isl_int v
)
743 if (isl_aff_is_nan(aff
))
745 aff
= isl_aff_cow(aff
);
749 aff
->v
= isl_vec_cow(aff
->v
);
751 return isl_aff_free(aff
);
753 isl_int_set(aff
->v
->el
[1], v
);
758 /* Replace the constant term of "aff" by "v".
760 * A NaN is unaffected by this operation.
762 __isl_give isl_aff
*isl_aff_set_constant_val(__isl_take isl_aff
*aff
,
763 __isl_take isl_val
*v
)
768 if (isl_aff_is_nan(aff
)) {
773 if (!isl_val_is_rat(v
))
774 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
775 "expecting rational value", goto error
);
777 if (isl_int_eq(aff
->v
->el
[1], v
->n
) &&
778 isl_int_eq(aff
->v
->el
[0], v
->d
)) {
783 aff
= isl_aff_cow(aff
);
786 aff
->v
= isl_vec_cow(aff
->v
);
790 if (isl_int_eq(aff
->v
->el
[0], v
->d
)) {
791 isl_int_set(aff
->v
->el
[1], v
->n
);
792 } else if (isl_int_is_one(v
->d
)) {
793 isl_int_mul(aff
->v
->el
[1], aff
->v
->el
[0], v
->n
);
795 isl_seq_scale(aff
->v
->el
+ 1,
796 aff
->v
->el
+ 1, v
->d
, aff
->v
->size
- 1);
797 isl_int_mul(aff
->v
->el
[1], aff
->v
->el
[0], v
->n
);
798 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], v
->d
);
799 aff
->v
= isl_vec_normalize(aff
->v
);
812 /* Add "v" to the constant term of "aff".
814 * A NaN is unaffected by this operation.
816 __isl_give isl_aff
*isl_aff_add_constant(__isl_take isl_aff
*aff
, isl_int v
)
818 if (isl_int_is_zero(v
))
823 if (isl_aff_is_nan(aff
))
825 aff
= isl_aff_cow(aff
);
829 aff
->v
= isl_vec_cow(aff
->v
);
831 return isl_aff_free(aff
);
833 isl_int_addmul(aff
->v
->el
[1], aff
->v
->el
[0], v
);
838 /* Add "v" to the constant term of "aff".
840 * A NaN is unaffected by this operation.
842 __isl_give isl_aff
*isl_aff_add_constant_val(__isl_take isl_aff
*aff
,
843 __isl_take isl_val
*v
)
848 if (isl_aff_is_nan(aff
) || isl_val_is_zero(v
)) {
853 if (!isl_val_is_rat(v
))
854 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
855 "expecting rational value", goto error
);
857 aff
= isl_aff_cow(aff
);
861 aff
->v
= isl_vec_cow(aff
->v
);
865 if (isl_int_is_one(v
->d
)) {
866 isl_int_addmul(aff
->v
->el
[1], aff
->v
->el
[0], v
->n
);
867 } else if (isl_int_eq(aff
->v
->el
[0], v
->d
)) {
868 isl_int_add(aff
->v
->el
[1], aff
->v
->el
[1], v
->n
);
869 aff
->v
= isl_vec_normalize(aff
->v
);
873 isl_seq_scale(aff
->v
->el
+ 1,
874 aff
->v
->el
+ 1, v
->d
, aff
->v
->size
- 1);
875 isl_int_addmul(aff
->v
->el
[1], aff
->v
->el
[0], v
->n
);
876 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], v
->d
);
877 aff
->v
= isl_vec_normalize(aff
->v
);
890 __isl_give isl_aff
*isl_aff_add_constant_si(__isl_take isl_aff
*aff
, int v
)
895 isl_int_set_si(t
, v
);
896 aff
= isl_aff_add_constant(aff
, t
);
902 /* Add "v" to the numerator of the constant term of "aff".
904 * A NaN is unaffected by this operation.
906 __isl_give isl_aff
*isl_aff_add_constant_num(__isl_take isl_aff
*aff
, isl_int v
)
908 if (isl_int_is_zero(v
))
913 if (isl_aff_is_nan(aff
))
915 aff
= isl_aff_cow(aff
);
919 aff
->v
= isl_vec_cow(aff
->v
);
921 return isl_aff_free(aff
);
923 isl_int_add(aff
->v
->el
[1], aff
->v
->el
[1], v
);
928 /* Add "v" to the numerator of the constant term of "aff".
930 * A NaN is unaffected by this operation.
932 __isl_give isl_aff
*isl_aff_add_constant_num_si(__isl_take isl_aff
*aff
, int v
)
940 isl_int_set_si(t
, v
);
941 aff
= isl_aff_add_constant_num(aff
, t
);
947 /* Replace the numerator of the constant term of "aff" by "v".
949 * A NaN is unaffected by this operation.
951 __isl_give isl_aff
*isl_aff_set_constant_si(__isl_take isl_aff
*aff
, int v
)
955 if (isl_aff_is_nan(aff
))
957 aff
= isl_aff_cow(aff
);
961 aff
->v
= isl_vec_cow(aff
->v
);
963 return isl_aff_free(aff
);
965 isl_int_set_si(aff
->v
->el
[1], v
);
970 /* Replace the numerator of the coefficient of the variable of type "type"
971 * at position "pos" of "aff" by "v".
973 * A NaN is unaffected by this operation.
975 __isl_give isl_aff
*isl_aff_set_coefficient(__isl_take isl_aff
*aff
,
976 enum isl_dim_type type
, int pos
, isl_int v
)
981 if (type
== isl_dim_out
)
982 isl_die(aff
->v
->ctx
, isl_error_invalid
,
983 "output/set dimension does not have a coefficient",
984 return isl_aff_free(aff
));
985 if (type
== isl_dim_in
)
988 if (isl_local_space_check_range(aff
->ls
, type
, pos
, 1) < 0)
989 return isl_aff_free(aff
);
991 if (isl_aff_is_nan(aff
))
993 aff
= isl_aff_cow(aff
);
997 aff
->v
= isl_vec_cow(aff
->v
);
999 return isl_aff_free(aff
);
1001 pos
+= isl_local_space_offset(aff
->ls
, type
);
1002 isl_int_set(aff
->v
->el
[1 + pos
], v
);
1007 /* Replace the numerator of the coefficient of the variable of type "type"
1008 * at position "pos" of "aff" by "v".
1010 * A NaN is unaffected by this operation.
1012 __isl_give isl_aff
*isl_aff_set_coefficient_si(__isl_take isl_aff
*aff
,
1013 enum isl_dim_type type
, int pos
, int v
)
1018 if (type
== isl_dim_out
)
1019 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1020 "output/set dimension does not have a coefficient",
1021 return isl_aff_free(aff
));
1022 if (type
== isl_dim_in
)
1025 if (isl_local_space_check_range(aff
->ls
, type
, pos
, 1) < 0)
1026 return isl_aff_free(aff
);
1028 if (isl_aff_is_nan(aff
))
1030 pos
+= isl_local_space_offset(aff
->ls
, type
);
1031 if (isl_int_cmp_si(aff
->v
->el
[1 + pos
], v
) == 0)
1034 aff
= isl_aff_cow(aff
);
1038 aff
->v
= isl_vec_cow(aff
->v
);
1040 return isl_aff_free(aff
);
1042 isl_int_set_si(aff
->v
->el
[1 + pos
], v
);
1047 /* Replace the coefficient of the variable of type "type" at position "pos"
1050 * A NaN is unaffected by this operation.
1052 __isl_give isl_aff
*isl_aff_set_coefficient_val(__isl_take isl_aff
*aff
,
1053 enum isl_dim_type type
, int pos
, __isl_take isl_val
*v
)
1058 if (type
== isl_dim_out
)
1059 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1060 "output/set dimension does not have a coefficient",
1062 if (type
== isl_dim_in
)
1065 if (isl_local_space_check_range(aff
->ls
, type
, pos
, 1) < 0)
1066 return isl_aff_free(aff
);
1068 if (isl_aff_is_nan(aff
)) {
1072 if (!isl_val_is_rat(v
))
1073 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1074 "expecting rational value", goto error
);
1076 pos
+= isl_local_space_offset(aff
->ls
, type
);
1077 if (isl_int_eq(aff
->v
->el
[1 + pos
], v
->n
) &&
1078 isl_int_eq(aff
->v
->el
[0], v
->d
)) {
1083 aff
= isl_aff_cow(aff
);
1086 aff
->v
= isl_vec_cow(aff
->v
);
1090 if (isl_int_eq(aff
->v
->el
[0], v
->d
)) {
1091 isl_int_set(aff
->v
->el
[1 + pos
], v
->n
);
1092 } else if (isl_int_is_one(v
->d
)) {
1093 isl_int_mul(aff
->v
->el
[1 + pos
], aff
->v
->el
[0], v
->n
);
1095 isl_seq_scale(aff
->v
->el
+ 1,
1096 aff
->v
->el
+ 1, v
->d
, aff
->v
->size
- 1);
1097 isl_int_mul(aff
->v
->el
[1 + pos
], aff
->v
->el
[0], v
->n
);
1098 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], v
->d
);
1099 aff
->v
= isl_vec_normalize(aff
->v
);
1112 /* Add "v" to the coefficient of the variable of type "type"
1113 * at position "pos" of "aff".
1115 * A NaN is unaffected by this operation.
1117 __isl_give isl_aff
*isl_aff_add_coefficient(__isl_take isl_aff
*aff
,
1118 enum isl_dim_type type
, int pos
, isl_int v
)
1123 if (type
== isl_dim_out
)
1124 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1125 "output/set dimension does not have a coefficient",
1126 return isl_aff_free(aff
));
1127 if (type
== isl_dim_in
)
1130 if (isl_local_space_check_range(aff
->ls
, type
, pos
, 1) < 0)
1131 return isl_aff_free(aff
);
1133 if (isl_aff_is_nan(aff
))
1135 aff
= isl_aff_cow(aff
);
1139 aff
->v
= isl_vec_cow(aff
->v
);
1141 return isl_aff_free(aff
);
1143 pos
+= isl_local_space_offset(aff
->ls
, type
);
1144 isl_int_addmul(aff
->v
->el
[1 + pos
], aff
->v
->el
[0], v
);
1149 /* Add "v" to the coefficient of the variable of type "type"
1150 * at position "pos" of "aff".
1152 * A NaN is unaffected by this operation.
1154 __isl_give isl_aff
*isl_aff_add_coefficient_val(__isl_take isl_aff
*aff
,
1155 enum isl_dim_type type
, int pos
, __isl_take isl_val
*v
)
1160 if (isl_val_is_zero(v
)) {
1165 if (type
== isl_dim_out
)
1166 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1167 "output/set dimension does not have a coefficient",
1169 if (type
== isl_dim_in
)
1172 if (isl_local_space_check_range(aff
->ls
, type
, pos
, 1) < 0)
1175 if (isl_aff_is_nan(aff
)) {
1179 if (!isl_val_is_rat(v
))
1180 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1181 "expecting rational value", goto error
);
1183 aff
= isl_aff_cow(aff
);
1187 aff
->v
= isl_vec_cow(aff
->v
);
1191 pos
+= isl_local_space_offset(aff
->ls
, type
);
1192 if (isl_int_is_one(v
->d
)) {
1193 isl_int_addmul(aff
->v
->el
[1 + pos
], aff
->v
->el
[0], v
->n
);
1194 } else if (isl_int_eq(aff
->v
->el
[0], v
->d
)) {
1195 isl_int_add(aff
->v
->el
[1 + pos
], aff
->v
->el
[1 + pos
], v
->n
);
1196 aff
->v
= isl_vec_normalize(aff
->v
);
1200 isl_seq_scale(aff
->v
->el
+ 1,
1201 aff
->v
->el
+ 1, v
->d
, aff
->v
->size
- 1);
1202 isl_int_addmul(aff
->v
->el
[1 + pos
], aff
->v
->el
[0], v
->n
);
1203 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], v
->d
);
1204 aff
->v
= isl_vec_normalize(aff
->v
);
1217 __isl_give isl_aff
*isl_aff_add_coefficient_si(__isl_take isl_aff
*aff
,
1218 enum isl_dim_type type
, int pos
, int v
)
1223 isl_int_set_si(t
, v
);
1224 aff
= isl_aff_add_coefficient(aff
, type
, pos
, t
);
1230 __isl_give isl_aff
*isl_aff_get_div(__isl_keep isl_aff
*aff
, int pos
)
1235 return isl_local_space_get_div(aff
->ls
, pos
);
1238 /* Return the negation of "aff".
1240 * As a special case, -NaN = NaN.
1242 __isl_give isl_aff
*isl_aff_neg(__isl_take isl_aff
*aff
)
1246 if (isl_aff_is_nan(aff
))
1248 aff
= isl_aff_cow(aff
);
1251 aff
->v
= isl_vec_cow(aff
->v
);
1253 return isl_aff_free(aff
);
1255 isl_seq_neg(aff
->v
->el
+ 1, aff
->v
->el
+ 1, aff
->v
->size
- 1);
1260 /* Remove divs from the local space that do not appear in the affine
1262 * We currently only remove divs at the end.
1263 * Some intermediate divs may also not appear directly in the affine
1264 * expression, but we would also need to check that no other divs are
1265 * defined in terms of them.
1267 __isl_give isl_aff
*isl_aff_remove_unused_divs(__isl_take isl_aff
*aff
)
1276 n
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
1278 return isl_aff_free(aff
);
1279 off
= isl_local_space_offset(aff
->ls
, isl_dim_div
);
1281 pos
= isl_seq_last_non_zero(aff
->v
->el
+ 1 + off
, n
) + 1;
1285 aff
= isl_aff_cow(aff
);
1289 aff
->ls
= isl_local_space_drop_dims(aff
->ls
, isl_dim_div
, pos
, n
- pos
);
1290 aff
->v
= isl_vec_drop_els(aff
->v
, 1 + off
+ pos
, n
- pos
);
1291 if (!aff
->ls
|| !aff
->v
)
1292 return isl_aff_free(aff
);
1297 /* Look for any divs in the aff->ls with a denominator equal to one
1298 * and plug them into the affine expression and any subsequent divs
1299 * that may reference the div.
1301 static __isl_give isl_aff
*plug_in_integral_divs(__isl_take isl_aff
*aff
)
1308 isl_local_space
*ls
;
1314 n
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
1316 return isl_aff_free(aff
);
1318 for (i
= 0; i
< n
; ++i
) {
1319 if (!isl_int_is_one(aff
->ls
->div
->row
[i
][0]))
1321 ls
= isl_local_space_copy(aff
->ls
);
1322 ls
= isl_local_space_substitute_seq(ls
, isl_dim_div
, i
,
1323 aff
->ls
->div
->row
[i
], len
, i
+ 1, n
- (i
+ 1));
1324 vec
= isl_vec_copy(aff
->v
);
1325 vec
= isl_vec_cow(vec
);
1331 pos
= isl_local_space_offset(aff
->ls
, isl_dim_div
) + i
;
1332 isl_seq_substitute(vec
->el
, pos
, aff
->ls
->div
->row
[i
],
1337 isl_vec_free(aff
->v
);
1339 isl_local_space_free(aff
->ls
);
1346 isl_local_space_free(ls
);
1347 return isl_aff_free(aff
);
1350 /* Look for any divs j that appear with a unit coefficient inside
1351 * the definitions of other divs i and plug them into the definitions
1354 * In particular, an expression of the form
1356 * floor((f(..) + floor(g(..)/n))/m)
1360 * floor((n * f(..) + g(..))/(n * m))
1362 * This simplification is correct because we can move the expression
1363 * f(..) into the inner floor in the original expression to obtain
1365 * floor(floor((n * f(..) + g(..))/n)/m)
1367 * from which we can derive the simplified expression.
1369 static __isl_give isl_aff
*plug_in_unit_divs(__isl_take isl_aff
*aff
)
1378 n
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
1380 return isl_aff_free(aff
);
1381 off
= isl_local_space_offset(aff
->ls
, isl_dim_div
);
1382 for (i
= 1; i
< n
; ++i
) {
1383 for (j
= 0; j
< i
; ++j
) {
1384 if (!isl_int_is_one(aff
->ls
->div
->row
[i
][1 + off
+ j
]))
1386 aff
->ls
= isl_local_space_substitute_seq(aff
->ls
,
1387 isl_dim_div
, j
, aff
->ls
->div
->row
[j
],
1388 aff
->v
->size
, i
, 1);
1390 return isl_aff_free(aff
);
1397 /* Swap divs "a" and "b" in "aff", which is assumed to be non-NULL.
1399 * Even though this function is only called on isl_affs with a single
1400 * reference, we are careful to only change aff->v and aff->ls together.
1402 static __isl_give isl_aff
*swap_div(__isl_take isl_aff
*aff
, int a
, int b
)
1404 unsigned off
= isl_local_space_offset(aff
->ls
, isl_dim_div
);
1405 isl_local_space
*ls
;
1408 ls
= isl_local_space_copy(aff
->ls
);
1409 ls
= isl_local_space_swap_div(ls
, a
, b
);
1410 v
= isl_vec_copy(aff
->v
);
1415 isl_int_swap(v
->el
[1 + off
+ a
], v
->el
[1 + off
+ b
]);
1416 isl_vec_free(aff
->v
);
1418 isl_local_space_free(aff
->ls
);
1424 isl_local_space_free(ls
);
1425 return isl_aff_free(aff
);
1428 /* Merge divs "a" and "b" in "aff", which is assumed to be non-NULL.
1430 * We currently do not actually remove div "b", but simply add its
1431 * coefficient to that of "a" and then zero it out.
1433 static __isl_give isl_aff
*merge_divs(__isl_take isl_aff
*aff
, int a
, int b
)
1435 unsigned off
= isl_local_space_offset(aff
->ls
, isl_dim_div
);
1437 if (isl_int_is_zero(aff
->v
->el
[1 + off
+ b
]))
1440 aff
->v
= isl_vec_cow(aff
->v
);
1442 return isl_aff_free(aff
);
1444 isl_int_add(aff
->v
->el
[1 + off
+ a
],
1445 aff
->v
->el
[1 + off
+ a
], aff
->v
->el
[1 + off
+ b
]);
1446 isl_int_set_si(aff
->v
->el
[1 + off
+ b
], 0);
1451 /* Sort the divs in the local space of "aff" according to
1452 * the comparison function "cmp_row" in isl_local_space.c,
1453 * combining the coefficients of identical divs.
1455 * Reordering divs does not change the semantics of "aff",
1456 * so there is no need to call isl_aff_cow.
1457 * Moreover, this function is currently only called on isl_affs
1458 * with a single reference.
1460 static __isl_give isl_aff
*sort_divs(__isl_take isl_aff
*aff
)
1465 n
= isl_aff_dim(aff
, isl_dim_div
);
1467 return isl_aff_free(aff
);
1468 for (i
= 1; i
< n
; ++i
) {
1469 for (j
= i
- 1; j
>= 0; --j
) {
1470 int cmp
= isl_mat_cmp_div(aff
->ls
->div
, j
, j
+ 1);
1474 aff
= merge_divs(aff
, j
, j
+ 1);
1476 aff
= swap_div(aff
, j
, j
+ 1);
1485 /* Normalize the representation of "aff".
1487 * This function should only be called of "new" isl_affs, i.e.,
1488 * with only a single reference. We therefore do not need to
1489 * worry about affecting other instances.
1491 __isl_give isl_aff
*isl_aff_normalize(__isl_take isl_aff
*aff
)
1495 aff
->v
= isl_vec_normalize(aff
->v
);
1497 return isl_aff_free(aff
);
1498 aff
= plug_in_integral_divs(aff
);
1499 aff
= plug_in_unit_divs(aff
);
1500 aff
= sort_divs(aff
);
1501 aff
= isl_aff_remove_unused_divs(aff
);
1505 /* Given f, return floor(f).
1506 * If f is an integer expression, then just return f.
1507 * If f is a constant, then return the constant floor(f).
1508 * Otherwise, if f = g/m, write g = q m + r,
1509 * create a new div d = [r/m] and return the expression q + d.
1510 * The coefficients in r are taken to lie between -m/2 and m/2.
1512 * reduce_div_coefficients performs the same normalization.
1514 * As a special case, floor(NaN) = NaN.
1516 __isl_give isl_aff
*isl_aff_floor(__isl_take isl_aff
*aff
)
1526 if (isl_aff_is_nan(aff
))
1528 if (isl_int_is_one(aff
->v
->el
[0]))
1531 aff
= isl_aff_cow(aff
);
1535 aff
->v
= isl_vec_cow(aff
->v
);
1537 return isl_aff_free(aff
);
1539 if (isl_aff_is_cst(aff
)) {
1540 isl_int_fdiv_q(aff
->v
->el
[1], aff
->v
->el
[1], aff
->v
->el
[0]);
1541 isl_int_set_si(aff
->v
->el
[0], 1);
1545 div
= isl_vec_copy(aff
->v
);
1546 div
= isl_vec_cow(div
);
1548 return isl_aff_free(aff
);
1550 ctx
= isl_aff_get_ctx(aff
);
1551 isl_int_fdiv_q(aff
->v
->el
[0], aff
->v
->el
[0], ctx
->two
);
1552 for (i
= 1; i
< aff
->v
->size
; ++i
) {
1553 isl_int_fdiv_r(div
->el
[i
], div
->el
[i
], div
->el
[0]);
1554 isl_int_fdiv_q(aff
->v
->el
[i
], aff
->v
->el
[i
], div
->el
[0]);
1555 if (isl_int_gt(div
->el
[i
], aff
->v
->el
[0])) {
1556 isl_int_sub(div
->el
[i
], div
->el
[i
], div
->el
[0]);
1557 isl_int_add_ui(aff
->v
->el
[i
], aff
->v
->el
[i
], 1);
1561 aff
->ls
= isl_local_space_add_div(aff
->ls
, div
);
1563 return isl_aff_free(aff
);
1565 size
= aff
->v
->size
;
1566 aff
->v
= isl_vec_extend(aff
->v
, size
+ 1);
1568 return isl_aff_free(aff
);
1569 isl_int_set_si(aff
->v
->el
[0], 1);
1570 isl_int_set_si(aff
->v
->el
[size
], 1);
1572 aff
= isl_aff_normalize(aff
);
1579 * aff mod m = aff - m * floor(aff/m)
1581 * with m an integer value.
1583 __isl_give isl_aff
*isl_aff_mod_val(__isl_take isl_aff
*aff
,
1584 __isl_take isl_val
*m
)
1591 if (!isl_val_is_int(m
))
1592 isl_die(isl_val_get_ctx(m
), isl_error_invalid
,
1593 "expecting integer modulo", goto error
);
1595 res
= isl_aff_copy(aff
);
1596 aff
= isl_aff_scale_down_val(aff
, isl_val_copy(m
));
1597 aff
= isl_aff_floor(aff
);
1598 aff
= isl_aff_scale_val(aff
, m
);
1599 res
= isl_aff_sub(res
, aff
);
1610 * pwaff mod m = pwaff - m * floor(pwaff/m)
1612 __isl_give isl_pw_aff
*isl_pw_aff_mod(__isl_take isl_pw_aff
*pwaff
, isl_int m
)
1616 res
= isl_pw_aff_copy(pwaff
);
1617 pwaff
= isl_pw_aff_scale_down(pwaff
, m
);
1618 pwaff
= isl_pw_aff_floor(pwaff
);
1619 pwaff
= isl_pw_aff_scale(pwaff
, m
);
1620 res
= isl_pw_aff_sub(res
, pwaff
);
1627 * pa mod m = pa - m * floor(pa/m)
1629 * with m an integer value.
1631 __isl_give isl_pw_aff
*isl_pw_aff_mod_val(__isl_take isl_pw_aff
*pa
,
1632 __isl_take isl_val
*m
)
1636 if (!isl_val_is_int(m
))
1637 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
1638 "expecting integer modulo", goto error
);
1639 pa
= isl_pw_aff_mod(pa
, m
->n
);
1643 isl_pw_aff_free(pa
);
1648 /* Given f, return ceil(f).
1649 * If f is an integer expression, then just return f.
1650 * Otherwise, let f be the expression
1656 * floor((e + m - 1)/m)
1658 * As a special case, ceil(NaN) = NaN.
1660 __isl_give isl_aff
*isl_aff_ceil(__isl_take isl_aff
*aff
)
1665 if (isl_aff_is_nan(aff
))
1667 if (isl_int_is_one(aff
->v
->el
[0]))
1670 aff
= isl_aff_cow(aff
);
1673 aff
->v
= isl_vec_cow(aff
->v
);
1675 return isl_aff_free(aff
);
1677 isl_int_add(aff
->v
->el
[1], aff
->v
->el
[1], aff
->v
->el
[0]);
1678 isl_int_sub_ui(aff
->v
->el
[1], aff
->v
->el
[1], 1);
1679 aff
= isl_aff_floor(aff
);
1684 /* Apply the expansion computed by isl_merge_divs.
1685 * The expansion itself is given by "exp" while the resulting
1686 * list of divs is given by "div".
1688 __isl_give isl_aff
*isl_aff_expand_divs(__isl_take isl_aff
*aff
,
1689 __isl_take isl_mat
*div
, int *exp
)
1695 aff
= isl_aff_cow(aff
);
1699 old_n_div
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
1700 new_n_div
= isl_mat_rows(div
);
1701 if (old_n_div
< 0 || new_n_div
< 0)
1703 offset
= 1 + isl_local_space_offset(aff
->ls
, isl_dim_div
);
1705 aff
->v
= isl_vec_expand(aff
->v
, offset
, old_n_div
, exp
, new_n_div
);
1706 aff
->ls
= isl_local_space_replace_divs(aff
->ls
, div
);
1707 if (!aff
->v
|| !aff
->ls
)
1708 return isl_aff_free(aff
);
1716 /* Add two affine expressions that live in the same local space.
1718 static __isl_give isl_aff
*add_expanded(__isl_take isl_aff
*aff1
,
1719 __isl_take isl_aff
*aff2
)
1723 aff1
= isl_aff_cow(aff1
);
1727 aff1
->v
= isl_vec_cow(aff1
->v
);
1733 isl_int_gcd(gcd
, aff1
->v
->el
[0], aff2
->v
->el
[0]);
1734 isl_int_divexact(f
, aff2
->v
->el
[0], gcd
);
1735 isl_seq_scale(aff1
->v
->el
+ 1, aff1
->v
->el
+ 1, f
, aff1
->v
->size
- 1);
1736 isl_int_divexact(f
, aff1
->v
->el
[0], gcd
);
1737 isl_seq_addmul(aff1
->v
->el
+ 1, f
, aff2
->v
->el
+ 1, aff1
->v
->size
- 1);
1738 isl_int_divexact(f
, aff2
->v
->el
[0], gcd
);
1739 isl_int_mul(aff1
->v
->el
[0], aff1
->v
->el
[0], f
);
1751 /* Return the sum of "aff1" and "aff2".
1753 * If either of the two is NaN, then the result is NaN.
1755 __isl_give isl_aff
*isl_aff_add(__isl_take isl_aff
*aff1
,
1756 __isl_take isl_aff
*aff2
)
1762 isl_size n_div1
, n_div2
;
1767 ctx
= isl_aff_get_ctx(aff1
);
1768 if (!isl_space_is_equal(aff1
->ls
->dim
, aff2
->ls
->dim
))
1769 isl_die(ctx
, isl_error_invalid
,
1770 "spaces don't match", goto error
);
1772 if (isl_aff_is_nan(aff1
)) {
1776 if (isl_aff_is_nan(aff2
)) {
1781 n_div1
= isl_aff_dim(aff1
, isl_dim_div
);
1782 n_div2
= isl_aff_dim(aff2
, isl_dim_div
);
1783 if (n_div1
< 0 || n_div2
< 0)
1785 if (n_div1
== 0 && n_div2
== 0)
1786 return add_expanded(aff1
, aff2
);
1788 exp1
= isl_alloc_array(ctx
, int, n_div1
);
1789 exp2
= isl_alloc_array(ctx
, int, n_div2
);
1790 if ((n_div1
&& !exp1
) || (n_div2
&& !exp2
))
1793 div
= isl_merge_divs(aff1
->ls
->div
, aff2
->ls
->div
, exp1
, exp2
);
1794 aff1
= isl_aff_expand_divs(aff1
, isl_mat_copy(div
), exp1
);
1795 aff2
= isl_aff_expand_divs(aff2
, div
, exp2
);
1799 return add_expanded(aff1
, aff2
);
1808 __isl_give isl_aff
*isl_aff_sub(__isl_take isl_aff
*aff1
,
1809 __isl_take isl_aff
*aff2
)
1811 return isl_aff_add(aff1
, isl_aff_neg(aff2
));
1814 /* Return the result of scaling "aff" by a factor of "f".
1816 * As a special case, f * NaN = NaN.
1818 __isl_give isl_aff
*isl_aff_scale(__isl_take isl_aff
*aff
, isl_int f
)
1824 if (isl_aff_is_nan(aff
))
1827 if (isl_int_is_one(f
))
1830 aff
= isl_aff_cow(aff
);
1833 aff
->v
= isl_vec_cow(aff
->v
);
1835 return isl_aff_free(aff
);
1837 if (isl_int_is_pos(f
) && isl_int_is_divisible_by(aff
->v
->el
[0], f
)) {
1838 isl_int_divexact(aff
->v
->el
[0], aff
->v
->el
[0], f
);
1843 isl_int_gcd(gcd
, aff
->v
->el
[0], f
);
1844 isl_int_divexact(aff
->v
->el
[0], aff
->v
->el
[0], gcd
);
1845 isl_int_divexact(gcd
, f
, gcd
);
1846 isl_seq_scale(aff
->v
->el
+ 1, aff
->v
->el
+ 1, gcd
, aff
->v
->size
- 1);
1852 /* Multiple "aff" by "v".
1854 __isl_give isl_aff
*isl_aff_scale_val(__isl_take isl_aff
*aff
,
1855 __isl_take isl_val
*v
)
1860 if (isl_val_is_one(v
)) {
1865 if (!isl_val_is_rat(v
))
1866 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1867 "expecting rational factor", goto error
);
1869 aff
= isl_aff_scale(aff
, v
->n
);
1870 aff
= isl_aff_scale_down(aff
, v
->d
);
1880 /* Return the result of scaling "aff" down by a factor of "f".
1882 * As a special case, NaN/f = NaN.
1884 __isl_give isl_aff
*isl_aff_scale_down(__isl_take isl_aff
*aff
, isl_int f
)
1890 if (isl_aff_is_nan(aff
))
1893 if (isl_int_is_one(f
))
1896 aff
= isl_aff_cow(aff
);
1900 if (isl_int_is_zero(f
))
1901 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1902 "cannot scale down by zero", return isl_aff_free(aff
));
1904 aff
->v
= isl_vec_cow(aff
->v
);
1906 return isl_aff_free(aff
);
1909 isl_seq_gcd(aff
->v
->el
+ 1, aff
->v
->size
- 1, &gcd
);
1910 isl_int_gcd(gcd
, gcd
, f
);
1911 isl_seq_scale_down(aff
->v
->el
+ 1, aff
->v
->el
+ 1, gcd
, aff
->v
->size
- 1);
1912 isl_int_divexact(gcd
, f
, gcd
);
1913 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], gcd
);
1919 /* Divide "aff" by "v".
1921 __isl_give isl_aff
*isl_aff_scale_down_val(__isl_take isl_aff
*aff
,
1922 __isl_take isl_val
*v
)
1927 if (isl_val_is_one(v
)) {
1932 if (!isl_val_is_rat(v
))
1933 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1934 "expecting rational factor", goto error
);
1935 if (!isl_val_is_pos(v
))
1936 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1937 "factor needs to be positive", goto error
);
1939 aff
= isl_aff_scale(aff
, v
->d
);
1940 aff
= isl_aff_scale_down(aff
, v
->n
);
1950 __isl_give isl_aff
*isl_aff_scale_down_ui(__isl_take isl_aff
*aff
, unsigned f
)
1958 isl_int_set_ui(v
, f
);
1959 aff
= isl_aff_scale_down(aff
, v
);
1965 __isl_give isl_aff
*isl_aff_set_dim_name(__isl_take isl_aff
*aff
,
1966 enum isl_dim_type type
, unsigned pos
, const char *s
)
1968 aff
= isl_aff_cow(aff
);
1971 if (type
== isl_dim_out
)
1972 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1973 "cannot set name of output/set dimension",
1974 return isl_aff_free(aff
));
1975 if (type
== isl_dim_in
)
1977 aff
->ls
= isl_local_space_set_dim_name(aff
->ls
, type
, pos
, s
);
1979 return isl_aff_free(aff
);
1984 __isl_give isl_aff
*isl_aff_set_dim_id(__isl_take isl_aff
*aff
,
1985 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
1987 aff
= isl_aff_cow(aff
);
1990 if (type
== isl_dim_out
)
1991 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1992 "cannot set name of output/set dimension",
1994 if (type
== isl_dim_in
)
1996 aff
->ls
= isl_local_space_set_dim_id(aff
->ls
, type
, pos
, id
);
1998 return isl_aff_free(aff
);
2007 /* Replace the identifier of the input tuple of "aff" by "id".
2008 * type is currently required to be equal to isl_dim_in
2010 __isl_give isl_aff
*isl_aff_set_tuple_id(__isl_take isl_aff
*aff
,
2011 enum isl_dim_type type
, __isl_take isl_id
*id
)
2013 aff
= isl_aff_cow(aff
);
2016 if (type
!= isl_dim_in
)
2017 isl_die(aff
->v
->ctx
, isl_error_invalid
,
2018 "cannot only set id of input tuple", goto error
);
2019 aff
->ls
= isl_local_space_set_tuple_id(aff
->ls
, isl_dim_set
, id
);
2021 return isl_aff_free(aff
);
2030 /* Exploit the equalities in "eq" to simplify the affine expression
2031 * and the expressions of the integer divisions in the local space.
2032 * The integer divisions in this local space are assumed to appear
2033 * as regular dimensions in "eq".
2035 static __isl_give isl_aff
*isl_aff_substitute_equalities_lifted(
2036 __isl_take isl_aff
*aff
, __isl_take isl_basic_set
*eq
)
2044 if (eq
->n_eq
== 0) {
2045 isl_basic_set_free(eq
);
2049 aff
= isl_aff_cow(aff
);
2053 aff
->ls
= isl_local_space_substitute_equalities(aff
->ls
,
2054 isl_basic_set_copy(eq
));
2055 aff
->v
= isl_vec_cow(aff
->v
);
2056 if (!aff
->ls
|| !aff
->v
)
2059 o_div
= isl_basic_set_offset(eq
, isl_dim_div
);
2061 for (i
= 0; i
< eq
->n_eq
; ++i
) {
2062 j
= isl_seq_last_non_zero(eq
->eq
[i
], o_div
+ n_div
);
2063 if (j
< 0 || j
== 0 || j
>= o_div
)
2066 isl_seq_elim(aff
->v
->el
+ 1, eq
->eq
[i
], j
, o_div
,
2070 isl_basic_set_free(eq
);
2071 aff
= isl_aff_normalize(aff
);
2074 isl_basic_set_free(eq
);
2079 /* Exploit the equalities in "eq" to simplify the affine expression
2080 * and the expressions of the integer divisions in the local space.
2082 __isl_give isl_aff
*isl_aff_substitute_equalities(__isl_take isl_aff
*aff
,
2083 __isl_take isl_basic_set
*eq
)
2089 n_div
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
2093 eq
= isl_basic_set_add_dims(eq
, isl_dim_set
, n_div
);
2094 return isl_aff_substitute_equalities_lifted(aff
, eq
);
2096 isl_basic_set_free(eq
);
2101 /* Look for equalities among the variables shared by context and aff
2102 * and the integer divisions of aff, if any.
2103 * The equalities are then used to eliminate coefficients and/or integer
2104 * divisions from aff.
2106 __isl_give isl_aff
*isl_aff_gist(__isl_take isl_aff
*aff
,
2107 __isl_take isl_set
*context
)
2109 isl_local_space
*ls
;
2110 isl_basic_set
*hull
;
2112 ls
= isl_aff_get_domain_local_space(aff
);
2113 context
= isl_local_space_lift_set(ls
, context
);
2115 hull
= isl_set_affine_hull(context
);
2116 return isl_aff_substitute_equalities_lifted(aff
, hull
);
2119 __isl_give isl_aff
*isl_aff_gist_params(__isl_take isl_aff
*aff
,
2120 __isl_take isl_set
*context
)
2122 isl_set
*dom_context
= isl_set_universe(isl_aff_get_domain_space(aff
));
2123 dom_context
= isl_set_intersect_params(dom_context
, context
);
2124 return isl_aff_gist(aff
, dom_context
);
2127 /* Return a basic set containing those elements in the space
2128 * of aff where it is positive. "rational" should not be set.
2130 * If "aff" is NaN, then it is not positive.
2132 static __isl_give isl_basic_set
*aff_pos_basic_set(__isl_take isl_aff
*aff
,
2135 isl_constraint
*ineq
;
2136 isl_basic_set
*bset
;
2141 if (isl_aff_is_nan(aff
)) {
2142 isl_space
*space
= isl_aff_get_domain_space(aff
);
2144 return isl_basic_set_empty(space
);
2147 isl_die(isl_aff_get_ctx(aff
), isl_error_unsupported
,
2148 "rational sets not supported", goto error
);
2150 ineq
= isl_inequality_from_aff(aff
);
2151 c
= isl_constraint_get_constant_val(ineq
);
2152 c
= isl_val_sub_ui(c
, 1);
2153 ineq
= isl_constraint_set_constant_val(ineq
, c
);
2155 bset
= isl_basic_set_from_constraint(ineq
);
2156 bset
= isl_basic_set_simplify(bset
);
2163 /* Return a basic set containing those elements in the space
2164 * of aff where it is non-negative.
2165 * If "rational" is set, then return a rational basic set.
2167 * If "aff" is NaN, then it is not non-negative (it's not negative either).
2169 static __isl_give isl_basic_set
*aff_nonneg_basic_set(
2170 __isl_take isl_aff
*aff
, int rational
)
2172 isl_constraint
*ineq
;
2173 isl_basic_set
*bset
;
2177 if (isl_aff_is_nan(aff
)) {
2178 isl_space
*space
= isl_aff_get_domain_space(aff
);
2180 return isl_basic_set_empty(space
);
2183 ineq
= isl_inequality_from_aff(aff
);
2185 bset
= isl_basic_set_from_constraint(ineq
);
2187 bset
= isl_basic_set_set_rational(bset
);
2188 bset
= isl_basic_set_simplify(bset
);
2192 /* Return a basic set containing those elements in the space
2193 * of aff where it is non-negative.
2195 __isl_give isl_basic_set
*isl_aff_nonneg_basic_set(__isl_take isl_aff
*aff
)
2197 return aff_nonneg_basic_set(aff
, 0);
2200 /* Return a basic set containing those elements in the domain space
2201 * of "aff" where it is positive.
2203 __isl_give isl_basic_set
*isl_aff_pos_basic_set(__isl_take isl_aff
*aff
)
2205 aff
= isl_aff_add_constant_num_si(aff
, -1);
2206 return isl_aff_nonneg_basic_set(aff
);
2209 /* Return a basic set containing those elements in the domain space
2210 * of aff where it is negative.
2212 __isl_give isl_basic_set
*isl_aff_neg_basic_set(__isl_take isl_aff
*aff
)
2214 aff
= isl_aff_neg(aff
);
2215 return isl_aff_pos_basic_set(aff
);
2218 /* Return a basic set containing those elements in the space
2219 * of aff where it is zero.
2220 * If "rational" is set, then return a rational basic set.
2222 * If "aff" is NaN, then it is not zero.
2224 static __isl_give isl_basic_set
*aff_zero_basic_set(__isl_take isl_aff
*aff
,
2227 isl_constraint
*ineq
;
2228 isl_basic_set
*bset
;
2232 if (isl_aff_is_nan(aff
)) {
2233 isl_space
*space
= isl_aff_get_domain_space(aff
);
2235 return isl_basic_set_empty(space
);
2238 ineq
= isl_equality_from_aff(aff
);
2240 bset
= isl_basic_set_from_constraint(ineq
);
2242 bset
= isl_basic_set_set_rational(bset
);
2243 bset
= isl_basic_set_simplify(bset
);
2247 /* Return a basic set containing those elements in the space
2248 * of aff where it is zero.
2250 __isl_give isl_basic_set
*isl_aff_zero_basic_set(__isl_take isl_aff
*aff
)
2252 return aff_zero_basic_set(aff
, 0);
2255 /* Return a basic set containing those elements in the shared space
2256 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
2258 __isl_give isl_basic_set
*isl_aff_ge_basic_set(__isl_take isl_aff
*aff1
,
2259 __isl_take isl_aff
*aff2
)
2261 aff1
= isl_aff_sub(aff1
, aff2
);
2263 return isl_aff_nonneg_basic_set(aff1
);
2266 /* Return a basic set containing those elements in the shared domain space
2267 * of "aff1" and "aff2" where "aff1" is greater than "aff2".
2269 __isl_give isl_basic_set
*isl_aff_gt_basic_set(__isl_take isl_aff
*aff1
,
2270 __isl_take isl_aff
*aff2
)
2272 aff1
= isl_aff_sub(aff1
, aff2
);
2274 return isl_aff_pos_basic_set(aff1
);
2277 /* Return a set containing those elements in the shared space
2278 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
2280 __isl_give isl_set
*isl_aff_ge_set(__isl_take isl_aff
*aff1
,
2281 __isl_take isl_aff
*aff2
)
2283 return isl_set_from_basic_set(isl_aff_ge_basic_set(aff1
, aff2
));
2286 /* Return a set containing those elements in the shared domain space
2287 * of aff1 and aff2 where aff1 is greater than aff2.
2289 * If either of the two inputs is NaN, then the result is empty,
2290 * as comparisons with NaN always return false.
2292 __isl_give isl_set
*isl_aff_gt_set(__isl_take isl_aff
*aff1
,
2293 __isl_take isl_aff
*aff2
)
2295 return isl_set_from_basic_set(isl_aff_gt_basic_set(aff1
, aff2
));
2298 /* Return a basic set containing those elements in the shared space
2299 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
2301 __isl_give isl_basic_set
*isl_aff_le_basic_set(__isl_take isl_aff
*aff1
,
2302 __isl_take isl_aff
*aff2
)
2304 return isl_aff_ge_basic_set(aff2
, aff1
);
2307 /* Return a basic set containing those elements in the shared domain space
2308 * of "aff1" and "aff2" where "aff1" is smaller than "aff2".
2310 __isl_give isl_basic_set
*isl_aff_lt_basic_set(__isl_take isl_aff
*aff1
,
2311 __isl_take isl_aff
*aff2
)
2313 return isl_aff_gt_basic_set(aff2
, aff1
);
2316 /* Return a set containing those elements in the shared space
2317 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
2319 __isl_give isl_set
*isl_aff_le_set(__isl_take isl_aff
*aff1
,
2320 __isl_take isl_aff
*aff2
)
2322 return isl_aff_ge_set(aff2
, aff1
);
2325 /* Return a set containing those elements in the shared domain space
2326 * of "aff1" and "aff2" where "aff1" is smaller than "aff2".
2328 __isl_give isl_set
*isl_aff_lt_set(__isl_take isl_aff
*aff1
,
2329 __isl_take isl_aff
*aff2
)
2331 return isl_set_from_basic_set(isl_aff_lt_basic_set(aff1
, aff2
));
2334 /* Return a basic set containing those elements in the shared space
2335 * of aff1 and aff2 where aff1 and aff2 are equal.
2337 __isl_give isl_basic_set
*isl_aff_eq_basic_set(__isl_take isl_aff
*aff1
,
2338 __isl_take isl_aff
*aff2
)
2340 aff1
= isl_aff_sub(aff1
, aff2
);
2342 return isl_aff_zero_basic_set(aff1
);
2345 /* Return a set containing those elements in the shared space
2346 * of aff1 and aff2 where aff1 and aff2 are equal.
2348 __isl_give isl_set
*isl_aff_eq_set(__isl_take isl_aff
*aff1
,
2349 __isl_take isl_aff
*aff2
)
2351 return isl_set_from_basic_set(isl_aff_eq_basic_set(aff1
, aff2
));
2354 /* Return a set containing those elements in the shared domain space
2355 * of aff1 and aff2 where aff1 and aff2 are not equal.
2357 * If either of the two inputs is NaN, then the result is empty,
2358 * as comparisons with NaN always return false.
2360 __isl_give isl_set
*isl_aff_ne_set(__isl_take isl_aff
*aff1
,
2361 __isl_take isl_aff
*aff2
)
2363 isl_set
*set_lt
, *set_gt
;
2365 set_lt
= isl_aff_lt_set(isl_aff_copy(aff1
),
2366 isl_aff_copy(aff2
));
2367 set_gt
= isl_aff_gt_set(aff1
, aff2
);
2368 return isl_set_union_disjoint(set_lt
, set_gt
);
2371 __isl_give isl_aff
*isl_aff_add_on_domain(__isl_keep isl_set
*dom
,
2372 __isl_take isl_aff
*aff1
, __isl_take isl_aff
*aff2
)
2374 aff1
= isl_aff_add(aff1
, aff2
);
2375 aff1
= isl_aff_gist(aff1
, isl_set_copy(dom
));
2379 int isl_aff_is_empty(__isl_keep isl_aff
*aff
)
2388 #define TYPE isl_aff
2390 #include "check_type_range_templ.c"
2392 /* Check whether the given affine expression has non-zero coefficient
2393 * for any dimension in the given range or if any of these dimensions
2394 * appear with non-zero coefficients in any of the integer divisions
2395 * involved in the affine expression.
2397 isl_bool
isl_aff_involves_dims(__isl_keep isl_aff
*aff
,
2398 enum isl_dim_type type
, unsigned first
, unsigned n
)
2402 isl_bool involves
= isl_bool_false
;
2405 return isl_bool_error
;
2407 return isl_bool_false
;
2408 if (isl_aff_check_range(aff
, type
, first
, n
) < 0)
2409 return isl_bool_error
;
2411 active
= isl_local_space_get_active(aff
->ls
, aff
->v
->el
+ 2);
2415 first
+= isl_local_space_offset(aff
->ls
, type
) - 1;
2416 for (i
= 0; i
< n
; ++i
)
2417 if (active
[first
+ i
]) {
2418 involves
= isl_bool_true
;
2427 return isl_bool_error
;
2430 __isl_give isl_aff
*isl_aff_drop_dims(__isl_take isl_aff
*aff
,
2431 enum isl_dim_type type
, unsigned first
, unsigned n
)
2437 if (type
== isl_dim_out
)
2438 isl_die(aff
->v
->ctx
, isl_error_invalid
,
2439 "cannot drop output/set dimension",
2440 return isl_aff_free(aff
));
2441 if (type
== isl_dim_in
)
2443 if (n
== 0 && !isl_local_space_is_named_or_nested(aff
->ls
, type
))
2446 ctx
= isl_aff_get_ctx(aff
);
2447 if (isl_local_space_check_range(aff
->ls
, type
, first
, n
) < 0)
2448 return isl_aff_free(aff
);
2450 aff
= isl_aff_cow(aff
);
2454 aff
->ls
= isl_local_space_drop_dims(aff
->ls
, type
, first
, n
);
2456 return isl_aff_free(aff
);
2458 first
+= 1 + isl_local_space_offset(aff
->ls
, type
);
2459 aff
->v
= isl_vec_drop_els(aff
->v
, first
, n
);
2461 return isl_aff_free(aff
);
2466 /* Drop the "n" domain dimensions starting at "first" from "aff",
2467 * after checking that they do not appear in the affine expression.
2469 static __isl_give isl_aff
*drop_domain(__isl_take isl_aff
*aff
, unsigned first
,
2474 involves
= isl_aff_involves_dims(aff
, isl_dim_in
, first
, n
);
2476 return isl_aff_free(aff
);
2478 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2479 "affine expression involves some of the domain dimensions",
2480 return isl_aff_free(aff
));
2481 return isl_aff_drop_dims(aff
, isl_dim_in
, first
, n
);
2484 /* Project the domain of the affine expression onto its parameter space.
2485 * The affine expression may not involve any of the domain dimensions.
2487 __isl_give isl_aff
*isl_aff_project_domain_on_params(__isl_take isl_aff
*aff
)
2492 n
= isl_aff_dim(aff
, isl_dim_in
);
2494 return isl_aff_free(aff
);
2495 aff
= drop_domain(aff
, 0, n
);
2496 space
= isl_aff_get_domain_space(aff
);
2497 space
= isl_space_params(space
);
2498 aff
= isl_aff_reset_domain_space(aff
, space
);
2502 /* Check that the domain of "aff" is a product.
2504 static isl_stat
check_domain_product(__isl_keep isl_aff
*aff
)
2506 isl_bool is_product
;
2508 is_product
= isl_space_is_product(isl_aff_peek_domain_space(aff
));
2510 return isl_stat_error
;
2512 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2513 "domain is not a product", return isl_stat_error
);
2517 /* Given an affine function with a domain of the form [A -> B] that
2518 * does not depend on B, return the same function on domain A.
2520 __isl_give isl_aff
*isl_aff_domain_factor_domain(__isl_take isl_aff
*aff
)
2525 if (check_domain_product(aff
) < 0)
2526 return isl_aff_free(aff
);
2527 space
= isl_aff_get_domain_space(aff
);
2528 n
= isl_space_dim(space
, isl_dim_set
);
2529 space
= isl_space_factor_domain(space
);
2530 n_in
= isl_space_dim(space
, isl_dim_set
);
2531 if (n
< 0 || n_in
< 0)
2532 aff
= isl_aff_free(aff
);
2534 aff
= drop_domain(aff
, n_in
, n
- n_in
);
2535 aff
= isl_aff_reset_domain_space(aff
, space
);
2539 /* Convert an affine expression defined over a parameter domain
2540 * into one that is defined over a zero-dimensional set.
2542 __isl_give isl_aff
*isl_aff_from_range(__isl_take isl_aff
*aff
)
2544 isl_local_space
*ls
;
2546 ls
= isl_aff_take_domain_local_space(aff
);
2547 ls
= isl_local_space_set_from_params(ls
);
2548 aff
= isl_aff_restore_domain_local_space(aff
, ls
);
2553 __isl_give isl_aff
*isl_aff_insert_dims(__isl_take isl_aff
*aff
,
2554 enum isl_dim_type type
, unsigned first
, unsigned n
)
2560 if (type
== isl_dim_out
)
2561 isl_die(aff
->v
->ctx
, isl_error_invalid
,
2562 "cannot insert output/set dimensions",
2563 return isl_aff_free(aff
));
2564 if (type
== isl_dim_in
)
2566 if (n
== 0 && !isl_local_space_is_named_or_nested(aff
->ls
, type
))
2569 ctx
= isl_aff_get_ctx(aff
);
2570 if (isl_local_space_check_range(aff
->ls
, type
, first
, 0) < 0)
2571 return isl_aff_free(aff
);
2573 aff
= isl_aff_cow(aff
);
2577 aff
->ls
= isl_local_space_insert_dims(aff
->ls
, type
, first
, n
);
2579 return isl_aff_free(aff
);
2581 first
+= 1 + isl_local_space_offset(aff
->ls
, type
);
2582 aff
->v
= isl_vec_insert_zero_els(aff
->v
, first
, n
);
2584 return isl_aff_free(aff
);
2589 __isl_give isl_aff
*isl_aff_add_dims(__isl_take isl_aff
*aff
,
2590 enum isl_dim_type type
, unsigned n
)
2594 pos
= isl_aff_dim(aff
, type
);
2596 return isl_aff_free(aff
);
2598 return isl_aff_insert_dims(aff
, type
, pos
, n
);
2601 __isl_give isl_pw_aff
*isl_pw_aff_add_dims(__isl_take isl_pw_aff
*pwaff
,
2602 enum isl_dim_type type
, unsigned n
)
2606 pos
= isl_pw_aff_dim(pwaff
, type
);
2608 return isl_pw_aff_free(pwaff
);
2610 return isl_pw_aff_insert_dims(pwaff
, type
, pos
, n
);
2613 /* Move the "n" dimensions of "src_type" starting at "src_pos" of "aff"
2614 * to dimensions of "dst_type" at "dst_pos".
2616 * We only support moving input dimensions to parameters and vice versa.
2618 __isl_give isl_aff
*isl_aff_move_dims(__isl_take isl_aff
*aff
,
2619 enum isl_dim_type dst_type
, unsigned dst_pos
,
2620 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
2628 !isl_local_space_is_named_or_nested(aff
->ls
, src_type
) &&
2629 !isl_local_space_is_named_or_nested(aff
->ls
, dst_type
))
2632 if (dst_type
== isl_dim_out
|| src_type
== isl_dim_out
)
2633 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2634 "cannot move output/set dimension",
2635 return isl_aff_free(aff
));
2636 if (dst_type
== isl_dim_div
|| src_type
== isl_dim_div
)
2637 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2638 "cannot move divs", return isl_aff_free(aff
));
2639 if (dst_type
== isl_dim_in
)
2640 dst_type
= isl_dim_set
;
2641 if (src_type
== isl_dim_in
)
2642 src_type
= isl_dim_set
;
2644 if (isl_local_space_check_range(aff
->ls
, src_type
, src_pos
, n
) < 0)
2645 return isl_aff_free(aff
);
2646 if (dst_type
== src_type
)
2647 isl_die(isl_aff_get_ctx(aff
), isl_error_unsupported
,
2648 "moving dims within the same type not supported",
2649 return isl_aff_free(aff
));
2651 aff
= isl_aff_cow(aff
);
2655 g_src_pos
= 1 + isl_local_space_offset(aff
->ls
, src_type
) + src_pos
;
2656 g_dst_pos
= 1 + isl_local_space_offset(aff
->ls
, dst_type
) + dst_pos
;
2657 if (dst_type
> src_type
)
2660 aff
->v
= isl_vec_move_els(aff
->v
, g_dst_pos
, g_src_pos
, n
);
2661 aff
->ls
= isl_local_space_move_dims(aff
->ls
, dst_type
, dst_pos
,
2662 src_type
, src_pos
, n
);
2663 if (!aff
->v
|| !aff
->ls
)
2664 return isl_aff_free(aff
);
2666 aff
= sort_divs(aff
);
2671 __isl_give isl_pw_aff
*isl_pw_aff_from_aff(__isl_take isl_aff
*aff
)
2673 isl_set
*dom
= isl_set_universe(isl_aff_get_domain_space(aff
));
2674 return isl_pw_aff_alloc(dom
, aff
);
2677 #define isl_aff_involves_nan isl_aff_is_nan
2680 #define PW isl_pw_aff
2684 #define EL_IS_ZERO is_empty
2688 #define IS_ZERO is_empty
2691 #undef DEFAULT_IS_ZERO
2692 #define DEFAULT_IS_ZERO 0
2698 #include <isl_pw_templ.c>
2699 #include <isl_pw_eval.c>
2700 #include <isl_pw_hash.c>
2701 #include <isl_pw_union_opt.c>
2706 #include <isl_union_single.c>
2707 #include <isl_union_neg.c>
2709 static __isl_give isl_set
*align_params_pw_pw_set_and(
2710 __isl_take isl_pw_aff
*pwaff1
, __isl_take isl_pw_aff
*pwaff2
,
2711 __isl_give isl_set
*(*fn
)(__isl_take isl_pw_aff
*pwaff1
,
2712 __isl_take isl_pw_aff
*pwaff2
))
2714 isl_bool equal_params
;
2716 if (!pwaff1
|| !pwaff2
)
2718 equal_params
= isl_space_has_equal_params(pwaff1
->dim
, pwaff2
->dim
);
2719 if (equal_params
< 0)
2722 return fn(pwaff1
, pwaff2
);
2723 if (isl_pw_aff_check_named_params(pwaff1
) < 0 ||
2724 isl_pw_aff_check_named_params(pwaff2
) < 0)
2726 pwaff1
= isl_pw_aff_align_params(pwaff1
, isl_pw_aff_get_space(pwaff2
));
2727 pwaff2
= isl_pw_aff_align_params(pwaff2
, isl_pw_aff_get_space(pwaff1
));
2728 return fn(pwaff1
, pwaff2
);
2730 isl_pw_aff_free(pwaff1
);
2731 isl_pw_aff_free(pwaff2
);
2735 /* Align the parameters of the to isl_pw_aff arguments and
2736 * then apply a function "fn" on them that returns an isl_map.
2738 static __isl_give isl_map
*align_params_pw_pw_map_and(
2739 __isl_take isl_pw_aff
*pa1
, __isl_take isl_pw_aff
*pa2
,
2740 __isl_give isl_map
*(*fn
)(__isl_take isl_pw_aff
*pa1
,
2741 __isl_take isl_pw_aff
*pa2
))
2743 isl_bool equal_params
;
2747 equal_params
= isl_space_has_equal_params(pa1
->dim
, pa2
->dim
);
2748 if (equal_params
< 0)
2751 return fn(pa1
, pa2
);
2752 if (isl_pw_aff_check_named_params(pa1
) < 0 ||
2753 isl_pw_aff_check_named_params(pa2
) < 0)
2755 pa1
= isl_pw_aff_align_params(pa1
, isl_pw_aff_get_space(pa2
));
2756 pa2
= isl_pw_aff_align_params(pa2
, isl_pw_aff_get_space(pa1
));
2757 return fn(pa1
, pa2
);
2759 isl_pw_aff_free(pa1
);
2760 isl_pw_aff_free(pa2
);
2764 /* Compute a piecewise quasi-affine expression with a domain that
2765 * is the union of those of pwaff1 and pwaff2 and such that on each
2766 * cell, the quasi-affine expression is the maximum of those of pwaff1
2767 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2768 * cell, then the associated expression is the defined one.
2770 static __isl_give isl_pw_aff
*pw_aff_union_max(__isl_take isl_pw_aff
*pwaff1
,
2771 __isl_take isl_pw_aff
*pwaff2
)
2773 return isl_pw_aff_union_opt_cmp(pwaff1
, pwaff2
, &isl_aff_ge_set
);
2776 __isl_give isl_pw_aff
*isl_pw_aff_union_max(__isl_take isl_pw_aff
*pwaff1
,
2777 __isl_take isl_pw_aff
*pwaff2
)
2779 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
,
2783 /* Compute a piecewise quasi-affine expression with a domain that
2784 * is the union of those of pwaff1 and pwaff2 and such that on each
2785 * cell, the quasi-affine expression is the minimum of those of pwaff1
2786 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2787 * cell, then the associated expression is the defined one.
2789 static __isl_give isl_pw_aff
*pw_aff_union_min(__isl_take isl_pw_aff
*pwaff1
,
2790 __isl_take isl_pw_aff
*pwaff2
)
2792 return isl_pw_aff_union_opt_cmp(pwaff1
, pwaff2
, &isl_aff_le_set
);
2795 __isl_give isl_pw_aff
*isl_pw_aff_union_min(__isl_take isl_pw_aff
*pwaff1
,
2796 __isl_take isl_pw_aff
*pwaff2
)
2798 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
,
2802 __isl_give isl_pw_aff
*isl_pw_aff_union_opt(__isl_take isl_pw_aff
*pwaff1
,
2803 __isl_take isl_pw_aff
*pwaff2
, int max
)
2806 return isl_pw_aff_union_max(pwaff1
, pwaff2
);
2808 return isl_pw_aff_union_min(pwaff1
, pwaff2
);
2811 /* Return a set containing those elements in the domain
2812 * of "pwaff" where it satisfies "fn" (if complement is 0) or
2813 * does not satisfy "fn" (if complement is 1).
2815 * The pieces with a NaN never belong to the result since
2816 * NaN does not satisfy any property.
2818 static __isl_give isl_set
*pw_aff_locus(__isl_take isl_pw_aff
*pwaff
,
2819 __isl_give isl_basic_set
*(*fn
)(__isl_take isl_aff
*aff
, int rational
),
2828 set
= isl_set_empty(isl_pw_aff_get_domain_space(pwaff
));
2830 for (i
= 0; i
< pwaff
->n
; ++i
) {
2831 isl_basic_set
*bset
;
2832 isl_set
*set_i
, *locus
;
2835 if (isl_aff_is_nan(pwaff
->p
[i
].aff
))
2838 rational
= isl_set_has_rational(pwaff
->p
[i
].set
);
2839 bset
= fn(isl_aff_copy(pwaff
->p
[i
].aff
), rational
);
2840 locus
= isl_set_from_basic_set(bset
);
2841 set_i
= isl_set_copy(pwaff
->p
[i
].set
);
2843 set_i
= isl_set_subtract(set_i
, locus
);
2845 set_i
= isl_set_intersect(set_i
, locus
);
2846 set
= isl_set_union_disjoint(set
, set_i
);
2849 isl_pw_aff_free(pwaff
);
2854 /* Return a set containing those elements in the domain
2855 * of "pa" where it is positive.
2857 __isl_give isl_set
*isl_pw_aff_pos_set(__isl_take isl_pw_aff
*pa
)
2859 return pw_aff_locus(pa
, &aff_pos_basic_set
, 0);
2862 /* Return a set containing those elements in the domain
2863 * of pwaff where it is non-negative.
2865 __isl_give isl_set
*isl_pw_aff_nonneg_set(__isl_take isl_pw_aff
*pwaff
)
2867 return pw_aff_locus(pwaff
, &aff_nonneg_basic_set
, 0);
2870 /* Return a set containing those elements in the domain
2871 * of pwaff where it is zero.
2873 __isl_give isl_set
*isl_pw_aff_zero_set(__isl_take isl_pw_aff
*pwaff
)
2875 return pw_aff_locus(pwaff
, &aff_zero_basic_set
, 0);
2878 /* Return a set containing those elements in the domain
2879 * of pwaff where it is not zero.
2881 __isl_give isl_set
*isl_pw_aff_non_zero_set(__isl_take isl_pw_aff
*pwaff
)
2883 return pw_aff_locus(pwaff
, &aff_zero_basic_set
, 1);
2886 /* Return a set containing those elements in the shared domain
2887 * of pwaff1 and pwaff2 where pwaff1 is greater than (or equal) to pwaff2.
2889 * We compute the difference on the shared domain and then construct
2890 * the set of values where this difference is non-negative.
2891 * If strict is set, we first subtract 1 from the difference.
2892 * If equal is set, we only return the elements where pwaff1 and pwaff2
2895 static __isl_give isl_set
*pw_aff_gte_set(__isl_take isl_pw_aff
*pwaff1
,
2896 __isl_take isl_pw_aff
*pwaff2
, int strict
, int equal
)
2898 isl_set
*set1
, *set2
;
2900 set1
= isl_pw_aff_domain(isl_pw_aff_copy(pwaff1
));
2901 set2
= isl_pw_aff_domain(isl_pw_aff_copy(pwaff2
));
2902 set1
= isl_set_intersect(set1
, set2
);
2903 pwaff1
= isl_pw_aff_intersect_domain(pwaff1
, isl_set_copy(set1
));
2904 pwaff2
= isl_pw_aff_intersect_domain(pwaff2
, isl_set_copy(set1
));
2905 pwaff1
= isl_pw_aff_add(pwaff1
, isl_pw_aff_neg(pwaff2
));
2908 isl_space
*space
= isl_set_get_space(set1
);
2910 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(space
));
2911 aff
= isl_aff_add_constant_si(aff
, -1);
2912 pwaff1
= isl_pw_aff_add(pwaff1
, isl_pw_aff_alloc(set1
, aff
));
2917 return isl_pw_aff_zero_set(pwaff1
);
2918 return isl_pw_aff_nonneg_set(pwaff1
);
2921 /* Return a set containing those elements in the shared domain
2922 * of pwaff1 and pwaff2 where pwaff1 is equal to pwaff2.
2924 static __isl_give isl_set
*pw_aff_eq_set(__isl_take isl_pw_aff
*pwaff1
,
2925 __isl_take isl_pw_aff
*pwaff2
)
2927 return pw_aff_gte_set(pwaff1
, pwaff2
, 0, 1);
2930 __isl_give isl_set
*isl_pw_aff_eq_set(__isl_take isl_pw_aff
*pwaff1
,
2931 __isl_take isl_pw_aff
*pwaff2
)
2933 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_eq_set
);
2936 /* Return a set containing those elements in the shared domain
2937 * of pwaff1 and pwaff2 where pwaff1 is greater than or equal to pwaff2.
2939 static __isl_give isl_set
*pw_aff_ge_set(__isl_take isl_pw_aff
*pwaff1
,
2940 __isl_take isl_pw_aff
*pwaff2
)
2942 return pw_aff_gte_set(pwaff1
, pwaff2
, 0, 0);
2945 __isl_give isl_set
*isl_pw_aff_ge_set(__isl_take isl_pw_aff
*pwaff1
,
2946 __isl_take isl_pw_aff
*pwaff2
)
2948 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_ge_set
);
2951 /* Return a set containing those elements in the shared domain
2952 * of pwaff1 and pwaff2 where pwaff1 is strictly greater than pwaff2.
2954 static __isl_give isl_set
*pw_aff_gt_set(__isl_take isl_pw_aff
*pwaff1
,
2955 __isl_take isl_pw_aff
*pwaff2
)
2957 return pw_aff_gte_set(pwaff1
, pwaff2
, 1, 0);
2960 __isl_give isl_set
*isl_pw_aff_gt_set(__isl_take isl_pw_aff
*pwaff1
,
2961 __isl_take isl_pw_aff
*pwaff2
)
2963 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_gt_set
);
2966 __isl_give isl_set
*isl_pw_aff_le_set(__isl_take isl_pw_aff
*pwaff1
,
2967 __isl_take isl_pw_aff
*pwaff2
)
2969 return isl_pw_aff_ge_set(pwaff2
, pwaff1
);
2972 __isl_give isl_set
*isl_pw_aff_lt_set(__isl_take isl_pw_aff
*pwaff1
,
2973 __isl_take isl_pw_aff
*pwaff2
)
2975 return isl_pw_aff_gt_set(pwaff2
, pwaff1
);
2978 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
2979 * where the function values are ordered in the same way as "order",
2980 * which returns a set in the shared domain of its two arguments.
2981 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
2983 * Let "pa1" and "pa2" be defined on domains A and B respectively.
2984 * We first pull back the two functions such that they are defined on
2985 * the domain [A -> B]. Then we apply "order", resulting in a set
2986 * in the space [A -> B]. Finally, we unwrap this set to obtain
2987 * a map in the space A -> B.
2989 static __isl_give isl_map
*isl_pw_aff_order_map_aligned(
2990 __isl_take isl_pw_aff
*pa1
, __isl_take isl_pw_aff
*pa2
,
2991 __isl_give isl_set
*(*order
)(__isl_take isl_pw_aff
*pa1
,
2992 __isl_take isl_pw_aff
*pa2
))
2994 isl_space
*space1
, *space2
;
2998 space1
= isl_space_domain(isl_pw_aff_get_space(pa1
));
2999 space2
= isl_space_domain(isl_pw_aff_get_space(pa2
));
3000 space1
= isl_space_map_from_domain_and_range(space1
, space2
);
3001 ma
= isl_multi_aff_domain_map(isl_space_copy(space1
));
3002 pa1
= isl_pw_aff_pullback_multi_aff(pa1
, ma
);
3003 ma
= isl_multi_aff_range_map(space1
);
3004 pa2
= isl_pw_aff_pullback_multi_aff(pa2
, ma
);
3005 set
= order(pa1
, pa2
);
3007 return isl_set_unwrap(set
);
3010 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3011 * where the function values are equal.
3012 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3014 static __isl_give isl_map
*isl_pw_aff_eq_map_aligned(__isl_take isl_pw_aff
*pa1
,
3015 __isl_take isl_pw_aff
*pa2
)
3017 return isl_pw_aff_order_map_aligned(pa1
, pa2
, &isl_pw_aff_eq_set
);
3020 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3021 * where the function values are equal.
3023 __isl_give isl_map
*isl_pw_aff_eq_map(__isl_take isl_pw_aff
*pa1
,
3024 __isl_take isl_pw_aff
*pa2
)
3026 return align_params_pw_pw_map_and(pa1
, pa2
, &isl_pw_aff_eq_map_aligned
);
3029 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3030 * where the function value of "pa1" is less than the function value of "pa2".
3031 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3033 static __isl_give isl_map
*isl_pw_aff_lt_map_aligned(__isl_take isl_pw_aff
*pa1
,
3034 __isl_take isl_pw_aff
*pa2
)
3036 return isl_pw_aff_order_map_aligned(pa1
, pa2
, &isl_pw_aff_lt_set
);
3039 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3040 * where the function value of "pa1" is less than the function value of "pa2".
3042 __isl_give isl_map
*isl_pw_aff_lt_map(__isl_take isl_pw_aff
*pa1
,
3043 __isl_take isl_pw_aff
*pa2
)
3045 return align_params_pw_pw_map_and(pa1
, pa2
, &isl_pw_aff_lt_map_aligned
);
3048 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3049 * where the function value of "pa1" is greater than the function value
3051 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3053 static __isl_give isl_map
*isl_pw_aff_gt_map_aligned(__isl_take isl_pw_aff
*pa1
,
3054 __isl_take isl_pw_aff
*pa2
)
3056 return isl_pw_aff_order_map_aligned(pa1
, pa2
, &isl_pw_aff_gt_set
);
3059 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3060 * where the function value of "pa1" is greater than the function value
3063 __isl_give isl_map
*isl_pw_aff_gt_map(__isl_take isl_pw_aff
*pa1
,
3064 __isl_take isl_pw_aff
*pa2
)
3066 return align_params_pw_pw_map_and(pa1
, pa2
, &isl_pw_aff_gt_map_aligned
);
3069 /* Return a set containing those elements in the shared domain
3070 * of the elements of list1 and list2 where each element in list1
3071 * has the relation specified by "fn" with each element in list2.
3073 static __isl_give isl_set
*pw_aff_list_set(__isl_take isl_pw_aff_list
*list1
,
3074 __isl_take isl_pw_aff_list
*list2
,
3075 __isl_give isl_set
*(*fn
)(__isl_take isl_pw_aff
*pwaff1
,
3076 __isl_take isl_pw_aff
*pwaff2
))
3082 if (!list1
|| !list2
)
3085 ctx
= isl_pw_aff_list_get_ctx(list1
);
3086 if (list1
->n
< 1 || list2
->n
< 1)
3087 isl_die(ctx
, isl_error_invalid
,
3088 "list should contain at least one element", goto error
);
3090 set
= isl_set_universe(isl_pw_aff_get_domain_space(list1
->p
[0]));
3091 for (i
= 0; i
< list1
->n
; ++i
)
3092 for (j
= 0; j
< list2
->n
; ++j
) {
3095 set_ij
= fn(isl_pw_aff_copy(list1
->p
[i
]),
3096 isl_pw_aff_copy(list2
->p
[j
]));
3097 set
= isl_set_intersect(set
, set_ij
);
3100 isl_pw_aff_list_free(list1
);
3101 isl_pw_aff_list_free(list2
);
3104 isl_pw_aff_list_free(list1
);
3105 isl_pw_aff_list_free(list2
);
3109 /* Return a set containing those elements in the shared domain
3110 * of the elements of list1 and list2 where each element in list1
3111 * is equal to each element in list2.
3113 __isl_give isl_set
*isl_pw_aff_list_eq_set(__isl_take isl_pw_aff_list
*list1
,
3114 __isl_take isl_pw_aff_list
*list2
)
3116 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_eq_set
);
3119 __isl_give isl_set
*isl_pw_aff_list_ne_set(__isl_take isl_pw_aff_list
*list1
,
3120 __isl_take isl_pw_aff_list
*list2
)
3122 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_ne_set
);
3125 /* Return a set containing those elements in the shared domain
3126 * of the elements of list1 and list2 where each element in list1
3127 * is less than or equal to each element in list2.
3129 __isl_give isl_set
*isl_pw_aff_list_le_set(__isl_take isl_pw_aff_list
*list1
,
3130 __isl_take isl_pw_aff_list
*list2
)
3132 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_le_set
);
3135 __isl_give isl_set
*isl_pw_aff_list_lt_set(__isl_take isl_pw_aff_list
*list1
,
3136 __isl_take isl_pw_aff_list
*list2
)
3138 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_lt_set
);
3141 __isl_give isl_set
*isl_pw_aff_list_ge_set(__isl_take isl_pw_aff_list
*list1
,
3142 __isl_take isl_pw_aff_list
*list2
)
3144 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_ge_set
);
3147 __isl_give isl_set
*isl_pw_aff_list_gt_set(__isl_take isl_pw_aff_list
*list1
,
3148 __isl_take isl_pw_aff_list
*list2
)
3150 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_gt_set
);
3154 /* Return a set containing those elements in the shared domain
3155 * of pwaff1 and pwaff2 where pwaff1 is not equal to pwaff2.
3157 static __isl_give isl_set
*pw_aff_ne_set(__isl_take isl_pw_aff
*pwaff1
,
3158 __isl_take isl_pw_aff
*pwaff2
)
3160 isl_set
*set_lt
, *set_gt
;
3162 set_lt
= isl_pw_aff_lt_set(isl_pw_aff_copy(pwaff1
),
3163 isl_pw_aff_copy(pwaff2
));
3164 set_gt
= isl_pw_aff_gt_set(pwaff1
, pwaff2
);
3165 return isl_set_union_disjoint(set_lt
, set_gt
);
3168 __isl_give isl_set
*isl_pw_aff_ne_set(__isl_take isl_pw_aff
*pwaff1
,
3169 __isl_take isl_pw_aff
*pwaff2
)
3171 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_ne_set
);
3174 __isl_give isl_pw_aff
*isl_pw_aff_scale_down(__isl_take isl_pw_aff
*pwaff
,
3179 if (isl_int_is_one(v
))
3181 if (!isl_int_is_pos(v
))
3182 isl_die(isl_pw_aff_get_ctx(pwaff
), isl_error_invalid
,
3183 "factor needs to be positive",
3184 return isl_pw_aff_free(pwaff
));
3185 pwaff
= isl_pw_aff_cow(pwaff
);
3191 for (i
= 0; i
< pwaff
->n
; ++i
) {
3192 pwaff
->p
[i
].aff
= isl_aff_scale_down(pwaff
->p
[i
].aff
, v
);
3193 if (!pwaff
->p
[i
].aff
)
3194 return isl_pw_aff_free(pwaff
);
3200 __isl_give isl_pw_aff
*isl_pw_aff_floor(__isl_take isl_pw_aff
*pwaff
)
3204 pwaff
= isl_pw_aff_cow(pwaff
);
3210 for (i
= 0; i
< pwaff
->n
; ++i
) {
3211 pwaff
->p
[i
].aff
= isl_aff_floor(pwaff
->p
[i
].aff
);
3212 if (!pwaff
->p
[i
].aff
)
3213 return isl_pw_aff_free(pwaff
);
3219 __isl_give isl_pw_aff
*isl_pw_aff_ceil(__isl_take isl_pw_aff
*pwaff
)
3223 pwaff
= isl_pw_aff_cow(pwaff
);
3229 for (i
= 0; i
< pwaff
->n
; ++i
) {
3230 pwaff
->p
[i
].aff
= isl_aff_ceil(pwaff
->p
[i
].aff
);
3231 if (!pwaff
->p
[i
].aff
)
3232 return isl_pw_aff_free(pwaff
);
3238 /* Assuming that "cond1" and "cond2" are disjoint,
3239 * return an affine expression that is equal to pwaff1 on cond1
3240 * and to pwaff2 on cond2.
3242 static __isl_give isl_pw_aff
*isl_pw_aff_select(
3243 __isl_take isl_set
*cond1
, __isl_take isl_pw_aff
*pwaff1
,
3244 __isl_take isl_set
*cond2
, __isl_take isl_pw_aff
*pwaff2
)
3246 pwaff1
= isl_pw_aff_intersect_domain(pwaff1
, cond1
);
3247 pwaff2
= isl_pw_aff_intersect_domain(pwaff2
, cond2
);
3249 return isl_pw_aff_add_disjoint(pwaff1
, pwaff2
);
3252 /* Return an affine expression that is equal to pwaff_true for elements
3253 * where "cond" is non-zero and to pwaff_false for elements where "cond"
3255 * That is, return cond ? pwaff_true : pwaff_false;
3257 * If "cond" involves and NaN, then we conservatively return a NaN
3258 * on its entire domain. In principle, we could consider the pieces
3259 * where it is NaN separately from those where it is not.
3261 * If "pwaff_true" and "pwaff_false" are obviously equal to each other,
3262 * then only use the domain of "cond" to restrict the domain.
3264 __isl_give isl_pw_aff
*isl_pw_aff_cond(__isl_take isl_pw_aff
*cond
,
3265 __isl_take isl_pw_aff
*pwaff_true
, __isl_take isl_pw_aff
*pwaff_false
)
3267 isl_set
*cond_true
, *cond_false
;
3272 if (isl_pw_aff_involves_nan(cond
)) {
3273 isl_space
*space
= isl_pw_aff_get_domain_space(cond
);
3274 isl_local_space
*ls
= isl_local_space_from_space(space
);
3275 isl_pw_aff_free(cond
);
3276 isl_pw_aff_free(pwaff_true
);
3277 isl_pw_aff_free(pwaff_false
);
3278 return isl_pw_aff_nan_on_domain(ls
);
3281 pwaff_true
= isl_pw_aff_align_params(pwaff_true
,
3282 isl_pw_aff_get_space(pwaff_false
));
3283 pwaff_false
= isl_pw_aff_align_params(pwaff_false
,
3284 isl_pw_aff_get_space(pwaff_true
));
3285 equal
= isl_pw_aff_plain_is_equal(pwaff_true
, pwaff_false
);
3291 dom
= isl_set_coalesce(isl_pw_aff_domain(cond
));
3292 isl_pw_aff_free(pwaff_false
);
3293 return isl_pw_aff_intersect_domain(pwaff_true
, dom
);
3296 cond_true
= isl_pw_aff_non_zero_set(isl_pw_aff_copy(cond
));
3297 cond_false
= isl_pw_aff_zero_set(cond
);
3298 return isl_pw_aff_select(cond_true
, pwaff_true
,
3299 cond_false
, pwaff_false
);
3301 isl_pw_aff_free(cond
);
3302 isl_pw_aff_free(pwaff_true
);
3303 isl_pw_aff_free(pwaff_false
);
3307 isl_bool
isl_aff_is_cst(__isl_keep isl_aff
*aff
)
3312 return isl_bool_error
;
3314 pos
= isl_seq_first_non_zero(aff
->v
->el
+ 2, aff
->v
->size
- 2);
3315 return isl_bool_ok(pos
== -1);
3318 /* Check whether pwaff is a piecewise constant.
3320 isl_bool
isl_pw_aff_is_cst(__isl_keep isl_pw_aff
*pwaff
)
3325 return isl_bool_error
;
3327 for (i
= 0; i
< pwaff
->n
; ++i
) {
3328 isl_bool is_cst
= isl_aff_is_cst(pwaff
->p
[i
].aff
);
3329 if (is_cst
< 0 || !is_cst
)
3333 return isl_bool_true
;
3336 /* Are all elements of "mpa" piecewise constants?
3338 isl_bool
isl_multi_pw_aff_is_cst(__isl_keep isl_multi_pw_aff
*mpa
)
3343 return isl_bool_error
;
3345 for (i
= 0; i
< mpa
->n
; ++i
) {
3346 isl_bool is_cst
= isl_pw_aff_is_cst(mpa
->u
.p
[i
]);
3347 if (is_cst
< 0 || !is_cst
)
3351 return isl_bool_true
;
3354 /* Return the product of "aff1" and "aff2".
3356 * If either of the two is NaN, then the result is NaN.
3358 * Otherwise, at least one of "aff1" or "aff2" needs to be a constant.
3360 __isl_give isl_aff
*isl_aff_mul(__isl_take isl_aff
*aff1
,
3361 __isl_take isl_aff
*aff2
)
3366 if (isl_aff_is_nan(aff1
)) {
3370 if (isl_aff_is_nan(aff2
)) {
3375 if (!isl_aff_is_cst(aff2
) && isl_aff_is_cst(aff1
))
3376 return isl_aff_mul(aff2
, aff1
);
3378 if (!isl_aff_is_cst(aff2
))
3379 isl_die(isl_aff_get_ctx(aff1
), isl_error_invalid
,
3380 "at least one affine expression should be constant",
3383 aff1
= isl_aff_cow(aff1
);
3387 aff1
= isl_aff_scale(aff1
, aff2
->v
->el
[1]);
3388 aff1
= isl_aff_scale_down(aff1
, aff2
->v
->el
[0]);
3398 /* Divide "aff1" by "aff2", assuming "aff2" is a constant.
3400 * If either of the two is NaN, then the result is NaN.
3402 __isl_give isl_aff
*isl_aff_div(__isl_take isl_aff
*aff1
,
3403 __isl_take isl_aff
*aff2
)
3411 if (isl_aff_is_nan(aff1
)) {
3415 if (isl_aff_is_nan(aff2
)) {
3420 is_cst
= isl_aff_is_cst(aff2
);
3424 isl_die(isl_aff_get_ctx(aff2
), isl_error_invalid
,
3425 "second argument should be a constant", goto error
);
3430 neg
= isl_int_is_neg(aff2
->v
->el
[1]);
3432 isl_int_neg(aff2
->v
->el
[0], aff2
->v
->el
[0]);
3433 isl_int_neg(aff2
->v
->el
[1], aff2
->v
->el
[1]);
3436 aff1
= isl_aff_scale(aff1
, aff2
->v
->el
[0]);
3437 aff1
= isl_aff_scale_down(aff1
, aff2
->v
->el
[1]);
3440 isl_int_neg(aff2
->v
->el
[0], aff2
->v
->el
[0]);
3441 isl_int_neg(aff2
->v
->el
[1], aff2
->v
->el
[1]);
3452 static __isl_give isl_pw_aff
*pw_aff_add(__isl_take isl_pw_aff
*pwaff1
,
3453 __isl_take isl_pw_aff
*pwaff2
)
3455 return isl_pw_aff_on_shared_domain(pwaff1
, pwaff2
, &isl_aff_add
);
3458 __isl_give isl_pw_aff
*isl_pw_aff_add(__isl_take isl_pw_aff
*pwaff1
,
3459 __isl_take isl_pw_aff
*pwaff2
)
3461 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
, &pw_aff_add
);
3464 __isl_give isl_pw_aff
*isl_pw_aff_union_add(__isl_take isl_pw_aff
*pwaff1
,
3465 __isl_take isl_pw_aff
*pwaff2
)
3467 return isl_pw_aff_union_add_(pwaff1
, pwaff2
);
3470 static __isl_give isl_pw_aff
*pw_aff_mul(__isl_take isl_pw_aff
*pwaff1
,
3471 __isl_take isl_pw_aff
*pwaff2
)
3473 return isl_pw_aff_on_shared_domain(pwaff1
, pwaff2
, &isl_aff_mul
);
3476 __isl_give isl_pw_aff
*isl_pw_aff_mul(__isl_take isl_pw_aff
*pwaff1
,
3477 __isl_take isl_pw_aff
*pwaff2
)
3479 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
, &pw_aff_mul
);
3482 static __isl_give isl_pw_aff
*pw_aff_div(__isl_take isl_pw_aff
*pa1
,
3483 __isl_take isl_pw_aff
*pa2
)
3485 return isl_pw_aff_on_shared_domain(pa1
, pa2
, &isl_aff_div
);
3488 /* Divide "pa1" by "pa2", assuming "pa2" is a piecewise constant.
3490 __isl_give isl_pw_aff
*isl_pw_aff_div(__isl_take isl_pw_aff
*pa1
,
3491 __isl_take isl_pw_aff
*pa2
)
3495 is_cst
= isl_pw_aff_is_cst(pa2
);
3499 isl_die(isl_pw_aff_get_ctx(pa2
), isl_error_invalid
,
3500 "second argument should be a piecewise constant",
3502 return isl_pw_aff_align_params_pw_pw_and(pa1
, pa2
, &pw_aff_div
);
3504 isl_pw_aff_free(pa1
);
3505 isl_pw_aff_free(pa2
);
3509 /* Compute the quotient of the integer division of "pa1" by "pa2"
3510 * with rounding towards zero.
3511 * "pa2" is assumed to be a piecewise constant.
3513 * In particular, return
3515 * pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2)
3518 __isl_give isl_pw_aff
*isl_pw_aff_tdiv_q(__isl_take isl_pw_aff
*pa1
,
3519 __isl_take isl_pw_aff
*pa2
)
3525 is_cst
= isl_pw_aff_is_cst(pa2
);
3529 isl_die(isl_pw_aff_get_ctx(pa2
), isl_error_invalid
,
3530 "second argument should be a piecewise constant",
3533 pa1
= isl_pw_aff_div(pa1
, pa2
);
3535 cond
= isl_pw_aff_nonneg_set(isl_pw_aff_copy(pa1
));
3536 f
= isl_pw_aff_floor(isl_pw_aff_copy(pa1
));
3537 c
= isl_pw_aff_ceil(pa1
);
3538 return isl_pw_aff_cond(isl_set_indicator_function(cond
), f
, c
);
3540 isl_pw_aff_free(pa1
);
3541 isl_pw_aff_free(pa2
);
3545 /* Compute the remainder of the integer division of "pa1" by "pa2"
3546 * with rounding towards zero.
3547 * "pa2" is assumed to be a piecewise constant.
3549 * In particular, return
3551 * pa1 - pa2 * (pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2))
3554 __isl_give isl_pw_aff
*isl_pw_aff_tdiv_r(__isl_take isl_pw_aff
*pa1
,
3555 __isl_take isl_pw_aff
*pa2
)
3560 is_cst
= isl_pw_aff_is_cst(pa2
);
3564 isl_die(isl_pw_aff_get_ctx(pa2
), isl_error_invalid
,
3565 "second argument should be a piecewise constant",
3567 res
= isl_pw_aff_tdiv_q(isl_pw_aff_copy(pa1
), isl_pw_aff_copy(pa2
));
3568 res
= isl_pw_aff_mul(pa2
, res
);
3569 res
= isl_pw_aff_sub(pa1
, res
);
3572 isl_pw_aff_free(pa1
);
3573 isl_pw_aff_free(pa2
);
3577 /* Does either of "pa1" or "pa2" involve any NaN2?
3579 static isl_bool
either_involves_nan(__isl_keep isl_pw_aff
*pa1
,
3580 __isl_keep isl_pw_aff
*pa2
)
3584 has_nan
= isl_pw_aff_involves_nan(pa1
);
3585 if (has_nan
< 0 || has_nan
)
3587 return isl_pw_aff_involves_nan(pa2
);
3590 /* Replace "pa1" and "pa2" (at least one of which involves a NaN)
3591 * by a NaN on their shared domain.
3593 * In principle, the result could be refined to only being NaN
3594 * on the parts of this domain where at least one of "pa1" or "pa2" is NaN.
3596 static __isl_give isl_pw_aff
*replace_by_nan(__isl_take isl_pw_aff
*pa1
,
3597 __isl_take isl_pw_aff
*pa2
)
3599 isl_local_space
*ls
;
3603 dom
= isl_set_intersect(isl_pw_aff_domain(pa1
), isl_pw_aff_domain(pa2
));
3604 ls
= isl_local_space_from_space(isl_set_get_space(dom
));
3605 pa
= isl_pw_aff_nan_on_domain(ls
);
3606 pa
= isl_pw_aff_intersect_domain(pa
, dom
);
3611 static __isl_give isl_pw_aff
*pw_aff_min(__isl_take isl_pw_aff
*pwaff1
,
3612 __isl_take isl_pw_aff
*pwaff2
)
3617 dom
= isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1
)),
3618 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2
)));
3619 le
= isl_pw_aff_le_set(isl_pw_aff_copy(pwaff1
),
3620 isl_pw_aff_copy(pwaff2
));
3621 dom
= isl_set_subtract(dom
, isl_set_copy(le
));
3622 return isl_pw_aff_select(le
, pwaff1
, dom
, pwaff2
);
3625 static __isl_give isl_pw_aff
*pw_aff_max(__isl_take isl_pw_aff
*pwaff1
,
3626 __isl_take isl_pw_aff
*pwaff2
)
3631 dom
= isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1
)),
3632 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2
)));
3633 ge
= isl_pw_aff_ge_set(isl_pw_aff_copy(pwaff1
),
3634 isl_pw_aff_copy(pwaff2
));
3635 dom
= isl_set_subtract(dom
, isl_set_copy(ge
));
3636 return isl_pw_aff_select(ge
, pwaff1
, dom
, pwaff2
);
3639 /* Return an expression for the minimum (if "max" is not set) or
3640 * the maximum (if "max" is set) of "pa1" and "pa2".
3641 * If either expression involves any NaN, then return a NaN
3642 * on the shared domain as result.
3644 static __isl_give isl_pw_aff
*pw_aff_min_max(__isl_take isl_pw_aff
*pa1
,
3645 __isl_take isl_pw_aff
*pa2
, int max
)
3649 has_nan
= either_involves_nan(pa1
, pa2
);
3651 pa1
= isl_pw_aff_free(pa1
);
3653 return replace_by_nan(pa1
, pa2
);
3656 return isl_pw_aff_align_params_pw_pw_and(pa1
, pa2
, &pw_aff_max
);
3658 return isl_pw_aff_align_params_pw_pw_and(pa1
, pa2
, &pw_aff_min
);
3661 /* Return an expression for the minimum of "pwaff1" and "pwaff2".
3663 __isl_give isl_pw_aff
*isl_pw_aff_min(__isl_take isl_pw_aff
*pwaff1
,
3664 __isl_take isl_pw_aff
*pwaff2
)
3666 return pw_aff_min_max(pwaff1
, pwaff2
, 0);
3669 /* Return an expression for the maximum of "pwaff1" and "pwaff2".
3671 __isl_give isl_pw_aff
*isl_pw_aff_max(__isl_take isl_pw_aff
*pwaff1
,
3672 __isl_take isl_pw_aff
*pwaff2
)
3674 return pw_aff_min_max(pwaff1
, pwaff2
, 1);
3677 static __isl_give isl_pw_aff
*pw_aff_list_reduce(
3678 __isl_take isl_pw_aff_list
*list
,
3679 __isl_give isl_pw_aff
*(*fn
)(__isl_take isl_pw_aff
*pwaff1
,
3680 __isl_take isl_pw_aff
*pwaff2
))
3689 ctx
= isl_pw_aff_list_get_ctx(list
);
3691 isl_die(ctx
, isl_error_invalid
,
3692 "list should contain at least one element", goto error
);
3694 res
= isl_pw_aff_copy(list
->p
[0]);
3695 for (i
= 1; i
< list
->n
; ++i
)
3696 res
= fn(res
, isl_pw_aff_copy(list
->p
[i
]));
3698 isl_pw_aff_list_free(list
);
3701 isl_pw_aff_list_free(list
);
3705 /* Return an isl_pw_aff that maps each element in the intersection of the
3706 * domains of the elements of list to the minimal corresponding affine
3709 __isl_give isl_pw_aff
*isl_pw_aff_list_min(__isl_take isl_pw_aff_list
*list
)
3711 return pw_aff_list_reduce(list
, &isl_pw_aff_min
);
3714 /* Return an isl_pw_aff that maps each element in the intersection of the
3715 * domains of the elements of list to the maximal corresponding affine
3718 __isl_give isl_pw_aff
*isl_pw_aff_list_max(__isl_take isl_pw_aff_list
*list
)
3720 return pw_aff_list_reduce(list
, &isl_pw_aff_max
);
3723 /* Mark the domains of "pwaff" as rational.
3725 __isl_give isl_pw_aff
*isl_pw_aff_set_rational(__isl_take isl_pw_aff
*pwaff
)
3729 pwaff
= isl_pw_aff_cow(pwaff
);
3735 for (i
= 0; i
< pwaff
->n
; ++i
) {
3736 pwaff
->p
[i
].set
= isl_set_set_rational(pwaff
->p
[i
].set
);
3737 if (!pwaff
->p
[i
].set
)
3738 return isl_pw_aff_free(pwaff
);
3744 /* Mark the domains of the elements of "list" as rational.
3746 __isl_give isl_pw_aff_list
*isl_pw_aff_list_set_rational(
3747 __isl_take isl_pw_aff_list
*list
)
3757 for (i
= 0; i
< n
; ++i
) {
3760 pa
= isl_pw_aff_list_get_pw_aff(list
, i
);
3761 pa
= isl_pw_aff_set_rational(pa
);
3762 list
= isl_pw_aff_list_set_pw_aff(list
, i
, pa
);
3768 /* Do the parameters of "aff" match those of "space"?
3770 isl_bool
isl_aff_matching_params(__isl_keep isl_aff
*aff
,
3771 __isl_keep isl_space
*space
)
3773 isl_space
*aff_space
;
3777 return isl_bool_error
;
3779 aff_space
= isl_aff_get_domain_space(aff
);
3781 match
= isl_space_has_equal_params(space
, aff_space
);
3783 isl_space_free(aff_space
);
3787 /* Check that the domain space of "aff" matches "space".
3789 isl_stat
isl_aff_check_match_domain_space(__isl_keep isl_aff
*aff
,
3790 __isl_keep isl_space
*space
)
3792 isl_space
*aff_space
;
3796 return isl_stat_error
;
3798 aff_space
= isl_aff_get_domain_space(aff
);
3800 match
= isl_space_has_equal_params(space
, aff_space
);
3804 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
3805 "parameters don't match", goto error
);
3806 match
= isl_space_tuple_is_equal(space
, isl_dim_in
,
3807 aff_space
, isl_dim_set
);
3811 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
3812 "domains don't match", goto error
);
3813 isl_space_free(aff_space
);
3816 isl_space_free(aff_space
);
3817 return isl_stat_error
;
3825 #include <isl_multi_no_explicit_domain.c>
3826 #include <isl_multi_templ.c>
3827 #include <isl_multi_apply_set.c>
3828 #include <isl_multi_cmp.c>
3829 #include <isl_multi_dims.c>
3830 #include <isl_multi_floor.c>
3831 #include <isl_multi_from_base_templ.c>
3832 #include <isl_multi_gist.c>
3833 #include <isl_multi_identity_templ.c>
3834 #include <isl_multi_move_dims_templ.c>
3835 #include <isl_multi_product_templ.c>
3836 #include <isl_multi_splice_templ.c>
3837 #include <isl_multi_zero_templ.c>
3839 /* Construct an isl_multi_aff living in "space" that corresponds
3840 * to the affine transformation matrix "mat".
3842 __isl_give isl_multi_aff
*isl_multi_aff_from_aff_mat(
3843 __isl_take isl_space
*space
, __isl_take isl_mat
*mat
)
3846 isl_local_space
*ls
= NULL
;
3847 isl_multi_aff
*ma
= NULL
;
3848 isl_size n_row
, n_col
, n_out
, total
;
3854 ctx
= isl_mat_get_ctx(mat
);
3856 n_row
= isl_mat_rows(mat
);
3857 n_col
= isl_mat_cols(mat
);
3858 n_out
= isl_space_dim(space
, isl_dim_out
);
3859 total
= isl_space_dim(space
, isl_dim_all
);
3860 if (n_row
< 0 || n_col
< 0 || n_out
< 0 || total
< 0)
3863 isl_die(ctx
, isl_error_invalid
,
3864 "insufficient number of rows", goto error
);
3866 isl_die(ctx
, isl_error_invalid
,
3867 "insufficient number of columns", goto error
);
3868 if (1 + n_out
!= n_row
|| 2 + total
!= n_row
+ n_col
)
3869 isl_die(ctx
, isl_error_invalid
,
3870 "dimension mismatch", goto error
);
3872 ma
= isl_multi_aff_zero(isl_space_copy(space
));
3873 ls
= isl_local_space_from_space(isl_space_domain(space
));
3875 for (i
= 0; i
< n_row
- 1; ++i
) {
3879 v
= isl_vec_alloc(ctx
, 1 + n_col
);
3882 isl_int_set(v
->el
[0], mat
->row
[0][0]);
3883 isl_seq_cpy(v
->el
+ 1, mat
->row
[1 + i
], n_col
);
3884 v
= isl_vec_normalize(v
);
3885 aff
= isl_aff_alloc_vec(isl_local_space_copy(ls
), v
);
3886 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
3889 isl_local_space_free(ls
);
3893 isl_local_space_free(ls
);
3895 isl_multi_aff_free(ma
);
3899 /* Remove any internal structure of the domain of "ma".
3900 * If there is any such internal structure in the input,
3901 * then the name of the corresponding space is also removed.
3903 __isl_give isl_multi_aff
*isl_multi_aff_flatten_domain(
3904 __isl_take isl_multi_aff
*ma
)
3911 if (!ma
->space
->nested
[0])
3914 space
= isl_multi_aff_get_space(ma
);
3915 space
= isl_space_flatten_domain(space
);
3916 ma
= isl_multi_aff_reset_space(ma
, space
);
3921 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
3922 * of the space to its domain.
3924 __isl_give isl_multi_aff
*isl_multi_aff_domain_map(__isl_take isl_space
*space
)
3928 isl_local_space
*ls
;
3933 if (!isl_space_is_map(space
))
3934 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
3935 "not a map space", goto error
);
3937 n_in
= isl_space_dim(space
, isl_dim_in
);
3940 space
= isl_space_domain_map(space
);
3942 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
3944 isl_space_free(space
);
3948 space
= isl_space_domain(space
);
3949 ls
= isl_local_space_from_space(space
);
3950 for (i
= 0; i
< n_in
; ++i
) {
3953 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
3955 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
3957 isl_local_space_free(ls
);
3960 isl_space_free(space
);
3964 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
3965 * of the space to its range.
3967 __isl_give isl_multi_aff
*isl_multi_aff_range_map(__isl_take isl_space
*space
)
3970 isl_size n_in
, n_out
;
3971 isl_local_space
*ls
;
3976 if (!isl_space_is_map(space
))
3977 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
3978 "not a map space", goto error
);
3980 n_in
= isl_space_dim(space
, isl_dim_in
);
3981 n_out
= isl_space_dim(space
, isl_dim_out
);
3982 if (n_in
< 0 || n_out
< 0)
3984 space
= isl_space_range_map(space
);
3986 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
3988 isl_space_free(space
);
3992 space
= isl_space_domain(space
);
3993 ls
= isl_local_space_from_space(space
);
3994 for (i
= 0; i
< n_out
; ++i
) {
3997 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
3998 isl_dim_set
, n_in
+ i
);
3999 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
4001 isl_local_space_free(ls
);
4004 isl_space_free(space
);
4008 /* Given a map space, return an isl_pw_multi_aff that maps a wrapped copy
4009 * of the space to its range.
4011 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_range_map(
4012 __isl_take isl_space
*space
)
4014 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_range_map(space
));
4017 /* Given the space of a set and a range of set dimensions,
4018 * construct an isl_multi_aff that projects out those dimensions.
4020 __isl_give isl_multi_aff
*isl_multi_aff_project_out_map(
4021 __isl_take isl_space
*space
, enum isl_dim_type type
,
4022 unsigned first
, unsigned n
)
4026 isl_local_space
*ls
;
4031 if (!isl_space_is_set(space
))
4032 isl_die(isl_space_get_ctx(space
), isl_error_unsupported
,
4033 "expecting set space", goto error
);
4034 if (type
!= isl_dim_set
)
4035 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
4036 "only set dimensions can be projected out", goto error
);
4037 if (isl_space_check_range(space
, type
, first
, n
) < 0)
4040 dim
= isl_space_dim(space
, isl_dim_set
);
4044 space
= isl_space_from_domain(space
);
4045 space
= isl_space_add_dims(space
, isl_dim_out
, dim
- n
);
4048 return isl_multi_aff_alloc(space
);
4050 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
4051 space
= isl_space_domain(space
);
4052 ls
= isl_local_space_from_space(space
);
4054 for (i
= 0; i
< first
; ++i
) {
4057 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
4059 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
4062 for (i
= 0; i
< dim
- (first
+ n
); ++i
) {
4065 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
4066 isl_dim_set
, first
+ n
+ i
);
4067 ma
= isl_multi_aff_set_aff(ma
, first
+ i
, aff
);
4070 isl_local_space_free(ls
);
4073 isl_space_free(space
);
4077 /* Given the space of a set and a range of set dimensions,
4078 * construct an isl_pw_multi_aff that projects out those dimensions.
4080 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_project_out_map(
4081 __isl_take isl_space
*space
, enum isl_dim_type type
,
4082 unsigned first
, unsigned n
)
4086 ma
= isl_multi_aff_project_out_map(space
, type
, first
, n
);
4087 return isl_pw_multi_aff_from_multi_aff(ma
);
4090 /* Create an isl_pw_multi_aff with the given isl_multi_aff on a universe
4093 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_multi_aff(
4094 __isl_take isl_multi_aff
*ma
)
4096 isl_set
*dom
= isl_set_universe(isl_multi_aff_get_domain_space(ma
));
4097 return isl_pw_multi_aff_alloc(dom
, ma
);
4100 /* Create a piecewise multi-affine expression in the given space that maps each
4101 * input dimension to the corresponding output dimension.
4103 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_identity(
4104 __isl_take isl_space
*space
)
4106 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_identity(space
));
4109 /* Exploit the equalities in "eq" to simplify the affine expressions.
4111 static __isl_give isl_multi_aff
*isl_multi_aff_substitute_equalities(
4112 __isl_take isl_multi_aff
*maff
, __isl_take isl_basic_set
*eq
)
4116 maff
= isl_multi_aff_cow(maff
);
4120 for (i
= 0; i
< maff
->n
; ++i
) {
4121 maff
->u
.p
[i
] = isl_aff_substitute_equalities(maff
->u
.p
[i
],
4122 isl_basic_set_copy(eq
));
4127 isl_basic_set_free(eq
);
4130 isl_basic_set_free(eq
);
4131 isl_multi_aff_free(maff
);
4135 __isl_give isl_multi_aff
*isl_multi_aff_scale(__isl_take isl_multi_aff
*maff
,
4140 maff
= isl_multi_aff_cow(maff
);
4144 for (i
= 0; i
< maff
->n
; ++i
) {
4145 maff
->u
.p
[i
] = isl_aff_scale(maff
->u
.p
[i
], f
);
4147 return isl_multi_aff_free(maff
);
4153 __isl_give isl_multi_aff
*isl_multi_aff_add_on_domain(__isl_keep isl_set
*dom
,
4154 __isl_take isl_multi_aff
*maff1
, __isl_take isl_multi_aff
*maff2
)
4156 maff1
= isl_multi_aff_add(maff1
, maff2
);
4157 maff1
= isl_multi_aff_gist(maff1
, isl_set_copy(dom
));
4161 int isl_multi_aff_is_empty(__isl_keep isl_multi_aff
*maff
)
4169 /* Return the set of domain elements where "ma1" is lexicographically
4170 * smaller than or equal to "ma2".
4172 __isl_give isl_set
*isl_multi_aff_lex_le_set(__isl_take isl_multi_aff
*ma1
,
4173 __isl_take isl_multi_aff
*ma2
)
4175 return isl_multi_aff_lex_ge_set(ma2
, ma1
);
4178 /* Return the set of domain elements where "ma1" is lexicographically
4179 * smaller than "ma2".
4181 __isl_give isl_set
*isl_multi_aff_lex_lt_set(__isl_take isl_multi_aff
*ma1
,
4182 __isl_take isl_multi_aff
*ma2
)
4184 return isl_multi_aff_lex_gt_set(ma2
, ma1
);
4187 /* Return the set of domain elements where "ma1" and "ma2"
4190 static __isl_give isl_set
*isl_multi_aff_order_set(
4191 __isl_take isl_multi_aff
*ma1
, __isl_take isl_multi_aff
*ma2
,
4192 __isl_give isl_map
*order(__isl_take isl_space
*set_space
))
4195 isl_map
*map1
, *map2
;
4198 map1
= isl_map_from_multi_aff_internal(ma1
);
4199 map2
= isl_map_from_multi_aff_internal(ma2
);
4200 map
= isl_map_range_product(map1
, map2
);
4201 space
= isl_space_range(isl_map_get_space(map
));
4202 space
= isl_space_domain(isl_space_unwrap(space
));
4204 map
= isl_map_intersect_range(map
, isl_map_wrap(ge
));
4206 return isl_map_domain(map
);
4209 /* Return the set of domain elements where "ma1" is lexicographically
4210 * greater than or equal to "ma2".
4212 __isl_give isl_set
*isl_multi_aff_lex_ge_set(__isl_take isl_multi_aff
*ma1
,
4213 __isl_take isl_multi_aff
*ma2
)
4215 return isl_multi_aff_order_set(ma1
, ma2
, &isl_map_lex_ge
);
4218 /* Return the set of domain elements where "ma1" is lexicographically
4219 * greater than "ma2".
4221 __isl_give isl_set
*isl_multi_aff_lex_gt_set(__isl_take isl_multi_aff
*ma1
,
4222 __isl_take isl_multi_aff
*ma2
)
4224 return isl_multi_aff_order_set(ma1
, ma2
, &isl_map_lex_gt
);
4228 #define PW isl_pw_multi_aff
4230 #define EL isl_multi_aff
4232 #define EL_IS_ZERO is_empty
4236 #define IS_ZERO is_empty
4239 #undef DEFAULT_IS_ZERO
4240 #define DEFAULT_IS_ZERO 0
4244 #define NO_INSERT_DIMS
4248 #include <isl_pw_templ.c>
4249 #include <isl_pw_union_opt.c>
4254 #define BASE pw_multi_aff
4256 #include <isl_union_multi.c>
4257 #include <isl_union_neg.c>
4259 static __isl_give isl_pw_multi_aff
*pw_multi_aff_union_lexmax(
4260 __isl_take isl_pw_multi_aff
*pma1
,
4261 __isl_take isl_pw_multi_aff
*pma2
)
4263 return isl_pw_multi_aff_union_opt_cmp(pma1
, pma2
,
4264 &isl_multi_aff_lex_ge_set
);
4267 /* Given two piecewise multi affine expressions, return a piecewise
4268 * multi-affine expression defined on the union of the definition domains
4269 * of the inputs that is equal to the lexicographic maximum of the two
4270 * inputs on each cell. If only one of the two inputs is defined on
4271 * a given cell, then it is considered to be the maximum.
4273 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_union_lexmax(
4274 __isl_take isl_pw_multi_aff
*pma1
,
4275 __isl_take isl_pw_multi_aff
*pma2
)
4277 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
4278 &pw_multi_aff_union_lexmax
);
4281 static __isl_give isl_pw_multi_aff
*pw_multi_aff_union_lexmin(
4282 __isl_take isl_pw_multi_aff
*pma1
,
4283 __isl_take isl_pw_multi_aff
*pma2
)
4285 return isl_pw_multi_aff_union_opt_cmp(pma1
, pma2
,
4286 &isl_multi_aff_lex_le_set
);
4289 /* Given two piecewise multi affine expressions, return a piecewise
4290 * multi-affine expression defined on the union of the definition domains
4291 * of the inputs that is equal to the lexicographic minimum of the two
4292 * inputs on each cell. If only one of the two inputs is defined on
4293 * a given cell, then it is considered to be the minimum.
4295 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_union_lexmin(
4296 __isl_take isl_pw_multi_aff
*pma1
,
4297 __isl_take isl_pw_multi_aff
*pma2
)
4299 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
4300 &pw_multi_aff_union_lexmin
);
4303 static __isl_give isl_pw_multi_aff
*pw_multi_aff_add(
4304 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4306 return isl_pw_multi_aff_on_shared_domain(pma1
, pma2
,
4307 &isl_multi_aff_add
);
4310 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_add(
4311 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4313 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
4317 static __isl_give isl_pw_multi_aff
*pw_multi_aff_sub(
4318 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4320 return isl_pw_multi_aff_on_shared_domain(pma1
, pma2
,
4321 &isl_multi_aff_sub
);
4324 /* Subtract "pma2" from "pma1" and return the result.
4326 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_sub(
4327 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4329 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
4333 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_union_add(
4334 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4336 return isl_pw_multi_aff_union_add_(pma1
, pma2
);
4339 /* Compute the sum of "upa1" and "upa2" on the union of their domains,
4340 * with the actual sum on the shared domain and
4341 * the defined expression on the symmetric difference of the domains.
4343 __isl_give isl_union_pw_aff
*isl_union_pw_aff_union_add(
4344 __isl_take isl_union_pw_aff
*upa1
, __isl_take isl_union_pw_aff
*upa2
)
4346 return isl_union_pw_aff_union_add_(upa1
, upa2
);
4349 /* Compute the sum of "upma1" and "upma2" on the union of their domains,
4350 * with the actual sum on the shared domain and
4351 * the defined expression on the symmetric difference of the domains.
4353 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_union_add(
4354 __isl_take isl_union_pw_multi_aff
*upma1
,
4355 __isl_take isl_union_pw_multi_aff
*upma2
)
4357 return isl_union_pw_multi_aff_union_add_(upma1
, upma2
);
4360 /* Given two piecewise multi-affine expressions A -> B and C -> D,
4361 * construct a piecewise multi-affine expression [A -> C] -> [B -> D].
4363 static __isl_give isl_pw_multi_aff
*pw_multi_aff_product(
4364 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4368 isl_pw_multi_aff
*res
;
4373 n
= pma1
->n
* pma2
->n
;
4374 space
= isl_space_product(isl_space_copy(pma1
->dim
),
4375 isl_space_copy(pma2
->dim
));
4376 res
= isl_pw_multi_aff_alloc_size(space
, n
);
4378 for (i
= 0; i
< pma1
->n
; ++i
) {
4379 for (j
= 0; j
< pma2
->n
; ++j
) {
4383 domain
= isl_set_product(isl_set_copy(pma1
->p
[i
].set
),
4384 isl_set_copy(pma2
->p
[j
].set
));
4385 ma
= isl_multi_aff_product(
4386 isl_multi_aff_copy(pma1
->p
[i
].maff
),
4387 isl_multi_aff_copy(pma2
->p
[j
].maff
));
4388 res
= isl_pw_multi_aff_add_piece(res
, domain
, ma
);
4392 isl_pw_multi_aff_free(pma1
);
4393 isl_pw_multi_aff_free(pma2
);
4396 isl_pw_multi_aff_free(pma1
);
4397 isl_pw_multi_aff_free(pma2
);
4401 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_product(
4402 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4404 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
4405 &pw_multi_aff_product
);
4408 /* Subtract the initial "n" elements in "ma" with coefficients in "c" and
4409 * denominator "denom".
4410 * "denom" is allowed to be negative, in which case the actual denominator
4411 * is -denom and the expressions are added instead.
4413 static __isl_give isl_aff
*subtract_initial(__isl_take isl_aff
*aff
,
4414 __isl_keep isl_multi_aff
*ma
, int n
, isl_int
*c
, isl_int denom
)
4420 first
= isl_seq_first_non_zero(c
, n
);
4424 sign
= isl_int_sgn(denom
);
4426 isl_int_abs(d
, denom
);
4427 for (i
= first
; i
< n
; ++i
) {
4430 if (isl_int_is_zero(c
[i
]))
4432 aff_i
= isl_multi_aff_get_aff(ma
, i
);
4433 aff_i
= isl_aff_scale(aff_i
, c
[i
]);
4434 aff_i
= isl_aff_scale_down(aff_i
, d
);
4436 aff
= isl_aff_sub(aff
, aff_i
);
4438 aff
= isl_aff_add(aff
, aff_i
);
4445 /* Extract an affine expression that expresses the output dimension "pos"
4446 * of "bmap" in terms of the parameters and input dimensions from
4448 * Note that this expression may involve integer divisions defined
4449 * in terms of parameters and input dimensions.
4450 * The equality may also involve references to earlier (but not later)
4451 * output dimensions. These are replaced by the corresponding elements
4454 * If the equality is of the form
4456 * f(i) + h(j) + a x + g(i) = 0,
4458 * with f(i) a linear combinations of the parameters and input dimensions,
4459 * g(i) a linear combination of integer divisions defined in terms of the same
4460 * and h(j) a linear combinations of earlier output dimensions,
4461 * then the affine expression is
4463 * (-f(i) - g(i))/a - h(j)/a
4465 * If the equality is of the form
4467 * f(i) + h(j) - a x + g(i) = 0,
4469 * then the affine expression is
4471 * (f(i) + g(i))/a - h(j)/(-a)
4474 * If "div" refers to an integer division (i.e., it is smaller than
4475 * the number of integer divisions), then the equality constraint
4476 * does involve an integer division (the one at position "div") that
4477 * is defined in terms of output dimensions. However, this integer
4478 * division can be eliminated by exploiting a pair of constraints
4479 * x >= l and x <= l + n, with n smaller than the coefficient of "div"
4480 * in the equality constraint. "ineq" refers to inequality x >= l, i.e.,
4482 * In particular, let
4484 * x = e(i) + m floor(...)
4486 * with e(i) the expression derived above and floor(...) the integer
4487 * division involving output dimensions.
4498 * e(i) + m floor(...) - l = (e(i) + m floor(...) - l) mod m
4499 * = (e(i) - l) mod m
4503 * x - l = (e(i) - l) mod m
4507 * x = ((e(i) - l) mod m) + l
4509 * The variable "shift" below contains the expression -l, which may
4510 * also involve a linear combination of earlier output dimensions.
4512 static __isl_give isl_aff
*extract_aff_from_equality(
4513 __isl_keep isl_basic_map
*bmap
, int pos
, int eq
, int div
, int ineq
,
4514 __isl_keep isl_multi_aff
*ma
)
4517 isl_size n_div
, n_out
;
4519 isl_local_space
*ls
;
4520 isl_aff
*aff
, *shift
;
4523 ctx
= isl_basic_map_get_ctx(bmap
);
4524 ls
= isl_basic_map_get_local_space(bmap
);
4525 ls
= isl_local_space_domain(ls
);
4526 aff
= isl_aff_alloc(isl_local_space_copy(ls
));
4529 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
4530 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4531 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4532 if (n_out
< 0 || n_div
< 0)
4534 if (isl_int_is_neg(bmap
->eq
[eq
][o_out
+ pos
])) {
4535 isl_seq_cpy(aff
->v
->el
+ 1, bmap
->eq
[eq
], o_out
);
4536 isl_seq_cpy(aff
->v
->el
+ 1 + o_out
,
4537 bmap
->eq
[eq
] + o_out
+ n_out
, n_div
);
4539 isl_seq_neg(aff
->v
->el
+ 1, bmap
->eq
[eq
], o_out
);
4540 isl_seq_neg(aff
->v
->el
+ 1 + o_out
,
4541 bmap
->eq
[eq
] + o_out
+ n_out
, n_div
);
4544 isl_int_set_si(aff
->v
->el
[1 + o_out
+ div
], 0);
4545 isl_int_abs(aff
->v
->el
[0], bmap
->eq
[eq
][o_out
+ pos
]);
4546 aff
= subtract_initial(aff
, ma
, pos
, bmap
->eq
[eq
] + o_out
,
4547 bmap
->eq
[eq
][o_out
+ pos
]);
4549 shift
= isl_aff_alloc(isl_local_space_copy(ls
));
4552 isl_seq_cpy(shift
->v
->el
+ 1, bmap
->ineq
[ineq
], o_out
);
4553 isl_seq_cpy(shift
->v
->el
+ 1 + o_out
,
4554 bmap
->ineq
[ineq
] + o_out
+ n_out
, n_div
);
4555 isl_int_set_si(shift
->v
->el
[0], 1);
4556 shift
= subtract_initial(shift
, ma
, pos
,
4557 bmap
->ineq
[ineq
] + o_out
, ctx
->negone
);
4558 aff
= isl_aff_add(aff
, isl_aff_copy(shift
));
4559 mod
= isl_val_int_from_isl_int(ctx
,
4560 bmap
->eq
[eq
][o_out
+ n_out
+ div
]);
4561 mod
= isl_val_abs(mod
);
4562 aff
= isl_aff_mod_val(aff
, mod
);
4563 aff
= isl_aff_sub(aff
, shift
);
4566 isl_local_space_free(ls
);
4569 isl_local_space_free(ls
);
4574 /* Given a basic map with output dimensions defined
4575 * in terms of the parameters input dimensions and earlier
4576 * output dimensions using an equality (and possibly a pair on inequalities),
4577 * extract an isl_aff that expresses output dimension "pos" in terms
4578 * of the parameters and input dimensions.
4579 * Note that this expression may involve integer divisions defined
4580 * in terms of parameters and input dimensions.
4581 * "ma" contains the expressions corresponding to earlier output dimensions.
4583 * This function shares some similarities with
4584 * isl_basic_map_has_defining_equality and isl_constraint_get_bound.
4586 static __isl_give isl_aff
*extract_isl_aff_from_basic_map(
4587 __isl_keep isl_basic_map
*bmap
, int pos
, __isl_keep isl_multi_aff
*ma
)
4594 eq
= isl_basic_map_output_defining_equality(bmap
, pos
, &div
, &ineq
);
4595 if (eq
>= bmap
->n_eq
)
4596 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
4597 "unable to find suitable equality", return NULL
);
4598 aff
= extract_aff_from_equality(bmap
, pos
, eq
, div
, ineq
, ma
);
4600 aff
= isl_aff_remove_unused_divs(aff
);
4604 /* Given a basic map where each output dimension is defined
4605 * in terms of the parameters and input dimensions using an equality,
4606 * extract an isl_multi_aff that expresses the output dimensions in terms
4607 * of the parameters and input dimensions.
4609 static __isl_give isl_multi_aff
*extract_isl_multi_aff_from_basic_map(
4610 __isl_take isl_basic_map
*bmap
)
4619 ma
= isl_multi_aff_alloc(isl_basic_map_get_space(bmap
));
4620 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4622 ma
= isl_multi_aff_free(ma
);
4624 for (i
= 0; i
< n_out
; ++i
) {
4627 aff
= extract_isl_aff_from_basic_map(bmap
, i
, ma
);
4628 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
4631 isl_basic_map_free(bmap
);
4636 /* Given a basic set where each set dimension is defined
4637 * in terms of the parameters using an equality,
4638 * extract an isl_multi_aff that expresses the set dimensions in terms
4639 * of the parameters.
4641 __isl_give isl_multi_aff
*isl_multi_aff_from_basic_set_equalities(
4642 __isl_take isl_basic_set
*bset
)
4644 return extract_isl_multi_aff_from_basic_map(bset
);
4647 /* Create an isl_pw_multi_aff that is equivalent to
4648 * isl_map_intersect_domain(isl_map_from_basic_map(bmap), domain).
4649 * The given basic map is such that each output dimension is defined
4650 * in terms of the parameters and input dimensions using an equality.
4652 * Since some applications expect the result of isl_pw_multi_aff_from_map
4653 * to only contain integer affine expressions, we compute the floor
4654 * of the expression before returning.
4656 * Remove all constraints involving local variables without
4657 * an explicit representation (resulting in the removal of those
4658 * local variables) prior to the actual extraction to ensure
4659 * that the local spaces in which the resulting affine expressions
4660 * are created do not contain any unknown local variables.
4661 * Removing such constraints is safe because constraints involving
4662 * unknown local variables are not used to determine whether
4663 * a basic map is obviously single-valued.
4665 static __isl_give isl_pw_multi_aff
*plain_pw_multi_aff_from_map(
4666 __isl_take isl_set
*domain
, __isl_take isl_basic_map
*bmap
)
4670 bmap
= isl_basic_map_drop_constraint_involving_unknown_divs(bmap
);
4671 ma
= extract_isl_multi_aff_from_basic_map(bmap
);
4672 ma
= isl_multi_aff_floor(ma
);
4673 return isl_pw_multi_aff_alloc(domain
, ma
);
4676 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4677 * This obviously only works if the input "map" is single-valued.
4678 * If so, we compute the lexicographic minimum of the image in the form
4679 * of an isl_pw_multi_aff. Since the image is unique, it is equal
4680 * to its lexicographic minimum.
4681 * If the input is not single-valued, we produce an error.
4683 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_base(
4684 __isl_take isl_map
*map
)
4688 isl_pw_multi_aff
*pma
;
4690 sv
= isl_map_is_single_valued(map
);
4694 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
4695 "map is not single-valued", goto error
);
4696 map
= isl_map_make_disjoint(map
);
4700 pma
= isl_pw_multi_aff_empty(isl_map_get_space(map
));
4702 for (i
= 0; i
< map
->n
; ++i
) {
4703 isl_pw_multi_aff
*pma_i
;
4704 isl_basic_map
*bmap
;
4705 bmap
= isl_basic_map_copy(map
->p
[i
]);
4706 pma_i
= isl_basic_map_lexmin_pw_multi_aff(bmap
);
4707 pma
= isl_pw_multi_aff_add_disjoint(pma
, pma_i
);
4717 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
4718 * taking into account that the output dimension at position "d"
4719 * can be represented as
4721 * x = floor((e(...) + c1) / m)
4723 * given that constraint "i" is of the form
4725 * e(...) + c1 - m x >= 0
4728 * Let "map" be of the form
4732 * We construct a mapping
4734 * A -> [A -> x = floor(...)]
4736 * apply that to the map, obtaining
4738 * [A -> x = floor(...)] -> B
4740 * and equate dimension "d" to x.
4741 * We then compute a isl_pw_multi_aff representation of the resulting map
4742 * and plug in the mapping above.
4744 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_div(
4745 __isl_take isl_map
*map
, __isl_take isl_basic_map
*hull
, int d
, int i
)
4748 isl_space
*space
= NULL
;
4749 isl_local_space
*ls
;
4757 isl_pw_multi_aff
*pma
;
4760 is_set
= isl_map_is_set(map
);
4764 offset
= isl_basic_map_offset(hull
, isl_dim_out
);
4765 ctx
= isl_map_get_ctx(map
);
4766 space
= isl_space_domain(isl_map_get_space(map
));
4767 n_in
= isl_space_dim(space
, isl_dim_set
);
4768 n
= isl_space_dim(space
, isl_dim_all
);
4769 if (n_in
< 0 || n
< 0)
4772 v
= isl_vec_alloc(ctx
, 1 + 1 + n
);
4774 isl_int_neg(v
->el
[0], hull
->ineq
[i
][offset
+ d
]);
4775 isl_seq_cpy(v
->el
+ 1, hull
->ineq
[i
], 1 + n
);
4777 isl_basic_map_free(hull
);
4779 ls
= isl_local_space_from_space(isl_space_copy(space
));
4780 aff
= isl_aff_alloc_vec(ls
, v
);
4781 aff
= isl_aff_floor(aff
);
4783 isl_space_free(space
);
4784 ma
= isl_multi_aff_from_aff(aff
);
4786 ma
= isl_multi_aff_identity(isl_space_map_from_set(space
));
4787 ma
= isl_multi_aff_range_product(ma
,
4788 isl_multi_aff_from_aff(aff
));
4791 insert
= isl_map_from_multi_aff_internal(isl_multi_aff_copy(ma
));
4792 map
= isl_map_apply_domain(map
, insert
);
4793 map
= isl_map_equate(map
, isl_dim_in
, n_in
, isl_dim_out
, d
);
4794 pma
= isl_pw_multi_aff_from_map(map
);
4795 pma
= isl_pw_multi_aff_pullback_multi_aff(pma
, ma
);
4799 isl_space_free(space
);
4801 isl_basic_map_free(hull
);
4805 /* Is constraint "c" of the form
4807 * e(...) + c1 - m x >= 0
4811 * -e(...) + c2 + m x >= 0
4813 * where m > 1 and e only depends on parameters and input dimemnsions?
4815 * "offset" is the offset of the output dimensions
4816 * "pos" is the position of output dimension x.
4818 static int is_potential_div_constraint(isl_int
*c
, int offset
, int d
, int total
)
4820 if (isl_int_is_zero(c
[offset
+ d
]))
4822 if (isl_int_is_one(c
[offset
+ d
]))
4824 if (isl_int_is_negone(c
[offset
+ d
]))
4826 if (isl_seq_first_non_zero(c
+ offset
, d
) != -1)
4828 if (isl_seq_first_non_zero(c
+ offset
+ d
+ 1,
4829 total
- (offset
+ d
+ 1)) != -1)
4834 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4836 * As a special case, we first check if there is any pair of constraints,
4837 * shared by all the basic maps in "map" that force a given dimension
4838 * to be equal to the floor of some affine combination of the input dimensions.
4840 * In particular, if we can find two constraints
4842 * e(...) + c1 - m x >= 0 i.e., m x <= e(...) + c1
4846 * -e(...) + c2 + m x >= 0 i.e., m x >= e(...) - c2
4848 * where m > 1 and e only depends on parameters and input dimemnsions,
4851 * c1 + c2 < m i.e., -c2 >= c1 - (m - 1)
4853 * then we know that we can take
4855 * x = floor((e(...) + c1) / m)
4857 * without having to perform any computation.
4859 * Note that we know that
4863 * If c1 + c2 were 0, then we would have detected an equality during
4864 * simplification. If c1 + c2 were negative, then we would have detected
4867 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_check_div(
4868 __isl_take isl_map
*map
)
4876 isl_basic_map
*hull
;
4878 hull
= isl_map_unshifted_simple_hull(isl_map_copy(map
));
4879 dim
= isl_map_dim(map
, isl_dim_out
);
4880 total
= isl_basic_map_dim(hull
, isl_dim_all
);
4881 if (dim
< 0 || total
< 0)
4885 offset
= isl_basic_map_offset(hull
, isl_dim_out
);
4887 for (d
= 0; d
< dim
; ++d
) {
4888 for (i
= 0; i
< n
; ++i
) {
4889 if (!is_potential_div_constraint(hull
->ineq
[i
],
4890 offset
, d
, 1 + total
))
4892 for (j
= i
+ 1; j
< n
; ++j
) {
4893 if (!isl_seq_is_neg(hull
->ineq
[i
] + 1,
4894 hull
->ineq
[j
] + 1, total
))
4896 isl_int_add(sum
, hull
->ineq
[i
][0],
4898 if (isl_int_abs_lt(sum
,
4899 hull
->ineq
[i
][offset
+ d
]))
4906 if (isl_int_is_pos(hull
->ineq
[j
][offset
+ d
]))
4908 return pw_multi_aff_from_map_div(map
, hull
, d
, j
);
4912 isl_basic_map_free(hull
);
4913 return pw_multi_aff_from_map_base(map
);
4916 isl_basic_map_free(hull
);
4920 /* Given an affine expression
4922 * [A -> B] -> f(A,B)
4924 * construct an isl_multi_aff
4928 * such that dimension "d" in B' is set to "aff" and the remaining
4929 * dimensions are set equal to the corresponding dimensions in B.
4930 * "n_in" is the dimension of the space A.
4931 * "n_out" is the dimension of the space B.
4933 * If "is_set" is set, then the affine expression is of the form
4937 * and we construct an isl_multi_aff
4941 static __isl_give isl_multi_aff
*range_map(__isl_take isl_aff
*aff
, int d
,
4942 unsigned n_in
, unsigned n_out
, int is_set
)
4946 isl_space
*space
, *space2
;
4947 isl_local_space
*ls
;
4949 space
= isl_aff_get_domain_space(aff
);
4950 ls
= isl_local_space_from_space(isl_space_copy(space
));
4951 space2
= isl_space_copy(space
);
4953 space2
= isl_space_range(isl_space_unwrap(space2
));
4954 space
= isl_space_map_from_domain_and_range(space
, space2
);
4955 ma
= isl_multi_aff_alloc(space
);
4956 ma
= isl_multi_aff_set_aff(ma
, d
, aff
);
4958 for (i
= 0; i
< n_out
; ++i
) {
4961 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
4962 isl_dim_set
, n_in
+ i
);
4963 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
4966 isl_local_space_free(ls
);
4971 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
4972 * taking into account that the dimension at position "d" can be written as
4974 * x = m a + f(..) (1)
4976 * where m is equal to "gcd".
4977 * "i" is the index of the equality in "hull" that defines f(..).
4978 * In particular, the equality is of the form
4980 * f(..) - x + m g(existentials) = 0
4984 * -f(..) + x + m g(existentials) = 0
4986 * We basically plug (1) into "map", resulting in a map with "a"
4987 * in the range instead of "x". The corresponding isl_pw_multi_aff
4988 * defining "a" is then plugged back into (1) to obtain a definition for "x".
4990 * Specifically, given the input map
4994 * We first wrap it into a set
4998 * and define (1) on top of the corresponding space, resulting in "aff".
4999 * We use this to create an isl_multi_aff that maps the output position "d"
5000 * from "a" to "x", leaving all other (intput and output) dimensions unchanged.
5001 * We plug this into the wrapped map, unwrap the result and compute the
5002 * corresponding isl_pw_multi_aff.
5003 * The result is an expression
5011 * so that we can plug that into "aff", after extending the latter to
5017 * If "map" is actually a set, then there is no "A" space, meaning
5018 * that we do not need to perform any wrapping, and that the result
5019 * of the recursive call is of the form
5023 * which is plugged into a mapping of the form
5027 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_stride(
5028 __isl_take isl_map
*map
, __isl_take isl_basic_map
*hull
, int d
, int i
,
5033 isl_local_space
*ls
;
5036 isl_pw_multi_aff
*pma
, *id
;
5042 is_set
= isl_map_is_set(map
);
5046 n_in
= isl_basic_map_dim(hull
, isl_dim_in
);
5047 n_out
= isl_basic_map_dim(hull
, isl_dim_out
);
5048 if (n_in
< 0 || n_out
< 0)
5050 o_out
= isl_basic_map_offset(hull
, isl_dim_out
);
5055 set
= isl_map_wrap(map
);
5056 space
= isl_space_map_from_set(isl_set_get_space(set
));
5057 ma
= isl_multi_aff_identity(space
);
5058 ls
= isl_local_space_from_space(isl_set_get_space(set
));
5059 aff
= isl_aff_alloc(ls
);
5061 isl_int_set_si(aff
->v
->el
[0], 1);
5062 if (isl_int_is_one(hull
->eq
[i
][o_out
+ d
]))
5063 isl_seq_neg(aff
->v
->el
+ 1, hull
->eq
[i
],
5066 isl_seq_cpy(aff
->v
->el
+ 1, hull
->eq
[i
],
5068 isl_int_set(aff
->v
->el
[1 + o_out
+ d
], gcd
);
5070 ma
= isl_multi_aff_set_aff(ma
, n_in
+ d
, isl_aff_copy(aff
));
5071 set
= isl_set_preimage_multi_aff(set
, ma
);
5073 ma
= range_map(aff
, d
, n_in
, n_out
, is_set
);
5078 map
= isl_set_unwrap(set
);
5079 pma
= isl_pw_multi_aff_from_map(map
);
5082 space
= isl_pw_multi_aff_get_domain_space(pma
);
5083 space
= isl_space_map_from_set(space
);
5084 id
= isl_pw_multi_aff_identity(space
);
5085 pma
= isl_pw_multi_aff_range_product(id
, pma
);
5087 id
= isl_pw_multi_aff_from_multi_aff(ma
);
5088 pma
= isl_pw_multi_aff_pullback_pw_multi_aff(id
, pma
);
5090 isl_basic_map_free(hull
);
5094 isl_basic_map_free(hull
);
5098 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5099 * "hull" contains the equalities valid for "map".
5101 * Check if any of the output dimensions is "strided".
5102 * That is, we check if it can be written as
5106 * with m greater than 1, a some combination of existentially quantified
5107 * variables and f an expression in the parameters and input dimensions.
5108 * If so, we remove the stride in pw_multi_aff_from_map_stride.
5110 * Otherwise, we continue with pw_multi_aff_from_map_check_div for a further
5113 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_check_strides(
5114 __isl_take isl_map
*map
, __isl_take isl_basic_map
*hull
)
5123 n_div
= isl_basic_map_dim(hull
, isl_dim_div
);
5124 n_out
= isl_basic_map_dim(hull
, isl_dim_out
);
5125 if (n_div
< 0 || n_out
< 0)
5129 isl_basic_map_free(hull
);
5130 return pw_multi_aff_from_map_check_div(map
);
5135 o_div
= isl_basic_map_offset(hull
, isl_dim_div
);
5136 o_out
= isl_basic_map_offset(hull
, isl_dim_out
);
5138 for (i
= 0; i
< n_out
; ++i
) {
5139 for (j
= 0; j
< hull
->n_eq
; ++j
) {
5140 isl_int
*eq
= hull
->eq
[j
];
5141 isl_pw_multi_aff
*res
;
5143 if (!isl_int_is_one(eq
[o_out
+ i
]) &&
5144 !isl_int_is_negone(eq
[o_out
+ i
]))
5146 if (isl_seq_first_non_zero(eq
+ o_out
, i
) != -1)
5148 if (isl_seq_first_non_zero(eq
+ o_out
+ i
+ 1,
5149 n_out
- (i
+ 1)) != -1)
5151 isl_seq_gcd(eq
+ o_div
, n_div
, &gcd
);
5152 if (isl_int_is_zero(gcd
))
5154 if (isl_int_is_one(gcd
))
5157 res
= pw_multi_aff_from_map_stride(map
, hull
,
5165 isl_basic_map_free(hull
);
5166 return pw_multi_aff_from_map_check_div(map
);
5169 isl_basic_map_free(hull
);
5173 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5175 * As a special case, we first check if all output dimensions are uniquely
5176 * defined in terms of the parameters and input dimensions over the entire
5177 * domain. If so, we extract the desired isl_pw_multi_aff directly
5178 * from the affine hull of "map" and its domain.
5180 * Otherwise, continue with pw_multi_aff_from_map_check_strides for more
5183 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_map(__isl_take isl_map
*map
)
5187 isl_basic_map
*hull
;
5189 n
= isl_map_n_basic_map(map
);
5194 hull
= isl_map_unshifted_simple_hull(isl_map_copy(map
));
5195 hull
= isl_basic_map_plain_affine_hull(hull
);
5196 sv
= isl_basic_map_plain_is_single_valued(hull
);
5198 return plain_pw_multi_aff_from_map(isl_map_domain(map
),
5200 isl_basic_map_free(hull
);
5202 map
= isl_map_detect_equalities(map
);
5203 hull
= isl_map_unshifted_simple_hull(isl_map_copy(map
));
5204 sv
= isl_basic_map_plain_is_single_valued(hull
);
5206 return plain_pw_multi_aff_from_map(isl_map_domain(map
), hull
);
5208 return pw_multi_aff_from_map_check_strides(map
, hull
);
5209 isl_basic_map_free(hull
);
5215 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_set(__isl_take isl_set
*set
)
5217 return isl_pw_multi_aff_from_map(set
);
5220 /* Convert "map" into an isl_pw_multi_aff (if possible) and
5223 static isl_stat
pw_multi_aff_from_map(__isl_take isl_map
*map
, void *user
)
5225 isl_union_pw_multi_aff
**upma
= user
;
5226 isl_pw_multi_aff
*pma
;
5228 pma
= isl_pw_multi_aff_from_map(map
);
5229 *upma
= isl_union_pw_multi_aff_add_pw_multi_aff(*upma
, pma
);
5231 return *upma
? isl_stat_ok
: isl_stat_error
;
5234 /* Create an isl_union_pw_multi_aff with the given isl_aff on a universe
5237 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_aff(
5238 __isl_take isl_aff
*aff
)
5241 isl_pw_multi_aff
*pma
;
5243 ma
= isl_multi_aff_from_aff(aff
);
5244 pma
= isl_pw_multi_aff_from_multi_aff(ma
);
5245 return isl_union_pw_multi_aff_from_pw_multi_aff(pma
);
5248 /* Try and create an isl_union_pw_multi_aff that is equivalent
5249 * to the given isl_union_map.
5250 * The isl_union_map is required to be single-valued in each space.
5251 * Otherwise, an error is produced.
5253 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_union_map(
5254 __isl_take isl_union_map
*umap
)
5257 isl_union_pw_multi_aff
*upma
;
5259 space
= isl_union_map_get_space(umap
);
5260 upma
= isl_union_pw_multi_aff_empty(space
);
5261 if (isl_union_map_foreach_map(umap
, &pw_multi_aff_from_map
, &upma
) < 0)
5262 upma
= isl_union_pw_multi_aff_free(upma
);
5263 isl_union_map_free(umap
);
5268 /* Try and create an isl_union_pw_multi_aff that is equivalent
5269 * to the given isl_union_set.
5270 * The isl_union_set is required to be a singleton in each space.
5271 * Otherwise, an error is produced.
5273 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_union_set(
5274 __isl_take isl_union_set
*uset
)
5276 return isl_union_pw_multi_aff_from_union_map(uset
);
5279 /* Return the piecewise affine expression "set ? 1 : 0".
5281 __isl_give isl_pw_aff
*isl_set_indicator_function(__isl_take isl_set
*set
)
5284 isl_space
*space
= isl_set_get_space(set
);
5285 isl_local_space
*ls
= isl_local_space_from_space(space
);
5286 isl_aff
*zero
= isl_aff_zero_on_domain(isl_local_space_copy(ls
));
5287 isl_aff
*one
= isl_aff_zero_on_domain(ls
);
5289 one
= isl_aff_add_constant_si(one
, 1);
5290 pa
= isl_pw_aff_alloc(isl_set_copy(set
), one
);
5291 set
= isl_set_complement(set
);
5292 pa
= isl_pw_aff_add_disjoint(pa
, isl_pw_aff_alloc(set
, zero
));
5297 /* Plug in "subs" for dimension "type", "pos" of "aff".
5299 * Let i be the dimension to replace and let "subs" be of the form
5303 * and "aff" of the form
5309 * (a f + d g')/(m d)
5311 * where g' is the result of plugging in "subs" in each of the integer
5314 __isl_give isl_aff
*isl_aff_substitute(__isl_take isl_aff
*aff
,
5315 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
5321 aff
= isl_aff_cow(aff
);
5323 return isl_aff_free(aff
);
5325 ctx
= isl_aff_get_ctx(aff
);
5326 if (!isl_space_is_equal(aff
->ls
->dim
, subs
->ls
->dim
))
5327 isl_die(ctx
, isl_error_invalid
,
5328 "spaces don't match", return isl_aff_free(aff
));
5329 n_div
= isl_local_space_dim(subs
->ls
, isl_dim_div
);
5331 return isl_aff_free(aff
);
5333 isl_die(ctx
, isl_error_unsupported
,
5334 "cannot handle divs yet", return isl_aff_free(aff
));
5336 aff
->ls
= isl_local_space_substitute(aff
->ls
, type
, pos
, subs
);
5338 return isl_aff_free(aff
);
5340 aff
->v
= isl_vec_cow(aff
->v
);
5342 return isl_aff_free(aff
);
5344 pos
+= isl_local_space_offset(aff
->ls
, type
);
5347 isl_seq_substitute(aff
->v
->el
, pos
, subs
->v
->el
,
5348 aff
->v
->size
, subs
->v
->size
, v
);
5354 /* Plug in "subs" for dimension "type", "pos" in each of the affine
5355 * expressions in "maff".
5357 __isl_give isl_multi_aff
*isl_multi_aff_substitute(
5358 __isl_take isl_multi_aff
*maff
, enum isl_dim_type type
, unsigned pos
,
5359 __isl_keep isl_aff
*subs
)
5363 maff
= isl_multi_aff_cow(maff
);
5365 return isl_multi_aff_free(maff
);
5367 if (type
== isl_dim_in
)
5370 for (i
= 0; i
< maff
->n
; ++i
) {
5371 maff
->u
.p
[i
] = isl_aff_substitute(maff
->u
.p
[i
],
5374 return isl_multi_aff_free(maff
);
5380 /* Plug in "subs" for dimension "type", "pos" of "pma".
5382 * pma is of the form
5386 * while subs is of the form
5388 * v' = B_j(v) -> S_j
5390 * Each pair i,j such that C_ij = A_i \cap B_i is non-empty
5391 * has a contribution in the result, in particular
5393 * C_ij(S_j) -> M_i(S_j)
5395 * Note that plugging in S_j in C_ij may also result in an empty set
5396 * and this contribution should simply be discarded.
5398 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_substitute(
5399 __isl_take isl_pw_multi_aff
*pma
, enum isl_dim_type type
, unsigned pos
,
5400 __isl_keep isl_pw_aff
*subs
)
5403 isl_pw_multi_aff
*res
;
5406 return isl_pw_multi_aff_free(pma
);
5408 n
= pma
->n
* subs
->n
;
5409 res
= isl_pw_multi_aff_alloc_size(isl_space_copy(pma
->dim
), n
);
5411 for (i
= 0; i
< pma
->n
; ++i
) {
5412 for (j
= 0; j
< subs
->n
; ++j
) {
5414 isl_multi_aff
*res_ij
;
5417 common
= isl_set_intersect(
5418 isl_set_copy(pma
->p
[i
].set
),
5419 isl_set_copy(subs
->p
[j
].set
));
5420 common
= isl_set_substitute(common
,
5421 type
, pos
, subs
->p
[j
].aff
);
5422 empty
= isl_set_plain_is_empty(common
);
5423 if (empty
< 0 || empty
) {
5424 isl_set_free(common
);
5430 res_ij
= isl_multi_aff_substitute(
5431 isl_multi_aff_copy(pma
->p
[i
].maff
),
5432 type
, pos
, subs
->p
[j
].aff
);
5434 res
= isl_pw_multi_aff_add_piece(res
, common
, res_ij
);
5438 isl_pw_multi_aff_free(pma
);
5441 isl_pw_multi_aff_free(pma
);
5442 isl_pw_multi_aff_free(res
);
5446 /* Compute the preimage of a range of dimensions in the affine expression "src"
5447 * under "ma" and put the result in "dst". The number of dimensions in "src"
5448 * that precede the range is given by "n_before". The number of dimensions
5449 * in the range is given by the number of output dimensions of "ma".
5450 * The number of dimensions that follow the range is given by "n_after".
5451 * If "has_denom" is set (to one),
5452 * then "src" and "dst" have an extra initial denominator.
5453 * "n_div_ma" is the number of existentials in "ma"
5454 * "n_div_bset" is the number of existentials in "src"
5455 * The resulting "dst" (which is assumed to have been allocated by
5456 * the caller) contains coefficients for both sets of existentials,
5457 * first those in "ma" and then those in "src".
5458 * f, c1, c2 and g are temporary objects that have been initialized
5461 * Let src represent the expression
5463 * (a(p) + f_u u + b v + f_w w + c(divs))/d
5465 * and let ma represent the expressions
5467 * v_i = (r_i(p) + s_i(y) + t_i(divs'))/m_i
5469 * We start out with the following expression for dst:
5471 * (a(p) + f_u u + 0 y + f_w w + 0 divs' + c(divs) + f \sum_i b_i v_i)/d
5473 * with the multiplication factor f initially equal to 1
5474 * and f \sum_i b_i v_i kept separately.
5475 * For each x_i that we substitute, we multiply the numerator
5476 * (and denominator) of dst by c_1 = m_i and add the numerator
5477 * of the x_i expression multiplied by c_2 = f b_i,
5478 * after removing the common factors of c_1 and c_2.
5479 * The multiplication factor f also needs to be multiplied by c_1
5480 * for the next x_j, j > i.
5482 isl_stat
isl_seq_preimage(isl_int
*dst
, isl_int
*src
,
5483 __isl_keep isl_multi_aff
*ma
, int n_before
, int n_after
,
5484 int n_div_ma
, int n_div_bmap
,
5485 isl_int f
, isl_int c1
, isl_int c2
, isl_int g
, int has_denom
)
5488 isl_size n_param
, n_in
, n_out
;
5491 n_param
= isl_multi_aff_dim(ma
, isl_dim_param
);
5492 n_in
= isl_multi_aff_dim(ma
, isl_dim_in
);
5493 n_out
= isl_multi_aff_dim(ma
, isl_dim_out
);
5494 if (n_param
< 0 || n_in
< 0 || n_out
< 0)
5495 return isl_stat_error
;
5497 isl_seq_cpy(dst
, src
, has_denom
+ 1 + n_param
+ n_before
);
5498 o_dst
= o_src
= has_denom
+ 1 + n_param
+ n_before
;
5499 isl_seq_clr(dst
+ o_dst
, n_in
);
5502 isl_seq_cpy(dst
+ o_dst
, src
+ o_src
, n_after
);
5505 isl_seq_clr(dst
+ o_dst
, n_div_ma
);
5507 isl_seq_cpy(dst
+ o_dst
, src
+ o_src
, n_div_bmap
);
5509 isl_int_set_si(f
, 1);
5511 for (i
= 0; i
< n_out
; ++i
) {
5512 int offset
= has_denom
+ 1 + n_param
+ n_before
+ i
;
5514 if (isl_int_is_zero(src
[offset
]))
5516 isl_int_set(c1
, ma
->u
.p
[i
]->v
->el
[0]);
5517 isl_int_mul(c2
, f
, src
[offset
]);
5518 isl_int_gcd(g
, c1
, c2
);
5519 isl_int_divexact(c1
, c1
, g
);
5520 isl_int_divexact(c2
, c2
, g
);
5522 isl_int_mul(f
, f
, c1
);
5525 isl_seq_combine(dst
+ o_dst
, c1
, dst
+ o_dst
,
5526 c2
, ma
->u
.p
[i
]->v
->el
+ o_src
, 1 + n_param
);
5527 o_dst
+= 1 + n_param
;
5528 o_src
+= 1 + n_param
;
5529 isl_seq_scale(dst
+ o_dst
, dst
+ o_dst
, c1
, n_before
);
5531 isl_seq_combine(dst
+ o_dst
, c1
, dst
+ o_dst
,
5532 c2
, ma
->u
.p
[i
]->v
->el
+ o_src
, n_in
);
5535 isl_seq_scale(dst
+ o_dst
, dst
+ o_dst
, c1
, n_after
);
5537 isl_seq_combine(dst
+ o_dst
, c1
, dst
+ o_dst
,
5538 c2
, ma
->u
.p
[i
]->v
->el
+ o_src
, n_div_ma
);
5541 isl_seq_scale(dst
+ o_dst
, dst
+ o_dst
, c1
, n_div_bmap
);
5543 isl_int_mul(dst
[0], dst
[0], c1
);
5549 /* Compute the pullback of "aff" by the function represented by "ma".
5550 * In other words, plug in "ma" in "aff". The result is an affine expression
5551 * defined over the domain space of "ma".
5553 * If "aff" is represented by
5555 * (a(p) + b x + c(divs))/d
5557 * and ma is represented by
5559 * x = D(p) + F(y) + G(divs')
5561 * then the result is
5563 * (a(p) + b D(p) + b F(y) + b G(divs') + c(divs))/d
5565 * The divs in the local space of the input are similarly adjusted
5566 * through a call to isl_local_space_preimage_multi_aff.
5568 __isl_give isl_aff
*isl_aff_pullback_multi_aff(__isl_take isl_aff
*aff
,
5569 __isl_take isl_multi_aff
*ma
)
5571 isl_aff
*res
= NULL
;
5572 isl_local_space
*ls
;
5573 isl_size n_div_aff
, n_div_ma
;
5574 isl_int f
, c1
, c2
, g
;
5576 ma
= isl_multi_aff_align_divs(ma
);
5580 n_div_aff
= isl_aff_dim(aff
, isl_dim_div
);
5581 n_div_ma
= ma
->n
? isl_aff_dim(ma
->u
.p
[0], isl_dim_div
) : 0;
5582 if (n_div_aff
< 0 || n_div_ma
< 0)
5585 ls
= isl_aff_get_domain_local_space(aff
);
5586 ls
= isl_local_space_preimage_multi_aff(ls
, isl_multi_aff_copy(ma
));
5587 res
= isl_aff_alloc(ls
);
5596 if (isl_seq_preimage(res
->v
->el
, aff
->v
->el
, ma
, 0, 0,
5597 n_div_ma
, n_div_aff
, f
, c1
, c2
, g
, 1) < 0)
5598 res
= isl_aff_free(res
);
5606 isl_multi_aff_free(ma
);
5607 res
= isl_aff_normalize(res
);
5611 isl_multi_aff_free(ma
);
5616 /* Compute the pullback of "aff1" by the function represented by "aff2".
5617 * In other words, plug in "aff2" in "aff1". The result is an affine expression
5618 * defined over the domain space of "aff1".
5620 * The domain of "aff1" should match the range of "aff2", which means
5621 * that it should be single-dimensional.
5623 __isl_give isl_aff
*isl_aff_pullback_aff(__isl_take isl_aff
*aff1
,
5624 __isl_take isl_aff
*aff2
)
5628 ma
= isl_multi_aff_from_aff(aff2
);
5629 return isl_aff_pullback_multi_aff(aff1
, ma
);
5632 /* Compute the pullback of "ma1" by the function represented by "ma2".
5633 * In other words, plug in "ma2" in "ma1".
5635 * The parameters of "ma1" and "ma2" are assumed to have been aligned.
5637 static __isl_give isl_multi_aff
*isl_multi_aff_pullback_multi_aff_aligned(
5638 __isl_take isl_multi_aff
*ma1
, __isl_take isl_multi_aff
*ma2
)
5641 isl_space
*space
= NULL
;
5643 ma2
= isl_multi_aff_align_divs(ma2
);
5644 ma1
= isl_multi_aff_cow(ma1
);
5648 space
= isl_space_join(isl_multi_aff_get_space(ma2
),
5649 isl_multi_aff_get_space(ma1
));
5651 for (i
= 0; i
< ma1
->n
; ++i
) {
5652 ma1
->u
.p
[i
] = isl_aff_pullback_multi_aff(ma1
->u
.p
[i
],
5653 isl_multi_aff_copy(ma2
));
5658 ma1
= isl_multi_aff_reset_space(ma1
, space
);
5659 isl_multi_aff_free(ma2
);
5662 isl_space_free(space
);
5663 isl_multi_aff_free(ma2
);
5664 isl_multi_aff_free(ma1
);
5668 /* Compute the pullback of "ma1" by the function represented by "ma2".
5669 * In other words, plug in "ma2" in "ma1".
5671 __isl_give isl_multi_aff
*isl_multi_aff_pullback_multi_aff(
5672 __isl_take isl_multi_aff
*ma1
, __isl_take isl_multi_aff
*ma2
)
5674 return isl_multi_aff_align_params_multi_multi_and(ma1
, ma2
,
5675 &isl_multi_aff_pullback_multi_aff_aligned
);
5678 /* Extend the local space of "dst" to include the divs
5679 * in the local space of "src".
5681 * If "src" does not have any divs or if the local spaces of "dst" and
5682 * "src" are the same, then no extension is required.
5684 __isl_give isl_aff
*isl_aff_align_divs(__isl_take isl_aff
*dst
,
5685 __isl_keep isl_aff
*src
)
5688 isl_size src_n_div
, dst_n_div
;
5695 return isl_aff_free(dst
);
5697 ctx
= isl_aff_get_ctx(src
);
5698 equal
= isl_local_space_has_equal_space(src
->ls
, dst
->ls
);
5700 return isl_aff_free(dst
);
5702 isl_die(ctx
, isl_error_invalid
,
5703 "spaces don't match", goto error
);
5705 src_n_div
= isl_local_space_dim(src
->ls
, isl_dim_div
);
5706 dst_n_div
= isl_local_space_dim(dst
->ls
, isl_dim_div
);
5709 equal
= isl_local_space_is_equal(src
->ls
, dst
->ls
);
5710 if (equal
< 0 || src_n_div
< 0 || dst_n_div
< 0)
5711 return isl_aff_free(dst
);
5715 exp1
= isl_alloc_array(ctx
, int, src_n_div
);
5716 exp2
= isl_alloc_array(ctx
, int, dst_n_div
);
5717 if (!exp1
|| (dst_n_div
&& !exp2
))
5720 div
= isl_merge_divs(src
->ls
->div
, dst
->ls
->div
, exp1
, exp2
);
5721 dst
= isl_aff_expand_divs(dst
, div
, exp2
);
5729 return isl_aff_free(dst
);
5732 /* Adjust the local spaces of the affine expressions in "maff"
5733 * such that they all have the save divs.
5735 __isl_give isl_multi_aff
*isl_multi_aff_align_divs(
5736 __isl_take isl_multi_aff
*maff
)
5744 maff
= isl_multi_aff_cow(maff
);
5748 for (i
= 1; i
< maff
->n
; ++i
)
5749 maff
->u
.p
[0] = isl_aff_align_divs(maff
->u
.p
[0], maff
->u
.p
[i
]);
5750 for (i
= 1; i
< maff
->n
; ++i
) {
5751 maff
->u
.p
[i
] = isl_aff_align_divs(maff
->u
.p
[i
], maff
->u
.p
[0]);
5753 return isl_multi_aff_free(maff
);
5759 __isl_give isl_aff
*isl_aff_lift(__isl_take isl_aff
*aff
)
5761 aff
= isl_aff_cow(aff
);
5765 aff
->ls
= isl_local_space_lift(aff
->ls
);
5767 return isl_aff_free(aff
);
5772 /* Lift "maff" to a space with extra dimensions such that the result
5773 * has no more existentially quantified variables.
5774 * If "ls" is not NULL, then *ls is assigned the local space that lies
5775 * at the basis of the lifting applied to "maff".
5777 __isl_give isl_multi_aff
*isl_multi_aff_lift(__isl_take isl_multi_aff
*maff
,
5778 __isl_give isl_local_space
**ls
)
5792 isl_space
*space
= isl_multi_aff_get_domain_space(maff
);
5793 *ls
= isl_local_space_from_space(space
);
5795 return isl_multi_aff_free(maff
);
5800 maff
= isl_multi_aff_cow(maff
);
5801 maff
= isl_multi_aff_align_divs(maff
);
5805 n_div
= isl_aff_dim(maff
->u
.p
[0], isl_dim_div
);
5807 return isl_multi_aff_free(maff
);
5808 space
= isl_multi_aff_get_space(maff
);
5809 space
= isl_space_lift(isl_space_domain(space
), n_div
);
5810 space
= isl_space_extend_domain_with_range(space
,
5811 isl_multi_aff_get_space(maff
));
5813 return isl_multi_aff_free(maff
);
5814 isl_space_free(maff
->space
);
5815 maff
->space
= space
;
5818 *ls
= isl_aff_get_domain_local_space(maff
->u
.p
[0]);
5820 return isl_multi_aff_free(maff
);
5823 for (i
= 0; i
< maff
->n
; ++i
) {
5824 maff
->u
.p
[i
] = isl_aff_lift(maff
->u
.p
[i
]);
5832 isl_local_space_free(*ls
);
5833 return isl_multi_aff_free(maff
);
5837 #define TYPE isl_pw_multi_aff
5839 #include "check_type_range_templ.c"
5841 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma".
5843 __isl_give isl_pw_aff
*isl_pw_multi_aff_get_pw_aff(
5844 __isl_keep isl_pw_multi_aff
*pma
, int pos
)
5851 if (isl_pw_multi_aff_check_range(pma
, isl_dim_out
, pos
, 1) < 0)
5854 n_out
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
5858 space
= isl_pw_multi_aff_get_space(pma
);
5859 space
= isl_space_drop_dims(space
, isl_dim_out
,
5860 pos
+ 1, n_out
- pos
- 1);
5861 space
= isl_space_drop_dims(space
, isl_dim_out
, 0, pos
);
5863 pa
= isl_pw_aff_alloc_size(space
, pma
->n
);
5864 for (i
= 0; i
< pma
->n
; ++i
) {
5866 aff
= isl_multi_aff_get_aff(pma
->p
[i
].maff
, pos
);
5867 pa
= isl_pw_aff_add_piece(pa
, isl_set_copy(pma
->p
[i
].set
), aff
);
5873 /* Return an isl_pw_multi_aff with the given "set" as domain and
5874 * an unnamed zero-dimensional range.
5876 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_domain(
5877 __isl_take isl_set
*set
)
5882 space
= isl_set_get_space(set
);
5883 space
= isl_space_from_domain(space
);
5884 ma
= isl_multi_aff_zero(space
);
5885 return isl_pw_multi_aff_alloc(set
, ma
);
5888 /* Add an isl_pw_multi_aff with the given "set" as domain and
5889 * an unnamed zero-dimensional range to *user.
5891 static isl_stat
add_pw_multi_aff_from_domain(__isl_take isl_set
*set
,
5894 isl_union_pw_multi_aff
**upma
= user
;
5895 isl_pw_multi_aff
*pma
;
5897 pma
= isl_pw_multi_aff_from_domain(set
);
5898 *upma
= isl_union_pw_multi_aff_add_pw_multi_aff(*upma
, pma
);
5903 /* Return an isl_union_pw_multi_aff with the given "uset" as domain and
5904 * an unnamed zero-dimensional range.
5906 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_domain(
5907 __isl_take isl_union_set
*uset
)
5910 isl_union_pw_multi_aff
*upma
;
5915 space
= isl_union_set_get_space(uset
);
5916 upma
= isl_union_pw_multi_aff_empty(space
);
5918 if (isl_union_set_foreach_set(uset
,
5919 &add_pw_multi_aff_from_domain
, &upma
) < 0)
5922 isl_union_set_free(uset
);
5925 isl_union_set_free(uset
);
5926 isl_union_pw_multi_aff_free(upma
);
5930 /* Local data for bin_entry and the callback "fn".
5932 struct isl_union_pw_multi_aff_bin_data
{
5933 isl_union_pw_multi_aff
*upma2
;
5934 isl_union_pw_multi_aff
*res
;
5935 isl_pw_multi_aff
*pma
;
5936 isl_stat (*fn
)(__isl_take isl_pw_multi_aff
*pma
, void *user
);
5939 /* Given an isl_pw_multi_aff from upma1, store it in data->pma
5940 * and call data->fn for each isl_pw_multi_aff in data->upma2.
5942 static isl_stat
bin_entry(__isl_take isl_pw_multi_aff
*pma
, void *user
)
5944 struct isl_union_pw_multi_aff_bin_data
*data
= user
;
5948 r
= isl_union_pw_multi_aff_foreach_pw_multi_aff(data
->upma2
,
5950 isl_pw_multi_aff_free(pma
);
5955 /* Call "fn" on each pair of isl_pw_multi_affs in "upma1" and "upma2".
5956 * The isl_pw_multi_aff from upma1 is stored in data->pma (where data is
5957 * passed as user field) and the isl_pw_multi_aff from upma2 is available
5958 * as *entry. The callback should adjust data->res if desired.
5960 static __isl_give isl_union_pw_multi_aff
*bin_op(
5961 __isl_take isl_union_pw_multi_aff
*upma1
,
5962 __isl_take isl_union_pw_multi_aff
*upma2
,
5963 isl_stat (*fn
)(__isl_take isl_pw_multi_aff
*pma
, void *user
))
5966 struct isl_union_pw_multi_aff_bin_data data
= { NULL
, NULL
, NULL
, fn
};
5968 space
= isl_union_pw_multi_aff_get_space(upma2
);
5969 upma1
= isl_union_pw_multi_aff_align_params(upma1
, space
);
5970 space
= isl_union_pw_multi_aff_get_space(upma1
);
5971 upma2
= isl_union_pw_multi_aff_align_params(upma2
, space
);
5973 if (!upma1
|| !upma2
)
5977 data
.res
= isl_union_pw_multi_aff_alloc_same_size(upma1
);
5978 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma1
,
5979 &bin_entry
, &data
) < 0)
5982 isl_union_pw_multi_aff_free(upma1
);
5983 isl_union_pw_multi_aff_free(upma2
);
5986 isl_union_pw_multi_aff_free(upma1
);
5987 isl_union_pw_multi_aff_free(upma2
);
5988 isl_union_pw_multi_aff_free(data
.res
);
5992 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
5993 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
5995 static __isl_give isl_pw_multi_aff
*pw_multi_aff_range_product(
5996 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
6000 space
= isl_space_range_product(isl_pw_multi_aff_get_space(pma1
),
6001 isl_pw_multi_aff_get_space(pma2
));
6002 return isl_pw_multi_aff_on_shared_domain_in(pma1
, pma2
, space
,
6003 &isl_multi_aff_range_product
);
6006 /* Given two isl_pw_multi_affs A -> B and C -> D,
6007 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
6009 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_range_product(
6010 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
6012 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
6013 &pw_multi_aff_range_product
);
6016 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
6017 * construct an isl_pw_multi_aff (A * C) -> (B, D).
6019 static __isl_give isl_pw_multi_aff
*pw_multi_aff_flat_range_product(
6020 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
6024 space
= isl_space_range_product(isl_pw_multi_aff_get_space(pma1
),
6025 isl_pw_multi_aff_get_space(pma2
));
6026 space
= isl_space_flatten_range(space
);
6027 return isl_pw_multi_aff_on_shared_domain_in(pma1
, pma2
, space
,
6028 &isl_multi_aff_flat_range_product
);
6031 /* Given two isl_pw_multi_affs A -> B and C -> D,
6032 * construct an isl_pw_multi_aff (A * C) -> (B, D).
6034 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_flat_range_product(
6035 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
6037 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
6038 &pw_multi_aff_flat_range_product
);
6041 /* If data->pma and "pma2" have the same domain space, then compute
6042 * their flat range product and the result to data->res.
6044 static isl_stat
flat_range_product_entry(__isl_take isl_pw_multi_aff
*pma2
,
6047 struct isl_union_pw_multi_aff_bin_data
*data
= user
;
6049 if (!isl_space_tuple_is_equal(data
->pma
->dim
, isl_dim_in
,
6050 pma2
->dim
, isl_dim_in
)) {
6051 isl_pw_multi_aff_free(pma2
);
6055 pma2
= isl_pw_multi_aff_flat_range_product(
6056 isl_pw_multi_aff_copy(data
->pma
), pma2
);
6058 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma2
);
6063 /* Given two isl_union_pw_multi_affs A -> B and C -> D,
6064 * construct an isl_union_pw_multi_aff (A * C) -> (B, D).
6066 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_flat_range_product(
6067 __isl_take isl_union_pw_multi_aff
*upma1
,
6068 __isl_take isl_union_pw_multi_aff
*upma2
)
6070 return bin_op(upma1
, upma2
, &flat_range_product_entry
);
6073 /* Replace the affine expressions at position "pos" in "pma" by "pa".
6074 * The parameters are assumed to have been aligned.
6076 * The implementation essentially performs an isl_pw_*_on_shared_domain,
6077 * except that it works on two different isl_pw_* types.
6079 static __isl_give isl_pw_multi_aff
*pw_multi_aff_set_pw_aff(
6080 __isl_take isl_pw_multi_aff
*pma
, unsigned pos
,
6081 __isl_take isl_pw_aff
*pa
)
6084 isl_pw_multi_aff
*res
= NULL
;
6089 if (!isl_space_tuple_is_equal(pma
->dim
, isl_dim_in
,
6090 pa
->dim
, isl_dim_in
))
6091 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
6092 "domains don't match", goto error
);
6093 if (isl_pw_multi_aff_check_range(pma
, isl_dim_out
, pos
, 1) < 0)
6097 res
= isl_pw_multi_aff_alloc_size(isl_pw_multi_aff_get_space(pma
), n
);
6099 for (i
= 0; i
< pma
->n
; ++i
) {
6100 for (j
= 0; j
< pa
->n
; ++j
) {
6102 isl_multi_aff
*res_ij
;
6105 common
= isl_set_intersect(isl_set_copy(pma
->p
[i
].set
),
6106 isl_set_copy(pa
->p
[j
].set
));
6107 empty
= isl_set_plain_is_empty(common
);
6108 if (empty
< 0 || empty
) {
6109 isl_set_free(common
);
6115 res_ij
= isl_multi_aff_set_aff(
6116 isl_multi_aff_copy(pma
->p
[i
].maff
), pos
,
6117 isl_aff_copy(pa
->p
[j
].aff
));
6118 res_ij
= isl_multi_aff_gist(res_ij
,
6119 isl_set_copy(common
));
6121 res
= isl_pw_multi_aff_add_piece(res
, common
, res_ij
);
6125 isl_pw_multi_aff_free(pma
);
6126 isl_pw_aff_free(pa
);
6129 isl_pw_multi_aff_free(pma
);
6130 isl_pw_aff_free(pa
);
6131 return isl_pw_multi_aff_free(res
);
6134 /* Replace the affine expressions at position "pos" in "pma" by "pa".
6136 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_set_pw_aff(
6137 __isl_take isl_pw_multi_aff
*pma
, unsigned pos
,
6138 __isl_take isl_pw_aff
*pa
)
6140 isl_bool equal_params
;
6144 equal_params
= isl_space_has_equal_params(pma
->dim
, pa
->dim
);
6145 if (equal_params
< 0)
6148 return pw_multi_aff_set_pw_aff(pma
, pos
, pa
);
6149 if (isl_pw_multi_aff_check_named_params(pma
) < 0 ||
6150 isl_pw_aff_check_named_params(pa
) < 0)
6152 pma
= isl_pw_multi_aff_align_params(pma
, isl_pw_aff_get_space(pa
));
6153 pa
= isl_pw_aff_align_params(pa
, isl_pw_multi_aff_get_space(pma
));
6154 return pw_multi_aff_set_pw_aff(pma
, pos
, pa
);
6156 isl_pw_multi_aff_free(pma
);
6157 isl_pw_aff_free(pa
);
6161 /* Do the parameters of "pa" match those of "space"?
6163 isl_bool
isl_pw_aff_matching_params(__isl_keep isl_pw_aff
*pa
,
6164 __isl_keep isl_space
*space
)
6166 isl_space
*pa_space
;
6170 return isl_bool_error
;
6172 pa_space
= isl_pw_aff_get_space(pa
);
6174 match
= isl_space_has_equal_params(space
, pa_space
);
6176 isl_space_free(pa_space
);
6180 /* Check that the domain space of "pa" matches "space".
6182 isl_stat
isl_pw_aff_check_match_domain_space(__isl_keep isl_pw_aff
*pa
,
6183 __isl_keep isl_space
*space
)
6185 isl_space
*pa_space
;
6189 return isl_stat_error
;
6191 pa_space
= isl_pw_aff_get_space(pa
);
6193 match
= isl_space_has_equal_params(space
, pa_space
);
6197 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
6198 "parameters don't match", goto error
);
6199 match
= isl_space_tuple_is_equal(space
, isl_dim_in
,
6200 pa_space
, isl_dim_in
);
6204 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
6205 "domains don't match", goto error
);
6206 isl_space_free(pa_space
);
6209 isl_space_free(pa_space
);
6210 return isl_stat_error
;
6218 #include <isl_multi_explicit_domain.c>
6219 #include <isl_multi_pw_aff_explicit_domain.c>
6220 #include <isl_multi_templ.c>
6221 #include <isl_multi_apply_set.c>
6222 #include <isl_multi_coalesce.c>
6223 #include <isl_multi_domain_templ.c>
6224 #include <isl_multi_dims.c>
6225 #include <isl_multi_from_base_templ.c>
6226 #include <isl_multi_gist.c>
6227 #include <isl_multi_hash.c>
6228 #include <isl_multi_identity_templ.c>
6229 #include <isl_multi_align_set.c>
6230 #include <isl_multi_intersect.c>
6231 #include <isl_multi_move_dims_templ.c>
6232 #include <isl_multi_product_templ.c>
6233 #include <isl_multi_splice_templ.c>
6234 #include <isl_multi_zero_templ.c>
6236 /* Does "mpa" have a non-trivial explicit domain?
6238 * The explicit domain, if present, is trivial if it represents
6239 * an (obviously) universe set.
6241 isl_bool
isl_multi_pw_aff_has_non_trivial_domain(
6242 __isl_keep isl_multi_pw_aff
*mpa
)
6245 return isl_bool_error
;
6246 if (!isl_multi_pw_aff_has_explicit_domain(mpa
))
6247 return isl_bool_false
;
6248 return isl_bool_not(isl_set_plain_is_universe(mpa
->u
.dom
));
6251 /* Scale the elements of "pma" by the corresponding elements of "mv".
6253 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_scale_multi_val(
6254 __isl_take isl_pw_multi_aff
*pma
, __isl_take isl_multi_val
*mv
)
6257 isl_bool equal_params
;
6259 pma
= isl_pw_multi_aff_cow(pma
);
6262 if (!isl_space_tuple_is_equal(pma
->dim
, isl_dim_out
,
6263 mv
->space
, isl_dim_set
))
6264 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
6265 "spaces don't match", goto error
);
6266 equal_params
= isl_space_has_equal_params(pma
->dim
, mv
->space
);
6267 if (equal_params
< 0)
6269 if (!equal_params
) {
6270 pma
= isl_pw_multi_aff_align_params(pma
,
6271 isl_multi_val_get_space(mv
));
6272 mv
= isl_multi_val_align_params(mv
,
6273 isl_pw_multi_aff_get_space(pma
));
6278 for (i
= 0; i
< pma
->n
; ++i
) {
6279 pma
->p
[i
].maff
= isl_multi_aff_scale_multi_val(pma
->p
[i
].maff
,
6280 isl_multi_val_copy(mv
));
6281 if (!pma
->p
[i
].maff
)
6285 isl_multi_val_free(mv
);
6288 isl_multi_val_free(mv
);
6289 isl_pw_multi_aff_free(pma
);
6293 /* This function is called for each entry of an isl_union_pw_multi_aff.
6294 * If the space of the entry matches that of data->mv,
6295 * then apply isl_pw_multi_aff_scale_multi_val and return the result.
6296 * Otherwise, return an empty isl_pw_multi_aff.
6298 static __isl_give isl_pw_multi_aff
*union_pw_multi_aff_scale_multi_val_entry(
6299 __isl_take isl_pw_multi_aff
*pma
, void *user
)
6301 isl_multi_val
*mv
= user
;
6305 if (!isl_space_tuple_is_equal(pma
->dim
, isl_dim_out
,
6306 mv
->space
, isl_dim_set
)) {
6307 isl_space
*space
= isl_pw_multi_aff_get_space(pma
);
6308 isl_pw_multi_aff_free(pma
);
6309 return isl_pw_multi_aff_empty(space
);
6312 return isl_pw_multi_aff_scale_multi_val(pma
, isl_multi_val_copy(mv
));
6315 /* Scale the elements of "upma" by the corresponding elements of "mv",
6316 * for those entries that match the space of "mv".
6318 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_scale_multi_val(
6319 __isl_take isl_union_pw_multi_aff
*upma
, __isl_take isl_multi_val
*mv
)
6321 upma
= isl_union_pw_multi_aff_align_params(upma
,
6322 isl_multi_val_get_space(mv
));
6323 mv
= isl_multi_val_align_params(mv
,
6324 isl_union_pw_multi_aff_get_space(upma
));
6328 return isl_union_pw_multi_aff_transform(upma
,
6329 &union_pw_multi_aff_scale_multi_val_entry
, mv
);
6331 isl_multi_val_free(mv
);
6334 isl_multi_val_free(mv
);
6335 isl_union_pw_multi_aff_free(upma
);
6339 /* Construct and return a piecewise multi affine expression
6340 * in the given space with value zero in each of the output dimensions and
6341 * a universe domain.
6343 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_zero(__isl_take isl_space
*space
)
6345 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_zero(space
));
6348 /* Construct and return a piecewise multi affine expression
6349 * that is equal to the given piecewise affine expression.
6351 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_pw_aff(
6352 __isl_take isl_pw_aff
*pa
)
6356 isl_pw_multi_aff
*pma
;
6361 space
= isl_pw_aff_get_space(pa
);
6362 pma
= isl_pw_multi_aff_alloc_size(space
, pa
->n
);
6364 for (i
= 0; i
< pa
->n
; ++i
) {
6368 set
= isl_set_copy(pa
->p
[i
].set
);
6369 ma
= isl_multi_aff_from_aff(isl_aff_copy(pa
->p
[i
].aff
));
6370 pma
= isl_pw_multi_aff_add_piece(pma
, set
, ma
);
6373 isl_pw_aff_free(pa
);
6377 /* Construct and return a piecewise multi affine expression
6378 * that is equal to the given multi piecewise affine expression
6379 * on the shared domain of the piecewise affine expressions,
6380 * in the special case of a 0D multi piecewise affine expression.
6382 * Create a piecewise multi affine expression with the explicit domain of
6383 * the 0D multi piecewise affine expression as domain.
6385 static __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_multi_pw_aff_0D(
6386 __isl_take isl_multi_pw_aff
*mpa
)
6392 space
= isl_multi_pw_aff_get_space(mpa
);
6393 dom
= isl_multi_pw_aff_get_explicit_domain(mpa
);
6394 isl_multi_pw_aff_free(mpa
);
6396 ma
= isl_multi_aff_zero(space
);
6397 return isl_pw_multi_aff_alloc(dom
, ma
);
6400 /* Construct and return a piecewise multi affine expression
6401 * that is equal to the given multi piecewise affine expression
6402 * on the shared domain of the piecewise affine expressions.
6404 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_multi_pw_aff(
6405 __isl_take isl_multi_pw_aff
*mpa
)
6410 isl_pw_multi_aff
*pma
;
6416 return isl_pw_multi_aff_from_multi_pw_aff_0D(mpa
);
6418 space
= isl_multi_pw_aff_get_space(mpa
);
6419 pa
= isl_multi_pw_aff_get_pw_aff(mpa
, 0);
6420 pma
= isl_pw_multi_aff_from_pw_aff(pa
);
6422 for (i
= 1; i
< mpa
->n
; ++i
) {
6423 isl_pw_multi_aff
*pma_i
;
6425 pa
= isl_multi_pw_aff_get_pw_aff(mpa
, i
);
6426 pma_i
= isl_pw_multi_aff_from_pw_aff(pa
);
6427 pma
= isl_pw_multi_aff_range_product(pma
, pma_i
);
6430 pma
= isl_pw_multi_aff_reset_space(pma
, space
);
6432 isl_multi_pw_aff_free(mpa
);
6436 /* Construct and return a multi piecewise affine expression
6437 * that is equal to the given multi affine expression.
6439 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_from_multi_aff(
6440 __isl_take isl_multi_aff
*ma
)
6444 isl_multi_pw_aff
*mpa
;
6446 n
= isl_multi_aff_dim(ma
, isl_dim_out
);
6448 ma
= isl_multi_aff_free(ma
);
6452 mpa
= isl_multi_pw_aff_alloc(isl_multi_aff_get_space(ma
));
6454 for (i
= 0; i
< n
; ++i
) {
6457 pa
= isl_pw_aff_from_aff(isl_multi_aff_get_aff(ma
, i
));
6458 mpa
= isl_multi_pw_aff_set_pw_aff(mpa
, i
, pa
);
6461 isl_multi_aff_free(ma
);
6465 /* Construct and return a multi piecewise affine expression
6466 * that is equal to the given piecewise multi affine expression.
6468 * If the resulting multi piecewise affine expression has
6469 * an explicit domain, then assign it the domain of the input.
6470 * In other cases, the domain is stored in the individual elements.
6472 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_from_pw_multi_aff(
6473 __isl_take isl_pw_multi_aff
*pma
)
6478 isl_multi_pw_aff
*mpa
;
6480 n
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
6482 pma
= isl_pw_multi_aff_free(pma
);
6483 space
= isl_pw_multi_aff_get_space(pma
);
6484 mpa
= isl_multi_pw_aff_alloc(space
);
6486 for (i
= 0; i
< n
; ++i
) {
6489 pa
= isl_pw_multi_aff_get_pw_aff(pma
, i
);
6490 mpa
= isl_multi_pw_aff_set_pw_aff(mpa
, i
, pa
);
6492 if (isl_multi_pw_aff_has_explicit_domain(mpa
)) {
6495 dom
= isl_pw_multi_aff_domain(isl_pw_multi_aff_copy(pma
));
6496 mpa
= isl_multi_pw_aff_intersect_domain(mpa
, dom
);
6499 isl_pw_multi_aff_free(pma
);
6503 /* Do "pa1" and "pa2" represent the same function?
6505 * We first check if they are obviously equal.
6506 * If not, we convert them to maps and check if those are equal.
6508 * If "pa1" or "pa2" contain any NaNs, then they are considered
6509 * not to be the same. A NaN is not equal to anything, not even
6512 isl_bool
isl_pw_aff_is_equal(__isl_keep isl_pw_aff
*pa1
,
6513 __isl_keep isl_pw_aff
*pa2
)
6517 isl_map
*map1
, *map2
;
6520 return isl_bool_error
;
6522 equal
= isl_pw_aff_plain_is_equal(pa1
, pa2
);
6523 if (equal
< 0 || equal
)
6525 has_nan
= either_involves_nan(pa1
, pa2
);
6527 return isl_bool_error
;
6529 return isl_bool_false
;
6531 map1
= isl_map_from_pw_aff_internal(isl_pw_aff_copy(pa1
));
6532 map2
= isl_map_from_pw_aff_internal(isl_pw_aff_copy(pa2
));
6533 equal
= isl_map_is_equal(map1
, map2
);
6540 /* Do "mpa1" and "mpa2" represent the same function?
6542 * Note that we cannot convert the entire isl_multi_pw_aff
6543 * to a map because the domains of the piecewise affine expressions
6544 * may not be the same.
6546 isl_bool
isl_multi_pw_aff_is_equal(__isl_keep isl_multi_pw_aff
*mpa1
,
6547 __isl_keep isl_multi_pw_aff
*mpa2
)
6550 isl_bool equal
, equal_params
;
6553 return isl_bool_error
;
6555 equal_params
= isl_space_has_equal_params(mpa1
->space
, mpa2
->space
);
6556 if (equal_params
< 0)
6557 return isl_bool_error
;
6558 if (!equal_params
) {
6559 if (!isl_space_has_named_params(mpa1
->space
))
6560 return isl_bool_false
;
6561 if (!isl_space_has_named_params(mpa2
->space
))
6562 return isl_bool_false
;
6563 mpa1
= isl_multi_pw_aff_copy(mpa1
);
6564 mpa2
= isl_multi_pw_aff_copy(mpa2
);
6565 mpa1
= isl_multi_pw_aff_align_params(mpa1
,
6566 isl_multi_pw_aff_get_space(mpa2
));
6567 mpa2
= isl_multi_pw_aff_align_params(mpa2
,
6568 isl_multi_pw_aff_get_space(mpa1
));
6569 equal
= isl_multi_pw_aff_is_equal(mpa1
, mpa2
);
6570 isl_multi_pw_aff_free(mpa1
);
6571 isl_multi_pw_aff_free(mpa2
);
6575 equal
= isl_space_is_equal(mpa1
->space
, mpa2
->space
);
6576 if (equal
< 0 || !equal
)
6579 for (i
= 0; i
< mpa1
->n
; ++i
) {
6580 equal
= isl_pw_aff_is_equal(mpa1
->u
.p
[i
], mpa2
->u
.p
[i
]);
6581 if (equal
< 0 || !equal
)
6585 return isl_bool_true
;
6588 /* Do "pma1" and "pma2" represent the same function?
6590 * First check if they are obviously equal.
6591 * If not, then convert them to maps and check if those are equal.
6593 * If "pa1" or "pa2" contain any NaNs, then they are considered
6594 * not to be the same. A NaN is not equal to anything, not even
6597 isl_bool
isl_pw_multi_aff_is_equal(__isl_keep isl_pw_multi_aff
*pma1
,
6598 __isl_keep isl_pw_multi_aff
*pma2
)
6602 isl_map
*map1
, *map2
;
6605 return isl_bool_error
;
6607 equal
= isl_pw_multi_aff_plain_is_equal(pma1
, pma2
);
6608 if (equal
< 0 || equal
)
6610 has_nan
= isl_pw_multi_aff_involves_nan(pma1
);
6611 if (has_nan
>= 0 && !has_nan
)
6612 has_nan
= isl_pw_multi_aff_involves_nan(pma2
);
6613 if (has_nan
< 0 || has_nan
)
6614 return isl_bool_not(has_nan
);
6616 map1
= isl_map_from_pw_multi_aff(isl_pw_multi_aff_copy(pma1
));
6617 map2
= isl_map_from_pw_multi_aff(isl_pw_multi_aff_copy(pma2
));
6618 equal
= isl_map_is_equal(map1
, map2
);
6625 /* Compute the pullback of "mpa" by the function represented by "ma".
6626 * In other words, plug in "ma" in "mpa".
6628 * The parameters of "mpa" and "ma" are assumed to have been aligned.
6630 * If "mpa" has an explicit domain, then it is this domain
6631 * that needs to undergo a pullback, i.e., a preimage.
6633 static __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_pullback_multi_aff_aligned(
6634 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_multi_aff
*ma
)
6637 isl_space
*space
= NULL
;
6639 mpa
= isl_multi_pw_aff_cow(mpa
);
6643 space
= isl_space_join(isl_multi_aff_get_space(ma
),
6644 isl_multi_pw_aff_get_space(mpa
));
6648 for (i
= 0; i
< mpa
->n
; ++i
) {
6649 mpa
->u
.p
[i
] = isl_pw_aff_pullback_multi_aff(mpa
->u
.p
[i
],
6650 isl_multi_aff_copy(ma
));
6654 if (isl_multi_pw_aff_has_explicit_domain(mpa
)) {
6655 mpa
->u
.dom
= isl_set_preimage_multi_aff(mpa
->u
.dom
,
6656 isl_multi_aff_copy(ma
));
6661 isl_multi_aff_free(ma
);
6662 isl_space_free(mpa
->space
);
6666 isl_space_free(space
);
6667 isl_multi_pw_aff_free(mpa
);
6668 isl_multi_aff_free(ma
);
6672 /* Compute the pullback of "mpa" by the function represented by "ma".
6673 * In other words, plug in "ma" in "mpa".
6675 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_pullback_multi_aff(
6676 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_multi_aff
*ma
)
6678 isl_bool equal_params
;
6682 equal_params
= isl_space_has_equal_params(mpa
->space
, ma
->space
);
6683 if (equal_params
< 0)
6686 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa
, ma
);
6687 mpa
= isl_multi_pw_aff_align_params(mpa
, isl_multi_aff_get_space(ma
));
6688 ma
= isl_multi_aff_align_params(ma
, isl_multi_pw_aff_get_space(mpa
));
6689 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa
, ma
);
6691 isl_multi_pw_aff_free(mpa
);
6692 isl_multi_aff_free(ma
);
6696 /* Compute the pullback of "mpa" by the function represented by "pma".
6697 * In other words, plug in "pma" in "mpa".
6699 * The parameters of "mpa" and "mpa" are assumed to have been aligned.
6701 * If "mpa" has an explicit domain, then it is this domain
6702 * that needs to undergo a pullback, i.e., a preimage.
6704 static __isl_give isl_multi_pw_aff
*
6705 isl_multi_pw_aff_pullback_pw_multi_aff_aligned(
6706 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_pw_multi_aff
*pma
)
6709 isl_space
*space
= NULL
;
6711 mpa
= isl_multi_pw_aff_cow(mpa
);
6715 space
= isl_space_join(isl_pw_multi_aff_get_space(pma
),
6716 isl_multi_pw_aff_get_space(mpa
));
6718 for (i
= 0; i
< mpa
->n
; ++i
) {
6719 mpa
->u
.p
[i
] = isl_pw_aff_pullback_pw_multi_aff_aligned(
6720 mpa
->u
.p
[i
], isl_pw_multi_aff_copy(pma
));
6724 if (isl_multi_pw_aff_has_explicit_domain(mpa
)) {
6725 mpa
->u
.dom
= isl_set_preimage_pw_multi_aff(mpa
->u
.dom
,
6726 isl_pw_multi_aff_copy(pma
));
6731 isl_pw_multi_aff_free(pma
);
6732 isl_space_free(mpa
->space
);
6736 isl_space_free(space
);
6737 isl_multi_pw_aff_free(mpa
);
6738 isl_pw_multi_aff_free(pma
);
6742 /* Compute the pullback of "mpa" by the function represented by "pma".
6743 * In other words, plug in "pma" in "mpa".
6745 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_pullback_pw_multi_aff(
6746 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_pw_multi_aff
*pma
)
6748 isl_bool equal_params
;
6752 equal_params
= isl_space_has_equal_params(mpa
->space
, pma
->dim
);
6753 if (equal_params
< 0)
6756 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa
, pma
);
6757 mpa
= isl_multi_pw_aff_align_params(mpa
,
6758 isl_pw_multi_aff_get_space(pma
));
6759 pma
= isl_pw_multi_aff_align_params(pma
,
6760 isl_multi_pw_aff_get_space(mpa
));
6761 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa
, pma
);
6763 isl_multi_pw_aff_free(mpa
);
6764 isl_pw_multi_aff_free(pma
);
6768 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
6769 * with the domain of "aff". The domain of the result is the same
6771 * "mpa" and "aff" are assumed to have been aligned.
6773 * We first extract the parametric constant from "aff", defined
6774 * over the correct domain.
6775 * Then we add the appropriate combinations of the members of "mpa".
6776 * Finally, we add the integer divisions through recursive calls.
6778 static __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_aff_aligned(
6779 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_aff
*aff
)
6782 isl_size n_in
, n_div
, n_mpa_in
;
6788 n_in
= isl_aff_dim(aff
, isl_dim_in
);
6789 n_div
= isl_aff_dim(aff
, isl_dim_div
);
6790 n_mpa_in
= isl_multi_pw_aff_dim(mpa
, isl_dim_in
);
6791 if (n_in
< 0 || n_div
< 0 || n_mpa_in
< 0)
6794 space
= isl_space_domain(isl_multi_pw_aff_get_space(mpa
));
6795 tmp
= isl_aff_copy(aff
);
6796 tmp
= isl_aff_drop_dims(tmp
, isl_dim_div
, 0, n_div
);
6797 tmp
= isl_aff_drop_dims(tmp
, isl_dim_in
, 0, n_in
);
6798 tmp
= isl_aff_add_dims(tmp
, isl_dim_in
, n_mpa_in
);
6799 tmp
= isl_aff_reset_domain_space(tmp
, space
);
6800 pa
= isl_pw_aff_from_aff(tmp
);
6802 for (i
= 0; i
< n_in
; ++i
) {
6805 if (!isl_aff_involves_dims(aff
, isl_dim_in
, i
, 1))
6807 v
= isl_aff_get_coefficient_val(aff
, isl_dim_in
, i
);
6808 pa_i
= isl_multi_pw_aff_get_pw_aff(mpa
, i
);
6809 pa_i
= isl_pw_aff_scale_val(pa_i
, v
);
6810 pa
= isl_pw_aff_add(pa
, pa_i
);
6813 for (i
= 0; i
< n_div
; ++i
) {
6817 if (!isl_aff_involves_dims(aff
, isl_dim_div
, i
, 1))
6819 div
= isl_aff_get_div(aff
, i
);
6820 pa_i
= isl_multi_pw_aff_apply_aff_aligned(
6821 isl_multi_pw_aff_copy(mpa
), div
);
6822 pa_i
= isl_pw_aff_floor(pa_i
);
6823 v
= isl_aff_get_coefficient_val(aff
, isl_dim_div
, i
);
6824 pa_i
= isl_pw_aff_scale_val(pa_i
, v
);
6825 pa
= isl_pw_aff_add(pa
, pa_i
);
6828 isl_multi_pw_aff_free(mpa
);
6833 isl_multi_pw_aff_free(mpa
);
6838 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
6839 * with the domain of "aff". The domain of the result is the same
6842 __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_aff(
6843 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_aff
*aff
)
6845 isl_bool equal_params
;
6849 equal_params
= isl_space_has_equal_params(aff
->ls
->dim
, mpa
->space
);
6850 if (equal_params
< 0)
6853 return isl_multi_pw_aff_apply_aff_aligned(mpa
, aff
);
6855 aff
= isl_aff_align_params(aff
, isl_multi_pw_aff_get_space(mpa
));
6856 mpa
= isl_multi_pw_aff_align_params(mpa
, isl_aff_get_space(aff
));
6858 return isl_multi_pw_aff_apply_aff_aligned(mpa
, aff
);
6861 isl_multi_pw_aff_free(mpa
);
6865 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
6866 * with the domain of "pa". The domain of the result is the same
6868 * "mpa" and "pa" are assumed to have been aligned.
6870 * We consider each piece in turn. Note that the domains of the
6871 * pieces are assumed to be disjoint and they remain disjoint
6872 * after taking the preimage (over the same function).
6874 static __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_pw_aff_aligned(
6875 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_pw_aff
*pa
)
6884 space
= isl_space_join(isl_multi_pw_aff_get_space(mpa
),
6885 isl_pw_aff_get_space(pa
));
6886 res
= isl_pw_aff_empty(space
);
6888 for (i
= 0; i
< pa
->n
; ++i
) {
6892 pa_i
= isl_multi_pw_aff_apply_aff_aligned(
6893 isl_multi_pw_aff_copy(mpa
),
6894 isl_aff_copy(pa
->p
[i
].aff
));
6895 domain
= isl_set_copy(pa
->p
[i
].set
);
6896 domain
= isl_set_preimage_multi_pw_aff(domain
,
6897 isl_multi_pw_aff_copy(mpa
));
6898 pa_i
= isl_pw_aff_intersect_domain(pa_i
, domain
);
6899 res
= isl_pw_aff_add_disjoint(res
, pa_i
);
6902 isl_pw_aff_free(pa
);
6903 isl_multi_pw_aff_free(mpa
);
6906 isl_pw_aff_free(pa
);
6907 isl_multi_pw_aff_free(mpa
);
6911 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
6912 * with the domain of "pa". The domain of the result is the same
6915 __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_pw_aff(
6916 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_pw_aff
*pa
)
6918 isl_bool equal_params
;
6922 equal_params
= isl_space_has_equal_params(pa
->dim
, mpa
->space
);
6923 if (equal_params
< 0)
6926 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa
, pa
);
6928 pa
= isl_pw_aff_align_params(pa
, isl_multi_pw_aff_get_space(mpa
));
6929 mpa
= isl_multi_pw_aff_align_params(mpa
, isl_pw_aff_get_space(pa
));
6931 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa
, pa
);
6933 isl_pw_aff_free(pa
);
6934 isl_multi_pw_aff_free(mpa
);
6938 /* Compute the pullback of "pa" by the function represented by "mpa".
6939 * In other words, plug in "mpa" in "pa".
6940 * "pa" and "mpa" are assumed to have been aligned.
6942 * The pullback is computed by applying "pa" to "mpa".
6944 static __isl_give isl_pw_aff
*isl_pw_aff_pullback_multi_pw_aff_aligned(
6945 __isl_take isl_pw_aff
*pa
, __isl_take isl_multi_pw_aff
*mpa
)
6947 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa
, pa
);
6950 /* Compute the pullback of "pa" by the function represented by "mpa".
6951 * In other words, plug in "mpa" in "pa".
6953 * The pullback is computed by applying "pa" to "mpa".
6955 __isl_give isl_pw_aff
*isl_pw_aff_pullback_multi_pw_aff(
6956 __isl_take isl_pw_aff
*pa
, __isl_take isl_multi_pw_aff
*mpa
)
6958 return isl_multi_pw_aff_apply_pw_aff(mpa
, pa
);
6961 /* Compute the pullback of "mpa1" by the function represented by "mpa2".
6962 * In other words, plug in "mpa2" in "mpa1".
6964 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
6966 * We pullback each member of "mpa1" in turn.
6968 * If "mpa1" has an explicit domain, then it is this domain
6969 * that needs to undergo a pullback instead, i.e., a preimage.
6971 static __isl_give isl_multi_pw_aff
*
6972 isl_multi_pw_aff_pullback_multi_pw_aff_aligned(
6973 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
)
6976 isl_space
*space
= NULL
;
6978 mpa1
= isl_multi_pw_aff_cow(mpa1
);
6982 space
= isl_space_join(isl_multi_pw_aff_get_space(mpa2
),
6983 isl_multi_pw_aff_get_space(mpa1
));
6985 for (i
= 0; i
< mpa1
->n
; ++i
) {
6986 mpa1
->u
.p
[i
] = isl_pw_aff_pullback_multi_pw_aff_aligned(
6987 mpa1
->u
.p
[i
], isl_multi_pw_aff_copy(mpa2
));
6992 if (isl_multi_pw_aff_has_explicit_domain(mpa1
)) {
6993 mpa1
->u
.dom
= isl_set_preimage_multi_pw_aff(mpa1
->u
.dom
,
6994 isl_multi_pw_aff_copy(mpa2
));
6998 mpa1
= isl_multi_pw_aff_reset_space(mpa1
, space
);
7000 isl_multi_pw_aff_free(mpa2
);
7003 isl_space_free(space
);
7004 isl_multi_pw_aff_free(mpa1
);
7005 isl_multi_pw_aff_free(mpa2
);
7009 /* Compute the pullback of "mpa1" by the function represented by "mpa2".
7010 * In other words, plug in "mpa2" in "mpa1".
7012 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_pullback_multi_pw_aff(
7013 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
)
7015 return isl_multi_pw_aff_align_params_multi_multi_and(mpa1
, mpa2
,
7016 &isl_multi_pw_aff_pullback_multi_pw_aff_aligned
);
7019 /* Align the parameters of "mpa1" and "mpa2", check that the ranges
7020 * of "mpa1" and "mpa2" live in the same space, construct map space
7021 * between the domain spaces of "mpa1" and "mpa2" and call "order"
7022 * with this map space as extract argument.
7024 static __isl_give isl_map
*isl_multi_pw_aff_order_map(
7025 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
,
7026 __isl_give isl_map
*(*order
)(__isl_keep isl_multi_pw_aff
*mpa1
,
7027 __isl_keep isl_multi_pw_aff
*mpa2
, __isl_take isl_space
*space
))
7030 isl_space
*space1
, *space2
;
7033 mpa1
= isl_multi_pw_aff_align_params(mpa1
,
7034 isl_multi_pw_aff_get_space(mpa2
));
7035 mpa2
= isl_multi_pw_aff_align_params(mpa2
,
7036 isl_multi_pw_aff_get_space(mpa1
));
7039 match
= isl_space_tuple_is_equal(mpa1
->space
, isl_dim_out
,
7040 mpa2
->space
, isl_dim_out
);
7044 isl_die(isl_multi_pw_aff_get_ctx(mpa1
), isl_error_invalid
,
7045 "range spaces don't match", goto error
);
7046 space1
= isl_space_domain(isl_multi_pw_aff_get_space(mpa1
));
7047 space2
= isl_space_domain(isl_multi_pw_aff_get_space(mpa2
));
7048 space1
= isl_space_map_from_domain_and_range(space1
, space2
);
7050 res
= order(mpa1
, mpa2
, space1
);
7051 isl_multi_pw_aff_free(mpa1
);
7052 isl_multi_pw_aff_free(mpa2
);
7055 isl_multi_pw_aff_free(mpa1
);
7056 isl_multi_pw_aff_free(mpa2
);
7060 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7061 * where the function values are equal. "space" is the space of the result.
7062 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7064 * "mpa1" and "mpa2" are equal when each of the pairs of elements
7065 * in the sequences are equal.
7067 static __isl_give isl_map
*isl_multi_pw_aff_eq_map_on_space(
7068 __isl_keep isl_multi_pw_aff
*mpa1
, __isl_keep isl_multi_pw_aff
*mpa2
,
7069 __isl_take isl_space
*space
)
7075 n
= isl_multi_pw_aff_dim(mpa1
, isl_dim_out
);
7077 space
= isl_space_free(space
);
7078 res
= isl_map_universe(space
);
7080 for (i
= 0; i
< n
; ++i
) {
7081 isl_pw_aff
*pa1
, *pa2
;
7084 pa1
= isl_multi_pw_aff_get_pw_aff(mpa1
, i
);
7085 pa2
= isl_multi_pw_aff_get_pw_aff(mpa2
, i
);
7086 map
= isl_pw_aff_eq_map(pa1
, pa2
);
7087 res
= isl_map_intersect(res
, map
);
7093 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7094 * where the function values are equal.
7096 __isl_give isl_map
*isl_multi_pw_aff_eq_map(__isl_take isl_multi_pw_aff
*mpa1
,
7097 __isl_take isl_multi_pw_aff
*mpa2
)
7099 return isl_multi_pw_aff_order_map(mpa1
, mpa2
,
7100 &isl_multi_pw_aff_eq_map_on_space
);
7103 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7104 * where the function values of "mpa1" is lexicographically satisfies "base"
7105 * compared to that of "mpa2". "space" is the space of the result.
7106 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7108 * "mpa1" lexicographically satisfies "base" compared to "mpa2"
7109 * if its i-th element satisfies "base" when compared to
7110 * the i-th element of "mpa2" while all previous elements are
7113 static __isl_give isl_map
*isl_multi_pw_aff_lex_map_on_space(
7114 __isl_keep isl_multi_pw_aff
*mpa1
, __isl_keep isl_multi_pw_aff
*mpa2
,
7115 __isl_give isl_map
*(*base
)(__isl_take isl_pw_aff
*pa1
,
7116 __isl_take isl_pw_aff
*pa2
),
7117 __isl_take isl_space
*space
)
7121 isl_map
*res
, *rest
;
7123 n
= isl_multi_pw_aff_dim(mpa1
, isl_dim_out
);
7125 space
= isl_space_free(space
);
7126 res
= isl_map_empty(isl_space_copy(space
));
7127 rest
= isl_map_universe(space
);
7129 for (i
= 0; i
< n
; ++i
) {
7130 isl_pw_aff
*pa1
, *pa2
;
7133 pa1
= isl_multi_pw_aff_get_pw_aff(mpa1
, i
);
7134 pa2
= isl_multi_pw_aff_get_pw_aff(mpa2
, i
);
7135 map
= base(pa1
, pa2
);
7136 map
= isl_map_intersect(map
, isl_map_copy(rest
));
7137 res
= isl_map_union(res
, map
);
7142 pa1
= isl_multi_pw_aff_get_pw_aff(mpa1
, i
);
7143 pa2
= isl_multi_pw_aff_get_pw_aff(mpa2
, i
);
7144 map
= isl_pw_aff_eq_map(pa1
, pa2
);
7145 rest
= isl_map_intersect(rest
, map
);
7152 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7153 * where the function value of "mpa1" is lexicographically less than that
7154 * of "mpa2". "space" is the space of the result.
7155 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7157 * "mpa1" is less than "mpa2" if its i-th element is smaller
7158 * than the i-th element of "mpa2" while all previous elements are
7161 __isl_give isl_map
*isl_multi_pw_aff_lex_lt_map_on_space(
7162 __isl_keep isl_multi_pw_aff
*mpa1
, __isl_keep isl_multi_pw_aff
*mpa2
,
7163 __isl_take isl_space
*space
)
7165 return isl_multi_pw_aff_lex_map_on_space(mpa1
, mpa2
,
7166 &isl_pw_aff_lt_map
, space
);
7169 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7170 * where the function value of "mpa1" is lexicographically less than that
7173 __isl_give isl_map
*isl_multi_pw_aff_lex_lt_map(
7174 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
)
7176 return isl_multi_pw_aff_order_map(mpa1
, mpa2
,
7177 &isl_multi_pw_aff_lex_lt_map_on_space
);
7180 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7181 * where the function value of "mpa1" is lexicographically greater than that
7182 * of "mpa2". "space" is the space of the result.
7183 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7185 * "mpa1" is greater than "mpa2" if its i-th element is greater
7186 * than the i-th element of "mpa2" while all previous elements are
7189 __isl_give isl_map
*isl_multi_pw_aff_lex_gt_map_on_space(
7190 __isl_keep isl_multi_pw_aff
*mpa1
, __isl_keep isl_multi_pw_aff
*mpa2
,
7191 __isl_take isl_space
*space
)
7193 return isl_multi_pw_aff_lex_map_on_space(mpa1
, mpa2
,
7194 &isl_pw_aff_gt_map
, space
);
7197 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7198 * where the function value of "mpa1" is lexicographically greater than that
7201 __isl_give isl_map
*isl_multi_pw_aff_lex_gt_map(
7202 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
)
7204 return isl_multi_pw_aff_order_map(mpa1
, mpa2
,
7205 &isl_multi_pw_aff_lex_gt_map_on_space
);
7208 /* Compare two isl_affs.
7210 * Return -1 if "aff1" is "smaller" than "aff2", 1 if "aff1" is "greater"
7211 * than "aff2" and 0 if they are equal.
7213 * The order is fairly arbitrary. We do consider expressions that only involve
7214 * earlier dimensions as "smaller".
7216 int isl_aff_plain_cmp(__isl_keep isl_aff
*aff1
, __isl_keep isl_aff
*aff2
)
7229 cmp
= isl_local_space_cmp(aff1
->ls
, aff2
->ls
);
7233 last1
= isl_seq_last_non_zero(aff1
->v
->el
+ 1, aff1
->v
->size
- 1);
7234 last2
= isl_seq_last_non_zero(aff2
->v
->el
+ 1, aff1
->v
->size
- 1);
7236 return last1
- last2
;
7238 return isl_seq_cmp(aff1
->v
->el
, aff2
->v
->el
, aff1
->v
->size
);
7241 /* Compare two isl_pw_affs.
7243 * Return -1 if "pa1" is "smaller" than "pa2", 1 if "pa1" is "greater"
7244 * than "pa2" and 0 if they are equal.
7246 * The order is fairly arbitrary. We do consider expressions that only involve
7247 * earlier dimensions as "smaller".
7249 int isl_pw_aff_plain_cmp(__isl_keep isl_pw_aff
*pa1
,
7250 __isl_keep isl_pw_aff
*pa2
)
7263 cmp
= isl_space_cmp(pa1
->dim
, pa2
->dim
);
7267 if (pa1
->n
!= pa2
->n
)
7268 return pa1
->n
- pa2
->n
;
7270 for (i
= 0; i
< pa1
->n
; ++i
) {
7271 cmp
= isl_set_plain_cmp(pa1
->p
[i
].set
, pa2
->p
[i
].set
);
7274 cmp
= isl_aff_plain_cmp(pa1
->p
[i
].aff
, pa2
->p
[i
].aff
);
7282 /* Return a piecewise affine expression that is equal to "v" on "domain".
7284 __isl_give isl_pw_aff
*isl_pw_aff_val_on_domain(__isl_take isl_set
*domain
,
7285 __isl_take isl_val
*v
)
7288 isl_local_space
*ls
;
7291 space
= isl_set_get_space(domain
);
7292 ls
= isl_local_space_from_space(space
);
7293 aff
= isl_aff_val_on_domain(ls
, v
);
7295 return isl_pw_aff_alloc(domain
, aff
);
7298 /* Return a multi affine expression that is equal to "mv" on domain
7301 __isl_give isl_multi_aff
*isl_multi_aff_multi_val_on_space(
7302 __isl_take isl_space
*space
, __isl_take isl_multi_val
*mv
)
7307 isl_local_space
*ls
;
7310 n
= isl_multi_val_dim(mv
, isl_dim_set
);
7311 if (!space
|| n
< 0)
7314 space2
= isl_multi_val_get_space(mv
);
7315 space2
= isl_space_align_params(space2
, isl_space_copy(space
));
7316 space
= isl_space_align_params(space
, isl_space_copy(space2
));
7317 space
= isl_space_map_from_domain_and_range(space
, space2
);
7318 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
7319 ls
= isl_local_space_from_space(isl_space_domain(space
));
7320 for (i
= 0; i
< n
; ++i
) {
7324 v
= isl_multi_val_get_val(mv
, i
);
7325 aff
= isl_aff_val_on_domain(isl_local_space_copy(ls
), v
);
7326 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
7328 isl_local_space_free(ls
);
7330 isl_multi_val_free(mv
);
7333 isl_space_free(space
);
7334 isl_multi_val_free(mv
);
7338 /* Return a piecewise multi-affine expression
7339 * that is equal to "mv" on "domain".
7341 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_multi_val_on_domain(
7342 __isl_take isl_set
*domain
, __isl_take isl_multi_val
*mv
)
7347 space
= isl_set_get_space(domain
);
7348 ma
= isl_multi_aff_multi_val_on_space(space
, mv
);
7350 return isl_pw_multi_aff_alloc(domain
, ma
);
7353 /* Internal data structure for isl_union_pw_multi_aff_multi_val_on_domain.
7354 * mv is the value that should be attained on each domain set
7355 * res collects the results
7357 struct isl_union_pw_multi_aff_multi_val_on_domain_data
{
7359 isl_union_pw_multi_aff
*res
;
7362 /* Create an isl_pw_multi_aff equal to data->mv on "domain"
7363 * and add it to data->res.
7365 static isl_stat
pw_multi_aff_multi_val_on_domain(__isl_take isl_set
*domain
,
7368 struct isl_union_pw_multi_aff_multi_val_on_domain_data
*data
= user
;
7369 isl_pw_multi_aff
*pma
;
7372 mv
= isl_multi_val_copy(data
->mv
);
7373 pma
= isl_pw_multi_aff_multi_val_on_domain(domain
, mv
);
7374 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma
);
7376 return data
->res
? isl_stat_ok
: isl_stat_error
;
7379 /* Return a union piecewise multi-affine expression
7380 * that is equal to "mv" on "domain".
7382 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_multi_val_on_domain(
7383 __isl_take isl_union_set
*domain
, __isl_take isl_multi_val
*mv
)
7385 struct isl_union_pw_multi_aff_multi_val_on_domain_data data
;
7388 space
= isl_union_set_get_space(domain
);
7389 data
.res
= isl_union_pw_multi_aff_empty(space
);
7391 if (isl_union_set_foreach_set(domain
,
7392 &pw_multi_aff_multi_val_on_domain
, &data
) < 0)
7393 data
.res
= isl_union_pw_multi_aff_free(data
.res
);
7394 isl_union_set_free(domain
);
7395 isl_multi_val_free(mv
);
7399 /* Compute the pullback of data->pma by the function represented by "pma2",
7400 * provided the spaces match, and add the results to data->res.
7402 static isl_stat
pullback_entry(__isl_take isl_pw_multi_aff
*pma2
, void *user
)
7404 struct isl_union_pw_multi_aff_bin_data
*data
= user
;
7406 if (!isl_space_tuple_is_equal(data
->pma
->dim
, isl_dim_in
,
7407 pma2
->dim
, isl_dim_out
)) {
7408 isl_pw_multi_aff_free(pma2
);
7412 pma2
= isl_pw_multi_aff_pullback_pw_multi_aff(
7413 isl_pw_multi_aff_copy(data
->pma
), pma2
);
7415 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma2
);
7417 return isl_stat_error
;
7422 /* Compute the pullback of "upma1" by the function represented by "upma2".
7424 __isl_give isl_union_pw_multi_aff
*
7425 isl_union_pw_multi_aff_pullback_union_pw_multi_aff(
7426 __isl_take isl_union_pw_multi_aff
*upma1
,
7427 __isl_take isl_union_pw_multi_aff
*upma2
)
7429 return bin_op(upma1
, upma2
, &pullback_entry
);
7432 /* Check that the domain space of "upa" matches "space".
7434 * This function is called from isl_multi_union_pw_aff_set_union_pw_aff and
7435 * can in principle never fail since the space "space" is that
7436 * of the isl_multi_union_pw_aff and is a set space such that
7437 * there is no domain space to match.
7439 * We check the parameters and double-check that "space" is
7440 * indeed that of a set.
7442 static isl_stat
isl_union_pw_aff_check_match_domain_space(
7443 __isl_keep isl_union_pw_aff
*upa
, __isl_keep isl_space
*space
)
7445 isl_space
*upa_space
;
7449 return isl_stat_error
;
7451 match
= isl_space_is_set(space
);
7453 return isl_stat_error
;
7455 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
7456 "expecting set space", return isl_stat_error
);
7458 upa_space
= isl_union_pw_aff_get_space(upa
);
7459 match
= isl_space_has_equal_params(space
, upa_space
);
7463 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
7464 "parameters don't match", goto error
);
7466 isl_space_free(upa_space
);
7469 isl_space_free(upa_space
);
7470 return isl_stat_error
;
7473 /* Do the parameters of "upa" match those of "space"?
7475 static isl_bool
isl_union_pw_aff_matching_params(
7476 __isl_keep isl_union_pw_aff
*upa
, __isl_keep isl_space
*space
)
7478 isl_space
*upa_space
;
7482 return isl_bool_error
;
7484 upa_space
= isl_union_pw_aff_get_space(upa
);
7486 match
= isl_space_has_equal_params(space
, upa_space
);
7488 isl_space_free(upa_space
);
7492 /* Internal data structure for isl_union_pw_aff_reset_domain_space.
7493 * space represents the new parameters.
7494 * res collects the results.
7496 struct isl_union_pw_aff_reset_params_data
{
7498 isl_union_pw_aff
*res
;
7501 /* Replace the parameters of "pa" by data->space and
7502 * add the result to data->res.
7504 static isl_stat
reset_params(__isl_take isl_pw_aff
*pa
, void *user
)
7506 struct isl_union_pw_aff_reset_params_data
*data
= user
;
7509 space
= isl_pw_aff_get_space(pa
);
7510 space
= isl_space_replace_params(space
, data
->space
);
7511 pa
= isl_pw_aff_reset_space(pa
, space
);
7512 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
7514 return data
->res
? isl_stat_ok
: isl_stat_error
;
7517 /* Replace the domain space of "upa" by "space".
7518 * Since a union expression does not have a (single) domain space,
7519 * "space" is necessarily a parameter space.
7521 * Since the order and the names of the parameters determine
7522 * the hash value, we need to create a new hash table.
7524 static __isl_give isl_union_pw_aff
*isl_union_pw_aff_reset_domain_space(
7525 __isl_take isl_union_pw_aff
*upa
, __isl_take isl_space
*space
)
7527 struct isl_union_pw_aff_reset_params_data data
= { space
};
7530 match
= isl_union_pw_aff_matching_params(upa
, space
);
7532 upa
= isl_union_pw_aff_free(upa
);
7534 isl_space_free(space
);
7538 data
.res
= isl_union_pw_aff_empty(isl_space_copy(space
));
7539 if (isl_union_pw_aff_foreach_pw_aff(upa
, &reset_params
, &data
) < 0)
7540 data
.res
= isl_union_pw_aff_free(data
.res
);
7542 isl_union_pw_aff_free(upa
);
7543 isl_space_free(space
);
7547 /* Return the floor of "pa".
7549 static __isl_give isl_pw_aff
*floor_entry(__isl_take isl_pw_aff
*pa
, void *user
)
7551 return isl_pw_aff_floor(pa
);
7554 /* Given f, return floor(f).
7556 __isl_give isl_union_pw_aff
*isl_union_pw_aff_floor(
7557 __isl_take isl_union_pw_aff
*upa
)
7559 return isl_union_pw_aff_transform_inplace(upa
, &floor_entry
, NULL
);
7564 * upa mod m = upa - m * floor(upa/m)
7566 * with m an integer value.
7568 __isl_give isl_union_pw_aff
*isl_union_pw_aff_mod_val(
7569 __isl_take isl_union_pw_aff
*upa
, __isl_take isl_val
*m
)
7571 isl_union_pw_aff
*res
;
7576 if (!isl_val_is_int(m
))
7577 isl_die(isl_val_get_ctx(m
), isl_error_invalid
,
7578 "expecting integer modulo", goto error
);
7579 if (!isl_val_is_pos(m
))
7580 isl_die(isl_val_get_ctx(m
), isl_error_invalid
,
7581 "expecting positive modulo", goto error
);
7583 res
= isl_union_pw_aff_copy(upa
);
7584 upa
= isl_union_pw_aff_scale_down_val(upa
, isl_val_copy(m
));
7585 upa
= isl_union_pw_aff_floor(upa
);
7586 upa
= isl_union_pw_aff_scale_val(upa
, m
);
7587 res
= isl_union_pw_aff_sub(res
, upa
);
7592 isl_union_pw_aff_free(upa
);
7596 /* Internal data structure for isl_union_pw_multi_aff_get_union_pw_aff.
7597 * pos is the output position that needs to be extracted.
7598 * res collects the results.
7600 struct isl_union_pw_multi_aff_get_union_pw_aff_data
{
7602 isl_union_pw_aff
*res
;
7605 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma"
7606 * (assuming it has such a dimension) and add it to data->res.
7608 static isl_stat
get_union_pw_aff(__isl_take isl_pw_multi_aff
*pma
, void *user
)
7610 struct isl_union_pw_multi_aff_get_union_pw_aff_data
*data
= user
;
7614 n_out
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
7616 return isl_stat_error
;
7617 if (data
->pos
>= n_out
) {
7618 isl_pw_multi_aff_free(pma
);
7622 pa
= isl_pw_multi_aff_get_pw_aff(pma
, data
->pos
);
7623 isl_pw_multi_aff_free(pma
);
7625 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
7627 return data
->res
? isl_stat_ok
: isl_stat_error
;
7630 /* Extract an isl_union_pw_aff corresponding to
7631 * output dimension "pos" of "upma".
7633 __isl_give isl_union_pw_aff
*isl_union_pw_multi_aff_get_union_pw_aff(
7634 __isl_keep isl_union_pw_multi_aff
*upma
, int pos
)
7636 struct isl_union_pw_multi_aff_get_union_pw_aff_data data
;
7643 isl_die(isl_union_pw_multi_aff_get_ctx(upma
), isl_error_invalid
,
7644 "cannot extract at negative position", return NULL
);
7646 space
= isl_union_pw_multi_aff_get_space(upma
);
7647 data
.res
= isl_union_pw_aff_empty(space
);
7649 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma
,
7650 &get_union_pw_aff
, &data
) < 0)
7651 data
.res
= isl_union_pw_aff_free(data
.res
);
7656 /* Return a union piecewise affine expression
7657 * that is equal to "aff" on "domain".
7659 __isl_give isl_union_pw_aff
*isl_union_pw_aff_aff_on_domain(
7660 __isl_take isl_union_set
*domain
, __isl_take isl_aff
*aff
)
7664 pa
= isl_pw_aff_from_aff(aff
);
7665 return isl_union_pw_aff_pw_aff_on_domain(domain
, pa
);
7668 /* Return a union piecewise affine expression
7669 * that is equal to the parameter identified by "id" on "domain".
7671 * Make sure the parameter appears in the space passed to
7672 * isl_aff_param_on_domain_space_id.
7674 __isl_give isl_union_pw_aff
*isl_union_pw_aff_param_on_domain_id(
7675 __isl_take isl_union_set
*domain
, __isl_take isl_id
*id
)
7680 space
= isl_union_set_get_space(domain
);
7681 space
= isl_space_add_param_id(space
, isl_id_copy(id
));
7682 aff
= isl_aff_param_on_domain_space_id(space
, id
);
7683 return isl_union_pw_aff_aff_on_domain(domain
, aff
);
7686 /* Internal data structure for isl_union_pw_aff_pw_aff_on_domain.
7687 * "pa" is the piecewise symbolic value that the resulting isl_union_pw_aff
7689 * "res" collects the results.
7691 struct isl_union_pw_aff_pw_aff_on_domain_data
{
7693 isl_union_pw_aff
*res
;
7696 /* Construct a piecewise affine expression that is equal to data->pa
7697 * on "domain" and add the result to data->res.
7699 static isl_stat
pw_aff_on_domain(__isl_take isl_set
*domain
, void *user
)
7701 struct isl_union_pw_aff_pw_aff_on_domain_data
*data
= user
;
7705 pa
= isl_pw_aff_copy(data
->pa
);
7706 dim
= isl_set_dim(domain
, isl_dim_set
);
7708 pa
= isl_pw_aff_free(pa
);
7709 pa
= isl_pw_aff_from_range(pa
);
7710 pa
= isl_pw_aff_add_dims(pa
, isl_dim_in
, dim
);
7711 pa
= isl_pw_aff_reset_domain_space(pa
, isl_set_get_space(domain
));
7712 pa
= isl_pw_aff_intersect_domain(pa
, domain
);
7713 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
7715 return data
->res
? isl_stat_ok
: isl_stat_error
;
7718 /* Return a union piecewise affine expression
7719 * that is equal to "pa" on "domain", assuming "domain" and "pa"
7720 * have been aligned.
7722 * Construct an isl_pw_aff on each of the sets in "domain" and
7723 * collect the results.
7725 static __isl_give isl_union_pw_aff
*isl_union_pw_aff_pw_aff_on_domain_aligned(
7726 __isl_take isl_union_set
*domain
, __isl_take isl_pw_aff
*pa
)
7728 struct isl_union_pw_aff_pw_aff_on_domain_data data
;
7731 space
= isl_union_set_get_space(domain
);
7732 data
.res
= isl_union_pw_aff_empty(space
);
7734 if (isl_union_set_foreach_set(domain
, &pw_aff_on_domain
, &data
) < 0)
7735 data
.res
= isl_union_pw_aff_free(data
.res
);
7736 isl_union_set_free(domain
);
7737 isl_pw_aff_free(pa
);
7741 /* Return a union piecewise affine expression
7742 * that is equal to "pa" on "domain".
7744 * Check that "pa" is a parametric expression,
7745 * align the parameters if needed and call
7746 * isl_union_pw_aff_pw_aff_on_domain_aligned.
7748 __isl_give isl_union_pw_aff
*isl_union_pw_aff_pw_aff_on_domain(
7749 __isl_take isl_union_set
*domain
, __isl_take isl_pw_aff
*pa
)
7752 isl_bool equal_params
;
7753 isl_space
*domain_space
, *pa_space
;
7755 pa_space
= isl_pw_aff_peek_space(pa
);
7756 is_set
= isl_space_is_set(pa_space
);
7760 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
7761 "expecting parametric expression", goto error
);
7763 domain_space
= isl_union_set_get_space(domain
);
7764 pa_space
= isl_pw_aff_get_space(pa
);
7765 equal_params
= isl_space_has_equal_params(domain_space
, pa_space
);
7766 if (equal_params
>= 0 && !equal_params
) {
7769 space
= isl_space_align_params(domain_space
, pa_space
);
7770 pa
= isl_pw_aff_align_params(pa
, isl_space_copy(space
));
7771 domain
= isl_union_set_align_params(domain
, space
);
7773 isl_space_free(domain_space
);
7774 isl_space_free(pa_space
);
7777 if (equal_params
< 0)
7779 return isl_union_pw_aff_pw_aff_on_domain_aligned(domain
, pa
);
7781 isl_union_set_free(domain
);
7782 isl_pw_aff_free(pa
);
7786 /* Internal data structure for isl_union_pw_aff_val_on_domain.
7787 * "v" is the value that the resulting isl_union_pw_aff needs to attain.
7788 * "res" collects the results.
7790 struct isl_union_pw_aff_val_on_domain_data
{
7792 isl_union_pw_aff
*res
;
7795 /* Construct a piecewise affine expression that is equal to data->v
7796 * on "domain" and add the result to data->res.
7798 static isl_stat
pw_aff_val_on_domain(__isl_take isl_set
*domain
, void *user
)
7800 struct isl_union_pw_aff_val_on_domain_data
*data
= user
;
7804 v
= isl_val_copy(data
->v
);
7805 pa
= isl_pw_aff_val_on_domain(domain
, v
);
7806 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
7808 return data
->res
? isl_stat_ok
: isl_stat_error
;
7811 /* Return a union piecewise affine expression
7812 * that is equal to "v" on "domain".
7814 * Construct an isl_pw_aff on each of the sets in "domain" and
7815 * collect the results.
7817 __isl_give isl_union_pw_aff
*isl_union_pw_aff_val_on_domain(
7818 __isl_take isl_union_set
*domain
, __isl_take isl_val
*v
)
7820 struct isl_union_pw_aff_val_on_domain_data data
;
7823 space
= isl_union_set_get_space(domain
);
7824 data
.res
= isl_union_pw_aff_empty(space
);
7826 if (isl_union_set_foreach_set(domain
, &pw_aff_val_on_domain
, &data
) < 0)
7827 data
.res
= isl_union_pw_aff_free(data
.res
);
7828 isl_union_set_free(domain
);
7833 /* Construct a piecewise multi affine expression
7834 * that is equal to "pa" and add it to upma.
7836 static isl_stat
pw_multi_aff_from_pw_aff_entry(__isl_take isl_pw_aff
*pa
,
7839 isl_union_pw_multi_aff
**upma
= user
;
7840 isl_pw_multi_aff
*pma
;
7842 pma
= isl_pw_multi_aff_from_pw_aff(pa
);
7843 *upma
= isl_union_pw_multi_aff_add_pw_multi_aff(*upma
, pma
);
7845 return *upma
? isl_stat_ok
: isl_stat_error
;
7848 /* Construct and return a union piecewise multi affine expression
7849 * that is equal to the given union piecewise affine expression.
7851 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_union_pw_aff(
7852 __isl_take isl_union_pw_aff
*upa
)
7855 isl_union_pw_multi_aff
*upma
;
7860 space
= isl_union_pw_aff_get_space(upa
);
7861 upma
= isl_union_pw_multi_aff_empty(space
);
7863 if (isl_union_pw_aff_foreach_pw_aff(upa
,
7864 &pw_multi_aff_from_pw_aff_entry
, &upma
) < 0)
7865 upma
= isl_union_pw_multi_aff_free(upma
);
7867 isl_union_pw_aff_free(upa
);
7871 /* Compute the set of elements in the domain of "pa" where it is zero and
7872 * add this set to "uset".
7874 static isl_stat
zero_union_set(__isl_take isl_pw_aff
*pa
, void *user
)
7876 isl_union_set
**uset
= (isl_union_set
**)user
;
7878 *uset
= isl_union_set_add_set(*uset
, isl_pw_aff_zero_set(pa
));
7880 return *uset
? isl_stat_ok
: isl_stat_error
;
7883 /* Return a union set containing those elements in the domain
7884 * of "upa" where it is zero.
7886 __isl_give isl_union_set
*isl_union_pw_aff_zero_union_set(
7887 __isl_take isl_union_pw_aff
*upa
)
7889 isl_union_set
*zero
;
7891 zero
= isl_union_set_empty(isl_union_pw_aff_get_space(upa
));
7892 if (isl_union_pw_aff_foreach_pw_aff(upa
, &zero_union_set
, &zero
) < 0)
7893 zero
= isl_union_set_free(zero
);
7895 isl_union_pw_aff_free(upa
);
7899 /* Internal data structure for isl_union_pw_aff_pullback_union_pw_multi_aff.
7900 * upma is the function that is plugged in.
7901 * pa is the current part of the function in which upma is plugged in.
7902 * res collects the results.
7904 struct isl_union_pw_aff_pullback_upma_data
{
7905 isl_union_pw_multi_aff
*upma
;
7907 isl_union_pw_aff
*res
;
7910 /* Check if "pma" can be plugged into data->pa.
7911 * If so, perform the pullback and add the result to data->res.
7913 static isl_stat
pa_pb_pma(__isl_take isl_pw_multi_aff
*pma
, void *user
)
7915 struct isl_union_pw_aff_pullback_upma_data
*data
= user
;
7918 if (!isl_space_tuple_is_equal(data
->pa
->dim
, isl_dim_in
,
7919 pma
->dim
, isl_dim_out
)) {
7920 isl_pw_multi_aff_free(pma
);
7924 pa
= isl_pw_aff_copy(data
->pa
);
7925 pa
= isl_pw_aff_pullback_pw_multi_aff(pa
, pma
);
7927 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
7929 return data
->res
? isl_stat_ok
: isl_stat_error
;
7932 /* Check if any of the elements of data->upma can be plugged into pa,
7933 * add if so add the result to data->res.
7935 static isl_stat
upa_pb_upma(__isl_take isl_pw_aff
*pa
, void *user
)
7937 struct isl_union_pw_aff_pullback_upma_data
*data
= user
;
7941 r
= isl_union_pw_multi_aff_foreach_pw_multi_aff(data
->upma
,
7943 isl_pw_aff_free(pa
);
7948 /* Compute the pullback of "upa" by the function represented by "upma".
7949 * In other words, plug in "upma" in "upa". The result contains
7950 * expressions defined over the domain space of "upma".
7952 * Run over all pairs of elements in "upa" and "upma", perform
7953 * the pullback when appropriate and collect the results.
7954 * If the hash value were based on the domain space rather than
7955 * the function space, then we could run through all elements
7956 * of "upma" and directly pick out the corresponding element of "upa".
7958 __isl_give isl_union_pw_aff
*isl_union_pw_aff_pullback_union_pw_multi_aff(
7959 __isl_take isl_union_pw_aff
*upa
,
7960 __isl_take isl_union_pw_multi_aff
*upma
)
7962 struct isl_union_pw_aff_pullback_upma_data data
= { NULL
, NULL
};
7965 space
= isl_union_pw_multi_aff_get_space(upma
);
7966 upa
= isl_union_pw_aff_align_params(upa
, space
);
7967 space
= isl_union_pw_aff_get_space(upa
);
7968 upma
= isl_union_pw_multi_aff_align_params(upma
, space
);
7974 data
.res
= isl_union_pw_aff_alloc_same_size(upa
);
7975 if (isl_union_pw_aff_foreach_pw_aff(upa
, &upa_pb_upma
, &data
) < 0)
7976 data
.res
= isl_union_pw_aff_free(data
.res
);
7978 isl_union_pw_aff_free(upa
);
7979 isl_union_pw_multi_aff_free(upma
);
7982 isl_union_pw_aff_free(upa
);
7983 isl_union_pw_multi_aff_free(upma
);
7988 #define BASE union_pw_aff
7990 #define DOMBASE union_set
7992 #include <isl_multi_explicit_domain.c>
7993 #include <isl_multi_union_pw_aff_explicit_domain.c>
7994 #include <isl_multi_templ.c>
7995 #include <isl_multi_apply_set.c>
7996 #include <isl_multi_apply_union_set.c>
7997 #include <isl_multi_coalesce.c>
7998 #include <isl_multi_floor.c>
7999 #include <isl_multi_from_base_templ.c>
8000 #include <isl_multi_gist.c>
8001 #include <isl_multi_align_set.c>
8002 #include <isl_multi_align_union_set.c>
8003 #include <isl_multi_intersect.c>
8005 /* Does "mupa" have a non-trivial explicit domain?
8007 * The explicit domain, if present, is trivial if it represents
8008 * an (obviously) universe parameter set.
8010 isl_bool
isl_multi_union_pw_aff_has_non_trivial_domain(
8011 __isl_keep isl_multi_union_pw_aff
*mupa
)
8013 isl_bool is_params
, trivial
;
8017 return isl_bool_error
;
8018 if (!isl_multi_union_pw_aff_has_explicit_domain(mupa
))
8019 return isl_bool_false
;
8020 is_params
= isl_union_set_is_params(mupa
->u
.dom
);
8021 if (is_params
< 0 || !is_params
)
8022 return isl_bool_not(is_params
);
8023 set
= isl_set_from_union_set(isl_union_set_copy(mupa
->u
.dom
));
8024 trivial
= isl_set_plain_is_universe(set
);
8026 return isl_bool_not(trivial
);
8029 /* Construct a multiple union piecewise affine expression
8030 * in the given space with value zero in each of the output dimensions.
8032 * Since there is no canonical zero value for
8033 * a union piecewise affine expression, we can only construct
8034 * a zero-dimensional "zero" value.
8036 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_zero(
8037 __isl_take isl_space
*space
)
8045 params
= isl_space_is_params(space
);
8049 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
8050 "expecting proper set space", goto error
);
8051 if (!isl_space_is_set(space
))
8052 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
8053 "expecting set space", goto error
);
8054 dim
= isl_space_dim(space
, isl_dim_out
);
8058 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
8059 "expecting 0D space", goto error
);
8061 return isl_multi_union_pw_aff_alloc(space
);
8063 isl_space_free(space
);
8067 /* Compute the sum of "mupa1" and "mupa2" on the union of their domains,
8068 * with the actual sum on the shared domain and
8069 * the defined expression on the symmetric difference of the domains.
8071 * We simply iterate over the elements in both arguments and
8072 * call isl_union_pw_aff_union_add on each of them, if there is
8073 * at least one element.
8075 * Otherwise, the two expressions have an explicit domain and
8076 * the union of these explicit domains is computed.
8077 * This assumes that the explicit domains are either both in terms
8078 * of specific domains elements or both in terms of parameters.
8079 * However, if one of the expressions does not have any constraints
8080 * on its explicit domain, then this is allowed as well and the result
8081 * is the expression with no constraints on its explicit domain.
8083 static __isl_give isl_multi_union_pw_aff
*
8084 isl_multi_union_pw_aff_union_add_aligned(
8085 __isl_take isl_multi_union_pw_aff
*mupa1
,
8086 __isl_take isl_multi_union_pw_aff
*mupa2
)
8088 isl_bool has_domain
, is_params1
, is_params2
;
8090 if (isl_multi_union_pw_aff_check_equal_space(mupa1
, mupa2
) < 0)
8093 return isl_multi_union_pw_aff_bin_op(mupa1
, mupa2
,
8094 &isl_union_pw_aff_union_add
);
8095 if (isl_multi_union_pw_aff_check_has_explicit_domain(mupa1
) < 0 ||
8096 isl_multi_union_pw_aff_check_has_explicit_domain(mupa2
) < 0)
8099 has_domain
= isl_multi_union_pw_aff_has_non_trivial_domain(mupa1
);
8103 isl_multi_union_pw_aff_free(mupa2
);
8106 has_domain
= isl_multi_union_pw_aff_has_non_trivial_domain(mupa2
);
8110 isl_multi_union_pw_aff_free(mupa1
);
8114 is_params1
= isl_union_set_is_params(mupa1
->u
.dom
);
8115 is_params2
= isl_union_set_is_params(mupa2
->u
.dom
);
8116 if (is_params1
< 0 || is_params2
< 0)
8118 if (is_params1
!= is_params2
)
8119 isl_die(isl_multi_union_pw_aff_get_ctx(mupa1
),
8121 "cannot compute union of concrete domain and "
8122 "parameter constraints", goto error
);
8123 mupa1
= isl_multi_union_pw_aff_cow(mupa1
);
8126 mupa1
->u
.dom
= isl_union_set_union(mupa1
->u
.dom
,
8127 isl_union_set_copy(mupa2
->u
.dom
));
8130 isl_multi_union_pw_aff_free(mupa2
);
8133 isl_multi_union_pw_aff_free(mupa1
);
8134 isl_multi_union_pw_aff_free(mupa2
);
8138 /* Compute the sum of "mupa1" and "mupa2" on the union of their domains,
8139 * with the actual sum on the shared domain and
8140 * the defined expression on the symmetric difference of the domains.
8142 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_union_add(
8143 __isl_take isl_multi_union_pw_aff
*mupa1
,
8144 __isl_take isl_multi_union_pw_aff
*mupa2
)
8146 return isl_multi_union_pw_aff_align_params_multi_multi_and(mupa1
, mupa2
,
8147 &isl_multi_union_pw_aff_union_add_aligned
);
8150 /* Construct and return a multi union piecewise affine expression
8151 * that is equal to the given multi affine expression.
8153 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_from_multi_aff(
8154 __isl_take isl_multi_aff
*ma
)
8156 isl_multi_pw_aff
*mpa
;
8158 mpa
= isl_multi_pw_aff_from_multi_aff(ma
);
8159 return isl_multi_union_pw_aff_from_multi_pw_aff(mpa
);
8162 /* Construct and return a multi union piecewise affine expression
8163 * that is equal to the given multi piecewise affine expression.
8165 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_from_multi_pw_aff(
8166 __isl_take isl_multi_pw_aff
*mpa
)
8171 isl_multi_union_pw_aff
*mupa
;
8173 n
= isl_multi_pw_aff_dim(mpa
, isl_dim_out
);
8175 mpa
= isl_multi_pw_aff_free(mpa
);
8179 space
= isl_multi_pw_aff_get_space(mpa
);
8180 space
= isl_space_range(space
);
8181 mupa
= isl_multi_union_pw_aff_alloc(space
);
8183 for (i
= 0; i
< n
; ++i
) {
8185 isl_union_pw_aff
*upa
;
8187 pa
= isl_multi_pw_aff_get_pw_aff(mpa
, i
);
8188 upa
= isl_union_pw_aff_from_pw_aff(pa
);
8189 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
8192 isl_multi_pw_aff_free(mpa
);
8197 /* Extract the range space of "pma" and assign it to *space.
8198 * If *space has already been set (through a previous call to this function),
8199 * then check that the range space is the same.
8201 static isl_stat
extract_space(__isl_take isl_pw_multi_aff
*pma
, void *user
)
8203 isl_space
**space
= user
;
8204 isl_space
*pma_space
;
8207 pma_space
= isl_space_range(isl_pw_multi_aff_get_space(pma
));
8208 isl_pw_multi_aff_free(pma
);
8211 return isl_stat_error
;
8217 equal
= isl_space_is_equal(pma_space
, *space
);
8218 isl_space_free(pma_space
);
8221 return isl_stat_error
;
8223 isl_die(isl_space_get_ctx(*space
), isl_error_invalid
,
8224 "range spaces not the same", return isl_stat_error
);
8228 /* Construct and return a multi union piecewise affine expression
8229 * that is equal to the given union piecewise multi affine expression.
8231 * In order to be able to perform the conversion, the input
8232 * needs to be non-empty and may only involve a single range space.
8234 * If the resulting multi union piecewise affine expression has
8235 * an explicit domain, then assign it the domain of the input.
8236 * In other cases, the domain is stored in the individual elements.
8238 __isl_give isl_multi_union_pw_aff
*
8239 isl_multi_union_pw_aff_from_union_pw_multi_aff(
8240 __isl_take isl_union_pw_multi_aff
*upma
)
8242 isl_space
*space
= NULL
;
8243 isl_multi_union_pw_aff
*mupa
;
8247 n
= isl_union_pw_multi_aff_n_pw_multi_aff(upma
);
8251 isl_die(isl_union_pw_multi_aff_get_ctx(upma
), isl_error_invalid
,
8252 "cannot extract range space from empty input",
8254 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma
, &extract_space
,
8261 n
= isl_space_dim(space
, isl_dim_set
);
8263 space
= isl_space_free(space
);
8264 mupa
= isl_multi_union_pw_aff_alloc(space
);
8266 for (i
= 0; i
< n
; ++i
) {
8267 isl_union_pw_aff
*upa
;
8269 upa
= isl_union_pw_multi_aff_get_union_pw_aff(upma
, i
);
8270 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
8272 if (isl_multi_union_pw_aff_has_explicit_domain(mupa
)) {
8274 isl_union_pw_multi_aff
*copy
;
8276 copy
= isl_union_pw_multi_aff_copy(upma
);
8277 dom
= isl_union_pw_multi_aff_domain(copy
);
8278 mupa
= isl_multi_union_pw_aff_intersect_domain(mupa
, dom
);
8281 isl_union_pw_multi_aff_free(upma
);
8284 isl_space_free(space
);
8285 isl_union_pw_multi_aff_free(upma
);
8289 /* Try and create an isl_multi_union_pw_aff that is equivalent
8290 * to the given isl_union_map.
8291 * The isl_union_map is required to be single-valued in each space.
8292 * Moreover, it cannot be empty and all range spaces need to be the same.
8293 * Otherwise, an error is produced.
8295 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_from_union_map(
8296 __isl_take isl_union_map
*umap
)
8298 isl_union_pw_multi_aff
*upma
;
8300 upma
= isl_union_pw_multi_aff_from_union_map(umap
);
8301 return isl_multi_union_pw_aff_from_union_pw_multi_aff(upma
);
8304 /* Return a multiple union piecewise affine expression
8305 * that is equal to "mv" on "domain", assuming "domain" and "mv"
8306 * have been aligned.
8308 * If the resulting multi union piecewise affine expression has
8309 * an explicit domain, then assign it the input domain.
8310 * In other cases, the domain is stored in the individual elements.
8312 static __isl_give isl_multi_union_pw_aff
*
8313 isl_multi_union_pw_aff_multi_val_on_domain_aligned(
8314 __isl_take isl_union_set
*domain
, __isl_take isl_multi_val
*mv
)
8319 isl_multi_union_pw_aff
*mupa
;
8321 n
= isl_multi_val_dim(mv
, isl_dim_set
);
8322 if (!domain
|| n
< 0)
8325 space
= isl_multi_val_get_space(mv
);
8326 mupa
= isl_multi_union_pw_aff_alloc(space
);
8327 for (i
= 0; i
< n
; ++i
) {
8329 isl_union_pw_aff
*upa
;
8331 v
= isl_multi_val_get_val(mv
, i
);
8332 upa
= isl_union_pw_aff_val_on_domain(isl_union_set_copy(domain
),
8334 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
8336 if (isl_multi_union_pw_aff_has_explicit_domain(mupa
))
8337 mupa
= isl_multi_union_pw_aff_intersect_domain(mupa
,
8338 isl_union_set_copy(domain
));
8340 isl_union_set_free(domain
);
8341 isl_multi_val_free(mv
);
8344 isl_union_set_free(domain
);
8345 isl_multi_val_free(mv
);
8349 /* Return a multiple union piecewise affine expression
8350 * that is equal to "mv" on "domain".
8352 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_multi_val_on_domain(
8353 __isl_take isl_union_set
*domain
, __isl_take isl_multi_val
*mv
)
8355 isl_bool equal_params
;
8359 equal_params
= isl_space_has_equal_params(domain
->dim
, mv
->space
);
8360 if (equal_params
< 0)
8363 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(
8365 domain
= isl_union_set_align_params(domain
,
8366 isl_multi_val_get_space(mv
));
8367 mv
= isl_multi_val_align_params(mv
, isl_union_set_get_space(domain
));
8368 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(domain
, mv
);
8370 isl_union_set_free(domain
);
8371 isl_multi_val_free(mv
);
8375 /* Return a multiple union piecewise affine expression
8376 * that is equal to "ma" on "domain".
8378 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_multi_aff_on_domain(
8379 __isl_take isl_union_set
*domain
, __isl_take isl_multi_aff
*ma
)
8381 isl_pw_multi_aff
*pma
;
8383 pma
= isl_pw_multi_aff_from_multi_aff(ma
);
8384 return isl_multi_union_pw_aff_pw_multi_aff_on_domain(domain
, pma
);
8387 /* Return a multiple union piecewise affine expression
8388 * that is equal to "pma" on "domain", assuming "domain" and "pma"
8389 * have been aligned.
8391 * If the resulting multi union piecewise affine expression has
8392 * an explicit domain, then assign it the input domain.
8393 * In other cases, the domain is stored in the individual elements.
8395 static __isl_give isl_multi_union_pw_aff
*
8396 isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(
8397 __isl_take isl_union_set
*domain
, __isl_take isl_pw_multi_aff
*pma
)
8402 isl_multi_union_pw_aff
*mupa
;
8404 n
= isl_pw_multi_aff_dim(pma
, isl_dim_set
);
8405 if (!domain
|| n
< 0)
8407 space
= isl_pw_multi_aff_get_space(pma
);
8408 mupa
= isl_multi_union_pw_aff_alloc(space
);
8409 for (i
= 0; i
< n
; ++i
) {
8411 isl_union_pw_aff
*upa
;
8413 pa
= isl_pw_multi_aff_get_pw_aff(pma
, i
);
8414 upa
= isl_union_pw_aff_pw_aff_on_domain(
8415 isl_union_set_copy(domain
), pa
);
8416 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
8418 if (isl_multi_union_pw_aff_has_explicit_domain(mupa
))
8419 mupa
= isl_multi_union_pw_aff_intersect_domain(mupa
,
8420 isl_union_set_copy(domain
));
8422 isl_union_set_free(domain
);
8423 isl_pw_multi_aff_free(pma
);
8426 isl_union_set_free(domain
);
8427 isl_pw_multi_aff_free(pma
);
8431 /* Return a multiple union piecewise affine expression
8432 * that is equal to "pma" on "domain".
8434 __isl_give isl_multi_union_pw_aff
*
8435 isl_multi_union_pw_aff_pw_multi_aff_on_domain(__isl_take isl_union_set
*domain
,
8436 __isl_take isl_pw_multi_aff
*pma
)
8438 isl_bool equal_params
;
8441 space
= isl_pw_multi_aff_peek_space(pma
);
8442 equal_params
= isl_union_set_space_has_equal_params(domain
, space
);
8443 if (equal_params
< 0)
8446 return isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(
8448 domain
= isl_union_set_align_params(domain
,
8449 isl_pw_multi_aff_get_space(pma
));
8450 pma
= isl_pw_multi_aff_align_params(pma
,
8451 isl_union_set_get_space(domain
));
8452 return isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(domain
,
8455 isl_union_set_free(domain
);
8456 isl_pw_multi_aff_free(pma
);
8460 /* Return a union set containing those elements in the domains
8461 * of the elements of "mupa" where they are all zero.
8463 * If there are no elements, then simply return the entire domain.
8465 __isl_give isl_union_set
*isl_multi_union_pw_aff_zero_union_set(
8466 __isl_take isl_multi_union_pw_aff
*mupa
)
8470 isl_union_pw_aff
*upa
;
8471 isl_union_set
*zero
;
8473 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8475 mupa
= isl_multi_union_pw_aff_free(mupa
);
8480 return isl_multi_union_pw_aff_domain(mupa
);
8482 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, 0);
8483 zero
= isl_union_pw_aff_zero_union_set(upa
);
8485 for (i
= 1; i
< n
; ++i
) {
8486 isl_union_set
*zero_i
;
8488 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
8489 zero_i
= isl_union_pw_aff_zero_union_set(upa
);
8491 zero
= isl_union_set_intersect(zero
, zero_i
);
8494 isl_multi_union_pw_aff_free(mupa
);
8498 /* Construct a union map mapping the shared domain
8499 * of the union piecewise affine expressions to the range of "mupa"
8500 * in the special case of a 0D multi union piecewise affine expression.
8502 * Construct a map between the explicit domain of "mupa" and
8504 * Note that this assumes that the domain consists of explicit elements.
8506 static __isl_give isl_union_map
*isl_union_map_from_multi_union_pw_aff_0D(
8507 __isl_take isl_multi_union_pw_aff
*mupa
)
8511 isl_union_set
*dom
, *ran
;
8513 space
= isl_multi_union_pw_aff_get_space(mupa
);
8514 dom
= isl_multi_union_pw_aff_domain(mupa
);
8515 ran
= isl_union_set_from_set(isl_set_universe(space
));
8517 is_params
= isl_union_set_is_params(dom
);
8519 dom
= isl_union_set_free(dom
);
8521 isl_die(isl_union_set_get_ctx(dom
), isl_error_invalid
,
8522 "cannot create union map from expression without "
8523 "explicit domain elements",
8524 dom
= isl_union_set_free(dom
));
8526 return isl_union_map_from_domain_and_range(dom
, ran
);
8529 /* Construct a union map mapping the shared domain
8530 * of the union piecewise affine expressions to the range of "mupa"
8531 * with each dimension in the range equated to the
8532 * corresponding union piecewise affine expression.
8534 * If the input is zero-dimensional, then construct a mapping
8535 * from its explicit domain.
8537 __isl_give isl_union_map
*isl_union_map_from_multi_union_pw_aff(
8538 __isl_take isl_multi_union_pw_aff
*mupa
)
8543 isl_union_map
*umap
;
8544 isl_union_pw_aff
*upa
;
8546 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8548 mupa
= isl_multi_union_pw_aff_free(mupa
);
8553 return isl_union_map_from_multi_union_pw_aff_0D(mupa
);
8555 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, 0);
8556 umap
= isl_union_map_from_union_pw_aff(upa
);
8558 for (i
= 1; i
< n
; ++i
) {
8559 isl_union_map
*umap_i
;
8561 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
8562 umap_i
= isl_union_map_from_union_pw_aff(upa
);
8563 umap
= isl_union_map_flat_range_product(umap
, umap_i
);
8566 space
= isl_multi_union_pw_aff_get_space(mupa
);
8567 umap
= isl_union_map_reset_range_space(umap
, space
);
8569 isl_multi_union_pw_aff_free(mupa
);
8573 /* Internal data structure for isl_union_pw_multi_aff_reset_range_space.
8574 * "range" is the space from which to set the range space.
8575 * "res" collects the results.
8577 struct isl_union_pw_multi_aff_reset_range_space_data
{
8579 isl_union_pw_multi_aff
*res
;
8582 /* Replace the range space of "pma" by the range space of data->range and
8583 * add the result to data->res.
8585 static isl_stat
reset_range_space(__isl_take isl_pw_multi_aff
*pma
, void *user
)
8587 struct isl_union_pw_multi_aff_reset_range_space_data
*data
= user
;
8590 space
= isl_pw_multi_aff_get_space(pma
);
8591 space
= isl_space_domain(space
);
8592 space
= isl_space_extend_domain_with_range(space
,
8593 isl_space_copy(data
->range
));
8594 pma
= isl_pw_multi_aff_reset_space(pma
, space
);
8595 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma
);
8597 return data
->res
? isl_stat_ok
: isl_stat_error
;
8600 /* Replace the range space of all the piecewise affine expressions in "upma" by
8601 * the range space of "space".
8603 * This assumes that all these expressions have the same output dimension.
8605 * Since the spaces of the expressions change, so do their hash values.
8606 * We therefore need to create a new isl_union_pw_multi_aff.
8607 * Note that the hash value is currently computed based on the entire
8608 * space even though there can only be a single expression with a given
8611 static __isl_give isl_union_pw_multi_aff
*
8612 isl_union_pw_multi_aff_reset_range_space(
8613 __isl_take isl_union_pw_multi_aff
*upma
, __isl_take isl_space
*space
)
8615 struct isl_union_pw_multi_aff_reset_range_space_data data
= { space
};
8616 isl_space
*space_upma
;
8618 space_upma
= isl_union_pw_multi_aff_get_space(upma
);
8619 data
.res
= isl_union_pw_multi_aff_empty(space_upma
);
8620 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma
,
8621 &reset_range_space
, &data
) < 0)
8622 data
.res
= isl_union_pw_multi_aff_free(data
.res
);
8624 isl_space_free(space
);
8625 isl_union_pw_multi_aff_free(upma
);
8629 /* Construct and return a union piecewise multi affine expression
8630 * that is equal to the given multi union piecewise affine expression,
8631 * in the special case of a 0D multi union piecewise affine expression.
8633 * Construct a union piecewise multi affine expression
8634 * on top of the explicit domain of the input.
8636 __isl_give isl_union_pw_multi_aff
*
8637 isl_union_pw_multi_aff_from_multi_union_pw_aff_0D(
8638 __isl_take isl_multi_union_pw_aff
*mupa
)
8642 isl_union_set
*domain
;
8644 space
= isl_multi_union_pw_aff_get_space(mupa
);
8645 mv
= isl_multi_val_zero(space
);
8646 domain
= isl_multi_union_pw_aff_domain(mupa
);
8647 return isl_union_pw_multi_aff_multi_val_on_domain(domain
, mv
);
8650 /* Construct and return a union piecewise multi affine expression
8651 * that is equal to the given multi union piecewise affine expression.
8653 * If the input is zero-dimensional, then
8654 * construct a union piecewise multi affine expression
8655 * on top of the explicit domain of the input.
8657 __isl_give isl_union_pw_multi_aff
*
8658 isl_union_pw_multi_aff_from_multi_union_pw_aff(
8659 __isl_take isl_multi_union_pw_aff
*mupa
)
8664 isl_union_pw_multi_aff
*upma
;
8665 isl_union_pw_aff
*upa
;
8667 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8669 mupa
= isl_multi_union_pw_aff_free(mupa
);
8674 return isl_union_pw_multi_aff_from_multi_union_pw_aff_0D(mupa
);
8676 space
= isl_multi_union_pw_aff_get_space(mupa
);
8677 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, 0);
8678 upma
= isl_union_pw_multi_aff_from_union_pw_aff(upa
);
8680 for (i
= 1; i
< n
; ++i
) {
8681 isl_union_pw_multi_aff
*upma_i
;
8683 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
8684 upma_i
= isl_union_pw_multi_aff_from_union_pw_aff(upa
);
8685 upma
= isl_union_pw_multi_aff_flat_range_product(upma
, upma_i
);
8688 upma
= isl_union_pw_multi_aff_reset_range_space(upma
, space
);
8690 isl_multi_union_pw_aff_free(mupa
);
8694 /* Intersect the range of "mupa" with "range",
8695 * in the special case where "mupa" is 0D.
8697 * Intersect the domain of "mupa" with the constraints on the parameters
8700 static __isl_give isl_multi_union_pw_aff
*mupa_intersect_range_0D(
8701 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_set
*range
)
8703 range
= isl_set_params(range
);
8704 mupa
= isl_multi_union_pw_aff_intersect_params(mupa
, range
);
8708 /* Intersect the range of "mupa" with "range".
8709 * That is, keep only those domain elements that have a function value
8712 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_intersect_range(
8713 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_set
*range
)
8715 isl_union_pw_multi_aff
*upma
;
8716 isl_union_set
*domain
;
8721 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8722 if (n
< 0 || !range
)
8725 space
= isl_set_get_space(range
);
8726 match
= isl_space_tuple_is_equal(mupa
->space
, isl_dim_set
,
8727 space
, isl_dim_set
);
8728 isl_space_free(space
);
8732 isl_die(isl_multi_union_pw_aff_get_ctx(mupa
), isl_error_invalid
,
8733 "space don't match", goto error
);
8735 return mupa_intersect_range_0D(mupa
, range
);
8737 upma
= isl_union_pw_multi_aff_from_multi_union_pw_aff(
8738 isl_multi_union_pw_aff_copy(mupa
));
8739 domain
= isl_union_set_from_set(range
);
8740 domain
= isl_union_set_preimage_union_pw_multi_aff(domain
, upma
);
8741 mupa
= isl_multi_union_pw_aff_intersect_domain(mupa
, domain
);
8745 isl_multi_union_pw_aff_free(mupa
);
8746 isl_set_free(range
);
8750 /* Return the shared domain of the elements of "mupa",
8751 * in the special case where "mupa" is zero-dimensional.
8753 * Return the explicit domain of "mupa".
8754 * Note that this domain may be a parameter set, either
8755 * because "mupa" is meant to live in a set space or
8756 * because no explicit domain has been set.
8758 __isl_give isl_union_set
*isl_multi_union_pw_aff_domain_0D(
8759 __isl_take isl_multi_union_pw_aff
*mupa
)
8763 dom
= isl_multi_union_pw_aff_get_explicit_domain(mupa
);
8764 isl_multi_union_pw_aff_free(mupa
);
8769 /* Return the shared domain of the elements of "mupa".
8771 * If "mupa" is zero-dimensional, then return its explicit domain.
8773 __isl_give isl_union_set
*isl_multi_union_pw_aff_domain(
8774 __isl_take isl_multi_union_pw_aff
*mupa
)
8778 isl_union_pw_aff
*upa
;
8781 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8783 mupa
= isl_multi_union_pw_aff_free(mupa
);
8788 return isl_multi_union_pw_aff_domain_0D(mupa
);
8790 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, 0);
8791 dom
= isl_union_pw_aff_domain(upa
);
8792 for (i
= 1; i
< n
; ++i
) {
8793 isl_union_set
*dom_i
;
8795 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
8796 dom_i
= isl_union_pw_aff_domain(upa
);
8797 dom
= isl_union_set_intersect(dom
, dom_i
);
8800 isl_multi_union_pw_aff_free(mupa
);
8804 /* Apply "aff" to "mupa". The space of "mupa" is equal to the domain of "aff".
8805 * In particular, the spaces have been aligned.
8806 * The result is defined over the shared domain of the elements of "mupa"
8808 * We first extract the parametric constant part of "aff" and
8809 * define that over the shared domain.
8810 * Then we iterate over all input dimensions of "aff" and add the corresponding
8811 * multiples of the elements of "mupa".
8812 * Finally, we consider the integer divisions, calling the function
8813 * recursively to obtain an isl_union_pw_aff corresponding to the
8814 * integer division argument.
8816 static __isl_give isl_union_pw_aff
*multi_union_pw_aff_apply_aff(
8817 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_aff
*aff
)
8820 isl_size n_in
, n_div
;
8821 isl_union_pw_aff
*upa
;
8822 isl_union_set
*uset
;
8826 n_in
= isl_aff_dim(aff
, isl_dim_in
);
8827 n_div
= isl_aff_dim(aff
, isl_dim_div
);
8828 if (n_in
< 0 || n_div
< 0)
8831 uset
= isl_multi_union_pw_aff_domain(isl_multi_union_pw_aff_copy(mupa
));
8832 cst
= isl_aff_copy(aff
);
8833 cst
= isl_aff_drop_dims(cst
, isl_dim_div
, 0, n_div
);
8834 cst
= isl_aff_drop_dims(cst
, isl_dim_in
, 0, n_in
);
8835 cst
= isl_aff_project_domain_on_params(cst
);
8836 upa
= isl_union_pw_aff_aff_on_domain(uset
, cst
);
8838 for (i
= 0; i
< n_in
; ++i
) {
8839 isl_union_pw_aff
*upa_i
;
8841 if (!isl_aff_involves_dims(aff
, isl_dim_in
, i
, 1))
8843 v
= isl_aff_get_coefficient_val(aff
, isl_dim_in
, i
);
8844 upa_i
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
8845 upa_i
= isl_union_pw_aff_scale_val(upa_i
, v
);
8846 upa
= isl_union_pw_aff_add(upa
, upa_i
);
8849 for (i
= 0; i
< n_div
; ++i
) {
8851 isl_union_pw_aff
*upa_i
;
8853 if (!isl_aff_involves_dims(aff
, isl_dim_div
, i
, 1))
8855 div
= isl_aff_get_div(aff
, i
);
8856 upa_i
= multi_union_pw_aff_apply_aff(
8857 isl_multi_union_pw_aff_copy(mupa
), div
);
8858 upa_i
= isl_union_pw_aff_floor(upa_i
);
8859 v
= isl_aff_get_coefficient_val(aff
, isl_dim_div
, i
);
8860 upa_i
= isl_union_pw_aff_scale_val(upa_i
, v
);
8861 upa
= isl_union_pw_aff_add(upa
, upa_i
);
8864 isl_multi_union_pw_aff_free(mupa
);
8869 isl_multi_union_pw_aff_free(mupa
);
8874 /* Apply "aff" to "mupa". The space of "mupa" needs to be compatible
8875 * with the domain of "aff".
8876 * Furthermore, the dimension of this space needs to be greater than zero.
8877 * The result is defined over the shared domain of the elements of "mupa"
8879 * We perform these checks and then hand over control to
8880 * multi_union_pw_aff_apply_aff.
8882 __isl_give isl_union_pw_aff
*isl_multi_union_pw_aff_apply_aff(
8883 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_aff
*aff
)
8886 isl_space
*space1
, *space2
;
8889 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
8890 isl_aff_get_space(aff
));
8891 aff
= isl_aff_align_params(aff
, isl_multi_union_pw_aff_get_space(mupa
));
8895 space1
= isl_multi_union_pw_aff_get_space(mupa
);
8896 space2
= isl_aff_get_domain_space(aff
);
8897 equal
= isl_space_is_equal(space1
, space2
);
8898 isl_space_free(space1
);
8899 isl_space_free(space2
);
8903 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
8904 "spaces don't match", goto error
);
8905 dim
= isl_aff_dim(aff
, isl_dim_in
);
8909 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
8910 "cannot determine domains", goto error
);
8912 return multi_union_pw_aff_apply_aff(mupa
, aff
);
8914 isl_multi_union_pw_aff_free(mupa
);
8919 /* Apply "ma" to "mupa", in the special case where "mupa" is 0D.
8920 * The space of "mupa" is known to be compatible with the domain of "ma".
8922 * Construct an isl_multi_union_pw_aff that is equal to "ma"
8923 * on the domain of "mupa".
8925 static __isl_give isl_multi_union_pw_aff
*mupa_apply_multi_aff_0D(
8926 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_multi_aff
*ma
)
8930 dom
= isl_multi_union_pw_aff_domain(mupa
);
8931 ma
= isl_multi_aff_project_domain_on_params(ma
);
8933 return isl_multi_union_pw_aff_multi_aff_on_domain(dom
, ma
);
8936 /* Apply "ma" to "mupa". The space of "mupa" needs to be compatible
8937 * with the domain of "ma".
8938 * The result is defined over the shared domain of the elements of "mupa"
8940 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_apply_multi_aff(
8941 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_multi_aff
*ma
)
8943 isl_space
*space1
, *space2
;
8944 isl_multi_union_pw_aff
*res
;
8947 isl_size n_in
, n_out
;
8949 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
8950 isl_multi_aff_get_space(ma
));
8951 ma
= isl_multi_aff_align_params(ma
,
8952 isl_multi_union_pw_aff_get_space(mupa
));
8953 n_in
= isl_multi_aff_dim(ma
, isl_dim_in
);
8954 n_out
= isl_multi_aff_dim(ma
, isl_dim_out
);
8955 if (!mupa
|| n_in
< 0 || n_out
< 0)
8958 space1
= isl_multi_union_pw_aff_get_space(mupa
);
8959 space2
= isl_multi_aff_get_domain_space(ma
);
8960 equal
= isl_space_is_equal(space1
, space2
);
8961 isl_space_free(space1
);
8962 isl_space_free(space2
);
8966 isl_die(isl_multi_aff_get_ctx(ma
), isl_error_invalid
,
8967 "spaces don't match", goto error
);
8969 return mupa_apply_multi_aff_0D(mupa
, ma
);
8971 space1
= isl_space_range(isl_multi_aff_get_space(ma
));
8972 res
= isl_multi_union_pw_aff_alloc(space1
);
8974 for (i
= 0; i
< n_out
; ++i
) {
8976 isl_union_pw_aff
*upa
;
8978 aff
= isl_multi_aff_get_aff(ma
, i
);
8979 upa
= multi_union_pw_aff_apply_aff(
8980 isl_multi_union_pw_aff_copy(mupa
), aff
);
8981 res
= isl_multi_union_pw_aff_set_union_pw_aff(res
, i
, upa
);
8984 isl_multi_aff_free(ma
);
8985 isl_multi_union_pw_aff_free(mupa
);
8988 isl_multi_union_pw_aff_free(mupa
);
8989 isl_multi_aff_free(ma
);
8993 /* Apply "pa" to "mupa", in the special case where "mupa" is 0D.
8994 * The space of "mupa" is known to be compatible with the domain of "pa".
8996 * Construct an isl_multi_union_pw_aff that is equal to "pa"
8997 * on the domain of "mupa".
8999 static __isl_give isl_union_pw_aff
*isl_multi_union_pw_aff_apply_pw_aff_0D(
9000 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_pw_aff
*pa
)
9004 dom
= isl_multi_union_pw_aff_domain(mupa
);
9005 pa
= isl_pw_aff_project_domain_on_params(pa
);
9007 return isl_union_pw_aff_pw_aff_on_domain(dom
, pa
);
9010 /* Apply "pa" to "mupa". The space of "mupa" needs to be compatible
9011 * with the domain of "pa".
9012 * Furthermore, the dimension of this space needs to be greater than zero.
9013 * The result is defined over the shared domain of the elements of "mupa"
9015 __isl_give isl_union_pw_aff
*isl_multi_union_pw_aff_apply_pw_aff(
9016 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_pw_aff
*pa
)
9021 isl_space
*space
, *space2
;
9022 isl_union_pw_aff
*upa
;
9024 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
9025 isl_pw_aff_get_space(pa
));
9026 pa
= isl_pw_aff_align_params(pa
,
9027 isl_multi_union_pw_aff_get_space(mupa
));
9031 space
= isl_multi_union_pw_aff_get_space(mupa
);
9032 space2
= isl_pw_aff_get_domain_space(pa
);
9033 equal
= isl_space_is_equal(space
, space2
);
9034 isl_space_free(space
);
9035 isl_space_free(space2
);
9039 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
9040 "spaces don't match", goto error
);
9041 n_in
= isl_pw_aff_dim(pa
, isl_dim_in
);
9045 return isl_multi_union_pw_aff_apply_pw_aff_0D(mupa
, pa
);
9047 space
= isl_space_params(isl_multi_union_pw_aff_get_space(mupa
));
9048 upa
= isl_union_pw_aff_empty(space
);
9050 for (i
= 0; i
< pa
->n
; ++i
) {
9053 isl_multi_union_pw_aff
*mupa_i
;
9054 isl_union_pw_aff
*upa_i
;
9056 mupa_i
= isl_multi_union_pw_aff_copy(mupa
);
9057 domain
= isl_set_copy(pa
->p
[i
].set
);
9058 mupa_i
= isl_multi_union_pw_aff_intersect_range(mupa_i
, domain
);
9059 aff
= isl_aff_copy(pa
->p
[i
].aff
);
9060 upa_i
= multi_union_pw_aff_apply_aff(mupa_i
, aff
);
9061 upa
= isl_union_pw_aff_union_add(upa
, upa_i
);
9064 isl_multi_union_pw_aff_free(mupa
);
9065 isl_pw_aff_free(pa
);
9068 isl_multi_union_pw_aff_free(mupa
);
9069 isl_pw_aff_free(pa
);
9073 /* Apply "pma" to "mupa", in the special case where "mupa" is 0D.
9074 * The space of "mupa" is known to be compatible with the domain of "pma".
9076 * Construct an isl_multi_union_pw_aff that is equal to "pma"
9077 * on the domain of "mupa".
9079 static __isl_give isl_multi_union_pw_aff
*mupa_apply_pw_multi_aff_0D(
9080 __isl_take isl_multi_union_pw_aff
*mupa
,
9081 __isl_take isl_pw_multi_aff
*pma
)
9085 dom
= isl_multi_union_pw_aff_domain(mupa
);
9086 pma
= isl_pw_multi_aff_project_domain_on_params(pma
);
9088 return isl_multi_union_pw_aff_pw_multi_aff_on_domain(dom
, pma
);
9091 /* Apply "pma" to "mupa". The space of "mupa" needs to be compatible
9092 * with the domain of "pma".
9093 * The result is defined over the shared domain of the elements of "mupa"
9095 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_apply_pw_multi_aff(
9096 __isl_take isl_multi_union_pw_aff
*mupa
,
9097 __isl_take isl_pw_multi_aff
*pma
)
9099 isl_space
*space1
, *space2
;
9100 isl_multi_union_pw_aff
*res
;
9103 isl_size n_in
, n_out
;
9105 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
9106 isl_pw_multi_aff_get_space(pma
));
9107 pma
= isl_pw_multi_aff_align_params(pma
,
9108 isl_multi_union_pw_aff_get_space(mupa
));
9112 space1
= isl_multi_union_pw_aff_get_space(mupa
);
9113 space2
= isl_pw_multi_aff_get_domain_space(pma
);
9114 equal
= isl_space_is_equal(space1
, space2
);
9115 isl_space_free(space1
);
9116 isl_space_free(space2
);
9120 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
9121 "spaces don't match", goto error
);
9122 n_in
= isl_pw_multi_aff_dim(pma
, isl_dim_in
);
9123 n_out
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
9124 if (n_in
< 0 || n_out
< 0)
9127 return mupa_apply_pw_multi_aff_0D(mupa
, pma
);
9129 space1
= isl_space_range(isl_pw_multi_aff_get_space(pma
));
9130 res
= isl_multi_union_pw_aff_alloc(space1
);
9132 for (i
= 0; i
< n_out
; ++i
) {
9134 isl_union_pw_aff
*upa
;
9136 pa
= isl_pw_multi_aff_get_pw_aff(pma
, i
);
9137 upa
= isl_multi_union_pw_aff_apply_pw_aff(
9138 isl_multi_union_pw_aff_copy(mupa
), pa
);
9139 res
= isl_multi_union_pw_aff_set_union_pw_aff(res
, i
, upa
);
9142 isl_pw_multi_aff_free(pma
);
9143 isl_multi_union_pw_aff_free(mupa
);
9146 isl_multi_union_pw_aff_free(mupa
);
9147 isl_pw_multi_aff_free(pma
);
9151 /* Replace the explicit domain of "mupa" by its preimage under "upma".
9152 * If the explicit domain only keeps track of constraints on the parameters,
9153 * then only update those constraints.
9155 static __isl_give isl_multi_union_pw_aff
*preimage_explicit_domain(
9156 __isl_take isl_multi_union_pw_aff
*mupa
,
9157 __isl_keep isl_union_pw_multi_aff
*upma
)
9161 if (isl_multi_union_pw_aff_check_has_explicit_domain(mupa
) < 0)
9162 return isl_multi_union_pw_aff_free(mupa
);
9164 mupa
= isl_multi_union_pw_aff_cow(mupa
);
9168 is_params
= isl_union_set_is_params(mupa
->u
.dom
);
9170 return isl_multi_union_pw_aff_free(mupa
);
9172 upma
= isl_union_pw_multi_aff_copy(upma
);
9174 mupa
->u
.dom
= isl_union_set_intersect_params(mupa
->u
.dom
,
9175 isl_union_set_params(isl_union_pw_multi_aff_domain(upma
)));
9177 mupa
->u
.dom
= isl_union_set_preimage_union_pw_multi_aff(
9180 return isl_multi_union_pw_aff_free(mupa
);
9184 /* Compute the pullback of "mupa" by the function represented by "upma".
9185 * In other words, plug in "upma" in "mupa". The result contains
9186 * expressions defined over the domain space of "upma".
9188 * Run over all elements of "mupa" and plug in "upma" in each of them.
9190 * If "mupa" has an explicit domain, then it is this domain
9191 * that needs to undergo a pullback instead, i.e., a preimage.
9193 __isl_give isl_multi_union_pw_aff
*
9194 isl_multi_union_pw_aff_pullback_union_pw_multi_aff(
9195 __isl_take isl_multi_union_pw_aff
*mupa
,
9196 __isl_take isl_union_pw_multi_aff
*upma
)
9201 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
9202 isl_union_pw_multi_aff_get_space(upma
));
9203 upma
= isl_union_pw_multi_aff_align_params(upma
,
9204 isl_multi_union_pw_aff_get_space(mupa
));
9205 mupa
= isl_multi_union_pw_aff_cow(mupa
);
9206 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
9210 for (i
= 0; i
< n
; ++i
) {
9211 isl_union_pw_aff
*upa
;
9213 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
9214 upa
= isl_union_pw_aff_pullback_union_pw_multi_aff(upa
,
9215 isl_union_pw_multi_aff_copy(upma
));
9216 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
9219 if (isl_multi_union_pw_aff_has_explicit_domain(mupa
))
9220 mupa
= preimage_explicit_domain(mupa
, upma
);
9222 isl_union_pw_multi_aff_free(upma
);
9225 isl_multi_union_pw_aff_free(mupa
);
9226 isl_union_pw_multi_aff_free(upma
);
9230 /* Extract the sequence of elements in "mupa" with domain space "space"
9231 * (ignoring parameters).
9233 * For the elements of "mupa" that are not defined on the specified space,
9234 * the corresponding element in the result is empty.
9236 __isl_give isl_multi_pw_aff
*isl_multi_union_pw_aff_extract_multi_pw_aff(
9237 __isl_keep isl_multi_union_pw_aff
*mupa
, __isl_take isl_space
*space
)
9241 isl_space
*space_mpa
;
9242 isl_multi_pw_aff
*mpa
;
9244 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
9245 if (n
< 0 || !space
)
9248 space_mpa
= isl_multi_union_pw_aff_get_space(mupa
);
9249 space
= isl_space_replace_params(space
, space_mpa
);
9250 space_mpa
= isl_space_map_from_domain_and_range(isl_space_copy(space
),
9252 mpa
= isl_multi_pw_aff_alloc(space_mpa
);
9254 space
= isl_space_from_domain(space
);
9255 space
= isl_space_add_dims(space
, isl_dim_out
, 1);
9256 for (i
= 0; i
< n
; ++i
) {
9257 isl_union_pw_aff
*upa
;
9260 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
9261 pa
= isl_union_pw_aff_extract_pw_aff(upa
,
9262 isl_space_copy(space
));
9263 mpa
= isl_multi_pw_aff_set_pw_aff(mpa
, i
, pa
);
9264 isl_union_pw_aff_free(upa
);
9267 isl_space_free(space
);
9270 isl_space_free(space
);
9274 /* Evaluate the affine function "aff" in the void point "pnt".
9275 * In particular, return the value NaN.
9277 static __isl_give isl_val
*eval_void(__isl_take isl_aff
*aff
,
9278 __isl_take isl_point
*pnt
)
9282 ctx
= isl_point_get_ctx(pnt
);
9284 isl_point_free(pnt
);
9285 return isl_val_nan(ctx
);
9288 /* Evaluate the affine expression "aff"
9289 * in the coordinates (with denominator) "pnt".
9291 static __isl_give isl_val
*eval(__isl_keep isl_vec
*aff
,
9292 __isl_keep isl_vec
*pnt
)
9301 ctx
= isl_vec_get_ctx(aff
);
9304 isl_seq_inner_product(aff
->el
+ 1, pnt
->el
, pnt
->size
, &n
);
9305 isl_int_mul(d
, aff
->el
[0], pnt
->el
[0]);
9306 v
= isl_val_rat_from_isl_int(ctx
, n
, d
);
9307 v
= isl_val_normalize(v
);
9314 /* Check that the domain space of "aff" is equal to "space".
9316 static isl_stat
isl_aff_check_has_domain_space(__isl_keep isl_aff
*aff
,
9317 __isl_keep isl_space
*space
)
9321 ok
= isl_space_is_equal(isl_aff_peek_domain_space(aff
), space
);
9323 return isl_stat_error
;
9325 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
9326 "incompatible spaces", return isl_stat_error
);
9330 /* Evaluate the affine function "aff" in "pnt".
9332 __isl_give isl_val
*isl_aff_eval(__isl_take isl_aff
*aff
,
9333 __isl_take isl_point
*pnt
)
9337 isl_local_space
*ls
;
9339 if (isl_aff_check_has_domain_space(aff
, isl_point_peek_space(pnt
)) < 0)
9341 is_void
= isl_point_is_void(pnt
);
9345 return eval_void(aff
, pnt
);
9347 ls
= isl_aff_get_domain_local_space(aff
);
9348 pnt
= isl_local_space_lift_point(ls
, pnt
);
9350 v
= eval(aff
->v
, isl_point_peek_vec(pnt
));
9353 isl_point_free(pnt
);
9358 isl_point_free(pnt
);