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>
2699 #define UNION isl_union_pw_aff
2701 #define PART isl_pw_aff
2703 #define PARTS pw_aff
2705 #include <isl_union_single.c>
2706 #include <isl_union_neg.c>
2708 static __isl_give isl_set
*align_params_pw_pw_set_and(
2709 __isl_take isl_pw_aff
*pwaff1
, __isl_take isl_pw_aff
*pwaff2
,
2710 __isl_give isl_set
*(*fn
)(__isl_take isl_pw_aff
*pwaff1
,
2711 __isl_take isl_pw_aff
*pwaff2
))
2713 isl_bool equal_params
;
2715 if (!pwaff1
|| !pwaff2
)
2717 equal_params
= isl_space_has_equal_params(pwaff1
->dim
, pwaff2
->dim
);
2718 if (equal_params
< 0)
2721 return fn(pwaff1
, pwaff2
);
2722 if (isl_pw_aff_check_named_params(pwaff1
) < 0 ||
2723 isl_pw_aff_check_named_params(pwaff2
) < 0)
2725 pwaff1
= isl_pw_aff_align_params(pwaff1
, isl_pw_aff_get_space(pwaff2
));
2726 pwaff2
= isl_pw_aff_align_params(pwaff2
, isl_pw_aff_get_space(pwaff1
));
2727 return fn(pwaff1
, pwaff2
);
2729 isl_pw_aff_free(pwaff1
);
2730 isl_pw_aff_free(pwaff2
);
2734 /* Align the parameters of the to isl_pw_aff arguments and
2735 * then apply a function "fn" on them that returns an isl_map.
2737 static __isl_give isl_map
*align_params_pw_pw_map_and(
2738 __isl_take isl_pw_aff
*pa1
, __isl_take isl_pw_aff
*pa2
,
2739 __isl_give isl_map
*(*fn
)(__isl_take isl_pw_aff
*pa1
,
2740 __isl_take isl_pw_aff
*pa2
))
2742 isl_bool equal_params
;
2746 equal_params
= isl_space_has_equal_params(pa1
->dim
, pa2
->dim
);
2747 if (equal_params
< 0)
2750 return fn(pa1
, pa2
);
2751 if (isl_pw_aff_check_named_params(pa1
) < 0 ||
2752 isl_pw_aff_check_named_params(pa2
) < 0)
2754 pa1
= isl_pw_aff_align_params(pa1
, isl_pw_aff_get_space(pa2
));
2755 pa2
= isl_pw_aff_align_params(pa2
, isl_pw_aff_get_space(pa1
));
2756 return fn(pa1
, pa2
);
2758 isl_pw_aff_free(pa1
);
2759 isl_pw_aff_free(pa2
);
2763 /* Compute a piecewise quasi-affine expression with a domain that
2764 * is the union of those of pwaff1 and pwaff2 and such that on each
2765 * cell, the quasi-affine expression is the maximum of those of pwaff1
2766 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2767 * cell, then the associated expression is the defined one.
2769 static __isl_give isl_pw_aff
*pw_aff_union_max(__isl_take isl_pw_aff
*pwaff1
,
2770 __isl_take isl_pw_aff
*pwaff2
)
2772 return isl_pw_aff_union_opt_cmp(pwaff1
, pwaff2
, &isl_aff_ge_set
);
2775 __isl_give isl_pw_aff
*isl_pw_aff_union_max(__isl_take isl_pw_aff
*pwaff1
,
2776 __isl_take isl_pw_aff
*pwaff2
)
2778 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
,
2782 /* Compute a piecewise quasi-affine expression with a domain that
2783 * is the union of those of pwaff1 and pwaff2 and such that on each
2784 * cell, the quasi-affine expression is the minimum of those of pwaff1
2785 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2786 * cell, then the associated expression is the defined one.
2788 static __isl_give isl_pw_aff
*pw_aff_union_min(__isl_take isl_pw_aff
*pwaff1
,
2789 __isl_take isl_pw_aff
*pwaff2
)
2791 return isl_pw_aff_union_opt_cmp(pwaff1
, pwaff2
, &isl_aff_le_set
);
2794 __isl_give isl_pw_aff
*isl_pw_aff_union_min(__isl_take isl_pw_aff
*pwaff1
,
2795 __isl_take isl_pw_aff
*pwaff2
)
2797 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
,
2801 __isl_give isl_pw_aff
*isl_pw_aff_union_opt(__isl_take isl_pw_aff
*pwaff1
,
2802 __isl_take isl_pw_aff
*pwaff2
, int max
)
2805 return isl_pw_aff_union_max(pwaff1
, pwaff2
);
2807 return isl_pw_aff_union_min(pwaff1
, pwaff2
);
2810 /* Construct a map with as domain the domain of pwaff and
2811 * one-dimensional range corresponding to the affine expressions.
2813 static __isl_give isl_map
*map_from_pw_aff(__isl_take isl_pw_aff
*pwaff
)
2822 dim
= isl_pw_aff_get_space(pwaff
);
2823 map
= isl_map_empty(dim
);
2825 for (i
= 0; i
< pwaff
->n
; ++i
) {
2826 isl_basic_map
*bmap
;
2829 bmap
= isl_basic_map_from_aff(isl_aff_copy(pwaff
->p
[i
].aff
));
2830 map_i
= isl_map_from_basic_map(bmap
);
2831 map_i
= isl_map_intersect_domain(map_i
,
2832 isl_set_copy(pwaff
->p
[i
].set
));
2833 map
= isl_map_union_disjoint(map
, map_i
);
2836 isl_pw_aff_free(pwaff
);
2841 /* Construct a map with as domain the domain of pwaff and
2842 * one-dimensional range corresponding to the affine expressions.
2844 __isl_give isl_map
*isl_map_from_pw_aff(__isl_take isl_pw_aff
*pwaff
)
2848 if (isl_space_is_set(pwaff
->dim
))
2849 isl_die(isl_pw_aff_get_ctx(pwaff
), isl_error_invalid
,
2850 "space of input is not a map", goto error
);
2851 return map_from_pw_aff(pwaff
);
2853 isl_pw_aff_free(pwaff
);
2857 /* Construct a one-dimensional set with as parameter domain
2858 * the domain of pwaff and the single set dimension
2859 * corresponding to the affine expressions.
2861 __isl_give isl_set
*isl_set_from_pw_aff(__isl_take isl_pw_aff
*pwaff
)
2865 if (!isl_space_is_set(pwaff
->dim
))
2866 isl_die(isl_pw_aff_get_ctx(pwaff
), isl_error_invalid
,
2867 "space of input is not a set", goto error
);
2868 return map_from_pw_aff(pwaff
);
2870 isl_pw_aff_free(pwaff
);
2874 /* Return a set containing those elements in the domain
2875 * of "pwaff" where it satisfies "fn" (if complement is 0) or
2876 * does not satisfy "fn" (if complement is 1).
2878 * The pieces with a NaN never belong to the result since
2879 * NaN does not satisfy any property.
2881 static __isl_give isl_set
*pw_aff_locus(__isl_take isl_pw_aff
*pwaff
,
2882 __isl_give isl_basic_set
*(*fn
)(__isl_take isl_aff
*aff
, int rational
),
2891 set
= isl_set_empty(isl_pw_aff_get_domain_space(pwaff
));
2893 for (i
= 0; i
< pwaff
->n
; ++i
) {
2894 isl_basic_set
*bset
;
2895 isl_set
*set_i
, *locus
;
2898 if (isl_aff_is_nan(pwaff
->p
[i
].aff
))
2901 rational
= isl_set_has_rational(pwaff
->p
[i
].set
);
2902 bset
= fn(isl_aff_copy(pwaff
->p
[i
].aff
), rational
);
2903 locus
= isl_set_from_basic_set(bset
);
2904 set_i
= isl_set_copy(pwaff
->p
[i
].set
);
2906 set_i
= isl_set_subtract(set_i
, locus
);
2908 set_i
= isl_set_intersect(set_i
, locus
);
2909 set
= isl_set_union_disjoint(set
, set_i
);
2912 isl_pw_aff_free(pwaff
);
2917 /* Return a set containing those elements in the domain
2918 * of "pa" where it is positive.
2920 __isl_give isl_set
*isl_pw_aff_pos_set(__isl_take isl_pw_aff
*pa
)
2922 return pw_aff_locus(pa
, &aff_pos_basic_set
, 0);
2925 /* Return a set containing those elements in the domain
2926 * of pwaff where it is non-negative.
2928 __isl_give isl_set
*isl_pw_aff_nonneg_set(__isl_take isl_pw_aff
*pwaff
)
2930 return pw_aff_locus(pwaff
, &aff_nonneg_basic_set
, 0);
2933 /* Return a set containing those elements in the domain
2934 * of pwaff where it is zero.
2936 __isl_give isl_set
*isl_pw_aff_zero_set(__isl_take isl_pw_aff
*pwaff
)
2938 return pw_aff_locus(pwaff
, &aff_zero_basic_set
, 0);
2941 /* Return a set containing those elements in the domain
2942 * of pwaff where it is not zero.
2944 __isl_give isl_set
*isl_pw_aff_non_zero_set(__isl_take isl_pw_aff
*pwaff
)
2946 return pw_aff_locus(pwaff
, &aff_zero_basic_set
, 1);
2949 /* Return a set containing those elements in the shared domain
2950 * of pwaff1 and pwaff2 where pwaff1 is greater than (or equal) to pwaff2.
2952 * We compute the difference on the shared domain and then construct
2953 * the set of values where this difference is non-negative.
2954 * If strict is set, we first subtract 1 from the difference.
2955 * If equal is set, we only return the elements where pwaff1 and pwaff2
2958 static __isl_give isl_set
*pw_aff_gte_set(__isl_take isl_pw_aff
*pwaff1
,
2959 __isl_take isl_pw_aff
*pwaff2
, int strict
, int equal
)
2961 isl_set
*set1
, *set2
;
2963 set1
= isl_pw_aff_domain(isl_pw_aff_copy(pwaff1
));
2964 set2
= isl_pw_aff_domain(isl_pw_aff_copy(pwaff2
));
2965 set1
= isl_set_intersect(set1
, set2
);
2966 pwaff1
= isl_pw_aff_intersect_domain(pwaff1
, isl_set_copy(set1
));
2967 pwaff2
= isl_pw_aff_intersect_domain(pwaff2
, isl_set_copy(set1
));
2968 pwaff1
= isl_pw_aff_add(pwaff1
, isl_pw_aff_neg(pwaff2
));
2971 isl_space
*dim
= isl_set_get_space(set1
);
2973 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(dim
));
2974 aff
= isl_aff_add_constant_si(aff
, -1);
2975 pwaff1
= isl_pw_aff_add(pwaff1
, isl_pw_aff_alloc(set1
, aff
));
2980 return isl_pw_aff_zero_set(pwaff1
);
2981 return isl_pw_aff_nonneg_set(pwaff1
);
2984 /* Return a set containing those elements in the shared domain
2985 * of pwaff1 and pwaff2 where pwaff1 is equal to pwaff2.
2987 static __isl_give isl_set
*pw_aff_eq_set(__isl_take isl_pw_aff
*pwaff1
,
2988 __isl_take isl_pw_aff
*pwaff2
)
2990 return pw_aff_gte_set(pwaff1
, pwaff2
, 0, 1);
2993 __isl_give isl_set
*isl_pw_aff_eq_set(__isl_take isl_pw_aff
*pwaff1
,
2994 __isl_take isl_pw_aff
*pwaff2
)
2996 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_eq_set
);
2999 /* Return a set containing those elements in the shared domain
3000 * of pwaff1 and pwaff2 where pwaff1 is greater than or equal to pwaff2.
3002 static __isl_give isl_set
*pw_aff_ge_set(__isl_take isl_pw_aff
*pwaff1
,
3003 __isl_take isl_pw_aff
*pwaff2
)
3005 return pw_aff_gte_set(pwaff1
, pwaff2
, 0, 0);
3008 __isl_give isl_set
*isl_pw_aff_ge_set(__isl_take isl_pw_aff
*pwaff1
,
3009 __isl_take isl_pw_aff
*pwaff2
)
3011 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_ge_set
);
3014 /* Return a set containing those elements in the shared domain
3015 * of pwaff1 and pwaff2 where pwaff1 is strictly greater than pwaff2.
3017 static __isl_give isl_set
*pw_aff_gt_set(__isl_take isl_pw_aff
*pwaff1
,
3018 __isl_take isl_pw_aff
*pwaff2
)
3020 return pw_aff_gte_set(pwaff1
, pwaff2
, 1, 0);
3023 __isl_give isl_set
*isl_pw_aff_gt_set(__isl_take isl_pw_aff
*pwaff1
,
3024 __isl_take isl_pw_aff
*pwaff2
)
3026 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_gt_set
);
3029 __isl_give isl_set
*isl_pw_aff_le_set(__isl_take isl_pw_aff
*pwaff1
,
3030 __isl_take isl_pw_aff
*pwaff2
)
3032 return isl_pw_aff_ge_set(pwaff2
, pwaff1
);
3035 __isl_give isl_set
*isl_pw_aff_lt_set(__isl_take isl_pw_aff
*pwaff1
,
3036 __isl_take isl_pw_aff
*pwaff2
)
3038 return isl_pw_aff_gt_set(pwaff2
, pwaff1
);
3041 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3042 * where the function values are ordered in the same way as "order",
3043 * which returns a set in the shared domain of its two arguments.
3044 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3046 * Let "pa1" and "pa2" be defined on domains A and B respectively.
3047 * We first pull back the two functions such that they are defined on
3048 * the domain [A -> B]. Then we apply "order", resulting in a set
3049 * in the space [A -> B]. Finally, we unwrap this set to obtain
3050 * a map in the space A -> B.
3052 static __isl_give isl_map
*isl_pw_aff_order_map_aligned(
3053 __isl_take isl_pw_aff
*pa1
, __isl_take isl_pw_aff
*pa2
,
3054 __isl_give isl_set
*(*order
)(__isl_take isl_pw_aff
*pa1
,
3055 __isl_take isl_pw_aff
*pa2
))
3057 isl_space
*space1
, *space2
;
3061 space1
= isl_space_domain(isl_pw_aff_get_space(pa1
));
3062 space2
= isl_space_domain(isl_pw_aff_get_space(pa2
));
3063 space1
= isl_space_map_from_domain_and_range(space1
, space2
);
3064 ma
= isl_multi_aff_domain_map(isl_space_copy(space1
));
3065 pa1
= isl_pw_aff_pullback_multi_aff(pa1
, ma
);
3066 ma
= isl_multi_aff_range_map(space1
);
3067 pa2
= isl_pw_aff_pullback_multi_aff(pa2
, ma
);
3068 set
= order(pa1
, pa2
);
3070 return isl_set_unwrap(set
);
3073 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3074 * where the function values are equal.
3075 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3077 static __isl_give isl_map
*isl_pw_aff_eq_map_aligned(__isl_take isl_pw_aff
*pa1
,
3078 __isl_take isl_pw_aff
*pa2
)
3080 return isl_pw_aff_order_map_aligned(pa1
, pa2
, &isl_pw_aff_eq_set
);
3083 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3084 * where the function values are equal.
3086 __isl_give isl_map
*isl_pw_aff_eq_map(__isl_take isl_pw_aff
*pa1
,
3087 __isl_take isl_pw_aff
*pa2
)
3089 return align_params_pw_pw_map_and(pa1
, pa2
, &isl_pw_aff_eq_map_aligned
);
3092 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3093 * where the function value of "pa1" is less than the function value of "pa2".
3094 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3096 static __isl_give isl_map
*isl_pw_aff_lt_map_aligned(__isl_take isl_pw_aff
*pa1
,
3097 __isl_take isl_pw_aff
*pa2
)
3099 return isl_pw_aff_order_map_aligned(pa1
, pa2
, &isl_pw_aff_lt_set
);
3102 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3103 * where the function value of "pa1" is less than the function value of "pa2".
3105 __isl_give isl_map
*isl_pw_aff_lt_map(__isl_take isl_pw_aff
*pa1
,
3106 __isl_take isl_pw_aff
*pa2
)
3108 return align_params_pw_pw_map_and(pa1
, pa2
, &isl_pw_aff_lt_map_aligned
);
3111 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3112 * where the function value of "pa1" is greater than the function value
3114 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3116 static __isl_give isl_map
*isl_pw_aff_gt_map_aligned(__isl_take isl_pw_aff
*pa1
,
3117 __isl_take isl_pw_aff
*pa2
)
3119 return isl_pw_aff_order_map_aligned(pa1
, pa2
, &isl_pw_aff_gt_set
);
3122 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3123 * where the function value of "pa1" is greater than the function value
3126 __isl_give isl_map
*isl_pw_aff_gt_map(__isl_take isl_pw_aff
*pa1
,
3127 __isl_take isl_pw_aff
*pa2
)
3129 return align_params_pw_pw_map_and(pa1
, pa2
, &isl_pw_aff_gt_map_aligned
);
3132 /* Return a set containing those elements in the shared domain
3133 * of the elements of list1 and list2 where each element in list1
3134 * has the relation specified by "fn" with each element in list2.
3136 static __isl_give isl_set
*pw_aff_list_set(__isl_take isl_pw_aff_list
*list1
,
3137 __isl_take isl_pw_aff_list
*list2
,
3138 __isl_give isl_set
*(*fn
)(__isl_take isl_pw_aff
*pwaff1
,
3139 __isl_take isl_pw_aff
*pwaff2
))
3145 if (!list1
|| !list2
)
3148 ctx
= isl_pw_aff_list_get_ctx(list1
);
3149 if (list1
->n
< 1 || list2
->n
< 1)
3150 isl_die(ctx
, isl_error_invalid
,
3151 "list should contain at least one element", goto error
);
3153 set
= isl_set_universe(isl_pw_aff_get_domain_space(list1
->p
[0]));
3154 for (i
= 0; i
< list1
->n
; ++i
)
3155 for (j
= 0; j
< list2
->n
; ++j
) {
3158 set_ij
= fn(isl_pw_aff_copy(list1
->p
[i
]),
3159 isl_pw_aff_copy(list2
->p
[j
]));
3160 set
= isl_set_intersect(set
, set_ij
);
3163 isl_pw_aff_list_free(list1
);
3164 isl_pw_aff_list_free(list2
);
3167 isl_pw_aff_list_free(list1
);
3168 isl_pw_aff_list_free(list2
);
3172 /* Return a set containing those elements in the shared domain
3173 * of the elements of list1 and list2 where each element in list1
3174 * is equal to each element in list2.
3176 __isl_give isl_set
*isl_pw_aff_list_eq_set(__isl_take isl_pw_aff_list
*list1
,
3177 __isl_take isl_pw_aff_list
*list2
)
3179 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_eq_set
);
3182 __isl_give isl_set
*isl_pw_aff_list_ne_set(__isl_take isl_pw_aff_list
*list1
,
3183 __isl_take isl_pw_aff_list
*list2
)
3185 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_ne_set
);
3188 /* Return a set containing those elements in the shared domain
3189 * of the elements of list1 and list2 where each element in list1
3190 * is less than or equal to each element in list2.
3192 __isl_give isl_set
*isl_pw_aff_list_le_set(__isl_take isl_pw_aff_list
*list1
,
3193 __isl_take isl_pw_aff_list
*list2
)
3195 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_le_set
);
3198 __isl_give isl_set
*isl_pw_aff_list_lt_set(__isl_take isl_pw_aff_list
*list1
,
3199 __isl_take isl_pw_aff_list
*list2
)
3201 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_lt_set
);
3204 __isl_give isl_set
*isl_pw_aff_list_ge_set(__isl_take isl_pw_aff_list
*list1
,
3205 __isl_take isl_pw_aff_list
*list2
)
3207 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_ge_set
);
3210 __isl_give isl_set
*isl_pw_aff_list_gt_set(__isl_take isl_pw_aff_list
*list1
,
3211 __isl_take isl_pw_aff_list
*list2
)
3213 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_gt_set
);
3217 /* Return a set containing those elements in the shared domain
3218 * of pwaff1 and pwaff2 where pwaff1 is not equal to pwaff2.
3220 static __isl_give isl_set
*pw_aff_ne_set(__isl_take isl_pw_aff
*pwaff1
,
3221 __isl_take isl_pw_aff
*pwaff2
)
3223 isl_set
*set_lt
, *set_gt
;
3225 set_lt
= isl_pw_aff_lt_set(isl_pw_aff_copy(pwaff1
),
3226 isl_pw_aff_copy(pwaff2
));
3227 set_gt
= isl_pw_aff_gt_set(pwaff1
, pwaff2
);
3228 return isl_set_union_disjoint(set_lt
, set_gt
);
3231 __isl_give isl_set
*isl_pw_aff_ne_set(__isl_take isl_pw_aff
*pwaff1
,
3232 __isl_take isl_pw_aff
*pwaff2
)
3234 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_ne_set
);
3237 __isl_give isl_pw_aff
*isl_pw_aff_scale_down(__isl_take isl_pw_aff
*pwaff
,
3242 if (isl_int_is_one(v
))
3244 if (!isl_int_is_pos(v
))
3245 isl_die(isl_pw_aff_get_ctx(pwaff
), isl_error_invalid
,
3246 "factor needs to be positive",
3247 return isl_pw_aff_free(pwaff
));
3248 pwaff
= isl_pw_aff_cow(pwaff
);
3254 for (i
= 0; i
< pwaff
->n
; ++i
) {
3255 pwaff
->p
[i
].aff
= isl_aff_scale_down(pwaff
->p
[i
].aff
, v
);
3256 if (!pwaff
->p
[i
].aff
)
3257 return isl_pw_aff_free(pwaff
);
3263 __isl_give isl_pw_aff
*isl_pw_aff_floor(__isl_take isl_pw_aff
*pwaff
)
3267 pwaff
= isl_pw_aff_cow(pwaff
);
3273 for (i
= 0; i
< pwaff
->n
; ++i
) {
3274 pwaff
->p
[i
].aff
= isl_aff_floor(pwaff
->p
[i
].aff
);
3275 if (!pwaff
->p
[i
].aff
)
3276 return isl_pw_aff_free(pwaff
);
3282 __isl_give isl_pw_aff
*isl_pw_aff_ceil(__isl_take isl_pw_aff
*pwaff
)
3286 pwaff
= isl_pw_aff_cow(pwaff
);
3292 for (i
= 0; i
< pwaff
->n
; ++i
) {
3293 pwaff
->p
[i
].aff
= isl_aff_ceil(pwaff
->p
[i
].aff
);
3294 if (!pwaff
->p
[i
].aff
)
3295 return isl_pw_aff_free(pwaff
);
3301 /* Assuming that "cond1" and "cond2" are disjoint,
3302 * return an affine expression that is equal to pwaff1 on cond1
3303 * and to pwaff2 on cond2.
3305 static __isl_give isl_pw_aff
*isl_pw_aff_select(
3306 __isl_take isl_set
*cond1
, __isl_take isl_pw_aff
*pwaff1
,
3307 __isl_take isl_set
*cond2
, __isl_take isl_pw_aff
*pwaff2
)
3309 pwaff1
= isl_pw_aff_intersect_domain(pwaff1
, cond1
);
3310 pwaff2
= isl_pw_aff_intersect_domain(pwaff2
, cond2
);
3312 return isl_pw_aff_add_disjoint(pwaff1
, pwaff2
);
3315 /* Return an affine expression that is equal to pwaff_true for elements
3316 * where "cond" is non-zero and to pwaff_false for elements where "cond"
3318 * That is, return cond ? pwaff_true : pwaff_false;
3320 * If "cond" involves and NaN, then we conservatively return a NaN
3321 * on its entire domain. In principle, we could consider the pieces
3322 * where it is NaN separately from those where it is not.
3324 * If "pwaff_true" and "pwaff_false" are obviously equal to each other,
3325 * then only use the domain of "cond" to restrict the domain.
3327 __isl_give isl_pw_aff
*isl_pw_aff_cond(__isl_take isl_pw_aff
*cond
,
3328 __isl_take isl_pw_aff
*pwaff_true
, __isl_take isl_pw_aff
*pwaff_false
)
3330 isl_set
*cond_true
, *cond_false
;
3335 if (isl_pw_aff_involves_nan(cond
)) {
3336 isl_space
*space
= isl_pw_aff_get_domain_space(cond
);
3337 isl_local_space
*ls
= isl_local_space_from_space(space
);
3338 isl_pw_aff_free(cond
);
3339 isl_pw_aff_free(pwaff_true
);
3340 isl_pw_aff_free(pwaff_false
);
3341 return isl_pw_aff_nan_on_domain(ls
);
3344 pwaff_true
= isl_pw_aff_align_params(pwaff_true
,
3345 isl_pw_aff_get_space(pwaff_false
));
3346 pwaff_false
= isl_pw_aff_align_params(pwaff_false
,
3347 isl_pw_aff_get_space(pwaff_true
));
3348 equal
= isl_pw_aff_plain_is_equal(pwaff_true
, pwaff_false
);
3354 dom
= isl_set_coalesce(isl_pw_aff_domain(cond
));
3355 isl_pw_aff_free(pwaff_false
);
3356 return isl_pw_aff_intersect_domain(pwaff_true
, dom
);
3359 cond_true
= isl_pw_aff_non_zero_set(isl_pw_aff_copy(cond
));
3360 cond_false
= isl_pw_aff_zero_set(cond
);
3361 return isl_pw_aff_select(cond_true
, pwaff_true
,
3362 cond_false
, pwaff_false
);
3364 isl_pw_aff_free(cond
);
3365 isl_pw_aff_free(pwaff_true
);
3366 isl_pw_aff_free(pwaff_false
);
3370 isl_bool
isl_aff_is_cst(__isl_keep isl_aff
*aff
)
3373 return isl_bool_error
;
3375 return isl_seq_first_non_zero(aff
->v
->el
+ 2, aff
->v
->size
- 2) == -1;
3378 /* Check whether pwaff is a piecewise constant.
3380 isl_bool
isl_pw_aff_is_cst(__isl_keep isl_pw_aff
*pwaff
)
3385 return isl_bool_error
;
3387 for (i
= 0; i
< pwaff
->n
; ++i
) {
3388 isl_bool is_cst
= isl_aff_is_cst(pwaff
->p
[i
].aff
);
3389 if (is_cst
< 0 || !is_cst
)
3393 return isl_bool_true
;
3396 /* Are all elements of "mpa" piecewise constants?
3398 isl_bool
isl_multi_pw_aff_is_cst(__isl_keep isl_multi_pw_aff
*mpa
)
3403 return isl_bool_error
;
3405 for (i
= 0; i
< mpa
->n
; ++i
) {
3406 isl_bool is_cst
= isl_pw_aff_is_cst(mpa
->u
.p
[i
]);
3407 if (is_cst
< 0 || !is_cst
)
3411 return isl_bool_true
;
3414 /* Return the product of "aff1" and "aff2".
3416 * If either of the two is NaN, then the result is NaN.
3418 * Otherwise, at least one of "aff1" or "aff2" needs to be a constant.
3420 __isl_give isl_aff
*isl_aff_mul(__isl_take isl_aff
*aff1
,
3421 __isl_take isl_aff
*aff2
)
3426 if (isl_aff_is_nan(aff1
)) {
3430 if (isl_aff_is_nan(aff2
)) {
3435 if (!isl_aff_is_cst(aff2
) && isl_aff_is_cst(aff1
))
3436 return isl_aff_mul(aff2
, aff1
);
3438 if (!isl_aff_is_cst(aff2
))
3439 isl_die(isl_aff_get_ctx(aff1
), isl_error_invalid
,
3440 "at least one affine expression should be constant",
3443 aff1
= isl_aff_cow(aff1
);
3447 aff1
= isl_aff_scale(aff1
, aff2
->v
->el
[1]);
3448 aff1
= isl_aff_scale_down(aff1
, aff2
->v
->el
[0]);
3458 /* Divide "aff1" by "aff2", assuming "aff2" is a constant.
3460 * If either of the two is NaN, then the result is NaN.
3462 __isl_give isl_aff
*isl_aff_div(__isl_take isl_aff
*aff1
,
3463 __isl_take isl_aff
*aff2
)
3471 if (isl_aff_is_nan(aff1
)) {
3475 if (isl_aff_is_nan(aff2
)) {
3480 is_cst
= isl_aff_is_cst(aff2
);
3484 isl_die(isl_aff_get_ctx(aff2
), isl_error_invalid
,
3485 "second argument should be a constant", goto error
);
3490 neg
= isl_int_is_neg(aff2
->v
->el
[1]);
3492 isl_int_neg(aff2
->v
->el
[0], aff2
->v
->el
[0]);
3493 isl_int_neg(aff2
->v
->el
[1], aff2
->v
->el
[1]);
3496 aff1
= isl_aff_scale(aff1
, aff2
->v
->el
[0]);
3497 aff1
= isl_aff_scale_down(aff1
, aff2
->v
->el
[1]);
3500 isl_int_neg(aff2
->v
->el
[0], aff2
->v
->el
[0]);
3501 isl_int_neg(aff2
->v
->el
[1], aff2
->v
->el
[1]);
3512 static __isl_give isl_pw_aff
*pw_aff_add(__isl_take isl_pw_aff
*pwaff1
,
3513 __isl_take isl_pw_aff
*pwaff2
)
3515 return isl_pw_aff_on_shared_domain(pwaff1
, pwaff2
, &isl_aff_add
);
3518 __isl_give isl_pw_aff
*isl_pw_aff_add(__isl_take isl_pw_aff
*pwaff1
,
3519 __isl_take isl_pw_aff
*pwaff2
)
3521 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
, &pw_aff_add
);
3524 __isl_give isl_pw_aff
*isl_pw_aff_union_add(__isl_take isl_pw_aff
*pwaff1
,
3525 __isl_take isl_pw_aff
*pwaff2
)
3527 return isl_pw_aff_union_add_(pwaff1
, pwaff2
);
3530 static __isl_give isl_pw_aff
*pw_aff_mul(__isl_take isl_pw_aff
*pwaff1
,
3531 __isl_take isl_pw_aff
*pwaff2
)
3533 return isl_pw_aff_on_shared_domain(pwaff1
, pwaff2
, &isl_aff_mul
);
3536 __isl_give isl_pw_aff
*isl_pw_aff_mul(__isl_take isl_pw_aff
*pwaff1
,
3537 __isl_take isl_pw_aff
*pwaff2
)
3539 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
, &pw_aff_mul
);
3542 static __isl_give isl_pw_aff
*pw_aff_div(__isl_take isl_pw_aff
*pa1
,
3543 __isl_take isl_pw_aff
*pa2
)
3545 return isl_pw_aff_on_shared_domain(pa1
, pa2
, &isl_aff_div
);
3548 /* Divide "pa1" by "pa2", assuming "pa2" is a piecewise constant.
3550 __isl_give isl_pw_aff
*isl_pw_aff_div(__isl_take isl_pw_aff
*pa1
,
3551 __isl_take isl_pw_aff
*pa2
)
3555 is_cst
= isl_pw_aff_is_cst(pa2
);
3559 isl_die(isl_pw_aff_get_ctx(pa2
), isl_error_invalid
,
3560 "second argument should be a piecewise constant",
3562 return isl_pw_aff_align_params_pw_pw_and(pa1
, pa2
, &pw_aff_div
);
3564 isl_pw_aff_free(pa1
);
3565 isl_pw_aff_free(pa2
);
3569 /* Compute the quotient of the integer division of "pa1" by "pa2"
3570 * with rounding towards zero.
3571 * "pa2" is assumed to be a piecewise constant.
3573 * In particular, return
3575 * pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2)
3578 __isl_give isl_pw_aff
*isl_pw_aff_tdiv_q(__isl_take isl_pw_aff
*pa1
,
3579 __isl_take isl_pw_aff
*pa2
)
3585 is_cst
= isl_pw_aff_is_cst(pa2
);
3589 isl_die(isl_pw_aff_get_ctx(pa2
), isl_error_invalid
,
3590 "second argument should be a piecewise constant",
3593 pa1
= isl_pw_aff_div(pa1
, pa2
);
3595 cond
= isl_pw_aff_nonneg_set(isl_pw_aff_copy(pa1
));
3596 f
= isl_pw_aff_floor(isl_pw_aff_copy(pa1
));
3597 c
= isl_pw_aff_ceil(pa1
);
3598 return isl_pw_aff_cond(isl_set_indicator_function(cond
), f
, c
);
3600 isl_pw_aff_free(pa1
);
3601 isl_pw_aff_free(pa2
);
3605 /* Compute the remainder of the integer division of "pa1" by "pa2"
3606 * with rounding towards zero.
3607 * "pa2" is assumed to be a piecewise constant.
3609 * In particular, return
3611 * pa1 - pa2 * (pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2))
3614 __isl_give isl_pw_aff
*isl_pw_aff_tdiv_r(__isl_take isl_pw_aff
*pa1
,
3615 __isl_take isl_pw_aff
*pa2
)
3620 is_cst
= isl_pw_aff_is_cst(pa2
);
3624 isl_die(isl_pw_aff_get_ctx(pa2
), isl_error_invalid
,
3625 "second argument should be a piecewise constant",
3627 res
= isl_pw_aff_tdiv_q(isl_pw_aff_copy(pa1
), isl_pw_aff_copy(pa2
));
3628 res
= isl_pw_aff_mul(pa2
, res
);
3629 res
= isl_pw_aff_sub(pa1
, res
);
3632 isl_pw_aff_free(pa1
);
3633 isl_pw_aff_free(pa2
);
3637 /* Does either of "pa1" or "pa2" involve any NaN2?
3639 static isl_bool
either_involves_nan(__isl_keep isl_pw_aff
*pa1
,
3640 __isl_keep isl_pw_aff
*pa2
)
3644 has_nan
= isl_pw_aff_involves_nan(pa1
);
3645 if (has_nan
< 0 || has_nan
)
3647 return isl_pw_aff_involves_nan(pa2
);
3650 /* Replace "pa1" and "pa2" (at least one of which involves a NaN)
3651 * by a NaN on their shared domain.
3653 * In principle, the result could be refined to only being NaN
3654 * on the parts of this domain where at least one of "pa1" or "pa2" is NaN.
3656 static __isl_give isl_pw_aff
*replace_by_nan(__isl_take isl_pw_aff
*pa1
,
3657 __isl_take isl_pw_aff
*pa2
)
3659 isl_local_space
*ls
;
3663 dom
= isl_set_intersect(isl_pw_aff_domain(pa1
), isl_pw_aff_domain(pa2
));
3664 ls
= isl_local_space_from_space(isl_set_get_space(dom
));
3665 pa
= isl_pw_aff_nan_on_domain(ls
);
3666 pa
= isl_pw_aff_intersect_domain(pa
, dom
);
3671 static __isl_give isl_pw_aff
*pw_aff_min(__isl_take isl_pw_aff
*pwaff1
,
3672 __isl_take isl_pw_aff
*pwaff2
)
3677 dom
= isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1
)),
3678 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2
)));
3679 le
= isl_pw_aff_le_set(isl_pw_aff_copy(pwaff1
),
3680 isl_pw_aff_copy(pwaff2
));
3681 dom
= isl_set_subtract(dom
, isl_set_copy(le
));
3682 return isl_pw_aff_select(le
, pwaff1
, dom
, pwaff2
);
3685 static __isl_give isl_pw_aff
*pw_aff_max(__isl_take isl_pw_aff
*pwaff1
,
3686 __isl_take isl_pw_aff
*pwaff2
)
3691 dom
= isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1
)),
3692 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2
)));
3693 ge
= isl_pw_aff_ge_set(isl_pw_aff_copy(pwaff1
),
3694 isl_pw_aff_copy(pwaff2
));
3695 dom
= isl_set_subtract(dom
, isl_set_copy(ge
));
3696 return isl_pw_aff_select(ge
, pwaff1
, dom
, pwaff2
);
3699 /* Return an expression for the minimum (if "max" is not set) or
3700 * the maximum (if "max" is set) of "pa1" and "pa2".
3701 * If either expression involves any NaN, then return a NaN
3702 * on the shared domain as result.
3704 static __isl_give isl_pw_aff
*pw_aff_min_max(__isl_take isl_pw_aff
*pa1
,
3705 __isl_take isl_pw_aff
*pa2
, int max
)
3709 has_nan
= either_involves_nan(pa1
, pa2
);
3711 pa1
= isl_pw_aff_free(pa1
);
3713 return replace_by_nan(pa1
, pa2
);
3716 return isl_pw_aff_align_params_pw_pw_and(pa1
, pa2
, &pw_aff_max
);
3718 return isl_pw_aff_align_params_pw_pw_and(pa1
, pa2
, &pw_aff_min
);
3721 /* Return an expression for the minimum of "pwaff1" and "pwaff2".
3723 __isl_give isl_pw_aff
*isl_pw_aff_min(__isl_take isl_pw_aff
*pwaff1
,
3724 __isl_take isl_pw_aff
*pwaff2
)
3726 return pw_aff_min_max(pwaff1
, pwaff2
, 0);
3729 /* Return an expression for the maximum of "pwaff1" and "pwaff2".
3731 __isl_give isl_pw_aff
*isl_pw_aff_max(__isl_take isl_pw_aff
*pwaff1
,
3732 __isl_take isl_pw_aff
*pwaff2
)
3734 return pw_aff_min_max(pwaff1
, pwaff2
, 1);
3737 static __isl_give isl_pw_aff
*pw_aff_list_reduce(
3738 __isl_take isl_pw_aff_list
*list
,
3739 __isl_give isl_pw_aff
*(*fn
)(__isl_take isl_pw_aff
*pwaff1
,
3740 __isl_take isl_pw_aff
*pwaff2
))
3749 ctx
= isl_pw_aff_list_get_ctx(list
);
3751 isl_die(ctx
, isl_error_invalid
,
3752 "list should contain at least one element", goto error
);
3754 res
= isl_pw_aff_copy(list
->p
[0]);
3755 for (i
= 1; i
< list
->n
; ++i
)
3756 res
= fn(res
, isl_pw_aff_copy(list
->p
[i
]));
3758 isl_pw_aff_list_free(list
);
3761 isl_pw_aff_list_free(list
);
3765 /* Return an isl_pw_aff that maps each element in the intersection of the
3766 * domains of the elements of list to the minimal corresponding affine
3769 __isl_give isl_pw_aff
*isl_pw_aff_list_min(__isl_take isl_pw_aff_list
*list
)
3771 return pw_aff_list_reduce(list
, &isl_pw_aff_min
);
3774 /* Return an isl_pw_aff that maps each element in the intersection of the
3775 * domains of the elements of list to the maximal corresponding affine
3778 __isl_give isl_pw_aff
*isl_pw_aff_list_max(__isl_take isl_pw_aff_list
*list
)
3780 return pw_aff_list_reduce(list
, &isl_pw_aff_max
);
3783 /* Mark the domains of "pwaff" as rational.
3785 __isl_give isl_pw_aff
*isl_pw_aff_set_rational(__isl_take isl_pw_aff
*pwaff
)
3789 pwaff
= isl_pw_aff_cow(pwaff
);
3795 for (i
= 0; i
< pwaff
->n
; ++i
) {
3796 pwaff
->p
[i
].set
= isl_set_set_rational(pwaff
->p
[i
].set
);
3797 if (!pwaff
->p
[i
].set
)
3798 return isl_pw_aff_free(pwaff
);
3804 /* Mark the domains of the elements of "list" as rational.
3806 __isl_give isl_pw_aff_list
*isl_pw_aff_list_set_rational(
3807 __isl_take isl_pw_aff_list
*list
)
3817 for (i
= 0; i
< n
; ++i
) {
3820 pa
= isl_pw_aff_list_get_pw_aff(list
, i
);
3821 pa
= isl_pw_aff_set_rational(pa
);
3822 list
= isl_pw_aff_list_set_pw_aff(list
, i
, pa
);
3828 /* Do the parameters of "aff" match those of "space"?
3830 isl_bool
isl_aff_matching_params(__isl_keep isl_aff
*aff
,
3831 __isl_keep isl_space
*space
)
3833 isl_space
*aff_space
;
3837 return isl_bool_error
;
3839 aff_space
= isl_aff_get_domain_space(aff
);
3841 match
= isl_space_has_equal_params(space
, aff_space
);
3843 isl_space_free(aff_space
);
3847 /* Check that the domain space of "aff" matches "space".
3849 isl_stat
isl_aff_check_match_domain_space(__isl_keep isl_aff
*aff
,
3850 __isl_keep isl_space
*space
)
3852 isl_space
*aff_space
;
3856 return isl_stat_error
;
3858 aff_space
= isl_aff_get_domain_space(aff
);
3860 match
= isl_space_has_equal_params(space
, aff_space
);
3864 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
3865 "parameters don't match", goto error
);
3866 match
= isl_space_tuple_is_equal(space
, isl_dim_in
,
3867 aff_space
, isl_dim_set
);
3871 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
3872 "domains don't match", goto error
);
3873 isl_space_free(aff_space
);
3876 isl_space_free(aff_space
);
3877 return isl_stat_error
;
3886 #include <isl_multi_no_explicit_domain.c>
3887 #include <isl_multi_templ.c>
3888 #include <isl_multi_apply_set.c>
3889 #include <isl_multi_cmp.c>
3890 #include <isl_multi_dims.c>
3891 #include <isl_multi_floor.c>
3892 #include <isl_multi_gist.c>
3896 /* Construct an isl_multi_aff living in "space" that corresponds
3897 * to the affine transformation matrix "mat".
3899 __isl_give isl_multi_aff
*isl_multi_aff_from_aff_mat(
3900 __isl_take isl_space
*space
, __isl_take isl_mat
*mat
)
3903 isl_local_space
*ls
= NULL
;
3904 isl_multi_aff
*ma
= NULL
;
3905 int n_row
, n_col
, n_out
, total
;
3911 ctx
= isl_mat_get_ctx(mat
);
3913 n_row
= isl_mat_rows(mat
);
3914 n_col
= isl_mat_cols(mat
);
3916 isl_die(ctx
, isl_error_invalid
,
3917 "insufficient number of rows", goto error
);
3919 isl_die(ctx
, isl_error_invalid
,
3920 "insufficient number of columns", goto error
);
3921 n_out
= isl_space_dim(space
, isl_dim_out
);
3922 total
= isl_space_dim(space
, isl_dim_all
);
3923 if (1 + n_out
!= n_row
|| 2 + total
!= n_row
+ n_col
)
3924 isl_die(ctx
, isl_error_invalid
,
3925 "dimension mismatch", goto error
);
3927 ma
= isl_multi_aff_zero(isl_space_copy(space
));
3928 ls
= isl_local_space_from_space(isl_space_domain(space
));
3930 for (i
= 0; i
< n_row
- 1; ++i
) {
3934 v
= isl_vec_alloc(ctx
, 1 + n_col
);
3937 isl_int_set(v
->el
[0], mat
->row
[0][0]);
3938 isl_seq_cpy(v
->el
+ 1, mat
->row
[1 + i
], n_col
);
3939 v
= isl_vec_normalize(v
);
3940 aff
= isl_aff_alloc_vec(isl_local_space_copy(ls
), v
);
3941 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
3944 isl_local_space_free(ls
);
3948 isl_local_space_free(ls
);
3950 isl_multi_aff_free(ma
);
3954 /* Remove any internal structure of the domain of "ma".
3955 * If there is any such internal structure in the input,
3956 * then the name of the corresponding space is also removed.
3958 __isl_give isl_multi_aff
*isl_multi_aff_flatten_domain(
3959 __isl_take isl_multi_aff
*ma
)
3966 if (!ma
->space
->nested
[0])
3969 space
= isl_multi_aff_get_space(ma
);
3970 space
= isl_space_flatten_domain(space
);
3971 ma
= isl_multi_aff_reset_space(ma
, space
);
3976 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
3977 * of the space to its domain.
3979 __isl_give isl_multi_aff
*isl_multi_aff_domain_map(__isl_take isl_space
*space
)
3982 isl_local_space
*ls
;
3987 if (!isl_space_is_map(space
))
3988 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
3989 "not a map space", goto error
);
3991 n_in
= isl_space_dim(space
, isl_dim_in
);
3992 space
= isl_space_domain_map(space
);
3994 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
3996 isl_space_free(space
);
4000 space
= isl_space_domain(space
);
4001 ls
= isl_local_space_from_space(space
);
4002 for (i
= 0; i
< n_in
; ++i
) {
4005 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
4007 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
4009 isl_local_space_free(ls
);
4012 isl_space_free(space
);
4016 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
4017 * of the space to its range.
4019 __isl_give isl_multi_aff
*isl_multi_aff_range_map(__isl_take isl_space
*space
)
4022 isl_local_space
*ls
;
4027 if (!isl_space_is_map(space
))
4028 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
4029 "not a map space", goto error
);
4031 n_in
= isl_space_dim(space
, isl_dim_in
);
4032 n_out
= isl_space_dim(space
, isl_dim_out
);
4033 space
= isl_space_range_map(space
);
4035 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
4037 isl_space_free(space
);
4041 space
= isl_space_domain(space
);
4042 ls
= isl_local_space_from_space(space
);
4043 for (i
= 0; i
< n_out
; ++i
) {
4046 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
4047 isl_dim_set
, n_in
+ i
);
4048 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
4050 isl_local_space_free(ls
);
4053 isl_space_free(space
);
4057 /* Given a map space, return an isl_pw_multi_aff that maps a wrapped copy
4058 * of the space to its range.
4060 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_range_map(
4061 __isl_take isl_space
*space
)
4063 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_range_map(space
));
4066 /* Given the space of a set and a range of set dimensions,
4067 * construct an isl_multi_aff that projects out those dimensions.
4069 __isl_give isl_multi_aff
*isl_multi_aff_project_out_map(
4070 __isl_take isl_space
*space
, enum isl_dim_type type
,
4071 unsigned first
, unsigned n
)
4074 isl_local_space
*ls
;
4079 if (!isl_space_is_set(space
))
4080 isl_die(isl_space_get_ctx(space
), isl_error_unsupported
,
4081 "expecting set space", goto error
);
4082 if (type
!= isl_dim_set
)
4083 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
4084 "only set dimensions can be projected out", goto error
);
4086 dim
= isl_space_dim(space
, isl_dim_set
);
4087 if (first
+ n
> dim
)
4088 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
4089 "range out of bounds", goto error
);
4091 space
= isl_space_from_domain(space
);
4092 space
= isl_space_add_dims(space
, isl_dim_out
, dim
- n
);
4095 return isl_multi_aff_alloc(space
);
4097 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
4098 space
= isl_space_domain(space
);
4099 ls
= isl_local_space_from_space(space
);
4101 for (i
= 0; i
< first
; ++i
) {
4104 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
4106 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
4109 for (i
= 0; i
< dim
- (first
+ n
); ++i
) {
4112 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
4113 isl_dim_set
, first
+ n
+ i
);
4114 ma
= isl_multi_aff_set_aff(ma
, first
+ i
, aff
);
4117 isl_local_space_free(ls
);
4120 isl_space_free(space
);
4124 /* Given the space of a set and a range of set dimensions,
4125 * construct an isl_pw_multi_aff that projects out those dimensions.
4127 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_project_out_map(
4128 __isl_take isl_space
*space
, enum isl_dim_type type
,
4129 unsigned first
, unsigned n
)
4133 ma
= isl_multi_aff_project_out_map(space
, type
, first
, n
);
4134 return isl_pw_multi_aff_from_multi_aff(ma
);
4137 /* Create an isl_pw_multi_aff with the given isl_multi_aff on a universe
4140 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_multi_aff(
4141 __isl_take isl_multi_aff
*ma
)
4143 isl_set
*dom
= isl_set_universe(isl_multi_aff_get_domain_space(ma
));
4144 return isl_pw_multi_aff_alloc(dom
, ma
);
4147 /* Create a piecewise multi-affine expression in the given space that maps each
4148 * input dimension to the corresponding output dimension.
4150 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_identity(
4151 __isl_take isl_space
*space
)
4153 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_identity(space
));
4156 /* Exploit the equalities in "eq" to simplify the affine expressions.
4158 static __isl_give isl_multi_aff
*isl_multi_aff_substitute_equalities(
4159 __isl_take isl_multi_aff
*maff
, __isl_take isl_basic_set
*eq
)
4163 maff
= isl_multi_aff_cow(maff
);
4167 for (i
= 0; i
< maff
->n
; ++i
) {
4168 maff
->u
.p
[i
] = isl_aff_substitute_equalities(maff
->u
.p
[i
],
4169 isl_basic_set_copy(eq
));
4174 isl_basic_set_free(eq
);
4177 isl_basic_set_free(eq
);
4178 isl_multi_aff_free(maff
);
4182 __isl_give isl_multi_aff
*isl_multi_aff_scale(__isl_take isl_multi_aff
*maff
,
4187 maff
= isl_multi_aff_cow(maff
);
4191 for (i
= 0; i
< maff
->n
; ++i
) {
4192 maff
->u
.p
[i
] = isl_aff_scale(maff
->u
.p
[i
], f
);
4194 return isl_multi_aff_free(maff
);
4200 __isl_give isl_multi_aff
*isl_multi_aff_add_on_domain(__isl_keep isl_set
*dom
,
4201 __isl_take isl_multi_aff
*maff1
, __isl_take isl_multi_aff
*maff2
)
4203 maff1
= isl_multi_aff_add(maff1
, maff2
);
4204 maff1
= isl_multi_aff_gist(maff1
, isl_set_copy(dom
));
4208 int isl_multi_aff_is_empty(__isl_keep isl_multi_aff
*maff
)
4216 /* Return the set of domain elements where "ma1" is lexicographically
4217 * smaller than or equal to "ma2".
4219 __isl_give isl_set
*isl_multi_aff_lex_le_set(__isl_take isl_multi_aff
*ma1
,
4220 __isl_take isl_multi_aff
*ma2
)
4222 return isl_multi_aff_lex_ge_set(ma2
, ma1
);
4225 /* Return the set of domain elements where "ma1" is lexicographically
4226 * smaller than "ma2".
4228 __isl_give isl_set
*isl_multi_aff_lex_lt_set(__isl_take isl_multi_aff
*ma1
,
4229 __isl_take isl_multi_aff
*ma2
)
4231 return isl_multi_aff_lex_gt_set(ma2
, ma1
);
4234 /* Return the set of domain elements where "ma1" and "ma2"
4237 static __isl_give isl_set
*isl_multi_aff_order_set(
4238 __isl_take isl_multi_aff
*ma1
, __isl_take isl_multi_aff
*ma2
,
4239 __isl_give isl_map
*order(__isl_take isl_space
*set_space
))
4242 isl_map
*map1
, *map2
;
4245 map1
= isl_map_from_multi_aff(ma1
);
4246 map2
= isl_map_from_multi_aff(ma2
);
4247 map
= isl_map_range_product(map1
, map2
);
4248 space
= isl_space_range(isl_map_get_space(map
));
4249 space
= isl_space_domain(isl_space_unwrap(space
));
4251 map
= isl_map_intersect_range(map
, isl_map_wrap(ge
));
4253 return isl_map_domain(map
);
4256 /* Return the set of domain elements where "ma1" is lexicographically
4257 * greater than or equal to "ma2".
4259 __isl_give isl_set
*isl_multi_aff_lex_ge_set(__isl_take isl_multi_aff
*ma1
,
4260 __isl_take isl_multi_aff
*ma2
)
4262 return isl_multi_aff_order_set(ma1
, ma2
, &isl_map_lex_ge
);
4265 /* Return the set of domain elements where "ma1" is lexicographically
4266 * greater than "ma2".
4268 __isl_give isl_set
*isl_multi_aff_lex_gt_set(__isl_take isl_multi_aff
*ma1
,
4269 __isl_take isl_multi_aff
*ma2
)
4271 return isl_multi_aff_order_set(ma1
, ma2
, &isl_map_lex_gt
);
4275 #define PW isl_pw_multi_aff
4277 #define EL isl_multi_aff
4279 #define EL_IS_ZERO is_empty
4283 #define IS_ZERO is_empty
4286 #undef DEFAULT_IS_ZERO
4287 #define DEFAULT_IS_ZERO 0
4291 #define NO_INSERT_DIMS
4295 #include <isl_pw_templ.c>
4296 #include <isl_pw_union_opt.c>
4301 #define UNION isl_union_pw_multi_aff
4303 #define PART isl_pw_multi_aff
4305 #define PARTS pw_multi_aff
4307 #include <isl_union_multi.c>
4308 #include <isl_union_neg.c>
4310 static __isl_give isl_pw_multi_aff
*pw_multi_aff_union_lexmax(
4311 __isl_take isl_pw_multi_aff
*pma1
,
4312 __isl_take isl_pw_multi_aff
*pma2
)
4314 return isl_pw_multi_aff_union_opt_cmp(pma1
, pma2
,
4315 &isl_multi_aff_lex_ge_set
);
4318 /* Given two piecewise multi affine expressions, return a piecewise
4319 * multi-affine expression defined on the union of the definition domains
4320 * of the inputs that is equal to the lexicographic maximum of the two
4321 * inputs on each cell. If only one of the two inputs is defined on
4322 * a given cell, then it is considered to be the maximum.
4324 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_union_lexmax(
4325 __isl_take isl_pw_multi_aff
*pma1
,
4326 __isl_take isl_pw_multi_aff
*pma2
)
4328 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
4329 &pw_multi_aff_union_lexmax
);
4332 static __isl_give isl_pw_multi_aff
*pw_multi_aff_union_lexmin(
4333 __isl_take isl_pw_multi_aff
*pma1
,
4334 __isl_take isl_pw_multi_aff
*pma2
)
4336 return isl_pw_multi_aff_union_opt_cmp(pma1
, pma2
,
4337 &isl_multi_aff_lex_le_set
);
4340 /* Given two piecewise multi affine expressions, return a piecewise
4341 * multi-affine expression defined on the union of the definition domains
4342 * of the inputs that is equal to the lexicographic minimum of the two
4343 * inputs on each cell. If only one of the two inputs is defined on
4344 * a given cell, then it is considered to be the minimum.
4346 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_union_lexmin(
4347 __isl_take isl_pw_multi_aff
*pma1
,
4348 __isl_take isl_pw_multi_aff
*pma2
)
4350 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
4351 &pw_multi_aff_union_lexmin
);
4354 static __isl_give isl_pw_multi_aff
*pw_multi_aff_add(
4355 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4357 return isl_pw_multi_aff_on_shared_domain(pma1
, pma2
,
4358 &isl_multi_aff_add
);
4361 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_add(
4362 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4364 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
4368 static __isl_give isl_pw_multi_aff
*pw_multi_aff_sub(
4369 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4371 return isl_pw_multi_aff_on_shared_domain(pma1
, pma2
,
4372 &isl_multi_aff_sub
);
4375 /* Subtract "pma2" from "pma1" and return the result.
4377 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_sub(
4378 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4380 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
4384 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_union_add(
4385 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4387 return isl_pw_multi_aff_union_add_(pma1
, pma2
);
4390 /* Compute the sum of "upa1" and "upa2" on the union of their domains,
4391 * with the actual sum on the shared domain and
4392 * the defined expression on the symmetric difference of the domains.
4394 __isl_give isl_union_pw_aff
*isl_union_pw_aff_union_add(
4395 __isl_take isl_union_pw_aff
*upa1
, __isl_take isl_union_pw_aff
*upa2
)
4397 return isl_union_pw_aff_union_add_(upa1
, upa2
);
4400 /* Compute the sum of "upma1" and "upma2" on the union of their domains,
4401 * with the actual sum on the shared domain and
4402 * the defined expression on the symmetric difference of the domains.
4404 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_union_add(
4405 __isl_take isl_union_pw_multi_aff
*upma1
,
4406 __isl_take isl_union_pw_multi_aff
*upma2
)
4408 return isl_union_pw_multi_aff_union_add_(upma1
, upma2
);
4411 /* Given two piecewise multi-affine expressions A -> B and C -> D,
4412 * construct a piecewise multi-affine expression [A -> C] -> [B -> D].
4414 static __isl_give isl_pw_multi_aff
*pw_multi_aff_product(
4415 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4419 isl_pw_multi_aff
*res
;
4424 n
= pma1
->n
* pma2
->n
;
4425 space
= isl_space_product(isl_space_copy(pma1
->dim
),
4426 isl_space_copy(pma2
->dim
));
4427 res
= isl_pw_multi_aff_alloc_size(space
, n
);
4429 for (i
= 0; i
< pma1
->n
; ++i
) {
4430 for (j
= 0; j
< pma2
->n
; ++j
) {
4434 domain
= isl_set_product(isl_set_copy(pma1
->p
[i
].set
),
4435 isl_set_copy(pma2
->p
[j
].set
));
4436 ma
= isl_multi_aff_product(
4437 isl_multi_aff_copy(pma1
->p
[i
].maff
),
4438 isl_multi_aff_copy(pma2
->p
[j
].maff
));
4439 res
= isl_pw_multi_aff_add_piece(res
, domain
, ma
);
4443 isl_pw_multi_aff_free(pma1
);
4444 isl_pw_multi_aff_free(pma2
);
4447 isl_pw_multi_aff_free(pma1
);
4448 isl_pw_multi_aff_free(pma2
);
4452 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_product(
4453 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4455 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
4456 &pw_multi_aff_product
);
4459 /* Construct a map mapping the domain of the piecewise multi-affine expression
4460 * to its range, with each dimension in the range equated to the
4461 * corresponding affine expression on its cell.
4463 * If the domain of "pma" is rational, then so is the constructed "map".
4465 __isl_give isl_map
*isl_map_from_pw_multi_aff(__isl_take isl_pw_multi_aff
*pma
)
4473 map
= isl_map_empty(isl_pw_multi_aff_get_space(pma
));
4475 for (i
= 0; i
< pma
->n
; ++i
) {
4477 isl_multi_aff
*maff
;
4478 isl_basic_map
*bmap
;
4481 rational
= isl_set_is_rational(pma
->p
[i
].set
);
4483 map
= isl_map_free(map
);
4484 maff
= isl_multi_aff_copy(pma
->p
[i
].maff
);
4485 bmap
= isl_basic_map_from_multi_aff2(maff
, rational
);
4486 map_i
= isl_map_from_basic_map(bmap
);
4487 map_i
= isl_map_intersect_domain(map_i
,
4488 isl_set_copy(pma
->p
[i
].set
));
4489 map
= isl_map_union_disjoint(map
, map_i
);
4492 isl_pw_multi_aff_free(pma
);
4496 __isl_give isl_set
*isl_set_from_pw_multi_aff(__isl_take isl_pw_multi_aff
*pma
)
4501 if (!isl_space_is_set(pma
->dim
))
4502 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
4503 "isl_pw_multi_aff cannot be converted into an isl_set",
4506 return isl_map_from_pw_multi_aff(pma
);
4508 isl_pw_multi_aff_free(pma
);
4512 /* Subtract the initial "n" elements in "ma" with coefficients in "c" and
4513 * denominator "denom".
4514 * "denom" is allowed to be negative, in which case the actual denominator
4515 * is -denom and the expressions are added instead.
4517 static __isl_give isl_aff
*subtract_initial(__isl_take isl_aff
*aff
,
4518 __isl_keep isl_multi_aff
*ma
, int n
, isl_int
*c
, isl_int denom
)
4524 first
= isl_seq_first_non_zero(c
, n
);
4528 sign
= isl_int_sgn(denom
);
4530 isl_int_abs(d
, denom
);
4531 for (i
= first
; i
< n
; ++i
) {
4534 if (isl_int_is_zero(c
[i
]))
4536 aff_i
= isl_multi_aff_get_aff(ma
, i
);
4537 aff_i
= isl_aff_scale(aff_i
, c
[i
]);
4538 aff_i
= isl_aff_scale_down(aff_i
, d
);
4540 aff
= isl_aff_sub(aff
, aff_i
);
4542 aff
= isl_aff_add(aff
, aff_i
);
4549 /* Extract an affine expression that expresses the output dimension "pos"
4550 * of "bmap" in terms of the parameters and input dimensions from
4552 * Note that this expression may involve integer divisions defined
4553 * in terms of parameters and input dimensions.
4554 * The equality may also involve references to earlier (but not later)
4555 * output dimensions. These are replaced by the corresponding elements
4558 * If the equality is of the form
4560 * f(i) + h(j) + a x + g(i) = 0,
4562 * with f(i) a linear combinations of the parameters and input dimensions,
4563 * g(i) a linear combination of integer divisions defined in terms of the same
4564 * and h(j) a linear combinations of earlier output dimensions,
4565 * then the affine expression is
4567 * (-f(i) - g(i))/a - h(j)/a
4569 * If the equality is of the form
4571 * f(i) + h(j) - a x + g(i) = 0,
4573 * then the affine expression is
4575 * (f(i) + g(i))/a - h(j)/(-a)
4578 * If "div" refers to an integer division (i.e., it is smaller than
4579 * the number of integer divisions), then the equality constraint
4580 * does involve an integer division (the one at position "div") that
4581 * is defined in terms of output dimensions. However, this integer
4582 * division can be eliminated by exploiting a pair of constraints
4583 * x >= l and x <= l + n, with n smaller than the coefficient of "div"
4584 * in the equality constraint. "ineq" refers to inequality x >= l, i.e.,
4586 * In particular, let
4588 * x = e(i) + m floor(...)
4590 * with e(i) the expression derived above and floor(...) the integer
4591 * division involving output dimensions.
4602 * e(i) + m floor(...) - l = (e(i) + m floor(...) - l) mod m
4603 * = (e(i) - l) mod m
4607 * x - l = (e(i) - l) mod m
4611 * x = ((e(i) - l) mod m) + l
4613 * The variable "shift" below contains the expression -l, which may
4614 * also involve a linear combination of earlier output dimensions.
4616 static __isl_give isl_aff
*extract_aff_from_equality(
4617 __isl_keep isl_basic_map
*bmap
, int pos
, int eq
, int div
, int ineq
,
4618 __isl_keep isl_multi_aff
*ma
)
4621 unsigned n_div
, n_out
;
4623 isl_local_space
*ls
;
4624 isl_aff
*aff
, *shift
;
4627 ctx
= isl_basic_map_get_ctx(bmap
);
4628 ls
= isl_basic_map_get_local_space(bmap
);
4629 ls
= isl_local_space_domain(ls
);
4630 aff
= isl_aff_alloc(isl_local_space_copy(ls
));
4633 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
4634 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4635 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4636 if (isl_int_is_neg(bmap
->eq
[eq
][o_out
+ pos
])) {
4637 isl_seq_cpy(aff
->v
->el
+ 1, bmap
->eq
[eq
], o_out
);
4638 isl_seq_cpy(aff
->v
->el
+ 1 + o_out
,
4639 bmap
->eq
[eq
] + o_out
+ n_out
, n_div
);
4641 isl_seq_neg(aff
->v
->el
+ 1, bmap
->eq
[eq
], o_out
);
4642 isl_seq_neg(aff
->v
->el
+ 1 + o_out
,
4643 bmap
->eq
[eq
] + o_out
+ n_out
, n_div
);
4646 isl_int_set_si(aff
->v
->el
[1 + o_out
+ div
], 0);
4647 isl_int_abs(aff
->v
->el
[0], bmap
->eq
[eq
][o_out
+ pos
]);
4648 aff
= subtract_initial(aff
, ma
, pos
, bmap
->eq
[eq
] + o_out
,
4649 bmap
->eq
[eq
][o_out
+ pos
]);
4651 shift
= isl_aff_alloc(isl_local_space_copy(ls
));
4654 isl_seq_cpy(shift
->v
->el
+ 1, bmap
->ineq
[ineq
], o_out
);
4655 isl_seq_cpy(shift
->v
->el
+ 1 + o_out
,
4656 bmap
->ineq
[ineq
] + o_out
+ n_out
, n_div
);
4657 isl_int_set_si(shift
->v
->el
[0], 1);
4658 shift
= subtract_initial(shift
, ma
, pos
,
4659 bmap
->ineq
[ineq
] + o_out
, ctx
->negone
);
4660 aff
= isl_aff_add(aff
, isl_aff_copy(shift
));
4661 mod
= isl_val_int_from_isl_int(ctx
,
4662 bmap
->eq
[eq
][o_out
+ n_out
+ div
]);
4663 mod
= isl_val_abs(mod
);
4664 aff
= isl_aff_mod_val(aff
, mod
);
4665 aff
= isl_aff_sub(aff
, shift
);
4668 isl_local_space_free(ls
);
4671 isl_local_space_free(ls
);
4676 /* Given a basic map with output dimensions defined
4677 * in terms of the parameters input dimensions and earlier
4678 * output dimensions using an equality (and possibly a pair on inequalities),
4679 * extract an isl_aff that expresses output dimension "pos" in terms
4680 * of the parameters and input dimensions.
4681 * Note that this expression may involve integer divisions defined
4682 * in terms of parameters and input dimensions.
4683 * "ma" contains the expressions corresponding to earlier output dimensions.
4685 * This function shares some similarities with
4686 * isl_basic_map_has_defining_equality and isl_constraint_get_bound.
4688 static __isl_give isl_aff
*extract_isl_aff_from_basic_map(
4689 __isl_keep isl_basic_map
*bmap
, int pos
, __isl_keep isl_multi_aff
*ma
)
4696 eq
= isl_basic_map_output_defining_equality(bmap
, pos
, &div
, &ineq
);
4697 if (eq
>= bmap
->n_eq
)
4698 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
4699 "unable to find suitable equality", return NULL
);
4700 aff
= extract_aff_from_equality(bmap
, pos
, eq
, div
, ineq
, ma
);
4702 aff
= isl_aff_remove_unused_divs(aff
);
4706 /* Given a basic map where each output dimension is defined
4707 * in terms of the parameters and input dimensions using an equality,
4708 * extract an isl_multi_aff that expresses the output dimensions in terms
4709 * of the parameters and input dimensions.
4711 static __isl_give isl_multi_aff
*extract_isl_multi_aff_from_basic_map(
4712 __isl_take isl_basic_map
*bmap
)
4721 ma
= isl_multi_aff_alloc(isl_basic_map_get_space(bmap
));
4722 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4724 for (i
= 0; i
< n_out
; ++i
) {
4727 aff
= extract_isl_aff_from_basic_map(bmap
, i
, ma
);
4728 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
4731 isl_basic_map_free(bmap
);
4736 /* Given a basic set where each set dimension is defined
4737 * in terms of the parameters using an equality,
4738 * extract an isl_multi_aff that expresses the set dimensions in terms
4739 * of the parameters.
4741 __isl_give isl_multi_aff
*isl_multi_aff_from_basic_set_equalities(
4742 __isl_take isl_basic_set
*bset
)
4744 return extract_isl_multi_aff_from_basic_map(bset
);
4747 /* Create an isl_pw_multi_aff that is equivalent to
4748 * isl_map_intersect_domain(isl_map_from_basic_map(bmap), domain).
4749 * The given basic map is such that each output dimension is defined
4750 * in terms of the parameters and input dimensions using an equality.
4752 * Since some applications expect the result of isl_pw_multi_aff_from_map
4753 * to only contain integer affine expressions, we compute the floor
4754 * of the expression before returning.
4756 * Remove all constraints involving local variables without
4757 * an explicit representation (resulting in the removal of those
4758 * local variables) prior to the actual extraction to ensure
4759 * that the local spaces in which the resulting affine expressions
4760 * are created do not contain any unknown local variables.
4761 * Removing such constraints is safe because constraints involving
4762 * unknown local variables are not used to determine whether
4763 * a basic map is obviously single-valued.
4765 static __isl_give isl_pw_multi_aff
*plain_pw_multi_aff_from_map(
4766 __isl_take isl_set
*domain
, __isl_take isl_basic_map
*bmap
)
4770 bmap
= isl_basic_map_drop_constraint_involving_unknown_divs(bmap
);
4771 ma
= extract_isl_multi_aff_from_basic_map(bmap
);
4772 ma
= isl_multi_aff_floor(ma
);
4773 return isl_pw_multi_aff_alloc(domain
, ma
);
4776 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4777 * This obviously only works if the input "map" is single-valued.
4778 * If so, we compute the lexicographic minimum of the image in the form
4779 * of an isl_pw_multi_aff. Since the image is unique, it is equal
4780 * to its lexicographic minimum.
4781 * If the input is not single-valued, we produce an error.
4783 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_base(
4784 __isl_take isl_map
*map
)
4788 isl_pw_multi_aff
*pma
;
4790 sv
= isl_map_is_single_valued(map
);
4794 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
4795 "map is not single-valued", goto error
);
4796 map
= isl_map_make_disjoint(map
);
4800 pma
= isl_pw_multi_aff_empty(isl_map_get_space(map
));
4802 for (i
= 0; i
< map
->n
; ++i
) {
4803 isl_pw_multi_aff
*pma_i
;
4804 isl_basic_map
*bmap
;
4805 bmap
= isl_basic_map_copy(map
->p
[i
]);
4806 pma_i
= isl_basic_map_lexmin_pw_multi_aff(bmap
);
4807 pma
= isl_pw_multi_aff_add_disjoint(pma
, pma_i
);
4817 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
4818 * taking into account that the output dimension at position "d"
4819 * can be represented as
4821 * x = floor((e(...) + c1) / m)
4823 * given that constraint "i" is of the form
4825 * e(...) + c1 - m x >= 0
4828 * Let "map" be of the form
4832 * We construct a mapping
4834 * A -> [A -> x = floor(...)]
4836 * apply that to the map, obtaining
4838 * [A -> x = floor(...)] -> B
4840 * and equate dimension "d" to x.
4841 * We then compute a isl_pw_multi_aff representation of the resulting map
4842 * and plug in the mapping above.
4844 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_div(
4845 __isl_take isl_map
*map
, __isl_take isl_basic_map
*hull
, int d
, int i
)
4849 isl_local_space
*ls
;
4857 isl_pw_multi_aff
*pma
;
4860 is_set
= isl_map_is_set(map
);
4864 offset
= isl_basic_map_offset(hull
, isl_dim_out
);
4865 ctx
= isl_map_get_ctx(map
);
4866 space
= isl_space_domain(isl_map_get_space(map
));
4867 n_in
= isl_space_dim(space
, isl_dim_set
);
4868 n
= isl_space_dim(space
, isl_dim_all
);
4870 v
= isl_vec_alloc(ctx
, 1 + 1 + n
);
4872 isl_int_neg(v
->el
[0], hull
->ineq
[i
][offset
+ d
]);
4873 isl_seq_cpy(v
->el
+ 1, hull
->ineq
[i
], 1 + n
);
4875 isl_basic_map_free(hull
);
4877 ls
= isl_local_space_from_space(isl_space_copy(space
));
4878 aff
= isl_aff_alloc_vec(ls
, v
);
4879 aff
= isl_aff_floor(aff
);
4881 isl_space_free(space
);
4882 ma
= isl_multi_aff_from_aff(aff
);
4884 ma
= isl_multi_aff_identity(isl_space_map_from_set(space
));
4885 ma
= isl_multi_aff_range_product(ma
,
4886 isl_multi_aff_from_aff(aff
));
4889 insert
= isl_map_from_multi_aff(isl_multi_aff_copy(ma
));
4890 map
= isl_map_apply_domain(map
, insert
);
4891 map
= isl_map_equate(map
, isl_dim_in
, n_in
, isl_dim_out
, d
);
4892 pma
= isl_pw_multi_aff_from_map(map
);
4893 pma
= isl_pw_multi_aff_pullback_multi_aff(pma
, ma
);
4898 isl_basic_map_free(hull
);
4902 /* Is constraint "c" of the form
4904 * e(...) + c1 - m x >= 0
4908 * -e(...) + c2 + m x >= 0
4910 * where m > 1 and e only depends on parameters and input dimemnsions?
4912 * "offset" is the offset of the output dimensions
4913 * "pos" is the position of output dimension x.
4915 static int is_potential_div_constraint(isl_int
*c
, int offset
, int d
, int total
)
4917 if (isl_int_is_zero(c
[offset
+ d
]))
4919 if (isl_int_is_one(c
[offset
+ d
]))
4921 if (isl_int_is_negone(c
[offset
+ d
]))
4923 if (isl_seq_first_non_zero(c
+ offset
, d
) != -1)
4925 if (isl_seq_first_non_zero(c
+ offset
+ d
+ 1,
4926 total
- (offset
+ d
+ 1)) != -1)
4931 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4933 * As a special case, we first check if there is any pair of constraints,
4934 * shared by all the basic maps in "map" that force a given dimension
4935 * to be equal to the floor of some affine combination of the input dimensions.
4937 * In particular, if we can find two constraints
4939 * e(...) + c1 - m x >= 0 i.e., m x <= e(...) + c1
4943 * -e(...) + c2 + m x >= 0 i.e., m x >= e(...) - c2
4945 * where m > 1 and e only depends on parameters and input dimemnsions,
4948 * c1 + c2 < m i.e., -c2 >= c1 - (m - 1)
4950 * then we know that we can take
4952 * x = floor((e(...) + c1) / m)
4954 * without having to perform any computation.
4956 * Note that we know that
4960 * If c1 + c2 were 0, then we would have detected an equality during
4961 * simplification. If c1 + c2 were negative, then we would have detected
4964 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_check_div(
4965 __isl_take isl_map
*map
)
4971 isl_basic_map
*hull
;
4973 hull
= isl_map_unshifted_simple_hull(isl_map_copy(map
));
4978 dim
= isl_map_dim(map
, isl_dim_out
);
4979 offset
= isl_basic_map_offset(hull
, isl_dim_out
);
4980 total
= 1 + isl_basic_map_total_dim(hull
);
4982 for (d
= 0; d
< dim
; ++d
) {
4983 for (i
= 0; i
< n
; ++i
) {
4984 if (!is_potential_div_constraint(hull
->ineq
[i
],
4987 for (j
= i
+ 1; j
< n
; ++j
) {
4988 if (!isl_seq_is_neg(hull
->ineq
[i
] + 1,
4989 hull
->ineq
[j
] + 1, total
- 1))
4991 isl_int_add(sum
, hull
->ineq
[i
][0],
4993 if (isl_int_abs_lt(sum
,
4994 hull
->ineq
[i
][offset
+ d
]))
5001 if (isl_int_is_pos(hull
->ineq
[j
][offset
+ d
]))
5003 return pw_multi_aff_from_map_div(map
, hull
, d
, j
);
5007 isl_basic_map_free(hull
);
5008 return pw_multi_aff_from_map_base(map
);
5011 isl_basic_map_free(hull
);
5015 /* Given an affine expression
5017 * [A -> B] -> f(A,B)
5019 * construct an isl_multi_aff
5023 * such that dimension "d" in B' is set to "aff" and the remaining
5024 * dimensions are set equal to the corresponding dimensions in B.
5025 * "n_in" is the dimension of the space A.
5026 * "n_out" is the dimension of the space B.
5028 * If "is_set" is set, then the affine expression is of the form
5032 * and we construct an isl_multi_aff
5036 static __isl_give isl_multi_aff
*range_map(__isl_take isl_aff
*aff
, int d
,
5037 unsigned n_in
, unsigned n_out
, int is_set
)
5041 isl_space
*space
, *space2
;
5042 isl_local_space
*ls
;
5044 space
= isl_aff_get_domain_space(aff
);
5045 ls
= isl_local_space_from_space(isl_space_copy(space
));
5046 space2
= isl_space_copy(space
);
5048 space2
= isl_space_range(isl_space_unwrap(space2
));
5049 space
= isl_space_map_from_domain_and_range(space
, space2
);
5050 ma
= isl_multi_aff_alloc(space
);
5051 ma
= isl_multi_aff_set_aff(ma
, d
, aff
);
5053 for (i
= 0; i
< n_out
; ++i
) {
5056 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
5057 isl_dim_set
, n_in
+ i
);
5058 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
5061 isl_local_space_free(ls
);
5066 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
5067 * taking into account that the dimension at position "d" can be written as
5069 * x = m a + f(..) (1)
5071 * where m is equal to "gcd".
5072 * "i" is the index of the equality in "hull" that defines f(..).
5073 * In particular, the equality is of the form
5075 * f(..) - x + m g(existentials) = 0
5079 * -f(..) + x + m g(existentials) = 0
5081 * We basically plug (1) into "map", resulting in a map with "a"
5082 * in the range instead of "x". The corresponding isl_pw_multi_aff
5083 * defining "a" is then plugged back into (1) to obtain a definition for "x".
5085 * Specifically, given the input map
5089 * We first wrap it into a set
5093 * and define (1) on top of the corresponding space, resulting in "aff".
5094 * We use this to create an isl_multi_aff that maps the output position "d"
5095 * from "a" to "x", leaving all other (intput and output) dimensions unchanged.
5096 * We plug this into the wrapped map, unwrap the result and compute the
5097 * corresponding isl_pw_multi_aff.
5098 * The result is an expression
5106 * so that we can plug that into "aff", after extending the latter to
5112 * If "map" is actually a set, then there is no "A" space, meaning
5113 * that we do not need to perform any wrapping, and that the result
5114 * of the recursive call is of the form
5118 * which is plugged into a mapping of the form
5122 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_stride(
5123 __isl_take isl_map
*map
, __isl_take isl_basic_map
*hull
, int d
, int i
,
5128 isl_local_space
*ls
;
5131 isl_pw_multi_aff
*pma
, *id
;
5137 is_set
= isl_map_is_set(map
);
5141 n_in
= isl_basic_map_dim(hull
, isl_dim_in
);
5142 n_out
= isl_basic_map_dim(hull
, isl_dim_out
);
5143 o_out
= isl_basic_map_offset(hull
, isl_dim_out
);
5148 set
= isl_map_wrap(map
);
5149 space
= isl_space_map_from_set(isl_set_get_space(set
));
5150 ma
= isl_multi_aff_identity(space
);
5151 ls
= isl_local_space_from_space(isl_set_get_space(set
));
5152 aff
= isl_aff_alloc(ls
);
5154 isl_int_set_si(aff
->v
->el
[0], 1);
5155 if (isl_int_is_one(hull
->eq
[i
][o_out
+ d
]))
5156 isl_seq_neg(aff
->v
->el
+ 1, hull
->eq
[i
],
5159 isl_seq_cpy(aff
->v
->el
+ 1, hull
->eq
[i
],
5161 isl_int_set(aff
->v
->el
[1 + o_out
+ d
], gcd
);
5163 ma
= isl_multi_aff_set_aff(ma
, n_in
+ d
, isl_aff_copy(aff
));
5164 set
= isl_set_preimage_multi_aff(set
, ma
);
5166 ma
= range_map(aff
, d
, n_in
, n_out
, is_set
);
5171 map
= isl_set_unwrap(set
);
5172 pma
= isl_pw_multi_aff_from_map(map
);
5175 space
= isl_pw_multi_aff_get_domain_space(pma
);
5176 space
= isl_space_map_from_set(space
);
5177 id
= isl_pw_multi_aff_identity(space
);
5178 pma
= isl_pw_multi_aff_range_product(id
, pma
);
5180 id
= isl_pw_multi_aff_from_multi_aff(ma
);
5181 pma
= isl_pw_multi_aff_pullback_pw_multi_aff(id
, pma
);
5183 isl_basic_map_free(hull
);
5187 isl_basic_map_free(hull
);
5191 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5192 * "hull" contains the equalities valid for "map".
5194 * Check if any of the output dimensions is "strided".
5195 * That is, we check if it can be written as
5199 * with m greater than 1, a some combination of existentially quantified
5200 * variables and f an expression in the parameters and input dimensions.
5201 * If so, we remove the stride in pw_multi_aff_from_map_stride.
5203 * Otherwise, we continue with pw_multi_aff_from_map_check_div for a further
5206 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_check_strides(
5207 __isl_take isl_map
*map
, __isl_take isl_basic_map
*hull
)
5216 n_div
= isl_basic_map_dim(hull
, isl_dim_div
);
5217 o_div
= isl_basic_map_offset(hull
, isl_dim_div
);
5220 isl_basic_map_free(hull
);
5221 return pw_multi_aff_from_map_check_div(map
);
5226 n_out
= isl_basic_map_dim(hull
, isl_dim_out
);
5227 o_out
= isl_basic_map_offset(hull
, isl_dim_out
);
5229 for (i
= 0; i
< n_out
; ++i
) {
5230 for (j
= 0; j
< hull
->n_eq
; ++j
) {
5231 isl_int
*eq
= hull
->eq
[j
];
5232 isl_pw_multi_aff
*res
;
5234 if (!isl_int_is_one(eq
[o_out
+ i
]) &&
5235 !isl_int_is_negone(eq
[o_out
+ i
]))
5237 if (isl_seq_first_non_zero(eq
+ o_out
, i
) != -1)
5239 if (isl_seq_first_non_zero(eq
+ o_out
+ i
+ 1,
5240 n_out
- (i
+ 1)) != -1)
5242 isl_seq_gcd(eq
+ o_div
, n_div
, &gcd
);
5243 if (isl_int_is_zero(gcd
))
5245 if (isl_int_is_one(gcd
))
5248 res
= pw_multi_aff_from_map_stride(map
, hull
,
5256 isl_basic_map_free(hull
);
5257 return pw_multi_aff_from_map_check_div(map
);
5260 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5262 * As a special case, we first check if all output dimensions are uniquely
5263 * defined in terms of the parameters and input dimensions over the entire
5264 * domain. If so, we extract the desired isl_pw_multi_aff directly
5265 * from the affine hull of "map" and its domain.
5267 * Otherwise, continue with pw_multi_aff_from_map_check_strides for more
5270 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_map(__isl_take isl_map
*map
)
5273 isl_basic_map
*hull
;
5278 if (isl_map_n_basic_map(map
) == 1) {
5279 hull
= isl_map_unshifted_simple_hull(isl_map_copy(map
));
5280 hull
= isl_basic_map_plain_affine_hull(hull
);
5281 sv
= isl_basic_map_plain_is_single_valued(hull
);
5283 return plain_pw_multi_aff_from_map(isl_map_domain(map
),
5285 isl_basic_map_free(hull
);
5287 map
= isl_map_detect_equalities(map
);
5288 hull
= isl_map_unshifted_simple_hull(isl_map_copy(map
));
5289 sv
= isl_basic_map_plain_is_single_valued(hull
);
5291 return plain_pw_multi_aff_from_map(isl_map_domain(map
), hull
);
5293 return pw_multi_aff_from_map_check_strides(map
, hull
);
5294 isl_basic_map_free(hull
);
5299 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_set(__isl_take isl_set
*set
)
5301 return isl_pw_multi_aff_from_map(set
);
5304 /* Convert "map" into an isl_pw_multi_aff (if possible) and
5307 static isl_stat
pw_multi_aff_from_map(__isl_take isl_map
*map
, void *user
)
5309 isl_union_pw_multi_aff
**upma
= user
;
5310 isl_pw_multi_aff
*pma
;
5312 pma
= isl_pw_multi_aff_from_map(map
);
5313 *upma
= isl_union_pw_multi_aff_add_pw_multi_aff(*upma
, pma
);
5315 return *upma
? isl_stat_ok
: isl_stat_error
;
5318 /* Create an isl_union_pw_multi_aff with the given isl_aff on a universe
5321 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_aff(
5322 __isl_take isl_aff
*aff
)
5325 isl_pw_multi_aff
*pma
;
5327 ma
= isl_multi_aff_from_aff(aff
);
5328 pma
= isl_pw_multi_aff_from_multi_aff(ma
);
5329 return isl_union_pw_multi_aff_from_pw_multi_aff(pma
);
5332 /* Try and create an isl_union_pw_multi_aff that is equivalent
5333 * to the given isl_union_map.
5334 * The isl_union_map is required to be single-valued in each space.
5335 * Otherwise, an error is produced.
5337 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_union_map(
5338 __isl_take isl_union_map
*umap
)
5341 isl_union_pw_multi_aff
*upma
;
5343 space
= isl_union_map_get_space(umap
);
5344 upma
= isl_union_pw_multi_aff_empty(space
);
5345 if (isl_union_map_foreach_map(umap
, &pw_multi_aff_from_map
, &upma
) < 0)
5346 upma
= isl_union_pw_multi_aff_free(upma
);
5347 isl_union_map_free(umap
);
5352 /* Try and create an isl_union_pw_multi_aff that is equivalent
5353 * to the given isl_union_set.
5354 * The isl_union_set is required to be a singleton in each space.
5355 * Otherwise, an error is produced.
5357 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_union_set(
5358 __isl_take isl_union_set
*uset
)
5360 return isl_union_pw_multi_aff_from_union_map(uset
);
5363 /* Return the piecewise affine expression "set ? 1 : 0".
5365 __isl_give isl_pw_aff
*isl_set_indicator_function(__isl_take isl_set
*set
)
5368 isl_space
*space
= isl_set_get_space(set
);
5369 isl_local_space
*ls
= isl_local_space_from_space(space
);
5370 isl_aff
*zero
= isl_aff_zero_on_domain(isl_local_space_copy(ls
));
5371 isl_aff
*one
= isl_aff_zero_on_domain(ls
);
5373 one
= isl_aff_add_constant_si(one
, 1);
5374 pa
= isl_pw_aff_alloc(isl_set_copy(set
), one
);
5375 set
= isl_set_complement(set
);
5376 pa
= isl_pw_aff_add_disjoint(pa
, isl_pw_aff_alloc(set
, zero
));
5381 /* Plug in "subs" for dimension "type", "pos" of "aff".
5383 * Let i be the dimension to replace and let "subs" be of the form
5387 * and "aff" of the form
5393 * (a f + d g')/(m d)
5395 * where g' is the result of plugging in "subs" in each of the integer
5398 __isl_give isl_aff
*isl_aff_substitute(__isl_take isl_aff
*aff
,
5399 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
5404 aff
= isl_aff_cow(aff
);
5406 return isl_aff_free(aff
);
5408 ctx
= isl_aff_get_ctx(aff
);
5409 if (!isl_space_is_equal(aff
->ls
->dim
, subs
->ls
->dim
))
5410 isl_die(ctx
, isl_error_invalid
,
5411 "spaces don't match", return isl_aff_free(aff
));
5412 if (isl_local_space_dim(subs
->ls
, isl_dim_div
) != 0)
5413 isl_die(ctx
, isl_error_unsupported
,
5414 "cannot handle divs yet", return isl_aff_free(aff
));
5416 aff
->ls
= isl_local_space_substitute(aff
->ls
, type
, pos
, subs
);
5418 return isl_aff_free(aff
);
5420 aff
->v
= isl_vec_cow(aff
->v
);
5422 return isl_aff_free(aff
);
5424 pos
+= isl_local_space_offset(aff
->ls
, type
);
5427 isl_seq_substitute(aff
->v
->el
, pos
, subs
->v
->el
,
5428 aff
->v
->size
, subs
->v
->size
, v
);
5434 /* Plug in "subs" for dimension "type", "pos" in each of the affine
5435 * expressions in "maff".
5437 __isl_give isl_multi_aff
*isl_multi_aff_substitute(
5438 __isl_take isl_multi_aff
*maff
, enum isl_dim_type type
, unsigned pos
,
5439 __isl_keep isl_aff
*subs
)
5443 maff
= isl_multi_aff_cow(maff
);
5445 return isl_multi_aff_free(maff
);
5447 if (type
== isl_dim_in
)
5450 for (i
= 0; i
< maff
->n
; ++i
) {
5451 maff
->u
.p
[i
] = isl_aff_substitute(maff
->u
.p
[i
],
5454 return isl_multi_aff_free(maff
);
5460 /* Plug in "subs" for dimension "type", "pos" of "pma".
5462 * pma is of the form
5466 * while subs is of the form
5468 * v' = B_j(v) -> S_j
5470 * Each pair i,j such that C_ij = A_i \cap B_i is non-empty
5471 * has a contribution in the result, in particular
5473 * C_ij(S_j) -> M_i(S_j)
5475 * Note that plugging in S_j in C_ij may also result in an empty set
5476 * and this contribution should simply be discarded.
5478 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_substitute(
5479 __isl_take isl_pw_multi_aff
*pma
, enum isl_dim_type type
, unsigned pos
,
5480 __isl_keep isl_pw_aff
*subs
)
5483 isl_pw_multi_aff
*res
;
5486 return isl_pw_multi_aff_free(pma
);
5488 n
= pma
->n
* subs
->n
;
5489 res
= isl_pw_multi_aff_alloc_size(isl_space_copy(pma
->dim
), n
);
5491 for (i
= 0; i
< pma
->n
; ++i
) {
5492 for (j
= 0; j
< subs
->n
; ++j
) {
5494 isl_multi_aff
*res_ij
;
5497 common
= isl_set_intersect(
5498 isl_set_copy(pma
->p
[i
].set
),
5499 isl_set_copy(subs
->p
[j
].set
));
5500 common
= isl_set_substitute(common
,
5501 type
, pos
, subs
->p
[j
].aff
);
5502 empty
= isl_set_plain_is_empty(common
);
5503 if (empty
< 0 || empty
) {
5504 isl_set_free(common
);
5510 res_ij
= isl_multi_aff_substitute(
5511 isl_multi_aff_copy(pma
->p
[i
].maff
),
5512 type
, pos
, subs
->p
[j
].aff
);
5514 res
= isl_pw_multi_aff_add_piece(res
, common
, res_ij
);
5518 isl_pw_multi_aff_free(pma
);
5521 isl_pw_multi_aff_free(pma
);
5522 isl_pw_multi_aff_free(res
);
5526 /* Compute the preimage of a range of dimensions in the affine expression "src"
5527 * under "ma" and put the result in "dst". The number of dimensions in "src"
5528 * that precede the range is given by "n_before". The number of dimensions
5529 * in the range is given by the number of output dimensions of "ma".
5530 * The number of dimensions that follow the range is given by "n_after".
5531 * If "has_denom" is set (to one),
5532 * then "src" and "dst" have an extra initial denominator.
5533 * "n_div_ma" is the number of existentials in "ma"
5534 * "n_div_bset" is the number of existentials in "src"
5535 * The resulting "dst" (which is assumed to have been allocated by
5536 * the caller) contains coefficients for both sets of existentials,
5537 * first those in "ma" and then those in "src".
5538 * f, c1, c2 and g are temporary objects that have been initialized
5541 * Let src represent the expression
5543 * (a(p) + f_u u + b v + f_w w + c(divs))/d
5545 * and let ma represent the expressions
5547 * v_i = (r_i(p) + s_i(y) + t_i(divs'))/m_i
5549 * We start out with the following expression for dst:
5551 * (a(p) + f_u u + 0 y + f_w w + 0 divs' + c(divs) + f \sum_i b_i v_i)/d
5553 * with the multiplication factor f initially equal to 1
5554 * and f \sum_i b_i v_i kept separately.
5555 * For each x_i that we substitute, we multiply the numerator
5556 * (and denominator) of dst by c_1 = m_i and add the numerator
5557 * of the x_i expression multiplied by c_2 = f b_i,
5558 * after removing the common factors of c_1 and c_2.
5559 * The multiplication factor f also needs to be multiplied by c_1
5560 * for the next x_j, j > i.
5562 void isl_seq_preimage(isl_int
*dst
, isl_int
*src
,
5563 __isl_keep isl_multi_aff
*ma
, int n_before
, int n_after
,
5564 int n_div_ma
, int n_div_bmap
,
5565 isl_int f
, isl_int c1
, isl_int c2
, isl_int g
, int has_denom
)
5568 int n_param
, n_in
, n_out
;
5571 n_param
= isl_multi_aff_dim(ma
, isl_dim_param
);
5572 n_in
= isl_multi_aff_dim(ma
, isl_dim_in
);
5573 n_out
= isl_multi_aff_dim(ma
, isl_dim_out
);
5575 isl_seq_cpy(dst
, src
, has_denom
+ 1 + n_param
+ n_before
);
5576 o_dst
= o_src
= has_denom
+ 1 + n_param
+ n_before
;
5577 isl_seq_clr(dst
+ o_dst
, n_in
);
5580 isl_seq_cpy(dst
+ o_dst
, src
+ o_src
, n_after
);
5583 isl_seq_clr(dst
+ o_dst
, n_div_ma
);
5585 isl_seq_cpy(dst
+ o_dst
, src
+ o_src
, n_div_bmap
);
5587 isl_int_set_si(f
, 1);
5589 for (i
= 0; i
< n_out
; ++i
) {
5590 int offset
= has_denom
+ 1 + n_param
+ n_before
+ i
;
5592 if (isl_int_is_zero(src
[offset
]))
5594 isl_int_set(c1
, ma
->u
.p
[i
]->v
->el
[0]);
5595 isl_int_mul(c2
, f
, src
[offset
]);
5596 isl_int_gcd(g
, c1
, c2
);
5597 isl_int_divexact(c1
, c1
, g
);
5598 isl_int_divexact(c2
, c2
, g
);
5600 isl_int_mul(f
, f
, c1
);
5603 isl_seq_combine(dst
+ o_dst
, c1
, dst
+ o_dst
,
5604 c2
, ma
->u
.p
[i
]->v
->el
+ o_src
, 1 + n_param
);
5605 o_dst
+= 1 + n_param
;
5606 o_src
+= 1 + n_param
;
5607 isl_seq_scale(dst
+ o_dst
, dst
+ o_dst
, c1
, n_before
);
5609 isl_seq_combine(dst
+ o_dst
, c1
, dst
+ o_dst
,
5610 c2
, ma
->u
.p
[i
]->v
->el
+ o_src
, n_in
);
5613 isl_seq_scale(dst
+ o_dst
, dst
+ o_dst
, c1
, n_after
);
5615 isl_seq_combine(dst
+ o_dst
, c1
, dst
+ o_dst
,
5616 c2
, ma
->u
.p
[i
]->v
->el
+ o_src
, n_div_ma
);
5619 isl_seq_scale(dst
+ o_dst
, dst
+ o_dst
, c1
, n_div_bmap
);
5621 isl_int_mul(dst
[0], dst
[0], c1
);
5625 /* Compute the pullback of "aff" by the function represented by "ma".
5626 * In other words, plug in "ma" in "aff". The result is an affine expression
5627 * defined over the domain space of "ma".
5629 * If "aff" is represented by
5631 * (a(p) + b x + c(divs))/d
5633 * and ma is represented by
5635 * x = D(p) + F(y) + G(divs')
5637 * then the result is
5639 * (a(p) + b D(p) + b F(y) + b G(divs') + c(divs))/d
5641 * The divs in the local space of the input are similarly adjusted
5642 * through a call to isl_local_space_preimage_multi_aff.
5644 __isl_give isl_aff
*isl_aff_pullback_multi_aff(__isl_take isl_aff
*aff
,
5645 __isl_take isl_multi_aff
*ma
)
5647 isl_aff
*res
= NULL
;
5648 isl_local_space
*ls
;
5649 int n_div_aff
, n_div_ma
;
5650 isl_int f
, c1
, c2
, g
;
5652 ma
= isl_multi_aff_align_divs(ma
);
5656 n_div_aff
= isl_aff_dim(aff
, isl_dim_div
);
5657 n_div_ma
= ma
->n
? isl_aff_dim(ma
->u
.p
[0], isl_dim_div
) : 0;
5659 ls
= isl_aff_get_domain_local_space(aff
);
5660 ls
= isl_local_space_preimage_multi_aff(ls
, isl_multi_aff_copy(ma
));
5661 res
= isl_aff_alloc(ls
);
5670 isl_seq_preimage(res
->v
->el
, aff
->v
->el
, ma
, 0, 0, n_div_ma
, n_div_aff
,
5679 isl_multi_aff_free(ma
);
5680 res
= isl_aff_normalize(res
);
5684 isl_multi_aff_free(ma
);
5689 /* Compute the pullback of "aff1" by the function represented by "aff2".
5690 * In other words, plug in "aff2" in "aff1". The result is an affine expression
5691 * defined over the domain space of "aff1".
5693 * The domain of "aff1" should match the range of "aff2", which means
5694 * that it should be single-dimensional.
5696 __isl_give isl_aff
*isl_aff_pullback_aff(__isl_take isl_aff
*aff1
,
5697 __isl_take isl_aff
*aff2
)
5701 ma
= isl_multi_aff_from_aff(aff2
);
5702 return isl_aff_pullback_multi_aff(aff1
, ma
);
5705 /* Compute the pullback of "ma1" by the function represented by "ma2".
5706 * In other words, plug in "ma2" in "ma1".
5708 * The parameters of "ma1" and "ma2" are assumed to have been aligned.
5710 static __isl_give isl_multi_aff
*isl_multi_aff_pullback_multi_aff_aligned(
5711 __isl_take isl_multi_aff
*ma1
, __isl_take isl_multi_aff
*ma2
)
5714 isl_space
*space
= NULL
;
5716 ma2
= isl_multi_aff_align_divs(ma2
);
5717 ma1
= isl_multi_aff_cow(ma1
);
5721 space
= isl_space_join(isl_multi_aff_get_space(ma2
),
5722 isl_multi_aff_get_space(ma1
));
5724 for (i
= 0; i
< ma1
->n
; ++i
) {
5725 ma1
->u
.p
[i
] = isl_aff_pullback_multi_aff(ma1
->u
.p
[i
],
5726 isl_multi_aff_copy(ma2
));
5731 ma1
= isl_multi_aff_reset_space(ma1
, space
);
5732 isl_multi_aff_free(ma2
);
5735 isl_space_free(space
);
5736 isl_multi_aff_free(ma2
);
5737 isl_multi_aff_free(ma1
);
5741 /* Compute the pullback of "ma1" by the function represented by "ma2".
5742 * In other words, plug in "ma2" in "ma1".
5744 __isl_give isl_multi_aff
*isl_multi_aff_pullback_multi_aff(
5745 __isl_take isl_multi_aff
*ma1
, __isl_take isl_multi_aff
*ma2
)
5747 return isl_multi_aff_align_params_multi_multi_and(ma1
, ma2
,
5748 &isl_multi_aff_pullback_multi_aff_aligned
);
5751 /* Extend the local space of "dst" to include the divs
5752 * in the local space of "src".
5754 * If "src" does not have any divs or if the local spaces of "dst" and
5755 * "src" are the same, then no extension is required.
5757 __isl_give isl_aff
*isl_aff_align_divs(__isl_take isl_aff
*dst
,
5758 __isl_keep isl_aff
*src
)
5761 int src_n_div
, dst_n_div
;
5768 return isl_aff_free(dst
);
5770 ctx
= isl_aff_get_ctx(src
);
5771 equal
= isl_local_space_has_equal_space(src
->ls
, dst
->ls
);
5773 return isl_aff_free(dst
);
5775 isl_die(ctx
, isl_error_invalid
,
5776 "spaces don't match", goto error
);
5778 src_n_div
= isl_local_space_dim(src
->ls
, isl_dim_div
);
5781 equal
= isl_local_space_is_equal(src
->ls
, dst
->ls
);
5783 return isl_aff_free(dst
);
5787 dst_n_div
= isl_local_space_dim(dst
->ls
, isl_dim_div
);
5788 exp1
= isl_alloc_array(ctx
, int, src_n_div
);
5789 exp2
= isl_alloc_array(ctx
, int, dst_n_div
);
5790 if (!exp1
|| (dst_n_div
&& !exp2
))
5793 div
= isl_merge_divs(src
->ls
->div
, dst
->ls
->div
, exp1
, exp2
);
5794 dst
= isl_aff_expand_divs(dst
, div
, exp2
);
5802 return isl_aff_free(dst
);
5805 /* Adjust the local spaces of the affine expressions in "maff"
5806 * such that they all have the save divs.
5808 __isl_give isl_multi_aff
*isl_multi_aff_align_divs(
5809 __isl_take isl_multi_aff
*maff
)
5817 maff
= isl_multi_aff_cow(maff
);
5821 for (i
= 1; i
< maff
->n
; ++i
)
5822 maff
->u
.p
[0] = isl_aff_align_divs(maff
->u
.p
[0], maff
->u
.p
[i
]);
5823 for (i
= 1; i
< maff
->n
; ++i
) {
5824 maff
->u
.p
[i
] = isl_aff_align_divs(maff
->u
.p
[i
], maff
->u
.p
[0]);
5826 return isl_multi_aff_free(maff
);
5832 __isl_give isl_aff
*isl_aff_lift(__isl_take isl_aff
*aff
)
5834 aff
= isl_aff_cow(aff
);
5838 aff
->ls
= isl_local_space_lift(aff
->ls
);
5840 return isl_aff_free(aff
);
5845 /* Lift "maff" to a space with extra dimensions such that the result
5846 * has no more existentially quantified variables.
5847 * If "ls" is not NULL, then *ls is assigned the local space that lies
5848 * at the basis of the lifting applied to "maff".
5850 __isl_give isl_multi_aff
*isl_multi_aff_lift(__isl_take isl_multi_aff
*maff
,
5851 __isl_give isl_local_space
**ls
)
5865 isl_space
*space
= isl_multi_aff_get_domain_space(maff
);
5866 *ls
= isl_local_space_from_space(space
);
5868 return isl_multi_aff_free(maff
);
5873 maff
= isl_multi_aff_cow(maff
);
5874 maff
= isl_multi_aff_align_divs(maff
);
5878 n_div
= isl_aff_dim(maff
->u
.p
[0], isl_dim_div
);
5879 space
= isl_multi_aff_get_space(maff
);
5880 space
= isl_space_lift(isl_space_domain(space
), n_div
);
5881 space
= isl_space_extend_domain_with_range(space
,
5882 isl_multi_aff_get_space(maff
));
5884 return isl_multi_aff_free(maff
);
5885 isl_space_free(maff
->space
);
5886 maff
->space
= space
;
5889 *ls
= isl_aff_get_domain_local_space(maff
->u
.p
[0]);
5891 return isl_multi_aff_free(maff
);
5894 for (i
= 0; i
< maff
->n
; ++i
) {
5895 maff
->u
.p
[i
] = isl_aff_lift(maff
->u
.p
[i
]);
5903 isl_local_space_free(*ls
);
5904 return isl_multi_aff_free(maff
);
5908 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma".
5910 __isl_give isl_pw_aff
*isl_pw_multi_aff_get_pw_aff(
5911 __isl_keep isl_pw_multi_aff
*pma
, int pos
)
5921 n_out
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
5922 if (pos
< 0 || pos
>= n_out
)
5923 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
5924 "index out of bounds", return NULL
);
5926 space
= isl_pw_multi_aff_get_space(pma
);
5927 space
= isl_space_drop_dims(space
, isl_dim_out
,
5928 pos
+ 1, n_out
- pos
- 1);
5929 space
= isl_space_drop_dims(space
, isl_dim_out
, 0, pos
);
5931 pa
= isl_pw_aff_alloc_size(space
, pma
->n
);
5932 for (i
= 0; i
< pma
->n
; ++i
) {
5934 aff
= isl_multi_aff_get_aff(pma
->p
[i
].maff
, pos
);
5935 pa
= isl_pw_aff_add_piece(pa
, isl_set_copy(pma
->p
[i
].set
), aff
);
5941 /* Return an isl_pw_multi_aff with the given "set" as domain and
5942 * an unnamed zero-dimensional range.
5944 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_domain(
5945 __isl_take isl_set
*set
)
5950 space
= isl_set_get_space(set
);
5951 space
= isl_space_from_domain(space
);
5952 ma
= isl_multi_aff_zero(space
);
5953 return isl_pw_multi_aff_alloc(set
, ma
);
5956 /* Add an isl_pw_multi_aff with the given "set" as domain and
5957 * an unnamed zero-dimensional range to *user.
5959 static isl_stat
add_pw_multi_aff_from_domain(__isl_take isl_set
*set
,
5962 isl_union_pw_multi_aff
**upma
= user
;
5963 isl_pw_multi_aff
*pma
;
5965 pma
= isl_pw_multi_aff_from_domain(set
);
5966 *upma
= isl_union_pw_multi_aff_add_pw_multi_aff(*upma
, pma
);
5971 /* Return an isl_union_pw_multi_aff with the given "uset" as domain and
5972 * an unnamed zero-dimensional range.
5974 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_domain(
5975 __isl_take isl_union_set
*uset
)
5978 isl_union_pw_multi_aff
*upma
;
5983 space
= isl_union_set_get_space(uset
);
5984 upma
= isl_union_pw_multi_aff_empty(space
);
5986 if (isl_union_set_foreach_set(uset
,
5987 &add_pw_multi_aff_from_domain
, &upma
) < 0)
5990 isl_union_set_free(uset
);
5993 isl_union_set_free(uset
);
5994 isl_union_pw_multi_aff_free(upma
);
5998 /* Convert "pma" to an isl_map and add it to *umap.
6000 static isl_stat
map_from_pw_multi_aff(__isl_take isl_pw_multi_aff
*pma
,
6003 isl_union_map
**umap
= user
;
6006 map
= isl_map_from_pw_multi_aff(pma
);
6007 *umap
= isl_union_map_add_map(*umap
, map
);
6012 /* Construct a union map mapping the domain of the union
6013 * piecewise multi-affine expression to its range, with each dimension
6014 * in the range equated to the corresponding affine expression on its cell.
6016 __isl_give isl_union_map
*isl_union_map_from_union_pw_multi_aff(
6017 __isl_take isl_union_pw_multi_aff
*upma
)
6020 isl_union_map
*umap
;
6025 space
= isl_union_pw_multi_aff_get_space(upma
);
6026 umap
= isl_union_map_empty(space
);
6028 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma
,
6029 &map_from_pw_multi_aff
, &umap
) < 0)
6032 isl_union_pw_multi_aff_free(upma
);
6035 isl_union_pw_multi_aff_free(upma
);
6036 isl_union_map_free(umap
);
6040 /* Local data for bin_entry and the callback "fn".
6042 struct isl_union_pw_multi_aff_bin_data
{
6043 isl_union_pw_multi_aff
*upma2
;
6044 isl_union_pw_multi_aff
*res
;
6045 isl_pw_multi_aff
*pma
;
6046 isl_stat (*fn
)(__isl_take isl_pw_multi_aff
*pma
, void *user
);
6049 /* Given an isl_pw_multi_aff from upma1, store it in data->pma
6050 * and call data->fn for each isl_pw_multi_aff in data->upma2.
6052 static isl_stat
bin_entry(__isl_take isl_pw_multi_aff
*pma
, void *user
)
6054 struct isl_union_pw_multi_aff_bin_data
*data
= user
;
6058 r
= isl_union_pw_multi_aff_foreach_pw_multi_aff(data
->upma2
,
6060 isl_pw_multi_aff_free(pma
);
6065 /* Call "fn" on each pair of isl_pw_multi_affs in "upma1" and "upma2".
6066 * The isl_pw_multi_aff from upma1 is stored in data->pma (where data is
6067 * passed as user field) and the isl_pw_multi_aff from upma2 is available
6068 * as *entry. The callback should adjust data->res if desired.
6070 static __isl_give isl_union_pw_multi_aff
*bin_op(
6071 __isl_take isl_union_pw_multi_aff
*upma1
,
6072 __isl_take isl_union_pw_multi_aff
*upma2
,
6073 isl_stat (*fn
)(__isl_take isl_pw_multi_aff
*pma
, void *user
))
6076 struct isl_union_pw_multi_aff_bin_data data
= { NULL
, NULL
, NULL
, fn
};
6078 space
= isl_union_pw_multi_aff_get_space(upma2
);
6079 upma1
= isl_union_pw_multi_aff_align_params(upma1
, space
);
6080 space
= isl_union_pw_multi_aff_get_space(upma1
);
6081 upma2
= isl_union_pw_multi_aff_align_params(upma2
, space
);
6083 if (!upma1
|| !upma2
)
6087 data
.res
= isl_union_pw_multi_aff_alloc_same_size(upma1
);
6088 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma1
,
6089 &bin_entry
, &data
) < 0)
6092 isl_union_pw_multi_aff_free(upma1
);
6093 isl_union_pw_multi_aff_free(upma2
);
6096 isl_union_pw_multi_aff_free(upma1
);
6097 isl_union_pw_multi_aff_free(upma2
);
6098 isl_union_pw_multi_aff_free(data
.res
);
6102 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
6103 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
6105 static __isl_give isl_pw_multi_aff
*pw_multi_aff_range_product(
6106 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
6110 space
= isl_space_range_product(isl_pw_multi_aff_get_space(pma1
),
6111 isl_pw_multi_aff_get_space(pma2
));
6112 return isl_pw_multi_aff_on_shared_domain_in(pma1
, pma2
, space
,
6113 &isl_multi_aff_range_product
);
6116 /* Given two isl_pw_multi_affs A -> B and C -> D,
6117 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
6119 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_range_product(
6120 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
6122 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
6123 &pw_multi_aff_range_product
);
6126 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
6127 * construct an isl_pw_multi_aff (A * C) -> (B, D).
6129 static __isl_give isl_pw_multi_aff
*pw_multi_aff_flat_range_product(
6130 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
6134 space
= isl_space_range_product(isl_pw_multi_aff_get_space(pma1
),
6135 isl_pw_multi_aff_get_space(pma2
));
6136 space
= isl_space_flatten_range(space
);
6137 return isl_pw_multi_aff_on_shared_domain_in(pma1
, pma2
, space
,
6138 &isl_multi_aff_flat_range_product
);
6141 /* Given two isl_pw_multi_affs A -> B and C -> D,
6142 * construct an isl_pw_multi_aff (A * C) -> (B, D).
6144 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_flat_range_product(
6145 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
6147 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
6148 &pw_multi_aff_flat_range_product
);
6151 /* If data->pma and "pma2" have the same domain space, then compute
6152 * their flat range product and the result to data->res.
6154 static isl_stat
flat_range_product_entry(__isl_take isl_pw_multi_aff
*pma2
,
6157 struct isl_union_pw_multi_aff_bin_data
*data
= user
;
6159 if (!isl_space_tuple_is_equal(data
->pma
->dim
, isl_dim_in
,
6160 pma2
->dim
, isl_dim_in
)) {
6161 isl_pw_multi_aff_free(pma2
);
6165 pma2
= isl_pw_multi_aff_flat_range_product(
6166 isl_pw_multi_aff_copy(data
->pma
), pma2
);
6168 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma2
);
6173 /* Given two isl_union_pw_multi_affs A -> B and C -> D,
6174 * construct an isl_union_pw_multi_aff (A * C) -> (B, D).
6176 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_flat_range_product(
6177 __isl_take isl_union_pw_multi_aff
*upma1
,
6178 __isl_take isl_union_pw_multi_aff
*upma2
)
6180 return bin_op(upma1
, upma2
, &flat_range_product_entry
);
6183 /* Replace the affine expressions at position "pos" in "pma" by "pa".
6184 * The parameters are assumed to have been aligned.
6186 * The implementation essentially performs an isl_pw_*_on_shared_domain,
6187 * except that it works on two different isl_pw_* types.
6189 static __isl_give isl_pw_multi_aff
*pw_multi_aff_set_pw_aff(
6190 __isl_take isl_pw_multi_aff
*pma
, unsigned pos
,
6191 __isl_take isl_pw_aff
*pa
)
6194 isl_pw_multi_aff
*res
= NULL
;
6199 if (!isl_space_tuple_is_equal(pma
->dim
, isl_dim_in
,
6200 pa
->dim
, isl_dim_in
))
6201 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
6202 "domains don't match", goto error
);
6203 if (pos
>= isl_pw_multi_aff_dim(pma
, isl_dim_out
))
6204 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
6205 "index out of bounds", goto error
);
6208 res
= isl_pw_multi_aff_alloc_size(isl_pw_multi_aff_get_space(pma
), n
);
6210 for (i
= 0; i
< pma
->n
; ++i
) {
6211 for (j
= 0; j
< pa
->n
; ++j
) {
6213 isl_multi_aff
*res_ij
;
6216 common
= isl_set_intersect(isl_set_copy(pma
->p
[i
].set
),
6217 isl_set_copy(pa
->p
[j
].set
));
6218 empty
= isl_set_plain_is_empty(common
);
6219 if (empty
< 0 || empty
) {
6220 isl_set_free(common
);
6226 res_ij
= isl_multi_aff_set_aff(
6227 isl_multi_aff_copy(pma
->p
[i
].maff
), pos
,
6228 isl_aff_copy(pa
->p
[j
].aff
));
6229 res_ij
= isl_multi_aff_gist(res_ij
,
6230 isl_set_copy(common
));
6232 res
= isl_pw_multi_aff_add_piece(res
, common
, res_ij
);
6236 isl_pw_multi_aff_free(pma
);
6237 isl_pw_aff_free(pa
);
6240 isl_pw_multi_aff_free(pma
);
6241 isl_pw_aff_free(pa
);
6242 return isl_pw_multi_aff_free(res
);
6245 /* Replace the affine expressions at position "pos" in "pma" by "pa".
6247 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_set_pw_aff(
6248 __isl_take isl_pw_multi_aff
*pma
, unsigned pos
,
6249 __isl_take isl_pw_aff
*pa
)
6251 isl_bool equal_params
;
6255 equal_params
= isl_space_has_equal_params(pma
->dim
, pa
->dim
);
6256 if (equal_params
< 0)
6259 return pw_multi_aff_set_pw_aff(pma
, pos
, pa
);
6260 if (isl_pw_multi_aff_check_named_params(pma
) < 0 ||
6261 isl_pw_aff_check_named_params(pa
) < 0)
6263 pma
= isl_pw_multi_aff_align_params(pma
, isl_pw_aff_get_space(pa
));
6264 pa
= isl_pw_aff_align_params(pa
, isl_pw_multi_aff_get_space(pma
));
6265 return pw_multi_aff_set_pw_aff(pma
, pos
, pa
);
6267 isl_pw_multi_aff_free(pma
);
6268 isl_pw_aff_free(pa
);
6272 /* Do the parameters of "pa" match those of "space"?
6274 isl_bool
isl_pw_aff_matching_params(__isl_keep isl_pw_aff
*pa
,
6275 __isl_keep isl_space
*space
)
6277 isl_space
*pa_space
;
6281 return isl_bool_error
;
6283 pa_space
= isl_pw_aff_get_space(pa
);
6285 match
= isl_space_has_equal_params(space
, pa_space
);
6287 isl_space_free(pa_space
);
6291 /* Check that the domain space of "pa" matches "space".
6293 isl_stat
isl_pw_aff_check_match_domain_space(__isl_keep isl_pw_aff
*pa
,
6294 __isl_keep isl_space
*space
)
6296 isl_space
*pa_space
;
6300 return isl_stat_error
;
6302 pa_space
= isl_pw_aff_get_space(pa
);
6304 match
= isl_space_has_equal_params(space
, pa_space
);
6308 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
6309 "parameters don't match", goto error
);
6310 match
= isl_space_tuple_is_equal(space
, isl_dim_in
,
6311 pa_space
, isl_dim_in
);
6315 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
6316 "domains don't match", goto error
);
6317 isl_space_free(pa_space
);
6320 isl_space_free(pa_space
);
6321 return isl_stat_error
;
6329 #include <isl_multi_explicit_domain.c>
6330 #include <isl_multi_pw_aff_explicit_domain.c>
6331 #include <isl_multi_templ.c>
6332 #include <isl_multi_apply_set.c>
6333 #include <isl_multi_coalesce.c>
6334 #include <isl_multi_dims.c>
6335 #include <isl_multi_gist.c>
6336 #include <isl_multi_hash.c>
6337 #include <isl_multi_align_set.c>
6338 #include <isl_multi_intersect.c>
6340 /* Does "mpa" have a non-trivial explicit domain?
6342 * The explicit domain, if present, is trivial if it represents
6343 * an (obviously) universe set.
6345 isl_bool
isl_multi_pw_aff_has_non_trivial_domain(
6346 __isl_keep isl_multi_pw_aff
*mpa
)
6349 return isl_bool_error
;
6350 if (!isl_multi_pw_aff_has_explicit_domain(mpa
))
6351 return isl_bool_false
;
6352 return isl_bool_not(isl_set_plain_is_universe(mpa
->u
.dom
));
6355 /* Scale the elements of "pma" by the corresponding elements of "mv".
6357 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_scale_multi_val(
6358 __isl_take isl_pw_multi_aff
*pma
, __isl_take isl_multi_val
*mv
)
6361 isl_bool equal_params
;
6363 pma
= isl_pw_multi_aff_cow(pma
);
6366 if (!isl_space_tuple_is_equal(pma
->dim
, isl_dim_out
,
6367 mv
->space
, isl_dim_set
))
6368 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
6369 "spaces don't match", goto error
);
6370 equal_params
= isl_space_has_equal_params(pma
->dim
, mv
->space
);
6371 if (equal_params
< 0)
6373 if (!equal_params
) {
6374 pma
= isl_pw_multi_aff_align_params(pma
,
6375 isl_multi_val_get_space(mv
));
6376 mv
= isl_multi_val_align_params(mv
,
6377 isl_pw_multi_aff_get_space(pma
));
6382 for (i
= 0; i
< pma
->n
; ++i
) {
6383 pma
->p
[i
].maff
= isl_multi_aff_scale_multi_val(pma
->p
[i
].maff
,
6384 isl_multi_val_copy(mv
));
6385 if (!pma
->p
[i
].maff
)
6389 isl_multi_val_free(mv
);
6392 isl_multi_val_free(mv
);
6393 isl_pw_multi_aff_free(pma
);
6397 /* This function is called for each entry of an isl_union_pw_multi_aff.
6398 * If the space of the entry matches that of data->mv,
6399 * then apply isl_pw_multi_aff_scale_multi_val and return the result.
6400 * Otherwise, return an empty isl_pw_multi_aff.
6402 static __isl_give isl_pw_multi_aff
*union_pw_multi_aff_scale_multi_val_entry(
6403 __isl_take isl_pw_multi_aff
*pma
, void *user
)
6405 isl_multi_val
*mv
= user
;
6409 if (!isl_space_tuple_is_equal(pma
->dim
, isl_dim_out
,
6410 mv
->space
, isl_dim_set
)) {
6411 isl_space
*space
= isl_pw_multi_aff_get_space(pma
);
6412 isl_pw_multi_aff_free(pma
);
6413 return isl_pw_multi_aff_empty(space
);
6416 return isl_pw_multi_aff_scale_multi_val(pma
, isl_multi_val_copy(mv
));
6419 /* Scale the elements of "upma" by the corresponding elements of "mv",
6420 * for those entries that match the space of "mv".
6422 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_scale_multi_val(
6423 __isl_take isl_union_pw_multi_aff
*upma
, __isl_take isl_multi_val
*mv
)
6425 upma
= isl_union_pw_multi_aff_align_params(upma
,
6426 isl_multi_val_get_space(mv
));
6427 mv
= isl_multi_val_align_params(mv
,
6428 isl_union_pw_multi_aff_get_space(upma
));
6432 return isl_union_pw_multi_aff_transform(upma
,
6433 &union_pw_multi_aff_scale_multi_val_entry
, mv
);
6435 isl_multi_val_free(mv
);
6438 isl_multi_val_free(mv
);
6439 isl_union_pw_multi_aff_free(upma
);
6443 /* Construct and return a piecewise multi affine expression
6444 * in the given space with value zero in each of the output dimensions and
6445 * a universe domain.
6447 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_zero(__isl_take isl_space
*space
)
6449 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_zero(space
));
6452 /* Construct and return a piecewise multi affine expression
6453 * that is equal to the given piecewise affine expression.
6455 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_pw_aff(
6456 __isl_take isl_pw_aff
*pa
)
6460 isl_pw_multi_aff
*pma
;
6465 space
= isl_pw_aff_get_space(pa
);
6466 pma
= isl_pw_multi_aff_alloc_size(space
, pa
->n
);
6468 for (i
= 0; i
< pa
->n
; ++i
) {
6472 set
= isl_set_copy(pa
->p
[i
].set
);
6473 ma
= isl_multi_aff_from_aff(isl_aff_copy(pa
->p
[i
].aff
));
6474 pma
= isl_pw_multi_aff_add_piece(pma
, set
, ma
);
6477 isl_pw_aff_free(pa
);
6481 /* Construct a set or map mapping the shared (parameter) domain
6482 * of the piecewise affine expressions to the range of "mpa"
6483 * with each dimension in the range equated to the
6484 * corresponding piecewise affine expression.
6486 static __isl_give isl_map
*map_from_multi_pw_aff(
6487 __isl_take isl_multi_pw_aff
*mpa
)
6496 if (isl_space_dim(mpa
->space
, isl_dim_out
) != mpa
->n
)
6497 isl_die(isl_multi_pw_aff_get_ctx(mpa
), isl_error_internal
,
6498 "invalid space", goto error
);
6500 space
= isl_multi_pw_aff_get_domain_space(mpa
);
6501 map
= isl_map_universe(isl_space_from_domain(space
));
6503 for (i
= 0; i
< mpa
->n
; ++i
) {
6507 pa
= isl_pw_aff_copy(mpa
->u
.p
[i
]);
6508 map_i
= map_from_pw_aff(pa
);
6510 map
= isl_map_flat_range_product(map
, map_i
);
6513 map
= isl_map_reset_space(map
, isl_multi_pw_aff_get_space(mpa
));
6515 isl_multi_pw_aff_free(mpa
);
6518 isl_multi_pw_aff_free(mpa
);
6522 /* Construct a map mapping the shared domain
6523 * of the piecewise affine expressions to the range of "mpa"
6524 * with each dimension in the range equated to the
6525 * corresponding piecewise affine expression.
6527 __isl_give isl_map
*isl_map_from_multi_pw_aff(__isl_take isl_multi_pw_aff
*mpa
)
6531 if (isl_space_is_set(mpa
->space
))
6532 isl_die(isl_multi_pw_aff_get_ctx(mpa
), isl_error_internal
,
6533 "space of input is not a map", goto error
);
6535 return map_from_multi_pw_aff(mpa
);
6537 isl_multi_pw_aff_free(mpa
);
6541 /* Construct a set mapping the shared parameter domain
6542 * of the piecewise affine expressions to the space of "mpa"
6543 * with each dimension in the range equated to the
6544 * corresponding piecewise affine expression.
6546 __isl_give isl_set
*isl_set_from_multi_pw_aff(__isl_take isl_multi_pw_aff
*mpa
)
6550 if (!isl_space_is_set(mpa
->space
))
6551 isl_die(isl_multi_pw_aff_get_ctx(mpa
), isl_error_internal
,
6552 "space of input is not a set", goto error
);
6554 return map_from_multi_pw_aff(mpa
);
6556 isl_multi_pw_aff_free(mpa
);
6560 /* Construct and return a piecewise multi affine expression
6561 * that is equal to the given multi piecewise affine expression
6562 * on the shared domain of the piecewise affine expressions,
6563 * in the special case of a 0D multi piecewise affine expression.
6565 * Create a piecewise multi affine expression with the explicit domain of
6566 * the 0D multi piecewise affine expression as domain.
6568 static __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_multi_pw_aff_0D(
6569 __isl_take isl_multi_pw_aff
*mpa
)
6575 space
= isl_multi_pw_aff_get_space(mpa
);
6576 dom
= isl_multi_pw_aff_get_explicit_domain(mpa
);
6577 isl_multi_pw_aff_free(mpa
);
6579 ma
= isl_multi_aff_zero(space
);
6580 return isl_pw_multi_aff_alloc(dom
, ma
);
6583 /* Construct and return a piecewise multi affine expression
6584 * that is equal to the given multi piecewise affine expression
6585 * on the shared domain of the piecewise affine expressions.
6587 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_multi_pw_aff(
6588 __isl_take isl_multi_pw_aff
*mpa
)
6593 isl_pw_multi_aff
*pma
;
6599 return isl_pw_multi_aff_from_multi_pw_aff_0D(mpa
);
6601 space
= isl_multi_pw_aff_get_space(mpa
);
6602 pa
= isl_multi_pw_aff_get_pw_aff(mpa
, 0);
6603 pma
= isl_pw_multi_aff_from_pw_aff(pa
);
6605 for (i
= 1; i
< mpa
->n
; ++i
) {
6606 isl_pw_multi_aff
*pma_i
;
6608 pa
= isl_multi_pw_aff_get_pw_aff(mpa
, i
);
6609 pma_i
= isl_pw_multi_aff_from_pw_aff(pa
);
6610 pma
= isl_pw_multi_aff_range_product(pma
, pma_i
);
6613 pma
= isl_pw_multi_aff_reset_space(pma
, space
);
6615 isl_multi_pw_aff_free(mpa
);
6619 /* Construct and return a multi piecewise affine expression
6620 * that is equal to the given multi affine expression.
6622 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_from_multi_aff(
6623 __isl_take isl_multi_aff
*ma
)
6626 isl_multi_pw_aff
*mpa
;
6631 n
= isl_multi_aff_dim(ma
, isl_dim_out
);
6632 mpa
= isl_multi_pw_aff_alloc(isl_multi_aff_get_space(ma
));
6634 for (i
= 0; i
< n
; ++i
) {
6637 pa
= isl_pw_aff_from_aff(isl_multi_aff_get_aff(ma
, i
));
6638 mpa
= isl_multi_pw_aff_set_pw_aff(mpa
, i
, pa
);
6641 isl_multi_aff_free(ma
);
6645 /* Construct and return a multi piecewise affine expression
6646 * that is equal to the given piecewise multi affine expression.
6648 * If the resulting multi piecewise affine expression has
6649 * an explicit domain, then assign it the domain of the input.
6650 * In other cases, the domain is stored in the individual elements.
6652 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_from_pw_multi_aff(
6653 __isl_take isl_pw_multi_aff
*pma
)
6657 isl_multi_pw_aff
*mpa
;
6662 n
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
6663 space
= isl_pw_multi_aff_get_space(pma
);
6664 mpa
= isl_multi_pw_aff_alloc(space
);
6666 for (i
= 0; i
< n
; ++i
) {
6669 pa
= isl_pw_multi_aff_get_pw_aff(pma
, i
);
6670 mpa
= isl_multi_pw_aff_set_pw_aff(mpa
, i
, pa
);
6672 if (isl_multi_pw_aff_has_explicit_domain(mpa
)) {
6675 dom
= isl_pw_multi_aff_domain(isl_pw_multi_aff_copy(pma
));
6676 mpa
= isl_multi_pw_aff_intersect_domain(mpa
, dom
);
6679 isl_pw_multi_aff_free(pma
);
6683 /* Do "pa1" and "pa2" represent the same function?
6685 * We first check if they are obviously equal.
6686 * If not, we convert them to maps and check if those are equal.
6688 * If "pa1" or "pa2" contain any NaNs, then they are considered
6689 * not to be the same. A NaN is not equal to anything, not even
6692 isl_bool
isl_pw_aff_is_equal(__isl_keep isl_pw_aff
*pa1
,
6693 __isl_keep isl_pw_aff
*pa2
)
6697 isl_map
*map1
, *map2
;
6700 return isl_bool_error
;
6702 equal
= isl_pw_aff_plain_is_equal(pa1
, pa2
);
6703 if (equal
< 0 || equal
)
6705 has_nan
= either_involves_nan(pa1
, pa2
);
6707 return isl_bool_error
;
6709 return isl_bool_false
;
6711 map1
= map_from_pw_aff(isl_pw_aff_copy(pa1
));
6712 map2
= map_from_pw_aff(isl_pw_aff_copy(pa2
));
6713 equal
= isl_map_is_equal(map1
, map2
);
6720 /* Do "mpa1" and "mpa2" represent the same function?
6722 * Note that we cannot convert the entire isl_multi_pw_aff
6723 * to a map because the domains of the piecewise affine expressions
6724 * may not be the same.
6726 isl_bool
isl_multi_pw_aff_is_equal(__isl_keep isl_multi_pw_aff
*mpa1
,
6727 __isl_keep isl_multi_pw_aff
*mpa2
)
6730 isl_bool equal
, equal_params
;
6733 return isl_bool_error
;
6735 equal_params
= isl_space_has_equal_params(mpa1
->space
, mpa2
->space
);
6736 if (equal_params
< 0)
6737 return isl_bool_error
;
6738 if (!equal_params
) {
6739 if (!isl_space_has_named_params(mpa1
->space
))
6740 return isl_bool_false
;
6741 if (!isl_space_has_named_params(mpa2
->space
))
6742 return isl_bool_false
;
6743 mpa1
= isl_multi_pw_aff_copy(mpa1
);
6744 mpa2
= isl_multi_pw_aff_copy(mpa2
);
6745 mpa1
= isl_multi_pw_aff_align_params(mpa1
,
6746 isl_multi_pw_aff_get_space(mpa2
));
6747 mpa2
= isl_multi_pw_aff_align_params(mpa2
,
6748 isl_multi_pw_aff_get_space(mpa1
));
6749 equal
= isl_multi_pw_aff_is_equal(mpa1
, mpa2
);
6750 isl_multi_pw_aff_free(mpa1
);
6751 isl_multi_pw_aff_free(mpa2
);
6755 equal
= isl_space_is_equal(mpa1
->space
, mpa2
->space
);
6756 if (equal
< 0 || !equal
)
6759 for (i
= 0; i
< mpa1
->n
; ++i
) {
6760 equal
= isl_pw_aff_is_equal(mpa1
->u
.p
[i
], mpa2
->u
.p
[i
]);
6761 if (equal
< 0 || !equal
)
6765 return isl_bool_true
;
6768 /* Do "pma1" and "pma2" represent the same function?
6770 * First check if they are obviously equal.
6771 * If not, then convert them to maps and check if those are equal.
6773 * If "pa1" or "pa2" contain any NaNs, then they are considered
6774 * not to be the same. A NaN is not equal to anything, not even
6777 isl_bool
isl_pw_multi_aff_is_equal(__isl_keep isl_pw_multi_aff
*pma1
,
6778 __isl_keep isl_pw_multi_aff
*pma2
)
6782 isl_map
*map1
, *map2
;
6785 return isl_bool_error
;
6787 equal
= isl_pw_multi_aff_plain_is_equal(pma1
, pma2
);
6788 if (equal
< 0 || equal
)
6790 has_nan
= isl_pw_multi_aff_involves_nan(pma1
);
6791 if (has_nan
>= 0 && !has_nan
)
6792 has_nan
= isl_pw_multi_aff_involves_nan(pma2
);
6793 if (has_nan
< 0 || has_nan
)
6794 return isl_bool_not(has_nan
);
6796 map1
= isl_map_from_pw_multi_aff(isl_pw_multi_aff_copy(pma1
));
6797 map2
= isl_map_from_pw_multi_aff(isl_pw_multi_aff_copy(pma2
));
6798 equal
= isl_map_is_equal(map1
, map2
);
6805 /* Compute the pullback of "mpa" by the function represented by "ma".
6806 * In other words, plug in "ma" in "mpa".
6808 * The parameters of "mpa" and "ma" are assumed to have been aligned.
6810 * If "mpa" has an explicit domain, then it is this domain
6811 * that needs to undergo a pullback, i.e., a preimage.
6813 static __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_pullback_multi_aff_aligned(
6814 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_multi_aff
*ma
)
6817 isl_space
*space
= NULL
;
6819 mpa
= isl_multi_pw_aff_cow(mpa
);
6823 space
= isl_space_join(isl_multi_aff_get_space(ma
),
6824 isl_multi_pw_aff_get_space(mpa
));
6828 for (i
= 0; i
< mpa
->n
; ++i
) {
6829 mpa
->u
.p
[i
] = isl_pw_aff_pullback_multi_aff(mpa
->u
.p
[i
],
6830 isl_multi_aff_copy(ma
));
6834 if (isl_multi_pw_aff_has_explicit_domain(mpa
)) {
6835 mpa
->u
.dom
= isl_set_preimage_multi_aff(mpa
->u
.dom
,
6836 isl_multi_aff_copy(ma
));
6841 isl_multi_aff_free(ma
);
6842 isl_space_free(mpa
->space
);
6846 isl_space_free(space
);
6847 isl_multi_pw_aff_free(mpa
);
6848 isl_multi_aff_free(ma
);
6852 /* Compute the pullback of "mpa" by the function represented by "ma".
6853 * In other words, plug in "ma" in "mpa".
6855 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_pullback_multi_aff(
6856 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_multi_aff
*ma
)
6858 isl_bool equal_params
;
6862 equal_params
= isl_space_has_equal_params(mpa
->space
, ma
->space
);
6863 if (equal_params
< 0)
6866 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa
, ma
);
6867 mpa
= isl_multi_pw_aff_align_params(mpa
, isl_multi_aff_get_space(ma
));
6868 ma
= isl_multi_aff_align_params(ma
, isl_multi_pw_aff_get_space(mpa
));
6869 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa
, ma
);
6871 isl_multi_pw_aff_free(mpa
);
6872 isl_multi_aff_free(ma
);
6876 /* Compute the pullback of "mpa" by the function represented by "pma".
6877 * In other words, plug in "pma" in "mpa".
6879 * The parameters of "mpa" and "mpa" are assumed to have been aligned.
6881 * If "mpa" has an explicit domain, then it is this domain
6882 * that needs to undergo a pullback, i.e., a preimage.
6884 static __isl_give isl_multi_pw_aff
*
6885 isl_multi_pw_aff_pullback_pw_multi_aff_aligned(
6886 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_pw_multi_aff
*pma
)
6889 isl_space
*space
= NULL
;
6891 mpa
= isl_multi_pw_aff_cow(mpa
);
6895 space
= isl_space_join(isl_pw_multi_aff_get_space(pma
),
6896 isl_multi_pw_aff_get_space(mpa
));
6898 for (i
= 0; i
< mpa
->n
; ++i
) {
6899 mpa
->u
.p
[i
] = isl_pw_aff_pullback_pw_multi_aff_aligned(
6900 mpa
->u
.p
[i
], isl_pw_multi_aff_copy(pma
));
6904 if (isl_multi_pw_aff_has_explicit_domain(mpa
)) {
6905 mpa
->u
.dom
= isl_set_preimage_pw_multi_aff(mpa
->u
.dom
,
6906 isl_pw_multi_aff_copy(pma
));
6911 isl_pw_multi_aff_free(pma
);
6912 isl_space_free(mpa
->space
);
6916 isl_space_free(space
);
6917 isl_multi_pw_aff_free(mpa
);
6918 isl_pw_multi_aff_free(pma
);
6922 /* Compute the pullback of "mpa" by the function represented by "pma".
6923 * In other words, plug in "pma" in "mpa".
6925 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_pullback_pw_multi_aff(
6926 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_pw_multi_aff
*pma
)
6928 isl_bool equal_params
;
6932 equal_params
= isl_space_has_equal_params(mpa
->space
, pma
->dim
);
6933 if (equal_params
< 0)
6936 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa
, pma
);
6937 mpa
= isl_multi_pw_aff_align_params(mpa
,
6938 isl_pw_multi_aff_get_space(pma
));
6939 pma
= isl_pw_multi_aff_align_params(pma
,
6940 isl_multi_pw_aff_get_space(mpa
));
6941 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa
, pma
);
6943 isl_multi_pw_aff_free(mpa
);
6944 isl_pw_multi_aff_free(pma
);
6948 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
6949 * with the domain of "aff". The domain of the result is the same
6951 * "mpa" and "aff" are assumed to have been aligned.
6953 * We first extract the parametric constant from "aff", defined
6954 * over the correct domain.
6955 * Then we add the appropriate combinations of the members of "mpa".
6956 * Finally, we add the integer divisions through recursive calls.
6958 static __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_aff_aligned(
6959 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_aff
*aff
)
6967 n_in
= isl_aff_dim(aff
, isl_dim_in
);
6968 n_div
= isl_aff_dim(aff
, isl_dim_div
);
6970 space
= isl_space_domain(isl_multi_pw_aff_get_space(mpa
));
6971 tmp
= isl_aff_copy(aff
);
6972 tmp
= isl_aff_drop_dims(tmp
, isl_dim_div
, 0, n_div
);
6973 tmp
= isl_aff_drop_dims(tmp
, isl_dim_in
, 0, n_in
);
6974 tmp
= isl_aff_add_dims(tmp
, isl_dim_in
,
6975 isl_space_dim(space
, isl_dim_set
));
6976 tmp
= isl_aff_reset_domain_space(tmp
, space
);
6977 pa
= isl_pw_aff_from_aff(tmp
);
6979 for (i
= 0; i
< n_in
; ++i
) {
6982 if (!isl_aff_involves_dims(aff
, isl_dim_in
, i
, 1))
6984 v
= isl_aff_get_coefficient_val(aff
, isl_dim_in
, i
);
6985 pa_i
= isl_multi_pw_aff_get_pw_aff(mpa
, i
);
6986 pa_i
= isl_pw_aff_scale_val(pa_i
, v
);
6987 pa
= isl_pw_aff_add(pa
, pa_i
);
6990 for (i
= 0; i
< n_div
; ++i
) {
6994 if (!isl_aff_involves_dims(aff
, isl_dim_div
, i
, 1))
6996 div
= isl_aff_get_div(aff
, i
);
6997 pa_i
= isl_multi_pw_aff_apply_aff_aligned(
6998 isl_multi_pw_aff_copy(mpa
), div
);
6999 pa_i
= isl_pw_aff_floor(pa_i
);
7000 v
= isl_aff_get_coefficient_val(aff
, isl_dim_div
, i
);
7001 pa_i
= isl_pw_aff_scale_val(pa_i
, v
);
7002 pa
= isl_pw_aff_add(pa
, pa_i
);
7005 isl_multi_pw_aff_free(mpa
);
7011 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
7012 * with the domain of "aff". The domain of the result is the same
7015 __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_aff(
7016 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_aff
*aff
)
7018 isl_bool equal_params
;
7022 equal_params
= isl_space_has_equal_params(aff
->ls
->dim
, mpa
->space
);
7023 if (equal_params
< 0)
7026 return isl_multi_pw_aff_apply_aff_aligned(mpa
, aff
);
7028 aff
= isl_aff_align_params(aff
, isl_multi_pw_aff_get_space(mpa
));
7029 mpa
= isl_multi_pw_aff_align_params(mpa
, isl_aff_get_space(aff
));
7031 return isl_multi_pw_aff_apply_aff_aligned(mpa
, aff
);
7034 isl_multi_pw_aff_free(mpa
);
7038 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
7039 * with the domain of "pa". The domain of the result is the same
7041 * "mpa" and "pa" are assumed to have been aligned.
7043 * We consider each piece in turn. Note that the domains of the
7044 * pieces are assumed to be disjoint and they remain disjoint
7045 * after taking the preimage (over the same function).
7047 static __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_pw_aff_aligned(
7048 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_pw_aff
*pa
)
7057 space
= isl_space_join(isl_multi_pw_aff_get_space(mpa
),
7058 isl_pw_aff_get_space(pa
));
7059 res
= isl_pw_aff_empty(space
);
7061 for (i
= 0; i
< pa
->n
; ++i
) {
7065 pa_i
= isl_multi_pw_aff_apply_aff_aligned(
7066 isl_multi_pw_aff_copy(mpa
),
7067 isl_aff_copy(pa
->p
[i
].aff
));
7068 domain
= isl_set_copy(pa
->p
[i
].set
);
7069 domain
= isl_set_preimage_multi_pw_aff(domain
,
7070 isl_multi_pw_aff_copy(mpa
));
7071 pa_i
= isl_pw_aff_intersect_domain(pa_i
, domain
);
7072 res
= isl_pw_aff_add_disjoint(res
, pa_i
);
7075 isl_pw_aff_free(pa
);
7076 isl_multi_pw_aff_free(mpa
);
7079 isl_pw_aff_free(pa
);
7080 isl_multi_pw_aff_free(mpa
);
7084 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
7085 * with the domain of "pa". The domain of the result is the same
7088 __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_pw_aff(
7089 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_pw_aff
*pa
)
7091 isl_bool equal_params
;
7095 equal_params
= isl_space_has_equal_params(pa
->dim
, mpa
->space
);
7096 if (equal_params
< 0)
7099 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa
, pa
);
7101 pa
= isl_pw_aff_align_params(pa
, isl_multi_pw_aff_get_space(mpa
));
7102 mpa
= isl_multi_pw_aff_align_params(mpa
, isl_pw_aff_get_space(pa
));
7104 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa
, pa
);
7106 isl_pw_aff_free(pa
);
7107 isl_multi_pw_aff_free(mpa
);
7111 /* Compute the pullback of "pa" by the function represented by "mpa".
7112 * In other words, plug in "mpa" in "pa".
7113 * "pa" and "mpa" are assumed to have been aligned.
7115 * The pullback is computed by applying "pa" to "mpa".
7117 static __isl_give isl_pw_aff
*isl_pw_aff_pullback_multi_pw_aff_aligned(
7118 __isl_take isl_pw_aff
*pa
, __isl_take isl_multi_pw_aff
*mpa
)
7120 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa
, pa
);
7123 /* Compute the pullback of "pa" by the function represented by "mpa".
7124 * In other words, plug in "mpa" in "pa".
7126 * The pullback is computed by applying "pa" to "mpa".
7128 __isl_give isl_pw_aff
*isl_pw_aff_pullback_multi_pw_aff(
7129 __isl_take isl_pw_aff
*pa
, __isl_take isl_multi_pw_aff
*mpa
)
7131 return isl_multi_pw_aff_apply_pw_aff(mpa
, pa
);
7134 /* Compute the pullback of "mpa1" by the function represented by "mpa2".
7135 * In other words, plug in "mpa2" in "mpa1".
7137 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7139 * We pullback each member of "mpa1" in turn.
7141 * If "mpa1" has an explicit domain, then it is this domain
7142 * that needs to undergo a pullback instead, i.e., a preimage.
7144 static __isl_give isl_multi_pw_aff
*
7145 isl_multi_pw_aff_pullback_multi_pw_aff_aligned(
7146 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
)
7149 isl_space
*space
= NULL
;
7151 mpa1
= isl_multi_pw_aff_cow(mpa1
);
7155 space
= isl_space_join(isl_multi_pw_aff_get_space(mpa2
),
7156 isl_multi_pw_aff_get_space(mpa1
));
7158 for (i
= 0; i
< mpa1
->n
; ++i
) {
7159 mpa1
->u
.p
[i
] = isl_pw_aff_pullback_multi_pw_aff_aligned(
7160 mpa1
->u
.p
[i
], isl_multi_pw_aff_copy(mpa2
));
7165 if (isl_multi_pw_aff_has_explicit_domain(mpa1
)) {
7166 mpa1
->u
.dom
= isl_set_preimage_multi_pw_aff(mpa1
->u
.dom
,
7167 isl_multi_pw_aff_copy(mpa2
));
7171 mpa1
= isl_multi_pw_aff_reset_space(mpa1
, space
);
7173 isl_multi_pw_aff_free(mpa2
);
7176 isl_space_free(space
);
7177 isl_multi_pw_aff_free(mpa1
);
7178 isl_multi_pw_aff_free(mpa2
);
7182 /* Compute the pullback of "mpa1" by the function represented by "mpa2".
7183 * In other words, plug in "mpa2" in "mpa1".
7185 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_pullback_multi_pw_aff(
7186 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
)
7188 return isl_multi_pw_aff_align_params_multi_multi_and(mpa1
, mpa2
,
7189 &isl_multi_pw_aff_pullback_multi_pw_aff_aligned
);
7192 /* Align the parameters of "mpa1" and "mpa2", check that the ranges
7193 * of "mpa1" and "mpa2" live in the same space, construct map space
7194 * between the domain spaces of "mpa1" and "mpa2" and call "order"
7195 * with this map space as extract argument.
7197 static __isl_give isl_map
*isl_multi_pw_aff_order_map(
7198 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
,
7199 __isl_give isl_map
*(*order
)(__isl_keep isl_multi_pw_aff
*mpa1
,
7200 __isl_keep isl_multi_pw_aff
*mpa2
, __isl_take isl_space
*space
))
7203 isl_space
*space1
, *space2
;
7206 mpa1
= isl_multi_pw_aff_align_params(mpa1
,
7207 isl_multi_pw_aff_get_space(mpa2
));
7208 mpa2
= isl_multi_pw_aff_align_params(mpa2
,
7209 isl_multi_pw_aff_get_space(mpa1
));
7212 match
= isl_space_tuple_is_equal(mpa1
->space
, isl_dim_out
,
7213 mpa2
->space
, isl_dim_out
);
7217 isl_die(isl_multi_pw_aff_get_ctx(mpa1
), isl_error_invalid
,
7218 "range spaces don't match", goto error
);
7219 space1
= isl_space_domain(isl_multi_pw_aff_get_space(mpa1
));
7220 space2
= isl_space_domain(isl_multi_pw_aff_get_space(mpa2
));
7221 space1
= isl_space_map_from_domain_and_range(space1
, space2
);
7223 res
= order(mpa1
, mpa2
, space1
);
7224 isl_multi_pw_aff_free(mpa1
);
7225 isl_multi_pw_aff_free(mpa2
);
7228 isl_multi_pw_aff_free(mpa1
);
7229 isl_multi_pw_aff_free(mpa2
);
7233 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7234 * where the function values are equal. "space" is the space of the result.
7235 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7237 * "mpa1" and "mpa2" are equal when each of the pairs of elements
7238 * in the sequences are equal.
7240 static __isl_give isl_map
*isl_multi_pw_aff_eq_map_on_space(
7241 __isl_keep isl_multi_pw_aff
*mpa1
, __isl_keep isl_multi_pw_aff
*mpa2
,
7242 __isl_take isl_space
*space
)
7247 res
= isl_map_universe(space
);
7249 n
= isl_multi_pw_aff_dim(mpa1
, isl_dim_out
);
7250 for (i
= 0; i
< n
; ++i
) {
7251 isl_pw_aff
*pa1
, *pa2
;
7254 pa1
= isl_multi_pw_aff_get_pw_aff(mpa1
, i
);
7255 pa2
= isl_multi_pw_aff_get_pw_aff(mpa2
, i
);
7256 map
= isl_pw_aff_eq_map(pa1
, pa2
);
7257 res
= isl_map_intersect(res
, map
);
7263 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7264 * where the function values are equal.
7266 __isl_give isl_map
*isl_multi_pw_aff_eq_map(__isl_take isl_multi_pw_aff
*mpa1
,
7267 __isl_take isl_multi_pw_aff
*mpa2
)
7269 return isl_multi_pw_aff_order_map(mpa1
, mpa2
,
7270 &isl_multi_pw_aff_eq_map_on_space
);
7273 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7274 * where the function values of "mpa1" is lexicographically satisfies "base"
7275 * compared to that of "mpa2". "space" is the space of the result.
7276 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7278 * "mpa1" lexicographically satisfies "base" compared to "mpa2"
7279 * if its i-th element satisfies "base" when compared to
7280 * the i-th element of "mpa2" while all previous elements are
7283 static __isl_give isl_map
*isl_multi_pw_aff_lex_map_on_space(
7284 __isl_keep isl_multi_pw_aff
*mpa1
, __isl_keep isl_multi_pw_aff
*mpa2
,
7285 __isl_give isl_map
*(*base
)(__isl_take isl_pw_aff
*pa1
,
7286 __isl_take isl_pw_aff
*pa2
),
7287 __isl_take isl_space
*space
)
7290 isl_map
*res
, *rest
;
7292 res
= isl_map_empty(isl_space_copy(space
));
7293 rest
= isl_map_universe(space
);
7295 n
= isl_multi_pw_aff_dim(mpa1
, isl_dim_out
);
7296 for (i
= 0; i
< n
; ++i
) {
7297 isl_pw_aff
*pa1
, *pa2
;
7300 pa1
= isl_multi_pw_aff_get_pw_aff(mpa1
, i
);
7301 pa2
= isl_multi_pw_aff_get_pw_aff(mpa2
, i
);
7302 map
= base(pa1
, pa2
);
7303 map
= isl_map_intersect(map
, isl_map_copy(rest
));
7304 res
= isl_map_union(res
, map
);
7309 pa1
= isl_multi_pw_aff_get_pw_aff(mpa1
, i
);
7310 pa2
= isl_multi_pw_aff_get_pw_aff(mpa2
, i
);
7311 map
= isl_pw_aff_eq_map(pa1
, pa2
);
7312 rest
= isl_map_intersect(rest
, map
);
7319 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7320 * where the function value of "mpa1" is lexicographically less than that
7321 * of "mpa2". "space" is the space of the result.
7322 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7324 * "mpa1" is less than "mpa2" if its i-th element is smaller
7325 * than the i-th element of "mpa2" while all previous elements are
7328 __isl_give isl_map
*isl_multi_pw_aff_lex_lt_map_on_space(
7329 __isl_keep isl_multi_pw_aff
*mpa1
, __isl_keep isl_multi_pw_aff
*mpa2
,
7330 __isl_take isl_space
*space
)
7332 return isl_multi_pw_aff_lex_map_on_space(mpa1
, mpa2
,
7333 &isl_pw_aff_lt_map
, space
);
7336 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7337 * where the function value of "mpa1" is lexicographically less than that
7340 __isl_give isl_map
*isl_multi_pw_aff_lex_lt_map(
7341 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
)
7343 return isl_multi_pw_aff_order_map(mpa1
, mpa2
,
7344 &isl_multi_pw_aff_lex_lt_map_on_space
);
7347 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7348 * where the function value of "mpa1" is lexicographically greater than that
7349 * of "mpa2". "space" is the space of the result.
7350 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7352 * "mpa1" is greater than "mpa2" if its i-th element is greater
7353 * than the i-th element of "mpa2" while all previous elements are
7356 __isl_give isl_map
*isl_multi_pw_aff_lex_gt_map_on_space(
7357 __isl_keep isl_multi_pw_aff
*mpa1
, __isl_keep isl_multi_pw_aff
*mpa2
,
7358 __isl_take isl_space
*space
)
7360 return isl_multi_pw_aff_lex_map_on_space(mpa1
, mpa2
,
7361 &isl_pw_aff_gt_map
, space
);
7364 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7365 * where the function value of "mpa1" is lexicographically greater than that
7368 __isl_give isl_map
*isl_multi_pw_aff_lex_gt_map(
7369 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
)
7371 return isl_multi_pw_aff_order_map(mpa1
, mpa2
,
7372 &isl_multi_pw_aff_lex_gt_map_on_space
);
7375 /* Compare two isl_affs.
7377 * Return -1 if "aff1" is "smaller" than "aff2", 1 if "aff1" is "greater"
7378 * than "aff2" and 0 if they are equal.
7380 * The order is fairly arbitrary. We do consider expressions that only involve
7381 * earlier dimensions as "smaller".
7383 int isl_aff_plain_cmp(__isl_keep isl_aff
*aff1
, __isl_keep isl_aff
*aff2
)
7396 cmp
= isl_local_space_cmp(aff1
->ls
, aff2
->ls
);
7400 last1
= isl_seq_last_non_zero(aff1
->v
->el
+ 1, aff1
->v
->size
- 1);
7401 last2
= isl_seq_last_non_zero(aff2
->v
->el
+ 1, aff1
->v
->size
- 1);
7403 return last1
- last2
;
7405 return isl_seq_cmp(aff1
->v
->el
, aff2
->v
->el
, aff1
->v
->size
);
7408 /* Compare two isl_pw_affs.
7410 * Return -1 if "pa1" is "smaller" than "pa2", 1 if "pa1" is "greater"
7411 * than "pa2" and 0 if they are equal.
7413 * The order is fairly arbitrary. We do consider expressions that only involve
7414 * earlier dimensions as "smaller".
7416 int isl_pw_aff_plain_cmp(__isl_keep isl_pw_aff
*pa1
,
7417 __isl_keep isl_pw_aff
*pa2
)
7430 cmp
= isl_space_cmp(pa1
->dim
, pa2
->dim
);
7434 if (pa1
->n
!= pa2
->n
)
7435 return pa1
->n
- pa2
->n
;
7437 for (i
= 0; i
< pa1
->n
; ++i
) {
7438 cmp
= isl_set_plain_cmp(pa1
->p
[i
].set
, pa2
->p
[i
].set
);
7441 cmp
= isl_aff_plain_cmp(pa1
->p
[i
].aff
, pa2
->p
[i
].aff
);
7449 /* Return a piecewise affine expression that is equal to "v" on "domain".
7451 __isl_give isl_pw_aff
*isl_pw_aff_val_on_domain(__isl_take isl_set
*domain
,
7452 __isl_take isl_val
*v
)
7455 isl_local_space
*ls
;
7458 space
= isl_set_get_space(domain
);
7459 ls
= isl_local_space_from_space(space
);
7460 aff
= isl_aff_val_on_domain(ls
, v
);
7462 return isl_pw_aff_alloc(domain
, aff
);
7465 /* Return a multi affine expression that is equal to "mv" on domain
7468 __isl_give isl_multi_aff
*isl_multi_aff_multi_val_on_space(
7469 __isl_take isl_space
*space
, __isl_take isl_multi_val
*mv
)
7473 isl_local_space
*ls
;
7479 n
= isl_multi_val_dim(mv
, isl_dim_set
);
7480 space2
= isl_multi_val_get_space(mv
);
7481 space2
= isl_space_align_params(space2
, isl_space_copy(space
));
7482 space
= isl_space_align_params(space
, isl_space_copy(space2
));
7483 space
= isl_space_map_from_domain_and_range(space
, space2
);
7484 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
7485 ls
= isl_local_space_from_space(isl_space_domain(space
));
7486 for (i
= 0; i
< n
; ++i
) {
7490 v
= isl_multi_val_get_val(mv
, i
);
7491 aff
= isl_aff_val_on_domain(isl_local_space_copy(ls
), v
);
7492 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
7494 isl_local_space_free(ls
);
7496 isl_multi_val_free(mv
);
7499 isl_space_free(space
);
7500 isl_multi_val_free(mv
);
7504 /* Return a piecewise multi-affine expression
7505 * that is equal to "mv" on "domain".
7507 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_multi_val_on_domain(
7508 __isl_take isl_set
*domain
, __isl_take isl_multi_val
*mv
)
7513 space
= isl_set_get_space(domain
);
7514 ma
= isl_multi_aff_multi_val_on_space(space
, mv
);
7516 return isl_pw_multi_aff_alloc(domain
, ma
);
7519 /* Internal data structure for isl_union_pw_multi_aff_multi_val_on_domain.
7520 * mv is the value that should be attained on each domain set
7521 * res collects the results
7523 struct isl_union_pw_multi_aff_multi_val_on_domain_data
{
7525 isl_union_pw_multi_aff
*res
;
7528 /* Create an isl_pw_multi_aff equal to data->mv on "domain"
7529 * and add it to data->res.
7531 static isl_stat
pw_multi_aff_multi_val_on_domain(__isl_take isl_set
*domain
,
7534 struct isl_union_pw_multi_aff_multi_val_on_domain_data
*data
= user
;
7535 isl_pw_multi_aff
*pma
;
7538 mv
= isl_multi_val_copy(data
->mv
);
7539 pma
= isl_pw_multi_aff_multi_val_on_domain(domain
, mv
);
7540 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma
);
7542 return data
->res
? isl_stat_ok
: isl_stat_error
;
7545 /* Return a union piecewise multi-affine expression
7546 * that is equal to "mv" on "domain".
7548 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_multi_val_on_domain(
7549 __isl_take isl_union_set
*domain
, __isl_take isl_multi_val
*mv
)
7551 struct isl_union_pw_multi_aff_multi_val_on_domain_data data
;
7554 space
= isl_union_set_get_space(domain
);
7555 data
.res
= isl_union_pw_multi_aff_empty(space
);
7557 if (isl_union_set_foreach_set(domain
,
7558 &pw_multi_aff_multi_val_on_domain
, &data
) < 0)
7559 data
.res
= isl_union_pw_multi_aff_free(data
.res
);
7560 isl_union_set_free(domain
);
7561 isl_multi_val_free(mv
);
7565 /* Compute the pullback of data->pma by the function represented by "pma2",
7566 * provided the spaces match, and add the results to data->res.
7568 static isl_stat
pullback_entry(__isl_take isl_pw_multi_aff
*pma2
, void *user
)
7570 struct isl_union_pw_multi_aff_bin_data
*data
= user
;
7572 if (!isl_space_tuple_is_equal(data
->pma
->dim
, isl_dim_in
,
7573 pma2
->dim
, isl_dim_out
)) {
7574 isl_pw_multi_aff_free(pma2
);
7578 pma2
= isl_pw_multi_aff_pullback_pw_multi_aff(
7579 isl_pw_multi_aff_copy(data
->pma
), pma2
);
7581 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma2
);
7583 return isl_stat_error
;
7588 /* Compute the pullback of "upma1" by the function represented by "upma2".
7590 __isl_give isl_union_pw_multi_aff
*
7591 isl_union_pw_multi_aff_pullback_union_pw_multi_aff(
7592 __isl_take isl_union_pw_multi_aff
*upma1
,
7593 __isl_take isl_union_pw_multi_aff
*upma2
)
7595 return bin_op(upma1
, upma2
, &pullback_entry
);
7598 /* Check that the domain space of "upa" matches "space".
7600 * This function is called from isl_multi_union_pw_aff_set_union_pw_aff and
7601 * can in principle never fail since the space "space" is that
7602 * of the isl_multi_union_pw_aff and is a set space such that
7603 * there is no domain space to match.
7605 * We check the parameters and double-check that "space" is
7606 * indeed that of a set.
7608 static isl_stat
isl_union_pw_aff_check_match_domain_space(
7609 __isl_keep isl_union_pw_aff
*upa
, __isl_keep isl_space
*space
)
7611 isl_space
*upa_space
;
7615 return isl_stat_error
;
7617 match
= isl_space_is_set(space
);
7619 return isl_stat_error
;
7621 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
7622 "expecting set space", return isl_stat_error
);
7624 upa_space
= isl_union_pw_aff_get_space(upa
);
7625 match
= isl_space_has_equal_params(space
, upa_space
);
7629 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
7630 "parameters don't match", goto error
);
7632 isl_space_free(upa_space
);
7635 isl_space_free(upa_space
);
7636 return isl_stat_error
;
7639 /* Do the parameters of "upa" match those of "space"?
7641 static isl_bool
isl_union_pw_aff_matching_params(
7642 __isl_keep isl_union_pw_aff
*upa
, __isl_keep isl_space
*space
)
7644 isl_space
*upa_space
;
7648 return isl_bool_error
;
7650 upa_space
= isl_union_pw_aff_get_space(upa
);
7652 match
= isl_space_has_equal_params(space
, upa_space
);
7654 isl_space_free(upa_space
);
7658 /* Internal data structure for isl_union_pw_aff_reset_domain_space.
7659 * space represents the new parameters.
7660 * res collects the results.
7662 struct isl_union_pw_aff_reset_params_data
{
7664 isl_union_pw_aff
*res
;
7667 /* Replace the parameters of "pa" by data->space and
7668 * add the result to data->res.
7670 static isl_stat
reset_params(__isl_take isl_pw_aff
*pa
, void *user
)
7672 struct isl_union_pw_aff_reset_params_data
*data
= user
;
7675 space
= isl_pw_aff_get_space(pa
);
7676 space
= isl_space_replace_params(space
, data
->space
);
7677 pa
= isl_pw_aff_reset_space(pa
, space
);
7678 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
7680 return data
->res
? isl_stat_ok
: isl_stat_error
;
7683 /* Replace the domain space of "upa" by "space".
7684 * Since a union expression does not have a (single) domain space,
7685 * "space" is necessarily a parameter space.
7687 * Since the order and the names of the parameters determine
7688 * the hash value, we need to create a new hash table.
7690 static __isl_give isl_union_pw_aff
*isl_union_pw_aff_reset_domain_space(
7691 __isl_take isl_union_pw_aff
*upa
, __isl_take isl_space
*space
)
7693 struct isl_union_pw_aff_reset_params_data data
= { space
};
7696 match
= isl_union_pw_aff_matching_params(upa
, space
);
7698 upa
= isl_union_pw_aff_free(upa
);
7700 isl_space_free(space
);
7704 data
.res
= isl_union_pw_aff_empty(isl_space_copy(space
));
7705 if (isl_union_pw_aff_foreach_pw_aff(upa
, &reset_params
, &data
) < 0)
7706 data
.res
= isl_union_pw_aff_free(data
.res
);
7708 isl_union_pw_aff_free(upa
);
7709 isl_space_free(space
);
7713 /* Return the floor of "pa".
7715 static __isl_give isl_pw_aff
*floor_entry(__isl_take isl_pw_aff
*pa
, void *user
)
7717 return isl_pw_aff_floor(pa
);
7720 /* Given f, return floor(f).
7722 __isl_give isl_union_pw_aff
*isl_union_pw_aff_floor(
7723 __isl_take isl_union_pw_aff
*upa
)
7725 return isl_union_pw_aff_transform_inplace(upa
, &floor_entry
, NULL
);
7730 * upa mod m = upa - m * floor(upa/m)
7732 * with m an integer value.
7734 __isl_give isl_union_pw_aff
*isl_union_pw_aff_mod_val(
7735 __isl_take isl_union_pw_aff
*upa
, __isl_take isl_val
*m
)
7737 isl_union_pw_aff
*res
;
7742 if (!isl_val_is_int(m
))
7743 isl_die(isl_val_get_ctx(m
), isl_error_invalid
,
7744 "expecting integer modulo", goto error
);
7745 if (!isl_val_is_pos(m
))
7746 isl_die(isl_val_get_ctx(m
), isl_error_invalid
,
7747 "expecting positive modulo", goto error
);
7749 res
= isl_union_pw_aff_copy(upa
);
7750 upa
= isl_union_pw_aff_scale_down_val(upa
, isl_val_copy(m
));
7751 upa
= isl_union_pw_aff_floor(upa
);
7752 upa
= isl_union_pw_aff_scale_val(upa
, m
);
7753 res
= isl_union_pw_aff_sub(res
, upa
);
7758 isl_union_pw_aff_free(upa
);
7762 /* Internal data structure for isl_union_pw_multi_aff_get_union_pw_aff.
7763 * pos is the output position that needs to be extracted.
7764 * res collects the results.
7766 struct isl_union_pw_multi_aff_get_union_pw_aff_data
{
7768 isl_union_pw_aff
*res
;
7771 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma"
7772 * (assuming it has such a dimension) and add it to data->res.
7774 static isl_stat
get_union_pw_aff(__isl_take isl_pw_multi_aff
*pma
, void *user
)
7776 struct isl_union_pw_multi_aff_get_union_pw_aff_data
*data
= user
;
7781 return isl_stat_error
;
7783 n_out
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
7784 if (data
->pos
>= n_out
) {
7785 isl_pw_multi_aff_free(pma
);
7789 pa
= isl_pw_multi_aff_get_pw_aff(pma
, data
->pos
);
7790 isl_pw_multi_aff_free(pma
);
7792 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
7794 return data
->res
? isl_stat_ok
: isl_stat_error
;
7797 /* Extract an isl_union_pw_aff corresponding to
7798 * output dimension "pos" of "upma".
7800 __isl_give isl_union_pw_aff
*isl_union_pw_multi_aff_get_union_pw_aff(
7801 __isl_keep isl_union_pw_multi_aff
*upma
, int pos
)
7803 struct isl_union_pw_multi_aff_get_union_pw_aff_data data
;
7810 isl_die(isl_union_pw_multi_aff_get_ctx(upma
), isl_error_invalid
,
7811 "cannot extract at negative position", return NULL
);
7813 space
= isl_union_pw_multi_aff_get_space(upma
);
7814 data
.res
= isl_union_pw_aff_empty(space
);
7816 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma
,
7817 &get_union_pw_aff
, &data
) < 0)
7818 data
.res
= isl_union_pw_aff_free(data
.res
);
7823 /* Return a union piecewise affine expression
7824 * that is equal to "aff" on "domain".
7826 __isl_give isl_union_pw_aff
*isl_union_pw_aff_aff_on_domain(
7827 __isl_take isl_union_set
*domain
, __isl_take isl_aff
*aff
)
7831 pa
= isl_pw_aff_from_aff(aff
);
7832 return isl_union_pw_aff_pw_aff_on_domain(domain
, pa
);
7835 /* Return a union piecewise affine expression
7836 * that is equal to the parameter identified by "id" on "domain".
7838 * Make sure the parameter appears in the space passed to
7839 * isl_aff_param_on_domain_space_id.
7841 __isl_give isl_union_pw_aff
*isl_union_pw_aff_param_on_domain_id(
7842 __isl_take isl_union_set
*domain
, __isl_take isl_id
*id
)
7847 space
= isl_union_set_get_space(domain
);
7848 space
= isl_space_add_param_id(space
, isl_id_copy(id
));
7849 aff
= isl_aff_param_on_domain_space_id(space
, id
);
7850 return isl_union_pw_aff_aff_on_domain(domain
, aff
);
7853 /* Internal data structure for isl_union_pw_aff_pw_aff_on_domain.
7854 * "pa" is the piecewise symbolic value that the resulting isl_union_pw_aff
7856 * "res" collects the results.
7858 struct isl_union_pw_aff_pw_aff_on_domain_data
{
7860 isl_union_pw_aff
*res
;
7863 /* Construct a piecewise affine expression that is equal to data->pa
7864 * on "domain" and add the result to data->res.
7866 static isl_stat
pw_aff_on_domain(__isl_take isl_set
*domain
, void *user
)
7868 struct isl_union_pw_aff_pw_aff_on_domain_data
*data
= user
;
7872 pa
= isl_pw_aff_copy(data
->pa
);
7873 dim
= isl_set_dim(domain
, isl_dim_set
);
7874 pa
= isl_pw_aff_from_range(pa
);
7875 pa
= isl_pw_aff_add_dims(pa
, isl_dim_in
, dim
);
7876 pa
= isl_pw_aff_reset_domain_space(pa
, isl_set_get_space(domain
));
7877 pa
= isl_pw_aff_intersect_domain(pa
, domain
);
7878 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
7880 return data
->res
? isl_stat_ok
: isl_stat_error
;
7883 /* Return a union piecewise affine expression
7884 * that is equal to "pa" on "domain", assuming "domain" and "pa"
7885 * have been aligned.
7887 * Construct an isl_pw_aff on each of the sets in "domain" and
7888 * collect the results.
7890 static __isl_give isl_union_pw_aff
*isl_union_pw_aff_pw_aff_on_domain_aligned(
7891 __isl_take isl_union_set
*domain
, __isl_take isl_pw_aff
*pa
)
7893 struct isl_union_pw_aff_pw_aff_on_domain_data data
;
7896 space
= isl_union_set_get_space(domain
);
7897 data
.res
= isl_union_pw_aff_empty(space
);
7899 if (isl_union_set_foreach_set(domain
, &pw_aff_on_domain
, &data
) < 0)
7900 data
.res
= isl_union_pw_aff_free(data
.res
);
7901 isl_union_set_free(domain
);
7902 isl_pw_aff_free(pa
);
7906 /* Return a union piecewise affine expression
7907 * that is equal to "pa" on "domain".
7909 * Check that "pa" is a parametric expression,
7910 * align the parameters if needed and call
7911 * isl_union_pw_aff_pw_aff_on_domain_aligned.
7913 __isl_give isl_union_pw_aff
*isl_union_pw_aff_pw_aff_on_domain(
7914 __isl_take isl_union_set
*domain
, __isl_take isl_pw_aff
*pa
)
7917 isl_bool equal_params
;
7918 isl_space
*domain_space
, *pa_space
;
7920 pa_space
= isl_pw_aff_peek_space(pa
);
7921 is_set
= isl_space_is_set(pa_space
);
7925 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
7926 "expecting parametric expression", goto error
);
7928 domain_space
= isl_union_set_get_space(domain
);
7929 pa_space
= isl_pw_aff_get_space(pa
);
7930 equal_params
= isl_space_has_equal_params(domain_space
, pa_space
);
7931 if (equal_params
>= 0 && !equal_params
) {
7934 space
= isl_space_align_params(domain_space
, pa_space
);
7935 pa
= isl_pw_aff_align_params(pa
, isl_space_copy(space
));
7936 domain
= isl_union_set_align_params(domain
, space
);
7938 isl_space_free(domain_space
);
7939 isl_space_free(pa_space
);
7942 if (equal_params
< 0)
7944 return isl_union_pw_aff_pw_aff_on_domain_aligned(domain
, pa
);
7946 isl_union_set_free(domain
);
7947 isl_pw_aff_free(pa
);
7951 /* Internal data structure for isl_union_pw_aff_val_on_domain.
7952 * "v" is the value that the resulting isl_union_pw_aff needs to attain.
7953 * "res" collects the results.
7955 struct isl_union_pw_aff_val_on_domain_data
{
7957 isl_union_pw_aff
*res
;
7960 /* Construct a piecewise affine expression that is equal to data->v
7961 * on "domain" and add the result to data->res.
7963 static isl_stat
pw_aff_val_on_domain(__isl_take isl_set
*domain
, void *user
)
7965 struct isl_union_pw_aff_val_on_domain_data
*data
= user
;
7969 v
= isl_val_copy(data
->v
);
7970 pa
= isl_pw_aff_val_on_domain(domain
, v
);
7971 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
7973 return data
->res
? isl_stat_ok
: isl_stat_error
;
7976 /* Return a union piecewise affine expression
7977 * that is equal to "v" on "domain".
7979 * Construct an isl_pw_aff on each of the sets in "domain" and
7980 * collect the results.
7982 __isl_give isl_union_pw_aff
*isl_union_pw_aff_val_on_domain(
7983 __isl_take isl_union_set
*domain
, __isl_take isl_val
*v
)
7985 struct isl_union_pw_aff_val_on_domain_data data
;
7988 space
= isl_union_set_get_space(domain
);
7989 data
.res
= isl_union_pw_aff_empty(space
);
7991 if (isl_union_set_foreach_set(domain
, &pw_aff_val_on_domain
, &data
) < 0)
7992 data
.res
= isl_union_pw_aff_free(data
.res
);
7993 isl_union_set_free(domain
);
7998 /* Construct a piecewise multi affine expression
7999 * that is equal to "pa" and add it to upma.
8001 static isl_stat
pw_multi_aff_from_pw_aff_entry(__isl_take isl_pw_aff
*pa
,
8004 isl_union_pw_multi_aff
**upma
= user
;
8005 isl_pw_multi_aff
*pma
;
8007 pma
= isl_pw_multi_aff_from_pw_aff(pa
);
8008 *upma
= isl_union_pw_multi_aff_add_pw_multi_aff(*upma
, pma
);
8010 return *upma
? isl_stat_ok
: isl_stat_error
;
8013 /* Construct and return a union piecewise multi affine expression
8014 * that is equal to the given union piecewise affine expression.
8016 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_union_pw_aff(
8017 __isl_take isl_union_pw_aff
*upa
)
8020 isl_union_pw_multi_aff
*upma
;
8025 space
= isl_union_pw_aff_get_space(upa
);
8026 upma
= isl_union_pw_multi_aff_empty(space
);
8028 if (isl_union_pw_aff_foreach_pw_aff(upa
,
8029 &pw_multi_aff_from_pw_aff_entry
, &upma
) < 0)
8030 upma
= isl_union_pw_multi_aff_free(upma
);
8032 isl_union_pw_aff_free(upa
);
8036 /* Compute the set of elements in the domain of "pa" where it is zero and
8037 * add this set to "uset".
8039 static isl_stat
zero_union_set(__isl_take isl_pw_aff
*pa
, void *user
)
8041 isl_union_set
**uset
= (isl_union_set
**)user
;
8043 *uset
= isl_union_set_add_set(*uset
, isl_pw_aff_zero_set(pa
));
8045 return *uset
? isl_stat_ok
: isl_stat_error
;
8048 /* Return a union set containing those elements in the domain
8049 * of "upa" where it is zero.
8051 __isl_give isl_union_set
*isl_union_pw_aff_zero_union_set(
8052 __isl_take isl_union_pw_aff
*upa
)
8054 isl_union_set
*zero
;
8056 zero
= isl_union_set_empty(isl_union_pw_aff_get_space(upa
));
8057 if (isl_union_pw_aff_foreach_pw_aff(upa
, &zero_union_set
, &zero
) < 0)
8058 zero
= isl_union_set_free(zero
);
8060 isl_union_pw_aff_free(upa
);
8064 /* Convert "pa" to an isl_map and add it to *umap.
8066 static isl_stat
map_from_pw_aff_entry(__isl_take isl_pw_aff
*pa
, void *user
)
8068 isl_union_map
**umap
= user
;
8071 map
= isl_map_from_pw_aff(pa
);
8072 *umap
= isl_union_map_add_map(*umap
, map
);
8074 return *umap
? isl_stat_ok
: isl_stat_error
;
8077 /* Construct a union map mapping the domain of the union
8078 * piecewise affine expression to its range, with the single output dimension
8079 * equated to the corresponding affine expressions on their cells.
8081 __isl_give isl_union_map
*isl_union_map_from_union_pw_aff(
8082 __isl_take isl_union_pw_aff
*upa
)
8085 isl_union_map
*umap
;
8090 space
= isl_union_pw_aff_get_space(upa
);
8091 umap
= isl_union_map_empty(space
);
8093 if (isl_union_pw_aff_foreach_pw_aff(upa
, &map_from_pw_aff_entry
,
8095 umap
= isl_union_map_free(umap
);
8097 isl_union_pw_aff_free(upa
);
8101 /* Internal data structure for isl_union_pw_aff_pullback_union_pw_multi_aff.
8102 * upma is the function that is plugged in.
8103 * pa is the current part of the function in which upma is plugged in.
8104 * res collects the results.
8106 struct isl_union_pw_aff_pullback_upma_data
{
8107 isl_union_pw_multi_aff
*upma
;
8109 isl_union_pw_aff
*res
;
8112 /* Check if "pma" can be plugged into data->pa.
8113 * If so, perform the pullback and add the result to data->res.
8115 static isl_stat
pa_pb_pma(__isl_take isl_pw_multi_aff
*pma
, void *user
)
8117 struct isl_union_pw_aff_pullback_upma_data
*data
= user
;
8120 if (!isl_space_tuple_is_equal(data
->pa
->dim
, isl_dim_in
,
8121 pma
->dim
, isl_dim_out
)) {
8122 isl_pw_multi_aff_free(pma
);
8126 pa
= isl_pw_aff_copy(data
->pa
);
8127 pa
= isl_pw_aff_pullback_pw_multi_aff(pa
, pma
);
8129 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
8131 return data
->res
? isl_stat_ok
: isl_stat_error
;
8134 /* Check if any of the elements of data->upma can be plugged into pa,
8135 * add if so add the result to data->res.
8137 static isl_stat
upa_pb_upma(__isl_take isl_pw_aff
*pa
, void *user
)
8139 struct isl_union_pw_aff_pullback_upma_data
*data
= user
;
8143 r
= isl_union_pw_multi_aff_foreach_pw_multi_aff(data
->upma
,
8145 isl_pw_aff_free(pa
);
8150 /* Compute the pullback of "upa" by the function represented by "upma".
8151 * In other words, plug in "upma" in "upa". The result contains
8152 * expressions defined over the domain space of "upma".
8154 * Run over all pairs of elements in "upa" and "upma", perform
8155 * the pullback when appropriate and collect the results.
8156 * If the hash value were based on the domain space rather than
8157 * the function space, then we could run through all elements
8158 * of "upma" and directly pick out the corresponding element of "upa".
8160 __isl_give isl_union_pw_aff
*isl_union_pw_aff_pullback_union_pw_multi_aff(
8161 __isl_take isl_union_pw_aff
*upa
,
8162 __isl_take isl_union_pw_multi_aff
*upma
)
8164 struct isl_union_pw_aff_pullback_upma_data data
= { NULL
, NULL
};
8167 space
= isl_union_pw_multi_aff_get_space(upma
);
8168 upa
= isl_union_pw_aff_align_params(upa
, space
);
8169 space
= isl_union_pw_aff_get_space(upa
);
8170 upma
= isl_union_pw_multi_aff_align_params(upma
, space
);
8176 data
.res
= isl_union_pw_aff_alloc_same_size(upa
);
8177 if (isl_union_pw_aff_foreach_pw_aff(upa
, &upa_pb_upma
, &data
) < 0)
8178 data
.res
= isl_union_pw_aff_free(data
.res
);
8180 isl_union_pw_aff_free(upa
);
8181 isl_union_pw_multi_aff_free(upma
);
8184 isl_union_pw_aff_free(upa
);
8185 isl_union_pw_multi_aff_free(upma
);
8190 #define BASE union_pw_aff
8192 #define DOMBASE union_set
8194 #define NO_MOVE_DIMS
8201 #include <isl_multi_explicit_domain.c>
8202 #include <isl_multi_union_pw_aff_explicit_domain.c>
8203 #include <isl_multi_templ.c>
8204 #include <isl_multi_apply_set.c>
8205 #include <isl_multi_apply_union_set.c>
8206 #include <isl_multi_coalesce.c>
8207 #include <isl_multi_floor.c>
8208 #include <isl_multi_gist.c>
8209 #include <isl_multi_align_set.c>
8210 #include <isl_multi_align_union_set.c>
8211 #include <isl_multi_intersect.c>
8213 /* Does "mupa" have a non-trivial explicit domain?
8215 * The explicit domain, if present, is trivial if it represents
8216 * an (obviously) universe parameter set.
8218 isl_bool
isl_multi_union_pw_aff_has_non_trivial_domain(
8219 __isl_keep isl_multi_union_pw_aff
*mupa
)
8221 isl_bool is_params
, trivial
;
8225 return isl_bool_error
;
8226 if (!isl_multi_union_pw_aff_has_explicit_domain(mupa
))
8227 return isl_bool_false
;
8228 is_params
= isl_union_set_is_params(mupa
->u
.dom
);
8229 if (is_params
< 0 || !is_params
)
8230 return isl_bool_not(is_params
);
8231 set
= isl_set_from_union_set(isl_union_set_copy(mupa
->u
.dom
));
8232 trivial
= isl_set_plain_is_universe(set
);
8234 return isl_bool_not(trivial
);
8237 /* Construct a multiple union piecewise affine expression
8238 * in the given space with value zero in each of the output dimensions.
8240 * Since there is no canonical zero value for
8241 * a union piecewise affine expression, we can only construct
8242 * a zero-dimensional "zero" value.
8244 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_zero(
8245 __isl_take isl_space
*space
)
8252 params
= isl_space_is_params(space
);
8256 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
8257 "expecting proper set space", goto error
);
8258 if (!isl_space_is_set(space
))
8259 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
8260 "expecting set space", goto error
);
8261 if (isl_space_dim(space
, isl_dim_out
) != 0)
8262 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
8263 "expecting 0D space", goto error
);
8265 return isl_multi_union_pw_aff_alloc(space
);
8267 isl_space_free(space
);
8271 /* Compute the sum of "mupa1" and "mupa2" on the union of their domains,
8272 * with the actual sum on the shared domain and
8273 * the defined expression on the symmetric difference of the domains.
8275 * We simply iterate over the elements in both arguments and
8276 * call isl_union_pw_aff_union_add on each of them, if there is
8277 * at least one element.
8279 * Otherwise, the two expressions have an explicit domain and
8280 * the union of these explicit domains is computed.
8281 * This assumes that the explicit domains are either both in terms
8282 * of specific domains elements or both in terms of parameters.
8283 * However, if one of the expressions does not have any constraints
8284 * on its explicit domain, then this is allowed as well and the result
8285 * is the expression with no constraints on its explicit domain.
8287 static __isl_give isl_multi_union_pw_aff
*
8288 isl_multi_union_pw_aff_union_add_aligned(
8289 __isl_take isl_multi_union_pw_aff
*mupa1
,
8290 __isl_take isl_multi_union_pw_aff
*mupa2
)
8292 isl_bool has_domain
, is_params1
, is_params2
;
8294 if (isl_multi_union_pw_aff_check_equal_space(mupa1
, mupa2
) < 0)
8297 return isl_multi_union_pw_aff_bin_op(mupa1
, mupa2
,
8298 &isl_union_pw_aff_union_add
);
8299 if (isl_multi_union_pw_aff_check_has_explicit_domain(mupa1
) < 0 ||
8300 isl_multi_union_pw_aff_check_has_explicit_domain(mupa2
) < 0)
8303 has_domain
= isl_multi_union_pw_aff_has_non_trivial_domain(mupa1
);
8307 isl_multi_union_pw_aff_free(mupa2
);
8310 has_domain
= isl_multi_union_pw_aff_has_non_trivial_domain(mupa2
);
8314 isl_multi_union_pw_aff_free(mupa1
);
8318 is_params1
= isl_union_set_is_params(mupa1
->u
.dom
);
8319 is_params2
= isl_union_set_is_params(mupa2
->u
.dom
);
8320 if (is_params1
< 0 || is_params2
< 0)
8322 if (is_params1
!= is_params2
)
8323 isl_die(isl_multi_union_pw_aff_get_ctx(mupa1
),
8325 "cannot compute union of concrete domain and "
8326 "parameter constraints", goto error
);
8327 mupa1
= isl_multi_union_pw_aff_cow(mupa1
);
8330 mupa1
->u
.dom
= isl_union_set_union(mupa1
->u
.dom
,
8331 isl_union_set_copy(mupa2
->u
.dom
));
8334 isl_multi_union_pw_aff_free(mupa2
);
8337 isl_multi_union_pw_aff_free(mupa1
);
8338 isl_multi_union_pw_aff_free(mupa2
);
8342 /* Compute the sum of "mupa1" and "mupa2" on the union of their domains,
8343 * with the actual sum on the shared domain and
8344 * the defined expression on the symmetric difference of the domains.
8346 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_union_add(
8347 __isl_take isl_multi_union_pw_aff
*mupa1
,
8348 __isl_take isl_multi_union_pw_aff
*mupa2
)
8350 return isl_multi_union_pw_aff_align_params_multi_multi_and(mupa1
, mupa2
,
8351 &isl_multi_union_pw_aff_union_add_aligned
);
8354 /* Construct and return a multi union piecewise affine expression
8355 * that is equal to the given multi affine expression.
8357 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_from_multi_aff(
8358 __isl_take isl_multi_aff
*ma
)
8360 isl_multi_pw_aff
*mpa
;
8362 mpa
= isl_multi_pw_aff_from_multi_aff(ma
);
8363 return isl_multi_union_pw_aff_from_multi_pw_aff(mpa
);
8366 /* Construct and return a multi union piecewise affine expression
8367 * that is equal to the given multi piecewise affine expression.
8369 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_from_multi_pw_aff(
8370 __isl_take isl_multi_pw_aff
*mpa
)
8374 isl_multi_union_pw_aff
*mupa
;
8379 space
= isl_multi_pw_aff_get_space(mpa
);
8380 space
= isl_space_range(space
);
8381 mupa
= isl_multi_union_pw_aff_alloc(space
);
8383 n
= isl_multi_pw_aff_dim(mpa
, isl_dim_out
);
8384 for (i
= 0; i
< n
; ++i
) {
8386 isl_union_pw_aff
*upa
;
8388 pa
= isl_multi_pw_aff_get_pw_aff(mpa
, i
);
8389 upa
= isl_union_pw_aff_from_pw_aff(pa
);
8390 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
8393 isl_multi_pw_aff_free(mpa
);
8398 /* Extract the range space of "pma" and assign it to *space.
8399 * If *space has already been set (through a previous call to this function),
8400 * then check that the range space is the same.
8402 static isl_stat
extract_space(__isl_take isl_pw_multi_aff
*pma
, void *user
)
8404 isl_space
**space
= user
;
8405 isl_space
*pma_space
;
8408 pma_space
= isl_space_range(isl_pw_multi_aff_get_space(pma
));
8409 isl_pw_multi_aff_free(pma
);
8412 return isl_stat_error
;
8418 equal
= isl_space_is_equal(pma_space
, *space
);
8419 isl_space_free(pma_space
);
8422 return isl_stat_error
;
8424 isl_die(isl_space_get_ctx(*space
), isl_error_invalid
,
8425 "range spaces not the same", return isl_stat_error
);
8429 /* Construct and return a multi union piecewise affine expression
8430 * that is equal to the given union piecewise multi affine expression.
8432 * In order to be able to perform the conversion, the input
8433 * needs to be non-empty and may only involve a single range space.
8435 * If the resulting multi union piecewise affine expression has
8436 * an explicit domain, then assign it the domain of the input.
8437 * In other cases, the domain is stored in the individual elements.
8439 __isl_give isl_multi_union_pw_aff
*
8440 isl_multi_union_pw_aff_from_union_pw_multi_aff(
8441 __isl_take isl_union_pw_multi_aff
*upma
)
8443 isl_space
*space
= NULL
;
8444 isl_multi_union_pw_aff
*mupa
;
8449 if (isl_union_pw_multi_aff_n_pw_multi_aff(upma
) == 0)
8450 isl_die(isl_union_pw_multi_aff_get_ctx(upma
), isl_error_invalid
,
8451 "cannot extract range space from empty input",
8453 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma
, &extract_space
,
8460 n
= isl_space_dim(space
, isl_dim_set
);
8461 mupa
= isl_multi_union_pw_aff_alloc(space
);
8463 for (i
= 0; i
< n
; ++i
) {
8464 isl_union_pw_aff
*upa
;
8466 upa
= isl_union_pw_multi_aff_get_union_pw_aff(upma
, i
);
8467 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
8469 if (isl_multi_union_pw_aff_has_explicit_domain(mupa
)) {
8471 isl_union_pw_multi_aff
*copy
;
8473 copy
= isl_union_pw_multi_aff_copy(upma
);
8474 dom
= isl_union_pw_multi_aff_domain(copy
);
8475 mupa
= isl_multi_union_pw_aff_intersect_domain(mupa
, dom
);
8478 isl_union_pw_multi_aff_free(upma
);
8481 isl_space_free(space
);
8482 isl_union_pw_multi_aff_free(upma
);
8486 /* Try and create an isl_multi_union_pw_aff that is equivalent
8487 * to the given isl_union_map.
8488 * The isl_union_map is required to be single-valued in each space.
8489 * Moreover, it cannot be empty and all range spaces need to be the same.
8490 * Otherwise, an error is produced.
8492 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_from_union_map(
8493 __isl_take isl_union_map
*umap
)
8495 isl_union_pw_multi_aff
*upma
;
8497 upma
= isl_union_pw_multi_aff_from_union_map(umap
);
8498 return isl_multi_union_pw_aff_from_union_pw_multi_aff(upma
);
8501 /* Return a multiple union piecewise affine expression
8502 * that is equal to "mv" on "domain", assuming "domain" and "mv"
8503 * have been aligned.
8505 * If the resulting multi union piecewise affine expression has
8506 * an explicit domain, then assign it the input domain.
8507 * In other cases, the domain is stored in the individual elements.
8509 static __isl_give isl_multi_union_pw_aff
*
8510 isl_multi_union_pw_aff_multi_val_on_domain_aligned(
8511 __isl_take isl_union_set
*domain
, __isl_take isl_multi_val
*mv
)
8515 isl_multi_union_pw_aff
*mupa
;
8520 n
= isl_multi_val_dim(mv
, isl_dim_set
);
8521 space
= isl_multi_val_get_space(mv
);
8522 mupa
= isl_multi_union_pw_aff_alloc(space
);
8523 for (i
= 0; i
< n
; ++i
) {
8525 isl_union_pw_aff
*upa
;
8527 v
= isl_multi_val_get_val(mv
, i
);
8528 upa
= isl_union_pw_aff_val_on_domain(isl_union_set_copy(domain
),
8530 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
8532 if (isl_multi_union_pw_aff_has_explicit_domain(mupa
))
8533 mupa
= isl_multi_union_pw_aff_intersect_domain(mupa
,
8534 isl_union_set_copy(domain
));
8536 isl_union_set_free(domain
);
8537 isl_multi_val_free(mv
);
8540 isl_union_set_free(domain
);
8541 isl_multi_val_free(mv
);
8545 /* Return a multiple union piecewise affine expression
8546 * that is equal to "mv" on "domain".
8548 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_multi_val_on_domain(
8549 __isl_take isl_union_set
*domain
, __isl_take isl_multi_val
*mv
)
8551 isl_bool equal_params
;
8555 equal_params
= isl_space_has_equal_params(domain
->dim
, mv
->space
);
8556 if (equal_params
< 0)
8559 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(
8561 domain
= isl_union_set_align_params(domain
,
8562 isl_multi_val_get_space(mv
));
8563 mv
= isl_multi_val_align_params(mv
, isl_union_set_get_space(domain
));
8564 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(domain
, mv
);
8566 isl_union_set_free(domain
);
8567 isl_multi_val_free(mv
);
8571 /* Return a multiple union piecewise affine expression
8572 * that is equal to "ma" on "domain".
8574 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_multi_aff_on_domain(
8575 __isl_take isl_union_set
*domain
, __isl_take isl_multi_aff
*ma
)
8577 isl_pw_multi_aff
*pma
;
8579 pma
= isl_pw_multi_aff_from_multi_aff(ma
);
8580 return isl_multi_union_pw_aff_pw_multi_aff_on_domain(domain
, pma
);
8583 /* Return a multiple union piecewise affine expression
8584 * that is equal to "pma" on "domain", assuming "domain" and "pma"
8585 * have been aligned.
8587 * If the resulting multi union piecewise affine expression has
8588 * an explicit domain, then assign it the input domain.
8589 * In other cases, the domain is stored in the individual elements.
8591 static __isl_give isl_multi_union_pw_aff
*
8592 isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(
8593 __isl_take isl_union_set
*domain
, __isl_take isl_pw_multi_aff
*pma
)
8597 isl_multi_union_pw_aff
*mupa
;
8599 if (!domain
|| !pma
)
8602 n
= isl_pw_multi_aff_dim(pma
, isl_dim_set
);
8603 space
= isl_pw_multi_aff_get_space(pma
);
8604 mupa
= isl_multi_union_pw_aff_alloc(space
);
8605 for (i
= 0; i
< n
; ++i
) {
8607 isl_union_pw_aff
*upa
;
8609 pa
= isl_pw_multi_aff_get_pw_aff(pma
, i
);
8610 upa
= isl_union_pw_aff_pw_aff_on_domain(
8611 isl_union_set_copy(domain
), pa
);
8612 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
8614 if (isl_multi_union_pw_aff_has_explicit_domain(mupa
))
8615 mupa
= isl_multi_union_pw_aff_intersect_domain(mupa
,
8616 isl_union_set_copy(domain
));
8618 isl_union_set_free(domain
);
8619 isl_pw_multi_aff_free(pma
);
8622 isl_union_set_free(domain
);
8623 isl_pw_multi_aff_free(pma
);
8627 /* Return a multiple union piecewise affine expression
8628 * that is equal to "pma" on "domain".
8630 __isl_give isl_multi_union_pw_aff
*
8631 isl_multi_union_pw_aff_pw_multi_aff_on_domain(__isl_take isl_union_set
*domain
,
8632 __isl_take isl_pw_multi_aff
*pma
)
8634 isl_bool equal_params
;
8637 space
= isl_pw_multi_aff_peek_space(pma
);
8638 equal_params
= isl_union_set_space_has_equal_params(domain
, space
);
8639 if (equal_params
< 0)
8642 return isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(
8644 domain
= isl_union_set_align_params(domain
,
8645 isl_pw_multi_aff_get_space(pma
));
8646 pma
= isl_pw_multi_aff_align_params(pma
,
8647 isl_union_set_get_space(domain
));
8648 return isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(domain
,
8651 isl_union_set_free(domain
);
8652 isl_pw_multi_aff_free(pma
);
8656 /* Return a union set containing those elements in the domains
8657 * of the elements of "mupa" where they are all zero.
8659 * If there are no elements, then simply return the entire domain.
8661 __isl_give isl_union_set
*isl_multi_union_pw_aff_zero_union_set(
8662 __isl_take isl_multi_union_pw_aff
*mupa
)
8665 isl_union_pw_aff
*upa
;
8666 isl_union_set
*zero
;
8671 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8673 return isl_multi_union_pw_aff_domain(mupa
);
8675 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, 0);
8676 zero
= isl_union_pw_aff_zero_union_set(upa
);
8678 for (i
= 1; i
< n
; ++i
) {
8679 isl_union_set
*zero_i
;
8681 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
8682 zero_i
= isl_union_pw_aff_zero_union_set(upa
);
8684 zero
= isl_union_set_intersect(zero
, zero_i
);
8687 isl_multi_union_pw_aff_free(mupa
);
8691 /* Construct a union map mapping the shared domain
8692 * of the union piecewise affine expressions to the range of "mupa"
8693 * in the special case of a 0D multi union piecewise affine expression.
8695 * Construct a map between the explicit domain of "mupa" and
8697 * Note that this assumes that the domain consists of explicit elements.
8699 static __isl_give isl_union_map
*isl_union_map_from_multi_union_pw_aff_0D(
8700 __isl_take isl_multi_union_pw_aff
*mupa
)
8704 isl_union_set
*dom
, *ran
;
8706 space
= isl_multi_union_pw_aff_get_space(mupa
);
8707 dom
= isl_multi_union_pw_aff_domain(mupa
);
8708 ran
= isl_union_set_from_set(isl_set_universe(space
));
8710 is_params
= isl_union_set_is_params(dom
);
8712 dom
= isl_union_set_free(dom
);
8714 isl_die(isl_union_set_get_ctx(dom
), isl_error_invalid
,
8715 "cannot create union map from expression without "
8716 "explicit domain elements",
8717 dom
= isl_union_set_free(dom
));
8719 return isl_union_map_from_domain_and_range(dom
, ran
);
8722 /* Construct a union map mapping the shared domain
8723 * of the union piecewise affine expressions to the range of "mupa"
8724 * with each dimension in the range equated to the
8725 * corresponding union piecewise affine expression.
8727 * If the input is zero-dimensional, then construct a mapping
8728 * from its explicit domain.
8730 __isl_give isl_union_map
*isl_union_map_from_multi_union_pw_aff(
8731 __isl_take isl_multi_union_pw_aff
*mupa
)
8735 isl_union_map
*umap
;
8736 isl_union_pw_aff
*upa
;
8741 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8743 return isl_union_map_from_multi_union_pw_aff_0D(mupa
);
8745 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, 0);
8746 umap
= isl_union_map_from_union_pw_aff(upa
);
8748 for (i
= 1; i
< n
; ++i
) {
8749 isl_union_map
*umap_i
;
8751 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
8752 umap_i
= isl_union_map_from_union_pw_aff(upa
);
8753 umap
= isl_union_map_flat_range_product(umap
, umap_i
);
8756 space
= isl_multi_union_pw_aff_get_space(mupa
);
8757 umap
= isl_union_map_reset_range_space(umap
, space
);
8759 isl_multi_union_pw_aff_free(mupa
);
8763 /* Internal data structure for isl_union_pw_multi_aff_reset_range_space.
8764 * "range" is the space from which to set the range space.
8765 * "res" collects the results.
8767 struct isl_union_pw_multi_aff_reset_range_space_data
{
8769 isl_union_pw_multi_aff
*res
;
8772 /* Replace the range space of "pma" by the range space of data->range and
8773 * add the result to data->res.
8775 static isl_stat
reset_range_space(__isl_take isl_pw_multi_aff
*pma
, void *user
)
8777 struct isl_union_pw_multi_aff_reset_range_space_data
*data
= user
;
8780 space
= isl_pw_multi_aff_get_space(pma
);
8781 space
= isl_space_domain(space
);
8782 space
= isl_space_extend_domain_with_range(space
,
8783 isl_space_copy(data
->range
));
8784 pma
= isl_pw_multi_aff_reset_space(pma
, space
);
8785 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma
);
8787 return data
->res
? isl_stat_ok
: isl_stat_error
;
8790 /* Replace the range space of all the piecewise affine expressions in "upma" by
8791 * the range space of "space".
8793 * This assumes that all these expressions have the same output dimension.
8795 * Since the spaces of the expressions change, so do their hash values.
8796 * We therefore need to create a new isl_union_pw_multi_aff.
8797 * Note that the hash value is currently computed based on the entire
8798 * space even though there can only be a single expression with a given
8801 static __isl_give isl_union_pw_multi_aff
*
8802 isl_union_pw_multi_aff_reset_range_space(
8803 __isl_take isl_union_pw_multi_aff
*upma
, __isl_take isl_space
*space
)
8805 struct isl_union_pw_multi_aff_reset_range_space_data data
= { space
};
8806 isl_space
*space_upma
;
8808 space_upma
= isl_union_pw_multi_aff_get_space(upma
);
8809 data
.res
= isl_union_pw_multi_aff_empty(space_upma
);
8810 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma
,
8811 &reset_range_space
, &data
) < 0)
8812 data
.res
= isl_union_pw_multi_aff_free(data
.res
);
8814 isl_space_free(space
);
8815 isl_union_pw_multi_aff_free(upma
);
8819 /* Construct and return a union piecewise multi affine expression
8820 * that is equal to the given multi union piecewise affine expression,
8821 * in the special case of a 0D multi union piecewise affine expression.
8823 * Construct a union piecewise multi affine expression
8824 * on top of the explicit domain of the input.
8826 __isl_give isl_union_pw_multi_aff
*
8827 isl_union_pw_multi_aff_from_multi_union_pw_aff_0D(
8828 __isl_take isl_multi_union_pw_aff
*mupa
)
8832 isl_union_set
*domain
;
8834 space
= isl_multi_union_pw_aff_get_space(mupa
);
8835 mv
= isl_multi_val_zero(space
);
8836 domain
= isl_multi_union_pw_aff_domain(mupa
);
8837 return isl_union_pw_multi_aff_multi_val_on_domain(domain
, mv
);
8840 /* Construct and return a union piecewise multi affine expression
8841 * that is equal to the given multi union piecewise affine expression.
8843 * If the input is zero-dimensional, then
8844 * construct a union piecewise multi affine expression
8845 * on top of the explicit domain of the input.
8847 __isl_give isl_union_pw_multi_aff
*
8848 isl_union_pw_multi_aff_from_multi_union_pw_aff(
8849 __isl_take isl_multi_union_pw_aff
*mupa
)
8853 isl_union_pw_multi_aff
*upma
;
8854 isl_union_pw_aff
*upa
;
8859 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8861 return isl_union_pw_multi_aff_from_multi_union_pw_aff_0D(mupa
);
8863 space
= isl_multi_union_pw_aff_get_space(mupa
);
8864 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, 0);
8865 upma
= isl_union_pw_multi_aff_from_union_pw_aff(upa
);
8867 for (i
= 1; i
< n
; ++i
) {
8868 isl_union_pw_multi_aff
*upma_i
;
8870 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
8871 upma_i
= isl_union_pw_multi_aff_from_union_pw_aff(upa
);
8872 upma
= isl_union_pw_multi_aff_flat_range_product(upma
, upma_i
);
8875 upma
= isl_union_pw_multi_aff_reset_range_space(upma
, space
);
8877 isl_multi_union_pw_aff_free(mupa
);
8881 /* Intersect the range of "mupa" with "range",
8882 * in the special case where "mupa" is 0D.
8884 * Intersect the domain of "mupa" with the constraints on the parameters
8887 static __isl_give isl_multi_union_pw_aff
*mupa_intersect_range_0D(
8888 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_set
*range
)
8890 range
= isl_set_params(range
);
8891 mupa
= isl_multi_union_pw_aff_intersect_params(mupa
, range
);
8895 /* Intersect the range of "mupa" with "range".
8896 * That is, keep only those domain elements that have a function value
8899 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_intersect_range(
8900 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_set
*range
)
8902 isl_union_pw_multi_aff
*upma
;
8903 isl_union_set
*domain
;
8908 if (!mupa
|| !range
)
8911 space
= isl_set_get_space(range
);
8912 match
= isl_space_tuple_is_equal(mupa
->space
, isl_dim_set
,
8913 space
, isl_dim_set
);
8914 isl_space_free(space
);
8918 isl_die(isl_multi_union_pw_aff_get_ctx(mupa
), isl_error_invalid
,
8919 "space don't match", goto error
);
8920 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8922 return mupa_intersect_range_0D(mupa
, range
);
8924 upma
= isl_union_pw_multi_aff_from_multi_union_pw_aff(
8925 isl_multi_union_pw_aff_copy(mupa
));
8926 domain
= isl_union_set_from_set(range
);
8927 domain
= isl_union_set_preimage_union_pw_multi_aff(domain
, upma
);
8928 mupa
= isl_multi_union_pw_aff_intersect_domain(mupa
, domain
);
8932 isl_multi_union_pw_aff_free(mupa
);
8933 isl_set_free(range
);
8937 /* Return the shared domain of the elements of "mupa",
8938 * in the special case where "mupa" is zero-dimensional.
8940 * Return the explicit domain of "mupa".
8941 * Note that this domain may be a parameter set, either
8942 * because "mupa" is meant to live in a set space or
8943 * because no explicit domain has been set.
8945 __isl_give isl_union_set
*isl_multi_union_pw_aff_domain_0D(
8946 __isl_take isl_multi_union_pw_aff
*mupa
)
8950 dom
= isl_multi_union_pw_aff_get_explicit_domain(mupa
);
8951 isl_multi_union_pw_aff_free(mupa
);
8956 /* Return the shared domain of the elements of "mupa".
8958 * If "mupa" is zero-dimensional, then return its explicit domain.
8960 __isl_give isl_union_set
*isl_multi_union_pw_aff_domain(
8961 __isl_take isl_multi_union_pw_aff
*mupa
)
8964 isl_union_pw_aff
*upa
;
8970 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8972 return isl_multi_union_pw_aff_domain_0D(mupa
);
8974 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, 0);
8975 dom
= isl_union_pw_aff_domain(upa
);
8976 for (i
= 1; i
< n
; ++i
) {
8977 isl_union_set
*dom_i
;
8979 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
8980 dom_i
= isl_union_pw_aff_domain(upa
);
8981 dom
= isl_union_set_intersect(dom
, dom_i
);
8984 isl_multi_union_pw_aff_free(mupa
);
8988 /* Apply "aff" to "mupa". The space of "mupa" is equal to the domain of "aff".
8989 * In particular, the spaces have been aligned.
8990 * The result is defined over the shared domain of the elements of "mupa"
8992 * We first extract the parametric constant part of "aff" and
8993 * define that over the shared domain.
8994 * Then we iterate over all input dimensions of "aff" and add the corresponding
8995 * multiples of the elements of "mupa".
8996 * Finally, we consider the integer divisions, calling the function
8997 * recursively to obtain an isl_union_pw_aff corresponding to the
8998 * integer division argument.
9000 static __isl_give isl_union_pw_aff
*multi_union_pw_aff_apply_aff(
9001 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_aff
*aff
)
9004 isl_union_pw_aff
*upa
;
9005 isl_union_set
*uset
;
9009 n_in
= isl_aff_dim(aff
, isl_dim_in
);
9010 n_div
= isl_aff_dim(aff
, isl_dim_div
);
9012 uset
= isl_multi_union_pw_aff_domain(isl_multi_union_pw_aff_copy(mupa
));
9013 cst
= isl_aff_copy(aff
);
9014 cst
= isl_aff_drop_dims(cst
, isl_dim_div
, 0, n_div
);
9015 cst
= isl_aff_drop_dims(cst
, isl_dim_in
, 0, n_in
);
9016 cst
= isl_aff_project_domain_on_params(cst
);
9017 upa
= isl_union_pw_aff_aff_on_domain(uset
, cst
);
9019 for (i
= 0; i
< n_in
; ++i
) {
9020 isl_union_pw_aff
*upa_i
;
9022 if (!isl_aff_involves_dims(aff
, isl_dim_in
, i
, 1))
9024 v
= isl_aff_get_coefficient_val(aff
, isl_dim_in
, i
);
9025 upa_i
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
9026 upa_i
= isl_union_pw_aff_scale_val(upa_i
, v
);
9027 upa
= isl_union_pw_aff_add(upa
, upa_i
);
9030 for (i
= 0; i
< n_div
; ++i
) {
9032 isl_union_pw_aff
*upa_i
;
9034 if (!isl_aff_involves_dims(aff
, isl_dim_div
, i
, 1))
9036 div
= isl_aff_get_div(aff
, i
);
9037 upa_i
= multi_union_pw_aff_apply_aff(
9038 isl_multi_union_pw_aff_copy(mupa
), div
);
9039 upa_i
= isl_union_pw_aff_floor(upa_i
);
9040 v
= isl_aff_get_coefficient_val(aff
, isl_dim_div
, i
);
9041 upa_i
= isl_union_pw_aff_scale_val(upa_i
, v
);
9042 upa
= isl_union_pw_aff_add(upa
, upa_i
);
9045 isl_multi_union_pw_aff_free(mupa
);
9051 /* Apply "aff" to "mupa". The space of "mupa" needs to be compatible
9052 * with the domain of "aff".
9053 * Furthermore, the dimension of this space needs to be greater than zero.
9054 * The result is defined over the shared domain of the elements of "mupa"
9056 * We perform these checks and then hand over control to
9057 * multi_union_pw_aff_apply_aff.
9059 __isl_give isl_union_pw_aff
*isl_multi_union_pw_aff_apply_aff(
9060 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_aff
*aff
)
9062 isl_space
*space1
, *space2
;
9065 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
9066 isl_aff_get_space(aff
));
9067 aff
= isl_aff_align_params(aff
, isl_multi_union_pw_aff_get_space(mupa
));
9071 space1
= isl_multi_union_pw_aff_get_space(mupa
);
9072 space2
= isl_aff_get_domain_space(aff
);
9073 equal
= isl_space_is_equal(space1
, space2
);
9074 isl_space_free(space1
);
9075 isl_space_free(space2
);
9079 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
9080 "spaces don't match", goto error
);
9081 if (isl_aff_dim(aff
, isl_dim_in
) == 0)
9082 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
9083 "cannot determine domains", goto error
);
9085 return multi_union_pw_aff_apply_aff(mupa
, aff
);
9087 isl_multi_union_pw_aff_free(mupa
);
9092 /* Apply "ma" to "mupa", in the special case where "mupa" is 0D.
9093 * The space of "mupa" is known to be compatible with the domain of "ma".
9095 * Construct an isl_multi_union_pw_aff that is equal to "ma"
9096 * on the domain of "mupa".
9098 static __isl_give isl_multi_union_pw_aff
*mupa_apply_multi_aff_0D(
9099 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_multi_aff
*ma
)
9103 dom
= isl_multi_union_pw_aff_domain(mupa
);
9104 ma
= isl_multi_aff_project_domain_on_params(ma
);
9106 return isl_multi_union_pw_aff_multi_aff_on_domain(dom
, ma
);
9109 /* Apply "ma" to "mupa". The space of "mupa" needs to be compatible
9110 * with the domain of "ma".
9111 * The result is defined over the shared domain of the elements of "mupa"
9113 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_apply_multi_aff(
9114 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_multi_aff
*ma
)
9116 isl_space
*space1
, *space2
;
9117 isl_multi_union_pw_aff
*res
;
9121 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
9122 isl_multi_aff_get_space(ma
));
9123 ma
= isl_multi_aff_align_params(ma
,
9124 isl_multi_union_pw_aff_get_space(mupa
));
9128 space1
= isl_multi_union_pw_aff_get_space(mupa
);
9129 space2
= isl_multi_aff_get_domain_space(ma
);
9130 equal
= isl_space_is_equal(space1
, space2
);
9131 isl_space_free(space1
);
9132 isl_space_free(space2
);
9136 isl_die(isl_multi_aff_get_ctx(ma
), isl_error_invalid
,
9137 "spaces don't match", goto error
);
9138 n_out
= isl_multi_aff_dim(ma
, isl_dim_out
);
9139 if (isl_multi_aff_dim(ma
, isl_dim_in
) == 0)
9140 return mupa_apply_multi_aff_0D(mupa
, ma
);
9142 space1
= isl_space_range(isl_multi_aff_get_space(ma
));
9143 res
= isl_multi_union_pw_aff_alloc(space1
);
9145 for (i
= 0; i
< n_out
; ++i
) {
9147 isl_union_pw_aff
*upa
;
9149 aff
= isl_multi_aff_get_aff(ma
, i
);
9150 upa
= multi_union_pw_aff_apply_aff(
9151 isl_multi_union_pw_aff_copy(mupa
), aff
);
9152 res
= isl_multi_union_pw_aff_set_union_pw_aff(res
, i
, upa
);
9155 isl_multi_aff_free(ma
);
9156 isl_multi_union_pw_aff_free(mupa
);
9159 isl_multi_union_pw_aff_free(mupa
);
9160 isl_multi_aff_free(ma
);
9164 /* Apply "pa" to "mupa", in the special case where "mupa" is 0D.
9165 * The space of "mupa" is known to be compatible with the domain of "pa".
9167 * Construct an isl_multi_union_pw_aff that is equal to "pa"
9168 * on the domain of "mupa".
9170 static __isl_give isl_union_pw_aff
*isl_multi_union_pw_aff_apply_pw_aff_0D(
9171 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_pw_aff
*pa
)
9175 dom
= isl_multi_union_pw_aff_domain(mupa
);
9176 pa
= isl_pw_aff_project_domain_on_params(pa
);
9178 return isl_union_pw_aff_pw_aff_on_domain(dom
, pa
);
9181 /* Apply "pa" to "mupa". The space of "mupa" needs to be compatible
9182 * with the domain of "pa".
9183 * Furthermore, the dimension of this space needs to be greater than zero.
9184 * The result is defined over the shared domain of the elements of "mupa"
9186 __isl_give isl_union_pw_aff
*isl_multi_union_pw_aff_apply_pw_aff(
9187 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_pw_aff
*pa
)
9191 isl_space
*space
, *space2
;
9192 isl_union_pw_aff
*upa
;
9194 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
9195 isl_pw_aff_get_space(pa
));
9196 pa
= isl_pw_aff_align_params(pa
,
9197 isl_multi_union_pw_aff_get_space(mupa
));
9201 space
= isl_multi_union_pw_aff_get_space(mupa
);
9202 space2
= isl_pw_aff_get_domain_space(pa
);
9203 equal
= isl_space_is_equal(space
, space2
);
9204 isl_space_free(space
);
9205 isl_space_free(space2
);
9209 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
9210 "spaces don't match", goto error
);
9211 if (isl_pw_aff_dim(pa
, isl_dim_in
) == 0)
9212 return isl_multi_union_pw_aff_apply_pw_aff_0D(mupa
, pa
);
9214 space
= isl_space_params(isl_multi_union_pw_aff_get_space(mupa
));
9215 upa
= isl_union_pw_aff_empty(space
);
9217 for (i
= 0; i
< pa
->n
; ++i
) {
9220 isl_multi_union_pw_aff
*mupa_i
;
9221 isl_union_pw_aff
*upa_i
;
9223 mupa_i
= isl_multi_union_pw_aff_copy(mupa
);
9224 domain
= isl_set_copy(pa
->p
[i
].set
);
9225 mupa_i
= isl_multi_union_pw_aff_intersect_range(mupa_i
, domain
);
9226 aff
= isl_aff_copy(pa
->p
[i
].aff
);
9227 upa_i
= multi_union_pw_aff_apply_aff(mupa_i
, aff
);
9228 upa
= isl_union_pw_aff_union_add(upa
, upa_i
);
9231 isl_multi_union_pw_aff_free(mupa
);
9232 isl_pw_aff_free(pa
);
9235 isl_multi_union_pw_aff_free(mupa
);
9236 isl_pw_aff_free(pa
);
9240 /* Apply "pma" to "mupa", in the special case where "mupa" is 0D.
9241 * The space of "mupa" is known to be compatible with the domain of "pma".
9243 * Construct an isl_multi_union_pw_aff that is equal to "pma"
9244 * on the domain of "mupa".
9246 static __isl_give isl_multi_union_pw_aff
*mupa_apply_pw_multi_aff_0D(
9247 __isl_take isl_multi_union_pw_aff
*mupa
,
9248 __isl_take isl_pw_multi_aff
*pma
)
9252 dom
= isl_multi_union_pw_aff_domain(mupa
);
9253 pma
= isl_pw_multi_aff_project_domain_on_params(pma
);
9255 return isl_multi_union_pw_aff_pw_multi_aff_on_domain(dom
, pma
);
9258 /* Apply "pma" to "mupa". The space of "mupa" needs to be compatible
9259 * with the domain of "pma".
9260 * The result is defined over the shared domain of the elements of "mupa"
9262 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_apply_pw_multi_aff(
9263 __isl_take isl_multi_union_pw_aff
*mupa
,
9264 __isl_take isl_pw_multi_aff
*pma
)
9266 isl_space
*space1
, *space2
;
9267 isl_multi_union_pw_aff
*res
;
9271 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
9272 isl_pw_multi_aff_get_space(pma
));
9273 pma
= isl_pw_multi_aff_align_params(pma
,
9274 isl_multi_union_pw_aff_get_space(mupa
));
9278 space1
= isl_multi_union_pw_aff_get_space(mupa
);
9279 space2
= isl_pw_multi_aff_get_domain_space(pma
);
9280 equal
= isl_space_is_equal(space1
, space2
);
9281 isl_space_free(space1
);
9282 isl_space_free(space2
);
9286 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
9287 "spaces don't match", goto error
);
9288 n_out
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
9289 if (isl_pw_multi_aff_dim(pma
, isl_dim_in
) == 0)
9290 return mupa_apply_pw_multi_aff_0D(mupa
, pma
);
9292 space1
= isl_space_range(isl_pw_multi_aff_get_space(pma
));
9293 res
= isl_multi_union_pw_aff_alloc(space1
);
9295 for (i
= 0; i
< n_out
; ++i
) {
9297 isl_union_pw_aff
*upa
;
9299 pa
= isl_pw_multi_aff_get_pw_aff(pma
, i
);
9300 upa
= isl_multi_union_pw_aff_apply_pw_aff(
9301 isl_multi_union_pw_aff_copy(mupa
), pa
);
9302 res
= isl_multi_union_pw_aff_set_union_pw_aff(res
, i
, upa
);
9305 isl_pw_multi_aff_free(pma
);
9306 isl_multi_union_pw_aff_free(mupa
);
9309 isl_multi_union_pw_aff_free(mupa
);
9310 isl_pw_multi_aff_free(pma
);
9314 /* Replace the explicit domain of "mupa" by its preimage under "upma".
9315 * If the explicit domain only keeps track of constraints on the parameters,
9316 * then only update those constraints.
9318 static __isl_give isl_multi_union_pw_aff
*preimage_explicit_domain(
9319 __isl_take isl_multi_union_pw_aff
*mupa
,
9320 __isl_keep isl_union_pw_multi_aff
*upma
)
9324 if (isl_multi_union_pw_aff_check_has_explicit_domain(mupa
) < 0)
9325 return isl_multi_union_pw_aff_free(mupa
);
9327 mupa
= isl_multi_union_pw_aff_cow(mupa
);
9331 is_params
= isl_union_set_is_params(mupa
->u
.dom
);
9333 return isl_multi_union_pw_aff_free(mupa
);
9335 upma
= isl_union_pw_multi_aff_copy(upma
);
9337 mupa
->u
.dom
= isl_union_set_intersect_params(mupa
->u
.dom
,
9338 isl_union_set_params(isl_union_pw_multi_aff_domain(upma
)));
9340 mupa
->u
.dom
= isl_union_set_preimage_union_pw_multi_aff(
9343 return isl_multi_union_pw_aff_free(mupa
);
9347 /* Compute the pullback of "mupa" by the function represented by "upma".
9348 * In other words, plug in "upma" in "mupa". The result contains
9349 * expressions defined over the domain space of "upma".
9351 * Run over all elements of "mupa" and plug in "upma" in each of them.
9353 * If "mupa" has an explicit domain, then it is this domain
9354 * that needs to undergo a pullback instead, i.e., a preimage.
9356 __isl_give isl_multi_union_pw_aff
*
9357 isl_multi_union_pw_aff_pullback_union_pw_multi_aff(
9358 __isl_take isl_multi_union_pw_aff
*mupa
,
9359 __isl_take isl_union_pw_multi_aff
*upma
)
9363 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
9364 isl_union_pw_multi_aff_get_space(upma
));
9365 upma
= isl_union_pw_multi_aff_align_params(upma
,
9366 isl_multi_union_pw_aff_get_space(mupa
));
9367 mupa
= isl_multi_union_pw_aff_cow(mupa
);
9371 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
9372 for (i
= 0; i
< n
; ++i
) {
9373 isl_union_pw_aff
*upa
;
9375 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
9376 upa
= isl_union_pw_aff_pullback_union_pw_multi_aff(upa
,
9377 isl_union_pw_multi_aff_copy(upma
));
9378 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
9381 if (isl_multi_union_pw_aff_has_explicit_domain(mupa
))
9382 mupa
= preimage_explicit_domain(mupa
, upma
);
9384 isl_union_pw_multi_aff_free(upma
);
9387 isl_multi_union_pw_aff_free(mupa
);
9388 isl_union_pw_multi_aff_free(upma
);
9392 /* Extract the sequence of elements in "mupa" with domain space "space"
9393 * (ignoring parameters).
9395 * For the elements of "mupa" that are not defined on the specified space,
9396 * the corresponding element in the result is empty.
9398 __isl_give isl_multi_pw_aff
*isl_multi_union_pw_aff_extract_multi_pw_aff(
9399 __isl_keep isl_multi_union_pw_aff
*mupa
, __isl_take isl_space
*space
)
9402 isl_space
*space_mpa
;
9403 isl_multi_pw_aff
*mpa
;
9405 if (!mupa
|| !space
)
9408 space_mpa
= isl_multi_union_pw_aff_get_space(mupa
);
9409 space
= isl_space_replace_params(space
, space_mpa
);
9410 space_mpa
= isl_space_map_from_domain_and_range(isl_space_copy(space
),
9412 mpa
= isl_multi_pw_aff_alloc(space_mpa
);
9414 space
= isl_space_from_domain(space
);
9415 space
= isl_space_add_dims(space
, isl_dim_out
, 1);
9416 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
9417 for (i
= 0; i
< n
; ++i
) {
9418 isl_union_pw_aff
*upa
;
9421 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
9422 pa
= isl_union_pw_aff_extract_pw_aff(upa
,
9423 isl_space_copy(space
));
9424 mpa
= isl_multi_pw_aff_set_pw_aff(mpa
, i
, pa
);
9425 isl_union_pw_aff_free(upa
);
9428 isl_space_free(space
);
9431 isl_space_free(space
);
9435 /* Evaluate the affine function "aff" in the void point "pnt".
9436 * In particular, return the value NaN.
9438 static __isl_give isl_val
*eval_void(__isl_take isl_aff
*aff
,
9439 __isl_take isl_point
*pnt
)
9443 ctx
= isl_point_get_ctx(pnt
);
9445 isl_point_free(pnt
);
9446 return isl_val_nan(ctx
);
9449 /* Evaluate the affine expression "aff"
9450 * in the coordinates (with denominator) "pnt".
9452 static __isl_give isl_val
*eval(__isl_keep isl_vec
*aff
,
9453 __isl_keep isl_vec
*pnt
)
9462 ctx
= isl_vec_get_ctx(aff
);
9465 isl_seq_inner_product(aff
->el
+ 1, pnt
->el
, pnt
->size
, &n
);
9466 isl_int_mul(d
, aff
->el
[0], pnt
->el
[0]);
9467 v
= isl_val_rat_from_isl_int(ctx
, n
, d
);
9468 v
= isl_val_normalize(v
);
9475 /* Check that the domain space of "aff" is equal to "space".
9477 static isl_stat
isl_aff_check_has_domain_space(__isl_keep isl_aff
*aff
,
9478 __isl_keep isl_space
*space
)
9482 ok
= isl_space_is_equal(isl_aff_peek_domain_space(aff
), space
);
9484 return isl_stat_error
;
9486 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
9487 "incompatible spaces", return isl_stat_error
);
9491 /* Evaluate the affine function "aff" in "pnt".
9493 __isl_give isl_val
*isl_aff_eval(__isl_take isl_aff
*aff
,
9494 __isl_take isl_point
*pnt
)
9498 isl_local_space
*ls
;
9500 if (isl_aff_check_has_domain_space(aff
, isl_point_peek_space(pnt
)) < 0)
9502 is_void
= isl_point_is_void(pnt
);
9506 return eval_void(aff
, pnt
);
9508 ls
= isl_aff_get_domain_local_space(aff
);
9509 pnt
= isl_local_space_lift_point(ls
, pnt
);
9511 v
= eval(aff
->v
, isl_point_peek_vec(pnt
));
9514 isl_point_free(pnt
);
9519 isl_point_free(pnt
);