2 * Copyright 2011 INRIA Saclay
3 * Copyright 2011 Sven Verdoolaege
4 * Copyright 2012-2014 Ecole Normale Superieure
5 * Copyright 2014 INRIA Rocquencourt
6 * Copyright 2016 Sven Verdoolaege
7 * Copyright 2018 Cerebras Systems
9 * Use of this software is governed by the MIT license
11 * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
12 * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
14 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
15 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
16 * B.P. 105 - 78153 Le Chesnay, France
17 * and Cerebras Systems, 175 S San Antonio Rd, Los Altos, CA, USA
20 #include <isl_ctx_private.h>
21 #include <isl_map_private.h>
22 #include <isl_union_map_private.h>
23 #include <isl_aff_private.h>
24 #include <isl_space_private.h>
25 #include <isl_local_space_private.h>
26 #include <isl_vec_private.h>
27 #include <isl_mat_private.h>
28 #include <isl_id_private.h>
29 #include <isl/constraint.h>
32 #include <isl_val_private.h>
33 #include <isl_point_private.h>
34 #include <isl_config.h>
39 #include <isl_list_templ.c>
42 #define EL_BASE pw_aff
44 #include <isl_list_templ.c>
47 #define EL_BASE pw_multi_aff
49 #include <isl_list_templ.c>
52 #define EL_BASE union_pw_aff
54 #include <isl_list_templ.c>
57 #define EL_BASE union_pw_multi_aff
59 #include <isl_list_templ.c>
61 __isl_give isl_aff
*isl_aff_alloc_vec(__isl_take isl_local_space
*ls
,
62 __isl_take isl_vec
*v
)
69 aff
= isl_calloc_type(v
->ctx
, struct isl_aff
);
79 isl_local_space_free(ls
);
84 __isl_give isl_aff
*isl_aff_alloc(__isl_take isl_local_space
*ls
)
93 ctx
= isl_local_space_get_ctx(ls
);
94 if (!isl_local_space_divs_known(ls
))
95 isl_die(ctx
, isl_error_invalid
, "local space has unknown divs",
97 if (!isl_local_space_is_set(ls
))
98 isl_die(ctx
, isl_error_invalid
,
99 "domain of affine expression should be a set",
102 total
= isl_local_space_dim(ls
, isl_dim_all
);
105 v
= isl_vec_alloc(ctx
, 1 + 1 + total
);
106 return isl_aff_alloc_vec(ls
, v
);
108 isl_local_space_free(ls
);
112 __isl_give isl_aff
*isl_aff_zero_on_domain(__isl_take isl_local_space
*ls
)
116 aff
= isl_aff_alloc(ls
);
120 isl_int_set_si(aff
->v
->el
[0], 1);
121 isl_seq_clr(aff
->v
->el
+ 1, aff
->v
->size
- 1);
126 /* Return an affine expression that is equal to zero on domain space "space".
128 __isl_give isl_aff
*isl_aff_zero_on_domain_space(__isl_take isl_space
*space
)
130 return isl_aff_zero_on_domain(isl_local_space_from_space(space
));
133 /* Return a piecewise affine expression defined on the specified domain
134 * that is equal to zero.
136 __isl_give isl_pw_aff
*isl_pw_aff_zero_on_domain(__isl_take isl_local_space
*ls
)
138 return isl_pw_aff_from_aff(isl_aff_zero_on_domain(ls
));
141 /* Return an affine expression defined on the specified domain
142 * that represents NaN.
144 __isl_give isl_aff
*isl_aff_nan_on_domain(__isl_take isl_local_space
*ls
)
148 aff
= isl_aff_alloc(ls
);
152 isl_seq_clr(aff
->v
->el
, aff
->v
->size
);
157 /* Return a piecewise affine expression defined on the specified domain
158 * that represents NaN.
160 __isl_give isl_pw_aff
*isl_pw_aff_nan_on_domain(__isl_take isl_local_space
*ls
)
162 return isl_pw_aff_from_aff(isl_aff_nan_on_domain(ls
));
165 /* Return an affine expression that is equal to "val" on
166 * domain local space "ls".
168 __isl_give isl_aff
*isl_aff_val_on_domain(__isl_take isl_local_space
*ls
,
169 __isl_take isl_val
*val
)
175 if (!isl_val_is_rat(val
))
176 isl_die(isl_val_get_ctx(val
), isl_error_invalid
,
177 "expecting rational value", goto error
);
179 aff
= isl_aff_alloc(isl_local_space_copy(ls
));
183 isl_seq_clr(aff
->v
->el
+ 2, aff
->v
->size
- 2);
184 isl_int_set(aff
->v
->el
[1], val
->n
);
185 isl_int_set(aff
->v
->el
[0], val
->d
);
187 isl_local_space_free(ls
);
191 isl_local_space_free(ls
);
196 /* Return an affine expression that is equal to "val" on domain space "space".
198 __isl_give isl_aff
*isl_aff_val_on_domain_space(__isl_take isl_space
*space
,
199 __isl_take isl_val
*val
)
201 return isl_aff_val_on_domain(isl_local_space_from_space(space
), val
);
204 /* Return an affine expression that is equal to the specified dimension
207 __isl_give isl_aff
*isl_aff_var_on_domain(__isl_take isl_local_space
*ls
,
208 enum isl_dim_type type
, unsigned pos
)
216 space
= isl_local_space_get_space(ls
);
219 if (isl_space_is_map(space
))
220 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
221 "expecting (parameter) set space", goto error
);
222 if (isl_local_space_check_range(ls
, type
, pos
, 1) < 0)
225 isl_space_free(space
);
226 aff
= isl_aff_alloc(ls
);
230 pos
+= isl_local_space_offset(aff
->ls
, type
);
232 isl_int_set_si(aff
->v
->el
[0], 1);
233 isl_seq_clr(aff
->v
->el
+ 1, aff
->v
->size
- 1);
234 isl_int_set_si(aff
->v
->el
[1 + pos
], 1);
238 isl_local_space_free(ls
);
239 isl_space_free(space
);
243 /* Return a piecewise affine expression that is equal to
244 * the specified dimension in "ls".
246 __isl_give isl_pw_aff
*isl_pw_aff_var_on_domain(__isl_take isl_local_space
*ls
,
247 enum isl_dim_type type
, unsigned pos
)
249 return isl_pw_aff_from_aff(isl_aff_var_on_domain(ls
, type
, pos
));
252 /* Return an affine expression that is equal to the parameter
253 * in the domain space "space" with identifier "id".
255 __isl_give isl_aff
*isl_aff_param_on_domain_space_id(
256 __isl_take isl_space
*space
, __isl_take isl_id
*id
)
263 pos
= isl_space_find_dim_by_id(space
, isl_dim_param
, id
);
265 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
266 "parameter not found in space", goto error
);
268 ls
= isl_local_space_from_space(space
);
269 return isl_aff_var_on_domain(ls
, isl_dim_param
, pos
);
271 isl_space_free(space
);
276 __isl_give isl_aff
*isl_aff_copy(__isl_keep isl_aff
*aff
)
285 __isl_give isl_aff
*isl_aff_dup(__isl_keep isl_aff
*aff
)
290 return isl_aff_alloc_vec(isl_local_space_copy(aff
->ls
),
291 isl_vec_copy(aff
->v
));
294 __isl_give isl_aff
*isl_aff_cow(__isl_take isl_aff
*aff
)
302 return isl_aff_dup(aff
);
305 __isl_null isl_aff
*isl_aff_free(__isl_take isl_aff
*aff
)
313 isl_local_space_free(aff
->ls
);
314 isl_vec_free(aff
->v
);
321 isl_ctx
*isl_aff_get_ctx(__isl_keep isl_aff
*aff
)
323 return aff
? isl_local_space_get_ctx(aff
->ls
) : NULL
;
326 /* Return a hash value that digests "aff".
328 uint32_t isl_aff_get_hash(__isl_keep isl_aff
*aff
)
330 uint32_t hash
, ls_hash
, v_hash
;
335 hash
= isl_hash_init();
336 ls_hash
= isl_local_space_get_hash(aff
->ls
);
337 isl_hash_hash(hash
, ls_hash
);
338 v_hash
= isl_vec_get_hash(aff
->v
);
339 isl_hash_hash(hash
, v_hash
);
344 /* Return the domain local space of "aff".
346 static __isl_keep isl_local_space
*isl_aff_peek_domain_local_space(
347 __isl_keep isl_aff
*aff
)
349 return aff
? aff
->ls
: NULL
;
352 /* Return the number of variables of the given type in the domain of "aff".
354 isl_size
isl_aff_domain_dim(__isl_keep isl_aff
*aff
, enum isl_dim_type type
)
358 ls
= isl_aff_peek_domain_local_space(aff
);
359 return isl_local_space_dim(ls
, type
);
362 /* Externally, an isl_aff has a map space, but internally, the
363 * ls field corresponds to the domain of that space.
365 isl_size
isl_aff_dim(__isl_keep isl_aff
*aff
, enum isl_dim_type type
)
368 return isl_size_error
;
369 if (type
== isl_dim_out
)
371 if (type
== isl_dim_in
)
373 return isl_aff_domain_dim(aff
, type
);
376 /* Return the offset of the first coefficient of type "type" in
377 * the domain of "aff".
379 isl_size
isl_aff_domain_offset(__isl_keep isl_aff
*aff
, enum isl_dim_type type
)
383 ls
= isl_aff_peek_domain_local_space(aff
);
384 return isl_local_space_offset(ls
, type
);
387 /* Return the position of the dimension of the given type and name
389 * Return -1 if no such dimension can be found.
391 int isl_aff_find_dim_by_name(__isl_keep isl_aff
*aff
, enum isl_dim_type type
,
396 if (type
== isl_dim_out
)
398 if (type
== isl_dim_in
)
400 return isl_local_space_find_dim_by_name(aff
->ls
, type
, name
);
403 /* Return the domain space of "aff".
405 static __isl_keep isl_space
*isl_aff_peek_domain_space(__isl_keep isl_aff
*aff
)
407 return aff
? isl_local_space_peek_space(aff
->ls
) : NULL
;
410 __isl_give isl_space
*isl_aff_get_domain_space(__isl_keep isl_aff
*aff
)
412 return isl_space_copy(isl_aff_peek_domain_space(aff
));
415 __isl_give isl_space
*isl_aff_get_space(__isl_keep isl_aff
*aff
)
420 space
= isl_local_space_get_space(aff
->ls
);
421 space
= isl_space_from_domain(space
);
422 space
= isl_space_add_dims(space
, isl_dim_out
, 1);
426 /* Return a copy of the domain space of "aff".
428 __isl_give isl_local_space
*isl_aff_get_domain_local_space(
429 __isl_keep isl_aff
*aff
)
431 return isl_local_space_copy(isl_aff_peek_domain_local_space(aff
));
434 __isl_give isl_local_space
*isl_aff_get_local_space(__isl_keep isl_aff
*aff
)
439 ls
= isl_local_space_copy(aff
->ls
);
440 ls
= isl_local_space_from_domain(ls
);
441 ls
= isl_local_space_add_dims(ls
, isl_dim_out
, 1);
445 /* Return the local space of the domain of "aff".
446 * This may be either a copy or the local space itself
447 * if there is only one reference to "aff".
448 * This allows the local space to be modified inplace
449 * if both the expression and its local space have only a single reference.
450 * The caller is not allowed to modify "aff" between this call and
451 * a subsequent call to isl_aff_restore_domain_local_space.
452 * The only exception is that isl_aff_free can be called instead.
454 __isl_give isl_local_space
*isl_aff_take_domain_local_space(
455 __isl_keep isl_aff
*aff
)
462 return isl_aff_get_domain_local_space(aff
);
468 /* Set the local space of the domain of "aff" to "ls",
469 * where the local space of "aff" may be missing
470 * due to a preceding call to isl_aff_take_domain_local_space.
471 * However, in this case, "aff" only has a single reference and
472 * then the call to isl_aff_cow has no effect.
474 __isl_give isl_aff
*isl_aff_restore_domain_local_space(
475 __isl_keep isl_aff
*aff
, __isl_take isl_local_space
*ls
)
481 isl_local_space_free(ls
);
485 aff
= isl_aff_cow(aff
);
488 isl_local_space_free(aff
->ls
);
494 isl_local_space_free(ls
);
498 /* Externally, an isl_aff has a map space, but internally, the
499 * ls field corresponds to the domain of that space.
501 const char *isl_aff_get_dim_name(__isl_keep isl_aff
*aff
,
502 enum isl_dim_type type
, unsigned pos
)
506 if (type
== isl_dim_out
)
508 if (type
== isl_dim_in
)
510 return isl_local_space_get_dim_name(aff
->ls
, type
, pos
);
513 __isl_give isl_aff
*isl_aff_reset_domain_space(__isl_take isl_aff
*aff
,
514 __isl_take isl_space
*space
)
516 aff
= isl_aff_cow(aff
);
520 aff
->ls
= isl_local_space_reset_space(aff
->ls
, space
);
522 return isl_aff_free(aff
);
527 isl_space_free(space
);
531 /* Reset the space of "aff". This function is called from isl_pw_templ.c
532 * and doesn't know if the space of an element object is represented
533 * directly or through its domain. It therefore passes along both.
535 __isl_give isl_aff
*isl_aff_reset_space_and_domain(__isl_take isl_aff
*aff
,
536 __isl_take isl_space
*space
, __isl_take isl_space
*domain
)
538 isl_space_free(space
);
539 return isl_aff_reset_domain_space(aff
, domain
);
542 /* Reorder the coefficients of the affine expression based
543 * on the given reordering.
544 * The reordering r is assumed to have been extended with the local
547 static __isl_give isl_vec
*vec_reorder(__isl_take isl_vec
*vec
,
548 __isl_take isl_reordering
*r
, int n_div
)
558 space
= isl_reordering_peek_space(r
);
559 dim
= isl_space_dim(space
, isl_dim_all
);
562 res
= isl_vec_alloc(vec
->ctx
, 2 + dim
+ n_div
);
565 isl_seq_cpy(res
->el
, vec
->el
, 2);
566 isl_seq_clr(res
->el
+ 2, res
->size
- 2);
567 for (i
= 0; i
< r
->len
; ++i
)
568 isl_int_set(res
->el
[2 + r
->pos
[i
]], vec
->el
[2 + i
]);
570 isl_reordering_free(r
);
575 isl_reordering_free(r
);
579 /* Reorder the dimensions of the domain of "aff" according
580 * to the given reordering.
582 __isl_give isl_aff
*isl_aff_realign_domain(__isl_take isl_aff
*aff
,
583 __isl_take isl_reordering
*r
)
585 aff
= isl_aff_cow(aff
);
589 r
= isl_reordering_extend(r
, aff
->ls
->div
->n_row
);
590 aff
->v
= vec_reorder(aff
->v
, isl_reordering_copy(r
),
591 aff
->ls
->div
->n_row
);
592 aff
->ls
= isl_local_space_realign(aff
->ls
, r
);
594 if (!aff
->v
|| !aff
->ls
)
595 return isl_aff_free(aff
);
600 isl_reordering_free(r
);
604 __isl_give isl_aff
*isl_aff_align_params(__isl_take isl_aff
*aff
,
605 __isl_take isl_space
*model
)
607 isl_bool equal_params
;
612 equal_params
= isl_space_has_equal_params(aff
->ls
->dim
, model
);
613 if (equal_params
< 0)
618 exp
= isl_parameter_alignment_reordering(aff
->ls
->dim
, model
);
619 exp
= isl_reordering_extend_space(exp
,
620 isl_aff_get_domain_space(aff
));
621 aff
= isl_aff_realign_domain(aff
, exp
);
624 isl_space_free(model
);
627 isl_space_free(model
);
632 /* Given an affine function "aff" defined over a parameter domain,
633 * convert it to a function defined over a domain corresponding
635 * Any parameters with identifiers in "domain" are reinterpreted
636 * as the corresponding domain dimensions.
638 __isl_give isl_aff
*isl_aff_unbind_params_insert_domain(
639 __isl_take isl_aff
*aff
, __isl_take isl_multi_id
*domain
)
645 space
= isl_aff_peek_domain_space(aff
);
646 is_params
= isl_space_is_params(space
);
648 domain
= isl_multi_id_free(domain
);
650 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
651 "expecting function with parameter domain",
652 domain
= isl_multi_id_free(domain
));
653 r
= isl_reordering_unbind_params_insert_domain(space
, domain
);
654 isl_multi_id_free(domain
);
656 return isl_aff_realign_domain(aff
, r
);
659 /* Is "aff" obviously equal to zero?
661 * If the denominator is zero, then "aff" is not equal to zero.
663 isl_bool
isl_aff_plain_is_zero(__isl_keep isl_aff
*aff
)
668 return isl_bool_error
;
670 if (isl_int_is_zero(aff
->v
->el
[0]))
671 return isl_bool_false
;
672 pos
= isl_seq_first_non_zero(aff
->v
->el
+ 1, aff
->v
->size
- 1);
673 return isl_bool_ok(pos
< 0);
676 /* Does "aff" represent NaN?
678 isl_bool
isl_aff_is_nan(__isl_keep isl_aff
*aff
)
681 return isl_bool_error
;
683 return isl_bool_ok(isl_seq_first_non_zero(aff
->v
->el
, 2) < 0);
686 /* Are "aff1" and "aff2" obviously equal?
688 * NaN is not equal to anything, not even to another NaN.
690 isl_bool
isl_aff_plain_is_equal(__isl_keep isl_aff
*aff1
,
691 __isl_keep isl_aff
*aff2
)
696 return isl_bool_error
;
698 if (isl_aff_is_nan(aff1
) || isl_aff_is_nan(aff2
))
699 return isl_bool_false
;
701 equal
= isl_local_space_is_equal(aff1
->ls
, aff2
->ls
);
702 if (equal
< 0 || !equal
)
705 return isl_vec_is_equal(aff1
->v
, aff2
->v
);
708 /* Return the common denominator of "aff" in "v".
710 * We cannot return anything meaningful in case of a NaN.
712 isl_stat
isl_aff_get_denominator(__isl_keep isl_aff
*aff
, isl_int
*v
)
715 return isl_stat_error
;
716 if (isl_aff_is_nan(aff
))
717 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
718 "cannot get denominator of NaN", return isl_stat_error
);
719 isl_int_set(*v
, aff
->v
->el
[0]);
723 /* Return the common denominator of "aff".
725 __isl_give isl_val
*isl_aff_get_denominator_val(__isl_keep isl_aff
*aff
)
732 ctx
= isl_aff_get_ctx(aff
);
733 if (isl_aff_is_nan(aff
))
734 return isl_val_nan(ctx
);
735 return isl_val_int_from_isl_int(ctx
, aff
->v
->el
[0]);
738 /* Return the constant term of "aff".
740 __isl_give isl_val
*isl_aff_get_constant_val(__isl_keep isl_aff
*aff
)
748 ctx
= isl_aff_get_ctx(aff
);
749 if (isl_aff_is_nan(aff
))
750 return isl_val_nan(ctx
);
751 v
= isl_val_rat_from_isl_int(ctx
, aff
->v
->el
[1], aff
->v
->el
[0]);
752 return isl_val_normalize(v
);
755 /* Return the coefficient of the variable of type "type" at position "pos"
758 __isl_give isl_val
*isl_aff_get_coefficient_val(__isl_keep isl_aff
*aff
,
759 enum isl_dim_type type
, int pos
)
767 ctx
= isl_aff_get_ctx(aff
);
768 if (type
== isl_dim_out
)
769 isl_die(ctx
, isl_error_invalid
,
770 "output/set dimension does not have a coefficient",
772 if (type
== isl_dim_in
)
775 if (isl_local_space_check_range(aff
->ls
, type
, pos
, 1) < 0)
778 if (isl_aff_is_nan(aff
))
779 return isl_val_nan(ctx
);
780 pos
+= isl_local_space_offset(aff
->ls
, type
);
781 v
= isl_val_rat_from_isl_int(ctx
, aff
->v
->el
[1 + pos
], aff
->v
->el
[0]);
782 return isl_val_normalize(v
);
785 /* Return the sign of the coefficient of the variable of type "type"
786 * at position "pos" of "aff".
788 int isl_aff_coefficient_sgn(__isl_keep isl_aff
*aff
, enum isl_dim_type type
,
796 ctx
= isl_aff_get_ctx(aff
);
797 if (type
== isl_dim_out
)
798 isl_die(ctx
, isl_error_invalid
,
799 "output/set dimension does not have a coefficient",
801 if (type
== isl_dim_in
)
804 if (isl_local_space_check_range(aff
->ls
, type
, pos
, 1) < 0)
807 pos
+= isl_local_space_offset(aff
->ls
, type
);
808 return isl_int_sgn(aff
->v
->el
[1 + pos
]);
811 /* Replace the numerator of the constant term of "aff" by "v".
813 * A NaN is unaffected by this operation.
815 __isl_give isl_aff
*isl_aff_set_constant(__isl_take isl_aff
*aff
, isl_int v
)
819 if (isl_aff_is_nan(aff
))
821 aff
= isl_aff_cow(aff
);
825 aff
->v
= isl_vec_cow(aff
->v
);
827 return isl_aff_free(aff
);
829 isl_int_set(aff
->v
->el
[1], v
);
834 /* Replace the constant term of "aff" by "v".
836 * A NaN is unaffected by this operation.
838 __isl_give isl_aff
*isl_aff_set_constant_val(__isl_take isl_aff
*aff
,
839 __isl_take isl_val
*v
)
844 if (isl_aff_is_nan(aff
)) {
849 if (!isl_val_is_rat(v
))
850 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
851 "expecting rational value", goto error
);
853 if (isl_int_eq(aff
->v
->el
[1], v
->n
) &&
854 isl_int_eq(aff
->v
->el
[0], v
->d
)) {
859 aff
= isl_aff_cow(aff
);
862 aff
->v
= isl_vec_cow(aff
->v
);
866 if (isl_int_eq(aff
->v
->el
[0], v
->d
)) {
867 isl_int_set(aff
->v
->el
[1], v
->n
);
868 } else if (isl_int_is_one(v
->d
)) {
869 isl_int_mul(aff
->v
->el
[1], aff
->v
->el
[0], v
->n
);
871 isl_seq_scale(aff
->v
->el
+ 1,
872 aff
->v
->el
+ 1, v
->d
, aff
->v
->size
- 1);
873 isl_int_mul(aff
->v
->el
[1], aff
->v
->el
[0], v
->n
);
874 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], v
->d
);
875 aff
->v
= isl_vec_normalize(aff
->v
);
888 /* Add "v" to the constant term of "aff".
890 * A NaN is unaffected by this operation.
892 __isl_give isl_aff
*isl_aff_add_constant(__isl_take isl_aff
*aff
, isl_int v
)
894 if (isl_int_is_zero(v
))
899 if (isl_aff_is_nan(aff
))
901 aff
= isl_aff_cow(aff
);
905 aff
->v
= isl_vec_cow(aff
->v
);
907 return isl_aff_free(aff
);
909 isl_int_addmul(aff
->v
->el
[1], aff
->v
->el
[0], v
);
914 /* Add "v" to the constant term of "aff".
916 * A NaN is unaffected by this operation.
918 __isl_give isl_aff
*isl_aff_add_constant_val(__isl_take isl_aff
*aff
,
919 __isl_take isl_val
*v
)
924 if (isl_aff_is_nan(aff
) || isl_val_is_zero(v
)) {
929 if (!isl_val_is_rat(v
))
930 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
931 "expecting rational value", goto error
);
933 aff
= isl_aff_cow(aff
);
937 aff
->v
= isl_vec_cow(aff
->v
);
941 if (isl_int_is_one(v
->d
)) {
942 isl_int_addmul(aff
->v
->el
[1], aff
->v
->el
[0], v
->n
);
943 } else if (isl_int_eq(aff
->v
->el
[0], v
->d
)) {
944 isl_int_add(aff
->v
->el
[1], aff
->v
->el
[1], v
->n
);
945 aff
->v
= isl_vec_normalize(aff
->v
);
949 isl_seq_scale(aff
->v
->el
+ 1,
950 aff
->v
->el
+ 1, v
->d
, aff
->v
->size
- 1);
951 isl_int_addmul(aff
->v
->el
[1], aff
->v
->el
[0], v
->n
);
952 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], v
->d
);
953 aff
->v
= isl_vec_normalize(aff
->v
);
966 __isl_give isl_aff
*isl_aff_add_constant_si(__isl_take isl_aff
*aff
, int v
)
971 isl_int_set_si(t
, v
);
972 aff
= isl_aff_add_constant(aff
, t
);
978 /* Add "v" to the numerator of the constant term of "aff".
980 * A NaN is unaffected by this operation.
982 __isl_give isl_aff
*isl_aff_add_constant_num(__isl_take isl_aff
*aff
, isl_int v
)
984 if (isl_int_is_zero(v
))
989 if (isl_aff_is_nan(aff
))
991 aff
= isl_aff_cow(aff
);
995 aff
->v
= isl_vec_cow(aff
->v
);
997 return isl_aff_free(aff
);
999 isl_int_add(aff
->v
->el
[1], aff
->v
->el
[1], v
);
1004 /* Add "v" to the numerator of the constant term of "aff".
1006 * A NaN is unaffected by this operation.
1008 __isl_give isl_aff
*isl_aff_add_constant_num_si(__isl_take isl_aff
*aff
, int v
)
1016 isl_int_set_si(t
, v
);
1017 aff
= isl_aff_add_constant_num(aff
, t
);
1023 /* Replace the numerator of the constant term of "aff" by "v".
1025 * A NaN is unaffected by this operation.
1027 __isl_give isl_aff
*isl_aff_set_constant_si(__isl_take isl_aff
*aff
, int v
)
1031 if (isl_aff_is_nan(aff
))
1033 aff
= isl_aff_cow(aff
);
1037 aff
->v
= isl_vec_cow(aff
->v
);
1039 return isl_aff_free(aff
);
1041 isl_int_set_si(aff
->v
->el
[1], v
);
1046 /* Replace the numerator of the coefficient of the variable of type "type"
1047 * at position "pos" of "aff" by "v".
1049 * A NaN is unaffected by this operation.
1051 __isl_give isl_aff
*isl_aff_set_coefficient(__isl_take isl_aff
*aff
,
1052 enum isl_dim_type type
, int pos
, isl_int v
)
1057 if (type
== isl_dim_out
)
1058 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1059 "output/set dimension does not have a coefficient",
1060 return isl_aff_free(aff
));
1061 if (type
== isl_dim_in
)
1064 if (isl_local_space_check_range(aff
->ls
, type
, pos
, 1) < 0)
1065 return isl_aff_free(aff
);
1067 if (isl_aff_is_nan(aff
))
1069 aff
= isl_aff_cow(aff
);
1073 aff
->v
= isl_vec_cow(aff
->v
);
1075 return isl_aff_free(aff
);
1077 pos
+= isl_local_space_offset(aff
->ls
, type
);
1078 isl_int_set(aff
->v
->el
[1 + pos
], v
);
1083 /* Replace the numerator of the coefficient of the variable of type "type"
1084 * at position "pos" of "aff" by "v".
1086 * A NaN is unaffected by this operation.
1088 __isl_give isl_aff
*isl_aff_set_coefficient_si(__isl_take isl_aff
*aff
,
1089 enum isl_dim_type type
, int pos
, int v
)
1094 if (type
== isl_dim_out
)
1095 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1096 "output/set dimension does not have a coefficient",
1097 return isl_aff_free(aff
));
1098 if (type
== isl_dim_in
)
1101 if (isl_local_space_check_range(aff
->ls
, type
, pos
, 1) < 0)
1102 return isl_aff_free(aff
);
1104 if (isl_aff_is_nan(aff
))
1106 pos
+= isl_local_space_offset(aff
->ls
, type
);
1107 if (isl_int_cmp_si(aff
->v
->el
[1 + pos
], v
) == 0)
1110 aff
= isl_aff_cow(aff
);
1114 aff
->v
= isl_vec_cow(aff
->v
);
1116 return isl_aff_free(aff
);
1118 isl_int_set_si(aff
->v
->el
[1 + pos
], v
);
1123 /* Replace the coefficient of the variable of type "type" at position "pos"
1126 * A NaN is unaffected by this operation.
1128 __isl_give isl_aff
*isl_aff_set_coefficient_val(__isl_take isl_aff
*aff
,
1129 enum isl_dim_type type
, int pos
, __isl_take isl_val
*v
)
1134 if (type
== isl_dim_out
)
1135 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1136 "output/set dimension does not have a coefficient",
1138 if (type
== isl_dim_in
)
1141 if (isl_local_space_check_range(aff
->ls
, type
, pos
, 1) < 0)
1142 return isl_aff_free(aff
);
1144 if (isl_aff_is_nan(aff
)) {
1148 if (!isl_val_is_rat(v
))
1149 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1150 "expecting rational value", goto error
);
1152 pos
+= isl_local_space_offset(aff
->ls
, type
);
1153 if (isl_int_eq(aff
->v
->el
[1 + pos
], v
->n
) &&
1154 isl_int_eq(aff
->v
->el
[0], v
->d
)) {
1159 aff
= isl_aff_cow(aff
);
1162 aff
->v
= isl_vec_cow(aff
->v
);
1166 if (isl_int_eq(aff
->v
->el
[0], v
->d
)) {
1167 isl_int_set(aff
->v
->el
[1 + pos
], v
->n
);
1168 } else if (isl_int_is_one(v
->d
)) {
1169 isl_int_mul(aff
->v
->el
[1 + pos
], aff
->v
->el
[0], v
->n
);
1171 isl_seq_scale(aff
->v
->el
+ 1,
1172 aff
->v
->el
+ 1, v
->d
, aff
->v
->size
- 1);
1173 isl_int_mul(aff
->v
->el
[1 + pos
], aff
->v
->el
[0], v
->n
);
1174 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], v
->d
);
1175 aff
->v
= isl_vec_normalize(aff
->v
);
1188 /* Add "v" to the coefficient of the variable of type "type"
1189 * at position "pos" of "aff".
1191 * A NaN is unaffected by this operation.
1193 __isl_give isl_aff
*isl_aff_add_coefficient(__isl_take isl_aff
*aff
,
1194 enum isl_dim_type type
, int pos
, isl_int v
)
1199 if (type
== isl_dim_out
)
1200 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1201 "output/set dimension does not have a coefficient",
1202 return isl_aff_free(aff
));
1203 if (type
== isl_dim_in
)
1206 if (isl_local_space_check_range(aff
->ls
, type
, pos
, 1) < 0)
1207 return isl_aff_free(aff
);
1209 if (isl_aff_is_nan(aff
))
1211 aff
= isl_aff_cow(aff
);
1215 aff
->v
= isl_vec_cow(aff
->v
);
1217 return isl_aff_free(aff
);
1219 pos
+= isl_local_space_offset(aff
->ls
, type
);
1220 isl_int_addmul(aff
->v
->el
[1 + pos
], aff
->v
->el
[0], v
);
1225 /* Add "v" to the coefficient of the variable of type "type"
1226 * at position "pos" of "aff".
1228 * A NaN is unaffected by this operation.
1230 __isl_give isl_aff
*isl_aff_add_coefficient_val(__isl_take isl_aff
*aff
,
1231 enum isl_dim_type type
, int pos
, __isl_take isl_val
*v
)
1236 if (isl_val_is_zero(v
)) {
1241 if (type
== isl_dim_out
)
1242 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1243 "output/set dimension does not have a coefficient",
1245 if (type
== isl_dim_in
)
1248 if (isl_local_space_check_range(aff
->ls
, type
, pos
, 1) < 0)
1251 if (isl_aff_is_nan(aff
)) {
1255 if (!isl_val_is_rat(v
))
1256 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1257 "expecting rational value", goto error
);
1259 aff
= isl_aff_cow(aff
);
1263 aff
->v
= isl_vec_cow(aff
->v
);
1267 pos
+= isl_local_space_offset(aff
->ls
, type
);
1268 if (isl_int_is_one(v
->d
)) {
1269 isl_int_addmul(aff
->v
->el
[1 + pos
], aff
->v
->el
[0], v
->n
);
1270 } else if (isl_int_eq(aff
->v
->el
[0], v
->d
)) {
1271 isl_int_add(aff
->v
->el
[1 + pos
], aff
->v
->el
[1 + pos
], v
->n
);
1272 aff
->v
= isl_vec_normalize(aff
->v
);
1276 isl_seq_scale(aff
->v
->el
+ 1,
1277 aff
->v
->el
+ 1, v
->d
, aff
->v
->size
- 1);
1278 isl_int_addmul(aff
->v
->el
[1 + pos
], aff
->v
->el
[0], v
->n
);
1279 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], v
->d
);
1280 aff
->v
= isl_vec_normalize(aff
->v
);
1293 __isl_give isl_aff
*isl_aff_add_coefficient_si(__isl_take isl_aff
*aff
,
1294 enum isl_dim_type type
, int pos
, int v
)
1299 isl_int_set_si(t
, v
);
1300 aff
= isl_aff_add_coefficient(aff
, type
, pos
, t
);
1306 __isl_give isl_aff
*isl_aff_get_div(__isl_keep isl_aff
*aff
, int pos
)
1311 return isl_local_space_get_div(aff
->ls
, pos
);
1314 /* Return the negation of "aff".
1316 * As a special case, -NaN = NaN.
1318 __isl_give isl_aff
*isl_aff_neg(__isl_take isl_aff
*aff
)
1322 if (isl_aff_is_nan(aff
))
1324 aff
= isl_aff_cow(aff
);
1327 aff
->v
= isl_vec_cow(aff
->v
);
1329 return isl_aff_free(aff
);
1331 isl_seq_neg(aff
->v
->el
+ 1, aff
->v
->el
+ 1, aff
->v
->size
- 1);
1336 /* Remove divs from the local space that do not appear in the affine
1338 * We currently only remove divs at the end.
1339 * Some intermediate divs may also not appear directly in the affine
1340 * expression, but we would also need to check that no other divs are
1341 * defined in terms of them.
1343 __isl_give isl_aff
*isl_aff_remove_unused_divs(__isl_take isl_aff
*aff
)
1349 n
= isl_aff_domain_dim(aff
, isl_dim_div
);
1350 off
= isl_aff_domain_offset(aff
, isl_dim_div
);
1351 if (n
< 0 || off
< 0)
1352 return isl_aff_free(aff
);
1354 pos
= isl_seq_last_non_zero(aff
->v
->el
+ 1 + off
, n
) + 1;
1358 aff
= isl_aff_cow(aff
);
1362 aff
->ls
= isl_local_space_drop_dims(aff
->ls
, isl_dim_div
, pos
, n
- pos
);
1363 aff
->v
= isl_vec_drop_els(aff
->v
, 1 + off
+ pos
, n
- pos
);
1364 if (!aff
->ls
|| !aff
->v
)
1365 return isl_aff_free(aff
);
1370 /* Look for any divs in the aff->ls with a denominator equal to one
1371 * and plug them into the affine expression and any subsequent divs
1372 * that may reference the div.
1374 static __isl_give isl_aff
*plug_in_integral_divs(__isl_take isl_aff
*aff
)
1381 isl_local_space
*ls
;
1384 n
= isl_aff_domain_dim(aff
, isl_dim_div
);
1385 off
= isl_aff_domain_offset(aff
, isl_dim_div
);
1386 if (n
< 0 || off
< 0)
1387 return isl_aff_free(aff
);
1389 for (i
= 0; i
< n
; ++i
) {
1390 if (!isl_int_is_one(aff
->ls
->div
->row
[i
][0]))
1392 ls
= isl_local_space_copy(aff
->ls
);
1393 ls
= isl_local_space_substitute_seq(ls
, isl_dim_div
, i
,
1394 aff
->ls
->div
->row
[i
], len
, i
+ 1, n
- (i
+ 1));
1395 vec
= isl_vec_copy(aff
->v
);
1396 vec
= isl_vec_cow(vec
);
1402 isl_seq_substitute(vec
->el
, off
+ i
, aff
->ls
->div
->row
[i
],
1407 isl_vec_free(aff
->v
);
1409 isl_local_space_free(aff
->ls
);
1416 isl_local_space_free(ls
);
1417 return isl_aff_free(aff
);
1420 /* Look for any divs j that appear with a unit coefficient inside
1421 * the definitions of other divs i and plug them into the definitions
1424 * In particular, an expression of the form
1426 * floor((f(..) + floor(g(..)/n))/m)
1430 * floor((n * f(..) + g(..))/(n * m))
1432 * This simplification is correct because we can move the expression
1433 * f(..) into the inner floor in the original expression to obtain
1435 * floor(floor((n * f(..) + g(..))/n)/m)
1437 * from which we can derive the simplified expression.
1439 static __isl_give isl_aff
*plug_in_unit_divs(__isl_take isl_aff
*aff
)
1445 n
= isl_aff_domain_dim(aff
, isl_dim_div
);
1446 off
= isl_aff_domain_offset(aff
, isl_dim_div
);
1447 if (n
< 0 || off
< 0)
1448 return isl_aff_free(aff
);
1449 for (i
= 1; i
< n
; ++i
) {
1450 for (j
= 0; j
< i
; ++j
) {
1451 if (!isl_int_is_one(aff
->ls
->div
->row
[i
][1 + off
+ j
]))
1453 aff
->ls
= isl_local_space_substitute_seq(aff
->ls
,
1454 isl_dim_div
, j
, aff
->ls
->div
->row
[j
],
1455 aff
->v
->size
, i
, 1);
1457 return isl_aff_free(aff
);
1464 /* Swap divs "a" and "b" in "aff", which is assumed to be non-NULL.
1466 * Even though this function is only called on isl_affs with a single
1467 * reference, we are careful to only change aff->v and aff->ls together.
1469 static __isl_give isl_aff
*swap_div(__isl_take isl_aff
*aff
, int a
, int b
)
1471 isl_size off
= isl_aff_domain_offset(aff
, isl_dim_div
);
1472 isl_local_space
*ls
;
1476 return isl_aff_free(aff
);
1478 ls
= isl_local_space_copy(aff
->ls
);
1479 ls
= isl_local_space_swap_div(ls
, a
, b
);
1480 v
= isl_vec_copy(aff
->v
);
1485 isl_int_swap(v
->el
[1 + off
+ a
], v
->el
[1 + off
+ b
]);
1486 isl_vec_free(aff
->v
);
1488 isl_local_space_free(aff
->ls
);
1494 isl_local_space_free(ls
);
1495 return isl_aff_free(aff
);
1498 /* Merge divs "a" and "b" in "aff", which is assumed to be non-NULL.
1500 * We currently do not actually remove div "b", but simply add its
1501 * coefficient to that of "a" and then zero it out.
1503 static __isl_give isl_aff
*merge_divs(__isl_take isl_aff
*aff
, int a
, int b
)
1505 isl_size off
= isl_aff_domain_offset(aff
, isl_dim_div
);
1508 return isl_aff_free(aff
);
1510 if (isl_int_is_zero(aff
->v
->el
[1 + off
+ b
]))
1513 aff
->v
= isl_vec_cow(aff
->v
);
1515 return isl_aff_free(aff
);
1517 isl_int_add(aff
->v
->el
[1 + off
+ a
],
1518 aff
->v
->el
[1 + off
+ a
], aff
->v
->el
[1 + off
+ b
]);
1519 isl_int_set_si(aff
->v
->el
[1 + off
+ b
], 0);
1524 /* Sort the divs in the local space of "aff" according to
1525 * the comparison function "cmp_row" in isl_local_space.c,
1526 * combining the coefficients of identical divs.
1528 * Reordering divs does not change the semantics of "aff",
1529 * so there is no need to call isl_aff_cow.
1530 * Moreover, this function is currently only called on isl_affs
1531 * with a single reference.
1533 static __isl_give isl_aff
*sort_divs(__isl_take isl_aff
*aff
)
1538 n
= isl_aff_dim(aff
, isl_dim_div
);
1540 return isl_aff_free(aff
);
1541 for (i
= 1; i
< n
; ++i
) {
1542 for (j
= i
- 1; j
>= 0; --j
) {
1543 int cmp
= isl_mat_cmp_div(aff
->ls
->div
, j
, j
+ 1);
1547 aff
= merge_divs(aff
, j
, j
+ 1);
1549 aff
= swap_div(aff
, j
, j
+ 1);
1558 /* Normalize the representation of "aff".
1560 * This function should only be called of "new" isl_affs, i.e.,
1561 * with only a single reference. We therefore do not need to
1562 * worry about affecting other instances.
1564 __isl_give isl_aff
*isl_aff_normalize(__isl_take isl_aff
*aff
)
1568 aff
->v
= isl_vec_normalize(aff
->v
);
1570 return isl_aff_free(aff
);
1571 aff
= plug_in_integral_divs(aff
);
1572 aff
= plug_in_unit_divs(aff
);
1573 aff
= sort_divs(aff
);
1574 aff
= isl_aff_remove_unused_divs(aff
);
1578 /* Given f, return floor(f).
1579 * If f is an integer expression, then just return f.
1580 * If f is a constant, then return the constant floor(f).
1581 * Otherwise, if f = g/m, write g = q m + r,
1582 * create a new div d = [r/m] and return the expression q + d.
1583 * The coefficients in r are taken to lie between -m/2 and m/2.
1585 * reduce_div_coefficients performs the same normalization.
1587 * As a special case, floor(NaN) = NaN.
1589 __isl_give isl_aff
*isl_aff_floor(__isl_take isl_aff
*aff
)
1599 if (isl_aff_is_nan(aff
))
1601 if (isl_int_is_one(aff
->v
->el
[0]))
1604 aff
= isl_aff_cow(aff
);
1608 aff
->v
= isl_vec_cow(aff
->v
);
1610 return isl_aff_free(aff
);
1612 if (isl_aff_is_cst(aff
)) {
1613 isl_int_fdiv_q(aff
->v
->el
[1], aff
->v
->el
[1], aff
->v
->el
[0]);
1614 isl_int_set_si(aff
->v
->el
[0], 1);
1618 div
= isl_vec_copy(aff
->v
);
1619 div
= isl_vec_cow(div
);
1621 return isl_aff_free(aff
);
1623 ctx
= isl_aff_get_ctx(aff
);
1624 isl_int_fdiv_q(aff
->v
->el
[0], aff
->v
->el
[0], ctx
->two
);
1625 for (i
= 1; i
< aff
->v
->size
; ++i
) {
1626 isl_int_fdiv_r(div
->el
[i
], div
->el
[i
], div
->el
[0]);
1627 isl_int_fdiv_q(aff
->v
->el
[i
], aff
->v
->el
[i
], div
->el
[0]);
1628 if (isl_int_gt(div
->el
[i
], aff
->v
->el
[0])) {
1629 isl_int_sub(div
->el
[i
], div
->el
[i
], div
->el
[0]);
1630 isl_int_add_ui(aff
->v
->el
[i
], aff
->v
->el
[i
], 1);
1634 aff
->ls
= isl_local_space_add_div(aff
->ls
, div
);
1636 return isl_aff_free(aff
);
1638 size
= aff
->v
->size
;
1639 aff
->v
= isl_vec_extend(aff
->v
, size
+ 1);
1641 return isl_aff_free(aff
);
1642 isl_int_set_si(aff
->v
->el
[0], 1);
1643 isl_int_set_si(aff
->v
->el
[size
], 1);
1645 aff
= isl_aff_normalize(aff
);
1652 * aff mod m = aff - m * floor(aff/m)
1654 * with m an integer value.
1656 __isl_give isl_aff
*isl_aff_mod_val(__isl_take isl_aff
*aff
,
1657 __isl_take isl_val
*m
)
1664 if (!isl_val_is_int(m
))
1665 isl_die(isl_val_get_ctx(m
), isl_error_invalid
,
1666 "expecting integer modulo", goto error
);
1668 res
= isl_aff_copy(aff
);
1669 aff
= isl_aff_scale_down_val(aff
, isl_val_copy(m
));
1670 aff
= isl_aff_floor(aff
);
1671 aff
= isl_aff_scale_val(aff
, m
);
1672 res
= isl_aff_sub(res
, aff
);
1683 * pwaff mod m = pwaff - m * floor(pwaff/m)
1685 __isl_give isl_pw_aff
*isl_pw_aff_mod(__isl_take isl_pw_aff
*pwaff
, isl_int m
)
1689 res
= isl_pw_aff_copy(pwaff
);
1690 pwaff
= isl_pw_aff_scale_down(pwaff
, m
);
1691 pwaff
= isl_pw_aff_floor(pwaff
);
1692 pwaff
= isl_pw_aff_scale(pwaff
, m
);
1693 res
= isl_pw_aff_sub(res
, pwaff
);
1700 * pa mod m = pa - m * floor(pa/m)
1702 * with m an integer value.
1704 __isl_give isl_pw_aff
*isl_pw_aff_mod_val(__isl_take isl_pw_aff
*pa
,
1705 __isl_take isl_val
*m
)
1709 if (!isl_val_is_int(m
))
1710 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
1711 "expecting integer modulo", goto error
);
1712 pa
= isl_pw_aff_mod(pa
, m
->n
);
1716 isl_pw_aff_free(pa
);
1721 /* Given f, return ceil(f).
1722 * If f is an integer expression, then just return f.
1723 * Otherwise, let f be the expression
1729 * floor((e + m - 1)/m)
1731 * As a special case, ceil(NaN) = NaN.
1733 __isl_give isl_aff
*isl_aff_ceil(__isl_take isl_aff
*aff
)
1738 if (isl_aff_is_nan(aff
))
1740 if (isl_int_is_one(aff
->v
->el
[0]))
1743 aff
= isl_aff_cow(aff
);
1746 aff
->v
= isl_vec_cow(aff
->v
);
1748 return isl_aff_free(aff
);
1750 isl_int_add(aff
->v
->el
[1], aff
->v
->el
[1], aff
->v
->el
[0]);
1751 isl_int_sub_ui(aff
->v
->el
[1], aff
->v
->el
[1], 1);
1752 aff
= isl_aff_floor(aff
);
1757 /* Apply the expansion computed by isl_merge_divs.
1758 * The expansion itself is given by "exp" while the resulting
1759 * list of divs is given by "div".
1761 __isl_give isl_aff
*isl_aff_expand_divs(__isl_take isl_aff
*aff
,
1762 __isl_take isl_mat
*div
, int *exp
)
1768 aff
= isl_aff_cow(aff
);
1770 offset
= isl_aff_domain_offset(aff
, isl_dim_div
);
1771 old_n_div
= isl_aff_domain_dim(aff
, isl_dim_div
);
1772 new_n_div
= isl_mat_rows(div
);
1773 if (offset
< 0 || old_n_div
< 0 || new_n_div
< 0)
1776 aff
->v
= isl_vec_expand(aff
->v
, 1 + offset
, old_n_div
, exp
, new_n_div
);
1777 aff
->ls
= isl_local_space_replace_divs(aff
->ls
, div
);
1778 if (!aff
->v
|| !aff
->ls
)
1779 return isl_aff_free(aff
);
1787 /* Add two affine expressions that live in the same local space.
1789 static __isl_give isl_aff
*add_expanded(__isl_take isl_aff
*aff1
,
1790 __isl_take isl_aff
*aff2
)
1794 aff1
= isl_aff_cow(aff1
);
1798 aff1
->v
= isl_vec_cow(aff1
->v
);
1804 isl_int_gcd(gcd
, aff1
->v
->el
[0], aff2
->v
->el
[0]);
1805 isl_int_divexact(f
, aff2
->v
->el
[0], gcd
);
1806 isl_seq_scale(aff1
->v
->el
+ 1, aff1
->v
->el
+ 1, f
, aff1
->v
->size
- 1);
1807 isl_int_divexact(f
, aff1
->v
->el
[0], gcd
);
1808 isl_seq_addmul(aff1
->v
->el
+ 1, f
, aff2
->v
->el
+ 1, aff1
->v
->size
- 1);
1809 isl_int_divexact(f
, aff2
->v
->el
[0], gcd
);
1810 isl_int_mul(aff1
->v
->el
[0], aff1
->v
->el
[0], f
);
1815 aff1
= isl_aff_normalize(aff1
);
1823 /* Return the sum of "aff1" and "aff2".
1825 * If either of the two is NaN, then the result is NaN.
1827 __isl_give isl_aff
*isl_aff_add(__isl_take isl_aff
*aff1
,
1828 __isl_take isl_aff
*aff2
)
1834 isl_size n_div1
, n_div2
;
1839 ctx
= isl_aff_get_ctx(aff1
);
1840 if (!isl_space_is_equal(aff1
->ls
->dim
, aff2
->ls
->dim
))
1841 isl_die(ctx
, isl_error_invalid
,
1842 "spaces don't match", goto error
);
1844 if (isl_aff_is_nan(aff1
)) {
1848 if (isl_aff_is_nan(aff2
)) {
1853 n_div1
= isl_aff_dim(aff1
, isl_dim_div
);
1854 n_div2
= isl_aff_dim(aff2
, isl_dim_div
);
1855 if (n_div1
< 0 || n_div2
< 0)
1857 if (n_div1
== 0 && n_div2
== 0)
1858 return add_expanded(aff1
, aff2
);
1860 exp1
= isl_alloc_array(ctx
, int, n_div1
);
1861 exp2
= isl_alloc_array(ctx
, int, n_div2
);
1862 if ((n_div1
&& !exp1
) || (n_div2
&& !exp2
))
1865 div
= isl_merge_divs(aff1
->ls
->div
, aff2
->ls
->div
, exp1
, exp2
);
1866 aff1
= isl_aff_expand_divs(aff1
, isl_mat_copy(div
), exp1
);
1867 aff2
= isl_aff_expand_divs(aff2
, div
, exp2
);
1871 return add_expanded(aff1
, aff2
);
1880 __isl_give isl_aff
*isl_aff_sub(__isl_take isl_aff
*aff1
,
1881 __isl_take isl_aff
*aff2
)
1883 return isl_aff_add(aff1
, isl_aff_neg(aff2
));
1886 /* Return the result of scaling "aff" by a factor of "f".
1888 * As a special case, f * NaN = NaN.
1890 __isl_give isl_aff
*isl_aff_scale(__isl_take isl_aff
*aff
, isl_int f
)
1896 if (isl_aff_is_nan(aff
))
1899 if (isl_int_is_one(f
))
1902 aff
= isl_aff_cow(aff
);
1905 aff
->v
= isl_vec_cow(aff
->v
);
1907 return isl_aff_free(aff
);
1909 if (isl_int_is_pos(f
) && isl_int_is_divisible_by(aff
->v
->el
[0], f
)) {
1910 isl_int_divexact(aff
->v
->el
[0], aff
->v
->el
[0], f
);
1915 isl_int_gcd(gcd
, aff
->v
->el
[0], f
);
1916 isl_int_divexact(aff
->v
->el
[0], aff
->v
->el
[0], gcd
);
1917 isl_int_divexact(gcd
, f
, gcd
);
1918 isl_seq_scale(aff
->v
->el
+ 1, aff
->v
->el
+ 1, gcd
, aff
->v
->size
- 1);
1924 /* Multiple "aff" by "v".
1926 __isl_give isl_aff
*isl_aff_scale_val(__isl_take isl_aff
*aff
,
1927 __isl_take isl_val
*v
)
1932 if (isl_val_is_one(v
)) {
1937 if (!isl_val_is_rat(v
))
1938 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1939 "expecting rational factor", goto error
);
1941 aff
= isl_aff_scale(aff
, v
->n
);
1942 aff
= isl_aff_scale_down(aff
, v
->d
);
1952 /* Return the result of scaling "aff" down by a factor of "f".
1954 * As a special case, NaN/f = NaN.
1956 __isl_give isl_aff
*isl_aff_scale_down(__isl_take isl_aff
*aff
, isl_int f
)
1962 if (isl_aff_is_nan(aff
))
1965 if (isl_int_is_one(f
))
1968 aff
= isl_aff_cow(aff
);
1972 if (isl_int_is_zero(f
))
1973 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1974 "cannot scale down by zero", return isl_aff_free(aff
));
1976 aff
->v
= isl_vec_cow(aff
->v
);
1978 return isl_aff_free(aff
);
1981 isl_seq_gcd(aff
->v
->el
+ 1, aff
->v
->size
- 1, &gcd
);
1982 isl_int_gcd(gcd
, gcd
, f
);
1983 isl_seq_scale_down(aff
->v
->el
+ 1, aff
->v
->el
+ 1, gcd
, aff
->v
->size
- 1);
1984 isl_int_divexact(gcd
, f
, gcd
);
1985 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], gcd
);
1991 /* Divide "aff" by "v".
1993 __isl_give isl_aff
*isl_aff_scale_down_val(__isl_take isl_aff
*aff
,
1994 __isl_take isl_val
*v
)
1999 if (isl_val_is_one(v
)) {
2004 if (!isl_val_is_rat(v
))
2005 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2006 "expecting rational factor", goto error
);
2007 if (!isl_val_is_pos(v
))
2008 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2009 "factor needs to be positive", goto error
);
2011 aff
= isl_aff_scale(aff
, v
->d
);
2012 aff
= isl_aff_scale_down(aff
, v
->n
);
2022 __isl_give isl_aff
*isl_aff_scale_down_ui(__isl_take isl_aff
*aff
, unsigned f
)
2030 isl_int_set_ui(v
, f
);
2031 aff
= isl_aff_scale_down(aff
, v
);
2037 __isl_give isl_aff
*isl_aff_set_dim_name(__isl_take isl_aff
*aff
,
2038 enum isl_dim_type type
, unsigned pos
, const char *s
)
2040 aff
= isl_aff_cow(aff
);
2043 if (type
== isl_dim_out
)
2044 isl_die(aff
->v
->ctx
, isl_error_invalid
,
2045 "cannot set name of output/set dimension",
2046 return isl_aff_free(aff
));
2047 if (type
== isl_dim_in
)
2049 aff
->ls
= isl_local_space_set_dim_name(aff
->ls
, type
, pos
, s
);
2051 return isl_aff_free(aff
);
2056 __isl_give isl_aff
*isl_aff_set_dim_id(__isl_take isl_aff
*aff
,
2057 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
2059 aff
= isl_aff_cow(aff
);
2062 if (type
== isl_dim_out
)
2063 isl_die(aff
->v
->ctx
, isl_error_invalid
,
2064 "cannot set name of output/set dimension",
2066 if (type
== isl_dim_in
)
2068 aff
->ls
= isl_local_space_set_dim_id(aff
->ls
, type
, pos
, id
);
2070 return isl_aff_free(aff
);
2079 /* Replace the identifier of the input tuple of "aff" by "id".
2080 * type is currently required to be equal to isl_dim_in
2082 __isl_give isl_aff
*isl_aff_set_tuple_id(__isl_take isl_aff
*aff
,
2083 enum isl_dim_type type
, __isl_take isl_id
*id
)
2085 aff
= isl_aff_cow(aff
);
2088 if (type
!= isl_dim_in
)
2089 isl_die(aff
->v
->ctx
, isl_error_invalid
,
2090 "cannot only set id of input tuple", goto error
);
2091 aff
->ls
= isl_local_space_set_tuple_id(aff
->ls
, isl_dim_set
, id
);
2093 return isl_aff_free(aff
);
2102 /* Exploit the equalities in "eq" to simplify the affine expression
2103 * and the expressions of the integer divisions in the local space.
2104 * The integer divisions in this local space are assumed to appear
2105 * as regular dimensions in "eq".
2107 static __isl_give isl_aff
*isl_aff_substitute_equalities_lifted(
2108 __isl_take isl_aff
*aff
, __isl_take isl_basic_set
*eq
)
2116 if (eq
->n_eq
== 0) {
2117 isl_basic_set_free(eq
);
2121 aff
= isl_aff_cow(aff
);
2125 aff
->ls
= isl_local_space_substitute_equalities(aff
->ls
,
2126 isl_basic_set_copy(eq
));
2127 aff
->v
= isl_vec_cow(aff
->v
);
2128 if (!aff
->ls
|| !aff
->v
)
2131 o_div
= isl_basic_set_offset(eq
, isl_dim_div
);
2133 for (i
= 0; i
< eq
->n_eq
; ++i
) {
2134 j
= isl_seq_last_non_zero(eq
->eq
[i
], o_div
+ n_div
);
2135 if (j
< 0 || j
== 0 || j
>= o_div
)
2138 isl_seq_elim(aff
->v
->el
+ 1, eq
->eq
[i
], j
, o_div
,
2142 isl_basic_set_free(eq
);
2143 aff
= isl_aff_normalize(aff
);
2146 isl_basic_set_free(eq
);
2151 /* Exploit the equalities in "eq" to simplify the affine expression
2152 * and the expressions of the integer divisions in the local space.
2154 __isl_give isl_aff
*isl_aff_substitute_equalities(__isl_take isl_aff
*aff
,
2155 __isl_take isl_basic_set
*eq
)
2159 n_div
= isl_aff_domain_dim(aff
, isl_dim_div
);
2163 eq
= isl_basic_set_add_dims(eq
, isl_dim_set
, n_div
);
2164 return isl_aff_substitute_equalities_lifted(aff
, eq
);
2166 isl_basic_set_free(eq
);
2171 /* Look for equalities among the variables shared by context and aff
2172 * and the integer divisions of aff, if any.
2173 * The equalities are then used to eliminate coefficients and/or integer
2174 * divisions from aff.
2176 __isl_give isl_aff
*isl_aff_gist(__isl_take isl_aff
*aff
,
2177 __isl_take isl_set
*context
)
2179 isl_local_space
*ls
;
2180 isl_basic_set
*hull
;
2182 ls
= isl_aff_get_domain_local_space(aff
);
2183 context
= isl_local_space_lift_set(ls
, context
);
2185 hull
= isl_set_affine_hull(context
);
2186 return isl_aff_substitute_equalities_lifted(aff
, hull
);
2189 __isl_give isl_aff
*isl_aff_gist_params(__isl_take isl_aff
*aff
,
2190 __isl_take isl_set
*context
)
2192 isl_set
*dom_context
= isl_set_universe(isl_aff_get_domain_space(aff
));
2193 dom_context
= isl_set_intersect_params(dom_context
, context
);
2194 return isl_aff_gist(aff
, dom_context
);
2197 /* Return a basic set containing those elements in the space
2198 * of aff where it is positive. "rational" should not be set.
2200 * If "aff" is NaN, then it is not positive.
2202 static __isl_give isl_basic_set
*aff_pos_basic_set(__isl_take isl_aff
*aff
,
2203 int rational
, void *user
)
2205 isl_constraint
*ineq
;
2206 isl_basic_set
*bset
;
2211 if (isl_aff_is_nan(aff
)) {
2212 isl_space
*space
= isl_aff_get_domain_space(aff
);
2214 return isl_basic_set_empty(space
);
2217 isl_die(isl_aff_get_ctx(aff
), isl_error_unsupported
,
2218 "rational sets not supported", goto error
);
2220 ineq
= isl_inequality_from_aff(aff
);
2221 c
= isl_constraint_get_constant_val(ineq
);
2222 c
= isl_val_sub_ui(c
, 1);
2223 ineq
= isl_constraint_set_constant_val(ineq
, c
);
2225 bset
= isl_basic_set_from_constraint(ineq
);
2226 bset
= isl_basic_set_simplify(bset
);
2233 /* Return a basic set containing those elements in the space
2234 * of aff where it is non-negative.
2235 * If "rational" is set, then return a rational basic set.
2237 * If "aff" is NaN, then it is not non-negative (it's not negative either).
2239 static __isl_give isl_basic_set
*aff_nonneg_basic_set(
2240 __isl_take isl_aff
*aff
, int rational
, void *user
)
2242 isl_constraint
*ineq
;
2243 isl_basic_set
*bset
;
2247 if (isl_aff_is_nan(aff
)) {
2248 isl_space
*space
= isl_aff_get_domain_space(aff
);
2250 return isl_basic_set_empty(space
);
2253 ineq
= isl_inequality_from_aff(aff
);
2255 bset
= isl_basic_set_from_constraint(ineq
);
2257 bset
= isl_basic_set_set_rational(bset
);
2258 bset
= isl_basic_set_simplify(bset
);
2262 /* Return a basic set containing those elements in the space
2263 * of aff where it is non-negative.
2265 __isl_give isl_basic_set
*isl_aff_nonneg_basic_set(__isl_take isl_aff
*aff
)
2267 return aff_nonneg_basic_set(aff
, 0, NULL
);
2270 /* Return a basic set containing those elements in the domain space
2271 * of "aff" where it is positive.
2273 __isl_give isl_basic_set
*isl_aff_pos_basic_set(__isl_take isl_aff
*aff
)
2275 aff
= isl_aff_add_constant_num_si(aff
, -1);
2276 return isl_aff_nonneg_basic_set(aff
);
2279 /* Return a basic set containing those elements in the domain space
2280 * of aff where it is negative.
2282 __isl_give isl_basic_set
*isl_aff_neg_basic_set(__isl_take isl_aff
*aff
)
2284 aff
= isl_aff_neg(aff
);
2285 return isl_aff_pos_basic_set(aff
);
2288 /* Return a basic set containing those elements in the space
2289 * of aff where it is zero.
2290 * If "rational" is set, then return a rational basic set.
2292 * If "aff" is NaN, then it is not zero.
2294 static __isl_give isl_basic_set
*aff_zero_basic_set(__isl_take isl_aff
*aff
,
2295 int rational
, void *user
)
2297 isl_constraint
*ineq
;
2298 isl_basic_set
*bset
;
2302 if (isl_aff_is_nan(aff
)) {
2303 isl_space
*space
= isl_aff_get_domain_space(aff
);
2305 return isl_basic_set_empty(space
);
2308 ineq
= isl_equality_from_aff(aff
);
2310 bset
= isl_basic_set_from_constraint(ineq
);
2312 bset
= isl_basic_set_set_rational(bset
);
2313 bset
= isl_basic_set_simplify(bset
);
2317 /* Return a basic set containing those elements in the space
2318 * of aff where it is zero.
2320 __isl_give isl_basic_set
*isl_aff_zero_basic_set(__isl_take isl_aff
*aff
)
2322 return aff_zero_basic_set(aff
, 0, NULL
);
2325 /* Return a basic set containing those elements in the shared space
2326 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
2328 __isl_give isl_basic_set
*isl_aff_ge_basic_set(__isl_take isl_aff
*aff1
,
2329 __isl_take isl_aff
*aff2
)
2331 aff1
= isl_aff_sub(aff1
, aff2
);
2333 return isl_aff_nonneg_basic_set(aff1
);
2336 /* Return a basic set containing those elements in the shared domain space
2337 * of "aff1" and "aff2" where "aff1" is greater than "aff2".
2339 __isl_give isl_basic_set
*isl_aff_gt_basic_set(__isl_take isl_aff
*aff1
,
2340 __isl_take isl_aff
*aff2
)
2342 aff1
= isl_aff_sub(aff1
, aff2
);
2344 return isl_aff_pos_basic_set(aff1
);
2347 /* Return a set containing those elements in the shared space
2348 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
2350 __isl_give isl_set
*isl_aff_ge_set(__isl_take isl_aff
*aff1
,
2351 __isl_take isl_aff
*aff2
)
2353 return isl_set_from_basic_set(isl_aff_ge_basic_set(aff1
, aff2
));
2356 /* Return a set containing those elements in the shared domain space
2357 * of aff1 and aff2 where aff1 is greater than aff2.
2359 * If either of the two inputs is NaN, then the result is empty,
2360 * as comparisons with NaN always return false.
2362 __isl_give isl_set
*isl_aff_gt_set(__isl_take isl_aff
*aff1
,
2363 __isl_take isl_aff
*aff2
)
2365 return isl_set_from_basic_set(isl_aff_gt_basic_set(aff1
, aff2
));
2368 /* Return a basic set containing those elements in the shared space
2369 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
2371 __isl_give isl_basic_set
*isl_aff_le_basic_set(__isl_take isl_aff
*aff1
,
2372 __isl_take isl_aff
*aff2
)
2374 return isl_aff_ge_basic_set(aff2
, aff1
);
2377 /* Return a basic set containing those elements in the shared domain space
2378 * of "aff1" and "aff2" where "aff1" is smaller than "aff2".
2380 __isl_give isl_basic_set
*isl_aff_lt_basic_set(__isl_take isl_aff
*aff1
,
2381 __isl_take isl_aff
*aff2
)
2383 return isl_aff_gt_basic_set(aff2
, aff1
);
2386 /* Return a set containing those elements in the shared space
2387 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
2389 __isl_give isl_set
*isl_aff_le_set(__isl_take isl_aff
*aff1
,
2390 __isl_take isl_aff
*aff2
)
2392 return isl_aff_ge_set(aff2
, aff1
);
2395 /* Return a set containing those elements in the shared domain space
2396 * of "aff1" and "aff2" where "aff1" is smaller than "aff2".
2398 __isl_give isl_set
*isl_aff_lt_set(__isl_take isl_aff
*aff1
,
2399 __isl_take isl_aff
*aff2
)
2401 return isl_set_from_basic_set(isl_aff_lt_basic_set(aff1
, aff2
));
2404 /* Return a basic set containing those elements in the shared space
2405 * of aff1 and aff2 where aff1 and aff2 are equal.
2407 __isl_give isl_basic_set
*isl_aff_eq_basic_set(__isl_take isl_aff
*aff1
,
2408 __isl_take isl_aff
*aff2
)
2410 aff1
= isl_aff_sub(aff1
, aff2
);
2412 return isl_aff_zero_basic_set(aff1
);
2415 /* Return a set containing those elements in the shared space
2416 * of aff1 and aff2 where aff1 and aff2 are equal.
2418 __isl_give isl_set
*isl_aff_eq_set(__isl_take isl_aff
*aff1
,
2419 __isl_take isl_aff
*aff2
)
2421 return isl_set_from_basic_set(isl_aff_eq_basic_set(aff1
, aff2
));
2424 /* Return a set containing those elements in the shared domain space
2425 * of aff1 and aff2 where aff1 and aff2 are not equal.
2427 * If either of the two inputs is NaN, then the result is empty,
2428 * as comparisons with NaN always return false.
2430 __isl_give isl_set
*isl_aff_ne_set(__isl_take isl_aff
*aff1
,
2431 __isl_take isl_aff
*aff2
)
2433 isl_set
*set_lt
, *set_gt
;
2435 set_lt
= isl_aff_lt_set(isl_aff_copy(aff1
),
2436 isl_aff_copy(aff2
));
2437 set_gt
= isl_aff_gt_set(aff1
, aff2
);
2438 return isl_set_union_disjoint(set_lt
, set_gt
);
2441 __isl_give isl_aff
*isl_aff_add_on_domain(__isl_keep isl_set
*dom
,
2442 __isl_take isl_aff
*aff1
, __isl_take isl_aff
*aff2
)
2444 aff1
= isl_aff_add(aff1
, aff2
);
2445 aff1
= isl_aff_gist(aff1
, isl_set_copy(dom
));
2449 isl_bool
isl_aff_is_empty(__isl_keep isl_aff
*aff
)
2452 return isl_bool_error
;
2454 return isl_bool_false
;
2458 #define TYPE isl_aff
2460 #include "check_type_range_templ.c"
2462 /* Check whether the given affine expression has non-zero coefficient
2463 * for any dimension in the given range or if any of these dimensions
2464 * appear with non-zero coefficients in any of the integer divisions
2465 * involved in the affine expression.
2467 isl_bool
isl_aff_involves_dims(__isl_keep isl_aff
*aff
,
2468 enum isl_dim_type type
, unsigned first
, unsigned n
)
2472 isl_bool involves
= isl_bool_false
;
2475 return isl_bool_error
;
2477 return isl_bool_false
;
2478 if (isl_aff_check_range(aff
, type
, first
, n
) < 0)
2479 return isl_bool_error
;
2481 active
= isl_local_space_get_active(aff
->ls
, aff
->v
->el
+ 2);
2485 first
+= isl_local_space_offset(aff
->ls
, type
) - 1;
2486 for (i
= 0; i
< n
; ++i
)
2487 if (active
[first
+ i
]) {
2488 involves
= isl_bool_true
;
2497 return isl_bool_error
;
2500 /* Does "aff" involve any local variables, i.e., integer divisions?
2502 isl_bool
isl_aff_involves_locals(__isl_keep isl_aff
*aff
)
2506 n
= isl_aff_dim(aff
, isl_dim_div
);
2508 return isl_bool_error
;
2509 return isl_aff_involves_dims(aff
, isl_dim_div
, 0, n
);
2512 __isl_give isl_aff
*isl_aff_drop_dims(__isl_take isl_aff
*aff
,
2513 enum isl_dim_type type
, unsigned first
, unsigned n
)
2519 if (type
== isl_dim_out
)
2520 isl_die(aff
->v
->ctx
, isl_error_invalid
,
2521 "cannot drop output/set dimension",
2522 return isl_aff_free(aff
));
2523 if (type
== isl_dim_in
)
2525 if (n
== 0 && !isl_local_space_is_named_or_nested(aff
->ls
, type
))
2528 ctx
= isl_aff_get_ctx(aff
);
2529 if (isl_local_space_check_range(aff
->ls
, type
, first
, n
) < 0)
2530 return isl_aff_free(aff
);
2532 aff
= isl_aff_cow(aff
);
2536 aff
->ls
= isl_local_space_drop_dims(aff
->ls
, type
, first
, n
);
2538 return isl_aff_free(aff
);
2540 first
+= 1 + isl_local_space_offset(aff
->ls
, type
);
2541 aff
->v
= isl_vec_drop_els(aff
->v
, first
, n
);
2543 return isl_aff_free(aff
);
2548 /* Is the domain of "aff" a product?
2550 static isl_bool
isl_aff_domain_is_product(__isl_keep isl_aff
*aff
)
2552 return isl_space_is_product(isl_aff_peek_domain_space(aff
));
2556 #define TYPE isl_aff
2557 #include <isl_domain_factor_templ.c>
2559 /* Project the domain of the affine expression onto its parameter space.
2560 * The affine expression may not involve any of the domain dimensions.
2562 __isl_give isl_aff
*isl_aff_project_domain_on_params(__isl_take isl_aff
*aff
)
2567 n
= isl_aff_dim(aff
, isl_dim_in
);
2569 return isl_aff_free(aff
);
2570 aff
= isl_aff_drop_domain(aff
, 0, n
);
2571 space
= isl_aff_get_domain_space(aff
);
2572 space
= isl_space_params(space
);
2573 aff
= isl_aff_reset_domain_space(aff
, space
);
2577 /* Convert an affine expression defined over a parameter domain
2578 * into one that is defined over a zero-dimensional set.
2580 __isl_give isl_aff
*isl_aff_from_range(__isl_take isl_aff
*aff
)
2582 isl_local_space
*ls
;
2584 ls
= isl_aff_take_domain_local_space(aff
);
2585 ls
= isl_local_space_set_from_params(ls
);
2586 aff
= isl_aff_restore_domain_local_space(aff
, ls
);
2591 __isl_give isl_aff
*isl_aff_insert_dims(__isl_take isl_aff
*aff
,
2592 enum isl_dim_type type
, unsigned first
, unsigned n
)
2598 if (type
== isl_dim_out
)
2599 isl_die(aff
->v
->ctx
, isl_error_invalid
,
2600 "cannot insert output/set dimensions",
2601 return isl_aff_free(aff
));
2602 if (type
== isl_dim_in
)
2604 if (n
== 0 && !isl_local_space_is_named_or_nested(aff
->ls
, type
))
2607 ctx
= isl_aff_get_ctx(aff
);
2608 if (isl_local_space_check_range(aff
->ls
, type
, first
, 0) < 0)
2609 return isl_aff_free(aff
);
2611 aff
= isl_aff_cow(aff
);
2615 aff
->ls
= isl_local_space_insert_dims(aff
->ls
, type
, first
, n
);
2617 return isl_aff_free(aff
);
2619 first
+= 1 + isl_local_space_offset(aff
->ls
, type
);
2620 aff
->v
= isl_vec_insert_zero_els(aff
->v
, first
, n
);
2622 return isl_aff_free(aff
);
2627 __isl_give isl_aff
*isl_aff_add_dims(__isl_take isl_aff
*aff
,
2628 enum isl_dim_type type
, unsigned n
)
2632 pos
= isl_aff_dim(aff
, type
);
2634 return isl_aff_free(aff
);
2636 return isl_aff_insert_dims(aff
, type
, pos
, n
);
2639 __isl_give isl_pw_aff
*isl_pw_aff_add_dims(__isl_take isl_pw_aff
*pwaff
,
2640 enum isl_dim_type type
, unsigned n
)
2644 pos
= isl_pw_aff_dim(pwaff
, type
);
2646 return isl_pw_aff_free(pwaff
);
2648 return isl_pw_aff_insert_dims(pwaff
, type
, pos
, n
);
2651 /* Move the "n" dimensions of "src_type" starting at "src_pos" of "aff"
2652 * to dimensions of "dst_type" at "dst_pos".
2654 * We only support moving input dimensions to parameters and vice versa.
2656 __isl_give isl_aff
*isl_aff_move_dims(__isl_take isl_aff
*aff
,
2657 enum isl_dim_type dst_type
, unsigned dst_pos
,
2658 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
2662 isl_size src_off
, dst_off
;
2667 !isl_local_space_is_named_or_nested(aff
->ls
, src_type
) &&
2668 !isl_local_space_is_named_or_nested(aff
->ls
, dst_type
))
2671 if (dst_type
== isl_dim_out
|| src_type
== isl_dim_out
)
2672 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2673 "cannot move output/set dimension",
2674 return isl_aff_free(aff
));
2675 if (dst_type
== isl_dim_div
|| src_type
== isl_dim_div
)
2676 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2677 "cannot move divs", return isl_aff_free(aff
));
2678 if (dst_type
== isl_dim_in
)
2679 dst_type
= isl_dim_set
;
2680 if (src_type
== isl_dim_in
)
2681 src_type
= isl_dim_set
;
2683 if (isl_local_space_check_range(aff
->ls
, src_type
, src_pos
, n
) < 0)
2684 return isl_aff_free(aff
);
2685 if (dst_type
== src_type
)
2686 isl_die(isl_aff_get_ctx(aff
), isl_error_unsupported
,
2687 "moving dims within the same type not supported",
2688 return isl_aff_free(aff
));
2690 aff
= isl_aff_cow(aff
);
2691 src_off
= isl_aff_domain_offset(aff
, src_type
);
2692 dst_off
= isl_aff_domain_offset(aff
, dst_type
);
2693 if (src_off
< 0 || dst_off
< 0)
2694 return isl_aff_free(aff
);
2696 g_src_pos
= 1 + src_off
+ src_pos
;
2697 g_dst_pos
= 1 + dst_off
+ dst_pos
;
2698 if (dst_type
> src_type
)
2701 aff
->v
= isl_vec_move_els(aff
->v
, g_dst_pos
, g_src_pos
, n
);
2702 aff
->ls
= isl_local_space_move_dims(aff
->ls
, dst_type
, dst_pos
,
2703 src_type
, src_pos
, n
);
2704 if (!aff
->v
|| !aff
->ls
)
2705 return isl_aff_free(aff
);
2707 aff
= sort_divs(aff
);
2712 /* Return a zero isl_aff in the given space.
2714 * This is a helper function for isl_pw_*_as_* that ensures a uniform
2715 * interface over all piecewise types.
2717 static __isl_give isl_aff
*isl_aff_zero_in_space(__isl_take isl_space
*space
)
2719 isl_local_space
*ls
;
2721 ls
= isl_local_space_from_space(isl_space_domain(space
));
2722 return isl_aff_zero_on_domain(ls
);
2725 #define isl_aff_involves_nan isl_aff_is_nan
2728 #define PW isl_pw_aff
2732 #define EL_IS_ZERO is_empty
2736 #define IS_ZERO is_empty
2739 #undef DEFAULT_IS_ZERO
2740 #define DEFAULT_IS_ZERO 0
2742 #include <isl_pw_templ.c>
2743 #include <isl_pw_add_constant_val_templ.c>
2744 #include <isl_pw_bind_domain_templ.c>
2745 #include <isl_pw_eval.c>
2746 #include <isl_pw_hash.c>
2747 #include <isl_pw_insert_dims_templ.c>
2748 #include <isl_pw_move_dims_templ.c>
2749 #include <isl_pw_neg_templ.c>
2750 #include <isl_pw_pullback_templ.c>
2751 #include <isl_pw_sub_templ.c>
2752 #include <isl_pw_union_opt.c>
2757 #include <isl_union_single.c>
2758 #include <isl_union_neg.c>
2760 /* Compute a piecewise quasi-affine expression with a domain that
2761 * is the union of those of pwaff1 and pwaff2 and such that on each
2762 * cell, the quasi-affine expression is the maximum of those of pwaff1
2763 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2764 * cell, then the associated expression is the defined one.
2766 __isl_give isl_pw_aff
*isl_pw_aff_union_max(__isl_take isl_pw_aff
*pwaff1
,
2767 __isl_take isl_pw_aff
*pwaff2
)
2769 isl_pw_aff_align_params_bin(&pwaff1
, &pwaff2
);
2770 return isl_pw_aff_union_opt_cmp(pwaff1
, pwaff2
, &isl_aff_ge_set
);
2773 /* Compute a piecewise quasi-affine expression with a domain that
2774 * is the union of those of pwaff1 and pwaff2 and such that on each
2775 * cell, the quasi-affine expression is the minimum of those of pwaff1
2776 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2777 * cell, then the associated expression is the defined one.
2779 __isl_give isl_pw_aff
*isl_pw_aff_union_min(__isl_take isl_pw_aff
*pwaff1
,
2780 __isl_take isl_pw_aff
*pwaff2
)
2782 isl_pw_aff_align_params_bin(&pwaff1
, &pwaff2
);
2783 return isl_pw_aff_union_opt_cmp(pwaff1
, pwaff2
, &isl_aff_le_set
);
2786 __isl_give isl_pw_aff
*isl_pw_aff_union_opt(__isl_take isl_pw_aff
*pwaff1
,
2787 __isl_take isl_pw_aff
*pwaff2
, int max
)
2790 return isl_pw_aff_union_max(pwaff1
, pwaff2
);
2792 return isl_pw_aff_union_min(pwaff1
, pwaff2
);
2795 /* Is the domain of "pa" a product?
2797 static isl_bool
isl_pw_aff_domain_is_product(__isl_keep isl_pw_aff
*pa
)
2799 return isl_space_domain_is_wrapping(isl_pw_aff_peek_space(pa
));
2803 #define TYPE isl_pw_aff
2804 #include <isl_domain_factor_templ.c>
2806 /* Return a set containing those elements in the domain
2807 * of "pwaff" where it satisfies "fn" (if complement is 0) or
2808 * does not satisfy "fn" (if complement is 1).
2810 * The pieces with a NaN never belong to the result since
2811 * NaN does not satisfy any property.
2813 static __isl_give isl_set
*pw_aff_locus(__isl_take isl_pw_aff
*pwaff
,
2814 __isl_give isl_basic_set
*(*fn
)(__isl_take isl_aff
*aff
, int rational
,
2816 int complement
, void *user
)
2824 set
= isl_set_empty(isl_pw_aff_get_domain_space(pwaff
));
2826 for (i
= 0; i
< pwaff
->n
; ++i
) {
2827 isl_basic_set
*bset
;
2828 isl_set
*set_i
, *locus
;
2831 if (isl_aff_is_nan(pwaff
->p
[i
].aff
))
2834 rational
= isl_set_has_rational(pwaff
->p
[i
].set
);
2835 bset
= fn(isl_aff_copy(pwaff
->p
[i
].aff
), rational
, user
);
2836 locus
= isl_set_from_basic_set(bset
);
2837 set_i
= isl_set_copy(pwaff
->p
[i
].set
);
2839 set_i
= isl_set_subtract(set_i
, locus
);
2841 set_i
= isl_set_intersect(set_i
, locus
);
2842 set
= isl_set_union_disjoint(set
, set_i
);
2845 isl_pw_aff_free(pwaff
);
2850 /* Return a set containing those elements in the domain
2851 * of "pa" where it is positive.
2853 __isl_give isl_set
*isl_pw_aff_pos_set(__isl_take isl_pw_aff
*pa
)
2855 return pw_aff_locus(pa
, &aff_pos_basic_set
, 0, NULL
);
2858 /* Return a set containing those elements in the domain
2859 * of pwaff where it is non-negative.
2861 __isl_give isl_set
*isl_pw_aff_nonneg_set(__isl_take isl_pw_aff
*pwaff
)
2863 return pw_aff_locus(pwaff
, &aff_nonneg_basic_set
, 0, NULL
);
2866 /* Return a set containing those elements in the domain
2867 * of pwaff where it is zero.
2869 __isl_give isl_set
*isl_pw_aff_zero_set(__isl_take isl_pw_aff
*pwaff
)
2871 return pw_aff_locus(pwaff
, &aff_zero_basic_set
, 0, NULL
);
2874 /* Return a set containing those elements in the domain
2875 * of pwaff where it is not zero.
2877 __isl_give isl_set
*isl_pw_aff_non_zero_set(__isl_take isl_pw_aff
*pwaff
)
2879 return pw_aff_locus(pwaff
, &aff_zero_basic_set
, 1, NULL
);
2882 /* Bind the affine function "aff" to the parameter "id",
2883 * returning the elements in the domain where the affine expression
2884 * is equal to the parameter.
2886 __isl_give isl_basic_set
*isl_aff_bind_id(__isl_take isl_aff
*aff
,
2887 __isl_take isl_id
*id
)
2892 space
= isl_aff_get_domain_space(aff
);
2893 space
= isl_space_add_param_id(space
, isl_id_copy(id
));
2895 aff
= isl_aff_align_params(aff
, isl_space_copy(space
));
2896 aff_id
= isl_aff_param_on_domain_space_id(space
, id
);
2898 return isl_aff_eq_basic_set(aff
, aff_id
);
2901 /* Wrapper around isl_aff_bind_id for use as pw_aff_locus callback.
2902 * "rational" should not be set.
2904 static __isl_give isl_basic_set
*aff_bind_id(__isl_take isl_aff
*aff
,
2905 int rational
, void *user
)
2912 isl_die(isl_aff_get_ctx(aff
), isl_error_unsupported
,
2913 "rational binding not supported", goto error
);
2914 return isl_aff_bind_id(aff
, isl_id_copy(id
));
2920 /* Bind the piecewise affine function "pa" to the parameter "id",
2921 * returning the elements in the domain where the expression
2922 * is equal to the parameter.
2924 __isl_give isl_set
*isl_pw_aff_bind_id(__isl_take isl_pw_aff
*pa
,
2925 __isl_take isl_id
*id
)
2929 bound
= pw_aff_locus(pa
, &aff_bind_id
, 0, id
);
2935 /* Return a set containing those elements in the shared domain
2936 * of pwaff1 and pwaff2 where pwaff1 is greater than (or equal) to pwaff2.
2938 * We compute the difference on the shared domain and then construct
2939 * the set of values where this difference is non-negative.
2940 * If strict is set, we first subtract 1 from the difference.
2941 * If equal is set, we only return the elements where pwaff1 and pwaff2
2944 static __isl_give isl_set
*pw_aff_gte_set(__isl_take isl_pw_aff
*pwaff1
,
2945 __isl_take isl_pw_aff
*pwaff2
, int strict
, int equal
)
2947 isl_set
*set1
, *set2
;
2949 set1
= isl_pw_aff_domain(isl_pw_aff_copy(pwaff1
));
2950 set2
= isl_pw_aff_domain(isl_pw_aff_copy(pwaff2
));
2951 set1
= isl_set_intersect(set1
, set2
);
2952 pwaff1
= isl_pw_aff_intersect_domain(pwaff1
, isl_set_copy(set1
));
2953 pwaff2
= isl_pw_aff_intersect_domain(pwaff2
, isl_set_copy(set1
));
2954 pwaff1
= isl_pw_aff_add(pwaff1
, isl_pw_aff_neg(pwaff2
));
2957 isl_space
*space
= isl_set_get_space(set1
);
2959 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(space
));
2960 aff
= isl_aff_add_constant_si(aff
, -1);
2961 pwaff1
= isl_pw_aff_add(pwaff1
, isl_pw_aff_alloc(set1
, aff
));
2966 return isl_pw_aff_zero_set(pwaff1
);
2967 return isl_pw_aff_nonneg_set(pwaff1
);
2970 /* Return a set containing those elements in the shared domain
2971 * of pwaff1 and pwaff2 where pwaff1 is equal to pwaff2.
2973 __isl_give isl_set
*isl_pw_aff_eq_set(__isl_take isl_pw_aff
*pwaff1
,
2974 __isl_take isl_pw_aff
*pwaff2
)
2976 isl_pw_aff_align_params_bin(&pwaff1
, &pwaff2
);
2977 return pw_aff_gte_set(pwaff1
, pwaff2
, 0, 1);
2980 /* Return a set containing those elements in the shared domain
2981 * of pwaff1 and pwaff2 where pwaff1 is greater than or equal to pwaff2.
2983 __isl_give isl_set
*isl_pw_aff_ge_set(__isl_take isl_pw_aff
*pwaff1
,
2984 __isl_take isl_pw_aff
*pwaff2
)
2986 isl_pw_aff_align_params_bin(&pwaff1
, &pwaff2
);
2987 return pw_aff_gte_set(pwaff1
, pwaff2
, 0, 0);
2990 /* Return a set containing those elements in the shared domain
2991 * of pwaff1 and pwaff2 where pwaff1 is strictly greater than pwaff2.
2993 __isl_give isl_set
*isl_pw_aff_gt_set(__isl_take isl_pw_aff
*pwaff1
,
2994 __isl_take isl_pw_aff
*pwaff2
)
2996 isl_pw_aff_align_params_bin(&pwaff1
, &pwaff2
);
2997 return pw_aff_gte_set(pwaff1
, pwaff2
, 1, 0);
3000 __isl_give isl_set
*isl_pw_aff_le_set(__isl_take isl_pw_aff
*pwaff1
,
3001 __isl_take isl_pw_aff
*pwaff2
)
3003 return isl_pw_aff_ge_set(pwaff2
, pwaff1
);
3006 __isl_give isl_set
*isl_pw_aff_lt_set(__isl_take isl_pw_aff
*pwaff1
,
3007 __isl_take isl_pw_aff
*pwaff2
)
3009 return isl_pw_aff_gt_set(pwaff2
, pwaff1
);
3012 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3013 * where the function values are ordered in the same way as "order",
3014 * which returns a set in the shared domain of its two arguments.
3016 * Let "pa1" and "pa2" be defined on domains A and B respectively.
3017 * We first pull back the two functions such that they are defined on
3018 * the domain [A -> B]. Then we apply "order", resulting in a set
3019 * in the space [A -> B]. Finally, we unwrap this set to obtain
3020 * a map in the space A -> B.
3022 static __isl_give isl_map
*isl_pw_aff_order_map(
3023 __isl_take isl_pw_aff
*pa1
, __isl_take isl_pw_aff
*pa2
,
3024 __isl_give isl_set
*(*order
)(__isl_take isl_pw_aff
*pa1
,
3025 __isl_take isl_pw_aff
*pa2
))
3027 isl_space
*space1
, *space2
;
3031 isl_pw_aff_align_params_bin(&pa1
, &pa2
);
3032 space1
= isl_space_domain(isl_pw_aff_get_space(pa1
));
3033 space2
= isl_space_domain(isl_pw_aff_get_space(pa2
));
3034 space1
= isl_space_map_from_domain_and_range(space1
, space2
);
3035 ma
= isl_multi_aff_domain_map(isl_space_copy(space1
));
3036 pa1
= isl_pw_aff_pullback_multi_aff(pa1
, ma
);
3037 ma
= isl_multi_aff_range_map(space1
);
3038 pa2
= isl_pw_aff_pullback_multi_aff(pa2
, ma
);
3039 set
= order(pa1
, pa2
);
3041 return isl_set_unwrap(set
);
3044 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3045 * where the function values are equal.
3047 __isl_give isl_map
*isl_pw_aff_eq_map(__isl_take isl_pw_aff
*pa1
,
3048 __isl_take isl_pw_aff
*pa2
)
3050 return isl_pw_aff_order_map(pa1
, pa2
, &isl_pw_aff_eq_set
);
3053 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3054 * where the function value of "pa1" is less than or equal to
3055 * the function value of "pa2".
3057 __isl_give isl_map
*isl_pw_aff_le_map(__isl_take isl_pw_aff
*pa1
,
3058 __isl_take isl_pw_aff
*pa2
)
3060 return isl_pw_aff_order_map(pa1
, pa2
, &isl_pw_aff_le_set
);
3063 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3064 * where the function value of "pa1" is less than the function value of "pa2".
3066 __isl_give isl_map
*isl_pw_aff_lt_map(__isl_take isl_pw_aff
*pa1
,
3067 __isl_take isl_pw_aff
*pa2
)
3069 return isl_pw_aff_order_map(pa1
, pa2
, &isl_pw_aff_lt_set
);
3072 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3073 * where the function value of "pa1" is greater than or equal to
3074 * the function value of "pa2".
3076 __isl_give isl_map
*isl_pw_aff_ge_map(__isl_take isl_pw_aff
*pa1
,
3077 __isl_take isl_pw_aff
*pa2
)
3079 return isl_pw_aff_order_map(pa1
, pa2
, &isl_pw_aff_ge_set
);
3082 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3083 * where the function value of "pa1" is greater than the function value
3086 __isl_give isl_map
*isl_pw_aff_gt_map(__isl_take isl_pw_aff
*pa1
,
3087 __isl_take isl_pw_aff
*pa2
)
3089 return isl_pw_aff_order_map(pa1
, pa2
, &isl_pw_aff_gt_set
);
3092 /* Return a set containing those elements in the shared domain
3093 * of the elements of list1 and list2 where each element in list1
3094 * has the relation specified by "fn" with each element in list2.
3096 static __isl_give isl_set
*pw_aff_list_set(__isl_take isl_pw_aff_list
*list1
,
3097 __isl_take isl_pw_aff_list
*list2
,
3098 __isl_give isl_set
*(*fn
)(__isl_take isl_pw_aff
*pwaff1
,
3099 __isl_take isl_pw_aff
*pwaff2
))
3105 if (!list1
|| !list2
)
3108 ctx
= isl_pw_aff_list_get_ctx(list1
);
3109 if (list1
->n
< 1 || list2
->n
< 1)
3110 isl_die(ctx
, isl_error_invalid
,
3111 "list should contain at least one element", goto error
);
3113 set
= isl_set_universe(isl_pw_aff_get_domain_space(list1
->p
[0]));
3114 for (i
= 0; i
< list1
->n
; ++i
)
3115 for (j
= 0; j
< list2
->n
; ++j
) {
3118 set_ij
= fn(isl_pw_aff_copy(list1
->p
[i
]),
3119 isl_pw_aff_copy(list2
->p
[j
]));
3120 set
= isl_set_intersect(set
, set_ij
);
3123 isl_pw_aff_list_free(list1
);
3124 isl_pw_aff_list_free(list2
);
3127 isl_pw_aff_list_free(list1
);
3128 isl_pw_aff_list_free(list2
);
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 * is equal to each element in list2.
3136 __isl_give isl_set
*isl_pw_aff_list_eq_set(__isl_take isl_pw_aff_list
*list1
,
3137 __isl_take isl_pw_aff_list
*list2
)
3139 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_eq_set
);
3142 __isl_give isl_set
*isl_pw_aff_list_ne_set(__isl_take isl_pw_aff_list
*list1
,
3143 __isl_take isl_pw_aff_list
*list2
)
3145 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_ne_set
);
3148 /* Return a set containing those elements in the shared domain
3149 * of the elements of list1 and list2 where each element in list1
3150 * is less than or equal to each element in list2.
3152 __isl_give isl_set
*isl_pw_aff_list_le_set(__isl_take isl_pw_aff_list
*list1
,
3153 __isl_take isl_pw_aff_list
*list2
)
3155 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_le_set
);
3158 __isl_give isl_set
*isl_pw_aff_list_lt_set(__isl_take isl_pw_aff_list
*list1
,
3159 __isl_take isl_pw_aff_list
*list2
)
3161 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_lt_set
);
3164 __isl_give isl_set
*isl_pw_aff_list_ge_set(__isl_take isl_pw_aff_list
*list1
,
3165 __isl_take isl_pw_aff_list
*list2
)
3167 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_ge_set
);
3170 __isl_give isl_set
*isl_pw_aff_list_gt_set(__isl_take isl_pw_aff_list
*list1
,
3171 __isl_take isl_pw_aff_list
*list2
)
3173 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_gt_set
);
3177 /* Return a set containing those elements in the shared domain
3178 * of pwaff1 and pwaff2 where pwaff1 is not equal to pwaff2.
3180 __isl_give isl_set
*isl_pw_aff_ne_set(__isl_take isl_pw_aff
*pwaff1
,
3181 __isl_take isl_pw_aff
*pwaff2
)
3183 isl_set
*set_lt
, *set_gt
;
3185 isl_pw_aff_align_params_bin(&pwaff1
, &pwaff2
);
3186 set_lt
= isl_pw_aff_lt_set(isl_pw_aff_copy(pwaff1
),
3187 isl_pw_aff_copy(pwaff2
));
3188 set_gt
= isl_pw_aff_gt_set(pwaff1
, pwaff2
);
3189 return isl_set_union_disjoint(set_lt
, set_gt
);
3192 __isl_give isl_pw_aff
*isl_pw_aff_scale_down(__isl_take isl_pw_aff
*pwaff
,
3197 if (isl_int_is_one(v
))
3199 if (!isl_int_is_pos(v
))
3200 isl_die(isl_pw_aff_get_ctx(pwaff
), isl_error_invalid
,
3201 "factor needs to be positive",
3202 return isl_pw_aff_free(pwaff
));
3203 pwaff
= isl_pw_aff_cow(pwaff
);
3209 for (i
= 0; i
< pwaff
->n
; ++i
) {
3210 pwaff
->p
[i
].aff
= isl_aff_scale_down(pwaff
->p
[i
].aff
, v
);
3211 if (!pwaff
->p
[i
].aff
)
3212 return isl_pw_aff_free(pwaff
);
3218 __isl_give isl_pw_aff
*isl_pw_aff_floor(__isl_take isl_pw_aff
*pwaff
)
3222 pwaff
= isl_pw_aff_cow(pwaff
);
3228 for (i
= 0; i
< pwaff
->n
; ++i
) {
3229 pwaff
->p
[i
].aff
= isl_aff_floor(pwaff
->p
[i
].aff
);
3230 if (!pwaff
->p
[i
].aff
)
3231 return isl_pw_aff_free(pwaff
);
3237 __isl_give isl_pw_aff
*isl_pw_aff_ceil(__isl_take isl_pw_aff
*pwaff
)
3241 pwaff
= isl_pw_aff_cow(pwaff
);
3247 for (i
= 0; i
< pwaff
->n
; ++i
) {
3248 pwaff
->p
[i
].aff
= isl_aff_ceil(pwaff
->p
[i
].aff
);
3249 if (!pwaff
->p
[i
].aff
)
3250 return isl_pw_aff_free(pwaff
);
3256 /* Assuming that "cond1" and "cond2" are disjoint,
3257 * return an affine expression that is equal to pwaff1 on cond1
3258 * and to pwaff2 on cond2.
3260 static __isl_give isl_pw_aff
*isl_pw_aff_select(
3261 __isl_take isl_set
*cond1
, __isl_take isl_pw_aff
*pwaff1
,
3262 __isl_take isl_set
*cond2
, __isl_take isl_pw_aff
*pwaff2
)
3264 pwaff1
= isl_pw_aff_intersect_domain(pwaff1
, cond1
);
3265 pwaff2
= isl_pw_aff_intersect_domain(pwaff2
, cond2
);
3267 return isl_pw_aff_add_disjoint(pwaff1
, pwaff2
);
3270 /* Return an affine expression that is equal to pwaff_true for elements
3271 * where "cond" is non-zero and to pwaff_false for elements where "cond"
3273 * That is, return cond ? pwaff_true : pwaff_false;
3275 * If "cond" involves and NaN, then we conservatively return a NaN
3276 * on its entire domain. In principle, we could consider the pieces
3277 * where it is NaN separately from those where it is not.
3279 * If "pwaff_true" and "pwaff_false" are obviously equal to each other,
3280 * then only use the domain of "cond" to restrict the domain.
3282 __isl_give isl_pw_aff
*isl_pw_aff_cond(__isl_take isl_pw_aff
*cond
,
3283 __isl_take isl_pw_aff
*pwaff_true
, __isl_take isl_pw_aff
*pwaff_false
)
3285 isl_set
*cond_true
, *cond_false
;
3290 if (isl_pw_aff_involves_nan(cond
)) {
3291 isl_space
*space
= isl_pw_aff_get_domain_space(cond
);
3292 isl_local_space
*ls
= isl_local_space_from_space(space
);
3293 isl_pw_aff_free(cond
);
3294 isl_pw_aff_free(pwaff_true
);
3295 isl_pw_aff_free(pwaff_false
);
3296 return isl_pw_aff_nan_on_domain(ls
);
3299 pwaff_true
= isl_pw_aff_align_params(pwaff_true
,
3300 isl_pw_aff_get_space(pwaff_false
));
3301 pwaff_false
= isl_pw_aff_align_params(pwaff_false
,
3302 isl_pw_aff_get_space(pwaff_true
));
3303 equal
= isl_pw_aff_plain_is_equal(pwaff_true
, pwaff_false
);
3309 dom
= isl_set_coalesce(isl_pw_aff_domain(cond
));
3310 isl_pw_aff_free(pwaff_false
);
3311 return isl_pw_aff_intersect_domain(pwaff_true
, dom
);
3314 cond_true
= isl_pw_aff_non_zero_set(isl_pw_aff_copy(cond
));
3315 cond_false
= isl_pw_aff_zero_set(cond
);
3316 return isl_pw_aff_select(cond_true
, pwaff_true
,
3317 cond_false
, pwaff_false
);
3319 isl_pw_aff_free(cond
);
3320 isl_pw_aff_free(pwaff_true
);
3321 isl_pw_aff_free(pwaff_false
);
3325 isl_bool
isl_aff_is_cst(__isl_keep isl_aff
*aff
)
3330 return isl_bool_error
;
3332 pos
= isl_seq_first_non_zero(aff
->v
->el
+ 2, aff
->v
->size
- 2);
3333 return isl_bool_ok(pos
== -1);
3336 /* Check whether pwaff is a piecewise constant.
3338 isl_bool
isl_pw_aff_is_cst(__isl_keep isl_pw_aff
*pwaff
)
3343 return isl_bool_error
;
3345 for (i
= 0; i
< pwaff
->n
; ++i
) {
3346 isl_bool is_cst
= isl_aff_is_cst(pwaff
->p
[i
].aff
);
3347 if (is_cst
< 0 || !is_cst
)
3351 return isl_bool_true
;
3354 /* Return the product of "aff1" and "aff2".
3356 * If either of the two is NaN, then the result is NaN.
3358 * Otherwise, at least one of "aff1" or "aff2" needs to be a constant.
3360 __isl_give isl_aff
*isl_aff_mul(__isl_take isl_aff
*aff1
,
3361 __isl_take isl_aff
*aff2
)
3366 if (isl_aff_is_nan(aff1
)) {
3370 if (isl_aff_is_nan(aff2
)) {
3375 if (!isl_aff_is_cst(aff2
) && isl_aff_is_cst(aff1
))
3376 return isl_aff_mul(aff2
, aff1
);
3378 if (!isl_aff_is_cst(aff2
))
3379 isl_die(isl_aff_get_ctx(aff1
), isl_error_invalid
,
3380 "at least one affine expression should be constant",
3383 aff1
= isl_aff_cow(aff1
);
3387 aff1
= isl_aff_scale(aff1
, aff2
->v
->el
[1]);
3388 aff1
= isl_aff_scale_down(aff1
, aff2
->v
->el
[0]);
3398 /* Divide "aff1" by "aff2", assuming "aff2" is a constant.
3400 * If either of the two is NaN, then the result is NaN.
3402 __isl_give isl_aff
*isl_aff_div(__isl_take isl_aff
*aff1
,
3403 __isl_take isl_aff
*aff2
)
3411 if (isl_aff_is_nan(aff1
)) {
3415 if (isl_aff_is_nan(aff2
)) {
3420 is_cst
= isl_aff_is_cst(aff2
);
3424 isl_die(isl_aff_get_ctx(aff2
), isl_error_invalid
,
3425 "second argument should be a constant", goto error
);
3430 neg
= isl_int_is_neg(aff2
->v
->el
[1]);
3432 isl_int_neg(aff2
->v
->el
[0], aff2
->v
->el
[0]);
3433 isl_int_neg(aff2
->v
->el
[1], aff2
->v
->el
[1]);
3436 aff1
= isl_aff_scale(aff1
, aff2
->v
->el
[0]);
3437 aff1
= isl_aff_scale_down(aff1
, aff2
->v
->el
[1]);
3440 isl_int_neg(aff2
->v
->el
[0], aff2
->v
->el
[0]);
3441 isl_int_neg(aff2
->v
->el
[1], aff2
->v
->el
[1]);
3452 __isl_give isl_pw_aff
*isl_pw_aff_add(__isl_take isl_pw_aff
*pwaff1
,
3453 __isl_take isl_pw_aff
*pwaff2
)
3455 isl_pw_aff_align_params_bin(&pwaff1
, &pwaff2
);
3456 return isl_pw_aff_on_shared_domain(pwaff1
, pwaff2
, &isl_aff_add
);
3459 __isl_give isl_pw_aff
*isl_pw_aff_union_add(__isl_take isl_pw_aff
*pwaff1
,
3460 __isl_take isl_pw_aff
*pwaff2
)
3462 return isl_pw_aff_union_add_(pwaff1
, pwaff2
);
3465 __isl_give isl_pw_aff
*isl_pw_aff_mul(__isl_take isl_pw_aff
*pwaff1
,
3466 __isl_take isl_pw_aff
*pwaff2
)
3468 isl_pw_aff_align_params_bin(&pwaff1
, &pwaff2
);
3469 return isl_pw_aff_on_shared_domain(pwaff1
, pwaff2
, &isl_aff_mul
);
3472 /* Divide "pa1" by "pa2", assuming "pa2" is a piecewise constant.
3474 __isl_give isl_pw_aff
*isl_pw_aff_div(__isl_take isl_pw_aff
*pa1
,
3475 __isl_take isl_pw_aff
*pa2
)
3479 is_cst
= isl_pw_aff_is_cst(pa2
);
3483 isl_die(isl_pw_aff_get_ctx(pa2
), isl_error_invalid
,
3484 "second argument should be a piecewise constant",
3486 isl_pw_aff_align_params_bin(&pa1
, &pa2
);
3487 return isl_pw_aff_on_shared_domain(pa1
, pa2
, &isl_aff_div
);
3489 isl_pw_aff_free(pa1
);
3490 isl_pw_aff_free(pa2
);
3494 /* Compute the quotient of the integer division of "pa1" by "pa2"
3495 * with rounding towards zero.
3496 * "pa2" is assumed to be a piecewise constant.
3498 * In particular, return
3500 * pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2)
3503 __isl_give isl_pw_aff
*isl_pw_aff_tdiv_q(__isl_take isl_pw_aff
*pa1
,
3504 __isl_take isl_pw_aff
*pa2
)
3510 is_cst
= isl_pw_aff_is_cst(pa2
);
3514 isl_die(isl_pw_aff_get_ctx(pa2
), isl_error_invalid
,
3515 "second argument should be a piecewise constant",
3518 pa1
= isl_pw_aff_div(pa1
, pa2
);
3520 cond
= isl_pw_aff_nonneg_set(isl_pw_aff_copy(pa1
));
3521 f
= isl_pw_aff_floor(isl_pw_aff_copy(pa1
));
3522 c
= isl_pw_aff_ceil(pa1
);
3523 return isl_pw_aff_cond(isl_set_indicator_function(cond
), f
, c
);
3525 isl_pw_aff_free(pa1
);
3526 isl_pw_aff_free(pa2
);
3530 /* Compute the remainder of the integer division of "pa1" by "pa2"
3531 * with rounding towards zero.
3532 * "pa2" is assumed to be a piecewise constant.
3534 * In particular, return
3536 * pa1 - pa2 * (pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2))
3539 __isl_give isl_pw_aff
*isl_pw_aff_tdiv_r(__isl_take isl_pw_aff
*pa1
,
3540 __isl_take isl_pw_aff
*pa2
)
3545 is_cst
= isl_pw_aff_is_cst(pa2
);
3549 isl_die(isl_pw_aff_get_ctx(pa2
), isl_error_invalid
,
3550 "second argument should be a piecewise constant",
3552 res
= isl_pw_aff_tdiv_q(isl_pw_aff_copy(pa1
), isl_pw_aff_copy(pa2
));
3553 res
= isl_pw_aff_mul(pa2
, res
);
3554 res
= isl_pw_aff_sub(pa1
, res
);
3557 isl_pw_aff_free(pa1
);
3558 isl_pw_aff_free(pa2
);
3562 /* Does either of "pa1" or "pa2" involve any NaN2?
3564 static isl_bool
either_involves_nan(__isl_keep isl_pw_aff
*pa1
,
3565 __isl_keep isl_pw_aff
*pa2
)
3569 has_nan
= isl_pw_aff_involves_nan(pa1
);
3570 if (has_nan
< 0 || has_nan
)
3572 return isl_pw_aff_involves_nan(pa2
);
3575 /* Replace "pa1" and "pa2" (at least one of which involves a NaN)
3576 * by a NaN on their shared domain.
3578 * In principle, the result could be refined to only being NaN
3579 * on the parts of this domain where at least one of "pa1" or "pa2" is NaN.
3581 static __isl_give isl_pw_aff
*replace_by_nan(__isl_take isl_pw_aff
*pa1
,
3582 __isl_take isl_pw_aff
*pa2
)
3584 isl_local_space
*ls
;
3588 dom
= isl_set_intersect(isl_pw_aff_domain(pa1
), isl_pw_aff_domain(pa2
));
3589 ls
= isl_local_space_from_space(isl_set_get_space(dom
));
3590 pa
= isl_pw_aff_nan_on_domain(ls
);
3591 pa
= isl_pw_aff_intersect_domain(pa
, dom
);
3596 static __isl_give isl_pw_aff
*pw_aff_min(__isl_take isl_pw_aff
*pwaff1
,
3597 __isl_take isl_pw_aff
*pwaff2
)
3602 dom
= isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1
)),
3603 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2
)));
3604 le
= isl_pw_aff_le_set(isl_pw_aff_copy(pwaff1
),
3605 isl_pw_aff_copy(pwaff2
));
3606 dom
= isl_set_subtract(dom
, isl_set_copy(le
));
3607 return isl_pw_aff_select(le
, pwaff1
, dom
, pwaff2
);
3610 static __isl_give isl_pw_aff
*pw_aff_max(__isl_take isl_pw_aff
*pwaff1
,
3611 __isl_take isl_pw_aff
*pwaff2
)
3616 dom
= isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1
)),
3617 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2
)));
3618 ge
= isl_pw_aff_ge_set(isl_pw_aff_copy(pwaff1
),
3619 isl_pw_aff_copy(pwaff2
));
3620 dom
= isl_set_subtract(dom
, isl_set_copy(ge
));
3621 return isl_pw_aff_select(ge
, pwaff1
, dom
, pwaff2
);
3624 /* Return an expression for the minimum (if "max" is not set) or
3625 * the maximum (if "max" is set) of "pa1" and "pa2".
3626 * If either expression involves any NaN, then return a NaN
3627 * on the shared domain as result.
3629 static __isl_give isl_pw_aff
*pw_aff_min_max(__isl_take isl_pw_aff
*pa1
,
3630 __isl_take isl_pw_aff
*pa2
, int max
)
3634 has_nan
= either_involves_nan(pa1
, pa2
);
3636 pa1
= isl_pw_aff_free(pa1
);
3638 return replace_by_nan(pa1
, pa2
);
3640 isl_pw_aff_align_params_bin(&pa1
, &pa2
);
3642 return pw_aff_max(pa1
, pa2
);
3644 return pw_aff_min(pa1
, pa2
);
3647 /* Return an expression for the minimum of "pwaff1" and "pwaff2".
3649 __isl_give isl_pw_aff
*isl_pw_aff_min(__isl_take isl_pw_aff
*pwaff1
,
3650 __isl_take isl_pw_aff
*pwaff2
)
3652 return pw_aff_min_max(pwaff1
, pwaff2
, 0);
3655 /* Return an expression for the maximum of "pwaff1" and "pwaff2".
3657 __isl_give isl_pw_aff
*isl_pw_aff_max(__isl_take isl_pw_aff
*pwaff1
,
3658 __isl_take isl_pw_aff
*pwaff2
)
3660 return pw_aff_min_max(pwaff1
, pwaff2
, 1);
3663 static __isl_give isl_pw_aff
*pw_aff_list_reduce(
3664 __isl_take isl_pw_aff_list
*list
,
3665 __isl_give isl_pw_aff
*(*fn
)(__isl_take isl_pw_aff
*pwaff1
,
3666 __isl_take isl_pw_aff
*pwaff2
))
3675 ctx
= isl_pw_aff_list_get_ctx(list
);
3677 isl_die(ctx
, isl_error_invalid
,
3678 "list should contain at least one element", goto error
);
3680 res
= isl_pw_aff_copy(list
->p
[0]);
3681 for (i
= 1; i
< list
->n
; ++i
)
3682 res
= fn(res
, isl_pw_aff_copy(list
->p
[i
]));
3684 isl_pw_aff_list_free(list
);
3687 isl_pw_aff_list_free(list
);
3691 /* Return an isl_pw_aff that maps each element in the intersection of the
3692 * domains of the elements of list to the minimal corresponding affine
3695 __isl_give isl_pw_aff
*isl_pw_aff_list_min(__isl_take isl_pw_aff_list
*list
)
3697 return pw_aff_list_reduce(list
, &isl_pw_aff_min
);
3700 /* Return an isl_pw_aff that maps each element in the intersection of the
3701 * domains of the elements of list to the maximal corresponding affine
3704 __isl_give isl_pw_aff
*isl_pw_aff_list_max(__isl_take isl_pw_aff_list
*list
)
3706 return pw_aff_list_reduce(list
, &isl_pw_aff_max
);
3709 /* Mark the domains of "pwaff" as rational.
3711 __isl_give isl_pw_aff
*isl_pw_aff_set_rational(__isl_take isl_pw_aff
*pwaff
)
3715 pwaff
= isl_pw_aff_cow(pwaff
);
3721 for (i
= 0; i
< pwaff
->n
; ++i
) {
3722 pwaff
->p
[i
].set
= isl_set_set_rational(pwaff
->p
[i
].set
);
3723 if (!pwaff
->p
[i
].set
)
3724 return isl_pw_aff_free(pwaff
);
3730 /* Mark the domains of the elements of "list" as rational.
3732 __isl_give isl_pw_aff_list
*isl_pw_aff_list_set_rational(
3733 __isl_take isl_pw_aff_list
*list
)
3743 for (i
= 0; i
< n
; ++i
) {
3746 pa
= isl_pw_aff_list_get_pw_aff(list
, i
);
3747 pa
= isl_pw_aff_set_rational(pa
);
3748 list
= isl_pw_aff_list_set_pw_aff(list
, i
, pa
);
3754 /* Do the parameters of "aff" match those of "space"?
3756 isl_bool
isl_aff_matching_params(__isl_keep isl_aff
*aff
,
3757 __isl_keep isl_space
*space
)
3759 isl_space
*aff_space
;
3763 return isl_bool_error
;
3765 aff_space
= isl_aff_get_domain_space(aff
);
3767 match
= isl_space_has_equal_params(space
, aff_space
);
3769 isl_space_free(aff_space
);
3773 /* Check that the domain space of "aff" matches "space".
3775 isl_stat
isl_aff_check_match_domain_space(__isl_keep isl_aff
*aff
,
3776 __isl_keep isl_space
*space
)
3778 isl_space
*aff_space
;
3782 return isl_stat_error
;
3784 aff_space
= isl_aff_get_domain_space(aff
);
3786 match
= isl_space_has_equal_params(space
, aff_space
);
3790 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
3791 "parameters don't match", goto error
);
3792 match
= isl_space_tuple_is_equal(space
, isl_dim_in
,
3793 aff_space
, isl_dim_set
);
3797 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
3798 "domains don't match", goto error
);
3799 isl_space_free(aff_space
);
3802 isl_space_free(aff_space
);
3803 return isl_stat_error
;
3806 /* Return the shared (universe) domain of the elements of "ma".
3808 * Since an isl_multi_aff (and an isl_aff) is always total,
3809 * the domain is always the universe set in its domain space.
3810 * This is a helper function for use in the generic isl_multi_*_bind.
3812 static __isl_give isl_basic_set
*isl_multi_aff_domain(
3813 __isl_take isl_multi_aff
*ma
)
3817 space
= isl_multi_aff_get_space(ma
);
3818 isl_multi_aff_free(ma
);
3820 return isl_basic_set_universe(isl_space_domain(space
));
3826 #include <isl_multi_no_explicit_domain.c>
3827 #include <isl_multi_templ.c>
3828 #include <isl_multi_add_constant_templ.c>
3829 #include <isl_multi_apply_set.c>
3830 #include <isl_multi_arith_templ.c>
3831 #include <isl_multi_bind_domain_templ.c>
3832 #include <isl_multi_cmp.c>
3833 #include <isl_multi_dim_id_templ.c>
3834 #include <isl_multi_dims.c>
3835 #include <isl_multi_floor.c>
3836 #include <isl_multi_from_base_templ.c>
3837 #include <isl_multi_identity_templ.c>
3838 #include <isl_multi_locals_templ.c>
3839 #include <isl_multi_move_dims_templ.c>
3840 #include <isl_multi_nan_templ.c>
3841 #include <isl_multi_product_templ.c>
3842 #include <isl_multi_splice_templ.c>
3843 #include <isl_multi_tuple_id_templ.c>
3844 #include <isl_multi_zero_templ.c>
3848 #include <isl_multi_gist.c>
3851 #define DOMBASE basic_set
3852 #include <isl_multi_bind_templ.c>
3854 /* Construct an isl_multi_aff living in "space" that corresponds
3855 * to the affine transformation matrix "mat".
3857 __isl_give isl_multi_aff
*isl_multi_aff_from_aff_mat(
3858 __isl_take isl_space
*space
, __isl_take isl_mat
*mat
)
3861 isl_local_space
*ls
= NULL
;
3862 isl_multi_aff
*ma
= NULL
;
3863 isl_size n_row
, n_col
, n_out
, total
;
3869 ctx
= isl_mat_get_ctx(mat
);
3871 n_row
= isl_mat_rows(mat
);
3872 n_col
= isl_mat_cols(mat
);
3873 n_out
= isl_space_dim(space
, isl_dim_out
);
3874 total
= isl_space_dim(space
, isl_dim_all
);
3875 if (n_row
< 0 || n_col
< 0 || n_out
< 0 || total
< 0)
3878 isl_die(ctx
, isl_error_invalid
,
3879 "insufficient number of rows", goto error
);
3881 isl_die(ctx
, isl_error_invalid
,
3882 "insufficient number of columns", goto error
);
3883 if (1 + n_out
!= n_row
|| 2 + total
!= n_row
+ n_col
)
3884 isl_die(ctx
, isl_error_invalid
,
3885 "dimension mismatch", goto error
);
3887 ma
= isl_multi_aff_zero(isl_space_copy(space
));
3888 space
= isl_space_domain(space
);
3889 ls
= isl_local_space_from_space(isl_space_copy(space
));
3891 for (i
= 0; i
< n_row
- 1; ++i
) {
3895 v
= isl_vec_alloc(ctx
, 1 + n_col
);
3898 isl_int_set(v
->el
[0], mat
->row
[0][0]);
3899 isl_seq_cpy(v
->el
+ 1, mat
->row
[1 + i
], n_col
);
3900 v
= isl_vec_normalize(v
);
3901 aff
= isl_aff_alloc_vec(isl_local_space_copy(ls
), v
);
3902 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
3905 isl_space_free(space
);
3906 isl_local_space_free(ls
);
3910 isl_space_free(space
);
3911 isl_local_space_free(ls
);
3913 isl_multi_aff_free(ma
);
3917 /* Return the constant terms of the affine expressions of "ma".
3919 __isl_give isl_multi_val
*isl_multi_aff_get_constant_multi_val(
3920 __isl_keep isl_multi_aff
*ma
)
3927 n
= isl_multi_aff_size(ma
);
3930 space
= isl_space_range(isl_multi_aff_get_space(ma
));
3931 space
= isl_space_drop_all_params(space
);
3932 mv
= isl_multi_val_zero(space
);
3934 for (i
= 0; i
< n
; ++i
) {
3938 aff
= isl_multi_aff_get_at(ma
, i
);
3939 val
= isl_aff_get_constant_val(aff
);
3941 mv
= isl_multi_val_set_at(mv
, i
, val
);
3947 /* Remove any internal structure of the domain of "ma".
3948 * If there is any such internal structure in the input,
3949 * then the name of the corresponding space is also removed.
3951 __isl_give isl_multi_aff
*isl_multi_aff_flatten_domain(
3952 __isl_take isl_multi_aff
*ma
)
3959 if (!ma
->space
->nested
[0])
3962 space
= isl_multi_aff_get_space(ma
);
3963 space
= isl_space_flatten_domain(space
);
3964 ma
= isl_multi_aff_reset_space(ma
, space
);
3969 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
3970 * of the space to its domain.
3972 __isl_give isl_multi_aff
*isl_multi_aff_domain_map(__isl_take isl_space
*space
)
3976 isl_local_space
*ls
;
3981 if (!isl_space_is_map(space
))
3982 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
3983 "not a map space", goto error
);
3985 n_in
= isl_space_dim(space
, isl_dim_in
);
3988 space
= isl_space_domain_map(space
);
3990 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
3992 isl_space_free(space
);
3996 space
= isl_space_domain(space
);
3997 ls
= isl_local_space_from_space(space
);
3998 for (i
= 0; i
< n_in
; ++i
) {
4001 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
4003 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
4005 isl_local_space_free(ls
);
4008 isl_space_free(space
);
4012 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
4013 * of the space to its range.
4015 __isl_give isl_multi_aff
*isl_multi_aff_range_map(__isl_take isl_space
*space
)
4018 isl_size n_in
, n_out
;
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
);
4029 n_out
= isl_space_dim(space
, isl_dim_out
);
4030 if (n_in
< 0 || n_out
< 0)
4032 space
= isl_space_range_map(space
);
4034 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
4036 isl_space_free(space
);
4040 space
= isl_space_domain(space
);
4041 ls
= isl_local_space_from_space(space
);
4042 for (i
= 0; i
< n_out
; ++i
) {
4045 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
4046 isl_dim_set
, n_in
+ i
);
4047 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
4049 isl_local_space_free(ls
);
4052 isl_space_free(space
);
4056 /* Given a map space, return an isl_pw_multi_aff that maps a wrapped copy
4057 * of the space to its range.
4059 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_range_map(
4060 __isl_take isl_space
*space
)
4062 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_range_map(space
));
4065 /* Given the space of a set and a range of set dimensions,
4066 * construct an isl_multi_aff that projects out those dimensions.
4068 __isl_give isl_multi_aff
*isl_multi_aff_project_out_map(
4069 __isl_take isl_space
*space
, enum isl_dim_type type
,
4070 unsigned first
, unsigned n
)
4074 isl_local_space
*ls
;
4079 if (!isl_space_is_set(space
))
4080 isl_die(isl_space_get_ctx(space
), isl_error_unsupported
,
4081 "expecting set space", goto error
);
4082 if (type
!= isl_dim_set
)
4083 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
4084 "only set dimensions can be projected out", goto error
);
4085 if (isl_space_check_range(space
, type
, first
, n
) < 0)
4088 dim
= isl_space_dim(space
, isl_dim_set
);
4092 space
= isl_space_from_domain(space
);
4093 space
= isl_space_add_dims(space
, isl_dim_out
, dim
- n
);
4096 return isl_multi_aff_alloc(space
);
4098 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
4099 space
= isl_space_domain(space
);
4100 ls
= isl_local_space_from_space(space
);
4102 for (i
= 0; i
< first
; ++i
) {
4105 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
4107 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
4110 for (i
= 0; i
< dim
- (first
+ n
); ++i
) {
4113 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
4114 isl_dim_set
, first
+ n
+ i
);
4115 ma
= isl_multi_aff_set_aff(ma
, first
+ i
, aff
);
4118 isl_local_space_free(ls
);
4121 isl_space_free(space
);
4125 /* Given the space of a set and a range of set dimensions,
4126 * construct an isl_pw_multi_aff that projects out those dimensions.
4128 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_project_out_map(
4129 __isl_take isl_space
*space
, enum isl_dim_type type
,
4130 unsigned first
, unsigned n
)
4134 ma
= isl_multi_aff_project_out_map(space
, type
, first
, n
);
4135 return isl_pw_multi_aff_from_multi_aff(ma
);
4138 /* Create a piecewise multi-affine expression in the given space that maps each
4139 * input dimension to the corresponding output dimension.
4141 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_identity(
4142 __isl_take isl_space
*space
)
4144 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_identity(space
));
4147 /* Exploit the equalities in "eq" to simplify the affine expressions.
4149 static __isl_give isl_multi_aff
*isl_multi_aff_substitute_equalities(
4150 __isl_take isl_multi_aff
*maff
, __isl_take isl_basic_set
*eq
)
4154 maff
= isl_multi_aff_cow(maff
);
4158 for (i
= 0; i
< maff
->n
; ++i
) {
4159 maff
->u
.p
[i
] = isl_aff_substitute_equalities(maff
->u
.p
[i
],
4160 isl_basic_set_copy(eq
));
4165 isl_basic_set_free(eq
);
4168 isl_basic_set_free(eq
);
4169 isl_multi_aff_free(maff
);
4173 __isl_give isl_multi_aff
*isl_multi_aff_scale(__isl_take isl_multi_aff
*maff
,
4178 maff
= isl_multi_aff_cow(maff
);
4182 for (i
= 0; i
< maff
->n
; ++i
) {
4183 maff
->u
.p
[i
] = isl_aff_scale(maff
->u
.p
[i
], f
);
4185 return isl_multi_aff_free(maff
);
4191 __isl_give isl_multi_aff
*isl_multi_aff_add_on_domain(__isl_keep isl_set
*dom
,
4192 __isl_take isl_multi_aff
*maff1
, __isl_take isl_multi_aff
*maff2
)
4194 maff1
= isl_multi_aff_add(maff1
, maff2
);
4195 maff1
= isl_multi_aff_gist(maff1
, isl_set_copy(dom
));
4199 isl_bool
isl_multi_aff_is_empty(__isl_keep isl_multi_aff
*maff
)
4202 return isl_bool_error
;
4204 return isl_bool_false
;
4207 /* Return the set of domain elements where "ma1" is lexicographically
4208 * smaller than or equal to "ma2".
4210 __isl_give isl_set
*isl_multi_aff_lex_le_set(__isl_take isl_multi_aff
*ma1
,
4211 __isl_take isl_multi_aff
*ma2
)
4213 return isl_multi_aff_lex_ge_set(ma2
, ma1
);
4216 /* Return the set of domain elements where "ma1" is lexicographically
4217 * smaller than "ma2".
4219 __isl_give isl_set
*isl_multi_aff_lex_lt_set(__isl_take isl_multi_aff
*ma1
,
4220 __isl_take isl_multi_aff
*ma2
)
4222 return isl_multi_aff_lex_gt_set(ma2
, ma1
);
4225 /* Return the set of domain elements where "ma1" and "ma2"
4228 static __isl_give isl_set
*isl_multi_aff_order_set(
4229 __isl_take isl_multi_aff
*ma1
, __isl_take isl_multi_aff
*ma2
,
4230 __isl_give isl_map
*order(__isl_take isl_space
*set_space
))
4233 isl_map
*map1
, *map2
;
4236 map1
= isl_map_from_multi_aff_internal(ma1
);
4237 map2
= isl_map_from_multi_aff_internal(ma2
);
4238 map
= isl_map_range_product(map1
, map2
);
4239 space
= isl_space_range(isl_map_get_space(map
));
4240 space
= isl_space_domain(isl_space_unwrap(space
));
4242 map
= isl_map_intersect_range(map
, isl_map_wrap(ge
));
4244 return isl_map_domain(map
);
4247 /* Return the set of domain elements where "ma1" is lexicographically
4248 * greater than or equal to "ma2".
4250 __isl_give isl_set
*isl_multi_aff_lex_ge_set(__isl_take isl_multi_aff
*ma1
,
4251 __isl_take isl_multi_aff
*ma2
)
4253 return isl_multi_aff_order_set(ma1
, ma2
, &isl_map_lex_ge
);
4256 /* Return the set of domain elements where "ma1" is lexicographically
4257 * greater than "ma2".
4259 __isl_give isl_set
*isl_multi_aff_lex_gt_set(__isl_take isl_multi_aff
*ma1
,
4260 __isl_take isl_multi_aff
*ma2
)
4262 return isl_multi_aff_order_set(ma1
, ma2
, &isl_map_lex_gt
);
4265 #define isl_multi_aff_zero_in_space isl_multi_aff_zero
4268 #define PW isl_pw_multi_aff
4270 #define BASE multi_aff
4272 #define EL_IS_ZERO is_empty
4276 #define IS_ZERO is_empty
4279 #undef DEFAULT_IS_ZERO
4280 #define DEFAULT_IS_ZERO 0
4282 #include <isl_pw_templ.c>
4283 #include <isl_pw_add_constant_multi_val_templ.c>
4284 #include <isl_pw_add_constant_val_templ.c>
4285 #include <isl_pw_bind_domain_templ.c>
4286 #include <isl_pw_move_dims_templ.c>
4287 #include <isl_pw_neg_templ.c>
4288 #include <isl_pw_pullback_templ.c>
4289 #include <isl_pw_union_opt.c>
4292 #define BASE pw_multi_aff
4294 #include <isl_union_multi.c>
4295 #include <isl_union_neg.c>
4297 /* Generic function for extracting a factor from a product "pma".
4298 * "check_space" checks that the space is that of the right kind of product.
4299 * "space_factor" extracts the factor from the space.
4300 * "multi_aff_factor" extracts the factor from the constituent functions.
4302 static __isl_give isl_pw_multi_aff
*pw_multi_aff_factor(
4303 __isl_take isl_pw_multi_aff
*pma
,
4304 isl_stat (*check_space
)(__isl_keep isl_pw_multi_aff
*pma
),
4305 __isl_give isl_space
*(*space_factor
)(__isl_take isl_space
*space
),
4306 __isl_give isl_multi_aff
*(*multi_aff_factor
)(
4307 __isl_take isl_multi_aff
*ma
))
4312 if (check_space(pma
) < 0)
4313 return isl_pw_multi_aff_free(pma
);
4315 space
= isl_pw_multi_aff_take_space(pma
);
4316 space
= space_factor(space
);
4318 for (i
= 0; pma
&& i
< pma
->n
; ++i
) {
4321 ma
= isl_pw_multi_aff_take_base_at(pma
, i
);
4322 ma
= multi_aff_factor(ma
);
4323 pma
= isl_pw_multi_aff_restore_base_at(pma
, i
, ma
);
4326 pma
= isl_pw_multi_aff_restore_space(pma
, space
);
4331 /* Is the range of "pma" a wrapped relation?
4333 static isl_bool
isl_pw_multi_aff_range_is_wrapping(
4334 __isl_keep isl_pw_multi_aff
*pma
)
4336 return isl_space_range_is_wrapping(isl_pw_multi_aff_peek_space(pma
));
4339 /* Check that the range of "pma" is a product.
4341 static isl_stat
pw_multi_aff_check_range_product(
4342 __isl_keep isl_pw_multi_aff
*pma
)
4346 wraps
= isl_pw_multi_aff_range_is_wrapping(pma
);
4348 return isl_stat_error
;
4350 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
4351 "range is not a product", return isl_stat_error
);
4355 /* Given a function A -> [B -> C], extract the function A -> B.
4357 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_range_factor_domain(
4358 __isl_take isl_pw_multi_aff
*pma
)
4360 return pw_multi_aff_factor(pma
, &pw_multi_aff_check_range_product
,
4361 &isl_space_range_factor_domain
,
4362 &isl_multi_aff_range_factor_domain
);
4365 /* Given a function A -> [B -> C], extract the function A -> C.
4367 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_range_factor_range(
4368 __isl_take isl_pw_multi_aff
*pma
)
4370 return pw_multi_aff_factor(pma
, &pw_multi_aff_check_range_product
,
4371 &isl_space_range_factor_range
,
4372 &isl_multi_aff_range_factor_range
);
4375 /* Given two piecewise multi affine expressions, return a piecewise
4376 * multi-affine expression defined on the union of the definition domains
4377 * of the inputs that is equal to the lexicographic maximum of the two
4378 * inputs on each cell. If only one of the two inputs is defined on
4379 * a given cell, then it is considered to be the maximum.
4381 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_union_lexmax(
4382 __isl_take isl_pw_multi_aff
*pma1
,
4383 __isl_take isl_pw_multi_aff
*pma2
)
4385 isl_pw_multi_aff_align_params_bin(&pma1
, &pma2
);
4386 return isl_pw_multi_aff_union_opt_cmp(pma1
, pma2
,
4387 &isl_multi_aff_lex_ge_set
);
4390 /* Given two piecewise multi affine expressions, return a piecewise
4391 * multi-affine expression defined on the union of the definition domains
4392 * of the inputs that is equal to the lexicographic minimum of the two
4393 * inputs on each cell. If only one of the two inputs is defined on
4394 * a given cell, then it is considered to be the minimum.
4396 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_union_lexmin(
4397 __isl_take isl_pw_multi_aff
*pma1
,
4398 __isl_take isl_pw_multi_aff
*pma2
)
4400 isl_pw_multi_aff_align_params_bin(&pma1
, &pma2
);
4401 return isl_pw_multi_aff_union_opt_cmp(pma1
, pma2
,
4402 &isl_multi_aff_lex_le_set
);
4405 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_add(
4406 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4408 isl_pw_multi_aff_align_params_bin(&pma1
, &pma2
);
4409 return isl_pw_multi_aff_on_shared_domain(pma1
, pma2
,
4410 &isl_multi_aff_add
);
4413 /* Subtract "pma2" from "pma1" and return the result.
4415 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_sub(
4416 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4418 isl_pw_multi_aff_align_params_bin(&pma1
, &pma2
);
4419 return isl_pw_multi_aff_on_shared_domain(pma1
, pma2
,
4420 &isl_multi_aff_sub
);
4423 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_union_add(
4424 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4426 return isl_pw_multi_aff_union_add_(pma1
, pma2
);
4429 /* Compute the sum of "upa1" and "upa2" on the union of their domains,
4430 * with the actual sum on the shared domain and
4431 * the defined expression on the symmetric difference of the domains.
4433 __isl_give isl_union_pw_aff
*isl_union_pw_aff_union_add(
4434 __isl_take isl_union_pw_aff
*upa1
, __isl_take isl_union_pw_aff
*upa2
)
4436 return isl_union_pw_aff_union_add_(upa1
, upa2
);
4439 /* Compute the sum of "upma1" and "upma2" on the union of their domains,
4440 * with the actual sum on the shared domain and
4441 * the defined expression on the symmetric difference of the domains.
4443 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_union_add(
4444 __isl_take isl_union_pw_multi_aff
*upma1
,
4445 __isl_take isl_union_pw_multi_aff
*upma2
)
4447 return isl_union_pw_multi_aff_union_add_(upma1
, upma2
);
4450 /* Given two piecewise multi-affine expressions A -> B and C -> D,
4451 * construct a piecewise multi-affine expression [A -> C] -> [B -> D].
4453 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_product(
4454 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4458 isl_pw_multi_aff
*res
;
4460 if (isl_pw_multi_aff_align_params_bin(&pma1
, &pma2
) < 0)
4463 n
= pma1
->n
* pma2
->n
;
4464 space
= isl_space_product(isl_space_copy(pma1
->dim
),
4465 isl_space_copy(pma2
->dim
));
4466 res
= isl_pw_multi_aff_alloc_size(space
, n
);
4468 for (i
= 0; i
< pma1
->n
; ++i
) {
4469 for (j
= 0; j
< pma2
->n
; ++j
) {
4473 domain
= isl_set_product(isl_set_copy(pma1
->p
[i
].set
),
4474 isl_set_copy(pma2
->p
[j
].set
));
4475 ma
= isl_multi_aff_product(
4476 isl_multi_aff_copy(pma1
->p
[i
].maff
),
4477 isl_multi_aff_copy(pma2
->p
[j
].maff
));
4478 res
= isl_pw_multi_aff_add_piece(res
, domain
, ma
);
4482 isl_pw_multi_aff_free(pma1
);
4483 isl_pw_multi_aff_free(pma2
);
4486 isl_pw_multi_aff_free(pma1
);
4487 isl_pw_multi_aff_free(pma2
);
4491 /* Subtract the initial "n" elements in "ma" with coefficients in "c" and
4492 * denominator "denom".
4493 * "denom" is allowed to be negative, in which case the actual denominator
4494 * is -denom and the expressions are added instead.
4496 static __isl_give isl_aff
*subtract_initial(__isl_take isl_aff
*aff
,
4497 __isl_keep isl_multi_aff
*ma
, int n
, isl_int
*c
, isl_int denom
)
4503 first
= isl_seq_first_non_zero(c
, n
);
4507 sign
= isl_int_sgn(denom
);
4509 isl_int_abs(d
, denom
);
4510 for (i
= first
; i
< n
; ++i
) {
4513 if (isl_int_is_zero(c
[i
]))
4515 aff_i
= isl_multi_aff_get_aff(ma
, i
);
4516 aff_i
= isl_aff_scale(aff_i
, c
[i
]);
4517 aff_i
= isl_aff_scale_down(aff_i
, d
);
4519 aff
= isl_aff_sub(aff
, aff_i
);
4521 aff
= isl_aff_add(aff
, aff_i
);
4528 /* Extract an affine expression that expresses the output dimension "pos"
4529 * of "bmap" in terms of the parameters and input dimensions from
4531 * Note that this expression may involve integer divisions defined
4532 * in terms of parameters and input dimensions.
4533 * The equality may also involve references to earlier (but not later)
4534 * output dimensions. These are replaced by the corresponding elements
4537 * If the equality is of the form
4539 * f(i) + h(j) + a x + g(i) = 0,
4541 * with f(i) a linear combinations of the parameters and input dimensions,
4542 * g(i) a linear combination of integer divisions defined in terms of the same
4543 * and h(j) a linear combinations of earlier output dimensions,
4544 * then the affine expression is
4546 * (-f(i) - g(i))/a - h(j)/a
4548 * If the equality is of the form
4550 * f(i) + h(j) - a x + g(i) = 0,
4552 * then the affine expression is
4554 * (f(i) + g(i))/a - h(j)/(-a)
4557 * If "div" refers to an integer division (i.e., it is smaller than
4558 * the number of integer divisions), then the equality constraint
4559 * does involve an integer division (the one at position "div") that
4560 * is defined in terms of output dimensions. However, this integer
4561 * division can be eliminated by exploiting a pair of constraints
4562 * x >= l and x <= l + n, with n smaller than the coefficient of "div"
4563 * in the equality constraint. "ineq" refers to inequality x >= l, i.e.,
4565 * In particular, let
4567 * x = e(i) + m floor(...)
4569 * with e(i) the expression derived above and floor(...) the integer
4570 * division involving output dimensions.
4581 * e(i) + m floor(...) - l = (e(i) + m floor(...) - l) mod m
4582 * = (e(i) - l) mod m
4586 * x - l = (e(i) - l) mod m
4590 * x = ((e(i) - l) mod m) + l
4592 * The variable "shift" below contains the expression -l, which may
4593 * also involve a linear combination of earlier output dimensions.
4595 static __isl_give isl_aff
*extract_aff_from_equality(
4596 __isl_keep isl_basic_map
*bmap
, int pos
, int eq
, int div
, int ineq
,
4597 __isl_keep isl_multi_aff
*ma
)
4600 isl_size n_div
, n_out
;
4602 isl_local_space
*ls
;
4603 isl_aff
*aff
, *shift
;
4606 ctx
= isl_basic_map_get_ctx(bmap
);
4607 ls
= isl_basic_map_get_local_space(bmap
);
4608 ls
= isl_local_space_domain(ls
);
4609 aff
= isl_aff_alloc(isl_local_space_copy(ls
));
4612 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
4613 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4614 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4615 if (n_out
< 0 || n_div
< 0)
4617 if (isl_int_is_neg(bmap
->eq
[eq
][o_out
+ pos
])) {
4618 isl_seq_cpy(aff
->v
->el
+ 1, bmap
->eq
[eq
], o_out
);
4619 isl_seq_cpy(aff
->v
->el
+ 1 + o_out
,
4620 bmap
->eq
[eq
] + o_out
+ n_out
, n_div
);
4622 isl_seq_neg(aff
->v
->el
+ 1, bmap
->eq
[eq
], o_out
);
4623 isl_seq_neg(aff
->v
->el
+ 1 + o_out
,
4624 bmap
->eq
[eq
] + o_out
+ n_out
, n_div
);
4627 isl_int_set_si(aff
->v
->el
[1 + o_out
+ div
], 0);
4628 isl_int_abs(aff
->v
->el
[0], bmap
->eq
[eq
][o_out
+ pos
]);
4629 aff
= subtract_initial(aff
, ma
, pos
, bmap
->eq
[eq
] + o_out
,
4630 bmap
->eq
[eq
][o_out
+ pos
]);
4632 shift
= isl_aff_alloc(isl_local_space_copy(ls
));
4635 isl_seq_cpy(shift
->v
->el
+ 1, bmap
->ineq
[ineq
], o_out
);
4636 isl_seq_cpy(shift
->v
->el
+ 1 + o_out
,
4637 bmap
->ineq
[ineq
] + o_out
+ n_out
, n_div
);
4638 isl_int_set_si(shift
->v
->el
[0], 1);
4639 shift
= subtract_initial(shift
, ma
, pos
,
4640 bmap
->ineq
[ineq
] + o_out
, ctx
->negone
);
4641 aff
= isl_aff_add(aff
, isl_aff_copy(shift
));
4642 mod
= isl_val_int_from_isl_int(ctx
,
4643 bmap
->eq
[eq
][o_out
+ n_out
+ div
]);
4644 mod
= isl_val_abs(mod
);
4645 aff
= isl_aff_mod_val(aff
, mod
);
4646 aff
= isl_aff_sub(aff
, shift
);
4649 isl_local_space_free(ls
);
4652 isl_local_space_free(ls
);
4657 /* Given a basic map with output dimensions defined
4658 * in terms of the parameters input dimensions and earlier
4659 * output dimensions using an equality (and possibly a pair on inequalities),
4660 * extract an isl_aff that expresses output dimension "pos" in terms
4661 * of the parameters and input dimensions.
4662 * Note that this expression may involve integer divisions defined
4663 * in terms of parameters and input dimensions.
4664 * "ma" contains the expressions corresponding to earlier output dimensions.
4666 * This function shares some similarities with
4667 * isl_basic_map_has_defining_equality and isl_constraint_get_bound.
4669 static __isl_give isl_aff
*extract_isl_aff_from_basic_map(
4670 __isl_keep isl_basic_map
*bmap
, int pos
, __isl_keep isl_multi_aff
*ma
)
4677 eq
= isl_basic_map_output_defining_equality(bmap
, pos
, &div
, &ineq
);
4678 if (eq
>= bmap
->n_eq
)
4679 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
4680 "unable to find suitable equality", return NULL
);
4681 aff
= extract_aff_from_equality(bmap
, pos
, eq
, div
, ineq
, ma
);
4683 aff
= isl_aff_remove_unused_divs(aff
);
4687 /* Given a basic map where each output dimension is defined
4688 * in terms of the parameters and input dimensions using an equality,
4689 * extract an isl_multi_aff that expresses the output dimensions in terms
4690 * of the parameters and input dimensions.
4692 static __isl_give isl_multi_aff
*extract_isl_multi_aff_from_basic_map(
4693 __isl_take isl_basic_map
*bmap
)
4702 ma
= isl_multi_aff_alloc(isl_basic_map_get_space(bmap
));
4703 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4705 ma
= isl_multi_aff_free(ma
);
4707 for (i
= 0; i
< n_out
; ++i
) {
4710 aff
= extract_isl_aff_from_basic_map(bmap
, i
, ma
);
4711 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
4714 isl_basic_map_free(bmap
);
4719 /* Given a basic set where each set dimension is defined
4720 * in terms of the parameters using an equality,
4721 * extract an isl_multi_aff that expresses the set dimensions in terms
4722 * of the parameters.
4724 __isl_give isl_multi_aff
*isl_multi_aff_from_basic_set_equalities(
4725 __isl_take isl_basic_set
*bset
)
4727 return extract_isl_multi_aff_from_basic_map(bset
);
4730 /* Create an isl_pw_multi_aff that is equivalent to
4731 * isl_map_intersect_domain(isl_map_from_basic_map(bmap), domain).
4732 * The given basic map is such that each output dimension is defined
4733 * in terms of the parameters and input dimensions using an equality.
4735 * Since some applications expect the result of isl_pw_multi_aff_from_map
4736 * to only contain integer affine expressions, we compute the floor
4737 * of the expression before returning.
4739 * Remove all constraints involving local variables without
4740 * an explicit representation (resulting in the removal of those
4741 * local variables) prior to the actual extraction to ensure
4742 * that the local spaces in which the resulting affine expressions
4743 * are created do not contain any unknown local variables.
4744 * Removing such constraints is safe because constraints involving
4745 * unknown local variables are not used to determine whether
4746 * a basic map is obviously single-valued.
4748 static __isl_give isl_pw_multi_aff
*plain_pw_multi_aff_from_map(
4749 __isl_take isl_set
*domain
, __isl_take isl_basic_map
*bmap
)
4753 bmap
= isl_basic_map_drop_constraints_involving_unknown_divs(bmap
);
4754 ma
= extract_isl_multi_aff_from_basic_map(bmap
);
4755 ma
= isl_multi_aff_floor(ma
);
4756 return isl_pw_multi_aff_alloc(domain
, ma
);
4759 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4760 * This obviously only works if the input "map" is single-valued.
4761 * If so, we compute the lexicographic minimum of the image in the form
4762 * of an isl_pw_multi_aff. Since the image is unique, it is equal
4763 * to its lexicographic minimum.
4764 * If the input is not single-valued, we produce an error.
4766 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_base(
4767 __isl_take isl_map
*map
)
4771 isl_pw_multi_aff
*pma
;
4773 sv
= isl_map_is_single_valued(map
);
4777 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
4778 "map is not single-valued", goto error
);
4779 map
= isl_map_make_disjoint(map
);
4783 pma
= isl_pw_multi_aff_empty(isl_map_get_space(map
));
4785 for (i
= 0; i
< map
->n
; ++i
) {
4786 isl_pw_multi_aff
*pma_i
;
4787 isl_basic_map
*bmap
;
4788 bmap
= isl_basic_map_copy(map
->p
[i
]);
4789 pma_i
= isl_basic_map_lexmin_pw_multi_aff(bmap
);
4790 pma
= isl_pw_multi_aff_add_disjoint(pma
, pma_i
);
4800 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
4801 * taking into account that the output dimension at position "d"
4802 * can be represented as
4804 * x = floor((e(...) + c1) / m)
4806 * given that constraint "i" is of the form
4808 * e(...) + c1 - m x >= 0
4811 * Let "map" be of the form
4815 * We construct a mapping
4817 * A -> [A -> x = floor(...)]
4819 * apply that to the map, obtaining
4821 * [A -> x = floor(...)] -> B
4823 * and equate dimension "d" to x.
4824 * We then compute a isl_pw_multi_aff representation of the resulting map
4825 * and plug in the mapping above.
4827 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_div(
4828 __isl_take isl_map
*map
, __isl_take isl_basic_map
*hull
, int d
, int i
)
4831 isl_space
*space
= NULL
;
4832 isl_local_space
*ls
;
4840 isl_pw_multi_aff
*pma
;
4843 is_set
= isl_map_is_set(map
);
4847 offset
= isl_basic_map_offset(hull
, isl_dim_out
);
4848 ctx
= isl_map_get_ctx(map
);
4849 space
= isl_space_domain(isl_map_get_space(map
));
4850 n_in
= isl_space_dim(space
, isl_dim_set
);
4851 n
= isl_space_dim(space
, isl_dim_all
);
4852 if (n_in
< 0 || n
< 0)
4855 v
= isl_vec_alloc(ctx
, 1 + 1 + n
);
4857 isl_int_neg(v
->el
[0], hull
->ineq
[i
][offset
+ d
]);
4858 isl_seq_cpy(v
->el
+ 1, hull
->ineq
[i
], 1 + n
);
4860 isl_basic_map_free(hull
);
4862 ls
= isl_local_space_from_space(isl_space_copy(space
));
4863 aff
= isl_aff_alloc_vec(ls
, v
);
4864 aff
= isl_aff_floor(aff
);
4866 isl_space_free(space
);
4867 ma
= isl_multi_aff_from_aff(aff
);
4869 ma
= isl_multi_aff_identity(isl_space_map_from_set(space
));
4870 ma
= isl_multi_aff_range_product(ma
,
4871 isl_multi_aff_from_aff(aff
));
4874 insert
= isl_map_from_multi_aff_internal(isl_multi_aff_copy(ma
));
4875 map
= isl_map_apply_domain(map
, insert
);
4876 map
= isl_map_equate(map
, isl_dim_in
, n_in
, isl_dim_out
, d
);
4877 pma
= isl_pw_multi_aff_from_map(map
);
4878 pma
= isl_pw_multi_aff_pullback_multi_aff(pma
, ma
);
4882 isl_space_free(space
);
4884 isl_basic_map_free(hull
);
4888 /* Is constraint "c" of the form
4890 * e(...) + c1 - m x >= 0
4894 * -e(...) + c2 + m x >= 0
4896 * where m > 1 and e only depends on parameters and input dimemnsions?
4898 * "offset" is the offset of the output dimensions
4899 * "pos" is the position of output dimension x.
4901 static int is_potential_div_constraint(isl_int
*c
, int offset
, int d
, int total
)
4903 if (isl_int_is_zero(c
[offset
+ d
]))
4905 if (isl_int_is_one(c
[offset
+ d
]))
4907 if (isl_int_is_negone(c
[offset
+ d
]))
4909 if (isl_seq_first_non_zero(c
+ offset
, d
) != -1)
4911 if (isl_seq_first_non_zero(c
+ offset
+ d
+ 1,
4912 total
- (offset
+ d
+ 1)) != -1)
4917 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4919 * As a special case, we first check if there is any pair of constraints,
4920 * shared by all the basic maps in "map" that force a given dimension
4921 * to be equal to the floor of some affine combination of the input dimensions.
4923 * In particular, if we can find two constraints
4925 * e(...) + c1 - m x >= 0 i.e., m x <= e(...) + c1
4929 * -e(...) + c2 + m x >= 0 i.e., m x >= e(...) - c2
4931 * where m > 1 and e only depends on parameters and input dimemnsions,
4934 * c1 + c2 < m i.e., -c2 >= c1 - (m - 1)
4936 * then we know that we can take
4938 * x = floor((e(...) + c1) / m)
4940 * without having to perform any computation.
4942 * Note that we know that
4946 * If c1 + c2 were 0, then we would have detected an equality during
4947 * simplification. If c1 + c2 were negative, then we would have detected
4950 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_check_div(
4951 __isl_take isl_map
*map
)
4959 isl_basic_map
*hull
;
4961 hull
= isl_map_unshifted_simple_hull(isl_map_copy(map
));
4962 dim
= isl_map_dim(map
, isl_dim_out
);
4963 total
= isl_basic_map_dim(hull
, isl_dim_all
);
4964 if (dim
< 0 || total
< 0)
4968 offset
= isl_basic_map_offset(hull
, isl_dim_out
);
4970 for (d
= 0; d
< dim
; ++d
) {
4971 for (i
= 0; i
< n
; ++i
) {
4972 if (!is_potential_div_constraint(hull
->ineq
[i
],
4973 offset
, d
, 1 + total
))
4975 for (j
= i
+ 1; j
< n
; ++j
) {
4976 if (!isl_seq_is_neg(hull
->ineq
[i
] + 1,
4977 hull
->ineq
[j
] + 1, total
))
4979 isl_int_add(sum
, hull
->ineq
[i
][0],
4981 if (isl_int_abs_lt(sum
,
4982 hull
->ineq
[i
][offset
+ d
]))
4989 if (isl_int_is_pos(hull
->ineq
[j
][offset
+ d
]))
4991 return pw_multi_aff_from_map_div(map
, hull
, d
, j
);
4995 isl_basic_map_free(hull
);
4996 return pw_multi_aff_from_map_base(map
);
4999 isl_basic_map_free(hull
);
5003 /* Given an affine expression
5005 * [A -> B] -> f(A,B)
5007 * construct an isl_multi_aff
5011 * such that dimension "d" in B' is set to "aff" and the remaining
5012 * dimensions are set equal to the corresponding dimensions in B.
5013 * "n_in" is the dimension of the space A.
5014 * "n_out" is the dimension of the space B.
5016 * If "is_set" is set, then the affine expression is of the form
5020 * and we construct an isl_multi_aff
5024 static __isl_give isl_multi_aff
*range_map(__isl_take isl_aff
*aff
, int d
,
5025 unsigned n_in
, unsigned n_out
, int is_set
)
5029 isl_space
*space
, *space2
;
5030 isl_local_space
*ls
;
5032 space
= isl_aff_get_domain_space(aff
);
5033 ls
= isl_local_space_from_space(isl_space_copy(space
));
5034 space2
= isl_space_copy(space
);
5036 space2
= isl_space_range(isl_space_unwrap(space2
));
5037 space
= isl_space_map_from_domain_and_range(space
, space2
);
5038 ma
= isl_multi_aff_alloc(space
);
5039 ma
= isl_multi_aff_set_aff(ma
, d
, aff
);
5041 for (i
= 0; i
< n_out
; ++i
) {
5044 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
5045 isl_dim_set
, n_in
+ i
);
5046 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
5049 isl_local_space_free(ls
);
5054 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
5055 * taking into account that the dimension at position "d" can be written as
5057 * x = m a + f(..) (1)
5059 * where m is equal to "gcd".
5060 * "i" is the index of the equality in "hull" that defines f(..).
5061 * In particular, the equality is of the form
5063 * f(..) - x + m g(existentials) = 0
5067 * -f(..) + x + m g(existentials) = 0
5069 * We basically plug (1) into "map", resulting in a map with "a"
5070 * in the range instead of "x". The corresponding isl_pw_multi_aff
5071 * defining "a" is then plugged back into (1) to obtain a definition for "x".
5073 * Specifically, given the input map
5077 * We first wrap it into a set
5081 * and define (1) on top of the corresponding space, resulting in "aff".
5082 * We use this to create an isl_multi_aff that maps the output position "d"
5083 * from "a" to "x", leaving all other (intput and output) dimensions unchanged.
5084 * We plug this into the wrapped map, unwrap the result and compute the
5085 * corresponding isl_pw_multi_aff.
5086 * The result is an expression
5094 * so that we can plug that into "aff", after extending the latter to
5100 * If "map" is actually a set, then there is no "A" space, meaning
5101 * that we do not need to perform any wrapping, and that the result
5102 * of the recursive call is of the form
5106 * which is plugged into a mapping of the form
5110 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_stride(
5111 __isl_take isl_map
*map
, __isl_take isl_basic_map
*hull
, int d
, int i
,
5116 isl_local_space
*ls
;
5119 isl_pw_multi_aff
*pma
, *id
;
5125 is_set
= isl_map_is_set(map
);
5129 n_in
= isl_basic_map_dim(hull
, isl_dim_in
);
5130 n_out
= isl_basic_map_dim(hull
, isl_dim_out
);
5131 if (n_in
< 0 || n_out
< 0)
5133 o_out
= isl_basic_map_offset(hull
, isl_dim_out
);
5138 set
= isl_map_wrap(map
);
5139 space
= isl_space_map_from_set(isl_set_get_space(set
));
5140 ma
= isl_multi_aff_identity(space
);
5141 ls
= isl_local_space_from_space(isl_set_get_space(set
));
5142 aff
= isl_aff_alloc(ls
);
5144 isl_int_set_si(aff
->v
->el
[0], 1);
5145 if (isl_int_is_one(hull
->eq
[i
][o_out
+ d
]))
5146 isl_seq_neg(aff
->v
->el
+ 1, hull
->eq
[i
],
5149 isl_seq_cpy(aff
->v
->el
+ 1, hull
->eq
[i
],
5151 isl_int_set(aff
->v
->el
[1 + o_out
+ d
], gcd
);
5153 ma
= isl_multi_aff_set_aff(ma
, n_in
+ d
, isl_aff_copy(aff
));
5154 set
= isl_set_preimage_multi_aff(set
, ma
);
5156 ma
= range_map(aff
, d
, n_in
, n_out
, is_set
);
5161 map
= isl_set_unwrap(set
);
5162 pma
= isl_pw_multi_aff_from_map(map
);
5165 space
= isl_pw_multi_aff_get_domain_space(pma
);
5166 space
= isl_space_map_from_set(space
);
5167 id
= isl_pw_multi_aff_identity(space
);
5168 pma
= isl_pw_multi_aff_range_product(id
, pma
);
5170 id
= isl_pw_multi_aff_from_multi_aff(ma
);
5171 pma
= isl_pw_multi_aff_pullback_pw_multi_aff(id
, pma
);
5173 isl_basic_map_free(hull
);
5177 isl_basic_map_free(hull
);
5181 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5182 * "hull" contains the equalities valid for "map".
5184 * Check if any of the output dimensions is "strided".
5185 * That is, we check if it can be written as
5189 * with m greater than 1, a some combination of existentially quantified
5190 * variables and f an expression in the parameters and input dimensions.
5191 * If so, we remove the stride in pw_multi_aff_from_map_stride.
5193 * Otherwise, we continue with pw_multi_aff_from_map_check_div for a further
5196 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_check_strides(
5197 __isl_take isl_map
*map
, __isl_take isl_basic_map
*hull
)
5206 n_div
= isl_basic_map_dim(hull
, isl_dim_div
);
5207 n_out
= isl_basic_map_dim(hull
, isl_dim_out
);
5208 if (n_div
< 0 || n_out
< 0)
5212 isl_basic_map_free(hull
);
5213 return pw_multi_aff_from_map_check_div(map
);
5218 o_div
= isl_basic_map_offset(hull
, isl_dim_div
);
5219 o_out
= isl_basic_map_offset(hull
, isl_dim_out
);
5221 for (i
= 0; i
< n_out
; ++i
) {
5222 for (j
= 0; j
< hull
->n_eq
; ++j
) {
5223 isl_int
*eq
= hull
->eq
[j
];
5224 isl_pw_multi_aff
*res
;
5226 if (!isl_int_is_one(eq
[o_out
+ i
]) &&
5227 !isl_int_is_negone(eq
[o_out
+ i
]))
5229 if (isl_seq_first_non_zero(eq
+ o_out
, i
) != -1)
5231 if (isl_seq_first_non_zero(eq
+ o_out
+ i
+ 1,
5232 n_out
- (i
+ 1)) != -1)
5234 isl_seq_gcd(eq
+ o_div
, n_div
, &gcd
);
5235 if (isl_int_is_zero(gcd
))
5237 if (isl_int_is_one(gcd
))
5240 res
= pw_multi_aff_from_map_stride(map
, hull
,
5248 isl_basic_map_free(hull
);
5249 return pw_multi_aff_from_map_check_div(map
);
5252 isl_basic_map_free(hull
);
5256 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5258 * As a special case, we first check if all output dimensions are uniquely
5259 * defined in terms of the parameters and input dimensions over the entire
5260 * domain. If so, we extract the desired isl_pw_multi_aff directly
5261 * from the affine hull of "map" and its domain.
5263 * Otherwise, continue with pw_multi_aff_from_map_check_strides for more
5266 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_map(__isl_take isl_map
*map
)
5270 isl_basic_map
*hull
;
5272 n
= isl_map_n_basic_map(map
);
5277 hull
= isl_map_unshifted_simple_hull(isl_map_copy(map
));
5278 hull
= isl_basic_map_plain_affine_hull(hull
);
5279 sv
= isl_basic_map_plain_is_single_valued(hull
);
5281 return plain_pw_multi_aff_from_map(isl_map_domain(map
),
5283 isl_basic_map_free(hull
);
5285 map
= isl_map_detect_equalities(map
);
5286 hull
= isl_map_unshifted_simple_hull(isl_map_copy(map
));
5287 sv
= isl_basic_map_plain_is_single_valued(hull
);
5289 return plain_pw_multi_aff_from_map(isl_map_domain(map
), hull
);
5291 return pw_multi_aff_from_map_check_strides(map
, hull
);
5292 isl_basic_map_free(hull
);
5298 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_set(__isl_take isl_set
*set
)
5300 return isl_pw_multi_aff_from_map(set
);
5303 /* Convert "map" into an isl_pw_multi_aff (if possible) and
5306 static isl_stat
pw_multi_aff_from_map(__isl_take isl_map
*map
, void *user
)
5308 isl_union_pw_multi_aff
**upma
= user
;
5309 isl_pw_multi_aff
*pma
;
5311 pma
= isl_pw_multi_aff_from_map(map
);
5312 *upma
= isl_union_pw_multi_aff_add_pw_multi_aff(*upma
, pma
);
5314 return *upma
? isl_stat_ok
: isl_stat_error
;
5317 /* Create an isl_union_pw_multi_aff with the given isl_aff on a universe
5320 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_aff(
5321 __isl_take isl_aff
*aff
)
5324 isl_pw_multi_aff
*pma
;
5326 ma
= isl_multi_aff_from_aff(aff
);
5327 pma
= isl_pw_multi_aff_from_multi_aff(ma
);
5328 return isl_union_pw_multi_aff_from_pw_multi_aff(pma
);
5331 /* Try and create an isl_union_pw_multi_aff that is equivalent
5332 * to the given isl_union_map.
5333 * The isl_union_map is required to be single-valued in each space.
5334 * Otherwise, an error is produced.
5336 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_union_map(
5337 __isl_take isl_union_map
*umap
)
5340 isl_union_pw_multi_aff
*upma
;
5342 space
= isl_union_map_get_space(umap
);
5343 upma
= isl_union_pw_multi_aff_empty(space
);
5344 if (isl_union_map_foreach_map(umap
, &pw_multi_aff_from_map
, &upma
) < 0)
5345 upma
= isl_union_pw_multi_aff_free(upma
);
5346 isl_union_map_free(umap
);
5351 /* Try and create an isl_union_pw_multi_aff that is equivalent
5352 * to the given isl_union_set.
5353 * The isl_union_set is required to be a singleton in each space.
5354 * Otherwise, an error is produced.
5356 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_union_set(
5357 __isl_take isl_union_set
*uset
)
5359 return isl_union_pw_multi_aff_from_union_map(uset
);
5362 /* Return the piecewise affine expression "set ? 1 : 0".
5364 __isl_give isl_pw_aff
*isl_set_indicator_function(__isl_take isl_set
*set
)
5367 isl_space
*space
= isl_set_get_space(set
);
5368 isl_local_space
*ls
= isl_local_space_from_space(space
);
5369 isl_aff
*zero
= isl_aff_zero_on_domain(isl_local_space_copy(ls
));
5370 isl_aff
*one
= isl_aff_zero_on_domain(ls
);
5372 one
= isl_aff_add_constant_si(one
, 1);
5373 pa
= isl_pw_aff_alloc(isl_set_copy(set
), one
);
5374 set
= isl_set_complement(set
);
5375 pa
= isl_pw_aff_add_disjoint(pa
, isl_pw_aff_alloc(set
, zero
));
5380 /* Plug in "subs" for dimension "type", "pos" of "aff".
5382 * Let i be the dimension to replace and let "subs" be of the form
5386 * and "aff" of the form
5392 * (a f + d g')/(m d)
5394 * where g' is the result of plugging in "subs" in each of the integer
5397 __isl_give isl_aff
*isl_aff_substitute(__isl_take isl_aff
*aff
,
5398 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
5404 aff
= isl_aff_cow(aff
);
5406 return isl_aff_free(aff
);
5408 ctx
= isl_aff_get_ctx(aff
);
5409 if (!isl_space_is_equal(aff
->ls
->dim
, subs
->ls
->dim
))
5410 isl_die(ctx
, isl_error_invalid
,
5411 "spaces don't match", return isl_aff_free(aff
));
5412 n_div
= isl_aff_domain_dim(subs
, isl_dim_div
);
5414 return isl_aff_free(aff
);
5416 isl_die(ctx
, isl_error_unsupported
,
5417 "cannot handle divs yet", return isl_aff_free(aff
));
5419 aff
->ls
= isl_local_space_substitute(aff
->ls
, type
, pos
, subs
);
5421 return isl_aff_free(aff
);
5423 aff
->v
= isl_vec_cow(aff
->v
);
5425 return isl_aff_free(aff
);
5427 pos
+= isl_local_space_offset(aff
->ls
, type
);
5430 isl_seq_substitute(aff
->v
->el
, pos
, subs
->v
->el
,
5431 aff
->v
->size
, subs
->v
->size
, v
);
5437 /* Plug in "subs" for dimension "type", "pos" in each of the affine
5438 * expressions in "maff".
5440 __isl_give isl_multi_aff
*isl_multi_aff_substitute(
5441 __isl_take isl_multi_aff
*maff
, enum isl_dim_type type
, unsigned pos
,
5442 __isl_keep isl_aff
*subs
)
5446 maff
= isl_multi_aff_cow(maff
);
5448 return isl_multi_aff_free(maff
);
5450 if (type
== isl_dim_in
)
5453 for (i
= 0; i
< maff
->n
; ++i
) {
5454 maff
->u
.p
[i
] = isl_aff_substitute(maff
->u
.p
[i
],
5457 return isl_multi_aff_free(maff
);
5463 /* Plug in "subs" for dimension "type", "pos" of "pma".
5465 * pma is of the form
5469 * while subs is of the form
5471 * v' = B_j(v) -> S_j
5473 * Each pair i,j such that C_ij = A_i \cap B_i is non-empty
5474 * has a contribution in the result, in particular
5476 * C_ij(S_j) -> M_i(S_j)
5478 * Note that plugging in S_j in C_ij may also result in an empty set
5479 * and this contribution should simply be discarded.
5481 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_substitute(
5482 __isl_take isl_pw_multi_aff
*pma
, enum isl_dim_type type
, unsigned pos
,
5483 __isl_keep isl_pw_aff
*subs
)
5486 isl_pw_multi_aff
*res
;
5489 return isl_pw_multi_aff_free(pma
);
5491 n
= pma
->n
* subs
->n
;
5492 res
= isl_pw_multi_aff_alloc_size(isl_space_copy(pma
->dim
), n
);
5494 for (i
= 0; i
< pma
->n
; ++i
) {
5495 for (j
= 0; j
< subs
->n
; ++j
) {
5497 isl_multi_aff
*res_ij
;
5500 common
= isl_set_intersect(
5501 isl_set_copy(pma
->p
[i
].set
),
5502 isl_set_copy(subs
->p
[j
].set
));
5503 common
= isl_set_substitute(common
,
5504 type
, pos
, subs
->p
[j
].aff
);
5505 empty
= isl_set_plain_is_empty(common
);
5506 if (empty
< 0 || empty
) {
5507 isl_set_free(common
);
5513 res_ij
= isl_multi_aff_substitute(
5514 isl_multi_aff_copy(pma
->p
[i
].maff
),
5515 type
, pos
, subs
->p
[j
].aff
);
5517 res
= isl_pw_multi_aff_add_piece(res
, common
, res_ij
);
5521 isl_pw_multi_aff_free(pma
);
5524 isl_pw_multi_aff_free(pma
);
5525 isl_pw_multi_aff_free(res
);
5529 /* Compute the preimage of a range of dimensions in the affine expression "src"
5530 * under "ma" and put the result in "dst". The number of dimensions in "src"
5531 * that precede the range is given by "n_before". The number of dimensions
5532 * in the range is given by the number of output dimensions of "ma".
5533 * The number of dimensions that follow the range is given by "n_after".
5534 * If "has_denom" is set (to one),
5535 * then "src" and "dst" have an extra initial denominator.
5536 * "n_div_ma" is the number of existentials in "ma"
5537 * "n_div_bset" is the number of existentials in "src"
5538 * The resulting "dst" (which is assumed to have been allocated by
5539 * the caller) contains coefficients for both sets of existentials,
5540 * first those in "ma" and then those in "src".
5541 * f, c1, c2 and g are temporary objects that have been initialized
5544 * Let src represent the expression
5546 * (a(p) + f_u u + b v + f_w w + c(divs))/d
5548 * and let ma represent the expressions
5550 * v_i = (r_i(p) + s_i(y) + t_i(divs'))/m_i
5552 * We start out with the following expression for dst:
5554 * (a(p) + f_u u + 0 y + f_w w + 0 divs' + c(divs) + f \sum_i b_i v_i)/d
5556 * with the multiplication factor f initially equal to 1
5557 * and f \sum_i b_i v_i kept separately.
5558 * For each x_i that we substitute, we multiply the numerator
5559 * (and denominator) of dst by c_1 = m_i and add the numerator
5560 * of the x_i expression multiplied by c_2 = f b_i,
5561 * after removing the common factors of c_1 and c_2.
5562 * The multiplication factor f also needs to be multiplied by c_1
5563 * for the next x_j, j > i.
5565 isl_stat
isl_seq_preimage(isl_int
*dst
, isl_int
*src
,
5566 __isl_keep isl_multi_aff
*ma
, int n_before
, int n_after
,
5567 int n_div_ma
, int n_div_bmap
,
5568 isl_int f
, isl_int c1
, isl_int c2
, isl_int g
, int has_denom
)
5571 isl_size n_param
, n_in
, n_out
;
5574 n_param
= isl_multi_aff_dim(ma
, isl_dim_param
);
5575 n_in
= isl_multi_aff_dim(ma
, isl_dim_in
);
5576 n_out
= isl_multi_aff_dim(ma
, isl_dim_out
);
5577 if (n_param
< 0 || n_in
< 0 || n_out
< 0)
5578 return isl_stat_error
;
5580 isl_seq_cpy(dst
, src
, has_denom
+ 1 + n_param
+ n_before
);
5581 o_dst
= o_src
= has_denom
+ 1 + n_param
+ n_before
;
5582 isl_seq_clr(dst
+ o_dst
, n_in
);
5585 isl_seq_cpy(dst
+ o_dst
, src
+ o_src
, n_after
);
5588 isl_seq_clr(dst
+ o_dst
, n_div_ma
);
5590 isl_seq_cpy(dst
+ o_dst
, src
+ o_src
, n_div_bmap
);
5592 isl_int_set_si(f
, 1);
5594 for (i
= 0; i
< n_out
; ++i
) {
5595 int offset
= has_denom
+ 1 + n_param
+ n_before
+ i
;
5597 if (isl_int_is_zero(src
[offset
]))
5599 isl_int_set(c1
, ma
->u
.p
[i
]->v
->el
[0]);
5600 isl_int_mul(c2
, f
, src
[offset
]);
5601 isl_int_gcd(g
, c1
, c2
);
5602 isl_int_divexact(c1
, c1
, g
);
5603 isl_int_divexact(c2
, c2
, g
);
5605 isl_int_mul(f
, f
, c1
);
5608 isl_seq_combine(dst
+ o_dst
, c1
, dst
+ o_dst
,
5609 c2
, ma
->u
.p
[i
]->v
->el
+ o_src
, 1 + n_param
);
5610 o_dst
+= 1 + n_param
;
5611 o_src
+= 1 + n_param
;
5612 isl_seq_scale(dst
+ o_dst
, dst
+ o_dst
, c1
, n_before
);
5614 isl_seq_combine(dst
+ o_dst
, c1
, dst
+ o_dst
,
5615 c2
, ma
->u
.p
[i
]->v
->el
+ o_src
, n_in
);
5618 isl_seq_scale(dst
+ o_dst
, dst
+ o_dst
, c1
, n_after
);
5620 isl_seq_combine(dst
+ o_dst
, c1
, dst
+ o_dst
,
5621 c2
, ma
->u
.p
[i
]->v
->el
+ o_src
, n_div_ma
);
5624 isl_seq_scale(dst
+ o_dst
, dst
+ o_dst
, c1
, n_div_bmap
);
5626 isl_int_mul(dst
[0], dst
[0], c1
);
5632 /* Compute the pullback of "aff" by the function represented by "ma".
5633 * In other words, plug in "ma" in "aff". The result is an affine expression
5634 * defined over the domain space of "ma".
5636 * If "aff" is represented by
5638 * (a(p) + b x + c(divs))/d
5640 * and ma is represented by
5642 * x = D(p) + F(y) + G(divs')
5644 * then the result is
5646 * (a(p) + b D(p) + b F(y) + b G(divs') + c(divs))/d
5648 * The divs in the local space of the input are similarly adjusted
5649 * through a call to isl_local_space_preimage_multi_aff.
5651 __isl_give isl_aff
*isl_aff_pullback_multi_aff(__isl_take isl_aff
*aff
,
5652 __isl_take isl_multi_aff
*ma
)
5654 isl_aff
*res
= NULL
;
5655 isl_local_space
*ls
;
5656 isl_size n_div_aff
, n_div_ma
;
5657 isl_int f
, c1
, c2
, g
;
5659 ma
= isl_multi_aff_align_divs(ma
);
5663 n_div_aff
= isl_aff_dim(aff
, isl_dim_div
);
5664 n_div_ma
= ma
->n
? isl_aff_dim(ma
->u
.p
[0], isl_dim_div
) : 0;
5665 if (n_div_aff
< 0 || n_div_ma
< 0)
5668 ls
= isl_aff_get_domain_local_space(aff
);
5669 ls
= isl_local_space_preimage_multi_aff(ls
, isl_multi_aff_copy(ma
));
5670 res
= isl_aff_alloc(ls
);
5679 if (isl_seq_preimage(res
->v
->el
, aff
->v
->el
, ma
, 0, 0,
5680 n_div_ma
, n_div_aff
, f
, c1
, c2
, g
, 1) < 0)
5681 res
= isl_aff_free(res
);
5689 isl_multi_aff_free(ma
);
5690 res
= isl_aff_normalize(res
);
5694 isl_multi_aff_free(ma
);
5699 /* Compute the pullback of "aff1" by the function represented by "aff2".
5700 * In other words, plug in "aff2" in "aff1". The result is an affine expression
5701 * defined over the domain space of "aff1".
5703 * The domain of "aff1" should match the range of "aff2", which means
5704 * that it should be single-dimensional.
5706 __isl_give isl_aff
*isl_aff_pullback_aff(__isl_take isl_aff
*aff1
,
5707 __isl_take isl_aff
*aff2
)
5711 ma
= isl_multi_aff_from_aff(aff2
);
5712 return isl_aff_pullback_multi_aff(aff1
, ma
);
5715 /* Compute the pullback of "ma1" by the function represented by "ma2".
5716 * In other words, plug in "ma2" in "ma1".
5718 __isl_give isl_multi_aff
*isl_multi_aff_pullback_multi_aff(
5719 __isl_take isl_multi_aff
*ma1
, __isl_take isl_multi_aff
*ma2
)
5722 isl_space
*space
= NULL
;
5724 isl_multi_aff_align_params_bin(&ma1
, &ma2
);
5725 ma2
= isl_multi_aff_align_divs(ma2
);
5726 ma1
= isl_multi_aff_cow(ma1
);
5730 space
= isl_space_join(isl_multi_aff_get_space(ma2
),
5731 isl_multi_aff_get_space(ma1
));
5733 for (i
= 0; i
< ma1
->n
; ++i
) {
5734 ma1
->u
.p
[i
] = isl_aff_pullback_multi_aff(ma1
->u
.p
[i
],
5735 isl_multi_aff_copy(ma2
));
5740 ma1
= isl_multi_aff_reset_space(ma1
, space
);
5741 isl_multi_aff_free(ma2
);
5744 isl_space_free(space
);
5745 isl_multi_aff_free(ma2
);
5746 isl_multi_aff_free(ma1
);
5750 /* Extend the local space of "dst" to include the divs
5751 * in the local space of "src".
5753 * If "src" does not have any divs or if the local spaces of "dst" and
5754 * "src" are the same, then no extension is required.
5756 __isl_give isl_aff
*isl_aff_align_divs(__isl_take isl_aff
*dst
,
5757 __isl_keep isl_aff
*src
)
5760 isl_size src_n_div
, dst_n_div
;
5767 return isl_aff_free(dst
);
5769 ctx
= isl_aff_get_ctx(src
);
5770 equal
= isl_local_space_has_equal_space(src
->ls
, dst
->ls
);
5772 return isl_aff_free(dst
);
5774 isl_die(ctx
, isl_error_invalid
,
5775 "spaces don't match", goto error
);
5777 src_n_div
= isl_aff_domain_dim(src
, isl_dim_div
);
5778 dst_n_div
= isl_aff_domain_dim(dst
, isl_dim_div
);
5781 equal
= isl_local_space_is_equal(src
->ls
, dst
->ls
);
5782 if (equal
< 0 || src_n_div
< 0 || dst_n_div
< 0)
5783 return isl_aff_free(dst
);
5787 exp1
= isl_alloc_array(ctx
, int, src_n_div
);
5788 exp2
= isl_alloc_array(ctx
, int, dst_n_div
);
5789 if (!exp1
|| (dst_n_div
&& !exp2
))
5792 div
= isl_merge_divs(src
->ls
->div
, dst
->ls
->div
, exp1
, exp2
);
5793 dst
= isl_aff_expand_divs(dst
, div
, exp2
);
5801 return isl_aff_free(dst
);
5804 /* Adjust the local spaces of the affine expressions in "maff"
5805 * such that they all have the save divs.
5807 __isl_give isl_multi_aff
*isl_multi_aff_align_divs(
5808 __isl_take isl_multi_aff
*maff
)
5816 maff
= isl_multi_aff_cow(maff
);
5820 for (i
= 1; i
< maff
->n
; ++i
)
5821 maff
->u
.p
[0] = isl_aff_align_divs(maff
->u
.p
[0], maff
->u
.p
[i
]);
5822 for (i
= 1; i
< maff
->n
; ++i
) {
5823 maff
->u
.p
[i
] = isl_aff_align_divs(maff
->u
.p
[i
], maff
->u
.p
[0]);
5825 return isl_multi_aff_free(maff
);
5831 __isl_give isl_aff
*isl_aff_lift(__isl_take isl_aff
*aff
)
5833 aff
= isl_aff_cow(aff
);
5837 aff
->ls
= isl_local_space_lift(aff
->ls
);
5839 return isl_aff_free(aff
);
5844 /* Lift "maff" to a space with extra dimensions such that the result
5845 * has no more existentially quantified variables.
5846 * If "ls" is not NULL, then *ls is assigned the local space that lies
5847 * at the basis of the lifting applied to "maff".
5849 __isl_give isl_multi_aff
*isl_multi_aff_lift(__isl_take isl_multi_aff
*maff
,
5850 __isl_give isl_local_space
**ls
)
5864 isl_space
*space
= isl_multi_aff_get_domain_space(maff
);
5865 *ls
= isl_local_space_from_space(space
);
5867 return isl_multi_aff_free(maff
);
5872 maff
= isl_multi_aff_cow(maff
);
5873 maff
= isl_multi_aff_align_divs(maff
);
5877 n_div
= isl_aff_dim(maff
->u
.p
[0], isl_dim_div
);
5879 return isl_multi_aff_free(maff
);
5880 space
= isl_multi_aff_get_space(maff
);
5881 space
= isl_space_lift(isl_space_domain(space
), n_div
);
5882 space
= isl_space_extend_domain_with_range(space
,
5883 isl_multi_aff_get_space(maff
));
5885 return isl_multi_aff_free(maff
);
5886 isl_space_free(maff
->space
);
5887 maff
->space
= space
;
5890 *ls
= isl_aff_get_domain_local_space(maff
->u
.p
[0]);
5892 return isl_multi_aff_free(maff
);
5895 for (i
= 0; i
< maff
->n
; ++i
) {
5896 maff
->u
.p
[i
] = isl_aff_lift(maff
->u
.p
[i
]);
5904 isl_local_space_free(*ls
);
5905 return isl_multi_aff_free(maff
);
5909 #define TYPE isl_pw_multi_aff
5911 #include "check_type_range_templ.c"
5913 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma".
5915 __isl_give isl_pw_aff
*isl_pw_multi_aff_get_pw_aff(
5916 __isl_keep isl_pw_multi_aff
*pma
, int pos
)
5923 if (isl_pw_multi_aff_check_range(pma
, isl_dim_out
, pos
, 1) < 0)
5926 n_out
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
5930 space
= isl_pw_multi_aff_get_space(pma
);
5931 space
= isl_space_drop_dims(space
, isl_dim_out
,
5932 pos
+ 1, n_out
- pos
- 1);
5933 space
= isl_space_drop_dims(space
, isl_dim_out
, 0, pos
);
5935 pa
= isl_pw_aff_alloc_size(space
, pma
->n
);
5936 for (i
= 0; i
< pma
->n
; ++i
) {
5938 aff
= isl_multi_aff_get_aff(pma
->p
[i
].maff
, pos
);
5939 pa
= isl_pw_aff_add_piece(pa
, isl_set_copy(pma
->p
[i
].set
), aff
);
5945 /* Return an isl_pw_multi_aff with the given "set" as domain and
5946 * an unnamed zero-dimensional range.
5948 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_domain(
5949 __isl_take isl_set
*set
)
5954 space
= isl_set_get_space(set
);
5955 space
= isl_space_from_domain(space
);
5956 ma
= isl_multi_aff_zero(space
);
5957 return isl_pw_multi_aff_alloc(set
, ma
);
5960 /* Add an isl_pw_multi_aff with the given "set" as domain and
5961 * an unnamed zero-dimensional range to *user.
5963 static isl_stat
add_pw_multi_aff_from_domain(__isl_take isl_set
*set
,
5966 isl_union_pw_multi_aff
**upma
= user
;
5967 isl_pw_multi_aff
*pma
;
5969 pma
= isl_pw_multi_aff_from_domain(set
);
5970 *upma
= isl_union_pw_multi_aff_add_pw_multi_aff(*upma
, pma
);
5975 /* Return an isl_union_pw_multi_aff with the given "uset" as domain and
5976 * an unnamed zero-dimensional range.
5978 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_domain(
5979 __isl_take isl_union_set
*uset
)
5982 isl_union_pw_multi_aff
*upma
;
5987 space
= isl_union_set_get_space(uset
);
5988 upma
= isl_union_pw_multi_aff_empty(space
);
5990 if (isl_union_set_foreach_set(uset
,
5991 &add_pw_multi_aff_from_domain
, &upma
) < 0)
5994 isl_union_set_free(uset
);
5997 isl_union_set_free(uset
);
5998 isl_union_pw_multi_aff_free(upma
);
6002 /* Local data for bin_entry and the callback "fn".
6004 struct isl_union_pw_multi_aff_bin_data
{
6005 isl_union_pw_multi_aff
*upma2
;
6006 isl_union_pw_multi_aff
*res
;
6007 isl_pw_multi_aff
*pma
;
6008 isl_stat (*fn
)(__isl_take isl_pw_multi_aff
*pma
, void *user
);
6011 /* Given an isl_pw_multi_aff from upma1, store it in data->pma
6012 * and call data->fn for each isl_pw_multi_aff in data->upma2.
6014 static isl_stat
bin_entry(__isl_take isl_pw_multi_aff
*pma
, void *user
)
6016 struct isl_union_pw_multi_aff_bin_data
*data
= user
;
6020 r
= isl_union_pw_multi_aff_foreach_pw_multi_aff(data
->upma2
,
6022 isl_pw_multi_aff_free(pma
);
6027 /* Call "fn" on each pair of isl_pw_multi_affs in "upma1" and "upma2".
6028 * The isl_pw_multi_aff from upma1 is stored in data->pma (where data is
6029 * passed as user field) and the isl_pw_multi_aff from upma2 is available
6030 * as *entry. The callback should adjust data->res if desired.
6032 static __isl_give isl_union_pw_multi_aff
*bin_op(
6033 __isl_take isl_union_pw_multi_aff
*upma1
,
6034 __isl_take isl_union_pw_multi_aff
*upma2
,
6035 isl_stat (*fn
)(__isl_take isl_pw_multi_aff
*pma
, void *user
))
6038 struct isl_union_pw_multi_aff_bin_data data
= { NULL
, NULL
, NULL
, fn
};
6040 space
= isl_union_pw_multi_aff_get_space(upma2
);
6041 upma1
= isl_union_pw_multi_aff_align_params(upma1
, space
);
6042 space
= isl_union_pw_multi_aff_get_space(upma1
);
6043 upma2
= isl_union_pw_multi_aff_align_params(upma2
, space
);
6045 if (!upma1
|| !upma2
)
6049 data
.res
= isl_union_pw_multi_aff_alloc_same_size(upma1
);
6050 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma1
,
6051 &bin_entry
, &data
) < 0)
6054 isl_union_pw_multi_aff_free(upma1
);
6055 isl_union_pw_multi_aff_free(upma2
);
6058 isl_union_pw_multi_aff_free(upma1
);
6059 isl_union_pw_multi_aff_free(upma2
);
6060 isl_union_pw_multi_aff_free(data
.res
);
6064 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
6065 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
6067 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_range_product(
6068 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
6072 isl_pw_multi_aff_align_params_bin(&pma1
, &pma2
);
6073 space
= isl_space_range_product(isl_pw_multi_aff_get_space(pma1
),
6074 isl_pw_multi_aff_get_space(pma2
));
6075 return isl_pw_multi_aff_on_shared_domain_in(pma1
, pma2
, space
,
6076 &isl_multi_aff_range_product
);
6079 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
6080 * construct an isl_pw_multi_aff (A * C) -> (B, D).
6082 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_flat_range_product(
6083 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
6087 isl_pw_multi_aff_align_params_bin(&pma1
, &pma2
);
6088 space
= isl_space_range_product(isl_pw_multi_aff_get_space(pma1
),
6089 isl_pw_multi_aff_get_space(pma2
));
6090 space
= isl_space_flatten_range(space
);
6091 return isl_pw_multi_aff_on_shared_domain_in(pma1
, pma2
, space
,
6092 &isl_multi_aff_flat_range_product
);
6095 /* If data->pma and "pma2" have the same domain space, then compute
6096 * their flat range product and the result to data->res.
6098 static isl_stat
flat_range_product_entry(__isl_take isl_pw_multi_aff
*pma2
,
6101 struct isl_union_pw_multi_aff_bin_data
*data
= user
;
6103 if (!isl_space_tuple_is_equal(data
->pma
->dim
, isl_dim_in
,
6104 pma2
->dim
, isl_dim_in
)) {
6105 isl_pw_multi_aff_free(pma2
);
6109 pma2
= isl_pw_multi_aff_flat_range_product(
6110 isl_pw_multi_aff_copy(data
->pma
), pma2
);
6112 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma2
);
6117 /* Given two isl_union_pw_multi_affs A -> B and C -> D,
6118 * construct an isl_union_pw_multi_aff (A * C) -> (B, D).
6120 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_flat_range_product(
6121 __isl_take isl_union_pw_multi_aff
*upma1
,
6122 __isl_take isl_union_pw_multi_aff
*upma2
)
6124 return bin_op(upma1
, upma2
, &flat_range_product_entry
);
6127 /* Replace the affine expressions at position "pos" in "pma" by "pa".
6128 * The parameters are assumed to have been aligned.
6130 * The implementation essentially performs an isl_pw_*_on_shared_domain,
6131 * except that it works on two different isl_pw_* types.
6133 static __isl_give isl_pw_multi_aff
*pw_multi_aff_set_pw_aff(
6134 __isl_take isl_pw_multi_aff
*pma
, unsigned pos
,
6135 __isl_take isl_pw_aff
*pa
)
6138 isl_pw_multi_aff
*res
= NULL
;
6143 if (!isl_space_tuple_is_equal(pma
->dim
, isl_dim_in
,
6144 pa
->dim
, isl_dim_in
))
6145 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
6146 "domains don't match", goto error
);
6147 if (isl_pw_multi_aff_check_range(pma
, isl_dim_out
, pos
, 1) < 0)
6151 res
= isl_pw_multi_aff_alloc_size(isl_pw_multi_aff_get_space(pma
), n
);
6153 for (i
= 0; i
< pma
->n
; ++i
) {
6154 for (j
= 0; j
< pa
->n
; ++j
) {
6156 isl_multi_aff
*res_ij
;
6159 common
= isl_set_intersect(isl_set_copy(pma
->p
[i
].set
),
6160 isl_set_copy(pa
->p
[j
].set
));
6161 empty
= isl_set_plain_is_empty(common
);
6162 if (empty
< 0 || empty
) {
6163 isl_set_free(common
);
6169 res_ij
= isl_multi_aff_set_aff(
6170 isl_multi_aff_copy(pma
->p
[i
].maff
), pos
,
6171 isl_aff_copy(pa
->p
[j
].aff
));
6172 res_ij
= isl_multi_aff_gist(res_ij
,
6173 isl_set_copy(common
));
6175 res
= isl_pw_multi_aff_add_piece(res
, common
, res_ij
);
6179 isl_pw_multi_aff_free(pma
);
6180 isl_pw_aff_free(pa
);
6183 isl_pw_multi_aff_free(pma
);
6184 isl_pw_aff_free(pa
);
6185 return isl_pw_multi_aff_free(res
);
6188 /* Replace the affine expressions at position "pos" in "pma" by "pa".
6190 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_set_pw_aff(
6191 __isl_take isl_pw_multi_aff
*pma
, unsigned pos
,
6192 __isl_take isl_pw_aff
*pa
)
6194 isl_bool equal_params
;
6198 equal_params
= isl_space_has_equal_params(pma
->dim
, pa
->dim
);
6199 if (equal_params
< 0)
6202 return pw_multi_aff_set_pw_aff(pma
, pos
, pa
);
6203 if (isl_pw_multi_aff_check_named_params(pma
) < 0 ||
6204 isl_pw_aff_check_named_params(pa
) < 0)
6206 pma
= isl_pw_multi_aff_align_params(pma
, isl_pw_aff_get_space(pa
));
6207 pa
= isl_pw_aff_align_params(pa
, isl_pw_multi_aff_get_space(pma
));
6208 return pw_multi_aff_set_pw_aff(pma
, pos
, pa
);
6210 isl_pw_multi_aff_free(pma
);
6211 isl_pw_aff_free(pa
);
6215 /* Do the parameters of "pa" match those of "space"?
6217 isl_bool
isl_pw_aff_matching_params(__isl_keep isl_pw_aff
*pa
,
6218 __isl_keep isl_space
*space
)
6220 isl_space
*pa_space
;
6224 return isl_bool_error
;
6226 pa_space
= isl_pw_aff_get_space(pa
);
6228 match
= isl_space_has_equal_params(space
, pa_space
);
6230 isl_space_free(pa_space
);
6234 /* Check that the domain space of "pa" matches "space".
6236 isl_stat
isl_pw_aff_check_match_domain_space(__isl_keep isl_pw_aff
*pa
,
6237 __isl_keep isl_space
*space
)
6239 isl_space
*pa_space
;
6243 return isl_stat_error
;
6245 pa_space
= isl_pw_aff_get_space(pa
);
6247 match
= isl_space_has_equal_params(space
, pa_space
);
6251 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
6252 "parameters don't match", goto error
);
6253 match
= isl_space_tuple_is_equal(space
, isl_dim_in
,
6254 pa_space
, isl_dim_in
);
6258 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
6259 "domains don't match", goto error
);
6260 isl_space_free(pa_space
);
6263 isl_space_free(pa_space
);
6264 return isl_stat_error
;
6272 #include <isl_multi_explicit_domain.c>
6273 #include <isl_multi_pw_aff_explicit_domain.c>
6274 #include <isl_multi_templ.c>
6275 #include <isl_multi_add_constant_templ.c>
6276 #include <isl_multi_apply_set.c>
6277 #include <isl_multi_arith_templ.c>
6278 #include <isl_multi_bind_templ.c>
6279 #include <isl_multi_bind_domain_templ.c>
6280 #include <isl_multi_coalesce.c>
6281 #include <isl_multi_domain_templ.c>
6282 #include <isl_multi_dim_id_templ.c>
6283 #include <isl_multi_dims.c>
6284 #include <isl_multi_from_base_templ.c>
6285 #include <isl_multi_gist.c>
6286 #include <isl_multi_hash.c>
6287 #include <isl_multi_identity_templ.c>
6288 #include <isl_multi_align_set.c>
6289 #include <isl_multi_intersect.c>
6290 #include <isl_multi_min_max_templ.c>
6291 #include <isl_multi_move_dims_templ.c>
6292 #include <isl_multi_nan_templ.c>
6293 #include <isl_multi_param_templ.c>
6294 #include <isl_multi_product_templ.c>
6295 #include <isl_multi_splice_templ.c>
6296 #include <isl_multi_tuple_id_templ.c>
6297 #include <isl_multi_union_add_templ.c>
6298 #include <isl_multi_zero_templ.c>
6300 /* Are all elements of "mpa" piecewise constants?
6302 isl_bool
isl_multi_pw_aff_is_cst(__isl_keep isl_multi_pw_aff
*mpa
)
6304 return isl_multi_pw_aff_every(mpa
, &isl_pw_aff_is_cst
);
6307 /* Does "mpa" have a non-trivial explicit domain?
6309 * The explicit domain, if present, is trivial if it represents
6310 * an (obviously) universe set.
6312 isl_bool
isl_multi_pw_aff_has_non_trivial_domain(
6313 __isl_keep isl_multi_pw_aff
*mpa
)
6316 return isl_bool_error
;
6317 if (!isl_multi_pw_aff_has_explicit_domain(mpa
))
6318 return isl_bool_false
;
6319 return isl_bool_not(isl_set_plain_is_universe(mpa
->u
.dom
));
6322 /* Scale the elements of "pma" by the corresponding elements of "mv".
6324 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_scale_multi_val(
6325 __isl_take isl_pw_multi_aff
*pma
, __isl_take isl_multi_val
*mv
)
6328 isl_bool equal_params
;
6330 pma
= isl_pw_multi_aff_cow(pma
);
6333 if (!isl_space_tuple_is_equal(pma
->dim
, isl_dim_out
,
6334 mv
->space
, isl_dim_set
))
6335 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
6336 "spaces don't match", goto error
);
6337 equal_params
= isl_space_has_equal_params(pma
->dim
, mv
->space
);
6338 if (equal_params
< 0)
6340 if (!equal_params
) {
6341 pma
= isl_pw_multi_aff_align_params(pma
,
6342 isl_multi_val_get_space(mv
));
6343 mv
= isl_multi_val_align_params(mv
,
6344 isl_pw_multi_aff_get_space(pma
));
6349 for (i
= 0; i
< pma
->n
; ++i
) {
6350 pma
->p
[i
].maff
= isl_multi_aff_scale_multi_val(pma
->p
[i
].maff
,
6351 isl_multi_val_copy(mv
));
6352 if (!pma
->p
[i
].maff
)
6356 isl_multi_val_free(mv
);
6359 isl_multi_val_free(mv
);
6360 isl_pw_multi_aff_free(pma
);
6364 /* This function is called for each entry of an isl_union_pw_multi_aff.
6365 * If the space of the entry matches that of data->mv,
6366 * then apply isl_pw_multi_aff_scale_multi_val and return the result.
6367 * Otherwise, return an empty isl_pw_multi_aff.
6369 static __isl_give isl_pw_multi_aff
*union_pw_multi_aff_scale_multi_val_entry(
6370 __isl_take isl_pw_multi_aff
*pma
, void *user
)
6372 isl_multi_val
*mv
= user
;
6376 if (!isl_space_tuple_is_equal(pma
->dim
, isl_dim_out
,
6377 mv
->space
, isl_dim_set
)) {
6378 isl_space
*space
= isl_pw_multi_aff_get_space(pma
);
6379 isl_pw_multi_aff_free(pma
);
6380 return isl_pw_multi_aff_empty(space
);
6383 return isl_pw_multi_aff_scale_multi_val(pma
, isl_multi_val_copy(mv
));
6386 /* Scale the elements of "upma" by the corresponding elements of "mv",
6387 * for those entries that match the space of "mv".
6389 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_scale_multi_val(
6390 __isl_take isl_union_pw_multi_aff
*upma
, __isl_take isl_multi_val
*mv
)
6392 struct isl_union_pw_multi_aff_transform_control control
= {
6393 .fn
= &union_pw_multi_aff_scale_multi_val_entry
,
6397 upma
= isl_union_pw_multi_aff_align_params(upma
,
6398 isl_multi_val_get_space(mv
));
6399 mv
= isl_multi_val_align_params(mv
,
6400 isl_union_pw_multi_aff_get_space(upma
));
6404 return isl_union_pw_multi_aff_transform(upma
, &control
);
6406 isl_multi_val_free(mv
);
6409 isl_multi_val_free(mv
);
6410 isl_union_pw_multi_aff_free(upma
);
6414 /* Construct and return a piecewise multi affine expression
6415 * in the given space with value zero in each of the output dimensions and
6416 * a universe domain.
6418 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_zero(__isl_take isl_space
*space
)
6420 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_zero(space
));
6423 /* Construct and return a piecewise multi affine expression
6424 * that is equal to the given piecewise affine expression.
6426 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_pw_aff(
6427 __isl_take isl_pw_aff
*pa
)
6431 isl_pw_multi_aff
*pma
;
6436 space
= isl_pw_aff_get_space(pa
);
6437 pma
= isl_pw_multi_aff_alloc_size(space
, pa
->n
);
6439 for (i
= 0; i
< pa
->n
; ++i
) {
6443 set
= isl_set_copy(pa
->p
[i
].set
);
6444 ma
= isl_multi_aff_from_aff(isl_aff_copy(pa
->p
[i
].aff
));
6445 pma
= isl_pw_multi_aff_add_piece(pma
, set
, ma
);
6448 isl_pw_aff_free(pa
);
6452 /* Construct and return a piecewise multi affine expression
6453 * that is equal to the given multi piecewise affine expression
6454 * on the shared domain of the piecewise affine expressions,
6455 * in the special case of a 0D multi piecewise affine expression.
6457 * Create a piecewise multi affine expression with the explicit domain of
6458 * the 0D multi piecewise affine expression as domain.
6460 static __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_multi_pw_aff_0D(
6461 __isl_take isl_multi_pw_aff
*mpa
)
6467 space
= isl_multi_pw_aff_get_space(mpa
);
6468 dom
= isl_multi_pw_aff_get_explicit_domain(mpa
);
6469 isl_multi_pw_aff_free(mpa
);
6471 ma
= isl_multi_aff_zero(space
);
6472 return isl_pw_multi_aff_alloc(dom
, ma
);
6475 /* Construct and return a piecewise multi affine expression
6476 * that is equal to the given multi piecewise affine expression
6477 * on the shared domain of the piecewise affine expressions.
6479 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_multi_pw_aff(
6480 __isl_take isl_multi_pw_aff
*mpa
)
6485 isl_pw_multi_aff
*pma
;
6491 return isl_pw_multi_aff_from_multi_pw_aff_0D(mpa
);
6493 space
= isl_multi_pw_aff_get_space(mpa
);
6494 pa
= isl_multi_pw_aff_get_pw_aff(mpa
, 0);
6495 pma
= isl_pw_multi_aff_from_pw_aff(pa
);
6497 for (i
= 1; i
< mpa
->n
; ++i
) {
6498 isl_pw_multi_aff
*pma_i
;
6500 pa
= isl_multi_pw_aff_get_pw_aff(mpa
, i
);
6501 pma_i
= isl_pw_multi_aff_from_pw_aff(pa
);
6502 pma
= isl_pw_multi_aff_range_product(pma
, pma_i
);
6505 pma
= isl_pw_multi_aff_reset_space(pma
, space
);
6507 isl_multi_pw_aff_free(mpa
);
6511 /* Construct and return a multi piecewise affine expression
6512 * that is equal to the given multi affine expression.
6514 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_from_multi_aff(
6515 __isl_take isl_multi_aff
*ma
)
6519 isl_multi_pw_aff
*mpa
;
6521 n
= isl_multi_aff_dim(ma
, isl_dim_out
);
6523 ma
= isl_multi_aff_free(ma
);
6527 mpa
= isl_multi_pw_aff_alloc(isl_multi_aff_get_space(ma
));
6529 for (i
= 0; i
< n
; ++i
) {
6532 pa
= isl_pw_aff_from_aff(isl_multi_aff_get_aff(ma
, i
));
6533 mpa
= isl_multi_pw_aff_set_pw_aff(mpa
, i
, pa
);
6536 isl_multi_aff_free(ma
);
6540 /* Construct and return a multi piecewise affine expression
6541 * that is equal to the given piecewise multi affine expression.
6543 * If the resulting multi piecewise affine expression has
6544 * an explicit domain, then assign it the domain of the input.
6545 * In other cases, the domain is stored in the individual elements.
6547 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_from_pw_multi_aff(
6548 __isl_take isl_pw_multi_aff
*pma
)
6553 isl_multi_pw_aff
*mpa
;
6555 n
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
6557 pma
= isl_pw_multi_aff_free(pma
);
6558 space
= isl_pw_multi_aff_get_space(pma
);
6559 mpa
= isl_multi_pw_aff_alloc(space
);
6561 for (i
= 0; i
< n
; ++i
) {
6564 pa
= isl_pw_multi_aff_get_pw_aff(pma
, i
);
6565 mpa
= isl_multi_pw_aff_set_pw_aff(mpa
, i
, pa
);
6567 if (isl_multi_pw_aff_has_explicit_domain(mpa
)) {
6570 dom
= isl_pw_multi_aff_domain(isl_pw_multi_aff_copy(pma
));
6571 mpa
= isl_multi_pw_aff_intersect_domain(mpa
, dom
);
6574 isl_pw_multi_aff_free(pma
);
6578 /* Do "pa1" and "pa2" represent the same function?
6580 * We first check if they are obviously equal.
6581 * If not, we convert them to maps and check if those are equal.
6583 * If "pa1" or "pa2" contain any NaNs, then they are considered
6584 * not to be the same. A NaN is not equal to anything, not even
6587 isl_bool
isl_pw_aff_is_equal(__isl_keep isl_pw_aff
*pa1
,
6588 __isl_keep isl_pw_aff
*pa2
)
6592 isl_map
*map1
, *map2
;
6595 return isl_bool_error
;
6597 equal
= isl_pw_aff_plain_is_equal(pa1
, pa2
);
6598 if (equal
< 0 || equal
)
6600 has_nan
= either_involves_nan(pa1
, pa2
);
6602 return isl_bool_error
;
6604 return isl_bool_false
;
6606 map1
= isl_map_from_pw_aff_internal(isl_pw_aff_copy(pa1
));
6607 map2
= isl_map_from_pw_aff_internal(isl_pw_aff_copy(pa2
));
6608 equal
= isl_map_is_equal(map1
, map2
);
6615 /* Do "mpa1" and "mpa2" represent the same function?
6617 * Note that we cannot convert the entire isl_multi_pw_aff
6618 * to a map because the domains of the piecewise affine expressions
6619 * may not be the same.
6621 isl_bool
isl_multi_pw_aff_is_equal(__isl_keep isl_multi_pw_aff
*mpa1
,
6622 __isl_keep isl_multi_pw_aff
*mpa2
)
6625 isl_bool equal
, equal_params
;
6628 return isl_bool_error
;
6630 equal_params
= isl_space_has_equal_params(mpa1
->space
, mpa2
->space
);
6631 if (equal_params
< 0)
6632 return isl_bool_error
;
6633 if (!equal_params
) {
6634 if (!isl_space_has_named_params(mpa1
->space
))
6635 return isl_bool_false
;
6636 if (!isl_space_has_named_params(mpa2
->space
))
6637 return isl_bool_false
;
6638 mpa1
= isl_multi_pw_aff_copy(mpa1
);
6639 mpa2
= isl_multi_pw_aff_copy(mpa2
);
6640 mpa1
= isl_multi_pw_aff_align_params(mpa1
,
6641 isl_multi_pw_aff_get_space(mpa2
));
6642 mpa2
= isl_multi_pw_aff_align_params(mpa2
,
6643 isl_multi_pw_aff_get_space(mpa1
));
6644 equal
= isl_multi_pw_aff_is_equal(mpa1
, mpa2
);
6645 isl_multi_pw_aff_free(mpa1
);
6646 isl_multi_pw_aff_free(mpa2
);
6650 equal
= isl_space_is_equal(mpa1
->space
, mpa2
->space
);
6651 if (equal
< 0 || !equal
)
6654 for (i
= 0; i
< mpa1
->n
; ++i
) {
6655 equal
= isl_pw_aff_is_equal(mpa1
->u
.p
[i
], mpa2
->u
.p
[i
]);
6656 if (equal
< 0 || !equal
)
6660 return isl_bool_true
;
6663 /* Do "pma1" and "pma2" represent the same function?
6665 * First check if they are obviously equal.
6666 * If not, then convert them to maps and check if those are equal.
6668 * If "pa1" or "pa2" contain any NaNs, then they are considered
6669 * not to be the same. A NaN is not equal to anything, not even
6672 isl_bool
isl_pw_multi_aff_is_equal(__isl_keep isl_pw_multi_aff
*pma1
,
6673 __isl_keep isl_pw_multi_aff
*pma2
)
6677 isl_map
*map1
, *map2
;
6680 return isl_bool_error
;
6682 equal
= isl_pw_multi_aff_plain_is_equal(pma1
, pma2
);
6683 if (equal
< 0 || equal
)
6685 has_nan
= isl_pw_multi_aff_involves_nan(pma1
);
6686 if (has_nan
>= 0 && !has_nan
)
6687 has_nan
= isl_pw_multi_aff_involves_nan(pma2
);
6688 if (has_nan
< 0 || has_nan
)
6689 return isl_bool_not(has_nan
);
6691 map1
= isl_map_from_pw_multi_aff_internal(isl_pw_multi_aff_copy(pma1
));
6692 map2
= isl_map_from_pw_multi_aff_internal(isl_pw_multi_aff_copy(pma2
));
6693 equal
= isl_map_is_equal(map1
, map2
);
6700 /* Compute the pullback of "mpa" by the function represented by "ma".
6701 * In other words, plug in "ma" in "mpa".
6703 * The parameters of "mpa" and "ma" are assumed to have been aligned.
6705 * If "mpa" has an explicit domain, then it is this domain
6706 * that needs to undergo a pullback, i.e., a preimage.
6708 static __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_pullback_multi_aff_aligned(
6709 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_multi_aff
*ma
)
6712 isl_space
*space
= NULL
;
6714 mpa
= isl_multi_pw_aff_cow(mpa
);
6718 space
= isl_space_join(isl_multi_aff_get_space(ma
),
6719 isl_multi_pw_aff_get_space(mpa
));
6723 for (i
= 0; i
< mpa
->n
; ++i
) {
6724 mpa
->u
.p
[i
] = isl_pw_aff_pullback_multi_aff(mpa
->u
.p
[i
],
6725 isl_multi_aff_copy(ma
));
6729 if (isl_multi_pw_aff_has_explicit_domain(mpa
)) {
6730 mpa
->u
.dom
= isl_set_preimage_multi_aff(mpa
->u
.dom
,
6731 isl_multi_aff_copy(ma
));
6736 isl_multi_aff_free(ma
);
6737 isl_space_free(mpa
->space
);
6741 isl_space_free(space
);
6742 isl_multi_pw_aff_free(mpa
);
6743 isl_multi_aff_free(ma
);
6747 /* Compute the pullback of "mpa" by the function represented by "ma".
6748 * In other words, plug in "ma" in "mpa".
6750 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_pullback_multi_aff(
6751 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_multi_aff
*ma
)
6753 isl_bool equal_params
;
6757 equal_params
= isl_space_has_equal_params(mpa
->space
, ma
->space
);
6758 if (equal_params
< 0)
6761 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa
, ma
);
6762 mpa
= isl_multi_pw_aff_align_params(mpa
, isl_multi_aff_get_space(ma
));
6763 ma
= isl_multi_aff_align_params(ma
, isl_multi_pw_aff_get_space(mpa
));
6764 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa
, ma
);
6766 isl_multi_pw_aff_free(mpa
);
6767 isl_multi_aff_free(ma
);
6771 /* Compute the pullback of "mpa" by the function represented by "pma".
6772 * In other words, plug in "pma" in "mpa".
6774 * The parameters of "mpa" and "mpa" are assumed to have been aligned.
6776 * If "mpa" has an explicit domain, then it is this domain
6777 * that needs to undergo a pullback, i.e., a preimage.
6779 static __isl_give isl_multi_pw_aff
*
6780 isl_multi_pw_aff_pullback_pw_multi_aff_aligned(
6781 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_pw_multi_aff
*pma
)
6784 isl_space
*space
= NULL
;
6786 mpa
= isl_multi_pw_aff_cow(mpa
);
6790 space
= isl_space_join(isl_pw_multi_aff_get_space(pma
),
6791 isl_multi_pw_aff_get_space(mpa
));
6793 for (i
= 0; i
< mpa
->n
; ++i
) {
6794 mpa
->u
.p
[i
] = isl_pw_aff_pullback_pw_multi_aff_aligned(
6795 mpa
->u
.p
[i
], isl_pw_multi_aff_copy(pma
));
6799 if (isl_multi_pw_aff_has_explicit_domain(mpa
)) {
6800 mpa
->u
.dom
= isl_set_preimage_pw_multi_aff(mpa
->u
.dom
,
6801 isl_pw_multi_aff_copy(pma
));
6806 isl_pw_multi_aff_free(pma
);
6807 isl_space_free(mpa
->space
);
6811 isl_space_free(space
);
6812 isl_multi_pw_aff_free(mpa
);
6813 isl_pw_multi_aff_free(pma
);
6817 /* Compute the pullback of "mpa" by the function represented by "pma".
6818 * In other words, plug in "pma" in "mpa".
6820 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_pullback_pw_multi_aff(
6821 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_pw_multi_aff
*pma
)
6823 isl_bool equal_params
;
6827 equal_params
= isl_space_has_equal_params(mpa
->space
, pma
->dim
);
6828 if (equal_params
< 0)
6831 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa
, pma
);
6832 mpa
= isl_multi_pw_aff_align_params(mpa
,
6833 isl_pw_multi_aff_get_space(pma
));
6834 pma
= isl_pw_multi_aff_align_params(pma
,
6835 isl_multi_pw_aff_get_space(mpa
));
6836 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa
, pma
);
6838 isl_multi_pw_aff_free(mpa
);
6839 isl_pw_multi_aff_free(pma
);
6843 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
6844 * with the domain of "aff". The domain of the result is the same
6846 * "mpa" and "aff" are assumed to have been aligned.
6848 * We first extract the parametric constant from "aff", defined
6849 * over the correct domain.
6850 * Then we add the appropriate combinations of the members of "mpa".
6851 * Finally, we add the integer divisions through recursive calls.
6853 static __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_aff_aligned(
6854 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_aff
*aff
)
6857 isl_size n_in
, n_div
, n_mpa_in
;
6863 n_in
= isl_aff_dim(aff
, isl_dim_in
);
6864 n_div
= isl_aff_dim(aff
, isl_dim_div
);
6865 n_mpa_in
= isl_multi_pw_aff_dim(mpa
, isl_dim_in
);
6866 if (n_in
< 0 || n_div
< 0 || n_mpa_in
< 0)
6869 space
= isl_space_domain(isl_multi_pw_aff_get_space(mpa
));
6870 tmp
= isl_aff_copy(aff
);
6871 tmp
= isl_aff_drop_dims(tmp
, isl_dim_div
, 0, n_div
);
6872 tmp
= isl_aff_drop_dims(tmp
, isl_dim_in
, 0, n_in
);
6873 tmp
= isl_aff_add_dims(tmp
, isl_dim_in
, n_mpa_in
);
6874 tmp
= isl_aff_reset_domain_space(tmp
, space
);
6875 pa
= isl_pw_aff_from_aff(tmp
);
6877 for (i
= 0; i
< n_in
; ++i
) {
6880 if (!isl_aff_involves_dims(aff
, isl_dim_in
, i
, 1))
6882 v
= isl_aff_get_coefficient_val(aff
, isl_dim_in
, i
);
6883 pa_i
= isl_multi_pw_aff_get_pw_aff(mpa
, i
);
6884 pa_i
= isl_pw_aff_scale_val(pa_i
, v
);
6885 pa
= isl_pw_aff_add(pa
, pa_i
);
6888 for (i
= 0; i
< n_div
; ++i
) {
6892 if (!isl_aff_involves_dims(aff
, isl_dim_div
, i
, 1))
6894 div
= isl_aff_get_div(aff
, i
);
6895 pa_i
= isl_multi_pw_aff_apply_aff_aligned(
6896 isl_multi_pw_aff_copy(mpa
), div
);
6897 pa_i
= isl_pw_aff_floor(pa_i
);
6898 v
= isl_aff_get_coefficient_val(aff
, isl_dim_div
, i
);
6899 pa_i
= isl_pw_aff_scale_val(pa_i
, v
);
6900 pa
= isl_pw_aff_add(pa
, pa_i
);
6903 isl_multi_pw_aff_free(mpa
);
6908 isl_multi_pw_aff_free(mpa
);
6913 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
6914 * with the domain of "aff". The domain of the result is the same
6917 __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_aff(
6918 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_aff
*aff
)
6920 isl_bool equal_params
;
6924 equal_params
= isl_space_has_equal_params(aff
->ls
->dim
, mpa
->space
);
6925 if (equal_params
< 0)
6928 return isl_multi_pw_aff_apply_aff_aligned(mpa
, aff
);
6930 aff
= isl_aff_align_params(aff
, isl_multi_pw_aff_get_space(mpa
));
6931 mpa
= isl_multi_pw_aff_align_params(mpa
, isl_aff_get_space(aff
));
6933 return isl_multi_pw_aff_apply_aff_aligned(mpa
, aff
);
6936 isl_multi_pw_aff_free(mpa
);
6940 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
6941 * with the domain of "pa". The domain of the result is the same
6943 * "mpa" and "pa" are assumed to have been aligned.
6945 * We consider each piece in turn. Note that the domains of the
6946 * pieces are assumed to be disjoint and they remain disjoint
6947 * after taking the preimage (over the same function).
6949 static __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_pw_aff_aligned(
6950 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_pw_aff
*pa
)
6959 space
= isl_space_join(isl_multi_pw_aff_get_space(mpa
),
6960 isl_pw_aff_get_space(pa
));
6961 res
= isl_pw_aff_empty(space
);
6963 for (i
= 0; i
< pa
->n
; ++i
) {
6967 pa_i
= isl_multi_pw_aff_apply_aff_aligned(
6968 isl_multi_pw_aff_copy(mpa
),
6969 isl_aff_copy(pa
->p
[i
].aff
));
6970 domain
= isl_set_copy(pa
->p
[i
].set
);
6971 domain
= isl_set_preimage_multi_pw_aff(domain
,
6972 isl_multi_pw_aff_copy(mpa
));
6973 pa_i
= isl_pw_aff_intersect_domain(pa_i
, domain
);
6974 res
= isl_pw_aff_add_disjoint(res
, pa_i
);
6977 isl_pw_aff_free(pa
);
6978 isl_multi_pw_aff_free(mpa
);
6981 isl_pw_aff_free(pa
);
6982 isl_multi_pw_aff_free(mpa
);
6986 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
6987 * with the domain of "pa". The domain of the result is the same
6990 __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_pw_aff(
6991 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_pw_aff
*pa
)
6993 isl_bool equal_params
;
6997 equal_params
= isl_space_has_equal_params(pa
->dim
, mpa
->space
);
6998 if (equal_params
< 0)
7001 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa
, pa
);
7003 pa
= isl_pw_aff_align_params(pa
, isl_multi_pw_aff_get_space(mpa
));
7004 mpa
= isl_multi_pw_aff_align_params(mpa
, isl_pw_aff_get_space(pa
));
7006 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa
, pa
);
7008 isl_pw_aff_free(pa
);
7009 isl_multi_pw_aff_free(mpa
);
7013 /* Compute the pullback of "pa" by the function represented by "mpa".
7014 * In other words, plug in "mpa" in "pa".
7015 * "pa" and "mpa" are assumed to have been aligned.
7017 * The pullback is computed by applying "pa" to "mpa".
7019 static __isl_give isl_pw_aff
*isl_pw_aff_pullback_multi_pw_aff_aligned(
7020 __isl_take isl_pw_aff
*pa
, __isl_take isl_multi_pw_aff
*mpa
)
7022 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa
, pa
);
7025 /* Compute the pullback of "pa" by the function represented by "mpa".
7026 * In other words, plug in "mpa" in "pa".
7028 * The pullback is computed by applying "pa" to "mpa".
7030 __isl_give isl_pw_aff
*isl_pw_aff_pullback_multi_pw_aff(
7031 __isl_take isl_pw_aff
*pa
, __isl_take isl_multi_pw_aff
*mpa
)
7033 return isl_multi_pw_aff_apply_pw_aff(mpa
, pa
);
7036 /* Compute the pullback of "mpa1" by the function represented by "mpa2".
7037 * In other words, plug in "mpa2" in "mpa1".
7039 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7041 * We pullback each member of "mpa1" in turn.
7043 * If "mpa1" has an explicit domain, then it is this domain
7044 * that needs to undergo a pullback instead, i.e., a preimage.
7046 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_pullback_multi_pw_aff(
7047 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
)
7050 isl_space
*space
= NULL
;
7052 isl_multi_pw_aff_align_params_bin(&mpa1
, &mpa2
);
7053 mpa1
= isl_multi_pw_aff_cow(mpa1
);
7057 space
= isl_space_join(isl_multi_pw_aff_get_space(mpa2
),
7058 isl_multi_pw_aff_get_space(mpa1
));
7060 for (i
= 0; i
< mpa1
->n
; ++i
) {
7061 mpa1
->u
.p
[i
] = isl_pw_aff_pullback_multi_pw_aff_aligned(
7062 mpa1
->u
.p
[i
], isl_multi_pw_aff_copy(mpa2
));
7067 if (isl_multi_pw_aff_has_explicit_domain(mpa1
)) {
7068 mpa1
->u
.dom
= isl_set_preimage_multi_pw_aff(mpa1
->u
.dom
,
7069 isl_multi_pw_aff_copy(mpa2
));
7073 mpa1
= isl_multi_pw_aff_reset_space(mpa1
, space
);
7075 isl_multi_pw_aff_free(mpa2
);
7078 isl_space_free(space
);
7079 isl_multi_pw_aff_free(mpa1
);
7080 isl_multi_pw_aff_free(mpa2
);
7084 /* Align the parameters of "mpa1" and "mpa2", check that the ranges
7085 * of "mpa1" and "mpa2" live in the same space, construct map space
7086 * between the domain spaces of "mpa1" and "mpa2" and call "order"
7087 * with this map space as extract argument.
7089 static __isl_give isl_map
*isl_multi_pw_aff_order_map(
7090 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
,
7091 __isl_give isl_map
*(*order
)(__isl_keep isl_multi_pw_aff
*mpa1
,
7092 __isl_keep isl_multi_pw_aff
*mpa2
, __isl_take isl_space
*space
))
7095 isl_space
*space1
, *space2
;
7098 mpa1
= isl_multi_pw_aff_align_params(mpa1
,
7099 isl_multi_pw_aff_get_space(mpa2
));
7100 mpa2
= isl_multi_pw_aff_align_params(mpa2
,
7101 isl_multi_pw_aff_get_space(mpa1
));
7104 match
= isl_space_tuple_is_equal(mpa1
->space
, isl_dim_out
,
7105 mpa2
->space
, isl_dim_out
);
7109 isl_die(isl_multi_pw_aff_get_ctx(mpa1
), isl_error_invalid
,
7110 "range spaces don't match", goto error
);
7111 space1
= isl_space_domain(isl_multi_pw_aff_get_space(mpa1
));
7112 space2
= isl_space_domain(isl_multi_pw_aff_get_space(mpa2
));
7113 space1
= isl_space_map_from_domain_and_range(space1
, space2
);
7115 res
= order(mpa1
, mpa2
, space1
);
7116 isl_multi_pw_aff_free(mpa1
);
7117 isl_multi_pw_aff_free(mpa2
);
7120 isl_multi_pw_aff_free(mpa1
);
7121 isl_multi_pw_aff_free(mpa2
);
7125 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7126 * where the function values are equal. "space" is the space of the result.
7127 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7129 * "mpa1" and "mpa2" are equal when each of the pairs of elements
7130 * in the sequences are equal.
7132 static __isl_give isl_map
*isl_multi_pw_aff_eq_map_on_space(
7133 __isl_keep isl_multi_pw_aff
*mpa1
, __isl_keep isl_multi_pw_aff
*mpa2
,
7134 __isl_take isl_space
*space
)
7140 n
= isl_multi_pw_aff_dim(mpa1
, isl_dim_out
);
7142 space
= isl_space_free(space
);
7143 res
= isl_map_universe(space
);
7145 for (i
= 0; i
< n
; ++i
) {
7146 isl_pw_aff
*pa1
, *pa2
;
7149 pa1
= isl_multi_pw_aff_get_pw_aff(mpa1
, i
);
7150 pa2
= isl_multi_pw_aff_get_pw_aff(mpa2
, i
);
7151 map
= isl_pw_aff_eq_map(pa1
, pa2
);
7152 res
= isl_map_intersect(res
, map
);
7158 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7159 * where the function values are equal.
7161 __isl_give isl_map
*isl_multi_pw_aff_eq_map(__isl_take isl_multi_pw_aff
*mpa1
,
7162 __isl_take isl_multi_pw_aff
*mpa2
)
7164 return isl_multi_pw_aff_order_map(mpa1
, mpa2
,
7165 &isl_multi_pw_aff_eq_map_on_space
);
7168 /* Intersect "map" with the result of applying "order"
7169 * on two copies of "mpa".
7171 static __isl_give isl_map
*isl_map_order_at_multi_pw_aff(
7172 __isl_take isl_map
*map
, __isl_take isl_multi_pw_aff
*mpa
,
7173 __isl_give isl_map
*(*order
)(__isl_take isl_multi_pw_aff
*mpa1
,
7174 __isl_take isl_multi_pw_aff
*mpa2
))
7176 return isl_map_intersect(map
, order(mpa
, isl_multi_pw_aff_copy(mpa
)));
7179 /* Return the subset of "map" where the domain and the range
7180 * have equal "mpa" values.
7182 __isl_give isl_map
*isl_map_eq_at_multi_pw_aff(__isl_take isl_map
*map
,
7183 __isl_take isl_multi_pw_aff
*mpa
)
7185 return isl_map_order_at_multi_pw_aff(map
, mpa
,
7186 &isl_multi_pw_aff_eq_map
);
7189 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7190 * where the function values of "mpa1" lexicographically satisfies "base"
7191 * compared to that of "mpa2". "space" is the space of the result.
7192 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7194 * "mpa1" lexicographically satisfies "base" compared to "mpa2"
7195 * if its i-th element satisfies "base" when compared to
7196 * the i-th element of "mpa2" while all previous elements are
7199 static __isl_give isl_map
*isl_multi_pw_aff_lex_map_on_space(
7200 __isl_keep isl_multi_pw_aff
*mpa1
, __isl_keep isl_multi_pw_aff
*mpa2
,
7201 __isl_give isl_map
*(*base
)(__isl_take isl_pw_aff
*pa1
,
7202 __isl_take isl_pw_aff
*pa2
),
7203 __isl_take isl_space
*space
)
7207 isl_map
*res
, *rest
;
7209 n
= isl_multi_pw_aff_dim(mpa1
, isl_dim_out
);
7211 space
= isl_space_free(space
);
7212 res
= isl_map_empty(isl_space_copy(space
));
7213 rest
= isl_map_universe(space
);
7215 for (i
= 0; i
< n
; ++i
) {
7216 isl_pw_aff
*pa1
, *pa2
;
7219 pa1
= isl_multi_pw_aff_get_pw_aff(mpa1
, i
);
7220 pa2
= isl_multi_pw_aff_get_pw_aff(mpa2
, i
);
7221 map
= base(pa1
, pa2
);
7222 map
= isl_map_intersect(map
, isl_map_copy(rest
));
7223 res
= isl_map_union(res
, map
);
7228 pa1
= isl_multi_pw_aff_get_pw_aff(mpa1
, i
);
7229 pa2
= isl_multi_pw_aff_get_pw_aff(mpa2
, i
);
7230 map
= isl_pw_aff_eq_map(pa1
, pa2
);
7231 rest
= isl_map_intersect(rest
, map
);
7240 #include "isl_aff_lex_templ.c"
7244 #include "isl_aff_lex_templ.c"
7248 #include "isl_aff_lex_templ.c"
7252 #include "isl_aff_lex_templ.c"
7254 /* Compare two isl_affs.
7256 * Return -1 if "aff1" is "smaller" than "aff2", 1 if "aff1" is "greater"
7257 * than "aff2" and 0 if they are equal.
7259 * The order is fairly arbitrary. We do consider expressions that only involve
7260 * earlier dimensions as "smaller".
7262 int isl_aff_plain_cmp(__isl_keep isl_aff
*aff1
, __isl_keep isl_aff
*aff2
)
7275 cmp
= isl_local_space_cmp(aff1
->ls
, aff2
->ls
);
7279 last1
= isl_seq_last_non_zero(aff1
->v
->el
+ 1, aff1
->v
->size
- 1);
7280 last2
= isl_seq_last_non_zero(aff2
->v
->el
+ 1, aff1
->v
->size
- 1);
7282 return last1
- last2
;
7284 return isl_seq_cmp(aff1
->v
->el
, aff2
->v
->el
, aff1
->v
->size
);
7287 /* Compare two isl_pw_affs.
7289 * Return -1 if "pa1" is "smaller" than "pa2", 1 if "pa1" is "greater"
7290 * than "pa2" and 0 if they are equal.
7292 * The order is fairly arbitrary. We do consider expressions that only involve
7293 * earlier dimensions as "smaller".
7295 int isl_pw_aff_plain_cmp(__isl_keep isl_pw_aff
*pa1
,
7296 __isl_keep isl_pw_aff
*pa2
)
7309 cmp
= isl_space_cmp(pa1
->dim
, pa2
->dim
);
7313 if (pa1
->n
!= pa2
->n
)
7314 return pa1
->n
- pa2
->n
;
7316 for (i
= 0; i
< pa1
->n
; ++i
) {
7317 cmp
= isl_set_plain_cmp(pa1
->p
[i
].set
, pa2
->p
[i
].set
);
7320 cmp
= isl_aff_plain_cmp(pa1
->p
[i
].aff
, pa2
->p
[i
].aff
);
7328 /* Return a piecewise affine expression that is equal to "v" on "domain".
7330 __isl_give isl_pw_aff
*isl_pw_aff_val_on_domain(__isl_take isl_set
*domain
,
7331 __isl_take isl_val
*v
)
7334 isl_local_space
*ls
;
7337 space
= isl_set_get_space(domain
);
7338 ls
= isl_local_space_from_space(space
);
7339 aff
= isl_aff_val_on_domain(ls
, v
);
7341 return isl_pw_aff_alloc(domain
, aff
);
7344 /* Return a piecewise affine expression that is equal to the parameter
7345 * with identifier "id" on "domain".
7347 __isl_give isl_pw_aff
*isl_pw_aff_param_on_domain_id(
7348 __isl_take isl_set
*domain
, __isl_take isl_id
*id
)
7353 space
= isl_set_get_space(domain
);
7354 space
= isl_space_add_param_id(space
, isl_id_copy(id
));
7355 domain
= isl_set_align_params(domain
, isl_space_copy(space
));
7356 aff
= isl_aff_param_on_domain_space_id(space
, id
);
7358 return isl_pw_aff_alloc(domain
, aff
);
7361 /* Return a multi affine expression that is equal to "mv" on domain
7364 __isl_give isl_multi_aff
*isl_multi_aff_multi_val_on_space(
7365 __isl_take isl_space
*space
, __isl_take isl_multi_val
*mv
)
7370 isl_local_space
*ls
;
7373 n
= isl_multi_val_dim(mv
, isl_dim_set
);
7374 if (!space
|| n
< 0)
7377 space2
= isl_multi_val_get_space(mv
);
7378 space2
= isl_space_align_params(space2
, isl_space_copy(space
));
7379 space
= isl_space_align_params(space
, isl_space_copy(space2
));
7380 space
= isl_space_map_from_domain_and_range(space
, space2
);
7381 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
7382 ls
= isl_local_space_from_space(isl_space_domain(space
));
7383 for (i
= 0; i
< n
; ++i
) {
7387 v
= isl_multi_val_get_val(mv
, i
);
7388 aff
= isl_aff_val_on_domain(isl_local_space_copy(ls
), v
);
7389 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
7391 isl_local_space_free(ls
);
7393 isl_multi_val_free(mv
);
7396 isl_space_free(space
);
7397 isl_multi_val_free(mv
);
7401 /* Return a piecewise multi-affine expression
7402 * that is equal to "mv" on "domain".
7404 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_multi_val_on_domain(
7405 __isl_take isl_set
*domain
, __isl_take isl_multi_val
*mv
)
7410 space
= isl_set_get_space(domain
);
7411 ma
= isl_multi_aff_multi_val_on_space(space
, mv
);
7413 return isl_pw_multi_aff_alloc(domain
, ma
);
7416 /* Internal data structure for isl_union_pw_multi_aff_multi_val_on_domain.
7417 * mv is the value that should be attained on each domain set
7418 * res collects the results
7420 struct isl_union_pw_multi_aff_multi_val_on_domain_data
{
7422 isl_union_pw_multi_aff
*res
;
7425 /* Create an isl_pw_multi_aff equal to data->mv on "domain"
7426 * and add it to data->res.
7428 static isl_stat
pw_multi_aff_multi_val_on_domain(__isl_take isl_set
*domain
,
7431 struct isl_union_pw_multi_aff_multi_val_on_domain_data
*data
= user
;
7432 isl_pw_multi_aff
*pma
;
7435 mv
= isl_multi_val_copy(data
->mv
);
7436 pma
= isl_pw_multi_aff_multi_val_on_domain(domain
, mv
);
7437 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma
);
7439 return data
->res
? isl_stat_ok
: isl_stat_error
;
7442 /* Return a union piecewise multi-affine expression
7443 * that is equal to "mv" on "domain".
7445 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_multi_val_on_domain(
7446 __isl_take isl_union_set
*domain
, __isl_take isl_multi_val
*mv
)
7448 struct isl_union_pw_multi_aff_multi_val_on_domain_data data
;
7451 space
= isl_union_set_get_space(domain
);
7452 data
.res
= isl_union_pw_multi_aff_empty(space
);
7454 if (isl_union_set_foreach_set(domain
,
7455 &pw_multi_aff_multi_val_on_domain
, &data
) < 0)
7456 data
.res
= isl_union_pw_multi_aff_free(data
.res
);
7457 isl_union_set_free(domain
);
7458 isl_multi_val_free(mv
);
7462 /* Compute the pullback of data->pma by the function represented by "pma2",
7463 * provided the spaces match, and add the results to data->res.
7465 static isl_stat
pullback_entry(__isl_take isl_pw_multi_aff
*pma2
, void *user
)
7467 struct isl_union_pw_multi_aff_bin_data
*data
= user
;
7469 if (!isl_space_tuple_is_equal(data
->pma
->dim
, isl_dim_in
,
7470 pma2
->dim
, isl_dim_out
)) {
7471 isl_pw_multi_aff_free(pma2
);
7475 pma2
= isl_pw_multi_aff_pullback_pw_multi_aff(
7476 isl_pw_multi_aff_copy(data
->pma
), pma2
);
7478 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma2
);
7480 return isl_stat_error
;
7485 /* Compute the pullback of "upma1" by the function represented by "upma2".
7487 __isl_give isl_union_pw_multi_aff
*
7488 isl_union_pw_multi_aff_pullback_union_pw_multi_aff(
7489 __isl_take isl_union_pw_multi_aff
*upma1
,
7490 __isl_take isl_union_pw_multi_aff
*upma2
)
7492 return bin_op(upma1
, upma2
, &pullback_entry
);
7495 /* Check that the domain space of "upa" matches "space".
7497 * This function is called from isl_multi_union_pw_aff_set_union_pw_aff and
7498 * can in principle never fail since the space "space" is that
7499 * of the isl_multi_union_pw_aff and is a set space such that
7500 * there is no domain space to match.
7502 * We check the parameters and double-check that "space" is
7503 * indeed that of a set.
7505 static isl_stat
isl_union_pw_aff_check_match_domain_space(
7506 __isl_keep isl_union_pw_aff
*upa
, __isl_keep isl_space
*space
)
7508 isl_space
*upa_space
;
7512 return isl_stat_error
;
7514 match
= isl_space_is_set(space
);
7516 return isl_stat_error
;
7518 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
7519 "expecting set space", return isl_stat_error
);
7521 upa_space
= isl_union_pw_aff_get_space(upa
);
7522 match
= isl_space_has_equal_params(space
, upa_space
);
7526 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
7527 "parameters don't match", goto error
);
7529 isl_space_free(upa_space
);
7532 isl_space_free(upa_space
);
7533 return isl_stat_error
;
7536 /* Do the parameters of "upa" match those of "space"?
7538 static isl_bool
isl_union_pw_aff_matching_params(
7539 __isl_keep isl_union_pw_aff
*upa
, __isl_keep isl_space
*space
)
7541 isl_space
*upa_space
;
7545 return isl_bool_error
;
7547 upa_space
= isl_union_pw_aff_get_space(upa
);
7549 match
= isl_space_has_equal_params(space
, upa_space
);
7551 isl_space_free(upa_space
);
7555 /* Internal data structure for isl_union_pw_aff_reset_domain_space.
7556 * space represents the new parameters.
7557 * res collects the results.
7559 struct isl_union_pw_aff_reset_params_data
{
7561 isl_union_pw_aff
*res
;
7564 /* Replace the parameters of "pa" by data->space and
7565 * add the result to data->res.
7567 static isl_stat
reset_params(__isl_take isl_pw_aff
*pa
, void *user
)
7569 struct isl_union_pw_aff_reset_params_data
*data
= user
;
7572 space
= isl_pw_aff_get_space(pa
);
7573 space
= isl_space_replace_params(space
, data
->space
);
7574 pa
= isl_pw_aff_reset_space(pa
, space
);
7575 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
7577 return data
->res
? isl_stat_ok
: isl_stat_error
;
7580 /* Replace the domain space of "upa" by "space".
7581 * Since a union expression does not have a (single) domain space,
7582 * "space" is necessarily a parameter space.
7584 * Since the order and the names of the parameters determine
7585 * the hash value, we need to create a new hash table.
7587 static __isl_give isl_union_pw_aff
*isl_union_pw_aff_reset_domain_space(
7588 __isl_take isl_union_pw_aff
*upa
, __isl_take isl_space
*space
)
7590 struct isl_union_pw_aff_reset_params_data data
= { space
};
7593 match
= isl_union_pw_aff_matching_params(upa
, space
);
7595 upa
= isl_union_pw_aff_free(upa
);
7597 isl_space_free(space
);
7601 data
.res
= isl_union_pw_aff_empty(isl_space_copy(space
));
7602 if (isl_union_pw_aff_foreach_pw_aff(upa
, &reset_params
, &data
) < 0)
7603 data
.res
= isl_union_pw_aff_free(data
.res
);
7605 isl_union_pw_aff_free(upa
);
7606 isl_space_free(space
);
7610 /* Return the floor of "pa".
7612 static __isl_give isl_pw_aff
*floor_entry(__isl_take isl_pw_aff
*pa
, void *user
)
7614 return isl_pw_aff_floor(pa
);
7617 /* Given f, return floor(f).
7619 __isl_give isl_union_pw_aff
*isl_union_pw_aff_floor(
7620 __isl_take isl_union_pw_aff
*upa
)
7622 return isl_union_pw_aff_transform_inplace(upa
, &floor_entry
, NULL
);
7627 * upa mod m = upa - m * floor(upa/m)
7629 * with m an integer value.
7631 __isl_give isl_union_pw_aff
*isl_union_pw_aff_mod_val(
7632 __isl_take isl_union_pw_aff
*upa
, __isl_take isl_val
*m
)
7634 isl_union_pw_aff
*res
;
7639 if (!isl_val_is_int(m
))
7640 isl_die(isl_val_get_ctx(m
), isl_error_invalid
,
7641 "expecting integer modulo", goto error
);
7642 if (!isl_val_is_pos(m
))
7643 isl_die(isl_val_get_ctx(m
), isl_error_invalid
,
7644 "expecting positive modulo", goto error
);
7646 res
= isl_union_pw_aff_copy(upa
);
7647 upa
= isl_union_pw_aff_scale_down_val(upa
, isl_val_copy(m
));
7648 upa
= isl_union_pw_aff_floor(upa
);
7649 upa
= isl_union_pw_aff_scale_val(upa
, m
);
7650 res
= isl_union_pw_aff_sub(res
, upa
);
7655 isl_union_pw_aff_free(upa
);
7659 /* Internal data structure for isl_union_pw_multi_aff_get_union_pw_aff.
7660 * pos is the output position that needs to be extracted.
7661 * res collects the results.
7663 struct isl_union_pw_multi_aff_get_union_pw_aff_data
{
7665 isl_union_pw_aff
*res
;
7668 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma"
7669 * (assuming it has such a dimension) and add it to data->res.
7671 static isl_stat
get_union_pw_aff(__isl_take isl_pw_multi_aff
*pma
, void *user
)
7673 struct isl_union_pw_multi_aff_get_union_pw_aff_data
*data
= user
;
7677 n_out
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
7679 return isl_stat_error
;
7680 if (data
->pos
>= n_out
) {
7681 isl_pw_multi_aff_free(pma
);
7685 pa
= isl_pw_multi_aff_get_pw_aff(pma
, data
->pos
);
7686 isl_pw_multi_aff_free(pma
);
7688 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
7690 return data
->res
? isl_stat_ok
: isl_stat_error
;
7693 /* Extract an isl_union_pw_aff corresponding to
7694 * output dimension "pos" of "upma".
7696 __isl_give isl_union_pw_aff
*isl_union_pw_multi_aff_get_union_pw_aff(
7697 __isl_keep isl_union_pw_multi_aff
*upma
, int pos
)
7699 struct isl_union_pw_multi_aff_get_union_pw_aff_data data
;
7706 isl_die(isl_union_pw_multi_aff_get_ctx(upma
), isl_error_invalid
,
7707 "cannot extract at negative position", return NULL
);
7709 space
= isl_union_pw_multi_aff_get_space(upma
);
7710 data
.res
= isl_union_pw_aff_empty(space
);
7712 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma
,
7713 &get_union_pw_aff
, &data
) < 0)
7714 data
.res
= isl_union_pw_aff_free(data
.res
);
7719 /* Return a union piecewise affine expression
7720 * that is equal to "aff" on "domain".
7722 __isl_give isl_union_pw_aff
*isl_union_pw_aff_aff_on_domain(
7723 __isl_take isl_union_set
*domain
, __isl_take isl_aff
*aff
)
7727 pa
= isl_pw_aff_from_aff(aff
);
7728 return isl_union_pw_aff_pw_aff_on_domain(domain
, pa
);
7731 /* Return a union piecewise affine expression
7732 * that is equal to the parameter identified by "id" on "domain".
7734 * Make sure the parameter appears in the space passed to
7735 * isl_aff_param_on_domain_space_id.
7737 __isl_give isl_union_pw_aff
*isl_union_pw_aff_param_on_domain_id(
7738 __isl_take isl_union_set
*domain
, __isl_take isl_id
*id
)
7743 space
= isl_union_set_get_space(domain
);
7744 space
= isl_space_add_param_id(space
, isl_id_copy(id
));
7745 aff
= isl_aff_param_on_domain_space_id(space
, id
);
7746 return isl_union_pw_aff_aff_on_domain(domain
, aff
);
7749 /* Internal data structure for isl_union_pw_aff_pw_aff_on_domain.
7750 * "pa" is the piecewise symbolic value that the resulting isl_union_pw_aff
7752 * "res" collects the results.
7754 struct isl_union_pw_aff_pw_aff_on_domain_data
{
7756 isl_union_pw_aff
*res
;
7759 /* Construct a piecewise affine expression that is equal to data->pa
7760 * on "domain" and add the result to data->res.
7762 static isl_stat
pw_aff_on_domain(__isl_take isl_set
*domain
, void *user
)
7764 struct isl_union_pw_aff_pw_aff_on_domain_data
*data
= user
;
7768 pa
= isl_pw_aff_copy(data
->pa
);
7769 dim
= isl_set_dim(domain
, isl_dim_set
);
7771 pa
= isl_pw_aff_free(pa
);
7772 pa
= isl_pw_aff_from_range(pa
);
7773 pa
= isl_pw_aff_add_dims(pa
, isl_dim_in
, dim
);
7774 pa
= isl_pw_aff_reset_domain_space(pa
, isl_set_get_space(domain
));
7775 pa
= isl_pw_aff_intersect_domain(pa
, domain
);
7776 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
7778 return data
->res
? isl_stat_ok
: isl_stat_error
;
7781 /* Return a union piecewise affine expression
7782 * that is equal to "pa" on "domain", assuming "domain" and "pa"
7783 * have been aligned.
7785 * Construct an isl_pw_aff on each of the sets in "domain" and
7786 * collect the results.
7788 static __isl_give isl_union_pw_aff
*isl_union_pw_aff_pw_aff_on_domain_aligned(
7789 __isl_take isl_union_set
*domain
, __isl_take isl_pw_aff
*pa
)
7791 struct isl_union_pw_aff_pw_aff_on_domain_data data
;
7794 space
= isl_union_set_get_space(domain
);
7795 data
.res
= isl_union_pw_aff_empty(space
);
7797 if (isl_union_set_foreach_set(domain
, &pw_aff_on_domain
, &data
) < 0)
7798 data
.res
= isl_union_pw_aff_free(data
.res
);
7799 isl_union_set_free(domain
);
7800 isl_pw_aff_free(pa
);
7804 /* Return a union piecewise affine expression
7805 * that is equal to "pa" on "domain".
7807 * Check that "pa" is a parametric expression,
7808 * align the parameters if needed and call
7809 * isl_union_pw_aff_pw_aff_on_domain_aligned.
7811 __isl_give isl_union_pw_aff
*isl_union_pw_aff_pw_aff_on_domain(
7812 __isl_take isl_union_set
*domain
, __isl_take isl_pw_aff
*pa
)
7815 isl_bool equal_params
;
7816 isl_space
*domain_space
, *pa_space
;
7818 pa_space
= isl_pw_aff_peek_space(pa
);
7819 is_set
= isl_space_is_set(pa_space
);
7823 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
7824 "expecting parametric expression", goto error
);
7826 domain_space
= isl_union_set_get_space(domain
);
7827 pa_space
= isl_pw_aff_get_space(pa
);
7828 equal_params
= isl_space_has_equal_params(domain_space
, pa_space
);
7829 if (equal_params
>= 0 && !equal_params
) {
7832 space
= isl_space_align_params(domain_space
, pa_space
);
7833 pa
= isl_pw_aff_align_params(pa
, isl_space_copy(space
));
7834 domain
= isl_union_set_align_params(domain
, space
);
7836 isl_space_free(domain_space
);
7837 isl_space_free(pa_space
);
7840 if (equal_params
< 0)
7842 return isl_union_pw_aff_pw_aff_on_domain_aligned(domain
, pa
);
7844 isl_union_set_free(domain
);
7845 isl_pw_aff_free(pa
);
7849 /* Internal data structure for isl_union_pw_aff_val_on_domain.
7850 * "v" is the value that the resulting isl_union_pw_aff needs to attain.
7851 * "res" collects the results.
7853 struct isl_union_pw_aff_val_on_domain_data
{
7855 isl_union_pw_aff
*res
;
7858 /* Construct a piecewise affine expression that is equal to data->v
7859 * on "domain" and add the result to data->res.
7861 static isl_stat
pw_aff_val_on_domain(__isl_take isl_set
*domain
, void *user
)
7863 struct isl_union_pw_aff_val_on_domain_data
*data
= user
;
7867 v
= isl_val_copy(data
->v
);
7868 pa
= isl_pw_aff_val_on_domain(domain
, v
);
7869 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
7871 return data
->res
? isl_stat_ok
: isl_stat_error
;
7874 /* Return a union piecewise affine expression
7875 * that is equal to "v" on "domain".
7877 * Construct an isl_pw_aff on each of the sets in "domain" and
7878 * collect the results.
7880 __isl_give isl_union_pw_aff
*isl_union_pw_aff_val_on_domain(
7881 __isl_take isl_union_set
*domain
, __isl_take isl_val
*v
)
7883 struct isl_union_pw_aff_val_on_domain_data data
;
7886 space
= isl_union_set_get_space(domain
);
7887 data
.res
= isl_union_pw_aff_empty(space
);
7889 if (isl_union_set_foreach_set(domain
, &pw_aff_val_on_domain
, &data
) < 0)
7890 data
.res
= isl_union_pw_aff_free(data
.res
);
7891 isl_union_set_free(domain
);
7896 /* Construct a piecewise multi affine expression
7897 * that is equal to "pa" and add it to upma.
7899 static isl_stat
pw_multi_aff_from_pw_aff_entry(__isl_take isl_pw_aff
*pa
,
7902 isl_union_pw_multi_aff
**upma
= user
;
7903 isl_pw_multi_aff
*pma
;
7905 pma
= isl_pw_multi_aff_from_pw_aff(pa
);
7906 *upma
= isl_union_pw_multi_aff_add_pw_multi_aff(*upma
, pma
);
7908 return *upma
? isl_stat_ok
: isl_stat_error
;
7911 /* Construct and return a union piecewise multi affine expression
7912 * that is equal to the given union piecewise affine expression.
7914 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_union_pw_aff(
7915 __isl_take isl_union_pw_aff
*upa
)
7918 isl_union_pw_multi_aff
*upma
;
7923 space
= isl_union_pw_aff_get_space(upa
);
7924 upma
= isl_union_pw_multi_aff_empty(space
);
7926 if (isl_union_pw_aff_foreach_pw_aff(upa
,
7927 &pw_multi_aff_from_pw_aff_entry
, &upma
) < 0)
7928 upma
= isl_union_pw_multi_aff_free(upma
);
7930 isl_union_pw_aff_free(upa
);
7934 /* Compute the set of elements in the domain of "pa" where it is zero and
7935 * add this set to "uset".
7937 static isl_stat
zero_union_set(__isl_take isl_pw_aff
*pa
, void *user
)
7939 isl_union_set
**uset
= (isl_union_set
**)user
;
7941 *uset
= isl_union_set_add_set(*uset
, isl_pw_aff_zero_set(pa
));
7943 return *uset
? isl_stat_ok
: isl_stat_error
;
7946 /* Return a union set containing those elements in the domain
7947 * of "upa" where it is zero.
7949 __isl_give isl_union_set
*isl_union_pw_aff_zero_union_set(
7950 __isl_take isl_union_pw_aff
*upa
)
7952 isl_union_set
*zero
;
7954 zero
= isl_union_set_empty(isl_union_pw_aff_get_space(upa
));
7955 if (isl_union_pw_aff_foreach_pw_aff(upa
, &zero_union_set
, &zero
) < 0)
7956 zero
= isl_union_set_free(zero
);
7958 isl_union_pw_aff_free(upa
);
7962 /* Internal data structure for isl_union_pw_aff_bind_id,
7963 * storing the parameter that needs to be bound and
7964 * the accumulated results.
7966 struct isl_bind_id_data
{
7968 isl_union_set
*bound
;
7971 /* Bind the piecewise affine function "pa" to the parameter data->id,
7972 * adding the resulting elements in the domain where the expression
7973 * is equal to the parameter to data->bound.
7975 static isl_stat
bind_id(__isl_take isl_pw_aff
*pa
, void *user
)
7977 struct isl_bind_id_data
*data
= user
;
7980 bound
= isl_pw_aff_bind_id(pa
, isl_id_copy(data
->id
));
7981 data
->bound
= isl_union_set_add_set(data
->bound
, bound
);
7983 return data
->bound
? isl_stat_ok
: isl_stat_error
;
7986 /* Bind the union piecewise affine function "upa" to the parameter "id",
7987 * returning the elements in the domain where the expression
7988 * is equal to the parameter.
7990 __isl_give isl_union_set
*isl_union_pw_aff_bind_id(
7991 __isl_take isl_union_pw_aff
*upa
, __isl_take isl_id
*id
)
7993 struct isl_bind_id_data data
= { id
};
7995 data
.bound
= isl_union_set_empty(isl_union_pw_aff_get_space(upa
));
7996 if (isl_union_pw_aff_foreach_pw_aff(upa
, &bind_id
, &data
) < 0)
7997 data
.bound
= isl_union_set_free(data
.bound
);
7999 isl_union_pw_aff_free(upa
);
8004 /* Internal data structure for isl_union_pw_aff_pullback_union_pw_multi_aff.
8005 * upma is the function that is plugged in.
8006 * pa is the current part of the function in which upma is plugged in.
8007 * res collects the results.
8009 struct isl_union_pw_aff_pullback_upma_data
{
8010 isl_union_pw_multi_aff
*upma
;
8012 isl_union_pw_aff
*res
;
8015 /* Check if "pma" can be plugged into data->pa.
8016 * If so, perform the pullback and add the result to data->res.
8018 static isl_stat
pa_pb_pma(__isl_take isl_pw_multi_aff
*pma
, void *user
)
8020 struct isl_union_pw_aff_pullback_upma_data
*data
= user
;
8023 if (!isl_space_tuple_is_equal(data
->pa
->dim
, isl_dim_in
,
8024 pma
->dim
, isl_dim_out
)) {
8025 isl_pw_multi_aff_free(pma
);
8029 pa
= isl_pw_aff_copy(data
->pa
);
8030 pa
= isl_pw_aff_pullback_pw_multi_aff(pa
, pma
);
8032 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
8034 return data
->res
? isl_stat_ok
: isl_stat_error
;
8037 /* Check if any of the elements of data->upma can be plugged into pa,
8038 * add if so add the result to data->res.
8040 static isl_stat
upa_pb_upma(__isl_take isl_pw_aff
*pa
, void *user
)
8042 struct isl_union_pw_aff_pullback_upma_data
*data
= user
;
8046 r
= isl_union_pw_multi_aff_foreach_pw_multi_aff(data
->upma
,
8048 isl_pw_aff_free(pa
);
8053 /* Compute the pullback of "upa" by the function represented by "upma".
8054 * In other words, plug in "upma" in "upa". The result contains
8055 * expressions defined over the domain space of "upma".
8057 * Run over all pairs of elements in "upa" and "upma", perform
8058 * the pullback when appropriate and collect the results.
8059 * If the hash value were based on the domain space rather than
8060 * the function space, then we could run through all elements
8061 * of "upma" and directly pick out the corresponding element of "upa".
8063 __isl_give isl_union_pw_aff
*isl_union_pw_aff_pullback_union_pw_multi_aff(
8064 __isl_take isl_union_pw_aff
*upa
,
8065 __isl_take isl_union_pw_multi_aff
*upma
)
8067 struct isl_union_pw_aff_pullback_upma_data data
= { NULL
, NULL
};
8070 space
= isl_union_pw_multi_aff_get_space(upma
);
8071 upa
= isl_union_pw_aff_align_params(upa
, space
);
8072 space
= isl_union_pw_aff_get_space(upa
);
8073 upma
= isl_union_pw_multi_aff_align_params(upma
, space
);
8079 data
.res
= isl_union_pw_aff_alloc_same_size(upa
);
8080 if (isl_union_pw_aff_foreach_pw_aff(upa
, &upa_pb_upma
, &data
) < 0)
8081 data
.res
= isl_union_pw_aff_free(data
.res
);
8083 isl_union_pw_aff_free(upa
);
8084 isl_union_pw_multi_aff_free(upma
);
8087 isl_union_pw_aff_free(upa
);
8088 isl_union_pw_multi_aff_free(upma
);
8093 #define BASE union_pw_aff
8095 #define DOMBASE union_set
8097 #include <isl_multi_explicit_domain.c>
8098 #include <isl_multi_union_pw_aff_explicit_domain.c>
8099 #include <isl_multi_templ.c>
8100 #include <isl_multi_apply_set.c>
8101 #include <isl_multi_apply_union_set.c>
8102 #include <isl_multi_arith_templ.c>
8103 #include <isl_multi_bind_templ.c>
8104 #include <isl_multi_coalesce.c>
8105 #include <isl_multi_dim_id_templ.c>
8106 #include <isl_multi_floor.c>
8107 #include <isl_multi_from_base_templ.c>
8108 #include <isl_multi_gist.c>
8109 #include <isl_multi_align_set.c>
8110 #include <isl_multi_align_union_set.c>
8111 #include <isl_multi_intersect.c>
8112 #include <isl_multi_nan_templ.c>
8113 #include <isl_multi_tuple_id_templ.c>
8114 #include <isl_multi_union_add_templ.c>
8116 /* Does "mupa" have a non-trivial explicit domain?
8118 * The explicit domain, if present, is trivial if it represents
8119 * an (obviously) universe parameter set.
8121 isl_bool
isl_multi_union_pw_aff_has_non_trivial_domain(
8122 __isl_keep isl_multi_union_pw_aff
*mupa
)
8124 isl_bool is_params
, trivial
;
8128 return isl_bool_error
;
8129 if (!isl_multi_union_pw_aff_has_explicit_domain(mupa
))
8130 return isl_bool_false
;
8131 is_params
= isl_union_set_is_params(mupa
->u
.dom
);
8132 if (is_params
< 0 || !is_params
)
8133 return isl_bool_not(is_params
);
8134 set
= isl_set_from_union_set(isl_union_set_copy(mupa
->u
.dom
));
8135 trivial
= isl_set_plain_is_universe(set
);
8137 return isl_bool_not(trivial
);
8140 /* Construct a multiple union piecewise affine expression
8141 * in the given space with value zero in each of the output dimensions.
8143 * Since there is no canonical zero value for
8144 * a union piecewise affine expression, we can only construct
8145 * a zero-dimensional "zero" value.
8147 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_zero(
8148 __isl_take isl_space
*space
)
8156 params
= isl_space_is_params(space
);
8160 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
8161 "expecting proper set space", goto error
);
8162 if (!isl_space_is_set(space
))
8163 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
8164 "expecting set space", goto error
);
8165 dim
= isl_space_dim(space
, isl_dim_out
);
8169 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
8170 "expecting 0D space", goto error
);
8172 return isl_multi_union_pw_aff_alloc(space
);
8174 isl_space_free(space
);
8178 /* Construct and return a multi union piecewise affine expression
8179 * that is equal to the given multi affine expression.
8181 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_from_multi_aff(
8182 __isl_take isl_multi_aff
*ma
)
8184 isl_multi_pw_aff
*mpa
;
8186 mpa
= isl_multi_pw_aff_from_multi_aff(ma
);
8187 return isl_multi_union_pw_aff_from_multi_pw_aff(mpa
);
8190 /* Construct and return a multi union piecewise affine expression
8191 * that is equal to the given multi piecewise affine expression.
8193 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_from_multi_pw_aff(
8194 __isl_take isl_multi_pw_aff
*mpa
)
8199 isl_multi_union_pw_aff
*mupa
;
8201 n
= isl_multi_pw_aff_dim(mpa
, isl_dim_out
);
8203 mpa
= isl_multi_pw_aff_free(mpa
);
8207 space
= isl_multi_pw_aff_get_space(mpa
);
8208 space
= isl_space_range(space
);
8209 mupa
= isl_multi_union_pw_aff_alloc(space
);
8211 for (i
= 0; i
< n
; ++i
) {
8213 isl_union_pw_aff
*upa
;
8215 pa
= isl_multi_pw_aff_get_pw_aff(mpa
, i
);
8216 upa
= isl_union_pw_aff_from_pw_aff(pa
);
8217 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
8220 isl_multi_pw_aff_free(mpa
);
8225 /* Extract the range space of "pma" and assign it to *space.
8226 * If *space has already been set (through a previous call to this function),
8227 * then check that the range space is the same.
8229 static isl_stat
extract_space(__isl_take isl_pw_multi_aff
*pma
, void *user
)
8231 isl_space
**space
= user
;
8232 isl_space
*pma_space
;
8235 pma_space
= isl_space_range(isl_pw_multi_aff_get_space(pma
));
8236 isl_pw_multi_aff_free(pma
);
8239 return isl_stat_error
;
8245 equal
= isl_space_is_equal(pma_space
, *space
);
8246 isl_space_free(pma_space
);
8249 return isl_stat_error
;
8251 isl_die(isl_space_get_ctx(*space
), isl_error_invalid
,
8252 "range spaces not the same", return isl_stat_error
);
8256 /* Construct and return a multi union piecewise affine expression
8257 * that is equal to the given union piecewise multi affine expression.
8259 * In order to be able to perform the conversion, the input
8260 * needs to be non-empty and may only involve a single range space.
8262 * If the resulting multi union piecewise affine expression has
8263 * an explicit domain, then assign it the domain of the input.
8264 * In other cases, the domain is stored in the individual elements.
8266 __isl_give isl_multi_union_pw_aff
*
8267 isl_multi_union_pw_aff_from_union_pw_multi_aff(
8268 __isl_take isl_union_pw_multi_aff
*upma
)
8270 isl_space
*space
= NULL
;
8271 isl_multi_union_pw_aff
*mupa
;
8275 n
= isl_union_pw_multi_aff_n_pw_multi_aff(upma
);
8279 isl_die(isl_union_pw_multi_aff_get_ctx(upma
), isl_error_invalid
,
8280 "cannot extract range space from empty input",
8282 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma
, &extract_space
,
8289 n
= isl_space_dim(space
, isl_dim_set
);
8291 space
= isl_space_free(space
);
8292 mupa
= isl_multi_union_pw_aff_alloc(space
);
8294 for (i
= 0; i
< n
; ++i
) {
8295 isl_union_pw_aff
*upa
;
8297 upa
= isl_union_pw_multi_aff_get_union_pw_aff(upma
, i
);
8298 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
8300 if (isl_multi_union_pw_aff_has_explicit_domain(mupa
)) {
8302 isl_union_pw_multi_aff
*copy
;
8304 copy
= isl_union_pw_multi_aff_copy(upma
);
8305 dom
= isl_union_pw_multi_aff_domain(copy
);
8306 mupa
= isl_multi_union_pw_aff_intersect_domain(mupa
, dom
);
8309 isl_union_pw_multi_aff_free(upma
);
8312 isl_space_free(space
);
8313 isl_union_pw_multi_aff_free(upma
);
8317 /* Try and create an isl_multi_union_pw_aff that is equivalent
8318 * to the given isl_union_map.
8319 * The isl_union_map is required to be single-valued in each space.
8320 * Moreover, it cannot be empty and all range spaces need to be the same.
8321 * Otherwise, an error is produced.
8323 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_from_union_map(
8324 __isl_take isl_union_map
*umap
)
8326 isl_union_pw_multi_aff
*upma
;
8328 upma
= isl_union_pw_multi_aff_from_union_map(umap
);
8329 return isl_multi_union_pw_aff_from_union_pw_multi_aff(upma
);
8332 /* Return a multiple union piecewise affine expression
8333 * that is equal to "mv" on "domain", assuming "domain" and "mv"
8334 * have been aligned.
8336 * If the resulting multi union piecewise affine expression has
8337 * an explicit domain, then assign it the input domain.
8338 * In other cases, the domain is stored in the individual elements.
8340 static __isl_give isl_multi_union_pw_aff
*
8341 isl_multi_union_pw_aff_multi_val_on_domain_aligned(
8342 __isl_take isl_union_set
*domain
, __isl_take isl_multi_val
*mv
)
8347 isl_multi_union_pw_aff
*mupa
;
8349 n
= isl_multi_val_dim(mv
, isl_dim_set
);
8350 if (!domain
|| n
< 0)
8353 space
= isl_multi_val_get_space(mv
);
8354 mupa
= isl_multi_union_pw_aff_alloc(space
);
8355 for (i
= 0; i
< n
; ++i
) {
8357 isl_union_pw_aff
*upa
;
8359 v
= isl_multi_val_get_val(mv
, i
);
8360 upa
= isl_union_pw_aff_val_on_domain(isl_union_set_copy(domain
),
8362 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
8364 if (isl_multi_union_pw_aff_has_explicit_domain(mupa
))
8365 mupa
= isl_multi_union_pw_aff_intersect_domain(mupa
,
8366 isl_union_set_copy(domain
));
8368 isl_union_set_free(domain
);
8369 isl_multi_val_free(mv
);
8372 isl_union_set_free(domain
);
8373 isl_multi_val_free(mv
);
8377 /* Return a multiple union piecewise affine expression
8378 * that is equal to "mv" on "domain".
8380 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_multi_val_on_domain(
8381 __isl_take isl_union_set
*domain
, __isl_take isl_multi_val
*mv
)
8383 isl_bool equal_params
;
8387 equal_params
= isl_space_has_equal_params(domain
->dim
, mv
->space
);
8388 if (equal_params
< 0)
8391 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(
8393 domain
= isl_union_set_align_params(domain
,
8394 isl_multi_val_get_space(mv
));
8395 mv
= isl_multi_val_align_params(mv
, isl_union_set_get_space(domain
));
8396 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(domain
, mv
);
8398 isl_union_set_free(domain
);
8399 isl_multi_val_free(mv
);
8403 /* Return a multiple union piecewise affine expression
8404 * that is equal to "ma" on "domain".
8406 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_multi_aff_on_domain(
8407 __isl_take isl_union_set
*domain
, __isl_take isl_multi_aff
*ma
)
8409 isl_pw_multi_aff
*pma
;
8411 pma
= isl_pw_multi_aff_from_multi_aff(ma
);
8412 return isl_multi_union_pw_aff_pw_multi_aff_on_domain(domain
, pma
);
8415 /* Return a multiple union piecewise affine expression
8416 * that is equal to "pma" on "domain", assuming "domain" and "pma"
8417 * have been aligned.
8419 * If the resulting multi union piecewise affine expression has
8420 * an explicit domain, then assign it the input domain.
8421 * In other cases, the domain is stored in the individual elements.
8423 static __isl_give isl_multi_union_pw_aff
*
8424 isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(
8425 __isl_take isl_union_set
*domain
, __isl_take isl_pw_multi_aff
*pma
)
8430 isl_multi_union_pw_aff
*mupa
;
8432 n
= isl_pw_multi_aff_dim(pma
, isl_dim_set
);
8433 if (!domain
|| n
< 0)
8435 space
= isl_pw_multi_aff_get_space(pma
);
8436 mupa
= isl_multi_union_pw_aff_alloc(space
);
8437 for (i
= 0; i
< n
; ++i
) {
8439 isl_union_pw_aff
*upa
;
8441 pa
= isl_pw_multi_aff_get_pw_aff(pma
, i
);
8442 upa
= isl_union_pw_aff_pw_aff_on_domain(
8443 isl_union_set_copy(domain
), pa
);
8444 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
8446 if (isl_multi_union_pw_aff_has_explicit_domain(mupa
))
8447 mupa
= isl_multi_union_pw_aff_intersect_domain(mupa
,
8448 isl_union_set_copy(domain
));
8450 isl_union_set_free(domain
);
8451 isl_pw_multi_aff_free(pma
);
8454 isl_union_set_free(domain
);
8455 isl_pw_multi_aff_free(pma
);
8459 /* Return a multiple union piecewise affine expression
8460 * that is equal to "pma" on "domain".
8462 __isl_give isl_multi_union_pw_aff
*
8463 isl_multi_union_pw_aff_pw_multi_aff_on_domain(__isl_take isl_union_set
*domain
,
8464 __isl_take isl_pw_multi_aff
*pma
)
8466 isl_bool equal_params
;
8469 space
= isl_pw_multi_aff_peek_space(pma
);
8470 equal_params
= isl_union_set_space_has_equal_params(domain
, space
);
8471 if (equal_params
< 0)
8474 return isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(
8476 domain
= isl_union_set_align_params(domain
,
8477 isl_pw_multi_aff_get_space(pma
));
8478 pma
= isl_pw_multi_aff_align_params(pma
,
8479 isl_union_set_get_space(domain
));
8480 return isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(domain
,
8483 isl_union_set_free(domain
);
8484 isl_pw_multi_aff_free(pma
);
8488 /* Return a union set containing those elements in the domains
8489 * of the elements of "mupa" where they are all zero.
8491 * If there are no elements, then simply return the entire domain.
8493 __isl_give isl_union_set
*isl_multi_union_pw_aff_zero_union_set(
8494 __isl_take isl_multi_union_pw_aff
*mupa
)
8498 isl_union_pw_aff
*upa
;
8499 isl_union_set
*zero
;
8501 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8503 mupa
= isl_multi_union_pw_aff_free(mupa
);
8508 return isl_multi_union_pw_aff_domain(mupa
);
8510 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, 0);
8511 zero
= isl_union_pw_aff_zero_union_set(upa
);
8513 for (i
= 1; i
< n
; ++i
) {
8514 isl_union_set
*zero_i
;
8516 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
8517 zero_i
= isl_union_pw_aff_zero_union_set(upa
);
8519 zero
= isl_union_set_intersect(zero
, zero_i
);
8522 isl_multi_union_pw_aff_free(mupa
);
8526 /* Construct a union map mapping the shared domain
8527 * of the union piecewise affine expressions to the range of "mupa"
8528 * in the special case of a 0D multi union piecewise affine expression.
8530 * Construct a map between the explicit domain of "mupa" and
8532 * Note that this assumes that the domain consists of explicit elements.
8534 static __isl_give isl_union_map
*isl_union_map_from_multi_union_pw_aff_0D(
8535 __isl_take isl_multi_union_pw_aff
*mupa
)
8539 isl_union_set
*dom
, *ran
;
8541 space
= isl_multi_union_pw_aff_get_space(mupa
);
8542 dom
= isl_multi_union_pw_aff_domain(mupa
);
8543 ran
= isl_union_set_from_set(isl_set_universe(space
));
8545 is_params
= isl_union_set_is_params(dom
);
8547 dom
= isl_union_set_free(dom
);
8549 isl_die(isl_union_set_get_ctx(dom
), isl_error_invalid
,
8550 "cannot create union map from expression without "
8551 "explicit domain elements",
8552 dom
= isl_union_set_free(dom
));
8554 return isl_union_map_from_domain_and_range(dom
, ran
);
8557 /* Construct a union map mapping the shared domain
8558 * of the union piecewise affine expressions to the range of "mupa"
8559 * with each dimension in the range equated to the
8560 * corresponding union piecewise affine expression.
8562 * If the input is zero-dimensional, then construct a mapping
8563 * from its explicit domain.
8565 __isl_give isl_union_map
*isl_union_map_from_multi_union_pw_aff(
8566 __isl_take isl_multi_union_pw_aff
*mupa
)
8571 isl_union_map
*umap
;
8572 isl_union_pw_aff
*upa
;
8574 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8576 mupa
= isl_multi_union_pw_aff_free(mupa
);
8581 return isl_union_map_from_multi_union_pw_aff_0D(mupa
);
8583 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, 0);
8584 umap
= isl_union_map_from_union_pw_aff(upa
);
8586 for (i
= 1; i
< n
; ++i
) {
8587 isl_union_map
*umap_i
;
8589 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
8590 umap_i
= isl_union_map_from_union_pw_aff(upa
);
8591 umap
= isl_union_map_flat_range_product(umap
, umap_i
);
8594 space
= isl_multi_union_pw_aff_get_space(mupa
);
8595 umap
= isl_union_map_reset_range_space(umap
, space
);
8597 isl_multi_union_pw_aff_free(mupa
);
8601 /* Internal data structure for isl_union_pw_multi_aff_reset_range_space.
8602 * "range" is the space from which to set the range space.
8603 * "res" collects the results.
8605 struct isl_union_pw_multi_aff_reset_range_space_data
{
8607 isl_union_pw_multi_aff
*res
;
8610 /* Replace the range space of "pma" by the range space of data->range and
8611 * add the result to data->res.
8613 static isl_stat
reset_range_space(__isl_take isl_pw_multi_aff
*pma
, void *user
)
8615 struct isl_union_pw_multi_aff_reset_range_space_data
*data
= user
;
8618 space
= isl_pw_multi_aff_get_space(pma
);
8619 space
= isl_space_domain(space
);
8620 space
= isl_space_extend_domain_with_range(space
,
8621 isl_space_copy(data
->range
));
8622 pma
= isl_pw_multi_aff_reset_space(pma
, space
);
8623 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma
);
8625 return data
->res
? isl_stat_ok
: isl_stat_error
;
8628 /* Replace the range space of all the piecewise affine expressions in "upma" by
8629 * the range space of "space".
8631 * This assumes that all these expressions have the same output dimension.
8633 * Since the spaces of the expressions change, so do their hash values.
8634 * We therefore need to create a new isl_union_pw_multi_aff.
8635 * Note that the hash value is currently computed based on the entire
8636 * space even though there can only be a single expression with a given
8639 static __isl_give isl_union_pw_multi_aff
*
8640 isl_union_pw_multi_aff_reset_range_space(
8641 __isl_take isl_union_pw_multi_aff
*upma
, __isl_take isl_space
*space
)
8643 struct isl_union_pw_multi_aff_reset_range_space_data data
= { space
};
8644 isl_space
*space_upma
;
8646 space_upma
= isl_union_pw_multi_aff_get_space(upma
);
8647 data
.res
= isl_union_pw_multi_aff_empty(space_upma
);
8648 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma
,
8649 &reset_range_space
, &data
) < 0)
8650 data
.res
= isl_union_pw_multi_aff_free(data
.res
);
8652 isl_space_free(space
);
8653 isl_union_pw_multi_aff_free(upma
);
8657 /* Construct and return a union piecewise multi affine expression
8658 * that is equal to the given multi union piecewise affine expression,
8659 * in the special case of a 0D multi union piecewise affine expression.
8661 * Construct a union piecewise multi affine expression
8662 * on top of the explicit domain of the input.
8664 __isl_give isl_union_pw_multi_aff
*
8665 isl_union_pw_multi_aff_from_multi_union_pw_aff_0D(
8666 __isl_take isl_multi_union_pw_aff
*mupa
)
8670 isl_union_set
*domain
;
8672 space
= isl_multi_union_pw_aff_get_space(mupa
);
8673 mv
= isl_multi_val_zero(space
);
8674 domain
= isl_multi_union_pw_aff_domain(mupa
);
8675 return isl_union_pw_multi_aff_multi_val_on_domain(domain
, mv
);
8678 /* Construct and return a union piecewise multi affine expression
8679 * that is equal to the given multi union piecewise affine expression.
8681 * If the input is zero-dimensional, then
8682 * construct a union piecewise multi affine expression
8683 * on top of the explicit domain of the input.
8685 __isl_give isl_union_pw_multi_aff
*
8686 isl_union_pw_multi_aff_from_multi_union_pw_aff(
8687 __isl_take isl_multi_union_pw_aff
*mupa
)
8692 isl_union_pw_multi_aff
*upma
;
8693 isl_union_pw_aff
*upa
;
8695 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8697 mupa
= isl_multi_union_pw_aff_free(mupa
);
8702 return isl_union_pw_multi_aff_from_multi_union_pw_aff_0D(mupa
);
8704 space
= isl_multi_union_pw_aff_get_space(mupa
);
8705 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, 0);
8706 upma
= isl_union_pw_multi_aff_from_union_pw_aff(upa
);
8708 for (i
= 1; i
< n
; ++i
) {
8709 isl_union_pw_multi_aff
*upma_i
;
8711 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
8712 upma_i
= isl_union_pw_multi_aff_from_union_pw_aff(upa
);
8713 upma
= isl_union_pw_multi_aff_flat_range_product(upma
, upma_i
);
8716 upma
= isl_union_pw_multi_aff_reset_range_space(upma
, space
);
8718 isl_multi_union_pw_aff_free(mupa
);
8722 /* Intersect the range of "mupa" with "range",
8723 * in the special case where "mupa" is 0D.
8725 * Intersect the domain of "mupa" with the constraints on the parameters
8728 static __isl_give isl_multi_union_pw_aff
*mupa_intersect_range_0D(
8729 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_set
*range
)
8731 range
= isl_set_params(range
);
8732 mupa
= isl_multi_union_pw_aff_intersect_params(mupa
, range
);
8736 /* Intersect the range of "mupa" with "range".
8737 * That is, keep only those domain elements that have a function value
8740 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_intersect_range(
8741 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_set
*range
)
8743 isl_union_pw_multi_aff
*upma
;
8744 isl_union_set
*domain
;
8749 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8750 if (n
< 0 || !range
)
8753 space
= isl_set_get_space(range
);
8754 match
= isl_space_tuple_is_equal(mupa
->space
, isl_dim_set
,
8755 space
, isl_dim_set
);
8756 isl_space_free(space
);
8760 isl_die(isl_multi_union_pw_aff_get_ctx(mupa
), isl_error_invalid
,
8761 "space don't match", goto error
);
8763 return mupa_intersect_range_0D(mupa
, range
);
8765 upma
= isl_union_pw_multi_aff_from_multi_union_pw_aff(
8766 isl_multi_union_pw_aff_copy(mupa
));
8767 domain
= isl_union_set_from_set(range
);
8768 domain
= isl_union_set_preimage_union_pw_multi_aff(domain
, upma
);
8769 mupa
= isl_multi_union_pw_aff_intersect_domain(mupa
, domain
);
8773 isl_multi_union_pw_aff_free(mupa
);
8774 isl_set_free(range
);
8778 /* Return the shared domain of the elements of "mupa",
8779 * in the special case where "mupa" is zero-dimensional.
8781 * Return the explicit domain of "mupa".
8782 * Note that this domain may be a parameter set, either
8783 * because "mupa" is meant to live in a set space or
8784 * because no explicit domain has been set.
8786 __isl_give isl_union_set
*isl_multi_union_pw_aff_domain_0D(
8787 __isl_take isl_multi_union_pw_aff
*mupa
)
8791 dom
= isl_multi_union_pw_aff_get_explicit_domain(mupa
);
8792 isl_multi_union_pw_aff_free(mupa
);
8797 /* Return the shared domain of the elements of "mupa".
8799 * If "mupa" is zero-dimensional, then return its explicit domain.
8801 __isl_give isl_union_set
*isl_multi_union_pw_aff_domain(
8802 __isl_take isl_multi_union_pw_aff
*mupa
)
8806 isl_union_pw_aff
*upa
;
8809 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8811 mupa
= isl_multi_union_pw_aff_free(mupa
);
8816 return isl_multi_union_pw_aff_domain_0D(mupa
);
8818 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, 0);
8819 dom
= isl_union_pw_aff_domain(upa
);
8820 for (i
= 1; i
< n
; ++i
) {
8821 isl_union_set
*dom_i
;
8823 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
8824 dom_i
= isl_union_pw_aff_domain(upa
);
8825 dom
= isl_union_set_intersect(dom
, dom_i
);
8828 isl_multi_union_pw_aff_free(mupa
);
8832 /* Apply "aff" to "mupa". The space of "mupa" is equal to the domain of "aff".
8833 * In particular, the spaces have been aligned.
8834 * The result is defined over the shared domain of the elements of "mupa"
8836 * We first extract the parametric constant part of "aff" and
8837 * define that over the shared domain.
8838 * Then we iterate over all input dimensions of "aff" and add the corresponding
8839 * multiples of the elements of "mupa".
8840 * Finally, we consider the integer divisions, calling the function
8841 * recursively to obtain an isl_union_pw_aff corresponding to the
8842 * integer division argument.
8844 static __isl_give isl_union_pw_aff
*multi_union_pw_aff_apply_aff(
8845 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_aff
*aff
)
8848 isl_size n_in
, n_div
;
8849 isl_union_pw_aff
*upa
;
8850 isl_union_set
*uset
;
8854 n_in
= isl_aff_dim(aff
, isl_dim_in
);
8855 n_div
= isl_aff_dim(aff
, isl_dim_div
);
8856 if (n_in
< 0 || n_div
< 0)
8859 uset
= isl_multi_union_pw_aff_domain(isl_multi_union_pw_aff_copy(mupa
));
8860 cst
= isl_aff_copy(aff
);
8861 cst
= isl_aff_drop_dims(cst
, isl_dim_div
, 0, n_div
);
8862 cst
= isl_aff_drop_dims(cst
, isl_dim_in
, 0, n_in
);
8863 cst
= isl_aff_project_domain_on_params(cst
);
8864 upa
= isl_union_pw_aff_aff_on_domain(uset
, cst
);
8866 for (i
= 0; i
< n_in
; ++i
) {
8867 isl_union_pw_aff
*upa_i
;
8869 if (!isl_aff_involves_dims(aff
, isl_dim_in
, i
, 1))
8871 v
= isl_aff_get_coefficient_val(aff
, isl_dim_in
, i
);
8872 upa_i
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
8873 upa_i
= isl_union_pw_aff_scale_val(upa_i
, v
);
8874 upa
= isl_union_pw_aff_add(upa
, upa_i
);
8877 for (i
= 0; i
< n_div
; ++i
) {
8879 isl_union_pw_aff
*upa_i
;
8881 if (!isl_aff_involves_dims(aff
, isl_dim_div
, i
, 1))
8883 div
= isl_aff_get_div(aff
, i
);
8884 upa_i
= multi_union_pw_aff_apply_aff(
8885 isl_multi_union_pw_aff_copy(mupa
), div
);
8886 upa_i
= isl_union_pw_aff_floor(upa_i
);
8887 v
= isl_aff_get_coefficient_val(aff
, isl_dim_div
, i
);
8888 upa_i
= isl_union_pw_aff_scale_val(upa_i
, v
);
8889 upa
= isl_union_pw_aff_add(upa
, upa_i
);
8892 isl_multi_union_pw_aff_free(mupa
);
8897 isl_multi_union_pw_aff_free(mupa
);
8902 /* Apply "aff" to "mupa". The space of "mupa" needs to be compatible
8903 * with the domain of "aff".
8904 * Furthermore, the dimension of this space needs to be greater than zero.
8905 * The result is defined over the shared domain of the elements of "mupa"
8907 * We perform these checks and then hand over control to
8908 * multi_union_pw_aff_apply_aff.
8910 __isl_give isl_union_pw_aff
*isl_multi_union_pw_aff_apply_aff(
8911 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_aff
*aff
)
8914 isl_space
*space1
, *space2
;
8917 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
8918 isl_aff_get_space(aff
));
8919 aff
= isl_aff_align_params(aff
, isl_multi_union_pw_aff_get_space(mupa
));
8923 space1
= isl_multi_union_pw_aff_get_space(mupa
);
8924 space2
= isl_aff_get_domain_space(aff
);
8925 equal
= isl_space_is_equal(space1
, space2
);
8926 isl_space_free(space1
);
8927 isl_space_free(space2
);
8931 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
8932 "spaces don't match", goto error
);
8933 dim
= isl_aff_dim(aff
, isl_dim_in
);
8937 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
8938 "cannot determine domains", goto error
);
8940 return multi_union_pw_aff_apply_aff(mupa
, aff
);
8942 isl_multi_union_pw_aff_free(mupa
);
8947 /* Apply "ma" to "mupa", in the special case where "mupa" is 0D.
8948 * The space of "mupa" is known to be compatible with the domain of "ma".
8950 * Construct an isl_multi_union_pw_aff that is equal to "ma"
8951 * on the domain of "mupa".
8953 static __isl_give isl_multi_union_pw_aff
*mupa_apply_multi_aff_0D(
8954 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_multi_aff
*ma
)
8958 dom
= isl_multi_union_pw_aff_domain(mupa
);
8959 ma
= isl_multi_aff_project_domain_on_params(ma
);
8961 return isl_multi_union_pw_aff_multi_aff_on_domain(dom
, ma
);
8964 /* Apply "ma" to "mupa". The space of "mupa" needs to be compatible
8965 * with the domain of "ma".
8966 * The result is defined over the shared domain of the elements of "mupa"
8968 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_apply_multi_aff(
8969 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_multi_aff
*ma
)
8971 isl_space
*space1
, *space2
;
8972 isl_multi_union_pw_aff
*res
;
8975 isl_size n_in
, n_out
;
8977 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
8978 isl_multi_aff_get_space(ma
));
8979 ma
= isl_multi_aff_align_params(ma
,
8980 isl_multi_union_pw_aff_get_space(mupa
));
8981 n_in
= isl_multi_aff_dim(ma
, isl_dim_in
);
8982 n_out
= isl_multi_aff_dim(ma
, isl_dim_out
);
8983 if (!mupa
|| n_in
< 0 || n_out
< 0)
8986 space1
= isl_multi_union_pw_aff_get_space(mupa
);
8987 space2
= isl_multi_aff_get_domain_space(ma
);
8988 equal
= isl_space_is_equal(space1
, space2
);
8989 isl_space_free(space1
);
8990 isl_space_free(space2
);
8994 isl_die(isl_multi_aff_get_ctx(ma
), isl_error_invalid
,
8995 "spaces don't match", goto error
);
8997 return mupa_apply_multi_aff_0D(mupa
, ma
);
8999 space1
= isl_space_range(isl_multi_aff_get_space(ma
));
9000 res
= isl_multi_union_pw_aff_alloc(space1
);
9002 for (i
= 0; i
< n_out
; ++i
) {
9004 isl_union_pw_aff
*upa
;
9006 aff
= isl_multi_aff_get_aff(ma
, i
);
9007 upa
= multi_union_pw_aff_apply_aff(
9008 isl_multi_union_pw_aff_copy(mupa
), aff
);
9009 res
= isl_multi_union_pw_aff_set_union_pw_aff(res
, i
, upa
);
9012 isl_multi_aff_free(ma
);
9013 isl_multi_union_pw_aff_free(mupa
);
9016 isl_multi_union_pw_aff_free(mupa
);
9017 isl_multi_aff_free(ma
);
9021 /* Apply "pa" to "mupa", in the special case where "mupa" is 0D.
9022 * The space of "mupa" is known to be compatible with the domain of "pa".
9024 * Construct an isl_multi_union_pw_aff that is equal to "pa"
9025 * on the domain of "mupa".
9027 static __isl_give isl_union_pw_aff
*isl_multi_union_pw_aff_apply_pw_aff_0D(
9028 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_pw_aff
*pa
)
9032 dom
= isl_multi_union_pw_aff_domain(mupa
);
9033 pa
= isl_pw_aff_project_domain_on_params(pa
);
9035 return isl_union_pw_aff_pw_aff_on_domain(dom
, pa
);
9038 /* Apply "pa" to "mupa". The space of "mupa" needs to be compatible
9039 * with the domain of "pa".
9040 * Furthermore, the dimension of this space needs to be greater than zero.
9041 * The result is defined over the shared domain of the elements of "mupa"
9043 __isl_give isl_union_pw_aff
*isl_multi_union_pw_aff_apply_pw_aff(
9044 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_pw_aff
*pa
)
9049 isl_space
*space
, *space2
;
9050 isl_union_pw_aff
*upa
;
9052 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
9053 isl_pw_aff_get_space(pa
));
9054 pa
= isl_pw_aff_align_params(pa
,
9055 isl_multi_union_pw_aff_get_space(mupa
));
9059 space
= isl_multi_union_pw_aff_get_space(mupa
);
9060 space2
= isl_pw_aff_get_domain_space(pa
);
9061 equal
= isl_space_is_equal(space
, space2
);
9062 isl_space_free(space
);
9063 isl_space_free(space2
);
9067 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
9068 "spaces don't match", goto error
);
9069 n_in
= isl_pw_aff_dim(pa
, isl_dim_in
);
9073 return isl_multi_union_pw_aff_apply_pw_aff_0D(mupa
, pa
);
9075 space
= isl_space_params(isl_multi_union_pw_aff_get_space(mupa
));
9076 upa
= isl_union_pw_aff_empty(space
);
9078 for (i
= 0; i
< pa
->n
; ++i
) {
9081 isl_multi_union_pw_aff
*mupa_i
;
9082 isl_union_pw_aff
*upa_i
;
9084 mupa_i
= isl_multi_union_pw_aff_copy(mupa
);
9085 domain
= isl_set_copy(pa
->p
[i
].set
);
9086 mupa_i
= isl_multi_union_pw_aff_intersect_range(mupa_i
, domain
);
9087 aff
= isl_aff_copy(pa
->p
[i
].aff
);
9088 upa_i
= multi_union_pw_aff_apply_aff(mupa_i
, aff
);
9089 upa
= isl_union_pw_aff_union_add(upa
, upa_i
);
9092 isl_multi_union_pw_aff_free(mupa
);
9093 isl_pw_aff_free(pa
);
9096 isl_multi_union_pw_aff_free(mupa
);
9097 isl_pw_aff_free(pa
);
9101 /* Apply "pma" to "mupa", in the special case where "mupa" is 0D.
9102 * The space of "mupa" is known to be compatible with the domain of "pma".
9104 * Construct an isl_multi_union_pw_aff that is equal to "pma"
9105 * on the domain of "mupa".
9107 static __isl_give isl_multi_union_pw_aff
*mupa_apply_pw_multi_aff_0D(
9108 __isl_take isl_multi_union_pw_aff
*mupa
,
9109 __isl_take isl_pw_multi_aff
*pma
)
9113 dom
= isl_multi_union_pw_aff_domain(mupa
);
9114 pma
= isl_pw_multi_aff_project_domain_on_params(pma
);
9116 return isl_multi_union_pw_aff_pw_multi_aff_on_domain(dom
, pma
);
9119 /* Apply "pma" to "mupa". The space of "mupa" needs to be compatible
9120 * with the domain of "pma".
9121 * The result is defined over the shared domain of the elements of "mupa"
9123 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_apply_pw_multi_aff(
9124 __isl_take isl_multi_union_pw_aff
*mupa
,
9125 __isl_take isl_pw_multi_aff
*pma
)
9127 isl_space
*space1
, *space2
;
9128 isl_multi_union_pw_aff
*res
;
9131 isl_size n_in
, n_out
;
9133 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
9134 isl_pw_multi_aff_get_space(pma
));
9135 pma
= isl_pw_multi_aff_align_params(pma
,
9136 isl_multi_union_pw_aff_get_space(mupa
));
9140 space1
= isl_multi_union_pw_aff_get_space(mupa
);
9141 space2
= isl_pw_multi_aff_get_domain_space(pma
);
9142 equal
= isl_space_is_equal(space1
, space2
);
9143 isl_space_free(space1
);
9144 isl_space_free(space2
);
9148 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
9149 "spaces don't match", goto error
);
9150 n_in
= isl_pw_multi_aff_dim(pma
, isl_dim_in
);
9151 n_out
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
9152 if (n_in
< 0 || n_out
< 0)
9155 return mupa_apply_pw_multi_aff_0D(mupa
, pma
);
9157 space1
= isl_space_range(isl_pw_multi_aff_get_space(pma
));
9158 res
= isl_multi_union_pw_aff_alloc(space1
);
9160 for (i
= 0; i
< n_out
; ++i
) {
9162 isl_union_pw_aff
*upa
;
9164 pa
= isl_pw_multi_aff_get_pw_aff(pma
, i
);
9165 upa
= isl_multi_union_pw_aff_apply_pw_aff(
9166 isl_multi_union_pw_aff_copy(mupa
), pa
);
9167 res
= isl_multi_union_pw_aff_set_union_pw_aff(res
, i
, upa
);
9170 isl_pw_multi_aff_free(pma
);
9171 isl_multi_union_pw_aff_free(mupa
);
9174 isl_multi_union_pw_aff_free(mupa
);
9175 isl_pw_multi_aff_free(pma
);
9179 /* Replace the explicit domain of "mupa" by its preimage under "upma".
9180 * If the explicit domain only keeps track of constraints on the parameters,
9181 * then only update those constraints.
9183 static __isl_give isl_multi_union_pw_aff
*preimage_explicit_domain(
9184 __isl_take isl_multi_union_pw_aff
*mupa
,
9185 __isl_keep isl_union_pw_multi_aff
*upma
)
9189 if (isl_multi_union_pw_aff_check_has_explicit_domain(mupa
) < 0)
9190 return isl_multi_union_pw_aff_free(mupa
);
9192 mupa
= isl_multi_union_pw_aff_cow(mupa
);
9196 is_params
= isl_union_set_is_params(mupa
->u
.dom
);
9198 return isl_multi_union_pw_aff_free(mupa
);
9200 upma
= isl_union_pw_multi_aff_copy(upma
);
9202 mupa
->u
.dom
= isl_union_set_intersect_params(mupa
->u
.dom
,
9203 isl_union_set_params(isl_union_pw_multi_aff_domain(upma
)));
9205 mupa
->u
.dom
= isl_union_set_preimage_union_pw_multi_aff(
9208 return isl_multi_union_pw_aff_free(mupa
);
9212 /* Compute the pullback of "mupa" by the function represented by "upma".
9213 * In other words, plug in "upma" in "mupa". The result contains
9214 * expressions defined over the domain space of "upma".
9216 * Run over all elements of "mupa" and plug in "upma" in each of them.
9218 * If "mupa" has an explicit domain, then it is this domain
9219 * that needs to undergo a pullback instead, i.e., a preimage.
9221 __isl_give isl_multi_union_pw_aff
*
9222 isl_multi_union_pw_aff_pullback_union_pw_multi_aff(
9223 __isl_take isl_multi_union_pw_aff
*mupa
,
9224 __isl_take isl_union_pw_multi_aff
*upma
)
9229 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
9230 isl_union_pw_multi_aff_get_space(upma
));
9231 upma
= isl_union_pw_multi_aff_align_params(upma
,
9232 isl_multi_union_pw_aff_get_space(mupa
));
9233 mupa
= isl_multi_union_pw_aff_cow(mupa
);
9234 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
9238 for (i
= 0; i
< n
; ++i
) {
9239 isl_union_pw_aff
*upa
;
9241 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
9242 upa
= isl_union_pw_aff_pullback_union_pw_multi_aff(upa
,
9243 isl_union_pw_multi_aff_copy(upma
));
9244 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
9247 if (isl_multi_union_pw_aff_has_explicit_domain(mupa
))
9248 mupa
= preimage_explicit_domain(mupa
, upma
);
9250 isl_union_pw_multi_aff_free(upma
);
9253 isl_multi_union_pw_aff_free(mupa
);
9254 isl_union_pw_multi_aff_free(upma
);
9258 /* Extract the sequence of elements in "mupa" with domain space "space"
9259 * (ignoring parameters).
9261 * For the elements of "mupa" that are not defined on the specified space,
9262 * the corresponding element in the result is empty.
9264 __isl_give isl_multi_pw_aff
*isl_multi_union_pw_aff_extract_multi_pw_aff(
9265 __isl_keep isl_multi_union_pw_aff
*mupa
, __isl_take isl_space
*space
)
9269 isl_space
*space_mpa
;
9270 isl_multi_pw_aff
*mpa
;
9272 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
9273 if (n
< 0 || !space
)
9276 space_mpa
= isl_multi_union_pw_aff_get_space(mupa
);
9277 space
= isl_space_replace_params(space
, space_mpa
);
9278 space_mpa
= isl_space_map_from_domain_and_range(isl_space_copy(space
),
9280 mpa
= isl_multi_pw_aff_alloc(space_mpa
);
9282 space
= isl_space_from_domain(space
);
9283 space
= isl_space_add_dims(space
, isl_dim_out
, 1);
9284 for (i
= 0; i
< n
; ++i
) {
9285 isl_union_pw_aff
*upa
;
9288 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
9289 pa
= isl_union_pw_aff_extract_pw_aff(upa
,
9290 isl_space_copy(space
));
9291 mpa
= isl_multi_pw_aff_set_pw_aff(mpa
, i
, pa
);
9292 isl_union_pw_aff_free(upa
);
9295 isl_space_free(space
);
9298 isl_space_free(space
);
9302 /* Data structure that specifies how isl_union_pw_multi_aff_un_op
9303 * should modify the base expressions in the input.
9305 * If "filter" is not NULL, then only the base expressions that satisfy "filter"
9306 * are taken into account.
9307 * "fn" is applied to each entry in the input.
9309 struct isl_union_pw_multi_aff_un_op_control
{
9310 isl_bool (*filter
)(__isl_keep isl_pw_multi_aff
*part
);
9311 __isl_give isl_pw_multi_aff
*(*fn
)(__isl_take isl_pw_multi_aff
*pma
);
9314 /* Wrapper for isl_union_pw_multi_aff_un_op base functions (which do not take
9315 * a second argument) for use as an isl_union_pw_multi_aff_transform
9316 * base function (which does take a second argument).
9317 * Simply call control->fn without the second argument.
9319 static __isl_give isl_pw_multi_aff
*isl_union_pw_multi_aff_un_op_drop_user(
9320 __isl_take isl_pw_multi_aff
*pma
, void *user
)
9322 struct isl_union_pw_multi_aff_un_op_control
*control
= user
;
9324 return control
->fn(pma
);
9327 /* Construct an isl_union_pw_multi_aff that is obtained by
9328 * modifying "upma" according to "control".
9330 * isl_union_pw_multi_aff_transform performs essentially
9331 * the same operation, but takes a callback function
9332 * of a different form (with an extra argument).
9333 * Call isl_union_pw_multi_aff_transform with a wrapper
9334 * that removes this extra argument.
9336 static __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_un_op(
9337 __isl_take isl_union_pw_multi_aff
*upma
,
9338 struct isl_union_pw_multi_aff_un_op_control
*control
)
9340 struct isl_union_pw_multi_aff_transform_control t_control
= {
9341 .filter
= control
->filter
,
9342 .fn
= &isl_union_pw_multi_aff_un_op_drop_user
,
9346 return isl_union_pw_multi_aff_transform(upma
, &t_control
);
9349 /* For each function in "upma" of the form A -> [B -> C],
9350 * extract the function A -> B and collect the results.
9352 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_range_factor_domain(
9353 __isl_take isl_union_pw_multi_aff
*upma
)
9355 struct isl_union_pw_multi_aff_un_op_control control
= {
9356 .filter
= &isl_pw_multi_aff_range_is_wrapping
,
9357 .fn
= &isl_pw_multi_aff_range_factor_domain
,
9359 return isl_union_pw_multi_aff_un_op(upma
, &control
);
9362 /* For each function in "upma" of the form A -> [B -> C],
9363 * extract the function A -> C and collect the results.
9365 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_range_factor_range(
9366 __isl_take isl_union_pw_multi_aff
*upma
)
9368 struct isl_union_pw_multi_aff_un_op_control control
= {
9369 .filter
= &isl_pw_multi_aff_range_is_wrapping
,
9370 .fn
= &isl_pw_multi_aff_range_factor_range
,
9372 return isl_union_pw_multi_aff_un_op(upma
, &control
);
9375 /* Evaluate the affine function "aff" in the void point "pnt".
9376 * In particular, return the value NaN.
9378 static __isl_give isl_val
*eval_void(__isl_take isl_aff
*aff
,
9379 __isl_take isl_point
*pnt
)
9383 ctx
= isl_point_get_ctx(pnt
);
9385 isl_point_free(pnt
);
9386 return isl_val_nan(ctx
);
9389 /* Evaluate the affine expression "aff"
9390 * in the coordinates (with denominator) "pnt".
9392 static __isl_give isl_val
*eval(__isl_keep isl_vec
*aff
,
9393 __isl_keep isl_vec
*pnt
)
9402 ctx
= isl_vec_get_ctx(aff
);
9405 isl_seq_inner_product(aff
->el
+ 1, pnt
->el
, pnt
->size
, &n
);
9406 isl_int_mul(d
, aff
->el
[0], pnt
->el
[0]);
9407 v
= isl_val_rat_from_isl_int(ctx
, n
, d
);
9408 v
= isl_val_normalize(v
);
9415 /* Check that the domain space of "aff" is equal to "space".
9417 static isl_stat
isl_aff_check_has_domain_space(__isl_keep isl_aff
*aff
,
9418 __isl_keep isl_space
*space
)
9422 ok
= isl_space_is_equal(isl_aff_peek_domain_space(aff
), space
);
9424 return isl_stat_error
;
9426 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
9427 "incompatible spaces", return isl_stat_error
);
9431 /* Evaluate the affine function "aff" in "pnt".
9433 __isl_give isl_val
*isl_aff_eval(__isl_take isl_aff
*aff
,
9434 __isl_take isl_point
*pnt
)
9438 isl_local_space
*ls
;
9440 if (isl_aff_check_has_domain_space(aff
, isl_point_peek_space(pnt
)) < 0)
9442 is_void
= isl_point_is_void(pnt
);
9446 return eval_void(aff
, pnt
);
9448 ls
= isl_aff_get_domain_local_space(aff
);
9449 pnt
= isl_local_space_lift_point(ls
, pnt
);
9451 v
= eval(aff
->v
, isl_point_peek_vec(pnt
));
9454 isl_point_free(pnt
);
9459 isl_point_free(pnt
);