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>
39 #define EL_BASE pw_aff
41 #include <isl_list_templ.c>
44 #define EL_BASE pw_multi_aff
46 #include <isl_list_templ.c>
49 #define EL_BASE union_pw_aff
51 #include <isl_list_templ.c>
54 #define EL_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_arith_templ.c>
3829 #include <isl_multi_cmp.c>
3830 #include <isl_multi_dims.c>
3831 #include <isl_multi_floor.c>
3832 #include <isl_multi_from_base_templ.c>
3833 #include <isl_multi_gist.c>
3834 #include <isl_multi_identity_templ.c>
3835 #include <isl_multi_move_dims_templ.c>
3836 #include <isl_multi_nan_templ.c>
3837 #include <isl_multi_product_templ.c>
3838 #include <isl_multi_splice_templ.c>
3839 #include <isl_multi_zero_templ.c>
3841 /* Construct an isl_multi_aff living in "space" that corresponds
3842 * to the affine transformation matrix "mat".
3844 __isl_give isl_multi_aff
*isl_multi_aff_from_aff_mat(
3845 __isl_take isl_space
*space
, __isl_take isl_mat
*mat
)
3848 isl_local_space
*ls
= NULL
;
3849 isl_multi_aff
*ma
= NULL
;
3850 isl_size n_row
, n_col
, n_out
, total
;
3856 ctx
= isl_mat_get_ctx(mat
);
3858 n_row
= isl_mat_rows(mat
);
3859 n_col
= isl_mat_cols(mat
);
3860 n_out
= isl_space_dim(space
, isl_dim_out
);
3861 total
= isl_space_dim(space
, isl_dim_all
);
3862 if (n_row
< 0 || n_col
< 0 || n_out
< 0 || total
< 0)
3865 isl_die(ctx
, isl_error_invalid
,
3866 "insufficient number of rows", goto error
);
3868 isl_die(ctx
, isl_error_invalid
,
3869 "insufficient number of columns", goto error
);
3870 if (1 + n_out
!= n_row
|| 2 + total
!= n_row
+ n_col
)
3871 isl_die(ctx
, isl_error_invalid
,
3872 "dimension mismatch", goto error
);
3874 ma
= isl_multi_aff_zero(isl_space_copy(space
));
3875 ls
= isl_local_space_from_space(isl_space_domain(space
));
3877 for (i
= 0; i
< n_row
- 1; ++i
) {
3881 v
= isl_vec_alloc(ctx
, 1 + n_col
);
3884 isl_int_set(v
->el
[0], mat
->row
[0][0]);
3885 isl_seq_cpy(v
->el
+ 1, mat
->row
[1 + i
], n_col
);
3886 v
= isl_vec_normalize(v
);
3887 aff
= isl_aff_alloc_vec(isl_local_space_copy(ls
), v
);
3888 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
3891 isl_local_space_free(ls
);
3895 isl_local_space_free(ls
);
3897 isl_multi_aff_free(ma
);
3901 /* Remove any internal structure of the domain of "ma".
3902 * If there is any such internal structure in the input,
3903 * then the name of the corresponding space is also removed.
3905 __isl_give isl_multi_aff
*isl_multi_aff_flatten_domain(
3906 __isl_take isl_multi_aff
*ma
)
3913 if (!ma
->space
->nested
[0])
3916 space
= isl_multi_aff_get_space(ma
);
3917 space
= isl_space_flatten_domain(space
);
3918 ma
= isl_multi_aff_reset_space(ma
, space
);
3923 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
3924 * of the space to its domain.
3926 __isl_give isl_multi_aff
*isl_multi_aff_domain_map(__isl_take isl_space
*space
)
3930 isl_local_space
*ls
;
3935 if (!isl_space_is_map(space
))
3936 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
3937 "not a map space", goto error
);
3939 n_in
= isl_space_dim(space
, isl_dim_in
);
3942 space
= isl_space_domain_map(space
);
3944 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
3946 isl_space_free(space
);
3950 space
= isl_space_domain(space
);
3951 ls
= isl_local_space_from_space(space
);
3952 for (i
= 0; i
< n_in
; ++i
) {
3955 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
3957 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
3959 isl_local_space_free(ls
);
3962 isl_space_free(space
);
3966 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
3967 * of the space to its range.
3969 __isl_give isl_multi_aff
*isl_multi_aff_range_map(__isl_take isl_space
*space
)
3972 isl_size n_in
, n_out
;
3973 isl_local_space
*ls
;
3978 if (!isl_space_is_map(space
))
3979 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
3980 "not a map space", goto error
);
3982 n_in
= isl_space_dim(space
, isl_dim_in
);
3983 n_out
= isl_space_dim(space
, isl_dim_out
);
3984 if (n_in
< 0 || n_out
< 0)
3986 space
= isl_space_range_map(space
);
3988 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
3990 isl_space_free(space
);
3994 space
= isl_space_domain(space
);
3995 ls
= isl_local_space_from_space(space
);
3996 for (i
= 0; i
< n_out
; ++i
) {
3999 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
4000 isl_dim_set
, n_in
+ i
);
4001 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
4003 isl_local_space_free(ls
);
4006 isl_space_free(space
);
4010 /* Given a map space, return an isl_pw_multi_aff that maps a wrapped copy
4011 * of the space to its range.
4013 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_range_map(
4014 __isl_take isl_space
*space
)
4016 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_range_map(space
));
4019 /* Given the space of a set and a range of set dimensions,
4020 * construct an isl_multi_aff that projects out those dimensions.
4022 __isl_give isl_multi_aff
*isl_multi_aff_project_out_map(
4023 __isl_take isl_space
*space
, enum isl_dim_type type
,
4024 unsigned first
, unsigned n
)
4028 isl_local_space
*ls
;
4033 if (!isl_space_is_set(space
))
4034 isl_die(isl_space_get_ctx(space
), isl_error_unsupported
,
4035 "expecting set space", goto error
);
4036 if (type
!= isl_dim_set
)
4037 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
4038 "only set dimensions can be projected out", goto error
);
4039 if (isl_space_check_range(space
, type
, first
, n
) < 0)
4042 dim
= isl_space_dim(space
, isl_dim_set
);
4046 space
= isl_space_from_domain(space
);
4047 space
= isl_space_add_dims(space
, isl_dim_out
, dim
- n
);
4050 return isl_multi_aff_alloc(space
);
4052 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
4053 space
= isl_space_domain(space
);
4054 ls
= isl_local_space_from_space(space
);
4056 for (i
= 0; i
< first
; ++i
) {
4059 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
4061 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
4064 for (i
= 0; i
< dim
- (first
+ n
); ++i
) {
4067 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
4068 isl_dim_set
, first
+ n
+ i
);
4069 ma
= isl_multi_aff_set_aff(ma
, first
+ i
, aff
);
4072 isl_local_space_free(ls
);
4075 isl_space_free(space
);
4079 /* Given the space of a set and a range of set dimensions,
4080 * construct an isl_pw_multi_aff that projects out those dimensions.
4082 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_project_out_map(
4083 __isl_take isl_space
*space
, enum isl_dim_type type
,
4084 unsigned first
, unsigned n
)
4088 ma
= isl_multi_aff_project_out_map(space
, type
, first
, n
);
4089 return isl_pw_multi_aff_from_multi_aff(ma
);
4092 /* Create an isl_pw_multi_aff with the given isl_multi_aff on a universe
4095 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_multi_aff(
4096 __isl_take isl_multi_aff
*ma
)
4098 isl_set
*dom
= isl_set_universe(isl_multi_aff_get_domain_space(ma
));
4099 return isl_pw_multi_aff_alloc(dom
, ma
);
4102 /* Create a piecewise multi-affine expression in the given space that maps each
4103 * input dimension to the corresponding output dimension.
4105 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_identity(
4106 __isl_take isl_space
*space
)
4108 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_identity(space
));
4111 /* Exploit the equalities in "eq" to simplify the affine expressions.
4113 static __isl_give isl_multi_aff
*isl_multi_aff_substitute_equalities(
4114 __isl_take isl_multi_aff
*maff
, __isl_take isl_basic_set
*eq
)
4118 maff
= isl_multi_aff_cow(maff
);
4122 for (i
= 0; i
< maff
->n
; ++i
) {
4123 maff
->u
.p
[i
] = isl_aff_substitute_equalities(maff
->u
.p
[i
],
4124 isl_basic_set_copy(eq
));
4129 isl_basic_set_free(eq
);
4132 isl_basic_set_free(eq
);
4133 isl_multi_aff_free(maff
);
4137 __isl_give isl_multi_aff
*isl_multi_aff_scale(__isl_take isl_multi_aff
*maff
,
4142 maff
= isl_multi_aff_cow(maff
);
4146 for (i
= 0; i
< maff
->n
; ++i
) {
4147 maff
->u
.p
[i
] = isl_aff_scale(maff
->u
.p
[i
], f
);
4149 return isl_multi_aff_free(maff
);
4155 __isl_give isl_multi_aff
*isl_multi_aff_add_on_domain(__isl_keep isl_set
*dom
,
4156 __isl_take isl_multi_aff
*maff1
, __isl_take isl_multi_aff
*maff2
)
4158 maff1
= isl_multi_aff_add(maff1
, maff2
);
4159 maff1
= isl_multi_aff_gist(maff1
, isl_set_copy(dom
));
4163 int isl_multi_aff_is_empty(__isl_keep isl_multi_aff
*maff
)
4171 /* Return the set of domain elements where "ma1" is lexicographically
4172 * smaller than or equal to "ma2".
4174 __isl_give isl_set
*isl_multi_aff_lex_le_set(__isl_take isl_multi_aff
*ma1
,
4175 __isl_take isl_multi_aff
*ma2
)
4177 return isl_multi_aff_lex_ge_set(ma2
, ma1
);
4180 /* Return the set of domain elements where "ma1" is lexicographically
4181 * smaller than "ma2".
4183 __isl_give isl_set
*isl_multi_aff_lex_lt_set(__isl_take isl_multi_aff
*ma1
,
4184 __isl_take isl_multi_aff
*ma2
)
4186 return isl_multi_aff_lex_gt_set(ma2
, ma1
);
4189 /* Return the set of domain elements where "ma1" and "ma2"
4192 static __isl_give isl_set
*isl_multi_aff_order_set(
4193 __isl_take isl_multi_aff
*ma1
, __isl_take isl_multi_aff
*ma2
,
4194 __isl_give isl_map
*order(__isl_take isl_space
*set_space
))
4197 isl_map
*map1
, *map2
;
4200 map1
= isl_map_from_multi_aff_internal(ma1
);
4201 map2
= isl_map_from_multi_aff_internal(ma2
);
4202 map
= isl_map_range_product(map1
, map2
);
4203 space
= isl_space_range(isl_map_get_space(map
));
4204 space
= isl_space_domain(isl_space_unwrap(space
));
4206 map
= isl_map_intersect_range(map
, isl_map_wrap(ge
));
4208 return isl_map_domain(map
);
4211 /* Return the set of domain elements where "ma1" is lexicographically
4212 * greater than or equal to "ma2".
4214 __isl_give isl_set
*isl_multi_aff_lex_ge_set(__isl_take isl_multi_aff
*ma1
,
4215 __isl_take isl_multi_aff
*ma2
)
4217 return isl_multi_aff_order_set(ma1
, ma2
, &isl_map_lex_ge
);
4220 /* Return the set of domain elements where "ma1" is lexicographically
4221 * greater than "ma2".
4223 __isl_give isl_set
*isl_multi_aff_lex_gt_set(__isl_take isl_multi_aff
*ma1
,
4224 __isl_take isl_multi_aff
*ma2
)
4226 return isl_multi_aff_order_set(ma1
, ma2
, &isl_map_lex_gt
);
4230 #define PW isl_pw_multi_aff
4232 #define EL isl_multi_aff
4234 #define EL_IS_ZERO is_empty
4238 #define IS_ZERO is_empty
4241 #undef DEFAULT_IS_ZERO
4242 #define DEFAULT_IS_ZERO 0
4246 #define NO_INSERT_DIMS
4250 #include <isl_pw_templ.c>
4251 #include <isl_pw_union_opt.c>
4256 #define BASE pw_multi_aff
4258 #include <isl_union_multi.c>
4259 #include <isl_union_neg.c>
4261 static __isl_give isl_pw_multi_aff
*pw_multi_aff_union_lexmax(
4262 __isl_take isl_pw_multi_aff
*pma1
,
4263 __isl_take isl_pw_multi_aff
*pma2
)
4265 return isl_pw_multi_aff_union_opt_cmp(pma1
, pma2
,
4266 &isl_multi_aff_lex_ge_set
);
4269 /* Given two piecewise multi affine expressions, return a piecewise
4270 * multi-affine expression defined on the union of the definition domains
4271 * of the inputs that is equal to the lexicographic maximum of the two
4272 * inputs on each cell. If only one of the two inputs is defined on
4273 * a given cell, then it is considered to be the maximum.
4275 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_union_lexmax(
4276 __isl_take isl_pw_multi_aff
*pma1
,
4277 __isl_take isl_pw_multi_aff
*pma2
)
4279 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
4280 &pw_multi_aff_union_lexmax
);
4283 static __isl_give isl_pw_multi_aff
*pw_multi_aff_union_lexmin(
4284 __isl_take isl_pw_multi_aff
*pma1
,
4285 __isl_take isl_pw_multi_aff
*pma2
)
4287 return isl_pw_multi_aff_union_opt_cmp(pma1
, pma2
,
4288 &isl_multi_aff_lex_le_set
);
4291 /* Given two piecewise multi affine expressions, return a piecewise
4292 * multi-affine expression defined on the union of the definition domains
4293 * of the inputs that is equal to the lexicographic minimum of the two
4294 * inputs on each cell. If only one of the two inputs is defined on
4295 * a given cell, then it is considered to be the minimum.
4297 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_union_lexmin(
4298 __isl_take isl_pw_multi_aff
*pma1
,
4299 __isl_take isl_pw_multi_aff
*pma2
)
4301 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
4302 &pw_multi_aff_union_lexmin
);
4305 static __isl_give isl_pw_multi_aff
*pw_multi_aff_add(
4306 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4308 return isl_pw_multi_aff_on_shared_domain(pma1
, pma2
,
4309 &isl_multi_aff_add
);
4312 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_add(
4313 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4315 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
4319 static __isl_give isl_pw_multi_aff
*pw_multi_aff_sub(
4320 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4322 return isl_pw_multi_aff_on_shared_domain(pma1
, pma2
,
4323 &isl_multi_aff_sub
);
4326 /* Subtract "pma2" from "pma1" and return the result.
4328 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_sub(
4329 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4331 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
4335 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_union_add(
4336 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4338 return isl_pw_multi_aff_union_add_(pma1
, pma2
);
4341 /* Compute the sum of "upa1" and "upa2" on the union of their domains,
4342 * with the actual sum on the shared domain and
4343 * the defined expression on the symmetric difference of the domains.
4345 __isl_give isl_union_pw_aff
*isl_union_pw_aff_union_add(
4346 __isl_take isl_union_pw_aff
*upa1
, __isl_take isl_union_pw_aff
*upa2
)
4348 return isl_union_pw_aff_union_add_(upa1
, upa2
);
4351 /* Compute the sum of "upma1" and "upma2" on the union of their domains,
4352 * with the actual sum on the shared domain and
4353 * the defined expression on the symmetric difference of the domains.
4355 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_union_add(
4356 __isl_take isl_union_pw_multi_aff
*upma1
,
4357 __isl_take isl_union_pw_multi_aff
*upma2
)
4359 return isl_union_pw_multi_aff_union_add_(upma1
, upma2
);
4362 /* Given two piecewise multi-affine expressions A -> B and C -> D,
4363 * construct a piecewise multi-affine expression [A -> C] -> [B -> D].
4365 static __isl_give isl_pw_multi_aff
*pw_multi_aff_product(
4366 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4370 isl_pw_multi_aff
*res
;
4375 n
= pma1
->n
* pma2
->n
;
4376 space
= isl_space_product(isl_space_copy(pma1
->dim
),
4377 isl_space_copy(pma2
->dim
));
4378 res
= isl_pw_multi_aff_alloc_size(space
, n
);
4380 for (i
= 0; i
< pma1
->n
; ++i
) {
4381 for (j
= 0; j
< pma2
->n
; ++j
) {
4385 domain
= isl_set_product(isl_set_copy(pma1
->p
[i
].set
),
4386 isl_set_copy(pma2
->p
[j
].set
));
4387 ma
= isl_multi_aff_product(
4388 isl_multi_aff_copy(pma1
->p
[i
].maff
),
4389 isl_multi_aff_copy(pma2
->p
[j
].maff
));
4390 res
= isl_pw_multi_aff_add_piece(res
, domain
, ma
);
4394 isl_pw_multi_aff_free(pma1
);
4395 isl_pw_multi_aff_free(pma2
);
4398 isl_pw_multi_aff_free(pma1
);
4399 isl_pw_multi_aff_free(pma2
);
4403 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_product(
4404 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4406 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
4407 &pw_multi_aff_product
);
4410 /* Subtract the initial "n" elements in "ma" with coefficients in "c" and
4411 * denominator "denom".
4412 * "denom" is allowed to be negative, in which case the actual denominator
4413 * is -denom and the expressions are added instead.
4415 static __isl_give isl_aff
*subtract_initial(__isl_take isl_aff
*aff
,
4416 __isl_keep isl_multi_aff
*ma
, int n
, isl_int
*c
, isl_int denom
)
4422 first
= isl_seq_first_non_zero(c
, n
);
4426 sign
= isl_int_sgn(denom
);
4428 isl_int_abs(d
, denom
);
4429 for (i
= first
; i
< n
; ++i
) {
4432 if (isl_int_is_zero(c
[i
]))
4434 aff_i
= isl_multi_aff_get_aff(ma
, i
);
4435 aff_i
= isl_aff_scale(aff_i
, c
[i
]);
4436 aff_i
= isl_aff_scale_down(aff_i
, d
);
4438 aff
= isl_aff_sub(aff
, aff_i
);
4440 aff
= isl_aff_add(aff
, aff_i
);
4447 /* Extract an affine expression that expresses the output dimension "pos"
4448 * of "bmap" in terms of the parameters and input dimensions from
4450 * Note that this expression may involve integer divisions defined
4451 * in terms of parameters and input dimensions.
4452 * The equality may also involve references to earlier (but not later)
4453 * output dimensions. These are replaced by the corresponding elements
4456 * If the equality is of the form
4458 * f(i) + h(j) + a x + g(i) = 0,
4460 * with f(i) a linear combinations of the parameters and input dimensions,
4461 * g(i) a linear combination of integer divisions defined in terms of the same
4462 * and h(j) a linear combinations of earlier output dimensions,
4463 * then the affine expression is
4465 * (-f(i) - g(i))/a - h(j)/a
4467 * If the equality is of the form
4469 * f(i) + h(j) - a x + g(i) = 0,
4471 * then the affine expression is
4473 * (f(i) + g(i))/a - h(j)/(-a)
4476 * If "div" refers to an integer division (i.e., it is smaller than
4477 * the number of integer divisions), then the equality constraint
4478 * does involve an integer division (the one at position "div") that
4479 * is defined in terms of output dimensions. However, this integer
4480 * division can be eliminated by exploiting a pair of constraints
4481 * x >= l and x <= l + n, with n smaller than the coefficient of "div"
4482 * in the equality constraint. "ineq" refers to inequality x >= l, i.e.,
4484 * In particular, let
4486 * x = e(i) + m floor(...)
4488 * with e(i) the expression derived above and floor(...) the integer
4489 * division involving output dimensions.
4500 * e(i) + m floor(...) - l = (e(i) + m floor(...) - l) mod m
4501 * = (e(i) - l) mod m
4505 * x - l = (e(i) - l) mod m
4509 * x = ((e(i) - l) mod m) + l
4511 * The variable "shift" below contains the expression -l, which may
4512 * also involve a linear combination of earlier output dimensions.
4514 static __isl_give isl_aff
*extract_aff_from_equality(
4515 __isl_keep isl_basic_map
*bmap
, int pos
, int eq
, int div
, int ineq
,
4516 __isl_keep isl_multi_aff
*ma
)
4519 isl_size n_div
, n_out
;
4521 isl_local_space
*ls
;
4522 isl_aff
*aff
, *shift
;
4525 ctx
= isl_basic_map_get_ctx(bmap
);
4526 ls
= isl_basic_map_get_local_space(bmap
);
4527 ls
= isl_local_space_domain(ls
);
4528 aff
= isl_aff_alloc(isl_local_space_copy(ls
));
4531 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
4532 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4533 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4534 if (n_out
< 0 || n_div
< 0)
4536 if (isl_int_is_neg(bmap
->eq
[eq
][o_out
+ pos
])) {
4537 isl_seq_cpy(aff
->v
->el
+ 1, bmap
->eq
[eq
], o_out
);
4538 isl_seq_cpy(aff
->v
->el
+ 1 + o_out
,
4539 bmap
->eq
[eq
] + o_out
+ n_out
, n_div
);
4541 isl_seq_neg(aff
->v
->el
+ 1, bmap
->eq
[eq
], o_out
);
4542 isl_seq_neg(aff
->v
->el
+ 1 + o_out
,
4543 bmap
->eq
[eq
] + o_out
+ n_out
, n_div
);
4546 isl_int_set_si(aff
->v
->el
[1 + o_out
+ div
], 0);
4547 isl_int_abs(aff
->v
->el
[0], bmap
->eq
[eq
][o_out
+ pos
]);
4548 aff
= subtract_initial(aff
, ma
, pos
, bmap
->eq
[eq
] + o_out
,
4549 bmap
->eq
[eq
][o_out
+ pos
]);
4551 shift
= isl_aff_alloc(isl_local_space_copy(ls
));
4554 isl_seq_cpy(shift
->v
->el
+ 1, bmap
->ineq
[ineq
], o_out
);
4555 isl_seq_cpy(shift
->v
->el
+ 1 + o_out
,
4556 bmap
->ineq
[ineq
] + o_out
+ n_out
, n_div
);
4557 isl_int_set_si(shift
->v
->el
[0], 1);
4558 shift
= subtract_initial(shift
, ma
, pos
,
4559 bmap
->ineq
[ineq
] + o_out
, ctx
->negone
);
4560 aff
= isl_aff_add(aff
, isl_aff_copy(shift
));
4561 mod
= isl_val_int_from_isl_int(ctx
,
4562 bmap
->eq
[eq
][o_out
+ n_out
+ div
]);
4563 mod
= isl_val_abs(mod
);
4564 aff
= isl_aff_mod_val(aff
, mod
);
4565 aff
= isl_aff_sub(aff
, shift
);
4568 isl_local_space_free(ls
);
4571 isl_local_space_free(ls
);
4576 /* Given a basic map with output dimensions defined
4577 * in terms of the parameters input dimensions and earlier
4578 * output dimensions using an equality (and possibly a pair on inequalities),
4579 * extract an isl_aff that expresses output dimension "pos" in terms
4580 * of the parameters and input dimensions.
4581 * Note that this expression may involve integer divisions defined
4582 * in terms of parameters and input dimensions.
4583 * "ma" contains the expressions corresponding to earlier output dimensions.
4585 * This function shares some similarities with
4586 * isl_basic_map_has_defining_equality and isl_constraint_get_bound.
4588 static __isl_give isl_aff
*extract_isl_aff_from_basic_map(
4589 __isl_keep isl_basic_map
*bmap
, int pos
, __isl_keep isl_multi_aff
*ma
)
4596 eq
= isl_basic_map_output_defining_equality(bmap
, pos
, &div
, &ineq
);
4597 if (eq
>= bmap
->n_eq
)
4598 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
4599 "unable to find suitable equality", return NULL
);
4600 aff
= extract_aff_from_equality(bmap
, pos
, eq
, div
, ineq
, ma
);
4602 aff
= isl_aff_remove_unused_divs(aff
);
4606 /* Given a basic map where each output dimension is defined
4607 * in terms of the parameters and input dimensions using an equality,
4608 * extract an isl_multi_aff that expresses the output dimensions in terms
4609 * of the parameters and input dimensions.
4611 static __isl_give isl_multi_aff
*extract_isl_multi_aff_from_basic_map(
4612 __isl_take isl_basic_map
*bmap
)
4621 ma
= isl_multi_aff_alloc(isl_basic_map_get_space(bmap
));
4622 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4624 ma
= isl_multi_aff_free(ma
);
4626 for (i
= 0; i
< n_out
; ++i
) {
4629 aff
= extract_isl_aff_from_basic_map(bmap
, i
, ma
);
4630 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
4633 isl_basic_map_free(bmap
);
4638 /* Given a basic set where each set dimension is defined
4639 * in terms of the parameters using an equality,
4640 * extract an isl_multi_aff that expresses the set dimensions in terms
4641 * of the parameters.
4643 __isl_give isl_multi_aff
*isl_multi_aff_from_basic_set_equalities(
4644 __isl_take isl_basic_set
*bset
)
4646 return extract_isl_multi_aff_from_basic_map(bset
);
4649 /* Create an isl_pw_multi_aff that is equivalent to
4650 * isl_map_intersect_domain(isl_map_from_basic_map(bmap), domain).
4651 * The given basic map is such that each output dimension is defined
4652 * in terms of the parameters and input dimensions using an equality.
4654 * Since some applications expect the result of isl_pw_multi_aff_from_map
4655 * to only contain integer affine expressions, we compute the floor
4656 * of the expression before returning.
4658 * Remove all constraints involving local variables without
4659 * an explicit representation (resulting in the removal of those
4660 * local variables) prior to the actual extraction to ensure
4661 * that the local spaces in which the resulting affine expressions
4662 * are created do not contain any unknown local variables.
4663 * Removing such constraints is safe because constraints involving
4664 * unknown local variables are not used to determine whether
4665 * a basic map is obviously single-valued.
4667 static __isl_give isl_pw_multi_aff
*plain_pw_multi_aff_from_map(
4668 __isl_take isl_set
*domain
, __isl_take isl_basic_map
*bmap
)
4672 bmap
= isl_basic_map_drop_constraint_involving_unknown_divs(bmap
);
4673 ma
= extract_isl_multi_aff_from_basic_map(bmap
);
4674 ma
= isl_multi_aff_floor(ma
);
4675 return isl_pw_multi_aff_alloc(domain
, ma
);
4678 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4679 * This obviously only works if the input "map" is single-valued.
4680 * If so, we compute the lexicographic minimum of the image in the form
4681 * of an isl_pw_multi_aff. Since the image is unique, it is equal
4682 * to its lexicographic minimum.
4683 * If the input is not single-valued, we produce an error.
4685 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_base(
4686 __isl_take isl_map
*map
)
4690 isl_pw_multi_aff
*pma
;
4692 sv
= isl_map_is_single_valued(map
);
4696 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
4697 "map is not single-valued", goto error
);
4698 map
= isl_map_make_disjoint(map
);
4702 pma
= isl_pw_multi_aff_empty(isl_map_get_space(map
));
4704 for (i
= 0; i
< map
->n
; ++i
) {
4705 isl_pw_multi_aff
*pma_i
;
4706 isl_basic_map
*bmap
;
4707 bmap
= isl_basic_map_copy(map
->p
[i
]);
4708 pma_i
= isl_basic_map_lexmin_pw_multi_aff(bmap
);
4709 pma
= isl_pw_multi_aff_add_disjoint(pma
, pma_i
);
4719 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
4720 * taking into account that the output dimension at position "d"
4721 * can be represented as
4723 * x = floor((e(...) + c1) / m)
4725 * given that constraint "i" is of the form
4727 * e(...) + c1 - m x >= 0
4730 * Let "map" be of the form
4734 * We construct a mapping
4736 * A -> [A -> x = floor(...)]
4738 * apply that to the map, obtaining
4740 * [A -> x = floor(...)] -> B
4742 * and equate dimension "d" to x.
4743 * We then compute a isl_pw_multi_aff representation of the resulting map
4744 * and plug in the mapping above.
4746 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_div(
4747 __isl_take isl_map
*map
, __isl_take isl_basic_map
*hull
, int d
, int i
)
4750 isl_space
*space
= NULL
;
4751 isl_local_space
*ls
;
4759 isl_pw_multi_aff
*pma
;
4762 is_set
= isl_map_is_set(map
);
4766 offset
= isl_basic_map_offset(hull
, isl_dim_out
);
4767 ctx
= isl_map_get_ctx(map
);
4768 space
= isl_space_domain(isl_map_get_space(map
));
4769 n_in
= isl_space_dim(space
, isl_dim_set
);
4770 n
= isl_space_dim(space
, isl_dim_all
);
4771 if (n_in
< 0 || n
< 0)
4774 v
= isl_vec_alloc(ctx
, 1 + 1 + n
);
4776 isl_int_neg(v
->el
[0], hull
->ineq
[i
][offset
+ d
]);
4777 isl_seq_cpy(v
->el
+ 1, hull
->ineq
[i
], 1 + n
);
4779 isl_basic_map_free(hull
);
4781 ls
= isl_local_space_from_space(isl_space_copy(space
));
4782 aff
= isl_aff_alloc_vec(ls
, v
);
4783 aff
= isl_aff_floor(aff
);
4785 isl_space_free(space
);
4786 ma
= isl_multi_aff_from_aff(aff
);
4788 ma
= isl_multi_aff_identity(isl_space_map_from_set(space
));
4789 ma
= isl_multi_aff_range_product(ma
,
4790 isl_multi_aff_from_aff(aff
));
4793 insert
= isl_map_from_multi_aff_internal(isl_multi_aff_copy(ma
));
4794 map
= isl_map_apply_domain(map
, insert
);
4795 map
= isl_map_equate(map
, isl_dim_in
, n_in
, isl_dim_out
, d
);
4796 pma
= isl_pw_multi_aff_from_map(map
);
4797 pma
= isl_pw_multi_aff_pullback_multi_aff(pma
, ma
);
4801 isl_space_free(space
);
4803 isl_basic_map_free(hull
);
4807 /* Is constraint "c" of the form
4809 * e(...) + c1 - m x >= 0
4813 * -e(...) + c2 + m x >= 0
4815 * where m > 1 and e only depends on parameters and input dimemnsions?
4817 * "offset" is the offset of the output dimensions
4818 * "pos" is the position of output dimension x.
4820 static int is_potential_div_constraint(isl_int
*c
, int offset
, int d
, int total
)
4822 if (isl_int_is_zero(c
[offset
+ d
]))
4824 if (isl_int_is_one(c
[offset
+ d
]))
4826 if (isl_int_is_negone(c
[offset
+ d
]))
4828 if (isl_seq_first_non_zero(c
+ offset
, d
) != -1)
4830 if (isl_seq_first_non_zero(c
+ offset
+ d
+ 1,
4831 total
- (offset
+ d
+ 1)) != -1)
4836 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4838 * As a special case, we first check if there is any pair of constraints,
4839 * shared by all the basic maps in "map" that force a given dimension
4840 * to be equal to the floor of some affine combination of the input dimensions.
4842 * In particular, if we can find two constraints
4844 * e(...) + c1 - m x >= 0 i.e., m x <= e(...) + c1
4848 * -e(...) + c2 + m x >= 0 i.e., m x >= e(...) - c2
4850 * where m > 1 and e only depends on parameters and input dimemnsions,
4853 * c1 + c2 < m i.e., -c2 >= c1 - (m - 1)
4855 * then we know that we can take
4857 * x = floor((e(...) + c1) / m)
4859 * without having to perform any computation.
4861 * Note that we know that
4865 * If c1 + c2 were 0, then we would have detected an equality during
4866 * simplification. If c1 + c2 were negative, then we would have detected
4869 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_check_div(
4870 __isl_take isl_map
*map
)
4878 isl_basic_map
*hull
;
4880 hull
= isl_map_unshifted_simple_hull(isl_map_copy(map
));
4881 dim
= isl_map_dim(map
, isl_dim_out
);
4882 total
= isl_basic_map_dim(hull
, isl_dim_all
);
4883 if (dim
< 0 || total
< 0)
4887 offset
= isl_basic_map_offset(hull
, isl_dim_out
);
4889 for (d
= 0; d
< dim
; ++d
) {
4890 for (i
= 0; i
< n
; ++i
) {
4891 if (!is_potential_div_constraint(hull
->ineq
[i
],
4892 offset
, d
, 1 + total
))
4894 for (j
= i
+ 1; j
< n
; ++j
) {
4895 if (!isl_seq_is_neg(hull
->ineq
[i
] + 1,
4896 hull
->ineq
[j
] + 1, total
))
4898 isl_int_add(sum
, hull
->ineq
[i
][0],
4900 if (isl_int_abs_lt(sum
,
4901 hull
->ineq
[i
][offset
+ d
]))
4908 if (isl_int_is_pos(hull
->ineq
[j
][offset
+ d
]))
4910 return pw_multi_aff_from_map_div(map
, hull
, d
, j
);
4914 isl_basic_map_free(hull
);
4915 return pw_multi_aff_from_map_base(map
);
4918 isl_basic_map_free(hull
);
4922 /* Given an affine expression
4924 * [A -> B] -> f(A,B)
4926 * construct an isl_multi_aff
4930 * such that dimension "d" in B' is set to "aff" and the remaining
4931 * dimensions are set equal to the corresponding dimensions in B.
4932 * "n_in" is the dimension of the space A.
4933 * "n_out" is the dimension of the space B.
4935 * If "is_set" is set, then the affine expression is of the form
4939 * and we construct an isl_multi_aff
4943 static __isl_give isl_multi_aff
*range_map(__isl_take isl_aff
*aff
, int d
,
4944 unsigned n_in
, unsigned n_out
, int is_set
)
4948 isl_space
*space
, *space2
;
4949 isl_local_space
*ls
;
4951 space
= isl_aff_get_domain_space(aff
);
4952 ls
= isl_local_space_from_space(isl_space_copy(space
));
4953 space2
= isl_space_copy(space
);
4955 space2
= isl_space_range(isl_space_unwrap(space2
));
4956 space
= isl_space_map_from_domain_and_range(space
, space2
);
4957 ma
= isl_multi_aff_alloc(space
);
4958 ma
= isl_multi_aff_set_aff(ma
, d
, aff
);
4960 for (i
= 0; i
< n_out
; ++i
) {
4963 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
4964 isl_dim_set
, n_in
+ i
);
4965 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
4968 isl_local_space_free(ls
);
4973 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
4974 * taking into account that the dimension at position "d" can be written as
4976 * x = m a + f(..) (1)
4978 * where m is equal to "gcd".
4979 * "i" is the index of the equality in "hull" that defines f(..).
4980 * In particular, the equality is of the form
4982 * f(..) - x + m g(existentials) = 0
4986 * -f(..) + x + m g(existentials) = 0
4988 * We basically plug (1) into "map", resulting in a map with "a"
4989 * in the range instead of "x". The corresponding isl_pw_multi_aff
4990 * defining "a" is then plugged back into (1) to obtain a definition for "x".
4992 * Specifically, given the input map
4996 * We first wrap it into a set
5000 * and define (1) on top of the corresponding space, resulting in "aff".
5001 * We use this to create an isl_multi_aff that maps the output position "d"
5002 * from "a" to "x", leaving all other (intput and output) dimensions unchanged.
5003 * We plug this into the wrapped map, unwrap the result and compute the
5004 * corresponding isl_pw_multi_aff.
5005 * The result is an expression
5013 * so that we can plug that into "aff", after extending the latter to
5019 * If "map" is actually a set, then there is no "A" space, meaning
5020 * that we do not need to perform any wrapping, and that the result
5021 * of the recursive call is of the form
5025 * which is plugged into a mapping of the form
5029 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_stride(
5030 __isl_take isl_map
*map
, __isl_take isl_basic_map
*hull
, int d
, int i
,
5035 isl_local_space
*ls
;
5038 isl_pw_multi_aff
*pma
, *id
;
5044 is_set
= isl_map_is_set(map
);
5048 n_in
= isl_basic_map_dim(hull
, isl_dim_in
);
5049 n_out
= isl_basic_map_dim(hull
, isl_dim_out
);
5050 if (n_in
< 0 || n_out
< 0)
5052 o_out
= isl_basic_map_offset(hull
, isl_dim_out
);
5057 set
= isl_map_wrap(map
);
5058 space
= isl_space_map_from_set(isl_set_get_space(set
));
5059 ma
= isl_multi_aff_identity(space
);
5060 ls
= isl_local_space_from_space(isl_set_get_space(set
));
5061 aff
= isl_aff_alloc(ls
);
5063 isl_int_set_si(aff
->v
->el
[0], 1);
5064 if (isl_int_is_one(hull
->eq
[i
][o_out
+ d
]))
5065 isl_seq_neg(aff
->v
->el
+ 1, hull
->eq
[i
],
5068 isl_seq_cpy(aff
->v
->el
+ 1, hull
->eq
[i
],
5070 isl_int_set(aff
->v
->el
[1 + o_out
+ d
], gcd
);
5072 ma
= isl_multi_aff_set_aff(ma
, n_in
+ d
, isl_aff_copy(aff
));
5073 set
= isl_set_preimage_multi_aff(set
, ma
);
5075 ma
= range_map(aff
, d
, n_in
, n_out
, is_set
);
5080 map
= isl_set_unwrap(set
);
5081 pma
= isl_pw_multi_aff_from_map(map
);
5084 space
= isl_pw_multi_aff_get_domain_space(pma
);
5085 space
= isl_space_map_from_set(space
);
5086 id
= isl_pw_multi_aff_identity(space
);
5087 pma
= isl_pw_multi_aff_range_product(id
, pma
);
5089 id
= isl_pw_multi_aff_from_multi_aff(ma
);
5090 pma
= isl_pw_multi_aff_pullback_pw_multi_aff(id
, pma
);
5092 isl_basic_map_free(hull
);
5096 isl_basic_map_free(hull
);
5100 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5101 * "hull" contains the equalities valid for "map".
5103 * Check if any of the output dimensions is "strided".
5104 * That is, we check if it can be written as
5108 * with m greater than 1, a some combination of existentially quantified
5109 * variables and f an expression in the parameters and input dimensions.
5110 * If so, we remove the stride in pw_multi_aff_from_map_stride.
5112 * Otherwise, we continue with pw_multi_aff_from_map_check_div for a further
5115 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_check_strides(
5116 __isl_take isl_map
*map
, __isl_take isl_basic_map
*hull
)
5125 n_div
= isl_basic_map_dim(hull
, isl_dim_div
);
5126 n_out
= isl_basic_map_dim(hull
, isl_dim_out
);
5127 if (n_div
< 0 || n_out
< 0)
5131 isl_basic_map_free(hull
);
5132 return pw_multi_aff_from_map_check_div(map
);
5137 o_div
= isl_basic_map_offset(hull
, isl_dim_div
);
5138 o_out
= isl_basic_map_offset(hull
, isl_dim_out
);
5140 for (i
= 0; i
< n_out
; ++i
) {
5141 for (j
= 0; j
< hull
->n_eq
; ++j
) {
5142 isl_int
*eq
= hull
->eq
[j
];
5143 isl_pw_multi_aff
*res
;
5145 if (!isl_int_is_one(eq
[o_out
+ i
]) &&
5146 !isl_int_is_negone(eq
[o_out
+ i
]))
5148 if (isl_seq_first_non_zero(eq
+ o_out
, i
) != -1)
5150 if (isl_seq_first_non_zero(eq
+ o_out
+ i
+ 1,
5151 n_out
- (i
+ 1)) != -1)
5153 isl_seq_gcd(eq
+ o_div
, n_div
, &gcd
);
5154 if (isl_int_is_zero(gcd
))
5156 if (isl_int_is_one(gcd
))
5159 res
= pw_multi_aff_from_map_stride(map
, hull
,
5167 isl_basic_map_free(hull
);
5168 return pw_multi_aff_from_map_check_div(map
);
5171 isl_basic_map_free(hull
);
5175 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5177 * As a special case, we first check if all output dimensions are uniquely
5178 * defined in terms of the parameters and input dimensions over the entire
5179 * domain. If so, we extract the desired isl_pw_multi_aff directly
5180 * from the affine hull of "map" and its domain.
5182 * Otherwise, continue with pw_multi_aff_from_map_check_strides for more
5185 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_map(__isl_take isl_map
*map
)
5189 isl_basic_map
*hull
;
5191 n
= isl_map_n_basic_map(map
);
5196 hull
= isl_map_unshifted_simple_hull(isl_map_copy(map
));
5197 hull
= isl_basic_map_plain_affine_hull(hull
);
5198 sv
= isl_basic_map_plain_is_single_valued(hull
);
5200 return plain_pw_multi_aff_from_map(isl_map_domain(map
),
5202 isl_basic_map_free(hull
);
5204 map
= isl_map_detect_equalities(map
);
5205 hull
= isl_map_unshifted_simple_hull(isl_map_copy(map
));
5206 sv
= isl_basic_map_plain_is_single_valued(hull
);
5208 return plain_pw_multi_aff_from_map(isl_map_domain(map
), hull
);
5210 return pw_multi_aff_from_map_check_strides(map
, hull
);
5211 isl_basic_map_free(hull
);
5217 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_set(__isl_take isl_set
*set
)
5219 return isl_pw_multi_aff_from_map(set
);
5222 /* Convert "map" into an isl_pw_multi_aff (if possible) and
5225 static isl_stat
pw_multi_aff_from_map(__isl_take isl_map
*map
, void *user
)
5227 isl_union_pw_multi_aff
**upma
= user
;
5228 isl_pw_multi_aff
*pma
;
5230 pma
= isl_pw_multi_aff_from_map(map
);
5231 *upma
= isl_union_pw_multi_aff_add_pw_multi_aff(*upma
, pma
);
5233 return *upma
? isl_stat_ok
: isl_stat_error
;
5236 /* Create an isl_union_pw_multi_aff with the given isl_aff on a universe
5239 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_aff(
5240 __isl_take isl_aff
*aff
)
5243 isl_pw_multi_aff
*pma
;
5245 ma
= isl_multi_aff_from_aff(aff
);
5246 pma
= isl_pw_multi_aff_from_multi_aff(ma
);
5247 return isl_union_pw_multi_aff_from_pw_multi_aff(pma
);
5250 /* Try and create an isl_union_pw_multi_aff that is equivalent
5251 * to the given isl_union_map.
5252 * The isl_union_map is required to be single-valued in each space.
5253 * Otherwise, an error is produced.
5255 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_union_map(
5256 __isl_take isl_union_map
*umap
)
5259 isl_union_pw_multi_aff
*upma
;
5261 space
= isl_union_map_get_space(umap
);
5262 upma
= isl_union_pw_multi_aff_empty(space
);
5263 if (isl_union_map_foreach_map(umap
, &pw_multi_aff_from_map
, &upma
) < 0)
5264 upma
= isl_union_pw_multi_aff_free(upma
);
5265 isl_union_map_free(umap
);
5270 /* Try and create an isl_union_pw_multi_aff that is equivalent
5271 * to the given isl_union_set.
5272 * The isl_union_set is required to be a singleton in each space.
5273 * Otherwise, an error is produced.
5275 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_union_set(
5276 __isl_take isl_union_set
*uset
)
5278 return isl_union_pw_multi_aff_from_union_map(uset
);
5281 /* Return the piecewise affine expression "set ? 1 : 0".
5283 __isl_give isl_pw_aff
*isl_set_indicator_function(__isl_take isl_set
*set
)
5286 isl_space
*space
= isl_set_get_space(set
);
5287 isl_local_space
*ls
= isl_local_space_from_space(space
);
5288 isl_aff
*zero
= isl_aff_zero_on_domain(isl_local_space_copy(ls
));
5289 isl_aff
*one
= isl_aff_zero_on_domain(ls
);
5291 one
= isl_aff_add_constant_si(one
, 1);
5292 pa
= isl_pw_aff_alloc(isl_set_copy(set
), one
);
5293 set
= isl_set_complement(set
);
5294 pa
= isl_pw_aff_add_disjoint(pa
, isl_pw_aff_alloc(set
, zero
));
5299 /* Plug in "subs" for dimension "type", "pos" of "aff".
5301 * Let i be the dimension to replace and let "subs" be of the form
5305 * and "aff" of the form
5311 * (a f + d g')/(m d)
5313 * where g' is the result of plugging in "subs" in each of the integer
5316 __isl_give isl_aff
*isl_aff_substitute(__isl_take isl_aff
*aff
,
5317 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
5323 aff
= isl_aff_cow(aff
);
5325 return isl_aff_free(aff
);
5327 ctx
= isl_aff_get_ctx(aff
);
5328 if (!isl_space_is_equal(aff
->ls
->dim
, subs
->ls
->dim
))
5329 isl_die(ctx
, isl_error_invalid
,
5330 "spaces don't match", return isl_aff_free(aff
));
5331 n_div
= isl_local_space_dim(subs
->ls
, isl_dim_div
);
5333 return isl_aff_free(aff
);
5335 isl_die(ctx
, isl_error_unsupported
,
5336 "cannot handle divs yet", return isl_aff_free(aff
));
5338 aff
->ls
= isl_local_space_substitute(aff
->ls
, type
, pos
, subs
);
5340 return isl_aff_free(aff
);
5342 aff
->v
= isl_vec_cow(aff
->v
);
5344 return isl_aff_free(aff
);
5346 pos
+= isl_local_space_offset(aff
->ls
, type
);
5349 isl_seq_substitute(aff
->v
->el
, pos
, subs
->v
->el
,
5350 aff
->v
->size
, subs
->v
->size
, v
);
5356 /* Plug in "subs" for dimension "type", "pos" in each of the affine
5357 * expressions in "maff".
5359 __isl_give isl_multi_aff
*isl_multi_aff_substitute(
5360 __isl_take isl_multi_aff
*maff
, enum isl_dim_type type
, unsigned pos
,
5361 __isl_keep isl_aff
*subs
)
5365 maff
= isl_multi_aff_cow(maff
);
5367 return isl_multi_aff_free(maff
);
5369 if (type
== isl_dim_in
)
5372 for (i
= 0; i
< maff
->n
; ++i
) {
5373 maff
->u
.p
[i
] = isl_aff_substitute(maff
->u
.p
[i
],
5376 return isl_multi_aff_free(maff
);
5382 /* Plug in "subs" for dimension "type", "pos" of "pma".
5384 * pma is of the form
5388 * while subs is of the form
5390 * v' = B_j(v) -> S_j
5392 * Each pair i,j such that C_ij = A_i \cap B_i is non-empty
5393 * has a contribution in the result, in particular
5395 * C_ij(S_j) -> M_i(S_j)
5397 * Note that plugging in S_j in C_ij may also result in an empty set
5398 * and this contribution should simply be discarded.
5400 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_substitute(
5401 __isl_take isl_pw_multi_aff
*pma
, enum isl_dim_type type
, unsigned pos
,
5402 __isl_keep isl_pw_aff
*subs
)
5405 isl_pw_multi_aff
*res
;
5408 return isl_pw_multi_aff_free(pma
);
5410 n
= pma
->n
* subs
->n
;
5411 res
= isl_pw_multi_aff_alloc_size(isl_space_copy(pma
->dim
), n
);
5413 for (i
= 0; i
< pma
->n
; ++i
) {
5414 for (j
= 0; j
< subs
->n
; ++j
) {
5416 isl_multi_aff
*res_ij
;
5419 common
= isl_set_intersect(
5420 isl_set_copy(pma
->p
[i
].set
),
5421 isl_set_copy(subs
->p
[j
].set
));
5422 common
= isl_set_substitute(common
,
5423 type
, pos
, subs
->p
[j
].aff
);
5424 empty
= isl_set_plain_is_empty(common
);
5425 if (empty
< 0 || empty
) {
5426 isl_set_free(common
);
5432 res_ij
= isl_multi_aff_substitute(
5433 isl_multi_aff_copy(pma
->p
[i
].maff
),
5434 type
, pos
, subs
->p
[j
].aff
);
5436 res
= isl_pw_multi_aff_add_piece(res
, common
, res_ij
);
5440 isl_pw_multi_aff_free(pma
);
5443 isl_pw_multi_aff_free(pma
);
5444 isl_pw_multi_aff_free(res
);
5448 /* Compute the preimage of a range of dimensions in the affine expression "src"
5449 * under "ma" and put the result in "dst". The number of dimensions in "src"
5450 * that precede the range is given by "n_before". The number of dimensions
5451 * in the range is given by the number of output dimensions of "ma".
5452 * The number of dimensions that follow the range is given by "n_after".
5453 * If "has_denom" is set (to one),
5454 * then "src" and "dst" have an extra initial denominator.
5455 * "n_div_ma" is the number of existentials in "ma"
5456 * "n_div_bset" is the number of existentials in "src"
5457 * The resulting "dst" (which is assumed to have been allocated by
5458 * the caller) contains coefficients for both sets of existentials,
5459 * first those in "ma" and then those in "src".
5460 * f, c1, c2 and g are temporary objects that have been initialized
5463 * Let src represent the expression
5465 * (a(p) + f_u u + b v + f_w w + c(divs))/d
5467 * and let ma represent the expressions
5469 * v_i = (r_i(p) + s_i(y) + t_i(divs'))/m_i
5471 * We start out with the following expression for dst:
5473 * (a(p) + f_u u + 0 y + f_w w + 0 divs' + c(divs) + f \sum_i b_i v_i)/d
5475 * with the multiplication factor f initially equal to 1
5476 * and f \sum_i b_i v_i kept separately.
5477 * For each x_i that we substitute, we multiply the numerator
5478 * (and denominator) of dst by c_1 = m_i and add the numerator
5479 * of the x_i expression multiplied by c_2 = f b_i,
5480 * after removing the common factors of c_1 and c_2.
5481 * The multiplication factor f also needs to be multiplied by c_1
5482 * for the next x_j, j > i.
5484 isl_stat
isl_seq_preimage(isl_int
*dst
, isl_int
*src
,
5485 __isl_keep isl_multi_aff
*ma
, int n_before
, int n_after
,
5486 int n_div_ma
, int n_div_bmap
,
5487 isl_int f
, isl_int c1
, isl_int c2
, isl_int g
, int has_denom
)
5490 isl_size n_param
, n_in
, n_out
;
5493 n_param
= isl_multi_aff_dim(ma
, isl_dim_param
);
5494 n_in
= isl_multi_aff_dim(ma
, isl_dim_in
);
5495 n_out
= isl_multi_aff_dim(ma
, isl_dim_out
);
5496 if (n_param
< 0 || n_in
< 0 || n_out
< 0)
5497 return isl_stat_error
;
5499 isl_seq_cpy(dst
, src
, has_denom
+ 1 + n_param
+ n_before
);
5500 o_dst
= o_src
= has_denom
+ 1 + n_param
+ n_before
;
5501 isl_seq_clr(dst
+ o_dst
, n_in
);
5504 isl_seq_cpy(dst
+ o_dst
, src
+ o_src
, n_after
);
5507 isl_seq_clr(dst
+ o_dst
, n_div_ma
);
5509 isl_seq_cpy(dst
+ o_dst
, src
+ o_src
, n_div_bmap
);
5511 isl_int_set_si(f
, 1);
5513 for (i
= 0; i
< n_out
; ++i
) {
5514 int offset
= has_denom
+ 1 + n_param
+ n_before
+ i
;
5516 if (isl_int_is_zero(src
[offset
]))
5518 isl_int_set(c1
, ma
->u
.p
[i
]->v
->el
[0]);
5519 isl_int_mul(c2
, f
, src
[offset
]);
5520 isl_int_gcd(g
, c1
, c2
);
5521 isl_int_divexact(c1
, c1
, g
);
5522 isl_int_divexact(c2
, c2
, g
);
5524 isl_int_mul(f
, f
, c1
);
5527 isl_seq_combine(dst
+ o_dst
, c1
, dst
+ o_dst
,
5528 c2
, ma
->u
.p
[i
]->v
->el
+ o_src
, 1 + n_param
);
5529 o_dst
+= 1 + n_param
;
5530 o_src
+= 1 + n_param
;
5531 isl_seq_scale(dst
+ o_dst
, dst
+ o_dst
, c1
, n_before
);
5533 isl_seq_combine(dst
+ o_dst
, c1
, dst
+ o_dst
,
5534 c2
, ma
->u
.p
[i
]->v
->el
+ o_src
, n_in
);
5537 isl_seq_scale(dst
+ o_dst
, dst
+ o_dst
, c1
, n_after
);
5539 isl_seq_combine(dst
+ o_dst
, c1
, dst
+ o_dst
,
5540 c2
, ma
->u
.p
[i
]->v
->el
+ o_src
, n_div_ma
);
5543 isl_seq_scale(dst
+ o_dst
, dst
+ o_dst
, c1
, n_div_bmap
);
5545 isl_int_mul(dst
[0], dst
[0], c1
);
5551 /* Compute the pullback of "aff" by the function represented by "ma".
5552 * In other words, plug in "ma" in "aff". The result is an affine expression
5553 * defined over the domain space of "ma".
5555 * If "aff" is represented by
5557 * (a(p) + b x + c(divs))/d
5559 * and ma is represented by
5561 * x = D(p) + F(y) + G(divs')
5563 * then the result is
5565 * (a(p) + b D(p) + b F(y) + b G(divs') + c(divs))/d
5567 * The divs in the local space of the input are similarly adjusted
5568 * through a call to isl_local_space_preimage_multi_aff.
5570 __isl_give isl_aff
*isl_aff_pullback_multi_aff(__isl_take isl_aff
*aff
,
5571 __isl_take isl_multi_aff
*ma
)
5573 isl_aff
*res
= NULL
;
5574 isl_local_space
*ls
;
5575 isl_size n_div_aff
, n_div_ma
;
5576 isl_int f
, c1
, c2
, g
;
5578 ma
= isl_multi_aff_align_divs(ma
);
5582 n_div_aff
= isl_aff_dim(aff
, isl_dim_div
);
5583 n_div_ma
= ma
->n
? isl_aff_dim(ma
->u
.p
[0], isl_dim_div
) : 0;
5584 if (n_div_aff
< 0 || n_div_ma
< 0)
5587 ls
= isl_aff_get_domain_local_space(aff
);
5588 ls
= isl_local_space_preimage_multi_aff(ls
, isl_multi_aff_copy(ma
));
5589 res
= isl_aff_alloc(ls
);
5598 if (isl_seq_preimage(res
->v
->el
, aff
->v
->el
, ma
, 0, 0,
5599 n_div_ma
, n_div_aff
, f
, c1
, c2
, g
, 1) < 0)
5600 res
= isl_aff_free(res
);
5608 isl_multi_aff_free(ma
);
5609 res
= isl_aff_normalize(res
);
5613 isl_multi_aff_free(ma
);
5618 /* Compute the pullback of "aff1" by the function represented by "aff2".
5619 * In other words, plug in "aff2" in "aff1". The result is an affine expression
5620 * defined over the domain space of "aff1".
5622 * The domain of "aff1" should match the range of "aff2", which means
5623 * that it should be single-dimensional.
5625 __isl_give isl_aff
*isl_aff_pullback_aff(__isl_take isl_aff
*aff1
,
5626 __isl_take isl_aff
*aff2
)
5630 ma
= isl_multi_aff_from_aff(aff2
);
5631 return isl_aff_pullback_multi_aff(aff1
, ma
);
5634 /* Compute the pullback of "ma1" by the function represented by "ma2".
5635 * In other words, plug in "ma2" in "ma1".
5637 * The parameters of "ma1" and "ma2" are assumed to have been aligned.
5639 static __isl_give isl_multi_aff
*isl_multi_aff_pullback_multi_aff_aligned(
5640 __isl_take isl_multi_aff
*ma1
, __isl_take isl_multi_aff
*ma2
)
5643 isl_space
*space
= NULL
;
5645 ma2
= isl_multi_aff_align_divs(ma2
);
5646 ma1
= isl_multi_aff_cow(ma1
);
5650 space
= isl_space_join(isl_multi_aff_get_space(ma2
),
5651 isl_multi_aff_get_space(ma1
));
5653 for (i
= 0; i
< ma1
->n
; ++i
) {
5654 ma1
->u
.p
[i
] = isl_aff_pullback_multi_aff(ma1
->u
.p
[i
],
5655 isl_multi_aff_copy(ma2
));
5660 ma1
= isl_multi_aff_reset_space(ma1
, space
);
5661 isl_multi_aff_free(ma2
);
5664 isl_space_free(space
);
5665 isl_multi_aff_free(ma2
);
5666 isl_multi_aff_free(ma1
);
5670 /* Compute the pullback of "ma1" by the function represented by "ma2".
5671 * In other words, plug in "ma2" in "ma1".
5673 __isl_give isl_multi_aff
*isl_multi_aff_pullback_multi_aff(
5674 __isl_take isl_multi_aff
*ma1
, __isl_take isl_multi_aff
*ma2
)
5676 return isl_multi_aff_align_params_multi_multi_and(ma1
, ma2
,
5677 &isl_multi_aff_pullback_multi_aff_aligned
);
5680 /* Extend the local space of "dst" to include the divs
5681 * in the local space of "src".
5683 * If "src" does not have any divs or if the local spaces of "dst" and
5684 * "src" are the same, then no extension is required.
5686 __isl_give isl_aff
*isl_aff_align_divs(__isl_take isl_aff
*dst
,
5687 __isl_keep isl_aff
*src
)
5690 isl_size src_n_div
, dst_n_div
;
5697 return isl_aff_free(dst
);
5699 ctx
= isl_aff_get_ctx(src
);
5700 equal
= isl_local_space_has_equal_space(src
->ls
, dst
->ls
);
5702 return isl_aff_free(dst
);
5704 isl_die(ctx
, isl_error_invalid
,
5705 "spaces don't match", goto error
);
5707 src_n_div
= isl_local_space_dim(src
->ls
, isl_dim_div
);
5708 dst_n_div
= isl_local_space_dim(dst
->ls
, isl_dim_div
);
5711 equal
= isl_local_space_is_equal(src
->ls
, dst
->ls
);
5712 if (equal
< 0 || src_n_div
< 0 || dst_n_div
< 0)
5713 return isl_aff_free(dst
);
5717 exp1
= isl_alloc_array(ctx
, int, src_n_div
);
5718 exp2
= isl_alloc_array(ctx
, int, dst_n_div
);
5719 if (!exp1
|| (dst_n_div
&& !exp2
))
5722 div
= isl_merge_divs(src
->ls
->div
, dst
->ls
->div
, exp1
, exp2
);
5723 dst
= isl_aff_expand_divs(dst
, div
, exp2
);
5731 return isl_aff_free(dst
);
5734 /* Adjust the local spaces of the affine expressions in "maff"
5735 * such that they all have the save divs.
5737 __isl_give isl_multi_aff
*isl_multi_aff_align_divs(
5738 __isl_take isl_multi_aff
*maff
)
5746 maff
= isl_multi_aff_cow(maff
);
5750 for (i
= 1; i
< maff
->n
; ++i
)
5751 maff
->u
.p
[0] = isl_aff_align_divs(maff
->u
.p
[0], maff
->u
.p
[i
]);
5752 for (i
= 1; i
< maff
->n
; ++i
) {
5753 maff
->u
.p
[i
] = isl_aff_align_divs(maff
->u
.p
[i
], maff
->u
.p
[0]);
5755 return isl_multi_aff_free(maff
);
5761 __isl_give isl_aff
*isl_aff_lift(__isl_take isl_aff
*aff
)
5763 aff
= isl_aff_cow(aff
);
5767 aff
->ls
= isl_local_space_lift(aff
->ls
);
5769 return isl_aff_free(aff
);
5774 /* Lift "maff" to a space with extra dimensions such that the result
5775 * has no more existentially quantified variables.
5776 * If "ls" is not NULL, then *ls is assigned the local space that lies
5777 * at the basis of the lifting applied to "maff".
5779 __isl_give isl_multi_aff
*isl_multi_aff_lift(__isl_take isl_multi_aff
*maff
,
5780 __isl_give isl_local_space
**ls
)
5794 isl_space
*space
= isl_multi_aff_get_domain_space(maff
);
5795 *ls
= isl_local_space_from_space(space
);
5797 return isl_multi_aff_free(maff
);
5802 maff
= isl_multi_aff_cow(maff
);
5803 maff
= isl_multi_aff_align_divs(maff
);
5807 n_div
= isl_aff_dim(maff
->u
.p
[0], isl_dim_div
);
5809 return isl_multi_aff_free(maff
);
5810 space
= isl_multi_aff_get_space(maff
);
5811 space
= isl_space_lift(isl_space_domain(space
), n_div
);
5812 space
= isl_space_extend_domain_with_range(space
,
5813 isl_multi_aff_get_space(maff
));
5815 return isl_multi_aff_free(maff
);
5816 isl_space_free(maff
->space
);
5817 maff
->space
= space
;
5820 *ls
= isl_aff_get_domain_local_space(maff
->u
.p
[0]);
5822 return isl_multi_aff_free(maff
);
5825 for (i
= 0; i
< maff
->n
; ++i
) {
5826 maff
->u
.p
[i
] = isl_aff_lift(maff
->u
.p
[i
]);
5834 isl_local_space_free(*ls
);
5835 return isl_multi_aff_free(maff
);
5839 #define TYPE isl_pw_multi_aff
5841 #include "check_type_range_templ.c"
5843 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma".
5845 __isl_give isl_pw_aff
*isl_pw_multi_aff_get_pw_aff(
5846 __isl_keep isl_pw_multi_aff
*pma
, int pos
)
5853 if (isl_pw_multi_aff_check_range(pma
, isl_dim_out
, pos
, 1) < 0)
5856 n_out
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
5860 space
= isl_pw_multi_aff_get_space(pma
);
5861 space
= isl_space_drop_dims(space
, isl_dim_out
,
5862 pos
+ 1, n_out
- pos
- 1);
5863 space
= isl_space_drop_dims(space
, isl_dim_out
, 0, pos
);
5865 pa
= isl_pw_aff_alloc_size(space
, pma
->n
);
5866 for (i
= 0; i
< pma
->n
; ++i
) {
5868 aff
= isl_multi_aff_get_aff(pma
->p
[i
].maff
, pos
);
5869 pa
= isl_pw_aff_add_piece(pa
, isl_set_copy(pma
->p
[i
].set
), aff
);
5875 /* Return an isl_pw_multi_aff with the given "set" as domain and
5876 * an unnamed zero-dimensional range.
5878 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_domain(
5879 __isl_take isl_set
*set
)
5884 space
= isl_set_get_space(set
);
5885 space
= isl_space_from_domain(space
);
5886 ma
= isl_multi_aff_zero(space
);
5887 return isl_pw_multi_aff_alloc(set
, ma
);
5890 /* Add an isl_pw_multi_aff with the given "set" as domain and
5891 * an unnamed zero-dimensional range to *user.
5893 static isl_stat
add_pw_multi_aff_from_domain(__isl_take isl_set
*set
,
5896 isl_union_pw_multi_aff
**upma
= user
;
5897 isl_pw_multi_aff
*pma
;
5899 pma
= isl_pw_multi_aff_from_domain(set
);
5900 *upma
= isl_union_pw_multi_aff_add_pw_multi_aff(*upma
, pma
);
5905 /* Return an isl_union_pw_multi_aff with the given "uset" as domain and
5906 * an unnamed zero-dimensional range.
5908 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_domain(
5909 __isl_take isl_union_set
*uset
)
5912 isl_union_pw_multi_aff
*upma
;
5917 space
= isl_union_set_get_space(uset
);
5918 upma
= isl_union_pw_multi_aff_empty(space
);
5920 if (isl_union_set_foreach_set(uset
,
5921 &add_pw_multi_aff_from_domain
, &upma
) < 0)
5924 isl_union_set_free(uset
);
5927 isl_union_set_free(uset
);
5928 isl_union_pw_multi_aff_free(upma
);
5932 /* Local data for bin_entry and the callback "fn".
5934 struct isl_union_pw_multi_aff_bin_data
{
5935 isl_union_pw_multi_aff
*upma2
;
5936 isl_union_pw_multi_aff
*res
;
5937 isl_pw_multi_aff
*pma
;
5938 isl_stat (*fn
)(__isl_take isl_pw_multi_aff
*pma
, void *user
);
5941 /* Given an isl_pw_multi_aff from upma1, store it in data->pma
5942 * and call data->fn for each isl_pw_multi_aff in data->upma2.
5944 static isl_stat
bin_entry(__isl_take isl_pw_multi_aff
*pma
, void *user
)
5946 struct isl_union_pw_multi_aff_bin_data
*data
= user
;
5950 r
= isl_union_pw_multi_aff_foreach_pw_multi_aff(data
->upma2
,
5952 isl_pw_multi_aff_free(pma
);
5957 /* Call "fn" on each pair of isl_pw_multi_affs in "upma1" and "upma2".
5958 * The isl_pw_multi_aff from upma1 is stored in data->pma (where data is
5959 * passed as user field) and the isl_pw_multi_aff from upma2 is available
5960 * as *entry. The callback should adjust data->res if desired.
5962 static __isl_give isl_union_pw_multi_aff
*bin_op(
5963 __isl_take isl_union_pw_multi_aff
*upma1
,
5964 __isl_take isl_union_pw_multi_aff
*upma2
,
5965 isl_stat (*fn
)(__isl_take isl_pw_multi_aff
*pma
, void *user
))
5968 struct isl_union_pw_multi_aff_bin_data data
= { NULL
, NULL
, NULL
, fn
};
5970 space
= isl_union_pw_multi_aff_get_space(upma2
);
5971 upma1
= isl_union_pw_multi_aff_align_params(upma1
, space
);
5972 space
= isl_union_pw_multi_aff_get_space(upma1
);
5973 upma2
= isl_union_pw_multi_aff_align_params(upma2
, space
);
5975 if (!upma1
|| !upma2
)
5979 data
.res
= isl_union_pw_multi_aff_alloc_same_size(upma1
);
5980 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma1
,
5981 &bin_entry
, &data
) < 0)
5984 isl_union_pw_multi_aff_free(upma1
);
5985 isl_union_pw_multi_aff_free(upma2
);
5988 isl_union_pw_multi_aff_free(upma1
);
5989 isl_union_pw_multi_aff_free(upma2
);
5990 isl_union_pw_multi_aff_free(data
.res
);
5994 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
5995 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
5997 static __isl_give isl_pw_multi_aff
*pw_multi_aff_range_product(
5998 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
6002 space
= isl_space_range_product(isl_pw_multi_aff_get_space(pma1
),
6003 isl_pw_multi_aff_get_space(pma2
));
6004 return isl_pw_multi_aff_on_shared_domain_in(pma1
, pma2
, space
,
6005 &isl_multi_aff_range_product
);
6008 /* Given two isl_pw_multi_affs A -> B and C -> D,
6009 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
6011 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_range_product(
6012 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
6014 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
6015 &pw_multi_aff_range_product
);
6018 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
6019 * construct an isl_pw_multi_aff (A * C) -> (B, D).
6021 static __isl_give isl_pw_multi_aff
*pw_multi_aff_flat_range_product(
6022 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
6026 space
= isl_space_range_product(isl_pw_multi_aff_get_space(pma1
),
6027 isl_pw_multi_aff_get_space(pma2
));
6028 space
= isl_space_flatten_range(space
);
6029 return isl_pw_multi_aff_on_shared_domain_in(pma1
, pma2
, space
,
6030 &isl_multi_aff_flat_range_product
);
6033 /* Given two isl_pw_multi_affs A -> B and C -> D,
6034 * construct an isl_pw_multi_aff (A * C) -> (B, D).
6036 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_flat_range_product(
6037 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
6039 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
6040 &pw_multi_aff_flat_range_product
);
6043 /* If data->pma and "pma2" have the same domain space, then compute
6044 * their flat range product and the result to data->res.
6046 static isl_stat
flat_range_product_entry(__isl_take isl_pw_multi_aff
*pma2
,
6049 struct isl_union_pw_multi_aff_bin_data
*data
= user
;
6051 if (!isl_space_tuple_is_equal(data
->pma
->dim
, isl_dim_in
,
6052 pma2
->dim
, isl_dim_in
)) {
6053 isl_pw_multi_aff_free(pma2
);
6057 pma2
= isl_pw_multi_aff_flat_range_product(
6058 isl_pw_multi_aff_copy(data
->pma
), pma2
);
6060 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma2
);
6065 /* Given two isl_union_pw_multi_affs A -> B and C -> D,
6066 * construct an isl_union_pw_multi_aff (A * C) -> (B, D).
6068 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_flat_range_product(
6069 __isl_take isl_union_pw_multi_aff
*upma1
,
6070 __isl_take isl_union_pw_multi_aff
*upma2
)
6072 return bin_op(upma1
, upma2
, &flat_range_product_entry
);
6075 /* Replace the affine expressions at position "pos" in "pma" by "pa".
6076 * The parameters are assumed to have been aligned.
6078 * The implementation essentially performs an isl_pw_*_on_shared_domain,
6079 * except that it works on two different isl_pw_* types.
6081 static __isl_give isl_pw_multi_aff
*pw_multi_aff_set_pw_aff(
6082 __isl_take isl_pw_multi_aff
*pma
, unsigned pos
,
6083 __isl_take isl_pw_aff
*pa
)
6086 isl_pw_multi_aff
*res
= NULL
;
6091 if (!isl_space_tuple_is_equal(pma
->dim
, isl_dim_in
,
6092 pa
->dim
, isl_dim_in
))
6093 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
6094 "domains don't match", goto error
);
6095 if (isl_pw_multi_aff_check_range(pma
, isl_dim_out
, pos
, 1) < 0)
6099 res
= isl_pw_multi_aff_alloc_size(isl_pw_multi_aff_get_space(pma
), n
);
6101 for (i
= 0; i
< pma
->n
; ++i
) {
6102 for (j
= 0; j
< pa
->n
; ++j
) {
6104 isl_multi_aff
*res_ij
;
6107 common
= isl_set_intersect(isl_set_copy(pma
->p
[i
].set
),
6108 isl_set_copy(pa
->p
[j
].set
));
6109 empty
= isl_set_plain_is_empty(common
);
6110 if (empty
< 0 || empty
) {
6111 isl_set_free(common
);
6117 res_ij
= isl_multi_aff_set_aff(
6118 isl_multi_aff_copy(pma
->p
[i
].maff
), pos
,
6119 isl_aff_copy(pa
->p
[j
].aff
));
6120 res_ij
= isl_multi_aff_gist(res_ij
,
6121 isl_set_copy(common
));
6123 res
= isl_pw_multi_aff_add_piece(res
, common
, res_ij
);
6127 isl_pw_multi_aff_free(pma
);
6128 isl_pw_aff_free(pa
);
6131 isl_pw_multi_aff_free(pma
);
6132 isl_pw_aff_free(pa
);
6133 return isl_pw_multi_aff_free(res
);
6136 /* Replace the affine expressions at position "pos" in "pma" by "pa".
6138 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_set_pw_aff(
6139 __isl_take isl_pw_multi_aff
*pma
, unsigned pos
,
6140 __isl_take isl_pw_aff
*pa
)
6142 isl_bool equal_params
;
6146 equal_params
= isl_space_has_equal_params(pma
->dim
, pa
->dim
);
6147 if (equal_params
< 0)
6150 return pw_multi_aff_set_pw_aff(pma
, pos
, pa
);
6151 if (isl_pw_multi_aff_check_named_params(pma
) < 0 ||
6152 isl_pw_aff_check_named_params(pa
) < 0)
6154 pma
= isl_pw_multi_aff_align_params(pma
, isl_pw_aff_get_space(pa
));
6155 pa
= isl_pw_aff_align_params(pa
, isl_pw_multi_aff_get_space(pma
));
6156 return pw_multi_aff_set_pw_aff(pma
, pos
, pa
);
6158 isl_pw_multi_aff_free(pma
);
6159 isl_pw_aff_free(pa
);
6163 /* Do the parameters of "pa" match those of "space"?
6165 isl_bool
isl_pw_aff_matching_params(__isl_keep isl_pw_aff
*pa
,
6166 __isl_keep isl_space
*space
)
6168 isl_space
*pa_space
;
6172 return isl_bool_error
;
6174 pa_space
= isl_pw_aff_get_space(pa
);
6176 match
= isl_space_has_equal_params(space
, pa_space
);
6178 isl_space_free(pa_space
);
6182 /* Check that the domain space of "pa" matches "space".
6184 isl_stat
isl_pw_aff_check_match_domain_space(__isl_keep isl_pw_aff
*pa
,
6185 __isl_keep isl_space
*space
)
6187 isl_space
*pa_space
;
6191 return isl_stat_error
;
6193 pa_space
= isl_pw_aff_get_space(pa
);
6195 match
= isl_space_has_equal_params(space
, pa_space
);
6199 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
6200 "parameters don't match", goto error
);
6201 match
= isl_space_tuple_is_equal(space
, isl_dim_in
,
6202 pa_space
, isl_dim_in
);
6206 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
6207 "domains don't match", goto error
);
6208 isl_space_free(pa_space
);
6211 isl_space_free(pa_space
);
6212 return isl_stat_error
;
6220 #include <isl_multi_explicit_domain.c>
6221 #include <isl_multi_pw_aff_explicit_domain.c>
6222 #include <isl_multi_templ.c>
6223 #include <isl_multi_apply_set.c>
6224 #include <isl_multi_arith_templ.c>
6225 #include <isl_multi_coalesce.c>
6226 #include <isl_multi_domain_templ.c>
6227 #include <isl_multi_dims.c>
6228 #include <isl_multi_from_base_templ.c>
6229 #include <isl_multi_gist.c>
6230 #include <isl_multi_hash.c>
6231 #include <isl_multi_identity_templ.c>
6232 #include <isl_multi_align_set.c>
6233 #include <isl_multi_intersect.c>
6234 #include <isl_multi_move_dims_templ.c>
6235 #include <isl_multi_nan_templ.c>
6236 #include <isl_multi_param_templ.c>
6237 #include <isl_multi_product_templ.c>
6238 #include <isl_multi_splice_templ.c>
6239 #include <isl_multi_zero_templ.c>
6241 /* Does "mpa" have a non-trivial explicit domain?
6243 * The explicit domain, if present, is trivial if it represents
6244 * an (obviously) universe set.
6246 isl_bool
isl_multi_pw_aff_has_non_trivial_domain(
6247 __isl_keep isl_multi_pw_aff
*mpa
)
6250 return isl_bool_error
;
6251 if (!isl_multi_pw_aff_has_explicit_domain(mpa
))
6252 return isl_bool_false
;
6253 return isl_bool_not(isl_set_plain_is_universe(mpa
->u
.dom
));
6256 /* Scale the elements of "pma" by the corresponding elements of "mv".
6258 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_scale_multi_val(
6259 __isl_take isl_pw_multi_aff
*pma
, __isl_take isl_multi_val
*mv
)
6262 isl_bool equal_params
;
6264 pma
= isl_pw_multi_aff_cow(pma
);
6267 if (!isl_space_tuple_is_equal(pma
->dim
, isl_dim_out
,
6268 mv
->space
, isl_dim_set
))
6269 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
6270 "spaces don't match", goto error
);
6271 equal_params
= isl_space_has_equal_params(pma
->dim
, mv
->space
);
6272 if (equal_params
< 0)
6274 if (!equal_params
) {
6275 pma
= isl_pw_multi_aff_align_params(pma
,
6276 isl_multi_val_get_space(mv
));
6277 mv
= isl_multi_val_align_params(mv
,
6278 isl_pw_multi_aff_get_space(pma
));
6283 for (i
= 0; i
< pma
->n
; ++i
) {
6284 pma
->p
[i
].maff
= isl_multi_aff_scale_multi_val(pma
->p
[i
].maff
,
6285 isl_multi_val_copy(mv
));
6286 if (!pma
->p
[i
].maff
)
6290 isl_multi_val_free(mv
);
6293 isl_multi_val_free(mv
);
6294 isl_pw_multi_aff_free(pma
);
6298 /* This function is called for each entry of an isl_union_pw_multi_aff.
6299 * If the space of the entry matches that of data->mv,
6300 * then apply isl_pw_multi_aff_scale_multi_val and return the result.
6301 * Otherwise, return an empty isl_pw_multi_aff.
6303 static __isl_give isl_pw_multi_aff
*union_pw_multi_aff_scale_multi_val_entry(
6304 __isl_take isl_pw_multi_aff
*pma
, void *user
)
6306 isl_multi_val
*mv
= user
;
6310 if (!isl_space_tuple_is_equal(pma
->dim
, isl_dim_out
,
6311 mv
->space
, isl_dim_set
)) {
6312 isl_space
*space
= isl_pw_multi_aff_get_space(pma
);
6313 isl_pw_multi_aff_free(pma
);
6314 return isl_pw_multi_aff_empty(space
);
6317 return isl_pw_multi_aff_scale_multi_val(pma
, isl_multi_val_copy(mv
));
6320 /* Scale the elements of "upma" by the corresponding elements of "mv",
6321 * for those entries that match the space of "mv".
6323 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_scale_multi_val(
6324 __isl_take isl_union_pw_multi_aff
*upma
, __isl_take isl_multi_val
*mv
)
6326 upma
= isl_union_pw_multi_aff_align_params(upma
,
6327 isl_multi_val_get_space(mv
));
6328 mv
= isl_multi_val_align_params(mv
,
6329 isl_union_pw_multi_aff_get_space(upma
));
6333 return isl_union_pw_multi_aff_transform(upma
,
6334 &union_pw_multi_aff_scale_multi_val_entry
, mv
);
6336 isl_multi_val_free(mv
);
6339 isl_multi_val_free(mv
);
6340 isl_union_pw_multi_aff_free(upma
);
6344 /* Construct and return a piecewise multi affine expression
6345 * in the given space with value zero in each of the output dimensions and
6346 * a universe domain.
6348 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_zero(__isl_take isl_space
*space
)
6350 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_zero(space
));
6353 /* Construct and return a piecewise multi affine expression
6354 * that is equal to the given piecewise affine expression.
6356 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_pw_aff(
6357 __isl_take isl_pw_aff
*pa
)
6361 isl_pw_multi_aff
*pma
;
6366 space
= isl_pw_aff_get_space(pa
);
6367 pma
= isl_pw_multi_aff_alloc_size(space
, pa
->n
);
6369 for (i
= 0; i
< pa
->n
; ++i
) {
6373 set
= isl_set_copy(pa
->p
[i
].set
);
6374 ma
= isl_multi_aff_from_aff(isl_aff_copy(pa
->p
[i
].aff
));
6375 pma
= isl_pw_multi_aff_add_piece(pma
, set
, ma
);
6378 isl_pw_aff_free(pa
);
6382 /* Construct and return a piecewise multi affine expression
6383 * that is equal to the given multi piecewise affine expression
6384 * on the shared domain of the piecewise affine expressions,
6385 * in the special case of a 0D multi piecewise affine expression.
6387 * Create a piecewise multi affine expression with the explicit domain of
6388 * the 0D multi piecewise affine expression as domain.
6390 static __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_multi_pw_aff_0D(
6391 __isl_take isl_multi_pw_aff
*mpa
)
6397 space
= isl_multi_pw_aff_get_space(mpa
);
6398 dom
= isl_multi_pw_aff_get_explicit_domain(mpa
);
6399 isl_multi_pw_aff_free(mpa
);
6401 ma
= isl_multi_aff_zero(space
);
6402 return isl_pw_multi_aff_alloc(dom
, ma
);
6405 /* Construct and return a piecewise multi affine expression
6406 * that is equal to the given multi piecewise affine expression
6407 * on the shared domain of the piecewise affine expressions.
6409 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_multi_pw_aff(
6410 __isl_take isl_multi_pw_aff
*mpa
)
6415 isl_pw_multi_aff
*pma
;
6421 return isl_pw_multi_aff_from_multi_pw_aff_0D(mpa
);
6423 space
= isl_multi_pw_aff_get_space(mpa
);
6424 pa
= isl_multi_pw_aff_get_pw_aff(mpa
, 0);
6425 pma
= isl_pw_multi_aff_from_pw_aff(pa
);
6427 for (i
= 1; i
< mpa
->n
; ++i
) {
6428 isl_pw_multi_aff
*pma_i
;
6430 pa
= isl_multi_pw_aff_get_pw_aff(mpa
, i
);
6431 pma_i
= isl_pw_multi_aff_from_pw_aff(pa
);
6432 pma
= isl_pw_multi_aff_range_product(pma
, pma_i
);
6435 pma
= isl_pw_multi_aff_reset_space(pma
, space
);
6437 isl_multi_pw_aff_free(mpa
);
6441 /* Construct and return a multi piecewise affine expression
6442 * that is equal to the given multi affine expression.
6444 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_from_multi_aff(
6445 __isl_take isl_multi_aff
*ma
)
6449 isl_multi_pw_aff
*mpa
;
6451 n
= isl_multi_aff_dim(ma
, isl_dim_out
);
6453 ma
= isl_multi_aff_free(ma
);
6457 mpa
= isl_multi_pw_aff_alloc(isl_multi_aff_get_space(ma
));
6459 for (i
= 0; i
< n
; ++i
) {
6462 pa
= isl_pw_aff_from_aff(isl_multi_aff_get_aff(ma
, i
));
6463 mpa
= isl_multi_pw_aff_set_pw_aff(mpa
, i
, pa
);
6466 isl_multi_aff_free(ma
);
6470 /* Construct and return a multi piecewise affine expression
6471 * that is equal to the given piecewise multi affine expression.
6473 * If the resulting multi piecewise affine expression has
6474 * an explicit domain, then assign it the domain of the input.
6475 * In other cases, the domain is stored in the individual elements.
6477 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_from_pw_multi_aff(
6478 __isl_take isl_pw_multi_aff
*pma
)
6483 isl_multi_pw_aff
*mpa
;
6485 n
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
6487 pma
= isl_pw_multi_aff_free(pma
);
6488 space
= isl_pw_multi_aff_get_space(pma
);
6489 mpa
= isl_multi_pw_aff_alloc(space
);
6491 for (i
= 0; i
< n
; ++i
) {
6494 pa
= isl_pw_multi_aff_get_pw_aff(pma
, i
);
6495 mpa
= isl_multi_pw_aff_set_pw_aff(mpa
, i
, pa
);
6497 if (isl_multi_pw_aff_has_explicit_domain(mpa
)) {
6500 dom
= isl_pw_multi_aff_domain(isl_pw_multi_aff_copy(pma
));
6501 mpa
= isl_multi_pw_aff_intersect_domain(mpa
, dom
);
6504 isl_pw_multi_aff_free(pma
);
6508 /* Do "pa1" and "pa2" represent the same function?
6510 * We first check if they are obviously equal.
6511 * If not, we convert them to maps and check if those are equal.
6513 * If "pa1" or "pa2" contain any NaNs, then they are considered
6514 * not to be the same. A NaN is not equal to anything, not even
6517 isl_bool
isl_pw_aff_is_equal(__isl_keep isl_pw_aff
*pa1
,
6518 __isl_keep isl_pw_aff
*pa2
)
6522 isl_map
*map1
, *map2
;
6525 return isl_bool_error
;
6527 equal
= isl_pw_aff_plain_is_equal(pa1
, pa2
);
6528 if (equal
< 0 || equal
)
6530 has_nan
= either_involves_nan(pa1
, pa2
);
6532 return isl_bool_error
;
6534 return isl_bool_false
;
6536 map1
= isl_map_from_pw_aff_internal(isl_pw_aff_copy(pa1
));
6537 map2
= isl_map_from_pw_aff_internal(isl_pw_aff_copy(pa2
));
6538 equal
= isl_map_is_equal(map1
, map2
);
6545 /* Do "mpa1" and "mpa2" represent the same function?
6547 * Note that we cannot convert the entire isl_multi_pw_aff
6548 * to a map because the domains of the piecewise affine expressions
6549 * may not be the same.
6551 isl_bool
isl_multi_pw_aff_is_equal(__isl_keep isl_multi_pw_aff
*mpa1
,
6552 __isl_keep isl_multi_pw_aff
*mpa2
)
6555 isl_bool equal
, equal_params
;
6558 return isl_bool_error
;
6560 equal_params
= isl_space_has_equal_params(mpa1
->space
, mpa2
->space
);
6561 if (equal_params
< 0)
6562 return isl_bool_error
;
6563 if (!equal_params
) {
6564 if (!isl_space_has_named_params(mpa1
->space
))
6565 return isl_bool_false
;
6566 if (!isl_space_has_named_params(mpa2
->space
))
6567 return isl_bool_false
;
6568 mpa1
= isl_multi_pw_aff_copy(mpa1
);
6569 mpa2
= isl_multi_pw_aff_copy(mpa2
);
6570 mpa1
= isl_multi_pw_aff_align_params(mpa1
,
6571 isl_multi_pw_aff_get_space(mpa2
));
6572 mpa2
= isl_multi_pw_aff_align_params(mpa2
,
6573 isl_multi_pw_aff_get_space(mpa1
));
6574 equal
= isl_multi_pw_aff_is_equal(mpa1
, mpa2
);
6575 isl_multi_pw_aff_free(mpa1
);
6576 isl_multi_pw_aff_free(mpa2
);
6580 equal
= isl_space_is_equal(mpa1
->space
, mpa2
->space
);
6581 if (equal
< 0 || !equal
)
6584 for (i
= 0; i
< mpa1
->n
; ++i
) {
6585 equal
= isl_pw_aff_is_equal(mpa1
->u
.p
[i
], mpa2
->u
.p
[i
]);
6586 if (equal
< 0 || !equal
)
6590 return isl_bool_true
;
6593 /* Do "pma1" and "pma2" represent the same function?
6595 * First check if they are obviously equal.
6596 * If not, then convert them to maps and check if those are equal.
6598 * If "pa1" or "pa2" contain any NaNs, then they are considered
6599 * not to be the same. A NaN is not equal to anything, not even
6602 isl_bool
isl_pw_multi_aff_is_equal(__isl_keep isl_pw_multi_aff
*pma1
,
6603 __isl_keep isl_pw_multi_aff
*pma2
)
6607 isl_map
*map1
, *map2
;
6610 return isl_bool_error
;
6612 equal
= isl_pw_multi_aff_plain_is_equal(pma1
, pma2
);
6613 if (equal
< 0 || equal
)
6615 has_nan
= isl_pw_multi_aff_involves_nan(pma1
);
6616 if (has_nan
>= 0 && !has_nan
)
6617 has_nan
= isl_pw_multi_aff_involves_nan(pma2
);
6618 if (has_nan
< 0 || has_nan
)
6619 return isl_bool_not(has_nan
);
6621 map1
= isl_map_from_pw_multi_aff(isl_pw_multi_aff_copy(pma1
));
6622 map2
= isl_map_from_pw_multi_aff(isl_pw_multi_aff_copy(pma2
));
6623 equal
= isl_map_is_equal(map1
, map2
);
6630 /* Compute the pullback of "mpa" by the function represented by "ma".
6631 * In other words, plug in "ma" in "mpa".
6633 * The parameters of "mpa" and "ma" are assumed to have been aligned.
6635 * If "mpa" has an explicit domain, then it is this domain
6636 * that needs to undergo a pullback, i.e., a preimage.
6638 static __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_pullback_multi_aff_aligned(
6639 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_multi_aff
*ma
)
6642 isl_space
*space
= NULL
;
6644 mpa
= isl_multi_pw_aff_cow(mpa
);
6648 space
= isl_space_join(isl_multi_aff_get_space(ma
),
6649 isl_multi_pw_aff_get_space(mpa
));
6653 for (i
= 0; i
< mpa
->n
; ++i
) {
6654 mpa
->u
.p
[i
] = isl_pw_aff_pullback_multi_aff(mpa
->u
.p
[i
],
6655 isl_multi_aff_copy(ma
));
6659 if (isl_multi_pw_aff_has_explicit_domain(mpa
)) {
6660 mpa
->u
.dom
= isl_set_preimage_multi_aff(mpa
->u
.dom
,
6661 isl_multi_aff_copy(ma
));
6666 isl_multi_aff_free(ma
);
6667 isl_space_free(mpa
->space
);
6671 isl_space_free(space
);
6672 isl_multi_pw_aff_free(mpa
);
6673 isl_multi_aff_free(ma
);
6677 /* Compute the pullback of "mpa" by the function represented by "ma".
6678 * In other words, plug in "ma" in "mpa".
6680 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_pullback_multi_aff(
6681 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_multi_aff
*ma
)
6683 isl_bool equal_params
;
6687 equal_params
= isl_space_has_equal_params(mpa
->space
, ma
->space
);
6688 if (equal_params
< 0)
6691 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa
, ma
);
6692 mpa
= isl_multi_pw_aff_align_params(mpa
, isl_multi_aff_get_space(ma
));
6693 ma
= isl_multi_aff_align_params(ma
, isl_multi_pw_aff_get_space(mpa
));
6694 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa
, ma
);
6696 isl_multi_pw_aff_free(mpa
);
6697 isl_multi_aff_free(ma
);
6701 /* Compute the pullback of "mpa" by the function represented by "pma".
6702 * In other words, plug in "pma" in "mpa".
6704 * The parameters of "mpa" and "mpa" are assumed to have been aligned.
6706 * If "mpa" has an explicit domain, then it is this domain
6707 * that needs to undergo a pullback, i.e., a preimage.
6709 static __isl_give isl_multi_pw_aff
*
6710 isl_multi_pw_aff_pullback_pw_multi_aff_aligned(
6711 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_pw_multi_aff
*pma
)
6714 isl_space
*space
= NULL
;
6716 mpa
= isl_multi_pw_aff_cow(mpa
);
6720 space
= isl_space_join(isl_pw_multi_aff_get_space(pma
),
6721 isl_multi_pw_aff_get_space(mpa
));
6723 for (i
= 0; i
< mpa
->n
; ++i
) {
6724 mpa
->u
.p
[i
] = isl_pw_aff_pullback_pw_multi_aff_aligned(
6725 mpa
->u
.p
[i
], isl_pw_multi_aff_copy(pma
));
6729 if (isl_multi_pw_aff_has_explicit_domain(mpa
)) {
6730 mpa
->u
.dom
= isl_set_preimage_pw_multi_aff(mpa
->u
.dom
,
6731 isl_pw_multi_aff_copy(pma
));
6736 isl_pw_multi_aff_free(pma
);
6737 isl_space_free(mpa
->space
);
6741 isl_space_free(space
);
6742 isl_multi_pw_aff_free(mpa
);
6743 isl_pw_multi_aff_free(pma
);
6747 /* Compute the pullback of "mpa" by the function represented by "pma".
6748 * In other words, plug in "pma" in "mpa".
6750 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_pullback_pw_multi_aff(
6751 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_pw_multi_aff
*pma
)
6753 isl_bool equal_params
;
6757 equal_params
= isl_space_has_equal_params(mpa
->space
, pma
->dim
);
6758 if (equal_params
< 0)
6761 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa
, pma
);
6762 mpa
= isl_multi_pw_aff_align_params(mpa
,
6763 isl_pw_multi_aff_get_space(pma
));
6764 pma
= isl_pw_multi_aff_align_params(pma
,
6765 isl_multi_pw_aff_get_space(mpa
));
6766 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa
, pma
);
6768 isl_multi_pw_aff_free(mpa
);
6769 isl_pw_multi_aff_free(pma
);
6773 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
6774 * with the domain of "aff". The domain of the result is the same
6776 * "mpa" and "aff" are assumed to have been aligned.
6778 * We first extract the parametric constant from "aff", defined
6779 * over the correct domain.
6780 * Then we add the appropriate combinations of the members of "mpa".
6781 * Finally, we add the integer divisions through recursive calls.
6783 static __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_aff_aligned(
6784 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_aff
*aff
)
6787 isl_size n_in
, n_div
, n_mpa_in
;
6793 n_in
= isl_aff_dim(aff
, isl_dim_in
);
6794 n_div
= isl_aff_dim(aff
, isl_dim_div
);
6795 n_mpa_in
= isl_multi_pw_aff_dim(mpa
, isl_dim_in
);
6796 if (n_in
< 0 || n_div
< 0 || n_mpa_in
< 0)
6799 space
= isl_space_domain(isl_multi_pw_aff_get_space(mpa
));
6800 tmp
= isl_aff_copy(aff
);
6801 tmp
= isl_aff_drop_dims(tmp
, isl_dim_div
, 0, n_div
);
6802 tmp
= isl_aff_drop_dims(tmp
, isl_dim_in
, 0, n_in
);
6803 tmp
= isl_aff_add_dims(tmp
, isl_dim_in
, n_mpa_in
);
6804 tmp
= isl_aff_reset_domain_space(tmp
, space
);
6805 pa
= isl_pw_aff_from_aff(tmp
);
6807 for (i
= 0; i
< n_in
; ++i
) {
6810 if (!isl_aff_involves_dims(aff
, isl_dim_in
, i
, 1))
6812 v
= isl_aff_get_coefficient_val(aff
, isl_dim_in
, i
);
6813 pa_i
= isl_multi_pw_aff_get_pw_aff(mpa
, i
);
6814 pa_i
= isl_pw_aff_scale_val(pa_i
, v
);
6815 pa
= isl_pw_aff_add(pa
, pa_i
);
6818 for (i
= 0; i
< n_div
; ++i
) {
6822 if (!isl_aff_involves_dims(aff
, isl_dim_div
, i
, 1))
6824 div
= isl_aff_get_div(aff
, i
);
6825 pa_i
= isl_multi_pw_aff_apply_aff_aligned(
6826 isl_multi_pw_aff_copy(mpa
), div
);
6827 pa_i
= isl_pw_aff_floor(pa_i
);
6828 v
= isl_aff_get_coefficient_val(aff
, isl_dim_div
, i
);
6829 pa_i
= isl_pw_aff_scale_val(pa_i
, v
);
6830 pa
= isl_pw_aff_add(pa
, pa_i
);
6833 isl_multi_pw_aff_free(mpa
);
6838 isl_multi_pw_aff_free(mpa
);
6843 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
6844 * with the domain of "aff". The domain of the result is the same
6847 __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_aff(
6848 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_aff
*aff
)
6850 isl_bool equal_params
;
6854 equal_params
= isl_space_has_equal_params(aff
->ls
->dim
, mpa
->space
);
6855 if (equal_params
< 0)
6858 return isl_multi_pw_aff_apply_aff_aligned(mpa
, aff
);
6860 aff
= isl_aff_align_params(aff
, isl_multi_pw_aff_get_space(mpa
));
6861 mpa
= isl_multi_pw_aff_align_params(mpa
, isl_aff_get_space(aff
));
6863 return isl_multi_pw_aff_apply_aff_aligned(mpa
, aff
);
6866 isl_multi_pw_aff_free(mpa
);
6870 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
6871 * with the domain of "pa". The domain of the result is the same
6873 * "mpa" and "pa" are assumed to have been aligned.
6875 * We consider each piece in turn. Note that the domains of the
6876 * pieces are assumed to be disjoint and they remain disjoint
6877 * after taking the preimage (over the same function).
6879 static __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_pw_aff_aligned(
6880 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_pw_aff
*pa
)
6889 space
= isl_space_join(isl_multi_pw_aff_get_space(mpa
),
6890 isl_pw_aff_get_space(pa
));
6891 res
= isl_pw_aff_empty(space
);
6893 for (i
= 0; i
< pa
->n
; ++i
) {
6897 pa_i
= isl_multi_pw_aff_apply_aff_aligned(
6898 isl_multi_pw_aff_copy(mpa
),
6899 isl_aff_copy(pa
->p
[i
].aff
));
6900 domain
= isl_set_copy(pa
->p
[i
].set
);
6901 domain
= isl_set_preimage_multi_pw_aff(domain
,
6902 isl_multi_pw_aff_copy(mpa
));
6903 pa_i
= isl_pw_aff_intersect_domain(pa_i
, domain
);
6904 res
= isl_pw_aff_add_disjoint(res
, pa_i
);
6907 isl_pw_aff_free(pa
);
6908 isl_multi_pw_aff_free(mpa
);
6911 isl_pw_aff_free(pa
);
6912 isl_multi_pw_aff_free(mpa
);
6916 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
6917 * with the domain of "pa". The domain of the result is the same
6920 __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_pw_aff(
6921 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_pw_aff
*pa
)
6923 isl_bool equal_params
;
6927 equal_params
= isl_space_has_equal_params(pa
->dim
, mpa
->space
);
6928 if (equal_params
< 0)
6931 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa
, pa
);
6933 pa
= isl_pw_aff_align_params(pa
, isl_multi_pw_aff_get_space(mpa
));
6934 mpa
= isl_multi_pw_aff_align_params(mpa
, isl_pw_aff_get_space(pa
));
6936 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa
, pa
);
6938 isl_pw_aff_free(pa
);
6939 isl_multi_pw_aff_free(mpa
);
6943 /* Compute the pullback of "pa" by the function represented by "mpa".
6944 * In other words, plug in "mpa" in "pa".
6945 * "pa" and "mpa" are assumed to have been aligned.
6947 * The pullback is computed by applying "pa" to "mpa".
6949 static __isl_give isl_pw_aff
*isl_pw_aff_pullback_multi_pw_aff_aligned(
6950 __isl_take isl_pw_aff
*pa
, __isl_take isl_multi_pw_aff
*mpa
)
6952 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa
, pa
);
6955 /* Compute the pullback of "pa" by the function represented by "mpa".
6956 * In other words, plug in "mpa" in "pa".
6958 * The pullback is computed by applying "pa" to "mpa".
6960 __isl_give isl_pw_aff
*isl_pw_aff_pullback_multi_pw_aff(
6961 __isl_take isl_pw_aff
*pa
, __isl_take isl_multi_pw_aff
*mpa
)
6963 return isl_multi_pw_aff_apply_pw_aff(mpa
, pa
);
6966 /* Compute the pullback of "mpa1" by the function represented by "mpa2".
6967 * In other words, plug in "mpa2" in "mpa1".
6969 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
6971 * We pullback each member of "mpa1" in turn.
6973 * If "mpa1" has an explicit domain, then it is this domain
6974 * that needs to undergo a pullback instead, i.e., a preimage.
6976 static __isl_give isl_multi_pw_aff
*
6977 isl_multi_pw_aff_pullback_multi_pw_aff_aligned(
6978 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
)
6981 isl_space
*space
= NULL
;
6983 mpa1
= isl_multi_pw_aff_cow(mpa1
);
6987 space
= isl_space_join(isl_multi_pw_aff_get_space(mpa2
),
6988 isl_multi_pw_aff_get_space(mpa1
));
6990 for (i
= 0; i
< mpa1
->n
; ++i
) {
6991 mpa1
->u
.p
[i
] = isl_pw_aff_pullback_multi_pw_aff_aligned(
6992 mpa1
->u
.p
[i
], isl_multi_pw_aff_copy(mpa2
));
6997 if (isl_multi_pw_aff_has_explicit_domain(mpa1
)) {
6998 mpa1
->u
.dom
= isl_set_preimage_multi_pw_aff(mpa1
->u
.dom
,
6999 isl_multi_pw_aff_copy(mpa2
));
7003 mpa1
= isl_multi_pw_aff_reset_space(mpa1
, space
);
7005 isl_multi_pw_aff_free(mpa2
);
7008 isl_space_free(space
);
7009 isl_multi_pw_aff_free(mpa1
);
7010 isl_multi_pw_aff_free(mpa2
);
7014 /* Compute the pullback of "mpa1" by the function represented by "mpa2".
7015 * In other words, plug in "mpa2" in "mpa1".
7017 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_pullback_multi_pw_aff(
7018 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
)
7020 return isl_multi_pw_aff_align_params_multi_multi_and(mpa1
, mpa2
,
7021 &isl_multi_pw_aff_pullback_multi_pw_aff_aligned
);
7024 /* Align the parameters of "mpa1" and "mpa2", check that the ranges
7025 * of "mpa1" and "mpa2" live in the same space, construct map space
7026 * between the domain spaces of "mpa1" and "mpa2" and call "order"
7027 * with this map space as extract argument.
7029 static __isl_give isl_map
*isl_multi_pw_aff_order_map(
7030 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
,
7031 __isl_give isl_map
*(*order
)(__isl_keep isl_multi_pw_aff
*mpa1
,
7032 __isl_keep isl_multi_pw_aff
*mpa2
, __isl_take isl_space
*space
))
7035 isl_space
*space1
, *space2
;
7038 mpa1
= isl_multi_pw_aff_align_params(mpa1
,
7039 isl_multi_pw_aff_get_space(mpa2
));
7040 mpa2
= isl_multi_pw_aff_align_params(mpa2
,
7041 isl_multi_pw_aff_get_space(mpa1
));
7044 match
= isl_space_tuple_is_equal(mpa1
->space
, isl_dim_out
,
7045 mpa2
->space
, isl_dim_out
);
7049 isl_die(isl_multi_pw_aff_get_ctx(mpa1
), isl_error_invalid
,
7050 "range spaces don't match", goto error
);
7051 space1
= isl_space_domain(isl_multi_pw_aff_get_space(mpa1
));
7052 space2
= isl_space_domain(isl_multi_pw_aff_get_space(mpa2
));
7053 space1
= isl_space_map_from_domain_and_range(space1
, space2
);
7055 res
= order(mpa1
, mpa2
, space1
);
7056 isl_multi_pw_aff_free(mpa1
);
7057 isl_multi_pw_aff_free(mpa2
);
7060 isl_multi_pw_aff_free(mpa1
);
7061 isl_multi_pw_aff_free(mpa2
);
7065 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7066 * where the function values are equal. "space" is the space of the result.
7067 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7069 * "mpa1" and "mpa2" are equal when each of the pairs of elements
7070 * in the sequences are equal.
7072 static __isl_give isl_map
*isl_multi_pw_aff_eq_map_on_space(
7073 __isl_keep isl_multi_pw_aff
*mpa1
, __isl_keep isl_multi_pw_aff
*mpa2
,
7074 __isl_take isl_space
*space
)
7080 n
= isl_multi_pw_aff_dim(mpa1
, isl_dim_out
);
7082 space
= isl_space_free(space
);
7083 res
= isl_map_universe(space
);
7085 for (i
= 0; i
< n
; ++i
) {
7086 isl_pw_aff
*pa1
, *pa2
;
7089 pa1
= isl_multi_pw_aff_get_pw_aff(mpa1
, i
);
7090 pa2
= isl_multi_pw_aff_get_pw_aff(mpa2
, i
);
7091 map
= isl_pw_aff_eq_map(pa1
, pa2
);
7092 res
= isl_map_intersect(res
, map
);
7098 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7099 * where the function values are equal.
7101 __isl_give isl_map
*isl_multi_pw_aff_eq_map(__isl_take isl_multi_pw_aff
*mpa1
,
7102 __isl_take isl_multi_pw_aff
*mpa2
)
7104 return isl_multi_pw_aff_order_map(mpa1
, mpa2
,
7105 &isl_multi_pw_aff_eq_map_on_space
);
7108 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7109 * where the function values of "mpa1" is lexicographically satisfies "base"
7110 * compared to that of "mpa2". "space" is the space of the result.
7111 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7113 * "mpa1" lexicographically satisfies "base" compared to "mpa2"
7114 * if its i-th element satisfies "base" when compared to
7115 * the i-th element of "mpa2" while all previous elements are
7118 static __isl_give isl_map
*isl_multi_pw_aff_lex_map_on_space(
7119 __isl_keep isl_multi_pw_aff
*mpa1
, __isl_keep isl_multi_pw_aff
*mpa2
,
7120 __isl_give isl_map
*(*base
)(__isl_take isl_pw_aff
*pa1
,
7121 __isl_take isl_pw_aff
*pa2
),
7122 __isl_take isl_space
*space
)
7126 isl_map
*res
, *rest
;
7128 n
= isl_multi_pw_aff_dim(mpa1
, isl_dim_out
);
7130 space
= isl_space_free(space
);
7131 res
= isl_map_empty(isl_space_copy(space
));
7132 rest
= isl_map_universe(space
);
7134 for (i
= 0; i
< n
; ++i
) {
7135 isl_pw_aff
*pa1
, *pa2
;
7138 pa1
= isl_multi_pw_aff_get_pw_aff(mpa1
, i
);
7139 pa2
= isl_multi_pw_aff_get_pw_aff(mpa2
, i
);
7140 map
= base(pa1
, pa2
);
7141 map
= isl_map_intersect(map
, isl_map_copy(rest
));
7142 res
= isl_map_union(res
, map
);
7147 pa1
= isl_multi_pw_aff_get_pw_aff(mpa1
, i
);
7148 pa2
= isl_multi_pw_aff_get_pw_aff(mpa2
, i
);
7149 map
= isl_pw_aff_eq_map(pa1
, pa2
);
7150 rest
= isl_map_intersect(rest
, map
);
7157 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7158 * where the function value of "mpa1" is lexicographically less than that
7159 * of "mpa2". "space" is the space of the result.
7160 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7162 * "mpa1" is less than "mpa2" if its i-th element is smaller
7163 * than the i-th element of "mpa2" while all previous elements are
7166 __isl_give isl_map
*isl_multi_pw_aff_lex_lt_map_on_space(
7167 __isl_keep isl_multi_pw_aff
*mpa1
, __isl_keep isl_multi_pw_aff
*mpa2
,
7168 __isl_take isl_space
*space
)
7170 return isl_multi_pw_aff_lex_map_on_space(mpa1
, mpa2
,
7171 &isl_pw_aff_lt_map
, space
);
7174 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7175 * where the function value of "mpa1" is lexicographically less than that
7178 __isl_give isl_map
*isl_multi_pw_aff_lex_lt_map(
7179 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
)
7181 return isl_multi_pw_aff_order_map(mpa1
, mpa2
,
7182 &isl_multi_pw_aff_lex_lt_map_on_space
);
7185 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7186 * where the function value of "mpa1" is lexicographically greater than that
7187 * of "mpa2". "space" is the space of the result.
7188 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7190 * "mpa1" is greater than "mpa2" if its i-th element is greater
7191 * than the i-th element of "mpa2" while all previous elements are
7194 __isl_give isl_map
*isl_multi_pw_aff_lex_gt_map_on_space(
7195 __isl_keep isl_multi_pw_aff
*mpa1
, __isl_keep isl_multi_pw_aff
*mpa2
,
7196 __isl_take isl_space
*space
)
7198 return isl_multi_pw_aff_lex_map_on_space(mpa1
, mpa2
,
7199 &isl_pw_aff_gt_map
, space
);
7202 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7203 * where the function value of "mpa1" is lexicographically greater than that
7206 __isl_give isl_map
*isl_multi_pw_aff_lex_gt_map(
7207 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
)
7209 return isl_multi_pw_aff_order_map(mpa1
, mpa2
,
7210 &isl_multi_pw_aff_lex_gt_map_on_space
);
7213 /* Compare two isl_affs.
7215 * Return -1 if "aff1" is "smaller" than "aff2", 1 if "aff1" is "greater"
7216 * than "aff2" and 0 if they are equal.
7218 * The order is fairly arbitrary. We do consider expressions that only involve
7219 * earlier dimensions as "smaller".
7221 int isl_aff_plain_cmp(__isl_keep isl_aff
*aff1
, __isl_keep isl_aff
*aff2
)
7234 cmp
= isl_local_space_cmp(aff1
->ls
, aff2
->ls
);
7238 last1
= isl_seq_last_non_zero(aff1
->v
->el
+ 1, aff1
->v
->size
- 1);
7239 last2
= isl_seq_last_non_zero(aff2
->v
->el
+ 1, aff1
->v
->size
- 1);
7241 return last1
- last2
;
7243 return isl_seq_cmp(aff1
->v
->el
, aff2
->v
->el
, aff1
->v
->size
);
7246 /* Compare two isl_pw_affs.
7248 * Return -1 if "pa1" is "smaller" than "pa2", 1 if "pa1" is "greater"
7249 * than "pa2" and 0 if they are equal.
7251 * The order is fairly arbitrary. We do consider expressions that only involve
7252 * earlier dimensions as "smaller".
7254 int isl_pw_aff_plain_cmp(__isl_keep isl_pw_aff
*pa1
,
7255 __isl_keep isl_pw_aff
*pa2
)
7268 cmp
= isl_space_cmp(pa1
->dim
, pa2
->dim
);
7272 if (pa1
->n
!= pa2
->n
)
7273 return pa1
->n
- pa2
->n
;
7275 for (i
= 0; i
< pa1
->n
; ++i
) {
7276 cmp
= isl_set_plain_cmp(pa1
->p
[i
].set
, pa2
->p
[i
].set
);
7279 cmp
= isl_aff_plain_cmp(pa1
->p
[i
].aff
, pa2
->p
[i
].aff
);
7287 /* Return a piecewise affine expression that is equal to "v" on "domain".
7289 __isl_give isl_pw_aff
*isl_pw_aff_val_on_domain(__isl_take isl_set
*domain
,
7290 __isl_take isl_val
*v
)
7293 isl_local_space
*ls
;
7296 space
= isl_set_get_space(domain
);
7297 ls
= isl_local_space_from_space(space
);
7298 aff
= isl_aff_val_on_domain(ls
, v
);
7300 return isl_pw_aff_alloc(domain
, aff
);
7303 /* Return a multi affine expression that is equal to "mv" on domain
7306 __isl_give isl_multi_aff
*isl_multi_aff_multi_val_on_space(
7307 __isl_take isl_space
*space
, __isl_take isl_multi_val
*mv
)
7312 isl_local_space
*ls
;
7315 n
= isl_multi_val_dim(mv
, isl_dim_set
);
7316 if (!space
|| n
< 0)
7319 space2
= isl_multi_val_get_space(mv
);
7320 space2
= isl_space_align_params(space2
, isl_space_copy(space
));
7321 space
= isl_space_align_params(space
, isl_space_copy(space2
));
7322 space
= isl_space_map_from_domain_and_range(space
, space2
);
7323 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
7324 ls
= isl_local_space_from_space(isl_space_domain(space
));
7325 for (i
= 0; i
< n
; ++i
) {
7329 v
= isl_multi_val_get_val(mv
, i
);
7330 aff
= isl_aff_val_on_domain(isl_local_space_copy(ls
), v
);
7331 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
7333 isl_local_space_free(ls
);
7335 isl_multi_val_free(mv
);
7338 isl_space_free(space
);
7339 isl_multi_val_free(mv
);
7343 /* Return a piecewise multi-affine expression
7344 * that is equal to "mv" on "domain".
7346 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_multi_val_on_domain(
7347 __isl_take isl_set
*domain
, __isl_take isl_multi_val
*mv
)
7352 space
= isl_set_get_space(domain
);
7353 ma
= isl_multi_aff_multi_val_on_space(space
, mv
);
7355 return isl_pw_multi_aff_alloc(domain
, ma
);
7358 /* Internal data structure for isl_union_pw_multi_aff_multi_val_on_domain.
7359 * mv is the value that should be attained on each domain set
7360 * res collects the results
7362 struct isl_union_pw_multi_aff_multi_val_on_domain_data
{
7364 isl_union_pw_multi_aff
*res
;
7367 /* Create an isl_pw_multi_aff equal to data->mv on "domain"
7368 * and add it to data->res.
7370 static isl_stat
pw_multi_aff_multi_val_on_domain(__isl_take isl_set
*domain
,
7373 struct isl_union_pw_multi_aff_multi_val_on_domain_data
*data
= user
;
7374 isl_pw_multi_aff
*pma
;
7377 mv
= isl_multi_val_copy(data
->mv
);
7378 pma
= isl_pw_multi_aff_multi_val_on_domain(domain
, mv
);
7379 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma
);
7381 return data
->res
? isl_stat_ok
: isl_stat_error
;
7384 /* Return a union piecewise multi-affine expression
7385 * that is equal to "mv" on "domain".
7387 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_multi_val_on_domain(
7388 __isl_take isl_union_set
*domain
, __isl_take isl_multi_val
*mv
)
7390 struct isl_union_pw_multi_aff_multi_val_on_domain_data data
;
7393 space
= isl_union_set_get_space(domain
);
7394 data
.res
= isl_union_pw_multi_aff_empty(space
);
7396 if (isl_union_set_foreach_set(domain
,
7397 &pw_multi_aff_multi_val_on_domain
, &data
) < 0)
7398 data
.res
= isl_union_pw_multi_aff_free(data
.res
);
7399 isl_union_set_free(domain
);
7400 isl_multi_val_free(mv
);
7404 /* Compute the pullback of data->pma by the function represented by "pma2",
7405 * provided the spaces match, and add the results to data->res.
7407 static isl_stat
pullback_entry(__isl_take isl_pw_multi_aff
*pma2
, void *user
)
7409 struct isl_union_pw_multi_aff_bin_data
*data
= user
;
7411 if (!isl_space_tuple_is_equal(data
->pma
->dim
, isl_dim_in
,
7412 pma2
->dim
, isl_dim_out
)) {
7413 isl_pw_multi_aff_free(pma2
);
7417 pma2
= isl_pw_multi_aff_pullback_pw_multi_aff(
7418 isl_pw_multi_aff_copy(data
->pma
), pma2
);
7420 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma2
);
7422 return isl_stat_error
;
7427 /* Compute the pullback of "upma1" by the function represented by "upma2".
7429 __isl_give isl_union_pw_multi_aff
*
7430 isl_union_pw_multi_aff_pullback_union_pw_multi_aff(
7431 __isl_take isl_union_pw_multi_aff
*upma1
,
7432 __isl_take isl_union_pw_multi_aff
*upma2
)
7434 return bin_op(upma1
, upma2
, &pullback_entry
);
7437 /* Check that the domain space of "upa" matches "space".
7439 * This function is called from isl_multi_union_pw_aff_set_union_pw_aff and
7440 * can in principle never fail since the space "space" is that
7441 * of the isl_multi_union_pw_aff and is a set space such that
7442 * there is no domain space to match.
7444 * We check the parameters and double-check that "space" is
7445 * indeed that of a set.
7447 static isl_stat
isl_union_pw_aff_check_match_domain_space(
7448 __isl_keep isl_union_pw_aff
*upa
, __isl_keep isl_space
*space
)
7450 isl_space
*upa_space
;
7454 return isl_stat_error
;
7456 match
= isl_space_is_set(space
);
7458 return isl_stat_error
;
7460 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
7461 "expecting set space", return isl_stat_error
);
7463 upa_space
= isl_union_pw_aff_get_space(upa
);
7464 match
= isl_space_has_equal_params(space
, upa_space
);
7468 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
7469 "parameters don't match", goto error
);
7471 isl_space_free(upa_space
);
7474 isl_space_free(upa_space
);
7475 return isl_stat_error
;
7478 /* Do the parameters of "upa" match those of "space"?
7480 static isl_bool
isl_union_pw_aff_matching_params(
7481 __isl_keep isl_union_pw_aff
*upa
, __isl_keep isl_space
*space
)
7483 isl_space
*upa_space
;
7487 return isl_bool_error
;
7489 upa_space
= isl_union_pw_aff_get_space(upa
);
7491 match
= isl_space_has_equal_params(space
, upa_space
);
7493 isl_space_free(upa_space
);
7497 /* Internal data structure for isl_union_pw_aff_reset_domain_space.
7498 * space represents the new parameters.
7499 * res collects the results.
7501 struct isl_union_pw_aff_reset_params_data
{
7503 isl_union_pw_aff
*res
;
7506 /* Replace the parameters of "pa" by data->space and
7507 * add the result to data->res.
7509 static isl_stat
reset_params(__isl_take isl_pw_aff
*pa
, void *user
)
7511 struct isl_union_pw_aff_reset_params_data
*data
= user
;
7514 space
= isl_pw_aff_get_space(pa
);
7515 space
= isl_space_replace_params(space
, data
->space
);
7516 pa
= isl_pw_aff_reset_space(pa
, space
);
7517 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
7519 return data
->res
? isl_stat_ok
: isl_stat_error
;
7522 /* Replace the domain space of "upa" by "space".
7523 * Since a union expression does not have a (single) domain space,
7524 * "space" is necessarily a parameter space.
7526 * Since the order and the names of the parameters determine
7527 * the hash value, we need to create a new hash table.
7529 static __isl_give isl_union_pw_aff
*isl_union_pw_aff_reset_domain_space(
7530 __isl_take isl_union_pw_aff
*upa
, __isl_take isl_space
*space
)
7532 struct isl_union_pw_aff_reset_params_data data
= { space
};
7535 match
= isl_union_pw_aff_matching_params(upa
, space
);
7537 upa
= isl_union_pw_aff_free(upa
);
7539 isl_space_free(space
);
7543 data
.res
= isl_union_pw_aff_empty(isl_space_copy(space
));
7544 if (isl_union_pw_aff_foreach_pw_aff(upa
, &reset_params
, &data
) < 0)
7545 data
.res
= isl_union_pw_aff_free(data
.res
);
7547 isl_union_pw_aff_free(upa
);
7548 isl_space_free(space
);
7552 /* Return the floor of "pa".
7554 static __isl_give isl_pw_aff
*floor_entry(__isl_take isl_pw_aff
*pa
, void *user
)
7556 return isl_pw_aff_floor(pa
);
7559 /* Given f, return floor(f).
7561 __isl_give isl_union_pw_aff
*isl_union_pw_aff_floor(
7562 __isl_take isl_union_pw_aff
*upa
)
7564 return isl_union_pw_aff_transform_inplace(upa
, &floor_entry
, NULL
);
7569 * upa mod m = upa - m * floor(upa/m)
7571 * with m an integer value.
7573 __isl_give isl_union_pw_aff
*isl_union_pw_aff_mod_val(
7574 __isl_take isl_union_pw_aff
*upa
, __isl_take isl_val
*m
)
7576 isl_union_pw_aff
*res
;
7581 if (!isl_val_is_int(m
))
7582 isl_die(isl_val_get_ctx(m
), isl_error_invalid
,
7583 "expecting integer modulo", goto error
);
7584 if (!isl_val_is_pos(m
))
7585 isl_die(isl_val_get_ctx(m
), isl_error_invalid
,
7586 "expecting positive modulo", goto error
);
7588 res
= isl_union_pw_aff_copy(upa
);
7589 upa
= isl_union_pw_aff_scale_down_val(upa
, isl_val_copy(m
));
7590 upa
= isl_union_pw_aff_floor(upa
);
7591 upa
= isl_union_pw_aff_scale_val(upa
, m
);
7592 res
= isl_union_pw_aff_sub(res
, upa
);
7597 isl_union_pw_aff_free(upa
);
7601 /* Internal data structure for isl_union_pw_multi_aff_get_union_pw_aff.
7602 * pos is the output position that needs to be extracted.
7603 * res collects the results.
7605 struct isl_union_pw_multi_aff_get_union_pw_aff_data
{
7607 isl_union_pw_aff
*res
;
7610 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma"
7611 * (assuming it has such a dimension) and add it to data->res.
7613 static isl_stat
get_union_pw_aff(__isl_take isl_pw_multi_aff
*pma
, void *user
)
7615 struct isl_union_pw_multi_aff_get_union_pw_aff_data
*data
= user
;
7619 n_out
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
7621 return isl_stat_error
;
7622 if (data
->pos
>= n_out
) {
7623 isl_pw_multi_aff_free(pma
);
7627 pa
= isl_pw_multi_aff_get_pw_aff(pma
, data
->pos
);
7628 isl_pw_multi_aff_free(pma
);
7630 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
7632 return data
->res
? isl_stat_ok
: isl_stat_error
;
7635 /* Extract an isl_union_pw_aff corresponding to
7636 * output dimension "pos" of "upma".
7638 __isl_give isl_union_pw_aff
*isl_union_pw_multi_aff_get_union_pw_aff(
7639 __isl_keep isl_union_pw_multi_aff
*upma
, int pos
)
7641 struct isl_union_pw_multi_aff_get_union_pw_aff_data data
;
7648 isl_die(isl_union_pw_multi_aff_get_ctx(upma
), isl_error_invalid
,
7649 "cannot extract at negative position", return NULL
);
7651 space
= isl_union_pw_multi_aff_get_space(upma
);
7652 data
.res
= isl_union_pw_aff_empty(space
);
7654 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma
,
7655 &get_union_pw_aff
, &data
) < 0)
7656 data
.res
= isl_union_pw_aff_free(data
.res
);
7661 /* Return a union piecewise affine expression
7662 * that is equal to "aff" on "domain".
7664 __isl_give isl_union_pw_aff
*isl_union_pw_aff_aff_on_domain(
7665 __isl_take isl_union_set
*domain
, __isl_take isl_aff
*aff
)
7669 pa
= isl_pw_aff_from_aff(aff
);
7670 return isl_union_pw_aff_pw_aff_on_domain(domain
, pa
);
7673 /* Return a union piecewise affine expression
7674 * that is equal to the parameter identified by "id" on "domain".
7676 * Make sure the parameter appears in the space passed to
7677 * isl_aff_param_on_domain_space_id.
7679 __isl_give isl_union_pw_aff
*isl_union_pw_aff_param_on_domain_id(
7680 __isl_take isl_union_set
*domain
, __isl_take isl_id
*id
)
7685 space
= isl_union_set_get_space(domain
);
7686 space
= isl_space_add_param_id(space
, isl_id_copy(id
));
7687 aff
= isl_aff_param_on_domain_space_id(space
, id
);
7688 return isl_union_pw_aff_aff_on_domain(domain
, aff
);
7691 /* Internal data structure for isl_union_pw_aff_pw_aff_on_domain.
7692 * "pa" is the piecewise symbolic value that the resulting isl_union_pw_aff
7694 * "res" collects the results.
7696 struct isl_union_pw_aff_pw_aff_on_domain_data
{
7698 isl_union_pw_aff
*res
;
7701 /* Construct a piecewise affine expression that is equal to data->pa
7702 * on "domain" and add the result to data->res.
7704 static isl_stat
pw_aff_on_domain(__isl_take isl_set
*domain
, void *user
)
7706 struct isl_union_pw_aff_pw_aff_on_domain_data
*data
= user
;
7710 pa
= isl_pw_aff_copy(data
->pa
);
7711 dim
= isl_set_dim(domain
, isl_dim_set
);
7713 pa
= isl_pw_aff_free(pa
);
7714 pa
= isl_pw_aff_from_range(pa
);
7715 pa
= isl_pw_aff_add_dims(pa
, isl_dim_in
, dim
);
7716 pa
= isl_pw_aff_reset_domain_space(pa
, isl_set_get_space(domain
));
7717 pa
= isl_pw_aff_intersect_domain(pa
, domain
);
7718 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
7720 return data
->res
? isl_stat_ok
: isl_stat_error
;
7723 /* Return a union piecewise affine expression
7724 * that is equal to "pa" on "domain", assuming "domain" and "pa"
7725 * have been aligned.
7727 * Construct an isl_pw_aff on each of the sets in "domain" and
7728 * collect the results.
7730 static __isl_give isl_union_pw_aff
*isl_union_pw_aff_pw_aff_on_domain_aligned(
7731 __isl_take isl_union_set
*domain
, __isl_take isl_pw_aff
*pa
)
7733 struct isl_union_pw_aff_pw_aff_on_domain_data data
;
7736 space
= isl_union_set_get_space(domain
);
7737 data
.res
= isl_union_pw_aff_empty(space
);
7739 if (isl_union_set_foreach_set(domain
, &pw_aff_on_domain
, &data
) < 0)
7740 data
.res
= isl_union_pw_aff_free(data
.res
);
7741 isl_union_set_free(domain
);
7742 isl_pw_aff_free(pa
);
7746 /* Return a union piecewise affine expression
7747 * that is equal to "pa" on "domain".
7749 * Check that "pa" is a parametric expression,
7750 * align the parameters if needed and call
7751 * isl_union_pw_aff_pw_aff_on_domain_aligned.
7753 __isl_give isl_union_pw_aff
*isl_union_pw_aff_pw_aff_on_domain(
7754 __isl_take isl_union_set
*domain
, __isl_take isl_pw_aff
*pa
)
7757 isl_bool equal_params
;
7758 isl_space
*domain_space
, *pa_space
;
7760 pa_space
= isl_pw_aff_peek_space(pa
);
7761 is_set
= isl_space_is_set(pa_space
);
7765 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
7766 "expecting parametric expression", goto error
);
7768 domain_space
= isl_union_set_get_space(domain
);
7769 pa_space
= isl_pw_aff_get_space(pa
);
7770 equal_params
= isl_space_has_equal_params(domain_space
, pa_space
);
7771 if (equal_params
>= 0 && !equal_params
) {
7774 space
= isl_space_align_params(domain_space
, pa_space
);
7775 pa
= isl_pw_aff_align_params(pa
, isl_space_copy(space
));
7776 domain
= isl_union_set_align_params(domain
, space
);
7778 isl_space_free(domain_space
);
7779 isl_space_free(pa_space
);
7782 if (equal_params
< 0)
7784 return isl_union_pw_aff_pw_aff_on_domain_aligned(domain
, pa
);
7786 isl_union_set_free(domain
);
7787 isl_pw_aff_free(pa
);
7791 /* Internal data structure for isl_union_pw_aff_val_on_domain.
7792 * "v" is the value that the resulting isl_union_pw_aff needs to attain.
7793 * "res" collects the results.
7795 struct isl_union_pw_aff_val_on_domain_data
{
7797 isl_union_pw_aff
*res
;
7800 /* Construct a piecewise affine expression that is equal to data->v
7801 * on "domain" and add the result to data->res.
7803 static isl_stat
pw_aff_val_on_domain(__isl_take isl_set
*domain
, void *user
)
7805 struct isl_union_pw_aff_val_on_domain_data
*data
= user
;
7809 v
= isl_val_copy(data
->v
);
7810 pa
= isl_pw_aff_val_on_domain(domain
, v
);
7811 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
7813 return data
->res
? isl_stat_ok
: isl_stat_error
;
7816 /* Return a union piecewise affine expression
7817 * that is equal to "v" on "domain".
7819 * Construct an isl_pw_aff on each of the sets in "domain" and
7820 * collect the results.
7822 __isl_give isl_union_pw_aff
*isl_union_pw_aff_val_on_domain(
7823 __isl_take isl_union_set
*domain
, __isl_take isl_val
*v
)
7825 struct isl_union_pw_aff_val_on_domain_data data
;
7828 space
= isl_union_set_get_space(domain
);
7829 data
.res
= isl_union_pw_aff_empty(space
);
7831 if (isl_union_set_foreach_set(domain
, &pw_aff_val_on_domain
, &data
) < 0)
7832 data
.res
= isl_union_pw_aff_free(data
.res
);
7833 isl_union_set_free(domain
);
7838 /* Construct a piecewise multi affine expression
7839 * that is equal to "pa" and add it to upma.
7841 static isl_stat
pw_multi_aff_from_pw_aff_entry(__isl_take isl_pw_aff
*pa
,
7844 isl_union_pw_multi_aff
**upma
= user
;
7845 isl_pw_multi_aff
*pma
;
7847 pma
= isl_pw_multi_aff_from_pw_aff(pa
);
7848 *upma
= isl_union_pw_multi_aff_add_pw_multi_aff(*upma
, pma
);
7850 return *upma
? isl_stat_ok
: isl_stat_error
;
7853 /* Construct and return a union piecewise multi affine expression
7854 * that is equal to the given union piecewise affine expression.
7856 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_union_pw_aff(
7857 __isl_take isl_union_pw_aff
*upa
)
7860 isl_union_pw_multi_aff
*upma
;
7865 space
= isl_union_pw_aff_get_space(upa
);
7866 upma
= isl_union_pw_multi_aff_empty(space
);
7868 if (isl_union_pw_aff_foreach_pw_aff(upa
,
7869 &pw_multi_aff_from_pw_aff_entry
, &upma
) < 0)
7870 upma
= isl_union_pw_multi_aff_free(upma
);
7872 isl_union_pw_aff_free(upa
);
7876 /* Compute the set of elements in the domain of "pa" where it is zero and
7877 * add this set to "uset".
7879 static isl_stat
zero_union_set(__isl_take isl_pw_aff
*pa
, void *user
)
7881 isl_union_set
**uset
= (isl_union_set
**)user
;
7883 *uset
= isl_union_set_add_set(*uset
, isl_pw_aff_zero_set(pa
));
7885 return *uset
? isl_stat_ok
: isl_stat_error
;
7888 /* Return a union set containing those elements in the domain
7889 * of "upa" where it is zero.
7891 __isl_give isl_union_set
*isl_union_pw_aff_zero_union_set(
7892 __isl_take isl_union_pw_aff
*upa
)
7894 isl_union_set
*zero
;
7896 zero
= isl_union_set_empty(isl_union_pw_aff_get_space(upa
));
7897 if (isl_union_pw_aff_foreach_pw_aff(upa
, &zero_union_set
, &zero
) < 0)
7898 zero
= isl_union_set_free(zero
);
7900 isl_union_pw_aff_free(upa
);
7904 /* Internal data structure for isl_union_pw_aff_pullback_union_pw_multi_aff.
7905 * upma is the function that is plugged in.
7906 * pa is the current part of the function in which upma is plugged in.
7907 * res collects the results.
7909 struct isl_union_pw_aff_pullback_upma_data
{
7910 isl_union_pw_multi_aff
*upma
;
7912 isl_union_pw_aff
*res
;
7915 /* Check if "pma" can be plugged into data->pa.
7916 * If so, perform the pullback and add the result to data->res.
7918 static isl_stat
pa_pb_pma(__isl_take isl_pw_multi_aff
*pma
, void *user
)
7920 struct isl_union_pw_aff_pullback_upma_data
*data
= user
;
7923 if (!isl_space_tuple_is_equal(data
->pa
->dim
, isl_dim_in
,
7924 pma
->dim
, isl_dim_out
)) {
7925 isl_pw_multi_aff_free(pma
);
7929 pa
= isl_pw_aff_copy(data
->pa
);
7930 pa
= isl_pw_aff_pullback_pw_multi_aff(pa
, pma
);
7932 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
7934 return data
->res
? isl_stat_ok
: isl_stat_error
;
7937 /* Check if any of the elements of data->upma can be plugged into pa,
7938 * add if so add the result to data->res.
7940 static isl_stat
upa_pb_upma(__isl_take isl_pw_aff
*pa
, void *user
)
7942 struct isl_union_pw_aff_pullback_upma_data
*data
= user
;
7946 r
= isl_union_pw_multi_aff_foreach_pw_multi_aff(data
->upma
,
7948 isl_pw_aff_free(pa
);
7953 /* Compute the pullback of "upa" by the function represented by "upma".
7954 * In other words, plug in "upma" in "upa". The result contains
7955 * expressions defined over the domain space of "upma".
7957 * Run over all pairs of elements in "upa" and "upma", perform
7958 * the pullback when appropriate and collect the results.
7959 * If the hash value were based on the domain space rather than
7960 * the function space, then we could run through all elements
7961 * of "upma" and directly pick out the corresponding element of "upa".
7963 __isl_give isl_union_pw_aff
*isl_union_pw_aff_pullback_union_pw_multi_aff(
7964 __isl_take isl_union_pw_aff
*upa
,
7965 __isl_take isl_union_pw_multi_aff
*upma
)
7967 struct isl_union_pw_aff_pullback_upma_data data
= { NULL
, NULL
};
7970 space
= isl_union_pw_multi_aff_get_space(upma
);
7971 upa
= isl_union_pw_aff_align_params(upa
, space
);
7972 space
= isl_union_pw_aff_get_space(upa
);
7973 upma
= isl_union_pw_multi_aff_align_params(upma
, space
);
7979 data
.res
= isl_union_pw_aff_alloc_same_size(upa
);
7980 if (isl_union_pw_aff_foreach_pw_aff(upa
, &upa_pb_upma
, &data
) < 0)
7981 data
.res
= isl_union_pw_aff_free(data
.res
);
7983 isl_union_pw_aff_free(upa
);
7984 isl_union_pw_multi_aff_free(upma
);
7987 isl_union_pw_aff_free(upa
);
7988 isl_union_pw_multi_aff_free(upma
);
7993 #define BASE union_pw_aff
7995 #define DOMBASE union_set
7997 #include <isl_multi_explicit_domain.c>
7998 #include <isl_multi_union_pw_aff_explicit_domain.c>
7999 #include <isl_multi_templ.c>
8000 #include <isl_multi_apply_set.c>
8001 #include <isl_multi_apply_union_set.c>
8002 #include <isl_multi_arith_templ.c>
8003 #include <isl_multi_coalesce.c>
8004 #include <isl_multi_floor.c>
8005 #include <isl_multi_from_base_templ.c>
8006 #include <isl_multi_gist.c>
8007 #include <isl_multi_align_set.c>
8008 #include <isl_multi_align_union_set.c>
8009 #include <isl_multi_intersect.c>
8010 #include <isl_multi_nan_templ.c>
8012 /* Does "mupa" have a non-trivial explicit domain?
8014 * The explicit domain, if present, is trivial if it represents
8015 * an (obviously) universe parameter set.
8017 isl_bool
isl_multi_union_pw_aff_has_non_trivial_domain(
8018 __isl_keep isl_multi_union_pw_aff
*mupa
)
8020 isl_bool is_params
, trivial
;
8024 return isl_bool_error
;
8025 if (!isl_multi_union_pw_aff_has_explicit_domain(mupa
))
8026 return isl_bool_false
;
8027 is_params
= isl_union_set_is_params(mupa
->u
.dom
);
8028 if (is_params
< 0 || !is_params
)
8029 return isl_bool_not(is_params
);
8030 set
= isl_set_from_union_set(isl_union_set_copy(mupa
->u
.dom
));
8031 trivial
= isl_set_plain_is_universe(set
);
8033 return isl_bool_not(trivial
);
8036 /* Construct a multiple union piecewise affine expression
8037 * in the given space with value zero in each of the output dimensions.
8039 * Since there is no canonical zero value for
8040 * a union piecewise affine expression, we can only construct
8041 * a zero-dimensional "zero" value.
8043 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_zero(
8044 __isl_take isl_space
*space
)
8052 params
= isl_space_is_params(space
);
8056 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
8057 "expecting proper set space", goto error
);
8058 if (!isl_space_is_set(space
))
8059 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
8060 "expecting set space", goto error
);
8061 dim
= isl_space_dim(space
, isl_dim_out
);
8065 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
8066 "expecting 0D space", goto error
);
8068 return isl_multi_union_pw_aff_alloc(space
);
8070 isl_space_free(space
);
8074 /* Compute the sum of "mupa1" and "mupa2" on the union of their domains,
8075 * with the actual sum on the shared domain and
8076 * the defined expression on the symmetric difference of the domains.
8078 * We simply iterate over the elements in both arguments and
8079 * call isl_union_pw_aff_union_add on each of them, if there is
8080 * at least one element.
8082 * Otherwise, the two expressions have an explicit domain and
8083 * the union of these explicit domains is computed.
8084 * This assumes that the explicit domains are either both in terms
8085 * of specific domains elements or both in terms of parameters.
8086 * However, if one of the expressions does not have any constraints
8087 * on its explicit domain, then this is allowed as well and the result
8088 * is the expression with no constraints on its explicit domain.
8090 static __isl_give isl_multi_union_pw_aff
*
8091 isl_multi_union_pw_aff_union_add_aligned(
8092 __isl_take isl_multi_union_pw_aff
*mupa1
,
8093 __isl_take isl_multi_union_pw_aff
*mupa2
)
8095 isl_bool has_domain
, is_params1
, is_params2
;
8097 if (isl_multi_union_pw_aff_check_equal_space(mupa1
, mupa2
) < 0)
8100 return isl_multi_union_pw_aff_bin_op(mupa1
, mupa2
,
8101 &isl_union_pw_aff_union_add
);
8102 if (isl_multi_union_pw_aff_check_has_explicit_domain(mupa1
) < 0 ||
8103 isl_multi_union_pw_aff_check_has_explicit_domain(mupa2
) < 0)
8106 has_domain
= isl_multi_union_pw_aff_has_non_trivial_domain(mupa1
);
8110 isl_multi_union_pw_aff_free(mupa2
);
8113 has_domain
= isl_multi_union_pw_aff_has_non_trivial_domain(mupa2
);
8117 isl_multi_union_pw_aff_free(mupa1
);
8121 is_params1
= isl_union_set_is_params(mupa1
->u
.dom
);
8122 is_params2
= isl_union_set_is_params(mupa2
->u
.dom
);
8123 if (is_params1
< 0 || is_params2
< 0)
8125 if (is_params1
!= is_params2
)
8126 isl_die(isl_multi_union_pw_aff_get_ctx(mupa1
),
8128 "cannot compute union of concrete domain and "
8129 "parameter constraints", goto error
);
8130 mupa1
= isl_multi_union_pw_aff_cow(mupa1
);
8133 mupa1
->u
.dom
= isl_union_set_union(mupa1
->u
.dom
,
8134 isl_union_set_copy(mupa2
->u
.dom
));
8137 isl_multi_union_pw_aff_free(mupa2
);
8140 isl_multi_union_pw_aff_free(mupa1
);
8141 isl_multi_union_pw_aff_free(mupa2
);
8145 /* Compute the sum of "mupa1" and "mupa2" on the union of their domains,
8146 * with the actual sum on the shared domain and
8147 * the defined expression on the symmetric difference of the domains.
8149 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_union_add(
8150 __isl_take isl_multi_union_pw_aff
*mupa1
,
8151 __isl_take isl_multi_union_pw_aff
*mupa2
)
8153 return isl_multi_union_pw_aff_align_params_multi_multi_and(mupa1
, mupa2
,
8154 &isl_multi_union_pw_aff_union_add_aligned
);
8157 /* Construct and return a multi union piecewise affine expression
8158 * that is equal to the given multi affine expression.
8160 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_from_multi_aff(
8161 __isl_take isl_multi_aff
*ma
)
8163 isl_multi_pw_aff
*mpa
;
8165 mpa
= isl_multi_pw_aff_from_multi_aff(ma
);
8166 return isl_multi_union_pw_aff_from_multi_pw_aff(mpa
);
8169 /* Construct and return a multi union piecewise affine expression
8170 * that is equal to the given multi piecewise affine expression.
8172 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_from_multi_pw_aff(
8173 __isl_take isl_multi_pw_aff
*mpa
)
8178 isl_multi_union_pw_aff
*mupa
;
8180 n
= isl_multi_pw_aff_dim(mpa
, isl_dim_out
);
8182 mpa
= isl_multi_pw_aff_free(mpa
);
8186 space
= isl_multi_pw_aff_get_space(mpa
);
8187 space
= isl_space_range(space
);
8188 mupa
= isl_multi_union_pw_aff_alloc(space
);
8190 for (i
= 0; i
< n
; ++i
) {
8192 isl_union_pw_aff
*upa
;
8194 pa
= isl_multi_pw_aff_get_pw_aff(mpa
, i
);
8195 upa
= isl_union_pw_aff_from_pw_aff(pa
);
8196 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
8199 isl_multi_pw_aff_free(mpa
);
8204 /* Extract the range space of "pma" and assign it to *space.
8205 * If *space has already been set (through a previous call to this function),
8206 * then check that the range space is the same.
8208 static isl_stat
extract_space(__isl_take isl_pw_multi_aff
*pma
, void *user
)
8210 isl_space
**space
= user
;
8211 isl_space
*pma_space
;
8214 pma_space
= isl_space_range(isl_pw_multi_aff_get_space(pma
));
8215 isl_pw_multi_aff_free(pma
);
8218 return isl_stat_error
;
8224 equal
= isl_space_is_equal(pma_space
, *space
);
8225 isl_space_free(pma_space
);
8228 return isl_stat_error
;
8230 isl_die(isl_space_get_ctx(*space
), isl_error_invalid
,
8231 "range spaces not the same", return isl_stat_error
);
8235 /* Construct and return a multi union piecewise affine expression
8236 * that is equal to the given union piecewise multi affine expression.
8238 * In order to be able to perform the conversion, the input
8239 * needs to be non-empty and may only involve a single range space.
8241 * If the resulting multi union piecewise affine expression has
8242 * an explicit domain, then assign it the domain of the input.
8243 * In other cases, the domain is stored in the individual elements.
8245 __isl_give isl_multi_union_pw_aff
*
8246 isl_multi_union_pw_aff_from_union_pw_multi_aff(
8247 __isl_take isl_union_pw_multi_aff
*upma
)
8249 isl_space
*space
= NULL
;
8250 isl_multi_union_pw_aff
*mupa
;
8254 n
= isl_union_pw_multi_aff_n_pw_multi_aff(upma
);
8258 isl_die(isl_union_pw_multi_aff_get_ctx(upma
), isl_error_invalid
,
8259 "cannot extract range space from empty input",
8261 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma
, &extract_space
,
8268 n
= isl_space_dim(space
, isl_dim_set
);
8270 space
= isl_space_free(space
);
8271 mupa
= isl_multi_union_pw_aff_alloc(space
);
8273 for (i
= 0; i
< n
; ++i
) {
8274 isl_union_pw_aff
*upa
;
8276 upa
= isl_union_pw_multi_aff_get_union_pw_aff(upma
, i
);
8277 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
8279 if (isl_multi_union_pw_aff_has_explicit_domain(mupa
)) {
8281 isl_union_pw_multi_aff
*copy
;
8283 copy
= isl_union_pw_multi_aff_copy(upma
);
8284 dom
= isl_union_pw_multi_aff_domain(copy
);
8285 mupa
= isl_multi_union_pw_aff_intersect_domain(mupa
, dom
);
8288 isl_union_pw_multi_aff_free(upma
);
8291 isl_space_free(space
);
8292 isl_union_pw_multi_aff_free(upma
);
8296 /* Try and create an isl_multi_union_pw_aff that is equivalent
8297 * to the given isl_union_map.
8298 * The isl_union_map is required to be single-valued in each space.
8299 * Moreover, it cannot be empty and all range spaces need to be the same.
8300 * Otherwise, an error is produced.
8302 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_from_union_map(
8303 __isl_take isl_union_map
*umap
)
8305 isl_union_pw_multi_aff
*upma
;
8307 upma
= isl_union_pw_multi_aff_from_union_map(umap
);
8308 return isl_multi_union_pw_aff_from_union_pw_multi_aff(upma
);
8311 /* Return a multiple union piecewise affine expression
8312 * that is equal to "mv" on "domain", assuming "domain" and "mv"
8313 * have been aligned.
8315 * If the resulting multi union piecewise affine expression has
8316 * an explicit domain, then assign it the input domain.
8317 * In other cases, the domain is stored in the individual elements.
8319 static __isl_give isl_multi_union_pw_aff
*
8320 isl_multi_union_pw_aff_multi_val_on_domain_aligned(
8321 __isl_take isl_union_set
*domain
, __isl_take isl_multi_val
*mv
)
8326 isl_multi_union_pw_aff
*mupa
;
8328 n
= isl_multi_val_dim(mv
, isl_dim_set
);
8329 if (!domain
|| n
< 0)
8332 space
= isl_multi_val_get_space(mv
);
8333 mupa
= isl_multi_union_pw_aff_alloc(space
);
8334 for (i
= 0; i
< n
; ++i
) {
8336 isl_union_pw_aff
*upa
;
8338 v
= isl_multi_val_get_val(mv
, i
);
8339 upa
= isl_union_pw_aff_val_on_domain(isl_union_set_copy(domain
),
8341 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
8343 if (isl_multi_union_pw_aff_has_explicit_domain(mupa
))
8344 mupa
= isl_multi_union_pw_aff_intersect_domain(mupa
,
8345 isl_union_set_copy(domain
));
8347 isl_union_set_free(domain
);
8348 isl_multi_val_free(mv
);
8351 isl_union_set_free(domain
);
8352 isl_multi_val_free(mv
);
8356 /* Return a multiple union piecewise affine expression
8357 * that is equal to "mv" on "domain".
8359 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_multi_val_on_domain(
8360 __isl_take isl_union_set
*domain
, __isl_take isl_multi_val
*mv
)
8362 isl_bool equal_params
;
8366 equal_params
= isl_space_has_equal_params(domain
->dim
, mv
->space
);
8367 if (equal_params
< 0)
8370 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(
8372 domain
= isl_union_set_align_params(domain
,
8373 isl_multi_val_get_space(mv
));
8374 mv
= isl_multi_val_align_params(mv
, isl_union_set_get_space(domain
));
8375 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(domain
, mv
);
8377 isl_union_set_free(domain
);
8378 isl_multi_val_free(mv
);
8382 /* Return a multiple union piecewise affine expression
8383 * that is equal to "ma" on "domain".
8385 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_multi_aff_on_domain(
8386 __isl_take isl_union_set
*domain
, __isl_take isl_multi_aff
*ma
)
8388 isl_pw_multi_aff
*pma
;
8390 pma
= isl_pw_multi_aff_from_multi_aff(ma
);
8391 return isl_multi_union_pw_aff_pw_multi_aff_on_domain(domain
, pma
);
8394 /* Return a multiple union piecewise affine expression
8395 * that is equal to "pma" on "domain", assuming "domain" and "pma"
8396 * have been aligned.
8398 * If the resulting multi union piecewise affine expression has
8399 * an explicit domain, then assign it the input domain.
8400 * In other cases, the domain is stored in the individual elements.
8402 static __isl_give isl_multi_union_pw_aff
*
8403 isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(
8404 __isl_take isl_union_set
*domain
, __isl_take isl_pw_multi_aff
*pma
)
8409 isl_multi_union_pw_aff
*mupa
;
8411 n
= isl_pw_multi_aff_dim(pma
, isl_dim_set
);
8412 if (!domain
|| n
< 0)
8414 space
= isl_pw_multi_aff_get_space(pma
);
8415 mupa
= isl_multi_union_pw_aff_alloc(space
);
8416 for (i
= 0; i
< n
; ++i
) {
8418 isl_union_pw_aff
*upa
;
8420 pa
= isl_pw_multi_aff_get_pw_aff(pma
, i
);
8421 upa
= isl_union_pw_aff_pw_aff_on_domain(
8422 isl_union_set_copy(domain
), pa
);
8423 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
8425 if (isl_multi_union_pw_aff_has_explicit_domain(mupa
))
8426 mupa
= isl_multi_union_pw_aff_intersect_domain(mupa
,
8427 isl_union_set_copy(domain
));
8429 isl_union_set_free(domain
);
8430 isl_pw_multi_aff_free(pma
);
8433 isl_union_set_free(domain
);
8434 isl_pw_multi_aff_free(pma
);
8438 /* Return a multiple union piecewise affine expression
8439 * that is equal to "pma" on "domain".
8441 __isl_give isl_multi_union_pw_aff
*
8442 isl_multi_union_pw_aff_pw_multi_aff_on_domain(__isl_take isl_union_set
*domain
,
8443 __isl_take isl_pw_multi_aff
*pma
)
8445 isl_bool equal_params
;
8448 space
= isl_pw_multi_aff_peek_space(pma
);
8449 equal_params
= isl_union_set_space_has_equal_params(domain
, space
);
8450 if (equal_params
< 0)
8453 return isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(
8455 domain
= isl_union_set_align_params(domain
,
8456 isl_pw_multi_aff_get_space(pma
));
8457 pma
= isl_pw_multi_aff_align_params(pma
,
8458 isl_union_set_get_space(domain
));
8459 return isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(domain
,
8462 isl_union_set_free(domain
);
8463 isl_pw_multi_aff_free(pma
);
8467 /* Return a union set containing those elements in the domains
8468 * of the elements of "mupa" where they are all zero.
8470 * If there are no elements, then simply return the entire domain.
8472 __isl_give isl_union_set
*isl_multi_union_pw_aff_zero_union_set(
8473 __isl_take isl_multi_union_pw_aff
*mupa
)
8477 isl_union_pw_aff
*upa
;
8478 isl_union_set
*zero
;
8480 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8482 mupa
= isl_multi_union_pw_aff_free(mupa
);
8487 return isl_multi_union_pw_aff_domain(mupa
);
8489 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, 0);
8490 zero
= isl_union_pw_aff_zero_union_set(upa
);
8492 for (i
= 1; i
< n
; ++i
) {
8493 isl_union_set
*zero_i
;
8495 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
8496 zero_i
= isl_union_pw_aff_zero_union_set(upa
);
8498 zero
= isl_union_set_intersect(zero
, zero_i
);
8501 isl_multi_union_pw_aff_free(mupa
);
8505 /* Construct a union map mapping the shared domain
8506 * of the union piecewise affine expressions to the range of "mupa"
8507 * in the special case of a 0D multi union piecewise affine expression.
8509 * Construct a map between the explicit domain of "mupa" and
8511 * Note that this assumes that the domain consists of explicit elements.
8513 static __isl_give isl_union_map
*isl_union_map_from_multi_union_pw_aff_0D(
8514 __isl_take isl_multi_union_pw_aff
*mupa
)
8518 isl_union_set
*dom
, *ran
;
8520 space
= isl_multi_union_pw_aff_get_space(mupa
);
8521 dom
= isl_multi_union_pw_aff_domain(mupa
);
8522 ran
= isl_union_set_from_set(isl_set_universe(space
));
8524 is_params
= isl_union_set_is_params(dom
);
8526 dom
= isl_union_set_free(dom
);
8528 isl_die(isl_union_set_get_ctx(dom
), isl_error_invalid
,
8529 "cannot create union map from expression without "
8530 "explicit domain elements",
8531 dom
= isl_union_set_free(dom
));
8533 return isl_union_map_from_domain_and_range(dom
, ran
);
8536 /* Construct a union map mapping the shared domain
8537 * of the union piecewise affine expressions to the range of "mupa"
8538 * with each dimension in the range equated to the
8539 * corresponding union piecewise affine expression.
8541 * If the input is zero-dimensional, then construct a mapping
8542 * from its explicit domain.
8544 __isl_give isl_union_map
*isl_union_map_from_multi_union_pw_aff(
8545 __isl_take isl_multi_union_pw_aff
*mupa
)
8550 isl_union_map
*umap
;
8551 isl_union_pw_aff
*upa
;
8553 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8555 mupa
= isl_multi_union_pw_aff_free(mupa
);
8560 return isl_union_map_from_multi_union_pw_aff_0D(mupa
);
8562 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, 0);
8563 umap
= isl_union_map_from_union_pw_aff(upa
);
8565 for (i
= 1; i
< n
; ++i
) {
8566 isl_union_map
*umap_i
;
8568 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
8569 umap_i
= isl_union_map_from_union_pw_aff(upa
);
8570 umap
= isl_union_map_flat_range_product(umap
, umap_i
);
8573 space
= isl_multi_union_pw_aff_get_space(mupa
);
8574 umap
= isl_union_map_reset_range_space(umap
, space
);
8576 isl_multi_union_pw_aff_free(mupa
);
8580 /* Internal data structure for isl_union_pw_multi_aff_reset_range_space.
8581 * "range" is the space from which to set the range space.
8582 * "res" collects the results.
8584 struct isl_union_pw_multi_aff_reset_range_space_data
{
8586 isl_union_pw_multi_aff
*res
;
8589 /* Replace the range space of "pma" by the range space of data->range and
8590 * add the result to data->res.
8592 static isl_stat
reset_range_space(__isl_take isl_pw_multi_aff
*pma
, void *user
)
8594 struct isl_union_pw_multi_aff_reset_range_space_data
*data
= user
;
8597 space
= isl_pw_multi_aff_get_space(pma
);
8598 space
= isl_space_domain(space
);
8599 space
= isl_space_extend_domain_with_range(space
,
8600 isl_space_copy(data
->range
));
8601 pma
= isl_pw_multi_aff_reset_space(pma
, space
);
8602 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma
);
8604 return data
->res
? isl_stat_ok
: isl_stat_error
;
8607 /* Replace the range space of all the piecewise affine expressions in "upma" by
8608 * the range space of "space".
8610 * This assumes that all these expressions have the same output dimension.
8612 * Since the spaces of the expressions change, so do their hash values.
8613 * We therefore need to create a new isl_union_pw_multi_aff.
8614 * Note that the hash value is currently computed based on the entire
8615 * space even though there can only be a single expression with a given
8618 static __isl_give isl_union_pw_multi_aff
*
8619 isl_union_pw_multi_aff_reset_range_space(
8620 __isl_take isl_union_pw_multi_aff
*upma
, __isl_take isl_space
*space
)
8622 struct isl_union_pw_multi_aff_reset_range_space_data data
= { space
};
8623 isl_space
*space_upma
;
8625 space_upma
= isl_union_pw_multi_aff_get_space(upma
);
8626 data
.res
= isl_union_pw_multi_aff_empty(space_upma
);
8627 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma
,
8628 &reset_range_space
, &data
) < 0)
8629 data
.res
= isl_union_pw_multi_aff_free(data
.res
);
8631 isl_space_free(space
);
8632 isl_union_pw_multi_aff_free(upma
);
8636 /* Construct and return a union piecewise multi affine expression
8637 * that is equal to the given multi union piecewise affine expression,
8638 * in the special case of a 0D multi union piecewise affine expression.
8640 * Construct a union piecewise multi affine expression
8641 * on top of the explicit domain of the input.
8643 __isl_give isl_union_pw_multi_aff
*
8644 isl_union_pw_multi_aff_from_multi_union_pw_aff_0D(
8645 __isl_take isl_multi_union_pw_aff
*mupa
)
8649 isl_union_set
*domain
;
8651 space
= isl_multi_union_pw_aff_get_space(mupa
);
8652 mv
= isl_multi_val_zero(space
);
8653 domain
= isl_multi_union_pw_aff_domain(mupa
);
8654 return isl_union_pw_multi_aff_multi_val_on_domain(domain
, mv
);
8657 /* Construct and return a union piecewise multi affine expression
8658 * that is equal to the given multi union piecewise affine expression.
8660 * If the input is zero-dimensional, then
8661 * construct a union piecewise multi affine expression
8662 * on top of the explicit domain of the input.
8664 __isl_give isl_union_pw_multi_aff
*
8665 isl_union_pw_multi_aff_from_multi_union_pw_aff(
8666 __isl_take isl_multi_union_pw_aff
*mupa
)
8671 isl_union_pw_multi_aff
*upma
;
8672 isl_union_pw_aff
*upa
;
8674 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8676 mupa
= isl_multi_union_pw_aff_free(mupa
);
8681 return isl_union_pw_multi_aff_from_multi_union_pw_aff_0D(mupa
);
8683 space
= isl_multi_union_pw_aff_get_space(mupa
);
8684 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, 0);
8685 upma
= isl_union_pw_multi_aff_from_union_pw_aff(upa
);
8687 for (i
= 1; i
< n
; ++i
) {
8688 isl_union_pw_multi_aff
*upma_i
;
8690 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
8691 upma_i
= isl_union_pw_multi_aff_from_union_pw_aff(upa
);
8692 upma
= isl_union_pw_multi_aff_flat_range_product(upma
, upma_i
);
8695 upma
= isl_union_pw_multi_aff_reset_range_space(upma
, space
);
8697 isl_multi_union_pw_aff_free(mupa
);
8701 /* Intersect the range of "mupa" with "range",
8702 * in the special case where "mupa" is 0D.
8704 * Intersect the domain of "mupa" with the constraints on the parameters
8707 static __isl_give isl_multi_union_pw_aff
*mupa_intersect_range_0D(
8708 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_set
*range
)
8710 range
= isl_set_params(range
);
8711 mupa
= isl_multi_union_pw_aff_intersect_params(mupa
, range
);
8715 /* Intersect the range of "mupa" with "range".
8716 * That is, keep only those domain elements that have a function value
8719 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_intersect_range(
8720 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_set
*range
)
8722 isl_union_pw_multi_aff
*upma
;
8723 isl_union_set
*domain
;
8728 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8729 if (n
< 0 || !range
)
8732 space
= isl_set_get_space(range
);
8733 match
= isl_space_tuple_is_equal(mupa
->space
, isl_dim_set
,
8734 space
, isl_dim_set
);
8735 isl_space_free(space
);
8739 isl_die(isl_multi_union_pw_aff_get_ctx(mupa
), isl_error_invalid
,
8740 "space don't match", goto error
);
8742 return mupa_intersect_range_0D(mupa
, range
);
8744 upma
= isl_union_pw_multi_aff_from_multi_union_pw_aff(
8745 isl_multi_union_pw_aff_copy(mupa
));
8746 domain
= isl_union_set_from_set(range
);
8747 domain
= isl_union_set_preimage_union_pw_multi_aff(domain
, upma
);
8748 mupa
= isl_multi_union_pw_aff_intersect_domain(mupa
, domain
);
8752 isl_multi_union_pw_aff_free(mupa
);
8753 isl_set_free(range
);
8757 /* Return the shared domain of the elements of "mupa",
8758 * in the special case where "mupa" is zero-dimensional.
8760 * Return the explicit domain of "mupa".
8761 * Note that this domain may be a parameter set, either
8762 * because "mupa" is meant to live in a set space or
8763 * because no explicit domain has been set.
8765 __isl_give isl_union_set
*isl_multi_union_pw_aff_domain_0D(
8766 __isl_take isl_multi_union_pw_aff
*mupa
)
8770 dom
= isl_multi_union_pw_aff_get_explicit_domain(mupa
);
8771 isl_multi_union_pw_aff_free(mupa
);
8776 /* Return the shared domain of the elements of "mupa".
8778 * If "mupa" is zero-dimensional, then return its explicit domain.
8780 __isl_give isl_union_set
*isl_multi_union_pw_aff_domain(
8781 __isl_take isl_multi_union_pw_aff
*mupa
)
8785 isl_union_pw_aff
*upa
;
8788 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8790 mupa
= isl_multi_union_pw_aff_free(mupa
);
8795 return isl_multi_union_pw_aff_domain_0D(mupa
);
8797 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, 0);
8798 dom
= isl_union_pw_aff_domain(upa
);
8799 for (i
= 1; i
< n
; ++i
) {
8800 isl_union_set
*dom_i
;
8802 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
8803 dom_i
= isl_union_pw_aff_domain(upa
);
8804 dom
= isl_union_set_intersect(dom
, dom_i
);
8807 isl_multi_union_pw_aff_free(mupa
);
8811 /* Apply "aff" to "mupa". The space of "mupa" is equal to the domain of "aff".
8812 * In particular, the spaces have been aligned.
8813 * The result is defined over the shared domain of the elements of "mupa"
8815 * We first extract the parametric constant part of "aff" and
8816 * define that over the shared domain.
8817 * Then we iterate over all input dimensions of "aff" and add the corresponding
8818 * multiples of the elements of "mupa".
8819 * Finally, we consider the integer divisions, calling the function
8820 * recursively to obtain an isl_union_pw_aff corresponding to the
8821 * integer division argument.
8823 static __isl_give isl_union_pw_aff
*multi_union_pw_aff_apply_aff(
8824 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_aff
*aff
)
8827 isl_size n_in
, n_div
;
8828 isl_union_pw_aff
*upa
;
8829 isl_union_set
*uset
;
8833 n_in
= isl_aff_dim(aff
, isl_dim_in
);
8834 n_div
= isl_aff_dim(aff
, isl_dim_div
);
8835 if (n_in
< 0 || n_div
< 0)
8838 uset
= isl_multi_union_pw_aff_domain(isl_multi_union_pw_aff_copy(mupa
));
8839 cst
= isl_aff_copy(aff
);
8840 cst
= isl_aff_drop_dims(cst
, isl_dim_div
, 0, n_div
);
8841 cst
= isl_aff_drop_dims(cst
, isl_dim_in
, 0, n_in
);
8842 cst
= isl_aff_project_domain_on_params(cst
);
8843 upa
= isl_union_pw_aff_aff_on_domain(uset
, cst
);
8845 for (i
= 0; i
< n_in
; ++i
) {
8846 isl_union_pw_aff
*upa_i
;
8848 if (!isl_aff_involves_dims(aff
, isl_dim_in
, i
, 1))
8850 v
= isl_aff_get_coefficient_val(aff
, isl_dim_in
, i
);
8851 upa_i
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
8852 upa_i
= isl_union_pw_aff_scale_val(upa_i
, v
);
8853 upa
= isl_union_pw_aff_add(upa
, upa_i
);
8856 for (i
= 0; i
< n_div
; ++i
) {
8858 isl_union_pw_aff
*upa_i
;
8860 if (!isl_aff_involves_dims(aff
, isl_dim_div
, i
, 1))
8862 div
= isl_aff_get_div(aff
, i
);
8863 upa_i
= multi_union_pw_aff_apply_aff(
8864 isl_multi_union_pw_aff_copy(mupa
), div
);
8865 upa_i
= isl_union_pw_aff_floor(upa_i
);
8866 v
= isl_aff_get_coefficient_val(aff
, isl_dim_div
, i
);
8867 upa_i
= isl_union_pw_aff_scale_val(upa_i
, v
);
8868 upa
= isl_union_pw_aff_add(upa
, upa_i
);
8871 isl_multi_union_pw_aff_free(mupa
);
8876 isl_multi_union_pw_aff_free(mupa
);
8881 /* Apply "aff" to "mupa". The space of "mupa" needs to be compatible
8882 * with the domain of "aff".
8883 * Furthermore, the dimension of this space needs to be greater than zero.
8884 * The result is defined over the shared domain of the elements of "mupa"
8886 * We perform these checks and then hand over control to
8887 * multi_union_pw_aff_apply_aff.
8889 __isl_give isl_union_pw_aff
*isl_multi_union_pw_aff_apply_aff(
8890 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_aff
*aff
)
8893 isl_space
*space1
, *space2
;
8896 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
8897 isl_aff_get_space(aff
));
8898 aff
= isl_aff_align_params(aff
, isl_multi_union_pw_aff_get_space(mupa
));
8902 space1
= isl_multi_union_pw_aff_get_space(mupa
);
8903 space2
= isl_aff_get_domain_space(aff
);
8904 equal
= isl_space_is_equal(space1
, space2
);
8905 isl_space_free(space1
);
8906 isl_space_free(space2
);
8910 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
8911 "spaces don't match", goto error
);
8912 dim
= isl_aff_dim(aff
, isl_dim_in
);
8916 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
8917 "cannot determine domains", goto error
);
8919 return multi_union_pw_aff_apply_aff(mupa
, aff
);
8921 isl_multi_union_pw_aff_free(mupa
);
8926 /* Apply "ma" to "mupa", in the special case where "mupa" is 0D.
8927 * The space of "mupa" is known to be compatible with the domain of "ma".
8929 * Construct an isl_multi_union_pw_aff that is equal to "ma"
8930 * on the domain of "mupa".
8932 static __isl_give isl_multi_union_pw_aff
*mupa_apply_multi_aff_0D(
8933 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_multi_aff
*ma
)
8937 dom
= isl_multi_union_pw_aff_domain(mupa
);
8938 ma
= isl_multi_aff_project_domain_on_params(ma
);
8940 return isl_multi_union_pw_aff_multi_aff_on_domain(dom
, ma
);
8943 /* Apply "ma" to "mupa". The space of "mupa" needs to be compatible
8944 * with the domain of "ma".
8945 * The result is defined over the shared domain of the elements of "mupa"
8947 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_apply_multi_aff(
8948 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_multi_aff
*ma
)
8950 isl_space
*space1
, *space2
;
8951 isl_multi_union_pw_aff
*res
;
8954 isl_size n_in
, n_out
;
8956 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
8957 isl_multi_aff_get_space(ma
));
8958 ma
= isl_multi_aff_align_params(ma
,
8959 isl_multi_union_pw_aff_get_space(mupa
));
8960 n_in
= isl_multi_aff_dim(ma
, isl_dim_in
);
8961 n_out
= isl_multi_aff_dim(ma
, isl_dim_out
);
8962 if (!mupa
|| n_in
< 0 || n_out
< 0)
8965 space1
= isl_multi_union_pw_aff_get_space(mupa
);
8966 space2
= isl_multi_aff_get_domain_space(ma
);
8967 equal
= isl_space_is_equal(space1
, space2
);
8968 isl_space_free(space1
);
8969 isl_space_free(space2
);
8973 isl_die(isl_multi_aff_get_ctx(ma
), isl_error_invalid
,
8974 "spaces don't match", goto error
);
8976 return mupa_apply_multi_aff_0D(mupa
, ma
);
8978 space1
= isl_space_range(isl_multi_aff_get_space(ma
));
8979 res
= isl_multi_union_pw_aff_alloc(space1
);
8981 for (i
= 0; i
< n_out
; ++i
) {
8983 isl_union_pw_aff
*upa
;
8985 aff
= isl_multi_aff_get_aff(ma
, i
);
8986 upa
= multi_union_pw_aff_apply_aff(
8987 isl_multi_union_pw_aff_copy(mupa
), aff
);
8988 res
= isl_multi_union_pw_aff_set_union_pw_aff(res
, i
, upa
);
8991 isl_multi_aff_free(ma
);
8992 isl_multi_union_pw_aff_free(mupa
);
8995 isl_multi_union_pw_aff_free(mupa
);
8996 isl_multi_aff_free(ma
);
9000 /* Apply "pa" to "mupa", in the special case where "mupa" is 0D.
9001 * The space of "mupa" is known to be compatible with the domain of "pa".
9003 * Construct an isl_multi_union_pw_aff that is equal to "pa"
9004 * on the domain of "mupa".
9006 static __isl_give isl_union_pw_aff
*isl_multi_union_pw_aff_apply_pw_aff_0D(
9007 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_pw_aff
*pa
)
9011 dom
= isl_multi_union_pw_aff_domain(mupa
);
9012 pa
= isl_pw_aff_project_domain_on_params(pa
);
9014 return isl_union_pw_aff_pw_aff_on_domain(dom
, pa
);
9017 /* Apply "pa" to "mupa". The space of "mupa" needs to be compatible
9018 * with the domain of "pa".
9019 * Furthermore, the dimension of this space needs to be greater than zero.
9020 * The result is defined over the shared domain of the elements of "mupa"
9022 __isl_give isl_union_pw_aff
*isl_multi_union_pw_aff_apply_pw_aff(
9023 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_pw_aff
*pa
)
9028 isl_space
*space
, *space2
;
9029 isl_union_pw_aff
*upa
;
9031 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
9032 isl_pw_aff_get_space(pa
));
9033 pa
= isl_pw_aff_align_params(pa
,
9034 isl_multi_union_pw_aff_get_space(mupa
));
9038 space
= isl_multi_union_pw_aff_get_space(mupa
);
9039 space2
= isl_pw_aff_get_domain_space(pa
);
9040 equal
= isl_space_is_equal(space
, space2
);
9041 isl_space_free(space
);
9042 isl_space_free(space2
);
9046 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
9047 "spaces don't match", goto error
);
9048 n_in
= isl_pw_aff_dim(pa
, isl_dim_in
);
9052 return isl_multi_union_pw_aff_apply_pw_aff_0D(mupa
, pa
);
9054 space
= isl_space_params(isl_multi_union_pw_aff_get_space(mupa
));
9055 upa
= isl_union_pw_aff_empty(space
);
9057 for (i
= 0; i
< pa
->n
; ++i
) {
9060 isl_multi_union_pw_aff
*mupa_i
;
9061 isl_union_pw_aff
*upa_i
;
9063 mupa_i
= isl_multi_union_pw_aff_copy(mupa
);
9064 domain
= isl_set_copy(pa
->p
[i
].set
);
9065 mupa_i
= isl_multi_union_pw_aff_intersect_range(mupa_i
, domain
);
9066 aff
= isl_aff_copy(pa
->p
[i
].aff
);
9067 upa_i
= multi_union_pw_aff_apply_aff(mupa_i
, aff
);
9068 upa
= isl_union_pw_aff_union_add(upa
, upa_i
);
9071 isl_multi_union_pw_aff_free(mupa
);
9072 isl_pw_aff_free(pa
);
9075 isl_multi_union_pw_aff_free(mupa
);
9076 isl_pw_aff_free(pa
);
9080 /* Apply "pma" to "mupa", in the special case where "mupa" is 0D.
9081 * The space of "mupa" is known to be compatible with the domain of "pma".
9083 * Construct an isl_multi_union_pw_aff that is equal to "pma"
9084 * on the domain of "mupa".
9086 static __isl_give isl_multi_union_pw_aff
*mupa_apply_pw_multi_aff_0D(
9087 __isl_take isl_multi_union_pw_aff
*mupa
,
9088 __isl_take isl_pw_multi_aff
*pma
)
9092 dom
= isl_multi_union_pw_aff_domain(mupa
);
9093 pma
= isl_pw_multi_aff_project_domain_on_params(pma
);
9095 return isl_multi_union_pw_aff_pw_multi_aff_on_domain(dom
, pma
);
9098 /* Apply "pma" to "mupa". The space of "mupa" needs to be compatible
9099 * with the domain of "pma".
9100 * The result is defined over the shared domain of the elements of "mupa"
9102 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_apply_pw_multi_aff(
9103 __isl_take isl_multi_union_pw_aff
*mupa
,
9104 __isl_take isl_pw_multi_aff
*pma
)
9106 isl_space
*space1
, *space2
;
9107 isl_multi_union_pw_aff
*res
;
9110 isl_size n_in
, n_out
;
9112 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
9113 isl_pw_multi_aff_get_space(pma
));
9114 pma
= isl_pw_multi_aff_align_params(pma
,
9115 isl_multi_union_pw_aff_get_space(mupa
));
9119 space1
= isl_multi_union_pw_aff_get_space(mupa
);
9120 space2
= isl_pw_multi_aff_get_domain_space(pma
);
9121 equal
= isl_space_is_equal(space1
, space2
);
9122 isl_space_free(space1
);
9123 isl_space_free(space2
);
9127 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
9128 "spaces don't match", goto error
);
9129 n_in
= isl_pw_multi_aff_dim(pma
, isl_dim_in
);
9130 n_out
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
9131 if (n_in
< 0 || n_out
< 0)
9134 return mupa_apply_pw_multi_aff_0D(mupa
, pma
);
9136 space1
= isl_space_range(isl_pw_multi_aff_get_space(pma
));
9137 res
= isl_multi_union_pw_aff_alloc(space1
);
9139 for (i
= 0; i
< n_out
; ++i
) {
9141 isl_union_pw_aff
*upa
;
9143 pa
= isl_pw_multi_aff_get_pw_aff(pma
, i
);
9144 upa
= isl_multi_union_pw_aff_apply_pw_aff(
9145 isl_multi_union_pw_aff_copy(mupa
), pa
);
9146 res
= isl_multi_union_pw_aff_set_union_pw_aff(res
, i
, upa
);
9149 isl_pw_multi_aff_free(pma
);
9150 isl_multi_union_pw_aff_free(mupa
);
9153 isl_multi_union_pw_aff_free(mupa
);
9154 isl_pw_multi_aff_free(pma
);
9158 /* Replace the explicit domain of "mupa" by its preimage under "upma".
9159 * If the explicit domain only keeps track of constraints on the parameters,
9160 * then only update those constraints.
9162 static __isl_give isl_multi_union_pw_aff
*preimage_explicit_domain(
9163 __isl_take isl_multi_union_pw_aff
*mupa
,
9164 __isl_keep isl_union_pw_multi_aff
*upma
)
9168 if (isl_multi_union_pw_aff_check_has_explicit_domain(mupa
) < 0)
9169 return isl_multi_union_pw_aff_free(mupa
);
9171 mupa
= isl_multi_union_pw_aff_cow(mupa
);
9175 is_params
= isl_union_set_is_params(mupa
->u
.dom
);
9177 return isl_multi_union_pw_aff_free(mupa
);
9179 upma
= isl_union_pw_multi_aff_copy(upma
);
9181 mupa
->u
.dom
= isl_union_set_intersect_params(mupa
->u
.dom
,
9182 isl_union_set_params(isl_union_pw_multi_aff_domain(upma
)));
9184 mupa
->u
.dom
= isl_union_set_preimage_union_pw_multi_aff(
9187 return isl_multi_union_pw_aff_free(mupa
);
9191 /* Compute the pullback of "mupa" by the function represented by "upma".
9192 * In other words, plug in "upma" in "mupa". The result contains
9193 * expressions defined over the domain space of "upma".
9195 * Run over all elements of "mupa" and plug in "upma" in each of them.
9197 * If "mupa" has an explicit domain, then it is this domain
9198 * that needs to undergo a pullback instead, i.e., a preimage.
9200 __isl_give isl_multi_union_pw_aff
*
9201 isl_multi_union_pw_aff_pullback_union_pw_multi_aff(
9202 __isl_take isl_multi_union_pw_aff
*mupa
,
9203 __isl_take isl_union_pw_multi_aff
*upma
)
9208 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
9209 isl_union_pw_multi_aff_get_space(upma
));
9210 upma
= isl_union_pw_multi_aff_align_params(upma
,
9211 isl_multi_union_pw_aff_get_space(mupa
));
9212 mupa
= isl_multi_union_pw_aff_cow(mupa
);
9213 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
9217 for (i
= 0; i
< n
; ++i
) {
9218 isl_union_pw_aff
*upa
;
9220 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
9221 upa
= isl_union_pw_aff_pullback_union_pw_multi_aff(upa
,
9222 isl_union_pw_multi_aff_copy(upma
));
9223 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
9226 if (isl_multi_union_pw_aff_has_explicit_domain(mupa
))
9227 mupa
= preimage_explicit_domain(mupa
, upma
);
9229 isl_union_pw_multi_aff_free(upma
);
9232 isl_multi_union_pw_aff_free(mupa
);
9233 isl_union_pw_multi_aff_free(upma
);
9237 /* Extract the sequence of elements in "mupa" with domain space "space"
9238 * (ignoring parameters).
9240 * For the elements of "mupa" that are not defined on the specified space,
9241 * the corresponding element in the result is empty.
9243 __isl_give isl_multi_pw_aff
*isl_multi_union_pw_aff_extract_multi_pw_aff(
9244 __isl_keep isl_multi_union_pw_aff
*mupa
, __isl_take isl_space
*space
)
9248 isl_space
*space_mpa
;
9249 isl_multi_pw_aff
*mpa
;
9251 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
9252 if (n
< 0 || !space
)
9255 space_mpa
= isl_multi_union_pw_aff_get_space(mupa
);
9256 space
= isl_space_replace_params(space
, space_mpa
);
9257 space_mpa
= isl_space_map_from_domain_and_range(isl_space_copy(space
),
9259 mpa
= isl_multi_pw_aff_alloc(space_mpa
);
9261 space
= isl_space_from_domain(space
);
9262 space
= isl_space_add_dims(space
, isl_dim_out
, 1);
9263 for (i
= 0; i
< n
; ++i
) {
9264 isl_union_pw_aff
*upa
;
9267 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
9268 pa
= isl_union_pw_aff_extract_pw_aff(upa
,
9269 isl_space_copy(space
));
9270 mpa
= isl_multi_pw_aff_set_pw_aff(mpa
, i
, pa
);
9271 isl_union_pw_aff_free(upa
);
9274 isl_space_free(space
);
9277 isl_space_free(space
);
9281 /* Evaluate the affine function "aff" in the void point "pnt".
9282 * In particular, return the value NaN.
9284 static __isl_give isl_val
*eval_void(__isl_take isl_aff
*aff
,
9285 __isl_take isl_point
*pnt
)
9289 ctx
= isl_point_get_ctx(pnt
);
9291 isl_point_free(pnt
);
9292 return isl_val_nan(ctx
);
9295 /* Evaluate the affine expression "aff"
9296 * in the coordinates (with denominator) "pnt".
9298 static __isl_give isl_val
*eval(__isl_keep isl_vec
*aff
,
9299 __isl_keep isl_vec
*pnt
)
9308 ctx
= isl_vec_get_ctx(aff
);
9311 isl_seq_inner_product(aff
->el
+ 1, pnt
->el
, pnt
->size
, &n
);
9312 isl_int_mul(d
, aff
->el
[0], pnt
->el
[0]);
9313 v
= isl_val_rat_from_isl_int(ctx
, n
, d
);
9314 v
= isl_val_normalize(v
);
9321 /* Check that the domain space of "aff" is equal to "space".
9323 static isl_stat
isl_aff_check_has_domain_space(__isl_keep isl_aff
*aff
,
9324 __isl_keep isl_space
*space
)
9328 ok
= isl_space_is_equal(isl_aff_peek_domain_space(aff
), space
);
9330 return isl_stat_error
;
9332 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
9333 "incompatible spaces", return isl_stat_error
);
9337 /* Evaluate the affine function "aff" in "pnt".
9339 __isl_give isl_val
*isl_aff_eval(__isl_take isl_aff
*aff
,
9340 __isl_take isl_point
*pnt
)
9344 isl_local_space
*ls
;
9346 if (isl_aff_check_has_domain_space(aff
, isl_point_peek_space(pnt
)) < 0)
9348 is_void
= isl_point_is_void(pnt
);
9352 return eval_void(aff
, pnt
);
9354 ls
= isl_aff_get_domain_local_space(aff
);
9355 pnt
= isl_local_space_lift_point(ls
, pnt
);
9357 v
= eval(aff
->v
, isl_point_peek_vec(pnt
));
9360 isl_point_free(pnt
);
9365 isl_point_free(pnt
);