2 * Copyright 2011 INRIA Saclay
3 * Copyright 2011 Sven Verdoolaege
4 * Copyright 2012-2014 Ecole Normale Superieure
5 * Copyright 2014 INRIA Rocquencourt
6 * Copyright 2018 Cerebras Systems
8 * Use of this software is governed by the MIT license
10 * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
11 * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
13 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
14 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
15 * B.P. 105 - 78153 Le Chesnay, France
16 * and Cerebras Systems, 175 S San Antonio Rd, Los Altos, CA, USA
19 #include <isl_ctx_private.h>
20 #include <isl_map_private.h>
21 #include <isl_union_map_private.h>
22 #include <isl_aff_private.h>
23 #include <isl_space_private.h>
24 #include <isl_local_space_private.h>
25 #include <isl_vec_private.h>
26 #include <isl_mat_private.h>
28 #include <isl/constraint.h>
31 #include <isl_val_private.h>
32 #include <isl_point_private.h>
33 #include <isl_config.h>
38 #include <isl_list_templ.c>
41 #define EL_BASE pw_aff
43 #include <isl_list_templ.c>
46 #define EL_BASE pw_multi_aff
48 #include <isl_list_templ.c>
51 #define EL_BASE union_pw_aff
53 #include <isl_list_templ.c>
56 #define EL_BASE union_pw_multi_aff
58 #include <isl_list_templ.c>
60 __isl_give isl_aff
*isl_aff_alloc_vec(__isl_take isl_local_space
*ls
,
61 __isl_take isl_vec
*v
)
68 aff
= isl_calloc_type(v
->ctx
, struct isl_aff
);
78 isl_local_space_free(ls
);
83 __isl_give isl_aff
*isl_aff_alloc(__isl_take isl_local_space
*ls
)
92 ctx
= isl_local_space_get_ctx(ls
);
93 if (!isl_local_space_divs_known(ls
))
94 isl_die(ctx
, isl_error_invalid
, "local space has unknown divs",
96 if (!isl_local_space_is_set(ls
))
97 isl_die(ctx
, isl_error_invalid
,
98 "domain of affine expression should be a set",
101 total
= isl_local_space_dim(ls
, isl_dim_all
);
104 v
= isl_vec_alloc(ctx
, 1 + 1 + total
);
105 return isl_aff_alloc_vec(ls
, v
);
107 isl_local_space_free(ls
);
111 __isl_give isl_aff
*isl_aff_zero_on_domain(__isl_take isl_local_space
*ls
)
115 aff
= isl_aff_alloc(ls
);
119 isl_int_set_si(aff
->v
->el
[0], 1);
120 isl_seq_clr(aff
->v
->el
+ 1, aff
->v
->size
- 1);
125 /* Return a piecewise affine expression defined on the specified domain
126 * that is equal to zero.
128 __isl_give isl_pw_aff
*isl_pw_aff_zero_on_domain(__isl_take isl_local_space
*ls
)
130 return isl_pw_aff_from_aff(isl_aff_zero_on_domain(ls
));
133 /* Return an affine expression defined on the specified domain
134 * that represents NaN.
136 __isl_give isl_aff
*isl_aff_nan_on_domain(__isl_take isl_local_space
*ls
)
140 aff
= isl_aff_alloc(ls
);
144 isl_seq_clr(aff
->v
->el
, aff
->v
->size
);
149 /* Return a piecewise affine expression defined on the specified domain
150 * that represents NaN.
152 __isl_give isl_pw_aff
*isl_pw_aff_nan_on_domain(__isl_take isl_local_space
*ls
)
154 return isl_pw_aff_from_aff(isl_aff_nan_on_domain(ls
));
157 /* Return an affine expression that is equal to "val" on
158 * domain local space "ls".
160 __isl_give isl_aff
*isl_aff_val_on_domain(__isl_take isl_local_space
*ls
,
161 __isl_take isl_val
*val
)
167 if (!isl_val_is_rat(val
))
168 isl_die(isl_val_get_ctx(val
), isl_error_invalid
,
169 "expecting rational value", goto error
);
171 aff
= isl_aff_alloc(isl_local_space_copy(ls
));
175 isl_seq_clr(aff
->v
->el
+ 2, aff
->v
->size
- 2);
176 isl_int_set(aff
->v
->el
[1], val
->n
);
177 isl_int_set(aff
->v
->el
[0], val
->d
);
179 isl_local_space_free(ls
);
183 isl_local_space_free(ls
);
188 /* Return an affine expression that is equal to the specified dimension
191 __isl_give isl_aff
*isl_aff_var_on_domain(__isl_take isl_local_space
*ls
,
192 enum isl_dim_type type
, unsigned pos
)
200 space
= isl_local_space_get_space(ls
);
203 if (isl_space_is_map(space
))
204 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
205 "expecting (parameter) set space", goto error
);
206 if (isl_local_space_check_range(ls
, type
, pos
, 1) < 0)
209 isl_space_free(space
);
210 aff
= isl_aff_alloc(ls
);
214 pos
+= isl_local_space_offset(aff
->ls
, type
);
216 isl_int_set_si(aff
->v
->el
[0], 1);
217 isl_seq_clr(aff
->v
->el
+ 1, aff
->v
->size
- 1);
218 isl_int_set_si(aff
->v
->el
[1 + pos
], 1);
222 isl_local_space_free(ls
);
223 isl_space_free(space
);
227 /* Return a piecewise affine expression that is equal to
228 * the specified dimension in "ls".
230 __isl_give isl_pw_aff
*isl_pw_aff_var_on_domain(__isl_take isl_local_space
*ls
,
231 enum isl_dim_type type
, unsigned pos
)
233 return isl_pw_aff_from_aff(isl_aff_var_on_domain(ls
, type
, pos
));
236 /* Return an affine expression that is equal to the parameter
237 * in the domain space "space" with identifier "id".
239 __isl_give isl_aff
*isl_aff_param_on_domain_space_id(
240 __isl_take isl_space
*space
, __isl_take isl_id
*id
)
247 pos
= isl_space_find_dim_by_id(space
, isl_dim_param
, id
);
249 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
250 "parameter not found in space", goto error
);
252 ls
= isl_local_space_from_space(space
);
253 return isl_aff_var_on_domain(ls
, isl_dim_param
, pos
);
255 isl_space_free(space
);
260 __isl_give isl_aff
*isl_aff_copy(__isl_keep isl_aff
*aff
)
269 __isl_give isl_aff
*isl_aff_dup(__isl_keep isl_aff
*aff
)
274 return isl_aff_alloc_vec(isl_local_space_copy(aff
->ls
),
275 isl_vec_copy(aff
->v
));
278 __isl_give isl_aff
*isl_aff_cow(__isl_take isl_aff
*aff
)
286 return isl_aff_dup(aff
);
289 __isl_null isl_aff
*isl_aff_free(__isl_take isl_aff
*aff
)
297 isl_local_space_free(aff
->ls
);
298 isl_vec_free(aff
->v
);
305 isl_ctx
*isl_aff_get_ctx(__isl_keep isl_aff
*aff
)
307 return aff
? isl_local_space_get_ctx(aff
->ls
) : NULL
;
310 /* Return a hash value that digests "aff".
312 uint32_t isl_aff_get_hash(__isl_keep isl_aff
*aff
)
314 uint32_t hash
, ls_hash
, v_hash
;
319 hash
= isl_hash_init();
320 ls_hash
= isl_local_space_get_hash(aff
->ls
);
321 isl_hash_hash(hash
, ls_hash
);
322 v_hash
= isl_vec_get_hash(aff
->v
);
323 isl_hash_hash(hash
, v_hash
);
328 /* Externally, an isl_aff has a map space, but internally, the
329 * ls field corresponds to the domain of that space.
331 isl_size
isl_aff_dim(__isl_keep isl_aff
*aff
, enum isl_dim_type type
)
334 return isl_size_error
;
335 if (type
== isl_dim_out
)
337 if (type
== isl_dim_in
)
339 return isl_local_space_dim(aff
->ls
, type
);
342 /* Return the position of the dimension of the given type and name
344 * Return -1 if no such dimension can be found.
346 int isl_aff_find_dim_by_name(__isl_keep isl_aff
*aff
, enum isl_dim_type type
,
351 if (type
== isl_dim_out
)
353 if (type
== isl_dim_in
)
355 return isl_local_space_find_dim_by_name(aff
->ls
, type
, name
);
358 /* Return the domain space of "aff".
360 static __isl_keep isl_space
*isl_aff_peek_domain_space(__isl_keep isl_aff
*aff
)
362 return aff
? isl_local_space_peek_space(aff
->ls
) : NULL
;
365 __isl_give isl_space
*isl_aff_get_domain_space(__isl_keep isl_aff
*aff
)
367 return isl_space_copy(isl_aff_peek_domain_space(aff
));
370 __isl_give isl_space
*isl_aff_get_space(__isl_keep isl_aff
*aff
)
375 space
= isl_local_space_get_space(aff
->ls
);
376 space
= isl_space_from_domain(space
);
377 space
= isl_space_add_dims(space
, isl_dim_out
, 1);
381 __isl_give isl_local_space
*isl_aff_get_domain_local_space(
382 __isl_keep isl_aff
*aff
)
384 return aff
? isl_local_space_copy(aff
->ls
) : NULL
;
387 __isl_give isl_local_space
*isl_aff_get_local_space(__isl_keep isl_aff
*aff
)
392 ls
= isl_local_space_copy(aff
->ls
);
393 ls
= isl_local_space_from_domain(ls
);
394 ls
= isl_local_space_add_dims(ls
, isl_dim_out
, 1);
398 /* Return the local space of the domain of "aff".
399 * This may be either a copy or the local space itself
400 * if there is only one reference to "aff".
401 * This allows the local space to be modified inplace
402 * if both the expression and its local space have only a single reference.
403 * The caller is not allowed to modify "aff" between this call and
404 * a subsequent call to isl_aff_restore_domain_local_space.
405 * The only exception is that isl_aff_free can be called instead.
407 __isl_give isl_local_space
*isl_aff_take_domain_local_space(
408 __isl_keep isl_aff
*aff
)
415 return isl_aff_get_domain_local_space(aff
);
421 /* Set the local space of the domain of "aff" to "ls",
422 * where the local space of "aff" may be missing
423 * due to a preceding call to isl_aff_take_domain_local_space.
424 * However, in this case, "aff" only has a single reference and
425 * then the call to isl_aff_cow has no effect.
427 __isl_give isl_aff
*isl_aff_restore_domain_local_space(
428 __isl_keep isl_aff
*aff
, __isl_take isl_local_space
*ls
)
434 isl_local_space_free(ls
);
438 aff
= isl_aff_cow(aff
);
441 isl_local_space_free(aff
->ls
);
447 isl_local_space_free(ls
);
451 /* Externally, an isl_aff has a map space, but internally, the
452 * ls field corresponds to the domain of that space.
454 const char *isl_aff_get_dim_name(__isl_keep isl_aff
*aff
,
455 enum isl_dim_type type
, unsigned pos
)
459 if (type
== isl_dim_out
)
461 if (type
== isl_dim_in
)
463 return isl_local_space_get_dim_name(aff
->ls
, type
, pos
);
466 __isl_give isl_aff
*isl_aff_reset_domain_space(__isl_take isl_aff
*aff
,
467 __isl_take isl_space
*dim
)
469 aff
= isl_aff_cow(aff
);
473 aff
->ls
= isl_local_space_reset_space(aff
->ls
, dim
);
475 return isl_aff_free(aff
);
484 /* Reset the space of "aff". This function is called from isl_pw_templ.c
485 * and doesn't know if the space of an element object is represented
486 * directly or through its domain. It therefore passes along both.
488 __isl_give isl_aff
*isl_aff_reset_space_and_domain(__isl_take isl_aff
*aff
,
489 __isl_take isl_space
*space
, __isl_take isl_space
*domain
)
491 isl_space_free(space
);
492 return isl_aff_reset_domain_space(aff
, domain
);
495 /* Reorder the coefficients of the affine expression based
496 * on the given reordering.
497 * The reordering r is assumed to have been extended with the local
500 static __isl_give isl_vec
*vec_reorder(__isl_take isl_vec
*vec
,
501 __isl_take isl_reordering
*r
, int n_div
)
511 space
= isl_reordering_peek_space(r
);
512 dim
= isl_space_dim(space
, isl_dim_all
);
515 res
= isl_vec_alloc(vec
->ctx
, 2 + dim
+ n_div
);
518 isl_seq_cpy(res
->el
, vec
->el
, 2);
519 isl_seq_clr(res
->el
+ 2, res
->size
- 2);
520 for (i
= 0; i
< r
->len
; ++i
)
521 isl_int_set(res
->el
[2 + r
->pos
[i
]], vec
->el
[2 + i
]);
523 isl_reordering_free(r
);
528 isl_reordering_free(r
);
532 /* Reorder the dimensions of the domain of "aff" according
533 * to the given reordering.
535 __isl_give isl_aff
*isl_aff_realign_domain(__isl_take isl_aff
*aff
,
536 __isl_take isl_reordering
*r
)
538 aff
= isl_aff_cow(aff
);
542 r
= isl_reordering_extend(r
, aff
->ls
->div
->n_row
);
543 aff
->v
= vec_reorder(aff
->v
, isl_reordering_copy(r
),
544 aff
->ls
->div
->n_row
);
545 aff
->ls
= isl_local_space_realign(aff
->ls
, r
);
547 if (!aff
->v
|| !aff
->ls
)
548 return isl_aff_free(aff
);
553 isl_reordering_free(r
);
557 __isl_give isl_aff
*isl_aff_align_params(__isl_take isl_aff
*aff
,
558 __isl_take isl_space
*model
)
560 isl_bool equal_params
;
565 equal_params
= isl_space_has_equal_params(aff
->ls
->dim
, model
);
566 if (equal_params
< 0)
571 exp
= isl_parameter_alignment_reordering(aff
->ls
->dim
, model
);
572 exp
= isl_reordering_extend_space(exp
,
573 isl_aff_get_domain_space(aff
));
574 aff
= isl_aff_realign_domain(aff
, exp
);
577 isl_space_free(model
);
580 isl_space_free(model
);
585 /* Given an affine function "aff" defined over a parameter domain,
586 * convert it to a function defined over a domain corresponding
588 * Any parameters with identifiers in "domain" are reinterpreted
589 * as the corresponding domain dimensions.
591 __isl_give isl_aff
*isl_aff_unbind_params_insert_domain(
592 __isl_take isl_aff
*aff
, __isl_take isl_multi_id
*domain
)
598 space
= isl_aff_peek_domain_space(aff
);
599 is_params
= isl_space_is_params(space
);
601 domain
= isl_multi_id_free(domain
);
603 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
604 "expecting function with parameter domain",
605 domain
= isl_multi_id_free(domain
));
606 r
= isl_reordering_unbind_params_insert_domain(space
, domain
);
607 isl_multi_id_free(domain
);
609 return isl_aff_realign_domain(aff
, r
);
612 /* Is "aff" obviously equal to zero?
614 * If the denominator is zero, then "aff" is not equal to zero.
616 isl_bool
isl_aff_plain_is_zero(__isl_keep isl_aff
*aff
)
621 return isl_bool_error
;
623 if (isl_int_is_zero(aff
->v
->el
[0]))
624 return isl_bool_false
;
625 pos
= isl_seq_first_non_zero(aff
->v
->el
+ 1, aff
->v
->size
- 1);
626 return isl_bool_ok(pos
< 0);
629 /* Does "aff" represent NaN?
631 isl_bool
isl_aff_is_nan(__isl_keep isl_aff
*aff
)
634 return isl_bool_error
;
636 return isl_bool_ok(isl_seq_first_non_zero(aff
->v
->el
, 2) < 0);
639 /* Are "aff1" and "aff2" obviously equal?
641 * NaN is not equal to anything, not even to another NaN.
643 isl_bool
isl_aff_plain_is_equal(__isl_keep isl_aff
*aff1
,
644 __isl_keep isl_aff
*aff2
)
649 return isl_bool_error
;
651 if (isl_aff_is_nan(aff1
) || isl_aff_is_nan(aff2
))
652 return isl_bool_false
;
654 equal
= isl_local_space_is_equal(aff1
->ls
, aff2
->ls
);
655 if (equal
< 0 || !equal
)
658 return isl_vec_is_equal(aff1
->v
, aff2
->v
);
661 /* Return the common denominator of "aff" in "v".
663 * We cannot return anything meaningful in case of a NaN.
665 isl_stat
isl_aff_get_denominator(__isl_keep isl_aff
*aff
, isl_int
*v
)
668 return isl_stat_error
;
669 if (isl_aff_is_nan(aff
))
670 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
671 "cannot get denominator of NaN", return isl_stat_error
);
672 isl_int_set(*v
, aff
->v
->el
[0]);
676 /* Return the common denominator of "aff".
678 __isl_give isl_val
*isl_aff_get_denominator_val(__isl_keep isl_aff
*aff
)
685 ctx
= isl_aff_get_ctx(aff
);
686 if (isl_aff_is_nan(aff
))
687 return isl_val_nan(ctx
);
688 return isl_val_int_from_isl_int(ctx
, aff
->v
->el
[0]);
691 /* Return the constant term of "aff".
693 __isl_give isl_val
*isl_aff_get_constant_val(__isl_keep isl_aff
*aff
)
701 ctx
= isl_aff_get_ctx(aff
);
702 if (isl_aff_is_nan(aff
))
703 return isl_val_nan(ctx
);
704 v
= isl_val_rat_from_isl_int(ctx
, aff
->v
->el
[1], aff
->v
->el
[0]);
705 return isl_val_normalize(v
);
708 /* Return the coefficient of the variable of type "type" at position "pos"
711 __isl_give isl_val
*isl_aff_get_coefficient_val(__isl_keep isl_aff
*aff
,
712 enum isl_dim_type type
, int pos
)
720 ctx
= isl_aff_get_ctx(aff
);
721 if (type
== isl_dim_out
)
722 isl_die(ctx
, isl_error_invalid
,
723 "output/set dimension does not have a coefficient",
725 if (type
== isl_dim_in
)
728 if (isl_local_space_check_range(aff
->ls
, type
, pos
, 1) < 0)
731 if (isl_aff_is_nan(aff
))
732 return isl_val_nan(ctx
);
733 pos
+= isl_local_space_offset(aff
->ls
, type
);
734 v
= isl_val_rat_from_isl_int(ctx
, aff
->v
->el
[1 + pos
], aff
->v
->el
[0]);
735 return isl_val_normalize(v
);
738 /* Return the sign of the coefficient of the variable of type "type"
739 * at position "pos" of "aff".
741 int isl_aff_coefficient_sgn(__isl_keep isl_aff
*aff
, enum isl_dim_type type
,
749 ctx
= isl_aff_get_ctx(aff
);
750 if (type
== isl_dim_out
)
751 isl_die(ctx
, isl_error_invalid
,
752 "output/set dimension does not have a coefficient",
754 if (type
== isl_dim_in
)
757 if (isl_local_space_check_range(aff
->ls
, type
, pos
, 1) < 0)
760 pos
+= isl_local_space_offset(aff
->ls
, type
);
761 return isl_int_sgn(aff
->v
->el
[1 + pos
]);
764 /* Replace the numerator of the constant term of "aff" by "v".
766 * A NaN is unaffected by this operation.
768 __isl_give isl_aff
*isl_aff_set_constant(__isl_take isl_aff
*aff
, isl_int v
)
772 if (isl_aff_is_nan(aff
))
774 aff
= isl_aff_cow(aff
);
778 aff
->v
= isl_vec_cow(aff
->v
);
780 return isl_aff_free(aff
);
782 isl_int_set(aff
->v
->el
[1], v
);
787 /* Replace the constant term of "aff" by "v".
789 * A NaN is unaffected by this operation.
791 __isl_give isl_aff
*isl_aff_set_constant_val(__isl_take isl_aff
*aff
,
792 __isl_take isl_val
*v
)
797 if (isl_aff_is_nan(aff
)) {
802 if (!isl_val_is_rat(v
))
803 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
804 "expecting rational value", goto error
);
806 if (isl_int_eq(aff
->v
->el
[1], v
->n
) &&
807 isl_int_eq(aff
->v
->el
[0], v
->d
)) {
812 aff
= isl_aff_cow(aff
);
815 aff
->v
= isl_vec_cow(aff
->v
);
819 if (isl_int_eq(aff
->v
->el
[0], v
->d
)) {
820 isl_int_set(aff
->v
->el
[1], v
->n
);
821 } else if (isl_int_is_one(v
->d
)) {
822 isl_int_mul(aff
->v
->el
[1], aff
->v
->el
[0], v
->n
);
824 isl_seq_scale(aff
->v
->el
+ 1,
825 aff
->v
->el
+ 1, v
->d
, aff
->v
->size
- 1);
826 isl_int_mul(aff
->v
->el
[1], aff
->v
->el
[0], v
->n
);
827 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], v
->d
);
828 aff
->v
= isl_vec_normalize(aff
->v
);
841 /* Add "v" to the constant term of "aff".
843 * A NaN is unaffected by this operation.
845 __isl_give isl_aff
*isl_aff_add_constant(__isl_take isl_aff
*aff
, isl_int v
)
847 if (isl_int_is_zero(v
))
852 if (isl_aff_is_nan(aff
))
854 aff
= isl_aff_cow(aff
);
858 aff
->v
= isl_vec_cow(aff
->v
);
860 return isl_aff_free(aff
);
862 isl_int_addmul(aff
->v
->el
[1], aff
->v
->el
[0], v
);
867 /* Add "v" to the constant term of "aff".
869 * A NaN is unaffected by this operation.
871 __isl_give isl_aff
*isl_aff_add_constant_val(__isl_take isl_aff
*aff
,
872 __isl_take isl_val
*v
)
877 if (isl_aff_is_nan(aff
) || isl_val_is_zero(v
)) {
882 if (!isl_val_is_rat(v
))
883 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
884 "expecting rational value", goto error
);
886 aff
= isl_aff_cow(aff
);
890 aff
->v
= isl_vec_cow(aff
->v
);
894 if (isl_int_is_one(v
->d
)) {
895 isl_int_addmul(aff
->v
->el
[1], aff
->v
->el
[0], v
->n
);
896 } else if (isl_int_eq(aff
->v
->el
[0], v
->d
)) {
897 isl_int_add(aff
->v
->el
[1], aff
->v
->el
[1], v
->n
);
898 aff
->v
= isl_vec_normalize(aff
->v
);
902 isl_seq_scale(aff
->v
->el
+ 1,
903 aff
->v
->el
+ 1, v
->d
, aff
->v
->size
- 1);
904 isl_int_addmul(aff
->v
->el
[1], aff
->v
->el
[0], v
->n
);
905 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], v
->d
);
906 aff
->v
= isl_vec_normalize(aff
->v
);
919 __isl_give isl_aff
*isl_aff_add_constant_si(__isl_take isl_aff
*aff
, int v
)
924 isl_int_set_si(t
, v
);
925 aff
= isl_aff_add_constant(aff
, t
);
931 /* Add "v" to the numerator of the constant term of "aff".
933 * A NaN is unaffected by this operation.
935 __isl_give isl_aff
*isl_aff_add_constant_num(__isl_take isl_aff
*aff
, isl_int v
)
937 if (isl_int_is_zero(v
))
942 if (isl_aff_is_nan(aff
))
944 aff
= isl_aff_cow(aff
);
948 aff
->v
= isl_vec_cow(aff
->v
);
950 return isl_aff_free(aff
);
952 isl_int_add(aff
->v
->el
[1], aff
->v
->el
[1], v
);
957 /* Add "v" to the numerator of the constant term of "aff".
959 * A NaN is unaffected by this operation.
961 __isl_give isl_aff
*isl_aff_add_constant_num_si(__isl_take isl_aff
*aff
, int v
)
969 isl_int_set_si(t
, v
);
970 aff
= isl_aff_add_constant_num(aff
, t
);
976 /* Replace the numerator of the constant term of "aff" by "v".
978 * A NaN is unaffected by this operation.
980 __isl_give isl_aff
*isl_aff_set_constant_si(__isl_take isl_aff
*aff
, int v
)
984 if (isl_aff_is_nan(aff
))
986 aff
= isl_aff_cow(aff
);
990 aff
->v
= isl_vec_cow(aff
->v
);
992 return isl_aff_free(aff
);
994 isl_int_set_si(aff
->v
->el
[1], v
);
999 /* Replace the numerator of the coefficient of the variable of type "type"
1000 * at position "pos" of "aff" by "v".
1002 * A NaN is unaffected by this operation.
1004 __isl_give isl_aff
*isl_aff_set_coefficient(__isl_take isl_aff
*aff
,
1005 enum isl_dim_type type
, int pos
, isl_int v
)
1010 if (type
== isl_dim_out
)
1011 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1012 "output/set dimension does not have a coefficient",
1013 return isl_aff_free(aff
));
1014 if (type
== isl_dim_in
)
1017 if (isl_local_space_check_range(aff
->ls
, type
, pos
, 1) < 0)
1018 return isl_aff_free(aff
);
1020 if (isl_aff_is_nan(aff
))
1022 aff
= isl_aff_cow(aff
);
1026 aff
->v
= isl_vec_cow(aff
->v
);
1028 return isl_aff_free(aff
);
1030 pos
+= isl_local_space_offset(aff
->ls
, type
);
1031 isl_int_set(aff
->v
->el
[1 + pos
], v
);
1036 /* Replace the numerator of the coefficient of the variable of type "type"
1037 * at position "pos" of "aff" by "v".
1039 * A NaN is unaffected by this operation.
1041 __isl_give isl_aff
*isl_aff_set_coefficient_si(__isl_take isl_aff
*aff
,
1042 enum isl_dim_type type
, int pos
, int v
)
1047 if (type
== isl_dim_out
)
1048 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1049 "output/set dimension does not have a coefficient",
1050 return isl_aff_free(aff
));
1051 if (type
== isl_dim_in
)
1054 if (isl_local_space_check_range(aff
->ls
, type
, pos
, 1) < 0)
1055 return isl_aff_free(aff
);
1057 if (isl_aff_is_nan(aff
))
1059 pos
+= isl_local_space_offset(aff
->ls
, type
);
1060 if (isl_int_cmp_si(aff
->v
->el
[1 + pos
], v
) == 0)
1063 aff
= isl_aff_cow(aff
);
1067 aff
->v
= isl_vec_cow(aff
->v
);
1069 return isl_aff_free(aff
);
1071 isl_int_set_si(aff
->v
->el
[1 + pos
], v
);
1076 /* Replace the coefficient of the variable of type "type" at position "pos"
1079 * A NaN is unaffected by this operation.
1081 __isl_give isl_aff
*isl_aff_set_coefficient_val(__isl_take isl_aff
*aff
,
1082 enum isl_dim_type type
, int pos
, __isl_take isl_val
*v
)
1087 if (type
== isl_dim_out
)
1088 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1089 "output/set dimension does not have a coefficient",
1091 if (type
== isl_dim_in
)
1094 if (isl_local_space_check_range(aff
->ls
, type
, pos
, 1) < 0)
1095 return isl_aff_free(aff
);
1097 if (isl_aff_is_nan(aff
)) {
1101 if (!isl_val_is_rat(v
))
1102 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1103 "expecting rational value", goto error
);
1105 pos
+= isl_local_space_offset(aff
->ls
, type
);
1106 if (isl_int_eq(aff
->v
->el
[1 + pos
], v
->n
) &&
1107 isl_int_eq(aff
->v
->el
[0], v
->d
)) {
1112 aff
= isl_aff_cow(aff
);
1115 aff
->v
= isl_vec_cow(aff
->v
);
1119 if (isl_int_eq(aff
->v
->el
[0], v
->d
)) {
1120 isl_int_set(aff
->v
->el
[1 + pos
], v
->n
);
1121 } else if (isl_int_is_one(v
->d
)) {
1122 isl_int_mul(aff
->v
->el
[1 + pos
], aff
->v
->el
[0], v
->n
);
1124 isl_seq_scale(aff
->v
->el
+ 1,
1125 aff
->v
->el
+ 1, v
->d
, aff
->v
->size
- 1);
1126 isl_int_mul(aff
->v
->el
[1 + pos
], aff
->v
->el
[0], v
->n
);
1127 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], v
->d
);
1128 aff
->v
= isl_vec_normalize(aff
->v
);
1141 /* Add "v" to the coefficient of the variable of type "type"
1142 * at position "pos" of "aff".
1144 * A NaN is unaffected by this operation.
1146 __isl_give isl_aff
*isl_aff_add_coefficient(__isl_take isl_aff
*aff
,
1147 enum isl_dim_type type
, int pos
, isl_int v
)
1152 if (type
== isl_dim_out
)
1153 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1154 "output/set dimension does not have a coefficient",
1155 return isl_aff_free(aff
));
1156 if (type
== isl_dim_in
)
1159 if (isl_local_space_check_range(aff
->ls
, type
, pos
, 1) < 0)
1160 return isl_aff_free(aff
);
1162 if (isl_aff_is_nan(aff
))
1164 aff
= isl_aff_cow(aff
);
1168 aff
->v
= isl_vec_cow(aff
->v
);
1170 return isl_aff_free(aff
);
1172 pos
+= isl_local_space_offset(aff
->ls
, type
);
1173 isl_int_addmul(aff
->v
->el
[1 + pos
], aff
->v
->el
[0], v
);
1178 /* Add "v" to the coefficient of the variable of type "type"
1179 * at position "pos" of "aff".
1181 * A NaN is unaffected by this operation.
1183 __isl_give isl_aff
*isl_aff_add_coefficient_val(__isl_take isl_aff
*aff
,
1184 enum isl_dim_type type
, int pos
, __isl_take isl_val
*v
)
1189 if (isl_val_is_zero(v
)) {
1194 if (type
== isl_dim_out
)
1195 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1196 "output/set dimension does not have a coefficient",
1198 if (type
== isl_dim_in
)
1201 if (isl_local_space_check_range(aff
->ls
, type
, pos
, 1) < 0)
1204 if (isl_aff_is_nan(aff
)) {
1208 if (!isl_val_is_rat(v
))
1209 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1210 "expecting rational value", goto error
);
1212 aff
= isl_aff_cow(aff
);
1216 aff
->v
= isl_vec_cow(aff
->v
);
1220 pos
+= isl_local_space_offset(aff
->ls
, type
);
1221 if (isl_int_is_one(v
->d
)) {
1222 isl_int_addmul(aff
->v
->el
[1 + pos
], aff
->v
->el
[0], v
->n
);
1223 } else if (isl_int_eq(aff
->v
->el
[0], v
->d
)) {
1224 isl_int_add(aff
->v
->el
[1 + pos
], aff
->v
->el
[1 + pos
], v
->n
);
1225 aff
->v
= isl_vec_normalize(aff
->v
);
1229 isl_seq_scale(aff
->v
->el
+ 1,
1230 aff
->v
->el
+ 1, v
->d
, aff
->v
->size
- 1);
1231 isl_int_addmul(aff
->v
->el
[1 + pos
], aff
->v
->el
[0], v
->n
);
1232 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], v
->d
);
1233 aff
->v
= isl_vec_normalize(aff
->v
);
1246 __isl_give isl_aff
*isl_aff_add_coefficient_si(__isl_take isl_aff
*aff
,
1247 enum isl_dim_type type
, int pos
, int v
)
1252 isl_int_set_si(t
, v
);
1253 aff
= isl_aff_add_coefficient(aff
, type
, pos
, t
);
1259 __isl_give isl_aff
*isl_aff_get_div(__isl_keep isl_aff
*aff
, int pos
)
1264 return isl_local_space_get_div(aff
->ls
, pos
);
1267 /* Return the negation of "aff".
1269 * As a special case, -NaN = NaN.
1271 __isl_give isl_aff
*isl_aff_neg(__isl_take isl_aff
*aff
)
1275 if (isl_aff_is_nan(aff
))
1277 aff
= isl_aff_cow(aff
);
1280 aff
->v
= isl_vec_cow(aff
->v
);
1282 return isl_aff_free(aff
);
1284 isl_seq_neg(aff
->v
->el
+ 1, aff
->v
->el
+ 1, aff
->v
->size
- 1);
1289 /* Remove divs from the local space that do not appear in the affine
1291 * We currently only remove divs at the end.
1292 * Some intermediate divs may also not appear directly in the affine
1293 * expression, but we would also need to check that no other divs are
1294 * defined in terms of them.
1296 __isl_give isl_aff
*isl_aff_remove_unused_divs(__isl_take isl_aff
*aff
)
1305 n
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
1307 return isl_aff_free(aff
);
1308 off
= isl_local_space_offset(aff
->ls
, isl_dim_div
);
1310 pos
= isl_seq_last_non_zero(aff
->v
->el
+ 1 + off
, n
) + 1;
1314 aff
= isl_aff_cow(aff
);
1318 aff
->ls
= isl_local_space_drop_dims(aff
->ls
, isl_dim_div
, pos
, n
- pos
);
1319 aff
->v
= isl_vec_drop_els(aff
->v
, 1 + off
+ pos
, n
- pos
);
1320 if (!aff
->ls
|| !aff
->v
)
1321 return isl_aff_free(aff
);
1326 /* Look for any divs in the aff->ls with a denominator equal to one
1327 * and plug them into the affine expression and any subsequent divs
1328 * that may reference the div.
1330 static __isl_give isl_aff
*plug_in_integral_divs(__isl_take isl_aff
*aff
)
1337 isl_local_space
*ls
;
1343 n
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
1345 return isl_aff_free(aff
);
1347 for (i
= 0; i
< n
; ++i
) {
1348 if (!isl_int_is_one(aff
->ls
->div
->row
[i
][0]))
1350 ls
= isl_local_space_copy(aff
->ls
);
1351 ls
= isl_local_space_substitute_seq(ls
, isl_dim_div
, i
,
1352 aff
->ls
->div
->row
[i
], len
, i
+ 1, n
- (i
+ 1));
1353 vec
= isl_vec_copy(aff
->v
);
1354 vec
= isl_vec_cow(vec
);
1360 pos
= isl_local_space_offset(aff
->ls
, isl_dim_div
) + i
;
1361 isl_seq_substitute(vec
->el
, pos
, aff
->ls
->div
->row
[i
],
1366 isl_vec_free(aff
->v
);
1368 isl_local_space_free(aff
->ls
);
1375 isl_local_space_free(ls
);
1376 return isl_aff_free(aff
);
1379 /* Look for any divs j that appear with a unit coefficient inside
1380 * the definitions of other divs i and plug them into the definitions
1383 * In particular, an expression of the form
1385 * floor((f(..) + floor(g(..)/n))/m)
1389 * floor((n * f(..) + g(..))/(n * m))
1391 * This simplification is correct because we can move the expression
1392 * f(..) into the inner floor in the original expression to obtain
1394 * floor(floor((n * f(..) + g(..))/n)/m)
1396 * from which we can derive the simplified expression.
1398 static __isl_give isl_aff
*plug_in_unit_divs(__isl_take isl_aff
*aff
)
1407 n
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
1409 return isl_aff_free(aff
);
1410 off
= isl_local_space_offset(aff
->ls
, isl_dim_div
);
1411 for (i
= 1; i
< n
; ++i
) {
1412 for (j
= 0; j
< i
; ++j
) {
1413 if (!isl_int_is_one(aff
->ls
->div
->row
[i
][1 + off
+ j
]))
1415 aff
->ls
= isl_local_space_substitute_seq(aff
->ls
,
1416 isl_dim_div
, j
, aff
->ls
->div
->row
[j
],
1417 aff
->v
->size
, i
, 1);
1419 return isl_aff_free(aff
);
1426 /* Swap divs "a" and "b" in "aff", which is assumed to be non-NULL.
1428 * Even though this function is only called on isl_affs with a single
1429 * reference, we are careful to only change aff->v and aff->ls together.
1431 static __isl_give isl_aff
*swap_div(__isl_take isl_aff
*aff
, int a
, int b
)
1433 unsigned off
= isl_local_space_offset(aff
->ls
, isl_dim_div
);
1434 isl_local_space
*ls
;
1437 ls
= isl_local_space_copy(aff
->ls
);
1438 ls
= isl_local_space_swap_div(ls
, a
, b
);
1439 v
= isl_vec_copy(aff
->v
);
1444 isl_int_swap(v
->el
[1 + off
+ a
], v
->el
[1 + off
+ b
]);
1445 isl_vec_free(aff
->v
);
1447 isl_local_space_free(aff
->ls
);
1453 isl_local_space_free(ls
);
1454 return isl_aff_free(aff
);
1457 /* Merge divs "a" and "b" in "aff", which is assumed to be non-NULL.
1459 * We currently do not actually remove div "b", but simply add its
1460 * coefficient to that of "a" and then zero it out.
1462 static __isl_give isl_aff
*merge_divs(__isl_take isl_aff
*aff
, int a
, int b
)
1464 unsigned off
= isl_local_space_offset(aff
->ls
, isl_dim_div
);
1466 if (isl_int_is_zero(aff
->v
->el
[1 + off
+ b
]))
1469 aff
->v
= isl_vec_cow(aff
->v
);
1471 return isl_aff_free(aff
);
1473 isl_int_add(aff
->v
->el
[1 + off
+ a
],
1474 aff
->v
->el
[1 + off
+ a
], aff
->v
->el
[1 + off
+ b
]);
1475 isl_int_set_si(aff
->v
->el
[1 + off
+ b
], 0);
1480 /* Sort the divs in the local space of "aff" according to
1481 * the comparison function "cmp_row" in isl_local_space.c,
1482 * combining the coefficients of identical divs.
1484 * Reordering divs does not change the semantics of "aff",
1485 * so there is no need to call isl_aff_cow.
1486 * Moreover, this function is currently only called on isl_affs
1487 * with a single reference.
1489 static __isl_give isl_aff
*sort_divs(__isl_take isl_aff
*aff
)
1494 n
= isl_aff_dim(aff
, isl_dim_div
);
1496 return isl_aff_free(aff
);
1497 for (i
= 1; i
< n
; ++i
) {
1498 for (j
= i
- 1; j
>= 0; --j
) {
1499 int cmp
= isl_mat_cmp_div(aff
->ls
->div
, j
, j
+ 1);
1503 aff
= merge_divs(aff
, j
, j
+ 1);
1505 aff
= swap_div(aff
, j
, j
+ 1);
1514 /* Normalize the representation of "aff".
1516 * This function should only be called of "new" isl_affs, i.e.,
1517 * with only a single reference. We therefore do not need to
1518 * worry about affecting other instances.
1520 __isl_give isl_aff
*isl_aff_normalize(__isl_take isl_aff
*aff
)
1524 aff
->v
= isl_vec_normalize(aff
->v
);
1526 return isl_aff_free(aff
);
1527 aff
= plug_in_integral_divs(aff
);
1528 aff
= plug_in_unit_divs(aff
);
1529 aff
= sort_divs(aff
);
1530 aff
= isl_aff_remove_unused_divs(aff
);
1534 /* Given f, return floor(f).
1535 * If f is an integer expression, then just return f.
1536 * If f is a constant, then return the constant floor(f).
1537 * Otherwise, if f = g/m, write g = q m + r,
1538 * create a new div d = [r/m] and return the expression q + d.
1539 * The coefficients in r are taken to lie between -m/2 and m/2.
1541 * reduce_div_coefficients performs the same normalization.
1543 * As a special case, floor(NaN) = NaN.
1545 __isl_give isl_aff
*isl_aff_floor(__isl_take isl_aff
*aff
)
1555 if (isl_aff_is_nan(aff
))
1557 if (isl_int_is_one(aff
->v
->el
[0]))
1560 aff
= isl_aff_cow(aff
);
1564 aff
->v
= isl_vec_cow(aff
->v
);
1566 return isl_aff_free(aff
);
1568 if (isl_aff_is_cst(aff
)) {
1569 isl_int_fdiv_q(aff
->v
->el
[1], aff
->v
->el
[1], aff
->v
->el
[0]);
1570 isl_int_set_si(aff
->v
->el
[0], 1);
1574 div
= isl_vec_copy(aff
->v
);
1575 div
= isl_vec_cow(div
);
1577 return isl_aff_free(aff
);
1579 ctx
= isl_aff_get_ctx(aff
);
1580 isl_int_fdiv_q(aff
->v
->el
[0], aff
->v
->el
[0], ctx
->two
);
1581 for (i
= 1; i
< aff
->v
->size
; ++i
) {
1582 isl_int_fdiv_r(div
->el
[i
], div
->el
[i
], div
->el
[0]);
1583 isl_int_fdiv_q(aff
->v
->el
[i
], aff
->v
->el
[i
], div
->el
[0]);
1584 if (isl_int_gt(div
->el
[i
], aff
->v
->el
[0])) {
1585 isl_int_sub(div
->el
[i
], div
->el
[i
], div
->el
[0]);
1586 isl_int_add_ui(aff
->v
->el
[i
], aff
->v
->el
[i
], 1);
1590 aff
->ls
= isl_local_space_add_div(aff
->ls
, div
);
1592 return isl_aff_free(aff
);
1594 size
= aff
->v
->size
;
1595 aff
->v
= isl_vec_extend(aff
->v
, size
+ 1);
1597 return isl_aff_free(aff
);
1598 isl_int_set_si(aff
->v
->el
[0], 1);
1599 isl_int_set_si(aff
->v
->el
[size
], 1);
1601 aff
= isl_aff_normalize(aff
);
1608 * aff mod m = aff - m * floor(aff/m)
1610 * with m an integer value.
1612 __isl_give isl_aff
*isl_aff_mod_val(__isl_take isl_aff
*aff
,
1613 __isl_take isl_val
*m
)
1620 if (!isl_val_is_int(m
))
1621 isl_die(isl_val_get_ctx(m
), isl_error_invalid
,
1622 "expecting integer modulo", goto error
);
1624 res
= isl_aff_copy(aff
);
1625 aff
= isl_aff_scale_down_val(aff
, isl_val_copy(m
));
1626 aff
= isl_aff_floor(aff
);
1627 aff
= isl_aff_scale_val(aff
, m
);
1628 res
= isl_aff_sub(res
, aff
);
1639 * pwaff mod m = pwaff - m * floor(pwaff/m)
1641 __isl_give isl_pw_aff
*isl_pw_aff_mod(__isl_take isl_pw_aff
*pwaff
, isl_int m
)
1645 res
= isl_pw_aff_copy(pwaff
);
1646 pwaff
= isl_pw_aff_scale_down(pwaff
, m
);
1647 pwaff
= isl_pw_aff_floor(pwaff
);
1648 pwaff
= isl_pw_aff_scale(pwaff
, m
);
1649 res
= isl_pw_aff_sub(res
, pwaff
);
1656 * pa mod m = pa - m * floor(pa/m)
1658 * with m an integer value.
1660 __isl_give isl_pw_aff
*isl_pw_aff_mod_val(__isl_take isl_pw_aff
*pa
,
1661 __isl_take isl_val
*m
)
1665 if (!isl_val_is_int(m
))
1666 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
1667 "expecting integer modulo", goto error
);
1668 pa
= isl_pw_aff_mod(pa
, m
->n
);
1672 isl_pw_aff_free(pa
);
1677 /* Given f, return ceil(f).
1678 * If f is an integer expression, then just return f.
1679 * Otherwise, let f be the expression
1685 * floor((e + m - 1)/m)
1687 * As a special case, ceil(NaN) = NaN.
1689 __isl_give isl_aff
*isl_aff_ceil(__isl_take isl_aff
*aff
)
1694 if (isl_aff_is_nan(aff
))
1696 if (isl_int_is_one(aff
->v
->el
[0]))
1699 aff
= isl_aff_cow(aff
);
1702 aff
->v
= isl_vec_cow(aff
->v
);
1704 return isl_aff_free(aff
);
1706 isl_int_add(aff
->v
->el
[1], aff
->v
->el
[1], aff
->v
->el
[0]);
1707 isl_int_sub_ui(aff
->v
->el
[1], aff
->v
->el
[1], 1);
1708 aff
= isl_aff_floor(aff
);
1713 /* Apply the expansion computed by isl_merge_divs.
1714 * The expansion itself is given by "exp" while the resulting
1715 * list of divs is given by "div".
1717 __isl_give isl_aff
*isl_aff_expand_divs(__isl_take isl_aff
*aff
,
1718 __isl_take isl_mat
*div
, int *exp
)
1724 aff
= isl_aff_cow(aff
);
1728 old_n_div
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
1729 new_n_div
= isl_mat_rows(div
);
1730 if (old_n_div
< 0 || new_n_div
< 0)
1732 offset
= 1 + isl_local_space_offset(aff
->ls
, isl_dim_div
);
1734 aff
->v
= isl_vec_expand(aff
->v
, offset
, old_n_div
, exp
, new_n_div
);
1735 aff
->ls
= isl_local_space_replace_divs(aff
->ls
, div
);
1736 if (!aff
->v
|| !aff
->ls
)
1737 return isl_aff_free(aff
);
1745 /* Add two affine expressions that live in the same local space.
1747 static __isl_give isl_aff
*add_expanded(__isl_take isl_aff
*aff1
,
1748 __isl_take isl_aff
*aff2
)
1752 aff1
= isl_aff_cow(aff1
);
1756 aff1
->v
= isl_vec_cow(aff1
->v
);
1762 isl_int_gcd(gcd
, aff1
->v
->el
[0], aff2
->v
->el
[0]);
1763 isl_int_divexact(f
, aff2
->v
->el
[0], gcd
);
1764 isl_seq_scale(aff1
->v
->el
+ 1, aff1
->v
->el
+ 1, f
, aff1
->v
->size
- 1);
1765 isl_int_divexact(f
, aff1
->v
->el
[0], gcd
);
1766 isl_seq_addmul(aff1
->v
->el
+ 1, f
, aff2
->v
->el
+ 1, aff1
->v
->size
- 1);
1767 isl_int_divexact(f
, aff2
->v
->el
[0], gcd
);
1768 isl_int_mul(aff1
->v
->el
[0], aff1
->v
->el
[0], f
);
1780 /* Return the sum of "aff1" and "aff2".
1782 * If either of the two is NaN, then the result is NaN.
1784 __isl_give isl_aff
*isl_aff_add(__isl_take isl_aff
*aff1
,
1785 __isl_take isl_aff
*aff2
)
1791 isl_size n_div1
, n_div2
;
1796 ctx
= isl_aff_get_ctx(aff1
);
1797 if (!isl_space_is_equal(aff1
->ls
->dim
, aff2
->ls
->dim
))
1798 isl_die(ctx
, isl_error_invalid
,
1799 "spaces don't match", goto error
);
1801 if (isl_aff_is_nan(aff1
)) {
1805 if (isl_aff_is_nan(aff2
)) {
1810 n_div1
= isl_aff_dim(aff1
, isl_dim_div
);
1811 n_div2
= isl_aff_dim(aff2
, isl_dim_div
);
1812 if (n_div1
< 0 || n_div2
< 0)
1814 if (n_div1
== 0 && n_div2
== 0)
1815 return add_expanded(aff1
, aff2
);
1817 exp1
= isl_alloc_array(ctx
, int, n_div1
);
1818 exp2
= isl_alloc_array(ctx
, int, n_div2
);
1819 if ((n_div1
&& !exp1
) || (n_div2
&& !exp2
))
1822 div
= isl_merge_divs(aff1
->ls
->div
, aff2
->ls
->div
, exp1
, exp2
);
1823 aff1
= isl_aff_expand_divs(aff1
, isl_mat_copy(div
), exp1
);
1824 aff2
= isl_aff_expand_divs(aff2
, div
, exp2
);
1828 return add_expanded(aff1
, aff2
);
1837 __isl_give isl_aff
*isl_aff_sub(__isl_take isl_aff
*aff1
,
1838 __isl_take isl_aff
*aff2
)
1840 return isl_aff_add(aff1
, isl_aff_neg(aff2
));
1843 /* Return the result of scaling "aff" by a factor of "f".
1845 * As a special case, f * NaN = NaN.
1847 __isl_give isl_aff
*isl_aff_scale(__isl_take isl_aff
*aff
, isl_int f
)
1853 if (isl_aff_is_nan(aff
))
1856 if (isl_int_is_one(f
))
1859 aff
= isl_aff_cow(aff
);
1862 aff
->v
= isl_vec_cow(aff
->v
);
1864 return isl_aff_free(aff
);
1866 if (isl_int_is_pos(f
) && isl_int_is_divisible_by(aff
->v
->el
[0], f
)) {
1867 isl_int_divexact(aff
->v
->el
[0], aff
->v
->el
[0], f
);
1872 isl_int_gcd(gcd
, aff
->v
->el
[0], f
);
1873 isl_int_divexact(aff
->v
->el
[0], aff
->v
->el
[0], gcd
);
1874 isl_int_divexact(gcd
, f
, gcd
);
1875 isl_seq_scale(aff
->v
->el
+ 1, aff
->v
->el
+ 1, gcd
, aff
->v
->size
- 1);
1881 /* Multiple "aff" by "v".
1883 __isl_give isl_aff
*isl_aff_scale_val(__isl_take isl_aff
*aff
,
1884 __isl_take isl_val
*v
)
1889 if (isl_val_is_one(v
)) {
1894 if (!isl_val_is_rat(v
))
1895 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1896 "expecting rational factor", goto error
);
1898 aff
= isl_aff_scale(aff
, v
->n
);
1899 aff
= isl_aff_scale_down(aff
, v
->d
);
1909 /* Return the result of scaling "aff" down by a factor of "f".
1911 * As a special case, NaN/f = NaN.
1913 __isl_give isl_aff
*isl_aff_scale_down(__isl_take isl_aff
*aff
, isl_int f
)
1919 if (isl_aff_is_nan(aff
))
1922 if (isl_int_is_one(f
))
1925 aff
= isl_aff_cow(aff
);
1929 if (isl_int_is_zero(f
))
1930 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1931 "cannot scale down by zero", return isl_aff_free(aff
));
1933 aff
->v
= isl_vec_cow(aff
->v
);
1935 return isl_aff_free(aff
);
1938 isl_seq_gcd(aff
->v
->el
+ 1, aff
->v
->size
- 1, &gcd
);
1939 isl_int_gcd(gcd
, gcd
, f
);
1940 isl_seq_scale_down(aff
->v
->el
+ 1, aff
->v
->el
+ 1, gcd
, aff
->v
->size
- 1);
1941 isl_int_divexact(gcd
, f
, gcd
);
1942 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], gcd
);
1948 /* Divide "aff" by "v".
1950 __isl_give isl_aff
*isl_aff_scale_down_val(__isl_take isl_aff
*aff
,
1951 __isl_take isl_val
*v
)
1956 if (isl_val_is_one(v
)) {
1961 if (!isl_val_is_rat(v
))
1962 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1963 "expecting rational factor", goto error
);
1964 if (!isl_val_is_pos(v
))
1965 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1966 "factor needs to be positive", goto error
);
1968 aff
= isl_aff_scale(aff
, v
->d
);
1969 aff
= isl_aff_scale_down(aff
, v
->n
);
1979 __isl_give isl_aff
*isl_aff_scale_down_ui(__isl_take isl_aff
*aff
, unsigned f
)
1987 isl_int_set_ui(v
, f
);
1988 aff
= isl_aff_scale_down(aff
, v
);
1994 __isl_give isl_aff
*isl_aff_set_dim_name(__isl_take isl_aff
*aff
,
1995 enum isl_dim_type type
, unsigned pos
, const char *s
)
1997 aff
= isl_aff_cow(aff
);
2000 if (type
== isl_dim_out
)
2001 isl_die(aff
->v
->ctx
, isl_error_invalid
,
2002 "cannot set name of output/set dimension",
2003 return isl_aff_free(aff
));
2004 if (type
== isl_dim_in
)
2006 aff
->ls
= isl_local_space_set_dim_name(aff
->ls
, type
, pos
, s
);
2008 return isl_aff_free(aff
);
2013 __isl_give isl_aff
*isl_aff_set_dim_id(__isl_take isl_aff
*aff
,
2014 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
2016 aff
= isl_aff_cow(aff
);
2019 if (type
== isl_dim_out
)
2020 isl_die(aff
->v
->ctx
, isl_error_invalid
,
2021 "cannot set name of output/set dimension",
2023 if (type
== isl_dim_in
)
2025 aff
->ls
= isl_local_space_set_dim_id(aff
->ls
, type
, pos
, id
);
2027 return isl_aff_free(aff
);
2036 /* Replace the identifier of the input tuple of "aff" by "id".
2037 * type is currently required to be equal to isl_dim_in
2039 __isl_give isl_aff
*isl_aff_set_tuple_id(__isl_take isl_aff
*aff
,
2040 enum isl_dim_type type
, __isl_take isl_id
*id
)
2042 aff
= isl_aff_cow(aff
);
2045 if (type
!= isl_dim_in
)
2046 isl_die(aff
->v
->ctx
, isl_error_invalid
,
2047 "cannot only set id of input tuple", goto error
);
2048 aff
->ls
= isl_local_space_set_tuple_id(aff
->ls
, isl_dim_set
, id
);
2050 return isl_aff_free(aff
);
2059 /* Exploit the equalities in "eq" to simplify the affine expression
2060 * and the expressions of the integer divisions in the local space.
2061 * The integer divisions in this local space are assumed to appear
2062 * as regular dimensions in "eq".
2064 static __isl_give isl_aff
*isl_aff_substitute_equalities_lifted(
2065 __isl_take isl_aff
*aff
, __isl_take isl_basic_set
*eq
)
2073 if (eq
->n_eq
== 0) {
2074 isl_basic_set_free(eq
);
2078 aff
= isl_aff_cow(aff
);
2082 aff
->ls
= isl_local_space_substitute_equalities(aff
->ls
,
2083 isl_basic_set_copy(eq
));
2084 aff
->v
= isl_vec_cow(aff
->v
);
2085 if (!aff
->ls
|| !aff
->v
)
2088 o_div
= isl_basic_set_offset(eq
, isl_dim_div
);
2090 for (i
= 0; i
< eq
->n_eq
; ++i
) {
2091 j
= isl_seq_last_non_zero(eq
->eq
[i
], o_div
+ n_div
);
2092 if (j
< 0 || j
== 0 || j
>= o_div
)
2095 isl_seq_elim(aff
->v
->el
+ 1, eq
->eq
[i
], j
, o_div
,
2099 isl_basic_set_free(eq
);
2100 aff
= isl_aff_normalize(aff
);
2103 isl_basic_set_free(eq
);
2108 /* Exploit the equalities in "eq" to simplify the affine expression
2109 * and the expressions of the integer divisions in the local space.
2111 __isl_give isl_aff
*isl_aff_substitute_equalities(__isl_take isl_aff
*aff
,
2112 __isl_take isl_basic_set
*eq
)
2118 n_div
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
2122 eq
= isl_basic_set_add_dims(eq
, isl_dim_set
, n_div
);
2123 return isl_aff_substitute_equalities_lifted(aff
, eq
);
2125 isl_basic_set_free(eq
);
2130 /* Look for equalities among the variables shared by context and aff
2131 * and the integer divisions of aff, if any.
2132 * The equalities are then used to eliminate coefficients and/or integer
2133 * divisions from aff.
2135 __isl_give isl_aff
*isl_aff_gist(__isl_take isl_aff
*aff
,
2136 __isl_take isl_set
*context
)
2138 isl_local_space
*ls
;
2139 isl_basic_set
*hull
;
2141 ls
= isl_aff_get_domain_local_space(aff
);
2142 context
= isl_local_space_lift_set(ls
, context
);
2144 hull
= isl_set_affine_hull(context
);
2145 return isl_aff_substitute_equalities_lifted(aff
, hull
);
2148 __isl_give isl_aff
*isl_aff_gist_params(__isl_take isl_aff
*aff
,
2149 __isl_take isl_set
*context
)
2151 isl_set
*dom_context
= isl_set_universe(isl_aff_get_domain_space(aff
));
2152 dom_context
= isl_set_intersect_params(dom_context
, context
);
2153 return isl_aff_gist(aff
, dom_context
);
2156 /* Return a basic set containing those elements in the space
2157 * of aff where it is positive. "rational" should not be set.
2159 * If "aff" is NaN, then it is not positive.
2161 static __isl_give isl_basic_set
*aff_pos_basic_set(__isl_take isl_aff
*aff
,
2162 int rational
, void *user
)
2164 isl_constraint
*ineq
;
2165 isl_basic_set
*bset
;
2170 if (isl_aff_is_nan(aff
)) {
2171 isl_space
*space
= isl_aff_get_domain_space(aff
);
2173 return isl_basic_set_empty(space
);
2176 isl_die(isl_aff_get_ctx(aff
), isl_error_unsupported
,
2177 "rational sets not supported", goto error
);
2179 ineq
= isl_inequality_from_aff(aff
);
2180 c
= isl_constraint_get_constant_val(ineq
);
2181 c
= isl_val_sub_ui(c
, 1);
2182 ineq
= isl_constraint_set_constant_val(ineq
, c
);
2184 bset
= isl_basic_set_from_constraint(ineq
);
2185 bset
= isl_basic_set_simplify(bset
);
2192 /* Return a basic set containing those elements in the space
2193 * of aff where it is non-negative.
2194 * If "rational" is set, then return a rational basic set.
2196 * If "aff" is NaN, then it is not non-negative (it's not negative either).
2198 static __isl_give isl_basic_set
*aff_nonneg_basic_set(
2199 __isl_take isl_aff
*aff
, int rational
, void *user
)
2201 isl_constraint
*ineq
;
2202 isl_basic_set
*bset
;
2206 if (isl_aff_is_nan(aff
)) {
2207 isl_space
*space
= isl_aff_get_domain_space(aff
);
2209 return isl_basic_set_empty(space
);
2212 ineq
= isl_inequality_from_aff(aff
);
2214 bset
= isl_basic_set_from_constraint(ineq
);
2216 bset
= isl_basic_set_set_rational(bset
);
2217 bset
= isl_basic_set_simplify(bset
);
2221 /* Return a basic set containing those elements in the space
2222 * of aff where it is non-negative.
2224 __isl_give isl_basic_set
*isl_aff_nonneg_basic_set(__isl_take isl_aff
*aff
)
2226 return aff_nonneg_basic_set(aff
, 0, NULL
);
2229 /* Return a basic set containing those elements in the domain space
2230 * of "aff" where it is positive.
2232 __isl_give isl_basic_set
*isl_aff_pos_basic_set(__isl_take isl_aff
*aff
)
2234 aff
= isl_aff_add_constant_num_si(aff
, -1);
2235 return isl_aff_nonneg_basic_set(aff
);
2238 /* Return a basic set containing those elements in the domain space
2239 * of aff where it is negative.
2241 __isl_give isl_basic_set
*isl_aff_neg_basic_set(__isl_take isl_aff
*aff
)
2243 aff
= isl_aff_neg(aff
);
2244 return isl_aff_pos_basic_set(aff
);
2247 /* Return a basic set containing those elements in the space
2248 * of aff where it is zero.
2249 * If "rational" is set, then return a rational basic set.
2251 * If "aff" is NaN, then it is not zero.
2253 static __isl_give isl_basic_set
*aff_zero_basic_set(__isl_take isl_aff
*aff
,
2254 int rational
, void *user
)
2256 isl_constraint
*ineq
;
2257 isl_basic_set
*bset
;
2261 if (isl_aff_is_nan(aff
)) {
2262 isl_space
*space
= isl_aff_get_domain_space(aff
);
2264 return isl_basic_set_empty(space
);
2267 ineq
= isl_equality_from_aff(aff
);
2269 bset
= isl_basic_set_from_constraint(ineq
);
2271 bset
= isl_basic_set_set_rational(bset
);
2272 bset
= isl_basic_set_simplify(bset
);
2276 /* Return a basic set containing those elements in the space
2277 * of aff where it is zero.
2279 __isl_give isl_basic_set
*isl_aff_zero_basic_set(__isl_take isl_aff
*aff
)
2281 return aff_zero_basic_set(aff
, 0, NULL
);
2284 /* Return a basic set containing those elements in the shared space
2285 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
2287 __isl_give isl_basic_set
*isl_aff_ge_basic_set(__isl_take isl_aff
*aff1
,
2288 __isl_take isl_aff
*aff2
)
2290 aff1
= isl_aff_sub(aff1
, aff2
);
2292 return isl_aff_nonneg_basic_set(aff1
);
2295 /* Return a basic set containing those elements in the shared domain space
2296 * of "aff1" and "aff2" where "aff1" is greater than "aff2".
2298 __isl_give isl_basic_set
*isl_aff_gt_basic_set(__isl_take isl_aff
*aff1
,
2299 __isl_take isl_aff
*aff2
)
2301 aff1
= isl_aff_sub(aff1
, aff2
);
2303 return isl_aff_pos_basic_set(aff1
);
2306 /* Return a set containing those elements in the shared space
2307 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
2309 __isl_give isl_set
*isl_aff_ge_set(__isl_take isl_aff
*aff1
,
2310 __isl_take isl_aff
*aff2
)
2312 return isl_set_from_basic_set(isl_aff_ge_basic_set(aff1
, aff2
));
2315 /* Return a set containing those elements in the shared domain space
2316 * of aff1 and aff2 where aff1 is greater than aff2.
2318 * If either of the two inputs is NaN, then the result is empty,
2319 * as comparisons with NaN always return false.
2321 __isl_give isl_set
*isl_aff_gt_set(__isl_take isl_aff
*aff1
,
2322 __isl_take isl_aff
*aff2
)
2324 return isl_set_from_basic_set(isl_aff_gt_basic_set(aff1
, aff2
));
2327 /* Return a basic set containing those elements in the shared space
2328 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
2330 __isl_give isl_basic_set
*isl_aff_le_basic_set(__isl_take isl_aff
*aff1
,
2331 __isl_take isl_aff
*aff2
)
2333 return isl_aff_ge_basic_set(aff2
, aff1
);
2336 /* Return a basic set containing those elements in the shared domain space
2337 * of "aff1" and "aff2" where "aff1" is smaller than "aff2".
2339 __isl_give isl_basic_set
*isl_aff_lt_basic_set(__isl_take isl_aff
*aff1
,
2340 __isl_take isl_aff
*aff2
)
2342 return isl_aff_gt_basic_set(aff2
, aff1
);
2345 /* Return a set containing those elements in the shared space
2346 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
2348 __isl_give isl_set
*isl_aff_le_set(__isl_take isl_aff
*aff1
,
2349 __isl_take isl_aff
*aff2
)
2351 return isl_aff_ge_set(aff2
, aff1
);
2354 /* Return a set containing those elements in the shared domain space
2355 * of "aff1" and "aff2" where "aff1" is smaller than "aff2".
2357 __isl_give isl_set
*isl_aff_lt_set(__isl_take isl_aff
*aff1
,
2358 __isl_take isl_aff
*aff2
)
2360 return isl_set_from_basic_set(isl_aff_lt_basic_set(aff1
, aff2
));
2363 /* Return a basic set containing those elements in the shared space
2364 * of aff1 and aff2 where aff1 and aff2 are equal.
2366 __isl_give isl_basic_set
*isl_aff_eq_basic_set(__isl_take isl_aff
*aff1
,
2367 __isl_take isl_aff
*aff2
)
2369 aff1
= isl_aff_sub(aff1
, aff2
);
2371 return isl_aff_zero_basic_set(aff1
);
2374 /* Return a set containing those elements in the shared space
2375 * of aff1 and aff2 where aff1 and aff2 are equal.
2377 __isl_give isl_set
*isl_aff_eq_set(__isl_take isl_aff
*aff1
,
2378 __isl_take isl_aff
*aff2
)
2380 return isl_set_from_basic_set(isl_aff_eq_basic_set(aff1
, aff2
));
2383 /* Return a set containing those elements in the shared domain space
2384 * of aff1 and aff2 where aff1 and aff2 are not equal.
2386 * If either of the two inputs is NaN, then the result is empty,
2387 * as comparisons with NaN always return false.
2389 __isl_give isl_set
*isl_aff_ne_set(__isl_take isl_aff
*aff1
,
2390 __isl_take isl_aff
*aff2
)
2392 isl_set
*set_lt
, *set_gt
;
2394 set_lt
= isl_aff_lt_set(isl_aff_copy(aff1
),
2395 isl_aff_copy(aff2
));
2396 set_gt
= isl_aff_gt_set(aff1
, aff2
);
2397 return isl_set_union_disjoint(set_lt
, set_gt
);
2400 __isl_give isl_aff
*isl_aff_add_on_domain(__isl_keep isl_set
*dom
,
2401 __isl_take isl_aff
*aff1
, __isl_take isl_aff
*aff2
)
2403 aff1
= isl_aff_add(aff1
, aff2
);
2404 aff1
= isl_aff_gist(aff1
, isl_set_copy(dom
));
2408 int isl_aff_is_empty(__isl_keep isl_aff
*aff
)
2417 #define TYPE isl_aff
2419 #include "check_type_range_templ.c"
2421 /* Check whether the given affine expression has non-zero coefficient
2422 * for any dimension in the given range or if any of these dimensions
2423 * appear with non-zero coefficients in any of the integer divisions
2424 * involved in the affine expression.
2426 isl_bool
isl_aff_involves_dims(__isl_keep isl_aff
*aff
,
2427 enum isl_dim_type type
, unsigned first
, unsigned n
)
2431 isl_bool involves
= isl_bool_false
;
2434 return isl_bool_error
;
2436 return isl_bool_false
;
2437 if (isl_aff_check_range(aff
, type
, first
, n
) < 0)
2438 return isl_bool_error
;
2440 active
= isl_local_space_get_active(aff
->ls
, aff
->v
->el
+ 2);
2444 first
+= isl_local_space_offset(aff
->ls
, type
) - 1;
2445 for (i
= 0; i
< n
; ++i
)
2446 if (active
[first
+ i
]) {
2447 involves
= isl_bool_true
;
2456 return isl_bool_error
;
2459 __isl_give isl_aff
*isl_aff_drop_dims(__isl_take isl_aff
*aff
,
2460 enum isl_dim_type type
, unsigned first
, unsigned n
)
2466 if (type
== isl_dim_out
)
2467 isl_die(aff
->v
->ctx
, isl_error_invalid
,
2468 "cannot drop output/set dimension",
2469 return isl_aff_free(aff
));
2470 if (type
== isl_dim_in
)
2472 if (n
== 0 && !isl_local_space_is_named_or_nested(aff
->ls
, type
))
2475 ctx
= isl_aff_get_ctx(aff
);
2476 if (isl_local_space_check_range(aff
->ls
, type
, first
, n
) < 0)
2477 return isl_aff_free(aff
);
2479 aff
= isl_aff_cow(aff
);
2483 aff
->ls
= isl_local_space_drop_dims(aff
->ls
, type
, first
, n
);
2485 return isl_aff_free(aff
);
2487 first
+= 1 + isl_local_space_offset(aff
->ls
, type
);
2488 aff
->v
= isl_vec_drop_els(aff
->v
, first
, n
);
2490 return isl_aff_free(aff
);
2495 /* Drop the "n" domain dimensions starting at "first" from "aff",
2496 * after checking that they do not appear in the affine expression.
2498 static __isl_give isl_aff
*drop_domain(__isl_take isl_aff
*aff
, unsigned first
,
2503 involves
= isl_aff_involves_dims(aff
, isl_dim_in
, first
, n
);
2505 return isl_aff_free(aff
);
2507 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2508 "affine expression involves some of the domain dimensions",
2509 return isl_aff_free(aff
));
2510 return isl_aff_drop_dims(aff
, isl_dim_in
, first
, n
);
2513 /* Project the domain of the affine expression onto its parameter space.
2514 * The affine expression may not involve any of the domain dimensions.
2516 __isl_give isl_aff
*isl_aff_project_domain_on_params(__isl_take isl_aff
*aff
)
2521 n
= isl_aff_dim(aff
, isl_dim_in
);
2523 return isl_aff_free(aff
);
2524 aff
= drop_domain(aff
, 0, n
);
2525 space
= isl_aff_get_domain_space(aff
);
2526 space
= isl_space_params(space
);
2527 aff
= isl_aff_reset_domain_space(aff
, space
);
2531 /* Check that the domain of "aff" is a product.
2533 static isl_stat
check_domain_product(__isl_keep isl_aff
*aff
)
2535 isl_bool is_product
;
2537 is_product
= isl_space_is_product(isl_aff_peek_domain_space(aff
));
2539 return isl_stat_error
;
2541 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2542 "domain is not a product", return isl_stat_error
);
2546 /* Given an affine function with a domain of the form [A -> B] that
2547 * does not depend on B, return the same function on domain A.
2549 __isl_give isl_aff
*isl_aff_domain_factor_domain(__isl_take isl_aff
*aff
)
2554 if (check_domain_product(aff
) < 0)
2555 return isl_aff_free(aff
);
2556 space
= isl_aff_get_domain_space(aff
);
2557 n
= isl_space_dim(space
, isl_dim_set
);
2558 space
= isl_space_factor_domain(space
);
2559 n_in
= isl_space_dim(space
, isl_dim_set
);
2560 if (n
< 0 || n_in
< 0)
2561 aff
= isl_aff_free(aff
);
2563 aff
= drop_domain(aff
, n_in
, n
- n_in
);
2564 aff
= isl_aff_reset_domain_space(aff
, space
);
2568 /* Convert an affine expression defined over a parameter domain
2569 * into one that is defined over a zero-dimensional set.
2571 __isl_give isl_aff
*isl_aff_from_range(__isl_take isl_aff
*aff
)
2573 isl_local_space
*ls
;
2575 ls
= isl_aff_take_domain_local_space(aff
);
2576 ls
= isl_local_space_set_from_params(ls
);
2577 aff
= isl_aff_restore_domain_local_space(aff
, ls
);
2582 __isl_give isl_aff
*isl_aff_insert_dims(__isl_take isl_aff
*aff
,
2583 enum isl_dim_type type
, unsigned first
, unsigned n
)
2589 if (type
== isl_dim_out
)
2590 isl_die(aff
->v
->ctx
, isl_error_invalid
,
2591 "cannot insert output/set dimensions",
2592 return isl_aff_free(aff
));
2593 if (type
== isl_dim_in
)
2595 if (n
== 0 && !isl_local_space_is_named_or_nested(aff
->ls
, type
))
2598 ctx
= isl_aff_get_ctx(aff
);
2599 if (isl_local_space_check_range(aff
->ls
, type
, first
, 0) < 0)
2600 return isl_aff_free(aff
);
2602 aff
= isl_aff_cow(aff
);
2606 aff
->ls
= isl_local_space_insert_dims(aff
->ls
, type
, first
, n
);
2608 return isl_aff_free(aff
);
2610 first
+= 1 + isl_local_space_offset(aff
->ls
, type
);
2611 aff
->v
= isl_vec_insert_zero_els(aff
->v
, first
, n
);
2613 return isl_aff_free(aff
);
2618 __isl_give isl_aff
*isl_aff_add_dims(__isl_take isl_aff
*aff
,
2619 enum isl_dim_type type
, unsigned n
)
2623 pos
= isl_aff_dim(aff
, type
);
2625 return isl_aff_free(aff
);
2627 return isl_aff_insert_dims(aff
, type
, pos
, n
);
2630 __isl_give isl_pw_aff
*isl_pw_aff_add_dims(__isl_take isl_pw_aff
*pwaff
,
2631 enum isl_dim_type type
, unsigned n
)
2635 pos
= isl_pw_aff_dim(pwaff
, type
);
2637 return isl_pw_aff_free(pwaff
);
2639 return isl_pw_aff_insert_dims(pwaff
, type
, pos
, n
);
2642 /* Move the "n" dimensions of "src_type" starting at "src_pos" of "aff"
2643 * to dimensions of "dst_type" at "dst_pos".
2645 * We only support moving input dimensions to parameters and vice versa.
2647 __isl_give isl_aff
*isl_aff_move_dims(__isl_take isl_aff
*aff
,
2648 enum isl_dim_type dst_type
, unsigned dst_pos
,
2649 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
2657 !isl_local_space_is_named_or_nested(aff
->ls
, src_type
) &&
2658 !isl_local_space_is_named_or_nested(aff
->ls
, dst_type
))
2661 if (dst_type
== isl_dim_out
|| src_type
== isl_dim_out
)
2662 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2663 "cannot move output/set dimension",
2664 return isl_aff_free(aff
));
2665 if (dst_type
== isl_dim_div
|| src_type
== isl_dim_div
)
2666 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2667 "cannot move divs", return isl_aff_free(aff
));
2668 if (dst_type
== isl_dim_in
)
2669 dst_type
= isl_dim_set
;
2670 if (src_type
== isl_dim_in
)
2671 src_type
= isl_dim_set
;
2673 if (isl_local_space_check_range(aff
->ls
, src_type
, src_pos
, n
) < 0)
2674 return isl_aff_free(aff
);
2675 if (dst_type
== src_type
)
2676 isl_die(isl_aff_get_ctx(aff
), isl_error_unsupported
,
2677 "moving dims within the same type not supported",
2678 return isl_aff_free(aff
));
2680 aff
= isl_aff_cow(aff
);
2684 g_src_pos
= 1 + isl_local_space_offset(aff
->ls
, src_type
) + src_pos
;
2685 g_dst_pos
= 1 + isl_local_space_offset(aff
->ls
, dst_type
) + dst_pos
;
2686 if (dst_type
> src_type
)
2689 aff
->v
= isl_vec_move_els(aff
->v
, g_dst_pos
, g_src_pos
, n
);
2690 aff
->ls
= isl_local_space_move_dims(aff
->ls
, dst_type
, dst_pos
,
2691 src_type
, src_pos
, n
);
2692 if (!aff
->v
|| !aff
->ls
)
2693 return isl_aff_free(aff
);
2695 aff
= sort_divs(aff
);
2700 __isl_give isl_pw_aff
*isl_pw_aff_from_aff(__isl_take isl_aff
*aff
)
2702 isl_set
*dom
= isl_set_universe(isl_aff_get_domain_space(aff
));
2703 return isl_pw_aff_alloc(dom
, aff
);
2706 #define isl_aff_involves_nan isl_aff_is_nan
2709 #define PW isl_pw_aff
2713 #define EL_IS_ZERO is_empty
2717 #define IS_ZERO is_empty
2720 #undef DEFAULT_IS_ZERO
2721 #define DEFAULT_IS_ZERO 0
2727 #include <isl_pw_templ.c>
2728 #include <isl_pw_eval.c>
2729 #include <isl_pw_hash.c>
2730 #include <isl_pw_union_opt.c>
2735 #include <isl_union_single.c>
2736 #include <isl_union_neg.c>
2738 static __isl_give isl_set
*align_params_pw_pw_set_and(
2739 __isl_take isl_pw_aff
*pwaff1
, __isl_take isl_pw_aff
*pwaff2
,
2740 __isl_give isl_set
*(*fn
)(__isl_take isl_pw_aff
*pwaff1
,
2741 __isl_take isl_pw_aff
*pwaff2
))
2743 isl_bool equal_params
;
2745 if (!pwaff1
|| !pwaff2
)
2747 equal_params
= isl_space_has_equal_params(pwaff1
->dim
, pwaff2
->dim
);
2748 if (equal_params
< 0)
2751 return fn(pwaff1
, pwaff2
);
2752 if (isl_pw_aff_check_named_params(pwaff1
) < 0 ||
2753 isl_pw_aff_check_named_params(pwaff2
) < 0)
2755 pwaff1
= isl_pw_aff_align_params(pwaff1
, isl_pw_aff_get_space(pwaff2
));
2756 pwaff2
= isl_pw_aff_align_params(pwaff2
, isl_pw_aff_get_space(pwaff1
));
2757 return fn(pwaff1
, pwaff2
);
2759 isl_pw_aff_free(pwaff1
);
2760 isl_pw_aff_free(pwaff2
);
2764 /* Align the parameters of the to isl_pw_aff arguments and
2765 * then apply a function "fn" on them that returns an isl_map.
2767 static __isl_give isl_map
*align_params_pw_pw_map_and(
2768 __isl_take isl_pw_aff
*pa1
, __isl_take isl_pw_aff
*pa2
,
2769 __isl_give isl_map
*(*fn
)(__isl_take isl_pw_aff
*pa1
,
2770 __isl_take isl_pw_aff
*pa2
))
2772 isl_bool equal_params
;
2776 equal_params
= isl_space_has_equal_params(pa1
->dim
, pa2
->dim
);
2777 if (equal_params
< 0)
2780 return fn(pa1
, pa2
);
2781 if (isl_pw_aff_check_named_params(pa1
) < 0 ||
2782 isl_pw_aff_check_named_params(pa2
) < 0)
2784 pa1
= isl_pw_aff_align_params(pa1
, isl_pw_aff_get_space(pa2
));
2785 pa2
= isl_pw_aff_align_params(pa2
, isl_pw_aff_get_space(pa1
));
2786 return fn(pa1
, pa2
);
2788 isl_pw_aff_free(pa1
);
2789 isl_pw_aff_free(pa2
);
2793 /* Compute a piecewise quasi-affine expression with a domain that
2794 * is the union of those of pwaff1 and pwaff2 and such that on each
2795 * cell, the quasi-affine expression is the maximum of those of pwaff1
2796 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2797 * cell, then the associated expression is the defined one.
2799 static __isl_give isl_pw_aff
*pw_aff_union_max(__isl_take isl_pw_aff
*pwaff1
,
2800 __isl_take isl_pw_aff
*pwaff2
)
2802 return isl_pw_aff_union_opt_cmp(pwaff1
, pwaff2
, &isl_aff_ge_set
);
2805 __isl_give isl_pw_aff
*isl_pw_aff_union_max(__isl_take isl_pw_aff
*pwaff1
,
2806 __isl_take isl_pw_aff
*pwaff2
)
2808 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
,
2812 /* Compute a piecewise quasi-affine expression with a domain that
2813 * is the union of those of pwaff1 and pwaff2 and such that on each
2814 * cell, the quasi-affine expression is the minimum of those of pwaff1
2815 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2816 * cell, then the associated expression is the defined one.
2818 static __isl_give isl_pw_aff
*pw_aff_union_min(__isl_take isl_pw_aff
*pwaff1
,
2819 __isl_take isl_pw_aff
*pwaff2
)
2821 return isl_pw_aff_union_opt_cmp(pwaff1
, pwaff2
, &isl_aff_le_set
);
2824 __isl_give isl_pw_aff
*isl_pw_aff_union_min(__isl_take isl_pw_aff
*pwaff1
,
2825 __isl_take isl_pw_aff
*pwaff2
)
2827 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
,
2831 __isl_give isl_pw_aff
*isl_pw_aff_union_opt(__isl_take isl_pw_aff
*pwaff1
,
2832 __isl_take isl_pw_aff
*pwaff2
, int max
)
2835 return isl_pw_aff_union_max(pwaff1
, pwaff2
);
2837 return isl_pw_aff_union_min(pwaff1
, pwaff2
);
2840 /* Return a set containing those elements in the domain
2841 * of "pwaff" where it satisfies "fn" (if complement is 0) or
2842 * does not satisfy "fn" (if complement is 1).
2844 * The pieces with a NaN never belong to the result since
2845 * NaN does not satisfy any property.
2847 static __isl_give isl_set
*pw_aff_locus(__isl_take isl_pw_aff
*pwaff
,
2848 __isl_give isl_basic_set
*(*fn
)(__isl_take isl_aff
*aff
, int rational
,
2850 int complement
, void *user
)
2858 set
= isl_set_empty(isl_pw_aff_get_domain_space(pwaff
));
2860 for (i
= 0; i
< pwaff
->n
; ++i
) {
2861 isl_basic_set
*bset
;
2862 isl_set
*set_i
, *locus
;
2865 if (isl_aff_is_nan(pwaff
->p
[i
].aff
))
2868 rational
= isl_set_has_rational(pwaff
->p
[i
].set
);
2869 bset
= fn(isl_aff_copy(pwaff
->p
[i
].aff
), rational
, user
);
2870 locus
= isl_set_from_basic_set(bset
);
2871 set_i
= isl_set_copy(pwaff
->p
[i
].set
);
2873 set_i
= isl_set_subtract(set_i
, locus
);
2875 set_i
= isl_set_intersect(set_i
, locus
);
2876 set
= isl_set_union_disjoint(set
, set_i
);
2879 isl_pw_aff_free(pwaff
);
2884 /* Return a set containing those elements in the domain
2885 * of "pa" where it is positive.
2887 __isl_give isl_set
*isl_pw_aff_pos_set(__isl_take isl_pw_aff
*pa
)
2889 return pw_aff_locus(pa
, &aff_pos_basic_set
, 0, NULL
);
2892 /* Return a set containing those elements in the domain
2893 * of pwaff where it is non-negative.
2895 __isl_give isl_set
*isl_pw_aff_nonneg_set(__isl_take isl_pw_aff
*pwaff
)
2897 return pw_aff_locus(pwaff
, &aff_nonneg_basic_set
, 0, NULL
);
2900 /* Return a set containing those elements in the domain
2901 * of pwaff where it is zero.
2903 __isl_give isl_set
*isl_pw_aff_zero_set(__isl_take isl_pw_aff
*pwaff
)
2905 return pw_aff_locus(pwaff
, &aff_zero_basic_set
, 0, NULL
);
2908 /* Return a set containing those elements in the domain
2909 * of pwaff where it is not zero.
2911 __isl_give isl_set
*isl_pw_aff_non_zero_set(__isl_take isl_pw_aff
*pwaff
)
2913 return pw_aff_locus(pwaff
, &aff_zero_basic_set
, 1, NULL
);
2916 /* Bind the affine function "aff" to the parameter "id",
2917 * returning the elements in the domain where the affine expression
2918 * is equal to the parameter.
2920 __isl_give isl_basic_set
*isl_aff_bind_id(__isl_take isl_aff
*aff
,
2921 __isl_take isl_id
*id
)
2926 space
= isl_aff_get_domain_space(aff
);
2927 space
= isl_space_add_param_id(space
, isl_id_copy(id
));
2929 aff
= isl_aff_align_params(aff
, isl_space_copy(space
));
2930 aff_id
= isl_aff_param_on_domain_space_id(space
, id
);
2932 return isl_aff_eq_basic_set(aff
, aff_id
);
2935 /* Wrapper around isl_aff_bind_id for use as pw_aff_locus callback.
2936 * "rational" should not be set.
2938 static __isl_give isl_basic_set
*aff_bind_id(__isl_take isl_aff
*aff
,
2939 int rational
, void *user
)
2946 isl_die(isl_aff_get_ctx(aff
), isl_error_unsupported
,
2947 "rational binding not supported", goto error
);
2948 return isl_aff_bind_id(aff
, isl_id_copy(id
));
2954 /* Bind the piecewise affine function "pa" to the parameter "id",
2955 * returning the elements in the domain where the expression
2956 * is equal to the parameter.
2958 __isl_give isl_set
*isl_pw_aff_bind_id(__isl_take isl_pw_aff
*pa
,
2959 __isl_take isl_id
*id
)
2963 bound
= pw_aff_locus(pa
, &aff_bind_id
, 0, id
);
2969 /* Return a set containing those elements in the shared domain
2970 * of pwaff1 and pwaff2 where pwaff1 is greater than (or equal) to pwaff2.
2972 * We compute the difference on the shared domain and then construct
2973 * the set of values where this difference is non-negative.
2974 * If strict is set, we first subtract 1 from the difference.
2975 * If equal is set, we only return the elements where pwaff1 and pwaff2
2978 static __isl_give isl_set
*pw_aff_gte_set(__isl_take isl_pw_aff
*pwaff1
,
2979 __isl_take isl_pw_aff
*pwaff2
, int strict
, int equal
)
2981 isl_set
*set1
, *set2
;
2983 set1
= isl_pw_aff_domain(isl_pw_aff_copy(pwaff1
));
2984 set2
= isl_pw_aff_domain(isl_pw_aff_copy(pwaff2
));
2985 set1
= isl_set_intersect(set1
, set2
);
2986 pwaff1
= isl_pw_aff_intersect_domain(pwaff1
, isl_set_copy(set1
));
2987 pwaff2
= isl_pw_aff_intersect_domain(pwaff2
, isl_set_copy(set1
));
2988 pwaff1
= isl_pw_aff_add(pwaff1
, isl_pw_aff_neg(pwaff2
));
2991 isl_space
*space
= isl_set_get_space(set1
);
2993 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(space
));
2994 aff
= isl_aff_add_constant_si(aff
, -1);
2995 pwaff1
= isl_pw_aff_add(pwaff1
, isl_pw_aff_alloc(set1
, aff
));
3000 return isl_pw_aff_zero_set(pwaff1
);
3001 return isl_pw_aff_nonneg_set(pwaff1
);
3004 /* Return a set containing those elements in the shared domain
3005 * of pwaff1 and pwaff2 where pwaff1 is equal to pwaff2.
3007 static __isl_give isl_set
*pw_aff_eq_set(__isl_take isl_pw_aff
*pwaff1
,
3008 __isl_take isl_pw_aff
*pwaff2
)
3010 return pw_aff_gte_set(pwaff1
, pwaff2
, 0, 1);
3013 __isl_give isl_set
*isl_pw_aff_eq_set(__isl_take isl_pw_aff
*pwaff1
,
3014 __isl_take isl_pw_aff
*pwaff2
)
3016 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_eq_set
);
3019 /* Return a set containing those elements in the shared domain
3020 * of pwaff1 and pwaff2 where pwaff1 is greater than or equal to pwaff2.
3022 static __isl_give isl_set
*pw_aff_ge_set(__isl_take isl_pw_aff
*pwaff1
,
3023 __isl_take isl_pw_aff
*pwaff2
)
3025 return pw_aff_gte_set(pwaff1
, pwaff2
, 0, 0);
3028 __isl_give isl_set
*isl_pw_aff_ge_set(__isl_take isl_pw_aff
*pwaff1
,
3029 __isl_take isl_pw_aff
*pwaff2
)
3031 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_ge_set
);
3034 /* Return a set containing those elements in the shared domain
3035 * of pwaff1 and pwaff2 where pwaff1 is strictly greater than pwaff2.
3037 static __isl_give isl_set
*pw_aff_gt_set(__isl_take isl_pw_aff
*pwaff1
,
3038 __isl_take isl_pw_aff
*pwaff2
)
3040 return pw_aff_gte_set(pwaff1
, pwaff2
, 1, 0);
3043 __isl_give isl_set
*isl_pw_aff_gt_set(__isl_take isl_pw_aff
*pwaff1
,
3044 __isl_take isl_pw_aff
*pwaff2
)
3046 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_gt_set
);
3049 __isl_give isl_set
*isl_pw_aff_le_set(__isl_take isl_pw_aff
*pwaff1
,
3050 __isl_take isl_pw_aff
*pwaff2
)
3052 return isl_pw_aff_ge_set(pwaff2
, pwaff1
);
3055 __isl_give isl_set
*isl_pw_aff_lt_set(__isl_take isl_pw_aff
*pwaff1
,
3056 __isl_take isl_pw_aff
*pwaff2
)
3058 return isl_pw_aff_gt_set(pwaff2
, pwaff1
);
3061 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3062 * where the function values are ordered in the same way as "order",
3063 * which returns a set in the shared domain of its two arguments.
3064 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3066 * Let "pa1" and "pa2" be defined on domains A and B respectively.
3067 * We first pull back the two functions such that they are defined on
3068 * the domain [A -> B]. Then we apply "order", resulting in a set
3069 * in the space [A -> B]. Finally, we unwrap this set to obtain
3070 * a map in the space A -> B.
3072 static __isl_give isl_map
*isl_pw_aff_order_map_aligned(
3073 __isl_take isl_pw_aff
*pa1
, __isl_take isl_pw_aff
*pa2
,
3074 __isl_give isl_set
*(*order
)(__isl_take isl_pw_aff
*pa1
,
3075 __isl_take isl_pw_aff
*pa2
))
3077 isl_space
*space1
, *space2
;
3081 space1
= isl_space_domain(isl_pw_aff_get_space(pa1
));
3082 space2
= isl_space_domain(isl_pw_aff_get_space(pa2
));
3083 space1
= isl_space_map_from_domain_and_range(space1
, space2
);
3084 ma
= isl_multi_aff_domain_map(isl_space_copy(space1
));
3085 pa1
= isl_pw_aff_pullback_multi_aff(pa1
, ma
);
3086 ma
= isl_multi_aff_range_map(space1
);
3087 pa2
= isl_pw_aff_pullback_multi_aff(pa2
, ma
);
3088 set
= order(pa1
, pa2
);
3090 return isl_set_unwrap(set
);
3093 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3094 * where the function values are equal.
3095 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3097 static __isl_give isl_map
*isl_pw_aff_eq_map_aligned(__isl_take isl_pw_aff
*pa1
,
3098 __isl_take isl_pw_aff
*pa2
)
3100 return isl_pw_aff_order_map_aligned(pa1
, pa2
, &isl_pw_aff_eq_set
);
3103 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3104 * where the function values are equal.
3106 __isl_give isl_map
*isl_pw_aff_eq_map(__isl_take isl_pw_aff
*pa1
,
3107 __isl_take isl_pw_aff
*pa2
)
3109 return align_params_pw_pw_map_and(pa1
, pa2
, &isl_pw_aff_eq_map_aligned
);
3112 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3113 * where the function value of "pa1" is less than the function value of "pa2".
3114 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3116 static __isl_give isl_map
*isl_pw_aff_lt_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_lt_set
);
3122 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3123 * where the function value of "pa1" is less than the function value of "pa2".
3125 __isl_give isl_map
*isl_pw_aff_lt_map(__isl_take isl_pw_aff
*pa1
,
3126 __isl_take isl_pw_aff
*pa2
)
3128 return align_params_pw_pw_map_and(pa1
, pa2
, &isl_pw_aff_lt_map_aligned
);
3131 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3132 * where the function value of "pa1" is greater than the function value
3134 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3136 static __isl_give isl_map
*isl_pw_aff_gt_map_aligned(__isl_take isl_pw_aff
*pa1
,
3137 __isl_take isl_pw_aff
*pa2
)
3139 return isl_pw_aff_order_map_aligned(pa1
, pa2
, &isl_pw_aff_gt_set
);
3142 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3143 * where the function value of "pa1" is greater than the function value
3146 __isl_give isl_map
*isl_pw_aff_gt_map(__isl_take isl_pw_aff
*pa1
,
3147 __isl_take isl_pw_aff
*pa2
)
3149 return align_params_pw_pw_map_and(pa1
, pa2
, &isl_pw_aff_gt_map_aligned
);
3152 /* Return a set containing those elements in the shared domain
3153 * of the elements of list1 and list2 where each element in list1
3154 * has the relation specified by "fn" with each element in list2.
3156 static __isl_give isl_set
*pw_aff_list_set(__isl_take isl_pw_aff_list
*list1
,
3157 __isl_take isl_pw_aff_list
*list2
,
3158 __isl_give isl_set
*(*fn
)(__isl_take isl_pw_aff
*pwaff1
,
3159 __isl_take isl_pw_aff
*pwaff2
))
3165 if (!list1
|| !list2
)
3168 ctx
= isl_pw_aff_list_get_ctx(list1
);
3169 if (list1
->n
< 1 || list2
->n
< 1)
3170 isl_die(ctx
, isl_error_invalid
,
3171 "list should contain at least one element", goto error
);
3173 set
= isl_set_universe(isl_pw_aff_get_domain_space(list1
->p
[0]));
3174 for (i
= 0; i
< list1
->n
; ++i
)
3175 for (j
= 0; j
< list2
->n
; ++j
) {
3178 set_ij
= fn(isl_pw_aff_copy(list1
->p
[i
]),
3179 isl_pw_aff_copy(list2
->p
[j
]));
3180 set
= isl_set_intersect(set
, set_ij
);
3183 isl_pw_aff_list_free(list1
);
3184 isl_pw_aff_list_free(list2
);
3187 isl_pw_aff_list_free(list1
);
3188 isl_pw_aff_list_free(list2
);
3192 /* Return a set containing those elements in the shared domain
3193 * of the elements of list1 and list2 where each element in list1
3194 * is equal to each element in list2.
3196 __isl_give isl_set
*isl_pw_aff_list_eq_set(__isl_take isl_pw_aff_list
*list1
,
3197 __isl_take isl_pw_aff_list
*list2
)
3199 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_eq_set
);
3202 __isl_give isl_set
*isl_pw_aff_list_ne_set(__isl_take isl_pw_aff_list
*list1
,
3203 __isl_take isl_pw_aff_list
*list2
)
3205 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_ne_set
);
3208 /* Return a set containing those elements in the shared domain
3209 * of the elements of list1 and list2 where each element in list1
3210 * is less than or equal to each element in list2.
3212 __isl_give isl_set
*isl_pw_aff_list_le_set(__isl_take isl_pw_aff_list
*list1
,
3213 __isl_take isl_pw_aff_list
*list2
)
3215 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_le_set
);
3218 __isl_give isl_set
*isl_pw_aff_list_lt_set(__isl_take isl_pw_aff_list
*list1
,
3219 __isl_take isl_pw_aff_list
*list2
)
3221 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_lt_set
);
3224 __isl_give isl_set
*isl_pw_aff_list_ge_set(__isl_take isl_pw_aff_list
*list1
,
3225 __isl_take isl_pw_aff_list
*list2
)
3227 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_ge_set
);
3230 __isl_give isl_set
*isl_pw_aff_list_gt_set(__isl_take isl_pw_aff_list
*list1
,
3231 __isl_take isl_pw_aff_list
*list2
)
3233 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_gt_set
);
3237 /* Return a set containing those elements in the shared domain
3238 * of pwaff1 and pwaff2 where pwaff1 is not equal to pwaff2.
3240 static __isl_give isl_set
*pw_aff_ne_set(__isl_take isl_pw_aff
*pwaff1
,
3241 __isl_take isl_pw_aff
*pwaff2
)
3243 isl_set
*set_lt
, *set_gt
;
3245 set_lt
= isl_pw_aff_lt_set(isl_pw_aff_copy(pwaff1
),
3246 isl_pw_aff_copy(pwaff2
));
3247 set_gt
= isl_pw_aff_gt_set(pwaff1
, pwaff2
);
3248 return isl_set_union_disjoint(set_lt
, set_gt
);
3251 __isl_give isl_set
*isl_pw_aff_ne_set(__isl_take isl_pw_aff
*pwaff1
,
3252 __isl_take isl_pw_aff
*pwaff2
)
3254 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_ne_set
);
3257 __isl_give isl_pw_aff
*isl_pw_aff_scale_down(__isl_take isl_pw_aff
*pwaff
,
3262 if (isl_int_is_one(v
))
3264 if (!isl_int_is_pos(v
))
3265 isl_die(isl_pw_aff_get_ctx(pwaff
), isl_error_invalid
,
3266 "factor needs to be positive",
3267 return isl_pw_aff_free(pwaff
));
3268 pwaff
= isl_pw_aff_cow(pwaff
);
3274 for (i
= 0; i
< pwaff
->n
; ++i
) {
3275 pwaff
->p
[i
].aff
= isl_aff_scale_down(pwaff
->p
[i
].aff
, v
);
3276 if (!pwaff
->p
[i
].aff
)
3277 return isl_pw_aff_free(pwaff
);
3283 __isl_give isl_pw_aff
*isl_pw_aff_floor(__isl_take isl_pw_aff
*pwaff
)
3287 pwaff
= isl_pw_aff_cow(pwaff
);
3293 for (i
= 0; i
< pwaff
->n
; ++i
) {
3294 pwaff
->p
[i
].aff
= isl_aff_floor(pwaff
->p
[i
].aff
);
3295 if (!pwaff
->p
[i
].aff
)
3296 return isl_pw_aff_free(pwaff
);
3302 __isl_give isl_pw_aff
*isl_pw_aff_ceil(__isl_take isl_pw_aff
*pwaff
)
3306 pwaff
= isl_pw_aff_cow(pwaff
);
3312 for (i
= 0; i
< pwaff
->n
; ++i
) {
3313 pwaff
->p
[i
].aff
= isl_aff_ceil(pwaff
->p
[i
].aff
);
3314 if (!pwaff
->p
[i
].aff
)
3315 return isl_pw_aff_free(pwaff
);
3321 /* Assuming that "cond1" and "cond2" are disjoint,
3322 * return an affine expression that is equal to pwaff1 on cond1
3323 * and to pwaff2 on cond2.
3325 static __isl_give isl_pw_aff
*isl_pw_aff_select(
3326 __isl_take isl_set
*cond1
, __isl_take isl_pw_aff
*pwaff1
,
3327 __isl_take isl_set
*cond2
, __isl_take isl_pw_aff
*pwaff2
)
3329 pwaff1
= isl_pw_aff_intersect_domain(pwaff1
, cond1
);
3330 pwaff2
= isl_pw_aff_intersect_domain(pwaff2
, cond2
);
3332 return isl_pw_aff_add_disjoint(pwaff1
, pwaff2
);
3335 /* Return an affine expression that is equal to pwaff_true for elements
3336 * where "cond" is non-zero and to pwaff_false for elements where "cond"
3338 * That is, return cond ? pwaff_true : pwaff_false;
3340 * If "cond" involves and NaN, then we conservatively return a NaN
3341 * on its entire domain. In principle, we could consider the pieces
3342 * where it is NaN separately from those where it is not.
3344 * If "pwaff_true" and "pwaff_false" are obviously equal to each other,
3345 * then only use the domain of "cond" to restrict the domain.
3347 __isl_give isl_pw_aff
*isl_pw_aff_cond(__isl_take isl_pw_aff
*cond
,
3348 __isl_take isl_pw_aff
*pwaff_true
, __isl_take isl_pw_aff
*pwaff_false
)
3350 isl_set
*cond_true
, *cond_false
;
3355 if (isl_pw_aff_involves_nan(cond
)) {
3356 isl_space
*space
= isl_pw_aff_get_domain_space(cond
);
3357 isl_local_space
*ls
= isl_local_space_from_space(space
);
3358 isl_pw_aff_free(cond
);
3359 isl_pw_aff_free(pwaff_true
);
3360 isl_pw_aff_free(pwaff_false
);
3361 return isl_pw_aff_nan_on_domain(ls
);
3364 pwaff_true
= isl_pw_aff_align_params(pwaff_true
,
3365 isl_pw_aff_get_space(pwaff_false
));
3366 pwaff_false
= isl_pw_aff_align_params(pwaff_false
,
3367 isl_pw_aff_get_space(pwaff_true
));
3368 equal
= isl_pw_aff_plain_is_equal(pwaff_true
, pwaff_false
);
3374 dom
= isl_set_coalesce(isl_pw_aff_domain(cond
));
3375 isl_pw_aff_free(pwaff_false
);
3376 return isl_pw_aff_intersect_domain(pwaff_true
, dom
);
3379 cond_true
= isl_pw_aff_non_zero_set(isl_pw_aff_copy(cond
));
3380 cond_false
= isl_pw_aff_zero_set(cond
);
3381 return isl_pw_aff_select(cond_true
, pwaff_true
,
3382 cond_false
, pwaff_false
);
3384 isl_pw_aff_free(cond
);
3385 isl_pw_aff_free(pwaff_true
);
3386 isl_pw_aff_free(pwaff_false
);
3390 isl_bool
isl_aff_is_cst(__isl_keep isl_aff
*aff
)
3395 return isl_bool_error
;
3397 pos
= isl_seq_first_non_zero(aff
->v
->el
+ 2, aff
->v
->size
- 2);
3398 return isl_bool_ok(pos
== -1);
3401 /* Check whether pwaff is a piecewise constant.
3403 isl_bool
isl_pw_aff_is_cst(__isl_keep isl_pw_aff
*pwaff
)
3408 return isl_bool_error
;
3410 for (i
= 0; i
< pwaff
->n
; ++i
) {
3411 isl_bool is_cst
= isl_aff_is_cst(pwaff
->p
[i
].aff
);
3412 if (is_cst
< 0 || !is_cst
)
3416 return isl_bool_true
;
3419 /* Are all elements of "mpa" piecewise constants?
3421 isl_bool
isl_multi_pw_aff_is_cst(__isl_keep isl_multi_pw_aff
*mpa
)
3426 return isl_bool_error
;
3428 for (i
= 0; i
< mpa
->n
; ++i
) {
3429 isl_bool is_cst
= isl_pw_aff_is_cst(mpa
->u
.p
[i
]);
3430 if (is_cst
< 0 || !is_cst
)
3434 return isl_bool_true
;
3437 /* Return the product of "aff1" and "aff2".
3439 * If either of the two is NaN, then the result is NaN.
3441 * Otherwise, at least one of "aff1" or "aff2" needs to be a constant.
3443 __isl_give isl_aff
*isl_aff_mul(__isl_take isl_aff
*aff1
,
3444 __isl_take isl_aff
*aff2
)
3449 if (isl_aff_is_nan(aff1
)) {
3453 if (isl_aff_is_nan(aff2
)) {
3458 if (!isl_aff_is_cst(aff2
) && isl_aff_is_cst(aff1
))
3459 return isl_aff_mul(aff2
, aff1
);
3461 if (!isl_aff_is_cst(aff2
))
3462 isl_die(isl_aff_get_ctx(aff1
), isl_error_invalid
,
3463 "at least one affine expression should be constant",
3466 aff1
= isl_aff_cow(aff1
);
3470 aff1
= isl_aff_scale(aff1
, aff2
->v
->el
[1]);
3471 aff1
= isl_aff_scale_down(aff1
, aff2
->v
->el
[0]);
3481 /* Divide "aff1" by "aff2", assuming "aff2" is a constant.
3483 * If either of the two is NaN, then the result is NaN.
3485 __isl_give isl_aff
*isl_aff_div(__isl_take isl_aff
*aff1
,
3486 __isl_take isl_aff
*aff2
)
3494 if (isl_aff_is_nan(aff1
)) {
3498 if (isl_aff_is_nan(aff2
)) {
3503 is_cst
= isl_aff_is_cst(aff2
);
3507 isl_die(isl_aff_get_ctx(aff2
), isl_error_invalid
,
3508 "second argument should be a constant", goto error
);
3513 neg
= isl_int_is_neg(aff2
->v
->el
[1]);
3515 isl_int_neg(aff2
->v
->el
[0], aff2
->v
->el
[0]);
3516 isl_int_neg(aff2
->v
->el
[1], aff2
->v
->el
[1]);
3519 aff1
= isl_aff_scale(aff1
, aff2
->v
->el
[0]);
3520 aff1
= isl_aff_scale_down(aff1
, aff2
->v
->el
[1]);
3523 isl_int_neg(aff2
->v
->el
[0], aff2
->v
->el
[0]);
3524 isl_int_neg(aff2
->v
->el
[1], aff2
->v
->el
[1]);
3535 static __isl_give isl_pw_aff
*pw_aff_add(__isl_take isl_pw_aff
*pwaff1
,
3536 __isl_take isl_pw_aff
*pwaff2
)
3538 return isl_pw_aff_on_shared_domain(pwaff1
, pwaff2
, &isl_aff_add
);
3541 __isl_give isl_pw_aff
*isl_pw_aff_add(__isl_take isl_pw_aff
*pwaff1
,
3542 __isl_take isl_pw_aff
*pwaff2
)
3544 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
, &pw_aff_add
);
3547 __isl_give isl_pw_aff
*isl_pw_aff_union_add(__isl_take isl_pw_aff
*pwaff1
,
3548 __isl_take isl_pw_aff
*pwaff2
)
3550 return isl_pw_aff_union_add_(pwaff1
, pwaff2
);
3553 static __isl_give isl_pw_aff
*pw_aff_mul(__isl_take isl_pw_aff
*pwaff1
,
3554 __isl_take isl_pw_aff
*pwaff2
)
3556 return isl_pw_aff_on_shared_domain(pwaff1
, pwaff2
, &isl_aff_mul
);
3559 __isl_give isl_pw_aff
*isl_pw_aff_mul(__isl_take isl_pw_aff
*pwaff1
,
3560 __isl_take isl_pw_aff
*pwaff2
)
3562 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
, &pw_aff_mul
);
3565 static __isl_give isl_pw_aff
*pw_aff_div(__isl_take isl_pw_aff
*pa1
,
3566 __isl_take isl_pw_aff
*pa2
)
3568 return isl_pw_aff_on_shared_domain(pa1
, pa2
, &isl_aff_div
);
3571 /* Divide "pa1" by "pa2", assuming "pa2" is a piecewise constant.
3573 __isl_give isl_pw_aff
*isl_pw_aff_div(__isl_take isl_pw_aff
*pa1
,
3574 __isl_take isl_pw_aff
*pa2
)
3578 is_cst
= isl_pw_aff_is_cst(pa2
);
3582 isl_die(isl_pw_aff_get_ctx(pa2
), isl_error_invalid
,
3583 "second argument should be a piecewise constant",
3585 return isl_pw_aff_align_params_pw_pw_and(pa1
, pa2
, &pw_aff_div
);
3587 isl_pw_aff_free(pa1
);
3588 isl_pw_aff_free(pa2
);
3592 /* Compute the quotient of the integer division of "pa1" by "pa2"
3593 * with rounding towards zero.
3594 * "pa2" is assumed to be a piecewise constant.
3596 * In particular, return
3598 * pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2)
3601 __isl_give isl_pw_aff
*isl_pw_aff_tdiv_q(__isl_take isl_pw_aff
*pa1
,
3602 __isl_take isl_pw_aff
*pa2
)
3608 is_cst
= isl_pw_aff_is_cst(pa2
);
3612 isl_die(isl_pw_aff_get_ctx(pa2
), isl_error_invalid
,
3613 "second argument should be a piecewise constant",
3616 pa1
= isl_pw_aff_div(pa1
, pa2
);
3618 cond
= isl_pw_aff_nonneg_set(isl_pw_aff_copy(pa1
));
3619 f
= isl_pw_aff_floor(isl_pw_aff_copy(pa1
));
3620 c
= isl_pw_aff_ceil(pa1
);
3621 return isl_pw_aff_cond(isl_set_indicator_function(cond
), f
, c
);
3623 isl_pw_aff_free(pa1
);
3624 isl_pw_aff_free(pa2
);
3628 /* Compute the remainder of the integer division of "pa1" by "pa2"
3629 * with rounding towards zero.
3630 * "pa2" is assumed to be a piecewise constant.
3632 * In particular, return
3634 * pa1 - pa2 * (pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2))
3637 __isl_give isl_pw_aff
*isl_pw_aff_tdiv_r(__isl_take isl_pw_aff
*pa1
,
3638 __isl_take isl_pw_aff
*pa2
)
3643 is_cst
= isl_pw_aff_is_cst(pa2
);
3647 isl_die(isl_pw_aff_get_ctx(pa2
), isl_error_invalid
,
3648 "second argument should be a piecewise constant",
3650 res
= isl_pw_aff_tdiv_q(isl_pw_aff_copy(pa1
), isl_pw_aff_copy(pa2
));
3651 res
= isl_pw_aff_mul(pa2
, res
);
3652 res
= isl_pw_aff_sub(pa1
, res
);
3655 isl_pw_aff_free(pa1
);
3656 isl_pw_aff_free(pa2
);
3660 /* Does either of "pa1" or "pa2" involve any NaN2?
3662 static isl_bool
either_involves_nan(__isl_keep isl_pw_aff
*pa1
,
3663 __isl_keep isl_pw_aff
*pa2
)
3667 has_nan
= isl_pw_aff_involves_nan(pa1
);
3668 if (has_nan
< 0 || has_nan
)
3670 return isl_pw_aff_involves_nan(pa2
);
3673 /* Replace "pa1" and "pa2" (at least one of which involves a NaN)
3674 * by a NaN on their shared domain.
3676 * In principle, the result could be refined to only being NaN
3677 * on the parts of this domain where at least one of "pa1" or "pa2" is NaN.
3679 static __isl_give isl_pw_aff
*replace_by_nan(__isl_take isl_pw_aff
*pa1
,
3680 __isl_take isl_pw_aff
*pa2
)
3682 isl_local_space
*ls
;
3686 dom
= isl_set_intersect(isl_pw_aff_domain(pa1
), isl_pw_aff_domain(pa2
));
3687 ls
= isl_local_space_from_space(isl_set_get_space(dom
));
3688 pa
= isl_pw_aff_nan_on_domain(ls
);
3689 pa
= isl_pw_aff_intersect_domain(pa
, dom
);
3694 static __isl_give isl_pw_aff
*pw_aff_min(__isl_take isl_pw_aff
*pwaff1
,
3695 __isl_take isl_pw_aff
*pwaff2
)
3700 dom
= isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1
)),
3701 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2
)));
3702 le
= isl_pw_aff_le_set(isl_pw_aff_copy(pwaff1
),
3703 isl_pw_aff_copy(pwaff2
));
3704 dom
= isl_set_subtract(dom
, isl_set_copy(le
));
3705 return isl_pw_aff_select(le
, pwaff1
, dom
, pwaff2
);
3708 static __isl_give isl_pw_aff
*pw_aff_max(__isl_take isl_pw_aff
*pwaff1
,
3709 __isl_take isl_pw_aff
*pwaff2
)
3714 dom
= isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1
)),
3715 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2
)));
3716 ge
= isl_pw_aff_ge_set(isl_pw_aff_copy(pwaff1
),
3717 isl_pw_aff_copy(pwaff2
));
3718 dom
= isl_set_subtract(dom
, isl_set_copy(ge
));
3719 return isl_pw_aff_select(ge
, pwaff1
, dom
, pwaff2
);
3722 /* Return an expression for the minimum (if "max" is not set) or
3723 * the maximum (if "max" is set) of "pa1" and "pa2".
3724 * If either expression involves any NaN, then return a NaN
3725 * on the shared domain as result.
3727 static __isl_give isl_pw_aff
*pw_aff_min_max(__isl_take isl_pw_aff
*pa1
,
3728 __isl_take isl_pw_aff
*pa2
, int max
)
3732 has_nan
= either_involves_nan(pa1
, pa2
);
3734 pa1
= isl_pw_aff_free(pa1
);
3736 return replace_by_nan(pa1
, pa2
);
3739 return isl_pw_aff_align_params_pw_pw_and(pa1
, pa2
, &pw_aff_max
);
3741 return isl_pw_aff_align_params_pw_pw_and(pa1
, pa2
, &pw_aff_min
);
3744 /* Return an expression for the minimum of "pwaff1" and "pwaff2".
3746 __isl_give isl_pw_aff
*isl_pw_aff_min(__isl_take isl_pw_aff
*pwaff1
,
3747 __isl_take isl_pw_aff
*pwaff2
)
3749 return pw_aff_min_max(pwaff1
, pwaff2
, 0);
3752 /* Return an expression for the maximum of "pwaff1" and "pwaff2".
3754 __isl_give isl_pw_aff
*isl_pw_aff_max(__isl_take isl_pw_aff
*pwaff1
,
3755 __isl_take isl_pw_aff
*pwaff2
)
3757 return pw_aff_min_max(pwaff1
, pwaff2
, 1);
3760 static __isl_give isl_pw_aff
*pw_aff_list_reduce(
3761 __isl_take isl_pw_aff_list
*list
,
3762 __isl_give isl_pw_aff
*(*fn
)(__isl_take isl_pw_aff
*pwaff1
,
3763 __isl_take isl_pw_aff
*pwaff2
))
3772 ctx
= isl_pw_aff_list_get_ctx(list
);
3774 isl_die(ctx
, isl_error_invalid
,
3775 "list should contain at least one element", goto error
);
3777 res
= isl_pw_aff_copy(list
->p
[0]);
3778 for (i
= 1; i
< list
->n
; ++i
)
3779 res
= fn(res
, isl_pw_aff_copy(list
->p
[i
]));
3781 isl_pw_aff_list_free(list
);
3784 isl_pw_aff_list_free(list
);
3788 /* Return an isl_pw_aff that maps each element in the intersection of the
3789 * domains of the elements of list to the minimal corresponding affine
3792 __isl_give isl_pw_aff
*isl_pw_aff_list_min(__isl_take isl_pw_aff_list
*list
)
3794 return pw_aff_list_reduce(list
, &isl_pw_aff_min
);
3797 /* Return an isl_pw_aff that maps each element in the intersection of the
3798 * domains of the elements of list to the maximal corresponding affine
3801 __isl_give isl_pw_aff
*isl_pw_aff_list_max(__isl_take isl_pw_aff_list
*list
)
3803 return pw_aff_list_reduce(list
, &isl_pw_aff_max
);
3806 /* Mark the domains of "pwaff" as rational.
3808 __isl_give isl_pw_aff
*isl_pw_aff_set_rational(__isl_take isl_pw_aff
*pwaff
)
3812 pwaff
= isl_pw_aff_cow(pwaff
);
3818 for (i
= 0; i
< pwaff
->n
; ++i
) {
3819 pwaff
->p
[i
].set
= isl_set_set_rational(pwaff
->p
[i
].set
);
3820 if (!pwaff
->p
[i
].set
)
3821 return isl_pw_aff_free(pwaff
);
3827 /* Mark the domains of the elements of "list" as rational.
3829 __isl_give isl_pw_aff_list
*isl_pw_aff_list_set_rational(
3830 __isl_take isl_pw_aff_list
*list
)
3840 for (i
= 0; i
< n
; ++i
) {
3843 pa
= isl_pw_aff_list_get_pw_aff(list
, i
);
3844 pa
= isl_pw_aff_set_rational(pa
);
3845 list
= isl_pw_aff_list_set_pw_aff(list
, i
, pa
);
3851 /* Do the parameters of "aff" match those of "space"?
3853 isl_bool
isl_aff_matching_params(__isl_keep isl_aff
*aff
,
3854 __isl_keep isl_space
*space
)
3856 isl_space
*aff_space
;
3860 return isl_bool_error
;
3862 aff_space
= isl_aff_get_domain_space(aff
);
3864 match
= isl_space_has_equal_params(space
, aff_space
);
3866 isl_space_free(aff_space
);
3870 /* Check that the domain space of "aff" matches "space".
3872 isl_stat
isl_aff_check_match_domain_space(__isl_keep isl_aff
*aff
,
3873 __isl_keep isl_space
*space
)
3875 isl_space
*aff_space
;
3879 return isl_stat_error
;
3881 aff_space
= isl_aff_get_domain_space(aff
);
3883 match
= isl_space_has_equal_params(space
, aff_space
);
3887 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
3888 "parameters don't match", goto error
);
3889 match
= isl_space_tuple_is_equal(space
, isl_dim_in
,
3890 aff_space
, isl_dim_set
);
3894 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
3895 "domains don't match", goto error
);
3896 isl_space_free(aff_space
);
3899 isl_space_free(aff_space
);
3900 return isl_stat_error
;
3906 #include <isl_multi_no_explicit_domain.c>
3907 #include <isl_multi_templ.c>
3908 #include <isl_multi_apply_set.c>
3909 #include <isl_multi_arith_templ.c>
3910 #include <isl_multi_cmp.c>
3911 #include <isl_multi_dim_id_templ.c>
3912 #include <isl_multi_dims.c>
3913 #include <isl_multi_floor.c>
3914 #include <isl_multi_from_base_templ.c>
3915 #include <isl_multi_identity_templ.c>
3916 #include <isl_multi_move_dims_templ.c>
3917 #include <isl_multi_nan_templ.c>
3918 #include <isl_multi_product_templ.c>
3919 #include <isl_multi_splice_templ.c>
3920 #include <isl_multi_tuple_id_templ.c>
3921 #include <isl_multi_zero_templ.c>
3925 #include <isl_multi_gist.c>
3927 /* Construct an isl_multi_aff living in "space" that corresponds
3928 * to the affine transformation matrix "mat".
3930 __isl_give isl_multi_aff
*isl_multi_aff_from_aff_mat(
3931 __isl_take isl_space
*space
, __isl_take isl_mat
*mat
)
3934 isl_local_space
*ls
= NULL
;
3935 isl_multi_aff
*ma
= NULL
;
3936 isl_size n_row
, n_col
, n_out
, total
;
3942 ctx
= isl_mat_get_ctx(mat
);
3944 n_row
= isl_mat_rows(mat
);
3945 n_col
= isl_mat_cols(mat
);
3946 n_out
= isl_space_dim(space
, isl_dim_out
);
3947 total
= isl_space_dim(space
, isl_dim_all
);
3948 if (n_row
< 0 || n_col
< 0 || n_out
< 0 || total
< 0)
3951 isl_die(ctx
, isl_error_invalid
,
3952 "insufficient number of rows", goto error
);
3954 isl_die(ctx
, isl_error_invalid
,
3955 "insufficient number of columns", goto error
);
3956 if (1 + n_out
!= n_row
|| 2 + total
!= n_row
+ n_col
)
3957 isl_die(ctx
, isl_error_invalid
,
3958 "dimension mismatch", goto error
);
3960 ma
= isl_multi_aff_zero(isl_space_copy(space
));
3961 ls
= isl_local_space_from_space(isl_space_domain(space
));
3963 for (i
= 0; i
< n_row
- 1; ++i
) {
3967 v
= isl_vec_alloc(ctx
, 1 + n_col
);
3970 isl_int_set(v
->el
[0], mat
->row
[0][0]);
3971 isl_seq_cpy(v
->el
+ 1, mat
->row
[1 + i
], n_col
);
3972 v
= isl_vec_normalize(v
);
3973 aff
= isl_aff_alloc_vec(isl_local_space_copy(ls
), v
);
3974 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
3977 isl_local_space_free(ls
);
3981 isl_local_space_free(ls
);
3983 isl_multi_aff_free(ma
);
3987 /* Remove any internal structure of the domain of "ma".
3988 * If there is any such internal structure in the input,
3989 * then the name of the corresponding space is also removed.
3991 __isl_give isl_multi_aff
*isl_multi_aff_flatten_domain(
3992 __isl_take isl_multi_aff
*ma
)
3999 if (!ma
->space
->nested
[0])
4002 space
= isl_multi_aff_get_space(ma
);
4003 space
= isl_space_flatten_domain(space
);
4004 ma
= isl_multi_aff_reset_space(ma
, space
);
4009 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
4010 * of the space to its domain.
4012 __isl_give isl_multi_aff
*isl_multi_aff_domain_map(__isl_take isl_space
*space
)
4016 isl_local_space
*ls
;
4021 if (!isl_space_is_map(space
))
4022 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
4023 "not a map space", goto error
);
4025 n_in
= isl_space_dim(space
, isl_dim_in
);
4028 space
= isl_space_domain_map(space
);
4030 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
4032 isl_space_free(space
);
4036 space
= isl_space_domain(space
);
4037 ls
= isl_local_space_from_space(space
);
4038 for (i
= 0; i
< n_in
; ++i
) {
4041 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
4043 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
4045 isl_local_space_free(ls
);
4048 isl_space_free(space
);
4052 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
4053 * of the space to its range.
4055 __isl_give isl_multi_aff
*isl_multi_aff_range_map(__isl_take isl_space
*space
)
4058 isl_size n_in
, n_out
;
4059 isl_local_space
*ls
;
4064 if (!isl_space_is_map(space
))
4065 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
4066 "not a map space", goto error
);
4068 n_in
= isl_space_dim(space
, isl_dim_in
);
4069 n_out
= isl_space_dim(space
, isl_dim_out
);
4070 if (n_in
< 0 || n_out
< 0)
4072 space
= isl_space_range_map(space
);
4074 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
4076 isl_space_free(space
);
4080 space
= isl_space_domain(space
);
4081 ls
= isl_local_space_from_space(space
);
4082 for (i
= 0; i
< n_out
; ++i
) {
4085 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
4086 isl_dim_set
, n_in
+ i
);
4087 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
4089 isl_local_space_free(ls
);
4092 isl_space_free(space
);
4096 /* Given a map space, return an isl_pw_multi_aff that maps a wrapped copy
4097 * of the space to its range.
4099 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_range_map(
4100 __isl_take isl_space
*space
)
4102 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_range_map(space
));
4105 /* Given the space of a set and a range of set dimensions,
4106 * construct an isl_multi_aff that projects out those dimensions.
4108 __isl_give isl_multi_aff
*isl_multi_aff_project_out_map(
4109 __isl_take isl_space
*space
, enum isl_dim_type type
,
4110 unsigned first
, unsigned n
)
4114 isl_local_space
*ls
;
4119 if (!isl_space_is_set(space
))
4120 isl_die(isl_space_get_ctx(space
), isl_error_unsupported
,
4121 "expecting set space", goto error
);
4122 if (type
!= isl_dim_set
)
4123 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
4124 "only set dimensions can be projected out", goto error
);
4125 if (isl_space_check_range(space
, type
, first
, n
) < 0)
4128 dim
= isl_space_dim(space
, isl_dim_set
);
4132 space
= isl_space_from_domain(space
);
4133 space
= isl_space_add_dims(space
, isl_dim_out
, dim
- n
);
4136 return isl_multi_aff_alloc(space
);
4138 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
4139 space
= isl_space_domain(space
);
4140 ls
= isl_local_space_from_space(space
);
4142 for (i
= 0; i
< first
; ++i
) {
4145 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
4147 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
4150 for (i
= 0; i
< dim
- (first
+ n
); ++i
) {
4153 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
4154 isl_dim_set
, first
+ n
+ i
);
4155 ma
= isl_multi_aff_set_aff(ma
, first
+ i
, aff
);
4158 isl_local_space_free(ls
);
4161 isl_space_free(space
);
4165 /* Given the space of a set and a range of set dimensions,
4166 * construct an isl_pw_multi_aff that projects out those dimensions.
4168 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_project_out_map(
4169 __isl_take isl_space
*space
, enum isl_dim_type type
,
4170 unsigned first
, unsigned n
)
4174 ma
= isl_multi_aff_project_out_map(space
, type
, first
, n
);
4175 return isl_pw_multi_aff_from_multi_aff(ma
);
4178 /* Create an isl_pw_multi_aff with the given isl_multi_aff on a universe
4181 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_multi_aff(
4182 __isl_take isl_multi_aff
*ma
)
4184 isl_set
*dom
= isl_set_universe(isl_multi_aff_get_domain_space(ma
));
4185 return isl_pw_multi_aff_alloc(dom
, ma
);
4188 /* Create a piecewise multi-affine expression in the given space that maps each
4189 * input dimension to the corresponding output dimension.
4191 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_identity(
4192 __isl_take isl_space
*space
)
4194 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_identity(space
));
4197 /* Exploit the equalities in "eq" to simplify the affine expressions.
4199 static __isl_give isl_multi_aff
*isl_multi_aff_substitute_equalities(
4200 __isl_take isl_multi_aff
*maff
, __isl_take isl_basic_set
*eq
)
4204 maff
= isl_multi_aff_cow(maff
);
4208 for (i
= 0; i
< maff
->n
; ++i
) {
4209 maff
->u
.p
[i
] = isl_aff_substitute_equalities(maff
->u
.p
[i
],
4210 isl_basic_set_copy(eq
));
4215 isl_basic_set_free(eq
);
4218 isl_basic_set_free(eq
);
4219 isl_multi_aff_free(maff
);
4223 __isl_give isl_multi_aff
*isl_multi_aff_scale(__isl_take isl_multi_aff
*maff
,
4228 maff
= isl_multi_aff_cow(maff
);
4232 for (i
= 0; i
< maff
->n
; ++i
) {
4233 maff
->u
.p
[i
] = isl_aff_scale(maff
->u
.p
[i
], f
);
4235 return isl_multi_aff_free(maff
);
4241 __isl_give isl_multi_aff
*isl_multi_aff_add_on_domain(__isl_keep isl_set
*dom
,
4242 __isl_take isl_multi_aff
*maff1
, __isl_take isl_multi_aff
*maff2
)
4244 maff1
= isl_multi_aff_add(maff1
, maff2
);
4245 maff1
= isl_multi_aff_gist(maff1
, isl_set_copy(dom
));
4249 int isl_multi_aff_is_empty(__isl_keep isl_multi_aff
*maff
)
4257 /* Return the set of domain elements where "ma1" is lexicographically
4258 * smaller than or equal to "ma2".
4260 __isl_give isl_set
*isl_multi_aff_lex_le_set(__isl_take isl_multi_aff
*ma1
,
4261 __isl_take isl_multi_aff
*ma2
)
4263 return isl_multi_aff_lex_ge_set(ma2
, ma1
);
4266 /* Return the set of domain elements where "ma1" is lexicographically
4267 * smaller than "ma2".
4269 __isl_give isl_set
*isl_multi_aff_lex_lt_set(__isl_take isl_multi_aff
*ma1
,
4270 __isl_take isl_multi_aff
*ma2
)
4272 return isl_multi_aff_lex_gt_set(ma2
, ma1
);
4275 /* Return the set of domain elements where "ma1" and "ma2"
4278 static __isl_give isl_set
*isl_multi_aff_order_set(
4279 __isl_take isl_multi_aff
*ma1
, __isl_take isl_multi_aff
*ma2
,
4280 __isl_give isl_map
*order(__isl_take isl_space
*set_space
))
4283 isl_map
*map1
, *map2
;
4286 map1
= isl_map_from_multi_aff_internal(ma1
);
4287 map2
= isl_map_from_multi_aff_internal(ma2
);
4288 map
= isl_map_range_product(map1
, map2
);
4289 space
= isl_space_range(isl_map_get_space(map
));
4290 space
= isl_space_domain(isl_space_unwrap(space
));
4292 map
= isl_map_intersect_range(map
, isl_map_wrap(ge
));
4294 return isl_map_domain(map
);
4297 /* Return the set of domain elements where "ma1" is lexicographically
4298 * greater than or equal to "ma2".
4300 __isl_give isl_set
*isl_multi_aff_lex_ge_set(__isl_take isl_multi_aff
*ma1
,
4301 __isl_take isl_multi_aff
*ma2
)
4303 return isl_multi_aff_order_set(ma1
, ma2
, &isl_map_lex_ge
);
4306 /* Return the set of domain elements where "ma1" is lexicographically
4307 * greater than "ma2".
4309 __isl_give isl_set
*isl_multi_aff_lex_gt_set(__isl_take isl_multi_aff
*ma1
,
4310 __isl_take isl_multi_aff
*ma2
)
4312 return isl_multi_aff_order_set(ma1
, ma2
, &isl_map_lex_gt
);
4316 #define PW isl_pw_multi_aff
4318 #define EL isl_multi_aff
4320 #define EL_IS_ZERO is_empty
4324 #define IS_ZERO is_empty
4327 #undef DEFAULT_IS_ZERO
4328 #define DEFAULT_IS_ZERO 0
4332 #define NO_INSERT_DIMS
4336 #include <isl_pw_templ.c>
4337 #include <isl_pw_union_opt.c>
4342 #define BASE pw_multi_aff
4344 #include <isl_union_multi.c>
4345 #include <isl_union_neg.c>
4347 static __isl_give isl_pw_multi_aff
*pw_multi_aff_union_lexmax(
4348 __isl_take isl_pw_multi_aff
*pma1
,
4349 __isl_take isl_pw_multi_aff
*pma2
)
4351 return isl_pw_multi_aff_union_opt_cmp(pma1
, pma2
,
4352 &isl_multi_aff_lex_ge_set
);
4355 /* Given two piecewise multi affine expressions, return a piecewise
4356 * multi-affine expression defined on the union of the definition domains
4357 * of the inputs that is equal to the lexicographic maximum of the two
4358 * inputs on each cell. If only one of the two inputs is defined on
4359 * a given cell, then it is considered to be the maximum.
4361 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_union_lexmax(
4362 __isl_take isl_pw_multi_aff
*pma1
,
4363 __isl_take isl_pw_multi_aff
*pma2
)
4365 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
4366 &pw_multi_aff_union_lexmax
);
4369 static __isl_give isl_pw_multi_aff
*pw_multi_aff_union_lexmin(
4370 __isl_take isl_pw_multi_aff
*pma1
,
4371 __isl_take isl_pw_multi_aff
*pma2
)
4373 return isl_pw_multi_aff_union_opt_cmp(pma1
, pma2
,
4374 &isl_multi_aff_lex_le_set
);
4377 /* Given two piecewise multi affine expressions, return a piecewise
4378 * multi-affine expression defined on the union of the definition domains
4379 * of the inputs that is equal to the lexicographic minimum of the two
4380 * inputs on each cell. If only one of the two inputs is defined on
4381 * a given cell, then it is considered to be the minimum.
4383 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_union_lexmin(
4384 __isl_take isl_pw_multi_aff
*pma1
,
4385 __isl_take isl_pw_multi_aff
*pma2
)
4387 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
4388 &pw_multi_aff_union_lexmin
);
4391 static __isl_give isl_pw_multi_aff
*pw_multi_aff_add(
4392 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4394 return isl_pw_multi_aff_on_shared_domain(pma1
, pma2
,
4395 &isl_multi_aff_add
);
4398 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_add(
4399 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4401 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
4405 static __isl_give isl_pw_multi_aff
*pw_multi_aff_sub(
4406 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4408 return isl_pw_multi_aff_on_shared_domain(pma1
, pma2
,
4409 &isl_multi_aff_sub
);
4412 /* Subtract "pma2" from "pma1" and return the result.
4414 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_sub(
4415 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4417 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
4421 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_union_add(
4422 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4424 return isl_pw_multi_aff_union_add_(pma1
, pma2
);
4427 /* Compute the sum of "upa1" and "upa2" on the union of their domains,
4428 * with the actual sum on the shared domain and
4429 * the defined expression on the symmetric difference of the domains.
4431 __isl_give isl_union_pw_aff
*isl_union_pw_aff_union_add(
4432 __isl_take isl_union_pw_aff
*upa1
, __isl_take isl_union_pw_aff
*upa2
)
4434 return isl_union_pw_aff_union_add_(upa1
, upa2
);
4437 /* Compute the sum of "upma1" and "upma2" on the union of their domains,
4438 * with the actual sum on the shared domain and
4439 * the defined expression on the symmetric difference of the domains.
4441 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_union_add(
4442 __isl_take isl_union_pw_multi_aff
*upma1
,
4443 __isl_take isl_union_pw_multi_aff
*upma2
)
4445 return isl_union_pw_multi_aff_union_add_(upma1
, upma2
);
4448 /* Given two piecewise multi-affine expressions A -> B and C -> D,
4449 * construct a piecewise multi-affine expression [A -> C] -> [B -> D].
4451 static __isl_give isl_pw_multi_aff
*pw_multi_aff_product(
4452 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4456 isl_pw_multi_aff
*res
;
4461 n
= pma1
->n
* pma2
->n
;
4462 space
= isl_space_product(isl_space_copy(pma1
->dim
),
4463 isl_space_copy(pma2
->dim
));
4464 res
= isl_pw_multi_aff_alloc_size(space
, n
);
4466 for (i
= 0; i
< pma1
->n
; ++i
) {
4467 for (j
= 0; j
< pma2
->n
; ++j
) {
4471 domain
= isl_set_product(isl_set_copy(pma1
->p
[i
].set
),
4472 isl_set_copy(pma2
->p
[j
].set
));
4473 ma
= isl_multi_aff_product(
4474 isl_multi_aff_copy(pma1
->p
[i
].maff
),
4475 isl_multi_aff_copy(pma2
->p
[j
].maff
));
4476 res
= isl_pw_multi_aff_add_piece(res
, domain
, ma
);
4480 isl_pw_multi_aff_free(pma1
);
4481 isl_pw_multi_aff_free(pma2
);
4484 isl_pw_multi_aff_free(pma1
);
4485 isl_pw_multi_aff_free(pma2
);
4489 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_product(
4490 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4492 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
4493 &pw_multi_aff_product
);
4496 /* Subtract the initial "n" elements in "ma" with coefficients in "c" and
4497 * denominator "denom".
4498 * "denom" is allowed to be negative, in which case the actual denominator
4499 * is -denom and the expressions are added instead.
4501 static __isl_give isl_aff
*subtract_initial(__isl_take isl_aff
*aff
,
4502 __isl_keep isl_multi_aff
*ma
, int n
, isl_int
*c
, isl_int denom
)
4508 first
= isl_seq_first_non_zero(c
, n
);
4512 sign
= isl_int_sgn(denom
);
4514 isl_int_abs(d
, denom
);
4515 for (i
= first
; i
< n
; ++i
) {
4518 if (isl_int_is_zero(c
[i
]))
4520 aff_i
= isl_multi_aff_get_aff(ma
, i
);
4521 aff_i
= isl_aff_scale(aff_i
, c
[i
]);
4522 aff_i
= isl_aff_scale_down(aff_i
, d
);
4524 aff
= isl_aff_sub(aff
, aff_i
);
4526 aff
= isl_aff_add(aff
, aff_i
);
4533 /* Extract an affine expression that expresses the output dimension "pos"
4534 * of "bmap" in terms of the parameters and input dimensions from
4536 * Note that this expression may involve integer divisions defined
4537 * in terms of parameters and input dimensions.
4538 * The equality may also involve references to earlier (but not later)
4539 * output dimensions. These are replaced by the corresponding elements
4542 * If the equality is of the form
4544 * f(i) + h(j) + a x + g(i) = 0,
4546 * with f(i) a linear combinations of the parameters and input dimensions,
4547 * g(i) a linear combination of integer divisions defined in terms of the same
4548 * and h(j) a linear combinations of earlier output dimensions,
4549 * then the affine expression is
4551 * (-f(i) - g(i))/a - h(j)/a
4553 * If the equality is of the form
4555 * f(i) + h(j) - a x + g(i) = 0,
4557 * then the affine expression is
4559 * (f(i) + g(i))/a - h(j)/(-a)
4562 * If "div" refers to an integer division (i.e., it is smaller than
4563 * the number of integer divisions), then the equality constraint
4564 * does involve an integer division (the one at position "div") that
4565 * is defined in terms of output dimensions. However, this integer
4566 * division can be eliminated by exploiting a pair of constraints
4567 * x >= l and x <= l + n, with n smaller than the coefficient of "div"
4568 * in the equality constraint. "ineq" refers to inequality x >= l, i.e.,
4570 * In particular, let
4572 * x = e(i) + m floor(...)
4574 * with e(i) the expression derived above and floor(...) the integer
4575 * division involving output dimensions.
4586 * e(i) + m floor(...) - l = (e(i) + m floor(...) - l) mod m
4587 * = (e(i) - l) mod m
4591 * x - l = (e(i) - l) mod m
4595 * x = ((e(i) - l) mod m) + l
4597 * The variable "shift" below contains the expression -l, which may
4598 * also involve a linear combination of earlier output dimensions.
4600 static __isl_give isl_aff
*extract_aff_from_equality(
4601 __isl_keep isl_basic_map
*bmap
, int pos
, int eq
, int div
, int ineq
,
4602 __isl_keep isl_multi_aff
*ma
)
4605 isl_size n_div
, n_out
;
4607 isl_local_space
*ls
;
4608 isl_aff
*aff
, *shift
;
4611 ctx
= isl_basic_map_get_ctx(bmap
);
4612 ls
= isl_basic_map_get_local_space(bmap
);
4613 ls
= isl_local_space_domain(ls
);
4614 aff
= isl_aff_alloc(isl_local_space_copy(ls
));
4617 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
4618 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4619 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4620 if (n_out
< 0 || n_div
< 0)
4622 if (isl_int_is_neg(bmap
->eq
[eq
][o_out
+ pos
])) {
4623 isl_seq_cpy(aff
->v
->el
+ 1, bmap
->eq
[eq
], o_out
);
4624 isl_seq_cpy(aff
->v
->el
+ 1 + o_out
,
4625 bmap
->eq
[eq
] + o_out
+ n_out
, n_div
);
4627 isl_seq_neg(aff
->v
->el
+ 1, bmap
->eq
[eq
], o_out
);
4628 isl_seq_neg(aff
->v
->el
+ 1 + o_out
,
4629 bmap
->eq
[eq
] + o_out
+ n_out
, n_div
);
4632 isl_int_set_si(aff
->v
->el
[1 + o_out
+ div
], 0);
4633 isl_int_abs(aff
->v
->el
[0], bmap
->eq
[eq
][o_out
+ pos
]);
4634 aff
= subtract_initial(aff
, ma
, pos
, bmap
->eq
[eq
] + o_out
,
4635 bmap
->eq
[eq
][o_out
+ pos
]);
4637 shift
= isl_aff_alloc(isl_local_space_copy(ls
));
4640 isl_seq_cpy(shift
->v
->el
+ 1, bmap
->ineq
[ineq
], o_out
);
4641 isl_seq_cpy(shift
->v
->el
+ 1 + o_out
,
4642 bmap
->ineq
[ineq
] + o_out
+ n_out
, n_div
);
4643 isl_int_set_si(shift
->v
->el
[0], 1);
4644 shift
= subtract_initial(shift
, ma
, pos
,
4645 bmap
->ineq
[ineq
] + o_out
, ctx
->negone
);
4646 aff
= isl_aff_add(aff
, isl_aff_copy(shift
));
4647 mod
= isl_val_int_from_isl_int(ctx
,
4648 bmap
->eq
[eq
][o_out
+ n_out
+ div
]);
4649 mod
= isl_val_abs(mod
);
4650 aff
= isl_aff_mod_val(aff
, mod
);
4651 aff
= isl_aff_sub(aff
, shift
);
4654 isl_local_space_free(ls
);
4657 isl_local_space_free(ls
);
4662 /* Given a basic map with output dimensions defined
4663 * in terms of the parameters input dimensions and earlier
4664 * output dimensions using an equality (and possibly a pair on inequalities),
4665 * extract an isl_aff that expresses output dimension "pos" in terms
4666 * of the parameters and input dimensions.
4667 * Note that this expression may involve integer divisions defined
4668 * in terms of parameters and input dimensions.
4669 * "ma" contains the expressions corresponding to earlier output dimensions.
4671 * This function shares some similarities with
4672 * isl_basic_map_has_defining_equality and isl_constraint_get_bound.
4674 static __isl_give isl_aff
*extract_isl_aff_from_basic_map(
4675 __isl_keep isl_basic_map
*bmap
, int pos
, __isl_keep isl_multi_aff
*ma
)
4682 eq
= isl_basic_map_output_defining_equality(bmap
, pos
, &div
, &ineq
);
4683 if (eq
>= bmap
->n_eq
)
4684 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
4685 "unable to find suitable equality", return NULL
);
4686 aff
= extract_aff_from_equality(bmap
, pos
, eq
, div
, ineq
, ma
);
4688 aff
= isl_aff_remove_unused_divs(aff
);
4692 /* Given a basic map where each output dimension is defined
4693 * in terms of the parameters and input dimensions using an equality,
4694 * extract an isl_multi_aff that expresses the output dimensions in terms
4695 * of the parameters and input dimensions.
4697 static __isl_give isl_multi_aff
*extract_isl_multi_aff_from_basic_map(
4698 __isl_take isl_basic_map
*bmap
)
4707 ma
= isl_multi_aff_alloc(isl_basic_map_get_space(bmap
));
4708 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4710 ma
= isl_multi_aff_free(ma
);
4712 for (i
= 0; i
< n_out
; ++i
) {
4715 aff
= extract_isl_aff_from_basic_map(bmap
, i
, ma
);
4716 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
4719 isl_basic_map_free(bmap
);
4724 /* Given a basic set where each set dimension is defined
4725 * in terms of the parameters using an equality,
4726 * extract an isl_multi_aff that expresses the set dimensions in terms
4727 * of the parameters.
4729 __isl_give isl_multi_aff
*isl_multi_aff_from_basic_set_equalities(
4730 __isl_take isl_basic_set
*bset
)
4732 return extract_isl_multi_aff_from_basic_map(bset
);
4735 /* Create an isl_pw_multi_aff that is equivalent to
4736 * isl_map_intersect_domain(isl_map_from_basic_map(bmap), domain).
4737 * The given basic map is such that each output dimension is defined
4738 * in terms of the parameters and input dimensions using an equality.
4740 * Since some applications expect the result of isl_pw_multi_aff_from_map
4741 * to only contain integer affine expressions, we compute the floor
4742 * of the expression before returning.
4744 * Remove all constraints involving local variables without
4745 * an explicit representation (resulting in the removal of those
4746 * local variables) prior to the actual extraction to ensure
4747 * that the local spaces in which the resulting affine expressions
4748 * are created do not contain any unknown local variables.
4749 * Removing such constraints is safe because constraints involving
4750 * unknown local variables are not used to determine whether
4751 * a basic map is obviously single-valued.
4753 static __isl_give isl_pw_multi_aff
*plain_pw_multi_aff_from_map(
4754 __isl_take isl_set
*domain
, __isl_take isl_basic_map
*bmap
)
4758 bmap
= isl_basic_map_drop_constraint_involving_unknown_divs(bmap
);
4759 ma
= extract_isl_multi_aff_from_basic_map(bmap
);
4760 ma
= isl_multi_aff_floor(ma
);
4761 return isl_pw_multi_aff_alloc(domain
, ma
);
4764 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4765 * This obviously only works if the input "map" is single-valued.
4766 * If so, we compute the lexicographic minimum of the image in the form
4767 * of an isl_pw_multi_aff. Since the image is unique, it is equal
4768 * to its lexicographic minimum.
4769 * If the input is not single-valued, we produce an error.
4771 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_base(
4772 __isl_take isl_map
*map
)
4776 isl_pw_multi_aff
*pma
;
4778 sv
= isl_map_is_single_valued(map
);
4782 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
4783 "map is not single-valued", goto error
);
4784 map
= isl_map_make_disjoint(map
);
4788 pma
= isl_pw_multi_aff_empty(isl_map_get_space(map
));
4790 for (i
= 0; i
< map
->n
; ++i
) {
4791 isl_pw_multi_aff
*pma_i
;
4792 isl_basic_map
*bmap
;
4793 bmap
= isl_basic_map_copy(map
->p
[i
]);
4794 pma_i
= isl_basic_map_lexmin_pw_multi_aff(bmap
);
4795 pma
= isl_pw_multi_aff_add_disjoint(pma
, pma_i
);
4805 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
4806 * taking into account that the output dimension at position "d"
4807 * can be represented as
4809 * x = floor((e(...) + c1) / m)
4811 * given that constraint "i" is of the form
4813 * e(...) + c1 - m x >= 0
4816 * Let "map" be of the form
4820 * We construct a mapping
4822 * A -> [A -> x = floor(...)]
4824 * apply that to the map, obtaining
4826 * [A -> x = floor(...)] -> B
4828 * and equate dimension "d" to x.
4829 * We then compute a isl_pw_multi_aff representation of the resulting map
4830 * and plug in the mapping above.
4832 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_div(
4833 __isl_take isl_map
*map
, __isl_take isl_basic_map
*hull
, int d
, int i
)
4836 isl_space
*space
= NULL
;
4837 isl_local_space
*ls
;
4845 isl_pw_multi_aff
*pma
;
4848 is_set
= isl_map_is_set(map
);
4852 offset
= isl_basic_map_offset(hull
, isl_dim_out
);
4853 ctx
= isl_map_get_ctx(map
);
4854 space
= isl_space_domain(isl_map_get_space(map
));
4855 n_in
= isl_space_dim(space
, isl_dim_set
);
4856 n
= isl_space_dim(space
, isl_dim_all
);
4857 if (n_in
< 0 || n
< 0)
4860 v
= isl_vec_alloc(ctx
, 1 + 1 + n
);
4862 isl_int_neg(v
->el
[0], hull
->ineq
[i
][offset
+ d
]);
4863 isl_seq_cpy(v
->el
+ 1, hull
->ineq
[i
], 1 + n
);
4865 isl_basic_map_free(hull
);
4867 ls
= isl_local_space_from_space(isl_space_copy(space
));
4868 aff
= isl_aff_alloc_vec(ls
, v
);
4869 aff
= isl_aff_floor(aff
);
4871 isl_space_free(space
);
4872 ma
= isl_multi_aff_from_aff(aff
);
4874 ma
= isl_multi_aff_identity(isl_space_map_from_set(space
));
4875 ma
= isl_multi_aff_range_product(ma
,
4876 isl_multi_aff_from_aff(aff
));
4879 insert
= isl_map_from_multi_aff_internal(isl_multi_aff_copy(ma
));
4880 map
= isl_map_apply_domain(map
, insert
);
4881 map
= isl_map_equate(map
, isl_dim_in
, n_in
, isl_dim_out
, d
);
4882 pma
= isl_pw_multi_aff_from_map(map
);
4883 pma
= isl_pw_multi_aff_pullback_multi_aff(pma
, ma
);
4887 isl_space_free(space
);
4889 isl_basic_map_free(hull
);
4893 /* Is constraint "c" of the form
4895 * e(...) + c1 - m x >= 0
4899 * -e(...) + c2 + m x >= 0
4901 * where m > 1 and e only depends on parameters and input dimemnsions?
4903 * "offset" is the offset of the output dimensions
4904 * "pos" is the position of output dimension x.
4906 static int is_potential_div_constraint(isl_int
*c
, int offset
, int d
, int total
)
4908 if (isl_int_is_zero(c
[offset
+ d
]))
4910 if (isl_int_is_one(c
[offset
+ d
]))
4912 if (isl_int_is_negone(c
[offset
+ d
]))
4914 if (isl_seq_first_non_zero(c
+ offset
, d
) != -1)
4916 if (isl_seq_first_non_zero(c
+ offset
+ d
+ 1,
4917 total
- (offset
+ d
+ 1)) != -1)
4922 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4924 * As a special case, we first check if there is any pair of constraints,
4925 * shared by all the basic maps in "map" that force a given dimension
4926 * to be equal to the floor of some affine combination of the input dimensions.
4928 * In particular, if we can find two constraints
4930 * e(...) + c1 - m x >= 0 i.e., m x <= e(...) + c1
4934 * -e(...) + c2 + m x >= 0 i.e., m x >= e(...) - c2
4936 * where m > 1 and e only depends on parameters and input dimemnsions,
4939 * c1 + c2 < m i.e., -c2 >= c1 - (m - 1)
4941 * then we know that we can take
4943 * x = floor((e(...) + c1) / m)
4945 * without having to perform any computation.
4947 * Note that we know that
4951 * If c1 + c2 were 0, then we would have detected an equality during
4952 * simplification. If c1 + c2 were negative, then we would have detected
4955 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_check_div(
4956 __isl_take isl_map
*map
)
4964 isl_basic_map
*hull
;
4966 hull
= isl_map_unshifted_simple_hull(isl_map_copy(map
));
4967 dim
= isl_map_dim(map
, isl_dim_out
);
4968 total
= isl_basic_map_dim(hull
, isl_dim_all
);
4969 if (dim
< 0 || total
< 0)
4973 offset
= isl_basic_map_offset(hull
, isl_dim_out
);
4975 for (d
= 0; d
< dim
; ++d
) {
4976 for (i
= 0; i
< n
; ++i
) {
4977 if (!is_potential_div_constraint(hull
->ineq
[i
],
4978 offset
, d
, 1 + total
))
4980 for (j
= i
+ 1; j
< n
; ++j
) {
4981 if (!isl_seq_is_neg(hull
->ineq
[i
] + 1,
4982 hull
->ineq
[j
] + 1, total
))
4984 isl_int_add(sum
, hull
->ineq
[i
][0],
4986 if (isl_int_abs_lt(sum
,
4987 hull
->ineq
[i
][offset
+ d
]))
4994 if (isl_int_is_pos(hull
->ineq
[j
][offset
+ d
]))
4996 return pw_multi_aff_from_map_div(map
, hull
, d
, j
);
5000 isl_basic_map_free(hull
);
5001 return pw_multi_aff_from_map_base(map
);
5004 isl_basic_map_free(hull
);
5008 /* Given an affine expression
5010 * [A -> B] -> f(A,B)
5012 * construct an isl_multi_aff
5016 * such that dimension "d" in B' is set to "aff" and the remaining
5017 * dimensions are set equal to the corresponding dimensions in B.
5018 * "n_in" is the dimension of the space A.
5019 * "n_out" is the dimension of the space B.
5021 * If "is_set" is set, then the affine expression is of the form
5025 * and we construct an isl_multi_aff
5029 static __isl_give isl_multi_aff
*range_map(__isl_take isl_aff
*aff
, int d
,
5030 unsigned n_in
, unsigned n_out
, int is_set
)
5034 isl_space
*space
, *space2
;
5035 isl_local_space
*ls
;
5037 space
= isl_aff_get_domain_space(aff
);
5038 ls
= isl_local_space_from_space(isl_space_copy(space
));
5039 space2
= isl_space_copy(space
);
5041 space2
= isl_space_range(isl_space_unwrap(space2
));
5042 space
= isl_space_map_from_domain_and_range(space
, space2
);
5043 ma
= isl_multi_aff_alloc(space
);
5044 ma
= isl_multi_aff_set_aff(ma
, d
, aff
);
5046 for (i
= 0; i
< n_out
; ++i
) {
5049 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
5050 isl_dim_set
, n_in
+ i
);
5051 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
5054 isl_local_space_free(ls
);
5059 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
5060 * taking into account that the dimension at position "d" can be written as
5062 * x = m a + f(..) (1)
5064 * where m is equal to "gcd".
5065 * "i" is the index of the equality in "hull" that defines f(..).
5066 * In particular, the equality is of the form
5068 * f(..) - x + m g(existentials) = 0
5072 * -f(..) + x + m g(existentials) = 0
5074 * We basically plug (1) into "map", resulting in a map with "a"
5075 * in the range instead of "x". The corresponding isl_pw_multi_aff
5076 * defining "a" is then plugged back into (1) to obtain a definition for "x".
5078 * Specifically, given the input map
5082 * We first wrap it into a set
5086 * and define (1) on top of the corresponding space, resulting in "aff".
5087 * We use this to create an isl_multi_aff that maps the output position "d"
5088 * from "a" to "x", leaving all other (intput and output) dimensions unchanged.
5089 * We plug this into the wrapped map, unwrap the result and compute the
5090 * corresponding isl_pw_multi_aff.
5091 * The result is an expression
5099 * so that we can plug that into "aff", after extending the latter to
5105 * If "map" is actually a set, then there is no "A" space, meaning
5106 * that we do not need to perform any wrapping, and that the result
5107 * of the recursive call is of the form
5111 * which is plugged into a mapping of the form
5115 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_stride(
5116 __isl_take isl_map
*map
, __isl_take isl_basic_map
*hull
, int d
, int i
,
5121 isl_local_space
*ls
;
5124 isl_pw_multi_aff
*pma
, *id
;
5130 is_set
= isl_map_is_set(map
);
5134 n_in
= isl_basic_map_dim(hull
, isl_dim_in
);
5135 n_out
= isl_basic_map_dim(hull
, isl_dim_out
);
5136 if (n_in
< 0 || n_out
< 0)
5138 o_out
= isl_basic_map_offset(hull
, isl_dim_out
);
5143 set
= isl_map_wrap(map
);
5144 space
= isl_space_map_from_set(isl_set_get_space(set
));
5145 ma
= isl_multi_aff_identity(space
);
5146 ls
= isl_local_space_from_space(isl_set_get_space(set
));
5147 aff
= isl_aff_alloc(ls
);
5149 isl_int_set_si(aff
->v
->el
[0], 1);
5150 if (isl_int_is_one(hull
->eq
[i
][o_out
+ d
]))
5151 isl_seq_neg(aff
->v
->el
+ 1, hull
->eq
[i
],
5154 isl_seq_cpy(aff
->v
->el
+ 1, hull
->eq
[i
],
5156 isl_int_set(aff
->v
->el
[1 + o_out
+ d
], gcd
);
5158 ma
= isl_multi_aff_set_aff(ma
, n_in
+ d
, isl_aff_copy(aff
));
5159 set
= isl_set_preimage_multi_aff(set
, ma
);
5161 ma
= range_map(aff
, d
, n_in
, n_out
, is_set
);
5166 map
= isl_set_unwrap(set
);
5167 pma
= isl_pw_multi_aff_from_map(map
);
5170 space
= isl_pw_multi_aff_get_domain_space(pma
);
5171 space
= isl_space_map_from_set(space
);
5172 id
= isl_pw_multi_aff_identity(space
);
5173 pma
= isl_pw_multi_aff_range_product(id
, pma
);
5175 id
= isl_pw_multi_aff_from_multi_aff(ma
);
5176 pma
= isl_pw_multi_aff_pullback_pw_multi_aff(id
, pma
);
5178 isl_basic_map_free(hull
);
5182 isl_basic_map_free(hull
);
5186 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5187 * "hull" contains the equalities valid for "map".
5189 * Check if any of the output dimensions is "strided".
5190 * That is, we check if it can be written as
5194 * with m greater than 1, a some combination of existentially quantified
5195 * variables and f an expression in the parameters and input dimensions.
5196 * If so, we remove the stride in pw_multi_aff_from_map_stride.
5198 * Otherwise, we continue with pw_multi_aff_from_map_check_div for a further
5201 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_check_strides(
5202 __isl_take isl_map
*map
, __isl_take isl_basic_map
*hull
)
5211 n_div
= isl_basic_map_dim(hull
, isl_dim_div
);
5212 n_out
= isl_basic_map_dim(hull
, isl_dim_out
);
5213 if (n_div
< 0 || n_out
< 0)
5217 isl_basic_map_free(hull
);
5218 return pw_multi_aff_from_map_check_div(map
);
5223 o_div
= isl_basic_map_offset(hull
, isl_dim_div
);
5224 o_out
= isl_basic_map_offset(hull
, isl_dim_out
);
5226 for (i
= 0; i
< n_out
; ++i
) {
5227 for (j
= 0; j
< hull
->n_eq
; ++j
) {
5228 isl_int
*eq
= hull
->eq
[j
];
5229 isl_pw_multi_aff
*res
;
5231 if (!isl_int_is_one(eq
[o_out
+ i
]) &&
5232 !isl_int_is_negone(eq
[o_out
+ i
]))
5234 if (isl_seq_first_non_zero(eq
+ o_out
, i
) != -1)
5236 if (isl_seq_first_non_zero(eq
+ o_out
+ i
+ 1,
5237 n_out
- (i
+ 1)) != -1)
5239 isl_seq_gcd(eq
+ o_div
, n_div
, &gcd
);
5240 if (isl_int_is_zero(gcd
))
5242 if (isl_int_is_one(gcd
))
5245 res
= pw_multi_aff_from_map_stride(map
, hull
,
5253 isl_basic_map_free(hull
);
5254 return pw_multi_aff_from_map_check_div(map
);
5257 isl_basic_map_free(hull
);
5261 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5263 * As a special case, we first check if all output dimensions are uniquely
5264 * defined in terms of the parameters and input dimensions over the entire
5265 * domain. If so, we extract the desired isl_pw_multi_aff directly
5266 * from the affine hull of "map" and its domain.
5268 * Otherwise, continue with pw_multi_aff_from_map_check_strides for more
5271 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_map(__isl_take isl_map
*map
)
5275 isl_basic_map
*hull
;
5277 n
= isl_map_n_basic_map(map
);
5282 hull
= isl_map_unshifted_simple_hull(isl_map_copy(map
));
5283 hull
= isl_basic_map_plain_affine_hull(hull
);
5284 sv
= isl_basic_map_plain_is_single_valued(hull
);
5286 return plain_pw_multi_aff_from_map(isl_map_domain(map
),
5288 isl_basic_map_free(hull
);
5290 map
= isl_map_detect_equalities(map
);
5291 hull
= isl_map_unshifted_simple_hull(isl_map_copy(map
));
5292 sv
= isl_basic_map_plain_is_single_valued(hull
);
5294 return plain_pw_multi_aff_from_map(isl_map_domain(map
), hull
);
5296 return pw_multi_aff_from_map_check_strides(map
, hull
);
5297 isl_basic_map_free(hull
);
5303 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_set(__isl_take isl_set
*set
)
5305 return isl_pw_multi_aff_from_map(set
);
5308 /* Convert "map" into an isl_pw_multi_aff (if possible) and
5311 static isl_stat
pw_multi_aff_from_map(__isl_take isl_map
*map
, void *user
)
5313 isl_union_pw_multi_aff
**upma
= user
;
5314 isl_pw_multi_aff
*pma
;
5316 pma
= isl_pw_multi_aff_from_map(map
);
5317 *upma
= isl_union_pw_multi_aff_add_pw_multi_aff(*upma
, pma
);
5319 return *upma
? isl_stat_ok
: isl_stat_error
;
5322 /* Create an isl_union_pw_multi_aff with the given isl_aff on a universe
5325 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_aff(
5326 __isl_take isl_aff
*aff
)
5329 isl_pw_multi_aff
*pma
;
5331 ma
= isl_multi_aff_from_aff(aff
);
5332 pma
= isl_pw_multi_aff_from_multi_aff(ma
);
5333 return isl_union_pw_multi_aff_from_pw_multi_aff(pma
);
5336 /* Try and create an isl_union_pw_multi_aff that is equivalent
5337 * to the given isl_union_map.
5338 * The isl_union_map is required to be single-valued in each space.
5339 * Otherwise, an error is produced.
5341 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_union_map(
5342 __isl_take isl_union_map
*umap
)
5345 isl_union_pw_multi_aff
*upma
;
5347 space
= isl_union_map_get_space(umap
);
5348 upma
= isl_union_pw_multi_aff_empty(space
);
5349 if (isl_union_map_foreach_map(umap
, &pw_multi_aff_from_map
, &upma
) < 0)
5350 upma
= isl_union_pw_multi_aff_free(upma
);
5351 isl_union_map_free(umap
);
5356 /* Try and create an isl_union_pw_multi_aff that is equivalent
5357 * to the given isl_union_set.
5358 * The isl_union_set is required to be a singleton in each space.
5359 * Otherwise, an error is produced.
5361 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_union_set(
5362 __isl_take isl_union_set
*uset
)
5364 return isl_union_pw_multi_aff_from_union_map(uset
);
5367 /* Return the piecewise affine expression "set ? 1 : 0".
5369 __isl_give isl_pw_aff
*isl_set_indicator_function(__isl_take isl_set
*set
)
5372 isl_space
*space
= isl_set_get_space(set
);
5373 isl_local_space
*ls
= isl_local_space_from_space(space
);
5374 isl_aff
*zero
= isl_aff_zero_on_domain(isl_local_space_copy(ls
));
5375 isl_aff
*one
= isl_aff_zero_on_domain(ls
);
5377 one
= isl_aff_add_constant_si(one
, 1);
5378 pa
= isl_pw_aff_alloc(isl_set_copy(set
), one
);
5379 set
= isl_set_complement(set
);
5380 pa
= isl_pw_aff_add_disjoint(pa
, isl_pw_aff_alloc(set
, zero
));
5385 /* Plug in "subs" for dimension "type", "pos" of "aff".
5387 * Let i be the dimension to replace and let "subs" be of the form
5391 * and "aff" of the form
5397 * (a f + d g')/(m d)
5399 * where g' is the result of plugging in "subs" in each of the integer
5402 __isl_give isl_aff
*isl_aff_substitute(__isl_take isl_aff
*aff
,
5403 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
5409 aff
= isl_aff_cow(aff
);
5411 return isl_aff_free(aff
);
5413 ctx
= isl_aff_get_ctx(aff
);
5414 if (!isl_space_is_equal(aff
->ls
->dim
, subs
->ls
->dim
))
5415 isl_die(ctx
, isl_error_invalid
,
5416 "spaces don't match", return isl_aff_free(aff
));
5417 n_div
= isl_local_space_dim(subs
->ls
, isl_dim_div
);
5419 return isl_aff_free(aff
);
5421 isl_die(ctx
, isl_error_unsupported
,
5422 "cannot handle divs yet", return isl_aff_free(aff
));
5424 aff
->ls
= isl_local_space_substitute(aff
->ls
, type
, pos
, subs
);
5426 return isl_aff_free(aff
);
5428 aff
->v
= isl_vec_cow(aff
->v
);
5430 return isl_aff_free(aff
);
5432 pos
+= isl_local_space_offset(aff
->ls
, type
);
5435 isl_seq_substitute(aff
->v
->el
, pos
, subs
->v
->el
,
5436 aff
->v
->size
, subs
->v
->size
, v
);
5442 /* Plug in "subs" for dimension "type", "pos" in each of the affine
5443 * expressions in "maff".
5445 __isl_give isl_multi_aff
*isl_multi_aff_substitute(
5446 __isl_take isl_multi_aff
*maff
, enum isl_dim_type type
, unsigned pos
,
5447 __isl_keep isl_aff
*subs
)
5451 maff
= isl_multi_aff_cow(maff
);
5453 return isl_multi_aff_free(maff
);
5455 if (type
== isl_dim_in
)
5458 for (i
= 0; i
< maff
->n
; ++i
) {
5459 maff
->u
.p
[i
] = isl_aff_substitute(maff
->u
.p
[i
],
5462 return isl_multi_aff_free(maff
);
5468 /* Plug in "subs" for dimension "type", "pos" of "pma".
5470 * pma is of the form
5474 * while subs is of the form
5476 * v' = B_j(v) -> S_j
5478 * Each pair i,j such that C_ij = A_i \cap B_i is non-empty
5479 * has a contribution in the result, in particular
5481 * C_ij(S_j) -> M_i(S_j)
5483 * Note that plugging in S_j in C_ij may also result in an empty set
5484 * and this contribution should simply be discarded.
5486 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_substitute(
5487 __isl_take isl_pw_multi_aff
*pma
, enum isl_dim_type type
, unsigned pos
,
5488 __isl_keep isl_pw_aff
*subs
)
5491 isl_pw_multi_aff
*res
;
5494 return isl_pw_multi_aff_free(pma
);
5496 n
= pma
->n
* subs
->n
;
5497 res
= isl_pw_multi_aff_alloc_size(isl_space_copy(pma
->dim
), n
);
5499 for (i
= 0; i
< pma
->n
; ++i
) {
5500 for (j
= 0; j
< subs
->n
; ++j
) {
5502 isl_multi_aff
*res_ij
;
5505 common
= isl_set_intersect(
5506 isl_set_copy(pma
->p
[i
].set
),
5507 isl_set_copy(subs
->p
[j
].set
));
5508 common
= isl_set_substitute(common
,
5509 type
, pos
, subs
->p
[j
].aff
);
5510 empty
= isl_set_plain_is_empty(common
);
5511 if (empty
< 0 || empty
) {
5512 isl_set_free(common
);
5518 res_ij
= isl_multi_aff_substitute(
5519 isl_multi_aff_copy(pma
->p
[i
].maff
),
5520 type
, pos
, subs
->p
[j
].aff
);
5522 res
= isl_pw_multi_aff_add_piece(res
, common
, res_ij
);
5526 isl_pw_multi_aff_free(pma
);
5529 isl_pw_multi_aff_free(pma
);
5530 isl_pw_multi_aff_free(res
);
5534 /* Compute the preimage of a range of dimensions in the affine expression "src"
5535 * under "ma" and put the result in "dst". The number of dimensions in "src"
5536 * that precede the range is given by "n_before". The number of dimensions
5537 * in the range is given by the number of output dimensions of "ma".
5538 * The number of dimensions that follow the range is given by "n_after".
5539 * If "has_denom" is set (to one),
5540 * then "src" and "dst" have an extra initial denominator.
5541 * "n_div_ma" is the number of existentials in "ma"
5542 * "n_div_bset" is the number of existentials in "src"
5543 * The resulting "dst" (which is assumed to have been allocated by
5544 * the caller) contains coefficients for both sets of existentials,
5545 * first those in "ma" and then those in "src".
5546 * f, c1, c2 and g are temporary objects that have been initialized
5549 * Let src represent the expression
5551 * (a(p) + f_u u + b v + f_w w + c(divs))/d
5553 * and let ma represent the expressions
5555 * v_i = (r_i(p) + s_i(y) + t_i(divs'))/m_i
5557 * We start out with the following expression for dst:
5559 * (a(p) + f_u u + 0 y + f_w w + 0 divs' + c(divs) + f \sum_i b_i v_i)/d
5561 * with the multiplication factor f initially equal to 1
5562 * and f \sum_i b_i v_i kept separately.
5563 * For each x_i that we substitute, we multiply the numerator
5564 * (and denominator) of dst by c_1 = m_i and add the numerator
5565 * of the x_i expression multiplied by c_2 = f b_i,
5566 * after removing the common factors of c_1 and c_2.
5567 * The multiplication factor f also needs to be multiplied by c_1
5568 * for the next x_j, j > i.
5570 isl_stat
isl_seq_preimage(isl_int
*dst
, isl_int
*src
,
5571 __isl_keep isl_multi_aff
*ma
, int n_before
, int n_after
,
5572 int n_div_ma
, int n_div_bmap
,
5573 isl_int f
, isl_int c1
, isl_int c2
, isl_int g
, int has_denom
)
5576 isl_size n_param
, n_in
, n_out
;
5579 n_param
= isl_multi_aff_dim(ma
, isl_dim_param
);
5580 n_in
= isl_multi_aff_dim(ma
, isl_dim_in
);
5581 n_out
= isl_multi_aff_dim(ma
, isl_dim_out
);
5582 if (n_param
< 0 || n_in
< 0 || n_out
< 0)
5583 return isl_stat_error
;
5585 isl_seq_cpy(dst
, src
, has_denom
+ 1 + n_param
+ n_before
);
5586 o_dst
= o_src
= has_denom
+ 1 + n_param
+ n_before
;
5587 isl_seq_clr(dst
+ o_dst
, n_in
);
5590 isl_seq_cpy(dst
+ o_dst
, src
+ o_src
, n_after
);
5593 isl_seq_clr(dst
+ o_dst
, n_div_ma
);
5595 isl_seq_cpy(dst
+ o_dst
, src
+ o_src
, n_div_bmap
);
5597 isl_int_set_si(f
, 1);
5599 for (i
= 0; i
< n_out
; ++i
) {
5600 int offset
= has_denom
+ 1 + n_param
+ n_before
+ i
;
5602 if (isl_int_is_zero(src
[offset
]))
5604 isl_int_set(c1
, ma
->u
.p
[i
]->v
->el
[0]);
5605 isl_int_mul(c2
, f
, src
[offset
]);
5606 isl_int_gcd(g
, c1
, c2
);
5607 isl_int_divexact(c1
, c1
, g
);
5608 isl_int_divexact(c2
, c2
, g
);
5610 isl_int_mul(f
, f
, c1
);
5613 isl_seq_combine(dst
+ o_dst
, c1
, dst
+ o_dst
,
5614 c2
, ma
->u
.p
[i
]->v
->el
+ o_src
, 1 + n_param
);
5615 o_dst
+= 1 + n_param
;
5616 o_src
+= 1 + n_param
;
5617 isl_seq_scale(dst
+ o_dst
, dst
+ o_dst
, c1
, n_before
);
5619 isl_seq_combine(dst
+ o_dst
, c1
, dst
+ o_dst
,
5620 c2
, ma
->u
.p
[i
]->v
->el
+ o_src
, n_in
);
5623 isl_seq_scale(dst
+ o_dst
, dst
+ o_dst
, c1
, n_after
);
5625 isl_seq_combine(dst
+ o_dst
, c1
, dst
+ o_dst
,
5626 c2
, ma
->u
.p
[i
]->v
->el
+ o_src
, n_div_ma
);
5629 isl_seq_scale(dst
+ o_dst
, dst
+ o_dst
, c1
, n_div_bmap
);
5631 isl_int_mul(dst
[0], dst
[0], c1
);
5637 /* Compute the pullback of "aff" by the function represented by "ma".
5638 * In other words, plug in "ma" in "aff". The result is an affine expression
5639 * defined over the domain space of "ma".
5641 * If "aff" is represented by
5643 * (a(p) + b x + c(divs))/d
5645 * and ma is represented by
5647 * x = D(p) + F(y) + G(divs')
5649 * then the result is
5651 * (a(p) + b D(p) + b F(y) + b G(divs') + c(divs))/d
5653 * The divs in the local space of the input are similarly adjusted
5654 * through a call to isl_local_space_preimage_multi_aff.
5656 __isl_give isl_aff
*isl_aff_pullback_multi_aff(__isl_take isl_aff
*aff
,
5657 __isl_take isl_multi_aff
*ma
)
5659 isl_aff
*res
= NULL
;
5660 isl_local_space
*ls
;
5661 isl_size n_div_aff
, n_div_ma
;
5662 isl_int f
, c1
, c2
, g
;
5664 ma
= isl_multi_aff_align_divs(ma
);
5668 n_div_aff
= isl_aff_dim(aff
, isl_dim_div
);
5669 n_div_ma
= ma
->n
? isl_aff_dim(ma
->u
.p
[0], isl_dim_div
) : 0;
5670 if (n_div_aff
< 0 || n_div_ma
< 0)
5673 ls
= isl_aff_get_domain_local_space(aff
);
5674 ls
= isl_local_space_preimage_multi_aff(ls
, isl_multi_aff_copy(ma
));
5675 res
= isl_aff_alloc(ls
);
5684 if (isl_seq_preimage(res
->v
->el
, aff
->v
->el
, ma
, 0, 0,
5685 n_div_ma
, n_div_aff
, f
, c1
, c2
, g
, 1) < 0)
5686 res
= isl_aff_free(res
);
5694 isl_multi_aff_free(ma
);
5695 res
= isl_aff_normalize(res
);
5699 isl_multi_aff_free(ma
);
5704 /* Compute the pullback of "aff1" by the function represented by "aff2".
5705 * In other words, plug in "aff2" in "aff1". The result is an affine expression
5706 * defined over the domain space of "aff1".
5708 * The domain of "aff1" should match the range of "aff2", which means
5709 * that it should be single-dimensional.
5711 __isl_give isl_aff
*isl_aff_pullback_aff(__isl_take isl_aff
*aff1
,
5712 __isl_take isl_aff
*aff2
)
5716 ma
= isl_multi_aff_from_aff(aff2
);
5717 return isl_aff_pullback_multi_aff(aff1
, ma
);
5720 /* Compute the pullback of "ma1" by the function represented by "ma2".
5721 * In other words, plug in "ma2" in "ma1".
5723 * The parameters of "ma1" and "ma2" are assumed to have been aligned.
5725 static __isl_give isl_multi_aff
*isl_multi_aff_pullback_multi_aff_aligned(
5726 __isl_take isl_multi_aff
*ma1
, __isl_take isl_multi_aff
*ma2
)
5729 isl_space
*space
= NULL
;
5731 ma2
= isl_multi_aff_align_divs(ma2
);
5732 ma1
= isl_multi_aff_cow(ma1
);
5736 space
= isl_space_join(isl_multi_aff_get_space(ma2
),
5737 isl_multi_aff_get_space(ma1
));
5739 for (i
= 0; i
< ma1
->n
; ++i
) {
5740 ma1
->u
.p
[i
] = isl_aff_pullback_multi_aff(ma1
->u
.p
[i
],
5741 isl_multi_aff_copy(ma2
));
5746 ma1
= isl_multi_aff_reset_space(ma1
, space
);
5747 isl_multi_aff_free(ma2
);
5750 isl_space_free(space
);
5751 isl_multi_aff_free(ma2
);
5752 isl_multi_aff_free(ma1
);
5756 /* Compute the pullback of "ma1" by the function represented by "ma2".
5757 * In other words, plug in "ma2" in "ma1".
5759 __isl_give isl_multi_aff
*isl_multi_aff_pullback_multi_aff(
5760 __isl_take isl_multi_aff
*ma1
, __isl_take isl_multi_aff
*ma2
)
5762 return isl_multi_aff_align_params_multi_multi_and(ma1
, ma2
,
5763 &isl_multi_aff_pullback_multi_aff_aligned
);
5766 /* Extend the local space of "dst" to include the divs
5767 * in the local space of "src".
5769 * If "src" does not have any divs or if the local spaces of "dst" and
5770 * "src" are the same, then no extension is required.
5772 __isl_give isl_aff
*isl_aff_align_divs(__isl_take isl_aff
*dst
,
5773 __isl_keep isl_aff
*src
)
5776 isl_size src_n_div
, dst_n_div
;
5783 return isl_aff_free(dst
);
5785 ctx
= isl_aff_get_ctx(src
);
5786 equal
= isl_local_space_has_equal_space(src
->ls
, dst
->ls
);
5788 return isl_aff_free(dst
);
5790 isl_die(ctx
, isl_error_invalid
,
5791 "spaces don't match", goto error
);
5793 src_n_div
= isl_local_space_dim(src
->ls
, isl_dim_div
);
5794 dst_n_div
= isl_local_space_dim(dst
->ls
, isl_dim_div
);
5797 equal
= isl_local_space_is_equal(src
->ls
, dst
->ls
);
5798 if (equal
< 0 || src_n_div
< 0 || dst_n_div
< 0)
5799 return isl_aff_free(dst
);
5803 exp1
= isl_alloc_array(ctx
, int, src_n_div
);
5804 exp2
= isl_alloc_array(ctx
, int, dst_n_div
);
5805 if (!exp1
|| (dst_n_div
&& !exp2
))
5808 div
= isl_merge_divs(src
->ls
->div
, dst
->ls
->div
, exp1
, exp2
);
5809 dst
= isl_aff_expand_divs(dst
, div
, exp2
);
5817 return isl_aff_free(dst
);
5820 /* Adjust the local spaces of the affine expressions in "maff"
5821 * such that they all have the save divs.
5823 __isl_give isl_multi_aff
*isl_multi_aff_align_divs(
5824 __isl_take isl_multi_aff
*maff
)
5832 maff
= isl_multi_aff_cow(maff
);
5836 for (i
= 1; i
< maff
->n
; ++i
)
5837 maff
->u
.p
[0] = isl_aff_align_divs(maff
->u
.p
[0], maff
->u
.p
[i
]);
5838 for (i
= 1; i
< maff
->n
; ++i
) {
5839 maff
->u
.p
[i
] = isl_aff_align_divs(maff
->u
.p
[i
], maff
->u
.p
[0]);
5841 return isl_multi_aff_free(maff
);
5847 __isl_give isl_aff
*isl_aff_lift(__isl_take isl_aff
*aff
)
5849 aff
= isl_aff_cow(aff
);
5853 aff
->ls
= isl_local_space_lift(aff
->ls
);
5855 return isl_aff_free(aff
);
5860 /* Lift "maff" to a space with extra dimensions such that the result
5861 * has no more existentially quantified variables.
5862 * If "ls" is not NULL, then *ls is assigned the local space that lies
5863 * at the basis of the lifting applied to "maff".
5865 __isl_give isl_multi_aff
*isl_multi_aff_lift(__isl_take isl_multi_aff
*maff
,
5866 __isl_give isl_local_space
**ls
)
5880 isl_space
*space
= isl_multi_aff_get_domain_space(maff
);
5881 *ls
= isl_local_space_from_space(space
);
5883 return isl_multi_aff_free(maff
);
5888 maff
= isl_multi_aff_cow(maff
);
5889 maff
= isl_multi_aff_align_divs(maff
);
5893 n_div
= isl_aff_dim(maff
->u
.p
[0], isl_dim_div
);
5895 return isl_multi_aff_free(maff
);
5896 space
= isl_multi_aff_get_space(maff
);
5897 space
= isl_space_lift(isl_space_domain(space
), n_div
);
5898 space
= isl_space_extend_domain_with_range(space
,
5899 isl_multi_aff_get_space(maff
));
5901 return isl_multi_aff_free(maff
);
5902 isl_space_free(maff
->space
);
5903 maff
->space
= space
;
5906 *ls
= isl_aff_get_domain_local_space(maff
->u
.p
[0]);
5908 return isl_multi_aff_free(maff
);
5911 for (i
= 0; i
< maff
->n
; ++i
) {
5912 maff
->u
.p
[i
] = isl_aff_lift(maff
->u
.p
[i
]);
5920 isl_local_space_free(*ls
);
5921 return isl_multi_aff_free(maff
);
5925 #define TYPE isl_pw_multi_aff
5927 #include "check_type_range_templ.c"
5929 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma".
5931 __isl_give isl_pw_aff
*isl_pw_multi_aff_get_pw_aff(
5932 __isl_keep isl_pw_multi_aff
*pma
, int pos
)
5939 if (isl_pw_multi_aff_check_range(pma
, isl_dim_out
, pos
, 1) < 0)
5942 n_out
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
5946 space
= isl_pw_multi_aff_get_space(pma
);
5947 space
= isl_space_drop_dims(space
, isl_dim_out
,
5948 pos
+ 1, n_out
- pos
- 1);
5949 space
= isl_space_drop_dims(space
, isl_dim_out
, 0, pos
);
5951 pa
= isl_pw_aff_alloc_size(space
, pma
->n
);
5952 for (i
= 0; i
< pma
->n
; ++i
) {
5954 aff
= isl_multi_aff_get_aff(pma
->p
[i
].maff
, pos
);
5955 pa
= isl_pw_aff_add_piece(pa
, isl_set_copy(pma
->p
[i
].set
), aff
);
5961 /* Return an isl_pw_multi_aff with the given "set" as domain and
5962 * an unnamed zero-dimensional range.
5964 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_domain(
5965 __isl_take isl_set
*set
)
5970 space
= isl_set_get_space(set
);
5971 space
= isl_space_from_domain(space
);
5972 ma
= isl_multi_aff_zero(space
);
5973 return isl_pw_multi_aff_alloc(set
, ma
);
5976 /* Add an isl_pw_multi_aff with the given "set" as domain and
5977 * an unnamed zero-dimensional range to *user.
5979 static isl_stat
add_pw_multi_aff_from_domain(__isl_take isl_set
*set
,
5982 isl_union_pw_multi_aff
**upma
= user
;
5983 isl_pw_multi_aff
*pma
;
5985 pma
= isl_pw_multi_aff_from_domain(set
);
5986 *upma
= isl_union_pw_multi_aff_add_pw_multi_aff(*upma
, pma
);
5991 /* Return an isl_union_pw_multi_aff with the given "uset" as domain and
5992 * an unnamed zero-dimensional range.
5994 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_domain(
5995 __isl_take isl_union_set
*uset
)
5998 isl_union_pw_multi_aff
*upma
;
6003 space
= isl_union_set_get_space(uset
);
6004 upma
= isl_union_pw_multi_aff_empty(space
);
6006 if (isl_union_set_foreach_set(uset
,
6007 &add_pw_multi_aff_from_domain
, &upma
) < 0)
6010 isl_union_set_free(uset
);
6013 isl_union_set_free(uset
);
6014 isl_union_pw_multi_aff_free(upma
);
6018 /* Local data for bin_entry and the callback "fn".
6020 struct isl_union_pw_multi_aff_bin_data
{
6021 isl_union_pw_multi_aff
*upma2
;
6022 isl_union_pw_multi_aff
*res
;
6023 isl_pw_multi_aff
*pma
;
6024 isl_stat (*fn
)(__isl_take isl_pw_multi_aff
*pma
, void *user
);
6027 /* Given an isl_pw_multi_aff from upma1, store it in data->pma
6028 * and call data->fn for each isl_pw_multi_aff in data->upma2.
6030 static isl_stat
bin_entry(__isl_take isl_pw_multi_aff
*pma
, void *user
)
6032 struct isl_union_pw_multi_aff_bin_data
*data
= user
;
6036 r
= isl_union_pw_multi_aff_foreach_pw_multi_aff(data
->upma2
,
6038 isl_pw_multi_aff_free(pma
);
6043 /* Call "fn" on each pair of isl_pw_multi_affs in "upma1" and "upma2".
6044 * The isl_pw_multi_aff from upma1 is stored in data->pma (where data is
6045 * passed as user field) and the isl_pw_multi_aff from upma2 is available
6046 * as *entry. The callback should adjust data->res if desired.
6048 static __isl_give isl_union_pw_multi_aff
*bin_op(
6049 __isl_take isl_union_pw_multi_aff
*upma1
,
6050 __isl_take isl_union_pw_multi_aff
*upma2
,
6051 isl_stat (*fn
)(__isl_take isl_pw_multi_aff
*pma
, void *user
))
6054 struct isl_union_pw_multi_aff_bin_data data
= { NULL
, NULL
, NULL
, fn
};
6056 space
= isl_union_pw_multi_aff_get_space(upma2
);
6057 upma1
= isl_union_pw_multi_aff_align_params(upma1
, space
);
6058 space
= isl_union_pw_multi_aff_get_space(upma1
);
6059 upma2
= isl_union_pw_multi_aff_align_params(upma2
, space
);
6061 if (!upma1
|| !upma2
)
6065 data
.res
= isl_union_pw_multi_aff_alloc_same_size(upma1
);
6066 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma1
,
6067 &bin_entry
, &data
) < 0)
6070 isl_union_pw_multi_aff_free(upma1
);
6071 isl_union_pw_multi_aff_free(upma2
);
6074 isl_union_pw_multi_aff_free(upma1
);
6075 isl_union_pw_multi_aff_free(upma2
);
6076 isl_union_pw_multi_aff_free(data
.res
);
6080 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
6081 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
6083 static __isl_give isl_pw_multi_aff
*pw_multi_aff_range_product(
6084 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
6088 space
= isl_space_range_product(isl_pw_multi_aff_get_space(pma1
),
6089 isl_pw_multi_aff_get_space(pma2
));
6090 return isl_pw_multi_aff_on_shared_domain_in(pma1
, pma2
, space
,
6091 &isl_multi_aff_range_product
);
6094 /* Given two isl_pw_multi_affs A -> B and C -> D,
6095 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
6097 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_range_product(
6098 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
6100 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
6101 &pw_multi_aff_range_product
);
6104 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
6105 * construct an isl_pw_multi_aff (A * C) -> (B, D).
6107 static __isl_give isl_pw_multi_aff
*pw_multi_aff_flat_range_product(
6108 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
6112 space
= isl_space_range_product(isl_pw_multi_aff_get_space(pma1
),
6113 isl_pw_multi_aff_get_space(pma2
));
6114 space
= isl_space_flatten_range(space
);
6115 return isl_pw_multi_aff_on_shared_domain_in(pma1
, pma2
, space
,
6116 &isl_multi_aff_flat_range_product
);
6119 /* Given two isl_pw_multi_affs A -> B and C -> D,
6120 * construct an isl_pw_multi_aff (A * C) -> (B, D).
6122 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_flat_range_product(
6123 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
6125 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
6126 &pw_multi_aff_flat_range_product
);
6129 /* If data->pma and "pma2" have the same domain space, then compute
6130 * their flat range product and the result to data->res.
6132 static isl_stat
flat_range_product_entry(__isl_take isl_pw_multi_aff
*pma2
,
6135 struct isl_union_pw_multi_aff_bin_data
*data
= user
;
6137 if (!isl_space_tuple_is_equal(data
->pma
->dim
, isl_dim_in
,
6138 pma2
->dim
, isl_dim_in
)) {
6139 isl_pw_multi_aff_free(pma2
);
6143 pma2
= isl_pw_multi_aff_flat_range_product(
6144 isl_pw_multi_aff_copy(data
->pma
), pma2
);
6146 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma2
);
6151 /* Given two isl_union_pw_multi_affs A -> B and C -> D,
6152 * construct an isl_union_pw_multi_aff (A * C) -> (B, D).
6154 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_flat_range_product(
6155 __isl_take isl_union_pw_multi_aff
*upma1
,
6156 __isl_take isl_union_pw_multi_aff
*upma2
)
6158 return bin_op(upma1
, upma2
, &flat_range_product_entry
);
6161 /* Replace the affine expressions at position "pos" in "pma" by "pa".
6162 * The parameters are assumed to have been aligned.
6164 * The implementation essentially performs an isl_pw_*_on_shared_domain,
6165 * except that it works on two different isl_pw_* types.
6167 static __isl_give isl_pw_multi_aff
*pw_multi_aff_set_pw_aff(
6168 __isl_take isl_pw_multi_aff
*pma
, unsigned pos
,
6169 __isl_take isl_pw_aff
*pa
)
6172 isl_pw_multi_aff
*res
= NULL
;
6177 if (!isl_space_tuple_is_equal(pma
->dim
, isl_dim_in
,
6178 pa
->dim
, isl_dim_in
))
6179 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
6180 "domains don't match", goto error
);
6181 if (isl_pw_multi_aff_check_range(pma
, isl_dim_out
, pos
, 1) < 0)
6185 res
= isl_pw_multi_aff_alloc_size(isl_pw_multi_aff_get_space(pma
), n
);
6187 for (i
= 0; i
< pma
->n
; ++i
) {
6188 for (j
= 0; j
< pa
->n
; ++j
) {
6190 isl_multi_aff
*res_ij
;
6193 common
= isl_set_intersect(isl_set_copy(pma
->p
[i
].set
),
6194 isl_set_copy(pa
->p
[j
].set
));
6195 empty
= isl_set_plain_is_empty(common
);
6196 if (empty
< 0 || empty
) {
6197 isl_set_free(common
);
6203 res_ij
= isl_multi_aff_set_aff(
6204 isl_multi_aff_copy(pma
->p
[i
].maff
), pos
,
6205 isl_aff_copy(pa
->p
[j
].aff
));
6206 res_ij
= isl_multi_aff_gist(res_ij
,
6207 isl_set_copy(common
));
6209 res
= isl_pw_multi_aff_add_piece(res
, common
, res_ij
);
6213 isl_pw_multi_aff_free(pma
);
6214 isl_pw_aff_free(pa
);
6217 isl_pw_multi_aff_free(pma
);
6218 isl_pw_aff_free(pa
);
6219 return isl_pw_multi_aff_free(res
);
6222 /* Replace the affine expressions at position "pos" in "pma" by "pa".
6224 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_set_pw_aff(
6225 __isl_take isl_pw_multi_aff
*pma
, unsigned pos
,
6226 __isl_take isl_pw_aff
*pa
)
6228 isl_bool equal_params
;
6232 equal_params
= isl_space_has_equal_params(pma
->dim
, pa
->dim
);
6233 if (equal_params
< 0)
6236 return pw_multi_aff_set_pw_aff(pma
, pos
, pa
);
6237 if (isl_pw_multi_aff_check_named_params(pma
) < 0 ||
6238 isl_pw_aff_check_named_params(pa
) < 0)
6240 pma
= isl_pw_multi_aff_align_params(pma
, isl_pw_aff_get_space(pa
));
6241 pa
= isl_pw_aff_align_params(pa
, isl_pw_multi_aff_get_space(pma
));
6242 return pw_multi_aff_set_pw_aff(pma
, pos
, pa
);
6244 isl_pw_multi_aff_free(pma
);
6245 isl_pw_aff_free(pa
);
6249 /* Do the parameters of "pa" match those of "space"?
6251 isl_bool
isl_pw_aff_matching_params(__isl_keep isl_pw_aff
*pa
,
6252 __isl_keep isl_space
*space
)
6254 isl_space
*pa_space
;
6258 return isl_bool_error
;
6260 pa_space
= isl_pw_aff_get_space(pa
);
6262 match
= isl_space_has_equal_params(space
, pa_space
);
6264 isl_space_free(pa_space
);
6268 /* Check that the domain space of "pa" matches "space".
6270 isl_stat
isl_pw_aff_check_match_domain_space(__isl_keep isl_pw_aff
*pa
,
6271 __isl_keep isl_space
*space
)
6273 isl_space
*pa_space
;
6277 return isl_stat_error
;
6279 pa_space
= isl_pw_aff_get_space(pa
);
6281 match
= isl_space_has_equal_params(space
, pa_space
);
6285 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
6286 "parameters don't match", goto error
);
6287 match
= isl_space_tuple_is_equal(space
, isl_dim_in
,
6288 pa_space
, isl_dim_in
);
6292 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
6293 "domains don't match", goto error
);
6294 isl_space_free(pa_space
);
6297 isl_space_free(pa_space
);
6298 return isl_stat_error
;
6306 #include <isl_multi_explicit_domain.c>
6307 #include <isl_multi_pw_aff_explicit_domain.c>
6308 #include <isl_multi_templ.c>
6309 #include <isl_multi_apply_set.c>
6310 #include <isl_multi_arith_templ.c>
6311 #include <isl_multi_coalesce.c>
6312 #include <isl_multi_domain_templ.c>
6313 #include <isl_multi_dim_id_templ.c>
6314 #include <isl_multi_dims.c>
6315 #include <isl_multi_from_base_templ.c>
6316 #include <isl_multi_gist.c>
6317 #include <isl_multi_hash.c>
6318 #include <isl_multi_identity_templ.c>
6319 #include <isl_multi_align_set.c>
6320 #include <isl_multi_intersect.c>
6321 #include <isl_multi_move_dims_templ.c>
6322 #include <isl_multi_nan_templ.c>
6323 #include <isl_multi_param_templ.c>
6324 #include <isl_multi_product_templ.c>
6325 #include <isl_multi_splice_templ.c>
6326 #include <isl_multi_tuple_id_templ.c>
6327 #include <isl_multi_zero_templ.c>
6329 /* Does "mpa" have a non-trivial explicit domain?
6331 * The explicit domain, if present, is trivial if it represents
6332 * an (obviously) universe set.
6334 isl_bool
isl_multi_pw_aff_has_non_trivial_domain(
6335 __isl_keep isl_multi_pw_aff
*mpa
)
6338 return isl_bool_error
;
6339 if (!isl_multi_pw_aff_has_explicit_domain(mpa
))
6340 return isl_bool_false
;
6341 return isl_bool_not(isl_set_plain_is_universe(mpa
->u
.dom
));
6344 /* Scale the elements of "pma" by the corresponding elements of "mv".
6346 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_scale_multi_val(
6347 __isl_take isl_pw_multi_aff
*pma
, __isl_take isl_multi_val
*mv
)
6350 isl_bool equal_params
;
6352 pma
= isl_pw_multi_aff_cow(pma
);
6355 if (!isl_space_tuple_is_equal(pma
->dim
, isl_dim_out
,
6356 mv
->space
, isl_dim_set
))
6357 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
6358 "spaces don't match", goto error
);
6359 equal_params
= isl_space_has_equal_params(pma
->dim
, mv
->space
);
6360 if (equal_params
< 0)
6362 if (!equal_params
) {
6363 pma
= isl_pw_multi_aff_align_params(pma
,
6364 isl_multi_val_get_space(mv
));
6365 mv
= isl_multi_val_align_params(mv
,
6366 isl_pw_multi_aff_get_space(pma
));
6371 for (i
= 0; i
< pma
->n
; ++i
) {
6372 pma
->p
[i
].maff
= isl_multi_aff_scale_multi_val(pma
->p
[i
].maff
,
6373 isl_multi_val_copy(mv
));
6374 if (!pma
->p
[i
].maff
)
6378 isl_multi_val_free(mv
);
6381 isl_multi_val_free(mv
);
6382 isl_pw_multi_aff_free(pma
);
6386 /* This function is called for each entry of an isl_union_pw_multi_aff.
6387 * If the space of the entry matches that of data->mv,
6388 * then apply isl_pw_multi_aff_scale_multi_val and return the result.
6389 * Otherwise, return an empty isl_pw_multi_aff.
6391 static __isl_give isl_pw_multi_aff
*union_pw_multi_aff_scale_multi_val_entry(
6392 __isl_take isl_pw_multi_aff
*pma
, void *user
)
6394 isl_multi_val
*mv
= user
;
6398 if (!isl_space_tuple_is_equal(pma
->dim
, isl_dim_out
,
6399 mv
->space
, isl_dim_set
)) {
6400 isl_space
*space
= isl_pw_multi_aff_get_space(pma
);
6401 isl_pw_multi_aff_free(pma
);
6402 return isl_pw_multi_aff_empty(space
);
6405 return isl_pw_multi_aff_scale_multi_val(pma
, isl_multi_val_copy(mv
));
6408 /* Scale the elements of "upma" by the corresponding elements of "mv",
6409 * for those entries that match the space of "mv".
6411 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_scale_multi_val(
6412 __isl_take isl_union_pw_multi_aff
*upma
, __isl_take isl_multi_val
*mv
)
6414 upma
= isl_union_pw_multi_aff_align_params(upma
,
6415 isl_multi_val_get_space(mv
));
6416 mv
= isl_multi_val_align_params(mv
,
6417 isl_union_pw_multi_aff_get_space(upma
));
6421 return isl_union_pw_multi_aff_transform(upma
,
6422 &union_pw_multi_aff_scale_multi_val_entry
, mv
);
6424 isl_multi_val_free(mv
);
6427 isl_multi_val_free(mv
);
6428 isl_union_pw_multi_aff_free(upma
);
6432 /* Construct and return a piecewise multi affine expression
6433 * in the given space with value zero in each of the output dimensions and
6434 * a universe domain.
6436 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_zero(__isl_take isl_space
*space
)
6438 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_zero(space
));
6441 /* Construct and return a piecewise multi affine expression
6442 * that is equal to the given piecewise affine expression.
6444 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_pw_aff(
6445 __isl_take isl_pw_aff
*pa
)
6449 isl_pw_multi_aff
*pma
;
6454 space
= isl_pw_aff_get_space(pa
);
6455 pma
= isl_pw_multi_aff_alloc_size(space
, pa
->n
);
6457 for (i
= 0; i
< pa
->n
; ++i
) {
6461 set
= isl_set_copy(pa
->p
[i
].set
);
6462 ma
= isl_multi_aff_from_aff(isl_aff_copy(pa
->p
[i
].aff
));
6463 pma
= isl_pw_multi_aff_add_piece(pma
, set
, ma
);
6466 isl_pw_aff_free(pa
);
6470 /* Construct and return a piecewise multi affine expression
6471 * that is equal to the given multi piecewise affine expression
6472 * on the shared domain of the piecewise affine expressions,
6473 * in the special case of a 0D multi piecewise affine expression.
6475 * Create a piecewise multi affine expression with the explicit domain of
6476 * the 0D multi piecewise affine expression as domain.
6478 static __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_multi_pw_aff_0D(
6479 __isl_take isl_multi_pw_aff
*mpa
)
6485 space
= isl_multi_pw_aff_get_space(mpa
);
6486 dom
= isl_multi_pw_aff_get_explicit_domain(mpa
);
6487 isl_multi_pw_aff_free(mpa
);
6489 ma
= isl_multi_aff_zero(space
);
6490 return isl_pw_multi_aff_alloc(dom
, ma
);
6493 /* Construct and return a piecewise multi affine expression
6494 * that is equal to the given multi piecewise affine expression
6495 * on the shared domain of the piecewise affine expressions.
6497 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_multi_pw_aff(
6498 __isl_take isl_multi_pw_aff
*mpa
)
6503 isl_pw_multi_aff
*pma
;
6509 return isl_pw_multi_aff_from_multi_pw_aff_0D(mpa
);
6511 space
= isl_multi_pw_aff_get_space(mpa
);
6512 pa
= isl_multi_pw_aff_get_pw_aff(mpa
, 0);
6513 pma
= isl_pw_multi_aff_from_pw_aff(pa
);
6515 for (i
= 1; i
< mpa
->n
; ++i
) {
6516 isl_pw_multi_aff
*pma_i
;
6518 pa
= isl_multi_pw_aff_get_pw_aff(mpa
, i
);
6519 pma_i
= isl_pw_multi_aff_from_pw_aff(pa
);
6520 pma
= isl_pw_multi_aff_range_product(pma
, pma_i
);
6523 pma
= isl_pw_multi_aff_reset_space(pma
, space
);
6525 isl_multi_pw_aff_free(mpa
);
6529 /* Construct and return a multi piecewise affine expression
6530 * that is equal to the given multi affine expression.
6532 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_from_multi_aff(
6533 __isl_take isl_multi_aff
*ma
)
6537 isl_multi_pw_aff
*mpa
;
6539 n
= isl_multi_aff_dim(ma
, isl_dim_out
);
6541 ma
= isl_multi_aff_free(ma
);
6545 mpa
= isl_multi_pw_aff_alloc(isl_multi_aff_get_space(ma
));
6547 for (i
= 0; i
< n
; ++i
) {
6550 pa
= isl_pw_aff_from_aff(isl_multi_aff_get_aff(ma
, i
));
6551 mpa
= isl_multi_pw_aff_set_pw_aff(mpa
, i
, pa
);
6554 isl_multi_aff_free(ma
);
6558 /* Construct and return a multi piecewise affine expression
6559 * that is equal to the given piecewise multi affine expression.
6561 * If the resulting multi piecewise affine expression has
6562 * an explicit domain, then assign it the domain of the input.
6563 * In other cases, the domain is stored in the individual elements.
6565 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_from_pw_multi_aff(
6566 __isl_take isl_pw_multi_aff
*pma
)
6571 isl_multi_pw_aff
*mpa
;
6573 n
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
6575 pma
= isl_pw_multi_aff_free(pma
);
6576 space
= isl_pw_multi_aff_get_space(pma
);
6577 mpa
= isl_multi_pw_aff_alloc(space
);
6579 for (i
= 0; i
< n
; ++i
) {
6582 pa
= isl_pw_multi_aff_get_pw_aff(pma
, i
);
6583 mpa
= isl_multi_pw_aff_set_pw_aff(mpa
, i
, pa
);
6585 if (isl_multi_pw_aff_has_explicit_domain(mpa
)) {
6588 dom
= isl_pw_multi_aff_domain(isl_pw_multi_aff_copy(pma
));
6589 mpa
= isl_multi_pw_aff_intersect_domain(mpa
, dom
);
6592 isl_pw_multi_aff_free(pma
);
6596 /* Do "pa1" and "pa2" represent the same function?
6598 * We first check if they are obviously equal.
6599 * If not, we convert them to maps and check if those are equal.
6601 * If "pa1" or "pa2" contain any NaNs, then they are considered
6602 * not to be the same. A NaN is not equal to anything, not even
6605 isl_bool
isl_pw_aff_is_equal(__isl_keep isl_pw_aff
*pa1
,
6606 __isl_keep isl_pw_aff
*pa2
)
6610 isl_map
*map1
, *map2
;
6613 return isl_bool_error
;
6615 equal
= isl_pw_aff_plain_is_equal(pa1
, pa2
);
6616 if (equal
< 0 || equal
)
6618 has_nan
= either_involves_nan(pa1
, pa2
);
6620 return isl_bool_error
;
6622 return isl_bool_false
;
6624 map1
= isl_map_from_pw_aff_internal(isl_pw_aff_copy(pa1
));
6625 map2
= isl_map_from_pw_aff_internal(isl_pw_aff_copy(pa2
));
6626 equal
= isl_map_is_equal(map1
, map2
);
6633 /* Do "mpa1" and "mpa2" represent the same function?
6635 * Note that we cannot convert the entire isl_multi_pw_aff
6636 * to a map because the domains of the piecewise affine expressions
6637 * may not be the same.
6639 isl_bool
isl_multi_pw_aff_is_equal(__isl_keep isl_multi_pw_aff
*mpa1
,
6640 __isl_keep isl_multi_pw_aff
*mpa2
)
6643 isl_bool equal
, equal_params
;
6646 return isl_bool_error
;
6648 equal_params
= isl_space_has_equal_params(mpa1
->space
, mpa2
->space
);
6649 if (equal_params
< 0)
6650 return isl_bool_error
;
6651 if (!equal_params
) {
6652 if (!isl_space_has_named_params(mpa1
->space
))
6653 return isl_bool_false
;
6654 if (!isl_space_has_named_params(mpa2
->space
))
6655 return isl_bool_false
;
6656 mpa1
= isl_multi_pw_aff_copy(mpa1
);
6657 mpa2
= isl_multi_pw_aff_copy(mpa2
);
6658 mpa1
= isl_multi_pw_aff_align_params(mpa1
,
6659 isl_multi_pw_aff_get_space(mpa2
));
6660 mpa2
= isl_multi_pw_aff_align_params(mpa2
,
6661 isl_multi_pw_aff_get_space(mpa1
));
6662 equal
= isl_multi_pw_aff_is_equal(mpa1
, mpa2
);
6663 isl_multi_pw_aff_free(mpa1
);
6664 isl_multi_pw_aff_free(mpa2
);
6668 equal
= isl_space_is_equal(mpa1
->space
, mpa2
->space
);
6669 if (equal
< 0 || !equal
)
6672 for (i
= 0; i
< mpa1
->n
; ++i
) {
6673 equal
= isl_pw_aff_is_equal(mpa1
->u
.p
[i
], mpa2
->u
.p
[i
]);
6674 if (equal
< 0 || !equal
)
6678 return isl_bool_true
;
6681 /* Do "pma1" and "pma2" represent the same function?
6683 * First check if they are obviously equal.
6684 * If not, then convert them to maps and check if those are equal.
6686 * If "pa1" or "pa2" contain any NaNs, then they are considered
6687 * not to be the same. A NaN is not equal to anything, not even
6690 isl_bool
isl_pw_multi_aff_is_equal(__isl_keep isl_pw_multi_aff
*pma1
,
6691 __isl_keep isl_pw_multi_aff
*pma2
)
6695 isl_map
*map1
, *map2
;
6698 return isl_bool_error
;
6700 equal
= isl_pw_multi_aff_plain_is_equal(pma1
, pma2
);
6701 if (equal
< 0 || equal
)
6703 has_nan
= isl_pw_multi_aff_involves_nan(pma1
);
6704 if (has_nan
>= 0 && !has_nan
)
6705 has_nan
= isl_pw_multi_aff_involves_nan(pma2
);
6706 if (has_nan
< 0 || has_nan
)
6707 return isl_bool_not(has_nan
);
6709 map1
= isl_map_from_pw_multi_aff(isl_pw_multi_aff_copy(pma1
));
6710 map2
= isl_map_from_pw_multi_aff(isl_pw_multi_aff_copy(pma2
));
6711 equal
= isl_map_is_equal(map1
, map2
);
6718 /* Compute the pullback of "mpa" by the function represented by "ma".
6719 * In other words, plug in "ma" in "mpa".
6721 * The parameters of "mpa" and "ma" are assumed to have been aligned.
6723 * If "mpa" has an explicit domain, then it is this domain
6724 * that needs to undergo a pullback, i.e., a preimage.
6726 static __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_pullback_multi_aff_aligned(
6727 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_multi_aff
*ma
)
6730 isl_space
*space
= NULL
;
6732 mpa
= isl_multi_pw_aff_cow(mpa
);
6736 space
= isl_space_join(isl_multi_aff_get_space(ma
),
6737 isl_multi_pw_aff_get_space(mpa
));
6741 for (i
= 0; i
< mpa
->n
; ++i
) {
6742 mpa
->u
.p
[i
] = isl_pw_aff_pullback_multi_aff(mpa
->u
.p
[i
],
6743 isl_multi_aff_copy(ma
));
6747 if (isl_multi_pw_aff_has_explicit_domain(mpa
)) {
6748 mpa
->u
.dom
= isl_set_preimage_multi_aff(mpa
->u
.dom
,
6749 isl_multi_aff_copy(ma
));
6754 isl_multi_aff_free(ma
);
6755 isl_space_free(mpa
->space
);
6759 isl_space_free(space
);
6760 isl_multi_pw_aff_free(mpa
);
6761 isl_multi_aff_free(ma
);
6765 /* Compute the pullback of "mpa" by the function represented by "ma".
6766 * In other words, plug in "ma" in "mpa".
6768 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_pullback_multi_aff(
6769 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_multi_aff
*ma
)
6771 isl_bool equal_params
;
6775 equal_params
= isl_space_has_equal_params(mpa
->space
, ma
->space
);
6776 if (equal_params
< 0)
6779 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa
, ma
);
6780 mpa
= isl_multi_pw_aff_align_params(mpa
, isl_multi_aff_get_space(ma
));
6781 ma
= isl_multi_aff_align_params(ma
, isl_multi_pw_aff_get_space(mpa
));
6782 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa
, ma
);
6784 isl_multi_pw_aff_free(mpa
);
6785 isl_multi_aff_free(ma
);
6789 /* Compute the pullback of "mpa" by the function represented by "pma".
6790 * In other words, plug in "pma" in "mpa".
6792 * The parameters of "mpa" and "mpa" are assumed to have been aligned.
6794 * If "mpa" has an explicit domain, then it is this domain
6795 * that needs to undergo a pullback, i.e., a preimage.
6797 static __isl_give isl_multi_pw_aff
*
6798 isl_multi_pw_aff_pullback_pw_multi_aff_aligned(
6799 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_pw_multi_aff
*pma
)
6802 isl_space
*space
= NULL
;
6804 mpa
= isl_multi_pw_aff_cow(mpa
);
6808 space
= isl_space_join(isl_pw_multi_aff_get_space(pma
),
6809 isl_multi_pw_aff_get_space(mpa
));
6811 for (i
= 0; i
< mpa
->n
; ++i
) {
6812 mpa
->u
.p
[i
] = isl_pw_aff_pullback_pw_multi_aff_aligned(
6813 mpa
->u
.p
[i
], isl_pw_multi_aff_copy(pma
));
6817 if (isl_multi_pw_aff_has_explicit_domain(mpa
)) {
6818 mpa
->u
.dom
= isl_set_preimage_pw_multi_aff(mpa
->u
.dom
,
6819 isl_pw_multi_aff_copy(pma
));
6824 isl_pw_multi_aff_free(pma
);
6825 isl_space_free(mpa
->space
);
6829 isl_space_free(space
);
6830 isl_multi_pw_aff_free(mpa
);
6831 isl_pw_multi_aff_free(pma
);
6835 /* Compute the pullback of "mpa" by the function represented by "pma".
6836 * In other words, plug in "pma" in "mpa".
6838 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_pullback_pw_multi_aff(
6839 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_pw_multi_aff
*pma
)
6841 isl_bool equal_params
;
6845 equal_params
= isl_space_has_equal_params(mpa
->space
, pma
->dim
);
6846 if (equal_params
< 0)
6849 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa
, pma
);
6850 mpa
= isl_multi_pw_aff_align_params(mpa
,
6851 isl_pw_multi_aff_get_space(pma
));
6852 pma
= isl_pw_multi_aff_align_params(pma
,
6853 isl_multi_pw_aff_get_space(mpa
));
6854 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa
, pma
);
6856 isl_multi_pw_aff_free(mpa
);
6857 isl_pw_multi_aff_free(pma
);
6861 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
6862 * with the domain of "aff". The domain of the result is the same
6864 * "mpa" and "aff" are assumed to have been aligned.
6866 * We first extract the parametric constant from "aff", defined
6867 * over the correct domain.
6868 * Then we add the appropriate combinations of the members of "mpa".
6869 * Finally, we add the integer divisions through recursive calls.
6871 static __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_aff_aligned(
6872 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_aff
*aff
)
6875 isl_size n_in
, n_div
, n_mpa_in
;
6881 n_in
= isl_aff_dim(aff
, isl_dim_in
);
6882 n_div
= isl_aff_dim(aff
, isl_dim_div
);
6883 n_mpa_in
= isl_multi_pw_aff_dim(mpa
, isl_dim_in
);
6884 if (n_in
< 0 || n_div
< 0 || n_mpa_in
< 0)
6887 space
= isl_space_domain(isl_multi_pw_aff_get_space(mpa
));
6888 tmp
= isl_aff_copy(aff
);
6889 tmp
= isl_aff_drop_dims(tmp
, isl_dim_div
, 0, n_div
);
6890 tmp
= isl_aff_drop_dims(tmp
, isl_dim_in
, 0, n_in
);
6891 tmp
= isl_aff_add_dims(tmp
, isl_dim_in
, n_mpa_in
);
6892 tmp
= isl_aff_reset_domain_space(tmp
, space
);
6893 pa
= isl_pw_aff_from_aff(tmp
);
6895 for (i
= 0; i
< n_in
; ++i
) {
6898 if (!isl_aff_involves_dims(aff
, isl_dim_in
, i
, 1))
6900 v
= isl_aff_get_coefficient_val(aff
, isl_dim_in
, i
);
6901 pa_i
= isl_multi_pw_aff_get_pw_aff(mpa
, i
);
6902 pa_i
= isl_pw_aff_scale_val(pa_i
, v
);
6903 pa
= isl_pw_aff_add(pa
, pa_i
);
6906 for (i
= 0; i
< n_div
; ++i
) {
6910 if (!isl_aff_involves_dims(aff
, isl_dim_div
, i
, 1))
6912 div
= isl_aff_get_div(aff
, i
);
6913 pa_i
= isl_multi_pw_aff_apply_aff_aligned(
6914 isl_multi_pw_aff_copy(mpa
), div
);
6915 pa_i
= isl_pw_aff_floor(pa_i
);
6916 v
= isl_aff_get_coefficient_val(aff
, isl_dim_div
, i
);
6917 pa_i
= isl_pw_aff_scale_val(pa_i
, v
);
6918 pa
= isl_pw_aff_add(pa
, pa_i
);
6921 isl_multi_pw_aff_free(mpa
);
6926 isl_multi_pw_aff_free(mpa
);
6931 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
6932 * with the domain of "aff". The domain of the result is the same
6935 __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_aff(
6936 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_aff
*aff
)
6938 isl_bool equal_params
;
6942 equal_params
= isl_space_has_equal_params(aff
->ls
->dim
, mpa
->space
);
6943 if (equal_params
< 0)
6946 return isl_multi_pw_aff_apply_aff_aligned(mpa
, aff
);
6948 aff
= isl_aff_align_params(aff
, isl_multi_pw_aff_get_space(mpa
));
6949 mpa
= isl_multi_pw_aff_align_params(mpa
, isl_aff_get_space(aff
));
6951 return isl_multi_pw_aff_apply_aff_aligned(mpa
, aff
);
6954 isl_multi_pw_aff_free(mpa
);
6958 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
6959 * with the domain of "pa". The domain of the result is the same
6961 * "mpa" and "pa" are assumed to have been aligned.
6963 * We consider each piece in turn. Note that the domains of the
6964 * pieces are assumed to be disjoint and they remain disjoint
6965 * after taking the preimage (over the same function).
6967 static __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_pw_aff_aligned(
6968 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_pw_aff
*pa
)
6977 space
= isl_space_join(isl_multi_pw_aff_get_space(mpa
),
6978 isl_pw_aff_get_space(pa
));
6979 res
= isl_pw_aff_empty(space
);
6981 for (i
= 0; i
< pa
->n
; ++i
) {
6985 pa_i
= isl_multi_pw_aff_apply_aff_aligned(
6986 isl_multi_pw_aff_copy(mpa
),
6987 isl_aff_copy(pa
->p
[i
].aff
));
6988 domain
= isl_set_copy(pa
->p
[i
].set
);
6989 domain
= isl_set_preimage_multi_pw_aff(domain
,
6990 isl_multi_pw_aff_copy(mpa
));
6991 pa_i
= isl_pw_aff_intersect_domain(pa_i
, domain
);
6992 res
= isl_pw_aff_add_disjoint(res
, pa_i
);
6995 isl_pw_aff_free(pa
);
6996 isl_multi_pw_aff_free(mpa
);
6999 isl_pw_aff_free(pa
);
7000 isl_multi_pw_aff_free(mpa
);
7004 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
7005 * with the domain of "pa". The domain of the result is the same
7008 __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_pw_aff(
7009 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_pw_aff
*pa
)
7011 isl_bool equal_params
;
7015 equal_params
= isl_space_has_equal_params(pa
->dim
, mpa
->space
);
7016 if (equal_params
< 0)
7019 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa
, pa
);
7021 pa
= isl_pw_aff_align_params(pa
, isl_multi_pw_aff_get_space(mpa
));
7022 mpa
= isl_multi_pw_aff_align_params(mpa
, isl_pw_aff_get_space(pa
));
7024 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa
, pa
);
7026 isl_pw_aff_free(pa
);
7027 isl_multi_pw_aff_free(mpa
);
7031 /* Compute the pullback of "pa" by the function represented by "mpa".
7032 * In other words, plug in "mpa" in "pa".
7033 * "pa" and "mpa" are assumed to have been aligned.
7035 * The pullback is computed by applying "pa" to "mpa".
7037 static __isl_give isl_pw_aff
*isl_pw_aff_pullback_multi_pw_aff_aligned(
7038 __isl_take isl_pw_aff
*pa
, __isl_take isl_multi_pw_aff
*mpa
)
7040 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa
, pa
);
7043 /* Compute the pullback of "pa" by the function represented by "mpa".
7044 * In other words, plug in "mpa" in "pa".
7046 * The pullback is computed by applying "pa" to "mpa".
7048 __isl_give isl_pw_aff
*isl_pw_aff_pullback_multi_pw_aff(
7049 __isl_take isl_pw_aff
*pa
, __isl_take isl_multi_pw_aff
*mpa
)
7051 return isl_multi_pw_aff_apply_pw_aff(mpa
, pa
);
7054 /* Compute the pullback of "mpa1" by the function represented by "mpa2".
7055 * In other words, plug in "mpa2" in "mpa1".
7057 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7059 * We pullback each member of "mpa1" in turn.
7061 * If "mpa1" has an explicit domain, then it is this domain
7062 * that needs to undergo a pullback instead, i.e., a preimage.
7064 static __isl_give isl_multi_pw_aff
*
7065 isl_multi_pw_aff_pullback_multi_pw_aff_aligned(
7066 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
)
7069 isl_space
*space
= NULL
;
7071 mpa1
= isl_multi_pw_aff_cow(mpa1
);
7075 space
= isl_space_join(isl_multi_pw_aff_get_space(mpa2
),
7076 isl_multi_pw_aff_get_space(mpa1
));
7078 for (i
= 0; i
< mpa1
->n
; ++i
) {
7079 mpa1
->u
.p
[i
] = isl_pw_aff_pullback_multi_pw_aff_aligned(
7080 mpa1
->u
.p
[i
], isl_multi_pw_aff_copy(mpa2
));
7085 if (isl_multi_pw_aff_has_explicit_domain(mpa1
)) {
7086 mpa1
->u
.dom
= isl_set_preimage_multi_pw_aff(mpa1
->u
.dom
,
7087 isl_multi_pw_aff_copy(mpa2
));
7091 mpa1
= isl_multi_pw_aff_reset_space(mpa1
, space
);
7093 isl_multi_pw_aff_free(mpa2
);
7096 isl_space_free(space
);
7097 isl_multi_pw_aff_free(mpa1
);
7098 isl_multi_pw_aff_free(mpa2
);
7102 /* Compute the pullback of "mpa1" by the function represented by "mpa2".
7103 * In other words, plug in "mpa2" in "mpa1".
7105 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_pullback_multi_pw_aff(
7106 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
)
7108 return isl_multi_pw_aff_align_params_multi_multi_and(mpa1
, mpa2
,
7109 &isl_multi_pw_aff_pullback_multi_pw_aff_aligned
);
7112 /* Align the parameters of "mpa1" and "mpa2", check that the ranges
7113 * of "mpa1" and "mpa2" live in the same space, construct map space
7114 * between the domain spaces of "mpa1" and "mpa2" and call "order"
7115 * with this map space as extract argument.
7117 static __isl_give isl_map
*isl_multi_pw_aff_order_map(
7118 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
,
7119 __isl_give isl_map
*(*order
)(__isl_keep isl_multi_pw_aff
*mpa1
,
7120 __isl_keep isl_multi_pw_aff
*mpa2
, __isl_take isl_space
*space
))
7123 isl_space
*space1
, *space2
;
7126 mpa1
= isl_multi_pw_aff_align_params(mpa1
,
7127 isl_multi_pw_aff_get_space(mpa2
));
7128 mpa2
= isl_multi_pw_aff_align_params(mpa2
,
7129 isl_multi_pw_aff_get_space(mpa1
));
7132 match
= isl_space_tuple_is_equal(mpa1
->space
, isl_dim_out
,
7133 mpa2
->space
, isl_dim_out
);
7137 isl_die(isl_multi_pw_aff_get_ctx(mpa1
), isl_error_invalid
,
7138 "range spaces don't match", goto error
);
7139 space1
= isl_space_domain(isl_multi_pw_aff_get_space(mpa1
));
7140 space2
= isl_space_domain(isl_multi_pw_aff_get_space(mpa2
));
7141 space1
= isl_space_map_from_domain_and_range(space1
, space2
);
7143 res
= order(mpa1
, mpa2
, space1
);
7144 isl_multi_pw_aff_free(mpa1
);
7145 isl_multi_pw_aff_free(mpa2
);
7148 isl_multi_pw_aff_free(mpa1
);
7149 isl_multi_pw_aff_free(mpa2
);
7153 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7154 * where the function values are equal. "space" is the space of the result.
7155 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7157 * "mpa1" and "mpa2" are equal when each of the pairs of elements
7158 * in the sequences are equal.
7160 static __isl_give isl_map
*isl_multi_pw_aff_eq_map_on_space(
7161 __isl_keep isl_multi_pw_aff
*mpa1
, __isl_keep isl_multi_pw_aff
*mpa2
,
7162 __isl_take isl_space
*space
)
7168 n
= isl_multi_pw_aff_dim(mpa1
, isl_dim_out
);
7170 space
= isl_space_free(space
);
7171 res
= isl_map_universe(space
);
7173 for (i
= 0; i
< n
; ++i
) {
7174 isl_pw_aff
*pa1
, *pa2
;
7177 pa1
= isl_multi_pw_aff_get_pw_aff(mpa1
, i
);
7178 pa2
= isl_multi_pw_aff_get_pw_aff(mpa2
, i
);
7179 map
= isl_pw_aff_eq_map(pa1
, pa2
);
7180 res
= isl_map_intersect(res
, map
);
7186 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7187 * where the function values are equal.
7189 __isl_give isl_map
*isl_multi_pw_aff_eq_map(__isl_take isl_multi_pw_aff
*mpa1
,
7190 __isl_take isl_multi_pw_aff
*mpa2
)
7192 return isl_multi_pw_aff_order_map(mpa1
, mpa2
,
7193 &isl_multi_pw_aff_eq_map_on_space
);
7196 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7197 * where the function values of "mpa1" is lexicographically satisfies "base"
7198 * compared to that of "mpa2". "space" is the space of the result.
7199 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7201 * "mpa1" lexicographically satisfies "base" compared to "mpa2"
7202 * if its i-th element satisfies "base" when compared to
7203 * the i-th element of "mpa2" while all previous elements are
7206 static __isl_give isl_map
*isl_multi_pw_aff_lex_map_on_space(
7207 __isl_keep isl_multi_pw_aff
*mpa1
, __isl_keep isl_multi_pw_aff
*mpa2
,
7208 __isl_give isl_map
*(*base
)(__isl_take isl_pw_aff
*pa1
,
7209 __isl_take isl_pw_aff
*pa2
),
7210 __isl_take isl_space
*space
)
7214 isl_map
*res
, *rest
;
7216 n
= isl_multi_pw_aff_dim(mpa1
, isl_dim_out
);
7218 space
= isl_space_free(space
);
7219 res
= isl_map_empty(isl_space_copy(space
));
7220 rest
= isl_map_universe(space
);
7222 for (i
= 0; i
< n
; ++i
) {
7223 isl_pw_aff
*pa1
, *pa2
;
7226 pa1
= isl_multi_pw_aff_get_pw_aff(mpa1
, i
);
7227 pa2
= isl_multi_pw_aff_get_pw_aff(mpa2
, i
);
7228 map
= base(pa1
, pa2
);
7229 map
= isl_map_intersect(map
, isl_map_copy(rest
));
7230 res
= isl_map_union(res
, map
);
7235 pa1
= isl_multi_pw_aff_get_pw_aff(mpa1
, i
);
7236 pa2
= isl_multi_pw_aff_get_pw_aff(mpa2
, i
);
7237 map
= isl_pw_aff_eq_map(pa1
, pa2
);
7238 rest
= isl_map_intersect(rest
, map
);
7245 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7246 * where the function value of "mpa1" is lexicographically less than that
7247 * of "mpa2". "space" is the space of the result.
7248 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7250 * "mpa1" is less than "mpa2" if its i-th element is smaller
7251 * than the i-th element of "mpa2" while all previous elements are
7254 __isl_give isl_map
*isl_multi_pw_aff_lex_lt_map_on_space(
7255 __isl_keep isl_multi_pw_aff
*mpa1
, __isl_keep isl_multi_pw_aff
*mpa2
,
7256 __isl_take isl_space
*space
)
7258 return isl_multi_pw_aff_lex_map_on_space(mpa1
, mpa2
,
7259 &isl_pw_aff_lt_map
, space
);
7262 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7263 * where the function value of "mpa1" is lexicographically less than that
7266 __isl_give isl_map
*isl_multi_pw_aff_lex_lt_map(
7267 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
)
7269 return isl_multi_pw_aff_order_map(mpa1
, mpa2
,
7270 &isl_multi_pw_aff_lex_lt_map_on_space
);
7273 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7274 * where the function value of "mpa1" is lexicographically greater than that
7275 * of "mpa2". "space" is the space of the result.
7276 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7278 * "mpa1" is greater than "mpa2" if its i-th element is greater
7279 * than the i-th element of "mpa2" while all previous elements are
7282 __isl_give isl_map
*isl_multi_pw_aff_lex_gt_map_on_space(
7283 __isl_keep isl_multi_pw_aff
*mpa1
, __isl_keep isl_multi_pw_aff
*mpa2
,
7284 __isl_take isl_space
*space
)
7286 return isl_multi_pw_aff_lex_map_on_space(mpa1
, mpa2
,
7287 &isl_pw_aff_gt_map
, space
);
7290 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7291 * where the function value of "mpa1" is lexicographically greater than that
7294 __isl_give isl_map
*isl_multi_pw_aff_lex_gt_map(
7295 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
)
7297 return isl_multi_pw_aff_order_map(mpa1
, mpa2
,
7298 &isl_multi_pw_aff_lex_gt_map_on_space
);
7301 /* Compare two isl_affs.
7303 * Return -1 if "aff1" is "smaller" than "aff2", 1 if "aff1" is "greater"
7304 * than "aff2" and 0 if they are equal.
7306 * The order is fairly arbitrary. We do consider expressions that only involve
7307 * earlier dimensions as "smaller".
7309 int isl_aff_plain_cmp(__isl_keep isl_aff
*aff1
, __isl_keep isl_aff
*aff2
)
7322 cmp
= isl_local_space_cmp(aff1
->ls
, aff2
->ls
);
7326 last1
= isl_seq_last_non_zero(aff1
->v
->el
+ 1, aff1
->v
->size
- 1);
7327 last2
= isl_seq_last_non_zero(aff2
->v
->el
+ 1, aff1
->v
->size
- 1);
7329 return last1
- last2
;
7331 return isl_seq_cmp(aff1
->v
->el
, aff2
->v
->el
, aff1
->v
->size
);
7334 /* Compare two isl_pw_affs.
7336 * Return -1 if "pa1" is "smaller" than "pa2", 1 if "pa1" is "greater"
7337 * than "pa2" and 0 if they are equal.
7339 * The order is fairly arbitrary. We do consider expressions that only involve
7340 * earlier dimensions as "smaller".
7342 int isl_pw_aff_plain_cmp(__isl_keep isl_pw_aff
*pa1
,
7343 __isl_keep isl_pw_aff
*pa2
)
7356 cmp
= isl_space_cmp(pa1
->dim
, pa2
->dim
);
7360 if (pa1
->n
!= pa2
->n
)
7361 return pa1
->n
- pa2
->n
;
7363 for (i
= 0; i
< pa1
->n
; ++i
) {
7364 cmp
= isl_set_plain_cmp(pa1
->p
[i
].set
, pa2
->p
[i
].set
);
7367 cmp
= isl_aff_plain_cmp(pa1
->p
[i
].aff
, pa2
->p
[i
].aff
);
7375 /* Return a piecewise affine expression that is equal to "v" on "domain".
7377 __isl_give isl_pw_aff
*isl_pw_aff_val_on_domain(__isl_take isl_set
*domain
,
7378 __isl_take isl_val
*v
)
7381 isl_local_space
*ls
;
7384 space
= isl_set_get_space(domain
);
7385 ls
= isl_local_space_from_space(space
);
7386 aff
= isl_aff_val_on_domain(ls
, v
);
7388 return isl_pw_aff_alloc(domain
, aff
);
7391 /* Return a multi affine expression that is equal to "mv" on domain
7394 __isl_give isl_multi_aff
*isl_multi_aff_multi_val_on_space(
7395 __isl_take isl_space
*space
, __isl_take isl_multi_val
*mv
)
7400 isl_local_space
*ls
;
7403 n
= isl_multi_val_dim(mv
, isl_dim_set
);
7404 if (!space
|| n
< 0)
7407 space2
= isl_multi_val_get_space(mv
);
7408 space2
= isl_space_align_params(space2
, isl_space_copy(space
));
7409 space
= isl_space_align_params(space
, isl_space_copy(space2
));
7410 space
= isl_space_map_from_domain_and_range(space
, space2
);
7411 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
7412 ls
= isl_local_space_from_space(isl_space_domain(space
));
7413 for (i
= 0; i
< n
; ++i
) {
7417 v
= isl_multi_val_get_val(mv
, i
);
7418 aff
= isl_aff_val_on_domain(isl_local_space_copy(ls
), v
);
7419 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
7421 isl_local_space_free(ls
);
7423 isl_multi_val_free(mv
);
7426 isl_space_free(space
);
7427 isl_multi_val_free(mv
);
7431 /* Return a piecewise multi-affine expression
7432 * that is equal to "mv" on "domain".
7434 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_multi_val_on_domain(
7435 __isl_take isl_set
*domain
, __isl_take isl_multi_val
*mv
)
7440 space
= isl_set_get_space(domain
);
7441 ma
= isl_multi_aff_multi_val_on_space(space
, mv
);
7443 return isl_pw_multi_aff_alloc(domain
, ma
);
7446 /* Internal data structure for isl_union_pw_multi_aff_multi_val_on_domain.
7447 * mv is the value that should be attained on each domain set
7448 * res collects the results
7450 struct isl_union_pw_multi_aff_multi_val_on_domain_data
{
7452 isl_union_pw_multi_aff
*res
;
7455 /* Create an isl_pw_multi_aff equal to data->mv on "domain"
7456 * and add it to data->res.
7458 static isl_stat
pw_multi_aff_multi_val_on_domain(__isl_take isl_set
*domain
,
7461 struct isl_union_pw_multi_aff_multi_val_on_domain_data
*data
= user
;
7462 isl_pw_multi_aff
*pma
;
7465 mv
= isl_multi_val_copy(data
->mv
);
7466 pma
= isl_pw_multi_aff_multi_val_on_domain(domain
, mv
);
7467 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma
);
7469 return data
->res
? isl_stat_ok
: isl_stat_error
;
7472 /* Return a union piecewise multi-affine expression
7473 * that is equal to "mv" on "domain".
7475 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_multi_val_on_domain(
7476 __isl_take isl_union_set
*domain
, __isl_take isl_multi_val
*mv
)
7478 struct isl_union_pw_multi_aff_multi_val_on_domain_data data
;
7481 space
= isl_union_set_get_space(domain
);
7482 data
.res
= isl_union_pw_multi_aff_empty(space
);
7484 if (isl_union_set_foreach_set(domain
,
7485 &pw_multi_aff_multi_val_on_domain
, &data
) < 0)
7486 data
.res
= isl_union_pw_multi_aff_free(data
.res
);
7487 isl_union_set_free(domain
);
7488 isl_multi_val_free(mv
);
7492 /* Compute the pullback of data->pma by the function represented by "pma2",
7493 * provided the spaces match, and add the results to data->res.
7495 static isl_stat
pullback_entry(__isl_take isl_pw_multi_aff
*pma2
, void *user
)
7497 struct isl_union_pw_multi_aff_bin_data
*data
= user
;
7499 if (!isl_space_tuple_is_equal(data
->pma
->dim
, isl_dim_in
,
7500 pma2
->dim
, isl_dim_out
)) {
7501 isl_pw_multi_aff_free(pma2
);
7505 pma2
= isl_pw_multi_aff_pullback_pw_multi_aff(
7506 isl_pw_multi_aff_copy(data
->pma
), pma2
);
7508 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma2
);
7510 return isl_stat_error
;
7515 /* Compute the pullback of "upma1" by the function represented by "upma2".
7517 __isl_give isl_union_pw_multi_aff
*
7518 isl_union_pw_multi_aff_pullback_union_pw_multi_aff(
7519 __isl_take isl_union_pw_multi_aff
*upma1
,
7520 __isl_take isl_union_pw_multi_aff
*upma2
)
7522 return bin_op(upma1
, upma2
, &pullback_entry
);
7525 /* Check that the domain space of "upa" matches "space".
7527 * This function is called from isl_multi_union_pw_aff_set_union_pw_aff and
7528 * can in principle never fail since the space "space" is that
7529 * of the isl_multi_union_pw_aff and is a set space such that
7530 * there is no domain space to match.
7532 * We check the parameters and double-check that "space" is
7533 * indeed that of a set.
7535 static isl_stat
isl_union_pw_aff_check_match_domain_space(
7536 __isl_keep isl_union_pw_aff
*upa
, __isl_keep isl_space
*space
)
7538 isl_space
*upa_space
;
7542 return isl_stat_error
;
7544 match
= isl_space_is_set(space
);
7546 return isl_stat_error
;
7548 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
7549 "expecting set space", return isl_stat_error
);
7551 upa_space
= isl_union_pw_aff_get_space(upa
);
7552 match
= isl_space_has_equal_params(space
, upa_space
);
7556 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
7557 "parameters don't match", goto error
);
7559 isl_space_free(upa_space
);
7562 isl_space_free(upa_space
);
7563 return isl_stat_error
;
7566 /* Do the parameters of "upa" match those of "space"?
7568 static isl_bool
isl_union_pw_aff_matching_params(
7569 __isl_keep isl_union_pw_aff
*upa
, __isl_keep isl_space
*space
)
7571 isl_space
*upa_space
;
7575 return isl_bool_error
;
7577 upa_space
= isl_union_pw_aff_get_space(upa
);
7579 match
= isl_space_has_equal_params(space
, upa_space
);
7581 isl_space_free(upa_space
);
7585 /* Internal data structure for isl_union_pw_aff_reset_domain_space.
7586 * space represents the new parameters.
7587 * res collects the results.
7589 struct isl_union_pw_aff_reset_params_data
{
7591 isl_union_pw_aff
*res
;
7594 /* Replace the parameters of "pa" by data->space and
7595 * add the result to data->res.
7597 static isl_stat
reset_params(__isl_take isl_pw_aff
*pa
, void *user
)
7599 struct isl_union_pw_aff_reset_params_data
*data
= user
;
7602 space
= isl_pw_aff_get_space(pa
);
7603 space
= isl_space_replace_params(space
, data
->space
);
7604 pa
= isl_pw_aff_reset_space(pa
, space
);
7605 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
7607 return data
->res
? isl_stat_ok
: isl_stat_error
;
7610 /* Replace the domain space of "upa" by "space".
7611 * Since a union expression does not have a (single) domain space,
7612 * "space" is necessarily a parameter space.
7614 * Since the order and the names of the parameters determine
7615 * the hash value, we need to create a new hash table.
7617 static __isl_give isl_union_pw_aff
*isl_union_pw_aff_reset_domain_space(
7618 __isl_take isl_union_pw_aff
*upa
, __isl_take isl_space
*space
)
7620 struct isl_union_pw_aff_reset_params_data data
= { space
};
7623 match
= isl_union_pw_aff_matching_params(upa
, space
);
7625 upa
= isl_union_pw_aff_free(upa
);
7627 isl_space_free(space
);
7631 data
.res
= isl_union_pw_aff_empty(isl_space_copy(space
));
7632 if (isl_union_pw_aff_foreach_pw_aff(upa
, &reset_params
, &data
) < 0)
7633 data
.res
= isl_union_pw_aff_free(data
.res
);
7635 isl_union_pw_aff_free(upa
);
7636 isl_space_free(space
);
7640 /* Return the floor of "pa".
7642 static __isl_give isl_pw_aff
*floor_entry(__isl_take isl_pw_aff
*pa
, void *user
)
7644 return isl_pw_aff_floor(pa
);
7647 /* Given f, return floor(f).
7649 __isl_give isl_union_pw_aff
*isl_union_pw_aff_floor(
7650 __isl_take isl_union_pw_aff
*upa
)
7652 return isl_union_pw_aff_transform_inplace(upa
, &floor_entry
, NULL
);
7657 * upa mod m = upa - m * floor(upa/m)
7659 * with m an integer value.
7661 __isl_give isl_union_pw_aff
*isl_union_pw_aff_mod_val(
7662 __isl_take isl_union_pw_aff
*upa
, __isl_take isl_val
*m
)
7664 isl_union_pw_aff
*res
;
7669 if (!isl_val_is_int(m
))
7670 isl_die(isl_val_get_ctx(m
), isl_error_invalid
,
7671 "expecting integer modulo", goto error
);
7672 if (!isl_val_is_pos(m
))
7673 isl_die(isl_val_get_ctx(m
), isl_error_invalid
,
7674 "expecting positive modulo", goto error
);
7676 res
= isl_union_pw_aff_copy(upa
);
7677 upa
= isl_union_pw_aff_scale_down_val(upa
, isl_val_copy(m
));
7678 upa
= isl_union_pw_aff_floor(upa
);
7679 upa
= isl_union_pw_aff_scale_val(upa
, m
);
7680 res
= isl_union_pw_aff_sub(res
, upa
);
7685 isl_union_pw_aff_free(upa
);
7689 /* Internal data structure for isl_union_pw_multi_aff_get_union_pw_aff.
7690 * pos is the output position that needs to be extracted.
7691 * res collects the results.
7693 struct isl_union_pw_multi_aff_get_union_pw_aff_data
{
7695 isl_union_pw_aff
*res
;
7698 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma"
7699 * (assuming it has such a dimension) and add it to data->res.
7701 static isl_stat
get_union_pw_aff(__isl_take isl_pw_multi_aff
*pma
, void *user
)
7703 struct isl_union_pw_multi_aff_get_union_pw_aff_data
*data
= user
;
7707 n_out
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
7709 return isl_stat_error
;
7710 if (data
->pos
>= n_out
) {
7711 isl_pw_multi_aff_free(pma
);
7715 pa
= isl_pw_multi_aff_get_pw_aff(pma
, data
->pos
);
7716 isl_pw_multi_aff_free(pma
);
7718 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
7720 return data
->res
? isl_stat_ok
: isl_stat_error
;
7723 /* Extract an isl_union_pw_aff corresponding to
7724 * output dimension "pos" of "upma".
7726 __isl_give isl_union_pw_aff
*isl_union_pw_multi_aff_get_union_pw_aff(
7727 __isl_keep isl_union_pw_multi_aff
*upma
, int pos
)
7729 struct isl_union_pw_multi_aff_get_union_pw_aff_data data
;
7736 isl_die(isl_union_pw_multi_aff_get_ctx(upma
), isl_error_invalid
,
7737 "cannot extract at negative position", return NULL
);
7739 space
= isl_union_pw_multi_aff_get_space(upma
);
7740 data
.res
= isl_union_pw_aff_empty(space
);
7742 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma
,
7743 &get_union_pw_aff
, &data
) < 0)
7744 data
.res
= isl_union_pw_aff_free(data
.res
);
7749 /* Return a union piecewise affine expression
7750 * that is equal to "aff" on "domain".
7752 __isl_give isl_union_pw_aff
*isl_union_pw_aff_aff_on_domain(
7753 __isl_take isl_union_set
*domain
, __isl_take isl_aff
*aff
)
7757 pa
= isl_pw_aff_from_aff(aff
);
7758 return isl_union_pw_aff_pw_aff_on_domain(domain
, pa
);
7761 /* Return a union piecewise affine expression
7762 * that is equal to the parameter identified by "id" on "domain".
7764 * Make sure the parameter appears in the space passed to
7765 * isl_aff_param_on_domain_space_id.
7767 __isl_give isl_union_pw_aff
*isl_union_pw_aff_param_on_domain_id(
7768 __isl_take isl_union_set
*domain
, __isl_take isl_id
*id
)
7773 space
= isl_union_set_get_space(domain
);
7774 space
= isl_space_add_param_id(space
, isl_id_copy(id
));
7775 aff
= isl_aff_param_on_domain_space_id(space
, id
);
7776 return isl_union_pw_aff_aff_on_domain(domain
, aff
);
7779 /* Internal data structure for isl_union_pw_aff_pw_aff_on_domain.
7780 * "pa" is the piecewise symbolic value that the resulting isl_union_pw_aff
7782 * "res" collects the results.
7784 struct isl_union_pw_aff_pw_aff_on_domain_data
{
7786 isl_union_pw_aff
*res
;
7789 /* Construct a piecewise affine expression that is equal to data->pa
7790 * on "domain" and add the result to data->res.
7792 static isl_stat
pw_aff_on_domain(__isl_take isl_set
*domain
, void *user
)
7794 struct isl_union_pw_aff_pw_aff_on_domain_data
*data
= user
;
7798 pa
= isl_pw_aff_copy(data
->pa
);
7799 dim
= isl_set_dim(domain
, isl_dim_set
);
7801 pa
= isl_pw_aff_free(pa
);
7802 pa
= isl_pw_aff_from_range(pa
);
7803 pa
= isl_pw_aff_add_dims(pa
, isl_dim_in
, dim
);
7804 pa
= isl_pw_aff_reset_domain_space(pa
, isl_set_get_space(domain
));
7805 pa
= isl_pw_aff_intersect_domain(pa
, domain
);
7806 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
7808 return data
->res
? isl_stat_ok
: isl_stat_error
;
7811 /* Return a union piecewise affine expression
7812 * that is equal to "pa" on "domain", assuming "domain" and "pa"
7813 * have been aligned.
7815 * Construct an isl_pw_aff on each of the sets in "domain" and
7816 * collect the results.
7818 static __isl_give isl_union_pw_aff
*isl_union_pw_aff_pw_aff_on_domain_aligned(
7819 __isl_take isl_union_set
*domain
, __isl_take isl_pw_aff
*pa
)
7821 struct isl_union_pw_aff_pw_aff_on_domain_data data
;
7824 space
= isl_union_set_get_space(domain
);
7825 data
.res
= isl_union_pw_aff_empty(space
);
7827 if (isl_union_set_foreach_set(domain
, &pw_aff_on_domain
, &data
) < 0)
7828 data
.res
= isl_union_pw_aff_free(data
.res
);
7829 isl_union_set_free(domain
);
7830 isl_pw_aff_free(pa
);
7834 /* Return a union piecewise affine expression
7835 * that is equal to "pa" on "domain".
7837 * Check that "pa" is a parametric expression,
7838 * align the parameters if needed and call
7839 * isl_union_pw_aff_pw_aff_on_domain_aligned.
7841 __isl_give isl_union_pw_aff
*isl_union_pw_aff_pw_aff_on_domain(
7842 __isl_take isl_union_set
*domain
, __isl_take isl_pw_aff
*pa
)
7845 isl_bool equal_params
;
7846 isl_space
*domain_space
, *pa_space
;
7848 pa_space
= isl_pw_aff_peek_space(pa
);
7849 is_set
= isl_space_is_set(pa_space
);
7853 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
7854 "expecting parametric expression", goto error
);
7856 domain_space
= isl_union_set_get_space(domain
);
7857 pa_space
= isl_pw_aff_get_space(pa
);
7858 equal_params
= isl_space_has_equal_params(domain_space
, pa_space
);
7859 if (equal_params
>= 0 && !equal_params
) {
7862 space
= isl_space_align_params(domain_space
, pa_space
);
7863 pa
= isl_pw_aff_align_params(pa
, isl_space_copy(space
));
7864 domain
= isl_union_set_align_params(domain
, space
);
7866 isl_space_free(domain_space
);
7867 isl_space_free(pa_space
);
7870 if (equal_params
< 0)
7872 return isl_union_pw_aff_pw_aff_on_domain_aligned(domain
, pa
);
7874 isl_union_set_free(domain
);
7875 isl_pw_aff_free(pa
);
7879 /* Internal data structure for isl_union_pw_aff_val_on_domain.
7880 * "v" is the value that the resulting isl_union_pw_aff needs to attain.
7881 * "res" collects the results.
7883 struct isl_union_pw_aff_val_on_domain_data
{
7885 isl_union_pw_aff
*res
;
7888 /* Construct a piecewise affine expression that is equal to data->v
7889 * on "domain" and add the result to data->res.
7891 static isl_stat
pw_aff_val_on_domain(__isl_take isl_set
*domain
, void *user
)
7893 struct isl_union_pw_aff_val_on_domain_data
*data
= user
;
7897 v
= isl_val_copy(data
->v
);
7898 pa
= isl_pw_aff_val_on_domain(domain
, v
);
7899 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
7901 return data
->res
? isl_stat_ok
: isl_stat_error
;
7904 /* Return a union piecewise affine expression
7905 * that is equal to "v" on "domain".
7907 * Construct an isl_pw_aff on each of the sets in "domain" and
7908 * collect the results.
7910 __isl_give isl_union_pw_aff
*isl_union_pw_aff_val_on_domain(
7911 __isl_take isl_union_set
*domain
, __isl_take isl_val
*v
)
7913 struct isl_union_pw_aff_val_on_domain_data data
;
7916 space
= isl_union_set_get_space(domain
);
7917 data
.res
= isl_union_pw_aff_empty(space
);
7919 if (isl_union_set_foreach_set(domain
, &pw_aff_val_on_domain
, &data
) < 0)
7920 data
.res
= isl_union_pw_aff_free(data
.res
);
7921 isl_union_set_free(domain
);
7926 /* Construct a piecewise multi affine expression
7927 * that is equal to "pa" and add it to upma.
7929 static isl_stat
pw_multi_aff_from_pw_aff_entry(__isl_take isl_pw_aff
*pa
,
7932 isl_union_pw_multi_aff
**upma
= user
;
7933 isl_pw_multi_aff
*pma
;
7935 pma
= isl_pw_multi_aff_from_pw_aff(pa
);
7936 *upma
= isl_union_pw_multi_aff_add_pw_multi_aff(*upma
, pma
);
7938 return *upma
? isl_stat_ok
: isl_stat_error
;
7941 /* Construct and return a union piecewise multi affine expression
7942 * that is equal to the given union piecewise affine expression.
7944 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_union_pw_aff(
7945 __isl_take isl_union_pw_aff
*upa
)
7948 isl_union_pw_multi_aff
*upma
;
7953 space
= isl_union_pw_aff_get_space(upa
);
7954 upma
= isl_union_pw_multi_aff_empty(space
);
7956 if (isl_union_pw_aff_foreach_pw_aff(upa
,
7957 &pw_multi_aff_from_pw_aff_entry
, &upma
) < 0)
7958 upma
= isl_union_pw_multi_aff_free(upma
);
7960 isl_union_pw_aff_free(upa
);
7964 /* Compute the set of elements in the domain of "pa" where it is zero and
7965 * add this set to "uset".
7967 static isl_stat
zero_union_set(__isl_take isl_pw_aff
*pa
, void *user
)
7969 isl_union_set
**uset
= (isl_union_set
**)user
;
7971 *uset
= isl_union_set_add_set(*uset
, isl_pw_aff_zero_set(pa
));
7973 return *uset
? isl_stat_ok
: isl_stat_error
;
7976 /* Return a union set containing those elements in the domain
7977 * of "upa" where it is zero.
7979 __isl_give isl_union_set
*isl_union_pw_aff_zero_union_set(
7980 __isl_take isl_union_pw_aff
*upa
)
7982 isl_union_set
*zero
;
7984 zero
= isl_union_set_empty(isl_union_pw_aff_get_space(upa
));
7985 if (isl_union_pw_aff_foreach_pw_aff(upa
, &zero_union_set
, &zero
) < 0)
7986 zero
= isl_union_set_free(zero
);
7988 isl_union_pw_aff_free(upa
);
7992 /* Internal data structure for isl_union_pw_aff_bind_id,
7993 * storing the parameter that needs to be bound and
7994 * the accumulated results.
7996 struct isl_bind_id_data
{
7998 isl_union_set
*bound
;
8001 /* Bind the piecewise affine function "pa" to the parameter data->id,
8002 * adding the resulting elements in the domain where the expression
8003 * is equal to the parameter to data->bound.
8005 static isl_stat
bind_id(__isl_take isl_pw_aff
*pa
, void *user
)
8007 struct isl_bind_id_data
*data
= user
;
8010 bound
= isl_pw_aff_bind_id(pa
, isl_id_copy(data
->id
));
8011 data
->bound
= isl_union_set_add_set(data
->bound
, bound
);
8013 return data
->bound
? isl_stat_ok
: isl_stat_error
;
8016 /* Bind the union piecewise affine function "upa" to the parameter "id",
8017 * returning the elements in the domain where the expression
8018 * is equal to the parameter.
8020 __isl_give isl_union_set
*isl_union_pw_aff_bind_id(
8021 __isl_take isl_union_pw_aff
*upa
, __isl_take isl_id
*id
)
8023 struct isl_bind_id_data data
= { id
};
8025 data
.bound
= isl_union_set_empty(isl_union_pw_aff_get_space(upa
));
8026 if (isl_union_pw_aff_foreach_pw_aff(upa
, &bind_id
, &data
) < 0)
8027 data
.bound
= isl_union_set_free(data
.bound
);
8029 isl_union_pw_aff_free(upa
);
8034 /* Internal data structure for isl_union_pw_aff_pullback_union_pw_multi_aff.
8035 * upma is the function that is plugged in.
8036 * pa is the current part of the function in which upma is plugged in.
8037 * res collects the results.
8039 struct isl_union_pw_aff_pullback_upma_data
{
8040 isl_union_pw_multi_aff
*upma
;
8042 isl_union_pw_aff
*res
;
8045 /* Check if "pma" can be plugged into data->pa.
8046 * If so, perform the pullback and add the result to data->res.
8048 static isl_stat
pa_pb_pma(__isl_take isl_pw_multi_aff
*pma
, void *user
)
8050 struct isl_union_pw_aff_pullback_upma_data
*data
= user
;
8053 if (!isl_space_tuple_is_equal(data
->pa
->dim
, isl_dim_in
,
8054 pma
->dim
, isl_dim_out
)) {
8055 isl_pw_multi_aff_free(pma
);
8059 pa
= isl_pw_aff_copy(data
->pa
);
8060 pa
= isl_pw_aff_pullback_pw_multi_aff(pa
, pma
);
8062 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
8064 return data
->res
? isl_stat_ok
: isl_stat_error
;
8067 /* Check if any of the elements of data->upma can be plugged into pa,
8068 * add if so add the result to data->res.
8070 static isl_stat
upa_pb_upma(__isl_take isl_pw_aff
*pa
, void *user
)
8072 struct isl_union_pw_aff_pullback_upma_data
*data
= user
;
8076 r
= isl_union_pw_multi_aff_foreach_pw_multi_aff(data
->upma
,
8078 isl_pw_aff_free(pa
);
8083 /* Compute the pullback of "upa" by the function represented by "upma".
8084 * In other words, plug in "upma" in "upa". The result contains
8085 * expressions defined over the domain space of "upma".
8087 * Run over all pairs of elements in "upa" and "upma", perform
8088 * the pullback when appropriate and collect the results.
8089 * If the hash value were based on the domain space rather than
8090 * the function space, then we could run through all elements
8091 * of "upma" and directly pick out the corresponding element of "upa".
8093 __isl_give isl_union_pw_aff
*isl_union_pw_aff_pullback_union_pw_multi_aff(
8094 __isl_take isl_union_pw_aff
*upa
,
8095 __isl_take isl_union_pw_multi_aff
*upma
)
8097 struct isl_union_pw_aff_pullback_upma_data data
= { NULL
, NULL
};
8100 space
= isl_union_pw_multi_aff_get_space(upma
);
8101 upa
= isl_union_pw_aff_align_params(upa
, space
);
8102 space
= isl_union_pw_aff_get_space(upa
);
8103 upma
= isl_union_pw_multi_aff_align_params(upma
, space
);
8109 data
.res
= isl_union_pw_aff_alloc_same_size(upa
);
8110 if (isl_union_pw_aff_foreach_pw_aff(upa
, &upa_pb_upma
, &data
) < 0)
8111 data
.res
= isl_union_pw_aff_free(data
.res
);
8113 isl_union_pw_aff_free(upa
);
8114 isl_union_pw_multi_aff_free(upma
);
8117 isl_union_pw_aff_free(upa
);
8118 isl_union_pw_multi_aff_free(upma
);
8123 #define BASE union_pw_aff
8125 #define DOMBASE union_set
8127 #include <isl_multi_explicit_domain.c>
8128 #include <isl_multi_union_pw_aff_explicit_domain.c>
8129 #include <isl_multi_templ.c>
8130 #include <isl_multi_apply_set.c>
8131 #include <isl_multi_apply_union_set.c>
8132 #include <isl_multi_arith_templ.c>
8133 #include <isl_multi_coalesce.c>
8134 #include <isl_multi_dim_id_templ.c>
8135 #include <isl_multi_floor.c>
8136 #include <isl_multi_from_base_templ.c>
8137 #include <isl_multi_gist.c>
8138 #include <isl_multi_align_set.c>
8139 #include <isl_multi_align_union_set.c>
8140 #include <isl_multi_intersect.c>
8141 #include <isl_multi_nan_templ.c>
8142 #include <isl_multi_tuple_id_templ.c>
8144 /* Does "mupa" have a non-trivial explicit domain?
8146 * The explicit domain, if present, is trivial if it represents
8147 * an (obviously) universe parameter set.
8149 isl_bool
isl_multi_union_pw_aff_has_non_trivial_domain(
8150 __isl_keep isl_multi_union_pw_aff
*mupa
)
8152 isl_bool is_params
, trivial
;
8156 return isl_bool_error
;
8157 if (!isl_multi_union_pw_aff_has_explicit_domain(mupa
))
8158 return isl_bool_false
;
8159 is_params
= isl_union_set_is_params(mupa
->u
.dom
);
8160 if (is_params
< 0 || !is_params
)
8161 return isl_bool_not(is_params
);
8162 set
= isl_set_from_union_set(isl_union_set_copy(mupa
->u
.dom
));
8163 trivial
= isl_set_plain_is_universe(set
);
8165 return isl_bool_not(trivial
);
8168 /* Construct a multiple union piecewise affine expression
8169 * in the given space with value zero in each of the output dimensions.
8171 * Since there is no canonical zero value for
8172 * a union piecewise affine expression, we can only construct
8173 * a zero-dimensional "zero" value.
8175 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_zero(
8176 __isl_take isl_space
*space
)
8184 params
= isl_space_is_params(space
);
8188 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
8189 "expecting proper set space", goto error
);
8190 if (!isl_space_is_set(space
))
8191 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
8192 "expecting set space", goto error
);
8193 dim
= isl_space_dim(space
, isl_dim_out
);
8197 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
8198 "expecting 0D space", goto error
);
8200 return isl_multi_union_pw_aff_alloc(space
);
8202 isl_space_free(space
);
8206 /* Compute the sum of "mupa1" and "mupa2" on the union of their domains,
8207 * with the actual sum on the shared domain and
8208 * the defined expression on the symmetric difference of the domains.
8210 * We simply iterate over the elements in both arguments and
8211 * call isl_union_pw_aff_union_add on each of them, if there is
8212 * at least one element.
8214 * Otherwise, the two expressions have an explicit domain and
8215 * the union of these explicit domains is computed.
8216 * This assumes that the explicit domains are either both in terms
8217 * of specific domains elements or both in terms of parameters.
8218 * However, if one of the expressions does not have any constraints
8219 * on its explicit domain, then this is allowed as well and the result
8220 * is the expression with no constraints on its explicit domain.
8222 static __isl_give isl_multi_union_pw_aff
*
8223 isl_multi_union_pw_aff_union_add_aligned(
8224 __isl_take isl_multi_union_pw_aff
*mupa1
,
8225 __isl_take isl_multi_union_pw_aff
*mupa2
)
8227 isl_bool has_domain
, is_params1
, is_params2
;
8229 if (isl_multi_union_pw_aff_check_equal_space(mupa1
, mupa2
) < 0)
8232 return isl_multi_union_pw_aff_bin_op(mupa1
, mupa2
,
8233 &isl_union_pw_aff_union_add
);
8234 if (isl_multi_union_pw_aff_check_has_explicit_domain(mupa1
) < 0 ||
8235 isl_multi_union_pw_aff_check_has_explicit_domain(mupa2
) < 0)
8238 has_domain
= isl_multi_union_pw_aff_has_non_trivial_domain(mupa1
);
8242 isl_multi_union_pw_aff_free(mupa2
);
8245 has_domain
= isl_multi_union_pw_aff_has_non_trivial_domain(mupa2
);
8249 isl_multi_union_pw_aff_free(mupa1
);
8253 is_params1
= isl_union_set_is_params(mupa1
->u
.dom
);
8254 is_params2
= isl_union_set_is_params(mupa2
->u
.dom
);
8255 if (is_params1
< 0 || is_params2
< 0)
8257 if (is_params1
!= is_params2
)
8258 isl_die(isl_multi_union_pw_aff_get_ctx(mupa1
),
8260 "cannot compute union of concrete domain and "
8261 "parameter constraints", goto error
);
8262 mupa1
= isl_multi_union_pw_aff_cow(mupa1
);
8265 mupa1
->u
.dom
= isl_union_set_union(mupa1
->u
.dom
,
8266 isl_union_set_copy(mupa2
->u
.dom
));
8269 isl_multi_union_pw_aff_free(mupa2
);
8272 isl_multi_union_pw_aff_free(mupa1
);
8273 isl_multi_union_pw_aff_free(mupa2
);
8277 /* Compute the sum of "mupa1" and "mupa2" on the union of their domains,
8278 * with the actual sum on the shared domain and
8279 * the defined expression on the symmetric difference of the domains.
8281 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_union_add(
8282 __isl_take isl_multi_union_pw_aff
*mupa1
,
8283 __isl_take isl_multi_union_pw_aff
*mupa2
)
8285 return isl_multi_union_pw_aff_align_params_multi_multi_and(mupa1
, mupa2
,
8286 &isl_multi_union_pw_aff_union_add_aligned
);
8289 /* Construct and return a multi union piecewise affine expression
8290 * that is equal to the given multi affine expression.
8292 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_from_multi_aff(
8293 __isl_take isl_multi_aff
*ma
)
8295 isl_multi_pw_aff
*mpa
;
8297 mpa
= isl_multi_pw_aff_from_multi_aff(ma
);
8298 return isl_multi_union_pw_aff_from_multi_pw_aff(mpa
);
8301 /* Construct and return a multi union piecewise affine expression
8302 * that is equal to the given multi piecewise affine expression.
8304 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_from_multi_pw_aff(
8305 __isl_take isl_multi_pw_aff
*mpa
)
8310 isl_multi_union_pw_aff
*mupa
;
8312 n
= isl_multi_pw_aff_dim(mpa
, isl_dim_out
);
8314 mpa
= isl_multi_pw_aff_free(mpa
);
8318 space
= isl_multi_pw_aff_get_space(mpa
);
8319 space
= isl_space_range(space
);
8320 mupa
= isl_multi_union_pw_aff_alloc(space
);
8322 for (i
= 0; i
< n
; ++i
) {
8324 isl_union_pw_aff
*upa
;
8326 pa
= isl_multi_pw_aff_get_pw_aff(mpa
, i
);
8327 upa
= isl_union_pw_aff_from_pw_aff(pa
);
8328 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
8331 isl_multi_pw_aff_free(mpa
);
8336 /* Extract the range space of "pma" and assign it to *space.
8337 * If *space has already been set (through a previous call to this function),
8338 * then check that the range space is the same.
8340 static isl_stat
extract_space(__isl_take isl_pw_multi_aff
*pma
, void *user
)
8342 isl_space
**space
= user
;
8343 isl_space
*pma_space
;
8346 pma_space
= isl_space_range(isl_pw_multi_aff_get_space(pma
));
8347 isl_pw_multi_aff_free(pma
);
8350 return isl_stat_error
;
8356 equal
= isl_space_is_equal(pma_space
, *space
);
8357 isl_space_free(pma_space
);
8360 return isl_stat_error
;
8362 isl_die(isl_space_get_ctx(*space
), isl_error_invalid
,
8363 "range spaces not the same", return isl_stat_error
);
8367 /* Construct and return a multi union piecewise affine expression
8368 * that is equal to the given union piecewise multi affine expression.
8370 * In order to be able to perform the conversion, the input
8371 * needs to be non-empty and may only involve a single range space.
8373 * If the resulting multi union piecewise affine expression has
8374 * an explicit domain, then assign it the domain of the input.
8375 * In other cases, the domain is stored in the individual elements.
8377 __isl_give isl_multi_union_pw_aff
*
8378 isl_multi_union_pw_aff_from_union_pw_multi_aff(
8379 __isl_take isl_union_pw_multi_aff
*upma
)
8381 isl_space
*space
= NULL
;
8382 isl_multi_union_pw_aff
*mupa
;
8386 n
= isl_union_pw_multi_aff_n_pw_multi_aff(upma
);
8390 isl_die(isl_union_pw_multi_aff_get_ctx(upma
), isl_error_invalid
,
8391 "cannot extract range space from empty input",
8393 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma
, &extract_space
,
8400 n
= isl_space_dim(space
, isl_dim_set
);
8402 space
= isl_space_free(space
);
8403 mupa
= isl_multi_union_pw_aff_alloc(space
);
8405 for (i
= 0; i
< n
; ++i
) {
8406 isl_union_pw_aff
*upa
;
8408 upa
= isl_union_pw_multi_aff_get_union_pw_aff(upma
, i
);
8409 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
8411 if (isl_multi_union_pw_aff_has_explicit_domain(mupa
)) {
8413 isl_union_pw_multi_aff
*copy
;
8415 copy
= isl_union_pw_multi_aff_copy(upma
);
8416 dom
= isl_union_pw_multi_aff_domain(copy
);
8417 mupa
= isl_multi_union_pw_aff_intersect_domain(mupa
, dom
);
8420 isl_union_pw_multi_aff_free(upma
);
8423 isl_space_free(space
);
8424 isl_union_pw_multi_aff_free(upma
);
8428 /* Try and create an isl_multi_union_pw_aff that is equivalent
8429 * to the given isl_union_map.
8430 * The isl_union_map is required to be single-valued in each space.
8431 * Moreover, it cannot be empty and all range spaces need to be the same.
8432 * Otherwise, an error is produced.
8434 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_from_union_map(
8435 __isl_take isl_union_map
*umap
)
8437 isl_union_pw_multi_aff
*upma
;
8439 upma
= isl_union_pw_multi_aff_from_union_map(umap
);
8440 return isl_multi_union_pw_aff_from_union_pw_multi_aff(upma
);
8443 /* Return a multiple union piecewise affine expression
8444 * that is equal to "mv" on "domain", assuming "domain" and "mv"
8445 * have been aligned.
8447 * If the resulting multi union piecewise affine expression has
8448 * an explicit domain, then assign it the input domain.
8449 * In other cases, the domain is stored in the individual elements.
8451 static __isl_give isl_multi_union_pw_aff
*
8452 isl_multi_union_pw_aff_multi_val_on_domain_aligned(
8453 __isl_take isl_union_set
*domain
, __isl_take isl_multi_val
*mv
)
8458 isl_multi_union_pw_aff
*mupa
;
8460 n
= isl_multi_val_dim(mv
, isl_dim_set
);
8461 if (!domain
|| n
< 0)
8464 space
= isl_multi_val_get_space(mv
);
8465 mupa
= isl_multi_union_pw_aff_alloc(space
);
8466 for (i
= 0; i
< n
; ++i
) {
8468 isl_union_pw_aff
*upa
;
8470 v
= isl_multi_val_get_val(mv
, i
);
8471 upa
= isl_union_pw_aff_val_on_domain(isl_union_set_copy(domain
),
8473 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
8475 if (isl_multi_union_pw_aff_has_explicit_domain(mupa
))
8476 mupa
= isl_multi_union_pw_aff_intersect_domain(mupa
,
8477 isl_union_set_copy(domain
));
8479 isl_union_set_free(domain
);
8480 isl_multi_val_free(mv
);
8483 isl_union_set_free(domain
);
8484 isl_multi_val_free(mv
);
8488 /* Return a multiple union piecewise affine expression
8489 * that is equal to "mv" on "domain".
8491 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_multi_val_on_domain(
8492 __isl_take isl_union_set
*domain
, __isl_take isl_multi_val
*mv
)
8494 isl_bool equal_params
;
8498 equal_params
= isl_space_has_equal_params(domain
->dim
, mv
->space
);
8499 if (equal_params
< 0)
8502 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(
8504 domain
= isl_union_set_align_params(domain
,
8505 isl_multi_val_get_space(mv
));
8506 mv
= isl_multi_val_align_params(mv
, isl_union_set_get_space(domain
));
8507 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(domain
, mv
);
8509 isl_union_set_free(domain
);
8510 isl_multi_val_free(mv
);
8514 /* Return a multiple union piecewise affine expression
8515 * that is equal to "ma" on "domain".
8517 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_multi_aff_on_domain(
8518 __isl_take isl_union_set
*domain
, __isl_take isl_multi_aff
*ma
)
8520 isl_pw_multi_aff
*pma
;
8522 pma
= isl_pw_multi_aff_from_multi_aff(ma
);
8523 return isl_multi_union_pw_aff_pw_multi_aff_on_domain(domain
, pma
);
8526 /* Return a multiple union piecewise affine expression
8527 * that is equal to "pma" on "domain", assuming "domain" and "pma"
8528 * have been aligned.
8530 * If the resulting multi union piecewise affine expression has
8531 * an explicit domain, then assign it the input domain.
8532 * In other cases, the domain is stored in the individual elements.
8534 static __isl_give isl_multi_union_pw_aff
*
8535 isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(
8536 __isl_take isl_union_set
*domain
, __isl_take isl_pw_multi_aff
*pma
)
8541 isl_multi_union_pw_aff
*mupa
;
8543 n
= isl_pw_multi_aff_dim(pma
, isl_dim_set
);
8544 if (!domain
|| n
< 0)
8546 space
= isl_pw_multi_aff_get_space(pma
);
8547 mupa
= isl_multi_union_pw_aff_alloc(space
);
8548 for (i
= 0; i
< n
; ++i
) {
8550 isl_union_pw_aff
*upa
;
8552 pa
= isl_pw_multi_aff_get_pw_aff(pma
, i
);
8553 upa
= isl_union_pw_aff_pw_aff_on_domain(
8554 isl_union_set_copy(domain
), pa
);
8555 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
8557 if (isl_multi_union_pw_aff_has_explicit_domain(mupa
))
8558 mupa
= isl_multi_union_pw_aff_intersect_domain(mupa
,
8559 isl_union_set_copy(domain
));
8561 isl_union_set_free(domain
);
8562 isl_pw_multi_aff_free(pma
);
8565 isl_union_set_free(domain
);
8566 isl_pw_multi_aff_free(pma
);
8570 /* Return a multiple union piecewise affine expression
8571 * that is equal to "pma" on "domain".
8573 __isl_give isl_multi_union_pw_aff
*
8574 isl_multi_union_pw_aff_pw_multi_aff_on_domain(__isl_take isl_union_set
*domain
,
8575 __isl_take isl_pw_multi_aff
*pma
)
8577 isl_bool equal_params
;
8580 space
= isl_pw_multi_aff_peek_space(pma
);
8581 equal_params
= isl_union_set_space_has_equal_params(domain
, space
);
8582 if (equal_params
< 0)
8585 return isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(
8587 domain
= isl_union_set_align_params(domain
,
8588 isl_pw_multi_aff_get_space(pma
));
8589 pma
= isl_pw_multi_aff_align_params(pma
,
8590 isl_union_set_get_space(domain
));
8591 return isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(domain
,
8594 isl_union_set_free(domain
);
8595 isl_pw_multi_aff_free(pma
);
8599 /* Return a union set containing those elements in the domains
8600 * of the elements of "mupa" where they are all zero.
8602 * If there are no elements, then simply return the entire domain.
8604 __isl_give isl_union_set
*isl_multi_union_pw_aff_zero_union_set(
8605 __isl_take isl_multi_union_pw_aff
*mupa
)
8609 isl_union_pw_aff
*upa
;
8610 isl_union_set
*zero
;
8612 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8614 mupa
= isl_multi_union_pw_aff_free(mupa
);
8619 return isl_multi_union_pw_aff_domain(mupa
);
8621 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, 0);
8622 zero
= isl_union_pw_aff_zero_union_set(upa
);
8624 for (i
= 1; i
< n
; ++i
) {
8625 isl_union_set
*zero_i
;
8627 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
8628 zero_i
= isl_union_pw_aff_zero_union_set(upa
);
8630 zero
= isl_union_set_intersect(zero
, zero_i
);
8633 isl_multi_union_pw_aff_free(mupa
);
8637 /* Construct a union map mapping the shared domain
8638 * of the union piecewise affine expressions to the range of "mupa"
8639 * in the special case of a 0D multi union piecewise affine expression.
8641 * Construct a map between the explicit domain of "mupa" and
8643 * Note that this assumes that the domain consists of explicit elements.
8645 static __isl_give isl_union_map
*isl_union_map_from_multi_union_pw_aff_0D(
8646 __isl_take isl_multi_union_pw_aff
*mupa
)
8650 isl_union_set
*dom
, *ran
;
8652 space
= isl_multi_union_pw_aff_get_space(mupa
);
8653 dom
= isl_multi_union_pw_aff_domain(mupa
);
8654 ran
= isl_union_set_from_set(isl_set_universe(space
));
8656 is_params
= isl_union_set_is_params(dom
);
8658 dom
= isl_union_set_free(dom
);
8660 isl_die(isl_union_set_get_ctx(dom
), isl_error_invalid
,
8661 "cannot create union map from expression without "
8662 "explicit domain elements",
8663 dom
= isl_union_set_free(dom
));
8665 return isl_union_map_from_domain_and_range(dom
, ran
);
8668 /* Construct a union map mapping the shared domain
8669 * of the union piecewise affine expressions to the range of "mupa"
8670 * with each dimension in the range equated to the
8671 * corresponding union piecewise affine expression.
8673 * If the input is zero-dimensional, then construct a mapping
8674 * from its explicit domain.
8676 __isl_give isl_union_map
*isl_union_map_from_multi_union_pw_aff(
8677 __isl_take isl_multi_union_pw_aff
*mupa
)
8682 isl_union_map
*umap
;
8683 isl_union_pw_aff
*upa
;
8685 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8687 mupa
= isl_multi_union_pw_aff_free(mupa
);
8692 return isl_union_map_from_multi_union_pw_aff_0D(mupa
);
8694 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, 0);
8695 umap
= isl_union_map_from_union_pw_aff(upa
);
8697 for (i
= 1; i
< n
; ++i
) {
8698 isl_union_map
*umap_i
;
8700 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
8701 umap_i
= isl_union_map_from_union_pw_aff(upa
);
8702 umap
= isl_union_map_flat_range_product(umap
, umap_i
);
8705 space
= isl_multi_union_pw_aff_get_space(mupa
);
8706 umap
= isl_union_map_reset_range_space(umap
, space
);
8708 isl_multi_union_pw_aff_free(mupa
);
8712 /* Internal data structure for isl_union_pw_multi_aff_reset_range_space.
8713 * "range" is the space from which to set the range space.
8714 * "res" collects the results.
8716 struct isl_union_pw_multi_aff_reset_range_space_data
{
8718 isl_union_pw_multi_aff
*res
;
8721 /* Replace the range space of "pma" by the range space of data->range and
8722 * add the result to data->res.
8724 static isl_stat
reset_range_space(__isl_take isl_pw_multi_aff
*pma
, void *user
)
8726 struct isl_union_pw_multi_aff_reset_range_space_data
*data
= user
;
8729 space
= isl_pw_multi_aff_get_space(pma
);
8730 space
= isl_space_domain(space
);
8731 space
= isl_space_extend_domain_with_range(space
,
8732 isl_space_copy(data
->range
));
8733 pma
= isl_pw_multi_aff_reset_space(pma
, space
);
8734 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma
);
8736 return data
->res
? isl_stat_ok
: isl_stat_error
;
8739 /* Replace the range space of all the piecewise affine expressions in "upma" by
8740 * the range space of "space".
8742 * This assumes that all these expressions have the same output dimension.
8744 * Since the spaces of the expressions change, so do their hash values.
8745 * We therefore need to create a new isl_union_pw_multi_aff.
8746 * Note that the hash value is currently computed based on the entire
8747 * space even though there can only be a single expression with a given
8750 static __isl_give isl_union_pw_multi_aff
*
8751 isl_union_pw_multi_aff_reset_range_space(
8752 __isl_take isl_union_pw_multi_aff
*upma
, __isl_take isl_space
*space
)
8754 struct isl_union_pw_multi_aff_reset_range_space_data data
= { space
};
8755 isl_space
*space_upma
;
8757 space_upma
= isl_union_pw_multi_aff_get_space(upma
);
8758 data
.res
= isl_union_pw_multi_aff_empty(space_upma
);
8759 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma
,
8760 &reset_range_space
, &data
) < 0)
8761 data
.res
= isl_union_pw_multi_aff_free(data
.res
);
8763 isl_space_free(space
);
8764 isl_union_pw_multi_aff_free(upma
);
8768 /* Construct and return a union piecewise multi affine expression
8769 * that is equal to the given multi union piecewise affine expression,
8770 * in the special case of a 0D multi union piecewise affine expression.
8772 * Construct a union piecewise multi affine expression
8773 * on top of the explicit domain of the input.
8775 __isl_give isl_union_pw_multi_aff
*
8776 isl_union_pw_multi_aff_from_multi_union_pw_aff_0D(
8777 __isl_take isl_multi_union_pw_aff
*mupa
)
8781 isl_union_set
*domain
;
8783 space
= isl_multi_union_pw_aff_get_space(mupa
);
8784 mv
= isl_multi_val_zero(space
);
8785 domain
= isl_multi_union_pw_aff_domain(mupa
);
8786 return isl_union_pw_multi_aff_multi_val_on_domain(domain
, mv
);
8789 /* Construct and return a union piecewise multi affine expression
8790 * that is equal to the given multi union piecewise affine expression.
8792 * If the input is zero-dimensional, then
8793 * construct a union piecewise multi affine expression
8794 * on top of the explicit domain of the input.
8796 __isl_give isl_union_pw_multi_aff
*
8797 isl_union_pw_multi_aff_from_multi_union_pw_aff(
8798 __isl_take isl_multi_union_pw_aff
*mupa
)
8803 isl_union_pw_multi_aff
*upma
;
8804 isl_union_pw_aff
*upa
;
8806 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8808 mupa
= isl_multi_union_pw_aff_free(mupa
);
8813 return isl_union_pw_multi_aff_from_multi_union_pw_aff_0D(mupa
);
8815 space
= isl_multi_union_pw_aff_get_space(mupa
);
8816 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, 0);
8817 upma
= isl_union_pw_multi_aff_from_union_pw_aff(upa
);
8819 for (i
= 1; i
< n
; ++i
) {
8820 isl_union_pw_multi_aff
*upma_i
;
8822 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
8823 upma_i
= isl_union_pw_multi_aff_from_union_pw_aff(upa
);
8824 upma
= isl_union_pw_multi_aff_flat_range_product(upma
, upma_i
);
8827 upma
= isl_union_pw_multi_aff_reset_range_space(upma
, space
);
8829 isl_multi_union_pw_aff_free(mupa
);
8833 /* Intersect the range of "mupa" with "range",
8834 * in the special case where "mupa" is 0D.
8836 * Intersect the domain of "mupa" with the constraints on the parameters
8839 static __isl_give isl_multi_union_pw_aff
*mupa_intersect_range_0D(
8840 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_set
*range
)
8842 range
= isl_set_params(range
);
8843 mupa
= isl_multi_union_pw_aff_intersect_params(mupa
, range
);
8847 /* Intersect the range of "mupa" with "range".
8848 * That is, keep only those domain elements that have a function value
8851 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_intersect_range(
8852 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_set
*range
)
8854 isl_union_pw_multi_aff
*upma
;
8855 isl_union_set
*domain
;
8860 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8861 if (n
< 0 || !range
)
8864 space
= isl_set_get_space(range
);
8865 match
= isl_space_tuple_is_equal(mupa
->space
, isl_dim_set
,
8866 space
, isl_dim_set
);
8867 isl_space_free(space
);
8871 isl_die(isl_multi_union_pw_aff_get_ctx(mupa
), isl_error_invalid
,
8872 "space don't match", goto error
);
8874 return mupa_intersect_range_0D(mupa
, range
);
8876 upma
= isl_union_pw_multi_aff_from_multi_union_pw_aff(
8877 isl_multi_union_pw_aff_copy(mupa
));
8878 domain
= isl_union_set_from_set(range
);
8879 domain
= isl_union_set_preimage_union_pw_multi_aff(domain
, upma
);
8880 mupa
= isl_multi_union_pw_aff_intersect_domain(mupa
, domain
);
8884 isl_multi_union_pw_aff_free(mupa
);
8885 isl_set_free(range
);
8889 /* Return the shared domain of the elements of "mupa",
8890 * in the special case where "mupa" is zero-dimensional.
8892 * Return the explicit domain of "mupa".
8893 * Note that this domain may be a parameter set, either
8894 * because "mupa" is meant to live in a set space or
8895 * because no explicit domain has been set.
8897 __isl_give isl_union_set
*isl_multi_union_pw_aff_domain_0D(
8898 __isl_take isl_multi_union_pw_aff
*mupa
)
8902 dom
= isl_multi_union_pw_aff_get_explicit_domain(mupa
);
8903 isl_multi_union_pw_aff_free(mupa
);
8908 /* Return the shared domain of the elements of "mupa".
8910 * If "mupa" is zero-dimensional, then return its explicit domain.
8912 __isl_give isl_union_set
*isl_multi_union_pw_aff_domain(
8913 __isl_take isl_multi_union_pw_aff
*mupa
)
8917 isl_union_pw_aff
*upa
;
8920 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8922 mupa
= isl_multi_union_pw_aff_free(mupa
);
8927 return isl_multi_union_pw_aff_domain_0D(mupa
);
8929 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, 0);
8930 dom
= isl_union_pw_aff_domain(upa
);
8931 for (i
= 1; i
< n
; ++i
) {
8932 isl_union_set
*dom_i
;
8934 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
8935 dom_i
= isl_union_pw_aff_domain(upa
);
8936 dom
= isl_union_set_intersect(dom
, dom_i
);
8939 isl_multi_union_pw_aff_free(mupa
);
8943 /* Apply "aff" to "mupa". The space of "mupa" is equal to the domain of "aff".
8944 * In particular, the spaces have been aligned.
8945 * The result is defined over the shared domain of the elements of "mupa"
8947 * We first extract the parametric constant part of "aff" and
8948 * define that over the shared domain.
8949 * Then we iterate over all input dimensions of "aff" and add the corresponding
8950 * multiples of the elements of "mupa".
8951 * Finally, we consider the integer divisions, calling the function
8952 * recursively to obtain an isl_union_pw_aff corresponding to the
8953 * integer division argument.
8955 static __isl_give isl_union_pw_aff
*multi_union_pw_aff_apply_aff(
8956 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_aff
*aff
)
8959 isl_size n_in
, n_div
;
8960 isl_union_pw_aff
*upa
;
8961 isl_union_set
*uset
;
8965 n_in
= isl_aff_dim(aff
, isl_dim_in
);
8966 n_div
= isl_aff_dim(aff
, isl_dim_div
);
8967 if (n_in
< 0 || n_div
< 0)
8970 uset
= isl_multi_union_pw_aff_domain(isl_multi_union_pw_aff_copy(mupa
));
8971 cst
= isl_aff_copy(aff
);
8972 cst
= isl_aff_drop_dims(cst
, isl_dim_div
, 0, n_div
);
8973 cst
= isl_aff_drop_dims(cst
, isl_dim_in
, 0, n_in
);
8974 cst
= isl_aff_project_domain_on_params(cst
);
8975 upa
= isl_union_pw_aff_aff_on_domain(uset
, cst
);
8977 for (i
= 0; i
< n_in
; ++i
) {
8978 isl_union_pw_aff
*upa_i
;
8980 if (!isl_aff_involves_dims(aff
, isl_dim_in
, i
, 1))
8982 v
= isl_aff_get_coefficient_val(aff
, isl_dim_in
, i
);
8983 upa_i
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
8984 upa_i
= isl_union_pw_aff_scale_val(upa_i
, v
);
8985 upa
= isl_union_pw_aff_add(upa
, upa_i
);
8988 for (i
= 0; i
< n_div
; ++i
) {
8990 isl_union_pw_aff
*upa_i
;
8992 if (!isl_aff_involves_dims(aff
, isl_dim_div
, i
, 1))
8994 div
= isl_aff_get_div(aff
, i
);
8995 upa_i
= multi_union_pw_aff_apply_aff(
8996 isl_multi_union_pw_aff_copy(mupa
), div
);
8997 upa_i
= isl_union_pw_aff_floor(upa_i
);
8998 v
= isl_aff_get_coefficient_val(aff
, isl_dim_div
, i
);
8999 upa_i
= isl_union_pw_aff_scale_val(upa_i
, v
);
9000 upa
= isl_union_pw_aff_add(upa
, upa_i
);
9003 isl_multi_union_pw_aff_free(mupa
);
9008 isl_multi_union_pw_aff_free(mupa
);
9013 /* Apply "aff" to "mupa". The space of "mupa" needs to be compatible
9014 * with the domain of "aff".
9015 * Furthermore, the dimension of this space needs to be greater than zero.
9016 * The result is defined over the shared domain of the elements of "mupa"
9018 * We perform these checks and then hand over control to
9019 * multi_union_pw_aff_apply_aff.
9021 __isl_give isl_union_pw_aff
*isl_multi_union_pw_aff_apply_aff(
9022 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_aff
*aff
)
9025 isl_space
*space1
, *space2
;
9028 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
9029 isl_aff_get_space(aff
));
9030 aff
= isl_aff_align_params(aff
, isl_multi_union_pw_aff_get_space(mupa
));
9034 space1
= isl_multi_union_pw_aff_get_space(mupa
);
9035 space2
= isl_aff_get_domain_space(aff
);
9036 equal
= isl_space_is_equal(space1
, space2
);
9037 isl_space_free(space1
);
9038 isl_space_free(space2
);
9042 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
9043 "spaces don't match", goto error
);
9044 dim
= isl_aff_dim(aff
, isl_dim_in
);
9048 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
9049 "cannot determine domains", goto error
);
9051 return multi_union_pw_aff_apply_aff(mupa
, aff
);
9053 isl_multi_union_pw_aff_free(mupa
);
9058 /* Apply "ma" to "mupa", in the special case where "mupa" is 0D.
9059 * The space of "mupa" is known to be compatible with the domain of "ma".
9061 * Construct an isl_multi_union_pw_aff that is equal to "ma"
9062 * on the domain of "mupa".
9064 static __isl_give isl_multi_union_pw_aff
*mupa_apply_multi_aff_0D(
9065 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_multi_aff
*ma
)
9069 dom
= isl_multi_union_pw_aff_domain(mupa
);
9070 ma
= isl_multi_aff_project_domain_on_params(ma
);
9072 return isl_multi_union_pw_aff_multi_aff_on_domain(dom
, ma
);
9075 /* Apply "ma" to "mupa". The space of "mupa" needs to be compatible
9076 * with the domain of "ma".
9077 * The result is defined over the shared domain of the elements of "mupa"
9079 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_apply_multi_aff(
9080 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_multi_aff
*ma
)
9082 isl_space
*space1
, *space2
;
9083 isl_multi_union_pw_aff
*res
;
9086 isl_size n_in
, n_out
;
9088 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
9089 isl_multi_aff_get_space(ma
));
9090 ma
= isl_multi_aff_align_params(ma
,
9091 isl_multi_union_pw_aff_get_space(mupa
));
9092 n_in
= isl_multi_aff_dim(ma
, isl_dim_in
);
9093 n_out
= isl_multi_aff_dim(ma
, isl_dim_out
);
9094 if (!mupa
|| n_in
< 0 || n_out
< 0)
9097 space1
= isl_multi_union_pw_aff_get_space(mupa
);
9098 space2
= isl_multi_aff_get_domain_space(ma
);
9099 equal
= isl_space_is_equal(space1
, space2
);
9100 isl_space_free(space1
);
9101 isl_space_free(space2
);
9105 isl_die(isl_multi_aff_get_ctx(ma
), isl_error_invalid
,
9106 "spaces don't match", goto error
);
9108 return mupa_apply_multi_aff_0D(mupa
, ma
);
9110 space1
= isl_space_range(isl_multi_aff_get_space(ma
));
9111 res
= isl_multi_union_pw_aff_alloc(space1
);
9113 for (i
= 0; i
< n_out
; ++i
) {
9115 isl_union_pw_aff
*upa
;
9117 aff
= isl_multi_aff_get_aff(ma
, i
);
9118 upa
= multi_union_pw_aff_apply_aff(
9119 isl_multi_union_pw_aff_copy(mupa
), aff
);
9120 res
= isl_multi_union_pw_aff_set_union_pw_aff(res
, i
, upa
);
9123 isl_multi_aff_free(ma
);
9124 isl_multi_union_pw_aff_free(mupa
);
9127 isl_multi_union_pw_aff_free(mupa
);
9128 isl_multi_aff_free(ma
);
9132 /* Apply "pa" to "mupa", in the special case where "mupa" is 0D.
9133 * The space of "mupa" is known to be compatible with the domain of "pa".
9135 * Construct an isl_multi_union_pw_aff that is equal to "pa"
9136 * on the domain of "mupa".
9138 static __isl_give isl_union_pw_aff
*isl_multi_union_pw_aff_apply_pw_aff_0D(
9139 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_pw_aff
*pa
)
9143 dom
= isl_multi_union_pw_aff_domain(mupa
);
9144 pa
= isl_pw_aff_project_domain_on_params(pa
);
9146 return isl_union_pw_aff_pw_aff_on_domain(dom
, pa
);
9149 /* Apply "pa" to "mupa". The space of "mupa" needs to be compatible
9150 * with the domain of "pa".
9151 * Furthermore, the dimension of this space needs to be greater than zero.
9152 * The result is defined over the shared domain of the elements of "mupa"
9154 __isl_give isl_union_pw_aff
*isl_multi_union_pw_aff_apply_pw_aff(
9155 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_pw_aff
*pa
)
9160 isl_space
*space
, *space2
;
9161 isl_union_pw_aff
*upa
;
9163 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
9164 isl_pw_aff_get_space(pa
));
9165 pa
= isl_pw_aff_align_params(pa
,
9166 isl_multi_union_pw_aff_get_space(mupa
));
9170 space
= isl_multi_union_pw_aff_get_space(mupa
);
9171 space2
= isl_pw_aff_get_domain_space(pa
);
9172 equal
= isl_space_is_equal(space
, space2
);
9173 isl_space_free(space
);
9174 isl_space_free(space2
);
9178 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
9179 "spaces don't match", goto error
);
9180 n_in
= isl_pw_aff_dim(pa
, isl_dim_in
);
9184 return isl_multi_union_pw_aff_apply_pw_aff_0D(mupa
, pa
);
9186 space
= isl_space_params(isl_multi_union_pw_aff_get_space(mupa
));
9187 upa
= isl_union_pw_aff_empty(space
);
9189 for (i
= 0; i
< pa
->n
; ++i
) {
9192 isl_multi_union_pw_aff
*mupa_i
;
9193 isl_union_pw_aff
*upa_i
;
9195 mupa_i
= isl_multi_union_pw_aff_copy(mupa
);
9196 domain
= isl_set_copy(pa
->p
[i
].set
);
9197 mupa_i
= isl_multi_union_pw_aff_intersect_range(mupa_i
, domain
);
9198 aff
= isl_aff_copy(pa
->p
[i
].aff
);
9199 upa_i
= multi_union_pw_aff_apply_aff(mupa_i
, aff
);
9200 upa
= isl_union_pw_aff_union_add(upa
, upa_i
);
9203 isl_multi_union_pw_aff_free(mupa
);
9204 isl_pw_aff_free(pa
);
9207 isl_multi_union_pw_aff_free(mupa
);
9208 isl_pw_aff_free(pa
);
9212 /* Apply "pma" to "mupa", in the special case where "mupa" is 0D.
9213 * The space of "mupa" is known to be compatible with the domain of "pma".
9215 * Construct an isl_multi_union_pw_aff that is equal to "pma"
9216 * on the domain of "mupa".
9218 static __isl_give isl_multi_union_pw_aff
*mupa_apply_pw_multi_aff_0D(
9219 __isl_take isl_multi_union_pw_aff
*mupa
,
9220 __isl_take isl_pw_multi_aff
*pma
)
9224 dom
= isl_multi_union_pw_aff_domain(mupa
);
9225 pma
= isl_pw_multi_aff_project_domain_on_params(pma
);
9227 return isl_multi_union_pw_aff_pw_multi_aff_on_domain(dom
, pma
);
9230 /* Apply "pma" to "mupa". The space of "mupa" needs to be compatible
9231 * with the domain of "pma".
9232 * The result is defined over the shared domain of the elements of "mupa"
9234 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_apply_pw_multi_aff(
9235 __isl_take isl_multi_union_pw_aff
*mupa
,
9236 __isl_take isl_pw_multi_aff
*pma
)
9238 isl_space
*space1
, *space2
;
9239 isl_multi_union_pw_aff
*res
;
9242 isl_size n_in
, n_out
;
9244 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
9245 isl_pw_multi_aff_get_space(pma
));
9246 pma
= isl_pw_multi_aff_align_params(pma
,
9247 isl_multi_union_pw_aff_get_space(mupa
));
9251 space1
= isl_multi_union_pw_aff_get_space(mupa
);
9252 space2
= isl_pw_multi_aff_get_domain_space(pma
);
9253 equal
= isl_space_is_equal(space1
, space2
);
9254 isl_space_free(space1
);
9255 isl_space_free(space2
);
9259 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
9260 "spaces don't match", goto error
);
9261 n_in
= isl_pw_multi_aff_dim(pma
, isl_dim_in
);
9262 n_out
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
9263 if (n_in
< 0 || n_out
< 0)
9266 return mupa_apply_pw_multi_aff_0D(mupa
, pma
);
9268 space1
= isl_space_range(isl_pw_multi_aff_get_space(pma
));
9269 res
= isl_multi_union_pw_aff_alloc(space1
);
9271 for (i
= 0; i
< n_out
; ++i
) {
9273 isl_union_pw_aff
*upa
;
9275 pa
= isl_pw_multi_aff_get_pw_aff(pma
, i
);
9276 upa
= isl_multi_union_pw_aff_apply_pw_aff(
9277 isl_multi_union_pw_aff_copy(mupa
), pa
);
9278 res
= isl_multi_union_pw_aff_set_union_pw_aff(res
, i
, upa
);
9281 isl_pw_multi_aff_free(pma
);
9282 isl_multi_union_pw_aff_free(mupa
);
9285 isl_multi_union_pw_aff_free(mupa
);
9286 isl_pw_multi_aff_free(pma
);
9290 /* Replace the explicit domain of "mupa" by its preimage under "upma".
9291 * If the explicit domain only keeps track of constraints on the parameters,
9292 * then only update those constraints.
9294 static __isl_give isl_multi_union_pw_aff
*preimage_explicit_domain(
9295 __isl_take isl_multi_union_pw_aff
*mupa
,
9296 __isl_keep isl_union_pw_multi_aff
*upma
)
9300 if (isl_multi_union_pw_aff_check_has_explicit_domain(mupa
) < 0)
9301 return isl_multi_union_pw_aff_free(mupa
);
9303 mupa
= isl_multi_union_pw_aff_cow(mupa
);
9307 is_params
= isl_union_set_is_params(mupa
->u
.dom
);
9309 return isl_multi_union_pw_aff_free(mupa
);
9311 upma
= isl_union_pw_multi_aff_copy(upma
);
9313 mupa
->u
.dom
= isl_union_set_intersect_params(mupa
->u
.dom
,
9314 isl_union_set_params(isl_union_pw_multi_aff_domain(upma
)));
9316 mupa
->u
.dom
= isl_union_set_preimage_union_pw_multi_aff(
9319 return isl_multi_union_pw_aff_free(mupa
);
9323 /* Compute the pullback of "mupa" by the function represented by "upma".
9324 * In other words, plug in "upma" in "mupa". The result contains
9325 * expressions defined over the domain space of "upma".
9327 * Run over all elements of "mupa" and plug in "upma" in each of them.
9329 * If "mupa" has an explicit domain, then it is this domain
9330 * that needs to undergo a pullback instead, i.e., a preimage.
9332 __isl_give isl_multi_union_pw_aff
*
9333 isl_multi_union_pw_aff_pullback_union_pw_multi_aff(
9334 __isl_take isl_multi_union_pw_aff
*mupa
,
9335 __isl_take isl_union_pw_multi_aff
*upma
)
9340 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
9341 isl_union_pw_multi_aff_get_space(upma
));
9342 upma
= isl_union_pw_multi_aff_align_params(upma
,
9343 isl_multi_union_pw_aff_get_space(mupa
));
9344 mupa
= isl_multi_union_pw_aff_cow(mupa
);
9345 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
9349 for (i
= 0; i
< n
; ++i
) {
9350 isl_union_pw_aff
*upa
;
9352 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
9353 upa
= isl_union_pw_aff_pullback_union_pw_multi_aff(upa
,
9354 isl_union_pw_multi_aff_copy(upma
));
9355 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
9358 if (isl_multi_union_pw_aff_has_explicit_domain(mupa
))
9359 mupa
= preimage_explicit_domain(mupa
, upma
);
9361 isl_union_pw_multi_aff_free(upma
);
9364 isl_multi_union_pw_aff_free(mupa
);
9365 isl_union_pw_multi_aff_free(upma
);
9369 /* Extract the sequence of elements in "mupa" with domain space "space"
9370 * (ignoring parameters).
9372 * For the elements of "mupa" that are not defined on the specified space,
9373 * the corresponding element in the result is empty.
9375 __isl_give isl_multi_pw_aff
*isl_multi_union_pw_aff_extract_multi_pw_aff(
9376 __isl_keep isl_multi_union_pw_aff
*mupa
, __isl_take isl_space
*space
)
9380 isl_space
*space_mpa
;
9381 isl_multi_pw_aff
*mpa
;
9383 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
9384 if (n
< 0 || !space
)
9387 space_mpa
= isl_multi_union_pw_aff_get_space(mupa
);
9388 space
= isl_space_replace_params(space
, space_mpa
);
9389 space_mpa
= isl_space_map_from_domain_and_range(isl_space_copy(space
),
9391 mpa
= isl_multi_pw_aff_alloc(space_mpa
);
9393 space
= isl_space_from_domain(space
);
9394 space
= isl_space_add_dims(space
, isl_dim_out
, 1);
9395 for (i
= 0; i
< n
; ++i
) {
9396 isl_union_pw_aff
*upa
;
9399 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
9400 pa
= isl_union_pw_aff_extract_pw_aff(upa
,
9401 isl_space_copy(space
));
9402 mpa
= isl_multi_pw_aff_set_pw_aff(mpa
, i
, pa
);
9403 isl_union_pw_aff_free(upa
);
9406 isl_space_free(space
);
9409 isl_space_free(space
);
9413 /* Evaluate the affine function "aff" in the void point "pnt".
9414 * In particular, return the value NaN.
9416 static __isl_give isl_val
*eval_void(__isl_take isl_aff
*aff
,
9417 __isl_take isl_point
*pnt
)
9421 ctx
= isl_point_get_ctx(pnt
);
9423 isl_point_free(pnt
);
9424 return isl_val_nan(ctx
);
9427 /* Evaluate the affine expression "aff"
9428 * in the coordinates (with denominator) "pnt".
9430 static __isl_give isl_val
*eval(__isl_keep isl_vec
*aff
,
9431 __isl_keep isl_vec
*pnt
)
9440 ctx
= isl_vec_get_ctx(aff
);
9443 isl_seq_inner_product(aff
->el
+ 1, pnt
->el
, pnt
->size
, &n
);
9444 isl_int_mul(d
, aff
->el
[0], pnt
->el
[0]);
9445 v
= isl_val_rat_from_isl_int(ctx
, n
, d
);
9446 v
= isl_val_normalize(v
);
9453 /* Check that the domain space of "aff" is equal to "space".
9455 static isl_stat
isl_aff_check_has_domain_space(__isl_keep isl_aff
*aff
,
9456 __isl_keep isl_space
*space
)
9460 ok
= isl_space_is_equal(isl_aff_peek_domain_space(aff
), space
);
9462 return isl_stat_error
;
9464 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
9465 "incompatible spaces", return isl_stat_error
);
9469 /* Evaluate the affine function "aff" in "pnt".
9471 __isl_give isl_val
*isl_aff_eval(__isl_take isl_aff
*aff
,
9472 __isl_take isl_point
*pnt
)
9476 isl_local_space
*ls
;
9478 if (isl_aff_check_has_domain_space(aff
, isl_point_peek_space(pnt
)) < 0)
9480 is_void
= isl_point_is_void(pnt
);
9484 return eval_void(aff
, pnt
);
9486 ls
= isl_aff_get_domain_local_space(aff
);
9487 pnt
= isl_local_space_lift_point(ls
, pnt
);
9489 v
= eval(aff
->v
, isl_point_peek_vec(pnt
));
9492 isl_point_free(pnt
);
9497 isl_point_free(pnt
);