2 * Copyright 2011 INRIA Saclay
3 * Copyright 2011 Sven Verdoolaege
4 * Copyright 2012-2014 Ecole Normale Superieure
5 * Copyright 2014 INRIA Rocquencourt
7 * Use of this software is governed by the MIT license
9 * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
10 * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
12 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
13 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
14 * B.P. 105 - 78153 Le Chesnay, France
17 #include <isl_ctx_private.h>
18 #include <isl_map_private.h>
19 #include <isl_union_map_private.h>
20 #include <isl_aff_private.h>
21 #include <isl_space_private.h>
22 #include <isl_local_space_private.h>
23 #include <isl_vec_private.h>
24 #include <isl_mat_private.h>
26 #include <isl/constraint.h>
29 #include <isl_val_private.h>
30 #include <isl_point_private.h>
31 #include <isl_config.h>
36 #include <isl_list_templ.c>
41 #include <isl_list_templ.c>
44 #define BASE pw_multi_aff
46 #include <isl_list_templ.c>
49 #define BASE union_pw_aff
51 #include <isl_list_templ.c>
54 #define BASE union_pw_multi_aff
56 #include <isl_list_templ.c>
58 __isl_give isl_aff
*isl_aff_alloc_vec(__isl_take isl_local_space
*ls
,
59 __isl_take isl_vec
*v
)
66 aff
= isl_calloc_type(v
->ctx
, struct isl_aff
);
76 isl_local_space_free(ls
);
81 __isl_give isl_aff
*isl_aff_alloc(__isl_take isl_local_space
*ls
)
90 ctx
= isl_local_space_get_ctx(ls
);
91 if (!isl_local_space_divs_known(ls
))
92 isl_die(ctx
, isl_error_invalid
, "local space has unknown divs",
94 if (!isl_local_space_is_set(ls
))
95 isl_die(ctx
, isl_error_invalid
,
96 "domain of affine expression should be a set",
99 total
= isl_local_space_dim(ls
, isl_dim_all
);
100 v
= isl_vec_alloc(ctx
, 1 + 1 + total
);
101 return isl_aff_alloc_vec(ls
, v
);
103 isl_local_space_free(ls
);
107 __isl_give isl_aff
*isl_aff_zero_on_domain(__isl_take isl_local_space
*ls
)
111 aff
= isl_aff_alloc(ls
);
115 isl_int_set_si(aff
->v
->el
[0], 1);
116 isl_seq_clr(aff
->v
->el
+ 1, aff
->v
->size
- 1);
121 /* Return a piecewise affine expression defined on the specified domain
122 * that is equal to zero.
124 __isl_give isl_pw_aff
*isl_pw_aff_zero_on_domain(__isl_take isl_local_space
*ls
)
126 return isl_pw_aff_from_aff(isl_aff_zero_on_domain(ls
));
129 /* Return an affine expression defined on the specified domain
130 * that represents NaN.
132 __isl_give isl_aff
*isl_aff_nan_on_domain(__isl_take isl_local_space
*ls
)
136 aff
= isl_aff_alloc(ls
);
140 isl_seq_clr(aff
->v
->el
, aff
->v
->size
);
145 /* Return a piecewise affine expression defined on the specified domain
146 * that represents NaN.
148 __isl_give isl_pw_aff
*isl_pw_aff_nan_on_domain(__isl_take isl_local_space
*ls
)
150 return isl_pw_aff_from_aff(isl_aff_nan_on_domain(ls
));
153 /* Return an affine expression that is equal to "val" on
154 * domain local space "ls".
156 __isl_give isl_aff
*isl_aff_val_on_domain(__isl_take isl_local_space
*ls
,
157 __isl_take isl_val
*val
)
163 if (!isl_val_is_rat(val
))
164 isl_die(isl_val_get_ctx(val
), isl_error_invalid
,
165 "expecting rational value", goto error
);
167 aff
= isl_aff_alloc(isl_local_space_copy(ls
));
171 isl_seq_clr(aff
->v
->el
+ 2, aff
->v
->size
- 2);
172 isl_int_set(aff
->v
->el
[1], val
->n
);
173 isl_int_set(aff
->v
->el
[0], val
->d
);
175 isl_local_space_free(ls
);
179 isl_local_space_free(ls
);
184 /* Return an affine expression that is equal to the specified dimension
187 __isl_give isl_aff
*isl_aff_var_on_domain(__isl_take isl_local_space
*ls
,
188 enum isl_dim_type type
, unsigned pos
)
196 space
= isl_local_space_get_space(ls
);
199 if (isl_space_is_map(space
))
200 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
201 "expecting (parameter) set space", goto error
);
202 if (pos
>= isl_local_space_dim(ls
, type
))
203 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
204 "position out of bounds", goto error
);
206 isl_space_free(space
);
207 aff
= isl_aff_alloc(ls
);
211 pos
+= isl_local_space_offset(aff
->ls
, type
);
213 isl_int_set_si(aff
->v
->el
[0], 1);
214 isl_seq_clr(aff
->v
->el
+ 1, aff
->v
->size
- 1);
215 isl_int_set_si(aff
->v
->el
[1 + pos
], 1);
219 isl_local_space_free(ls
);
220 isl_space_free(space
);
224 /* Return a piecewise affine expression that is equal to
225 * the specified dimension in "ls".
227 __isl_give isl_pw_aff
*isl_pw_aff_var_on_domain(__isl_take isl_local_space
*ls
,
228 enum isl_dim_type type
, unsigned pos
)
230 return isl_pw_aff_from_aff(isl_aff_var_on_domain(ls
, type
, pos
));
233 /* Return an affine expression that is equal to the parameter
234 * in the domain space "space" with identifier "id".
236 __isl_give isl_aff
*isl_aff_param_on_domain_space_id(
237 __isl_take isl_space
*space
, __isl_take isl_id
*id
)
244 pos
= isl_space_find_dim_by_id(space
, isl_dim_param
, id
);
246 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
247 "parameter not found in space", goto error
);
249 ls
= isl_local_space_from_space(space
);
250 return isl_aff_var_on_domain(ls
, isl_dim_param
, pos
);
252 isl_space_free(space
);
257 __isl_give isl_aff
*isl_aff_copy(__isl_keep isl_aff
*aff
)
266 __isl_give isl_aff
*isl_aff_dup(__isl_keep isl_aff
*aff
)
271 return isl_aff_alloc_vec(isl_local_space_copy(aff
->ls
),
272 isl_vec_copy(aff
->v
));
275 __isl_give isl_aff
*isl_aff_cow(__isl_take isl_aff
*aff
)
283 return isl_aff_dup(aff
);
286 __isl_null isl_aff
*isl_aff_free(__isl_take isl_aff
*aff
)
294 isl_local_space_free(aff
->ls
);
295 isl_vec_free(aff
->v
);
302 isl_ctx
*isl_aff_get_ctx(__isl_keep isl_aff
*aff
)
304 return aff
? isl_local_space_get_ctx(aff
->ls
) : NULL
;
307 /* Return a hash value that digests "aff".
309 uint32_t isl_aff_get_hash(__isl_keep isl_aff
*aff
)
311 uint32_t hash
, ls_hash
, v_hash
;
316 hash
= isl_hash_init();
317 ls_hash
= isl_local_space_get_hash(aff
->ls
);
318 isl_hash_hash(hash
, ls_hash
);
319 v_hash
= isl_vec_get_hash(aff
->v
);
320 isl_hash_hash(hash
, v_hash
);
325 /* Externally, an isl_aff has a map space, but internally, the
326 * ls field corresponds to the domain of that space.
328 int isl_aff_dim(__isl_keep isl_aff
*aff
, enum isl_dim_type type
)
332 if (type
== isl_dim_out
)
334 if (type
== isl_dim_in
)
336 return isl_local_space_dim(aff
->ls
, type
);
339 /* Return the position of the dimension of the given type and name
341 * Return -1 if no such dimension can be found.
343 int isl_aff_find_dim_by_name(__isl_keep isl_aff
*aff
, enum isl_dim_type type
,
348 if (type
== isl_dim_out
)
350 if (type
== isl_dim_in
)
352 return isl_local_space_find_dim_by_name(aff
->ls
, type
, name
);
355 /* Return the domain space of "aff".
357 static __isl_keep isl_space
*isl_aff_peek_domain_space(__isl_keep isl_aff
*aff
)
359 return aff
? isl_local_space_peek_space(aff
->ls
) : NULL
;
362 __isl_give isl_space
*isl_aff_get_domain_space(__isl_keep isl_aff
*aff
)
364 return isl_space_copy(isl_aff_peek_domain_space(aff
));
367 __isl_give isl_space
*isl_aff_get_space(__isl_keep isl_aff
*aff
)
372 space
= isl_local_space_get_space(aff
->ls
);
373 space
= isl_space_from_domain(space
);
374 space
= isl_space_add_dims(space
, isl_dim_out
, 1);
378 __isl_give isl_local_space
*isl_aff_get_domain_local_space(
379 __isl_keep isl_aff
*aff
)
381 return aff
? isl_local_space_copy(aff
->ls
) : NULL
;
384 __isl_give isl_local_space
*isl_aff_get_local_space(__isl_keep isl_aff
*aff
)
389 ls
= isl_local_space_copy(aff
->ls
);
390 ls
= isl_local_space_from_domain(ls
);
391 ls
= isl_local_space_add_dims(ls
, isl_dim_out
, 1);
395 /* Return the local space of the domain of "aff".
396 * This may be either a copy or the local space itself
397 * if there is only one reference to "aff".
398 * This allows the local space to be modified inplace
399 * if both the expression and its local space have only a single reference.
400 * The caller is not allowed to modify "aff" between this call and
401 * a subsequent call to isl_aff_restore_domain_local_space.
402 * The only exception is that isl_aff_free can be called instead.
404 __isl_give isl_local_space
*isl_aff_take_domain_local_space(
405 __isl_keep isl_aff
*aff
)
412 return isl_aff_get_domain_local_space(aff
);
418 /* Set the local space of the domain of "aff" to "ls",
419 * where the local space of "aff" may be missing
420 * due to a preceding call to isl_aff_take_domain_local_space.
421 * However, in this case, "aff" only has a single reference and
422 * then the call to isl_aff_cow has no effect.
424 __isl_give isl_aff
*isl_aff_restore_domain_local_space(
425 __isl_keep isl_aff
*aff
, __isl_take isl_local_space
*ls
)
431 isl_local_space_free(ls
);
435 aff
= isl_aff_cow(aff
);
438 isl_local_space_free(aff
->ls
);
444 isl_local_space_free(ls
);
448 /* Externally, an isl_aff has a map space, but internally, the
449 * ls field corresponds to the domain of that space.
451 const char *isl_aff_get_dim_name(__isl_keep isl_aff
*aff
,
452 enum isl_dim_type type
, unsigned pos
)
456 if (type
== isl_dim_out
)
458 if (type
== isl_dim_in
)
460 return isl_local_space_get_dim_name(aff
->ls
, type
, pos
);
463 __isl_give isl_aff
*isl_aff_reset_domain_space(__isl_take isl_aff
*aff
,
464 __isl_take isl_space
*dim
)
466 aff
= isl_aff_cow(aff
);
470 aff
->ls
= isl_local_space_reset_space(aff
->ls
, dim
);
472 return isl_aff_free(aff
);
481 /* Reset the space of "aff". This function is called from isl_pw_templ.c
482 * and doesn't know if the space of an element object is represented
483 * directly or through its domain. It therefore passes along both.
485 __isl_give isl_aff
*isl_aff_reset_space_and_domain(__isl_take isl_aff
*aff
,
486 __isl_take isl_space
*space
, __isl_take isl_space
*domain
)
488 isl_space_free(space
);
489 return isl_aff_reset_domain_space(aff
, domain
);
492 /* Reorder the coefficients of the affine expression based
493 * on the given reordering.
494 * The reordering r is assumed to have been extended with the local
497 static __isl_give isl_vec
*vec_reorder(__isl_take isl_vec
*vec
,
498 __isl_take isl_reordering
*r
, int n_div
)
507 space
= isl_reordering_peek_space(r
);
508 res
= isl_vec_alloc(vec
->ctx
,
509 2 + isl_space_dim(space
, isl_dim_all
) + n_div
);
512 isl_seq_cpy(res
->el
, vec
->el
, 2);
513 isl_seq_clr(res
->el
+ 2, res
->size
- 2);
514 for (i
= 0; i
< r
->len
; ++i
)
515 isl_int_set(res
->el
[2 + r
->pos
[i
]], vec
->el
[2 + i
]);
517 isl_reordering_free(r
);
522 isl_reordering_free(r
);
526 /* Reorder the dimensions of the domain of "aff" according
527 * to the given reordering.
529 __isl_give isl_aff
*isl_aff_realign_domain(__isl_take isl_aff
*aff
,
530 __isl_take isl_reordering
*r
)
532 aff
= isl_aff_cow(aff
);
536 r
= isl_reordering_extend(r
, aff
->ls
->div
->n_row
);
537 aff
->v
= vec_reorder(aff
->v
, isl_reordering_copy(r
),
538 aff
->ls
->div
->n_row
);
539 aff
->ls
= isl_local_space_realign(aff
->ls
, r
);
541 if (!aff
->v
|| !aff
->ls
)
542 return isl_aff_free(aff
);
547 isl_reordering_free(r
);
551 __isl_give isl_aff
*isl_aff_align_params(__isl_take isl_aff
*aff
,
552 __isl_take isl_space
*model
)
554 isl_bool equal_params
;
559 equal_params
= isl_space_has_equal_params(aff
->ls
->dim
, model
);
560 if (equal_params
< 0)
565 exp
= isl_parameter_alignment_reordering(aff
->ls
->dim
, model
);
566 exp
= isl_reordering_extend_space(exp
,
567 isl_aff_get_domain_space(aff
));
568 aff
= isl_aff_realign_domain(aff
, exp
);
571 isl_space_free(model
);
574 isl_space_free(model
);
579 /* Is "aff" obviously equal to zero?
581 * If the denominator is zero, then "aff" is not equal to zero.
583 isl_bool
isl_aff_plain_is_zero(__isl_keep isl_aff
*aff
)
586 return isl_bool_error
;
588 if (isl_int_is_zero(aff
->v
->el
[0]))
589 return isl_bool_false
;
590 return isl_seq_first_non_zero(aff
->v
->el
+ 1, aff
->v
->size
- 1) < 0;
593 /* Does "aff" represent NaN?
595 isl_bool
isl_aff_is_nan(__isl_keep isl_aff
*aff
)
598 return isl_bool_error
;
600 return isl_seq_first_non_zero(aff
->v
->el
, 2) < 0;
603 /* Are "aff1" and "aff2" obviously equal?
605 * NaN is not equal to anything, not even to another NaN.
607 isl_bool
isl_aff_plain_is_equal(__isl_keep isl_aff
*aff1
,
608 __isl_keep isl_aff
*aff2
)
613 return isl_bool_error
;
615 if (isl_aff_is_nan(aff1
) || isl_aff_is_nan(aff2
))
616 return isl_bool_false
;
618 equal
= isl_local_space_is_equal(aff1
->ls
, aff2
->ls
);
619 if (equal
< 0 || !equal
)
622 return isl_vec_is_equal(aff1
->v
, aff2
->v
);
625 /* Return the common denominator of "aff" in "v".
627 * We cannot return anything meaningful in case of a NaN.
629 isl_stat
isl_aff_get_denominator(__isl_keep isl_aff
*aff
, isl_int
*v
)
632 return isl_stat_error
;
633 if (isl_aff_is_nan(aff
))
634 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
635 "cannot get denominator of NaN", return isl_stat_error
);
636 isl_int_set(*v
, aff
->v
->el
[0]);
640 /* Return the common denominator of "aff".
642 __isl_give isl_val
*isl_aff_get_denominator_val(__isl_keep isl_aff
*aff
)
649 ctx
= isl_aff_get_ctx(aff
);
650 if (isl_aff_is_nan(aff
))
651 return isl_val_nan(ctx
);
652 return isl_val_int_from_isl_int(ctx
, aff
->v
->el
[0]);
655 /* Return the constant term of "aff".
657 __isl_give isl_val
*isl_aff_get_constant_val(__isl_keep isl_aff
*aff
)
665 ctx
= isl_aff_get_ctx(aff
);
666 if (isl_aff_is_nan(aff
))
667 return isl_val_nan(ctx
);
668 v
= isl_val_rat_from_isl_int(ctx
, aff
->v
->el
[1], aff
->v
->el
[0]);
669 return isl_val_normalize(v
);
672 /* Return the coefficient of the variable of type "type" at position "pos"
675 __isl_give isl_val
*isl_aff_get_coefficient_val(__isl_keep isl_aff
*aff
,
676 enum isl_dim_type type
, int pos
)
684 ctx
= isl_aff_get_ctx(aff
);
685 if (type
== isl_dim_out
)
686 isl_die(ctx
, isl_error_invalid
,
687 "output/set dimension does not have a coefficient",
689 if (type
== isl_dim_in
)
692 if (pos
>= isl_local_space_dim(aff
->ls
, type
))
693 isl_die(ctx
, isl_error_invalid
,
694 "position out of bounds", return NULL
);
696 if (isl_aff_is_nan(aff
))
697 return isl_val_nan(ctx
);
698 pos
+= isl_local_space_offset(aff
->ls
, type
);
699 v
= isl_val_rat_from_isl_int(ctx
, aff
->v
->el
[1 + pos
], aff
->v
->el
[0]);
700 return isl_val_normalize(v
);
703 /* Return the sign of the coefficient of the variable of type "type"
704 * at position "pos" of "aff".
706 int isl_aff_coefficient_sgn(__isl_keep isl_aff
*aff
, enum isl_dim_type type
,
714 ctx
= isl_aff_get_ctx(aff
);
715 if (type
== isl_dim_out
)
716 isl_die(ctx
, isl_error_invalid
,
717 "output/set dimension does not have a coefficient",
719 if (type
== isl_dim_in
)
722 if (pos
>= isl_local_space_dim(aff
->ls
, type
))
723 isl_die(ctx
, isl_error_invalid
,
724 "position out of bounds", return 0);
726 pos
+= isl_local_space_offset(aff
->ls
, type
);
727 return isl_int_sgn(aff
->v
->el
[1 + pos
]);
730 /* Replace the numerator of the constant term of "aff" by "v".
732 * A NaN is unaffected by this operation.
734 __isl_give isl_aff
*isl_aff_set_constant(__isl_take isl_aff
*aff
, isl_int v
)
738 if (isl_aff_is_nan(aff
))
740 aff
= isl_aff_cow(aff
);
744 aff
->v
= isl_vec_cow(aff
->v
);
746 return isl_aff_free(aff
);
748 isl_int_set(aff
->v
->el
[1], v
);
753 /* Replace the constant term of "aff" by "v".
755 * A NaN is unaffected by this operation.
757 __isl_give isl_aff
*isl_aff_set_constant_val(__isl_take isl_aff
*aff
,
758 __isl_take isl_val
*v
)
763 if (isl_aff_is_nan(aff
)) {
768 if (!isl_val_is_rat(v
))
769 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
770 "expecting rational value", goto error
);
772 if (isl_int_eq(aff
->v
->el
[1], v
->n
) &&
773 isl_int_eq(aff
->v
->el
[0], v
->d
)) {
778 aff
= isl_aff_cow(aff
);
781 aff
->v
= isl_vec_cow(aff
->v
);
785 if (isl_int_eq(aff
->v
->el
[0], v
->d
)) {
786 isl_int_set(aff
->v
->el
[1], v
->n
);
787 } else if (isl_int_is_one(v
->d
)) {
788 isl_int_mul(aff
->v
->el
[1], aff
->v
->el
[0], v
->n
);
790 isl_seq_scale(aff
->v
->el
+ 1,
791 aff
->v
->el
+ 1, v
->d
, aff
->v
->size
- 1);
792 isl_int_mul(aff
->v
->el
[1], aff
->v
->el
[0], v
->n
);
793 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], v
->d
);
794 aff
->v
= isl_vec_normalize(aff
->v
);
807 /* Add "v" to the constant term of "aff".
809 * A NaN is unaffected by this operation.
811 __isl_give isl_aff
*isl_aff_add_constant(__isl_take isl_aff
*aff
, isl_int v
)
813 if (isl_int_is_zero(v
))
818 if (isl_aff_is_nan(aff
))
820 aff
= isl_aff_cow(aff
);
824 aff
->v
= isl_vec_cow(aff
->v
);
826 return isl_aff_free(aff
);
828 isl_int_addmul(aff
->v
->el
[1], aff
->v
->el
[0], v
);
833 /* Add "v" to the constant term of "aff".
835 * A NaN is unaffected by this operation.
837 __isl_give isl_aff
*isl_aff_add_constant_val(__isl_take isl_aff
*aff
,
838 __isl_take isl_val
*v
)
843 if (isl_aff_is_nan(aff
) || isl_val_is_zero(v
)) {
848 if (!isl_val_is_rat(v
))
849 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
850 "expecting rational value", goto error
);
852 aff
= isl_aff_cow(aff
);
856 aff
->v
= isl_vec_cow(aff
->v
);
860 if (isl_int_is_one(v
->d
)) {
861 isl_int_addmul(aff
->v
->el
[1], aff
->v
->el
[0], v
->n
);
862 } else if (isl_int_eq(aff
->v
->el
[0], v
->d
)) {
863 isl_int_add(aff
->v
->el
[1], aff
->v
->el
[1], v
->n
);
864 aff
->v
= isl_vec_normalize(aff
->v
);
868 isl_seq_scale(aff
->v
->el
+ 1,
869 aff
->v
->el
+ 1, v
->d
, aff
->v
->size
- 1);
870 isl_int_addmul(aff
->v
->el
[1], aff
->v
->el
[0], v
->n
);
871 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], v
->d
);
872 aff
->v
= isl_vec_normalize(aff
->v
);
885 __isl_give isl_aff
*isl_aff_add_constant_si(__isl_take isl_aff
*aff
, int v
)
890 isl_int_set_si(t
, v
);
891 aff
= isl_aff_add_constant(aff
, t
);
897 /* Add "v" to the numerator of the constant term of "aff".
899 * A NaN is unaffected by this operation.
901 __isl_give isl_aff
*isl_aff_add_constant_num(__isl_take isl_aff
*aff
, isl_int v
)
903 if (isl_int_is_zero(v
))
908 if (isl_aff_is_nan(aff
))
910 aff
= isl_aff_cow(aff
);
914 aff
->v
= isl_vec_cow(aff
->v
);
916 return isl_aff_free(aff
);
918 isl_int_add(aff
->v
->el
[1], aff
->v
->el
[1], v
);
923 /* Add "v" to the numerator of the constant term of "aff".
925 * A NaN is unaffected by this operation.
927 __isl_give isl_aff
*isl_aff_add_constant_num_si(__isl_take isl_aff
*aff
, int v
)
935 isl_int_set_si(t
, v
);
936 aff
= isl_aff_add_constant_num(aff
, t
);
942 /* Replace the numerator of the constant term of "aff" by "v".
944 * A NaN is unaffected by this operation.
946 __isl_give isl_aff
*isl_aff_set_constant_si(__isl_take isl_aff
*aff
, int v
)
950 if (isl_aff_is_nan(aff
))
952 aff
= isl_aff_cow(aff
);
956 aff
->v
= isl_vec_cow(aff
->v
);
958 return isl_aff_free(aff
);
960 isl_int_set_si(aff
->v
->el
[1], v
);
965 /* Replace the numerator of the coefficient of the variable of type "type"
966 * at position "pos" of "aff" by "v".
968 * A NaN is unaffected by this operation.
970 __isl_give isl_aff
*isl_aff_set_coefficient(__isl_take isl_aff
*aff
,
971 enum isl_dim_type type
, int pos
, isl_int v
)
976 if (type
== isl_dim_out
)
977 isl_die(aff
->v
->ctx
, isl_error_invalid
,
978 "output/set dimension does not have a coefficient",
979 return isl_aff_free(aff
));
980 if (type
== isl_dim_in
)
983 if (pos
>= isl_local_space_dim(aff
->ls
, type
))
984 isl_die(aff
->v
->ctx
, isl_error_invalid
,
985 "position out of bounds", return isl_aff_free(aff
));
987 if (isl_aff_is_nan(aff
))
989 aff
= isl_aff_cow(aff
);
993 aff
->v
= isl_vec_cow(aff
->v
);
995 return isl_aff_free(aff
);
997 pos
+= isl_local_space_offset(aff
->ls
, type
);
998 isl_int_set(aff
->v
->el
[1 + pos
], v
);
1003 /* Replace the numerator of the coefficient of the variable of type "type"
1004 * at position "pos" of "aff" by "v".
1006 * A NaN is unaffected by this operation.
1008 __isl_give isl_aff
*isl_aff_set_coefficient_si(__isl_take isl_aff
*aff
,
1009 enum isl_dim_type type
, int pos
, int v
)
1014 if (type
== isl_dim_out
)
1015 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1016 "output/set dimension does not have a coefficient",
1017 return isl_aff_free(aff
));
1018 if (type
== isl_dim_in
)
1021 if (pos
< 0 || pos
>= isl_local_space_dim(aff
->ls
, type
))
1022 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1023 "position out of bounds", return isl_aff_free(aff
));
1025 if (isl_aff_is_nan(aff
))
1027 pos
+= isl_local_space_offset(aff
->ls
, type
);
1028 if (isl_int_cmp_si(aff
->v
->el
[1 + pos
], v
) == 0)
1031 aff
= isl_aff_cow(aff
);
1035 aff
->v
= isl_vec_cow(aff
->v
);
1037 return isl_aff_free(aff
);
1039 isl_int_set_si(aff
->v
->el
[1 + pos
], v
);
1044 /* Replace the coefficient of the variable of type "type" at position "pos"
1047 * A NaN is unaffected by this operation.
1049 __isl_give isl_aff
*isl_aff_set_coefficient_val(__isl_take isl_aff
*aff
,
1050 enum isl_dim_type type
, int pos
, __isl_take isl_val
*v
)
1055 if (type
== isl_dim_out
)
1056 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1057 "output/set dimension does not have a coefficient",
1059 if (type
== isl_dim_in
)
1062 if (pos
>= isl_local_space_dim(aff
->ls
, type
))
1063 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1064 "position out of bounds", goto error
);
1066 if (isl_aff_is_nan(aff
)) {
1070 if (!isl_val_is_rat(v
))
1071 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1072 "expecting rational value", goto error
);
1074 pos
+= isl_local_space_offset(aff
->ls
, type
);
1075 if (isl_int_eq(aff
->v
->el
[1 + pos
], v
->n
) &&
1076 isl_int_eq(aff
->v
->el
[0], v
->d
)) {
1081 aff
= isl_aff_cow(aff
);
1084 aff
->v
= isl_vec_cow(aff
->v
);
1088 if (isl_int_eq(aff
->v
->el
[0], v
->d
)) {
1089 isl_int_set(aff
->v
->el
[1 + pos
], v
->n
);
1090 } else if (isl_int_is_one(v
->d
)) {
1091 isl_int_mul(aff
->v
->el
[1 + pos
], aff
->v
->el
[0], v
->n
);
1093 isl_seq_scale(aff
->v
->el
+ 1,
1094 aff
->v
->el
+ 1, v
->d
, aff
->v
->size
- 1);
1095 isl_int_mul(aff
->v
->el
[1 + pos
], aff
->v
->el
[0], v
->n
);
1096 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], v
->d
);
1097 aff
->v
= isl_vec_normalize(aff
->v
);
1110 /* Add "v" to the coefficient of the variable of type "type"
1111 * at position "pos" of "aff".
1113 * A NaN is unaffected by this operation.
1115 __isl_give isl_aff
*isl_aff_add_coefficient(__isl_take isl_aff
*aff
,
1116 enum isl_dim_type type
, int pos
, isl_int v
)
1121 if (type
== isl_dim_out
)
1122 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1123 "output/set dimension does not have a coefficient",
1124 return isl_aff_free(aff
));
1125 if (type
== isl_dim_in
)
1128 if (pos
>= isl_local_space_dim(aff
->ls
, type
))
1129 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1130 "position out of bounds", return isl_aff_free(aff
));
1132 if (isl_aff_is_nan(aff
))
1134 aff
= isl_aff_cow(aff
);
1138 aff
->v
= isl_vec_cow(aff
->v
);
1140 return isl_aff_free(aff
);
1142 pos
+= isl_local_space_offset(aff
->ls
, type
);
1143 isl_int_addmul(aff
->v
->el
[1 + pos
], aff
->v
->el
[0], v
);
1148 /* Add "v" to the coefficient of the variable of type "type"
1149 * at position "pos" of "aff".
1151 * A NaN is unaffected by this operation.
1153 __isl_give isl_aff
*isl_aff_add_coefficient_val(__isl_take isl_aff
*aff
,
1154 enum isl_dim_type type
, int pos
, __isl_take isl_val
*v
)
1159 if (isl_val_is_zero(v
)) {
1164 if (type
== isl_dim_out
)
1165 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1166 "output/set dimension does not have a coefficient",
1168 if (type
== isl_dim_in
)
1171 if (pos
>= isl_local_space_dim(aff
->ls
, type
))
1172 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1173 "position out of bounds", goto error
);
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
);
1277 off
= isl_local_space_offset(aff
->ls
, isl_dim_div
);
1279 pos
= isl_seq_last_non_zero(aff
->v
->el
+ 1 + off
, n
) + 1;
1283 aff
= isl_aff_cow(aff
);
1287 aff
->ls
= isl_local_space_drop_dims(aff
->ls
, isl_dim_div
, pos
, n
- pos
);
1288 aff
->v
= isl_vec_drop_els(aff
->v
, 1 + off
+ pos
, n
- pos
);
1289 if (!aff
->ls
|| !aff
->v
)
1290 return isl_aff_free(aff
);
1295 /* Look for any divs in the aff->ls with a denominator equal to one
1296 * and plug them into the affine expression and any subsequent divs
1297 * that may reference the div.
1299 static __isl_give isl_aff
*plug_in_integral_divs(__isl_take isl_aff
*aff
)
1305 isl_local_space
*ls
;
1311 n
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
1313 for (i
= 0; i
< n
; ++i
) {
1314 if (!isl_int_is_one(aff
->ls
->div
->row
[i
][0]))
1316 ls
= isl_local_space_copy(aff
->ls
);
1317 ls
= isl_local_space_substitute_seq(ls
, isl_dim_div
, i
,
1318 aff
->ls
->div
->row
[i
], len
, i
+ 1, n
- (i
+ 1));
1319 vec
= isl_vec_copy(aff
->v
);
1320 vec
= isl_vec_cow(vec
);
1326 pos
= isl_local_space_offset(aff
->ls
, isl_dim_div
) + i
;
1327 isl_seq_substitute(vec
->el
, pos
, aff
->ls
->div
->row
[i
],
1332 isl_vec_free(aff
->v
);
1334 isl_local_space_free(aff
->ls
);
1341 isl_local_space_free(ls
);
1342 return isl_aff_free(aff
);
1345 /* Look for any divs j that appear with a unit coefficient inside
1346 * the definitions of other divs i and plug them into the definitions
1349 * In particular, an expression of the form
1351 * floor((f(..) + floor(g(..)/n))/m)
1355 * floor((n * f(..) + g(..))/(n * m))
1357 * This simplification is correct because we can move the expression
1358 * f(..) into the inner floor in the original expression to obtain
1360 * floor(floor((n * f(..) + g(..))/n)/m)
1362 * from which we can derive the simplified expression.
1364 static __isl_give isl_aff
*plug_in_unit_divs(__isl_take isl_aff
*aff
)
1372 n
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
1373 off
= isl_local_space_offset(aff
->ls
, isl_dim_div
);
1374 for (i
= 1; i
< n
; ++i
) {
1375 for (j
= 0; j
< i
; ++j
) {
1376 if (!isl_int_is_one(aff
->ls
->div
->row
[i
][1 + off
+ j
]))
1378 aff
->ls
= isl_local_space_substitute_seq(aff
->ls
,
1379 isl_dim_div
, j
, aff
->ls
->div
->row
[j
],
1380 aff
->v
->size
, i
, 1);
1382 return isl_aff_free(aff
);
1389 /* Swap divs "a" and "b" in "aff", which is assumed to be non-NULL.
1391 * Even though this function is only called on isl_affs with a single
1392 * reference, we are careful to only change aff->v and aff->ls together.
1394 static __isl_give isl_aff
*swap_div(__isl_take isl_aff
*aff
, int a
, int b
)
1396 unsigned off
= isl_local_space_offset(aff
->ls
, isl_dim_div
);
1397 isl_local_space
*ls
;
1400 ls
= isl_local_space_copy(aff
->ls
);
1401 ls
= isl_local_space_swap_div(ls
, a
, b
);
1402 v
= isl_vec_copy(aff
->v
);
1407 isl_int_swap(v
->el
[1 + off
+ a
], v
->el
[1 + off
+ b
]);
1408 isl_vec_free(aff
->v
);
1410 isl_local_space_free(aff
->ls
);
1416 isl_local_space_free(ls
);
1417 return isl_aff_free(aff
);
1420 /* Merge divs "a" and "b" in "aff", which is assumed to be non-NULL.
1422 * We currently do not actually remove div "b", but simply add its
1423 * coefficient to that of "a" and then zero it out.
1425 static __isl_give isl_aff
*merge_divs(__isl_take isl_aff
*aff
, int a
, int b
)
1427 unsigned off
= isl_local_space_offset(aff
->ls
, isl_dim_div
);
1429 if (isl_int_is_zero(aff
->v
->el
[1 + off
+ b
]))
1432 aff
->v
= isl_vec_cow(aff
->v
);
1434 return isl_aff_free(aff
);
1436 isl_int_add(aff
->v
->el
[1 + off
+ a
],
1437 aff
->v
->el
[1 + off
+ a
], aff
->v
->el
[1 + off
+ b
]);
1438 isl_int_set_si(aff
->v
->el
[1 + off
+ b
], 0);
1443 /* Sort the divs in the local space of "aff" according to
1444 * the comparison function "cmp_row" in isl_local_space.c,
1445 * combining the coefficients of identical divs.
1447 * Reordering divs does not change the semantics of "aff",
1448 * so there is no need to call isl_aff_cow.
1449 * Moreover, this function is currently only called on isl_affs
1450 * with a single reference.
1452 static __isl_give isl_aff
*sort_divs(__isl_take isl_aff
*aff
)
1459 n
= isl_aff_dim(aff
, isl_dim_div
);
1460 for (i
= 1; i
< n
; ++i
) {
1461 for (j
= i
- 1; j
>= 0; --j
) {
1462 int cmp
= isl_mat_cmp_div(aff
->ls
->div
, j
, j
+ 1);
1466 aff
= merge_divs(aff
, j
, j
+ 1);
1468 aff
= swap_div(aff
, j
, j
+ 1);
1477 /* Normalize the representation of "aff".
1479 * This function should only be called of "new" isl_affs, i.e.,
1480 * with only a single reference. We therefore do not need to
1481 * worry about affecting other instances.
1483 __isl_give isl_aff
*isl_aff_normalize(__isl_take isl_aff
*aff
)
1487 aff
->v
= isl_vec_normalize(aff
->v
);
1489 return isl_aff_free(aff
);
1490 aff
= plug_in_integral_divs(aff
);
1491 aff
= plug_in_unit_divs(aff
);
1492 aff
= sort_divs(aff
);
1493 aff
= isl_aff_remove_unused_divs(aff
);
1497 /* Given f, return floor(f).
1498 * If f is an integer expression, then just return f.
1499 * If f is a constant, then return the constant floor(f).
1500 * Otherwise, if f = g/m, write g = q m + r,
1501 * create a new div d = [r/m] and return the expression q + d.
1502 * The coefficients in r are taken to lie between -m/2 and m/2.
1504 * reduce_div_coefficients performs the same normalization.
1506 * As a special case, floor(NaN) = NaN.
1508 __isl_give isl_aff
*isl_aff_floor(__isl_take isl_aff
*aff
)
1518 if (isl_aff_is_nan(aff
))
1520 if (isl_int_is_one(aff
->v
->el
[0]))
1523 aff
= isl_aff_cow(aff
);
1527 aff
->v
= isl_vec_cow(aff
->v
);
1529 return isl_aff_free(aff
);
1531 if (isl_aff_is_cst(aff
)) {
1532 isl_int_fdiv_q(aff
->v
->el
[1], aff
->v
->el
[1], aff
->v
->el
[0]);
1533 isl_int_set_si(aff
->v
->el
[0], 1);
1537 div
= isl_vec_copy(aff
->v
);
1538 div
= isl_vec_cow(div
);
1540 return isl_aff_free(aff
);
1542 ctx
= isl_aff_get_ctx(aff
);
1543 isl_int_fdiv_q(aff
->v
->el
[0], aff
->v
->el
[0], ctx
->two
);
1544 for (i
= 1; i
< aff
->v
->size
; ++i
) {
1545 isl_int_fdiv_r(div
->el
[i
], div
->el
[i
], div
->el
[0]);
1546 isl_int_fdiv_q(aff
->v
->el
[i
], aff
->v
->el
[i
], div
->el
[0]);
1547 if (isl_int_gt(div
->el
[i
], aff
->v
->el
[0])) {
1548 isl_int_sub(div
->el
[i
], div
->el
[i
], div
->el
[0]);
1549 isl_int_add_ui(aff
->v
->el
[i
], aff
->v
->el
[i
], 1);
1553 aff
->ls
= isl_local_space_add_div(aff
->ls
, div
);
1555 return isl_aff_free(aff
);
1557 size
= aff
->v
->size
;
1558 aff
->v
= isl_vec_extend(aff
->v
, size
+ 1);
1560 return isl_aff_free(aff
);
1561 isl_int_set_si(aff
->v
->el
[0], 1);
1562 isl_int_set_si(aff
->v
->el
[size
], 1);
1564 aff
= isl_aff_normalize(aff
);
1571 * aff mod m = aff - m * floor(aff/m)
1573 * with m an integer value.
1575 __isl_give isl_aff
*isl_aff_mod_val(__isl_take isl_aff
*aff
,
1576 __isl_take isl_val
*m
)
1583 if (!isl_val_is_int(m
))
1584 isl_die(isl_val_get_ctx(m
), isl_error_invalid
,
1585 "expecting integer modulo", goto error
);
1587 res
= isl_aff_copy(aff
);
1588 aff
= isl_aff_scale_down_val(aff
, isl_val_copy(m
));
1589 aff
= isl_aff_floor(aff
);
1590 aff
= isl_aff_scale_val(aff
, m
);
1591 res
= isl_aff_sub(res
, aff
);
1602 * pwaff mod m = pwaff - m * floor(pwaff/m)
1604 __isl_give isl_pw_aff
*isl_pw_aff_mod(__isl_take isl_pw_aff
*pwaff
, isl_int m
)
1608 res
= isl_pw_aff_copy(pwaff
);
1609 pwaff
= isl_pw_aff_scale_down(pwaff
, m
);
1610 pwaff
= isl_pw_aff_floor(pwaff
);
1611 pwaff
= isl_pw_aff_scale(pwaff
, m
);
1612 res
= isl_pw_aff_sub(res
, pwaff
);
1619 * pa mod m = pa - m * floor(pa/m)
1621 * with m an integer value.
1623 __isl_give isl_pw_aff
*isl_pw_aff_mod_val(__isl_take isl_pw_aff
*pa
,
1624 __isl_take isl_val
*m
)
1628 if (!isl_val_is_int(m
))
1629 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
1630 "expecting integer modulo", goto error
);
1631 pa
= isl_pw_aff_mod(pa
, m
->n
);
1635 isl_pw_aff_free(pa
);
1640 /* Given f, return ceil(f).
1641 * If f is an integer expression, then just return f.
1642 * Otherwise, let f be the expression
1648 * floor((e + m - 1)/m)
1650 * As a special case, ceil(NaN) = NaN.
1652 __isl_give isl_aff
*isl_aff_ceil(__isl_take isl_aff
*aff
)
1657 if (isl_aff_is_nan(aff
))
1659 if (isl_int_is_one(aff
->v
->el
[0]))
1662 aff
= isl_aff_cow(aff
);
1665 aff
->v
= isl_vec_cow(aff
->v
);
1667 return isl_aff_free(aff
);
1669 isl_int_add(aff
->v
->el
[1], aff
->v
->el
[1], aff
->v
->el
[0]);
1670 isl_int_sub_ui(aff
->v
->el
[1], aff
->v
->el
[1], 1);
1671 aff
= isl_aff_floor(aff
);
1676 /* Apply the expansion computed by isl_merge_divs.
1677 * The expansion itself is given by "exp" while the resulting
1678 * list of divs is given by "div".
1680 __isl_give isl_aff
*isl_aff_expand_divs(__isl_take isl_aff
*aff
,
1681 __isl_take isl_mat
*div
, int *exp
)
1687 aff
= isl_aff_cow(aff
);
1691 old_n_div
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
1692 new_n_div
= isl_mat_rows(div
);
1693 offset
= 1 + isl_local_space_offset(aff
->ls
, isl_dim_div
);
1695 aff
->v
= isl_vec_expand(aff
->v
, offset
, old_n_div
, exp
, new_n_div
);
1696 aff
->ls
= isl_local_space_replace_divs(aff
->ls
, div
);
1697 if (!aff
->v
|| !aff
->ls
)
1698 return isl_aff_free(aff
);
1706 /* Add two affine expressions that live in the same local space.
1708 static __isl_give isl_aff
*add_expanded(__isl_take isl_aff
*aff1
,
1709 __isl_take isl_aff
*aff2
)
1713 aff1
= isl_aff_cow(aff1
);
1717 aff1
->v
= isl_vec_cow(aff1
->v
);
1723 isl_int_gcd(gcd
, aff1
->v
->el
[0], aff2
->v
->el
[0]);
1724 isl_int_divexact(f
, aff2
->v
->el
[0], gcd
);
1725 isl_seq_scale(aff1
->v
->el
+ 1, aff1
->v
->el
+ 1, f
, aff1
->v
->size
- 1);
1726 isl_int_divexact(f
, aff1
->v
->el
[0], gcd
);
1727 isl_seq_addmul(aff1
->v
->el
+ 1, f
, aff2
->v
->el
+ 1, aff1
->v
->size
- 1);
1728 isl_int_divexact(f
, aff2
->v
->el
[0], gcd
);
1729 isl_int_mul(aff1
->v
->el
[0], aff1
->v
->el
[0], f
);
1741 /* Return the sum of "aff1" and "aff2".
1743 * If either of the two is NaN, then the result is NaN.
1745 __isl_give isl_aff
*isl_aff_add(__isl_take isl_aff
*aff1
,
1746 __isl_take isl_aff
*aff2
)
1757 ctx
= isl_aff_get_ctx(aff1
);
1758 if (!isl_space_is_equal(aff1
->ls
->dim
, aff2
->ls
->dim
))
1759 isl_die(ctx
, isl_error_invalid
,
1760 "spaces don't match", goto error
);
1762 if (isl_aff_is_nan(aff1
)) {
1766 if (isl_aff_is_nan(aff2
)) {
1771 n_div1
= isl_aff_dim(aff1
, isl_dim_div
);
1772 n_div2
= isl_aff_dim(aff2
, isl_dim_div
);
1773 if (n_div1
== 0 && n_div2
== 0)
1774 return add_expanded(aff1
, aff2
);
1776 exp1
= isl_alloc_array(ctx
, int, n_div1
);
1777 exp2
= isl_alloc_array(ctx
, int, n_div2
);
1778 if ((n_div1
&& !exp1
) || (n_div2
&& !exp2
))
1781 div
= isl_merge_divs(aff1
->ls
->div
, aff2
->ls
->div
, exp1
, exp2
);
1782 aff1
= isl_aff_expand_divs(aff1
, isl_mat_copy(div
), exp1
);
1783 aff2
= isl_aff_expand_divs(aff2
, div
, exp2
);
1787 return add_expanded(aff1
, aff2
);
1796 __isl_give isl_aff
*isl_aff_sub(__isl_take isl_aff
*aff1
,
1797 __isl_take isl_aff
*aff2
)
1799 return isl_aff_add(aff1
, isl_aff_neg(aff2
));
1802 /* Return the result of scaling "aff" by a factor of "f".
1804 * As a special case, f * NaN = NaN.
1806 __isl_give isl_aff
*isl_aff_scale(__isl_take isl_aff
*aff
, isl_int f
)
1812 if (isl_aff_is_nan(aff
))
1815 if (isl_int_is_one(f
))
1818 aff
= isl_aff_cow(aff
);
1821 aff
->v
= isl_vec_cow(aff
->v
);
1823 return isl_aff_free(aff
);
1825 if (isl_int_is_pos(f
) && isl_int_is_divisible_by(aff
->v
->el
[0], f
)) {
1826 isl_int_divexact(aff
->v
->el
[0], aff
->v
->el
[0], f
);
1831 isl_int_gcd(gcd
, aff
->v
->el
[0], f
);
1832 isl_int_divexact(aff
->v
->el
[0], aff
->v
->el
[0], gcd
);
1833 isl_int_divexact(gcd
, f
, gcd
);
1834 isl_seq_scale(aff
->v
->el
+ 1, aff
->v
->el
+ 1, gcd
, aff
->v
->size
- 1);
1840 /* Multiple "aff" by "v".
1842 __isl_give isl_aff
*isl_aff_scale_val(__isl_take isl_aff
*aff
,
1843 __isl_take isl_val
*v
)
1848 if (isl_val_is_one(v
)) {
1853 if (!isl_val_is_rat(v
))
1854 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1855 "expecting rational factor", goto error
);
1857 aff
= isl_aff_scale(aff
, v
->n
);
1858 aff
= isl_aff_scale_down(aff
, v
->d
);
1868 /* Return the result of scaling "aff" down by a factor of "f".
1870 * As a special case, NaN/f = NaN.
1872 __isl_give isl_aff
*isl_aff_scale_down(__isl_take isl_aff
*aff
, isl_int f
)
1878 if (isl_aff_is_nan(aff
))
1881 if (isl_int_is_one(f
))
1884 aff
= isl_aff_cow(aff
);
1888 if (isl_int_is_zero(f
))
1889 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1890 "cannot scale down by zero", return isl_aff_free(aff
));
1892 aff
->v
= isl_vec_cow(aff
->v
);
1894 return isl_aff_free(aff
);
1897 isl_seq_gcd(aff
->v
->el
+ 1, aff
->v
->size
- 1, &gcd
);
1898 isl_int_gcd(gcd
, gcd
, f
);
1899 isl_seq_scale_down(aff
->v
->el
+ 1, aff
->v
->el
+ 1, gcd
, aff
->v
->size
- 1);
1900 isl_int_divexact(gcd
, f
, gcd
);
1901 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], gcd
);
1907 /* Divide "aff" by "v".
1909 __isl_give isl_aff
*isl_aff_scale_down_val(__isl_take isl_aff
*aff
,
1910 __isl_take isl_val
*v
)
1915 if (isl_val_is_one(v
)) {
1920 if (!isl_val_is_rat(v
))
1921 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1922 "expecting rational factor", goto error
);
1923 if (!isl_val_is_pos(v
))
1924 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1925 "factor needs to be positive", goto error
);
1927 aff
= isl_aff_scale(aff
, v
->d
);
1928 aff
= isl_aff_scale_down(aff
, v
->n
);
1938 __isl_give isl_aff
*isl_aff_scale_down_ui(__isl_take isl_aff
*aff
, unsigned f
)
1946 isl_int_set_ui(v
, f
);
1947 aff
= isl_aff_scale_down(aff
, v
);
1953 __isl_give isl_aff
*isl_aff_set_dim_name(__isl_take isl_aff
*aff
,
1954 enum isl_dim_type type
, unsigned pos
, const char *s
)
1956 aff
= isl_aff_cow(aff
);
1959 if (type
== isl_dim_out
)
1960 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1961 "cannot set name of output/set dimension",
1962 return isl_aff_free(aff
));
1963 if (type
== isl_dim_in
)
1965 aff
->ls
= isl_local_space_set_dim_name(aff
->ls
, type
, pos
, s
);
1967 return isl_aff_free(aff
);
1972 __isl_give isl_aff
*isl_aff_set_dim_id(__isl_take isl_aff
*aff
,
1973 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
1975 aff
= isl_aff_cow(aff
);
1978 if (type
== isl_dim_out
)
1979 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1980 "cannot set name of output/set dimension",
1982 if (type
== isl_dim_in
)
1984 aff
->ls
= isl_local_space_set_dim_id(aff
->ls
, type
, pos
, id
);
1986 return isl_aff_free(aff
);
1995 /* Replace the identifier of the input tuple of "aff" by "id".
1996 * type is currently required to be equal to isl_dim_in
1998 __isl_give isl_aff
*isl_aff_set_tuple_id(__isl_take isl_aff
*aff
,
1999 enum isl_dim_type type
, __isl_take isl_id
*id
)
2001 aff
= isl_aff_cow(aff
);
2004 if (type
!= isl_dim_in
)
2005 isl_die(aff
->v
->ctx
, isl_error_invalid
,
2006 "cannot only set id of input tuple", goto error
);
2007 aff
->ls
= isl_local_space_set_tuple_id(aff
->ls
, isl_dim_set
, id
);
2009 return isl_aff_free(aff
);
2018 /* Exploit the equalities in "eq" to simplify the affine expression
2019 * and the expressions of the integer divisions in the local space.
2020 * The integer divisions in this local space are assumed to appear
2021 * as regular dimensions in "eq".
2023 static __isl_give isl_aff
*isl_aff_substitute_equalities_lifted(
2024 __isl_take isl_aff
*aff
, __isl_take isl_basic_set
*eq
)
2032 if (eq
->n_eq
== 0) {
2033 isl_basic_set_free(eq
);
2037 aff
= isl_aff_cow(aff
);
2041 aff
->ls
= isl_local_space_substitute_equalities(aff
->ls
,
2042 isl_basic_set_copy(eq
));
2043 aff
->v
= isl_vec_cow(aff
->v
);
2044 if (!aff
->ls
|| !aff
->v
)
2047 total
= 1 + isl_space_dim(eq
->dim
, isl_dim_all
);
2049 for (i
= 0; i
< eq
->n_eq
; ++i
) {
2050 j
= isl_seq_last_non_zero(eq
->eq
[i
], total
+ n_div
);
2051 if (j
< 0 || j
== 0 || j
>= total
)
2054 isl_seq_elim(aff
->v
->el
+ 1, eq
->eq
[i
], j
, total
,
2058 isl_basic_set_free(eq
);
2059 aff
= isl_aff_normalize(aff
);
2062 isl_basic_set_free(eq
);
2067 /* Exploit the equalities in "eq" to simplify the affine expression
2068 * and the expressions of the integer divisions in the local space.
2070 __isl_give isl_aff
*isl_aff_substitute_equalities(__isl_take isl_aff
*aff
,
2071 __isl_take isl_basic_set
*eq
)
2077 n_div
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
2079 eq
= isl_basic_set_add_dims(eq
, isl_dim_set
, n_div
);
2080 return isl_aff_substitute_equalities_lifted(aff
, eq
);
2082 isl_basic_set_free(eq
);
2087 /* Look for equalities among the variables shared by context and aff
2088 * and the integer divisions of aff, if any.
2089 * The equalities are then used to eliminate coefficients and/or integer
2090 * divisions from aff.
2092 __isl_give isl_aff
*isl_aff_gist(__isl_take isl_aff
*aff
,
2093 __isl_take isl_set
*context
)
2095 isl_basic_set
*hull
;
2100 n_div
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
2102 isl_basic_set
*bset
;
2103 isl_local_space
*ls
;
2104 context
= isl_set_add_dims(context
, isl_dim_set
, n_div
);
2105 ls
= isl_aff_get_domain_local_space(aff
);
2106 bset
= isl_basic_set_from_local_space(ls
);
2107 bset
= isl_basic_set_lift(bset
);
2108 bset
= isl_basic_set_flatten(bset
);
2109 context
= isl_set_intersect(context
,
2110 isl_set_from_basic_set(bset
));
2113 hull
= isl_set_affine_hull(context
);
2114 return isl_aff_substitute_equalities_lifted(aff
, hull
);
2117 isl_set_free(context
);
2121 __isl_give isl_aff
*isl_aff_gist_params(__isl_take isl_aff
*aff
,
2122 __isl_take isl_set
*context
)
2124 isl_set
*dom_context
= isl_set_universe(isl_aff_get_domain_space(aff
));
2125 dom_context
= isl_set_intersect_params(dom_context
, context
);
2126 return isl_aff_gist(aff
, dom_context
);
2129 /* Return a basic set containing those elements in the space
2130 * of aff where it is positive. "rational" should not be set.
2132 * If "aff" is NaN, then it is not positive.
2134 static __isl_give isl_basic_set
*aff_pos_basic_set(__isl_take isl_aff
*aff
,
2137 isl_constraint
*ineq
;
2138 isl_basic_set
*bset
;
2143 if (isl_aff_is_nan(aff
)) {
2144 isl_space
*space
= isl_aff_get_domain_space(aff
);
2146 return isl_basic_set_empty(space
);
2149 isl_die(isl_aff_get_ctx(aff
), isl_error_unsupported
,
2150 "rational sets not supported", goto error
);
2152 ineq
= isl_inequality_from_aff(aff
);
2153 c
= isl_constraint_get_constant_val(ineq
);
2154 c
= isl_val_sub_ui(c
, 1);
2155 ineq
= isl_constraint_set_constant_val(ineq
, c
);
2157 bset
= isl_basic_set_from_constraint(ineq
);
2158 bset
= isl_basic_set_simplify(bset
);
2165 /* Return a basic set containing those elements in the space
2166 * of aff where it is non-negative.
2167 * If "rational" is set, then return a rational basic set.
2169 * If "aff" is NaN, then it is not non-negative (it's not negative either).
2171 static __isl_give isl_basic_set
*aff_nonneg_basic_set(
2172 __isl_take isl_aff
*aff
, int rational
)
2174 isl_constraint
*ineq
;
2175 isl_basic_set
*bset
;
2179 if (isl_aff_is_nan(aff
)) {
2180 isl_space
*space
= isl_aff_get_domain_space(aff
);
2182 return isl_basic_set_empty(space
);
2185 ineq
= isl_inequality_from_aff(aff
);
2187 bset
= isl_basic_set_from_constraint(ineq
);
2189 bset
= isl_basic_set_set_rational(bset
);
2190 bset
= isl_basic_set_simplify(bset
);
2194 /* Return a basic set containing those elements in the space
2195 * of aff where it is non-negative.
2197 __isl_give isl_basic_set
*isl_aff_nonneg_basic_set(__isl_take isl_aff
*aff
)
2199 return aff_nonneg_basic_set(aff
, 0);
2202 /* Return a basic set containing those elements in the domain space
2203 * of "aff" where it is positive.
2205 __isl_give isl_basic_set
*isl_aff_pos_basic_set(__isl_take isl_aff
*aff
)
2207 aff
= isl_aff_add_constant_num_si(aff
, -1);
2208 return isl_aff_nonneg_basic_set(aff
);
2211 /* Return a basic set containing those elements in the domain space
2212 * of aff where it is negative.
2214 __isl_give isl_basic_set
*isl_aff_neg_basic_set(__isl_take isl_aff
*aff
)
2216 aff
= isl_aff_neg(aff
);
2217 return isl_aff_pos_basic_set(aff
);
2220 /* Return a basic set containing those elements in the space
2221 * of aff where it is zero.
2222 * If "rational" is set, then return a rational basic set.
2224 * If "aff" is NaN, then it is not zero.
2226 static __isl_give isl_basic_set
*aff_zero_basic_set(__isl_take isl_aff
*aff
,
2229 isl_constraint
*ineq
;
2230 isl_basic_set
*bset
;
2234 if (isl_aff_is_nan(aff
)) {
2235 isl_space
*space
= isl_aff_get_domain_space(aff
);
2237 return isl_basic_set_empty(space
);
2240 ineq
= isl_equality_from_aff(aff
);
2242 bset
= isl_basic_set_from_constraint(ineq
);
2244 bset
= isl_basic_set_set_rational(bset
);
2245 bset
= isl_basic_set_simplify(bset
);
2249 /* Return a basic set containing those elements in the space
2250 * of aff where it is zero.
2252 __isl_give isl_basic_set
*isl_aff_zero_basic_set(__isl_take isl_aff
*aff
)
2254 return aff_zero_basic_set(aff
, 0);
2257 /* Return a basic set containing those elements in the shared space
2258 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
2260 __isl_give isl_basic_set
*isl_aff_ge_basic_set(__isl_take isl_aff
*aff1
,
2261 __isl_take isl_aff
*aff2
)
2263 aff1
= isl_aff_sub(aff1
, aff2
);
2265 return isl_aff_nonneg_basic_set(aff1
);
2268 /* Return a basic set containing those elements in the shared domain space
2269 * of "aff1" and "aff2" where "aff1" is greater than "aff2".
2271 __isl_give isl_basic_set
*isl_aff_gt_basic_set(__isl_take isl_aff
*aff1
,
2272 __isl_take isl_aff
*aff2
)
2274 aff1
= isl_aff_sub(aff1
, aff2
);
2276 return isl_aff_pos_basic_set(aff1
);
2279 /* Return a set containing those elements in the shared space
2280 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
2282 __isl_give isl_set
*isl_aff_ge_set(__isl_take isl_aff
*aff1
,
2283 __isl_take isl_aff
*aff2
)
2285 return isl_set_from_basic_set(isl_aff_ge_basic_set(aff1
, aff2
));
2288 /* Return a set containing those elements in the shared domain space
2289 * of aff1 and aff2 where aff1 is greater than aff2.
2291 * If either of the two inputs is NaN, then the result is empty,
2292 * as comparisons with NaN always return false.
2294 __isl_give isl_set
*isl_aff_gt_set(__isl_take isl_aff
*aff1
,
2295 __isl_take isl_aff
*aff2
)
2297 return isl_set_from_basic_set(isl_aff_gt_basic_set(aff1
, aff2
));
2300 /* Return a basic set containing those elements in the shared space
2301 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
2303 __isl_give isl_basic_set
*isl_aff_le_basic_set(__isl_take isl_aff
*aff1
,
2304 __isl_take isl_aff
*aff2
)
2306 return isl_aff_ge_basic_set(aff2
, aff1
);
2309 /* Return a basic set containing those elements in the shared domain space
2310 * of "aff1" and "aff2" where "aff1" is smaller than "aff2".
2312 __isl_give isl_basic_set
*isl_aff_lt_basic_set(__isl_take isl_aff
*aff1
,
2313 __isl_take isl_aff
*aff2
)
2315 return isl_aff_gt_basic_set(aff2
, aff1
);
2318 /* Return a set containing those elements in the shared space
2319 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
2321 __isl_give isl_set
*isl_aff_le_set(__isl_take isl_aff
*aff1
,
2322 __isl_take isl_aff
*aff2
)
2324 return isl_aff_ge_set(aff2
, aff1
);
2327 /* Return a set containing those elements in the shared domain space
2328 * of "aff1" and "aff2" where "aff1" is smaller than "aff2".
2330 __isl_give isl_set
*isl_aff_lt_set(__isl_take isl_aff
*aff1
,
2331 __isl_take isl_aff
*aff2
)
2333 return isl_set_from_basic_set(isl_aff_lt_basic_set(aff1
, aff2
));
2336 /* Return a basic set containing those elements in the shared space
2337 * of aff1 and aff2 where aff1 and aff2 are equal.
2339 __isl_give isl_basic_set
*isl_aff_eq_basic_set(__isl_take isl_aff
*aff1
,
2340 __isl_take isl_aff
*aff2
)
2342 aff1
= isl_aff_sub(aff1
, aff2
);
2344 return isl_aff_zero_basic_set(aff1
);
2347 /* Return a set containing those elements in the shared space
2348 * of aff1 and aff2 where aff1 and aff2 are equal.
2350 __isl_give isl_set
*isl_aff_eq_set(__isl_take isl_aff
*aff1
,
2351 __isl_take isl_aff
*aff2
)
2353 return isl_set_from_basic_set(isl_aff_eq_basic_set(aff1
, aff2
));
2356 /* Return a set containing those elements in the shared domain space
2357 * of aff1 and aff2 where aff1 and aff2 are not equal.
2359 * If either of the two inputs is NaN, then the result is empty,
2360 * as comparisons with NaN always return false.
2362 __isl_give isl_set
*isl_aff_ne_set(__isl_take isl_aff
*aff1
,
2363 __isl_take isl_aff
*aff2
)
2365 isl_set
*set_lt
, *set_gt
;
2367 set_lt
= isl_aff_lt_set(isl_aff_copy(aff1
),
2368 isl_aff_copy(aff2
));
2369 set_gt
= isl_aff_gt_set(aff1
, aff2
);
2370 return isl_set_union_disjoint(set_lt
, set_gt
);
2373 __isl_give isl_aff
*isl_aff_add_on_domain(__isl_keep isl_set
*dom
,
2374 __isl_take isl_aff
*aff1
, __isl_take isl_aff
*aff2
)
2376 aff1
= isl_aff_add(aff1
, aff2
);
2377 aff1
= isl_aff_gist(aff1
, isl_set_copy(dom
));
2381 int isl_aff_is_empty(__isl_keep isl_aff
*aff
)
2389 /* Check whether the given affine expression has non-zero coefficient
2390 * for any dimension in the given range or if any of these dimensions
2391 * appear with non-zero coefficients in any of the integer divisions
2392 * involved in the affine expression.
2394 isl_bool
isl_aff_involves_dims(__isl_keep isl_aff
*aff
,
2395 enum isl_dim_type type
, unsigned first
, unsigned n
)
2400 isl_bool involves
= isl_bool_false
;
2403 return isl_bool_error
;
2405 return isl_bool_false
;
2407 ctx
= isl_aff_get_ctx(aff
);
2408 if (first
+ n
> isl_aff_dim(aff
, type
))
2409 isl_die(ctx
, isl_error_invalid
,
2410 "range out of bounds", return isl_bool_error
);
2412 active
= isl_local_space_get_active(aff
->ls
, aff
->v
->el
+ 2);
2416 first
+= isl_local_space_offset(aff
->ls
, type
) - 1;
2417 for (i
= 0; i
< n
; ++i
)
2418 if (active
[first
+ i
]) {
2419 involves
= isl_bool_true
;
2428 return isl_bool_error
;
2431 __isl_give isl_aff
*isl_aff_drop_dims(__isl_take isl_aff
*aff
,
2432 enum isl_dim_type type
, unsigned first
, unsigned n
)
2438 if (type
== isl_dim_out
)
2439 isl_die(aff
->v
->ctx
, isl_error_invalid
,
2440 "cannot drop output/set dimension",
2441 return isl_aff_free(aff
));
2442 if (type
== isl_dim_in
)
2444 if (n
== 0 && !isl_local_space_is_named_or_nested(aff
->ls
, type
))
2447 ctx
= isl_aff_get_ctx(aff
);
2448 if (first
+ n
> isl_local_space_dim(aff
->ls
, type
))
2449 isl_die(ctx
, isl_error_invalid
, "range out of bounds",
2450 return isl_aff_free(aff
));
2452 aff
= isl_aff_cow(aff
);
2456 aff
->ls
= isl_local_space_drop_dims(aff
->ls
, type
, first
, n
);
2458 return isl_aff_free(aff
);
2460 first
+= 1 + isl_local_space_offset(aff
->ls
, type
);
2461 aff
->v
= isl_vec_drop_els(aff
->v
, first
, n
);
2463 return isl_aff_free(aff
);
2468 /* Drop the "n" domain dimensions starting at "first" from "aff",
2469 * after checking that they do not appear in the affine expression.
2471 static __isl_give isl_aff
*drop_domain(__isl_take isl_aff
*aff
, unsigned first
,
2476 involves
= isl_aff_involves_dims(aff
, isl_dim_in
, first
, n
);
2478 return isl_aff_free(aff
);
2480 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2481 "affine expression involves some of the domain dimensions",
2482 return isl_aff_free(aff
));
2483 return isl_aff_drop_dims(aff
, isl_dim_in
, first
, n
);
2486 /* Project the domain of the affine expression onto its parameter space.
2487 * The affine expression may not involve any of the domain dimensions.
2489 __isl_give isl_aff
*isl_aff_project_domain_on_params(__isl_take isl_aff
*aff
)
2494 n
= isl_aff_dim(aff
, isl_dim_in
);
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 aff
= drop_domain(aff
, n_in
, n
- n_in
);
2532 aff
= isl_aff_reset_domain_space(aff
, space
);
2536 /* Convert an affine expression defined over a parameter domain
2537 * into one that is defined over a zero-dimensional set.
2539 __isl_give isl_aff
*isl_aff_from_range(__isl_take isl_aff
*aff
)
2541 isl_local_space
*ls
;
2543 ls
= isl_aff_take_domain_local_space(aff
);
2544 ls
= isl_local_space_set_from_params(ls
);
2545 aff
= isl_aff_restore_domain_local_space(aff
, ls
);
2550 __isl_give isl_aff
*isl_aff_insert_dims(__isl_take isl_aff
*aff
,
2551 enum isl_dim_type type
, unsigned first
, unsigned n
)
2557 if (type
== isl_dim_out
)
2558 isl_die(aff
->v
->ctx
, isl_error_invalid
,
2559 "cannot insert output/set dimensions",
2560 return isl_aff_free(aff
));
2561 if (type
== isl_dim_in
)
2563 if (n
== 0 && !isl_local_space_is_named_or_nested(aff
->ls
, type
))
2566 ctx
= isl_aff_get_ctx(aff
);
2567 if (first
> isl_local_space_dim(aff
->ls
, type
))
2568 isl_die(ctx
, isl_error_invalid
, "position out of bounds",
2569 return isl_aff_free(aff
));
2571 aff
= isl_aff_cow(aff
);
2575 aff
->ls
= isl_local_space_insert_dims(aff
->ls
, type
, first
, n
);
2577 return isl_aff_free(aff
);
2579 first
+= 1 + isl_local_space_offset(aff
->ls
, type
);
2580 aff
->v
= isl_vec_insert_zero_els(aff
->v
, first
, n
);
2582 return isl_aff_free(aff
);
2587 __isl_give isl_aff
*isl_aff_add_dims(__isl_take isl_aff
*aff
,
2588 enum isl_dim_type type
, unsigned n
)
2592 pos
= isl_aff_dim(aff
, type
);
2594 return isl_aff_insert_dims(aff
, type
, pos
, n
);
2597 __isl_give isl_pw_aff
*isl_pw_aff_add_dims(__isl_take isl_pw_aff
*pwaff
,
2598 enum isl_dim_type type
, unsigned n
)
2602 pos
= isl_pw_aff_dim(pwaff
, type
);
2604 return isl_pw_aff_insert_dims(pwaff
, type
, pos
, n
);
2607 /* Move the "n" dimensions of "src_type" starting at "src_pos" of "aff"
2608 * to dimensions of "dst_type" at "dst_pos".
2610 * We only support moving input dimensions to parameters and vice versa.
2612 __isl_give isl_aff
*isl_aff_move_dims(__isl_take isl_aff
*aff
,
2613 enum isl_dim_type dst_type
, unsigned dst_pos
,
2614 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
2622 !isl_local_space_is_named_or_nested(aff
->ls
, src_type
) &&
2623 !isl_local_space_is_named_or_nested(aff
->ls
, dst_type
))
2626 if (dst_type
== isl_dim_out
|| src_type
== isl_dim_out
)
2627 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2628 "cannot move output/set dimension",
2629 return isl_aff_free(aff
));
2630 if (dst_type
== isl_dim_div
|| src_type
== isl_dim_div
)
2631 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2632 "cannot move divs", return isl_aff_free(aff
));
2633 if (dst_type
== isl_dim_in
)
2634 dst_type
= isl_dim_set
;
2635 if (src_type
== isl_dim_in
)
2636 src_type
= isl_dim_set
;
2638 if (src_pos
+ n
> isl_local_space_dim(aff
->ls
, src_type
))
2639 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2640 "range out of bounds", return isl_aff_free(aff
));
2641 if (dst_type
== src_type
)
2642 isl_die(isl_aff_get_ctx(aff
), isl_error_unsupported
,
2643 "moving dims within the same type not supported",
2644 return isl_aff_free(aff
));
2646 aff
= isl_aff_cow(aff
);
2650 g_src_pos
= 1 + isl_local_space_offset(aff
->ls
, src_type
) + src_pos
;
2651 g_dst_pos
= 1 + isl_local_space_offset(aff
->ls
, dst_type
) + dst_pos
;
2652 if (dst_type
> src_type
)
2655 aff
->v
= isl_vec_move_els(aff
->v
, g_dst_pos
, g_src_pos
, n
);
2656 aff
->ls
= isl_local_space_move_dims(aff
->ls
, dst_type
, dst_pos
,
2657 src_type
, src_pos
, n
);
2658 if (!aff
->v
|| !aff
->ls
)
2659 return isl_aff_free(aff
);
2661 aff
= sort_divs(aff
);
2666 __isl_give isl_pw_aff
*isl_pw_aff_from_aff(__isl_take isl_aff
*aff
)
2668 isl_set
*dom
= isl_set_universe(isl_aff_get_domain_space(aff
));
2669 return isl_pw_aff_alloc(dom
, aff
);
2672 #define isl_aff_involves_nan isl_aff_is_nan
2675 #define PW isl_pw_aff
2679 #define EL_IS_ZERO is_empty
2683 #define IS_ZERO is_empty
2686 #undef DEFAULT_IS_ZERO
2687 #define DEFAULT_IS_ZERO 0
2693 #include <isl_pw_templ.c>
2694 #include <isl_pw_eval.c>
2695 #include <isl_pw_hash.c>
2696 #include <isl_pw_union_opt.c>
2701 #include <isl_union_single.c>
2702 #include <isl_union_neg.c>
2704 static __isl_give isl_set
*align_params_pw_pw_set_and(
2705 __isl_take isl_pw_aff
*pwaff1
, __isl_take isl_pw_aff
*pwaff2
,
2706 __isl_give isl_set
*(*fn
)(__isl_take isl_pw_aff
*pwaff1
,
2707 __isl_take isl_pw_aff
*pwaff2
))
2709 isl_bool equal_params
;
2711 if (!pwaff1
|| !pwaff2
)
2713 equal_params
= isl_space_has_equal_params(pwaff1
->dim
, pwaff2
->dim
);
2714 if (equal_params
< 0)
2717 return fn(pwaff1
, pwaff2
);
2718 if (isl_pw_aff_check_named_params(pwaff1
) < 0 ||
2719 isl_pw_aff_check_named_params(pwaff2
) < 0)
2721 pwaff1
= isl_pw_aff_align_params(pwaff1
, isl_pw_aff_get_space(pwaff2
));
2722 pwaff2
= isl_pw_aff_align_params(pwaff2
, isl_pw_aff_get_space(pwaff1
));
2723 return fn(pwaff1
, pwaff2
);
2725 isl_pw_aff_free(pwaff1
);
2726 isl_pw_aff_free(pwaff2
);
2730 /* Align the parameters of the to isl_pw_aff arguments and
2731 * then apply a function "fn" on them that returns an isl_map.
2733 static __isl_give isl_map
*align_params_pw_pw_map_and(
2734 __isl_take isl_pw_aff
*pa1
, __isl_take isl_pw_aff
*pa2
,
2735 __isl_give isl_map
*(*fn
)(__isl_take isl_pw_aff
*pa1
,
2736 __isl_take isl_pw_aff
*pa2
))
2738 isl_bool equal_params
;
2742 equal_params
= isl_space_has_equal_params(pa1
->dim
, pa2
->dim
);
2743 if (equal_params
< 0)
2746 return fn(pa1
, pa2
);
2747 if (isl_pw_aff_check_named_params(pa1
) < 0 ||
2748 isl_pw_aff_check_named_params(pa2
) < 0)
2750 pa1
= isl_pw_aff_align_params(pa1
, isl_pw_aff_get_space(pa2
));
2751 pa2
= isl_pw_aff_align_params(pa2
, isl_pw_aff_get_space(pa1
));
2752 return fn(pa1
, pa2
);
2754 isl_pw_aff_free(pa1
);
2755 isl_pw_aff_free(pa2
);
2759 /* Compute a piecewise quasi-affine expression with a domain that
2760 * is the union of those of pwaff1 and pwaff2 and such that on each
2761 * cell, the quasi-affine expression is the maximum of those of pwaff1
2762 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2763 * cell, then the associated expression is the defined one.
2765 static __isl_give isl_pw_aff
*pw_aff_union_max(__isl_take isl_pw_aff
*pwaff1
,
2766 __isl_take isl_pw_aff
*pwaff2
)
2768 return isl_pw_aff_union_opt_cmp(pwaff1
, pwaff2
, &isl_aff_ge_set
);
2771 __isl_give isl_pw_aff
*isl_pw_aff_union_max(__isl_take isl_pw_aff
*pwaff1
,
2772 __isl_take isl_pw_aff
*pwaff2
)
2774 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
,
2778 /* Compute a piecewise quasi-affine expression with a domain that
2779 * is the union of those of pwaff1 and pwaff2 and such that on each
2780 * cell, the quasi-affine expression is the minimum of those of pwaff1
2781 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2782 * cell, then the associated expression is the defined one.
2784 static __isl_give isl_pw_aff
*pw_aff_union_min(__isl_take isl_pw_aff
*pwaff1
,
2785 __isl_take isl_pw_aff
*pwaff2
)
2787 return isl_pw_aff_union_opt_cmp(pwaff1
, pwaff2
, &isl_aff_le_set
);
2790 __isl_give isl_pw_aff
*isl_pw_aff_union_min(__isl_take isl_pw_aff
*pwaff1
,
2791 __isl_take isl_pw_aff
*pwaff2
)
2793 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
,
2797 __isl_give isl_pw_aff
*isl_pw_aff_union_opt(__isl_take isl_pw_aff
*pwaff1
,
2798 __isl_take isl_pw_aff
*pwaff2
, int max
)
2801 return isl_pw_aff_union_max(pwaff1
, pwaff2
);
2803 return isl_pw_aff_union_min(pwaff1
, pwaff2
);
2806 /* Construct a map with as domain the domain of pwaff and
2807 * one-dimensional range corresponding to the affine expressions.
2809 static __isl_give isl_map
*map_from_pw_aff(__isl_take isl_pw_aff
*pwaff
)
2818 dim
= isl_pw_aff_get_space(pwaff
);
2819 map
= isl_map_empty(dim
);
2821 for (i
= 0; i
< pwaff
->n
; ++i
) {
2822 isl_basic_map
*bmap
;
2825 bmap
= isl_basic_map_from_aff(isl_aff_copy(pwaff
->p
[i
].aff
));
2826 map_i
= isl_map_from_basic_map(bmap
);
2827 map_i
= isl_map_intersect_domain(map_i
,
2828 isl_set_copy(pwaff
->p
[i
].set
));
2829 map
= isl_map_union_disjoint(map
, map_i
);
2832 isl_pw_aff_free(pwaff
);
2837 /* Construct a map with as domain the domain of pwaff and
2838 * one-dimensional range corresponding to the affine expressions.
2840 __isl_give isl_map
*isl_map_from_pw_aff(__isl_take isl_pw_aff
*pwaff
)
2844 if (isl_space_is_set(pwaff
->dim
))
2845 isl_die(isl_pw_aff_get_ctx(pwaff
), isl_error_invalid
,
2846 "space of input is not a map", goto error
);
2847 return map_from_pw_aff(pwaff
);
2849 isl_pw_aff_free(pwaff
);
2853 /* Construct a one-dimensional set with as parameter domain
2854 * the domain of pwaff and the single set dimension
2855 * corresponding to the affine expressions.
2857 __isl_give isl_set
*isl_set_from_pw_aff(__isl_take isl_pw_aff
*pwaff
)
2861 if (!isl_space_is_set(pwaff
->dim
))
2862 isl_die(isl_pw_aff_get_ctx(pwaff
), isl_error_invalid
,
2863 "space of input is not a set", goto error
);
2864 return map_from_pw_aff(pwaff
);
2866 isl_pw_aff_free(pwaff
);
2870 /* Return a set containing those elements in the domain
2871 * of "pwaff" where it satisfies "fn" (if complement is 0) or
2872 * does not satisfy "fn" (if complement is 1).
2874 * The pieces with a NaN never belong to the result since
2875 * NaN does not satisfy any property.
2877 static __isl_give isl_set
*pw_aff_locus(__isl_take isl_pw_aff
*pwaff
,
2878 __isl_give isl_basic_set
*(*fn
)(__isl_take isl_aff
*aff
, int rational
),
2887 set
= isl_set_empty(isl_pw_aff_get_domain_space(pwaff
));
2889 for (i
= 0; i
< pwaff
->n
; ++i
) {
2890 isl_basic_set
*bset
;
2891 isl_set
*set_i
, *locus
;
2894 if (isl_aff_is_nan(pwaff
->p
[i
].aff
))
2897 rational
= isl_set_has_rational(pwaff
->p
[i
].set
);
2898 bset
= fn(isl_aff_copy(pwaff
->p
[i
].aff
), rational
);
2899 locus
= isl_set_from_basic_set(bset
);
2900 set_i
= isl_set_copy(pwaff
->p
[i
].set
);
2902 set_i
= isl_set_subtract(set_i
, locus
);
2904 set_i
= isl_set_intersect(set_i
, locus
);
2905 set
= isl_set_union_disjoint(set
, set_i
);
2908 isl_pw_aff_free(pwaff
);
2913 /* Return a set containing those elements in the domain
2914 * of "pa" where it is positive.
2916 __isl_give isl_set
*isl_pw_aff_pos_set(__isl_take isl_pw_aff
*pa
)
2918 return pw_aff_locus(pa
, &aff_pos_basic_set
, 0);
2921 /* Return a set containing those elements in the domain
2922 * of pwaff where it is non-negative.
2924 __isl_give isl_set
*isl_pw_aff_nonneg_set(__isl_take isl_pw_aff
*pwaff
)
2926 return pw_aff_locus(pwaff
, &aff_nonneg_basic_set
, 0);
2929 /* Return a set containing those elements in the domain
2930 * of pwaff where it is zero.
2932 __isl_give isl_set
*isl_pw_aff_zero_set(__isl_take isl_pw_aff
*pwaff
)
2934 return pw_aff_locus(pwaff
, &aff_zero_basic_set
, 0);
2937 /* Return a set containing those elements in the domain
2938 * of pwaff where it is not zero.
2940 __isl_give isl_set
*isl_pw_aff_non_zero_set(__isl_take isl_pw_aff
*pwaff
)
2942 return pw_aff_locus(pwaff
, &aff_zero_basic_set
, 1);
2945 /* Return a set containing those elements in the shared domain
2946 * of pwaff1 and pwaff2 where pwaff1 is greater than (or equal) to pwaff2.
2948 * We compute the difference on the shared domain and then construct
2949 * the set of values where this difference is non-negative.
2950 * If strict is set, we first subtract 1 from the difference.
2951 * If equal is set, we only return the elements where pwaff1 and pwaff2
2954 static __isl_give isl_set
*pw_aff_gte_set(__isl_take isl_pw_aff
*pwaff1
,
2955 __isl_take isl_pw_aff
*pwaff2
, int strict
, int equal
)
2957 isl_set
*set1
, *set2
;
2959 set1
= isl_pw_aff_domain(isl_pw_aff_copy(pwaff1
));
2960 set2
= isl_pw_aff_domain(isl_pw_aff_copy(pwaff2
));
2961 set1
= isl_set_intersect(set1
, set2
);
2962 pwaff1
= isl_pw_aff_intersect_domain(pwaff1
, isl_set_copy(set1
));
2963 pwaff2
= isl_pw_aff_intersect_domain(pwaff2
, isl_set_copy(set1
));
2964 pwaff1
= isl_pw_aff_add(pwaff1
, isl_pw_aff_neg(pwaff2
));
2967 isl_space
*dim
= isl_set_get_space(set1
);
2969 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(dim
));
2970 aff
= isl_aff_add_constant_si(aff
, -1);
2971 pwaff1
= isl_pw_aff_add(pwaff1
, isl_pw_aff_alloc(set1
, aff
));
2976 return isl_pw_aff_zero_set(pwaff1
);
2977 return isl_pw_aff_nonneg_set(pwaff1
);
2980 /* Return a set containing those elements in the shared domain
2981 * of pwaff1 and pwaff2 where pwaff1 is equal to pwaff2.
2983 static __isl_give isl_set
*pw_aff_eq_set(__isl_take isl_pw_aff
*pwaff1
,
2984 __isl_take isl_pw_aff
*pwaff2
)
2986 return pw_aff_gte_set(pwaff1
, pwaff2
, 0, 1);
2989 __isl_give isl_set
*isl_pw_aff_eq_set(__isl_take isl_pw_aff
*pwaff1
,
2990 __isl_take isl_pw_aff
*pwaff2
)
2992 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_eq_set
);
2995 /* Return a set containing those elements in the shared domain
2996 * of pwaff1 and pwaff2 where pwaff1 is greater than or equal to pwaff2.
2998 static __isl_give isl_set
*pw_aff_ge_set(__isl_take isl_pw_aff
*pwaff1
,
2999 __isl_take isl_pw_aff
*pwaff2
)
3001 return pw_aff_gte_set(pwaff1
, pwaff2
, 0, 0);
3004 __isl_give isl_set
*isl_pw_aff_ge_set(__isl_take isl_pw_aff
*pwaff1
,
3005 __isl_take isl_pw_aff
*pwaff2
)
3007 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_ge_set
);
3010 /* Return a set containing those elements in the shared domain
3011 * of pwaff1 and pwaff2 where pwaff1 is strictly greater than pwaff2.
3013 static __isl_give isl_set
*pw_aff_gt_set(__isl_take isl_pw_aff
*pwaff1
,
3014 __isl_take isl_pw_aff
*pwaff2
)
3016 return pw_aff_gte_set(pwaff1
, pwaff2
, 1, 0);
3019 __isl_give isl_set
*isl_pw_aff_gt_set(__isl_take isl_pw_aff
*pwaff1
,
3020 __isl_take isl_pw_aff
*pwaff2
)
3022 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_gt_set
);
3025 __isl_give isl_set
*isl_pw_aff_le_set(__isl_take isl_pw_aff
*pwaff1
,
3026 __isl_take isl_pw_aff
*pwaff2
)
3028 return isl_pw_aff_ge_set(pwaff2
, pwaff1
);
3031 __isl_give isl_set
*isl_pw_aff_lt_set(__isl_take isl_pw_aff
*pwaff1
,
3032 __isl_take isl_pw_aff
*pwaff2
)
3034 return isl_pw_aff_gt_set(pwaff2
, pwaff1
);
3037 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3038 * where the function values are ordered in the same way as "order",
3039 * which returns a set in the shared domain of its two arguments.
3040 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3042 * Let "pa1" and "pa2" be defined on domains A and B respectively.
3043 * We first pull back the two functions such that they are defined on
3044 * the domain [A -> B]. Then we apply "order", resulting in a set
3045 * in the space [A -> B]. Finally, we unwrap this set to obtain
3046 * a map in the space A -> B.
3048 static __isl_give isl_map
*isl_pw_aff_order_map_aligned(
3049 __isl_take isl_pw_aff
*pa1
, __isl_take isl_pw_aff
*pa2
,
3050 __isl_give isl_set
*(*order
)(__isl_take isl_pw_aff
*pa1
,
3051 __isl_take isl_pw_aff
*pa2
))
3053 isl_space
*space1
, *space2
;
3057 space1
= isl_space_domain(isl_pw_aff_get_space(pa1
));
3058 space2
= isl_space_domain(isl_pw_aff_get_space(pa2
));
3059 space1
= isl_space_map_from_domain_and_range(space1
, space2
);
3060 ma
= isl_multi_aff_domain_map(isl_space_copy(space1
));
3061 pa1
= isl_pw_aff_pullback_multi_aff(pa1
, ma
);
3062 ma
= isl_multi_aff_range_map(space1
);
3063 pa2
= isl_pw_aff_pullback_multi_aff(pa2
, ma
);
3064 set
= order(pa1
, pa2
);
3066 return isl_set_unwrap(set
);
3069 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3070 * where the function values are equal.
3071 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3073 static __isl_give isl_map
*isl_pw_aff_eq_map_aligned(__isl_take isl_pw_aff
*pa1
,
3074 __isl_take isl_pw_aff
*pa2
)
3076 return isl_pw_aff_order_map_aligned(pa1
, pa2
, &isl_pw_aff_eq_set
);
3079 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3080 * where the function values are equal.
3082 __isl_give isl_map
*isl_pw_aff_eq_map(__isl_take isl_pw_aff
*pa1
,
3083 __isl_take isl_pw_aff
*pa2
)
3085 return align_params_pw_pw_map_and(pa1
, pa2
, &isl_pw_aff_eq_map_aligned
);
3088 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3089 * where the function value of "pa1" is less than the function value of "pa2".
3090 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3092 static __isl_give isl_map
*isl_pw_aff_lt_map_aligned(__isl_take isl_pw_aff
*pa1
,
3093 __isl_take isl_pw_aff
*pa2
)
3095 return isl_pw_aff_order_map_aligned(pa1
, pa2
, &isl_pw_aff_lt_set
);
3098 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3099 * where the function value of "pa1" is less than the function value of "pa2".
3101 __isl_give isl_map
*isl_pw_aff_lt_map(__isl_take isl_pw_aff
*pa1
,
3102 __isl_take isl_pw_aff
*pa2
)
3104 return align_params_pw_pw_map_and(pa1
, pa2
, &isl_pw_aff_lt_map_aligned
);
3107 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3108 * where the function value of "pa1" is greater than the function value
3110 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3112 static __isl_give isl_map
*isl_pw_aff_gt_map_aligned(__isl_take isl_pw_aff
*pa1
,
3113 __isl_take isl_pw_aff
*pa2
)
3115 return isl_pw_aff_order_map_aligned(pa1
, pa2
, &isl_pw_aff_gt_set
);
3118 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3119 * where the function value of "pa1" is greater than the function value
3122 __isl_give isl_map
*isl_pw_aff_gt_map(__isl_take isl_pw_aff
*pa1
,
3123 __isl_take isl_pw_aff
*pa2
)
3125 return align_params_pw_pw_map_and(pa1
, pa2
, &isl_pw_aff_gt_map_aligned
);
3128 /* Return a set containing those elements in the shared domain
3129 * of the elements of list1 and list2 where each element in list1
3130 * has the relation specified by "fn" with each element in list2.
3132 static __isl_give isl_set
*pw_aff_list_set(__isl_take isl_pw_aff_list
*list1
,
3133 __isl_take isl_pw_aff_list
*list2
,
3134 __isl_give isl_set
*(*fn
)(__isl_take isl_pw_aff
*pwaff1
,
3135 __isl_take isl_pw_aff
*pwaff2
))
3141 if (!list1
|| !list2
)
3144 ctx
= isl_pw_aff_list_get_ctx(list1
);
3145 if (list1
->n
< 1 || list2
->n
< 1)
3146 isl_die(ctx
, isl_error_invalid
,
3147 "list should contain at least one element", goto error
);
3149 set
= isl_set_universe(isl_pw_aff_get_domain_space(list1
->p
[0]));
3150 for (i
= 0; i
< list1
->n
; ++i
)
3151 for (j
= 0; j
< list2
->n
; ++j
) {
3154 set_ij
= fn(isl_pw_aff_copy(list1
->p
[i
]),
3155 isl_pw_aff_copy(list2
->p
[j
]));
3156 set
= isl_set_intersect(set
, set_ij
);
3159 isl_pw_aff_list_free(list1
);
3160 isl_pw_aff_list_free(list2
);
3163 isl_pw_aff_list_free(list1
);
3164 isl_pw_aff_list_free(list2
);
3168 /* Return a set containing those elements in the shared domain
3169 * of the elements of list1 and list2 where each element in list1
3170 * is equal to each element in list2.
3172 __isl_give isl_set
*isl_pw_aff_list_eq_set(__isl_take isl_pw_aff_list
*list1
,
3173 __isl_take isl_pw_aff_list
*list2
)
3175 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_eq_set
);
3178 __isl_give isl_set
*isl_pw_aff_list_ne_set(__isl_take isl_pw_aff_list
*list1
,
3179 __isl_take isl_pw_aff_list
*list2
)
3181 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_ne_set
);
3184 /* Return a set containing those elements in the shared domain
3185 * of the elements of list1 and list2 where each element in list1
3186 * is less than or equal to each element in list2.
3188 __isl_give isl_set
*isl_pw_aff_list_le_set(__isl_take isl_pw_aff_list
*list1
,
3189 __isl_take isl_pw_aff_list
*list2
)
3191 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_le_set
);
3194 __isl_give isl_set
*isl_pw_aff_list_lt_set(__isl_take isl_pw_aff_list
*list1
,
3195 __isl_take isl_pw_aff_list
*list2
)
3197 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_lt_set
);
3200 __isl_give isl_set
*isl_pw_aff_list_ge_set(__isl_take isl_pw_aff_list
*list1
,
3201 __isl_take isl_pw_aff_list
*list2
)
3203 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_ge_set
);
3206 __isl_give isl_set
*isl_pw_aff_list_gt_set(__isl_take isl_pw_aff_list
*list1
,
3207 __isl_take isl_pw_aff_list
*list2
)
3209 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_gt_set
);
3213 /* Return a set containing those elements in the shared domain
3214 * of pwaff1 and pwaff2 where pwaff1 is not equal to pwaff2.
3216 static __isl_give isl_set
*pw_aff_ne_set(__isl_take isl_pw_aff
*pwaff1
,
3217 __isl_take isl_pw_aff
*pwaff2
)
3219 isl_set
*set_lt
, *set_gt
;
3221 set_lt
= isl_pw_aff_lt_set(isl_pw_aff_copy(pwaff1
),
3222 isl_pw_aff_copy(pwaff2
));
3223 set_gt
= isl_pw_aff_gt_set(pwaff1
, pwaff2
);
3224 return isl_set_union_disjoint(set_lt
, set_gt
);
3227 __isl_give isl_set
*isl_pw_aff_ne_set(__isl_take isl_pw_aff
*pwaff1
,
3228 __isl_take isl_pw_aff
*pwaff2
)
3230 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_ne_set
);
3233 __isl_give isl_pw_aff
*isl_pw_aff_scale_down(__isl_take isl_pw_aff
*pwaff
,
3238 if (isl_int_is_one(v
))
3240 if (!isl_int_is_pos(v
))
3241 isl_die(isl_pw_aff_get_ctx(pwaff
), isl_error_invalid
,
3242 "factor needs to be positive",
3243 return isl_pw_aff_free(pwaff
));
3244 pwaff
= isl_pw_aff_cow(pwaff
);
3250 for (i
= 0; i
< pwaff
->n
; ++i
) {
3251 pwaff
->p
[i
].aff
= isl_aff_scale_down(pwaff
->p
[i
].aff
, v
);
3252 if (!pwaff
->p
[i
].aff
)
3253 return isl_pw_aff_free(pwaff
);
3259 __isl_give isl_pw_aff
*isl_pw_aff_floor(__isl_take isl_pw_aff
*pwaff
)
3263 pwaff
= isl_pw_aff_cow(pwaff
);
3269 for (i
= 0; i
< pwaff
->n
; ++i
) {
3270 pwaff
->p
[i
].aff
= isl_aff_floor(pwaff
->p
[i
].aff
);
3271 if (!pwaff
->p
[i
].aff
)
3272 return isl_pw_aff_free(pwaff
);
3278 __isl_give isl_pw_aff
*isl_pw_aff_ceil(__isl_take isl_pw_aff
*pwaff
)
3282 pwaff
= isl_pw_aff_cow(pwaff
);
3288 for (i
= 0; i
< pwaff
->n
; ++i
) {
3289 pwaff
->p
[i
].aff
= isl_aff_ceil(pwaff
->p
[i
].aff
);
3290 if (!pwaff
->p
[i
].aff
)
3291 return isl_pw_aff_free(pwaff
);
3297 /* Assuming that "cond1" and "cond2" are disjoint,
3298 * return an affine expression that is equal to pwaff1 on cond1
3299 * and to pwaff2 on cond2.
3301 static __isl_give isl_pw_aff
*isl_pw_aff_select(
3302 __isl_take isl_set
*cond1
, __isl_take isl_pw_aff
*pwaff1
,
3303 __isl_take isl_set
*cond2
, __isl_take isl_pw_aff
*pwaff2
)
3305 pwaff1
= isl_pw_aff_intersect_domain(pwaff1
, cond1
);
3306 pwaff2
= isl_pw_aff_intersect_domain(pwaff2
, cond2
);
3308 return isl_pw_aff_add_disjoint(pwaff1
, pwaff2
);
3311 /* Return an affine expression that is equal to pwaff_true for elements
3312 * where "cond" is non-zero and to pwaff_false for elements where "cond"
3314 * That is, return cond ? pwaff_true : pwaff_false;
3316 * If "cond" involves and NaN, then we conservatively return a NaN
3317 * on its entire domain. In principle, we could consider the pieces
3318 * where it is NaN separately from those where it is not.
3320 * If "pwaff_true" and "pwaff_false" are obviously equal to each other,
3321 * then only use the domain of "cond" to restrict the domain.
3323 __isl_give isl_pw_aff
*isl_pw_aff_cond(__isl_take isl_pw_aff
*cond
,
3324 __isl_take isl_pw_aff
*pwaff_true
, __isl_take isl_pw_aff
*pwaff_false
)
3326 isl_set
*cond_true
, *cond_false
;
3331 if (isl_pw_aff_involves_nan(cond
)) {
3332 isl_space
*space
= isl_pw_aff_get_domain_space(cond
);
3333 isl_local_space
*ls
= isl_local_space_from_space(space
);
3334 isl_pw_aff_free(cond
);
3335 isl_pw_aff_free(pwaff_true
);
3336 isl_pw_aff_free(pwaff_false
);
3337 return isl_pw_aff_nan_on_domain(ls
);
3340 pwaff_true
= isl_pw_aff_align_params(pwaff_true
,
3341 isl_pw_aff_get_space(pwaff_false
));
3342 pwaff_false
= isl_pw_aff_align_params(pwaff_false
,
3343 isl_pw_aff_get_space(pwaff_true
));
3344 equal
= isl_pw_aff_plain_is_equal(pwaff_true
, pwaff_false
);
3350 dom
= isl_set_coalesce(isl_pw_aff_domain(cond
));
3351 isl_pw_aff_free(pwaff_false
);
3352 return isl_pw_aff_intersect_domain(pwaff_true
, dom
);
3355 cond_true
= isl_pw_aff_non_zero_set(isl_pw_aff_copy(cond
));
3356 cond_false
= isl_pw_aff_zero_set(cond
);
3357 return isl_pw_aff_select(cond_true
, pwaff_true
,
3358 cond_false
, pwaff_false
);
3360 isl_pw_aff_free(cond
);
3361 isl_pw_aff_free(pwaff_true
);
3362 isl_pw_aff_free(pwaff_false
);
3366 isl_bool
isl_aff_is_cst(__isl_keep isl_aff
*aff
)
3369 return isl_bool_error
;
3371 return isl_seq_first_non_zero(aff
->v
->el
+ 2, aff
->v
->size
- 2) == -1;
3374 /* Check whether pwaff is a piecewise constant.
3376 isl_bool
isl_pw_aff_is_cst(__isl_keep isl_pw_aff
*pwaff
)
3381 return isl_bool_error
;
3383 for (i
= 0; i
< pwaff
->n
; ++i
) {
3384 isl_bool is_cst
= isl_aff_is_cst(pwaff
->p
[i
].aff
);
3385 if (is_cst
< 0 || !is_cst
)
3389 return isl_bool_true
;
3392 /* Are all elements of "mpa" piecewise constants?
3394 isl_bool
isl_multi_pw_aff_is_cst(__isl_keep isl_multi_pw_aff
*mpa
)
3399 return isl_bool_error
;
3401 for (i
= 0; i
< mpa
->n
; ++i
) {
3402 isl_bool is_cst
= isl_pw_aff_is_cst(mpa
->u
.p
[i
]);
3403 if (is_cst
< 0 || !is_cst
)
3407 return isl_bool_true
;
3410 /* Return the product of "aff1" and "aff2".
3412 * If either of the two is NaN, then the result is NaN.
3414 * Otherwise, at least one of "aff1" or "aff2" needs to be a constant.
3416 __isl_give isl_aff
*isl_aff_mul(__isl_take isl_aff
*aff1
,
3417 __isl_take isl_aff
*aff2
)
3422 if (isl_aff_is_nan(aff1
)) {
3426 if (isl_aff_is_nan(aff2
)) {
3431 if (!isl_aff_is_cst(aff2
) && isl_aff_is_cst(aff1
))
3432 return isl_aff_mul(aff2
, aff1
);
3434 if (!isl_aff_is_cst(aff2
))
3435 isl_die(isl_aff_get_ctx(aff1
), isl_error_invalid
,
3436 "at least one affine expression should be constant",
3439 aff1
= isl_aff_cow(aff1
);
3443 aff1
= isl_aff_scale(aff1
, aff2
->v
->el
[1]);
3444 aff1
= isl_aff_scale_down(aff1
, aff2
->v
->el
[0]);
3454 /* Divide "aff1" by "aff2", assuming "aff2" is a constant.
3456 * If either of the two is NaN, then the result is NaN.
3458 __isl_give isl_aff
*isl_aff_div(__isl_take isl_aff
*aff1
,
3459 __isl_take isl_aff
*aff2
)
3467 if (isl_aff_is_nan(aff1
)) {
3471 if (isl_aff_is_nan(aff2
)) {
3476 is_cst
= isl_aff_is_cst(aff2
);
3480 isl_die(isl_aff_get_ctx(aff2
), isl_error_invalid
,
3481 "second argument should be a constant", goto error
);
3486 neg
= isl_int_is_neg(aff2
->v
->el
[1]);
3488 isl_int_neg(aff2
->v
->el
[0], aff2
->v
->el
[0]);
3489 isl_int_neg(aff2
->v
->el
[1], aff2
->v
->el
[1]);
3492 aff1
= isl_aff_scale(aff1
, aff2
->v
->el
[0]);
3493 aff1
= isl_aff_scale_down(aff1
, aff2
->v
->el
[1]);
3496 isl_int_neg(aff2
->v
->el
[0], aff2
->v
->el
[0]);
3497 isl_int_neg(aff2
->v
->el
[1], aff2
->v
->el
[1]);
3508 static __isl_give isl_pw_aff
*pw_aff_add(__isl_take isl_pw_aff
*pwaff1
,
3509 __isl_take isl_pw_aff
*pwaff2
)
3511 return isl_pw_aff_on_shared_domain(pwaff1
, pwaff2
, &isl_aff_add
);
3514 __isl_give isl_pw_aff
*isl_pw_aff_add(__isl_take isl_pw_aff
*pwaff1
,
3515 __isl_take isl_pw_aff
*pwaff2
)
3517 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
, &pw_aff_add
);
3520 __isl_give isl_pw_aff
*isl_pw_aff_union_add(__isl_take isl_pw_aff
*pwaff1
,
3521 __isl_take isl_pw_aff
*pwaff2
)
3523 return isl_pw_aff_union_add_(pwaff1
, pwaff2
);
3526 static __isl_give isl_pw_aff
*pw_aff_mul(__isl_take isl_pw_aff
*pwaff1
,
3527 __isl_take isl_pw_aff
*pwaff2
)
3529 return isl_pw_aff_on_shared_domain(pwaff1
, pwaff2
, &isl_aff_mul
);
3532 __isl_give isl_pw_aff
*isl_pw_aff_mul(__isl_take isl_pw_aff
*pwaff1
,
3533 __isl_take isl_pw_aff
*pwaff2
)
3535 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
, &pw_aff_mul
);
3538 static __isl_give isl_pw_aff
*pw_aff_div(__isl_take isl_pw_aff
*pa1
,
3539 __isl_take isl_pw_aff
*pa2
)
3541 return isl_pw_aff_on_shared_domain(pa1
, pa2
, &isl_aff_div
);
3544 /* Divide "pa1" by "pa2", assuming "pa2" is a piecewise constant.
3546 __isl_give isl_pw_aff
*isl_pw_aff_div(__isl_take isl_pw_aff
*pa1
,
3547 __isl_take isl_pw_aff
*pa2
)
3551 is_cst
= isl_pw_aff_is_cst(pa2
);
3555 isl_die(isl_pw_aff_get_ctx(pa2
), isl_error_invalid
,
3556 "second argument should be a piecewise constant",
3558 return isl_pw_aff_align_params_pw_pw_and(pa1
, pa2
, &pw_aff_div
);
3560 isl_pw_aff_free(pa1
);
3561 isl_pw_aff_free(pa2
);
3565 /* Compute the quotient of the integer division of "pa1" by "pa2"
3566 * with rounding towards zero.
3567 * "pa2" is assumed to be a piecewise constant.
3569 * In particular, return
3571 * pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2)
3574 __isl_give isl_pw_aff
*isl_pw_aff_tdiv_q(__isl_take isl_pw_aff
*pa1
,
3575 __isl_take isl_pw_aff
*pa2
)
3581 is_cst
= isl_pw_aff_is_cst(pa2
);
3585 isl_die(isl_pw_aff_get_ctx(pa2
), isl_error_invalid
,
3586 "second argument should be a piecewise constant",
3589 pa1
= isl_pw_aff_div(pa1
, pa2
);
3591 cond
= isl_pw_aff_nonneg_set(isl_pw_aff_copy(pa1
));
3592 f
= isl_pw_aff_floor(isl_pw_aff_copy(pa1
));
3593 c
= isl_pw_aff_ceil(pa1
);
3594 return isl_pw_aff_cond(isl_set_indicator_function(cond
), f
, c
);
3596 isl_pw_aff_free(pa1
);
3597 isl_pw_aff_free(pa2
);
3601 /* Compute the remainder of the integer division of "pa1" by "pa2"
3602 * with rounding towards zero.
3603 * "pa2" is assumed to be a piecewise constant.
3605 * In particular, return
3607 * pa1 - pa2 * (pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2))
3610 __isl_give isl_pw_aff
*isl_pw_aff_tdiv_r(__isl_take isl_pw_aff
*pa1
,
3611 __isl_take isl_pw_aff
*pa2
)
3616 is_cst
= isl_pw_aff_is_cst(pa2
);
3620 isl_die(isl_pw_aff_get_ctx(pa2
), isl_error_invalid
,
3621 "second argument should be a piecewise constant",
3623 res
= isl_pw_aff_tdiv_q(isl_pw_aff_copy(pa1
), isl_pw_aff_copy(pa2
));
3624 res
= isl_pw_aff_mul(pa2
, res
);
3625 res
= isl_pw_aff_sub(pa1
, res
);
3628 isl_pw_aff_free(pa1
);
3629 isl_pw_aff_free(pa2
);
3633 /* Does either of "pa1" or "pa2" involve any NaN2?
3635 static isl_bool
either_involves_nan(__isl_keep isl_pw_aff
*pa1
,
3636 __isl_keep isl_pw_aff
*pa2
)
3640 has_nan
= isl_pw_aff_involves_nan(pa1
);
3641 if (has_nan
< 0 || has_nan
)
3643 return isl_pw_aff_involves_nan(pa2
);
3646 /* Replace "pa1" and "pa2" (at least one of which involves a NaN)
3647 * by a NaN on their shared domain.
3649 * In principle, the result could be refined to only being NaN
3650 * on the parts of this domain where at least one of "pa1" or "pa2" is NaN.
3652 static __isl_give isl_pw_aff
*replace_by_nan(__isl_take isl_pw_aff
*pa1
,
3653 __isl_take isl_pw_aff
*pa2
)
3655 isl_local_space
*ls
;
3659 dom
= isl_set_intersect(isl_pw_aff_domain(pa1
), isl_pw_aff_domain(pa2
));
3660 ls
= isl_local_space_from_space(isl_set_get_space(dom
));
3661 pa
= isl_pw_aff_nan_on_domain(ls
);
3662 pa
= isl_pw_aff_intersect_domain(pa
, dom
);
3667 static __isl_give isl_pw_aff
*pw_aff_min(__isl_take isl_pw_aff
*pwaff1
,
3668 __isl_take isl_pw_aff
*pwaff2
)
3673 dom
= isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1
)),
3674 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2
)));
3675 le
= isl_pw_aff_le_set(isl_pw_aff_copy(pwaff1
),
3676 isl_pw_aff_copy(pwaff2
));
3677 dom
= isl_set_subtract(dom
, isl_set_copy(le
));
3678 return isl_pw_aff_select(le
, pwaff1
, dom
, pwaff2
);
3681 static __isl_give isl_pw_aff
*pw_aff_max(__isl_take isl_pw_aff
*pwaff1
,
3682 __isl_take isl_pw_aff
*pwaff2
)
3687 dom
= isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1
)),
3688 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2
)));
3689 ge
= isl_pw_aff_ge_set(isl_pw_aff_copy(pwaff1
),
3690 isl_pw_aff_copy(pwaff2
));
3691 dom
= isl_set_subtract(dom
, isl_set_copy(ge
));
3692 return isl_pw_aff_select(ge
, pwaff1
, dom
, pwaff2
);
3695 /* Return an expression for the minimum (if "max" is not set) or
3696 * the maximum (if "max" is set) of "pa1" and "pa2".
3697 * If either expression involves any NaN, then return a NaN
3698 * on the shared domain as result.
3700 static __isl_give isl_pw_aff
*pw_aff_min_max(__isl_take isl_pw_aff
*pa1
,
3701 __isl_take isl_pw_aff
*pa2
, int max
)
3705 has_nan
= either_involves_nan(pa1
, pa2
);
3707 pa1
= isl_pw_aff_free(pa1
);
3709 return replace_by_nan(pa1
, pa2
);
3712 return isl_pw_aff_align_params_pw_pw_and(pa1
, pa2
, &pw_aff_max
);
3714 return isl_pw_aff_align_params_pw_pw_and(pa1
, pa2
, &pw_aff_min
);
3717 /* Return an expression for the minimum of "pwaff1" and "pwaff2".
3719 __isl_give isl_pw_aff
*isl_pw_aff_min(__isl_take isl_pw_aff
*pwaff1
,
3720 __isl_take isl_pw_aff
*pwaff2
)
3722 return pw_aff_min_max(pwaff1
, pwaff2
, 0);
3725 /* Return an expression for the maximum of "pwaff1" and "pwaff2".
3727 __isl_give isl_pw_aff
*isl_pw_aff_max(__isl_take isl_pw_aff
*pwaff1
,
3728 __isl_take isl_pw_aff
*pwaff2
)
3730 return pw_aff_min_max(pwaff1
, pwaff2
, 1);
3733 static __isl_give isl_pw_aff
*pw_aff_list_reduce(
3734 __isl_take isl_pw_aff_list
*list
,
3735 __isl_give isl_pw_aff
*(*fn
)(__isl_take isl_pw_aff
*pwaff1
,
3736 __isl_take isl_pw_aff
*pwaff2
))
3745 ctx
= isl_pw_aff_list_get_ctx(list
);
3747 isl_die(ctx
, isl_error_invalid
,
3748 "list should contain at least one element", goto error
);
3750 res
= isl_pw_aff_copy(list
->p
[0]);
3751 for (i
= 1; i
< list
->n
; ++i
)
3752 res
= fn(res
, isl_pw_aff_copy(list
->p
[i
]));
3754 isl_pw_aff_list_free(list
);
3757 isl_pw_aff_list_free(list
);
3761 /* Return an isl_pw_aff that maps each element in the intersection of the
3762 * domains of the elements of list to the minimal corresponding affine
3765 __isl_give isl_pw_aff
*isl_pw_aff_list_min(__isl_take isl_pw_aff_list
*list
)
3767 return pw_aff_list_reduce(list
, &isl_pw_aff_min
);
3770 /* Return an isl_pw_aff that maps each element in the intersection of the
3771 * domains of the elements of list to the maximal corresponding affine
3774 __isl_give isl_pw_aff
*isl_pw_aff_list_max(__isl_take isl_pw_aff_list
*list
)
3776 return pw_aff_list_reduce(list
, &isl_pw_aff_max
);
3779 /* Mark the domains of "pwaff" as rational.
3781 __isl_give isl_pw_aff
*isl_pw_aff_set_rational(__isl_take isl_pw_aff
*pwaff
)
3785 pwaff
= isl_pw_aff_cow(pwaff
);
3791 for (i
= 0; i
< pwaff
->n
; ++i
) {
3792 pwaff
->p
[i
].set
= isl_set_set_rational(pwaff
->p
[i
].set
);
3793 if (!pwaff
->p
[i
].set
)
3794 return isl_pw_aff_free(pwaff
);
3800 /* Mark the domains of the elements of "list" as rational.
3802 __isl_give isl_pw_aff_list
*isl_pw_aff_list_set_rational(
3803 __isl_take isl_pw_aff_list
*list
)
3813 for (i
= 0; i
< n
; ++i
) {
3816 pa
= isl_pw_aff_list_get_pw_aff(list
, i
);
3817 pa
= isl_pw_aff_set_rational(pa
);
3818 list
= isl_pw_aff_list_set_pw_aff(list
, i
, pa
);
3824 /* Do the parameters of "aff" match those of "space"?
3826 isl_bool
isl_aff_matching_params(__isl_keep isl_aff
*aff
,
3827 __isl_keep isl_space
*space
)
3829 isl_space
*aff_space
;
3833 return isl_bool_error
;
3835 aff_space
= isl_aff_get_domain_space(aff
);
3837 match
= isl_space_has_equal_params(space
, aff_space
);
3839 isl_space_free(aff_space
);
3843 /* Check that the domain space of "aff" matches "space".
3845 isl_stat
isl_aff_check_match_domain_space(__isl_keep isl_aff
*aff
,
3846 __isl_keep isl_space
*space
)
3848 isl_space
*aff_space
;
3852 return isl_stat_error
;
3854 aff_space
= isl_aff_get_domain_space(aff
);
3856 match
= isl_space_has_equal_params(space
, aff_space
);
3860 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
3861 "parameters don't match", goto error
);
3862 match
= isl_space_tuple_is_equal(space
, isl_dim_in
,
3863 aff_space
, isl_dim_set
);
3867 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
3868 "domains don't match", goto error
);
3869 isl_space_free(aff_space
);
3872 isl_space_free(aff_space
);
3873 return isl_stat_error
;
3882 #include <isl_multi_no_explicit_domain.c>
3883 #include <isl_multi_templ.c>
3884 #include <isl_multi_apply_set.c>
3885 #include <isl_multi_cmp.c>
3886 #include <isl_multi_dims.c>
3887 #include <isl_multi_floor.c>
3888 #include <isl_multi_gist.c>
3892 /* Construct an isl_multi_aff living in "space" that corresponds
3893 * to the affine transformation matrix "mat".
3895 __isl_give isl_multi_aff
*isl_multi_aff_from_aff_mat(
3896 __isl_take isl_space
*space
, __isl_take isl_mat
*mat
)
3899 isl_local_space
*ls
= NULL
;
3900 isl_multi_aff
*ma
= NULL
;
3901 int n_row
, n_col
, n_out
, total
;
3907 ctx
= isl_mat_get_ctx(mat
);
3909 n_row
= isl_mat_rows(mat
);
3910 n_col
= isl_mat_cols(mat
);
3912 isl_die(ctx
, isl_error_invalid
,
3913 "insufficient number of rows", goto error
);
3915 isl_die(ctx
, isl_error_invalid
,
3916 "insufficient number of columns", goto error
);
3917 n_out
= isl_space_dim(space
, isl_dim_out
);
3918 total
= isl_space_dim(space
, isl_dim_all
);
3919 if (1 + n_out
!= n_row
|| 2 + total
!= n_row
+ n_col
)
3920 isl_die(ctx
, isl_error_invalid
,
3921 "dimension mismatch", goto error
);
3923 ma
= isl_multi_aff_zero(isl_space_copy(space
));
3924 ls
= isl_local_space_from_space(isl_space_domain(space
));
3926 for (i
= 0; i
< n_row
- 1; ++i
) {
3930 v
= isl_vec_alloc(ctx
, 1 + n_col
);
3933 isl_int_set(v
->el
[0], mat
->row
[0][0]);
3934 isl_seq_cpy(v
->el
+ 1, mat
->row
[1 + i
], n_col
);
3935 v
= isl_vec_normalize(v
);
3936 aff
= isl_aff_alloc_vec(isl_local_space_copy(ls
), v
);
3937 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
3940 isl_local_space_free(ls
);
3944 isl_local_space_free(ls
);
3946 isl_multi_aff_free(ma
);
3950 /* Remove any internal structure of the domain of "ma".
3951 * If there is any such internal structure in the input,
3952 * then the name of the corresponding space is also removed.
3954 __isl_give isl_multi_aff
*isl_multi_aff_flatten_domain(
3955 __isl_take isl_multi_aff
*ma
)
3962 if (!ma
->space
->nested
[0])
3965 space
= isl_multi_aff_get_space(ma
);
3966 space
= isl_space_flatten_domain(space
);
3967 ma
= isl_multi_aff_reset_space(ma
, space
);
3972 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
3973 * of the space to its domain.
3975 __isl_give isl_multi_aff
*isl_multi_aff_domain_map(__isl_take isl_space
*space
)
3978 isl_local_space
*ls
;
3983 if (!isl_space_is_map(space
))
3984 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
3985 "not a map space", goto error
);
3987 n_in
= isl_space_dim(space
, isl_dim_in
);
3988 space
= isl_space_domain_map(space
);
3990 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
3992 isl_space_free(space
);
3996 space
= isl_space_domain(space
);
3997 ls
= isl_local_space_from_space(space
);
3998 for (i
= 0; i
< n_in
; ++i
) {
4001 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
4003 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
4005 isl_local_space_free(ls
);
4008 isl_space_free(space
);
4012 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
4013 * of the space to its range.
4015 __isl_give isl_multi_aff
*isl_multi_aff_range_map(__isl_take isl_space
*space
)
4018 isl_local_space
*ls
;
4023 if (!isl_space_is_map(space
))
4024 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
4025 "not a map space", goto error
);
4027 n_in
= isl_space_dim(space
, isl_dim_in
);
4028 n_out
= isl_space_dim(space
, isl_dim_out
);
4029 space
= isl_space_range_map(space
);
4031 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
4033 isl_space_free(space
);
4037 space
= isl_space_domain(space
);
4038 ls
= isl_local_space_from_space(space
);
4039 for (i
= 0; i
< n_out
; ++i
) {
4042 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
4043 isl_dim_set
, n_in
+ i
);
4044 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
4046 isl_local_space_free(ls
);
4049 isl_space_free(space
);
4053 /* Given a map space, return an isl_pw_multi_aff that maps a wrapped copy
4054 * of the space to its range.
4056 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_range_map(
4057 __isl_take isl_space
*space
)
4059 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_range_map(space
));
4062 /* Given the space of a set and a range of set dimensions,
4063 * construct an isl_multi_aff that projects out those dimensions.
4065 __isl_give isl_multi_aff
*isl_multi_aff_project_out_map(
4066 __isl_take isl_space
*space
, enum isl_dim_type type
,
4067 unsigned first
, unsigned n
)
4070 isl_local_space
*ls
;
4075 if (!isl_space_is_set(space
))
4076 isl_die(isl_space_get_ctx(space
), isl_error_unsupported
,
4077 "expecting set space", goto error
);
4078 if (type
!= isl_dim_set
)
4079 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
4080 "only set dimensions can be projected out", goto error
);
4082 dim
= isl_space_dim(space
, isl_dim_set
);
4083 if (first
+ n
> dim
)
4084 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
4085 "range out of bounds", goto error
);
4087 space
= isl_space_from_domain(space
);
4088 space
= isl_space_add_dims(space
, isl_dim_out
, dim
- n
);
4091 return isl_multi_aff_alloc(space
);
4093 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
4094 space
= isl_space_domain(space
);
4095 ls
= isl_local_space_from_space(space
);
4097 for (i
= 0; i
< first
; ++i
) {
4100 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
4102 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
4105 for (i
= 0; i
< dim
- (first
+ n
); ++i
) {
4108 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
4109 isl_dim_set
, first
+ n
+ i
);
4110 ma
= isl_multi_aff_set_aff(ma
, first
+ i
, aff
);
4113 isl_local_space_free(ls
);
4116 isl_space_free(space
);
4120 /* Given the space of a set and a range of set dimensions,
4121 * construct an isl_pw_multi_aff that projects out those dimensions.
4123 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_project_out_map(
4124 __isl_take isl_space
*space
, enum isl_dim_type type
,
4125 unsigned first
, unsigned n
)
4129 ma
= isl_multi_aff_project_out_map(space
, type
, first
, n
);
4130 return isl_pw_multi_aff_from_multi_aff(ma
);
4133 /* Create an isl_pw_multi_aff with the given isl_multi_aff on a universe
4136 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_multi_aff(
4137 __isl_take isl_multi_aff
*ma
)
4139 isl_set
*dom
= isl_set_universe(isl_multi_aff_get_domain_space(ma
));
4140 return isl_pw_multi_aff_alloc(dom
, ma
);
4143 /* Create a piecewise multi-affine expression in the given space that maps each
4144 * input dimension to the corresponding output dimension.
4146 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_identity(
4147 __isl_take isl_space
*space
)
4149 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_identity(space
));
4152 /* Exploit the equalities in "eq" to simplify the affine expressions.
4154 static __isl_give isl_multi_aff
*isl_multi_aff_substitute_equalities(
4155 __isl_take isl_multi_aff
*maff
, __isl_take isl_basic_set
*eq
)
4159 maff
= isl_multi_aff_cow(maff
);
4163 for (i
= 0; i
< maff
->n
; ++i
) {
4164 maff
->u
.p
[i
] = isl_aff_substitute_equalities(maff
->u
.p
[i
],
4165 isl_basic_set_copy(eq
));
4170 isl_basic_set_free(eq
);
4173 isl_basic_set_free(eq
);
4174 isl_multi_aff_free(maff
);
4178 __isl_give isl_multi_aff
*isl_multi_aff_scale(__isl_take isl_multi_aff
*maff
,
4183 maff
= isl_multi_aff_cow(maff
);
4187 for (i
= 0; i
< maff
->n
; ++i
) {
4188 maff
->u
.p
[i
] = isl_aff_scale(maff
->u
.p
[i
], f
);
4190 return isl_multi_aff_free(maff
);
4196 __isl_give isl_multi_aff
*isl_multi_aff_add_on_domain(__isl_keep isl_set
*dom
,
4197 __isl_take isl_multi_aff
*maff1
, __isl_take isl_multi_aff
*maff2
)
4199 maff1
= isl_multi_aff_add(maff1
, maff2
);
4200 maff1
= isl_multi_aff_gist(maff1
, isl_set_copy(dom
));
4204 int isl_multi_aff_is_empty(__isl_keep isl_multi_aff
*maff
)
4212 /* Return the set of domain elements where "ma1" is lexicographically
4213 * smaller than or equal to "ma2".
4215 __isl_give isl_set
*isl_multi_aff_lex_le_set(__isl_take isl_multi_aff
*ma1
,
4216 __isl_take isl_multi_aff
*ma2
)
4218 return isl_multi_aff_lex_ge_set(ma2
, ma1
);
4221 /* Return the set of domain elements where "ma1" is lexicographically
4222 * smaller than "ma2".
4224 __isl_give isl_set
*isl_multi_aff_lex_lt_set(__isl_take isl_multi_aff
*ma1
,
4225 __isl_take isl_multi_aff
*ma2
)
4227 return isl_multi_aff_lex_gt_set(ma2
, ma1
);
4230 /* Return the set of domain elements where "ma1" and "ma2"
4233 static __isl_give isl_set
*isl_multi_aff_order_set(
4234 __isl_take isl_multi_aff
*ma1
, __isl_take isl_multi_aff
*ma2
,
4235 __isl_give isl_map
*order(__isl_take isl_space
*set_space
))
4238 isl_map
*map1
, *map2
;
4241 map1
= isl_map_from_multi_aff(ma1
);
4242 map2
= isl_map_from_multi_aff(ma2
);
4243 map
= isl_map_range_product(map1
, map2
);
4244 space
= isl_space_range(isl_map_get_space(map
));
4245 space
= isl_space_domain(isl_space_unwrap(space
));
4247 map
= isl_map_intersect_range(map
, isl_map_wrap(ge
));
4249 return isl_map_domain(map
);
4252 /* Return the set of domain elements where "ma1" is lexicographically
4253 * greater than or equal to "ma2".
4255 __isl_give isl_set
*isl_multi_aff_lex_ge_set(__isl_take isl_multi_aff
*ma1
,
4256 __isl_take isl_multi_aff
*ma2
)
4258 return isl_multi_aff_order_set(ma1
, ma2
, &isl_map_lex_ge
);
4261 /* Return the set of domain elements where "ma1" is lexicographically
4262 * greater than "ma2".
4264 __isl_give isl_set
*isl_multi_aff_lex_gt_set(__isl_take isl_multi_aff
*ma1
,
4265 __isl_take isl_multi_aff
*ma2
)
4267 return isl_multi_aff_order_set(ma1
, ma2
, &isl_map_lex_gt
);
4271 #define PW isl_pw_multi_aff
4273 #define EL isl_multi_aff
4275 #define EL_IS_ZERO is_empty
4279 #define IS_ZERO is_empty
4282 #undef DEFAULT_IS_ZERO
4283 #define DEFAULT_IS_ZERO 0
4287 #define NO_INSERT_DIMS
4291 #include <isl_pw_templ.c>
4292 #include <isl_pw_union_opt.c>
4297 #define BASE pw_multi_aff
4299 #include <isl_union_multi.c>
4300 #include <isl_union_neg.c>
4302 static __isl_give isl_pw_multi_aff
*pw_multi_aff_union_lexmax(
4303 __isl_take isl_pw_multi_aff
*pma1
,
4304 __isl_take isl_pw_multi_aff
*pma2
)
4306 return isl_pw_multi_aff_union_opt_cmp(pma1
, pma2
,
4307 &isl_multi_aff_lex_ge_set
);
4310 /* Given two piecewise multi affine expressions, return a piecewise
4311 * multi-affine expression defined on the union of the definition domains
4312 * of the inputs that is equal to the lexicographic maximum of the two
4313 * inputs on each cell. If only one of the two inputs is defined on
4314 * a given cell, then it is considered to be the maximum.
4316 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_union_lexmax(
4317 __isl_take isl_pw_multi_aff
*pma1
,
4318 __isl_take isl_pw_multi_aff
*pma2
)
4320 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
4321 &pw_multi_aff_union_lexmax
);
4324 static __isl_give isl_pw_multi_aff
*pw_multi_aff_union_lexmin(
4325 __isl_take isl_pw_multi_aff
*pma1
,
4326 __isl_take isl_pw_multi_aff
*pma2
)
4328 return isl_pw_multi_aff_union_opt_cmp(pma1
, pma2
,
4329 &isl_multi_aff_lex_le_set
);
4332 /* Given two piecewise multi affine expressions, return a piecewise
4333 * multi-affine expression defined on the union of the definition domains
4334 * of the inputs that is equal to the lexicographic minimum of the two
4335 * inputs on each cell. If only one of the two inputs is defined on
4336 * a given cell, then it is considered to be the minimum.
4338 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_union_lexmin(
4339 __isl_take isl_pw_multi_aff
*pma1
,
4340 __isl_take isl_pw_multi_aff
*pma2
)
4342 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
4343 &pw_multi_aff_union_lexmin
);
4346 static __isl_give isl_pw_multi_aff
*pw_multi_aff_add(
4347 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4349 return isl_pw_multi_aff_on_shared_domain(pma1
, pma2
,
4350 &isl_multi_aff_add
);
4353 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_add(
4354 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4356 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
4360 static __isl_give isl_pw_multi_aff
*pw_multi_aff_sub(
4361 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4363 return isl_pw_multi_aff_on_shared_domain(pma1
, pma2
,
4364 &isl_multi_aff_sub
);
4367 /* Subtract "pma2" from "pma1" and return the result.
4369 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_sub(
4370 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4372 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
4376 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_union_add(
4377 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4379 return isl_pw_multi_aff_union_add_(pma1
, pma2
);
4382 /* Compute the sum of "upa1" and "upa2" on the union of their domains,
4383 * with the actual sum on the shared domain and
4384 * the defined expression on the symmetric difference of the domains.
4386 __isl_give isl_union_pw_aff
*isl_union_pw_aff_union_add(
4387 __isl_take isl_union_pw_aff
*upa1
, __isl_take isl_union_pw_aff
*upa2
)
4389 return isl_union_pw_aff_union_add_(upa1
, upa2
);
4392 /* Compute the sum of "upma1" and "upma2" on the union of their domains,
4393 * with the actual sum on the shared domain and
4394 * the defined expression on the symmetric difference of the domains.
4396 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_union_add(
4397 __isl_take isl_union_pw_multi_aff
*upma1
,
4398 __isl_take isl_union_pw_multi_aff
*upma2
)
4400 return isl_union_pw_multi_aff_union_add_(upma1
, upma2
);
4403 /* Given two piecewise multi-affine expressions A -> B and C -> D,
4404 * construct a piecewise multi-affine expression [A -> C] -> [B -> D].
4406 static __isl_give isl_pw_multi_aff
*pw_multi_aff_product(
4407 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4411 isl_pw_multi_aff
*res
;
4416 n
= pma1
->n
* pma2
->n
;
4417 space
= isl_space_product(isl_space_copy(pma1
->dim
),
4418 isl_space_copy(pma2
->dim
));
4419 res
= isl_pw_multi_aff_alloc_size(space
, n
);
4421 for (i
= 0; i
< pma1
->n
; ++i
) {
4422 for (j
= 0; j
< pma2
->n
; ++j
) {
4426 domain
= isl_set_product(isl_set_copy(pma1
->p
[i
].set
),
4427 isl_set_copy(pma2
->p
[j
].set
));
4428 ma
= isl_multi_aff_product(
4429 isl_multi_aff_copy(pma1
->p
[i
].maff
),
4430 isl_multi_aff_copy(pma2
->p
[j
].maff
));
4431 res
= isl_pw_multi_aff_add_piece(res
, domain
, ma
);
4435 isl_pw_multi_aff_free(pma1
);
4436 isl_pw_multi_aff_free(pma2
);
4439 isl_pw_multi_aff_free(pma1
);
4440 isl_pw_multi_aff_free(pma2
);
4444 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_product(
4445 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4447 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
4448 &pw_multi_aff_product
);
4451 /* Construct a map mapping the domain of the piecewise multi-affine expression
4452 * to its range, with each dimension in the range equated to the
4453 * corresponding affine expression on its cell.
4455 * If the domain of "pma" is rational, then so is the constructed "map".
4457 __isl_give isl_map
*isl_map_from_pw_multi_aff(__isl_take isl_pw_multi_aff
*pma
)
4465 map
= isl_map_empty(isl_pw_multi_aff_get_space(pma
));
4467 for (i
= 0; i
< pma
->n
; ++i
) {
4469 isl_multi_aff
*maff
;
4470 isl_basic_map
*bmap
;
4473 rational
= isl_set_is_rational(pma
->p
[i
].set
);
4475 map
= isl_map_free(map
);
4476 maff
= isl_multi_aff_copy(pma
->p
[i
].maff
);
4477 bmap
= isl_basic_map_from_multi_aff2(maff
, rational
);
4478 map_i
= isl_map_from_basic_map(bmap
);
4479 map_i
= isl_map_intersect_domain(map_i
,
4480 isl_set_copy(pma
->p
[i
].set
));
4481 map
= isl_map_union_disjoint(map
, map_i
);
4484 isl_pw_multi_aff_free(pma
);
4488 __isl_give isl_set
*isl_set_from_pw_multi_aff(__isl_take isl_pw_multi_aff
*pma
)
4493 if (!isl_space_is_set(pma
->dim
))
4494 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
4495 "isl_pw_multi_aff cannot be converted into an isl_set",
4498 return isl_map_from_pw_multi_aff(pma
);
4500 isl_pw_multi_aff_free(pma
);
4504 /* Subtract the initial "n" elements in "ma" with coefficients in "c" and
4505 * denominator "denom".
4506 * "denom" is allowed to be negative, in which case the actual denominator
4507 * is -denom and the expressions are added instead.
4509 static __isl_give isl_aff
*subtract_initial(__isl_take isl_aff
*aff
,
4510 __isl_keep isl_multi_aff
*ma
, int n
, isl_int
*c
, isl_int denom
)
4516 first
= isl_seq_first_non_zero(c
, n
);
4520 sign
= isl_int_sgn(denom
);
4522 isl_int_abs(d
, denom
);
4523 for (i
= first
; i
< n
; ++i
) {
4526 if (isl_int_is_zero(c
[i
]))
4528 aff_i
= isl_multi_aff_get_aff(ma
, i
);
4529 aff_i
= isl_aff_scale(aff_i
, c
[i
]);
4530 aff_i
= isl_aff_scale_down(aff_i
, d
);
4532 aff
= isl_aff_sub(aff
, aff_i
);
4534 aff
= isl_aff_add(aff
, aff_i
);
4541 /* Extract an affine expression that expresses the output dimension "pos"
4542 * of "bmap" in terms of the parameters and input dimensions from
4544 * Note that this expression may involve integer divisions defined
4545 * in terms of parameters and input dimensions.
4546 * The equality may also involve references to earlier (but not later)
4547 * output dimensions. These are replaced by the corresponding elements
4550 * If the equality is of the form
4552 * f(i) + h(j) + a x + g(i) = 0,
4554 * with f(i) a linear combinations of the parameters and input dimensions,
4555 * g(i) a linear combination of integer divisions defined in terms of the same
4556 * and h(j) a linear combinations of earlier output dimensions,
4557 * then the affine expression is
4559 * (-f(i) - g(i))/a - h(j)/a
4561 * If the equality is of the form
4563 * f(i) + h(j) - a x + g(i) = 0,
4565 * then the affine expression is
4567 * (f(i) + g(i))/a - h(j)/(-a)
4570 * If "div" refers to an integer division (i.e., it is smaller than
4571 * the number of integer divisions), then the equality constraint
4572 * does involve an integer division (the one at position "div") that
4573 * is defined in terms of output dimensions. However, this integer
4574 * division can be eliminated by exploiting a pair of constraints
4575 * x >= l and x <= l + n, with n smaller than the coefficient of "div"
4576 * in the equality constraint. "ineq" refers to inequality x >= l, i.e.,
4578 * In particular, let
4580 * x = e(i) + m floor(...)
4582 * with e(i) the expression derived above and floor(...) the integer
4583 * division involving output dimensions.
4594 * e(i) + m floor(...) - l = (e(i) + m floor(...) - l) mod m
4595 * = (e(i) - l) mod m
4599 * x - l = (e(i) - l) mod m
4603 * x = ((e(i) - l) mod m) + l
4605 * The variable "shift" below contains the expression -l, which may
4606 * also involve a linear combination of earlier output dimensions.
4608 static __isl_give isl_aff
*extract_aff_from_equality(
4609 __isl_keep isl_basic_map
*bmap
, int pos
, int eq
, int div
, int ineq
,
4610 __isl_keep isl_multi_aff
*ma
)
4613 unsigned n_div
, n_out
;
4615 isl_local_space
*ls
;
4616 isl_aff
*aff
, *shift
;
4619 ctx
= isl_basic_map_get_ctx(bmap
);
4620 ls
= isl_basic_map_get_local_space(bmap
);
4621 ls
= isl_local_space_domain(ls
);
4622 aff
= isl_aff_alloc(isl_local_space_copy(ls
));
4625 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
4626 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4627 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4628 if (isl_int_is_neg(bmap
->eq
[eq
][o_out
+ pos
])) {
4629 isl_seq_cpy(aff
->v
->el
+ 1, bmap
->eq
[eq
], o_out
);
4630 isl_seq_cpy(aff
->v
->el
+ 1 + o_out
,
4631 bmap
->eq
[eq
] + o_out
+ n_out
, n_div
);
4633 isl_seq_neg(aff
->v
->el
+ 1, bmap
->eq
[eq
], o_out
);
4634 isl_seq_neg(aff
->v
->el
+ 1 + o_out
,
4635 bmap
->eq
[eq
] + o_out
+ n_out
, n_div
);
4638 isl_int_set_si(aff
->v
->el
[1 + o_out
+ div
], 0);
4639 isl_int_abs(aff
->v
->el
[0], bmap
->eq
[eq
][o_out
+ pos
]);
4640 aff
= subtract_initial(aff
, ma
, pos
, bmap
->eq
[eq
] + o_out
,
4641 bmap
->eq
[eq
][o_out
+ pos
]);
4643 shift
= isl_aff_alloc(isl_local_space_copy(ls
));
4646 isl_seq_cpy(shift
->v
->el
+ 1, bmap
->ineq
[ineq
], o_out
);
4647 isl_seq_cpy(shift
->v
->el
+ 1 + o_out
,
4648 bmap
->ineq
[ineq
] + o_out
+ n_out
, n_div
);
4649 isl_int_set_si(shift
->v
->el
[0], 1);
4650 shift
= subtract_initial(shift
, ma
, pos
,
4651 bmap
->ineq
[ineq
] + o_out
, ctx
->negone
);
4652 aff
= isl_aff_add(aff
, isl_aff_copy(shift
));
4653 mod
= isl_val_int_from_isl_int(ctx
,
4654 bmap
->eq
[eq
][o_out
+ n_out
+ div
]);
4655 mod
= isl_val_abs(mod
);
4656 aff
= isl_aff_mod_val(aff
, mod
);
4657 aff
= isl_aff_sub(aff
, shift
);
4660 isl_local_space_free(ls
);
4663 isl_local_space_free(ls
);
4668 /* Given a basic map with output dimensions defined
4669 * in terms of the parameters input dimensions and earlier
4670 * output dimensions using an equality (and possibly a pair on inequalities),
4671 * extract an isl_aff that expresses output dimension "pos" in terms
4672 * of the parameters and input dimensions.
4673 * Note that this expression may involve integer divisions defined
4674 * in terms of parameters and input dimensions.
4675 * "ma" contains the expressions corresponding to earlier output dimensions.
4677 * This function shares some similarities with
4678 * isl_basic_map_has_defining_equality and isl_constraint_get_bound.
4680 static __isl_give isl_aff
*extract_isl_aff_from_basic_map(
4681 __isl_keep isl_basic_map
*bmap
, int pos
, __isl_keep isl_multi_aff
*ma
)
4688 eq
= isl_basic_map_output_defining_equality(bmap
, pos
, &div
, &ineq
);
4689 if (eq
>= bmap
->n_eq
)
4690 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
4691 "unable to find suitable equality", return NULL
);
4692 aff
= extract_aff_from_equality(bmap
, pos
, eq
, div
, ineq
, ma
);
4694 aff
= isl_aff_remove_unused_divs(aff
);
4698 /* Given a basic map where each output dimension is defined
4699 * in terms of the parameters and input dimensions using an equality,
4700 * extract an isl_multi_aff that expresses the output dimensions in terms
4701 * of the parameters and input dimensions.
4703 static __isl_give isl_multi_aff
*extract_isl_multi_aff_from_basic_map(
4704 __isl_take isl_basic_map
*bmap
)
4713 ma
= isl_multi_aff_alloc(isl_basic_map_get_space(bmap
));
4714 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4716 for (i
= 0; i
< n_out
; ++i
) {
4719 aff
= extract_isl_aff_from_basic_map(bmap
, i
, ma
);
4720 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
4723 isl_basic_map_free(bmap
);
4728 /* Given a basic set where each set dimension is defined
4729 * in terms of the parameters using an equality,
4730 * extract an isl_multi_aff that expresses the set dimensions in terms
4731 * of the parameters.
4733 __isl_give isl_multi_aff
*isl_multi_aff_from_basic_set_equalities(
4734 __isl_take isl_basic_set
*bset
)
4736 return extract_isl_multi_aff_from_basic_map(bset
);
4739 /* Create an isl_pw_multi_aff that is equivalent to
4740 * isl_map_intersect_domain(isl_map_from_basic_map(bmap), domain).
4741 * The given basic map is such that each output dimension is defined
4742 * in terms of the parameters and input dimensions using an equality.
4744 * Since some applications expect the result of isl_pw_multi_aff_from_map
4745 * to only contain integer affine expressions, we compute the floor
4746 * of the expression before returning.
4748 * Remove all constraints involving local variables without
4749 * an explicit representation (resulting in the removal of those
4750 * local variables) prior to the actual extraction to ensure
4751 * that the local spaces in which the resulting affine expressions
4752 * are created do not contain any unknown local variables.
4753 * Removing such constraints is safe because constraints involving
4754 * unknown local variables are not used to determine whether
4755 * a basic map is obviously single-valued.
4757 static __isl_give isl_pw_multi_aff
*plain_pw_multi_aff_from_map(
4758 __isl_take isl_set
*domain
, __isl_take isl_basic_map
*bmap
)
4762 bmap
= isl_basic_map_drop_constraint_involving_unknown_divs(bmap
);
4763 ma
= extract_isl_multi_aff_from_basic_map(bmap
);
4764 ma
= isl_multi_aff_floor(ma
);
4765 return isl_pw_multi_aff_alloc(domain
, ma
);
4768 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4769 * This obviously only works if the input "map" is single-valued.
4770 * If so, we compute the lexicographic minimum of the image in the form
4771 * of an isl_pw_multi_aff. Since the image is unique, it is equal
4772 * to its lexicographic minimum.
4773 * If the input is not single-valued, we produce an error.
4775 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_base(
4776 __isl_take isl_map
*map
)
4780 isl_pw_multi_aff
*pma
;
4782 sv
= isl_map_is_single_valued(map
);
4786 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
4787 "map is not single-valued", goto error
);
4788 map
= isl_map_make_disjoint(map
);
4792 pma
= isl_pw_multi_aff_empty(isl_map_get_space(map
));
4794 for (i
= 0; i
< map
->n
; ++i
) {
4795 isl_pw_multi_aff
*pma_i
;
4796 isl_basic_map
*bmap
;
4797 bmap
= isl_basic_map_copy(map
->p
[i
]);
4798 pma_i
= isl_basic_map_lexmin_pw_multi_aff(bmap
);
4799 pma
= isl_pw_multi_aff_add_disjoint(pma
, pma_i
);
4809 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
4810 * taking into account that the output dimension at position "d"
4811 * can be represented as
4813 * x = floor((e(...) + c1) / m)
4815 * given that constraint "i" is of the form
4817 * e(...) + c1 - m x >= 0
4820 * Let "map" be of the form
4824 * We construct a mapping
4826 * A -> [A -> x = floor(...)]
4828 * apply that to the map, obtaining
4830 * [A -> x = floor(...)] -> B
4832 * and equate dimension "d" to x.
4833 * We then compute a isl_pw_multi_aff representation of the resulting map
4834 * and plug in the mapping above.
4836 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_div(
4837 __isl_take isl_map
*map
, __isl_take isl_basic_map
*hull
, int d
, int i
)
4841 isl_local_space
*ls
;
4849 isl_pw_multi_aff
*pma
;
4852 is_set
= isl_map_is_set(map
);
4856 offset
= isl_basic_map_offset(hull
, isl_dim_out
);
4857 ctx
= isl_map_get_ctx(map
);
4858 space
= isl_space_domain(isl_map_get_space(map
));
4859 n_in
= isl_space_dim(space
, isl_dim_set
);
4860 n
= isl_space_dim(space
, isl_dim_all
);
4862 v
= isl_vec_alloc(ctx
, 1 + 1 + n
);
4864 isl_int_neg(v
->el
[0], hull
->ineq
[i
][offset
+ d
]);
4865 isl_seq_cpy(v
->el
+ 1, hull
->ineq
[i
], 1 + n
);
4867 isl_basic_map_free(hull
);
4869 ls
= isl_local_space_from_space(isl_space_copy(space
));
4870 aff
= isl_aff_alloc_vec(ls
, v
);
4871 aff
= isl_aff_floor(aff
);
4873 isl_space_free(space
);
4874 ma
= isl_multi_aff_from_aff(aff
);
4876 ma
= isl_multi_aff_identity(isl_space_map_from_set(space
));
4877 ma
= isl_multi_aff_range_product(ma
,
4878 isl_multi_aff_from_aff(aff
));
4881 insert
= isl_map_from_multi_aff(isl_multi_aff_copy(ma
));
4882 map
= isl_map_apply_domain(map
, insert
);
4883 map
= isl_map_equate(map
, isl_dim_in
, n_in
, isl_dim_out
, d
);
4884 pma
= isl_pw_multi_aff_from_map(map
);
4885 pma
= isl_pw_multi_aff_pullback_multi_aff(pma
, ma
);
4890 isl_basic_map_free(hull
);
4894 /* Is constraint "c" of the form
4896 * e(...) + c1 - m x >= 0
4900 * -e(...) + c2 + m x >= 0
4902 * where m > 1 and e only depends on parameters and input dimemnsions?
4904 * "offset" is the offset of the output dimensions
4905 * "pos" is the position of output dimension x.
4907 static int is_potential_div_constraint(isl_int
*c
, int offset
, int d
, int total
)
4909 if (isl_int_is_zero(c
[offset
+ d
]))
4911 if (isl_int_is_one(c
[offset
+ d
]))
4913 if (isl_int_is_negone(c
[offset
+ d
]))
4915 if (isl_seq_first_non_zero(c
+ offset
, d
) != -1)
4917 if (isl_seq_first_non_zero(c
+ offset
+ d
+ 1,
4918 total
- (offset
+ d
+ 1)) != -1)
4923 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4925 * As a special case, we first check if there is any pair of constraints,
4926 * shared by all the basic maps in "map" that force a given dimension
4927 * to be equal to the floor of some affine combination of the input dimensions.
4929 * In particular, if we can find two constraints
4931 * e(...) + c1 - m x >= 0 i.e., m x <= e(...) + c1
4935 * -e(...) + c2 + m x >= 0 i.e., m x >= e(...) - c2
4937 * where m > 1 and e only depends on parameters and input dimemnsions,
4940 * c1 + c2 < m i.e., -c2 >= c1 - (m - 1)
4942 * then we know that we can take
4944 * x = floor((e(...) + c1) / m)
4946 * without having to perform any computation.
4948 * Note that we know that
4952 * If c1 + c2 were 0, then we would have detected an equality during
4953 * simplification. If c1 + c2 were negative, then we would have detected
4956 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_check_div(
4957 __isl_take isl_map
*map
)
4963 isl_basic_map
*hull
;
4965 hull
= isl_map_unshifted_simple_hull(isl_map_copy(map
));
4970 dim
= isl_map_dim(map
, isl_dim_out
);
4971 offset
= isl_basic_map_offset(hull
, isl_dim_out
);
4972 total
= 1 + isl_basic_map_total_dim(hull
);
4974 for (d
= 0; d
< dim
; ++d
) {
4975 for (i
= 0; i
< n
; ++i
) {
4976 if (!is_potential_div_constraint(hull
->ineq
[i
],
4979 for (j
= i
+ 1; j
< n
; ++j
) {
4980 if (!isl_seq_is_neg(hull
->ineq
[i
] + 1,
4981 hull
->ineq
[j
] + 1, total
- 1))
4983 isl_int_add(sum
, hull
->ineq
[i
][0],
4985 if (isl_int_abs_lt(sum
,
4986 hull
->ineq
[i
][offset
+ d
]))
4993 if (isl_int_is_pos(hull
->ineq
[j
][offset
+ d
]))
4995 return pw_multi_aff_from_map_div(map
, hull
, d
, j
);
4999 isl_basic_map_free(hull
);
5000 return pw_multi_aff_from_map_base(map
);
5003 isl_basic_map_free(hull
);
5007 /* Given an affine expression
5009 * [A -> B] -> f(A,B)
5011 * construct an isl_multi_aff
5015 * such that dimension "d" in B' is set to "aff" and the remaining
5016 * dimensions are set equal to the corresponding dimensions in B.
5017 * "n_in" is the dimension of the space A.
5018 * "n_out" is the dimension of the space B.
5020 * If "is_set" is set, then the affine expression is of the form
5024 * and we construct an isl_multi_aff
5028 static __isl_give isl_multi_aff
*range_map(__isl_take isl_aff
*aff
, int d
,
5029 unsigned n_in
, unsigned n_out
, int is_set
)
5033 isl_space
*space
, *space2
;
5034 isl_local_space
*ls
;
5036 space
= isl_aff_get_domain_space(aff
);
5037 ls
= isl_local_space_from_space(isl_space_copy(space
));
5038 space2
= isl_space_copy(space
);
5040 space2
= isl_space_range(isl_space_unwrap(space2
));
5041 space
= isl_space_map_from_domain_and_range(space
, space2
);
5042 ma
= isl_multi_aff_alloc(space
);
5043 ma
= isl_multi_aff_set_aff(ma
, d
, aff
);
5045 for (i
= 0; i
< n_out
; ++i
) {
5048 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
5049 isl_dim_set
, n_in
+ i
);
5050 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
5053 isl_local_space_free(ls
);
5058 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
5059 * taking into account that the dimension at position "d" can be written as
5061 * x = m a + f(..) (1)
5063 * where m is equal to "gcd".
5064 * "i" is the index of the equality in "hull" that defines f(..).
5065 * In particular, the equality is of the form
5067 * f(..) - x + m g(existentials) = 0
5071 * -f(..) + x + m g(existentials) = 0
5073 * We basically plug (1) into "map", resulting in a map with "a"
5074 * in the range instead of "x". The corresponding isl_pw_multi_aff
5075 * defining "a" is then plugged back into (1) to obtain a definition for "x".
5077 * Specifically, given the input map
5081 * We first wrap it into a set
5085 * and define (1) on top of the corresponding space, resulting in "aff".
5086 * We use this to create an isl_multi_aff that maps the output position "d"
5087 * from "a" to "x", leaving all other (intput and output) dimensions unchanged.
5088 * We plug this into the wrapped map, unwrap the result and compute the
5089 * corresponding isl_pw_multi_aff.
5090 * The result is an expression
5098 * so that we can plug that into "aff", after extending the latter to
5104 * If "map" is actually a set, then there is no "A" space, meaning
5105 * that we do not need to perform any wrapping, and that the result
5106 * of the recursive call is of the form
5110 * which is plugged into a mapping of the form
5114 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_stride(
5115 __isl_take isl_map
*map
, __isl_take isl_basic_map
*hull
, int d
, int i
,
5120 isl_local_space
*ls
;
5123 isl_pw_multi_aff
*pma
, *id
;
5129 is_set
= isl_map_is_set(map
);
5133 n_in
= isl_basic_map_dim(hull
, isl_dim_in
);
5134 n_out
= isl_basic_map_dim(hull
, isl_dim_out
);
5135 o_out
= isl_basic_map_offset(hull
, isl_dim_out
);
5140 set
= isl_map_wrap(map
);
5141 space
= isl_space_map_from_set(isl_set_get_space(set
));
5142 ma
= isl_multi_aff_identity(space
);
5143 ls
= isl_local_space_from_space(isl_set_get_space(set
));
5144 aff
= isl_aff_alloc(ls
);
5146 isl_int_set_si(aff
->v
->el
[0], 1);
5147 if (isl_int_is_one(hull
->eq
[i
][o_out
+ d
]))
5148 isl_seq_neg(aff
->v
->el
+ 1, hull
->eq
[i
],
5151 isl_seq_cpy(aff
->v
->el
+ 1, hull
->eq
[i
],
5153 isl_int_set(aff
->v
->el
[1 + o_out
+ d
], gcd
);
5155 ma
= isl_multi_aff_set_aff(ma
, n_in
+ d
, isl_aff_copy(aff
));
5156 set
= isl_set_preimage_multi_aff(set
, ma
);
5158 ma
= range_map(aff
, d
, n_in
, n_out
, is_set
);
5163 map
= isl_set_unwrap(set
);
5164 pma
= isl_pw_multi_aff_from_map(map
);
5167 space
= isl_pw_multi_aff_get_domain_space(pma
);
5168 space
= isl_space_map_from_set(space
);
5169 id
= isl_pw_multi_aff_identity(space
);
5170 pma
= isl_pw_multi_aff_range_product(id
, pma
);
5172 id
= isl_pw_multi_aff_from_multi_aff(ma
);
5173 pma
= isl_pw_multi_aff_pullback_pw_multi_aff(id
, pma
);
5175 isl_basic_map_free(hull
);
5179 isl_basic_map_free(hull
);
5183 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5184 * "hull" contains the equalities valid for "map".
5186 * Check if any of the output dimensions is "strided".
5187 * That is, we check if it can be written as
5191 * with m greater than 1, a some combination of existentially quantified
5192 * variables and f an expression in the parameters and input dimensions.
5193 * If so, we remove the stride in pw_multi_aff_from_map_stride.
5195 * Otherwise, we continue with pw_multi_aff_from_map_check_div for a further
5198 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_check_strides(
5199 __isl_take isl_map
*map
, __isl_take isl_basic_map
*hull
)
5208 n_div
= isl_basic_map_dim(hull
, isl_dim_div
);
5209 o_div
= isl_basic_map_offset(hull
, isl_dim_div
);
5212 isl_basic_map_free(hull
);
5213 return pw_multi_aff_from_map_check_div(map
);
5218 n_out
= isl_basic_map_dim(hull
, isl_dim_out
);
5219 o_out
= isl_basic_map_offset(hull
, isl_dim_out
);
5221 for (i
= 0; i
< n_out
; ++i
) {
5222 for (j
= 0; j
< hull
->n_eq
; ++j
) {
5223 isl_int
*eq
= hull
->eq
[j
];
5224 isl_pw_multi_aff
*res
;
5226 if (!isl_int_is_one(eq
[o_out
+ i
]) &&
5227 !isl_int_is_negone(eq
[o_out
+ i
]))
5229 if (isl_seq_first_non_zero(eq
+ o_out
, i
) != -1)
5231 if (isl_seq_first_non_zero(eq
+ o_out
+ i
+ 1,
5232 n_out
- (i
+ 1)) != -1)
5234 isl_seq_gcd(eq
+ o_div
, n_div
, &gcd
);
5235 if (isl_int_is_zero(gcd
))
5237 if (isl_int_is_one(gcd
))
5240 res
= pw_multi_aff_from_map_stride(map
, hull
,
5248 isl_basic_map_free(hull
);
5249 return pw_multi_aff_from_map_check_div(map
);
5252 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5254 * As a special case, we first check if all output dimensions are uniquely
5255 * defined in terms of the parameters and input dimensions over the entire
5256 * domain. If so, we extract the desired isl_pw_multi_aff directly
5257 * from the affine hull of "map" and its domain.
5259 * Otherwise, continue with pw_multi_aff_from_map_check_strides for more
5262 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_map(__isl_take isl_map
*map
)
5265 isl_basic_map
*hull
;
5270 if (isl_map_n_basic_map(map
) == 1) {
5271 hull
= isl_map_unshifted_simple_hull(isl_map_copy(map
));
5272 hull
= isl_basic_map_plain_affine_hull(hull
);
5273 sv
= isl_basic_map_plain_is_single_valued(hull
);
5275 return plain_pw_multi_aff_from_map(isl_map_domain(map
),
5277 isl_basic_map_free(hull
);
5279 map
= isl_map_detect_equalities(map
);
5280 hull
= isl_map_unshifted_simple_hull(isl_map_copy(map
));
5281 sv
= isl_basic_map_plain_is_single_valued(hull
);
5283 return plain_pw_multi_aff_from_map(isl_map_domain(map
), hull
);
5285 return pw_multi_aff_from_map_check_strides(map
, hull
);
5286 isl_basic_map_free(hull
);
5291 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_set(__isl_take isl_set
*set
)
5293 return isl_pw_multi_aff_from_map(set
);
5296 /* Convert "map" into an isl_pw_multi_aff (if possible) and
5299 static isl_stat
pw_multi_aff_from_map(__isl_take isl_map
*map
, void *user
)
5301 isl_union_pw_multi_aff
**upma
= user
;
5302 isl_pw_multi_aff
*pma
;
5304 pma
= isl_pw_multi_aff_from_map(map
);
5305 *upma
= isl_union_pw_multi_aff_add_pw_multi_aff(*upma
, pma
);
5307 return *upma
? isl_stat_ok
: isl_stat_error
;
5310 /* Create an isl_union_pw_multi_aff with the given isl_aff on a universe
5313 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_aff(
5314 __isl_take isl_aff
*aff
)
5317 isl_pw_multi_aff
*pma
;
5319 ma
= isl_multi_aff_from_aff(aff
);
5320 pma
= isl_pw_multi_aff_from_multi_aff(ma
);
5321 return isl_union_pw_multi_aff_from_pw_multi_aff(pma
);
5324 /* Try and create an isl_union_pw_multi_aff that is equivalent
5325 * to the given isl_union_map.
5326 * The isl_union_map is required to be single-valued in each space.
5327 * Otherwise, an error is produced.
5329 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_union_map(
5330 __isl_take isl_union_map
*umap
)
5333 isl_union_pw_multi_aff
*upma
;
5335 space
= isl_union_map_get_space(umap
);
5336 upma
= isl_union_pw_multi_aff_empty(space
);
5337 if (isl_union_map_foreach_map(umap
, &pw_multi_aff_from_map
, &upma
) < 0)
5338 upma
= isl_union_pw_multi_aff_free(upma
);
5339 isl_union_map_free(umap
);
5344 /* Try and create an isl_union_pw_multi_aff that is equivalent
5345 * to the given isl_union_set.
5346 * The isl_union_set is required to be a singleton in each space.
5347 * Otherwise, an error is produced.
5349 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_union_set(
5350 __isl_take isl_union_set
*uset
)
5352 return isl_union_pw_multi_aff_from_union_map(uset
);
5355 /* Return the piecewise affine expression "set ? 1 : 0".
5357 __isl_give isl_pw_aff
*isl_set_indicator_function(__isl_take isl_set
*set
)
5360 isl_space
*space
= isl_set_get_space(set
);
5361 isl_local_space
*ls
= isl_local_space_from_space(space
);
5362 isl_aff
*zero
= isl_aff_zero_on_domain(isl_local_space_copy(ls
));
5363 isl_aff
*one
= isl_aff_zero_on_domain(ls
);
5365 one
= isl_aff_add_constant_si(one
, 1);
5366 pa
= isl_pw_aff_alloc(isl_set_copy(set
), one
);
5367 set
= isl_set_complement(set
);
5368 pa
= isl_pw_aff_add_disjoint(pa
, isl_pw_aff_alloc(set
, zero
));
5373 /* Plug in "subs" for dimension "type", "pos" of "aff".
5375 * Let i be the dimension to replace and let "subs" be of the form
5379 * and "aff" of the form
5385 * (a f + d g')/(m d)
5387 * where g' is the result of plugging in "subs" in each of the integer
5390 __isl_give isl_aff
*isl_aff_substitute(__isl_take isl_aff
*aff
,
5391 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
5396 aff
= isl_aff_cow(aff
);
5398 return isl_aff_free(aff
);
5400 ctx
= isl_aff_get_ctx(aff
);
5401 if (!isl_space_is_equal(aff
->ls
->dim
, subs
->ls
->dim
))
5402 isl_die(ctx
, isl_error_invalid
,
5403 "spaces don't match", return isl_aff_free(aff
));
5404 if (isl_local_space_dim(subs
->ls
, isl_dim_div
) != 0)
5405 isl_die(ctx
, isl_error_unsupported
,
5406 "cannot handle divs yet", return isl_aff_free(aff
));
5408 aff
->ls
= isl_local_space_substitute(aff
->ls
, type
, pos
, subs
);
5410 return isl_aff_free(aff
);
5412 aff
->v
= isl_vec_cow(aff
->v
);
5414 return isl_aff_free(aff
);
5416 pos
+= isl_local_space_offset(aff
->ls
, type
);
5419 isl_seq_substitute(aff
->v
->el
, pos
, subs
->v
->el
,
5420 aff
->v
->size
, subs
->v
->size
, v
);
5426 /* Plug in "subs" for dimension "type", "pos" in each of the affine
5427 * expressions in "maff".
5429 __isl_give isl_multi_aff
*isl_multi_aff_substitute(
5430 __isl_take isl_multi_aff
*maff
, enum isl_dim_type type
, unsigned pos
,
5431 __isl_keep isl_aff
*subs
)
5435 maff
= isl_multi_aff_cow(maff
);
5437 return isl_multi_aff_free(maff
);
5439 if (type
== isl_dim_in
)
5442 for (i
= 0; i
< maff
->n
; ++i
) {
5443 maff
->u
.p
[i
] = isl_aff_substitute(maff
->u
.p
[i
],
5446 return isl_multi_aff_free(maff
);
5452 /* Plug in "subs" for dimension "type", "pos" of "pma".
5454 * pma is of the form
5458 * while subs is of the form
5460 * v' = B_j(v) -> S_j
5462 * Each pair i,j such that C_ij = A_i \cap B_i is non-empty
5463 * has a contribution in the result, in particular
5465 * C_ij(S_j) -> M_i(S_j)
5467 * Note that plugging in S_j in C_ij may also result in an empty set
5468 * and this contribution should simply be discarded.
5470 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_substitute(
5471 __isl_take isl_pw_multi_aff
*pma
, enum isl_dim_type type
, unsigned pos
,
5472 __isl_keep isl_pw_aff
*subs
)
5475 isl_pw_multi_aff
*res
;
5478 return isl_pw_multi_aff_free(pma
);
5480 n
= pma
->n
* subs
->n
;
5481 res
= isl_pw_multi_aff_alloc_size(isl_space_copy(pma
->dim
), n
);
5483 for (i
= 0; i
< pma
->n
; ++i
) {
5484 for (j
= 0; j
< subs
->n
; ++j
) {
5486 isl_multi_aff
*res_ij
;
5489 common
= isl_set_intersect(
5490 isl_set_copy(pma
->p
[i
].set
),
5491 isl_set_copy(subs
->p
[j
].set
));
5492 common
= isl_set_substitute(common
,
5493 type
, pos
, subs
->p
[j
].aff
);
5494 empty
= isl_set_plain_is_empty(common
);
5495 if (empty
< 0 || empty
) {
5496 isl_set_free(common
);
5502 res_ij
= isl_multi_aff_substitute(
5503 isl_multi_aff_copy(pma
->p
[i
].maff
),
5504 type
, pos
, subs
->p
[j
].aff
);
5506 res
= isl_pw_multi_aff_add_piece(res
, common
, res_ij
);
5510 isl_pw_multi_aff_free(pma
);
5513 isl_pw_multi_aff_free(pma
);
5514 isl_pw_multi_aff_free(res
);
5518 /* Compute the preimage of a range of dimensions in the affine expression "src"
5519 * under "ma" and put the result in "dst". The number of dimensions in "src"
5520 * that precede the range is given by "n_before". The number of dimensions
5521 * in the range is given by the number of output dimensions of "ma".
5522 * The number of dimensions that follow the range is given by "n_after".
5523 * If "has_denom" is set (to one),
5524 * then "src" and "dst" have an extra initial denominator.
5525 * "n_div_ma" is the number of existentials in "ma"
5526 * "n_div_bset" is the number of existentials in "src"
5527 * The resulting "dst" (which is assumed to have been allocated by
5528 * the caller) contains coefficients for both sets of existentials,
5529 * first those in "ma" and then those in "src".
5530 * f, c1, c2 and g are temporary objects that have been initialized
5533 * Let src represent the expression
5535 * (a(p) + f_u u + b v + f_w w + c(divs))/d
5537 * and let ma represent the expressions
5539 * v_i = (r_i(p) + s_i(y) + t_i(divs'))/m_i
5541 * We start out with the following expression for dst:
5543 * (a(p) + f_u u + 0 y + f_w w + 0 divs' + c(divs) + f \sum_i b_i v_i)/d
5545 * with the multiplication factor f initially equal to 1
5546 * and f \sum_i b_i v_i kept separately.
5547 * For each x_i that we substitute, we multiply the numerator
5548 * (and denominator) of dst by c_1 = m_i and add the numerator
5549 * of the x_i expression multiplied by c_2 = f b_i,
5550 * after removing the common factors of c_1 and c_2.
5551 * The multiplication factor f also needs to be multiplied by c_1
5552 * for the next x_j, j > i.
5554 void isl_seq_preimage(isl_int
*dst
, isl_int
*src
,
5555 __isl_keep isl_multi_aff
*ma
, int n_before
, int n_after
,
5556 int n_div_ma
, int n_div_bmap
,
5557 isl_int f
, isl_int c1
, isl_int c2
, isl_int g
, int has_denom
)
5560 int n_param
, n_in
, n_out
;
5563 n_param
= isl_multi_aff_dim(ma
, isl_dim_param
);
5564 n_in
= isl_multi_aff_dim(ma
, isl_dim_in
);
5565 n_out
= isl_multi_aff_dim(ma
, isl_dim_out
);
5567 isl_seq_cpy(dst
, src
, has_denom
+ 1 + n_param
+ n_before
);
5568 o_dst
= o_src
= has_denom
+ 1 + n_param
+ n_before
;
5569 isl_seq_clr(dst
+ o_dst
, n_in
);
5572 isl_seq_cpy(dst
+ o_dst
, src
+ o_src
, n_after
);
5575 isl_seq_clr(dst
+ o_dst
, n_div_ma
);
5577 isl_seq_cpy(dst
+ o_dst
, src
+ o_src
, n_div_bmap
);
5579 isl_int_set_si(f
, 1);
5581 for (i
= 0; i
< n_out
; ++i
) {
5582 int offset
= has_denom
+ 1 + n_param
+ n_before
+ i
;
5584 if (isl_int_is_zero(src
[offset
]))
5586 isl_int_set(c1
, ma
->u
.p
[i
]->v
->el
[0]);
5587 isl_int_mul(c2
, f
, src
[offset
]);
5588 isl_int_gcd(g
, c1
, c2
);
5589 isl_int_divexact(c1
, c1
, g
);
5590 isl_int_divexact(c2
, c2
, g
);
5592 isl_int_mul(f
, f
, c1
);
5595 isl_seq_combine(dst
+ o_dst
, c1
, dst
+ o_dst
,
5596 c2
, ma
->u
.p
[i
]->v
->el
+ o_src
, 1 + n_param
);
5597 o_dst
+= 1 + n_param
;
5598 o_src
+= 1 + n_param
;
5599 isl_seq_scale(dst
+ o_dst
, dst
+ o_dst
, c1
, n_before
);
5601 isl_seq_combine(dst
+ o_dst
, c1
, dst
+ o_dst
,
5602 c2
, ma
->u
.p
[i
]->v
->el
+ o_src
, n_in
);
5605 isl_seq_scale(dst
+ o_dst
, dst
+ o_dst
, c1
, n_after
);
5607 isl_seq_combine(dst
+ o_dst
, c1
, dst
+ o_dst
,
5608 c2
, ma
->u
.p
[i
]->v
->el
+ o_src
, n_div_ma
);
5611 isl_seq_scale(dst
+ o_dst
, dst
+ o_dst
, c1
, n_div_bmap
);
5613 isl_int_mul(dst
[0], dst
[0], c1
);
5617 /* Compute the pullback of "aff" by the function represented by "ma".
5618 * In other words, plug in "ma" in "aff". The result is an affine expression
5619 * defined over the domain space of "ma".
5621 * If "aff" is represented by
5623 * (a(p) + b x + c(divs))/d
5625 * and ma is represented by
5627 * x = D(p) + F(y) + G(divs')
5629 * then the result is
5631 * (a(p) + b D(p) + b F(y) + b G(divs') + c(divs))/d
5633 * The divs in the local space of the input are similarly adjusted
5634 * through a call to isl_local_space_preimage_multi_aff.
5636 __isl_give isl_aff
*isl_aff_pullback_multi_aff(__isl_take isl_aff
*aff
,
5637 __isl_take isl_multi_aff
*ma
)
5639 isl_aff
*res
= NULL
;
5640 isl_local_space
*ls
;
5641 int n_div_aff
, n_div_ma
;
5642 isl_int f
, c1
, c2
, g
;
5644 ma
= isl_multi_aff_align_divs(ma
);
5648 n_div_aff
= isl_aff_dim(aff
, isl_dim_div
);
5649 n_div_ma
= ma
->n
? isl_aff_dim(ma
->u
.p
[0], isl_dim_div
) : 0;
5651 ls
= isl_aff_get_domain_local_space(aff
);
5652 ls
= isl_local_space_preimage_multi_aff(ls
, isl_multi_aff_copy(ma
));
5653 res
= isl_aff_alloc(ls
);
5662 isl_seq_preimage(res
->v
->el
, aff
->v
->el
, ma
, 0, 0, n_div_ma
, n_div_aff
,
5671 isl_multi_aff_free(ma
);
5672 res
= isl_aff_normalize(res
);
5676 isl_multi_aff_free(ma
);
5681 /* Compute the pullback of "aff1" by the function represented by "aff2".
5682 * In other words, plug in "aff2" in "aff1". The result is an affine expression
5683 * defined over the domain space of "aff1".
5685 * The domain of "aff1" should match the range of "aff2", which means
5686 * that it should be single-dimensional.
5688 __isl_give isl_aff
*isl_aff_pullback_aff(__isl_take isl_aff
*aff1
,
5689 __isl_take isl_aff
*aff2
)
5693 ma
= isl_multi_aff_from_aff(aff2
);
5694 return isl_aff_pullback_multi_aff(aff1
, ma
);
5697 /* Compute the pullback of "ma1" by the function represented by "ma2".
5698 * In other words, plug in "ma2" in "ma1".
5700 * The parameters of "ma1" and "ma2" are assumed to have been aligned.
5702 static __isl_give isl_multi_aff
*isl_multi_aff_pullback_multi_aff_aligned(
5703 __isl_take isl_multi_aff
*ma1
, __isl_take isl_multi_aff
*ma2
)
5706 isl_space
*space
= NULL
;
5708 ma2
= isl_multi_aff_align_divs(ma2
);
5709 ma1
= isl_multi_aff_cow(ma1
);
5713 space
= isl_space_join(isl_multi_aff_get_space(ma2
),
5714 isl_multi_aff_get_space(ma1
));
5716 for (i
= 0; i
< ma1
->n
; ++i
) {
5717 ma1
->u
.p
[i
] = isl_aff_pullback_multi_aff(ma1
->u
.p
[i
],
5718 isl_multi_aff_copy(ma2
));
5723 ma1
= isl_multi_aff_reset_space(ma1
, space
);
5724 isl_multi_aff_free(ma2
);
5727 isl_space_free(space
);
5728 isl_multi_aff_free(ma2
);
5729 isl_multi_aff_free(ma1
);
5733 /* Compute the pullback of "ma1" by the function represented by "ma2".
5734 * In other words, plug in "ma2" in "ma1".
5736 __isl_give isl_multi_aff
*isl_multi_aff_pullback_multi_aff(
5737 __isl_take isl_multi_aff
*ma1
, __isl_take isl_multi_aff
*ma2
)
5739 return isl_multi_aff_align_params_multi_multi_and(ma1
, ma2
,
5740 &isl_multi_aff_pullback_multi_aff_aligned
);
5743 /* Extend the local space of "dst" to include the divs
5744 * in the local space of "src".
5746 * If "src" does not have any divs or if the local spaces of "dst" and
5747 * "src" are the same, then no extension is required.
5749 __isl_give isl_aff
*isl_aff_align_divs(__isl_take isl_aff
*dst
,
5750 __isl_keep isl_aff
*src
)
5753 int src_n_div
, dst_n_div
;
5760 return isl_aff_free(dst
);
5762 ctx
= isl_aff_get_ctx(src
);
5763 equal
= isl_local_space_has_equal_space(src
->ls
, dst
->ls
);
5765 return isl_aff_free(dst
);
5767 isl_die(ctx
, isl_error_invalid
,
5768 "spaces don't match", goto error
);
5770 src_n_div
= isl_local_space_dim(src
->ls
, isl_dim_div
);
5773 equal
= isl_local_space_is_equal(src
->ls
, dst
->ls
);
5775 return isl_aff_free(dst
);
5779 dst_n_div
= isl_local_space_dim(dst
->ls
, isl_dim_div
);
5780 exp1
= isl_alloc_array(ctx
, int, src_n_div
);
5781 exp2
= isl_alloc_array(ctx
, int, dst_n_div
);
5782 if (!exp1
|| (dst_n_div
&& !exp2
))
5785 div
= isl_merge_divs(src
->ls
->div
, dst
->ls
->div
, exp1
, exp2
);
5786 dst
= isl_aff_expand_divs(dst
, div
, exp2
);
5794 return isl_aff_free(dst
);
5797 /* Adjust the local spaces of the affine expressions in "maff"
5798 * such that they all have the save divs.
5800 __isl_give isl_multi_aff
*isl_multi_aff_align_divs(
5801 __isl_take isl_multi_aff
*maff
)
5809 maff
= isl_multi_aff_cow(maff
);
5813 for (i
= 1; i
< maff
->n
; ++i
)
5814 maff
->u
.p
[0] = isl_aff_align_divs(maff
->u
.p
[0], maff
->u
.p
[i
]);
5815 for (i
= 1; i
< maff
->n
; ++i
) {
5816 maff
->u
.p
[i
] = isl_aff_align_divs(maff
->u
.p
[i
], maff
->u
.p
[0]);
5818 return isl_multi_aff_free(maff
);
5824 __isl_give isl_aff
*isl_aff_lift(__isl_take isl_aff
*aff
)
5826 aff
= isl_aff_cow(aff
);
5830 aff
->ls
= isl_local_space_lift(aff
->ls
);
5832 return isl_aff_free(aff
);
5837 /* Lift "maff" to a space with extra dimensions such that the result
5838 * has no more existentially quantified variables.
5839 * If "ls" is not NULL, then *ls is assigned the local space that lies
5840 * at the basis of the lifting applied to "maff".
5842 __isl_give isl_multi_aff
*isl_multi_aff_lift(__isl_take isl_multi_aff
*maff
,
5843 __isl_give isl_local_space
**ls
)
5857 isl_space
*space
= isl_multi_aff_get_domain_space(maff
);
5858 *ls
= isl_local_space_from_space(space
);
5860 return isl_multi_aff_free(maff
);
5865 maff
= isl_multi_aff_cow(maff
);
5866 maff
= isl_multi_aff_align_divs(maff
);
5870 n_div
= isl_aff_dim(maff
->u
.p
[0], isl_dim_div
);
5871 space
= isl_multi_aff_get_space(maff
);
5872 space
= isl_space_lift(isl_space_domain(space
), n_div
);
5873 space
= isl_space_extend_domain_with_range(space
,
5874 isl_multi_aff_get_space(maff
));
5876 return isl_multi_aff_free(maff
);
5877 isl_space_free(maff
->space
);
5878 maff
->space
= space
;
5881 *ls
= isl_aff_get_domain_local_space(maff
->u
.p
[0]);
5883 return isl_multi_aff_free(maff
);
5886 for (i
= 0; i
< maff
->n
; ++i
) {
5887 maff
->u
.p
[i
] = isl_aff_lift(maff
->u
.p
[i
]);
5895 isl_local_space_free(*ls
);
5896 return isl_multi_aff_free(maff
);
5900 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma".
5902 __isl_give isl_pw_aff
*isl_pw_multi_aff_get_pw_aff(
5903 __isl_keep isl_pw_multi_aff
*pma
, int pos
)
5913 n_out
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
5914 if (pos
< 0 || pos
>= n_out
)
5915 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
5916 "index out of bounds", return NULL
);
5918 space
= isl_pw_multi_aff_get_space(pma
);
5919 space
= isl_space_drop_dims(space
, isl_dim_out
,
5920 pos
+ 1, n_out
- pos
- 1);
5921 space
= isl_space_drop_dims(space
, isl_dim_out
, 0, pos
);
5923 pa
= isl_pw_aff_alloc_size(space
, pma
->n
);
5924 for (i
= 0; i
< pma
->n
; ++i
) {
5926 aff
= isl_multi_aff_get_aff(pma
->p
[i
].maff
, pos
);
5927 pa
= isl_pw_aff_add_piece(pa
, isl_set_copy(pma
->p
[i
].set
), aff
);
5933 /* Return an isl_pw_multi_aff with the given "set" as domain and
5934 * an unnamed zero-dimensional range.
5936 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_domain(
5937 __isl_take isl_set
*set
)
5942 space
= isl_set_get_space(set
);
5943 space
= isl_space_from_domain(space
);
5944 ma
= isl_multi_aff_zero(space
);
5945 return isl_pw_multi_aff_alloc(set
, ma
);
5948 /* Add an isl_pw_multi_aff with the given "set" as domain and
5949 * an unnamed zero-dimensional range to *user.
5951 static isl_stat
add_pw_multi_aff_from_domain(__isl_take isl_set
*set
,
5954 isl_union_pw_multi_aff
**upma
= user
;
5955 isl_pw_multi_aff
*pma
;
5957 pma
= isl_pw_multi_aff_from_domain(set
);
5958 *upma
= isl_union_pw_multi_aff_add_pw_multi_aff(*upma
, pma
);
5963 /* Return an isl_union_pw_multi_aff with the given "uset" as domain and
5964 * an unnamed zero-dimensional range.
5966 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_domain(
5967 __isl_take isl_union_set
*uset
)
5970 isl_union_pw_multi_aff
*upma
;
5975 space
= isl_union_set_get_space(uset
);
5976 upma
= isl_union_pw_multi_aff_empty(space
);
5978 if (isl_union_set_foreach_set(uset
,
5979 &add_pw_multi_aff_from_domain
, &upma
) < 0)
5982 isl_union_set_free(uset
);
5985 isl_union_set_free(uset
);
5986 isl_union_pw_multi_aff_free(upma
);
5990 /* Convert "pma" to an isl_map and add it to *umap.
5992 static isl_stat
map_from_pw_multi_aff(__isl_take isl_pw_multi_aff
*pma
,
5995 isl_union_map
**umap
= user
;
5998 map
= isl_map_from_pw_multi_aff(pma
);
5999 *umap
= isl_union_map_add_map(*umap
, map
);
6004 /* Construct a union map mapping the domain of the union
6005 * piecewise multi-affine expression to its range, with each dimension
6006 * in the range equated to the corresponding affine expression on its cell.
6008 __isl_give isl_union_map
*isl_union_map_from_union_pw_multi_aff(
6009 __isl_take isl_union_pw_multi_aff
*upma
)
6012 isl_union_map
*umap
;
6017 space
= isl_union_pw_multi_aff_get_space(upma
);
6018 umap
= isl_union_map_empty(space
);
6020 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma
,
6021 &map_from_pw_multi_aff
, &umap
) < 0)
6024 isl_union_pw_multi_aff_free(upma
);
6027 isl_union_pw_multi_aff_free(upma
);
6028 isl_union_map_free(umap
);
6032 /* Local data for bin_entry and the callback "fn".
6034 struct isl_union_pw_multi_aff_bin_data
{
6035 isl_union_pw_multi_aff
*upma2
;
6036 isl_union_pw_multi_aff
*res
;
6037 isl_pw_multi_aff
*pma
;
6038 isl_stat (*fn
)(__isl_take isl_pw_multi_aff
*pma
, void *user
);
6041 /* Given an isl_pw_multi_aff from upma1, store it in data->pma
6042 * and call data->fn for each isl_pw_multi_aff in data->upma2.
6044 static isl_stat
bin_entry(__isl_take isl_pw_multi_aff
*pma
, void *user
)
6046 struct isl_union_pw_multi_aff_bin_data
*data
= user
;
6050 r
= isl_union_pw_multi_aff_foreach_pw_multi_aff(data
->upma2
,
6052 isl_pw_multi_aff_free(pma
);
6057 /* Call "fn" on each pair of isl_pw_multi_affs in "upma1" and "upma2".
6058 * The isl_pw_multi_aff from upma1 is stored in data->pma (where data is
6059 * passed as user field) and the isl_pw_multi_aff from upma2 is available
6060 * as *entry. The callback should adjust data->res if desired.
6062 static __isl_give isl_union_pw_multi_aff
*bin_op(
6063 __isl_take isl_union_pw_multi_aff
*upma1
,
6064 __isl_take isl_union_pw_multi_aff
*upma2
,
6065 isl_stat (*fn
)(__isl_take isl_pw_multi_aff
*pma
, void *user
))
6068 struct isl_union_pw_multi_aff_bin_data data
= { NULL
, NULL
, NULL
, fn
};
6070 space
= isl_union_pw_multi_aff_get_space(upma2
);
6071 upma1
= isl_union_pw_multi_aff_align_params(upma1
, space
);
6072 space
= isl_union_pw_multi_aff_get_space(upma1
);
6073 upma2
= isl_union_pw_multi_aff_align_params(upma2
, space
);
6075 if (!upma1
|| !upma2
)
6079 data
.res
= isl_union_pw_multi_aff_alloc_same_size(upma1
);
6080 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma1
,
6081 &bin_entry
, &data
) < 0)
6084 isl_union_pw_multi_aff_free(upma1
);
6085 isl_union_pw_multi_aff_free(upma2
);
6088 isl_union_pw_multi_aff_free(upma1
);
6089 isl_union_pw_multi_aff_free(upma2
);
6090 isl_union_pw_multi_aff_free(data
.res
);
6094 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
6095 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
6097 static __isl_give isl_pw_multi_aff
*pw_multi_aff_range_product(
6098 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
6102 space
= isl_space_range_product(isl_pw_multi_aff_get_space(pma1
),
6103 isl_pw_multi_aff_get_space(pma2
));
6104 return isl_pw_multi_aff_on_shared_domain_in(pma1
, pma2
, space
,
6105 &isl_multi_aff_range_product
);
6108 /* Given two isl_pw_multi_affs A -> B and C -> D,
6109 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
6111 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_range_product(
6112 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
6114 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
6115 &pw_multi_aff_range_product
);
6118 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
6119 * construct an isl_pw_multi_aff (A * C) -> (B, D).
6121 static __isl_give isl_pw_multi_aff
*pw_multi_aff_flat_range_product(
6122 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
6126 space
= isl_space_range_product(isl_pw_multi_aff_get_space(pma1
),
6127 isl_pw_multi_aff_get_space(pma2
));
6128 space
= isl_space_flatten_range(space
);
6129 return isl_pw_multi_aff_on_shared_domain_in(pma1
, pma2
, space
,
6130 &isl_multi_aff_flat_range_product
);
6133 /* Given two isl_pw_multi_affs A -> B and C -> D,
6134 * construct an isl_pw_multi_aff (A * C) -> (B, D).
6136 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_flat_range_product(
6137 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
6139 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
6140 &pw_multi_aff_flat_range_product
);
6143 /* If data->pma and "pma2" have the same domain space, then compute
6144 * their flat range product and the result to data->res.
6146 static isl_stat
flat_range_product_entry(__isl_take isl_pw_multi_aff
*pma2
,
6149 struct isl_union_pw_multi_aff_bin_data
*data
= user
;
6151 if (!isl_space_tuple_is_equal(data
->pma
->dim
, isl_dim_in
,
6152 pma2
->dim
, isl_dim_in
)) {
6153 isl_pw_multi_aff_free(pma2
);
6157 pma2
= isl_pw_multi_aff_flat_range_product(
6158 isl_pw_multi_aff_copy(data
->pma
), pma2
);
6160 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma2
);
6165 /* Given two isl_union_pw_multi_affs A -> B and C -> D,
6166 * construct an isl_union_pw_multi_aff (A * C) -> (B, D).
6168 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_flat_range_product(
6169 __isl_take isl_union_pw_multi_aff
*upma1
,
6170 __isl_take isl_union_pw_multi_aff
*upma2
)
6172 return bin_op(upma1
, upma2
, &flat_range_product_entry
);
6175 /* Replace the affine expressions at position "pos" in "pma" by "pa".
6176 * The parameters are assumed to have been aligned.
6178 * The implementation essentially performs an isl_pw_*_on_shared_domain,
6179 * except that it works on two different isl_pw_* types.
6181 static __isl_give isl_pw_multi_aff
*pw_multi_aff_set_pw_aff(
6182 __isl_take isl_pw_multi_aff
*pma
, unsigned pos
,
6183 __isl_take isl_pw_aff
*pa
)
6186 isl_pw_multi_aff
*res
= NULL
;
6191 if (!isl_space_tuple_is_equal(pma
->dim
, isl_dim_in
,
6192 pa
->dim
, isl_dim_in
))
6193 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
6194 "domains don't match", goto error
);
6195 if (pos
>= isl_pw_multi_aff_dim(pma
, isl_dim_out
))
6196 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
6197 "index out of bounds", goto error
);
6200 res
= isl_pw_multi_aff_alloc_size(isl_pw_multi_aff_get_space(pma
), n
);
6202 for (i
= 0; i
< pma
->n
; ++i
) {
6203 for (j
= 0; j
< pa
->n
; ++j
) {
6205 isl_multi_aff
*res_ij
;
6208 common
= isl_set_intersect(isl_set_copy(pma
->p
[i
].set
),
6209 isl_set_copy(pa
->p
[j
].set
));
6210 empty
= isl_set_plain_is_empty(common
);
6211 if (empty
< 0 || empty
) {
6212 isl_set_free(common
);
6218 res_ij
= isl_multi_aff_set_aff(
6219 isl_multi_aff_copy(pma
->p
[i
].maff
), pos
,
6220 isl_aff_copy(pa
->p
[j
].aff
));
6221 res_ij
= isl_multi_aff_gist(res_ij
,
6222 isl_set_copy(common
));
6224 res
= isl_pw_multi_aff_add_piece(res
, common
, res_ij
);
6228 isl_pw_multi_aff_free(pma
);
6229 isl_pw_aff_free(pa
);
6232 isl_pw_multi_aff_free(pma
);
6233 isl_pw_aff_free(pa
);
6234 return isl_pw_multi_aff_free(res
);
6237 /* Replace the affine expressions at position "pos" in "pma" by "pa".
6239 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_set_pw_aff(
6240 __isl_take isl_pw_multi_aff
*pma
, unsigned pos
,
6241 __isl_take isl_pw_aff
*pa
)
6243 isl_bool equal_params
;
6247 equal_params
= isl_space_has_equal_params(pma
->dim
, pa
->dim
);
6248 if (equal_params
< 0)
6251 return pw_multi_aff_set_pw_aff(pma
, pos
, pa
);
6252 if (isl_pw_multi_aff_check_named_params(pma
) < 0 ||
6253 isl_pw_aff_check_named_params(pa
) < 0)
6255 pma
= isl_pw_multi_aff_align_params(pma
, isl_pw_aff_get_space(pa
));
6256 pa
= isl_pw_aff_align_params(pa
, isl_pw_multi_aff_get_space(pma
));
6257 return pw_multi_aff_set_pw_aff(pma
, pos
, pa
);
6259 isl_pw_multi_aff_free(pma
);
6260 isl_pw_aff_free(pa
);
6264 /* Do the parameters of "pa" match those of "space"?
6266 isl_bool
isl_pw_aff_matching_params(__isl_keep isl_pw_aff
*pa
,
6267 __isl_keep isl_space
*space
)
6269 isl_space
*pa_space
;
6273 return isl_bool_error
;
6275 pa_space
= isl_pw_aff_get_space(pa
);
6277 match
= isl_space_has_equal_params(space
, pa_space
);
6279 isl_space_free(pa_space
);
6283 /* Check that the domain space of "pa" matches "space".
6285 isl_stat
isl_pw_aff_check_match_domain_space(__isl_keep isl_pw_aff
*pa
,
6286 __isl_keep isl_space
*space
)
6288 isl_space
*pa_space
;
6292 return isl_stat_error
;
6294 pa_space
= isl_pw_aff_get_space(pa
);
6296 match
= isl_space_has_equal_params(space
, pa_space
);
6300 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
6301 "parameters don't match", goto error
);
6302 match
= isl_space_tuple_is_equal(space
, isl_dim_in
,
6303 pa_space
, isl_dim_in
);
6307 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
6308 "domains don't match", goto error
);
6309 isl_space_free(pa_space
);
6312 isl_space_free(pa_space
);
6313 return isl_stat_error
;
6321 #include <isl_multi_explicit_domain.c>
6322 #include <isl_multi_pw_aff_explicit_domain.c>
6323 #include <isl_multi_templ.c>
6324 #include <isl_multi_apply_set.c>
6325 #include <isl_multi_coalesce.c>
6326 #include <isl_multi_dims.c>
6327 #include <isl_multi_gist.c>
6328 #include <isl_multi_hash.c>
6329 #include <isl_multi_align_set.c>
6330 #include <isl_multi_intersect.c>
6332 /* Does "mpa" have a non-trivial explicit domain?
6334 * The explicit domain, if present, is trivial if it represents
6335 * an (obviously) universe set.
6337 isl_bool
isl_multi_pw_aff_has_non_trivial_domain(
6338 __isl_keep isl_multi_pw_aff
*mpa
)
6341 return isl_bool_error
;
6342 if (!isl_multi_pw_aff_has_explicit_domain(mpa
))
6343 return isl_bool_false
;
6344 return isl_bool_not(isl_set_plain_is_universe(mpa
->u
.dom
));
6347 /* Scale the elements of "pma" by the corresponding elements of "mv".
6349 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_scale_multi_val(
6350 __isl_take isl_pw_multi_aff
*pma
, __isl_take isl_multi_val
*mv
)
6353 isl_bool equal_params
;
6355 pma
= isl_pw_multi_aff_cow(pma
);
6358 if (!isl_space_tuple_is_equal(pma
->dim
, isl_dim_out
,
6359 mv
->space
, isl_dim_set
))
6360 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
6361 "spaces don't match", goto error
);
6362 equal_params
= isl_space_has_equal_params(pma
->dim
, mv
->space
);
6363 if (equal_params
< 0)
6365 if (!equal_params
) {
6366 pma
= isl_pw_multi_aff_align_params(pma
,
6367 isl_multi_val_get_space(mv
));
6368 mv
= isl_multi_val_align_params(mv
,
6369 isl_pw_multi_aff_get_space(pma
));
6374 for (i
= 0; i
< pma
->n
; ++i
) {
6375 pma
->p
[i
].maff
= isl_multi_aff_scale_multi_val(pma
->p
[i
].maff
,
6376 isl_multi_val_copy(mv
));
6377 if (!pma
->p
[i
].maff
)
6381 isl_multi_val_free(mv
);
6384 isl_multi_val_free(mv
);
6385 isl_pw_multi_aff_free(pma
);
6389 /* This function is called for each entry of an isl_union_pw_multi_aff.
6390 * If the space of the entry matches that of data->mv,
6391 * then apply isl_pw_multi_aff_scale_multi_val and return the result.
6392 * Otherwise, return an empty isl_pw_multi_aff.
6394 static __isl_give isl_pw_multi_aff
*union_pw_multi_aff_scale_multi_val_entry(
6395 __isl_take isl_pw_multi_aff
*pma
, void *user
)
6397 isl_multi_val
*mv
= user
;
6401 if (!isl_space_tuple_is_equal(pma
->dim
, isl_dim_out
,
6402 mv
->space
, isl_dim_set
)) {
6403 isl_space
*space
= isl_pw_multi_aff_get_space(pma
);
6404 isl_pw_multi_aff_free(pma
);
6405 return isl_pw_multi_aff_empty(space
);
6408 return isl_pw_multi_aff_scale_multi_val(pma
, isl_multi_val_copy(mv
));
6411 /* Scale the elements of "upma" by the corresponding elements of "mv",
6412 * for those entries that match the space of "mv".
6414 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_scale_multi_val(
6415 __isl_take isl_union_pw_multi_aff
*upma
, __isl_take isl_multi_val
*mv
)
6417 upma
= isl_union_pw_multi_aff_align_params(upma
,
6418 isl_multi_val_get_space(mv
));
6419 mv
= isl_multi_val_align_params(mv
,
6420 isl_union_pw_multi_aff_get_space(upma
));
6424 return isl_union_pw_multi_aff_transform(upma
,
6425 &union_pw_multi_aff_scale_multi_val_entry
, mv
);
6427 isl_multi_val_free(mv
);
6430 isl_multi_val_free(mv
);
6431 isl_union_pw_multi_aff_free(upma
);
6435 /* Construct and return a piecewise multi affine expression
6436 * in the given space with value zero in each of the output dimensions and
6437 * a universe domain.
6439 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_zero(__isl_take isl_space
*space
)
6441 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_zero(space
));
6444 /* Construct and return a piecewise multi affine expression
6445 * that is equal to the given piecewise affine expression.
6447 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_pw_aff(
6448 __isl_take isl_pw_aff
*pa
)
6452 isl_pw_multi_aff
*pma
;
6457 space
= isl_pw_aff_get_space(pa
);
6458 pma
= isl_pw_multi_aff_alloc_size(space
, pa
->n
);
6460 for (i
= 0; i
< pa
->n
; ++i
) {
6464 set
= isl_set_copy(pa
->p
[i
].set
);
6465 ma
= isl_multi_aff_from_aff(isl_aff_copy(pa
->p
[i
].aff
));
6466 pma
= isl_pw_multi_aff_add_piece(pma
, set
, ma
);
6469 isl_pw_aff_free(pa
);
6473 /* Construct a set or map mapping the shared (parameter) domain
6474 * of the piecewise affine expressions to the range of "mpa"
6475 * with each dimension in the range equated to the
6476 * corresponding piecewise affine expression.
6478 static __isl_give isl_map
*map_from_multi_pw_aff(
6479 __isl_take isl_multi_pw_aff
*mpa
)
6488 if (isl_space_dim(mpa
->space
, isl_dim_out
) != mpa
->n
)
6489 isl_die(isl_multi_pw_aff_get_ctx(mpa
), isl_error_internal
,
6490 "invalid space", goto error
);
6492 space
= isl_multi_pw_aff_get_domain_space(mpa
);
6493 map
= isl_map_universe(isl_space_from_domain(space
));
6495 for (i
= 0; i
< mpa
->n
; ++i
) {
6499 pa
= isl_pw_aff_copy(mpa
->u
.p
[i
]);
6500 map_i
= map_from_pw_aff(pa
);
6502 map
= isl_map_flat_range_product(map
, map_i
);
6505 map
= isl_map_reset_space(map
, isl_multi_pw_aff_get_space(mpa
));
6507 isl_multi_pw_aff_free(mpa
);
6510 isl_multi_pw_aff_free(mpa
);
6514 /* Construct a map mapping the shared domain
6515 * of the piecewise affine expressions to the range of "mpa"
6516 * with each dimension in the range equated to the
6517 * corresponding piecewise affine expression.
6519 __isl_give isl_map
*isl_map_from_multi_pw_aff(__isl_take isl_multi_pw_aff
*mpa
)
6523 if (isl_space_is_set(mpa
->space
))
6524 isl_die(isl_multi_pw_aff_get_ctx(mpa
), isl_error_internal
,
6525 "space of input is not a map", goto error
);
6527 return map_from_multi_pw_aff(mpa
);
6529 isl_multi_pw_aff_free(mpa
);
6533 /* Construct a set mapping the shared parameter domain
6534 * of the piecewise affine expressions to the space of "mpa"
6535 * with each dimension in the range equated to the
6536 * corresponding piecewise affine expression.
6538 __isl_give isl_set
*isl_set_from_multi_pw_aff(__isl_take isl_multi_pw_aff
*mpa
)
6542 if (!isl_space_is_set(mpa
->space
))
6543 isl_die(isl_multi_pw_aff_get_ctx(mpa
), isl_error_internal
,
6544 "space of input is not a set", goto error
);
6546 return map_from_multi_pw_aff(mpa
);
6548 isl_multi_pw_aff_free(mpa
);
6552 /* Construct and return a piecewise multi affine expression
6553 * that is equal to the given multi piecewise affine expression
6554 * on the shared domain of the piecewise affine expressions,
6555 * in the special case of a 0D multi piecewise affine expression.
6557 * Create a piecewise multi affine expression with the explicit domain of
6558 * the 0D multi piecewise affine expression as domain.
6560 static __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_multi_pw_aff_0D(
6561 __isl_take isl_multi_pw_aff
*mpa
)
6567 space
= isl_multi_pw_aff_get_space(mpa
);
6568 dom
= isl_multi_pw_aff_get_explicit_domain(mpa
);
6569 isl_multi_pw_aff_free(mpa
);
6571 ma
= isl_multi_aff_zero(space
);
6572 return isl_pw_multi_aff_alloc(dom
, ma
);
6575 /* Construct and return a piecewise multi affine expression
6576 * that is equal to the given multi piecewise affine expression
6577 * on the shared domain of the piecewise affine expressions.
6579 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_multi_pw_aff(
6580 __isl_take isl_multi_pw_aff
*mpa
)
6585 isl_pw_multi_aff
*pma
;
6591 return isl_pw_multi_aff_from_multi_pw_aff_0D(mpa
);
6593 space
= isl_multi_pw_aff_get_space(mpa
);
6594 pa
= isl_multi_pw_aff_get_pw_aff(mpa
, 0);
6595 pma
= isl_pw_multi_aff_from_pw_aff(pa
);
6597 for (i
= 1; i
< mpa
->n
; ++i
) {
6598 isl_pw_multi_aff
*pma_i
;
6600 pa
= isl_multi_pw_aff_get_pw_aff(mpa
, i
);
6601 pma_i
= isl_pw_multi_aff_from_pw_aff(pa
);
6602 pma
= isl_pw_multi_aff_range_product(pma
, pma_i
);
6605 pma
= isl_pw_multi_aff_reset_space(pma
, space
);
6607 isl_multi_pw_aff_free(mpa
);
6611 /* Construct and return a multi piecewise affine expression
6612 * that is equal to the given multi affine expression.
6614 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_from_multi_aff(
6615 __isl_take isl_multi_aff
*ma
)
6618 isl_multi_pw_aff
*mpa
;
6623 n
= isl_multi_aff_dim(ma
, isl_dim_out
);
6624 mpa
= isl_multi_pw_aff_alloc(isl_multi_aff_get_space(ma
));
6626 for (i
= 0; i
< n
; ++i
) {
6629 pa
= isl_pw_aff_from_aff(isl_multi_aff_get_aff(ma
, i
));
6630 mpa
= isl_multi_pw_aff_set_pw_aff(mpa
, i
, pa
);
6633 isl_multi_aff_free(ma
);
6637 /* Construct and return a multi piecewise affine expression
6638 * that is equal to the given piecewise multi affine expression.
6640 * If the resulting multi piecewise affine expression has
6641 * an explicit domain, then assign it the domain of the input.
6642 * In other cases, the domain is stored in the individual elements.
6644 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_from_pw_multi_aff(
6645 __isl_take isl_pw_multi_aff
*pma
)
6649 isl_multi_pw_aff
*mpa
;
6654 n
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
6655 space
= isl_pw_multi_aff_get_space(pma
);
6656 mpa
= isl_multi_pw_aff_alloc(space
);
6658 for (i
= 0; i
< n
; ++i
) {
6661 pa
= isl_pw_multi_aff_get_pw_aff(pma
, i
);
6662 mpa
= isl_multi_pw_aff_set_pw_aff(mpa
, i
, pa
);
6664 if (isl_multi_pw_aff_has_explicit_domain(mpa
)) {
6667 dom
= isl_pw_multi_aff_domain(isl_pw_multi_aff_copy(pma
));
6668 mpa
= isl_multi_pw_aff_intersect_domain(mpa
, dom
);
6671 isl_pw_multi_aff_free(pma
);
6675 /* Do "pa1" and "pa2" represent the same function?
6677 * We first check if they are obviously equal.
6678 * If not, we convert them to maps and check if those are equal.
6680 * If "pa1" or "pa2" contain any NaNs, then they are considered
6681 * not to be the same. A NaN is not equal to anything, not even
6684 isl_bool
isl_pw_aff_is_equal(__isl_keep isl_pw_aff
*pa1
,
6685 __isl_keep isl_pw_aff
*pa2
)
6689 isl_map
*map1
, *map2
;
6692 return isl_bool_error
;
6694 equal
= isl_pw_aff_plain_is_equal(pa1
, pa2
);
6695 if (equal
< 0 || equal
)
6697 has_nan
= either_involves_nan(pa1
, pa2
);
6699 return isl_bool_error
;
6701 return isl_bool_false
;
6703 map1
= map_from_pw_aff(isl_pw_aff_copy(pa1
));
6704 map2
= map_from_pw_aff(isl_pw_aff_copy(pa2
));
6705 equal
= isl_map_is_equal(map1
, map2
);
6712 /* Do "mpa1" and "mpa2" represent the same function?
6714 * Note that we cannot convert the entire isl_multi_pw_aff
6715 * to a map because the domains of the piecewise affine expressions
6716 * may not be the same.
6718 isl_bool
isl_multi_pw_aff_is_equal(__isl_keep isl_multi_pw_aff
*mpa1
,
6719 __isl_keep isl_multi_pw_aff
*mpa2
)
6722 isl_bool equal
, equal_params
;
6725 return isl_bool_error
;
6727 equal_params
= isl_space_has_equal_params(mpa1
->space
, mpa2
->space
);
6728 if (equal_params
< 0)
6729 return isl_bool_error
;
6730 if (!equal_params
) {
6731 if (!isl_space_has_named_params(mpa1
->space
))
6732 return isl_bool_false
;
6733 if (!isl_space_has_named_params(mpa2
->space
))
6734 return isl_bool_false
;
6735 mpa1
= isl_multi_pw_aff_copy(mpa1
);
6736 mpa2
= isl_multi_pw_aff_copy(mpa2
);
6737 mpa1
= isl_multi_pw_aff_align_params(mpa1
,
6738 isl_multi_pw_aff_get_space(mpa2
));
6739 mpa2
= isl_multi_pw_aff_align_params(mpa2
,
6740 isl_multi_pw_aff_get_space(mpa1
));
6741 equal
= isl_multi_pw_aff_is_equal(mpa1
, mpa2
);
6742 isl_multi_pw_aff_free(mpa1
);
6743 isl_multi_pw_aff_free(mpa2
);
6747 equal
= isl_space_is_equal(mpa1
->space
, mpa2
->space
);
6748 if (equal
< 0 || !equal
)
6751 for (i
= 0; i
< mpa1
->n
; ++i
) {
6752 equal
= isl_pw_aff_is_equal(mpa1
->u
.p
[i
], mpa2
->u
.p
[i
]);
6753 if (equal
< 0 || !equal
)
6757 return isl_bool_true
;
6760 /* Do "pma1" and "pma2" represent the same function?
6762 * First check if they are obviously equal.
6763 * If not, then convert them to maps and check if those are equal.
6765 * If "pa1" or "pa2" contain any NaNs, then they are considered
6766 * not to be the same. A NaN is not equal to anything, not even
6769 isl_bool
isl_pw_multi_aff_is_equal(__isl_keep isl_pw_multi_aff
*pma1
,
6770 __isl_keep isl_pw_multi_aff
*pma2
)
6774 isl_map
*map1
, *map2
;
6777 return isl_bool_error
;
6779 equal
= isl_pw_multi_aff_plain_is_equal(pma1
, pma2
);
6780 if (equal
< 0 || equal
)
6782 has_nan
= isl_pw_multi_aff_involves_nan(pma1
);
6783 if (has_nan
>= 0 && !has_nan
)
6784 has_nan
= isl_pw_multi_aff_involves_nan(pma2
);
6785 if (has_nan
< 0 || has_nan
)
6786 return isl_bool_not(has_nan
);
6788 map1
= isl_map_from_pw_multi_aff(isl_pw_multi_aff_copy(pma1
));
6789 map2
= isl_map_from_pw_multi_aff(isl_pw_multi_aff_copy(pma2
));
6790 equal
= isl_map_is_equal(map1
, map2
);
6797 /* Compute the pullback of "mpa" by the function represented by "ma".
6798 * In other words, plug in "ma" in "mpa".
6800 * The parameters of "mpa" and "ma" are assumed to have been aligned.
6802 * If "mpa" has an explicit domain, then it is this domain
6803 * that needs to undergo a pullback, i.e., a preimage.
6805 static __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_pullback_multi_aff_aligned(
6806 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_multi_aff
*ma
)
6809 isl_space
*space
= NULL
;
6811 mpa
= isl_multi_pw_aff_cow(mpa
);
6815 space
= isl_space_join(isl_multi_aff_get_space(ma
),
6816 isl_multi_pw_aff_get_space(mpa
));
6820 for (i
= 0; i
< mpa
->n
; ++i
) {
6821 mpa
->u
.p
[i
] = isl_pw_aff_pullback_multi_aff(mpa
->u
.p
[i
],
6822 isl_multi_aff_copy(ma
));
6826 if (isl_multi_pw_aff_has_explicit_domain(mpa
)) {
6827 mpa
->u
.dom
= isl_set_preimage_multi_aff(mpa
->u
.dom
,
6828 isl_multi_aff_copy(ma
));
6833 isl_multi_aff_free(ma
);
6834 isl_space_free(mpa
->space
);
6838 isl_space_free(space
);
6839 isl_multi_pw_aff_free(mpa
);
6840 isl_multi_aff_free(ma
);
6844 /* Compute the pullback of "mpa" by the function represented by "ma".
6845 * In other words, plug in "ma" in "mpa".
6847 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_pullback_multi_aff(
6848 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_multi_aff
*ma
)
6850 isl_bool equal_params
;
6854 equal_params
= isl_space_has_equal_params(mpa
->space
, ma
->space
);
6855 if (equal_params
< 0)
6858 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa
, ma
);
6859 mpa
= isl_multi_pw_aff_align_params(mpa
, isl_multi_aff_get_space(ma
));
6860 ma
= isl_multi_aff_align_params(ma
, isl_multi_pw_aff_get_space(mpa
));
6861 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa
, ma
);
6863 isl_multi_pw_aff_free(mpa
);
6864 isl_multi_aff_free(ma
);
6868 /* Compute the pullback of "mpa" by the function represented by "pma".
6869 * In other words, plug in "pma" in "mpa".
6871 * The parameters of "mpa" and "mpa" are assumed to have been aligned.
6873 * If "mpa" has an explicit domain, then it is this domain
6874 * that needs to undergo a pullback, i.e., a preimage.
6876 static __isl_give isl_multi_pw_aff
*
6877 isl_multi_pw_aff_pullback_pw_multi_aff_aligned(
6878 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_pw_multi_aff
*pma
)
6881 isl_space
*space
= NULL
;
6883 mpa
= isl_multi_pw_aff_cow(mpa
);
6887 space
= isl_space_join(isl_pw_multi_aff_get_space(pma
),
6888 isl_multi_pw_aff_get_space(mpa
));
6890 for (i
= 0; i
< mpa
->n
; ++i
) {
6891 mpa
->u
.p
[i
] = isl_pw_aff_pullback_pw_multi_aff_aligned(
6892 mpa
->u
.p
[i
], isl_pw_multi_aff_copy(pma
));
6896 if (isl_multi_pw_aff_has_explicit_domain(mpa
)) {
6897 mpa
->u
.dom
= isl_set_preimage_pw_multi_aff(mpa
->u
.dom
,
6898 isl_pw_multi_aff_copy(pma
));
6903 isl_pw_multi_aff_free(pma
);
6904 isl_space_free(mpa
->space
);
6908 isl_space_free(space
);
6909 isl_multi_pw_aff_free(mpa
);
6910 isl_pw_multi_aff_free(pma
);
6914 /* Compute the pullback of "mpa" by the function represented by "pma".
6915 * In other words, plug in "pma" in "mpa".
6917 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_pullback_pw_multi_aff(
6918 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_pw_multi_aff
*pma
)
6920 isl_bool equal_params
;
6924 equal_params
= isl_space_has_equal_params(mpa
->space
, pma
->dim
);
6925 if (equal_params
< 0)
6928 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa
, pma
);
6929 mpa
= isl_multi_pw_aff_align_params(mpa
,
6930 isl_pw_multi_aff_get_space(pma
));
6931 pma
= isl_pw_multi_aff_align_params(pma
,
6932 isl_multi_pw_aff_get_space(mpa
));
6933 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa
, pma
);
6935 isl_multi_pw_aff_free(mpa
);
6936 isl_pw_multi_aff_free(pma
);
6940 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
6941 * with the domain of "aff". The domain of the result is the same
6943 * "mpa" and "aff" are assumed to have been aligned.
6945 * We first extract the parametric constant from "aff", defined
6946 * over the correct domain.
6947 * Then we add the appropriate combinations of the members of "mpa".
6948 * Finally, we add the integer divisions through recursive calls.
6950 static __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_aff_aligned(
6951 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_aff
*aff
)
6959 n_in
= isl_aff_dim(aff
, isl_dim_in
);
6960 n_div
= isl_aff_dim(aff
, isl_dim_div
);
6962 space
= isl_space_domain(isl_multi_pw_aff_get_space(mpa
));
6963 tmp
= isl_aff_copy(aff
);
6964 tmp
= isl_aff_drop_dims(tmp
, isl_dim_div
, 0, n_div
);
6965 tmp
= isl_aff_drop_dims(tmp
, isl_dim_in
, 0, n_in
);
6966 tmp
= isl_aff_add_dims(tmp
, isl_dim_in
,
6967 isl_space_dim(space
, isl_dim_set
));
6968 tmp
= isl_aff_reset_domain_space(tmp
, space
);
6969 pa
= isl_pw_aff_from_aff(tmp
);
6971 for (i
= 0; i
< n_in
; ++i
) {
6974 if (!isl_aff_involves_dims(aff
, isl_dim_in
, i
, 1))
6976 v
= isl_aff_get_coefficient_val(aff
, isl_dim_in
, i
);
6977 pa_i
= isl_multi_pw_aff_get_pw_aff(mpa
, i
);
6978 pa_i
= isl_pw_aff_scale_val(pa_i
, v
);
6979 pa
= isl_pw_aff_add(pa
, pa_i
);
6982 for (i
= 0; i
< n_div
; ++i
) {
6986 if (!isl_aff_involves_dims(aff
, isl_dim_div
, i
, 1))
6988 div
= isl_aff_get_div(aff
, i
);
6989 pa_i
= isl_multi_pw_aff_apply_aff_aligned(
6990 isl_multi_pw_aff_copy(mpa
), div
);
6991 pa_i
= isl_pw_aff_floor(pa_i
);
6992 v
= isl_aff_get_coefficient_val(aff
, isl_dim_div
, i
);
6993 pa_i
= isl_pw_aff_scale_val(pa_i
, v
);
6994 pa
= isl_pw_aff_add(pa
, pa_i
);
6997 isl_multi_pw_aff_free(mpa
);
7003 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
7004 * with the domain of "aff". The domain of the result is the same
7007 __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_aff(
7008 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_aff
*aff
)
7010 isl_bool equal_params
;
7014 equal_params
= isl_space_has_equal_params(aff
->ls
->dim
, mpa
->space
);
7015 if (equal_params
< 0)
7018 return isl_multi_pw_aff_apply_aff_aligned(mpa
, aff
);
7020 aff
= isl_aff_align_params(aff
, isl_multi_pw_aff_get_space(mpa
));
7021 mpa
= isl_multi_pw_aff_align_params(mpa
, isl_aff_get_space(aff
));
7023 return isl_multi_pw_aff_apply_aff_aligned(mpa
, aff
);
7026 isl_multi_pw_aff_free(mpa
);
7030 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
7031 * with the domain of "pa". The domain of the result is the same
7033 * "mpa" and "pa" are assumed to have been aligned.
7035 * We consider each piece in turn. Note that the domains of the
7036 * pieces are assumed to be disjoint and they remain disjoint
7037 * after taking the preimage (over the same function).
7039 static __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_pw_aff_aligned(
7040 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_pw_aff
*pa
)
7049 space
= isl_space_join(isl_multi_pw_aff_get_space(mpa
),
7050 isl_pw_aff_get_space(pa
));
7051 res
= isl_pw_aff_empty(space
);
7053 for (i
= 0; i
< pa
->n
; ++i
) {
7057 pa_i
= isl_multi_pw_aff_apply_aff_aligned(
7058 isl_multi_pw_aff_copy(mpa
),
7059 isl_aff_copy(pa
->p
[i
].aff
));
7060 domain
= isl_set_copy(pa
->p
[i
].set
);
7061 domain
= isl_set_preimage_multi_pw_aff(domain
,
7062 isl_multi_pw_aff_copy(mpa
));
7063 pa_i
= isl_pw_aff_intersect_domain(pa_i
, domain
);
7064 res
= isl_pw_aff_add_disjoint(res
, pa_i
);
7067 isl_pw_aff_free(pa
);
7068 isl_multi_pw_aff_free(mpa
);
7071 isl_pw_aff_free(pa
);
7072 isl_multi_pw_aff_free(mpa
);
7076 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
7077 * with the domain of "pa". The domain of the result is the same
7080 __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_pw_aff(
7081 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_pw_aff
*pa
)
7083 isl_bool equal_params
;
7087 equal_params
= isl_space_has_equal_params(pa
->dim
, mpa
->space
);
7088 if (equal_params
< 0)
7091 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa
, pa
);
7093 pa
= isl_pw_aff_align_params(pa
, isl_multi_pw_aff_get_space(mpa
));
7094 mpa
= isl_multi_pw_aff_align_params(mpa
, isl_pw_aff_get_space(pa
));
7096 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa
, pa
);
7098 isl_pw_aff_free(pa
);
7099 isl_multi_pw_aff_free(mpa
);
7103 /* Compute the pullback of "pa" by the function represented by "mpa".
7104 * In other words, plug in "mpa" in "pa".
7105 * "pa" and "mpa" are assumed to have been aligned.
7107 * The pullback is computed by applying "pa" to "mpa".
7109 static __isl_give isl_pw_aff
*isl_pw_aff_pullback_multi_pw_aff_aligned(
7110 __isl_take isl_pw_aff
*pa
, __isl_take isl_multi_pw_aff
*mpa
)
7112 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa
, pa
);
7115 /* Compute the pullback of "pa" by the function represented by "mpa".
7116 * In other words, plug in "mpa" in "pa".
7118 * The pullback is computed by applying "pa" to "mpa".
7120 __isl_give isl_pw_aff
*isl_pw_aff_pullback_multi_pw_aff(
7121 __isl_take isl_pw_aff
*pa
, __isl_take isl_multi_pw_aff
*mpa
)
7123 return isl_multi_pw_aff_apply_pw_aff(mpa
, pa
);
7126 /* Compute the pullback of "mpa1" by the function represented by "mpa2".
7127 * In other words, plug in "mpa2" in "mpa1".
7129 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7131 * We pullback each member of "mpa1" in turn.
7133 * If "mpa1" has an explicit domain, then it is this domain
7134 * that needs to undergo a pullback instead, i.e., a preimage.
7136 static __isl_give isl_multi_pw_aff
*
7137 isl_multi_pw_aff_pullback_multi_pw_aff_aligned(
7138 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
)
7141 isl_space
*space
= NULL
;
7143 mpa1
= isl_multi_pw_aff_cow(mpa1
);
7147 space
= isl_space_join(isl_multi_pw_aff_get_space(mpa2
),
7148 isl_multi_pw_aff_get_space(mpa1
));
7150 for (i
= 0; i
< mpa1
->n
; ++i
) {
7151 mpa1
->u
.p
[i
] = isl_pw_aff_pullback_multi_pw_aff_aligned(
7152 mpa1
->u
.p
[i
], isl_multi_pw_aff_copy(mpa2
));
7157 if (isl_multi_pw_aff_has_explicit_domain(mpa1
)) {
7158 mpa1
->u
.dom
= isl_set_preimage_multi_pw_aff(mpa1
->u
.dom
,
7159 isl_multi_pw_aff_copy(mpa2
));
7163 mpa1
= isl_multi_pw_aff_reset_space(mpa1
, space
);
7165 isl_multi_pw_aff_free(mpa2
);
7168 isl_space_free(space
);
7169 isl_multi_pw_aff_free(mpa1
);
7170 isl_multi_pw_aff_free(mpa2
);
7174 /* Compute the pullback of "mpa1" by the function represented by "mpa2".
7175 * In other words, plug in "mpa2" in "mpa1".
7177 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_pullback_multi_pw_aff(
7178 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
)
7180 return isl_multi_pw_aff_align_params_multi_multi_and(mpa1
, mpa2
,
7181 &isl_multi_pw_aff_pullback_multi_pw_aff_aligned
);
7184 /* Align the parameters of "mpa1" and "mpa2", check that the ranges
7185 * of "mpa1" and "mpa2" live in the same space, construct map space
7186 * between the domain spaces of "mpa1" and "mpa2" and call "order"
7187 * with this map space as extract argument.
7189 static __isl_give isl_map
*isl_multi_pw_aff_order_map(
7190 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
,
7191 __isl_give isl_map
*(*order
)(__isl_keep isl_multi_pw_aff
*mpa1
,
7192 __isl_keep isl_multi_pw_aff
*mpa2
, __isl_take isl_space
*space
))
7195 isl_space
*space1
, *space2
;
7198 mpa1
= isl_multi_pw_aff_align_params(mpa1
,
7199 isl_multi_pw_aff_get_space(mpa2
));
7200 mpa2
= isl_multi_pw_aff_align_params(mpa2
,
7201 isl_multi_pw_aff_get_space(mpa1
));
7204 match
= isl_space_tuple_is_equal(mpa1
->space
, isl_dim_out
,
7205 mpa2
->space
, isl_dim_out
);
7209 isl_die(isl_multi_pw_aff_get_ctx(mpa1
), isl_error_invalid
,
7210 "range spaces don't match", goto error
);
7211 space1
= isl_space_domain(isl_multi_pw_aff_get_space(mpa1
));
7212 space2
= isl_space_domain(isl_multi_pw_aff_get_space(mpa2
));
7213 space1
= isl_space_map_from_domain_and_range(space1
, space2
);
7215 res
= order(mpa1
, mpa2
, space1
);
7216 isl_multi_pw_aff_free(mpa1
);
7217 isl_multi_pw_aff_free(mpa2
);
7220 isl_multi_pw_aff_free(mpa1
);
7221 isl_multi_pw_aff_free(mpa2
);
7225 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7226 * where the function values are equal. "space" is the space of the result.
7227 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7229 * "mpa1" and "mpa2" are equal when each of the pairs of elements
7230 * in the sequences are equal.
7232 static __isl_give isl_map
*isl_multi_pw_aff_eq_map_on_space(
7233 __isl_keep isl_multi_pw_aff
*mpa1
, __isl_keep isl_multi_pw_aff
*mpa2
,
7234 __isl_take isl_space
*space
)
7239 res
= isl_map_universe(space
);
7241 n
= isl_multi_pw_aff_dim(mpa1
, isl_dim_out
);
7242 for (i
= 0; i
< n
; ++i
) {
7243 isl_pw_aff
*pa1
, *pa2
;
7246 pa1
= isl_multi_pw_aff_get_pw_aff(mpa1
, i
);
7247 pa2
= isl_multi_pw_aff_get_pw_aff(mpa2
, i
);
7248 map
= isl_pw_aff_eq_map(pa1
, pa2
);
7249 res
= isl_map_intersect(res
, map
);
7255 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7256 * where the function values are equal.
7258 __isl_give isl_map
*isl_multi_pw_aff_eq_map(__isl_take isl_multi_pw_aff
*mpa1
,
7259 __isl_take isl_multi_pw_aff
*mpa2
)
7261 return isl_multi_pw_aff_order_map(mpa1
, mpa2
,
7262 &isl_multi_pw_aff_eq_map_on_space
);
7265 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7266 * where the function values of "mpa1" is lexicographically satisfies "base"
7267 * compared to that of "mpa2". "space" is the space of the result.
7268 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7270 * "mpa1" lexicographically satisfies "base" compared to "mpa2"
7271 * if its i-th element satisfies "base" when compared to
7272 * the i-th element of "mpa2" while all previous elements are
7275 static __isl_give isl_map
*isl_multi_pw_aff_lex_map_on_space(
7276 __isl_keep isl_multi_pw_aff
*mpa1
, __isl_keep isl_multi_pw_aff
*mpa2
,
7277 __isl_give isl_map
*(*base
)(__isl_take isl_pw_aff
*pa1
,
7278 __isl_take isl_pw_aff
*pa2
),
7279 __isl_take isl_space
*space
)
7282 isl_map
*res
, *rest
;
7284 res
= isl_map_empty(isl_space_copy(space
));
7285 rest
= isl_map_universe(space
);
7287 n
= isl_multi_pw_aff_dim(mpa1
, isl_dim_out
);
7288 for (i
= 0; i
< n
; ++i
) {
7289 isl_pw_aff
*pa1
, *pa2
;
7292 pa1
= isl_multi_pw_aff_get_pw_aff(mpa1
, i
);
7293 pa2
= isl_multi_pw_aff_get_pw_aff(mpa2
, i
);
7294 map
= base(pa1
, pa2
);
7295 map
= isl_map_intersect(map
, isl_map_copy(rest
));
7296 res
= isl_map_union(res
, map
);
7301 pa1
= isl_multi_pw_aff_get_pw_aff(mpa1
, i
);
7302 pa2
= isl_multi_pw_aff_get_pw_aff(mpa2
, i
);
7303 map
= isl_pw_aff_eq_map(pa1
, pa2
);
7304 rest
= isl_map_intersect(rest
, map
);
7311 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7312 * where the function value of "mpa1" is lexicographically less than that
7313 * of "mpa2". "space" is the space of the result.
7314 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7316 * "mpa1" is less than "mpa2" if its i-th element is smaller
7317 * than the i-th element of "mpa2" while all previous elements are
7320 __isl_give isl_map
*isl_multi_pw_aff_lex_lt_map_on_space(
7321 __isl_keep isl_multi_pw_aff
*mpa1
, __isl_keep isl_multi_pw_aff
*mpa2
,
7322 __isl_take isl_space
*space
)
7324 return isl_multi_pw_aff_lex_map_on_space(mpa1
, mpa2
,
7325 &isl_pw_aff_lt_map
, space
);
7328 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7329 * where the function value of "mpa1" is lexicographically less than that
7332 __isl_give isl_map
*isl_multi_pw_aff_lex_lt_map(
7333 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
)
7335 return isl_multi_pw_aff_order_map(mpa1
, mpa2
,
7336 &isl_multi_pw_aff_lex_lt_map_on_space
);
7339 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7340 * where the function value of "mpa1" is lexicographically greater than that
7341 * of "mpa2". "space" is the space of the result.
7342 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7344 * "mpa1" is greater than "mpa2" if its i-th element is greater
7345 * than the i-th element of "mpa2" while all previous elements are
7348 __isl_give isl_map
*isl_multi_pw_aff_lex_gt_map_on_space(
7349 __isl_keep isl_multi_pw_aff
*mpa1
, __isl_keep isl_multi_pw_aff
*mpa2
,
7350 __isl_take isl_space
*space
)
7352 return isl_multi_pw_aff_lex_map_on_space(mpa1
, mpa2
,
7353 &isl_pw_aff_gt_map
, space
);
7356 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7357 * where the function value of "mpa1" is lexicographically greater than that
7360 __isl_give isl_map
*isl_multi_pw_aff_lex_gt_map(
7361 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
)
7363 return isl_multi_pw_aff_order_map(mpa1
, mpa2
,
7364 &isl_multi_pw_aff_lex_gt_map_on_space
);
7367 /* Compare two isl_affs.
7369 * Return -1 if "aff1" is "smaller" than "aff2", 1 if "aff1" is "greater"
7370 * than "aff2" and 0 if they are equal.
7372 * The order is fairly arbitrary. We do consider expressions that only involve
7373 * earlier dimensions as "smaller".
7375 int isl_aff_plain_cmp(__isl_keep isl_aff
*aff1
, __isl_keep isl_aff
*aff2
)
7388 cmp
= isl_local_space_cmp(aff1
->ls
, aff2
->ls
);
7392 last1
= isl_seq_last_non_zero(aff1
->v
->el
+ 1, aff1
->v
->size
- 1);
7393 last2
= isl_seq_last_non_zero(aff2
->v
->el
+ 1, aff1
->v
->size
- 1);
7395 return last1
- last2
;
7397 return isl_seq_cmp(aff1
->v
->el
, aff2
->v
->el
, aff1
->v
->size
);
7400 /* Compare two isl_pw_affs.
7402 * Return -1 if "pa1" is "smaller" than "pa2", 1 if "pa1" is "greater"
7403 * than "pa2" and 0 if they are equal.
7405 * The order is fairly arbitrary. We do consider expressions that only involve
7406 * earlier dimensions as "smaller".
7408 int isl_pw_aff_plain_cmp(__isl_keep isl_pw_aff
*pa1
,
7409 __isl_keep isl_pw_aff
*pa2
)
7422 cmp
= isl_space_cmp(pa1
->dim
, pa2
->dim
);
7426 if (pa1
->n
!= pa2
->n
)
7427 return pa1
->n
- pa2
->n
;
7429 for (i
= 0; i
< pa1
->n
; ++i
) {
7430 cmp
= isl_set_plain_cmp(pa1
->p
[i
].set
, pa2
->p
[i
].set
);
7433 cmp
= isl_aff_plain_cmp(pa1
->p
[i
].aff
, pa2
->p
[i
].aff
);
7441 /* Return a piecewise affine expression that is equal to "v" on "domain".
7443 __isl_give isl_pw_aff
*isl_pw_aff_val_on_domain(__isl_take isl_set
*domain
,
7444 __isl_take isl_val
*v
)
7447 isl_local_space
*ls
;
7450 space
= isl_set_get_space(domain
);
7451 ls
= isl_local_space_from_space(space
);
7452 aff
= isl_aff_val_on_domain(ls
, v
);
7454 return isl_pw_aff_alloc(domain
, aff
);
7457 /* Return a multi affine expression that is equal to "mv" on domain
7460 __isl_give isl_multi_aff
*isl_multi_aff_multi_val_on_space(
7461 __isl_take isl_space
*space
, __isl_take isl_multi_val
*mv
)
7465 isl_local_space
*ls
;
7471 n
= isl_multi_val_dim(mv
, isl_dim_set
);
7472 space2
= isl_multi_val_get_space(mv
);
7473 space2
= isl_space_align_params(space2
, isl_space_copy(space
));
7474 space
= isl_space_align_params(space
, isl_space_copy(space2
));
7475 space
= isl_space_map_from_domain_and_range(space
, space2
);
7476 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
7477 ls
= isl_local_space_from_space(isl_space_domain(space
));
7478 for (i
= 0; i
< n
; ++i
) {
7482 v
= isl_multi_val_get_val(mv
, i
);
7483 aff
= isl_aff_val_on_domain(isl_local_space_copy(ls
), v
);
7484 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
7486 isl_local_space_free(ls
);
7488 isl_multi_val_free(mv
);
7491 isl_space_free(space
);
7492 isl_multi_val_free(mv
);
7496 /* Return a piecewise multi-affine expression
7497 * that is equal to "mv" on "domain".
7499 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_multi_val_on_domain(
7500 __isl_take isl_set
*domain
, __isl_take isl_multi_val
*mv
)
7505 space
= isl_set_get_space(domain
);
7506 ma
= isl_multi_aff_multi_val_on_space(space
, mv
);
7508 return isl_pw_multi_aff_alloc(domain
, ma
);
7511 /* Internal data structure for isl_union_pw_multi_aff_multi_val_on_domain.
7512 * mv is the value that should be attained on each domain set
7513 * res collects the results
7515 struct isl_union_pw_multi_aff_multi_val_on_domain_data
{
7517 isl_union_pw_multi_aff
*res
;
7520 /* Create an isl_pw_multi_aff equal to data->mv on "domain"
7521 * and add it to data->res.
7523 static isl_stat
pw_multi_aff_multi_val_on_domain(__isl_take isl_set
*domain
,
7526 struct isl_union_pw_multi_aff_multi_val_on_domain_data
*data
= user
;
7527 isl_pw_multi_aff
*pma
;
7530 mv
= isl_multi_val_copy(data
->mv
);
7531 pma
= isl_pw_multi_aff_multi_val_on_domain(domain
, mv
);
7532 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma
);
7534 return data
->res
? isl_stat_ok
: isl_stat_error
;
7537 /* Return a union piecewise multi-affine expression
7538 * that is equal to "mv" on "domain".
7540 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_multi_val_on_domain(
7541 __isl_take isl_union_set
*domain
, __isl_take isl_multi_val
*mv
)
7543 struct isl_union_pw_multi_aff_multi_val_on_domain_data data
;
7546 space
= isl_union_set_get_space(domain
);
7547 data
.res
= isl_union_pw_multi_aff_empty(space
);
7549 if (isl_union_set_foreach_set(domain
,
7550 &pw_multi_aff_multi_val_on_domain
, &data
) < 0)
7551 data
.res
= isl_union_pw_multi_aff_free(data
.res
);
7552 isl_union_set_free(domain
);
7553 isl_multi_val_free(mv
);
7557 /* Compute the pullback of data->pma by the function represented by "pma2",
7558 * provided the spaces match, and add the results to data->res.
7560 static isl_stat
pullback_entry(__isl_take isl_pw_multi_aff
*pma2
, void *user
)
7562 struct isl_union_pw_multi_aff_bin_data
*data
= user
;
7564 if (!isl_space_tuple_is_equal(data
->pma
->dim
, isl_dim_in
,
7565 pma2
->dim
, isl_dim_out
)) {
7566 isl_pw_multi_aff_free(pma2
);
7570 pma2
= isl_pw_multi_aff_pullback_pw_multi_aff(
7571 isl_pw_multi_aff_copy(data
->pma
), pma2
);
7573 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma2
);
7575 return isl_stat_error
;
7580 /* Compute the pullback of "upma1" by the function represented by "upma2".
7582 __isl_give isl_union_pw_multi_aff
*
7583 isl_union_pw_multi_aff_pullback_union_pw_multi_aff(
7584 __isl_take isl_union_pw_multi_aff
*upma1
,
7585 __isl_take isl_union_pw_multi_aff
*upma2
)
7587 return bin_op(upma1
, upma2
, &pullback_entry
);
7590 /* Check that the domain space of "upa" matches "space".
7592 * This function is called from isl_multi_union_pw_aff_set_union_pw_aff and
7593 * can in principle never fail since the space "space" is that
7594 * of the isl_multi_union_pw_aff and is a set space such that
7595 * there is no domain space to match.
7597 * We check the parameters and double-check that "space" is
7598 * indeed that of a set.
7600 static isl_stat
isl_union_pw_aff_check_match_domain_space(
7601 __isl_keep isl_union_pw_aff
*upa
, __isl_keep isl_space
*space
)
7603 isl_space
*upa_space
;
7607 return isl_stat_error
;
7609 match
= isl_space_is_set(space
);
7611 return isl_stat_error
;
7613 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
7614 "expecting set space", return isl_stat_error
);
7616 upa_space
= isl_union_pw_aff_get_space(upa
);
7617 match
= isl_space_has_equal_params(space
, upa_space
);
7621 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
7622 "parameters don't match", goto error
);
7624 isl_space_free(upa_space
);
7627 isl_space_free(upa_space
);
7628 return isl_stat_error
;
7631 /* Do the parameters of "upa" match those of "space"?
7633 static isl_bool
isl_union_pw_aff_matching_params(
7634 __isl_keep isl_union_pw_aff
*upa
, __isl_keep isl_space
*space
)
7636 isl_space
*upa_space
;
7640 return isl_bool_error
;
7642 upa_space
= isl_union_pw_aff_get_space(upa
);
7644 match
= isl_space_has_equal_params(space
, upa_space
);
7646 isl_space_free(upa_space
);
7650 /* Internal data structure for isl_union_pw_aff_reset_domain_space.
7651 * space represents the new parameters.
7652 * res collects the results.
7654 struct isl_union_pw_aff_reset_params_data
{
7656 isl_union_pw_aff
*res
;
7659 /* Replace the parameters of "pa" by data->space and
7660 * add the result to data->res.
7662 static isl_stat
reset_params(__isl_take isl_pw_aff
*pa
, void *user
)
7664 struct isl_union_pw_aff_reset_params_data
*data
= user
;
7667 space
= isl_pw_aff_get_space(pa
);
7668 space
= isl_space_replace_params(space
, data
->space
);
7669 pa
= isl_pw_aff_reset_space(pa
, space
);
7670 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
7672 return data
->res
? isl_stat_ok
: isl_stat_error
;
7675 /* Replace the domain space of "upa" by "space".
7676 * Since a union expression does not have a (single) domain space,
7677 * "space" is necessarily a parameter space.
7679 * Since the order and the names of the parameters determine
7680 * the hash value, we need to create a new hash table.
7682 static __isl_give isl_union_pw_aff
*isl_union_pw_aff_reset_domain_space(
7683 __isl_take isl_union_pw_aff
*upa
, __isl_take isl_space
*space
)
7685 struct isl_union_pw_aff_reset_params_data data
= { space
};
7688 match
= isl_union_pw_aff_matching_params(upa
, space
);
7690 upa
= isl_union_pw_aff_free(upa
);
7692 isl_space_free(space
);
7696 data
.res
= isl_union_pw_aff_empty(isl_space_copy(space
));
7697 if (isl_union_pw_aff_foreach_pw_aff(upa
, &reset_params
, &data
) < 0)
7698 data
.res
= isl_union_pw_aff_free(data
.res
);
7700 isl_union_pw_aff_free(upa
);
7701 isl_space_free(space
);
7705 /* Return the floor of "pa".
7707 static __isl_give isl_pw_aff
*floor_entry(__isl_take isl_pw_aff
*pa
, void *user
)
7709 return isl_pw_aff_floor(pa
);
7712 /* Given f, return floor(f).
7714 __isl_give isl_union_pw_aff
*isl_union_pw_aff_floor(
7715 __isl_take isl_union_pw_aff
*upa
)
7717 return isl_union_pw_aff_transform_inplace(upa
, &floor_entry
, NULL
);
7722 * upa mod m = upa - m * floor(upa/m)
7724 * with m an integer value.
7726 __isl_give isl_union_pw_aff
*isl_union_pw_aff_mod_val(
7727 __isl_take isl_union_pw_aff
*upa
, __isl_take isl_val
*m
)
7729 isl_union_pw_aff
*res
;
7734 if (!isl_val_is_int(m
))
7735 isl_die(isl_val_get_ctx(m
), isl_error_invalid
,
7736 "expecting integer modulo", goto error
);
7737 if (!isl_val_is_pos(m
))
7738 isl_die(isl_val_get_ctx(m
), isl_error_invalid
,
7739 "expecting positive modulo", goto error
);
7741 res
= isl_union_pw_aff_copy(upa
);
7742 upa
= isl_union_pw_aff_scale_down_val(upa
, isl_val_copy(m
));
7743 upa
= isl_union_pw_aff_floor(upa
);
7744 upa
= isl_union_pw_aff_scale_val(upa
, m
);
7745 res
= isl_union_pw_aff_sub(res
, upa
);
7750 isl_union_pw_aff_free(upa
);
7754 /* Internal data structure for isl_union_pw_multi_aff_get_union_pw_aff.
7755 * pos is the output position that needs to be extracted.
7756 * res collects the results.
7758 struct isl_union_pw_multi_aff_get_union_pw_aff_data
{
7760 isl_union_pw_aff
*res
;
7763 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma"
7764 * (assuming it has such a dimension) and add it to data->res.
7766 static isl_stat
get_union_pw_aff(__isl_take isl_pw_multi_aff
*pma
, void *user
)
7768 struct isl_union_pw_multi_aff_get_union_pw_aff_data
*data
= user
;
7773 return isl_stat_error
;
7775 n_out
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
7776 if (data
->pos
>= n_out
) {
7777 isl_pw_multi_aff_free(pma
);
7781 pa
= isl_pw_multi_aff_get_pw_aff(pma
, data
->pos
);
7782 isl_pw_multi_aff_free(pma
);
7784 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
7786 return data
->res
? isl_stat_ok
: isl_stat_error
;
7789 /* Extract an isl_union_pw_aff corresponding to
7790 * output dimension "pos" of "upma".
7792 __isl_give isl_union_pw_aff
*isl_union_pw_multi_aff_get_union_pw_aff(
7793 __isl_keep isl_union_pw_multi_aff
*upma
, int pos
)
7795 struct isl_union_pw_multi_aff_get_union_pw_aff_data data
;
7802 isl_die(isl_union_pw_multi_aff_get_ctx(upma
), isl_error_invalid
,
7803 "cannot extract at negative position", return NULL
);
7805 space
= isl_union_pw_multi_aff_get_space(upma
);
7806 data
.res
= isl_union_pw_aff_empty(space
);
7808 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma
,
7809 &get_union_pw_aff
, &data
) < 0)
7810 data
.res
= isl_union_pw_aff_free(data
.res
);
7815 /* Return a union piecewise affine expression
7816 * that is equal to "aff" on "domain".
7818 __isl_give isl_union_pw_aff
*isl_union_pw_aff_aff_on_domain(
7819 __isl_take isl_union_set
*domain
, __isl_take isl_aff
*aff
)
7823 pa
= isl_pw_aff_from_aff(aff
);
7824 return isl_union_pw_aff_pw_aff_on_domain(domain
, pa
);
7827 /* Return a union piecewise affine expression
7828 * that is equal to the parameter identified by "id" on "domain".
7830 * Make sure the parameter appears in the space passed to
7831 * isl_aff_param_on_domain_space_id.
7833 __isl_give isl_union_pw_aff
*isl_union_pw_aff_param_on_domain_id(
7834 __isl_take isl_union_set
*domain
, __isl_take isl_id
*id
)
7839 space
= isl_union_set_get_space(domain
);
7840 space
= isl_space_add_param_id(space
, isl_id_copy(id
));
7841 aff
= isl_aff_param_on_domain_space_id(space
, id
);
7842 return isl_union_pw_aff_aff_on_domain(domain
, aff
);
7845 /* Internal data structure for isl_union_pw_aff_pw_aff_on_domain.
7846 * "pa" is the piecewise symbolic value that the resulting isl_union_pw_aff
7848 * "res" collects the results.
7850 struct isl_union_pw_aff_pw_aff_on_domain_data
{
7852 isl_union_pw_aff
*res
;
7855 /* Construct a piecewise affine expression that is equal to data->pa
7856 * on "domain" and add the result to data->res.
7858 static isl_stat
pw_aff_on_domain(__isl_take isl_set
*domain
, void *user
)
7860 struct isl_union_pw_aff_pw_aff_on_domain_data
*data
= user
;
7864 pa
= isl_pw_aff_copy(data
->pa
);
7865 dim
= isl_set_dim(domain
, isl_dim_set
);
7866 pa
= isl_pw_aff_from_range(pa
);
7867 pa
= isl_pw_aff_add_dims(pa
, isl_dim_in
, dim
);
7868 pa
= isl_pw_aff_reset_domain_space(pa
, isl_set_get_space(domain
));
7869 pa
= isl_pw_aff_intersect_domain(pa
, domain
);
7870 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
7872 return data
->res
? isl_stat_ok
: isl_stat_error
;
7875 /* Return a union piecewise affine expression
7876 * that is equal to "pa" on "domain", assuming "domain" and "pa"
7877 * have been aligned.
7879 * Construct an isl_pw_aff on each of the sets in "domain" and
7880 * collect the results.
7882 static __isl_give isl_union_pw_aff
*isl_union_pw_aff_pw_aff_on_domain_aligned(
7883 __isl_take isl_union_set
*domain
, __isl_take isl_pw_aff
*pa
)
7885 struct isl_union_pw_aff_pw_aff_on_domain_data data
;
7888 space
= isl_union_set_get_space(domain
);
7889 data
.res
= isl_union_pw_aff_empty(space
);
7891 if (isl_union_set_foreach_set(domain
, &pw_aff_on_domain
, &data
) < 0)
7892 data
.res
= isl_union_pw_aff_free(data
.res
);
7893 isl_union_set_free(domain
);
7894 isl_pw_aff_free(pa
);
7898 /* Return a union piecewise affine expression
7899 * that is equal to "pa" on "domain".
7901 * Check that "pa" is a parametric expression,
7902 * align the parameters if needed and call
7903 * isl_union_pw_aff_pw_aff_on_domain_aligned.
7905 __isl_give isl_union_pw_aff
*isl_union_pw_aff_pw_aff_on_domain(
7906 __isl_take isl_union_set
*domain
, __isl_take isl_pw_aff
*pa
)
7909 isl_bool equal_params
;
7910 isl_space
*domain_space
, *pa_space
;
7912 pa_space
= isl_pw_aff_peek_space(pa
);
7913 is_set
= isl_space_is_set(pa_space
);
7917 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
7918 "expecting parametric expression", goto error
);
7920 domain_space
= isl_union_set_get_space(domain
);
7921 pa_space
= isl_pw_aff_get_space(pa
);
7922 equal_params
= isl_space_has_equal_params(domain_space
, pa_space
);
7923 if (equal_params
>= 0 && !equal_params
) {
7926 space
= isl_space_align_params(domain_space
, pa_space
);
7927 pa
= isl_pw_aff_align_params(pa
, isl_space_copy(space
));
7928 domain
= isl_union_set_align_params(domain
, space
);
7930 isl_space_free(domain_space
);
7931 isl_space_free(pa_space
);
7934 if (equal_params
< 0)
7936 return isl_union_pw_aff_pw_aff_on_domain_aligned(domain
, pa
);
7938 isl_union_set_free(domain
);
7939 isl_pw_aff_free(pa
);
7943 /* Internal data structure for isl_union_pw_aff_val_on_domain.
7944 * "v" is the value that the resulting isl_union_pw_aff needs to attain.
7945 * "res" collects the results.
7947 struct isl_union_pw_aff_val_on_domain_data
{
7949 isl_union_pw_aff
*res
;
7952 /* Construct a piecewise affine expression that is equal to data->v
7953 * on "domain" and add the result to data->res.
7955 static isl_stat
pw_aff_val_on_domain(__isl_take isl_set
*domain
, void *user
)
7957 struct isl_union_pw_aff_val_on_domain_data
*data
= user
;
7961 v
= isl_val_copy(data
->v
);
7962 pa
= isl_pw_aff_val_on_domain(domain
, v
);
7963 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
7965 return data
->res
? isl_stat_ok
: isl_stat_error
;
7968 /* Return a union piecewise affine expression
7969 * that is equal to "v" on "domain".
7971 * Construct an isl_pw_aff on each of the sets in "domain" and
7972 * collect the results.
7974 __isl_give isl_union_pw_aff
*isl_union_pw_aff_val_on_domain(
7975 __isl_take isl_union_set
*domain
, __isl_take isl_val
*v
)
7977 struct isl_union_pw_aff_val_on_domain_data data
;
7980 space
= isl_union_set_get_space(domain
);
7981 data
.res
= isl_union_pw_aff_empty(space
);
7983 if (isl_union_set_foreach_set(domain
, &pw_aff_val_on_domain
, &data
) < 0)
7984 data
.res
= isl_union_pw_aff_free(data
.res
);
7985 isl_union_set_free(domain
);
7990 /* Construct a piecewise multi affine expression
7991 * that is equal to "pa" and add it to upma.
7993 static isl_stat
pw_multi_aff_from_pw_aff_entry(__isl_take isl_pw_aff
*pa
,
7996 isl_union_pw_multi_aff
**upma
= user
;
7997 isl_pw_multi_aff
*pma
;
7999 pma
= isl_pw_multi_aff_from_pw_aff(pa
);
8000 *upma
= isl_union_pw_multi_aff_add_pw_multi_aff(*upma
, pma
);
8002 return *upma
? isl_stat_ok
: isl_stat_error
;
8005 /* Construct and return a union piecewise multi affine expression
8006 * that is equal to the given union piecewise affine expression.
8008 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_union_pw_aff(
8009 __isl_take isl_union_pw_aff
*upa
)
8012 isl_union_pw_multi_aff
*upma
;
8017 space
= isl_union_pw_aff_get_space(upa
);
8018 upma
= isl_union_pw_multi_aff_empty(space
);
8020 if (isl_union_pw_aff_foreach_pw_aff(upa
,
8021 &pw_multi_aff_from_pw_aff_entry
, &upma
) < 0)
8022 upma
= isl_union_pw_multi_aff_free(upma
);
8024 isl_union_pw_aff_free(upa
);
8028 /* Compute the set of elements in the domain of "pa" where it is zero and
8029 * add this set to "uset".
8031 static isl_stat
zero_union_set(__isl_take isl_pw_aff
*pa
, void *user
)
8033 isl_union_set
**uset
= (isl_union_set
**)user
;
8035 *uset
= isl_union_set_add_set(*uset
, isl_pw_aff_zero_set(pa
));
8037 return *uset
? isl_stat_ok
: isl_stat_error
;
8040 /* Return a union set containing those elements in the domain
8041 * of "upa" where it is zero.
8043 __isl_give isl_union_set
*isl_union_pw_aff_zero_union_set(
8044 __isl_take isl_union_pw_aff
*upa
)
8046 isl_union_set
*zero
;
8048 zero
= isl_union_set_empty(isl_union_pw_aff_get_space(upa
));
8049 if (isl_union_pw_aff_foreach_pw_aff(upa
, &zero_union_set
, &zero
) < 0)
8050 zero
= isl_union_set_free(zero
);
8052 isl_union_pw_aff_free(upa
);
8056 /* Convert "pa" to an isl_map and add it to *umap.
8058 static isl_stat
map_from_pw_aff_entry(__isl_take isl_pw_aff
*pa
, void *user
)
8060 isl_union_map
**umap
= user
;
8063 map
= isl_map_from_pw_aff(pa
);
8064 *umap
= isl_union_map_add_map(*umap
, map
);
8066 return *umap
? isl_stat_ok
: isl_stat_error
;
8069 /* Construct a union map mapping the domain of the union
8070 * piecewise affine expression to its range, with the single output dimension
8071 * equated to the corresponding affine expressions on their cells.
8073 __isl_give isl_union_map
*isl_union_map_from_union_pw_aff(
8074 __isl_take isl_union_pw_aff
*upa
)
8077 isl_union_map
*umap
;
8082 space
= isl_union_pw_aff_get_space(upa
);
8083 umap
= isl_union_map_empty(space
);
8085 if (isl_union_pw_aff_foreach_pw_aff(upa
, &map_from_pw_aff_entry
,
8087 umap
= isl_union_map_free(umap
);
8089 isl_union_pw_aff_free(upa
);
8093 /* Internal data structure for isl_union_pw_aff_pullback_union_pw_multi_aff.
8094 * upma is the function that is plugged in.
8095 * pa is the current part of the function in which upma is plugged in.
8096 * res collects the results.
8098 struct isl_union_pw_aff_pullback_upma_data
{
8099 isl_union_pw_multi_aff
*upma
;
8101 isl_union_pw_aff
*res
;
8104 /* Check if "pma" can be plugged into data->pa.
8105 * If so, perform the pullback and add the result to data->res.
8107 static isl_stat
pa_pb_pma(__isl_take isl_pw_multi_aff
*pma
, void *user
)
8109 struct isl_union_pw_aff_pullback_upma_data
*data
= user
;
8112 if (!isl_space_tuple_is_equal(data
->pa
->dim
, isl_dim_in
,
8113 pma
->dim
, isl_dim_out
)) {
8114 isl_pw_multi_aff_free(pma
);
8118 pa
= isl_pw_aff_copy(data
->pa
);
8119 pa
= isl_pw_aff_pullback_pw_multi_aff(pa
, pma
);
8121 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
8123 return data
->res
? isl_stat_ok
: isl_stat_error
;
8126 /* Check if any of the elements of data->upma can be plugged into pa,
8127 * add if so add the result to data->res.
8129 static isl_stat
upa_pb_upma(__isl_take isl_pw_aff
*pa
, void *user
)
8131 struct isl_union_pw_aff_pullback_upma_data
*data
= user
;
8135 r
= isl_union_pw_multi_aff_foreach_pw_multi_aff(data
->upma
,
8137 isl_pw_aff_free(pa
);
8142 /* Compute the pullback of "upa" by the function represented by "upma".
8143 * In other words, plug in "upma" in "upa". The result contains
8144 * expressions defined over the domain space of "upma".
8146 * Run over all pairs of elements in "upa" and "upma", perform
8147 * the pullback when appropriate and collect the results.
8148 * If the hash value were based on the domain space rather than
8149 * the function space, then we could run through all elements
8150 * of "upma" and directly pick out the corresponding element of "upa".
8152 __isl_give isl_union_pw_aff
*isl_union_pw_aff_pullback_union_pw_multi_aff(
8153 __isl_take isl_union_pw_aff
*upa
,
8154 __isl_take isl_union_pw_multi_aff
*upma
)
8156 struct isl_union_pw_aff_pullback_upma_data data
= { NULL
, NULL
};
8159 space
= isl_union_pw_multi_aff_get_space(upma
);
8160 upa
= isl_union_pw_aff_align_params(upa
, space
);
8161 space
= isl_union_pw_aff_get_space(upa
);
8162 upma
= isl_union_pw_multi_aff_align_params(upma
, space
);
8168 data
.res
= isl_union_pw_aff_alloc_same_size(upa
);
8169 if (isl_union_pw_aff_foreach_pw_aff(upa
, &upa_pb_upma
, &data
) < 0)
8170 data
.res
= isl_union_pw_aff_free(data
.res
);
8172 isl_union_pw_aff_free(upa
);
8173 isl_union_pw_multi_aff_free(upma
);
8176 isl_union_pw_aff_free(upa
);
8177 isl_union_pw_multi_aff_free(upma
);
8182 #define BASE union_pw_aff
8184 #define DOMBASE union_set
8186 #define NO_MOVE_DIMS
8193 #include <isl_multi_explicit_domain.c>
8194 #include <isl_multi_union_pw_aff_explicit_domain.c>
8195 #include <isl_multi_templ.c>
8196 #include <isl_multi_apply_set.c>
8197 #include <isl_multi_apply_union_set.c>
8198 #include <isl_multi_coalesce.c>
8199 #include <isl_multi_floor.c>
8200 #include <isl_multi_gist.c>
8201 #include <isl_multi_align_set.c>
8202 #include <isl_multi_align_union_set.c>
8203 #include <isl_multi_intersect.c>
8205 /* Does "mupa" have a non-trivial explicit domain?
8207 * The explicit domain, if present, is trivial if it represents
8208 * an (obviously) universe parameter set.
8210 isl_bool
isl_multi_union_pw_aff_has_non_trivial_domain(
8211 __isl_keep isl_multi_union_pw_aff
*mupa
)
8213 isl_bool is_params
, trivial
;
8217 return isl_bool_error
;
8218 if (!isl_multi_union_pw_aff_has_explicit_domain(mupa
))
8219 return isl_bool_false
;
8220 is_params
= isl_union_set_is_params(mupa
->u
.dom
);
8221 if (is_params
< 0 || !is_params
)
8222 return isl_bool_not(is_params
);
8223 set
= isl_set_from_union_set(isl_union_set_copy(mupa
->u
.dom
));
8224 trivial
= isl_set_plain_is_universe(set
);
8226 return isl_bool_not(trivial
);
8229 /* Construct a multiple union piecewise affine expression
8230 * in the given space with value zero in each of the output dimensions.
8232 * Since there is no canonical zero value for
8233 * a union piecewise affine expression, we can only construct
8234 * a zero-dimensional "zero" value.
8236 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_zero(
8237 __isl_take isl_space
*space
)
8244 params
= isl_space_is_params(space
);
8248 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
8249 "expecting proper set space", goto error
);
8250 if (!isl_space_is_set(space
))
8251 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
8252 "expecting set space", goto error
);
8253 if (isl_space_dim(space
, isl_dim_out
) != 0)
8254 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
8255 "expecting 0D space", goto error
);
8257 return isl_multi_union_pw_aff_alloc(space
);
8259 isl_space_free(space
);
8263 /* Compute the sum of "mupa1" and "mupa2" on the union of their domains,
8264 * with the actual sum on the shared domain and
8265 * the defined expression on the symmetric difference of the domains.
8267 * We simply iterate over the elements in both arguments and
8268 * call isl_union_pw_aff_union_add on each of them, if there is
8269 * at least one element.
8271 * Otherwise, the two expressions have an explicit domain and
8272 * the union of these explicit domains is computed.
8273 * This assumes that the explicit domains are either both in terms
8274 * of specific domains elements or both in terms of parameters.
8275 * However, if one of the expressions does not have any constraints
8276 * on its explicit domain, then this is allowed as well and the result
8277 * is the expression with no constraints on its explicit domain.
8279 static __isl_give isl_multi_union_pw_aff
*
8280 isl_multi_union_pw_aff_union_add_aligned(
8281 __isl_take isl_multi_union_pw_aff
*mupa1
,
8282 __isl_take isl_multi_union_pw_aff
*mupa2
)
8284 isl_bool has_domain
, is_params1
, is_params2
;
8286 if (isl_multi_union_pw_aff_check_equal_space(mupa1
, mupa2
) < 0)
8289 return isl_multi_union_pw_aff_bin_op(mupa1
, mupa2
,
8290 &isl_union_pw_aff_union_add
);
8291 if (isl_multi_union_pw_aff_check_has_explicit_domain(mupa1
) < 0 ||
8292 isl_multi_union_pw_aff_check_has_explicit_domain(mupa2
) < 0)
8295 has_domain
= isl_multi_union_pw_aff_has_non_trivial_domain(mupa1
);
8299 isl_multi_union_pw_aff_free(mupa2
);
8302 has_domain
= isl_multi_union_pw_aff_has_non_trivial_domain(mupa2
);
8306 isl_multi_union_pw_aff_free(mupa1
);
8310 is_params1
= isl_union_set_is_params(mupa1
->u
.dom
);
8311 is_params2
= isl_union_set_is_params(mupa2
->u
.dom
);
8312 if (is_params1
< 0 || is_params2
< 0)
8314 if (is_params1
!= is_params2
)
8315 isl_die(isl_multi_union_pw_aff_get_ctx(mupa1
),
8317 "cannot compute union of concrete domain and "
8318 "parameter constraints", goto error
);
8319 mupa1
= isl_multi_union_pw_aff_cow(mupa1
);
8322 mupa1
->u
.dom
= isl_union_set_union(mupa1
->u
.dom
,
8323 isl_union_set_copy(mupa2
->u
.dom
));
8326 isl_multi_union_pw_aff_free(mupa2
);
8329 isl_multi_union_pw_aff_free(mupa1
);
8330 isl_multi_union_pw_aff_free(mupa2
);
8334 /* Compute the sum of "mupa1" and "mupa2" on the union of their domains,
8335 * with the actual sum on the shared domain and
8336 * the defined expression on the symmetric difference of the domains.
8338 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_union_add(
8339 __isl_take isl_multi_union_pw_aff
*mupa1
,
8340 __isl_take isl_multi_union_pw_aff
*mupa2
)
8342 return isl_multi_union_pw_aff_align_params_multi_multi_and(mupa1
, mupa2
,
8343 &isl_multi_union_pw_aff_union_add_aligned
);
8346 /* Construct and return a multi union piecewise affine expression
8347 * that is equal to the given multi affine expression.
8349 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_from_multi_aff(
8350 __isl_take isl_multi_aff
*ma
)
8352 isl_multi_pw_aff
*mpa
;
8354 mpa
= isl_multi_pw_aff_from_multi_aff(ma
);
8355 return isl_multi_union_pw_aff_from_multi_pw_aff(mpa
);
8358 /* Construct and return a multi union piecewise affine expression
8359 * that is equal to the given multi piecewise affine expression.
8361 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_from_multi_pw_aff(
8362 __isl_take isl_multi_pw_aff
*mpa
)
8366 isl_multi_union_pw_aff
*mupa
;
8371 space
= isl_multi_pw_aff_get_space(mpa
);
8372 space
= isl_space_range(space
);
8373 mupa
= isl_multi_union_pw_aff_alloc(space
);
8375 n
= isl_multi_pw_aff_dim(mpa
, isl_dim_out
);
8376 for (i
= 0; i
< n
; ++i
) {
8378 isl_union_pw_aff
*upa
;
8380 pa
= isl_multi_pw_aff_get_pw_aff(mpa
, i
);
8381 upa
= isl_union_pw_aff_from_pw_aff(pa
);
8382 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
8385 isl_multi_pw_aff_free(mpa
);
8390 /* Extract the range space of "pma" and assign it to *space.
8391 * If *space has already been set (through a previous call to this function),
8392 * then check that the range space is the same.
8394 static isl_stat
extract_space(__isl_take isl_pw_multi_aff
*pma
, void *user
)
8396 isl_space
**space
= user
;
8397 isl_space
*pma_space
;
8400 pma_space
= isl_space_range(isl_pw_multi_aff_get_space(pma
));
8401 isl_pw_multi_aff_free(pma
);
8404 return isl_stat_error
;
8410 equal
= isl_space_is_equal(pma_space
, *space
);
8411 isl_space_free(pma_space
);
8414 return isl_stat_error
;
8416 isl_die(isl_space_get_ctx(*space
), isl_error_invalid
,
8417 "range spaces not the same", return isl_stat_error
);
8421 /* Construct and return a multi union piecewise affine expression
8422 * that is equal to the given union piecewise multi affine expression.
8424 * In order to be able to perform the conversion, the input
8425 * needs to be non-empty and may only involve a single range space.
8427 * If the resulting multi union piecewise affine expression has
8428 * an explicit domain, then assign it the domain of the input.
8429 * In other cases, the domain is stored in the individual elements.
8431 __isl_give isl_multi_union_pw_aff
*
8432 isl_multi_union_pw_aff_from_union_pw_multi_aff(
8433 __isl_take isl_union_pw_multi_aff
*upma
)
8435 isl_space
*space
= NULL
;
8436 isl_multi_union_pw_aff
*mupa
;
8441 if (isl_union_pw_multi_aff_n_pw_multi_aff(upma
) == 0)
8442 isl_die(isl_union_pw_multi_aff_get_ctx(upma
), isl_error_invalid
,
8443 "cannot extract range space from empty input",
8445 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma
, &extract_space
,
8452 n
= isl_space_dim(space
, isl_dim_set
);
8453 mupa
= isl_multi_union_pw_aff_alloc(space
);
8455 for (i
= 0; i
< n
; ++i
) {
8456 isl_union_pw_aff
*upa
;
8458 upa
= isl_union_pw_multi_aff_get_union_pw_aff(upma
, i
);
8459 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
8461 if (isl_multi_union_pw_aff_has_explicit_domain(mupa
)) {
8463 isl_union_pw_multi_aff
*copy
;
8465 copy
= isl_union_pw_multi_aff_copy(upma
);
8466 dom
= isl_union_pw_multi_aff_domain(copy
);
8467 mupa
= isl_multi_union_pw_aff_intersect_domain(mupa
, dom
);
8470 isl_union_pw_multi_aff_free(upma
);
8473 isl_space_free(space
);
8474 isl_union_pw_multi_aff_free(upma
);
8478 /* Try and create an isl_multi_union_pw_aff that is equivalent
8479 * to the given isl_union_map.
8480 * The isl_union_map is required to be single-valued in each space.
8481 * Moreover, it cannot be empty and all range spaces need to be the same.
8482 * Otherwise, an error is produced.
8484 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_from_union_map(
8485 __isl_take isl_union_map
*umap
)
8487 isl_union_pw_multi_aff
*upma
;
8489 upma
= isl_union_pw_multi_aff_from_union_map(umap
);
8490 return isl_multi_union_pw_aff_from_union_pw_multi_aff(upma
);
8493 /* Return a multiple union piecewise affine expression
8494 * that is equal to "mv" on "domain", assuming "domain" and "mv"
8495 * have been aligned.
8497 * If the resulting multi union piecewise affine expression has
8498 * an explicit domain, then assign it the input domain.
8499 * In other cases, the domain is stored in the individual elements.
8501 static __isl_give isl_multi_union_pw_aff
*
8502 isl_multi_union_pw_aff_multi_val_on_domain_aligned(
8503 __isl_take isl_union_set
*domain
, __isl_take isl_multi_val
*mv
)
8507 isl_multi_union_pw_aff
*mupa
;
8512 n
= isl_multi_val_dim(mv
, isl_dim_set
);
8513 space
= isl_multi_val_get_space(mv
);
8514 mupa
= isl_multi_union_pw_aff_alloc(space
);
8515 for (i
= 0; i
< n
; ++i
) {
8517 isl_union_pw_aff
*upa
;
8519 v
= isl_multi_val_get_val(mv
, i
);
8520 upa
= isl_union_pw_aff_val_on_domain(isl_union_set_copy(domain
),
8522 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
8524 if (isl_multi_union_pw_aff_has_explicit_domain(mupa
))
8525 mupa
= isl_multi_union_pw_aff_intersect_domain(mupa
,
8526 isl_union_set_copy(domain
));
8528 isl_union_set_free(domain
);
8529 isl_multi_val_free(mv
);
8532 isl_union_set_free(domain
);
8533 isl_multi_val_free(mv
);
8537 /* Return a multiple union piecewise affine expression
8538 * that is equal to "mv" on "domain".
8540 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_multi_val_on_domain(
8541 __isl_take isl_union_set
*domain
, __isl_take isl_multi_val
*mv
)
8543 isl_bool equal_params
;
8547 equal_params
= isl_space_has_equal_params(domain
->dim
, mv
->space
);
8548 if (equal_params
< 0)
8551 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(
8553 domain
= isl_union_set_align_params(domain
,
8554 isl_multi_val_get_space(mv
));
8555 mv
= isl_multi_val_align_params(mv
, isl_union_set_get_space(domain
));
8556 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(domain
, mv
);
8558 isl_union_set_free(domain
);
8559 isl_multi_val_free(mv
);
8563 /* Return a multiple union piecewise affine expression
8564 * that is equal to "ma" on "domain".
8566 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_multi_aff_on_domain(
8567 __isl_take isl_union_set
*domain
, __isl_take isl_multi_aff
*ma
)
8569 isl_pw_multi_aff
*pma
;
8571 pma
= isl_pw_multi_aff_from_multi_aff(ma
);
8572 return isl_multi_union_pw_aff_pw_multi_aff_on_domain(domain
, pma
);
8575 /* Return a multiple union piecewise affine expression
8576 * that is equal to "pma" on "domain", assuming "domain" and "pma"
8577 * have been aligned.
8579 * If the resulting multi union piecewise affine expression has
8580 * an explicit domain, then assign it the input domain.
8581 * In other cases, the domain is stored in the individual elements.
8583 static __isl_give isl_multi_union_pw_aff
*
8584 isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(
8585 __isl_take isl_union_set
*domain
, __isl_take isl_pw_multi_aff
*pma
)
8589 isl_multi_union_pw_aff
*mupa
;
8591 if (!domain
|| !pma
)
8594 n
= isl_pw_multi_aff_dim(pma
, isl_dim_set
);
8595 space
= isl_pw_multi_aff_get_space(pma
);
8596 mupa
= isl_multi_union_pw_aff_alloc(space
);
8597 for (i
= 0; i
< n
; ++i
) {
8599 isl_union_pw_aff
*upa
;
8601 pa
= isl_pw_multi_aff_get_pw_aff(pma
, i
);
8602 upa
= isl_union_pw_aff_pw_aff_on_domain(
8603 isl_union_set_copy(domain
), pa
);
8604 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
8606 if (isl_multi_union_pw_aff_has_explicit_domain(mupa
))
8607 mupa
= isl_multi_union_pw_aff_intersect_domain(mupa
,
8608 isl_union_set_copy(domain
));
8610 isl_union_set_free(domain
);
8611 isl_pw_multi_aff_free(pma
);
8614 isl_union_set_free(domain
);
8615 isl_pw_multi_aff_free(pma
);
8619 /* Return a multiple union piecewise affine expression
8620 * that is equal to "pma" on "domain".
8622 __isl_give isl_multi_union_pw_aff
*
8623 isl_multi_union_pw_aff_pw_multi_aff_on_domain(__isl_take isl_union_set
*domain
,
8624 __isl_take isl_pw_multi_aff
*pma
)
8626 isl_bool equal_params
;
8629 space
= isl_pw_multi_aff_peek_space(pma
);
8630 equal_params
= isl_union_set_space_has_equal_params(domain
, space
);
8631 if (equal_params
< 0)
8634 return isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(
8636 domain
= isl_union_set_align_params(domain
,
8637 isl_pw_multi_aff_get_space(pma
));
8638 pma
= isl_pw_multi_aff_align_params(pma
,
8639 isl_union_set_get_space(domain
));
8640 return isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(domain
,
8643 isl_union_set_free(domain
);
8644 isl_pw_multi_aff_free(pma
);
8648 /* Return a union set containing those elements in the domains
8649 * of the elements of "mupa" where they are all zero.
8651 * If there are no elements, then simply return the entire domain.
8653 __isl_give isl_union_set
*isl_multi_union_pw_aff_zero_union_set(
8654 __isl_take isl_multi_union_pw_aff
*mupa
)
8657 isl_union_pw_aff
*upa
;
8658 isl_union_set
*zero
;
8663 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8665 return isl_multi_union_pw_aff_domain(mupa
);
8667 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, 0);
8668 zero
= isl_union_pw_aff_zero_union_set(upa
);
8670 for (i
= 1; i
< n
; ++i
) {
8671 isl_union_set
*zero_i
;
8673 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
8674 zero_i
= isl_union_pw_aff_zero_union_set(upa
);
8676 zero
= isl_union_set_intersect(zero
, zero_i
);
8679 isl_multi_union_pw_aff_free(mupa
);
8683 /* Construct a union map mapping the shared domain
8684 * of the union piecewise affine expressions to the range of "mupa"
8685 * in the special case of a 0D multi union piecewise affine expression.
8687 * Construct a map between the explicit domain of "mupa" and
8689 * Note that this assumes that the domain consists of explicit elements.
8691 static __isl_give isl_union_map
*isl_union_map_from_multi_union_pw_aff_0D(
8692 __isl_take isl_multi_union_pw_aff
*mupa
)
8696 isl_union_set
*dom
, *ran
;
8698 space
= isl_multi_union_pw_aff_get_space(mupa
);
8699 dom
= isl_multi_union_pw_aff_domain(mupa
);
8700 ran
= isl_union_set_from_set(isl_set_universe(space
));
8702 is_params
= isl_union_set_is_params(dom
);
8704 dom
= isl_union_set_free(dom
);
8706 isl_die(isl_union_set_get_ctx(dom
), isl_error_invalid
,
8707 "cannot create union map from expression without "
8708 "explicit domain elements",
8709 dom
= isl_union_set_free(dom
));
8711 return isl_union_map_from_domain_and_range(dom
, ran
);
8714 /* Construct a union map mapping the shared domain
8715 * of the union piecewise affine expressions to the range of "mupa"
8716 * with each dimension in the range equated to the
8717 * corresponding union piecewise affine expression.
8719 * If the input is zero-dimensional, then construct a mapping
8720 * from its explicit domain.
8722 __isl_give isl_union_map
*isl_union_map_from_multi_union_pw_aff(
8723 __isl_take isl_multi_union_pw_aff
*mupa
)
8727 isl_union_map
*umap
;
8728 isl_union_pw_aff
*upa
;
8733 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8735 return isl_union_map_from_multi_union_pw_aff_0D(mupa
);
8737 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, 0);
8738 umap
= isl_union_map_from_union_pw_aff(upa
);
8740 for (i
= 1; i
< n
; ++i
) {
8741 isl_union_map
*umap_i
;
8743 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
8744 umap_i
= isl_union_map_from_union_pw_aff(upa
);
8745 umap
= isl_union_map_flat_range_product(umap
, umap_i
);
8748 space
= isl_multi_union_pw_aff_get_space(mupa
);
8749 umap
= isl_union_map_reset_range_space(umap
, space
);
8751 isl_multi_union_pw_aff_free(mupa
);
8755 /* Internal data structure for isl_union_pw_multi_aff_reset_range_space.
8756 * "range" is the space from which to set the range space.
8757 * "res" collects the results.
8759 struct isl_union_pw_multi_aff_reset_range_space_data
{
8761 isl_union_pw_multi_aff
*res
;
8764 /* Replace the range space of "pma" by the range space of data->range and
8765 * add the result to data->res.
8767 static isl_stat
reset_range_space(__isl_take isl_pw_multi_aff
*pma
, void *user
)
8769 struct isl_union_pw_multi_aff_reset_range_space_data
*data
= user
;
8772 space
= isl_pw_multi_aff_get_space(pma
);
8773 space
= isl_space_domain(space
);
8774 space
= isl_space_extend_domain_with_range(space
,
8775 isl_space_copy(data
->range
));
8776 pma
= isl_pw_multi_aff_reset_space(pma
, space
);
8777 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma
);
8779 return data
->res
? isl_stat_ok
: isl_stat_error
;
8782 /* Replace the range space of all the piecewise affine expressions in "upma" by
8783 * the range space of "space".
8785 * This assumes that all these expressions have the same output dimension.
8787 * Since the spaces of the expressions change, so do their hash values.
8788 * We therefore need to create a new isl_union_pw_multi_aff.
8789 * Note that the hash value is currently computed based on the entire
8790 * space even though there can only be a single expression with a given
8793 static __isl_give isl_union_pw_multi_aff
*
8794 isl_union_pw_multi_aff_reset_range_space(
8795 __isl_take isl_union_pw_multi_aff
*upma
, __isl_take isl_space
*space
)
8797 struct isl_union_pw_multi_aff_reset_range_space_data data
= { space
};
8798 isl_space
*space_upma
;
8800 space_upma
= isl_union_pw_multi_aff_get_space(upma
);
8801 data
.res
= isl_union_pw_multi_aff_empty(space_upma
);
8802 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma
,
8803 &reset_range_space
, &data
) < 0)
8804 data
.res
= isl_union_pw_multi_aff_free(data
.res
);
8806 isl_space_free(space
);
8807 isl_union_pw_multi_aff_free(upma
);
8811 /* Construct and return a union piecewise multi affine expression
8812 * that is equal to the given multi union piecewise affine expression,
8813 * in the special case of a 0D multi union piecewise affine expression.
8815 * Construct a union piecewise multi affine expression
8816 * on top of the explicit domain of the input.
8818 __isl_give isl_union_pw_multi_aff
*
8819 isl_union_pw_multi_aff_from_multi_union_pw_aff_0D(
8820 __isl_take isl_multi_union_pw_aff
*mupa
)
8824 isl_union_set
*domain
;
8826 space
= isl_multi_union_pw_aff_get_space(mupa
);
8827 mv
= isl_multi_val_zero(space
);
8828 domain
= isl_multi_union_pw_aff_domain(mupa
);
8829 return isl_union_pw_multi_aff_multi_val_on_domain(domain
, mv
);
8832 /* Construct and return a union piecewise multi affine expression
8833 * that is equal to the given multi union piecewise affine expression.
8835 * If the input is zero-dimensional, then
8836 * construct a union piecewise multi affine expression
8837 * on top of the explicit domain of the input.
8839 __isl_give isl_union_pw_multi_aff
*
8840 isl_union_pw_multi_aff_from_multi_union_pw_aff(
8841 __isl_take isl_multi_union_pw_aff
*mupa
)
8845 isl_union_pw_multi_aff
*upma
;
8846 isl_union_pw_aff
*upa
;
8851 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8853 return isl_union_pw_multi_aff_from_multi_union_pw_aff_0D(mupa
);
8855 space
= isl_multi_union_pw_aff_get_space(mupa
);
8856 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, 0);
8857 upma
= isl_union_pw_multi_aff_from_union_pw_aff(upa
);
8859 for (i
= 1; i
< n
; ++i
) {
8860 isl_union_pw_multi_aff
*upma_i
;
8862 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
8863 upma_i
= isl_union_pw_multi_aff_from_union_pw_aff(upa
);
8864 upma
= isl_union_pw_multi_aff_flat_range_product(upma
, upma_i
);
8867 upma
= isl_union_pw_multi_aff_reset_range_space(upma
, space
);
8869 isl_multi_union_pw_aff_free(mupa
);
8873 /* Intersect the range of "mupa" with "range",
8874 * in the special case where "mupa" is 0D.
8876 * Intersect the domain of "mupa" with the constraints on the parameters
8879 static __isl_give isl_multi_union_pw_aff
*mupa_intersect_range_0D(
8880 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_set
*range
)
8882 range
= isl_set_params(range
);
8883 mupa
= isl_multi_union_pw_aff_intersect_params(mupa
, range
);
8887 /* Intersect the range of "mupa" with "range".
8888 * That is, keep only those domain elements that have a function value
8891 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_intersect_range(
8892 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_set
*range
)
8894 isl_union_pw_multi_aff
*upma
;
8895 isl_union_set
*domain
;
8900 if (!mupa
|| !range
)
8903 space
= isl_set_get_space(range
);
8904 match
= isl_space_tuple_is_equal(mupa
->space
, isl_dim_set
,
8905 space
, isl_dim_set
);
8906 isl_space_free(space
);
8910 isl_die(isl_multi_union_pw_aff_get_ctx(mupa
), isl_error_invalid
,
8911 "space don't match", goto error
);
8912 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8914 return mupa_intersect_range_0D(mupa
, range
);
8916 upma
= isl_union_pw_multi_aff_from_multi_union_pw_aff(
8917 isl_multi_union_pw_aff_copy(mupa
));
8918 domain
= isl_union_set_from_set(range
);
8919 domain
= isl_union_set_preimage_union_pw_multi_aff(domain
, upma
);
8920 mupa
= isl_multi_union_pw_aff_intersect_domain(mupa
, domain
);
8924 isl_multi_union_pw_aff_free(mupa
);
8925 isl_set_free(range
);
8929 /* Return the shared domain of the elements of "mupa",
8930 * in the special case where "mupa" is zero-dimensional.
8932 * Return the explicit domain of "mupa".
8933 * Note that this domain may be a parameter set, either
8934 * because "mupa" is meant to live in a set space or
8935 * because no explicit domain has been set.
8937 __isl_give isl_union_set
*isl_multi_union_pw_aff_domain_0D(
8938 __isl_take isl_multi_union_pw_aff
*mupa
)
8942 dom
= isl_multi_union_pw_aff_get_explicit_domain(mupa
);
8943 isl_multi_union_pw_aff_free(mupa
);
8948 /* Return the shared domain of the elements of "mupa".
8950 * If "mupa" is zero-dimensional, then return its explicit domain.
8952 __isl_give isl_union_set
*isl_multi_union_pw_aff_domain(
8953 __isl_take isl_multi_union_pw_aff
*mupa
)
8956 isl_union_pw_aff
*upa
;
8962 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8964 return isl_multi_union_pw_aff_domain_0D(mupa
);
8966 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, 0);
8967 dom
= isl_union_pw_aff_domain(upa
);
8968 for (i
= 1; i
< n
; ++i
) {
8969 isl_union_set
*dom_i
;
8971 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
8972 dom_i
= isl_union_pw_aff_domain(upa
);
8973 dom
= isl_union_set_intersect(dom
, dom_i
);
8976 isl_multi_union_pw_aff_free(mupa
);
8980 /* Apply "aff" to "mupa". The space of "mupa" is equal to the domain of "aff".
8981 * In particular, the spaces have been aligned.
8982 * The result is defined over the shared domain of the elements of "mupa"
8984 * We first extract the parametric constant part of "aff" and
8985 * define that over the shared domain.
8986 * Then we iterate over all input dimensions of "aff" and add the corresponding
8987 * multiples of the elements of "mupa".
8988 * Finally, we consider the integer divisions, calling the function
8989 * recursively to obtain an isl_union_pw_aff corresponding to the
8990 * integer division argument.
8992 static __isl_give isl_union_pw_aff
*multi_union_pw_aff_apply_aff(
8993 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_aff
*aff
)
8996 isl_union_pw_aff
*upa
;
8997 isl_union_set
*uset
;
9001 n_in
= isl_aff_dim(aff
, isl_dim_in
);
9002 n_div
= isl_aff_dim(aff
, isl_dim_div
);
9004 uset
= isl_multi_union_pw_aff_domain(isl_multi_union_pw_aff_copy(mupa
));
9005 cst
= isl_aff_copy(aff
);
9006 cst
= isl_aff_drop_dims(cst
, isl_dim_div
, 0, n_div
);
9007 cst
= isl_aff_drop_dims(cst
, isl_dim_in
, 0, n_in
);
9008 cst
= isl_aff_project_domain_on_params(cst
);
9009 upa
= isl_union_pw_aff_aff_on_domain(uset
, cst
);
9011 for (i
= 0; i
< n_in
; ++i
) {
9012 isl_union_pw_aff
*upa_i
;
9014 if (!isl_aff_involves_dims(aff
, isl_dim_in
, i
, 1))
9016 v
= isl_aff_get_coefficient_val(aff
, isl_dim_in
, i
);
9017 upa_i
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
9018 upa_i
= isl_union_pw_aff_scale_val(upa_i
, v
);
9019 upa
= isl_union_pw_aff_add(upa
, upa_i
);
9022 for (i
= 0; i
< n_div
; ++i
) {
9024 isl_union_pw_aff
*upa_i
;
9026 if (!isl_aff_involves_dims(aff
, isl_dim_div
, i
, 1))
9028 div
= isl_aff_get_div(aff
, i
);
9029 upa_i
= multi_union_pw_aff_apply_aff(
9030 isl_multi_union_pw_aff_copy(mupa
), div
);
9031 upa_i
= isl_union_pw_aff_floor(upa_i
);
9032 v
= isl_aff_get_coefficient_val(aff
, isl_dim_div
, i
);
9033 upa_i
= isl_union_pw_aff_scale_val(upa_i
, v
);
9034 upa
= isl_union_pw_aff_add(upa
, upa_i
);
9037 isl_multi_union_pw_aff_free(mupa
);
9043 /* Apply "aff" to "mupa". The space of "mupa" needs to be compatible
9044 * with the domain of "aff".
9045 * Furthermore, the dimension of this space needs to be greater than zero.
9046 * The result is defined over the shared domain of the elements of "mupa"
9048 * We perform these checks and then hand over control to
9049 * multi_union_pw_aff_apply_aff.
9051 __isl_give isl_union_pw_aff
*isl_multi_union_pw_aff_apply_aff(
9052 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_aff
*aff
)
9054 isl_space
*space1
, *space2
;
9057 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
9058 isl_aff_get_space(aff
));
9059 aff
= isl_aff_align_params(aff
, isl_multi_union_pw_aff_get_space(mupa
));
9063 space1
= isl_multi_union_pw_aff_get_space(mupa
);
9064 space2
= isl_aff_get_domain_space(aff
);
9065 equal
= isl_space_is_equal(space1
, space2
);
9066 isl_space_free(space1
);
9067 isl_space_free(space2
);
9071 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
9072 "spaces don't match", goto error
);
9073 if (isl_aff_dim(aff
, isl_dim_in
) == 0)
9074 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
9075 "cannot determine domains", goto error
);
9077 return multi_union_pw_aff_apply_aff(mupa
, aff
);
9079 isl_multi_union_pw_aff_free(mupa
);
9084 /* Apply "ma" to "mupa", in the special case where "mupa" is 0D.
9085 * The space of "mupa" is known to be compatible with the domain of "ma".
9087 * Construct an isl_multi_union_pw_aff that is equal to "ma"
9088 * on the domain of "mupa".
9090 static __isl_give isl_multi_union_pw_aff
*mupa_apply_multi_aff_0D(
9091 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_multi_aff
*ma
)
9095 dom
= isl_multi_union_pw_aff_domain(mupa
);
9096 ma
= isl_multi_aff_project_domain_on_params(ma
);
9098 return isl_multi_union_pw_aff_multi_aff_on_domain(dom
, ma
);
9101 /* Apply "ma" to "mupa". The space of "mupa" needs to be compatible
9102 * with the domain of "ma".
9103 * The result is defined over the shared domain of the elements of "mupa"
9105 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_apply_multi_aff(
9106 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_multi_aff
*ma
)
9108 isl_space
*space1
, *space2
;
9109 isl_multi_union_pw_aff
*res
;
9113 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
9114 isl_multi_aff_get_space(ma
));
9115 ma
= isl_multi_aff_align_params(ma
,
9116 isl_multi_union_pw_aff_get_space(mupa
));
9120 space1
= isl_multi_union_pw_aff_get_space(mupa
);
9121 space2
= isl_multi_aff_get_domain_space(ma
);
9122 equal
= isl_space_is_equal(space1
, space2
);
9123 isl_space_free(space1
);
9124 isl_space_free(space2
);
9128 isl_die(isl_multi_aff_get_ctx(ma
), isl_error_invalid
,
9129 "spaces don't match", goto error
);
9130 n_out
= isl_multi_aff_dim(ma
, isl_dim_out
);
9131 if (isl_multi_aff_dim(ma
, isl_dim_in
) == 0)
9132 return mupa_apply_multi_aff_0D(mupa
, ma
);
9134 space1
= isl_space_range(isl_multi_aff_get_space(ma
));
9135 res
= isl_multi_union_pw_aff_alloc(space1
);
9137 for (i
= 0; i
< n_out
; ++i
) {
9139 isl_union_pw_aff
*upa
;
9141 aff
= isl_multi_aff_get_aff(ma
, i
);
9142 upa
= multi_union_pw_aff_apply_aff(
9143 isl_multi_union_pw_aff_copy(mupa
), aff
);
9144 res
= isl_multi_union_pw_aff_set_union_pw_aff(res
, i
, upa
);
9147 isl_multi_aff_free(ma
);
9148 isl_multi_union_pw_aff_free(mupa
);
9151 isl_multi_union_pw_aff_free(mupa
);
9152 isl_multi_aff_free(ma
);
9156 /* Apply "pa" to "mupa", in the special case where "mupa" is 0D.
9157 * The space of "mupa" is known to be compatible with the domain of "pa".
9159 * Construct an isl_multi_union_pw_aff that is equal to "pa"
9160 * on the domain of "mupa".
9162 static __isl_give isl_union_pw_aff
*isl_multi_union_pw_aff_apply_pw_aff_0D(
9163 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_pw_aff
*pa
)
9167 dom
= isl_multi_union_pw_aff_domain(mupa
);
9168 pa
= isl_pw_aff_project_domain_on_params(pa
);
9170 return isl_union_pw_aff_pw_aff_on_domain(dom
, pa
);
9173 /* Apply "pa" to "mupa". The space of "mupa" needs to be compatible
9174 * with the domain of "pa".
9175 * Furthermore, the dimension of this space needs to be greater than zero.
9176 * The result is defined over the shared domain of the elements of "mupa"
9178 __isl_give isl_union_pw_aff
*isl_multi_union_pw_aff_apply_pw_aff(
9179 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_pw_aff
*pa
)
9183 isl_space
*space
, *space2
;
9184 isl_union_pw_aff
*upa
;
9186 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
9187 isl_pw_aff_get_space(pa
));
9188 pa
= isl_pw_aff_align_params(pa
,
9189 isl_multi_union_pw_aff_get_space(mupa
));
9193 space
= isl_multi_union_pw_aff_get_space(mupa
);
9194 space2
= isl_pw_aff_get_domain_space(pa
);
9195 equal
= isl_space_is_equal(space
, space2
);
9196 isl_space_free(space
);
9197 isl_space_free(space2
);
9201 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
9202 "spaces don't match", goto error
);
9203 if (isl_pw_aff_dim(pa
, isl_dim_in
) == 0)
9204 return isl_multi_union_pw_aff_apply_pw_aff_0D(mupa
, pa
);
9206 space
= isl_space_params(isl_multi_union_pw_aff_get_space(mupa
));
9207 upa
= isl_union_pw_aff_empty(space
);
9209 for (i
= 0; i
< pa
->n
; ++i
) {
9212 isl_multi_union_pw_aff
*mupa_i
;
9213 isl_union_pw_aff
*upa_i
;
9215 mupa_i
= isl_multi_union_pw_aff_copy(mupa
);
9216 domain
= isl_set_copy(pa
->p
[i
].set
);
9217 mupa_i
= isl_multi_union_pw_aff_intersect_range(mupa_i
, domain
);
9218 aff
= isl_aff_copy(pa
->p
[i
].aff
);
9219 upa_i
= multi_union_pw_aff_apply_aff(mupa_i
, aff
);
9220 upa
= isl_union_pw_aff_union_add(upa
, upa_i
);
9223 isl_multi_union_pw_aff_free(mupa
);
9224 isl_pw_aff_free(pa
);
9227 isl_multi_union_pw_aff_free(mupa
);
9228 isl_pw_aff_free(pa
);
9232 /* Apply "pma" to "mupa", in the special case where "mupa" is 0D.
9233 * The space of "mupa" is known to be compatible with the domain of "pma".
9235 * Construct an isl_multi_union_pw_aff that is equal to "pma"
9236 * on the domain of "mupa".
9238 static __isl_give isl_multi_union_pw_aff
*mupa_apply_pw_multi_aff_0D(
9239 __isl_take isl_multi_union_pw_aff
*mupa
,
9240 __isl_take isl_pw_multi_aff
*pma
)
9244 dom
= isl_multi_union_pw_aff_domain(mupa
);
9245 pma
= isl_pw_multi_aff_project_domain_on_params(pma
);
9247 return isl_multi_union_pw_aff_pw_multi_aff_on_domain(dom
, pma
);
9250 /* Apply "pma" to "mupa". The space of "mupa" needs to be compatible
9251 * with the domain of "pma".
9252 * The result is defined over the shared domain of the elements of "mupa"
9254 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_apply_pw_multi_aff(
9255 __isl_take isl_multi_union_pw_aff
*mupa
,
9256 __isl_take isl_pw_multi_aff
*pma
)
9258 isl_space
*space1
, *space2
;
9259 isl_multi_union_pw_aff
*res
;
9263 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
9264 isl_pw_multi_aff_get_space(pma
));
9265 pma
= isl_pw_multi_aff_align_params(pma
,
9266 isl_multi_union_pw_aff_get_space(mupa
));
9270 space1
= isl_multi_union_pw_aff_get_space(mupa
);
9271 space2
= isl_pw_multi_aff_get_domain_space(pma
);
9272 equal
= isl_space_is_equal(space1
, space2
);
9273 isl_space_free(space1
);
9274 isl_space_free(space2
);
9278 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
9279 "spaces don't match", goto error
);
9280 n_out
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
9281 if (isl_pw_multi_aff_dim(pma
, isl_dim_in
) == 0)
9282 return mupa_apply_pw_multi_aff_0D(mupa
, pma
);
9284 space1
= isl_space_range(isl_pw_multi_aff_get_space(pma
));
9285 res
= isl_multi_union_pw_aff_alloc(space1
);
9287 for (i
= 0; i
< n_out
; ++i
) {
9289 isl_union_pw_aff
*upa
;
9291 pa
= isl_pw_multi_aff_get_pw_aff(pma
, i
);
9292 upa
= isl_multi_union_pw_aff_apply_pw_aff(
9293 isl_multi_union_pw_aff_copy(mupa
), pa
);
9294 res
= isl_multi_union_pw_aff_set_union_pw_aff(res
, i
, upa
);
9297 isl_pw_multi_aff_free(pma
);
9298 isl_multi_union_pw_aff_free(mupa
);
9301 isl_multi_union_pw_aff_free(mupa
);
9302 isl_pw_multi_aff_free(pma
);
9306 /* Replace the explicit domain of "mupa" by its preimage under "upma".
9307 * If the explicit domain only keeps track of constraints on the parameters,
9308 * then only update those constraints.
9310 static __isl_give isl_multi_union_pw_aff
*preimage_explicit_domain(
9311 __isl_take isl_multi_union_pw_aff
*mupa
,
9312 __isl_keep isl_union_pw_multi_aff
*upma
)
9316 if (isl_multi_union_pw_aff_check_has_explicit_domain(mupa
) < 0)
9317 return isl_multi_union_pw_aff_free(mupa
);
9319 mupa
= isl_multi_union_pw_aff_cow(mupa
);
9323 is_params
= isl_union_set_is_params(mupa
->u
.dom
);
9325 return isl_multi_union_pw_aff_free(mupa
);
9327 upma
= isl_union_pw_multi_aff_copy(upma
);
9329 mupa
->u
.dom
= isl_union_set_intersect_params(mupa
->u
.dom
,
9330 isl_union_set_params(isl_union_pw_multi_aff_domain(upma
)));
9332 mupa
->u
.dom
= isl_union_set_preimage_union_pw_multi_aff(
9335 return isl_multi_union_pw_aff_free(mupa
);
9339 /* Compute the pullback of "mupa" by the function represented by "upma".
9340 * In other words, plug in "upma" in "mupa". The result contains
9341 * expressions defined over the domain space of "upma".
9343 * Run over all elements of "mupa" and plug in "upma" in each of them.
9345 * If "mupa" has an explicit domain, then it is this domain
9346 * that needs to undergo a pullback instead, i.e., a preimage.
9348 __isl_give isl_multi_union_pw_aff
*
9349 isl_multi_union_pw_aff_pullback_union_pw_multi_aff(
9350 __isl_take isl_multi_union_pw_aff
*mupa
,
9351 __isl_take isl_union_pw_multi_aff
*upma
)
9355 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
9356 isl_union_pw_multi_aff_get_space(upma
));
9357 upma
= isl_union_pw_multi_aff_align_params(upma
,
9358 isl_multi_union_pw_aff_get_space(mupa
));
9359 mupa
= isl_multi_union_pw_aff_cow(mupa
);
9363 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
9364 for (i
= 0; i
< n
; ++i
) {
9365 isl_union_pw_aff
*upa
;
9367 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
9368 upa
= isl_union_pw_aff_pullback_union_pw_multi_aff(upa
,
9369 isl_union_pw_multi_aff_copy(upma
));
9370 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
9373 if (isl_multi_union_pw_aff_has_explicit_domain(mupa
))
9374 mupa
= preimage_explicit_domain(mupa
, upma
);
9376 isl_union_pw_multi_aff_free(upma
);
9379 isl_multi_union_pw_aff_free(mupa
);
9380 isl_union_pw_multi_aff_free(upma
);
9384 /* Extract the sequence of elements in "mupa" with domain space "space"
9385 * (ignoring parameters).
9387 * For the elements of "mupa" that are not defined on the specified space,
9388 * the corresponding element in the result is empty.
9390 __isl_give isl_multi_pw_aff
*isl_multi_union_pw_aff_extract_multi_pw_aff(
9391 __isl_keep isl_multi_union_pw_aff
*mupa
, __isl_take isl_space
*space
)
9394 isl_space
*space_mpa
;
9395 isl_multi_pw_aff
*mpa
;
9397 if (!mupa
|| !space
)
9400 space_mpa
= isl_multi_union_pw_aff_get_space(mupa
);
9401 space
= isl_space_replace_params(space
, space_mpa
);
9402 space_mpa
= isl_space_map_from_domain_and_range(isl_space_copy(space
),
9404 mpa
= isl_multi_pw_aff_alloc(space_mpa
);
9406 space
= isl_space_from_domain(space
);
9407 space
= isl_space_add_dims(space
, isl_dim_out
, 1);
9408 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
9409 for (i
= 0; i
< n
; ++i
) {
9410 isl_union_pw_aff
*upa
;
9413 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
9414 pa
= isl_union_pw_aff_extract_pw_aff(upa
,
9415 isl_space_copy(space
));
9416 mpa
= isl_multi_pw_aff_set_pw_aff(mpa
, i
, pa
);
9417 isl_union_pw_aff_free(upa
);
9420 isl_space_free(space
);
9423 isl_space_free(space
);
9427 /* Evaluate the affine function "aff" in the void point "pnt".
9428 * In particular, return the value NaN.
9430 static __isl_give isl_val
*eval_void(__isl_take isl_aff
*aff
,
9431 __isl_take isl_point
*pnt
)
9435 ctx
= isl_point_get_ctx(pnt
);
9437 isl_point_free(pnt
);
9438 return isl_val_nan(ctx
);
9441 /* Evaluate the affine expression "aff"
9442 * in the coordinates (with denominator) "pnt".
9444 static __isl_give isl_val
*eval(__isl_keep isl_vec
*aff
,
9445 __isl_keep isl_vec
*pnt
)
9454 ctx
= isl_vec_get_ctx(aff
);
9457 isl_seq_inner_product(aff
->el
+ 1, pnt
->el
, pnt
->size
, &n
);
9458 isl_int_mul(d
, aff
->el
[0], pnt
->el
[0]);
9459 v
= isl_val_rat_from_isl_int(ctx
, n
, d
);
9460 v
= isl_val_normalize(v
);
9467 /* Check that the domain space of "aff" is equal to "space".
9469 static isl_stat
isl_aff_check_has_domain_space(__isl_keep isl_aff
*aff
,
9470 __isl_keep isl_space
*space
)
9474 ok
= isl_space_is_equal(isl_aff_peek_domain_space(aff
), space
);
9476 return isl_stat_error
;
9478 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
9479 "incompatible spaces", return isl_stat_error
);
9483 /* Evaluate the affine function "aff" in "pnt".
9485 __isl_give isl_val
*isl_aff_eval(__isl_take isl_aff
*aff
,
9486 __isl_take isl_point
*pnt
)
9490 isl_local_space
*ls
;
9492 if (isl_aff_check_has_domain_space(aff
, isl_point_peek_space(pnt
)) < 0)
9494 is_void
= isl_point_is_void(pnt
);
9498 return eval_void(aff
, pnt
);
9500 ls
= isl_aff_get_domain_local_space(aff
);
9501 pnt
= isl_local_space_lift_point(ls
, pnt
);
9503 v
= eval(aff
->v
, isl_point_peek_vec(pnt
));
9506 isl_point_free(pnt
);
9511 isl_point_free(pnt
);