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,2020 Cerebras Systems
8 * Copyright 2021 Sven Verdoolaege
10 * Use of this software is governed by the MIT license
12 * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
13 * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
15 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
16 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
17 * B.P. 105 - 78153 Le Chesnay, France
18 * and Cerebras Systems, 175 S San Antonio Rd, Los Altos, CA, USA
21 #include <isl_ctx_private.h>
22 #include <isl_map_private.h>
23 #include <isl_union_map_private.h>
24 #include <isl_aff_private.h>
25 #include <isl_space_private.h>
26 #include <isl_local_space_private.h>
27 #include <isl_vec_private.h>
28 #include <isl_mat_private.h>
29 #include <isl_id_private.h>
30 #include <isl/constraint.h>
33 #include <isl_val_private.h>
34 #include <isl_point_private.h>
35 #include <isl_config.h>
40 #include <isl_list_templ.c>
41 #include <isl_list_read_templ.c>
44 #define EL_BASE pw_aff
46 #include <isl_list_templ.c>
47 #include <isl_list_read_templ.c>
50 #define EL_BASE pw_multi_aff
52 #include <isl_list_templ.c>
53 #include <isl_list_read_templ.c>
56 #define EL_BASE union_pw_aff
58 #include <isl_list_templ.c>
59 #include <isl_list_read_templ.c>
62 #define EL_BASE union_pw_multi_aff
64 #include <isl_list_templ.c>
66 /* Construct an isl_aff from the given domain local space "ls" and
67 * coefficients "v", where the local space is known to be valid
68 * for an affine expression.
70 static __isl_give isl_aff
*isl_aff_alloc_vec_validated(
71 __isl_take isl_local_space
*ls
, __isl_take isl_vec
*v
)
78 aff
= isl_calloc_type(v
->ctx
, struct isl_aff
);
88 isl_local_space_free(ls
);
93 /* Construct an isl_aff from the given domain local space "ls" and
96 * First check that "ls" is a valid domain local space
97 * for an affine expression.
99 __isl_give isl_aff
*isl_aff_alloc_vec(__isl_take isl_local_space
*ls
,
100 __isl_take isl_vec
*v
)
107 ctx
= isl_local_space_get_ctx(ls
);
108 if (!isl_local_space_divs_known(ls
))
109 isl_die(ctx
, isl_error_invalid
, "local space has unknown divs",
111 if (!isl_local_space_is_set(ls
))
112 isl_die(ctx
, isl_error_invalid
,
113 "domain of affine expression should be a set",
115 return isl_aff_alloc_vec_validated(ls
, v
);
117 isl_local_space_free(ls
);
122 __isl_give isl_aff
*isl_aff_alloc(__isl_take isl_local_space
*ls
)
131 ctx
= isl_local_space_get_ctx(ls
);
133 total
= isl_local_space_dim(ls
, isl_dim_all
);
136 v
= isl_vec_alloc(ctx
, 1 + 1 + total
);
137 return isl_aff_alloc_vec(ls
, v
);
139 isl_local_space_free(ls
);
143 __isl_give isl_aff
*isl_aff_copy(__isl_keep isl_aff
*aff
)
152 __isl_give isl_aff
*isl_aff_dup(__isl_keep isl_aff
*aff
)
157 return isl_aff_alloc_vec_validated(isl_local_space_copy(aff
->ls
),
158 isl_vec_copy(aff
->v
));
161 __isl_give isl_aff
*isl_aff_cow(__isl_take isl_aff
*aff
)
169 return isl_aff_dup(aff
);
172 __isl_give isl_aff
*isl_aff_zero_on_domain(__isl_take isl_local_space
*ls
)
176 aff
= isl_aff_alloc(ls
);
180 isl_int_set_si(aff
->v
->el
[0], 1);
181 isl_seq_clr(aff
->v
->el
+ 1, aff
->v
->size
- 1);
186 /* Return an affine expression that is equal to zero on domain space "space".
188 __isl_give isl_aff
*isl_aff_zero_on_domain_space(__isl_take isl_space
*space
)
190 return isl_aff_zero_on_domain(isl_local_space_from_space(space
));
193 /* This function performs the same operation as isl_aff_zero_on_domain_space,
194 * but is considered as a function on an isl_space when exported.
196 __isl_give isl_aff
*isl_space_zero_aff_on_domain(__isl_take isl_space
*space
)
198 return isl_aff_zero_on_domain_space(space
);
201 /* Return a piecewise affine expression defined on the specified domain
202 * that is equal to zero.
204 __isl_give isl_pw_aff
*isl_pw_aff_zero_on_domain(__isl_take isl_local_space
*ls
)
206 return isl_pw_aff_from_aff(isl_aff_zero_on_domain(ls
));
209 /* Change "aff" into a NaN.
211 * Note that this function gets called from isl_aff_nan_on_domain,
212 * so "aff" may not have been initialized yet.
214 static __isl_give isl_aff
*isl_aff_set_nan(__isl_take isl_aff
*aff
)
216 aff
= isl_aff_cow(aff
);
220 aff
->v
= isl_vec_clr(aff
->v
);
222 return isl_aff_free(aff
);
227 /* Return an affine expression defined on the specified domain
228 * that represents NaN.
230 __isl_give isl_aff
*isl_aff_nan_on_domain(__isl_take isl_local_space
*ls
)
234 aff
= isl_aff_alloc(ls
);
235 return isl_aff_set_nan(aff
);
238 /* Return an affine expression defined on the specified domain space
239 * that represents NaN.
241 __isl_give isl_aff
*isl_aff_nan_on_domain_space(__isl_take isl_space
*space
)
243 return isl_aff_nan_on_domain(isl_local_space_from_space(space
));
246 /* Return a piecewise affine expression defined on the specified domain space
247 * that represents NaN.
249 __isl_give isl_pw_aff
*isl_pw_aff_nan_on_domain_space(
250 __isl_take isl_space
*space
)
252 return isl_pw_aff_from_aff(isl_aff_nan_on_domain_space(space
));
255 /* Return a piecewise affine expression defined on the specified domain
256 * that represents NaN.
258 __isl_give isl_pw_aff
*isl_pw_aff_nan_on_domain(__isl_take isl_local_space
*ls
)
260 return isl_pw_aff_from_aff(isl_aff_nan_on_domain(ls
));
263 /* Return an affine expression that is equal to "val" on
264 * domain local space "ls".
266 * Note that the encoding for the special value NaN
267 * is the same in isl_val and isl_aff, so this does not need
268 * to be treated in any special way.
270 __isl_give isl_aff
*isl_aff_val_on_domain(__isl_take isl_local_space
*ls
,
271 __isl_take isl_val
*val
)
277 if (!isl_val_is_rat(val
) && !isl_val_is_nan(val
))
278 isl_die(isl_val_get_ctx(val
), isl_error_invalid
,
279 "expecting rational value or NaN", goto error
);
281 aff
= isl_aff_alloc(isl_local_space_copy(ls
));
285 isl_seq_clr(aff
->v
->el
+ 2, aff
->v
->size
- 2);
286 isl_int_set(aff
->v
->el
[1], val
->n
);
287 isl_int_set(aff
->v
->el
[0], val
->d
);
289 isl_local_space_free(ls
);
293 isl_local_space_free(ls
);
298 /* Return an affine expression that is equal to "val" on domain space "space".
300 __isl_give isl_aff
*isl_aff_val_on_domain_space(__isl_take isl_space
*space
,
301 __isl_take isl_val
*val
)
303 return isl_aff_val_on_domain(isl_local_space_from_space(space
), val
);
306 /* Return an affine expression that is equal to the specified dimension
309 __isl_give isl_aff
*isl_aff_var_on_domain(__isl_take isl_local_space
*ls
,
310 enum isl_dim_type type
, unsigned pos
)
318 space
= isl_local_space_get_space(ls
);
321 if (isl_space_is_map(space
))
322 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
323 "expecting (parameter) set space", goto error
);
324 if (isl_local_space_check_range(ls
, type
, pos
, 1) < 0)
327 isl_space_free(space
);
328 aff
= isl_aff_alloc(ls
);
332 pos
+= isl_local_space_offset(aff
->ls
, type
);
334 isl_int_set_si(aff
->v
->el
[0], 1);
335 isl_seq_clr(aff
->v
->el
+ 1, aff
->v
->size
- 1);
336 isl_int_set_si(aff
->v
->el
[1 + pos
], 1);
340 isl_local_space_free(ls
);
341 isl_space_free(space
);
345 /* Return a piecewise affine expression that is equal to
346 * the specified dimension in "ls".
348 __isl_give isl_pw_aff
*isl_pw_aff_var_on_domain(__isl_take isl_local_space
*ls
,
349 enum isl_dim_type type
, unsigned pos
)
351 return isl_pw_aff_from_aff(isl_aff_var_on_domain(ls
, type
, pos
));
354 /* Return an affine expression that is equal to the parameter
355 * in the domain space "space" with identifier "id".
357 __isl_give isl_aff
*isl_aff_param_on_domain_space_id(
358 __isl_take isl_space
*space
, __isl_take isl_id
*id
)
365 pos
= isl_space_find_dim_by_id(space
, isl_dim_param
, id
);
367 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
368 "parameter not found in space", goto error
);
370 ls
= isl_local_space_from_space(space
);
371 return isl_aff_var_on_domain(ls
, isl_dim_param
, pos
);
373 isl_space_free(space
);
378 /* This function performs the same operation as
379 * isl_aff_param_on_domain_space_id,
380 * but is considered as a function on an isl_space when exported.
382 __isl_give isl_aff
*isl_space_param_aff_on_domain_id(
383 __isl_take isl_space
*space
, __isl_take isl_id
*id
)
385 return isl_aff_param_on_domain_space_id(space
, id
);
388 __isl_null isl_aff
*isl_aff_free(__isl_take isl_aff
*aff
)
396 isl_local_space_free(aff
->ls
);
397 isl_vec_free(aff
->v
);
404 isl_ctx
*isl_aff_get_ctx(__isl_keep isl_aff
*aff
)
406 return aff
? isl_local_space_get_ctx(aff
->ls
) : NULL
;
409 /* Return a hash value that digests "aff".
411 uint32_t isl_aff_get_hash(__isl_keep isl_aff
*aff
)
413 uint32_t hash
, ls_hash
, v_hash
;
418 hash
= isl_hash_init();
419 ls_hash
= isl_local_space_get_hash(aff
->ls
);
420 isl_hash_hash(hash
, ls_hash
);
421 v_hash
= isl_vec_get_hash(aff
->v
);
422 isl_hash_hash(hash
, v_hash
);
427 /* Return the domain local space of "aff".
429 static __isl_keep isl_local_space
*isl_aff_peek_domain_local_space(
430 __isl_keep isl_aff
*aff
)
432 return aff
? aff
->ls
: NULL
;
435 /* Return the number of variables of the given type in the domain of "aff".
437 isl_size
isl_aff_domain_dim(__isl_keep isl_aff
*aff
, enum isl_dim_type type
)
441 ls
= isl_aff_peek_domain_local_space(aff
);
442 return isl_local_space_dim(ls
, type
);
445 /* Externally, an isl_aff has a map space, but internally, the
446 * ls field corresponds to the domain of that space.
448 isl_size
isl_aff_dim(__isl_keep isl_aff
*aff
, enum isl_dim_type type
)
451 return isl_size_error
;
452 if (type
== isl_dim_out
)
454 if (type
== isl_dim_in
)
456 return isl_aff_domain_dim(aff
, type
);
459 /* Return the offset of the first coefficient of type "type" in
460 * the domain of "aff".
462 isl_size
isl_aff_domain_offset(__isl_keep isl_aff
*aff
, enum isl_dim_type type
)
466 ls
= isl_aff_peek_domain_local_space(aff
);
467 return isl_local_space_offset(ls
, type
);
470 /* Return the position of the dimension of the given type and name
472 * Return -1 if no such dimension can be found.
474 int isl_aff_find_dim_by_name(__isl_keep isl_aff
*aff
, enum isl_dim_type type
,
479 if (type
== isl_dim_out
)
481 if (type
== isl_dim_in
)
483 return isl_local_space_find_dim_by_name(aff
->ls
, type
, name
);
486 /* Return the domain space of "aff".
488 static __isl_keep isl_space
*isl_aff_peek_domain_space(__isl_keep isl_aff
*aff
)
490 return aff
? isl_local_space_peek_space(aff
->ls
) : NULL
;
493 __isl_give isl_space
*isl_aff_get_domain_space(__isl_keep isl_aff
*aff
)
495 return isl_space_copy(isl_aff_peek_domain_space(aff
));
498 __isl_give isl_space
*isl_aff_get_space(__isl_keep isl_aff
*aff
)
503 space
= isl_local_space_get_space(aff
->ls
);
504 space
= isl_space_from_domain(space
);
505 space
= isl_space_add_dims(space
, isl_dim_out
, 1);
509 /* Return a copy of the domain space of "aff".
511 __isl_give isl_local_space
*isl_aff_get_domain_local_space(
512 __isl_keep isl_aff
*aff
)
514 return isl_local_space_copy(isl_aff_peek_domain_local_space(aff
));
517 __isl_give isl_local_space
*isl_aff_get_local_space(__isl_keep isl_aff
*aff
)
522 ls
= isl_local_space_copy(aff
->ls
);
523 ls
= isl_local_space_from_domain(ls
);
524 ls
= isl_local_space_add_dims(ls
, isl_dim_out
, 1);
528 /* Return the local space of the domain of "aff".
529 * This may be either a copy or the local space itself
530 * if there is only one reference to "aff".
531 * This allows the local space to be modified inplace
532 * if both the expression and its local space have only a single reference.
533 * The caller is not allowed to modify "aff" between this call and
534 * a subsequent call to isl_aff_restore_domain_local_space.
535 * The only exception is that isl_aff_free can be called instead.
537 __isl_give isl_local_space
*isl_aff_take_domain_local_space(
538 __isl_keep isl_aff
*aff
)
545 return isl_aff_get_domain_local_space(aff
);
551 /* Set the local space of the domain of "aff" to "ls",
552 * where the local space of "aff" may be missing
553 * due to a preceding call to isl_aff_take_domain_local_space.
554 * However, in this case, "aff" only has a single reference and
555 * then the call to isl_aff_cow has no effect.
557 __isl_give isl_aff
*isl_aff_restore_domain_local_space(
558 __isl_keep isl_aff
*aff
, __isl_take isl_local_space
*ls
)
564 isl_local_space_free(ls
);
568 aff
= isl_aff_cow(aff
);
571 isl_local_space_free(aff
->ls
);
577 isl_local_space_free(ls
);
581 /* Externally, an isl_aff has a map space, but internally, the
582 * ls field corresponds to the domain of that space.
584 const char *isl_aff_get_dim_name(__isl_keep isl_aff
*aff
,
585 enum isl_dim_type type
, unsigned pos
)
589 if (type
== isl_dim_out
)
591 if (type
== isl_dim_in
)
593 return isl_local_space_get_dim_name(aff
->ls
, type
, pos
);
596 __isl_give isl_aff
*isl_aff_reset_domain_space(__isl_take isl_aff
*aff
,
597 __isl_take isl_space
*space
)
599 aff
= isl_aff_cow(aff
);
603 aff
->ls
= isl_local_space_reset_space(aff
->ls
, space
);
605 return isl_aff_free(aff
);
610 isl_space_free(space
);
614 /* Reset the space of "aff". This function is called from isl_pw_templ.c
615 * and doesn't know if the space of an element object is represented
616 * directly or through its domain. It therefore passes along both.
618 __isl_give isl_aff
*isl_aff_reset_space_and_domain(__isl_take isl_aff
*aff
,
619 __isl_take isl_space
*space
, __isl_take isl_space
*domain
)
621 isl_space_free(space
);
622 return isl_aff_reset_domain_space(aff
, domain
);
625 /* Reorder the coefficients of the affine expression based
626 * on the given reordering.
627 * The reordering r is assumed to have been extended with the local
630 static __isl_give isl_vec
*vec_reorder(__isl_take isl_vec
*vec
,
631 __isl_take isl_reordering
*r
, int n_div
)
641 space
= isl_reordering_peek_space(r
);
642 dim
= isl_space_dim(space
, isl_dim_all
);
645 res
= isl_vec_alloc(vec
->ctx
, 2 + dim
+ n_div
);
648 isl_seq_cpy(res
->el
, vec
->el
, 2);
649 isl_seq_clr(res
->el
+ 2, res
->size
- 2);
650 for (i
= 0; i
< r
->len
; ++i
)
651 isl_int_set(res
->el
[2 + r
->pos
[i
]], vec
->el
[2 + i
]);
653 isl_reordering_free(r
);
658 isl_reordering_free(r
);
662 /* Reorder the dimensions of the domain of "aff" according
663 * to the given reordering.
665 __isl_give isl_aff
*isl_aff_realign_domain(__isl_take isl_aff
*aff
,
666 __isl_take isl_reordering
*r
)
668 aff
= isl_aff_cow(aff
);
672 r
= isl_reordering_extend(r
, aff
->ls
->div
->n_row
);
673 aff
->v
= vec_reorder(aff
->v
, isl_reordering_copy(r
),
674 aff
->ls
->div
->n_row
);
675 aff
->ls
= isl_local_space_realign(aff
->ls
, r
);
677 if (!aff
->v
|| !aff
->ls
)
678 return isl_aff_free(aff
);
683 isl_reordering_free(r
);
687 __isl_give isl_aff
*isl_aff_align_params(__isl_take isl_aff
*aff
,
688 __isl_take isl_space
*model
)
690 isl_bool equal_params
;
695 equal_params
= isl_space_has_equal_params(aff
->ls
->dim
, model
);
696 if (equal_params
< 0)
701 exp
= isl_parameter_alignment_reordering(aff
->ls
->dim
, model
);
702 exp
= isl_reordering_extend_space(exp
,
703 isl_aff_get_domain_space(aff
));
704 aff
= isl_aff_realign_domain(aff
, exp
);
707 isl_space_free(model
);
710 isl_space_free(model
);
717 #include "isl_unbind_params_templ.c"
719 /* Is "aff" obviously equal to zero?
721 * If the denominator is zero, then "aff" is not equal to zero.
723 isl_bool
isl_aff_plain_is_zero(__isl_keep isl_aff
*aff
)
728 return isl_bool_error
;
730 if (isl_int_is_zero(aff
->v
->el
[0]))
731 return isl_bool_false
;
732 pos
= isl_seq_first_non_zero(aff
->v
->el
+ 1, aff
->v
->size
- 1);
733 return isl_bool_ok(pos
< 0);
736 /* Does "aff" represent NaN?
738 isl_bool
isl_aff_is_nan(__isl_keep isl_aff
*aff
)
741 return isl_bool_error
;
743 return isl_bool_ok(isl_seq_first_non_zero(aff
->v
->el
, 2) < 0);
746 /* Are "aff1" and "aff2" obviously equal?
748 * NaN is not equal to anything, not even to another NaN.
750 isl_bool
isl_aff_plain_is_equal(__isl_keep isl_aff
*aff1
,
751 __isl_keep isl_aff
*aff2
)
756 return isl_bool_error
;
758 if (isl_aff_is_nan(aff1
) || isl_aff_is_nan(aff2
))
759 return isl_bool_false
;
761 equal
= isl_local_space_is_equal(aff1
->ls
, aff2
->ls
);
762 if (equal
< 0 || !equal
)
765 return isl_vec_is_equal(aff1
->v
, aff2
->v
);
768 /* Return the common denominator of "aff" in "v".
770 * We cannot return anything meaningful in case of a NaN.
772 isl_stat
isl_aff_get_denominator(__isl_keep isl_aff
*aff
, isl_int
*v
)
775 return isl_stat_error
;
776 if (isl_aff_is_nan(aff
))
777 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
778 "cannot get denominator of NaN", return isl_stat_error
);
779 isl_int_set(*v
, aff
->v
->el
[0]);
783 /* Return the common denominator of "aff".
785 __isl_give isl_val
*isl_aff_get_denominator_val(__isl_keep isl_aff
*aff
)
792 ctx
= isl_aff_get_ctx(aff
);
793 if (isl_aff_is_nan(aff
))
794 return isl_val_nan(ctx
);
795 return isl_val_int_from_isl_int(ctx
, aff
->v
->el
[0]);
798 /* Return the constant term of "aff".
800 __isl_give isl_val
*isl_aff_get_constant_val(__isl_keep isl_aff
*aff
)
808 ctx
= isl_aff_get_ctx(aff
);
809 if (isl_aff_is_nan(aff
))
810 return isl_val_nan(ctx
);
811 v
= isl_val_rat_from_isl_int(ctx
, aff
->v
->el
[1], aff
->v
->el
[0]);
812 return isl_val_normalize(v
);
815 /* Return the coefficient of the variable of type "type" at position "pos"
818 __isl_give isl_val
*isl_aff_get_coefficient_val(__isl_keep isl_aff
*aff
,
819 enum isl_dim_type type
, int pos
)
827 ctx
= isl_aff_get_ctx(aff
);
828 if (type
== isl_dim_out
)
829 isl_die(ctx
, isl_error_invalid
,
830 "output/set dimension does not have a coefficient",
832 if (type
== isl_dim_in
)
835 if (isl_local_space_check_range(aff
->ls
, type
, pos
, 1) < 0)
838 if (isl_aff_is_nan(aff
))
839 return isl_val_nan(ctx
);
840 pos
+= isl_local_space_offset(aff
->ls
, type
);
841 v
= isl_val_rat_from_isl_int(ctx
, aff
->v
->el
[1 + pos
], aff
->v
->el
[0]);
842 return isl_val_normalize(v
);
845 /* Return the sign of the coefficient of the variable of type "type"
846 * at position "pos" of "aff".
848 int isl_aff_coefficient_sgn(__isl_keep isl_aff
*aff
, enum isl_dim_type type
,
856 ctx
= isl_aff_get_ctx(aff
);
857 if (type
== isl_dim_out
)
858 isl_die(ctx
, isl_error_invalid
,
859 "output/set dimension does not have a coefficient",
861 if (type
== isl_dim_in
)
864 if (isl_local_space_check_range(aff
->ls
, type
, pos
, 1) < 0)
867 pos
+= isl_local_space_offset(aff
->ls
, type
);
868 return isl_int_sgn(aff
->v
->el
[1 + pos
]);
871 /* Replace the numerator of the constant term of "aff" by "v".
873 * A NaN is unaffected by this operation.
875 __isl_give isl_aff
*isl_aff_set_constant(__isl_take isl_aff
*aff
, isl_int v
)
879 if (isl_aff_is_nan(aff
))
881 aff
= isl_aff_cow(aff
);
885 aff
->v
= isl_vec_cow(aff
->v
);
887 return isl_aff_free(aff
);
889 isl_int_set(aff
->v
->el
[1], v
);
894 /* Replace the constant term of "aff" by "v".
896 * A NaN is unaffected by this operation.
898 __isl_give isl_aff
*isl_aff_set_constant_val(__isl_take isl_aff
*aff
,
899 __isl_take isl_val
*v
)
904 if (isl_aff_is_nan(aff
)) {
909 if (!isl_val_is_rat(v
))
910 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
911 "expecting rational value", goto error
);
913 if (isl_int_eq(aff
->v
->el
[1], v
->n
) &&
914 isl_int_eq(aff
->v
->el
[0], v
->d
)) {
919 aff
= isl_aff_cow(aff
);
922 aff
->v
= isl_vec_cow(aff
->v
);
926 if (isl_int_eq(aff
->v
->el
[0], v
->d
)) {
927 isl_int_set(aff
->v
->el
[1], v
->n
);
928 } else if (isl_int_is_one(v
->d
)) {
929 isl_int_mul(aff
->v
->el
[1], aff
->v
->el
[0], v
->n
);
931 isl_seq_scale(aff
->v
->el
+ 1,
932 aff
->v
->el
+ 1, v
->d
, aff
->v
->size
- 1);
933 isl_int_mul(aff
->v
->el
[1], aff
->v
->el
[0], v
->n
);
934 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], v
->d
);
935 aff
->v
= isl_vec_normalize(aff
->v
);
948 /* Add "v" to the constant term of "aff".
950 * A NaN is unaffected by this operation.
952 __isl_give isl_aff
*isl_aff_add_constant(__isl_take isl_aff
*aff
, isl_int v
)
954 if (isl_int_is_zero(v
))
959 if (isl_aff_is_nan(aff
))
961 aff
= isl_aff_cow(aff
);
965 aff
->v
= isl_vec_cow(aff
->v
);
967 return isl_aff_free(aff
);
969 isl_int_addmul(aff
->v
->el
[1], aff
->v
->el
[0], v
);
974 /* Add "v" to the constant term of "aff",
975 * in case "aff" is a rational expression.
977 static __isl_give isl_aff
*isl_aff_add_rat_constant_val(__isl_take isl_aff
*aff
,
978 __isl_take isl_val
*v
)
980 aff
= isl_aff_cow(aff
);
984 aff
->v
= isl_vec_cow(aff
->v
);
988 if (isl_int_is_one(v
->d
)) {
989 isl_int_addmul(aff
->v
->el
[1], aff
->v
->el
[0], v
->n
);
990 } else if (isl_int_eq(aff
->v
->el
[0], v
->d
)) {
991 isl_int_add(aff
->v
->el
[1], aff
->v
->el
[1], v
->n
);
992 aff
->v
= isl_vec_normalize(aff
->v
);
996 isl_seq_scale(aff
->v
->el
+ 1,
997 aff
->v
->el
+ 1, v
->d
, aff
->v
->size
- 1);
998 isl_int_addmul(aff
->v
->el
[1], aff
->v
->el
[0], v
->n
);
999 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], v
->d
);
1000 aff
->v
= isl_vec_normalize(aff
->v
);
1013 /* Return the first argument and free the second.
1015 static __isl_give isl_aff
*pick_free(__isl_take isl_aff
*aff
,
1016 __isl_take isl_val
*v
)
1022 /* Replace the first argument by NaN and free the second argument.
1024 static __isl_give isl_aff
*set_nan_free_val(__isl_take isl_aff
*aff
,
1025 __isl_take isl_val
*v
)
1028 return isl_aff_set_nan(aff
);
1031 /* Add "v" to the constant term of "aff".
1033 * A NaN is unaffected by this operation.
1034 * Conversely, adding a NaN turns "aff" into a NaN.
1036 __isl_give isl_aff
*isl_aff_add_constant_val(__isl_take isl_aff
*aff
,
1037 __isl_take isl_val
*v
)
1039 isl_bool is_nan
, is_zero
, is_rat
;
1041 is_nan
= isl_aff_is_nan(aff
);
1042 is_zero
= isl_val_is_zero(v
);
1043 if (is_nan
< 0 || is_zero
< 0)
1045 if (is_nan
|| is_zero
)
1046 return pick_free(aff
, v
);
1048 is_nan
= isl_val_is_nan(v
);
1049 is_rat
= isl_val_is_rat(v
);
1050 if (is_nan
< 0 || is_rat
< 0)
1053 return set_nan_free_val(aff
, v
);
1055 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1056 "expecting rational value or NaN", goto error
);
1058 return isl_aff_add_rat_constant_val(aff
, v
);
1065 __isl_give isl_aff
*isl_aff_add_constant_si(__isl_take isl_aff
*aff
, int v
)
1070 isl_int_set_si(t
, v
);
1071 aff
= isl_aff_add_constant(aff
, t
);
1077 /* Add "v" to the numerator of the constant term of "aff".
1079 * A NaN is unaffected by this operation.
1081 __isl_give isl_aff
*isl_aff_add_constant_num(__isl_take isl_aff
*aff
, isl_int v
)
1083 if (isl_int_is_zero(v
))
1088 if (isl_aff_is_nan(aff
))
1090 aff
= isl_aff_cow(aff
);
1094 aff
->v
= isl_vec_cow(aff
->v
);
1096 return isl_aff_free(aff
);
1098 isl_int_add(aff
->v
->el
[1], aff
->v
->el
[1], v
);
1103 /* Add "v" to the numerator of the constant term of "aff".
1105 * A NaN is unaffected by this operation.
1107 __isl_give isl_aff
*isl_aff_add_constant_num_si(__isl_take isl_aff
*aff
, int v
)
1115 isl_int_set_si(t
, v
);
1116 aff
= isl_aff_add_constant_num(aff
, t
);
1122 /* Replace the numerator of the constant term of "aff" by "v".
1124 * A NaN is unaffected by this operation.
1126 __isl_give isl_aff
*isl_aff_set_constant_si(__isl_take isl_aff
*aff
, int v
)
1130 if (isl_aff_is_nan(aff
))
1132 aff
= isl_aff_cow(aff
);
1136 aff
->v
= isl_vec_cow(aff
->v
);
1138 return isl_aff_free(aff
);
1140 isl_int_set_si(aff
->v
->el
[1], v
);
1145 /* Replace the numerator of the coefficient of the variable of type "type"
1146 * at position "pos" of "aff" by "v".
1148 * A NaN is unaffected by this operation.
1150 __isl_give isl_aff
*isl_aff_set_coefficient(__isl_take isl_aff
*aff
,
1151 enum isl_dim_type type
, int pos
, isl_int v
)
1156 if (type
== isl_dim_out
)
1157 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1158 "output/set dimension does not have a coefficient",
1159 return isl_aff_free(aff
));
1160 if (type
== isl_dim_in
)
1163 if (isl_local_space_check_range(aff
->ls
, type
, pos
, 1) < 0)
1164 return isl_aff_free(aff
);
1166 if (isl_aff_is_nan(aff
))
1168 aff
= isl_aff_cow(aff
);
1172 aff
->v
= isl_vec_cow(aff
->v
);
1174 return isl_aff_free(aff
);
1176 pos
+= isl_local_space_offset(aff
->ls
, type
);
1177 isl_int_set(aff
->v
->el
[1 + pos
], v
);
1182 /* Replace the numerator of the coefficient of the variable of type "type"
1183 * at position "pos" of "aff" by "v".
1185 * A NaN is unaffected by this operation.
1187 __isl_give isl_aff
*isl_aff_set_coefficient_si(__isl_take isl_aff
*aff
,
1188 enum isl_dim_type type
, int pos
, int v
)
1193 if (type
== isl_dim_out
)
1194 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1195 "output/set dimension does not have a coefficient",
1196 return isl_aff_free(aff
));
1197 if (type
== isl_dim_in
)
1200 if (isl_local_space_check_range(aff
->ls
, type
, pos
, 1) < 0)
1201 return isl_aff_free(aff
);
1203 if (isl_aff_is_nan(aff
))
1205 pos
+= isl_local_space_offset(aff
->ls
, type
);
1206 if (isl_int_cmp_si(aff
->v
->el
[1 + pos
], v
) == 0)
1209 aff
= isl_aff_cow(aff
);
1213 aff
->v
= isl_vec_cow(aff
->v
);
1215 return isl_aff_free(aff
);
1217 isl_int_set_si(aff
->v
->el
[1 + pos
], v
);
1222 /* Replace the coefficient of the variable of type "type" at position "pos"
1225 * A NaN is unaffected by this operation.
1227 __isl_give isl_aff
*isl_aff_set_coefficient_val(__isl_take isl_aff
*aff
,
1228 enum isl_dim_type type
, int pos
, __isl_take isl_val
*v
)
1233 if (type
== isl_dim_out
)
1234 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1235 "output/set dimension does not have a coefficient",
1237 if (type
== isl_dim_in
)
1240 if (isl_local_space_check_range(aff
->ls
, type
, pos
, 1) < 0)
1241 return isl_aff_free(aff
);
1243 if (isl_aff_is_nan(aff
)) {
1247 if (!isl_val_is_rat(v
))
1248 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1249 "expecting rational value", goto error
);
1251 pos
+= isl_local_space_offset(aff
->ls
, type
);
1252 if (isl_int_eq(aff
->v
->el
[1 + pos
], v
->n
) &&
1253 isl_int_eq(aff
->v
->el
[0], v
->d
)) {
1258 aff
= isl_aff_cow(aff
);
1261 aff
->v
= isl_vec_cow(aff
->v
);
1265 if (isl_int_eq(aff
->v
->el
[0], v
->d
)) {
1266 isl_int_set(aff
->v
->el
[1 + pos
], v
->n
);
1267 } else if (isl_int_is_one(v
->d
)) {
1268 isl_int_mul(aff
->v
->el
[1 + pos
], aff
->v
->el
[0], v
->n
);
1270 isl_seq_scale(aff
->v
->el
+ 1,
1271 aff
->v
->el
+ 1, v
->d
, aff
->v
->size
- 1);
1272 isl_int_mul(aff
->v
->el
[1 + pos
], aff
->v
->el
[0], v
->n
);
1273 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], v
->d
);
1274 aff
->v
= isl_vec_normalize(aff
->v
);
1287 /* Add "v" to the coefficient of the variable of type "type"
1288 * at position "pos" of "aff".
1290 * A NaN is unaffected by this operation.
1292 __isl_give isl_aff
*isl_aff_add_coefficient(__isl_take isl_aff
*aff
,
1293 enum isl_dim_type type
, int pos
, isl_int v
)
1298 if (type
== isl_dim_out
)
1299 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1300 "output/set dimension does not have a coefficient",
1301 return isl_aff_free(aff
));
1302 if (type
== isl_dim_in
)
1305 if (isl_local_space_check_range(aff
->ls
, type
, pos
, 1) < 0)
1306 return isl_aff_free(aff
);
1308 if (isl_aff_is_nan(aff
))
1310 aff
= isl_aff_cow(aff
);
1314 aff
->v
= isl_vec_cow(aff
->v
);
1316 return isl_aff_free(aff
);
1318 pos
+= isl_local_space_offset(aff
->ls
, type
);
1319 isl_int_addmul(aff
->v
->el
[1 + pos
], aff
->v
->el
[0], v
);
1324 /* Add "v" to the coefficient of the variable of type "type"
1325 * at position "pos" of "aff".
1327 * A NaN is unaffected by this operation.
1329 __isl_give isl_aff
*isl_aff_add_coefficient_val(__isl_take isl_aff
*aff
,
1330 enum isl_dim_type type
, int pos
, __isl_take isl_val
*v
)
1335 if (isl_val_is_zero(v
)) {
1340 if (type
== isl_dim_out
)
1341 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1342 "output/set dimension does not have a coefficient",
1344 if (type
== isl_dim_in
)
1347 if (isl_local_space_check_range(aff
->ls
, type
, pos
, 1) < 0)
1350 if (isl_aff_is_nan(aff
)) {
1354 if (!isl_val_is_rat(v
))
1355 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1356 "expecting rational value", goto error
);
1358 aff
= isl_aff_cow(aff
);
1362 aff
->v
= isl_vec_cow(aff
->v
);
1366 pos
+= isl_local_space_offset(aff
->ls
, type
);
1367 if (isl_int_is_one(v
->d
)) {
1368 isl_int_addmul(aff
->v
->el
[1 + pos
], aff
->v
->el
[0], v
->n
);
1369 } else if (isl_int_eq(aff
->v
->el
[0], v
->d
)) {
1370 isl_int_add(aff
->v
->el
[1 + pos
], aff
->v
->el
[1 + pos
], v
->n
);
1371 aff
->v
= isl_vec_normalize(aff
->v
);
1375 isl_seq_scale(aff
->v
->el
+ 1,
1376 aff
->v
->el
+ 1, v
->d
, aff
->v
->size
- 1);
1377 isl_int_addmul(aff
->v
->el
[1 + pos
], aff
->v
->el
[0], v
->n
);
1378 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], v
->d
);
1379 aff
->v
= isl_vec_normalize(aff
->v
);
1392 __isl_give isl_aff
*isl_aff_add_coefficient_si(__isl_take isl_aff
*aff
,
1393 enum isl_dim_type type
, int pos
, int v
)
1398 isl_int_set_si(t
, v
);
1399 aff
= isl_aff_add_coefficient(aff
, type
, pos
, t
);
1405 __isl_give isl_aff
*isl_aff_get_div(__isl_keep isl_aff
*aff
, int pos
)
1410 return isl_local_space_get_div(aff
->ls
, pos
);
1413 /* Return the negation of "aff".
1415 * As a special case, -NaN = NaN.
1417 __isl_give isl_aff
*isl_aff_neg(__isl_take isl_aff
*aff
)
1421 if (isl_aff_is_nan(aff
))
1423 aff
= isl_aff_cow(aff
);
1426 aff
->v
= isl_vec_cow(aff
->v
);
1428 return isl_aff_free(aff
);
1430 isl_seq_neg(aff
->v
->el
+ 1, aff
->v
->el
+ 1, aff
->v
->size
- 1);
1435 /* Remove divs from the local space that do not appear in the affine
1437 * We currently only remove divs at the end.
1438 * Some intermediate divs may also not appear directly in the affine
1439 * expression, but we would also need to check that no other divs are
1440 * defined in terms of them.
1442 __isl_give isl_aff
*isl_aff_remove_unused_divs(__isl_take isl_aff
*aff
)
1448 n
= isl_aff_domain_dim(aff
, isl_dim_div
);
1449 off
= isl_aff_domain_offset(aff
, isl_dim_div
);
1450 if (n
< 0 || off
< 0)
1451 return isl_aff_free(aff
);
1453 pos
= isl_seq_last_non_zero(aff
->v
->el
+ 1 + off
, n
) + 1;
1457 aff
= isl_aff_cow(aff
);
1461 aff
->ls
= isl_local_space_drop_dims(aff
->ls
, isl_dim_div
, pos
, n
- pos
);
1462 aff
->v
= isl_vec_drop_els(aff
->v
, 1 + off
+ pos
, n
- pos
);
1463 if (!aff
->ls
|| !aff
->v
)
1464 return isl_aff_free(aff
);
1469 /* Look for any divs in the aff->ls with a denominator equal to one
1470 * and plug them into the affine expression and any subsequent divs
1471 * that may reference the div.
1473 static __isl_give isl_aff
*plug_in_integral_divs(__isl_take isl_aff
*aff
)
1480 isl_local_space
*ls
;
1483 n
= isl_aff_domain_dim(aff
, isl_dim_div
);
1484 off
= isl_aff_domain_offset(aff
, isl_dim_div
);
1485 if (n
< 0 || off
< 0)
1486 return isl_aff_free(aff
);
1488 for (i
= 0; i
< n
; ++i
) {
1489 if (!isl_int_is_one(aff
->ls
->div
->row
[i
][0]))
1491 ls
= isl_local_space_copy(aff
->ls
);
1492 ls
= isl_local_space_substitute_seq(ls
, isl_dim_div
, i
,
1493 aff
->ls
->div
->row
[i
], len
, i
+ 1, n
- (i
+ 1));
1494 vec
= isl_vec_copy(aff
->v
);
1495 vec
= isl_vec_cow(vec
);
1501 isl_seq_substitute(vec
->el
, off
+ i
, aff
->ls
->div
->row
[i
],
1506 isl_vec_free(aff
->v
);
1508 isl_local_space_free(aff
->ls
);
1515 isl_local_space_free(ls
);
1516 return isl_aff_free(aff
);
1519 /* Look for any divs j that appear with a unit coefficient inside
1520 * the definitions of other divs i and plug them into the definitions
1523 * In particular, an expression of the form
1525 * floor((f(..) + floor(g(..)/n))/m)
1529 * floor((n * f(..) + g(..))/(n * m))
1531 * This simplification is correct because we can move the expression
1532 * f(..) into the inner floor in the original expression to obtain
1534 * floor(floor((n * f(..) + g(..))/n)/m)
1536 * from which we can derive the simplified expression.
1538 static __isl_give isl_aff
*plug_in_unit_divs(__isl_take isl_aff
*aff
)
1544 n
= isl_aff_domain_dim(aff
, isl_dim_div
);
1545 off
= isl_aff_domain_offset(aff
, isl_dim_div
);
1546 if (n
< 0 || off
< 0)
1547 return isl_aff_free(aff
);
1548 for (i
= 1; i
< n
; ++i
) {
1549 for (j
= 0; j
< i
; ++j
) {
1550 if (!isl_int_is_one(aff
->ls
->div
->row
[i
][1 + off
+ j
]))
1552 aff
->ls
= isl_local_space_substitute_seq(aff
->ls
,
1553 isl_dim_div
, j
, aff
->ls
->div
->row
[j
],
1554 aff
->v
->size
, i
, 1);
1556 return isl_aff_free(aff
);
1563 /* Swap divs "a" and "b" in "aff", which is assumed to be non-NULL.
1565 * Even though this function is only called on isl_affs with a single
1566 * reference, we are careful to only change aff->v and aff->ls together.
1568 static __isl_give isl_aff
*swap_div(__isl_take isl_aff
*aff
, int a
, int b
)
1570 isl_size off
= isl_aff_domain_offset(aff
, isl_dim_div
);
1571 isl_local_space
*ls
;
1575 return isl_aff_free(aff
);
1577 ls
= isl_local_space_copy(aff
->ls
);
1578 ls
= isl_local_space_swap_div(ls
, a
, b
);
1579 v
= isl_vec_copy(aff
->v
);
1584 isl_int_swap(v
->el
[1 + off
+ a
], v
->el
[1 + off
+ b
]);
1585 isl_vec_free(aff
->v
);
1587 isl_local_space_free(aff
->ls
);
1593 isl_local_space_free(ls
);
1594 return isl_aff_free(aff
);
1597 /* Merge divs "a" and "b" in "aff", which is assumed to be non-NULL.
1599 * We currently do not actually remove div "b", but simply add its
1600 * coefficient to that of "a" and then zero it out.
1602 static __isl_give isl_aff
*merge_divs(__isl_take isl_aff
*aff
, int a
, int b
)
1604 isl_size off
= isl_aff_domain_offset(aff
, isl_dim_div
);
1607 return isl_aff_free(aff
);
1609 if (isl_int_is_zero(aff
->v
->el
[1 + off
+ b
]))
1612 aff
->v
= isl_vec_cow(aff
->v
);
1614 return isl_aff_free(aff
);
1616 isl_int_add(aff
->v
->el
[1 + off
+ a
],
1617 aff
->v
->el
[1 + off
+ a
], aff
->v
->el
[1 + off
+ b
]);
1618 isl_int_set_si(aff
->v
->el
[1 + off
+ b
], 0);
1623 /* Sort the divs in the local space of "aff" according to
1624 * the comparison function "cmp_row" in isl_local_space.c,
1625 * combining the coefficients of identical divs.
1627 * Reordering divs does not change the semantics of "aff",
1628 * so there is no need to call isl_aff_cow.
1629 * Moreover, this function is currently only called on isl_affs
1630 * with a single reference.
1632 static __isl_give isl_aff
*sort_divs(__isl_take isl_aff
*aff
)
1637 n
= isl_aff_dim(aff
, isl_dim_div
);
1639 return isl_aff_free(aff
);
1640 for (i
= 1; i
< n
; ++i
) {
1641 for (j
= i
- 1; j
>= 0; --j
) {
1642 int cmp
= isl_mat_cmp_div(aff
->ls
->div
, j
, j
+ 1);
1646 aff
= merge_divs(aff
, j
, j
+ 1);
1648 aff
= swap_div(aff
, j
, j
+ 1);
1657 /* Normalize the representation of "aff".
1659 * This function should only be called on "new" isl_affs, i.e.,
1660 * with only a single reference. We therefore do not need to
1661 * worry about affecting other instances.
1663 __isl_give isl_aff
*isl_aff_normalize(__isl_take isl_aff
*aff
)
1667 aff
->v
= isl_vec_normalize(aff
->v
);
1669 return isl_aff_free(aff
);
1670 aff
= plug_in_integral_divs(aff
);
1671 aff
= plug_in_unit_divs(aff
);
1672 aff
= sort_divs(aff
);
1673 aff
= isl_aff_remove_unused_divs(aff
);
1677 /* Given f, return floor(f).
1678 * If f is an integer expression, then just return f.
1679 * If f is a constant, then return the constant floor(f).
1680 * Otherwise, if f = g/m, write g = q m + r,
1681 * create a new div d = [r/m] and return the expression q + d.
1682 * The coefficients in r are taken to lie between -m/2 and m/2.
1684 * reduce_div_coefficients performs the same normalization.
1686 * As a special case, floor(NaN) = NaN.
1688 __isl_give isl_aff
*isl_aff_floor(__isl_take isl_aff
*aff
)
1698 if (isl_aff_is_nan(aff
))
1700 if (isl_int_is_one(aff
->v
->el
[0]))
1703 aff
= isl_aff_cow(aff
);
1707 aff
->v
= isl_vec_cow(aff
->v
);
1709 return isl_aff_free(aff
);
1711 if (isl_aff_is_cst(aff
)) {
1712 isl_int_fdiv_q(aff
->v
->el
[1], aff
->v
->el
[1], aff
->v
->el
[0]);
1713 isl_int_set_si(aff
->v
->el
[0], 1);
1717 div
= isl_vec_copy(aff
->v
);
1718 div
= isl_vec_cow(div
);
1720 return isl_aff_free(aff
);
1722 ctx
= isl_aff_get_ctx(aff
);
1723 isl_int_fdiv_q(aff
->v
->el
[0], aff
->v
->el
[0], ctx
->two
);
1724 for (i
= 1; i
< aff
->v
->size
; ++i
) {
1725 isl_int_fdiv_r(div
->el
[i
], div
->el
[i
], div
->el
[0]);
1726 isl_int_fdiv_q(aff
->v
->el
[i
], aff
->v
->el
[i
], div
->el
[0]);
1727 if (isl_int_gt(div
->el
[i
], aff
->v
->el
[0])) {
1728 isl_int_sub(div
->el
[i
], div
->el
[i
], div
->el
[0]);
1729 isl_int_add_ui(aff
->v
->el
[i
], aff
->v
->el
[i
], 1);
1733 aff
->ls
= isl_local_space_add_div(aff
->ls
, div
);
1735 return isl_aff_free(aff
);
1737 size
= aff
->v
->size
;
1738 aff
->v
= isl_vec_extend(aff
->v
, size
+ 1);
1740 return isl_aff_free(aff
);
1741 isl_int_set_si(aff
->v
->el
[0], 1);
1742 isl_int_set_si(aff
->v
->el
[size
], 1);
1744 aff
= isl_aff_normalize(aff
);
1751 * aff mod m = aff - m * floor(aff/m)
1753 * with m an integer value.
1755 __isl_give isl_aff
*isl_aff_mod_val(__isl_take isl_aff
*aff
,
1756 __isl_take isl_val
*m
)
1763 if (!isl_val_is_int(m
))
1764 isl_die(isl_val_get_ctx(m
), isl_error_invalid
,
1765 "expecting integer modulo", goto error
);
1767 res
= isl_aff_copy(aff
);
1768 aff
= isl_aff_scale_down_val(aff
, isl_val_copy(m
));
1769 aff
= isl_aff_floor(aff
);
1770 aff
= isl_aff_scale_val(aff
, m
);
1771 res
= isl_aff_sub(res
, aff
);
1782 * pwaff mod m = pwaff - m * floor(pwaff/m)
1784 __isl_give isl_pw_aff
*isl_pw_aff_mod(__isl_take isl_pw_aff
*pwaff
, isl_int m
)
1788 res
= isl_pw_aff_copy(pwaff
);
1789 pwaff
= isl_pw_aff_scale_down(pwaff
, m
);
1790 pwaff
= isl_pw_aff_floor(pwaff
);
1791 pwaff
= isl_pw_aff_scale(pwaff
, m
);
1792 res
= isl_pw_aff_sub(res
, pwaff
);
1799 * pa mod m = pa - m * floor(pa/m)
1801 * with m an integer value.
1803 __isl_give isl_pw_aff
*isl_pw_aff_mod_val(__isl_take isl_pw_aff
*pa
,
1804 __isl_take isl_val
*m
)
1808 if (!isl_val_is_int(m
))
1809 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
1810 "expecting integer modulo", goto error
);
1811 pa
= isl_pw_aff_mod(pa
, m
->n
);
1815 isl_pw_aff_free(pa
);
1820 /* Given f, return ceil(f).
1821 * If f is an integer expression, then just return f.
1822 * Otherwise, let f be the expression
1828 * floor((e + m - 1)/m)
1830 * As a special case, ceil(NaN) = NaN.
1832 __isl_give isl_aff
*isl_aff_ceil(__isl_take isl_aff
*aff
)
1837 if (isl_aff_is_nan(aff
))
1839 if (isl_int_is_one(aff
->v
->el
[0]))
1842 aff
= isl_aff_cow(aff
);
1845 aff
->v
= isl_vec_cow(aff
->v
);
1847 return isl_aff_free(aff
);
1849 isl_int_add(aff
->v
->el
[1], aff
->v
->el
[1], aff
->v
->el
[0]);
1850 isl_int_sub_ui(aff
->v
->el
[1], aff
->v
->el
[1], 1);
1851 aff
= isl_aff_floor(aff
);
1856 /* Apply the expansion computed by isl_merge_divs.
1857 * The expansion itself is given by "exp" while the resulting
1858 * list of divs is given by "div".
1860 __isl_give isl_aff
*isl_aff_expand_divs(__isl_take isl_aff
*aff
,
1861 __isl_take isl_mat
*div
, int *exp
)
1867 aff
= isl_aff_cow(aff
);
1869 offset
= isl_aff_domain_offset(aff
, isl_dim_div
);
1870 old_n_div
= isl_aff_domain_dim(aff
, isl_dim_div
);
1871 new_n_div
= isl_mat_rows(div
);
1872 if (offset
< 0 || old_n_div
< 0 || new_n_div
< 0)
1875 aff
->v
= isl_vec_expand(aff
->v
, 1 + offset
, old_n_div
, exp
, new_n_div
);
1876 aff
->ls
= isl_local_space_replace_divs(aff
->ls
, div
);
1877 if (!aff
->v
|| !aff
->ls
)
1878 return isl_aff_free(aff
);
1886 /* Add two affine expressions that live in the same local space.
1888 static __isl_give isl_aff
*add_expanded(__isl_take isl_aff
*aff1
,
1889 __isl_take isl_aff
*aff2
)
1893 aff1
= isl_aff_cow(aff1
);
1897 aff1
->v
= isl_vec_cow(aff1
->v
);
1903 isl_int_gcd(gcd
, aff1
->v
->el
[0], aff2
->v
->el
[0]);
1904 isl_int_divexact(f
, aff2
->v
->el
[0], gcd
);
1905 isl_seq_scale(aff1
->v
->el
+ 1, aff1
->v
->el
+ 1, f
, aff1
->v
->size
- 1);
1906 isl_int_divexact(f
, aff1
->v
->el
[0], gcd
);
1907 isl_seq_addmul(aff1
->v
->el
+ 1, f
, aff2
->v
->el
+ 1, aff1
->v
->size
- 1);
1908 isl_int_divexact(f
, aff2
->v
->el
[0], gcd
);
1909 isl_int_mul(aff1
->v
->el
[0], aff1
->v
->el
[0], f
);
1914 aff1
= isl_aff_normalize(aff1
);
1922 /* Replace one of the arguments by a NaN and free the other one.
1924 static __isl_give isl_aff
*set_nan_free(__isl_take isl_aff
*aff1
,
1925 __isl_take isl_aff
*aff2
)
1928 return isl_aff_set_nan(aff1
);
1931 /* Return the sum of "aff1" and "aff2".
1933 * If either of the two is NaN, then the result is NaN.
1935 __isl_give isl_aff
*isl_aff_add(__isl_take isl_aff
*aff1
,
1936 __isl_take isl_aff
*aff2
)
1942 isl_size n_div1
, n_div2
;
1947 ctx
= isl_aff_get_ctx(aff1
);
1948 if (!isl_space_is_equal(aff1
->ls
->dim
, aff2
->ls
->dim
))
1949 isl_die(ctx
, isl_error_invalid
,
1950 "spaces don't match", goto error
);
1952 if (isl_aff_is_nan(aff1
)) {
1956 if (isl_aff_is_nan(aff2
)) {
1961 n_div1
= isl_aff_dim(aff1
, isl_dim_div
);
1962 n_div2
= isl_aff_dim(aff2
, isl_dim_div
);
1963 if (n_div1
< 0 || n_div2
< 0)
1965 if (n_div1
== 0 && n_div2
== 0)
1966 return add_expanded(aff1
, aff2
);
1968 exp1
= isl_alloc_array(ctx
, int, n_div1
);
1969 exp2
= isl_alloc_array(ctx
, int, n_div2
);
1970 if ((n_div1
&& !exp1
) || (n_div2
&& !exp2
))
1973 div
= isl_merge_divs(aff1
->ls
->div
, aff2
->ls
->div
, exp1
, exp2
);
1974 aff1
= isl_aff_expand_divs(aff1
, isl_mat_copy(div
), exp1
);
1975 aff2
= isl_aff_expand_divs(aff2
, div
, exp2
);
1979 return add_expanded(aff1
, aff2
);
1988 __isl_give isl_aff
*isl_aff_sub(__isl_take isl_aff
*aff1
,
1989 __isl_take isl_aff
*aff2
)
1991 return isl_aff_add(aff1
, isl_aff_neg(aff2
));
1994 /* Return the result of scaling "aff" by a factor of "f".
1996 * As a special case, f * NaN = NaN.
1998 __isl_give isl_aff
*isl_aff_scale(__isl_take isl_aff
*aff
, isl_int f
)
2004 if (isl_aff_is_nan(aff
))
2007 if (isl_int_is_one(f
))
2010 aff
= isl_aff_cow(aff
);
2013 aff
->v
= isl_vec_cow(aff
->v
);
2015 return isl_aff_free(aff
);
2017 if (isl_int_is_pos(f
) && isl_int_is_divisible_by(aff
->v
->el
[0], f
)) {
2018 isl_int_divexact(aff
->v
->el
[0], aff
->v
->el
[0], f
);
2023 isl_int_gcd(gcd
, aff
->v
->el
[0], f
);
2024 isl_int_divexact(aff
->v
->el
[0], aff
->v
->el
[0], gcd
);
2025 isl_int_divexact(gcd
, f
, gcd
);
2026 isl_seq_scale(aff
->v
->el
+ 1, aff
->v
->el
+ 1, gcd
, aff
->v
->size
- 1);
2032 /* Multiple "aff" by "v".
2034 __isl_give isl_aff
*isl_aff_scale_val(__isl_take isl_aff
*aff
,
2035 __isl_take isl_val
*v
)
2040 if (isl_val_is_one(v
)) {
2045 if (!isl_val_is_rat(v
))
2046 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2047 "expecting rational factor", goto error
);
2049 aff
= isl_aff_scale(aff
, v
->n
);
2050 aff
= isl_aff_scale_down(aff
, v
->d
);
2060 /* Return the result of scaling "aff" down by a factor of "f".
2062 * As a special case, NaN/f = NaN.
2064 __isl_give isl_aff
*isl_aff_scale_down(__isl_take isl_aff
*aff
, isl_int f
)
2070 if (isl_aff_is_nan(aff
))
2073 if (isl_int_is_one(f
))
2076 aff
= isl_aff_cow(aff
);
2080 if (isl_int_is_zero(f
))
2081 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2082 "cannot scale down by zero", return isl_aff_free(aff
));
2084 aff
->v
= isl_vec_cow(aff
->v
);
2086 return isl_aff_free(aff
);
2089 isl_seq_gcd(aff
->v
->el
+ 1, aff
->v
->size
- 1, &gcd
);
2090 isl_int_gcd(gcd
, gcd
, f
);
2091 isl_seq_scale_down(aff
->v
->el
+ 1, aff
->v
->el
+ 1, gcd
, aff
->v
->size
- 1);
2092 isl_int_divexact(gcd
, f
, gcd
);
2093 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], gcd
);
2099 /* Divide "aff" by "v".
2101 __isl_give isl_aff
*isl_aff_scale_down_val(__isl_take isl_aff
*aff
,
2102 __isl_take isl_val
*v
)
2107 if (isl_val_is_one(v
)) {
2112 if (!isl_val_is_rat(v
))
2113 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2114 "expecting rational factor", goto error
);
2115 if (!isl_val_is_pos(v
))
2116 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2117 "factor needs to be positive", goto error
);
2119 aff
= isl_aff_scale(aff
, v
->d
);
2120 aff
= isl_aff_scale_down(aff
, v
->n
);
2130 __isl_give isl_aff
*isl_aff_scale_down_ui(__isl_take isl_aff
*aff
, unsigned f
)
2138 isl_int_set_ui(v
, f
);
2139 aff
= isl_aff_scale_down(aff
, v
);
2145 __isl_give isl_aff
*isl_aff_set_dim_name(__isl_take isl_aff
*aff
,
2146 enum isl_dim_type type
, unsigned pos
, const char *s
)
2148 aff
= isl_aff_cow(aff
);
2151 if (type
== isl_dim_out
)
2152 isl_die(aff
->v
->ctx
, isl_error_invalid
,
2153 "cannot set name of output/set dimension",
2154 return isl_aff_free(aff
));
2155 if (type
== isl_dim_in
)
2157 aff
->ls
= isl_local_space_set_dim_name(aff
->ls
, type
, pos
, s
);
2159 return isl_aff_free(aff
);
2164 __isl_give isl_aff
*isl_aff_set_dim_id(__isl_take isl_aff
*aff
,
2165 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
2167 aff
= isl_aff_cow(aff
);
2170 if (type
== isl_dim_out
)
2171 isl_die(aff
->v
->ctx
, isl_error_invalid
,
2172 "cannot set name of output/set dimension",
2174 if (type
== isl_dim_in
)
2176 aff
->ls
= isl_local_space_set_dim_id(aff
->ls
, type
, pos
, id
);
2178 return isl_aff_free(aff
);
2187 /* Replace the identifier of the input tuple of "aff" by "id".
2188 * type is currently required to be equal to isl_dim_in
2190 __isl_give isl_aff
*isl_aff_set_tuple_id(__isl_take isl_aff
*aff
,
2191 enum isl_dim_type type
, __isl_take isl_id
*id
)
2193 aff
= isl_aff_cow(aff
);
2196 if (type
!= isl_dim_in
)
2197 isl_die(aff
->v
->ctx
, isl_error_invalid
,
2198 "cannot only set id of input tuple", goto error
);
2199 aff
->ls
= isl_local_space_set_tuple_id(aff
->ls
, isl_dim_set
, id
);
2201 return isl_aff_free(aff
);
2210 /* Exploit the equalities in "eq" to simplify the affine expression
2211 * and the expressions of the integer divisions in the local space.
2212 * The integer divisions in this local space are assumed to appear
2213 * as regular dimensions in "eq".
2215 static __isl_give isl_aff
*isl_aff_substitute_equalities_lifted(
2216 __isl_take isl_aff
*aff
, __isl_take isl_basic_set
*eq
)
2224 if (eq
->n_eq
== 0) {
2225 isl_basic_set_free(eq
);
2229 aff
= isl_aff_cow(aff
);
2233 aff
->ls
= isl_local_space_substitute_equalities(aff
->ls
,
2234 isl_basic_set_copy(eq
));
2235 aff
->v
= isl_vec_cow(aff
->v
);
2236 if (!aff
->ls
|| !aff
->v
)
2239 o_div
= isl_basic_set_offset(eq
, isl_dim_div
);
2241 for (i
= 0; i
< eq
->n_eq
; ++i
) {
2242 j
= isl_seq_last_non_zero(eq
->eq
[i
], o_div
+ n_div
);
2243 if (j
< 0 || j
== 0 || j
>= o_div
)
2246 isl_seq_elim(aff
->v
->el
+ 1, eq
->eq
[i
], j
, o_div
,
2250 isl_basic_set_free(eq
);
2251 aff
= isl_aff_normalize(aff
);
2254 isl_basic_set_free(eq
);
2259 /* Exploit the equalities in "eq" to simplify the affine expression
2260 * and the expressions of the integer divisions in the local space.
2262 __isl_give isl_aff
*isl_aff_substitute_equalities(__isl_take isl_aff
*aff
,
2263 __isl_take isl_basic_set
*eq
)
2267 n_div
= isl_aff_domain_dim(aff
, isl_dim_div
);
2271 eq
= isl_basic_set_add_dims(eq
, isl_dim_set
, n_div
);
2272 return isl_aff_substitute_equalities_lifted(aff
, eq
);
2274 isl_basic_set_free(eq
);
2279 /* Look for equalities among the variables shared by context and aff
2280 * and the integer divisions of aff, if any.
2281 * The equalities are then used to eliminate coefficients and/or integer
2282 * divisions from aff.
2284 __isl_give isl_aff
*isl_aff_gist(__isl_take isl_aff
*aff
,
2285 __isl_take isl_set
*context
)
2287 isl_local_space
*ls
;
2288 isl_basic_set
*hull
;
2290 ls
= isl_aff_get_domain_local_space(aff
);
2291 context
= isl_local_space_lift_set(ls
, context
);
2293 hull
= isl_set_affine_hull(context
);
2294 return isl_aff_substitute_equalities_lifted(aff
, hull
);
2297 __isl_give isl_aff
*isl_aff_gist_params(__isl_take isl_aff
*aff
,
2298 __isl_take isl_set
*context
)
2300 isl_set
*dom_context
= isl_set_universe(isl_aff_get_domain_space(aff
));
2301 dom_context
= isl_set_intersect_params(dom_context
, context
);
2302 return isl_aff_gist(aff
, dom_context
);
2305 /* Return a basic set containing those elements in the space
2306 * of aff where it is positive. "rational" should not be set.
2308 * If "aff" is NaN, then it is not positive.
2310 static __isl_give isl_basic_set
*aff_pos_basic_set(__isl_take isl_aff
*aff
,
2311 int rational
, void *user
)
2313 isl_constraint
*ineq
;
2314 isl_basic_set
*bset
;
2319 if (isl_aff_is_nan(aff
)) {
2320 isl_space
*space
= isl_aff_get_domain_space(aff
);
2322 return isl_basic_set_empty(space
);
2325 isl_die(isl_aff_get_ctx(aff
), isl_error_unsupported
,
2326 "rational sets not supported", goto error
);
2328 ineq
= isl_inequality_from_aff(aff
);
2329 c
= isl_constraint_get_constant_val(ineq
);
2330 c
= isl_val_sub_ui(c
, 1);
2331 ineq
= isl_constraint_set_constant_val(ineq
, c
);
2333 bset
= isl_basic_set_from_constraint(ineq
);
2334 bset
= isl_basic_set_simplify(bset
);
2341 /* Return a basic set containing those elements in the space
2342 * of aff where it is non-negative.
2343 * If "rational" is set, then return a rational basic set.
2345 * If "aff" is NaN, then it is not non-negative (it's not negative either).
2347 static __isl_give isl_basic_set
*aff_nonneg_basic_set(
2348 __isl_take isl_aff
*aff
, int rational
, void *user
)
2350 isl_constraint
*ineq
;
2351 isl_basic_set
*bset
;
2355 if (isl_aff_is_nan(aff
)) {
2356 isl_space
*space
= isl_aff_get_domain_space(aff
);
2358 return isl_basic_set_empty(space
);
2361 ineq
= isl_inequality_from_aff(aff
);
2363 bset
= isl_basic_set_from_constraint(ineq
);
2365 bset
= isl_basic_set_set_rational(bset
);
2366 bset
= isl_basic_set_simplify(bset
);
2370 /* Return a basic set containing those elements in the space
2371 * of aff where it is non-negative.
2373 __isl_give isl_basic_set
*isl_aff_nonneg_basic_set(__isl_take isl_aff
*aff
)
2375 return aff_nonneg_basic_set(aff
, 0, NULL
);
2378 /* Return a basic set containing those elements in the domain space
2379 * of "aff" where it is positive.
2381 __isl_give isl_basic_set
*isl_aff_pos_basic_set(__isl_take isl_aff
*aff
)
2383 aff
= isl_aff_add_constant_num_si(aff
, -1);
2384 return isl_aff_nonneg_basic_set(aff
);
2387 /* Return a basic set containing those elements in the domain space
2388 * of aff where it is negative.
2390 __isl_give isl_basic_set
*isl_aff_neg_basic_set(__isl_take isl_aff
*aff
)
2392 aff
= isl_aff_neg(aff
);
2393 return isl_aff_pos_basic_set(aff
);
2396 /* Return a basic set containing those elements in the space
2397 * of aff where it is zero.
2398 * If "rational" is set, then return a rational basic set.
2400 * If "aff" is NaN, then it is not zero.
2402 static __isl_give isl_basic_set
*aff_zero_basic_set(__isl_take isl_aff
*aff
,
2403 int rational
, void *user
)
2405 isl_constraint
*ineq
;
2406 isl_basic_set
*bset
;
2410 if (isl_aff_is_nan(aff
)) {
2411 isl_space
*space
= isl_aff_get_domain_space(aff
);
2413 return isl_basic_set_empty(space
);
2416 ineq
= isl_equality_from_aff(aff
);
2418 bset
= isl_basic_set_from_constraint(ineq
);
2420 bset
= isl_basic_set_set_rational(bset
);
2421 bset
= isl_basic_set_simplify(bset
);
2425 /* Return a basic set containing those elements in the space
2426 * of aff where it is zero.
2428 __isl_give isl_basic_set
*isl_aff_zero_basic_set(__isl_take isl_aff
*aff
)
2430 return aff_zero_basic_set(aff
, 0, NULL
);
2433 /* Return a basic set containing those elements in the shared space
2434 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
2436 __isl_give isl_basic_set
*isl_aff_ge_basic_set(__isl_take isl_aff
*aff1
,
2437 __isl_take isl_aff
*aff2
)
2439 aff1
= isl_aff_sub(aff1
, aff2
);
2441 return isl_aff_nonneg_basic_set(aff1
);
2444 /* Return a basic set containing those elements in the shared domain space
2445 * of "aff1" and "aff2" where "aff1" is greater than "aff2".
2447 __isl_give isl_basic_set
*isl_aff_gt_basic_set(__isl_take isl_aff
*aff1
,
2448 __isl_take isl_aff
*aff2
)
2450 aff1
= isl_aff_sub(aff1
, aff2
);
2452 return isl_aff_pos_basic_set(aff1
);
2455 /* Return a set containing those elements in the shared space
2456 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
2458 __isl_give isl_set
*isl_aff_ge_set(__isl_take isl_aff
*aff1
,
2459 __isl_take isl_aff
*aff2
)
2461 return isl_set_from_basic_set(isl_aff_ge_basic_set(aff1
, aff2
));
2464 /* Return a set containing those elements in the shared domain space
2465 * of aff1 and aff2 where aff1 is greater than aff2.
2467 * If either of the two inputs is NaN, then the result is empty,
2468 * as comparisons with NaN always return false.
2470 __isl_give isl_set
*isl_aff_gt_set(__isl_take isl_aff
*aff1
,
2471 __isl_take isl_aff
*aff2
)
2473 return isl_set_from_basic_set(isl_aff_gt_basic_set(aff1
, aff2
));
2476 /* Return a basic set containing those elements in the shared space
2477 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
2479 __isl_give isl_basic_set
*isl_aff_le_basic_set(__isl_take isl_aff
*aff1
,
2480 __isl_take isl_aff
*aff2
)
2482 return isl_aff_ge_basic_set(aff2
, aff1
);
2485 /* Return a basic set containing those elements in the shared domain space
2486 * of "aff1" and "aff2" where "aff1" is smaller than "aff2".
2488 __isl_give isl_basic_set
*isl_aff_lt_basic_set(__isl_take isl_aff
*aff1
,
2489 __isl_take isl_aff
*aff2
)
2491 return isl_aff_gt_basic_set(aff2
, aff1
);
2494 /* Return a set containing those elements in the shared space
2495 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
2497 __isl_give isl_set
*isl_aff_le_set(__isl_take isl_aff
*aff1
,
2498 __isl_take isl_aff
*aff2
)
2500 return isl_aff_ge_set(aff2
, aff1
);
2503 /* Return a set containing those elements in the shared domain space
2504 * of "aff1" and "aff2" where "aff1" is smaller than "aff2".
2506 __isl_give isl_set
*isl_aff_lt_set(__isl_take isl_aff
*aff1
,
2507 __isl_take isl_aff
*aff2
)
2509 return isl_set_from_basic_set(isl_aff_lt_basic_set(aff1
, aff2
));
2512 /* Return a basic set containing those elements in the shared space
2513 * of aff1 and aff2 where aff1 and aff2 are equal.
2515 __isl_give isl_basic_set
*isl_aff_eq_basic_set(__isl_take isl_aff
*aff1
,
2516 __isl_take isl_aff
*aff2
)
2518 aff1
= isl_aff_sub(aff1
, aff2
);
2520 return isl_aff_zero_basic_set(aff1
);
2523 /* Return a set containing those elements in the shared space
2524 * of aff1 and aff2 where aff1 and aff2 are equal.
2526 __isl_give isl_set
*isl_aff_eq_set(__isl_take isl_aff
*aff1
,
2527 __isl_take isl_aff
*aff2
)
2529 return isl_set_from_basic_set(isl_aff_eq_basic_set(aff1
, aff2
));
2532 /* Return a set containing those elements in the shared domain space
2533 * of aff1 and aff2 where aff1 and aff2 are not equal.
2535 * If either of the two inputs is NaN, then the result is empty,
2536 * as comparisons with NaN always return false.
2538 __isl_give isl_set
*isl_aff_ne_set(__isl_take isl_aff
*aff1
,
2539 __isl_take isl_aff
*aff2
)
2541 isl_set
*set_lt
, *set_gt
;
2543 set_lt
= isl_aff_lt_set(isl_aff_copy(aff1
),
2544 isl_aff_copy(aff2
));
2545 set_gt
= isl_aff_gt_set(aff1
, aff2
);
2546 return isl_set_union_disjoint(set_lt
, set_gt
);
2549 __isl_give isl_aff
*isl_aff_add_on_domain(__isl_keep isl_set
*dom
,
2550 __isl_take isl_aff
*aff1
, __isl_take isl_aff
*aff2
)
2552 aff1
= isl_aff_add(aff1
, aff2
);
2553 aff1
= isl_aff_gist(aff1
, isl_set_copy(dom
));
2557 isl_bool
isl_aff_is_empty(__isl_keep isl_aff
*aff
)
2560 return isl_bool_error
;
2562 return isl_bool_false
;
2566 #define TYPE isl_aff
2568 #include "check_type_range_templ.c"
2570 /* Check whether the given affine expression has non-zero coefficient
2571 * for any dimension in the given range or if any of these dimensions
2572 * appear with non-zero coefficients in any of the integer divisions
2573 * involved in the affine expression.
2575 isl_bool
isl_aff_involves_dims(__isl_keep isl_aff
*aff
,
2576 enum isl_dim_type type
, unsigned first
, unsigned n
)
2580 isl_bool involves
= isl_bool_false
;
2583 return isl_bool_error
;
2585 return isl_bool_false
;
2586 if (isl_aff_check_range(aff
, type
, first
, n
) < 0)
2587 return isl_bool_error
;
2589 active
= isl_local_space_get_active(aff
->ls
, aff
->v
->el
+ 2);
2593 first
+= isl_local_space_offset(aff
->ls
, type
) - 1;
2594 for (i
= 0; i
< n
; ++i
)
2595 if (active
[first
+ i
]) {
2596 involves
= isl_bool_true
;
2605 return isl_bool_error
;
2608 /* Does "aff" involve any local variables, i.e., integer divisions?
2610 isl_bool
isl_aff_involves_locals(__isl_keep isl_aff
*aff
)
2614 n
= isl_aff_dim(aff
, isl_dim_div
);
2616 return isl_bool_error
;
2617 return isl_bool_ok(n
> 0);
2620 __isl_give isl_aff
*isl_aff_drop_dims(__isl_take isl_aff
*aff
,
2621 enum isl_dim_type type
, unsigned first
, unsigned n
)
2625 if (type
== isl_dim_out
)
2626 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2627 "cannot drop output/set dimension",
2628 return isl_aff_free(aff
));
2629 if (type
== isl_dim_in
)
2631 if (n
== 0 && !isl_local_space_is_named_or_nested(aff
->ls
, type
))
2634 if (isl_local_space_check_range(aff
->ls
, type
, first
, n
) < 0)
2635 return isl_aff_free(aff
);
2637 aff
= isl_aff_cow(aff
);
2641 aff
->ls
= isl_local_space_drop_dims(aff
->ls
, type
, first
, n
);
2643 return isl_aff_free(aff
);
2645 first
+= 1 + isl_local_space_offset(aff
->ls
, type
);
2646 aff
->v
= isl_vec_drop_els(aff
->v
, first
, n
);
2648 return isl_aff_free(aff
);
2653 /* Is the domain of "aff" a product?
2655 static isl_bool
isl_aff_domain_is_product(__isl_keep isl_aff
*aff
)
2657 return isl_space_is_product(isl_aff_peek_domain_space(aff
));
2661 #define TYPE isl_aff
2662 #include <isl_domain_factor_templ.c>
2664 /* Project the domain of the affine expression onto its parameter space.
2665 * The affine expression may not involve any of the domain dimensions.
2667 __isl_give isl_aff
*isl_aff_project_domain_on_params(__isl_take isl_aff
*aff
)
2672 n
= isl_aff_dim(aff
, isl_dim_in
);
2674 return isl_aff_free(aff
);
2675 aff
= isl_aff_drop_domain(aff
, 0, n
);
2676 space
= isl_aff_get_domain_space(aff
);
2677 space
= isl_space_params(space
);
2678 aff
= isl_aff_reset_domain_space(aff
, space
);
2682 /* Convert an affine expression defined over a parameter domain
2683 * into one that is defined over a zero-dimensional set.
2685 __isl_give isl_aff
*isl_aff_from_range(__isl_take isl_aff
*aff
)
2687 isl_local_space
*ls
;
2689 ls
= isl_aff_take_domain_local_space(aff
);
2690 ls
= isl_local_space_set_from_params(ls
);
2691 aff
= isl_aff_restore_domain_local_space(aff
, ls
);
2696 __isl_give isl_aff
*isl_aff_insert_dims(__isl_take isl_aff
*aff
,
2697 enum isl_dim_type type
, unsigned first
, unsigned n
)
2701 if (type
== isl_dim_out
)
2702 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2703 "cannot insert output/set dimensions",
2704 return isl_aff_free(aff
));
2705 if (type
== isl_dim_in
)
2707 if (n
== 0 && !isl_local_space_is_named_or_nested(aff
->ls
, type
))
2710 if (isl_local_space_check_range(aff
->ls
, type
, first
, 0) < 0)
2711 return isl_aff_free(aff
);
2713 aff
= isl_aff_cow(aff
);
2717 aff
->ls
= isl_local_space_insert_dims(aff
->ls
, type
, first
, n
);
2719 return isl_aff_free(aff
);
2721 first
+= 1 + isl_local_space_offset(aff
->ls
, type
);
2722 aff
->v
= isl_vec_insert_zero_els(aff
->v
, first
, n
);
2724 return isl_aff_free(aff
);
2729 __isl_give isl_aff
*isl_aff_add_dims(__isl_take isl_aff
*aff
,
2730 enum isl_dim_type type
, unsigned n
)
2734 pos
= isl_aff_dim(aff
, type
);
2736 return isl_aff_free(aff
);
2738 return isl_aff_insert_dims(aff
, type
, pos
, n
);
2741 /* Move the "n" dimensions of "src_type" starting at "src_pos" of "aff"
2742 * to dimensions of "dst_type" at "dst_pos".
2744 * We only support moving input dimensions to parameters and vice versa.
2746 __isl_give isl_aff
*isl_aff_move_dims(__isl_take isl_aff
*aff
,
2747 enum isl_dim_type dst_type
, unsigned dst_pos
,
2748 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
2752 isl_size src_off
, dst_off
;
2757 !isl_local_space_is_named_or_nested(aff
->ls
, src_type
) &&
2758 !isl_local_space_is_named_or_nested(aff
->ls
, dst_type
))
2761 if (dst_type
== isl_dim_out
|| src_type
== isl_dim_out
)
2762 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2763 "cannot move output/set dimension",
2764 return isl_aff_free(aff
));
2765 if (dst_type
== isl_dim_div
|| src_type
== isl_dim_div
)
2766 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2767 "cannot move divs", return isl_aff_free(aff
));
2768 if (dst_type
== isl_dim_in
)
2769 dst_type
= isl_dim_set
;
2770 if (src_type
== isl_dim_in
)
2771 src_type
= isl_dim_set
;
2773 if (isl_local_space_check_range(aff
->ls
, src_type
, src_pos
, n
) < 0)
2774 return isl_aff_free(aff
);
2775 if (dst_type
== src_type
)
2776 isl_die(isl_aff_get_ctx(aff
), isl_error_unsupported
,
2777 "moving dims within the same type not supported",
2778 return isl_aff_free(aff
));
2780 aff
= isl_aff_cow(aff
);
2781 src_off
= isl_aff_domain_offset(aff
, src_type
);
2782 dst_off
= isl_aff_domain_offset(aff
, dst_type
);
2783 if (src_off
< 0 || dst_off
< 0)
2784 return isl_aff_free(aff
);
2786 g_src_pos
= 1 + src_off
+ src_pos
;
2787 g_dst_pos
= 1 + dst_off
+ dst_pos
;
2788 if (dst_type
> src_type
)
2791 aff
->v
= isl_vec_move_els(aff
->v
, g_dst_pos
, g_src_pos
, n
);
2792 aff
->ls
= isl_local_space_move_dims(aff
->ls
, dst_type
, dst_pos
,
2793 src_type
, src_pos
, n
);
2794 if (!aff
->v
|| !aff
->ls
)
2795 return isl_aff_free(aff
);
2797 aff
= sort_divs(aff
);
2802 /* Return a zero isl_aff in the given space.
2804 * This is a helper function for isl_pw_*_as_* that ensures a uniform
2805 * interface over all piecewise types.
2807 static __isl_give isl_aff
*isl_aff_zero_in_space(__isl_take isl_space
*space
)
2809 isl_local_space
*ls
;
2811 ls
= isl_local_space_from_space(isl_space_domain(space
));
2812 return isl_aff_zero_on_domain(ls
);
2815 #define isl_aff_involves_nan isl_aff_is_nan
2818 #define PW isl_pw_aff
2822 #define EL_IS_ZERO is_empty
2826 #define IS_ZERO is_empty
2829 #undef DEFAULT_IS_ZERO
2830 #define DEFAULT_IS_ZERO 0
2832 #include <isl_pw_templ.c>
2833 #include <isl_pw_un_op_templ.c>
2834 #include <isl_pw_add_constant_val_templ.c>
2835 #include <isl_pw_bind_domain_templ.c>
2836 #include <isl_pw_eval.c>
2837 #include <isl_pw_hash.c>
2838 #include <isl_pw_insert_dims_templ.c>
2839 #include <isl_pw_insert_domain_templ.c>
2840 #include <isl_pw_move_dims_templ.c>
2841 #include <isl_pw_neg_templ.c>
2842 #include <isl_pw_pullback_templ.c>
2843 #include <isl_pw_sub_templ.c>
2844 #include <isl_pw_union_opt.c>
2849 #include <isl_union_single.c>
2850 #include <isl_union_neg.c>
2855 #include <isl_union_pw_templ.c>
2857 /* Compute a piecewise quasi-affine expression with a domain that
2858 * is the union of those of pwaff1 and pwaff2 and such that on each
2859 * cell, the quasi-affine expression is the maximum of those of pwaff1
2860 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2861 * cell, then the associated expression is the defined one.
2863 __isl_give isl_pw_aff
*isl_pw_aff_union_max(__isl_take isl_pw_aff
*pwaff1
,
2864 __isl_take isl_pw_aff
*pwaff2
)
2866 isl_pw_aff_align_params_bin(&pwaff1
, &pwaff2
);
2867 return isl_pw_aff_union_opt_cmp(pwaff1
, pwaff2
, &isl_aff_ge_set
);
2870 /* Compute a piecewise quasi-affine expression with a domain that
2871 * is the union of those of pwaff1 and pwaff2 and such that on each
2872 * cell, the quasi-affine expression is the minimum of those of pwaff1
2873 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2874 * cell, then the associated expression is the defined one.
2876 __isl_give isl_pw_aff
*isl_pw_aff_union_min(__isl_take isl_pw_aff
*pwaff1
,
2877 __isl_take isl_pw_aff
*pwaff2
)
2879 isl_pw_aff_align_params_bin(&pwaff1
, &pwaff2
);
2880 return isl_pw_aff_union_opt_cmp(pwaff1
, pwaff2
, &isl_aff_le_set
);
2883 __isl_give isl_pw_aff
*isl_pw_aff_union_opt(__isl_take isl_pw_aff
*pwaff1
,
2884 __isl_take isl_pw_aff
*pwaff2
, int max
)
2887 return isl_pw_aff_union_max(pwaff1
, pwaff2
);
2889 return isl_pw_aff_union_min(pwaff1
, pwaff2
);
2892 /* Is the domain of "pa" a product?
2894 static isl_bool
isl_pw_aff_domain_is_product(__isl_keep isl_pw_aff
*pa
)
2896 return isl_space_domain_is_wrapping(isl_pw_aff_peek_space(pa
));
2900 #define TYPE isl_pw_aff
2901 #include <isl_domain_factor_templ.c>
2903 /* Return a set containing those elements in the domain
2904 * of "pwaff" where it satisfies "fn" (if complement is 0) or
2905 * does not satisfy "fn" (if complement is 1).
2907 * The pieces with a NaN never belong to the result since
2908 * NaN does not satisfy any property.
2910 static __isl_give isl_set
*pw_aff_locus(__isl_take isl_pw_aff
*pwaff
,
2911 __isl_give isl_basic_set
*(*fn
)(__isl_take isl_aff
*aff
, int rational
,
2913 int complement
, void *user
)
2921 set
= isl_set_empty(isl_pw_aff_get_domain_space(pwaff
));
2923 for (i
= 0; i
< pwaff
->n
; ++i
) {
2924 isl_basic_set
*bset
;
2925 isl_set
*set_i
, *locus
;
2928 if (isl_aff_is_nan(pwaff
->p
[i
].aff
))
2931 rational
= isl_set_has_rational(pwaff
->p
[i
].set
);
2932 bset
= fn(isl_aff_copy(pwaff
->p
[i
].aff
), rational
, user
);
2933 locus
= isl_set_from_basic_set(bset
);
2934 set_i
= isl_set_copy(pwaff
->p
[i
].set
);
2936 set_i
= isl_set_subtract(set_i
, locus
);
2938 set_i
= isl_set_intersect(set_i
, locus
);
2939 set
= isl_set_union_disjoint(set
, set_i
);
2942 isl_pw_aff_free(pwaff
);
2947 /* Return a set containing those elements in the domain
2948 * of "pa" where it is positive.
2950 __isl_give isl_set
*isl_pw_aff_pos_set(__isl_take isl_pw_aff
*pa
)
2952 return pw_aff_locus(pa
, &aff_pos_basic_set
, 0, NULL
);
2955 /* Return a set containing those elements in the domain
2956 * of pwaff where it is non-negative.
2958 __isl_give isl_set
*isl_pw_aff_nonneg_set(__isl_take isl_pw_aff
*pwaff
)
2960 return pw_aff_locus(pwaff
, &aff_nonneg_basic_set
, 0, NULL
);
2963 /* Return a set containing those elements in the domain
2964 * of pwaff where it is zero.
2966 __isl_give isl_set
*isl_pw_aff_zero_set(__isl_take isl_pw_aff
*pwaff
)
2968 return pw_aff_locus(pwaff
, &aff_zero_basic_set
, 0, NULL
);
2971 /* Return a set containing those elements in the domain
2972 * of pwaff where it is not zero.
2974 __isl_give isl_set
*isl_pw_aff_non_zero_set(__isl_take isl_pw_aff
*pwaff
)
2976 return pw_aff_locus(pwaff
, &aff_zero_basic_set
, 1, NULL
);
2979 /* Bind the affine function "aff" to the parameter "id",
2980 * returning the elements in the domain where the affine expression
2981 * is equal to the parameter.
2983 __isl_give isl_basic_set
*isl_aff_bind_id(__isl_take isl_aff
*aff
,
2984 __isl_take isl_id
*id
)
2989 space
= isl_aff_get_domain_space(aff
);
2990 space
= isl_space_add_param_id(space
, isl_id_copy(id
));
2992 aff
= isl_aff_align_params(aff
, isl_space_copy(space
));
2993 aff_id
= isl_aff_param_on_domain_space_id(space
, id
);
2995 return isl_aff_eq_basic_set(aff
, aff_id
);
2998 /* Wrapper around isl_aff_bind_id for use as pw_aff_locus callback.
2999 * "rational" should not be set.
3001 static __isl_give isl_basic_set
*aff_bind_id(__isl_take isl_aff
*aff
,
3002 int rational
, void *user
)
3009 isl_die(isl_aff_get_ctx(aff
), isl_error_unsupported
,
3010 "rational binding not supported", goto error
);
3011 return isl_aff_bind_id(aff
, isl_id_copy(id
));
3017 /* Bind the piecewise affine function "pa" to the parameter "id",
3018 * returning the elements in the domain where the expression
3019 * is equal to the parameter.
3021 __isl_give isl_set
*isl_pw_aff_bind_id(__isl_take isl_pw_aff
*pa
,
3022 __isl_take isl_id
*id
)
3026 bound
= pw_aff_locus(pa
, &aff_bind_id
, 0, id
);
3032 /* Return a set containing those elements in the shared domain
3033 * of pwaff1 and pwaff2 where pwaff1 is greater than (or equal) to pwaff2.
3035 * We compute the difference on the shared domain and then construct
3036 * the set of values where this difference is non-negative.
3037 * If strict is set, we first subtract 1 from the difference.
3038 * If equal is set, we only return the elements where pwaff1 and pwaff2
3041 static __isl_give isl_set
*pw_aff_gte_set(__isl_take isl_pw_aff
*pwaff1
,
3042 __isl_take isl_pw_aff
*pwaff2
, int strict
, int equal
)
3044 isl_set
*set1
, *set2
;
3046 set1
= isl_pw_aff_domain(isl_pw_aff_copy(pwaff1
));
3047 set2
= isl_pw_aff_domain(isl_pw_aff_copy(pwaff2
));
3048 set1
= isl_set_intersect(set1
, set2
);
3049 pwaff1
= isl_pw_aff_intersect_domain(pwaff1
, isl_set_copy(set1
));
3050 pwaff2
= isl_pw_aff_intersect_domain(pwaff2
, isl_set_copy(set1
));
3051 pwaff1
= isl_pw_aff_add(pwaff1
, isl_pw_aff_neg(pwaff2
));
3054 isl_space
*space
= isl_set_get_space(set1
);
3056 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(space
));
3057 aff
= isl_aff_add_constant_si(aff
, -1);
3058 pwaff1
= isl_pw_aff_add(pwaff1
, isl_pw_aff_alloc(set1
, aff
));
3063 return isl_pw_aff_zero_set(pwaff1
);
3064 return isl_pw_aff_nonneg_set(pwaff1
);
3067 /* Return a set containing those elements in the shared domain
3068 * of pwaff1 and pwaff2 where pwaff1 is equal to pwaff2.
3070 __isl_give isl_set
*isl_pw_aff_eq_set(__isl_take isl_pw_aff
*pwaff1
,
3071 __isl_take isl_pw_aff
*pwaff2
)
3073 isl_pw_aff_align_params_bin(&pwaff1
, &pwaff2
);
3074 return pw_aff_gte_set(pwaff1
, pwaff2
, 0, 1);
3077 /* Return a set containing those elements in the shared domain
3078 * of pwaff1 and pwaff2 where pwaff1 is greater than or equal to pwaff2.
3080 __isl_give isl_set
*isl_pw_aff_ge_set(__isl_take isl_pw_aff
*pwaff1
,
3081 __isl_take isl_pw_aff
*pwaff2
)
3083 isl_pw_aff_align_params_bin(&pwaff1
, &pwaff2
);
3084 return pw_aff_gte_set(pwaff1
, pwaff2
, 0, 0);
3087 /* Return a set containing those elements in the shared domain
3088 * of pwaff1 and pwaff2 where pwaff1 is strictly greater than pwaff2.
3090 __isl_give isl_set
*isl_pw_aff_gt_set(__isl_take isl_pw_aff
*pwaff1
,
3091 __isl_take isl_pw_aff
*pwaff2
)
3093 isl_pw_aff_align_params_bin(&pwaff1
, &pwaff2
);
3094 return pw_aff_gte_set(pwaff1
, pwaff2
, 1, 0);
3097 __isl_give isl_set
*isl_pw_aff_le_set(__isl_take isl_pw_aff
*pwaff1
,
3098 __isl_take isl_pw_aff
*pwaff2
)
3100 return isl_pw_aff_ge_set(pwaff2
, pwaff1
);
3103 __isl_give isl_set
*isl_pw_aff_lt_set(__isl_take isl_pw_aff
*pwaff1
,
3104 __isl_take isl_pw_aff
*pwaff2
)
3106 return isl_pw_aff_gt_set(pwaff2
, pwaff1
);
3109 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3110 * where the function values are ordered in the same way as "order",
3111 * which returns a set in the shared domain of its two arguments.
3113 * Let "pa1" and "pa2" be defined on domains A and B respectively.
3114 * We first pull back the two functions such that they are defined on
3115 * the domain [A -> B]. Then we apply "order", resulting in a set
3116 * in the space [A -> B]. Finally, we unwrap this set to obtain
3117 * a map in the space A -> B.
3119 static __isl_give isl_map
*isl_pw_aff_order_map(
3120 __isl_take isl_pw_aff
*pa1
, __isl_take isl_pw_aff
*pa2
,
3121 __isl_give isl_set
*(*order
)(__isl_take isl_pw_aff
*pa1
,
3122 __isl_take isl_pw_aff
*pa2
))
3124 isl_space
*space1
, *space2
;
3128 isl_pw_aff_align_params_bin(&pa1
, &pa2
);
3129 space1
= isl_space_domain(isl_pw_aff_get_space(pa1
));
3130 space2
= isl_space_domain(isl_pw_aff_get_space(pa2
));
3131 space1
= isl_space_map_from_domain_and_range(space1
, space2
);
3132 ma
= isl_multi_aff_domain_map(isl_space_copy(space1
));
3133 pa1
= isl_pw_aff_pullback_multi_aff(pa1
, ma
);
3134 ma
= isl_multi_aff_range_map(space1
);
3135 pa2
= isl_pw_aff_pullback_multi_aff(pa2
, ma
);
3136 set
= order(pa1
, pa2
);
3138 return isl_set_unwrap(set
);
3141 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3142 * where the function values are equal.
3144 __isl_give isl_map
*isl_pw_aff_eq_map(__isl_take isl_pw_aff
*pa1
,
3145 __isl_take isl_pw_aff
*pa2
)
3147 return isl_pw_aff_order_map(pa1
, pa2
, &isl_pw_aff_eq_set
);
3150 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3151 * where the function value of "pa1" is less than or equal to
3152 * the function value of "pa2".
3154 __isl_give isl_map
*isl_pw_aff_le_map(__isl_take isl_pw_aff
*pa1
,
3155 __isl_take isl_pw_aff
*pa2
)
3157 return isl_pw_aff_order_map(pa1
, pa2
, &isl_pw_aff_le_set
);
3160 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3161 * where the function value of "pa1" is less than the function value of "pa2".
3163 __isl_give isl_map
*isl_pw_aff_lt_map(__isl_take isl_pw_aff
*pa1
,
3164 __isl_take isl_pw_aff
*pa2
)
3166 return isl_pw_aff_order_map(pa1
, pa2
, &isl_pw_aff_lt_set
);
3169 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3170 * where the function value of "pa1" is greater than or equal to
3171 * the function value of "pa2".
3173 __isl_give isl_map
*isl_pw_aff_ge_map(__isl_take isl_pw_aff
*pa1
,
3174 __isl_take isl_pw_aff
*pa2
)
3176 return isl_pw_aff_order_map(pa1
, pa2
, &isl_pw_aff_ge_set
);
3179 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3180 * where the function value of "pa1" is greater than the function value
3183 __isl_give isl_map
*isl_pw_aff_gt_map(__isl_take isl_pw_aff
*pa1
,
3184 __isl_take isl_pw_aff
*pa2
)
3186 return isl_pw_aff_order_map(pa1
, pa2
, &isl_pw_aff_gt_set
);
3189 /* Return a set containing those elements in the shared domain
3190 * of the elements of list1 and list2 where each element in list1
3191 * has the relation specified by "fn" with each element in list2.
3193 static __isl_give isl_set
*pw_aff_list_set(__isl_take isl_pw_aff_list
*list1
,
3194 __isl_take isl_pw_aff_list
*list2
,
3195 __isl_give isl_set
*(*fn
)(__isl_take isl_pw_aff
*pwaff1
,
3196 __isl_take isl_pw_aff
*pwaff2
))
3202 if (!list1
|| !list2
)
3205 ctx
= isl_pw_aff_list_get_ctx(list1
);
3206 if (list1
->n
< 1 || list2
->n
< 1)
3207 isl_die(ctx
, isl_error_invalid
,
3208 "list should contain at least one element", goto error
);
3210 set
= isl_set_universe(isl_pw_aff_get_domain_space(list1
->p
[0]));
3211 for (i
= 0; i
< list1
->n
; ++i
)
3212 for (j
= 0; j
< list2
->n
; ++j
) {
3215 set_ij
= fn(isl_pw_aff_copy(list1
->p
[i
]),
3216 isl_pw_aff_copy(list2
->p
[j
]));
3217 set
= isl_set_intersect(set
, set_ij
);
3220 isl_pw_aff_list_free(list1
);
3221 isl_pw_aff_list_free(list2
);
3224 isl_pw_aff_list_free(list1
);
3225 isl_pw_aff_list_free(list2
);
3229 /* Return a set containing those elements in the shared domain
3230 * of the elements of list1 and list2 where each element in list1
3231 * is equal to each element in list2.
3233 __isl_give isl_set
*isl_pw_aff_list_eq_set(__isl_take isl_pw_aff_list
*list1
,
3234 __isl_take isl_pw_aff_list
*list2
)
3236 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_eq_set
);
3239 __isl_give isl_set
*isl_pw_aff_list_ne_set(__isl_take isl_pw_aff_list
*list1
,
3240 __isl_take isl_pw_aff_list
*list2
)
3242 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_ne_set
);
3245 /* Return a set containing those elements in the shared domain
3246 * of the elements of list1 and list2 where each element in list1
3247 * is less than or equal to each element in list2.
3249 __isl_give isl_set
*isl_pw_aff_list_le_set(__isl_take isl_pw_aff_list
*list1
,
3250 __isl_take isl_pw_aff_list
*list2
)
3252 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_le_set
);
3255 __isl_give isl_set
*isl_pw_aff_list_lt_set(__isl_take isl_pw_aff_list
*list1
,
3256 __isl_take isl_pw_aff_list
*list2
)
3258 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_lt_set
);
3261 __isl_give isl_set
*isl_pw_aff_list_ge_set(__isl_take isl_pw_aff_list
*list1
,
3262 __isl_take isl_pw_aff_list
*list2
)
3264 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_ge_set
);
3267 __isl_give isl_set
*isl_pw_aff_list_gt_set(__isl_take isl_pw_aff_list
*list1
,
3268 __isl_take isl_pw_aff_list
*list2
)
3270 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_gt_set
);
3274 /* Return a set containing those elements in the shared domain
3275 * of pwaff1 and pwaff2 where pwaff1 is not equal to pwaff2.
3277 __isl_give isl_set
*isl_pw_aff_ne_set(__isl_take isl_pw_aff
*pwaff1
,
3278 __isl_take isl_pw_aff
*pwaff2
)
3280 isl_set
*set_lt
, *set_gt
;
3282 isl_pw_aff_align_params_bin(&pwaff1
, &pwaff2
);
3283 set_lt
= isl_pw_aff_lt_set(isl_pw_aff_copy(pwaff1
),
3284 isl_pw_aff_copy(pwaff2
));
3285 set_gt
= isl_pw_aff_gt_set(pwaff1
, pwaff2
);
3286 return isl_set_union_disjoint(set_lt
, set_gt
);
3289 __isl_give isl_pw_aff
*isl_pw_aff_scale_down(__isl_take isl_pw_aff
*pwaff
,
3294 if (isl_int_is_one(v
))
3296 if (!isl_int_is_pos(v
))
3297 isl_die(isl_pw_aff_get_ctx(pwaff
), isl_error_invalid
,
3298 "factor needs to be positive",
3299 return isl_pw_aff_free(pwaff
));
3300 pwaff
= isl_pw_aff_cow(pwaff
);
3306 for (i
= 0; i
< pwaff
->n
; ++i
) {
3307 pwaff
->p
[i
].aff
= isl_aff_scale_down(pwaff
->p
[i
].aff
, v
);
3308 if (!pwaff
->p
[i
].aff
)
3309 return isl_pw_aff_free(pwaff
);
3315 __isl_give isl_pw_aff
*isl_pw_aff_floor(__isl_take isl_pw_aff
*pwaff
)
3317 return isl_pw_aff_un_op(pwaff
, &isl_aff_floor
);
3320 __isl_give isl_pw_aff
*isl_pw_aff_ceil(__isl_take isl_pw_aff
*pwaff
)
3322 return isl_pw_aff_un_op(pwaff
, &isl_aff_ceil
);
3325 /* Assuming that "cond1" and "cond2" are disjoint,
3326 * return an affine expression that is equal to pwaff1 on cond1
3327 * and to pwaff2 on cond2.
3329 static __isl_give isl_pw_aff
*isl_pw_aff_select(
3330 __isl_take isl_set
*cond1
, __isl_take isl_pw_aff
*pwaff1
,
3331 __isl_take isl_set
*cond2
, __isl_take isl_pw_aff
*pwaff2
)
3333 pwaff1
= isl_pw_aff_intersect_domain(pwaff1
, cond1
);
3334 pwaff2
= isl_pw_aff_intersect_domain(pwaff2
, cond2
);
3336 return isl_pw_aff_add_disjoint(pwaff1
, pwaff2
);
3339 /* Return an affine expression that is equal to pwaff_true for elements
3340 * where "cond" is non-zero and to pwaff_false for elements where "cond"
3342 * That is, return cond ? pwaff_true : pwaff_false;
3344 * If "cond" involves and NaN, then we conservatively return a NaN
3345 * on its entire domain. In principle, we could consider the pieces
3346 * where it is NaN separately from those where it is not.
3348 * If "pwaff_true" and "pwaff_false" are obviously equal to each other,
3349 * then only use the domain of "cond" to restrict the domain.
3351 __isl_give isl_pw_aff
*isl_pw_aff_cond(__isl_take isl_pw_aff
*cond
,
3352 __isl_take isl_pw_aff
*pwaff_true
, __isl_take isl_pw_aff
*pwaff_false
)
3354 isl_set
*cond_true
, *cond_false
;
3359 if (isl_pw_aff_involves_nan(cond
)) {
3360 isl_space
*space
= isl_pw_aff_get_domain_space(cond
);
3361 isl_local_space
*ls
= isl_local_space_from_space(space
);
3362 isl_pw_aff_free(cond
);
3363 isl_pw_aff_free(pwaff_true
);
3364 isl_pw_aff_free(pwaff_false
);
3365 return isl_pw_aff_nan_on_domain(ls
);
3368 pwaff_true
= isl_pw_aff_align_params(pwaff_true
,
3369 isl_pw_aff_get_space(pwaff_false
));
3370 pwaff_false
= isl_pw_aff_align_params(pwaff_false
,
3371 isl_pw_aff_get_space(pwaff_true
));
3372 equal
= isl_pw_aff_plain_is_equal(pwaff_true
, pwaff_false
);
3378 dom
= isl_set_coalesce(isl_pw_aff_domain(cond
));
3379 isl_pw_aff_free(pwaff_false
);
3380 return isl_pw_aff_intersect_domain(pwaff_true
, dom
);
3383 cond_true
= isl_pw_aff_non_zero_set(isl_pw_aff_copy(cond
));
3384 cond_false
= isl_pw_aff_zero_set(cond
);
3385 return isl_pw_aff_select(cond_true
, pwaff_true
,
3386 cond_false
, pwaff_false
);
3388 isl_pw_aff_free(cond
);
3389 isl_pw_aff_free(pwaff_true
);
3390 isl_pw_aff_free(pwaff_false
);
3394 isl_bool
isl_aff_is_cst(__isl_keep isl_aff
*aff
)
3399 return isl_bool_error
;
3401 pos
= isl_seq_first_non_zero(aff
->v
->el
+ 2, aff
->v
->size
- 2);
3402 return isl_bool_ok(pos
== -1);
3405 /* Check whether pwaff is a piecewise constant.
3407 isl_bool
isl_pw_aff_is_cst(__isl_keep isl_pw_aff
*pwaff
)
3412 return isl_bool_error
;
3414 for (i
= 0; i
< pwaff
->n
; ++i
) {
3415 isl_bool is_cst
= isl_aff_is_cst(pwaff
->p
[i
].aff
);
3416 if (is_cst
< 0 || !is_cst
)
3420 return isl_bool_true
;
3423 /* Return the product of "aff1" and "aff2".
3425 * If either of the two is NaN, then the result is NaN.
3427 * Otherwise, at least one of "aff1" or "aff2" needs to be a constant.
3429 __isl_give isl_aff
*isl_aff_mul(__isl_take isl_aff
*aff1
,
3430 __isl_take isl_aff
*aff2
)
3435 if (isl_aff_is_nan(aff1
)) {
3439 if (isl_aff_is_nan(aff2
)) {
3444 if (!isl_aff_is_cst(aff2
) && isl_aff_is_cst(aff1
))
3445 return isl_aff_mul(aff2
, aff1
);
3447 if (!isl_aff_is_cst(aff2
))
3448 isl_die(isl_aff_get_ctx(aff1
), isl_error_invalid
,
3449 "at least one affine expression should be constant",
3452 aff1
= isl_aff_cow(aff1
);
3456 aff1
= isl_aff_scale(aff1
, aff2
->v
->el
[1]);
3457 aff1
= isl_aff_scale_down(aff1
, aff2
->v
->el
[0]);
3467 /* Divide "aff1" by "aff2", assuming "aff2" is a constant.
3469 * If either of the two is NaN, then the result is NaN.
3470 * A division by zero also results in NaN.
3472 __isl_give isl_aff
*isl_aff_div(__isl_take isl_aff
*aff1
,
3473 __isl_take isl_aff
*aff2
)
3475 isl_bool is_cst
, is_zero
;
3481 if (isl_aff_is_nan(aff1
)) {
3485 if (isl_aff_is_nan(aff2
)) {
3490 is_cst
= isl_aff_is_cst(aff2
);
3494 isl_die(isl_aff_get_ctx(aff2
), isl_error_invalid
,
3495 "second argument should be a constant", goto error
);
3496 is_zero
= isl_aff_plain_is_zero(aff2
);
3500 return set_nan_free(aff1
, aff2
);
3502 neg
= isl_int_is_neg(aff2
->v
->el
[1]);
3504 isl_int_neg(aff2
->v
->el
[0], aff2
->v
->el
[0]);
3505 isl_int_neg(aff2
->v
->el
[1], aff2
->v
->el
[1]);
3508 aff1
= isl_aff_scale(aff1
, aff2
->v
->el
[0]);
3509 aff1
= isl_aff_scale_down(aff1
, aff2
->v
->el
[1]);
3512 isl_int_neg(aff2
->v
->el
[0], aff2
->v
->el
[0]);
3513 isl_int_neg(aff2
->v
->el
[1], aff2
->v
->el
[1]);
3524 __isl_give isl_pw_aff
*isl_pw_aff_add(__isl_take isl_pw_aff
*pwaff1
,
3525 __isl_take isl_pw_aff
*pwaff2
)
3527 isl_pw_aff_align_params_bin(&pwaff1
, &pwaff2
);
3528 return isl_pw_aff_on_shared_domain(pwaff1
, pwaff2
, &isl_aff_add
);
3531 __isl_give isl_pw_aff
*isl_pw_aff_union_add(__isl_take isl_pw_aff
*pwaff1
,
3532 __isl_take isl_pw_aff
*pwaff2
)
3534 return isl_pw_aff_union_add_(pwaff1
, pwaff2
);
3537 __isl_give isl_pw_aff
*isl_pw_aff_mul(__isl_take isl_pw_aff
*pwaff1
,
3538 __isl_take isl_pw_aff
*pwaff2
)
3540 isl_pw_aff_align_params_bin(&pwaff1
, &pwaff2
);
3541 return isl_pw_aff_on_shared_domain(pwaff1
, pwaff2
, &isl_aff_mul
);
3544 /* Divide "pa1" by "pa2", assuming "pa2" is a piecewise constant.
3546 __isl_give isl_pw_aff
*isl_pw_aff_div(__isl_take isl_pw_aff
*pa1
,
3547 __isl_take isl_pw_aff
*pa2
)
3551 is_cst
= isl_pw_aff_is_cst(pa2
);
3555 isl_die(isl_pw_aff_get_ctx(pa2
), isl_error_invalid
,
3556 "second argument should be a piecewise constant",
3558 isl_pw_aff_align_params_bin(&pa1
, &pa2
);
3559 return isl_pw_aff_on_shared_domain(pa1
, pa2
, &isl_aff_div
);
3561 isl_pw_aff_free(pa1
);
3562 isl_pw_aff_free(pa2
);
3566 /* Compute the quotient of the integer division of "pa1" by "pa2"
3567 * with rounding towards zero.
3568 * "pa2" is assumed to be a piecewise constant.
3570 * In particular, return
3572 * pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2)
3575 __isl_give isl_pw_aff
*isl_pw_aff_tdiv_q(__isl_take isl_pw_aff
*pa1
,
3576 __isl_take isl_pw_aff
*pa2
)
3582 is_cst
= isl_pw_aff_is_cst(pa2
);
3586 isl_die(isl_pw_aff_get_ctx(pa2
), isl_error_invalid
,
3587 "second argument should be a piecewise constant",
3590 pa1
= isl_pw_aff_div(pa1
, pa2
);
3592 cond
= isl_pw_aff_nonneg_set(isl_pw_aff_copy(pa1
));
3593 f
= isl_pw_aff_floor(isl_pw_aff_copy(pa1
));
3594 c
= isl_pw_aff_ceil(pa1
);
3595 return isl_pw_aff_cond(isl_set_indicator_function(cond
), f
, c
);
3597 isl_pw_aff_free(pa1
);
3598 isl_pw_aff_free(pa2
);
3602 /* Compute the remainder of the integer division of "pa1" by "pa2"
3603 * with rounding towards zero.
3604 * "pa2" is assumed to be a piecewise constant.
3606 * In particular, return
3608 * pa1 - pa2 * (pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2))
3611 __isl_give isl_pw_aff
*isl_pw_aff_tdiv_r(__isl_take isl_pw_aff
*pa1
,
3612 __isl_take isl_pw_aff
*pa2
)
3617 is_cst
= isl_pw_aff_is_cst(pa2
);
3621 isl_die(isl_pw_aff_get_ctx(pa2
), isl_error_invalid
,
3622 "second argument should be a piecewise constant",
3624 res
= isl_pw_aff_tdiv_q(isl_pw_aff_copy(pa1
), isl_pw_aff_copy(pa2
));
3625 res
= isl_pw_aff_mul(pa2
, res
);
3626 res
= isl_pw_aff_sub(pa1
, res
);
3629 isl_pw_aff_free(pa1
);
3630 isl_pw_aff_free(pa2
);
3634 /* Does either of "pa1" or "pa2" involve any NaN2?
3636 static isl_bool
either_involves_nan(__isl_keep isl_pw_aff
*pa1
,
3637 __isl_keep isl_pw_aff
*pa2
)
3641 has_nan
= isl_pw_aff_involves_nan(pa1
);
3642 if (has_nan
< 0 || has_nan
)
3644 return isl_pw_aff_involves_nan(pa2
);
3647 /* Replace "pa1" and "pa2" (at least one of which involves a NaN)
3648 * by a NaN on their shared domain.
3650 * In principle, the result could be refined to only being NaN
3651 * on the parts of this domain where at least one of "pa1" or "pa2" is NaN.
3653 static __isl_give isl_pw_aff
*replace_by_nan(__isl_take isl_pw_aff
*pa1
,
3654 __isl_take isl_pw_aff
*pa2
)
3656 isl_local_space
*ls
;
3660 dom
= isl_set_intersect(isl_pw_aff_domain(pa1
), isl_pw_aff_domain(pa2
));
3661 ls
= isl_local_space_from_space(isl_set_get_space(dom
));
3662 pa
= isl_pw_aff_nan_on_domain(ls
);
3663 pa
= isl_pw_aff_intersect_domain(pa
, dom
);
3668 static __isl_give isl_pw_aff
*pw_aff_min(__isl_take isl_pw_aff
*pwaff1
,
3669 __isl_take isl_pw_aff
*pwaff2
)
3674 dom
= isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1
)),
3675 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2
)));
3676 le
= isl_pw_aff_le_set(isl_pw_aff_copy(pwaff1
),
3677 isl_pw_aff_copy(pwaff2
));
3678 dom
= isl_set_subtract(dom
, isl_set_copy(le
));
3679 return isl_pw_aff_select(le
, pwaff1
, dom
, pwaff2
);
3682 static __isl_give isl_pw_aff
*pw_aff_max(__isl_take isl_pw_aff
*pwaff1
,
3683 __isl_take isl_pw_aff
*pwaff2
)
3688 dom
= isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1
)),
3689 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2
)));
3690 ge
= isl_pw_aff_ge_set(isl_pw_aff_copy(pwaff1
),
3691 isl_pw_aff_copy(pwaff2
));
3692 dom
= isl_set_subtract(dom
, isl_set_copy(ge
));
3693 return isl_pw_aff_select(ge
, pwaff1
, dom
, pwaff2
);
3696 /* Return an expression for the minimum (if "max" is not set) or
3697 * the maximum (if "max" is set) of "pa1" and "pa2".
3698 * If either expression involves any NaN, then return a NaN
3699 * on the shared domain as result.
3701 static __isl_give isl_pw_aff
*pw_aff_min_max(__isl_take isl_pw_aff
*pa1
,
3702 __isl_take isl_pw_aff
*pa2
, int max
)
3706 has_nan
= either_involves_nan(pa1
, pa2
);
3708 pa1
= isl_pw_aff_free(pa1
);
3710 return replace_by_nan(pa1
, pa2
);
3712 isl_pw_aff_align_params_bin(&pa1
, &pa2
);
3714 return pw_aff_max(pa1
, pa2
);
3716 return pw_aff_min(pa1
, pa2
);
3719 /* Return an expression for the minimum of "pwaff1" and "pwaff2".
3721 __isl_give isl_pw_aff
*isl_pw_aff_min(__isl_take isl_pw_aff
*pwaff1
,
3722 __isl_take isl_pw_aff
*pwaff2
)
3724 return pw_aff_min_max(pwaff1
, pwaff2
, 0);
3727 /* Return an expression for the maximum of "pwaff1" and "pwaff2".
3729 __isl_give isl_pw_aff
*isl_pw_aff_max(__isl_take isl_pw_aff
*pwaff1
,
3730 __isl_take isl_pw_aff
*pwaff2
)
3732 return pw_aff_min_max(pwaff1
, pwaff2
, 1);
3735 static __isl_give isl_pw_aff
*pw_aff_list_reduce(
3736 __isl_take isl_pw_aff_list
*list
,
3737 __isl_give isl_pw_aff
*(*fn
)(__isl_take isl_pw_aff
*pwaff1
,
3738 __isl_take isl_pw_aff
*pwaff2
))
3747 ctx
= isl_pw_aff_list_get_ctx(list
);
3749 isl_die(ctx
, isl_error_invalid
,
3750 "list should contain at least one element", goto error
);
3752 res
= isl_pw_aff_copy(list
->p
[0]);
3753 for (i
= 1; i
< list
->n
; ++i
)
3754 res
= fn(res
, isl_pw_aff_copy(list
->p
[i
]));
3756 isl_pw_aff_list_free(list
);
3759 isl_pw_aff_list_free(list
);
3763 /* Return an isl_pw_aff that maps each element in the intersection of the
3764 * domains of the elements of list to the minimal corresponding affine
3767 __isl_give isl_pw_aff
*isl_pw_aff_list_min(__isl_take isl_pw_aff_list
*list
)
3769 return pw_aff_list_reduce(list
, &isl_pw_aff_min
);
3772 /* Return an isl_pw_aff that maps each element in the intersection of the
3773 * domains of the elements of list to the maximal corresponding affine
3776 __isl_give isl_pw_aff
*isl_pw_aff_list_max(__isl_take isl_pw_aff_list
*list
)
3778 return pw_aff_list_reduce(list
, &isl_pw_aff_max
);
3781 /* Mark the domains of "pwaff" as rational.
3783 __isl_give isl_pw_aff
*isl_pw_aff_set_rational(__isl_take isl_pw_aff
*pwaff
)
3787 pwaff
= isl_pw_aff_cow(pwaff
);
3793 for (i
= 0; i
< pwaff
->n
; ++i
) {
3794 pwaff
->p
[i
].set
= isl_set_set_rational(pwaff
->p
[i
].set
);
3795 if (!pwaff
->p
[i
].set
)
3796 return isl_pw_aff_free(pwaff
);
3802 /* Mark the domains of the elements of "list" as rational.
3804 __isl_give isl_pw_aff_list
*isl_pw_aff_list_set_rational(
3805 __isl_take isl_pw_aff_list
*list
)
3815 for (i
= 0; i
< n
; ++i
) {
3818 pa
= isl_pw_aff_list_get_pw_aff(list
, i
);
3819 pa
= isl_pw_aff_set_rational(pa
);
3820 list
= isl_pw_aff_list_set_pw_aff(list
, i
, pa
);
3826 /* Do the parameters of "aff" match those of "space"?
3828 isl_bool
isl_aff_matching_params(__isl_keep isl_aff
*aff
,
3829 __isl_keep isl_space
*space
)
3831 isl_space
*aff_space
;
3835 return isl_bool_error
;
3837 aff_space
= isl_aff_get_domain_space(aff
);
3839 match
= isl_space_has_equal_params(space
, aff_space
);
3841 isl_space_free(aff_space
);
3845 /* Check that the domain space of "aff" matches "space".
3847 isl_stat
isl_aff_check_match_domain_space(__isl_keep isl_aff
*aff
,
3848 __isl_keep isl_space
*space
)
3850 isl_space
*aff_space
;
3854 return isl_stat_error
;
3856 aff_space
= isl_aff_get_domain_space(aff
);
3858 match
= isl_space_has_equal_params(space
, aff_space
);
3862 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
3863 "parameters don't match", goto error
);
3864 match
= isl_space_tuple_is_equal(space
, isl_dim_in
,
3865 aff_space
, isl_dim_set
);
3869 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
3870 "domains don't match", goto error
);
3871 isl_space_free(aff_space
);
3874 isl_space_free(aff_space
);
3875 return isl_stat_error
;
3878 /* Return the shared (universe) domain of the elements of "ma".
3880 * Since an isl_multi_aff (and an isl_aff) is always total,
3881 * the domain is always the universe set in its domain space.
3882 * This is a helper function for use in the generic isl_multi_*_bind.
3884 static __isl_give isl_basic_set
*isl_multi_aff_domain(
3885 __isl_take isl_multi_aff
*ma
)
3889 space
= isl_multi_aff_get_space(ma
);
3890 isl_multi_aff_free(ma
);
3892 return isl_basic_set_universe(isl_space_domain(space
));
3898 #include <isl_multi_no_explicit_domain.c>
3899 #include <isl_multi_templ.c>
3900 #include <isl_multi_un_op_templ.c>
3901 #include <isl_multi_bin_val_templ.c>
3902 #include <isl_multi_add_constant_templ.c>
3903 #include <isl_multi_apply_set.c>
3904 #include <isl_multi_arith_templ.c>
3905 #include <isl_multi_bind_domain_templ.c>
3906 #include <isl_multi_cmp.c>
3907 #include <isl_multi_dim_id_templ.c>
3908 #include <isl_multi_dims.c>
3909 #include <isl_multi_floor.c>
3910 #include <isl_multi_from_base_templ.c>
3911 #include <isl_multi_identity_templ.c>
3912 #include <isl_multi_insert_domain_templ.c>
3913 #include <isl_multi_locals_templ.c>
3914 #include <isl_multi_move_dims_templ.c>
3915 #include <isl_multi_nan_templ.c>
3916 #include <isl_multi_product_templ.c>
3917 #include <isl_multi_splice_templ.c>
3918 #include <isl_multi_tuple_id_templ.c>
3919 #include <isl_multi_unbind_params_templ.c>
3920 #include <isl_multi_zero_templ.c>
3924 #include <isl_multi_gist.c>
3927 #define DOMBASE basic_set
3928 #include <isl_multi_bind_templ.c>
3930 /* Construct an isl_multi_aff living in "space" that corresponds
3931 * to the affine transformation matrix "mat".
3933 __isl_give isl_multi_aff
*isl_multi_aff_from_aff_mat(
3934 __isl_take isl_space
*space
, __isl_take isl_mat
*mat
)
3937 isl_local_space
*ls
= NULL
;
3938 isl_multi_aff
*ma
= NULL
;
3939 isl_size n_row
, n_col
, n_out
, total
;
3945 ctx
= isl_mat_get_ctx(mat
);
3947 n_row
= isl_mat_rows(mat
);
3948 n_col
= isl_mat_cols(mat
);
3949 n_out
= isl_space_dim(space
, isl_dim_out
);
3950 total
= isl_space_dim(space
, isl_dim_all
);
3951 if (n_row
< 0 || n_col
< 0 || n_out
< 0 || total
< 0)
3954 isl_die(ctx
, isl_error_invalid
,
3955 "insufficient number of rows", goto error
);
3957 isl_die(ctx
, isl_error_invalid
,
3958 "insufficient number of columns", goto error
);
3959 if (1 + n_out
!= n_row
|| 2 + total
!= n_row
+ n_col
)
3960 isl_die(ctx
, isl_error_invalid
,
3961 "dimension mismatch", goto error
);
3963 ma
= isl_multi_aff_zero(isl_space_copy(space
));
3964 space
= isl_space_domain(space
);
3965 ls
= isl_local_space_from_space(isl_space_copy(space
));
3967 for (i
= 0; i
< n_row
- 1; ++i
) {
3971 v
= isl_vec_alloc(ctx
, 1 + n_col
);
3974 isl_int_set(v
->el
[0], mat
->row
[0][0]);
3975 isl_seq_cpy(v
->el
+ 1, mat
->row
[1 + i
], n_col
);
3976 v
= isl_vec_normalize(v
);
3977 aff
= isl_aff_alloc_vec_validated(isl_local_space_copy(ls
), v
);
3978 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
3981 isl_space_free(space
);
3982 isl_local_space_free(ls
);
3986 isl_space_free(space
);
3987 isl_local_space_free(ls
);
3989 isl_multi_aff_free(ma
);
3993 /* Return the constant terms of the affine expressions of "ma".
3995 __isl_give isl_multi_val
*isl_multi_aff_get_constant_multi_val(
3996 __isl_keep isl_multi_aff
*ma
)
4003 n
= isl_multi_aff_size(ma
);
4006 space
= isl_space_range(isl_multi_aff_get_space(ma
));
4007 space
= isl_space_drop_all_params(space
);
4008 mv
= isl_multi_val_zero(space
);
4010 for (i
= 0; i
< n
; ++i
) {
4014 aff
= isl_multi_aff_get_at(ma
, i
);
4015 val
= isl_aff_get_constant_val(aff
);
4017 mv
= isl_multi_val_set_at(mv
, i
, val
);
4023 /* Remove any internal structure of the domain of "ma".
4024 * If there is any such internal structure in the input,
4025 * then the name of the corresponding space is also removed.
4027 __isl_give isl_multi_aff
*isl_multi_aff_flatten_domain(
4028 __isl_take isl_multi_aff
*ma
)
4035 if (!ma
->space
->nested
[0])
4038 space
= isl_multi_aff_get_space(ma
);
4039 space
= isl_space_flatten_domain(space
);
4040 ma
= isl_multi_aff_reset_space(ma
, space
);
4045 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
4046 * of the space to its domain.
4048 __isl_give isl_multi_aff
*isl_multi_aff_domain_map(__isl_take isl_space
*space
)
4052 isl_local_space
*ls
;
4057 if (!isl_space_is_map(space
))
4058 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
4059 "not a map space", goto error
);
4061 n_in
= isl_space_dim(space
, isl_dim_in
);
4064 space
= isl_space_domain_map(space
);
4066 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
4068 isl_space_free(space
);
4072 space
= isl_space_domain(space
);
4073 ls
= isl_local_space_from_space(space
);
4074 for (i
= 0; i
< n_in
; ++i
) {
4077 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
4079 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
4081 isl_local_space_free(ls
);
4084 isl_space_free(space
);
4088 /* This function performs the same operation as isl_multi_aff_domain_map,
4089 * but is considered as a function on an isl_space when exported.
4091 __isl_give isl_multi_aff
*isl_space_domain_map_multi_aff(
4092 __isl_take isl_space
*space
)
4094 return isl_multi_aff_domain_map(space
);
4097 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
4098 * of the space to its range.
4100 __isl_give isl_multi_aff
*isl_multi_aff_range_map(__isl_take isl_space
*space
)
4103 isl_size n_in
, n_out
;
4104 isl_local_space
*ls
;
4109 if (!isl_space_is_map(space
))
4110 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
4111 "not a map space", goto error
);
4113 n_in
= isl_space_dim(space
, isl_dim_in
);
4114 n_out
= isl_space_dim(space
, isl_dim_out
);
4115 if (n_in
< 0 || n_out
< 0)
4117 space
= isl_space_range_map(space
);
4119 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
4121 isl_space_free(space
);
4125 space
= isl_space_domain(space
);
4126 ls
= isl_local_space_from_space(space
);
4127 for (i
= 0; i
< n_out
; ++i
) {
4130 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
4131 isl_dim_set
, n_in
+ i
);
4132 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
4134 isl_local_space_free(ls
);
4137 isl_space_free(space
);
4141 /* This function performs the same operation as isl_multi_aff_range_map,
4142 * but is considered as a function on an isl_space when exported.
4144 __isl_give isl_multi_aff
*isl_space_range_map_multi_aff(
4145 __isl_take isl_space
*space
)
4147 return isl_multi_aff_range_map(space
);
4150 /* Given a map space, return an isl_pw_multi_aff that maps a wrapped copy
4151 * of the space to its domain.
4153 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_domain_map(
4154 __isl_take isl_space
*space
)
4156 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_domain_map(space
));
4159 /* This function performs the same operation as isl_pw_multi_aff_domain_map,
4160 * but is considered as a function on an isl_space when exported.
4162 __isl_give isl_pw_multi_aff
*isl_space_domain_map_pw_multi_aff(
4163 __isl_take isl_space
*space
)
4165 return isl_pw_multi_aff_domain_map(space
);
4168 /* Given a map space, return an isl_pw_multi_aff that maps a wrapped copy
4169 * of the space to its range.
4171 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_range_map(
4172 __isl_take isl_space
*space
)
4174 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_range_map(space
));
4177 /* This function performs the same operation as isl_pw_multi_aff_range_map,
4178 * but is considered as a function on an isl_space when exported.
4180 __isl_give isl_pw_multi_aff
*isl_space_range_map_pw_multi_aff(
4181 __isl_take isl_space
*space
)
4183 return isl_pw_multi_aff_range_map(space
);
4186 /* Given the space of a set and a range of set dimensions,
4187 * construct an isl_multi_aff that projects out those dimensions.
4189 __isl_give isl_multi_aff
*isl_multi_aff_project_out_map(
4190 __isl_take isl_space
*space
, enum isl_dim_type type
,
4191 unsigned first
, unsigned n
)
4195 isl_local_space
*ls
;
4200 if (!isl_space_is_set(space
))
4201 isl_die(isl_space_get_ctx(space
), isl_error_unsupported
,
4202 "expecting set space", goto error
);
4203 if (type
!= isl_dim_set
)
4204 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
4205 "only set dimensions can be projected out", goto error
);
4206 if (isl_space_check_range(space
, type
, first
, n
) < 0)
4209 dim
= isl_space_dim(space
, isl_dim_set
);
4213 space
= isl_space_from_domain(space
);
4214 space
= isl_space_add_dims(space
, isl_dim_out
, dim
- n
);
4217 return isl_multi_aff_alloc(space
);
4219 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
4220 space
= isl_space_domain(space
);
4221 ls
= isl_local_space_from_space(space
);
4223 for (i
= 0; i
< first
; ++i
) {
4226 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
4228 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
4231 for (i
= 0; i
< dim
- (first
+ n
); ++i
) {
4234 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
4235 isl_dim_set
, first
+ n
+ i
);
4236 ma
= isl_multi_aff_set_aff(ma
, first
+ i
, aff
);
4239 isl_local_space_free(ls
);
4242 isl_space_free(space
);
4246 /* Given the space of a set and a range of set dimensions,
4247 * construct an isl_pw_multi_aff that projects out those dimensions.
4249 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_project_out_map(
4250 __isl_take isl_space
*space
, enum isl_dim_type type
,
4251 unsigned first
, unsigned n
)
4255 ma
= isl_multi_aff_project_out_map(space
, type
, first
, n
);
4256 return isl_pw_multi_aff_from_multi_aff(ma
);
4259 /* This function performs the same operation as isl_pw_multi_aff_from_multi_aff,
4260 * but is considered as a function on an isl_multi_aff when exported.
4262 __isl_give isl_pw_multi_aff
*isl_multi_aff_to_pw_multi_aff(
4263 __isl_take isl_multi_aff
*ma
)
4265 return isl_pw_multi_aff_from_multi_aff(ma
);
4268 /* Create a piecewise multi-affine expression in the given space that maps each
4269 * input dimension to the corresponding output dimension.
4271 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_identity(
4272 __isl_take isl_space
*space
)
4274 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_identity(space
));
4277 /* Create a piecewise multi expression that maps elements in the given space
4280 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_identity_on_domain_space(
4281 __isl_take isl_space
*space
)
4285 ma
= isl_multi_aff_identity_on_domain_space(space
);
4286 return isl_pw_multi_aff_from_multi_aff(ma
);
4289 /* This function performs the same operation as
4290 * isl_pw_multi_aff_identity_on_domain_space,
4291 * but is considered as a function on an isl_space when exported.
4293 __isl_give isl_pw_multi_aff
*isl_space_identity_pw_multi_aff_on_domain(
4294 __isl_take isl_space
*space
)
4296 return isl_pw_multi_aff_identity_on_domain_space(space
);
4299 /* Exploit the equalities in "eq" to simplify the affine expressions.
4301 static __isl_give isl_multi_aff
*isl_multi_aff_substitute_equalities(
4302 __isl_take isl_multi_aff
*maff
, __isl_take isl_basic_set
*eq
)
4307 n
= isl_multi_aff_size(maff
);
4311 for (i
= 0; i
< n
; ++i
) {
4314 aff
= isl_multi_aff_take_at(maff
, i
);
4315 aff
= isl_aff_substitute_equalities(aff
,
4316 isl_basic_set_copy(eq
));
4317 maff
= isl_multi_aff_restore_at(maff
, i
, aff
);
4320 isl_basic_set_free(eq
);
4323 isl_basic_set_free(eq
);
4324 isl_multi_aff_free(maff
);
4328 __isl_give isl_multi_aff
*isl_multi_aff_scale(__isl_take isl_multi_aff
*maff
,
4334 n
= isl_multi_aff_size(maff
);
4336 return isl_multi_aff_free(maff
);
4338 for (i
= 0; i
< n
; ++i
) {
4341 aff
= isl_multi_aff_take_at(maff
, i
);
4342 aff
= isl_aff_scale(aff
, f
);
4343 maff
= isl_multi_aff_restore_at(maff
, i
, aff
);
4349 __isl_give isl_multi_aff
*isl_multi_aff_add_on_domain(__isl_keep isl_set
*dom
,
4350 __isl_take isl_multi_aff
*maff1
, __isl_take isl_multi_aff
*maff2
)
4352 maff1
= isl_multi_aff_add(maff1
, maff2
);
4353 maff1
= isl_multi_aff_gist(maff1
, isl_set_copy(dom
));
4357 isl_bool
isl_multi_aff_is_empty(__isl_keep isl_multi_aff
*maff
)
4360 return isl_bool_error
;
4362 return isl_bool_false
;
4365 /* Return the set of domain elements where "ma1" is lexicographically
4366 * smaller than or equal to "ma2".
4368 __isl_give isl_set
*isl_multi_aff_lex_le_set(__isl_take isl_multi_aff
*ma1
,
4369 __isl_take isl_multi_aff
*ma2
)
4371 return isl_multi_aff_lex_ge_set(ma2
, ma1
);
4374 /* Return the set of domain elements where "ma1" is lexicographically
4375 * smaller than "ma2".
4377 __isl_give isl_set
*isl_multi_aff_lex_lt_set(__isl_take isl_multi_aff
*ma1
,
4378 __isl_take isl_multi_aff
*ma2
)
4380 return isl_multi_aff_lex_gt_set(ma2
, ma1
);
4383 /* Return the set of domain elements where "ma1" is lexicographically
4384 * greater than to "ma2". If "equal" is set, then include the domain
4385 * elements where they are equal.
4386 * Do this for the case where there are no entries.
4387 * In this case, "ma1" cannot be greater than "ma2",
4388 * but it is (greater than or) equal to "ma2".
4390 static __isl_give isl_set
*isl_multi_aff_lex_gte_set_0d(
4391 __isl_take isl_multi_aff
*ma1
, __isl_take isl_multi_aff
*ma2
, int equal
)
4395 space
= isl_multi_aff_get_domain_space(ma1
);
4397 isl_multi_aff_free(ma1
);
4398 isl_multi_aff_free(ma2
);
4401 return isl_set_universe(space
);
4403 return isl_set_empty(space
);
4406 /* Return the set where entry "i" of "ma1" and "ma2"
4407 * satisfy the relation prescribed by "cmp".
4409 static __isl_give isl_set
*isl_multi_aff_order_at(__isl_keep isl_multi_aff
*ma1
,
4410 __isl_keep isl_multi_aff
*ma2
, int i
,
4411 __isl_give isl_set
*(*cmp
)(__isl_take isl_aff
*aff1
,
4412 __isl_take isl_aff
*aff2
))
4414 isl_aff
*aff1
, *aff2
;
4416 aff1
= isl_multi_aff_get_at(ma1
, i
);
4417 aff2
= isl_multi_aff_get_at(ma2
, i
);
4418 return cmp(aff1
, aff2
);
4421 /* Return the set of domain elements where "ma1" is lexicographically
4422 * greater than to "ma2". If "equal" is set, then include the domain
4423 * elements where they are equal.
4425 * In particular, for all but the final entry,
4426 * include the set of elements where this entry is strictly greater in "ma1"
4427 * and all previous entries are equal.
4428 * The final entry is also allowed to be equal in the two functions
4429 * if "equal" is set.
4431 * The case where there are no entries is handled separately.
4433 static __isl_give isl_set
*isl_multi_aff_lex_gte_set(
4434 __isl_take isl_multi_aff
*ma1
, __isl_take isl_multi_aff
*ma2
, int equal
)
4443 if (isl_multi_aff_check_equal_space(ma1
, ma2
) < 0)
4445 n
= isl_multi_aff_size(ma1
);
4449 return isl_multi_aff_lex_gte_set_0d(ma1
, ma2
, equal
);
4451 space
= isl_multi_aff_get_domain_space(ma1
);
4452 res
= isl_set_empty(isl_space_copy(space
));
4453 equal_set
= isl_set_universe(space
);
4455 for (i
= 0; i
+ 1 < n
; ++i
) {
4459 gt
= isl_multi_aff_order_at(ma1
, ma2
, i
, &isl_aff_gt_set
);
4460 gt
= isl_set_intersect(gt
, isl_set_copy(equal_set
));
4461 res
= isl_set_union(res
, gt
);
4462 eq
= isl_multi_aff_order_at(ma1
, ma2
, i
, &isl_aff_eq_set
);
4463 equal_set
= isl_set_intersect(equal_set
, eq
);
4465 empty
= isl_set_is_empty(equal_set
);
4466 if (empty
>= 0 && empty
)
4471 gte
= isl_multi_aff_order_at(ma1
, ma2
, n
- 1, &isl_aff_ge_set
);
4473 gte
= isl_multi_aff_order_at(ma1
, ma2
, n
- 1, &isl_aff_gt_set
);
4474 isl_multi_aff_free(ma1
);
4475 isl_multi_aff_free(ma2
);
4477 gte
= isl_set_intersect(gte
, equal_set
);
4478 return isl_set_union(res
, gte
);
4480 isl_multi_aff_free(ma1
);
4481 isl_multi_aff_free(ma2
);
4485 /* Return the set of domain elements where "ma1" is lexicographically
4486 * greater than or equal to "ma2".
4488 __isl_give isl_set
*isl_multi_aff_lex_ge_set(__isl_take isl_multi_aff
*ma1
,
4489 __isl_take isl_multi_aff
*ma2
)
4491 return isl_multi_aff_lex_gte_set(ma1
, ma2
, 1);
4494 /* Return the set of domain elements where "ma1" is lexicographically
4495 * greater than "ma2".
4497 __isl_give isl_set
*isl_multi_aff_lex_gt_set(__isl_take isl_multi_aff
*ma1
,
4498 __isl_take isl_multi_aff
*ma2
)
4500 return isl_multi_aff_lex_gte_set(ma1
, ma2
, 0);
4503 #define isl_multi_aff_zero_in_space isl_multi_aff_zero
4506 #define PW isl_pw_multi_aff
4508 #define BASE multi_aff
4510 #define EL_IS_ZERO is_empty
4514 #define IS_ZERO is_empty
4517 #undef DEFAULT_IS_ZERO
4518 #define DEFAULT_IS_ZERO 0
4520 #include <isl_pw_templ.c>
4521 #include <isl_pw_un_op_templ.c>
4522 #include <isl_pw_add_constant_multi_val_templ.c>
4523 #include <isl_pw_add_constant_val_templ.c>
4524 #include <isl_pw_bind_domain_templ.c>
4525 #include <isl_pw_insert_dims_templ.c>
4526 #include <isl_pw_insert_domain_templ.c>
4527 #include <isl_pw_locals_templ.c>
4528 #include <isl_pw_move_dims_templ.c>
4529 #include <isl_pw_neg_templ.c>
4530 #include <isl_pw_pullback_templ.c>
4531 #include <isl_pw_range_tuple_id_templ.c>
4532 #include <isl_pw_union_opt.c>
4535 #define BASE pw_multi_aff
4537 #include <isl_union_multi.c>
4538 #include "isl_union_locals_templ.c"
4539 #include <isl_union_neg.c>
4542 #define BASE multi_aff
4544 #include <isl_union_pw_templ.c>
4546 /* Generic function for extracting a factor from a product "pma".
4547 * "check_space" checks that the space is that of the right kind of product.
4548 * "space_factor" extracts the factor from the space.
4549 * "multi_aff_factor" extracts the factor from the constituent functions.
4551 static __isl_give isl_pw_multi_aff
*pw_multi_aff_factor(
4552 __isl_take isl_pw_multi_aff
*pma
,
4553 isl_stat (*check_space
)(__isl_keep isl_pw_multi_aff
*pma
),
4554 __isl_give isl_space
*(*space_factor
)(__isl_take isl_space
*space
),
4555 __isl_give isl_multi_aff
*(*multi_aff_factor
)(
4556 __isl_take isl_multi_aff
*ma
))
4561 if (check_space(pma
) < 0)
4562 return isl_pw_multi_aff_free(pma
);
4564 space
= isl_pw_multi_aff_take_space(pma
);
4565 space
= space_factor(space
);
4567 for (i
= 0; pma
&& i
< pma
->n
; ++i
) {
4570 ma
= isl_pw_multi_aff_take_base_at(pma
, i
);
4571 ma
= multi_aff_factor(ma
);
4572 pma
= isl_pw_multi_aff_restore_base_at(pma
, i
, ma
);
4575 pma
= isl_pw_multi_aff_restore_space(pma
, space
);
4580 /* Is the range of "pma" a wrapped relation?
4582 static isl_bool
isl_pw_multi_aff_range_is_wrapping(
4583 __isl_keep isl_pw_multi_aff
*pma
)
4585 return isl_space_range_is_wrapping(isl_pw_multi_aff_peek_space(pma
));
4588 /* Check that the range of "pma" is a product.
4590 static isl_stat
pw_multi_aff_check_range_product(
4591 __isl_keep isl_pw_multi_aff
*pma
)
4595 wraps
= isl_pw_multi_aff_range_is_wrapping(pma
);
4597 return isl_stat_error
;
4599 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
4600 "range is not a product", return isl_stat_error
);
4604 /* Given a function A -> [B -> C], extract the function A -> B.
4606 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_range_factor_domain(
4607 __isl_take isl_pw_multi_aff
*pma
)
4609 return pw_multi_aff_factor(pma
, &pw_multi_aff_check_range_product
,
4610 &isl_space_range_factor_domain
,
4611 &isl_multi_aff_range_factor_domain
);
4614 /* Given a function A -> [B -> C], extract the function A -> C.
4616 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_range_factor_range(
4617 __isl_take isl_pw_multi_aff
*pma
)
4619 return pw_multi_aff_factor(pma
, &pw_multi_aff_check_range_product
,
4620 &isl_space_range_factor_range
,
4621 &isl_multi_aff_range_factor_range
);
4624 /* Given two piecewise multi affine expressions, return a piecewise
4625 * multi-affine expression defined on the union of the definition domains
4626 * of the inputs that is equal to the lexicographic maximum of the two
4627 * inputs on each cell. If only one of the two inputs is defined on
4628 * a given cell, then it is considered to be the maximum.
4630 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_union_lexmax(
4631 __isl_take isl_pw_multi_aff
*pma1
,
4632 __isl_take isl_pw_multi_aff
*pma2
)
4634 isl_pw_multi_aff_align_params_bin(&pma1
, &pma2
);
4635 return isl_pw_multi_aff_union_opt_cmp(pma1
, pma2
,
4636 &isl_multi_aff_lex_ge_set
);
4639 /* Given two piecewise multi affine expressions, return a piecewise
4640 * multi-affine expression defined on the union of the definition domains
4641 * of the inputs that is equal to the lexicographic minimum of the two
4642 * inputs on each cell. If only one of the two inputs is defined on
4643 * a given cell, then it is considered to be the minimum.
4645 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_union_lexmin(
4646 __isl_take isl_pw_multi_aff
*pma1
,
4647 __isl_take isl_pw_multi_aff
*pma2
)
4649 isl_pw_multi_aff_align_params_bin(&pma1
, &pma2
);
4650 return isl_pw_multi_aff_union_opt_cmp(pma1
, pma2
,
4651 &isl_multi_aff_lex_le_set
);
4654 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_add(
4655 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4657 isl_pw_multi_aff_align_params_bin(&pma1
, &pma2
);
4658 return isl_pw_multi_aff_on_shared_domain(pma1
, pma2
,
4659 &isl_multi_aff_add
);
4662 /* Subtract "pma2" from "pma1" and return the result.
4664 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_sub(
4665 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4667 isl_pw_multi_aff_align_params_bin(&pma1
, &pma2
);
4668 return isl_pw_multi_aff_on_shared_domain(pma1
, pma2
,
4669 &isl_multi_aff_sub
);
4672 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_union_add(
4673 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4675 return isl_pw_multi_aff_union_add_(pma1
, pma2
);
4678 /* Compute the sum of "upa1" and "upa2" on the union of their domains,
4679 * with the actual sum on the shared domain and
4680 * the defined expression on the symmetric difference of the domains.
4682 __isl_give isl_union_pw_aff
*isl_union_pw_aff_union_add(
4683 __isl_take isl_union_pw_aff
*upa1
, __isl_take isl_union_pw_aff
*upa2
)
4685 return isl_union_pw_aff_union_add_(upa1
, upa2
);
4688 /* Compute the sum of "upma1" and "upma2" on the union of their domains,
4689 * with the actual sum on the shared domain and
4690 * the defined expression on the symmetric difference of the domains.
4692 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_union_add(
4693 __isl_take isl_union_pw_multi_aff
*upma1
,
4694 __isl_take isl_union_pw_multi_aff
*upma2
)
4696 return isl_union_pw_multi_aff_union_add_(upma1
, upma2
);
4699 /* Given two piecewise multi-affine expressions A -> B and C -> D,
4700 * construct a piecewise multi-affine expression [A -> C] -> [B -> D].
4702 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_product(
4703 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4707 isl_pw_multi_aff
*res
;
4709 if (isl_pw_multi_aff_align_params_bin(&pma1
, &pma2
) < 0)
4712 n
= pma1
->n
* pma2
->n
;
4713 space
= isl_space_product(isl_space_copy(pma1
->dim
),
4714 isl_space_copy(pma2
->dim
));
4715 res
= isl_pw_multi_aff_alloc_size(space
, n
);
4717 for (i
= 0; i
< pma1
->n
; ++i
) {
4718 for (j
= 0; j
< pma2
->n
; ++j
) {
4722 domain
= isl_set_product(isl_set_copy(pma1
->p
[i
].set
),
4723 isl_set_copy(pma2
->p
[j
].set
));
4724 ma
= isl_multi_aff_product(
4725 isl_multi_aff_copy(pma1
->p
[i
].maff
),
4726 isl_multi_aff_copy(pma2
->p
[j
].maff
));
4727 res
= isl_pw_multi_aff_add_piece(res
, domain
, ma
);
4731 isl_pw_multi_aff_free(pma1
);
4732 isl_pw_multi_aff_free(pma2
);
4735 isl_pw_multi_aff_free(pma1
);
4736 isl_pw_multi_aff_free(pma2
);
4740 /* Subtract the initial "n" elements in "ma" with coefficients in "c" and
4741 * denominator "denom".
4742 * "denom" is allowed to be negative, in which case the actual denominator
4743 * is -denom and the expressions are added instead.
4745 static __isl_give isl_aff
*subtract_initial(__isl_take isl_aff
*aff
,
4746 __isl_keep isl_multi_aff
*ma
, int n
, isl_int
*c
, isl_int denom
)
4752 first
= isl_seq_first_non_zero(c
, n
);
4756 sign
= isl_int_sgn(denom
);
4758 isl_int_abs(d
, denom
);
4759 for (i
= first
; i
< n
; ++i
) {
4762 if (isl_int_is_zero(c
[i
]))
4764 aff_i
= isl_multi_aff_get_aff(ma
, i
);
4765 aff_i
= isl_aff_scale(aff_i
, c
[i
]);
4766 aff_i
= isl_aff_scale_down(aff_i
, d
);
4768 aff
= isl_aff_sub(aff
, aff_i
);
4770 aff
= isl_aff_add(aff
, aff_i
);
4777 /* Extract an affine expression that expresses the output dimension "pos"
4778 * of "bmap" in terms of the parameters and input dimensions from
4780 * Note that this expression may involve integer divisions defined
4781 * in terms of parameters and input dimensions.
4782 * The equality may also involve references to earlier (but not later)
4783 * output dimensions. These are replaced by the corresponding elements
4786 * If the equality is of the form
4788 * f(i) + h(j) + a x + g(i) = 0,
4790 * with f(i) a linear combinations of the parameters and input dimensions,
4791 * g(i) a linear combination of integer divisions defined in terms of the same
4792 * and h(j) a linear combinations of earlier output dimensions,
4793 * then the affine expression is
4795 * (-f(i) - g(i))/a - h(j)/a
4797 * If the equality is of the form
4799 * f(i) + h(j) - a x + g(i) = 0,
4801 * then the affine expression is
4803 * (f(i) + g(i))/a - h(j)/(-a)
4806 * If "div" refers to an integer division (i.e., it is smaller than
4807 * the number of integer divisions), then the equality constraint
4808 * does involve an integer division (the one at position "div") that
4809 * is defined in terms of output dimensions. However, this integer
4810 * division can be eliminated by exploiting a pair of constraints
4811 * x >= l and x <= l + n, with n smaller than the coefficient of "div"
4812 * in the equality constraint. "ineq" refers to inequality x >= l, i.e.,
4814 * In particular, let
4816 * x = e(i) + m floor(...)
4818 * with e(i) the expression derived above and floor(...) the integer
4819 * division involving output dimensions.
4830 * e(i) + m floor(...) - l = (e(i) + m floor(...) - l) mod m
4831 * = (e(i) - l) mod m
4835 * x - l = (e(i) - l) mod m
4839 * x = ((e(i) - l) mod m) + l
4841 * The variable "shift" below contains the expression -l, which may
4842 * also involve a linear combination of earlier output dimensions.
4844 static __isl_give isl_aff
*extract_aff_from_equality(
4845 __isl_keep isl_basic_map
*bmap
, int pos
, int eq
, int div
, int ineq
,
4846 __isl_keep isl_multi_aff
*ma
)
4849 isl_size n_div
, n_out
;
4851 isl_local_space
*ls
;
4852 isl_aff
*aff
, *shift
;
4855 ctx
= isl_basic_map_get_ctx(bmap
);
4856 ls
= isl_basic_map_get_local_space(bmap
);
4857 ls
= isl_local_space_domain(ls
);
4858 aff
= isl_aff_alloc(isl_local_space_copy(ls
));
4861 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
4862 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4863 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4864 if (n_out
< 0 || n_div
< 0)
4866 if (isl_int_is_neg(bmap
->eq
[eq
][o_out
+ pos
])) {
4867 isl_seq_cpy(aff
->v
->el
+ 1, bmap
->eq
[eq
], o_out
);
4868 isl_seq_cpy(aff
->v
->el
+ 1 + o_out
,
4869 bmap
->eq
[eq
] + o_out
+ n_out
, n_div
);
4871 isl_seq_neg(aff
->v
->el
+ 1, bmap
->eq
[eq
], o_out
);
4872 isl_seq_neg(aff
->v
->el
+ 1 + o_out
,
4873 bmap
->eq
[eq
] + o_out
+ n_out
, n_div
);
4876 isl_int_set_si(aff
->v
->el
[1 + o_out
+ div
], 0);
4877 isl_int_abs(aff
->v
->el
[0], bmap
->eq
[eq
][o_out
+ pos
]);
4878 aff
= subtract_initial(aff
, ma
, pos
, bmap
->eq
[eq
] + o_out
,
4879 bmap
->eq
[eq
][o_out
+ pos
]);
4881 shift
= isl_aff_alloc(isl_local_space_copy(ls
));
4884 isl_seq_cpy(shift
->v
->el
+ 1, bmap
->ineq
[ineq
], o_out
);
4885 isl_seq_cpy(shift
->v
->el
+ 1 + o_out
,
4886 bmap
->ineq
[ineq
] + o_out
+ n_out
, n_div
);
4887 isl_int_set_si(shift
->v
->el
[0], 1);
4888 shift
= subtract_initial(shift
, ma
, pos
,
4889 bmap
->ineq
[ineq
] + o_out
, ctx
->negone
);
4890 aff
= isl_aff_add(aff
, isl_aff_copy(shift
));
4891 mod
= isl_val_int_from_isl_int(ctx
,
4892 bmap
->eq
[eq
][o_out
+ n_out
+ div
]);
4893 mod
= isl_val_abs(mod
);
4894 aff
= isl_aff_mod_val(aff
, mod
);
4895 aff
= isl_aff_sub(aff
, shift
);
4898 isl_local_space_free(ls
);
4901 isl_local_space_free(ls
);
4906 /* Given a basic map with output dimensions defined
4907 * in terms of the parameters input dimensions and earlier
4908 * output dimensions using an equality (and possibly a pair on inequalities),
4909 * extract an isl_aff that expresses output dimension "pos" in terms
4910 * of the parameters and input dimensions.
4911 * Note that this expression may involve integer divisions defined
4912 * in terms of parameters and input dimensions.
4913 * "ma" contains the expressions corresponding to earlier output dimensions.
4915 * This function shares some similarities with
4916 * isl_basic_map_has_defining_equality and isl_constraint_get_bound.
4918 static __isl_give isl_aff
*extract_isl_aff_from_basic_map(
4919 __isl_keep isl_basic_map
*bmap
, int pos
, __isl_keep isl_multi_aff
*ma
)
4926 eq
= isl_basic_map_output_defining_equality(bmap
, pos
, &div
, &ineq
);
4927 if (eq
>= bmap
->n_eq
)
4928 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
4929 "unable to find suitable equality", return NULL
);
4930 aff
= extract_aff_from_equality(bmap
, pos
, eq
, div
, ineq
, ma
);
4932 aff
= isl_aff_remove_unused_divs(aff
);
4936 /* Given a basic map where each output dimension is defined
4937 * in terms of the parameters and input dimensions using an equality,
4938 * extract an isl_multi_aff that expresses the output dimensions in terms
4939 * of the parameters and input dimensions.
4941 static __isl_give isl_multi_aff
*extract_isl_multi_aff_from_basic_map(
4942 __isl_take isl_basic_map
*bmap
)
4951 ma
= isl_multi_aff_alloc(isl_basic_map_get_space(bmap
));
4952 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4954 ma
= isl_multi_aff_free(ma
);
4956 for (i
= 0; i
< n_out
; ++i
) {
4959 aff
= extract_isl_aff_from_basic_map(bmap
, i
, ma
);
4960 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
4963 isl_basic_map_free(bmap
);
4968 /* Given a basic set where each set dimension is defined
4969 * in terms of the parameters using an equality,
4970 * extract an isl_multi_aff that expresses the set dimensions in terms
4971 * of the parameters.
4973 __isl_give isl_multi_aff
*isl_multi_aff_from_basic_set_equalities(
4974 __isl_take isl_basic_set
*bset
)
4976 return extract_isl_multi_aff_from_basic_map(bset
);
4979 /* Create an isl_pw_multi_aff that is equivalent to
4980 * isl_map_intersect_domain(isl_map_from_basic_map(bmap), domain).
4981 * The given basic map is such that each output dimension is defined
4982 * in terms of the parameters and input dimensions using an equality.
4984 * Since some applications expect the result of isl_pw_multi_aff_from_map
4985 * to only contain integer affine expressions, we compute the floor
4986 * of the expression before returning.
4988 * Remove all constraints involving local variables without
4989 * an explicit representation (resulting in the removal of those
4990 * local variables) prior to the actual extraction to ensure
4991 * that the local spaces in which the resulting affine expressions
4992 * are created do not contain any unknown local variables.
4993 * Removing such constraints is safe because constraints involving
4994 * unknown local variables are not used to determine whether
4995 * a basic map is obviously single-valued.
4997 static __isl_give isl_pw_multi_aff
*plain_pw_multi_aff_from_map(
4998 __isl_take isl_set
*domain
, __isl_take isl_basic_map
*bmap
)
5002 bmap
= isl_basic_map_drop_constraints_involving_unknown_divs(bmap
);
5003 ma
= extract_isl_multi_aff_from_basic_map(bmap
);
5004 ma
= isl_multi_aff_floor(ma
);
5005 return isl_pw_multi_aff_alloc(domain
, ma
);
5008 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5009 * This obviously only works if the input "map" is single-valued.
5010 * If so, we compute the lexicographic minimum of the image in the form
5011 * of an isl_pw_multi_aff. Since the image is unique, it is equal
5012 * to its lexicographic minimum.
5013 * If the input is not single-valued, we produce an error.
5015 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_base(
5016 __isl_take isl_map
*map
)
5020 isl_pw_multi_aff
*pma
;
5022 sv
= isl_map_is_single_valued(map
);
5026 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
5027 "map is not single-valued", goto error
);
5028 map
= isl_map_make_disjoint(map
);
5032 pma
= isl_pw_multi_aff_empty(isl_map_get_space(map
));
5034 for (i
= 0; i
< map
->n
; ++i
) {
5035 isl_pw_multi_aff
*pma_i
;
5036 isl_basic_map
*bmap
;
5037 bmap
= isl_basic_map_copy(map
->p
[i
]);
5038 pma_i
= isl_basic_map_lexmin_pw_multi_aff(bmap
);
5039 pma
= isl_pw_multi_aff_add_disjoint(pma
, pma_i
);
5049 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
5050 * taking into account that the output dimension at position "d"
5051 * can be represented as
5053 * x = floor((e(...) + c1) / m)
5055 * given that constraint "i" is of the form
5057 * e(...) + c1 - m x >= 0
5060 * Let "map" be of the form
5064 * We construct a mapping
5066 * A -> [A -> x = floor(...)]
5068 * apply that to the map, obtaining
5070 * [A -> x = floor(...)] -> B
5072 * and equate dimension "d" to x.
5073 * We then compute a isl_pw_multi_aff representation of the resulting map
5074 * and plug in the mapping above.
5076 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_div(
5077 __isl_take isl_map
*map
, __isl_take isl_basic_map
*hull
, int d
, int i
)
5080 isl_space
*space
= NULL
;
5081 isl_local_space
*ls
;
5089 isl_pw_multi_aff
*pma
;
5092 is_set
= isl_map_is_set(map
);
5096 offset
= isl_basic_map_offset(hull
, isl_dim_out
);
5097 ctx
= isl_map_get_ctx(map
);
5098 space
= isl_space_domain(isl_map_get_space(map
));
5099 n_in
= isl_space_dim(space
, isl_dim_set
);
5100 n
= isl_space_dim(space
, isl_dim_all
);
5101 if (n_in
< 0 || n
< 0)
5104 v
= isl_vec_alloc(ctx
, 1 + 1 + n
);
5106 isl_int_neg(v
->el
[0], hull
->ineq
[i
][offset
+ d
]);
5107 isl_seq_cpy(v
->el
+ 1, hull
->ineq
[i
], 1 + n
);
5109 isl_basic_map_free(hull
);
5111 ls
= isl_local_space_from_space(isl_space_copy(space
));
5112 aff
= isl_aff_alloc_vec_validated(ls
, v
);
5113 aff
= isl_aff_floor(aff
);
5115 isl_space_free(space
);
5116 ma
= isl_multi_aff_from_aff(aff
);
5118 ma
= isl_multi_aff_identity(isl_space_map_from_set(space
));
5119 ma
= isl_multi_aff_range_product(ma
,
5120 isl_multi_aff_from_aff(aff
));
5123 insert
= isl_map_from_multi_aff_internal(isl_multi_aff_copy(ma
));
5124 map
= isl_map_apply_domain(map
, insert
);
5125 map
= isl_map_equate(map
, isl_dim_in
, n_in
, isl_dim_out
, d
);
5126 pma
= isl_pw_multi_aff_from_map(map
);
5127 pma
= isl_pw_multi_aff_pullback_multi_aff(pma
, ma
);
5131 isl_space_free(space
);
5133 isl_basic_map_free(hull
);
5137 /* Is constraint "c" of the form
5139 * e(...) + c1 - m x >= 0
5143 * -e(...) + c2 + m x >= 0
5145 * where m > 1 and e only depends on parameters and input dimensions?
5147 * "offset" is the offset of the output dimensions
5148 * "pos" is the position of output dimension x.
5150 static int is_potential_div_constraint(isl_int
*c
, int offset
, int d
, int total
)
5152 if (isl_int_is_zero(c
[offset
+ d
]))
5154 if (isl_int_is_one(c
[offset
+ d
]))
5156 if (isl_int_is_negone(c
[offset
+ d
]))
5158 if (isl_seq_first_non_zero(c
+ offset
, d
) != -1)
5160 if (isl_seq_first_non_zero(c
+ offset
+ d
+ 1,
5161 total
- (offset
+ d
+ 1)) != -1)
5166 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5168 * As a special case, we first check if there is any pair of constraints,
5169 * shared by all the basic maps in "map" that force a given dimension
5170 * to be equal to the floor of some affine combination of the input dimensions.
5172 * In particular, if we can find two constraints
5174 * e(...) + c1 - m x >= 0 i.e., m x <= e(...) + c1
5178 * -e(...) + c2 + m x >= 0 i.e., m x >= e(...) - c2
5180 * where m > 1 and e only depends on parameters and input dimensions,
5183 * c1 + c2 < m i.e., -c2 >= c1 - (m - 1)
5185 * then we know that we can take
5187 * x = floor((e(...) + c1) / m)
5189 * without having to perform any computation.
5191 * Note that we know that
5195 * If c1 + c2 were 0, then we would have detected an equality during
5196 * simplification. If c1 + c2 were negative, then we would have detected
5199 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_check_div(
5200 __isl_take isl_map
*map
)
5208 isl_basic_map
*hull
;
5210 hull
= isl_map_unshifted_simple_hull(isl_map_copy(map
));
5211 dim
= isl_map_dim(map
, isl_dim_out
);
5212 total
= isl_basic_map_dim(hull
, isl_dim_all
);
5213 if (dim
< 0 || total
< 0)
5217 offset
= isl_basic_map_offset(hull
, isl_dim_out
);
5219 for (d
= 0; d
< dim
; ++d
) {
5220 for (i
= 0; i
< n
; ++i
) {
5221 if (!is_potential_div_constraint(hull
->ineq
[i
],
5222 offset
, d
, 1 + total
))
5224 for (j
= i
+ 1; j
< n
; ++j
) {
5225 if (!isl_seq_is_neg(hull
->ineq
[i
] + 1,
5226 hull
->ineq
[j
] + 1, total
))
5228 isl_int_add(sum
, hull
->ineq
[i
][0],
5230 if (isl_int_abs_lt(sum
,
5231 hull
->ineq
[i
][offset
+ d
]))
5238 if (isl_int_is_pos(hull
->ineq
[j
][offset
+ d
]))
5240 return pw_multi_aff_from_map_div(map
, hull
, d
, j
);
5244 isl_basic_map_free(hull
);
5245 return pw_multi_aff_from_map_base(map
);
5248 isl_basic_map_free(hull
);
5252 /* Given an affine expression
5254 * [A -> B] -> f(A,B)
5256 * construct an isl_multi_aff
5260 * such that dimension "d" in B' is set to "aff" and the remaining
5261 * dimensions are set equal to the corresponding dimensions in B.
5262 * "n_in" is the dimension of the space A.
5263 * "n_out" is the dimension of the space B.
5265 * If "is_set" is set, then the affine expression is of the form
5269 * and we construct an isl_multi_aff
5273 static __isl_give isl_multi_aff
*range_map(__isl_take isl_aff
*aff
, int d
,
5274 unsigned n_in
, unsigned n_out
, int is_set
)
5278 isl_space
*space
, *space2
;
5279 isl_local_space
*ls
;
5281 space
= isl_aff_get_domain_space(aff
);
5282 ls
= isl_local_space_from_space(isl_space_copy(space
));
5283 space2
= isl_space_copy(space
);
5285 space2
= isl_space_range(isl_space_unwrap(space2
));
5286 space
= isl_space_map_from_domain_and_range(space
, space2
);
5287 ma
= isl_multi_aff_alloc(space
);
5288 ma
= isl_multi_aff_set_aff(ma
, d
, aff
);
5290 for (i
= 0; i
< n_out
; ++i
) {
5293 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
5294 isl_dim_set
, n_in
+ i
);
5295 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
5298 isl_local_space_free(ls
);
5303 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
5304 * taking into account that the dimension at position "d" can be written as
5306 * x = m a + f(..) (1)
5308 * where m is equal to "gcd".
5309 * "i" is the index of the equality in "hull" that defines f(..).
5310 * In particular, the equality is of the form
5312 * f(..) - x + m g(existentials) = 0
5316 * -f(..) + x + m g(existentials) = 0
5318 * We basically plug (1) into "map", resulting in a map with "a"
5319 * in the range instead of "x". The corresponding isl_pw_multi_aff
5320 * defining "a" is then plugged back into (1) to obtain a definition for "x".
5322 * Specifically, given the input map
5326 * We first wrap it into a set
5330 * and define (1) on top of the corresponding space, resulting in "aff".
5331 * We use this to create an isl_multi_aff that maps the output position "d"
5332 * from "a" to "x", leaving all other (intput and output) dimensions unchanged.
5333 * We plug this into the wrapped map, unwrap the result and compute the
5334 * corresponding isl_pw_multi_aff.
5335 * The result is an expression
5343 * so that we can plug that into "aff", after extending the latter to
5349 * If "map" is actually a set, then there is no "A" space, meaning
5350 * that we do not need to perform any wrapping, and that the result
5351 * of the recursive call is of the form
5355 * which is plugged into a mapping of the form
5359 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_stride(
5360 __isl_take isl_map
*map
, __isl_take isl_basic_map
*hull
, int d
, int i
,
5365 isl_local_space
*ls
;
5368 isl_pw_multi_aff
*pma
, *id
;
5374 is_set
= isl_map_is_set(map
);
5378 n_in
= isl_basic_map_dim(hull
, isl_dim_in
);
5379 n_out
= isl_basic_map_dim(hull
, isl_dim_out
);
5380 if (n_in
< 0 || n_out
< 0)
5382 o_out
= isl_basic_map_offset(hull
, isl_dim_out
);
5387 set
= isl_map_wrap(map
);
5388 space
= isl_space_map_from_set(isl_set_get_space(set
));
5389 ma
= isl_multi_aff_identity(space
);
5390 ls
= isl_local_space_from_space(isl_set_get_space(set
));
5391 aff
= isl_aff_alloc(ls
);
5393 isl_int_set_si(aff
->v
->el
[0], 1);
5394 if (isl_int_is_one(hull
->eq
[i
][o_out
+ d
]))
5395 isl_seq_neg(aff
->v
->el
+ 1, hull
->eq
[i
],
5398 isl_seq_cpy(aff
->v
->el
+ 1, hull
->eq
[i
],
5400 isl_int_set(aff
->v
->el
[1 + o_out
+ d
], gcd
);
5402 ma
= isl_multi_aff_set_aff(ma
, n_in
+ d
, isl_aff_copy(aff
));
5403 set
= isl_set_preimage_multi_aff(set
, ma
);
5405 ma
= range_map(aff
, d
, n_in
, n_out
, is_set
);
5410 map
= isl_set_unwrap(set
);
5411 pma
= isl_pw_multi_aff_from_map(map
);
5414 space
= isl_pw_multi_aff_get_domain_space(pma
);
5415 space
= isl_space_map_from_set(space
);
5416 id
= isl_pw_multi_aff_identity(space
);
5417 pma
= isl_pw_multi_aff_range_product(id
, pma
);
5419 id
= isl_pw_multi_aff_from_multi_aff(ma
);
5420 pma
= isl_pw_multi_aff_pullback_pw_multi_aff(id
, pma
);
5422 isl_basic_map_free(hull
);
5426 isl_basic_map_free(hull
);
5430 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5431 * "hull" contains the equalities valid for "map".
5433 * Check if any of the output dimensions is "strided".
5434 * That is, we check if it can be written as
5438 * with m greater than 1, a some combination of existentially quantified
5439 * variables and f an expression in the parameters and input dimensions.
5440 * If so, we remove the stride in pw_multi_aff_from_map_stride.
5442 * Otherwise, we continue with pw_multi_aff_from_map_check_div for a further
5445 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_check_strides(
5446 __isl_take isl_map
*map
, __isl_take isl_basic_map
*hull
)
5455 n_div
= isl_basic_map_dim(hull
, isl_dim_div
);
5456 n_out
= isl_basic_map_dim(hull
, isl_dim_out
);
5457 if (n_div
< 0 || n_out
< 0)
5461 isl_basic_map_free(hull
);
5462 return pw_multi_aff_from_map_check_div(map
);
5467 o_div
= isl_basic_map_offset(hull
, isl_dim_div
);
5468 o_out
= isl_basic_map_offset(hull
, isl_dim_out
);
5470 for (i
= 0; i
< n_out
; ++i
) {
5471 for (j
= 0; j
< hull
->n_eq
; ++j
) {
5472 isl_int
*eq
= hull
->eq
[j
];
5473 isl_pw_multi_aff
*res
;
5475 if (!isl_int_is_one(eq
[o_out
+ i
]) &&
5476 !isl_int_is_negone(eq
[o_out
+ i
]))
5478 if (isl_seq_first_non_zero(eq
+ o_out
, i
) != -1)
5480 if (isl_seq_first_non_zero(eq
+ o_out
+ i
+ 1,
5481 n_out
- (i
+ 1)) != -1)
5483 isl_seq_gcd(eq
+ o_div
, n_div
, &gcd
);
5484 if (isl_int_is_zero(gcd
))
5486 if (isl_int_is_one(gcd
))
5489 res
= pw_multi_aff_from_map_stride(map
, hull
,
5497 isl_basic_map_free(hull
);
5498 return pw_multi_aff_from_map_check_div(map
);
5501 isl_basic_map_free(hull
);
5505 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5507 * As a special case, we first check if all output dimensions are uniquely
5508 * defined in terms of the parameters and input dimensions over the entire
5509 * domain. If so, we extract the desired isl_pw_multi_aff directly
5510 * from the affine hull of "map" and its domain.
5512 * Otherwise, continue with pw_multi_aff_from_map_check_strides for more
5515 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_map(__isl_take isl_map
*map
)
5519 isl_basic_map
*hull
;
5521 n
= isl_map_n_basic_map(map
);
5526 hull
= isl_map_unshifted_simple_hull(isl_map_copy(map
));
5527 hull
= isl_basic_map_plain_affine_hull(hull
);
5528 sv
= isl_basic_map_plain_is_single_valued(hull
);
5530 return plain_pw_multi_aff_from_map(isl_map_domain(map
),
5532 isl_basic_map_free(hull
);
5534 map
= isl_map_detect_equalities(map
);
5535 hull
= isl_map_unshifted_simple_hull(isl_map_copy(map
));
5536 sv
= isl_basic_map_plain_is_single_valued(hull
);
5538 return plain_pw_multi_aff_from_map(isl_map_domain(map
), hull
);
5540 return pw_multi_aff_from_map_check_strides(map
, hull
);
5541 isl_basic_map_free(hull
);
5547 /* This function performs the same operation as isl_pw_multi_aff_from_map,
5548 * but is considered as a function on an isl_map when exported.
5550 __isl_give isl_pw_multi_aff
*isl_map_as_pw_multi_aff(__isl_take isl_map
*map
)
5552 return isl_pw_multi_aff_from_map(map
);
5555 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_set(__isl_take isl_set
*set
)
5557 return isl_pw_multi_aff_from_map(set
);
5560 /* This function performs the same operation as isl_pw_multi_aff_from_set,
5561 * but is considered as a function on an isl_set when exported.
5563 __isl_give isl_pw_multi_aff
*isl_set_as_pw_multi_aff(__isl_take isl_set
*set
)
5565 return isl_pw_multi_aff_from_set(set
);
5568 /* Convert "map" into an isl_pw_multi_aff (if possible) and
5571 static isl_stat
pw_multi_aff_from_map(__isl_take isl_map
*map
, void *user
)
5573 isl_union_pw_multi_aff
**upma
= user
;
5574 isl_pw_multi_aff
*pma
;
5576 pma
= isl_pw_multi_aff_from_map(map
);
5577 *upma
= isl_union_pw_multi_aff_add_pw_multi_aff(*upma
, pma
);
5579 return *upma
? isl_stat_ok
: isl_stat_error
;
5582 /* Create an isl_union_pw_multi_aff with the given isl_aff on a universe
5585 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_aff(
5586 __isl_take isl_aff
*aff
)
5589 isl_pw_multi_aff
*pma
;
5591 ma
= isl_multi_aff_from_aff(aff
);
5592 pma
= isl_pw_multi_aff_from_multi_aff(ma
);
5593 return isl_union_pw_multi_aff_from_pw_multi_aff(pma
);
5596 /* Try and create an isl_union_pw_multi_aff that is equivalent
5597 * to the given isl_union_map.
5598 * The isl_union_map is required to be single-valued in each space.
5599 * Otherwise, an error is produced.
5601 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_union_map(
5602 __isl_take isl_union_map
*umap
)
5605 isl_union_pw_multi_aff
*upma
;
5607 space
= isl_union_map_get_space(umap
);
5608 upma
= isl_union_pw_multi_aff_empty(space
);
5609 if (isl_union_map_foreach_map(umap
, &pw_multi_aff_from_map
, &upma
) < 0)
5610 upma
= isl_union_pw_multi_aff_free(upma
);
5611 isl_union_map_free(umap
);
5616 /* This function performs the same operation as
5617 * isl_union_pw_multi_aff_from_union_map,
5618 * but is considered as a function on an isl_union_map when exported.
5620 __isl_give isl_union_pw_multi_aff
*isl_union_map_as_union_pw_multi_aff(
5621 __isl_take isl_union_map
*umap
)
5623 return isl_union_pw_multi_aff_from_union_map(umap
);
5626 /* Try and create an isl_union_pw_multi_aff that is equivalent
5627 * to the given isl_union_set.
5628 * The isl_union_set is required to be a singleton in each space.
5629 * Otherwise, an error is produced.
5631 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_union_set(
5632 __isl_take isl_union_set
*uset
)
5634 return isl_union_pw_multi_aff_from_union_map(uset
);
5637 /* Return the piecewise affine expression "set ? 1 : 0".
5639 __isl_give isl_pw_aff
*isl_set_indicator_function(__isl_take isl_set
*set
)
5642 isl_space
*space
= isl_set_get_space(set
);
5643 isl_local_space
*ls
= isl_local_space_from_space(space
);
5644 isl_aff
*zero
= isl_aff_zero_on_domain(isl_local_space_copy(ls
));
5645 isl_aff
*one
= isl_aff_zero_on_domain(ls
);
5647 one
= isl_aff_add_constant_si(one
, 1);
5648 pa
= isl_pw_aff_alloc(isl_set_copy(set
), one
);
5649 set
= isl_set_complement(set
);
5650 pa
= isl_pw_aff_add_disjoint(pa
, isl_pw_aff_alloc(set
, zero
));
5655 /* Plug in "subs" for dimension "type", "pos" of "aff".
5657 * Let i be the dimension to replace and let "subs" be of the form
5661 * and "aff" of the form
5667 * (a f + d g')/(m d)
5669 * where g' is the result of plugging in "subs" in each of the integer
5672 __isl_give isl_aff
*isl_aff_substitute(__isl_take isl_aff
*aff
,
5673 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
5679 aff
= isl_aff_cow(aff
);
5681 return isl_aff_free(aff
);
5683 ctx
= isl_aff_get_ctx(aff
);
5684 if (!isl_space_is_equal(aff
->ls
->dim
, subs
->ls
->dim
))
5685 isl_die(ctx
, isl_error_invalid
,
5686 "spaces don't match", return isl_aff_free(aff
));
5687 n_div
= isl_aff_domain_dim(subs
, isl_dim_div
);
5689 return isl_aff_free(aff
);
5691 isl_die(ctx
, isl_error_unsupported
,
5692 "cannot handle divs yet", return isl_aff_free(aff
));
5694 aff
->ls
= isl_local_space_substitute(aff
->ls
, type
, pos
, subs
);
5696 return isl_aff_free(aff
);
5698 aff
->v
= isl_vec_cow(aff
->v
);
5700 return isl_aff_free(aff
);
5702 pos
+= isl_local_space_offset(aff
->ls
, type
);
5705 isl_seq_substitute(aff
->v
->el
, pos
, subs
->v
->el
,
5706 aff
->v
->size
, subs
->v
->size
, v
);
5712 /* Plug in "subs" for dimension "type", "pos" in each of the affine
5713 * expressions in "maff".
5715 __isl_give isl_multi_aff
*isl_multi_aff_substitute(
5716 __isl_take isl_multi_aff
*maff
, enum isl_dim_type type
, unsigned pos
,
5717 __isl_keep isl_aff
*subs
)
5722 n
= isl_multi_aff_size(maff
);
5724 return isl_multi_aff_free(maff
);
5726 if (type
== isl_dim_in
)
5729 for (i
= 0; i
< n
; ++i
) {
5732 aff
= isl_multi_aff_take_at(maff
, i
);
5733 aff
= isl_aff_substitute(aff
, type
, pos
, subs
);
5734 maff
= isl_multi_aff_restore_at(maff
, i
, aff
);
5740 /* Plug in "subs" for input dimension "pos" of "pma".
5742 * pma is of the form
5746 * while subs is of the form
5748 * v' = B_j(v) -> S_j
5750 * Each pair i,j such that C_ij = A_i \cap B_i is non-empty
5751 * has a contribution in the result, in particular
5753 * C_ij(S_j) -> M_i(S_j)
5755 * Note that plugging in S_j in C_ij may also result in an empty set
5756 * and this contribution should simply be discarded.
5758 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_substitute(
5759 __isl_take isl_pw_multi_aff
*pma
, unsigned pos
,
5760 __isl_keep isl_pw_aff
*subs
)
5763 isl_pw_multi_aff
*res
;
5766 return isl_pw_multi_aff_free(pma
);
5768 n
= pma
->n
* subs
->n
;
5769 res
= isl_pw_multi_aff_alloc_size(isl_space_copy(pma
->dim
), n
);
5771 for (i
= 0; i
< pma
->n
; ++i
) {
5772 for (j
= 0; j
< subs
->n
; ++j
) {
5774 isl_multi_aff
*res_ij
;
5777 common
= isl_set_intersect(
5778 isl_set_copy(pma
->p
[i
].set
),
5779 isl_set_copy(subs
->p
[j
].set
));
5780 common
= isl_set_substitute(common
,
5781 pos
, subs
->p
[j
].aff
);
5782 empty
= isl_set_plain_is_empty(common
);
5783 if (empty
< 0 || empty
) {
5784 isl_set_free(common
);
5790 res_ij
= isl_multi_aff_substitute(
5791 isl_multi_aff_copy(pma
->p
[i
].maff
),
5792 isl_dim_in
, pos
, subs
->p
[j
].aff
);
5794 res
= isl_pw_multi_aff_add_piece(res
, common
, res_ij
);
5798 isl_pw_multi_aff_free(pma
);
5801 isl_pw_multi_aff_free(pma
);
5802 isl_pw_multi_aff_free(res
);
5806 /* Compute the preimage of a range of dimensions in the affine expression "src"
5807 * under "ma" and put the result in "dst". The number of dimensions in "src"
5808 * that precede the range is given by "n_before". The number of dimensions
5809 * in the range is given by the number of output dimensions of "ma".
5810 * The number of dimensions that follow the range is given by "n_after".
5811 * If "has_denom" is set (to one),
5812 * then "src" and "dst" have an extra initial denominator.
5813 * "n_div_ma" is the number of existentials in "ma"
5814 * "n_div_bset" is the number of existentials in "src"
5815 * The resulting "dst" (which is assumed to have been allocated by
5816 * the caller) contains coefficients for both sets of existentials,
5817 * first those in "ma" and then those in "src".
5818 * f, c1, c2 and g are temporary objects that have been initialized
5821 * Let src represent the expression
5823 * (a(p) + f_u u + b v + f_w w + c(divs))/d
5825 * and let ma represent the expressions
5827 * v_i = (r_i(p) + s_i(y) + t_i(divs'))/m_i
5829 * We start out with the following expression for dst:
5831 * (a(p) + f_u u + 0 y + f_w w + 0 divs' + c(divs) + f \sum_i b_i v_i)/d
5833 * with the multiplication factor f initially equal to 1
5834 * and f \sum_i b_i v_i kept separately.
5835 * For each x_i that we substitute, we multiply the numerator
5836 * (and denominator) of dst by c_1 = m_i and add the numerator
5837 * of the x_i expression multiplied by c_2 = f b_i,
5838 * after removing the common factors of c_1 and c_2.
5839 * The multiplication factor f also needs to be multiplied by c_1
5840 * for the next x_j, j > i.
5842 isl_stat
isl_seq_preimage(isl_int
*dst
, isl_int
*src
,
5843 __isl_keep isl_multi_aff
*ma
, int n_before
, int n_after
,
5844 int n_div_ma
, int n_div_bmap
,
5845 isl_int f
, isl_int c1
, isl_int c2
, isl_int g
, int has_denom
)
5848 isl_size n_param
, n_in
, n_out
;
5851 n_param
= isl_multi_aff_dim(ma
, isl_dim_param
);
5852 n_in
= isl_multi_aff_dim(ma
, isl_dim_in
);
5853 n_out
= isl_multi_aff_dim(ma
, isl_dim_out
);
5854 if (n_param
< 0 || n_in
< 0 || n_out
< 0)
5855 return isl_stat_error
;
5857 isl_seq_cpy(dst
, src
, has_denom
+ 1 + n_param
+ n_before
);
5858 o_dst
= o_src
= has_denom
+ 1 + n_param
+ n_before
;
5859 isl_seq_clr(dst
+ o_dst
, n_in
);
5862 isl_seq_cpy(dst
+ o_dst
, src
+ o_src
, n_after
);
5865 isl_seq_clr(dst
+ o_dst
, n_div_ma
);
5867 isl_seq_cpy(dst
+ o_dst
, src
+ o_src
, n_div_bmap
);
5869 isl_int_set_si(f
, 1);
5871 for (i
= 0; i
< n_out
; ++i
) {
5872 int offset
= has_denom
+ 1 + n_param
+ n_before
+ i
;
5874 if (isl_int_is_zero(src
[offset
]))
5876 isl_int_set(c1
, ma
->u
.p
[i
]->v
->el
[0]);
5877 isl_int_mul(c2
, f
, src
[offset
]);
5878 isl_int_gcd(g
, c1
, c2
);
5879 isl_int_divexact(c1
, c1
, g
);
5880 isl_int_divexact(c2
, c2
, g
);
5882 isl_int_mul(f
, f
, c1
);
5885 isl_seq_combine(dst
+ o_dst
, c1
, dst
+ o_dst
,
5886 c2
, ma
->u
.p
[i
]->v
->el
+ o_src
, 1 + n_param
);
5887 o_dst
+= 1 + n_param
;
5888 o_src
+= 1 + n_param
;
5889 isl_seq_scale(dst
+ o_dst
, dst
+ o_dst
, c1
, n_before
);
5891 isl_seq_combine(dst
+ o_dst
, c1
, dst
+ o_dst
,
5892 c2
, ma
->u
.p
[i
]->v
->el
+ o_src
, n_in
);
5895 isl_seq_scale(dst
+ o_dst
, dst
+ o_dst
, c1
, n_after
);
5897 isl_seq_combine(dst
+ o_dst
, c1
, dst
+ o_dst
,
5898 c2
, ma
->u
.p
[i
]->v
->el
+ o_src
, n_div_ma
);
5901 isl_seq_scale(dst
+ o_dst
, dst
+ o_dst
, c1
, n_div_bmap
);
5903 isl_int_mul(dst
[0], dst
[0], c1
);
5909 /* Compute the pullback of "aff" by the function represented by "ma".
5910 * In other words, plug in "ma" in "aff". The result is an affine expression
5911 * defined over the domain space of "ma".
5913 * If "aff" is represented by
5915 * (a(p) + b x + c(divs))/d
5917 * and ma is represented by
5919 * x = D(p) + F(y) + G(divs')
5921 * then the result is
5923 * (a(p) + b D(p) + b F(y) + b G(divs') + c(divs))/d
5925 * The divs in the local space of the input are similarly adjusted
5926 * through a call to isl_local_space_preimage_multi_aff.
5928 __isl_give isl_aff
*isl_aff_pullback_multi_aff(__isl_take isl_aff
*aff
,
5929 __isl_take isl_multi_aff
*ma
)
5931 isl_aff
*res
= NULL
;
5932 isl_local_space
*ls
;
5933 isl_size n_div_aff
, n_div_ma
;
5934 isl_int f
, c1
, c2
, g
;
5936 ma
= isl_multi_aff_align_divs(ma
);
5940 n_div_aff
= isl_aff_dim(aff
, isl_dim_div
);
5941 n_div_ma
= ma
->n
? isl_aff_dim(ma
->u
.p
[0], isl_dim_div
) : 0;
5942 if (n_div_aff
< 0 || n_div_ma
< 0)
5945 ls
= isl_aff_get_domain_local_space(aff
);
5946 ls
= isl_local_space_preimage_multi_aff(ls
, isl_multi_aff_copy(ma
));
5947 res
= isl_aff_alloc(ls
);
5956 if (isl_seq_preimage(res
->v
->el
, aff
->v
->el
, ma
, 0, 0,
5957 n_div_ma
, n_div_aff
, f
, c1
, c2
, g
, 1) < 0)
5958 res
= isl_aff_free(res
);
5966 isl_multi_aff_free(ma
);
5967 res
= isl_aff_normalize(res
);
5971 isl_multi_aff_free(ma
);
5976 /* Compute the pullback of "aff1" by the function represented by "aff2".
5977 * In other words, plug in "aff2" in "aff1". The result is an affine expression
5978 * defined over the domain space of "aff1".
5980 * The domain of "aff1" should match the range of "aff2", which means
5981 * that it should be single-dimensional.
5983 __isl_give isl_aff
*isl_aff_pullback_aff(__isl_take isl_aff
*aff1
,
5984 __isl_take isl_aff
*aff2
)
5988 ma
= isl_multi_aff_from_aff(aff2
);
5989 return isl_aff_pullback_multi_aff(aff1
, ma
);
5992 /* Compute the pullback of "ma1" by the function represented by "ma2".
5993 * In other words, plug in "ma2" in "ma1".
5995 __isl_give isl_multi_aff
*isl_multi_aff_pullback_multi_aff(
5996 __isl_take isl_multi_aff
*ma1
, __isl_take isl_multi_aff
*ma2
)
6000 isl_space
*space
= NULL
;
6002 isl_multi_aff_align_params_bin(&ma1
, &ma2
);
6003 ma2
= isl_multi_aff_align_divs(ma2
);
6004 n
= isl_multi_aff_size(ma1
);
6008 space
= isl_space_join(isl_multi_aff_get_space(ma2
),
6009 isl_multi_aff_get_space(ma1
));
6011 for (i
= 0; i
< n
; ++i
) {
6014 aff
= isl_multi_aff_take_at(ma1
, i
);
6015 aff
= isl_aff_pullback_multi_aff(aff
, isl_multi_aff_copy(ma2
));
6016 ma1
= isl_multi_aff_restore_at(ma1
, i
, aff
);
6019 ma1
= isl_multi_aff_reset_space(ma1
, space
);
6020 isl_multi_aff_free(ma2
);
6023 isl_space_free(space
);
6024 isl_multi_aff_free(ma2
);
6025 isl_multi_aff_free(ma1
);
6029 /* Extend the local space of "dst" to include the divs
6030 * in the local space of "src".
6032 * If "src" does not have any divs or if the local spaces of "dst" and
6033 * "src" are the same, then no extension is required.
6035 __isl_give isl_aff
*isl_aff_align_divs(__isl_take isl_aff
*dst
,
6036 __isl_keep isl_aff
*src
)
6039 isl_size src_n_div
, dst_n_div
;
6046 return isl_aff_free(dst
);
6048 ctx
= isl_aff_get_ctx(src
);
6049 equal
= isl_local_space_has_equal_space(src
->ls
, dst
->ls
);
6051 return isl_aff_free(dst
);
6053 isl_die(ctx
, isl_error_invalid
,
6054 "spaces don't match", goto error
);
6056 src_n_div
= isl_aff_domain_dim(src
, isl_dim_div
);
6057 dst_n_div
= isl_aff_domain_dim(dst
, isl_dim_div
);
6060 equal
= isl_local_space_is_equal(src
->ls
, dst
->ls
);
6061 if (equal
< 0 || src_n_div
< 0 || dst_n_div
< 0)
6062 return isl_aff_free(dst
);
6066 exp1
= isl_alloc_array(ctx
, int, src_n_div
);
6067 exp2
= isl_alloc_array(ctx
, int, dst_n_div
);
6068 if (!exp1
|| (dst_n_div
&& !exp2
))
6071 div
= isl_merge_divs(src
->ls
->div
, dst
->ls
->div
, exp1
, exp2
);
6072 dst
= isl_aff_expand_divs(dst
, div
, exp2
);
6080 return isl_aff_free(dst
);
6083 /* Adjust the local spaces of the affine expressions in "maff"
6084 * such that they all have the save divs.
6086 __isl_give isl_multi_aff
*isl_multi_aff_align_divs(
6087 __isl_take isl_multi_aff
*maff
)
6093 n
= isl_multi_aff_size(maff
);
6095 return isl_multi_aff_free(maff
);
6099 aff_0
= isl_multi_aff_take_at(maff
, 0);
6100 for (i
= 1; i
< n
; ++i
) {
6103 aff_i
= isl_multi_aff_peek_at(maff
, i
);
6104 aff_0
= isl_aff_align_divs(aff_0
, aff_i
);
6106 maff
= isl_multi_aff_restore_at(maff
, 0, aff_0
);
6108 aff_0
= isl_multi_aff_peek_at(maff
, 0);
6109 for (i
= 1; i
< n
; ++i
) {
6112 aff_i
= isl_multi_aff_take_at(maff
, i
);
6113 aff_i
= isl_aff_align_divs(aff_i
, aff_0
);
6114 maff
= isl_multi_aff_restore_at(maff
, i
, aff_i
);
6120 __isl_give isl_aff
*isl_aff_lift(__isl_take isl_aff
*aff
)
6122 aff
= isl_aff_cow(aff
);
6126 aff
->ls
= isl_local_space_lift(aff
->ls
);
6128 return isl_aff_free(aff
);
6133 /* Lift "maff" to a space with extra dimensions such that the result
6134 * has no more existentially quantified variables.
6135 * If "ls" is not NULL, then *ls is assigned the local space that lies
6136 * at the basis of the lifting applied to "maff".
6138 __isl_give isl_multi_aff
*isl_multi_aff_lift(__isl_take isl_multi_aff
*maff
,
6139 __isl_give isl_local_space
**ls
)
6149 n
= isl_multi_aff_size(maff
);
6151 return isl_multi_aff_free(maff
);
6155 isl_space
*space
= isl_multi_aff_get_domain_space(maff
);
6156 *ls
= isl_local_space_from_space(space
);
6158 return isl_multi_aff_free(maff
);
6163 maff
= isl_multi_aff_align_divs(maff
);
6165 aff
= isl_multi_aff_peek_at(maff
, 0);
6166 n_div
= isl_aff_dim(aff
, isl_dim_div
);
6168 return isl_multi_aff_free(maff
);
6169 space
= isl_multi_aff_get_space(maff
);
6170 space
= isl_space_lift(isl_space_domain(space
), n_div
);
6171 space
= isl_space_extend_domain_with_range(space
,
6172 isl_multi_aff_get_space(maff
));
6173 maff
= isl_multi_aff_restore_space(maff
, space
);
6176 aff
= isl_multi_aff_peek_at(maff
, 0);
6177 *ls
= isl_aff_get_domain_local_space(aff
);
6179 return isl_multi_aff_free(maff
);
6182 for (i
= 0; i
< n
; ++i
) {
6183 aff
= isl_multi_aff_take_at(maff
, i
);
6184 aff
= isl_aff_lift(aff
);
6185 maff
= isl_multi_aff_restore_at(maff
, i
, aff
);
6192 #define TYPE isl_pw_multi_aff
6194 #include "check_type_range_templ.c"
6196 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma".
6198 __isl_give isl_pw_aff
*isl_pw_multi_aff_get_at(
6199 __isl_keep isl_pw_multi_aff
*pma
, int pos
)
6206 if (isl_pw_multi_aff_check_range(pma
, isl_dim_out
, pos
, 1) < 0)
6209 n_out
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
6213 space
= isl_pw_multi_aff_get_space(pma
);
6214 space
= isl_space_drop_dims(space
, isl_dim_out
,
6215 pos
+ 1, n_out
- pos
- 1);
6216 space
= isl_space_drop_dims(space
, isl_dim_out
, 0, pos
);
6218 pa
= isl_pw_aff_alloc_size(space
, pma
->n
);
6219 for (i
= 0; i
< pma
->n
; ++i
) {
6221 aff
= isl_multi_aff_get_aff(pma
->p
[i
].maff
, pos
);
6222 pa
= isl_pw_aff_add_piece(pa
, isl_set_copy(pma
->p
[i
].set
), aff
);
6228 /* This is an alternative name for the function above.
6230 __isl_give isl_pw_aff
*isl_pw_multi_aff_get_pw_aff(
6231 __isl_keep isl_pw_multi_aff
*pma
, int pos
)
6233 return isl_pw_multi_aff_get_at(pma
, pos
);
6236 /* Return an isl_pw_multi_aff with the given "set" as domain and
6237 * an unnamed zero-dimensional range.
6239 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_domain(
6240 __isl_take isl_set
*set
)
6245 space
= isl_set_get_space(set
);
6246 space
= isl_space_from_domain(space
);
6247 ma
= isl_multi_aff_zero(space
);
6248 return isl_pw_multi_aff_alloc(set
, ma
);
6251 /* Add an isl_pw_multi_aff with the given "set" as domain and
6252 * an unnamed zero-dimensional range to *user.
6254 static isl_stat
add_pw_multi_aff_from_domain(__isl_take isl_set
*set
,
6257 isl_union_pw_multi_aff
**upma
= user
;
6258 isl_pw_multi_aff
*pma
;
6260 pma
= isl_pw_multi_aff_from_domain(set
);
6261 *upma
= isl_union_pw_multi_aff_add_pw_multi_aff(*upma
, pma
);
6266 /* Return an isl_union_pw_multi_aff with the given "uset" as domain and
6267 * an unnamed zero-dimensional range.
6269 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_domain(
6270 __isl_take isl_union_set
*uset
)
6273 isl_union_pw_multi_aff
*upma
;
6278 space
= isl_union_set_get_space(uset
);
6279 upma
= isl_union_pw_multi_aff_empty(space
);
6281 if (isl_union_set_foreach_set(uset
,
6282 &add_pw_multi_aff_from_domain
, &upma
) < 0)
6285 isl_union_set_free(uset
);
6288 isl_union_set_free(uset
);
6289 isl_union_pw_multi_aff_free(upma
);
6293 /* Local data for bin_entry and the callback "fn".
6295 struct isl_union_pw_multi_aff_bin_data
{
6296 isl_union_pw_multi_aff
*upma2
;
6297 isl_union_pw_multi_aff
*res
;
6298 isl_pw_multi_aff
*pma
;
6299 isl_stat (*fn
)(__isl_take isl_pw_multi_aff
*pma
, void *user
);
6302 /* Given an isl_pw_multi_aff from upma1, store it in data->pma
6303 * and call data->fn for each isl_pw_multi_aff in data->upma2.
6305 static isl_stat
bin_entry(__isl_take isl_pw_multi_aff
*pma
, void *user
)
6307 struct isl_union_pw_multi_aff_bin_data
*data
= user
;
6311 r
= isl_union_pw_multi_aff_foreach_pw_multi_aff(data
->upma2
,
6313 isl_pw_multi_aff_free(pma
);
6318 /* Call "fn" on each pair of isl_pw_multi_affs in "upma1" and "upma2".
6319 * The isl_pw_multi_aff from upma1 is stored in data->pma (where data is
6320 * passed as user field) and the isl_pw_multi_aff from upma2 is available
6321 * as *entry. The callback should adjust data->res if desired.
6323 static __isl_give isl_union_pw_multi_aff
*bin_op(
6324 __isl_take isl_union_pw_multi_aff
*upma1
,
6325 __isl_take isl_union_pw_multi_aff
*upma2
,
6326 isl_stat (*fn
)(__isl_take isl_pw_multi_aff
*pma
, void *user
))
6329 struct isl_union_pw_multi_aff_bin_data data
= { NULL
, NULL
, NULL
, fn
};
6331 space
= isl_union_pw_multi_aff_get_space(upma2
);
6332 upma1
= isl_union_pw_multi_aff_align_params(upma1
, space
);
6333 space
= isl_union_pw_multi_aff_get_space(upma1
);
6334 upma2
= isl_union_pw_multi_aff_align_params(upma2
, space
);
6336 if (!upma1
|| !upma2
)
6340 data
.res
= isl_union_pw_multi_aff_alloc_same_size(upma1
);
6341 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma1
,
6342 &bin_entry
, &data
) < 0)
6345 isl_union_pw_multi_aff_free(upma1
);
6346 isl_union_pw_multi_aff_free(upma2
);
6349 isl_union_pw_multi_aff_free(upma1
);
6350 isl_union_pw_multi_aff_free(upma2
);
6351 isl_union_pw_multi_aff_free(data
.res
);
6355 /* Given two isl_pw_multi_affs A -> B and C -> D,
6356 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
6358 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_range_product(
6359 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
6363 isl_pw_multi_aff_align_params_bin(&pma1
, &pma2
);
6364 space
= isl_space_range_product(isl_pw_multi_aff_get_space(pma1
),
6365 isl_pw_multi_aff_get_space(pma2
));
6366 return isl_pw_multi_aff_on_shared_domain_in(pma1
, pma2
, space
,
6367 &isl_multi_aff_range_product
);
6370 /* Given two isl_pw_multi_affs A -> B and C -> D,
6371 * construct an isl_pw_multi_aff (A * C) -> (B, D).
6373 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_flat_range_product(
6374 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
6378 isl_pw_multi_aff_align_params_bin(&pma1
, &pma2
);
6379 space
= isl_space_range_product(isl_pw_multi_aff_get_space(pma1
),
6380 isl_pw_multi_aff_get_space(pma2
));
6381 space
= isl_space_flatten_range(space
);
6382 return isl_pw_multi_aff_on_shared_domain_in(pma1
, pma2
, space
,
6383 &isl_multi_aff_flat_range_product
);
6386 /* If data->pma and "pma2" have the same domain space, then use "range_product"
6387 * to compute some form of range product and add the result to data->res.
6389 static isl_stat
gen_range_product_entry(__isl_take isl_pw_multi_aff
*pma2
,
6390 __isl_give isl_pw_multi_aff
*(*range_product
)(
6391 __isl_take isl_pw_multi_aff
*pma1
,
6392 __isl_take isl_pw_multi_aff
*pma2
),
6395 struct isl_union_pw_multi_aff_bin_data
*data
= user
;
6397 isl_space
*space1
, *space2
;
6399 space1
= isl_pw_multi_aff_peek_space(data
->pma
);
6400 space2
= isl_pw_multi_aff_peek_space(pma2
);
6401 match
= isl_space_tuple_is_equal(space1
, isl_dim_in
,
6402 space2
, isl_dim_in
);
6403 if (match
< 0 || !match
) {
6404 isl_pw_multi_aff_free(pma2
);
6405 return match
< 0 ? isl_stat_error
: isl_stat_ok
;
6408 pma2
= range_product(isl_pw_multi_aff_copy(data
->pma
), pma2
);
6410 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma2
);
6415 /* If data->pma and "pma2" have the same domain space, then compute
6416 * their flat range product and add the result to data->res.
6418 static isl_stat
flat_range_product_entry(__isl_take isl_pw_multi_aff
*pma2
,
6421 return gen_range_product_entry(pma2
,
6422 &isl_pw_multi_aff_flat_range_product
, user
);
6425 /* Given two isl_union_pw_multi_affs A -> B and C -> D,
6426 * construct an isl_union_pw_multi_aff (A * C) -> (B, D).
6428 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_flat_range_product(
6429 __isl_take isl_union_pw_multi_aff
*upma1
,
6430 __isl_take isl_union_pw_multi_aff
*upma2
)
6432 return bin_op(upma1
, upma2
, &flat_range_product_entry
);
6435 /* If data->pma and "pma2" have the same domain space, then compute
6436 * their range product and add the result to data->res.
6438 static isl_stat
range_product_entry(__isl_take isl_pw_multi_aff
*pma2
,
6441 return gen_range_product_entry(pma2
,
6442 &isl_pw_multi_aff_range_product
, user
);
6445 /* Given two isl_union_pw_multi_affs A -> B and C -> D,
6446 * construct an isl_union_pw_multi_aff (A * C) -> [B -> D].
6448 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_range_product(
6449 __isl_take isl_union_pw_multi_aff
*upma1
,
6450 __isl_take isl_union_pw_multi_aff
*upma2
)
6452 return bin_op(upma1
, upma2
, &range_product_entry
);
6455 /* Replace the affine expressions at position "pos" in "pma" by "pa".
6456 * The parameters are assumed to have been aligned.
6458 * The implementation essentially performs an isl_pw_*_on_shared_domain,
6459 * except that it works on two different isl_pw_* types.
6461 static __isl_give isl_pw_multi_aff
*pw_multi_aff_set_pw_aff(
6462 __isl_take isl_pw_multi_aff
*pma
, unsigned pos
,
6463 __isl_take isl_pw_aff
*pa
)
6466 isl_pw_multi_aff
*res
= NULL
;
6471 if (!isl_space_tuple_is_equal(pma
->dim
, isl_dim_in
,
6472 pa
->dim
, isl_dim_in
))
6473 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
6474 "domains don't match", goto error
);
6475 if (isl_pw_multi_aff_check_range(pma
, isl_dim_out
, pos
, 1) < 0)
6479 res
= isl_pw_multi_aff_alloc_size(isl_pw_multi_aff_get_space(pma
), n
);
6481 for (i
= 0; i
< pma
->n
; ++i
) {
6482 for (j
= 0; j
< pa
->n
; ++j
) {
6484 isl_multi_aff
*res_ij
;
6487 common
= isl_set_intersect(isl_set_copy(pma
->p
[i
].set
),
6488 isl_set_copy(pa
->p
[j
].set
));
6489 empty
= isl_set_plain_is_empty(common
);
6490 if (empty
< 0 || empty
) {
6491 isl_set_free(common
);
6497 res_ij
= isl_multi_aff_set_aff(
6498 isl_multi_aff_copy(pma
->p
[i
].maff
), pos
,
6499 isl_aff_copy(pa
->p
[j
].aff
));
6500 res_ij
= isl_multi_aff_gist(res_ij
,
6501 isl_set_copy(common
));
6503 res
= isl_pw_multi_aff_add_piece(res
, common
, res_ij
);
6507 isl_pw_multi_aff_free(pma
);
6508 isl_pw_aff_free(pa
);
6511 isl_pw_multi_aff_free(pma
);
6512 isl_pw_aff_free(pa
);
6513 return isl_pw_multi_aff_free(res
);
6516 /* Replace the affine expressions at position "pos" in "pma" by "pa".
6518 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_set_pw_aff(
6519 __isl_take isl_pw_multi_aff
*pma
, unsigned pos
,
6520 __isl_take isl_pw_aff
*pa
)
6522 isl_bool equal_params
;
6526 equal_params
= isl_space_has_equal_params(pma
->dim
, pa
->dim
);
6527 if (equal_params
< 0)
6530 return pw_multi_aff_set_pw_aff(pma
, pos
, pa
);
6531 if (isl_pw_multi_aff_check_named_params(pma
) < 0 ||
6532 isl_pw_aff_check_named_params(pa
) < 0)
6534 pma
= isl_pw_multi_aff_align_params(pma
, isl_pw_aff_get_space(pa
));
6535 pa
= isl_pw_aff_align_params(pa
, isl_pw_multi_aff_get_space(pma
));
6536 return pw_multi_aff_set_pw_aff(pma
, pos
, pa
);
6538 isl_pw_multi_aff_free(pma
);
6539 isl_pw_aff_free(pa
);
6543 /* Do the parameters of "pa" match those of "space"?
6545 isl_bool
isl_pw_aff_matching_params(__isl_keep isl_pw_aff
*pa
,
6546 __isl_keep isl_space
*space
)
6548 isl_space
*pa_space
;
6552 return isl_bool_error
;
6554 pa_space
= isl_pw_aff_get_space(pa
);
6556 match
= isl_space_has_equal_params(space
, pa_space
);
6558 isl_space_free(pa_space
);
6562 /* Check that the domain space of "pa" matches "space".
6564 isl_stat
isl_pw_aff_check_match_domain_space(__isl_keep isl_pw_aff
*pa
,
6565 __isl_keep isl_space
*space
)
6567 isl_space
*pa_space
;
6571 return isl_stat_error
;
6573 pa_space
= isl_pw_aff_get_space(pa
);
6575 match
= isl_space_has_equal_params(space
, pa_space
);
6579 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
6580 "parameters don't match", goto error
);
6581 match
= isl_space_tuple_is_equal(space
, isl_dim_in
,
6582 pa_space
, isl_dim_in
);
6586 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
6587 "domains don't match", goto error
);
6588 isl_space_free(pa_space
);
6591 isl_space_free(pa_space
);
6592 return isl_stat_error
;
6600 #include <isl_multi_explicit_domain.c>
6601 #include <isl_multi_pw_aff_explicit_domain.c>
6602 #include <isl_multi_templ.c>
6603 #include <isl_multi_un_op_templ.c>
6604 #include <isl_multi_bin_val_templ.c>
6605 #include <isl_multi_add_constant_templ.c>
6606 #include <isl_multi_apply_set.c>
6607 #include <isl_multi_arith_templ.c>
6608 #include <isl_multi_bind_templ.c>
6609 #include <isl_multi_bind_domain_templ.c>
6610 #include <isl_multi_coalesce.c>
6611 #include <isl_multi_domain_templ.c>
6612 #include <isl_multi_dim_id_templ.c>
6613 #include <isl_multi_dims.c>
6614 #include <isl_multi_from_base_templ.c>
6615 #include <isl_multi_gist.c>
6616 #include <isl_multi_hash.c>
6617 #include <isl_multi_identity_templ.c>
6618 #include <isl_multi_align_set.c>
6619 #include <isl_multi_insert_domain_templ.c>
6620 #include <isl_multi_intersect.c>
6621 #include <isl_multi_min_max_templ.c>
6622 #include <isl_multi_move_dims_templ.c>
6623 #include <isl_multi_nan_templ.c>
6624 #include <isl_multi_param_templ.c>
6625 #include <isl_multi_product_templ.c>
6626 #include <isl_multi_splice_templ.c>
6627 #include <isl_multi_tuple_id_templ.c>
6628 #include <isl_multi_union_add_templ.c>
6629 #include <isl_multi_zero_templ.c>
6630 #include <isl_multi_unbind_params_templ.c>
6632 /* Is every element of "mpa" defined over a single universe domain?
6634 isl_bool
isl_multi_pw_aff_isa_multi_aff(__isl_keep isl_multi_pw_aff
*mpa
)
6636 return isl_multi_pw_aff_every(mpa
, &isl_pw_aff_isa_aff
);
6639 /* Given that every element of "mpa" is defined over a single universe domain,
6640 * return the corresponding base expressions.
6642 __isl_give isl_multi_aff
*isl_multi_pw_aff_as_multi_aff(
6643 __isl_take isl_multi_pw_aff
*mpa
)
6649 n
= isl_multi_pw_aff_size(mpa
);
6651 mpa
= isl_multi_pw_aff_free(mpa
);
6652 ma
= isl_multi_aff_alloc(isl_multi_pw_aff_get_space(mpa
));
6653 for (i
= 0; i
< n
; ++i
) {
6656 aff
= isl_pw_aff_as_aff(isl_multi_pw_aff_get_at(mpa
, i
));
6657 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
6659 isl_multi_pw_aff_free(mpa
);
6663 /* If "mpa" has an explicit domain, then intersect the domain of "map"
6664 * with this explicit domain.
6666 __isl_give isl_map
*isl_map_intersect_multi_pw_aff_explicit_domain(
6667 __isl_take isl_map
*map
, __isl_keep isl_multi_pw_aff
*mpa
)
6671 if (!isl_multi_pw_aff_has_explicit_domain(mpa
))
6674 dom
= isl_multi_pw_aff_domain(isl_multi_pw_aff_copy(mpa
));
6675 map
= isl_map_intersect_domain(map
, dom
);
6680 /* Are all elements of "mpa" piecewise constants?
6682 isl_bool
isl_multi_pw_aff_is_cst(__isl_keep isl_multi_pw_aff
*mpa
)
6684 return isl_multi_pw_aff_every(mpa
, &isl_pw_aff_is_cst
);
6687 /* Does "mpa" have a non-trivial explicit domain?
6689 * The explicit domain, if present, is trivial if it represents
6690 * an (obviously) universe set.
6692 isl_bool
isl_multi_pw_aff_has_non_trivial_domain(
6693 __isl_keep isl_multi_pw_aff
*mpa
)
6696 return isl_bool_error
;
6697 if (!isl_multi_pw_aff_has_explicit_domain(mpa
))
6698 return isl_bool_false
;
6699 return isl_bool_not(isl_set_plain_is_universe(mpa
->u
.dom
));
6705 #include "isl_opt_mpa_templ.c"
6707 /* Compute the minima of the set dimensions as a function of the
6708 * parameters, but independently of the other set dimensions.
6710 __isl_give isl_multi_pw_aff
*isl_set_min_multi_pw_aff(__isl_take isl_set
*set
)
6712 return set_opt_mpa(set
, &isl_set_dim_min
);
6715 /* Compute the maxima of the set dimensions as a function of the
6716 * parameters, but independently of the other set dimensions.
6718 __isl_give isl_multi_pw_aff
*isl_set_max_multi_pw_aff(__isl_take isl_set
*set
)
6720 return set_opt_mpa(set
, &isl_set_dim_max
);
6726 #include "isl_opt_mpa_templ.c"
6728 /* Compute the minima of the output dimensions as a function of the
6729 * parameters and input dimensions, but independently of
6730 * the other output dimensions.
6732 __isl_give isl_multi_pw_aff
*isl_map_min_multi_pw_aff(__isl_take isl_map
*map
)
6734 return map_opt_mpa(map
, &isl_map_dim_min
);
6737 /* Compute the maxima of the output dimensions as a function of the
6738 * parameters and input dimensions, but independently of
6739 * the other output dimensions.
6741 __isl_give isl_multi_pw_aff
*isl_map_max_multi_pw_aff(__isl_take isl_map
*map
)
6743 return map_opt_mpa(map
, &isl_map_dim_max
);
6747 #define TYPE isl_pw_multi_aff
6748 #include "isl_type_check_match_range_multi_val.c"
6750 /* Apply "fn" to the base expressions of "pma" and "mv".
6752 static __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_op_multi_val(
6753 __isl_take isl_pw_multi_aff
*pma
, __isl_take isl_multi_val
*mv
,
6754 __isl_give isl_multi_aff
*(*fn
)(__isl_take isl_multi_aff
*ma
,
6755 __isl_take isl_multi_val
*mv
))
6760 if (isl_pw_multi_aff_check_match_range_multi_val(pma
, mv
) < 0)
6763 n
= isl_pw_multi_aff_n_piece(pma
);
6767 for (i
= 0; i
< n
; ++i
) {
6770 ma
= isl_pw_multi_aff_take_base_at(pma
, i
);
6771 ma
= fn(ma
, isl_multi_val_copy(mv
));
6772 pma
= isl_pw_multi_aff_restore_base_at(pma
, i
, ma
);
6775 isl_multi_val_free(mv
);
6778 isl_multi_val_free(mv
);
6779 isl_pw_multi_aff_free(pma
);
6783 /* Scale the elements of "pma" by the corresponding elements of "mv".
6785 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_scale_multi_val(
6786 __isl_take isl_pw_multi_aff
*pma
, __isl_take isl_multi_val
*mv
)
6788 return isl_pw_multi_aff_op_multi_val(pma
, mv
,
6789 &isl_multi_aff_scale_multi_val
);
6792 /* Scale the elements of "pma" down by the corresponding elements of "mv".
6794 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_scale_down_multi_val(
6795 __isl_take isl_pw_multi_aff
*pma
, __isl_take isl_multi_val
*mv
)
6797 return isl_pw_multi_aff_op_multi_val(pma
, mv
,
6798 &isl_multi_aff_scale_down_multi_val
);
6801 /* This function is called for each entry of an isl_union_pw_multi_aff.
6802 * If the space of the entry matches that of data->mv,
6803 * then apply isl_pw_multi_aff_scale_multi_val and return the result.
6804 * Otherwise, return an empty isl_pw_multi_aff.
6806 static __isl_give isl_pw_multi_aff
*union_pw_multi_aff_scale_multi_val_entry(
6807 __isl_take isl_pw_multi_aff
*pma
, void *user
)
6810 isl_multi_val
*mv
= user
;
6812 equal
= isl_pw_multi_aff_match_range_multi_val(pma
, mv
);
6814 return isl_pw_multi_aff_free(pma
);
6816 isl_space
*space
= isl_pw_multi_aff_get_space(pma
);
6817 isl_pw_multi_aff_free(pma
);
6818 return isl_pw_multi_aff_empty(space
);
6821 return isl_pw_multi_aff_scale_multi_val(pma
, isl_multi_val_copy(mv
));
6824 /* Scale the elements of "upma" by the corresponding elements of "mv",
6825 * for those entries that match the space of "mv".
6827 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_scale_multi_val(
6828 __isl_take isl_union_pw_multi_aff
*upma
, __isl_take isl_multi_val
*mv
)
6830 struct isl_union_pw_multi_aff_transform_control control
= {
6831 .fn
= &union_pw_multi_aff_scale_multi_val_entry
,
6835 upma
= isl_union_pw_multi_aff_align_params(upma
,
6836 isl_multi_val_get_space(mv
));
6837 mv
= isl_multi_val_align_params(mv
,
6838 isl_union_pw_multi_aff_get_space(upma
));
6842 return isl_union_pw_multi_aff_transform(upma
, &control
);
6844 isl_multi_val_free(mv
);
6847 isl_multi_val_free(mv
);
6848 isl_union_pw_multi_aff_free(upma
);
6852 /* Construct and return a piecewise multi affine expression
6853 * in the given space with value zero in each of the output dimensions and
6854 * a universe domain.
6856 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_zero(__isl_take isl_space
*space
)
6858 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_zero(space
));
6861 /* Construct and return a piecewise multi affine expression
6862 * that is equal to the given piecewise affine expression.
6864 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_pw_aff(
6865 __isl_take isl_pw_aff
*pa
)
6869 isl_pw_multi_aff
*pma
;
6874 space
= isl_pw_aff_get_space(pa
);
6875 pma
= isl_pw_multi_aff_alloc_size(space
, pa
->n
);
6877 for (i
= 0; i
< pa
->n
; ++i
) {
6881 set
= isl_set_copy(pa
->p
[i
].set
);
6882 ma
= isl_multi_aff_from_aff(isl_aff_copy(pa
->p
[i
].aff
));
6883 pma
= isl_pw_multi_aff_add_piece(pma
, set
, ma
);
6886 isl_pw_aff_free(pa
);
6890 /* Construct and return a piecewise multi affine expression
6891 * that is equal to the given multi piecewise affine expression
6892 * on the shared domain of the piecewise affine expressions,
6893 * in the special case of a 0D multi piecewise affine expression.
6895 * Create a piecewise multi affine expression with the explicit domain of
6896 * the 0D multi piecewise affine expression as domain.
6898 static __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_multi_pw_aff_0D(
6899 __isl_take isl_multi_pw_aff
*mpa
)
6905 space
= isl_multi_pw_aff_get_space(mpa
);
6906 dom
= isl_multi_pw_aff_get_explicit_domain(mpa
);
6907 isl_multi_pw_aff_free(mpa
);
6909 ma
= isl_multi_aff_zero(space
);
6910 return isl_pw_multi_aff_alloc(dom
, ma
);
6913 /* Construct and return a piecewise multi affine expression
6914 * that is equal to the given multi piecewise affine expression
6915 * on the shared domain of the piecewise affine expressions.
6917 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_multi_pw_aff(
6918 __isl_take isl_multi_pw_aff
*mpa
)
6923 isl_pw_multi_aff
*pma
;
6929 return isl_pw_multi_aff_from_multi_pw_aff_0D(mpa
);
6931 space
= isl_multi_pw_aff_get_space(mpa
);
6932 pa
= isl_multi_pw_aff_get_pw_aff(mpa
, 0);
6933 pma
= isl_pw_multi_aff_from_pw_aff(pa
);
6935 for (i
= 1; i
< mpa
->n
; ++i
) {
6936 isl_pw_multi_aff
*pma_i
;
6938 pa
= isl_multi_pw_aff_get_pw_aff(mpa
, i
);
6939 pma_i
= isl_pw_multi_aff_from_pw_aff(pa
);
6940 pma
= isl_pw_multi_aff_range_product(pma
, pma_i
);
6943 pma
= isl_pw_multi_aff_reset_space(pma
, space
);
6945 isl_multi_pw_aff_free(mpa
);
6949 /* Convenience function that constructs an isl_multi_pw_aff
6950 * directly from an isl_aff.
6952 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_from_aff(__isl_take isl_aff
*aff
)
6954 return isl_multi_pw_aff_from_pw_aff(isl_pw_aff_from_aff(aff
));
6957 /* Construct and return a multi piecewise affine expression
6958 * that is equal to the given multi affine expression.
6960 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_from_multi_aff(
6961 __isl_take isl_multi_aff
*ma
)
6965 isl_multi_pw_aff
*mpa
;
6967 n
= isl_multi_aff_dim(ma
, isl_dim_out
);
6969 ma
= isl_multi_aff_free(ma
);
6973 mpa
= isl_multi_pw_aff_alloc(isl_multi_aff_get_space(ma
));
6975 for (i
= 0; i
< n
; ++i
) {
6978 pa
= isl_pw_aff_from_aff(isl_multi_aff_get_aff(ma
, i
));
6979 mpa
= isl_multi_pw_aff_set_pw_aff(mpa
, i
, pa
);
6982 isl_multi_aff_free(ma
);
6986 /* This function performs the same operation as isl_multi_pw_aff_from_multi_aff,
6987 * but is considered as a function on an isl_multi_aff when exported.
6989 __isl_give isl_multi_pw_aff
*isl_multi_aff_to_multi_pw_aff(
6990 __isl_take isl_multi_aff
*ma
)
6992 return isl_multi_pw_aff_from_multi_aff(ma
);
6995 /* Construct and return a multi piecewise affine expression
6996 * that is equal to the given piecewise multi affine expression.
6998 * If the resulting multi piecewise affine expression has
6999 * an explicit domain, then assign it the domain of the input.
7000 * In other cases, the domain is stored in the individual elements.
7002 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_from_pw_multi_aff(
7003 __isl_take isl_pw_multi_aff
*pma
)
7008 isl_multi_pw_aff
*mpa
;
7010 n
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
7012 pma
= isl_pw_multi_aff_free(pma
);
7013 space
= isl_pw_multi_aff_get_space(pma
);
7014 mpa
= isl_multi_pw_aff_alloc(space
);
7016 for (i
= 0; i
< n
; ++i
) {
7019 pa
= isl_pw_multi_aff_get_pw_aff(pma
, i
);
7020 mpa
= isl_multi_pw_aff_set_pw_aff(mpa
, i
, pa
);
7022 if (isl_multi_pw_aff_has_explicit_domain(mpa
)) {
7025 dom
= isl_pw_multi_aff_domain(isl_pw_multi_aff_copy(pma
));
7026 mpa
= isl_multi_pw_aff_intersect_domain(mpa
, dom
);
7029 isl_pw_multi_aff_free(pma
);
7033 /* This function performs the same operation as
7034 * isl_multi_pw_aff_from_pw_multi_aff,
7035 * but is considered as a function on an isl_pw_multi_aff when exported.
7037 __isl_give isl_multi_pw_aff
*isl_pw_multi_aff_to_multi_pw_aff(
7038 __isl_take isl_pw_multi_aff
*pma
)
7040 return isl_multi_pw_aff_from_pw_multi_aff(pma
);
7043 /* Do "pa1" and "pa2" represent the same function?
7045 * We first check if they are obviously equal.
7046 * If not, we convert them to maps and check if those are equal.
7048 * If "pa1" or "pa2" contain any NaNs, then they are considered
7049 * not to be the same. A NaN is not equal to anything, not even
7052 isl_bool
isl_pw_aff_is_equal(__isl_keep isl_pw_aff
*pa1
,
7053 __isl_keep isl_pw_aff
*pa2
)
7057 isl_map
*map1
, *map2
;
7060 return isl_bool_error
;
7062 equal
= isl_pw_aff_plain_is_equal(pa1
, pa2
);
7063 if (equal
< 0 || equal
)
7065 has_nan
= either_involves_nan(pa1
, pa2
);
7067 return isl_bool_error
;
7069 return isl_bool_false
;
7071 map1
= isl_map_from_pw_aff_internal(isl_pw_aff_copy(pa1
));
7072 map2
= isl_map_from_pw_aff_internal(isl_pw_aff_copy(pa2
));
7073 equal
= isl_map_is_equal(map1
, map2
);
7080 /* Do "mpa1" and "mpa2" represent the same function?
7082 * Note that we cannot convert the entire isl_multi_pw_aff
7083 * to a map because the domains of the piecewise affine expressions
7084 * may not be the same.
7086 isl_bool
isl_multi_pw_aff_is_equal(__isl_keep isl_multi_pw_aff
*mpa1
,
7087 __isl_keep isl_multi_pw_aff
*mpa2
)
7090 isl_bool equal
, equal_params
;
7093 return isl_bool_error
;
7095 equal_params
= isl_space_has_equal_params(mpa1
->space
, mpa2
->space
);
7096 if (equal_params
< 0)
7097 return isl_bool_error
;
7098 if (!equal_params
) {
7099 if (!isl_space_has_named_params(mpa1
->space
))
7100 return isl_bool_false
;
7101 if (!isl_space_has_named_params(mpa2
->space
))
7102 return isl_bool_false
;
7103 mpa1
= isl_multi_pw_aff_copy(mpa1
);
7104 mpa2
= isl_multi_pw_aff_copy(mpa2
);
7105 mpa1
= isl_multi_pw_aff_align_params(mpa1
,
7106 isl_multi_pw_aff_get_space(mpa2
));
7107 mpa2
= isl_multi_pw_aff_align_params(mpa2
,
7108 isl_multi_pw_aff_get_space(mpa1
));
7109 equal
= isl_multi_pw_aff_is_equal(mpa1
, mpa2
);
7110 isl_multi_pw_aff_free(mpa1
);
7111 isl_multi_pw_aff_free(mpa2
);
7115 equal
= isl_space_is_equal(mpa1
->space
, mpa2
->space
);
7116 if (equal
< 0 || !equal
)
7119 for (i
= 0; i
< mpa1
->n
; ++i
) {
7120 equal
= isl_pw_aff_is_equal(mpa1
->u
.p
[i
], mpa2
->u
.p
[i
]);
7121 if (equal
< 0 || !equal
)
7125 return isl_bool_true
;
7128 /* Do "pma1" and "pma2" represent the same function?
7130 * First check if they are obviously equal.
7131 * If not, then convert them to maps and check if those are equal.
7133 * If "pa1" or "pa2" contain any NaNs, then they are considered
7134 * not to be the same. A NaN is not equal to anything, not even
7137 isl_bool
isl_pw_multi_aff_is_equal(__isl_keep isl_pw_multi_aff
*pma1
,
7138 __isl_keep isl_pw_multi_aff
*pma2
)
7142 isl_map
*map1
, *map2
;
7145 return isl_bool_error
;
7147 equal
= isl_pw_multi_aff_plain_is_equal(pma1
, pma2
);
7148 if (equal
< 0 || equal
)
7150 has_nan
= isl_pw_multi_aff_involves_nan(pma1
);
7151 if (has_nan
>= 0 && !has_nan
)
7152 has_nan
= isl_pw_multi_aff_involves_nan(pma2
);
7153 if (has_nan
< 0 || has_nan
)
7154 return isl_bool_not(has_nan
);
7156 map1
= isl_map_from_pw_multi_aff_internal(isl_pw_multi_aff_copy(pma1
));
7157 map2
= isl_map_from_pw_multi_aff_internal(isl_pw_multi_aff_copy(pma2
));
7158 equal
= isl_map_is_equal(map1
, map2
);
7166 #define BASE multi_aff
7168 #include "isl_multi_pw_aff_pullback_templ.c"
7171 #define BASE pw_multi_aff
7173 #include "isl_multi_pw_aff_pullback_templ.c"
7175 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
7176 * with the domain of "aff". The domain of the result is the same
7178 * "mpa" and "aff" are assumed to have been aligned.
7180 * We first extract the parametric constant from "aff", defined
7181 * over the correct domain.
7182 * Then we add the appropriate combinations of the members of "mpa".
7183 * Finally, we add the integer divisions through recursive calls.
7185 static __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_aff_aligned(
7186 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_aff
*aff
)
7189 isl_size n_in
, n_div
, n_mpa_in
;
7195 n_in
= isl_aff_dim(aff
, isl_dim_in
);
7196 n_div
= isl_aff_dim(aff
, isl_dim_div
);
7197 n_mpa_in
= isl_multi_pw_aff_dim(mpa
, isl_dim_in
);
7198 if (n_in
< 0 || n_div
< 0 || n_mpa_in
< 0)
7201 space
= isl_space_domain(isl_multi_pw_aff_get_space(mpa
));
7202 tmp
= isl_aff_copy(aff
);
7203 tmp
= isl_aff_drop_dims(tmp
, isl_dim_div
, 0, n_div
);
7204 tmp
= isl_aff_drop_dims(tmp
, isl_dim_in
, 0, n_in
);
7205 tmp
= isl_aff_add_dims(tmp
, isl_dim_in
, n_mpa_in
);
7206 tmp
= isl_aff_reset_domain_space(tmp
, space
);
7207 pa
= isl_pw_aff_from_aff(tmp
);
7209 for (i
= 0; i
< n_in
; ++i
) {
7212 if (!isl_aff_involves_dims(aff
, isl_dim_in
, i
, 1))
7214 v
= isl_aff_get_coefficient_val(aff
, isl_dim_in
, i
);
7215 pa_i
= isl_multi_pw_aff_get_pw_aff(mpa
, i
);
7216 pa_i
= isl_pw_aff_scale_val(pa_i
, v
);
7217 pa
= isl_pw_aff_add(pa
, pa_i
);
7220 for (i
= 0; i
< n_div
; ++i
) {
7224 if (!isl_aff_involves_dims(aff
, isl_dim_div
, i
, 1))
7226 div
= isl_aff_get_div(aff
, i
);
7227 pa_i
= isl_multi_pw_aff_apply_aff_aligned(
7228 isl_multi_pw_aff_copy(mpa
), div
);
7229 pa_i
= isl_pw_aff_floor(pa_i
);
7230 v
= isl_aff_get_coefficient_val(aff
, isl_dim_div
, i
);
7231 pa_i
= isl_pw_aff_scale_val(pa_i
, v
);
7232 pa
= isl_pw_aff_add(pa
, pa_i
);
7235 isl_multi_pw_aff_free(mpa
);
7240 isl_multi_pw_aff_free(mpa
);
7245 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
7246 * with the domain of "aff". The domain of the result is the same
7249 __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_aff(
7250 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_aff
*aff
)
7252 isl_bool equal_params
;
7256 equal_params
= isl_space_has_equal_params(aff
->ls
->dim
, mpa
->space
);
7257 if (equal_params
< 0)
7260 return isl_multi_pw_aff_apply_aff_aligned(mpa
, aff
);
7262 aff
= isl_aff_align_params(aff
, isl_multi_pw_aff_get_space(mpa
));
7263 mpa
= isl_multi_pw_aff_align_params(mpa
, isl_aff_get_space(aff
));
7265 return isl_multi_pw_aff_apply_aff_aligned(mpa
, aff
);
7268 isl_multi_pw_aff_free(mpa
);
7272 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
7273 * with the domain of "pa". The domain of the result is the same
7275 * "mpa" and "pa" are assumed to have been aligned.
7277 * We consider each piece in turn. Note that the domains of the
7278 * pieces are assumed to be disjoint and they remain disjoint
7279 * after taking the preimage (over the same function).
7281 static __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_pw_aff_aligned(
7282 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_pw_aff
*pa
)
7291 space
= isl_space_join(isl_multi_pw_aff_get_space(mpa
),
7292 isl_pw_aff_get_space(pa
));
7293 res
= isl_pw_aff_empty(space
);
7295 for (i
= 0; i
< pa
->n
; ++i
) {
7299 pa_i
= isl_multi_pw_aff_apply_aff_aligned(
7300 isl_multi_pw_aff_copy(mpa
),
7301 isl_aff_copy(pa
->p
[i
].aff
));
7302 domain
= isl_set_copy(pa
->p
[i
].set
);
7303 domain
= isl_set_preimage_multi_pw_aff(domain
,
7304 isl_multi_pw_aff_copy(mpa
));
7305 pa_i
= isl_pw_aff_intersect_domain(pa_i
, domain
);
7306 res
= isl_pw_aff_add_disjoint(res
, pa_i
);
7309 isl_pw_aff_free(pa
);
7310 isl_multi_pw_aff_free(mpa
);
7313 isl_pw_aff_free(pa
);
7314 isl_multi_pw_aff_free(mpa
);
7318 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
7319 * with the domain of "pa". The domain of the result is the same
7322 __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_pw_aff(
7323 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_pw_aff
*pa
)
7325 isl_bool equal_params
;
7329 equal_params
= isl_space_has_equal_params(pa
->dim
, mpa
->space
);
7330 if (equal_params
< 0)
7333 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa
, pa
);
7335 pa
= isl_pw_aff_align_params(pa
, isl_multi_pw_aff_get_space(mpa
));
7336 mpa
= isl_multi_pw_aff_align_params(mpa
, isl_pw_aff_get_space(pa
));
7338 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa
, pa
);
7340 isl_pw_aff_free(pa
);
7341 isl_multi_pw_aff_free(mpa
);
7345 /* Compute the pullback of "pa" by the function represented by "mpa".
7346 * In other words, plug in "mpa" in "pa".
7348 * The pullback is computed by applying "pa" to "mpa".
7350 __isl_give isl_pw_aff
*isl_pw_aff_pullback_multi_pw_aff(
7351 __isl_take isl_pw_aff
*pa
, __isl_take isl_multi_pw_aff
*mpa
)
7353 return isl_multi_pw_aff_apply_pw_aff(mpa
, pa
);
7357 #define BASE multi_pw_aff
7359 #include "isl_multi_pw_aff_pullback_templ.c"
7361 /* Align the parameters of "mpa1" and "mpa2", check that the ranges
7362 * of "mpa1" and "mpa2" live in the same space, construct map space
7363 * between the domain spaces of "mpa1" and "mpa2" and call "order"
7364 * with this map space as extract argument.
7366 static __isl_give isl_map
*isl_multi_pw_aff_order_map(
7367 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
,
7368 __isl_give isl_map
*(*order
)(__isl_keep isl_multi_pw_aff
*mpa1
,
7369 __isl_keep isl_multi_pw_aff
*mpa2
, __isl_take isl_space
*space
))
7372 isl_space
*space1
, *space2
;
7375 mpa1
= isl_multi_pw_aff_align_params(mpa1
,
7376 isl_multi_pw_aff_get_space(mpa2
));
7377 mpa2
= isl_multi_pw_aff_align_params(mpa2
,
7378 isl_multi_pw_aff_get_space(mpa1
));
7381 match
= isl_space_tuple_is_equal(mpa1
->space
, isl_dim_out
,
7382 mpa2
->space
, isl_dim_out
);
7386 isl_die(isl_multi_pw_aff_get_ctx(mpa1
), isl_error_invalid
,
7387 "range spaces don't match", goto error
);
7388 space1
= isl_space_domain(isl_multi_pw_aff_get_space(mpa1
));
7389 space2
= isl_space_domain(isl_multi_pw_aff_get_space(mpa2
));
7390 space1
= isl_space_map_from_domain_and_range(space1
, space2
);
7392 res
= order(mpa1
, mpa2
, space1
);
7393 isl_multi_pw_aff_free(mpa1
);
7394 isl_multi_pw_aff_free(mpa2
);
7397 isl_multi_pw_aff_free(mpa1
);
7398 isl_multi_pw_aff_free(mpa2
);
7402 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7403 * where the function values are equal. "space" is the space of the result.
7404 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7406 * "mpa1" and "mpa2" are equal when each of the pairs of elements
7407 * in the sequences are equal.
7409 static __isl_give isl_map
*isl_multi_pw_aff_eq_map_on_space(
7410 __isl_keep isl_multi_pw_aff
*mpa1
, __isl_keep isl_multi_pw_aff
*mpa2
,
7411 __isl_take isl_space
*space
)
7417 n
= isl_multi_pw_aff_dim(mpa1
, isl_dim_out
);
7419 space
= isl_space_free(space
);
7420 res
= isl_map_universe(space
);
7422 for (i
= 0; i
< n
; ++i
) {
7423 isl_pw_aff
*pa1
, *pa2
;
7426 pa1
= isl_multi_pw_aff_get_pw_aff(mpa1
, i
);
7427 pa2
= isl_multi_pw_aff_get_pw_aff(mpa2
, i
);
7428 map
= isl_pw_aff_eq_map(pa1
, pa2
);
7429 res
= isl_map_intersect(res
, map
);
7435 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7436 * where the function values are equal.
7438 __isl_give isl_map
*isl_multi_pw_aff_eq_map(__isl_take isl_multi_pw_aff
*mpa1
,
7439 __isl_take isl_multi_pw_aff
*mpa2
)
7441 return isl_multi_pw_aff_order_map(mpa1
, mpa2
,
7442 &isl_multi_pw_aff_eq_map_on_space
);
7445 /* Intersect "map" with the result of applying "order"
7446 * on two copies of "mpa".
7448 static __isl_give isl_map
*isl_map_order_at_multi_pw_aff(
7449 __isl_take isl_map
*map
, __isl_take isl_multi_pw_aff
*mpa
,
7450 __isl_give isl_map
*(*order
)(__isl_take isl_multi_pw_aff
*mpa1
,
7451 __isl_take isl_multi_pw_aff
*mpa2
))
7453 return isl_map_intersect(map
, order(mpa
, isl_multi_pw_aff_copy(mpa
)));
7456 /* Return the subset of "map" where the domain and the range
7457 * have equal "mpa" values.
7459 __isl_give isl_map
*isl_map_eq_at_multi_pw_aff(__isl_take isl_map
*map
,
7460 __isl_take isl_multi_pw_aff
*mpa
)
7462 return isl_map_order_at_multi_pw_aff(map
, mpa
,
7463 &isl_multi_pw_aff_eq_map
);
7466 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7467 * where the function values of "mpa1" lexicographically satisfies
7468 * "strict_base"/"base" compared to that of "mpa2".
7469 * "space" is the space of the result.
7470 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7472 * "mpa1" lexicographically satisfies "strict_base"/"base" compared to "mpa2"
7473 * if, for some i, the i-th element of "mpa1" satisfies "strict_base"/"base"
7474 * when compared to the i-th element of "mpa2" while all previous elements are
7476 * In particular, if i corresponds to the final elements
7477 * then they need to satisfy "base", while "strict_base" needs to be satisfied
7478 * for other values of i.
7479 * If "base" is a strict order, then "base" and "strict_base" are the same.
7481 static __isl_give isl_map
*isl_multi_pw_aff_lex_map_on_space(
7482 __isl_keep isl_multi_pw_aff
*mpa1
, __isl_keep isl_multi_pw_aff
*mpa2
,
7483 __isl_give isl_map
*(*strict_base
)(__isl_take isl_pw_aff
*pa1
,
7484 __isl_take isl_pw_aff
*pa2
),
7485 __isl_give isl_map
*(*base
)(__isl_take isl_pw_aff
*pa1
,
7486 __isl_take isl_pw_aff
*pa2
),
7487 __isl_take isl_space
*space
)
7491 isl_map
*res
, *rest
;
7493 n
= isl_multi_pw_aff_dim(mpa1
, isl_dim_out
);
7495 space
= isl_space_free(space
);
7496 res
= isl_map_empty(isl_space_copy(space
));
7497 rest
= isl_map_universe(space
);
7499 for (i
= 0; i
< n
; ++i
) {
7501 isl_pw_aff
*pa1
, *pa2
;
7506 pa1
= isl_multi_pw_aff_get_pw_aff(mpa1
, i
);
7507 pa2
= isl_multi_pw_aff_get_pw_aff(mpa2
, i
);
7508 map
= last
? base(pa1
, pa2
) : strict_base(pa1
, pa2
);
7509 map
= isl_map_intersect(map
, isl_map_copy(rest
));
7510 res
= isl_map_union(res
, map
);
7515 pa1
= isl_multi_pw_aff_get_pw_aff(mpa1
, i
);
7516 pa2
= isl_multi_pw_aff_get_pw_aff(mpa2
, i
);
7517 map
= isl_pw_aff_eq_map(pa1
, pa2
);
7518 rest
= isl_map_intersect(rest
, map
);
7528 #define STRICT_ORDER lt
7529 #include "isl_aff_lex_templ.c"
7534 #define STRICT_ORDER lt
7535 #include "isl_aff_lex_templ.c"
7540 #define STRICT_ORDER gt
7541 #include "isl_aff_lex_templ.c"
7546 #define STRICT_ORDER gt
7547 #include "isl_aff_lex_templ.c"
7549 /* Compare two isl_affs.
7551 * Return -1 if "aff1" is "smaller" than "aff2", 1 if "aff1" is "greater"
7552 * than "aff2" and 0 if they are equal.
7554 * The order is fairly arbitrary. We do consider expressions that only involve
7555 * earlier dimensions as "smaller".
7557 int isl_aff_plain_cmp(__isl_keep isl_aff
*aff1
, __isl_keep isl_aff
*aff2
)
7570 cmp
= isl_local_space_cmp(aff1
->ls
, aff2
->ls
);
7574 last1
= isl_seq_last_non_zero(aff1
->v
->el
+ 1, aff1
->v
->size
- 1);
7575 last2
= isl_seq_last_non_zero(aff2
->v
->el
+ 1, aff1
->v
->size
- 1);
7577 return last1
- last2
;
7579 return isl_seq_cmp(aff1
->v
->el
, aff2
->v
->el
, aff1
->v
->size
);
7582 /* Compare two isl_pw_affs.
7584 * Return -1 if "pa1" is "smaller" than "pa2", 1 if "pa1" is "greater"
7585 * than "pa2" and 0 if they are equal.
7587 * The order is fairly arbitrary. We do consider expressions that only involve
7588 * earlier dimensions as "smaller".
7590 int isl_pw_aff_plain_cmp(__isl_keep isl_pw_aff
*pa1
,
7591 __isl_keep isl_pw_aff
*pa2
)
7604 cmp
= isl_space_cmp(pa1
->dim
, pa2
->dim
);
7608 if (pa1
->n
!= pa2
->n
)
7609 return pa1
->n
- pa2
->n
;
7611 for (i
= 0; i
< pa1
->n
; ++i
) {
7612 cmp
= isl_set_plain_cmp(pa1
->p
[i
].set
, pa2
->p
[i
].set
);
7615 cmp
= isl_aff_plain_cmp(pa1
->p
[i
].aff
, pa2
->p
[i
].aff
);
7623 /* Return a piecewise affine expression that is equal to "v" on "domain".
7625 __isl_give isl_pw_aff
*isl_pw_aff_val_on_domain(__isl_take isl_set
*domain
,
7626 __isl_take isl_val
*v
)
7629 isl_local_space
*ls
;
7632 space
= isl_set_get_space(domain
);
7633 ls
= isl_local_space_from_space(space
);
7634 aff
= isl_aff_val_on_domain(ls
, v
);
7636 return isl_pw_aff_alloc(domain
, aff
);
7639 /* This function performs the same operation as isl_pw_aff_val_on_domain,
7640 * but is considered as a function on an isl_set when exported.
7642 __isl_give isl_pw_aff
*isl_set_pw_aff_on_domain_val(__isl_take isl_set
*domain
,
7643 __isl_take isl_val
*v
)
7645 return isl_pw_aff_val_on_domain(domain
, v
);
7648 /* Return a piecewise affine expression that is equal to the parameter
7649 * with identifier "id" on "domain".
7651 __isl_give isl_pw_aff
*isl_pw_aff_param_on_domain_id(
7652 __isl_take isl_set
*domain
, __isl_take isl_id
*id
)
7657 space
= isl_set_get_space(domain
);
7658 space
= isl_space_add_param_id(space
, isl_id_copy(id
));
7659 domain
= isl_set_align_params(domain
, isl_space_copy(space
));
7660 aff
= isl_aff_param_on_domain_space_id(space
, id
);
7662 return isl_pw_aff_alloc(domain
, aff
);
7665 /* This function performs the same operation as
7666 * isl_pw_aff_param_on_domain_id,
7667 * but is considered as a function on an isl_set when exported.
7669 __isl_give isl_pw_aff
*isl_set_param_pw_aff_on_domain_id(
7670 __isl_take isl_set
*domain
, __isl_take isl_id
*id
)
7672 return isl_pw_aff_param_on_domain_id(domain
, id
);
7675 /* Return a multi affine expression that is equal to "mv" on domain
7678 __isl_give isl_multi_aff
*isl_multi_aff_multi_val_on_domain_space(
7679 __isl_take isl_space
*space
, __isl_take isl_multi_val
*mv
)
7684 isl_local_space
*ls
;
7687 n
= isl_multi_val_dim(mv
, isl_dim_set
);
7688 if (!space
|| n
< 0)
7691 space2
= isl_multi_val_get_space(mv
);
7692 space2
= isl_space_align_params(space2
, isl_space_copy(space
));
7693 space
= isl_space_align_params(space
, isl_space_copy(space2
));
7694 space
= isl_space_map_from_domain_and_range(space
, space2
);
7695 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
7696 ls
= isl_local_space_from_space(isl_space_domain(space
));
7697 for (i
= 0; i
< n
; ++i
) {
7701 v
= isl_multi_val_get_val(mv
, i
);
7702 aff
= isl_aff_val_on_domain(isl_local_space_copy(ls
), v
);
7703 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
7705 isl_local_space_free(ls
);
7707 isl_multi_val_free(mv
);
7710 isl_space_free(space
);
7711 isl_multi_val_free(mv
);
7715 /* This is an alternative name for the function above.
7717 __isl_give isl_multi_aff
*isl_multi_aff_multi_val_on_space(
7718 __isl_take isl_space
*space
, __isl_take isl_multi_val
*mv
)
7720 return isl_multi_aff_multi_val_on_domain_space(space
, mv
);
7723 /* This function performs the same operation as
7724 * isl_multi_aff_multi_val_on_domain_space,
7725 * but is considered as a function on an isl_space when exported.
7727 __isl_give isl_multi_aff
*isl_space_multi_aff_on_domain_multi_val(
7728 __isl_take isl_space
*space
, __isl_take isl_multi_val
*mv
)
7730 return isl_multi_aff_multi_val_on_domain_space(space
, mv
);
7733 /* Return a piecewise multi-affine expression
7734 * that is equal to "mv" on "domain".
7736 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_multi_val_on_domain(
7737 __isl_take isl_set
*domain
, __isl_take isl_multi_val
*mv
)
7742 space
= isl_set_get_space(domain
);
7743 ma
= isl_multi_aff_multi_val_on_space(space
, mv
);
7745 return isl_pw_multi_aff_alloc(domain
, ma
);
7748 /* This function performs the same operation as
7749 * isl_pw_multi_aff_multi_val_on_domain,
7750 * but is considered as a function on an isl_set when exported.
7752 __isl_give isl_pw_multi_aff
*isl_set_pw_multi_aff_on_domain_multi_val(
7753 __isl_take isl_set
*domain
, __isl_take isl_multi_val
*mv
)
7755 return isl_pw_multi_aff_multi_val_on_domain(domain
, mv
);
7758 /* Internal data structure for isl_union_pw_multi_aff_multi_val_on_domain.
7759 * mv is the value that should be attained on each domain set
7760 * res collects the results
7762 struct isl_union_pw_multi_aff_multi_val_on_domain_data
{
7764 isl_union_pw_multi_aff
*res
;
7767 /* Create an isl_pw_multi_aff equal to data->mv on "domain"
7768 * and add it to data->res.
7770 static isl_stat
pw_multi_aff_multi_val_on_domain(__isl_take isl_set
*domain
,
7773 struct isl_union_pw_multi_aff_multi_val_on_domain_data
*data
= user
;
7774 isl_pw_multi_aff
*pma
;
7777 mv
= isl_multi_val_copy(data
->mv
);
7778 pma
= isl_pw_multi_aff_multi_val_on_domain(domain
, mv
);
7779 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma
);
7781 return data
->res
? isl_stat_ok
: isl_stat_error
;
7784 /* Return a union piecewise multi-affine expression
7785 * that is equal to "mv" on "domain".
7787 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_multi_val_on_domain(
7788 __isl_take isl_union_set
*domain
, __isl_take isl_multi_val
*mv
)
7790 struct isl_union_pw_multi_aff_multi_val_on_domain_data data
;
7793 space
= isl_union_set_get_space(domain
);
7794 data
.res
= isl_union_pw_multi_aff_empty(space
);
7796 if (isl_union_set_foreach_set(domain
,
7797 &pw_multi_aff_multi_val_on_domain
, &data
) < 0)
7798 data
.res
= isl_union_pw_multi_aff_free(data
.res
);
7799 isl_union_set_free(domain
);
7800 isl_multi_val_free(mv
);
7804 /* Compute the pullback of data->pma by the function represented by "pma2",
7805 * provided the spaces match, and add the results to data->res.
7807 static isl_stat
pullback_entry(__isl_take isl_pw_multi_aff
*pma2
, void *user
)
7809 struct isl_union_pw_multi_aff_bin_data
*data
= user
;
7811 if (!isl_space_tuple_is_equal(data
->pma
->dim
, isl_dim_in
,
7812 pma2
->dim
, isl_dim_out
)) {
7813 isl_pw_multi_aff_free(pma2
);
7817 pma2
= isl_pw_multi_aff_pullback_pw_multi_aff(
7818 isl_pw_multi_aff_copy(data
->pma
), pma2
);
7820 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma2
);
7822 return isl_stat_error
;
7827 /* Compute the pullback of "upma1" by the function represented by "upma2".
7829 __isl_give isl_union_pw_multi_aff
*
7830 isl_union_pw_multi_aff_pullback_union_pw_multi_aff(
7831 __isl_take isl_union_pw_multi_aff
*upma1
,
7832 __isl_take isl_union_pw_multi_aff
*upma2
)
7834 return bin_op(upma1
, upma2
, &pullback_entry
);
7837 /* Apply "upma2" to "upma1".
7839 * That is, compute the pullback of "upma2" by "upma1".
7841 __isl_give isl_union_pw_multi_aff
*
7842 isl_union_pw_multi_aff_apply_union_pw_multi_aff(
7843 __isl_take isl_union_pw_multi_aff
*upma1
,
7844 __isl_take isl_union_pw_multi_aff
*upma2
)
7846 return isl_union_pw_multi_aff_pullback_union_pw_multi_aff(upma2
, upma1
);
7850 #define TYPE isl_pw_multi_aff
7852 #include "isl_copy_tuple_id_templ.c"
7854 /* Given a function "pma1" of the form A[B -> C] -> D and
7855 * a function "pma2" of the form E -> B,
7856 * replace the domain of the wrapped relation inside the domain of "pma1"
7857 * by the preimage with respect to "pma2".
7858 * In other words, plug in "pma2" in this nested domain.
7859 * The result is of the form A[E -> C] -> D.
7861 * In particular, extend E -> B to A[E -> C] -> A[B -> C] and
7862 * plug that into "pma1".
7864 __isl_give isl_pw_multi_aff
*
7865 isl_pw_multi_aff_preimage_domain_wrapped_domain_pw_multi_aff(
7866 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
7868 isl_space
*pma1_space
, *pma2_space
;
7870 isl_pw_multi_aff
*id
;
7872 pma1_space
= isl_pw_multi_aff_peek_space(pma1
);
7873 pma2_space
= isl_pw_multi_aff_peek_space(pma2
);
7875 if (isl_space_check_domain_is_wrapping(pma1_space
) < 0)
7877 if (isl_space_check_wrapped_tuple_is_equal(pma1_space
,
7878 isl_dim_in
, isl_dim_in
, pma2_space
, isl_dim_out
) < 0)
7881 space
= isl_space_domain(isl_space_copy(pma1_space
));
7882 space
= isl_space_range(isl_space_unwrap(space
));
7883 id
= isl_pw_multi_aff_identity_on_domain_space(space
);
7884 pma2
= isl_pw_multi_aff_product(pma2
, id
);
7886 pma2
= isl_pw_multi_aff_copy_tuple_id(pma2
, isl_dim_in
,
7887 pma1_space
, isl_dim_in
);
7888 pma2
= isl_pw_multi_aff_copy_tuple_id(pma2
, isl_dim_out
,
7889 pma1_space
, isl_dim_in
);
7891 return isl_pw_multi_aff_pullback_pw_multi_aff(pma1
, pma2
);
7893 isl_pw_multi_aff_free(pma1
);
7894 isl_pw_multi_aff_free(pma2
);
7898 /* If data->pma and "pma2" are such that
7899 * data->pma is of the form A[B -> C] -> D and
7900 * "pma2" is of the form E -> B,
7901 * then replace the domain of the wrapped relation
7902 * inside the domain of data->pma by the preimage with respect to "pma2" and
7903 * add the result to data->res.
7905 static isl_stat
preimage_domain_wrapped_domain_entry(
7906 __isl_take isl_pw_multi_aff
*pma2
, void *user
)
7908 struct isl_union_pw_multi_aff_bin_data
*data
= user
;
7909 isl_space
*pma1_space
, *pma2_space
;
7912 pma1_space
= isl_pw_multi_aff_peek_space(data
->pma
);
7913 pma2_space
= isl_pw_multi_aff_peek_space(pma2
);
7915 match
= isl_space_domain_is_wrapping(pma1_space
);
7916 if (match
>= 0 && match
)
7917 match
= isl_space_wrapped_tuple_is_equal(pma1_space
, isl_dim_in
,
7918 isl_dim_in
, pma2_space
, isl_dim_out
);
7919 if (match
< 0 || !match
) {
7920 isl_pw_multi_aff_free(pma2
);
7921 return match
< 0 ? isl_stat_error
: isl_stat_ok
;
7924 pma2
= isl_pw_multi_aff_preimage_domain_wrapped_domain_pw_multi_aff(
7925 isl_pw_multi_aff_copy(data
->pma
), pma2
);
7927 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma2
);
7929 return isl_stat_non_null(data
->res
);
7932 /* For each pair of functions A[B -> C] -> D in "upma1" and
7933 * E -> B in "upma2",
7934 * replace the domain of the wrapped relation inside the domain of the first
7935 * by the preimage with respect to the second and collect the results.
7936 * In other words, plug in the second function in this nested domain.
7937 * The results are of the form A[E -> C] -> D.
7939 __isl_give isl_union_pw_multi_aff
*
7940 isl_union_pw_multi_aff_preimage_domain_wrapped_domain_union_pw_multi_aff(
7941 __isl_take isl_union_pw_multi_aff
*upma1
,
7942 __isl_take isl_union_pw_multi_aff
*upma2
)
7944 return bin_op(upma1
, upma2
, &preimage_domain_wrapped_domain_entry
);
7947 /* Check that the domain space of "upa" matches "space".
7949 * This function is called from isl_multi_union_pw_aff_set_union_pw_aff and
7950 * can in principle never fail since the space "space" is that
7951 * of the isl_multi_union_pw_aff and is a set space such that
7952 * there is no domain space to match.
7954 * We check the parameters and double-check that "space" is
7955 * indeed that of a set.
7957 static isl_stat
isl_union_pw_aff_check_match_domain_space(
7958 __isl_keep isl_union_pw_aff
*upa
, __isl_keep isl_space
*space
)
7960 isl_space
*upa_space
;
7964 return isl_stat_error
;
7966 match
= isl_space_is_set(space
);
7968 return isl_stat_error
;
7970 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
7971 "expecting set space", return isl_stat_error
);
7973 upa_space
= isl_union_pw_aff_get_space(upa
);
7974 match
= isl_space_has_equal_params(space
, upa_space
);
7978 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
7979 "parameters don't match", goto error
);
7981 isl_space_free(upa_space
);
7984 isl_space_free(upa_space
);
7985 return isl_stat_error
;
7988 /* Do the parameters of "upa" match those of "space"?
7990 static isl_bool
isl_union_pw_aff_matching_params(
7991 __isl_keep isl_union_pw_aff
*upa
, __isl_keep isl_space
*space
)
7993 isl_space
*upa_space
;
7997 return isl_bool_error
;
7999 upa_space
= isl_union_pw_aff_get_space(upa
);
8001 match
= isl_space_has_equal_params(space
, upa_space
);
8003 isl_space_free(upa_space
);
8007 /* Internal data structure for isl_union_pw_aff_reset_domain_space.
8008 * space represents the new parameters.
8009 * res collects the results.
8011 struct isl_union_pw_aff_reset_params_data
{
8013 isl_union_pw_aff
*res
;
8016 /* Replace the parameters of "pa" by data->space and
8017 * add the result to data->res.
8019 static isl_stat
reset_params(__isl_take isl_pw_aff
*pa
, void *user
)
8021 struct isl_union_pw_aff_reset_params_data
*data
= user
;
8024 space
= isl_pw_aff_get_space(pa
);
8025 space
= isl_space_replace_params(space
, data
->space
);
8026 pa
= isl_pw_aff_reset_space(pa
, space
);
8027 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
8029 return data
->res
? isl_stat_ok
: isl_stat_error
;
8032 /* Replace the domain space of "upa" by "space".
8033 * Since a union expression does not have a (single) domain space,
8034 * "space" is necessarily a parameter space.
8036 * Since the order and the names of the parameters determine
8037 * the hash value, we need to create a new hash table.
8039 static __isl_give isl_union_pw_aff
*isl_union_pw_aff_reset_domain_space(
8040 __isl_take isl_union_pw_aff
*upa
, __isl_take isl_space
*space
)
8042 struct isl_union_pw_aff_reset_params_data data
= { space
};
8045 match
= isl_union_pw_aff_matching_params(upa
, space
);
8047 upa
= isl_union_pw_aff_free(upa
);
8049 isl_space_free(space
);
8053 data
.res
= isl_union_pw_aff_empty(isl_space_copy(space
));
8054 if (isl_union_pw_aff_foreach_pw_aff(upa
, &reset_params
, &data
) < 0)
8055 data
.res
= isl_union_pw_aff_free(data
.res
);
8057 isl_union_pw_aff_free(upa
);
8058 isl_space_free(space
);
8062 /* Return the floor of "pa".
8064 static __isl_give isl_pw_aff
*floor_entry(__isl_take isl_pw_aff
*pa
, void *user
)
8066 return isl_pw_aff_floor(pa
);
8069 /* Given f, return floor(f).
8071 __isl_give isl_union_pw_aff
*isl_union_pw_aff_floor(
8072 __isl_take isl_union_pw_aff
*upa
)
8074 return isl_union_pw_aff_transform_inplace(upa
, &floor_entry
, NULL
);
8079 * upa mod m = upa - m * floor(upa/m)
8081 * with m an integer value.
8083 __isl_give isl_union_pw_aff
*isl_union_pw_aff_mod_val(
8084 __isl_take isl_union_pw_aff
*upa
, __isl_take isl_val
*m
)
8086 isl_union_pw_aff
*res
;
8091 if (!isl_val_is_int(m
))
8092 isl_die(isl_val_get_ctx(m
), isl_error_invalid
,
8093 "expecting integer modulo", goto error
);
8094 if (!isl_val_is_pos(m
))
8095 isl_die(isl_val_get_ctx(m
), isl_error_invalid
,
8096 "expecting positive modulo", goto error
);
8098 res
= isl_union_pw_aff_copy(upa
);
8099 upa
= isl_union_pw_aff_scale_down_val(upa
, isl_val_copy(m
));
8100 upa
= isl_union_pw_aff_floor(upa
);
8101 upa
= isl_union_pw_aff_scale_val(upa
, m
);
8102 res
= isl_union_pw_aff_sub(res
, upa
);
8107 isl_union_pw_aff_free(upa
);
8111 /* Internal data structure for isl_union_pw_multi_aff_get_union_pw_aff.
8112 * pos is the output position that needs to be extracted.
8113 * res collects the results.
8115 struct isl_union_pw_multi_aff_get_union_pw_aff_data
{
8117 isl_union_pw_aff
*res
;
8120 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma"
8121 * (assuming it has such a dimension) and add it to data->res.
8123 static isl_stat
get_union_pw_aff(__isl_take isl_pw_multi_aff
*pma
, void *user
)
8125 struct isl_union_pw_multi_aff_get_union_pw_aff_data
*data
= user
;
8129 n_out
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
8131 return isl_stat_error
;
8132 if (data
->pos
>= n_out
) {
8133 isl_pw_multi_aff_free(pma
);
8137 pa
= isl_pw_multi_aff_get_pw_aff(pma
, data
->pos
);
8138 isl_pw_multi_aff_free(pma
);
8140 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
8142 return data
->res
? isl_stat_ok
: isl_stat_error
;
8145 /* Extract an isl_union_pw_aff corresponding to
8146 * output dimension "pos" of "upma".
8148 __isl_give isl_union_pw_aff
*isl_union_pw_multi_aff_get_union_pw_aff(
8149 __isl_keep isl_union_pw_multi_aff
*upma
, int pos
)
8151 struct isl_union_pw_multi_aff_get_union_pw_aff_data data
;
8158 isl_die(isl_union_pw_multi_aff_get_ctx(upma
), isl_error_invalid
,
8159 "cannot extract at negative position", return NULL
);
8161 space
= isl_union_pw_multi_aff_get_space(upma
);
8162 data
.res
= isl_union_pw_aff_empty(space
);
8164 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma
,
8165 &get_union_pw_aff
, &data
) < 0)
8166 data
.res
= isl_union_pw_aff_free(data
.res
);
8171 /* Return a union piecewise affine expression
8172 * that is equal to "aff" on "domain".
8174 __isl_give isl_union_pw_aff
*isl_union_pw_aff_aff_on_domain(
8175 __isl_take isl_union_set
*domain
, __isl_take isl_aff
*aff
)
8179 pa
= isl_pw_aff_from_aff(aff
);
8180 return isl_union_pw_aff_pw_aff_on_domain(domain
, pa
);
8183 /* Return a union piecewise affine expression
8184 * that is equal to the parameter identified by "id" on "domain".
8186 * Make sure the parameter appears in the space passed to
8187 * isl_aff_param_on_domain_space_id.
8189 __isl_give isl_union_pw_aff
*isl_union_pw_aff_param_on_domain_id(
8190 __isl_take isl_union_set
*domain
, __isl_take isl_id
*id
)
8195 space
= isl_union_set_get_space(domain
);
8196 space
= isl_space_add_param_id(space
, isl_id_copy(id
));
8197 aff
= isl_aff_param_on_domain_space_id(space
, id
);
8198 return isl_union_pw_aff_aff_on_domain(domain
, aff
);
8201 /* Internal data structure for isl_union_pw_aff_pw_aff_on_domain.
8202 * "pa" is the piecewise symbolic value that the resulting isl_union_pw_aff
8204 * "res" collects the results.
8206 struct isl_union_pw_aff_pw_aff_on_domain_data
{
8208 isl_union_pw_aff
*res
;
8211 /* Construct a piecewise affine expression that is equal to data->pa
8212 * on "domain" and add the result to data->res.
8214 static isl_stat
pw_aff_on_domain(__isl_take isl_set
*domain
, void *user
)
8216 struct isl_union_pw_aff_pw_aff_on_domain_data
*data
= user
;
8220 pa
= isl_pw_aff_copy(data
->pa
);
8221 dim
= isl_set_dim(domain
, isl_dim_set
);
8223 pa
= isl_pw_aff_free(pa
);
8224 pa
= isl_pw_aff_from_range(pa
);
8225 pa
= isl_pw_aff_add_dims(pa
, isl_dim_in
, dim
);
8226 pa
= isl_pw_aff_reset_domain_space(pa
, isl_set_get_space(domain
));
8227 pa
= isl_pw_aff_intersect_domain(pa
, domain
);
8228 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
8230 return data
->res
? isl_stat_ok
: isl_stat_error
;
8233 /* Return a union piecewise affine expression
8234 * that is equal to "pa" on "domain", assuming "domain" and "pa"
8235 * have been aligned.
8237 * Construct an isl_pw_aff on each of the sets in "domain" and
8238 * collect the results.
8240 static __isl_give isl_union_pw_aff
*isl_union_pw_aff_pw_aff_on_domain_aligned(
8241 __isl_take isl_union_set
*domain
, __isl_take isl_pw_aff
*pa
)
8243 struct isl_union_pw_aff_pw_aff_on_domain_data data
;
8246 space
= isl_union_set_get_space(domain
);
8247 data
.res
= isl_union_pw_aff_empty(space
);
8249 if (isl_union_set_foreach_set(domain
, &pw_aff_on_domain
, &data
) < 0)
8250 data
.res
= isl_union_pw_aff_free(data
.res
);
8251 isl_union_set_free(domain
);
8252 isl_pw_aff_free(pa
);
8256 /* Return a union piecewise affine expression
8257 * that is equal to "pa" on "domain".
8259 * Check that "pa" is a parametric expression,
8260 * align the parameters if needed and call
8261 * isl_union_pw_aff_pw_aff_on_domain_aligned.
8263 __isl_give isl_union_pw_aff
*isl_union_pw_aff_pw_aff_on_domain(
8264 __isl_take isl_union_set
*domain
, __isl_take isl_pw_aff
*pa
)
8267 isl_bool equal_params
;
8268 isl_space
*domain_space
, *pa_space
;
8270 pa_space
= isl_pw_aff_peek_space(pa
);
8271 is_set
= isl_space_is_set(pa_space
);
8275 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
8276 "expecting parametric expression", goto error
);
8278 domain_space
= isl_union_set_get_space(domain
);
8279 pa_space
= isl_pw_aff_get_space(pa
);
8280 equal_params
= isl_space_has_equal_params(domain_space
, pa_space
);
8281 if (equal_params
>= 0 && !equal_params
) {
8284 space
= isl_space_align_params(domain_space
, pa_space
);
8285 pa
= isl_pw_aff_align_params(pa
, isl_space_copy(space
));
8286 domain
= isl_union_set_align_params(domain
, space
);
8288 isl_space_free(domain_space
);
8289 isl_space_free(pa_space
);
8292 if (equal_params
< 0)
8294 return isl_union_pw_aff_pw_aff_on_domain_aligned(domain
, pa
);
8296 isl_union_set_free(domain
);
8297 isl_pw_aff_free(pa
);
8301 /* Internal data structure for isl_union_pw_aff_val_on_domain.
8302 * "v" is the value that the resulting isl_union_pw_aff needs to attain.
8303 * "res" collects the results.
8305 struct isl_union_pw_aff_val_on_domain_data
{
8307 isl_union_pw_aff
*res
;
8310 /* Construct a piecewise affine expression that is equal to data->v
8311 * on "domain" and add the result to data->res.
8313 static isl_stat
pw_aff_val_on_domain(__isl_take isl_set
*domain
, void *user
)
8315 struct isl_union_pw_aff_val_on_domain_data
*data
= user
;
8319 v
= isl_val_copy(data
->v
);
8320 pa
= isl_pw_aff_val_on_domain(domain
, v
);
8321 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
8323 return data
->res
? isl_stat_ok
: isl_stat_error
;
8326 /* Return a union piecewise affine expression
8327 * that is equal to "v" on "domain".
8329 * Construct an isl_pw_aff on each of the sets in "domain" and
8330 * collect the results.
8332 __isl_give isl_union_pw_aff
*isl_union_pw_aff_val_on_domain(
8333 __isl_take isl_union_set
*domain
, __isl_take isl_val
*v
)
8335 struct isl_union_pw_aff_val_on_domain_data data
;
8338 space
= isl_union_set_get_space(domain
);
8339 data
.res
= isl_union_pw_aff_empty(space
);
8341 if (isl_union_set_foreach_set(domain
, &pw_aff_val_on_domain
, &data
) < 0)
8342 data
.res
= isl_union_pw_aff_free(data
.res
);
8343 isl_union_set_free(domain
);
8348 /* Construct a piecewise multi affine expression
8349 * that is equal to "pa" and add it to upma.
8351 static isl_stat
pw_multi_aff_from_pw_aff_entry(__isl_take isl_pw_aff
*pa
,
8354 isl_union_pw_multi_aff
**upma
= user
;
8355 isl_pw_multi_aff
*pma
;
8357 pma
= isl_pw_multi_aff_from_pw_aff(pa
);
8358 *upma
= isl_union_pw_multi_aff_add_pw_multi_aff(*upma
, pma
);
8360 return *upma
? isl_stat_ok
: isl_stat_error
;
8363 /* Construct and return a union piecewise multi affine expression
8364 * that is equal to the given union piecewise affine expression.
8366 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_union_pw_aff(
8367 __isl_take isl_union_pw_aff
*upa
)
8370 isl_union_pw_multi_aff
*upma
;
8375 space
= isl_union_pw_aff_get_space(upa
);
8376 upma
= isl_union_pw_multi_aff_empty(space
);
8378 if (isl_union_pw_aff_foreach_pw_aff(upa
,
8379 &pw_multi_aff_from_pw_aff_entry
, &upma
) < 0)
8380 upma
= isl_union_pw_multi_aff_free(upma
);
8382 isl_union_pw_aff_free(upa
);
8386 /* Compute the set of elements in the domain of "pa" where it is zero and
8387 * add this set to "uset".
8389 static isl_stat
zero_union_set(__isl_take isl_pw_aff
*pa
, void *user
)
8391 isl_union_set
**uset
= (isl_union_set
**)user
;
8393 *uset
= isl_union_set_add_set(*uset
, isl_pw_aff_zero_set(pa
));
8395 return *uset
? isl_stat_ok
: isl_stat_error
;
8398 /* Return a union set containing those elements in the domain
8399 * of "upa" where it is zero.
8401 __isl_give isl_union_set
*isl_union_pw_aff_zero_union_set(
8402 __isl_take isl_union_pw_aff
*upa
)
8404 isl_union_set
*zero
;
8406 zero
= isl_union_set_empty(isl_union_pw_aff_get_space(upa
));
8407 if (isl_union_pw_aff_foreach_pw_aff(upa
, &zero_union_set
, &zero
) < 0)
8408 zero
= isl_union_set_free(zero
);
8410 isl_union_pw_aff_free(upa
);
8414 /* Internal data structure for isl_union_pw_aff_bind_id,
8415 * storing the parameter that needs to be bound and
8416 * the accumulated results.
8418 struct isl_bind_id_data
{
8420 isl_union_set
*bound
;
8423 /* Bind the piecewise affine function "pa" to the parameter data->id,
8424 * adding the resulting elements in the domain where the expression
8425 * is equal to the parameter to data->bound.
8427 static isl_stat
bind_id(__isl_take isl_pw_aff
*pa
, void *user
)
8429 struct isl_bind_id_data
*data
= user
;
8432 bound
= isl_pw_aff_bind_id(pa
, isl_id_copy(data
->id
));
8433 data
->bound
= isl_union_set_add_set(data
->bound
, bound
);
8435 return data
->bound
? isl_stat_ok
: isl_stat_error
;
8438 /* Bind the union piecewise affine function "upa" to the parameter "id",
8439 * returning the elements in the domain where the expression
8440 * is equal to the parameter.
8442 __isl_give isl_union_set
*isl_union_pw_aff_bind_id(
8443 __isl_take isl_union_pw_aff
*upa
, __isl_take isl_id
*id
)
8445 struct isl_bind_id_data data
= { id
};
8447 data
.bound
= isl_union_set_empty(isl_union_pw_aff_get_space(upa
));
8448 if (isl_union_pw_aff_foreach_pw_aff(upa
, &bind_id
, &data
) < 0)
8449 data
.bound
= isl_union_set_free(data
.bound
);
8451 isl_union_pw_aff_free(upa
);
8456 /* Internal data structure for isl_union_pw_aff_pullback_union_pw_multi_aff.
8457 * upma is the function that is plugged in.
8458 * pa is the current part of the function in which upma is plugged in.
8459 * res collects the results.
8461 struct isl_union_pw_aff_pullback_upma_data
{
8462 isl_union_pw_multi_aff
*upma
;
8464 isl_union_pw_aff
*res
;
8467 /* Check if "pma" can be plugged into data->pa.
8468 * If so, perform the pullback and add the result to data->res.
8470 static isl_stat
pa_pb_pma(__isl_take isl_pw_multi_aff
*pma
, void *user
)
8472 struct isl_union_pw_aff_pullback_upma_data
*data
= user
;
8475 if (!isl_space_tuple_is_equal(data
->pa
->dim
, isl_dim_in
,
8476 pma
->dim
, isl_dim_out
)) {
8477 isl_pw_multi_aff_free(pma
);
8481 pa
= isl_pw_aff_copy(data
->pa
);
8482 pa
= isl_pw_aff_pullback_pw_multi_aff(pa
, pma
);
8484 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
8486 return data
->res
? isl_stat_ok
: isl_stat_error
;
8489 /* Check if any of the elements of data->upma can be plugged into pa,
8490 * add if so add the result to data->res.
8492 static isl_stat
upa_pb_upma(__isl_take isl_pw_aff
*pa
, void *user
)
8494 struct isl_union_pw_aff_pullback_upma_data
*data
= user
;
8498 r
= isl_union_pw_multi_aff_foreach_pw_multi_aff(data
->upma
,
8500 isl_pw_aff_free(pa
);
8505 /* Compute the pullback of "upa" by the function represented by "upma".
8506 * In other words, plug in "upma" in "upa". The result contains
8507 * expressions defined over the domain space of "upma".
8509 * Run over all pairs of elements in "upa" and "upma", perform
8510 * the pullback when appropriate and collect the results.
8511 * If the hash value were based on the domain space rather than
8512 * the function space, then we could run through all elements
8513 * of "upma" and directly pick out the corresponding element of "upa".
8515 __isl_give isl_union_pw_aff
*isl_union_pw_aff_pullback_union_pw_multi_aff(
8516 __isl_take isl_union_pw_aff
*upa
,
8517 __isl_take isl_union_pw_multi_aff
*upma
)
8519 struct isl_union_pw_aff_pullback_upma_data data
= { NULL
, NULL
};
8522 space
= isl_union_pw_multi_aff_get_space(upma
);
8523 upa
= isl_union_pw_aff_align_params(upa
, space
);
8524 space
= isl_union_pw_aff_get_space(upa
);
8525 upma
= isl_union_pw_multi_aff_align_params(upma
, space
);
8531 data
.res
= isl_union_pw_aff_alloc_same_size(upa
);
8532 if (isl_union_pw_aff_foreach_pw_aff(upa
, &upa_pb_upma
, &data
) < 0)
8533 data
.res
= isl_union_pw_aff_free(data
.res
);
8535 isl_union_pw_aff_free(upa
);
8536 isl_union_pw_multi_aff_free(upma
);
8539 isl_union_pw_aff_free(upa
);
8540 isl_union_pw_multi_aff_free(upma
);
8545 #define BASE union_pw_aff
8547 #define DOMBASE union_set
8549 #include <isl_multi_explicit_domain.c>
8550 #include <isl_multi_union_pw_aff_explicit_domain.c>
8551 #include <isl_multi_templ.c>
8552 #include <isl_multi_un_op_templ.c>
8553 #include <isl_multi_bin_val_templ.c>
8554 #include <isl_multi_apply_set.c>
8555 #include <isl_multi_apply_union_set.c>
8556 #include <isl_multi_arith_templ.c>
8557 #include <isl_multi_bind_templ.c>
8558 #include <isl_multi_coalesce.c>
8559 #include <isl_multi_dim_id_templ.c>
8560 #include <isl_multi_floor.c>
8561 #include <isl_multi_from_base_templ.c>
8562 #include <isl_multi_gist.c>
8563 #include <isl_multi_align_set.c>
8564 #include <isl_multi_align_union_set.c>
8565 #include <isl_multi_intersect.c>
8566 #include <isl_multi_nan_templ.c>
8567 #include <isl_multi_tuple_id_templ.c>
8568 #include <isl_multi_union_add_templ.c>
8569 #include <isl_multi_zero_space_templ.c>
8571 /* Does "mupa" have a non-trivial explicit domain?
8573 * The explicit domain, if present, is trivial if it represents
8574 * an (obviously) universe parameter set.
8576 isl_bool
isl_multi_union_pw_aff_has_non_trivial_domain(
8577 __isl_keep isl_multi_union_pw_aff
*mupa
)
8579 isl_bool is_params
, trivial
;
8583 return isl_bool_error
;
8584 if (!isl_multi_union_pw_aff_has_explicit_domain(mupa
))
8585 return isl_bool_false
;
8586 is_params
= isl_union_set_is_params(mupa
->u
.dom
);
8587 if (is_params
< 0 || !is_params
)
8588 return isl_bool_not(is_params
);
8589 set
= isl_set_from_union_set(isl_union_set_copy(mupa
->u
.dom
));
8590 trivial
= isl_set_plain_is_universe(set
);
8592 return isl_bool_not(trivial
);
8595 /* Construct a multiple union piecewise affine expression
8596 * in the given space with value zero in each of the output dimensions.
8598 * Since there is no canonical zero value for
8599 * a union piecewise affine expression, we can only construct
8600 * a zero-dimensional "zero" value.
8602 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_zero(
8603 __isl_take isl_space
*space
)
8611 params
= isl_space_is_params(space
);
8615 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
8616 "expecting proper set space", goto error
);
8617 if (!isl_space_is_set(space
))
8618 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
8619 "expecting set space", goto error
);
8620 dim
= isl_space_dim(space
, isl_dim_out
);
8624 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
8625 "expecting 0D space", goto error
);
8627 return isl_multi_union_pw_aff_alloc(space
);
8629 isl_space_free(space
);
8633 /* Construct and return a multi union piecewise affine expression
8634 * that is equal to the given multi affine expression.
8636 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_from_multi_aff(
8637 __isl_take isl_multi_aff
*ma
)
8639 isl_multi_pw_aff
*mpa
;
8641 mpa
= isl_multi_pw_aff_from_multi_aff(ma
);
8642 return isl_multi_union_pw_aff_from_multi_pw_aff(mpa
);
8645 /* This function performs the same operation as
8646 * isl_multi_union_pw_aff_from_multi_aff, but is considered as a function on an
8647 * isl_multi_aff when exported.
8649 __isl_give isl_multi_union_pw_aff
*isl_multi_aff_to_multi_union_pw_aff(
8650 __isl_take isl_multi_aff
*ma
)
8652 return isl_multi_union_pw_aff_from_multi_aff(ma
);
8655 /* Construct and return a multi union piecewise affine expression
8656 * that is equal to the given multi piecewise affine expression.
8658 * If the resulting multi union piecewise affine expression has
8659 * an explicit domain, then assign it the domain of the input.
8660 * In other cases, the domain is stored in the individual elements.
8662 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_from_multi_pw_aff(
8663 __isl_take isl_multi_pw_aff
*mpa
)
8668 isl_multi_union_pw_aff
*mupa
;
8670 n
= isl_multi_pw_aff_dim(mpa
, isl_dim_out
);
8672 mpa
= isl_multi_pw_aff_free(mpa
);
8676 space
= isl_multi_pw_aff_get_space(mpa
);
8677 space
= isl_space_range(space
);
8678 mupa
= isl_multi_union_pw_aff_alloc(space
);
8680 for (i
= 0; i
< n
; ++i
) {
8682 isl_union_pw_aff
*upa
;
8684 pa
= isl_multi_pw_aff_get_pw_aff(mpa
, i
);
8685 upa
= isl_union_pw_aff_from_pw_aff(pa
);
8686 mupa
= isl_multi_union_pw_aff_restore_check_space(mupa
, i
, upa
);
8688 if (isl_multi_union_pw_aff_has_explicit_domain(mupa
)) {
8690 isl_multi_pw_aff
*copy
;
8692 copy
= isl_multi_pw_aff_copy(mpa
);
8693 dom
= isl_union_set_from_set(isl_multi_pw_aff_domain(copy
));
8694 mupa
= isl_multi_union_pw_aff_intersect_domain(mupa
, dom
);
8697 isl_multi_pw_aff_free(mpa
);
8702 /* Extract the range space of "pma" and assign it to *space.
8703 * If *space has already been set (through a previous call to this function),
8704 * then check that the range space is the same.
8706 static isl_stat
extract_space(__isl_take isl_pw_multi_aff
*pma
, void *user
)
8708 isl_space
**space
= user
;
8709 isl_space
*pma_space
;
8712 pma_space
= isl_space_range(isl_pw_multi_aff_get_space(pma
));
8713 isl_pw_multi_aff_free(pma
);
8716 return isl_stat_error
;
8722 equal
= isl_space_is_equal(pma_space
, *space
);
8723 isl_space_free(pma_space
);
8726 return isl_stat_error
;
8728 isl_die(isl_space_get_ctx(*space
), isl_error_invalid
,
8729 "range spaces not the same", return isl_stat_error
);
8733 /* Construct and return a multi union piecewise affine expression
8734 * that is equal to the given union piecewise multi affine expression.
8736 * In order to be able to perform the conversion, the input
8737 * needs to be non-empty and may only involve a single range space.
8739 * If the resulting multi union piecewise affine expression has
8740 * an explicit domain, then assign it the domain of the input.
8741 * In other cases, the domain is stored in the individual elements.
8743 __isl_give isl_multi_union_pw_aff
*
8744 isl_multi_union_pw_aff_from_union_pw_multi_aff(
8745 __isl_take isl_union_pw_multi_aff
*upma
)
8747 isl_space
*space
= NULL
;
8748 isl_multi_union_pw_aff
*mupa
;
8752 n
= isl_union_pw_multi_aff_n_pw_multi_aff(upma
);
8756 isl_die(isl_union_pw_multi_aff_get_ctx(upma
), isl_error_invalid
,
8757 "cannot extract range space from empty input",
8759 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma
, &extract_space
,
8766 n
= isl_space_dim(space
, isl_dim_set
);
8768 space
= isl_space_free(space
);
8769 mupa
= isl_multi_union_pw_aff_alloc(space
);
8771 for (i
= 0; i
< n
; ++i
) {
8772 isl_union_pw_aff
*upa
;
8774 upa
= isl_union_pw_multi_aff_get_union_pw_aff(upma
, i
);
8775 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
8777 if (isl_multi_union_pw_aff_has_explicit_domain(mupa
)) {
8779 isl_union_pw_multi_aff
*copy
;
8781 copy
= isl_union_pw_multi_aff_copy(upma
);
8782 dom
= isl_union_pw_multi_aff_domain(copy
);
8783 mupa
= isl_multi_union_pw_aff_intersect_domain(mupa
, dom
);
8786 isl_union_pw_multi_aff_free(upma
);
8789 isl_space_free(space
);
8790 isl_union_pw_multi_aff_free(upma
);
8794 /* This function performs the same operation as
8795 * isl_multi_union_pw_aff_from_union_pw_multi_aff,
8796 * but is considered as a function on an isl_union_pw_multi_aff when exported.
8798 __isl_give isl_multi_union_pw_aff
*
8799 isl_union_pw_multi_aff_as_multi_union_pw_aff(
8800 __isl_take isl_union_pw_multi_aff
*upma
)
8802 return isl_multi_union_pw_aff_from_union_pw_multi_aff(upma
);
8805 /* Try and create an isl_multi_union_pw_aff that is equivalent
8806 * to the given isl_union_map.
8807 * The isl_union_map is required to be single-valued in each space.
8808 * Moreover, it cannot be empty and all range spaces need to be the same.
8809 * Otherwise, an error is produced.
8811 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_from_union_map(
8812 __isl_take isl_union_map
*umap
)
8814 isl_union_pw_multi_aff
*upma
;
8816 upma
= isl_union_pw_multi_aff_from_union_map(umap
);
8817 return isl_multi_union_pw_aff_from_union_pw_multi_aff(upma
);
8820 /* This function performs the same operation as
8821 * isl_multi_union_pw_aff_from_union_map,
8822 * but is considered as a function on an isl_union_map when exported.
8824 __isl_give isl_multi_union_pw_aff
*isl_union_map_as_multi_union_pw_aff(
8825 __isl_take isl_union_map
*umap
)
8827 return isl_multi_union_pw_aff_from_union_map(umap
);
8830 /* Return a multiple union piecewise affine expression
8831 * that is equal to "mv" on "domain", assuming "domain" and "mv"
8832 * have been aligned.
8834 * If the resulting multi union piecewise affine expression has
8835 * an explicit domain, then assign it the input domain.
8836 * In other cases, the domain is stored in the individual elements.
8838 static __isl_give isl_multi_union_pw_aff
*
8839 isl_multi_union_pw_aff_multi_val_on_domain_aligned(
8840 __isl_take isl_union_set
*domain
, __isl_take isl_multi_val
*mv
)
8845 isl_multi_union_pw_aff
*mupa
;
8847 n
= isl_multi_val_dim(mv
, isl_dim_set
);
8848 if (!domain
|| n
< 0)
8851 space
= isl_multi_val_get_space(mv
);
8852 mupa
= isl_multi_union_pw_aff_alloc(space
);
8853 for (i
= 0; i
< n
; ++i
) {
8855 isl_union_pw_aff
*upa
;
8857 v
= isl_multi_val_get_val(mv
, i
);
8858 upa
= isl_union_pw_aff_val_on_domain(isl_union_set_copy(domain
),
8860 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
8862 if (isl_multi_union_pw_aff_has_explicit_domain(mupa
))
8863 mupa
= isl_multi_union_pw_aff_intersect_domain(mupa
,
8864 isl_union_set_copy(domain
));
8866 isl_union_set_free(domain
);
8867 isl_multi_val_free(mv
);
8870 isl_union_set_free(domain
);
8871 isl_multi_val_free(mv
);
8875 /* Return a multiple union piecewise affine expression
8876 * that is equal to "mv" on "domain".
8878 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_multi_val_on_domain(
8879 __isl_take isl_union_set
*domain
, __isl_take isl_multi_val
*mv
)
8881 isl_bool equal_params
;
8885 equal_params
= isl_space_has_equal_params(domain
->dim
, mv
->space
);
8886 if (equal_params
< 0)
8889 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(
8891 domain
= isl_union_set_align_params(domain
,
8892 isl_multi_val_get_space(mv
));
8893 mv
= isl_multi_val_align_params(mv
, isl_union_set_get_space(domain
));
8894 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(domain
, mv
);
8896 isl_union_set_free(domain
);
8897 isl_multi_val_free(mv
);
8901 /* Return a multiple union piecewise affine expression
8902 * that is equal to "ma" on "domain".
8904 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_multi_aff_on_domain(
8905 __isl_take isl_union_set
*domain
, __isl_take isl_multi_aff
*ma
)
8907 isl_pw_multi_aff
*pma
;
8909 pma
= isl_pw_multi_aff_from_multi_aff(ma
);
8910 return isl_multi_union_pw_aff_pw_multi_aff_on_domain(domain
, pma
);
8913 /* Return a multiple union piecewise affine expression
8914 * that is equal to "pma" on "domain", assuming "domain" and "pma"
8915 * have been aligned.
8917 * If the resulting multi union piecewise affine expression has
8918 * an explicit domain, then assign it the input domain.
8919 * In other cases, the domain is stored in the individual elements.
8921 static __isl_give isl_multi_union_pw_aff
*
8922 isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(
8923 __isl_take isl_union_set
*domain
, __isl_take isl_pw_multi_aff
*pma
)
8928 isl_multi_union_pw_aff
*mupa
;
8930 n
= isl_pw_multi_aff_dim(pma
, isl_dim_set
);
8931 if (!domain
|| n
< 0)
8933 space
= isl_pw_multi_aff_get_space(pma
);
8934 mupa
= isl_multi_union_pw_aff_alloc(space
);
8935 for (i
= 0; i
< n
; ++i
) {
8937 isl_union_pw_aff
*upa
;
8939 pa
= isl_pw_multi_aff_get_pw_aff(pma
, i
);
8940 upa
= isl_union_pw_aff_pw_aff_on_domain(
8941 isl_union_set_copy(domain
), pa
);
8942 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
8944 if (isl_multi_union_pw_aff_has_explicit_domain(mupa
))
8945 mupa
= isl_multi_union_pw_aff_intersect_domain(mupa
,
8946 isl_union_set_copy(domain
));
8948 isl_union_set_free(domain
);
8949 isl_pw_multi_aff_free(pma
);
8952 isl_union_set_free(domain
);
8953 isl_pw_multi_aff_free(pma
);
8957 /* Return a multiple union piecewise affine expression
8958 * that is equal to "pma" on "domain".
8960 __isl_give isl_multi_union_pw_aff
*
8961 isl_multi_union_pw_aff_pw_multi_aff_on_domain(__isl_take isl_union_set
*domain
,
8962 __isl_take isl_pw_multi_aff
*pma
)
8964 isl_bool equal_params
;
8967 space
= isl_pw_multi_aff_peek_space(pma
);
8968 equal_params
= isl_union_set_space_has_equal_params(domain
, space
);
8969 if (equal_params
< 0)
8972 return isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(
8974 domain
= isl_union_set_align_params(domain
,
8975 isl_pw_multi_aff_get_space(pma
));
8976 pma
= isl_pw_multi_aff_align_params(pma
,
8977 isl_union_set_get_space(domain
));
8978 return isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(domain
,
8981 isl_union_set_free(domain
);
8982 isl_pw_multi_aff_free(pma
);
8986 /* Return a union set containing those elements in the domains
8987 * of the elements of "mupa" where they are all zero.
8989 * If there are no elements, then simply return the entire domain.
8991 __isl_give isl_union_set
*isl_multi_union_pw_aff_zero_union_set(
8992 __isl_take isl_multi_union_pw_aff
*mupa
)
8996 isl_union_pw_aff
*upa
;
8997 isl_union_set
*zero
;
8999 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
9001 mupa
= isl_multi_union_pw_aff_free(mupa
);
9006 return isl_multi_union_pw_aff_domain(mupa
);
9008 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, 0);
9009 zero
= isl_union_pw_aff_zero_union_set(upa
);
9011 for (i
= 1; i
< n
; ++i
) {
9012 isl_union_set
*zero_i
;
9014 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
9015 zero_i
= isl_union_pw_aff_zero_union_set(upa
);
9017 zero
= isl_union_set_intersect(zero
, zero_i
);
9020 isl_multi_union_pw_aff_free(mupa
);
9024 /* Construct a union map mapping the shared domain
9025 * of the union piecewise affine expressions to the range of "mupa"
9026 * in the special case of a 0D multi union piecewise affine expression.
9028 * Construct a map between the explicit domain of "mupa" and
9030 * Note that this assumes that the domain consists of explicit elements.
9032 static __isl_give isl_union_map
*isl_union_map_from_multi_union_pw_aff_0D(
9033 __isl_take isl_multi_union_pw_aff
*mupa
)
9037 isl_union_set
*dom
, *ran
;
9039 space
= isl_multi_union_pw_aff_get_space(mupa
);
9040 dom
= isl_multi_union_pw_aff_domain(mupa
);
9041 ran
= isl_union_set_from_set(isl_set_universe(space
));
9043 is_params
= isl_union_set_is_params(dom
);
9045 dom
= isl_union_set_free(dom
);
9047 isl_die(isl_union_set_get_ctx(dom
), isl_error_invalid
,
9048 "cannot create union map from expression without "
9049 "explicit domain elements",
9050 dom
= isl_union_set_free(dom
));
9052 return isl_union_map_from_domain_and_range(dom
, ran
);
9055 /* Construct a union map mapping the shared domain
9056 * of the union piecewise affine expressions to the range of "mupa"
9057 * with each dimension in the range equated to the
9058 * corresponding union piecewise affine expression.
9060 * If the input is zero-dimensional, then construct a mapping
9061 * from its explicit domain.
9063 __isl_give isl_union_map
*isl_union_map_from_multi_union_pw_aff(
9064 __isl_take isl_multi_union_pw_aff
*mupa
)
9069 isl_union_map
*umap
;
9070 isl_union_pw_aff
*upa
;
9072 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
9074 mupa
= isl_multi_union_pw_aff_free(mupa
);
9079 return isl_union_map_from_multi_union_pw_aff_0D(mupa
);
9081 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, 0);
9082 umap
= isl_union_map_from_union_pw_aff(upa
);
9084 for (i
= 1; i
< n
; ++i
) {
9085 isl_union_map
*umap_i
;
9087 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
9088 umap_i
= isl_union_map_from_union_pw_aff(upa
);
9089 umap
= isl_union_map_flat_range_product(umap
, umap_i
);
9092 space
= isl_multi_union_pw_aff_get_space(mupa
);
9093 umap
= isl_union_map_reset_range_space(umap
, space
);
9095 isl_multi_union_pw_aff_free(mupa
);
9099 /* Internal data structure for isl_union_pw_multi_aff_reset_range_space.
9100 * "range" is the space from which to set the range space.
9101 * "res" collects the results.
9103 struct isl_union_pw_multi_aff_reset_range_space_data
{
9105 isl_union_pw_multi_aff
*res
;
9108 /* Replace the range space of "pma" by the range space of data->range and
9109 * add the result to data->res.
9111 static isl_stat
reset_range_space(__isl_take isl_pw_multi_aff
*pma
, void *user
)
9113 struct isl_union_pw_multi_aff_reset_range_space_data
*data
= user
;
9116 space
= isl_pw_multi_aff_get_space(pma
);
9117 space
= isl_space_domain(space
);
9118 space
= isl_space_extend_domain_with_range(space
,
9119 isl_space_copy(data
->range
));
9120 pma
= isl_pw_multi_aff_reset_space(pma
, space
);
9121 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma
);
9123 return data
->res
? isl_stat_ok
: isl_stat_error
;
9126 /* Replace the range space of all the piecewise affine expressions in "upma" by
9127 * the range space of "space".
9129 * This assumes that all these expressions have the same output dimension.
9131 * Since the spaces of the expressions change, so do their hash values.
9132 * We therefore need to create a new isl_union_pw_multi_aff.
9133 * Note that the hash value is currently computed based on the entire
9134 * space even though there can only be a single expression with a given
9137 static __isl_give isl_union_pw_multi_aff
*
9138 isl_union_pw_multi_aff_reset_range_space(
9139 __isl_take isl_union_pw_multi_aff
*upma
, __isl_take isl_space
*space
)
9141 struct isl_union_pw_multi_aff_reset_range_space_data data
= { space
};
9142 isl_space
*space_upma
;
9144 space_upma
= isl_union_pw_multi_aff_get_space(upma
);
9145 data
.res
= isl_union_pw_multi_aff_empty(space_upma
);
9146 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma
,
9147 &reset_range_space
, &data
) < 0)
9148 data
.res
= isl_union_pw_multi_aff_free(data
.res
);
9150 isl_space_free(space
);
9151 isl_union_pw_multi_aff_free(upma
);
9155 /* Construct and return a union piecewise multi affine expression
9156 * that is equal to the given multi union piecewise affine expression,
9157 * in the special case of a 0D multi union piecewise affine expression.
9159 * Construct a union piecewise multi affine expression
9160 * on top of the explicit domain of the input.
9162 __isl_give isl_union_pw_multi_aff
*
9163 isl_union_pw_multi_aff_from_multi_union_pw_aff_0D(
9164 __isl_take isl_multi_union_pw_aff
*mupa
)
9168 isl_union_set
*domain
;
9170 space
= isl_multi_union_pw_aff_get_space(mupa
);
9171 mv
= isl_multi_val_zero(space
);
9172 domain
= isl_multi_union_pw_aff_domain(mupa
);
9173 return isl_union_pw_multi_aff_multi_val_on_domain(domain
, mv
);
9176 /* Construct and return a union piecewise multi affine expression
9177 * that is equal to the given multi union piecewise affine expression.
9179 * If the input is zero-dimensional, then
9180 * construct a union piecewise multi affine expression
9181 * on top of the explicit domain of the input.
9183 __isl_give isl_union_pw_multi_aff
*
9184 isl_union_pw_multi_aff_from_multi_union_pw_aff(
9185 __isl_take isl_multi_union_pw_aff
*mupa
)
9190 isl_union_pw_multi_aff
*upma
;
9191 isl_union_pw_aff
*upa
;
9193 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
9195 mupa
= isl_multi_union_pw_aff_free(mupa
);
9200 return isl_union_pw_multi_aff_from_multi_union_pw_aff_0D(mupa
);
9202 space
= isl_multi_union_pw_aff_get_space(mupa
);
9203 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, 0);
9204 upma
= isl_union_pw_multi_aff_from_union_pw_aff(upa
);
9206 for (i
= 1; i
< n
; ++i
) {
9207 isl_union_pw_multi_aff
*upma_i
;
9209 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
9210 upma_i
= isl_union_pw_multi_aff_from_union_pw_aff(upa
);
9211 upma
= isl_union_pw_multi_aff_flat_range_product(upma
, upma_i
);
9214 upma
= isl_union_pw_multi_aff_reset_range_space(upma
, space
);
9216 isl_multi_union_pw_aff_free(mupa
);
9220 /* Intersect the range of "mupa" with "range",
9221 * in the special case where "mupa" is 0D.
9223 * Intersect the domain of "mupa" with the constraints on the parameters
9226 static __isl_give isl_multi_union_pw_aff
*mupa_intersect_range_0D(
9227 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_set
*range
)
9229 range
= isl_set_params(range
);
9230 mupa
= isl_multi_union_pw_aff_intersect_params(mupa
, range
);
9234 /* Intersect the range of "mupa" with "range".
9235 * That is, keep only those domain elements that have a function value
9238 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_intersect_range(
9239 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_set
*range
)
9241 isl_union_pw_multi_aff
*upma
;
9242 isl_union_set
*domain
;
9247 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
9248 if (n
< 0 || !range
)
9251 space
= isl_set_get_space(range
);
9252 match
= isl_space_tuple_is_equal(mupa
->space
, isl_dim_set
,
9253 space
, isl_dim_set
);
9254 isl_space_free(space
);
9258 isl_die(isl_multi_union_pw_aff_get_ctx(mupa
), isl_error_invalid
,
9259 "space don't match", goto error
);
9261 return mupa_intersect_range_0D(mupa
, range
);
9263 upma
= isl_union_pw_multi_aff_from_multi_union_pw_aff(
9264 isl_multi_union_pw_aff_copy(mupa
));
9265 domain
= isl_union_set_from_set(range
);
9266 domain
= isl_union_set_preimage_union_pw_multi_aff(domain
, upma
);
9267 mupa
= isl_multi_union_pw_aff_intersect_domain(mupa
, domain
);
9271 isl_multi_union_pw_aff_free(mupa
);
9272 isl_set_free(range
);
9276 /* Return the shared domain of the elements of "mupa",
9277 * in the special case where "mupa" is zero-dimensional.
9279 * Return the explicit domain of "mupa".
9280 * Note that this domain may be a parameter set, either
9281 * because "mupa" is meant to live in a set space or
9282 * because no explicit domain has been set.
9284 __isl_give isl_union_set
*isl_multi_union_pw_aff_domain_0D(
9285 __isl_take isl_multi_union_pw_aff
*mupa
)
9289 dom
= isl_multi_union_pw_aff_get_explicit_domain(mupa
);
9290 isl_multi_union_pw_aff_free(mupa
);
9295 /* Return the shared domain of the elements of "mupa".
9297 * If "mupa" is zero-dimensional, then return its explicit domain.
9299 __isl_give isl_union_set
*isl_multi_union_pw_aff_domain(
9300 __isl_take isl_multi_union_pw_aff
*mupa
)
9304 isl_union_pw_aff
*upa
;
9307 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
9309 mupa
= isl_multi_union_pw_aff_free(mupa
);
9314 return isl_multi_union_pw_aff_domain_0D(mupa
);
9316 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, 0);
9317 dom
= isl_union_pw_aff_domain(upa
);
9318 for (i
= 1; i
< n
; ++i
) {
9319 isl_union_set
*dom_i
;
9321 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
9322 dom_i
= isl_union_pw_aff_domain(upa
);
9323 dom
= isl_union_set_intersect(dom
, dom_i
);
9326 isl_multi_union_pw_aff_free(mupa
);
9330 /* Apply "aff" to "mupa". The space of "mupa" is equal to the domain of "aff".
9331 * In particular, the spaces have been aligned.
9332 * The result is defined over the shared domain of the elements of "mupa"
9334 * We first extract the parametric constant part of "aff" and
9335 * define that over the shared domain.
9336 * Then we iterate over all input dimensions of "aff" and add the corresponding
9337 * multiples of the elements of "mupa".
9338 * Finally, we consider the integer divisions, calling the function
9339 * recursively to obtain an isl_union_pw_aff corresponding to the
9340 * integer division argument.
9342 static __isl_give isl_union_pw_aff
*multi_union_pw_aff_apply_aff(
9343 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_aff
*aff
)
9346 isl_size n_in
, n_div
;
9347 isl_union_pw_aff
*upa
;
9348 isl_union_set
*uset
;
9352 n_in
= isl_aff_dim(aff
, isl_dim_in
);
9353 n_div
= isl_aff_dim(aff
, isl_dim_div
);
9354 if (n_in
< 0 || n_div
< 0)
9357 uset
= isl_multi_union_pw_aff_domain(isl_multi_union_pw_aff_copy(mupa
));
9358 cst
= isl_aff_copy(aff
);
9359 cst
= isl_aff_drop_dims(cst
, isl_dim_div
, 0, n_div
);
9360 cst
= isl_aff_drop_dims(cst
, isl_dim_in
, 0, n_in
);
9361 cst
= isl_aff_project_domain_on_params(cst
);
9362 upa
= isl_union_pw_aff_aff_on_domain(uset
, cst
);
9364 for (i
= 0; i
< n_in
; ++i
) {
9365 isl_union_pw_aff
*upa_i
;
9367 if (!isl_aff_involves_dims(aff
, isl_dim_in
, i
, 1))
9369 v
= isl_aff_get_coefficient_val(aff
, isl_dim_in
, i
);
9370 upa_i
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
9371 upa_i
= isl_union_pw_aff_scale_val(upa_i
, v
);
9372 upa
= isl_union_pw_aff_add(upa
, upa_i
);
9375 for (i
= 0; i
< n_div
; ++i
) {
9377 isl_union_pw_aff
*upa_i
;
9379 if (!isl_aff_involves_dims(aff
, isl_dim_div
, i
, 1))
9381 div
= isl_aff_get_div(aff
, i
);
9382 upa_i
= multi_union_pw_aff_apply_aff(
9383 isl_multi_union_pw_aff_copy(mupa
), div
);
9384 upa_i
= isl_union_pw_aff_floor(upa_i
);
9385 v
= isl_aff_get_coefficient_val(aff
, isl_dim_div
, i
);
9386 upa_i
= isl_union_pw_aff_scale_val(upa_i
, v
);
9387 upa
= isl_union_pw_aff_add(upa
, upa_i
);
9390 isl_multi_union_pw_aff_free(mupa
);
9395 isl_multi_union_pw_aff_free(mupa
);
9400 /* Apply "aff" to "mupa". The space of "mupa" needs to be compatible
9401 * with the domain of "aff".
9402 * Furthermore, the dimension of this space needs to be greater than zero.
9403 * The result is defined over the shared domain of the elements of "mupa"
9405 * We perform these checks and then hand over control to
9406 * multi_union_pw_aff_apply_aff.
9408 __isl_give isl_union_pw_aff
*isl_multi_union_pw_aff_apply_aff(
9409 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_aff
*aff
)
9412 isl_space
*space1
, *space2
;
9415 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
9416 isl_aff_get_space(aff
));
9417 aff
= isl_aff_align_params(aff
, isl_multi_union_pw_aff_get_space(mupa
));
9421 space1
= isl_multi_union_pw_aff_get_space(mupa
);
9422 space2
= isl_aff_get_domain_space(aff
);
9423 equal
= isl_space_is_equal(space1
, space2
);
9424 isl_space_free(space1
);
9425 isl_space_free(space2
);
9429 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
9430 "spaces don't match", goto error
);
9431 dim
= isl_aff_dim(aff
, isl_dim_in
);
9435 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
9436 "cannot determine domains", goto error
);
9438 return multi_union_pw_aff_apply_aff(mupa
, aff
);
9440 isl_multi_union_pw_aff_free(mupa
);
9445 /* Apply "ma" to "mupa", in the special case where "mupa" is 0D.
9446 * The space of "mupa" is known to be compatible with the domain of "ma".
9448 * Construct an isl_multi_union_pw_aff that is equal to "ma"
9449 * on the domain of "mupa".
9451 static __isl_give isl_multi_union_pw_aff
*mupa_apply_multi_aff_0D(
9452 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_multi_aff
*ma
)
9456 dom
= isl_multi_union_pw_aff_domain(mupa
);
9457 ma
= isl_multi_aff_project_domain_on_params(ma
);
9459 return isl_multi_union_pw_aff_multi_aff_on_domain(dom
, ma
);
9462 /* Apply "ma" to "mupa". The space of "mupa" needs to be compatible
9463 * with the domain of "ma".
9464 * The result is defined over the shared domain of the elements of "mupa"
9466 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_apply_multi_aff(
9467 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_multi_aff
*ma
)
9469 isl_space
*space1
, *space2
;
9470 isl_multi_union_pw_aff
*res
;
9473 isl_size n_in
, n_out
;
9475 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
9476 isl_multi_aff_get_space(ma
));
9477 ma
= isl_multi_aff_align_params(ma
,
9478 isl_multi_union_pw_aff_get_space(mupa
));
9479 n_in
= isl_multi_aff_dim(ma
, isl_dim_in
);
9480 n_out
= isl_multi_aff_dim(ma
, isl_dim_out
);
9481 if (!mupa
|| n_in
< 0 || n_out
< 0)
9484 space1
= isl_multi_union_pw_aff_get_space(mupa
);
9485 space2
= isl_multi_aff_get_domain_space(ma
);
9486 equal
= isl_space_is_equal(space1
, space2
);
9487 isl_space_free(space1
);
9488 isl_space_free(space2
);
9492 isl_die(isl_multi_aff_get_ctx(ma
), isl_error_invalid
,
9493 "spaces don't match", goto error
);
9495 return mupa_apply_multi_aff_0D(mupa
, ma
);
9497 space1
= isl_space_range(isl_multi_aff_get_space(ma
));
9498 res
= isl_multi_union_pw_aff_alloc(space1
);
9500 for (i
= 0; i
< n_out
; ++i
) {
9502 isl_union_pw_aff
*upa
;
9504 aff
= isl_multi_aff_get_aff(ma
, i
);
9505 upa
= multi_union_pw_aff_apply_aff(
9506 isl_multi_union_pw_aff_copy(mupa
), aff
);
9507 res
= isl_multi_union_pw_aff_set_union_pw_aff(res
, i
, upa
);
9510 isl_multi_aff_free(ma
);
9511 isl_multi_union_pw_aff_free(mupa
);
9514 isl_multi_union_pw_aff_free(mupa
);
9515 isl_multi_aff_free(ma
);
9519 /* Apply "pa" to "mupa", in the special case where "mupa" is 0D.
9520 * The space of "mupa" is known to be compatible with the domain of "pa".
9522 * Construct an isl_multi_union_pw_aff that is equal to "pa"
9523 * on the domain of "mupa".
9525 static __isl_give isl_union_pw_aff
*isl_multi_union_pw_aff_apply_pw_aff_0D(
9526 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_pw_aff
*pa
)
9530 dom
= isl_multi_union_pw_aff_domain(mupa
);
9531 pa
= isl_pw_aff_project_domain_on_params(pa
);
9533 return isl_union_pw_aff_pw_aff_on_domain(dom
, pa
);
9536 /* Apply "pa" to "mupa". The space of "mupa" needs to be compatible
9537 * with the domain of "pa".
9538 * Furthermore, the dimension of this space needs to be greater than zero.
9539 * The result is defined over the shared domain of the elements of "mupa"
9541 __isl_give isl_union_pw_aff
*isl_multi_union_pw_aff_apply_pw_aff(
9542 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_pw_aff
*pa
)
9547 isl_space
*space
, *space2
;
9548 isl_union_pw_aff
*upa
;
9550 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
9551 isl_pw_aff_get_space(pa
));
9552 pa
= isl_pw_aff_align_params(pa
,
9553 isl_multi_union_pw_aff_get_space(mupa
));
9557 space
= isl_multi_union_pw_aff_get_space(mupa
);
9558 space2
= isl_pw_aff_get_domain_space(pa
);
9559 equal
= isl_space_is_equal(space
, space2
);
9560 isl_space_free(space
);
9561 isl_space_free(space2
);
9565 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
9566 "spaces don't match", goto error
);
9567 n_in
= isl_pw_aff_dim(pa
, isl_dim_in
);
9571 return isl_multi_union_pw_aff_apply_pw_aff_0D(mupa
, pa
);
9573 space
= isl_space_params(isl_multi_union_pw_aff_get_space(mupa
));
9574 upa
= isl_union_pw_aff_empty(space
);
9576 for (i
= 0; i
< pa
->n
; ++i
) {
9579 isl_multi_union_pw_aff
*mupa_i
;
9580 isl_union_pw_aff
*upa_i
;
9582 mupa_i
= isl_multi_union_pw_aff_copy(mupa
);
9583 domain
= isl_set_copy(pa
->p
[i
].set
);
9584 mupa_i
= isl_multi_union_pw_aff_intersect_range(mupa_i
, domain
);
9585 aff
= isl_aff_copy(pa
->p
[i
].aff
);
9586 upa_i
= multi_union_pw_aff_apply_aff(mupa_i
, aff
);
9587 upa
= isl_union_pw_aff_union_add(upa
, upa_i
);
9590 isl_multi_union_pw_aff_free(mupa
);
9591 isl_pw_aff_free(pa
);
9594 isl_multi_union_pw_aff_free(mupa
);
9595 isl_pw_aff_free(pa
);
9599 /* Apply "pma" to "mupa", in the special case where "mupa" is 0D.
9600 * The space of "mupa" is known to be compatible with the domain of "pma".
9602 * Construct an isl_multi_union_pw_aff that is equal to "pma"
9603 * on the domain of "mupa".
9605 static __isl_give isl_multi_union_pw_aff
*mupa_apply_pw_multi_aff_0D(
9606 __isl_take isl_multi_union_pw_aff
*mupa
,
9607 __isl_take isl_pw_multi_aff
*pma
)
9611 dom
= isl_multi_union_pw_aff_domain(mupa
);
9612 pma
= isl_pw_multi_aff_project_domain_on_params(pma
);
9614 return isl_multi_union_pw_aff_pw_multi_aff_on_domain(dom
, pma
);
9617 /* Apply "pma" to "mupa". The space of "mupa" needs to be compatible
9618 * with the domain of "pma".
9619 * The result is defined over the shared domain of the elements of "mupa"
9621 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_apply_pw_multi_aff(
9622 __isl_take isl_multi_union_pw_aff
*mupa
,
9623 __isl_take isl_pw_multi_aff
*pma
)
9625 isl_space
*space1
, *space2
;
9626 isl_multi_union_pw_aff
*res
;
9629 isl_size n_in
, n_out
;
9631 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
9632 isl_pw_multi_aff_get_space(pma
));
9633 pma
= isl_pw_multi_aff_align_params(pma
,
9634 isl_multi_union_pw_aff_get_space(mupa
));
9638 space1
= isl_multi_union_pw_aff_get_space(mupa
);
9639 space2
= isl_pw_multi_aff_get_domain_space(pma
);
9640 equal
= isl_space_is_equal(space1
, space2
);
9641 isl_space_free(space1
);
9642 isl_space_free(space2
);
9646 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
9647 "spaces don't match", goto error
);
9648 n_in
= isl_pw_multi_aff_dim(pma
, isl_dim_in
);
9649 n_out
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
9650 if (n_in
< 0 || n_out
< 0)
9653 return mupa_apply_pw_multi_aff_0D(mupa
, pma
);
9655 space1
= isl_space_range(isl_pw_multi_aff_get_space(pma
));
9656 res
= isl_multi_union_pw_aff_alloc(space1
);
9658 for (i
= 0; i
< n_out
; ++i
) {
9660 isl_union_pw_aff
*upa
;
9662 pa
= isl_pw_multi_aff_get_pw_aff(pma
, i
);
9663 upa
= isl_multi_union_pw_aff_apply_pw_aff(
9664 isl_multi_union_pw_aff_copy(mupa
), pa
);
9665 res
= isl_multi_union_pw_aff_set_union_pw_aff(res
, i
, upa
);
9668 isl_pw_multi_aff_free(pma
);
9669 isl_multi_union_pw_aff_free(mupa
);
9672 isl_multi_union_pw_aff_free(mupa
);
9673 isl_pw_multi_aff_free(pma
);
9677 /* Replace the explicit domain of "mupa" by its preimage under "upma".
9678 * If the explicit domain only keeps track of constraints on the parameters,
9679 * then only update those constraints.
9681 static __isl_give isl_multi_union_pw_aff
*preimage_explicit_domain(
9682 __isl_take isl_multi_union_pw_aff
*mupa
,
9683 __isl_keep isl_union_pw_multi_aff
*upma
)
9687 if (isl_multi_union_pw_aff_check_has_explicit_domain(mupa
) < 0)
9688 return isl_multi_union_pw_aff_free(mupa
);
9690 mupa
= isl_multi_union_pw_aff_cow(mupa
);
9694 is_params
= isl_union_set_is_params(mupa
->u
.dom
);
9696 return isl_multi_union_pw_aff_free(mupa
);
9698 upma
= isl_union_pw_multi_aff_copy(upma
);
9700 mupa
->u
.dom
= isl_union_set_intersect_params(mupa
->u
.dom
,
9701 isl_union_set_params(isl_union_pw_multi_aff_domain(upma
)));
9703 mupa
->u
.dom
= isl_union_set_preimage_union_pw_multi_aff(
9706 return isl_multi_union_pw_aff_free(mupa
);
9710 /* Compute the pullback of "mupa" by the function represented by "upma".
9711 * In other words, plug in "upma" in "mupa". The result contains
9712 * expressions defined over the domain space of "upma".
9714 * Run over all elements of "mupa" and plug in "upma" in each of them.
9716 * If "mupa" has an explicit domain, then it is this domain
9717 * that needs to undergo a pullback instead, i.e., a preimage.
9719 __isl_give isl_multi_union_pw_aff
*
9720 isl_multi_union_pw_aff_pullback_union_pw_multi_aff(
9721 __isl_take isl_multi_union_pw_aff
*mupa
,
9722 __isl_take isl_union_pw_multi_aff
*upma
)
9727 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
9728 isl_union_pw_multi_aff_get_space(upma
));
9729 upma
= isl_union_pw_multi_aff_align_params(upma
,
9730 isl_multi_union_pw_aff_get_space(mupa
));
9731 mupa
= isl_multi_union_pw_aff_cow(mupa
);
9732 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
9736 for (i
= 0; i
< n
; ++i
) {
9737 isl_union_pw_aff
*upa
;
9739 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
9740 upa
= isl_union_pw_aff_pullback_union_pw_multi_aff(upa
,
9741 isl_union_pw_multi_aff_copy(upma
));
9742 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
9745 if (isl_multi_union_pw_aff_has_explicit_domain(mupa
))
9746 mupa
= preimage_explicit_domain(mupa
, upma
);
9748 isl_union_pw_multi_aff_free(upma
);
9751 isl_multi_union_pw_aff_free(mupa
);
9752 isl_union_pw_multi_aff_free(upma
);
9756 /* Extract the sequence of elements in "mupa" with domain space "space"
9757 * (ignoring parameters).
9759 * For the elements of "mupa" that are not defined on the specified space,
9760 * the corresponding element in the result is empty.
9762 __isl_give isl_multi_pw_aff
*isl_multi_union_pw_aff_extract_multi_pw_aff(
9763 __isl_keep isl_multi_union_pw_aff
*mupa
, __isl_take isl_space
*space
)
9767 isl_space
*space_mpa
;
9768 isl_multi_pw_aff
*mpa
;
9770 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
9771 if (n
< 0 || !space
)
9774 space_mpa
= isl_multi_union_pw_aff_get_space(mupa
);
9775 space
= isl_space_replace_params(space
, space_mpa
);
9776 space_mpa
= isl_space_map_from_domain_and_range(isl_space_copy(space
),
9778 mpa
= isl_multi_pw_aff_alloc(space_mpa
);
9780 space
= isl_space_from_domain(space
);
9781 space
= isl_space_add_dims(space
, isl_dim_out
, 1);
9782 for (i
= 0; i
< n
; ++i
) {
9783 isl_union_pw_aff
*upa
;
9786 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
9787 pa
= isl_union_pw_aff_extract_pw_aff(upa
,
9788 isl_space_copy(space
));
9789 mpa
= isl_multi_pw_aff_set_pw_aff(mpa
, i
, pa
);
9790 isl_union_pw_aff_free(upa
);
9793 isl_space_free(space
);
9796 isl_space_free(space
);
9800 /* Data structure that specifies how isl_union_pw_multi_aff_un_op
9801 * should modify the base expressions in the input.
9803 * If "filter" is not NULL, then only the base expressions that satisfy "filter"
9804 * are taken into account.
9805 * "fn" is applied to each entry in the input.
9807 struct isl_union_pw_multi_aff_un_op_control
{
9808 isl_bool (*filter
)(__isl_keep isl_pw_multi_aff
*part
);
9809 __isl_give isl_pw_multi_aff
*(*fn
)(__isl_take isl_pw_multi_aff
*pma
);
9812 /* Wrapper for isl_union_pw_multi_aff_un_op filter functions (which do not take
9813 * a second argument) for use as an isl_union_pw_multi_aff_transform
9814 * filter function (which does take a second argument).
9815 * Simply call control->filter without the second argument.
9817 static isl_bool
isl_union_pw_multi_aff_un_op_filter_drop_user(
9818 __isl_take isl_pw_multi_aff
*pma
, void *user
)
9820 struct isl_union_pw_multi_aff_un_op_control
*control
= user
;
9822 return control
->filter(pma
);
9825 /* Wrapper for isl_union_pw_multi_aff_un_op base functions (which do not take
9826 * a second argument) for use as an isl_union_pw_multi_aff_transform
9827 * base function (which does take a second argument).
9828 * Simply call control->fn without the second argument.
9830 static __isl_give isl_pw_multi_aff
*isl_union_pw_multi_aff_un_op_drop_user(
9831 __isl_take isl_pw_multi_aff
*pma
, void *user
)
9833 struct isl_union_pw_multi_aff_un_op_control
*control
= user
;
9835 return control
->fn(pma
);
9838 /* Construct an isl_union_pw_multi_aff that is obtained by
9839 * modifying "upma" according to "control".
9841 * isl_union_pw_multi_aff_transform performs essentially
9842 * the same operation, but takes a filter and a callback function
9843 * of a different form (with an extra argument).
9844 * Call isl_union_pw_multi_aff_transform with wrappers
9845 * that remove this extra argument.
9847 static __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_un_op(
9848 __isl_take isl_union_pw_multi_aff
*upma
,
9849 struct isl_union_pw_multi_aff_un_op_control
*control
)
9851 struct isl_union_pw_multi_aff_transform_control t_control
= {
9852 .filter
= &isl_union_pw_multi_aff_un_op_filter_drop_user
,
9853 .filter_user
= control
,
9854 .fn
= &isl_union_pw_multi_aff_un_op_drop_user
,
9858 return isl_union_pw_multi_aff_transform(upma
, &t_control
);
9861 /* For each function in "upma" of the form A -> [B -> C],
9862 * extract the function A -> B and collect the results.
9864 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_range_factor_domain(
9865 __isl_take isl_union_pw_multi_aff
*upma
)
9867 struct isl_union_pw_multi_aff_un_op_control control
= {
9868 .filter
= &isl_pw_multi_aff_range_is_wrapping
,
9869 .fn
= &isl_pw_multi_aff_range_factor_domain
,
9871 return isl_union_pw_multi_aff_un_op(upma
, &control
);
9874 /* For each function in "upma" of the form A -> [B -> C],
9875 * extract the function A -> C and collect the results.
9877 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_range_factor_range(
9878 __isl_take isl_union_pw_multi_aff
*upma
)
9880 struct isl_union_pw_multi_aff_un_op_control control
= {
9881 .filter
= &isl_pw_multi_aff_range_is_wrapping
,
9882 .fn
= &isl_pw_multi_aff_range_factor_range
,
9884 return isl_union_pw_multi_aff_un_op(upma
, &control
);
9887 /* Evaluate the affine function "aff" in the void point "pnt".
9888 * In particular, return the value NaN.
9890 static __isl_give isl_val
*eval_void(__isl_take isl_aff
*aff
,
9891 __isl_take isl_point
*pnt
)
9895 ctx
= isl_point_get_ctx(pnt
);
9897 isl_point_free(pnt
);
9898 return isl_val_nan(ctx
);
9901 /* Evaluate the affine expression "aff"
9902 * in the coordinates (with denominator) "pnt".
9904 static __isl_give isl_val
*eval(__isl_keep isl_vec
*aff
,
9905 __isl_keep isl_vec
*pnt
)
9914 ctx
= isl_vec_get_ctx(aff
);
9917 isl_seq_inner_product(aff
->el
+ 1, pnt
->el
, pnt
->size
, &n
);
9918 isl_int_mul(d
, aff
->el
[0], pnt
->el
[0]);
9919 v
= isl_val_rat_from_isl_int(ctx
, n
, d
);
9920 v
= isl_val_normalize(v
);
9927 /* Check that the domain space of "aff" is equal to "space".
9929 static isl_stat
isl_aff_check_has_domain_space(__isl_keep isl_aff
*aff
,
9930 __isl_keep isl_space
*space
)
9934 ok
= isl_space_is_equal(isl_aff_peek_domain_space(aff
), space
);
9936 return isl_stat_error
;
9938 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
9939 "incompatible spaces", return isl_stat_error
);
9943 /* Evaluate the affine function "aff" in "pnt".
9945 __isl_give isl_val
*isl_aff_eval(__isl_take isl_aff
*aff
,
9946 __isl_take isl_point
*pnt
)
9950 isl_local_space
*ls
;
9952 if (isl_aff_check_has_domain_space(aff
, isl_point_peek_space(pnt
)) < 0)
9954 is_void
= isl_point_is_void(pnt
);
9958 return eval_void(aff
, pnt
);
9960 ls
= isl_aff_get_domain_local_space(aff
);
9961 pnt
= isl_local_space_lift_point(ls
, pnt
);
9963 v
= eval(aff
->v
, isl_point_peek_vec(pnt
));
9966 isl_point_free(pnt
);
9971 isl_point_free(pnt
);