2 * Copyright 2011 INRIA Saclay
3 * Copyright 2011 Sven Verdoolaege
4 * Copyright 2012-2014 Ecole Normale Superieure
5 * Copyright 2014 INRIA Rocquencourt
7 * Use of this software is governed by the MIT license
9 * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
10 * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
12 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
13 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
14 * B.P. 105 - 78153 Le Chesnay, France
17 #include <isl_ctx_private.h>
18 #include <isl_map_private.h>
19 #include <isl_union_map_private.h>
20 #include <isl_aff_private.h>
21 #include <isl_space_private.h>
22 #include <isl_local_space_private.h>
23 #include <isl_vec_private.h>
24 #include <isl_mat_private.h>
26 #include <isl/constraint.h>
29 #include <isl_val_private.h>
30 #include <isl_point_private.h>
31 #include <isl_config.h>
36 #include <isl_list_templ.c>
41 #include <isl_list_templ.c>
44 #define BASE pw_multi_aff
46 #include <isl_list_templ.c>
49 #define BASE union_pw_aff
51 #include <isl_list_templ.c>
54 #define BASE union_pw_multi_aff
56 #include <isl_list_templ.c>
58 __isl_give isl_aff
*isl_aff_alloc_vec(__isl_take isl_local_space
*ls
,
59 __isl_take isl_vec
*v
)
66 aff
= isl_calloc_type(v
->ctx
, struct isl_aff
);
76 isl_local_space_free(ls
);
81 __isl_give isl_aff
*isl_aff_alloc(__isl_take isl_local_space
*ls
)
90 ctx
= isl_local_space_get_ctx(ls
);
91 if (!isl_local_space_divs_known(ls
))
92 isl_die(ctx
, isl_error_invalid
, "local space has unknown divs",
94 if (!isl_local_space_is_set(ls
))
95 isl_die(ctx
, isl_error_invalid
,
96 "domain of affine expression should be a set",
99 total
= isl_local_space_dim(ls
, isl_dim_all
);
100 v
= isl_vec_alloc(ctx
, 1 + 1 + total
);
101 return isl_aff_alloc_vec(ls
, v
);
103 isl_local_space_free(ls
);
107 __isl_give isl_aff
*isl_aff_zero_on_domain(__isl_take isl_local_space
*ls
)
111 aff
= isl_aff_alloc(ls
);
115 isl_int_set_si(aff
->v
->el
[0], 1);
116 isl_seq_clr(aff
->v
->el
+ 1, aff
->v
->size
- 1);
121 /* Return a piecewise affine expression defined on the specified domain
122 * that is equal to zero.
124 __isl_give isl_pw_aff
*isl_pw_aff_zero_on_domain(__isl_take isl_local_space
*ls
)
126 return isl_pw_aff_from_aff(isl_aff_zero_on_domain(ls
));
129 /* Return an affine expression defined on the specified domain
130 * that represents NaN.
132 __isl_give isl_aff
*isl_aff_nan_on_domain(__isl_take isl_local_space
*ls
)
136 aff
= isl_aff_alloc(ls
);
140 isl_seq_clr(aff
->v
->el
, aff
->v
->size
);
145 /* Return a piecewise affine expression defined on the specified domain
146 * that represents NaN.
148 __isl_give isl_pw_aff
*isl_pw_aff_nan_on_domain(__isl_take isl_local_space
*ls
)
150 return isl_pw_aff_from_aff(isl_aff_nan_on_domain(ls
));
153 /* Return an affine expression that is equal to "val" on
154 * domain local space "ls".
156 __isl_give isl_aff
*isl_aff_val_on_domain(__isl_take isl_local_space
*ls
,
157 __isl_take isl_val
*val
)
163 if (!isl_val_is_rat(val
))
164 isl_die(isl_val_get_ctx(val
), isl_error_invalid
,
165 "expecting rational value", goto error
);
167 aff
= isl_aff_alloc(isl_local_space_copy(ls
));
171 isl_seq_clr(aff
->v
->el
+ 2, aff
->v
->size
- 2);
172 isl_int_set(aff
->v
->el
[1], val
->n
);
173 isl_int_set(aff
->v
->el
[0], val
->d
);
175 isl_local_space_free(ls
);
179 isl_local_space_free(ls
);
184 /* Return an affine expression that is equal to the specified dimension
187 __isl_give isl_aff
*isl_aff_var_on_domain(__isl_take isl_local_space
*ls
,
188 enum isl_dim_type type
, unsigned pos
)
196 space
= isl_local_space_get_space(ls
);
199 if (isl_space_is_map(space
))
200 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
201 "expecting (parameter) set space", goto error
);
202 if (isl_local_space_check_range(ls
, type
, pos
, 1) < 0)
205 isl_space_free(space
);
206 aff
= isl_aff_alloc(ls
);
210 pos
+= isl_local_space_offset(aff
->ls
, type
);
212 isl_int_set_si(aff
->v
->el
[0], 1);
213 isl_seq_clr(aff
->v
->el
+ 1, aff
->v
->size
- 1);
214 isl_int_set_si(aff
->v
->el
[1 + pos
], 1);
218 isl_local_space_free(ls
);
219 isl_space_free(space
);
223 /* Return a piecewise affine expression that is equal to
224 * the specified dimension in "ls".
226 __isl_give isl_pw_aff
*isl_pw_aff_var_on_domain(__isl_take isl_local_space
*ls
,
227 enum isl_dim_type type
, unsigned pos
)
229 return isl_pw_aff_from_aff(isl_aff_var_on_domain(ls
, type
, pos
));
232 /* Return an affine expression that is equal to the parameter
233 * in the domain space "space" with identifier "id".
235 __isl_give isl_aff
*isl_aff_param_on_domain_space_id(
236 __isl_take isl_space
*space
, __isl_take isl_id
*id
)
243 pos
= isl_space_find_dim_by_id(space
, isl_dim_param
, id
);
245 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
246 "parameter not found in space", goto error
);
248 ls
= isl_local_space_from_space(space
);
249 return isl_aff_var_on_domain(ls
, isl_dim_param
, pos
);
251 isl_space_free(space
);
256 __isl_give isl_aff
*isl_aff_copy(__isl_keep isl_aff
*aff
)
265 __isl_give isl_aff
*isl_aff_dup(__isl_keep isl_aff
*aff
)
270 return isl_aff_alloc_vec(isl_local_space_copy(aff
->ls
),
271 isl_vec_copy(aff
->v
));
274 __isl_give isl_aff
*isl_aff_cow(__isl_take isl_aff
*aff
)
282 return isl_aff_dup(aff
);
285 __isl_null isl_aff
*isl_aff_free(__isl_take isl_aff
*aff
)
293 isl_local_space_free(aff
->ls
);
294 isl_vec_free(aff
->v
);
301 isl_ctx
*isl_aff_get_ctx(__isl_keep isl_aff
*aff
)
303 return aff
? isl_local_space_get_ctx(aff
->ls
) : NULL
;
306 /* Return a hash value that digests "aff".
308 uint32_t isl_aff_get_hash(__isl_keep isl_aff
*aff
)
310 uint32_t hash
, ls_hash
, v_hash
;
315 hash
= isl_hash_init();
316 ls_hash
= isl_local_space_get_hash(aff
->ls
);
317 isl_hash_hash(hash
, ls_hash
);
318 v_hash
= isl_vec_get_hash(aff
->v
);
319 isl_hash_hash(hash
, v_hash
);
324 /* Externally, an isl_aff has a map space, but internally, the
325 * ls field corresponds to the domain of that space.
327 int isl_aff_dim(__isl_keep isl_aff
*aff
, enum isl_dim_type type
)
331 if (type
== isl_dim_out
)
333 if (type
== isl_dim_in
)
335 return isl_local_space_dim(aff
->ls
, type
);
338 /* Return the position of the dimension of the given type and name
340 * Return -1 if no such dimension can be found.
342 int isl_aff_find_dim_by_name(__isl_keep isl_aff
*aff
, enum isl_dim_type type
,
347 if (type
== isl_dim_out
)
349 if (type
== isl_dim_in
)
351 return isl_local_space_find_dim_by_name(aff
->ls
, type
, name
);
354 /* Return the domain space of "aff".
356 static __isl_keep isl_space
*isl_aff_peek_domain_space(__isl_keep isl_aff
*aff
)
358 return aff
? isl_local_space_peek_space(aff
->ls
) : NULL
;
361 __isl_give isl_space
*isl_aff_get_domain_space(__isl_keep isl_aff
*aff
)
363 return isl_space_copy(isl_aff_peek_domain_space(aff
));
366 __isl_give isl_space
*isl_aff_get_space(__isl_keep isl_aff
*aff
)
371 space
= isl_local_space_get_space(aff
->ls
);
372 space
= isl_space_from_domain(space
);
373 space
= isl_space_add_dims(space
, isl_dim_out
, 1);
377 __isl_give isl_local_space
*isl_aff_get_domain_local_space(
378 __isl_keep isl_aff
*aff
)
380 return aff
? isl_local_space_copy(aff
->ls
) : NULL
;
383 __isl_give isl_local_space
*isl_aff_get_local_space(__isl_keep isl_aff
*aff
)
388 ls
= isl_local_space_copy(aff
->ls
);
389 ls
= isl_local_space_from_domain(ls
);
390 ls
= isl_local_space_add_dims(ls
, isl_dim_out
, 1);
394 /* Return the local space of the domain of "aff".
395 * This may be either a copy or the local space itself
396 * if there is only one reference to "aff".
397 * This allows the local space to be modified inplace
398 * if both the expression and its local space have only a single reference.
399 * The caller is not allowed to modify "aff" between this call and
400 * a subsequent call to isl_aff_restore_domain_local_space.
401 * The only exception is that isl_aff_free can be called instead.
403 __isl_give isl_local_space
*isl_aff_take_domain_local_space(
404 __isl_keep isl_aff
*aff
)
411 return isl_aff_get_domain_local_space(aff
);
417 /* Set the local space of the domain of "aff" to "ls",
418 * where the local space of "aff" may be missing
419 * due to a preceding call to isl_aff_take_domain_local_space.
420 * However, in this case, "aff" only has a single reference and
421 * then the call to isl_aff_cow has no effect.
423 __isl_give isl_aff
*isl_aff_restore_domain_local_space(
424 __isl_keep isl_aff
*aff
, __isl_take isl_local_space
*ls
)
430 isl_local_space_free(ls
);
434 aff
= isl_aff_cow(aff
);
437 isl_local_space_free(aff
->ls
);
443 isl_local_space_free(ls
);
447 /* Externally, an isl_aff has a map space, but internally, the
448 * ls field corresponds to the domain of that space.
450 const char *isl_aff_get_dim_name(__isl_keep isl_aff
*aff
,
451 enum isl_dim_type type
, unsigned pos
)
455 if (type
== isl_dim_out
)
457 if (type
== isl_dim_in
)
459 return isl_local_space_get_dim_name(aff
->ls
, type
, pos
);
462 __isl_give isl_aff
*isl_aff_reset_domain_space(__isl_take isl_aff
*aff
,
463 __isl_take isl_space
*dim
)
465 aff
= isl_aff_cow(aff
);
469 aff
->ls
= isl_local_space_reset_space(aff
->ls
, dim
);
471 return isl_aff_free(aff
);
480 /* Reset the space of "aff". This function is called from isl_pw_templ.c
481 * and doesn't know if the space of an element object is represented
482 * directly or through its domain. It therefore passes along both.
484 __isl_give isl_aff
*isl_aff_reset_space_and_domain(__isl_take isl_aff
*aff
,
485 __isl_take isl_space
*space
, __isl_take isl_space
*domain
)
487 isl_space_free(space
);
488 return isl_aff_reset_domain_space(aff
, domain
);
491 /* Reorder the coefficients of the affine expression based
492 * on the given reordering.
493 * The reordering r is assumed to have been extended with the local
496 static __isl_give isl_vec
*vec_reorder(__isl_take isl_vec
*vec
,
497 __isl_take isl_reordering
*r
, int n_div
)
506 space
= isl_reordering_peek_space(r
);
507 res
= isl_vec_alloc(vec
->ctx
,
508 2 + isl_space_dim(space
, isl_dim_all
) + n_div
);
511 isl_seq_cpy(res
->el
, vec
->el
, 2);
512 isl_seq_clr(res
->el
+ 2, res
->size
- 2);
513 for (i
= 0; i
< r
->len
; ++i
)
514 isl_int_set(res
->el
[2 + r
->pos
[i
]], vec
->el
[2 + i
]);
516 isl_reordering_free(r
);
521 isl_reordering_free(r
);
525 /* Reorder the dimensions of the domain of "aff" according
526 * to the given reordering.
528 __isl_give isl_aff
*isl_aff_realign_domain(__isl_take isl_aff
*aff
,
529 __isl_take isl_reordering
*r
)
531 aff
= isl_aff_cow(aff
);
535 r
= isl_reordering_extend(r
, aff
->ls
->div
->n_row
);
536 aff
->v
= vec_reorder(aff
->v
, isl_reordering_copy(r
),
537 aff
->ls
->div
->n_row
);
538 aff
->ls
= isl_local_space_realign(aff
->ls
, r
);
540 if (!aff
->v
|| !aff
->ls
)
541 return isl_aff_free(aff
);
546 isl_reordering_free(r
);
550 __isl_give isl_aff
*isl_aff_align_params(__isl_take isl_aff
*aff
,
551 __isl_take isl_space
*model
)
553 isl_bool equal_params
;
558 equal_params
= isl_space_has_equal_params(aff
->ls
->dim
, model
);
559 if (equal_params
< 0)
564 exp
= isl_parameter_alignment_reordering(aff
->ls
->dim
, model
);
565 exp
= isl_reordering_extend_space(exp
,
566 isl_aff_get_domain_space(aff
));
567 aff
= isl_aff_realign_domain(aff
, exp
);
570 isl_space_free(model
);
573 isl_space_free(model
);
578 /* Is "aff" obviously equal to zero?
580 * If the denominator is zero, then "aff" is not equal to zero.
582 isl_bool
isl_aff_plain_is_zero(__isl_keep isl_aff
*aff
)
585 return isl_bool_error
;
587 if (isl_int_is_zero(aff
->v
->el
[0]))
588 return isl_bool_false
;
589 return isl_seq_first_non_zero(aff
->v
->el
+ 1, aff
->v
->size
- 1) < 0;
592 /* Does "aff" represent NaN?
594 isl_bool
isl_aff_is_nan(__isl_keep isl_aff
*aff
)
597 return isl_bool_error
;
599 return isl_seq_first_non_zero(aff
->v
->el
, 2) < 0;
602 /* Are "aff1" and "aff2" obviously equal?
604 * NaN is not equal to anything, not even to another NaN.
606 isl_bool
isl_aff_plain_is_equal(__isl_keep isl_aff
*aff1
,
607 __isl_keep isl_aff
*aff2
)
612 return isl_bool_error
;
614 if (isl_aff_is_nan(aff1
) || isl_aff_is_nan(aff2
))
615 return isl_bool_false
;
617 equal
= isl_local_space_is_equal(aff1
->ls
, aff2
->ls
);
618 if (equal
< 0 || !equal
)
621 return isl_vec_is_equal(aff1
->v
, aff2
->v
);
624 /* Return the common denominator of "aff" in "v".
626 * We cannot return anything meaningful in case of a NaN.
628 isl_stat
isl_aff_get_denominator(__isl_keep isl_aff
*aff
, isl_int
*v
)
631 return isl_stat_error
;
632 if (isl_aff_is_nan(aff
))
633 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
634 "cannot get denominator of NaN", return isl_stat_error
);
635 isl_int_set(*v
, aff
->v
->el
[0]);
639 /* Return the common denominator of "aff".
641 __isl_give isl_val
*isl_aff_get_denominator_val(__isl_keep isl_aff
*aff
)
648 ctx
= isl_aff_get_ctx(aff
);
649 if (isl_aff_is_nan(aff
))
650 return isl_val_nan(ctx
);
651 return isl_val_int_from_isl_int(ctx
, aff
->v
->el
[0]);
654 /* Return the constant term of "aff".
656 __isl_give isl_val
*isl_aff_get_constant_val(__isl_keep isl_aff
*aff
)
664 ctx
= isl_aff_get_ctx(aff
);
665 if (isl_aff_is_nan(aff
))
666 return isl_val_nan(ctx
);
667 v
= isl_val_rat_from_isl_int(ctx
, aff
->v
->el
[1], aff
->v
->el
[0]);
668 return isl_val_normalize(v
);
671 /* Return the coefficient of the variable of type "type" at position "pos"
674 __isl_give isl_val
*isl_aff_get_coefficient_val(__isl_keep isl_aff
*aff
,
675 enum isl_dim_type type
, int pos
)
683 ctx
= isl_aff_get_ctx(aff
);
684 if (type
== isl_dim_out
)
685 isl_die(ctx
, isl_error_invalid
,
686 "output/set dimension does not have a coefficient",
688 if (type
== isl_dim_in
)
691 if (isl_local_space_check_range(aff
->ls
, type
, pos
, 1) < 0)
694 if (isl_aff_is_nan(aff
))
695 return isl_val_nan(ctx
);
696 pos
+= isl_local_space_offset(aff
->ls
, type
);
697 v
= isl_val_rat_from_isl_int(ctx
, aff
->v
->el
[1 + pos
], aff
->v
->el
[0]);
698 return isl_val_normalize(v
);
701 /* Return the sign of the coefficient of the variable of type "type"
702 * at position "pos" of "aff".
704 int isl_aff_coefficient_sgn(__isl_keep isl_aff
*aff
, enum isl_dim_type type
,
712 ctx
= isl_aff_get_ctx(aff
);
713 if (type
== isl_dim_out
)
714 isl_die(ctx
, isl_error_invalid
,
715 "output/set dimension does not have a coefficient",
717 if (type
== isl_dim_in
)
720 if (isl_local_space_check_range(aff
->ls
, type
, pos
, 1) < 0)
723 pos
+= isl_local_space_offset(aff
->ls
, type
);
724 return isl_int_sgn(aff
->v
->el
[1 + pos
]);
727 /* Replace the numerator of the constant term of "aff" by "v".
729 * A NaN is unaffected by this operation.
731 __isl_give isl_aff
*isl_aff_set_constant(__isl_take isl_aff
*aff
, isl_int v
)
735 if (isl_aff_is_nan(aff
))
737 aff
= isl_aff_cow(aff
);
741 aff
->v
= isl_vec_cow(aff
->v
);
743 return isl_aff_free(aff
);
745 isl_int_set(aff
->v
->el
[1], v
);
750 /* Replace the constant term of "aff" by "v".
752 * A NaN is unaffected by this operation.
754 __isl_give isl_aff
*isl_aff_set_constant_val(__isl_take isl_aff
*aff
,
755 __isl_take isl_val
*v
)
760 if (isl_aff_is_nan(aff
)) {
765 if (!isl_val_is_rat(v
))
766 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
767 "expecting rational value", goto error
);
769 if (isl_int_eq(aff
->v
->el
[1], v
->n
) &&
770 isl_int_eq(aff
->v
->el
[0], v
->d
)) {
775 aff
= isl_aff_cow(aff
);
778 aff
->v
= isl_vec_cow(aff
->v
);
782 if (isl_int_eq(aff
->v
->el
[0], v
->d
)) {
783 isl_int_set(aff
->v
->el
[1], v
->n
);
784 } else if (isl_int_is_one(v
->d
)) {
785 isl_int_mul(aff
->v
->el
[1], aff
->v
->el
[0], v
->n
);
787 isl_seq_scale(aff
->v
->el
+ 1,
788 aff
->v
->el
+ 1, v
->d
, aff
->v
->size
- 1);
789 isl_int_mul(aff
->v
->el
[1], aff
->v
->el
[0], v
->n
);
790 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], v
->d
);
791 aff
->v
= isl_vec_normalize(aff
->v
);
804 /* Add "v" to the constant term of "aff".
806 * A NaN is unaffected by this operation.
808 __isl_give isl_aff
*isl_aff_add_constant(__isl_take isl_aff
*aff
, isl_int v
)
810 if (isl_int_is_zero(v
))
815 if (isl_aff_is_nan(aff
))
817 aff
= isl_aff_cow(aff
);
821 aff
->v
= isl_vec_cow(aff
->v
);
823 return isl_aff_free(aff
);
825 isl_int_addmul(aff
->v
->el
[1], aff
->v
->el
[0], v
);
830 /* Add "v" to the constant term of "aff".
832 * A NaN is unaffected by this operation.
834 __isl_give isl_aff
*isl_aff_add_constant_val(__isl_take isl_aff
*aff
,
835 __isl_take isl_val
*v
)
840 if (isl_aff_is_nan(aff
) || isl_val_is_zero(v
)) {
845 if (!isl_val_is_rat(v
))
846 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
847 "expecting rational value", goto error
);
849 aff
= isl_aff_cow(aff
);
853 aff
->v
= isl_vec_cow(aff
->v
);
857 if (isl_int_is_one(v
->d
)) {
858 isl_int_addmul(aff
->v
->el
[1], aff
->v
->el
[0], v
->n
);
859 } else if (isl_int_eq(aff
->v
->el
[0], v
->d
)) {
860 isl_int_add(aff
->v
->el
[1], aff
->v
->el
[1], v
->n
);
861 aff
->v
= isl_vec_normalize(aff
->v
);
865 isl_seq_scale(aff
->v
->el
+ 1,
866 aff
->v
->el
+ 1, v
->d
, aff
->v
->size
- 1);
867 isl_int_addmul(aff
->v
->el
[1], aff
->v
->el
[0], v
->n
);
868 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], v
->d
);
869 aff
->v
= isl_vec_normalize(aff
->v
);
882 __isl_give isl_aff
*isl_aff_add_constant_si(__isl_take isl_aff
*aff
, int v
)
887 isl_int_set_si(t
, v
);
888 aff
= isl_aff_add_constant(aff
, t
);
894 /* Add "v" to the numerator of the constant term of "aff".
896 * A NaN is unaffected by this operation.
898 __isl_give isl_aff
*isl_aff_add_constant_num(__isl_take isl_aff
*aff
, isl_int v
)
900 if (isl_int_is_zero(v
))
905 if (isl_aff_is_nan(aff
))
907 aff
= isl_aff_cow(aff
);
911 aff
->v
= isl_vec_cow(aff
->v
);
913 return isl_aff_free(aff
);
915 isl_int_add(aff
->v
->el
[1], aff
->v
->el
[1], v
);
920 /* Add "v" to the numerator of the constant term of "aff".
922 * A NaN is unaffected by this operation.
924 __isl_give isl_aff
*isl_aff_add_constant_num_si(__isl_take isl_aff
*aff
, int v
)
932 isl_int_set_si(t
, v
);
933 aff
= isl_aff_add_constant_num(aff
, t
);
939 /* Replace the numerator of the constant term of "aff" by "v".
941 * A NaN is unaffected by this operation.
943 __isl_give isl_aff
*isl_aff_set_constant_si(__isl_take isl_aff
*aff
, int v
)
947 if (isl_aff_is_nan(aff
))
949 aff
= isl_aff_cow(aff
);
953 aff
->v
= isl_vec_cow(aff
->v
);
955 return isl_aff_free(aff
);
957 isl_int_set_si(aff
->v
->el
[1], v
);
962 /* Replace the numerator of the coefficient of the variable of type "type"
963 * at position "pos" of "aff" by "v".
965 * A NaN is unaffected by this operation.
967 __isl_give isl_aff
*isl_aff_set_coefficient(__isl_take isl_aff
*aff
,
968 enum isl_dim_type type
, int pos
, isl_int v
)
973 if (type
== isl_dim_out
)
974 isl_die(aff
->v
->ctx
, isl_error_invalid
,
975 "output/set dimension does not have a coefficient",
976 return isl_aff_free(aff
));
977 if (type
== isl_dim_in
)
980 if (isl_local_space_check_range(aff
->ls
, type
, pos
, 1) < 0)
981 return isl_aff_free(aff
);
983 if (isl_aff_is_nan(aff
))
985 aff
= isl_aff_cow(aff
);
989 aff
->v
= isl_vec_cow(aff
->v
);
991 return isl_aff_free(aff
);
993 pos
+= isl_local_space_offset(aff
->ls
, type
);
994 isl_int_set(aff
->v
->el
[1 + pos
], v
);
999 /* Replace the numerator of the coefficient of the variable of type "type"
1000 * at position "pos" of "aff" by "v".
1002 * A NaN is unaffected by this operation.
1004 __isl_give isl_aff
*isl_aff_set_coefficient_si(__isl_take isl_aff
*aff
,
1005 enum isl_dim_type type
, int pos
, int v
)
1010 if (type
== isl_dim_out
)
1011 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1012 "output/set dimension does not have a coefficient",
1013 return isl_aff_free(aff
));
1014 if (type
== isl_dim_in
)
1017 if (isl_local_space_check_range(aff
->ls
, type
, pos
, 1) < 0)
1018 return isl_aff_free(aff
);
1020 if (isl_aff_is_nan(aff
))
1022 pos
+= isl_local_space_offset(aff
->ls
, type
);
1023 if (isl_int_cmp_si(aff
->v
->el
[1 + pos
], v
) == 0)
1026 aff
= isl_aff_cow(aff
);
1030 aff
->v
= isl_vec_cow(aff
->v
);
1032 return isl_aff_free(aff
);
1034 isl_int_set_si(aff
->v
->el
[1 + pos
], v
);
1039 /* Replace the coefficient of the variable of type "type" at position "pos"
1042 * A NaN is unaffected by this operation.
1044 __isl_give isl_aff
*isl_aff_set_coefficient_val(__isl_take isl_aff
*aff
,
1045 enum isl_dim_type type
, int pos
, __isl_take isl_val
*v
)
1050 if (type
== isl_dim_out
)
1051 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1052 "output/set dimension does not have a coefficient",
1054 if (type
== isl_dim_in
)
1057 if (isl_local_space_check_range(aff
->ls
, type
, pos
, 1) < 0)
1058 return isl_aff_free(aff
);
1060 if (isl_aff_is_nan(aff
)) {
1064 if (!isl_val_is_rat(v
))
1065 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1066 "expecting rational value", goto error
);
1068 pos
+= isl_local_space_offset(aff
->ls
, type
);
1069 if (isl_int_eq(aff
->v
->el
[1 + pos
], v
->n
) &&
1070 isl_int_eq(aff
->v
->el
[0], v
->d
)) {
1075 aff
= isl_aff_cow(aff
);
1078 aff
->v
= isl_vec_cow(aff
->v
);
1082 if (isl_int_eq(aff
->v
->el
[0], v
->d
)) {
1083 isl_int_set(aff
->v
->el
[1 + pos
], v
->n
);
1084 } else if (isl_int_is_one(v
->d
)) {
1085 isl_int_mul(aff
->v
->el
[1 + pos
], aff
->v
->el
[0], v
->n
);
1087 isl_seq_scale(aff
->v
->el
+ 1,
1088 aff
->v
->el
+ 1, v
->d
, aff
->v
->size
- 1);
1089 isl_int_mul(aff
->v
->el
[1 + pos
], aff
->v
->el
[0], v
->n
);
1090 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], v
->d
);
1091 aff
->v
= isl_vec_normalize(aff
->v
);
1104 /* Add "v" to the coefficient of the variable of type "type"
1105 * at position "pos" of "aff".
1107 * A NaN is unaffected by this operation.
1109 __isl_give isl_aff
*isl_aff_add_coefficient(__isl_take isl_aff
*aff
,
1110 enum isl_dim_type type
, int pos
, isl_int v
)
1115 if (type
== isl_dim_out
)
1116 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1117 "output/set dimension does not have a coefficient",
1118 return isl_aff_free(aff
));
1119 if (type
== isl_dim_in
)
1122 if (isl_local_space_check_range(aff
->ls
, type
, pos
, 1) < 0)
1123 return isl_aff_free(aff
);
1125 if (isl_aff_is_nan(aff
))
1127 aff
= isl_aff_cow(aff
);
1131 aff
->v
= isl_vec_cow(aff
->v
);
1133 return isl_aff_free(aff
);
1135 pos
+= isl_local_space_offset(aff
->ls
, type
);
1136 isl_int_addmul(aff
->v
->el
[1 + pos
], aff
->v
->el
[0], v
);
1141 /* Add "v" to the coefficient of the variable of type "type"
1142 * at position "pos" of "aff".
1144 * A NaN is unaffected by this operation.
1146 __isl_give isl_aff
*isl_aff_add_coefficient_val(__isl_take isl_aff
*aff
,
1147 enum isl_dim_type type
, int pos
, __isl_take isl_val
*v
)
1152 if (isl_val_is_zero(v
)) {
1157 if (type
== isl_dim_out
)
1158 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1159 "output/set dimension does not have a coefficient",
1161 if (type
== isl_dim_in
)
1164 if (isl_local_space_check_range(aff
->ls
, type
, pos
, 1) < 0)
1167 if (isl_aff_is_nan(aff
)) {
1171 if (!isl_val_is_rat(v
))
1172 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1173 "expecting rational value", goto error
);
1175 aff
= isl_aff_cow(aff
);
1179 aff
->v
= isl_vec_cow(aff
->v
);
1183 pos
+= isl_local_space_offset(aff
->ls
, type
);
1184 if (isl_int_is_one(v
->d
)) {
1185 isl_int_addmul(aff
->v
->el
[1 + pos
], aff
->v
->el
[0], v
->n
);
1186 } else if (isl_int_eq(aff
->v
->el
[0], v
->d
)) {
1187 isl_int_add(aff
->v
->el
[1 + pos
], aff
->v
->el
[1 + pos
], v
->n
);
1188 aff
->v
= isl_vec_normalize(aff
->v
);
1192 isl_seq_scale(aff
->v
->el
+ 1,
1193 aff
->v
->el
+ 1, v
->d
, aff
->v
->size
- 1);
1194 isl_int_addmul(aff
->v
->el
[1 + pos
], aff
->v
->el
[0], v
->n
);
1195 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], v
->d
);
1196 aff
->v
= isl_vec_normalize(aff
->v
);
1209 __isl_give isl_aff
*isl_aff_add_coefficient_si(__isl_take isl_aff
*aff
,
1210 enum isl_dim_type type
, int pos
, int v
)
1215 isl_int_set_si(t
, v
);
1216 aff
= isl_aff_add_coefficient(aff
, type
, pos
, t
);
1222 __isl_give isl_aff
*isl_aff_get_div(__isl_keep isl_aff
*aff
, int pos
)
1227 return isl_local_space_get_div(aff
->ls
, pos
);
1230 /* Return the negation of "aff".
1232 * As a special case, -NaN = NaN.
1234 __isl_give isl_aff
*isl_aff_neg(__isl_take isl_aff
*aff
)
1238 if (isl_aff_is_nan(aff
))
1240 aff
= isl_aff_cow(aff
);
1243 aff
->v
= isl_vec_cow(aff
->v
);
1245 return isl_aff_free(aff
);
1247 isl_seq_neg(aff
->v
->el
+ 1, aff
->v
->el
+ 1, aff
->v
->size
- 1);
1252 /* Remove divs from the local space that do not appear in the affine
1254 * We currently only remove divs at the end.
1255 * Some intermediate divs may also not appear directly in the affine
1256 * expression, but we would also need to check that no other divs are
1257 * defined in terms of them.
1259 __isl_give isl_aff
*isl_aff_remove_unused_divs(__isl_take isl_aff
*aff
)
1268 n
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
1269 off
= isl_local_space_offset(aff
->ls
, isl_dim_div
);
1271 pos
= isl_seq_last_non_zero(aff
->v
->el
+ 1 + off
, n
) + 1;
1275 aff
= isl_aff_cow(aff
);
1279 aff
->ls
= isl_local_space_drop_dims(aff
->ls
, isl_dim_div
, pos
, n
- pos
);
1280 aff
->v
= isl_vec_drop_els(aff
->v
, 1 + off
+ pos
, n
- pos
);
1281 if (!aff
->ls
|| !aff
->v
)
1282 return isl_aff_free(aff
);
1287 /* Look for any divs in the aff->ls with a denominator equal to one
1288 * and plug them into the affine expression and any subsequent divs
1289 * that may reference the div.
1291 static __isl_give isl_aff
*plug_in_integral_divs(__isl_take isl_aff
*aff
)
1297 isl_local_space
*ls
;
1303 n
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
1305 for (i
= 0; i
< n
; ++i
) {
1306 if (!isl_int_is_one(aff
->ls
->div
->row
[i
][0]))
1308 ls
= isl_local_space_copy(aff
->ls
);
1309 ls
= isl_local_space_substitute_seq(ls
, isl_dim_div
, i
,
1310 aff
->ls
->div
->row
[i
], len
, i
+ 1, n
- (i
+ 1));
1311 vec
= isl_vec_copy(aff
->v
);
1312 vec
= isl_vec_cow(vec
);
1318 pos
= isl_local_space_offset(aff
->ls
, isl_dim_div
) + i
;
1319 isl_seq_substitute(vec
->el
, pos
, aff
->ls
->div
->row
[i
],
1324 isl_vec_free(aff
->v
);
1326 isl_local_space_free(aff
->ls
);
1333 isl_local_space_free(ls
);
1334 return isl_aff_free(aff
);
1337 /* Look for any divs j that appear with a unit coefficient inside
1338 * the definitions of other divs i and plug them into the definitions
1341 * In particular, an expression of the form
1343 * floor((f(..) + floor(g(..)/n))/m)
1347 * floor((n * f(..) + g(..))/(n * m))
1349 * This simplification is correct because we can move the expression
1350 * f(..) into the inner floor in the original expression to obtain
1352 * floor(floor((n * f(..) + g(..))/n)/m)
1354 * from which we can derive the simplified expression.
1356 static __isl_give isl_aff
*plug_in_unit_divs(__isl_take isl_aff
*aff
)
1364 n
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
1365 off
= isl_local_space_offset(aff
->ls
, isl_dim_div
);
1366 for (i
= 1; i
< n
; ++i
) {
1367 for (j
= 0; j
< i
; ++j
) {
1368 if (!isl_int_is_one(aff
->ls
->div
->row
[i
][1 + off
+ j
]))
1370 aff
->ls
= isl_local_space_substitute_seq(aff
->ls
,
1371 isl_dim_div
, j
, aff
->ls
->div
->row
[j
],
1372 aff
->v
->size
, i
, 1);
1374 return isl_aff_free(aff
);
1381 /* Swap divs "a" and "b" in "aff", which is assumed to be non-NULL.
1383 * Even though this function is only called on isl_affs with a single
1384 * reference, we are careful to only change aff->v and aff->ls together.
1386 static __isl_give isl_aff
*swap_div(__isl_take isl_aff
*aff
, int a
, int b
)
1388 unsigned off
= isl_local_space_offset(aff
->ls
, isl_dim_div
);
1389 isl_local_space
*ls
;
1392 ls
= isl_local_space_copy(aff
->ls
);
1393 ls
= isl_local_space_swap_div(ls
, a
, b
);
1394 v
= isl_vec_copy(aff
->v
);
1399 isl_int_swap(v
->el
[1 + off
+ a
], v
->el
[1 + off
+ b
]);
1400 isl_vec_free(aff
->v
);
1402 isl_local_space_free(aff
->ls
);
1408 isl_local_space_free(ls
);
1409 return isl_aff_free(aff
);
1412 /* Merge divs "a" and "b" in "aff", which is assumed to be non-NULL.
1414 * We currently do not actually remove div "b", but simply add its
1415 * coefficient to that of "a" and then zero it out.
1417 static __isl_give isl_aff
*merge_divs(__isl_take isl_aff
*aff
, int a
, int b
)
1419 unsigned off
= isl_local_space_offset(aff
->ls
, isl_dim_div
);
1421 if (isl_int_is_zero(aff
->v
->el
[1 + off
+ b
]))
1424 aff
->v
= isl_vec_cow(aff
->v
);
1426 return isl_aff_free(aff
);
1428 isl_int_add(aff
->v
->el
[1 + off
+ a
],
1429 aff
->v
->el
[1 + off
+ a
], aff
->v
->el
[1 + off
+ b
]);
1430 isl_int_set_si(aff
->v
->el
[1 + off
+ b
], 0);
1435 /* Sort the divs in the local space of "aff" according to
1436 * the comparison function "cmp_row" in isl_local_space.c,
1437 * combining the coefficients of identical divs.
1439 * Reordering divs does not change the semantics of "aff",
1440 * so there is no need to call isl_aff_cow.
1441 * Moreover, this function is currently only called on isl_affs
1442 * with a single reference.
1444 static __isl_give isl_aff
*sort_divs(__isl_take isl_aff
*aff
)
1451 n
= isl_aff_dim(aff
, isl_dim_div
);
1452 for (i
= 1; i
< n
; ++i
) {
1453 for (j
= i
- 1; j
>= 0; --j
) {
1454 int cmp
= isl_mat_cmp_div(aff
->ls
->div
, j
, j
+ 1);
1458 aff
= merge_divs(aff
, j
, j
+ 1);
1460 aff
= swap_div(aff
, j
, j
+ 1);
1469 /* Normalize the representation of "aff".
1471 * This function should only be called of "new" isl_affs, i.e.,
1472 * with only a single reference. We therefore do not need to
1473 * worry about affecting other instances.
1475 __isl_give isl_aff
*isl_aff_normalize(__isl_take isl_aff
*aff
)
1479 aff
->v
= isl_vec_normalize(aff
->v
);
1481 return isl_aff_free(aff
);
1482 aff
= plug_in_integral_divs(aff
);
1483 aff
= plug_in_unit_divs(aff
);
1484 aff
= sort_divs(aff
);
1485 aff
= isl_aff_remove_unused_divs(aff
);
1489 /* Given f, return floor(f).
1490 * If f is an integer expression, then just return f.
1491 * If f is a constant, then return the constant floor(f).
1492 * Otherwise, if f = g/m, write g = q m + r,
1493 * create a new div d = [r/m] and return the expression q + d.
1494 * The coefficients in r are taken to lie between -m/2 and m/2.
1496 * reduce_div_coefficients performs the same normalization.
1498 * As a special case, floor(NaN) = NaN.
1500 __isl_give isl_aff
*isl_aff_floor(__isl_take isl_aff
*aff
)
1510 if (isl_aff_is_nan(aff
))
1512 if (isl_int_is_one(aff
->v
->el
[0]))
1515 aff
= isl_aff_cow(aff
);
1519 aff
->v
= isl_vec_cow(aff
->v
);
1521 return isl_aff_free(aff
);
1523 if (isl_aff_is_cst(aff
)) {
1524 isl_int_fdiv_q(aff
->v
->el
[1], aff
->v
->el
[1], aff
->v
->el
[0]);
1525 isl_int_set_si(aff
->v
->el
[0], 1);
1529 div
= isl_vec_copy(aff
->v
);
1530 div
= isl_vec_cow(div
);
1532 return isl_aff_free(aff
);
1534 ctx
= isl_aff_get_ctx(aff
);
1535 isl_int_fdiv_q(aff
->v
->el
[0], aff
->v
->el
[0], ctx
->two
);
1536 for (i
= 1; i
< aff
->v
->size
; ++i
) {
1537 isl_int_fdiv_r(div
->el
[i
], div
->el
[i
], div
->el
[0]);
1538 isl_int_fdiv_q(aff
->v
->el
[i
], aff
->v
->el
[i
], div
->el
[0]);
1539 if (isl_int_gt(div
->el
[i
], aff
->v
->el
[0])) {
1540 isl_int_sub(div
->el
[i
], div
->el
[i
], div
->el
[0]);
1541 isl_int_add_ui(aff
->v
->el
[i
], aff
->v
->el
[i
], 1);
1545 aff
->ls
= isl_local_space_add_div(aff
->ls
, div
);
1547 return isl_aff_free(aff
);
1549 size
= aff
->v
->size
;
1550 aff
->v
= isl_vec_extend(aff
->v
, size
+ 1);
1552 return isl_aff_free(aff
);
1553 isl_int_set_si(aff
->v
->el
[0], 1);
1554 isl_int_set_si(aff
->v
->el
[size
], 1);
1556 aff
= isl_aff_normalize(aff
);
1563 * aff mod m = aff - m * floor(aff/m)
1565 * with m an integer value.
1567 __isl_give isl_aff
*isl_aff_mod_val(__isl_take isl_aff
*aff
,
1568 __isl_take isl_val
*m
)
1575 if (!isl_val_is_int(m
))
1576 isl_die(isl_val_get_ctx(m
), isl_error_invalid
,
1577 "expecting integer modulo", goto error
);
1579 res
= isl_aff_copy(aff
);
1580 aff
= isl_aff_scale_down_val(aff
, isl_val_copy(m
));
1581 aff
= isl_aff_floor(aff
);
1582 aff
= isl_aff_scale_val(aff
, m
);
1583 res
= isl_aff_sub(res
, aff
);
1594 * pwaff mod m = pwaff - m * floor(pwaff/m)
1596 __isl_give isl_pw_aff
*isl_pw_aff_mod(__isl_take isl_pw_aff
*pwaff
, isl_int m
)
1600 res
= isl_pw_aff_copy(pwaff
);
1601 pwaff
= isl_pw_aff_scale_down(pwaff
, m
);
1602 pwaff
= isl_pw_aff_floor(pwaff
);
1603 pwaff
= isl_pw_aff_scale(pwaff
, m
);
1604 res
= isl_pw_aff_sub(res
, pwaff
);
1611 * pa mod m = pa - m * floor(pa/m)
1613 * with m an integer value.
1615 __isl_give isl_pw_aff
*isl_pw_aff_mod_val(__isl_take isl_pw_aff
*pa
,
1616 __isl_take isl_val
*m
)
1620 if (!isl_val_is_int(m
))
1621 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
1622 "expecting integer modulo", goto error
);
1623 pa
= isl_pw_aff_mod(pa
, m
->n
);
1627 isl_pw_aff_free(pa
);
1632 /* Given f, return ceil(f).
1633 * If f is an integer expression, then just return f.
1634 * Otherwise, let f be the expression
1640 * floor((e + m - 1)/m)
1642 * As a special case, ceil(NaN) = NaN.
1644 __isl_give isl_aff
*isl_aff_ceil(__isl_take isl_aff
*aff
)
1649 if (isl_aff_is_nan(aff
))
1651 if (isl_int_is_one(aff
->v
->el
[0]))
1654 aff
= isl_aff_cow(aff
);
1657 aff
->v
= isl_vec_cow(aff
->v
);
1659 return isl_aff_free(aff
);
1661 isl_int_add(aff
->v
->el
[1], aff
->v
->el
[1], aff
->v
->el
[0]);
1662 isl_int_sub_ui(aff
->v
->el
[1], aff
->v
->el
[1], 1);
1663 aff
= isl_aff_floor(aff
);
1668 /* Apply the expansion computed by isl_merge_divs.
1669 * The expansion itself is given by "exp" while the resulting
1670 * list of divs is given by "div".
1672 __isl_give isl_aff
*isl_aff_expand_divs(__isl_take isl_aff
*aff
,
1673 __isl_take isl_mat
*div
, int *exp
)
1679 aff
= isl_aff_cow(aff
);
1683 old_n_div
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
1684 new_n_div
= isl_mat_rows(div
);
1685 offset
= 1 + isl_local_space_offset(aff
->ls
, isl_dim_div
);
1687 aff
->v
= isl_vec_expand(aff
->v
, offset
, old_n_div
, exp
, new_n_div
);
1688 aff
->ls
= isl_local_space_replace_divs(aff
->ls
, div
);
1689 if (!aff
->v
|| !aff
->ls
)
1690 return isl_aff_free(aff
);
1698 /* Add two affine expressions that live in the same local space.
1700 static __isl_give isl_aff
*add_expanded(__isl_take isl_aff
*aff1
,
1701 __isl_take isl_aff
*aff2
)
1705 aff1
= isl_aff_cow(aff1
);
1709 aff1
->v
= isl_vec_cow(aff1
->v
);
1715 isl_int_gcd(gcd
, aff1
->v
->el
[0], aff2
->v
->el
[0]);
1716 isl_int_divexact(f
, aff2
->v
->el
[0], gcd
);
1717 isl_seq_scale(aff1
->v
->el
+ 1, aff1
->v
->el
+ 1, f
, aff1
->v
->size
- 1);
1718 isl_int_divexact(f
, aff1
->v
->el
[0], gcd
);
1719 isl_seq_addmul(aff1
->v
->el
+ 1, f
, aff2
->v
->el
+ 1, aff1
->v
->size
- 1);
1720 isl_int_divexact(f
, aff2
->v
->el
[0], gcd
);
1721 isl_int_mul(aff1
->v
->el
[0], aff1
->v
->el
[0], f
);
1733 /* Return the sum of "aff1" and "aff2".
1735 * If either of the two is NaN, then the result is NaN.
1737 __isl_give isl_aff
*isl_aff_add(__isl_take isl_aff
*aff1
,
1738 __isl_take isl_aff
*aff2
)
1749 ctx
= isl_aff_get_ctx(aff1
);
1750 if (!isl_space_is_equal(aff1
->ls
->dim
, aff2
->ls
->dim
))
1751 isl_die(ctx
, isl_error_invalid
,
1752 "spaces don't match", goto error
);
1754 if (isl_aff_is_nan(aff1
)) {
1758 if (isl_aff_is_nan(aff2
)) {
1763 n_div1
= isl_aff_dim(aff1
, isl_dim_div
);
1764 n_div2
= isl_aff_dim(aff2
, isl_dim_div
);
1765 if (n_div1
== 0 && n_div2
== 0)
1766 return add_expanded(aff1
, aff2
);
1768 exp1
= isl_alloc_array(ctx
, int, n_div1
);
1769 exp2
= isl_alloc_array(ctx
, int, n_div2
);
1770 if ((n_div1
&& !exp1
) || (n_div2
&& !exp2
))
1773 div
= isl_merge_divs(aff1
->ls
->div
, aff2
->ls
->div
, exp1
, exp2
);
1774 aff1
= isl_aff_expand_divs(aff1
, isl_mat_copy(div
), exp1
);
1775 aff2
= isl_aff_expand_divs(aff2
, div
, exp2
);
1779 return add_expanded(aff1
, aff2
);
1788 __isl_give isl_aff
*isl_aff_sub(__isl_take isl_aff
*aff1
,
1789 __isl_take isl_aff
*aff2
)
1791 return isl_aff_add(aff1
, isl_aff_neg(aff2
));
1794 /* Return the result of scaling "aff" by a factor of "f".
1796 * As a special case, f * NaN = NaN.
1798 __isl_give isl_aff
*isl_aff_scale(__isl_take isl_aff
*aff
, isl_int f
)
1804 if (isl_aff_is_nan(aff
))
1807 if (isl_int_is_one(f
))
1810 aff
= isl_aff_cow(aff
);
1813 aff
->v
= isl_vec_cow(aff
->v
);
1815 return isl_aff_free(aff
);
1817 if (isl_int_is_pos(f
) && isl_int_is_divisible_by(aff
->v
->el
[0], f
)) {
1818 isl_int_divexact(aff
->v
->el
[0], aff
->v
->el
[0], f
);
1823 isl_int_gcd(gcd
, aff
->v
->el
[0], f
);
1824 isl_int_divexact(aff
->v
->el
[0], aff
->v
->el
[0], gcd
);
1825 isl_int_divexact(gcd
, f
, gcd
);
1826 isl_seq_scale(aff
->v
->el
+ 1, aff
->v
->el
+ 1, gcd
, aff
->v
->size
- 1);
1832 /* Multiple "aff" by "v".
1834 __isl_give isl_aff
*isl_aff_scale_val(__isl_take isl_aff
*aff
,
1835 __isl_take isl_val
*v
)
1840 if (isl_val_is_one(v
)) {
1845 if (!isl_val_is_rat(v
))
1846 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1847 "expecting rational factor", goto error
);
1849 aff
= isl_aff_scale(aff
, v
->n
);
1850 aff
= isl_aff_scale_down(aff
, v
->d
);
1860 /* Return the result of scaling "aff" down by a factor of "f".
1862 * As a special case, NaN/f = NaN.
1864 __isl_give isl_aff
*isl_aff_scale_down(__isl_take isl_aff
*aff
, isl_int f
)
1870 if (isl_aff_is_nan(aff
))
1873 if (isl_int_is_one(f
))
1876 aff
= isl_aff_cow(aff
);
1880 if (isl_int_is_zero(f
))
1881 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1882 "cannot scale down by zero", return isl_aff_free(aff
));
1884 aff
->v
= isl_vec_cow(aff
->v
);
1886 return isl_aff_free(aff
);
1889 isl_seq_gcd(aff
->v
->el
+ 1, aff
->v
->size
- 1, &gcd
);
1890 isl_int_gcd(gcd
, gcd
, f
);
1891 isl_seq_scale_down(aff
->v
->el
+ 1, aff
->v
->el
+ 1, gcd
, aff
->v
->size
- 1);
1892 isl_int_divexact(gcd
, f
, gcd
);
1893 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], gcd
);
1899 /* Divide "aff" by "v".
1901 __isl_give isl_aff
*isl_aff_scale_down_val(__isl_take isl_aff
*aff
,
1902 __isl_take isl_val
*v
)
1907 if (isl_val_is_one(v
)) {
1912 if (!isl_val_is_rat(v
))
1913 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1914 "expecting rational factor", goto error
);
1915 if (!isl_val_is_pos(v
))
1916 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1917 "factor needs to be positive", goto error
);
1919 aff
= isl_aff_scale(aff
, v
->d
);
1920 aff
= isl_aff_scale_down(aff
, v
->n
);
1930 __isl_give isl_aff
*isl_aff_scale_down_ui(__isl_take isl_aff
*aff
, unsigned f
)
1938 isl_int_set_ui(v
, f
);
1939 aff
= isl_aff_scale_down(aff
, v
);
1945 __isl_give isl_aff
*isl_aff_set_dim_name(__isl_take isl_aff
*aff
,
1946 enum isl_dim_type type
, unsigned pos
, const char *s
)
1948 aff
= isl_aff_cow(aff
);
1951 if (type
== isl_dim_out
)
1952 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1953 "cannot set name of output/set dimension",
1954 return isl_aff_free(aff
));
1955 if (type
== isl_dim_in
)
1957 aff
->ls
= isl_local_space_set_dim_name(aff
->ls
, type
, pos
, s
);
1959 return isl_aff_free(aff
);
1964 __isl_give isl_aff
*isl_aff_set_dim_id(__isl_take isl_aff
*aff
,
1965 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
1967 aff
= isl_aff_cow(aff
);
1970 if (type
== isl_dim_out
)
1971 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1972 "cannot set name of output/set dimension",
1974 if (type
== isl_dim_in
)
1976 aff
->ls
= isl_local_space_set_dim_id(aff
->ls
, type
, pos
, id
);
1978 return isl_aff_free(aff
);
1987 /* Replace the identifier of the input tuple of "aff" by "id".
1988 * type is currently required to be equal to isl_dim_in
1990 __isl_give isl_aff
*isl_aff_set_tuple_id(__isl_take isl_aff
*aff
,
1991 enum isl_dim_type type
, __isl_take isl_id
*id
)
1993 aff
= isl_aff_cow(aff
);
1996 if (type
!= isl_dim_in
)
1997 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1998 "cannot only set id of input tuple", goto error
);
1999 aff
->ls
= isl_local_space_set_tuple_id(aff
->ls
, isl_dim_set
, id
);
2001 return isl_aff_free(aff
);
2010 /* Exploit the equalities in "eq" to simplify the affine expression
2011 * and the expressions of the integer divisions in the local space.
2012 * The integer divisions in this local space are assumed to appear
2013 * as regular dimensions in "eq".
2015 static __isl_give isl_aff
*isl_aff_substitute_equalities_lifted(
2016 __isl_take isl_aff
*aff
, __isl_take isl_basic_set
*eq
)
2024 if (eq
->n_eq
== 0) {
2025 isl_basic_set_free(eq
);
2029 aff
= isl_aff_cow(aff
);
2033 aff
->ls
= isl_local_space_substitute_equalities(aff
->ls
,
2034 isl_basic_set_copy(eq
));
2035 aff
->v
= isl_vec_cow(aff
->v
);
2036 if (!aff
->ls
|| !aff
->v
)
2039 total
= 1 + isl_space_dim(eq
->dim
, isl_dim_all
);
2041 for (i
= 0; i
< eq
->n_eq
; ++i
) {
2042 j
= isl_seq_last_non_zero(eq
->eq
[i
], total
+ n_div
);
2043 if (j
< 0 || j
== 0 || j
>= total
)
2046 isl_seq_elim(aff
->v
->el
+ 1, eq
->eq
[i
], j
, total
,
2050 isl_basic_set_free(eq
);
2051 aff
= isl_aff_normalize(aff
);
2054 isl_basic_set_free(eq
);
2059 /* Exploit the equalities in "eq" to simplify the affine expression
2060 * and the expressions of the integer divisions in the local space.
2062 __isl_give isl_aff
*isl_aff_substitute_equalities(__isl_take isl_aff
*aff
,
2063 __isl_take isl_basic_set
*eq
)
2069 n_div
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
2071 eq
= isl_basic_set_add_dims(eq
, isl_dim_set
, n_div
);
2072 return isl_aff_substitute_equalities_lifted(aff
, eq
);
2074 isl_basic_set_free(eq
);
2079 /* Look for equalities among the variables shared by context and aff
2080 * and the integer divisions of aff, if any.
2081 * The equalities are then used to eliminate coefficients and/or integer
2082 * divisions from aff.
2084 __isl_give isl_aff
*isl_aff_gist(__isl_take isl_aff
*aff
,
2085 __isl_take isl_set
*context
)
2087 isl_local_space
*ls
;
2088 isl_basic_set
*hull
;
2090 ls
= isl_aff_get_domain_local_space(aff
);
2091 context
= isl_local_space_lift_set(ls
, context
);
2093 hull
= isl_set_affine_hull(context
);
2094 return isl_aff_substitute_equalities_lifted(aff
, hull
);
2097 __isl_give isl_aff
*isl_aff_gist_params(__isl_take isl_aff
*aff
,
2098 __isl_take isl_set
*context
)
2100 isl_set
*dom_context
= isl_set_universe(isl_aff_get_domain_space(aff
));
2101 dom_context
= isl_set_intersect_params(dom_context
, context
);
2102 return isl_aff_gist(aff
, dom_context
);
2105 /* Return a basic set containing those elements in the space
2106 * of aff where it is positive. "rational" should not be set.
2108 * If "aff" is NaN, then it is not positive.
2110 static __isl_give isl_basic_set
*aff_pos_basic_set(__isl_take isl_aff
*aff
,
2113 isl_constraint
*ineq
;
2114 isl_basic_set
*bset
;
2119 if (isl_aff_is_nan(aff
)) {
2120 isl_space
*space
= isl_aff_get_domain_space(aff
);
2122 return isl_basic_set_empty(space
);
2125 isl_die(isl_aff_get_ctx(aff
), isl_error_unsupported
,
2126 "rational sets not supported", goto error
);
2128 ineq
= isl_inequality_from_aff(aff
);
2129 c
= isl_constraint_get_constant_val(ineq
);
2130 c
= isl_val_sub_ui(c
, 1);
2131 ineq
= isl_constraint_set_constant_val(ineq
, c
);
2133 bset
= isl_basic_set_from_constraint(ineq
);
2134 bset
= isl_basic_set_simplify(bset
);
2141 /* Return a basic set containing those elements in the space
2142 * of aff where it is non-negative.
2143 * If "rational" is set, then return a rational basic set.
2145 * If "aff" is NaN, then it is not non-negative (it's not negative either).
2147 static __isl_give isl_basic_set
*aff_nonneg_basic_set(
2148 __isl_take isl_aff
*aff
, int rational
)
2150 isl_constraint
*ineq
;
2151 isl_basic_set
*bset
;
2155 if (isl_aff_is_nan(aff
)) {
2156 isl_space
*space
= isl_aff_get_domain_space(aff
);
2158 return isl_basic_set_empty(space
);
2161 ineq
= isl_inequality_from_aff(aff
);
2163 bset
= isl_basic_set_from_constraint(ineq
);
2165 bset
= isl_basic_set_set_rational(bset
);
2166 bset
= isl_basic_set_simplify(bset
);
2170 /* Return a basic set containing those elements in the space
2171 * of aff where it is non-negative.
2173 __isl_give isl_basic_set
*isl_aff_nonneg_basic_set(__isl_take isl_aff
*aff
)
2175 return aff_nonneg_basic_set(aff
, 0);
2178 /* Return a basic set containing those elements in the domain space
2179 * of "aff" where it is positive.
2181 __isl_give isl_basic_set
*isl_aff_pos_basic_set(__isl_take isl_aff
*aff
)
2183 aff
= isl_aff_add_constant_num_si(aff
, -1);
2184 return isl_aff_nonneg_basic_set(aff
);
2187 /* Return a basic set containing those elements in the domain space
2188 * of aff where it is negative.
2190 __isl_give isl_basic_set
*isl_aff_neg_basic_set(__isl_take isl_aff
*aff
)
2192 aff
= isl_aff_neg(aff
);
2193 return isl_aff_pos_basic_set(aff
);
2196 /* Return a basic set containing those elements in the space
2197 * of aff where it is zero.
2198 * If "rational" is set, then return a rational basic set.
2200 * If "aff" is NaN, then it is not zero.
2202 static __isl_give isl_basic_set
*aff_zero_basic_set(__isl_take isl_aff
*aff
,
2205 isl_constraint
*ineq
;
2206 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 ineq
= isl_equality_from_aff(aff
);
2218 bset
= isl_basic_set_from_constraint(ineq
);
2220 bset
= isl_basic_set_set_rational(bset
);
2221 bset
= isl_basic_set_simplify(bset
);
2225 /* Return a basic set containing those elements in the space
2226 * of aff where it is zero.
2228 __isl_give isl_basic_set
*isl_aff_zero_basic_set(__isl_take isl_aff
*aff
)
2230 return aff_zero_basic_set(aff
, 0);
2233 /* Return a basic set containing those elements in the shared space
2234 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
2236 __isl_give isl_basic_set
*isl_aff_ge_basic_set(__isl_take isl_aff
*aff1
,
2237 __isl_take isl_aff
*aff2
)
2239 aff1
= isl_aff_sub(aff1
, aff2
);
2241 return isl_aff_nonneg_basic_set(aff1
);
2244 /* Return a basic set containing those elements in the shared domain space
2245 * of "aff1" and "aff2" where "aff1" is greater than "aff2".
2247 __isl_give isl_basic_set
*isl_aff_gt_basic_set(__isl_take isl_aff
*aff1
,
2248 __isl_take isl_aff
*aff2
)
2250 aff1
= isl_aff_sub(aff1
, aff2
);
2252 return isl_aff_pos_basic_set(aff1
);
2255 /* Return a set containing those elements in the shared space
2256 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
2258 __isl_give isl_set
*isl_aff_ge_set(__isl_take isl_aff
*aff1
,
2259 __isl_take isl_aff
*aff2
)
2261 return isl_set_from_basic_set(isl_aff_ge_basic_set(aff1
, aff2
));
2264 /* Return a set containing those elements in the shared domain space
2265 * of aff1 and aff2 where aff1 is greater than aff2.
2267 * If either of the two inputs is NaN, then the result is empty,
2268 * as comparisons with NaN always return false.
2270 __isl_give isl_set
*isl_aff_gt_set(__isl_take isl_aff
*aff1
,
2271 __isl_take isl_aff
*aff2
)
2273 return isl_set_from_basic_set(isl_aff_gt_basic_set(aff1
, aff2
));
2276 /* Return a basic set containing those elements in the shared space
2277 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
2279 __isl_give isl_basic_set
*isl_aff_le_basic_set(__isl_take isl_aff
*aff1
,
2280 __isl_take isl_aff
*aff2
)
2282 return isl_aff_ge_basic_set(aff2
, aff1
);
2285 /* Return a basic set containing those elements in the shared domain space
2286 * of "aff1" and "aff2" where "aff1" is smaller than "aff2".
2288 __isl_give isl_basic_set
*isl_aff_lt_basic_set(__isl_take isl_aff
*aff1
,
2289 __isl_take isl_aff
*aff2
)
2291 return isl_aff_gt_basic_set(aff2
, aff1
);
2294 /* Return a set containing those elements in the shared space
2295 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
2297 __isl_give isl_set
*isl_aff_le_set(__isl_take isl_aff
*aff1
,
2298 __isl_take isl_aff
*aff2
)
2300 return isl_aff_ge_set(aff2
, aff1
);
2303 /* Return a set containing those elements in the shared domain space
2304 * of "aff1" and "aff2" where "aff1" is smaller than "aff2".
2306 __isl_give isl_set
*isl_aff_lt_set(__isl_take isl_aff
*aff1
,
2307 __isl_take isl_aff
*aff2
)
2309 return isl_set_from_basic_set(isl_aff_lt_basic_set(aff1
, aff2
));
2312 /* Return a basic set containing those elements in the shared space
2313 * of aff1 and aff2 where aff1 and aff2 are equal.
2315 __isl_give isl_basic_set
*isl_aff_eq_basic_set(__isl_take isl_aff
*aff1
,
2316 __isl_take isl_aff
*aff2
)
2318 aff1
= isl_aff_sub(aff1
, aff2
);
2320 return isl_aff_zero_basic_set(aff1
);
2323 /* Return a set containing those elements in the shared space
2324 * of aff1 and aff2 where aff1 and aff2 are equal.
2326 __isl_give isl_set
*isl_aff_eq_set(__isl_take isl_aff
*aff1
,
2327 __isl_take isl_aff
*aff2
)
2329 return isl_set_from_basic_set(isl_aff_eq_basic_set(aff1
, aff2
));
2332 /* Return a set containing those elements in the shared domain space
2333 * of aff1 and aff2 where aff1 and aff2 are not equal.
2335 * If either of the two inputs is NaN, then the result is empty,
2336 * as comparisons with NaN always return false.
2338 __isl_give isl_set
*isl_aff_ne_set(__isl_take isl_aff
*aff1
,
2339 __isl_take isl_aff
*aff2
)
2341 isl_set
*set_lt
, *set_gt
;
2343 set_lt
= isl_aff_lt_set(isl_aff_copy(aff1
),
2344 isl_aff_copy(aff2
));
2345 set_gt
= isl_aff_gt_set(aff1
, aff2
);
2346 return isl_set_union_disjoint(set_lt
, set_gt
);
2349 __isl_give isl_aff
*isl_aff_add_on_domain(__isl_keep isl_set
*dom
,
2350 __isl_take isl_aff
*aff1
, __isl_take isl_aff
*aff2
)
2352 aff1
= isl_aff_add(aff1
, aff2
);
2353 aff1
= isl_aff_gist(aff1
, isl_set_copy(dom
));
2357 int isl_aff_is_empty(__isl_keep isl_aff
*aff
)
2365 /* Check whether the given affine expression has non-zero coefficient
2366 * for any dimension in the given range or if any of these dimensions
2367 * appear with non-zero coefficients in any of the integer divisions
2368 * involved in the affine expression.
2370 isl_bool
isl_aff_involves_dims(__isl_keep isl_aff
*aff
,
2371 enum isl_dim_type type
, unsigned first
, unsigned n
)
2376 isl_bool involves
= isl_bool_false
;
2379 return isl_bool_error
;
2381 return isl_bool_false
;
2383 ctx
= isl_aff_get_ctx(aff
);
2384 if (first
+ n
> isl_aff_dim(aff
, type
))
2385 isl_die(ctx
, isl_error_invalid
,
2386 "range out of bounds", return isl_bool_error
);
2388 active
= isl_local_space_get_active(aff
->ls
, aff
->v
->el
+ 2);
2392 first
+= isl_local_space_offset(aff
->ls
, type
) - 1;
2393 for (i
= 0; i
< n
; ++i
)
2394 if (active
[first
+ i
]) {
2395 involves
= isl_bool_true
;
2404 return isl_bool_error
;
2407 __isl_give isl_aff
*isl_aff_drop_dims(__isl_take isl_aff
*aff
,
2408 enum isl_dim_type type
, unsigned first
, unsigned n
)
2414 if (type
== isl_dim_out
)
2415 isl_die(aff
->v
->ctx
, isl_error_invalid
,
2416 "cannot drop output/set dimension",
2417 return isl_aff_free(aff
));
2418 if (type
== isl_dim_in
)
2420 if (n
== 0 && !isl_local_space_is_named_or_nested(aff
->ls
, type
))
2423 ctx
= isl_aff_get_ctx(aff
);
2424 if (isl_local_space_check_range(aff
->ls
, type
, first
, n
) < 0)
2425 return isl_aff_free(aff
);
2427 aff
= isl_aff_cow(aff
);
2431 aff
->ls
= isl_local_space_drop_dims(aff
->ls
, type
, first
, n
);
2433 return isl_aff_free(aff
);
2435 first
+= 1 + isl_local_space_offset(aff
->ls
, type
);
2436 aff
->v
= isl_vec_drop_els(aff
->v
, first
, n
);
2438 return isl_aff_free(aff
);
2443 /* Drop the "n" domain dimensions starting at "first" from "aff",
2444 * after checking that they do not appear in the affine expression.
2446 static __isl_give isl_aff
*drop_domain(__isl_take isl_aff
*aff
, unsigned first
,
2451 involves
= isl_aff_involves_dims(aff
, isl_dim_in
, first
, n
);
2453 return isl_aff_free(aff
);
2455 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2456 "affine expression involves some of the domain dimensions",
2457 return isl_aff_free(aff
));
2458 return isl_aff_drop_dims(aff
, isl_dim_in
, first
, n
);
2461 /* Project the domain of the affine expression onto its parameter space.
2462 * The affine expression may not involve any of the domain dimensions.
2464 __isl_give isl_aff
*isl_aff_project_domain_on_params(__isl_take isl_aff
*aff
)
2469 n
= isl_aff_dim(aff
, isl_dim_in
);
2470 aff
= drop_domain(aff
, 0, n
);
2471 space
= isl_aff_get_domain_space(aff
);
2472 space
= isl_space_params(space
);
2473 aff
= isl_aff_reset_domain_space(aff
, space
);
2477 /* Check that the domain of "aff" is a product.
2479 static isl_stat
check_domain_product(__isl_keep isl_aff
*aff
)
2481 isl_bool is_product
;
2483 is_product
= isl_space_is_product(isl_aff_peek_domain_space(aff
));
2485 return isl_stat_error
;
2487 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2488 "domain is not a product", return isl_stat_error
);
2492 /* Given an affine function with a domain of the form [A -> B] that
2493 * does not depend on B, return the same function on domain A.
2495 __isl_give isl_aff
*isl_aff_domain_factor_domain(__isl_take isl_aff
*aff
)
2500 if (check_domain_product(aff
) < 0)
2501 return isl_aff_free(aff
);
2502 space
= isl_aff_get_domain_space(aff
);
2503 n
= isl_space_dim(space
, isl_dim_set
);
2504 space
= isl_space_factor_domain(space
);
2505 n_in
= isl_space_dim(space
, isl_dim_set
);
2506 aff
= drop_domain(aff
, n_in
, n
- n_in
);
2507 aff
= isl_aff_reset_domain_space(aff
, space
);
2511 /* Convert an affine expression defined over a parameter domain
2512 * into one that is defined over a zero-dimensional set.
2514 __isl_give isl_aff
*isl_aff_from_range(__isl_take isl_aff
*aff
)
2516 isl_local_space
*ls
;
2518 ls
= isl_aff_take_domain_local_space(aff
);
2519 ls
= isl_local_space_set_from_params(ls
);
2520 aff
= isl_aff_restore_domain_local_space(aff
, ls
);
2525 __isl_give isl_aff
*isl_aff_insert_dims(__isl_take isl_aff
*aff
,
2526 enum isl_dim_type type
, unsigned first
, unsigned n
)
2532 if (type
== isl_dim_out
)
2533 isl_die(aff
->v
->ctx
, isl_error_invalid
,
2534 "cannot insert output/set dimensions",
2535 return isl_aff_free(aff
));
2536 if (type
== isl_dim_in
)
2538 if (n
== 0 && !isl_local_space_is_named_or_nested(aff
->ls
, type
))
2541 ctx
= isl_aff_get_ctx(aff
);
2542 if (isl_local_space_check_range(aff
->ls
, type
, first
, 0) < 0)
2543 return isl_aff_free(aff
);
2545 aff
= isl_aff_cow(aff
);
2549 aff
->ls
= isl_local_space_insert_dims(aff
->ls
, type
, first
, n
);
2551 return isl_aff_free(aff
);
2553 first
+= 1 + isl_local_space_offset(aff
->ls
, type
);
2554 aff
->v
= isl_vec_insert_zero_els(aff
->v
, first
, n
);
2556 return isl_aff_free(aff
);
2561 __isl_give isl_aff
*isl_aff_add_dims(__isl_take isl_aff
*aff
,
2562 enum isl_dim_type type
, unsigned n
)
2566 pos
= isl_aff_dim(aff
, type
);
2568 return isl_aff_insert_dims(aff
, type
, pos
, n
);
2571 __isl_give isl_pw_aff
*isl_pw_aff_add_dims(__isl_take isl_pw_aff
*pwaff
,
2572 enum isl_dim_type type
, unsigned n
)
2576 pos
= isl_pw_aff_dim(pwaff
, type
);
2578 return isl_pw_aff_insert_dims(pwaff
, type
, pos
, n
);
2581 /* Move the "n" dimensions of "src_type" starting at "src_pos" of "aff"
2582 * to dimensions of "dst_type" at "dst_pos".
2584 * We only support moving input dimensions to parameters and vice versa.
2586 __isl_give isl_aff
*isl_aff_move_dims(__isl_take isl_aff
*aff
,
2587 enum isl_dim_type dst_type
, unsigned dst_pos
,
2588 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
2596 !isl_local_space_is_named_or_nested(aff
->ls
, src_type
) &&
2597 !isl_local_space_is_named_or_nested(aff
->ls
, dst_type
))
2600 if (dst_type
== isl_dim_out
|| src_type
== isl_dim_out
)
2601 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2602 "cannot move output/set dimension",
2603 return isl_aff_free(aff
));
2604 if (dst_type
== isl_dim_div
|| src_type
== isl_dim_div
)
2605 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2606 "cannot move divs", return isl_aff_free(aff
));
2607 if (dst_type
== isl_dim_in
)
2608 dst_type
= isl_dim_set
;
2609 if (src_type
== isl_dim_in
)
2610 src_type
= isl_dim_set
;
2612 if (isl_local_space_check_range(aff
->ls
, src_type
, src_pos
, n
) < 0)
2613 return isl_aff_free(aff
);
2614 if (dst_type
== src_type
)
2615 isl_die(isl_aff_get_ctx(aff
), isl_error_unsupported
,
2616 "moving dims within the same type not supported",
2617 return isl_aff_free(aff
));
2619 aff
= isl_aff_cow(aff
);
2623 g_src_pos
= 1 + isl_local_space_offset(aff
->ls
, src_type
) + src_pos
;
2624 g_dst_pos
= 1 + isl_local_space_offset(aff
->ls
, dst_type
) + dst_pos
;
2625 if (dst_type
> src_type
)
2628 aff
->v
= isl_vec_move_els(aff
->v
, g_dst_pos
, g_src_pos
, n
);
2629 aff
->ls
= isl_local_space_move_dims(aff
->ls
, dst_type
, dst_pos
,
2630 src_type
, src_pos
, n
);
2631 if (!aff
->v
|| !aff
->ls
)
2632 return isl_aff_free(aff
);
2634 aff
= sort_divs(aff
);
2639 __isl_give isl_pw_aff
*isl_pw_aff_from_aff(__isl_take isl_aff
*aff
)
2641 isl_set
*dom
= isl_set_universe(isl_aff_get_domain_space(aff
));
2642 return isl_pw_aff_alloc(dom
, aff
);
2645 #define isl_aff_involves_nan isl_aff_is_nan
2648 #define PW isl_pw_aff
2652 #define EL_IS_ZERO is_empty
2656 #define IS_ZERO is_empty
2659 #undef DEFAULT_IS_ZERO
2660 #define DEFAULT_IS_ZERO 0
2666 #include <isl_pw_templ.c>
2667 #include <isl_pw_eval.c>
2668 #include <isl_pw_hash.c>
2669 #include <isl_pw_union_opt.c>
2674 #include <isl_union_single.c>
2675 #include <isl_union_neg.c>
2677 static __isl_give isl_set
*align_params_pw_pw_set_and(
2678 __isl_take isl_pw_aff
*pwaff1
, __isl_take isl_pw_aff
*pwaff2
,
2679 __isl_give isl_set
*(*fn
)(__isl_take isl_pw_aff
*pwaff1
,
2680 __isl_take isl_pw_aff
*pwaff2
))
2682 isl_bool equal_params
;
2684 if (!pwaff1
|| !pwaff2
)
2686 equal_params
= isl_space_has_equal_params(pwaff1
->dim
, pwaff2
->dim
);
2687 if (equal_params
< 0)
2690 return fn(pwaff1
, pwaff2
);
2691 if (isl_pw_aff_check_named_params(pwaff1
) < 0 ||
2692 isl_pw_aff_check_named_params(pwaff2
) < 0)
2694 pwaff1
= isl_pw_aff_align_params(pwaff1
, isl_pw_aff_get_space(pwaff2
));
2695 pwaff2
= isl_pw_aff_align_params(pwaff2
, isl_pw_aff_get_space(pwaff1
));
2696 return fn(pwaff1
, pwaff2
);
2698 isl_pw_aff_free(pwaff1
);
2699 isl_pw_aff_free(pwaff2
);
2703 /* Align the parameters of the to isl_pw_aff arguments and
2704 * then apply a function "fn" on them that returns an isl_map.
2706 static __isl_give isl_map
*align_params_pw_pw_map_and(
2707 __isl_take isl_pw_aff
*pa1
, __isl_take isl_pw_aff
*pa2
,
2708 __isl_give isl_map
*(*fn
)(__isl_take isl_pw_aff
*pa1
,
2709 __isl_take isl_pw_aff
*pa2
))
2711 isl_bool equal_params
;
2715 equal_params
= isl_space_has_equal_params(pa1
->dim
, pa2
->dim
);
2716 if (equal_params
< 0)
2719 return fn(pa1
, pa2
);
2720 if (isl_pw_aff_check_named_params(pa1
) < 0 ||
2721 isl_pw_aff_check_named_params(pa2
) < 0)
2723 pa1
= isl_pw_aff_align_params(pa1
, isl_pw_aff_get_space(pa2
));
2724 pa2
= isl_pw_aff_align_params(pa2
, isl_pw_aff_get_space(pa1
));
2725 return fn(pa1
, pa2
);
2727 isl_pw_aff_free(pa1
);
2728 isl_pw_aff_free(pa2
);
2732 /* Compute a piecewise quasi-affine expression with a domain that
2733 * is the union of those of pwaff1 and pwaff2 and such that on each
2734 * cell, the quasi-affine expression is the maximum of those of pwaff1
2735 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2736 * cell, then the associated expression is the defined one.
2738 static __isl_give isl_pw_aff
*pw_aff_union_max(__isl_take isl_pw_aff
*pwaff1
,
2739 __isl_take isl_pw_aff
*pwaff2
)
2741 return isl_pw_aff_union_opt_cmp(pwaff1
, pwaff2
, &isl_aff_ge_set
);
2744 __isl_give isl_pw_aff
*isl_pw_aff_union_max(__isl_take isl_pw_aff
*pwaff1
,
2745 __isl_take isl_pw_aff
*pwaff2
)
2747 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
,
2751 /* Compute a piecewise quasi-affine expression with a domain that
2752 * is the union of those of pwaff1 and pwaff2 and such that on each
2753 * cell, the quasi-affine expression is the minimum of those of pwaff1
2754 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2755 * cell, then the associated expression is the defined one.
2757 static __isl_give isl_pw_aff
*pw_aff_union_min(__isl_take isl_pw_aff
*pwaff1
,
2758 __isl_take isl_pw_aff
*pwaff2
)
2760 return isl_pw_aff_union_opt_cmp(pwaff1
, pwaff2
, &isl_aff_le_set
);
2763 __isl_give isl_pw_aff
*isl_pw_aff_union_min(__isl_take isl_pw_aff
*pwaff1
,
2764 __isl_take isl_pw_aff
*pwaff2
)
2766 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
,
2770 __isl_give isl_pw_aff
*isl_pw_aff_union_opt(__isl_take isl_pw_aff
*pwaff1
,
2771 __isl_take isl_pw_aff
*pwaff2
, int max
)
2774 return isl_pw_aff_union_max(pwaff1
, pwaff2
);
2776 return isl_pw_aff_union_min(pwaff1
, pwaff2
);
2779 /* Return a set containing those elements in the domain
2780 * of "pwaff" where it satisfies "fn" (if complement is 0) or
2781 * does not satisfy "fn" (if complement is 1).
2783 * The pieces with a NaN never belong to the result since
2784 * NaN does not satisfy any property.
2786 static __isl_give isl_set
*pw_aff_locus(__isl_take isl_pw_aff
*pwaff
,
2787 __isl_give isl_basic_set
*(*fn
)(__isl_take isl_aff
*aff
, int rational
),
2796 set
= isl_set_empty(isl_pw_aff_get_domain_space(pwaff
));
2798 for (i
= 0; i
< pwaff
->n
; ++i
) {
2799 isl_basic_set
*bset
;
2800 isl_set
*set_i
, *locus
;
2803 if (isl_aff_is_nan(pwaff
->p
[i
].aff
))
2806 rational
= isl_set_has_rational(pwaff
->p
[i
].set
);
2807 bset
= fn(isl_aff_copy(pwaff
->p
[i
].aff
), rational
);
2808 locus
= isl_set_from_basic_set(bset
);
2809 set_i
= isl_set_copy(pwaff
->p
[i
].set
);
2811 set_i
= isl_set_subtract(set_i
, locus
);
2813 set_i
= isl_set_intersect(set_i
, locus
);
2814 set
= isl_set_union_disjoint(set
, set_i
);
2817 isl_pw_aff_free(pwaff
);
2822 /* Return a set containing those elements in the domain
2823 * of "pa" where it is positive.
2825 __isl_give isl_set
*isl_pw_aff_pos_set(__isl_take isl_pw_aff
*pa
)
2827 return pw_aff_locus(pa
, &aff_pos_basic_set
, 0);
2830 /* Return a set containing those elements in the domain
2831 * of pwaff where it is non-negative.
2833 __isl_give isl_set
*isl_pw_aff_nonneg_set(__isl_take isl_pw_aff
*pwaff
)
2835 return pw_aff_locus(pwaff
, &aff_nonneg_basic_set
, 0);
2838 /* Return a set containing those elements in the domain
2839 * of pwaff where it is zero.
2841 __isl_give isl_set
*isl_pw_aff_zero_set(__isl_take isl_pw_aff
*pwaff
)
2843 return pw_aff_locus(pwaff
, &aff_zero_basic_set
, 0);
2846 /* Return a set containing those elements in the domain
2847 * of pwaff where it is not zero.
2849 __isl_give isl_set
*isl_pw_aff_non_zero_set(__isl_take isl_pw_aff
*pwaff
)
2851 return pw_aff_locus(pwaff
, &aff_zero_basic_set
, 1);
2854 /* Return a set containing those elements in the shared domain
2855 * of pwaff1 and pwaff2 where pwaff1 is greater than (or equal) to pwaff2.
2857 * We compute the difference on the shared domain and then construct
2858 * the set of values where this difference is non-negative.
2859 * If strict is set, we first subtract 1 from the difference.
2860 * If equal is set, we only return the elements where pwaff1 and pwaff2
2863 static __isl_give isl_set
*pw_aff_gte_set(__isl_take isl_pw_aff
*pwaff1
,
2864 __isl_take isl_pw_aff
*pwaff2
, int strict
, int equal
)
2866 isl_set
*set1
, *set2
;
2868 set1
= isl_pw_aff_domain(isl_pw_aff_copy(pwaff1
));
2869 set2
= isl_pw_aff_domain(isl_pw_aff_copy(pwaff2
));
2870 set1
= isl_set_intersect(set1
, set2
);
2871 pwaff1
= isl_pw_aff_intersect_domain(pwaff1
, isl_set_copy(set1
));
2872 pwaff2
= isl_pw_aff_intersect_domain(pwaff2
, isl_set_copy(set1
));
2873 pwaff1
= isl_pw_aff_add(pwaff1
, isl_pw_aff_neg(pwaff2
));
2876 isl_space
*space
= isl_set_get_space(set1
);
2878 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(space
));
2879 aff
= isl_aff_add_constant_si(aff
, -1);
2880 pwaff1
= isl_pw_aff_add(pwaff1
, isl_pw_aff_alloc(set1
, aff
));
2885 return isl_pw_aff_zero_set(pwaff1
);
2886 return isl_pw_aff_nonneg_set(pwaff1
);
2889 /* Return a set containing those elements in the shared domain
2890 * of pwaff1 and pwaff2 where pwaff1 is equal to pwaff2.
2892 static __isl_give isl_set
*pw_aff_eq_set(__isl_take isl_pw_aff
*pwaff1
,
2893 __isl_take isl_pw_aff
*pwaff2
)
2895 return pw_aff_gte_set(pwaff1
, pwaff2
, 0, 1);
2898 __isl_give isl_set
*isl_pw_aff_eq_set(__isl_take isl_pw_aff
*pwaff1
,
2899 __isl_take isl_pw_aff
*pwaff2
)
2901 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_eq_set
);
2904 /* Return a set containing those elements in the shared domain
2905 * of pwaff1 and pwaff2 where pwaff1 is greater than or equal to pwaff2.
2907 static __isl_give isl_set
*pw_aff_ge_set(__isl_take isl_pw_aff
*pwaff1
,
2908 __isl_take isl_pw_aff
*pwaff2
)
2910 return pw_aff_gte_set(pwaff1
, pwaff2
, 0, 0);
2913 __isl_give isl_set
*isl_pw_aff_ge_set(__isl_take isl_pw_aff
*pwaff1
,
2914 __isl_take isl_pw_aff
*pwaff2
)
2916 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_ge_set
);
2919 /* Return a set containing those elements in the shared domain
2920 * of pwaff1 and pwaff2 where pwaff1 is strictly greater than pwaff2.
2922 static __isl_give isl_set
*pw_aff_gt_set(__isl_take isl_pw_aff
*pwaff1
,
2923 __isl_take isl_pw_aff
*pwaff2
)
2925 return pw_aff_gte_set(pwaff1
, pwaff2
, 1, 0);
2928 __isl_give isl_set
*isl_pw_aff_gt_set(__isl_take isl_pw_aff
*pwaff1
,
2929 __isl_take isl_pw_aff
*pwaff2
)
2931 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_gt_set
);
2934 __isl_give isl_set
*isl_pw_aff_le_set(__isl_take isl_pw_aff
*pwaff1
,
2935 __isl_take isl_pw_aff
*pwaff2
)
2937 return isl_pw_aff_ge_set(pwaff2
, pwaff1
);
2940 __isl_give isl_set
*isl_pw_aff_lt_set(__isl_take isl_pw_aff
*pwaff1
,
2941 __isl_take isl_pw_aff
*pwaff2
)
2943 return isl_pw_aff_gt_set(pwaff2
, pwaff1
);
2946 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
2947 * where the function values are ordered in the same way as "order",
2948 * which returns a set in the shared domain of its two arguments.
2949 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
2951 * Let "pa1" and "pa2" be defined on domains A and B respectively.
2952 * We first pull back the two functions such that they are defined on
2953 * the domain [A -> B]. Then we apply "order", resulting in a set
2954 * in the space [A -> B]. Finally, we unwrap this set to obtain
2955 * a map in the space A -> B.
2957 static __isl_give isl_map
*isl_pw_aff_order_map_aligned(
2958 __isl_take isl_pw_aff
*pa1
, __isl_take isl_pw_aff
*pa2
,
2959 __isl_give isl_set
*(*order
)(__isl_take isl_pw_aff
*pa1
,
2960 __isl_take isl_pw_aff
*pa2
))
2962 isl_space
*space1
, *space2
;
2966 space1
= isl_space_domain(isl_pw_aff_get_space(pa1
));
2967 space2
= isl_space_domain(isl_pw_aff_get_space(pa2
));
2968 space1
= isl_space_map_from_domain_and_range(space1
, space2
);
2969 ma
= isl_multi_aff_domain_map(isl_space_copy(space1
));
2970 pa1
= isl_pw_aff_pullback_multi_aff(pa1
, ma
);
2971 ma
= isl_multi_aff_range_map(space1
);
2972 pa2
= isl_pw_aff_pullback_multi_aff(pa2
, ma
);
2973 set
= order(pa1
, pa2
);
2975 return isl_set_unwrap(set
);
2978 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
2979 * where the function values are equal.
2980 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
2982 static __isl_give isl_map
*isl_pw_aff_eq_map_aligned(__isl_take isl_pw_aff
*pa1
,
2983 __isl_take isl_pw_aff
*pa2
)
2985 return isl_pw_aff_order_map_aligned(pa1
, pa2
, &isl_pw_aff_eq_set
);
2988 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
2989 * where the function values are equal.
2991 __isl_give isl_map
*isl_pw_aff_eq_map(__isl_take isl_pw_aff
*pa1
,
2992 __isl_take isl_pw_aff
*pa2
)
2994 return align_params_pw_pw_map_and(pa1
, pa2
, &isl_pw_aff_eq_map_aligned
);
2997 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
2998 * where the function value of "pa1" is less than the function value of "pa2".
2999 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3001 static __isl_give isl_map
*isl_pw_aff_lt_map_aligned(__isl_take isl_pw_aff
*pa1
,
3002 __isl_take isl_pw_aff
*pa2
)
3004 return isl_pw_aff_order_map_aligned(pa1
, pa2
, &isl_pw_aff_lt_set
);
3007 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3008 * where the function value of "pa1" is less than the function value of "pa2".
3010 __isl_give isl_map
*isl_pw_aff_lt_map(__isl_take isl_pw_aff
*pa1
,
3011 __isl_take isl_pw_aff
*pa2
)
3013 return align_params_pw_pw_map_and(pa1
, pa2
, &isl_pw_aff_lt_map_aligned
);
3016 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3017 * where the function value of "pa1" is greater than the function value
3019 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3021 static __isl_give isl_map
*isl_pw_aff_gt_map_aligned(__isl_take isl_pw_aff
*pa1
,
3022 __isl_take isl_pw_aff
*pa2
)
3024 return isl_pw_aff_order_map_aligned(pa1
, pa2
, &isl_pw_aff_gt_set
);
3027 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3028 * where the function value of "pa1" is greater than the function value
3031 __isl_give isl_map
*isl_pw_aff_gt_map(__isl_take isl_pw_aff
*pa1
,
3032 __isl_take isl_pw_aff
*pa2
)
3034 return align_params_pw_pw_map_and(pa1
, pa2
, &isl_pw_aff_gt_map_aligned
);
3037 /* Return a set containing those elements in the shared domain
3038 * of the elements of list1 and list2 where each element in list1
3039 * has the relation specified by "fn" with each element in list2.
3041 static __isl_give isl_set
*pw_aff_list_set(__isl_take isl_pw_aff_list
*list1
,
3042 __isl_take isl_pw_aff_list
*list2
,
3043 __isl_give isl_set
*(*fn
)(__isl_take isl_pw_aff
*pwaff1
,
3044 __isl_take isl_pw_aff
*pwaff2
))
3050 if (!list1
|| !list2
)
3053 ctx
= isl_pw_aff_list_get_ctx(list1
);
3054 if (list1
->n
< 1 || list2
->n
< 1)
3055 isl_die(ctx
, isl_error_invalid
,
3056 "list should contain at least one element", goto error
);
3058 set
= isl_set_universe(isl_pw_aff_get_domain_space(list1
->p
[0]));
3059 for (i
= 0; i
< list1
->n
; ++i
)
3060 for (j
= 0; j
< list2
->n
; ++j
) {
3063 set_ij
= fn(isl_pw_aff_copy(list1
->p
[i
]),
3064 isl_pw_aff_copy(list2
->p
[j
]));
3065 set
= isl_set_intersect(set
, set_ij
);
3068 isl_pw_aff_list_free(list1
);
3069 isl_pw_aff_list_free(list2
);
3072 isl_pw_aff_list_free(list1
);
3073 isl_pw_aff_list_free(list2
);
3077 /* Return a set containing those elements in the shared domain
3078 * of the elements of list1 and list2 where each element in list1
3079 * is equal to each element in list2.
3081 __isl_give isl_set
*isl_pw_aff_list_eq_set(__isl_take isl_pw_aff_list
*list1
,
3082 __isl_take isl_pw_aff_list
*list2
)
3084 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_eq_set
);
3087 __isl_give isl_set
*isl_pw_aff_list_ne_set(__isl_take isl_pw_aff_list
*list1
,
3088 __isl_take isl_pw_aff_list
*list2
)
3090 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_ne_set
);
3093 /* Return a set containing those elements in the shared domain
3094 * of the elements of list1 and list2 where each element in list1
3095 * is less than or equal to each element in list2.
3097 __isl_give isl_set
*isl_pw_aff_list_le_set(__isl_take isl_pw_aff_list
*list1
,
3098 __isl_take isl_pw_aff_list
*list2
)
3100 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_le_set
);
3103 __isl_give isl_set
*isl_pw_aff_list_lt_set(__isl_take isl_pw_aff_list
*list1
,
3104 __isl_take isl_pw_aff_list
*list2
)
3106 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_lt_set
);
3109 __isl_give isl_set
*isl_pw_aff_list_ge_set(__isl_take isl_pw_aff_list
*list1
,
3110 __isl_take isl_pw_aff_list
*list2
)
3112 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_ge_set
);
3115 __isl_give isl_set
*isl_pw_aff_list_gt_set(__isl_take isl_pw_aff_list
*list1
,
3116 __isl_take isl_pw_aff_list
*list2
)
3118 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_gt_set
);
3122 /* Return a set containing those elements in the shared domain
3123 * of pwaff1 and pwaff2 where pwaff1 is not equal to pwaff2.
3125 static __isl_give isl_set
*pw_aff_ne_set(__isl_take isl_pw_aff
*pwaff1
,
3126 __isl_take isl_pw_aff
*pwaff2
)
3128 isl_set
*set_lt
, *set_gt
;
3130 set_lt
= isl_pw_aff_lt_set(isl_pw_aff_copy(pwaff1
),
3131 isl_pw_aff_copy(pwaff2
));
3132 set_gt
= isl_pw_aff_gt_set(pwaff1
, pwaff2
);
3133 return isl_set_union_disjoint(set_lt
, set_gt
);
3136 __isl_give isl_set
*isl_pw_aff_ne_set(__isl_take isl_pw_aff
*pwaff1
,
3137 __isl_take isl_pw_aff
*pwaff2
)
3139 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_ne_set
);
3142 __isl_give isl_pw_aff
*isl_pw_aff_scale_down(__isl_take isl_pw_aff
*pwaff
,
3147 if (isl_int_is_one(v
))
3149 if (!isl_int_is_pos(v
))
3150 isl_die(isl_pw_aff_get_ctx(pwaff
), isl_error_invalid
,
3151 "factor needs to be positive",
3152 return isl_pw_aff_free(pwaff
));
3153 pwaff
= isl_pw_aff_cow(pwaff
);
3159 for (i
= 0; i
< pwaff
->n
; ++i
) {
3160 pwaff
->p
[i
].aff
= isl_aff_scale_down(pwaff
->p
[i
].aff
, v
);
3161 if (!pwaff
->p
[i
].aff
)
3162 return isl_pw_aff_free(pwaff
);
3168 __isl_give isl_pw_aff
*isl_pw_aff_floor(__isl_take isl_pw_aff
*pwaff
)
3172 pwaff
= isl_pw_aff_cow(pwaff
);
3178 for (i
= 0; i
< pwaff
->n
; ++i
) {
3179 pwaff
->p
[i
].aff
= isl_aff_floor(pwaff
->p
[i
].aff
);
3180 if (!pwaff
->p
[i
].aff
)
3181 return isl_pw_aff_free(pwaff
);
3187 __isl_give isl_pw_aff
*isl_pw_aff_ceil(__isl_take isl_pw_aff
*pwaff
)
3191 pwaff
= isl_pw_aff_cow(pwaff
);
3197 for (i
= 0; i
< pwaff
->n
; ++i
) {
3198 pwaff
->p
[i
].aff
= isl_aff_ceil(pwaff
->p
[i
].aff
);
3199 if (!pwaff
->p
[i
].aff
)
3200 return isl_pw_aff_free(pwaff
);
3206 /* Assuming that "cond1" and "cond2" are disjoint,
3207 * return an affine expression that is equal to pwaff1 on cond1
3208 * and to pwaff2 on cond2.
3210 static __isl_give isl_pw_aff
*isl_pw_aff_select(
3211 __isl_take isl_set
*cond1
, __isl_take isl_pw_aff
*pwaff1
,
3212 __isl_take isl_set
*cond2
, __isl_take isl_pw_aff
*pwaff2
)
3214 pwaff1
= isl_pw_aff_intersect_domain(pwaff1
, cond1
);
3215 pwaff2
= isl_pw_aff_intersect_domain(pwaff2
, cond2
);
3217 return isl_pw_aff_add_disjoint(pwaff1
, pwaff2
);
3220 /* Return an affine expression that is equal to pwaff_true for elements
3221 * where "cond" is non-zero and to pwaff_false for elements where "cond"
3223 * That is, return cond ? pwaff_true : pwaff_false;
3225 * If "cond" involves and NaN, then we conservatively return a NaN
3226 * on its entire domain. In principle, we could consider the pieces
3227 * where it is NaN separately from those where it is not.
3229 * If "pwaff_true" and "pwaff_false" are obviously equal to each other,
3230 * then only use the domain of "cond" to restrict the domain.
3232 __isl_give isl_pw_aff
*isl_pw_aff_cond(__isl_take isl_pw_aff
*cond
,
3233 __isl_take isl_pw_aff
*pwaff_true
, __isl_take isl_pw_aff
*pwaff_false
)
3235 isl_set
*cond_true
, *cond_false
;
3240 if (isl_pw_aff_involves_nan(cond
)) {
3241 isl_space
*space
= isl_pw_aff_get_domain_space(cond
);
3242 isl_local_space
*ls
= isl_local_space_from_space(space
);
3243 isl_pw_aff_free(cond
);
3244 isl_pw_aff_free(pwaff_true
);
3245 isl_pw_aff_free(pwaff_false
);
3246 return isl_pw_aff_nan_on_domain(ls
);
3249 pwaff_true
= isl_pw_aff_align_params(pwaff_true
,
3250 isl_pw_aff_get_space(pwaff_false
));
3251 pwaff_false
= isl_pw_aff_align_params(pwaff_false
,
3252 isl_pw_aff_get_space(pwaff_true
));
3253 equal
= isl_pw_aff_plain_is_equal(pwaff_true
, pwaff_false
);
3259 dom
= isl_set_coalesce(isl_pw_aff_domain(cond
));
3260 isl_pw_aff_free(pwaff_false
);
3261 return isl_pw_aff_intersect_domain(pwaff_true
, dom
);
3264 cond_true
= isl_pw_aff_non_zero_set(isl_pw_aff_copy(cond
));
3265 cond_false
= isl_pw_aff_zero_set(cond
);
3266 return isl_pw_aff_select(cond_true
, pwaff_true
,
3267 cond_false
, pwaff_false
);
3269 isl_pw_aff_free(cond
);
3270 isl_pw_aff_free(pwaff_true
);
3271 isl_pw_aff_free(pwaff_false
);
3275 isl_bool
isl_aff_is_cst(__isl_keep isl_aff
*aff
)
3278 return isl_bool_error
;
3280 return isl_seq_first_non_zero(aff
->v
->el
+ 2, aff
->v
->size
- 2) == -1;
3283 /* Check whether pwaff is a piecewise constant.
3285 isl_bool
isl_pw_aff_is_cst(__isl_keep isl_pw_aff
*pwaff
)
3290 return isl_bool_error
;
3292 for (i
= 0; i
< pwaff
->n
; ++i
) {
3293 isl_bool is_cst
= isl_aff_is_cst(pwaff
->p
[i
].aff
);
3294 if (is_cst
< 0 || !is_cst
)
3298 return isl_bool_true
;
3301 /* Are all elements of "mpa" piecewise constants?
3303 isl_bool
isl_multi_pw_aff_is_cst(__isl_keep isl_multi_pw_aff
*mpa
)
3308 return isl_bool_error
;
3310 for (i
= 0; i
< mpa
->n
; ++i
) {
3311 isl_bool is_cst
= isl_pw_aff_is_cst(mpa
->u
.p
[i
]);
3312 if (is_cst
< 0 || !is_cst
)
3316 return isl_bool_true
;
3319 /* Return the product of "aff1" and "aff2".
3321 * If either of the two is NaN, then the result is NaN.
3323 * Otherwise, at least one of "aff1" or "aff2" needs to be a constant.
3325 __isl_give isl_aff
*isl_aff_mul(__isl_take isl_aff
*aff1
,
3326 __isl_take isl_aff
*aff2
)
3331 if (isl_aff_is_nan(aff1
)) {
3335 if (isl_aff_is_nan(aff2
)) {
3340 if (!isl_aff_is_cst(aff2
) && isl_aff_is_cst(aff1
))
3341 return isl_aff_mul(aff2
, aff1
);
3343 if (!isl_aff_is_cst(aff2
))
3344 isl_die(isl_aff_get_ctx(aff1
), isl_error_invalid
,
3345 "at least one affine expression should be constant",
3348 aff1
= isl_aff_cow(aff1
);
3352 aff1
= isl_aff_scale(aff1
, aff2
->v
->el
[1]);
3353 aff1
= isl_aff_scale_down(aff1
, aff2
->v
->el
[0]);
3363 /* Divide "aff1" by "aff2", assuming "aff2" is a constant.
3365 * If either of the two is NaN, then the result is NaN.
3367 __isl_give isl_aff
*isl_aff_div(__isl_take isl_aff
*aff1
,
3368 __isl_take isl_aff
*aff2
)
3376 if (isl_aff_is_nan(aff1
)) {
3380 if (isl_aff_is_nan(aff2
)) {
3385 is_cst
= isl_aff_is_cst(aff2
);
3389 isl_die(isl_aff_get_ctx(aff2
), isl_error_invalid
,
3390 "second argument should be a constant", goto error
);
3395 neg
= isl_int_is_neg(aff2
->v
->el
[1]);
3397 isl_int_neg(aff2
->v
->el
[0], aff2
->v
->el
[0]);
3398 isl_int_neg(aff2
->v
->el
[1], aff2
->v
->el
[1]);
3401 aff1
= isl_aff_scale(aff1
, aff2
->v
->el
[0]);
3402 aff1
= isl_aff_scale_down(aff1
, aff2
->v
->el
[1]);
3405 isl_int_neg(aff2
->v
->el
[0], aff2
->v
->el
[0]);
3406 isl_int_neg(aff2
->v
->el
[1], aff2
->v
->el
[1]);
3417 static __isl_give isl_pw_aff
*pw_aff_add(__isl_take isl_pw_aff
*pwaff1
,
3418 __isl_take isl_pw_aff
*pwaff2
)
3420 return isl_pw_aff_on_shared_domain(pwaff1
, pwaff2
, &isl_aff_add
);
3423 __isl_give isl_pw_aff
*isl_pw_aff_add(__isl_take isl_pw_aff
*pwaff1
,
3424 __isl_take isl_pw_aff
*pwaff2
)
3426 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
, &pw_aff_add
);
3429 __isl_give isl_pw_aff
*isl_pw_aff_union_add(__isl_take isl_pw_aff
*pwaff1
,
3430 __isl_take isl_pw_aff
*pwaff2
)
3432 return isl_pw_aff_union_add_(pwaff1
, pwaff2
);
3435 static __isl_give isl_pw_aff
*pw_aff_mul(__isl_take isl_pw_aff
*pwaff1
,
3436 __isl_take isl_pw_aff
*pwaff2
)
3438 return isl_pw_aff_on_shared_domain(pwaff1
, pwaff2
, &isl_aff_mul
);
3441 __isl_give isl_pw_aff
*isl_pw_aff_mul(__isl_take isl_pw_aff
*pwaff1
,
3442 __isl_take isl_pw_aff
*pwaff2
)
3444 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
, &pw_aff_mul
);
3447 static __isl_give isl_pw_aff
*pw_aff_div(__isl_take isl_pw_aff
*pa1
,
3448 __isl_take isl_pw_aff
*pa2
)
3450 return isl_pw_aff_on_shared_domain(pa1
, pa2
, &isl_aff_div
);
3453 /* Divide "pa1" by "pa2", assuming "pa2" is a piecewise constant.
3455 __isl_give isl_pw_aff
*isl_pw_aff_div(__isl_take isl_pw_aff
*pa1
,
3456 __isl_take isl_pw_aff
*pa2
)
3460 is_cst
= isl_pw_aff_is_cst(pa2
);
3464 isl_die(isl_pw_aff_get_ctx(pa2
), isl_error_invalid
,
3465 "second argument should be a piecewise constant",
3467 return isl_pw_aff_align_params_pw_pw_and(pa1
, pa2
, &pw_aff_div
);
3469 isl_pw_aff_free(pa1
);
3470 isl_pw_aff_free(pa2
);
3474 /* Compute the quotient of the integer division of "pa1" by "pa2"
3475 * with rounding towards zero.
3476 * "pa2" is assumed to be a piecewise constant.
3478 * In particular, return
3480 * pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2)
3483 __isl_give isl_pw_aff
*isl_pw_aff_tdiv_q(__isl_take isl_pw_aff
*pa1
,
3484 __isl_take isl_pw_aff
*pa2
)
3490 is_cst
= isl_pw_aff_is_cst(pa2
);
3494 isl_die(isl_pw_aff_get_ctx(pa2
), isl_error_invalid
,
3495 "second argument should be a piecewise constant",
3498 pa1
= isl_pw_aff_div(pa1
, pa2
);
3500 cond
= isl_pw_aff_nonneg_set(isl_pw_aff_copy(pa1
));
3501 f
= isl_pw_aff_floor(isl_pw_aff_copy(pa1
));
3502 c
= isl_pw_aff_ceil(pa1
);
3503 return isl_pw_aff_cond(isl_set_indicator_function(cond
), f
, c
);
3505 isl_pw_aff_free(pa1
);
3506 isl_pw_aff_free(pa2
);
3510 /* Compute the remainder of the integer division of "pa1" by "pa2"
3511 * with rounding towards zero.
3512 * "pa2" is assumed to be a piecewise constant.
3514 * In particular, return
3516 * pa1 - pa2 * (pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2))
3519 __isl_give isl_pw_aff
*isl_pw_aff_tdiv_r(__isl_take isl_pw_aff
*pa1
,
3520 __isl_take isl_pw_aff
*pa2
)
3525 is_cst
= isl_pw_aff_is_cst(pa2
);
3529 isl_die(isl_pw_aff_get_ctx(pa2
), isl_error_invalid
,
3530 "second argument should be a piecewise constant",
3532 res
= isl_pw_aff_tdiv_q(isl_pw_aff_copy(pa1
), isl_pw_aff_copy(pa2
));
3533 res
= isl_pw_aff_mul(pa2
, res
);
3534 res
= isl_pw_aff_sub(pa1
, res
);
3537 isl_pw_aff_free(pa1
);
3538 isl_pw_aff_free(pa2
);
3542 /* Does either of "pa1" or "pa2" involve any NaN2?
3544 static isl_bool
either_involves_nan(__isl_keep isl_pw_aff
*pa1
,
3545 __isl_keep isl_pw_aff
*pa2
)
3549 has_nan
= isl_pw_aff_involves_nan(pa1
);
3550 if (has_nan
< 0 || has_nan
)
3552 return isl_pw_aff_involves_nan(pa2
);
3555 /* Replace "pa1" and "pa2" (at least one of which involves a NaN)
3556 * by a NaN on their shared domain.
3558 * In principle, the result could be refined to only being NaN
3559 * on the parts of this domain where at least one of "pa1" or "pa2" is NaN.
3561 static __isl_give isl_pw_aff
*replace_by_nan(__isl_take isl_pw_aff
*pa1
,
3562 __isl_take isl_pw_aff
*pa2
)
3564 isl_local_space
*ls
;
3568 dom
= isl_set_intersect(isl_pw_aff_domain(pa1
), isl_pw_aff_domain(pa2
));
3569 ls
= isl_local_space_from_space(isl_set_get_space(dom
));
3570 pa
= isl_pw_aff_nan_on_domain(ls
);
3571 pa
= isl_pw_aff_intersect_domain(pa
, dom
);
3576 static __isl_give isl_pw_aff
*pw_aff_min(__isl_take isl_pw_aff
*pwaff1
,
3577 __isl_take isl_pw_aff
*pwaff2
)
3582 dom
= isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1
)),
3583 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2
)));
3584 le
= isl_pw_aff_le_set(isl_pw_aff_copy(pwaff1
),
3585 isl_pw_aff_copy(pwaff2
));
3586 dom
= isl_set_subtract(dom
, isl_set_copy(le
));
3587 return isl_pw_aff_select(le
, pwaff1
, dom
, pwaff2
);
3590 static __isl_give isl_pw_aff
*pw_aff_max(__isl_take isl_pw_aff
*pwaff1
,
3591 __isl_take isl_pw_aff
*pwaff2
)
3596 dom
= isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1
)),
3597 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2
)));
3598 ge
= isl_pw_aff_ge_set(isl_pw_aff_copy(pwaff1
),
3599 isl_pw_aff_copy(pwaff2
));
3600 dom
= isl_set_subtract(dom
, isl_set_copy(ge
));
3601 return isl_pw_aff_select(ge
, pwaff1
, dom
, pwaff2
);
3604 /* Return an expression for the minimum (if "max" is not set) or
3605 * the maximum (if "max" is set) of "pa1" and "pa2".
3606 * If either expression involves any NaN, then return a NaN
3607 * on the shared domain as result.
3609 static __isl_give isl_pw_aff
*pw_aff_min_max(__isl_take isl_pw_aff
*pa1
,
3610 __isl_take isl_pw_aff
*pa2
, int max
)
3614 has_nan
= either_involves_nan(pa1
, pa2
);
3616 pa1
= isl_pw_aff_free(pa1
);
3618 return replace_by_nan(pa1
, pa2
);
3621 return isl_pw_aff_align_params_pw_pw_and(pa1
, pa2
, &pw_aff_max
);
3623 return isl_pw_aff_align_params_pw_pw_and(pa1
, pa2
, &pw_aff_min
);
3626 /* Return an expression for the minimum of "pwaff1" and "pwaff2".
3628 __isl_give isl_pw_aff
*isl_pw_aff_min(__isl_take isl_pw_aff
*pwaff1
,
3629 __isl_take isl_pw_aff
*pwaff2
)
3631 return pw_aff_min_max(pwaff1
, pwaff2
, 0);
3634 /* Return an expression for the maximum of "pwaff1" and "pwaff2".
3636 __isl_give isl_pw_aff
*isl_pw_aff_max(__isl_take isl_pw_aff
*pwaff1
,
3637 __isl_take isl_pw_aff
*pwaff2
)
3639 return pw_aff_min_max(pwaff1
, pwaff2
, 1);
3642 static __isl_give isl_pw_aff
*pw_aff_list_reduce(
3643 __isl_take isl_pw_aff_list
*list
,
3644 __isl_give isl_pw_aff
*(*fn
)(__isl_take isl_pw_aff
*pwaff1
,
3645 __isl_take isl_pw_aff
*pwaff2
))
3654 ctx
= isl_pw_aff_list_get_ctx(list
);
3656 isl_die(ctx
, isl_error_invalid
,
3657 "list should contain at least one element", goto error
);
3659 res
= isl_pw_aff_copy(list
->p
[0]);
3660 for (i
= 1; i
< list
->n
; ++i
)
3661 res
= fn(res
, isl_pw_aff_copy(list
->p
[i
]));
3663 isl_pw_aff_list_free(list
);
3666 isl_pw_aff_list_free(list
);
3670 /* Return an isl_pw_aff that maps each element in the intersection of the
3671 * domains of the elements of list to the minimal corresponding affine
3674 __isl_give isl_pw_aff
*isl_pw_aff_list_min(__isl_take isl_pw_aff_list
*list
)
3676 return pw_aff_list_reduce(list
, &isl_pw_aff_min
);
3679 /* Return an isl_pw_aff that maps each element in the intersection of the
3680 * domains of the elements of list to the maximal corresponding affine
3683 __isl_give isl_pw_aff
*isl_pw_aff_list_max(__isl_take isl_pw_aff_list
*list
)
3685 return pw_aff_list_reduce(list
, &isl_pw_aff_max
);
3688 /* Mark the domains of "pwaff" as rational.
3690 __isl_give isl_pw_aff
*isl_pw_aff_set_rational(__isl_take isl_pw_aff
*pwaff
)
3694 pwaff
= isl_pw_aff_cow(pwaff
);
3700 for (i
= 0; i
< pwaff
->n
; ++i
) {
3701 pwaff
->p
[i
].set
= isl_set_set_rational(pwaff
->p
[i
].set
);
3702 if (!pwaff
->p
[i
].set
)
3703 return isl_pw_aff_free(pwaff
);
3709 /* Mark the domains of the elements of "list" as rational.
3711 __isl_give isl_pw_aff_list
*isl_pw_aff_list_set_rational(
3712 __isl_take isl_pw_aff_list
*list
)
3722 for (i
= 0; i
< n
; ++i
) {
3725 pa
= isl_pw_aff_list_get_pw_aff(list
, i
);
3726 pa
= isl_pw_aff_set_rational(pa
);
3727 list
= isl_pw_aff_list_set_pw_aff(list
, i
, pa
);
3733 /* Do the parameters of "aff" match those of "space"?
3735 isl_bool
isl_aff_matching_params(__isl_keep isl_aff
*aff
,
3736 __isl_keep isl_space
*space
)
3738 isl_space
*aff_space
;
3742 return isl_bool_error
;
3744 aff_space
= isl_aff_get_domain_space(aff
);
3746 match
= isl_space_has_equal_params(space
, aff_space
);
3748 isl_space_free(aff_space
);
3752 /* Check that the domain space of "aff" matches "space".
3754 isl_stat
isl_aff_check_match_domain_space(__isl_keep isl_aff
*aff
,
3755 __isl_keep isl_space
*space
)
3757 isl_space
*aff_space
;
3761 return isl_stat_error
;
3763 aff_space
= isl_aff_get_domain_space(aff
);
3765 match
= isl_space_has_equal_params(space
, aff_space
);
3769 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
3770 "parameters don't match", goto error
);
3771 match
= isl_space_tuple_is_equal(space
, isl_dim_in
,
3772 aff_space
, isl_dim_set
);
3776 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
3777 "domains don't match", goto error
);
3778 isl_space_free(aff_space
);
3781 isl_space_free(aff_space
);
3782 return isl_stat_error
;
3790 #include <isl_multi_no_explicit_domain.c>
3791 #include <isl_multi_templ.c>
3792 #include <isl_multi_apply_set.c>
3793 #include <isl_multi_cmp.c>
3794 #include <isl_multi_dims.c>
3795 #include <isl_multi_floor.c>
3796 #include <isl_multi_from_base_templ.c>
3797 #include <isl_multi_gist.c>
3798 #include <isl_multi_identity_templ.c>
3799 #include <isl_multi_move_dims_templ.c>
3800 #include <isl_multi_product_templ.c>
3801 #include <isl_multi_splice_templ.c>
3802 #include <isl_multi_zero_templ.c>
3804 /* Construct an isl_multi_aff living in "space" that corresponds
3805 * to the affine transformation matrix "mat".
3807 __isl_give isl_multi_aff
*isl_multi_aff_from_aff_mat(
3808 __isl_take isl_space
*space
, __isl_take isl_mat
*mat
)
3811 isl_local_space
*ls
= NULL
;
3812 isl_multi_aff
*ma
= NULL
;
3813 int n_row
, n_col
, n_out
, total
;
3819 ctx
= isl_mat_get_ctx(mat
);
3821 n_row
= isl_mat_rows(mat
);
3822 n_col
= isl_mat_cols(mat
);
3824 isl_die(ctx
, isl_error_invalid
,
3825 "insufficient number of rows", goto error
);
3827 isl_die(ctx
, isl_error_invalid
,
3828 "insufficient number of columns", goto error
);
3829 n_out
= isl_space_dim(space
, isl_dim_out
);
3830 total
= isl_space_dim(space
, isl_dim_all
);
3831 if (1 + n_out
!= n_row
|| 2 + total
!= n_row
+ n_col
)
3832 isl_die(ctx
, isl_error_invalid
,
3833 "dimension mismatch", goto error
);
3835 ma
= isl_multi_aff_zero(isl_space_copy(space
));
3836 ls
= isl_local_space_from_space(isl_space_domain(space
));
3838 for (i
= 0; i
< n_row
- 1; ++i
) {
3842 v
= isl_vec_alloc(ctx
, 1 + n_col
);
3845 isl_int_set(v
->el
[0], mat
->row
[0][0]);
3846 isl_seq_cpy(v
->el
+ 1, mat
->row
[1 + i
], n_col
);
3847 v
= isl_vec_normalize(v
);
3848 aff
= isl_aff_alloc_vec(isl_local_space_copy(ls
), v
);
3849 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
3852 isl_local_space_free(ls
);
3856 isl_local_space_free(ls
);
3858 isl_multi_aff_free(ma
);
3862 /* Remove any internal structure of the domain of "ma".
3863 * If there is any such internal structure in the input,
3864 * then the name of the corresponding space is also removed.
3866 __isl_give isl_multi_aff
*isl_multi_aff_flatten_domain(
3867 __isl_take isl_multi_aff
*ma
)
3874 if (!ma
->space
->nested
[0])
3877 space
= isl_multi_aff_get_space(ma
);
3878 space
= isl_space_flatten_domain(space
);
3879 ma
= isl_multi_aff_reset_space(ma
, space
);
3884 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
3885 * of the space to its domain.
3887 __isl_give isl_multi_aff
*isl_multi_aff_domain_map(__isl_take isl_space
*space
)
3890 isl_local_space
*ls
;
3895 if (!isl_space_is_map(space
))
3896 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
3897 "not a map space", goto error
);
3899 n_in
= isl_space_dim(space
, isl_dim_in
);
3900 space
= isl_space_domain_map(space
);
3902 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
3904 isl_space_free(space
);
3908 space
= isl_space_domain(space
);
3909 ls
= isl_local_space_from_space(space
);
3910 for (i
= 0; i
< n_in
; ++i
) {
3913 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
3915 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
3917 isl_local_space_free(ls
);
3920 isl_space_free(space
);
3924 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
3925 * of the space to its range.
3927 __isl_give isl_multi_aff
*isl_multi_aff_range_map(__isl_take isl_space
*space
)
3930 isl_local_space
*ls
;
3935 if (!isl_space_is_map(space
))
3936 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
3937 "not a map space", goto error
);
3939 n_in
= isl_space_dim(space
, isl_dim_in
);
3940 n_out
= isl_space_dim(space
, isl_dim_out
);
3941 space
= isl_space_range_map(space
);
3943 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
3945 isl_space_free(space
);
3949 space
= isl_space_domain(space
);
3950 ls
= isl_local_space_from_space(space
);
3951 for (i
= 0; i
< n_out
; ++i
) {
3954 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
3955 isl_dim_set
, n_in
+ i
);
3956 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
3958 isl_local_space_free(ls
);
3961 isl_space_free(space
);
3965 /* Given a map space, return an isl_pw_multi_aff that maps a wrapped copy
3966 * of the space to its range.
3968 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_range_map(
3969 __isl_take isl_space
*space
)
3971 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_range_map(space
));
3974 /* Given the space of a set and a range of set dimensions,
3975 * construct an isl_multi_aff that projects out those dimensions.
3977 __isl_give isl_multi_aff
*isl_multi_aff_project_out_map(
3978 __isl_take isl_space
*space
, enum isl_dim_type type
,
3979 unsigned first
, unsigned n
)
3982 isl_local_space
*ls
;
3987 if (!isl_space_is_set(space
))
3988 isl_die(isl_space_get_ctx(space
), isl_error_unsupported
,
3989 "expecting set space", goto error
);
3990 if (type
!= isl_dim_set
)
3991 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
3992 "only set dimensions can be projected out", goto error
);
3993 if (isl_space_check_range(space
, type
, first
, n
) < 0)
3996 dim
= isl_space_dim(space
, isl_dim_set
);
3998 space
= isl_space_from_domain(space
);
3999 space
= isl_space_add_dims(space
, isl_dim_out
, dim
- n
);
4002 return isl_multi_aff_alloc(space
);
4004 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
4005 space
= isl_space_domain(space
);
4006 ls
= isl_local_space_from_space(space
);
4008 for (i
= 0; i
< first
; ++i
) {
4011 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
4013 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
4016 for (i
= 0; i
< dim
- (first
+ n
); ++i
) {
4019 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
4020 isl_dim_set
, first
+ n
+ i
);
4021 ma
= isl_multi_aff_set_aff(ma
, first
+ i
, aff
);
4024 isl_local_space_free(ls
);
4027 isl_space_free(space
);
4031 /* Given the space of a set and a range of set dimensions,
4032 * construct an isl_pw_multi_aff that projects out those dimensions.
4034 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_project_out_map(
4035 __isl_take isl_space
*space
, enum isl_dim_type type
,
4036 unsigned first
, unsigned n
)
4040 ma
= isl_multi_aff_project_out_map(space
, type
, first
, n
);
4041 return isl_pw_multi_aff_from_multi_aff(ma
);
4044 /* Create an isl_pw_multi_aff with the given isl_multi_aff on a universe
4047 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_multi_aff(
4048 __isl_take isl_multi_aff
*ma
)
4050 isl_set
*dom
= isl_set_universe(isl_multi_aff_get_domain_space(ma
));
4051 return isl_pw_multi_aff_alloc(dom
, ma
);
4054 /* Create a piecewise multi-affine expression in the given space that maps each
4055 * input dimension to the corresponding output dimension.
4057 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_identity(
4058 __isl_take isl_space
*space
)
4060 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_identity(space
));
4063 /* Exploit the equalities in "eq" to simplify the affine expressions.
4065 static __isl_give isl_multi_aff
*isl_multi_aff_substitute_equalities(
4066 __isl_take isl_multi_aff
*maff
, __isl_take isl_basic_set
*eq
)
4070 maff
= isl_multi_aff_cow(maff
);
4074 for (i
= 0; i
< maff
->n
; ++i
) {
4075 maff
->u
.p
[i
] = isl_aff_substitute_equalities(maff
->u
.p
[i
],
4076 isl_basic_set_copy(eq
));
4081 isl_basic_set_free(eq
);
4084 isl_basic_set_free(eq
);
4085 isl_multi_aff_free(maff
);
4089 __isl_give isl_multi_aff
*isl_multi_aff_scale(__isl_take isl_multi_aff
*maff
,
4094 maff
= isl_multi_aff_cow(maff
);
4098 for (i
= 0; i
< maff
->n
; ++i
) {
4099 maff
->u
.p
[i
] = isl_aff_scale(maff
->u
.p
[i
], f
);
4101 return isl_multi_aff_free(maff
);
4107 __isl_give isl_multi_aff
*isl_multi_aff_add_on_domain(__isl_keep isl_set
*dom
,
4108 __isl_take isl_multi_aff
*maff1
, __isl_take isl_multi_aff
*maff2
)
4110 maff1
= isl_multi_aff_add(maff1
, maff2
);
4111 maff1
= isl_multi_aff_gist(maff1
, isl_set_copy(dom
));
4115 int isl_multi_aff_is_empty(__isl_keep isl_multi_aff
*maff
)
4123 /* Return the set of domain elements where "ma1" is lexicographically
4124 * smaller than or equal to "ma2".
4126 __isl_give isl_set
*isl_multi_aff_lex_le_set(__isl_take isl_multi_aff
*ma1
,
4127 __isl_take isl_multi_aff
*ma2
)
4129 return isl_multi_aff_lex_ge_set(ma2
, ma1
);
4132 /* Return the set of domain elements where "ma1" is lexicographically
4133 * smaller than "ma2".
4135 __isl_give isl_set
*isl_multi_aff_lex_lt_set(__isl_take isl_multi_aff
*ma1
,
4136 __isl_take isl_multi_aff
*ma2
)
4138 return isl_multi_aff_lex_gt_set(ma2
, ma1
);
4141 /* Return the set of domain elements where "ma1" and "ma2"
4144 static __isl_give isl_set
*isl_multi_aff_order_set(
4145 __isl_take isl_multi_aff
*ma1
, __isl_take isl_multi_aff
*ma2
,
4146 __isl_give isl_map
*order(__isl_take isl_space
*set_space
))
4149 isl_map
*map1
, *map2
;
4152 map1
= isl_map_from_multi_aff_internal(ma1
);
4153 map2
= isl_map_from_multi_aff_internal(ma2
);
4154 map
= isl_map_range_product(map1
, map2
);
4155 space
= isl_space_range(isl_map_get_space(map
));
4156 space
= isl_space_domain(isl_space_unwrap(space
));
4158 map
= isl_map_intersect_range(map
, isl_map_wrap(ge
));
4160 return isl_map_domain(map
);
4163 /* Return the set of domain elements where "ma1" is lexicographically
4164 * greater than or equal to "ma2".
4166 __isl_give isl_set
*isl_multi_aff_lex_ge_set(__isl_take isl_multi_aff
*ma1
,
4167 __isl_take isl_multi_aff
*ma2
)
4169 return isl_multi_aff_order_set(ma1
, ma2
, &isl_map_lex_ge
);
4172 /* Return the set of domain elements where "ma1" is lexicographically
4173 * greater than "ma2".
4175 __isl_give isl_set
*isl_multi_aff_lex_gt_set(__isl_take isl_multi_aff
*ma1
,
4176 __isl_take isl_multi_aff
*ma2
)
4178 return isl_multi_aff_order_set(ma1
, ma2
, &isl_map_lex_gt
);
4182 #define PW isl_pw_multi_aff
4184 #define EL isl_multi_aff
4186 #define EL_IS_ZERO is_empty
4190 #define IS_ZERO is_empty
4193 #undef DEFAULT_IS_ZERO
4194 #define DEFAULT_IS_ZERO 0
4198 #define NO_INSERT_DIMS
4202 #include <isl_pw_templ.c>
4203 #include <isl_pw_union_opt.c>
4208 #define BASE pw_multi_aff
4210 #include <isl_union_multi.c>
4211 #include <isl_union_neg.c>
4213 static __isl_give isl_pw_multi_aff
*pw_multi_aff_union_lexmax(
4214 __isl_take isl_pw_multi_aff
*pma1
,
4215 __isl_take isl_pw_multi_aff
*pma2
)
4217 return isl_pw_multi_aff_union_opt_cmp(pma1
, pma2
,
4218 &isl_multi_aff_lex_ge_set
);
4221 /* Given two piecewise multi affine expressions, return a piecewise
4222 * multi-affine expression defined on the union of the definition domains
4223 * of the inputs that is equal to the lexicographic maximum of the two
4224 * inputs on each cell. If only one of the two inputs is defined on
4225 * a given cell, then it is considered to be the maximum.
4227 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_union_lexmax(
4228 __isl_take isl_pw_multi_aff
*pma1
,
4229 __isl_take isl_pw_multi_aff
*pma2
)
4231 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
4232 &pw_multi_aff_union_lexmax
);
4235 static __isl_give isl_pw_multi_aff
*pw_multi_aff_union_lexmin(
4236 __isl_take isl_pw_multi_aff
*pma1
,
4237 __isl_take isl_pw_multi_aff
*pma2
)
4239 return isl_pw_multi_aff_union_opt_cmp(pma1
, pma2
,
4240 &isl_multi_aff_lex_le_set
);
4243 /* Given two piecewise multi affine expressions, return a piecewise
4244 * multi-affine expression defined on the union of the definition domains
4245 * of the inputs that is equal to the lexicographic minimum of the two
4246 * inputs on each cell. If only one of the two inputs is defined on
4247 * a given cell, then it is considered to be the minimum.
4249 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_union_lexmin(
4250 __isl_take isl_pw_multi_aff
*pma1
,
4251 __isl_take isl_pw_multi_aff
*pma2
)
4253 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
4254 &pw_multi_aff_union_lexmin
);
4257 static __isl_give isl_pw_multi_aff
*pw_multi_aff_add(
4258 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4260 return isl_pw_multi_aff_on_shared_domain(pma1
, pma2
,
4261 &isl_multi_aff_add
);
4264 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_add(
4265 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4267 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
4271 static __isl_give isl_pw_multi_aff
*pw_multi_aff_sub(
4272 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4274 return isl_pw_multi_aff_on_shared_domain(pma1
, pma2
,
4275 &isl_multi_aff_sub
);
4278 /* Subtract "pma2" from "pma1" and return the result.
4280 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_sub(
4281 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4283 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
4287 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_union_add(
4288 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4290 return isl_pw_multi_aff_union_add_(pma1
, pma2
);
4293 /* Compute the sum of "upa1" and "upa2" on the union of their domains,
4294 * with the actual sum on the shared domain and
4295 * the defined expression on the symmetric difference of the domains.
4297 __isl_give isl_union_pw_aff
*isl_union_pw_aff_union_add(
4298 __isl_take isl_union_pw_aff
*upa1
, __isl_take isl_union_pw_aff
*upa2
)
4300 return isl_union_pw_aff_union_add_(upa1
, upa2
);
4303 /* Compute the sum of "upma1" and "upma2" on the union of their domains,
4304 * with the actual sum on the shared domain and
4305 * the defined expression on the symmetric difference of the domains.
4307 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_union_add(
4308 __isl_take isl_union_pw_multi_aff
*upma1
,
4309 __isl_take isl_union_pw_multi_aff
*upma2
)
4311 return isl_union_pw_multi_aff_union_add_(upma1
, upma2
);
4314 /* Given two piecewise multi-affine expressions A -> B and C -> D,
4315 * construct a piecewise multi-affine expression [A -> C] -> [B -> D].
4317 static __isl_give isl_pw_multi_aff
*pw_multi_aff_product(
4318 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4322 isl_pw_multi_aff
*res
;
4327 n
= pma1
->n
* pma2
->n
;
4328 space
= isl_space_product(isl_space_copy(pma1
->dim
),
4329 isl_space_copy(pma2
->dim
));
4330 res
= isl_pw_multi_aff_alloc_size(space
, n
);
4332 for (i
= 0; i
< pma1
->n
; ++i
) {
4333 for (j
= 0; j
< pma2
->n
; ++j
) {
4337 domain
= isl_set_product(isl_set_copy(pma1
->p
[i
].set
),
4338 isl_set_copy(pma2
->p
[j
].set
));
4339 ma
= isl_multi_aff_product(
4340 isl_multi_aff_copy(pma1
->p
[i
].maff
),
4341 isl_multi_aff_copy(pma2
->p
[j
].maff
));
4342 res
= isl_pw_multi_aff_add_piece(res
, domain
, ma
);
4346 isl_pw_multi_aff_free(pma1
);
4347 isl_pw_multi_aff_free(pma2
);
4350 isl_pw_multi_aff_free(pma1
);
4351 isl_pw_multi_aff_free(pma2
);
4355 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_product(
4356 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4358 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
4359 &pw_multi_aff_product
);
4362 /* Subtract the initial "n" elements in "ma" with coefficients in "c" and
4363 * denominator "denom".
4364 * "denom" is allowed to be negative, in which case the actual denominator
4365 * is -denom and the expressions are added instead.
4367 static __isl_give isl_aff
*subtract_initial(__isl_take isl_aff
*aff
,
4368 __isl_keep isl_multi_aff
*ma
, int n
, isl_int
*c
, isl_int denom
)
4374 first
= isl_seq_first_non_zero(c
, n
);
4378 sign
= isl_int_sgn(denom
);
4380 isl_int_abs(d
, denom
);
4381 for (i
= first
; i
< n
; ++i
) {
4384 if (isl_int_is_zero(c
[i
]))
4386 aff_i
= isl_multi_aff_get_aff(ma
, i
);
4387 aff_i
= isl_aff_scale(aff_i
, c
[i
]);
4388 aff_i
= isl_aff_scale_down(aff_i
, d
);
4390 aff
= isl_aff_sub(aff
, aff_i
);
4392 aff
= isl_aff_add(aff
, aff_i
);
4399 /* Extract an affine expression that expresses the output dimension "pos"
4400 * of "bmap" in terms of the parameters and input dimensions from
4402 * Note that this expression may involve integer divisions defined
4403 * in terms of parameters and input dimensions.
4404 * The equality may also involve references to earlier (but not later)
4405 * output dimensions. These are replaced by the corresponding elements
4408 * If the equality is of the form
4410 * f(i) + h(j) + a x + g(i) = 0,
4412 * with f(i) a linear combinations of the parameters and input dimensions,
4413 * g(i) a linear combination of integer divisions defined in terms of the same
4414 * and h(j) a linear combinations of earlier output dimensions,
4415 * then the affine expression is
4417 * (-f(i) - g(i))/a - h(j)/a
4419 * If the equality is of the form
4421 * f(i) + h(j) - a x + g(i) = 0,
4423 * then the affine expression is
4425 * (f(i) + g(i))/a - h(j)/(-a)
4428 * If "div" refers to an integer division (i.e., it is smaller than
4429 * the number of integer divisions), then the equality constraint
4430 * does involve an integer division (the one at position "div") that
4431 * is defined in terms of output dimensions. However, this integer
4432 * division can be eliminated by exploiting a pair of constraints
4433 * x >= l and x <= l + n, with n smaller than the coefficient of "div"
4434 * in the equality constraint. "ineq" refers to inequality x >= l, i.e.,
4436 * In particular, let
4438 * x = e(i) + m floor(...)
4440 * with e(i) the expression derived above and floor(...) the integer
4441 * division involving output dimensions.
4452 * e(i) + m floor(...) - l = (e(i) + m floor(...) - l) mod m
4453 * = (e(i) - l) mod m
4457 * x - l = (e(i) - l) mod m
4461 * x = ((e(i) - l) mod m) + l
4463 * The variable "shift" below contains the expression -l, which may
4464 * also involve a linear combination of earlier output dimensions.
4466 static __isl_give isl_aff
*extract_aff_from_equality(
4467 __isl_keep isl_basic_map
*bmap
, int pos
, int eq
, int div
, int ineq
,
4468 __isl_keep isl_multi_aff
*ma
)
4471 unsigned n_div
, n_out
;
4473 isl_local_space
*ls
;
4474 isl_aff
*aff
, *shift
;
4477 ctx
= isl_basic_map_get_ctx(bmap
);
4478 ls
= isl_basic_map_get_local_space(bmap
);
4479 ls
= isl_local_space_domain(ls
);
4480 aff
= isl_aff_alloc(isl_local_space_copy(ls
));
4483 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
4484 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4485 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4486 if (isl_int_is_neg(bmap
->eq
[eq
][o_out
+ pos
])) {
4487 isl_seq_cpy(aff
->v
->el
+ 1, bmap
->eq
[eq
], o_out
);
4488 isl_seq_cpy(aff
->v
->el
+ 1 + o_out
,
4489 bmap
->eq
[eq
] + o_out
+ n_out
, n_div
);
4491 isl_seq_neg(aff
->v
->el
+ 1, bmap
->eq
[eq
], o_out
);
4492 isl_seq_neg(aff
->v
->el
+ 1 + o_out
,
4493 bmap
->eq
[eq
] + o_out
+ n_out
, n_div
);
4496 isl_int_set_si(aff
->v
->el
[1 + o_out
+ div
], 0);
4497 isl_int_abs(aff
->v
->el
[0], bmap
->eq
[eq
][o_out
+ pos
]);
4498 aff
= subtract_initial(aff
, ma
, pos
, bmap
->eq
[eq
] + o_out
,
4499 bmap
->eq
[eq
][o_out
+ pos
]);
4501 shift
= isl_aff_alloc(isl_local_space_copy(ls
));
4504 isl_seq_cpy(shift
->v
->el
+ 1, bmap
->ineq
[ineq
], o_out
);
4505 isl_seq_cpy(shift
->v
->el
+ 1 + o_out
,
4506 bmap
->ineq
[ineq
] + o_out
+ n_out
, n_div
);
4507 isl_int_set_si(shift
->v
->el
[0], 1);
4508 shift
= subtract_initial(shift
, ma
, pos
,
4509 bmap
->ineq
[ineq
] + o_out
, ctx
->negone
);
4510 aff
= isl_aff_add(aff
, isl_aff_copy(shift
));
4511 mod
= isl_val_int_from_isl_int(ctx
,
4512 bmap
->eq
[eq
][o_out
+ n_out
+ div
]);
4513 mod
= isl_val_abs(mod
);
4514 aff
= isl_aff_mod_val(aff
, mod
);
4515 aff
= isl_aff_sub(aff
, shift
);
4518 isl_local_space_free(ls
);
4521 isl_local_space_free(ls
);
4526 /* Given a basic map with output dimensions defined
4527 * in terms of the parameters input dimensions and earlier
4528 * output dimensions using an equality (and possibly a pair on inequalities),
4529 * extract an isl_aff that expresses output dimension "pos" in terms
4530 * of the parameters and input dimensions.
4531 * Note that this expression may involve integer divisions defined
4532 * in terms of parameters and input dimensions.
4533 * "ma" contains the expressions corresponding to earlier output dimensions.
4535 * This function shares some similarities with
4536 * isl_basic_map_has_defining_equality and isl_constraint_get_bound.
4538 static __isl_give isl_aff
*extract_isl_aff_from_basic_map(
4539 __isl_keep isl_basic_map
*bmap
, int pos
, __isl_keep isl_multi_aff
*ma
)
4546 eq
= isl_basic_map_output_defining_equality(bmap
, pos
, &div
, &ineq
);
4547 if (eq
>= bmap
->n_eq
)
4548 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
4549 "unable to find suitable equality", return NULL
);
4550 aff
= extract_aff_from_equality(bmap
, pos
, eq
, div
, ineq
, ma
);
4552 aff
= isl_aff_remove_unused_divs(aff
);
4556 /* Given a basic map where each output dimension is defined
4557 * in terms of the parameters and input dimensions using an equality,
4558 * extract an isl_multi_aff that expresses the output dimensions in terms
4559 * of the parameters and input dimensions.
4561 static __isl_give isl_multi_aff
*extract_isl_multi_aff_from_basic_map(
4562 __isl_take isl_basic_map
*bmap
)
4571 ma
= isl_multi_aff_alloc(isl_basic_map_get_space(bmap
));
4572 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4574 for (i
= 0; i
< n_out
; ++i
) {
4577 aff
= extract_isl_aff_from_basic_map(bmap
, i
, ma
);
4578 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
4581 isl_basic_map_free(bmap
);
4586 /* Given a basic set where each set dimension is defined
4587 * in terms of the parameters using an equality,
4588 * extract an isl_multi_aff that expresses the set dimensions in terms
4589 * of the parameters.
4591 __isl_give isl_multi_aff
*isl_multi_aff_from_basic_set_equalities(
4592 __isl_take isl_basic_set
*bset
)
4594 return extract_isl_multi_aff_from_basic_map(bset
);
4597 /* Create an isl_pw_multi_aff that is equivalent to
4598 * isl_map_intersect_domain(isl_map_from_basic_map(bmap), domain).
4599 * The given basic map is such that each output dimension is defined
4600 * in terms of the parameters and input dimensions using an equality.
4602 * Since some applications expect the result of isl_pw_multi_aff_from_map
4603 * to only contain integer affine expressions, we compute the floor
4604 * of the expression before returning.
4606 * Remove all constraints involving local variables without
4607 * an explicit representation (resulting in the removal of those
4608 * local variables) prior to the actual extraction to ensure
4609 * that the local spaces in which the resulting affine expressions
4610 * are created do not contain any unknown local variables.
4611 * Removing such constraints is safe because constraints involving
4612 * unknown local variables are not used to determine whether
4613 * a basic map is obviously single-valued.
4615 static __isl_give isl_pw_multi_aff
*plain_pw_multi_aff_from_map(
4616 __isl_take isl_set
*domain
, __isl_take isl_basic_map
*bmap
)
4620 bmap
= isl_basic_map_drop_constraint_involving_unknown_divs(bmap
);
4621 ma
= extract_isl_multi_aff_from_basic_map(bmap
);
4622 ma
= isl_multi_aff_floor(ma
);
4623 return isl_pw_multi_aff_alloc(domain
, ma
);
4626 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4627 * This obviously only works if the input "map" is single-valued.
4628 * If so, we compute the lexicographic minimum of the image in the form
4629 * of an isl_pw_multi_aff. Since the image is unique, it is equal
4630 * to its lexicographic minimum.
4631 * If the input is not single-valued, we produce an error.
4633 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_base(
4634 __isl_take isl_map
*map
)
4638 isl_pw_multi_aff
*pma
;
4640 sv
= isl_map_is_single_valued(map
);
4644 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
4645 "map is not single-valued", goto error
);
4646 map
= isl_map_make_disjoint(map
);
4650 pma
= isl_pw_multi_aff_empty(isl_map_get_space(map
));
4652 for (i
= 0; i
< map
->n
; ++i
) {
4653 isl_pw_multi_aff
*pma_i
;
4654 isl_basic_map
*bmap
;
4655 bmap
= isl_basic_map_copy(map
->p
[i
]);
4656 pma_i
= isl_basic_map_lexmin_pw_multi_aff(bmap
);
4657 pma
= isl_pw_multi_aff_add_disjoint(pma
, pma_i
);
4667 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
4668 * taking into account that the output dimension at position "d"
4669 * can be represented as
4671 * x = floor((e(...) + c1) / m)
4673 * given that constraint "i" is of the form
4675 * e(...) + c1 - m x >= 0
4678 * Let "map" be of the form
4682 * We construct a mapping
4684 * A -> [A -> x = floor(...)]
4686 * apply that to the map, obtaining
4688 * [A -> x = floor(...)] -> B
4690 * and equate dimension "d" to x.
4691 * We then compute a isl_pw_multi_aff representation of the resulting map
4692 * and plug in the mapping above.
4694 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_div(
4695 __isl_take isl_map
*map
, __isl_take isl_basic_map
*hull
, int d
, int i
)
4699 isl_local_space
*ls
;
4707 isl_pw_multi_aff
*pma
;
4710 is_set
= isl_map_is_set(map
);
4714 offset
= isl_basic_map_offset(hull
, isl_dim_out
);
4715 ctx
= isl_map_get_ctx(map
);
4716 space
= isl_space_domain(isl_map_get_space(map
));
4717 n_in
= isl_space_dim(space
, isl_dim_set
);
4718 n
= isl_space_dim(space
, isl_dim_all
);
4720 v
= isl_vec_alloc(ctx
, 1 + 1 + n
);
4722 isl_int_neg(v
->el
[0], hull
->ineq
[i
][offset
+ d
]);
4723 isl_seq_cpy(v
->el
+ 1, hull
->ineq
[i
], 1 + n
);
4725 isl_basic_map_free(hull
);
4727 ls
= isl_local_space_from_space(isl_space_copy(space
));
4728 aff
= isl_aff_alloc_vec(ls
, v
);
4729 aff
= isl_aff_floor(aff
);
4731 isl_space_free(space
);
4732 ma
= isl_multi_aff_from_aff(aff
);
4734 ma
= isl_multi_aff_identity(isl_space_map_from_set(space
));
4735 ma
= isl_multi_aff_range_product(ma
,
4736 isl_multi_aff_from_aff(aff
));
4739 insert
= isl_map_from_multi_aff_internal(isl_multi_aff_copy(ma
));
4740 map
= isl_map_apply_domain(map
, insert
);
4741 map
= isl_map_equate(map
, isl_dim_in
, n_in
, isl_dim_out
, d
);
4742 pma
= isl_pw_multi_aff_from_map(map
);
4743 pma
= isl_pw_multi_aff_pullback_multi_aff(pma
, ma
);
4748 isl_basic_map_free(hull
);
4752 /* Is constraint "c" of the form
4754 * e(...) + c1 - m x >= 0
4758 * -e(...) + c2 + m x >= 0
4760 * where m > 1 and e only depends on parameters and input dimemnsions?
4762 * "offset" is the offset of the output dimensions
4763 * "pos" is the position of output dimension x.
4765 static int is_potential_div_constraint(isl_int
*c
, int offset
, int d
, int total
)
4767 if (isl_int_is_zero(c
[offset
+ d
]))
4769 if (isl_int_is_one(c
[offset
+ d
]))
4771 if (isl_int_is_negone(c
[offset
+ d
]))
4773 if (isl_seq_first_non_zero(c
+ offset
, d
) != -1)
4775 if (isl_seq_first_non_zero(c
+ offset
+ d
+ 1,
4776 total
- (offset
+ d
+ 1)) != -1)
4781 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4783 * As a special case, we first check if there is any pair of constraints,
4784 * shared by all the basic maps in "map" that force a given dimension
4785 * to be equal to the floor of some affine combination of the input dimensions.
4787 * In particular, if we can find two constraints
4789 * e(...) + c1 - m x >= 0 i.e., m x <= e(...) + c1
4793 * -e(...) + c2 + m x >= 0 i.e., m x >= e(...) - c2
4795 * where m > 1 and e only depends on parameters and input dimemnsions,
4798 * c1 + c2 < m i.e., -c2 >= c1 - (m - 1)
4800 * then we know that we can take
4802 * x = floor((e(...) + c1) / m)
4804 * without having to perform any computation.
4806 * Note that we know that
4810 * If c1 + c2 were 0, then we would have detected an equality during
4811 * simplification. If c1 + c2 were negative, then we would have detected
4814 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_check_div(
4815 __isl_take isl_map
*map
)
4821 isl_basic_map
*hull
;
4823 hull
= isl_map_unshifted_simple_hull(isl_map_copy(map
));
4828 dim
= isl_map_dim(map
, isl_dim_out
);
4829 offset
= isl_basic_map_offset(hull
, isl_dim_out
);
4830 total
= 1 + isl_basic_map_total_dim(hull
);
4832 for (d
= 0; d
< dim
; ++d
) {
4833 for (i
= 0; i
< n
; ++i
) {
4834 if (!is_potential_div_constraint(hull
->ineq
[i
],
4837 for (j
= i
+ 1; j
< n
; ++j
) {
4838 if (!isl_seq_is_neg(hull
->ineq
[i
] + 1,
4839 hull
->ineq
[j
] + 1, total
- 1))
4841 isl_int_add(sum
, hull
->ineq
[i
][0],
4843 if (isl_int_abs_lt(sum
,
4844 hull
->ineq
[i
][offset
+ d
]))
4851 if (isl_int_is_pos(hull
->ineq
[j
][offset
+ d
]))
4853 return pw_multi_aff_from_map_div(map
, hull
, d
, j
);
4857 isl_basic_map_free(hull
);
4858 return pw_multi_aff_from_map_base(map
);
4861 isl_basic_map_free(hull
);
4865 /* Given an affine expression
4867 * [A -> B] -> f(A,B)
4869 * construct an isl_multi_aff
4873 * such that dimension "d" in B' is set to "aff" and the remaining
4874 * dimensions are set equal to the corresponding dimensions in B.
4875 * "n_in" is the dimension of the space A.
4876 * "n_out" is the dimension of the space B.
4878 * If "is_set" is set, then the affine expression is of the form
4882 * and we construct an isl_multi_aff
4886 static __isl_give isl_multi_aff
*range_map(__isl_take isl_aff
*aff
, int d
,
4887 unsigned n_in
, unsigned n_out
, int is_set
)
4891 isl_space
*space
, *space2
;
4892 isl_local_space
*ls
;
4894 space
= isl_aff_get_domain_space(aff
);
4895 ls
= isl_local_space_from_space(isl_space_copy(space
));
4896 space2
= isl_space_copy(space
);
4898 space2
= isl_space_range(isl_space_unwrap(space2
));
4899 space
= isl_space_map_from_domain_and_range(space
, space2
);
4900 ma
= isl_multi_aff_alloc(space
);
4901 ma
= isl_multi_aff_set_aff(ma
, d
, aff
);
4903 for (i
= 0; i
< n_out
; ++i
) {
4906 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
4907 isl_dim_set
, n_in
+ i
);
4908 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
4911 isl_local_space_free(ls
);
4916 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
4917 * taking into account that the dimension at position "d" can be written as
4919 * x = m a + f(..) (1)
4921 * where m is equal to "gcd".
4922 * "i" is the index of the equality in "hull" that defines f(..).
4923 * In particular, the equality is of the form
4925 * f(..) - x + m g(existentials) = 0
4929 * -f(..) + x + m g(existentials) = 0
4931 * We basically plug (1) into "map", resulting in a map with "a"
4932 * in the range instead of "x". The corresponding isl_pw_multi_aff
4933 * defining "a" is then plugged back into (1) to obtain a definition for "x".
4935 * Specifically, given the input map
4939 * We first wrap it into a set
4943 * and define (1) on top of the corresponding space, resulting in "aff".
4944 * We use this to create an isl_multi_aff that maps the output position "d"
4945 * from "a" to "x", leaving all other (intput and output) dimensions unchanged.
4946 * We plug this into the wrapped map, unwrap the result and compute the
4947 * corresponding isl_pw_multi_aff.
4948 * The result is an expression
4956 * so that we can plug that into "aff", after extending the latter to
4962 * If "map" is actually a set, then there is no "A" space, meaning
4963 * that we do not need to perform any wrapping, and that the result
4964 * of the recursive call is of the form
4968 * which is plugged into a mapping of the form
4972 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_stride(
4973 __isl_take isl_map
*map
, __isl_take isl_basic_map
*hull
, int d
, int i
,
4978 isl_local_space
*ls
;
4981 isl_pw_multi_aff
*pma
, *id
;
4987 is_set
= isl_map_is_set(map
);
4991 n_in
= isl_basic_map_dim(hull
, isl_dim_in
);
4992 n_out
= isl_basic_map_dim(hull
, isl_dim_out
);
4993 o_out
= isl_basic_map_offset(hull
, isl_dim_out
);
4998 set
= isl_map_wrap(map
);
4999 space
= isl_space_map_from_set(isl_set_get_space(set
));
5000 ma
= isl_multi_aff_identity(space
);
5001 ls
= isl_local_space_from_space(isl_set_get_space(set
));
5002 aff
= isl_aff_alloc(ls
);
5004 isl_int_set_si(aff
->v
->el
[0], 1);
5005 if (isl_int_is_one(hull
->eq
[i
][o_out
+ d
]))
5006 isl_seq_neg(aff
->v
->el
+ 1, hull
->eq
[i
],
5009 isl_seq_cpy(aff
->v
->el
+ 1, hull
->eq
[i
],
5011 isl_int_set(aff
->v
->el
[1 + o_out
+ d
], gcd
);
5013 ma
= isl_multi_aff_set_aff(ma
, n_in
+ d
, isl_aff_copy(aff
));
5014 set
= isl_set_preimage_multi_aff(set
, ma
);
5016 ma
= range_map(aff
, d
, n_in
, n_out
, is_set
);
5021 map
= isl_set_unwrap(set
);
5022 pma
= isl_pw_multi_aff_from_map(map
);
5025 space
= isl_pw_multi_aff_get_domain_space(pma
);
5026 space
= isl_space_map_from_set(space
);
5027 id
= isl_pw_multi_aff_identity(space
);
5028 pma
= isl_pw_multi_aff_range_product(id
, pma
);
5030 id
= isl_pw_multi_aff_from_multi_aff(ma
);
5031 pma
= isl_pw_multi_aff_pullback_pw_multi_aff(id
, pma
);
5033 isl_basic_map_free(hull
);
5037 isl_basic_map_free(hull
);
5041 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5042 * "hull" contains the equalities valid for "map".
5044 * Check if any of the output dimensions is "strided".
5045 * That is, we check if it can be written as
5049 * with m greater than 1, a some combination of existentially quantified
5050 * variables and f an expression in the parameters and input dimensions.
5051 * If so, we remove the stride in pw_multi_aff_from_map_stride.
5053 * Otherwise, we continue with pw_multi_aff_from_map_check_div for a further
5056 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_check_strides(
5057 __isl_take isl_map
*map
, __isl_take isl_basic_map
*hull
)
5066 n_div
= isl_basic_map_dim(hull
, isl_dim_div
);
5067 o_div
= isl_basic_map_offset(hull
, isl_dim_div
);
5070 isl_basic_map_free(hull
);
5071 return pw_multi_aff_from_map_check_div(map
);
5076 n_out
= isl_basic_map_dim(hull
, isl_dim_out
);
5077 o_out
= isl_basic_map_offset(hull
, isl_dim_out
);
5079 for (i
= 0; i
< n_out
; ++i
) {
5080 for (j
= 0; j
< hull
->n_eq
; ++j
) {
5081 isl_int
*eq
= hull
->eq
[j
];
5082 isl_pw_multi_aff
*res
;
5084 if (!isl_int_is_one(eq
[o_out
+ i
]) &&
5085 !isl_int_is_negone(eq
[o_out
+ i
]))
5087 if (isl_seq_first_non_zero(eq
+ o_out
, i
) != -1)
5089 if (isl_seq_first_non_zero(eq
+ o_out
+ i
+ 1,
5090 n_out
- (i
+ 1)) != -1)
5092 isl_seq_gcd(eq
+ o_div
, n_div
, &gcd
);
5093 if (isl_int_is_zero(gcd
))
5095 if (isl_int_is_one(gcd
))
5098 res
= pw_multi_aff_from_map_stride(map
, hull
,
5106 isl_basic_map_free(hull
);
5107 return pw_multi_aff_from_map_check_div(map
);
5110 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5112 * As a special case, we first check if all output dimensions are uniquely
5113 * defined in terms of the parameters and input dimensions over the entire
5114 * domain. If so, we extract the desired isl_pw_multi_aff directly
5115 * from the affine hull of "map" and its domain.
5117 * Otherwise, continue with pw_multi_aff_from_map_check_strides for more
5120 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_map(__isl_take isl_map
*map
)
5123 isl_basic_map
*hull
;
5128 if (isl_map_n_basic_map(map
) == 1) {
5129 hull
= isl_map_unshifted_simple_hull(isl_map_copy(map
));
5130 hull
= isl_basic_map_plain_affine_hull(hull
);
5131 sv
= isl_basic_map_plain_is_single_valued(hull
);
5133 return plain_pw_multi_aff_from_map(isl_map_domain(map
),
5135 isl_basic_map_free(hull
);
5137 map
= isl_map_detect_equalities(map
);
5138 hull
= isl_map_unshifted_simple_hull(isl_map_copy(map
));
5139 sv
= isl_basic_map_plain_is_single_valued(hull
);
5141 return plain_pw_multi_aff_from_map(isl_map_domain(map
), hull
);
5143 return pw_multi_aff_from_map_check_strides(map
, hull
);
5144 isl_basic_map_free(hull
);
5149 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_set(__isl_take isl_set
*set
)
5151 return isl_pw_multi_aff_from_map(set
);
5154 /* Convert "map" into an isl_pw_multi_aff (if possible) and
5157 static isl_stat
pw_multi_aff_from_map(__isl_take isl_map
*map
, void *user
)
5159 isl_union_pw_multi_aff
**upma
= user
;
5160 isl_pw_multi_aff
*pma
;
5162 pma
= isl_pw_multi_aff_from_map(map
);
5163 *upma
= isl_union_pw_multi_aff_add_pw_multi_aff(*upma
, pma
);
5165 return *upma
? isl_stat_ok
: isl_stat_error
;
5168 /* Create an isl_union_pw_multi_aff with the given isl_aff on a universe
5171 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_aff(
5172 __isl_take isl_aff
*aff
)
5175 isl_pw_multi_aff
*pma
;
5177 ma
= isl_multi_aff_from_aff(aff
);
5178 pma
= isl_pw_multi_aff_from_multi_aff(ma
);
5179 return isl_union_pw_multi_aff_from_pw_multi_aff(pma
);
5182 /* Try and create an isl_union_pw_multi_aff that is equivalent
5183 * to the given isl_union_map.
5184 * The isl_union_map is required to be single-valued in each space.
5185 * Otherwise, an error is produced.
5187 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_union_map(
5188 __isl_take isl_union_map
*umap
)
5191 isl_union_pw_multi_aff
*upma
;
5193 space
= isl_union_map_get_space(umap
);
5194 upma
= isl_union_pw_multi_aff_empty(space
);
5195 if (isl_union_map_foreach_map(umap
, &pw_multi_aff_from_map
, &upma
) < 0)
5196 upma
= isl_union_pw_multi_aff_free(upma
);
5197 isl_union_map_free(umap
);
5202 /* Try and create an isl_union_pw_multi_aff that is equivalent
5203 * to the given isl_union_set.
5204 * The isl_union_set is required to be a singleton in each space.
5205 * Otherwise, an error is produced.
5207 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_union_set(
5208 __isl_take isl_union_set
*uset
)
5210 return isl_union_pw_multi_aff_from_union_map(uset
);
5213 /* Return the piecewise affine expression "set ? 1 : 0".
5215 __isl_give isl_pw_aff
*isl_set_indicator_function(__isl_take isl_set
*set
)
5218 isl_space
*space
= isl_set_get_space(set
);
5219 isl_local_space
*ls
= isl_local_space_from_space(space
);
5220 isl_aff
*zero
= isl_aff_zero_on_domain(isl_local_space_copy(ls
));
5221 isl_aff
*one
= isl_aff_zero_on_domain(ls
);
5223 one
= isl_aff_add_constant_si(one
, 1);
5224 pa
= isl_pw_aff_alloc(isl_set_copy(set
), one
);
5225 set
= isl_set_complement(set
);
5226 pa
= isl_pw_aff_add_disjoint(pa
, isl_pw_aff_alloc(set
, zero
));
5231 /* Plug in "subs" for dimension "type", "pos" of "aff".
5233 * Let i be the dimension to replace and let "subs" be of the form
5237 * and "aff" of the form
5243 * (a f + d g')/(m d)
5245 * where g' is the result of plugging in "subs" in each of the integer
5248 __isl_give isl_aff
*isl_aff_substitute(__isl_take isl_aff
*aff
,
5249 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
5254 aff
= isl_aff_cow(aff
);
5256 return isl_aff_free(aff
);
5258 ctx
= isl_aff_get_ctx(aff
);
5259 if (!isl_space_is_equal(aff
->ls
->dim
, subs
->ls
->dim
))
5260 isl_die(ctx
, isl_error_invalid
,
5261 "spaces don't match", return isl_aff_free(aff
));
5262 if (isl_local_space_dim(subs
->ls
, isl_dim_div
) != 0)
5263 isl_die(ctx
, isl_error_unsupported
,
5264 "cannot handle divs yet", return isl_aff_free(aff
));
5266 aff
->ls
= isl_local_space_substitute(aff
->ls
, type
, pos
, subs
);
5268 return isl_aff_free(aff
);
5270 aff
->v
= isl_vec_cow(aff
->v
);
5272 return isl_aff_free(aff
);
5274 pos
+= isl_local_space_offset(aff
->ls
, type
);
5277 isl_seq_substitute(aff
->v
->el
, pos
, subs
->v
->el
,
5278 aff
->v
->size
, subs
->v
->size
, v
);
5284 /* Plug in "subs" for dimension "type", "pos" in each of the affine
5285 * expressions in "maff".
5287 __isl_give isl_multi_aff
*isl_multi_aff_substitute(
5288 __isl_take isl_multi_aff
*maff
, enum isl_dim_type type
, unsigned pos
,
5289 __isl_keep isl_aff
*subs
)
5293 maff
= isl_multi_aff_cow(maff
);
5295 return isl_multi_aff_free(maff
);
5297 if (type
== isl_dim_in
)
5300 for (i
= 0; i
< maff
->n
; ++i
) {
5301 maff
->u
.p
[i
] = isl_aff_substitute(maff
->u
.p
[i
],
5304 return isl_multi_aff_free(maff
);
5310 /* Plug in "subs" for dimension "type", "pos" of "pma".
5312 * pma is of the form
5316 * while subs is of the form
5318 * v' = B_j(v) -> S_j
5320 * Each pair i,j such that C_ij = A_i \cap B_i is non-empty
5321 * has a contribution in the result, in particular
5323 * C_ij(S_j) -> M_i(S_j)
5325 * Note that plugging in S_j in C_ij may also result in an empty set
5326 * and this contribution should simply be discarded.
5328 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_substitute(
5329 __isl_take isl_pw_multi_aff
*pma
, enum isl_dim_type type
, unsigned pos
,
5330 __isl_keep isl_pw_aff
*subs
)
5333 isl_pw_multi_aff
*res
;
5336 return isl_pw_multi_aff_free(pma
);
5338 n
= pma
->n
* subs
->n
;
5339 res
= isl_pw_multi_aff_alloc_size(isl_space_copy(pma
->dim
), n
);
5341 for (i
= 0; i
< pma
->n
; ++i
) {
5342 for (j
= 0; j
< subs
->n
; ++j
) {
5344 isl_multi_aff
*res_ij
;
5347 common
= isl_set_intersect(
5348 isl_set_copy(pma
->p
[i
].set
),
5349 isl_set_copy(subs
->p
[j
].set
));
5350 common
= isl_set_substitute(common
,
5351 type
, pos
, subs
->p
[j
].aff
);
5352 empty
= isl_set_plain_is_empty(common
);
5353 if (empty
< 0 || empty
) {
5354 isl_set_free(common
);
5360 res_ij
= isl_multi_aff_substitute(
5361 isl_multi_aff_copy(pma
->p
[i
].maff
),
5362 type
, pos
, subs
->p
[j
].aff
);
5364 res
= isl_pw_multi_aff_add_piece(res
, common
, res_ij
);
5368 isl_pw_multi_aff_free(pma
);
5371 isl_pw_multi_aff_free(pma
);
5372 isl_pw_multi_aff_free(res
);
5376 /* Compute the preimage of a range of dimensions in the affine expression "src"
5377 * under "ma" and put the result in "dst". The number of dimensions in "src"
5378 * that precede the range is given by "n_before". The number of dimensions
5379 * in the range is given by the number of output dimensions of "ma".
5380 * The number of dimensions that follow the range is given by "n_after".
5381 * If "has_denom" is set (to one),
5382 * then "src" and "dst" have an extra initial denominator.
5383 * "n_div_ma" is the number of existentials in "ma"
5384 * "n_div_bset" is the number of existentials in "src"
5385 * The resulting "dst" (which is assumed to have been allocated by
5386 * the caller) contains coefficients for both sets of existentials,
5387 * first those in "ma" and then those in "src".
5388 * f, c1, c2 and g are temporary objects that have been initialized
5391 * Let src represent the expression
5393 * (a(p) + f_u u + b v + f_w w + c(divs))/d
5395 * and let ma represent the expressions
5397 * v_i = (r_i(p) + s_i(y) + t_i(divs'))/m_i
5399 * We start out with the following expression for dst:
5401 * (a(p) + f_u u + 0 y + f_w w + 0 divs' + c(divs) + f \sum_i b_i v_i)/d
5403 * with the multiplication factor f initially equal to 1
5404 * and f \sum_i b_i v_i kept separately.
5405 * For each x_i that we substitute, we multiply the numerator
5406 * (and denominator) of dst by c_1 = m_i and add the numerator
5407 * of the x_i expression multiplied by c_2 = f b_i,
5408 * after removing the common factors of c_1 and c_2.
5409 * The multiplication factor f also needs to be multiplied by c_1
5410 * for the next x_j, j > i.
5412 void isl_seq_preimage(isl_int
*dst
, isl_int
*src
,
5413 __isl_keep isl_multi_aff
*ma
, int n_before
, int n_after
,
5414 int n_div_ma
, int n_div_bmap
,
5415 isl_int f
, isl_int c1
, isl_int c2
, isl_int g
, int has_denom
)
5418 int n_param
, n_in
, n_out
;
5421 n_param
= isl_multi_aff_dim(ma
, isl_dim_param
);
5422 n_in
= isl_multi_aff_dim(ma
, isl_dim_in
);
5423 n_out
= isl_multi_aff_dim(ma
, isl_dim_out
);
5425 isl_seq_cpy(dst
, src
, has_denom
+ 1 + n_param
+ n_before
);
5426 o_dst
= o_src
= has_denom
+ 1 + n_param
+ n_before
;
5427 isl_seq_clr(dst
+ o_dst
, n_in
);
5430 isl_seq_cpy(dst
+ o_dst
, src
+ o_src
, n_after
);
5433 isl_seq_clr(dst
+ o_dst
, n_div_ma
);
5435 isl_seq_cpy(dst
+ o_dst
, src
+ o_src
, n_div_bmap
);
5437 isl_int_set_si(f
, 1);
5439 for (i
= 0; i
< n_out
; ++i
) {
5440 int offset
= has_denom
+ 1 + n_param
+ n_before
+ i
;
5442 if (isl_int_is_zero(src
[offset
]))
5444 isl_int_set(c1
, ma
->u
.p
[i
]->v
->el
[0]);
5445 isl_int_mul(c2
, f
, src
[offset
]);
5446 isl_int_gcd(g
, c1
, c2
);
5447 isl_int_divexact(c1
, c1
, g
);
5448 isl_int_divexact(c2
, c2
, g
);
5450 isl_int_mul(f
, f
, c1
);
5453 isl_seq_combine(dst
+ o_dst
, c1
, dst
+ o_dst
,
5454 c2
, ma
->u
.p
[i
]->v
->el
+ o_src
, 1 + n_param
);
5455 o_dst
+= 1 + n_param
;
5456 o_src
+= 1 + n_param
;
5457 isl_seq_scale(dst
+ o_dst
, dst
+ o_dst
, c1
, n_before
);
5459 isl_seq_combine(dst
+ o_dst
, c1
, dst
+ o_dst
,
5460 c2
, ma
->u
.p
[i
]->v
->el
+ o_src
, n_in
);
5463 isl_seq_scale(dst
+ o_dst
, dst
+ o_dst
, c1
, n_after
);
5465 isl_seq_combine(dst
+ o_dst
, c1
, dst
+ o_dst
,
5466 c2
, ma
->u
.p
[i
]->v
->el
+ o_src
, n_div_ma
);
5469 isl_seq_scale(dst
+ o_dst
, dst
+ o_dst
, c1
, n_div_bmap
);
5471 isl_int_mul(dst
[0], dst
[0], c1
);
5475 /* Compute the pullback of "aff" by the function represented by "ma".
5476 * In other words, plug in "ma" in "aff". The result is an affine expression
5477 * defined over the domain space of "ma".
5479 * If "aff" is represented by
5481 * (a(p) + b x + c(divs))/d
5483 * and ma is represented by
5485 * x = D(p) + F(y) + G(divs')
5487 * then the result is
5489 * (a(p) + b D(p) + b F(y) + b G(divs') + c(divs))/d
5491 * The divs in the local space of the input are similarly adjusted
5492 * through a call to isl_local_space_preimage_multi_aff.
5494 __isl_give isl_aff
*isl_aff_pullback_multi_aff(__isl_take isl_aff
*aff
,
5495 __isl_take isl_multi_aff
*ma
)
5497 isl_aff
*res
= NULL
;
5498 isl_local_space
*ls
;
5499 int n_div_aff
, n_div_ma
;
5500 isl_int f
, c1
, c2
, g
;
5502 ma
= isl_multi_aff_align_divs(ma
);
5506 n_div_aff
= isl_aff_dim(aff
, isl_dim_div
);
5507 n_div_ma
= ma
->n
? isl_aff_dim(ma
->u
.p
[0], isl_dim_div
) : 0;
5509 ls
= isl_aff_get_domain_local_space(aff
);
5510 ls
= isl_local_space_preimage_multi_aff(ls
, isl_multi_aff_copy(ma
));
5511 res
= isl_aff_alloc(ls
);
5520 isl_seq_preimage(res
->v
->el
, aff
->v
->el
, ma
, 0, 0, n_div_ma
, n_div_aff
,
5529 isl_multi_aff_free(ma
);
5530 res
= isl_aff_normalize(res
);
5534 isl_multi_aff_free(ma
);
5539 /* Compute the pullback of "aff1" by the function represented by "aff2".
5540 * In other words, plug in "aff2" in "aff1". The result is an affine expression
5541 * defined over the domain space of "aff1".
5543 * The domain of "aff1" should match the range of "aff2", which means
5544 * that it should be single-dimensional.
5546 __isl_give isl_aff
*isl_aff_pullback_aff(__isl_take isl_aff
*aff1
,
5547 __isl_take isl_aff
*aff2
)
5551 ma
= isl_multi_aff_from_aff(aff2
);
5552 return isl_aff_pullback_multi_aff(aff1
, ma
);
5555 /* Compute the pullback of "ma1" by the function represented by "ma2".
5556 * In other words, plug in "ma2" in "ma1".
5558 * The parameters of "ma1" and "ma2" are assumed to have been aligned.
5560 static __isl_give isl_multi_aff
*isl_multi_aff_pullback_multi_aff_aligned(
5561 __isl_take isl_multi_aff
*ma1
, __isl_take isl_multi_aff
*ma2
)
5564 isl_space
*space
= NULL
;
5566 ma2
= isl_multi_aff_align_divs(ma2
);
5567 ma1
= isl_multi_aff_cow(ma1
);
5571 space
= isl_space_join(isl_multi_aff_get_space(ma2
),
5572 isl_multi_aff_get_space(ma1
));
5574 for (i
= 0; i
< ma1
->n
; ++i
) {
5575 ma1
->u
.p
[i
] = isl_aff_pullback_multi_aff(ma1
->u
.p
[i
],
5576 isl_multi_aff_copy(ma2
));
5581 ma1
= isl_multi_aff_reset_space(ma1
, space
);
5582 isl_multi_aff_free(ma2
);
5585 isl_space_free(space
);
5586 isl_multi_aff_free(ma2
);
5587 isl_multi_aff_free(ma1
);
5591 /* Compute the pullback of "ma1" by the function represented by "ma2".
5592 * In other words, plug in "ma2" in "ma1".
5594 __isl_give isl_multi_aff
*isl_multi_aff_pullback_multi_aff(
5595 __isl_take isl_multi_aff
*ma1
, __isl_take isl_multi_aff
*ma2
)
5597 return isl_multi_aff_align_params_multi_multi_and(ma1
, ma2
,
5598 &isl_multi_aff_pullback_multi_aff_aligned
);
5601 /* Extend the local space of "dst" to include the divs
5602 * in the local space of "src".
5604 * If "src" does not have any divs or if the local spaces of "dst" and
5605 * "src" are the same, then no extension is required.
5607 __isl_give isl_aff
*isl_aff_align_divs(__isl_take isl_aff
*dst
,
5608 __isl_keep isl_aff
*src
)
5611 int src_n_div
, dst_n_div
;
5618 return isl_aff_free(dst
);
5620 ctx
= isl_aff_get_ctx(src
);
5621 equal
= isl_local_space_has_equal_space(src
->ls
, dst
->ls
);
5623 return isl_aff_free(dst
);
5625 isl_die(ctx
, isl_error_invalid
,
5626 "spaces don't match", goto error
);
5628 src_n_div
= isl_local_space_dim(src
->ls
, isl_dim_div
);
5631 equal
= isl_local_space_is_equal(src
->ls
, dst
->ls
);
5633 return isl_aff_free(dst
);
5637 dst_n_div
= isl_local_space_dim(dst
->ls
, isl_dim_div
);
5638 exp1
= isl_alloc_array(ctx
, int, src_n_div
);
5639 exp2
= isl_alloc_array(ctx
, int, dst_n_div
);
5640 if (!exp1
|| (dst_n_div
&& !exp2
))
5643 div
= isl_merge_divs(src
->ls
->div
, dst
->ls
->div
, exp1
, exp2
);
5644 dst
= isl_aff_expand_divs(dst
, div
, exp2
);
5652 return isl_aff_free(dst
);
5655 /* Adjust the local spaces of the affine expressions in "maff"
5656 * such that they all have the save divs.
5658 __isl_give isl_multi_aff
*isl_multi_aff_align_divs(
5659 __isl_take isl_multi_aff
*maff
)
5667 maff
= isl_multi_aff_cow(maff
);
5671 for (i
= 1; i
< maff
->n
; ++i
)
5672 maff
->u
.p
[0] = isl_aff_align_divs(maff
->u
.p
[0], maff
->u
.p
[i
]);
5673 for (i
= 1; i
< maff
->n
; ++i
) {
5674 maff
->u
.p
[i
] = isl_aff_align_divs(maff
->u
.p
[i
], maff
->u
.p
[0]);
5676 return isl_multi_aff_free(maff
);
5682 __isl_give isl_aff
*isl_aff_lift(__isl_take isl_aff
*aff
)
5684 aff
= isl_aff_cow(aff
);
5688 aff
->ls
= isl_local_space_lift(aff
->ls
);
5690 return isl_aff_free(aff
);
5695 /* Lift "maff" to a space with extra dimensions such that the result
5696 * has no more existentially quantified variables.
5697 * If "ls" is not NULL, then *ls is assigned the local space that lies
5698 * at the basis of the lifting applied to "maff".
5700 __isl_give isl_multi_aff
*isl_multi_aff_lift(__isl_take isl_multi_aff
*maff
,
5701 __isl_give isl_local_space
**ls
)
5715 isl_space
*space
= isl_multi_aff_get_domain_space(maff
);
5716 *ls
= isl_local_space_from_space(space
);
5718 return isl_multi_aff_free(maff
);
5723 maff
= isl_multi_aff_cow(maff
);
5724 maff
= isl_multi_aff_align_divs(maff
);
5728 n_div
= isl_aff_dim(maff
->u
.p
[0], isl_dim_div
);
5729 space
= isl_multi_aff_get_space(maff
);
5730 space
= isl_space_lift(isl_space_domain(space
), n_div
);
5731 space
= isl_space_extend_domain_with_range(space
,
5732 isl_multi_aff_get_space(maff
));
5734 return isl_multi_aff_free(maff
);
5735 isl_space_free(maff
->space
);
5736 maff
->space
= space
;
5739 *ls
= isl_aff_get_domain_local_space(maff
->u
.p
[0]);
5741 return isl_multi_aff_free(maff
);
5744 for (i
= 0; i
< maff
->n
; ++i
) {
5745 maff
->u
.p
[i
] = isl_aff_lift(maff
->u
.p
[i
]);
5753 isl_local_space_free(*ls
);
5754 return isl_multi_aff_free(maff
);
5758 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma".
5760 __isl_give isl_pw_aff
*isl_pw_multi_aff_get_pw_aff(
5761 __isl_keep isl_pw_multi_aff
*pma
, int pos
)
5771 n_out
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
5772 if (pos
< 0 || pos
>= n_out
)
5773 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
5774 "index out of bounds", return NULL
);
5776 space
= isl_pw_multi_aff_get_space(pma
);
5777 space
= isl_space_drop_dims(space
, isl_dim_out
,
5778 pos
+ 1, n_out
- pos
- 1);
5779 space
= isl_space_drop_dims(space
, isl_dim_out
, 0, pos
);
5781 pa
= isl_pw_aff_alloc_size(space
, pma
->n
);
5782 for (i
= 0; i
< pma
->n
; ++i
) {
5784 aff
= isl_multi_aff_get_aff(pma
->p
[i
].maff
, pos
);
5785 pa
= isl_pw_aff_add_piece(pa
, isl_set_copy(pma
->p
[i
].set
), aff
);
5791 /* Return an isl_pw_multi_aff with the given "set" as domain and
5792 * an unnamed zero-dimensional range.
5794 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_domain(
5795 __isl_take isl_set
*set
)
5800 space
= isl_set_get_space(set
);
5801 space
= isl_space_from_domain(space
);
5802 ma
= isl_multi_aff_zero(space
);
5803 return isl_pw_multi_aff_alloc(set
, ma
);
5806 /* Add an isl_pw_multi_aff with the given "set" as domain and
5807 * an unnamed zero-dimensional range to *user.
5809 static isl_stat
add_pw_multi_aff_from_domain(__isl_take isl_set
*set
,
5812 isl_union_pw_multi_aff
**upma
= user
;
5813 isl_pw_multi_aff
*pma
;
5815 pma
= isl_pw_multi_aff_from_domain(set
);
5816 *upma
= isl_union_pw_multi_aff_add_pw_multi_aff(*upma
, pma
);
5821 /* Return an isl_union_pw_multi_aff with the given "uset" as domain and
5822 * an unnamed zero-dimensional range.
5824 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_domain(
5825 __isl_take isl_union_set
*uset
)
5828 isl_union_pw_multi_aff
*upma
;
5833 space
= isl_union_set_get_space(uset
);
5834 upma
= isl_union_pw_multi_aff_empty(space
);
5836 if (isl_union_set_foreach_set(uset
,
5837 &add_pw_multi_aff_from_domain
, &upma
) < 0)
5840 isl_union_set_free(uset
);
5843 isl_union_set_free(uset
);
5844 isl_union_pw_multi_aff_free(upma
);
5848 /* Local data for bin_entry and the callback "fn".
5850 struct isl_union_pw_multi_aff_bin_data
{
5851 isl_union_pw_multi_aff
*upma2
;
5852 isl_union_pw_multi_aff
*res
;
5853 isl_pw_multi_aff
*pma
;
5854 isl_stat (*fn
)(__isl_take isl_pw_multi_aff
*pma
, void *user
);
5857 /* Given an isl_pw_multi_aff from upma1, store it in data->pma
5858 * and call data->fn for each isl_pw_multi_aff in data->upma2.
5860 static isl_stat
bin_entry(__isl_take isl_pw_multi_aff
*pma
, void *user
)
5862 struct isl_union_pw_multi_aff_bin_data
*data
= user
;
5866 r
= isl_union_pw_multi_aff_foreach_pw_multi_aff(data
->upma2
,
5868 isl_pw_multi_aff_free(pma
);
5873 /* Call "fn" on each pair of isl_pw_multi_affs in "upma1" and "upma2".
5874 * The isl_pw_multi_aff from upma1 is stored in data->pma (where data is
5875 * passed as user field) and the isl_pw_multi_aff from upma2 is available
5876 * as *entry. The callback should adjust data->res if desired.
5878 static __isl_give isl_union_pw_multi_aff
*bin_op(
5879 __isl_take isl_union_pw_multi_aff
*upma1
,
5880 __isl_take isl_union_pw_multi_aff
*upma2
,
5881 isl_stat (*fn
)(__isl_take isl_pw_multi_aff
*pma
, void *user
))
5884 struct isl_union_pw_multi_aff_bin_data data
= { NULL
, NULL
, NULL
, fn
};
5886 space
= isl_union_pw_multi_aff_get_space(upma2
);
5887 upma1
= isl_union_pw_multi_aff_align_params(upma1
, space
);
5888 space
= isl_union_pw_multi_aff_get_space(upma1
);
5889 upma2
= isl_union_pw_multi_aff_align_params(upma2
, space
);
5891 if (!upma1
|| !upma2
)
5895 data
.res
= isl_union_pw_multi_aff_alloc_same_size(upma1
);
5896 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma1
,
5897 &bin_entry
, &data
) < 0)
5900 isl_union_pw_multi_aff_free(upma1
);
5901 isl_union_pw_multi_aff_free(upma2
);
5904 isl_union_pw_multi_aff_free(upma1
);
5905 isl_union_pw_multi_aff_free(upma2
);
5906 isl_union_pw_multi_aff_free(data
.res
);
5910 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
5911 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
5913 static __isl_give isl_pw_multi_aff
*pw_multi_aff_range_product(
5914 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
5918 space
= isl_space_range_product(isl_pw_multi_aff_get_space(pma1
),
5919 isl_pw_multi_aff_get_space(pma2
));
5920 return isl_pw_multi_aff_on_shared_domain_in(pma1
, pma2
, space
,
5921 &isl_multi_aff_range_product
);
5924 /* Given two isl_pw_multi_affs A -> B and C -> D,
5925 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
5927 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_range_product(
5928 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
5930 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
5931 &pw_multi_aff_range_product
);
5934 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
5935 * construct an isl_pw_multi_aff (A * C) -> (B, D).
5937 static __isl_give isl_pw_multi_aff
*pw_multi_aff_flat_range_product(
5938 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
5942 space
= isl_space_range_product(isl_pw_multi_aff_get_space(pma1
),
5943 isl_pw_multi_aff_get_space(pma2
));
5944 space
= isl_space_flatten_range(space
);
5945 return isl_pw_multi_aff_on_shared_domain_in(pma1
, pma2
, space
,
5946 &isl_multi_aff_flat_range_product
);
5949 /* Given two isl_pw_multi_affs A -> B and C -> D,
5950 * construct an isl_pw_multi_aff (A * C) -> (B, D).
5952 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_flat_range_product(
5953 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
5955 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
5956 &pw_multi_aff_flat_range_product
);
5959 /* If data->pma and "pma2" have the same domain space, then compute
5960 * their flat range product and the result to data->res.
5962 static isl_stat
flat_range_product_entry(__isl_take isl_pw_multi_aff
*pma2
,
5965 struct isl_union_pw_multi_aff_bin_data
*data
= user
;
5967 if (!isl_space_tuple_is_equal(data
->pma
->dim
, isl_dim_in
,
5968 pma2
->dim
, isl_dim_in
)) {
5969 isl_pw_multi_aff_free(pma2
);
5973 pma2
= isl_pw_multi_aff_flat_range_product(
5974 isl_pw_multi_aff_copy(data
->pma
), pma2
);
5976 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma2
);
5981 /* Given two isl_union_pw_multi_affs A -> B and C -> D,
5982 * construct an isl_union_pw_multi_aff (A * C) -> (B, D).
5984 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_flat_range_product(
5985 __isl_take isl_union_pw_multi_aff
*upma1
,
5986 __isl_take isl_union_pw_multi_aff
*upma2
)
5988 return bin_op(upma1
, upma2
, &flat_range_product_entry
);
5991 /* Replace the affine expressions at position "pos" in "pma" by "pa".
5992 * The parameters are assumed to have been aligned.
5994 * The implementation essentially performs an isl_pw_*_on_shared_domain,
5995 * except that it works on two different isl_pw_* types.
5997 static __isl_give isl_pw_multi_aff
*pw_multi_aff_set_pw_aff(
5998 __isl_take isl_pw_multi_aff
*pma
, unsigned pos
,
5999 __isl_take isl_pw_aff
*pa
)
6002 isl_pw_multi_aff
*res
= NULL
;
6007 if (!isl_space_tuple_is_equal(pma
->dim
, isl_dim_in
,
6008 pa
->dim
, isl_dim_in
))
6009 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
6010 "domains don't match", goto error
);
6011 if (pos
>= isl_pw_multi_aff_dim(pma
, isl_dim_out
))
6012 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
6013 "index out of bounds", goto error
);
6016 res
= isl_pw_multi_aff_alloc_size(isl_pw_multi_aff_get_space(pma
), n
);
6018 for (i
= 0; i
< pma
->n
; ++i
) {
6019 for (j
= 0; j
< pa
->n
; ++j
) {
6021 isl_multi_aff
*res_ij
;
6024 common
= isl_set_intersect(isl_set_copy(pma
->p
[i
].set
),
6025 isl_set_copy(pa
->p
[j
].set
));
6026 empty
= isl_set_plain_is_empty(common
);
6027 if (empty
< 0 || empty
) {
6028 isl_set_free(common
);
6034 res_ij
= isl_multi_aff_set_aff(
6035 isl_multi_aff_copy(pma
->p
[i
].maff
), pos
,
6036 isl_aff_copy(pa
->p
[j
].aff
));
6037 res_ij
= isl_multi_aff_gist(res_ij
,
6038 isl_set_copy(common
));
6040 res
= isl_pw_multi_aff_add_piece(res
, common
, res_ij
);
6044 isl_pw_multi_aff_free(pma
);
6045 isl_pw_aff_free(pa
);
6048 isl_pw_multi_aff_free(pma
);
6049 isl_pw_aff_free(pa
);
6050 return isl_pw_multi_aff_free(res
);
6053 /* Replace the affine expressions at position "pos" in "pma" by "pa".
6055 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_set_pw_aff(
6056 __isl_take isl_pw_multi_aff
*pma
, unsigned pos
,
6057 __isl_take isl_pw_aff
*pa
)
6059 isl_bool equal_params
;
6063 equal_params
= isl_space_has_equal_params(pma
->dim
, pa
->dim
);
6064 if (equal_params
< 0)
6067 return pw_multi_aff_set_pw_aff(pma
, pos
, pa
);
6068 if (isl_pw_multi_aff_check_named_params(pma
) < 0 ||
6069 isl_pw_aff_check_named_params(pa
) < 0)
6071 pma
= isl_pw_multi_aff_align_params(pma
, isl_pw_aff_get_space(pa
));
6072 pa
= isl_pw_aff_align_params(pa
, isl_pw_multi_aff_get_space(pma
));
6073 return pw_multi_aff_set_pw_aff(pma
, pos
, pa
);
6075 isl_pw_multi_aff_free(pma
);
6076 isl_pw_aff_free(pa
);
6080 /* Do the parameters of "pa" match those of "space"?
6082 isl_bool
isl_pw_aff_matching_params(__isl_keep isl_pw_aff
*pa
,
6083 __isl_keep isl_space
*space
)
6085 isl_space
*pa_space
;
6089 return isl_bool_error
;
6091 pa_space
= isl_pw_aff_get_space(pa
);
6093 match
= isl_space_has_equal_params(space
, pa_space
);
6095 isl_space_free(pa_space
);
6099 /* Check that the domain space of "pa" matches "space".
6101 isl_stat
isl_pw_aff_check_match_domain_space(__isl_keep isl_pw_aff
*pa
,
6102 __isl_keep isl_space
*space
)
6104 isl_space
*pa_space
;
6108 return isl_stat_error
;
6110 pa_space
= isl_pw_aff_get_space(pa
);
6112 match
= isl_space_has_equal_params(space
, pa_space
);
6116 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
6117 "parameters don't match", goto error
);
6118 match
= isl_space_tuple_is_equal(space
, isl_dim_in
,
6119 pa_space
, isl_dim_in
);
6123 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
6124 "domains don't match", goto error
);
6125 isl_space_free(pa_space
);
6128 isl_space_free(pa_space
);
6129 return isl_stat_error
;
6137 #include <isl_multi_explicit_domain.c>
6138 #include <isl_multi_pw_aff_explicit_domain.c>
6139 #include <isl_multi_templ.c>
6140 #include <isl_multi_apply_set.c>
6141 #include <isl_multi_coalesce.c>
6142 #include <isl_multi_domain_templ.c>
6143 #include <isl_multi_dims.c>
6144 #include <isl_multi_from_base_templ.c>
6145 #include <isl_multi_gist.c>
6146 #include <isl_multi_hash.c>
6147 #include <isl_multi_identity_templ.c>
6148 #include <isl_multi_align_set.c>
6149 #include <isl_multi_intersect.c>
6150 #include <isl_multi_move_dims_templ.c>
6151 #include <isl_multi_product_templ.c>
6152 #include <isl_multi_splice_templ.c>
6153 #include <isl_multi_zero_templ.c>
6155 /* Does "mpa" have a non-trivial explicit domain?
6157 * The explicit domain, if present, is trivial if it represents
6158 * an (obviously) universe set.
6160 isl_bool
isl_multi_pw_aff_has_non_trivial_domain(
6161 __isl_keep isl_multi_pw_aff
*mpa
)
6164 return isl_bool_error
;
6165 if (!isl_multi_pw_aff_has_explicit_domain(mpa
))
6166 return isl_bool_false
;
6167 return isl_bool_not(isl_set_plain_is_universe(mpa
->u
.dom
));
6170 /* Scale the elements of "pma" by the corresponding elements of "mv".
6172 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_scale_multi_val(
6173 __isl_take isl_pw_multi_aff
*pma
, __isl_take isl_multi_val
*mv
)
6176 isl_bool equal_params
;
6178 pma
= isl_pw_multi_aff_cow(pma
);
6181 if (!isl_space_tuple_is_equal(pma
->dim
, isl_dim_out
,
6182 mv
->space
, isl_dim_set
))
6183 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
6184 "spaces don't match", goto error
);
6185 equal_params
= isl_space_has_equal_params(pma
->dim
, mv
->space
);
6186 if (equal_params
< 0)
6188 if (!equal_params
) {
6189 pma
= isl_pw_multi_aff_align_params(pma
,
6190 isl_multi_val_get_space(mv
));
6191 mv
= isl_multi_val_align_params(mv
,
6192 isl_pw_multi_aff_get_space(pma
));
6197 for (i
= 0; i
< pma
->n
; ++i
) {
6198 pma
->p
[i
].maff
= isl_multi_aff_scale_multi_val(pma
->p
[i
].maff
,
6199 isl_multi_val_copy(mv
));
6200 if (!pma
->p
[i
].maff
)
6204 isl_multi_val_free(mv
);
6207 isl_multi_val_free(mv
);
6208 isl_pw_multi_aff_free(pma
);
6212 /* This function is called for each entry of an isl_union_pw_multi_aff.
6213 * If the space of the entry matches that of data->mv,
6214 * then apply isl_pw_multi_aff_scale_multi_val and return the result.
6215 * Otherwise, return an empty isl_pw_multi_aff.
6217 static __isl_give isl_pw_multi_aff
*union_pw_multi_aff_scale_multi_val_entry(
6218 __isl_take isl_pw_multi_aff
*pma
, void *user
)
6220 isl_multi_val
*mv
= user
;
6224 if (!isl_space_tuple_is_equal(pma
->dim
, isl_dim_out
,
6225 mv
->space
, isl_dim_set
)) {
6226 isl_space
*space
= isl_pw_multi_aff_get_space(pma
);
6227 isl_pw_multi_aff_free(pma
);
6228 return isl_pw_multi_aff_empty(space
);
6231 return isl_pw_multi_aff_scale_multi_val(pma
, isl_multi_val_copy(mv
));
6234 /* Scale the elements of "upma" by the corresponding elements of "mv",
6235 * for those entries that match the space of "mv".
6237 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_scale_multi_val(
6238 __isl_take isl_union_pw_multi_aff
*upma
, __isl_take isl_multi_val
*mv
)
6240 upma
= isl_union_pw_multi_aff_align_params(upma
,
6241 isl_multi_val_get_space(mv
));
6242 mv
= isl_multi_val_align_params(mv
,
6243 isl_union_pw_multi_aff_get_space(upma
));
6247 return isl_union_pw_multi_aff_transform(upma
,
6248 &union_pw_multi_aff_scale_multi_val_entry
, mv
);
6250 isl_multi_val_free(mv
);
6253 isl_multi_val_free(mv
);
6254 isl_union_pw_multi_aff_free(upma
);
6258 /* Construct and return a piecewise multi affine expression
6259 * in the given space with value zero in each of the output dimensions and
6260 * a universe domain.
6262 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_zero(__isl_take isl_space
*space
)
6264 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_zero(space
));
6267 /* Construct and return a piecewise multi affine expression
6268 * that is equal to the given piecewise affine expression.
6270 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_pw_aff(
6271 __isl_take isl_pw_aff
*pa
)
6275 isl_pw_multi_aff
*pma
;
6280 space
= isl_pw_aff_get_space(pa
);
6281 pma
= isl_pw_multi_aff_alloc_size(space
, pa
->n
);
6283 for (i
= 0; i
< pa
->n
; ++i
) {
6287 set
= isl_set_copy(pa
->p
[i
].set
);
6288 ma
= isl_multi_aff_from_aff(isl_aff_copy(pa
->p
[i
].aff
));
6289 pma
= isl_pw_multi_aff_add_piece(pma
, set
, ma
);
6292 isl_pw_aff_free(pa
);
6296 /* Construct and return a piecewise multi affine expression
6297 * that is equal to the given multi piecewise affine expression
6298 * on the shared domain of the piecewise affine expressions,
6299 * in the special case of a 0D multi piecewise affine expression.
6301 * Create a piecewise multi affine expression with the explicit domain of
6302 * the 0D multi piecewise affine expression as domain.
6304 static __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_multi_pw_aff_0D(
6305 __isl_take isl_multi_pw_aff
*mpa
)
6311 space
= isl_multi_pw_aff_get_space(mpa
);
6312 dom
= isl_multi_pw_aff_get_explicit_domain(mpa
);
6313 isl_multi_pw_aff_free(mpa
);
6315 ma
= isl_multi_aff_zero(space
);
6316 return isl_pw_multi_aff_alloc(dom
, ma
);
6319 /* Construct and return a piecewise multi affine expression
6320 * that is equal to the given multi piecewise affine expression
6321 * on the shared domain of the piecewise affine expressions.
6323 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_multi_pw_aff(
6324 __isl_take isl_multi_pw_aff
*mpa
)
6329 isl_pw_multi_aff
*pma
;
6335 return isl_pw_multi_aff_from_multi_pw_aff_0D(mpa
);
6337 space
= isl_multi_pw_aff_get_space(mpa
);
6338 pa
= isl_multi_pw_aff_get_pw_aff(mpa
, 0);
6339 pma
= isl_pw_multi_aff_from_pw_aff(pa
);
6341 for (i
= 1; i
< mpa
->n
; ++i
) {
6342 isl_pw_multi_aff
*pma_i
;
6344 pa
= isl_multi_pw_aff_get_pw_aff(mpa
, i
);
6345 pma_i
= isl_pw_multi_aff_from_pw_aff(pa
);
6346 pma
= isl_pw_multi_aff_range_product(pma
, pma_i
);
6349 pma
= isl_pw_multi_aff_reset_space(pma
, space
);
6351 isl_multi_pw_aff_free(mpa
);
6355 /* Construct and return a multi piecewise affine expression
6356 * that is equal to the given multi affine expression.
6358 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_from_multi_aff(
6359 __isl_take isl_multi_aff
*ma
)
6362 isl_multi_pw_aff
*mpa
;
6367 n
= isl_multi_aff_dim(ma
, isl_dim_out
);
6368 mpa
= isl_multi_pw_aff_alloc(isl_multi_aff_get_space(ma
));
6370 for (i
= 0; i
< n
; ++i
) {
6373 pa
= isl_pw_aff_from_aff(isl_multi_aff_get_aff(ma
, i
));
6374 mpa
= isl_multi_pw_aff_set_pw_aff(mpa
, i
, pa
);
6377 isl_multi_aff_free(ma
);
6381 /* Construct and return a multi piecewise affine expression
6382 * that is equal to the given piecewise multi affine expression.
6384 * If the resulting multi piecewise affine expression has
6385 * an explicit domain, then assign it the domain of the input.
6386 * In other cases, the domain is stored in the individual elements.
6388 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_from_pw_multi_aff(
6389 __isl_take isl_pw_multi_aff
*pma
)
6393 isl_multi_pw_aff
*mpa
;
6398 n
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
6399 space
= isl_pw_multi_aff_get_space(pma
);
6400 mpa
= isl_multi_pw_aff_alloc(space
);
6402 for (i
= 0; i
< n
; ++i
) {
6405 pa
= isl_pw_multi_aff_get_pw_aff(pma
, i
);
6406 mpa
= isl_multi_pw_aff_set_pw_aff(mpa
, i
, pa
);
6408 if (isl_multi_pw_aff_has_explicit_domain(mpa
)) {
6411 dom
= isl_pw_multi_aff_domain(isl_pw_multi_aff_copy(pma
));
6412 mpa
= isl_multi_pw_aff_intersect_domain(mpa
, dom
);
6415 isl_pw_multi_aff_free(pma
);
6419 /* Do "pa1" and "pa2" represent the same function?
6421 * We first check if they are obviously equal.
6422 * If not, we convert them to maps and check if those are equal.
6424 * If "pa1" or "pa2" contain any NaNs, then they are considered
6425 * not to be the same. A NaN is not equal to anything, not even
6428 isl_bool
isl_pw_aff_is_equal(__isl_keep isl_pw_aff
*pa1
,
6429 __isl_keep isl_pw_aff
*pa2
)
6433 isl_map
*map1
, *map2
;
6436 return isl_bool_error
;
6438 equal
= isl_pw_aff_plain_is_equal(pa1
, pa2
);
6439 if (equal
< 0 || equal
)
6441 has_nan
= either_involves_nan(pa1
, pa2
);
6443 return isl_bool_error
;
6445 return isl_bool_false
;
6447 map1
= isl_map_from_pw_aff_internal(isl_pw_aff_copy(pa1
));
6448 map2
= isl_map_from_pw_aff_internal(isl_pw_aff_copy(pa2
));
6449 equal
= isl_map_is_equal(map1
, map2
);
6456 /* Do "mpa1" and "mpa2" represent the same function?
6458 * Note that we cannot convert the entire isl_multi_pw_aff
6459 * to a map because the domains of the piecewise affine expressions
6460 * may not be the same.
6462 isl_bool
isl_multi_pw_aff_is_equal(__isl_keep isl_multi_pw_aff
*mpa1
,
6463 __isl_keep isl_multi_pw_aff
*mpa2
)
6466 isl_bool equal
, equal_params
;
6469 return isl_bool_error
;
6471 equal_params
= isl_space_has_equal_params(mpa1
->space
, mpa2
->space
);
6472 if (equal_params
< 0)
6473 return isl_bool_error
;
6474 if (!equal_params
) {
6475 if (!isl_space_has_named_params(mpa1
->space
))
6476 return isl_bool_false
;
6477 if (!isl_space_has_named_params(mpa2
->space
))
6478 return isl_bool_false
;
6479 mpa1
= isl_multi_pw_aff_copy(mpa1
);
6480 mpa2
= isl_multi_pw_aff_copy(mpa2
);
6481 mpa1
= isl_multi_pw_aff_align_params(mpa1
,
6482 isl_multi_pw_aff_get_space(mpa2
));
6483 mpa2
= isl_multi_pw_aff_align_params(mpa2
,
6484 isl_multi_pw_aff_get_space(mpa1
));
6485 equal
= isl_multi_pw_aff_is_equal(mpa1
, mpa2
);
6486 isl_multi_pw_aff_free(mpa1
);
6487 isl_multi_pw_aff_free(mpa2
);
6491 equal
= isl_space_is_equal(mpa1
->space
, mpa2
->space
);
6492 if (equal
< 0 || !equal
)
6495 for (i
= 0; i
< mpa1
->n
; ++i
) {
6496 equal
= isl_pw_aff_is_equal(mpa1
->u
.p
[i
], mpa2
->u
.p
[i
]);
6497 if (equal
< 0 || !equal
)
6501 return isl_bool_true
;
6504 /* Do "pma1" and "pma2" represent the same function?
6506 * First check if they are obviously equal.
6507 * If not, then convert them to maps and check if those are equal.
6509 * If "pa1" or "pa2" contain any NaNs, then they are considered
6510 * not to be the same. A NaN is not equal to anything, not even
6513 isl_bool
isl_pw_multi_aff_is_equal(__isl_keep isl_pw_multi_aff
*pma1
,
6514 __isl_keep isl_pw_multi_aff
*pma2
)
6518 isl_map
*map1
, *map2
;
6521 return isl_bool_error
;
6523 equal
= isl_pw_multi_aff_plain_is_equal(pma1
, pma2
);
6524 if (equal
< 0 || equal
)
6526 has_nan
= isl_pw_multi_aff_involves_nan(pma1
);
6527 if (has_nan
>= 0 && !has_nan
)
6528 has_nan
= isl_pw_multi_aff_involves_nan(pma2
);
6529 if (has_nan
< 0 || has_nan
)
6530 return isl_bool_not(has_nan
);
6532 map1
= isl_map_from_pw_multi_aff(isl_pw_multi_aff_copy(pma1
));
6533 map2
= isl_map_from_pw_multi_aff(isl_pw_multi_aff_copy(pma2
));
6534 equal
= isl_map_is_equal(map1
, map2
);
6541 /* Compute the pullback of "mpa" by the function represented by "ma".
6542 * In other words, plug in "ma" in "mpa".
6544 * The parameters of "mpa" and "ma" are assumed to have been aligned.
6546 * If "mpa" has an explicit domain, then it is this domain
6547 * that needs to undergo a pullback, i.e., a preimage.
6549 static __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_pullback_multi_aff_aligned(
6550 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_multi_aff
*ma
)
6553 isl_space
*space
= NULL
;
6555 mpa
= isl_multi_pw_aff_cow(mpa
);
6559 space
= isl_space_join(isl_multi_aff_get_space(ma
),
6560 isl_multi_pw_aff_get_space(mpa
));
6564 for (i
= 0; i
< mpa
->n
; ++i
) {
6565 mpa
->u
.p
[i
] = isl_pw_aff_pullback_multi_aff(mpa
->u
.p
[i
],
6566 isl_multi_aff_copy(ma
));
6570 if (isl_multi_pw_aff_has_explicit_domain(mpa
)) {
6571 mpa
->u
.dom
= isl_set_preimage_multi_aff(mpa
->u
.dom
,
6572 isl_multi_aff_copy(ma
));
6577 isl_multi_aff_free(ma
);
6578 isl_space_free(mpa
->space
);
6582 isl_space_free(space
);
6583 isl_multi_pw_aff_free(mpa
);
6584 isl_multi_aff_free(ma
);
6588 /* Compute the pullback of "mpa" by the function represented by "ma".
6589 * In other words, plug in "ma" in "mpa".
6591 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_pullback_multi_aff(
6592 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_multi_aff
*ma
)
6594 isl_bool equal_params
;
6598 equal_params
= isl_space_has_equal_params(mpa
->space
, ma
->space
);
6599 if (equal_params
< 0)
6602 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa
, ma
);
6603 mpa
= isl_multi_pw_aff_align_params(mpa
, isl_multi_aff_get_space(ma
));
6604 ma
= isl_multi_aff_align_params(ma
, isl_multi_pw_aff_get_space(mpa
));
6605 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa
, ma
);
6607 isl_multi_pw_aff_free(mpa
);
6608 isl_multi_aff_free(ma
);
6612 /* Compute the pullback of "mpa" by the function represented by "pma".
6613 * In other words, plug in "pma" in "mpa".
6615 * The parameters of "mpa" and "mpa" are assumed to have been aligned.
6617 * If "mpa" has an explicit domain, then it is this domain
6618 * that needs to undergo a pullback, i.e., a preimage.
6620 static __isl_give isl_multi_pw_aff
*
6621 isl_multi_pw_aff_pullback_pw_multi_aff_aligned(
6622 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_pw_multi_aff
*pma
)
6625 isl_space
*space
= NULL
;
6627 mpa
= isl_multi_pw_aff_cow(mpa
);
6631 space
= isl_space_join(isl_pw_multi_aff_get_space(pma
),
6632 isl_multi_pw_aff_get_space(mpa
));
6634 for (i
= 0; i
< mpa
->n
; ++i
) {
6635 mpa
->u
.p
[i
] = isl_pw_aff_pullback_pw_multi_aff_aligned(
6636 mpa
->u
.p
[i
], isl_pw_multi_aff_copy(pma
));
6640 if (isl_multi_pw_aff_has_explicit_domain(mpa
)) {
6641 mpa
->u
.dom
= isl_set_preimage_pw_multi_aff(mpa
->u
.dom
,
6642 isl_pw_multi_aff_copy(pma
));
6647 isl_pw_multi_aff_free(pma
);
6648 isl_space_free(mpa
->space
);
6652 isl_space_free(space
);
6653 isl_multi_pw_aff_free(mpa
);
6654 isl_pw_multi_aff_free(pma
);
6658 /* Compute the pullback of "mpa" by the function represented by "pma".
6659 * In other words, plug in "pma" in "mpa".
6661 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_pullback_pw_multi_aff(
6662 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_pw_multi_aff
*pma
)
6664 isl_bool equal_params
;
6668 equal_params
= isl_space_has_equal_params(mpa
->space
, pma
->dim
);
6669 if (equal_params
< 0)
6672 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa
, pma
);
6673 mpa
= isl_multi_pw_aff_align_params(mpa
,
6674 isl_pw_multi_aff_get_space(pma
));
6675 pma
= isl_pw_multi_aff_align_params(pma
,
6676 isl_multi_pw_aff_get_space(mpa
));
6677 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa
, pma
);
6679 isl_multi_pw_aff_free(mpa
);
6680 isl_pw_multi_aff_free(pma
);
6684 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
6685 * with the domain of "aff". The domain of the result is the same
6687 * "mpa" and "aff" are assumed to have been aligned.
6689 * We first extract the parametric constant from "aff", defined
6690 * over the correct domain.
6691 * Then we add the appropriate combinations of the members of "mpa".
6692 * Finally, we add the integer divisions through recursive calls.
6694 static __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_aff_aligned(
6695 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_aff
*aff
)
6703 n_in
= isl_aff_dim(aff
, isl_dim_in
);
6704 n_div
= isl_aff_dim(aff
, isl_dim_div
);
6706 space
= isl_space_domain(isl_multi_pw_aff_get_space(mpa
));
6707 tmp
= isl_aff_copy(aff
);
6708 tmp
= isl_aff_drop_dims(tmp
, isl_dim_div
, 0, n_div
);
6709 tmp
= isl_aff_drop_dims(tmp
, isl_dim_in
, 0, n_in
);
6710 tmp
= isl_aff_add_dims(tmp
, isl_dim_in
,
6711 isl_space_dim(space
, isl_dim_set
));
6712 tmp
= isl_aff_reset_domain_space(tmp
, space
);
6713 pa
= isl_pw_aff_from_aff(tmp
);
6715 for (i
= 0; i
< n_in
; ++i
) {
6718 if (!isl_aff_involves_dims(aff
, isl_dim_in
, i
, 1))
6720 v
= isl_aff_get_coefficient_val(aff
, isl_dim_in
, i
);
6721 pa_i
= isl_multi_pw_aff_get_pw_aff(mpa
, i
);
6722 pa_i
= isl_pw_aff_scale_val(pa_i
, v
);
6723 pa
= isl_pw_aff_add(pa
, pa_i
);
6726 for (i
= 0; i
< n_div
; ++i
) {
6730 if (!isl_aff_involves_dims(aff
, isl_dim_div
, i
, 1))
6732 div
= isl_aff_get_div(aff
, i
);
6733 pa_i
= isl_multi_pw_aff_apply_aff_aligned(
6734 isl_multi_pw_aff_copy(mpa
), div
);
6735 pa_i
= isl_pw_aff_floor(pa_i
);
6736 v
= isl_aff_get_coefficient_val(aff
, isl_dim_div
, i
);
6737 pa_i
= isl_pw_aff_scale_val(pa_i
, v
);
6738 pa
= isl_pw_aff_add(pa
, pa_i
);
6741 isl_multi_pw_aff_free(mpa
);
6747 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
6748 * with the domain of "aff". The domain of the result is the same
6751 __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_aff(
6752 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_aff
*aff
)
6754 isl_bool equal_params
;
6758 equal_params
= isl_space_has_equal_params(aff
->ls
->dim
, mpa
->space
);
6759 if (equal_params
< 0)
6762 return isl_multi_pw_aff_apply_aff_aligned(mpa
, aff
);
6764 aff
= isl_aff_align_params(aff
, isl_multi_pw_aff_get_space(mpa
));
6765 mpa
= isl_multi_pw_aff_align_params(mpa
, isl_aff_get_space(aff
));
6767 return isl_multi_pw_aff_apply_aff_aligned(mpa
, aff
);
6770 isl_multi_pw_aff_free(mpa
);
6774 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
6775 * with the domain of "pa". The domain of the result is the same
6777 * "mpa" and "pa" are assumed to have been aligned.
6779 * We consider each piece in turn. Note that the domains of the
6780 * pieces are assumed to be disjoint and they remain disjoint
6781 * after taking the preimage (over the same function).
6783 static __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_pw_aff_aligned(
6784 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_pw_aff
*pa
)
6793 space
= isl_space_join(isl_multi_pw_aff_get_space(mpa
),
6794 isl_pw_aff_get_space(pa
));
6795 res
= isl_pw_aff_empty(space
);
6797 for (i
= 0; i
< pa
->n
; ++i
) {
6801 pa_i
= isl_multi_pw_aff_apply_aff_aligned(
6802 isl_multi_pw_aff_copy(mpa
),
6803 isl_aff_copy(pa
->p
[i
].aff
));
6804 domain
= isl_set_copy(pa
->p
[i
].set
);
6805 domain
= isl_set_preimage_multi_pw_aff(domain
,
6806 isl_multi_pw_aff_copy(mpa
));
6807 pa_i
= isl_pw_aff_intersect_domain(pa_i
, domain
);
6808 res
= isl_pw_aff_add_disjoint(res
, pa_i
);
6811 isl_pw_aff_free(pa
);
6812 isl_multi_pw_aff_free(mpa
);
6815 isl_pw_aff_free(pa
);
6816 isl_multi_pw_aff_free(mpa
);
6820 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
6821 * with the domain of "pa". The domain of the result is the same
6824 __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_pw_aff(
6825 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_pw_aff
*pa
)
6827 isl_bool equal_params
;
6831 equal_params
= isl_space_has_equal_params(pa
->dim
, mpa
->space
);
6832 if (equal_params
< 0)
6835 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa
, pa
);
6837 pa
= isl_pw_aff_align_params(pa
, isl_multi_pw_aff_get_space(mpa
));
6838 mpa
= isl_multi_pw_aff_align_params(mpa
, isl_pw_aff_get_space(pa
));
6840 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa
, pa
);
6842 isl_pw_aff_free(pa
);
6843 isl_multi_pw_aff_free(mpa
);
6847 /* Compute the pullback of "pa" by the function represented by "mpa".
6848 * In other words, plug in "mpa" in "pa".
6849 * "pa" and "mpa" are assumed to have been aligned.
6851 * The pullback is computed by applying "pa" to "mpa".
6853 static __isl_give isl_pw_aff
*isl_pw_aff_pullback_multi_pw_aff_aligned(
6854 __isl_take isl_pw_aff
*pa
, __isl_take isl_multi_pw_aff
*mpa
)
6856 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa
, pa
);
6859 /* Compute the pullback of "pa" by the function represented by "mpa".
6860 * In other words, plug in "mpa" in "pa".
6862 * The pullback is computed by applying "pa" to "mpa".
6864 __isl_give isl_pw_aff
*isl_pw_aff_pullback_multi_pw_aff(
6865 __isl_take isl_pw_aff
*pa
, __isl_take isl_multi_pw_aff
*mpa
)
6867 return isl_multi_pw_aff_apply_pw_aff(mpa
, pa
);
6870 /* Compute the pullback of "mpa1" by the function represented by "mpa2".
6871 * In other words, plug in "mpa2" in "mpa1".
6873 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
6875 * We pullback each member of "mpa1" in turn.
6877 * If "mpa1" has an explicit domain, then it is this domain
6878 * that needs to undergo a pullback instead, i.e., a preimage.
6880 static __isl_give isl_multi_pw_aff
*
6881 isl_multi_pw_aff_pullback_multi_pw_aff_aligned(
6882 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
)
6885 isl_space
*space
= NULL
;
6887 mpa1
= isl_multi_pw_aff_cow(mpa1
);
6891 space
= isl_space_join(isl_multi_pw_aff_get_space(mpa2
),
6892 isl_multi_pw_aff_get_space(mpa1
));
6894 for (i
= 0; i
< mpa1
->n
; ++i
) {
6895 mpa1
->u
.p
[i
] = isl_pw_aff_pullback_multi_pw_aff_aligned(
6896 mpa1
->u
.p
[i
], isl_multi_pw_aff_copy(mpa2
));
6901 if (isl_multi_pw_aff_has_explicit_domain(mpa1
)) {
6902 mpa1
->u
.dom
= isl_set_preimage_multi_pw_aff(mpa1
->u
.dom
,
6903 isl_multi_pw_aff_copy(mpa2
));
6907 mpa1
= isl_multi_pw_aff_reset_space(mpa1
, space
);
6909 isl_multi_pw_aff_free(mpa2
);
6912 isl_space_free(space
);
6913 isl_multi_pw_aff_free(mpa1
);
6914 isl_multi_pw_aff_free(mpa2
);
6918 /* Compute the pullback of "mpa1" by the function represented by "mpa2".
6919 * In other words, plug in "mpa2" in "mpa1".
6921 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_pullback_multi_pw_aff(
6922 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
)
6924 return isl_multi_pw_aff_align_params_multi_multi_and(mpa1
, mpa2
,
6925 &isl_multi_pw_aff_pullback_multi_pw_aff_aligned
);
6928 /* Align the parameters of "mpa1" and "mpa2", check that the ranges
6929 * of "mpa1" and "mpa2" live in the same space, construct map space
6930 * between the domain spaces of "mpa1" and "mpa2" and call "order"
6931 * with this map space as extract argument.
6933 static __isl_give isl_map
*isl_multi_pw_aff_order_map(
6934 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
,
6935 __isl_give isl_map
*(*order
)(__isl_keep isl_multi_pw_aff
*mpa1
,
6936 __isl_keep isl_multi_pw_aff
*mpa2
, __isl_take isl_space
*space
))
6939 isl_space
*space1
, *space2
;
6942 mpa1
= isl_multi_pw_aff_align_params(mpa1
,
6943 isl_multi_pw_aff_get_space(mpa2
));
6944 mpa2
= isl_multi_pw_aff_align_params(mpa2
,
6945 isl_multi_pw_aff_get_space(mpa1
));
6948 match
= isl_space_tuple_is_equal(mpa1
->space
, isl_dim_out
,
6949 mpa2
->space
, isl_dim_out
);
6953 isl_die(isl_multi_pw_aff_get_ctx(mpa1
), isl_error_invalid
,
6954 "range spaces don't match", goto error
);
6955 space1
= isl_space_domain(isl_multi_pw_aff_get_space(mpa1
));
6956 space2
= isl_space_domain(isl_multi_pw_aff_get_space(mpa2
));
6957 space1
= isl_space_map_from_domain_and_range(space1
, space2
);
6959 res
= order(mpa1
, mpa2
, space1
);
6960 isl_multi_pw_aff_free(mpa1
);
6961 isl_multi_pw_aff_free(mpa2
);
6964 isl_multi_pw_aff_free(mpa1
);
6965 isl_multi_pw_aff_free(mpa2
);
6969 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
6970 * where the function values are equal. "space" is the space of the result.
6971 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
6973 * "mpa1" and "mpa2" are equal when each of the pairs of elements
6974 * in the sequences are equal.
6976 static __isl_give isl_map
*isl_multi_pw_aff_eq_map_on_space(
6977 __isl_keep isl_multi_pw_aff
*mpa1
, __isl_keep isl_multi_pw_aff
*mpa2
,
6978 __isl_take isl_space
*space
)
6983 res
= isl_map_universe(space
);
6985 n
= isl_multi_pw_aff_dim(mpa1
, isl_dim_out
);
6986 for (i
= 0; i
< n
; ++i
) {
6987 isl_pw_aff
*pa1
, *pa2
;
6990 pa1
= isl_multi_pw_aff_get_pw_aff(mpa1
, i
);
6991 pa2
= isl_multi_pw_aff_get_pw_aff(mpa2
, i
);
6992 map
= isl_pw_aff_eq_map(pa1
, pa2
);
6993 res
= isl_map_intersect(res
, map
);
6999 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7000 * where the function values are equal.
7002 __isl_give isl_map
*isl_multi_pw_aff_eq_map(__isl_take isl_multi_pw_aff
*mpa1
,
7003 __isl_take isl_multi_pw_aff
*mpa2
)
7005 return isl_multi_pw_aff_order_map(mpa1
, mpa2
,
7006 &isl_multi_pw_aff_eq_map_on_space
);
7009 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7010 * where the function values of "mpa1" is lexicographically satisfies "base"
7011 * compared to that of "mpa2". "space" is the space of the result.
7012 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7014 * "mpa1" lexicographically satisfies "base" compared to "mpa2"
7015 * if its i-th element satisfies "base" when compared to
7016 * the i-th element of "mpa2" while all previous elements are
7019 static __isl_give isl_map
*isl_multi_pw_aff_lex_map_on_space(
7020 __isl_keep isl_multi_pw_aff
*mpa1
, __isl_keep isl_multi_pw_aff
*mpa2
,
7021 __isl_give isl_map
*(*base
)(__isl_take isl_pw_aff
*pa1
,
7022 __isl_take isl_pw_aff
*pa2
),
7023 __isl_take isl_space
*space
)
7026 isl_map
*res
, *rest
;
7028 res
= isl_map_empty(isl_space_copy(space
));
7029 rest
= isl_map_universe(space
);
7031 n
= isl_multi_pw_aff_dim(mpa1
, isl_dim_out
);
7032 for (i
= 0; i
< n
; ++i
) {
7033 isl_pw_aff
*pa1
, *pa2
;
7036 pa1
= isl_multi_pw_aff_get_pw_aff(mpa1
, i
);
7037 pa2
= isl_multi_pw_aff_get_pw_aff(mpa2
, i
);
7038 map
= base(pa1
, pa2
);
7039 map
= isl_map_intersect(map
, isl_map_copy(rest
));
7040 res
= isl_map_union(res
, map
);
7045 pa1
= isl_multi_pw_aff_get_pw_aff(mpa1
, i
);
7046 pa2
= isl_multi_pw_aff_get_pw_aff(mpa2
, i
);
7047 map
= isl_pw_aff_eq_map(pa1
, pa2
);
7048 rest
= isl_map_intersect(rest
, map
);
7055 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7056 * where the function value of "mpa1" is lexicographically less than that
7057 * of "mpa2". "space" is the space of the result.
7058 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7060 * "mpa1" is less than "mpa2" if its i-th element is smaller
7061 * than the i-th element of "mpa2" while all previous elements are
7064 __isl_give isl_map
*isl_multi_pw_aff_lex_lt_map_on_space(
7065 __isl_keep isl_multi_pw_aff
*mpa1
, __isl_keep isl_multi_pw_aff
*mpa2
,
7066 __isl_take isl_space
*space
)
7068 return isl_multi_pw_aff_lex_map_on_space(mpa1
, mpa2
,
7069 &isl_pw_aff_lt_map
, space
);
7072 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7073 * where the function value of "mpa1" is lexicographically less than that
7076 __isl_give isl_map
*isl_multi_pw_aff_lex_lt_map(
7077 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
)
7079 return isl_multi_pw_aff_order_map(mpa1
, mpa2
,
7080 &isl_multi_pw_aff_lex_lt_map_on_space
);
7083 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7084 * where the function value of "mpa1" is lexicographically greater than that
7085 * of "mpa2". "space" is the space of the result.
7086 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7088 * "mpa1" is greater than "mpa2" if its i-th element is greater
7089 * than the i-th element of "mpa2" while all previous elements are
7092 __isl_give isl_map
*isl_multi_pw_aff_lex_gt_map_on_space(
7093 __isl_keep isl_multi_pw_aff
*mpa1
, __isl_keep isl_multi_pw_aff
*mpa2
,
7094 __isl_take isl_space
*space
)
7096 return isl_multi_pw_aff_lex_map_on_space(mpa1
, mpa2
,
7097 &isl_pw_aff_gt_map
, space
);
7100 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7101 * where the function value of "mpa1" is lexicographically greater than that
7104 __isl_give isl_map
*isl_multi_pw_aff_lex_gt_map(
7105 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
)
7107 return isl_multi_pw_aff_order_map(mpa1
, mpa2
,
7108 &isl_multi_pw_aff_lex_gt_map_on_space
);
7111 /* Compare two isl_affs.
7113 * Return -1 if "aff1" is "smaller" than "aff2", 1 if "aff1" is "greater"
7114 * than "aff2" and 0 if they are equal.
7116 * The order is fairly arbitrary. We do consider expressions that only involve
7117 * earlier dimensions as "smaller".
7119 int isl_aff_plain_cmp(__isl_keep isl_aff
*aff1
, __isl_keep isl_aff
*aff2
)
7132 cmp
= isl_local_space_cmp(aff1
->ls
, aff2
->ls
);
7136 last1
= isl_seq_last_non_zero(aff1
->v
->el
+ 1, aff1
->v
->size
- 1);
7137 last2
= isl_seq_last_non_zero(aff2
->v
->el
+ 1, aff1
->v
->size
- 1);
7139 return last1
- last2
;
7141 return isl_seq_cmp(aff1
->v
->el
, aff2
->v
->el
, aff1
->v
->size
);
7144 /* Compare two isl_pw_affs.
7146 * Return -1 if "pa1" is "smaller" than "pa2", 1 if "pa1" is "greater"
7147 * than "pa2" and 0 if they are equal.
7149 * The order is fairly arbitrary. We do consider expressions that only involve
7150 * earlier dimensions as "smaller".
7152 int isl_pw_aff_plain_cmp(__isl_keep isl_pw_aff
*pa1
,
7153 __isl_keep isl_pw_aff
*pa2
)
7166 cmp
= isl_space_cmp(pa1
->dim
, pa2
->dim
);
7170 if (pa1
->n
!= pa2
->n
)
7171 return pa1
->n
- pa2
->n
;
7173 for (i
= 0; i
< pa1
->n
; ++i
) {
7174 cmp
= isl_set_plain_cmp(pa1
->p
[i
].set
, pa2
->p
[i
].set
);
7177 cmp
= isl_aff_plain_cmp(pa1
->p
[i
].aff
, pa2
->p
[i
].aff
);
7185 /* Return a piecewise affine expression that is equal to "v" on "domain".
7187 __isl_give isl_pw_aff
*isl_pw_aff_val_on_domain(__isl_take isl_set
*domain
,
7188 __isl_take isl_val
*v
)
7191 isl_local_space
*ls
;
7194 space
= isl_set_get_space(domain
);
7195 ls
= isl_local_space_from_space(space
);
7196 aff
= isl_aff_val_on_domain(ls
, v
);
7198 return isl_pw_aff_alloc(domain
, aff
);
7201 /* Return a multi affine expression that is equal to "mv" on domain
7204 __isl_give isl_multi_aff
*isl_multi_aff_multi_val_on_space(
7205 __isl_take isl_space
*space
, __isl_take isl_multi_val
*mv
)
7209 isl_local_space
*ls
;
7215 n
= isl_multi_val_dim(mv
, isl_dim_set
);
7216 space2
= isl_multi_val_get_space(mv
);
7217 space2
= isl_space_align_params(space2
, isl_space_copy(space
));
7218 space
= isl_space_align_params(space
, isl_space_copy(space2
));
7219 space
= isl_space_map_from_domain_and_range(space
, space2
);
7220 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
7221 ls
= isl_local_space_from_space(isl_space_domain(space
));
7222 for (i
= 0; i
< n
; ++i
) {
7226 v
= isl_multi_val_get_val(mv
, i
);
7227 aff
= isl_aff_val_on_domain(isl_local_space_copy(ls
), v
);
7228 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
7230 isl_local_space_free(ls
);
7232 isl_multi_val_free(mv
);
7235 isl_space_free(space
);
7236 isl_multi_val_free(mv
);
7240 /* Return a piecewise multi-affine expression
7241 * that is equal to "mv" on "domain".
7243 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_multi_val_on_domain(
7244 __isl_take isl_set
*domain
, __isl_take isl_multi_val
*mv
)
7249 space
= isl_set_get_space(domain
);
7250 ma
= isl_multi_aff_multi_val_on_space(space
, mv
);
7252 return isl_pw_multi_aff_alloc(domain
, ma
);
7255 /* Internal data structure for isl_union_pw_multi_aff_multi_val_on_domain.
7256 * mv is the value that should be attained on each domain set
7257 * res collects the results
7259 struct isl_union_pw_multi_aff_multi_val_on_domain_data
{
7261 isl_union_pw_multi_aff
*res
;
7264 /* Create an isl_pw_multi_aff equal to data->mv on "domain"
7265 * and add it to data->res.
7267 static isl_stat
pw_multi_aff_multi_val_on_domain(__isl_take isl_set
*domain
,
7270 struct isl_union_pw_multi_aff_multi_val_on_domain_data
*data
= user
;
7271 isl_pw_multi_aff
*pma
;
7274 mv
= isl_multi_val_copy(data
->mv
);
7275 pma
= isl_pw_multi_aff_multi_val_on_domain(domain
, mv
);
7276 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma
);
7278 return data
->res
? isl_stat_ok
: isl_stat_error
;
7281 /* Return a union piecewise multi-affine expression
7282 * that is equal to "mv" on "domain".
7284 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_multi_val_on_domain(
7285 __isl_take isl_union_set
*domain
, __isl_take isl_multi_val
*mv
)
7287 struct isl_union_pw_multi_aff_multi_val_on_domain_data data
;
7290 space
= isl_union_set_get_space(domain
);
7291 data
.res
= isl_union_pw_multi_aff_empty(space
);
7293 if (isl_union_set_foreach_set(domain
,
7294 &pw_multi_aff_multi_val_on_domain
, &data
) < 0)
7295 data
.res
= isl_union_pw_multi_aff_free(data
.res
);
7296 isl_union_set_free(domain
);
7297 isl_multi_val_free(mv
);
7301 /* Compute the pullback of data->pma by the function represented by "pma2",
7302 * provided the spaces match, and add the results to data->res.
7304 static isl_stat
pullback_entry(__isl_take isl_pw_multi_aff
*pma2
, void *user
)
7306 struct isl_union_pw_multi_aff_bin_data
*data
= user
;
7308 if (!isl_space_tuple_is_equal(data
->pma
->dim
, isl_dim_in
,
7309 pma2
->dim
, isl_dim_out
)) {
7310 isl_pw_multi_aff_free(pma2
);
7314 pma2
= isl_pw_multi_aff_pullback_pw_multi_aff(
7315 isl_pw_multi_aff_copy(data
->pma
), pma2
);
7317 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma2
);
7319 return isl_stat_error
;
7324 /* Compute the pullback of "upma1" by the function represented by "upma2".
7326 __isl_give isl_union_pw_multi_aff
*
7327 isl_union_pw_multi_aff_pullback_union_pw_multi_aff(
7328 __isl_take isl_union_pw_multi_aff
*upma1
,
7329 __isl_take isl_union_pw_multi_aff
*upma2
)
7331 return bin_op(upma1
, upma2
, &pullback_entry
);
7334 /* Check that the domain space of "upa" matches "space".
7336 * This function is called from isl_multi_union_pw_aff_set_union_pw_aff and
7337 * can in principle never fail since the space "space" is that
7338 * of the isl_multi_union_pw_aff and is a set space such that
7339 * there is no domain space to match.
7341 * We check the parameters and double-check that "space" is
7342 * indeed that of a set.
7344 static isl_stat
isl_union_pw_aff_check_match_domain_space(
7345 __isl_keep isl_union_pw_aff
*upa
, __isl_keep isl_space
*space
)
7347 isl_space
*upa_space
;
7351 return isl_stat_error
;
7353 match
= isl_space_is_set(space
);
7355 return isl_stat_error
;
7357 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
7358 "expecting set space", return isl_stat_error
);
7360 upa_space
= isl_union_pw_aff_get_space(upa
);
7361 match
= isl_space_has_equal_params(space
, upa_space
);
7365 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
7366 "parameters don't match", goto error
);
7368 isl_space_free(upa_space
);
7371 isl_space_free(upa_space
);
7372 return isl_stat_error
;
7375 /* Do the parameters of "upa" match those of "space"?
7377 static isl_bool
isl_union_pw_aff_matching_params(
7378 __isl_keep isl_union_pw_aff
*upa
, __isl_keep isl_space
*space
)
7380 isl_space
*upa_space
;
7384 return isl_bool_error
;
7386 upa_space
= isl_union_pw_aff_get_space(upa
);
7388 match
= isl_space_has_equal_params(space
, upa_space
);
7390 isl_space_free(upa_space
);
7394 /* Internal data structure for isl_union_pw_aff_reset_domain_space.
7395 * space represents the new parameters.
7396 * res collects the results.
7398 struct isl_union_pw_aff_reset_params_data
{
7400 isl_union_pw_aff
*res
;
7403 /* Replace the parameters of "pa" by data->space and
7404 * add the result to data->res.
7406 static isl_stat
reset_params(__isl_take isl_pw_aff
*pa
, void *user
)
7408 struct isl_union_pw_aff_reset_params_data
*data
= user
;
7411 space
= isl_pw_aff_get_space(pa
);
7412 space
= isl_space_replace_params(space
, data
->space
);
7413 pa
= isl_pw_aff_reset_space(pa
, space
);
7414 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
7416 return data
->res
? isl_stat_ok
: isl_stat_error
;
7419 /* Replace the domain space of "upa" by "space".
7420 * Since a union expression does not have a (single) domain space,
7421 * "space" is necessarily a parameter space.
7423 * Since the order and the names of the parameters determine
7424 * the hash value, we need to create a new hash table.
7426 static __isl_give isl_union_pw_aff
*isl_union_pw_aff_reset_domain_space(
7427 __isl_take isl_union_pw_aff
*upa
, __isl_take isl_space
*space
)
7429 struct isl_union_pw_aff_reset_params_data data
= { space
};
7432 match
= isl_union_pw_aff_matching_params(upa
, space
);
7434 upa
= isl_union_pw_aff_free(upa
);
7436 isl_space_free(space
);
7440 data
.res
= isl_union_pw_aff_empty(isl_space_copy(space
));
7441 if (isl_union_pw_aff_foreach_pw_aff(upa
, &reset_params
, &data
) < 0)
7442 data
.res
= isl_union_pw_aff_free(data
.res
);
7444 isl_union_pw_aff_free(upa
);
7445 isl_space_free(space
);
7449 /* Return the floor of "pa".
7451 static __isl_give isl_pw_aff
*floor_entry(__isl_take isl_pw_aff
*pa
, void *user
)
7453 return isl_pw_aff_floor(pa
);
7456 /* Given f, return floor(f).
7458 __isl_give isl_union_pw_aff
*isl_union_pw_aff_floor(
7459 __isl_take isl_union_pw_aff
*upa
)
7461 return isl_union_pw_aff_transform_inplace(upa
, &floor_entry
, NULL
);
7466 * upa mod m = upa - m * floor(upa/m)
7468 * with m an integer value.
7470 __isl_give isl_union_pw_aff
*isl_union_pw_aff_mod_val(
7471 __isl_take isl_union_pw_aff
*upa
, __isl_take isl_val
*m
)
7473 isl_union_pw_aff
*res
;
7478 if (!isl_val_is_int(m
))
7479 isl_die(isl_val_get_ctx(m
), isl_error_invalid
,
7480 "expecting integer modulo", goto error
);
7481 if (!isl_val_is_pos(m
))
7482 isl_die(isl_val_get_ctx(m
), isl_error_invalid
,
7483 "expecting positive modulo", goto error
);
7485 res
= isl_union_pw_aff_copy(upa
);
7486 upa
= isl_union_pw_aff_scale_down_val(upa
, isl_val_copy(m
));
7487 upa
= isl_union_pw_aff_floor(upa
);
7488 upa
= isl_union_pw_aff_scale_val(upa
, m
);
7489 res
= isl_union_pw_aff_sub(res
, upa
);
7494 isl_union_pw_aff_free(upa
);
7498 /* Internal data structure for isl_union_pw_multi_aff_get_union_pw_aff.
7499 * pos is the output position that needs to be extracted.
7500 * res collects the results.
7502 struct isl_union_pw_multi_aff_get_union_pw_aff_data
{
7504 isl_union_pw_aff
*res
;
7507 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma"
7508 * (assuming it has such a dimension) and add it to data->res.
7510 static isl_stat
get_union_pw_aff(__isl_take isl_pw_multi_aff
*pma
, void *user
)
7512 struct isl_union_pw_multi_aff_get_union_pw_aff_data
*data
= user
;
7517 return isl_stat_error
;
7519 n_out
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
7520 if (data
->pos
>= n_out
) {
7521 isl_pw_multi_aff_free(pma
);
7525 pa
= isl_pw_multi_aff_get_pw_aff(pma
, data
->pos
);
7526 isl_pw_multi_aff_free(pma
);
7528 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
7530 return data
->res
? isl_stat_ok
: isl_stat_error
;
7533 /* Extract an isl_union_pw_aff corresponding to
7534 * output dimension "pos" of "upma".
7536 __isl_give isl_union_pw_aff
*isl_union_pw_multi_aff_get_union_pw_aff(
7537 __isl_keep isl_union_pw_multi_aff
*upma
, int pos
)
7539 struct isl_union_pw_multi_aff_get_union_pw_aff_data data
;
7546 isl_die(isl_union_pw_multi_aff_get_ctx(upma
), isl_error_invalid
,
7547 "cannot extract at negative position", return NULL
);
7549 space
= isl_union_pw_multi_aff_get_space(upma
);
7550 data
.res
= isl_union_pw_aff_empty(space
);
7552 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma
,
7553 &get_union_pw_aff
, &data
) < 0)
7554 data
.res
= isl_union_pw_aff_free(data
.res
);
7559 /* Return a union piecewise affine expression
7560 * that is equal to "aff" on "domain".
7562 __isl_give isl_union_pw_aff
*isl_union_pw_aff_aff_on_domain(
7563 __isl_take isl_union_set
*domain
, __isl_take isl_aff
*aff
)
7567 pa
= isl_pw_aff_from_aff(aff
);
7568 return isl_union_pw_aff_pw_aff_on_domain(domain
, pa
);
7571 /* Return a union piecewise affine expression
7572 * that is equal to the parameter identified by "id" on "domain".
7574 * Make sure the parameter appears in the space passed to
7575 * isl_aff_param_on_domain_space_id.
7577 __isl_give isl_union_pw_aff
*isl_union_pw_aff_param_on_domain_id(
7578 __isl_take isl_union_set
*domain
, __isl_take isl_id
*id
)
7583 space
= isl_union_set_get_space(domain
);
7584 space
= isl_space_add_param_id(space
, isl_id_copy(id
));
7585 aff
= isl_aff_param_on_domain_space_id(space
, id
);
7586 return isl_union_pw_aff_aff_on_domain(domain
, aff
);
7589 /* Internal data structure for isl_union_pw_aff_pw_aff_on_domain.
7590 * "pa" is the piecewise symbolic value that the resulting isl_union_pw_aff
7592 * "res" collects the results.
7594 struct isl_union_pw_aff_pw_aff_on_domain_data
{
7596 isl_union_pw_aff
*res
;
7599 /* Construct a piecewise affine expression that is equal to data->pa
7600 * on "domain" and add the result to data->res.
7602 static isl_stat
pw_aff_on_domain(__isl_take isl_set
*domain
, void *user
)
7604 struct isl_union_pw_aff_pw_aff_on_domain_data
*data
= user
;
7608 pa
= isl_pw_aff_copy(data
->pa
);
7609 dim
= isl_set_dim(domain
, isl_dim_set
);
7610 pa
= isl_pw_aff_from_range(pa
);
7611 pa
= isl_pw_aff_add_dims(pa
, isl_dim_in
, dim
);
7612 pa
= isl_pw_aff_reset_domain_space(pa
, isl_set_get_space(domain
));
7613 pa
= isl_pw_aff_intersect_domain(pa
, domain
);
7614 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
7616 return data
->res
? isl_stat_ok
: isl_stat_error
;
7619 /* Return a union piecewise affine expression
7620 * that is equal to "pa" on "domain", assuming "domain" and "pa"
7621 * have been aligned.
7623 * Construct an isl_pw_aff on each of the sets in "domain" and
7624 * collect the results.
7626 static __isl_give isl_union_pw_aff
*isl_union_pw_aff_pw_aff_on_domain_aligned(
7627 __isl_take isl_union_set
*domain
, __isl_take isl_pw_aff
*pa
)
7629 struct isl_union_pw_aff_pw_aff_on_domain_data data
;
7632 space
= isl_union_set_get_space(domain
);
7633 data
.res
= isl_union_pw_aff_empty(space
);
7635 if (isl_union_set_foreach_set(domain
, &pw_aff_on_domain
, &data
) < 0)
7636 data
.res
= isl_union_pw_aff_free(data
.res
);
7637 isl_union_set_free(domain
);
7638 isl_pw_aff_free(pa
);
7642 /* Return a union piecewise affine expression
7643 * that is equal to "pa" on "domain".
7645 * Check that "pa" is a parametric expression,
7646 * align the parameters if needed and call
7647 * isl_union_pw_aff_pw_aff_on_domain_aligned.
7649 __isl_give isl_union_pw_aff
*isl_union_pw_aff_pw_aff_on_domain(
7650 __isl_take isl_union_set
*domain
, __isl_take isl_pw_aff
*pa
)
7653 isl_bool equal_params
;
7654 isl_space
*domain_space
, *pa_space
;
7656 pa_space
= isl_pw_aff_peek_space(pa
);
7657 is_set
= isl_space_is_set(pa_space
);
7661 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
7662 "expecting parametric expression", goto error
);
7664 domain_space
= isl_union_set_get_space(domain
);
7665 pa_space
= isl_pw_aff_get_space(pa
);
7666 equal_params
= isl_space_has_equal_params(domain_space
, pa_space
);
7667 if (equal_params
>= 0 && !equal_params
) {
7670 space
= isl_space_align_params(domain_space
, pa_space
);
7671 pa
= isl_pw_aff_align_params(pa
, isl_space_copy(space
));
7672 domain
= isl_union_set_align_params(domain
, space
);
7674 isl_space_free(domain_space
);
7675 isl_space_free(pa_space
);
7678 if (equal_params
< 0)
7680 return isl_union_pw_aff_pw_aff_on_domain_aligned(domain
, pa
);
7682 isl_union_set_free(domain
);
7683 isl_pw_aff_free(pa
);
7687 /* Internal data structure for isl_union_pw_aff_val_on_domain.
7688 * "v" is the value that the resulting isl_union_pw_aff needs to attain.
7689 * "res" collects the results.
7691 struct isl_union_pw_aff_val_on_domain_data
{
7693 isl_union_pw_aff
*res
;
7696 /* Construct a piecewise affine expression that is equal to data->v
7697 * on "domain" and add the result to data->res.
7699 static isl_stat
pw_aff_val_on_domain(__isl_take isl_set
*domain
, void *user
)
7701 struct isl_union_pw_aff_val_on_domain_data
*data
= user
;
7705 v
= isl_val_copy(data
->v
);
7706 pa
= isl_pw_aff_val_on_domain(domain
, v
);
7707 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
7709 return data
->res
? isl_stat_ok
: isl_stat_error
;
7712 /* Return a union piecewise affine expression
7713 * that is equal to "v" on "domain".
7715 * Construct an isl_pw_aff on each of the sets in "domain" and
7716 * collect the results.
7718 __isl_give isl_union_pw_aff
*isl_union_pw_aff_val_on_domain(
7719 __isl_take isl_union_set
*domain
, __isl_take isl_val
*v
)
7721 struct isl_union_pw_aff_val_on_domain_data data
;
7724 space
= isl_union_set_get_space(domain
);
7725 data
.res
= isl_union_pw_aff_empty(space
);
7727 if (isl_union_set_foreach_set(domain
, &pw_aff_val_on_domain
, &data
) < 0)
7728 data
.res
= isl_union_pw_aff_free(data
.res
);
7729 isl_union_set_free(domain
);
7734 /* Construct a piecewise multi affine expression
7735 * that is equal to "pa" and add it to upma.
7737 static isl_stat
pw_multi_aff_from_pw_aff_entry(__isl_take isl_pw_aff
*pa
,
7740 isl_union_pw_multi_aff
**upma
= user
;
7741 isl_pw_multi_aff
*pma
;
7743 pma
= isl_pw_multi_aff_from_pw_aff(pa
);
7744 *upma
= isl_union_pw_multi_aff_add_pw_multi_aff(*upma
, pma
);
7746 return *upma
? isl_stat_ok
: isl_stat_error
;
7749 /* Construct and return a union piecewise multi affine expression
7750 * that is equal to the given union piecewise affine expression.
7752 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_union_pw_aff(
7753 __isl_take isl_union_pw_aff
*upa
)
7756 isl_union_pw_multi_aff
*upma
;
7761 space
= isl_union_pw_aff_get_space(upa
);
7762 upma
= isl_union_pw_multi_aff_empty(space
);
7764 if (isl_union_pw_aff_foreach_pw_aff(upa
,
7765 &pw_multi_aff_from_pw_aff_entry
, &upma
) < 0)
7766 upma
= isl_union_pw_multi_aff_free(upma
);
7768 isl_union_pw_aff_free(upa
);
7772 /* Compute the set of elements in the domain of "pa" where it is zero and
7773 * add this set to "uset".
7775 static isl_stat
zero_union_set(__isl_take isl_pw_aff
*pa
, void *user
)
7777 isl_union_set
**uset
= (isl_union_set
**)user
;
7779 *uset
= isl_union_set_add_set(*uset
, isl_pw_aff_zero_set(pa
));
7781 return *uset
? isl_stat_ok
: isl_stat_error
;
7784 /* Return a union set containing those elements in the domain
7785 * of "upa" where it is zero.
7787 __isl_give isl_union_set
*isl_union_pw_aff_zero_union_set(
7788 __isl_take isl_union_pw_aff
*upa
)
7790 isl_union_set
*zero
;
7792 zero
= isl_union_set_empty(isl_union_pw_aff_get_space(upa
));
7793 if (isl_union_pw_aff_foreach_pw_aff(upa
, &zero_union_set
, &zero
) < 0)
7794 zero
= isl_union_set_free(zero
);
7796 isl_union_pw_aff_free(upa
);
7800 /* Internal data structure for isl_union_pw_aff_pullback_union_pw_multi_aff.
7801 * upma is the function that is plugged in.
7802 * pa is the current part of the function in which upma is plugged in.
7803 * res collects the results.
7805 struct isl_union_pw_aff_pullback_upma_data
{
7806 isl_union_pw_multi_aff
*upma
;
7808 isl_union_pw_aff
*res
;
7811 /* Check if "pma" can be plugged into data->pa.
7812 * If so, perform the pullback and add the result to data->res.
7814 static isl_stat
pa_pb_pma(__isl_take isl_pw_multi_aff
*pma
, void *user
)
7816 struct isl_union_pw_aff_pullback_upma_data
*data
= user
;
7819 if (!isl_space_tuple_is_equal(data
->pa
->dim
, isl_dim_in
,
7820 pma
->dim
, isl_dim_out
)) {
7821 isl_pw_multi_aff_free(pma
);
7825 pa
= isl_pw_aff_copy(data
->pa
);
7826 pa
= isl_pw_aff_pullback_pw_multi_aff(pa
, pma
);
7828 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
7830 return data
->res
? isl_stat_ok
: isl_stat_error
;
7833 /* Check if any of the elements of data->upma can be plugged into pa,
7834 * add if so add the result to data->res.
7836 static isl_stat
upa_pb_upma(__isl_take isl_pw_aff
*pa
, void *user
)
7838 struct isl_union_pw_aff_pullback_upma_data
*data
= user
;
7842 r
= isl_union_pw_multi_aff_foreach_pw_multi_aff(data
->upma
,
7844 isl_pw_aff_free(pa
);
7849 /* Compute the pullback of "upa" by the function represented by "upma".
7850 * In other words, plug in "upma" in "upa". The result contains
7851 * expressions defined over the domain space of "upma".
7853 * Run over all pairs of elements in "upa" and "upma", perform
7854 * the pullback when appropriate and collect the results.
7855 * If the hash value were based on the domain space rather than
7856 * the function space, then we could run through all elements
7857 * of "upma" and directly pick out the corresponding element of "upa".
7859 __isl_give isl_union_pw_aff
*isl_union_pw_aff_pullback_union_pw_multi_aff(
7860 __isl_take isl_union_pw_aff
*upa
,
7861 __isl_take isl_union_pw_multi_aff
*upma
)
7863 struct isl_union_pw_aff_pullback_upma_data data
= { NULL
, NULL
};
7866 space
= isl_union_pw_multi_aff_get_space(upma
);
7867 upa
= isl_union_pw_aff_align_params(upa
, space
);
7868 space
= isl_union_pw_aff_get_space(upa
);
7869 upma
= isl_union_pw_multi_aff_align_params(upma
, space
);
7875 data
.res
= isl_union_pw_aff_alloc_same_size(upa
);
7876 if (isl_union_pw_aff_foreach_pw_aff(upa
, &upa_pb_upma
, &data
) < 0)
7877 data
.res
= isl_union_pw_aff_free(data
.res
);
7879 isl_union_pw_aff_free(upa
);
7880 isl_union_pw_multi_aff_free(upma
);
7883 isl_union_pw_aff_free(upa
);
7884 isl_union_pw_multi_aff_free(upma
);
7889 #define BASE union_pw_aff
7891 #define DOMBASE union_set
7893 #include <isl_multi_explicit_domain.c>
7894 #include <isl_multi_union_pw_aff_explicit_domain.c>
7895 #include <isl_multi_templ.c>
7896 #include <isl_multi_apply_set.c>
7897 #include <isl_multi_apply_union_set.c>
7898 #include <isl_multi_coalesce.c>
7899 #include <isl_multi_floor.c>
7900 #include <isl_multi_from_base_templ.c>
7901 #include <isl_multi_gist.c>
7902 #include <isl_multi_align_set.c>
7903 #include <isl_multi_align_union_set.c>
7904 #include <isl_multi_intersect.c>
7906 /* Does "mupa" have a non-trivial explicit domain?
7908 * The explicit domain, if present, is trivial if it represents
7909 * an (obviously) universe parameter set.
7911 isl_bool
isl_multi_union_pw_aff_has_non_trivial_domain(
7912 __isl_keep isl_multi_union_pw_aff
*mupa
)
7914 isl_bool is_params
, trivial
;
7918 return isl_bool_error
;
7919 if (!isl_multi_union_pw_aff_has_explicit_domain(mupa
))
7920 return isl_bool_false
;
7921 is_params
= isl_union_set_is_params(mupa
->u
.dom
);
7922 if (is_params
< 0 || !is_params
)
7923 return isl_bool_not(is_params
);
7924 set
= isl_set_from_union_set(isl_union_set_copy(mupa
->u
.dom
));
7925 trivial
= isl_set_plain_is_universe(set
);
7927 return isl_bool_not(trivial
);
7930 /* Construct a multiple union piecewise affine expression
7931 * in the given space with value zero in each of the output dimensions.
7933 * Since there is no canonical zero value for
7934 * a union piecewise affine expression, we can only construct
7935 * a zero-dimensional "zero" value.
7937 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_zero(
7938 __isl_take isl_space
*space
)
7945 params
= isl_space_is_params(space
);
7949 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
7950 "expecting proper set space", goto error
);
7951 if (!isl_space_is_set(space
))
7952 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
7953 "expecting set space", goto error
);
7954 if (isl_space_dim(space
, isl_dim_out
) != 0)
7955 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
7956 "expecting 0D space", goto error
);
7958 return isl_multi_union_pw_aff_alloc(space
);
7960 isl_space_free(space
);
7964 /* Compute the sum of "mupa1" and "mupa2" on the union of their domains,
7965 * with the actual sum on the shared domain and
7966 * the defined expression on the symmetric difference of the domains.
7968 * We simply iterate over the elements in both arguments and
7969 * call isl_union_pw_aff_union_add on each of them, if there is
7970 * at least one element.
7972 * Otherwise, the two expressions have an explicit domain and
7973 * the union of these explicit domains is computed.
7974 * This assumes that the explicit domains are either both in terms
7975 * of specific domains elements or both in terms of parameters.
7976 * However, if one of the expressions does not have any constraints
7977 * on its explicit domain, then this is allowed as well and the result
7978 * is the expression with no constraints on its explicit domain.
7980 static __isl_give isl_multi_union_pw_aff
*
7981 isl_multi_union_pw_aff_union_add_aligned(
7982 __isl_take isl_multi_union_pw_aff
*mupa1
,
7983 __isl_take isl_multi_union_pw_aff
*mupa2
)
7985 isl_bool has_domain
, is_params1
, is_params2
;
7987 if (isl_multi_union_pw_aff_check_equal_space(mupa1
, mupa2
) < 0)
7990 return isl_multi_union_pw_aff_bin_op(mupa1
, mupa2
,
7991 &isl_union_pw_aff_union_add
);
7992 if (isl_multi_union_pw_aff_check_has_explicit_domain(mupa1
) < 0 ||
7993 isl_multi_union_pw_aff_check_has_explicit_domain(mupa2
) < 0)
7996 has_domain
= isl_multi_union_pw_aff_has_non_trivial_domain(mupa1
);
8000 isl_multi_union_pw_aff_free(mupa2
);
8003 has_domain
= isl_multi_union_pw_aff_has_non_trivial_domain(mupa2
);
8007 isl_multi_union_pw_aff_free(mupa1
);
8011 is_params1
= isl_union_set_is_params(mupa1
->u
.dom
);
8012 is_params2
= isl_union_set_is_params(mupa2
->u
.dom
);
8013 if (is_params1
< 0 || is_params2
< 0)
8015 if (is_params1
!= is_params2
)
8016 isl_die(isl_multi_union_pw_aff_get_ctx(mupa1
),
8018 "cannot compute union of concrete domain and "
8019 "parameter constraints", goto error
);
8020 mupa1
= isl_multi_union_pw_aff_cow(mupa1
);
8023 mupa1
->u
.dom
= isl_union_set_union(mupa1
->u
.dom
,
8024 isl_union_set_copy(mupa2
->u
.dom
));
8027 isl_multi_union_pw_aff_free(mupa2
);
8030 isl_multi_union_pw_aff_free(mupa1
);
8031 isl_multi_union_pw_aff_free(mupa2
);
8035 /* Compute the sum of "mupa1" and "mupa2" on the union of their domains,
8036 * with the actual sum on the shared domain and
8037 * the defined expression on the symmetric difference of the domains.
8039 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_union_add(
8040 __isl_take isl_multi_union_pw_aff
*mupa1
,
8041 __isl_take isl_multi_union_pw_aff
*mupa2
)
8043 return isl_multi_union_pw_aff_align_params_multi_multi_and(mupa1
, mupa2
,
8044 &isl_multi_union_pw_aff_union_add_aligned
);
8047 /* Construct and return a multi union piecewise affine expression
8048 * that is equal to the given multi affine expression.
8050 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_from_multi_aff(
8051 __isl_take isl_multi_aff
*ma
)
8053 isl_multi_pw_aff
*mpa
;
8055 mpa
= isl_multi_pw_aff_from_multi_aff(ma
);
8056 return isl_multi_union_pw_aff_from_multi_pw_aff(mpa
);
8059 /* Construct and return a multi union piecewise affine expression
8060 * that is equal to the given multi piecewise affine expression.
8062 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_from_multi_pw_aff(
8063 __isl_take isl_multi_pw_aff
*mpa
)
8067 isl_multi_union_pw_aff
*mupa
;
8072 space
= isl_multi_pw_aff_get_space(mpa
);
8073 space
= isl_space_range(space
);
8074 mupa
= isl_multi_union_pw_aff_alloc(space
);
8076 n
= isl_multi_pw_aff_dim(mpa
, isl_dim_out
);
8077 for (i
= 0; i
< n
; ++i
) {
8079 isl_union_pw_aff
*upa
;
8081 pa
= isl_multi_pw_aff_get_pw_aff(mpa
, i
);
8082 upa
= isl_union_pw_aff_from_pw_aff(pa
);
8083 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
8086 isl_multi_pw_aff_free(mpa
);
8091 /* Extract the range space of "pma" and assign it to *space.
8092 * If *space has already been set (through a previous call to this function),
8093 * then check that the range space is the same.
8095 static isl_stat
extract_space(__isl_take isl_pw_multi_aff
*pma
, void *user
)
8097 isl_space
**space
= user
;
8098 isl_space
*pma_space
;
8101 pma_space
= isl_space_range(isl_pw_multi_aff_get_space(pma
));
8102 isl_pw_multi_aff_free(pma
);
8105 return isl_stat_error
;
8111 equal
= isl_space_is_equal(pma_space
, *space
);
8112 isl_space_free(pma_space
);
8115 return isl_stat_error
;
8117 isl_die(isl_space_get_ctx(*space
), isl_error_invalid
,
8118 "range spaces not the same", return isl_stat_error
);
8122 /* Construct and return a multi union piecewise affine expression
8123 * that is equal to the given union piecewise multi affine expression.
8125 * In order to be able to perform the conversion, the input
8126 * needs to be non-empty and may only involve a single range space.
8128 * If the resulting multi union piecewise affine expression has
8129 * an explicit domain, then assign it the domain of the input.
8130 * In other cases, the domain is stored in the individual elements.
8132 __isl_give isl_multi_union_pw_aff
*
8133 isl_multi_union_pw_aff_from_union_pw_multi_aff(
8134 __isl_take isl_union_pw_multi_aff
*upma
)
8136 isl_space
*space
= NULL
;
8137 isl_multi_union_pw_aff
*mupa
;
8142 if (isl_union_pw_multi_aff_n_pw_multi_aff(upma
) == 0)
8143 isl_die(isl_union_pw_multi_aff_get_ctx(upma
), isl_error_invalid
,
8144 "cannot extract range space from empty input",
8146 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma
, &extract_space
,
8153 n
= isl_space_dim(space
, isl_dim_set
);
8154 mupa
= isl_multi_union_pw_aff_alloc(space
);
8156 for (i
= 0; i
< n
; ++i
) {
8157 isl_union_pw_aff
*upa
;
8159 upa
= isl_union_pw_multi_aff_get_union_pw_aff(upma
, i
);
8160 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
8162 if (isl_multi_union_pw_aff_has_explicit_domain(mupa
)) {
8164 isl_union_pw_multi_aff
*copy
;
8166 copy
= isl_union_pw_multi_aff_copy(upma
);
8167 dom
= isl_union_pw_multi_aff_domain(copy
);
8168 mupa
= isl_multi_union_pw_aff_intersect_domain(mupa
, dom
);
8171 isl_union_pw_multi_aff_free(upma
);
8174 isl_space_free(space
);
8175 isl_union_pw_multi_aff_free(upma
);
8179 /* Try and create an isl_multi_union_pw_aff that is equivalent
8180 * to the given isl_union_map.
8181 * The isl_union_map is required to be single-valued in each space.
8182 * Moreover, it cannot be empty and all range spaces need to be the same.
8183 * Otherwise, an error is produced.
8185 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_from_union_map(
8186 __isl_take isl_union_map
*umap
)
8188 isl_union_pw_multi_aff
*upma
;
8190 upma
= isl_union_pw_multi_aff_from_union_map(umap
);
8191 return isl_multi_union_pw_aff_from_union_pw_multi_aff(upma
);
8194 /* Return a multiple union piecewise affine expression
8195 * that is equal to "mv" on "domain", assuming "domain" and "mv"
8196 * have been aligned.
8198 * If the resulting multi union piecewise affine expression has
8199 * an explicit domain, then assign it the input domain.
8200 * In other cases, the domain is stored in the individual elements.
8202 static __isl_give isl_multi_union_pw_aff
*
8203 isl_multi_union_pw_aff_multi_val_on_domain_aligned(
8204 __isl_take isl_union_set
*domain
, __isl_take isl_multi_val
*mv
)
8208 isl_multi_union_pw_aff
*mupa
;
8213 n
= isl_multi_val_dim(mv
, isl_dim_set
);
8214 space
= isl_multi_val_get_space(mv
);
8215 mupa
= isl_multi_union_pw_aff_alloc(space
);
8216 for (i
= 0; i
< n
; ++i
) {
8218 isl_union_pw_aff
*upa
;
8220 v
= isl_multi_val_get_val(mv
, i
);
8221 upa
= isl_union_pw_aff_val_on_domain(isl_union_set_copy(domain
),
8223 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
8225 if (isl_multi_union_pw_aff_has_explicit_domain(mupa
))
8226 mupa
= isl_multi_union_pw_aff_intersect_domain(mupa
,
8227 isl_union_set_copy(domain
));
8229 isl_union_set_free(domain
);
8230 isl_multi_val_free(mv
);
8233 isl_union_set_free(domain
);
8234 isl_multi_val_free(mv
);
8238 /* Return a multiple union piecewise affine expression
8239 * that is equal to "mv" on "domain".
8241 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_multi_val_on_domain(
8242 __isl_take isl_union_set
*domain
, __isl_take isl_multi_val
*mv
)
8244 isl_bool equal_params
;
8248 equal_params
= isl_space_has_equal_params(domain
->dim
, mv
->space
);
8249 if (equal_params
< 0)
8252 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(
8254 domain
= isl_union_set_align_params(domain
,
8255 isl_multi_val_get_space(mv
));
8256 mv
= isl_multi_val_align_params(mv
, isl_union_set_get_space(domain
));
8257 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(domain
, mv
);
8259 isl_union_set_free(domain
);
8260 isl_multi_val_free(mv
);
8264 /* Return a multiple union piecewise affine expression
8265 * that is equal to "ma" on "domain".
8267 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_multi_aff_on_domain(
8268 __isl_take isl_union_set
*domain
, __isl_take isl_multi_aff
*ma
)
8270 isl_pw_multi_aff
*pma
;
8272 pma
= isl_pw_multi_aff_from_multi_aff(ma
);
8273 return isl_multi_union_pw_aff_pw_multi_aff_on_domain(domain
, pma
);
8276 /* Return a multiple union piecewise affine expression
8277 * that is equal to "pma" on "domain", assuming "domain" and "pma"
8278 * have been aligned.
8280 * If the resulting multi union piecewise affine expression has
8281 * an explicit domain, then assign it the input domain.
8282 * In other cases, the domain is stored in the individual elements.
8284 static __isl_give isl_multi_union_pw_aff
*
8285 isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(
8286 __isl_take isl_union_set
*domain
, __isl_take isl_pw_multi_aff
*pma
)
8290 isl_multi_union_pw_aff
*mupa
;
8292 if (!domain
|| !pma
)
8295 n
= isl_pw_multi_aff_dim(pma
, isl_dim_set
);
8296 space
= isl_pw_multi_aff_get_space(pma
);
8297 mupa
= isl_multi_union_pw_aff_alloc(space
);
8298 for (i
= 0; i
< n
; ++i
) {
8300 isl_union_pw_aff
*upa
;
8302 pa
= isl_pw_multi_aff_get_pw_aff(pma
, i
);
8303 upa
= isl_union_pw_aff_pw_aff_on_domain(
8304 isl_union_set_copy(domain
), pa
);
8305 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
8307 if (isl_multi_union_pw_aff_has_explicit_domain(mupa
))
8308 mupa
= isl_multi_union_pw_aff_intersect_domain(mupa
,
8309 isl_union_set_copy(domain
));
8311 isl_union_set_free(domain
);
8312 isl_pw_multi_aff_free(pma
);
8315 isl_union_set_free(domain
);
8316 isl_pw_multi_aff_free(pma
);
8320 /* Return a multiple union piecewise affine expression
8321 * that is equal to "pma" on "domain".
8323 __isl_give isl_multi_union_pw_aff
*
8324 isl_multi_union_pw_aff_pw_multi_aff_on_domain(__isl_take isl_union_set
*domain
,
8325 __isl_take isl_pw_multi_aff
*pma
)
8327 isl_bool equal_params
;
8330 space
= isl_pw_multi_aff_peek_space(pma
);
8331 equal_params
= isl_union_set_space_has_equal_params(domain
, space
);
8332 if (equal_params
< 0)
8335 return isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(
8337 domain
= isl_union_set_align_params(domain
,
8338 isl_pw_multi_aff_get_space(pma
));
8339 pma
= isl_pw_multi_aff_align_params(pma
,
8340 isl_union_set_get_space(domain
));
8341 return isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(domain
,
8344 isl_union_set_free(domain
);
8345 isl_pw_multi_aff_free(pma
);
8349 /* Return a union set containing those elements in the domains
8350 * of the elements of "mupa" where they are all zero.
8352 * If there are no elements, then simply return the entire domain.
8354 __isl_give isl_union_set
*isl_multi_union_pw_aff_zero_union_set(
8355 __isl_take isl_multi_union_pw_aff
*mupa
)
8358 isl_union_pw_aff
*upa
;
8359 isl_union_set
*zero
;
8364 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8366 return isl_multi_union_pw_aff_domain(mupa
);
8368 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, 0);
8369 zero
= isl_union_pw_aff_zero_union_set(upa
);
8371 for (i
= 1; i
< n
; ++i
) {
8372 isl_union_set
*zero_i
;
8374 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
8375 zero_i
= isl_union_pw_aff_zero_union_set(upa
);
8377 zero
= isl_union_set_intersect(zero
, zero_i
);
8380 isl_multi_union_pw_aff_free(mupa
);
8384 /* Construct a union map mapping the shared domain
8385 * of the union piecewise affine expressions to the range of "mupa"
8386 * in the special case of a 0D multi union piecewise affine expression.
8388 * Construct a map between the explicit domain of "mupa" and
8390 * Note that this assumes that the domain consists of explicit elements.
8392 static __isl_give isl_union_map
*isl_union_map_from_multi_union_pw_aff_0D(
8393 __isl_take isl_multi_union_pw_aff
*mupa
)
8397 isl_union_set
*dom
, *ran
;
8399 space
= isl_multi_union_pw_aff_get_space(mupa
);
8400 dom
= isl_multi_union_pw_aff_domain(mupa
);
8401 ran
= isl_union_set_from_set(isl_set_universe(space
));
8403 is_params
= isl_union_set_is_params(dom
);
8405 dom
= isl_union_set_free(dom
);
8407 isl_die(isl_union_set_get_ctx(dom
), isl_error_invalid
,
8408 "cannot create union map from expression without "
8409 "explicit domain elements",
8410 dom
= isl_union_set_free(dom
));
8412 return isl_union_map_from_domain_and_range(dom
, ran
);
8415 /* Construct a union map mapping the shared domain
8416 * of the union piecewise affine expressions to the range of "mupa"
8417 * with each dimension in the range equated to the
8418 * corresponding union piecewise affine expression.
8420 * If the input is zero-dimensional, then construct a mapping
8421 * from its explicit domain.
8423 __isl_give isl_union_map
*isl_union_map_from_multi_union_pw_aff(
8424 __isl_take isl_multi_union_pw_aff
*mupa
)
8428 isl_union_map
*umap
;
8429 isl_union_pw_aff
*upa
;
8434 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8436 return isl_union_map_from_multi_union_pw_aff_0D(mupa
);
8438 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, 0);
8439 umap
= isl_union_map_from_union_pw_aff(upa
);
8441 for (i
= 1; i
< n
; ++i
) {
8442 isl_union_map
*umap_i
;
8444 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
8445 umap_i
= isl_union_map_from_union_pw_aff(upa
);
8446 umap
= isl_union_map_flat_range_product(umap
, umap_i
);
8449 space
= isl_multi_union_pw_aff_get_space(mupa
);
8450 umap
= isl_union_map_reset_range_space(umap
, space
);
8452 isl_multi_union_pw_aff_free(mupa
);
8456 /* Internal data structure for isl_union_pw_multi_aff_reset_range_space.
8457 * "range" is the space from which to set the range space.
8458 * "res" collects the results.
8460 struct isl_union_pw_multi_aff_reset_range_space_data
{
8462 isl_union_pw_multi_aff
*res
;
8465 /* Replace the range space of "pma" by the range space of data->range and
8466 * add the result to data->res.
8468 static isl_stat
reset_range_space(__isl_take isl_pw_multi_aff
*pma
, void *user
)
8470 struct isl_union_pw_multi_aff_reset_range_space_data
*data
= user
;
8473 space
= isl_pw_multi_aff_get_space(pma
);
8474 space
= isl_space_domain(space
);
8475 space
= isl_space_extend_domain_with_range(space
,
8476 isl_space_copy(data
->range
));
8477 pma
= isl_pw_multi_aff_reset_space(pma
, space
);
8478 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma
);
8480 return data
->res
? isl_stat_ok
: isl_stat_error
;
8483 /* Replace the range space of all the piecewise affine expressions in "upma" by
8484 * the range space of "space".
8486 * This assumes that all these expressions have the same output dimension.
8488 * Since the spaces of the expressions change, so do their hash values.
8489 * We therefore need to create a new isl_union_pw_multi_aff.
8490 * Note that the hash value is currently computed based on the entire
8491 * space even though there can only be a single expression with a given
8494 static __isl_give isl_union_pw_multi_aff
*
8495 isl_union_pw_multi_aff_reset_range_space(
8496 __isl_take isl_union_pw_multi_aff
*upma
, __isl_take isl_space
*space
)
8498 struct isl_union_pw_multi_aff_reset_range_space_data data
= { space
};
8499 isl_space
*space_upma
;
8501 space_upma
= isl_union_pw_multi_aff_get_space(upma
);
8502 data
.res
= isl_union_pw_multi_aff_empty(space_upma
);
8503 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma
,
8504 &reset_range_space
, &data
) < 0)
8505 data
.res
= isl_union_pw_multi_aff_free(data
.res
);
8507 isl_space_free(space
);
8508 isl_union_pw_multi_aff_free(upma
);
8512 /* Construct and return a union piecewise multi affine expression
8513 * that is equal to the given multi union piecewise affine expression,
8514 * in the special case of a 0D multi union piecewise affine expression.
8516 * Construct a union piecewise multi affine expression
8517 * on top of the explicit domain of the input.
8519 __isl_give isl_union_pw_multi_aff
*
8520 isl_union_pw_multi_aff_from_multi_union_pw_aff_0D(
8521 __isl_take isl_multi_union_pw_aff
*mupa
)
8525 isl_union_set
*domain
;
8527 space
= isl_multi_union_pw_aff_get_space(mupa
);
8528 mv
= isl_multi_val_zero(space
);
8529 domain
= isl_multi_union_pw_aff_domain(mupa
);
8530 return isl_union_pw_multi_aff_multi_val_on_domain(domain
, mv
);
8533 /* Construct and return a union piecewise multi affine expression
8534 * that is equal to the given multi union piecewise affine expression.
8536 * If the input is zero-dimensional, then
8537 * construct a union piecewise multi affine expression
8538 * on top of the explicit domain of the input.
8540 __isl_give isl_union_pw_multi_aff
*
8541 isl_union_pw_multi_aff_from_multi_union_pw_aff(
8542 __isl_take isl_multi_union_pw_aff
*mupa
)
8546 isl_union_pw_multi_aff
*upma
;
8547 isl_union_pw_aff
*upa
;
8552 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8554 return isl_union_pw_multi_aff_from_multi_union_pw_aff_0D(mupa
);
8556 space
= isl_multi_union_pw_aff_get_space(mupa
);
8557 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, 0);
8558 upma
= isl_union_pw_multi_aff_from_union_pw_aff(upa
);
8560 for (i
= 1; i
< n
; ++i
) {
8561 isl_union_pw_multi_aff
*upma_i
;
8563 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
8564 upma_i
= isl_union_pw_multi_aff_from_union_pw_aff(upa
);
8565 upma
= isl_union_pw_multi_aff_flat_range_product(upma
, upma_i
);
8568 upma
= isl_union_pw_multi_aff_reset_range_space(upma
, space
);
8570 isl_multi_union_pw_aff_free(mupa
);
8574 /* Intersect the range of "mupa" with "range",
8575 * in the special case where "mupa" is 0D.
8577 * Intersect the domain of "mupa" with the constraints on the parameters
8580 static __isl_give isl_multi_union_pw_aff
*mupa_intersect_range_0D(
8581 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_set
*range
)
8583 range
= isl_set_params(range
);
8584 mupa
= isl_multi_union_pw_aff_intersect_params(mupa
, range
);
8588 /* Intersect the range of "mupa" with "range".
8589 * That is, keep only those domain elements that have a function value
8592 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_intersect_range(
8593 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_set
*range
)
8595 isl_union_pw_multi_aff
*upma
;
8596 isl_union_set
*domain
;
8601 if (!mupa
|| !range
)
8604 space
= isl_set_get_space(range
);
8605 match
= isl_space_tuple_is_equal(mupa
->space
, isl_dim_set
,
8606 space
, isl_dim_set
);
8607 isl_space_free(space
);
8611 isl_die(isl_multi_union_pw_aff_get_ctx(mupa
), isl_error_invalid
,
8612 "space don't match", goto error
);
8613 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8615 return mupa_intersect_range_0D(mupa
, range
);
8617 upma
= isl_union_pw_multi_aff_from_multi_union_pw_aff(
8618 isl_multi_union_pw_aff_copy(mupa
));
8619 domain
= isl_union_set_from_set(range
);
8620 domain
= isl_union_set_preimage_union_pw_multi_aff(domain
, upma
);
8621 mupa
= isl_multi_union_pw_aff_intersect_domain(mupa
, domain
);
8625 isl_multi_union_pw_aff_free(mupa
);
8626 isl_set_free(range
);
8630 /* Return the shared domain of the elements of "mupa",
8631 * in the special case where "mupa" is zero-dimensional.
8633 * Return the explicit domain of "mupa".
8634 * Note that this domain may be a parameter set, either
8635 * because "mupa" is meant to live in a set space or
8636 * because no explicit domain has been set.
8638 __isl_give isl_union_set
*isl_multi_union_pw_aff_domain_0D(
8639 __isl_take isl_multi_union_pw_aff
*mupa
)
8643 dom
= isl_multi_union_pw_aff_get_explicit_domain(mupa
);
8644 isl_multi_union_pw_aff_free(mupa
);
8649 /* Return the shared domain of the elements of "mupa".
8651 * If "mupa" is zero-dimensional, then return its explicit domain.
8653 __isl_give isl_union_set
*isl_multi_union_pw_aff_domain(
8654 __isl_take isl_multi_union_pw_aff
*mupa
)
8657 isl_union_pw_aff
*upa
;
8663 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8665 return isl_multi_union_pw_aff_domain_0D(mupa
);
8667 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, 0);
8668 dom
= isl_union_pw_aff_domain(upa
);
8669 for (i
= 1; i
< n
; ++i
) {
8670 isl_union_set
*dom_i
;
8672 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
8673 dom_i
= isl_union_pw_aff_domain(upa
);
8674 dom
= isl_union_set_intersect(dom
, dom_i
);
8677 isl_multi_union_pw_aff_free(mupa
);
8681 /* Apply "aff" to "mupa". The space of "mupa" is equal to the domain of "aff".
8682 * In particular, the spaces have been aligned.
8683 * The result is defined over the shared domain of the elements of "mupa"
8685 * We first extract the parametric constant part of "aff" and
8686 * define that over the shared domain.
8687 * Then we iterate over all input dimensions of "aff" and add the corresponding
8688 * multiples of the elements of "mupa".
8689 * Finally, we consider the integer divisions, calling the function
8690 * recursively to obtain an isl_union_pw_aff corresponding to the
8691 * integer division argument.
8693 static __isl_give isl_union_pw_aff
*multi_union_pw_aff_apply_aff(
8694 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_aff
*aff
)
8697 isl_union_pw_aff
*upa
;
8698 isl_union_set
*uset
;
8702 n_in
= isl_aff_dim(aff
, isl_dim_in
);
8703 n_div
= isl_aff_dim(aff
, isl_dim_div
);
8705 uset
= isl_multi_union_pw_aff_domain(isl_multi_union_pw_aff_copy(mupa
));
8706 cst
= isl_aff_copy(aff
);
8707 cst
= isl_aff_drop_dims(cst
, isl_dim_div
, 0, n_div
);
8708 cst
= isl_aff_drop_dims(cst
, isl_dim_in
, 0, n_in
);
8709 cst
= isl_aff_project_domain_on_params(cst
);
8710 upa
= isl_union_pw_aff_aff_on_domain(uset
, cst
);
8712 for (i
= 0; i
< n_in
; ++i
) {
8713 isl_union_pw_aff
*upa_i
;
8715 if (!isl_aff_involves_dims(aff
, isl_dim_in
, i
, 1))
8717 v
= isl_aff_get_coefficient_val(aff
, isl_dim_in
, i
);
8718 upa_i
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
8719 upa_i
= isl_union_pw_aff_scale_val(upa_i
, v
);
8720 upa
= isl_union_pw_aff_add(upa
, upa_i
);
8723 for (i
= 0; i
< n_div
; ++i
) {
8725 isl_union_pw_aff
*upa_i
;
8727 if (!isl_aff_involves_dims(aff
, isl_dim_div
, i
, 1))
8729 div
= isl_aff_get_div(aff
, i
);
8730 upa_i
= multi_union_pw_aff_apply_aff(
8731 isl_multi_union_pw_aff_copy(mupa
), div
);
8732 upa_i
= isl_union_pw_aff_floor(upa_i
);
8733 v
= isl_aff_get_coefficient_val(aff
, isl_dim_div
, i
);
8734 upa_i
= isl_union_pw_aff_scale_val(upa_i
, v
);
8735 upa
= isl_union_pw_aff_add(upa
, upa_i
);
8738 isl_multi_union_pw_aff_free(mupa
);
8744 /* Apply "aff" to "mupa". The space of "mupa" needs to be compatible
8745 * with the domain of "aff".
8746 * Furthermore, the dimension of this space needs to be greater than zero.
8747 * The result is defined over the shared domain of the elements of "mupa"
8749 * We perform these checks and then hand over control to
8750 * multi_union_pw_aff_apply_aff.
8752 __isl_give isl_union_pw_aff
*isl_multi_union_pw_aff_apply_aff(
8753 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_aff
*aff
)
8755 isl_space
*space1
, *space2
;
8758 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
8759 isl_aff_get_space(aff
));
8760 aff
= isl_aff_align_params(aff
, isl_multi_union_pw_aff_get_space(mupa
));
8764 space1
= isl_multi_union_pw_aff_get_space(mupa
);
8765 space2
= isl_aff_get_domain_space(aff
);
8766 equal
= isl_space_is_equal(space1
, space2
);
8767 isl_space_free(space1
);
8768 isl_space_free(space2
);
8772 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
8773 "spaces don't match", goto error
);
8774 if (isl_aff_dim(aff
, isl_dim_in
) == 0)
8775 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
8776 "cannot determine domains", goto error
);
8778 return multi_union_pw_aff_apply_aff(mupa
, aff
);
8780 isl_multi_union_pw_aff_free(mupa
);
8785 /* Apply "ma" to "mupa", in the special case where "mupa" is 0D.
8786 * The space of "mupa" is known to be compatible with the domain of "ma".
8788 * Construct an isl_multi_union_pw_aff that is equal to "ma"
8789 * on the domain of "mupa".
8791 static __isl_give isl_multi_union_pw_aff
*mupa_apply_multi_aff_0D(
8792 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_multi_aff
*ma
)
8796 dom
= isl_multi_union_pw_aff_domain(mupa
);
8797 ma
= isl_multi_aff_project_domain_on_params(ma
);
8799 return isl_multi_union_pw_aff_multi_aff_on_domain(dom
, ma
);
8802 /* Apply "ma" to "mupa". The space of "mupa" needs to be compatible
8803 * with the domain of "ma".
8804 * The result is defined over the shared domain of the elements of "mupa"
8806 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_apply_multi_aff(
8807 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_multi_aff
*ma
)
8809 isl_space
*space1
, *space2
;
8810 isl_multi_union_pw_aff
*res
;
8814 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
8815 isl_multi_aff_get_space(ma
));
8816 ma
= isl_multi_aff_align_params(ma
,
8817 isl_multi_union_pw_aff_get_space(mupa
));
8821 space1
= isl_multi_union_pw_aff_get_space(mupa
);
8822 space2
= isl_multi_aff_get_domain_space(ma
);
8823 equal
= isl_space_is_equal(space1
, space2
);
8824 isl_space_free(space1
);
8825 isl_space_free(space2
);
8829 isl_die(isl_multi_aff_get_ctx(ma
), isl_error_invalid
,
8830 "spaces don't match", goto error
);
8831 n_out
= isl_multi_aff_dim(ma
, isl_dim_out
);
8832 if (isl_multi_aff_dim(ma
, isl_dim_in
) == 0)
8833 return mupa_apply_multi_aff_0D(mupa
, ma
);
8835 space1
= isl_space_range(isl_multi_aff_get_space(ma
));
8836 res
= isl_multi_union_pw_aff_alloc(space1
);
8838 for (i
= 0; i
< n_out
; ++i
) {
8840 isl_union_pw_aff
*upa
;
8842 aff
= isl_multi_aff_get_aff(ma
, i
);
8843 upa
= multi_union_pw_aff_apply_aff(
8844 isl_multi_union_pw_aff_copy(mupa
), aff
);
8845 res
= isl_multi_union_pw_aff_set_union_pw_aff(res
, i
, upa
);
8848 isl_multi_aff_free(ma
);
8849 isl_multi_union_pw_aff_free(mupa
);
8852 isl_multi_union_pw_aff_free(mupa
);
8853 isl_multi_aff_free(ma
);
8857 /* Apply "pa" to "mupa", in the special case where "mupa" is 0D.
8858 * The space of "mupa" is known to be compatible with the domain of "pa".
8860 * Construct an isl_multi_union_pw_aff that is equal to "pa"
8861 * on the domain of "mupa".
8863 static __isl_give isl_union_pw_aff
*isl_multi_union_pw_aff_apply_pw_aff_0D(
8864 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_pw_aff
*pa
)
8868 dom
= isl_multi_union_pw_aff_domain(mupa
);
8869 pa
= isl_pw_aff_project_domain_on_params(pa
);
8871 return isl_union_pw_aff_pw_aff_on_domain(dom
, pa
);
8874 /* Apply "pa" to "mupa". The space of "mupa" needs to be compatible
8875 * with the domain of "pa".
8876 * Furthermore, the dimension of this space needs to be greater than zero.
8877 * The result is defined over the shared domain of the elements of "mupa"
8879 __isl_give isl_union_pw_aff
*isl_multi_union_pw_aff_apply_pw_aff(
8880 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_pw_aff
*pa
)
8884 isl_space
*space
, *space2
;
8885 isl_union_pw_aff
*upa
;
8887 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
8888 isl_pw_aff_get_space(pa
));
8889 pa
= isl_pw_aff_align_params(pa
,
8890 isl_multi_union_pw_aff_get_space(mupa
));
8894 space
= isl_multi_union_pw_aff_get_space(mupa
);
8895 space2
= isl_pw_aff_get_domain_space(pa
);
8896 equal
= isl_space_is_equal(space
, space2
);
8897 isl_space_free(space
);
8898 isl_space_free(space2
);
8902 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
8903 "spaces don't match", goto error
);
8904 if (isl_pw_aff_dim(pa
, isl_dim_in
) == 0)
8905 return isl_multi_union_pw_aff_apply_pw_aff_0D(mupa
, pa
);
8907 space
= isl_space_params(isl_multi_union_pw_aff_get_space(mupa
));
8908 upa
= isl_union_pw_aff_empty(space
);
8910 for (i
= 0; i
< pa
->n
; ++i
) {
8913 isl_multi_union_pw_aff
*mupa_i
;
8914 isl_union_pw_aff
*upa_i
;
8916 mupa_i
= isl_multi_union_pw_aff_copy(mupa
);
8917 domain
= isl_set_copy(pa
->p
[i
].set
);
8918 mupa_i
= isl_multi_union_pw_aff_intersect_range(mupa_i
, domain
);
8919 aff
= isl_aff_copy(pa
->p
[i
].aff
);
8920 upa_i
= multi_union_pw_aff_apply_aff(mupa_i
, aff
);
8921 upa
= isl_union_pw_aff_union_add(upa
, upa_i
);
8924 isl_multi_union_pw_aff_free(mupa
);
8925 isl_pw_aff_free(pa
);
8928 isl_multi_union_pw_aff_free(mupa
);
8929 isl_pw_aff_free(pa
);
8933 /* Apply "pma" to "mupa", in the special case where "mupa" is 0D.
8934 * The space of "mupa" is known to be compatible with the domain of "pma".
8936 * Construct an isl_multi_union_pw_aff that is equal to "pma"
8937 * on the domain of "mupa".
8939 static __isl_give isl_multi_union_pw_aff
*mupa_apply_pw_multi_aff_0D(
8940 __isl_take isl_multi_union_pw_aff
*mupa
,
8941 __isl_take isl_pw_multi_aff
*pma
)
8945 dom
= isl_multi_union_pw_aff_domain(mupa
);
8946 pma
= isl_pw_multi_aff_project_domain_on_params(pma
);
8948 return isl_multi_union_pw_aff_pw_multi_aff_on_domain(dom
, pma
);
8951 /* Apply "pma" to "mupa". The space of "mupa" needs to be compatible
8952 * with the domain of "pma".
8953 * The result is defined over the shared domain of the elements of "mupa"
8955 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_apply_pw_multi_aff(
8956 __isl_take isl_multi_union_pw_aff
*mupa
,
8957 __isl_take isl_pw_multi_aff
*pma
)
8959 isl_space
*space1
, *space2
;
8960 isl_multi_union_pw_aff
*res
;
8964 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
8965 isl_pw_multi_aff_get_space(pma
));
8966 pma
= isl_pw_multi_aff_align_params(pma
,
8967 isl_multi_union_pw_aff_get_space(mupa
));
8971 space1
= isl_multi_union_pw_aff_get_space(mupa
);
8972 space2
= isl_pw_multi_aff_get_domain_space(pma
);
8973 equal
= isl_space_is_equal(space1
, space2
);
8974 isl_space_free(space1
);
8975 isl_space_free(space2
);
8979 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
8980 "spaces don't match", goto error
);
8981 n_out
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
8982 if (isl_pw_multi_aff_dim(pma
, isl_dim_in
) == 0)
8983 return mupa_apply_pw_multi_aff_0D(mupa
, pma
);
8985 space1
= isl_space_range(isl_pw_multi_aff_get_space(pma
));
8986 res
= isl_multi_union_pw_aff_alloc(space1
);
8988 for (i
= 0; i
< n_out
; ++i
) {
8990 isl_union_pw_aff
*upa
;
8992 pa
= isl_pw_multi_aff_get_pw_aff(pma
, i
);
8993 upa
= isl_multi_union_pw_aff_apply_pw_aff(
8994 isl_multi_union_pw_aff_copy(mupa
), pa
);
8995 res
= isl_multi_union_pw_aff_set_union_pw_aff(res
, i
, upa
);
8998 isl_pw_multi_aff_free(pma
);
8999 isl_multi_union_pw_aff_free(mupa
);
9002 isl_multi_union_pw_aff_free(mupa
);
9003 isl_pw_multi_aff_free(pma
);
9007 /* Replace the explicit domain of "mupa" by its preimage under "upma".
9008 * If the explicit domain only keeps track of constraints on the parameters,
9009 * then only update those constraints.
9011 static __isl_give isl_multi_union_pw_aff
*preimage_explicit_domain(
9012 __isl_take isl_multi_union_pw_aff
*mupa
,
9013 __isl_keep isl_union_pw_multi_aff
*upma
)
9017 if (isl_multi_union_pw_aff_check_has_explicit_domain(mupa
) < 0)
9018 return isl_multi_union_pw_aff_free(mupa
);
9020 mupa
= isl_multi_union_pw_aff_cow(mupa
);
9024 is_params
= isl_union_set_is_params(mupa
->u
.dom
);
9026 return isl_multi_union_pw_aff_free(mupa
);
9028 upma
= isl_union_pw_multi_aff_copy(upma
);
9030 mupa
->u
.dom
= isl_union_set_intersect_params(mupa
->u
.dom
,
9031 isl_union_set_params(isl_union_pw_multi_aff_domain(upma
)));
9033 mupa
->u
.dom
= isl_union_set_preimage_union_pw_multi_aff(
9036 return isl_multi_union_pw_aff_free(mupa
);
9040 /* Compute the pullback of "mupa" by the function represented by "upma".
9041 * In other words, plug in "upma" in "mupa". The result contains
9042 * expressions defined over the domain space of "upma".
9044 * Run over all elements of "mupa" and plug in "upma" in each of them.
9046 * If "mupa" has an explicit domain, then it is this domain
9047 * that needs to undergo a pullback instead, i.e., a preimage.
9049 __isl_give isl_multi_union_pw_aff
*
9050 isl_multi_union_pw_aff_pullback_union_pw_multi_aff(
9051 __isl_take isl_multi_union_pw_aff
*mupa
,
9052 __isl_take isl_union_pw_multi_aff
*upma
)
9056 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
9057 isl_union_pw_multi_aff_get_space(upma
));
9058 upma
= isl_union_pw_multi_aff_align_params(upma
,
9059 isl_multi_union_pw_aff_get_space(mupa
));
9060 mupa
= isl_multi_union_pw_aff_cow(mupa
);
9064 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
9065 for (i
= 0; i
< n
; ++i
) {
9066 isl_union_pw_aff
*upa
;
9068 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
9069 upa
= isl_union_pw_aff_pullback_union_pw_multi_aff(upa
,
9070 isl_union_pw_multi_aff_copy(upma
));
9071 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
9074 if (isl_multi_union_pw_aff_has_explicit_domain(mupa
))
9075 mupa
= preimage_explicit_domain(mupa
, upma
);
9077 isl_union_pw_multi_aff_free(upma
);
9080 isl_multi_union_pw_aff_free(mupa
);
9081 isl_union_pw_multi_aff_free(upma
);
9085 /* Extract the sequence of elements in "mupa" with domain space "space"
9086 * (ignoring parameters).
9088 * For the elements of "mupa" that are not defined on the specified space,
9089 * the corresponding element in the result is empty.
9091 __isl_give isl_multi_pw_aff
*isl_multi_union_pw_aff_extract_multi_pw_aff(
9092 __isl_keep isl_multi_union_pw_aff
*mupa
, __isl_take isl_space
*space
)
9095 isl_space
*space_mpa
;
9096 isl_multi_pw_aff
*mpa
;
9098 if (!mupa
|| !space
)
9101 space_mpa
= isl_multi_union_pw_aff_get_space(mupa
);
9102 space
= isl_space_replace_params(space
, space_mpa
);
9103 space_mpa
= isl_space_map_from_domain_and_range(isl_space_copy(space
),
9105 mpa
= isl_multi_pw_aff_alloc(space_mpa
);
9107 space
= isl_space_from_domain(space
);
9108 space
= isl_space_add_dims(space
, isl_dim_out
, 1);
9109 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
9110 for (i
= 0; i
< n
; ++i
) {
9111 isl_union_pw_aff
*upa
;
9114 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
9115 pa
= isl_union_pw_aff_extract_pw_aff(upa
,
9116 isl_space_copy(space
));
9117 mpa
= isl_multi_pw_aff_set_pw_aff(mpa
, i
, pa
);
9118 isl_union_pw_aff_free(upa
);
9121 isl_space_free(space
);
9124 isl_space_free(space
);
9128 /* Evaluate the affine function "aff" in the void point "pnt".
9129 * In particular, return the value NaN.
9131 static __isl_give isl_val
*eval_void(__isl_take isl_aff
*aff
,
9132 __isl_take isl_point
*pnt
)
9136 ctx
= isl_point_get_ctx(pnt
);
9138 isl_point_free(pnt
);
9139 return isl_val_nan(ctx
);
9142 /* Evaluate the affine expression "aff"
9143 * in the coordinates (with denominator) "pnt".
9145 static __isl_give isl_val
*eval(__isl_keep isl_vec
*aff
,
9146 __isl_keep isl_vec
*pnt
)
9155 ctx
= isl_vec_get_ctx(aff
);
9158 isl_seq_inner_product(aff
->el
+ 1, pnt
->el
, pnt
->size
, &n
);
9159 isl_int_mul(d
, aff
->el
[0], pnt
->el
[0]);
9160 v
= isl_val_rat_from_isl_int(ctx
, n
, d
);
9161 v
= isl_val_normalize(v
);
9168 /* Check that the domain space of "aff" is equal to "space".
9170 static isl_stat
isl_aff_check_has_domain_space(__isl_keep isl_aff
*aff
,
9171 __isl_keep isl_space
*space
)
9175 ok
= isl_space_is_equal(isl_aff_peek_domain_space(aff
), space
);
9177 return isl_stat_error
;
9179 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
9180 "incompatible spaces", return isl_stat_error
);
9184 /* Evaluate the affine function "aff" in "pnt".
9186 __isl_give isl_val
*isl_aff_eval(__isl_take isl_aff
*aff
,
9187 __isl_take isl_point
*pnt
)
9191 isl_local_space
*ls
;
9193 if (isl_aff_check_has_domain_space(aff
, isl_point_peek_space(pnt
)) < 0)
9195 is_void
= isl_point_is_void(pnt
);
9199 return eval_void(aff
, pnt
);
9201 ls
= isl_aff_get_domain_local_space(aff
);
9202 pnt
= isl_local_space_lift_point(ls
, pnt
);
9204 v
= eval(aff
->v
, isl_point_peek_vec(pnt
));
9207 isl_point_free(pnt
);
9212 isl_point_free(pnt
);