2 * Copyright 2011 INRIA Saclay
3 * Copyright 2011 Sven Verdoolaege
4 * Copyright 2012-2014 Ecole Normale Superieure
5 * Copyright 2014 INRIA Rocquencourt
6 * Copyright 2016 Sven Verdoolaege
7 * Copyright 2018 Cerebras Systems
9 * Use of this software is governed by the MIT license
11 * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
12 * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
14 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
15 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
16 * B.P. 105 - 78153 Le Chesnay, France
17 * and Cerebras Systems, 175 S San Antonio Rd, Los Altos, CA, USA
20 #include <isl_ctx_private.h>
21 #include <isl_map_private.h>
22 #include <isl_union_map_private.h>
23 #include <isl_aff_private.h>
24 #include <isl_space_private.h>
25 #include <isl_local_space_private.h>
26 #include <isl_vec_private.h>
27 #include <isl_mat_private.h>
28 #include <isl_id_private.h>
29 #include <isl/constraint.h>
32 #include <isl_val_private.h>
33 #include <isl_point_private.h>
34 #include <isl_config.h>
39 #include <isl_list_templ.c>
42 #define EL_BASE pw_aff
44 #include <isl_list_templ.c>
47 #define EL_BASE pw_multi_aff
49 #include <isl_list_templ.c>
52 #define EL_BASE union_pw_aff
54 #include <isl_list_templ.c>
57 #define EL_BASE union_pw_multi_aff
59 #include <isl_list_templ.c>
61 __isl_give isl_aff
*isl_aff_alloc_vec(__isl_take isl_local_space
*ls
,
62 __isl_take isl_vec
*v
)
69 aff
= isl_calloc_type(v
->ctx
, struct isl_aff
);
79 isl_local_space_free(ls
);
84 __isl_give isl_aff
*isl_aff_alloc(__isl_take isl_local_space
*ls
)
93 ctx
= isl_local_space_get_ctx(ls
);
94 if (!isl_local_space_divs_known(ls
))
95 isl_die(ctx
, isl_error_invalid
, "local space has unknown divs",
97 if (!isl_local_space_is_set(ls
))
98 isl_die(ctx
, isl_error_invalid
,
99 "domain of affine expression should be a set",
102 total
= isl_local_space_dim(ls
, isl_dim_all
);
105 v
= isl_vec_alloc(ctx
, 1 + 1 + total
);
106 return isl_aff_alloc_vec(ls
, v
);
108 isl_local_space_free(ls
);
112 __isl_give isl_aff
*isl_aff_copy(__isl_keep isl_aff
*aff
)
121 __isl_give isl_aff
*isl_aff_dup(__isl_keep isl_aff
*aff
)
126 return isl_aff_alloc_vec(isl_local_space_copy(aff
->ls
),
127 isl_vec_copy(aff
->v
));
130 __isl_give isl_aff
*isl_aff_cow(__isl_take isl_aff
*aff
)
138 return isl_aff_dup(aff
);
141 __isl_give isl_aff
*isl_aff_zero_on_domain(__isl_take isl_local_space
*ls
)
145 aff
= isl_aff_alloc(ls
);
149 isl_int_set_si(aff
->v
->el
[0], 1);
150 isl_seq_clr(aff
->v
->el
+ 1, aff
->v
->size
- 1);
155 /* Return an affine expression that is equal to zero on domain space "space".
157 __isl_give isl_aff
*isl_aff_zero_on_domain_space(__isl_take isl_space
*space
)
159 return isl_aff_zero_on_domain(isl_local_space_from_space(space
));
162 /* Return a piecewise affine expression defined on the specified domain
163 * that is equal to zero.
165 __isl_give isl_pw_aff
*isl_pw_aff_zero_on_domain(__isl_take isl_local_space
*ls
)
167 return isl_pw_aff_from_aff(isl_aff_zero_on_domain(ls
));
170 /* Change "aff" into a NaN.
172 * Note that this function gets called from isl_aff_nan_on_domain,
173 * so "aff" may not have been initialized yet.
175 static __isl_give isl_aff
*isl_aff_set_nan(__isl_take isl_aff
*aff
)
177 aff
= isl_aff_cow(aff
);
181 aff
->v
= isl_vec_clr(aff
->v
);
183 return isl_aff_free(aff
);
188 /* Return an affine expression defined on the specified domain
189 * that represents NaN.
191 __isl_give isl_aff
*isl_aff_nan_on_domain(__isl_take isl_local_space
*ls
)
195 aff
= isl_aff_alloc(ls
);
196 return isl_aff_set_nan(aff
);
199 /* Return a piecewise affine expression defined on the specified domain
200 * that represents NaN.
202 __isl_give isl_pw_aff
*isl_pw_aff_nan_on_domain(__isl_take isl_local_space
*ls
)
204 return isl_pw_aff_from_aff(isl_aff_nan_on_domain(ls
));
207 /* Return an affine expression that is equal to "val" on
208 * domain local space "ls".
210 __isl_give isl_aff
*isl_aff_val_on_domain(__isl_take isl_local_space
*ls
,
211 __isl_take isl_val
*val
)
217 if (!isl_val_is_rat(val
))
218 isl_die(isl_val_get_ctx(val
), isl_error_invalid
,
219 "expecting rational value", goto error
);
221 aff
= isl_aff_alloc(isl_local_space_copy(ls
));
225 isl_seq_clr(aff
->v
->el
+ 2, aff
->v
->size
- 2);
226 isl_int_set(aff
->v
->el
[1], val
->n
);
227 isl_int_set(aff
->v
->el
[0], val
->d
);
229 isl_local_space_free(ls
);
233 isl_local_space_free(ls
);
238 /* Return an affine expression that is equal to "val" on domain space "space".
240 __isl_give isl_aff
*isl_aff_val_on_domain_space(__isl_take isl_space
*space
,
241 __isl_take isl_val
*val
)
243 return isl_aff_val_on_domain(isl_local_space_from_space(space
), val
);
246 /* Return an affine expression that is equal to the specified dimension
249 __isl_give isl_aff
*isl_aff_var_on_domain(__isl_take isl_local_space
*ls
,
250 enum isl_dim_type type
, unsigned pos
)
258 space
= isl_local_space_get_space(ls
);
261 if (isl_space_is_map(space
))
262 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
263 "expecting (parameter) set space", goto error
);
264 if (isl_local_space_check_range(ls
, type
, pos
, 1) < 0)
267 isl_space_free(space
);
268 aff
= isl_aff_alloc(ls
);
272 pos
+= isl_local_space_offset(aff
->ls
, type
);
274 isl_int_set_si(aff
->v
->el
[0], 1);
275 isl_seq_clr(aff
->v
->el
+ 1, aff
->v
->size
- 1);
276 isl_int_set_si(aff
->v
->el
[1 + pos
], 1);
280 isl_local_space_free(ls
);
281 isl_space_free(space
);
285 /* Return a piecewise affine expression that is equal to
286 * the specified dimension in "ls".
288 __isl_give isl_pw_aff
*isl_pw_aff_var_on_domain(__isl_take isl_local_space
*ls
,
289 enum isl_dim_type type
, unsigned pos
)
291 return isl_pw_aff_from_aff(isl_aff_var_on_domain(ls
, type
, pos
));
294 /* Return an affine expression that is equal to the parameter
295 * in the domain space "space" with identifier "id".
297 __isl_give isl_aff
*isl_aff_param_on_domain_space_id(
298 __isl_take isl_space
*space
, __isl_take isl_id
*id
)
305 pos
= isl_space_find_dim_by_id(space
, isl_dim_param
, id
);
307 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
308 "parameter not found in space", goto error
);
310 ls
= isl_local_space_from_space(space
);
311 return isl_aff_var_on_domain(ls
, isl_dim_param
, pos
);
313 isl_space_free(space
);
318 __isl_null isl_aff
*isl_aff_free(__isl_take isl_aff
*aff
)
326 isl_local_space_free(aff
->ls
);
327 isl_vec_free(aff
->v
);
334 isl_ctx
*isl_aff_get_ctx(__isl_keep isl_aff
*aff
)
336 return aff
? isl_local_space_get_ctx(aff
->ls
) : NULL
;
339 /* Return a hash value that digests "aff".
341 uint32_t isl_aff_get_hash(__isl_keep isl_aff
*aff
)
343 uint32_t hash
, ls_hash
, v_hash
;
348 hash
= isl_hash_init();
349 ls_hash
= isl_local_space_get_hash(aff
->ls
);
350 isl_hash_hash(hash
, ls_hash
);
351 v_hash
= isl_vec_get_hash(aff
->v
);
352 isl_hash_hash(hash
, v_hash
);
357 /* Return the domain local space of "aff".
359 static __isl_keep isl_local_space
*isl_aff_peek_domain_local_space(
360 __isl_keep isl_aff
*aff
)
362 return aff
? aff
->ls
: NULL
;
365 /* Return the number of variables of the given type in the domain of "aff".
367 isl_size
isl_aff_domain_dim(__isl_keep isl_aff
*aff
, enum isl_dim_type type
)
371 ls
= isl_aff_peek_domain_local_space(aff
);
372 return isl_local_space_dim(ls
, type
);
375 /* Externally, an isl_aff has a map space, but internally, the
376 * ls field corresponds to the domain of that space.
378 isl_size
isl_aff_dim(__isl_keep isl_aff
*aff
, enum isl_dim_type type
)
381 return isl_size_error
;
382 if (type
== isl_dim_out
)
384 if (type
== isl_dim_in
)
386 return isl_aff_domain_dim(aff
, type
);
389 /* Return the offset of the first coefficient of type "type" in
390 * the domain of "aff".
392 isl_size
isl_aff_domain_offset(__isl_keep isl_aff
*aff
, enum isl_dim_type type
)
396 ls
= isl_aff_peek_domain_local_space(aff
);
397 return isl_local_space_offset(ls
, type
);
400 /* Return the position of the dimension of the given type and name
402 * Return -1 if no such dimension can be found.
404 int isl_aff_find_dim_by_name(__isl_keep isl_aff
*aff
, enum isl_dim_type type
,
409 if (type
== isl_dim_out
)
411 if (type
== isl_dim_in
)
413 return isl_local_space_find_dim_by_name(aff
->ls
, type
, name
);
416 /* Return the domain space of "aff".
418 static __isl_keep isl_space
*isl_aff_peek_domain_space(__isl_keep isl_aff
*aff
)
420 return aff
? isl_local_space_peek_space(aff
->ls
) : NULL
;
423 __isl_give isl_space
*isl_aff_get_domain_space(__isl_keep isl_aff
*aff
)
425 return isl_space_copy(isl_aff_peek_domain_space(aff
));
428 __isl_give isl_space
*isl_aff_get_space(__isl_keep isl_aff
*aff
)
433 space
= isl_local_space_get_space(aff
->ls
);
434 space
= isl_space_from_domain(space
);
435 space
= isl_space_add_dims(space
, isl_dim_out
, 1);
439 /* Return a copy of the domain space of "aff".
441 __isl_give isl_local_space
*isl_aff_get_domain_local_space(
442 __isl_keep isl_aff
*aff
)
444 return isl_local_space_copy(isl_aff_peek_domain_local_space(aff
));
447 __isl_give isl_local_space
*isl_aff_get_local_space(__isl_keep isl_aff
*aff
)
452 ls
= isl_local_space_copy(aff
->ls
);
453 ls
= isl_local_space_from_domain(ls
);
454 ls
= isl_local_space_add_dims(ls
, isl_dim_out
, 1);
458 /* Return the local space of the domain of "aff".
459 * This may be either a copy or the local space itself
460 * if there is only one reference to "aff".
461 * This allows the local space to be modified inplace
462 * if both the expression and its local space have only a single reference.
463 * The caller is not allowed to modify "aff" between this call and
464 * a subsequent call to isl_aff_restore_domain_local_space.
465 * The only exception is that isl_aff_free can be called instead.
467 __isl_give isl_local_space
*isl_aff_take_domain_local_space(
468 __isl_keep isl_aff
*aff
)
475 return isl_aff_get_domain_local_space(aff
);
481 /* Set the local space of the domain of "aff" to "ls",
482 * where the local space of "aff" may be missing
483 * due to a preceding call to isl_aff_take_domain_local_space.
484 * However, in this case, "aff" only has a single reference and
485 * then the call to isl_aff_cow has no effect.
487 __isl_give isl_aff
*isl_aff_restore_domain_local_space(
488 __isl_keep isl_aff
*aff
, __isl_take isl_local_space
*ls
)
494 isl_local_space_free(ls
);
498 aff
= isl_aff_cow(aff
);
501 isl_local_space_free(aff
->ls
);
507 isl_local_space_free(ls
);
511 /* Externally, an isl_aff has a map space, but internally, the
512 * ls field corresponds to the domain of that space.
514 const char *isl_aff_get_dim_name(__isl_keep isl_aff
*aff
,
515 enum isl_dim_type type
, unsigned pos
)
519 if (type
== isl_dim_out
)
521 if (type
== isl_dim_in
)
523 return isl_local_space_get_dim_name(aff
->ls
, type
, pos
);
526 __isl_give isl_aff
*isl_aff_reset_domain_space(__isl_take isl_aff
*aff
,
527 __isl_take isl_space
*space
)
529 aff
= isl_aff_cow(aff
);
533 aff
->ls
= isl_local_space_reset_space(aff
->ls
, space
);
535 return isl_aff_free(aff
);
540 isl_space_free(space
);
544 /* Reset the space of "aff". This function is called from isl_pw_templ.c
545 * and doesn't know if the space of an element object is represented
546 * directly or through its domain. It therefore passes along both.
548 __isl_give isl_aff
*isl_aff_reset_space_and_domain(__isl_take isl_aff
*aff
,
549 __isl_take isl_space
*space
, __isl_take isl_space
*domain
)
551 isl_space_free(space
);
552 return isl_aff_reset_domain_space(aff
, domain
);
555 /* Reorder the coefficients of the affine expression based
556 * on the given reordering.
557 * The reordering r is assumed to have been extended with the local
560 static __isl_give isl_vec
*vec_reorder(__isl_take isl_vec
*vec
,
561 __isl_take isl_reordering
*r
, int n_div
)
571 space
= isl_reordering_peek_space(r
);
572 dim
= isl_space_dim(space
, isl_dim_all
);
575 res
= isl_vec_alloc(vec
->ctx
, 2 + dim
+ n_div
);
578 isl_seq_cpy(res
->el
, vec
->el
, 2);
579 isl_seq_clr(res
->el
+ 2, res
->size
- 2);
580 for (i
= 0; i
< r
->len
; ++i
)
581 isl_int_set(res
->el
[2 + r
->pos
[i
]], vec
->el
[2 + i
]);
583 isl_reordering_free(r
);
588 isl_reordering_free(r
);
592 /* Reorder the dimensions of the domain of "aff" according
593 * to the given reordering.
595 __isl_give isl_aff
*isl_aff_realign_domain(__isl_take isl_aff
*aff
,
596 __isl_take isl_reordering
*r
)
598 aff
= isl_aff_cow(aff
);
602 r
= isl_reordering_extend(r
, aff
->ls
->div
->n_row
);
603 aff
->v
= vec_reorder(aff
->v
, isl_reordering_copy(r
),
604 aff
->ls
->div
->n_row
);
605 aff
->ls
= isl_local_space_realign(aff
->ls
, r
);
607 if (!aff
->v
|| !aff
->ls
)
608 return isl_aff_free(aff
);
613 isl_reordering_free(r
);
617 __isl_give isl_aff
*isl_aff_align_params(__isl_take isl_aff
*aff
,
618 __isl_take isl_space
*model
)
620 isl_bool equal_params
;
625 equal_params
= isl_space_has_equal_params(aff
->ls
->dim
, model
);
626 if (equal_params
< 0)
631 exp
= isl_parameter_alignment_reordering(aff
->ls
->dim
, model
);
632 exp
= isl_reordering_extend_space(exp
,
633 isl_aff_get_domain_space(aff
));
634 aff
= isl_aff_realign_domain(aff
, exp
);
637 isl_space_free(model
);
640 isl_space_free(model
);
647 #include "isl_unbind_params_templ.c"
649 /* Is "aff" obviously equal to zero?
651 * If the denominator is zero, then "aff" is not equal to zero.
653 isl_bool
isl_aff_plain_is_zero(__isl_keep isl_aff
*aff
)
658 return isl_bool_error
;
660 if (isl_int_is_zero(aff
->v
->el
[0]))
661 return isl_bool_false
;
662 pos
= isl_seq_first_non_zero(aff
->v
->el
+ 1, aff
->v
->size
- 1);
663 return isl_bool_ok(pos
< 0);
666 /* Does "aff" represent NaN?
668 isl_bool
isl_aff_is_nan(__isl_keep isl_aff
*aff
)
671 return isl_bool_error
;
673 return isl_bool_ok(isl_seq_first_non_zero(aff
->v
->el
, 2) < 0);
676 /* Are "aff1" and "aff2" obviously equal?
678 * NaN is not equal to anything, not even to another NaN.
680 isl_bool
isl_aff_plain_is_equal(__isl_keep isl_aff
*aff1
,
681 __isl_keep isl_aff
*aff2
)
686 return isl_bool_error
;
688 if (isl_aff_is_nan(aff1
) || isl_aff_is_nan(aff2
))
689 return isl_bool_false
;
691 equal
= isl_local_space_is_equal(aff1
->ls
, aff2
->ls
);
692 if (equal
< 0 || !equal
)
695 return isl_vec_is_equal(aff1
->v
, aff2
->v
);
698 /* Return the common denominator of "aff" in "v".
700 * We cannot return anything meaningful in case of a NaN.
702 isl_stat
isl_aff_get_denominator(__isl_keep isl_aff
*aff
, isl_int
*v
)
705 return isl_stat_error
;
706 if (isl_aff_is_nan(aff
))
707 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
708 "cannot get denominator of NaN", return isl_stat_error
);
709 isl_int_set(*v
, aff
->v
->el
[0]);
713 /* Return the common denominator of "aff".
715 __isl_give isl_val
*isl_aff_get_denominator_val(__isl_keep isl_aff
*aff
)
722 ctx
= isl_aff_get_ctx(aff
);
723 if (isl_aff_is_nan(aff
))
724 return isl_val_nan(ctx
);
725 return isl_val_int_from_isl_int(ctx
, aff
->v
->el
[0]);
728 /* Return the constant term of "aff".
730 __isl_give isl_val
*isl_aff_get_constant_val(__isl_keep isl_aff
*aff
)
738 ctx
= isl_aff_get_ctx(aff
);
739 if (isl_aff_is_nan(aff
))
740 return isl_val_nan(ctx
);
741 v
= isl_val_rat_from_isl_int(ctx
, aff
->v
->el
[1], aff
->v
->el
[0]);
742 return isl_val_normalize(v
);
745 /* Return the coefficient of the variable of type "type" at position "pos"
748 __isl_give isl_val
*isl_aff_get_coefficient_val(__isl_keep isl_aff
*aff
,
749 enum isl_dim_type type
, int pos
)
757 ctx
= isl_aff_get_ctx(aff
);
758 if (type
== isl_dim_out
)
759 isl_die(ctx
, isl_error_invalid
,
760 "output/set dimension does not have a coefficient",
762 if (type
== isl_dim_in
)
765 if (isl_local_space_check_range(aff
->ls
, type
, pos
, 1) < 0)
768 if (isl_aff_is_nan(aff
))
769 return isl_val_nan(ctx
);
770 pos
+= isl_local_space_offset(aff
->ls
, type
);
771 v
= isl_val_rat_from_isl_int(ctx
, aff
->v
->el
[1 + pos
], aff
->v
->el
[0]);
772 return isl_val_normalize(v
);
775 /* Return the sign of the coefficient of the variable of type "type"
776 * at position "pos" of "aff".
778 int isl_aff_coefficient_sgn(__isl_keep isl_aff
*aff
, enum isl_dim_type type
,
786 ctx
= isl_aff_get_ctx(aff
);
787 if (type
== isl_dim_out
)
788 isl_die(ctx
, isl_error_invalid
,
789 "output/set dimension does not have a coefficient",
791 if (type
== isl_dim_in
)
794 if (isl_local_space_check_range(aff
->ls
, type
, pos
, 1) < 0)
797 pos
+= isl_local_space_offset(aff
->ls
, type
);
798 return isl_int_sgn(aff
->v
->el
[1 + pos
]);
801 /* Replace the numerator of the constant term of "aff" by "v".
803 * A NaN is unaffected by this operation.
805 __isl_give isl_aff
*isl_aff_set_constant(__isl_take isl_aff
*aff
, isl_int v
)
809 if (isl_aff_is_nan(aff
))
811 aff
= isl_aff_cow(aff
);
815 aff
->v
= isl_vec_cow(aff
->v
);
817 return isl_aff_free(aff
);
819 isl_int_set(aff
->v
->el
[1], v
);
824 /* Replace the constant term of "aff" by "v".
826 * A NaN is unaffected by this operation.
828 __isl_give isl_aff
*isl_aff_set_constant_val(__isl_take isl_aff
*aff
,
829 __isl_take isl_val
*v
)
834 if (isl_aff_is_nan(aff
)) {
839 if (!isl_val_is_rat(v
))
840 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
841 "expecting rational value", goto error
);
843 if (isl_int_eq(aff
->v
->el
[1], v
->n
) &&
844 isl_int_eq(aff
->v
->el
[0], v
->d
)) {
849 aff
= isl_aff_cow(aff
);
852 aff
->v
= isl_vec_cow(aff
->v
);
856 if (isl_int_eq(aff
->v
->el
[0], v
->d
)) {
857 isl_int_set(aff
->v
->el
[1], v
->n
);
858 } else if (isl_int_is_one(v
->d
)) {
859 isl_int_mul(aff
->v
->el
[1], aff
->v
->el
[0], v
->n
);
861 isl_seq_scale(aff
->v
->el
+ 1,
862 aff
->v
->el
+ 1, v
->d
, aff
->v
->size
- 1);
863 isl_int_mul(aff
->v
->el
[1], aff
->v
->el
[0], v
->n
);
864 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], v
->d
);
865 aff
->v
= isl_vec_normalize(aff
->v
);
878 /* Add "v" to the constant term of "aff".
880 * A NaN is unaffected by this operation.
882 __isl_give isl_aff
*isl_aff_add_constant(__isl_take isl_aff
*aff
, isl_int v
)
884 if (isl_int_is_zero(v
))
889 if (isl_aff_is_nan(aff
))
891 aff
= isl_aff_cow(aff
);
895 aff
->v
= isl_vec_cow(aff
->v
);
897 return isl_aff_free(aff
);
899 isl_int_addmul(aff
->v
->el
[1], aff
->v
->el
[0], v
);
904 /* Add "v" to the constant term of "aff".
906 * A NaN is unaffected by this operation.
908 __isl_give isl_aff
*isl_aff_add_constant_val(__isl_take isl_aff
*aff
,
909 __isl_take isl_val
*v
)
914 if (isl_aff_is_nan(aff
) || isl_val_is_zero(v
)) {
919 if (!isl_val_is_rat(v
))
920 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
921 "expecting rational value", goto error
);
923 aff
= isl_aff_cow(aff
);
927 aff
->v
= isl_vec_cow(aff
->v
);
931 if (isl_int_is_one(v
->d
)) {
932 isl_int_addmul(aff
->v
->el
[1], aff
->v
->el
[0], v
->n
);
933 } else if (isl_int_eq(aff
->v
->el
[0], v
->d
)) {
934 isl_int_add(aff
->v
->el
[1], aff
->v
->el
[1], v
->n
);
935 aff
->v
= isl_vec_normalize(aff
->v
);
939 isl_seq_scale(aff
->v
->el
+ 1,
940 aff
->v
->el
+ 1, v
->d
, aff
->v
->size
- 1);
941 isl_int_addmul(aff
->v
->el
[1], aff
->v
->el
[0], v
->n
);
942 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], v
->d
);
943 aff
->v
= isl_vec_normalize(aff
->v
);
956 __isl_give isl_aff
*isl_aff_add_constant_si(__isl_take isl_aff
*aff
, int v
)
961 isl_int_set_si(t
, v
);
962 aff
= isl_aff_add_constant(aff
, t
);
968 /* Add "v" to the numerator of the constant term of "aff".
970 * A NaN is unaffected by this operation.
972 __isl_give isl_aff
*isl_aff_add_constant_num(__isl_take isl_aff
*aff
, isl_int v
)
974 if (isl_int_is_zero(v
))
979 if (isl_aff_is_nan(aff
))
981 aff
= isl_aff_cow(aff
);
985 aff
->v
= isl_vec_cow(aff
->v
);
987 return isl_aff_free(aff
);
989 isl_int_add(aff
->v
->el
[1], aff
->v
->el
[1], v
);
994 /* Add "v" to the numerator of the constant term of "aff".
996 * A NaN is unaffected by this operation.
998 __isl_give isl_aff
*isl_aff_add_constant_num_si(__isl_take isl_aff
*aff
, int v
)
1006 isl_int_set_si(t
, v
);
1007 aff
= isl_aff_add_constant_num(aff
, t
);
1013 /* Replace the numerator of the constant term of "aff" by "v".
1015 * A NaN is unaffected by this operation.
1017 __isl_give isl_aff
*isl_aff_set_constant_si(__isl_take isl_aff
*aff
, int v
)
1021 if (isl_aff_is_nan(aff
))
1023 aff
= isl_aff_cow(aff
);
1027 aff
->v
= isl_vec_cow(aff
->v
);
1029 return isl_aff_free(aff
);
1031 isl_int_set_si(aff
->v
->el
[1], v
);
1036 /* Replace the numerator of the coefficient of the variable of type "type"
1037 * at position "pos" of "aff" by "v".
1039 * A NaN is unaffected by this operation.
1041 __isl_give isl_aff
*isl_aff_set_coefficient(__isl_take isl_aff
*aff
,
1042 enum isl_dim_type type
, int pos
, isl_int v
)
1047 if (type
== isl_dim_out
)
1048 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1049 "output/set dimension does not have a coefficient",
1050 return isl_aff_free(aff
));
1051 if (type
== isl_dim_in
)
1054 if (isl_local_space_check_range(aff
->ls
, type
, pos
, 1) < 0)
1055 return isl_aff_free(aff
);
1057 if (isl_aff_is_nan(aff
))
1059 aff
= isl_aff_cow(aff
);
1063 aff
->v
= isl_vec_cow(aff
->v
);
1065 return isl_aff_free(aff
);
1067 pos
+= isl_local_space_offset(aff
->ls
, type
);
1068 isl_int_set(aff
->v
->el
[1 + pos
], v
);
1073 /* Replace the numerator of the coefficient of the variable of type "type"
1074 * at position "pos" of "aff" by "v".
1076 * A NaN is unaffected by this operation.
1078 __isl_give isl_aff
*isl_aff_set_coefficient_si(__isl_take isl_aff
*aff
,
1079 enum isl_dim_type type
, int pos
, int v
)
1084 if (type
== isl_dim_out
)
1085 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1086 "output/set dimension does not have a coefficient",
1087 return isl_aff_free(aff
));
1088 if (type
== isl_dim_in
)
1091 if (isl_local_space_check_range(aff
->ls
, type
, pos
, 1) < 0)
1092 return isl_aff_free(aff
);
1094 if (isl_aff_is_nan(aff
))
1096 pos
+= isl_local_space_offset(aff
->ls
, type
);
1097 if (isl_int_cmp_si(aff
->v
->el
[1 + pos
], v
) == 0)
1100 aff
= isl_aff_cow(aff
);
1104 aff
->v
= isl_vec_cow(aff
->v
);
1106 return isl_aff_free(aff
);
1108 isl_int_set_si(aff
->v
->el
[1 + pos
], v
);
1113 /* Replace the coefficient of the variable of type "type" at position "pos"
1116 * A NaN is unaffected by this operation.
1118 __isl_give isl_aff
*isl_aff_set_coefficient_val(__isl_take isl_aff
*aff
,
1119 enum isl_dim_type type
, int pos
, __isl_take isl_val
*v
)
1124 if (type
== isl_dim_out
)
1125 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1126 "output/set dimension does not have a coefficient",
1128 if (type
== isl_dim_in
)
1131 if (isl_local_space_check_range(aff
->ls
, type
, pos
, 1) < 0)
1132 return isl_aff_free(aff
);
1134 if (isl_aff_is_nan(aff
)) {
1138 if (!isl_val_is_rat(v
))
1139 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1140 "expecting rational value", goto error
);
1142 pos
+= isl_local_space_offset(aff
->ls
, type
);
1143 if (isl_int_eq(aff
->v
->el
[1 + pos
], v
->n
) &&
1144 isl_int_eq(aff
->v
->el
[0], v
->d
)) {
1149 aff
= isl_aff_cow(aff
);
1152 aff
->v
= isl_vec_cow(aff
->v
);
1156 if (isl_int_eq(aff
->v
->el
[0], v
->d
)) {
1157 isl_int_set(aff
->v
->el
[1 + pos
], v
->n
);
1158 } else if (isl_int_is_one(v
->d
)) {
1159 isl_int_mul(aff
->v
->el
[1 + pos
], aff
->v
->el
[0], v
->n
);
1161 isl_seq_scale(aff
->v
->el
+ 1,
1162 aff
->v
->el
+ 1, v
->d
, aff
->v
->size
- 1);
1163 isl_int_mul(aff
->v
->el
[1 + pos
], aff
->v
->el
[0], v
->n
);
1164 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], v
->d
);
1165 aff
->v
= isl_vec_normalize(aff
->v
);
1178 /* Add "v" to the coefficient of the variable of type "type"
1179 * at position "pos" of "aff".
1181 * A NaN is unaffected by this operation.
1183 __isl_give isl_aff
*isl_aff_add_coefficient(__isl_take isl_aff
*aff
,
1184 enum isl_dim_type type
, int pos
, isl_int v
)
1189 if (type
== isl_dim_out
)
1190 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1191 "output/set dimension does not have a coefficient",
1192 return isl_aff_free(aff
));
1193 if (type
== isl_dim_in
)
1196 if (isl_local_space_check_range(aff
->ls
, type
, pos
, 1) < 0)
1197 return isl_aff_free(aff
);
1199 if (isl_aff_is_nan(aff
))
1201 aff
= isl_aff_cow(aff
);
1205 aff
->v
= isl_vec_cow(aff
->v
);
1207 return isl_aff_free(aff
);
1209 pos
+= isl_local_space_offset(aff
->ls
, type
);
1210 isl_int_addmul(aff
->v
->el
[1 + pos
], aff
->v
->el
[0], v
);
1215 /* Add "v" to the coefficient of the variable of type "type"
1216 * at position "pos" of "aff".
1218 * A NaN is unaffected by this operation.
1220 __isl_give isl_aff
*isl_aff_add_coefficient_val(__isl_take isl_aff
*aff
,
1221 enum isl_dim_type type
, int pos
, __isl_take isl_val
*v
)
1226 if (isl_val_is_zero(v
)) {
1231 if (type
== isl_dim_out
)
1232 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1233 "output/set dimension does not have a coefficient",
1235 if (type
== isl_dim_in
)
1238 if (isl_local_space_check_range(aff
->ls
, type
, pos
, 1) < 0)
1241 if (isl_aff_is_nan(aff
)) {
1245 if (!isl_val_is_rat(v
))
1246 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1247 "expecting rational value", goto error
);
1249 aff
= isl_aff_cow(aff
);
1253 aff
->v
= isl_vec_cow(aff
->v
);
1257 pos
+= isl_local_space_offset(aff
->ls
, type
);
1258 if (isl_int_is_one(v
->d
)) {
1259 isl_int_addmul(aff
->v
->el
[1 + pos
], aff
->v
->el
[0], v
->n
);
1260 } else if (isl_int_eq(aff
->v
->el
[0], v
->d
)) {
1261 isl_int_add(aff
->v
->el
[1 + pos
], aff
->v
->el
[1 + pos
], v
->n
);
1262 aff
->v
= isl_vec_normalize(aff
->v
);
1266 isl_seq_scale(aff
->v
->el
+ 1,
1267 aff
->v
->el
+ 1, v
->d
, aff
->v
->size
- 1);
1268 isl_int_addmul(aff
->v
->el
[1 + pos
], aff
->v
->el
[0], v
->n
);
1269 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], v
->d
);
1270 aff
->v
= isl_vec_normalize(aff
->v
);
1283 __isl_give isl_aff
*isl_aff_add_coefficient_si(__isl_take isl_aff
*aff
,
1284 enum isl_dim_type type
, int pos
, int v
)
1289 isl_int_set_si(t
, v
);
1290 aff
= isl_aff_add_coefficient(aff
, type
, pos
, t
);
1296 __isl_give isl_aff
*isl_aff_get_div(__isl_keep isl_aff
*aff
, int pos
)
1301 return isl_local_space_get_div(aff
->ls
, pos
);
1304 /* Return the negation of "aff".
1306 * As a special case, -NaN = NaN.
1308 __isl_give isl_aff
*isl_aff_neg(__isl_take isl_aff
*aff
)
1312 if (isl_aff_is_nan(aff
))
1314 aff
= isl_aff_cow(aff
);
1317 aff
->v
= isl_vec_cow(aff
->v
);
1319 return isl_aff_free(aff
);
1321 isl_seq_neg(aff
->v
->el
+ 1, aff
->v
->el
+ 1, aff
->v
->size
- 1);
1326 /* Remove divs from the local space that do not appear in the affine
1328 * We currently only remove divs at the end.
1329 * Some intermediate divs may also not appear directly in the affine
1330 * expression, but we would also need to check that no other divs are
1331 * defined in terms of them.
1333 __isl_give isl_aff
*isl_aff_remove_unused_divs(__isl_take isl_aff
*aff
)
1339 n
= isl_aff_domain_dim(aff
, isl_dim_div
);
1340 off
= isl_aff_domain_offset(aff
, isl_dim_div
);
1341 if (n
< 0 || off
< 0)
1342 return isl_aff_free(aff
);
1344 pos
= isl_seq_last_non_zero(aff
->v
->el
+ 1 + off
, n
) + 1;
1348 aff
= isl_aff_cow(aff
);
1352 aff
->ls
= isl_local_space_drop_dims(aff
->ls
, isl_dim_div
, pos
, n
- pos
);
1353 aff
->v
= isl_vec_drop_els(aff
->v
, 1 + off
+ pos
, n
- pos
);
1354 if (!aff
->ls
|| !aff
->v
)
1355 return isl_aff_free(aff
);
1360 /* Look for any divs in the aff->ls with a denominator equal to one
1361 * and plug them into the affine expression and any subsequent divs
1362 * that may reference the div.
1364 static __isl_give isl_aff
*plug_in_integral_divs(__isl_take isl_aff
*aff
)
1371 isl_local_space
*ls
;
1374 n
= isl_aff_domain_dim(aff
, isl_dim_div
);
1375 off
= isl_aff_domain_offset(aff
, isl_dim_div
);
1376 if (n
< 0 || off
< 0)
1377 return isl_aff_free(aff
);
1379 for (i
= 0; i
< n
; ++i
) {
1380 if (!isl_int_is_one(aff
->ls
->div
->row
[i
][0]))
1382 ls
= isl_local_space_copy(aff
->ls
);
1383 ls
= isl_local_space_substitute_seq(ls
, isl_dim_div
, i
,
1384 aff
->ls
->div
->row
[i
], len
, i
+ 1, n
- (i
+ 1));
1385 vec
= isl_vec_copy(aff
->v
);
1386 vec
= isl_vec_cow(vec
);
1392 isl_seq_substitute(vec
->el
, off
+ i
, aff
->ls
->div
->row
[i
],
1397 isl_vec_free(aff
->v
);
1399 isl_local_space_free(aff
->ls
);
1406 isl_local_space_free(ls
);
1407 return isl_aff_free(aff
);
1410 /* Look for any divs j that appear with a unit coefficient inside
1411 * the definitions of other divs i and plug them into the definitions
1414 * In particular, an expression of the form
1416 * floor((f(..) + floor(g(..)/n))/m)
1420 * floor((n * f(..) + g(..))/(n * m))
1422 * This simplification is correct because we can move the expression
1423 * f(..) into the inner floor in the original expression to obtain
1425 * floor(floor((n * f(..) + g(..))/n)/m)
1427 * from which we can derive the simplified expression.
1429 static __isl_give isl_aff
*plug_in_unit_divs(__isl_take isl_aff
*aff
)
1435 n
= isl_aff_domain_dim(aff
, isl_dim_div
);
1436 off
= isl_aff_domain_offset(aff
, isl_dim_div
);
1437 if (n
< 0 || off
< 0)
1438 return isl_aff_free(aff
);
1439 for (i
= 1; i
< n
; ++i
) {
1440 for (j
= 0; j
< i
; ++j
) {
1441 if (!isl_int_is_one(aff
->ls
->div
->row
[i
][1 + off
+ j
]))
1443 aff
->ls
= isl_local_space_substitute_seq(aff
->ls
,
1444 isl_dim_div
, j
, aff
->ls
->div
->row
[j
],
1445 aff
->v
->size
, i
, 1);
1447 return isl_aff_free(aff
);
1454 /* Swap divs "a" and "b" in "aff", which is assumed to be non-NULL.
1456 * Even though this function is only called on isl_affs with a single
1457 * reference, we are careful to only change aff->v and aff->ls together.
1459 static __isl_give isl_aff
*swap_div(__isl_take isl_aff
*aff
, int a
, int b
)
1461 isl_size off
= isl_aff_domain_offset(aff
, isl_dim_div
);
1462 isl_local_space
*ls
;
1466 return isl_aff_free(aff
);
1468 ls
= isl_local_space_copy(aff
->ls
);
1469 ls
= isl_local_space_swap_div(ls
, a
, b
);
1470 v
= isl_vec_copy(aff
->v
);
1475 isl_int_swap(v
->el
[1 + off
+ a
], v
->el
[1 + off
+ b
]);
1476 isl_vec_free(aff
->v
);
1478 isl_local_space_free(aff
->ls
);
1484 isl_local_space_free(ls
);
1485 return isl_aff_free(aff
);
1488 /* Merge divs "a" and "b" in "aff", which is assumed to be non-NULL.
1490 * We currently do not actually remove div "b", but simply add its
1491 * coefficient to that of "a" and then zero it out.
1493 static __isl_give isl_aff
*merge_divs(__isl_take isl_aff
*aff
, int a
, int b
)
1495 isl_size off
= isl_aff_domain_offset(aff
, isl_dim_div
);
1498 return isl_aff_free(aff
);
1500 if (isl_int_is_zero(aff
->v
->el
[1 + off
+ b
]))
1503 aff
->v
= isl_vec_cow(aff
->v
);
1505 return isl_aff_free(aff
);
1507 isl_int_add(aff
->v
->el
[1 + off
+ a
],
1508 aff
->v
->el
[1 + off
+ a
], aff
->v
->el
[1 + off
+ b
]);
1509 isl_int_set_si(aff
->v
->el
[1 + off
+ b
], 0);
1514 /* Sort the divs in the local space of "aff" according to
1515 * the comparison function "cmp_row" in isl_local_space.c,
1516 * combining the coefficients of identical divs.
1518 * Reordering divs does not change the semantics of "aff",
1519 * so there is no need to call isl_aff_cow.
1520 * Moreover, this function is currently only called on isl_affs
1521 * with a single reference.
1523 static __isl_give isl_aff
*sort_divs(__isl_take isl_aff
*aff
)
1528 n
= isl_aff_dim(aff
, isl_dim_div
);
1530 return isl_aff_free(aff
);
1531 for (i
= 1; i
< n
; ++i
) {
1532 for (j
= i
- 1; j
>= 0; --j
) {
1533 int cmp
= isl_mat_cmp_div(aff
->ls
->div
, j
, j
+ 1);
1537 aff
= merge_divs(aff
, j
, j
+ 1);
1539 aff
= swap_div(aff
, j
, j
+ 1);
1548 /* Normalize the representation of "aff".
1550 * This function should only be called on "new" isl_affs, i.e.,
1551 * with only a single reference. We therefore do not need to
1552 * worry about affecting other instances.
1554 __isl_give isl_aff
*isl_aff_normalize(__isl_take isl_aff
*aff
)
1558 aff
->v
= isl_vec_normalize(aff
->v
);
1560 return isl_aff_free(aff
);
1561 aff
= plug_in_integral_divs(aff
);
1562 aff
= plug_in_unit_divs(aff
);
1563 aff
= sort_divs(aff
);
1564 aff
= isl_aff_remove_unused_divs(aff
);
1568 /* Given f, return floor(f).
1569 * If f is an integer expression, then just return f.
1570 * If f is a constant, then return the constant floor(f).
1571 * Otherwise, if f = g/m, write g = q m + r,
1572 * create a new div d = [r/m] and return the expression q + d.
1573 * The coefficients in r are taken to lie between -m/2 and m/2.
1575 * reduce_div_coefficients performs the same normalization.
1577 * As a special case, floor(NaN) = NaN.
1579 __isl_give isl_aff
*isl_aff_floor(__isl_take isl_aff
*aff
)
1589 if (isl_aff_is_nan(aff
))
1591 if (isl_int_is_one(aff
->v
->el
[0]))
1594 aff
= isl_aff_cow(aff
);
1598 aff
->v
= isl_vec_cow(aff
->v
);
1600 return isl_aff_free(aff
);
1602 if (isl_aff_is_cst(aff
)) {
1603 isl_int_fdiv_q(aff
->v
->el
[1], aff
->v
->el
[1], aff
->v
->el
[0]);
1604 isl_int_set_si(aff
->v
->el
[0], 1);
1608 div
= isl_vec_copy(aff
->v
);
1609 div
= isl_vec_cow(div
);
1611 return isl_aff_free(aff
);
1613 ctx
= isl_aff_get_ctx(aff
);
1614 isl_int_fdiv_q(aff
->v
->el
[0], aff
->v
->el
[0], ctx
->two
);
1615 for (i
= 1; i
< aff
->v
->size
; ++i
) {
1616 isl_int_fdiv_r(div
->el
[i
], div
->el
[i
], div
->el
[0]);
1617 isl_int_fdiv_q(aff
->v
->el
[i
], aff
->v
->el
[i
], div
->el
[0]);
1618 if (isl_int_gt(div
->el
[i
], aff
->v
->el
[0])) {
1619 isl_int_sub(div
->el
[i
], div
->el
[i
], div
->el
[0]);
1620 isl_int_add_ui(aff
->v
->el
[i
], aff
->v
->el
[i
], 1);
1624 aff
->ls
= isl_local_space_add_div(aff
->ls
, div
);
1626 return isl_aff_free(aff
);
1628 size
= aff
->v
->size
;
1629 aff
->v
= isl_vec_extend(aff
->v
, size
+ 1);
1631 return isl_aff_free(aff
);
1632 isl_int_set_si(aff
->v
->el
[0], 1);
1633 isl_int_set_si(aff
->v
->el
[size
], 1);
1635 aff
= isl_aff_normalize(aff
);
1642 * aff mod m = aff - m * floor(aff/m)
1644 * with m an integer value.
1646 __isl_give isl_aff
*isl_aff_mod_val(__isl_take isl_aff
*aff
,
1647 __isl_take isl_val
*m
)
1654 if (!isl_val_is_int(m
))
1655 isl_die(isl_val_get_ctx(m
), isl_error_invalid
,
1656 "expecting integer modulo", goto error
);
1658 res
= isl_aff_copy(aff
);
1659 aff
= isl_aff_scale_down_val(aff
, isl_val_copy(m
));
1660 aff
= isl_aff_floor(aff
);
1661 aff
= isl_aff_scale_val(aff
, m
);
1662 res
= isl_aff_sub(res
, aff
);
1673 * pwaff mod m = pwaff - m * floor(pwaff/m)
1675 __isl_give isl_pw_aff
*isl_pw_aff_mod(__isl_take isl_pw_aff
*pwaff
, isl_int m
)
1679 res
= isl_pw_aff_copy(pwaff
);
1680 pwaff
= isl_pw_aff_scale_down(pwaff
, m
);
1681 pwaff
= isl_pw_aff_floor(pwaff
);
1682 pwaff
= isl_pw_aff_scale(pwaff
, m
);
1683 res
= isl_pw_aff_sub(res
, pwaff
);
1690 * pa mod m = pa - m * floor(pa/m)
1692 * with m an integer value.
1694 __isl_give isl_pw_aff
*isl_pw_aff_mod_val(__isl_take isl_pw_aff
*pa
,
1695 __isl_take isl_val
*m
)
1699 if (!isl_val_is_int(m
))
1700 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
1701 "expecting integer modulo", goto error
);
1702 pa
= isl_pw_aff_mod(pa
, m
->n
);
1706 isl_pw_aff_free(pa
);
1711 /* Given f, return ceil(f).
1712 * If f is an integer expression, then just return f.
1713 * Otherwise, let f be the expression
1719 * floor((e + m - 1)/m)
1721 * As a special case, ceil(NaN) = NaN.
1723 __isl_give isl_aff
*isl_aff_ceil(__isl_take isl_aff
*aff
)
1728 if (isl_aff_is_nan(aff
))
1730 if (isl_int_is_one(aff
->v
->el
[0]))
1733 aff
= isl_aff_cow(aff
);
1736 aff
->v
= isl_vec_cow(aff
->v
);
1738 return isl_aff_free(aff
);
1740 isl_int_add(aff
->v
->el
[1], aff
->v
->el
[1], aff
->v
->el
[0]);
1741 isl_int_sub_ui(aff
->v
->el
[1], aff
->v
->el
[1], 1);
1742 aff
= isl_aff_floor(aff
);
1747 /* Apply the expansion computed by isl_merge_divs.
1748 * The expansion itself is given by "exp" while the resulting
1749 * list of divs is given by "div".
1751 __isl_give isl_aff
*isl_aff_expand_divs(__isl_take isl_aff
*aff
,
1752 __isl_take isl_mat
*div
, int *exp
)
1758 aff
= isl_aff_cow(aff
);
1760 offset
= isl_aff_domain_offset(aff
, isl_dim_div
);
1761 old_n_div
= isl_aff_domain_dim(aff
, isl_dim_div
);
1762 new_n_div
= isl_mat_rows(div
);
1763 if (offset
< 0 || old_n_div
< 0 || new_n_div
< 0)
1766 aff
->v
= isl_vec_expand(aff
->v
, 1 + offset
, old_n_div
, exp
, new_n_div
);
1767 aff
->ls
= isl_local_space_replace_divs(aff
->ls
, div
);
1768 if (!aff
->v
|| !aff
->ls
)
1769 return isl_aff_free(aff
);
1777 /* Add two affine expressions that live in the same local space.
1779 static __isl_give isl_aff
*add_expanded(__isl_take isl_aff
*aff1
,
1780 __isl_take isl_aff
*aff2
)
1784 aff1
= isl_aff_cow(aff1
);
1788 aff1
->v
= isl_vec_cow(aff1
->v
);
1794 isl_int_gcd(gcd
, aff1
->v
->el
[0], aff2
->v
->el
[0]);
1795 isl_int_divexact(f
, aff2
->v
->el
[0], gcd
);
1796 isl_seq_scale(aff1
->v
->el
+ 1, aff1
->v
->el
+ 1, f
, aff1
->v
->size
- 1);
1797 isl_int_divexact(f
, aff1
->v
->el
[0], gcd
);
1798 isl_seq_addmul(aff1
->v
->el
+ 1, f
, aff2
->v
->el
+ 1, aff1
->v
->size
- 1);
1799 isl_int_divexact(f
, aff2
->v
->el
[0], gcd
);
1800 isl_int_mul(aff1
->v
->el
[0], aff1
->v
->el
[0], f
);
1805 aff1
= isl_aff_normalize(aff1
);
1813 /* Replace one of the arguments by a NaN and free the other one.
1815 static __isl_give isl_aff
*set_nan_free(__isl_take isl_aff
*aff1
,
1816 __isl_take isl_aff
*aff2
)
1819 return isl_aff_set_nan(aff1
);
1822 /* Return the sum of "aff1" and "aff2".
1824 * If either of the two is NaN, then the result is NaN.
1826 __isl_give isl_aff
*isl_aff_add(__isl_take isl_aff
*aff1
,
1827 __isl_take isl_aff
*aff2
)
1833 isl_size n_div1
, n_div2
;
1838 ctx
= isl_aff_get_ctx(aff1
);
1839 if (!isl_space_is_equal(aff1
->ls
->dim
, aff2
->ls
->dim
))
1840 isl_die(ctx
, isl_error_invalid
,
1841 "spaces don't match", goto error
);
1843 if (isl_aff_is_nan(aff1
)) {
1847 if (isl_aff_is_nan(aff2
)) {
1852 n_div1
= isl_aff_dim(aff1
, isl_dim_div
);
1853 n_div2
= isl_aff_dim(aff2
, isl_dim_div
);
1854 if (n_div1
< 0 || n_div2
< 0)
1856 if (n_div1
== 0 && n_div2
== 0)
1857 return add_expanded(aff1
, aff2
);
1859 exp1
= isl_alloc_array(ctx
, int, n_div1
);
1860 exp2
= isl_alloc_array(ctx
, int, n_div2
);
1861 if ((n_div1
&& !exp1
) || (n_div2
&& !exp2
))
1864 div
= isl_merge_divs(aff1
->ls
->div
, aff2
->ls
->div
, exp1
, exp2
);
1865 aff1
= isl_aff_expand_divs(aff1
, isl_mat_copy(div
), exp1
);
1866 aff2
= isl_aff_expand_divs(aff2
, div
, exp2
);
1870 return add_expanded(aff1
, aff2
);
1879 __isl_give isl_aff
*isl_aff_sub(__isl_take isl_aff
*aff1
,
1880 __isl_take isl_aff
*aff2
)
1882 return isl_aff_add(aff1
, isl_aff_neg(aff2
));
1885 /* Return the result of scaling "aff" by a factor of "f".
1887 * As a special case, f * NaN = NaN.
1889 __isl_give isl_aff
*isl_aff_scale(__isl_take isl_aff
*aff
, isl_int f
)
1895 if (isl_aff_is_nan(aff
))
1898 if (isl_int_is_one(f
))
1901 aff
= isl_aff_cow(aff
);
1904 aff
->v
= isl_vec_cow(aff
->v
);
1906 return isl_aff_free(aff
);
1908 if (isl_int_is_pos(f
) && isl_int_is_divisible_by(aff
->v
->el
[0], f
)) {
1909 isl_int_divexact(aff
->v
->el
[0], aff
->v
->el
[0], f
);
1914 isl_int_gcd(gcd
, aff
->v
->el
[0], f
);
1915 isl_int_divexact(aff
->v
->el
[0], aff
->v
->el
[0], gcd
);
1916 isl_int_divexact(gcd
, f
, gcd
);
1917 isl_seq_scale(aff
->v
->el
+ 1, aff
->v
->el
+ 1, gcd
, aff
->v
->size
- 1);
1923 /* Multiple "aff" by "v".
1925 __isl_give isl_aff
*isl_aff_scale_val(__isl_take isl_aff
*aff
,
1926 __isl_take isl_val
*v
)
1931 if (isl_val_is_one(v
)) {
1936 if (!isl_val_is_rat(v
))
1937 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1938 "expecting rational factor", goto error
);
1940 aff
= isl_aff_scale(aff
, v
->n
);
1941 aff
= isl_aff_scale_down(aff
, v
->d
);
1951 /* Return the result of scaling "aff" down by a factor of "f".
1953 * As a special case, NaN/f = NaN.
1955 __isl_give isl_aff
*isl_aff_scale_down(__isl_take isl_aff
*aff
, isl_int f
)
1961 if (isl_aff_is_nan(aff
))
1964 if (isl_int_is_one(f
))
1967 aff
= isl_aff_cow(aff
);
1971 if (isl_int_is_zero(f
))
1972 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1973 "cannot scale down by zero", return isl_aff_free(aff
));
1975 aff
->v
= isl_vec_cow(aff
->v
);
1977 return isl_aff_free(aff
);
1980 isl_seq_gcd(aff
->v
->el
+ 1, aff
->v
->size
- 1, &gcd
);
1981 isl_int_gcd(gcd
, gcd
, f
);
1982 isl_seq_scale_down(aff
->v
->el
+ 1, aff
->v
->el
+ 1, gcd
, aff
->v
->size
- 1);
1983 isl_int_divexact(gcd
, f
, gcd
);
1984 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], gcd
);
1990 /* Divide "aff" by "v".
1992 __isl_give isl_aff
*isl_aff_scale_down_val(__isl_take isl_aff
*aff
,
1993 __isl_take isl_val
*v
)
1998 if (isl_val_is_one(v
)) {
2003 if (!isl_val_is_rat(v
))
2004 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2005 "expecting rational factor", goto error
);
2006 if (!isl_val_is_pos(v
))
2007 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2008 "factor needs to be positive", goto error
);
2010 aff
= isl_aff_scale(aff
, v
->d
);
2011 aff
= isl_aff_scale_down(aff
, v
->n
);
2021 __isl_give isl_aff
*isl_aff_scale_down_ui(__isl_take isl_aff
*aff
, unsigned f
)
2029 isl_int_set_ui(v
, f
);
2030 aff
= isl_aff_scale_down(aff
, v
);
2036 __isl_give isl_aff
*isl_aff_set_dim_name(__isl_take isl_aff
*aff
,
2037 enum isl_dim_type type
, unsigned pos
, const char *s
)
2039 aff
= isl_aff_cow(aff
);
2042 if (type
== isl_dim_out
)
2043 isl_die(aff
->v
->ctx
, isl_error_invalid
,
2044 "cannot set name of output/set dimension",
2045 return isl_aff_free(aff
));
2046 if (type
== isl_dim_in
)
2048 aff
->ls
= isl_local_space_set_dim_name(aff
->ls
, type
, pos
, s
);
2050 return isl_aff_free(aff
);
2055 __isl_give isl_aff
*isl_aff_set_dim_id(__isl_take isl_aff
*aff
,
2056 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
2058 aff
= isl_aff_cow(aff
);
2061 if (type
== isl_dim_out
)
2062 isl_die(aff
->v
->ctx
, isl_error_invalid
,
2063 "cannot set name of output/set dimension",
2065 if (type
== isl_dim_in
)
2067 aff
->ls
= isl_local_space_set_dim_id(aff
->ls
, type
, pos
, id
);
2069 return isl_aff_free(aff
);
2078 /* Replace the identifier of the input tuple of "aff" by "id".
2079 * type is currently required to be equal to isl_dim_in
2081 __isl_give isl_aff
*isl_aff_set_tuple_id(__isl_take isl_aff
*aff
,
2082 enum isl_dim_type type
, __isl_take isl_id
*id
)
2084 aff
= isl_aff_cow(aff
);
2087 if (type
!= isl_dim_in
)
2088 isl_die(aff
->v
->ctx
, isl_error_invalid
,
2089 "cannot only set id of input tuple", goto error
);
2090 aff
->ls
= isl_local_space_set_tuple_id(aff
->ls
, isl_dim_set
, id
);
2092 return isl_aff_free(aff
);
2101 /* Exploit the equalities in "eq" to simplify the affine expression
2102 * and the expressions of the integer divisions in the local space.
2103 * The integer divisions in this local space are assumed to appear
2104 * as regular dimensions in "eq".
2106 static __isl_give isl_aff
*isl_aff_substitute_equalities_lifted(
2107 __isl_take isl_aff
*aff
, __isl_take isl_basic_set
*eq
)
2115 if (eq
->n_eq
== 0) {
2116 isl_basic_set_free(eq
);
2120 aff
= isl_aff_cow(aff
);
2124 aff
->ls
= isl_local_space_substitute_equalities(aff
->ls
,
2125 isl_basic_set_copy(eq
));
2126 aff
->v
= isl_vec_cow(aff
->v
);
2127 if (!aff
->ls
|| !aff
->v
)
2130 o_div
= isl_basic_set_offset(eq
, isl_dim_div
);
2132 for (i
= 0; i
< eq
->n_eq
; ++i
) {
2133 j
= isl_seq_last_non_zero(eq
->eq
[i
], o_div
+ n_div
);
2134 if (j
< 0 || j
== 0 || j
>= o_div
)
2137 isl_seq_elim(aff
->v
->el
+ 1, eq
->eq
[i
], j
, o_div
,
2141 isl_basic_set_free(eq
);
2142 aff
= isl_aff_normalize(aff
);
2145 isl_basic_set_free(eq
);
2150 /* Exploit the equalities in "eq" to simplify the affine expression
2151 * and the expressions of the integer divisions in the local space.
2153 __isl_give isl_aff
*isl_aff_substitute_equalities(__isl_take isl_aff
*aff
,
2154 __isl_take isl_basic_set
*eq
)
2158 n_div
= isl_aff_domain_dim(aff
, isl_dim_div
);
2162 eq
= isl_basic_set_add_dims(eq
, isl_dim_set
, n_div
);
2163 return isl_aff_substitute_equalities_lifted(aff
, eq
);
2165 isl_basic_set_free(eq
);
2170 /* Look for equalities among the variables shared by context and aff
2171 * and the integer divisions of aff, if any.
2172 * The equalities are then used to eliminate coefficients and/or integer
2173 * divisions from aff.
2175 __isl_give isl_aff
*isl_aff_gist(__isl_take isl_aff
*aff
,
2176 __isl_take isl_set
*context
)
2178 isl_local_space
*ls
;
2179 isl_basic_set
*hull
;
2181 ls
= isl_aff_get_domain_local_space(aff
);
2182 context
= isl_local_space_lift_set(ls
, context
);
2184 hull
= isl_set_affine_hull(context
);
2185 return isl_aff_substitute_equalities_lifted(aff
, hull
);
2188 __isl_give isl_aff
*isl_aff_gist_params(__isl_take isl_aff
*aff
,
2189 __isl_take isl_set
*context
)
2191 isl_set
*dom_context
= isl_set_universe(isl_aff_get_domain_space(aff
));
2192 dom_context
= isl_set_intersect_params(dom_context
, context
);
2193 return isl_aff_gist(aff
, dom_context
);
2196 /* Return a basic set containing those elements in the space
2197 * of aff where it is positive. "rational" should not be set.
2199 * If "aff" is NaN, then it is not positive.
2201 static __isl_give isl_basic_set
*aff_pos_basic_set(__isl_take isl_aff
*aff
,
2202 int rational
, void *user
)
2204 isl_constraint
*ineq
;
2205 isl_basic_set
*bset
;
2210 if (isl_aff_is_nan(aff
)) {
2211 isl_space
*space
= isl_aff_get_domain_space(aff
);
2213 return isl_basic_set_empty(space
);
2216 isl_die(isl_aff_get_ctx(aff
), isl_error_unsupported
,
2217 "rational sets not supported", goto error
);
2219 ineq
= isl_inequality_from_aff(aff
);
2220 c
= isl_constraint_get_constant_val(ineq
);
2221 c
= isl_val_sub_ui(c
, 1);
2222 ineq
= isl_constraint_set_constant_val(ineq
, c
);
2224 bset
= isl_basic_set_from_constraint(ineq
);
2225 bset
= isl_basic_set_simplify(bset
);
2232 /* Return a basic set containing those elements in the space
2233 * of aff where it is non-negative.
2234 * If "rational" is set, then return a rational basic set.
2236 * If "aff" is NaN, then it is not non-negative (it's not negative either).
2238 static __isl_give isl_basic_set
*aff_nonneg_basic_set(
2239 __isl_take isl_aff
*aff
, int rational
, void *user
)
2241 isl_constraint
*ineq
;
2242 isl_basic_set
*bset
;
2246 if (isl_aff_is_nan(aff
)) {
2247 isl_space
*space
= isl_aff_get_domain_space(aff
);
2249 return isl_basic_set_empty(space
);
2252 ineq
= isl_inequality_from_aff(aff
);
2254 bset
= isl_basic_set_from_constraint(ineq
);
2256 bset
= isl_basic_set_set_rational(bset
);
2257 bset
= isl_basic_set_simplify(bset
);
2261 /* Return a basic set containing those elements in the space
2262 * of aff where it is non-negative.
2264 __isl_give isl_basic_set
*isl_aff_nonneg_basic_set(__isl_take isl_aff
*aff
)
2266 return aff_nonneg_basic_set(aff
, 0, NULL
);
2269 /* Return a basic set containing those elements in the domain space
2270 * of "aff" where it is positive.
2272 __isl_give isl_basic_set
*isl_aff_pos_basic_set(__isl_take isl_aff
*aff
)
2274 aff
= isl_aff_add_constant_num_si(aff
, -1);
2275 return isl_aff_nonneg_basic_set(aff
);
2278 /* Return a basic set containing those elements in the domain space
2279 * of aff where it is negative.
2281 __isl_give isl_basic_set
*isl_aff_neg_basic_set(__isl_take isl_aff
*aff
)
2283 aff
= isl_aff_neg(aff
);
2284 return isl_aff_pos_basic_set(aff
);
2287 /* Return a basic set containing those elements in the space
2288 * of aff where it is zero.
2289 * If "rational" is set, then return a rational basic set.
2291 * If "aff" is NaN, then it is not zero.
2293 static __isl_give isl_basic_set
*aff_zero_basic_set(__isl_take isl_aff
*aff
,
2294 int rational
, void *user
)
2296 isl_constraint
*ineq
;
2297 isl_basic_set
*bset
;
2301 if (isl_aff_is_nan(aff
)) {
2302 isl_space
*space
= isl_aff_get_domain_space(aff
);
2304 return isl_basic_set_empty(space
);
2307 ineq
= isl_equality_from_aff(aff
);
2309 bset
= isl_basic_set_from_constraint(ineq
);
2311 bset
= isl_basic_set_set_rational(bset
);
2312 bset
= isl_basic_set_simplify(bset
);
2316 /* Return a basic set containing those elements in the space
2317 * of aff where it is zero.
2319 __isl_give isl_basic_set
*isl_aff_zero_basic_set(__isl_take isl_aff
*aff
)
2321 return aff_zero_basic_set(aff
, 0, NULL
);
2324 /* Return a basic set containing those elements in the shared space
2325 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
2327 __isl_give isl_basic_set
*isl_aff_ge_basic_set(__isl_take isl_aff
*aff1
,
2328 __isl_take isl_aff
*aff2
)
2330 aff1
= isl_aff_sub(aff1
, aff2
);
2332 return isl_aff_nonneg_basic_set(aff1
);
2335 /* Return a basic set containing those elements in the shared domain space
2336 * of "aff1" and "aff2" where "aff1" is greater than "aff2".
2338 __isl_give isl_basic_set
*isl_aff_gt_basic_set(__isl_take isl_aff
*aff1
,
2339 __isl_take isl_aff
*aff2
)
2341 aff1
= isl_aff_sub(aff1
, aff2
);
2343 return isl_aff_pos_basic_set(aff1
);
2346 /* Return a set containing those elements in the shared space
2347 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
2349 __isl_give isl_set
*isl_aff_ge_set(__isl_take isl_aff
*aff1
,
2350 __isl_take isl_aff
*aff2
)
2352 return isl_set_from_basic_set(isl_aff_ge_basic_set(aff1
, aff2
));
2355 /* Return a set containing those elements in the shared domain space
2356 * of aff1 and aff2 where aff1 is greater than aff2.
2358 * If either of the two inputs is NaN, then the result is empty,
2359 * as comparisons with NaN always return false.
2361 __isl_give isl_set
*isl_aff_gt_set(__isl_take isl_aff
*aff1
,
2362 __isl_take isl_aff
*aff2
)
2364 return isl_set_from_basic_set(isl_aff_gt_basic_set(aff1
, aff2
));
2367 /* Return a basic set containing those elements in the shared space
2368 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
2370 __isl_give isl_basic_set
*isl_aff_le_basic_set(__isl_take isl_aff
*aff1
,
2371 __isl_take isl_aff
*aff2
)
2373 return isl_aff_ge_basic_set(aff2
, aff1
);
2376 /* Return a basic set containing those elements in the shared domain space
2377 * of "aff1" and "aff2" where "aff1" is smaller than "aff2".
2379 __isl_give isl_basic_set
*isl_aff_lt_basic_set(__isl_take isl_aff
*aff1
,
2380 __isl_take isl_aff
*aff2
)
2382 return isl_aff_gt_basic_set(aff2
, aff1
);
2385 /* Return a set containing those elements in the shared space
2386 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
2388 __isl_give isl_set
*isl_aff_le_set(__isl_take isl_aff
*aff1
,
2389 __isl_take isl_aff
*aff2
)
2391 return isl_aff_ge_set(aff2
, aff1
);
2394 /* Return a set containing those elements in the shared domain space
2395 * of "aff1" and "aff2" where "aff1" is smaller than "aff2".
2397 __isl_give isl_set
*isl_aff_lt_set(__isl_take isl_aff
*aff1
,
2398 __isl_take isl_aff
*aff2
)
2400 return isl_set_from_basic_set(isl_aff_lt_basic_set(aff1
, aff2
));
2403 /* Return a basic set containing those elements in the shared space
2404 * of aff1 and aff2 where aff1 and aff2 are equal.
2406 __isl_give isl_basic_set
*isl_aff_eq_basic_set(__isl_take isl_aff
*aff1
,
2407 __isl_take isl_aff
*aff2
)
2409 aff1
= isl_aff_sub(aff1
, aff2
);
2411 return isl_aff_zero_basic_set(aff1
);
2414 /* Return a set containing those elements in the shared space
2415 * of aff1 and aff2 where aff1 and aff2 are equal.
2417 __isl_give isl_set
*isl_aff_eq_set(__isl_take isl_aff
*aff1
,
2418 __isl_take isl_aff
*aff2
)
2420 return isl_set_from_basic_set(isl_aff_eq_basic_set(aff1
, aff2
));
2423 /* Return a set containing those elements in the shared domain space
2424 * of aff1 and aff2 where aff1 and aff2 are not equal.
2426 * If either of the two inputs is NaN, then the result is empty,
2427 * as comparisons with NaN always return false.
2429 __isl_give isl_set
*isl_aff_ne_set(__isl_take isl_aff
*aff1
,
2430 __isl_take isl_aff
*aff2
)
2432 isl_set
*set_lt
, *set_gt
;
2434 set_lt
= isl_aff_lt_set(isl_aff_copy(aff1
),
2435 isl_aff_copy(aff2
));
2436 set_gt
= isl_aff_gt_set(aff1
, aff2
);
2437 return isl_set_union_disjoint(set_lt
, set_gt
);
2440 __isl_give isl_aff
*isl_aff_add_on_domain(__isl_keep isl_set
*dom
,
2441 __isl_take isl_aff
*aff1
, __isl_take isl_aff
*aff2
)
2443 aff1
= isl_aff_add(aff1
, aff2
);
2444 aff1
= isl_aff_gist(aff1
, isl_set_copy(dom
));
2448 isl_bool
isl_aff_is_empty(__isl_keep isl_aff
*aff
)
2451 return isl_bool_error
;
2453 return isl_bool_false
;
2457 #define TYPE isl_aff
2459 #include "check_type_range_templ.c"
2461 /* Check whether the given affine expression has non-zero coefficient
2462 * for any dimension in the given range or if any of these dimensions
2463 * appear with non-zero coefficients in any of the integer divisions
2464 * involved in the affine expression.
2466 isl_bool
isl_aff_involves_dims(__isl_keep isl_aff
*aff
,
2467 enum isl_dim_type type
, unsigned first
, unsigned n
)
2471 isl_bool involves
= isl_bool_false
;
2474 return isl_bool_error
;
2476 return isl_bool_false
;
2477 if (isl_aff_check_range(aff
, type
, first
, n
) < 0)
2478 return isl_bool_error
;
2480 active
= isl_local_space_get_active(aff
->ls
, aff
->v
->el
+ 2);
2484 first
+= isl_local_space_offset(aff
->ls
, type
) - 1;
2485 for (i
= 0; i
< n
; ++i
)
2486 if (active
[first
+ i
]) {
2487 involves
= isl_bool_true
;
2496 return isl_bool_error
;
2499 /* Does "aff" involve any local variables, i.e., integer divisions?
2501 isl_bool
isl_aff_involves_locals(__isl_keep isl_aff
*aff
)
2505 n
= isl_aff_dim(aff
, isl_dim_div
);
2507 return isl_bool_error
;
2508 return isl_bool_ok(n
> 0);
2511 __isl_give isl_aff
*isl_aff_drop_dims(__isl_take isl_aff
*aff
,
2512 enum isl_dim_type type
, unsigned first
, unsigned n
)
2518 if (type
== isl_dim_out
)
2519 isl_die(aff
->v
->ctx
, isl_error_invalid
,
2520 "cannot drop output/set dimension",
2521 return isl_aff_free(aff
));
2522 if (type
== isl_dim_in
)
2524 if (n
== 0 && !isl_local_space_is_named_or_nested(aff
->ls
, type
))
2527 ctx
= isl_aff_get_ctx(aff
);
2528 if (isl_local_space_check_range(aff
->ls
, type
, first
, n
) < 0)
2529 return isl_aff_free(aff
);
2531 aff
= isl_aff_cow(aff
);
2535 aff
->ls
= isl_local_space_drop_dims(aff
->ls
, type
, first
, n
);
2537 return isl_aff_free(aff
);
2539 first
+= 1 + isl_local_space_offset(aff
->ls
, type
);
2540 aff
->v
= isl_vec_drop_els(aff
->v
, first
, n
);
2542 return isl_aff_free(aff
);
2547 /* Is the domain of "aff" a product?
2549 static isl_bool
isl_aff_domain_is_product(__isl_keep isl_aff
*aff
)
2551 return isl_space_is_product(isl_aff_peek_domain_space(aff
));
2555 #define TYPE isl_aff
2556 #include <isl_domain_factor_templ.c>
2558 /* Project the domain of the affine expression onto its parameter space.
2559 * The affine expression may not involve any of the domain dimensions.
2561 __isl_give isl_aff
*isl_aff_project_domain_on_params(__isl_take isl_aff
*aff
)
2566 n
= isl_aff_dim(aff
, isl_dim_in
);
2568 return isl_aff_free(aff
);
2569 aff
= isl_aff_drop_domain(aff
, 0, n
);
2570 space
= isl_aff_get_domain_space(aff
);
2571 space
= isl_space_params(space
);
2572 aff
= isl_aff_reset_domain_space(aff
, space
);
2576 /* Convert an affine expression defined over a parameter domain
2577 * into one that is defined over a zero-dimensional set.
2579 __isl_give isl_aff
*isl_aff_from_range(__isl_take isl_aff
*aff
)
2581 isl_local_space
*ls
;
2583 ls
= isl_aff_take_domain_local_space(aff
);
2584 ls
= isl_local_space_set_from_params(ls
);
2585 aff
= isl_aff_restore_domain_local_space(aff
, ls
);
2590 __isl_give isl_aff
*isl_aff_insert_dims(__isl_take isl_aff
*aff
,
2591 enum isl_dim_type type
, unsigned first
, unsigned n
)
2597 if (type
== isl_dim_out
)
2598 isl_die(aff
->v
->ctx
, isl_error_invalid
,
2599 "cannot insert output/set dimensions",
2600 return isl_aff_free(aff
));
2601 if (type
== isl_dim_in
)
2603 if (n
== 0 && !isl_local_space_is_named_or_nested(aff
->ls
, type
))
2606 ctx
= isl_aff_get_ctx(aff
);
2607 if (isl_local_space_check_range(aff
->ls
, type
, first
, 0) < 0)
2608 return isl_aff_free(aff
);
2610 aff
= isl_aff_cow(aff
);
2614 aff
->ls
= isl_local_space_insert_dims(aff
->ls
, type
, first
, n
);
2616 return isl_aff_free(aff
);
2618 first
+= 1 + isl_local_space_offset(aff
->ls
, type
);
2619 aff
->v
= isl_vec_insert_zero_els(aff
->v
, first
, n
);
2621 return isl_aff_free(aff
);
2626 __isl_give isl_aff
*isl_aff_add_dims(__isl_take isl_aff
*aff
,
2627 enum isl_dim_type type
, unsigned n
)
2631 pos
= isl_aff_dim(aff
, type
);
2633 return isl_aff_free(aff
);
2635 return isl_aff_insert_dims(aff
, type
, pos
, n
);
2638 /* Move the "n" dimensions of "src_type" starting at "src_pos" of "aff"
2639 * to dimensions of "dst_type" at "dst_pos".
2641 * We only support moving input dimensions to parameters and vice versa.
2643 __isl_give isl_aff
*isl_aff_move_dims(__isl_take isl_aff
*aff
,
2644 enum isl_dim_type dst_type
, unsigned dst_pos
,
2645 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
2649 isl_size src_off
, dst_off
;
2654 !isl_local_space_is_named_or_nested(aff
->ls
, src_type
) &&
2655 !isl_local_space_is_named_or_nested(aff
->ls
, dst_type
))
2658 if (dst_type
== isl_dim_out
|| src_type
== isl_dim_out
)
2659 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2660 "cannot move output/set dimension",
2661 return isl_aff_free(aff
));
2662 if (dst_type
== isl_dim_div
|| src_type
== isl_dim_div
)
2663 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2664 "cannot move divs", return isl_aff_free(aff
));
2665 if (dst_type
== isl_dim_in
)
2666 dst_type
= isl_dim_set
;
2667 if (src_type
== isl_dim_in
)
2668 src_type
= isl_dim_set
;
2670 if (isl_local_space_check_range(aff
->ls
, src_type
, src_pos
, n
) < 0)
2671 return isl_aff_free(aff
);
2672 if (dst_type
== src_type
)
2673 isl_die(isl_aff_get_ctx(aff
), isl_error_unsupported
,
2674 "moving dims within the same type not supported",
2675 return isl_aff_free(aff
));
2677 aff
= isl_aff_cow(aff
);
2678 src_off
= isl_aff_domain_offset(aff
, src_type
);
2679 dst_off
= isl_aff_domain_offset(aff
, dst_type
);
2680 if (src_off
< 0 || dst_off
< 0)
2681 return isl_aff_free(aff
);
2683 g_src_pos
= 1 + src_off
+ src_pos
;
2684 g_dst_pos
= 1 + dst_off
+ dst_pos
;
2685 if (dst_type
> src_type
)
2688 aff
->v
= isl_vec_move_els(aff
->v
, g_dst_pos
, g_src_pos
, n
);
2689 aff
->ls
= isl_local_space_move_dims(aff
->ls
, dst_type
, dst_pos
,
2690 src_type
, src_pos
, n
);
2691 if (!aff
->v
|| !aff
->ls
)
2692 return isl_aff_free(aff
);
2694 aff
= sort_divs(aff
);
2699 /* Return a zero isl_aff in the given space.
2701 * This is a helper function for isl_pw_*_as_* that ensures a uniform
2702 * interface over all piecewise types.
2704 static __isl_give isl_aff
*isl_aff_zero_in_space(__isl_take isl_space
*space
)
2706 isl_local_space
*ls
;
2708 ls
= isl_local_space_from_space(isl_space_domain(space
));
2709 return isl_aff_zero_on_domain(ls
);
2712 #define isl_aff_involves_nan isl_aff_is_nan
2715 #define PW isl_pw_aff
2719 #define EL_IS_ZERO is_empty
2723 #define IS_ZERO is_empty
2726 #undef DEFAULT_IS_ZERO
2727 #define DEFAULT_IS_ZERO 0
2729 #include <isl_pw_templ.c>
2730 #include <isl_pw_add_constant_val_templ.c>
2731 #include <isl_pw_bind_domain_templ.c>
2732 #include <isl_pw_eval.c>
2733 #include <isl_pw_hash.c>
2734 #include <isl_pw_insert_dims_templ.c>
2735 #include <isl_pw_insert_domain_templ.c>
2736 #include <isl_pw_move_dims_templ.c>
2737 #include <isl_pw_neg_templ.c>
2738 #include <isl_pw_pullback_templ.c>
2739 #include <isl_pw_sub_templ.c>
2740 #include <isl_pw_union_opt.c>
2745 #include <isl_union_single.c>
2746 #include <isl_union_neg.c>
2751 #include <isl_union_pw_templ.c>
2753 /* Compute a piecewise quasi-affine expression with a domain that
2754 * is the union of those of pwaff1 and pwaff2 and such that on each
2755 * cell, the quasi-affine expression is the maximum of those of pwaff1
2756 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2757 * cell, then the associated expression is the defined one.
2759 __isl_give isl_pw_aff
*isl_pw_aff_union_max(__isl_take isl_pw_aff
*pwaff1
,
2760 __isl_take isl_pw_aff
*pwaff2
)
2762 isl_pw_aff_align_params_bin(&pwaff1
, &pwaff2
);
2763 return isl_pw_aff_union_opt_cmp(pwaff1
, pwaff2
, &isl_aff_ge_set
);
2766 /* Compute a piecewise quasi-affine expression with a domain that
2767 * is the union of those of pwaff1 and pwaff2 and such that on each
2768 * cell, the quasi-affine expression is the minimum of those of pwaff1
2769 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2770 * cell, then the associated expression is the defined one.
2772 __isl_give isl_pw_aff
*isl_pw_aff_union_min(__isl_take isl_pw_aff
*pwaff1
,
2773 __isl_take isl_pw_aff
*pwaff2
)
2775 isl_pw_aff_align_params_bin(&pwaff1
, &pwaff2
);
2776 return isl_pw_aff_union_opt_cmp(pwaff1
, pwaff2
, &isl_aff_le_set
);
2779 __isl_give isl_pw_aff
*isl_pw_aff_union_opt(__isl_take isl_pw_aff
*pwaff1
,
2780 __isl_take isl_pw_aff
*pwaff2
, int max
)
2783 return isl_pw_aff_union_max(pwaff1
, pwaff2
);
2785 return isl_pw_aff_union_min(pwaff1
, pwaff2
);
2788 /* Is the domain of "pa" a product?
2790 static isl_bool
isl_pw_aff_domain_is_product(__isl_keep isl_pw_aff
*pa
)
2792 return isl_space_domain_is_wrapping(isl_pw_aff_peek_space(pa
));
2796 #define TYPE isl_pw_aff
2797 #include <isl_domain_factor_templ.c>
2799 /* Return a set containing those elements in the domain
2800 * of "pwaff" where it satisfies "fn" (if complement is 0) or
2801 * does not satisfy "fn" (if complement is 1).
2803 * The pieces with a NaN never belong to the result since
2804 * NaN does not satisfy any property.
2806 static __isl_give isl_set
*pw_aff_locus(__isl_take isl_pw_aff
*pwaff
,
2807 __isl_give isl_basic_set
*(*fn
)(__isl_take isl_aff
*aff
, int rational
,
2809 int complement
, void *user
)
2817 set
= isl_set_empty(isl_pw_aff_get_domain_space(pwaff
));
2819 for (i
= 0; i
< pwaff
->n
; ++i
) {
2820 isl_basic_set
*bset
;
2821 isl_set
*set_i
, *locus
;
2824 if (isl_aff_is_nan(pwaff
->p
[i
].aff
))
2827 rational
= isl_set_has_rational(pwaff
->p
[i
].set
);
2828 bset
= fn(isl_aff_copy(pwaff
->p
[i
].aff
), rational
, user
);
2829 locus
= isl_set_from_basic_set(bset
);
2830 set_i
= isl_set_copy(pwaff
->p
[i
].set
);
2832 set_i
= isl_set_subtract(set_i
, locus
);
2834 set_i
= isl_set_intersect(set_i
, locus
);
2835 set
= isl_set_union_disjoint(set
, set_i
);
2838 isl_pw_aff_free(pwaff
);
2843 /* Return a set containing those elements in the domain
2844 * of "pa" where it is positive.
2846 __isl_give isl_set
*isl_pw_aff_pos_set(__isl_take isl_pw_aff
*pa
)
2848 return pw_aff_locus(pa
, &aff_pos_basic_set
, 0, NULL
);
2851 /* Return a set containing those elements in the domain
2852 * of pwaff where it is non-negative.
2854 __isl_give isl_set
*isl_pw_aff_nonneg_set(__isl_take isl_pw_aff
*pwaff
)
2856 return pw_aff_locus(pwaff
, &aff_nonneg_basic_set
, 0, NULL
);
2859 /* Return a set containing those elements in the domain
2860 * of pwaff where it is zero.
2862 __isl_give isl_set
*isl_pw_aff_zero_set(__isl_take isl_pw_aff
*pwaff
)
2864 return pw_aff_locus(pwaff
, &aff_zero_basic_set
, 0, NULL
);
2867 /* Return a set containing those elements in the domain
2868 * of pwaff where it is not zero.
2870 __isl_give isl_set
*isl_pw_aff_non_zero_set(__isl_take isl_pw_aff
*pwaff
)
2872 return pw_aff_locus(pwaff
, &aff_zero_basic_set
, 1, NULL
);
2875 /* Bind the affine function "aff" to the parameter "id",
2876 * returning the elements in the domain where the affine expression
2877 * is equal to the parameter.
2879 __isl_give isl_basic_set
*isl_aff_bind_id(__isl_take isl_aff
*aff
,
2880 __isl_take isl_id
*id
)
2885 space
= isl_aff_get_domain_space(aff
);
2886 space
= isl_space_add_param_id(space
, isl_id_copy(id
));
2888 aff
= isl_aff_align_params(aff
, isl_space_copy(space
));
2889 aff_id
= isl_aff_param_on_domain_space_id(space
, id
);
2891 return isl_aff_eq_basic_set(aff
, aff_id
);
2894 /* Wrapper around isl_aff_bind_id for use as pw_aff_locus callback.
2895 * "rational" should not be set.
2897 static __isl_give isl_basic_set
*aff_bind_id(__isl_take isl_aff
*aff
,
2898 int rational
, void *user
)
2905 isl_die(isl_aff_get_ctx(aff
), isl_error_unsupported
,
2906 "rational binding not supported", goto error
);
2907 return isl_aff_bind_id(aff
, isl_id_copy(id
));
2913 /* Bind the piecewise affine function "pa" to the parameter "id",
2914 * returning the elements in the domain where the expression
2915 * is equal to the parameter.
2917 __isl_give isl_set
*isl_pw_aff_bind_id(__isl_take isl_pw_aff
*pa
,
2918 __isl_take isl_id
*id
)
2922 bound
= pw_aff_locus(pa
, &aff_bind_id
, 0, id
);
2928 /* Return a set containing those elements in the shared domain
2929 * of pwaff1 and pwaff2 where pwaff1 is greater than (or equal) to pwaff2.
2931 * We compute the difference on the shared domain and then construct
2932 * the set of values where this difference is non-negative.
2933 * If strict is set, we first subtract 1 from the difference.
2934 * If equal is set, we only return the elements where pwaff1 and pwaff2
2937 static __isl_give isl_set
*pw_aff_gte_set(__isl_take isl_pw_aff
*pwaff1
,
2938 __isl_take isl_pw_aff
*pwaff2
, int strict
, int equal
)
2940 isl_set
*set1
, *set2
;
2942 set1
= isl_pw_aff_domain(isl_pw_aff_copy(pwaff1
));
2943 set2
= isl_pw_aff_domain(isl_pw_aff_copy(pwaff2
));
2944 set1
= isl_set_intersect(set1
, set2
);
2945 pwaff1
= isl_pw_aff_intersect_domain(pwaff1
, isl_set_copy(set1
));
2946 pwaff2
= isl_pw_aff_intersect_domain(pwaff2
, isl_set_copy(set1
));
2947 pwaff1
= isl_pw_aff_add(pwaff1
, isl_pw_aff_neg(pwaff2
));
2950 isl_space
*space
= isl_set_get_space(set1
);
2952 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(space
));
2953 aff
= isl_aff_add_constant_si(aff
, -1);
2954 pwaff1
= isl_pw_aff_add(pwaff1
, isl_pw_aff_alloc(set1
, aff
));
2959 return isl_pw_aff_zero_set(pwaff1
);
2960 return isl_pw_aff_nonneg_set(pwaff1
);
2963 /* Return a set containing those elements in the shared domain
2964 * of pwaff1 and pwaff2 where pwaff1 is equal to pwaff2.
2966 __isl_give isl_set
*isl_pw_aff_eq_set(__isl_take isl_pw_aff
*pwaff1
,
2967 __isl_take isl_pw_aff
*pwaff2
)
2969 isl_pw_aff_align_params_bin(&pwaff1
, &pwaff2
);
2970 return pw_aff_gte_set(pwaff1
, pwaff2
, 0, 1);
2973 /* Return a set containing those elements in the shared domain
2974 * of pwaff1 and pwaff2 where pwaff1 is greater than or equal to pwaff2.
2976 __isl_give isl_set
*isl_pw_aff_ge_set(__isl_take isl_pw_aff
*pwaff1
,
2977 __isl_take isl_pw_aff
*pwaff2
)
2979 isl_pw_aff_align_params_bin(&pwaff1
, &pwaff2
);
2980 return pw_aff_gte_set(pwaff1
, pwaff2
, 0, 0);
2983 /* Return a set containing those elements in the shared domain
2984 * of pwaff1 and pwaff2 where pwaff1 is strictly greater than pwaff2.
2986 __isl_give isl_set
*isl_pw_aff_gt_set(__isl_take isl_pw_aff
*pwaff1
,
2987 __isl_take isl_pw_aff
*pwaff2
)
2989 isl_pw_aff_align_params_bin(&pwaff1
, &pwaff2
);
2990 return pw_aff_gte_set(pwaff1
, pwaff2
, 1, 0);
2993 __isl_give isl_set
*isl_pw_aff_le_set(__isl_take isl_pw_aff
*pwaff1
,
2994 __isl_take isl_pw_aff
*pwaff2
)
2996 return isl_pw_aff_ge_set(pwaff2
, pwaff1
);
2999 __isl_give isl_set
*isl_pw_aff_lt_set(__isl_take isl_pw_aff
*pwaff1
,
3000 __isl_take isl_pw_aff
*pwaff2
)
3002 return isl_pw_aff_gt_set(pwaff2
, pwaff1
);
3005 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3006 * where the function values are ordered in the same way as "order",
3007 * which returns a set in the shared domain of its two arguments.
3009 * Let "pa1" and "pa2" be defined on domains A and B respectively.
3010 * We first pull back the two functions such that they are defined on
3011 * the domain [A -> B]. Then we apply "order", resulting in a set
3012 * in the space [A -> B]. Finally, we unwrap this set to obtain
3013 * a map in the space A -> B.
3015 static __isl_give isl_map
*isl_pw_aff_order_map(
3016 __isl_take isl_pw_aff
*pa1
, __isl_take isl_pw_aff
*pa2
,
3017 __isl_give isl_set
*(*order
)(__isl_take isl_pw_aff
*pa1
,
3018 __isl_take isl_pw_aff
*pa2
))
3020 isl_space
*space1
, *space2
;
3024 isl_pw_aff_align_params_bin(&pa1
, &pa2
);
3025 space1
= isl_space_domain(isl_pw_aff_get_space(pa1
));
3026 space2
= isl_space_domain(isl_pw_aff_get_space(pa2
));
3027 space1
= isl_space_map_from_domain_and_range(space1
, space2
);
3028 ma
= isl_multi_aff_domain_map(isl_space_copy(space1
));
3029 pa1
= isl_pw_aff_pullback_multi_aff(pa1
, ma
);
3030 ma
= isl_multi_aff_range_map(space1
);
3031 pa2
= isl_pw_aff_pullback_multi_aff(pa2
, ma
);
3032 set
= order(pa1
, pa2
);
3034 return isl_set_unwrap(set
);
3037 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3038 * where the function values are equal.
3040 __isl_give isl_map
*isl_pw_aff_eq_map(__isl_take isl_pw_aff
*pa1
,
3041 __isl_take isl_pw_aff
*pa2
)
3043 return isl_pw_aff_order_map(pa1
, pa2
, &isl_pw_aff_eq_set
);
3046 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3047 * where the function value of "pa1" is less than or equal to
3048 * the function value of "pa2".
3050 __isl_give isl_map
*isl_pw_aff_le_map(__isl_take isl_pw_aff
*pa1
,
3051 __isl_take isl_pw_aff
*pa2
)
3053 return isl_pw_aff_order_map(pa1
, pa2
, &isl_pw_aff_le_set
);
3056 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3057 * where the function value of "pa1" is less than the function value of "pa2".
3059 __isl_give isl_map
*isl_pw_aff_lt_map(__isl_take isl_pw_aff
*pa1
,
3060 __isl_take isl_pw_aff
*pa2
)
3062 return isl_pw_aff_order_map(pa1
, pa2
, &isl_pw_aff_lt_set
);
3065 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3066 * where the function value of "pa1" is greater than or equal to
3067 * the function value of "pa2".
3069 __isl_give isl_map
*isl_pw_aff_ge_map(__isl_take isl_pw_aff
*pa1
,
3070 __isl_take isl_pw_aff
*pa2
)
3072 return isl_pw_aff_order_map(pa1
, pa2
, &isl_pw_aff_ge_set
);
3075 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3076 * where the function value of "pa1" is greater than the function value
3079 __isl_give isl_map
*isl_pw_aff_gt_map(__isl_take isl_pw_aff
*pa1
,
3080 __isl_take isl_pw_aff
*pa2
)
3082 return isl_pw_aff_order_map(pa1
, pa2
, &isl_pw_aff_gt_set
);
3085 /* Return a set containing those elements in the shared domain
3086 * of the elements of list1 and list2 where each element in list1
3087 * has the relation specified by "fn" with each element in list2.
3089 static __isl_give isl_set
*pw_aff_list_set(__isl_take isl_pw_aff_list
*list1
,
3090 __isl_take isl_pw_aff_list
*list2
,
3091 __isl_give isl_set
*(*fn
)(__isl_take isl_pw_aff
*pwaff1
,
3092 __isl_take isl_pw_aff
*pwaff2
))
3098 if (!list1
|| !list2
)
3101 ctx
= isl_pw_aff_list_get_ctx(list1
);
3102 if (list1
->n
< 1 || list2
->n
< 1)
3103 isl_die(ctx
, isl_error_invalid
,
3104 "list should contain at least one element", goto error
);
3106 set
= isl_set_universe(isl_pw_aff_get_domain_space(list1
->p
[0]));
3107 for (i
= 0; i
< list1
->n
; ++i
)
3108 for (j
= 0; j
< list2
->n
; ++j
) {
3111 set_ij
= fn(isl_pw_aff_copy(list1
->p
[i
]),
3112 isl_pw_aff_copy(list2
->p
[j
]));
3113 set
= isl_set_intersect(set
, set_ij
);
3116 isl_pw_aff_list_free(list1
);
3117 isl_pw_aff_list_free(list2
);
3120 isl_pw_aff_list_free(list1
);
3121 isl_pw_aff_list_free(list2
);
3125 /* Return a set containing those elements in the shared domain
3126 * of the elements of list1 and list2 where each element in list1
3127 * is equal to each element in list2.
3129 __isl_give isl_set
*isl_pw_aff_list_eq_set(__isl_take isl_pw_aff_list
*list1
,
3130 __isl_take isl_pw_aff_list
*list2
)
3132 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_eq_set
);
3135 __isl_give isl_set
*isl_pw_aff_list_ne_set(__isl_take isl_pw_aff_list
*list1
,
3136 __isl_take isl_pw_aff_list
*list2
)
3138 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_ne_set
);
3141 /* Return a set containing those elements in the shared domain
3142 * of the elements of list1 and list2 where each element in list1
3143 * is less than or equal to each element in list2.
3145 __isl_give isl_set
*isl_pw_aff_list_le_set(__isl_take isl_pw_aff_list
*list1
,
3146 __isl_take isl_pw_aff_list
*list2
)
3148 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_le_set
);
3151 __isl_give isl_set
*isl_pw_aff_list_lt_set(__isl_take isl_pw_aff_list
*list1
,
3152 __isl_take isl_pw_aff_list
*list2
)
3154 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_lt_set
);
3157 __isl_give isl_set
*isl_pw_aff_list_ge_set(__isl_take isl_pw_aff_list
*list1
,
3158 __isl_take isl_pw_aff_list
*list2
)
3160 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_ge_set
);
3163 __isl_give isl_set
*isl_pw_aff_list_gt_set(__isl_take isl_pw_aff_list
*list1
,
3164 __isl_take isl_pw_aff_list
*list2
)
3166 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_gt_set
);
3170 /* Return a set containing those elements in the shared domain
3171 * of pwaff1 and pwaff2 where pwaff1 is not equal to pwaff2.
3173 __isl_give isl_set
*isl_pw_aff_ne_set(__isl_take isl_pw_aff
*pwaff1
,
3174 __isl_take isl_pw_aff
*pwaff2
)
3176 isl_set
*set_lt
, *set_gt
;
3178 isl_pw_aff_align_params_bin(&pwaff1
, &pwaff2
);
3179 set_lt
= isl_pw_aff_lt_set(isl_pw_aff_copy(pwaff1
),
3180 isl_pw_aff_copy(pwaff2
));
3181 set_gt
= isl_pw_aff_gt_set(pwaff1
, pwaff2
);
3182 return isl_set_union_disjoint(set_lt
, set_gt
);
3185 __isl_give isl_pw_aff
*isl_pw_aff_scale_down(__isl_take isl_pw_aff
*pwaff
,
3190 if (isl_int_is_one(v
))
3192 if (!isl_int_is_pos(v
))
3193 isl_die(isl_pw_aff_get_ctx(pwaff
), isl_error_invalid
,
3194 "factor needs to be positive",
3195 return isl_pw_aff_free(pwaff
));
3196 pwaff
= isl_pw_aff_cow(pwaff
);
3202 for (i
= 0; i
< pwaff
->n
; ++i
) {
3203 pwaff
->p
[i
].aff
= isl_aff_scale_down(pwaff
->p
[i
].aff
, v
);
3204 if (!pwaff
->p
[i
].aff
)
3205 return isl_pw_aff_free(pwaff
);
3211 __isl_give isl_pw_aff
*isl_pw_aff_floor(__isl_take isl_pw_aff
*pwaff
)
3215 pwaff
= isl_pw_aff_cow(pwaff
);
3221 for (i
= 0; i
< pwaff
->n
; ++i
) {
3222 pwaff
->p
[i
].aff
= isl_aff_floor(pwaff
->p
[i
].aff
);
3223 if (!pwaff
->p
[i
].aff
)
3224 return isl_pw_aff_free(pwaff
);
3230 __isl_give isl_pw_aff
*isl_pw_aff_ceil(__isl_take isl_pw_aff
*pwaff
)
3234 pwaff
= isl_pw_aff_cow(pwaff
);
3240 for (i
= 0; i
< pwaff
->n
; ++i
) {
3241 pwaff
->p
[i
].aff
= isl_aff_ceil(pwaff
->p
[i
].aff
);
3242 if (!pwaff
->p
[i
].aff
)
3243 return isl_pw_aff_free(pwaff
);
3249 /* Assuming that "cond1" and "cond2" are disjoint,
3250 * return an affine expression that is equal to pwaff1 on cond1
3251 * and to pwaff2 on cond2.
3253 static __isl_give isl_pw_aff
*isl_pw_aff_select(
3254 __isl_take isl_set
*cond1
, __isl_take isl_pw_aff
*pwaff1
,
3255 __isl_take isl_set
*cond2
, __isl_take isl_pw_aff
*pwaff2
)
3257 pwaff1
= isl_pw_aff_intersect_domain(pwaff1
, cond1
);
3258 pwaff2
= isl_pw_aff_intersect_domain(pwaff2
, cond2
);
3260 return isl_pw_aff_add_disjoint(pwaff1
, pwaff2
);
3263 /* Return an affine expression that is equal to pwaff_true for elements
3264 * where "cond" is non-zero and to pwaff_false for elements where "cond"
3266 * That is, return cond ? pwaff_true : pwaff_false;
3268 * If "cond" involves and NaN, then we conservatively return a NaN
3269 * on its entire domain. In principle, we could consider the pieces
3270 * where it is NaN separately from those where it is not.
3272 * If "pwaff_true" and "pwaff_false" are obviously equal to each other,
3273 * then only use the domain of "cond" to restrict the domain.
3275 __isl_give isl_pw_aff
*isl_pw_aff_cond(__isl_take isl_pw_aff
*cond
,
3276 __isl_take isl_pw_aff
*pwaff_true
, __isl_take isl_pw_aff
*pwaff_false
)
3278 isl_set
*cond_true
, *cond_false
;
3283 if (isl_pw_aff_involves_nan(cond
)) {
3284 isl_space
*space
= isl_pw_aff_get_domain_space(cond
);
3285 isl_local_space
*ls
= isl_local_space_from_space(space
);
3286 isl_pw_aff_free(cond
);
3287 isl_pw_aff_free(pwaff_true
);
3288 isl_pw_aff_free(pwaff_false
);
3289 return isl_pw_aff_nan_on_domain(ls
);
3292 pwaff_true
= isl_pw_aff_align_params(pwaff_true
,
3293 isl_pw_aff_get_space(pwaff_false
));
3294 pwaff_false
= isl_pw_aff_align_params(pwaff_false
,
3295 isl_pw_aff_get_space(pwaff_true
));
3296 equal
= isl_pw_aff_plain_is_equal(pwaff_true
, pwaff_false
);
3302 dom
= isl_set_coalesce(isl_pw_aff_domain(cond
));
3303 isl_pw_aff_free(pwaff_false
);
3304 return isl_pw_aff_intersect_domain(pwaff_true
, dom
);
3307 cond_true
= isl_pw_aff_non_zero_set(isl_pw_aff_copy(cond
));
3308 cond_false
= isl_pw_aff_zero_set(cond
);
3309 return isl_pw_aff_select(cond_true
, pwaff_true
,
3310 cond_false
, pwaff_false
);
3312 isl_pw_aff_free(cond
);
3313 isl_pw_aff_free(pwaff_true
);
3314 isl_pw_aff_free(pwaff_false
);
3318 isl_bool
isl_aff_is_cst(__isl_keep isl_aff
*aff
)
3323 return isl_bool_error
;
3325 pos
= isl_seq_first_non_zero(aff
->v
->el
+ 2, aff
->v
->size
- 2);
3326 return isl_bool_ok(pos
== -1);
3329 /* Check whether pwaff is a piecewise constant.
3331 isl_bool
isl_pw_aff_is_cst(__isl_keep isl_pw_aff
*pwaff
)
3336 return isl_bool_error
;
3338 for (i
= 0; i
< pwaff
->n
; ++i
) {
3339 isl_bool is_cst
= isl_aff_is_cst(pwaff
->p
[i
].aff
);
3340 if (is_cst
< 0 || !is_cst
)
3344 return isl_bool_true
;
3347 /* Return the product of "aff1" and "aff2".
3349 * If either of the two is NaN, then the result is NaN.
3351 * Otherwise, at least one of "aff1" or "aff2" needs to be a constant.
3353 __isl_give isl_aff
*isl_aff_mul(__isl_take isl_aff
*aff1
,
3354 __isl_take isl_aff
*aff2
)
3359 if (isl_aff_is_nan(aff1
)) {
3363 if (isl_aff_is_nan(aff2
)) {
3368 if (!isl_aff_is_cst(aff2
) && isl_aff_is_cst(aff1
))
3369 return isl_aff_mul(aff2
, aff1
);
3371 if (!isl_aff_is_cst(aff2
))
3372 isl_die(isl_aff_get_ctx(aff1
), isl_error_invalid
,
3373 "at least one affine expression should be constant",
3376 aff1
= isl_aff_cow(aff1
);
3380 aff1
= isl_aff_scale(aff1
, aff2
->v
->el
[1]);
3381 aff1
= isl_aff_scale_down(aff1
, aff2
->v
->el
[0]);
3391 /* Divide "aff1" by "aff2", assuming "aff2" is a constant.
3393 * If either of the two is NaN, then the result is NaN.
3394 * A division by zero also results in NaN.
3396 __isl_give isl_aff
*isl_aff_div(__isl_take isl_aff
*aff1
,
3397 __isl_take isl_aff
*aff2
)
3399 isl_bool is_cst
, is_zero
;
3405 if (isl_aff_is_nan(aff1
)) {
3409 if (isl_aff_is_nan(aff2
)) {
3414 is_cst
= isl_aff_is_cst(aff2
);
3418 isl_die(isl_aff_get_ctx(aff2
), isl_error_invalid
,
3419 "second argument should be a constant", goto error
);
3420 is_zero
= isl_aff_plain_is_zero(aff2
);
3424 return set_nan_free(aff1
, aff2
);
3426 neg
= isl_int_is_neg(aff2
->v
->el
[1]);
3428 isl_int_neg(aff2
->v
->el
[0], aff2
->v
->el
[0]);
3429 isl_int_neg(aff2
->v
->el
[1], aff2
->v
->el
[1]);
3432 aff1
= isl_aff_scale(aff1
, aff2
->v
->el
[0]);
3433 aff1
= isl_aff_scale_down(aff1
, aff2
->v
->el
[1]);
3436 isl_int_neg(aff2
->v
->el
[0], aff2
->v
->el
[0]);
3437 isl_int_neg(aff2
->v
->el
[1], aff2
->v
->el
[1]);
3448 __isl_give isl_pw_aff
*isl_pw_aff_add(__isl_take isl_pw_aff
*pwaff1
,
3449 __isl_take isl_pw_aff
*pwaff2
)
3451 isl_pw_aff_align_params_bin(&pwaff1
, &pwaff2
);
3452 return isl_pw_aff_on_shared_domain(pwaff1
, pwaff2
, &isl_aff_add
);
3455 __isl_give isl_pw_aff
*isl_pw_aff_union_add(__isl_take isl_pw_aff
*pwaff1
,
3456 __isl_take isl_pw_aff
*pwaff2
)
3458 return isl_pw_aff_union_add_(pwaff1
, pwaff2
);
3461 __isl_give isl_pw_aff
*isl_pw_aff_mul(__isl_take isl_pw_aff
*pwaff1
,
3462 __isl_take isl_pw_aff
*pwaff2
)
3464 isl_pw_aff_align_params_bin(&pwaff1
, &pwaff2
);
3465 return isl_pw_aff_on_shared_domain(pwaff1
, pwaff2
, &isl_aff_mul
);
3468 /* Divide "pa1" by "pa2", assuming "pa2" is a piecewise constant.
3470 __isl_give isl_pw_aff
*isl_pw_aff_div(__isl_take isl_pw_aff
*pa1
,
3471 __isl_take isl_pw_aff
*pa2
)
3475 is_cst
= isl_pw_aff_is_cst(pa2
);
3479 isl_die(isl_pw_aff_get_ctx(pa2
), isl_error_invalid
,
3480 "second argument should be a piecewise constant",
3482 isl_pw_aff_align_params_bin(&pa1
, &pa2
);
3483 return isl_pw_aff_on_shared_domain(pa1
, pa2
, &isl_aff_div
);
3485 isl_pw_aff_free(pa1
);
3486 isl_pw_aff_free(pa2
);
3490 /* Compute the quotient of the integer division of "pa1" by "pa2"
3491 * with rounding towards zero.
3492 * "pa2" is assumed to be a piecewise constant.
3494 * In particular, return
3496 * pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2)
3499 __isl_give isl_pw_aff
*isl_pw_aff_tdiv_q(__isl_take isl_pw_aff
*pa1
,
3500 __isl_take isl_pw_aff
*pa2
)
3506 is_cst
= isl_pw_aff_is_cst(pa2
);
3510 isl_die(isl_pw_aff_get_ctx(pa2
), isl_error_invalid
,
3511 "second argument should be a piecewise constant",
3514 pa1
= isl_pw_aff_div(pa1
, pa2
);
3516 cond
= isl_pw_aff_nonneg_set(isl_pw_aff_copy(pa1
));
3517 f
= isl_pw_aff_floor(isl_pw_aff_copy(pa1
));
3518 c
= isl_pw_aff_ceil(pa1
);
3519 return isl_pw_aff_cond(isl_set_indicator_function(cond
), f
, c
);
3521 isl_pw_aff_free(pa1
);
3522 isl_pw_aff_free(pa2
);
3526 /* Compute the remainder of the integer division of "pa1" by "pa2"
3527 * with rounding towards zero.
3528 * "pa2" is assumed to be a piecewise constant.
3530 * In particular, return
3532 * pa1 - pa2 * (pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2))
3535 __isl_give isl_pw_aff
*isl_pw_aff_tdiv_r(__isl_take isl_pw_aff
*pa1
,
3536 __isl_take isl_pw_aff
*pa2
)
3541 is_cst
= isl_pw_aff_is_cst(pa2
);
3545 isl_die(isl_pw_aff_get_ctx(pa2
), isl_error_invalid
,
3546 "second argument should be a piecewise constant",
3548 res
= isl_pw_aff_tdiv_q(isl_pw_aff_copy(pa1
), isl_pw_aff_copy(pa2
));
3549 res
= isl_pw_aff_mul(pa2
, res
);
3550 res
= isl_pw_aff_sub(pa1
, res
);
3553 isl_pw_aff_free(pa1
);
3554 isl_pw_aff_free(pa2
);
3558 /* Does either of "pa1" or "pa2" involve any NaN2?
3560 static isl_bool
either_involves_nan(__isl_keep isl_pw_aff
*pa1
,
3561 __isl_keep isl_pw_aff
*pa2
)
3565 has_nan
= isl_pw_aff_involves_nan(pa1
);
3566 if (has_nan
< 0 || has_nan
)
3568 return isl_pw_aff_involves_nan(pa2
);
3571 /* Replace "pa1" and "pa2" (at least one of which involves a NaN)
3572 * by a NaN on their shared domain.
3574 * In principle, the result could be refined to only being NaN
3575 * on the parts of this domain where at least one of "pa1" or "pa2" is NaN.
3577 static __isl_give isl_pw_aff
*replace_by_nan(__isl_take isl_pw_aff
*pa1
,
3578 __isl_take isl_pw_aff
*pa2
)
3580 isl_local_space
*ls
;
3584 dom
= isl_set_intersect(isl_pw_aff_domain(pa1
), isl_pw_aff_domain(pa2
));
3585 ls
= isl_local_space_from_space(isl_set_get_space(dom
));
3586 pa
= isl_pw_aff_nan_on_domain(ls
);
3587 pa
= isl_pw_aff_intersect_domain(pa
, dom
);
3592 static __isl_give isl_pw_aff
*pw_aff_min(__isl_take isl_pw_aff
*pwaff1
,
3593 __isl_take isl_pw_aff
*pwaff2
)
3598 dom
= isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1
)),
3599 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2
)));
3600 le
= isl_pw_aff_le_set(isl_pw_aff_copy(pwaff1
),
3601 isl_pw_aff_copy(pwaff2
));
3602 dom
= isl_set_subtract(dom
, isl_set_copy(le
));
3603 return isl_pw_aff_select(le
, pwaff1
, dom
, pwaff2
);
3606 static __isl_give isl_pw_aff
*pw_aff_max(__isl_take isl_pw_aff
*pwaff1
,
3607 __isl_take isl_pw_aff
*pwaff2
)
3612 dom
= isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1
)),
3613 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2
)));
3614 ge
= isl_pw_aff_ge_set(isl_pw_aff_copy(pwaff1
),
3615 isl_pw_aff_copy(pwaff2
));
3616 dom
= isl_set_subtract(dom
, isl_set_copy(ge
));
3617 return isl_pw_aff_select(ge
, pwaff1
, dom
, pwaff2
);
3620 /* Return an expression for the minimum (if "max" is not set) or
3621 * the maximum (if "max" is set) of "pa1" and "pa2".
3622 * If either expression involves any NaN, then return a NaN
3623 * on the shared domain as result.
3625 static __isl_give isl_pw_aff
*pw_aff_min_max(__isl_take isl_pw_aff
*pa1
,
3626 __isl_take isl_pw_aff
*pa2
, int max
)
3630 has_nan
= either_involves_nan(pa1
, pa2
);
3632 pa1
= isl_pw_aff_free(pa1
);
3634 return replace_by_nan(pa1
, pa2
);
3636 isl_pw_aff_align_params_bin(&pa1
, &pa2
);
3638 return pw_aff_max(pa1
, pa2
);
3640 return pw_aff_min(pa1
, pa2
);
3643 /* Return an expression for the minimum of "pwaff1" and "pwaff2".
3645 __isl_give isl_pw_aff
*isl_pw_aff_min(__isl_take isl_pw_aff
*pwaff1
,
3646 __isl_take isl_pw_aff
*pwaff2
)
3648 return pw_aff_min_max(pwaff1
, pwaff2
, 0);
3651 /* Return an expression for the maximum of "pwaff1" and "pwaff2".
3653 __isl_give isl_pw_aff
*isl_pw_aff_max(__isl_take isl_pw_aff
*pwaff1
,
3654 __isl_take isl_pw_aff
*pwaff2
)
3656 return pw_aff_min_max(pwaff1
, pwaff2
, 1);
3659 static __isl_give isl_pw_aff
*pw_aff_list_reduce(
3660 __isl_take isl_pw_aff_list
*list
,
3661 __isl_give isl_pw_aff
*(*fn
)(__isl_take isl_pw_aff
*pwaff1
,
3662 __isl_take isl_pw_aff
*pwaff2
))
3671 ctx
= isl_pw_aff_list_get_ctx(list
);
3673 isl_die(ctx
, isl_error_invalid
,
3674 "list should contain at least one element", goto error
);
3676 res
= isl_pw_aff_copy(list
->p
[0]);
3677 for (i
= 1; i
< list
->n
; ++i
)
3678 res
= fn(res
, isl_pw_aff_copy(list
->p
[i
]));
3680 isl_pw_aff_list_free(list
);
3683 isl_pw_aff_list_free(list
);
3687 /* Return an isl_pw_aff that maps each element in the intersection of the
3688 * domains of the elements of list to the minimal corresponding affine
3691 __isl_give isl_pw_aff
*isl_pw_aff_list_min(__isl_take isl_pw_aff_list
*list
)
3693 return pw_aff_list_reduce(list
, &isl_pw_aff_min
);
3696 /* Return an isl_pw_aff that maps each element in the intersection of the
3697 * domains of the elements of list to the maximal corresponding affine
3700 __isl_give isl_pw_aff
*isl_pw_aff_list_max(__isl_take isl_pw_aff_list
*list
)
3702 return pw_aff_list_reduce(list
, &isl_pw_aff_max
);
3705 /* Mark the domains of "pwaff" as rational.
3707 __isl_give isl_pw_aff
*isl_pw_aff_set_rational(__isl_take isl_pw_aff
*pwaff
)
3711 pwaff
= isl_pw_aff_cow(pwaff
);
3717 for (i
= 0; i
< pwaff
->n
; ++i
) {
3718 pwaff
->p
[i
].set
= isl_set_set_rational(pwaff
->p
[i
].set
);
3719 if (!pwaff
->p
[i
].set
)
3720 return isl_pw_aff_free(pwaff
);
3726 /* Mark the domains of the elements of "list" as rational.
3728 __isl_give isl_pw_aff_list
*isl_pw_aff_list_set_rational(
3729 __isl_take isl_pw_aff_list
*list
)
3739 for (i
= 0; i
< n
; ++i
) {
3742 pa
= isl_pw_aff_list_get_pw_aff(list
, i
);
3743 pa
= isl_pw_aff_set_rational(pa
);
3744 list
= isl_pw_aff_list_set_pw_aff(list
, i
, pa
);
3750 /* Do the parameters of "aff" match those of "space"?
3752 isl_bool
isl_aff_matching_params(__isl_keep isl_aff
*aff
,
3753 __isl_keep isl_space
*space
)
3755 isl_space
*aff_space
;
3759 return isl_bool_error
;
3761 aff_space
= isl_aff_get_domain_space(aff
);
3763 match
= isl_space_has_equal_params(space
, aff_space
);
3765 isl_space_free(aff_space
);
3769 /* Check that the domain space of "aff" matches "space".
3771 isl_stat
isl_aff_check_match_domain_space(__isl_keep isl_aff
*aff
,
3772 __isl_keep isl_space
*space
)
3774 isl_space
*aff_space
;
3778 return isl_stat_error
;
3780 aff_space
= isl_aff_get_domain_space(aff
);
3782 match
= isl_space_has_equal_params(space
, aff_space
);
3786 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
3787 "parameters don't match", goto error
);
3788 match
= isl_space_tuple_is_equal(space
, isl_dim_in
,
3789 aff_space
, isl_dim_set
);
3793 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
3794 "domains don't match", goto error
);
3795 isl_space_free(aff_space
);
3798 isl_space_free(aff_space
);
3799 return isl_stat_error
;
3802 /* Return the shared (universe) domain of the elements of "ma".
3804 * Since an isl_multi_aff (and an isl_aff) is always total,
3805 * the domain is always the universe set in its domain space.
3806 * This is a helper function for use in the generic isl_multi_*_bind.
3808 static __isl_give isl_basic_set
*isl_multi_aff_domain(
3809 __isl_take isl_multi_aff
*ma
)
3813 space
= isl_multi_aff_get_space(ma
);
3814 isl_multi_aff_free(ma
);
3816 return isl_basic_set_universe(isl_space_domain(space
));
3822 #include <isl_multi_no_explicit_domain.c>
3823 #include <isl_multi_templ.c>
3824 #include <isl_multi_add_constant_templ.c>
3825 #include <isl_multi_apply_set.c>
3826 #include <isl_multi_arith_templ.c>
3827 #include <isl_multi_bind_domain_templ.c>
3828 #include <isl_multi_cmp.c>
3829 #include <isl_multi_dim_id_templ.c>
3830 #include <isl_multi_dims.c>
3831 #include <isl_multi_floor.c>
3832 #include <isl_multi_from_base_templ.c>
3833 #include <isl_multi_identity_templ.c>
3834 #include <isl_multi_insert_domain_templ.c>
3835 #include <isl_multi_locals_templ.c>
3836 #include <isl_multi_move_dims_templ.c>
3837 #include <isl_multi_nan_templ.c>
3838 #include <isl_multi_product_templ.c>
3839 #include <isl_multi_splice_templ.c>
3840 #include <isl_multi_tuple_id_templ.c>
3841 #include <isl_multi_unbind_params_templ.c>
3842 #include <isl_multi_zero_templ.c>
3846 #include <isl_multi_gist.c>
3849 #define DOMBASE basic_set
3850 #include <isl_multi_bind_templ.c>
3852 /* Construct an isl_multi_aff living in "space" that corresponds
3853 * to the affine transformation matrix "mat".
3855 __isl_give isl_multi_aff
*isl_multi_aff_from_aff_mat(
3856 __isl_take isl_space
*space
, __isl_take isl_mat
*mat
)
3859 isl_local_space
*ls
= NULL
;
3860 isl_multi_aff
*ma
= NULL
;
3861 isl_size n_row
, n_col
, n_out
, total
;
3867 ctx
= isl_mat_get_ctx(mat
);
3869 n_row
= isl_mat_rows(mat
);
3870 n_col
= isl_mat_cols(mat
);
3871 n_out
= isl_space_dim(space
, isl_dim_out
);
3872 total
= isl_space_dim(space
, isl_dim_all
);
3873 if (n_row
< 0 || n_col
< 0 || n_out
< 0 || total
< 0)
3876 isl_die(ctx
, isl_error_invalid
,
3877 "insufficient number of rows", goto error
);
3879 isl_die(ctx
, isl_error_invalid
,
3880 "insufficient number of columns", goto error
);
3881 if (1 + n_out
!= n_row
|| 2 + total
!= n_row
+ n_col
)
3882 isl_die(ctx
, isl_error_invalid
,
3883 "dimension mismatch", goto error
);
3885 ma
= isl_multi_aff_zero(isl_space_copy(space
));
3886 space
= isl_space_domain(space
);
3887 ls
= isl_local_space_from_space(isl_space_copy(space
));
3889 for (i
= 0; i
< n_row
- 1; ++i
) {
3893 v
= isl_vec_alloc(ctx
, 1 + n_col
);
3896 isl_int_set(v
->el
[0], mat
->row
[0][0]);
3897 isl_seq_cpy(v
->el
+ 1, mat
->row
[1 + i
], n_col
);
3898 v
= isl_vec_normalize(v
);
3899 aff
= isl_aff_alloc_vec(isl_local_space_copy(ls
), v
);
3900 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
3903 isl_space_free(space
);
3904 isl_local_space_free(ls
);
3908 isl_space_free(space
);
3909 isl_local_space_free(ls
);
3911 isl_multi_aff_free(ma
);
3915 /* Return the constant terms of the affine expressions of "ma".
3917 __isl_give isl_multi_val
*isl_multi_aff_get_constant_multi_val(
3918 __isl_keep isl_multi_aff
*ma
)
3925 n
= isl_multi_aff_size(ma
);
3928 space
= isl_space_range(isl_multi_aff_get_space(ma
));
3929 space
= isl_space_drop_all_params(space
);
3930 mv
= isl_multi_val_zero(space
);
3932 for (i
= 0; i
< n
; ++i
) {
3936 aff
= isl_multi_aff_get_at(ma
, i
);
3937 val
= isl_aff_get_constant_val(aff
);
3939 mv
= isl_multi_val_set_at(mv
, i
, val
);
3945 /* Remove any internal structure of the domain of "ma".
3946 * If there is any such internal structure in the input,
3947 * then the name of the corresponding space is also removed.
3949 __isl_give isl_multi_aff
*isl_multi_aff_flatten_domain(
3950 __isl_take isl_multi_aff
*ma
)
3957 if (!ma
->space
->nested
[0])
3960 space
= isl_multi_aff_get_space(ma
);
3961 space
= isl_space_flatten_domain(space
);
3962 ma
= isl_multi_aff_reset_space(ma
, space
);
3967 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
3968 * of the space to its domain.
3970 __isl_give isl_multi_aff
*isl_multi_aff_domain_map(__isl_take isl_space
*space
)
3974 isl_local_space
*ls
;
3979 if (!isl_space_is_map(space
))
3980 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
3981 "not a map space", goto error
);
3983 n_in
= isl_space_dim(space
, isl_dim_in
);
3986 space
= isl_space_domain_map(space
);
3988 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
3990 isl_space_free(space
);
3994 space
= isl_space_domain(space
);
3995 ls
= isl_local_space_from_space(space
);
3996 for (i
= 0; i
< n_in
; ++i
) {
3999 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
4001 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
4003 isl_local_space_free(ls
);
4006 isl_space_free(space
);
4010 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
4011 * of the space to its range.
4013 __isl_give isl_multi_aff
*isl_multi_aff_range_map(__isl_take isl_space
*space
)
4016 isl_size n_in
, n_out
;
4017 isl_local_space
*ls
;
4022 if (!isl_space_is_map(space
))
4023 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
4024 "not a map space", goto error
);
4026 n_in
= isl_space_dim(space
, isl_dim_in
);
4027 n_out
= isl_space_dim(space
, isl_dim_out
);
4028 if (n_in
< 0 || n_out
< 0)
4030 space
= isl_space_range_map(space
);
4032 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
4034 isl_space_free(space
);
4038 space
= isl_space_domain(space
);
4039 ls
= isl_local_space_from_space(space
);
4040 for (i
= 0; i
< n_out
; ++i
) {
4043 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
4044 isl_dim_set
, n_in
+ i
);
4045 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
4047 isl_local_space_free(ls
);
4050 isl_space_free(space
);
4054 /* Given a map space, return an isl_pw_multi_aff that maps a wrapped copy
4055 * of the space to its domain.
4057 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_domain_map(
4058 __isl_take isl_space
*space
)
4060 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_domain_map(space
));
4063 /* Given a map space, return an isl_pw_multi_aff that maps a wrapped copy
4064 * of the space to its range.
4066 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_range_map(
4067 __isl_take isl_space
*space
)
4069 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_range_map(space
));
4072 /* Given the space of a set and a range of set dimensions,
4073 * construct an isl_multi_aff that projects out those dimensions.
4075 __isl_give isl_multi_aff
*isl_multi_aff_project_out_map(
4076 __isl_take isl_space
*space
, enum isl_dim_type type
,
4077 unsigned first
, unsigned n
)
4081 isl_local_space
*ls
;
4086 if (!isl_space_is_set(space
))
4087 isl_die(isl_space_get_ctx(space
), isl_error_unsupported
,
4088 "expecting set space", goto error
);
4089 if (type
!= isl_dim_set
)
4090 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
4091 "only set dimensions can be projected out", goto error
);
4092 if (isl_space_check_range(space
, type
, first
, n
) < 0)
4095 dim
= isl_space_dim(space
, isl_dim_set
);
4099 space
= isl_space_from_domain(space
);
4100 space
= isl_space_add_dims(space
, isl_dim_out
, dim
- n
);
4103 return isl_multi_aff_alloc(space
);
4105 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
4106 space
= isl_space_domain(space
);
4107 ls
= isl_local_space_from_space(space
);
4109 for (i
= 0; i
< first
; ++i
) {
4112 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
4114 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
4117 for (i
= 0; i
< dim
- (first
+ n
); ++i
) {
4120 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
4121 isl_dim_set
, first
+ n
+ i
);
4122 ma
= isl_multi_aff_set_aff(ma
, first
+ i
, aff
);
4125 isl_local_space_free(ls
);
4128 isl_space_free(space
);
4132 /* Given the space of a set and a range of set dimensions,
4133 * construct an isl_pw_multi_aff that projects out those dimensions.
4135 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_project_out_map(
4136 __isl_take isl_space
*space
, enum isl_dim_type type
,
4137 unsigned first
, unsigned n
)
4141 ma
= isl_multi_aff_project_out_map(space
, type
, first
, n
);
4142 return isl_pw_multi_aff_from_multi_aff(ma
);
4145 /* Create a piecewise multi-affine expression in the given space that maps each
4146 * input dimension to the corresponding output dimension.
4148 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_identity(
4149 __isl_take isl_space
*space
)
4151 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_identity(space
));
4154 /* Exploit the equalities in "eq" to simplify the affine expressions.
4156 static __isl_give isl_multi_aff
*isl_multi_aff_substitute_equalities(
4157 __isl_take isl_multi_aff
*maff
, __isl_take isl_basic_set
*eq
)
4161 maff
= isl_multi_aff_cow(maff
);
4165 for (i
= 0; i
< maff
->n
; ++i
) {
4166 maff
->u
.p
[i
] = isl_aff_substitute_equalities(maff
->u
.p
[i
],
4167 isl_basic_set_copy(eq
));
4172 isl_basic_set_free(eq
);
4175 isl_basic_set_free(eq
);
4176 isl_multi_aff_free(maff
);
4180 __isl_give isl_multi_aff
*isl_multi_aff_scale(__isl_take isl_multi_aff
*maff
,
4185 maff
= isl_multi_aff_cow(maff
);
4189 for (i
= 0; i
< maff
->n
; ++i
) {
4190 maff
->u
.p
[i
] = isl_aff_scale(maff
->u
.p
[i
], f
);
4192 return isl_multi_aff_free(maff
);
4198 __isl_give isl_multi_aff
*isl_multi_aff_add_on_domain(__isl_keep isl_set
*dom
,
4199 __isl_take isl_multi_aff
*maff1
, __isl_take isl_multi_aff
*maff2
)
4201 maff1
= isl_multi_aff_add(maff1
, maff2
);
4202 maff1
= isl_multi_aff_gist(maff1
, isl_set_copy(dom
));
4206 isl_bool
isl_multi_aff_is_empty(__isl_keep isl_multi_aff
*maff
)
4209 return isl_bool_error
;
4211 return isl_bool_false
;
4214 /* Return the set of domain elements where "ma1" is lexicographically
4215 * smaller than or equal to "ma2".
4217 __isl_give isl_set
*isl_multi_aff_lex_le_set(__isl_take isl_multi_aff
*ma1
,
4218 __isl_take isl_multi_aff
*ma2
)
4220 return isl_multi_aff_lex_ge_set(ma2
, ma1
);
4223 /* Return the set of domain elements where "ma1" is lexicographically
4224 * smaller than "ma2".
4226 __isl_give isl_set
*isl_multi_aff_lex_lt_set(__isl_take isl_multi_aff
*ma1
,
4227 __isl_take isl_multi_aff
*ma2
)
4229 return isl_multi_aff_lex_gt_set(ma2
, ma1
);
4232 /* Return the set of domain elements where "ma1" is lexicographically
4233 * greater than to "ma2". If "equal" is set, then include the domain
4234 * elements where they are equal.
4235 * Do this for the case where there are no entries.
4236 * In this case, "ma1" cannot be greater than "ma2",
4237 * but it is (greater than or) equal to "ma2".
4239 static __isl_give isl_set
*isl_multi_aff_lex_gte_set_0d(
4240 __isl_take isl_multi_aff
*ma1
, __isl_take isl_multi_aff
*ma2
, int equal
)
4244 space
= isl_multi_aff_get_domain_space(ma1
);
4246 isl_multi_aff_free(ma1
);
4247 isl_multi_aff_free(ma2
);
4250 return isl_set_universe(space
);
4252 return isl_set_empty(space
);
4255 /* Return the set where entry "i" of "ma1" and "ma2"
4256 * satisfy the relation prescribed by "cmp".
4258 static __isl_give isl_set
*isl_multi_aff_order_at(__isl_keep isl_multi_aff
*ma1
,
4259 __isl_keep isl_multi_aff
*ma2
, int i
,
4260 __isl_give isl_set
*(*cmp
)(__isl_take isl_aff
*aff1
,
4261 __isl_take isl_aff
*aff2
))
4263 isl_aff
*aff1
, *aff2
;
4265 aff1
= isl_multi_aff_get_at(ma1
, i
);
4266 aff2
= isl_multi_aff_get_at(ma2
, i
);
4267 return cmp(aff1
, aff2
);
4270 /* Return the set of domain elements where "ma1" is lexicographically
4271 * greater than to "ma2". If "equal" is set, then include the domain
4272 * elements where they are equal.
4274 * In particular, for all but the final entry,
4275 * include the set of elements where this entry is strictly greater in "ma1"
4276 * and all previous entries are equal.
4277 * The final entry is also allowed to be equal in the two functions
4278 * if "equal" is set.
4280 * The case where there are no entries is handled separately.
4282 static __isl_give isl_set
*isl_multi_aff_lex_gte_set(
4283 __isl_take isl_multi_aff
*ma1
, __isl_take isl_multi_aff
*ma2
, int equal
)
4292 if (isl_multi_aff_check_equal_space(ma1
, ma2
) < 0)
4294 n
= isl_multi_aff_size(ma1
);
4298 return isl_multi_aff_lex_gte_set_0d(ma1
, ma2
, equal
);
4300 space
= isl_multi_aff_get_domain_space(ma1
);
4301 res
= isl_set_empty(isl_space_copy(space
));
4302 equal_set
= isl_set_universe(space
);
4304 for (i
= 0; i
+ 1 < n
; ++i
) {
4308 gt
= isl_multi_aff_order_at(ma1
, ma2
, i
, &isl_aff_gt_set
);
4309 gt
= isl_set_intersect(gt
, isl_set_copy(equal_set
));
4310 res
= isl_set_union(res
, gt
);
4311 eq
= isl_multi_aff_order_at(ma1
, ma2
, i
, &isl_aff_eq_set
);
4312 equal_set
= isl_set_intersect(equal_set
, eq
);
4314 empty
= isl_set_is_empty(equal_set
);
4315 if (empty
>= 0 && empty
)
4320 gte
= isl_multi_aff_order_at(ma1
, ma2
, n
- 1, &isl_aff_ge_set
);
4322 gte
= isl_multi_aff_order_at(ma1
, ma2
, n
- 1, &isl_aff_gt_set
);
4323 isl_multi_aff_free(ma1
);
4324 isl_multi_aff_free(ma2
);
4326 gte
= isl_set_intersect(gte
, equal_set
);
4327 return isl_set_union(res
, gte
);
4329 isl_multi_aff_free(ma1
);
4330 isl_multi_aff_free(ma2
);
4334 /* Return the set of domain elements where "ma1" is lexicographically
4335 * greater than or equal to "ma2".
4337 __isl_give isl_set
*isl_multi_aff_lex_ge_set(__isl_take isl_multi_aff
*ma1
,
4338 __isl_take isl_multi_aff
*ma2
)
4340 return isl_multi_aff_lex_gte_set(ma1
, ma2
, 1);
4343 /* Return the set of domain elements where "ma1" is lexicographically
4344 * greater than "ma2".
4346 __isl_give isl_set
*isl_multi_aff_lex_gt_set(__isl_take isl_multi_aff
*ma1
,
4347 __isl_take isl_multi_aff
*ma2
)
4349 return isl_multi_aff_lex_gte_set(ma1
, ma2
, 0);
4352 #define isl_multi_aff_zero_in_space isl_multi_aff_zero
4355 #define PW isl_pw_multi_aff
4357 #define BASE multi_aff
4359 #define EL_IS_ZERO is_empty
4363 #define IS_ZERO is_empty
4366 #undef DEFAULT_IS_ZERO
4367 #define DEFAULT_IS_ZERO 0
4369 #include <isl_pw_templ.c>
4370 #include <isl_pw_add_constant_multi_val_templ.c>
4371 #include <isl_pw_add_constant_val_templ.c>
4372 #include <isl_pw_bind_domain_templ.c>
4373 #include <isl_pw_insert_dims_templ.c>
4374 #include <isl_pw_insert_domain_templ.c>
4375 #include <isl_pw_locals_templ.c>
4376 #include <isl_pw_move_dims_templ.c>
4377 #include <isl_pw_neg_templ.c>
4378 #include <isl_pw_pullback_templ.c>
4379 #include <isl_pw_union_opt.c>
4382 #define BASE pw_multi_aff
4384 #include <isl_union_multi.c>
4385 #include "isl_union_locals_templ.c"
4386 #include <isl_union_neg.c>
4389 #define BASE multi_aff
4391 #include <isl_union_pw_templ.c>
4393 /* Generic function for extracting a factor from a product "pma".
4394 * "check_space" checks that the space is that of the right kind of product.
4395 * "space_factor" extracts the factor from the space.
4396 * "multi_aff_factor" extracts the factor from the constituent functions.
4398 static __isl_give isl_pw_multi_aff
*pw_multi_aff_factor(
4399 __isl_take isl_pw_multi_aff
*pma
,
4400 isl_stat (*check_space
)(__isl_keep isl_pw_multi_aff
*pma
),
4401 __isl_give isl_space
*(*space_factor
)(__isl_take isl_space
*space
),
4402 __isl_give isl_multi_aff
*(*multi_aff_factor
)(
4403 __isl_take isl_multi_aff
*ma
))
4408 if (check_space(pma
) < 0)
4409 return isl_pw_multi_aff_free(pma
);
4411 space
= isl_pw_multi_aff_take_space(pma
);
4412 space
= space_factor(space
);
4414 for (i
= 0; pma
&& i
< pma
->n
; ++i
) {
4417 ma
= isl_pw_multi_aff_take_base_at(pma
, i
);
4418 ma
= multi_aff_factor(ma
);
4419 pma
= isl_pw_multi_aff_restore_base_at(pma
, i
, ma
);
4422 pma
= isl_pw_multi_aff_restore_space(pma
, space
);
4427 /* Is the range of "pma" a wrapped relation?
4429 static isl_bool
isl_pw_multi_aff_range_is_wrapping(
4430 __isl_keep isl_pw_multi_aff
*pma
)
4432 return isl_space_range_is_wrapping(isl_pw_multi_aff_peek_space(pma
));
4435 /* Check that the range of "pma" is a product.
4437 static isl_stat
pw_multi_aff_check_range_product(
4438 __isl_keep isl_pw_multi_aff
*pma
)
4442 wraps
= isl_pw_multi_aff_range_is_wrapping(pma
);
4444 return isl_stat_error
;
4446 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
4447 "range is not a product", return isl_stat_error
);
4451 /* Given a function A -> [B -> C], extract the function A -> B.
4453 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_range_factor_domain(
4454 __isl_take isl_pw_multi_aff
*pma
)
4456 return pw_multi_aff_factor(pma
, &pw_multi_aff_check_range_product
,
4457 &isl_space_range_factor_domain
,
4458 &isl_multi_aff_range_factor_domain
);
4461 /* Given a function A -> [B -> C], extract the function A -> C.
4463 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_range_factor_range(
4464 __isl_take isl_pw_multi_aff
*pma
)
4466 return pw_multi_aff_factor(pma
, &pw_multi_aff_check_range_product
,
4467 &isl_space_range_factor_range
,
4468 &isl_multi_aff_range_factor_range
);
4471 /* Given two piecewise multi affine expressions, return a piecewise
4472 * multi-affine expression defined on the union of the definition domains
4473 * of the inputs that is equal to the lexicographic maximum of the two
4474 * inputs on each cell. If only one of the two inputs is defined on
4475 * a given cell, then it is considered to be the maximum.
4477 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_union_lexmax(
4478 __isl_take isl_pw_multi_aff
*pma1
,
4479 __isl_take isl_pw_multi_aff
*pma2
)
4481 isl_pw_multi_aff_align_params_bin(&pma1
, &pma2
);
4482 return isl_pw_multi_aff_union_opt_cmp(pma1
, pma2
,
4483 &isl_multi_aff_lex_ge_set
);
4486 /* Given two piecewise multi affine expressions, return a piecewise
4487 * multi-affine expression defined on the union of the definition domains
4488 * of the inputs that is equal to the lexicographic minimum of the two
4489 * inputs on each cell. If only one of the two inputs is defined on
4490 * a given cell, then it is considered to be the minimum.
4492 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_union_lexmin(
4493 __isl_take isl_pw_multi_aff
*pma1
,
4494 __isl_take isl_pw_multi_aff
*pma2
)
4496 isl_pw_multi_aff_align_params_bin(&pma1
, &pma2
);
4497 return isl_pw_multi_aff_union_opt_cmp(pma1
, pma2
,
4498 &isl_multi_aff_lex_le_set
);
4501 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_add(
4502 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4504 isl_pw_multi_aff_align_params_bin(&pma1
, &pma2
);
4505 return isl_pw_multi_aff_on_shared_domain(pma1
, pma2
,
4506 &isl_multi_aff_add
);
4509 /* Subtract "pma2" from "pma1" and return the result.
4511 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_sub(
4512 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4514 isl_pw_multi_aff_align_params_bin(&pma1
, &pma2
);
4515 return isl_pw_multi_aff_on_shared_domain(pma1
, pma2
,
4516 &isl_multi_aff_sub
);
4519 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_union_add(
4520 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4522 return isl_pw_multi_aff_union_add_(pma1
, pma2
);
4525 /* Compute the sum of "upa1" and "upa2" on the union of their domains,
4526 * with the actual sum on the shared domain and
4527 * the defined expression on the symmetric difference of the domains.
4529 __isl_give isl_union_pw_aff
*isl_union_pw_aff_union_add(
4530 __isl_take isl_union_pw_aff
*upa1
, __isl_take isl_union_pw_aff
*upa2
)
4532 return isl_union_pw_aff_union_add_(upa1
, upa2
);
4535 /* Compute the sum of "upma1" and "upma2" on the union of their domains,
4536 * with the actual sum on the shared domain and
4537 * the defined expression on the symmetric difference of the domains.
4539 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_union_add(
4540 __isl_take isl_union_pw_multi_aff
*upma1
,
4541 __isl_take isl_union_pw_multi_aff
*upma2
)
4543 return isl_union_pw_multi_aff_union_add_(upma1
, upma2
);
4546 /* Given two piecewise multi-affine expressions A -> B and C -> D,
4547 * construct a piecewise multi-affine expression [A -> C] -> [B -> D].
4549 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_product(
4550 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4554 isl_pw_multi_aff
*res
;
4556 if (isl_pw_multi_aff_align_params_bin(&pma1
, &pma2
) < 0)
4559 n
= pma1
->n
* pma2
->n
;
4560 space
= isl_space_product(isl_space_copy(pma1
->dim
),
4561 isl_space_copy(pma2
->dim
));
4562 res
= isl_pw_multi_aff_alloc_size(space
, n
);
4564 for (i
= 0; i
< pma1
->n
; ++i
) {
4565 for (j
= 0; j
< pma2
->n
; ++j
) {
4569 domain
= isl_set_product(isl_set_copy(pma1
->p
[i
].set
),
4570 isl_set_copy(pma2
->p
[j
].set
));
4571 ma
= isl_multi_aff_product(
4572 isl_multi_aff_copy(pma1
->p
[i
].maff
),
4573 isl_multi_aff_copy(pma2
->p
[j
].maff
));
4574 res
= isl_pw_multi_aff_add_piece(res
, domain
, ma
);
4578 isl_pw_multi_aff_free(pma1
);
4579 isl_pw_multi_aff_free(pma2
);
4582 isl_pw_multi_aff_free(pma1
);
4583 isl_pw_multi_aff_free(pma2
);
4587 /* Subtract the initial "n" elements in "ma" with coefficients in "c" and
4588 * denominator "denom".
4589 * "denom" is allowed to be negative, in which case the actual denominator
4590 * is -denom and the expressions are added instead.
4592 static __isl_give isl_aff
*subtract_initial(__isl_take isl_aff
*aff
,
4593 __isl_keep isl_multi_aff
*ma
, int n
, isl_int
*c
, isl_int denom
)
4599 first
= isl_seq_first_non_zero(c
, n
);
4603 sign
= isl_int_sgn(denom
);
4605 isl_int_abs(d
, denom
);
4606 for (i
= first
; i
< n
; ++i
) {
4609 if (isl_int_is_zero(c
[i
]))
4611 aff_i
= isl_multi_aff_get_aff(ma
, i
);
4612 aff_i
= isl_aff_scale(aff_i
, c
[i
]);
4613 aff_i
= isl_aff_scale_down(aff_i
, d
);
4615 aff
= isl_aff_sub(aff
, aff_i
);
4617 aff
= isl_aff_add(aff
, aff_i
);
4624 /* Extract an affine expression that expresses the output dimension "pos"
4625 * of "bmap" in terms of the parameters and input dimensions from
4627 * Note that this expression may involve integer divisions defined
4628 * in terms of parameters and input dimensions.
4629 * The equality may also involve references to earlier (but not later)
4630 * output dimensions. These are replaced by the corresponding elements
4633 * If the equality is of the form
4635 * f(i) + h(j) + a x + g(i) = 0,
4637 * with f(i) a linear combinations of the parameters and input dimensions,
4638 * g(i) a linear combination of integer divisions defined in terms of the same
4639 * and h(j) a linear combinations of earlier output dimensions,
4640 * then the affine expression is
4642 * (-f(i) - g(i))/a - h(j)/a
4644 * If the equality is of the form
4646 * f(i) + h(j) - a x + g(i) = 0,
4648 * then the affine expression is
4650 * (f(i) + g(i))/a - h(j)/(-a)
4653 * If "div" refers to an integer division (i.e., it is smaller than
4654 * the number of integer divisions), then the equality constraint
4655 * does involve an integer division (the one at position "div") that
4656 * is defined in terms of output dimensions. However, this integer
4657 * division can be eliminated by exploiting a pair of constraints
4658 * x >= l and x <= l + n, with n smaller than the coefficient of "div"
4659 * in the equality constraint. "ineq" refers to inequality x >= l, i.e.,
4661 * In particular, let
4663 * x = e(i) + m floor(...)
4665 * with e(i) the expression derived above and floor(...) the integer
4666 * division involving output dimensions.
4677 * e(i) + m floor(...) - l = (e(i) + m floor(...) - l) mod m
4678 * = (e(i) - l) mod m
4682 * x - l = (e(i) - l) mod m
4686 * x = ((e(i) - l) mod m) + l
4688 * The variable "shift" below contains the expression -l, which may
4689 * also involve a linear combination of earlier output dimensions.
4691 static __isl_give isl_aff
*extract_aff_from_equality(
4692 __isl_keep isl_basic_map
*bmap
, int pos
, int eq
, int div
, int ineq
,
4693 __isl_keep isl_multi_aff
*ma
)
4696 isl_size n_div
, n_out
;
4698 isl_local_space
*ls
;
4699 isl_aff
*aff
, *shift
;
4702 ctx
= isl_basic_map_get_ctx(bmap
);
4703 ls
= isl_basic_map_get_local_space(bmap
);
4704 ls
= isl_local_space_domain(ls
);
4705 aff
= isl_aff_alloc(isl_local_space_copy(ls
));
4708 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
4709 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4710 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4711 if (n_out
< 0 || n_div
< 0)
4713 if (isl_int_is_neg(bmap
->eq
[eq
][o_out
+ pos
])) {
4714 isl_seq_cpy(aff
->v
->el
+ 1, bmap
->eq
[eq
], o_out
);
4715 isl_seq_cpy(aff
->v
->el
+ 1 + o_out
,
4716 bmap
->eq
[eq
] + o_out
+ n_out
, n_div
);
4718 isl_seq_neg(aff
->v
->el
+ 1, bmap
->eq
[eq
], o_out
);
4719 isl_seq_neg(aff
->v
->el
+ 1 + o_out
,
4720 bmap
->eq
[eq
] + o_out
+ n_out
, n_div
);
4723 isl_int_set_si(aff
->v
->el
[1 + o_out
+ div
], 0);
4724 isl_int_abs(aff
->v
->el
[0], bmap
->eq
[eq
][o_out
+ pos
]);
4725 aff
= subtract_initial(aff
, ma
, pos
, bmap
->eq
[eq
] + o_out
,
4726 bmap
->eq
[eq
][o_out
+ pos
]);
4728 shift
= isl_aff_alloc(isl_local_space_copy(ls
));
4731 isl_seq_cpy(shift
->v
->el
+ 1, bmap
->ineq
[ineq
], o_out
);
4732 isl_seq_cpy(shift
->v
->el
+ 1 + o_out
,
4733 bmap
->ineq
[ineq
] + o_out
+ n_out
, n_div
);
4734 isl_int_set_si(shift
->v
->el
[0], 1);
4735 shift
= subtract_initial(shift
, ma
, pos
,
4736 bmap
->ineq
[ineq
] + o_out
, ctx
->negone
);
4737 aff
= isl_aff_add(aff
, isl_aff_copy(shift
));
4738 mod
= isl_val_int_from_isl_int(ctx
,
4739 bmap
->eq
[eq
][o_out
+ n_out
+ div
]);
4740 mod
= isl_val_abs(mod
);
4741 aff
= isl_aff_mod_val(aff
, mod
);
4742 aff
= isl_aff_sub(aff
, shift
);
4745 isl_local_space_free(ls
);
4748 isl_local_space_free(ls
);
4753 /* Given a basic map with output dimensions defined
4754 * in terms of the parameters input dimensions and earlier
4755 * output dimensions using an equality (and possibly a pair on inequalities),
4756 * extract an isl_aff that expresses output dimension "pos" in terms
4757 * of the parameters and input dimensions.
4758 * Note that this expression may involve integer divisions defined
4759 * in terms of parameters and input dimensions.
4760 * "ma" contains the expressions corresponding to earlier output dimensions.
4762 * This function shares some similarities with
4763 * isl_basic_map_has_defining_equality and isl_constraint_get_bound.
4765 static __isl_give isl_aff
*extract_isl_aff_from_basic_map(
4766 __isl_keep isl_basic_map
*bmap
, int pos
, __isl_keep isl_multi_aff
*ma
)
4773 eq
= isl_basic_map_output_defining_equality(bmap
, pos
, &div
, &ineq
);
4774 if (eq
>= bmap
->n_eq
)
4775 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
4776 "unable to find suitable equality", return NULL
);
4777 aff
= extract_aff_from_equality(bmap
, pos
, eq
, div
, ineq
, ma
);
4779 aff
= isl_aff_remove_unused_divs(aff
);
4783 /* Given a basic map where each output dimension is defined
4784 * in terms of the parameters and input dimensions using an equality,
4785 * extract an isl_multi_aff that expresses the output dimensions in terms
4786 * of the parameters and input dimensions.
4788 static __isl_give isl_multi_aff
*extract_isl_multi_aff_from_basic_map(
4789 __isl_take isl_basic_map
*bmap
)
4798 ma
= isl_multi_aff_alloc(isl_basic_map_get_space(bmap
));
4799 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4801 ma
= isl_multi_aff_free(ma
);
4803 for (i
= 0; i
< n_out
; ++i
) {
4806 aff
= extract_isl_aff_from_basic_map(bmap
, i
, ma
);
4807 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
4810 isl_basic_map_free(bmap
);
4815 /* Given a basic set where each set dimension is defined
4816 * in terms of the parameters using an equality,
4817 * extract an isl_multi_aff that expresses the set dimensions in terms
4818 * of the parameters.
4820 __isl_give isl_multi_aff
*isl_multi_aff_from_basic_set_equalities(
4821 __isl_take isl_basic_set
*bset
)
4823 return extract_isl_multi_aff_from_basic_map(bset
);
4826 /* Create an isl_pw_multi_aff that is equivalent to
4827 * isl_map_intersect_domain(isl_map_from_basic_map(bmap), domain).
4828 * The given basic map is such that each output dimension is defined
4829 * in terms of the parameters and input dimensions using an equality.
4831 * Since some applications expect the result of isl_pw_multi_aff_from_map
4832 * to only contain integer affine expressions, we compute the floor
4833 * of the expression before returning.
4835 * Remove all constraints involving local variables without
4836 * an explicit representation (resulting in the removal of those
4837 * local variables) prior to the actual extraction to ensure
4838 * that the local spaces in which the resulting affine expressions
4839 * are created do not contain any unknown local variables.
4840 * Removing such constraints is safe because constraints involving
4841 * unknown local variables are not used to determine whether
4842 * a basic map is obviously single-valued.
4844 static __isl_give isl_pw_multi_aff
*plain_pw_multi_aff_from_map(
4845 __isl_take isl_set
*domain
, __isl_take isl_basic_map
*bmap
)
4849 bmap
= isl_basic_map_drop_constraints_involving_unknown_divs(bmap
);
4850 ma
= extract_isl_multi_aff_from_basic_map(bmap
);
4851 ma
= isl_multi_aff_floor(ma
);
4852 return isl_pw_multi_aff_alloc(domain
, ma
);
4855 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4856 * This obviously only works if the input "map" is single-valued.
4857 * If so, we compute the lexicographic minimum of the image in the form
4858 * of an isl_pw_multi_aff. Since the image is unique, it is equal
4859 * to its lexicographic minimum.
4860 * If the input is not single-valued, we produce an error.
4862 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_base(
4863 __isl_take isl_map
*map
)
4867 isl_pw_multi_aff
*pma
;
4869 sv
= isl_map_is_single_valued(map
);
4873 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
4874 "map is not single-valued", goto error
);
4875 map
= isl_map_make_disjoint(map
);
4879 pma
= isl_pw_multi_aff_empty(isl_map_get_space(map
));
4881 for (i
= 0; i
< map
->n
; ++i
) {
4882 isl_pw_multi_aff
*pma_i
;
4883 isl_basic_map
*bmap
;
4884 bmap
= isl_basic_map_copy(map
->p
[i
]);
4885 pma_i
= isl_basic_map_lexmin_pw_multi_aff(bmap
);
4886 pma
= isl_pw_multi_aff_add_disjoint(pma
, pma_i
);
4896 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
4897 * taking into account that the output dimension at position "d"
4898 * can be represented as
4900 * x = floor((e(...) + c1) / m)
4902 * given that constraint "i" is of the form
4904 * e(...) + c1 - m x >= 0
4907 * Let "map" be of the form
4911 * We construct a mapping
4913 * A -> [A -> x = floor(...)]
4915 * apply that to the map, obtaining
4917 * [A -> x = floor(...)] -> B
4919 * and equate dimension "d" to x.
4920 * We then compute a isl_pw_multi_aff representation of the resulting map
4921 * and plug in the mapping above.
4923 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_div(
4924 __isl_take isl_map
*map
, __isl_take isl_basic_map
*hull
, int d
, int i
)
4927 isl_space
*space
= NULL
;
4928 isl_local_space
*ls
;
4936 isl_pw_multi_aff
*pma
;
4939 is_set
= isl_map_is_set(map
);
4943 offset
= isl_basic_map_offset(hull
, isl_dim_out
);
4944 ctx
= isl_map_get_ctx(map
);
4945 space
= isl_space_domain(isl_map_get_space(map
));
4946 n_in
= isl_space_dim(space
, isl_dim_set
);
4947 n
= isl_space_dim(space
, isl_dim_all
);
4948 if (n_in
< 0 || n
< 0)
4951 v
= isl_vec_alloc(ctx
, 1 + 1 + n
);
4953 isl_int_neg(v
->el
[0], hull
->ineq
[i
][offset
+ d
]);
4954 isl_seq_cpy(v
->el
+ 1, hull
->ineq
[i
], 1 + n
);
4956 isl_basic_map_free(hull
);
4958 ls
= isl_local_space_from_space(isl_space_copy(space
));
4959 aff
= isl_aff_alloc_vec(ls
, v
);
4960 aff
= isl_aff_floor(aff
);
4962 isl_space_free(space
);
4963 ma
= isl_multi_aff_from_aff(aff
);
4965 ma
= isl_multi_aff_identity(isl_space_map_from_set(space
));
4966 ma
= isl_multi_aff_range_product(ma
,
4967 isl_multi_aff_from_aff(aff
));
4970 insert
= isl_map_from_multi_aff_internal(isl_multi_aff_copy(ma
));
4971 map
= isl_map_apply_domain(map
, insert
);
4972 map
= isl_map_equate(map
, isl_dim_in
, n_in
, isl_dim_out
, d
);
4973 pma
= isl_pw_multi_aff_from_map(map
);
4974 pma
= isl_pw_multi_aff_pullback_multi_aff(pma
, ma
);
4978 isl_space_free(space
);
4980 isl_basic_map_free(hull
);
4984 /* Is constraint "c" of the form
4986 * e(...) + c1 - m x >= 0
4990 * -e(...) + c2 + m x >= 0
4992 * where m > 1 and e only depends on parameters and input dimemnsions?
4994 * "offset" is the offset of the output dimensions
4995 * "pos" is the position of output dimension x.
4997 static int is_potential_div_constraint(isl_int
*c
, int offset
, int d
, int total
)
4999 if (isl_int_is_zero(c
[offset
+ d
]))
5001 if (isl_int_is_one(c
[offset
+ d
]))
5003 if (isl_int_is_negone(c
[offset
+ d
]))
5005 if (isl_seq_first_non_zero(c
+ offset
, d
) != -1)
5007 if (isl_seq_first_non_zero(c
+ offset
+ d
+ 1,
5008 total
- (offset
+ d
+ 1)) != -1)
5013 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5015 * As a special case, we first check if there is any pair of constraints,
5016 * shared by all the basic maps in "map" that force a given dimension
5017 * to be equal to the floor of some affine combination of the input dimensions.
5019 * In particular, if we can find two constraints
5021 * e(...) + c1 - m x >= 0 i.e., m x <= e(...) + c1
5025 * -e(...) + c2 + m x >= 0 i.e., m x >= e(...) - c2
5027 * where m > 1 and e only depends on parameters and input dimemnsions,
5030 * c1 + c2 < m i.e., -c2 >= c1 - (m - 1)
5032 * then we know that we can take
5034 * x = floor((e(...) + c1) / m)
5036 * without having to perform any computation.
5038 * Note that we know that
5042 * If c1 + c2 were 0, then we would have detected an equality during
5043 * simplification. If c1 + c2 were negative, then we would have detected
5046 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_check_div(
5047 __isl_take isl_map
*map
)
5055 isl_basic_map
*hull
;
5057 hull
= isl_map_unshifted_simple_hull(isl_map_copy(map
));
5058 dim
= isl_map_dim(map
, isl_dim_out
);
5059 total
= isl_basic_map_dim(hull
, isl_dim_all
);
5060 if (dim
< 0 || total
< 0)
5064 offset
= isl_basic_map_offset(hull
, isl_dim_out
);
5066 for (d
= 0; d
< dim
; ++d
) {
5067 for (i
= 0; i
< n
; ++i
) {
5068 if (!is_potential_div_constraint(hull
->ineq
[i
],
5069 offset
, d
, 1 + total
))
5071 for (j
= i
+ 1; j
< n
; ++j
) {
5072 if (!isl_seq_is_neg(hull
->ineq
[i
] + 1,
5073 hull
->ineq
[j
] + 1, total
))
5075 isl_int_add(sum
, hull
->ineq
[i
][0],
5077 if (isl_int_abs_lt(sum
,
5078 hull
->ineq
[i
][offset
+ d
]))
5085 if (isl_int_is_pos(hull
->ineq
[j
][offset
+ d
]))
5087 return pw_multi_aff_from_map_div(map
, hull
, d
, j
);
5091 isl_basic_map_free(hull
);
5092 return pw_multi_aff_from_map_base(map
);
5095 isl_basic_map_free(hull
);
5099 /* Given an affine expression
5101 * [A -> B] -> f(A,B)
5103 * construct an isl_multi_aff
5107 * such that dimension "d" in B' is set to "aff" and the remaining
5108 * dimensions are set equal to the corresponding dimensions in B.
5109 * "n_in" is the dimension of the space A.
5110 * "n_out" is the dimension of the space B.
5112 * If "is_set" is set, then the affine expression is of the form
5116 * and we construct an isl_multi_aff
5120 static __isl_give isl_multi_aff
*range_map(__isl_take isl_aff
*aff
, int d
,
5121 unsigned n_in
, unsigned n_out
, int is_set
)
5125 isl_space
*space
, *space2
;
5126 isl_local_space
*ls
;
5128 space
= isl_aff_get_domain_space(aff
);
5129 ls
= isl_local_space_from_space(isl_space_copy(space
));
5130 space2
= isl_space_copy(space
);
5132 space2
= isl_space_range(isl_space_unwrap(space2
));
5133 space
= isl_space_map_from_domain_and_range(space
, space2
);
5134 ma
= isl_multi_aff_alloc(space
);
5135 ma
= isl_multi_aff_set_aff(ma
, d
, aff
);
5137 for (i
= 0; i
< n_out
; ++i
) {
5140 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
5141 isl_dim_set
, n_in
+ i
);
5142 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
5145 isl_local_space_free(ls
);
5150 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
5151 * taking into account that the dimension at position "d" can be written as
5153 * x = m a + f(..) (1)
5155 * where m is equal to "gcd".
5156 * "i" is the index of the equality in "hull" that defines f(..).
5157 * In particular, the equality is of the form
5159 * f(..) - x + m g(existentials) = 0
5163 * -f(..) + x + m g(existentials) = 0
5165 * We basically plug (1) into "map", resulting in a map with "a"
5166 * in the range instead of "x". The corresponding isl_pw_multi_aff
5167 * defining "a" is then plugged back into (1) to obtain a definition for "x".
5169 * Specifically, given the input map
5173 * We first wrap it into a set
5177 * and define (1) on top of the corresponding space, resulting in "aff".
5178 * We use this to create an isl_multi_aff that maps the output position "d"
5179 * from "a" to "x", leaving all other (intput and output) dimensions unchanged.
5180 * We plug this into the wrapped map, unwrap the result and compute the
5181 * corresponding isl_pw_multi_aff.
5182 * The result is an expression
5190 * so that we can plug that into "aff", after extending the latter to
5196 * If "map" is actually a set, then there is no "A" space, meaning
5197 * that we do not need to perform any wrapping, and that the result
5198 * of the recursive call is of the form
5202 * which is plugged into a mapping of the form
5206 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_stride(
5207 __isl_take isl_map
*map
, __isl_take isl_basic_map
*hull
, int d
, int i
,
5212 isl_local_space
*ls
;
5215 isl_pw_multi_aff
*pma
, *id
;
5221 is_set
= isl_map_is_set(map
);
5225 n_in
= isl_basic_map_dim(hull
, isl_dim_in
);
5226 n_out
= isl_basic_map_dim(hull
, isl_dim_out
);
5227 if (n_in
< 0 || n_out
< 0)
5229 o_out
= isl_basic_map_offset(hull
, isl_dim_out
);
5234 set
= isl_map_wrap(map
);
5235 space
= isl_space_map_from_set(isl_set_get_space(set
));
5236 ma
= isl_multi_aff_identity(space
);
5237 ls
= isl_local_space_from_space(isl_set_get_space(set
));
5238 aff
= isl_aff_alloc(ls
);
5240 isl_int_set_si(aff
->v
->el
[0], 1);
5241 if (isl_int_is_one(hull
->eq
[i
][o_out
+ d
]))
5242 isl_seq_neg(aff
->v
->el
+ 1, hull
->eq
[i
],
5245 isl_seq_cpy(aff
->v
->el
+ 1, hull
->eq
[i
],
5247 isl_int_set(aff
->v
->el
[1 + o_out
+ d
], gcd
);
5249 ma
= isl_multi_aff_set_aff(ma
, n_in
+ d
, isl_aff_copy(aff
));
5250 set
= isl_set_preimage_multi_aff(set
, ma
);
5252 ma
= range_map(aff
, d
, n_in
, n_out
, is_set
);
5257 map
= isl_set_unwrap(set
);
5258 pma
= isl_pw_multi_aff_from_map(map
);
5261 space
= isl_pw_multi_aff_get_domain_space(pma
);
5262 space
= isl_space_map_from_set(space
);
5263 id
= isl_pw_multi_aff_identity(space
);
5264 pma
= isl_pw_multi_aff_range_product(id
, pma
);
5266 id
= isl_pw_multi_aff_from_multi_aff(ma
);
5267 pma
= isl_pw_multi_aff_pullback_pw_multi_aff(id
, pma
);
5269 isl_basic_map_free(hull
);
5273 isl_basic_map_free(hull
);
5277 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5278 * "hull" contains the equalities valid for "map".
5280 * Check if any of the output dimensions is "strided".
5281 * That is, we check if it can be written as
5285 * with m greater than 1, a some combination of existentially quantified
5286 * variables and f an expression in the parameters and input dimensions.
5287 * If so, we remove the stride in pw_multi_aff_from_map_stride.
5289 * Otherwise, we continue with pw_multi_aff_from_map_check_div for a further
5292 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_check_strides(
5293 __isl_take isl_map
*map
, __isl_take isl_basic_map
*hull
)
5302 n_div
= isl_basic_map_dim(hull
, isl_dim_div
);
5303 n_out
= isl_basic_map_dim(hull
, isl_dim_out
);
5304 if (n_div
< 0 || n_out
< 0)
5308 isl_basic_map_free(hull
);
5309 return pw_multi_aff_from_map_check_div(map
);
5314 o_div
= isl_basic_map_offset(hull
, isl_dim_div
);
5315 o_out
= isl_basic_map_offset(hull
, isl_dim_out
);
5317 for (i
= 0; i
< n_out
; ++i
) {
5318 for (j
= 0; j
< hull
->n_eq
; ++j
) {
5319 isl_int
*eq
= hull
->eq
[j
];
5320 isl_pw_multi_aff
*res
;
5322 if (!isl_int_is_one(eq
[o_out
+ i
]) &&
5323 !isl_int_is_negone(eq
[o_out
+ i
]))
5325 if (isl_seq_first_non_zero(eq
+ o_out
, i
) != -1)
5327 if (isl_seq_first_non_zero(eq
+ o_out
+ i
+ 1,
5328 n_out
- (i
+ 1)) != -1)
5330 isl_seq_gcd(eq
+ o_div
, n_div
, &gcd
);
5331 if (isl_int_is_zero(gcd
))
5333 if (isl_int_is_one(gcd
))
5336 res
= pw_multi_aff_from_map_stride(map
, hull
,
5344 isl_basic_map_free(hull
);
5345 return pw_multi_aff_from_map_check_div(map
);
5348 isl_basic_map_free(hull
);
5352 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5354 * As a special case, we first check if all output dimensions are uniquely
5355 * defined in terms of the parameters and input dimensions over the entire
5356 * domain. If so, we extract the desired isl_pw_multi_aff directly
5357 * from the affine hull of "map" and its domain.
5359 * Otherwise, continue with pw_multi_aff_from_map_check_strides for more
5362 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_map(__isl_take isl_map
*map
)
5366 isl_basic_map
*hull
;
5368 n
= isl_map_n_basic_map(map
);
5373 hull
= isl_map_unshifted_simple_hull(isl_map_copy(map
));
5374 hull
= isl_basic_map_plain_affine_hull(hull
);
5375 sv
= isl_basic_map_plain_is_single_valued(hull
);
5377 return plain_pw_multi_aff_from_map(isl_map_domain(map
),
5379 isl_basic_map_free(hull
);
5381 map
= isl_map_detect_equalities(map
);
5382 hull
= isl_map_unshifted_simple_hull(isl_map_copy(map
));
5383 sv
= isl_basic_map_plain_is_single_valued(hull
);
5385 return plain_pw_multi_aff_from_map(isl_map_domain(map
), hull
);
5387 return pw_multi_aff_from_map_check_strides(map
, hull
);
5388 isl_basic_map_free(hull
);
5394 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_set(__isl_take isl_set
*set
)
5396 return isl_pw_multi_aff_from_map(set
);
5399 /* Convert "map" into an isl_pw_multi_aff (if possible) and
5402 static isl_stat
pw_multi_aff_from_map(__isl_take isl_map
*map
, void *user
)
5404 isl_union_pw_multi_aff
**upma
= user
;
5405 isl_pw_multi_aff
*pma
;
5407 pma
= isl_pw_multi_aff_from_map(map
);
5408 *upma
= isl_union_pw_multi_aff_add_pw_multi_aff(*upma
, pma
);
5410 return *upma
? isl_stat_ok
: isl_stat_error
;
5413 /* Create an isl_union_pw_multi_aff with the given isl_aff on a universe
5416 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_aff(
5417 __isl_take isl_aff
*aff
)
5420 isl_pw_multi_aff
*pma
;
5422 ma
= isl_multi_aff_from_aff(aff
);
5423 pma
= isl_pw_multi_aff_from_multi_aff(ma
);
5424 return isl_union_pw_multi_aff_from_pw_multi_aff(pma
);
5427 /* Try and create an isl_union_pw_multi_aff that is equivalent
5428 * to the given isl_union_map.
5429 * The isl_union_map is required to be single-valued in each space.
5430 * Otherwise, an error is produced.
5432 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_union_map(
5433 __isl_take isl_union_map
*umap
)
5436 isl_union_pw_multi_aff
*upma
;
5438 space
= isl_union_map_get_space(umap
);
5439 upma
= isl_union_pw_multi_aff_empty(space
);
5440 if (isl_union_map_foreach_map(umap
, &pw_multi_aff_from_map
, &upma
) < 0)
5441 upma
= isl_union_pw_multi_aff_free(upma
);
5442 isl_union_map_free(umap
);
5447 /* Try and create an isl_union_pw_multi_aff that is equivalent
5448 * to the given isl_union_set.
5449 * The isl_union_set is required to be a singleton in each space.
5450 * Otherwise, an error is produced.
5452 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_union_set(
5453 __isl_take isl_union_set
*uset
)
5455 return isl_union_pw_multi_aff_from_union_map(uset
);
5458 /* Return the piecewise affine expression "set ? 1 : 0".
5460 __isl_give isl_pw_aff
*isl_set_indicator_function(__isl_take isl_set
*set
)
5463 isl_space
*space
= isl_set_get_space(set
);
5464 isl_local_space
*ls
= isl_local_space_from_space(space
);
5465 isl_aff
*zero
= isl_aff_zero_on_domain(isl_local_space_copy(ls
));
5466 isl_aff
*one
= isl_aff_zero_on_domain(ls
);
5468 one
= isl_aff_add_constant_si(one
, 1);
5469 pa
= isl_pw_aff_alloc(isl_set_copy(set
), one
);
5470 set
= isl_set_complement(set
);
5471 pa
= isl_pw_aff_add_disjoint(pa
, isl_pw_aff_alloc(set
, zero
));
5476 /* Plug in "subs" for dimension "type", "pos" of "aff".
5478 * Let i be the dimension to replace and let "subs" be of the form
5482 * and "aff" of the form
5488 * (a f + d g')/(m d)
5490 * where g' is the result of plugging in "subs" in each of the integer
5493 __isl_give isl_aff
*isl_aff_substitute(__isl_take isl_aff
*aff
,
5494 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
5500 aff
= isl_aff_cow(aff
);
5502 return isl_aff_free(aff
);
5504 ctx
= isl_aff_get_ctx(aff
);
5505 if (!isl_space_is_equal(aff
->ls
->dim
, subs
->ls
->dim
))
5506 isl_die(ctx
, isl_error_invalid
,
5507 "spaces don't match", return isl_aff_free(aff
));
5508 n_div
= isl_aff_domain_dim(subs
, isl_dim_div
);
5510 return isl_aff_free(aff
);
5512 isl_die(ctx
, isl_error_unsupported
,
5513 "cannot handle divs yet", return isl_aff_free(aff
));
5515 aff
->ls
= isl_local_space_substitute(aff
->ls
, type
, pos
, subs
);
5517 return isl_aff_free(aff
);
5519 aff
->v
= isl_vec_cow(aff
->v
);
5521 return isl_aff_free(aff
);
5523 pos
+= isl_local_space_offset(aff
->ls
, type
);
5526 isl_seq_substitute(aff
->v
->el
, pos
, subs
->v
->el
,
5527 aff
->v
->size
, subs
->v
->size
, v
);
5533 /* Plug in "subs" for dimension "type", "pos" in each of the affine
5534 * expressions in "maff".
5536 __isl_give isl_multi_aff
*isl_multi_aff_substitute(
5537 __isl_take isl_multi_aff
*maff
, enum isl_dim_type type
, unsigned pos
,
5538 __isl_keep isl_aff
*subs
)
5542 maff
= isl_multi_aff_cow(maff
);
5544 return isl_multi_aff_free(maff
);
5546 if (type
== isl_dim_in
)
5549 for (i
= 0; i
< maff
->n
; ++i
) {
5550 maff
->u
.p
[i
] = isl_aff_substitute(maff
->u
.p
[i
],
5553 return isl_multi_aff_free(maff
);
5559 /* Plug in "subs" for dimension "type", "pos" of "pma".
5561 * pma is of the form
5565 * while subs is of the form
5567 * v' = B_j(v) -> S_j
5569 * Each pair i,j such that C_ij = A_i \cap B_i is non-empty
5570 * has a contribution in the result, in particular
5572 * C_ij(S_j) -> M_i(S_j)
5574 * Note that plugging in S_j in C_ij may also result in an empty set
5575 * and this contribution should simply be discarded.
5577 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_substitute(
5578 __isl_take isl_pw_multi_aff
*pma
, enum isl_dim_type type
, unsigned pos
,
5579 __isl_keep isl_pw_aff
*subs
)
5582 isl_pw_multi_aff
*res
;
5585 return isl_pw_multi_aff_free(pma
);
5587 n
= pma
->n
* subs
->n
;
5588 res
= isl_pw_multi_aff_alloc_size(isl_space_copy(pma
->dim
), n
);
5590 for (i
= 0; i
< pma
->n
; ++i
) {
5591 for (j
= 0; j
< subs
->n
; ++j
) {
5593 isl_multi_aff
*res_ij
;
5596 common
= isl_set_intersect(
5597 isl_set_copy(pma
->p
[i
].set
),
5598 isl_set_copy(subs
->p
[j
].set
));
5599 common
= isl_set_substitute(common
,
5600 type
, pos
, subs
->p
[j
].aff
);
5601 empty
= isl_set_plain_is_empty(common
);
5602 if (empty
< 0 || empty
) {
5603 isl_set_free(common
);
5609 res_ij
= isl_multi_aff_substitute(
5610 isl_multi_aff_copy(pma
->p
[i
].maff
),
5611 type
, pos
, subs
->p
[j
].aff
);
5613 res
= isl_pw_multi_aff_add_piece(res
, common
, res_ij
);
5617 isl_pw_multi_aff_free(pma
);
5620 isl_pw_multi_aff_free(pma
);
5621 isl_pw_multi_aff_free(res
);
5625 /* Compute the preimage of a range of dimensions in the affine expression "src"
5626 * under "ma" and put the result in "dst". The number of dimensions in "src"
5627 * that precede the range is given by "n_before". The number of dimensions
5628 * in the range is given by the number of output dimensions of "ma".
5629 * The number of dimensions that follow the range is given by "n_after".
5630 * If "has_denom" is set (to one),
5631 * then "src" and "dst" have an extra initial denominator.
5632 * "n_div_ma" is the number of existentials in "ma"
5633 * "n_div_bset" is the number of existentials in "src"
5634 * The resulting "dst" (which is assumed to have been allocated by
5635 * the caller) contains coefficients for both sets of existentials,
5636 * first those in "ma" and then those in "src".
5637 * f, c1, c2 and g are temporary objects that have been initialized
5640 * Let src represent the expression
5642 * (a(p) + f_u u + b v + f_w w + c(divs))/d
5644 * and let ma represent the expressions
5646 * v_i = (r_i(p) + s_i(y) + t_i(divs'))/m_i
5648 * We start out with the following expression for dst:
5650 * (a(p) + f_u u + 0 y + f_w w + 0 divs' + c(divs) + f \sum_i b_i v_i)/d
5652 * with the multiplication factor f initially equal to 1
5653 * and f \sum_i b_i v_i kept separately.
5654 * For each x_i that we substitute, we multiply the numerator
5655 * (and denominator) of dst by c_1 = m_i and add the numerator
5656 * of the x_i expression multiplied by c_2 = f b_i,
5657 * after removing the common factors of c_1 and c_2.
5658 * The multiplication factor f also needs to be multiplied by c_1
5659 * for the next x_j, j > i.
5661 isl_stat
isl_seq_preimage(isl_int
*dst
, isl_int
*src
,
5662 __isl_keep isl_multi_aff
*ma
, int n_before
, int n_after
,
5663 int n_div_ma
, int n_div_bmap
,
5664 isl_int f
, isl_int c1
, isl_int c2
, isl_int g
, int has_denom
)
5667 isl_size n_param
, n_in
, n_out
;
5670 n_param
= isl_multi_aff_dim(ma
, isl_dim_param
);
5671 n_in
= isl_multi_aff_dim(ma
, isl_dim_in
);
5672 n_out
= isl_multi_aff_dim(ma
, isl_dim_out
);
5673 if (n_param
< 0 || n_in
< 0 || n_out
< 0)
5674 return isl_stat_error
;
5676 isl_seq_cpy(dst
, src
, has_denom
+ 1 + n_param
+ n_before
);
5677 o_dst
= o_src
= has_denom
+ 1 + n_param
+ n_before
;
5678 isl_seq_clr(dst
+ o_dst
, n_in
);
5681 isl_seq_cpy(dst
+ o_dst
, src
+ o_src
, n_after
);
5684 isl_seq_clr(dst
+ o_dst
, n_div_ma
);
5686 isl_seq_cpy(dst
+ o_dst
, src
+ o_src
, n_div_bmap
);
5688 isl_int_set_si(f
, 1);
5690 for (i
= 0; i
< n_out
; ++i
) {
5691 int offset
= has_denom
+ 1 + n_param
+ n_before
+ i
;
5693 if (isl_int_is_zero(src
[offset
]))
5695 isl_int_set(c1
, ma
->u
.p
[i
]->v
->el
[0]);
5696 isl_int_mul(c2
, f
, src
[offset
]);
5697 isl_int_gcd(g
, c1
, c2
);
5698 isl_int_divexact(c1
, c1
, g
);
5699 isl_int_divexact(c2
, c2
, g
);
5701 isl_int_mul(f
, f
, c1
);
5704 isl_seq_combine(dst
+ o_dst
, c1
, dst
+ o_dst
,
5705 c2
, ma
->u
.p
[i
]->v
->el
+ o_src
, 1 + n_param
);
5706 o_dst
+= 1 + n_param
;
5707 o_src
+= 1 + n_param
;
5708 isl_seq_scale(dst
+ o_dst
, dst
+ o_dst
, c1
, n_before
);
5710 isl_seq_combine(dst
+ o_dst
, c1
, dst
+ o_dst
,
5711 c2
, ma
->u
.p
[i
]->v
->el
+ o_src
, n_in
);
5714 isl_seq_scale(dst
+ o_dst
, dst
+ o_dst
, c1
, n_after
);
5716 isl_seq_combine(dst
+ o_dst
, c1
, dst
+ o_dst
,
5717 c2
, ma
->u
.p
[i
]->v
->el
+ o_src
, n_div_ma
);
5720 isl_seq_scale(dst
+ o_dst
, dst
+ o_dst
, c1
, n_div_bmap
);
5722 isl_int_mul(dst
[0], dst
[0], c1
);
5728 /* Compute the pullback of "aff" by the function represented by "ma".
5729 * In other words, plug in "ma" in "aff". The result is an affine expression
5730 * defined over the domain space of "ma".
5732 * If "aff" is represented by
5734 * (a(p) + b x + c(divs))/d
5736 * and ma is represented by
5738 * x = D(p) + F(y) + G(divs')
5740 * then the result is
5742 * (a(p) + b D(p) + b F(y) + b G(divs') + c(divs))/d
5744 * The divs in the local space of the input are similarly adjusted
5745 * through a call to isl_local_space_preimage_multi_aff.
5747 __isl_give isl_aff
*isl_aff_pullback_multi_aff(__isl_take isl_aff
*aff
,
5748 __isl_take isl_multi_aff
*ma
)
5750 isl_aff
*res
= NULL
;
5751 isl_local_space
*ls
;
5752 isl_size n_div_aff
, n_div_ma
;
5753 isl_int f
, c1
, c2
, g
;
5755 ma
= isl_multi_aff_align_divs(ma
);
5759 n_div_aff
= isl_aff_dim(aff
, isl_dim_div
);
5760 n_div_ma
= ma
->n
? isl_aff_dim(ma
->u
.p
[0], isl_dim_div
) : 0;
5761 if (n_div_aff
< 0 || n_div_ma
< 0)
5764 ls
= isl_aff_get_domain_local_space(aff
);
5765 ls
= isl_local_space_preimage_multi_aff(ls
, isl_multi_aff_copy(ma
));
5766 res
= isl_aff_alloc(ls
);
5775 if (isl_seq_preimage(res
->v
->el
, aff
->v
->el
, ma
, 0, 0,
5776 n_div_ma
, n_div_aff
, f
, c1
, c2
, g
, 1) < 0)
5777 res
= isl_aff_free(res
);
5785 isl_multi_aff_free(ma
);
5786 res
= isl_aff_normalize(res
);
5790 isl_multi_aff_free(ma
);
5795 /* Compute the pullback of "aff1" by the function represented by "aff2".
5796 * In other words, plug in "aff2" in "aff1". The result is an affine expression
5797 * defined over the domain space of "aff1".
5799 * The domain of "aff1" should match the range of "aff2", which means
5800 * that it should be single-dimensional.
5802 __isl_give isl_aff
*isl_aff_pullback_aff(__isl_take isl_aff
*aff1
,
5803 __isl_take isl_aff
*aff2
)
5807 ma
= isl_multi_aff_from_aff(aff2
);
5808 return isl_aff_pullback_multi_aff(aff1
, ma
);
5811 /* Compute the pullback of "ma1" by the function represented by "ma2".
5812 * In other words, plug in "ma2" in "ma1".
5814 __isl_give isl_multi_aff
*isl_multi_aff_pullback_multi_aff(
5815 __isl_take isl_multi_aff
*ma1
, __isl_take isl_multi_aff
*ma2
)
5818 isl_space
*space
= NULL
;
5820 isl_multi_aff_align_params_bin(&ma1
, &ma2
);
5821 ma2
= isl_multi_aff_align_divs(ma2
);
5822 ma1
= isl_multi_aff_cow(ma1
);
5826 space
= isl_space_join(isl_multi_aff_get_space(ma2
),
5827 isl_multi_aff_get_space(ma1
));
5829 for (i
= 0; i
< ma1
->n
; ++i
) {
5830 ma1
->u
.p
[i
] = isl_aff_pullback_multi_aff(ma1
->u
.p
[i
],
5831 isl_multi_aff_copy(ma2
));
5836 ma1
= isl_multi_aff_reset_space(ma1
, space
);
5837 isl_multi_aff_free(ma2
);
5840 isl_space_free(space
);
5841 isl_multi_aff_free(ma2
);
5842 isl_multi_aff_free(ma1
);
5846 /* Extend the local space of "dst" to include the divs
5847 * in the local space of "src".
5849 * If "src" does not have any divs or if the local spaces of "dst" and
5850 * "src" are the same, then no extension is required.
5852 __isl_give isl_aff
*isl_aff_align_divs(__isl_take isl_aff
*dst
,
5853 __isl_keep isl_aff
*src
)
5856 isl_size src_n_div
, dst_n_div
;
5863 return isl_aff_free(dst
);
5865 ctx
= isl_aff_get_ctx(src
);
5866 equal
= isl_local_space_has_equal_space(src
->ls
, dst
->ls
);
5868 return isl_aff_free(dst
);
5870 isl_die(ctx
, isl_error_invalid
,
5871 "spaces don't match", goto error
);
5873 src_n_div
= isl_aff_domain_dim(src
, isl_dim_div
);
5874 dst_n_div
= isl_aff_domain_dim(dst
, isl_dim_div
);
5877 equal
= isl_local_space_is_equal(src
->ls
, dst
->ls
);
5878 if (equal
< 0 || src_n_div
< 0 || dst_n_div
< 0)
5879 return isl_aff_free(dst
);
5883 exp1
= isl_alloc_array(ctx
, int, src_n_div
);
5884 exp2
= isl_alloc_array(ctx
, int, dst_n_div
);
5885 if (!exp1
|| (dst_n_div
&& !exp2
))
5888 div
= isl_merge_divs(src
->ls
->div
, dst
->ls
->div
, exp1
, exp2
);
5889 dst
= isl_aff_expand_divs(dst
, div
, exp2
);
5897 return isl_aff_free(dst
);
5900 /* Adjust the local spaces of the affine expressions in "maff"
5901 * such that they all have the save divs.
5903 __isl_give isl_multi_aff
*isl_multi_aff_align_divs(
5904 __isl_take isl_multi_aff
*maff
)
5912 maff
= isl_multi_aff_cow(maff
);
5916 for (i
= 1; i
< maff
->n
; ++i
)
5917 maff
->u
.p
[0] = isl_aff_align_divs(maff
->u
.p
[0], maff
->u
.p
[i
]);
5918 for (i
= 1; i
< maff
->n
; ++i
) {
5919 maff
->u
.p
[i
] = isl_aff_align_divs(maff
->u
.p
[i
], maff
->u
.p
[0]);
5921 return isl_multi_aff_free(maff
);
5927 __isl_give isl_aff
*isl_aff_lift(__isl_take isl_aff
*aff
)
5929 aff
= isl_aff_cow(aff
);
5933 aff
->ls
= isl_local_space_lift(aff
->ls
);
5935 return isl_aff_free(aff
);
5940 /* Lift "maff" to a space with extra dimensions such that the result
5941 * has no more existentially quantified variables.
5942 * If "ls" is not NULL, then *ls is assigned the local space that lies
5943 * at the basis of the lifting applied to "maff".
5945 __isl_give isl_multi_aff
*isl_multi_aff_lift(__isl_take isl_multi_aff
*maff
,
5946 __isl_give isl_local_space
**ls
)
5960 isl_space
*space
= isl_multi_aff_get_domain_space(maff
);
5961 *ls
= isl_local_space_from_space(space
);
5963 return isl_multi_aff_free(maff
);
5968 maff
= isl_multi_aff_cow(maff
);
5969 maff
= isl_multi_aff_align_divs(maff
);
5973 n_div
= isl_aff_dim(maff
->u
.p
[0], isl_dim_div
);
5975 return isl_multi_aff_free(maff
);
5976 space
= isl_multi_aff_get_space(maff
);
5977 space
= isl_space_lift(isl_space_domain(space
), n_div
);
5978 space
= isl_space_extend_domain_with_range(space
,
5979 isl_multi_aff_get_space(maff
));
5981 return isl_multi_aff_free(maff
);
5982 isl_space_free(maff
->space
);
5983 maff
->space
= space
;
5986 *ls
= isl_aff_get_domain_local_space(maff
->u
.p
[0]);
5988 return isl_multi_aff_free(maff
);
5991 for (i
= 0; i
< maff
->n
; ++i
) {
5992 maff
->u
.p
[i
] = isl_aff_lift(maff
->u
.p
[i
]);
6000 isl_local_space_free(*ls
);
6001 return isl_multi_aff_free(maff
);
6005 #define TYPE isl_pw_multi_aff
6007 #include "check_type_range_templ.c"
6009 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma".
6011 __isl_give isl_pw_aff
*isl_pw_multi_aff_get_pw_aff(
6012 __isl_keep isl_pw_multi_aff
*pma
, int pos
)
6019 if (isl_pw_multi_aff_check_range(pma
, isl_dim_out
, pos
, 1) < 0)
6022 n_out
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
6026 space
= isl_pw_multi_aff_get_space(pma
);
6027 space
= isl_space_drop_dims(space
, isl_dim_out
,
6028 pos
+ 1, n_out
- pos
- 1);
6029 space
= isl_space_drop_dims(space
, isl_dim_out
, 0, pos
);
6031 pa
= isl_pw_aff_alloc_size(space
, pma
->n
);
6032 for (i
= 0; i
< pma
->n
; ++i
) {
6034 aff
= isl_multi_aff_get_aff(pma
->p
[i
].maff
, pos
);
6035 pa
= isl_pw_aff_add_piece(pa
, isl_set_copy(pma
->p
[i
].set
), aff
);
6041 /* Return an isl_pw_multi_aff with the given "set" as domain and
6042 * an unnamed zero-dimensional range.
6044 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_domain(
6045 __isl_take isl_set
*set
)
6050 space
= isl_set_get_space(set
);
6051 space
= isl_space_from_domain(space
);
6052 ma
= isl_multi_aff_zero(space
);
6053 return isl_pw_multi_aff_alloc(set
, ma
);
6056 /* Add an isl_pw_multi_aff with the given "set" as domain and
6057 * an unnamed zero-dimensional range to *user.
6059 static isl_stat
add_pw_multi_aff_from_domain(__isl_take isl_set
*set
,
6062 isl_union_pw_multi_aff
**upma
= user
;
6063 isl_pw_multi_aff
*pma
;
6065 pma
= isl_pw_multi_aff_from_domain(set
);
6066 *upma
= isl_union_pw_multi_aff_add_pw_multi_aff(*upma
, pma
);
6071 /* Return an isl_union_pw_multi_aff with the given "uset" as domain and
6072 * an unnamed zero-dimensional range.
6074 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_domain(
6075 __isl_take isl_union_set
*uset
)
6078 isl_union_pw_multi_aff
*upma
;
6083 space
= isl_union_set_get_space(uset
);
6084 upma
= isl_union_pw_multi_aff_empty(space
);
6086 if (isl_union_set_foreach_set(uset
,
6087 &add_pw_multi_aff_from_domain
, &upma
) < 0)
6090 isl_union_set_free(uset
);
6093 isl_union_set_free(uset
);
6094 isl_union_pw_multi_aff_free(upma
);
6098 /* Local data for bin_entry and the callback "fn".
6100 struct isl_union_pw_multi_aff_bin_data
{
6101 isl_union_pw_multi_aff
*upma2
;
6102 isl_union_pw_multi_aff
*res
;
6103 isl_pw_multi_aff
*pma
;
6104 isl_stat (*fn
)(__isl_take isl_pw_multi_aff
*pma
, void *user
);
6107 /* Given an isl_pw_multi_aff from upma1, store it in data->pma
6108 * and call data->fn for each isl_pw_multi_aff in data->upma2.
6110 static isl_stat
bin_entry(__isl_take isl_pw_multi_aff
*pma
, void *user
)
6112 struct isl_union_pw_multi_aff_bin_data
*data
= user
;
6116 r
= isl_union_pw_multi_aff_foreach_pw_multi_aff(data
->upma2
,
6118 isl_pw_multi_aff_free(pma
);
6123 /* Call "fn" on each pair of isl_pw_multi_affs in "upma1" and "upma2".
6124 * The isl_pw_multi_aff from upma1 is stored in data->pma (where data is
6125 * passed as user field) and the isl_pw_multi_aff from upma2 is available
6126 * as *entry. The callback should adjust data->res if desired.
6128 static __isl_give isl_union_pw_multi_aff
*bin_op(
6129 __isl_take isl_union_pw_multi_aff
*upma1
,
6130 __isl_take isl_union_pw_multi_aff
*upma2
,
6131 isl_stat (*fn
)(__isl_take isl_pw_multi_aff
*pma
, void *user
))
6134 struct isl_union_pw_multi_aff_bin_data data
= { NULL
, NULL
, NULL
, fn
};
6136 space
= isl_union_pw_multi_aff_get_space(upma2
);
6137 upma1
= isl_union_pw_multi_aff_align_params(upma1
, space
);
6138 space
= isl_union_pw_multi_aff_get_space(upma1
);
6139 upma2
= isl_union_pw_multi_aff_align_params(upma2
, space
);
6141 if (!upma1
|| !upma2
)
6145 data
.res
= isl_union_pw_multi_aff_alloc_same_size(upma1
);
6146 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma1
,
6147 &bin_entry
, &data
) < 0)
6150 isl_union_pw_multi_aff_free(upma1
);
6151 isl_union_pw_multi_aff_free(upma2
);
6154 isl_union_pw_multi_aff_free(upma1
);
6155 isl_union_pw_multi_aff_free(upma2
);
6156 isl_union_pw_multi_aff_free(data
.res
);
6160 /* Given two isl_pw_multi_affs A -> B and C -> D,
6161 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
6163 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_range_product(
6164 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
6168 isl_pw_multi_aff_align_params_bin(&pma1
, &pma2
);
6169 space
= isl_space_range_product(isl_pw_multi_aff_get_space(pma1
),
6170 isl_pw_multi_aff_get_space(pma2
));
6171 return isl_pw_multi_aff_on_shared_domain_in(pma1
, pma2
, space
,
6172 &isl_multi_aff_range_product
);
6175 /* Given two isl_pw_multi_affs A -> B and C -> D,
6176 * construct an isl_pw_multi_aff (A * C) -> (B, D).
6178 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_flat_range_product(
6179 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
6183 isl_pw_multi_aff_align_params_bin(&pma1
, &pma2
);
6184 space
= isl_space_range_product(isl_pw_multi_aff_get_space(pma1
),
6185 isl_pw_multi_aff_get_space(pma2
));
6186 space
= isl_space_flatten_range(space
);
6187 return isl_pw_multi_aff_on_shared_domain_in(pma1
, pma2
, space
,
6188 &isl_multi_aff_flat_range_product
);
6191 /* If data->pma and "pma2" have the same domain space, then use "range_product"
6192 * to compute some form of range product and add the result to data->res.
6194 static isl_stat
gen_range_product_entry(__isl_take isl_pw_multi_aff
*pma2
,
6195 __isl_give isl_pw_multi_aff
*(*range_product
)(
6196 __isl_take isl_pw_multi_aff
*pma1
,
6197 __isl_take isl_pw_multi_aff
*pma2
),
6200 struct isl_union_pw_multi_aff_bin_data
*data
= user
;
6202 isl_space
*space1
, *space2
;
6204 space1
= isl_pw_multi_aff_peek_space(data
->pma
);
6205 space2
= isl_pw_multi_aff_peek_space(pma2
);
6206 match
= isl_space_tuple_is_equal(space1
, isl_dim_in
,
6207 space2
, isl_dim_in
);
6208 if (match
< 0 || !match
) {
6209 isl_pw_multi_aff_free(pma2
);
6210 return match
< 0 ? isl_stat_error
: isl_stat_ok
;
6213 pma2
= range_product(isl_pw_multi_aff_copy(data
->pma
), pma2
);
6215 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma2
);
6220 /* If data->pma and "pma2" have the same domain space, then compute
6221 * their flat range product and add the result to data->res.
6223 static isl_stat
flat_range_product_entry(__isl_take isl_pw_multi_aff
*pma2
,
6226 return gen_range_product_entry(pma2
,
6227 &isl_pw_multi_aff_flat_range_product
, user
);
6230 /* Given two isl_union_pw_multi_affs A -> B and C -> D,
6231 * construct an isl_union_pw_multi_aff (A * C) -> (B, D).
6233 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_flat_range_product(
6234 __isl_take isl_union_pw_multi_aff
*upma1
,
6235 __isl_take isl_union_pw_multi_aff
*upma2
)
6237 return bin_op(upma1
, upma2
, &flat_range_product_entry
);
6240 /* If data->pma and "pma2" have the same domain space, then compute
6241 * their range product and add the result to data->res.
6243 static isl_stat
range_product_entry(__isl_take isl_pw_multi_aff
*pma2
,
6246 return gen_range_product_entry(pma2
,
6247 &isl_pw_multi_aff_range_product
, user
);
6250 /* Given two isl_union_pw_multi_affs A -> B and C -> D,
6251 * construct an isl_union_pw_multi_aff (A * C) -> [B -> D].
6253 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_range_product(
6254 __isl_take isl_union_pw_multi_aff
*upma1
,
6255 __isl_take isl_union_pw_multi_aff
*upma2
)
6257 return bin_op(upma1
, upma2
, &range_product_entry
);
6260 /* Replace the affine expressions at position "pos" in "pma" by "pa".
6261 * The parameters are assumed to have been aligned.
6263 * The implementation essentially performs an isl_pw_*_on_shared_domain,
6264 * except that it works on two different isl_pw_* types.
6266 static __isl_give isl_pw_multi_aff
*pw_multi_aff_set_pw_aff(
6267 __isl_take isl_pw_multi_aff
*pma
, unsigned pos
,
6268 __isl_take isl_pw_aff
*pa
)
6271 isl_pw_multi_aff
*res
= NULL
;
6276 if (!isl_space_tuple_is_equal(pma
->dim
, isl_dim_in
,
6277 pa
->dim
, isl_dim_in
))
6278 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
6279 "domains don't match", goto error
);
6280 if (isl_pw_multi_aff_check_range(pma
, isl_dim_out
, pos
, 1) < 0)
6284 res
= isl_pw_multi_aff_alloc_size(isl_pw_multi_aff_get_space(pma
), n
);
6286 for (i
= 0; i
< pma
->n
; ++i
) {
6287 for (j
= 0; j
< pa
->n
; ++j
) {
6289 isl_multi_aff
*res_ij
;
6292 common
= isl_set_intersect(isl_set_copy(pma
->p
[i
].set
),
6293 isl_set_copy(pa
->p
[j
].set
));
6294 empty
= isl_set_plain_is_empty(common
);
6295 if (empty
< 0 || empty
) {
6296 isl_set_free(common
);
6302 res_ij
= isl_multi_aff_set_aff(
6303 isl_multi_aff_copy(pma
->p
[i
].maff
), pos
,
6304 isl_aff_copy(pa
->p
[j
].aff
));
6305 res_ij
= isl_multi_aff_gist(res_ij
,
6306 isl_set_copy(common
));
6308 res
= isl_pw_multi_aff_add_piece(res
, common
, res_ij
);
6312 isl_pw_multi_aff_free(pma
);
6313 isl_pw_aff_free(pa
);
6316 isl_pw_multi_aff_free(pma
);
6317 isl_pw_aff_free(pa
);
6318 return isl_pw_multi_aff_free(res
);
6321 /* Replace the affine expressions at position "pos" in "pma" by "pa".
6323 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_set_pw_aff(
6324 __isl_take isl_pw_multi_aff
*pma
, unsigned pos
,
6325 __isl_take isl_pw_aff
*pa
)
6327 isl_bool equal_params
;
6331 equal_params
= isl_space_has_equal_params(pma
->dim
, pa
->dim
);
6332 if (equal_params
< 0)
6335 return pw_multi_aff_set_pw_aff(pma
, pos
, pa
);
6336 if (isl_pw_multi_aff_check_named_params(pma
) < 0 ||
6337 isl_pw_aff_check_named_params(pa
) < 0)
6339 pma
= isl_pw_multi_aff_align_params(pma
, isl_pw_aff_get_space(pa
));
6340 pa
= isl_pw_aff_align_params(pa
, isl_pw_multi_aff_get_space(pma
));
6341 return pw_multi_aff_set_pw_aff(pma
, pos
, pa
);
6343 isl_pw_multi_aff_free(pma
);
6344 isl_pw_aff_free(pa
);
6348 /* Do the parameters of "pa" match those of "space"?
6350 isl_bool
isl_pw_aff_matching_params(__isl_keep isl_pw_aff
*pa
,
6351 __isl_keep isl_space
*space
)
6353 isl_space
*pa_space
;
6357 return isl_bool_error
;
6359 pa_space
= isl_pw_aff_get_space(pa
);
6361 match
= isl_space_has_equal_params(space
, pa_space
);
6363 isl_space_free(pa_space
);
6367 /* Check that the domain space of "pa" matches "space".
6369 isl_stat
isl_pw_aff_check_match_domain_space(__isl_keep isl_pw_aff
*pa
,
6370 __isl_keep isl_space
*space
)
6372 isl_space
*pa_space
;
6376 return isl_stat_error
;
6378 pa_space
= isl_pw_aff_get_space(pa
);
6380 match
= isl_space_has_equal_params(space
, pa_space
);
6384 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
6385 "parameters don't match", goto error
);
6386 match
= isl_space_tuple_is_equal(space
, isl_dim_in
,
6387 pa_space
, isl_dim_in
);
6391 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
6392 "domains don't match", goto error
);
6393 isl_space_free(pa_space
);
6396 isl_space_free(pa_space
);
6397 return isl_stat_error
;
6405 #include <isl_multi_explicit_domain.c>
6406 #include <isl_multi_pw_aff_explicit_domain.c>
6407 #include <isl_multi_templ.c>
6408 #include <isl_multi_add_constant_templ.c>
6409 #include <isl_multi_apply_set.c>
6410 #include <isl_multi_arith_templ.c>
6411 #include <isl_multi_bind_templ.c>
6412 #include <isl_multi_bind_domain_templ.c>
6413 #include <isl_multi_coalesce.c>
6414 #include <isl_multi_domain_templ.c>
6415 #include <isl_multi_dim_id_templ.c>
6416 #include <isl_multi_dims.c>
6417 #include <isl_multi_from_base_templ.c>
6418 #include <isl_multi_gist.c>
6419 #include <isl_multi_hash.c>
6420 #include <isl_multi_identity_templ.c>
6421 #include <isl_multi_align_set.c>
6422 #include <isl_multi_insert_domain_templ.c>
6423 #include <isl_multi_intersect.c>
6424 #include <isl_multi_min_max_templ.c>
6425 #include <isl_multi_move_dims_templ.c>
6426 #include <isl_multi_nan_templ.c>
6427 #include <isl_multi_param_templ.c>
6428 #include <isl_multi_product_templ.c>
6429 #include <isl_multi_splice_templ.c>
6430 #include <isl_multi_tuple_id_templ.c>
6431 #include <isl_multi_union_add_templ.c>
6432 #include <isl_multi_zero_templ.c>
6433 #include <isl_multi_unbind_params_templ.c>
6435 /* Are all elements of "mpa" piecewise constants?
6437 isl_bool
isl_multi_pw_aff_is_cst(__isl_keep isl_multi_pw_aff
*mpa
)
6439 return isl_multi_pw_aff_every(mpa
, &isl_pw_aff_is_cst
);
6442 /* Does "mpa" have a non-trivial explicit domain?
6444 * The explicit domain, if present, is trivial if it represents
6445 * an (obviously) universe set.
6447 isl_bool
isl_multi_pw_aff_has_non_trivial_domain(
6448 __isl_keep isl_multi_pw_aff
*mpa
)
6451 return isl_bool_error
;
6452 if (!isl_multi_pw_aff_has_explicit_domain(mpa
))
6453 return isl_bool_false
;
6454 return isl_bool_not(isl_set_plain_is_universe(mpa
->u
.dom
));
6460 #include "isl_opt_mpa_templ.c"
6462 /* Compute the minima of the set dimensions as a function of the
6463 * parameters, but independently of the other set dimensions.
6465 __isl_give isl_multi_pw_aff
*isl_set_min_multi_pw_aff(__isl_take isl_set
*set
)
6467 return set_opt_mpa(set
, &isl_set_dim_min
);
6470 /* Compute the maxima of the set dimensions as a function of the
6471 * parameters, but independently of the other set dimensions.
6473 __isl_give isl_multi_pw_aff
*isl_set_max_multi_pw_aff(__isl_take isl_set
*set
)
6475 return set_opt_mpa(set
, &isl_set_dim_max
);
6481 #include "isl_opt_mpa_templ.c"
6483 /* Compute the minima of the output dimensions as a function of the
6484 * parameters and input dimensions, but independently of
6485 * the other output dimensions.
6487 __isl_give isl_multi_pw_aff
*isl_map_min_multi_pw_aff(__isl_take isl_map
*map
)
6489 return map_opt_mpa(map
, &isl_map_dim_min
);
6492 /* Compute the maxima of the output dimensions as a function of the
6493 * parameters and input dimensions, but independently of
6494 * the other output dimensions.
6496 __isl_give isl_multi_pw_aff
*isl_map_max_multi_pw_aff(__isl_take isl_map
*map
)
6498 return map_opt_mpa(map
, &isl_map_dim_max
);
6501 /* Scale the elements of "pma" by the corresponding elements of "mv".
6503 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_scale_multi_val(
6504 __isl_take isl_pw_multi_aff
*pma
, __isl_take isl_multi_val
*mv
)
6507 isl_bool equal_params
;
6509 pma
= isl_pw_multi_aff_cow(pma
);
6512 if (!isl_space_tuple_is_equal(pma
->dim
, isl_dim_out
,
6513 mv
->space
, isl_dim_set
))
6514 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
6515 "spaces don't match", goto error
);
6516 equal_params
= isl_space_has_equal_params(pma
->dim
, mv
->space
);
6517 if (equal_params
< 0)
6519 if (!equal_params
) {
6520 pma
= isl_pw_multi_aff_align_params(pma
,
6521 isl_multi_val_get_space(mv
));
6522 mv
= isl_multi_val_align_params(mv
,
6523 isl_pw_multi_aff_get_space(pma
));
6528 for (i
= 0; i
< pma
->n
; ++i
) {
6529 pma
->p
[i
].maff
= isl_multi_aff_scale_multi_val(pma
->p
[i
].maff
,
6530 isl_multi_val_copy(mv
));
6531 if (!pma
->p
[i
].maff
)
6535 isl_multi_val_free(mv
);
6538 isl_multi_val_free(mv
);
6539 isl_pw_multi_aff_free(pma
);
6543 /* This function is called for each entry of an isl_union_pw_multi_aff.
6544 * If the space of the entry matches that of data->mv,
6545 * then apply isl_pw_multi_aff_scale_multi_val and return the result.
6546 * Otherwise, return an empty isl_pw_multi_aff.
6548 static __isl_give isl_pw_multi_aff
*union_pw_multi_aff_scale_multi_val_entry(
6549 __isl_take isl_pw_multi_aff
*pma
, void *user
)
6551 isl_multi_val
*mv
= user
;
6555 if (!isl_space_tuple_is_equal(pma
->dim
, isl_dim_out
,
6556 mv
->space
, isl_dim_set
)) {
6557 isl_space
*space
= isl_pw_multi_aff_get_space(pma
);
6558 isl_pw_multi_aff_free(pma
);
6559 return isl_pw_multi_aff_empty(space
);
6562 return isl_pw_multi_aff_scale_multi_val(pma
, isl_multi_val_copy(mv
));
6565 /* Scale the elements of "upma" by the corresponding elements of "mv",
6566 * for those entries that match the space of "mv".
6568 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_scale_multi_val(
6569 __isl_take isl_union_pw_multi_aff
*upma
, __isl_take isl_multi_val
*mv
)
6571 struct isl_union_pw_multi_aff_transform_control control
= {
6572 .fn
= &union_pw_multi_aff_scale_multi_val_entry
,
6576 upma
= isl_union_pw_multi_aff_align_params(upma
,
6577 isl_multi_val_get_space(mv
));
6578 mv
= isl_multi_val_align_params(mv
,
6579 isl_union_pw_multi_aff_get_space(upma
));
6583 return isl_union_pw_multi_aff_transform(upma
, &control
);
6585 isl_multi_val_free(mv
);
6588 isl_multi_val_free(mv
);
6589 isl_union_pw_multi_aff_free(upma
);
6593 /* Construct and return a piecewise multi affine expression
6594 * in the given space with value zero in each of the output dimensions and
6595 * a universe domain.
6597 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_zero(__isl_take isl_space
*space
)
6599 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_zero(space
));
6602 /* Construct and return a piecewise multi affine expression
6603 * that is equal to the given piecewise affine expression.
6605 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_pw_aff(
6606 __isl_take isl_pw_aff
*pa
)
6610 isl_pw_multi_aff
*pma
;
6615 space
= isl_pw_aff_get_space(pa
);
6616 pma
= isl_pw_multi_aff_alloc_size(space
, pa
->n
);
6618 for (i
= 0; i
< pa
->n
; ++i
) {
6622 set
= isl_set_copy(pa
->p
[i
].set
);
6623 ma
= isl_multi_aff_from_aff(isl_aff_copy(pa
->p
[i
].aff
));
6624 pma
= isl_pw_multi_aff_add_piece(pma
, set
, ma
);
6627 isl_pw_aff_free(pa
);
6631 /* Construct and return a piecewise multi affine expression
6632 * that is equal to the given multi piecewise affine expression
6633 * on the shared domain of the piecewise affine expressions,
6634 * in the special case of a 0D multi piecewise affine expression.
6636 * Create a piecewise multi affine expression with the explicit domain of
6637 * the 0D multi piecewise affine expression as domain.
6639 static __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_multi_pw_aff_0D(
6640 __isl_take isl_multi_pw_aff
*mpa
)
6646 space
= isl_multi_pw_aff_get_space(mpa
);
6647 dom
= isl_multi_pw_aff_get_explicit_domain(mpa
);
6648 isl_multi_pw_aff_free(mpa
);
6650 ma
= isl_multi_aff_zero(space
);
6651 return isl_pw_multi_aff_alloc(dom
, ma
);
6654 /* Construct and return a piecewise multi affine expression
6655 * that is equal to the given multi piecewise affine expression
6656 * on the shared domain of the piecewise affine expressions.
6658 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_multi_pw_aff(
6659 __isl_take isl_multi_pw_aff
*mpa
)
6664 isl_pw_multi_aff
*pma
;
6670 return isl_pw_multi_aff_from_multi_pw_aff_0D(mpa
);
6672 space
= isl_multi_pw_aff_get_space(mpa
);
6673 pa
= isl_multi_pw_aff_get_pw_aff(mpa
, 0);
6674 pma
= isl_pw_multi_aff_from_pw_aff(pa
);
6676 for (i
= 1; i
< mpa
->n
; ++i
) {
6677 isl_pw_multi_aff
*pma_i
;
6679 pa
= isl_multi_pw_aff_get_pw_aff(mpa
, i
);
6680 pma_i
= isl_pw_multi_aff_from_pw_aff(pa
);
6681 pma
= isl_pw_multi_aff_range_product(pma
, pma_i
);
6684 pma
= isl_pw_multi_aff_reset_space(pma
, space
);
6686 isl_multi_pw_aff_free(mpa
);
6690 /* Convenience function that constructs an isl_multi_pw_aff
6691 * directly from an isl_aff.
6693 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_from_aff(__isl_take isl_aff
*aff
)
6695 return isl_multi_pw_aff_from_pw_aff(isl_pw_aff_from_aff(aff
));
6698 /* Construct and return a multi piecewise affine expression
6699 * that is equal to the given multi affine expression.
6701 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_from_multi_aff(
6702 __isl_take isl_multi_aff
*ma
)
6706 isl_multi_pw_aff
*mpa
;
6708 n
= isl_multi_aff_dim(ma
, isl_dim_out
);
6710 ma
= isl_multi_aff_free(ma
);
6714 mpa
= isl_multi_pw_aff_alloc(isl_multi_aff_get_space(ma
));
6716 for (i
= 0; i
< n
; ++i
) {
6719 pa
= isl_pw_aff_from_aff(isl_multi_aff_get_aff(ma
, i
));
6720 mpa
= isl_multi_pw_aff_set_pw_aff(mpa
, i
, pa
);
6723 isl_multi_aff_free(ma
);
6727 /* Construct and return a multi piecewise affine expression
6728 * that is equal to the given piecewise multi affine expression.
6730 * If the resulting multi piecewise affine expression has
6731 * an explicit domain, then assign it the domain of the input.
6732 * In other cases, the domain is stored in the individual elements.
6734 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_from_pw_multi_aff(
6735 __isl_take isl_pw_multi_aff
*pma
)
6740 isl_multi_pw_aff
*mpa
;
6742 n
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
6744 pma
= isl_pw_multi_aff_free(pma
);
6745 space
= isl_pw_multi_aff_get_space(pma
);
6746 mpa
= isl_multi_pw_aff_alloc(space
);
6748 for (i
= 0; i
< n
; ++i
) {
6751 pa
= isl_pw_multi_aff_get_pw_aff(pma
, i
);
6752 mpa
= isl_multi_pw_aff_set_pw_aff(mpa
, i
, pa
);
6754 if (isl_multi_pw_aff_has_explicit_domain(mpa
)) {
6757 dom
= isl_pw_multi_aff_domain(isl_pw_multi_aff_copy(pma
));
6758 mpa
= isl_multi_pw_aff_intersect_domain(mpa
, dom
);
6761 isl_pw_multi_aff_free(pma
);
6765 /* Do "pa1" and "pa2" represent the same function?
6767 * We first check if they are obviously equal.
6768 * If not, we convert them to maps and check if those are equal.
6770 * If "pa1" or "pa2" contain any NaNs, then they are considered
6771 * not to be the same. A NaN is not equal to anything, not even
6774 isl_bool
isl_pw_aff_is_equal(__isl_keep isl_pw_aff
*pa1
,
6775 __isl_keep isl_pw_aff
*pa2
)
6779 isl_map
*map1
, *map2
;
6782 return isl_bool_error
;
6784 equal
= isl_pw_aff_plain_is_equal(pa1
, pa2
);
6785 if (equal
< 0 || equal
)
6787 has_nan
= either_involves_nan(pa1
, pa2
);
6789 return isl_bool_error
;
6791 return isl_bool_false
;
6793 map1
= isl_map_from_pw_aff_internal(isl_pw_aff_copy(pa1
));
6794 map2
= isl_map_from_pw_aff_internal(isl_pw_aff_copy(pa2
));
6795 equal
= isl_map_is_equal(map1
, map2
);
6802 /* Do "mpa1" and "mpa2" represent the same function?
6804 * Note that we cannot convert the entire isl_multi_pw_aff
6805 * to a map because the domains of the piecewise affine expressions
6806 * may not be the same.
6808 isl_bool
isl_multi_pw_aff_is_equal(__isl_keep isl_multi_pw_aff
*mpa1
,
6809 __isl_keep isl_multi_pw_aff
*mpa2
)
6812 isl_bool equal
, equal_params
;
6815 return isl_bool_error
;
6817 equal_params
= isl_space_has_equal_params(mpa1
->space
, mpa2
->space
);
6818 if (equal_params
< 0)
6819 return isl_bool_error
;
6820 if (!equal_params
) {
6821 if (!isl_space_has_named_params(mpa1
->space
))
6822 return isl_bool_false
;
6823 if (!isl_space_has_named_params(mpa2
->space
))
6824 return isl_bool_false
;
6825 mpa1
= isl_multi_pw_aff_copy(mpa1
);
6826 mpa2
= isl_multi_pw_aff_copy(mpa2
);
6827 mpa1
= isl_multi_pw_aff_align_params(mpa1
,
6828 isl_multi_pw_aff_get_space(mpa2
));
6829 mpa2
= isl_multi_pw_aff_align_params(mpa2
,
6830 isl_multi_pw_aff_get_space(mpa1
));
6831 equal
= isl_multi_pw_aff_is_equal(mpa1
, mpa2
);
6832 isl_multi_pw_aff_free(mpa1
);
6833 isl_multi_pw_aff_free(mpa2
);
6837 equal
= isl_space_is_equal(mpa1
->space
, mpa2
->space
);
6838 if (equal
< 0 || !equal
)
6841 for (i
= 0; i
< mpa1
->n
; ++i
) {
6842 equal
= isl_pw_aff_is_equal(mpa1
->u
.p
[i
], mpa2
->u
.p
[i
]);
6843 if (equal
< 0 || !equal
)
6847 return isl_bool_true
;
6850 /* Do "pma1" and "pma2" represent the same function?
6852 * First check if they are obviously equal.
6853 * If not, then convert them to maps and check if those are equal.
6855 * If "pa1" or "pa2" contain any NaNs, then they are considered
6856 * not to be the same. A NaN is not equal to anything, not even
6859 isl_bool
isl_pw_multi_aff_is_equal(__isl_keep isl_pw_multi_aff
*pma1
,
6860 __isl_keep isl_pw_multi_aff
*pma2
)
6864 isl_map
*map1
, *map2
;
6867 return isl_bool_error
;
6869 equal
= isl_pw_multi_aff_plain_is_equal(pma1
, pma2
);
6870 if (equal
< 0 || equal
)
6872 has_nan
= isl_pw_multi_aff_involves_nan(pma1
);
6873 if (has_nan
>= 0 && !has_nan
)
6874 has_nan
= isl_pw_multi_aff_involves_nan(pma2
);
6875 if (has_nan
< 0 || has_nan
)
6876 return isl_bool_not(has_nan
);
6878 map1
= isl_map_from_pw_multi_aff_internal(isl_pw_multi_aff_copy(pma1
));
6879 map2
= isl_map_from_pw_multi_aff_internal(isl_pw_multi_aff_copy(pma2
));
6880 equal
= isl_map_is_equal(map1
, map2
);
6887 /* Compute the pullback of "mpa" by the function represented by "ma".
6888 * In other words, plug in "ma" in "mpa".
6890 * The parameters of "mpa" and "ma" are assumed to have been aligned.
6892 * If "mpa" has an explicit domain, then it is this domain
6893 * that needs to undergo a pullback, i.e., a preimage.
6895 static __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_pullback_multi_aff_aligned(
6896 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_multi_aff
*ma
)
6899 isl_space
*space
= NULL
;
6901 mpa
= isl_multi_pw_aff_cow(mpa
);
6905 space
= isl_space_join(isl_multi_aff_get_space(ma
),
6906 isl_multi_pw_aff_get_space(mpa
));
6910 for (i
= 0; i
< mpa
->n
; ++i
) {
6911 mpa
->u
.p
[i
] = isl_pw_aff_pullback_multi_aff(mpa
->u
.p
[i
],
6912 isl_multi_aff_copy(ma
));
6916 if (isl_multi_pw_aff_has_explicit_domain(mpa
)) {
6917 mpa
->u
.dom
= isl_set_preimage_multi_aff(mpa
->u
.dom
,
6918 isl_multi_aff_copy(ma
));
6923 isl_multi_aff_free(ma
);
6924 isl_space_free(mpa
->space
);
6928 isl_space_free(space
);
6929 isl_multi_pw_aff_free(mpa
);
6930 isl_multi_aff_free(ma
);
6934 /* Compute the pullback of "mpa" by the function represented by "ma".
6935 * In other words, plug in "ma" in "mpa".
6937 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_pullback_multi_aff(
6938 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_multi_aff
*ma
)
6940 isl_bool equal_params
;
6944 equal_params
= isl_space_has_equal_params(mpa
->space
, ma
->space
);
6945 if (equal_params
< 0)
6948 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa
, ma
);
6949 mpa
= isl_multi_pw_aff_align_params(mpa
, isl_multi_aff_get_space(ma
));
6950 ma
= isl_multi_aff_align_params(ma
, isl_multi_pw_aff_get_space(mpa
));
6951 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa
, ma
);
6953 isl_multi_pw_aff_free(mpa
);
6954 isl_multi_aff_free(ma
);
6958 /* Compute the pullback of "mpa" by the function represented by "pma".
6959 * In other words, plug in "pma" in "mpa".
6961 * The parameters of "mpa" and "mpa" are assumed to have been aligned.
6963 * If "mpa" has an explicit domain, then it is this domain
6964 * that needs to undergo a pullback, i.e., a preimage.
6966 static __isl_give isl_multi_pw_aff
*
6967 isl_multi_pw_aff_pullback_pw_multi_aff_aligned(
6968 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_pw_multi_aff
*pma
)
6971 isl_space
*space
= NULL
;
6973 mpa
= isl_multi_pw_aff_cow(mpa
);
6977 space
= isl_space_join(isl_pw_multi_aff_get_space(pma
),
6978 isl_multi_pw_aff_get_space(mpa
));
6980 for (i
= 0; i
< mpa
->n
; ++i
) {
6981 mpa
->u
.p
[i
] = isl_pw_aff_pullback_pw_multi_aff_aligned(
6982 mpa
->u
.p
[i
], isl_pw_multi_aff_copy(pma
));
6986 if (isl_multi_pw_aff_has_explicit_domain(mpa
)) {
6987 mpa
->u
.dom
= isl_set_preimage_pw_multi_aff(mpa
->u
.dom
,
6988 isl_pw_multi_aff_copy(pma
));
6993 isl_pw_multi_aff_free(pma
);
6994 isl_space_free(mpa
->space
);
6998 isl_space_free(space
);
6999 isl_multi_pw_aff_free(mpa
);
7000 isl_pw_multi_aff_free(pma
);
7004 /* Compute the pullback of "mpa" by the function represented by "pma".
7005 * In other words, plug in "pma" in "mpa".
7007 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_pullback_pw_multi_aff(
7008 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_pw_multi_aff
*pma
)
7010 isl_bool equal_params
;
7014 equal_params
= isl_space_has_equal_params(mpa
->space
, pma
->dim
);
7015 if (equal_params
< 0)
7018 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa
, pma
);
7019 mpa
= isl_multi_pw_aff_align_params(mpa
,
7020 isl_pw_multi_aff_get_space(pma
));
7021 pma
= isl_pw_multi_aff_align_params(pma
,
7022 isl_multi_pw_aff_get_space(mpa
));
7023 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa
, pma
);
7025 isl_multi_pw_aff_free(mpa
);
7026 isl_pw_multi_aff_free(pma
);
7030 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
7031 * with the domain of "aff". The domain of the result is the same
7033 * "mpa" and "aff" are assumed to have been aligned.
7035 * We first extract the parametric constant from "aff", defined
7036 * over the correct domain.
7037 * Then we add the appropriate combinations of the members of "mpa".
7038 * Finally, we add the integer divisions through recursive calls.
7040 static __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_aff_aligned(
7041 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_aff
*aff
)
7044 isl_size n_in
, n_div
, n_mpa_in
;
7050 n_in
= isl_aff_dim(aff
, isl_dim_in
);
7051 n_div
= isl_aff_dim(aff
, isl_dim_div
);
7052 n_mpa_in
= isl_multi_pw_aff_dim(mpa
, isl_dim_in
);
7053 if (n_in
< 0 || n_div
< 0 || n_mpa_in
< 0)
7056 space
= isl_space_domain(isl_multi_pw_aff_get_space(mpa
));
7057 tmp
= isl_aff_copy(aff
);
7058 tmp
= isl_aff_drop_dims(tmp
, isl_dim_div
, 0, n_div
);
7059 tmp
= isl_aff_drop_dims(tmp
, isl_dim_in
, 0, n_in
);
7060 tmp
= isl_aff_add_dims(tmp
, isl_dim_in
, n_mpa_in
);
7061 tmp
= isl_aff_reset_domain_space(tmp
, space
);
7062 pa
= isl_pw_aff_from_aff(tmp
);
7064 for (i
= 0; i
< n_in
; ++i
) {
7067 if (!isl_aff_involves_dims(aff
, isl_dim_in
, i
, 1))
7069 v
= isl_aff_get_coefficient_val(aff
, isl_dim_in
, i
);
7070 pa_i
= isl_multi_pw_aff_get_pw_aff(mpa
, i
);
7071 pa_i
= isl_pw_aff_scale_val(pa_i
, v
);
7072 pa
= isl_pw_aff_add(pa
, pa_i
);
7075 for (i
= 0; i
< n_div
; ++i
) {
7079 if (!isl_aff_involves_dims(aff
, isl_dim_div
, i
, 1))
7081 div
= isl_aff_get_div(aff
, i
);
7082 pa_i
= isl_multi_pw_aff_apply_aff_aligned(
7083 isl_multi_pw_aff_copy(mpa
), div
);
7084 pa_i
= isl_pw_aff_floor(pa_i
);
7085 v
= isl_aff_get_coefficient_val(aff
, isl_dim_div
, i
);
7086 pa_i
= isl_pw_aff_scale_val(pa_i
, v
);
7087 pa
= isl_pw_aff_add(pa
, pa_i
);
7090 isl_multi_pw_aff_free(mpa
);
7095 isl_multi_pw_aff_free(mpa
);
7100 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
7101 * with the domain of "aff". The domain of the result is the same
7104 __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_aff(
7105 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_aff
*aff
)
7107 isl_bool equal_params
;
7111 equal_params
= isl_space_has_equal_params(aff
->ls
->dim
, mpa
->space
);
7112 if (equal_params
< 0)
7115 return isl_multi_pw_aff_apply_aff_aligned(mpa
, aff
);
7117 aff
= isl_aff_align_params(aff
, isl_multi_pw_aff_get_space(mpa
));
7118 mpa
= isl_multi_pw_aff_align_params(mpa
, isl_aff_get_space(aff
));
7120 return isl_multi_pw_aff_apply_aff_aligned(mpa
, aff
);
7123 isl_multi_pw_aff_free(mpa
);
7127 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
7128 * with the domain of "pa". The domain of the result is the same
7130 * "mpa" and "pa" are assumed to have been aligned.
7132 * We consider each piece in turn. Note that the domains of the
7133 * pieces are assumed to be disjoint and they remain disjoint
7134 * after taking the preimage (over the same function).
7136 static __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_pw_aff_aligned(
7137 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_pw_aff
*pa
)
7146 space
= isl_space_join(isl_multi_pw_aff_get_space(mpa
),
7147 isl_pw_aff_get_space(pa
));
7148 res
= isl_pw_aff_empty(space
);
7150 for (i
= 0; i
< pa
->n
; ++i
) {
7154 pa_i
= isl_multi_pw_aff_apply_aff_aligned(
7155 isl_multi_pw_aff_copy(mpa
),
7156 isl_aff_copy(pa
->p
[i
].aff
));
7157 domain
= isl_set_copy(pa
->p
[i
].set
);
7158 domain
= isl_set_preimage_multi_pw_aff(domain
,
7159 isl_multi_pw_aff_copy(mpa
));
7160 pa_i
= isl_pw_aff_intersect_domain(pa_i
, domain
);
7161 res
= isl_pw_aff_add_disjoint(res
, pa_i
);
7164 isl_pw_aff_free(pa
);
7165 isl_multi_pw_aff_free(mpa
);
7168 isl_pw_aff_free(pa
);
7169 isl_multi_pw_aff_free(mpa
);
7173 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
7174 * with the domain of "pa". The domain of the result is the same
7177 __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_pw_aff(
7178 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_pw_aff
*pa
)
7180 isl_bool equal_params
;
7184 equal_params
= isl_space_has_equal_params(pa
->dim
, mpa
->space
);
7185 if (equal_params
< 0)
7188 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa
, pa
);
7190 pa
= isl_pw_aff_align_params(pa
, isl_multi_pw_aff_get_space(mpa
));
7191 mpa
= isl_multi_pw_aff_align_params(mpa
, isl_pw_aff_get_space(pa
));
7193 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa
, pa
);
7195 isl_pw_aff_free(pa
);
7196 isl_multi_pw_aff_free(mpa
);
7200 /* Compute the pullback of "pa" by the function represented by "mpa".
7201 * In other words, plug in "mpa" in "pa".
7202 * "pa" and "mpa" are assumed to have been aligned.
7204 * The pullback is computed by applying "pa" to "mpa".
7206 static __isl_give isl_pw_aff
*isl_pw_aff_pullback_multi_pw_aff_aligned(
7207 __isl_take isl_pw_aff
*pa
, __isl_take isl_multi_pw_aff
*mpa
)
7209 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa
, pa
);
7212 /* Compute the pullback of "pa" by the function represented by "mpa".
7213 * In other words, plug in "mpa" in "pa".
7215 * The pullback is computed by applying "pa" to "mpa".
7217 __isl_give isl_pw_aff
*isl_pw_aff_pullback_multi_pw_aff(
7218 __isl_take isl_pw_aff
*pa
, __isl_take isl_multi_pw_aff
*mpa
)
7220 return isl_multi_pw_aff_apply_pw_aff(mpa
, pa
);
7223 /* Compute the pullback of "mpa1" by the function represented by "mpa2".
7224 * In other words, plug in "mpa2" in "mpa1".
7226 * We pullback each member of "mpa1" in turn.
7228 * If "mpa1" has an explicit domain, then it is this domain
7229 * that needs to undergo a pullback instead, i.e., a preimage.
7231 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_pullback_multi_pw_aff(
7232 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
)
7235 isl_space
*space
= NULL
;
7237 isl_multi_pw_aff_align_params_bin(&mpa1
, &mpa2
);
7238 mpa1
= isl_multi_pw_aff_cow(mpa1
);
7242 space
= isl_space_join(isl_multi_pw_aff_get_space(mpa2
),
7243 isl_multi_pw_aff_get_space(mpa1
));
7245 for (i
= 0; i
< mpa1
->n
; ++i
) {
7246 mpa1
->u
.p
[i
] = isl_pw_aff_pullback_multi_pw_aff_aligned(
7247 mpa1
->u
.p
[i
], isl_multi_pw_aff_copy(mpa2
));
7252 if (isl_multi_pw_aff_has_explicit_domain(mpa1
)) {
7253 mpa1
->u
.dom
= isl_set_preimage_multi_pw_aff(mpa1
->u
.dom
,
7254 isl_multi_pw_aff_copy(mpa2
));
7258 mpa1
= isl_multi_pw_aff_reset_space(mpa1
, space
);
7260 isl_multi_pw_aff_free(mpa2
);
7263 isl_space_free(space
);
7264 isl_multi_pw_aff_free(mpa1
);
7265 isl_multi_pw_aff_free(mpa2
);
7269 /* Align the parameters of "mpa1" and "mpa2", check that the ranges
7270 * of "mpa1" and "mpa2" live in the same space, construct map space
7271 * between the domain spaces of "mpa1" and "mpa2" and call "order"
7272 * with this map space as extract argument.
7274 static __isl_give isl_map
*isl_multi_pw_aff_order_map(
7275 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
,
7276 __isl_give isl_map
*(*order
)(__isl_keep isl_multi_pw_aff
*mpa1
,
7277 __isl_keep isl_multi_pw_aff
*mpa2
, __isl_take isl_space
*space
))
7280 isl_space
*space1
, *space2
;
7283 mpa1
= isl_multi_pw_aff_align_params(mpa1
,
7284 isl_multi_pw_aff_get_space(mpa2
));
7285 mpa2
= isl_multi_pw_aff_align_params(mpa2
,
7286 isl_multi_pw_aff_get_space(mpa1
));
7289 match
= isl_space_tuple_is_equal(mpa1
->space
, isl_dim_out
,
7290 mpa2
->space
, isl_dim_out
);
7294 isl_die(isl_multi_pw_aff_get_ctx(mpa1
), isl_error_invalid
,
7295 "range spaces don't match", goto error
);
7296 space1
= isl_space_domain(isl_multi_pw_aff_get_space(mpa1
));
7297 space2
= isl_space_domain(isl_multi_pw_aff_get_space(mpa2
));
7298 space1
= isl_space_map_from_domain_and_range(space1
, space2
);
7300 res
= order(mpa1
, mpa2
, space1
);
7301 isl_multi_pw_aff_free(mpa1
);
7302 isl_multi_pw_aff_free(mpa2
);
7305 isl_multi_pw_aff_free(mpa1
);
7306 isl_multi_pw_aff_free(mpa2
);
7310 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7311 * where the function values are equal. "space" is the space of the result.
7312 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7314 * "mpa1" and "mpa2" are equal when each of the pairs of elements
7315 * in the sequences are equal.
7317 static __isl_give isl_map
*isl_multi_pw_aff_eq_map_on_space(
7318 __isl_keep isl_multi_pw_aff
*mpa1
, __isl_keep isl_multi_pw_aff
*mpa2
,
7319 __isl_take isl_space
*space
)
7325 n
= isl_multi_pw_aff_dim(mpa1
, isl_dim_out
);
7327 space
= isl_space_free(space
);
7328 res
= isl_map_universe(space
);
7330 for (i
= 0; i
< n
; ++i
) {
7331 isl_pw_aff
*pa1
, *pa2
;
7334 pa1
= isl_multi_pw_aff_get_pw_aff(mpa1
, i
);
7335 pa2
= isl_multi_pw_aff_get_pw_aff(mpa2
, i
);
7336 map
= isl_pw_aff_eq_map(pa1
, pa2
);
7337 res
= isl_map_intersect(res
, map
);
7343 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7344 * where the function values are equal.
7346 __isl_give isl_map
*isl_multi_pw_aff_eq_map(__isl_take isl_multi_pw_aff
*mpa1
,
7347 __isl_take isl_multi_pw_aff
*mpa2
)
7349 return isl_multi_pw_aff_order_map(mpa1
, mpa2
,
7350 &isl_multi_pw_aff_eq_map_on_space
);
7353 /* Intersect "map" with the result of applying "order"
7354 * on two copies of "mpa".
7356 static __isl_give isl_map
*isl_map_order_at_multi_pw_aff(
7357 __isl_take isl_map
*map
, __isl_take isl_multi_pw_aff
*mpa
,
7358 __isl_give isl_map
*(*order
)(__isl_take isl_multi_pw_aff
*mpa1
,
7359 __isl_take isl_multi_pw_aff
*mpa2
))
7361 return isl_map_intersect(map
, order(mpa
, isl_multi_pw_aff_copy(mpa
)));
7364 /* Return the subset of "map" where the domain and the range
7365 * have equal "mpa" values.
7367 __isl_give isl_map
*isl_map_eq_at_multi_pw_aff(__isl_take isl_map
*map
,
7368 __isl_take isl_multi_pw_aff
*mpa
)
7370 return isl_map_order_at_multi_pw_aff(map
, mpa
,
7371 &isl_multi_pw_aff_eq_map
);
7374 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7375 * where the function values of "mpa1" lexicographically satisfies "base"
7376 * compared to that of "mpa2". "space" is the space of the result.
7377 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7379 * "mpa1" lexicographically satisfies "base" compared to "mpa2"
7380 * if its i-th element satisfies "base" when compared to
7381 * the i-th element of "mpa2" while all previous elements are
7384 static __isl_give isl_map
*isl_multi_pw_aff_lex_map_on_space(
7385 __isl_keep isl_multi_pw_aff
*mpa1
, __isl_keep isl_multi_pw_aff
*mpa2
,
7386 __isl_give isl_map
*(*base
)(__isl_take isl_pw_aff
*pa1
,
7387 __isl_take isl_pw_aff
*pa2
),
7388 __isl_take isl_space
*space
)
7392 isl_map
*res
, *rest
;
7394 n
= isl_multi_pw_aff_dim(mpa1
, isl_dim_out
);
7396 space
= isl_space_free(space
);
7397 res
= isl_map_empty(isl_space_copy(space
));
7398 rest
= isl_map_universe(space
);
7400 for (i
= 0; i
< n
; ++i
) {
7401 isl_pw_aff
*pa1
, *pa2
;
7404 pa1
= isl_multi_pw_aff_get_pw_aff(mpa1
, i
);
7405 pa2
= isl_multi_pw_aff_get_pw_aff(mpa2
, i
);
7406 map
= base(pa1
, pa2
);
7407 map
= isl_map_intersect(map
, isl_map_copy(rest
));
7408 res
= isl_map_union(res
, map
);
7413 pa1
= isl_multi_pw_aff_get_pw_aff(mpa1
, i
);
7414 pa2
= isl_multi_pw_aff_get_pw_aff(mpa2
, i
);
7415 map
= isl_pw_aff_eq_map(pa1
, pa2
);
7416 rest
= isl_map_intersect(rest
, map
);
7425 #include "isl_aff_lex_templ.c"
7429 #include "isl_aff_lex_templ.c"
7433 #include "isl_aff_lex_templ.c"
7437 #include "isl_aff_lex_templ.c"
7439 /* Compare two isl_affs.
7441 * Return -1 if "aff1" is "smaller" than "aff2", 1 if "aff1" is "greater"
7442 * than "aff2" and 0 if they are equal.
7444 * The order is fairly arbitrary. We do consider expressions that only involve
7445 * earlier dimensions as "smaller".
7447 int isl_aff_plain_cmp(__isl_keep isl_aff
*aff1
, __isl_keep isl_aff
*aff2
)
7460 cmp
= isl_local_space_cmp(aff1
->ls
, aff2
->ls
);
7464 last1
= isl_seq_last_non_zero(aff1
->v
->el
+ 1, aff1
->v
->size
- 1);
7465 last2
= isl_seq_last_non_zero(aff2
->v
->el
+ 1, aff1
->v
->size
- 1);
7467 return last1
- last2
;
7469 return isl_seq_cmp(aff1
->v
->el
, aff2
->v
->el
, aff1
->v
->size
);
7472 /* Compare two isl_pw_affs.
7474 * Return -1 if "pa1" is "smaller" than "pa2", 1 if "pa1" is "greater"
7475 * than "pa2" and 0 if they are equal.
7477 * The order is fairly arbitrary. We do consider expressions that only involve
7478 * earlier dimensions as "smaller".
7480 int isl_pw_aff_plain_cmp(__isl_keep isl_pw_aff
*pa1
,
7481 __isl_keep isl_pw_aff
*pa2
)
7494 cmp
= isl_space_cmp(pa1
->dim
, pa2
->dim
);
7498 if (pa1
->n
!= pa2
->n
)
7499 return pa1
->n
- pa2
->n
;
7501 for (i
= 0; i
< pa1
->n
; ++i
) {
7502 cmp
= isl_set_plain_cmp(pa1
->p
[i
].set
, pa2
->p
[i
].set
);
7505 cmp
= isl_aff_plain_cmp(pa1
->p
[i
].aff
, pa2
->p
[i
].aff
);
7513 /* Return a piecewise affine expression that is equal to "v" on "domain".
7515 __isl_give isl_pw_aff
*isl_pw_aff_val_on_domain(__isl_take isl_set
*domain
,
7516 __isl_take isl_val
*v
)
7519 isl_local_space
*ls
;
7522 space
= isl_set_get_space(domain
);
7523 ls
= isl_local_space_from_space(space
);
7524 aff
= isl_aff_val_on_domain(ls
, v
);
7526 return isl_pw_aff_alloc(domain
, aff
);
7529 /* Return a piecewise affine expression that is equal to the parameter
7530 * with identifier "id" on "domain".
7532 __isl_give isl_pw_aff
*isl_pw_aff_param_on_domain_id(
7533 __isl_take isl_set
*domain
, __isl_take isl_id
*id
)
7538 space
= isl_set_get_space(domain
);
7539 space
= isl_space_add_param_id(space
, isl_id_copy(id
));
7540 domain
= isl_set_align_params(domain
, isl_space_copy(space
));
7541 aff
= isl_aff_param_on_domain_space_id(space
, id
);
7543 return isl_pw_aff_alloc(domain
, aff
);
7546 /* Return a multi affine expression that is equal to "mv" on domain
7549 __isl_give isl_multi_aff
*isl_multi_aff_multi_val_on_space(
7550 __isl_take isl_space
*space
, __isl_take isl_multi_val
*mv
)
7555 isl_local_space
*ls
;
7558 n
= isl_multi_val_dim(mv
, isl_dim_set
);
7559 if (!space
|| n
< 0)
7562 space2
= isl_multi_val_get_space(mv
);
7563 space2
= isl_space_align_params(space2
, isl_space_copy(space
));
7564 space
= isl_space_align_params(space
, isl_space_copy(space2
));
7565 space
= isl_space_map_from_domain_and_range(space
, space2
);
7566 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
7567 ls
= isl_local_space_from_space(isl_space_domain(space
));
7568 for (i
= 0; i
< n
; ++i
) {
7572 v
= isl_multi_val_get_val(mv
, i
);
7573 aff
= isl_aff_val_on_domain(isl_local_space_copy(ls
), v
);
7574 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
7576 isl_local_space_free(ls
);
7578 isl_multi_val_free(mv
);
7581 isl_space_free(space
);
7582 isl_multi_val_free(mv
);
7586 /* Return a piecewise multi-affine expression
7587 * that is equal to "mv" on "domain".
7589 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_multi_val_on_domain(
7590 __isl_take isl_set
*domain
, __isl_take isl_multi_val
*mv
)
7595 space
= isl_set_get_space(domain
);
7596 ma
= isl_multi_aff_multi_val_on_space(space
, mv
);
7598 return isl_pw_multi_aff_alloc(domain
, ma
);
7601 /* Internal data structure for isl_union_pw_multi_aff_multi_val_on_domain.
7602 * mv is the value that should be attained on each domain set
7603 * res collects the results
7605 struct isl_union_pw_multi_aff_multi_val_on_domain_data
{
7607 isl_union_pw_multi_aff
*res
;
7610 /* Create an isl_pw_multi_aff equal to data->mv on "domain"
7611 * and add it to data->res.
7613 static isl_stat
pw_multi_aff_multi_val_on_domain(__isl_take isl_set
*domain
,
7616 struct isl_union_pw_multi_aff_multi_val_on_domain_data
*data
= user
;
7617 isl_pw_multi_aff
*pma
;
7620 mv
= isl_multi_val_copy(data
->mv
);
7621 pma
= isl_pw_multi_aff_multi_val_on_domain(domain
, mv
);
7622 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma
);
7624 return data
->res
? isl_stat_ok
: isl_stat_error
;
7627 /* Return a union piecewise multi-affine expression
7628 * that is equal to "mv" on "domain".
7630 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_multi_val_on_domain(
7631 __isl_take isl_union_set
*domain
, __isl_take isl_multi_val
*mv
)
7633 struct isl_union_pw_multi_aff_multi_val_on_domain_data data
;
7636 space
= isl_union_set_get_space(domain
);
7637 data
.res
= isl_union_pw_multi_aff_empty(space
);
7639 if (isl_union_set_foreach_set(domain
,
7640 &pw_multi_aff_multi_val_on_domain
, &data
) < 0)
7641 data
.res
= isl_union_pw_multi_aff_free(data
.res
);
7642 isl_union_set_free(domain
);
7643 isl_multi_val_free(mv
);
7647 /* Compute the pullback of data->pma by the function represented by "pma2",
7648 * provided the spaces match, and add the results to data->res.
7650 static isl_stat
pullback_entry(__isl_take isl_pw_multi_aff
*pma2
, void *user
)
7652 struct isl_union_pw_multi_aff_bin_data
*data
= user
;
7654 if (!isl_space_tuple_is_equal(data
->pma
->dim
, isl_dim_in
,
7655 pma2
->dim
, isl_dim_out
)) {
7656 isl_pw_multi_aff_free(pma2
);
7660 pma2
= isl_pw_multi_aff_pullback_pw_multi_aff(
7661 isl_pw_multi_aff_copy(data
->pma
), pma2
);
7663 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma2
);
7665 return isl_stat_error
;
7670 /* Compute the pullback of "upma1" by the function represented by "upma2".
7672 __isl_give isl_union_pw_multi_aff
*
7673 isl_union_pw_multi_aff_pullback_union_pw_multi_aff(
7674 __isl_take isl_union_pw_multi_aff
*upma1
,
7675 __isl_take isl_union_pw_multi_aff
*upma2
)
7677 return bin_op(upma1
, upma2
, &pullback_entry
);
7680 /* Apply "upma2" to "upma1".
7682 * That is, compute the pullback of "upma2" by "upma1".
7684 __isl_give isl_union_pw_multi_aff
*
7685 isl_union_pw_multi_aff_apply_union_pw_multi_aff(
7686 __isl_take isl_union_pw_multi_aff
*upma1
,
7687 __isl_take isl_union_pw_multi_aff
*upma2
)
7689 return isl_union_pw_multi_aff_pullback_union_pw_multi_aff(upma2
, upma1
);
7692 /* Check that the domain space of "upa" matches "space".
7694 * This function is called from isl_multi_union_pw_aff_set_union_pw_aff and
7695 * can in principle never fail since the space "space" is that
7696 * of the isl_multi_union_pw_aff and is a set space such that
7697 * there is no domain space to match.
7699 * We check the parameters and double-check that "space" is
7700 * indeed that of a set.
7702 static isl_stat
isl_union_pw_aff_check_match_domain_space(
7703 __isl_keep isl_union_pw_aff
*upa
, __isl_keep isl_space
*space
)
7705 isl_space
*upa_space
;
7709 return isl_stat_error
;
7711 match
= isl_space_is_set(space
);
7713 return isl_stat_error
;
7715 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
7716 "expecting set space", return isl_stat_error
);
7718 upa_space
= isl_union_pw_aff_get_space(upa
);
7719 match
= isl_space_has_equal_params(space
, upa_space
);
7723 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
7724 "parameters don't match", goto error
);
7726 isl_space_free(upa_space
);
7729 isl_space_free(upa_space
);
7730 return isl_stat_error
;
7733 /* Do the parameters of "upa" match those of "space"?
7735 static isl_bool
isl_union_pw_aff_matching_params(
7736 __isl_keep isl_union_pw_aff
*upa
, __isl_keep isl_space
*space
)
7738 isl_space
*upa_space
;
7742 return isl_bool_error
;
7744 upa_space
= isl_union_pw_aff_get_space(upa
);
7746 match
= isl_space_has_equal_params(space
, upa_space
);
7748 isl_space_free(upa_space
);
7752 /* Internal data structure for isl_union_pw_aff_reset_domain_space.
7753 * space represents the new parameters.
7754 * res collects the results.
7756 struct isl_union_pw_aff_reset_params_data
{
7758 isl_union_pw_aff
*res
;
7761 /* Replace the parameters of "pa" by data->space and
7762 * add the result to data->res.
7764 static isl_stat
reset_params(__isl_take isl_pw_aff
*pa
, void *user
)
7766 struct isl_union_pw_aff_reset_params_data
*data
= user
;
7769 space
= isl_pw_aff_get_space(pa
);
7770 space
= isl_space_replace_params(space
, data
->space
);
7771 pa
= isl_pw_aff_reset_space(pa
, space
);
7772 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
7774 return data
->res
? isl_stat_ok
: isl_stat_error
;
7777 /* Replace the domain space of "upa" by "space".
7778 * Since a union expression does not have a (single) domain space,
7779 * "space" is necessarily a parameter space.
7781 * Since the order and the names of the parameters determine
7782 * the hash value, we need to create a new hash table.
7784 static __isl_give isl_union_pw_aff
*isl_union_pw_aff_reset_domain_space(
7785 __isl_take isl_union_pw_aff
*upa
, __isl_take isl_space
*space
)
7787 struct isl_union_pw_aff_reset_params_data data
= { space
};
7790 match
= isl_union_pw_aff_matching_params(upa
, space
);
7792 upa
= isl_union_pw_aff_free(upa
);
7794 isl_space_free(space
);
7798 data
.res
= isl_union_pw_aff_empty(isl_space_copy(space
));
7799 if (isl_union_pw_aff_foreach_pw_aff(upa
, &reset_params
, &data
) < 0)
7800 data
.res
= isl_union_pw_aff_free(data
.res
);
7802 isl_union_pw_aff_free(upa
);
7803 isl_space_free(space
);
7807 /* Return the floor of "pa".
7809 static __isl_give isl_pw_aff
*floor_entry(__isl_take isl_pw_aff
*pa
, void *user
)
7811 return isl_pw_aff_floor(pa
);
7814 /* Given f, return floor(f).
7816 __isl_give isl_union_pw_aff
*isl_union_pw_aff_floor(
7817 __isl_take isl_union_pw_aff
*upa
)
7819 return isl_union_pw_aff_transform_inplace(upa
, &floor_entry
, NULL
);
7824 * upa mod m = upa - m * floor(upa/m)
7826 * with m an integer value.
7828 __isl_give isl_union_pw_aff
*isl_union_pw_aff_mod_val(
7829 __isl_take isl_union_pw_aff
*upa
, __isl_take isl_val
*m
)
7831 isl_union_pw_aff
*res
;
7836 if (!isl_val_is_int(m
))
7837 isl_die(isl_val_get_ctx(m
), isl_error_invalid
,
7838 "expecting integer modulo", goto error
);
7839 if (!isl_val_is_pos(m
))
7840 isl_die(isl_val_get_ctx(m
), isl_error_invalid
,
7841 "expecting positive modulo", goto error
);
7843 res
= isl_union_pw_aff_copy(upa
);
7844 upa
= isl_union_pw_aff_scale_down_val(upa
, isl_val_copy(m
));
7845 upa
= isl_union_pw_aff_floor(upa
);
7846 upa
= isl_union_pw_aff_scale_val(upa
, m
);
7847 res
= isl_union_pw_aff_sub(res
, upa
);
7852 isl_union_pw_aff_free(upa
);
7856 /* Internal data structure for isl_union_pw_multi_aff_get_union_pw_aff.
7857 * pos is the output position that needs to be extracted.
7858 * res collects the results.
7860 struct isl_union_pw_multi_aff_get_union_pw_aff_data
{
7862 isl_union_pw_aff
*res
;
7865 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma"
7866 * (assuming it has such a dimension) and add it to data->res.
7868 static isl_stat
get_union_pw_aff(__isl_take isl_pw_multi_aff
*pma
, void *user
)
7870 struct isl_union_pw_multi_aff_get_union_pw_aff_data
*data
= user
;
7874 n_out
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
7876 return isl_stat_error
;
7877 if (data
->pos
>= n_out
) {
7878 isl_pw_multi_aff_free(pma
);
7882 pa
= isl_pw_multi_aff_get_pw_aff(pma
, data
->pos
);
7883 isl_pw_multi_aff_free(pma
);
7885 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
7887 return data
->res
? isl_stat_ok
: isl_stat_error
;
7890 /* Extract an isl_union_pw_aff corresponding to
7891 * output dimension "pos" of "upma".
7893 __isl_give isl_union_pw_aff
*isl_union_pw_multi_aff_get_union_pw_aff(
7894 __isl_keep isl_union_pw_multi_aff
*upma
, int pos
)
7896 struct isl_union_pw_multi_aff_get_union_pw_aff_data data
;
7903 isl_die(isl_union_pw_multi_aff_get_ctx(upma
), isl_error_invalid
,
7904 "cannot extract at negative position", return NULL
);
7906 space
= isl_union_pw_multi_aff_get_space(upma
);
7907 data
.res
= isl_union_pw_aff_empty(space
);
7909 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma
,
7910 &get_union_pw_aff
, &data
) < 0)
7911 data
.res
= isl_union_pw_aff_free(data
.res
);
7916 /* Return a union piecewise affine expression
7917 * that is equal to "aff" on "domain".
7919 __isl_give isl_union_pw_aff
*isl_union_pw_aff_aff_on_domain(
7920 __isl_take isl_union_set
*domain
, __isl_take isl_aff
*aff
)
7924 pa
= isl_pw_aff_from_aff(aff
);
7925 return isl_union_pw_aff_pw_aff_on_domain(domain
, pa
);
7928 /* Return a union piecewise affine expression
7929 * that is equal to the parameter identified by "id" on "domain".
7931 * Make sure the parameter appears in the space passed to
7932 * isl_aff_param_on_domain_space_id.
7934 __isl_give isl_union_pw_aff
*isl_union_pw_aff_param_on_domain_id(
7935 __isl_take isl_union_set
*domain
, __isl_take isl_id
*id
)
7940 space
= isl_union_set_get_space(domain
);
7941 space
= isl_space_add_param_id(space
, isl_id_copy(id
));
7942 aff
= isl_aff_param_on_domain_space_id(space
, id
);
7943 return isl_union_pw_aff_aff_on_domain(domain
, aff
);
7946 /* Internal data structure for isl_union_pw_aff_pw_aff_on_domain.
7947 * "pa" is the piecewise symbolic value that the resulting isl_union_pw_aff
7949 * "res" collects the results.
7951 struct isl_union_pw_aff_pw_aff_on_domain_data
{
7953 isl_union_pw_aff
*res
;
7956 /* Construct a piecewise affine expression that is equal to data->pa
7957 * on "domain" and add the result to data->res.
7959 static isl_stat
pw_aff_on_domain(__isl_take isl_set
*domain
, void *user
)
7961 struct isl_union_pw_aff_pw_aff_on_domain_data
*data
= user
;
7965 pa
= isl_pw_aff_copy(data
->pa
);
7966 dim
= isl_set_dim(domain
, isl_dim_set
);
7968 pa
= isl_pw_aff_free(pa
);
7969 pa
= isl_pw_aff_from_range(pa
);
7970 pa
= isl_pw_aff_add_dims(pa
, isl_dim_in
, dim
);
7971 pa
= isl_pw_aff_reset_domain_space(pa
, isl_set_get_space(domain
));
7972 pa
= isl_pw_aff_intersect_domain(pa
, domain
);
7973 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
7975 return data
->res
? isl_stat_ok
: isl_stat_error
;
7978 /* Return a union piecewise affine expression
7979 * that is equal to "pa" on "domain", assuming "domain" and "pa"
7980 * have been aligned.
7982 * Construct an isl_pw_aff on each of the sets in "domain" and
7983 * collect the results.
7985 static __isl_give isl_union_pw_aff
*isl_union_pw_aff_pw_aff_on_domain_aligned(
7986 __isl_take isl_union_set
*domain
, __isl_take isl_pw_aff
*pa
)
7988 struct isl_union_pw_aff_pw_aff_on_domain_data data
;
7991 space
= isl_union_set_get_space(domain
);
7992 data
.res
= isl_union_pw_aff_empty(space
);
7994 if (isl_union_set_foreach_set(domain
, &pw_aff_on_domain
, &data
) < 0)
7995 data
.res
= isl_union_pw_aff_free(data
.res
);
7996 isl_union_set_free(domain
);
7997 isl_pw_aff_free(pa
);
8001 /* Return a union piecewise affine expression
8002 * that is equal to "pa" on "domain".
8004 * Check that "pa" is a parametric expression,
8005 * align the parameters if needed and call
8006 * isl_union_pw_aff_pw_aff_on_domain_aligned.
8008 __isl_give isl_union_pw_aff
*isl_union_pw_aff_pw_aff_on_domain(
8009 __isl_take isl_union_set
*domain
, __isl_take isl_pw_aff
*pa
)
8012 isl_bool equal_params
;
8013 isl_space
*domain_space
, *pa_space
;
8015 pa_space
= isl_pw_aff_peek_space(pa
);
8016 is_set
= isl_space_is_set(pa_space
);
8020 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
8021 "expecting parametric expression", goto error
);
8023 domain_space
= isl_union_set_get_space(domain
);
8024 pa_space
= isl_pw_aff_get_space(pa
);
8025 equal_params
= isl_space_has_equal_params(domain_space
, pa_space
);
8026 if (equal_params
>= 0 && !equal_params
) {
8029 space
= isl_space_align_params(domain_space
, pa_space
);
8030 pa
= isl_pw_aff_align_params(pa
, isl_space_copy(space
));
8031 domain
= isl_union_set_align_params(domain
, space
);
8033 isl_space_free(domain_space
);
8034 isl_space_free(pa_space
);
8037 if (equal_params
< 0)
8039 return isl_union_pw_aff_pw_aff_on_domain_aligned(domain
, pa
);
8041 isl_union_set_free(domain
);
8042 isl_pw_aff_free(pa
);
8046 /* Internal data structure for isl_union_pw_aff_val_on_domain.
8047 * "v" is the value that the resulting isl_union_pw_aff needs to attain.
8048 * "res" collects the results.
8050 struct isl_union_pw_aff_val_on_domain_data
{
8052 isl_union_pw_aff
*res
;
8055 /* Construct a piecewise affine expression that is equal to data->v
8056 * on "domain" and add the result to data->res.
8058 static isl_stat
pw_aff_val_on_domain(__isl_take isl_set
*domain
, void *user
)
8060 struct isl_union_pw_aff_val_on_domain_data
*data
= user
;
8064 v
= isl_val_copy(data
->v
);
8065 pa
= isl_pw_aff_val_on_domain(domain
, v
);
8066 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
8068 return data
->res
? isl_stat_ok
: isl_stat_error
;
8071 /* Return a union piecewise affine expression
8072 * that is equal to "v" on "domain".
8074 * Construct an isl_pw_aff on each of the sets in "domain" and
8075 * collect the results.
8077 __isl_give isl_union_pw_aff
*isl_union_pw_aff_val_on_domain(
8078 __isl_take isl_union_set
*domain
, __isl_take isl_val
*v
)
8080 struct isl_union_pw_aff_val_on_domain_data data
;
8083 space
= isl_union_set_get_space(domain
);
8084 data
.res
= isl_union_pw_aff_empty(space
);
8086 if (isl_union_set_foreach_set(domain
, &pw_aff_val_on_domain
, &data
) < 0)
8087 data
.res
= isl_union_pw_aff_free(data
.res
);
8088 isl_union_set_free(domain
);
8093 /* Construct a piecewise multi affine expression
8094 * that is equal to "pa" and add it to upma.
8096 static isl_stat
pw_multi_aff_from_pw_aff_entry(__isl_take isl_pw_aff
*pa
,
8099 isl_union_pw_multi_aff
**upma
= user
;
8100 isl_pw_multi_aff
*pma
;
8102 pma
= isl_pw_multi_aff_from_pw_aff(pa
);
8103 *upma
= isl_union_pw_multi_aff_add_pw_multi_aff(*upma
, pma
);
8105 return *upma
? isl_stat_ok
: isl_stat_error
;
8108 /* Construct and return a union piecewise multi affine expression
8109 * that is equal to the given union piecewise affine expression.
8111 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_union_pw_aff(
8112 __isl_take isl_union_pw_aff
*upa
)
8115 isl_union_pw_multi_aff
*upma
;
8120 space
= isl_union_pw_aff_get_space(upa
);
8121 upma
= isl_union_pw_multi_aff_empty(space
);
8123 if (isl_union_pw_aff_foreach_pw_aff(upa
,
8124 &pw_multi_aff_from_pw_aff_entry
, &upma
) < 0)
8125 upma
= isl_union_pw_multi_aff_free(upma
);
8127 isl_union_pw_aff_free(upa
);
8131 /* Compute the set of elements in the domain of "pa" where it is zero and
8132 * add this set to "uset".
8134 static isl_stat
zero_union_set(__isl_take isl_pw_aff
*pa
, void *user
)
8136 isl_union_set
**uset
= (isl_union_set
**)user
;
8138 *uset
= isl_union_set_add_set(*uset
, isl_pw_aff_zero_set(pa
));
8140 return *uset
? isl_stat_ok
: isl_stat_error
;
8143 /* Return a union set containing those elements in the domain
8144 * of "upa" where it is zero.
8146 __isl_give isl_union_set
*isl_union_pw_aff_zero_union_set(
8147 __isl_take isl_union_pw_aff
*upa
)
8149 isl_union_set
*zero
;
8151 zero
= isl_union_set_empty(isl_union_pw_aff_get_space(upa
));
8152 if (isl_union_pw_aff_foreach_pw_aff(upa
, &zero_union_set
, &zero
) < 0)
8153 zero
= isl_union_set_free(zero
);
8155 isl_union_pw_aff_free(upa
);
8159 /* Internal data structure for isl_union_pw_aff_bind_id,
8160 * storing the parameter that needs to be bound and
8161 * the accumulated results.
8163 struct isl_bind_id_data
{
8165 isl_union_set
*bound
;
8168 /* Bind the piecewise affine function "pa" to the parameter data->id,
8169 * adding the resulting elements in the domain where the expression
8170 * is equal to the parameter to data->bound.
8172 static isl_stat
bind_id(__isl_take isl_pw_aff
*pa
, void *user
)
8174 struct isl_bind_id_data
*data
= user
;
8177 bound
= isl_pw_aff_bind_id(pa
, isl_id_copy(data
->id
));
8178 data
->bound
= isl_union_set_add_set(data
->bound
, bound
);
8180 return data
->bound
? isl_stat_ok
: isl_stat_error
;
8183 /* Bind the union piecewise affine function "upa" to the parameter "id",
8184 * returning the elements in the domain where the expression
8185 * is equal to the parameter.
8187 __isl_give isl_union_set
*isl_union_pw_aff_bind_id(
8188 __isl_take isl_union_pw_aff
*upa
, __isl_take isl_id
*id
)
8190 struct isl_bind_id_data data
= { id
};
8192 data
.bound
= isl_union_set_empty(isl_union_pw_aff_get_space(upa
));
8193 if (isl_union_pw_aff_foreach_pw_aff(upa
, &bind_id
, &data
) < 0)
8194 data
.bound
= isl_union_set_free(data
.bound
);
8196 isl_union_pw_aff_free(upa
);
8201 /* Internal data structure for isl_union_pw_aff_pullback_union_pw_multi_aff.
8202 * upma is the function that is plugged in.
8203 * pa is the current part of the function in which upma is plugged in.
8204 * res collects the results.
8206 struct isl_union_pw_aff_pullback_upma_data
{
8207 isl_union_pw_multi_aff
*upma
;
8209 isl_union_pw_aff
*res
;
8212 /* Check if "pma" can be plugged into data->pa.
8213 * If so, perform the pullback and add the result to data->res.
8215 static isl_stat
pa_pb_pma(__isl_take isl_pw_multi_aff
*pma
, void *user
)
8217 struct isl_union_pw_aff_pullback_upma_data
*data
= user
;
8220 if (!isl_space_tuple_is_equal(data
->pa
->dim
, isl_dim_in
,
8221 pma
->dim
, isl_dim_out
)) {
8222 isl_pw_multi_aff_free(pma
);
8226 pa
= isl_pw_aff_copy(data
->pa
);
8227 pa
= isl_pw_aff_pullback_pw_multi_aff(pa
, pma
);
8229 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
8231 return data
->res
? isl_stat_ok
: isl_stat_error
;
8234 /* Check if any of the elements of data->upma can be plugged into pa,
8235 * add if so add the result to data->res.
8237 static isl_stat
upa_pb_upma(__isl_take isl_pw_aff
*pa
, void *user
)
8239 struct isl_union_pw_aff_pullback_upma_data
*data
= user
;
8243 r
= isl_union_pw_multi_aff_foreach_pw_multi_aff(data
->upma
,
8245 isl_pw_aff_free(pa
);
8250 /* Compute the pullback of "upa" by the function represented by "upma".
8251 * In other words, plug in "upma" in "upa". The result contains
8252 * expressions defined over the domain space of "upma".
8254 * Run over all pairs of elements in "upa" and "upma", perform
8255 * the pullback when appropriate and collect the results.
8256 * If the hash value were based on the domain space rather than
8257 * the function space, then we could run through all elements
8258 * of "upma" and directly pick out the corresponding element of "upa".
8260 __isl_give isl_union_pw_aff
*isl_union_pw_aff_pullback_union_pw_multi_aff(
8261 __isl_take isl_union_pw_aff
*upa
,
8262 __isl_take isl_union_pw_multi_aff
*upma
)
8264 struct isl_union_pw_aff_pullback_upma_data data
= { NULL
, NULL
};
8267 space
= isl_union_pw_multi_aff_get_space(upma
);
8268 upa
= isl_union_pw_aff_align_params(upa
, space
);
8269 space
= isl_union_pw_aff_get_space(upa
);
8270 upma
= isl_union_pw_multi_aff_align_params(upma
, space
);
8276 data
.res
= isl_union_pw_aff_alloc_same_size(upa
);
8277 if (isl_union_pw_aff_foreach_pw_aff(upa
, &upa_pb_upma
, &data
) < 0)
8278 data
.res
= isl_union_pw_aff_free(data
.res
);
8280 isl_union_pw_aff_free(upa
);
8281 isl_union_pw_multi_aff_free(upma
);
8284 isl_union_pw_aff_free(upa
);
8285 isl_union_pw_multi_aff_free(upma
);
8290 #define BASE union_pw_aff
8292 #define DOMBASE union_set
8294 #include <isl_multi_explicit_domain.c>
8295 #include <isl_multi_union_pw_aff_explicit_domain.c>
8296 #include <isl_multi_templ.c>
8297 #include <isl_multi_apply_set.c>
8298 #include <isl_multi_apply_union_set.c>
8299 #include <isl_multi_arith_templ.c>
8300 #include <isl_multi_bind_templ.c>
8301 #include <isl_multi_coalesce.c>
8302 #include <isl_multi_dim_id_templ.c>
8303 #include <isl_multi_floor.c>
8304 #include <isl_multi_from_base_templ.c>
8305 #include <isl_multi_gist.c>
8306 #include <isl_multi_align_set.c>
8307 #include <isl_multi_align_union_set.c>
8308 #include <isl_multi_intersect.c>
8309 #include <isl_multi_nan_templ.c>
8310 #include <isl_multi_tuple_id_templ.c>
8311 #include <isl_multi_union_add_templ.c>
8313 /* Does "mupa" have a non-trivial explicit domain?
8315 * The explicit domain, if present, is trivial if it represents
8316 * an (obviously) universe parameter set.
8318 isl_bool
isl_multi_union_pw_aff_has_non_trivial_domain(
8319 __isl_keep isl_multi_union_pw_aff
*mupa
)
8321 isl_bool is_params
, trivial
;
8325 return isl_bool_error
;
8326 if (!isl_multi_union_pw_aff_has_explicit_domain(mupa
))
8327 return isl_bool_false
;
8328 is_params
= isl_union_set_is_params(mupa
->u
.dom
);
8329 if (is_params
< 0 || !is_params
)
8330 return isl_bool_not(is_params
);
8331 set
= isl_set_from_union_set(isl_union_set_copy(mupa
->u
.dom
));
8332 trivial
= isl_set_plain_is_universe(set
);
8334 return isl_bool_not(trivial
);
8337 /* Construct a multiple union piecewise affine expression
8338 * in the given space with value zero in each of the output dimensions.
8340 * Since there is no canonical zero value for
8341 * a union piecewise affine expression, we can only construct
8342 * a zero-dimensional "zero" value.
8344 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_zero(
8345 __isl_take isl_space
*space
)
8353 params
= isl_space_is_params(space
);
8357 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
8358 "expecting proper set space", goto error
);
8359 if (!isl_space_is_set(space
))
8360 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
8361 "expecting set space", goto error
);
8362 dim
= isl_space_dim(space
, isl_dim_out
);
8366 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
8367 "expecting 0D space", goto error
);
8369 return isl_multi_union_pw_aff_alloc(space
);
8371 isl_space_free(space
);
8375 /* Construct and return a multi union piecewise affine expression
8376 * that is equal to the given multi affine expression.
8378 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_from_multi_aff(
8379 __isl_take isl_multi_aff
*ma
)
8381 isl_multi_pw_aff
*mpa
;
8383 mpa
= isl_multi_pw_aff_from_multi_aff(ma
);
8384 return isl_multi_union_pw_aff_from_multi_pw_aff(mpa
);
8387 /* Construct and return a multi union piecewise affine expression
8388 * that is equal to the given multi piecewise affine expression.
8390 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_from_multi_pw_aff(
8391 __isl_take isl_multi_pw_aff
*mpa
)
8396 isl_multi_union_pw_aff
*mupa
;
8398 n
= isl_multi_pw_aff_dim(mpa
, isl_dim_out
);
8400 mpa
= isl_multi_pw_aff_free(mpa
);
8404 space
= isl_multi_pw_aff_get_space(mpa
);
8405 space
= isl_space_range(space
);
8406 mupa
= isl_multi_union_pw_aff_alloc(space
);
8408 for (i
= 0; i
< n
; ++i
) {
8410 isl_union_pw_aff
*upa
;
8412 pa
= isl_multi_pw_aff_get_pw_aff(mpa
, i
);
8413 upa
= isl_union_pw_aff_from_pw_aff(pa
);
8414 mupa
= isl_multi_union_pw_aff_restore_check_space(mupa
, i
, upa
);
8417 isl_multi_pw_aff_free(mpa
);
8422 /* Extract the range space of "pma" and assign it to *space.
8423 * If *space has already been set (through a previous call to this function),
8424 * then check that the range space is the same.
8426 static isl_stat
extract_space(__isl_take isl_pw_multi_aff
*pma
, void *user
)
8428 isl_space
**space
= user
;
8429 isl_space
*pma_space
;
8432 pma_space
= isl_space_range(isl_pw_multi_aff_get_space(pma
));
8433 isl_pw_multi_aff_free(pma
);
8436 return isl_stat_error
;
8442 equal
= isl_space_is_equal(pma_space
, *space
);
8443 isl_space_free(pma_space
);
8446 return isl_stat_error
;
8448 isl_die(isl_space_get_ctx(*space
), isl_error_invalid
,
8449 "range spaces not the same", return isl_stat_error
);
8453 /* Construct and return a multi union piecewise affine expression
8454 * that is equal to the given union piecewise multi affine expression.
8456 * In order to be able to perform the conversion, the input
8457 * needs to be non-empty and may only involve a single range space.
8459 * If the resulting multi union piecewise affine expression has
8460 * an explicit domain, then assign it the domain of the input.
8461 * In other cases, the domain is stored in the individual elements.
8463 __isl_give isl_multi_union_pw_aff
*
8464 isl_multi_union_pw_aff_from_union_pw_multi_aff(
8465 __isl_take isl_union_pw_multi_aff
*upma
)
8467 isl_space
*space
= NULL
;
8468 isl_multi_union_pw_aff
*mupa
;
8472 n
= isl_union_pw_multi_aff_n_pw_multi_aff(upma
);
8476 isl_die(isl_union_pw_multi_aff_get_ctx(upma
), isl_error_invalid
,
8477 "cannot extract range space from empty input",
8479 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma
, &extract_space
,
8486 n
= isl_space_dim(space
, isl_dim_set
);
8488 space
= isl_space_free(space
);
8489 mupa
= isl_multi_union_pw_aff_alloc(space
);
8491 for (i
= 0; i
< n
; ++i
) {
8492 isl_union_pw_aff
*upa
;
8494 upa
= isl_union_pw_multi_aff_get_union_pw_aff(upma
, i
);
8495 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
8497 if (isl_multi_union_pw_aff_has_explicit_domain(mupa
)) {
8499 isl_union_pw_multi_aff
*copy
;
8501 copy
= isl_union_pw_multi_aff_copy(upma
);
8502 dom
= isl_union_pw_multi_aff_domain(copy
);
8503 mupa
= isl_multi_union_pw_aff_intersect_domain(mupa
, dom
);
8506 isl_union_pw_multi_aff_free(upma
);
8509 isl_space_free(space
);
8510 isl_union_pw_multi_aff_free(upma
);
8514 /* Try and create an isl_multi_union_pw_aff that is equivalent
8515 * to the given isl_union_map.
8516 * The isl_union_map is required to be single-valued in each space.
8517 * Moreover, it cannot be empty and all range spaces need to be the same.
8518 * Otherwise, an error is produced.
8520 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_from_union_map(
8521 __isl_take isl_union_map
*umap
)
8523 isl_union_pw_multi_aff
*upma
;
8525 upma
= isl_union_pw_multi_aff_from_union_map(umap
);
8526 return isl_multi_union_pw_aff_from_union_pw_multi_aff(upma
);
8529 /* Return a multiple union piecewise affine expression
8530 * that is equal to "mv" on "domain", assuming "domain" and "mv"
8531 * have been aligned.
8533 * If the resulting multi union piecewise affine expression has
8534 * an explicit domain, then assign it the input domain.
8535 * In other cases, the domain is stored in the individual elements.
8537 static __isl_give isl_multi_union_pw_aff
*
8538 isl_multi_union_pw_aff_multi_val_on_domain_aligned(
8539 __isl_take isl_union_set
*domain
, __isl_take isl_multi_val
*mv
)
8544 isl_multi_union_pw_aff
*mupa
;
8546 n
= isl_multi_val_dim(mv
, isl_dim_set
);
8547 if (!domain
|| n
< 0)
8550 space
= isl_multi_val_get_space(mv
);
8551 mupa
= isl_multi_union_pw_aff_alloc(space
);
8552 for (i
= 0; i
< n
; ++i
) {
8554 isl_union_pw_aff
*upa
;
8556 v
= isl_multi_val_get_val(mv
, i
);
8557 upa
= isl_union_pw_aff_val_on_domain(isl_union_set_copy(domain
),
8559 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
8561 if (isl_multi_union_pw_aff_has_explicit_domain(mupa
))
8562 mupa
= isl_multi_union_pw_aff_intersect_domain(mupa
,
8563 isl_union_set_copy(domain
));
8565 isl_union_set_free(domain
);
8566 isl_multi_val_free(mv
);
8569 isl_union_set_free(domain
);
8570 isl_multi_val_free(mv
);
8574 /* Return a multiple union piecewise affine expression
8575 * that is equal to "mv" on "domain".
8577 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_multi_val_on_domain(
8578 __isl_take isl_union_set
*domain
, __isl_take isl_multi_val
*mv
)
8580 isl_bool equal_params
;
8584 equal_params
= isl_space_has_equal_params(domain
->dim
, mv
->space
);
8585 if (equal_params
< 0)
8588 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(
8590 domain
= isl_union_set_align_params(domain
,
8591 isl_multi_val_get_space(mv
));
8592 mv
= isl_multi_val_align_params(mv
, isl_union_set_get_space(domain
));
8593 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(domain
, mv
);
8595 isl_union_set_free(domain
);
8596 isl_multi_val_free(mv
);
8600 /* Return a multiple union piecewise affine expression
8601 * that is equal to "ma" on "domain".
8603 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_multi_aff_on_domain(
8604 __isl_take isl_union_set
*domain
, __isl_take isl_multi_aff
*ma
)
8606 isl_pw_multi_aff
*pma
;
8608 pma
= isl_pw_multi_aff_from_multi_aff(ma
);
8609 return isl_multi_union_pw_aff_pw_multi_aff_on_domain(domain
, pma
);
8612 /* Return a multiple union piecewise affine expression
8613 * that is equal to "pma" on "domain", assuming "domain" and "pma"
8614 * have been aligned.
8616 * If the resulting multi union piecewise affine expression has
8617 * an explicit domain, then assign it the input domain.
8618 * In other cases, the domain is stored in the individual elements.
8620 static __isl_give isl_multi_union_pw_aff
*
8621 isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(
8622 __isl_take isl_union_set
*domain
, __isl_take isl_pw_multi_aff
*pma
)
8627 isl_multi_union_pw_aff
*mupa
;
8629 n
= isl_pw_multi_aff_dim(pma
, isl_dim_set
);
8630 if (!domain
|| n
< 0)
8632 space
= isl_pw_multi_aff_get_space(pma
);
8633 mupa
= isl_multi_union_pw_aff_alloc(space
);
8634 for (i
= 0; i
< n
; ++i
) {
8636 isl_union_pw_aff
*upa
;
8638 pa
= isl_pw_multi_aff_get_pw_aff(pma
, i
);
8639 upa
= isl_union_pw_aff_pw_aff_on_domain(
8640 isl_union_set_copy(domain
), pa
);
8641 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
8643 if (isl_multi_union_pw_aff_has_explicit_domain(mupa
))
8644 mupa
= isl_multi_union_pw_aff_intersect_domain(mupa
,
8645 isl_union_set_copy(domain
));
8647 isl_union_set_free(domain
);
8648 isl_pw_multi_aff_free(pma
);
8651 isl_union_set_free(domain
);
8652 isl_pw_multi_aff_free(pma
);
8656 /* Return a multiple union piecewise affine expression
8657 * that is equal to "pma" on "domain".
8659 __isl_give isl_multi_union_pw_aff
*
8660 isl_multi_union_pw_aff_pw_multi_aff_on_domain(__isl_take isl_union_set
*domain
,
8661 __isl_take isl_pw_multi_aff
*pma
)
8663 isl_bool equal_params
;
8666 space
= isl_pw_multi_aff_peek_space(pma
);
8667 equal_params
= isl_union_set_space_has_equal_params(domain
, space
);
8668 if (equal_params
< 0)
8671 return isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(
8673 domain
= isl_union_set_align_params(domain
,
8674 isl_pw_multi_aff_get_space(pma
));
8675 pma
= isl_pw_multi_aff_align_params(pma
,
8676 isl_union_set_get_space(domain
));
8677 return isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(domain
,
8680 isl_union_set_free(domain
);
8681 isl_pw_multi_aff_free(pma
);
8685 /* Return a union set containing those elements in the domains
8686 * of the elements of "mupa" where they are all zero.
8688 * If there are no elements, then simply return the entire domain.
8690 __isl_give isl_union_set
*isl_multi_union_pw_aff_zero_union_set(
8691 __isl_take isl_multi_union_pw_aff
*mupa
)
8695 isl_union_pw_aff
*upa
;
8696 isl_union_set
*zero
;
8698 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8700 mupa
= isl_multi_union_pw_aff_free(mupa
);
8705 return isl_multi_union_pw_aff_domain(mupa
);
8707 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, 0);
8708 zero
= isl_union_pw_aff_zero_union_set(upa
);
8710 for (i
= 1; i
< n
; ++i
) {
8711 isl_union_set
*zero_i
;
8713 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
8714 zero_i
= isl_union_pw_aff_zero_union_set(upa
);
8716 zero
= isl_union_set_intersect(zero
, zero_i
);
8719 isl_multi_union_pw_aff_free(mupa
);
8723 /* Construct a union map mapping the shared domain
8724 * of the union piecewise affine expressions to the range of "mupa"
8725 * in the special case of a 0D multi union piecewise affine expression.
8727 * Construct a map between the explicit domain of "mupa" and
8729 * Note that this assumes that the domain consists of explicit elements.
8731 static __isl_give isl_union_map
*isl_union_map_from_multi_union_pw_aff_0D(
8732 __isl_take isl_multi_union_pw_aff
*mupa
)
8736 isl_union_set
*dom
, *ran
;
8738 space
= isl_multi_union_pw_aff_get_space(mupa
);
8739 dom
= isl_multi_union_pw_aff_domain(mupa
);
8740 ran
= isl_union_set_from_set(isl_set_universe(space
));
8742 is_params
= isl_union_set_is_params(dom
);
8744 dom
= isl_union_set_free(dom
);
8746 isl_die(isl_union_set_get_ctx(dom
), isl_error_invalid
,
8747 "cannot create union map from expression without "
8748 "explicit domain elements",
8749 dom
= isl_union_set_free(dom
));
8751 return isl_union_map_from_domain_and_range(dom
, ran
);
8754 /* Construct a union map mapping the shared domain
8755 * of the union piecewise affine expressions to the range of "mupa"
8756 * with each dimension in the range equated to the
8757 * corresponding union piecewise affine expression.
8759 * If the input is zero-dimensional, then construct a mapping
8760 * from its explicit domain.
8762 __isl_give isl_union_map
*isl_union_map_from_multi_union_pw_aff(
8763 __isl_take isl_multi_union_pw_aff
*mupa
)
8768 isl_union_map
*umap
;
8769 isl_union_pw_aff
*upa
;
8771 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8773 mupa
= isl_multi_union_pw_aff_free(mupa
);
8778 return isl_union_map_from_multi_union_pw_aff_0D(mupa
);
8780 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, 0);
8781 umap
= isl_union_map_from_union_pw_aff(upa
);
8783 for (i
= 1; i
< n
; ++i
) {
8784 isl_union_map
*umap_i
;
8786 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
8787 umap_i
= isl_union_map_from_union_pw_aff(upa
);
8788 umap
= isl_union_map_flat_range_product(umap
, umap_i
);
8791 space
= isl_multi_union_pw_aff_get_space(mupa
);
8792 umap
= isl_union_map_reset_range_space(umap
, space
);
8794 isl_multi_union_pw_aff_free(mupa
);
8798 /* Internal data structure for isl_union_pw_multi_aff_reset_range_space.
8799 * "range" is the space from which to set the range space.
8800 * "res" collects the results.
8802 struct isl_union_pw_multi_aff_reset_range_space_data
{
8804 isl_union_pw_multi_aff
*res
;
8807 /* Replace the range space of "pma" by the range space of data->range and
8808 * add the result to data->res.
8810 static isl_stat
reset_range_space(__isl_take isl_pw_multi_aff
*pma
, void *user
)
8812 struct isl_union_pw_multi_aff_reset_range_space_data
*data
= user
;
8815 space
= isl_pw_multi_aff_get_space(pma
);
8816 space
= isl_space_domain(space
);
8817 space
= isl_space_extend_domain_with_range(space
,
8818 isl_space_copy(data
->range
));
8819 pma
= isl_pw_multi_aff_reset_space(pma
, space
);
8820 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma
);
8822 return data
->res
? isl_stat_ok
: isl_stat_error
;
8825 /* Replace the range space of all the piecewise affine expressions in "upma" by
8826 * the range space of "space".
8828 * This assumes that all these expressions have the same output dimension.
8830 * Since the spaces of the expressions change, so do their hash values.
8831 * We therefore need to create a new isl_union_pw_multi_aff.
8832 * Note that the hash value is currently computed based on the entire
8833 * space even though there can only be a single expression with a given
8836 static __isl_give isl_union_pw_multi_aff
*
8837 isl_union_pw_multi_aff_reset_range_space(
8838 __isl_take isl_union_pw_multi_aff
*upma
, __isl_take isl_space
*space
)
8840 struct isl_union_pw_multi_aff_reset_range_space_data data
= { space
};
8841 isl_space
*space_upma
;
8843 space_upma
= isl_union_pw_multi_aff_get_space(upma
);
8844 data
.res
= isl_union_pw_multi_aff_empty(space_upma
);
8845 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma
,
8846 &reset_range_space
, &data
) < 0)
8847 data
.res
= isl_union_pw_multi_aff_free(data
.res
);
8849 isl_space_free(space
);
8850 isl_union_pw_multi_aff_free(upma
);
8854 /* Construct and return a union piecewise multi affine expression
8855 * that is equal to the given multi union piecewise affine expression,
8856 * in the special case of a 0D multi union piecewise affine expression.
8858 * Construct a union piecewise multi affine expression
8859 * on top of the explicit domain of the input.
8861 __isl_give isl_union_pw_multi_aff
*
8862 isl_union_pw_multi_aff_from_multi_union_pw_aff_0D(
8863 __isl_take isl_multi_union_pw_aff
*mupa
)
8867 isl_union_set
*domain
;
8869 space
= isl_multi_union_pw_aff_get_space(mupa
);
8870 mv
= isl_multi_val_zero(space
);
8871 domain
= isl_multi_union_pw_aff_domain(mupa
);
8872 return isl_union_pw_multi_aff_multi_val_on_domain(domain
, mv
);
8875 /* Construct and return a union piecewise multi affine expression
8876 * that is equal to the given multi union piecewise affine expression.
8878 * If the input is zero-dimensional, then
8879 * construct a union piecewise multi affine expression
8880 * on top of the explicit domain of the input.
8882 __isl_give isl_union_pw_multi_aff
*
8883 isl_union_pw_multi_aff_from_multi_union_pw_aff(
8884 __isl_take isl_multi_union_pw_aff
*mupa
)
8889 isl_union_pw_multi_aff
*upma
;
8890 isl_union_pw_aff
*upa
;
8892 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8894 mupa
= isl_multi_union_pw_aff_free(mupa
);
8899 return isl_union_pw_multi_aff_from_multi_union_pw_aff_0D(mupa
);
8901 space
= isl_multi_union_pw_aff_get_space(mupa
);
8902 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, 0);
8903 upma
= isl_union_pw_multi_aff_from_union_pw_aff(upa
);
8905 for (i
= 1; i
< n
; ++i
) {
8906 isl_union_pw_multi_aff
*upma_i
;
8908 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
8909 upma_i
= isl_union_pw_multi_aff_from_union_pw_aff(upa
);
8910 upma
= isl_union_pw_multi_aff_flat_range_product(upma
, upma_i
);
8913 upma
= isl_union_pw_multi_aff_reset_range_space(upma
, space
);
8915 isl_multi_union_pw_aff_free(mupa
);
8919 /* Intersect the range of "mupa" with "range",
8920 * in the special case where "mupa" is 0D.
8922 * Intersect the domain of "mupa" with the constraints on the parameters
8925 static __isl_give isl_multi_union_pw_aff
*mupa_intersect_range_0D(
8926 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_set
*range
)
8928 range
= isl_set_params(range
);
8929 mupa
= isl_multi_union_pw_aff_intersect_params(mupa
, range
);
8933 /* Intersect the range of "mupa" with "range".
8934 * That is, keep only those domain elements that have a function value
8937 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_intersect_range(
8938 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_set
*range
)
8940 isl_union_pw_multi_aff
*upma
;
8941 isl_union_set
*domain
;
8946 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8947 if (n
< 0 || !range
)
8950 space
= isl_set_get_space(range
);
8951 match
= isl_space_tuple_is_equal(mupa
->space
, isl_dim_set
,
8952 space
, isl_dim_set
);
8953 isl_space_free(space
);
8957 isl_die(isl_multi_union_pw_aff_get_ctx(mupa
), isl_error_invalid
,
8958 "space don't match", goto error
);
8960 return mupa_intersect_range_0D(mupa
, range
);
8962 upma
= isl_union_pw_multi_aff_from_multi_union_pw_aff(
8963 isl_multi_union_pw_aff_copy(mupa
));
8964 domain
= isl_union_set_from_set(range
);
8965 domain
= isl_union_set_preimage_union_pw_multi_aff(domain
, upma
);
8966 mupa
= isl_multi_union_pw_aff_intersect_domain(mupa
, domain
);
8970 isl_multi_union_pw_aff_free(mupa
);
8971 isl_set_free(range
);
8975 /* Return the shared domain of the elements of "mupa",
8976 * in the special case where "mupa" is zero-dimensional.
8978 * Return the explicit domain of "mupa".
8979 * Note that this domain may be a parameter set, either
8980 * because "mupa" is meant to live in a set space or
8981 * because no explicit domain has been set.
8983 __isl_give isl_union_set
*isl_multi_union_pw_aff_domain_0D(
8984 __isl_take isl_multi_union_pw_aff
*mupa
)
8988 dom
= isl_multi_union_pw_aff_get_explicit_domain(mupa
);
8989 isl_multi_union_pw_aff_free(mupa
);
8994 /* Return the shared domain of the elements of "mupa".
8996 * If "mupa" is zero-dimensional, then return its explicit domain.
8998 __isl_give isl_union_set
*isl_multi_union_pw_aff_domain(
8999 __isl_take isl_multi_union_pw_aff
*mupa
)
9003 isl_union_pw_aff
*upa
;
9006 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
9008 mupa
= isl_multi_union_pw_aff_free(mupa
);
9013 return isl_multi_union_pw_aff_domain_0D(mupa
);
9015 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, 0);
9016 dom
= isl_union_pw_aff_domain(upa
);
9017 for (i
= 1; i
< n
; ++i
) {
9018 isl_union_set
*dom_i
;
9020 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
9021 dom_i
= isl_union_pw_aff_domain(upa
);
9022 dom
= isl_union_set_intersect(dom
, dom_i
);
9025 isl_multi_union_pw_aff_free(mupa
);
9029 /* Apply "aff" to "mupa". The space of "mupa" is equal to the domain of "aff".
9030 * In particular, the spaces have been aligned.
9031 * The result is defined over the shared domain of the elements of "mupa"
9033 * We first extract the parametric constant part of "aff" and
9034 * define that over the shared domain.
9035 * Then we iterate over all input dimensions of "aff" and add the corresponding
9036 * multiples of the elements of "mupa".
9037 * Finally, we consider the integer divisions, calling the function
9038 * recursively to obtain an isl_union_pw_aff corresponding to the
9039 * integer division argument.
9041 static __isl_give isl_union_pw_aff
*multi_union_pw_aff_apply_aff(
9042 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_aff
*aff
)
9045 isl_size n_in
, n_div
;
9046 isl_union_pw_aff
*upa
;
9047 isl_union_set
*uset
;
9051 n_in
= isl_aff_dim(aff
, isl_dim_in
);
9052 n_div
= isl_aff_dim(aff
, isl_dim_div
);
9053 if (n_in
< 0 || n_div
< 0)
9056 uset
= isl_multi_union_pw_aff_domain(isl_multi_union_pw_aff_copy(mupa
));
9057 cst
= isl_aff_copy(aff
);
9058 cst
= isl_aff_drop_dims(cst
, isl_dim_div
, 0, n_div
);
9059 cst
= isl_aff_drop_dims(cst
, isl_dim_in
, 0, n_in
);
9060 cst
= isl_aff_project_domain_on_params(cst
);
9061 upa
= isl_union_pw_aff_aff_on_domain(uset
, cst
);
9063 for (i
= 0; i
< n_in
; ++i
) {
9064 isl_union_pw_aff
*upa_i
;
9066 if (!isl_aff_involves_dims(aff
, isl_dim_in
, i
, 1))
9068 v
= isl_aff_get_coefficient_val(aff
, isl_dim_in
, i
);
9069 upa_i
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
9070 upa_i
= isl_union_pw_aff_scale_val(upa_i
, v
);
9071 upa
= isl_union_pw_aff_add(upa
, upa_i
);
9074 for (i
= 0; i
< n_div
; ++i
) {
9076 isl_union_pw_aff
*upa_i
;
9078 if (!isl_aff_involves_dims(aff
, isl_dim_div
, i
, 1))
9080 div
= isl_aff_get_div(aff
, i
);
9081 upa_i
= multi_union_pw_aff_apply_aff(
9082 isl_multi_union_pw_aff_copy(mupa
), div
);
9083 upa_i
= isl_union_pw_aff_floor(upa_i
);
9084 v
= isl_aff_get_coefficient_val(aff
, isl_dim_div
, i
);
9085 upa_i
= isl_union_pw_aff_scale_val(upa_i
, v
);
9086 upa
= isl_union_pw_aff_add(upa
, upa_i
);
9089 isl_multi_union_pw_aff_free(mupa
);
9094 isl_multi_union_pw_aff_free(mupa
);
9099 /* Apply "aff" to "mupa". The space of "mupa" needs to be compatible
9100 * with the domain of "aff".
9101 * Furthermore, the dimension of this space needs to be greater than zero.
9102 * The result is defined over the shared domain of the elements of "mupa"
9104 * We perform these checks and then hand over control to
9105 * multi_union_pw_aff_apply_aff.
9107 __isl_give isl_union_pw_aff
*isl_multi_union_pw_aff_apply_aff(
9108 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_aff
*aff
)
9111 isl_space
*space1
, *space2
;
9114 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
9115 isl_aff_get_space(aff
));
9116 aff
= isl_aff_align_params(aff
, isl_multi_union_pw_aff_get_space(mupa
));
9120 space1
= isl_multi_union_pw_aff_get_space(mupa
);
9121 space2
= isl_aff_get_domain_space(aff
);
9122 equal
= isl_space_is_equal(space1
, space2
);
9123 isl_space_free(space1
);
9124 isl_space_free(space2
);
9128 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
9129 "spaces don't match", goto error
);
9130 dim
= isl_aff_dim(aff
, isl_dim_in
);
9134 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
9135 "cannot determine domains", goto error
);
9137 return multi_union_pw_aff_apply_aff(mupa
, aff
);
9139 isl_multi_union_pw_aff_free(mupa
);
9144 /* Apply "ma" to "mupa", in the special case where "mupa" is 0D.
9145 * The space of "mupa" is known to be compatible with the domain of "ma".
9147 * Construct an isl_multi_union_pw_aff that is equal to "ma"
9148 * on the domain of "mupa".
9150 static __isl_give isl_multi_union_pw_aff
*mupa_apply_multi_aff_0D(
9151 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_multi_aff
*ma
)
9155 dom
= isl_multi_union_pw_aff_domain(mupa
);
9156 ma
= isl_multi_aff_project_domain_on_params(ma
);
9158 return isl_multi_union_pw_aff_multi_aff_on_domain(dom
, ma
);
9161 /* Apply "ma" to "mupa". The space of "mupa" needs to be compatible
9162 * with the domain of "ma".
9163 * The result is defined over the shared domain of the elements of "mupa"
9165 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_apply_multi_aff(
9166 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_multi_aff
*ma
)
9168 isl_space
*space1
, *space2
;
9169 isl_multi_union_pw_aff
*res
;
9172 isl_size n_in
, n_out
;
9174 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
9175 isl_multi_aff_get_space(ma
));
9176 ma
= isl_multi_aff_align_params(ma
,
9177 isl_multi_union_pw_aff_get_space(mupa
));
9178 n_in
= isl_multi_aff_dim(ma
, isl_dim_in
);
9179 n_out
= isl_multi_aff_dim(ma
, isl_dim_out
);
9180 if (!mupa
|| n_in
< 0 || n_out
< 0)
9183 space1
= isl_multi_union_pw_aff_get_space(mupa
);
9184 space2
= isl_multi_aff_get_domain_space(ma
);
9185 equal
= isl_space_is_equal(space1
, space2
);
9186 isl_space_free(space1
);
9187 isl_space_free(space2
);
9191 isl_die(isl_multi_aff_get_ctx(ma
), isl_error_invalid
,
9192 "spaces don't match", goto error
);
9194 return mupa_apply_multi_aff_0D(mupa
, ma
);
9196 space1
= isl_space_range(isl_multi_aff_get_space(ma
));
9197 res
= isl_multi_union_pw_aff_alloc(space1
);
9199 for (i
= 0; i
< n_out
; ++i
) {
9201 isl_union_pw_aff
*upa
;
9203 aff
= isl_multi_aff_get_aff(ma
, i
);
9204 upa
= multi_union_pw_aff_apply_aff(
9205 isl_multi_union_pw_aff_copy(mupa
), aff
);
9206 res
= isl_multi_union_pw_aff_set_union_pw_aff(res
, i
, upa
);
9209 isl_multi_aff_free(ma
);
9210 isl_multi_union_pw_aff_free(mupa
);
9213 isl_multi_union_pw_aff_free(mupa
);
9214 isl_multi_aff_free(ma
);
9218 /* Apply "pa" to "mupa", in the special case where "mupa" is 0D.
9219 * The space of "mupa" is known to be compatible with the domain of "pa".
9221 * Construct an isl_multi_union_pw_aff that is equal to "pa"
9222 * on the domain of "mupa".
9224 static __isl_give isl_union_pw_aff
*isl_multi_union_pw_aff_apply_pw_aff_0D(
9225 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_pw_aff
*pa
)
9229 dom
= isl_multi_union_pw_aff_domain(mupa
);
9230 pa
= isl_pw_aff_project_domain_on_params(pa
);
9232 return isl_union_pw_aff_pw_aff_on_domain(dom
, pa
);
9235 /* Apply "pa" to "mupa". The space of "mupa" needs to be compatible
9236 * with the domain of "pa".
9237 * Furthermore, the dimension of this space needs to be greater than zero.
9238 * The result is defined over the shared domain of the elements of "mupa"
9240 __isl_give isl_union_pw_aff
*isl_multi_union_pw_aff_apply_pw_aff(
9241 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_pw_aff
*pa
)
9246 isl_space
*space
, *space2
;
9247 isl_union_pw_aff
*upa
;
9249 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
9250 isl_pw_aff_get_space(pa
));
9251 pa
= isl_pw_aff_align_params(pa
,
9252 isl_multi_union_pw_aff_get_space(mupa
));
9256 space
= isl_multi_union_pw_aff_get_space(mupa
);
9257 space2
= isl_pw_aff_get_domain_space(pa
);
9258 equal
= isl_space_is_equal(space
, space2
);
9259 isl_space_free(space
);
9260 isl_space_free(space2
);
9264 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
9265 "spaces don't match", goto error
);
9266 n_in
= isl_pw_aff_dim(pa
, isl_dim_in
);
9270 return isl_multi_union_pw_aff_apply_pw_aff_0D(mupa
, pa
);
9272 space
= isl_space_params(isl_multi_union_pw_aff_get_space(mupa
));
9273 upa
= isl_union_pw_aff_empty(space
);
9275 for (i
= 0; i
< pa
->n
; ++i
) {
9278 isl_multi_union_pw_aff
*mupa_i
;
9279 isl_union_pw_aff
*upa_i
;
9281 mupa_i
= isl_multi_union_pw_aff_copy(mupa
);
9282 domain
= isl_set_copy(pa
->p
[i
].set
);
9283 mupa_i
= isl_multi_union_pw_aff_intersect_range(mupa_i
, domain
);
9284 aff
= isl_aff_copy(pa
->p
[i
].aff
);
9285 upa_i
= multi_union_pw_aff_apply_aff(mupa_i
, aff
);
9286 upa
= isl_union_pw_aff_union_add(upa
, upa_i
);
9289 isl_multi_union_pw_aff_free(mupa
);
9290 isl_pw_aff_free(pa
);
9293 isl_multi_union_pw_aff_free(mupa
);
9294 isl_pw_aff_free(pa
);
9298 /* Apply "pma" to "mupa", in the special case where "mupa" is 0D.
9299 * The space of "mupa" is known to be compatible with the domain of "pma".
9301 * Construct an isl_multi_union_pw_aff that is equal to "pma"
9302 * on the domain of "mupa".
9304 static __isl_give isl_multi_union_pw_aff
*mupa_apply_pw_multi_aff_0D(
9305 __isl_take isl_multi_union_pw_aff
*mupa
,
9306 __isl_take isl_pw_multi_aff
*pma
)
9310 dom
= isl_multi_union_pw_aff_domain(mupa
);
9311 pma
= isl_pw_multi_aff_project_domain_on_params(pma
);
9313 return isl_multi_union_pw_aff_pw_multi_aff_on_domain(dom
, pma
);
9316 /* Apply "pma" to "mupa". The space of "mupa" needs to be compatible
9317 * with the domain of "pma".
9318 * The result is defined over the shared domain of the elements of "mupa"
9320 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_apply_pw_multi_aff(
9321 __isl_take isl_multi_union_pw_aff
*mupa
,
9322 __isl_take isl_pw_multi_aff
*pma
)
9324 isl_space
*space1
, *space2
;
9325 isl_multi_union_pw_aff
*res
;
9328 isl_size n_in
, n_out
;
9330 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
9331 isl_pw_multi_aff_get_space(pma
));
9332 pma
= isl_pw_multi_aff_align_params(pma
,
9333 isl_multi_union_pw_aff_get_space(mupa
));
9337 space1
= isl_multi_union_pw_aff_get_space(mupa
);
9338 space2
= isl_pw_multi_aff_get_domain_space(pma
);
9339 equal
= isl_space_is_equal(space1
, space2
);
9340 isl_space_free(space1
);
9341 isl_space_free(space2
);
9345 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
9346 "spaces don't match", goto error
);
9347 n_in
= isl_pw_multi_aff_dim(pma
, isl_dim_in
);
9348 n_out
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
9349 if (n_in
< 0 || n_out
< 0)
9352 return mupa_apply_pw_multi_aff_0D(mupa
, pma
);
9354 space1
= isl_space_range(isl_pw_multi_aff_get_space(pma
));
9355 res
= isl_multi_union_pw_aff_alloc(space1
);
9357 for (i
= 0; i
< n_out
; ++i
) {
9359 isl_union_pw_aff
*upa
;
9361 pa
= isl_pw_multi_aff_get_pw_aff(pma
, i
);
9362 upa
= isl_multi_union_pw_aff_apply_pw_aff(
9363 isl_multi_union_pw_aff_copy(mupa
), pa
);
9364 res
= isl_multi_union_pw_aff_set_union_pw_aff(res
, i
, upa
);
9367 isl_pw_multi_aff_free(pma
);
9368 isl_multi_union_pw_aff_free(mupa
);
9371 isl_multi_union_pw_aff_free(mupa
);
9372 isl_pw_multi_aff_free(pma
);
9376 /* Replace the explicit domain of "mupa" by its preimage under "upma".
9377 * If the explicit domain only keeps track of constraints on the parameters,
9378 * then only update those constraints.
9380 static __isl_give isl_multi_union_pw_aff
*preimage_explicit_domain(
9381 __isl_take isl_multi_union_pw_aff
*mupa
,
9382 __isl_keep isl_union_pw_multi_aff
*upma
)
9386 if (isl_multi_union_pw_aff_check_has_explicit_domain(mupa
) < 0)
9387 return isl_multi_union_pw_aff_free(mupa
);
9389 mupa
= isl_multi_union_pw_aff_cow(mupa
);
9393 is_params
= isl_union_set_is_params(mupa
->u
.dom
);
9395 return isl_multi_union_pw_aff_free(mupa
);
9397 upma
= isl_union_pw_multi_aff_copy(upma
);
9399 mupa
->u
.dom
= isl_union_set_intersect_params(mupa
->u
.dom
,
9400 isl_union_set_params(isl_union_pw_multi_aff_domain(upma
)));
9402 mupa
->u
.dom
= isl_union_set_preimage_union_pw_multi_aff(
9405 return isl_multi_union_pw_aff_free(mupa
);
9409 /* Compute the pullback of "mupa" by the function represented by "upma".
9410 * In other words, plug in "upma" in "mupa". The result contains
9411 * expressions defined over the domain space of "upma".
9413 * Run over all elements of "mupa" and plug in "upma" in each of them.
9415 * If "mupa" has an explicit domain, then it is this domain
9416 * that needs to undergo a pullback instead, i.e., a preimage.
9418 __isl_give isl_multi_union_pw_aff
*
9419 isl_multi_union_pw_aff_pullback_union_pw_multi_aff(
9420 __isl_take isl_multi_union_pw_aff
*mupa
,
9421 __isl_take isl_union_pw_multi_aff
*upma
)
9426 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
9427 isl_union_pw_multi_aff_get_space(upma
));
9428 upma
= isl_union_pw_multi_aff_align_params(upma
,
9429 isl_multi_union_pw_aff_get_space(mupa
));
9430 mupa
= isl_multi_union_pw_aff_cow(mupa
);
9431 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
9435 for (i
= 0; i
< n
; ++i
) {
9436 isl_union_pw_aff
*upa
;
9438 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
9439 upa
= isl_union_pw_aff_pullback_union_pw_multi_aff(upa
,
9440 isl_union_pw_multi_aff_copy(upma
));
9441 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
9444 if (isl_multi_union_pw_aff_has_explicit_domain(mupa
))
9445 mupa
= preimage_explicit_domain(mupa
, upma
);
9447 isl_union_pw_multi_aff_free(upma
);
9450 isl_multi_union_pw_aff_free(mupa
);
9451 isl_union_pw_multi_aff_free(upma
);
9455 /* Extract the sequence of elements in "mupa" with domain space "space"
9456 * (ignoring parameters).
9458 * For the elements of "mupa" that are not defined on the specified space,
9459 * the corresponding element in the result is empty.
9461 __isl_give isl_multi_pw_aff
*isl_multi_union_pw_aff_extract_multi_pw_aff(
9462 __isl_keep isl_multi_union_pw_aff
*mupa
, __isl_take isl_space
*space
)
9466 isl_space
*space_mpa
;
9467 isl_multi_pw_aff
*mpa
;
9469 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
9470 if (n
< 0 || !space
)
9473 space_mpa
= isl_multi_union_pw_aff_get_space(mupa
);
9474 space
= isl_space_replace_params(space
, space_mpa
);
9475 space_mpa
= isl_space_map_from_domain_and_range(isl_space_copy(space
),
9477 mpa
= isl_multi_pw_aff_alloc(space_mpa
);
9479 space
= isl_space_from_domain(space
);
9480 space
= isl_space_add_dims(space
, isl_dim_out
, 1);
9481 for (i
= 0; i
< n
; ++i
) {
9482 isl_union_pw_aff
*upa
;
9485 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
9486 pa
= isl_union_pw_aff_extract_pw_aff(upa
,
9487 isl_space_copy(space
));
9488 mpa
= isl_multi_pw_aff_set_pw_aff(mpa
, i
, pa
);
9489 isl_union_pw_aff_free(upa
);
9492 isl_space_free(space
);
9495 isl_space_free(space
);
9499 /* Data structure that specifies how isl_union_pw_multi_aff_un_op
9500 * should modify the base expressions in the input.
9502 * If "filter" is not NULL, then only the base expressions that satisfy "filter"
9503 * are taken into account.
9504 * "fn" is applied to each entry in the input.
9506 struct isl_union_pw_multi_aff_un_op_control
{
9507 isl_bool (*filter
)(__isl_keep isl_pw_multi_aff
*part
);
9508 __isl_give isl_pw_multi_aff
*(*fn
)(__isl_take isl_pw_multi_aff
*pma
);
9511 /* Wrapper for isl_union_pw_multi_aff_un_op filter functions (which do not take
9512 * a second argument) for use as an isl_union_pw_multi_aff_transform
9513 * filter function (which does take a second argument).
9514 * Simply call control->filter without the second argument.
9516 static isl_bool
isl_union_pw_multi_aff_un_op_filter_drop_user(
9517 __isl_take isl_pw_multi_aff
*pma
, void *user
)
9519 struct isl_union_pw_multi_aff_un_op_control
*control
= user
;
9521 return control
->filter(pma
);
9524 /* Wrapper for isl_union_pw_multi_aff_un_op base functions (which do not take
9525 * a second argument) for use as an isl_union_pw_multi_aff_transform
9526 * base function (which does take a second argument).
9527 * Simply call control->fn without the second argument.
9529 static __isl_give isl_pw_multi_aff
*isl_union_pw_multi_aff_un_op_drop_user(
9530 __isl_take isl_pw_multi_aff
*pma
, void *user
)
9532 struct isl_union_pw_multi_aff_un_op_control
*control
= user
;
9534 return control
->fn(pma
);
9537 /* Construct an isl_union_pw_multi_aff that is obtained by
9538 * modifying "upma" according to "control".
9540 * isl_union_pw_multi_aff_transform performs essentially
9541 * the same operation, but takes a filter and a callback function
9542 * of a different form (with an extra argument).
9543 * Call isl_union_pw_multi_aff_transform with wrappers
9544 * that remove this extra argument.
9546 static __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_un_op(
9547 __isl_take isl_union_pw_multi_aff
*upma
,
9548 struct isl_union_pw_multi_aff_un_op_control
*control
)
9550 struct isl_union_pw_multi_aff_transform_control t_control
= {
9551 .filter
= &isl_union_pw_multi_aff_un_op_filter_drop_user
,
9552 .filter_user
= control
,
9553 .fn
= &isl_union_pw_multi_aff_un_op_drop_user
,
9557 return isl_union_pw_multi_aff_transform(upma
, &t_control
);
9560 /* For each function in "upma" of the form A -> [B -> C],
9561 * extract the function A -> B and collect the results.
9563 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_range_factor_domain(
9564 __isl_take isl_union_pw_multi_aff
*upma
)
9566 struct isl_union_pw_multi_aff_un_op_control control
= {
9567 .filter
= &isl_pw_multi_aff_range_is_wrapping
,
9568 .fn
= &isl_pw_multi_aff_range_factor_domain
,
9570 return isl_union_pw_multi_aff_un_op(upma
, &control
);
9573 /* For each function in "upma" of the form A -> [B -> C],
9574 * extract the function A -> C and collect the results.
9576 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_range_factor_range(
9577 __isl_take isl_union_pw_multi_aff
*upma
)
9579 struct isl_union_pw_multi_aff_un_op_control control
= {
9580 .filter
= &isl_pw_multi_aff_range_is_wrapping
,
9581 .fn
= &isl_pw_multi_aff_range_factor_range
,
9583 return isl_union_pw_multi_aff_un_op(upma
, &control
);
9586 /* Evaluate the affine function "aff" in the void point "pnt".
9587 * In particular, return the value NaN.
9589 static __isl_give isl_val
*eval_void(__isl_take isl_aff
*aff
,
9590 __isl_take isl_point
*pnt
)
9594 ctx
= isl_point_get_ctx(pnt
);
9596 isl_point_free(pnt
);
9597 return isl_val_nan(ctx
);
9600 /* Evaluate the affine expression "aff"
9601 * in the coordinates (with denominator) "pnt".
9603 static __isl_give isl_val
*eval(__isl_keep isl_vec
*aff
,
9604 __isl_keep isl_vec
*pnt
)
9613 ctx
= isl_vec_get_ctx(aff
);
9616 isl_seq_inner_product(aff
->el
+ 1, pnt
->el
, pnt
->size
, &n
);
9617 isl_int_mul(d
, aff
->el
[0], pnt
->el
[0]);
9618 v
= isl_val_rat_from_isl_int(ctx
, n
, d
);
9619 v
= isl_val_normalize(v
);
9626 /* Check that the domain space of "aff" is equal to "space".
9628 static isl_stat
isl_aff_check_has_domain_space(__isl_keep isl_aff
*aff
,
9629 __isl_keep isl_space
*space
)
9633 ok
= isl_space_is_equal(isl_aff_peek_domain_space(aff
), space
);
9635 return isl_stat_error
;
9637 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
9638 "incompatible spaces", return isl_stat_error
);
9642 /* Evaluate the affine function "aff" in "pnt".
9644 __isl_give isl_val
*isl_aff_eval(__isl_take isl_aff
*aff
,
9645 __isl_take isl_point
*pnt
)
9649 isl_local_space
*ls
;
9651 if (isl_aff_check_has_domain_space(aff
, isl_point_peek_space(pnt
)) < 0)
9653 is_void
= isl_point_is_void(pnt
);
9657 return eval_void(aff
, pnt
);
9659 ls
= isl_aff_get_domain_local_space(aff
);
9660 pnt
= isl_local_space_lift_point(ls
, pnt
);
9662 v
= eval(aff
->v
, isl_point_peek_vec(pnt
));
9665 isl_point_free(pnt
);
9670 isl_point_free(pnt
);