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
,
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
)
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);
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
,
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);
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
),
2857 set
= isl_set_empty(isl_pw_aff_get_domain_space(pwaff
));
2859 for (i
= 0; i
< pwaff
->n
; ++i
) {
2860 isl_basic_set
*bset
;
2861 isl_set
*set_i
, *locus
;
2864 if (isl_aff_is_nan(pwaff
->p
[i
].aff
))
2867 rational
= isl_set_has_rational(pwaff
->p
[i
].set
);
2868 bset
= fn(isl_aff_copy(pwaff
->p
[i
].aff
), rational
);
2869 locus
= isl_set_from_basic_set(bset
);
2870 set_i
= isl_set_copy(pwaff
->p
[i
].set
);
2872 set_i
= isl_set_subtract(set_i
, locus
);
2874 set_i
= isl_set_intersect(set_i
, locus
);
2875 set
= isl_set_union_disjoint(set
, set_i
);
2878 isl_pw_aff_free(pwaff
);
2883 /* Return a set containing those elements in the domain
2884 * of "pa" where it is positive.
2886 __isl_give isl_set
*isl_pw_aff_pos_set(__isl_take isl_pw_aff
*pa
)
2888 return pw_aff_locus(pa
, &aff_pos_basic_set
, 0);
2891 /* Return a set containing those elements in the domain
2892 * of pwaff where it is non-negative.
2894 __isl_give isl_set
*isl_pw_aff_nonneg_set(__isl_take isl_pw_aff
*pwaff
)
2896 return pw_aff_locus(pwaff
, &aff_nonneg_basic_set
, 0);
2899 /* Return a set containing those elements in the domain
2900 * of pwaff where it is zero.
2902 __isl_give isl_set
*isl_pw_aff_zero_set(__isl_take isl_pw_aff
*pwaff
)
2904 return pw_aff_locus(pwaff
, &aff_zero_basic_set
, 0);
2907 /* Return a set containing those elements in the domain
2908 * of pwaff where it is not zero.
2910 __isl_give isl_set
*isl_pw_aff_non_zero_set(__isl_take isl_pw_aff
*pwaff
)
2912 return pw_aff_locus(pwaff
, &aff_zero_basic_set
, 1);
2915 /* Bind the affine function "aff" to the parameter "id",
2916 * returning the elements in the domain where the affine expression
2917 * is equal to the parameter.
2919 __isl_give isl_basic_set
*isl_aff_bind_id(__isl_take isl_aff
*aff
,
2920 __isl_take isl_id
*id
)
2925 space
= isl_aff_get_domain_space(aff
);
2926 space
= isl_space_add_param_id(space
, isl_id_copy(id
));
2928 aff
= isl_aff_align_params(aff
, isl_space_copy(space
));
2929 aff_id
= isl_aff_param_on_domain_space_id(space
, id
);
2931 return isl_aff_eq_basic_set(aff
, aff_id
);
2934 /* Return a set containing those elements in the shared domain
2935 * of pwaff1 and pwaff2 where pwaff1 is greater than (or equal) to pwaff2.
2937 * We compute the difference on the shared domain and then construct
2938 * the set of values where this difference is non-negative.
2939 * If strict is set, we first subtract 1 from the difference.
2940 * If equal is set, we only return the elements where pwaff1 and pwaff2
2943 static __isl_give isl_set
*pw_aff_gte_set(__isl_take isl_pw_aff
*pwaff1
,
2944 __isl_take isl_pw_aff
*pwaff2
, int strict
, int equal
)
2946 isl_set
*set1
, *set2
;
2948 set1
= isl_pw_aff_domain(isl_pw_aff_copy(pwaff1
));
2949 set2
= isl_pw_aff_domain(isl_pw_aff_copy(pwaff2
));
2950 set1
= isl_set_intersect(set1
, set2
);
2951 pwaff1
= isl_pw_aff_intersect_domain(pwaff1
, isl_set_copy(set1
));
2952 pwaff2
= isl_pw_aff_intersect_domain(pwaff2
, isl_set_copy(set1
));
2953 pwaff1
= isl_pw_aff_add(pwaff1
, isl_pw_aff_neg(pwaff2
));
2956 isl_space
*space
= isl_set_get_space(set1
);
2958 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(space
));
2959 aff
= isl_aff_add_constant_si(aff
, -1);
2960 pwaff1
= isl_pw_aff_add(pwaff1
, isl_pw_aff_alloc(set1
, aff
));
2965 return isl_pw_aff_zero_set(pwaff1
);
2966 return isl_pw_aff_nonneg_set(pwaff1
);
2969 /* Return a set containing those elements in the shared domain
2970 * of pwaff1 and pwaff2 where pwaff1 is equal to pwaff2.
2972 static __isl_give isl_set
*pw_aff_eq_set(__isl_take isl_pw_aff
*pwaff1
,
2973 __isl_take isl_pw_aff
*pwaff2
)
2975 return pw_aff_gte_set(pwaff1
, pwaff2
, 0, 1);
2978 __isl_give isl_set
*isl_pw_aff_eq_set(__isl_take isl_pw_aff
*pwaff1
,
2979 __isl_take isl_pw_aff
*pwaff2
)
2981 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_eq_set
);
2984 /* Return a set containing those elements in the shared domain
2985 * of pwaff1 and pwaff2 where pwaff1 is greater than or equal to pwaff2.
2987 static __isl_give isl_set
*pw_aff_ge_set(__isl_take isl_pw_aff
*pwaff1
,
2988 __isl_take isl_pw_aff
*pwaff2
)
2990 return pw_aff_gte_set(pwaff1
, pwaff2
, 0, 0);
2993 __isl_give isl_set
*isl_pw_aff_ge_set(__isl_take isl_pw_aff
*pwaff1
,
2994 __isl_take isl_pw_aff
*pwaff2
)
2996 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_ge_set
);
2999 /* Return a set containing those elements in the shared domain
3000 * of pwaff1 and pwaff2 where pwaff1 is strictly greater than pwaff2.
3002 static __isl_give isl_set
*pw_aff_gt_set(__isl_take isl_pw_aff
*pwaff1
,
3003 __isl_take isl_pw_aff
*pwaff2
)
3005 return pw_aff_gte_set(pwaff1
, pwaff2
, 1, 0);
3008 __isl_give isl_set
*isl_pw_aff_gt_set(__isl_take isl_pw_aff
*pwaff1
,
3009 __isl_take isl_pw_aff
*pwaff2
)
3011 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_gt_set
);
3014 __isl_give isl_set
*isl_pw_aff_le_set(__isl_take isl_pw_aff
*pwaff1
,
3015 __isl_take isl_pw_aff
*pwaff2
)
3017 return isl_pw_aff_ge_set(pwaff2
, pwaff1
);
3020 __isl_give isl_set
*isl_pw_aff_lt_set(__isl_take isl_pw_aff
*pwaff1
,
3021 __isl_take isl_pw_aff
*pwaff2
)
3023 return isl_pw_aff_gt_set(pwaff2
, pwaff1
);
3026 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3027 * where the function values are ordered in the same way as "order",
3028 * which returns a set in the shared domain of its two arguments.
3029 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3031 * Let "pa1" and "pa2" be defined on domains A and B respectively.
3032 * We first pull back the two functions such that they are defined on
3033 * the domain [A -> B]. Then we apply "order", resulting in a set
3034 * in the space [A -> B]. Finally, we unwrap this set to obtain
3035 * a map in the space A -> B.
3037 static __isl_give isl_map
*isl_pw_aff_order_map_aligned(
3038 __isl_take isl_pw_aff
*pa1
, __isl_take isl_pw_aff
*pa2
,
3039 __isl_give isl_set
*(*order
)(__isl_take isl_pw_aff
*pa1
,
3040 __isl_take isl_pw_aff
*pa2
))
3042 isl_space
*space1
, *space2
;
3046 space1
= isl_space_domain(isl_pw_aff_get_space(pa1
));
3047 space2
= isl_space_domain(isl_pw_aff_get_space(pa2
));
3048 space1
= isl_space_map_from_domain_and_range(space1
, space2
);
3049 ma
= isl_multi_aff_domain_map(isl_space_copy(space1
));
3050 pa1
= isl_pw_aff_pullback_multi_aff(pa1
, ma
);
3051 ma
= isl_multi_aff_range_map(space1
);
3052 pa2
= isl_pw_aff_pullback_multi_aff(pa2
, ma
);
3053 set
= order(pa1
, pa2
);
3055 return isl_set_unwrap(set
);
3058 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3059 * where the function values are equal.
3060 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3062 static __isl_give isl_map
*isl_pw_aff_eq_map_aligned(__isl_take isl_pw_aff
*pa1
,
3063 __isl_take isl_pw_aff
*pa2
)
3065 return isl_pw_aff_order_map_aligned(pa1
, pa2
, &isl_pw_aff_eq_set
);
3068 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3069 * where the function values are equal.
3071 __isl_give isl_map
*isl_pw_aff_eq_map(__isl_take isl_pw_aff
*pa1
,
3072 __isl_take isl_pw_aff
*pa2
)
3074 return align_params_pw_pw_map_and(pa1
, pa2
, &isl_pw_aff_eq_map_aligned
);
3077 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3078 * where the function value of "pa1" is less than the function value of "pa2".
3079 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3081 static __isl_give isl_map
*isl_pw_aff_lt_map_aligned(__isl_take isl_pw_aff
*pa1
,
3082 __isl_take isl_pw_aff
*pa2
)
3084 return isl_pw_aff_order_map_aligned(pa1
, pa2
, &isl_pw_aff_lt_set
);
3087 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3088 * where the function value of "pa1" is less than the function value of "pa2".
3090 __isl_give isl_map
*isl_pw_aff_lt_map(__isl_take isl_pw_aff
*pa1
,
3091 __isl_take isl_pw_aff
*pa2
)
3093 return align_params_pw_pw_map_and(pa1
, pa2
, &isl_pw_aff_lt_map_aligned
);
3096 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3097 * where the function value of "pa1" is greater than the function value
3099 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3101 static __isl_give isl_map
*isl_pw_aff_gt_map_aligned(__isl_take isl_pw_aff
*pa1
,
3102 __isl_take isl_pw_aff
*pa2
)
3104 return isl_pw_aff_order_map_aligned(pa1
, pa2
, &isl_pw_aff_gt_set
);
3107 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3108 * where the function value of "pa1" is greater than the function value
3111 __isl_give isl_map
*isl_pw_aff_gt_map(__isl_take isl_pw_aff
*pa1
,
3112 __isl_take isl_pw_aff
*pa2
)
3114 return align_params_pw_pw_map_and(pa1
, pa2
, &isl_pw_aff_gt_map_aligned
);
3117 /* Return a set containing those elements in the shared domain
3118 * of the elements of list1 and list2 where each element in list1
3119 * has the relation specified by "fn" with each element in list2.
3121 static __isl_give isl_set
*pw_aff_list_set(__isl_take isl_pw_aff_list
*list1
,
3122 __isl_take isl_pw_aff_list
*list2
,
3123 __isl_give isl_set
*(*fn
)(__isl_take isl_pw_aff
*pwaff1
,
3124 __isl_take isl_pw_aff
*pwaff2
))
3130 if (!list1
|| !list2
)
3133 ctx
= isl_pw_aff_list_get_ctx(list1
);
3134 if (list1
->n
< 1 || list2
->n
< 1)
3135 isl_die(ctx
, isl_error_invalid
,
3136 "list should contain at least one element", goto error
);
3138 set
= isl_set_universe(isl_pw_aff_get_domain_space(list1
->p
[0]));
3139 for (i
= 0; i
< list1
->n
; ++i
)
3140 for (j
= 0; j
< list2
->n
; ++j
) {
3143 set_ij
= fn(isl_pw_aff_copy(list1
->p
[i
]),
3144 isl_pw_aff_copy(list2
->p
[j
]));
3145 set
= isl_set_intersect(set
, set_ij
);
3148 isl_pw_aff_list_free(list1
);
3149 isl_pw_aff_list_free(list2
);
3152 isl_pw_aff_list_free(list1
);
3153 isl_pw_aff_list_free(list2
);
3157 /* Return a set containing those elements in the shared domain
3158 * of the elements of list1 and list2 where each element in list1
3159 * is equal to each element in list2.
3161 __isl_give isl_set
*isl_pw_aff_list_eq_set(__isl_take isl_pw_aff_list
*list1
,
3162 __isl_take isl_pw_aff_list
*list2
)
3164 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_eq_set
);
3167 __isl_give isl_set
*isl_pw_aff_list_ne_set(__isl_take isl_pw_aff_list
*list1
,
3168 __isl_take isl_pw_aff_list
*list2
)
3170 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_ne_set
);
3173 /* Return a set containing those elements in the shared domain
3174 * of the elements of list1 and list2 where each element in list1
3175 * is less than or equal to each element in list2.
3177 __isl_give isl_set
*isl_pw_aff_list_le_set(__isl_take isl_pw_aff_list
*list1
,
3178 __isl_take isl_pw_aff_list
*list2
)
3180 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_le_set
);
3183 __isl_give isl_set
*isl_pw_aff_list_lt_set(__isl_take isl_pw_aff_list
*list1
,
3184 __isl_take isl_pw_aff_list
*list2
)
3186 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_lt_set
);
3189 __isl_give isl_set
*isl_pw_aff_list_ge_set(__isl_take isl_pw_aff_list
*list1
,
3190 __isl_take isl_pw_aff_list
*list2
)
3192 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_ge_set
);
3195 __isl_give isl_set
*isl_pw_aff_list_gt_set(__isl_take isl_pw_aff_list
*list1
,
3196 __isl_take isl_pw_aff_list
*list2
)
3198 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_gt_set
);
3202 /* Return a set containing those elements in the shared domain
3203 * of pwaff1 and pwaff2 where pwaff1 is not equal to pwaff2.
3205 static __isl_give isl_set
*pw_aff_ne_set(__isl_take isl_pw_aff
*pwaff1
,
3206 __isl_take isl_pw_aff
*pwaff2
)
3208 isl_set
*set_lt
, *set_gt
;
3210 set_lt
= isl_pw_aff_lt_set(isl_pw_aff_copy(pwaff1
),
3211 isl_pw_aff_copy(pwaff2
));
3212 set_gt
= isl_pw_aff_gt_set(pwaff1
, pwaff2
);
3213 return isl_set_union_disjoint(set_lt
, set_gt
);
3216 __isl_give isl_set
*isl_pw_aff_ne_set(__isl_take isl_pw_aff
*pwaff1
,
3217 __isl_take isl_pw_aff
*pwaff2
)
3219 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_ne_set
);
3222 __isl_give isl_pw_aff
*isl_pw_aff_scale_down(__isl_take isl_pw_aff
*pwaff
,
3227 if (isl_int_is_one(v
))
3229 if (!isl_int_is_pos(v
))
3230 isl_die(isl_pw_aff_get_ctx(pwaff
), isl_error_invalid
,
3231 "factor needs to be positive",
3232 return isl_pw_aff_free(pwaff
));
3233 pwaff
= isl_pw_aff_cow(pwaff
);
3239 for (i
= 0; i
< pwaff
->n
; ++i
) {
3240 pwaff
->p
[i
].aff
= isl_aff_scale_down(pwaff
->p
[i
].aff
, v
);
3241 if (!pwaff
->p
[i
].aff
)
3242 return isl_pw_aff_free(pwaff
);
3248 __isl_give isl_pw_aff
*isl_pw_aff_floor(__isl_take isl_pw_aff
*pwaff
)
3252 pwaff
= isl_pw_aff_cow(pwaff
);
3258 for (i
= 0; i
< pwaff
->n
; ++i
) {
3259 pwaff
->p
[i
].aff
= isl_aff_floor(pwaff
->p
[i
].aff
);
3260 if (!pwaff
->p
[i
].aff
)
3261 return isl_pw_aff_free(pwaff
);
3267 __isl_give isl_pw_aff
*isl_pw_aff_ceil(__isl_take isl_pw_aff
*pwaff
)
3271 pwaff
= isl_pw_aff_cow(pwaff
);
3277 for (i
= 0; i
< pwaff
->n
; ++i
) {
3278 pwaff
->p
[i
].aff
= isl_aff_ceil(pwaff
->p
[i
].aff
);
3279 if (!pwaff
->p
[i
].aff
)
3280 return isl_pw_aff_free(pwaff
);
3286 /* Assuming that "cond1" and "cond2" are disjoint,
3287 * return an affine expression that is equal to pwaff1 on cond1
3288 * and to pwaff2 on cond2.
3290 static __isl_give isl_pw_aff
*isl_pw_aff_select(
3291 __isl_take isl_set
*cond1
, __isl_take isl_pw_aff
*pwaff1
,
3292 __isl_take isl_set
*cond2
, __isl_take isl_pw_aff
*pwaff2
)
3294 pwaff1
= isl_pw_aff_intersect_domain(pwaff1
, cond1
);
3295 pwaff2
= isl_pw_aff_intersect_domain(pwaff2
, cond2
);
3297 return isl_pw_aff_add_disjoint(pwaff1
, pwaff2
);
3300 /* Return an affine expression that is equal to pwaff_true for elements
3301 * where "cond" is non-zero and to pwaff_false for elements where "cond"
3303 * That is, return cond ? pwaff_true : pwaff_false;
3305 * If "cond" involves and NaN, then we conservatively return a NaN
3306 * on its entire domain. In principle, we could consider the pieces
3307 * where it is NaN separately from those where it is not.
3309 * If "pwaff_true" and "pwaff_false" are obviously equal to each other,
3310 * then only use the domain of "cond" to restrict the domain.
3312 __isl_give isl_pw_aff
*isl_pw_aff_cond(__isl_take isl_pw_aff
*cond
,
3313 __isl_take isl_pw_aff
*pwaff_true
, __isl_take isl_pw_aff
*pwaff_false
)
3315 isl_set
*cond_true
, *cond_false
;
3320 if (isl_pw_aff_involves_nan(cond
)) {
3321 isl_space
*space
= isl_pw_aff_get_domain_space(cond
);
3322 isl_local_space
*ls
= isl_local_space_from_space(space
);
3323 isl_pw_aff_free(cond
);
3324 isl_pw_aff_free(pwaff_true
);
3325 isl_pw_aff_free(pwaff_false
);
3326 return isl_pw_aff_nan_on_domain(ls
);
3329 pwaff_true
= isl_pw_aff_align_params(pwaff_true
,
3330 isl_pw_aff_get_space(pwaff_false
));
3331 pwaff_false
= isl_pw_aff_align_params(pwaff_false
,
3332 isl_pw_aff_get_space(pwaff_true
));
3333 equal
= isl_pw_aff_plain_is_equal(pwaff_true
, pwaff_false
);
3339 dom
= isl_set_coalesce(isl_pw_aff_domain(cond
));
3340 isl_pw_aff_free(pwaff_false
);
3341 return isl_pw_aff_intersect_domain(pwaff_true
, dom
);
3344 cond_true
= isl_pw_aff_non_zero_set(isl_pw_aff_copy(cond
));
3345 cond_false
= isl_pw_aff_zero_set(cond
);
3346 return isl_pw_aff_select(cond_true
, pwaff_true
,
3347 cond_false
, pwaff_false
);
3349 isl_pw_aff_free(cond
);
3350 isl_pw_aff_free(pwaff_true
);
3351 isl_pw_aff_free(pwaff_false
);
3355 isl_bool
isl_aff_is_cst(__isl_keep isl_aff
*aff
)
3360 return isl_bool_error
;
3362 pos
= isl_seq_first_non_zero(aff
->v
->el
+ 2, aff
->v
->size
- 2);
3363 return isl_bool_ok(pos
== -1);
3366 /* Check whether pwaff is a piecewise constant.
3368 isl_bool
isl_pw_aff_is_cst(__isl_keep isl_pw_aff
*pwaff
)
3373 return isl_bool_error
;
3375 for (i
= 0; i
< pwaff
->n
; ++i
) {
3376 isl_bool is_cst
= isl_aff_is_cst(pwaff
->p
[i
].aff
);
3377 if (is_cst
< 0 || !is_cst
)
3381 return isl_bool_true
;
3384 /* Are all elements of "mpa" piecewise constants?
3386 isl_bool
isl_multi_pw_aff_is_cst(__isl_keep isl_multi_pw_aff
*mpa
)
3391 return isl_bool_error
;
3393 for (i
= 0; i
< mpa
->n
; ++i
) {
3394 isl_bool is_cst
= isl_pw_aff_is_cst(mpa
->u
.p
[i
]);
3395 if (is_cst
< 0 || !is_cst
)
3399 return isl_bool_true
;
3402 /* Return the product of "aff1" and "aff2".
3404 * If either of the two is NaN, then the result is NaN.
3406 * Otherwise, at least one of "aff1" or "aff2" needs to be a constant.
3408 __isl_give isl_aff
*isl_aff_mul(__isl_take isl_aff
*aff1
,
3409 __isl_take isl_aff
*aff2
)
3414 if (isl_aff_is_nan(aff1
)) {
3418 if (isl_aff_is_nan(aff2
)) {
3423 if (!isl_aff_is_cst(aff2
) && isl_aff_is_cst(aff1
))
3424 return isl_aff_mul(aff2
, aff1
);
3426 if (!isl_aff_is_cst(aff2
))
3427 isl_die(isl_aff_get_ctx(aff1
), isl_error_invalid
,
3428 "at least one affine expression should be constant",
3431 aff1
= isl_aff_cow(aff1
);
3435 aff1
= isl_aff_scale(aff1
, aff2
->v
->el
[1]);
3436 aff1
= isl_aff_scale_down(aff1
, aff2
->v
->el
[0]);
3446 /* Divide "aff1" by "aff2", assuming "aff2" is a constant.
3448 * If either of the two is NaN, then the result is NaN.
3450 __isl_give isl_aff
*isl_aff_div(__isl_take isl_aff
*aff1
,
3451 __isl_take isl_aff
*aff2
)
3459 if (isl_aff_is_nan(aff1
)) {
3463 if (isl_aff_is_nan(aff2
)) {
3468 is_cst
= isl_aff_is_cst(aff2
);
3472 isl_die(isl_aff_get_ctx(aff2
), isl_error_invalid
,
3473 "second argument should be a constant", goto error
);
3478 neg
= isl_int_is_neg(aff2
->v
->el
[1]);
3480 isl_int_neg(aff2
->v
->el
[0], aff2
->v
->el
[0]);
3481 isl_int_neg(aff2
->v
->el
[1], aff2
->v
->el
[1]);
3484 aff1
= isl_aff_scale(aff1
, aff2
->v
->el
[0]);
3485 aff1
= isl_aff_scale_down(aff1
, aff2
->v
->el
[1]);
3488 isl_int_neg(aff2
->v
->el
[0], aff2
->v
->el
[0]);
3489 isl_int_neg(aff2
->v
->el
[1], aff2
->v
->el
[1]);
3500 static __isl_give isl_pw_aff
*pw_aff_add(__isl_take isl_pw_aff
*pwaff1
,
3501 __isl_take isl_pw_aff
*pwaff2
)
3503 return isl_pw_aff_on_shared_domain(pwaff1
, pwaff2
, &isl_aff_add
);
3506 __isl_give isl_pw_aff
*isl_pw_aff_add(__isl_take isl_pw_aff
*pwaff1
,
3507 __isl_take isl_pw_aff
*pwaff2
)
3509 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
, &pw_aff_add
);
3512 __isl_give isl_pw_aff
*isl_pw_aff_union_add(__isl_take isl_pw_aff
*pwaff1
,
3513 __isl_take isl_pw_aff
*pwaff2
)
3515 return isl_pw_aff_union_add_(pwaff1
, pwaff2
);
3518 static __isl_give isl_pw_aff
*pw_aff_mul(__isl_take isl_pw_aff
*pwaff1
,
3519 __isl_take isl_pw_aff
*pwaff2
)
3521 return isl_pw_aff_on_shared_domain(pwaff1
, pwaff2
, &isl_aff_mul
);
3524 __isl_give isl_pw_aff
*isl_pw_aff_mul(__isl_take isl_pw_aff
*pwaff1
,
3525 __isl_take isl_pw_aff
*pwaff2
)
3527 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
, &pw_aff_mul
);
3530 static __isl_give isl_pw_aff
*pw_aff_div(__isl_take isl_pw_aff
*pa1
,
3531 __isl_take isl_pw_aff
*pa2
)
3533 return isl_pw_aff_on_shared_domain(pa1
, pa2
, &isl_aff_div
);
3536 /* Divide "pa1" by "pa2", assuming "pa2" is a piecewise constant.
3538 __isl_give isl_pw_aff
*isl_pw_aff_div(__isl_take isl_pw_aff
*pa1
,
3539 __isl_take isl_pw_aff
*pa2
)
3543 is_cst
= isl_pw_aff_is_cst(pa2
);
3547 isl_die(isl_pw_aff_get_ctx(pa2
), isl_error_invalid
,
3548 "second argument should be a piecewise constant",
3550 return isl_pw_aff_align_params_pw_pw_and(pa1
, pa2
, &pw_aff_div
);
3552 isl_pw_aff_free(pa1
);
3553 isl_pw_aff_free(pa2
);
3557 /* Compute the quotient of the integer division of "pa1" by "pa2"
3558 * with rounding towards zero.
3559 * "pa2" is assumed to be a piecewise constant.
3561 * In particular, return
3563 * pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2)
3566 __isl_give isl_pw_aff
*isl_pw_aff_tdiv_q(__isl_take isl_pw_aff
*pa1
,
3567 __isl_take isl_pw_aff
*pa2
)
3573 is_cst
= isl_pw_aff_is_cst(pa2
);
3577 isl_die(isl_pw_aff_get_ctx(pa2
), isl_error_invalid
,
3578 "second argument should be a piecewise constant",
3581 pa1
= isl_pw_aff_div(pa1
, pa2
);
3583 cond
= isl_pw_aff_nonneg_set(isl_pw_aff_copy(pa1
));
3584 f
= isl_pw_aff_floor(isl_pw_aff_copy(pa1
));
3585 c
= isl_pw_aff_ceil(pa1
);
3586 return isl_pw_aff_cond(isl_set_indicator_function(cond
), f
, c
);
3588 isl_pw_aff_free(pa1
);
3589 isl_pw_aff_free(pa2
);
3593 /* Compute the remainder of the integer division of "pa1" by "pa2"
3594 * with rounding towards zero.
3595 * "pa2" is assumed to be a piecewise constant.
3597 * In particular, return
3599 * pa1 - pa2 * (pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2))
3602 __isl_give isl_pw_aff
*isl_pw_aff_tdiv_r(__isl_take isl_pw_aff
*pa1
,
3603 __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",
3615 res
= isl_pw_aff_tdiv_q(isl_pw_aff_copy(pa1
), isl_pw_aff_copy(pa2
));
3616 res
= isl_pw_aff_mul(pa2
, res
);
3617 res
= isl_pw_aff_sub(pa1
, res
);
3620 isl_pw_aff_free(pa1
);
3621 isl_pw_aff_free(pa2
);
3625 /* Does either of "pa1" or "pa2" involve any NaN2?
3627 static isl_bool
either_involves_nan(__isl_keep isl_pw_aff
*pa1
,
3628 __isl_keep isl_pw_aff
*pa2
)
3632 has_nan
= isl_pw_aff_involves_nan(pa1
);
3633 if (has_nan
< 0 || has_nan
)
3635 return isl_pw_aff_involves_nan(pa2
);
3638 /* Replace "pa1" and "pa2" (at least one of which involves a NaN)
3639 * by a NaN on their shared domain.
3641 * In principle, the result could be refined to only being NaN
3642 * on the parts of this domain where at least one of "pa1" or "pa2" is NaN.
3644 static __isl_give isl_pw_aff
*replace_by_nan(__isl_take isl_pw_aff
*pa1
,
3645 __isl_take isl_pw_aff
*pa2
)
3647 isl_local_space
*ls
;
3651 dom
= isl_set_intersect(isl_pw_aff_domain(pa1
), isl_pw_aff_domain(pa2
));
3652 ls
= isl_local_space_from_space(isl_set_get_space(dom
));
3653 pa
= isl_pw_aff_nan_on_domain(ls
);
3654 pa
= isl_pw_aff_intersect_domain(pa
, dom
);
3659 static __isl_give isl_pw_aff
*pw_aff_min(__isl_take isl_pw_aff
*pwaff1
,
3660 __isl_take isl_pw_aff
*pwaff2
)
3665 dom
= isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1
)),
3666 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2
)));
3667 le
= isl_pw_aff_le_set(isl_pw_aff_copy(pwaff1
),
3668 isl_pw_aff_copy(pwaff2
));
3669 dom
= isl_set_subtract(dom
, isl_set_copy(le
));
3670 return isl_pw_aff_select(le
, pwaff1
, dom
, pwaff2
);
3673 static __isl_give isl_pw_aff
*pw_aff_max(__isl_take isl_pw_aff
*pwaff1
,
3674 __isl_take isl_pw_aff
*pwaff2
)
3679 dom
= isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1
)),
3680 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2
)));
3681 ge
= isl_pw_aff_ge_set(isl_pw_aff_copy(pwaff1
),
3682 isl_pw_aff_copy(pwaff2
));
3683 dom
= isl_set_subtract(dom
, isl_set_copy(ge
));
3684 return isl_pw_aff_select(ge
, pwaff1
, dom
, pwaff2
);
3687 /* Return an expression for the minimum (if "max" is not set) or
3688 * the maximum (if "max" is set) of "pa1" and "pa2".
3689 * If either expression involves any NaN, then return a NaN
3690 * on the shared domain as result.
3692 static __isl_give isl_pw_aff
*pw_aff_min_max(__isl_take isl_pw_aff
*pa1
,
3693 __isl_take isl_pw_aff
*pa2
, int max
)
3697 has_nan
= either_involves_nan(pa1
, pa2
);
3699 pa1
= isl_pw_aff_free(pa1
);
3701 return replace_by_nan(pa1
, pa2
);
3704 return isl_pw_aff_align_params_pw_pw_and(pa1
, pa2
, &pw_aff_max
);
3706 return isl_pw_aff_align_params_pw_pw_and(pa1
, pa2
, &pw_aff_min
);
3709 /* Return an expression for the minimum of "pwaff1" and "pwaff2".
3711 __isl_give isl_pw_aff
*isl_pw_aff_min(__isl_take isl_pw_aff
*pwaff1
,
3712 __isl_take isl_pw_aff
*pwaff2
)
3714 return pw_aff_min_max(pwaff1
, pwaff2
, 0);
3717 /* Return an expression for the maximum of "pwaff1" and "pwaff2".
3719 __isl_give isl_pw_aff
*isl_pw_aff_max(__isl_take isl_pw_aff
*pwaff1
,
3720 __isl_take isl_pw_aff
*pwaff2
)
3722 return pw_aff_min_max(pwaff1
, pwaff2
, 1);
3725 static __isl_give isl_pw_aff
*pw_aff_list_reduce(
3726 __isl_take isl_pw_aff_list
*list
,
3727 __isl_give isl_pw_aff
*(*fn
)(__isl_take isl_pw_aff
*pwaff1
,
3728 __isl_take isl_pw_aff
*pwaff2
))
3737 ctx
= isl_pw_aff_list_get_ctx(list
);
3739 isl_die(ctx
, isl_error_invalid
,
3740 "list should contain at least one element", goto error
);
3742 res
= isl_pw_aff_copy(list
->p
[0]);
3743 for (i
= 1; i
< list
->n
; ++i
)
3744 res
= fn(res
, isl_pw_aff_copy(list
->p
[i
]));
3746 isl_pw_aff_list_free(list
);
3749 isl_pw_aff_list_free(list
);
3753 /* Return an isl_pw_aff that maps each element in the intersection of the
3754 * domains of the elements of list to the minimal corresponding affine
3757 __isl_give isl_pw_aff
*isl_pw_aff_list_min(__isl_take isl_pw_aff_list
*list
)
3759 return pw_aff_list_reduce(list
, &isl_pw_aff_min
);
3762 /* Return an isl_pw_aff that maps each element in the intersection of the
3763 * domains of the elements of list to the maximal corresponding affine
3766 __isl_give isl_pw_aff
*isl_pw_aff_list_max(__isl_take isl_pw_aff_list
*list
)
3768 return pw_aff_list_reduce(list
, &isl_pw_aff_max
);
3771 /* Mark the domains of "pwaff" as rational.
3773 __isl_give isl_pw_aff
*isl_pw_aff_set_rational(__isl_take isl_pw_aff
*pwaff
)
3777 pwaff
= isl_pw_aff_cow(pwaff
);
3783 for (i
= 0; i
< pwaff
->n
; ++i
) {
3784 pwaff
->p
[i
].set
= isl_set_set_rational(pwaff
->p
[i
].set
);
3785 if (!pwaff
->p
[i
].set
)
3786 return isl_pw_aff_free(pwaff
);
3792 /* Mark the domains of the elements of "list" as rational.
3794 __isl_give isl_pw_aff_list
*isl_pw_aff_list_set_rational(
3795 __isl_take isl_pw_aff_list
*list
)
3805 for (i
= 0; i
< n
; ++i
) {
3808 pa
= isl_pw_aff_list_get_pw_aff(list
, i
);
3809 pa
= isl_pw_aff_set_rational(pa
);
3810 list
= isl_pw_aff_list_set_pw_aff(list
, i
, pa
);
3816 /* Do the parameters of "aff" match those of "space"?
3818 isl_bool
isl_aff_matching_params(__isl_keep isl_aff
*aff
,
3819 __isl_keep isl_space
*space
)
3821 isl_space
*aff_space
;
3825 return isl_bool_error
;
3827 aff_space
= isl_aff_get_domain_space(aff
);
3829 match
= isl_space_has_equal_params(space
, aff_space
);
3831 isl_space_free(aff_space
);
3835 /* Check that the domain space of "aff" matches "space".
3837 isl_stat
isl_aff_check_match_domain_space(__isl_keep isl_aff
*aff
,
3838 __isl_keep isl_space
*space
)
3840 isl_space
*aff_space
;
3844 return isl_stat_error
;
3846 aff_space
= isl_aff_get_domain_space(aff
);
3848 match
= isl_space_has_equal_params(space
, aff_space
);
3852 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
3853 "parameters don't match", goto error
);
3854 match
= isl_space_tuple_is_equal(space
, isl_dim_in
,
3855 aff_space
, isl_dim_set
);
3859 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
3860 "domains don't match", goto error
);
3861 isl_space_free(aff_space
);
3864 isl_space_free(aff_space
);
3865 return isl_stat_error
;
3871 #include <isl_multi_no_explicit_domain.c>
3872 #include <isl_multi_templ.c>
3873 #include <isl_multi_apply_set.c>
3874 #include <isl_multi_arith_templ.c>
3875 #include <isl_multi_cmp.c>
3876 #include <isl_multi_dim_id_templ.c>
3877 #include <isl_multi_dims.c>
3878 #include <isl_multi_floor.c>
3879 #include <isl_multi_from_base_templ.c>
3880 #include <isl_multi_identity_templ.c>
3881 #include <isl_multi_move_dims_templ.c>
3882 #include <isl_multi_nan_templ.c>
3883 #include <isl_multi_product_templ.c>
3884 #include <isl_multi_splice_templ.c>
3885 #include <isl_multi_tuple_id_templ.c>
3886 #include <isl_multi_zero_templ.c>
3890 #include <isl_multi_gist.c>
3892 /* Construct an isl_multi_aff living in "space" that corresponds
3893 * to the affine transformation matrix "mat".
3895 __isl_give isl_multi_aff
*isl_multi_aff_from_aff_mat(
3896 __isl_take isl_space
*space
, __isl_take isl_mat
*mat
)
3899 isl_local_space
*ls
= NULL
;
3900 isl_multi_aff
*ma
= NULL
;
3901 isl_size n_row
, n_col
, n_out
, total
;
3907 ctx
= isl_mat_get_ctx(mat
);
3909 n_row
= isl_mat_rows(mat
);
3910 n_col
= isl_mat_cols(mat
);
3911 n_out
= isl_space_dim(space
, isl_dim_out
);
3912 total
= isl_space_dim(space
, isl_dim_all
);
3913 if (n_row
< 0 || n_col
< 0 || n_out
< 0 || total
< 0)
3916 isl_die(ctx
, isl_error_invalid
,
3917 "insufficient number of rows", goto error
);
3919 isl_die(ctx
, isl_error_invalid
,
3920 "insufficient number of columns", goto error
);
3921 if (1 + n_out
!= n_row
|| 2 + total
!= n_row
+ n_col
)
3922 isl_die(ctx
, isl_error_invalid
,
3923 "dimension mismatch", goto error
);
3925 ma
= isl_multi_aff_zero(isl_space_copy(space
));
3926 ls
= isl_local_space_from_space(isl_space_domain(space
));
3928 for (i
= 0; i
< n_row
- 1; ++i
) {
3932 v
= isl_vec_alloc(ctx
, 1 + n_col
);
3935 isl_int_set(v
->el
[0], mat
->row
[0][0]);
3936 isl_seq_cpy(v
->el
+ 1, mat
->row
[1 + i
], n_col
);
3937 v
= isl_vec_normalize(v
);
3938 aff
= isl_aff_alloc_vec(isl_local_space_copy(ls
), v
);
3939 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
3942 isl_local_space_free(ls
);
3946 isl_local_space_free(ls
);
3948 isl_multi_aff_free(ma
);
3952 /* Remove any internal structure of the domain of "ma".
3953 * If there is any such internal structure in the input,
3954 * then the name of the corresponding space is also removed.
3956 __isl_give isl_multi_aff
*isl_multi_aff_flatten_domain(
3957 __isl_take isl_multi_aff
*ma
)
3964 if (!ma
->space
->nested
[0])
3967 space
= isl_multi_aff_get_space(ma
);
3968 space
= isl_space_flatten_domain(space
);
3969 ma
= isl_multi_aff_reset_space(ma
, space
);
3974 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
3975 * of the space to its domain.
3977 __isl_give isl_multi_aff
*isl_multi_aff_domain_map(__isl_take isl_space
*space
)
3981 isl_local_space
*ls
;
3986 if (!isl_space_is_map(space
))
3987 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
3988 "not a map space", goto error
);
3990 n_in
= isl_space_dim(space
, isl_dim_in
);
3993 space
= isl_space_domain_map(space
);
3995 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
3997 isl_space_free(space
);
4001 space
= isl_space_domain(space
);
4002 ls
= isl_local_space_from_space(space
);
4003 for (i
= 0; i
< n_in
; ++i
) {
4006 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
4008 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
4010 isl_local_space_free(ls
);
4013 isl_space_free(space
);
4017 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
4018 * of the space to its range.
4020 __isl_give isl_multi_aff
*isl_multi_aff_range_map(__isl_take isl_space
*space
)
4023 isl_size n_in
, n_out
;
4024 isl_local_space
*ls
;
4029 if (!isl_space_is_map(space
))
4030 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
4031 "not a map space", goto error
);
4033 n_in
= isl_space_dim(space
, isl_dim_in
);
4034 n_out
= isl_space_dim(space
, isl_dim_out
);
4035 if (n_in
< 0 || n_out
< 0)
4037 space
= isl_space_range_map(space
);
4039 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
4041 isl_space_free(space
);
4045 space
= isl_space_domain(space
);
4046 ls
= isl_local_space_from_space(space
);
4047 for (i
= 0; i
< n_out
; ++i
) {
4050 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
4051 isl_dim_set
, n_in
+ i
);
4052 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
4054 isl_local_space_free(ls
);
4057 isl_space_free(space
);
4061 /* Given a map space, return an isl_pw_multi_aff that maps a wrapped copy
4062 * of the space to its range.
4064 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_range_map(
4065 __isl_take isl_space
*space
)
4067 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_range_map(space
));
4070 /* Given the space of a set and a range of set dimensions,
4071 * construct an isl_multi_aff that projects out those dimensions.
4073 __isl_give isl_multi_aff
*isl_multi_aff_project_out_map(
4074 __isl_take isl_space
*space
, enum isl_dim_type type
,
4075 unsigned first
, unsigned n
)
4079 isl_local_space
*ls
;
4084 if (!isl_space_is_set(space
))
4085 isl_die(isl_space_get_ctx(space
), isl_error_unsupported
,
4086 "expecting set space", goto error
);
4087 if (type
!= isl_dim_set
)
4088 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
4089 "only set dimensions can be projected out", goto error
);
4090 if (isl_space_check_range(space
, type
, first
, n
) < 0)
4093 dim
= isl_space_dim(space
, isl_dim_set
);
4097 space
= isl_space_from_domain(space
);
4098 space
= isl_space_add_dims(space
, isl_dim_out
, dim
- n
);
4101 return isl_multi_aff_alloc(space
);
4103 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
4104 space
= isl_space_domain(space
);
4105 ls
= isl_local_space_from_space(space
);
4107 for (i
= 0; i
< first
; ++i
) {
4110 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
4112 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
4115 for (i
= 0; i
< dim
- (first
+ n
); ++i
) {
4118 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
4119 isl_dim_set
, first
+ n
+ i
);
4120 ma
= isl_multi_aff_set_aff(ma
, first
+ i
, aff
);
4123 isl_local_space_free(ls
);
4126 isl_space_free(space
);
4130 /* Given the space of a set and a range of set dimensions,
4131 * construct an isl_pw_multi_aff that projects out those dimensions.
4133 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_project_out_map(
4134 __isl_take isl_space
*space
, enum isl_dim_type type
,
4135 unsigned first
, unsigned n
)
4139 ma
= isl_multi_aff_project_out_map(space
, type
, first
, n
);
4140 return isl_pw_multi_aff_from_multi_aff(ma
);
4143 /* Create an isl_pw_multi_aff with the given isl_multi_aff on a universe
4146 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_multi_aff(
4147 __isl_take isl_multi_aff
*ma
)
4149 isl_set
*dom
= isl_set_universe(isl_multi_aff_get_domain_space(ma
));
4150 return isl_pw_multi_aff_alloc(dom
, ma
);
4153 /* Create a piecewise multi-affine expression in the given space that maps each
4154 * input dimension to the corresponding output dimension.
4156 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_identity(
4157 __isl_take isl_space
*space
)
4159 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_identity(space
));
4162 /* Exploit the equalities in "eq" to simplify the affine expressions.
4164 static __isl_give isl_multi_aff
*isl_multi_aff_substitute_equalities(
4165 __isl_take isl_multi_aff
*maff
, __isl_take isl_basic_set
*eq
)
4169 maff
= isl_multi_aff_cow(maff
);
4173 for (i
= 0; i
< maff
->n
; ++i
) {
4174 maff
->u
.p
[i
] = isl_aff_substitute_equalities(maff
->u
.p
[i
],
4175 isl_basic_set_copy(eq
));
4180 isl_basic_set_free(eq
);
4183 isl_basic_set_free(eq
);
4184 isl_multi_aff_free(maff
);
4188 __isl_give isl_multi_aff
*isl_multi_aff_scale(__isl_take isl_multi_aff
*maff
,
4193 maff
= isl_multi_aff_cow(maff
);
4197 for (i
= 0; i
< maff
->n
; ++i
) {
4198 maff
->u
.p
[i
] = isl_aff_scale(maff
->u
.p
[i
], f
);
4200 return isl_multi_aff_free(maff
);
4206 __isl_give isl_multi_aff
*isl_multi_aff_add_on_domain(__isl_keep isl_set
*dom
,
4207 __isl_take isl_multi_aff
*maff1
, __isl_take isl_multi_aff
*maff2
)
4209 maff1
= isl_multi_aff_add(maff1
, maff2
);
4210 maff1
= isl_multi_aff_gist(maff1
, isl_set_copy(dom
));
4214 int isl_multi_aff_is_empty(__isl_keep isl_multi_aff
*maff
)
4222 /* Return the set of domain elements where "ma1" is lexicographically
4223 * smaller than or equal to "ma2".
4225 __isl_give isl_set
*isl_multi_aff_lex_le_set(__isl_take isl_multi_aff
*ma1
,
4226 __isl_take isl_multi_aff
*ma2
)
4228 return isl_multi_aff_lex_ge_set(ma2
, ma1
);
4231 /* Return the set of domain elements where "ma1" is lexicographically
4232 * smaller than "ma2".
4234 __isl_give isl_set
*isl_multi_aff_lex_lt_set(__isl_take isl_multi_aff
*ma1
,
4235 __isl_take isl_multi_aff
*ma2
)
4237 return isl_multi_aff_lex_gt_set(ma2
, ma1
);
4240 /* Return the set of domain elements where "ma1" and "ma2"
4243 static __isl_give isl_set
*isl_multi_aff_order_set(
4244 __isl_take isl_multi_aff
*ma1
, __isl_take isl_multi_aff
*ma2
,
4245 __isl_give isl_map
*order(__isl_take isl_space
*set_space
))
4248 isl_map
*map1
, *map2
;
4251 map1
= isl_map_from_multi_aff_internal(ma1
);
4252 map2
= isl_map_from_multi_aff_internal(ma2
);
4253 map
= isl_map_range_product(map1
, map2
);
4254 space
= isl_space_range(isl_map_get_space(map
));
4255 space
= isl_space_domain(isl_space_unwrap(space
));
4257 map
= isl_map_intersect_range(map
, isl_map_wrap(ge
));
4259 return isl_map_domain(map
);
4262 /* Return the set of domain elements where "ma1" is lexicographically
4263 * greater than or equal to "ma2".
4265 __isl_give isl_set
*isl_multi_aff_lex_ge_set(__isl_take isl_multi_aff
*ma1
,
4266 __isl_take isl_multi_aff
*ma2
)
4268 return isl_multi_aff_order_set(ma1
, ma2
, &isl_map_lex_ge
);
4271 /* Return the set of domain elements where "ma1" is lexicographically
4272 * greater than "ma2".
4274 __isl_give isl_set
*isl_multi_aff_lex_gt_set(__isl_take isl_multi_aff
*ma1
,
4275 __isl_take isl_multi_aff
*ma2
)
4277 return isl_multi_aff_order_set(ma1
, ma2
, &isl_map_lex_gt
);
4281 #define PW isl_pw_multi_aff
4283 #define EL isl_multi_aff
4285 #define EL_IS_ZERO is_empty
4289 #define IS_ZERO is_empty
4292 #undef DEFAULT_IS_ZERO
4293 #define DEFAULT_IS_ZERO 0
4297 #define NO_INSERT_DIMS
4301 #include <isl_pw_templ.c>
4302 #include <isl_pw_union_opt.c>
4307 #define BASE pw_multi_aff
4309 #include <isl_union_multi.c>
4310 #include <isl_union_neg.c>
4312 static __isl_give isl_pw_multi_aff
*pw_multi_aff_union_lexmax(
4313 __isl_take isl_pw_multi_aff
*pma1
,
4314 __isl_take isl_pw_multi_aff
*pma2
)
4316 return isl_pw_multi_aff_union_opt_cmp(pma1
, pma2
,
4317 &isl_multi_aff_lex_ge_set
);
4320 /* Given two piecewise multi affine expressions, return a piecewise
4321 * multi-affine expression defined on the union of the definition domains
4322 * of the inputs that is equal to the lexicographic maximum of the two
4323 * inputs on each cell. If only one of the two inputs is defined on
4324 * a given cell, then it is considered to be the maximum.
4326 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_union_lexmax(
4327 __isl_take isl_pw_multi_aff
*pma1
,
4328 __isl_take isl_pw_multi_aff
*pma2
)
4330 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
4331 &pw_multi_aff_union_lexmax
);
4334 static __isl_give isl_pw_multi_aff
*pw_multi_aff_union_lexmin(
4335 __isl_take isl_pw_multi_aff
*pma1
,
4336 __isl_take isl_pw_multi_aff
*pma2
)
4338 return isl_pw_multi_aff_union_opt_cmp(pma1
, pma2
,
4339 &isl_multi_aff_lex_le_set
);
4342 /* Given two piecewise multi affine expressions, return a piecewise
4343 * multi-affine expression defined on the union of the definition domains
4344 * of the inputs that is equal to the lexicographic minimum of the two
4345 * inputs on each cell. If only one of the two inputs is defined on
4346 * a given cell, then it is considered to be the minimum.
4348 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_union_lexmin(
4349 __isl_take isl_pw_multi_aff
*pma1
,
4350 __isl_take isl_pw_multi_aff
*pma2
)
4352 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
4353 &pw_multi_aff_union_lexmin
);
4356 static __isl_give isl_pw_multi_aff
*pw_multi_aff_add(
4357 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4359 return isl_pw_multi_aff_on_shared_domain(pma1
, pma2
,
4360 &isl_multi_aff_add
);
4363 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_add(
4364 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4366 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
4370 static __isl_give isl_pw_multi_aff
*pw_multi_aff_sub(
4371 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4373 return isl_pw_multi_aff_on_shared_domain(pma1
, pma2
,
4374 &isl_multi_aff_sub
);
4377 /* Subtract "pma2" from "pma1" and return the result.
4379 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_sub(
4380 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4382 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
4386 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_union_add(
4387 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4389 return isl_pw_multi_aff_union_add_(pma1
, pma2
);
4392 /* Compute the sum of "upa1" and "upa2" on the union of their domains,
4393 * with the actual sum on the shared domain and
4394 * the defined expression on the symmetric difference of the domains.
4396 __isl_give isl_union_pw_aff
*isl_union_pw_aff_union_add(
4397 __isl_take isl_union_pw_aff
*upa1
, __isl_take isl_union_pw_aff
*upa2
)
4399 return isl_union_pw_aff_union_add_(upa1
, upa2
);
4402 /* Compute the sum of "upma1" and "upma2" on the union of their domains,
4403 * with the actual sum on the shared domain and
4404 * the defined expression on the symmetric difference of the domains.
4406 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_union_add(
4407 __isl_take isl_union_pw_multi_aff
*upma1
,
4408 __isl_take isl_union_pw_multi_aff
*upma2
)
4410 return isl_union_pw_multi_aff_union_add_(upma1
, upma2
);
4413 /* Given two piecewise multi-affine expressions A -> B and C -> D,
4414 * construct a piecewise multi-affine expression [A -> C] -> [B -> D].
4416 static __isl_give isl_pw_multi_aff
*pw_multi_aff_product(
4417 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4421 isl_pw_multi_aff
*res
;
4426 n
= pma1
->n
* pma2
->n
;
4427 space
= isl_space_product(isl_space_copy(pma1
->dim
),
4428 isl_space_copy(pma2
->dim
));
4429 res
= isl_pw_multi_aff_alloc_size(space
, n
);
4431 for (i
= 0; i
< pma1
->n
; ++i
) {
4432 for (j
= 0; j
< pma2
->n
; ++j
) {
4436 domain
= isl_set_product(isl_set_copy(pma1
->p
[i
].set
),
4437 isl_set_copy(pma2
->p
[j
].set
));
4438 ma
= isl_multi_aff_product(
4439 isl_multi_aff_copy(pma1
->p
[i
].maff
),
4440 isl_multi_aff_copy(pma2
->p
[j
].maff
));
4441 res
= isl_pw_multi_aff_add_piece(res
, domain
, ma
);
4445 isl_pw_multi_aff_free(pma1
);
4446 isl_pw_multi_aff_free(pma2
);
4449 isl_pw_multi_aff_free(pma1
);
4450 isl_pw_multi_aff_free(pma2
);
4454 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_product(
4455 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4457 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
4458 &pw_multi_aff_product
);
4461 /* Subtract the initial "n" elements in "ma" with coefficients in "c" and
4462 * denominator "denom".
4463 * "denom" is allowed to be negative, in which case the actual denominator
4464 * is -denom and the expressions are added instead.
4466 static __isl_give isl_aff
*subtract_initial(__isl_take isl_aff
*aff
,
4467 __isl_keep isl_multi_aff
*ma
, int n
, isl_int
*c
, isl_int denom
)
4473 first
= isl_seq_first_non_zero(c
, n
);
4477 sign
= isl_int_sgn(denom
);
4479 isl_int_abs(d
, denom
);
4480 for (i
= first
; i
< n
; ++i
) {
4483 if (isl_int_is_zero(c
[i
]))
4485 aff_i
= isl_multi_aff_get_aff(ma
, i
);
4486 aff_i
= isl_aff_scale(aff_i
, c
[i
]);
4487 aff_i
= isl_aff_scale_down(aff_i
, d
);
4489 aff
= isl_aff_sub(aff
, aff_i
);
4491 aff
= isl_aff_add(aff
, aff_i
);
4498 /* Extract an affine expression that expresses the output dimension "pos"
4499 * of "bmap" in terms of the parameters and input dimensions from
4501 * Note that this expression may involve integer divisions defined
4502 * in terms of parameters and input dimensions.
4503 * The equality may also involve references to earlier (but not later)
4504 * output dimensions. These are replaced by the corresponding elements
4507 * If the equality is of the form
4509 * f(i) + h(j) + a x + g(i) = 0,
4511 * with f(i) a linear combinations of the parameters and input dimensions,
4512 * g(i) a linear combination of integer divisions defined in terms of the same
4513 * and h(j) a linear combinations of earlier output dimensions,
4514 * then the affine expression is
4516 * (-f(i) - g(i))/a - h(j)/a
4518 * If the equality is of the form
4520 * f(i) + h(j) - a x + g(i) = 0,
4522 * then the affine expression is
4524 * (f(i) + g(i))/a - h(j)/(-a)
4527 * If "div" refers to an integer division (i.e., it is smaller than
4528 * the number of integer divisions), then the equality constraint
4529 * does involve an integer division (the one at position "div") that
4530 * is defined in terms of output dimensions. However, this integer
4531 * division can be eliminated by exploiting a pair of constraints
4532 * x >= l and x <= l + n, with n smaller than the coefficient of "div"
4533 * in the equality constraint. "ineq" refers to inequality x >= l, i.e.,
4535 * In particular, let
4537 * x = e(i) + m floor(...)
4539 * with e(i) the expression derived above and floor(...) the integer
4540 * division involving output dimensions.
4551 * e(i) + m floor(...) - l = (e(i) + m floor(...) - l) mod m
4552 * = (e(i) - l) mod m
4556 * x - l = (e(i) - l) mod m
4560 * x = ((e(i) - l) mod m) + l
4562 * The variable "shift" below contains the expression -l, which may
4563 * also involve a linear combination of earlier output dimensions.
4565 static __isl_give isl_aff
*extract_aff_from_equality(
4566 __isl_keep isl_basic_map
*bmap
, int pos
, int eq
, int div
, int ineq
,
4567 __isl_keep isl_multi_aff
*ma
)
4570 isl_size n_div
, n_out
;
4572 isl_local_space
*ls
;
4573 isl_aff
*aff
, *shift
;
4576 ctx
= isl_basic_map_get_ctx(bmap
);
4577 ls
= isl_basic_map_get_local_space(bmap
);
4578 ls
= isl_local_space_domain(ls
);
4579 aff
= isl_aff_alloc(isl_local_space_copy(ls
));
4582 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
4583 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4584 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4585 if (n_out
< 0 || n_div
< 0)
4587 if (isl_int_is_neg(bmap
->eq
[eq
][o_out
+ pos
])) {
4588 isl_seq_cpy(aff
->v
->el
+ 1, bmap
->eq
[eq
], o_out
);
4589 isl_seq_cpy(aff
->v
->el
+ 1 + o_out
,
4590 bmap
->eq
[eq
] + o_out
+ n_out
, n_div
);
4592 isl_seq_neg(aff
->v
->el
+ 1, bmap
->eq
[eq
], o_out
);
4593 isl_seq_neg(aff
->v
->el
+ 1 + o_out
,
4594 bmap
->eq
[eq
] + o_out
+ n_out
, n_div
);
4597 isl_int_set_si(aff
->v
->el
[1 + o_out
+ div
], 0);
4598 isl_int_abs(aff
->v
->el
[0], bmap
->eq
[eq
][o_out
+ pos
]);
4599 aff
= subtract_initial(aff
, ma
, pos
, bmap
->eq
[eq
] + o_out
,
4600 bmap
->eq
[eq
][o_out
+ pos
]);
4602 shift
= isl_aff_alloc(isl_local_space_copy(ls
));
4605 isl_seq_cpy(shift
->v
->el
+ 1, bmap
->ineq
[ineq
], o_out
);
4606 isl_seq_cpy(shift
->v
->el
+ 1 + o_out
,
4607 bmap
->ineq
[ineq
] + o_out
+ n_out
, n_div
);
4608 isl_int_set_si(shift
->v
->el
[0], 1);
4609 shift
= subtract_initial(shift
, ma
, pos
,
4610 bmap
->ineq
[ineq
] + o_out
, ctx
->negone
);
4611 aff
= isl_aff_add(aff
, isl_aff_copy(shift
));
4612 mod
= isl_val_int_from_isl_int(ctx
,
4613 bmap
->eq
[eq
][o_out
+ n_out
+ div
]);
4614 mod
= isl_val_abs(mod
);
4615 aff
= isl_aff_mod_val(aff
, mod
);
4616 aff
= isl_aff_sub(aff
, shift
);
4619 isl_local_space_free(ls
);
4622 isl_local_space_free(ls
);
4627 /* Given a basic map with output dimensions defined
4628 * in terms of the parameters input dimensions and earlier
4629 * output dimensions using an equality (and possibly a pair on inequalities),
4630 * extract an isl_aff that expresses output dimension "pos" in terms
4631 * of the parameters and input dimensions.
4632 * Note that this expression may involve integer divisions defined
4633 * in terms of parameters and input dimensions.
4634 * "ma" contains the expressions corresponding to earlier output dimensions.
4636 * This function shares some similarities with
4637 * isl_basic_map_has_defining_equality and isl_constraint_get_bound.
4639 static __isl_give isl_aff
*extract_isl_aff_from_basic_map(
4640 __isl_keep isl_basic_map
*bmap
, int pos
, __isl_keep isl_multi_aff
*ma
)
4647 eq
= isl_basic_map_output_defining_equality(bmap
, pos
, &div
, &ineq
);
4648 if (eq
>= bmap
->n_eq
)
4649 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
4650 "unable to find suitable equality", return NULL
);
4651 aff
= extract_aff_from_equality(bmap
, pos
, eq
, div
, ineq
, ma
);
4653 aff
= isl_aff_remove_unused_divs(aff
);
4657 /* Given a basic map where each output dimension is defined
4658 * in terms of the parameters and input dimensions using an equality,
4659 * extract an isl_multi_aff that expresses the output dimensions in terms
4660 * of the parameters and input dimensions.
4662 static __isl_give isl_multi_aff
*extract_isl_multi_aff_from_basic_map(
4663 __isl_take isl_basic_map
*bmap
)
4672 ma
= isl_multi_aff_alloc(isl_basic_map_get_space(bmap
));
4673 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4675 ma
= isl_multi_aff_free(ma
);
4677 for (i
= 0; i
< n_out
; ++i
) {
4680 aff
= extract_isl_aff_from_basic_map(bmap
, i
, ma
);
4681 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
4684 isl_basic_map_free(bmap
);
4689 /* Given a basic set where each set dimension is defined
4690 * in terms of the parameters using an equality,
4691 * extract an isl_multi_aff that expresses the set dimensions in terms
4692 * of the parameters.
4694 __isl_give isl_multi_aff
*isl_multi_aff_from_basic_set_equalities(
4695 __isl_take isl_basic_set
*bset
)
4697 return extract_isl_multi_aff_from_basic_map(bset
);
4700 /* Create an isl_pw_multi_aff that is equivalent to
4701 * isl_map_intersect_domain(isl_map_from_basic_map(bmap), domain).
4702 * The given basic map is such that each output dimension is defined
4703 * in terms of the parameters and input dimensions using an equality.
4705 * Since some applications expect the result of isl_pw_multi_aff_from_map
4706 * to only contain integer affine expressions, we compute the floor
4707 * of the expression before returning.
4709 * Remove all constraints involving local variables without
4710 * an explicit representation (resulting in the removal of those
4711 * local variables) prior to the actual extraction to ensure
4712 * that the local spaces in which the resulting affine expressions
4713 * are created do not contain any unknown local variables.
4714 * Removing such constraints is safe because constraints involving
4715 * unknown local variables are not used to determine whether
4716 * a basic map is obviously single-valued.
4718 static __isl_give isl_pw_multi_aff
*plain_pw_multi_aff_from_map(
4719 __isl_take isl_set
*domain
, __isl_take isl_basic_map
*bmap
)
4723 bmap
= isl_basic_map_drop_constraint_involving_unknown_divs(bmap
);
4724 ma
= extract_isl_multi_aff_from_basic_map(bmap
);
4725 ma
= isl_multi_aff_floor(ma
);
4726 return isl_pw_multi_aff_alloc(domain
, ma
);
4729 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4730 * This obviously only works if the input "map" is single-valued.
4731 * If so, we compute the lexicographic minimum of the image in the form
4732 * of an isl_pw_multi_aff. Since the image is unique, it is equal
4733 * to its lexicographic minimum.
4734 * If the input is not single-valued, we produce an error.
4736 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_base(
4737 __isl_take isl_map
*map
)
4741 isl_pw_multi_aff
*pma
;
4743 sv
= isl_map_is_single_valued(map
);
4747 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
4748 "map is not single-valued", goto error
);
4749 map
= isl_map_make_disjoint(map
);
4753 pma
= isl_pw_multi_aff_empty(isl_map_get_space(map
));
4755 for (i
= 0; i
< map
->n
; ++i
) {
4756 isl_pw_multi_aff
*pma_i
;
4757 isl_basic_map
*bmap
;
4758 bmap
= isl_basic_map_copy(map
->p
[i
]);
4759 pma_i
= isl_basic_map_lexmin_pw_multi_aff(bmap
);
4760 pma
= isl_pw_multi_aff_add_disjoint(pma
, pma_i
);
4770 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
4771 * taking into account that the output dimension at position "d"
4772 * can be represented as
4774 * x = floor((e(...) + c1) / m)
4776 * given that constraint "i" is of the form
4778 * e(...) + c1 - m x >= 0
4781 * Let "map" be of the form
4785 * We construct a mapping
4787 * A -> [A -> x = floor(...)]
4789 * apply that to the map, obtaining
4791 * [A -> x = floor(...)] -> B
4793 * and equate dimension "d" to x.
4794 * We then compute a isl_pw_multi_aff representation of the resulting map
4795 * and plug in the mapping above.
4797 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_div(
4798 __isl_take isl_map
*map
, __isl_take isl_basic_map
*hull
, int d
, int i
)
4801 isl_space
*space
= NULL
;
4802 isl_local_space
*ls
;
4810 isl_pw_multi_aff
*pma
;
4813 is_set
= isl_map_is_set(map
);
4817 offset
= isl_basic_map_offset(hull
, isl_dim_out
);
4818 ctx
= isl_map_get_ctx(map
);
4819 space
= isl_space_domain(isl_map_get_space(map
));
4820 n_in
= isl_space_dim(space
, isl_dim_set
);
4821 n
= isl_space_dim(space
, isl_dim_all
);
4822 if (n_in
< 0 || n
< 0)
4825 v
= isl_vec_alloc(ctx
, 1 + 1 + n
);
4827 isl_int_neg(v
->el
[0], hull
->ineq
[i
][offset
+ d
]);
4828 isl_seq_cpy(v
->el
+ 1, hull
->ineq
[i
], 1 + n
);
4830 isl_basic_map_free(hull
);
4832 ls
= isl_local_space_from_space(isl_space_copy(space
));
4833 aff
= isl_aff_alloc_vec(ls
, v
);
4834 aff
= isl_aff_floor(aff
);
4836 isl_space_free(space
);
4837 ma
= isl_multi_aff_from_aff(aff
);
4839 ma
= isl_multi_aff_identity(isl_space_map_from_set(space
));
4840 ma
= isl_multi_aff_range_product(ma
,
4841 isl_multi_aff_from_aff(aff
));
4844 insert
= isl_map_from_multi_aff_internal(isl_multi_aff_copy(ma
));
4845 map
= isl_map_apply_domain(map
, insert
);
4846 map
= isl_map_equate(map
, isl_dim_in
, n_in
, isl_dim_out
, d
);
4847 pma
= isl_pw_multi_aff_from_map(map
);
4848 pma
= isl_pw_multi_aff_pullback_multi_aff(pma
, ma
);
4852 isl_space_free(space
);
4854 isl_basic_map_free(hull
);
4858 /* Is constraint "c" of the form
4860 * e(...) + c1 - m x >= 0
4864 * -e(...) + c2 + m x >= 0
4866 * where m > 1 and e only depends on parameters and input dimemnsions?
4868 * "offset" is the offset of the output dimensions
4869 * "pos" is the position of output dimension x.
4871 static int is_potential_div_constraint(isl_int
*c
, int offset
, int d
, int total
)
4873 if (isl_int_is_zero(c
[offset
+ d
]))
4875 if (isl_int_is_one(c
[offset
+ d
]))
4877 if (isl_int_is_negone(c
[offset
+ d
]))
4879 if (isl_seq_first_non_zero(c
+ offset
, d
) != -1)
4881 if (isl_seq_first_non_zero(c
+ offset
+ d
+ 1,
4882 total
- (offset
+ d
+ 1)) != -1)
4887 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4889 * As a special case, we first check if there is any pair of constraints,
4890 * shared by all the basic maps in "map" that force a given dimension
4891 * to be equal to the floor of some affine combination of the input dimensions.
4893 * In particular, if we can find two constraints
4895 * e(...) + c1 - m x >= 0 i.e., m x <= e(...) + c1
4899 * -e(...) + c2 + m x >= 0 i.e., m x >= e(...) - c2
4901 * where m > 1 and e only depends on parameters and input dimemnsions,
4904 * c1 + c2 < m i.e., -c2 >= c1 - (m - 1)
4906 * then we know that we can take
4908 * x = floor((e(...) + c1) / m)
4910 * without having to perform any computation.
4912 * Note that we know that
4916 * If c1 + c2 were 0, then we would have detected an equality during
4917 * simplification. If c1 + c2 were negative, then we would have detected
4920 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_check_div(
4921 __isl_take isl_map
*map
)
4929 isl_basic_map
*hull
;
4931 hull
= isl_map_unshifted_simple_hull(isl_map_copy(map
));
4932 dim
= isl_map_dim(map
, isl_dim_out
);
4933 total
= isl_basic_map_dim(hull
, isl_dim_all
);
4934 if (dim
< 0 || total
< 0)
4938 offset
= isl_basic_map_offset(hull
, isl_dim_out
);
4940 for (d
= 0; d
< dim
; ++d
) {
4941 for (i
= 0; i
< n
; ++i
) {
4942 if (!is_potential_div_constraint(hull
->ineq
[i
],
4943 offset
, d
, 1 + total
))
4945 for (j
= i
+ 1; j
< n
; ++j
) {
4946 if (!isl_seq_is_neg(hull
->ineq
[i
] + 1,
4947 hull
->ineq
[j
] + 1, total
))
4949 isl_int_add(sum
, hull
->ineq
[i
][0],
4951 if (isl_int_abs_lt(sum
,
4952 hull
->ineq
[i
][offset
+ d
]))
4959 if (isl_int_is_pos(hull
->ineq
[j
][offset
+ d
]))
4961 return pw_multi_aff_from_map_div(map
, hull
, d
, j
);
4965 isl_basic_map_free(hull
);
4966 return pw_multi_aff_from_map_base(map
);
4969 isl_basic_map_free(hull
);
4973 /* Given an affine expression
4975 * [A -> B] -> f(A,B)
4977 * construct an isl_multi_aff
4981 * such that dimension "d" in B' is set to "aff" and the remaining
4982 * dimensions are set equal to the corresponding dimensions in B.
4983 * "n_in" is the dimension of the space A.
4984 * "n_out" is the dimension of the space B.
4986 * If "is_set" is set, then the affine expression is of the form
4990 * and we construct an isl_multi_aff
4994 static __isl_give isl_multi_aff
*range_map(__isl_take isl_aff
*aff
, int d
,
4995 unsigned n_in
, unsigned n_out
, int is_set
)
4999 isl_space
*space
, *space2
;
5000 isl_local_space
*ls
;
5002 space
= isl_aff_get_domain_space(aff
);
5003 ls
= isl_local_space_from_space(isl_space_copy(space
));
5004 space2
= isl_space_copy(space
);
5006 space2
= isl_space_range(isl_space_unwrap(space2
));
5007 space
= isl_space_map_from_domain_and_range(space
, space2
);
5008 ma
= isl_multi_aff_alloc(space
);
5009 ma
= isl_multi_aff_set_aff(ma
, d
, aff
);
5011 for (i
= 0; i
< n_out
; ++i
) {
5014 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
5015 isl_dim_set
, n_in
+ i
);
5016 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
5019 isl_local_space_free(ls
);
5024 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
5025 * taking into account that the dimension at position "d" can be written as
5027 * x = m a + f(..) (1)
5029 * where m is equal to "gcd".
5030 * "i" is the index of the equality in "hull" that defines f(..).
5031 * In particular, the equality is of the form
5033 * f(..) - x + m g(existentials) = 0
5037 * -f(..) + x + m g(existentials) = 0
5039 * We basically plug (1) into "map", resulting in a map with "a"
5040 * in the range instead of "x". The corresponding isl_pw_multi_aff
5041 * defining "a" is then plugged back into (1) to obtain a definition for "x".
5043 * Specifically, given the input map
5047 * We first wrap it into a set
5051 * and define (1) on top of the corresponding space, resulting in "aff".
5052 * We use this to create an isl_multi_aff that maps the output position "d"
5053 * from "a" to "x", leaving all other (intput and output) dimensions unchanged.
5054 * We plug this into the wrapped map, unwrap the result and compute the
5055 * corresponding isl_pw_multi_aff.
5056 * The result is an expression
5064 * so that we can plug that into "aff", after extending the latter to
5070 * If "map" is actually a set, then there is no "A" space, meaning
5071 * that we do not need to perform any wrapping, and that the result
5072 * of the recursive call is of the form
5076 * which is plugged into a mapping of the form
5080 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_stride(
5081 __isl_take isl_map
*map
, __isl_take isl_basic_map
*hull
, int d
, int i
,
5086 isl_local_space
*ls
;
5089 isl_pw_multi_aff
*pma
, *id
;
5095 is_set
= isl_map_is_set(map
);
5099 n_in
= isl_basic_map_dim(hull
, isl_dim_in
);
5100 n_out
= isl_basic_map_dim(hull
, isl_dim_out
);
5101 if (n_in
< 0 || n_out
< 0)
5103 o_out
= isl_basic_map_offset(hull
, isl_dim_out
);
5108 set
= isl_map_wrap(map
);
5109 space
= isl_space_map_from_set(isl_set_get_space(set
));
5110 ma
= isl_multi_aff_identity(space
);
5111 ls
= isl_local_space_from_space(isl_set_get_space(set
));
5112 aff
= isl_aff_alloc(ls
);
5114 isl_int_set_si(aff
->v
->el
[0], 1);
5115 if (isl_int_is_one(hull
->eq
[i
][o_out
+ d
]))
5116 isl_seq_neg(aff
->v
->el
+ 1, hull
->eq
[i
],
5119 isl_seq_cpy(aff
->v
->el
+ 1, hull
->eq
[i
],
5121 isl_int_set(aff
->v
->el
[1 + o_out
+ d
], gcd
);
5123 ma
= isl_multi_aff_set_aff(ma
, n_in
+ d
, isl_aff_copy(aff
));
5124 set
= isl_set_preimage_multi_aff(set
, ma
);
5126 ma
= range_map(aff
, d
, n_in
, n_out
, is_set
);
5131 map
= isl_set_unwrap(set
);
5132 pma
= isl_pw_multi_aff_from_map(map
);
5135 space
= isl_pw_multi_aff_get_domain_space(pma
);
5136 space
= isl_space_map_from_set(space
);
5137 id
= isl_pw_multi_aff_identity(space
);
5138 pma
= isl_pw_multi_aff_range_product(id
, pma
);
5140 id
= isl_pw_multi_aff_from_multi_aff(ma
);
5141 pma
= isl_pw_multi_aff_pullback_pw_multi_aff(id
, pma
);
5143 isl_basic_map_free(hull
);
5147 isl_basic_map_free(hull
);
5151 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5152 * "hull" contains the equalities valid for "map".
5154 * Check if any of the output dimensions is "strided".
5155 * That is, we check if it can be written as
5159 * with m greater than 1, a some combination of existentially quantified
5160 * variables and f an expression in the parameters and input dimensions.
5161 * If so, we remove the stride in pw_multi_aff_from_map_stride.
5163 * Otherwise, we continue with pw_multi_aff_from_map_check_div for a further
5166 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_check_strides(
5167 __isl_take isl_map
*map
, __isl_take isl_basic_map
*hull
)
5176 n_div
= isl_basic_map_dim(hull
, isl_dim_div
);
5177 n_out
= isl_basic_map_dim(hull
, isl_dim_out
);
5178 if (n_div
< 0 || n_out
< 0)
5182 isl_basic_map_free(hull
);
5183 return pw_multi_aff_from_map_check_div(map
);
5188 o_div
= isl_basic_map_offset(hull
, isl_dim_div
);
5189 o_out
= isl_basic_map_offset(hull
, isl_dim_out
);
5191 for (i
= 0; i
< n_out
; ++i
) {
5192 for (j
= 0; j
< hull
->n_eq
; ++j
) {
5193 isl_int
*eq
= hull
->eq
[j
];
5194 isl_pw_multi_aff
*res
;
5196 if (!isl_int_is_one(eq
[o_out
+ i
]) &&
5197 !isl_int_is_negone(eq
[o_out
+ i
]))
5199 if (isl_seq_first_non_zero(eq
+ o_out
, i
) != -1)
5201 if (isl_seq_first_non_zero(eq
+ o_out
+ i
+ 1,
5202 n_out
- (i
+ 1)) != -1)
5204 isl_seq_gcd(eq
+ o_div
, n_div
, &gcd
);
5205 if (isl_int_is_zero(gcd
))
5207 if (isl_int_is_one(gcd
))
5210 res
= pw_multi_aff_from_map_stride(map
, hull
,
5218 isl_basic_map_free(hull
);
5219 return pw_multi_aff_from_map_check_div(map
);
5222 isl_basic_map_free(hull
);
5226 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5228 * As a special case, we first check if all output dimensions are uniquely
5229 * defined in terms of the parameters and input dimensions over the entire
5230 * domain. If so, we extract the desired isl_pw_multi_aff directly
5231 * from the affine hull of "map" and its domain.
5233 * Otherwise, continue with pw_multi_aff_from_map_check_strides for more
5236 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_map(__isl_take isl_map
*map
)
5240 isl_basic_map
*hull
;
5242 n
= isl_map_n_basic_map(map
);
5247 hull
= isl_map_unshifted_simple_hull(isl_map_copy(map
));
5248 hull
= isl_basic_map_plain_affine_hull(hull
);
5249 sv
= isl_basic_map_plain_is_single_valued(hull
);
5251 return plain_pw_multi_aff_from_map(isl_map_domain(map
),
5253 isl_basic_map_free(hull
);
5255 map
= isl_map_detect_equalities(map
);
5256 hull
= isl_map_unshifted_simple_hull(isl_map_copy(map
));
5257 sv
= isl_basic_map_plain_is_single_valued(hull
);
5259 return plain_pw_multi_aff_from_map(isl_map_domain(map
), hull
);
5261 return pw_multi_aff_from_map_check_strides(map
, hull
);
5262 isl_basic_map_free(hull
);
5268 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_set(__isl_take isl_set
*set
)
5270 return isl_pw_multi_aff_from_map(set
);
5273 /* Convert "map" into an isl_pw_multi_aff (if possible) and
5276 static isl_stat
pw_multi_aff_from_map(__isl_take isl_map
*map
, void *user
)
5278 isl_union_pw_multi_aff
**upma
= user
;
5279 isl_pw_multi_aff
*pma
;
5281 pma
= isl_pw_multi_aff_from_map(map
);
5282 *upma
= isl_union_pw_multi_aff_add_pw_multi_aff(*upma
, pma
);
5284 return *upma
? isl_stat_ok
: isl_stat_error
;
5287 /* Create an isl_union_pw_multi_aff with the given isl_aff on a universe
5290 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_aff(
5291 __isl_take isl_aff
*aff
)
5294 isl_pw_multi_aff
*pma
;
5296 ma
= isl_multi_aff_from_aff(aff
);
5297 pma
= isl_pw_multi_aff_from_multi_aff(ma
);
5298 return isl_union_pw_multi_aff_from_pw_multi_aff(pma
);
5301 /* Try and create an isl_union_pw_multi_aff that is equivalent
5302 * to the given isl_union_map.
5303 * The isl_union_map is required to be single-valued in each space.
5304 * Otherwise, an error is produced.
5306 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_union_map(
5307 __isl_take isl_union_map
*umap
)
5310 isl_union_pw_multi_aff
*upma
;
5312 space
= isl_union_map_get_space(umap
);
5313 upma
= isl_union_pw_multi_aff_empty(space
);
5314 if (isl_union_map_foreach_map(umap
, &pw_multi_aff_from_map
, &upma
) < 0)
5315 upma
= isl_union_pw_multi_aff_free(upma
);
5316 isl_union_map_free(umap
);
5321 /* Try and create an isl_union_pw_multi_aff that is equivalent
5322 * to the given isl_union_set.
5323 * The isl_union_set is required to be a singleton in each space.
5324 * Otherwise, an error is produced.
5326 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_union_set(
5327 __isl_take isl_union_set
*uset
)
5329 return isl_union_pw_multi_aff_from_union_map(uset
);
5332 /* Return the piecewise affine expression "set ? 1 : 0".
5334 __isl_give isl_pw_aff
*isl_set_indicator_function(__isl_take isl_set
*set
)
5337 isl_space
*space
= isl_set_get_space(set
);
5338 isl_local_space
*ls
= isl_local_space_from_space(space
);
5339 isl_aff
*zero
= isl_aff_zero_on_domain(isl_local_space_copy(ls
));
5340 isl_aff
*one
= isl_aff_zero_on_domain(ls
);
5342 one
= isl_aff_add_constant_si(one
, 1);
5343 pa
= isl_pw_aff_alloc(isl_set_copy(set
), one
);
5344 set
= isl_set_complement(set
);
5345 pa
= isl_pw_aff_add_disjoint(pa
, isl_pw_aff_alloc(set
, zero
));
5350 /* Plug in "subs" for dimension "type", "pos" of "aff".
5352 * Let i be the dimension to replace and let "subs" be of the form
5356 * and "aff" of the form
5362 * (a f + d g')/(m d)
5364 * where g' is the result of plugging in "subs" in each of the integer
5367 __isl_give isl_aff
*isl_aff_substitute(__isl_take isl_aff
*aff
,
5368 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
5374 aff
= isl_aff_cow(aff
);
5376 return isl_aff_free(aff
);
5378 ctx
= isl_aff_get_ctx(aff
);
5379 if (!isl_space_is_equal(aff
->ls
->dim
, subs
->ls
->dim
))
5380 isl_die(ctx
, isl_error_invalid
,
5381 "spaces don't match", return isl_aff_free(aff
));
5382 n_div
= isl_local_space_dim(subs
->ls
, isl_dim_div
);
5384 return isl_aff_free(aff
);
5386 isl_die(ctx
, isl_error_unsupported
,
5387 "cannot handle divs yet", return isl_aff_free(aff
));
5389 aff
->ls
= isl_local_space_substitute(aff
->ls
, type
, pos
, subs
);
5391 return isl_aff_free(aff
);
5393 aff
->v
= isl_vec_cow(aff
->v
);
5395 return isl_aff_free(aff
);
5397 pos
+= isl_local_space_offset(aff
->ls
, type
);
5400 isl_seq_substitute(aff
->v
->el
, pos
, subs
->v
->el
,
5401 aff
->v
->size
, subs
->v
->size
, v
);
5407 /* Plug in "subs" for dimension "type", "pos" in each of the affine
5408 * expressions in "maff".
5410 __isl_give isl_multi_aff
*isl_multi_aff_substitute(
5411 __isl_take isl_multi_aff
*maff
, enum isl_dim_type type
, unsigned pos
,
5412 __isl_keep isl_aff
*subs
)
5416 maff
= isl_multi_aff_cow(maff
);
5418 return isl_multi_aff_free(maff
);
5420 if (type
== isl_dim_in
)
5423 for (i
= 0; i
< maff
->n
; ++i
) {
5424 maff
->u
.p
[i
] = isl_aff_substitute(maff
->u
.p
[i
],
5427 return isl_multi_aff_free(maff
);
5433 /* Plug in "subs" for dimension "type", "pos" of "pma".
5435 * pma is of the form
5439 * while subs is of the form
5441 * v' = B_j(v) -> S_j
5443 * Each pair i,j such that C_ij = A_i \cap B_i is non-empty
5444 * has a contribution in the result, in particular
5446 * C_ij(S_j) -> M_i(S_j)
5448 * Note that plugging in S_j in C_ij may also result in an empty set
5449 * and this contribution should simply be discarded.
5451 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_substitute(
5452 __isl_take isl_pw_multi_aff
*pma
, enum isl_dim_type type
, unsigned pos
,
5453 __isl_keep isl_pw_aff
*subs
)
5456 isl_pw_multi_aff
*res
;
5459 return isl_pw_multi_aff_free(pma
);
5461 n
= pma
->n
* subs
->n
;
5462 res
= isl_pw_multi_aff_alloc_size(isl_space_copy(pma
->dim
), n
);
5464 for (i
= 0; i
< pma
->n
; ++i
) {
5465 for (j
= 0; j
< subs
->n
; ++j
) {
5467 isl_multi_aff
*res_ij
;
5470 common
= isl_set_intersect(
5471 isl_set_copy(pma
->p
[i
].set
),
5472 isl_set_copy(subs
->p
[j
].set
));
5473 common
= isl_set_substitute(common
,
5474 type
, pos
, subs
->p
[j
].aff
);
5475 empty
= isl_set_plain_is_empty(common
);
5476 if (empty
< 0 || empty
) {
5477 isl_set_free(common
);
5483 res_ij
= isl_multi_aff_substitute(
5484 isl_multi_aff_copy(pma
->p
[i
].maff
),
5485 type
, pos
, subs
->p
[j
].aff
);
5487 res
= isl_pw_multi_aff_add_piece(res
, common
, res_ij
);
5491 isl_pw_multi_aff_free(pma
);
5494 isl_pw_multi_aff_free(pma
);
5495 isl_pw_multi_aff_free(res
);
5499 /* Compute the preimage of a range of dimensions in the affine expression "src"
5500 * under "ma" and put the result in "dst". The number of dimensions in "src"
5501 * that precede the range is given by "n_before". The number of dimensions
5502 * in the range is given by the number of output dimensions of "ma".
5503 * The number of dimensions that follow the range is given by "n_after".
5504 * If "has_denom" is set (to one),
5505 * then "src" and "dst" have an extra initial denominator.
5506 * "n_div_ma" is the number of existentials in "ma"
5507 * "n_div_bset" is the number of existentials in "src"
5508 * The resulting "dst" (which is assumed to have been allocated by
5509 * the caller) contains coefficients for both sets of existentials,
5510 * first those in "ma" and then those in "src".
5511 * f, c1, c2 and g are temporary objects that have been initialized
5514 * Let src represent the expression
5516 * (a(p) + f_u u + b v + f_w w + c(divs))/d
5518 * and let ma represent the expressions
5520 * v_i = (r_i(p) + s_i(y) + t_i(divs'))/m_i
5522 * We start out with the following expression for dst:
5524 * (a(p) + f_u u + 0 y + f_w w + 0 divs' + c(divs) + f \sum_i b_i v_i)/d
5526 * with the multiplication factor f initially equal to 1
5527 * and f \sum_i b_i v_i kept separately.
5528 * For each x_i that we substitute, we multiply the numerator
5529 * (and denominator) of dst by c_1 = m_i and add the numerator
5530 * of the x_i expression multiplied by c_2 = f b_i,
5531 * after removing the common factors of c_1 and c_2.
5532 * The multiplication factor f also needs to be multiplied by c_1
5533 * for the next x_j, j > i.
5535 isl_stat
isl_seq_preimage(isl_int
*dst
, isl_int
*src
,
5536 __isl_keep isl_multi_aff
*ma
, int n_before
, int n_after
,
5537 int n_div_ma
, int n_div_bmap
,
5538 isl_int f
, isl_int c1
, isl_int c2
, isl_int g
, int has_denom
)
5541 isl_size n_param
, n_in
, n_out
;
5544 n_param
= isl_multi_aff_dim(ma
, isl_dim_param
);
5545 n_in
= isl_multi_aff_dim(ma
, isl_dim_in
);
5546 n_out
= isl_multi_aff_dim(ma
, isl_dim_out
);
5547 if (n_param
< 0 || n_in
< 0 || n_out
< 0)
5548 return isl_stat_error
;
5550 isl_seq_cpy(dst
, src
, has_denom
+ 1 + n_param
+ n_before
);
5551 o_dst
= o_src
= has_denom
+ 1 + n_param
+ n_before
;
5552 isl_seq_clr(dst
+ o_dst
, n_in
);
5555 isl_seq_cpy(dst
+ o_dst
, src
+ o_src
, n_after
);
5558 isl_seq_clr(dst
+ o_dst
, n_div_ma
);
5560 isl_seq_cpy(dst
+ o_dst
, src
+ o_src
, n_div_bmap
);
5562 isl_int_set_si(f
, 1);
5564 for (i
= 0; i
< n_out
; ++i
) {
5565 int offset
= has_denom
+ 1 + n_param
+ n_before
+ i
;
5567 if (isl_int_is_zero(src
[offset
]))
5569 isl_int_set(c1
, ma
->u
.p
[i
]->v
->el
[0]);
5570 isl_int_mul(c2
, f
, src
[offset
]);
5571 isl_int_gcd(g
, c1
, c2
);
5572 isl_int_divexact(c1
, c1
, g
);
5573 isl_int_divexact(c2
, c2
, g
);
5575 isl_int_mul(f
, f
, c1
);
5578 isl_seq_combine(dst
+ o_dst
, c1
, dst
+ o_dst
,
5579 c2
, ma
->u
.p
[i
]->v
->el
+ o_src
, 1 + n_param
);
5580 o_dst
+= 1 + n_param
;
5581 o_src
+= 1 + n_param
;
5582 isl_seq_scale(dst
+ o_dst
, dst
+ o_dst
, c1
, n_before
);
5584 isl_seq_combine(dst
+ o_dst
, c1
, dst
+ o_dst
,
5585 c2
, ma
->u
.p
[i
]->v
->el
+ o_src
, n_in
);
5588 isl_seq_scale(dst
+ o_dst
, dst
+ o_dst
, c1
, n_after
);
5590 isl_seq_combine(dst
+ o_dst
, c1
, dst
+ o_dst
,
5591 c2
, ma
->u
.p
[i
]->v
->el
+ o_src
, n_div_ma
);
5594 isl_seq_scale(dst
+ o_dst
, dst
+ o_dst
, c1
, n_div_bmap
);
5596 isl_int_mul(dst
[0], dst
[0], c1
);
5602 /* Compute the pullback of "aff" by the function represented by "ma".
5603 * In other words, plug in "ma" in "aff". The result is an affine expression
5604 * defined over the domain space of "ma".
5606 * If "aff" is represented by
5608 * (a(p) + b x + c(divs))/d
5610 * and ma is represented by
5612 * x = D(p) + F(y) + G(divs')
5614 * then the result is
5616 * (a(p) + b D(p) + b F(y) + b G(divs') + c(divs))/d
5618 * The divs in the local space of the input are similarly adjusted
5619 * through a call to isl_local_space_preimage_multi_aff.
5621 __isl_give isl_aff
*isl_aff_pullback_multi_aff(__isl_take isl_aff
*aff
,
5622 __isl_take isl_multi_aff
*ma
)
5624 isl_aff
*res
= NULL
;
5625 isl_local_space
*ls
;
5626 isl_size n_div_aff
, n_div_ma
;
5627 isl_int f
, c1
, c2
, g
;
5629 ma
= isl_multi_aff_align_divs(ma
);
5633 n_div_aff
= isl_aff_dim(aff
, isl_dim_div
);
5634 n_div_ma
= ma
->n
? isl_aff_dim(ma
->u
.p
[0], isl_dim_div
) : 0;
5635 if (n_div_aff
< 0 || n_div_ma
< 0)
5638 ls
= isl_aff_get_domain_local_space(aff
);
5639 ls
= isl_local_space_preimage_multi_aff(ls
, isl_multi_aff_copy(ma
));
5640 res
= isl_aff_alloc(ls
);
5649 if (isl_seq_preimage(res
->v
->el
, aff
->v
->el
, ma
, 0, 0,
5650 n_div_ma
, n_div_aff
, f
, c1
, c2
, g
, 1) < 0)
5651 res
= isl_aff_free(res
);
5659 isl_multi_aff_free(ma
);
5660 res
= isl_aff_normalize(res
);
5664 isl_multi_aff_free(ma
);
5669 /* Compute the pullback of "aff1" by the function represented by "aff2".
5670 * In other words, plug in "aff2" in "aff1". The result is an affine expression
5671 * defined over the domain space of "aff1".
5673 * The domain of "aff1" should match the range of "aff2", which means
5674 * that it should be single-dimensional.
5676 __isl_give isl_aff
*isl_aff_pullback_aff(__isl_take isl_aff
*aff1
,
5677 __isl_take isl_aff
*aff2
)
5681 ma
= isl_multi_aff_from_aff(aff2
);
5682 return isl_aff_pullback_multi_aff(aff1
, ma
);
5685 /* Compute the pullback of "ma1" by the function represented by "ma2".
5686 * In other words, plug in "ma2" in "ma1".
5688 * The parameters of "ma1" and "ma2" are assumed to have been aligned.
5690 static __isl_give isl_multi_aff
*isl_multi_aff_pullback_multi_aff_aligned(
5691 __isl_take isl_multi_aff
*ma1
, __isl_take isl_multi_aff
*ma2
)
5694 isl_space
*space
= NULL
;
5696 ma2
= isl_multi_aff_align_divs(ma2
);
5697 ma1
= isl_multi_aff_cow(ma1
);
5701 space
= isl_space_join(isl_multi_aff_get_space(ma2
),
5702 isl_multi_aff_get_space(ma1
));
5704 for (i
= 0; i
< ma1
->n
; ++i
) {
5705 ma1
->u
.p
[i
] = isl_aff_pullback_multi_aff(ma1
->u
.p
[i
],
5706 isl_multi_aff_copy(ma2
));
5711 ma1
= isl_multi_aff_reset_space(ma1
, space
);
5712 isl_multi_aff_free(ma2
);
5715 isl_space_free(space
);
5716 isl_multi_aff_free(ma2
);
5717 isl_multi_aff_free(ma1
);
5721 /* Compute the pullback of "ma1" by the function represented by "ma2".
5722 * In other words, plug in "ma2" in "ma1".
5724 __isl_give isl_multi_aff
*isl_multi_aff_pullback_multi_aff(
5725 __isl_take isl_multi_aff
*ma1
, __isl_take isl_multi_aff
*ma2
)
5727 return isl_multi_aff_align_params_multi_multi_and(ma1
, ma2
,
5728 &isl_multi_aff_pullback_multi_aff_aligned
);
5731 /* Extend the local space of "dst" to include the divs
5732 * in the local space of "src".
5734 * If "src" does not have any divs or if the local spaces of "dst" and
5735 * "src" are the same, then no extension is required.
5737 __isl_give isl_aff
*isl_aff_align_divs(__isl_take isl_aff
*dst
,
5738 __isl_keep isl_aff
*src
)
5741 isl_size src_n_div
, dst_n_div
;
5748 return isl_aff_free(dst
);
5750 ctx
= isl_aff_get_ctx(src
);
5751 equal
= isl_local_space_has_equal_space(src
->ls
, dst
->ls
);
5753 return isl_aff_free(dst
);
5755 isl_die(ctx
, isl_error_invalid
,
5756 "spaces don't match", goto error
);
5758 src_n_div
= isl_local_space_dim(src
->ls
, isl_dim_div
);
5759 dst_n_div
= isl_local_space_dim(dst
->ls
, isl_dim_div
);
5762 equal
= isl_local_space_is_equal(src
->ls
, dst
->ls
);
5763 if (equal
< 0 || src_n_div
< 0 || dst_n_div
< 0)
5764 return isl_aff_free(dst
);
5768 exp1
= isl_alloc_array(ctx
, int, src_n_div
);
5769 exp2
= isl_alloc_array(ctx
, int, dst_n_div
);
5770 if (!exp1
|| (dst_n_div
&& !exp2
))
5773 div
= isl_merge_divs(src
->ls
->div
, dst
->ls
->div
, exp1
, exp2
);
5774 dst
= isl_aff_expand_divs(dst
, div
, exp2
);
5782 return isl_aff_free(dst
);
5785 /* Adjust the local spaces of the affine expressions in "maff"
5786 * such that they all have the save divs.
5788 __isl_give isl_multi_aff
*isl_multi_aff_align_divs(
5789 __isl_take isl_multi_aff
*maff
)
5797 maff
= isl_multi_aff_cow(maff
);
5801 for (i
= 1; i
< maff
->n
; ++i
)
5802 maff
->u
.p
[0] = isl_aff_align_divs(maff
->u
.p
[0], maff
->u
.p
[i
]);
5803 for (i
= 1; i
< maff
->n
; ++i
) {
5804 maff
->u
.p
[i
] = isl_aff_align_divs(maff
->u
.p
[i
], maff
->u
.p
[0]);
5806 return isl_multi_aff_free(maff
);
5812 __isl_give isl_aff
*isl_aff_lift(__isl_take isl_aff
*aff
)
5814 aff
= isl_aff_cow(aff
);
5818 aff
->ls
= isl_local_space_lift(aff
->ls
);
5820 return isl_aff_free(aff
);
5825 /* Lift "maff" to a space with extra dimensions such that the result
5826 * has no more existentially quantified variables.
5827 * If "ls" is not NULL, then *ls is assigned the local space that lies
5828 * at the basis of the lifting applied to "maff".
5830 __isl_give isl_multi_aff
*isl_multi_aff_lift(__isl_take isl_multi_aff
*maff
,
5831 __isl_give isl_local_space
**ls
)
5845 isl_space
*space
= isl_multi_aff_get_domain_space(maff
);
5846 *ls
= isl_local_space_from_space(space
);
5848 return isl_multi_aff_free(maff
);
5853 maff
= isl_multi_aff_cow(maff
);
5854 maff
= isl_multi_aff_align_divs(maff
);
5858 n_div
= isl_aff_dim(maff
->u
.p
[0], isl_dim_div
);
5860 return isl_multi_aff_free(maff
);
5861 space
= isl_multi_aff_get_space(maff
);
5862 space
= isl_space_lift(isl_space_domain(space
), n_div
);
5863 space
= isl_space_extend_domain_with_range(space
,
5864 isl_multi_aff_get_space(maff
));
5866 return isl_multi_aff_free(maff
);
5867 isl_space_free(maff
->space
);
5868 maff
->space
= space
;
5871 *ls
= isl_aff_get_domain_local_space(maff
->u
.p
[0]);
5873 return isl_multi_aff_free(maff
);
5876 for (i
= 0; i
< maff
->n
; ++i
) {
5877 maff
->u
.p
[i
] = isl_aff_lift(maff
->u
.p
[i
]);
5885 isl_local_space_free(*ls
);
5886 return isl_multi_aff_free(maff
);
5890 #define TYPE isl_pw_multi_aff
5892 #include "check_type_range_templ.c"
5894 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma".
5896 __isl_give isl_pw_aff
*isl_pw_multi_aff_get_pw_aff(
5897 __isl_keep isl_pw_multi_aff
*pma
, int pos
)
5904 if (isl_pw_multi_aff_check_range(pma
, isl_dim_out
, pos
, 1) < 0)
5907 n_out
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
5911 space
= isl_pw_multi_aff_get_space(pma
);
5912 space
= isl_space_drop_dims(space
, isl_dim_out
,
5913 pos
+ 1, n_out
- pos
- 1);
5914 space
= isl_space_drop_dims(space
, isl_dim_out
, 0, pos
);
5916 pa
= isl_pw_aff_alloc_size(space
, pma
->n
);
5917 for (i
= 0; i
< pma
->n
; ++i
) {
5919 aff
= isl_multi_aff_get_aff(pma
->p
[i
].maff
, pos
);
5920 pa
= isl_pw_aff_add_piece(pa
, isl_set_copy(pma
->p
[i
].set
), aff
);
5926 /* Return an isl_pw_multi_aff with the given "set" as domain and
5927 * an unnamed zero-dimensional range.
5929 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_domain(
5930 __isl_take isl_set
*set
)
5935 space
= isl_set_get_space(set
);
5936 space
= isl_space_from_domain(space
);
5937 ma
= isl_multi_aff_zero(space
);
5938 return isl_pw_multi_aff_alloc(set
, ma
);
5941 /* Add an isl_pw_multi_aff with the given "set" as domain and
5942 * an unnamed zero-dimensional range to *user.
5944 static isl_stat
add_pw_multi_aff_from_domain(__isl_take isl_set
*set
,
5947 isl_union_pw_multi_aff
**upma
= user
;
5948 isl_pw_multi_aff
*pma
;
5950 pma
= isl_pw_multi_aff_from_domain(set
);
5951 *upma
= isl_union_pw_multi_aff_add_pw_multi_aff(*upma
, pma
);
5956 /* Return an isl_union_pw_multi_aff with the given "uset" as domain and
5957 * an unnamed zero-dimensional range.
5959 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_domain(
5960 __isl_take isl_union_set
*uset
)
5963 isl_union_pw_multi_aff
*upma
;
5968 space
= isl_union_set_get_space(uset
);
5969 upma
= isl_union_pw_multi_aff_empty(space
);
5971 if (isl_union_set_foreach_set(uset
,
5972 &add_pw_multi_aff_from_domain
, &upma
) < 0)
5975 isl_union_set_free(uset
);
5978 isl_union_set_free(uset
);
5979 isl_union_pw_multi_aff_free(upma
);
5983 /* Local data for bin_entry and the callback "fn".
5985 struct isl_union_pw_multi_aff_bin_data
{
5986 isl_union_pw_multi_aff
*upma2
;
5987 isl_union_pw_multi_aff
*res
;
5988 isl_pw_multi_aff
*pma
;
5989 isl_stat (*fn
)(__isl_take isl_pw_multi_aff
*pma
, void *user
);
5992 /* Given an isl_pw_multi_aff from upma1, store it in data->pma
5993 * and call data->fn for each isl_pw_multi_aff in data->upma2.
5995 static isl_stat
bin_entry(__isl_take isl_pw_multi_aff
*pma
, void *user
)
5997 struct isl_union_pw_multi_aff_bin_data
*data
= user
;
6001 r
= isl_union_pw_multi_aff_foreach_pw_multi_aff(data
->upma2
,
6003 isl_pw_multi_aff_free(pma
);
6008 /* Call "fn" on each pair of isl_pw_multi_affs in "upma1" and "upma2".
6009 * The isl_pw_multi_aff from upma1 is stored in data->pma (where data is
6010 * passed as user field) and the isl_pw_multi_aff from upma2 is available
6011 * as *entry. The callback should adjust data->res if desired.
6013 static __isl_give isl_union_pw_multi_aff
*bin_op(
6014 __isl_take isl_union_pw_multi_aff
*upma1
,
6015 __isl_take isl_union_pw_multi_aff
*upma2
,
6016 isl_stat (*fn
)(__isl_take isl_pw_multi_aff
*pma
, void *user
))
6019 struct isl_union_pw_multi_aff_bin_data data
= { NULL
, NULL
, NULL
, fn
};
6021 space
= isl_union_pw_multi_aff_get_space(upma2
);
6022 upma1
= isl_union_pw_multi_aff_align_params(upma1
, space
);
6023 space
= isl_union_pw_multi_aff_get_space(upma1
);
6024 upma2
= isl_union_pw_multi_aff_align_params(upma2
, space
);
6026 if (!upma1
|| !upma2
)
6030 data
.res
= isl_union_pw_multi_aff_alloc_same_size(upma1
);
6031 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma1
,
6032 &bin_entry
, &data
) < 0)
6035 isl_union_pw_multi_aff_free(upma1
);
6036 isl_union_pw_multi_aff_free(upma2
);
6039 isl_union_pw_multi_aff_free(upma1
);
6040 isl_union_pw_multi_aff_free(upma2
);
6041 isl_union_pw_multi_aff_free(data
.res
);
6045 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
6046 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
6048 static __isl_give isl_pw_multi_aff
*pw_multi_aff_range_product(
6049 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
6053 space
= isl_space_range_product(isl_pw_multi_aff_get_space(pma1
),
6054 isl_pw_multi_aff_get_space(pma2
));
6055 return isl_pw_multi_aff_on_shared_domain_in(pma1
, pma2
, space
,
6056 &isl_multi_aff_range_product
);
6059 /* Given two isl_pw_multi_affs A -> B and C -> D,
6060 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
6062 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_range_product(
6063 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
6065 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
6066 &pw_multi_aff_range_product
);
6069 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
6070 * construct an isl_pw_multi_aff (A * C) -> (B, D).
6072 static __isl_give isl_pw_multi_aff
*pw_multi_aff_flat_range_product(
6073 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
6077 space
= isl_space_range_product(isl_pw_multi_aff_get_space(pma1
),
6078 isl_pw_multi_aff_get_space(pma2
));
6079 space
= isl_space_flatten_range(space
);
6080 return isl_pw_multi_aff_on_shared_domain_in(pma1
, pma2
, space
,
6081 &isl_multi_aff_flat_range_product
);
6084 /* Given two isl_pw_multi_affs A -> B and C -> D,
6085 * construct an isl_pw_multi_aff (A * C) -> (B, D).
6087 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_flat_range_product(
6088 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
6090 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
6091 &pw_multi_aff_flat_range_product
);
6094 /* If data->pma and "pma2" have the same domain space, then compute
6095 * their flat range product and the result to data->res.
6097 static isl_stat
flat_range_product_entry(__isl_take isl_pw_multi_aff
*pma2
,
6100 struct isl_union_pw_multi_aff_bin_data
*data
= user
;
6102 if (!isl_space_tuple_is_equal(data
->pma
->dim
, isl_dim_in
,
6103 pma2
->dim
, isl_dim_in
)) {
6104 isl_pw_multi_aff_free(pma2
);
6108 pma2
= isl_pw_multi_aff_flat_range_product(
6109 isl_pw_multi_aff_copy(data
->pma
), pma2
);
6111 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma2
);
6116 /* Given two isl_union_pw_multi_affs A -> B and C -> D,
6117 * construct an isl_union_pw_multi_aff (A * C) -> (B, D).
6119 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_flat_range_product(
6120 __isl_take isl_union_pw_multi_aff
*upma1
,
6121 __isl_take isl_union_pw_multi_aff
*upma2
)
6123 return bin_op(upma1
, upma2
, &flat_range_product_entry
);
6126 /* Replace the affine expressions at position "pos" in "pma" by "pa".
6127 * The parameters are assumed to have been aligned.
6129 * The implementation essentially performs an isl_pw_*_on_shared_domain,
6130 * except that it works on two different isl_pw_* types.
6132 static __isl_give isl_pw_multi_aff
*pw_multi_aff_set_pw_aff(
6133 __isl_take isl_pw_multi_aff
*pma
, unsigned pos
,
6134 __isl_take isl_pw_aff
*pa
)
6137 isl_pw_multi_aff
*res
= NULL
;
6142 if (!isl_space_tuple_is_equal(pma
->dim
, isl_dim_in
,
6143 pa
->dim
, isl_dim_in
))
6144 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
6145 "domains don't match", goto error
);
6146 if (isl_pw_multi_aff_check_range(pma
, isl_dim_out
, pos
, 1) < 0)
6150 res
= isl_pw_multi_aff_alloc_size(isl_pw_multi_aff_get_space(pma
), n
);
6152 for (i
= 0; i
< pma
->n
; ++i
) {
6153 for (j
= 0; j
< pa
->n
; ++j
) {
6155 isl_multi_aff
*res_ij
;
6158 common
= isl_set_intersect(isl_set_copy(pma
->p
[i
].set
),
6159 isl_set_copy(pa
->p
[j
].set
));
6160 empty
= isl_set_plain_is_empty(common
);
6161 if (empty
< 0 || empty
) {
6162 isl_set_free(common
);
6168 res_ij
= isl_multi_aff_set_aff(
6169 isl_multi_aff_copy(pma
->p
[i
].maff
), pos
,
6170 isl_aff_copy(pa
->p
[j
].aff
));
6171 res_ij
= isl_multi_aff_gist(res_ij
,
6172 isl_set_copy(common
));
6174 res
= isl_pw_multi_aff_add_piece(res
, common
, res_ij
);
6178 isl_pw_multi_aff_free(pma
);
6179 isl_pw_aff_free(pa
);
6182 isl_pw_multi_aff_free(pma
);
6183 isl_pw_aff_free(pa
);
6184 return isl_pw_multi_aff_free(res
);
6187 /* Replace the affine expressions at position "pos" in "pma" by "pa".
6189 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_set_pw_aff(
6190 __isl_take isl_pw_multi_aff
*pma
, unsigned pos
,
6191 __isl_take isl_pw_aff
*pa
)
6193 isl_bool equal_params
;
6197 equal_params
= isl_space_has_equal_params(pma
->dim
, pa
->dim
);
6198 if (equal_params
< 0)
6201 return pw_multi_aff_set_pw_aff(pma
, pos
, pa
);
6202 if (isl_pw_multi_aff_check_named_params(pma
) < 0 ||
6203 isl_pw_aff_check_named_params(pa
) < 0)
6205 pma
= isl_pw_multi_aff_align_params(pma
, isl_pw_aff_get_space(pa
));
6206 pa
= isl_pw_aff_align_params(pa
, isl_pw_multi_aff_get_space(pma
));
6207 return pw_multi_aff_set_pw_aff(pma
, pos
, pa
);
6209 isl_pw_multi_aff_free(pma
);
6210 isl_pw_aff_free(pa
);
6214 /* Do the parameters of "pa" match those of "space"?
6216 isl_bool
isl_pw_aff_matching_params(__isl_keep isl_pw_aff
*pa
,
6217 __isl_keep isl_space
*space
)
6219 isl_space
*pa_space
;
6223 return isl_bool_error
;
6225 pa_space
= isl_pw_aff_get_space(pa
);
6227 match
= isl_space_has_equal_params(space
, pa_space
);
6229 isl_space_free(pa_space
);
6233 /* Check that the domain space of "pa" matches "space".
6235 isl_stat
isl_pw_aff_check_match_domain_space(__isl_keep isl_pw_aff
*pa
,
6236 __isl_keep isl_space
*space
)
6238 isl_space
*pa_space
;
6242 return isl_stat_error
;
6244 pa_space
= isl_pw_aff_get_space(pa
);
6246 match
= isl_space_has_equal_params(space
, pa_space
);
6250 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
6251 "parameters don't match", goto error
);
6252 match
= isl_space_tuple_is_equal(space
, isl_dim_in
,
6253 pa_space
, isl_dim_in
);
6257 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
6258 "domains don't match", goto error
);
6259 isl_space_free(pa_space
);
6262 isl_space_free(pa_space
);
6263 return isl_stat_error
;
6271 #include <isl_multi_explicit_domain.c>
6272 #include <isl_multi_pw_aff_explicit_domain.c>
6273 #include <isl_multi_templ.c>
6274 #include <isl_multi_apply_set.c>
6275 #include <isl_multi_arith_templ.c>
6276 #include <isl_multi_coalesce.c>
6277 #include <isl_multi_domain_templ.c>
6278 #include <isl_multi_dim_id_templ.c>
6279 #include <isl_multi_dims.c>
6280 #include <isl_multi_from_base_templ.c>
6281 #include <isl_multi_gist.c>
6282 #include <isl_multi_hash.c>
6283 #include <isl_multi_identity_templ.c>
6284 #include <isl_multi_align_set.c>
6285 #include <isl_multi_intersect.c>
6286 #include <isl_multi_move_dims_templ.c>
6287 #include <isl_multi_nan_templ.c>
6288 #include <isl_multi_param_templ.c>
6289 #include <isl_multi_product_templ.c>
6290 #include <isl_multi_splice_templ.c>
6291 #include <isl_multi_tuple_id_templ.c>
6292 #include <isl_multi_zero_templ.c>
6294 /* Does "mpa" have a non-trivial explicit domain?
6296 * The explicit domain, if present, is trivial if it represents
6297 * an (obviously) universe set.
6299 isl_bool
isl_multi_pw_aff_has_non_trivial_domain(
6300 __isl_keep isl_multi_pw_aff
*mpa
)
6303 return isl_bool_error
;
6304 if (!isl_multi_pw_aff_has_explicit_domain(mpa
))
6305 return isl_bool_false
;
6306 return isl_bool_not(isl_set_plain_is_universe(mpa
->u
.dom
));
6309 /* Scale the elements of "pma" by the corresponding elements of "mv".
6311 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_scale_multi_val(
6312 __isl_take isl_pw_multi_aff
*pma
, __isl_take isl_multi_val
*mv
)
6315 isl_bool equal_params
;
6317 pma
= isl_pw_multi_aff_cow(pma
);
6320 if (!isl_space_tuple_is_equal(pma
->dim
, isl_dim_out
,
6321 mv
->space
, isl_dim_set
))
6322 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
6323 "spaces don't match", goto error
);
6324 equal_params
= isl_space_has_equal_params(pma
->dim
, mv
->space
);
6325 if (equal_params
< 0)
6327 if (!equal_params
) {
6328 pma
= isl_pw_multi_aff_align_params(pma
,
6329 isl_multi_val_get_space(mv
));
6330 mv
= isl_multi_val_align_params(mv
,
6331 isl_pw_multi_aff_get_space(pma
));
6336 for (i
= 0; i
< pma
->n
; ++i
) {
6337 pma
->p
[i
].maff
= isl_multi_aff_scale_multi_val(pma
->p
[i
].maff
,
6338 isl_multi_val_copy(mv
));
6339 if (!pma
->p
[i
].maff
)
6343 isl_multi_val_free(mv
);
6346 isl_multi_val_free(mv
);
6347 isl_pw_multi_aff_free(pma
);
6351 /* This function is called for each entry of an isl_union_pw_multi_aff.
6352 * If the space of the entry matches that of data->mv,
6353 * then apply isl_pw_multi_aff_scale_multi_val and return the result.
6354 * Otherwise, return an empty isl_pw_multi_aff.
6356 static __isl_give isl_pw_multi_aff
*union_pw_multi_aff_scale_multi_val_entry(
6357 __isl_take isl_pw_multi_aff
*pma
, void *user
)
6359 isl_multi_val
*mv
= user
;
6363 if (!isl_space_tuple_is_equal(pma
->dim
, isl_dim_out
,
6364 mv
->space
, isl_dim_set
)) {
6365 isl_space
*space
= isl_pw_multi_aff_get_space(pma
);
6366 isl_pw_multi_aff_free(pma
);
6367 return isl_pw_multi_aff_empty(space
);
6370 return isl_pw_multi_aff_scale_multi_val(pma
, isl_multi_val_copy(mv
));
6373 /* Scale the elements of "upma" by the corresponding elements of "mv",
6374 * for those entries that match the space of "mv".
6376 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_scale_multi_val(
6377 __isl_take isl_union_pw_multi_aff
*upma
, __isl_take isl_multi_val
*mv
)
6379 upma
= isl_union_pw_multi_aff_align_params(upma
,
6380 isl_multi_val_get_space(mv
));
6381 mv
= isl_multi_val_align_params(mv
,
6382 isl_union_pw_multi_aff_get_space(upma
));
6386 return isl_union_pw_multi_aff_transform(upma
,
6387 &union_pw_multi_aff_scale_multi_val_entry
, mv
);
6389 isl_multi_val_free(mv
);
6392 isl_multi_val_free(mv
);
6393 isl_union_pw_multi_aff_free(upma
);
6397 /* Construct and return a piecewise multi affine expression
6398 * in the given space with value zero in each of the output dimensions and
6399 * a universe domain.
6401 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_zero(__isl_take isl_space
*space
)
6403 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_zero(space
));
6406 /* Construct and return a piecewise multi affine expression
6407 * that is equal to the given piecewise affine expression.
6409 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_pw_aff(
6410 __isl_take isl_pw_aff
*pa
)
6414 isl_pw_multi_aff
*pma
;
6419 space
= isl_pw_aff_get_space(pa
);
6420 pma
= isl_pw_multi_aff_alloc_size(space
, pa
->n
);
6422 for (i
= 0; i
< pa
->n
; ++i
) {
6426 set
= isl_set_copy(pa
->p
[i
].set
);
6427 ma
= isl_multi_aff_from_aff(isl_aff_copy(pa
->p
[i
].aff
));
6428 pma
= isl_pw_multi_aff_add_piece(pma
, set
, ma
);
6431 isl_pw_aff_free(pa
);
6435 /* Construct and return a piecewise multi affine expression
6436 * that is equal to the given multi piecewise affine expression
6437 * on the shared domain of the piecewise affine expressions,
6438 * in the special case of a 0D multi piecewise affine expression.
6440 * Create a piecewise multi affine expression with the explicit domain of
6441 * the 0D multi piecewise affine expression as domain.
6443 static __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_multi_pw_aff_0D(
6444 __isl_take isl_multi_pw_aff
*mpa
)
6450 space
= isl_multi_pw_aff_get_space(mpa
);
6451 dom
= isl_multi_pw_aff_get_explicit_domain(mpa
);
6452 isl_multi_pw_aff_free(mpa
);
6454 ma
= isl_multi_aff_zero(space
);
6455 return isl_pw_multi_aff_alloc(dom
, ma
);
6458 /* Construct and return a piecewise multi affine expression
6459 * that is equal to the given multi piecewise affine expression
6460 * on the shared domain of the piecewise affine expressions.
6462 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_multi_pw_aff(
6463 __isl_take isl_multi_pw_aff
*mpa
)
6468 isl_pw_multi_aff
*pma
;
6474 return isl_pw_multi_aff_from_multi_pw_aff_0D(mpa
);
6476 space
= isl_multi_pw_aff_get_space(mpa
);
6477 pa
= isl_multi_pw_aff_get_pw_aff(mpa
, 0);
6478 pma
= isl_pw_multi_aff_from_pw_aff(pa
);
6480 for (i
= 1; i
< mpa
->n
; ++i
) {
6481 isl_pw_multi_aff
*pma_i
;
6483 pa
= isl_multi_pw_aff_get_pw_aff(mpa
, i
);
6484 pma_i
= isl_pw_multi_aff_from_pw_aff(pa
);
6485 pma
= isl_pw_multi_aff_range_product(pma
, pma_i
);
6488 pma
= isl_pw_multi_aff_reset_space(pma
, space
);
6490 isl_multi_pw_aff_free(mpa
);
6494 /* Construct and return a multi piecewise affine expression
6495 * that is equal to the given multi affine expression.
6497 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_from_multi_aff(
6498 __isl_take isl_multi_aff
*ma
)
6502 isl_multi_pw_aff
*mpa
;
6504 n
= isl_multi_aff_dim(ma
, isl_dim_out
);
6506 ma
= isl_multi_aff_free(ma
);
6510 mpa
= isl_multi_pw_aff_alloc(isl_multi_aff_get_space(ma
));
6512 for (i
= 0; i
< n
; ++i
) {
6515 pa
= isl_pw_aff_from_aff(isl_multi_aff_get_aff(ma
, i
));
6516 mpa
= isl_multi_pw_aff_set_pw_aff(mpa
, i
, pa
);
6519 isl_multi_aff_free(ma
);
6523 /* Construct and return a multi piecewise affine expression
6524 * that is equal to the given piecewise multi affine expression.
6526 * If the resulting multi piecewise affine expression has
6527 * an explicit domain, then assign it the domain of the input.
6528 * In other cases, the domain is stored in the individual elements.
6530 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_from_pw_multi_aff(
6531 __isl_take isl_pw_multi_aff
*pma
)
6536 isl_multi_pw_aff
*mpa
;
6538 n
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
6540 pma
= isl_pw_multi_aff_free(pma
);
6541 space
= isl_pw_multi_aff_get_space(pma
);
6542 mpa
= isl_multi_pw_aff_alloc(space
);
6544 for (i
= 0; i
< n
; ++i
) {
6547 pa
= isl_pw_multi_aff_get_pw_aff(pma
, i
);
6548 mpa
= isl_multi_pw_aff_set_pw_aff(mpa
, i
, pa
);
6550 if (isl_multi_pw_aff_has_explicit_domain(mpa
)) {
6553 dom
= isl_pw_multi_aff_domain(isl_pw_multi_aff_copy(pma
));
6554 mpa
= isl_multi_pw_aff_intersect_domain(mpa
, dom
);
6557 isl_pw_multi_aff_free(pma
);
6561 /* Do "pa1" and "pa2" represent the same function?
6563 * We first check if they are obviously equal.
6564 * If not, we convert them to maps and check if those are equal.
6566 * If "pa1" or "pa2" contain any NaNs, then they are considered
6567 * not to be the same. A NaN is not equal to anything, not even
6570 isl_bool
isl_pw_aff_is_equal(__isl_keep isl_pw_aff
*pa1
,
6571 __isl_keep isl_pw_aff
*pa2
)
6575 isl_map
*map1
, *map2
;
6578 return isl_bool_error
;
6580 equal
= isl_pw_aff_plain_is_equal(pa1
, pa2
);
6581 if (equal
< 0 || equal
)
6583 has_nan
= either_involves_nan(pa1
, pa2
);
6585 return isl_bool_error
;
6587 return isl_bool_false
;
6589 map1
= isl_map_from_pw_aff_internal(isl_pw_aff_copy(pa1
));
6590 map2
= isl_map_from_pw_aff_internal(isl_pw_aff_copy(pa2
));
6591 equal
= isl_map_is_equal(map1
, map2
);
6598 /* Do "mpa1" and "mpa2" represent the same function?
6600 * Note that we cannot convert the entire isl_multi_pw_aff
6601 * to a map because the domains of the piecewise affine expressions
6602 * may not be the same.
6604 isl_bool
isl_multi_pw_aff_is_equal(__isl_keep isl_multi_pw_aff
*mpa1
,
6605 __isl_keep isl_multi_pw_aff
*mpa2
)
6608 isl_bool equal
, equal_params
;
6611 return isl_bool_error
;
6613 equal_params
= isl_space_has_equal_params(mpa1
->space
, mpa2
->space
);
6614 if (equal_params
< 0)
6615 return isl_bool_error
;
6616 if (!equal_params
) {
6617 if (!isl_space_has_named_params(mpa1
->space
))
6618 return isl_bool_false
;
6619 if (!isl_space_has_named_params(mpa2
->space
))
6620 return isl_bool_false
;
6621 mpa1
= isl_multi_pw_aff_copy(mpa1
);
6622 mpa2
= isl_multi_pw_aff_copy(mpa2
);
6623 mpa1
= isl_multi_pw_aff_align_params(mpa1
,
6624 isl_multi_pw_aff_get_space(mpa2
));
6625 mpa2
= isl_multi_pw_aff_align_params(mpa2
,
6626 isl_multi_pw_aff_get_space(mpa1
));
6627 equal
= isl_multi_pw_aff_is_equal(mpa1
, mpa2
);
6628 isl_multi_pw_aff_free(mpa1
);
6629 isl_multi_pw_aff_free(mpa2
);
6633 equal
= isl_space_is_equal(mpa1
->space
, mpa2
->space
);
6634 if (equal
< 0 || !equal
)
6637 for (i
= 0; i
< mpa1
->n
; ++i
) {
6638 equal
= isl_pw_aff_is_equal(mpa1
->u
.p
[i
], mpa2
->u
.p
[i
]);
6639 if (equal
< 0 || !equal
)
6643 return isl_bool_true
;
6646 /* Do "pma1" and "pma2" represent the same function?
6648 * First check if they are obviously equal.
6649 * If not, then convert them to maps and check if those are equal.
6651 * If "pa1" or "pa2" contain any NaNs, then they are considered
6652 * not to be the same. A NaN is not equal to anything, not even
6655 isl_bool
isl_pw_multi_aff_is_equal(__isl_keep isl_pw_multi_aff
*pma1
,
6656 __isl_keep isl_pw_multi_aff
*pma2
)
6660 isl_map
*map1
, *map2
;
6663 return isl_bool_error
;
6665 equal
= isl_pw_multi_aff_plain_is_equal(pma1
, pma2
);
6666 if (equal
< 0 || equal
)
6668 has_nan
= isl_pw_multi_aff_involves_nan(pma1
);
6669 if (has_nan
>= 0 && !has_nan
)
6670 has_nan
= isl_pw_multi_aff_involves_nan(pma2
);
6671 if (has_nan
< 0 || has_nan
)
6672 return isl_bool_not(has_nan
);
6674 map1
= isl_map_from_pw_multi_aff(isl_pw_multi_aff_copy(pma1
));
6675 map2
= isl_map_from_pw_multi_aff(isl_pw_multi_aff_copy(pma2
));
6676 equal
= isl_map_is_equal(map1
, map2
);
6683 /* Compute the pullback of "mpa" by the function represented by "ma".
6684 * In other words, plug in "ma" in "mpa".
6686 * The parameters of "mpa" and "ma" are assumed to have been aligned.
6688 * If "mpa" has an explicit domain, then it is this domain
6689 * that needs to undergo a pullback, i.e., a preimage.
6691 static __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_pullback_multi_aff_aligned(
6692 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_multi_aff
*ma
)
6695 isl_space
*space
= NULL
;
6697 mpa
= isl_multi_pw_aff_cow(mpa
);
6701 space
= isl_space_join(isl_multi_aff_get_space(ma
),
6702 isl_multi_pw_aff_get_space(mpa
));
6706 for (i
= 0; i
< mpa
->n
; ++i
) {
6707 mpa
->u
.p
[i
] = isl_pw_aff_pullback_multi_aff(mpa
->u
.p
[i
],
6708 isl_multi_aff_copy(ma
));
6712 if (isl_multi_pw_aff_has_explicit_domain(mpa
)) {
6713 mpa
->u
.dom
= isl_set_preimage_multi_aff(mpa
->u
.dom
,
6714 isl_multi_aff_copy(ma
));
6719 isl_multi_aff_free(ma
);
6720 isl_space_free(mpa
->space
);
6724 isl_space_free(space
);
6725 isl_multi_pw_aff_free(mpa
);
6726 isl_multi_aff_free(ma
);
6730 /* Compute the pullback of "mpa" by the function represented by "ma".
6731 * In other words, plug in "ma" in "mpa".
6733 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_pullback_multi_aff(
6734 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_multi_aff
*ma
)
6736 isl_bool equal_params
;
6740 equal_params
= isl_space_has_equal_params(mpa
->space
, ma
->space
);
6741 if (equal_params
< 0)
6744 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa
, ma
);
6745 mpa
= isl_multi_pw_aff_align_params(mpa
, isl_multi_aff_get_space(ma
));
6746 ma
= isl_multi_aff_align_params(ma
, isl_multi_pw_aff_get_space(mpa
));
6747 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa
, ma
);
6749 isl_multi_pw_aff_free(mpa
);
6750 isl_multi_aff_free(ma
);
6754 /* Compute the pullback of "mpa" by the function represented by "pma".
6755 * In other words, plug in "pma" in "mpa".
6757 * The parameters of "mpa" and "mpa" are assumed to have been aligned.
6759 * If "mpa" has an explicit domain, then it is this domain
6760 * that needs to undergo a pullback, i.e., a preimage.
6762 static __isl_give isl_multi_pw_aff
*
6763 isl_multi_pw_aff_pullback_pw_multi_aff_aligned(
6764 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_pw_multi_aff
*pma
)
6767 isl_space
*space
= NULL
;
6769 mpa
= isl_multi_pw_aff_cow(mpa
);
6773 space
= isl_space_join(isl_pw_multi_aff_get_space(pma
),
6774 isl_multi_pw_aff_get_space(mpa
));
6776 for (i
= 0; i
< mpa
->n
; ++i
) {
6777 mpa
->u
.p
[i
] = isl_pw_aff_pullback_pw_multi_aff_aligned(
6778 mpa
->u
.p
[i
], isl_pw_multi_aff_copy(pma
));
6782 if (isl_multi_pw_aff_has_explicit_domain(mpa
)) {
6783 mpa
->u
.dom
= isl_set_preimage_pw_multi_aff(mpa
->u
.dom
,
6784 isl_pw_multi_aff_copy(pma
));
6789 isl_pw_multi_aff_free(pma
);
6790 isl_space_free(mpa
->space
);
6794 isl_space_free(space
);
6795 isl_multi_pw_aff_free(mpa
);
6796 isl_pw_multi_aff_free(pma
);
6800 /* Compute the pullback of "mpa" by the function represented by "pma".
6801 * In other words, plug in "pma" in "mpa".
6803 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_pullback_pw_multi_aff(
6804 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_pw_multi_aff
*pma
)
6806 isl_bool equal_params
;
6810 equal_params
= isl_space_has_equal_params(mpa
->space
, pma
->dim
);
6811 if (equal_params
< 0)
6814 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa
, pma
);
6815 mpa
= isl_multi_pw_aff_align_params(mpa
,
6816 isl_pw_multi_aff_get_space(pma
));
6817 pma
= isl_pw_multi_aff_align_params(pma
,
6818 isl_multi_pw_aff_get_space(mpa
));
6819 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa
, pma
);
6821 isl_multi_pw_aff_free(mpa
);
6822 isl_pw_multi_aff_free(pma
);
6826 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
6827 * with the domain of "aff". The domain of the result is the same
6829 * "mpa" and "aff" are assumed to have been aligned.
6831 * We first extract the parametric constant from "aff", defined
6832 * over the correct domain.
6833 * Then we add the appropriate combinations of the members of "mpa".
6834 * Finally, we add the integer divisions through recursive calls.
6836 static __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_aff_aligned(
6837 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_aff
*aff
)
6840 isl_size n_in
, n_div
, n_mpa_in
;
6846 n_in
= isl_aff_dim(aff
, isl_dim_in
);
6847 n_div
= isl_aff_dim(aff
, isl_dim_div
);
6848 n_mpa_in
= isl_multi_pw_aff_dim(mpa
, isl_dim_in
);
6849 if (n_in
< 0 || n_div
< 0 || n_mpa_in
< 0)
6852 space
= isl_space_domain(isl_multi_pw_aff_get_space(mpa
));
6853 tmp
= isl_aff_copy(aff
);
6854 tmp
= isl_aff_drop_dims(tmp
, isl_dim_div
, 0, n_div
);
6855 tmp
= isl_aff_drop_dims(tmp
, isl_dim_in
, 0, n_in
);
6856 tmp
= isl_aff_add_dims(tmp
, isl_dim_in
, n_mpa_in
);
6857 tmp
= isl_aff_reset_domain_space(tmp
, space
);
6858 pa
= isl_pw_aff_from_aff(tmp
);
6860 for (i
= 0; i
< n_in
; ++i
) {
6863 if (!isl_aff_involves_dims(aff
, isl_dim_in
, i
, 1))
6865 v
= isl_aff_get_coefficient_val(aff
, isl_dim_in
, i
);
6866 pa_i
= isl_multi_pw_aff_get_pw_aff(mpa
, i
);
6867 pa_i
= isl_pw_aff_scale_val(pa_i
, v
);
6868 pa
= isl_pw_aff_add(pa
, pa_i
);
6871 for (i
= 0; i
< n_div
; ++i
) {
6875 if (!isl_aff_involves_dims(aff
, isl_dim_div
, i
, 1))
6877 div
= isl_aff_get_div(aff
, i
);
6878 pa_i
= isl_multi_pw_aff_apply_aff_aligned(
6879 isl_multi_pw_aff_copy(mpa
), div
);
6880 pa_i
= isl_pw_aff_floor(pa_i
);
6881 v
= isl_aff_get_coefficient_val(aff
, isl_dim_div
, i
);
6882 pa_i
= isl_pw_aff_scale_val(pa_i
, v
);
6883 pa
= isl_pw_aff_add(pa
, pa_i
);
6886 isl_multi_pw_aff_free(mpa
);
6891 isl_multi_pw_aff_free(mpa
);
6896 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
6897 * with the domain of "aff". The domain of the result is the same
6900 __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_aff(
6901 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_aff
*aff
)
6903 isl_bool equal_params
;
6907 equal_params
= isl_space_has_equal_params(aff
->ls
->dim
, mpa
->space
);
6908 if (equal_params
< 0)
6911 return isl_multi_pw_aff_apply_aff_aligned(mpa
, aff
);
6913 aff
= isl_aff_align_params(aff
, isl_multi_pw_aff_get_space(mpa
));
6914 mpa
= isl_multi_pw_aff_align_params(mpa
, isl_aff_get_space(aff
));
6916 return isl_multi_pw_aff_apply_aff_aligned(mpa
, aff
);
6919 isl_multi_pw_aff_free(mpa
);
6923 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
6924 * with the domain of "pa". The domain of the result is the same
6926 * "mpa" and "pa" are assumed to have been aligned.
6928 * We consider each piece in turn. Note that the domains of the
6929 * pieces are assumed to be disjoint and they remain disjoint
6930 * after taking the preimage (over the same function).
6932 static __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_pw_aff_aligned(
6933 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_pw_aff
*pa
)
6942 space
= isl_space_join(isl_multi_pw_aff_get_space(mpa
),
6943 isl_pw_aff_get_space(pa
));
6944 res
= isl_pw_aff_empty(space
);
6946 for (i
= 0; i
< pa
->n
; ++i
) {
6950 pa_i
= isl_multi_pw_aff_apply_aff_aligned(
6951 isl_multi_pw_aff_copy(mpa
),
6952 isl_aff_copy(pa
->p
[i
].aff
));
6953 domain
= isl_set_copy(pa
->p
[i
].set
);
6954 domain
= isl_set_preimage_multi_pw_aff(domain
,
6955 isl_multi_pw_aff_copy(mpa
));
6956 pa_i
= isl_pw_aff_intersect_domain(pa_i
, domain
);
6957 res
= isl_pw_aff_add_disjoint(res
, pa_i
);
6960 isl_pw_aff_free(pa
);
6961 isl_multi_pw_aff_free(mpa
);
6964 isl_pw_aff_free(pa
);
6965 isl_multi_pw_aff_free(mpa
);
6969 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
6970 * with the domain of "pa". The domain of the result is the same
6973 __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_pw_aff(
6974 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_pw_aff
*pa
)
6976 isl_bool equal_params
;
6980 equal_params
= isl_space_has_equal_params(pa
->dim
, mpa
->space
);
6981 if (equal_params
< 0)
6984 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa
, pa
);
6986 pa
= isl_pw_aff_align_params(pa
, isl_multi_pw_aff_get_space(mpa
));
6987 mpa
= isl_multi_pw_aff_align_params(mpa
, isl_pw_aff_get_space(pa
));
6989 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa
, pa
);
6991 isl_pw_aff_free(pa
);
6992 isl_multi_pw_aff_free(mpa
);
6996 /* Compute the pullback of "pa" by the function represented by "mpa".
6997 * In other words, plug in "mpa" in "pa".
6998 * "pa" and "mpa" are assumed to have been aligned.
7000 * The pullback is computed by applying "pa" to "mpa".
7002 static __isl_give isl_pw_aff
*isl_pw_aff_pullback_multi_pw_aff_aligned(
7003 __isl_take isl_pw_aff
*pa
, __isl_take isl_multi_pw_aff
*mpa
)
7005 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa
, pa
);
7008 /* Compute the pullback of "pa" by the function represented by "mpa".
7009 * In other words, plug in "mpa" in "pa".
7011 * The pullback is computed by applying "pa" to "mpa".
7013 __isl_give isl_pw_aff
*isl_pw_aff_pullback_multi_pw_aff(
7014 __isl_take isl_pw_aff
*pa
, __isl_take isl_multi_pw_aff
*mpa
)
7016 return isl_multi_pw_aff_apply_pw_aff(mpa
, pa
);
7019 /* Compute the pullback of "mpa1" by the function represented by "mpa2".
7020 * In other words, plug in "mpa2" in "mpa1".
7022 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7024 * We pullback each member of "mpa1" in turn.
7026 * If "mpa1" has an explicit domain, then it is this domain
7027 * that needs to undergo a pullback instead, i.e., a preimage.
7029 static __isl_give isl_multi_pw_aff
*
7030 isl_multi_pw_aff_pullback_multi_pw_aff_aligned(
7031 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
)
7034 isl_space
*space
= NULL
;
7036 mpa1
= isl_multi_pw_aff_cow(mpa1
);
7040 space
= isl_space_join(isl_multi_pw_aff_get_space(mpa2
),
7041 isl_multi_pw_aff_get_space(mpa1
));
7043 for (i
= 0; i
< mpa1
->n
; ++i
) {
7044 mpa1
->u
.p
[i
] = isl_pw_aff_pullback_multi_pw_aff_aligned(
7045 mpa1
->u
.p
[i
], isl_multi_pw_aff_copy(mpa2
));
7050 if (isl_multi_pw_aff_has_explicit_domain(mpa1
)) {
7051 mpa1
->u
.dom
= isl_set_preimage_multi_pw_aff(mpa1
->u
.dom
,
7052 isl_multi_pw_aff_copy(mpa2
));
7056 mpa1
= isl_multi_pw_aff_reset_space(mpa1
, space
);
7058 isl_multi_pw_aff_free(mpa2
);
7061 isl_space_free(space
);
7062 isl_multi_pw_aff_free(mpa1
);
7063 isl_multi_pw_aff_free(mpa2
);
7067 /* Compute the pullback of "mpa1" by the function represented by "mpa2".
7068 * In other words, plug in "mpa2" in "mpa1".
7070 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_pullback_multi_pw_aff(
7071 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
)
7073 return isl_multi_pw_aff_align_params_multi_multi_and(mpa1
, mpa2
,
7074 &isl_multi_pw_aff_pullback_multi_pw_aff_aligned
);
7077 /* Align the parameters of "mpa1" and "mpa2", check that the ranges
7078 * of "mpa1" and "mpa2" live in the same space, construct map space
7079 * between the domain spaces of "mpa1" and "mpa2" and call "order"
7080 * with this map space as extract argument.
7082 static __isl_give isl_map
*isl_multi_pw_aff_order_map(
7083 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
,
7084 __isl_give isl_map
*(*order
)(__isl_keep isl_multi_pw_aff
*mpa1
,
7085 __isl_keep isl_multi_pw_aff
*mpa2
, __isl_take isl_space
*space
))
7088 isl_space
*space1
, *space2
;
7091 mpa1
= isl_multi_pw_aff_align_params(mpa1
,
7092 isl_multi_pw_aff_get_space(mpa2
));
7093 mpa2
= isl_multi_pw_aff_align_params(mpa2
,
7094 isl_multi_pw_aff_get_space(mpa1
));
7097 match
= isl_space_tuple_is_equal(mpa1
->space
, isl_dim_out
,
7098 mpa2
->space
, isl_dim_out
);
7102 isl_die(isl_multi_pw_aff_get_ctx(mpa1
), isl_error_invalid
,
7103 "range spaces don't match", goto error
);
7104 space1
= isl_space_domain(isl_multi_pw_aff_get_space(mpa1
));
7105 space2
= isl_space_domain(isl_multi_pw_aff_get_space(mpa2
));
7106 space1
= isl_space_map_from_domain_and_range(space1
, space2
);
7108 res
= order(mpa1
, mpa2
, space1
);
7109 isl_multi_pw_aff_free(mpa1
);
7110 isl_multi_pw_aff_free(mpa2
);
7113 isl_multi_pw_aff_free(mpa1
);
7114 isl_multi_pw_aff_free(mpa2
);
7118 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7119 * where the function values are equal. "space" is the space of the result.
7120 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7122 * "mpa1" and "mpa2" are equal when each of the pairs of elements
7123 * in the sequences are equal.
7125 static __isl_give isl_map
*isl_multi_pw_aff_eq_map_on_space(
7126 __isl_keep isl_multi_pw_aff
*mpa1
, __isl_keep isl_multi_pw_aff
*mpa2
,
7127 __isl_take isl_space
*space
)
7133 n
= isl_multi_pw_aff_dim(mpa1
, isl_dim_out
);
7135 space
= isl_space_free(space
);
7136 res
= isl_map_universe(space
);
7138 for (i
= 0; i
< n
; ++i
) {
7139 isl_pw_aff
*pa1
, *pa2
;
7142 pa1
= isl_multi_pw_aff_get_pw_aff(mpa1
, i
);
7143 pa2
= isl_multi_pw_aff_get_pw_aff(mpa2
, i
);
7144 map
= isl_pw_aff_eq_map(pa1
, pa2
);
7145 res
= isl_map_intersect(res
, map
);
7151 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7152 * where the function values are equal.
7154 __isl_give isl_map
*isl_multi_pw_aff_eq_map(__isl_take isl_multi_pw_aff
*mpa1
,
7155 __isl_take isl_multi_pw_aff
*mpa2
)
7157 return isl_multi_pw_aff_order_map(mpa1
, mpa2
,
7158 &isl_multi_pw_aff_eq_map_on_space
);
7161 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7162 * where the function values of "mpa1" is lexicographically satisfies "base"
7163 * compared to that of "mpa2". "space" is the space of the result.
7164 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7166 * "mpa1" lexicographically satisfies "base" compared to "mpa2"
7167 * if its i-th element satisfies "base" when compared to
7168 * the i-th element of "mpa2" while all previous elements are
7171 static __isl_give isl_map
*isl_multi_pw_aff_lex_map_on_space(
7172 __isl_keep isl_multi_pw_aff
*mpa1
, __isl_keep isl_multi_pw_aff
*mpa2
,
7173 __isl_give isl_map
*(*base
)(__isl_take isl_pw_aff
*pa1
,
7174 __isl_take isl_pw_aff
*pa2
),
7175 __isl_take isl_space
*space
)
7179 isl_map
*res
, *rest
;
7181 n
= isl_multi_pw_aff_dim(mpa1
, isl_dim_out
);
7183 space
= isl_space_free(space
);
7184 res
= isl_map_empty(isl_space_copy(space
));
7185 rest
= isl_map_universe(space
);
7187 for (i
= 0; i
< n
; ++i
) {
7188 isl_pw_aff
*pa1
, *pa2
;
7191 pa1
= isl_multi_pw_aff_get_pw_aff(mpa1
, i
);
7192 pa2
= isl_multi_pw_aff_get_pw_aff(mpa2
, i
);
7193 map
= base(pa1
, pa2
);
7194 map
= isl_map_intersect(map
, isl_map_copy(rest
));
7195 res
= isl_map_union(res
, map
);
7200 pa1
= isl_multi_pw_aff_get_pw_aff(mpa1
, i
);
7201 pa2
= isl_multi_pw_aff_get_pw_aff(mpa2
, i
);
7202 map
= isl_pw_aff_eq_map(pa1
, pa2
);
7203 rest
= isl_map_intersect(rest
, map
);
7210 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7211 * where the function value of "mpa1" is lexicographically less than that
7212 * of "mpa2". "space" is the space of the result.
7213 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7215 * "mpa1" is less than "mpa2" if its i-th element is smaller
7216 * than the i-th element of "mpa2" while all previous elements are
7219 __isl_give isl_map
*isl_multi_pw_aff_lex_lt_map_on_space(
7220 __isl_keep isl_multi_pw_aff
*mpa1
, __isl_keep isl_multi_pw_aff
*mpa2
,
7221 __isl_take isl_space
*space
)
7223 return isl_multi_pw_aff_lex_map_on_space(mpa1
, mpa2
,
7224 &isl_pw_aff_lt_map
, space
);
7227 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7228 * where the function value of "mpa1" is lexicographically less than that
7231 __isl_give isl_map
*isl_multi_pw_aff_lex_lt_map(
7232 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
)
7234 return isl_multi_pw_aff_order_map(mpa1
, mpa2
,
7235 &isl_multi_pw_aff_lex_lt_map_on_space
);
7238 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7239 * where the function value of "mpa1" is lexicographically greater than that
7240 * of "mpa2". "space" is the space of the result.
7241 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7243 * "mpa1" is greater than "mpa2" if its i-th element is greater
7244 * than the i-th element of "mpa2" while all previous elements are
7247 __isl_give isl_map
*isl_multi_pw_aff_lex_gt_map_on_space(
7248 __isl_keep isl_multi_pw_aff
*mpa1
, __isl_keep isl_multi_pw_aff
*mpa2
,
7249 __isl_take isl_space
*space
)
7251 return isl_multi_pw_aff_lex_map_on_space(mpa1
, mpa2
,
7252 &isl_pw_aff_gt_map
, space
);
7255 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7256 * where the function value of "mpa1" is lexicographically greater than that
7259 __isl_give isl_map
*isl_multi_pw_aff_lex_gt_map(
7260 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
)
7262 return isl_multi_pw_aff_order_map(mpa1
, mpa2
,
7263 &isl_multi_pw_aff_lex_gt_map_on_space
);
7266 /* Compare two isl_affs.
7268 * Return -1 if "aff1" is "smaller" than "aff2", 1 if "aff1" is "greater"
7269 * than "aff2" and 0 if they are equal.
7271 * The order is fairly arbitrary. We do consider expressions that only involve
7272 * earlier dimensions as "smaller".
7274 int isl_aff_plain_cmp(__isl_keep isl_aff
*aff1
, __isl_keep isl_aff
*aff2
)
7287 cmp
= isl_local_space_cmp(aff1
->ls
, aff2
->ls
);
7291 last1
= isl_seq_last_non_zero(aff1
->v
->el
+ 1, aff1
->v
->size
- 1);
7292 last2
= isl_seq_last_non_zero(aff2
->v
->el
+ 1, aff1
->v
->size
- 1);
7294 return last1
- last2
;
7296 return isl_seq_cmp(aff1
->v
->el
, aff2
->v
->el
, aff1
->v
->size
);
7299 /* Compare two isl_pw_affs.
7301 * Return -1 if "pa1" is "smaller" than "pa2", 1 if "pa1" is "greater"
7302 * than "pa2" and 0 if they are equal.
7304 * The order is fairly arbitrary. We do consider expressions that only involve
7305 * earlier dimensions as "smaller".
7307 int isl_pw_aff_plain_cmp(__isl_keep isl_pw_aff
*pa1
,
7308 __isl_keep isl_pw_aff
*pa2
)
7321 cmp
= isl_space_cmp(pa1
->dim
, pa2
->dim
);
7325 if (pa1
->n
!= pa2
->n
)
7326 return pa1
->n
- pa2
->n
;
7328 for (i
= 0; i
< pa1
->n
; ++i
) {
7329 cmp
= isl_set_plain_cmp(pa1
->p
[i
].set
, pa2
->p
[i
].set
);
7332 cmp
= isl_aff_plain_cmp(pa1
->p
[i
].aff
, pa2
->p
[i
].aff
);
7340 /* Return a piecewise affine expression that is equal to "v" on "domain".
7342 __isl_give isl_pw_aff
*isl_pw_aff_val_on_domain(__isl_take isl_set
*domain
,
7343 __isl_take isl_val
*v
)
7346 isl_local_space
*ls
;
7349 space
= isl_set_get_space(domain
);
7350 ls
= isl_local_space_from_space(space
);
7351 aff
= isl_aff_val_on_domain(ls
, v
);
7353 return isl_pw_aff_alloc(domain
, aff
);
7356 /* Return a multi affine expression that is equal to "mv" on domain
7359 __isl_give isl_multi_aff
*isl_multi_aff_multi_val_on_space(
7360 __isl_take isl_space
*space
, __isl_take isl_multi_val
*mv
)
7365 isl_local_space
*ls
;
7368 n
= isl_multi_val_dim(mv
, isl_dim_set
);
7369 if (!space
|| n
< 0)
7372 space2
= isl_multi_val_get_space(mv
);
7373 space2
= isl_space_align_params(space2
, isl_space_copy(space
));
7374 space
= isl_space_align_params(space
, isl_space_copy(space2
));
7375 space
= isl_space_map_from_domain_and_range(space
, space2
);
7376 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
7377 ls
= isl_local_space_from_space(isl_space_domain(space
));
7378 for (i
= 0; i
< n
; ++i
) {
7382 v
= isl_multi_val_get_val(mv
, i
);
7383 aff
= isl_aff_val_on_domain(isl_local_space_copy(ls
), v
);
7384 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
7386 isl_local_space_free(ls
);
7388 isl_multi_val_free(mv
);
7391 isl_space_free(space
);
7392 isl_multi_val_free(mv
);
7396 /* Return a piecewise multi-affine expression
7397 * that is equal to "mv" on "domain".
7399 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_multi_val_on_domain(
7400 __isl_take isl_set
*domain
, __isl_take isl_multi_val
*mv
)
7405 space
= isl_set_get_space(domain
);
7406 ma
= isl_multi_aff_multi_val_on_space(space
, mv
);
7408 return isl_pw_multi_aff_alloc(domain
, ma
);
7411 /* Internal data structure for isl_union_pw_multi_aff_multi_val_on_domain.
7412 * mv is the value that should be attained on each domain set
7413 * res collects the results
7415 struct isl_union_pw_multi_aff_multi_val_on_domain_data
{
7417 isl_union_pw_multi_aff
*res
;
7420 /* Create an isl_pw_multi_aff equal to data->mv on "domain"
7421 * and add it to data->res.
7423 static isl_stat
pw_multi_aff_multi_val_on_domain(__isl_take isl_set
*domain
,
7426 struct isl_union_pw_multi_aff_multi_val_on_domain_data
*data
= user
;
7427 isl_pw_multi_aff
*pma
;
7430 mv
= isl_multi_val_copy(data
->mv
);
7431 pma
= isl_pw_multi_aff_multi_val_on_domain(domain
, mv
);
7432 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma
);
7434 return data
->res
? isl_stat_ok
: isl_stat_error
;
7437 /* Return a union piecewise multi-affine expression
7438 * that is equal to "mv" on "domain".
7440 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_multi_val_on_domain(
7441 __isl_take isl_union_set
*domain
, __isl_take isl_multi_val
*mv
)
7443 struct isl_union_pw_multi_aff_multi_val_on_domain_data data
;
7446 space
= isl_union_set_get_space(domain
);
7447 data
.res
= isl_union_pw_multi_aff_empty(space
);
7449 if (isl_union_set_foreach_set(domain
,
7450 &pw_multi_aff_multi_val_on_domain
, &data
) < 0)
7451 data
.res
= isl_union_pw_multi_aff_free(data
.res
);
7452 isl_union_set_free(domain
);
7453 isl_multi_val_free(mv
);
7457 /* Compute the pullback of data->pma by the function represented by "pma2",
7458 * provided the spaces match, and add the results to data->res.
7460 static isl_stat
pullback_entry(__isl_take isl_pw_multi_aff
*pma2
, void *user
)
7462 struct isl_union_pw_multi_aff_bin_data
*data
= user
;
7464 if (!isl_space_tuple_is_equal(data
->pma
->dim
, isl_dim_in
,
7465 pma2
->dim
, isl_dim_out
)) {
7466 isl_pw_multi_aff_free(pma2
);
7470 pma2
= isl_pw_multi_aff_pullback_pw_multi_aff(
7471 isl_pw_multi_aff_copy(data
->pma
), pma2
);
7473 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma2
);
7475 return isl_stat_error
;
7480 /* Compute the pullback of "upma1" by the function represented by "upma2".
7482 __isl_give isl_union_pw_multi_aff
*
7483 isl_union_pw_multi_aff_pullback_union_pw_multi_aff(
7484 __isl_take isl_union_pw_multi_aff
*upma1
,
7485 __isl_take isl_union_pw_multi_aff
*upma2
)
7487 return bin_op(upma1
, upma2
, &pullback_entry
);
7490 /* Check that the domain space of "upa" matches "space".
7492 * This function is called from isl_multi_union_pw_aff_set_union_pw_aff and
7493 * can in principle never fail since the space "space" is that
7494 * of the isl_multi_union_pw_aff and is a set space such that
7495 * there is no domain space to match.
7497 * We check the parameters and double-check that "space" is
7498 * indeed that of a set.
7500 static isl_stat
isl_union_pw_aff_check_match_domain_space(
7501 __isl_keep isl_union_pw_aff
*upa
, __isl_keep isl_space
*space
)
7503 isl_space
*upa_space
;
7507 return isl_stat_error
;
7509 match
= isl_space_is_set(space
);
7511 return isl_stat_error
;
7513 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
7514 "expecting set space", return isl_stat_error
);
7516 upa_space
= isl_union_pw_aff_get_space(upa
);
7517 match
= isl_space_has_equal_params(space
, upa_space
);
7521 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
7522 "parameters don't match", goto error
);
7524 isl_space_free(upa_space
);
7527 isl_space_free(upa_space
);
7528 return isl_stat_error
;
7531 /* Do the parameters of "upa" match those of "space"?
7533 static isl_bool
isl_union_pw_aff_matching_params(
7534 __isl_keep isl_union_pw_aff
*upa
, __isl_keep isl_space
*space
)
7536 isl_space
*upa_space
;
7540 return isl_bool_error
;
7542 upa_space
= isl_union_pw_aff_get_space(upa
);
7544 match
= isl_space_has_equal_params(space
, upa_space
);
7546 isl_space_free(upa_space
);
7550 /* Internal data structure for isl_union_pw_aff_reset_domain_space.
7551 * space represents the new parameters.
7552 * res collects the results.
7554 struct isl_union_pw_aff_reset_params_data
{
7556 isl_union_pw_aff
*res
;
7559 /* Replace the parameters of "pa" by data->space and
7560 * add the result to data->res.
7562 static isl_stat
reset_params(__isl_take isl_pw_aff
*pa
, void *user
)
7564 struct isl_union_pw_aff_reset_params_data
*data
= user
;
7567 space
= isl_pw_aff_get_space(pa
);
7568 space
= isl_space_replace_params(space
, data
->space
);
7569 pa
= isl_pw_aff_reset_space(pa
, space
);
7570 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
7572 return data
->res
? isl_stat_ok
: isl_stat_error
;
7575 /* Replace the domain space of "upa" by "space".
7576 * Since a union expression does not have a (single) domain space,
7577 * "space" is necessarily a parameter space.
7579 * Since the order and the names of the parameters determine
7580 * the hash value, we need to create a new hash table.
7582 static __isl_give isl_union_pw_aff
*isl_union_pw_aff_reset_domain_space(
7583 __isl_take isl_union_pw_aff
*upa
, __isl_take isl_space
*space
)
7585 struct isl_union_pw_aff_reset_params_data data
= { space
};
7588 match
= isl_union_pw_aff_matching_params(upa
, space
);
7590 upa
= isl_union_pw_aff_free(upa
);
7592 isl_space_free(space
);
7596 data
.res
= isl_union_pw_aff_empty(isl_space_copy(space
));
7597 if (isl_union_pw_aff_foreach_pw_aff(upa
, &reset_params
, &data
) < 0)
7598 data
.res
= isl_union_pw_aff_free(data
.res
);
7600 isl_union_pw_aff_free(upa
);
7601 isl_space_free(space
);
7605 /* Return the floor of "pa".
7607 static __isl_give isl_pw_aff
*floor_entry(__isl_take isl_pw_aff
*pa
, void *user
)
7609 return isl_pw_aff_floor(pa
);
7612 /* Given f, return floor(f).
7614 __isl_give isl_union_pw_aff
*isl_union_pw_aff_floor(
7615 __isl_take isl_union_pw_aff
*upa
)
7617 return isl_union_pw_aff_transform_inplace(upa
, &floor_entry
, NULL
);
7622 * upa mod m = upa - m * floor(upa/m)
7624 * with m an integer value.
7626 __isl_give isl_union_pw_aff
*isl_union_pw_aff_mod_val(
7627 __isl_take isl_union_pw_aff
*upa
, __isl_take isl_val
*m
)
7629 isl_union_pw_aff
*res
;
7634 if (!isl_val_is_int(m
))
7635 isl_die(isl_val_get_ctx(m
), isl_error_invalid
,
7636 "expecting integer modulo", goto error
);
7637 if (!isl_val_is_pos(m
))
7638 isl_die(isl_val_get_ctx(m
), isl_error_invalid
,
7639 "expecting positive modulo", goto error
);
7641 res
= isl_union_pw_aff_copy(upa
);
7642 upa
= isl_union_pw_aff_scale_down_val(upa
, isl_val_copy(m
));
7643 upa
= isl_union_pw_aff_floor(upa
);
7644 upa
= isl_union_pw_aff_scale_val(upa
, m
);
7645 res
= isl_union_pw_aff_sub(res
, upa
);
7650 isl_union_pw_aff_free(upa
);
7654 /* Internal data structure for isl_union_pw_multi_aff_get_union_pw_aff.
7655 * pos is the output position that needs to be extracted.
7656 * res collects the results.
7658 struct isl_union_pw_multi_aff_get_union_pw_aff_data
{
7660 isl_union_pw_aff
*res
;
7663 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma"
7664 * (assuming it has such a dimension) and add it to data->res.
7666 static isl_stat
get_union_pw_aff(__isl_take isl_pw_multi_aff
*pma
, void *user
)
7668 struct isl_union_pw_multi_aff_get_union_pw_aff_data
*data
= user
;
7672 n_out
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
7674 return isl_stat_error
;
7675 if (data
->pos
>= n_out
) {
7676 isl_pw_multi_aff_free(pma
);
7680 pa
= isl_pw_multi_aff_get_pw_aff(pma
, data
->pos
);
7681 isl_pw_multi_aff_free(pma
);
7683 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
7685 return data
->res
? isl_stat_ok
: isl_stat_error
;
7688 /* Extract an isl_union_pw_aff corresponding to
7689 * output dimension "pos" of "upma".
7691 __isl_give isl_union_pw_aff
*isl_union_pw_multi_aff_get_union_pw_aff(
7692 __isl_keep isl_union_pw_multi_aff
*upma
, int pos
)
7694 struct isl_union_pw_multi_aff_get_union_pw_aff_data data
;
7701 isl_die(isl_union_pw_multi_aff_get_ctx(upma
), isl_error_invalid
,
7702 "cannot extract at negative position", return NULL
);
7704 space
= isl_union_pw_multi_aff_get_space(upma
);
7705 data
.res
= isl_union_pw_aff_empty(space
);
7707 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma
,
7708 &get_union_pw_aff
, &data
) < 0)
7709 data
.res
= isl_union_pw_aff_free(data
.res
);
7714 /* Return a union piecewise affine expression
7715 * that is equal to "aff" on "domain".
7717 __isl_give isl_union_pw_aff
*isl_union_pw_aff_aff_on_domain(
7718 __isl_take isl_union_set
*domain
, __isl_take isl_aff
*aff
)
7722 pa
= isl_pw_aff_from_aff(aff
);
7723 return isl_union_pw_aff_pw_aff_on_domain(domain
, pa
);
7726 /* Return a union piecewise affine expression
7727 * that is equal to the parameter identified by "id" on "domain".
7729 * Make sure the parameter appears in the space passed to
7730 * isl_aff_param_on_domain_space_id.
7732 __isl_give isl_union_pw_aff
*isl_union_pw_aff_param_on_domain_id(
7733 __isl_take isl_union_set
*domain
, __isl_take isl_id
*id
)
7738 space
= isl_union_set_get_space(domain
);
7739 space
= isl_space_add_param_id(space
, isl_id_copy(id
));
7740 aff
= isl_aff_param_on_domain_space_id(space
, id
);
7741 return isl_union_pw_aff_aff_on_domain(domain
, aff
);
7744 /* Internal data structure for isl_union_pw_aff_pw_aff_on_domain.
7745 * "pa" is the piecewise symbolic value that the resulting isl_union_pw_aff
7747 * "res" collects the results.
7749 struct isl_union_pw_aff_pw_aff_on_domain_data
{
7751 isl_union_pw_aff
*res
;
7754 /* Construct a piecewise affine expression that is equal to data->pa
7755 * on "domain" and add the result to data->res.
7757 static isl_stat
pw_aff_on_domain(__isl_take isl_set
*domain
, void *user
)
7759 struct isl_union_pw_aff_pw_aff_on_domain_data
*data
= user
;
7763 pa
= isl_pw_aff_copy(data
->pa
);
7764 dim
= isl_set_dim(domain
, isl_dim_set
);
7766 pa
= isl_pw_aff_free(pa
);
7767 pa
= isl_pw_aff_from_range(pa
);
7768 pa
= isl_pw_aff_add_dims(pa
, isl_dim_in
, dim
);
7769 pa
= isl_pw_aff_reset_domain_space(pa
, isl_set_get_space(domain
));
7770 pa
= isl_pw_aff_intersect_domain(pa
, domain
);
7771 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
7773 return data
->res
? isl_stat_ok
: isl_stat_error
;
7776 /* Return a union piecewise affine expression
7777 * that is equal to "pa" on "domain", assuming "domain" and "pa"
7778 * have been aligned.
7780 * Construct an isl_pw_aff on each of the sets in "domain" and
7781 * collect the results.
7783 static __isl_give isl_union_pw_aff
*isl_union_pw_aff_pw_aff_on_domain_aligned(
7784 __isl_take isl_union_set
*domain
, __isl_take isl_pw_aff
*pa
)
7786 struct isl_union_pw_aff_pw_aff_on_domain_data data
;
7789 space
= isl_union_set_get_space(domain
);
7790 data
.res
= isl_union_pw_aff_empty(space
);
7792 if (isl_union_set_foreach_set(domain
, &pw_aff_on_domain
, &data
) < 0)
7793 data
.res
= isl_union_pw_aff_free(data
.res
);
7794 isl_union_set_free(domain
);
7795 isl_pw_aff_free(pa
);
7799 /* Return a union piecewise affine expression
7800 * that is equal to "pa" on "domain".
7802 * Check that "pa" is a parametric expression,
7803 * align the parameters if needed and call
7804 * isl_union_pw_aff_pw_aff_on_domain_aligned.
7806 __isl_give isl_union_pw_aff
*isl_union_pw_aff_pw_aff_on_domain(
7807 __isl_take isl_union_set
*domain
, __isl_take isl_pw_aff
*pa
)
7810 isl_bool equal_params
;
7811 isl_space
*domain_space
, *pa_space
;
7813 pa_space
= isl_pw_aff_peek_space(pa
);
7814 is_set
= isl_space_is_set(pa_space
);
7818 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
7819 "expecting parametric expression", goto error
);
7821 domain_space
= isl_union_set_get_space(domain
);
7822 pa_space
= isl_pw_aff_get_space(pa
);
7823 equal_params
= isl_space_has_equal_params(domain_space
, pa_space
);
7824 if (equal_params
>= 0 && !equal_params
) {
7827 space
= isl_space_align_params(domain_space
, pa_space
);
7828 pa
= isl_pw_aff_align_params(pa
, isl_space_copy(space
));
7829 domain
= isl_union_set_align_params(domain
, space
);
7831 isl_space_free(domain_space
);
7832 isl_space_free(pa_space
);
7835 if (equal_params
< 0)
7837 return isl_union_pw_aff_pw_aff_on_domain_aligned(domain
, pa
);
7839 isl_union_set_free(domain
);
7840 isl_pw_aff_free(pa
);
7844 /* Internal data structure for isl_union_pw_aff_val_on_domain.
7845 * "v" is the value that the resulting isl_union_pw_aff needs to attain.
7846 * "res" collects the results.
7848 struct isl_union_pw_aff_val_on_domain_data
{
7850 isl_union_pw_aff
*res
;
7853 /* Construct a piecewise affine expression that is equal to data->v
7854 * on "domain" and add the result to data->res.
7856 static isl_stat
pw_aff_val_on_domain(__isl_take isl_set
*domain
, void *user
)
7858 struct isl_union_pw_aff_val_on_domain_data
*data
= user
;
7862 v
= isl_val_copy(data
->v
);
7863 pa
= isl_pw_aff_val_on_domain(domain
, v
);
7864 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
7866 return data
->res
? isl_stat_ok
: isl_stat_error
;
7869 /* Return a union piecewise affine expression
7870 * that is equal to "v" on "domain".
7872 * Construct an isl_pw_aff on each of the sets in "domain" and
7873 * collect the results.
7875 __isl_give isl_union_pw_aff
*isl_union_pw_aff_val_on_domain(
7876 __isl_take isl_union_set
*domain
, __isl_take isl_val
*v
)
7878 struct isl_union_pw_aff_val_on_domain_data data
;
7881 space
= isl_union_set_get_space(domain
);
7882 data
.res
= isl_union_pw_aff_empty(space
);
7884 if (isl_union_set_foreach_set(domain
, &pw_aff_val_on_domain
, &data
) < 0)
7885 data
.res
= isl_union_pw_aff_free(data
.res
);
7886 isl_union_set_free(domain
);
7891 /* Construct a piecewise multi affine expression
7892 * that is equal to "pa" and add it to upma.
7894 static isl_stat
pw_multi_aff_from_pw_aff_entry(__isl_take isl_pw_aff
*pa
,
7897 isl_union_pw_multi_aff
**upma
= user
;
7898 isl_pw_multi_aff
*pma
;
7900 pma
= isl_pw_multi_aff_from_pw_aff(pa
);
7901 *upma
= isl_union_pw_multi_aff_add_pw_multi_aff(*upma
, pma
);
7903 return *upma
? isl_stat_ok
: isl_stat_error
;
7906 /* Construct and return a union piecewise multi affine expression
7907 * that is equal to the given union piecewise affine expression.
7909 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_union_pw_aff(
7910 __isl_take isl_union_pw_aff
*upa
)
7913 isl_union_pw_multi_aff
*upma
;
7918 space
= isl_union_pw_aff_get_space(upa
);
7919 upma
= isl_union_pw_multi_aff_empty(space
);
7921 if (isl_union_pw_aff_foreach_pw_aff(upa
,
7922 &pw_multi_aff_from_pw_aff_entry
, &upma
) < 0)
7923 upma
= isl_union_pw_multi_aff_free(upma
);
7925 isl_union_pw_aff_free(upa
);
7929 /* Compute the set of elements in the domain of "pa" where it is zero and
7930 * add this set to "uset".
7932 static isl_stat
zero_union_set(__isl_take isl_pw_aff
*pa
, void *user
)
7934 isl_union_set
**uset
= (isl_union_set
**)user
;
7936 *uset
= isl_union_set_add_set(*uset
, isl_pw_aff_zero_set(pa
));
7938 return *uset
? isl_stat_ok
: isl_stat_error
;
7941 /* Return a union set containing those elements in the domain
7942 * of "upa" where it is zero.
7944 __isl_give isl_union_set
*isl_union_pw_aff_zero_union_set(
7945 __isl_take isl_union_pw_aff
*upa
)
7947 isl_union_set
*zero
;
7949 zero
= isl_union_set_empty(isl_union_pw_aff_get_space(upa
));
7950 if (isl_union_pw_aff_foreach_pw_aff(upa
, &zero_union_set
, &zero
) < 0)
7951 zero
= isl_union_set_free(zero
);
7953 isl_union_pw_aff_free(upa
);
7957 /* Internal data structure for isl_union_pw_aff_pullback_union_pw_multi_aff.
7958 * upma is the function that is plugged in.
7959 * pa is the current part of the function in which upma is plugged in.
7960 * res collects the results.
7962 struct isl_union_pw_aff_pullback_upma_data
{
7963 isl_union_pw_multi_aff
*upma
;
7965 isl_union_pw_aff
*res
;
7968 /* Check if "pma" can be plugged into data->pa.
7969 * If so, perform the pullback and add the result to data->res.
7971 static isl_stat
pa_pb_pma(__isl_take isl_pw_multi_aff
*pma
, void *user
)
7973 struct isl_union_pw_aff_pullback_upma_data
*data
= user
;
7976 if (!isl_space_tuple_is_equal(data
->pa
->dim
, isl_dim_in
,
7977 pma
->dim
, isl_dim_out
)) {
7978 isl_pw_multi_aff_free(pma
);
7982 pa
= isl_pw_aff_copy(data
->pa
);
7983 pa
= isl_pw_aff_pullback_pw_multi_aff(pa
, pma
);
7985 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
7987 return data
->res
? isl_stat_ok
: isl_stat_error
;
7990 /* Check if any of the elements of data->upma can be plugged into pa,
7991 * add if so add the result to data->res.
7993 static isl_stat
upa_pb_upma(__isl_take isl_pw_aff
*pa
, void *user
)
7995 struct isl_union_pw_aff_pullback_upma_data
*data
= user
;
7999 r
= isl_union_pw_multi_aff_foreach_pw_multi_aff(data
->upma
,
8001 isl_pw_aff_free(pa
);
8006 /* Compute the pullback of "upa" by the function represented by "upma".
8007 * In other words, plug in "upma" in "upa". The result contains
8008 * expressions defined over the domain space of "upma".
8010 * Run over all pairs of elements in "upa" and "upma", perform
8011 * the pullback when appropriate and collect the results.
8012 * If the hash value were based on the domain space rather than
8013 * the function space, then we could run through all elements
8014 * of "upma" and directly pick out the corresponding element of "upa".
8016 __isl_give isl_union_pw_aff
*isl_union_pw_aff_pullback_union_pw_multi_aff(
8017 __isl_take isl_union_pw_aff
*upa
,
8018 __isl_take isl_union_pw_multi_aff
*upma
)
8020 struct isl_union_pw_aff_pullback_upma_data data
= { NULL
, NULL
};
8023 space
= isl_union_pw_multi_aff_get_space(upma
);
8024 upa
= isl_union_pw_aff_align_params(upa
, space
);
8025 space
= isl_union_pw_aff_get_space(upa
);
8026 upma
= isl_union_pw_multi_aff_align_params(upma
, space
);
8032 data
.res
= isl_union_pw_aff_alloc_same_size(upa
);
8033 if (isl_union_pw_aff_foreach_pw_aff(upa
, &upa_pb_upma
, &data
) < 0)
8034 data
.res
= isl_union_pw_aff_free(data
.res
);
8036 isl_union_pw_aff_free(upa
);
8037 isl_union_pw_multi_aff_free(upma
);
8040 isl_union_pw_aff_free(upa
);
8041 isl_union_pw_multi_aff_free(upma
);
8046 #define BASE union_pw_aff
8048 #define DOMBASE union_set
8050 #include <isl_multi_explicit_domain.c>
8051 #include <isl_multi_union_pw_aff_explicit_domain.c>
8052 #include <isl_multi_templ.c>
8053 #include <isl_multi_apply_set.c>
8054 #include <isl_multi_apply_union_set.c>
8055 #include <isl_multi_arith_templ.c>
8056 #include <isl_multi_coalesce.c>
8057 #include <isl_multi_dim_id_templ.c>
8058 #include <isl_multi_floor.c>
8059 #include <isl_multi_from_base_templ.c>
8060 #include <isl_multi_gist.c>
8061 #include <isl_multi_align_set.c>
8062 #include <isl_multi_align_union_set.c>
8063 #include <isl_multi_intersect.c>
8064 #include <isl_multi_nan_templ.c>
8065 #include <isl_multi_tuple_id_templ.c>
8067 /* Does "mupa" have a non-trivial explicit domain?
8069 * The explicit domain, if present, is trivial if it represents
8070 * an (obviously) universe parameter set.
8072 isl_bool
isl_multi_union_pw_aff_has_non_trivial_domain(
8073 __isl_keep isl_multi_union_pw_aff
*mupa
)
8075 isl_bool is_params
, trivial
;
8079 return isl_bool_error
;
8080 if (!isl_multi_union_pw_aff_has_explicit_domain(mupa
))
8081 return isl_bool_false
;
8082 is_params
= isl_union_set_is_params(mupa
->u
.dom
);
8083 if (is_params
< 0 || !is_params
)
8084 return isl_bool_not(is_params
);
8085 set
= isl_set_from_union_set(isl_union_set_copy(mupa
->u
.dom
));
8086 trivial
= isl_set_plain_is_universe(set
);
8088 return isl_bool_not(trivial
);
8091 /* Construct a multiple union piecewise affine expression
8092 * in the given space with value zero in each of the output dimensions.
8094 * Since there is no canonical zero value for
8095 * a union piecewise affine expression, we can only construct
8096 * a zero-dimensional "zero" value.
8098 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_zero(
8099 __isl_take isl_space
*space
)
8107 params
= isl_space_is_params(space
);
8111 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
8112 "expecting proper set space", goto error
);
8113 if (!isl_space_is_set(space
))
8114 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
8115 "expecting set space", goto error
);
8116 dim
= isl_space_dim(space
, isl_dim_out
);
8120 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
8121 "expecting 0D space", goto error
);
8123 return isl_multi_union_pw_aff_alloc(space
);
8125 isl_space_free(space
);
8129 /* Compute the sum of "mupa1" and "mupa2" on the union of their domains,
8130 * with the actual sum on the shared domain and
8131 * the defined expression on the symmetric difference of the domains.
8133 * We simply iterate over the elements in both arguments and
8134 * call isl_union_pw_aff_union_add on each of them, if there is
8135 * at least one element.
8137 * Otherwise, the two expressions have an explicit domain and
8138 * the union of these explicit domains is computed.
8139 * This assumes that the explicit domains are either both in terms
8140 * of specific domains elements or both in terms of parameters.
8141 * However, if one of the expressions does not have any constraints
8142 * on its explicit domain, then this is allowed as well and the result
8143 * is the expression with no constraints on its explicit domain.
8145 static __isl_give isl_multi_union_pw_aff
*
8146 isl_multi_union_pw_aff_union_add_aligned(
8147 __isl_take isl_multi_union_pw_aff
*mupa1
,
8148 __isl_take isl_multi_union_pw_aff
*mupa2
)
8150 isl_bool has_domain
, is_params1
, is_params2
;
8152 if (isl_multi_union_pw_aff_check_equal_space(mupa1
, mupa2
) < 0)
8155 return isl_multi_union_pw_aff_bin_op(mupa1
, mupa2
,
8156 &isl_union_pw_aff_union_add
);
8157 if (isl_multi_union_pw_aff_check_has_explicit_domain(mupa1
) < 0 ||
8158 isl_multi_union_pw_aff_check_has_explicit_domain(mupa2
) < 0)
8161 has_domain
= isl_multi_union_pw_aff_has_non_trivial_domain(mupa1
);
8165 isl_multi_union_pw_aff_free(mupa2
);
8168 has_domain
= isl_multi_union_pw_aff_has_non_trivial_domain(mupa2
);
8172 isl_multi_union_pw_aff_free(mupa1
);
8176 is_params1
= isl_union_set_is_params(mupa1
->u
.dom
);
8177 is_params2
= isl_union_set_is_params(mupa2
->u
.dom
);
8178 if (is_params1
< 0 || is_params2
< 0)
8180 if (is_params1
!= is_params2
)
8181 isl_die(isl_multi_union_pw_aff_get_ctx(mupa1
),
8183 "cannot compute union of concrete domain and "
8184 "parameter constraints", goto error
);
8185 mupa1
= isl_multi_union_pw_aff_cow(mupa1
);
8188 mupa1
->u
.dom
= isl_union_set_union(mupa1
->u
.dom
,
8189 isl_union_set_copy(mupa2
->u
.dom
));
8192 isl_multi_union_pw_aff_free(mupa2
);
8195 isl_multi_union_pw_aff_free(mupa1
);
8196 isl_multi_union_pw_aff_free(mupa2
);
8200 /* Compute the sum of "mupa1" and "mupa2" on the union of their domains,
8201 * with the actual sum on the shared domain and
8202 * the defined expression on the symmetric difference of the domains.
8204 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_union_add(
8205 __isl_take isl_multi_union_pw_aff
*mupa1
,
8206 __isl_take isl_multi_union_pw_aff
*mupa2
)
8208 return isl_multi_union_pw_aff_align_params_multi_multi_and(mupa1
, mupa2
,
8209 &isl_multi_union_pw_aff_union_add_aligned
);
8212 /* Construct and return a multi union piecewise affine expression
8213 * that is equal to the given multi affine expression.
8215 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_from_multi_aff(
8216 __isl_take isl_multi_aff
*ma
)
8218 isl_multi_pw_aff
*mpa
;
8220 mpa
= isl_multi_pw_aff_from_multi_aff(ma
);
8221 return isl_multi_union_pw_aff_from_multi_pw_aff(mpa
);
8224 /* Construct and return a multi union piecewise affine expression
8225 * that is equal to the given multi piecewise affine expression.
8227 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_from_multi_pw_aff(
8228 __isl_take isl_multi_pw_aff
*mpa
)
8233 isl_multi_union_pw_aff
*mupa
;
8235 n
= isl_multi_pw_aff_dim(mpa
, isl_dim_out
);
8237 mpa
= isl_multi_pw_aff_free(mpa
);
8241 space
= isl_multi_pw_aff_get_space(mpa
);
8242 space
= isl_space_range(space
);
8243 mupa
= isl_multi_union_pw_aff_alloc(space
);
8245 for (i
= 0; i
< n
; ++i
) {
8247 isl_union_pw_aff
*upa
;
8249 pa
= isl_multi_pw_aff_get_pw_aff(mpa
, i
);
8250 upa
= isl_union_pw_aff_from_pw_aff(pa
);
8251 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
8254 isl_multi_pw_aff_free(mpa
);
8259 /* Extract the range space of "pma" and assign it to *space.
8260 * If *space has already been set (through a previous call to this function),
8261 * then check that the range space is the same.
8263 static isl_stat
extract_space(__isl_take isl_pw_multi_aff
*pma
, void *user
)
8265 isl_space
**space
= user
;
8266 isl_space
*pma_space
;
8269 pma_space
= isl_space_range(isl_pw_multi_aff_get_space(pma
));
8270 isl_pw_multi_aff_free(pma
);
8273 return isl_stat_error
;
8279 equal
= isl_space_is_equal(pma_space
, *space
);
8280 isl_space_free(pma_space
);
8283 return isl_stat_error
;
8285 isl_die(isl_space_get_ctx(*space
), isl_error_invalid
,
8286 "range spaces not the same", return isl_stat_error
);
8290 /* Construct and return a multi union piecewise affine expression
8291 * that is equal to the given union piecewise multi affine expression.
8293 * In order to be able to perform the conversion, the input
8294 * needs to be non-empty and may only involve a single range space.
8296 * If the resulting multi union piecewise affine expression has
8297 * an explicit domain, then assign it the domain of the input.
8298 * In other cases, the domain is stored in the individual elements.
8300 __isl_give isl_multi_union_pw_aff
*
8301 isl_multi_union_pw_aff_from_union_pw_multi_aff(
8302 __isl_take isl_union_pw_multi_aff
*upma
)
8304 isl_space
*space
= NULL
;
8305 isl_multi_union_pw_aff
*mupa
;
8309 n
= isl_union_pw_multi_aff_n_pw_multi_aff(upma
);
8313 isl_die(isl_union_pw_multi_aff_get_ctx(upma
), isl_error_invalid
,
8314 "cannot extract range space from empty input",
8316 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma
, &extract_space
,
8323 n
= isl_space_dim(space
, isl_dim_set
);
8325 space
= isl_space_free(space
);
8326 mupa
= isl_multi_union_pw_aff_alloc(space
);
8328 for (i
= 0; i
< n
; ++i
) {
8329 isl_union_pw_aff
*upa
;
8331 upa
= isl_union_pw_multi_aff_get_union_pw_aff(upma
, i
);
8332 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
8334 if (isl_multi_union_pw_aff_has_explicit_domain(mupa
)) {
8336 isl_union_pw_multi_aff
*copy
;
8338 copy
= isl_union_pw_multi_aff_copy(upma
);
8339 dom
= isl_union_pw_multi_aff_domain(copy
);
8340 mupa
= isl_multi_union_pw_aff_intersect_domain(mupa
, dom
);
8343 isl_union_pw_multi_aff_free(upma
);
8346 isl_space_free(space
);
8347 isl_union_pw_multi_aff_free(upma
);
8351 /* Try and create an isl_multi_union_pw_aff that is equivalent
8352 * to the given isl_union_map.
8353 * The isl_union_map is required to be single-valued in each space.
8354 * Moreover, it cannot be empty and all range spaces need to be the same.
8355 * Otherwise, an error is produced.
8357 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_from_union_map(
8358 __isl_take isl_union_map
*umap
)
8360 isl_union_pw_multi_aff
*upma
;
8362 upma
= isl_union_pw_multi_aff_from_union_map(umap
);
8363 return isl_multi_union_pw_aff_from_union_pw_multi_aff(upma
);
8366 /* Return a multiple union piecewise affine expression
8367 * that is equal to "mv" on "domain", assuming "domain" and "mv"
8368 * have been aligned.
8370 * If the resulting multi union piecewise affine expression has
8371 * an explicit domain, then assign it the input domain.
8372 * In other cases, the domain is stored in the individual elements.
8374 static __isl_give isl_multi_union_pw_aff
*
8375 isl_multi_union_pw_aff_multi_val_on_domain_aligned(
8376 __isl_take isl_union_set
*domain
, __isl_take isl_multi_val
*mv
)
8381 isl_multi_union_pw_aff
*mupa
;
8383 n
= isl_multi_val_dim(mv
, isl_dim_set
);
8384 if (!domain
|| n
< 0)
8387 space
= isl_multi_val_get_space(mv
);
8388 mupa
= isl_multi_union_pw_aff_alloc(space
);
8389 for (i
= 0; i
< n
; ++i
) {
8391 isl_union_pw_aff
*upa
;
8393 v
= isl_multi_val_get_val(mv
, i
);
8394 upa
= isl_union_pw_aff_val_on_domain(isl_union_set_copy(domain
),
8396 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
8398 if (isl_multi_union_pw_aff_has_explicit_domain(mupa
))
8399 mupa
= isl_multi_union_pw_aff_intersect_domain(mupa
,
8400 isl_union_set_copy(domain
));
8402 isl_union_set_free(domain
);
8403 isl_multi_val_free(mv
);
8406 isl_union_set_free(domain
);
8407 isl_multi_val_free(mv
);
8411 /* Return a multiple union piecewise affine expression
8412 * that is equal to "mv" on "domain".
8414 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_multi_val_on_domain(
8415 __isl_take isl_union_set
*domain
, __isl_take isl_multi_val
*mv
)
8417 isl_bool equal_params
;
8421 equal_params
= isl_space_has_equal_params(domain
->dim
, mv
->space
);
8422 if (equal_params
< 0)
8425 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(
8427 domain
= isl_union_set_align_params(domain
,
8428 isl_multi_val_get_space(mv
));
8429 mv
= isl_multi_val_align_params(mv
, isl_union_set_get_space(domain
));
8430 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(domain
, mv
);
8432 isl_union_set_free(domain
);
8433 isl_multi_val_free(mv
);
8437 /* Return a multiple union piecewise affine expression
8438 * that is equal to "ma" on "domain".
8440 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_multi_aff_on_domain(
8441 __isl_take isl_union_set
*domain
, __isl_take isl_multi_aff
*ma
)
8443 isl_pw_multi_aff
*pma
;
8445 pma
= isl_pw_multi_aff_from_multi_aff(ma
);
8446 return isl_multi_union_pw_aff_pw_multi_aff_on_domain(domain
, pma
);
8449 /* Return a multiple union piecewise affine expression
8450 * that is equal to "pma" on "domain", assuming "domain" and "pma"
8451 * have been aligned.
8453 * If the resulting multi union piecewise affine expression has
8454 * an explicit domain, then assign it the input domain.
8455 * In other cases, the domain is stored in the individual elements.
8457 static __isl_give isl_multi_union_pw_aff
*
8458 isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(
8459 __isl_take isl_union_set
*domain
, __isl_take isl_pw_multi_aff
*pma
)
8464 isl_multi_union_pw_aff
*mupa
;
8466 n
= isl_pw_multi_aff_dim(pma
, isl_dim_set
);
8467 if (!domain
|| n
< 0)
8469 space
= isl_pw_multi_aff_get_space(pma
);
8470 mupa
= isl_multi_union_pw_aff_alloc(space
);
8471 for (i
= 0; i
< n
; ++i
) {
8473 isl_union_pw_aff
*upa
;
8475 pa
= isl_pw_multi_aff_get_pw_aff(pma
, i
);
8476 upa
= isl_union_pw_aff_pw_aff_on_domain(
8477 isl_union_set_copy(domain
), pa
);
8478 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
8480 if (isl_multi_union_pw_aff_has_explicit_domain(mupa
))
8481 mupa
= isl_multi_union_pw_aff_intersect_domain(mupa
,
8482 isl_union_set_copy(domain
));
8484 isl_union_set_free(domain
);
8485 isl_pw_multi_aff_free(pma
);
8488 isl_union_set_free(domain
);
8489 isl_pw_multi_aff_free(pma
);
8493 /* Return a multiple union piecewise affine expression
8494 * that is equal to "pma" on "domain".
8496 __isl_give isl_multi_union_pw_aff
*
8497 isl_multi_union_pw_aff_pw_multi_aff_on_domain(__isl_take isl_union_set
*domain
,
8498 __isl_take isl_pw_multi_aff
*pma
)
8500 isl_bool equal_params
;
8503 space
= isl_pw_multi_aff_peek_space(pma
);
8504 equal_params
= isl_union_set_space_has_equal_params(domain
, space
);
8505 if (equal_params
< 0)
8508 return isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(
8510 domain
= isl_union_set_align_params(domain
,
8511 isl_pw_multi_aff_get_space(pma
));
8512 pma
= isl_pw_multi_aff_align_params(pma
,
8513 isl_union_set_get_space(domain
));
8514 return isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(domain
,
8517 isl_union_set_free(domain
);
8518 isl_pw_multi_aff_free(pma
);
8522 /* Return a union set containing those elements in the domains
8523 * of the elements of "mupa" where they are all zero.
8525 * If there are no elements, then simply return the entire domain.
8527 __isl_give isl_union_set
*isl_multi_union_pw_aff_zero_union_set(
8528 __isl_take isl_multi_union_pw_aff
*mupa
)
8532 isl_union_pw_aff
*upa
;
8533 isl_union_set
*zero
;
8535 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8537 mupa
= isl_multi_union_pw_aff_free(mupa
);
8542 return isl_multi_union_pw_aff_domain(mupa
);
8544 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, 0);
8545 zero
= isl_union_pw_aff_zero_union_set(upa
);
8547 for (i
= 1; i
< n
; ++i
) {
8548 isl_union_set
*zero_i
;
8550 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
8551 zero_i
= isl_union_pw_aff_zero_union_set(upa
);
8553 zero
= isl_union_set_intersect(zero
, zero_i
);
8556 isl_multi_union_pw_aff_free(mupa
);
8560 /* Construct a union map mapping the shared domain
8561 * of the union piecewise affine expressions to the range of "mupa"
8562 * in the special case of a 0D multi union piecewise affine expression.
8564 * Construct a map between the explicit domain of "mupa" and
8566 * Note that this assumes that the domain consists of explicit elements.
8568 static __isl_give isl_union_map
*isl_union_map_from_multi_union_pw_aff_0D(
8569 __isl_take isl_multi_union_pw_aff
*mupa
)
8573 isl_union_set
*dom
, *ran
;
8575 space
= isl_multi_union_pw_aff_get_space(mupa
);
8576 dom
= isl_multi_union_pw_aff_domain(mupa
);
8577 ran
= isl_union_set_from_set(isl_set_universe(space
));
8579 is_params
= isl_union_set_is_params(dom
);
8581 dom
= isl_union_set_free(dom
);
8583 isl_die(isl_union_set_get_ctx(dom
), isl_error_invalid
,
8584 "cannot create union map from expression without "
8585 "explicit domain elements",
8586 dom
= isl_union_set_free(dom
));
8588 return isl_union_map_from_domain_and_range(dom
, ran
);
8591 /* Construct a union map mapping the shared domain
8592 * of the union piecewise affine expressions to the range of "mupa"
8593 * with each dimension in the range equated to the
8594 * corresponding union piecewise affine expression.
8596 * If the input is zero-dimensional, then construct a mapping
8597 * from its explicit domain.
8599 __isl_give isl_union_map
*isl_union_map_from_multi_union_pw_aff(
8600 __isl_take isl_multi_union_pw_aff
*mupa
)
8605 isl_union_map
*umap
;
8606 isl_union_pw_aff
*upa
;
8608 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8610 mupa
= isl_multi_union_pw_aff_free(mupa
);
8615 return isl_union_map_from_multi_union_pw_aff_0D(mupa
);
8617 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, 0);
8618 umap
= isl_union_map_from_union_pw_aff(upa
);
8620 for (i
= 1; i
< n
; ++i
) {
8621 isl_union_map
*umap_i
;
8623 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
8624 umap_i
= isl_union_map_from_union_pw_aff(upa
);
8625 umap
= isl_union_map_flat_range_product(umap
, umap_i
);
8628 space
= isl_multi_union_pw_aff_get_space(mupa
);
8629 umap
= isl_union_map_reset_range_space(umap
, space
);
8631 isl_multi_union_pw_aff_free(mupa
);
8635 /* Internal data structure for isl_union_pw_multi_aff_reset_range_space.
8636 * "range" is the space from which to set the range space.
8637 * "res" collects the results.
8639 struct isl_union_pw_multi_aff_reset_range_space_data
{
8641 isl_union_pw_multi_aff
*res
;
8644 /* Replace the range space of "pma" by the range space of data->range and
8645 * add the result to data->res.
8647 static isl_stat
reset_range_space(__isl_take isl_pw_multi_aff
*pma
, void *user
)
8649 struct isl_union_pw_multi_aff_reset_range_space_data
*data
= user
;
8652 space
= isl_pw_multi_aff_get_space(pma
);
8653 space
= isl_space_domain(space
);
8654 space
= isl_space_extend_domain_with_range(space
,
8655 isl_space_copy(data
->range
));
8656 pma
= isl_pw_multi_aff_reset_space(pma
, space
);
8657 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma
);
8659 return data
->res
? isl_stat_ok
: isl_stat_error
;
8662 /* Replace the range space of all the piecewise affine expressions in "upma" by
8663 * the range space of "space".
8665 * This assumes that all these expressions have the same output dimension.
8667 * Since the spaces of the expressions change, so do their hash values.
8668 * We therefore need to create a new isl_union_pw_multi_aff.
8669 * Note that the hash value is currently computed based on the entire
8670 * space even though there can only be a single expression with a given
8673 static __isl_give isl_union_pw_multi_aff
*
8674 isl_union_pw_multi_aff_reset_range_space(
8675 __isl_take isl_union_pw_multi_aff
*upma
, __isl_take isl_space
*space
)
8677 struct isl_union_pw_multi_aff_reset_range_space_data data
= { space
};
8678 isl_space
*space_upma
;
8680 space_upma
= isl_union_pw_multi_aff_get_space(upma
);
8681 data
.res
= isl_union_pw_multi_aff_empty(space_upma
);
8682 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma
,
8683 &reset_range_space
, &data
) < 0)
8684 data
.res
= isl_union_pw_multi_aff_free(data
.res
);
8686 isl_space_free(space
);
8687 isl_union_pw_multi_aff_free(upma
);
8691 /* Construct and return a union piecewise multi affine expression
8692 * that is equal to the given multi union piecewise affine expression,
8693 * in the special case of a 0D multi union piecewise affine expression.
8695 * Construct a union piecewise multi affine expression
8696 * on top of the explicit domain of the input.
8698 __isl_give isl_union_pw_multi_aff
*
8699 isl_union_pw_multi_aff_from_multi_union_pw_aff_0D(
8700 __isl_take isl_multi_union_pw_aff
*mupa
)
8704 isl_union_set
*domain
;
8706 space
= isl_multi_union_pw_aff_get_space(mupa
);
8707 mv
= isl_multi_val_zero(space
);
8708 domain
= isl_multi_union_pw_aff_domain(mupa
);
8709 return isl_union_pw_multi_aff_multi_val_on_domain(domain
, mv
);
8712 /* Construct and return a union piecewise multi affine expression
8713 * that is equal to the given multi union piecewise affine expression.
8715 * If the input is zero-dimensional, then
8716 * construct a union piecewise multi affine expression
8717 * on top of the explicit domain of the input.
8719 __isl_give isl_union_pw_multi_aff
*
8720 isl_union_pw_multi_aff_from_multi_union_pw_aff(
8721 __isl_take isl_multi_union_pw_aff
*mupa
)
8726 isl_union_pw_multi_aff
*upma
;
8727 isl_union_pw_aff
*upa
;
8729 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8731 mupa
= isl_multi_union_pw_aff_free(mupa
);
8736 return isl_union_pw_multi_aff_from_multi_union_pw_aff_0D(mupa
);
8738 space
= isl_multi_union_pw_aff_get_space(mupa
);
8739 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, 0);
8740 upma
= isl_union_pw_multi_aff_from_union_pw_aff(upa
);
8742 for (i
= 1; i
< n
; ++i
) {
8743 isl_union_pw_multi_aff
*upma_i
;
8745 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
8746 upma_i
= isl_union_pw_multi_aff_from_union_pw_aff(upa
);
8747 upma
= isl_union_pw_multi_aff_flat_range_product(upma
, upma_i
);
8750 upma
= isl_union_pw_multi_aff_reset_range_space(upma
, space
);
8752 isl_multi_union_pw_aff_free(mupa
);
8756 /* Intersect the range of "mupa" with "range",
8757 * in the special case where "mupa" is 0D.
8759 * Intersect the domain of "mupa" with the constraints on the parameters
8762 static __isl_give isl_multi_union_pw_aff
*mupa_intersect_range_0D(
8763 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_set
*range
)
8765 range
= isl_set_params(range
);
8766 mupa
= isl_multi_union_pw_aff_intersect_params(mupa
, range
);
8770 /* Intersect the range of "mupa" with "range".
8771 * That is, keep only those domain elements that have a function value
8774 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_intersect_range(
8775 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_set
*range
)
8777 isl_union_pw_multi_aff
*upma
;
8778 isl_union_set
*domain
;
8783 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8784 if (n
< 0 || !range
)
8787 space
= isl_set_get_space(range
);
8788 match
= isl_space_tuple_is_equal(mupa
->space
, isl_dim_set
,
8789 space
, isl_dim_set
);
8790 isl_space_free(space
);
8794 isl_die(isl_multi_union_pw_aff_get_ctx(mupa
), isl_error_invalid
,
8795 "space don't match", goto error
);
8797 return mupa_intersect_range_0D(mupa
, range
);
8799 upma
= isl_union_pw_multi_aff_from_multi_union_pw_aff(
8800 isl_multi_union_pw_aff_copy(mupa
));
8801 domain
= isl_union_set_from_set(range
);
8802 domain
= isl_union_set_preimage_union_pw_multi_aff(domain
, upma
);
8803 mupa
= isl_multi_union_pw_aff_intersect_domain(mupa
, domain
);
8807 isl_multi_union_pw_aff_free(mupa
);
8808 isl_set_free(range
);
8812 /* Return the shared domain of the elements of "mupa",
8813 * in the special case where "mupa" is zero-dimensional.
8815 * Return the explicit domain of "mupa".
8816 * Note that this domain may be a parameter set, either
8817 * because "mupa" is meant to live in a set space or
8818 * because no explicit domain has been set.
8820 __isl_give isl_union_set
*isl_multi_union_pw_aff_domain_0D(
8821 __isl_take isl_multi_union_pw_aff
*mupa
)
8825 dom
= isl_multi_union_pw_aff_get_explicit_domain(mupa
);
8826 isl_multi_union_pw_aff_free(mupa
);
8831 /* Return the shared domain of the elements of "mupa".
8833 * If "mupa" is zero-dimensional, then return its explicit domain.
8835 __isl_give isl_union_set
*isl_multi_union_pw_aff_domain(
8836 __isl_take isl_multi_union_pw_aff
*mupa
)
8840 isl_union_pw_aff
*upa
;
8843 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8845 mupa
= isl_multi_union_pw_aff_free(mupa
);
8850 return isl_multi_union_pw_aff_domain_0D(mupa
);
8852 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, 0);
8853 dom
= isl_union_pw_aff_domain(upa
);
8854 for (i
= 1; i
< n
; ++i
) {
8855 isl_union_set
*dom_i
;
8857 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
8858 dom_i
= isl_union_pw_aff_domain(upa
);
8859 dom
= isl_union_set_intersect(dom
, dom_i
);
8862 isl_multi_union_pw_aff_free(mupa
);
8866 /* Apply "aff" to "mupa". The space of "mupa" is equal to the domain of "aff".
8867 * In particular, the spaces have been aligned.
8868 * The result is defined over the shared domain of the elements of "mupa"
8870 * We first extract the parametric constant part of "aff" and
8871 * define that over the shared domain.
8872 * Then we iterate over all input dimensions of "aff" and add the corresponding
8873 * multiples of the elements of "mupa".
8874 * Finally, we consider the integer divisions, calling the function
8875 * recursively to obtain an isl_union_pw_aff corresponding to the
8876 * integer division argument.
8878 static __isl_give isl_union_pw_aff
*multi_union_pw_aff_apply_aff(
8879 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_aff
*aff
)
8882 isl_size n_in
, n_div
;
8883 isl_union_pw_aff
*upa
;
8884 isl_union_set
*uset
;
8888 n_in
= isl_aff_dim(aff
, isl_dim_in
);
8889 n_div
= isl_aff_dim(aff
, isl_dim_div
);
8890 if (n_in
< 0 || n_div
< 0)
8893 uset
= isl_multi_union_pw_aff_domain(isl_multi_union_pw_aff_copy(mupa
));
8894 cst
= isl_aff_copy(aff
);
8895 cst
= isl_aff_drop_dims(cst
, isl_dim_div
, 0, n_div
);
8896 cst
= isl_aff_drop_dims(cst
, isl_dim_in
, 0, n_in
);
8897 cst
= isl_aff_project_domain_on_params(cst
);
8898 upa
= isl_union_pw_aff_aff_on_domain(uset
, cst
);
8900 for (i
= 0; i
< n_in
; ++i
) {
8901 isl_union_pw_aff
*upa_i
;
8903 if (!isl_aff_involves_dims(aff
, isl_dim_in
, i
, 1))
8905 v
= isl_aff_get_coefficient_val(aff
, isl_dim_in
, i
);
8906 upa_i
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
8907 upa_i
= isl_union_pw_aff_scale_val(upa_i
, v
);
8908 upa
= isl_union_pw_aff_add(upa
, upa_i
);
8911 for (i
= 0; i
< n_div
; ++i
) {
8913 isl_union_pw_aff
*upa_i
;
8915 if (!isl_aff_involves_dims(aff
, isl_dim_div
, i
, 1))
8917 div
= isl_aff_get_div(aff
, i
);
8918 upa_i
= multi_union_pw_aff_apply_aff(
8919 isl_multi_union_pw_aff_copy(mupa
), div
);
8920 upa_i
= isl_union_pw_aff_floor(upa_i
);
8921 v
= isl_aff_get_coefficient_val(aff
, isl_dim_div
, i
);
8922 upa_i
= isl_union_pw_aff_scale_val(upa_i
, v
);
8923 upa
= isl_union_pw_aff_add(upa
, upa_i
);
8926 isl_multi_union_pw_aff_free(mupa
);
8931 isl_multi_union_pw_aff_free(mupa
);
8936 /* Apply "aff" to "mupa". The space of "mupa" needs to be compatible
8937 * with the domain of "aff".
8938 * Furthermore, the dimension of this space needs to be greater than zero.
8939 * The result is defined over the shared domain of the elements of "mupa"
8941 * We perform these checks and then hand over control to
8942 * multi_union_pw_aff_apply_aff.
8944 __isl_give isl_union_pw_aff
*isl_multi_union_pw_aff_apply_aff(
8945 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_aff
*aff
)
8948 isl_space
*space1
, *space2
;
8951 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
8952 isl_aff_get_space(aff
));
8953 aff
= isl_aff_align_params(aff
, isl_multi_union_pw_aff_get_space(mupa
));
8957 space1
= isl_multi_union_pw_aff_get_space(mupa
);
8958 space2
= isl_aff_get_domain_space(aff
);
8959 equal
= isl_space_is_equal(space1
, space2
);
8960 isl_space_free(space1
);
8961 isl_space_free(space2
);
8965 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
8966 "spaces don't match", goto error
);
8967 dim
= isl_aff_dim(aff
, isl_dim_in
);
8971 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
8972 "cannot determine domains", goto error
);
8974 return multi_union_pw_aff_apply_aff(mupa
, aff
);
8976 isl_multi_union_pw_aff_free(mupa
);
8981 /* Apply "ma" to "mupa", in the special case where "mupa" is 0D.
8982 * The space of "mupa" is known to be compatible with the domain of "ma".
8984 * Construct an isl_multi_union_pw_aff that is equal to "ma"
8985 * on the domain of "mupa".
8987 static __isl_give isl_multi_union_pw_aff
*mupa_apply_multi_aff_0D(
8988 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_multi_aff
*ma
)
8992 dom
= isl_multi_union_pw_aff_domain(mupa
);
8993 ma
= isl_multi_aff_project_domain_on_params(ma
);
8995 return isl_multi_union_pw_aff_multi_aff_on_domain(dom
, ma
);
8998 /* Apply "ma" to "mupa". The space of "mupa" needs to be compatible
8999 * with the domain of "ma".
9000 * The result is defined over the shared domain of the elements of "mupa"
9002 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_apply_multi_aff(
9003 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_multi_aff
*ma
)
9005 isl_space
*space1
, *space2
;
9006 isl_multi_union_pw_aff
*res
;
9009 isl_size n_in
, n_out
;
9011 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
9012 isl_multi_aff_get_space(ma
));
9013 ma
= isl_multi_aff_align_params(ma
,
9014 isl_multi_union_pw_aff_get_space(mupa
));
9015 n_in
= isl_multi_aff_dim(ma
, isl_dim_in
);
9016 n_out
= isl_multi_aff_dim(ma
, isl_dim_out
);
9017 if (!mupa
|| n_in
< 0 || n_out
< 0)
9020 space1
= isl_multi_union_pw_aff_get_space(mupa
);
9021 space2
= isl_multi_aff_get_domain_space(ma
);
9022 equal
= isl_space_is_equal(space1
, space2
);
9023 isl_space_free(space1
);
9024 isl_space_free(space2
);
9028 isl_die(isl_multi_aff_get_ctx(ma
), isl_error_invalid
,
9029 "spaces don't match", goto error
);
9031 return mupa_apply_multi_aff_0D(mupa
, ma
);
9033 space1
= isl_space_range(isl_multi_aff_get_space(ma
));
9034 res
= isl_multi_union_pw_aff_alloc(space1
);
9036 for (i
= 0; i
< n_out
; ++i
) {
9038 isl_union_pw_aff
*upa
;
9040 aff
= isl_multi_aff_get_aff(ma
, i
);
9041 upa
= multi_union_pw_aff_apply_aff(
9042 isl_multi_union_pw_aff_copy(mupa
), aff
);
9043 res
= isl_multi_union_pw_aff_set_union_pw_aff(res
, i
, upa
);
9046 isl_multi_aff_free(ma
);
9047 isl_multi_union_pw_aff_free(mupa
);
9050 isl_multi_union_pw_aff_free(mupa
);
9051 isl_multi_aff_free(ma
);
9055 /* Apply "pa" to "mupa", in the special case where "mupa" is 0D.
9056 * The space of "mupa" is known to be compatible with the domain of "pa".
9058 * Construct an isl_multi_union_pw_aff that is equal to "pa"
9059 * on the domain of "mupa".
9061 static __isl_give isl_union_pw_aff
*isl_multi_union_pw_aff_apply_pw_aff_0D(
9062 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_pw_aff
*pa
)
9066 dom
= isl_multi_union_pw_aff_domain(mupa
);
9067 pa
= isl_pw_aff_project_domain_on_params(pa
);
9069 return isl_union_pw_aff_pw_aff_on_domain(dom
, pa
);
9072 /* Apply "pa" to "mupa". The space of "mupa" needs to be compatible
9073 * with the domain of "pa".
9074 * Furthermore, the dimension of this space needs to be greater than zero.
9075 * The result is defined over the shared domain of the elements of "mupa"
9077 __isl_give isl_union_pw_aff
*isl_multi_union_pw_aff_apply_pw_aff(
9078 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_pw_aff
*pa
)
9083 isl_space
*space
, *space2
;
9084 isl_union_pw_aff
*upa
;
9086 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
9087 isl_pw_aff_get_space(pa
));
9088 pa
= isl_pw_aff_align_params(pa
,
9089 isl_multi_union_pw_aff_get_space(mupa
));
9093 space
= isl_multi_union_pw_aff_get_space(mupa
);
9094 space2
= isl_pw_aff_get_domain_space(pa
);
9095 equal
= isl_space_is_equal(space
, space2
);
9096 isl_space_free(space
);
9097 isl_space_free(space2
);
9101 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
9102 "spaces don't match", goto error
);
9103 n_in
= isl_pw_aff_dim(pa
, isl_dim_in
);
9107 return isl_multi_union_pw_aff_apply_pw_aff_0D(mupa
, pa
);
9109 space
= isl_space_params(isl_multi_union_pw_aff_get_space(mupa
));
9110 upa
= isl_union_pw_aff_empty(space
);
9112 for (i
= 0; i
< pa
->n
; ++i
) {
9115 isl_multi_union_pw_aff
*mupa_i
;
9116 isl_union_pw_aff
*upa_i
;
9118 mupa_i
= isl_multi_union_pw_aff_copy(mupa
);
9119 domain
= isl_set_copy(pa
->p
[i
].set
);
9120 mupa_i
= isl_multi_union_pw_aff_intersect_range(mupa_i
, domain
);
9121 aff
= isl_aff_copy(pa
->p
[i
].aff
);
9122 upa_i
= multi_union_pw_aff_apply_aff(mupa_i
, aff
);
9123 upa
= isl_union_pw_aff_union_add(upa
, upa_i
);
9126 isl_multi_union_pw_aff_free(mupa
);
9127 isl_pw_aff_free(pa
);
9130 isl_multi_union_pw_aff_free(mupa
);
9131 isl_pw_aff_free(pa
);
9135 /* Apply "pma" to "mupa", in the special case where "mupa" is 0D.
9136 * The space of "mupa" is known to be compatible with the domain of "pma".
9138 * Construct an isl_multi_union_pw_aff that is equal to "pma"
9139 * on the domain of "mupa".
9141 static __isl_give isl_multi_union_pw_aff
*mupa_apply_pw_multi_aff_0D(
9142 __isl_take isl_multi_union_pw_aff
*mupa
,
9143 __isl_take isl_pw_multi_aff
*pma
)
9147 dom
= isl_multi_union_pw_aff_domain(mupa
);
9148 pma
= isl_pw_multi_aff_project_domain_on_params(pma
);
9150 return isl_multi_union_pw_aff_pw_multi_aff_on_domain(dom
, pma
);
9153 /* Apply "pma" to "mupa". The space of "mupa" needs to be compatible
9154 * with the domain of "pma".
9155 * The result is defined over the shared domain of the elements of "mupa"
9157 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_apply_pw_multi_aff(
9158 __isl_take isl_multi_union_pw_aff
*mupa
,
9159 __isl_take isl_pw_multi_aff
*pma
)
9161 isl_space
*space1
, *space2
;
9162 isl_multi_union_pw_aff
*res
;
9165 isl_size n_in
, n_out
;
9167 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
9168 isl_pw_multi_aff_get_space(pma
));
9169 pma
= isl_pw_multi_aff_align_params(pma
,
9170 isl_multi_union_pw_aff_get_space(mupa
));
9174 space1
= isl_multi_union_pw_aff_get_space(mupa
);
9175 space2
= isl_pw_multi_aff_get_domain_space(pma
);
9176 equal
= isl_space_is_equal(space1
, space2
);
9177 isl_space_free(space1
);
9178 isl_space_free(space2
);
9182 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
9183 "spaces don't match", goto error
);
9184 n_in
= isl_pw_multi_aff_dim(pma
, isl_dim_in
);
9185 n_out
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
9186 if (n_in
< 0 || n_out
< 0)
9189 return mupa_apply_pw_multi_aff_0D(mupa
, pma
);
9191 space1
= isl_space_range(isl_pw_multi_aff_get_space(pma
));
9192 res
= isl_multi_union_pw_aff_alloc(space1
);
9194 for (i
= 0; i
< n_out
; ++i
) {
9196 isl_union_pw_aff
*upa
;
9198 pa
= isl_pw_multi_aff_get_pw_aff(pma
, i
);
9199 upa
= isl_multi_union_pw_aff_apply_pw_aff(
9200 isl_multi_union_pw_aff_copy(mupa
), pa
);
9201 res
= isl_multi_union_pw_aff_set_union_pw_aff(res
, i
, upa
);
9204 isl_pw_multi_aff_free(pma
);
9205 isl_multi_union_pw_aff_free(mupa
);
9208 isl_multi_union_pw_aff_free(mupa
);
9209 isl_pw_multi_aff_free(pma
);
9213 /* Replace the explicit domain of "mupa" by its preimage under "upma".
9214 * If the explicit domain only keeps track of constraints on the parameters,
9215 * then only update those constraints.
9217 static __isl_give isl_multi_union_pw_aff
*preimage_explicit_domain(
9218 __isl_take isl_multi_union_pw_aff
*mupa
,
9219 __isl_keep isl_union_pw_multi_aff
*upma
)
9223 if (isl_multi_union_pw_aff_check_has_explicit_domain(mupa
) < 0)
9224 return isl_multi_union_pw_aff_free(mupa
);
9226 mupa
= isl_multi_union_pw_aff_cow(mupa
);
9230 is_params
= isl_union_set_is_params(mupa
->u
.dom
);
9232 return isl_multi_union_pw_aff_free(mupa
);
9234 upma
= isl_union_pw_multi_aff_copy(upma
);
9236 mupa
->u
.dom
= isl_union_set_intersect_params(mupa
->u
.dom
,
9237 isl_union_set_params(isl_union_pw_multi_aff_domain(upma
)));
9239 mupa
->u
.dom
= isl_union_set_preimage_union_pw_multi_aff(
9242 return isl_multi_union_pw_aff_free(mupa
);
9246 /* Compute the pullback of "mupa" by the function represented by "upma".
9247 * In other words, plug in "upma" in "mupa". The result contains
9248 * expressions defined over the domain space of "upma".
9250 * Run over all elements of "mupa" and plug in "upma" in each of them.
9252 * If "mupa" has an explicit domain, then it is this domain
9253 * that needs to undergo a pullback instead, i.e., a preimage.
9255 __isl_give isl_multi_union_pw_aff
*
9256 isl_multi_union_pw_aff_pullback_union_pw_multi_aff(
9257 __isl_take isl_multi_union_pw_aff
*mupa
,
9258 __isl_take isl_union_pw_multi_aff
*upma
)
9263 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
9264 isl_union_pw_multi_aff_get_space(upma
));
9265 upma
= isl_union_pw_multi_aff_align_params(upma
,
9266 isl_multi_union_pw_aff_get_space(mupa
));
9267 mupa
= isl_multi_union_pw_aff_cow(mupa
);
9268 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
9272 for (i
= 0; i
< n
; ++i
) {
9273 isl_union_pw_aff
*upa
;
9275 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
9276 upa
= isl_union_pw_aff_pullback_union_pw_multi_aff(upa
,
9277 isl_union_pw_multi_aff_copy(upma
));
9278 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
9281 if (isl_multi_union_pw_aff_has_explicit_domain(mupa
))
9282 mupa
= preimage_explicit_domain(mupa
, upma
);
9284 isl_union_pw_multi_aff_free(upma
);
9287 isl_multi_union_pw_aff_free(mupa
);
9288 isl_union_pw_multi_aff_free(upma
);
9292 /* Extract the sequence of elements in "mupa" with domain space "space"
9293 * (ignoring parameters).
9295 * For the elements of "mupa" that are not defined on the specified space,
9296 * the corresponding element in the result is empty.
9298 __isl_give isl_multi_pw_aff
*isl_multi_union_pw_aff_extract_multi_pw_aff(
9299 __isl_keep isl_multi_union_pw_aff
*mupa
, __isl_take isl_space
*space
)
9303 isl_space
*space_mpa
;
9304 isl_multi_pw_aff
*mpa
;
9306 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
9307 if (n
< 0 || !space
)
9310 space_mpa
= isl_multi_union_pw_aff_get_space(mupa
);
9311 space
= isl_space_replace_params(space
, space_mpa
);
9312 space_mpa
= isl_space_map_from_domain_and_range(isl_space_copy(space
),
9314 mpa
= isl_multi_pw_aff_alloc(space_mpa
);
9316 space
= isl_space_from_domain(space
);
9317 space
= isl_space_add_dims(space
, isl_dim_out
, 1);
9318 for (i
= 0; i
< n
; ++i
) {
9319 isl_union_pw_aff
*upa
;
9322 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
9323 pa
= isl_union_pw_aff_extract_pw_aff(upa
,
9324 isl_space_copy(space
));
9325 mpa
= isl_multi_pw_aff_set_pw_aff(mpa
, i
, pa
);
9326 isl_union_pw_aff_free(upa
);
9329 isl_space_free(space
);
9332 isl_space_free(space
);
9336 /* Evaluate the affine function "aff" in the void point "pnt".
9337 * In particular, return the value NaN.
9339 static __isl_give isl_val
*eval_void(__isl_take isl_aff
*aff
,
9340 __isl_take isl_point
*pnt
)
9344 ctx
= isl_point_get_ctx(pnt
);
9346 isl_point_free(pnt
);
9347 return isl_val_nan(ctx
);
9350 /* Evaluate the affine expression "aff"
9351 * in the coordinates (with denominator) "pnt".
9353 static __isl_give isl_val
*eval(__isl_keep isl_vec
*aff
,
9354 __isl_keep isl_vec
*pnt
)
9363 ctx
= isl_vec_get_ctx(aff
);
9366 isl_seq_inner_product(aff
->el
+ 1, pnt
->el
, pnt
->size
, &n
);
9367 isl_int_mul(d
, aff
->el
[0], pnt
->el
[0]);
9368 v
= isl_val_rat_from_isl_int(ctx
, n
, d
);
9369 v
= isl_val_normalize(v
);
9376 /* Check that the domain space of "aff" is equal to "space".
9378 static isl_stat
isl_aff_check_has_domain_space(__isl_keep isl_aff
*aff
,
9379 __isl_keep isl_space
*space
)
9383 ok
= isl_space_is_equal(isl_aff_peek_domain_space(aff
), space
);
9385 return isl_stat_error
;
9387 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
9388 "incompatible spaces", return isl_stat_error
);
9392 /* Evaluate the affine function "aff" in "pnt".
9394 __isl_give isl_val
*isl_aff_eval(__isl_take isl_aff
*aff
,
9395 __isl_take isl_point
*pnt
)
9399 isl_local_space
*ls
;
9401 if (isl_aff_check_has_domain_space(aff
, isl_point_peek_space(pnt
)) < 0)
9403 is_void
= isl_point_is_void(pnt
);
9407 return eval_void(aff
, pnt
);
9409 ls
= isl_aff_get_domain_local_space(aff
);
9410 pnt
= isl_local_space_lift_point(ls
, pnt
);
9412 v
= eval(aff
->v
, isl_point_peek_vec(pnt
));
9415 isl_point_free(pnt
);
9420 isl_point_free(pnt
);