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>
27 #include <isl_id_private.h>
28 #include <isl/constraint.h>
31 #include <isl_val_private.h>
32 #include <isl_point_private.h>
33 #include <isl_config.h>
38 #include <isl_list_templ.c>
41 #define EL_BASE pw_aff
43 #include <isl_list_templ.c>
46 #define EL_BASE pw_multi_aff
48 #include <isl_list_templ.c>
51 #define EL_BASE union_pw_aff
53 #include <isl_list_templ.c>
56 #define EL_BASE union_pw_multi_aff
58 #include <isl_list_templ.c>
60 __isl_give isl_aff
*isl_aff_alloc_vec(__isl_take isl_local_space
*ls
,
61 __isl_take isl_vec
*v
)
68 aff
= isl_calloc_type(v
->ctx
, struct isl_aff
);
78 isl_local_space_free(ls
);
83 __isl_give isl_aff
*isl_aff_alloc(__isl_take isl_local_space
*ls
)
92 ctx
= isl_local_space_get_ctx(ls
);
93 if (!isl_local_space_divs_known(ls
))
94 isl_die(ctx
, isl_error_invalid
, "local space has unknown divs",
96 if (!isl_local_space_is_set(ls
))
97 isl_die(ctx
, isl_error_invalid
,
98 "domain of affine expression should be a set",
101 total
= isl_local_space_dim(ls
, isl_dim_all
);
104 v
= isl_vec_alloc(ctx
, 1 + 1 + total
);
105 return isl_aff_alloc_vec(ls
, v
);
107 isl_local_space_free(ls
);
111 __isl_give isl_aff
*isl_aff_zero_on_domain(__isl_take isl_local_space
*ls
)
115 aff
= isl_aff_alloc(ls
);
119 isl_int_set_si(aff
->v
->el
[0], 1);
120 isl_seq_clr(aff
->v
->el
+ 1, aff
->v
->size
- 1);
125 /* Return a piecewise affine expression defined on the specified domain
126 * that is equal to zero.
128 __isl_give isl_pw_aff
*isl_pw_aff_zero_on_domain(__isl_take isl_local_space
*ls
)
130 return isl_pw_aff_from_aff(isl_aff_zero_on_domain(ls
));
133 /* Return an affine expression defined on the specified domain
134 * that represents NaN.
136 __isl_give isl_aff
*isl_aff_nan_on_domain(__isl_take isl_local_space
*ls
)
140 aff
= isl_aff_alloc(ls
);
144 isl_seq_clr(aff
->v
->el
, aff
->v
->size
);
149 /* Return a piecewise affine expression defined on the specified domain
150 * that represents NaN.
152 __isl_give isl_pw_aff
*isl_pw_aff_nan_on_domain(__isl_take isl_local_space
*ls
)
154 return isl_pw_aff_from_aff(isl_aff_nan_on_domain(ls
));
157 /* Return an affine expression that is equal to "val" on
158 * domain local space "ls".
160 __isl_give isl_aff
*isl_aff_val_on_domain(__isl_take isl_local_space
*ls
,
161 __isl_take isl_val
*val
)
167 if (!isl_val_is_rat(val
))
168 isl_die(isl_val_get_ctx(val
), isl_error_invalid
,
169 "expecting rational value", goto error
);
171 aff
= isl_aff_alloc(isl_local_space_copy(ls
));
175 isl_seq_clr(aff
->v
->el
+ 2, aff
->v
->size
- 2);
176 isl_int_set(aff
->v
->el
[1], val
->n
);
177 isl_int_set(aff
->v
->el
[0], val
->d
);
179 isl_local_space_free(ls
);
183 isl_local_space_free(ls
);
188 /* Return an affine expression that is equal to the specified dimension
191 __isl_give isl_aff
*isl_aff_var_on_domain(__isl_take isl_local_space
*ls
,
192 enum isl_dim_type type
, unsigned pos
)
200 space
= isl_local_space_get_space(ls
);
203 if (isl_space_is_map(space
))
204 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
205 "expecting (parameter) set space", goto error
);
206 if (isl_local_space_check_range(ls
, type
, pos
, 1) < 0)
209 isl_space_free(space
);
210 aff
= isl_aff_alloc(ls
);
214 pos
+= isl_local_space_offset(aff
->ls
, type
);
216 isl_int_set_si(aff
->v
->el
[0], 1);
217 isl_seq_clr(aff
->v
->el
+ 1, aff
->v
->size
- 1);
218 isl_int_set_si(aff
->v
->el
[1 + pos
], 1);
222 isl_local_space_free(ls
);
223 isl_space_free(space
);
227 /* Return a piecewise affine expression that is equal to
228 * the specified dimension in "ls".
230 __isl_give isl_pw_aff
*isl_pw_aff_var_on_domain(__isl_take isl_local_space
*ls
,
231 enum isl_dim_type type
, unsigned pos
)
233 return isl_pw_aff_from_aff(isl_aff_var_on_domain(ls
, type
, pos
));
236 /* Return an affine expression that is equal to the parameter
237 * in the domain space "space" with identifier "id".
239 __isl_give isl_aff
*isl_aff_param_on_domain_space_id(
240 __isl_take isl_space
*space
, __isl_take isl_id
*id
)
247 pos
= isl_space_find_dim_by_id(space
, isl_dim_param
, id
);
249 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
250 "parameter not found in space", goto error
);
252 ls
= isl_local_space_from_space(space
);
253 return isl_aff_var_on_domain(ls
, isl_dim_param
, pos
);
255 isl_space_free(space
);
260 __isl_give isl_aff
*isl_aff_copy(__isl_keep isl_aff
*aff
)
269 __isl_give isl_aff
*isl_aff_dup(__isl_keep isl_aff
*aff
)
274 return isl_aff_alloc_vec(isl_local_space_copy(aff
->ls
),
275 isl_vec_copy(aff
->v
));
278 __isl_give isl_aff
*isl_aff_cow(__isl_take isl_aff
*aff
)
286 return isl_aff_dup(aff
);
289 __isl_null isl_aff
*isl_aff_free(__isl_take isl_aff
*aff
)
297 isl_local_space_free(aff
->ls
);
298 isl_vec_free(aff
->v
);
305 isl_ctx
*isl_aff_get_ctx(__isl_keep isl_aff
*aff
)
307 return aff
? isl_local_space_get_ctx(aff
->ls
) : NULL
;
310 /* Return a hash value that digests "aff".
312 uint32_t isl_aff_get_hash(__isl_keep isl_aff
*aff
)
314 uint32_t hash
, ls_hash
, v_hash
;
319 hash
= isl_hash_init();
320 ls_hash
= isl_local_space_get_hash(aff
->ls
);
321 isl_hash_hash(hash
, ls_hash
);
322 v_hash
= isl_vec_get_hash(aff
->v
);
323 isl_hash_hash(hash
, v_hash
);
328 /* Externally, an isl_aff has a map space, but internally, the
329 * ls field corresponds to the domain of that space.
331 isl_size
isl_aff_dim(__isl_keep isl_aff
*aff
, enum isl_dim_type type
)
334 return isl_size_error
;
335 if (type
== isl_dim_out
)
337 if (type
== isl_dim_in
)
339 return isl_local_space_dim(aff
->ls
, type
);
342 /* Return the position of the dimension of the given type and name
344 * Return -1 if no such dimension can be found.
346 int isl_aff_find_dim_by_name(__isl_keep isl_aff
*aff
, enum isl_dim_type type
,
351 if (type
== isl_dim_out
)
353 if (type
== isl_dim_in
)
355 return isl_local_space_find_dim_by_name(aff
->ls
, type
, name
);
358 /* Return the domain space of "aff".
360 static __isl_keep isl_space
*isl_aff_peek_domain_space(__isl_keep isl_aff
*aff
)
362 return aff
? isl_local_space_peek_space(aff
->ls
) : NULL
;
365 __isl_give isl_space
*isl_aff_get_domain_space(__isl_keep isl_aff
*aff
)
367 return isl_space_copy(isl_aff_peek_domain_space(aff
));
370 __isl_give isl_space
*isl_aff_get_space(__isl_keep isl_aff
*aff
)
375 space
= isl_local_space_get_space(aff
->ls
);
376 space
= isl_space_from_domain(space
);
377 space
= isl_space_add_dims(space
, isl_dim_out
, 1);
381 __isl_give isl_local_space
*isl_aff_get_domain_local_space(
382 __isl_keep isl_aff
*aff
)
384 return aff
? isl_local_space_copy(aff
->ls
) : NULL
;
387 __isl_give isl_local_space
*isl_aff_get_local_space(__isl_keep isl_aff
*aff
)
392 ls
= isl_local_space_copy(aff
->ls
);
393 ls
= isl_local_space_from_domain(ls
);
394 ls
= isl_local_space_add_dims(ls
, isl_dim_out
, 1);
398 /* Return the local space of the domain of "aff".
399 * This may be either a copy or the local space itself
400 * if there is only one reference to "aff".
401 * This allows the local space to be modified inplace
402 * if both the expression and its local space have only a single reference.
403 * The caller is not allowed to modify "aff" between this call and
404 * a subsequent call to isl_aff_restore_domain_local_space.
405 * The only exception is that isl_aff_free can be called instead.
407 __isl_give isl_local_space
*isl_aff_take_domain_local_space(
408 __isl_keep isl_aff
*aff
)
415 return isl_aff_get_domain_local_space(aff
);
421 /* Set the local space of the domain of "aff" to "ls",
422 * where the local space of "aff" may be missing
423 * due to a preceding call to isl_aff_take_domain_local_space.
424 * However, in this case, "aff" only has a single reference and
425 * then the call to isl_aff_cow has no effect.
427 __isl_give isl_aff
*isl_aff_restore_domain_local_space(
428 __isl_keep isl_aff
*aff
, __isl_take isl_local_space
*ls
)
434 isl_local_space_free(ls
);
438 aff
= isl_aff_cow(aff
);
441 isl_local_space_free(aff
->ls
);
447 isl_local_space_free(ls
);
451 /* Externally, an isl_aff has a map space, but internally, the
452 * ls field corresponds to the domain of that space.
454 const char *isl_aff_get_dim_name(__isl_keep isl_aff
*aff
,
455 enum isl_dim_type type
, unsigned pos
)
459 if (type
== isl_dim_out
)
461 if (type
== isl_dim_in
)
463 return isl_local_space_get_dim_name(aff
->ls
, type
, pos
);
466 __isl_give isl_aff
*isl_aff_reset_domain_space(__isl_take isl_aff
*aff
,
467 __isl_take isl_space
*dim
)
469 aff
= isl_aff_cow(aff
);
473 aff
->ls
= isl_local_space_reset_space(aff
->ls
, dim
);
475 return isl_aff_free(aff
);
484 /* Reset the space of "aff". This function is called from isl_pw_templ.c
485 * and doesn't know if the space of an element object is represented
486 * directly or through its domain. It therefore passes along both.
488 __isl_give isl_aff
*isl_aff_reset_space_and_domain(__isl_take isl_aff
*aff
,
489 __isl_take isl_space
*space
, __isl_take isl_space
*domain
)
491 isl_space_free(space
);
492 return isl_aff_reset_domain_space(aff
, domain
);
495 /* Reorder the coefficients of the affine expression based
496 * on the given reordering.
497 * The reordering r is assumed to have been extended with the local
500 static __isl_give isl_vec
*vec_reorder(__isl_take isl_vec
*vec
,
501 __isl_take isl_reordering
*r
, int n_div
)
511 space
= isl_reordering_peek_space(r
);
512 dim
= isl_space_dim(space
, isl_dim_all
);
515 res
= isl_vec_alloc(vec
->ctx
, 2 + dim
+ n_div
);
518 isl_seq_cpy(res
->el
, vec
->el
, 2);
519 isl_seq_clr(res
->el
+ 2, res
->size
- 2);
520 for (i
= 0; i
< r
->len
; ++i
)
521 isl_int_set(res
->el
[2 + r
->pos
[i
]], vec
->el
[2 + i
]);
523 isl_reordering_free(r
);
528 isl_reordering_free(r
);
532 /* Reorder the dimensions of the domain of "aff" according
533 * to the given reordering.
535 __isl_give isl_aff
*isl_aff_realign_domain(__isl_take isl_aff
*aff
,
536 __isl_take isl_reordering
*r
)
538 aff
= isl_aff_cow(aff
);
542 r
= isl_reordering_extend(r
, aff
->ls
->div
->n_row
);
543 aff
->v
= vec_reorder(aff
->v
, isl_reordering_copy(r
),
544 aff
->ls
->div
->n_row
);
545 aff
->ls
= isl_local_space_realign(aff
->ls
, r
);
547 if (!aff
->v
|| !aff
->ls
)
548 return isl_aff_free(aff
);
553 isl_reordering_free(r
);
557 __isl_give isl_aff
*isl_aff_align_params(__isl_take isl_aff
*aff
,
558 __isl_take isl_space
*model
)
560 isl_bool equal_params
;
565 equal_params
= isl_space_has_equal_params(aff
->ls
->dim
, model
);
566 if (equal_params
< 0)
571 exp
= isl_parameter_alignment_reordering(aff
->ls
->dim
, model
);
572 exp
= isl_reordering_extend_space(exp
,
573 isl_aff_get_domain_space(aff
));
574 aff
= isl_aff_realign_domain(aff
, exp
);
577 isl_space_free(model
);
580 isl_space_free(model
);
585 /* Given an affine function "aff" defined over a parameter domain,
586 * convert it to a function defined over a domain corresponding
588 * Any parameters with identifiers in "domain" are reinterpreted
589 * as the corresponding domain dimensions.
591 __isl_give isl_aff
*isl_aff_unbind_params_insert_domain(
592 __isl_take isl_aff
*aff
, __isl_take isl_multi_id
*domain
)
598 space
= isl_aff_peek_domain_space(aff
);
599 is_params
= isl_space_is_params(space
);
601 domain
= isl_multi_id_free(domain
);
603 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
604 "expecting function with parameter domain",
605 domain
= isl_multi_id_free(domain
));
606 r
= isl_reordering_unbind_params_insert_domain(space
, domain
);
607 isl_multi_id_free(domain
);
609 return isl_aff_realign_domain(aff
, r
);
612 /* Is "aff" obviously equal to zero?
614 * If the denominator is zero, then "aff" is not equal to zero.
616 isl_bool
isl_aff_plain_is_zero(__isl_keep isl_aff
*aff
)
621 return isl_bool_error
;
623 if (isl_int_is_zero(aff
->v
->el
[0]))
624 return isl_bool_false
;
625 pos
= isl_seq_first_non_zero(aff
->v
->el
+ 1, aff
->v
->size
- 1);
626 return isl_bool_ok(pos
< 0);
629 /* Does "aff" represent NaN?
631 isl_bool
isl_aff_is_nan(__isl_keep isl_aff
*aff
)
634 return isl_bool_error
;
636 return isl_bool_ok(isl_seq_first_non_zero(aff
->v
->el
, 2) < 0);
639 /* Are "aff1" and "aff2" obviously equal?
641 * NaN is not equal to anything, not even to another NaN.
643 isl_bool
isl_aff_plain_is_equal(__isl_keep isl_aff
*aff1
,
644 __isl_keep isl_aff
*aff2
)
649 return isl_bool_error
;
651 if (isl_aff_is_nan(aff1
) || isl_aff_is_nan(aff2
))
652 return isl_bool_false
;
654 equal
= isl_local_space_is_equal(aff1
->ls
, aff2
->ls
);
655 if (equal
< 0 || !equal
)
658 return isl_vec_is_equal(aff1
->v
, aff2
->v
);
661 /* Return the common denominator of "aff" in "v".
663 * We cannot return anything meaningful in case of a NaN.
665 isl_stat
isl_aff_get_denominator(__isl_keep isl_aff
*aff
, isl_int
*v
)
668 return isl_stat_error
;
669 if (isl_aff_is_nan(aff
))
670 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
671 "cannot get denominator of NaN", return isl_stat_error
);
672 isl_int_set(*v
, aff
->v
->el
[0]);
676 /* Return the common denominator of "aff".
678 __isl_give isl_val
*isl_aff_get_denominator_val(__isl_keep isl_aff
*aff
)
685 ctx
= isl_aff_get_ctx(aff
);
686 if (isl_aff_is_nan(aff
))
687 return isl_val_nan(ctx
);
688 return isl_val_int_from_isl_int(ctx
, aff
->v
->el
[0]);
691 /* Return the constant term of "aff".
693 __isl_give isl_val
*isl_aff_get_constant_val(__isl_keep isl_aff
*aff
)
701 ctx
= isl_aff_get_ctx(aff
);
702 if (isl_aff_is_nan(aff
))
703 return isl_val_nan(ctx
);
704 v
= isl_val_rat_from_isl_int(ctx
, aff
->v
->el
[1], aff
->v
->el
[0]);
705 return isl_val_normalize(v
);
708 /* Return the coefficient of the variable of type "type" at position "pos"
711 __isl_give isl_val
*isl_aff_get_coefficient_val(__isl_keep isl_aff
*aff
,
712 enum isl_dim_type type
, int pos
)
720 ctx
= isl_aff_get_ctx(aff
);
721 if (type
== isl_dim_out
)
722 isl_die(ctx
, isl_error_invalid
,
723 "output/set dimension does not have a coefficient",
725 if (type
== isl_dim_in
)
728 if (isl_local_space_check_range(aff
->ls
, type
, pos
, 1) < 0)
731 if (isl_aff_is_nan(aff
))
732 return isl_val_nan(ctx
);
733 pos
+= isl_local_space_offset(aff
->ls
, type
);
734 v
= isl_val_rat_from_isl_int(ctx
, aff
->v
->el
[1 + pos
], aff
->v
->el
[0]);
735 return isl_val_normalize(v
);
738 /* Return the sign of the coefficient of the variable of type "type"
739 * at position "pos" of "aff".
741 int isl_aff_coefficient_sgn(__isl_keep isl_aff
*aff
, enum isl_dim_type type
,
749 ctx
= isl_aff_get_ctx(aff
);
750 if (type
== isl_dim_out
)
751 isl_die(ctx
, isl_error_invalid
,
752 "output/set dimension does not have a coefficient",
754 if (type
== isl_dim_in
)
757 if (isl_local_space_check_range(aff
->ls
, type
, pos
, 1) < 0)
760 pos
+= isl_local_space_offset(aff
->ls
, type
);
761 return isl_int_sgn(aff
->v
->el
[1 + pos
]);
764 /* Replace the numerator of the constant term of "aff" by "v".
766 * A NaN is unaffected by this operation.
768 __isl_give isl_aff
*isl_aff_set_constant(__isl_take isl_aff
*aff
, isl_int v
)
772 if (isl_aff_is_nan(aff
))
774 aff
= isl_aff_cow(aff
);
778 aff
->v
= isl_vec_cow(aff
->v
);
780 return isl_aff_free(aff
);
782 isl_int_set(aff
->v
->el
[1], v
);
787 /* Replace the constant term of "aff" by "v".
789 * A NaN is unaffected by this operation.
791 __isl_give isl_aff
*isl_aff_set_constant_val(__isl_take isl_aff
*aff
,
792 __isl_take isl_val
*v
)
797 if (isl_aff_is_nan(aff
)) {
802 if (!isl_val_is_rat(v
))
803 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
804 "expecting rational value", goto error
);
806 if (isl_int_eq(aff
->v
->el
[1], v
->n
) &&
807 isl_int_eq(aff
->v
->el
[0], v
->d
)) {
812 aff
= isl_aff_cow(aff
);
815 aff
->v
= isl_vec_cow(aff
->v
);
819 if (isl_int_eq(aff
->v
->el
[0], v
->d
)) {
820 isl_int_set(aff
->v
->el
[1], v
->n
);
821 } else if (isl_int_is_one(v
->d
)) {
822 isl_int_mul(aff
->v
->el
[1], aff
->v
->el
[0], v
->n
);
824 isl_seq_scale(aff
->v
->el
+ 1,
825 aff
->v
->el
+ 1, v
->d
, aff
->v
->size
- 1);
826 isl_int_mul(aff
->v
->el
[1], aff
->v
->el
[0], v
->n
);
827 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], v
->d
);
828 aff
->v
= isl_vec_normalize(aff
->v
);
841 /* Add "v" to the constant term of "aff".
843 * A NaN is unaffected by this operation.
845 __isl_give isl_aff
*isl_aff_add_constant(__isl_take isl_aff
*aff
, isl_int v
)
847 if (isl_int_is_zero(v
))
852 if (isl_aff_is_nan(aff
))
854 aff
= isl_aff_cow(aff
);
858 aff
->v
= isl_vec_cow(aff
->v
);
860 return isl_aff_free(aff
);
862 isl_int_addmul(aff
->v
->el
[1], aff
->v
->el
[0], v
);
867 /* Add "v" to the constant term of "aff".
869 * A NaN is unaffected by this operation.
871 __isl_give isl_aff
*isl_aff_add_constant_val(__isl_take isl_aff
*aff
,
872 __isl_take isl_val
*v
)
877 if (isl_aff_is_nan(aff
) || isl_val_is_zero(v
)) {
882 if (!isl_val_is_rat(v
))
883 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
884 "expecting rational value", goto error
);
886 aff
= isl_aff_cow(aff
);
890 aff
->v
= isl_vec_cow(aff
->v
);
894 if (isl_int_is_one(v
->d
)) {
895 isl_int_addmul(aff
->v
->el
[1], aff
->v
->el
[0], v
->n
);
896 } else if (isl_int_eq(aff
->v
->el
[0], v
->d
)) {
897 isl_int_add(aff
->v
->el
[1], aff
->v
->el
[1], v
->n
);
898 aff
->v
= isl_vec_normalize(aff
->v
);
902 isl_seq_scale(aff
->v
->el
+ 1,
903 aff
->v
->el
+ 1, v
->d
, aff
->v
->size
- 1);
904 isl_int_addmul(aff
->v
->el
[1], aff
->v
->el
[0], v
->n
);
905 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], v
->d
);
906 aff
->v
= isl_vec_normalize(aff
->v
);
919 __isl_give isl_aff
*isl_aff_add_constant_si(__isl_take isl_aff
*aff
, int v
)
924 isl_int_set_si(t
, v
);
925 aff
= isl_aff_add_constant(aff
, t
);
931 /* Add "v" to the numerator of the constant term of "aff".
933 * A NaN is unaffected by this operation.
935 __isl_give isl_aff
*isl_aff_add_constant_num(__isl_take isl_aff
*aff
, isl_int v
)
937 if (isl_int_is_zero(v
))
942 if (isl_aff_is_nan(aff
))
944 aff
= isl_aff_cow(aff
);
948 aff
->v
= isl_vec_cow(aff
->v
);
950 return isl_aff_free(aff
);
952 isl_int_add(aff
->v
->el
[1], aff
->v
->el
[1], v
);
957 /* Add "v" to the numerator of the constant term of "aff".
959 * A NaN is unaffected by this operation.
961 __isl_give isl_aff
*isl_aff_add_constant_num_si(__isl_take isl_aff
*aff
, int v
)
969 isl_int_set_si(t
, v
);
970 aff
= isl_aff_add_constant_num(aff
, t
);
976 /* Replace the numerator of the constant term of "aff" by "v".
978 * A NaN is unaffected by this operation.
980 __isl_give isl_aff
*isl_aff_set_constant_si(__isl_take isl_aff
*aff
, int v
)
984 if (isl_aff_is_nan(aff
))
986 aff
= isl_aff_cow(aff
);
990 aff
->v
= isl_vec_cow(aff
->v
);
992 return isl_aff_free(aff
);
994 isl_int_set_si(aff
->v
->el
[1], v
);
999 /* Replace the numerator of the coefficient of the variable of type "type"
1000 * at position "pos" of "aff" by "v".
1002 * A NaN is unaffected by this operation.
1004 __isl_give isl_aff
*isl_aff_set_coefficient(__isl_take isl_aff
*aff
,
1005 enum isl_dim_type type
, int pos
, isl_int v
)
1010 if (type
== isl_dim_out
)
1011 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1012 "output/set dimension does not have a coefficient",
1013 return isl_aff_free(aff
));
1014 if (type
== isl_dim_in
)
1017 if (isl_local_space_check_range(aff
->ls
, type
, pos
, 1) < 0)
1018 return isl_aff_free(aff
);
1020 if (isl_aff_is_nan(aff
))
1022 aff
= isl_aff_cow(aff
);
1026 aff
->v
= isl_vec_cow(aff
->v
);
1028 return isl_aff_free(aff
);
1030 pos
+= isl_local_space_offset(aff
->ls
, type
);
1031 isl_int_set(aff
->v
->el
[1 + pos
], v
);
1036 /* Replace the numerator of the coefficient of the variable of type "type"
1037 * at position "pos" of "aff" by "v".
1039 * A NaN is unaffected by this operation.
1041 __isl_give isl_aff
*isl_aff_set_coefficient_si(__isl_take isl_aff
*aff
,
1042 enum isl_dim_type type
, int pos
, int v
)
1047 if (type
== isl_dim_out
)
1048 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1049 "output/set dimension does not have a coefficient",
1050 return isl_aff_free(aff
));
1051 if (type
== isl_dim_in
)
1054 if (isl_local_space_check_range(aff
->ls
, type
, pos
, 1) < 0)
1055 return isl_aff_free(aff
);
1057 if (isl_aff_is_nan(aff
))
1059 pos
+= isl_local_space_offset(aff
->ls
, type
);
1060 if (isl_int_cmp_si(aff
->v
->el
[1 + pos
], v
) == 0)
1063 aff
= isl_aff_cow(aff
);
1067 aff
->v
= isl_vec_cow(aff
->v
);
1069 return isl_aff_free(aff
);
1071 isl_int_set_si(aff
->v
->el
[1 + pos
], v
);
1076 /* Replace the coefficient of the variable of type "type" at position "pos"
1079 * A NaN is unaffected by this operation.
1081 __isl_give isl_aff
*isl_aff_set_coefficient_val(__isl_take isl_aff
*aff
,
1082 enum isl_dim_type type
, int pos
, __isl_take isl_val
*v
)
1087 if (type
== isl_dim_out
)
1088 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1089 "output/set dimension does not have a coefficient",
1091 if (type
== isl_dim_in
)
1094 if (isl_local_space_check_range(aff
->ls
, type
, pos
, 1) < 0)
1095 return isl_aff_free(aff
);
1097 if (isl_aff_is_nan(aff
)) {
1101 if (!isl_val_is_rat(v
))
1102 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1103 "expecting rational value", goto error
);
1105 pos
+= isl_local_space_offset(aff
->ls
, type
);
1106 if (isl_int_eq(aff
->v
->el
[1 + pos
], v
->n
) &&
1107 isl_int_eq(aff
->v
->el
[0], v
->d
)) {
1112 aff
= isl_aff_cow(aff
);
1115 aff
->v
= isl_vec_cow(aff
->v
);
1119 if (isl_int_eq(aff
->v
->el
[0], v
->d
)) {
1120 isl_int_set(aff
->v
->el
[1 + pos
], v
->n
);
1121 } else if (isl_int_is_one(v
->d
)) {
1122 isl_int_mul(aff
->v
->el
[1 + pos
], aff
->v
->el
[0], v
->n
);
1124 isl_seq_scale(aff
->v
->el
+ 1,
1125 aff
->v
->el
+ 1, v
->d
, aff
->v
->size
- 1);
1126 isl_int_mul(aff
->v
->el
[1 + pos
], aff
->v
->el
[0], v
->n
);
1127 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], v
->d
);
1128 aff
->v
= isl_vec_normalize(aff
->v
);
1141 /* Add "v" to the coefficient of the variable of type "type"
1142 * at position "pos" of "aff".
1144 * A NaN is unaffected by this operation.
1146 __isl_give isl_aff
*isl_aff_add_coefficient(__isl_take isl_aff
*aff
,
1147 enum isl_dim_type type
, int pos
, isl_int v
)
1152 if (type
== isl_dim_out
)
1153 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1154 "output/set dimension does not have a coefficient",
1155 return isl_aff_free(aff
));
1156 if (type
== isl_dim_in
)
1159 if (isl_local_space_check_range(aff
->ls
, type
, pos
, 1) < 0)
1160 return isl_aff_free(aff
);
1162 if (isl_aff_is_nan(aff
))
1164 aff
= isl_aff_cow(aff
);
1168 aff
->v
= isl_vec_cow(aff
->v
);
1170 return isl_aff_free(aff
);
1172 pos
+= isl_local_space_offset(aff
->ls
, type
);
1173 isl_int_addmul(aff
->v
->el
[1 + pos
], aff
->v
->el
[0], v
);
1178 /* Add "v" to the coefficient of the variable of type "type"
1179 * at position "pos" of "aff".
1181 * A NaN is unaffected by this operation.
1183 __isl_give isl_aff
*isl_aff_add_coefficient_val(__isl_take isl_aff
*aff
,
1184 enum isl_dim_type type
, int pos
, __isl_take isl_val
*v
)
1189 if (isl_val_is_zero(v
)) {
1194 if (type
== isl_dim_out
)
1195 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1196 "output/set dimension does not have a coefficient",
1198 if (type
== isl_dim_in
)
1201 if (isl_local_space_check_range(aff
->ls
, type
, pos
, 1) < 0)
1204 if (isl_aff_is_nan(aff
)) {
1208 if (!isl_val_is_rat(v
))
1209 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1210 "expecting rational value", goto error
);
1212 aff
= isl_aff_cow(aff
);
1216 aff
->v
= isl_vec_cow(aff
->v
);
1220 pos
+= isl_local_space_offset(aff
->ls
, type
);
1221 if (isl_int_is_one(v
->d
)) {
1222 isl_int_addmul(aff
->v
->el
[1 + pos
], aff
->v
->el
[0], v
->n
);
1223 } else if (isl_int_eq(aff
->v
->el
[0], v
->d
)) {
1224 isl_int_add(aff
->v
->el
[1 + pos
], aff
->v
->el
[1 + pos
], v
->n
);
1225 aff
->v
= isl_vec_normalize(aff
->v
);
1229 isl_seq_scale(aff
->v
->el
+ 1,
1230 aff
->v
->el
+ 1, v
->d
, aff
->v
->size
- 1);
1231 isl_int_addmul(aff
->v
->el
[1 + pos
], aff
->v
->el
[0], v
->n
);
1232 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], v
->d
);
1233 aff
->v
= isl_vec_normalize(aff
->v
);
1246 __isl_give isl_aff
*isl_aff_add_coefficient_si(__isl_take isl_aff
*aff
,
1247 enum isl_dim_type type
, int pos
, int v
)
1252 isl_int_set_si(t
, v
);
1253 aff
= isl_aff_add_coefficient(aff
, type
, pos
, t
);
1259 __isl_give isl_aff
*isl_aff_get_div(__isl_keep isl_aff
*aff
, int pos
)
1264 return isl_local_space_get_div(aff
->ls
, pos
);
1267 /* Return the negation of "aff".
1269 * As a special case, -NaN = NaN.
1271 __isl_give isl_aff
*isl_aff_neg(__isl_take isl_aff
*aff
)
1275 if (isl_aff_is_nan(aff
))
1277 aff
= isl_aff_cow(aff
);
1280 aff
->v
= isl_vec_cow(aff
->v
);
1282 return isl_aff_free(aff
);
1284 isl_seq_neg(aff
->v
->el
+ 1, aff
->v
->el
+ 1, aff
->v
->size
- 1);
1289 /* Remove divs from the local space that do not appear in the affine
1291 * We currently only remove divs at the end.
1292 * Some intermediate divs may also not appear directly in the affine
1293 * expression, but we would also need to check that no other divs are
1294 * defined in terms of them.
1296 __isl_give isl_aff
*isl_aff_remove_unused_divs(__isl_take isl_aff
*aff
)
1305 n
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
1307 return isl_aff_free(aff
);
1308 off
= isl_local_space_offset(aff
->ls
, isl_dim_div
);
1310 pos
= isl_seq_last_non_zero(aff
->v
->el
+ 1 + off
, n
) + 1;
1314 aff
= isl_aff_cow(aff
);
1318 aff
->ls
= isl_local_space_drop_dims(aff
->ls
, isl_dim_div
, pos
, n
- pos
);
1319 aff
->v
= isl_vec_drop_els(aff
->v
, 1 + off
+ pos
, n
- pos
);
1320 if (!aff
->ls
|| !aff
->v
)
1321 return isl_aff_free(aff
);
1326 /* Look for any divs in the aff->ls with a denominator equal to one
1327 * and plug them into the affine expression and any subsequent divs
1328 * that may reference the div.
1330 static __isl_give isl_aff
*plug_in_integral_divs(__isl_take isl_aff
*aff
)
1337 isl_local_space
*ls
;
1343 n
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
1345 return isl_aff_free(aff
);
1347 for (i
= 0; i
< n
; ++i
) {
1348 if (!isl_int_is_one(aff
->ls
->div
->row
[i
][0]))
1350 ls
= isl_local_space_copy(aff
->ls
);
1351 ls
= isl_local_space_substitute_seq(ls
, isl_dim_div
, i
,
1352 aff
->ls
->div
->row
[i
], len
, i
+ 1, n
- (i
+ 1));
1353 vec
= isl_vec_copy(aff
->v
);
1354 vec
= isl_vec_cow(vec
);
1360 pos
= isl_local_space_offset(aff
->ls
, isl_dim_div
) + i
;
1361 isl_seq_substitute(vec
->el
, pos
, aff
->ls
->div
->row
[i
],
1366 isl_vec_free(aff
->v
);
1368 isl_local_space_free(aff
->ls
);
1375 isl_local_space_free(ls
);
1376 return isl_aff_free(aff
);
1379 /* Look for any divs j that appear with a unit coefficient inside
1380 * the definitions of other divs i and plug them into the definitions
1383 * In particular, an expression of the form
1385 * floor((f(..) + floor(g(..)/n))/m)
1389 * floor((n * f(..) + g(..))/(n * m))
1391 * This simplification is correct because we can move the expression
1392 * f(..) into the inner floor in the original expression to obtain
1394 * floor(floor((n * f(..) + g(..))/n)/m)
1396 * from which we can derive the simplified expression.
1398 static __isl_give isl_aff
*plug_in_unit_divs(__isl_take isl_aff
*aff
)
1407 n
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
1409 return isl_aff_free(aff
);
1410 off
= isl_local_space_offset(aff
->ls
, isl_dim_div
);
1411 for (i
= 1; i
< n
; ++i
) {
1412 for (j
= 0; j
< i
; ++j
) {
1413 if (!isl_int_is_one(aff
->ls
->div
->row
[i
][1 + off
+ j
]))
1415 aff
->ls
= isl_local_space_substitute_seq(aff
->ls
,
1416 isl_dim_div
, j
, aff
->ls
->div
->row
[j
],
1417 aff
->v
->size
, i
, 1);
1419 return isl_aff_free(aff
);
1426 /* Swap divs "a" and "b" in "aff", which is assumed to be non-NULL.
1428 * Even though this function is only called on isl_affs with a single
1429 * reference, we are careful to only change aff->v and aff->ls together.
1431 static __isl_give isl_aff
*swap_div(__isl_take isl_aff
*aff
, int a
, int b
)
1433 unsigned off
= isl_local_space_offset(aff
->ls
, isl_dim_div
);
1434 isl_local_space
*ls
;
1437 ls
= isl_local_space_copy(aff
->ls
);
1438 ls
= isl_local_space_swap_div(ls
, a
, b
);
1439 v
= isl_vec_copy(aff
->v
);
1444 isl_int_swap(v
->el
[1 + off
+ a
], v
->el
[1 + off
+ b
]);
1445 isl_vec_free(aff
->v
);
1447 isl_local_space_free(aff
->ls
);
1453 isl_local_space_free(ls
);
1454 return isl_aff_free(aff
);
1457 /* Merge divs "a" and "b" in "aff", which is assumed to be non-NULL.
1459 * We currently do not actually remove div "b", but simply add its
1460 * coefficient to that of "a" and then zero it out.
1462 static __isl_give isl_aff
*merge_divs(__isl_take isl_aff
*aff
, int a
, int b
)
1464 unsigned off
= isl_local_space_offset(aff
->ls
, isl_dim_div
);
1466 if (isl_int_is_zero(aff
->v
->el
[1 + off
+ b
]))
1469 aff
->v
= isl_vec_cow(aff
->v
);
1471 return isl_aff_free(aff
);
1473 isl_int_add(aff
->v
->el
[1 + off
+ a
],
1474 aff
->v
->el
[1 + off
+ a
], aff
->v
->el
[1 + off
+ b
]);
1475 isl_int_set_si(aff
->v
->el
[1 + off
+ b
], 0);
1480 /* Sort the divs in the local space of "aff" according to
1481 * the comparison function "cmp_row" in isl_local_space.c,
1482 * combining the coefficients of identical divs.
1484 * Reordering divs does not change the semantics of "aff",
1485 * so there is no need to call isl_aff_cow.
1486 * Moreover, this function is currently only called on isl_affs
1487 * with a single reference.
1489 static __isl_give isl_aff
*sort_divs(__isl_take isl_aff
*aff
)
1494 n
= isl_aff_dim(aff
, isl_dim_div
);
1496 return isl_aff_free(aff
);
1497 for (i
= 1; i
< n
; ++i
) {
1498 for (j
= i
- 1; j
>= 0; --j
) {
1499 int cmp
= isl_mat_cmp_div(aff
->ls
->div
, j
, j
+ 1);
1503 aff
= merge_divs(aff
, j
, j
+ 1);
1505 aff
= swap_div(aff
, j
, j
+ 1);
1514 /* Normalize the representation of "aff".
1516 * This function should only be called of "new" isl_affs, i.e.,
1517 * with only a single reference. We therefore do not need to
1518 * worry about affecting other instances.
1520 __isl_give isl_aff
*isl_aff_normalize(__isl_take isl_aff
*aff
)
1524 aff
->v
= isl_vec_normalize(aff
->v
);
1526 return isl_aff_free(aff
);
1527 aff
= plug_in_integral_divs(aff
);
1528 aff
= plug_in_unit_divs(aff
);
1529 aff
= sort_divs(aff
);
1530 aff
= isl_aff_remove_unused_divs(aff
);
1534 /* Given f, return floor(f).
1535 * If f is an integer expression, then just return f.
1536 * If f is a constant, then return the constant floor(f).
1537 * Otherwise, if f = g/m, write g = q m + r,
1538 * create a new div d = [r/m] and return the expression q + d.
1539 * The coefficients in r are taken to lie between -m/2 and m/2.
1541 * reduce_div_coefficients performs the same normalization.
1543 * As a special case, floor(NaN) = NaN.
1545 __isl_give isl_aff
*isl_aff_floor(__isl_take isl_aff
*aff
)
1555 if (isl_aff_is_nan(aff
))
1557 if (isl_int_is_one(aff
->v
->el
[0]))
1560 aff
= isl_aff_cow(aff
);
1564 aff
->v
= isl_vec_cow(aff
->v
);
1566 return isl_aff_free(aff
);
1568 if (isl_aff_is_cst(aff
)) {
1569 isl_int_fdiv_q(aff
->v
->el
[1], aff
->v
->el
[1], aff
->v
->el
[0]);
1570 isl_int_set_si(aff
->v
->el
[0], 1);
1574 div
= isl_vec_copy(aff
->v
);
1575 div
= isl_vec_cow(div
);
1577 return isl_aff_free(aff
);
1579 ctx
= isl_aff_get_ctx(aff
);
1580 isl_int_fdiv_q(aff
->v
->el
[0], aff
->v
->el
[0], ctx
->two
);
1581 for (i
= 1; i
< aff
->v
->size
; ++i
) {
1582 isl_int_fdiv_r(div
->el
[i
], div
->el
[i
], div
->el
[0]);
1583 isl_int_fdiv_q(aff
->v
->el
[i
], aff
->v
->el
[i
], div
->el
[0]);
1584 if (isl_int_gt(div
->el
[i
], aff
->v
->el
[0])) {
1585 isl_int_sub(div
->el
[i
], div
->el
[i
], div
->el
[0]);
1586 isl_int_add_ui(aff
->v
->el
[i
], aff
->v
->el
[i
], 1);
1590 aff
->ls
= isl_local_space_add_div(aff
->ls
, div
);
1592 return isl_aff_free(aff
);
1594 size
= aff
->v
->size
;
1595 aff
->v
= isl_vec_extend(aff
->v
, size
+ 1);
1597 return isl_aff_free(aff
);
1598 isl_int_set_si(aff
->v
->el
[0], 1);
1599 isl_int_set_si(aff
->v
->el
[size
], 1);
1601 aff
= isl_aff_normalize(aff
);
1608 * aff mod m = aff - m * floor(aff/m)
1610 * with m an integer value.
1612 __isl_give isl_aff
*isl_aff_mod_val(__isl_take isl_aff
*aff
,
1613 __isl_take isl_val
*m
)
1620 if (!isl_val_is_int(m
))
1621 isl_die(isl_val_get_ctx(m
), isl_error_invalid
,
1622 "expecting integer modulo", goto error
);
1624 res
= isl_aff_copy(aff
);
1625 aff
= isl_aff_scale_down_val(aff
, isl_val_copy(m
));
1626 aff
= isl_aff_floor(aff
);
1627 aff
= isl_aff_scale_val(aff
, m
);
1628 res
= isl_aff_sub(res
, aff
);
1639 * pwaff mod m = pwaff - m * floor(pwaff/m)
1641 __isl_give isl_pw_aff
*isl_pw_aff_mod(__isl_take isl_pw_aff
*pwaff
, isl_int m
)
1645 res
= isl_pw_aff_copy(pwaff
);
1646 pwaff
= isl_pw_aff_scale_down(pwaff
, m
);
1647 pwaff
= isl_pw_aff_floor(pwaff
);
1648 pwaff
= isl_pw_aff_scale(pwaff
, m
);
1649 res
= isl_pw_aff_sub(res
, pwaff
);
1656 * pa mod m = pa - m * floor(pa/m)
1658 * with m an integer value.
1660 __isl_give isl_pw_aff
*isl_pw_aff_mod_val(__isl_take isl_pw_aff
*pa
,
1661 __isl_take isl_val
*m
)
1665 if (!isl_val_is_int(m
))
1666 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
1667 "expecting integer modulo", goto error
);
1668 pa
= isl_pw_aff_mod(pa
, m
->n
);
1672 isl_pw_aff_free(pa
);
1677 /* Given f, return ceil(f).
1678 * If f is an integer expression, then just return f.
1679 * Otherwise, let f be the expression
1685 * floor((e + m - 1)/m)
1687 * As a special case, ceil(NaN) = NaN.
1689 __isl_give isl_aff
*isl_aff_ceil(__isl_take isl_aff
*aff
)
1694 if (isl_aff_is_nan(aff
))
1696 if (isl_int_is_one(aff
->v
->el
[0]))
1699 aff
= isl_aff_cow(aff
);
1702 aff
->v
= isl_vec_cow(aff
->v
);
1704 return isl_aff_free(aff
);
1706 isl_int_add(aff
->v
->el
[1], aff
->v
->el
[1], aff
->v
->el
[0]);
1707 isl_int_sub_ui(aff
->v
->el
[1], aff
->v
->el
[1], 1);
1708 aff
= isl_aff_floor(aff
);
1713 /* Apply the expansion computed by isl_merge_divs.
1714 * The expansion itself is given by "exp" while the resulting
1715 * list of divs is given by "div".
1717 __isl_give isl_aff
*isl_aff_expand_divs(__isl_take isl_aff
*aff
,
1718 __isl_take isl_mat
*div
, int *exp
)
1724 aff
= isl_aff_cow(aff
);
1728 old_n_div
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
1729 new_n_div
= isl_mat_rows(div
);
1730 if (old_n_div
< 0 || new_n_div
< 0)
1732 offset
= 1 + isl_local_space_offset(aff
->ls
, isl_dim_div
);
1734 aff
->v
= isl_vec_expand(aff
->v
, offset
, old_n_div
, exp
, new_n_div
);
1735 aff
->ls
= isl_local_space_replace_divs(aff
->ls
, div
);
1736 if (!aff
->v
|| !aff
->ls
)
1737 return isl_aff_free(aff
);
1745 /* Add two affine expressions that live in the same local space.
1747 static __isl_give isl_aff
*add_expanded(__isl_take isl_aff
*aff1
,
1748 __isl_take isl_aff
*aff2
)
1752 aff1
= isl_aff_cow(aff1
);
1756 aff1
->v
= isl_vec_cow(aff1
->v
);
1762 isl_int_gcd(gcd
, aff1
->v
->el
[0], aff2
->v
->el
[0]);
1763 isl_int_divexact(f
, aff2
->v
->el
[0], gcd
);
1764 isl_seq_scale(aff1
->v
->el
+ 1, aff1
->v
->el
+ 1, f
, aff1
->v
->size
- 1);
1765 isl_int_divexact(f
, aff1
->v
->el
[0], gcd
);
1766 isl_seq_addmul(aff1
->v
->el
+ 1, f
, aff2
->v
->el
+ 1, aff1
->v
->size
- 1);
1767 isl_int_divexact(f
, aff2
->v
->el
[0], gcd
);
1768 isl_int_mul(aff1
->v
->el
[0], aff1
->v
->el
[0], f
);
1780 /* Return the sum of "aff1" and "aff2".
1782 * If either of the two is NaN, then the result is NaN.
1784 __isl_give isl_aff
*isl_aff_add(__isl_take isl_aff
*aff1
,
1785 __isl_take isl_aff
*aff2
)
1791 isl_size n_div1
, n_div2
;
1796 ctx
= isl_aff_get_ctx(aff1
);
1797 if (!isl_space_is_equal(aff1
->ls
->dim
, aff2
->ls
->dim
))
1798 isl_die(ctx
, isl_error_invalid
,
1799 "spaces don't match", goto error
);
1801 if (isl_aff_is_nan(aff1
)) {
1805 if (isl_aff_is_nan(aff2
)) {
1810 n_div1
= isl_aff_dim(aff1
, isl_dim_div
);
1811 n_div2
= isl_aff_dim(aff2
, isl_dim_div
);
1812 if (n_div1
< 0 || n_div2
< 0)
1814 if (n_div1
== 0 && n_div2
== 0)
1815 return add_expanded(aff1
, aff2
);
1817 exp1
= isl_alloc_array(ctx
, int, n_div1
);
1818 exp2
= isl_alloc_array(ctx
, int, n_div2
);
1819 if ((n_div1
&& !exp1
) || (n_div2
&& !exp2
))
1822 div
= isl_merge_divs(aff1
->ls
->div
, aff2
->ls
->div
, exp1
, exp2
);
1823 aff1
= isl_aff_expand_divs(aff1
, isl_mat_copy(div
), exp1
);
1824 aff2
= isl_aff_expand_divs(aff2
, div
, exp2
);
1828 return add_expanded(aff1
, aff2
);
1837 __isl_give isl_aff
*isl_aff_sub(__isl_take isl_aff
*aff1
,
1838 __isl_take isl_aff
*aff2
)
1840 return isl_aff_add(aff1
, isl_aff_neg(aff2
));
1843 /* Return the result of scaling "aff" by a factor of "f".
1845 * As a special case, f * NaN = NaN.
1847 __isl_give isl_aff
*isl_aff_scale(__isl_take isl_aff
*aff
, isl_int f
)
1853 if (isl_aff_is_nan(aff
))
1856 if (isl_int_is_one(f
))
1859 aff
= isl_aff_cow(aff
);
1862 aff
->v
= isl_vec_cow(aff
->v
);
1864 return isl_aff_free(aff
);
1866 if (isl_int_is_pos(f
) && isl_int_is_divisible_by(aff
->v
->el
[0], f
)) {
1867 isl_int_divexact(aff
->v
->el
[0], aff
->v
->el
[0], f
);
1872 isl_int_gcd(gcd
, aff
->v
->el
[0], f
);
1873 isl_int_divexact(aff
->v
->el
[0], aff
->v
->el
[0], gcd
);
1874 isl_int_divexact(gcd
, f
, gcd
);
1875 isl_seq_scale(aff
->v
->el
+ 1, aff
->v
->el
+ 1, gcd
, aff
->v
->size
- 1);
1881 /* Multiple "aff" by "v".
1883 __isl_give isl_aff
*isl_aff_scale_val(__isl_take isl_aff
*aff
,
1884 __isl_take isl_val
*v
)
1889 if (isl_val_is_one(v
)) {
1894 if (!isl_val_is_rat(v
))
1895 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1896 "expecting rational factor", goto error
);
1898 aff
= isl_aff_scale(aff
, v
->n
);
1899 aff
= isl_aff_scale_down(aff
, v
->d
);
1909 /* Return the result of scaling "aff" down by a factor of "f".
1911 * As a special case, NaN/f = NaN.
1913 __isl_give isl_aff
*isl_aff_scale_down(__isl_take isl_aff
*aff
, isl_int f
)
1919 if (isl_aff_is_nan(aff
))
1922 if (isl_int_is_one(f
))
1925 aff
= isl_aff_cow(aff
);
1929 if (isl_int_is_zero(f
))
1930 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1931 "cannot scale down by zero", return isl_aff_free(aff
));
1933 aff
->v
= isl_vec_cow(aff
->v
);
1935 return isl_aff_free(aff
);
1938 isl_seq_gcd(aff
->v
->el
+ 1, aff
->v
->size
- 1, &gcd
);
1939 isl_int_gcd(gcd
, gcd
, f
);
1940 isl_seq_scale_down(aff
->v
->el
+ 1, aff
->v
->el
+ 1, gcd
, aff
->v
->size
- 1);
1941 isl_int_divexact(gcd
, f
, gcd
);
1942 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], gcd
);
1948 /* Divide "aff" by "v".
1950 __isl_give isl_aff
*isl_aff_scale_down_val(__isl_take isl_aff
*aff
,
1951 __isl_take isl_val
*v
)
1956 if (isl_val_is_one(v
)) {
1961 if (!isl_val_is_rat(v
))
1962 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1963 "expecting rational factor", goto error
);
1964 if (!isl_val_is_pos(v
))
1965 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1966 "factor needs to be positive", goto error
);
1968 aff
= isl_aff_scale(aff
, v
->d
);
1969 aff
= isl_aff_scale_down(aff
, v
->n
);
1979 __isl_give isl_aff
*isl_aff_scale_down_ui(__isl_take isl_aff
*aff
, unsigned f
)
1987 isl_int_set_ui(v
, f
);
1988 aff
= isl_aff_scale_down(aff
, v
);
1994 __isl_give isl_aff
*isl_aff_set_dim_name(__isl_take isl_aff
*aff
,
1995 enum isl_dim_type type
, unsigned pos
, const char *s
)
1997 aff
= isl_aff_cow(aff
);
2000 if (type
== isl_dim_out
)
2001 isl_die(aff
->v
->ctx
, isl_error_invalid
,
2002 "cannot set name of output/set dimension",
2003 return isl_aff_free(aff
));
2004 if (type
== isl_dim_in
)
2006 aff
->ls
= isl_local_space_set_dim_name(aff
->ls
, type
, pos
, s
);
2008 return isl_aff_free(aff
);
2013 __isl_give isl_aff
*isl_aff_set_dim_id(__isl_take isl_aff
*aff
,
2014 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
2016 aff
= isl_aff_cow(aff
);
2019 if (type
== isl_dim_out
)
2020 isl_die(aff
->v
->ctx
, isl_error_invalid
,
2021 "cannot set name of output/set dimension",
2023 if (type
== isl_dim_in
)
2025 aff
->ls
= isl_local_space_set_dim_id(aff
->ls
, type
, pos
, id
);
2027 return isl_aff_free(aff
);
2036 /* Replace the identifier of the input tuple of "aff" by "id".
2037 * type is currently required to be equal to isl_dim_in
2039 __isl_give isl_aff
*isl_aff_set_tuple_id(__isl_take isl_aff
*aff
,
2040 enum isl_dim_type type
, __isl_take isl_id
*id
)
2042 aff
= isl_aff_cow(aff
);
2045 if (type
!= isl_dim_in
)
2046 isl_die(aff
->v
->ctx
, isl_error_invalid
,
2047 "cannot only set id of input tuple", goto error
);
2048 aff
->ls
= isl_local_space_set_tuple_id(aff
->ls
, isl_dim_set
, id
);
2050 return isl_aff_free(aff
);
2059 /* Exploit the equalities in "eq" to simplify the affine expression
2060 * and the expressions of the integer divisions in the local space.
2061 * The integer divisions in this local space are assumed to appear
2062 * as regular dimensions in "eq".
2064 static __isl_give isl_aff
*isl_aff_substitute_equalities_lifted(
2065 __isl_take isl_aff
*aff
, __isl_take isl_basic_set
*eq
)
2073 if (eq
->n_eq
== 0) {
2074 isl_basic_set_free(eq
);
2078 aff
= isl_aff_cow(aff
);
2082 aff
->ls
= isl_local_space_substitute_equalities(aff
->ls
,
2083 isl_basic_set_copy(eq
));
2084 aff
->v
= isl_vec_cow(aff
->v
);
2085 if (!aff
->ls
|| !aff
->v
)
2088 o_div
= isl_basic_set_offset(eq
, isl_dim_div
);
2090 for (i
= 0; i
< eq
->n_eq
; ++i
) {
2091 j
= isl_seq_last_non_zero(eq
->eq
[i
], o_div
+ n_div
);
2092 if (j
< 0 || j
== 0 || j
>= o_div
)
2095 isl_seq_elim(aff
->v
->el
+ 1, eq
->eq
[i
], j
, o_div
,
2099 isl_basic_set_free(eq
);
2100 aff
= isl_aff_normalize(aff
);
2103 isl_basic_set_free(eq
);
2108 /* Exploit the equalities in "eq" to simplify the affine expression
2109 * and the expressions of the integer divisions in the local space.
2111 __isl_give isl_aff
*isl_aff_substitute_equalities(__isl_take isl_aff
*aff
,
2112 __isl_take isl_basic_set
*eq
)
2118 n_div
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
2122 eq
= isl_basic_set_add_dims(eq
, isl_dim_set
, n_div
);
2123 return isl_aff_substitute_equalities_lifted(aff
, eq
);
2125 isl_basic_set_free(eq
);
2130 /* Look for equalities among the variables shared by context and aff
2131 * and the integer divisions of aff, if any.
2132 * The equalities are then used to eliminate coefficients and/or integer
2133 * divisions from aff.
2135 __isl_give isl_aff
*isl_aff_gist(__isl_take isl_aff
*aff
,
2136 __isl_take isl_set
*context
)
2138 isl_local_space
*ls
;
2139 isl_basic_set
*hull
;
2141 ls
= isl_aff_get_domain_local_space(aff
);
2142 context
= isl_local_space_lift_set(ls
, context
);
2144 hull
= isl_set_affine_hull(context
);
2145 return isl_aff_substitute_equalities_lifted(aff
, hull
);
2148 __isl_give isl_aff
*isl_aff_gist_params(__isl_take isl_aff
*aff
,
2149 __isl_take isl_set
*context
)
2151 isl_set
*dom_context
= isl_set_universe(isl_aff_get_domain_space(aff
));
2152 dom_context
= isl_set_intersect_params(dom_context
, context
);
2153 return isl_aff_gist(aff
, dom_context
);
2156 /* Return a basic set containing those elements in the space
2157 * of aff where it is positive. "rational" should not be set.
2159 * If "aff" is NaN, then it is not positive.
2161 static __isl_give isl_basic_set
*aff_pos_basic_set(__isl_take isl_aff
*aff
,
2162 int rational
, void *user
)
2164 isl_constraint
*ineq
;
2165 isl_basic_set
*bset
;
2170 if (isl_aff_is_nan(aff
)) {
2171 isl_space
*space
= isl_aff_get_domain_space(aff
);
2173 return isl_basic_set_empty(space
);
2176 isl_die(isl_aff_get_ctx(aff
), isl_error_unsupported
,
2177 "rational sets not supported", goto error
);
2179 ineq
= isl_inequality_from_aff(aff
);
2180 c
= isl_constraint_get_constant_val(ineq
);
2181 c
= isl_val_sub_ui(c
, 1);
2182 ineq
= isl_constraint_set_constant_val(ineq
, c
);
2184 bset
= isl_basic_set_from_constraint(ineq
);
2185 bset
= isl_basic_set_simplify(bset
);
2192 /* Return a basic set containing those elements in the space
2193 * of aff where it is non-negative.
2194 * If "rational" is set, then return a rational basic set.
2196 * If "aff" is NaN, then it is not non-negative (it's not negative either).
2198 static __isl_give isl_basic_set
*aff_nonneg_basic_set(
2199 __isl_take isl_aff
*aff
, int rational
, void *user
)
2201 isl_constraint
*ineq
;
2202 isl_basic_set
*bset
;
2206 if (isl_aff_is_nan(aff
)) {
2207 isl_space
*space
= isl_aff_get_domain_space(aff
);
2209 return isl_basic_set_empty(space
);
2212 ineq
= isl_inequality_from_aff(aff
);
2214 bset
= isl_basic_set_from_constraint(ineq
);
2216 bset
= isl_basic_set_set_rational(bset
);
2217 bset
= isl_basic_set_simplify(bset
);
2221 /* Return a basic set containing those elements in the space
2222 * of aff where it is non-negative.
2224 __isl_give isl_basic_set
*isl_aff_nonneg_basic_set(__isl_take isl_aff
*aff
)
2226 return aff_nonneg_basic_set(aff
, 0, NULL
);
2229 /* Return a basic set containing those elements in the domain space
2230 * of "aff" where it is positive.
2232 __isl_give isl_basic_set
*isl_aff_pos_basic_set(__isl_take isl_aff
*aff
)
2234 aff
= isl_aff_add_constant_num_si(aff
, -1);
2235 return isl_aff_nonneg_basic_set(aff
);
2238 /* Return a basic set containing those elements in the domain space
2239 * of aff where it is negative.
2241 __isl_give isl_basic_set
*isl_aff_neg_basic_set(__isl_take isl_aff
*aff
)
2243 aff
= isl_aff_neg(aff
);
2244 return isl_aff_pos_basic_set(aff
);
2247 /* Return a basic set containing those elements in the space
2248 * of aff where it is zero.
2249 * If "rational" is set, then return a rational basic set.
2251 * If "aff" is NaN, then it is not zero.
2253 static __isl_give isl_basic_set
*aff_zero_basic_set(__isl_take isl_aff
*aff
,
2254 int rational
, void *user
)
2256 isl_constraint
*ineq
;
2257 isl_basic_set
*bset
;
2261 if (isl_aff_is_nan(aff
)) {
2262 isl_space
*space
= isl_aff_get_domain_space(aff
);
2264 return isl_basic_set_empty(space
);
2267 ineq
= isl_equality_from_aff(aff
);
2269 bset
= isl_basic_set_from_constraint(ineq
);
2271 bset
= isl_basic_set_set_rational(bset
);
2272 bset
= isl_basic_set_simplify(bset
);
2276 /* Return a basic set containing those elements in the space
2277 * of aff where it is zero.
2279 __isl_give isl_basic_set
*isl_aff_zero_basic_set(__isl_take isl_aff
*aff
)
2281 return aff_zero_basic_set(aff
, 0, NULL
);
2284 /* Return a basic set containing those elements in the shared space
2285 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
2287 __isl_give isl_basic_set
*isl_aff_ge_basic_set(__isl_take isl_aff
*aff1
,
2288 __isl_take isl_aff
*aff2
)
2290 aff1
= isl_aff_sub(aff1
, aff2
);
2292 return isl_aff_nonneg_basic_set(aff1
);
2295 /* Return a basic set containing those elements in the shared domain space
2296 * of "aff1" and "aff2" where "aff1" is greater than "aff2".
2298 __isl_give isl_basic_set
*isl_aff_gt_basic_set(__isl_take isl_aff
*aff1
,
2299 __isl_take isl_aff
*aff2
)
2301 aff1
= isl_aff_sub(aff1
, aff2
);
2303 return isl_aff_pos_basic_set(aff1
);
2306 /* Return a set containing those elements in the shared space
2307 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
2309 __isl_give isl_set
*isl_aff_ge_set(__isl_take isl_aff
*aff1
,
2310 __isl_take isl_aff
*aff2
)
2312 return isl_set_from_basic_set(isl_aff_ge_basic_set(aff1
, aff2
));
2315 /* Return a set containing those elements in the shared domain space
2316 * of aff1 and aff2 where aff1 is greater than aff2.
2318 * If either of the two inputs is NaN, then the result is empty,
2319 * as comparisons with NaN always return false.
2321 __isl_give isl_set
*isl_aff_gt_set(__isl_take isl_aff
*aff1
,
2322 __isl_take isl_aff
*aff2
)
2324 return isl_set_from_basic_set(isl_aff_gt_basic_set(aff1
, aff2
));
2327 /* Return a basic set containing those elements in the shared space
2328 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
2330 __isl_give isl_basic_set
*isl_aff_le_basic_set(__isl_take isl_aff
*aff1
,
2331 __isl_take isl_aff
*aff2
)
2333 return isl_aff_ge_basic_set(aff2
, aff1
);
2336 /* Return a basic set containing those elements in the shared domain space
2337 * of "aff1" and "aff2" where "aff1" is smaller than "aff2".
2339 __isl_give isl_basic_set
*isl_aff_lt_basic_set(__isl_take isl_aff
*aff1
,
2340 __isl_take isl_aff
*aff2
)
2342 return isl_aff_gt_basic_set(aff2
, aff1
);
2345 /* Return a set containing those elements in the shared space
2346 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
2348 __isl_give isl_set
*isl_aff_le_set(__isl_take isl_aff
*aff1
,
2349 __isl_take isl_aff
*aff2
)
2351 return isl_aff_ge_set(aff2
, aff1
);
2354 /* Return a set containing those elements in the shared domain space
2355 * of "aff1" and "aff2" where "aff1" is smaller than "aff2".
2357 __isl_give isl_set
*isl_aff_lt_set(__isl_take isl_aff
*aff1
,
2358 __isl_take isl_aff
*aff2
)
2360 return isl_set_from_basic_set(isl_aff_lt_basic_set(aff1
, aff2
));
2363 /* Return a basic set containing those elements in the shared space
2364 * of aff1 and aff2 where aff1 and aff2 are equal.
2366 __isl_give isl_basic_set
*isl_aff_eq_basic_set(__isl_take isl_aff
*aff1
,
2367 __isl_take isl_aff
*aff2
)
2369 aff1
= isl_aff_sub(aff1
, aff2
);
2371 return isl_aff_zero_basic_set(aff1
);
2374 /* Return a set containing those elements in the shared space
2375 * of aff1 and aff2 where aff1 and aff2 are equal.
2377 __isl_give isl_set
*isl_aff_eq_set(__isl_take isl_aff
*aff1
,
2378 __isl_take isl_aff
*aff2
)
2380 return isl_set_from_basic_set(isl_aff_eq_basic_set(aff1
, aff2
));
2383 /* Return a set containing those elements in the shared domain space
2384 * of aff1 and aff2 where aff1 and aff2 are not equal.
2386 * If either of the two inputs is NaN, then the result is empty,
2387 * as comparisons with NaN always return false.
2389 __isl_give isl_set
*isl_aff_ne_set(__isl_take isl_aff
*aff1
,
2390 __isl_take isl_aff
*aff2
)
2392 isl_set
*set_lt
, *set_gt
;
2394 set_lt
= isl_aff_lt_set(isl_aff_copy(aff1
),
2395 isl_aff_copy(aff2
));
2396 set_gt
= isl_aff_gt_set(aff1
, aff2
);
2397 return isl_set_union_disjoint(set_lt
, set_gt
);
2400 __isl_give isl_aff
*isl_aff_add_on_domain(__isl_keep isl_set
*dom
,
2401 __isl_take isl_aff
*aff1
, __isl_take isl_aff
*aff2
)
2403 aff1
= isl_aff_add(aff1
, aff2
);
2404 aff1
= isl_aff_gist(aff1
, isl_set_copy(dom
));
2408 int isl_aff_is_empty(__isl_keep isl_aff
*aff
)
2417 #define TYPE isl_aff
2419 #include "check_type_range_templ.c"
2421 /* Check whether the given affine expression has non-zero coefficient
2422 * for any dimension in the given range or if any of these dimensions
2423 * appear with non-zero coefficients in any of the integer divisions
2424 * involved in the affine expression.
2426 isl_bool
isl_aff_involves_dims(__isl_keep isl_aff
*aff
,
2427 enum isl_dim_type type
, unsigned first
, unsigned n
)
2431 isl_bool involves
= isl_bool_false
;
2434 return isl_bool_error
;
2436 return isl_bool_false
;
2437 if (isl_aff_check_range(aff
, type
, first
, n
) < 0)
2438 return isl_bool_error
;
2440 active
= isl_local_space_get_active(aff
->ls
, aff
->v
->el
+ 2);
2444 first
+= isl_local_space_offset(aff
->ls
, type
) - 1;
2445 for (i
= 0; i
< n
; ++i
)
2446 if (active
[first
+ i
]) {
2447 involves
= isl_bool_true
;
2456 return isl_bool_error
;
2459 /* Does "aff" involve any local variables, i.e., integer divisions?
2461 isl_bool
isl_aff_involves_locals(__isl_keep isl_aff
*aff
)
2465 n
= isl_aff_dim(aff
, isl_dim_div
);
2467 return isl_bool_error
;
2468 return isl_aff_involves_dims(aff
, isl_dim_div
, 0, n
);
2471 __isl_give isl_aff
*isl_aff_drop_dims(__isl_take isl_aff
*aff
,
2472 enum isl_dim_type type
, unsigned first
, unsigned n
)
2478 if (type
== isl_dim_out
)
2479 isl_die(aff
->v
->ctx
, isl_error_invalid
,
2480 "cannot drop output/set dimension",
2481 return isl_aff_free(aff
));
2482 if (type
== isl_dim_in
)
2484 if (n
== 0 && !isl_local_space_is_named_or_nested(aff
->ls
, type
))
2487 ctx
= isl_aff_get_ctx(aff
);
2488 if (isl_local_space_check_range(aff
->ls
, type
, first
, n
) < 0)
2489 return isl_aff_free(aff
);
2491 aff
= isl_aff_cow(aff
);
2495 aff
->ls
= isl_local_space_drop_dims(aff
->ls
, type
, first
, n
);
2497 return isl_aff_free(aff
);
2499 first
+= 1 + isl_local_space_offset(aff
->ls
, type
);
2500 aff
->v
= isl_vec_drop_els(aff
->v
, first
, n
);
2502 return isl_aff_free(aff
);
2507 /* Is the domain of "aff" a product?
2509 static isl_bool
isl_aff_domain_is_product(__isl_keep isl_aff
*aff
)
2511 return isl_space_is_product(isl_aff_peek_domain_space(aff
));
2515 #define TYPE isl_aff
2516 #include <isl_domain_factor_templ.c>
2518 /* Project the domain of the affine expression onto its parameter space.
2519 * The affine expression may not involve any of the domain dimensions.
2521 __isl_give isl_aff
*isl_aff_project_domain_on_params(__isl_take isl_aff
*aff
)
2526 n
= isl_aff_dim(aff
, isl_dim_in
);
2528 return isl_aff_free(aff
);
2529 aff
= isl_aff_drop_domain(aff
, 0, n
);
2530 space
= isl_aff_get_domain_space(aff
);
2531 space
= isl_space_params(space
);
2532 aff
= isl_aff_reset_domain_space(aff
, space
);
2536 /* Convert an affine expression defined over a parameter domain
2537 * into one that is defined over a zero-dimensional set.
2539 __isl_give isl_aff
*isl_aff_from_range(__isl_take isl_aff
*aff
)
2541 isl_local_space
*ls
;
2543 ls
= isl_aff_take_domain_local_space(aff
);
2544 ls
= isl_local_space_set_from_params(ls
);
2545 aff
= isl_aff_restore_domain_local_space(aff
, ls
);
2550 __isl_give isl_aff
*isl_aff_insert_dims(__isl_take isl_aff
*aff
,
2551 enum isl_dim_type type
, unsigned first
, unsigned n
)
2557 if (type
== isl_dim_out
)
2558 isl_die(aff
->v
->ctx
, isl_error_invalid
,
2559 "cannot insert output/set dimensions",
2560 return isl_aff_free(aff
));
2561 if (type
== isl_dim_in
)
2563 if (n
== 0 && !isl_local_space_is_named_or_nested(aff
->ls
, type
))
2566 ctx
= isl_aff_get_ctx(aff
);
2567 if (isl_local_space_check_range(aff
->ls
, type
, first
, 0) < 0)
2568 return isl_aff_free(aff
);
2570 aff
= isl_aff_cow(aff
);
2574 aff
->ls
= isl_local_space_insert_dims(aff
->ls
, type
, first
, n
);
2576 return isl_aff_free(aff
);
2578 first
+= 1 + isl_local_space_offset(aff
->ls
, type
);
2579 aff
->v
= isl_vec_insert_zero_els(aff
->v
, first
, n
);
2581 return isl_aff_free(aff
);
2586 __isl_give isl_aff
*isl_aff_add_dims(__isl_take isl_aff
*aff
,
2587 enum isl_dim_type type
, unsigned n
)
2591 pos
= isl_aff_dim(aff
, type
);
2593 return isl_aff_free(aff
);
2595 return isl_aff_insert_dims(aff
, type
, pos
, n
);
2598 __isl_give isl_pw_aff
*isl_pw_aff_add_dims(__isl_take isl_pw_aff
*pwaff
,
2599 enum isl_dim_type type
, unsigned n
)
2603 pos
= isl_pw_aff_dim(pwaff
, type
);
2605 return isl_pw_aff_free(pwaff
);
2607 return isl_pw_aff_insert_dims(pwaff
, type
, pos
, n
);
2610 /* Move the "n" dimensions of "src_type" starting at "src_pos" of "aff"
2611 * to dimensions of "dst_type" at "dst_pos".
2613 * We only support moving input dimensions to parameters and vice versa.
2615 __isl_give isl_aff
*isl_aff_move_dims(__isl_take isl_aff
*aff
,
2616 enum isl_dim_type dst_type
, unsigned dst_pos
,
2617 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
2625 !isl_local_space_is_named_or_nested(aff
->ls
, src_type
) &&
2626 !isl_local_space_is_named_or_nested(aff
->ls
, dst_type
))
2629 if (dst_type
== isl_dim_out
|| src_type
== isl_dim_out
)
2630 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2631 "cannot move output/set dimension",
2632 return isl_aff_free(aff
));
2633 if (dst_type
== isl_dim_div
|| src_type
== isl_dim_div
)
2634 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2635 "cannot move divs", return isl_aff_free(aff
));
2636 if (dst_type
== isl_dim_in
)
2637 dst_type
= isl_dim_set
;
2638 if (src_type
== isl_dim_in
)
2639 src_type
= isl_dim_set
;
2641 if (isl_local_space_check_range(aff
->ls
, src_type
, src_pos
, n
) < 0)
2642 return isl_aff_free(aff
);
2643 if (dst_type
== src_type
)
2644 isl_die(isl_aff_get_ctx(aff
), isl_error_unsupported
,
2645 "moving dims within the same type not supported",
2646 return isl_aff_free(aff
));
2648 aff
= isl_aff_cow(aff
);
2652 g_src_pos
= 1 + isl_local_space_offset(aff
->ls
, src_type
) + src_pos
;
2653 g_dst_pos
= 1 + isl_local_space_offset(aff
->ls
, dst_type
) + dst_pos
;
2654 if (dst_type
> src_type
)
2657 aff
->v
= isl_vec_move_els(aff
->v
, g_dst_pos
, g_src_pos
, n
);
2658 aff
->ls
= isl_local_space_move_dims(aff
->ls
, dst_type
, dst_pos
,
2659 src_type
, src_pos
, n
);
2660 if (!aff
->v
|| !aff
->ls
)
2661 return isl_aff_free(aff
);
2663 aff
= sort_divs(aff
);
2668 __isl_give isl_pw_aff
*isl_pw_aff_from_aff(__isl_take isl_aff
*aff
)
2670 isl_set
*dom
= isl_set_universe(isl_aff_get_domain_space(aff
));
2671 return isl_pw_aff_alloc(dom
, aff
);
2674 #define isl_aff_involves_nan isl_aff_is_nan
2677 #define PW isl_pw_aff
2681 #define EL_IS_ZERO is_empty
2685 #define IS_ZERO is_empty
2688 #undef DEFAULT_IS_ZERO
2689 #define DEFAULT_IS_ZERO 0
2695 #include <isl_pw_templ.c>
2696 #include <isl_pw_bind_domain_templ.c>
2697 #include <isl_pw_eval.c>
2698 #include <isl_pw_hash.c>
2699 #include <isl_pw_union_opt.c>
2704 #include <isl_union_single.c>
2705 #include <isl_union_neg.c>
2707 static __isl_give isl_set
*align_params_pw_pw_set_and(
2708 __isl_take isl_pw_aff
*pwaff1
, __isl_take isl_pw_aff
*pwaff2
,
2709 __isl_give isl_set
*(*fn
)(__isl_take isl_pw_aff
*pwaff1
,
2710 __isl_take isl_pw_aff
*pwaff2
))
2712 isl_bool equal_params
;
2714 if (!pwaff1
|| !pwaff2
)
2716 equal_params
= isl_space_has_equal_params(pwaff1
->dim
, pwaff2
->dim
);
2717 if (equal_params
< 0)
2720 return fn(pwaff1
, pwaff2
);
2721 if (isl_pw_aff_check_named_params(pwaff1
) < 0 ||
2722 isl_pw_aff_check_named_params(pwaff2
) < 0)
2724 pwaff1
= isl_pw_aff_align_params(pwaff1
, isl_pw_aff_get_space(pwaff2
));
2725 pwaff2
= isl_pw_aff_align_params(pwaff2
, isl_pw_aff_get_space(pwaff1
));
2726 return fn(pwaff1
, pwaff2
);
2728 isl_pw_aff_free(pwaff1
);
2729 isl_pw_aff_free(pwaff2
);
2733 /* Align the parameters of the to isl_pw_aff arguments and
2734 * then apply a function "fn" on them that returns an isl_map.
2736 static __isl_give isl_map
*align_params_pw_pw_map_and(
2737 __isl_take isl_pw_aff
*pa1
, __isl_take isl_pw_aff
*pa2
,
2738 __isl_give isl_map
*(*fn
)(__isl_take isl_pw_aff
*pa1
,
2739 __isl_take isl_pw_aff
*pa2
))
2741 isl_bool equal_params
;
2745 equal_params
= isl_space_has_equal_params(pa1
->dim
, pa2
->dim
);
2746 if (equal_params
< 0)
2749 return fn(pa1
, pa2
);
2750 if (isl_pw_aff_check_named_params(pa1
) < 0 ||
2751 isl_pw_aff_check_named_params(pa2
) < 0)
2753 pa1
= isl_pw_aff_align_params(pa1
, isl_pw_aff_get_space(pa2
));
2754 pa2
= isl_pw_aff_align_params(pa2
, isl_pw_aff_get_space(pa1
));
2755 return fn(pa1
, pa2
);
2757 isl_pw_aff_free(pa1
);
2758 isl_pw_aff_free(pa2
);
2762 /* Compute a piecewise quasi-affine expression with a domain that
2763 * is the union of those of pwaff1 and pwaff2 and such that on each
2764 * cell, the quasi-affine expression is the maximum of those of pwaff1
2765 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2766 * cell, then the associated expression is the defined one.
2768 static __isl_give isl_pw_aff
*pw_aff_union_max(__isl_take isl_pw_aff
*pwaff1
,
2769 __isl_take isl_pw_aff
*pwaff2
)
2771 return isl_pw_aff_union_opt_cmp(pwaff1
, pwaff2
, &isl_aff_ge_set
);
2774 __isl_give isl_pw_aff
*isl_pw_aff_union_max(__isl_take isl_pw_aff
*pwaff1
,
2775 __isl_take isl_pw_aff
*pwaff2
)
2777 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
,
2781 /* Compute a piecewise quasi-affine expression with a domain that
2782 * is the union of those of pwaff1 and pwaff2 and such that on each
2783 * cell, the quasi-affine expression is the minimum of those of pwaff1
2784 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2785 * cell, then the associated expression is the defined one.
2787 static __isl_give isl_pw_aff
*pw_aff_union_min(__isl_take isl_pw_aff
*pwaff1
,
2788 __isl_take isl_pw_aff
*pwaff2
)
2790 return isl_pw_aff_union_opt_cmp(pwaff1
, pwaff2
, &isl_aff_le_set
);
2793 __isl_give isl_pw_aff
*isl_pw_aff_union_min(__isl_take isl_pw_aff
*pwaff1
,
2794 __isl_take isl_pw_aff
*pwaff2
)
2796 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
,
2800 __isl_give isl_pw_aff
*isl_pw_aff_union_opt(__isl_take isl_pw_aff
*pwaff1
,
2801 __isl_take isl_pw_aff
*pwaff2
, int max
)
2804 return isl_pw_aff_union_max(pwaff1
, pwaff2
);
2806 return isl_pw_aff_union_min(pwaff1
, pwaff2
);
2809 /* Is the domain of "pa" a product?
2811 static isl_bool
isl_pw_aff_domain_is_product(__isl_keep isl_pw_aff
*pa
)
2813 return isl_space_domain_is_wrapping(isl_pw_aff_peek_space(pa
));
2817 #define TYPE isl_pw_aff
2818 #include <isl_domain_factor_templ.c>
2820 /* Return a set containing those elements in the domain
2821 * of "pwaff" where it satisfies "fn" (if complement is 0) or
2822 * does not satisfy "fn" (if complement is 1).
2824 * The pieces with a NaN never belong to the result since
2825 * NaN does not satisfy any property.
2827 static __isl_give isl_set
*pw_aff_locus(__isl_take isl_pw_aff
*pwaff
,
2828 __isl_give isl_basic_set
*(*fn
)(__isl_take isl_aff
*aff
, int rational
,
2830 int complement
, void *user
)
2838 set
= isl_set_empty(isl_pw_aff_get_domain_space(pwaff
));
2840 for (i
= 0; i
< pwaff
->n
; ++i
) {
2841 isl_basic_set
*bset
;
2842 isl_set
*set_i
, *locus
;
2845 if (isl_aff_is_nan(pwaff
->p
[i
].aff
))
2848 rational
= isl_set_has_rational(pwaff
->p
[i
].set
);
2849 bset
= fn(isl_aff_copy(pwaff
->p
[i
].aff
), rational
, user
);
2850 locus
= isl_set_from_basic_set(bset
);
2851 set_i
= isl_set_copy(pwaff
->p
[i
].set
);
2853 set_i
= isl_set_subtract(set_i
, locus
);
2855 set_i
= isl_set_intersect(set_i
, locus
);
2856 set
= isl_set_union_disjoint(set
, set_i
);
2859 isl_pw_aff_free(pwaff
);
2864 /* Return a set containing those elements in the domain
2865 * of "pa" where it is positive.
2867 __isl_give isl_set
*isl_pw_aff_pos_set(__isl_take isl_pw_aff
*pa
)
2869 return pw_aff_locus(pa
, &aff_pos_basic_set
, 0, NULL
);
2872 /* Return a set containing those elements in the domain
2873 * of pwaff where it is non-negative.
2875 __isl_give isl_set
*isl_pw_aff_nonneg_set(__isl_take isl_pw_aff
*pwaff
)
2877 return pw_aff_locus(pwaff
, &aff_nonneg_basic_set
, 0, NULL
);
2880 /* Return a set containing those elements in the domain
2881 * of pwaff where it is zero.
2883 __isl_give isl_set
*isl_pw_aff_zero_set(__isl_take isl_pw_aff
*pwaff
)
2885 return pw_aff_locus(pwaff
, &aff_zero_basic_set
, 0, NULL
);
2888 /* Return a set containing those elements in the domain
2889 * of pwaff where it is not zero.
2891 __isl_give isl_set
*isl_pw_aff_non_zero_set(__isl_take isl_pw_aff
*pwaff
)
2893 return pw_aff_locus(pwaff
, &aff_zero_basic_set
, 1, NULL
);
2896 /* Bind the affine function "aff" to the parameter "id",
2897 * returning the elements in the domain where the affine expression
2898 * is equal to the parameter.
2900 __isl_give isl_basic_set
*isl_aff_bind_id(__isl_take isl_aff
*aff
,
2901 __isl_take isl_id
*id
)
2906 space
= isl_aff_get_domain_space(aff
);
2907 space
= isl_space_add_param_id(space
, isl_id_copy(id
));
2909 aff
= isl_aff_align_params(aff
, isl_space_copy(space
));
2910 aff_id
= isl_aff_param_on_domain_space_id(space
, id
);
2912 return isl_aff_eq_basic_set(aff
, aff_id
);
2915 /* Wrapper around isl_aff_bind_id for use as pw_aff_locus callback.
2916 * "rational" should not be set.
2918 static __isl_give isl_basic_set
*aff_bind_id(__isl_take isl_aff
*aff
,
2919 int rational
, void *user
)
2926 isl_die(isl_aff_get_ctx(aff
), isl_error_unsupported
,
2927 "rational binding not supported", goto error
);
2928 return isl_aff_bind_id(aff
, isl_id_copy(id
));
2934 /* Bind the piecewise affine function "pa" to the parameter "id",
2935 * returning the elements in the domain where the expression
2936 * is equal to the parameter.
2938 __isl_give isl_set
*isl_pw_aff_bind_id(__isl_take isl_pw_aff
*pa
,
2939 __isl_take isl_id
*id
)
2943 bound
= pw_aff_locus(pa
, &aff_bind_id
, 0, id
);
2949 /* Return a set containing those elements in the shared domain
2950 * of pwaff1 and pwaff2 where pwaff1 is greater than (or equal) to pwaff2.
2952 * We compute the difference on the shared domain and then construct
2953 * the set of values where this difference is non-negative.
2954 * If strict is set, we first subtract 1 from the difference.
2955 * If equal is set, we only return the elements where pwaff1 and pwaff2
2958 static __isl_give isl_set
*pw_aff_gte_set(__isl_take isl_pw_aff
*pwaff1
,
2959 __isl_take isl_pw_aff
*pwaff2
, int strict
, int equal
)
2961 isl_set
*set1
, *set2
;
2963 set1
= isl_pw_aff_domain(isl_pw_aff_copy(pwaff1
));
2964 set2
= isl_pw_aff_domain(isl_pw_aff_copy(pwaff2
));
2965 set1
= isl_set_intersect(set1
, set2
);
2966 pwaff1
= isl_pw_aff_intersect_domain(pwaff1
, isl_set_copy(set1
));
2967 pwaff2
= isl_pw_aff_intersect_domain(pwaff2
, isl_set_copy(set1
));
2968 pwaff1
= isl_pw_aff_add(pwaff1
, isl_pw_aff_neg(pwaff2
));
2971 isl_space
*space
= isl_set_get_space(set1
);
2973 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(space
));
2974 aff
= isl_aff_add_constant_si(aff
, -1);
2975 pwaff1
= isl_pw_aff_add(pwaff1
, isl_pw_aff_alloc(set1
, aff
));
2980 return isl_pw_aff_zero_set(pwaff1
);
2981 return isl_pw_aff_nonneg_set(pwaff1
);
2984 /* Return a set containing those elements in the shared domain
2985 * of pwaff1 and pwaff2 where pwaff1 is equal to pwaff2.
2987 static __isl_give isl_set
*pw_aff_eq_set(__isl_take isl_pw_aff
*pwaff1
,
2988 __isl_take isl_pw_aff
*pwaff2
)
2990 return pw_aff_gte_set(pwaff1
, pwaff2
, 0, 1);
2993 __isl_give isl_set
*isl_pw_aff_eq_set(__isl_take isl_pw_aff
*pwaff1
,
2994 __isl_take isl_pw_aff
*pwaff2
)
2996 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_eq_set
);
2999 /* Return a set containing those elements in the shared domain
3000 * of pwaff1 and pwaff2 where pwaff1 is greater than or equal to pwaff2.
3002 static __isl_give isl_set
*pw_aff_ge_set(__isl_take isl_pw_aff
*pwaff1
,
3003 __isl_take isl_pw_aff
*pwaff2
)
3005 return pw_aff_gte_set(pwaff1
, pwaff2
, 0, 0);
3008 __isl_give isl_set
*isl_pw_aff_ge_set(__isl_take isl_pw_aff
*pwaff1
,
3009 __isl_take isl_pw_aff
*pwaff2
)
3011 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_ge_set
);
3014 /* Return a set containing those elements in the shared domain
3015 * of pwaff1 and pwaff2 where pwaff1 is strictly greater than pwaff2.
3017 static __isl_give isl_set
*pw_aff_gt_set(__isl_take isl_pw_aff
*pwaff1
,
3018 __isl_take isl_pw_aff
*pwaff2
)
3020 return pw_aff_gte_set(pwaff1
, pwaff2
, 1, 0);
3023 __isl_give isl_set
*isl_pw_aff_gt_set(__isl_take isl_pw_aff
*pwaff1
,
3024 __isl_take isl_pw_aff
*pwaff2
)
3026 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_gt_set
);
3029 __isl_give isl_set
*isl_pw_aff_le_set(__isl_take isl_pw_aff
*pwaff1
,
3030 __isl_take isl_pw_aff
*pwaff2
)
3032 return isl_pw_aff_ge_set(pwaff2
, pwaff1
);
3035 __isl_give isl_set
*isl_pw_aff_lt_set(__isl_take isl_pw_aff
*pwaff1
,
3036 __isl_take isl_pw_aff
*pwaff2
)
3038 return isl_pw_aff_gt_set(pwaff2
, pwaff1
);
3041 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3042 * where the function values are ordered in the same way as "order",
3043 * which returns a set in the shared domain of its two arguments.
3044 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3046 * Let "pa1" and "pa2" be defined on domains A and B respectively.
3047 * We first pull back the two functions such that they are defined on
3048 * the domain [A -> B]. Then we apply "order", resulting in a set
3049 * in the space [A -> B]. Finally, we unwrap this set to obtain
3050 * a map in the space A -> B.
3052 static __isl_give isl_map
*isl_pw_aff_order_map_aligned(
3053 __isl_take isl_pw_aff
*pa1
, __isl_take isl_pw_aff
*pa2
,
3054 __isl_give isl_set
*(*order
)(__isl_take isl_pw_aff
*pa1
,
3055 __isl_take isl_pw_aff
*pa2
))
3057 isl_space
*space1
, *space2
;
3061 space1
= isl_space_domain(isl_pw_aff_get_space(pa1
));
3062 space2
= isl_space_domain(isl_pw_aff_get_space(pa2
));
3063 space1
= isl_space_map_from_domain_and_range(space1
, space2
);
3064 ma
= isl_multi_aff_domain_map(isl_space_copy(space1
));
3065 pa1
= isl_pw_aff_pullback_multi_aff(pa1
, ma
);
3066 ma
= isl_multi_aff_range_map(space1
);
3067 pa2
= isl_pw_aff_pullback_multi_aff(pa2
, ma
);
3068 set
= order(pa1
, pa2
);
3070 return isl_set_unwrap(set
);
3073 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3074 * where the function values are equal.
3075 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3077 static __isl_give isl_map
*isl_pw_aff_eq_map_aligned(__isl_take isl_pw_aff
*pa1
,
3078 __isl_take isl_pw_aff
*pa2
)
3080 return isl_pw_aff_order_map_aligned(pa1
, pa2
, &isl_pw_aff_eq_set
);
3083 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3084 * where the function values are equal.
3086 __isl_give isl_map
*isl_pw_aff_eq_map(__isl_take isl_pw_aff
*pa1
,
3087 __isl_take isl_pw_aff
*pa2
)
3089 return align_params_pw_pw_map_and(pa1
, pa2
, &isl_pw_aff_eq_map_aligned
);
3092 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3093 * where the function value of "pa1" is less than the function value of "pa2".
3094 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3096 static __isl_give isl_map
*isl_pw_aff_lt_map_aligned(__isl_take isl_pw_aff
*pa1
,
3097 __isl_take isl_pw_aff
*pa2
)
3099 return isl_pw_aff_order_map_aligned(pa1
, pa2
, &isl_pw_aff_lt_set
);
3102 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3103 * where the function value of "pa1" is less than the function value of "pa2".
3105 __isl_give isl_map
*isl_pw_aff_lt_map(__isl_take isl_pw_aff
*pa1
,
3106 __isl_take isl_pw_aff
*pa2
)
3108 return align_params_pw_pw_map_and(pa1
, pa2
, &isl_pw_aff_lt_map_aligned
);
3111 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3112 * where the function value of "pa1" is greater than the function value
3114 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3116 static __isl_give isl_map
*isl_pw_aff_gt_map_aligned(__isl_take isl_pw_aff
*pa1
,
3117 __isl_take isl_pw_aff
*pa2
)
3119 return isl_pw_aff_order_map_aligned(pa1
, pa2
, &isl_pw_aff_gt_set
);
3122 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3123 * where the function value of "pa1" is greater than the function value
3126 __isl_give isl_map
*isl_pw_aff_gt_map(__isl_take isl_pw_aff
*pa1
,
3127 __isl_take isl_pw_aff
*pa2
)
3129 return align_params_pw_pw_map_and(pa1
, pa2
, &isl_pw_aff_gt_map_aligned
);
3132 /* Return a set containing those elements in the shared domain
3133 * of the elements of list1 and list2 where each element in list1
3134 * has the relation specified by "fn" with each element in list2.
3136 static __isl_give isl_set
*pw_aff_list_set(__isl_take isl_pw_aff_list
*list1
,
3137 __isl_take isl_pw_aff_list
*list2
,
3138 __isl_give isl_set
*(*fn
)(__isl_take isl_pw_aff
*pwaff1
,
3139 __isl_take isl_pw_aff
*pwaff2
))
3145 if (!list1
|| !list2
)
3148 ctx
= isl_pw_aff_list_get_ctx(list1
);
3149 if (list1
->n
< 1 || list2
->n
< 1)
3150 isl_die(ctx
, isl_error_invalid
,
3151 "list should contain at least one element", goto error
);
3153 set
= isl_set_universe(isl_pw_aff_get_domain_space(list1
->p
[0]));
3154 for (i
= 0; i
< list1
->n
; ++i
)
3155 for (j
= 0; j
< list2
->n
; ++j
) {
3158 set_ij
= fn(isl_pw_aff_copy(list1
->p
[i
]),
3159 isl_pw_aff_copy(list2
->p
[j
]));
3160 set
= isl_set_intersect(set
, set_ij
);
3163 isl_pw_aff_list_free(list1
);
3164 isl_pw_aff_list_free(list2
);
3167 isl_pw_aff_list_free(list1
);
3168 isl_pw_aff_list_free(list2
);
3172 /* Return a set containing those elements in the shared domain
3173 * of the elements of list1 and list2 where each element in list1
3174 * is equal to each element in list2.
3176 __isl_give isl_set
*isl_pw_aff_list_eq_set(__isl_take isl_pw_aff_list
*list1
,
3177 __isl_take isl_pw_aff_list
*list2
)
3179 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_eq_set
);
3182 __isl_give isl_set
*isl_pw_aff_list_ne_set(__isl_take isl_pw_aff_list
*list1
,
3183 __isl_take isl_pw_aff_list
*list2
)
3185 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_ne_set
);
3188 /* Return a set containing those elements in the shared domain
3189 * of the elements of list1 and list2 where each element in list1
3190 * is less than or equal to each element in list2.
3192 __isl_give isl_set
*isl_pw_aff_list_le_set(__isl_take isl_pw_aff_list
*list1
,
3193 __isl_take isl_pw_aff_list
*list2
)
3195 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_le_set
);
3198 __isl_give isl_set
*isl_pw_aff_list_lt_set(__isl_take isl_pw_aff_list
*list1
,
3199 __isl_take isl_pw_aff_list
*list2
)
3201 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_lt_set
);
3204 __isl_give isl_set
*isl_pw_aff_list_ge_set(__isl_take isl_pw_aff_list
*list1
,
3205 __isl_take isl_pw_aff_list
*list2
)
3207 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_ge_set
);
3210 __isl_give isl_set
*isl_pw_aff_list_gt_set(__isl_take isl_pw_aff_list
*list1
,
3211 __isl_take isl_pw_aff_list
*list2
)
3213 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_gt_set
);
3217 /* Return a set containing those elements in the shared domain
3218 * of pwaff1 and pwaff2 where pwaff1 is not equal to pwaff2.
3220 static __isl_give isl_set
*pw_aff_ne_set(__isl_take isl_pw_aff
*pwaff1
,
3221 __isl_take isl_pw_aff
*pwaff2
)
3223 isl_set
*set_lt
, *set_gt
;
3225 set_lt
= isl_pw_aff_lt_set(isl_pw_aff_copy(pwaff1
),
3226 isl_pw_aff_copy(pwaff2
));
3227 set_gt
= isl_pw_aff_gt_set(pwaff1
, pwaff2
);
3228 return isl_set_union_disjoint(set_lt
, set_gt
);
3231 __isl_give isl_set
*isl_pw_aff_ne_set(__isl_take isl_pw_aff
*pwaff1
,
3232 __isl_take isl_pw_aff
*pwaff2
)
3234 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_ne_set
);
3237 __isl_give isl_pw_aff
*isl_pw_aff_scale_down(__isl_take isl_pw_aff
*pwaff
,
3242 if (isl_int_is_one(v
))
3244 if (!isl_int_is_pos(v
))
3245 isl_die(isl_pw_aff_get_ctx(pwaff
), isl_error_invalid
,
3246 "factor needs to be positive",
3247 return isl_pw_aff_free(pwaff
));
3248 pwaff
= isl_pw_aff_cow(pwaff
);
3254 for (i
= 0; i
< pwaff
->n
; ++i
) {
3255 pwaff
->p
[i
].aff
= isl_aff_scale_down(pwaff
->p
[i
].aff
, v
);
3256 if (!pwaff
->p
[i
].aff
)
3257 return isl_pw_aff_free(pwaff
);
3263 __isl_give isl_pw_aff
*isl_pw_aff_floor(__isl_take isl_pw_aff
*pwaff
)
3267 pwaff
= isl_pw_aff_cow(pwaff
);
3273 for (i
= 0; i
< pwaff
->n
; ++i
) {
3274 pwaff
->p
[i
].aff
= isl_aff_floor(pwaff
->p
[i
].aff
);
3275 if (!pwaff
->p
[i
].aff
)
3276 return isl_pw_aff_free(pwaff
);
3282 __isl_give isl_pw_aff
*isl_pw_aff_ceil(__isl_take isl_pw_aff
*pwaff
)
3286 pwaff
= isl_pw_aff_cow(pwaff
);
3292 for (i
= 0; i
< pwaff
->n
; ++i
) {
3293 pwaff
->p
[i
].aff
= isl_aff_ceil(pwaff
->p
[i
].aff
);
3294 if (!pwaff
->p
[i
].aff
)
3295 return isl_pw_aff_free(pwaff
);
3301 /* Assuming that "cond1" and "cond2" are disjoint,
3302 * return an affine expression that is equal to pwaff1 on cond1
3303 * and to pwaff2 on cond2.
3305 static __isl_give isl_pw_aff
*isl_pw_aff_select(
3306 __isl_take isl_set
*cond1
, __isl_take isl_pw_aff
*pwaff1
,
3307 __isl_take isl_set
*cond2
, __isl_take isl_pw_aff
*pwaff2
)
3309 pwaff1
= isl_pw_aff_intersect_domain(pwaff1
, cond1
);
3310 pwaff2
= isl_pw_aff_intersect_domain(pwaff2
, cond2
);
3312 return isl_pw_aff_add_disjoint(pwaff1
, pwaff2
);
3315 /* Return an affine expression that is equal to pwaff_true for elements
3316 * where "cond" is non-zero and to pwaff_false for elements where "cond"
3318 * That is, return cond ? pwaff_true : pwaff_false;
3320 * If "cond" involves and NaN, then we conservatively return a NaN
3321 * on its entire domain. In principle, we could consider the pieces
3322 * where it is NaN separately from those where it is not.
3324 * If "pwaff_true" and "pwaff_false" are obviously equal to each other,
3325 * then only use the domain of "cond" to restrict the domain.
3327 __isl_give isl_pw_aff
*isl_pw_aff_cond(__isl_take isl_pw_aff
*cond
,
3328 __isl_take isl_pw_aff
*pwaff_true
, __isl_take isl_pw_aff
*pwaff_false
)
3330 isl_set
*cond_true
, *cond_false
;
3335 if (isl_pw_aff_involves_nan(cond
)) {
3336 isl_space
*space
= isl_pw_aff_get_domain_space(cond
);
3337 isl_local_space
*ls
= isl_local_space_from_space(space
);
3338 isl_pw_aff_free(cond
);
3339 isl_pw_aff_free(pwaff_true
);
3340 isl_pw_aff_free(pwaff_false
);
3341 return isl_pw_aff_nan_on_domain(ls
);
3344 pwaff_true
= isl_pw_aff_align_params(pwaff_true
,
3345 isl_pw_aff_get_space(pwaff_false
));
3346 pwaff_false
= isl_pw_aff_align_params(pwaff_false
,
3347 isl_pw_aff_get_space(pwaff_true
));
3348 equal
= isl_pw_aff_plain_is_equal(pwaff_true
, pwaff_false
);
3354 dom
= isl_set_coalesce(isl_pw_aff_domain(cond
));
3355 isl_pw_aff_free(pwaff_false
);
3356 return isl_pw_aff_intersect_domain(pwaff_true
, dom
);
3359 cond_true
= isl_pw_aff_non_zero_set(isl_pw_aff_copy(cond
));
3360 cond_false
= isl_pw_aff_zero_set(cond
);
3361 return isl_pw_aff_select(cond_true
, pwaff_true
,
3362 cond_false
, pwaff_false
);
3364 isl_pw_aff_free(cond
);
3365 isl_pw_aff_free(pwaff_true
);
3366 isl_pw_aff_free(pwaff_false
);
3370 isl_bool
isl_aff_is_cst(__isl_keep isl_aff
*aff
)
3375 return isl_bool_error
;
3377 pos
= isl_seq_first_non_zero(aff
->v
->el
+ 2, aff
->v
->size
- 2);
3378 return isl_bool_ok(pos
== -1);
3381 /* Check whether pwaff is a piecewise constant.
3383 isl_bool
isl_pw_aff_is_cst(__isl_keep isl_pw_aff
*pwaff
)
3388 return isl_bool_error
;
3390 for (i
= 0; i
< pwaff
->n
; ++i
) {
3391 isl_bool is_cst
= isl_aff_is_cst(pwaff
->p
[i
].aff
);
3392 if (is_cst
< 0 || !is_cst
)
3396 return isl_bool_true
;
3399 /* Are all elements of "mpa" piecewise constants?
3401 isl_bool
isl_multi_pw_aff_is_cst(__isl_keep isl_multi_pw_aff
*mpa
)
3406 return isl_bool_error
;
3408 for (i
= 0; i
< mpa
->n
; ++i
) {
3409 isl_bool is_cst
= isl_pw_aff_is_cst(mpa
->u
.p
[i
]);
3410 if (is_cst
< 0 || !is_cst
)
3414 return isl_bool_true
;
3417 /* Return the product of "aff1" and "aff2".
3419 * If either of the two is NaN, then the result is NaN.
3421 * Otherwise, at least one of "aff1" or "aff2" needs to be a constant.
3423 __isl_give isl_aff
*isl_aff_mul(__isl_take isl_aff
*aff1
,
3424 __isl_take isl_aff
*aff2
)
3429 if (isl_aff_is_nan(aff1
)) {
3433 if (isl_aff_is_nan(aff2
)) {
3438 if (!isl_aff_is_cst(aff2
) && isl_aff_is_cst(aff1
))
3439 return isl_aff_mul(aff2
, aff1
);
3441 if (!isl_aff_is_cst(aff2
))
3442 isl_die(isl_aff_get_ctx(aff1
), isl_error_invalid
,
3443 "at least one affine expression should be constant",
3446 aff1
= isl_aff_cow(aff1
);
3450 aff1
= isl_aff_scale(aff1
, aff2
->v
->el
[1]);
3451 aff1
= isl_aff_scale_down(aff1
, aff2
->v
->el
[0]);
3461 /* Divide "aff1" by "aff2", assuming "aff2" is a constant.
3463 * If either of the two is NaN, then the result is NaN.
3465 __isl_give isl_aff
*isl_aff_div(__isl_take isl_aff
*aff1
,
3466 __isl_take isl_aff
*aff2
)
3474 if (isl_aff_is_nan(aff1
)) {
3478 if (isl_aff_is_nan(aff2
)) {
3483 is_cst
= isl_aff_is_cst(aff2
);
3487 isl_die(isl_aff_get_ctx(aff2
), isl_error_invalid
,
3488 "second argument should be a constant", goto error
);
3493 neg
= isl_int_is_neg(aff2
->v
->el
[1]);
3495 isl_int_neg(aff2
->v
->el
[0], aff2
->v
->el
[0]);
3496 isl_int_neg(aff2
->v
->el
[1], aff2
->v
->el
[1]);
3499 aff1
= isl_aff_scale(aff1
, aff2
->v
->el
[0]);
3500 aff1
= isl_aff_scale_down(aff1
, aff2
->v
->el
[1]);
3503 isl_int_neg(aff2
->v
->el
[0], aff2
->v
->el
[0]);
3504 isl_int_neg(aff2
->v
->el
[1], aff2
->v
->el
[1]);
3515 static __isl_give isl_pw_aff
*pw_aff_add(__isl_take isl_pw_aff
*pwaff1
,
3516 __isl_take isl_pw_aff
*pwaff2
)
3518 return isl_pw_aff_on_shared_domain(pwaff1
, pwaff2
, &isl_aff_add
);
3521 __isl_give isl_pw_aff
*isl_pw_aff_add(__isl_take isl_pw_aff
*pwaff1
,
3522 __isl_take isl_pw_aff
*pwaff2
)
3524 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
, &pw_aff_add
);
3527 __isl_give isl_pw_aff
*isl_pw_aff_union_add(__isl_take isl_pw_aff
*pwaff1
,
3528 __isl_take isl_pw_aff
*pwaff2
)
3530 return isl_pw_aff_union_add_(pwaff1
, pwaff2
);
3533 static __isl_give isl_pw_aff
*pw_aff_mul(__isl_take isl_pw_aff
*pwaff1
,
3534 __isl_take isl_pw_aff
*pwaff2
)
3536 return isl_pw_aff_on_shared_domain(pwaff1
, pwaff2
, &isl_aff_mul
);
3539 __isl_give isl_pw_aff
*isl_pw_aff_mul(__isl_take isl_pw_aff
*pwaff1
,
3540 __isl_take isl_pw_aff
*pwaff2
)
3542 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
, &pw_aff_mul
);
3545 static __isl_give isl_pw_aff
*pw_aff_div(__isl_take isl_pw_aff
*pa1
,
3546 __isl_take isl_pw_aff
*pa2
)
3548 return isl_pw_aff_on_shared_domain(pa1
, pa2
, &isl_aff_div
);
3551 /* Divide "pa1" by "pa2", assuming "pa2" is a piecewise constant.
3553 __isl_give isl_pw_aff
*isl_pw_aff_div(__isl_take isl_pw_aff
*pa1
,
3554 __isl_take isl_pw_aff
*pa2
)
3558 is_cst
= isl_pw_aff_is_cst(pa2
);
3562 isl_die(isl_pw_aff_get_ctx(pa2
), isl_error_invalid
,
3563 "second argument should be a piecewise constant",
3565 return isl_pw_aff_align_params_pw_pw_and(pa1
, pa2
, &pw_aff_div
);
3567 isl_pw_aff_free(pa1
);
3568 isl_pw_aff_free(pa2
);
3572 /* Compute the quotient of the integer division of "pa1" by "pa2"
3573 * with rounding towards zero.
3574 * "pa2" is assumed to be a piecewise constant.
3576 * In particular, return
3578 * pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2)
3581 __isl_give isl_pw_aff
*isl_pw_aff_tdiv_q(__isl_take isl_pw_aff
*pa1
,
3582 __isl_take isl_pw_aff
*pa2
)
3588 is_cst
= isl_pw_aff_is_cst(pa2
);
3592 isl_die(isl_pw_aff_get_ctx(pa2
), isl_error_invalid
,
3593 "second argument should be a piecewise constant",
3596 pa1
= isl_pw_aff_div(pa1
, pa2
);
3598 cond
= isl_pw_aff_nonneg_set(isl_pw_aff_copy(pa1
));
3599 f
= isl_pw_aff_floor(isl_pw_aff_copy(pa1
));
3600 c
= isl_pw_aff_ceil(pa1
);
3601 return isl_pw_aff_cond(isl_set_indicator_function(cond
), f
, c
);
3603 isl_pw_aff_free(pa1
);
3604 isl_pw_aff_free(pa2
);
3608 /* Compute the remainder of the integer division of "pa1" by "pa2"
3609 * with rounding towards zero.
3610 * "pa2" is assumed to be a piecewise constant.
3612 * In particular, return
3614 * pa1 - pa2 * (pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2))
3617 __isl_give isl_pw_aff
*isl_pw_aff_tdiv_r(__isl_take isl_pw_aff
*pa1
,
3618 __isl_take isl_pw_aff
*pa2
)
3623 is_cst
= isl_pw_aff_is_cst(pa2
);
3627 isl_die(isl_pw_aff_get_ctx(pa2
), isl_error_invalid
,
3628 "second argument should be a piecewise constant",
3630 res
= isl_pw_aff_tdiv_q(isl_pw_aff_copy(pa1
), isl_pw_aff_copy(pa2
));
3631 res
= isl_pw_aff_mul(pa2
, res
);
3632 res
= isl_pw_aff_sub(pa1
, res
);
3635 isl_pw_aff_free(pa1
);
3636 isl_pw_aff_free(pa2
);
3640 /* Does either of "pa1" or "pa2" involve any NaN2?
3642 static isl_bool
either_involves_nan(__isl_keep isl_pw_aff
*pa1
,
3643 __isl_keep isl_pw_aff
*pa2
)
3647 has_nan
= isl_pw_aff_involves_nan(pa1
);
3648 if (has_nan
< 0 || has_nan
)
3650 return isl_pw_aff_involves_nan(pa2
);
3653 /* Replace "pa1" and "pa2" (at least one of which involves a NaN)
3654 * by a NaN on their shared domain.
3656 * In principle, the result could be refined to only being NaN
3657 * on the parts of this domain where at least one of "pa1" or "pa2" is NaN.
3659 static __isl_give isl_pw_aff
*replace_by_nan(__isl_take isl_pw_aff
*pa1
,
3660 __isl_take isl_pw_aff
*pa2
)
3662 isl_local_space
*ls
;
3666 dom
= isl_set_intersect(isl_pw_aff_domain(pa1
), isl_pw_aff_domain(pa2
));
3667 ls
= isl_local_space_from_space(isl_set_get_space(dom
));
3668 pa
= isl_pw_aff_nan_on_domain(ls
);
3669 pa
= isl_pw_aff_intersect_domain(pa
, dom
);
3674 static __isl_give isl_pw_aff
*pw_aff_min(__isl_take isl_pw_aff
*pwaff1
,
3675 __isl_take isl_pw_aff
*pwaff2
)
3680 dom
= isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1
)),
3681 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2
)));
3682 le
= isl_pw_aff_le_set(isl_pw_aff_copy(pwaff1
),
3683 isl_pw_aff_copy(pwaff2
));
3684 dom
= isl_set_subtract(dom
, isl_set_copy(le
));
3685 return isl_pw_aff_select(le
, pwaff1
, dom
, pwaff2
);
3688 static __isl_give isl_pw_aff
*pw_aff_max(__isl_take isl_pw_aff
*pwaff1
,
3689 __isl_take isl_pw_aff
*pwaff2
)
3694 dom
= isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1
)),
3695 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2
)));
3696 ge
= isl_pw_aff_ge_set(isl_pw_aff_copy(pwaff1
),
3697 isl_pw_aff_copy(pwaff2
));
3698 dom
= isl_set_subtract(dom
, isl_set_copy(ge
));
3699 return isl_pw_aff_select(ge
, pwaff1
, dom
, pwaff2
);
3702 /* Return an expression for the minimum (if "max" is not set) or
3703 * the maximum (if "max" is set) of "pa1" and "pa2".
3704 * If either expression involves any NaN, then return a NaN
3705 * on the shared domain as result.
3707 static __isl_give isl_pw_aff
*pw_aff_min_max(__isl_take isl_pw_aff
*pa1
,
3708 __isl_take isl_pw_aff
*pa2
, int max
)
3712 has_nan
= either_involves_nan(pa1
, pa2
);
3714 pa1
= isl_pw_aff_free(pa1
);
3716 return replace_by_nan(pa1
, pa2
);
3719 return isl_pw_aff_align_params_pw_pw_and(pa1
, pa2
, &pw_aff_max
);
3721 return isl_pw_aff_align_params_pw_pw_and(pa1
, pa2
, &pw_aff_min
);
3724 /* Return an expression for the minimum of "pwaff1" and "pwaff2".
3726 __isl_give isl_pw_aff
*isl_pw_aff_min(__isl_take isl_pw_aff
*pwaff1
,
3727 __isl_take isl_pw_aff
*pwaff2
)
3729 return pw_aff_min_max(pwaff1
, pwaff2
, 0);
3732 /* Return an expression for the maximum of "pwaff1" and "pwaff2".
3734 __isl_give isl_pw_aff
*isl_pw_aff_max(__isl_take isl_pw_aff
*pwaff1
,
3735 __isl_take isl_pw_aff
*pwaff2
)
3737 return pw_aff_min_max(pwaff1
, pwaff2
, 1);
3740 static __isl_give isl_pw_aff
*pw_aff_list_reduce(
3741 __isl_take isl_pw_aff_list
*list
,
3742 __isl_give isl_pw_aff
*(*fn
)(__isl_take isl_pw_aff
*pwaff1
,
3743 __isl_take isl_pw_aff
*pwaff2
))
3752 ctx
= isl_pw_aff_list_get_ctx(list
);
3754 isl_die(ctx
, isl_error_invalid
,
3755 "list should contain at least one element", goto error
);
3757 res
= isl_pw_aff_copy(list
->p
[0]);
3758 for (i
= 1; i
< list
->n
; ++i
)
3759 res
= fn(res
, isl_pw_aff_copy(list
->p
[i
]));
3761 isl_pw_aff_list_free(list
);
3764 isl_pw_aff_list_free(list
);
3768 /* Return an isl_pw_aff that maps each element in the intersection of the
3769 * domains of the elements of list to the minimal corresponding affine
3772 __isl_give isl_pw_aff
*isl_pw_aff_list_min(__isl_take isl_pw_aff_list
*list
)
3774 return pw_aff_list_reduce(list
, &isl_pw_aff_min
);
3777 /* Return an isl_pw_aff that maps each element in the intersection of the
3778 * domains of the elements of list to the maximal corresponding affine
3781 __isl_give isl_pw_aff
*isl_pw_aff_list_max(__isl_take isl_pw_aff_list
*list
)
3783 return pw_aff_list_reduce(list
, &isl_pw_aff_max
);
3786 /* Mark the domains of "pwaff" as rational.
3788 __isl_give isl_pw_aff
*isl_pw_aff_set_rational(__isl_take isl_pw_aff
*pwaff
)
3792 pwaff
= isl_pw_aff_cow(pwaff
);
3798 for (i
= 0; i
< pwaff
->n
; ++i
) {
3799 pwaff
->p
[i
].set
= isl_set_set_rational(pwaff
->p
[i
].set
);
3800 if (!pwaff
->p
[i
].set
)
3801 return isl_pw_aff_free(pwaff
);
3807 /* Mark the domains of the elements of "list" as rational.
3809 __isl_give isl_pw_aff_list
*isl_pw_aff_list_set_rational(
3810 __isl_take isl_pw_aff_list
*list
)
3820 for (i
= 0; i
< n
; ++i
) {
3823 pa
= isl_pw_aff_list_get_pw_aff(list
, i
);
3824 pa
= isl_pw_aff_set_rational(pa
);
3825 list
= isl_pw_aff_list_set_pw_aff(list
, i
, pa
);
3831 /* Do the parameters of "aff" match those of "space"?
3833 isl_bool
isl_aff_matching_params(__isl_keep isl_aff
*aff
,
3834 __isl_keep isl_space
*space
)
3836 isl_space
*aff_space
;
3840 return isl_bool_error
;
3842 aff_space
= isl_aff_get_domain_space(aff
);
3844 match
= isl_space_has_equal_params(space
, aff_space
);
3846 isl_space_free(aff_space
);
3850 /* Check that the domain space of "aff" matches "space".
3852 isl_stat
isl_aff_check_match_domain_space(__isl_keep isl_aff
*aff
,
3853 __isl_keep isl_space
*space
)
3855 isl_space
*aff_space
;
3859 return isl_stat_error
;
3861 aff_space
= isl_aff_get_domain_space(aff
);
3863 match
= isl_space_has_equal_params(space
, aff_space
);
3867 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
3868 "parameters don't match", goto error
);
3869 match
= isl_space_tuple_is_equal(space
, isl_dim_in
,
3870 aff_space
, isl_dim_set
);
3874 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
3875 "domains don't match", goto error
);
3876 isl_space_free(aff_space
);
3879 isl_space_free(aff_space
);
3880 return isl_stat_error
;
3883 /* Return the shared (universe) domain of the elements of "ma".
3885 * Since an isl_multi_aff (and an isl_aff) is always total,
3886 * the domain is always the universe set in its domain space.
3887 * This is a helper function for use in the generic isl_multi_*_bind.
3889 static __isl_give isl_basic_set
*isl_multi_aff_domain(
3890 __isl_take isl_multi_aff
*ma
)
3894 space
= isl_multi_aff_get_space(ma
);
3895 isl_multi_aff_free(ma
);
3897 return isl_basic_set_universe(isl_space_domain(space
));
3903 #include <isl_multi_no_explicit_domain.c>
3904 #include <isl_multi_templ.c>
3905 #include <isl_multi_apply_set.c>
3906 #include <isl_multi_arith_templ.c>
3907 #include <isl_multi_bind_domain_templ.c>
3908 #include <isl_multi_cmp.c>
3909 #include <isl_multi_dim_id_templ.c>
3910 #include <isl_multi_dims.c>
3911 #include <isl_multi_floor.c>
3912 #include <isl_multi_from_base_templ.c>
3913 #include <isl_multi_identity_templ.c>
3914 #include <isl_multi_locals_templ.c>
3915 #include <isl_multi_move_dims_templ.c>
3916 #include <isl_multi_nan_templ.c>
3917 #include <isl_multi_product_templ.c>
3918 #include <isl_multi_splice_templ.c>
3919 #include <isl_multi_tuple_id_templ.c>
3920 #include <isl_multi_zero_templ.c>
3924 #include <isl_multi_gist.c>
3927 #define DOMBASE basic_set
3928 #include <isl_multi_bind_templ.c>
3930 /* Construct an isl_multi_aff living in "space" that corresponds
3931 * to the affine transformation matrix "mat".
3933 __isl_give isl_multi_aff
*isl_multi_aff_from_aff_mat(
3934 __isl_take isl_space
*space
, __isl_take isl_mat
*mat
)
3937 isl_local_space
*ls
= NULL
;
3938 isl_multi_aff
*ma
= NULL
;
3939 isl_size n_row
, n_col
, n_out
, total
;
3945 ctx
= isl_mat_get_ctx(mat
);
3947 n_row
= isl_mat_rows(mat
);
3948 n_col
= isl_mat_cols(mat
);
3949 n_out
= isl_space_dim(space
, isl_dim_out
);
3950 total
= isl_space_dim(space
, isl_dim_all
);
3951 if (n_row
< 0 || n_col
< 0 || n_out
< 0 || total
< 0)
3954 isl_die(ctx
, isl_error_invalid
,
3955 "insufficient number of rows", goto error
);
3957 isl_die(ctx
, isl_error_invalid
,
3958 "insufficient number of columns", goto error
);
3959 if (1 + n_out
!= n_row
|| 2 + total
!= n_row
+ n_col
)
3960 isl_die(ctx
, isl_error_invalid
,
3961 "dimension mismatch", goto error
);
3963 ma
= isl_multi_aff_zero(isl_space_copy(space
));
3964 ls
= isl_local_space_from_space(isl_space_domain(space
));
3966 for (i
= 0; i
< n_row
- 1; ++i
) {
3970 v
= isl_vec_alloc(ctx
, 1 + n_col
);
3973 isl_int_set(v
->el
[0], mat
->row
[0][0]);
3974 isl_seq_cpy(v
->el
+ 1, mat
->row
[1 + i
], n_col
);
3975 v
= isl_vec_normalize(v
);
3976 aff
= isl_aff_alloc_vec(isl_local_space_copy(ls
), v
);
3977 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
3980 isl_local_space_free(ls
);
3984 isl_local_space_free(ls
);
3986 isl_multi_aff_free(ma
);
3990 /* Remove any internal structure of the domain of "ma".
3991 * If there is any such internal structure in the input,
3992 * then the name of the corresponding space is also removed.
3994 __isl_give isl_multi_aff
*isl_multi_aff_flatten_domain(
3995 __isl_take isl_multi_aff
*ma
)
4002 if (!ma
->space
->nested
[0])
4005 space
= isl_multi_aff_get_space(ma
);
4006 space
= isl_space_flatten_domain(space
);
4007 ma
= isl_multi_aff_reset_space(ma
, space
);
4012 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
4013 * of the space to its domain.
4015 __isl_give isl_multi_aff
*isl_multi_aff_domain_map(__isl_take isl_space
*space
)
4019 isl_local_space
*ls
;
4024 if (!isl_space_is_map(space
))
4025 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
4026 "not a map space", goto error
);
4028 n_in
= isl_space_dim(space
, isl_dim_in
);
4031 space
= isl_space_domain_map(space
);
4033 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
4035 isl_space_free(space
);
4039 space
= isl_space_domain(space
);
4040 ls
= isl_local_space_from_space(space
);
4041 for (i
= 0; i
< n_in
; ++i
) {
4044 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
4046 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
4048 isl_local_space_free(ls
);
4051 isl_space_free(space
);
4055 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
4056 * of the space to its range.
4058 __isl_give isl_multi_aff
*isl_multi_aff_range_map(__isl_take isl_space
*space
)
4061 isl_size n_in
, n_out
;
4062 isl_local_space
*ls
;
4067 if (!isl_space_is_map(space
))
4068 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
4069 "not a map space", goto error
);
4071 n_in
= isl_space_dim(space
, isl_dim_in
);
4072 n_out
= isl_space_dim(space
, isl_dim_out
);
4073 if (n_in
< 0 || n_out
< 0)
4075 space
= isl_space_range_map(space
);
4077 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
4079 isl_space_free(space
);
4083 space
= isl_space_domain(space
);
4084 ls
= isl_local_space_from_space(space
);
4085 for (i
= 0; i
< n_out
; ++i
) {
4088 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
4089 isl_dim_set
, n_in
+ i
);
4090 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
4092 isl_local_space_free(ls
);
4095 isl_space_free(space
);
4099 /* Given a map space, return an isl_pw_multi_aff that maps a wrapped copy
4100 * of the space to its range.
4102 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_range_map(
4103 __isl_take isl_space
*space
)
4105 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_range_map(space
));
4108 /* Given the space of a set and a range of set dimensions,
4109 * construct an isl_multi_aff that projects out those dimensions.
4111 __isl_give isl_multi_aff
*isl_multi_aff_project_out_map(
4112 __isl_take isl_space
*space
, enum isl_dim_type type
,
4113 unsigned first
, unsigned n
)
4117 isl_local_space
*ls
;
4122 if (!isl_space_is_set(space
))
4123 isl_die(isl_space_get_ctx(space
), isl_error_unsupported
,
4124 "expecting set space", goto error
);
4125 if (type
!= isl_dim_set
)
4126 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
4127 "only set dimensions can be projected out", goto error
);
4128 if (isl_space_check_range(space
, type
, first
, n
) < 0)
4131 dim
= isl_space_dim(space
, isl_dim_set
);
4135 space
= isl_space_from_domain(space
);
4136 space
= isl_space_add_dims(space
, isl_dim_out
, dim
- n
);
4139 return isl_multi_aff_alloc(space
);
4141 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
4142 space
= isl_space_domain(space
);
4143 ls
= isl_local_space_from_space(space
);
4145 for (i
= 0; i
< first
; ++i
) {
4148 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
4150 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
4153 for (i
= 0; i
< dim
- (first
+ n
); ++i
) {
4156 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
4157 isl_dim_set
, first
+ n
+ i
);
4158 ma
= isl_multi_aff_set_aff(ma
, first
+ i
, aff
);
4161 isl_local_space_free(ls
);
4164 isl_space_free(space
);
4168 /* Given the space of a set and a range of set dimensions,
4169 * construct an isl_pw_multi_aff that projects out those dimensions.
4171 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_project_out_map(
4172 __isl_take isl_space
*space
, enum isl_dim_type type
,
4173 unsigned first
, unsigned n
)
4177 ma
= isl_multi_aff_project_out_map(space
, type
, first
, n
);
4178 return isl_pw_multi_aff_from_multi_aff(ma
);
4181 /* Create an isl_pw_multi_aff with the given isl_multi_aff on a universe
4184 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_multi_aff(
4185 __isl_take isl_multi_aff
*ma
)
4187 isl_set
*dom
= isl_set_universe(isl_multi_aff_get_domain_space(ma
));
4188 return isl_pw_multi_aff_alloc(dom
, ma
);
4191 /* Create a piecewise multi-affine expression in the given space that maps each
4192 * input dimension to the corresponding output dimension.
4194 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_identity(
4195 __isl_take isl_space
*space
)
4197 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_identity(space
));
4200 /* Exploit the equalities in "eq" to simplify the affine expressions.
4202 static __isl_give isl_multi_aff
*isl_multi_aff_substitute_equalities(
4203 __isl_take isl_multi_aff
*maff
, __isl_take isl_basic_set
*eq
)
4207 maff
= isl_multi_aff_cow(maff
);
4211 for (i
= 0; i
< maff
->n
; ++i
) {
4212 maff
->u
.p
[i
] = isl_aff_substitute_equalities(maff
->u
.p
[i
],
4213 isl_basic_set_copy(eq
));
4218 isl_basic_set_free(eq
);
4221 isl_basic_set_free(eq
);
4222 isl_multi_aff_free(maff
);
4226 __isl_give isl_multi_aff
*isl_multi_aff_scale(__isl_take isl_multi_aff
*maff
,
4231 maff
= isl_multi_aff_cow(maff
);
4235 for (i
= 0; i
< maff
->n
; ++i
) {
4236 maff
->u
.p
[i
] = isl_aff_scale(maff
->u
.p
[i
], f
);
4238 return isl_multi_aff_free(maff
);
4244 __isl_give isl_multi_aff
*isl_multi_aff_add_on_domain(__isl_keep isl_set
*dom
,
4245 __isl_take isl_multi_aff
*maff1
, __isl_take isl_multi_aff
*maff2
)
4247 maff1
= isl_multi_aff_add(maff1
, maff2
);
4248 maff1
= isl_multi_aff_gist(maff1
, isl_set_copy(dom
));
4252 int isl_multi_aff_is_empty(__isl_keep isl_multi_aff
*maff
)
4260 /* Return the set of domain elements where "ma1" is lexicographically
4261 * smaller than or equal to "ma2".
4263 __isl_give isl_set
*isl_multi_aff_lex_le_set(__isl_take isl_multi_aff
*ma1
,
4264 __isl_take isl_multi_aff
*ma2
)
4266 return isl_multi_aff_lex_ge_set(ma2
, ma1
);
4269 /* Return the set of domain elements where "ma1" is lexicographically
4270 * smaller than "ma2".
4272 __isl_give isl_set
*isl_multi_aff_lex_lt_set(__isl_take isl_multi_aff
*ma1
,
4273 __isl_take isl_multi_aff
*ma2
)
4275 return isl_multi_aff_lex_gt_set(ma2
, ma1
);
4278 /* Return the set of domain elements where "ma1" and "ma2"
4281 static __isl_give isl_set
*isl_multi_aff_order_set(
4282 __isl_take isl_multi_aff
*ma1
, __isl_take isl_multi_aff
*ma2
,
4283 __isl_give isl_map
*order(__isl_take isl_space
*set_space
))
4286 isl_map
*map1
, *map2
;
4289 map1
= isl_map_from_multi_aff_internal(ma1
);
4290 map2
= isl_map_from_multi_aff_internal(ma2
);
4291 map
= isl_map_range_product(map1
, map2
);
4292 space
= isl_space_range(isl_map_get_space(map
));
4293 space
= isl_space_domain(isl_space_unwrap(space
));
4295 map
= isl_map_intersect_range(map
, isl_map_wrap(ge
));
4297 return isl_map_domain(map
);
4300 /* Return the set of domain elements where "ma1" is lexicographically
4301 * greater than or equal to "ma2".
4303 __isl_give isl_set
*isl_multi_aff_lex_ge_set(__isl_take isl_multi_aff
*ma1
,
4304 __isl_take isl_multi_aff
*ma2
)
4306 return isl_multi_aff_order_set(ma1
, ma2
, &isl_map_lex_ge
);
4309 /* Return the set of domain elements where "ma1" is lexicographically
4310 * greater than "ma2".
4312 __isl_give isl_set
*isl_multi_aff_lex_gt_set(__isl_take isl_multi_aff
*ma1
,
4313 __isl_take isl_multi_aff
*ma2
)
4315 return isl_multi_aff_order_set(ma1
, ma2
, &isl_map_lex_gt
);
4319 #define PW isl_pw_multi_aff
4321 #define EL isl_multi_aff
4323 #define EL_IS_ZERO is_empty
4327 #define IS_ZERO is_empty
4330 #undef DEFAULT_IS_ZERO
4331 #define DEFAULT_IS_ZERO 0
4335 #define NO_INSERT_DIMS
4339 #include <isl_pw_templ.c>
4340 #include <isl_pw_bind_domain_templ.c>
4341 #include <isl_pw_union_opt.c>
4346 #define BASE pw_multi_aff
4348 #include <isl_union_multi.c>
4349 #include <isl_union_neg.c>
4351 static __isl_give isl_pw_multi_aff
*pw_multi_aff_union_lexmax(
4352 __isl_take isl_pw_multi_aff
*pma1
,
4353 __isl_take isl_pw_multi_aff
*pma2
)
4355 return isl_pw_multi_aff_union_opt_cmp(pma1
, pma2
,
4356 &isl_multi_aff_lex_ge_set
);
4359 /* Given two piecewise multi affine expressions, return a piecewise
4360 * multi-affine expression defined on the union of the definition domains
4361 * of the inputs that is equal to the lexicographic maximum of the two
4362 * inputs on each cell. If only one of the two inputs is defined on
4363 * a given cell, then it is considered to be the maximum.
4365 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_union_lexmax(
4366 __isl_take isl_pw_multi_aff
*pma1
,
4367 __isl_take isl_pw_multi_aff
*pma2
)
4369 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
4370 &pw_multi_aff_union_lexmax
);
4373 static __isl_give isl_pw_multi_aff
*pw_multi_aff_union_lexmin(
4374 __isl_take isl_pw_multi_aff
*pma1
,
4375 __isl_take isl_pw_multi_aff
*pma2
)
4377 return isl_pw_multi_aff_union_opt_cmp(pma1
, pma2
,
4378 &isl_multi_aff_lex_le_set
);
4381 /* Given two piecewise multi affine expressions, return a piecewise
4382 * multi-affine expression defined on the union of the definition domains
4383 * of the inputs that is equal to the lexicographic minimum of the two
4384 * inputs on each cell. If only one of the two inputs is defined on
4385 * a given cell, then it is considered to be the minimum.
4387 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_union_lexmin(
4388 __isl_take isl_pw_multi_aff
*pma1
,
4389 __isl_take isl_pw_multi_aff
*pma2
)
4391 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
4392 &pw_multi_aff_union_lexmin
);
4395 static __isl_give isl_pw_multi_aff
*pw_multi_aff_add(
4396 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4398 return isl_pw_multi_aff_on_shared_domain(pma1
, pma2
,
4399 &isl_multi_aff_add
);
4402 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_add(
4403 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4405 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
4409 static __isl_give isl_pw_multi_aff
*pw_multi_aff_sub(
4410 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4412 return isl_pw_multi_aff_on_shared_domain(pma1
, pma2
,
4413 &isl_multi_aff_sub
);
4416 /* Subtract "pma2" from "pma1" and return the result.
4418 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_sub(
4419 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4421 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
4425 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_union_add(
4426 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4428 return isl_pw_multi_aff_union_add_(pma1
, pma2
);
4431 /* Compute the sum of "upa1" and "upa2" on the union of their domains,
4432 * with the actual sum on the shared domain and
4433 * the defined expression on the symmetric difference of the domains.
4435 __isl_give isl_union_pw_aff
*isl_union_pw_aff_union_add(
4436 __isl_take isl_union_pw_aff
*upa1
, __isl_take isl_union_pw_aff
*upa2
)
4438 return isl_union_pw_aff_union_add_(upa1
, upa2
);
4441 /* Compute the sum of "upma1" and "upma2" on the union of their domains,
4442 * with the actual sum on the shared domain and
4443 * the defined expression on the symmetric difference of the domains.
4445 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_union_add(
4446 __isl_take isl_union_pw_multi_aff
*upma1
,
4447 __isl_take isl_union_pw_multi_aff
*upma2
)
4449 return isl_union_pw_multi_aff_union_add_(upma1
, upma2
);
4452 /* Given two piecewise multi-affine expressions A -> B and C -> D,
4453 * construct a piecewise multi-affine expression [A -> C] -> [B -> D].
4455 static __isl_give isl_pw_multi_aff
*pw_multi_aff_product(
4456 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4460 isl_pw_multi_aff
*res
;
4465 n
= pma1
->n
* pma2
->n
;
4466 space
= isl_space_product(isl_space_copy(pma1
->dim
),
4467 isl_space_copy(pma2
->dim
));
4468 res
= isl_pw_multi_aff_alloc_size(space
, n
);
4470 for (i
= 0; i
< pma1
->n
; ++i
) {
4471 for (j
= 0; j
< pma2
->n
; ++j
) {
4475 domain
= isl_set_product(isl_set_copy(pma1
->p
[i
].set
),
4476 isl_set_copy(pma2
->p
[j
].set
));
4477 ma
= isl_multi_aff_product(
4478 isl_multi_aff_copy(pma1
->p
[i
].maff
),
4479 isl_multi_aff_copy(pma2
->p
[j
].maff
));
4480 res
= isl_pw_multi_aff_add_piece(res
, domain
, ma
);
4484 isl_pw_multi_aff_free(pma1
);
4485 isl_pw_multi_aff_free(pma2
);
4488 isl_pw_multi_aff_free(pma1
);
4489 isl_pw_multi_aff_free(pma2
);
4493 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_product(
4494 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4496 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
4497 &pw_multi_aff_product
);
4500 /* Subtract the initial "n" elements in "ma" with coefficients in "c" and
4501 * denominator "denom".
4502 * "denom" is allowed to be negative, in which case the actual denominator
4503 * is -denom and the expressions are added instead.
4505 static __isl_give isl_aff
*subtract_initial(__isl_take isl_aff
*aff
,
4506 __isl_keep isl_multi_aff
*ma
, int n
, isl_int
*c
, isl_int denom
)
4512 first
= isl_seq_first_non_zero(c
, n
);
4516 sign
= isl_int_sgn(denom
);
4518 isl_int_abs(d
, denom
);
4519 for (i
= first
; i
< n
; ++i
) {
4522 if (isl_int_is_zero(c
[i
]))
4524 aff_i
= isl_multi_aff_get_aff(ma
, i
);
4525 aff_i
= isl_aff_scale(aff_i
, c
[i
]);
4526 aff_i
= isl_aff_scale_down(aff_i
, d
);
4528 aff
= isl_aff_sub(aff
, aff_i
);
4530 aff
= isl_aff_add(aff
, aff_i
);
4537 /* Extract an affine expression that expresses the output dimension "pos"
4538 * of "bmap" in terms of the parameters and input dimensions from
4540 * Note that this expression may involve integer divisions defined
4541 * in terms of parameters and input dimensions.
4542 * The equality may also involve references to earlier (but not later)
4543 * output dimensions. These are replaced by the corresponding elements
4546 * If the equality is of the form
4548 * f(i) + h(j) + a x + g(i) = 0,
4550 * with f(i) a linear combinations of the parameters and input dimensions,
4551 * g(i) a linear combination of integer divisions defined in terms of the same
4552 * and h(j) a linear combinations of earlier output dimensions,
4553 * then the affine expression is
4555 * (-f(i) - g(i))/a - h(j)/a
4557 * If the equality is of the form
4559 * f(i) + h(j) - a x + g(i) = 0,
4561 * then the affine expression is
4563 * (f(i) + g(i))/a - h(j)/(-a)
4566 * If "div" refers to an integer division (i.e., it is smaller than
4567 * the number of integer divisions), then the equality constraint
4568 * does involve an integer division (the one at position "div") that
4569 * is defined in terms of output dimensions. However, this integer
4570 * division can be eliminated by exploiting a pair of constraints
4571 * x >= l and x <= l + n, with n smaller than the coefficient of "div"
4572 * in the equality constraint. "ineq" refers to inequality x >= l, i.e.,
4574 * In particular, let
4576 * x = e(i) + m floor(...)
4578 * with e(i) the expression derived above and floor(...) the integer
4579 * division involving output dimensions.
4590 * e(i) + m floor(...) - l = (e(i) + m floor(...) - l) mod m
4591 * = (e(i) - l) mod m
4595 * x - l = (e(i) - l) mod m
4599 * x = ((e(i) - l) mod m) + l
4601 * The variable "shift" below contains the expression -l, which may
4602 * also involve a linear combination of earlier output dimensions.
4604 static __isl_give isl_aff
*extract_aff_from_equality(
4605 __isl_keep isl_basic_map
*bmap
, int pos
, int eq
, int div
, int ineq
,
4606 __isl_keep isl_multi_aff
*ma
)
4609 isl_size n_div
, n_out
;
4611 isl_local_space
*ls
;
4612 isl_aff
*aff
, *shift
;
4615 ctx
= isl_basic_map_get_ctx(bmap
);
4616 ls
= isl_basic_map_get_local_space(bmap
);
4617 ls
= isl_local_space_domain(ls
);
4618 aff
= isl_aff_alloc(isl_local_space_copy(ls
));
4621 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
4622 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4623 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4624 if (n_out
< 0 || n_div
< 0)
4626 if (isl_int_is_neg(bmap
->eq
[eq
][o_out
+ pos
])) {
4627 isl_seq_cpy(aff
->v
->el
+ 1, bmap
->eq
[eq
], o_out
);
4628 isl_seq_cpy(aff
->v
->el
+ 1 + o_out
,
4629 bmap
->eq
[eq
] + o_out
+ n_out
, n_div
);
4631 isl_seq_neg(aff
->v
->el
+ 1, bmap
->eq
[eq
], o_out
);
4632 isl_seq_neg(aff
->v
->el
+ 1 + o_out
,
4633 bmap
->eq
[eq
] + o_out
+ n_out
, n_div
);
4636 isl_int_set_si(aff
->v
->el
[1 + o_out
+ div
], 0);
4637 isl_int_abs(aff
->v
->el
[0], bmap
->eq
[eq
][o_out
+ pos
]);
4638 aff
= subtract_initial(aff
, ma
, pos
, bmap
->eq
[eq
] + o_out
,
4639 bmap
->eq
[eq
][o_out
+ pos
]);
4641 shift
= isl_aff_alloc(isl_local_space_copy(ls
));
4644 isl_seq_cpy(shift
->v
->el
+ 1, bmap
->ineq
[ineq
], o_out
);
4645 isl_seq_cpy(shift
->v
->el
+ 1 + o_out
,
4646 bmap
->ineq
[ineq
] + o_out
+ n_out
, n_div
);
4647 isl_int_set_si(shift
->v
->el
[0], 1);
4648 shift
= subtract_initial(shift
, ma
, pos
,
4649 bmap
->ineq
[ineq
] + o_out
, ctx
->negone
);
4650 aff
= isl_aff_add(aff
, isl_aff_copy(shift
));
4651 mod
= isl_val_int_from_isl_int(ctx
,
4652 bmap
->eq
[eq
][o_out
+ n_out
+ div
]);
4653 mod
= isl_val_abs(mod
);
4654 aff
= isl_aff_mod_val(aff
, mod
);
4655 aff
= isl_aff_sub(aff
, shift
);
4658 isl_local_space_free(ls
);
4661 isl_local_space_free(ls
);
4666 /* Given a basic map with output dimensions defined
4667 * in terms of the parameters input dimensions and earlier
4668 * output dimensions using an equality (and possibly a pair on inequalities),
4669 * extract an isl_aff that expresses output dimension "pos" in terms
4670 * of the parameters and input dimensions.
4671 * Note that this expression may involve integer divisions defined
4672 * in terms of parameters and input dimensions.
4673 * "ma" contains the expressions corresponding to earlier output dimensions.
4675 * This function shares some similarities with
4676 * isl_basic_map_has_defining_equality and isl_constraint_get_bound.
4678 static __isl_give isl_aff
*extract_isl_aff_from_basic_map(
4679 __isl_keep isl_basic_map
*bmap
, int pos
, __isl_keep isl_multi_aff
*ma
)
4686 eq
= isl_basic_map_output_defining_equality(bmap
, pos
, &div
, &ineq
);
4687 if (eq
>= bmap
->n_eq
)
4688 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
4689 "unable to find suitable equality", return NULL
);
4690 aff
= extract_aff_from_equality(bmap
, pos
, eq
, div
, ineq
, ma
);
4692 aff
= isl_aff_remove_unused_divs(aff
);
4696 /* Given a basic map where each output dimension is defined
4697 * in terms of the parameters and input dimensions using an equality,
4698 * extract an isl_multi_aff that expresses the output dimensions in terms
4699 * of the parameters and input dimensions.
4701 static __isl_give isl_multi_aff
*extract_isl_multi_aff_from_basic_map(
4702 __isl_take isl_basic_map
*bmap
)
4711 ma
= isl_multi_aff_alloc(isl_basic_map_get_space(bmap
));
4712 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4714 ma
= isl_multi_aff_free(ma
);
4716 for (i
= 0; i
< n_out
; ++i
) {
4719 aff
= extract_isl_aff_from_basic_map(bmap
, i
, ma
);
4720 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
4723 isl_basic_map_free(bmap
);
4728 /* Given a basic set where each set dimension is defined
4729 * in terms of the parameters using an equality,
4730 * extract an isl_multi_aff that expresses the set dimensions in terms
4731 * of the parameters.
4733 __isl_give isl_multi_aff
*isl_multi_aff_from_basic_set_equalities(
4734 __isl_take isl_basic_set
*bset
)
4736 return extract_isl_multi_aff_from_basic_map(bset
);
4739 /* Create an isl_pw_multi_aff that is equivalent to
4740 * isl_map_intersect_domain(isl_map_from_basic_map(bmap), domain).
4741 * The given basic map is such that each output dimension is defined
4742 * in terms of the parameters and input dimensions using an equality.
4744 * Since some applications expect the result of isl_pw_multi_aff_from_map
4745 * to only contain integer affine expressions, we compute the floor
4746 * of the expression before returning.
4748 * Remove all constraints involving local variables without
4749 * an explicit representation (resulting in the removal of those
4750 * local variables) prior to the actual extraction to ensure
4751 * that the local spaces in which the resulting affine expressions
4752 * are created do not contain any unknown local variables.
4753 * Removing such constraints is safe because constraints involving
4754 * unknown local variables are not used to determine whether
4755 * a basic map is obviously single-valued.
4757 static __isl_give isl_pw_multi_aff
*plain_pw_multi_aff_from_map(
4758 __isl_take isl_set
*domain
, __isl_take isl_basic_map
*bmap
)
4762 bmap
= isl_basic_map_drop_constraint_involving_unknown_divs(bmap
);
4763 ma
= extract_isl_multi_aff_from_basic_map(bmap
);
4764 ma
= isl_multi_aff_floor(ma
);
4765 return isl_pw_multi_aff_alloc(domain
, ma
);
4768 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4769 * This obviously only works if the input "map" is single-valued.
4770 * If so, we compute the lexicographic minimum of the image in the form
4771 * of an isl_pw_multi_aff. Since the image is unique, it is equal
4772 * to its lexicographic minimum.
4773 * If the input is not single-valued, we produce an error.
4775 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_base(
4776 __isl_take isl_map
*map
)
4780 isl_pw_multi_aff
*pma
;
4782 sv
= isl_map_is_single_valued(map
);
4786 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
4787 "map is not single-valued", goto error
);
4788 map
= isl_map_make_disjoint(map
);
4792 pma
= isl_pw_multi_aff_empty(isl_map_get_space(map
));
4794 for (i
= 0; i
< map
->n
; ++i
) {
4795 isl_pw_multi_aff
*pma_i
;
4796 isl_basic_map
*bmap
;
4797 bmap
= isl_basic_map_copy(map
->p
[i
]);
4798 pma_i
= isl_basic_map_lexmin_pw_multi_aff(bmap
);
4799 pma
= isl_pw_multi_aff_add_disjoint(pma
, pma_i
);
4809 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
4810 * taking into account that the output dimension at position "d"
4811 * can be represented as
4813 * x = floor((e(...) + c1) / m)
4815 * given that constraint "i" is of the form
4817 * e(...) + c1 - m x >= 0
4820 * Let "map" be of the form
4824 * We construct a mapping
4826 * A -> [A -> x = floor(...)]
4828 * apply that to the map, obtaining
4830 * [A -> x = floor(...)] -> B
4832 * and equate dimension "d" to x.
4833 * We then compute a isl_pw_multi_aff representation of the resulting map
4834 * and plug in the mapping above.
4836 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_div(
4837 __isl_take isl_map
*map
, __isl_take isl_basic_map
*hull
, int d
, int i
)
4840 isl_space
*space
= NULL
;
4841 isl_local_space
*ls
;
4849 isl_pw_multi_aff
*pma
;
4852 is_set
= isl_map_is_set(map
);
4856 offset
= isl_basic_map_offset(hull
, isl_dim_out
);
4857 ctx
= isl_map_get_ctx(map
);
4858 space
= isl_space_domain(isl_map_get_space(map
));
4859 n_in
= isl_space_dim(space
, isl_dim_set
);
4860 n
= isl_space_dim(space
, isl_dim_all
);
4861 if (n_in
< 0 || n
< 0)
4864 v
= isl_vec_alloc(ctx
, 1 + 1 + n
);
4866 isl_int_neg(v
->el
[0], hull
->ineq
[i
][offset
+ d
]);
4867 isl_seq_cpy(v
->el
+ 1, hull
->ineq
[i
], 1 + n
);
4869 isl_basic_map_free(hull
);
4871 ls
= isl_local_space_from_space(isl_space_copy(space
));
4872 aff
= isl_aff_alloc_vec(ls
, v
);
4873 aff
= isl_aff_floor(aff
);
4875 isl_space_free(space
);
4876 ma
= isl_multi_aff_from_aff(aff
);
4878 ma
= isl_multi_aff_identity(isl_space_map_from_set(space
));
4879 ma
= isl_multi_aff_range_product(ma
,
4880 isl_multi_aff_from_aff(aff
));
4883 insert
= isl_map_from_multi_aff_internal(isl_multi_aff_copy(ma
));
4884 map
= isl_map_apply_domain(map
, insert
);
4885 map
= isl_map_equate(map
, isl_dim_in
, n_in
, isl_dim_out
, d
);
4886 pma
= isl_pw_multi_aff_from_map(map
);
4887 pma
= isl_pw_multi_aff_pullback_multi_aff(pma
, ma
);
4891 isl_space_free(space
);
4893 isl_basic_map_free(hull
);
4897 /* Is constraint "c" of the form
4899 * e(...) + c1 - m x >= 0
4903 * -e(...) + c2 + m x >= 0
4905 * where m > 1 and e only depends on parameters and input dimemnsions?
4907 * "offset" is the offset of the output dimensions
4908 * "pos" is the position of output dimension x.
4910 static int is_potential_div_constraint(isl_int
*c
, int offset
, int d
, int total
)
4912 if (isl_int_is_zero(c
[offset
+ d
]))
4914 if (isl_int_is_one(c
[offset
+ d
]))
4916 if (isl_int_is_negone(c
[offset
+ d
]))
4918 if (isl_seq_first_non_zero(c
+ offset
, d
) != -1)
4920 if (isl_seq_first_non_zero(c
+ offset
+ d
+ 1,
4921 total
- (offset
+ d
+ 1)) != -1)
4926 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4928 * As a special case, we first check if there is any pair of constraints,
4929 * shared by all the basic maps in "map" that force a given dimension
4930 * to be equal to the floor of some affine combination of the input dimensions.
4932 * In particular, if we can find two constraints
4934 * e(...) + c1 - m x >= 0 i.e., m x <= e(...) + c1
4938 * -e(...) + c2 + m x >= 0 i.e., m x >= e(...) - c2
4940 * where m > 1 and e only depends on parameters and input dimemnsions,
4943 * c1 + c2 < m i.e., -c2 >= c1 - (m - 1)
4945 * then we know that we can take
4947 * x = floor((e(...) + c1) / m)
4949 * without having to perform any computation.
4951 * Note that we know that
4955 * If c1 + c2 were 0, then we would have detected an equality during
4956 * simplification. If c1 + c2 were negative, then we would have detected
4959 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_check_div(
4960 __isl_take isl_map
*map
)
4968 isl_basic_map
*hull
;
4970 hull
= isl_map_unshifted_simple_hull(isl_map_copy(map
));
4971 dim
= isl_map_dim(map
, isl_dim_out
);
4972 total
= isl_basic_map_dim(hull
, isl_dim_all
);
4973 if (dim
< 0 || total
< 0)
4977 offset
= isl_basic_map_offset(hull
, isl_dim_out
);
4979 for (d
= 0; d
< dim
; ++d
) {
4980 for (i
= 0; i
< n
; ++i
) {
4981 if (!is_potential_div_constraint(hull
->ineq
[i
],
4982 offset
, d
, 1 + total
))
4984 for (j
= i
+ 1; j
< n
; ++j
) {
4985 if (!isl_seq_is_neg(hull
->ineq
[i
] + 1,
4986 hull
->ineq
[j
] + 1, total
))
4988 isl_int_add(sum
, hull
->ineq
[i
][0],
4990 if (isl_int_abs_lt(sum
,
4991 hull
->ineq
[i
][offset
+ d
]))
4998 if (isl_int_is_pos(hull
->ineq
[j
][offset
+ d
]))
5000 return pw_multi_aff_from_map_div(map
, hull
, d
, j
);
5004 isl_basic_map_free(hull
);
5005 return pw_multi_aff_from_map_base(map
);
5008 isl_basic_map_free(hull
);
5012 /* Given an affine expression
5014 * [A -> B] -> f(A,B)
5016 * construct an isl_multi_aff
5020 * such that dimension "d" in B' is set to "aff" and the remaining
5021 * dimensions are set equal to the corresponding dimensions in B.
5022 * "n_in" is the dimension of the space A.
5023 * "n_out" is the dimension of the space B.
5025 * If "is_set" is set, then the affine expression is of the form
5029 * and we construct an isl_multi_aff
5033 static __isl_give isl_multi_aff
*range_map(__isl_take isl_aff
*aff
, int d
,
5034 unsigned n_in
, unsigned n_out
, int is_set
)
5038 isl_space
*space
, *space2
;
5039 isl_local_space
*ls
;
5041 space
= isl_aff_get_domain_space(aff
);
5042 ls
= isl_local_space_from_space(isl_space_copy(space
));
5043 space2
= isl_space_copy(space
);
5045 space2
= isl_space_range(isl_space_unwrap(space2
));
5046 space
= isl_space_map_from_domain_and_range(space
, space2
);
5047 ma
= isl_multi_aff_alloc(space
);
5048 ma
= isl_multi_aff_set_aff(ma
, d
, aff
);
5050 for (i
= 0; i
< n_out
; ++i
) {
5053 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
5054 isl_dim_set
, n_in
+ i
);
5055 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
5058 isl_local_space_free(ls
);
5063 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
5064 * taking into account that the dimension at position "d" can be written as
5066 * x = m a + f(..) (1)
5068 * where m is equal to "gcd".
5069 * "i" is the index of the equality in "hull" that defines f(..).
5070 * In particular, the equality is of the form
5072 * f(..) - x + m g(existentials) = 0
5076 * -f(..) + x + m g(existentials) = 0
5078 * We basically plug (1) into "map", resulting in a map with "a"
5079 * in the range instead of "x". The corresponding isl_pw_multi_aff
5080 * defining "a" is then plugged back into (1) to obtain a definition for "x".
5082 * Specifically, given the input map
5086 * We first wrap it into a set
5090 * and define (1) on top of the corresponding space, resulting in "aff".
5091 * We use this to create an isl_multi_aff that maps the output position "d"
5092 * from "a" to "x", leaving all other (intput and output) dimensions unchanged.
5093 * We plug this into the wrapped map, unwrap the result and compute the
5094 * corresponding isl_pw_multi_aff.
5095 * The result is an expression
5103 * so that we can plug that into "aff", after extending the latter to
5109 * If "map" is actually a set, then there is no "A" space, meaning
5110 * that we do not need to perform any wrapping, and that the result
5111 * of the recursive call is of the form
5115 * which is plugged into a mapping of the form
5119 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_stride(
5120 __isl_take isl_map
*map
, __isl_take isl_basic_map
*hull
, int d
, int i
,
5125 isl_local_space
*ls
;
5128 isl_pw_multi_aff
*pma
, *id
;
5134 is_set
= isl_map_is_set(map
);
5138 n_in
= isl_basic_map_dim(hull
, isl_dim_in
);
5139 n_out
= isl_basic_map_dim(hull
, isl_dim_out
);
5140 if (n_in
< 0 || n_out
< 0)
5142 o_out
= isl_basic_map_offset(hull
, isl_dim_out
);
5147 set
= isl_map_wrap(map
);
5148 space
= isl_space_map_from_set(isl_set_get_space(set
));
5149 ma
= isl_multi_aff_identity(space
);
5150 ls
= isl_local_space_from_space(isl_set_get_space(set
));
5151 aff
= isl_aff_alloc(ls
);
5153 isl_int_set_si(aff
->v
->el
[0], 1);
5154 if (isl_int_is_one(hull
->eq
[i
][o_out
+ d
]))
5155 isl_seq_neg(aff
->v
->el
+ 1, hull
->eq
[i
],
5158 isl_seq_cpy(aff
->v
->el
+ 1, hull
->eq
[i
],
5160 isl_int_set(aff
->v
->el
[1 + o_out
+ d
], gcd
);
5162 ma
= isl_multi_aff_set_aff(ma
, n_in
+ d
, isl_aff_copy(aff
));
5163 set
= isl_set_preimage_multi_aff(set
, ma
);
5165 ma
= range_map(aff
, d
, n_in
, n_out
, is_set
);
5170 map
= isl_set_unwrap(set
);
5171 pma
= isl_pw_multi_aff_from_map(map
);
5174 space
= isl_pw_multi_aff_get_domain_space(pma
);
5175 space
= isl_space_map_from_set(space
);
5176 id
= isl_pw_multi_aff_identity(space
);
5177 pma
= isl_pw_multi_aff_range_product(id
, pma
);
5179 id
= isl_pw_multi_aff_from_multi_aff(ma
);
5180 pma
= isl_pw_multi_aff_pullback_pw_multi_aff(id
, pma
);
5182 isl_basic_map_free(hull
);
5186 isl_basic_map_free(hull
);
5190 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5191 * "hull" contains the equalities valid for "map".
5193 * Check if any of the output dimensions is "strided".
5194 * That is, we check if it can be written as
5198 * with m greater than 1, a some combination of existentially quantified
5199 * variables and f an expression in the parameters and input dimensions.
5200 * If so, we remove the stride in pw_multi_aff_from_map_stride.
5202 * Otherwise, we continue with pw_multi_aff_from_map_check_div for a further
5205 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_check_strides(
5206 __isl_take isl_map
*map
, __isl_take isl_basic_map
*hull
)
5215 n_div
= isl_basic_map_dim(hull
, isl_dim_div
);
5216 n_out
= isl_basic_map_dim(hull
, isl_dim_out
);
5217 if (n_div
< 0 || n_out
< 0)
5221 isl_basic_map_free(hull
);
5222 return pw_multi_aff_from_map_check_div(map
);
5227 o_div
= isl_basic_map_offset(hull
, isl_dim_div
);
5228 o_out
= isl_basic_map_offset(hull
, isl_dim_out
);
5230 for (i
= 0; i
< n_out
; ++i
) {
5231 for (j
= 0; j
< hull
->n_eq
; ++j
) {
5232 isl_int
*eq
= hull
->eq
[j
];
5233 isl_pw_multi_aff
*res
;
5235 if (!isl_int_is_one(eq
[o_out
+ i
]) &&
5236 !isl_int_is_negone(eq
[o_out
+ i
]))
5238 if (isl_seq_first_non_zero(eq
+ o_out
, i
) != -1)
5240 if (isl_seq_first_non_zero(eq
+ o_out
+ i
+ 1,
5241 n_out
- (i
+ 1)) != -1)
5243 isl_seq_gcd(eq
+ o_div
, n_div
, &gcd
);
5244 if (isl_int_is_zero(gcd
))
5246 if (isl_int_is_one(gcd
))
5249 res
= pw_multi_aff_from_map_stride(map
, hull
,
5257 isl_basic_map_free(hull
);
5258 return pw_multi_aff_from_map_check_div(map
);
5261 isl_basic_map_free(hull
);
5265 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5267 * As a special case, we first check if all output dimensions are uniquely
5268 * defined in terms of the parameters and input dimensions over the entire
5269 * domain. If so, we extract the desired isl_pw_multi_aff directly
5270 * from the affine hull of "map" and its domain.
5272 * Otherwise, continue with pw_multi_aff_from_map_check_strides for more
5275 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_map(__isl_take isl_map
*map
)
5279 isl_basic_map
*hull
;
5281 n
= isl_map_n_basic_map(map
);
5286 hull
= isl_map_unshifted_simple_hull(isl_map_copy(map
));
5287 hull
= isl_basic_map_plain_affine_hull(hull
);
5288 sv
= isl_basic_map_plain_is_single_valued(hull
);
5290 return plain_pw_multi_aff_from_map(isl_map_domain(map
),
5292 isl_basic_map_free(hull
);
5294 map
= isl_map_detect_equalities(map
);
5295 hull
= isl_map_unshifted_simple_hull(isl_map_copy(map
));
5296 sv
= isl_basic_map_plain_is_single_valued(hull
);
5298 return plain_pw_multi_aff_from_map(isl_map_domain(map
), hull
);
5300 return pw_multi_aff_from_map_check_strides(map
, hull
);
5301 isl_basic_map_free(hull
);
5307 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_set(__isl_take isl_set
*set
)
5309 return isl_pw_multi_aff_from_map(set
);
5312 /* Convert "map" into an isl_pw_multi_aff (if possible) and
5315 static isl_stat
pw_multi_aff_from_map(__isl_take isl_map
*map
, void *user
)
5317 isl_union_pw_multi_aff
**upma
= user
;
5318 isl_pw_multi_aff
*pma
;
5320 pma
= isl_pw_multi_aff_from_map(map
);
5321 *upma
= isl_union_pw_multi_aff_add_pw_multi_aff(*upma
, pma
);
5323 return *upma
? isl_stat_ok
: isl_stat_error
;
5326 /* Create an isl_union_pw_multi_aff with the given isl_aff on a universe
5329 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_aff(
5330 __isl_take isl_aff
*aff
)
5333 isl_pw_multi_aff
*pma
;
5335 ma
= isl_multi_aff_from_aff(aff
);
5336 pma
= isl_pw_multi_aff_from_multi_aff(ma
);
5337 return isl_union_pw_multi_aff_from_pw_multi_aff(pma
);
5340 /* Try and create an isl_union_pw_multi_aff that is equivalent
5341 * to the given isl_union_map.
5342 * The isl_union_map is required to be single-valued in each space.
5343 * Otherwise, an error is produced.
5345 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_union_map(
5346 __isl_take isl_union_map
*umap
)
5349 isl_union_pw_multi_aff
*upma
;
5351 space
= isl_union_map_get_space(umap
);
5352 upma
= isl_union_pw_multi_aff_empty(space
);
5353 if (isl_union_map_foreach_map(umap
, &pw_multi_aff_from_map
, &upma
) < 0)
5354 upma
= isl_union_pw_multi_aff_free(upma
);
5355 isl_union_map_free(umap
);
5360 /* Try and create an isl_union_pw_multi_aff that is equivalent
5361 * to the given isl_union_set.
5362 * The isl_union_set is required to be a singleton in each space.
5363 * Otherwise, an error is produced.
5365 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_union_set(
5366 __isl_take isl_union_set
*uset
)
5368 return isl_union_pw_multi_aff_from_union_map(uset
);
5371 /* Return the piecewise affine expression "set ? 1 : 0".
5373 __isl_give isl_pw_aff
*isl_set_indicator_function(__isl_take isl_set
*set
)
5376 isl_space
*space
= isl_set_get_space(set
);
5377 isl_local_space
*ls
= isl_local_space_from_space(space
);
5378 isl_aff
*zero
= isl_aff_zero_on_domain(isl_local_space_copy(ls
));
5379 isl_aff
*one
= isl_aff_zero_on_domain(ls
);
5381 one
= isl_aff_add_constant_si(one
, 1);
5382 pa
= isl_pw_aff_alloc(isl_set_copy(set
), one
);
5383 set
= isl_set_complement(set
);
5384 pa
= isl_pw_aff_add_disjoint(pa
, isl_pw_aff_alloc(set
, zero
));
5389 /* Plug in "subs" for dimension "type", "pos" of "aff".
5391 * Let i be the dimension to replace and let "subs" be of the form
5395 * and "aff" of the form
5401 * (a f + d g')/(m d)
5403 * where g' is the result of plugging in "subs" in each of the integer
5406 __isl_give isl_aff
*isl_aff_substitute(__isl_take isl_aff
*aff
,
5407 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
5413 aff
= isl_aff_cow(aff
);
5415 return isl_aff_free(aff
);
5417 ctx
= isl_aff_get_ctx(aff
);
5418 if (!isl_space_is_equal(aff
->ls
->dim
, subs
->ls
->dim
))
5419 isl_die(ctx
, isl_error_invalid
,
5420 "spaces don't match", return isl_aff_free(aff
));
5421 n_div
= isl_local_space_dim(subs
->ls
, isl_dim_div
);
5423 return isl_aff_free(aff
);
5425 isl_die(ctx
, isl_error_unsupported
,
5426 "cannot handle divs yet", return isl_aff_free(aff
));
5428 aff
->ls
= isl_local_space_substitute(aff
->ls
, type
, pos
, subs
);
5430 return isl_aff_free(aff
);
5432 aff
->v
= isl_vec_cow(aff
->v
);
5434 return isl_aff_free(aff
);
5436 pos
+= isl_local_space_offset(aff
->ls
, type
);
5439 isl_seq_substitute(aff
->v
->el
, pos
, subs
->v
->el
,
5440 aff
->v
->size
, subs
->v
->size
, v
);
5446 /* Plug in "subs" for dimension "type", "pos" in each of the affine
5447 * expressions in "maff".
5449 __isl_give isl_multi_aff
*isl_multi_aff_substitute(
5450 __isl_take isl_multi_aff
*maff
, enum isl_dim_type type
, unsigned pos
,
5451 __isl_keep isl_aff
*subs
)
5455 maff
= isl_multi_aff_cow(maff
);
5457 return isl_multi_aff_free(maff
);
5459 if (type
== isl_dim_in
)
5462 for (i
= 0; i
< maff
->n
; ++i
) {
5463 maff
->u
.p
[i
] = isl_aff_substitute(maff
->u
.p
[i
],
5466 return isl_multi_aff_free(maff
);
5472 /* Plug in "subs" for dimension "type", "pos" of "pma".
5474 * pma is of the form
5478 * while subs is of the form
5480 * v' = B_j(v) -> S_j
5482 * Each pair i,j such that C_ij = A_i \cap B_i is non-empty
5483 * has a contribution in the result, in particular
5485 * C_ij(S_j) -> M_i(S_j)
5487 * Note that plugging in S_j in C_ij may also result in an empty set
5488 * and this contribution should simply be discarded.
5490 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_substitute(
5491 __isl_take isl_pw_multi_aff
*pma
, enum isl_dim_type type
, unsigned pos
,
5492 __isl_keep isl_pw_aff
*subs
)
5495 isl_pw_multi_aff
*res
;
5498 return isl_pw_multi_aff_free(pma
);
5500 n
= pma
->n
* subs
->n
;
5501 res
= isl_pw_multi_aff_alloc_size(isl_space_copy(pma
->dim
), n
);
5503 for (i
= 0; i
< pma
->n
; ++i
) {
5504 for (j
= 0; j
< subs
->n
; ++j
) {
5506 isl_multi_aff
*res_ij
;
5509 common
= isl_set_intersect(
5510 isl_set_copy(pma
->p
[i
].set
),
5511 isl_set_copy(subs
->p
[j
].set
));
5512 common
= isl_set_substitute(common
,
5513 type
, pos
, subs
->p
[j
].aff
);
5514 empty
= isl_set_plain_is_empty(common
);
5515 if (empty
< 0 || empty
) {
5516 isl_set_free(common
);
5522 res_ij
= isl_multi_aff_substitute(
5523 isl_multi_aff_copy(pma
->p
[i
].maff
),
5524 type
, pos
, subs
->p
[j
].aff
);
5526 res
= isl_pw_multi_aff_add_piece(res
, common
, res_ij
);
5530 isl_pw_multi_aff_free(pma
);
5533 isl_pw_multi_aff_free(pma
);
5534 isl_pw_multi_aff_free(res
);
5538 /* Compute the preimage of a range of dimensions in the affine expression "src"
5539 * under "ma" and put the result in "dst". The number of dimensions in "src"
5540 * that precede the range is given by "n_before". The number of dimensions
5541 * in the range is given by the number of output dimensions of "ma".
5542 * The number of dimensions that follow the range is given by "n_after".
5543 * If "has_denom" is set (to one),
5544 * then "src" and "dst" have an extra initial denominator.
5545 * "n_div_ma" is the number of existentials in "ma"
5546 * "n_div_bset" is the number of existentials in "src"
5547 * The resulting "dst" (which is assumed to have been allocated by
5548 * the caller) contains coefficients for both sets of existentials,
5549 * first those in "ma" and then those in "src".
5550 * f, c1, c2 and g are temporary objects that have been initialized
5553 * Let src represent the expression
5555 * (a(p) + f_u u + b v + f_w w + c(divs))/d
5557 * and let ma represent the expressions
5559 * v_i = (r_i(p) + s_i(y) + t_i(divs'))/m_i
5561 * We start out with the following expression for dst:
5563 * (a(p) + f_u u + 0 y + f_w w + 0 divs' + c(divs) + f \sum_i b_i v_i)/d
5565 * with the multiplication factor f initially equal to 1
5566 * and f \sum_i b_i v_i kept separately.
5567 * For each x_i that we substitute, we multiply the numerator
5568 * (and denominator) of dst by c_1 = m_i and add the numerator
5569 * of the x_i expression multiplied by c_2 = f b_i,
5570 * after removing the common factors of c_1 and c_2.
5571 * The multiplication factor f also needs to be multiplied by c_1
5572 * for the next x_j, j > i.
5574 isl_stat
isl_seq_preimage(isl_int
*dst
, isl_int
*src
,
5575 __isl_keep isl_multi_aff
*ma
, int n_before
, int n_after
,
5576 int n_div_ma
, int n_div_bmap
,
5577 isl_int f
, isl_int c1
, isl_int c2
, isl_int g
, int has_denom
)
5580 isl_size n_param
, n_in
, n_out
;
5583 n_param
= isl_multi_aff_dim(ma
, isl_dim_param
);
5584 n_in
= isl_multi_aff_dim(ma
, isl_dim_in
);
5585 n_out
= isl_multi_aff_dim(ma
, isl_dim_out
);
5586 if (n_param
< 0 || n_in
< 0 || n_out
< 0)
5587 return isl_stat_error
;
5589 isl_seq_cpy(dst
, src
, has_denom
+ 1 + n_param
+ n_before
);
5590 o_dst
= o_src
= has_denom
+ 1 + n_param
+ n_before
;
5591 isl_seq_clr(dst
+ o_dst
, n_in
);
5594 isl_seq_cpy(dst
+ o_dst
, src
+ o_src
, n_after
);
5597 isl_seq_clr(dst
+ o_dst
, n_div_ma
);
5599 isl_seq_cpy(dst
+ o_dst
, src
+ o_src
, n_div_bmap
);
5601 isl_int_set_si(f
, 1);
5603 for (i
= 0; i
< n_out
; ++i
) {
5604 int offset
= has_denom
+ 1 + n_param
+ n_before
+ i
;
5606 if (isl_int_is_zero(src
[offset
]))
5608 isl_int_set(c1
, ma
->u
.p
[i
]->v
->el
[0]);
5609 isl_int_mul(c2
, f
, src
[offset
]);
5610 isl_int_gcd(g
, c1
, c2
);
5611 isl_int_divexact(c1
, c1
, g
);
5612 isl_int_divexact(c2
, c2
, g
);
5614 isl_int_mul(f
, f
, c1
);
5617 isl_seq_combine(dst
+ o_dst
, c1
, dst
+ o_dst
,
5618 c2
, ma
->u
.p
[i
]->v
->el
+ o_src
, 1 + n_param
);
5619 o_dst
+= 1 + n_param
;
5620 o_src
+= 1 + n_param
;
5621 isl_seq_scale(dst
+ o_dst
, dst
+ o_dst
, c1
, n_before
);
5623 isl_seq_combine(dst
+ o_dst
, c1
, dst
+ o_dst
,
5624 c2
, ma
->u
.p
[i
]->v
->el
+ o_src
, n_in
);
5627 isl_seq_scale(dst
+ o_dst
, dst
+ o_dst
, c1
, n_after
);
5629 isl_seq_combine(dst
+ o_dst
, c1
, dst
+ o_dst
,
5630 c2
, ma
->u
.p
[i
]->v
->el
+ o_src
, n_div_ma
);
5633 isl_seq_scale(dst
+ o_dst
, dst
+ o_dst
, c1
, n_div_bmap
);
5635 isl_int_mul(dst
[0], dst
[0], c1
);
5641 /* Compute the pullback of "aff" by the function represented by "ma".
5642 * In other words, plug in "ma" in "aff". The result is an affine expression
5643 * defined over the domain space of "ma".
5645 * If "aff" is represented by
5647 * (a(p) + b x + c(divs))/d
5649 * and ma is represented by
5651 * x = D(p) + F(y) + G(divs')
5653 * then the result is
5655 * (a(p) + b D(p) + b F(y) + b G(divs') + c(divs))/d
5657 * The divs in the local space of the input are similarly adjusted
5658 * through a call to isl_local_space_preimage_multi_aff.
5660 __isl_give isl_aff
*isl_aff_pullback_multi_aff(__isl_take isl_aff
*aff
,
5661 __isl_take isl_multi_aff
*ma
)
5663 isl_aff
*res
= NULL
;
5664 isl_local_space
*ls
;
5665 isl_size n_div_aff
, n_div_ma
;
5666 isl_int f
, c1
, c2
, g
;
5668 ma
= isl_multi_aff_align_divs(ma
);
5672 n_div_aff
= isl_aff_dim(aff
, isl_dim_div
);
5673 n_div_ma
= ma
->n
? isl_aff_dim(ma
->u
.p
[0], isl_dim_div
) : 0;
5674 if (n_div_aff
< 0 || n_div_ma
< 0)
5677 ls
= isl_aff_get_domain_local_space(aff
);
5678 ls
= isl_local_space_preimage_multi_aff(ls
, isl_multi_aff_copy(ma
));
5679 res
= isl_aff_alloc(ls
);
5688 if (isl_seq_preimage(res
->v
->el
, aff
->v
->el
, ma
, 0, 0,
5689 n_div_ma
, n_div_aff
, f
, c1
, c2
, g
, 1) < 0)
5690 res
= isl_aff_free(res
);
5698 isl_multi_aff_free(ma
);
5699 res
= isl_aff_normalize(res
);
5703 isl_multi_aff_free(ma
);
5708 /* Compute the pullback of "aff1" by the function represented by "aff2".
5709 * In other words, plug in "aff2" in "aff1". The result is an affine expression
5710 * defined over the domain space of "aff1".
5712 * The domain of "aff1" should match the range of "aff2", which means
5713 * that it should be single-dimensional.
5715 __isl_give isl_aff
*isl_aff_pullback_aff(__isl_take isl_aff
*aff1
,
5716 __isl_take isl_aff
*aff2
)
5720 ma
= isl_multi_aff_from_aff(aff2
);
5721 return isl_aff_pullback_multi_aff(aff1
, ma
);
5724 /* Compute the pullback of "ma1" by the function represented by "ma2".
5725 * In other words, plug in "ma2" in "ma1".
5727 * The parameters of "ma1" and "ma2" are assumed to have been aligned.
5729 static __isl_give isl_multi_aff
*isl_multi_aff_pullback_multi_aff_aligned(
5730 __isl_take isl_multi_aff
*ma1
, __isl_take isl_multi_aff
*ma2
)
5733 isl_space
*space
= NULL
;
5735 ma2
= isl_multi_aff_align_divs(ma2
);
5736 ma1
= isl_multi_aff_cow(ma1
);
5740 space
= isl_space_join(isl_multi_aff_get_space(ma2
),
5741 isl_multi_aff_get_space(ma1
));
5743 for (i
= 0; i
< ma1
->n
; ++i
) {
5744 ma1
->u
.p
[i
] = isl_aff_pullback_multi_aff(ma1
->u
.p
[i
],
5745 isl_multi_aff_copy(ma2
));
5750 ma1
= isl_multi_aff_reset_space(ma1
, space
);
5751 isl_multi_aff_free(ma2
);
5754 isl_space_free(space
);
5755 isl_multi_aff_free(ma2
);
5756 isl_multi_aff_free(ma1
);
5760 /* Compute the pullback of "ma1" by the function represented by "ma2".
5761 * In other words, plug in "ma2" in "ma1".
5763 __isl_give isl_multi_aff
*isl_multi_aff_pullback_multi_aff(
5764 __isl_take isl_multi_aff
*ma1
, __isl_take isl_multi_aff
*ma2
)
5766 return isl_multi_aff_align_params_multi_multi_and(ma1
, ma2
,
5767 &isl_multi_aff_pullback_multi_aff_aligned
);
5770 /* Extend the local space of "dst" to include the divs
5771 * in the local space of "src".
5773 * If "src" does not have any divs or if the local spaces of "dst" and
5774 * "src" are the same, then no extension is required.
5776 __isl_give isl_aff
*isl_aff_align_divs(__isl_take isl_aff
*dst
,
5777 __isl_keep isl_aff
*src
)
5780 isl_size src_n_div
, dst_n_div
;
5787 return isl_aff_free(dst
);
5789 ctx
= isl_aff_get_ctx(src
);
5790 equal
= isl_local_space_has_equal_space(src
->ls
, dst
->ls
);
5792 return isl_aff_free(dst
);
5794 isl_die(ctx
, isl_error_invalid
,
5795 "spaces don't match", goto error
);
5797 src_n_div
= isl_local_space_dim(src
->ls
, isl_dim_div
);
5798 dst_n_div
= isl_local_space_dim(dst
->ls
, isl_dim_div
);
5801 equal
= isl_local_space_is_equal(src
->ls
, dst
->ls
);
5802 if (equal
< 0 || src_n_div
< 0 || dst_n_div
< 0)
5803 return isl_aff_free(dst
);
5807 exp1
= isl_alloc_array(ctx
, int, src_n_div
);
5808 exp2
= isl_alloc_array(ctx
, int, dst_n_div
);
5809 if (!exp1
|| (dst_n_div
&& !exp2
))
5812 div
= isl_merge_divs(src
->ls
->div
, dst
->ls
->div
, exp1
, exp2
);
5813 dst
= isl_aff_expand_divs(dst
, div
, exp2
);
5821 return isl_aff_free(dst
);
5824 /* Adjust the local spaces of the affine expressions in "maff"
5825 * such that they all have the save divs.
5827 __isl_give isl_multi_aff
*isl_multi_aff_align_divs(
5828 __isl_take isl_multi_aff
*maff
)
5836 maff
= isl_multi_aff_cow(maff
);
5840 for (i
= 1; i
< maff
->n
; ++i
)
5841 maff
->u
.p
[0] = isl_aff_align_divs(maff
->u
.p
[0], maff
->u
.p
[i
]);
5842 for (i
= 1; i
< maff
->n
; ++i
) {
5843 maff
->u
.p
[i
] = isl_aff_align_divs(maff
->u
.p
[i
], maff
->u
.p
[0]);
5845 return isl_multi_aff_free(maff
);
5851 __isl_give isl_aff
*isl_aff_lift(__isl_take isl_aff
*aff
)
5853 aff
= isl_aff_cow(aff
);
5857 aff
->ls
= isl_local_space_lift(aff
->ls
);
5859 return isl_aff_free(aff
);
5864 /* Lift "maff" to a space with extra dimensions such that the result
5865 * has no more existentially quantified variables.
5866 * If "ls" is not NULL, then *ls is assigned the local space that lies
5867 * at the basis of the lifting applied to "maff".
5869 __isl_give isl_multi_aff
*isl_multi_aff_lift(__isl_take isl_multi_aff
*maff
,
5870 __isl_give isl_local_space
**ls
)
5884 isl_space
*space
= isl_multi_aff_get_domain_space(maff
);
5885 *ls
= isl_local_space_from_space(space
);
5887 return isl_multi_aff_free(maff
);
5892 maff
= isl_multi_aff_cow(maff
);
5893 maff
= isl_multi_aff_align_divs(maff
);
5897 n_div
= isl_aff_dim(maff
->u
.p
[0], isl_dim_div
);
5899 return isl_multi_aff_free(maff
);
5900 space
= isl_multi_aff_get_space(maff
);
5901 space
= isl_space_lift(isl_space_domain(space
), n_div
);
5902 space
= isl_space_extend_domain_with_range(space
,
5903 isl_multi_aff_get_space(maff
));
5905 return isl_multi_aff_free(maff
);
5906 isl_space_free(maff
->space
);
5907 maff
->space
= space
;
5910 *ls
= isl_aff_get_domain_local_space(maff
->u
.p
[0]);
5912 return isl_multi_aff_free(maff
);
5915 for (i
= 0; i
< maff
->n
; ++i
) {
5916 maff
->u
.p
[i
] = isl_aff_lift(maff
->u
.p
[i
]);
5924 isl_local_space_free(*ls
);
5925 return isl_multi_aff_free(maff
);
5929 #define TYPE isl_pw_multi_aff
5931 #include "check_type_range_templ.c"
5933 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma".
5935 __isl_give isl_pw_aff
*isl_pw_multi_aff_get_pw_aff(
5936 __isl_keep isl_pw_multi_aff
*pma
, int pos
)
5943 if (isl_pw_multi_aff_check_range(pma
, isl_dim_out
, pos
, 1) < 0)
5946 n_out
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
5950 space
= isl_pw_multi_aff_get_space(pma
);
5951 space
= isl_space_drop_dims(space
, isl_dim_out
,
5952 pos
+ 1, n_out
- pos
- 1);
5953 space
= isl_space_drop_dims(space
, isl_dim_out
, 0, pos
);
5955 pa
= isl_pw_aff_alloc_size(space
, pma
->n
);
5956 for (i
= 0; i
< pma
->n
; ++i
) {
5958 aff
= isl_multi_aff_get_aff(pma
->p
[i
].maff
, pos
);
5959 pa
= isl_pw_aff_add_piece(pa
, isl_set_copy(pma
->p
[i
].set
), aff
);
5965 /* Return an isl_pw_multi_aff with the given "set" as domain and
5966 * an unnamed zero-dimensional range.
5968 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_domain(
5969 __isl_take isl_set
*set
)
5974 space
= isl_set_get_space(set
);
5975 space
= isl_space_from_domain(space
);
5976 ma
= isl_multi_aff_zero(space
);
5977 return isl_pw_multi_aff_alloc(set
, ma
);
5980 /* Add an isl_pw_multi_aff with the given "set" as domain and
5981 * an unnamed zero-dimensional range to *user.
5983 static isl_stat
add_pw_multi_aff_from_domain(__isl_take isl_set
*set
,
5986 isl_union_pw_multi_aff
**upma
= user
;
5987 isl_pw_multi_aff
*pma
;
5989 pma
= isl_pw_multi_aff_from_domain(set
);
5990 *upma
= isl_union_pw_multi_aff_add_pw_multi_aff(*upma
, pma
);
5995 /* Return an isl_union_pw_multi_aff with the given "uset" as domain and
5996 * an unnamed zero-dimensional range.
5998 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_domain(
5999 __isl_take isl_union_set
*uset
)
6002 isl_union_pw_multi_aff
*upma
;
6007 space
= isl_union_set_get_space(uset
);
6008 upma
= isl_union_pw_multi_aff_empty(space
);
6010 if (isl_union_set_foreach_set(uset
,
6011 &add_pw_multi_aff_from_domain
, &upma
) < 0)
6014 isl_union_set_free(uset
);
6017 isl_union_set_free(uset
);
6018 isl_union_pw_multi_aff_free(upma
);
6022 /* Local data for bin_entry and the callback "fn".
6024 struct isl_union_pw_multi_aff_bin_data
{
6025 isl_union_pw_multi_aff
*upma2
;
6026 isl_union_pw_multi_aff
*res
;
6027 isl_pw_multi_aff
*pma
;
6028 isl_stat (*fn
)(__isl_take isl_pw_multi_aff
*pma
, void *user
);
6031 /* Given an isl_pw_multi_aff from upma1, store it in data->pma
6032 * and call data->fn for each isl_pw_multi_aff in data->upma2.
6034 static isl_stat
bin_entry(__isl_take isl_pw_multi_aff
*pma
, void *user
)
6036 struct isl_union_pw_multi_aff_bin_data
*data
= user
;
6040 r
= isl_union_pw_multi_aff_foreach_pw_multi_aff(data
->upma2
,
6042 isl_pw_multi_aff_free(pma
);
6047 /* Call "fn" on each pair of isl_pw_multi_affs in "upma1" and "upma2".
6048 * The isl_pw_multi_aff from upma1 is stored in data->pma (where data is
6049 * passed as user field) and the isl_pw_multi_aff from upma2 is available
6050 * as *entry. The callback should adjust data->res if desired.
6052 static __isl_give isl_union_pw_multi_aff
*bin_op(
6053 __isl_take isl_union_pw_multi_aff
*upma1
,
6054 __isl_take isl_union_pw_multi_aff
*upma2
,
6055 isl_stat (*fn
)(__isl_take isl_pw_multi_aff
*pma
, void *user
))
6058 struct isl_union_pw_multi_aff_bin_data data
= { NULL
, NULL
, NULL
, fn
};
6060 space
= isl_union_pw_multi_aff_get_space(upma2
);
6061 upma1
= isl_union_pw_multi_aff_align_params(upma1
, space
);
6062 space
= isl_union_pw_multi_aff_get_space(upma1
);
6063 upma2
= isl_union_pw_multi_aff_align_params(upma2
, space
);
6065 if (!upma1
|| !upma2
)
6069 data
.res
= isl_union_pw_multi_aff_alloc_same_size(upma1
);
6070 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma1
,
6071 &bin_entry
, &data
) < 0)
6074 isl_union_pw_multi_aff_free(upma1
);
6075 isl_union_pw_multi_aff_free(upma2
);
6078 isl_union_pw_multi_aff_free(upma1
);
6079 isl_union_pw_multi_aff_free(upma2
);
6080 isl_union_pw_multi_aff_free(data
.res
);
6084 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
6085 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
6087 static __isl_give isl_pw_multi_aff
*pw_multi_aff_range_product(
6088 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
6092 space
= isl_space_range_product(isl_pw_multi_aff_get_space(pma1
),
6093 isl_pw_multi_aff_get_space(pma2
));
6094 return isl_pw_multi_aff_on_shared_domain_in(pma1
, pma2
, space
,
6095 &isl_multi_aff_range_product
);
6098 /* Given two isl_pw_multi_affs A -> B and C -> D,
6099 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
6101 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_range_product(
6102 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
6104 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
6105 &pw_multi_aff_range_product
);
6108 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
6109 * construct an isl_pw_multi_aff (A * C) -> (B, D).
6111 static __isl_give isl_pw_multi_aff
*pw_multi_aff_flat_range_product(
6112 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
6116 space
= isl_space_range_product(isl_pw_multi_aff_get_space(pma1
),
6117 isl_pw_multi_aff_get_space(pma2
));
6118 space
= isl_space_flatten_range(space
);
6119 return isl_pw_multi_aff_on_shared_domain_in(pma1
, pma2
, space
,
6120 &isl_multi_aff_flat_range_product
);
6123 /* Given two isl_pw_multi_affs A -> B and C -> D,
6124 * construct an isl_pw_multi_aff (A * C) -> (B, D).
6126 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_flat_range_product(
6127 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
6129 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
6130 &pw_multi_aff_flat_range_product
);
6133 /* If data->pma and "pma2" have the same domain space, then compute
6134 * their flat range product and the result to data->res.
6136 static isl_stat
flat_range_product_entry(__isl_take isl_pw_multi_aff
*pma2
,
6139 struct isl_union_pw_multi_aff_bin_data
*data
= user
;
6141 if (!isl_space_tuple_is_equal(data
->pma
->dim
, isl_dim_in
,
6142 pma2
->dim
, isl_dim_in
)) {
6143 isl_pw_multi_aff_free(pma2
);
6147 pma2
= isl_pw_multi_aff_flat_range_product(
6148 isl_pw_multi_aff_copy(data
->pma
), pma2
);
6150 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma2
);
6155 /* Given two isl_union_pw_multi_affs A -> B and C -> D,
6156 * construct an isl_union_pw_multi_aff (A * C) -> (B, D).
6158 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_flat_range_product(
6159 __isl_take isl_union_pw_multi_aff
*upma1
,
6160 __isl_take isl_union_pw_multi_aff
*upma2
)
6162 return bin_op(upma1
, upma2
, &flat_range_product_entry
);
6165 /* Replace the affine expressions at position "pos" in "pma" by "pa".
6166 * The parameters are assumed to have been aligned.
6168 * The implementation essentially performs an isl_pw_*_on_shared_domain,
6169 * except that it works on two different isl_pw_* types.
6171 static __isl_give isl_pw_multi_aff
*pw_multi_aff_set_pw_aff(
6172 __isl_take isl_pw_multi_aff
*pma
, unsigned pos
,
6173 __isl_take isl_pw_aff
*pa
)
6176 isl_pw_multi_aff
*res
= NULL
;
6181 if (!isl_space_tuple_is_equal(pma
->dim
, isl_dim_in
,
6182 pa
->dim
, isl_dim_in
))
6183 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
6184 "domains don't match", goto error
);
6185 if (isl_pw_multi_aff_check_range(pma
, isl_dim_out
, pos
, 1) < 0)
6189 res
= isl_pw_multi_aff_alloc_size(isl_pw_multi_aff_get_space(pma
), n
);
6191 for (i
= 0; i
< pma
->n
; ++i
) {
6192 for (j
= 0; j
< pa
->n
; ++j
) {
6194 isl_multi_aff
*res_ij
;
6197 common
= isl_set_intersect(isl_set_copy(pma
->p
[i
].set
),
6198 isl_set_copy(pa
->p
[j
].set
));
6199 empty
= isl_set_plain_is_empty(common
);
6200 if (empty
< 0 || empty
) {
6201 isl_set_free(common
);
6207 res_ij
= isl_multi_aff_set_aff(
6208 isl_multi_aff_copy(pma
->p
[i
].maff
), pos
,
6209 isl_aff_copy(pa
->p
[j
].aff
));
6210 res_ij
= isl_multi_aff_gist(res_ij
,
6211 isl_set_copy(common
));
6213 res
= isl_pw_multi_aff_add_piece(res
, common
, res_ij
);
6217 isl_pw_multi_aff_free(pma
);
6218 isl_pw_aff_free(pa
);
6221 isl_pw_multi_aff_free(pma
);
6222 isl_pw_aff_free(pa
);
6223 return isl_pw_multi_aff_free(res
);
6226 /* Replace the affine expressions at position "pos" in "pma" by "pa".
6228 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_set_pw_aff(
6229 __isl_take isl_pw_multi_aff
*pma
, unsigned pos
,
6230 __isl_take isl_pw_aff
*pa
)
6232 isl_bool equal_params
;
6236 equal_params
= isl_space_has_equal_params(pma
->dim
, pa
->dim
);
6237 if (equal_params
< 0)
6240 return pw_multi_aff_set_pw_aff(pma
, pos
, pa
);
6241 if (isl_pw_multi_aff_check_named_params(pma
) < 0 ||
6242 isl_pw_aff_check_named_params(pa
) < 0)
6244 pma
= isl_pw_multi_aff_align_params(pma
, isl_pw_aff_get_space(pa
));
6245 pa
= isl_pw_aff_align_params(pa
, isl_pw_multi_aff_get_space(pma
));
6246 return pw_multi_aff_set_pw_aff(pma
, pos
, pa
);
6248 isl_pw_multi_aff_free(pma
);
6249 isl_pw_aff_free(pa
);
6253 /* Do the parameters of "pa" match those of "space"?
6255 isl_bool
isl_pw_aff_matching_params(__isl_keep isl_pw_aff
*pa
,
6256 __isl_keep isl_space
*space
)
6258 isl_space
*pa_space
;
6262 return isl_bool_error
;
6264 pa_space
= isl_pw_aff_get_space(pa
);
6266 match
= isl_space_has_equal_params(space
, pa_space
);
6268 isl_space_free(pa_space
);
6272 /* Check that the domain space of "pa" matches "space".
6274 isl_stat
isl_pw_aff_check_match_domain_space(__isl_keep isl_pw_aff
*pa
,
6275 __isl_keep isl_space
*space
)
6277 isl_space
*pa_space
;
6281 return isl_stat_error
;
6283 pa_space
= isl_pw_aff_get_space(pa
);
6285 match
= isl_space_has_equal_params(space
, pa_space
);
6289 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
6290 "parameters don't match", goto error
);
6291 match
= isl_space_tuple_is_equal(space
, isl_dim_in
,
6292 pa_space
, isl_dim_in
);
6296 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
6297 "domains don't match", goto error
);
6298 isl_space_free(pa_space
);
6301 isl_space_free(pa_space
);
6302 return isl_stat_error
;
6310 #include <isl_multi_explicit_domain.c>
6311 #include <isl_multi_pw_aff_explicit_domain.c>
6312 #include <isl_multi_templ.c>
6313 #include <isl_multi_apply_set.c>
6314 #include <isl_multi_arith_templ.c>
6315 #include <isl_multi_bind_templ.c>
6316 #include <isl_multi_bind_domain_templ.c>
6317 #include <isl_multi_coalesce.c>
6318 #include <isl_multi_domain_templ.c>
6319 #include <isl_multi_dim_id_templ.c>
6320 #include <isl_multi_dims.c>
6321 #include <isl_multi_from_base_templ.c>
6322 #include <isl_multi_gist.c>
6323 #include <isl_multi_hash.c>
6324 #include <isl_multi_identity_templ.c>
6325 #include <isl_multi_align_set.c>
6326 #include <isl_multi_intersect.c>
6327 #include <isl_multi_move_dims_templ.c>
6328 #include <isl_multi_nan_templ.c>
6329 #include <isl_multi_param_templ.c>
6330 #include <isl_multi_product_templ.c>
6331 #include <isl_multi_splice_templ.c>
6332 #include <isl_multi_tuple_id_templ.c>
6333 #include <isl_multi_zero_templ.c>
6335 /* Does "mpa" have a non-trivial explicit domain?
6337 * The explicit domain, if present, is trivial if it represents
6338 * an (obviously) universe set.
6340 isl_bool
isl_multi_pw_aff_has_non_trivial_domain(
6341 __isl_keep isl_multi_pw_aff
*mpa
)
6344 return isl_bool_error
;
6345 if (!isl_multi_pw_aff_has_explicit_domain(mpa
))
6346 return isl_bool_false
;
6347 return isl_bool_not(isl_set_plain_is_universe(mpa
->u
.dom
));
6350 /* Scale the elements of "pma" by the corresponding elements of "mv".
6352 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_scale_multi_val(
6353 __isl_take isl_pw_multi_aff
*pma
, __isl_take isl_multi_val
*mv
)
6356 isl_bool equal_params
;
6358 pma
= isl_pw_multi_aff_cow(pma
);
6361 if (!isl_space_tuple_is_equal(pma
->dim
, isl_dim_out
,
6362 mv
->space
, isl_dim_set
))
6363 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
6364 "spaces don't match", goto error
);
6365 equal_params
= isl_space_has_equal_params(pma
->dim
, mv
->space
);
6366 if (equal_params
< 0)
6368 if (!equal_params
) {
6369 pma
= isl_pw_multi_aff_align_params(pma
,
6370 isl_multi_val_get_space(mv
));
6371 mv
= isl_multi_val_align_params(mv
,
6372 isl_pw_multi_aff_get_space(pma
));
6377 for (i
= 0; i
< pma
->n
; ++i
) {
6378 pma
->p
[i
].maff
= isl_multi_aff_scale_multi_val(pma
->p
[i
].maff
,
6379 isl_multi_val_copy(mv
));
6380 if (!pma
->p
[i
].maff
)
6384 isl_multi_val_free(mv
);
6387 isl_multi_val_free(mv
);
6388 isl_pw_multi_aff_free(pma
);
6392 /* This function is called for each entry of an isl_union_pw_multi_aff.
6393 * If the space of the entry matches that of data->mv,
6394 * then apply isl_pw_multi_aff_scale_multi_val and return the result.
6395 * Otherwise, return an empty isl_pw_multi_aff.
6397 static __isl_give isl_pw_multi_aff
*union_pw_multi_aff_scale_multi_val_entry(
6398 __isl_take isl_pw_multi_aff
*pma
, void *user
)
6400 isl_multi_val
*mv
= user
;
6404 if (!isl_space_tuple_is_equal(pma
->dim
, isl_dim_out
,
6405 mv
->space
, isl_dim_set
)) {
6406 isl_space
*space
= isl_pw_multi_aff_get_space(pma
);
6407 isl_pw_multi_aff_free(pma
);
6408 return isl_pw_multi_aff_empty(space
);
6411 return isl_pw_multi_aff_scale_multi_val(pma
, isl_multi_val_copy(mv
));
6414 /* Scale the elements of "upma" by the corresponding elements of "mv",
6415 * for those entries that match the space of "mv".
6417 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_scale_multi_val(
6418 __isl_take isl_union_pw_multi_aff
*upma
, __isl_take isl_multi_val
*mv
)
6420 upma
= isl_union_pw_multi_aff_align_params(upma
,
6421 isl_multi_val_get_space(mv
));
6422 mv
= isl_multi_val_align_params(mv
,
6423 isl_union_pw_multi_aff_get_space(upma
));
6427 return isl_union_pw_multi_aff_transform(upma
,
6428 &union_pw_multi_aff_scale_multi_val_entry
, mv
);
6430 isl_multi_val_free(mv
);
6433 isl_multi_val_free(mv
);
6434 isl_union_pw_multi_aff_free(upma
);
6438 /* Construct and return a piecewise multi affine expression
6439 * in the given space with value zero in each of the output dimensions and
6440 * a universe domain.
6442 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_zero(__isl_take isl_space
*space
)
6444 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_zero(space
));
6447 /* Construct and return a piecewise multi affine expression
6448 * that is equal to the given piecewise affine expression.
6450 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_pw_aff(
6451 __isl_take isl_pw_aff
*pa
)
6455 isl_pw_multi_aff
*pma
;
6460 space
= isl_pw_aff_get_space(pa
);
6461 pma
= isl_pw_multi_aff_alloc_size(space
, pa
->n
);
6463 for (i
= 0; i
< pa
->n
; ++i
) {
6467 set
= isl_set_copy(pa
->p
[i
].set
);
6468 ma
= isl_multi_aff_from_aff(isl_aff_copy(pa
->p
[i
].aff
));
6469 pma
= isl_pw_multi_aff_add_piece(pma
, set
, ma
);
6472 isl_pw_aff_free(pa
);
6476 /* Construct and return a piecewise multi affine expression
6477 * that is equal to the given multi piecewise affine expression
6478 * on the shared domain of the piecewise affine expressions,
6479 * in the special case of a 0D multi piecewise affine expression.
6481 * Create a piecewise multi affine expression with the explicit domain of
6482 * the 0D multi piecewise affine expression as domain.
6484 static __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_multi_pw_aff_0D(
6485 __isl_take isl_multi_pw_aff
*mpa
)
6491 space
= isl_multi_pw_aff_get_space(mpa
);
6492 dom
= isl_multi_pw_aff_get_explicit_domain(mpa
);
6493 isl_multi_pw_aff_free(mpa
);
6495 ma
= isl_multi_aff_zero(space
);
6496 return isl_pw_multi_aff_alloc(dom
, ma
);
6499 /* Construct and return a piecewise multi affine expression
6500 * that is equal to the given multi piecewise affine expression
6501 * on the shared domain of the piecewise affine expressions.
6503 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_multi_pw_aff(
6504 __isl_take isl_multi_pw_aff
*mpa
)
6509 isl_pw_multi_aff
*pma
;
6515 return isl_pw_multi_aff_from_multi_pw_aff_0D(mpa
);
6517 space
= isl_multi_pw_aff_get_space(mpa
);
6518 pa
= isl_multi_pw_aff_get_pw_aff(mpa
, 0);
6519 pma
= isl_pw_multi_aff_from_pw_aff(pa
);
6521 for (i
= 1; i
< mpa
->n
; ++i
) {
6522 isl_pw_multi_aff
*pma_i
;
6524 pa
= isl_multi_pw_aff_get_pw_aff(mpa
, i
);
6525 pma_i
= isl_pw_multi_aff_from_pw_aff(pa
);
6526 pma
= isl_pw_multi_aff_range_product(pma
, pma_i
);
6529 pma
= isl_pw_multi_aff_reset_space(pma
, space
);
6531 isl_multi_pw_aff_free(mpa
);
6535 /* Construct and return a multi piecewise affine expression
6536 * that is equal to the given multi affine expression.
6538 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_from_multi_aff(
6539 __isl_take isl_multi_aff
*ma
)
6543 isl_multi_pw_aff
*mpa
;
6545 n
= isl_multi_aff_dim(ma
, isl_dim_out
);
6547 ma
= isl_multi_aff_free(ma
);
6551 mpa
= isl_multi_pw_aff_alloc(isl_multi_aff_get_space(ma
));
6553 for (i
= 0; i
< n
; ++i
) {
6556 pa
= isl_pw_aff_from_aff(isl_multi_aff_get_aff(ma
, i
));
6557 mpa
= isl_multi_pw_aff_set_pw_aff(mpa
, i
, pa
);
6560 isl_multi_aff_free(ma
);
6564 /* Construct and return a multi piecewise affine expression
6565 * that is equal to the given piecewise multi affine expression.
6567 * If the resulting multi piecewise affine expression has
6568 * an explicit domain, then assign it the domain of the input.
6569 * In other cases, the domain is stored in the individual elements.
6571 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_from_pw_multi_aff(
6572 __isl_take isl_pw_multi_aff
*pma
)
6577 isl_multi_pw_aff
*mpa
;
6579 n
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
6581 pma
= isl_pw_multi_aff_free(pma
);
6582 space
= isl_pw_multi_aff_get_space(pma
);
6583 mpa
= isl_multi_pw_aff_alloc(space
);
6585 for (i
= 0; i
< n
; ++i
) {
6588 pa
= isl_pw_multi_aff_get_pw_aff(pma
, i
);
6589 mpa
= isl_multi_pw_aff_set_pw_aff(mpa
, i
, pa
);
6591 if (isl_multi_pw_aff_has_explicit_domain(mpa
)) {
6594 dom
= isl_pw_multi_aff_domain(isl_pw_multi_aff_copy(pma
));
6595 mpa
= isl_multi_pw_aff_intersect_domain(mpa
, dom
);
6598 isl_pw_multi_aff_free(pma
);
6602 /* Do "pa1" and "pa2" represent the same function?
6604 * We first check if they are obviously equal.
6605 * If not, we convert them to maps and check if those are equal.
6607 * If "pa1" or "pa2" contain any NaNs, then they are considered
6608 * not to be the same. A NaN is not equal to anything, not even
6611 isl_bool
isl_pw_aff_is_equal(__isl_keep isl_pw_aff
*pa1
,
6612 __isl_keep isl_pw_aff
*pa2
)
6616 isl_map
*map1
, *map2
;
6619 return isl_bool_error
;
6621 equal
= isl_pw_aff_plain_is_equal(pa1
, pa2
);
6622 if (equal
< 0 || equal
)
6624 has_nan
= either_involves_nan(pa1
, pa2
);
6626 return isl_bool_error
;
6628 return isl_bool_false
;
6630 map1
= isl_map_from_pw_aff_internal(isl_pw_aff_copy(pa1
));
6631 map2
= isl_map_from_pw_aff_internal(isl_pw_aff_copy(pa2
));
6632 equal
= isl_map_is_equal(map1
, map2
);
6639 /* Do "mpa1" and "mpa2" represent the same function?
6641 * Note that we cannot convert the entire isl_multi_pw_aff
6642 * to a map because the domains of the piecewise affine expressions
6643 * may not be the same.
6645 isl_bool
isl_multi_pw_aff_is_equal(__isl_keep isl_multi_pw_aff
*mpa1
,
6646 __isl_keep isl_multi_pw_aff
*mpa2
)
6649 isl_bool equal
, equal_params
;
6652 return isl_bool_error
;
6654 equal_params
= isl_space_has_equal_params(mpa1
->space
, mpa2
->space
);
6655 if (equal_params
< 0)
6656 return isl_bool_error
;
6657 if (!equal_params
) {
6658 if (!isl_space_has_named_params(mpa1
->space
))
6659 return isl_bool_false
;
6660 if (!isl_space_has_named_params(mpa2
->space
))
6661 return isl_bool_false
;
6662 mpa1
= isl_multi_pw_aff_copy(mpa1
);
6663 mpa2
= isl_multi_pw_aff_copy(mpa2
);
6664 mpa1
= isl_multi_pw_aff_align_params(mpa1
,
6665 isl_multi_pw_aff_get_space(mpa2
));
6666 mpa2
= isl_multi_pw_aff_align_params(mpa2
,
6667 isl_multi_pw_aff_get_space(mpa1
));
6668 equal
= isl_multi_pw_aff_is_equal(mpa1
, mpa2
);
6669 isl_multi_pw_aff_free(mpa1
);
6670 isl_multi_pw_aff_free(mpa2
);
6674 equal
= isl_space_is_equal(mpa1
->space
, mpa2
->space
);
6675 if (equal
< 0 || !equal
)
6678 for (i
= 0; i
< mpa1
->n
; ++i
) {
6679 equal
= isl_pw_aff_is_equal(mpa1
->u
.p
[i
], mpa2
->u
.p
[i
]);
6680 if (equal
< 0 || !equal
)
6684 return isl_bool_true
;
6687 /* Do "pma1" and "pma2" represent the same function?
6689 * First check if they are obviously equal.
6690 * If not, then convert them to maps and check if those are equal.
6692 * If "pa1" or "pa2" contain any NaNs, then they are considered
6693 * not to be the same. A NaN is not equal to anything, not even
6696 isl_bool
isl_pw_multi_aff_is_equal(__isl_keep isl_pw_multi_aff
*pma1
,
6697 __isl_keep isl_pw_multi_aff
*pma2
)
6701 isl_map
*map1
, *map2
;
6704 return isl_bool_error
;
6706 equal
= isl_pw_multi_aff_plain_is_equal(pma1
, pma2
);
6707 if (equal
< 0 || equal
)
6709 has_nan
= isl_pw_multi_aff_involves_nan(pma1
);
6710 if (has_nan
>= 0 && !has_nan
)
6711 has_nan
= isl_pw_multi_aff_involves_nan(pma2
);
6712 if (has_nan
< 0 || has_nan
)
6713 return isl_bool_not(has_nan
);
6715 map1
= isl_map_from_pw_multi_aff(isl_pw_multi_aff_copy(pma1
));
6716 map2
= isl_map_from_pw_multi_aff(isl_pw_multi_aff_copy(pma2
));
6717 equal
= isl_map_is_equal(map1
, map2
);
6724 /* Compute the pullback of "mpa" by the function represented by "ma".
6725 * In other words, plug in "ma" in "mpa".
6727 * The parameters of "mpa" and "ma" are assumed to have been aligned.
6729 * If "mpa" has an explicit domain, then it is this domain
6730 * that needs to undergo a pullback, i.e., a preimage.
6732 static __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_pullback_multi_aff_aligned(
6733 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_multi_aff
*ma
)
6736 isl_space
*space
= NULL
;
6738 mpa
= isl_multi_pw_aff_cow(mpa
);
6742 space
= isl_space_join(isl_multi_aff_get_space(ma
),
6743 isl_multi_pw_aff_get_space(mpa
));
6747 for (i
= 0; i
< mpa
->n
; ++i
) {
6748 mpa
->u
.p
[i
] = isl_pw_aff_pullback_multi_aff(mpa
->u
.p
[i
],
6749 isl_multi_aff_copy(ma
));
6753 if (isl_multi_pw_aff_has_explicit_domain(mpa
)) {
6754 mpa
->u
.dom
= isl_set_preimage_multi_aff(mpa
->u
.dom
,
6755 isl_multi_aff_copy(ma
));
6760 isl_multi_aff_free(ma
);
6761 isl_space_free(mpa
->space
);
6765 isl_space_free(space
);
6766 isl_multi_pw_aff_free(mpa
);
6767 isl_multi_aff_free(ma
);
6771 /* Compute the pullback of "mpa" by the function represented by "ma".
6772 * In other words, plug in "ma" in "mpa".
6774 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_pullback_multi_aff(
6775 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_multi_aff
*ma
)
6777 isl_bool equal_params
;
6781 equal_params
= isl_space_has_equal_params(mpa
->space
, ma
->space
);
6782 if (equal_params
< 0)
6785 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa
, ma
);
6786 mpa
= isl_multi_pw_aff_align_params(mpa
, isl_multi_aff_get_space(ma
));
6787 ma
= isl_multi_aff_align_params(ma
, isl_multi_pw_aff_get_space(mpa
));
6788 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa
, ma
);
6790 isl_multi_pw_aff_free(mpa
);
6791 isl_multi_aff_free(ma
);
6795 /* Compute the pullback of "mpa" by the function represented by "pma".
6796 * In other words, plug in "pma" in "mpa".
6798 * The parameters of "mpa" and "mpa" are assumed to have been aligned.
6800 * If "mpa" has an explicit domain, then it is this domain
6801 * that needs to undergo a pullback, i.e., a preimage.
6803 static __isl_give isl_multi_pw_aff
*
6804 isl_multi_pw_aff_pullback_pw_multi_aff_aligned(
6805 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_pw_multi_aff
*pma
)
6808 isl_space
*space
= NULL
;
6810 mpa
= isl_multi_pw_aff_cow(mpa
);
6814 space
= isl_space_join(isl_pw_multi_aff_get_space(pma
),
6815 isl_multi_pw_aff_get_space(mpa
));
6817 for (i
= 0; i
< mpa
->n
; ++i
) {
6818 mpa
->u
.p
[i
] = isl_pw_aff_pullback_pw_multi_aff_aligned(
6819 mpa
->u
.p
[i
], isl_pw_multi_aff_copy(pma
));
6823 if (isl_multi_pw_aff_has_explicit_domain(mpa
)) {
6824 mpa
->u
.dom
= isl_set_preimage_pw_multi_aff(mpa
->u
.dom
,
6825 isl_pw_multi_aff_copy(pma
));
6830 isl_pw_multi_aff_free(pma
);
6831 isl_space_free(mpa
->space
);
6835 isl_space_free(space
);
6836 isl_multi_pw_aff_free(mpa
);
6837 isl_pw_multi_aff_free(pma
);
6841 /* Compute the pullback of "mpa" by the function represented by "pma".
6842 * In other words, plug in "pma" in "mpa".
6844 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_pullback_pw_multi_aff(
6845 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_pw_multi_aff
*pma
)
6847 isl_bool equal_params
;
6851 equal_params
= isl_space_has_equal_params(mpa
->space
, pma
->dim
);
6852 if (equal_params
< 0)
6855 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa
, pma
);
6856 mpa
= isl_multi_pw_aff_align_params(mpa
,
6857 isl_pw_multi_aff_get_space(pma
));
6858 pma
= isl_pw_multi_aff_align_params(pma
,
6859 isl_multi_pw_aff_get_space(mpa
));
6860 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa
, pma
);
6862 isl_multi_pw_aff_free(mpa
);
6863 isl_pw_multi_aff_free(pma
);
6867 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
6868 * with the domain of "aff". The domain of the result is the same
6870 * "mpa" and "aff" are assumed to have been aligned.
6872 * We first extract the parametric constant from "aff", defined
6873 * over the correct domain.
6874 * Then we add the appropriate combinations of the members of "mpa".
6875 * Finally, we add the integer divisions through recursive calls.
6877 static __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_aff_aligned(
6878 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_aff
*aff
)
6881 isl_size n_in
, n_div
, n_mpa_in
;
6887 n_in
= isl_aff_dim(aff
, isl_dim_in
);
6888 n_div
= isl_aff_dim(aff
, isl_dim_div
);
6889 n_mpa_in
= isl_multi_pw_aff_dim(mpa
, isl_dim_in
);
6890 if (n_in
< 0 || n_div
< 0 || n_mpa_in
< 0)
6893 space
= isl_space_domain(isl_multi_pw_aff_get_space(mpa
));
6894 tmp
= isl_aff_copy(aff
);
6895 tmp
= isl_aff_drop_dims(tmp
, isl_dim_div
, 0, n_div
);
6896 tmp
= isl_aff_drop_dims(tmp
, isl_dim_in
, 0, n_in
);
6897 tmp
= isl_aff_add_dims(tmp
, isl_dim_in
, n_mpa_in
);
6898 tmp
= isl_aff_reset_domain_space(tmp
, space
);
6899 pa
= isl_pw_aff_from_aff(tmp
);
6901 for (i
= 0; i
< n_in
; ++i
) {
6904 if (!isl_aff_involves_dims(aff
, isl_dim_in
, i
, 1))
6906 v
= isl_aff_get_coefficient_val(aff
, isl_dim_in
, i
);
6907 pa_i
= isl_multi_pw_aff_get_pw_aff(mpa
, i
);
6908 pa_i
= isl_pw_aff_scale_val(pa_i
, v
);
6909 pa
= isl_pw_aff_add(pa
, pa_i
);
6912 for (i
= 0; i
< n_div
; ++i
) {
6916 if (!isl_aff_involves_dims(aff
, isl_dim_div
, i
, 1))
6918 div
= isl_aff_get_div(aff
, i
);
6919 pa_i
= isl_multi_pw_aff_apply_aff_aligned(
6920 isl_multi_pw_aff_copy(mpa
), div
);
6921 pa_i
= isl_pw_aff_floor(pa_i
);
6922 v
= isl_aff_get_coefficient_val(aff
, isl_dim_div
, i
);
6923 pa_i
= isl_pw_aff_scale_val(pa_i
, v
);
6924 pa
= isl_pw_aff_add(pa
, pa_i
);
6927 isl_multi_pw_aff_free(mpa
);
6932 isl_multi_pw_aff_free(mpa
);
6937 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
6938 * with the domain of "aff". The domain of the result is the same
6941 __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_aff(
6942 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_aff
*aff
)
6944 isl_bool equal_params
;
6948 equal_params
= isl_space_has_equal_params(aff
->ls
->dim
, mpa
->space
);
6949 if (equal_params
< 0)
6952 return isl_multi_pw_aff_apply_aff_aligned(mpa
, aff
);
6954 aff
= isl_aff_align_params(aff
, isl_multi_pw_aff_get_space(mpa
));
6955 mpa
= isl_multi_pw_aff_align_params(mpa
, isl_aff_get_space(aff
));
6957 return isl_multi_pw_aff_apply_aff_aligned(mpa
, aff
);
6960 isl_multi_pw_aff_free(mpa
);
6964 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
6965 * with the domain of "pa". The domain of the result is the same
6967 * "mpa" and "pa" are assumed to have been aligned.
6969 * We consider each piece in turn. Note that the domains of the
6970 * pieces are assumed to be disjoint and they remain disjoint
6971 * after taking the preimage (over the same function).
6973 static __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_pw_aff_aligned(
6974 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_pw_aff
*pa
)
6983 space
= isl_space_join(isl_multi_pw_aff_get_space(mpa
),
6984 isl_pw_aff_get_space(pa
));
6985 res
= isl_pw_aff_empty(space
);
6987 for (i
= 0; i
< pa
->n
; ++i
) {
6991 pa_i
= isl_multi_pw_aff_apply_aff_aligned(
6992 isl_multi_pw_aff_copy(mpa
),
6993 isl_aff_copy(pa
->p
[i
].aff
));
6994 domain
= isl_set_copy(pa
->p
[i
].set
);
6995 domain
= isl_set_preimage_multi_pw_aff(domain
,
6996 isl_multi_pw_aff_copy(mpa
));
6997 pa_i
= isl_pw_aff_intersect_domain(pa_i
, domain
);
6998 res
= isl_pw_aff_add_disjoint(res
, pa_i
);
7001 isl_pw_aff_free(pa
);
7002 isl_multi_pw_aff_free(mpa
);
7005 isl_pw_aff_free(pa
);
7006 isl_multi_pw_aff_free(mpa
);
7010 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
7011 * with the domain of "pa". The domain of the result is the same
7014 __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_pw_aff(
7015 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_pw_aff
*pa
)
7017 isl_bool equal_params
;
7021 equal_params
= isl_space_has_equal_params(pa
->dim
, mpa
->space
);
7022 if (equal_params
< 0)
7025 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa
, pa
);
7027 pa
= isl_pw_aff_align_params(pa
, isl_multi_pw_aff_get_space(mpa
));
7028 mpa
= isl_multi_pw_aff_align_params(mpa
, isl_pw_aff_get_space(pa
));
7030 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa
, pa
);
7032 isl_pw_aff_free(pa
);
7033 isl_multi_pw_aff_free(mpa
);
7037 /* Compute the pullback of "pa" by the function represented by "mpa".
7038 * In other words, plug in "mpa" in "pa".
7039 * "pa" and "mpa" are assumed to have been aligned.
7041 * The pullback is computed by applying "pa" to "mpa".
7043 static __isl_give isl_pw_aff
*isl_pw_aff_pullback_multi_pw_aff_aligned(
7044 __isl_take isl_pw_aff
*pa
, __isl_take isl_multi_pw_aff
*mpa
)
7046 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa
, pa
);
7049 /* Compute the pullback of "pa" by the function represented by "mpa".
7050 * In other words, plug in "mpa" in "pa".
7052 * The pullback is computed by applying "pa" to "mpa".
7054 __isl_give isl_pw_aff
*isl_pw_aff_pullback_multi_pw_aff(
7055 __isl_take isl_pw_aff
*pa
, __isl_take isl_multi_pw_aff
*mpa
)
7057 return isl_multi_pw_aff_apply_pw_aff(mpa
, pa
);
7060 /* Compute the pullback of "mpa1" by the function represented by "mpa2".
7061 * In other words, plug in "mpa2" in "mpa1".
7063 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7065 * We pullback each member of "mpa1" in turn.
7067 * If "mpa1" has an explicit domain, then it is this domain
7068 * that needs to undergo a pullback instead, i.e., a preimage.
7070 static __isl_give isl_multi_pw_aff
*
7071 isl_multi_pw_aff_pullback_multi_pw_aff_aligned(
7072 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
)
7075 isl_space
*space
= NULL
;
7077 mpa1
= isl_multi_pw_aff_cow(mpa1
);
7081 space
= isl_space_join(isl_multi_pw_aff_get_space(mpa2
),
7082 isl_multi_pw_aff_get_space(mpa1
));
7084 for (i
= 0; i
< mpa1
->n
; ++i
) {
7085 mpa1
->u
.p
[i
] = isl_pw_aff_pullback_multi_pw_aff_aligned(
7086 mpa1
->u
.p
[i
], isl_multi_pw_aff_copy(mpa2
));
7091 if (isl_multi_pw_aff_has_explicit_domain(mpa1
)) {
7092 mpa1
->u
.dom
= isl_set_preimage_multi_pw_aff(mpa1
->u
.dom
,
7093 isl_multi_pw_aff_copy(mpa2
));
7097 mpa1
= isl_multi_pw_aff_reset_space(mpa1
, space
);
7099 isl_multi_pw_aff_free(mpa2
);
7102 isl_space_free(space
);
7103 isl_multi_pw_aff_free(mpa1
);
7104 isl_multi_pw_aff_free(mpa2
);
7108 /* Compute the pullback of "mpa1" by the function represented by "mpa2".
7109 * In other words, plug in "mpa2" in "mpa1".
7111 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_pullback_multi_pw_aff(
7112 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
)
7114 return isl_multi_pw_aff_align_params_multi_multi_and(mpa1
, mpa2
,
7115 &isl_multi_pw_aff_pullback_multi_pw_aff_aligned
);
7118 /* Align the parameters of "mpa1" and "mpa2", check that the ranges
7119 * of "mpa1" and "mpa2" live in the same space, construct map space
7120 * between the domain spaces of "mpa1" and "mpa2" and call "order"
7121 * with this map space as extract argument.
7123 static __isl_give isl_map
*isl_multi_pw_aff_order_map(
7124 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
,
7125 __isl_give isl_map
*(*order
)(__isl_keep isl_multi_pw_aff
*mpa1
,
7126 __isl_keep isl_multi_pw_aff
*mpa2
, __isl_take isl_space
*space
))
7129 isl_space
*space1
, *space2
;
7132 mpa1
= isl_multi_pw_aff_align_params(mpa1
,
7133 isl_multi_pw_aff_get_space(mpa2
));
7134 mpa2
= isl_multi_pw_aff_align_params(mpa2
,
7135 isl_multi_pw_aff_get_space(mpa1
));
7138 match
= isl_space_tuple_is_equal(mpa1
->space
, isl_dim_out
,
7139 mpa2
->space
, isl_dim_out
);
7143 isl_die(isl_multi_pw_aff_get_ctx(mpa1
), isl_error_invalid
,
7144 "range spaces don't match", goto error
);
7145 space1
= isl_space_domain(isl_multi_pw_aff_get_space(mpa1
));
7146 space2
= isl_space_domain(isl_multi_pw_aff_get_space(mpa2
));
7147 space1
= isl_space_map_from_domain_and_range(space1
, space2
);
7149 res
= order(mpa1
, mpa2
, space1
);
7150 isl_multi_pw_aff_free(mpa1
);
7151 isl_multi_pw_aff_free(mpa2
);
7154 isl_multi_pw_aff_free(mpa1
);
7155 isl_multi_pw_aff_free(mpa2
);
7159 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7160 * where the function values are equal. "space" is the space of the result.
7161 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7163 * "mpa1" and "mpa2" are equal when each of the pairs of elements
7164 * in the sequences are equal.
7166 static __isl_give isl_map
*isl_multi_pw_aff_eq_map_on_space(
7167 __isl_keep isl_multi_pw_aff
*mpa1
, __isl_keep isl_multi_pw_aff
*mpa2
,
7168 __isl_take isl_space
*space
)
7174 n
= isl_multi_pw_aff_dim(mpa1
, isl_dim_out
);
7176 space
= isl_space_free(space
);
7177 res
= isl_map_universe(space
);
7179 for (i
= 0; i
< n
; ++i
) {
7180 isl_pw_aff
*pa1
, *pa2
;
7183 pa1
= isl_multi_pw_aff_get_pw_aff(mpa1
, i
);
7184 pa2
= isl_multi_pw_aff_get_pw_aff(mpa2
, i
);
7185 map
= isl_pw_aff_eq_map(pa1
, pa2
);
7186 res
= isl_map_intersect(res
, map
);
7192 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7193 * where the function values are equal.
7195 __isl_give isl_map
*isl_multi_pw_aff_eq_map(__isl_take isl_multi_pw_aff
*mpa1
,
7196 __isl_take isl_multi_pw_aff
*mpa2
)
7198 return isl_multi_pw_aff_order_map(mpa1
, mpa2
,
7199 &isl_multi_pw_aff_eq_map_on_space
);
7202 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7203 * where the function values of "mpa1" is lexicographically satisfies "base"
7204 * compared to that of "mpa2". "space" is the space of the result.
7205 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7207 * "mpa1" lexicographically satisfies "base" compared to "mpa2"
7208 * if its i-th element satisfies "base" when compared to
7209 * the i-th element of "mpa2" while all previous elements are
7212 static __isl_give isl_map
*isl_multi_pw_aff_lex_map_on_space(
7213 __isl_keep isl_multi_pw_aff
*mpa1
, __isl_keep isl_multi_pw_aff
*mpa2
,
7214 __isl_give isl_map
*(*base
)(__isl_take isl_pw_aff
*pa1
,
7215 __isl_take isl_pw_aff
*pa2
),
7216 __isl_take isl_space
*space
)
7220 isl_map
*res
, *rest
;
7222 n
= isl_multi_pw_aff_dim(mpa1
, isl_dim_out
);
7224 space
= isl_space_free(space
);
7225 res
= isl_map_empty(isl_space_copy(space
));
7226 rest
= isl_map_universe(space
);
7228 for (i
= 0; i
< n
; ++i
) {
7229 isl_pw_aff
*pa1
, *pa2
;
7232 pa1
= isl_multi_pw_aff_get_pw_aff(mpa1
, i
);
7233 pa2
= isl_multi_pw_aff_get_pw_aff(mpa2
, i
);
7234 map
= base(pa1
, pa2
);
7235 map
= isl_map_intersect(map
, isl_map_copy(rest
));
7236 res
= isl_map_union(res
, map
);
7241 pa1
= isl_multi_pw_aff_get_pw_aff(mpa1
, i
);
7242 pa2
= isl_multi_pw_aff_get_pw_aff(mpa2
, i
);
7243 map
= isl_pw_aff_eq_map(pa1
, pa2
);
7244 rest
= isl_map_intersect(rest
, map
);
7251 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7252 * where the function value of "mpa1" is lexicographically less than that
7253 * of "mpa2". "space" is the space of the result.
7254 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7256 * "mpa1" is less than "mpa2" if its i-th element is smaller
7257 * than the i-th element of "mpa2" while all previous elements are
7260 __isl_give isl_map
*isl_multi_pw_aff_lex_lt_map_on_space(
7261 __isl_keep isl_multi_pw_aff
*mpa1
, __isl_keep isl_multi_pw_aff
*mpa2
,
7262 __isl_take isl_space
*space
)
7264 return isl_multi_pw_aff_lex_map_on_space(mpa1
, mpa2
,
7265 &isl_pw_aff_lt_map
, space
);
7268 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7269 * where the function value of "mpa1" is lexicographically less than that
7272 __isl_give isl_map
*isl_multi_pw_aff_lex_lt_map(
7273 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
)
7275 return isl_multi_pw_aff_order_map(mpa1
, mpa2
,
7276 &isl_multi_pw_aff_lex_lt_map_on_space
);
7279 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7280 * where the function value of "mpa1" is lexicographically greater than that
7281 * of "mpa2". "space" is the space of the result.
7282 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7284 * "mpa1" is greater than "mpa2" if its i-th element is greater
7285 * than the i-th element of "mpa2" while all previous elements are
7288 __isl_give isl_map
*isl_multi_pw_aff_lex_gt_map_on_space(
7289 __isl_keep isl_multi_pw_aff
*mpa1
, __isl_keep isl_multi_pw_aff
*mpa2
,
7290 __isl_take isl_space
*space
)
7292 return isl_multi_pw_aff_lex_map_on_space(mpa1
, mpa2
,
7293 &isl_pw_aff_gt_map
, space
);
7296 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7297 * where the function value of "mpa1" is lexicographically greater than that
7300 __isl_give isl_map
*isl_multi_pw_aff_lex_gt_map(
7301 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
)
7303 return isl_multi_pw_aff_order_map(mpa1
, mpa2
,
7304 &isl_multi_pw_aff_lex_gt_map_on_space
);
7307 /* Compare two isl_affs.
7309 * Return -1 if "aff1" is "smaller" than "aff2", 1 if "aff1" is "greater"
7310 * than "aff2" and 0 if they are equal.
7312 * The order is fairly arbitrary. We do consider expressions that only involve
7313 * earlier dimensions as "smaller".
7315 int isl_aff_plain_cmp(__isl_keep isl_aff
*aff1
, __isl_keep isl_aff
*aff2
)
7328 cmp
= isl_local_space_cmp(aff1
->ls
, aff2
->ls
);
7332 last1
= isl_seq_last_non_zero(aff1
->v
->el
+ 1, aff1
->v
->size
- 1);
7333 last2
= isl_seq_last_non_zero(aff2
->v
->el
+ 1, aff1
->v
->size
- 1);
7335 return last1
- last2
;
7337 return isl_seq_cmp(aff1
->v
->el
, aff2
->v
->el
, aff1
->v
->size
);
7340 /* Compare two isl_pw_affs.
7342 * Return -1 if "pa1" is "smaller" than "pa2", 1 if "pa1" is "greater"
7343 * than "pa2" and 0 if they are equal.
7345 * The order is fairly arbitrary. We do consider expressions that only involve
7346 * earlier dimensions as "smaller".
7348 int isl_pw_aff_plain_cmp(__isl_keep isl_pw_aff
*pa1
,
7349 __isl_keep isl_pw_aff
*pa2
)
7362 cmp
= isl_space_cmp(pa1
->dim
, pa2
->dim
);
7366 if (pa1
->n
!= pa2
->n
)
7367 return pa1
->n
- pa2
->n
;
7369 for (i
= 0; i
< pa1
->n
; ++i
) {
7370 cmp
= isl_set_plain_cmp(pa1
->p
[i
].set
, pa2
->p
[i
].set
);
7373 cmp
= isl_aff_plain_cmp(pa1
->p
[i
].aff
, pa2
->p
[i
].aff
);
7381 /* Return a piecewise affine expression that is equal to "v" on "domain".
7383 __isl_give isl_pw_aff
*isl_pw_aff_val_on_domain(__isl_take isl_set
*domain
,
7384 __isl_take isl_val
*v
)
7387 isl_local_space
*ls
;
7390 space
= isl_set_get_space(domain
);
7391 ls
= isl_local_space_from_space(space
);
7392 aff
= isl_aff_val_on_domain(ls
, v
);
7394 return isl_pw_aff_alloc(domain
, aff
);
7397 /* Return a multi affine expression that is equal to "mv" on domain
7400 __isl_give isl_multi_aff
*isl_multi_aff_multi_val_on_space(
7401 __isl_take isl_space
*space
, __isl_take isl_multi_val
*mv
)
7406 isl_local_space
*ls
;
7409 n
= isl_multi_val_dim(mv
, isl_dim_set
);
7410 if (!space
|| n
< 0)
7413 space2
= isl_multi_val_get_space(mv
);
7414 space2
= isl_space_align_params(space2
, isl_space_copy(space
));
7415 space
= isl_space_align_params(space
, isl_space_copy(space2
));
7416 space
= isl_space_map_from_domain_and_range(space
, space2
);
7417 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
7418 ls
= isl_local_space_from_space(isl_space_domain(space
));
7419 for (i
= 0; i
< n
; ++i
) {
7423 v
= isl_multi_val_get_val(mv
, i
);
7424 aff
= isl_aff_val_on_domain(isl_local_space_copy(ls
), v
);
7425 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
7427 isl_local_space_free(ls
);
7429 isl_multi_val_free(mv
);
7432 isl_space_free(space
);
7433 isl_multi_val_free(mv
);
7437 /* Return a piecewise multi-affine expression
7438 * that is equal to "mv" on "domain".
7440 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_multi_val_on_domain(
7441 __isl_take isl_set
*domain
, __isl_take isl_multi_val
*mv
)
7446 space
= isl_set_get_space(domain
);
7447 ma
= isl_multi_aff_multi_val_on_space(space
, mv
);
7449 return isl_pw_multi_aff_alloc(domain
, ma
);
7452 /* Internal data structure for isl_union_pw_multi_aff_multi_val_on_domain.
7453 * mv is the value that should be attained on each domain set
7454 * res collects the results
7456 struct isl_union_pw_multi_aff_multi_val_on_domain_data
{
7458 isl_union_pw_multi_aff
*res
;
7461 /* Create an isl_pw_multi_aff equal to data->mv on "domain"
7462 * and add it to data->res.
7464 static isl_stat
pw_multi_aff_multi_val_on_domain(__isl_take isl_set
*domain
,
7467 struct isl_union_pw_multi_aff_multi_val_on_domain_data
*data
= user
;
7468 isl_pw_multi_aff
*pma
;
7471 mv
= isl_multi_val_copy(data
->mv
);
7472 pma
= isl_pw_multi_aff_multi_val_on_domain(domain
, mv
);
7473 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma
);
7475 return data
->res
? isl_stat_ok
: isl_stat_error
;
7478 /* Return a union piecewise multi-affine expression
7479 * that is equal to "mv" on "domain".
7481 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_multi_val_on_domain(
7482 __isl_take isl_union_set
*domain
, __isl_take isl_multi_val
*mv
)
7484 struct isl_union_pw_multi_aff_multi_val_on_domain_data data
;
7487 space
= isl_union_set_get_space(domain
);
7488 data
.res
= isl_union_pw_multi_aff_empty(space
);
7490 if (isl_union_set_foreach_set(domain
,
7491 &pw_multi_aff_multi_val_on_domain
, &data
) < 0)
7492 data
.res
= isl_union_pw_multi_aff_free(data
.res
);
7493 isl_union_set_free(domain
);
7494 isl_multi_val_free(mv
);
7498 /* Compute the pullback of data->pma by the function represented by "pma2",
7499 * provided the spaces match, and add the results to data->res.
7501 static isl_stat
pullback_entry(__isl_take isl_pw_multi_aff
*pma2
, void *user
)
7503 struct isl_union_pw_multi_aff_bin_data
*data
= user
;
7505 if (!isl_space_tuple_is_equal(data
->pma
->dim
, isl_dim_in
,
7506 pma2
->dim
, isl_dim_out
)) {
7507 isl_pw_multi_aff_free(pma2
);
7511 pma2
= isl_pw_multi_aff_pullback_pw_multi_aff(
7512 isl_pw_multi_aff_copy(data
->pma
), pma2
);
7514 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma2
);
7516 return isl_stat_error
;
7521 /* Compute the pullback of "upma1" by the function represented by "upma2".
7523 __isl_give isl_union_pw_multi_aff
*
7524 isl_union_pw_multi_aff_pullback_union_pw_multi_aff(
7525 __isl_take isl_union_pw_multi_aff
*upma1
,
7526 __isl_take isl_union_pw_multi_aff
*upma2
)
7528 return bin_op(upma1
, upma2
, &pullback_entry
);
7531 /* Check that the domain space of "upa" matches "space".
7533 * This function is called from isl_multi_union_pw_aff_set_union_pw_aff and
7534 * can in principle never fail since the space "space" is that
7535 * of the isl_multi_union_pw_aff and is a set space such that
7536 * there is no domain space to match.
7538 * We check the parameters and double-check that "space" is
7539 * indeed that of a set.
7541 static isl_stat
isl_union_pw_aff_check_match_domain_space(
7542 __isl_keep isl_union_pw_aff
*upa
, __isl_keep isl_space
*space
)
7544 isl_space
*upa_space
;
7548 return isl_stat_error
;
7550 match
= isl_space_is_set(space
);
7552 return isl_stat_error
;
7554 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
7555 "expecting set space", return isl_stat_error
);
7557 upa_space
= isl_union_pw_aff_get_space(upa
);
7558 match
= isl_space_has_equal_params(space
, upa_space
);
7562 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
7563 "parameters don't match", goto error
);
7565 isl_space_free(upa_space
);
7568 isl_space_free(upa_space
);
7569 return isl_stat_error
;
7572 /* Do the parameters of "upa" match those of "space"?
7574 static isl_bool
isl_union_pw_aff_matching_params(
7575 __isl_keep isl_union_pw_aff
*upa
, __isl_keep isl_space
*space
)
7577 isl_space
*upa_space
;
7581 return isl_bool_error
;
7583 upa_space
= isl_union_pw_aff_get_space(upa
);
7585 match
= isl_space_has_equal_params(space
, upa_space
);
7587 isl_space_free(upa_space
);
7591 /* Internal data structure for isl_union_pw_aff_reset_domain_space.
7592 * space represents the new parameters.
7593 * res collects the results.
7595 struct isl_union_pw_aff_reset_params_data
{
7597 isl_union_pw_aff
*res
;
7600 /* Replace the parameters of "pa" by data->space and
7601 * add the result to data->res.
7603 static isl_stat
reset_params(__isl_take isl_pw_aff
*pa
, void *user
)
7605 struct isl_union_pw_aff_reset_params_data
*data
= user
;
7608 space
= isl_pw_aff_get_space(pa
);
7609 space
= isl_space_replace_params(space
, data
->space
);
7610 pa
= isl_pw_aff_reset_space(pa
, space
);
7611 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
7613 return data
->res
? isl_stat_ok
: isl_stat_error
;
7616 /* Replace the domain space of "upa" by "space".
7617 * Since a union expression does not have a (single) domain space,
7618 * "space" is necessarily a parameter space.
7620 * Since the order and the names of the parameters determine
7621 * the hash value, we need to create a new hash table.
7623 static __isl_give isl_union_pw_aff
*isl_union_pw_aff_reset_domain_space(
7624 __isl_take isl_union_pw_aff
*upa
, __isl_take isl_space
*space
)
7626 struct isl_union_pw_aff_reset_params_data data
= { space
};
7629 match
= isl_union_pw_aff_matching_params(upa
, space
);
7631 upa
= isl_union_pw_aff_free(upa
);
7633 isl_space_free(space
);
7637 data
.res
= isl_union_pw_aff_empty(isl_space_copy(space
));
7638 if (isl_union_pw_aff_foreach_pw_aff(upa
, &reset_params
, &data
) < 0)
7639 data
.res
= isl_union_pw_aff_free(data
.res
);
7641 isl_union_pw_aff_free(upa
);
7642 isl_space_free(space
);
7646 /* Return the floor of "pa".
7648 static __isl_give isl_pw_aff
*floor_entry(__isl_take isl_pw_aff
*pa
, void *user
)
7650 return isl_pw_aff_floor(pa
);
7653 /* Given f, return floor(f).
7655 __isl_give isl_union_pw_aff
*isl_union_pw_aff_floor(
7656 __isl_take isl_union_pw_aff
*upa
)
7658 return isl_union_pw_aff_transform_inplace(upa
, &floor_entry
, NULL
);
7663 * upa mod m = upa - m * floor(upa/m)
7665 * with m an integer value.
7667 __isl_give isl_union_pw_aff
*isl_union_pw_aff_mod_val(
7668 __isl_take isl_union_pw_aff
*upa
, __isl_take isl_val
*m
)
7670 isl_union_pw_aff
*res
;
7675 if (!isl_val_is_int(m
))
7676 isl_die(isl_val_get_ctx(m
), isl_error_invalid
,
7677 "expecting integer modulo", goto error
);
7678 if (!isl_val_is_pos(m
))
7679 isl_die(isl_val_get_ctx(m
), isl_error_invalid
,
7680 "expecting positive modulo", goto error
);
7682 res
= isl_union_pw_aff_copy(upa
);
7683 upa
= isl_union_pw_aff_scale_down_val(upa
, isl_val_copy(m
));
7684 upa
= isl_union_pw_aff_floor(upa
);
7685 upa
= isl_union_pw_aff_scale_val(upa
, m
);
7686 res
= isl_union_pw_aff_sub(res
, upa
);
7691 isl_union_pw_aff_free(upa
);
7695 /* Internal data structure for isl_union_pw_multi_aff_get_union_pw_aff.
7696 * pos is the output position that needs to be extracted.
7697 * res collects the results.
7699 struct isl_union_pw_multi_aff_get_union_pw_aff_data
{
7701 isl_union_pw_aff
*res
;
7704 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma"
7705 * (assuming it has such a dimension) and add it to data->res.
7707 static isl_stat
get_union_pw_aff(__isl_take isl_pw_multi_aff
*pma
, void *user
)
7709 struct isl_union_pw_multi_aff_get_union_pw_aff_data
*data
= user
;
7713 n_out
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
7715 return isl_stat_error
;
7716 if (data
->pos
>= n_out
) {
7717 isl_pw_multi_aff_free(pma
);
7721 pa
= isl_pw_multi_aff_get_pw_aff(pma
, data
->pos
);
7722 isl_pw_multi_aff_free(pma
);
7724 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
7726 return data
->res
? isl_stat_ok
: isl_stat_error
;
7729 /* Extract an isl_union_pw_aff corresponding to
7730 * output dimension "pos" of "upma".
7732 __isl_give isl_union_pw_aff
*isl_union_pw_multi_aff_get_union_pw_aff(
7733 __isl_keep isl_union_pw_multi_aff
*upma
, int pos
)
7735 struct isl_union_pw_multi_aff_get_union_pw_aff_data data
;
7742 isl_die(isl_union_pw_multi_aff_get_ctx(upma
), isl_error_invalid
,
7743 "cannot extract at negative position", return NULL
);
7745 space
= isl_union_pw_multi_aff_get_space(upma
);
7746 data
.res
= isl_union_pw_aff_empty(space
);
7748 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma
,
7749 &get_union_pw_aff
, &data
) < 0)
7750 data
.res
= isl_union_pw_aff_free(data
.res
);
7755 /* Return a union piecewise affine expression
7756 * that is equal to "aff" on "domain".
7758 __isl_give isl_union_pw_aff
*isl_union_pw_aff_aff_on_domain(
7759 __isl_take isl_union_set
*domain
, __isl_take isl_aff
*aff
)
7763 pa
= isl_pw_aff_from_aff(aff
);
7764 return isl_union_pw_aff_pw_aff_on_domain(domain
, pa
);
7767 /* Return a union piecewise affine expression
7768 * that is equal to the parameter identified by "id" on "domain".
7770 * Make sure the parameter appears in the space passed to
7771 * isl_aff_param_on_domain_space_id.
7773 __isl_give isl_union_pw_aff
*isl_union_pw_aff_param_on_domain_id(
7774 __isl_take isl_union_set
*domain
, __isl_take isl_id
*id
)
7779 space
= isl_union_set_get_space(domain
);
7780 space
= isl_space_add_param_id(space
, isl_id_copy(id
));
7781 aff
= isl_aff_param_on_domain_space_id(space
, id
);
7782 return isl_union_pw_aff_aff_on_domain(domain
, aff
);
7785 /* Internal data structure for isl_union_pw_aff_pw_aff_on_domain.
7786 * "pa" is the piecewise symbolic value that the resulting isl_union_pw_aff
7788 * "res" collects the results.
7790 struct isl_union_pw_aff_pw_aff_on_domain_data
{
7792 isl_union_pw_aff
*res
;
7795 /* Construct a piecewise affine expression that is equal to data->pa
7796 * on "domain" and add the result to data->res.
7798 static isl_stat
pw_aff_on_domain(__isl_take isl_set
*domain
, void *user
)
7800 struct isl_union_pw_aff_pw_aff_on_domain_data
*data
= user
;
7804 pa
= isl_pw_aff_copy(data
->pa
);
7805 dim
= isl_set_dim(domain
, isl_dim_set
);
7807 pa
= isl_pw_aff_free(pa
);
7808 pa
= isl_pw_aff_from_range(pa
);
7809 pa
= isl_pw_aff_add_dims(pa
, isl_dim_in
, dim
);
7810 pa
= isl_pw_aff_reset_domain_space(pa
, isl_set_get_space(domain
));
7811 pa
= isl_pw_aff_intersect_domain(pa
, domain
);
7812 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
7814 return data
->res
? isl_stat_ok
: isl_stat_error
;
7817 /* Return a union piecewise affine expression
7818 * that is equal to "pa" on "domain", assuming "domain" and "pa"
7819 * have been aligned.
7821 * Construct an isl_pw_aff on each of the sets in "domain" and
7822 * collect the results.
7824 static __isl_give isl_union_pw_aff
*isl_union_pw_aff_pw_aff_on_domain_aligned(
7825 __isl_take isl_union_set
*domain
, __isl_take isl_pw_aff
*pa
)
7827 struct isl_union_pw_aff_pw_aff_on_domain_data data
;
7830 space
= isl_union_set_get_space(domain
);
7831 data
.res
= isl_union_pw_aff_empty(space
);
7833 if (isl_union_set_foreach_set(domain
, &pw_aff_on_domain
, &data
) < 0)
7834 data
.res
= isl_union_pw_aff_free(data
.res
);
7835 isl_union_set_free(domain
);
7836 isl_pw_aff_free(pa
);
7840 /* Return a union piecewise affine expression
7841 * that is equal to "pa" on "domain".
7843 * Check that "pa" is a parametric expression,
7844 * align the parameters if needed and call
7845 * isl_union_pw_aff_pw_aff_on_domain_aligned.
7847 __isl_give isl_union_pw_aff
*isl_union_pw_aff_pw_aff_on_domain(
7848 __isl_take isl_union_set
*domain
, __isl_take isl_pw_aff
*pa
)
7851 isl_bool equal_params
;
7852 isl_space
*domain_space
, *pa_space
;
7854 pa_space
= isl_pw_aff_peek_space(pa
);
7855 is_set
= isl_space_is_set(pa_space
);
7859 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
7860 "expecting parametric expression", goto error
);
7862 domain_space
= isl_union_set_get_space(domain
);
7863 pa_space
= isl_pw_aff_get_space(pa
);
7864 equal_params
= isl_space_has_equal_params(domain_space
, pa_space
);
7865 if (equal_params
>= 0 && !equal_params
) {
7868 space
= isl_space_align_params(domain_space
, pa_space
);
7869 pa
= isl_pw_aff_align_params(pa
, isl_space_copy(space
));
7870 domain
= isl_union_set_align_params(domain
, space
);
7872 isl_space_free(domain_space
);
7873 isl_space_free(pa_space
);
7876 if (equal_params
< 0)
7878 return isl_union_pw_aff_pw_aff_on_domain_aligned(domain
, pa
);
7880 isl_union_set_free(domain
);
7881 isl_pw_aff_free(pa
);
7885 /* Internal data structure for isl_union_pw_aff_val_on_domain.
7886 * "v" is the value that the resulting isl_union_pw_aff needs to attain.
7887 * "res" collects the results.
7889 struct isl_union_pw_aff_val_on_domain_data
{
7891 isl_union_pw_aff
*res
;
7894 /* Construct a piecewise affine expression that is equal to data->v
7895 * on "domain" and add the result to data->res.
7897 static isl_stat
pw_aff_val_on_domain(__isl_take isl_set
*domain
, void *user
)
7899 struct isl_union_pw_aff_val_on_domain_data
*data
= user
;
7903 v
= isl_val_copy(data
->v
);
7904 pa
= isl_pw_aff_val_on_domain(domain
, v
);
7905 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
7907 return data
->res
? isl_stat_ok
: isl_stat_error
;
7910 /* Return a union piecewise affine expression
7911 * that is equal to "v" on "domain".
7913 * Construct an isl_pw_aff on each of the sets in "domain" and
7914 * collect the results.
7916 __isl_give isl_union_pw_aff
*isl_union_pw_aff_val_on_domain(
7917 __isl_take isl_union_set
*domain
, __isl_take isl_val
*v
)
7919 struct isl_union_pw_aff_val_on_domain_data data
;
7922 space
= isl_union_set_get_space(domain
);
7923 data
.res
= isl_union_pw_aff_empty(space
);
7925 if (isl_union_set_foreach_set(domain
, &pw_aff_val_on_domain
, &data
) < 0)
7926 data
.res
= isl_union_pw_aff_free(data
.res
);
7927 isl_union_set_free(domain
);
7932 /* Construct a piecewise multi affine expression
7933 * that is equal to "pa" and add it to upma.
7935 static isl_stat
pw_multi_aff_from_pw_aff_entry(__isl_take isl_pw_aff
*pa
,
7938 isl_union_pw_multi_aff
**upma
= user
;
7939 isl_pw_multi_aff
*pma
;
7941 pma
= isl_pw_multi_aff_from_pw_aff(pa
);
7942 *upma
= isl_union_pw_multi_aff_add_pw_multi_aff(*upma
, pma
);
7944 return *upma
? isl_stat_ok
: isl_stat_error
;
7947 /* Construct and return a union piecewise multi affine expression
7948 * that is equal to the given union piecewise affine expression.
7950 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_union_pw_aff(
7951 __isl_take isl_union_pw_aff
*upa
)
7954 isl_union_pw_multi_aff
*upma
;
7959 space
= isl_union_pw_aff_get_space(upa
);
7960 upma
= isl_union_pw_multi_aff_empty(space
);
7962 if (isl_union_pw_aff_foreach_pw_aff(upa
,
7963 &pw_multi_aff_from_pw_aff_entry
, &upma
) < 0)
7964 upma
= isl_union_pw_multi_aff_free(upma
);
7966 isl_union_pw_aff_free(upa
);
7970 /* Compute the set of elements in the domain of "pa" where it is zero and
7971 * add this set to "uset".
7973 static isl_stat
zero_union_set(__isl_take isl_pw_aff
*pa
, void *user
)
7975 isl_union_set
**uset
= (isl_union_set
**)user
;
7977 *uset
= isl_union_set_add_set(*uset
, isl_pw_aff_zero_set(pa
));
7979 return *uset
? isl_stat_ok
: isl_stat_error
;
7982 /* Return a union set containing those elements in the domain
7983 * of "upa" where it is zero.
7985 __isl_give isl_union_set
*isl_union_pw_aff_zero_union_set(
7986 __isl_take isl_union_pw_aff
*upa
)
7988 isl_union_set
*zero
;
7990 zero
= isl_union_set_empty(isl_union_pw_aff_get_space(upa
));
7991 if (isl_union_pw_aff_foreach_pw_aff(upa
, &zero_union_set
, &zero
) < 0)
7992 zero
= isl_union_set_free(zero
);
7994 isl_union_pw_aff_free(upa
);
7998 /* Internal data structure for isl_union_pw_aff_bind_id,
7999 * storing the parameter that needs to be bound and
8000 * the accumulated results.
8002 struct isl_bind_id_data
{
8004 isl_union_set
*bound
;
8007 /* Bind the piecewise affine function "pa" to the parameter data->id,
8008 * adding the resulting elements in the domain where the expression
8009 * is equal to the parameter to data->bound.
8011 static isl_stat
bind_id(__isl_take isl_pw_aff
*pa
, void *user
)
8013 struct isl_bind_id_data
*data
= user
;
8016 bound
= isl_pw_aff_bind_id(pa
, isl_id_copy(data
->id
));
8017 data
->bound
= isl_union_set_add_set(data
->bound
, bound
);
8019 return data
->bound
? isl_stat_ok
: isl_stat_error
;
8022 /* Bind the union piecewise affine function "upa" to the parameter "id",
8023 * returning the elements in the domain where the expression
8024 * is equal to the parameter.
8026 __isl_give isl_union_set
*isl_union_pw_aff_bind_id(
8027 __isl_take isl_union_pw_aff
*upa
, __isl_take isl_id
*id
)
8029 struct isl_bind_id_data data
= { id
};
8031 data
.bound
= isl_union_set_empty(isl_union_pw_aff_get_space(upa
));
8032 if (isl_union_pw_aff_foreach_pw_aff(upa
, &bind_id
, &data
) < 0)
8033 data
.bound
= isl_union_set_free(data
.bound
);
8035 isl_union_pw_aff_free(upa
);
8040 /* Internal data structure for isl_union_pw_aff_pullback_union_pw_multi_aff.
8041 * upma is the function that is plugged in.
8042 * pa is the current part of the function in which upma is plugged in.
8043 * res collects the results.
8045 struct isl_union_pw_aff_pullback_upma_data
{
8046 isl_union_pw_multi_aff
*upma
;
8048 isl_union_pw_aff
*res
;
8051 /* Check if "pma" can be plugged into data->pa.
8052 * If so, perform the pullback and add the result to data->res.
8054 static isl_stat
pa_pb_pma(__isl_take isl_pw_multi_aff
*pma
, void *user
)
8056 struct isl_union_pw_aff_pullback_upma_data
*data
= user
;
8059 if (!isl_space_tuple_is_equal(data
->pa
->dim
, isl_dim_in
,
8060 pma
->dim
, isl_dim_out
)) {
8061 isl_pw_multi_aff_free(pma
);
8065 pa
= isl_pw_aff_copy(data
->pa
);
8066 pa
= isl_pw_aff_pullback_pw_multi_aff(pa
, pma
);
8068 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
8070 return data
->res
? isl_stat_ok
: isl_stat_error
;
8073 /* Check if any of the elements of data->upma can be plugged into pa,
8074 * add if so add the result to data->res.
8076 static isl_stat
upa_pb_upma(__isl_take isl_pw_aff
*pa
, void *user
)
8078 struct isl_union_pw_aff_pullback_upma_data
*data
= user
;
8082 r
= isl_union_pw_multi_aff_foreach_pw_multi_aff(data
->upma
,
8084 isl_pw_aff_free(pa
);
8089 /* Compute the pullback of "upa" by the function represented by "upma".
8090 * In other words, plug in "upma" in "upa". The result contains
8091 * expressions defined over the domain space of "upma".
8093 * Run over all pairs of elements in "upa" and "upma", perform
8094 * the pullback when appropriate and collect the results.
8095 * If the hash value were based on the domain space rather than
8096 * the function space, then we could run through all elements
8097 * of "upma" and directly pick out the corresponding element of "upa".
8099 __isl_give isl_union_pw_aff
*isl_union_pw_aff_pullback_union_pw_multi_aff(
8100 __isl_take isl_union_pw_aff
*upa
,
8101 __isl_take isl_union_pw_multi_aff
*upma
)
8103 struct isl_union_pw_aff_pullback_upma_data data
= { NULL
, NULL
};
8106 space
= isl_union_pw_multi_aff_get_space(upma
);
8107 upa
= isl_union_pw_aff_align_params(upa
, space
);
8108 space
= isl_union_pw_aff_get_space(upa
);
8109 upma
= isl_union_pw_multi_aff_align_params(upma
, space
);
8115 data
.res
= isl_union_pw_aff_alloc_same_size(upa
);
8116 if (isl_union_pw_aff_foreach_pw_aff(upa
, &upa_pb_upma
, &data
) < 0)
8117 data
.res
= isl_union_pw_aff_free(data
.res
);
8119 isl_union_pw_aff_free(upa
);
8120 isl_union_pw_multi_aff_free(upma
);
8123 isl_union_pw_aff_free(upa
);
8124 isl_union_pw_multi_aff_free(upma
);
8129 #define BASE union_pw_aff
8131 #define DOMBASE union_set
8133 #include <isl_multi_explicit_domain.c>
8134 #include <isl_multi_union_pw_aff_explicit_domain.c>
8135 #include <isl_multi_templ.c>
8136 #include <isl_multi_apply_set.c>
8137 #include <isl_multi_apply_union_set.c>
8138 #include <isl_multi_arith_templ.c>
8139 #include <isl_multi_bind_templ.c>
8140 #include <isl_multi_coalesce.c>
8141 #include <isl_multi_dim_id_templ.c>
8142 #include <isl_multi_floor.c>
8143 #include <isl_multi_from_base_templ.c>
8144 #include <isl_multi_gist.c>
8145 #include <isl_multi_align_set.c>
8146 #include <isl_multi_align_union_set.c>
8147 #include <isl_multi_intersect.c>
8148 #include <isl_multi_nan_templ.c>
8149 #include <isl_multi_tuple_id_templ.c>
8151 /* Does "mupa" have a non-trivial explicit domain?
8153 * The explicit domain, if present, is trivial if it represents
8154 * an (obviously) universe parameter set.
8156 isl_bool
isl_multi_union_pw_aff_has_non_trivial_domain(
8157 __isl_keep isl_multi_union_pw_aff
*mupa
)
8159 isl_bool is_params
, trivial
;
8163 return isl_bool_error
;
8164 if (!isl_multi_union_pw_aff_has_explicit_domain(mupa
))
8165 return isl_bool_false
;
8166 is_params
= isl_union_set_is_params(mupa
->u
.dom
);
8167 if (is_params
< 0 || !is_params
)
8168 return isl_bool_not(is_params
);
8169 set
= isl_set_from_union_set(isl_union_set_copy(mupa
->u
.dom
));
8170 trivial
= isl_set_plain_is_universe(set
);
8172 return isl_bool_not(trivial
);
8175 /* Construct a multiple union piecewise affine expression
8176 * in the given space with value zero in each of the output dimensions.
8178 * Since there is no canonical zero value for
8179 * a union piecewise affine expression, we can only construct
8180 * a zero-dimensional "zero" value.
8182 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_zero(
8183 __isl_take isl_space
*space
)
8191 params
= isl_space_is_params(space
);
8195 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
8196 "expecting proper set space", goto error
);
8197 if (!isl_space_is_set(space
))
8198 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
8199 "expecting set space", goto error
);
8200 dim
= isl_space_dim(space
, isl_dim_out
);
8204 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
8205 "expecting 0D space", goto error
);
8207 return isl_multi_union_pw_aff_alloc(space
);
8209 isl_space_free(space
);
8213 /* Compute the sum of "mupa1" and "mupa2" on the union of their domains,
8214 * with the actual sum on the shared domain and
8215 * the defined expression on the symmetric difference of the domains.
8217 * We simply iterate over the elements in both arguments and
8218 * call isl_union_pw_aff_union_add on each of them, if there is
8219 * at least one element.
8221 * Otherwise, the two expressions have an explicit domain and
8222 * the union of these explicit domains is computed.
8223 * This assumes that the explicit domains are either both in terms
8224 * of specific domains elements or both in terms of parameters.
8225 * However, if one of the expressions does not have any constraints
8226 * on its explicit domain, then this is allowed as well and the result
8227 * is the expression with no constraints on its explicit domain.
8229 static __isl_give isl_multi_union_pw_aff
*
8230 isl_multi_union_pw_aff_union_add_aligned(
8231 __isl_take isl_multi_union_pw_aff
*mupa1
,
8232 __isl_take isl_multi_union_pw_aff
*mupa2
)
8234 isl_bool has_domain
, is_params1
, is_params2
;
8236 if (isl_multi_union_pw_aff_check_equal_space(mupa1
, mupa2
) < 0)
8239 return isl_multi_union_pw_aff_bin_op(mupa1
, mupa2
,
8240 &isl_union_pw_aff_union_add
);
8241 if (isl_multi_union_pw_aff_check_has_explicit_domain(mupa1
) < 0 ||
8242 isl_multi_union_pw_aff_check_has_explicit_domain(mupa2
) < 0)
8245 has_domain
= isl_multi_union_pw_aff_has_non_trivial_domain(mupa1
);
8249 isl_multi_union_pw_aff_free(mupa2
);
8252 has_domain
= isl_multi_union_pw_aff_has_non_trivial_domain(mupa2
);
8256 isl_multi_union_pw_aff_free(mupa1
);
8260 is_params1
= isl_union_set_is_params(mupa1
->u
.dom
);
8261 is_params2
= isl_union_set_is_params(mupa2
->u
.dom
);
8262 if (is_params1
< 0 || is_params2
< 0)
8264 if (is_params1
!= is_params2
)
8265 isl_die(isl_multi_union_pw_aff_get_ctx(mupa1
),
8267 "cannot compute union of concrete domain and "
8268 "parameter constraints", goto error
);
8269 mupa1
= isl_multi_union_pw_aff_cow(mupa1
);
8272 mupa1
->u
.dom
= isl_union_set_union(mupa1
->u
.dom
,
8273 isl_union_set_copy(mupa2
->u
.dom
));
8276 isl_multi_union_pw_aff_free(mupa2
);
8279 isl_multi_union_pw_aff_free(mupa1
);
8280 isl_multi_union_pw_aff_free(mupa2
);
8284 /* Compute the sum of "mupa1" and "mupa2" on the union of their domains,
8285 * with the actual sum on the shared domain and
8286 * the defined expression on the symmetric difference of the domains.
8288 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_union_add(
8289 __isl_take isl_multi_union_pw_aff
*mupa1
,
8290 __isl_take isl_multi_union_pw_aff
*mupa2
)
8292 return isl_multi_union_pw_aff_align_params_multi_multi_and(mupa1
, mupa2
,
8293 &isl_multi_union_pw_aff_union_add_aligned
);
8296 /* Construct and return a multi union piecewise affine expression
8297 * that is equal to the given multi affine expression.
8299 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_from_multi_aff(
8300 __isl_take isl_multi_aff
*ma
)
8302 isl_multi_pw_aff
*mpa
;
8304 mpa
= isl_multi_pw_aff_from_multi_aff(ma
);
8305 return isl_multi_union_pw_aff_from_multi_pw_aff(mpa
);
8308 /* Construct and return a multi union piecewise affine expression
8309 * that is equal to the given multi piecewise affine expression.
8311 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_from_multi_pw_aff(
8312 __isl_take isl_multi_pw_aff
*mpa
)
8317 isl_multi_union_pw_aff
*mupa
;
8319 n
= isl_multi_pw_aff_dim(mpa
, isl_dim_out
);
8321 mpa
= isl_multi_pw_aff_free(mpa
);
8325 space
= isl_multi_pw_aff_get_space(mpa
);
8326 space
= isl_space_range(space
);
8327 mupa
= isl_multi_union_pw_aff_alloc(space
);
8329 for (i
= 0; i
< n
; ++i
) {
8331 isl_union_pw_aff
*upa
;
8333 pa
= isl_multi_pw_aff_get_pw_aff(mpa
, i
);
8334 upa
= isl_union_pw_aff_from_pw_aff(pa
);
8335 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
8338 isl_multi_pw_aff_free(mpa
);
8343 /* Extract the range space of "pma" and assign it to *space.
8344 * If *space has already been set (through a previous call to this function),
8345 * then check that the range space is the same.
8347 static isl_stat
extract_space(__isl_take isl_pw_multi_aff
*pma
, void *user
)
8349 isl_space
**space
= user
;
8350 isl_space
*pma_space
;
8353 pma_space
= isl_space_range(isl_pw_multi_aff_get_space(pma
));
8354 isl_pw_multi_aff_free(pma
);
8357 return isl_stat_error
;
8363 equal
= isl_space_is_equal(pma_space
, *space
);
8364 isl_space_free(pma_space
);
8367 return isl_stat_error
;
8369 isl_die(isl_space_get_ctx(*space
), isl_error_invalid
,
8370 "range spaces not the same", return isl_stat_error
);
8374 /* Construct and return a multi union piecewise affine expression
8375 * that is equal to the given union piecewise multi affine expression.
8377 * In order to be able to perform the conversion, the input
8378 * needs to be non-empty and may only involve a single range space.
8380 * If the resulting multi union piecewise affine expression has
8381 * an explicit domain, then assign it the domain of the input.
8382 * In other cases, the domain is stored in the individual elements.
8384 __isl_give isl_multi_union_pw_aff
*
8385 isl_multi_union_pw_aff_from_union_pw_multi_aff(
8386 __isl_take isl_union_pw_multi_aff
*upma
)
8388 isl_space
*space
= NULL
;
8389 isl_multi_union_pw_aff
*mupa
;
8393 n
= isl_union_pw_multi_aff_n_pw_multi_aff(upma
);
8397 isl_die(isl_union_pw_multi_aff_get_ctx(upma
), isl_error_invalid
,
8398 "cannot extract range space from empty input",
8400 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma
, &extract_space
,
8407 n
= isl_space_dim(space
, isl_dim_set
);
8409 space
= isl_space_free(space
);
8410 mupa
= isl_multi_union_pw_aff_alloc(space
);
8412 for (i
= 0; i
< n
; ++i
) {
8413 isl_union_pw_aff
*upa
;
8415 upa
= isl_union_pw_multi_aff_get_union_pw_aff(upma
, i
);
8416 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
8418 if (isl_multi_union_pw_aff_has_explicit_domain(mupa
)) {
8420 isl_union_pw_multi_aff
*copy
;
8422 copy
= isl_union_pw_multi_aff_copy(upma
);
8423 dom
= isl_union_pw_multi_aff_domain(copy
);
8424 mupa
= isl_multi_union_pw_aff_intersect_domain(mupa
, dom
);
8427 isl_union_pw_multi_aff_free(upma
);
8430 isl_space_free(space
);
8431 isl_union_pw_multi_aff_free(upma
);
8435 /* Try and create an isl_multi_union_pw_aff that is equivalent
8436 * to the given isl_union_map.
8437 * The isl_union_map is required to be single-valued in each space.
8438 * Moreover, it cannot be empty and all range spaces need to be the same.
8439 * Otherwise, an error is produced.
8441 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_from_union_map(
8442 __isl_take isl_union_map
*umap
)
8444 isl_union_pw_multi_aff
*upma
;
8446 upma
= isl_union_pw_multi_aff_from_union_map(umap
);
8447 return isl_multi_union_pw_aff_from_union_pw_multi_aff(upma
);
8450 /* Return a multiple union piecewise affine expression
8451 * that is equal to "mv" on "domain", assuming "domain" and "mv"
8452 * have been aligned.
8454 * If the resulting multi union piecewise affine expression has
8455 * an explicit domain, then assign it the input domain.
8456 * In other cases, the domain is stored in the individual elements.
8458 static __isl_give isl_multi_union_pw_aff
*
8459 isl_multi_union_pw_aff_multi_val_on_domain_aligned(
8460 __isl_take isl_union_set
*domain
, __isl_take isl_multi_val
*mv
)
8465 isl_multi_union_pw_aff
*mupa
;
8467 n
= isl_multi_val_dim(mv
, isl_dim_set
);
8468 if (!domain
|| n
< 0)
8471 space
= isl_multi_val_get_space(mv
);
8472 mupa
= isl_multi_union_pw_aff_alloc(space
);
8473 for (i
= 0; i
< n
; ++i
) {
8475 isl_union_pw_aff
*upa
;
8477 v
= isl_multi_val_get_val(mv
, i
);
8478 upa
= isl_union_pw_aff_val_on_domain(isl_union_set_copy(domain
),
8480 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
8482 if (isl_multi_union_pw_aff_has_explicit_domain(mupa
))
8483 mupa
= isl_multi_union_pw_aff_intersect_domain(mupa
,
8484 isl_union_set_copy(domain
));
8486 isl_union_set_free(domain
);
8487 isl_multi_val_free(mv
);
8490 isl_union_set_free(domain
);
8491 isl_multi_val_free(mv
);
8495 /* Return a multiple union piecewise affine expression
8496 * that is equal to "mv" on "domain".
8498 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_multi_val_on_domain(
8499 __isl_take isl_union_set
*domain
, __isl_take isl_multi_val
*mv
)
8501 isl_bool equal_params
;
8505 equal_params
= isl_space_has_equal_params(domain
->dim
, mv
->space
);
8506 if (equal_params
< 0)
8509 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(
8511 domain
= isl_union_set_align_params(domain
,
8512 isl_multi_val_get_space(mv
));
8513 mv
= isl_multi_val_align_params(mv
, isl_union_set_get_space(domain
));
8514 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(domain
, mv
);
8516 isl_union_set_free(domain
);
8517 isl_multi_val_free(mv
);
8521 /* Return a multiple union piecewise affine expression
8522 * that is equal to "ma" on "domain".
8524 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_multi_aff_on_domain(
8525 __isl_take isl_union_set
*domain
, __isl_take isl_multi_aff
*ma
)
8527 isl_pw_multi_aff
*pma
;
8529 pma
= isl_pw_multi_aff_from_multi_aff(ma
);
8530 return isl_multi_union_pw_aff_pw_multi_aff_on_domain(domain
, pma
);
8533 /* Return a multiple union piecewise affine expression
8534 * that is equal to "pma" on "domain", assuming "domain" and "pma"
8535 * have been aligned.
8537 * If the resulting multi union piecewise affine expression has
8538 * an explicit domain, then assign it the input domain.
8539 * In other cases, the domain is stored in the individual elements.
8541 static __isl_give isl_multi_union_pw_aff
*
8542 isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(
8543 __isl_take isl_union_set
*domain
, __isl_take isl_pw_multi_aff
*pma
)
8548 isl_multi_union_pw_aff
*mupa
;
8550 n
= isl_pw_multi_aff_dim(pma
, isl_dim_set
);
8551 if (!domain
|| n
< 0)
8553 space
= isl_pw_multi_aff_get_space(pma
);
8554 mupa
= isl_multi_union_pw_aff_alloc(space
);
8555 for (i
= 0; i
< n
; ++i
) {
8557 isl_union_pw_aff
*upa
;
8559 pa
= isl_pw_multi_aff_get_pw_aff(pma
, i
);
8560 upa
= isl_union_pw_aff_pw_aff_on_domain(
8561 isl_union_set_copy(domain
), pa
);
8562 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
8564 if (isl_multi_union_pw_aff_has_explicit_domain(mupa
))
8565 mupa
= isl_multi_union_pw_aff_intersect_domain(mupa
,
8566 isl_union_set_copy(domain
));
8568 isl_union_set_free(domain
);
8569 isl_pw_multi_aff_free(pma
);
8572 isl_union_set_free(domain
);
8573 isl_pw_multi_aff_free(pma
);
8577 /* Return a multiple union piecewise affine expression
8578 * that is equal to "pma" on "domain".
8580 __isl_give isl_multi_union_pw_aff
*
8581 isl_multi_union_pw_aff_pw_multi_aff_on_domain(__isl_take isl_union_set
*domain
,
8582 __isl_take isl_pw_multi_aff
*pma
)
8584 isl_bool equal_params
;
8587 space
= isl_pw_multi_aff_peek_space(pma
);
8588 equal_params
= isl_union_set_space_has_equal_params(domain
, space
);
8589 if (equal_params
< 0)
8592 return isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(
8594 domain
= isl_union_set_align_params(domain
,
8595 isl_pw_multi_aff_get_space(pma
));
8596 pma
= isl_pw_multi_aff_align_params(pma
,
8597 isl_union_set_get_space(domain
));
8598 return isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(domain
,
8601 isl_union_set_free(domain
);
8602 isl_pw_multi_aff_free(pma
);
8606 /* Return a union set containing those elements in the domains
8607 * of the elements of "mupa" where they are all zero.
8609 * If there are no elements, then simply return the entire domain.
8611 __isl_give isl_union_set
*isl_multi_union_pw_aff_zero_union_set(
8612 __isl_take isl_multi_union_pw_aff
*mupa
)
8616 isl_union_pw_aff
*upa
;
8617 isl_union_set
*zero
;
8619 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8621 mupa
= isl_multi_union_pw_aff_free(mupa
);
8626 return isl_multi_union_pw_aff_domain(mupa
);
8628 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, 0);
8629 zero
= isl_union_pw_aff_zero_union_set(upa
);
8631 for (i
= 1; i
< n
; ++i
) {
8632 isl_union_set
*zero_i
;
8634 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
8635 zero_i
= isl_union_pw_aff_zero_union_set(upa
);
8637 zero
= isl_union_set_intersect(zero
, zero_i
);
8640 isl_multi_union_pw_aff_free(mupa
);
8644 /* Construct a union map mapping the shared domain
8645 * of the union piecewise affine expressions to the range of "mupa"
8646 * in the special case of a 0D multi union piecewise affine expression.
8648 * Construct a map between the explicit domain of "mupa" and
8650 * Note that this assumes that the domain consists of explicit elements.
8652 static __isl_give isl_union_map
*isl_union_map_from_multi_union_pw_aff_0D(
8653 __isl_take isl_multi_union_pw_aff
*mupa
)
8657 isl_union_set
*dom
, *ran
;
8659 space
= isl_multi_union_pw_aff_get_space(mupa
);
8660 dom
= isl_multi_union_pw_aff_domain(mupa
);
8661 ran
= isl_union_set_from_set(isl_set_universe(space
));
8663 is_params
= isl_union_set_is_params(dom
);
8665 dom
= isl_union_set_free(dom
);
8667 isl_die(isl_union_set_get_ctx(dom
), isl_error_invalid
,
8668 "cannot create union map from expression without "
8669 "explicit domain elements",
8670 dom
= isl_union_set_free(dom
));
8672 return isl_union_map_from_domain_and_range(dom
, ran
);
8675 /* Construct a union map mapping the shared domain
8676 * of the union piecewise affine expressions to the range of "mupa"
8677 * with each dimension in the range equated to the
8678 * corresponding union piecewise affine expression.
8680 * If the input is zero-dimensional, then construct a mapping
8681 * from its explicit domain.
8683 __isl_give isl_union_map
*isl_union_map_from_multi_union_pw_aff(
8684 __isl_take isl_multi_union_pw_aff
*mupa
)
8689 isl_union_map
*umap
;
8690 isl_union_pw_aff
*upa
;
8692 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8694 mupa
= isl_multi_union_pw_aff_free(mupa
);
8699 return isl_union_map_from_multi_union_pw_aff_0D(mupa
);
8701 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, 0);
8702 umap
= isl_union_map_from_union_pw_aff(upa
);
8704 for (i
= 1; i
< n
; ++i
) {
8705 isl_union_map
*umap_i
;
8707 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
8708 umap_i
= isl_union_map_from_union_pw_aff(upa
);
8709 umap
= isl_union_map_flat_range_product(umap
, umap_i
);
8712 space
= isl_multi_union_pw_aff_get_space(mupa
);
8713 umap
= isl_union_map_reset_range_space(umap
, space
);
8715 isl_multi_union_pw_aff_free(mupa
);
8719 /* Internal data structure for isl_union_pw_multi_aff_reset_range_space.
8720 * "range" is the space from which to set the range space.
8721 * "res" collects the results.
8723 struct isl_union_pw_multi_aff_reset_range_space_data
{
8725 isl_union_pw_multi_aff
*res
;
8728 /* Replace the range space of "pma" by the range space of data->range and
8729 * add the result to data->res.
8731 static isl_stat
reset_range_space(__isl_take isl_pw_multi_aff
*pma
, void *user
)
8733 struct isl_union_pw_multi_aff_reset_range_space_data
*data
= user
;
8736 space
= isl_pw_multi_aff_get_space(pma
);
8737 space
= isl_space_domain(space
);
8738 space
= isl_space_extend_domain_with_range(space
,
8739 isl_space_copy(data
->range
));
8740 pma
= isl_pw_multi_aff_reset_space(pma
, space
);
8741 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma
);
8743 return data
->res
? isl_stat_ok
: isl_stat_error
;
8746 /* Replace the range space of all the piecewise affine expressions in "upma" by
8747 * the range space of "space".
8749 * This assumes that all these expressions have the same output dimension.
8751 * Since the spaces of the expressions change, so do their hash values.
8752 * We therefore need to create a new isl_union_pw_multi_aff.
8753 * Note that the hash value is currently computed based on the entire
8754 * space even though there can only be a single expression with a given
8757 static __isl_give isl_union_pw_multi_aff
*
8758 isl_union_pw_multi_aff_reset_range_space(
8759 __isl_take isl_union_pw_multi_aff
*upma
, __isl_take isl_space
*space
)
8761 struct isl_union_pw_multi_aff_reset_range_space_data data
= { space
};
8762 isl_space
*space_upma
;
8764 space_upma
= isl_union_pw_multi_aff_get_space(upma
);
8765 data
.res
= isl_union_pw_multi_aff_empty(space_upma
);
8766 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma
,
8767 &reset_range_space
, &data
) < 0)
8768 data
.res
= isl_union_pw_multi_aff_free(data
.res
);
8770 isl_space_free(space
);
8771 isl_union_pw_multi_aff_free(upma
);
8775 /* Construct and return a union piecewise multi affine expression
8776 * that is equal to the given multi union piecewise affine expression,
8777 * in the special case of a 0D multi union piecewise affine expression.
8779 * Construct a union piecewise multi affine expression
8780 * on top of the explicit domain of the input.
8782 __isl_give isl_union_pw_multi_aff
*
8783 isl_union_pw_multi_aff_from_multi_union_pw_aff_0D(
8784 __isl_take isl_multi_union_pw_aff
*mupa
)
8788 isl_union_set
*domain
;
8790 space
= isl_multi_union_pw_aff_get_space(mupa
);
8791 mv
= isl_multi_val_zero(space
);
8792 domain
= isl_multi_union_pw_aff_domain(mupa
);
8793 return isl_union_pw_multi_aff_multi_val_on_domain(domain
, mv
);
8796 /* Construct and return a union piecewise multi affine expression
8797 * that is equal to the given multi union piecewise affine expression.
8799 * If the input is zero-dimensional, then
8800 * construct a union piecewise multi affine expression
8801 * on top of the explicit domain of the input.
8803 __isl_give isl_union_pw_multi_aff
*
8804 isl_union_pw_multi_aff_from_multi_union_pw_aff(
8805 __isl_take isl_multi_union_pw_aff
*mupa
)
8810 isl_union_pw_multi_aff
*upma
;
8811 isl_union_pw_aff
*upa
;
8813 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8815 mupa
= isl_multi_union_pw_aff_free(mupa
);
8820 return isl_union_pw_multi_aff_from_multi_union_pw_aff_0D(mupa
);
8822 space
= isl_multi_union_pw_aff_get_space(mupa
);
8823 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, 0);
8824 upma
= isl_union_pw_multi_aff_from_union_pw_aff(upa
);
8826 for (i
= 1; i
< n
; ++i
) {
8827 isl_union_pw_multi_aff
*upma_i
;
8829 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
8830 upma_i
= isl_union_pw_multi_aff_from_union_pw_aff(upa
);
8831 upma
= isl_union_pw_multi_aff_flat_range_product(upma
, upma_i
);
8834 upma
= isl_union_pw_multi_aff_reset_range_space(upma
, space
);
8836 isl_multi_union_pw_aff_free(mupa
);
8840 /* Intersect the range of "mupa" with "range",
8841 * in the special case where "mupa" is 0D.
8843 * Intersect the domain of "mupa" with the constraints on the parameters
8846 static __isl_give isl_multi_union_pw_aff
*mupa_intersect_range_0D(
8847 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_set
*range
)
8849 range
= isl_set_params(range
);
8850 mupa
= isl_multi_union_pw_aff_intersect_params(mupa
, range
);
8854 /* Intersect the range of "mupa" with "range".
8855 * That is, keep only those domain elements that have a function value
8858 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_intersect_range(
8859 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_set
*range
)
8861 isl_union_pw_multi_aff
*upma
;
8862 isl_union_set
*domain
;
8867 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8868 if (n
< 0 || !range
)
8871 space
= isl_set_get_space(range
);
8872 match
= isl_space_tuple_is_equal(mupa
->space
, isl_dim_set
,
8873 space
, isl_dim_set
);
8874 isl_space_free(space
);
8878 isl_die(isl_multi_union_pw_aff_get_ctx(mupa
), isl_error_invalid
,
8879 "space don't match", goto error
);
8881 return mupa_intersect_range_0D(mupa
, range
);
8883 upma
= isl_union_pw_multi_aff_from_multi_union_pw_aff(
8884 isl_multi_union_pw_aff_copy(mupa
));
8885 domain
= isl_union_set_from_set(range
);
8886 domain
= isl_union_set_preimage_union_pw_multi_aff(domain
, upma
);
8887 mupa
= isl_multi_union_pw_aff_intersect_domain(mupa
, domain
);
8891 isl_multi_union_pw_aff_free(mupa
);
8892 isl_set_free(range
);
8896 /* Return the shared domain of the elements of "mupa",
8897 * in the special case where "mupa" is zero-dimensional.
8899 * Return the explicit domain of "mupa".
8900 * Note that this domain may be a parameter set, either
8901 * because "mupa" is meant to live in a set space or
8902 * because no explicit domain has been set.
8904 __isl_give isl_union_set
*isl_multi_union_pw_aff_domain_0D(
8905 __isl_take isl_multi_union_pw_aff
*mupa
)
8909 dom
= isl_multi_union_pw_aff_get_explicit_domain(mupa
);
8910 isl_multi_union_pw_aff_free(mupa
);
8915 /* Return the shared domain of the elements of "mupa".
8917 * If "mupa" is zero-dimensional, then return its explicit domain.
8919 __isl_give isl_union_set
*isl_multi_union_pw_aff_domain(
8920 __isl_take isl_multi_union_pw_aff
*mupa
)
8924 isl_union_pw_aff
*upa
;
8927 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8929 mupa
= isl_multi_union_pw_aff_free(mupa
);
8934 return isl_multi_union_pw_aff_domain_0D(mupa
);
8936 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, 0);
8937 dom
= isl_union_pw_aff_domain(upa
);
8938 for (i
= 1; i
< n
; ++i
) {
8939 isl_union_set
*dom_i
;
8941 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
8942 dom_i
= isl_union_pw_aff_domain(upa
);
8943 dom
= isl_union_set_intersect(dom
, dom_i
);
8946 isl_multi_union_pw_aff_free(mupa
);
8950 /* Apply "aff" to "mupa". The space of "mupa" is equal to the domain of "aff".
8951 * In particular, the spaces have been aligned.
8952 * The result is defined over the shared domain of the elements of "mupa"
8954 * We first extract the parametric constant part of "aff" and
8955 * define that over the shared domain.
8956 * Then we iterate over all input dimensions of "aff" and add the corresponding
8957 * multiples of the elements of "mupa".
8958 * Finally, we consider the integer divisions, calling the function
8959 * recursively to obtain an isl_union_pw_aff corresponding to the
8960 * integer division argument.
8962 static __isl_give isl_union_pw_aff
*multi_union_pw_aff_apply_aff(
8963 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_aff
*aff
)
8966 isl_size n_in
, n_div
;
8967 isl_union_pw_aff
*upa
;
8968 isl_union_set
*uset
;
8972 n_in
= isl_aff_dim(aff
, isl_dim_in
);
8973 n_div
= isl_aff_dim(aff
, isl_dim_div
);
8974 if (n_in
< 0 || n_div
< 0)
8977 uset
= isl_multi_union_pw_aff_domain(isl_multi_union_pw_aff_copy(mupa
));
8978 cst
= isl_aff_copy(aff
);
8979 cst
= isl_aff_drop_dims(cst
, isl_dim_div
, 0, n_div
);
8980 cst
= isl_aff_drop_dims(cst
, isl_dim_in
, 0, n_in
);
8981 cst
= isl_aff_project_domain_on_params(cst
);
8982 upa
= isl_union_pw_aff_aff_on_domain(uset
, cst
);
8984 for (i
= 0; i
< n_in
; ++i
) {
8985 isl_union_pw_aff
*upa_i
;
8987 if (!isl_aff_involves_dims(aff
, isl_dim_in
, i
, 1))
8989 v
= isl_aff_get_coefficient_val(aff
, isl_dim_in
, i
);
8990 upa_i
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
8991 upa_i
= isl_union_pw_aff_scale_val(upa_i
, v
);
8992 upa
= isl_union_pw_aff_add(upa
, upa_i
);
8995 for (i
= 0; i
< n_div
; ++i
) {
8997 isl_union_pw_aff
*upa_i
;
8999 if (!isl_aff_involves_dims(aff
, isl_dim_div
, i
, 1))
9001 div
= isl_aff_get_div(aff
, i
);
9002 upa_i
= multi_union_pw_aff_apply_aff(
9003 isl_multi_union_pw_aff_copy(mupa
), div
);
9004 upa_i
= isl_union_pw_aff_floor(upa_i
);
9005 v
= isl_aff_get_coefficient_val(aff
, isl_dim_div
, i
);
9006 upa_i
= isl_union_pw_aff_scale_val(upa_i
, v
);
9007 upa
= isl_union_pw_aff_add(upa
, upa_i
);
9010 isl_multi_union_pw_aff_free(mupa
);
9015 isl_multi_union_pw_aff_free(mupa
);
9020 /* Apply "aff" to "mupa". The space of "mupa" needs to be compatible
9021 * with the domain of "aff".
9022 * Furthermore, the dimension of this space needs to be greater than zero.
9023 * The result is defined over the shared domain of the elements of "mupa"
9025 * We perform these checks and then hand over control to
9026 * multi_union_pw_aff_apply_aff.
9028 __isl_give isl_union_pw_aff
*isl_multi_union_pw_aff_apply_aff(
9029 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_aff
*aff
)
9032 isl_space
*space1
, *space2
;
9035 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
9036 isl_aff_get_space(aff
));
9037 aff
= isl_aff_align_params(aff
, isl_multi_union_pw_aff_get_space(mupa
));
9041 space1
= isl_multi_union_pw_aff_get_space(mupa
);
9042 space2
= isl_aff_get_domain_space(aff
);
9043 equal
= isl_space_is_equal(space1
, space2
);
9044 isl_space_free(space1
);
9045 isl_space_free(space2
);
9049 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
9050 "spaces don't match", goto error
);
9051 dim
= isl_aff_dim(aff
, isl_dim_in
);
9055 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
9056 "cannot determine domains", goto error
);
9058 return multi_union_pw_aff_apply_aff(mupa
, aff
);
9060 isl_multi_union_pw_aff_free(mupa
);
9065 /* Apply "ma" to "mupa", in the special case where "mupa" is 0D.
9066 * The space of "mupa" is known to be compatible with the domain of "ma".
9068 * Construct an isl_multi_union_pw_aff that is equal to "ma"
9069 * on the domain of "mupa".
9071 static __isl_give isl_multi_union_pw_aff
*mupa_apply_multi_aff_0D(
9072 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_multi_aff
*ma
)
9076 dom
= isl_multi_union_pw_aff_domain(mupa
);
9077 ma
= isl_multi_aff_project_domain_on_params(ma
);
9079 return isl_multi_union_pw_aff_multi_aff_on_domain(dom
, ma
);
9082 /* Apply "ma" to "mupa". The space of "mupa" needs to be compatible
9083 * with the domain of "ma".
9084 * The result is defined over the shared domain of the elements of "mupa"
9086 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_apply_multi_aff(
9087 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_multi_aff
*ma
)
9089 isl_space
*space1
, *space2
;
9090 isl_multi_union_pw_aff
*res
;
9093 isl_size n_in
, n_out
;
9095 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
9096 isl_multi_aff_get_space(ma
));
9097 ma
= isl_multi_aff_align_params(ma
,
9098 isl_multi_union_pw_aff_get_space(mupa
));
9099 n_in
= isl_multi_aff_dim(ma
, isl_dim_in
);
9100 n_out
= isl_multi_aff_dim(ma
, isl_dim_out
);
9101 if (!mupa
|| n_in
< 0 || n_out
< 0)
9104 space1
= isl_multi_union_pw_aff_get_space(mupa
);
9105 space2
= isl_multi_aff_get_domain_space(ma
);
9106 equal
= isl_space_is_equal(space1
, space2
);
9107 isl_space_free(space1
);
9108 isl_space_free(space2
);
9112 isl_die(isl_multi_aff_get_ctx(ma
), isl_error_invalid
,
9113 "spaces don't match", goto error
);
9115 return mupa_apply_multi_aff_0D(mupa
, ma
);
9117 space1
= isl_space_range(isl_multi_aff_get_space(ma
));
9118 res
= isl_multi_union_pw_aff_alloc(space1
);
9120 for (i
= 0; i
< n_out
; ++i
) {
9122 isl_union_pw_aff
*upa
;
9124 aff
= isl_multi_aff_get_aff(ma
, i
);
9125 upa
= multi_union_pw_aff_apply_aff(
9126 isl_multi_union_pw_aff_copy(mupa
), aff
);
9127 res
= isl_multi_union_pw_aff_set_union_pw_aff(res
, i
, upa
);
9130 isl_multi_aff_free(ma
);
9131 isl_multi_union_pw_aff_free(mupa
);
9134 isl_multi_union_pw_aff_free(mupa
);
9135 isl_multi_aff_free(ma
);
9139 /* Apply "pa" to "mupa", in the special case where "mupa" is 0D.
9140 * The space of "mupa" is known to be compatible with the domain of "pa".
9142 * Construct an isl_multi_union_pw_aff that is equal to "pa"
9143 * on the domain of "mupa".
9145 static __isl_give isl_union_pw_aff
*isl_multi_union_pw_aff_apply_pw_aff_0D(
9146 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_pw_aff
*pa
)
9150 dom
= isl_multi_union_pw_aff_domain(mupa
);
9151 pa
= isl_pw_aff_project_domain_on_params(pa
);
9153 return isl_union_pw_aff_pw_aff_on_domain(dom
, pa
);
9156 /* Apply "pa" to "mupa". The space of "mupa" needs to be compatible
9157 * with the domain of "pa".
9158 * Furthermore, the dimension of this space needs to be greater than zero.
9159 * The result is defined over the shared domain of the elements of "mupa"
9161 __isl_give isl_union_pw_aff
*isl_multi_union_pw_aff_apply_pw_aff(
9162 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_pw_aff
*pa
)
9167 isl_space
*space
, *space2
;
9168 isl_union_pw_aff
*upa
;
9170 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
9171 isl_pw_aff_get_space(pa
));
9172 pa
= isl_pw_aff_align_params(pa
,
9173 isl_multi_union_pw_aff_get_space(mupa
));
9177 space
= isl_multi_union_pw_aff_get_space(mupa
);
9178 space2
= isl_pw_aff_get_domain_space(pa
);
9179 equal
= isl_space_is_equal(space
, space2
);
9180 isl_space_free(space
);
9181 isl_space_free(space2
);
9185 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
9186 "spaces don't match", goto error
);
9187 n_in
= isl_pw_aff_dim(pa
, isl_dim_in
);
9191 return isl_multi_union_pw_aff_apply_pw_aff_0D(mupa
, pa
);
9193 space
= isl_space_params(isl_multi_union_pw_aff_get_space(mupa
));
9194 upa
= isl_union_pw_aff_empty(space
);
9196 for (i
= 0; i
< pa
->n
; ++i
) {
9199 isl_multi_union_pw_aff
*mupa_i
;
9200 isl_union_pw_aff
*upa_i
;
9202 mupa_i
= isl_multi_union_pw_aff_copy(mupa
);
9203 domain
= isl_set_copy(pa
->p
[i
].set
);
9204 mupa_i
= isl_multi_union_pw_aff_intersect_range(mupa_i
, domain
);
9205 aff
= isl_aff_copy(pa
->p
[i
].aff
);
9206 upa_i
= multi_union_pw_aff_apply_aff(mupa_i
, aff
);
9207 upa
= isl_union_pw_aff_union_add(upa
, upa_i
);
9210 isl_multi_union_pw_aff_free(mupa
);
9211 isl_pw_aff_free(pa
);
9214 isl_multi_union_pw_aff_free(mupa
);
9215 isl_pw_aff_free(pa
);
9219 /* Apply "pma" to "mupa", in the special case where "mupa" is 0D.
9220 * The space of "mupa" is known to be compatible with the domain of "pma".
9222 * Construct an isl_multi_union_pw_aff that is equal to "pma"
9223 * on the domain of "mupa".
9225 static __isl_give isl_multi_union_pw_aff
*mupa_apply_pw_multi_aff_0D(
9226 __isl_take isl_multi_union_pw_aff
*mupa
,
9227 __isl_take isl_pw_multi_aff
*pma
)
9231 dom
= isl_multi_union_pw_aff_domain(mupa
);
9232 pma
= isl_pw_multi_aff_project_domain_on_params(pma
);
9234 return isl_multi_union_pw_aff_pw_multi_aff_on_domain(dom
, pma
);
9237 /* Apply "pma" to "mupa". The space of "mupa" needs to be compatible
9238 * with the domain of "pma".
9239 * The result is defined over the shared domain of the elements of "mupa"
9241 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_apply_pw_multi_aff(
9242 __isl_take isl_multi_union_pw_aff
*mupa
,
9243 __isl_take isl_pw_multi_aff
*pma
)
9245 isl_space
*space1
, *space2
;
9246 isl_multi_union_pw_aff
*res
;
9249 isl_size n_in
, n_out
;
9251 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
9252 isl_pw_multi_aff_get_space(pma
));
9253 pma
= isl_pw_multi_aff_align_params(pma
,
9254 isl_multi_union_pw_aff_get_space(mupa
));
9258 space1
= isl_multi_union_pw_aff_get_space(mupa
);
9259 space2
= isl_pw_multi_aff_get_domain_space(pma
);
9260 equal
= isl_space_is_equal(space1
, space2
);
9261 isl_space_free(space1
);
9262 isl_space_free(space2
);
9266 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
9267 "spaces don't match", goto error
);
9268 n_in
= isl_pw_multi_aff_dim(pma
, isl_dim_in
);
9269 n_out
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
9270 if (n_in
< 0 || n_out
< 0)
9273 return mupa_apply_pw_multi_aff_0D(mupa
, pma
);
9275 space1
= isl_space_range(isl_pw_multi_aff_get_space(pma
));
9276 res
= isl_multi_union_pw_aff_alloc(space1
);
9278 for (i
= 0; i
< n_out
; ++i
) {
9280 isl_union_pw_aff
*upa
;
9282 pa
= isl_pw_multi_aff_get_pw_aff(pma
, i
);
9283 upa
= isl_multi_union_pw_aff_apply_pw_aff(
9284 isl_multi_union_pw_aff_copy(mupa
), pa
);
9285 res
= isl_multi_union_pw_aff_set_union_pw_aff(res
, i
, upa
);
9288 isl_pw_multi_aff_free(pma
);
9289 isl_multi_union_pw_aff_free(mupa
);
9292 isl_multi_union_pw_aff_free(mupa
);
9293 isl_pw_multi_aff_free(pma
);
9297 /* Replace the explicit domain of "mupa" by its preimage under "upma".
9298 * If the explicit domain only keeps track of constraints on the parameters,
9299 * then only update those constraints.
9301 static __isl_give isl_multi_union_pw_aff
*preimage_explicit_domain(
9302 __isl_take isl_multi_union_pw_aff
*mupa
,
9303 __isl_keep isl_union_pw_multi_aff
*upma
)
9307 if (isl_multi_union_pw_aff_check_has_explicit_domain(mupa
) < 0)
9308 return isl_multi_union_pw_aff_free(mupa
);
9310 mupa
= isl_multi_union_pw_aff_cow(mupa
);
9314 is_params
= isl_union_set_is_params(mupa
->u
.dom
);
9316 return isl_multi_union_pw_aff_free(mupa
);
9318 upma
= isl_union_pw_multi_aff_copy(upma
);
9320 mupa
->u
.dom
= isl_union_set_intersect_params(mupa
->u
.dom
,
9321 isl_union_set_params(isl_union_pw_multi_aff_domain(upma
)));
9323 mupa
->u
.dom
= isl_union_set_preimage_union_pw_multi_aff(
9326 return isl_multi_union_pw_aff_free(mupa
);
9330 /* Compute the pullback of "mupa" by the function represented by "upma".
9331 * In other words, plug in "upma" in "mupa". The result contains
9332 * expressions defined over the domain space of "upma".
9334 * Run over all elements of "mupa" and plug in "upma" in each of them.
9336 * If "mupa" has an explicit domain, then it is this domain
9337 * that needs to undergo a pullback instead, i.e., a preimage.
9339 __isl_give isl_multi_union_pw_aff
*
9340 isl_multi_union_pw_aff_pullback_union_pw_multi_aff(
9341 __isl_take isl_multi_union_pw_aff
*mupa
,
9342 __isl_take isl_union_pw_multi_aff
*upma
)
9347 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
9348 isl_union_pw_multi_aff_get_space(upma
));
9349 upma
= isl_union_pw_multi_aff_align_params(upma
,
9350 isl_multi_union_pw_aff_get_space(mupa
));
9351 mupa
= isl_multi_union_pw_aff_cow(mupa
);
9352 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
9356 for (i
= 0; i
< n
; ++i
) {
9357 isl_union_pw_aff
*upa
;
9359 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
9360 upa
= isl_union_pw_aff_pullback_union_pw_multi_aff(upa
,
9361 isl_union_pw_multi_aff_copy(upma
));
9362 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
9365 if (isl_multi_union_pw_aff_has_explicit_domain(mupa
))
9366 mupa
= preimage_explicit_domain(mupa
, upma
);
9368 isl_union_pw_multi_aff_free(upma
);
9371 isl_multi_union_pw_aff_free(mupa
);
9372 isl_union_pw_multi_aff_free(upma
);
9376 /* Extract the sequence of elements in "mupa" with domain space "space"
9377 * (ignoring parameters).
9379 * For the elements of "mupa" that are not defined on the specified space,
9380 * the corresponding element in the result is empty.
9382 __isl_give isl_multi_pw_aff
*isl_multi_union_pw_aff_extract_multi_pw_aff(
9383 __isl_keep isl_multi_union_pw_aff
*mupa
, __isl_take isl_space
*space
)
9387 isl_space
*space_mpa
;
9388 isl_multi_pw_aff
*mpa
;
9390 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
9391 if (n
< 0 || !space
)
9394 space_mpa
= isl_multi_union_pw_aff_get_space(mupa
);
9395 space
= isl_space_replace_params(space
, space_mpa
);
9396 space_mpa
= isl_space_map_from_domain_and_range(isl_space_copy(space
),
9398 mpa
= isl_multi_pw_aff_alloc(space_mpa
);
9400 space
= isl_space_from_domain(space
);
9401 space
= isl_space_add_dims(space
, isl_dim_out
, 1);
9402 for (i
= 0; i
< n
; ++i
) {
9403 isl_union_pw_aff
*upa
;
9406 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
9407 pa
= isl_union_pw_aff_extract_pw_aff(upa
,
9408 isl_space_copy(space
));
9409 mpa
= isl_multi_pw_aff_set_pw_aff(mpa
, i
, pa
);
9410 isl_union_pw_aff_free(upa
);
9413 isl_space_free(space
);
9416 isl_space_free(space
);
9420 /* Evaluate the affine function "aff" in the void point "pnt".
9421 * In particular, return the value NaN.
9423 static __isl_give isl_val
*eval_void(__isl_take isl_aff
*aff
,
9424 __isl_take isl_point
*pnt
)
9428 ctx
= isl_point_get_ctx(pnt
);
9430 isl_point_free(pnt
);
9431 return isl_val_nan(ctx
);
9434 /* Evaluate the affine expression "aff"
9435 * in the coordinates (with denominator) "pnt".
9437 static __isl_give isl_val
*eval(__isl_keep isl_vec
*aff
,
9438 __isl_keep isl_vec
*pnt
)
9447 ctx
= isl_vec_get_ctx(aff
);
9450 isl_seq_inner_product(aff
->el
+ 1, pnt
->el
, pnt
->size
, &n
);
9451 isl_int_mul(d
, aff
->el
[0], pnt
->el
[0]);
9452 v
= isl_val_rat_from_isl_int(ctx
, n
, d
);
9453 v
= isl_val_normalize(v
);
9460 /* Check that the domain space of "aff" is equal to "space".
9462 static isl_stat
isl_aff_check_has_domain_space(__isl_keep isl_aff
*aff
,
9463 __isl_keep isl_space
*space
)
9467 ok
= isl_space_is_equal(isl_aff_peek_domain_space(aff
), space
);
9469 return isl_stat_error
;
9471 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
9472 "incompatible spaces", return isl_stat_error
);
9476 /* Evaluate the affine function "aff" in "pnt".
9478 __isl_give isl_val
*isl_aff_eval(__isl_take isl_aff
*aff
,
9479 __isl_take isl_point
*pnt
)
9483 isl_local_space
*ls
;
9485 if (isl_aff_check_has_domain_space(aff
, isl_point_peek_space(pnt
)) < 0)
9487 is_void
= isl_point_is_void(pnt
);
9491 return eval_void(aff
, pnt
);
9493 ls
= isl_aff_get_domain_local_space(aff
);
9494 pnt
= isl_local_space_lift_point(ls
, pnt
);
9496 v
= eval(aff
->v
, isl_point_peek_vec(pnt
));
9499 isl_point_free(pnt
);
9504 isl_point_free(pnt
);