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
)
2366 #define TYPE isl_aff
2368 #include "check_type_range_templ.c"
2370 /* Check whether the given affine expression has non-zero coefficient
2371 * for any dimension in the given range or if any of these dimensions
2372 * appear with non-zero coefficients in any of the integer divisions
2373 * involved in the affine expression.
2375 isl_bool
isl_aff_involves_dims(__isl_keep isl_aff
*aff
,
2376 enum isl_dim_type type
, unsigned first
, unsigned n
)
2380 isl_bool involves
= isl_bool_false
;
2383 return isl_bool_error
;
2385 return isl_bool_false
;
2386 if (isl_aff_check_range(aff
, type
, first
, n
) < 0)
2387 return isl_bool_error
;
2389 active
= isl_local_space_get_active(aff
->ls
, aff
->v
->el
+ 2);
2393 first
+= isl_local_space_offset(aff
->ls
, type
) - 1;
2394 for (i
= 0; i
< n
; ++i
)
2395 if (active
[first
+ i
]) {
2396 involves
= isl_bool_true
;
2405 return isl_bool_error
;
2408 __isl_give isl_aff
*isl_aff_drop_dims(__isl_take isl_aff
*aff
,
2409 enum isl_dim_type type
, unsigned first
, unsigned n
)
2415 if (type
== isl_dim_out
)
2416 isl_die(aff
->v
->ctx
, isl_error_invalid
,
2417 "cannot drop output/set dimension",
2418 return isl_aff_free(aff
));
2419 if (type
== isl_dim_in
)
2421 if (n
== 0 && !isl_local_space_is_named_or_nested(aff
->ls
, type
))
2424 ctx
= isl_aff_get_ctx(aff
);
2425 if (isl_local_space_check_range(aff
->ls
, type
, first
, n
) < 0)
2426 return isl_aff_free(aff
);
2428 aff
= isl_aff_cow(aff
);
2432 aff
->ls
= isl_local_space_drop_dims(aff
->ls
, type
, first
, n
);
2434 return isl_aff_free(aff
);
2436 first
+= 1 + isl_local_space_offset(aff
->ls
, type
);
2437 aff
->v
= isl_vec_drop_els(aff
->v
, first
, n
);
2439 return isl_aff_free(aff
);
2444 /* Drop the "n" domain dimensions starting at "first" from "aff",
2445 * after checking that they do not appear in the affine expression.
2447 static __isl_give isl_aff
*drop_domain(__isl_take isl_aff
*aff
, unsigned first
,
2452 involves
= isl_aff_involves_dims(aff
, isl_dim_in
, first
, n
);
2454 return isl_aff_free(aff
);
2456 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2457 "affine expression involves some of the domain dimensions",
2458 return isl_aff_free(aff
));
2459 return isl_aff_drop_dims(aff
, isl_dim_in
, first
, n
);
2462 /* Project the domain of the affine expression onto its parameter space.
2463 * The affine expression may not involve any of the domain dimensions.
2465 __isl_give isl_aff
*isl_aff_project_domain_on_params(__isl_take isl_aff
*aff
)
2470 n
= isl_aff_dim(aff
, isl_dim_in
);
2471 aff
= drop_domain(aff
, 0, n
);
2472 space
= isl_aff_get_domain_space(aff
);
2473 space
= isl_space_params(space
);
2474 aff
= isl_aff_reset_domain_space(aff
, space
);
2478 /* Check that the domain of "aff" is a product.
2480 static isl_stat
check_domain_product(__isl_keep isl_aff
*aff
)
2482 isl_bool is_product
;
2484 is_product
= isl_space_is_product(isl_aff_peek_domain_space(aff
));
2486 return isl_stat_error
;
2488 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2489 "domain is not a product", return isl_stat_error
);
2493 /* Given an affine function with a domain of the form [A -> B] that
2494 * does not depend on B, return the same function on domain A.
2496 __isl_give isl_aff
*isl_aff_domain_factor_domain(__isl_take isl_aff
*aff
)
2501 if (check_domain_product(aff
) < 0)
2502 return isl_aff_free(aff
);
2503 space
= isl_aff_get_domain_space(aff
);
2504 n
= isl_space_dim(space
, isl_dim_set
);
2505 space
= isl_space_factor_domain(space
);
2506 n_in
= isl_space_dim(space
, isl_dim_set
);
2507 aff
= drop_domain(aff
, n_in
, n
- n_in
);
2508 aff
= isl_aff_reset_domain_space(aff
, space
);
2512 /* Convert an affine expression defined over a parameter domain
2513 * into one that is defined over a zero-dimensional set.
2515 __isl_give isl_aff
*isl_aff_from_range(__isl_take isl_aff
*aff
)
2517 isl_local_space
*ls
;
2519 ls
= isl_aff_take_domain_local_space(aff
);
2520 ls
= isl_local_space_set_from_params(ls
);
2521 aff
= isl_aff_restore_domain_local_space(aff
, ls
);
2526 __isl_give isl_aff
*isl_aff_insert_dims(__isl_take isl_aff
*aff
,
2527 enum isl_dim_type type
, unsigned first
, unsigned n
)
2533 if (type
== isl_dim_out
)
2534 isl_die(aff
->v
->ctx
, isl_error_invalid
,
2535 "cannot insert output/set dimensions",
2536 return isl_aff_free(aff
));
2537 if (type
== isl_dim_in
)
2539 if (n
== 0 && !isl_local_space_is_named_or_nested(aff
->ls
, type
))
2542 ctx
= isl_aff_get_ctx(aff
);
2543 if (isl_local_space_check_range(aff
->ls
, type
, first
, 0) < 0)
2544 return isl_aff_free(aff
);
2546 aff
= isl_aff_cow(aff
);
2550 aff
->ls
= isl_local_space_insert_dims(aff
->ls
, type
, first
, n
);
2552 return isl_aff_free(aff
);
2554 first
+= 1 + isl_local_space_offset(aff
->ls
, type
);
2555 aff
->v
= isl_vec_insert_zero_els(aff
->v
, first
, n
);
2557 return isl_aff_free(aff
);
2562 __isl_give isl_aff
*isl_aff_add_dims(__isl_take isl_aff
*aff
,
2563 enum isl_dim_type type
, unsigned n
)
2567 pos
= isl_aff_dim(aff
, type
);
2569 return isl_aff_insert_dims(aff
, type
, pos
, n
);
2572 __isl_give isl_pw_aff
*isl_pw_aff_add_dims(__isl_take isl_pw_aff
*pwaff
,
2573 enum isl_dim_type type
, unsigned n
)
2577 pos
= isl_pw_aff_dim(pwaff
, type
);
2579 return isl_pw_aff_insert_dims(pwaff
, type
, pos
, n
);
2582 /* Move the "n" dimensions of "src_type" starting at "src_pos" of "aff"
2583 * to dimensions of "dst_type" at "dst_pos".
2585 * We only support moving input dimensions to parameters and vice versa.
2587 __isl_give isl_aff
*isl_aff_move_dims(__isl_take isl_aff
*aff
,
2588 enum isl_dim_type dst_type
, unsigned dst_pos
,
2589 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
2597 !isl_local_space_is_named_or_nested(aff
->ls
, src_type
) &&
2598 !isl_local_space_is_named_or_nested(aff
->ls
, dst_type
))
2601 if (dst_type
== isl_dim_out
|| src_type
== isl_dim_out
)
2602 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2603 "cannot move output/set dimension",
2604 return isl_aff_free(aff
));
2605 if (dst_type
== isl_dim_div
|| src_type
== isl_dim_div
)
2606 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2607 "cannot move divs", return isl_aff_free(aff
));
2608 if (dst_type
== isl_dim_in
)
2609 dst_type
= isl_dim_set
;
2610 if (src_type
== isl_dim_in
)
2611 src_type
= isl_dim_set
;
2613 if (isl_local_space_check_range(aff
->ls
, src_type
, src_pos
, n
) < 0)
2614 return isl_aff_free(aff
);
2615 if (dst_type
== src_type
)
2616 isl_die(isl_aff_get_ctx(aff
), isl_error_unsupported
,
2617 "moving dims within the same type not supported",
2618 return isl_aff_free(aff
));
2620 aff
= isl_aff_cow(aff
);
2624 g_src_pos
= 1 + isl_local_space_offset(aff
->ls
, src_type
) + src_pos
;
2625 g_dst_pos
= 1 + isl_local_space_offset(aff
->ls
, dst_type
) + dst_pos
;
2626 if (dst_type
> src_type
)
2629 aff
->v
= isl_vec_move_els(aff
->v
, g_dst_pos
, g_src_pos
, n
);
2630 aff
->ls
= isl_local_space_move_dims(aff
->ls
, dst_type
, dst_pos
,
2631 src_type
, src_pos
, n
);
2632 if (!aff
->v
|| !aff
->ls
)
2633 return isl_aff_free(aff
);
2635 aff
= sort_divs(aff
);
2640 __isl_give isl_pw_aff
*isl_pw_aff_from_aff(__isl_take isl_aff
*aff
)
2642 isl_set
*dom
= isl_set_universe(isl_aff_get_domain_space(aff
));
2643 return isl_pw_aff_alloc(dom
, aff
);
2646 #define isl_aff_involves_nan isl_aff_is_nan
2649 #define PW isl_pw_aff
2653 #define EL_IS_ZERO is_empty
2657 #define IS_ZERO is_empty
2660 #undef DEFAULT_IS_ZERO
2661 #define DEFAULT_IS_ZERO 0
2667 #include <isl_pw_templ.c>
2668 #include <isl_pw_eval.c>
2669 #include <isl_pw_hash.c>
2670 #include <isl_pw_union_opt.c>
2675 #include <isl_union_single.c>
2676 #include <isl_union_neg.c>
2678 static __isl_give isl_set
*align_params_pw_pw_set_and(
2679 __isl_take isl_pw_aff
*pwaff1
, __isl_take isl_pw_aff
*pwaff2
,
2680 __isl_give isl_set
*(*fn
)(__isl_take isl_pw_aff
*pwaff1
,
2681 __isl_take isl_pw_aff
*pwaff2
))
2683 isl_bool equal_params
;
2685 if (!pwaff1
|| !pwaff2
)
2687 equal_params
= isl_space_has_equal_params(pwaff1
->dim
, pwaff2
->dim
);
2688 if (equal_params
< 0)
2691 return fn(pwaff1
, pwaff2
);
2692 if (isl_pw_aff_check_named_params(pwaff1
) < 0 ||
2693 isl_pw_aff_check_named_params(pwaff2
) < 0)
2695 pwaff1
= isl_pw_aff_align_params(pwaff1
, isl_pw_aff_get_space(pwaff2
));
2696 pwaff2
= isl_pw_aff_align_params(pwaff2
, isl_pw_aff_get_space(pwaff1
));
2697 return fn(pwaff1
, pwaff2
);
2699 isl_pw_aff_free(pwaff1
);
2700 isl_pw_aff_free(pwaff2
);
2704 /* Align the parameters of the to isl_pw_aff arguments and
2705 * then apply a function "fn" on them that returns an isl_map.
2707 static __isl_give isl_map
*align_params_pw_pw_map_and(
2708 __isl_take isl_pw_aff
*pa1
, __isl_take isl_pw_aff
*pa2
,
2709 __isl_give isl_map
*(*fn
)(__isl_take isl_pw_aff
*pa1
,
2710 __isl_take isl_pw_aff
*pa2
))
2712 isl_bool equal_params
;
2716 equal_params
= isl_space_has_equal_params(pa1
->dim
, pa2
->dim
);
2717 if (equal_params
< 0)
2720 return fn(pa1
, pa2
);
2721 if (isl_pw_aff_check_named_params(pa1
) < 0 ||
2722 isl_pw_aff_check_named_params(pa2
) < 0)
2724 pa1
= isl_pw_aff_align_params(pa1
, isl_pw_aff_get_space(pa2
));
2725 pa2
= isl_pw_aff_align_params(pa2
, isl_pw_aff_get_space(pa1
));
2726 return fn(pa1
, pa2
);
2728 isl_pw_aff_free(pa1
);
2729 isl_pw_aff_free(pa2
);
2733 /* Compute a piecewise quasi-affine expression with a domain that
2734 * is the union of those of pwaff1 and pwaff2 and such that on each
2735 * cell, the quasi-affine expression is the maximum of those of pwaff1
2736 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2737 * cell, then the associated expression is the defined one.
2739 static __isl_give isl_pw_aff
*pw_aff_union_max(__isl_take isl_pw_aff
*pwaff1
,
2740 __isl_take isl_pw_aff
*pwaff2
)
2742 return isl_pw_aff_union_opt_cmp(pwaff1
, pwaff2
, &isl_aff_ge_set
);
2745 __isl_give isl_pw_aff
*isl_pw_aff_union_max(__isl_take isl_pw_aff
*pwaff1
,
2746 __isl_take isl_pw_aff
*pwaff2
)
2748 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
,
2752 /* Compute a piecewise quasi-affine expression with a domain that
2753 * is the union of those of pwaff1 and pwaff2 and such that on each
2754 * cell, the quasi-affine expression is the minimum of those of pwaff1
2755 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2756 * cell, then the associated expression is the defined one.
2758 static __isl_give isl_pw_aff
*pw_aff_union_min(__isl_take isl_pw_aff
*pwaff1
,
2759 __isl_take isl_pw_aff
*pwaff2
)
2761 return isl_pw_aff_union_opt_cmp(pwaff1
, pwaff2
, &isl_aff_le_set
);
2764 __isl_give isl_pw_aff
*isl_pw_aff_union_min(__isl_take isl_pw_aff
*pwaff1
,
2765 __isl_take isl_pw_aff
*pwaff2
)
2767 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
,
2771 __isl_give isl_pw_aff
*isl_pw_aff_union_opt(__isl_take isl_pw_aff
*pwaff1
,
2772 __isl_take isl_pw_aff
*pwaff2
, int max
)
2775 return isl_pw_aff_union_max(pwaff1
, pwaff2
);
2777 return isl_pw_aff_union_min(pwaff1
, pwaff2
);
2780 /* Return a set containing those elements in the domain
2781 * of "pwaff" where it satisfies "fn" (if complement is 0) or
2782 * does not satisfy "fn" (if complement is 1).
2784 * The pieces with a NaN never belong to the result since
2785 * NaN does not satisfy any property.
2787 static __isl_give isl_set
*pw_aff_locus(__isl_take isl_pw_aff
*pwaff
,
2788 __isl_give isl_basic_set
*(*fn
)(__isl_take isl_aff
*aff
, int rational
),
2797 set
= isl_set_empty(isl_pw_aff_get_domain_space(pwaff
));
2799 for (i
= 0; i
< pwaff
->n
; ++i
) {
2800 isl_basic_set
*bset
;
2801 isl_set
*set_i
, *locus
;
2804 if (isl_aff_is_nan(pwaff
->p
[i
].aff
))
2807 rational
= isl_set_has_rational(pwaff
->p
[i
].set
);
2808 bset
= fn(isl_aff_copy(pwaff
->p
[i
].aff
), rational
);
2809 locus
= isl_set_from_basic_set(bset
);
2810 set_i
= isl_set_copy(pwaff
->p
[i
].set
);
2812 set_i
= isl_set_subtract(set_i
, locus
);
2814 set_i
= isl_set_intersect(set_i
, locus
);
2815 set
= isl_set_union_disjoint(set
, set_i
);
2818 isl_pw_aff_free(pwaff
);
2823 /* Return a set containing those elements in the domain
2824 * of "pa" where it is positive.
2826 __isl_give isl_set
*isl_pw_aff_pos_set(__isl_take isl_pw_aff
*pa
)
2828 return pw_aff_locus(pa
, &aff_pos_basic_set
, 0);
2831 /* Return a set containing those elements in the domain
2832 * of pwaff where it is non-negative.
2834 __isl_give isl_set
*isl_pw_aff_nonneg_set(__isl_take isl_pw_aff
*pwaff
)
2836 return pw_aff_locus(pwaff
, &aff_nonneg_basic_set
, 0);
2839 /* Return a set containing those elements in the domain
2840 * of pwaff where it is zero.
2842 __isl_give isl_set
*isl_pw_aff_zero_set(__isl_take isl_pw_aff
*pwaff
)
2844 return pw_aff_locus(pwaff
, &aff_zero_basic_set
, 0);
2847 /* Return a set containing those elements in the domain
2848 * of pwaff where it is not zero.
2850 __isl_give isl_set
*isl_pw_aff_non_zero_set(__isl_take isl_pw_aff
*pwaff
)
2852 return pw_aff_locus(pwaff
, &aff_zero_basic_set
, 1);
2855 /* Return a set containing those elements in the shared domain
2856 * of pwaff1 and pwaff2 where pwaff1 is greater than (or equal) to pwaff2.
2858 * We compute the difference on the shared domain and then construct
2859 * the set of values where this difference is non-negative.
2860 * If strict is set, we first subtract 1 from the difference.
2861 * If equal is set, we only return the elements where pwaff1 and pwaff2
2864 static __isl_give isl_set
*pw_aff_gte_set(__isl_take isl_pw_aff
*pwaff1
,
2865 __isl_take isl_pw_aff
*pwaff2
, int strict
, int equal
)
2867 isl_set
*set1
, *set2
;
2869 set1
= isl_pw_aff_domain(isl_pw_aff_copy(pwaff1
));
2870 set2
= isl_pw_aff_domain(isl_pw_aff_copy(pwaff2
));
2871 set1
= isl_set_intersect(set1
, set2
);
2872 pwaff1
= isl_pw_aff_intersect_domain(pwaff1
, isl_set_copy(set1
));
2873 pwaff2
= isl_pw_aff_intersect_domain(pwaff2
, isl_set_copy(set1
));
2874 pwaff1
= isl_pw_aff_add(pwaff1
, isl_pw_aff_neg(pwaff2
));
2877 isl_space
*space
= isl_set_get_space(set1
);
2879 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(space
));
2880 aff
= isl_aff_add_constant_si(aff
, -1);
2881 pwaff1
= isl_pw_aff_add(pwaff1
, isl_pw_aff_alloc(set1
, aff
));
2886 return isl_pw_aff_zero_set(pwaff1
);
2887 return isl_pw_aff_nonneg_set(pwaff1
);
2890 /* Return a set containing those elements in the shared domain
2891 * of pwaff1 and pwaff2 where pwaff1 is equal to pwaff2.
2893 static __isl_give isl_set
*pw_aff_eq_set(__isl_take isl_pw_aff
*pwaff1
,
2894 __isl_take isl_pw_aff
*pwaff2
)
2896 return pw_aff_gte_set(pwaff1
, pwaff2
, 0, 1);
2899 __isl_give isl_set
*isl_pw_aff_eq_set(__isl_take isl_pw_aff
*pwaff1
,
2900 __isl_take isl_pw_aff
*pwaff2
)
2902 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_eq_set
);
2905 /* Return a set containing those elements in the shared domain
2906 * of pwaff1 and pwaff2 where pwaff1 is greater than or equal to pwaff2.
2908 static __isl_give isl_set
*pw_aff_ge_set(__isl_take isl_pw_aff
*pwaff1
,
2909 __isl_take isl_pw_aff
*pwaff2
)
2911 return pw_aff_gte_set(pwaff1
, pwaff2
, 0, 0);
2914 __isl_give isl_set
*isl_pw_aff_ge_set(__isl_take isl_pw_aff
*pwaff1
,
2915 __isl_take isl_pw_aff
*pwaff2
)
2917 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_ge_set
);
2920 /* Return a set containing those elements in the shared domain
2921 * of pwaff1 and pwaff2 where pwaff1 is strictly greater than pwaff2.
2923 static __isl_give isl_set
*pw_aff_gt_set(__isl_take isl_pw_aff
*pwaff1
,
2924 __isl_take isl_pw_aff
*pwaff2
)
2926 return pw_aff_gte_set(pwaff1
, pwaff2
, 1, 0);
2929 __isl_give isl_set
*isl_pw_aff_gt_set(__isl_take isl_pw_aff
*pwaff1
,
2930 __isl_take isl_pw_aff
*pwaff2
)
2932 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_gt_set
);
2935 __isl_give isl_set
*isl_pw_aff_le_set(__isl_take isl_pw_aff
*pwaff1
,
2936 __isl_take isl_pw_aff
*pwaff2
)
2938 return isl_pw_aff_ge_set(pwaff2
, pwaff1
);
2941 __isl_give isl_set
*isl_pw_aff_lt_set(__isl_take isl_pw_aff
*pwaff1
,
2942 __isl_take isl_pw_aff
*pwaff2
)
2944 return isl_pw_aff_gt_set(pwaff2
, pwaff1
);
2947 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
2948 * where the function values are ordered in the same way as "order",
2949 * which returns a set in the shared domain of its two arguments.
2950 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
2952 * Let "pa1" and "pa2" be defined on domains A and B respectively.
2953 * We first pull back the two functions such that they are defined on
2954 * the domain [A -> B]. Then we apply "order", resulting in a set
2955 * in the space [A -> B]. Finally, we unwrap this set to obtain
2956 * a map in the space A -> B.
2958 static __isl_give isl_map
*isl_pw_aff_order_map_aligned(
2959 __isl_take isl_pw_aff
*pa1
, __isl_take isl_pw_aff
*pa2
,
2960 __isl_give isl_set
*(*order
)(__isl_take isl_pw_aff
*pa1
,
2961 __isl_take isl_pw_aff
*pa2
))
2963 isl_space
*space1
, *space2
;
2967 space1
= isl_space_domain(isl_pw_aff_get_space(pa1
));
2968 space2
= isl_space_domain(isl_pw_aff_get_space(pa2
));
2969 space1
= isl_space_map_from_domain_and_range(space1
, space2
);
2970 ma
= isl_multi_aff_domain_map(isl_space_copy(space1
));
2971 pa1
= isl_pw_aff_pullback_multi_aff(pa1
, ma
);
2972 ma
= isl_multi_aff_range_map(space1
);
2973 pa2
= isl_pw_aff_pullback_multi_aff(pa2
, ma
);
2974 set
= order(pa1
, pa2
);
2976 return isl_set_unwrap(set
);
2979 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
2980 * where the function values are equal.
2981 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
2983 static __isl_give isl_map
*isl_pw_aff_eq_map_aligned(__isl_take isl_pw_aff
*pa1
,
2984 __isl_take isl_pw_aff
*pa2
)
2986 return isl_pw_aff_order_map_aligned(pa1
, pa2
, &isl_pw_aff_eq_set
);
2989 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
2990 * where the function values are equal.
2992 __isl_give isl_map
*isl_pw_aff_eq_map(__isl_take isl_pw_aff
*pa1
,
2993 __isl_take isl_pw_aff
*pa2
)
2995 return align_params_pw_pw_map_and(pa1
, pa2
, &isl_pw_aff_eq_map_aligned
);
2998 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
2999 * where the function value of "pa1" is less than the function value of "pa2".
3000 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3002 static __isl_give isl_map
*isl_pw_aff_lt_map_aligned(__isl_take isl_pw_aff
*pa1
,
3003 __isl_take isl_pw_aff
*pa2
)
3005 return isl_pw_aff_order_map_aligned(pa1
, pa2
, &isl_pw_aff_lt_set
);
3008 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3009 * where the function value of "pa1" is less than the function value of "pa2".
3011 __isl_give isl_map
*isl_pw_aff_lt_map(__isl_take isl_pw_aff
*pa1
,
3012 __isl_take isl_pw_aff
*pa2
)
3014 return align_params_pw_pw_map_and(pa1
, pa2
, &isl_pw_aff_lt_map_aligned
);
3017 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3018 * where the function value of "pa1" is greater than the function value
3020 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3022 static __isl_give isl_map
*isl_pw_aff_gt_map_aligned(__isl_take isl_pw_aff
*pa1
,
3023 __isl_take isl_pw_aff
*pa2
)
3025 return isl_pw_aff_order_map_aligned(pa1
, pa2
, &isl_pw_aff_gt_set
);
3028 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3029 * where the function value of "pa1" is greater than the function value
3032 __isl_give isl_map
*isl_pw_aff_gt_map(__isl_take isl_pw_aff
*pa1
,
3033 __isl_take isl_pw_aff
*pa2
)
3035 return align_params_pw_pw_map_and(pa1
, pa2
, &isl_pw_aff_gt_map_aligned
);
3038 /* Return a set containing those elements in the shared domain
3039 * of the elements of list1 and list2 where each element in list1
3040 * has the relation specified by "fn" with each element in list2.
3042 static __isl_give isl_set
*pw_aff_list_set(__isl_take isl_pw_aff_list
*list1
,
3043 __isl_take isl_pw_aff_list
*list2
,
3044 __isl_give isl_set
*(*fn
)(__isl_take isl_pw_aff
*pwaff1
,
3045 __isl_take isl_pw_aff
*pwaff2
))
3051 if (!list1
|| !list2
)
3054 ctx
= isl_pw_aff_list_get_ctx(list1
);
3055 if (list1
->n
< 1 || list2
->n
< 1)
3056 isl_die(ctx
, isl_error_invalid
,
3057 "list should contain at least one element", goto error
);
3059 set
= isl_set_universe(isl_pw_aff_get_domain_space(list1
->p
[0]));
3060 for (i
= 0; i
< list1
->n
; ++i
)
3061 for (j
= 0; j
< list2
->n
; ++j
) {
3064 set_ij
= fn(isl_pw_aff_copy(list1
->p
[i
]),
3065 isl_pw_aff_copy(list2
->p
[j
]));
3066 set
= isl_set_intersect(set
, set_ij
);
3069 isl_pw_aff_list_free(list1
);
3070 isl_pw_aff_list_free(list2
);
3073 isl_pw_aff_list_free(list1
);
3074 isl_pw_aff_list_free(list2
);
3078 /* Return a set containing those elements in the shared domain
3079 * of the elements of list1 and list2 where each element in list1
3080 * is equal to each element in list2.
3082 __isl_give isl_set
*isl_pw_aff_list_eq_set(__isl_take isl_pw_aff_list
*list1
,
3083 __isl_take isl_pw_aff_list
*list2
)
3085 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_eq_set
);
3088 __isl_give isl_set
*isl_pw_aff_list_ne_set(__isl_take isl_pw_aff_list
*list1
,
3089 __isl_take isl_pw_aff_list
*list2
)
3091 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_ne_set
);
3094 /* Return a set containing those elements in the shared domain
3095 * of the elements of list1 and list2 where each element in list1
3096 * is less than or equal to each element in list2.
3098 __isl_give isl_set
*isl_pw_aff_list_le_set(__isl_take isl_pw_aff_list
*list1
,
3099 __isl_take isl_pw_aff_list
*list2
)
3101 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_le_set
);
3104 __isl_give isl_set
*isl_pw_aff_list_lt_set(__isl_take isl_pw_aff_list
*list1
,
3105 __isl_take isl_pw_aff_list
*list2
)
3107 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_lt_set
);
3110 __isl_give isl_set
*isl_pw_aff_list_ge_set(__isl_take isl_pw_aff_list
*list1
,
3111 __isl_take isl_pw_aff_list
*list2
)
3113 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_ge_set
);
3116 __isl_give isl_set
*isl_pw_aff_list_gt_set(__isl_take isl_pw_aff_list
*list1
,
3117 __isl_take isl_pw_aff_list
*list2
)
3119 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_gt_set
);
3123 /* Return a set containing those elements in the shared domain
3124 * of pwaff1 and pwaff2 where pwaff1 is not equal to pwaff2.
3126 static __isl_give isl_set
*pw_aff_ne_set(__isl_take isl_pw_aff
*pwaff1
,
3127 __isl_take isl_pw_aff
*pwaff2
)
3129 isl_set
*set_lt
, *set_gt
;
3131 set_lt
= isl_pw_aff_lt_set(isl_pw_aff_copy(pwaff1
),
3132 isl_pw_aff_copy(pwaff2
));
3133 set_gt
= isl_pw_aff_gt_set(pwaff1
, pwaff2
);
3134 return isl_set_union_disjoint(set_lt
, set_gt
);
3137 __isl_give isl_set
*isl_pw_aff_ne_set(__isl_take isl_pw_aff
*pwaff1
,
3138 __isl_take isl_pw_aff
*pwaff2
)
3140 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_ne_set
);
3143 __isl_give isl_pw_aff
*isl_pw_aff_scale_down(__isl_take isl_pw_aff
*pwaff
,
3148 if (isl_int_is_one(v
))
3150 if (!isl_int_is_pos(v
))
3151 isl_die(isl_pw_aff_get_ctx(pwaff
), isl_error_invalid
,
3152 "factor needs to be positive",
3153 return isl_pw_aff_free(pwaff
));
3154 pwaff
= isl_pw_aff_cow(pwaff
);
3160 for (i
= 0; i
< pwaff
->n
; ++i
) {
3161 pwaff
->p
[i
].aff
= isl_aff_scale_down(pwaff
->p
[i
].aff
, v
);
3162 if (!pwaff
->p
[i
].aff
)
3163 return isl_pw_aff_free(pwaff
);
3169 __isl_give isl_pw_aff
*isl_pw_aff_floor(__isl_take isl_pw_aff
*pwaff
)
3173 pwaff
= isl_pw_aff_cow(pwaff
);
3179 for (i
= 0; i
< pwaff
->n
; ++i
) {
3180 pwaff
->p
[i
].aff
= isl_aff_floor(pwaff
->p
[i
].aff
);
3181 if (!pwaff
->p
[i
].aff
)
3182 return isl_pw_aff_free(pwaff
);
3188 __isl_give isl_pw_aff
*isl_pw_aff_ceil(__isl_take isl_pw_aff
*pwaff
)
3192 pwaff
= isl_pw_aff_cow(pwaff
);
3198 for (i
= 0; i
< pwaff
->n
; ++i
) {
3199 pwaff
->p
[i
].aff
= isl_aff_ceil(pwaff
->p
[i
].aff
);
3200 if (!pwaff
->p
[i
].aff
)
3201 return isl_pw_aff_free(pwaff
);
3207 /* Assuming that "cond1" and "cond2" are disjoint,
3208 * return an affine expression that is equal to pwaff1 on cond1
3209 * and to pwaff2 on cond2.
3211 static __isl_give isl_pw_aff
*isl_pw_aff_select(
3212 __isl_take isl_set
*cond1
, __isl_take isl_pw_aff
*pwaff1
,
3213 __isl_take isl_set
*cond2
, __isl_take isl_pw_aff
*pwaff2
)
3215 pwaff1
= isl_pw_aff_intersect_domain(pwaff1
, cond1
);
3216 pwaff2
= isl_pw_aff_intersect_domain(pwaff2
, cond2
);
3218 return isl_pw_aff_add_disjoint(pwaff1
, pwaff2
);
3221 /* Return an affine expression that is equal to pwaff_true for elements
3222 * where "cond" is non-zero and to pwaff_false for elements where "cond"
3224 * That is, return cond ? pwaff_true : pwaff_false;
3226 * If "cond" involves and NaN, then we conservatively return a NaN
3227 * on its entire domain. In principle, we could consider the pieces
3228 * where it is NaN separately from those where it is not.
3230 * If "pwaff_true" and "pwaff_false" are obviously equal to each other,
3231 * then only use the domain of "cond" to restrict the domain.
3233 __isl_give isl_pw_aff
*isl_pw_aff_cond(__isl_take isl_pw_aff
*cond
,
3234 __isl_take isl_pw_aff
*pwaff_true
, __isl_take isl_pw_aff
*pwaff_false
)
3236 isl_set
*cond_true
, *cond_false
;
3241 if (isl_pw_aff_involves_nan(cond
)) {
3242 isl_space
*space
= isl_pw_aff_get_domain_space(cond
);
3243 isl_local_space
*ls
= isl_local_space_from_space(space
);
3244 isl_pw_aff_free(cond
);
3245 isl_pw_aff_free(pwaff_true
);
3246 isl_pw_aff_free(pwaff_false
);
3247 return isl_pw_aff_nan_on_domain(ls
);
3250 pwaff_true
= isl_pw_aff_align_params(pwaff_true
,
3251 isl_pw_aff_get_space(pwaff_false
));
3252 pwaff_false
= isl_pw_aff_align_params(pwaff_false
,
3253 isl_pw_aff_get_space(pwaff_true
));
3254 equal
= isl_pw_aff_plain_is_equal(pwaff_true
, pwaff_false
);
3260 dom
= isl_set_coalesce(isl_pw_aff_domain(cond
));
3261 isl_pw_aff_free(pwaff_false
);
3262 return isl_pw_aff_intersect_domain(pwaff_true
, dom
);
3265 cond_true
= isl_pw_aff_non_zero_set(isl_pw_aff_copy(cond
));
3266 cond_false
= isl_pw_aff_zero_set(cond
);
3267 return isl_pw_aff_select(cond_true
, pwaff_true
,
3268 cond_false
, pwaff_false
);
3270 isl_pw_aff_free(cond
);
3271 isl_pw_aff_free(pwaff_true
);
3272 isl_pw_aff_free(pwaff_false
);
3276 isl_bool
isl_aff_is_cst(__isl_keep isl_aff
*aff
)
3279 return isl_bool_error
;
3281 return isl_seq_first_non_zero(aff
->v
->el
+ 2, aff
->v
->size
- 2) == -1;
3284 /* Check whether pwaff is a piecewise constant.
3286 isl_bool
isl_pw_aff_is_cst(__isl_keep isl_pw_aff
*pwaff
)
3291 return isl_bool_error
;
3293 for (i
= 0; i
< pwaff
->n
; ++i
) {
3294 isl_bool is_cst
= isl_aff_is_cst(pwaff
->p
[i
].aff
);
3295 if (is_cst
< 0 || !is_cst
)
3299 return isl_bool_true
;
3302 /* Are all elements of "mpa" piecewise constants?
3304 isl_bool
isl_multi_pw_aff_is_cst(__isl_keep isl_multi_pw_aff
*mpa
)
3309 return isl_bool_error
;
3311 for (i
= 0; i
< mpa
->n
; ++i
) {
3312 isl_bool is_cst
= isl_pw_aff_is_cst(mpa
->u
.p
[i
]);
3313 if (is_cst
< 0 || !is_cst
)
3317 return isl_bool_true
;
3320 /* Return the product of "aff1" and "aff2".
3322 * If either of the two is NaN, then the result is NaN.
3324 * Otherwise, at least one of "aff1" or "aff2" needs to be a constant.
3326 __isl_give isl_aff
*isl_aff_mul(__isl_take isl_aff
*aff1
,
3327 __isl_take isl_aff
*aff2
)
3332 if (isl_aff_is_nan(aff1
)) {
3336 if (isl_aff_is_nan(aff2
)) {
3341 if (!isl_aff_is_cst(aff2
) && isl_aff_is_cst(aff1
))
3342 return isl_aff_mul(aff2
, aff1
);
3344 if (!isl_aff_is_cst(aff2
))
3345 isl_die(isl_aff_get_ctx(aff1
), isl_error_invalid
,
3346 "at least one affine expression should be constant",
3349 aff1
= isl_aff_cow(aff1
);
3353 aff1
= isl_aff_scale(aff1
, aff2
->v
->el
[1]);
3354 aff1
= isl_aff_scale_down(aff1
, aff2
->v
->el
[0]);
3364 /* Divide "aff1" by "aff2", assuming "aff2" is a constant.
3366 * If either of the two is NaN, then the result is NaN.
3368 __isl_give isl_aff
*isl_aff_div(__isl_take isl_aff
*aff1
,
3369 __isl_take isl_aff
*aff2
)
3377 if (isl_aff_is_nan(aff1
)) {
3381 if (isl_aff_is_nan(aff2
)) {
3386 is_cst
= isl_aff_is_cst(aff2
);
3390 isl_die(isl_aff_get_ctx(aff2
), isl_error_invalid
,
3391 "second argument should be a constant", goto error
);
3396 neg
= isl_int_is_neg(aff2
->v
->el
[1]);
3398 isl_int_neg(aff2
->v
->el
[0], aff2
->v
->el
[0]);
3399 isl_int_neg(aff2
->v
->el
[1], aff2
->v
->el
[1]);
3402 aff1
= isl_aff_scale(aff1
, aff2
->v
->el
[0]);
3403 aff1
= isl_aff_scale_down(aff1
, aff2
->v
->el
[1]);
3406 isl_int_neg(aff2
->v
->el
[0], aff2
->v
->el
[0]);
3407 isl_int_neg(aff2
->v
->el
[1], aff2
->v
->el
[1]);
3418 static __isl_give isl_pw_aff
*pw_aff_add(__isl_take isl_pw_aff
*pwaff1
,
3419 __isl_take isl_pw_aff
*pwaff2
)
3421 return isl_pw_aff_on_shared_domain(pwaff1
, pwaff2
, &isl_aff_add
);
3424 __isl_give isl_pw_aff
*isl_pw_aff_add(__isl_take isl_pw_aff
*pwaff1
,
3425 __isl_take isl_pw_aff
*pwaff2
)
3427 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
, &pw_aff_add
);
3430 __isl_give isl_pw_aff
*isl_pw_aff_union_add(__isl_take isl_pw_aff
*pwaff1
,
3431 __isl_take isl_pw_aff
*pwaff2
)
3433 return isl_pw_aff_union_add_(pwaff1
, pwaff2
);
3436 static __isl_give isl_pw_aff
*pw_aff_mul(__isl_take isl_pw_aff
*pwaff1
,
3437 __isl_take isl_pw_aff
*pwaff2
)
3439 return isl_pw_aff_on_shared_domain(pwaff1
, pwaff2
, &isl_aff_mul
);
3442 __isl_give isl_pw_aff
*isl_pw_aff_mul(__isl_take isl_pw_aff
*pwaff1
,
3443 __isl_take isl_pw_aff
*pwaff2
)
3445 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
, &pw_aff_mul
);
3448 static __isl_give isl_pw_aff
*pw_aff_div(__isl_take isl_pw_aff
*pa1
,
3449 __isl_take isl_pw_aff
*pa2
)
3451 return isl_pw_aff_on_shared_domain(pa1
, pa2
, &isl_aff_div
);
3454 /* Divide "pa1" by "pa2", assuming "pa2" is a piecewise constant.
3456 __isl_give isl_pw_aff
*isl_pw_aff_div(__isl_take isl_pw_aff
*pa1
,
3457 __isl_take isl_pw_aff
*pa2
)
3461 is_cst
= isl_pw_aff_is_cst(pa2
);
3465 isl_die(isl_pw_aff_get_ctx(pa2
), isl_error_invalid
,
3466 "second argument should be a piecewise constant",
3468 return isl_pw_aff_align_params_pw_pw_and(pa1
, pa2
, &pw_aff_div
);
3470 isl_pw_aff_free(pa1
);
3471 isl_pw_aff_free(pa2
);
3475 /* Compute the quotient of the integer division of "pa1" by "pa2"
3476 * with rounding towards zero.
3477 * "pa2" is assumed to be a piecewise constant.
3479 * In particular, return
3481 * pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2)
3484 __isl_give isl_pw_aff
*isl_pw_aff_tdiv_q(__isl_take isl_pw_aff
*pa1
,
3485 __isl_take isl_pw_aff
*pa2
)
3491 is_cst
= isl_pw_aff_is_cst(pa2
);
3495 isl_die(isl_pw_aff_get_ctx(pa2
), isl_error_invalid
,
3496 "second argument should be a piecewise constant",
3499 pa1
= isl_pw_aff_div(pa1
, pa2
);
3501 cond
= isl_pw_aff_nonneg_set(isl_pw_aff_copy(pa1
));
3502 f
= isl_pw_aff_floor(isl_pw_aff_copy(pa1
));
3503 c
= isl_pw_aff_ceil(pa1
);
3504 return isl_pw_aff_cond(isl_set_indicator_function(cond
), f
, c
);
3506 isl_pw_aff_free(pa1
);
3507 isl_pw_aff_free(pa2
);
3511 /* Compute the remainder of the integer division of "pa1" by "pa2"
3512 * with rounding towards zero.
3513 * "pa2" is assumed to be a piecewise constant.
3515 * In particular, return
3517 * pa1 - pa2 * (pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2))
3520 __isl_give isl_pw_aff
*isl_pw_aff_tdiv_r(__isl_take isl_pw_aff
*pa1
,
3521 __isl_take isl_pw_aff
*pa2
)
3526 is_cst
= isl_pw_aff_is_cst(pa2
);
3530 isl_die(isl_pw_aff_get_ctx(pa2
), isl_error_invalid
,
3531 "second argument should be a piecewise constant",
3533 res
= isl_pw_aff_tdiv_q(isl_pw_aff_copy(pa1
), isl_pw_aff_copy(pa2
));
3534 res
= isl_pw_aff_mul(pa2
, res
);
3535 res
= isl_pw_aff_sub(pa1
, res
);
3538 isl_pw_aff_free(pa1
);
3539 isl_pw_aff_free(pa2
);
3543 /* Does either of "pa1" or "pa2" involve any NaN2?
3545 static isl_bool
either_involves_nan(__isl_keep isl_pw_aff
*pa1
,
3546 __isl_keep isl_pw_aff
*pa2
)
3550 has_nan
= isl_pw_aff_involves_nan(pa1
);
3551 if (has_nan
< 0 || has_nan
)
3553 return isl_pw_aff_involves_nan(pa2
);
3556 /* Replace "pa1" and "pa2" (at least one of which involves a NaN)
3557 * by a NaN on their shared domain.
3559 * In principle, the result could be refined to only being NaN
3560 * on the parts of this domain where at least one of "pa1" or "pa2" is NaN.
3562 static __isl_give isl_pw_aff
*replace_by_nan(__isl_take isl_pw_aff
*pa1
,
3563 __isl_take isl_pw_aff
*pa2
)
3565 isl_local_space
*ls
;
3569 dom
= isl_set_intersect(isl_pw_aff_domain(pa1
), isl_pw_aff_domain(pa2
));
3570 ls
= isl_local_space_from_space(isl_set_get_space(dom
));
3571 pa
= isl_pw_aff_nan_on_domain(ls
);
3572 pa
= isl_pw_aff_intersect_domain(pa
, dom
);
3577 static __isl_give isl_pw_aff
*pw_aff_min(__isl_take isl_pw_aff
*pwaff1
,
3578 __isl_take isl_pw_aff
*pwaff2
)
3583 dom
= isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1
)),
3584 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2
)));
3585 le
= isl_pw_aff_le_set(isl_pw_aff_copy(pwaff1
),
3586 isl_pw_aff_copy(pwaff2
));
3587 dom
= isl_set_subtract(dom
, isl_set_copy(le
));
3588 return isl_pw_aff_select(le
, pwaff1
, dom
, pwaff2
);
3591 static __isl_give isl_pw_aff
*pw_aff_max(__isl_take isl_pw_aff
*pwaff1
,
3592 __isl_take isl_pw_aff
*pwaff2
)
3597 dom
= isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1
)),
3598 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2
)));
3599 ge
= isl_pw_aff_ge_set(isl_pw_aff_copy(pwaff1
),
3600 isl_pw_aff_copy(pwaff2
));
3601 dom
= isl_set_subtract(dom
, isl_set_copy(ge
));
3602 return isl_pw_aff_select(ge
, pwaff1
, dom
, pwaff2
);
3605 /* Return an expression for the minimum (if "max" is not set) or
3606 * the maximum (if "max" is set) of "pa1" and "pa2".
3607 * If either expression involves any NaN, then return a NaN
3608 * on the shared domain as result.
3610 static __isl_give isl_pw_aff
*pw_aff_min_max(__isl_take isl_pw_aff
*pa1
,
3611 __isl_take isl_pw_aff
*pa2
, int max
)
3615 has_nan
= either_involves_nan(pa1
, pa2
);
3617 pa1
= isl_pw_aff_free(pa1
);
3619 return replace_by_nan(pa1
, pa2
);
3622 return isl_pw_aff_align_params_pw_pw_and(pa1
, pa2
, &pw_aff_max
);
3624 return isl_pw_aff_align_params_pw_pw_and(pa1
, pa2
, &pw_aff_min
);
3627 /* Return an expression for the minimum of "pwaff1" and "pwaff2".
3629 __isl_give isl_pw_aff
*isl_pw_aff_min(__isl_take isl_pw_aff
*pwaff1
,
3630 __isl_take isl_pw_aff
*pwaff2
)
3632 return pw_aff_min_max(pwaff1
, pwaff2
, 0);
3635 /* Return an expression for the maximum of "pwaff1" and "pwaff2".
3637 __isl_give isl_pw_aff
*isl_pw_aff_max(__isl_take isl_pw_aff
*pwaff1
,
3638 __isl_take isl_pw_aff
*pwaff2
)
3640 return pw_aff_min_max(pwaff1
, pwaff2
, 1);
3643 static __isl_give isl_pw_aff
*pw_aff_list_reduce(
3644 __isl_take isl_pw_aff_list
*list
,
3645 __isl_give isl_pw_aff
*(*fn
)(__isl_take isl_pw_aff
*pwaff1
,
3646 __isl_take isl_pw_aff
*pwaff2
))
3655 ctx
= isl_pw_aff_list_get_ctx(list
);
3657 isl_die(ctx
, isl_error_invalid
,
3658 "list should contain at least one element", goto error
);
3660 res
= isl_pw_aff_copy(list
->p
[0]);
3661 for (i
= 1; i
< list
->n
; ++i
)
3662 res
= fn(res
, isl_pw_aff_copy(list
->p
[i
]));
3664 isl_pw_aff_list_free(list
);
3667 isl_pw_aff_list_free(list
);
3671 /* Return an isl_pw_aff that maps each element in the intersection of the
3672 * domains of the elements of list to the minimal corresponding affine
3675 __isl_give isl_pw_aff
*isl_pw_aff_list_min(__isl_take isl_pw_aff_list
*list
)
3677 return pw_aff_list_reduce(list
, &isl_pw_aff_min
);
3680 /* Return an isl_pw_aff that maps each element in the intersection of the
3681 * domains of the elements of list to the maximal corresponding affine
3684 __isl_give isl_pw_aff
*isl_pw_aff_list_max(__isl_take isl_pw_aff_list
*list
)
3686 return pw_aff_list_reduce(list
, &isl_pw_aff_max
);
3689 /* Mark the domains of "pwaff" as rational.
3691 __isl_give isl_pw_aff
*isl_pw_aff_set_rational(__isl_take isl_pw_aff
*pwaff
)
3695 pwaff
= isl_pw_aff_cow(pwaff
);
3701 for (i
= 0; i
< pwaff
->n
; ++i
) {
3702 pwaff
->p
[i
].set
= isl_set_set_rational(pwaff
->p
[i
].set
);
3703 if (!pwaff
->p
[i
].set
)
3704 return isl_pw_aff_free(pwaff
);
3710 /* Mark the domains of the elements of "list" as rational.
3712 __isl_give isl_pw_aff_list
*isl_pw_aff_list_set_rational(
3713 __isl_take isl_pw_aff_list
*list
)
3723 for (i
= 0; i
< n
; ++i
) {
3726 pa
= isl_pw_aff_list_get_pw_aff(list
, i
);
3727 pa
= isl_pw_aff_set_rational(pa
);
3728 list
= isl_pw_aff_list_set_pw_aff(list
, i
, pa
);
3734 /* Do the parameters of "aff" match those of "space"?
3736 isl_bool
isl_aff_matching_params(__isl_keep isl_aff
*aff
,
3737 __isl_keep isl_space
*space
)
3739 isl_space
*aff_space
;
3743 return isl_bool_error
;
3745 aff_space
= isl_aff_get_domain_space(aff
);
3747 match
= isl_space_has_equal_params(space
, aff_space
);
3749 isl_space_free(aff_space
);
3753 /* Check that the domain space of "aff" matches "space".
3755 isl_stat
isl_aff_check_match_domain_space(__isl_keep isl_aff
*aff
,
3756 __isl_keep isl_space
*space
)
3758 isl_space
*aff_space
;
3762 return isl_stat_error
;
3764 aff_space
= isl_aff_get_domain_space(aff
);
3766 match
= isl_space_has_equal_params(space
, aff_space
);
3770 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
3771 "parameters don't match", goto error
);
3772 match
= isl_space_tuple_is_equal(space
, isl_dim_in
,
3773 aff_space
, isl_dim_set
);
3777 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
3778 "domains don't match", goto error
);
3779 isl_space_free(aff_space
);
3782 isl_space_free(aff_space
);
3783 return isl_stat_error
;
3791 #include <isl_multi_no_explicit_domain.c>
3792 #include <isl_multi_templ.c>
3793 #include <isl_multi_apply_set.c>
3794 #include <isl_multi_cmp.c>
3795 #include <isl_multi_dims.c>
3796 #include <isl_multi_floor.c>
3797 #include <isl_multi_from_base_templ.c>
3798 #include <isl_multi_gist.c>
3799 #include <isl_multi_identity_templ.c>
3800 #include <isl_multi_move_dims_templ.c>
3801 #include <isl_multi_product_templ.c>
3802 #include <isl_multi_splice_templ.c>
3803 #include <isl_multi_zero_templ.c>
3805 /* Construct an isl_multi_aff living in "space" that corresponds
3806 * to the affine transformation matrix "mat".
3808 __isl_give isl_multi_aff
*isl_multi_aff_from_aff_mat(
3809 __isl_take isl_space
*space
, __isl_take isl_mat
*mat
)
3812 isl_local_space
*ls
= NULL
;
3813 isl_multi_aff
*ma
= NULL
;
3814 int n_row
, n_col
, n_out
, total
;
3820 ctx
= isl_mat_get_ctx(mat
);
3822 n_row
= isl_mat_rows(mat
);
3823 n_col
= isl_mat_cols(mat
);
3825 isl_die(ctx
, isl_error_invalid
,
3826 "insufficient number of rows", goto error
);
3828 isl_die(ctx
, isl_error_invalid
,
3829 "insufficient number of columns", goto error
);
3830 n_out
= isl_space_dim(space
, isl_dim_out
);
3831 total
= isl_space_dim(space
, isl_dim_all
);
3832 if (1 + n_out
!= n_row
|| 2 + total
!= n_row
+ n_col
)
3833 isl_die(ctx
, isl_error_invalid
,
3834 "dimension mismatch", goto error
);
3836 ma
= isl_multi_aff_zero(isl_space_copy(space
));
3837 ls
= isl_local_space_from_space(isl_space_domain(space
));
3839 for (i
= 0; i
< n_row
- 1; ++i
) {
3843 v
= isl_vec_alloc(ctx
, 1 + n_col
);
3846 isl_int_set(v
->el
[0], mat
->row
[0][0]);
3847 isl_seq_cpy(v
->el
+ 1, mat
->row
[1 + i
], n_col
);
3848 v
= isl_vec_normalize(v
);
3849 aff
= isl_aff_alloc_vec(isl_local_space_copy(ls
), v
);
3850 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
3853 isl_local_space_free(ls
);
3857 isl_local_space_free(ls
);
3859 isl_multi_aff_free(ma
);
3863 /* Remove any internal structure of the domain of "ma".
3864 * If there is any such internal structure in the input,
3865 * then the name of the corresponding space is also removed.
3867 __isl_give isl_multi_aff
*isl_multi_aff_flatten_domain(
3868 __isl_take isl_multi_aff
*ma
)
3875 if (!ma
->space
->nested
[0])
3878 space
= isl_multi_aff_get_space(ma
);
3879 space
= isl_space_flatten_domain(space
);
3880 ma
= isl_multi_aff_reset_space(ma
, space
);
3885 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
3886 * of the space to its domain.
3888 __isl_give isl_multi_aff
*isl_multi_aff_domain_map(__isl_take isl_space
*space
)
3891 isl_local_space
*ls
;
3896 if (!isl_space_is_map(space
))
3897 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
3898 "not a map space", goto error
);
3900 n_in
= isl_space_dim(space
, isl_dim_in
);
3901 space
= isl_space_domain_map(space
);
3903 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
3905 isl_space_free(space
);
3909 space
= isl_space_domain(space
);
3910 ls
= isl_local_space_from_space(space
);
3911 for (i
= 0; i
< n_in
; ++i
) {
3914 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
3916 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
3918 isl_local_space_free(ls
);
3921 isl_space_free(space
);
3925 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
3926 * of the space to its range.
3928 __isl_give isl_multi_aff
*isl_multi_aff_range_map(__isl_take isl_space
*space
)
3931 isl_local_space
*ls
;
3936 if (!isl_space_is_map(space
))
3937 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
3938 "not a map space", goto error
);
3940 n_in
= isl_space_dim(space
, isl_dim_in
);
3941 n_out
= isl_space_dim(space
, isl_dim_out
);
3942 space
= isl_space_range_map(space
);
3944 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
3946 isl_space_free(space
);
3950 space
= isl_space_domain(space
);
3951 ls
= isl_local_space_from_space(space
);
3952 for (i
= 0; i
< n_out
; ++i
) {
3955 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
3956 isl_dim_set
, n_in
+ i
);
3957 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
3959 isl_local_space_free(ls
);
3962 isl_space_free(space
);
3966 /* Given a map space, return an isl_pw_multi_aff that maps a wrapped copy
3967 * of the space to its range.
3969 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_range_map(
3970 __isl_take isl_space
*space
)
3972 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_range_map(space
));
3975 /* Given the space of a set and a range of set dimensions,
3976 * construct an isl_multi_aff that projects out those dimensions.
3978 __isl_give isl_multi_aff
*isl_multi_aff_project_out_map(
3979 __isl_take isl_space
*space
, enum isl_dim_type type
,
3980 unsigned first
, unsigned n
)
3983 isl_local_space
*ls
;
3988 if (!isl_space_is_set(space
))
3989 isl_die(isl_space_get_ctx(space
), isl_error_unsupported
,
3990 "expecting set space", goto error
);
3991 if (type
!= isl_dim_set
)
3992 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
3993 "only set dimensions can be projected out", goto error
);
3994 if (isl_space_check_range(space
, type
, first
, n
) < 0)
3997 dim
= isl_space_dim(space
, isl_dim_set
);
3999 space
= isl_space_from_domain(space
);
4000 space
= isl_space_add_dims(space
, isl_dim_out
, dim
- n
);
4003 return isl_multi_aff_alloc(space
);
4005 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
4006 space
= isl_space_domain(space
);
4007 ls
= isl_local_space_from_space(space
);
4009 for (i
= 0; i
< first
; ++i
) {
4012 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
4014 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
4017 for (i
= 0; i
< dim
- (first
+ n
); ++i
) {
4020 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
4021 isl_dim_set
, first
+ n
+ i
);
4022 ma
= isl_multi_aff_set_aff(ma
, first
+ i
, aff
);
4025 isl_local_space_free(ls
);
4028 isl_space_free(space
);
4032 /* Given the space of a set and a range of set dimensions,
4033 * construct an isl_pw_multi_aff that projects out those dimensions.
4035 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_project_out_map(
4036 __isl_take isl_space
*space
, enum isl_dim_type type
,
4037 unsigned first
, unsigned n
)
4041 ma
= isl_multi_aff_project_out_map(space
, type
, first
, n
);
4042 return isl_pw_multi_aff_from_multi_aff(ma
);
4045 /* Create an isl_pw_multi_aff with the given isl_multi_aff on a universe
4048 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_multi_aff(
4049 __isl_take isl_multi_aff
*ma
)
4051 isl_set
*dom
= isl_set_universe(isl_multi_aff_get_domain_space(ma
));
4052 return isl_pw_multi_aff_alloc(dom
, ma
);
4055 /* Create a piecewise multi-affine expression in the given space that maps each
4056 * input dimension to the corresponding output dimension.
4058 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_identity(
4059 __isl_take isl_space
*space
)
4061 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_identity(space
));
4064 /* Exploit the equalities in "eq" to simplify the affine expressions.
4066 static __isl_give isl_multi_aff
*isl_multi_aff_substitute_equalities(
4067 __isl_take isl_multi_aff
*maff
, __isl_take isl_basic_set
*eq
)
4071 maff
= isl_multi_aff_cow(maff
);
4075 for (i
= 0; i
< maff
->n
; ++i
) {
4076 maff
->u
.p
[i
] = isl_aff_substitute_equalities(maff
->u
.p
[i
],
4077 isl_basic_set_copy(eq
));
4082 isl_basic_set_free(eq
);
4085 isl_basic_set_free(eq
);
4086 isl_multi_aff_free(maff
);
4090 __isl_give isl_multi_aff
*isl_multi_aff_scale(__isl_take isl_multi_aff
*maff
,
4095 maff
= isl_multi_aff_cow(maff
);
4099 for (i
= 0; i
< maff
->n
; ++i
) {
4100 maff
->u
.p
[i
] = isl_aff_scale(maff
->u
.p
[i
], f
);
4102 return isl_multi_aff_free(maff
);
4108 __isl_give isl_multi_aff
*isl_multi_aff_add_on_domain(__isl_keep isl_set
*dom
,
4109 __isl_take isl_multi_aff
*maff1
, __isl_take isl_multi_aff
*maff2
)
4111 maff1
= isl_multi_aff_add(maff1
, maff2
);
4112 maff1
= isl_multi_aff_gist(maff1
, isl_set_copy(dom
));
4116 int isl_multi_aff_is_empty(__isl_keep isl_multi_aff
*maff
)
4124 /* Return the set of domain elements where "ma1" is lexicographically
4125 * smaller than or equal to "ma2".
4127 __isl_give isl_set
*isl_multi_aff_lex_le_set(__isl_take isl_multi_aff
*ma1
,
4128 __isl_take isl_multi_aff
*ma2
)
4130 return isl_multi_aff_lex_ge_set(ma2
, ma1
);
4133 /* Return the set of domain elements where "ma1" is lexicographically
4134 * smaller than "ma2".
4136 __isl_give isl_set
*isl_multi_aff_lex_lt_set(__isl_take isl_multi_aff
*ma1
,
4137 __isl_take isl_multi_aff
*ma2
)
4139 return isl_multi_aff_lex_gt_set(ma2
, ma1
);
4142 /* Return the set of domain elements where "ma1" and "ma2"
4145 static __isl_give isl_set
*isl_multi_aff_order_set(
4146 __isl_take isl_multi_aff
*ma1
, __isl_take isl_multi_aff
*ma2
,
4147 __isl_give isl_map
*order(__isl_take isl_space
*set_space
))
4150 isl_map
*map1
, *map2
;
4153 map1
= isl_map_from_multi_aff_internal(ma1
);
4154 map2
= isl_map_from_multi_aff_internal(ma2
);
4155 map
= isl_map_range_product(map1
, map2
);
4156 space
= isl_space_range(isl_map_get_space(map
));
4157 space
= isl_space_domain(isl_space_unwrap(space
));
4159 map
= isl_map_intersect_range(map
, isl_map_wrap(ge
));
4161 return isl_map_domain(map
);
4164 /* Return the set of domain elements where "ma1" is lexicographically
4165 * greater than or equal to "ma2".
4167 __isl_give isl_set
*isl_multi_aff_lex_ge_set(__isl_take isl_multi_aff
*ma1
,
4168 __isl_take isl_multi_aff
*ma2
)
4170 return isl_multi_aff_order_set(ma1
, ma2
, &isl_map_lex_ge
);
4173 /* Return the set of domain elements where "ma1" is lexicographically
4174 * greater than "ma2".
4176 __isl_give isl_set
*isl_multi_aff_lex_gt_set(__isl_take isl_multi_aff
*ma1
,
4177 __isl_take isl_multi_aff
*ma2
)
4179 return isl_multi_aff_order_set(ma1
, ma2
, &isl_map_lex_gt
);
4183 #define PW isl_pw_multi_aff
4185 #define EL isl_multi_aff
4187 #define EL_IS_ZERO is_empty
4191 #define IS_ZERO is_empty
4194 #undef DEFAULT_IS_ZERO
4195 #define DEFAULT_IS_ZERO 0
4199 #define NO_INSERT_DIMS
4203 #include <isl_pw_templ.c>
4204 #include <isl_pw_union_opt.c>
4209 #define BASE pw_multi_aff
4211 #include <isl_union_multi.c>
4212 #include <isl_union_neg.c>
4214 static __isl_give isl_pw_multi_aff
*pw_multi_aff_union_lexmax(
4215 __isl_take isl_pw_multi_aff
*pma1
,
4216 __isl_take isl_pw_multi_aff
*pma2
)
4218 return isl_pw_multi_aff_union_opt_cmp(pma1
, pma2
,
4219 &isl_multi_aff_lex_ge_set
);
4222 /* Given two piecewise multi affine expressions, return a piecewise
4223 * multi-affine expression defined on the union of the definition domains
4224 * of the inputs that is equal to the lexicographic maximum of the two
4225 * inputs on each cell. If only one of the two inputs is defined on
4226 * a given cell, then it is considered to be the maximum.
4228 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_union_lexmax(
4229 __isl_take isl_pw_multi_aff
*pma1
,
4230 __isl_take isl_pw_multi_aff
*pma2
)
4232 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
4233 &pw_multi_aff_union_lexmax
);
4236 static __isl_give isl_pw_multi_aff
*pw_multi_aff_union_lexmin(
4237 __isl_take isl_pw_multi_aff
*pma1
,
4238 __isl_take isl_pw_multi_aff
*pma2
)
4240 return isl_pw_multi_aff_union_opt_cmp(pma1
, pma2
,
4241 &isl_multi_aff_lex_le_set
);
4244 /* Given two piecewise multi affine expressions, return a piecewise
4245 * multi-affine expression defined on the union of the definition domains
4246 * of the inputs that is equal to the lexicographic minimum of the two
4247 * inputs on each cell. If only one of the two inputs is defined on
4248 * a given cell, then it is considered to be the minimum.
4250 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_union_lexmin(
4251 __isl_take isl_pw_multi_aff
*pma1
,
4252 __isl_take isl_pw_multi_aff
*pma2
)
4254 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
4255 &pw_multi_aff_union_lexmin
);
4258 static __isl_give isl_pw_multi_aff
*pw_multi_aff_add(
4259 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4261 return isl_pw_multi_aff_on_shared_domain(pma1
, pma2
,
4262 &isl_multi_aff_add
);
4265 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_add(
4266 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4268 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
4272 static __isl_give isl_pw_multi_aff
*pw_multi_aff_sub(
4273 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4275 return isl_pw_multi_aff_on_shared_domain(pma1
, pma2
,
4276 &isl_multi_aff_sub
);
4279 /* Subtract "pma2" from "pma1" and return the result.
4281 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_sub(
4282 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4284 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
4288 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_union_add(
4289 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4291 return isl_pw_multi_aff_union_add_(pma1
, pma2
);
4294 /* Compute the sum of "upa1" and "upa2" on the union of their domains,
4295 * with the actual sum on the shared domain and
4296 * the defined expression on the symmetric difference of the domains.
4298 __isl_give isl_union_pw_aff
*isl_union_pw_aff_union_add(
4299 __isl_take isl_union_pw_aff
*upa1
, __isl_take isl_union_pw_aff
*upa2
)
4301 return isl_union_pw_aff_union_add_(upa1
, upa2
);
4304 /* Compute the sum of "upma1" and "upma2" on the union of their domains,
4305 * with the actual sum on the shared domain and
4306 * the defined expression on the symmetric difference of the domains.
4308 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_union_add(
4309 __isl_take isl_union_pw_multi_aff
*upma1
,
4310 __isl_take isl_union_pw_multi_aff
*upma2
)
4312 return isl_union_pw_multi_aff_union_add_(upma1
, upma2
);
4315 /* Given two piecewise multi-affine expressions A -> B and C -> D,
4316 * construct a piecewise multi-affine expression [A -> C] -> [B -> D].
4318 static __isl_give isl_pw_multi_aff
*pw_multi_aff_product(
4319 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4323 isl_pw_multi_aff
*res
;
4328 n
= pma1
->n
* pma2
->n
;
4329 space
= isl_space_product(isl_space_copy(pma1
->dim
),
4330 isl_space_copy(pma2
->dim
));
4331 res
= isl_pw_multi_aff_alloc_size(space
, n
);
4333 for (i
= 0; i
< pma1
->n
; ++i
) {
4334 for (j
= 0; j
< pma2
->n
; ++j
) {
4338 domain
= isl_set_product(isl_set_copy(pma1
->p
[i
].set
),
4339 isl_set_copy(pma2
->p
[j
].set
));
4340 ma
= isl_multi_aff_product(
4341 isl_multi_aff_copy(pma1
->p
[i
].maff
),
4342 isl_multi_aff_copy(pma2
->p
[j
].maff
));
4343 res
= isl_pw_multi_aff_add_piece(res
, domain
, ma
);
4347 isl_pw_multi_aff_free(pma1
);
4348 isl_pw_multi_aff_free(pma2
);
4351 isl_pw_multi_aff_free(pma1
);
4352 isl_pw_multi_aff_free(pma2
);
4356 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_product(
4357 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4359 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
4360 &pw_multi_aff_product
);
4363 /* Subtract the initial "n" elements in "ma" with coefficients in "c" and
4364 * denominator "denom".
4365 * "denom" is allowed to be negative, in which case the actual denominator
4366 * is -denom and the expressions are added instead.
4368 static __isl_give isl_aff
*subtract_initial(__isl_take isl_aff
*aff
,
4369 __isl_keep isl_multi_aff
*ma
, int n
, isl_int
*c
, isl_int denom
)
4375 first
= isl_seq_first_non_zero(c
, n
);
4379 sign
= isl_int_sgn(denom
);
4381 isl_int_abs(d
, denom
);
4382 for (i
= first
; i
< n
; ++i
) {
4385 if (isl_int_is_zero(c
[i
]))
4387 aff_i
= isl_multi_aff_get_aff(ma
, i
);
4388 aff_i
= isl_aff_scale(aff_i
, c
[i
]);
4389 aff_i
= isl_aff_scale_down(aff_i
, d
);
4391 aff
= isl_aff_sub(aff
, aff_i
);
4393 aff
= isl_aff_add(aff
, aff_i
);
4400 /* Extract an affine expression that expresses the output dimension "pos"
4401 * of "bmap" in terms of the parameters and input dimensions from
4403 * Note that this expression may involve integer divisions defined
4404 * in terms of parameters and input dimensions.
4405 * The equality may also involve references to earlier (but not later)
4406 * output dimensions. These are replaced by the corresponding elements
4409 * If the equality is of the form
4411 * f(i) + h(j) + a x + g(i) = 0,
4413 * with f(i) a linear combinations of the parameters and input dimensions,
4414 * g(i) a linear combination of integer divisions defined in terms of the same
4415 * and h(j) a linear combinations of earlier output dimensions,
4416 * then the affine expression is
4418 * (-f(i) - g(i))/a - h(j)/a
4420 * If the equality is of the form
4422 * f(i) + h(j) - a x + g(i) = 0,
4424 * then the affine expression is
4426 * (f(i) + g(i))/a - h(j)/(-a)
4429 * If "div" refers to an integer division (i.e., it is smaller than
4430 * the number of integer divisions), then the equality constraint
4431 * does involve an integer division (the one at position "div") that
4432 * is defined in terms of output dimensions. However, this integer
4433 * division can be eliminated by exploiting a pair of constraints
4434 * x >= l and x <= l + n, with n smaller than the coefficient of "div"
4435 * in the equality constraint. "ineq" refers to inequality x >= l, i.e.,
4437 * In particular, let
4439 * x = e(i) + m floor(...)
4441 * with e(i) the expression derived above and floor(...) the integer
4442 * division involving output dimensions.
4453 * e(i) + m floor(...) - l = (e(i) + m floor(...) - l) mod m
4454 * = (e(i) - l) mod m
4458 * x - l = (e(i) - l) mod m
4462 * x = ((e(i) - l) mod m) + l
4464 * The variable "shift" below contains the expression -l, which may
4465 * also involve a linear combination of earlier output dimensions.
4467 static __isl_give isl_aff
*extract_aff_from_equality(
4468 __isl_keep isl_basic_map
*bmap
, int pos
, int eq
, int div
, int ineq
,
4469 __isl_keep isl_multi_aff
*ma
)
4472 unsigned n_div
, n_out
;
4474 isl_local_space
*ls
;
4475 isl_aff
*aff
, *shift
;
4478 ctx
= isl_basic_map_get_ctx(bmap
);
4479 ls
= isl_basic_map_get_local_space(bmap
);
4480 ls
= isl_local_space_domain(ls
);
4481 aff
= isl_aff_alloc(isl_local_space_copy(ls
));
4484 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
4485 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4486 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4487 if (isl_int_is_neg(bmap
->eq
[eq
][o_out
+ pos
])) {
4488 isl_seq_cpy(aff
->v
->el
+ 1, bmap
->eq
[eq
], o_out
);
4489 isl_seq_cpy(aff
->v
->el
+ 1 + o_out
,
4490 bmap
->eq
[eq
] + o_out
+ n_out
, n_div
);
4492 isl_seq_neg(aff
->v
->el
+ 1, bmap
->eq
[eq
], o_out
);
4493 isl_seq_neg(aff
->v
->el
+ 1 + o_out
,
4494 bmap
->eq
[eq
] + o_out
+ n_out
, n_div
);
4497 isl_int_set_si(aff
->v
->el
[1 + o_out
+ div
], 0);
4498 isl_int_abs(aff
->v
->el
[0], bmap
->eq
[eq
][o_out
+ pos
]);
4499 aff
= subtract_initial(aff
, ma
, pos
, bmap
->eq
[eq
] + o_out
,
4500 bmap
->eq
[eq
][o_out
+ pos
]);
4502 shift
= isl_aff_alloc(isl_local_space_copy(ls
));
4505 isl_seq_cpy(shift
->v
->el
+ 1, bmap
->ineq
[ineq
], o_out
);
4506 isl_seq_cpy(shift
->v
->el
+ 1 + o_out
,
4507 bmap
->ineq
[ineq
] + o_out
+ n_out
, n_div
);
4508 isl_int_set_si(shift
->v
->el
[0], 1);
4509 shift
= subtract_initial(shift
, ma
, pos
,
4510 bmap
->ineq
[ineq
] + o_out
, ctx
->negone
);
4511 aff
= isl_aff_add(aff
, isl_aff_copy(shift
));
4512 mod
= isl_val_int_from_isl_int(ctx
,
4513 bmap
->eq
[eq
][o_out
+ n_out
+ div
]);
4514 mod
= isl_val_abs(mod
);
4515 aff
= isl_aff_mod_val(aff
, mod
);
4516 aff
= isl_aff_sub(aff
, shift
);
4519 isl_local_space_free(ls
);
4522 isl_local_space_free(ls
);
4527 /* Given a basic map with output dimensions defined
4528 * in terms of the parameters input dimensions and earlier
4529 * output dimensions using an equality (and possibly a pair on inequalities),
4530 * extract an isl_aff that expresses output dimension "pos" in terms
4531 * of the parameters and input dimensions.
4532 * Note that this expression may involve integer divisions defined
4533 * in terms of parameters and input dimensions.
4534 * "ma" contains the expressions corresponding to earlier output dimensions.
4536 * This function shares some similarities with
4537 * isl_basic_map_has_defining_equality and isl_constraint_get_bound.
4539 static __isl_give isl_aff
*extract_isl_aff_from_basic_map(
4540 __isl_keep isl_basic_map
*bmap
, int pos
, __isl_keep isl_multi_aff
*ma
)
4547 eq
= isl_basic_map_output_defining_equality(bmap
, pos
, &div
, &ineq
);
4548 if (eq
>= bmap
->n_eq
)
4549 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
4550 "unable to find suitable equality", return NULL
);
4551 aff
= extract_aff_from_equality(bmap
, pos
, eq
, div
, ineq
, ma
);
4553 aff
= isl_aff_remove_unused_divs(aff
);
4557 /* Given a basic map where each output dimension is defined
4558 * in terms of the parameters and input dimensions using an equality,
4559 * extract an isl_multi_aff that expresses the output dimensions in terms
4560 * of the parameters and input dimensions.
4562 static __isl_give isl_multi_aff
*extract_isl_multi_aff_from_basic_map(
4563 __isl_take isl_basic_map
*bmap
)
4572 ma
= isl_multi_aff_alloc(isl_basic_map_get_space(bmap
));
4573 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4575 for (i
= 0; i
< n_out
; ++i
) {
4578 aff
= extract_isl_aff_from_basic_map(bmap
, i
, ma
);
4579 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
4582 isl_basic_map_free(bmap
);
4587 /* Given a basic set where each set dimension is defined
4588 * in terms of the parameters using an equality,
4589 * extract an isl_multi_aff that expresses the set dimensions in terms
4590 * of the parameters.
4592 __isl_give isl_multi_aff
*isl_multi_aff_from_basic_set_equalities(
4593 __isl_take isl_basic_set
*bset
)
4595 return extract_isl_multi_aff_from_basic_map(bset
);
4598 /* Create an isl_pw_multi_aff that is equivalent to
4599 * isl_map_intersect_domain(isl_map_from_basic_map(bmap), domain).
4600 * The given basic map is such that each output dimension is defined
4601 * in terms of the parameters and input dimensions using an equality.
4603 * Since some applications expect the result of isl_pw_multi_aff_from_map
4604 * to only contain integer affine expressions, we compute the floor
4605 * of the expression before returning.
4607 * Remove all constraints involving local variables without
4608 * an explicit representation (resulting in the removal of those
4609 * local variables) prior to the actual extraction to ensure
4610 * that the local spaces in which the resulting affine expressions
4611 * are created do not contain any unknown local variables.
4612 * Removing such constraints is safe because constraints involving
4613 * unknown local variables are not used to determine whether
4614 * a basic map is obviously single-valued.
4616 static __isl_give isl_pw_multi_aff
*plain_pw_multi_aff_from_map(
4617 __isl_take isl_set
*domain
, __isl_take isl_basic_map
*bmap
)
4621 bmap
= isl_basic_map_drop_constraint_involving_unknown_divs(bmap
);
4622 ma
= extract_isl_multi_aff_from_basic_map(bmap
);
4623 ma
= isl_multi_aff_floor(ma
);
4624 return isl_pw_multi_aff_alloc(domain
, ma
);
4627 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4628 * This obviously only works if the input "map" is single-valued.
4629 * If so, we compute the lexicographic minimum of the image in the form
4630 * of an isl_pw_multi_aff. Since the image is unique, it is equal
4631 * to its lexicographic minimum.
4632 * If the input is not single-valued, we produce an error.
4634 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_base(
4635 __isl_take isl_map
*map
)
4639 isl_pw_multi_aff
*pma
;
4641 sv
= isl_map_is_single_valued(map
);
4645 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
4646 "map is not single-valued", goto error
);
4647 map
= isl_map_make_disjoint(map
);
4651 pma
= isl_pw_multi_aff_empty(isl_map_get_space(map
));
4653 for (i
= 0; i
< map
->n
; ++i
) {
4654 isl_pw_multi_aff
*pma_i
;
4655 isl_basic_map
*bmap
;
4656 bmap
= isl_basic_map_copy(map
->p
[i
]);
4657 pma_i
= isl_basic_map_lexmin_pw_multi_aff(bmap
);
4658 pma
= isl_pw_multi_aff_add_disjoint(pma
, pma_i
);
4668 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
4669 * taking into account that the output dimension at position "d"
4670 * can be represented as
4672 * x = floor((e(...) + c1) / m)
4674 * given that constraint "i" is of the form
4676 * e(...) + c1 - m x >= 0
4679 * Let "map" be of the form
4683 * We construct a mapping
4685 * A -> [A -> x = floor(...)]
4687 * apply that to the map, obtaining
4689 * [A -> x = floor(...)] -> B
4691 * and equate dimension "d" to x.
4692 * We then compute a isl_pw_multi_aff representation of the resulting map
4693 * and plug in the mapping above.
4695 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_div(
4696 __isl_take isl_map
*map
, __isl_take isl_basic_map
*hull
, int d
, int i
)
4700 isl_local_space
*ls
;
4708 isl_pw_multi_aff
*pma
;
4711 is_set
= isl_map_is_set(map
);
4715 offset
= isl_basic_map_offset(hull
, isl_dim_out
);
4716 ctx
= isl_map_get_ctx(map
);
4717 space
= isl_space_domain(isl_map_get_space(map
));
4718 n_in
= isl_space_dim(space
, isl_dim_set
);
4719 n
= isl_space_dim(space
, isl_dim_all
);
4721 v
= isl_vec_alloc(ctx
, 1 + 1 + n
);
4723 isl_int_neg(v
->el
[0], hull
->ineq
[i
][offset
+ d
]);
4724 isl_seq_cpy(v
->el
+ 1, hull
->ineq
[i
], 1 + n
);
4726 isl_basic_map_free(hull
);
4728 ls
= isl_local_space_from_space(isl_space_copy(space
));
4729 aff
= isl_aff_alloc_vec(ls
, v
);
4730 aff
= isl_aff_floor(aff
);
4732 isl_space_free(space
);
4733 ma
= isl_multi_aff_from_aff(aff
);
4735 ma
= isl_multi_aff_identity(isl_space_map_from_set(space
));
4736 ma
= isl_multi_aff_range_product(ma
,
4737 isl_multi_aff_from_aff(aff
));
4740 insert
= isl_map_from_multi_aff_internal(isl_multi_aff_copy(ma
));
4741 map
= isl_map_apply_domain(map
, insert
);
4742 map
= isl_map_equate(map
, isl_dim_in
, n_in
, isl_dim_out
, d
);
4743 pma
= isl_pw_multi_aff_from_map(map
);
4744 pma
= isl_pw_multi_aff_pullback_multi_aff(pma
, ma
);
4749 isl_basic_map_free(hull
);
4753 /* Is constraint "c" of the form
4755 * e(...) + c1 - m x >= 0
4759 * -e(...) + c2 + m x >= 0
4761 * where m > 1 and e only depends on parameters and input dimemnsions?
4763 * "offset" is the offset of the output dimensions
4764 * "pos" is the position of output dimension x.
4766 static int is_potential_div_constraint(isl_int
*c
, int offset
, int d
, int total
)
4768 if (isl_int_is_zero(c
[offset
+ d
]))
4770 if (isl_int_is_one(c
[offset
+ d
]))
4772 if (isl_int_is_negone(c
[offset
+ d
]))
4774 if (isl_seq_first_non_zero(c
+ offset
, d
) != -1)
4776 if (isl_seq_first_non_zero(c
+ offset
+ d
+ 1,
4777 total
- (offset
+ d
+ 1)) != -1)
4782 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4784 * As a special case, we first check if there is any pair of constraints,
4785 * shared by all the basic maps in "map" that force a given dimension
4786 * to be equal to the floor of some affine combination of the input dimensions.
4788 * In particular, if we can find two constraints
4790 * e(...) + c1 - m x >= 0 i.e., m x <= e(...) + c1
4794 * -e(...) + c2 + m x >= 0 i.e., m x >= e(...) - c2
4796 * where m > 1 and e only depends on parameters and input dimemnsions,
4799 * c1 + c2 < m i.e., -c2 >= c1 - (m - 1)
4801 * then we know that we can take
4803 * x = floor((e(...) + c1) / m)
4805 * without having to perform any computation.
4807 * Note that we know that
4811 * If c1 + c2 were 0, then we would have detected an equality during
4812 * simplification. If c1 + c2 were negative, then we would have detected
4815 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_check_div(
4816 __isl_take isl_map
*map
)
4822 isl_basic_map
*hull
;
4824 hull
= isl_map_unshifted_simple_hull(isl_map_copy(map
));
4829 dim
= isl_map_dim(map
, isl_dim_out
);
4830 offset
= isl_basic_map_offset(hull
, isl_dim_out
);
4831 total
= 1 + isl_basic_map_total_dim(hull
);
4833 for (d
= 0; d
< dim
; ++d
) {
4834 for (i
= 0; i
< n
; ++i
) {
4835 if (!is_potential_div_constraint(hull
->ineq
[i
],
4838 for (j
= i
+ 1; j
< n
; ++j
) {
4839 if (!isl_seq_is_neg(hull
->ineq
[i
] + 1,
4840 hull
->ineq
[j
] + 1, total
- 1))
4842 isl_int_add(sum
, hull
->ineq
[i
][0],
4844 if (isl_int_abs_lt(sum
,
4845 hull
->ineq
[i
][offset
+ d
]))
4852 if (isl_int_is_pos(hull
->ineq
[j
][offset
+ d
]))
4854 return pw_multi_aff_from_map_div(map
, hull
, d
, j
);
4858 isl_basic_map_free(hull
);
4859 return pw_multi_aff_from_map_base(map
);
4862 isl_basic_map_free(hull
);
4866 /* Given an affine expression
4868 * [A -> B] -> f(A,B)
4870 * construct an isl_multi_aff
4874 * such that dimension "d" in B' is set to "aff" and the remaining
4875 * dimensions are set equal to the corresponding dimensions in B.
4876 * "n_in" is the dimension of the space A.
4877 * "n_out" is the dimension of the space B.
4879 * If "is_set" is set, then the affine expression is of the form
4883 * and we construct an isl_multi_aff
4887 static __isl_give isl_multi_aff
*range_map(__isl_take isl_aff
*aff
, int d
,
4888 unsigned n_in
, unsigned n_out
, int is_set
)
4892 isl_space
*space
, *space2
;
4893 isl_local_space
*ls
;
4895 space
= isl_aff_get_domain_space(aff
);
4896 ls
= isl_local_space_from_space(isl_space_copy(space
));
4897 space2
= isl_space_copy(space
);
4899 space2
= isl_space_range(isl_space_unwrap(space2
));
4900 space
= isl_space_map_from_domain_and_range(space
, space2
);
4901 ma
= isl_multi_aff_alloc(space
);
4902 ma
= isl_multi_aff_set_aff(ma
, d
, aff
);
4904 for (i
= 0; i
< n_out
; ++i
) {
4907 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
4908 isl_dim_set
, n_in
+ i
);
4909 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
4912 isl_local_space_free(ls
);
4917 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
4918 * taking into account that the dimension at position "d" can be written as
4920 * x = m a + f(..) (1)
4922 * where m is equal to "gcd".
4923 * "i" is the index of the equality in "hull" that defines f(..).
4924 * In particular, the equality is of the form
4926 * f(..) - x + m g(existentials) = 0
4930 * -f(..) + x + m g(existentials) = 0
4932 * We basically plug (1) into "map", resulting in a map with "a"
4933 * in the range instead of "x". The corresponding isl_pw_multi_aff
4934 * defining "a" is then plugged back into (1) to obtain a definition for "x".
4936 * Specifically, given the input map
4940 * We first wrap it into a set
4944 * and define (1) on top of the corresponding space, resulting in "aff".
4945 * We use this to create an isl_multi_aff that maps the output position "d"
4946 * from "a" to "x", leaving all other (intput and output) dimensions unchanged.
4947 * We plug this into the wrapped map, unwrap the result and compute the
4948 * corresponding isl_pw_multi_aff.
4949 * The result is an expression
4957 * so that we can plug that into "aff", after extending the latter to
4963 * If "map" is actually a set, then there is no "A" space, meaning
4964 * that we do not need to perform any wrapping, and that the result
4965 * of the recursive call is of the form
4969 * which is plugged into a mapping of the form
4973 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_stride(
4974 __isl_take isl_map
*map
, __isl_take isl_basic_map
*hull
, int d
, int i
,
4979 isl_local_space
*ls
;
4982 isl_pw_multi_aff
*pma
, *id
;
4988 is_set
= isl_map_is_set(map
);
4992 n_in
= isl_basic_map_dim(hull
, isl_dim_in
);
4993 n_out
= isl_basic_map_dim(hull
, isl_dim_out
);
4994 o_out
= isl_basic_map_offset(hull
, isl_dim_out
);
4999 set
= isl_map_wrap(map
);
5000 space
= isl_space_map_from_set(isl_set_get_space(set
));
5001 ma
= isl_multi_aff_identity(space
);
5002 ls
= isl_local_space_from_space(isl_set_get_space(set
));
5003 aff
= isl_aff_alloc(ls
);
5005 isl_int_set_si(aff
->v
->el
[0], 1);
5006 if (isl_int_is_one(hull
->eq
[i
][o_out
+ d
]))
5007 isl_seq_neg(aff
->v
->el
+ 1, hull
->eq
[i
],
5010 isl_seq_cpy(aff
->v
->el
+ 1, hull
->eq
[i
],
5012 isl_int_set(aff
->v
->el
[1 + o_out
+ d
], gcd
);
5014 ma
= isl_multi_aff_set_aff(ma
, n_in
+ d
, isl_aff_copy(aff
));
5015 set
= isl_set_preimage_multi_aff(set
, ma
);
5017 ma
= range_map(aff
, d
, n_in
, n_out
, is_set
);
5022 map
= isl_set_unwrap(set
);
5023 pma
= isl_pw_multi_aff_from_map(map
);
5026 space
= isl_pw_multi_aff_get_domain_space(pma
);
5027 space
= isl_space_map_from_set(space
);
5028 id
= isl_pw_multi_aff_identity(space
);
5029 pma
= isl_pw_multi_aff_range_product(id
, pma
);
5031 id
= isl_pw_multi_aff_from_multi_aff(ma
);
5032 pma
= isl_pw_multi_aff_pullback_pw_multi_aff(id
, pma
);
5034 isl_basic_map_free(hull
);
5038 isl_basic_map_free(hull
);
5042 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5043 * "hull" contains the equalities valid for "map".
5045 * Check if any of the output dimensions is "strided".
5046 * That is, we check if it can be written as
5050 * with m greater than 1, a some combination of existentially quantified
5051 * variables and f an expression in the parameters and input dimensions.
5052 * If so, we remove the stride in pw_multi_aff_from_map_stride.
5054 * Otherwise, we continue with pw_multi_aff_from_map_check_div for a further
5057 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_check_strides(
5058 __isl_take isl_map
*map
, __isl_take isl_basic_map
*hull
)
5067 n_div
= isl_basic_map_dim(hull
, isl_dim_div
);
5068 o_div
= isl_basic_map_offset(hull
, isl_dim_div
);
5071 isl_basic_map_free(hull
);
5072 return pw_multi_aff_from_map_check_div(map
);
5077 n_out
= isl_basic_map_dim(hull
, isl_dim_out
);
5078 o_out
= isl_basic_map_offset(hull
, isl_dim_out
);
5080 for (i
= 0; i
< n_out
; ++i
) {
5081 for (j
= 0; j
< hull
->n_eq
; ++j
) {
5082 isl_int
*eq
= hull
->eq
[j
];
5083 isl_pw_multi_aff
*res
;
5085 if (!isl_int_is_one(eq
[o_out
+ i
]) &&
5086 !isl_int_is_negone(eq
[o_out
+ i
]))
5088 if (isl_seq_first_non_zero(eq
+ o_out
, i
) != -1)
5090 if (isl_seq_first_non_zero(eq
+ o_out
+ i
+ 1,
5091 n_out
- (i
+ 1)) != -1)
5093 isl_seq_gcd(eq
+ o_div
, n_div
, &gcd
);
5094 if (isl_int_is_zero(gcd
))
5096 if (isl_int_is_one(gcd
))
5099 res
= pw_multi_aff_from_map_stride(map
, hull
,
5107 isl_basic_map_free(hull
);
5108 return pw_multi_aff_from_map_check_div(map
);
5111 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5113 * As a special case, we first check if all output dimensions are uniquely
5114 * defined in terms of the parameters and input dimensions over the entire
5115 * domain. If so, we extract the desired isl_pw_multi_aff directly
5116 * from the affine hull of "map" and its domain.
5118 * Otherwise, continue with pw_multi_aff_from_map_check_strides for more
5121 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_map(__isl_take isl_map
*map
)
5124 isl_basic_map
*hull
;
5129 if (isl_map_n_basic_map(map
) == 1) {
5130 hull
= isl_map_unshifted_simple_hull(isl_map_copy(map
));
5131 hull
= isl_basic_map_plain_affine_hull(hull
);
5132 sv
= isl_basic_map_plain_is_single_valued(hull
);
5134 return plain_pw_multi_aff_from_map(isl_map_domain(map
),
5136 isl_basic_map_free(hull
);
5138 map
= isl_map_detect_equalities(map
);
5139 hull
= isl_map_unshifted_simple_hull(isl_map_copy(map
));
5140 sv
= isl_basic_map_plain_is_single_valued(hull
);
5142 return plain_pw_multi_aff_from_map(isl_map_domain(map
), hull
);
5144 return pw_multi_aff_from_map_check_strides(map
, hull
);
5145 isl_basic_map_free(hull
);
5150 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_set(__isl_take isl_set
*set
)
5152 return isl_pw_multi_aff_from_map(set
);
5155 /* Convert "map" into an isl_pw_multi_aff (if possible) and
5158 static isl_stat
pw_multi_aff_from_map(__isl_take isl_map
*map
, void *user
)
5160 isl_union_pw_multi_aff
**upma
= user
;
5161 isl_pw_multi_aff
*pma
;
5163 pma
= isl_pw_multi_aff_from_map(map
);
5164 *upma
= isl_union_pw_multi_aff_add_pw_multi_aff(*upma
, pma
);
5166 return *upma
? isl_stat_ok
: isl_stat_error
;
5169 /* Create an isl_union_pw_multi_aff with the given isl_aff on a universe
5172 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_aff(
5173 __isl_take isl_aff
*aff
)
5176 isl_pw_multi_aff
*pma
;
5178 ma
= isl_multi_aff_from_aff(aff
);
5179 pma
= isl_pw_multi_aff_from_multi_aff(ma
);
5180 return isl_union_pw_multi_aff_from_pw_multi_aff(pma
);
5183 /* Try and create an isl_union_pw_multi_aff that is equivalent
5184 * to the given isl_union_map.
5185 * The isl_union_map is required to be single-valued in each space.
5186 * Otherwise, an error is produced.
5188 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_union_map(
5189 __isl_take isl_union_map
*umap
)
5192 isl_union_pw_multi_aff
*upma
;
5194 space
= isl_union_map_get_space(umap
);
5195 upma
= isl_union_pw_multi_aff_empty(space
);
5196 if (isl_union_map_foreach_map(umap
, &pw_multi_aff_from_map
, &upma
) < 0)
5197 upma
= isl_union_pw_multi_aff_free(upma
);
5198 isl_union_map_free(umap
);
5203 /* Try and create an isl_union_pw_multi_aff that is equivalent
5204 * to the given isl_union_set.
5205 * The isl_union_set is required to be a singleton in each space.
5206 * Otherwise, an error is produced.
5208 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_union_set(
5209 __isl_take isl_union_set
*uset
)
5211 return isl_union_pw_multi_aff_from_union_map(uset
);
5214 /* Return the piecewise affine expression "set ? 1 : 0".
5216 __isl_give isl_pw_aff
*isl_set_indicator_function(__isl_take isl_set
*set
)
5219 isl_space
*space
= isl_set_get_space(set
);
5220 isl_local_space
*ls
= isl_local_space_from_space(space
);
5221 isl_aff
*zero
= isl_aff_zero_on_domain(isl_local_space_copy(ls
));
5222 isl_aff
*one
= isl_aff_zero_on_domain(ls
);
5224 one
= isl_aff_add_constant_si(one
, 1);
5225 pa
= isl_pw_aff_alloc(isl_set_copy(set
), one
);
5226 set
= isl_set_complement(set
);
5227 pa
= isl_pw_aff_add_disjoint(pa
, isl_pw_aff_alloc(set
, zero
));
5232 /* Plug in "subs" for dimension "type", "pos" of "aff".
5234 * Let i be the dimension to replace and let "subs" be of the form
5238 * and "aff" of the form
5244 * (a f + d g')/(m d)
5246 * where g' is the result of plugging in "subs" in each of the integer
5249 __isl_give isl_aff
*isl_aff_substitute(__isl_take isl_aff
*aff
,
5250 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
5255 aff
= isl_aff_cow(aff
);
5257 return isl_aff_free(aff
);
5259 ctx
= isl_aff_get_ctx(aff
);
5260 if (!isl_space_is_equal(aff
->ls
->dim
, subs
->ls
->dim
))
5261 isl_die(ctx
, isl_error_invalid
,
5262 "spaces don't match", return isl_aff_free(aff
));
5263 if (isl_local_space_dim(subs
->ls
, isl_dim_div
) != 0)
5264 isl_die(ctx
, isl_error_unsupported
,
5265 "cannot handle divs yet", return isl_aff_free(aff
));
5267 aff
->ls
= isl_local_space_substitute(aff
->ls
, type
, pos
, subs
);
5269 return isl_aff_free(aff
);
5271 aff
->v
= isl_vec_cow(aff
->v
);
5273 return isl_aff_free(aff
);
5275 pos
+= isl_local_space_offset(aff
->ls
, type
);
5278 isl_seq_substitute(aff
->v
->el
, pos
, subs
->v
->el
,
5279 aff
->v
->size
, subs
->v
->size
, v
);
5285 /* Plug in "subs" for dimension "type", "pos" in each of the affine
5286 * expressions in "maff".
5288 __isl_give isl_multi_aff
*isl_multi_aff_substitute(
5289 __isl_take isl_multi_aff
*maff
, enum isl_dim_type type
, unsigned pos
,
5290 __isl_keep isl_aff
*subs
)
5294 maff
= isl_multi_aff_cow(maff
);
5296 return isl_multi_aff_free(maff
);
5298 if (type
== isl_dim_in
)
5301 for (i
= 0; i
< maff
->n
; ++i
) {
5302 maff
->u
.p
[i
] = isl_aff_substitute(maff
->u
.p
[i
],
5305 return isl_multi_aff_free(maff
);
5311 /* Plug in "subs" for dimension "type", "pos" of "pma".
5313 * pma is of the form
5317 * while subs is of the form
5319 * v' = B_j(v) -> S_j
5321 * Each pair i,j such that C_ij = A_i \cap B_i is non-empty
5322 * has a contribution in the result, in particular
5324 * C_ij(S_j) -> M_i(S_j)
5326 * Note that plugging in S_j in C_ij may also result in an empty set
5327 * and this contribution should simply be discarded.
5329 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_substitute(
5330 __isl_take isl_pw_multi_aff
*pma
, enum isl_dim_type type
, unsigned pos
,
5331 __isl_keep isl_pw_aff
*subs
)
5334 isl_pw_multi_aff
*res
;
5337 return isl_pw_multi_aff_free(pma
);
5339 n
= pma
->n
* subs
->n
;
5340 res
= isl_pw_multi_aff_alloc_size(isl_space_copy(pma
->dim
), n
);
5342 for (i
= 0; i
< pma
->n
; ++i
) {
5343 for (j
= 0; j
< subs
->n
; ++j
) {
5345 isl_multi_aff
*res_ij
;
5348 common
= isl_set_intersect(
5349 isl_set_copy(pma
->p
[i
].set
),
5350 isl_set_copy(subs
->p
[j
].set
));
5351 common
= isl_set_substitute(common
,
5352 type
, pos
, subs
->p
[j
].aff
);
5353 empty
= isl_set_plain_is_empty(common
);
5354 if (empty
< 0 || empty
) {
5355 isl_set_free(common
);
5361 res_ij
= isl_multi_aff_substitute(
5362 isl_multi_aff_copy(pma
->p
[i
].maff
),
5363 type
, pos
, subs
->p
[j
].aff
);
5365 res
= isl_pw_multi_aff_add_piece(res
, common
, res_ij
);
5369 isl_pw_multi_aff_free(pma
);
5372 isl_pw_multi_aff_free(pma
);
5373 isl_pw_multi_aff_free(res
);
5377 /* Compute the preimage of a range of dimensions in the affine expression "src"
5378 * under "ma" and put the result in "dst". The number of dimensions in "src"
5379 * that precede the range is given by "n_before". The number of dimensions
5380 * in the range is given by the number of output dimensions of "ma".
5381 * The number of dimensions that follow the range is given by "n_after".
5382 * If "has_denom" is set (to one),
5383 * then "src" and "dst" have an extra initial denominator.
5384 * "n_div_ma" is the number of existentials in "ma"
5385 * "n_div_bset" is the number of existentials in "src"
5386 * The resulting "dst" (which is assumed to have been allocated by
5387 * the caller) contains coefficients for both sets of existentials,
5388 * first those in "ma" and then those in "src".
5389 * f, c1, c2 and g are temporary objects that have been initialized
5392 * Let src represent the expression
5394 * (a(p) + f_u u + b v + f_w w + c(divs))/d
5396 * and let ma represent the expressions
5398 * v_i = (r_i(p) + s_i(y) + t_i(divs'))/m_i
5400 * We start out with the following expression for dst:
5402 * (a(p) + f_u u + 0 y + f_w w + 0 divs' + c(divs) + f \sum_i b_i v_i)/d
5404 * with the multiplication factor f initially equal to 1
5405 * and f \sum_i b_i v_i kept separately.
5406 * For each x_i that we substitute, we multiply the numerator
5407 * (and denominator) of dst by c_1 = m_i and add the numerator
5408 * of the x_i expression multiplied by c_2 = f b_i,
5409 * after removing the common factors of c_1 and c_2.
5410 * The multiplication factor f also needs to be multiplied by c_1
5411 * for the next x_j, j > i.
5413 void isl_seq_preimage(isl_int
*dst
, isl_int
*src
,
5414 __isl_keep isl_multi_aff
*ma
, int n_before
, int n_after
,
5415 int n_div_ma
, int n_div_bmap
,
5416 isl_int f
, isl_int c1
, isl_int c2
, isl_int g
, int has_denom
)
5419 int n_param
, n_in
, n_out
;
5422 n_param
= isl_multi_aff_dim(ma
, isl_dim_param
);
5423 n_in
= isl_multi_aff_dim(ma
, isl_dim_in
);
5424 n_out
= isl_multi_aff_dim(ma
, isl_dim_out
);
5426 isl_seq_cpy(dst
, src
, has_denom
+ 1 + n_param
+ n_before
);
5427 o_dst
= o_src
= has_denom
+ 1 + n_param
+ n_before
;
5428 isl_seq_clr(dst
+ o_dst
, n_in
);
5431 isl_seq_cpy(dst
+ o_dst
, src
+ o_src
, n_after
);
5434 isl_seq_clr(dst
+ o_dst
, n_div_ma
);
5436 isl_seq_cpy(dst
+ o_dst
, src
+ o_src
, n_div_bmap
);
5438 isl_int_set_si(f
, 1);
5440 for (i
= 0; i
< n_out
; ++i
) {
5441 int offset
= has_denom
+ 1 + n_param
+ n_before
+ i
;
5443 if (isl_int_is_zero(src
[offset
]))
5445 isl_int_set(c1
, ma
->u
.p
[i
]->v
->el
[0]);
5446 isl_int_mul(c2
, f
, src
[offset
]);
5447 isl_int_gcd(g
, c1
, c2
);
5448 isl_int_divexact(c1
, c1
, g
);
5449 isl_int_divexact(c2
, c2
, g
);
5451 isl_int_mul(f
, f
, c1
);
5454 isl_seq_combine(dst
+ o_dst
, c1
, dst
+ o_dst
,
5455 c2
, ma
->u
.p
[i
]->v
->el
+ o_src
, 1 + n_param
);
5456 o_dst
+= 1 + n_param
;
5457 o_src
+= 1 + n_param
;
5458 isl_seq_scale(dst
+ o_dst
, dst
+ o_dst
, c1
, n_before
);
5460 isl_seq_combine(dst
+ o_dst
, c1
, dst
+ o_dst
,
5461 c2
, ma
->u
.p
[i
]->v
->el
+ o_src
, n_in
);
5464 isl_seq_scale(dst
+ o_dst
, dst
+ o_dst
, c1
, n_after
);
5466 isl_seq_combine(dst
+ o_dst
, c1
, dst
+ o_dst
,
5467 c2
, ma
->u
.p
[i
]->v
->el
+ o_src
, n_div_ma
);
5470 isl_seq_scale(dst
+ o_dst
, dst
+ o_dst
, c1
, n_div_bmap
);
5472 isl_int_mul(dst
[0], dst
[0], c1
);
5476 /* Compute the pullback of "aff" by the function represented by "ma".
5477 * In other words, plug in "ma" in "aff". The result is an affine expression
5478 * defined over the domain space of "ma".
5480 * If "aff" is represented by
5482 * (a(p) + b x + c(divs))/d
5484 * and ma is represented by
5486 * x = D(p) + F(y) + G(divs')
5488 * then the result is
5490 * (a(p) + b D(p) + b F(y) + b G(divs') + c(divs))/d
5492 * The divs in the local space of the input are similarly adjusted
5493 * through a call to isl_local_space_preimage_multi_aff.
5495 __isl_give isl_aff
*isl_aff_pullback_multi_aff(__isl_take isl_aff
*aff
,
5496 __isl_take isl_multi_aff
*ma
)
5498 isl_aff
*res
= NULL
;
5499 isl_local_space
*ls
;
5500 int n_div_aff
, n_div_ma
;
5501 isl_int f
, c1
, c2
, g
;
5503 ma
= isl_multi_aff_align_divs(ma
);
5507 n_div_aff
= isl_aff_dim(aff
, isl_dim_div
);
5508 n_div_ma
= ma
->n
? isl_aff_dim(ma
->u
.p
[0], isl_dim_div
) : 0;
5510 ls
= isl_aff_get_domain_local_space(aff
);
5511 ls
= isl_local_space_preimage_multi_aff(ls
, isl_multi_aff_copy(ma
));
5512 res
= isl_aff_alloc(ls
);
5521 isl_seq_preimage(res
->v
->el
, aff
->v
->el
, ma
, 0, 0, n_div_ma
, n_div_aff
,
5530 isl_multi_aff_free(ma
);
5531 res
= isl_aff_normalize(res
);
5535 isl_multi_aff_free(ma
);
5540 /* Compute the pullback of "aff1" by the function represented by "aff2".
5541 * In other words, plug in "aff2" in "aff1". The result is an affine expression
5542 * defined over the domain space of "aff1".
5544 * The domain of "aff1" should match the range of "aff2", which means
5545 * that it should be single-dimensional.
5547 __isl_give isl_aff
*isl_aff_pullback_aff(__isl_take isl_aff
*aff1
,
5548 __isl_take isl_aff
*aff2
)
5552 ma
= isl_multi_aff_from_aff(aff2
);
5553 return isl_aff_pullback_multi_aff(aff1
, ma
);
5556 /* Compute the pullback of "ma1" by the function represented by "ma2".
5557 * In other words, plug in "ma2" in "ma1".
5559 * The parameters of "ma1" and "ma2" are assumed to have been aligned.
5561 static __isl_give isl_multi_aff
*isl_multi_aff_pullback_multi_aff_aligned(
5562 __isl_take isl_multi_aff
*ma1
, __isl_take isl_multi_aff
*ma2
)
5565 isl_space
*space
= NULL
;
5567 ma2
= isl_multi_aff_align_divs(ma2
);
5568 ma1
= isl_multi_aff_cow(ma1
);
5572 space
= isl_space_join(isl_multi_aff_get_space(ma2
),
5573 isl_multi_aff_get_space(ma1
));
5575 for (i
= 0; i
< ma1
->n
; ++i
) {
5576 ma1
->u
.p
[i
] = isl_aff_pullback_multi_aff(ma1
->u
.p
[i
],
5577 isl_multi_aff_copy(ma2
));
5582 ma1
= isl_multi_aff_reset_space(ma1
, space
);
5583 isl_multi_aff_free(ma2
);
5586 isl_space_free(space
);
5587 isl_multi_aff_free(ma2
);
5588 isl_multi_aff_free(ma1
);
5592 /* Compute the pullback of "ma1" by the function represented by "ma2".
5593 * In other words, plug in "ma2" in "ma1".
5595 __isl_give isl_multi_aff
*isl_multi_aff_pullback_multi_aff(
5596 __isl_take isl_multi_aff
*ma1
, __isl_take isl_multi_aff
*ma2
)
5598 return isl_multi_aff_align_params_multi_multi_and(ma1
, ma2
,
5599 &isl_multi_aff_pullback_multi_aff_aligned
);
5602 /* Extend the local space of "dst" to include the divs
5603 * in the local space of "src".
5605 * If "src" does not have any divs or if the local spaces of "dst" and
5606 * "src" are the same, then no extension is required.
5608 __isl_give isl_aff
*isl_aff_align_divs(__isl_take isl_aff
*dst
,
5609 __isl_keep isl_aff
*src
)
5612 int src_n_div
, dst_n_div
;
5619 return isl_aff_free(dst
);
5621 ctx
= isl_aff_get_ctx(src
);
5622 equal
= isl_local_space_has_equal_space(src
->ls
, dst
->ls
);
5624 return isl_aff_free(dst
);
5626 isl_die(ctx
, isl_error_invalid
,
5627 "spaces don't match", goto error
);
5629 src_n_div
= isl_local_space_dim(src
->ls
, isl_dim_div
);
5632 equal
= isl_local_space_is_equal(src
->ls
, dst
->ls
);
5634 return isl_aff_free(dst
);
5638 dst_n_div
= isl_local_space_dim(dst
->ls
, isl_dim_div
);
5639 exp1
= isl_alloc_array(ctx
, int, src_n_div
);
5640 exp2
= isl_alloc_array(ctx
, int, dst_n_div
);
5641 if (!exp1
|| (dst_n_div
&& !exp2
))
5644 div
= isl_merge_divs(src
->ls
->div
, dst
->ls
->div
, exp1
, exp2
);
5645 dst
= isl_aff_expand_divs(dst
, div
, exp2
);
5653 return isl_aff_free(dst
);
5656 /* Adjust the local spaces of the affine expressions in "maff"
5657 * such that they all have the save divs.
5659 __isl_give isl_multi_aff
*isl_multi_aff_align_divs(
5660 __isl_take isl_multi_aff
*maff
)
5668 maff
= isl_multi_aff_cow(maff
);
5672 for (i
= 1; i
< maff
->n
; ++i
)
5673 maff
->u
.p
[0] = isl_aff_align_divs(maff
->u
.p
[0], maff
->u
.p
[i
]);
5674 for (i
= 1; i
< maff
->n
; ++i
) {
5675 maff
->u
.p
[i
] = isl_aff_align_divs(maff
->u
.p
[i
], maff
->u
.p
[0]);
5677 return isl_multi_aff_free(maff
);
5683 __isl_give isl_aff
*isl_aff_lift(__isl_take isl_aff
*aff
)
5685 aff
= isl_aff_cow(aff
);
5689 aff
->ls
= isl_local_space_lift(aff
->ls
);
5691 return isl_aff_free(aff
);
5696 /* Lift "maff" to a space with extra dimensions such that the result
5697 * has no more existentially quantified variables.
5698 * If "ls" is not NULL, then *ls is assigned the local space that lies
5699 * at the basis of the lifting applied to "maff".
5701 __isl_give isl_multi_aff
*isl_multi_aff_lift(__isl_take isl_multi_aff
*maff
,
5702 __isl_give isl_local_space
**ls
)
5716 isl_space
*space
= isl_multi_aff_get_domain_space(maff
);
5717 *ls
= isl_local_space_from_space(space
);
5719 return isl_multi_aff_free(maff
);
5724 maff
= isl_multi_aff_cow(maff
);
5725 maff
= isl_multi_aff_align_divs(maff
);
5729 n_div
= isl_aff_dim(maff
->u
.p
[0], isl_dim_div
);
5730 space
= isl_multi_aff_get_space(maff
);
5731 space
= isl_space_lift(isl_space_domain(space
), n_div
);
5732 space
= isl_space_extend_domain_with_range(space
,
5733 isl_multi_aff_get_space(maff
));
5735 return isl_multi_aff_free(maff
);
5736 isl_space_free(maff
->space
);
5737 maff
->space
= space
;
5740 *ls
= isl_aff_get_domain_local_space(maff
->u
.p
[0]);
5742 return isl_multi_aff_free(maff
);
5745 for (i
= 0; i
< maff
->n
; ++i
) {
5746 maff
->u
.p
[i
] = isl_aff_lift(maff
->u
.p
[i
]);
5754 isl_local_space_free(*ls
);
5755 return isl_multi_aff_free(maff
);
5759 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma".
5761 __isl_give isl_pw_aff
*isl_pw_multi_aff_get_pw_aff(
5762 __isl_keep isl_pw_multi_aff
*pma
, int pos
)
5772 n_out
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
5773 if (pos
< 0 || pos
>= n_out
)
5774 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
5775 "index out of bounds", return NULL
);
5777 space
= isl_pw_multi_aff_get_space(pma
);
5778 space
= isl_space_drop_dims(space
, isl_dim_out
,
5779 pos
+ 1, n_out
- pos
- 1);
5780 space
= isl_space_drop_dims(space
, isl_dim_out
, 0, pos
);
5782 pa
= isl_pw_aff_alloc_size(space
, pma
->n
);
5783 for (i
= 0; i
< pma
->n
; ++i
) {
5785 aff
= isl_multi_aff_get_aff(pma
->p
[i
].maff
, pos
);
5786 pa
= isl_pw_aff_add_piece(pa
, isl_set_copy(pma
->p
[i
].set
), aff
);
5792 /* Return an isl_pw_multi_aff with the given "set" as domain and
5793 * an unnamed zero-dimensional range.
5795 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_domain(
5796 __isl_take isl_set
*set
)
5801 space
= isl_set_get_space(set
);
5802 space
= isl_space_from_domain(space
);
5803 ma
= isl_multi_aff_zero(space
);
5804 return isl_pw_multi_aff_alloc(set
, ma
);
5807 /* Add an isl_pw_multi_aff with the given "set" as domain and
5808 * an unnamed zero-dimensional range to *user.
5810 static isl_stat
add_pw_multi_aff_from_domain(__isl_take isl_set
*set
,
5813 isl_union_pw_multi_aff
**upma
= user
;
5814 isl_pw_multi_aff
*pma
;
5816 pma
= isl_pw_multi_aff_from_domain(set
);
5817 *upma
= isl_union_pw_multi_aff_add_pw_multi_aff(*upma
, pma
);
5822 /* Return an isl_union_pw_multi_aff with the given "uset" as domain and
5823 * an unnamed zero-dimensional range.
5825 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_domain(
5826 __isl_take isl_union_set
*uset
)
5829 isl_union_pw_multi_aff
*upma
;
5834 space
= isl_union_set_get_space(uset
);
5835 upma
= isl_union_pw_multi_aff_empty(space
);
5837 if (isl_union_set_foreach_set(uset
,
5838 &add_pw_multi_aff_from_domain
, &upma
) < 0)
5841 isl_union_set_free(uset
);
5844 isl_union_set_free(uset
);
5845 isl_union_pw_multi_aff_free(upma
);
5849 /* Local data for bin_entry and the callback "fn".
5851 struct isl_union_pw_multi_aff_bin_data
{
5852 isl_union_pw_multi_aff
*upma2
;
5853 isl_union_pw_multi_aff
*res
;
5854 isl_pw_multi_aff
*pma
;
5855 isl_stat (*fn
)(__isl_take isl_pw_multi_aff
*pma
, void *user
);
5858 /* Given an isl_pw_multi_aff from upma1, store it in data->pma
5859 * and call data->fn for each isl_pw_multi_aff in data->upma2.
5861 static isl_stat
bin_entry(__isl_take isl_pw_multi_aff
*pma
, void *user
)
5863 struct isl_union_pw_multi_aff_bin_data
*data
= user
;
5867 r
= isl_union_pw_multi_aff_foreach_pw_multi_aff(data
->upma2
,
5869 isl_pw_multi_aff_free(pma
);
5874 /* Call "fn" on each pair of isl_pw_multi_affs in "upma1" and "upma2".
5875 * The isl_pw_multi_aff from upma1 is stored in data->pma (where data is
5876 * passed as user field) and the isl_pw_multi_aff from upma2 is available
5877 * as *entry. The callback should adjust data->res if desired.
5879 static __isl_give isl_union_pw_multi_aff
*bin_op(
5880 __isl_take isl_union_pw_multi_aff
*upma1
,
5881 __isl_take isl_union_pw_multi_aff
*upma2
,
5882 isl_stat (*fn
)(__isl_take isl_pw_multi_aff
*pma
, void *user
))
5885 struct isl_union_pw_multi_aff_bin_data data
= { NULL
, NULL
, NULL
, fn
};
5887 space
= isl_union_pw_multi_aff_get_space(upma2
);
5888 upma1
= isl_union_pw_multi_aff_align_params(upma1
, space
);
5889 space
= isl_union_pw_multi_aff_get_space(upma1
);
5890 upma2
= isl_union_pw_multi_aff_align_params(upma2
, space
);
5892 if (!upma1
|| !upma2
)
5896 data
.res
= isl_union_pw_multi_aff_alloc_same_size(upma1
);
5897 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma1
,
5898 &bin_entry
, &data
) < 0)
5901 isl_union_pw_multi_aff_free(upma1
);
5902 isl_union_pw_multi_aff_free(upma2
);
5905 isl_union_pw_multi_aff_free(upma1
);
5906 isl_union_pw_multi_aff_free(upma2
);
5907 isl_union_pw_multi_aff_free(data
.res
);
5911 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
5912 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
5914 static __isl_give isl_pw_multi_aff
*pw_multi_aff_range_product(
5915 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
5919 space
= isl_space_range_product(isl_pw_multi_aff_get_space(pma1
),
5920 isl_pw_multi_aff_get_space(pma2
));
5921 return isl_pw_multi_aff_on_shared_domain_in(pma1
, pma2
, space
,
5922 &isl_multi_aff_range_product
);
5925 /* Given two isl_pw_multi_affs A -> B and C -> D,
5926 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
5928 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_range_product(
5929 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
5931 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
5932 &pw_multi_aff_range_product
);
5935 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
5936 * construct an isl_pw_multi_aff (A * C) -> (B, D).
5938 static __isl_give isl_pw_multi_aff
*pw_multi_aff_flat_range_product(
5939 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
5943 space
= isl_space_range_product(isl_pw_multi_aff_get_space(pma1
),
5944 isl_pw_multi_aff_get_space(pma2
));
5945 space
= isl_space_flatten_range(space
);
5946 return isl_pw_multi_aff_on_shared_domain_in(pma1
, pma2
, space
,
5947 &isl_multi_aff_flat_range_product
);
5950 /* Given two isl_pw_multi_affs A -> B and C -> D,
5951 * construct an isl_pw_multi_aff (A * C) -> (B, D).
5953 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_flat_range_product(
5954 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
5956 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
5957 &pw_multi_aff_flat_range_product
);
5960 /* If data->pma and "pma2" have the same domain space, then compute
5961 * their flat range product and the result to data->res.
5963 static isl_stat
flat_range_product_entry(__isl_take isl_pw_multi_aff
*pma2
,
5966 struct isl_union_pw_multi_aff_bin_data
*data
= user
;
5968 if (!isl_space_tuple_is_equal(data
->pma
->dim
, isl_dim_in
,
5969 pma2
->dim
, isl_dim_in
)) {
5970 isl_pw_multi_aff_free(pma2
);
5974 pma2
= isl_pw_multi_aff_flat_range_product(
5975 isl_pw_multi_aff_copy(data
->pma
), pma2
);
5977 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma2
);
5982 /* Given two isl_union_pw_multi_affs A -> B and C -> D,
5983 * construct an isl_union_pw_multi_aff (A * C) -> (B, D).
5985 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_flat_range_product(
5986 __isl_take isl_union_pw_multi_aff
*upma1
,
5987 __isl_take isl_union_pw_multi_aff
*upma2
)
5989 return bin_op(upma1
, upma2
, &flat_range_product_entry
);
5992 /* Replace the affine expressions at position "pos" in "pma" by "pa".
5993 * The parameters are assumed to have been aligned.
5995 * The implementation essentially performs an isl_pw_*_on_shared_domain,
5996 * except that it works on two different isl_pw_* types.
5998 static __isl_give isl_pw_multi_aff
*pw_multi_aff_set_pw_aff(
5999 __isl_take isl_pw_multi_aff
*pma
, unsigned pos
,
6000 __isl_take isl_pw_aff
*pa
)
6003 isl_pw_multi_aff
*res
= NULL
;
6008 if (!isl_space_tuple_is_equal(pma
->dim
, isl_dim_in
,
6009 pa
->dim
, isl_dim_in
))
6010 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
6011 "domains don't match", goto error
);
6012 if (pos
>= isl_pw_multi_aff_dim(pma
, isl_dim_out
))
6013 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
6014 "index out of bounds", goto error
);
6017 res
= isl_pw_multi_aff_alloc_size(isl_pw_multi_aff_get_space(pma
), n
);
6019 for (i
= 0; i
< pma
->n
; ++i
) {
6020 for (j
= 0; j
< pa
->n
; ++j
) {
6022 isl_multi_aff
*res_ij
;
6025 common
= isl_set_intersect(isl_set_copy(pma
->p
[i
].set
),
6026 isl_set_copy(pa
->p
[j
].set
));
6027 empty
= isl_set_plain_is_empty(common
);
6028 if (empty
< 0 || empty
) {
6029 isl_set_free(common
);
6035 res_ij
= isl_multi_aff_set_aff(
6036 isl_multi_aff_copy(pma
->p
[i
].maff
), pos
,
6037 isl_aff_copy(pa
->p
[j
].aff
));
6038 res_ij
= isl_multi_aff_gist(res_ij
,
6039 isl_set_copy(common
));
6041 res
= isl_pw_multi_aff_add_piece(res
, common
, res_ij
);
6045 isl_pw_multi_aff_free(pma
);
6046 isl_pw_aff_free(pa
);
6049 isl_pw_multi_aff_free(pma
);
6050 isl_pw_aff_free(pa
);
6051 return isl_pw_multi_aff_free(res
);
6054 /* Replace the affine expressions at position "pos" in "pma" by "pa".
6056 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_set_pw_aff(
6057 __isl_take isl_pw_multi_aff
*pma
, unsigned pos
,
6058 __isl_take isl_pw_aff
*pa
)
6060 isl_bool equal_params
;
6064 equal_params
= isl_space_has_equal_params(pma
->dim
, pa
->dim
);
6065 if (equal_params
< 0)
6068 return pw_multi_aff_set_pw_aff(pma
, pos
, pa
);
6069 if (isl_pw_multi_aff_check_named_params(pma
) < 0 ||
6070 isl_pw_aff_check_named_params(pa
) < 0)
6072 pma
= isl_pw_multi_aff_align_params(pma
, isl_pw_aff_get_space(pa
));
6073 pa
= isl_pw_aff_align_params(pa
, isl_pw_multi_aff_get_space(pma
));
6074 return pw_multi_aff_set_pw_aff(pma
, pos
, pa
);
6076 isl_pw_multi_aff_free(pma
);
6077 isl_pw_aff_free(pa
);
6081 /* Do the parameters of "pa" match those of "space"?
6083 isl_bool
isl_pw_aff_matching_params(__isl_keep isl_pw_aff
*pa
,
6084 __isl_keep isl_space
*space
)
6086 isl_space
*pa_space
;
6090 return isl_bool_error
;
6092 pa_space
= isl_pw_aff_get_space(pa
);
6094 match
= isl_space_has_equal_params(space
, pa_space
);
6096 isl_space_free(pa_space
);
6100 /* Check that the domain space of "pa" matches "space".
6102 isl_stat
isl_pw_aff_check_match_domain_space(__isl_keep isl_pw_aff
*pa
,
6103 __isl_keep isl_space
*space
)
6105 isl_space
*pa_space
;
6109 return isl_stat_error
;
6111 pa_space
= isl_pw_aff_get_space(pa
);
6113 match
= isl_space_has_equal_params(space
, pa_space
);
6117 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
6118 "parameters don't match", goto error
);
6119 match
= isl_space_tuple_is_equal(space
, isl_dim_in
,
6120 pa_space
, isl_dim_in
);
6124 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
6125 "domains don't match", goto error
);
6126 isl_space_free(pa_space
);
6129 isl_space_free(pa_space
);
6130 return isl_stat_error
;
6138 #include <isl_multi_explicit_domain.c>
6139 #include <isl_multi_pw_aff_explicit_domain.c>
6140 #include <isl_multi_templ.c>
6141 #include <isl_multi_apply_set.c>
6142 #include <isl_multi_coalesce.c>
6143 #include <isl_multi_domain_templ.c>
6144 #include <isl_multi_dims.c>
6145 #include <isl_multi_from_base_templ.c>
6146 #include <isl_multi_gist.c>
6147 #include <isl_multi_hash.c>
6148 #include <isl_multi_identity_templ.c>
6149 #include <isl_multi_align_set.c>
6150 #include <isl_multi_intersect.c>
6151 #include <isl_multi_move_dims_templ.c>
6152 #include <isl_multi_product_templ.c>
6153 #include <isl_multi_splice_templ.c>
6154 #include <isl_multi_zero_templ.c>
6156 /* Does "mpa" have a non-trivial explicit domain?
6158 * The explicit domain, if present, is trivial if it represents
6159 * an (obviously) universe set.
6161 isl_bool
isl_multi_pw_aff_has_non_trivial_domain(
6162 __isl_keep isl_multi_pw_aff
*mpa
)
6165 return isl_bool_error
;
6166 if (!isl_multi_pw_aff_has_explicit_domain(mpa
))
6167 return isl_bool_false
;
6168 return isl_bool_not(isl_set_plain_is_universe(mpa
->u
.dom
));
6171 /* Scale the elements of "pma" by the corresponding elements of "mv".
6173 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_scale_multi_val(
6174 __isl_take isl_pw_multi_aff
*pma
, __isl_take isl_multi_val
*mv
)
6177 isl_bool equal_params
;
6179 pma
= isl_pw_multi_aff_cow(pma
);
6182 if (!isl_space_tuple_is_equal(pma
->dim
, isl_dim_out
,
6183 mv
->space
, isl_dim_set
))
6184 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
6185 "spaces don't match", goto error
);
6186 equal_params
= isl_space_has_equal_params(pma
->dim
, mv
->space
);
6187 if (equal_params
< 0)
6189 if (!equal_params
) {
6190 pma
= isl_pw_multi_aff_align_params(pma
,
6191 isl_multi_val_get_space(mv
));
6192 mv
= isl_multi_val_align_params(mv
,
6193 isl_pw_multi_aff_get_space(pma
));
6198 for (i
= 0; i
< pma
->n
; ++i
) {
6199 pma
->p
[i
].maff
= isl_multi_aff_scale_multi_val(pma
->p
[i
].maff
,
6200 isl_multi_val_copy(mv
));
6201 if (!pma
->p
[i
].maff
)
6205 isl_multi_val_free(mv
);
6208 isl_multi_val_free(mv
);
6209 isl_pw_multi_aff_free(pma
);
6213 /* This function is called for each entry of an isl_union_pw_multi_aff.
6214 * If the space of the entry matches that of data->mv,
6215 * then apply isl_pw_multi_aff_scale_multi_val and return the result.
6216 * Otherwise, return an empty isl_pw_multi_aff.
6218 static __isl_give isl_pw_multi_aff
*union_pw_multi_aff_scale_multi_val_entry(
6219 __isl_take isl_pw_multi_aff
*pma
, void *user
)
6221 isl_multi_val
*mv
= user
;
6225 if (!isl_space_tuple_is_equal(pma
->dim
, isl_dim_out
,
6226 mv
->space
, isl_dim_set
)) {
6227 isl_space
*space
= isl_pw_multi_aff_get_space(pma
);
6228 isl_pw_multi_aff_free(pma
);
6229 return isl_pw_multi_aff_empty(space
);
6232 return isl_pw_multi_aff_scale_multi_val(pma
, isl_multi_val_copy(mv
));
6235 /* Scale the elements of "upma" by the corresponding elements of "mv",
6236 * for those entries that match the space of "mv".
6238 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_scale_multi_val(
6239 __isl_take isl_union_pw_multi_aff
*upma
, __isl_take isl_multi_val
*mv
)
6241 upma
= isl_union_pw_multi_aff_align_params(upma
,
6242 isl_multi_val_get_space(mv
));
6243 mv
= isl_multi_val_align_params(mv
,
6244 isl_union_pw_multi_aff_get_space(upma
));
6248 return isl_union_pw_multi_aff_transform(upma
,
6249 &union_pw_multi_aff_scale_multi_val_entry
, mv
);
6251 isl_multi_val_free(mv
);
6254 isl_multi_val_free(mv
);
6255 isl_union_pw_multi_aff_free(upma
);
6259 /* Construct and return a piecewise multi affine expression
6260 * in the given space with value zero in each of the output dimensions and
6261 * a universe domain.
6263 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_zero(__isl_take isl_space
*space
)
6265 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_zero(space
));
6268 /* Construct and return a piecewise multi affine expression
6269 * that is equal to the given piecewise affine expression.
6271 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_pw_aff(
6272 __isl_take isl_pw_aff
*pa
)
6276 isl_pw_multi_aff
*pma
;
6281 space
= isl_pw_aff_get_space(pa
);
6282 pma
= isl_pw_multi_aff_alloc_size(space
, pa
->n
);
6284 for (i
= 0; i
< pa
->n
; ++i
) {
6288 set
= isl_set_copy(pa
->p
[i
].set
);
6289 ma
= isl_multi_aff_from_aff(isl_aff_copy(pa
->p
[i
].aff
));
6290 pma
= isl_pw_multi_aff_add_piece(pma
, set
, ma
);
6293 isl_pw_aff_free(pa
);
6297 /* Construct and return a piecewise multi affine expression
6298 * that is equal to the given multi piecewise affine expression
6299 * on the shared domain of the piecewise affine expressions,
6300 * in the special case of a 0D multi piecewise affine expression.
6302 * Create a piecewise multi affine expression with the explicit domain of
6303 * the 0D multi piecewise affine expression as domain.
6305 static __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_multi_pw_aff_0D(
6306 __isl_take isl_multi_pw_aff
*mpa
)
6312 space
= isl_multi_pw_aff_get_space(mpa
);
6313 dom
= isl_multi_pw_aff_get_explicit_domain(mpa
);
6314 isl_multi_pw_aff_free(mpa
);
6316 ma
= isl_multi_aff_zero(space
);
6317 return isl_pw_multi_aff_alloc(dom
, ma
);
6320 /* Construct and return a piecewise multi affine expression
6321 * that is equal to the given multi piecewise affine expression
6322 * on the shared domain of the piecewise affine expressions.
6324 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_multi_pw_aff(
6325 __isl_take isl_multi_pw_aff
*mpa
)
6330 isl_pw_multi_aff
*pma
;
6336 return isl_pw_multi_aff_from_multi_pw_aff_0D(mpa
);
6338 space
= isl_multi_pw_aff_get_space(mpa
);
6339 pa
= isl_multi_pw_aff_get_pw_aff(mpa
, 0);
6340 pma
= isl_pw_multi_aff_from_pw_aff(pa
);
6342 for (i
= 1; i
< mpa
->n
; ++i
) {
6343 isl_pw_multi_aff
*pma_i
;
6345 pa
= isl_multi_pw_aff_get_pw_aff(mpa
, i
);
6346 pma_i
= isl_pw_multi_aff_from_pw_aff(pa
);
6347 pma
= isl_pw_multi_aff_range_product(pma
, pma_i
);
6350 pma
= isl_pw_multi_aff_reset_space(pma
, space
);
6352 isl_multi_pw_aff_free(mpa
);
6356 /* Construct and return a multi piecewise affine expression
6357 * that is equal to the given multi affine expression.
6359 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_from_multi_aff(
6360 __isl_take isl_multi_aff
*ma
)
6363 isl_multi_pw_aff
*mpa
;
6368 n
= isl_multi_aff_dim(ma
, isl_dim_out
);
6369 mpa
= isl_multi_pw_aff_alloc(isl_multi_aff_get_space(ma
));
6371 for (i
= 0; i
< n
; ++i
) {
6374 pa
= isl_pw_aff_from_aff(isl_multi_aff_get_aff(ma
, i
));
6375 mpa
= isl_multi_pw_aff_set_pw_aff(mpa
, i
, pa
);
6378 isl_multi_aff_free(ma
);
6382 /* Construct and return a multi piecewise affine expression
6383 * that is equal to the given piecewise multi affine expression.
6385 * If the resulting multi piecewise affine expression has
6386 * an explicit domain, then assign it the domain of the input.
6387 * In other cases, the domain is stored in the individual elements.
6389 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_from_pw_multi_aff(
6390 __isl_take isl_pw_multi_aff
*pma
)
6394 isl_multi_pw_aff
*mpa
;
6399 n
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
6400 space
= isl_pw_multi_aff_get_space(pma
);
6401 mpa
= isl_multi_pw_aff_alloc(space
);
6403 for (i
= 0; i
< n
; ++i
) {
6406 pa
= isl_pw_multi_aff_get_pw_aff(pma
, i
);
6407 mpa
= isl_multi_pw_aff_set_pw_aff(mpa
, i
, pa
);
6409 if (isl_multi_pw_aff_has_explicit_domain(mpa
)) {
6412 dom
= isl_pw_multi_aff_domain(isl_pw_multi_aff_copy(pma
));
6413 mpa
= isl_multi_pw_aff_intersect_domain(mpa
, dom
);
6416 isl_pw_multi_aff_free(pma
);
6420 /* Do "pa1" and "pa2" represent the same function?
6422 * We first check if they are obviously equal.
6423 * If not, we convert them to maps and check if those are equal.
6425 * If "pa1" or "pa2" contain any NaNs, then they are considered
6426 * not to be the same. A NaN is not equal to anything, not even
6429 isl_bool
isl_pw_aff_is_equal(__isl_keep isl_pw_aff
*pa1
,
6430 __isl_keep isl_pw_aff
*pa2
)
6434 isl_map
*map1
, *map2
;
6437 return isl_bool_error
;
6439 equal
= isl_pw_aff_plain_is_equal(pa1
, pa2
);
6440 if (equal
< 0 || equal
)
6442 has_nan
= either_involves_nan(pa1
, pa2
);
6444 return isl_bool_error
;
6446 return isl_bool_false
;
6448 map1
= isl_map_from_pw_aff_internal(isl_pw_aff_copy(pa1
));
6449 map2
= isl_map_from_pw_aff_internal(isl_pw_aff_copy(pa2
));
6450 equal
= isl_map_is_equal(map1
, map2
);
6457 /* Do "mpa1" and "mpa2" represent the same function?
6459 * Note that we cannot convert the entire isl_multi_pw_aff
6460 * to a map because the domains of the piecewise affine expressions
6461 * may not be the same.
6463 isl_bool
isl_multi_pw_aff_is_equal(__isl_keep isl_multi_pw_aff
*mpa1
,
6464 __isl_keep isl_multi_pw_aff
*mpa2
)
6467 isl_bool equal
, equal_params
;
6470 return isl_bool_error
;
6472 equal_params
= isl_space_has_equal_params(mpa1
->space
, mpa2
->space
);
6473 if (equal_params
< 0)
6474 return isl_bool_error
;
6475 if (!equal_params
) {
6476 if (!isl_space_has_named_params(mpa1
->space
))
6477 return isl_bool_false
;
6478 if (!isl_space_has_named_params(mpa2
->space
))
6479 return isl_bool_false
;
6480 mpa1
= isl_multi_pw_aff_copy(mpa1
);
6481 mpa2
= isl_multi_pw_aff_copy(mpa2
);
6482 mpa1
= isl_multi_pw_aff_align_params(mpa1
,
6483 isl_multi_pw_aff_get_space(mpa2
));
6484 mpa2
= isl_multi_pw_aff_align_params(mpa2
,
6485 isl_multi_pw_aff_get_space(mpa1
));
6486 equal
= isl_multi_pw_aff_is_equal(mpa1
, mpa2
);
6487 isl_multi_pw_aff_free(mpa1
);
6488 isl_multi_pw_aff_free(mpa2
);
6492 equal
= isl_space_is_equal(mpa1
->space
, mpa2
->space
);
6493 if (equal
< 0 || !equal
)
6496 for (i
= 0; i
< mpa1
->n
; ++i
) {
6497 equal
= isl_pw_aff_is_equal(mpa1
->u
.p
[i
], mpa2
->u
.p
[i
]);
6498 if (equal
< 0 || !equal
)
6502 return isl_bool_true
;
6505 /* Do "pma1" and "pma2" represent the same function?
6507 * First check if they are obviously equal.
6508 * If not, then convert them to maps and check if those are equal.
6510 * If "pa1" or "pa2" contain any NaNs, then they are considered
6511 * not to be the same. A NaN is not equal to anything, not even
6514 isl_bool
isl_pw_multi_aff_is_equal(__isl_keep isl_pw_multi_aff
*pma1
,
6515 __isl_keep isl_pw_multi_aff
*pma2
)
6519 isl_map
*map1
, *map2
;
6522 return isl_bool_error
;
6524 equal
= isl_pw_multi_aff_plain_is_equal(pma1
, pma2
);
6525 if (equal
< 0 || equal
)
6527 has_nan
= isl_pw_multi_aff_involves_nan(pma1
);
6528 if (has_nan
>= 0 && !has_nan
)
6529 has_nan
= isl_pw_multi_aff_involves_nan(pma2
);
6530 if (has_nan
< 0 || has_nan
)
6531 return isl_bool_not(has_nan
);
6533 map1
= isl_map_from_pw_multi_aff(isl_pw_multi_aff_copy(pma1
));
6534 map2
= isl_map_from_pw_multi_aff(isl_pw_multi_aff_copy(pma2
));
6535 equal
= isl_map_is_equal(map1
, map2
);
6542 /* Compute the pullback of "mpa" by the function represented by "ma".
6543 * In other words, plug in "ma" in "mpa".
6545 * The parameters of "mpa" and "ma" are assumed to have been aligned.
6547 * If "mpa" has an explicit domain, then it is this domain
6548 * that needs to undergo a pullback, i.e., a preimage.
6550 static __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_pullback_multi_aff_aligned(
6551 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_multi_aff
*ma
)
6554 isl_space
*space
= NULL
;
6556 mpa
= isl_multi_pw_aff_cow(mpa
);
6560 space
= isl_space_join(isl_multi_aff_get_space(ma
),
6561 isl_multi_pw_aff_get_space(mpa
));
6565 for (i
= 0; i
< mpa
->n
; ++i
) {
6566 mpa
->u
.p
[i
] = isl_pw_aff_pullback_multi_aff(mpa
->u
.p
[i
],
6567 isl_multi_aff_copy(ma
));
6571 if (isl_multi_pw_aff_has_explicit_domain(mpa
)) {
6572 mpa
->u
.dom
= isl_set_preimage_multi_aff(mpa
->u
.dom
,
6573 isl_multi_aff_copy(ma
));
6578 isl_multi_aff_free(ma
);
6579 isl_space_free(mpa
->space
);
6583 isl_space_free(space
);
6584 isl_multi_pw_aff_free(mpa
);
6585 isl_multi_aff_free(ma
);
6589 /* Compute the pullback of "mpa" by the function represented by "ma".
6590 * In other words, plug in "ma" in "mpa".
6592 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_pullback_multi_aff(
6593 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_multi_aff
*ma
)
6595 isl_bool equal_params
;
6599 equal_params
= isl_space_has_equal_params(mpa
->space
, ma
->space
);
6600 if (equal_params
< 0)
6603 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa
, ma
);
6604 mpa
= isl_multi_pw_aff_align_params(mpa
, isl_multi_aff_get_space(ma
));
6605 ma
= isl_multi_aff_align_params(ma
, isl_multi_pw_aff_get_space(mpa
));
6606 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa
, ma
);
6608 isl_multi_pw_aff_free(mpa
);
6609 isl_multi_aff_free(ma
);
6613 /* Compute the pullback of "mpa" by the function represented by "pma".
6614 * In other words, plug in "pma" in "mpa".
6616 * The parameters of "mpa" and "mpa" are assumed to have been aligned.
6618 * If "mpa" has an explicit domain, then it is this domain
6619 * that needs to undergo a pullback, i.e., a preimage.
6621 static __isl_give isl_multi_pw_aff
*
6622 isl_multi_pw_aff_pullback_pw_multi_aff_aligned(
6623 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_pw_multi_aff
*pma
)
6626 isl_space
*space
= NULL
;
6628 mpa
= isl_multi_pw_aff_cow(mpa
);
6632 space
= isl_space_join(isl_pw_multi_aff_get_space(pma
),
6633 isl_multi_pw_aff_get_space(mpa
));
6635 for (i
= 0; i
< mpa
->n
; ++i
) {
6636 mpa
->u
.p
[i
] = isl_pw_aff_pullback_pw_multi_aff_aligned(
6637 mpa
->u
.p
[i
], isl_pw_multi_aff_copy(pma
));
6641 if (isl_multi_pw_aff_has_explicit_domain(mpa
)) {
6642 mpa
->u
.dom
= isl_set_preimage_pw_multi_aff(mpa
->u
.dom
,
6643 isl_pw_multi_aff_copy(pma
));
6648 isl_pw_multi_aff_free(pma
);
6649 isl_space_free(mpa
->space
);
6653 isl_space_free(space
);
6654 isl_multi_pw_aff_free(mpa
);
6655 isl_pw_multi_aff_free(pma
);
6659 /* Compute the pullback of "mpa" by the function represented by "pma".
6660 * In other words, plug in "pma" in "mpa".
6662 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_pullback_pw_multi_aff(
6663 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_pw_multi_aff
*pma
)
6665 isl_bool equal_params
;
6669 equal_params
= isl_space_has_equal_params(mpa
->space
, pma
->dim
);
6670 if (equal_params
< 0)
6673 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa
, pma
);
6674 mpa
= isl_multi_pw_aff_align_params(mpa
,
6675 isl_pw_multi_aff_get_space(pma
));
6676 pma
= isl_pw_multi_aff_align_params(pma
,
6677 isl_multi_pw_aff_get_space(mpa
));
6678 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa
, pma
);
6680 isl_multi_pw_aff_free(mpa
);
6681 isl_pw_multi_aff_free(pma
);
6685 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
6686 * with the domain of "aff". The domain of the result is the same
6688 * "mpa" and "aff" are assumed to have been aligned.
6690 * We first extract the parametric constant from "aff", defined
6691 * over the correct domain.
6692 * Then we add the appropriate combinations of the members of "mpa".
6693 * Finally, we add the integer divisions through recursive calls.
6695 static __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_aff_aligned(
6696 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_aff
*aff
)
6704 n_in
= isl_aff_dim(aff
, isl_dim_in
);
6705 n_div
= isl_aff_dim(aff
, isl_dim_div
);
6707 space
= isl_space_domain(isl_multi_pw_aff_get_space(mpa
));
6708 tmp
= isl_aff_copy(aff
);
6709 tmp
= isl_aff_drop_dims(tmp
, isl_dim_div
, 0, n_div
);
6710 tmp
= isl_aff_drop_dims(tmp
, isl_dim_in
, 0, n_in
);
6711 tmp
= isl_aff_add_dims(tmp
, isl_dim_in
,
6712 isl_space_dim(space
, isl_dim_set
));
6713 tmp
= isl_aff_reset_domain_space(tmp
, space
);
6714 pa
= isl_pw_aff_from_aff(tmp
);
6716 for (i
= 0; i
< n_in
; ++i
) {
6719 if (!isl_aff_involves_dims(aff
, isl_dim_in
, i
, 1))
6721 v
= isl_aff_get_coefficient_val(aff
, isl_dim_in
, i
);
6722 pa_i
= isl_multi_pw_aff_get_pw_aff(mpa
, i
);
6723 pa_i
= isl_pw_aff_scale_val(pa_i
, v
);
6724 pa
= isl_pw_aff_add(pa
, pa_i
);
6727 for (i
= 0; i
< n_div
; ++i
) {
6731 if (!isl_aff_involves_dims(aff
, isl_dim_div
, i
, 1))
6733 div
= isl_aff_get_div(aff
, i
);
6734 pa_i
= isl_multi_pw_aff_apply_aff_aligned(
6735 isl_multi_pw_aff_copy(mpa
), div
);
6736 pa_i
= isl_pw_aff_floor(pa_i
);
6737 v
= isl_aff_get_coefficient_val(aff
, isl_dim_div
, i
);
6738 pa_i
= isl_pw_aff_scale_val(pa_i
, v
);
6739 pa
= isl_pw_aff_add(pa
, pa_i
);
6742 isl_multi_pw_aff_free(mpa
);
6748 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
6749 * with the domain of "aff". The domain of the result is the same
6752 __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_aff(
6753 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_aff
*aff
)
6755 isl_bool equal_params
;
6759 equal_params
= isl_space_has_equal_params(aff
->ls
->dim
, mpa
->space
);
6760 if (equal_params
< 0)
6763 return isl_multi_pw_aff_apply_aff_aligned(mpa
, aff
);
6765 aff
= isl_aff_align_params(aff
, isl_multi_pw_aff_get_space(mpa
));
6766 mpa
= isl_multi_pw_aff_align_params(mpa
, isl_aff_get_space(aff
));
6768 return isl_multi_pw_aff_apply_aff_aligned(mpa
, aff
);
6771 isl_multi_pw_aff_free(mpa
);
6775 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
6776 * with the domain of "pa". The domain of the result is the same
6778 * "mpa" and "pa" are assumed to have been aligned.
6780 * We consider each piece in turn. Note that the domains of the
6781 * pieces are assumed to be disjoint and they remain disjoint
6782 * after taking the preimage (over the same function).
6784 static __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_pw_aff_aligned(
6785 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_pw_aff
*pa
)
6794 space
= isl_space_join(isl_multi_pw_aff_get_space(mpa
),
6795 isl_pw_aff_get_space(pa
));
6796 res
= isl_pw_aff_empty(space
);
6798 for (i
= 0; i
< pa
->n
; ++i
) {
6802 pa_i
= isl_multi_pw_aff_apply_aff_aligned(
6803 isl_multi_pw_aff_copy(mpa
),
6804 isl_aff_copy(pa
->p
[i
].aff
));
6805 domain
= isl_set_copy(pa
->p
[i
].set
);
6806 domain
= isl_set_preimage_multi_pw_aff(domain
,
6807 isl_multi_pw_aff_copy(mpa
));
6808 pa_i
= isl_pw_aff_intersect_domain(pa_i
, domain
);
6809 res
= isl_pw_aff_add_disjoint(res
, pa_i
);
6812 isl_pw_aff_free(pa
);
6813 isl_multi_pw_aff_free(mpa
);
6816 isl_pw_aff_free(pa
);
6817 isl_multi_pw_aff_free(mpa
);
6821 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
6822 * with the domain of "pa". The domain of the result is the same
6825 __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_pw_aff(
6826 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_pw_aff
*pa
)
6828 isl_bool equal_params
;
6832 equal_params
= isl_space_has_equal_params(pa
->dim
, mpa
->space
);
6833 if (equal_params
< 0)
6836 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa
, pa
);
6838 pa
= isl_pw_aff_align_params(pa
, isl_multi_pw_aff_get_space(mpa
));
6839 mpa
= isl_multi_pw_aff_align_params(mpa
, isl_pw_aff_get_space(pa
));
6841 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa
, pa
);
6843 isl_pw_aff_free(pa
);
6844 isl_multi_pw_aff_free(mpa
);
6848 /* Compute the pullback of "pa" by the function represented by "mpa".
6849 * In other words, plug in "mpa" in "pa".
6850 * "pa" and "mpa" are assumed to have been aligned.
6852 * The pullback is computed by applying "pa" to "mpa".
6854 static __isl_give isl_pw_aff
*isl_pw_aff_pullback_multi_pw_aff_aligned(
6855 __isl_take isl_pw_aff
*pa
, __isl_take isl_multi_pw_aff
*mpa
)
6857 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa
, pa
);
6860 /* Compute the pullback of "pa" by the function represented by "mpa".
6861 * In other words, plug in "mpa" in "pa".
6863 * The pullback is computed by applying "pa" to "mpa".
6865 __isl_give isl_pw_aff
*isl_pw_aff_pullback_multi_pw_aff(
6866 __isl_take isl_pw_aff
*pa
, __isl_take isl_multi_pw_aff
*mpa
)
6868 return isl_multi_pw_aff_apply_pw_aff(mpa
, pa
);
6871 /* Compute the pullback of "mpa1" by the function represented by "mpa2".
6872 * In other words, plug in "mpa2" in "mpa1".
6874 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
6876 * We pullback each member of "mpa1" in turn.
6878 * If "mpa1" has an explicit domain, then it is this domain
6879 * that needs to undergo a pullback instead, i.e., a preimage.
6881 static __isl_give isl_multi_pw_aff
*
6882 isl_multi_pw_aff_pullback_multi_pw_aff_aligned(
6883 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
)
6886 isl_space
*space
= NULL
;
6888 mpa1
= isl_multi_pw_aff_cow(mpa1
);
6892 space
= isl_space_join(isl_multi_pw_aff_get_space(mpa2
),
6893 isl_multi_pw_aff_get_space(mpa1
));
6895 for (i
= 0; i
< mpa1
->n
; ++i
) {
6896 mpa1
->u
.p
[i
] = isl_pw_aff_pullback_multi_pw_aff_aligned(
6897 mpa1
->u
.p
[i
], isl_multi_pw_aff_copy(mpa2
));
6902 if (isl_multi_pw_aff_has_explicit_domain(mpa1
)) {
6903 mpa1
->u
.dom
= isl_set_preimage_multi_pw_aff(mpa1
->u
.dom
,
6904 isl_multi_pw_aff_copy(mpa2
));
6908 mpa1
= isl_multi_pw_aff_reset_space(mpa1
, space
);
6910 isl_multi_pw_aff_free(mpa2
);
6913 isl_space_free(space
);
6914 isl_multi_pw_aff_free(mpa1
);
6915 isl_multi_pw_aff_free(mpa2
);
6919 /* Compute the pullback of "mpa1" by the function represented by "mpa2".
6920 * In other words, plug in "mpa2" in "mpa1".
6922 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_pullback_multi_pw_aff(
6923 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
)
6925 return isl_multi_pw_aff_align_params_multi_multi_and(mpa1
, mpa2
,
6926 &isl_multi_pw_aff_pullback_multi_pw_aff_aligned
);
6929 /* Align the parameters of "mpa1" and "mpa2", check that the ranges
6930 * of "mpa1" and "mpa2" live in the same space, construct map space
6931 * between the domain spaces of "mpa1" and "mpa2" and call "order"
6932 * with this map space as extract argument.
6934 static __isl_give isl_map
*isl_multi_pw_aff_order_map(
6935 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
,
6936 __isl_give isl_map
*(*order
)(__isl_keep isl_multi_pw_aff
*mpa1
,
6937 __isl_keep isl_multi_pw_aff
*mpa2
, __isl_take isl_space
*space
))
6940 isl_space
*space1
, *space2
;
6943 mpa1
= isl_multi_pw_aff_align_params(mpa1
,
6944 isl_multi_pw_aff_get_space(mpa2
));
6945 mpa2
= isl_multi_pw_aff_align_params(mpa2
,
6946 isl_multi_pw_aff_get_space(mpa1
));
6949 match
= isl_space_tuple_is_equal(mpa1
->space
, isl_dim_out
,
6950 mpa2
->space
, isl_dim_out
);
6954 isl_die(isl_multi_pw_aff_get_ctx(mpa1
), isl_error_invalid
,
6955 "range spaces don't match", goto error
);
6956 space1
= isl_space_domain(isl_multi_pw_aff_get_space(mpa1
));
6957 space2
= isl_space_domain(isl_multi_pw_aff_get_space(mpa2
));
6958 space1
= isl_space_map_from_domain_and_range(space1
, space2
);
6960 res
= order(mpa1
, mpa2
, space1
);
6961 isl_multi_pw_aff_free(mpa1
);
6962 isl_multi_pw_aff_free(mpa2
);
6965 isl_multi_pw_aff_free(mpa1
);
6966 isl_multi_pw_aff_free(mpa2
);
6970 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
6971 * where the function values are equal. "space" is the space of the result.
6972 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
6974 * "mpa1" and "mpa2" are equal when each of the pairs of elements
6975 * in the sequences are equal.
6977 static __isl_give isl_map
*isl_multi_pw_aff_eq_map_on_space(
6978 __isl_keep isl_multi_pw_aff
*mpa1
, __isl_keep isl_multi_pw_aff
*mpa2
,
6979 __isl_take isl_space
*space
)
6984 res
= isl_map_universe(space
);
6986 n
= isl_multi_pw_aff_dim(mpa1
, isl_dim_out
);
6987 for (i
= 0; i
< n
; ++i
) {
6988 isl_pw_aff
*pa1
, *pa2
;
6991 pa1
= isl_multi_pw_aff_get_pw_aff(mpa1
, i
);
6992 pa2
= isl_multi_pw_aff_get_pw_aff(mpa2
, i
);
6993 map
= isl_pw_aff_eq_map(pa1
, pa2
);
6994 res
= isl_map_intersect(res
, map
);
7000 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7001 * where the function values are equal.
7003 __isl_give isl_map
*isl_multi_pw_aff_eq_map(__isl_take isl_multi_pw_aff
*mpa1
,
7004 __isl_take isl_multi_pw_aff
*mpa2
)
7006 return isl_multi_pw_aff_order_map(mpa1
, mpa2
,
7007 &isl_multi_pw_aff_eq_map_on_space
);
7010 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7011 * where the function values of "mpa1" is lexicographically satisfies "base"
7012 * compared to that of "mpa2". "space" is the space of the result.
7013 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7015 * "mpa1" lexicographically satisfies "base" compared to "mpa2"
7016 * if its i-th element satisfies "base" when compared to
7017 * the i-th element of "mpa2" while all previous elements are
7020 static __isl_give isl_map
*isl_multi_pw_aff_lex_map_on_space(
7021 __isl_keep isl_multi_pw_aff
*mpa1
, __isl_keep isl_multi_pw_aff
*mpa2
,
7022 __isl_give isl_map
*(*base
)(__isl_take isl_pw_aff
*pa1
,
7023 __isl_take isl_pw_aff
*pa2
),
7024 __isl_take isl_space
*space
)
7027 isl_map
*res
, *rest
;
7029 res
= isl_map_empty(isl_space_copy(space
));
7030 rest
= isl_map_universe(space
);
7032 n
= isl_multi_pw_aff_dim(mpa1
, isl_dim_out
);
7033 for (i
= 0; i
< n
; ++i
) {
7034 isl_pw_aff
*pa1
, *pa2
;
7037 pa1
= isl_multi_pw_aff_get_pw_aff(mpa1
, i
);
7038 pa2
= isl_multi_pw_aff_get_pw_aff(mpa2
, i
);
7039 map
= base(pa1
, pa2
);
7040 map
= isl_map_intersect(map
, isl_map_copy(rest
));
7041 res
= isl_map_union(res
, map
);
7046 pa1
= isl_multi_pw_aff_get_pw_aff(mpa1
, i
);
7047 pa2
= isl_multi_pw_aff_get_pw_aff(mpa2
, i
);
7048 map
= isl_pw_aff_eq_map(pa1
, pa2
);
7049 rest
= isl_map_intersect(rest
, map
);
7056 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7057 * where the function value of "mpa1" is lexicographically less than that
7058 * of "mpa2". "space" is the space of the result.
7059 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7061 * "mpa1" is less than "mpa2" if its i-th element is smaller
7062 * than the i-th element of "mpa2" while all previous elements are
7065 __isl_give isl_map
*isl_multi_pw_aff_lex_lt_map_on_space(
7066 __isl_keep isl_multi_pw_aff
*mpa1
, __isl_keep isl_multi_pw_aff
*mpa2
,
7067 __isl_take isl_space
*space
)
7069 return isl_multi_pw_aff_lex_map_on_space(mpa1
, mpa2
,
7070 &isl_pw_aff_lt_map
, space
);
7073 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7074 * where the function value of "mpa1" is lexicographically less than that
7077 __isl_give isl_map
*isl_multi_pw_aff_lex_lt_map(
7078 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
)
7080 return isl_multi_pw_aff_order_map(mpa1
, mpa2
,
7081 &isl_multi_pw_aff_lex_lt_map_on_space
);
7084 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7085 * where the function value of "mpa1" is lexicographically greater than that
7086 * of "mpa2". "space" is the space of the result.
7087 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7089 * "mpa1" is greater than "mpa2" if its i-th element is greater
7090 * than the i-th element of "mpa2" while all previous elements are
7093 __isl_give isl_map
*isl_multi_pw_aff_lex_gt_map_on_space(
7094 __isl_keep isl_multi_pw_aff
*mpa1
, __isl_keep isl_multi_pw_aff
*mpa2
,
7095 __isl_take isl_space
*space
)
7097 return isl_multi_pw_aff_lex_map_on_space(mpa1
, mpa2
,
7098 &isl_pw_aff_gt_map
, space
);
7101 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7102 * where the function value of "mpa1" is lexicographically greater than that
7105 __isl_give isl_map
*isl_multi_pw_aff_lex_gt_map(
7106 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
)
7108 return isl_multi_pw_aff_order_map(mpa1
, mpa2
,
7109 &isl_multi_pw_aff_lex_gt_map_on_space
);
7112 /* Compare two isl_affs.
7114 * Return -1 if "aff1" is "smaller" than "aff2", 1 if "aff1" is "greater"
7115 * than "aff2" and 0 if they are equal.
7117 * The order is fairly arbitrary. We do consider expressions that only involve
7118 * earlier dimensions as "smaller".
7120 int isl_aff_plain_cmp(__isl_keep isl_aff
*aff1
, __isl_keep isl_aff
*aff2
)
7133 cmp
= isl_local_space_cmp(aff1
->ls
, aff2
->ls
);
7137 last1
= isl_seq_last_non_zero(aff1
->v
->el
+ 1, aff1
->v
->size
- 1);
7138 last2
= isl_seq_last_non_zero(aff2
->v
->el
+ 1, aff1
->v
->size
- 1);
7140 return last1
- last2
;
7142 return isl_seq_cmp(aff1
->v
->el
, aff2
->v
->el
, aff1
->v
->size
);
7145 /* Compare two isl_pw_affs.
7147 * Return -1 if "pa1" is "smaller" than "pa2", 1 if "pa1" is "greater"
7148 * than "pa2" and 0 if they are equal.
7150 * The order is fairly arbitrary. We do consider expressions that only involve
7151 * earlier dimensions as "smaller".
7153 int isl_pw_aff_plain_cmp(__isl_keep isl_pw_aff
*pa1
,
7154 __isl_keep isl_pw_aff
*pa2
)
7167 cmp
= isl_space_cmp(pa1
->dim
, pa2
->dim
);
7171 if (pa1
->n
!= pa2
->n
)
7172 return pa1
->n
- pa2
->n
;
7174 for (i
= 0; i
< pa1
->n
; ++i
) {
7175 cmp
= isl_set_plain_cmp(pa1
->p
[i
].set
, pa2
->p
[i
].set
);
7178 cmp
= isl_aff_plain_cmp(pa1
->p
[i
].aff
, pa2
->p
[i
].aff
);
7186 /* Return a piecewise affine expression that is equal to "v" on "domain".
7188 __isl_give isl_pw_aff
*isl_pw_aff_val_on_domain(__isl_take isl_set
*domain
,
7189 __isl_take isl_val
*v
)
7192 isl_local_space
*ls
;
7195 space
= isl_set_get_space(domain
);
7196 ls
= isl_local_space_from_space(space
);
7197 aff
= isl_aff_val_on_domain(ls
, v
);
7199 return isl_pw_aff_alloc(domain
, aff
);
7202 /* Return a multi affine expression that is equal to "mv" on domain
7205 __isl_give isl_multi_aff
*isl_multi_aff_multi_val_on_space(
7206 __isl_take isl_space
*space
, __isl_take isl_multi_val
*mv
)
7210 isl_local_space
*ls
;
7216 n
= isl_multi_val_dim(mv
, isl_dim_set
);
7217 space2
= isl_multi_val_get_space(mv
);
7218 space2
= isl_space_align_params(space2
, isl_space_copy(space
));
7219 space
= isl_space_align_params(space
, isl_space_copy(space2
));
7220 space
= isl_space_map_from_domain_and_range(space
, space2
);
7221 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
7222 ls
= isl_local_space_from_space(isl_space_domain(space
));
7223 for (i
= 0; i
< n
; ++i
) {
7227 v
= isl_multi_val_get_val(mv
, i
);
7228 aff
= isl_aff_val_on_domain(isl_local_space_copy(ls
), v
);
7229 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
7231 isl_local_space_free(ls
);
7233 isl_multi_val_free(mv
);
7236 isl_space_free(space
);
7237 isl_multi_val_free(mv
);
7241 /* Return a piecewise multi-affine expression
7242 * that is equal to "mv" on "domain".
7244 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_multi_val_on_domain(
7245 __isl_take isl_set
*domain
, __isl_take isl_multi_val
*mv
)
7250 space
= isl_set_get_space(domain
);
7251 ma
= isl_multi_aff_multi_val_on_space(space
, mv
);
7253 return isl_pw_multi_aff_alloc(domain
, ma
);
7256 /* Internal data structure for isl_union_pw_multi_aff_multi_val_on_domain.
7257 * mv is the value that should be attained on each domain set
7258 * res collects the results
7260 struct isl_union_pw_multi_aff_multi_val_on_domain_data
{
7262 isl_union_pw_multi_aff
*res
;
7265 /* Create an isl_pw_multi_aff equal to data->mv on "domain"
7266 * and add it to data->res.
7268 static isl_stat
pw_multi_aff_multi_val_on_domain(__isl_take isl_set
*domain
,
7271 struct isl_union_pw_multi_aff_multi_val_on_domain_data
*data
= user
;
7272 isl_pw_multi_aff
*pma
;
7275 mv
= isl_multi_val_copy(data
->mv
);
7276 pma
= isl_pw_multi_aff_multi_val_on_domain(domain
, mv
);
7277 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma
);
7279 return data
->res
? isl_stat_ok
: isl_stat_error
;
7282 /* Return a union piecewise multi-affine expression
7283 * that is equal to "mv" on "domain".
7285 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_multi_val_on_domain(
7286 __isl_take isl_union_set
*domain
, __isl_take isl_multi_val
*mv
)
7288 struct isl_union_pw_multi_aff_multi_val_on_domain_data data
;
7291 space
= isl_union_set_get_space(domain
);
7292 data
.res
= isl_union_pw_multi_aff_empty(space
);
7294 if (isl_union_set_foreach_set(domain
,
7295 &pw_multi_aff_multi_val_on_domain
, &data
) < 0)
7296 data
.res
= isl_union_pw_multi_aff_free(data
.res
);
7297 isl_union_set_free(domain
);
7298 isl_multi_val_free(mv
);
7302 /* Compute the pullback of data->pma by the function represented by "pma2",
7303 * provided the spaces match, and add the results to data->res.
7305 static isl_stat
pullback_entry(__isl_take isl_pw_multi_aff
*pma2
, void *user
)
7307 struct isl_union_pw_multi_aff_bin_data
*data
= user
;
7309 if (!isl_space_tuple_is_equal(data
->pma
->dim
, isl_dim_in
,
7310 pma2
->dim
, isl_dim_out
)) {
7311 isl_pw_multi_aff_free(pma2
);
7315 pma2
= isl_pw_multi_aff_pullback_pw_multi_aff(
7316 isl_pw_multi_aff_copy(data
->pma
), pma2
);
7318 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma2
);
7320 return isl_stat_error
;
7325 /* Compute the pullback of "upma1" by the function represented by "upma2".
7327 __isl_give isl_union_pw_multi_aff
*
7328 isl_union_pw_multi_aff_pullback_union_pw_multi_aff(
7329 __isl_take isl_union_pw_multi_aff
*upma1
,
7330 __isl_take isl_union_pw_multi_aff
*upma2
)
7332 return bin_op(upma1
, upma2
, &pullback_entry
);
7335 /* Check that the domain space of "upa" matches "space".
7337 * This function is called from isl_multi_union_pw_aff_set_union_pw_aff and
7338 * can in principle never fail since the space "space" is that
7339 * of the isl_multi_union_pw_aff and is a set space such that
7340 * there is no domain space to match.
7342 * We check the parameters and double-check that "space" is
7343 * indeed that of a set.
7345 static isl_stat
isl_union_pw_aff_check_match_domain_space(
7346 __isl_keep isl_union_pw_aff
*upa
, __isl_keep isl_space
*space
)
7348 isl_space
*upa_space
;
7352 return isl_stat_error
;
7354 match
= isl_space_is_set(space
);
7356 return isl_stat_error
;
7358 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
7359 "expecting set space", return isl_stat_error
);
7361 upa_space
= isl_union_pw_aff_get_space(upa
);
7362 match
= isl_space_has_equal_params(space
, upa_space
);
7366 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
7367 "parameters don't match", goto error
);
7369 isl_space_free(upa_space
);
7372 isl_space_free(upa_space
);
7373 return isl_stat_error
;
7376 /* Do the parameters of "upa" match those of "space"?
7378 static isl_bool
isl_union_pw_aff_matching_params(
7379 __isl_keep isl_union_pw_aff
*upa
, __isl_keep isl_space
*space
)
7381 isl_space
*upa_space
;
7385 return isl_bool_error
;
7387 upa_space
= isl_union_pw_aff_get_space(upa
);
7389 match
= isl_space_has_equal_params(space
, upa_space
);
7391 isl_space_free(upa_space
);
7395 /* Internal data structure for isl_union_pw_aff_reset_domain_space.
7396 * space represents the new parameters.
7397 * res collects the results.
7399 struct isl_union_pw_aff_reset_params_data
{
7401 isl_union_pw_aff
*res
;
7404 /* Replace the parameters of "pa" by data->space and
7405 * add the result to data->res.
7407 static isl_stat
reset_params(__isl_take isl_pw_aff
*pa
, void *user
)
7409 struct isl_union_pw_aff_reset_params_data
*data
= user
;
7412 space
= isl_pw_aff_get_space(pa
);
7413 space
= isl_space_replace_params(space
, data
->space
);
7414 pa
= isl_pw_aff_reset_space(pa
, space
);
7415 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
7417 return data
->res
? isl_stat_ok
: isl_stat_error
;
7420 /* Replace the domain space of "upa" by "space".
7421 * Since a union expression does not have a (single) domain space,
7422 * "space" is necessarily a parameter space.
7424 * Since the order and the names of the parameters determine
7425 * the hash value, we need to create a new hash table.
7427 static __isl_give isl_union_pw_aff
*isl_union_pw_aff_reset_domain_space(
7428 __isl_take isl_union_pw_aff
*upa
, __isl_take isl_space
*space
)
7430 struct isl_union_pw_aff_reset_params_data data
= { space
};
7433 match
= isl_union_pw_aff_matching_params(upa
, space
);
7435 upa
= isl_union_pw_aff_free(upa
);
7437 isl_space_free(space
);
7441 data
.res
= isl_union_pw_aff_empty(isl_space_copy(space
));
7442 if (isl_union_pw_aff_foreach_pw_aff(upa
, &reset_params
, &data
) < 0)
7443 data
.res
= isl_union_pw_aff_free(data
.res
);
7445 isl_union_pw_aff_free(upa
);
7446 isl_space_free(space
);
7450 /* Return the floor of "pa".
7452 static __isl_give isl_pw_aff
*floor_entry(__isl_take isl_pw_aff
*pa
, void *user
)
7454 return isl_pw_aff_floor(pa
);
7457 /* Given f, return floor(f).
7459 __isl_give isl_union_pw_aff
*isl_union_pw_aff_floor(
7460 __isl_take isl_union_pw_aff
*upa
)
7462 return isl_union_pw_aff_transform_inplace(upa
, &floor_entry
, NULL
);
7467 * upa mod m = upa - m * floor(upa/m)
7469 * with m an integer value.
7471 __isl_give isl_union_pw_aff
*isl_union_pw_aff_mod_val(
7472 __isl_take isl_union_pw_aff
*upa
, __isl_take isl_val
*m
)
7474 isl_union_pw_aff
*res
;
7479 if (!isl_val_is_int(m
))
7480 isl_die(isl_val_get_ctx(m
), isl_error_invalid
,
7481 "expecting integer modulo", goto error
);
7482 if (!isl_val_is_pos(m
))
7483 isl_die(isl_val_get_ctx(m
), isl_error_invalid
,
7484 "expecting positive modulo", goto error
);
7486 res
= isl_union_pw_aff_copy(upa
);
7487 upa
= isl_union_pw_aff_scale_down_val(upa
, isl_val_copy(m
));
7488 upa
= isl_union_pw_aff_floor(upa
);
7489 upa
= isl_union_pw_aff_scale_val(upa
, m
);
7490 res
= isl_union_pw_aff_sub(res
, upa
);
7495 isl_union_pw_aff_free(upa
);
7499 /* Internal data structure for isl_union_pw_multi_aff_get_union_pw_aff.
7500 * pos is the output position that needs to be extracted.
7501 * res collects the results.
7503 struct isl_union_pw_multi_aff_get_union_pw_aff_data
{
7505 isl_union_pw_aff
*res
;
7508 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma"
7509 * (assuming it has such a dimension) and add it to data->res.
7511 static isl_stat
get_union_pw_aff(__isl_take isl_pw_multi_aff
*pma
, void *user
)
7513 struct isl_union_pw_multi_aff_get_union_pw_aff_data
*data
= user
;
7518 return isl_stat_error
;
7520 n_out
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
7521 if (data
->pos
>= n_out
) {
7522 isl_pw_multi_aff_free(pma
);
7526 pa
= isl_pw_multi_aff_get_pw_aff(pma
, data
->pos
);
7527 isl_pw_multi_aff_free(pma
);
7529 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
7531 return data
->res
? isl_stat_ok
: isl_stat_error
;
7534 /* Extract an isl_union_pw_aff corresponding to
7535 * output dimension "pos" of "upma".
7537 __isl_give isl_union_pw_aff
*isl_union_pw_multi_aff_get_union_pw_aff(
7538 __isl_keep isl_union_pw_multi_aff
*upma
, int pos
)
7540 struct isl_union_pw_multi_aff_get_union_pw_aff_data data
;
7547 isl_die(isl_union_pw_multi_aff_get_ctx(upma
), isl_error_invalid
,
7548 "cannot extract at negative position", return NULL
);
7550 space
= isl_union_pw_multi_aff_get_space(upma
);
7551 data
.res
= isl_union_pw_aff_empty(space
);
7553 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma
,
7554 &get_union_pw_aff
, &data
) < 0)
7555 data
.res
= isl_union_pw_aff_free(data
.res
);
7560 /* Return a union piecewise affine expression
7561 * that is equal to "aff" on "domain".
7563 __isl_give isl_union_pw_aff
*isl_union_pw_aff_aff_on_domain(
7564 __isl_take isl_union_set
*domain
, __isl_take isl_aff
*aff
)
7568 pa
= isl_pw_aff_from_aff(aff
);
7569 return isl_union_pw_aff_pw_aff_on_domain(domain
, pa
);
7572 /* Return a union piecewise affine expression
7573 * that is equal to the parameter identified by "id" on "domain".
7575 * Make sure the parameter appears in the space passed to
7576 * isl_aff_param_on_domain_space_id.
7578 __isl_give isl_union_pw_aff
*isl_union_pw_aff_param_on_domain_id(
7579 __isl_take isl_union_set
*domain
, __isl_take isl_id
*id
)
7584 space
= isl_union_set_get_space(domain
);
7585 space
= isl_space_add_param_id(space
, isl_id_copy(id
));
7586 aff
= isl_aff_param_on_domain_space_id(space
, id
);
7587 return isl_union_pw_aff_aff_on_domain(domain
, aff
);
7590 /* Internal data structure for isl_union_pw_aff_pw_aff_on_domain.
7591 * "pa" is the piecewise symbolic value that the resulting isl_union_pw_aff
7593 * "res" collects the results.
7595 struct isl_union_pw_aff_pw_aff_on_domain_data
{
7597 isl_union_pw_aff
*res
;
7600 /* Construct a piecewise affine expression that is equal to data->pa
7601 * on "domain" and add the result to data->res.
7603 static isl_stat
pw_aff_on_domain(__isl_take isl_set
*domain
, void *user
)
7605 struct isl_union_pw_aff_pw_aff_on_domain_data
*data
= user
;
7609 pa
= isl_pw_aff_copy(data
->pa
);
7610 dim
= isl_set_dim(domain
, isl_dim_set
);
7611 pa
= isl_pw_aff_from_range(pa
);
7612 pa
= isl_pw_aff_add_dims(pa
, isl_dim_in
, dim
);
7613 pa
= isl_pw_aff_reset_domain_space(pa
, isl_set_get_space(domain
));
7614 pa
= isl_pw_aff_intersect_domain(pa
, domain
);
7615 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
7617 return data
->res
? isl_stat_ok
: isl_stat_error
;
7620 /* Return a union piecewise affine expression
7621 * that is equal to "pa" on "domain", assuming "domain" and "pa"
7622 * have been aligned.
7624 * Construct an isl_pw_aff on each of the sets in "domain" and
7625 * collect the results.
7627 static __isl_give isl_union_pw_aff
*isl_union_pw_aff_pw_aff_on_domain_aligned(
7628 __isl_take isl_union_set
*domain
, __isl_take isl_pw_aff
*pa
)
7630 struct isl_union_pw_aff_pw_aff_on_domain_data data
;
7633 space
= isl_union_set_get_space(domain
);
7634 data
.res
= isl_union_pw_aff_empty(space
);
7636 if (isl_union_set_foreach_set(domain
, &pw_aff_on_domain
, &data
) < 0)
7637 data
.res
= isl_union_pw_aff_free(data
.res
);
7638 isl_union_set_free(domain
);
7639 isl_pw_aff_free(pa
);
7643 /* Return a union piecewise affine expression
7644 * that is equal to "pa" on "domain".
7646 * Check that "pa" is a parametric expression,
7647 * align the parameters if needed and call
7648 * isl_union_pw_aff_pw_aff_on_domain_aligned.
7650 __isl_give isl_union_pw_aff
*isl_union_pw_aff_pw_aff_on_domain(
7651 __isl_take isl_union_set
*domain
, __isl_take isl_pw_aff
*pa
)
7654 isl_bool equal_params
;
7655 isl_space
*domain_space
, *pa_space
;
7657 pa_space
= isl_pw_aff_peek_space(pa
);
7658 is_set
= isl_space_is_set(pa_space
);
7662 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
7663 "expecting parametric expression", goto error
);
7665 domain_space
= isl_union_set_get_space(domain
);
7666 pa_space
= isl_pw_aff_get_space(pa
);
7667 equal_params
= isl_space_has_equal_params(domain_space
, pa_space
);
7668 if (equal_params
>= 0 && !equal_params
) {
7671 space
= isl_space_align_params(domain_space
, pa_space
);
7672 pa
= isl_pw_aff_align_params(pa
, isl_space_copy(space
));
7673 domain
= isl_union_set_align_params(domain
, space
);
7675 isl_space_free(domain_space
);
7676 isl_space_free(pa_space
);
7679 if (equal_params
< 0)
7681 return isl_union_pw_aff_pw_aff_on_domain_aligned(domain
, pa
);
7683 isl_union_set_free(domain
);
7684 isl_pw_aff_free(pa
);
7688 /* Internal data structure for isl_union_pw_aff_val_on_domain.
7689 * "v" is the value that the resulting isl_union_pw_aff needs to attain.
7690 * "res" collects the results.
7692 struct isl_union_pw_aff_val_on_domain_data
{
7694 isl_union_pw_aff
*res
;
7697 /* Construct a piecewise affine expression that is equal to data->v
7698 * on "domain" and add the result to data->res.
7700 static isl_stat
pw_aff_val_on_domain(__isl_take isl_set
*domain
, void *user
)
7702 struct isl_union_pw_aff_val_on_domain_data
*data
= user
;
7706 v
= isl_val_copy(data
->v
);
7707 pa
= isl_pw_aff_val_on_domain(domain
, v
);
7708 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
7710 return data
->res
? isl_stat_ok
: isl_stat_error
;
7713 /* Return a union piecewise affine expression
7714 * that is equal to "v" on "domain".
7716 * Construct an isl_pw_aff on each of the sets in "domain" and
7717 * collect the results.
7719 __isl_give isl_union_pw_aff
*isl_union_pw_aff_val_on_domain(
7720 __isl_take isl_union_set
*domain
, __isl_take isl_val
*v
)
7722 struct isl_union_pw_aff_val_on_domain_data data
;
7725 space
= isl_union_set_get_space(domain
);
7726 data
.res
= isl_union_pw_aff_empty(space
);
7728 if (isl_union_set_foreach_set(domain
, &pw_aff_val_on_domain
, &data
) < 0)
7729 data
.res
= isl_union_pw_aff_free(data
.res
);
7730 isl_union_set_free(domain
);
7735 /* Construct a piecewise multi affine expression
7736 * that is equal to "pa" and add it to upma.
7738 static isl_stat
pw_multi_aff_from_pw_aff_entry(__isl_take isl_pw_aff
*pa
,
7741 isl_union_pw_multi_aff
**upma
= user
;
7742 isl_pw_multi_aff
*pma
;
7744 pma
= isl_pw_multi_aff_from_pw_aff(pa
);
7745 *upma
= isl_union_pw_multi_aff_add_pw_multi_aff(*upma
, pma
);
7747 return *upma
? isl_stat_ok
: isl_stat_error
;
7750 /* Construct and return a union piecewise multi affine expression
7751 * that is equal to the given union piecewise affine expression.
7753 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_union_pw_aff(
7754 __isl_take isl_union_pw_aff
*upa
)
7757 isl_union_pw_multi_aff
*upma
;
7762 space
= isl_union_pw_aff_get_space(upa
);
7763 upma
= isl_union_pw_multi_aff_empty(space
);
7765 if (isl_union_pw_aff_foreach_pw_aff(upa
,
7766 &pw_multi_aff_from_pw_aff_entry
, &upma
) < 0)
7767 upma
= isl_union_pw_multi_aff_free(upma
);
7769 isl_union_pw_aff_free(upa
);
7773 /* Compute the set of elements in the domain of "pa" where it is zero and
7774 * add this set to "uset".
7776 static isl_stat
zero_union_set(__isl_take isl_pw_aff
*pa
, void *user
)
7778 isl_union_set
**uset
= (isl_union_set
**)user
;
7780 *uset
= isl_union_set_add_set(*uset
, isl_pw_aff_zero_set(pa
));
7782 return *uset
? isl_stat_ok
: isl_stat_error
;
7785 /* Return a union set containing those elements in the domain
7786 * of "upa" where it is zero.
7788 __isl_give isl_union_set
*isl_union_pw_aff_zero_union_set(
7789 __isl_take isl_union_pw_aff
*upa
)
7791 isl_union_set
*zero
;
7793 zero
= isl_union_set_empty(isl_union_pw_aff_get_space(upa
));
7794 if (isl_union_pw_aff_foreach_pw_aff(upa
, &zero_union_set
, &zero
) < 0)
7795 zero
= isl_union_set_free(zero
);
7797 isl_union_pw_aff_free(upa
);
7801 /* Internal data structure for isl_union_pw_aff_pullback_union_pw_multi_aff.
7802 * upma is the function that is plugged in.
7803 * pa is the current part of the function in which upma is plugged in.
7804 * res collects the results.
7806 struct isl_union_pw_aff_pullback_upma_data
{
7807 isl_union_pw_multi_aff
*upma
;
7809 isl_union_pw_aff
*res
;
7812 /* Check if "pma" can be plugged into data->pa.
7813 * If so, perform the pullback and add the result to data->res.
7815 static isl_stat
pa_pb_pma(__isl_take isl_pw_multi_aff
*pma
, void *user
)
7817 struct isl_union_pw_aff_pullback_upma_data
*data
= user
;
7820 if (!isl_space_tuple_is_equal(data
->pa
->dim
, isl_dim_in
,
7821 pma
->dim
, isl_dim_out
)) {
7822 isl_pw_multi_aff_free(pma
);
7826 pa
= isl_pw_aff_copy(data
->pa
);
7827 pa
= isl_pw_aff_pullback_pw_multi_aff(pa
, pma
);
7829 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
7831 return data
->res
? isl_stat_ok
: isl_stat_error
;
7834 /* Check if any of the elements of data->upma can be plugged into pa,
7835 * add if so add the result to data->res.
7837 static isl_stat
upa_pb_upma(__isl_take isl_pw_aff
*pa
, void *user
)
7839 struct isl_union_pw_aff_pullback_upma_data
*data
= user
;
7843 r
= isl_union_pw_multi_aff_foreach_pw_multi_aff(data
->upma
,
7845 isl_pw_aff_free(pa
);
7850 /* Compute the pullback of "upa" by the function represented by "upma".
7851 * In other words, plug in "upma" in "upa". The result contains
7852 * expressions defined over the domain space of "upma".
7854 * Run over all pairs of elements in "upa" and "upma", perform
7855 * the pullback when appropriate and collect the results.
7856 * If the hash value were based on the domain space rather than
7857 * the function space, then we could run through all elements
7858 * of "upma" and directly pick out the corresponding element of "upa".
7860 __isl_give isl_union_pw_aff
*isl_union_pw_aff_pullback_union_pw_multi_aff(
7861 __isl_take isl_union_pw_aff
*upa
,
7862 __isl_take isl_union_pw_multi_aff
*upma
)
7864 struct isl_union_pw_aff_pullback_upma_data data
= { NULL
, NULL
};
7867 space
= isl_union_pw_multi_aff_get_space(upma
);
7868 upa
= isl_union_pw_aff_align_params(upa
, space
);
7869 space
= isl_union_pw_aff_get_space(upa
);
7870 upma
= isl_union_pw_multi_aff_align_params(upma
, space
);
7876 data
.res
= isl_union_pw_aff_alloc_same_size(upa
);
7877 if (isl_union_pw_aff_foreach_pw_aff(upa
, &upa_pb_upma
, &data
) < 0)
7878 data
.res
= isl_union_pw_aff_free(data
.res
);
7880 isl_union_pw_aff_free(upa
);
7881 isl_union_pw_multi_aff_free(upma
);
7884 isl_union_pw_aff_free(upa
);
7885 isl_union_pw_multi_aff_free(upma
);
7890 #define BASE union_pw_aff
7892 #define DOMBASE union_set
7894 #include <isl_multi_explicit_domain.c>
7895 #include <isl_multi_union_pw_aff_explicit_domain.c>
7896 #include <isl_multi_templ.c>
7897 #include <isl_multi_apply_set.c>
7898 #include <isl_multi_apply_union_set.c>
7899 #include <isl_multi_coalesce.c>
7900 #include <isl_multi_floor.c>
7901 #include <isl_multi_from_base_templ.c>
7902 #include <isl_multi_gist.c>
7903 #include <isl_multi_align_set.c>
7904 #include <isl_multi_align_union_set.c>
7905 #include <isl_multi_intersect.c>
7907 /* Does "mupa" have a non-trivial explicit domain?
7909 * The explicit domain, if present, is trivial if it represents
7910 * an (obviously) universe parameter set.
7912 isl_bool
isl_multi_union_pw_aff_has_non_trivial_domain(
7913 __isl_keep isl_multi_union_pw_aff
*mupa
)
7915 isl_bool is_params
, trivial
;
7919 return isl_bool_error
;
7920 if (!isl_multi_union_pw_aff_has_explicit_domain(mupa
))
7921 return isl_bool_false
;
7922 is_params
= isl_union_set_is_params(mupa
->u
.dom
);
7923 if (is_params
< 0 || !is_params
)
7924 return isl_bool_not(is_params
);
7925 set
= isl_set_from_union_set(isl_union_set_copy(mupa
->u
.dom
));
7926 trivial
= isl_set_plain_is_universe(set
);
7928 return isl_bool_not(trivial
);
7931 /* Construct a multiple union piecewise affine expression
7932 * in the given space with value zero in each of the output dimensions.
7934 * Since there is no canonical zero value for
7935 * a union piecewise affine expression, we can only construct
7936 * a zero-dimensional "zero" value.
7938 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_zero(
7939 __isl_take isl_space
*space
)
7946 params
= isl_space_is_params(space
);
7950 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
7951 "expecting proper set space", goto error
);
7952 if (!isl_space_is_set(space
))
7953 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
7954 "expecting set space", goto error
);
7955 if (isl_space_dim(space
, isl_dim_out
) != 0)
7956 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
7957 "expecting 0D space", goto error
);
7959 return isl_multi_union_pw_aff_alloc(space
);
7961 isl_space_free(space
);
7965 /* Compute the sum of "mupa1" and "mupa2" on the union of their domains,
7966 * with the actual sum on the shared domain and
7967 * the defined expression on the symmetric difference of the domains.
7969 * We simply iterate over the elements in both arguments and
7970 * call isl_union_pw_aff_union_add on each of them, if there is
7971 * at least one element.
7973 * Otherwise, the two expressions have an explicit domain and
7974 * the union of these explicit domains is computed.
7975 * This assumes that the explicit domains are either both in terms
7976 * of specific domains elements or both in terms of parameters.
7977 * However, if one of the expressions does not have any constraints
7978 * on its explicit domain, then this is allowed as well and the result
7979 * is the expression with no constraints on its explicit domain.
7981 static __isl_give isl_multi_union_pw_aff
*
7982 isl_multi_union_pw_aff_union_add_aligned(
7983 __isl_take isl_multi_union_pw_aff
*mupa1
,
7984 __isl_take isl_multi_union_pw_aff
*mupa2
)
7986 isl_bool has_domain
, is_params1
, is_params2
;
7988 if (isl_multi_union_pw_aff_check_equal_space(mupa1
, mupa2
) < 0)
7991 return isl_multi_union_pw_aff_bin_op(mupa1
, mupa2
,
7992 &isl_union_pw_aff_union_add
);
7993 if (isl_multi_union_pw_aff_check_has_explicit_domain(mupa1
) < 0 ||
7994 isl_multi_union_pw_aff_check_has_explicit_domain(mupa2
) < 0)
7997 has_domain
= isl_multi_union_pw_aff_has_non_trivial_domain(mupa1
);
8001 isl_multi_union_pw_aff_free(mupa2
);
8004 has_domain
= isl_multi_union_pw_aff_has_non_trivial_domain(mupa2
);
8008 isl_multi_union_pw_aff_free(mupa1
);
8012 is_params1
= isl_union_set_is_params(mupa1
->u
.dom
);
8013 is_params2
= isl_union_set_is_params(mupa2
->u
.dom
);
8014 if (is_params1
< 0 || is_params2
< 0)
8016 if (is_params1
!= is_params2
)
8017 isl_die(isl_multi_union_pw_aff_get_ctx(mupa1
),
8019 "cannot compute union of concrete domain and "
8020 "parameter constraints", goto error
);
8021 mupa1
= isl_multi_union_pw_aff_cow(mupa1
);
8024 mupa1
->u
.dom
= isl_union_set_union(mupa1
->u
.dom
,
8025 isl_union_set_copy(mupa2
->u
.dom
));
8028 isl_multi_union_pw_aff_free(mupa2
);
8031 isl_multi_union_pw_aff_free(mupa1
);
8032 isl_multi_union_pw_aff_free(mupa2
);
8036 /* Compute the sum of "mupa1" and "mupa2" on the union of their domains,
8037 * with the actual sum on the shared domain and
8038 * the defined expression on the symmetric difference of the domains.
8040 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_union_add(
8041 __isl_take isl_multi_union_pw_aff
*mupa1
,
8042 __isl_take isl_multi_union_pw_aff
*mupa2
)
8044 return isl_multi_union_pw_aff_align_params_multi_multi_and(mupa1
, mupa2
,
8045 &isl_multi_union_pw_aff_union_add_aligned
);
8048 /* Construct and return a multi union piecewise affine expression
8049 * that is equal to the given multi affine expression.
8051 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_from_multi_aff(
8052 __isl_take isl_multi_aff
*ma
)
8054 isl_multi_pw_aff
*mpa
;
8056 mpa
= isl_multi_pw_aff_from_multi_aff(ma
);
8057 return isl_multi_union_pw_aff_from_multi_pw_aff(mpa
);
8060 /* Construct and return a multi union piecewise affine expression
8061 * that is equal to the given multi piecewise affine expression.
8063 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_from_multi_pw_aff(
8064 __isl_take isl_multi_pw_aff
*mpa
)
8068 isl_multi_union_pw_aff
*mupa
;
8073 space
= isl_multi_pw_aff_get_space(mpa
);
8074 space
= isl_space_range(space
);
8075 mupa
= isl_multi_union_pw_aff_alloc(space
);
8077 n
= isl_multi_pw_aff_dim(mpa
, isl_dim_out
);
8078 for (i
= 0; i
< n
; ++i
) {
8080 isl_union_pw_aff
*upa
;
8082 pa
= isl_multi_pw_aff_get_pw_aff(mpa
, i
);
8083 upa
= isl_union_pw_aff_from_pw_aff(pa
);
8084 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
8087 isl_multi_pw_aff_free(mpa
);
8092 /* Extract the range space of "pma" and assign it to *space.
8093 * If *space has already been set (through a previous call to this function),
8094 * then check that the range space is the same.
8096 static isl_stat
extract_space(__isl_take isl_pw_multi_aff
*pma
, void *user
)
8098 isl_space
**space
= user
;
8099 isl_space
*pma_space
;
8102 pma_space
= isl_space_range(isl_pw_multi_aff_get_space(pma
));
8103 isl_pw_multi_aff_free(pma
);
8106 return isl_stat_error
;
8112 equal
= isl_space_is_equal(pma_space
, *space
);
8113 isl_space_free(pma_space
);
8116 return isl_stat_error
;
8118 isl_die(isl_space_get_ctx(*space
), isl_error_invalid
,
8119 "range spaces not the same", return isl_stat_error
);
8123 /* Construct and return a multi union piecewise affine expression
8124 * that is equal to the given union piecewise multi affine expression.
8126 * In order to be able to perform the conversion, the input
8127 * needs to be non-empty and may only involve a single range space.
8129 * If the resulting multi union piecewise affine expression has
8130 * an explicit domain, then assign it the domain of the input.
8131 * In other cases, the domain is stored in the individual elements.
8133 __isl_give isl_multi_union_pw_aff
*
8134 isl_multi_union_pw_aff_from_union_pw_multi_aff(
8135 __isl_take isl_union_pw_multi_aff
*upma
)
8137 isl_space
*space
= NULL
;
8138 isl_multi_union_pw_aff
*mupa
;
8143 if (isl_union_pw_multi_aff_n_pw_multi_aff(upma
) == 0)
8144 isl_die(isl_union_pw_multi_aff_get_ctx(upma
), isl_error_invalid
,
8145 "cannot extract range space from empty input",
8147 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma
, &extract_space
,
8154 n
= isl_space_dim(space
, isl_dim_set
);
8155 mupa
= isl_multi_union_pw_aff_alloc(space
);
8157 for (i
= 0; i
< n
; ++i
) {
8158 isl_union_pw_aff
*upa
;
8160 upa
= isl_union_pw_multi_aff_get_union_pw_aff(upma
, i
);
8161 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
8163 if (isl_multi_union_pw_aff_has_explicit_domain(mupa
)) {
8165 isl_union_pw_multi_aff
*copy
;
8167 copy
= isl_union_pw_multi_aff_copy(upma
);
8168 dom
= isl_union_pw_multi_aff_domain(copy
);
8169 mupa
= isl_multi_union_pw_aff_intersect_domain(mupa
, dom
);
8172 isl_union_pw_multi_aff_free(upma
);
8175 isl_space_free(space
);
8176 isl_union_pw_multi_aff_free(upma
);
8180 /* Try and create an isl_multi_union_pw_aff that is equivalent
8181 * to the given isl_union_map.
8182 * The isl_union_map is required to be single-valued in each space.
8183 * Moreover, it cannot be empty and all range spaces need to be the same.
8184 * Otherwise, an error is produced.
8186 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_from_union_map(
8187 __isl_take isl_union_map
*umap
)
8189 isl_union_pw_multi_aff
*upma
;
8191 upma
= isl_union_pw_multi_aff_from_union_map(umap
);
8192 return isl_multi_union_pw_aff_from_union_pw_multi_aff(upma
);
8195 /* Return a multiple union piecewise affine expression
8196 * that is equal to "mv" on "domain", assuming "domain" and "mv"
8197 * have been aligned.
8199 * If the resulting multi union piecewise affine expression has
8200 * an explicit domain, then assign it the input domain.
8201 * In other cases, the domain is stored in the individual elements.
8203 static __isl_give isl_multi_union_pw_aff
*
8204 isl_multi_union_pw_aff_multi_val_on_domain_aligned(
8205 __isl_take isl_union_set
*domain
, __isl_take isl_multi_val
*mv
)
8209 isl_multi_union_pw_aff
*mupa
;
8214 n
= isl_multi_val_dim(mv
, isl_dim_set
);
8215 space
= isl_multi_val_get_space(mv
);
8216 mupa
= isl_multi_union_pw_aff_alloc(space
);
8217 for (i
= 0; i
< n
; ++i
) {
8219 isl_union_pw_aff
*upa
;
8221 v
= isl_multi_val_get_val(mv
, i
);
8222 upa
= isl_union_pw_aff_val_on_domain(isl_union_set_copy(domain
),
8224 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
8226 if (isl_multi_union_pw_aff_has_explicit_domain(mupa
))
8227 mupa
= isl_multi_union_pw_aff_intersect_domain(mupa
,
8228 isl_union_set_copy(domain
));
8230 isl_union_set_free(domain
);
8231 isl_multi_val_free(mv
);
8234 isl_union_set_free(domain
);
8235 isl_multi_val_free(mv
);
8239 /* Return a multiple union piecewise affine expression
8240 * that is equal to "mv" on "domain".
8242 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_multi_val_on_domain(
8243 __isl_take isl_union_set
*domain
, __isl_take isl_multi_val
*mv
)
8245 isl_bool equal_params
;
8249 equal_params
= isl_space_has_equal_params(domain
->dim
, mv
->space
);
8250 if (equal_params
< 0)
8253 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(
8255 domain
= isl_union_set_align_params(domain
,
8256 isl_multi_val_get_space(mv
));
8257 mv
= isl_multi_val_align_params(mv
, isl_union_set_get_space(domain
));
8258 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(domain
, mv
);
8260 isl_union_set_free(domain
);
8261 isl_multi_val_free(mv
);
8265 /* Return a multiple union piecewise affine expression
8266 * that is equal to "ma" on "domain".
8268 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_multi_aff_on_domain(
8269 __isl_take isl_union_set
*domain
, __isl_take isl_multi_aff
*ma
)
8271 isl_pw_multi_aff
*pma
;
8273 pma
= isl_pw_multi_aff_from_multi_aff(ma
);
8274 return isl_multi_union_pw_aff_pw_multi_aff_on_domain(domain
, pma
);
8277 /* Return a multiple union piecewise affine expression
8278 * that is equal to "pma" on "domain", assuming "domain" and "pma"
8279 * have been aligned.
8281 * If the resulting multi union piecewise affine expression has
8282 * an explicit domain, then assign it the input domain.
8283 * In other cases, the domain is stored in the individual elements.
8285 static __isl_give isl_multi_union_pw_aff
*
8286 isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(
8287 __isl_take isl_union_set
*domain
, __isl_take isl_pw_multi_aff
*pma
)
8291 isl_multi_union_pw_aff
*mupa
;
8293 if (!domain
|| !pma
)
8296 n
= isl_pw_multi_aff_dim(pma
, isl_dim_set
);
8297 space
= isl_pw_multi_aff_get_space(pma
);
8298 mupa
= isl_multi_union_pw_aff_alloc(space
);
8299 for (i
= 0; i
< n
; ++i
) {
8301 isl_union_pw_aff
*upa
;
8303 pa
= isl_pw_multi_aff_get_pw_aff(pma
, i
);
8304 upa
= isl_union_pw_aff_pw_aff_on_domain(
8305 isl_union_set_copy(domain
), pa
);
8306 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
8308 if (isl_multi_union_pw_aff_has_explicit_domain(mupa
))
8309 mupa
= isl_multi_union_pw_aff_intersect_domain(mupa
,
8310 isl_union_set_copy(domain
));
8312 isl_union_set_free(domain
);
8313 isl_pw_multi_aff_free(pma
);
8316 isl_union_set_free(domain
);
8317 isl_pw_multi_aff_free(pma
);
8321 /* Return a multiple union piecewise affine expression
8322 * that is equal to "pma" on "domain".
8324 __isl_give isl_multi_union_pw_aff
*
8325 isl_multi_union_pw_aff_pw_multi_aff_on_domain(__isl_take isl_union_set
*domain
,
8326 __isl_take isl_pw_multi_aff
*pma
)
8328 isl_bool equal_params
;
8331 space
= isl_pw_multi_aff_peek_space(pma
);
8332 equal_params
= isl_union_set_space_has_equal_params(domain
, space
);
8333 if (equal_params
< 0)
8336 return isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(
8338 domain
= isl_union_set_align_params(domain
,
8339 isl_pw_multi_aff_get_space(pma
));
8340 pma
= isl_pw_multi_aff_align_params(pma
,
8341 isl_union_set_get_space(domain
));
8342 return isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(domain
,
8345 isl_union_set_free(domain
);
8346 isl_pw_multi_aff_free(pma
);
8350 /* Return a union set containing those elements in the domains
8351 * of the elements of "mupa" where they are all zero.
8353 * If there are no elements, then simply return the entire domain.
8355 __isl_give isl_union_set
*isl_multi_union_pw_aff_zero_union_set(
8356 __isl_take isl_multi_union_pw_aff
*mupa
)
8359 isl_union_pw_aff
*upa
;
8360 isl_union_set
*zero
;
8365 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8367 return isl_multi_union_pw_aff_domain(mupa
);
8369 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, 0);
8370 zero
= isl_union_pw_aff_zero_union_set(upa
);
8372 for (i
= 1; i
< n
; ++i
) {
8373 isl_union_set
*zero_i
;
8375 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
8376 zero_i
= isl_union_pw_aff_zero_union_set(upa
);
8378 zero
= isl_union_set_intersect(zero
, zero_i
);
8381 isl_multi_union_pw_aff_free(mupa
);
8385 /* Construct a union map mapping the shared domain
8386 * of the union piecewise affine expressions to the range of "mupa"
8387 * in the special case of a 0D multi union piecewise affine expression.
8389 * Construct a map between the explicit domain of "mupa" and
8391 * Note that this assumes that the domain consists of explicit elements.
8393 static __isl_give isl_union_map
*isl_union_map_from_multi_union_pw_aff_0D(
8394 __isl_take isl_multi_union_pw_aff
*mupa
)
8398 isl_union_set
*dom
, *ran
;
8400 space
= isl_multi_union_pw_aff_get_space(mupa
);
8401 dom
= isl_multi_union_pw_aff_domain(mupa
);
8402 ran
= isl_union_set_from_set(isl_set_universe(space
));
8404 is_params
= isl_union_set_is_params(dom
);
8406 dom
= isl_union_set_free(dom
);
8408 isl_die(isl_union_set_get_ctx(dom
), isl_error_invalid
,
8409 "cannot create union map from expression without "
8410 "explicit domain elements",
8411 dom
= isl_union_set_free(dom
));
8413 return isl_union_map_from_domain_and_range(dom
, ran
);
8416 /* Construct a union map mapping the shared domain
8417 * of the union piecewise affine expressions to the range of "mupa"
8418 * with each dimension in the range equated to the
8419 * corresponding union piecewise affine expression.
8421 * If the input is zero-dimensional, then construct a mapping
8422 * from its explicit domain.
8424 __isl_give isl_union_map
*isl_union_map_from_multi_union_pw_aff(
8425 __isl_take isl_multi_union_pw_aff
*mupa
)
8429 isl_union_map
*umap
;
8430 isl_union_pw_aff
*upa
;
8435 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8437 return isl_union_map_from_multi_union_pw_aff_0D(mupa
);
8439 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, 0);
8440 umap
= isl_union_map_from_union_pw_aff(upa
);
8442 for (i
= 1; i
< n
; ++i
) {
8443 isl_union_map
*umap_i
;
8445 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
8446 umap_i
= isl_union_map_from_union_pw_aff(upa
);
8447 umap
= isl_union_map_flat_range_product(umap
, umap_i
);
8450 space
= isl_multi_union_pw_aff_get_space(mupa
);
8451 umap
= isl_union_map_reset_range_space(umap
, space
);
8453 isl_multi_union_pw_aff_free(mupa
);
8457 /* Internal data structure for isl_union_pw_multi_aff_reset_range_space.
8458 * "range" is the space from which to set the range space.
8459 * "res" collects the results.
8461 struct isl_union_pw_multi_aff_reset_range_space_data
{
8463 isl_union_pw_multi_aff
*res
;
8466 /* Replace the range space of "pma" by the range space of data->range and
8467 * add the result to data->res.
8469 static isl_stat
reset_range_space(__isl_take isl_pw_multi_aff
*pma
, void *user
)
8471 struct isl_union_pw_multi_aff_reset_range_space_data
*data
= user
;
8474 space
= isl_pw_multi_aff_get_space(pma
);
8475 space
= isl_space_domain(space
);
8476 space
= isl_space_extend_domain_with_range(space
,
8477 isl_space_copy(data
->range
));
8478 pma
= isl_pw_multi_aff_reset_space(pma
, space
);
8479 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma
);
8481 return data
->res
? isl_stat_ok
: isl_stat_error
;
8484 /* Replace the range space of all the piecewise affine expressions in "upma" by
8485 * the range space of "space".
8487 * This assumes that all these expressions have the same output dimension.
8489 * Since the spaces of the expressions change, so do their hash values.
8490 * We therefore need to create a new isl_union_pw_multi_aff.
8491 * Note that the hash value is currently computed based on the entire
8492 * space even though there can only be a single expression with a given
8495 static __isl_give isl_union_pw_multi_aff
*
8496 isl_union_pw_multi_aff_reset_range_space(
8497 __isl_take isl_union_pw_multi_aff
*upma
, __isl_take isl_space
*space
)
8499 struct isl_union_pw_multi_aff_reset_range_space_data data
= { space
};
8500 isl_space
*space_upma
;
8502 space_upma
= isl_union_pw_multi_aff_get_space(upma
);
8503 data
.res
= isl_union_pw_multi_aff_empty(space_upma
);
8504 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma
,
8505 &reset_range_space
, &data
) < 0)
8506 data
.res
= isl_union_pw_multi_aff_free(data
.res
);
8508 isl_space_free(space
);
8509 isl_union_pw_multi_aff_free(upma
);
8513 /* Construct and return a union piecewise multi affine expression
8514 * that is equal to the given multi union piecewise affine expression,
8515 * in the special case of a 0D multi union piecewise affine expression.
8517 * Construct a union piecewise multi affine expression
8518 * on top of the explicit domain of the input.
8520 __isl_give isl_union_pw_multi_aff
*
8521 isl_union_pw_multi_aff_from_multi_union_pw_aff_0D(
8522 __isl_take isl_multi_union_pw_aff
*mupa
)
8526 isl_union_set
*domain
;
8528 space
= isl_multi_union_pw_aff_get_space(mupa
);
8529 mv
= isl_multi_val_zero(space
);
8530 domain
= isl_multi_union_pw_aff_domain(mupa
);
8531 return isl_union_pw_multi_aff_multi_val_on_domain(domain
, mv
);
8534 /* Construct and return a union piecewise multi affine expression
8535 * that is equal to the given multi union piecewise affine expression.
8537 * If the input is zero-dimensional, then
8538 * construct a union piecewise multi affine expression
8539 * on top of the explicit domain of the input.
8541 __isl_give isl_union_pw_multi_aff
*
8542 isl_union_pw_multi_aff_from_multi_union_pw_aff(
8543 __isl_take isl_multi_union_pw_aff
*mupa
)
8547 isl_union_pw_multi_aff
*upma
;
8548 isl_union_pw_aff
*upa
;
8553 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8555 return isl_union_pw_multi_aff_from_multi_union_pw_aff_0D(mupa
);
8557 space
= isl_multi_union_pw_aff_get_space(mupa
);
8558 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, 0);
8559 upma
= isl_union_pw_multi_aff_from_union_pw_aff(upa
);
8561 for (i
= 1; i
< n
; ++i
) {
8562 isl_union_pw_multi_aff
*upma_i
;
8564 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
8565 upma_i
= isl_union_pw_multi_aff_from_union_pw_aff(upa
);
8566 upma
= isl_union_pw_multi_aff_flat_range_product(upma
, upma_i
);
8569 upma
= isl_union_pw_multi_aff_reset_range_space(upma
, space
);
8571 isl_multi_union_pw_aff_free(mupa
);
8575 /* Intersect the range of "mupa" with "range",
8576 * in the special case where "mupa" is 0D.
8578 * Intersect the domain of "mupa" with the constraints on the parameters
8581 static __isl_give isl_multi_union_pw_aff
*mupa_intersect_range_0D(
8582 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_set
*range
)
8584 range
= isl_set_params(range
);
8585 mupa
= isl_multi_union_pw_aff_intersect_params(mupa
, range
);
8589 /* Intersect the range of "mupa" with "range".
8590 * That is, keep only those domain elements that have a function value
8593 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_intersect_range(
8594 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_set
*range
)
8596 isl_union_pw_multi_aff
*upma
;
8597 isl_union_set
*domain
;
8602 if (!mupa
|| !range
)
8605 space
= isl_set_get_space(range
);
8606 match
= isl_space_tuple_is_equal(mupa
->space
, isl_dim_set
,
8607 space
, isl_dim_set
);
8608 isl_space_free(space
);
8612 isl_die(isl_multi_union_pw_aff_get_ctx(mupa
), isl_error_invalid
,
8613 "space don't match", goto error
);
8614 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8616 return mupa_intersect_range_0D(mupa
, range
);
8618 upma
= isl_union_pw_multi_aff_from_multi_union_pw_aff(
8619 isl_multi_union_pw_aff_copy(mupa
));
8620 domain
= isl_union_set_from_set(range
);
8621 domain
= isl_union_set_preimage_union_pw_multi_aff(domain
, upma
);
8622 mupa
= isl_multi_union_pw_aff_intersect_domain(mupa
, domain
);
8626 isl_multi_union_pw_aff_free(mupa
);
8627 isl_set_free(range
);
8631 /* Return the shared domain of the elements of "mupa",
8632 * in the special case where "mupa" is zero-dimensional.
8634 * Return the explicit domain of "mupa".
8635 * Note that this domain may be a parameter set, either
8636 * because "mupa" is meant to live in a set space or
8637 * because no explicit domain has been set.
8639 __isl_give isl_union_set
*isl_multi_union_pw_aff_domain_0D(
8640 __isl_take isl_multi_union_pw_aff
*mupa
)
8644 dom
= isl_multi_union_pw_aff_get_explicit_domain(mupa
);
8645 isl_multi_union_pw_aff_free(mupa
);
8650 /* Return the shared domain of the elements of "mupa".
8652 * If "mupa" is zero-dimensional, then return its explicit domain.
8654 __isl_give isl_union_set
*isl_multi_union_pw_aff_domain(
8655 __isl_take isl_multi_union_pw_aff
*mupa
)
8658 isl_union_pw_aff
*upa
;
8664 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8666 return isl_multi_union_pw_aff_domain_0D(mupa
);
8668 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, 0);
8669 dom
= isl_union_pw_aff_domain(upa
);
8670 for (i
= 1; i
< n
; ++i
) {
8671 isl_union_set
*dom_i
;
8673 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
8674 dom_i
= isl_union_pw_aff_domain(upa
);
8675 dom
= isl_union_set_intersect(dom
, dom_i
);
8678 isl_multi_union_pw_aff_free(mupa
);
8682 /* Apply "aff" to "mupa". The space of "mupa" is equal to the domain of "aff".
8683 * In particular, the spaces have been aligned.
8684 * The result is defined over the shared domain of the elements of "mupa"
8686 * We first extract the parametric constant part of "aff" and
8687 * define that over the shared domain.
8688 * Then we iterate over all input dimensions of "aff" and add the corresponding
8689 * multiples of the elements of "mupa".
8690 * Finally, we consider the integer divisions, calling the function
8691 * recursively to obtain an isl_union_pw_aff corresponding to the
8692 * integer division argument.
8694 static __isl_give isl_union_pw_aff
*multi_union_pw_aff_apply_aff(
8695 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_aff
*aff
)
8698 isl_union_pw_aff
*upa
;
8699 isl_union_set
*uset
;
8703 n_in
= isl_aff_dim(aff
, isl_dim_in
);
8704 n_div
= isl_aff_dim(aff
, isl_dim_div
);
8706 uset
= isl_multi_union_pw_aff_domain(isl_multi_union_pw_aff_copy(mupa
));
8707 cst
= isl_aff_copy(aff
);
8708 cst
= isl_aff_drop_dims(cst
, isl_dim_div
, 0, n_div
);
8709 cst
= isl_aff_drop_dims(cst
, isl_dim_in
, 0, n_in
);
8710 cst
= isl_aff_project_domain_on_params(cst
);
8711 upa
= isl_union_pw_aff_aff_on_domain(uset
, cst
);
8713 for (i
= 0; i
< n_in
; ++i
) {
8714 isl_union_pw_aff
*upa_i
;
8716 if (!isl_aff_involves_dims(aff
, isl_dim_in
, i
, 1))
8718 v
= isl_aff_get_coefficient_val(aff
, isl_dim_in
, i
);
8719 upa_i
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
8720 upa_i
= isl_union_pw_aff_scale_val(upa_i
, v
);
8721 upa
= isl_union_pw_aff_add(upa
, upa_i
);
8724 for (i
= 0; i
< n_div
; ++i
) {
8726 isl_union_pw_aff
*upa_i
;
8728 if (!isl_aff_involves_dims(aff
, isl_dim_div
, i
, 1))
8730 div
= isl_aff_get_div(aff
, i
);
8731 upa_i
= multi_union_pw_aff_apply_aff(
8732 isl_multi_union_pw_aff_copy(mupa
), div
);
8733 upa_i
= isl_union_pw_aff_floor(upa_i
);
8734 v
= isl_aff_get_coefficient_val(aff
, isl_dim_div
, i
);
8735 upa_i
= isl_union_pw_aff_scale_val(upa_i
, v
);
8736 upa
= isl_union_pw_aff_add(upa
, upa_i
);
8739 isl_multi_union_pw_aff_free(mupa
);
8745 /* Apply "aff" to "mupa". The space of "mupa" needs to be compatible
8746 * with the domain of "aff".
8747 * Furthermore, the dimension of this space needs to be greater than zero.
8748 * The result is defined over the shared domain of the elements of "mupa"
8750 * We perform these checks and then hand over control to
8751 * multi_union_pw_aff_apply_aff.
8753 __isl_give isl_union_pw_aff
*isl_multi_union_pw_aff_apply_aff(
8754 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_aff
*aff
)
8756 isl_space
*space1
, *space2
;
8759 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
8760 isl_aff_get_space(aff
));
8761 aff
= isl_aff_align_params(aff
, isl_multi_union_pw_aff_get_space(mupa
));
8765 space1
= isl_multi_union_pw_aff_get_space(mupa
);
8766 space2
= isl_aff_get_domain_space(aff
);
8767 equal
= isl_space_is_equal(space1
, space2
);
8768 isl_space_free(space1
);
8769 isl_space_free(space2
);
8773 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
8774 "spaces don't match", goto error
);
8775 if (isl_aff_dim(aff
, isl_dim_in
) == 0)
8776 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
8777 "cannot determine domains", goto error
);
8779 return multi_union_pw_aff_apply_aff(mupa
, aff
);
8781 isl_multi_union_pw_aff_free(mupa
);
8786 /* Apply "ma" to "mupa", in the special case where "mupa" is 0D.
8787 * The space of "mupa" is known to be compatible with the domain of "ma".
8789 * Construct an isl_multi_union_pw_aff that is equal to "ma"
8790 * on the domain of "mupa".
8792 static __isl_give isl_multi_union_pw_aff
*mupa_apply_multi_aff_0D(
8793 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_multi_aff
*ma
)
8797 dom
= isl_multi_union_pw_aff_domain(mupa
);
8798 ma
= isl_multi_aff_project_domain_on_params(ma
);
8800 return isl_multi_union_pw_aff_multi_aff_on_domain(dom
, ma
);
8803 /* Apply "ma" to "mupa". The space of "mupa" needs to be compatible
8804 * with the domain of "ma".
8805 * The result is defined over the shared domain of the elements of "mupa"
8807 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_apply_multi_aff(
8808 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_multi_aff
*ma
)
8810 isl_space
*space1
, *space2
;
8811 isl_multi_union_pw_aff
*res
;
8815 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
8816 isl_multi_aff_get_space(ma
));
8817 ma
= isl_multi_aff_align_params(ma
,
8818 isl_multi_union_pw_aff_get_space(mupa
));
8822 space1
= isl_multi_union_pw_aff_get_space(mupa
);
8823 space2
= isl_multi_aff_get_domain_space(ma
);
8824 equal
= isl_space_is_equal(space1
, space2
);
8825 isl_space_free(space1
);
8826 isl_space_free(space2
);
8830 isl_die(isl_multi_aff_get_ctx(ma
), isl_error_invalid
,
8831 "spaces don't match", goto error
);
8832 n_out
= isl_multi_aff_dim(ma
, isl_dim_out
);
8833 if (isl_multi_aff_dim(ma
, isl_dim_in
) == 0)
8834 return mupa_apply_multi_aff_0D(mupa
, ma
);
8836 space1
= isl_space_range(isl_multi_aff_get_space(ma
));
8837 res
= isl_multi_union_pw_aff_alloc(space1
);
8839 for (i
= 0; i
< n_out
; ++i
) {
8841 isl_union_pw_aff
*upa
;
8843 aff
= isl_multi_aff_get_aff(ma
, i
);
8844 upa
= multi_union_pw_aff_apply_aff(
8845 isl_multi_union_pw_aff_copy(mupa
), aff
);
8846 res
= isl_multi_union_pw_aff_set_union_pw_aff(res
, i
, upa
);
8849 isl_multi_aff_free(ma
);
8850 isl_multi_union_pw_aff_free(mupa
);
8853 isl_multi_union_pw_aff_free(mupa
);
8854 isl_multi_aff_free(ma
);
8858 /* Apply "pa" to "mupa", in the special case where "mupa" is 0D.
8859 * The space of "mupa" is known to be compatible with the domain of "pa".
8861 * Construct an isl_multi_union_pw_aff that is equal to "pa"
8862 * on the domain of "mupa".
8864 static __isl_give isl_union_pw_aff
*isl_multi_union_pw_aff_apply_pw_aff_0D(
8865 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_pw_aff
*pa
)
8869 dom
= isl_multi_union_pw_aff_domain(mupa
);
8870 pa
= isl_pw_aff_project_domain_on_params(pa
);
8872 return isl_union_pw_aff_pw_aff_on_domain(dom
, pa
);
8875 /* Apply "pa" to "mupa". The space of "mupa" needs to be compatible
8876 * with the domain of "pa".
8877 * Furthermore, the dimension of this space needs to be greater than zero.
8878 * The result is defined over the shared domain of the elements of "mupa"
8880 __isl_give isl_union_pw_aff
*isl_multi_union_pw_aff_apply_pw_aff(
8881 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_pw_aff
*pa
)
8885 isl_space
*space
, *space2
;
8886 isl_union_pw_aff
*upa
;
8888 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
8889 isl_pw_aff_get_space(pa
));
8890 pa
= isl_pw_aff_align_params(pa
,
8891 isl_multi_union_pw_aff_get_space(mupa
));
8895 space
= isl_multi_union_pw_aff_get_space(mupa
);
8896 space2
= isl_pw_aff_get_domain_space(pa
);
8897 equal
= isl_space_is_equal(space
, space2
);
8898 isl_space_free(space
);
8899 isl_space_free(space2
);
8903 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
8904 "spaces don't match", goto error
);
8905 if (isl_pw_aff_dim(pa
, isl_dim_in
) == 0)
8906 return isl_multi_union_pw_aff_apply_pw_aff_0D(mupa
, pa
);
8908 space
= isl_space_params(isl_multi_union_pw_aff_get_space(mupa
));
8909 upa
= isl_union_pw_aff_empty(space
);
8911 for (i
= 0; i
< pa
->n
; ++i
) {
8914 isl_multi_union_pw_aff
*mupa_i
;
8915 isl_union_pw_aff
*upa_i
;
8917 mupa_i
= isl_multi_union_pw_aff_copy(mupa
);
8918 domain
= isl_set_copy(pa
->p
[i
].set
);
8919 mupa_i
= isl_multi_union_pw_aff_intersect_range(mupa_i
, domain
);
8920 aff
= isl_aff_copy(pa
->p
[i
].aff
);
8921 upa_i
= multi_union_pw_aff_apply_aff(mupa_i
, aff
);
8922 upa
= isl_union_pw_aff_union_add(upa
, upa_i
);
8925 isl_multi_union_pw_aff_free(mupa
);
8926 isl_pw_aff_free(pa
);
8929 isl_multi_union_pw_aff_free(mupa
);
8930 isl_pw_aff_free(pa
);
8934 /* Apply "pma" to "mupa", in the special case where "mupa" is 0D.
8935 * The space of "mupa" is known to be compatible with the domain of "pma".
8937 * Construct an isl_multi_union_pw_aff that is equal to "pma"
8938 * on the domain of "mupa".
8940 static __isl_give isl_multi_union_pw_aff
*mupa_apply_pw_multi_aff_0D(
8941 __isl_take isl_multi_union_pw_aff
*mupa
,
8942 __isl_take isl_pw_multi_aff
*pma
)
8946 dom
= isl_multi_union_pw_aff_domain(mupa
);
8947 pma
= isl_pw_multi_aff_project_domain_on_params(pma
);
8949 return isl_multi_union_pw_aff_pw_multi_aff_on_domain(dom
, pma
);
8952 /* Apply "pma" to "mupa". The space of "mupa" needs to be compatible
8953 * with the domain of "pma".
8954 * The result is defined over the shared domain of the elements of "mupa"
8956 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_apply_pw_multi_aff(
8957 __isl_take isl_multi_union_pw_aff
*mupa
,
8958 __isl_take isl_pw_multi_aff
*pma
)
8960 isl_space
*space1
, *space2
;
8961 isl_multi_union_pw_aff
*res
;
8965 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
8966 isl_pw_multi_aff_get_space(pma
));
8967 pma
= isl_pw_multi_aff_align_params(pma
,
8968 isl_multi_union_pw_aff_get_space(mupa
));
8972 space1
= isl_multi_union_pw_aff_get_space(mupa
);
8973 space2
= isl_pw_multi_aff_get_domain_space(pma
);
8974 equal
= isl_space_is_equal(space1
, space2
);
8975 isl_space_free(space1
);
8976 isl_space_free(space2
);
8980 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
8981 "spaces don't match", goto error
);
8982 n_out
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
8983 if (isl_pw_multi_aff_dim(pma
, isl_dim_in
) == 0)
8984 return mupa_apply_pw_multi_aff_0D(mupa
, pma
);
8986 space1
= isl_space_range(isl_pw_multi_aff_get_space(pma
));
8987 res
= isl_multi_union_pw_aff_alloc(space1
);
8989 for (i
= 0; i
< n_out
; ++i
) {
8991 isl_union_pw_aff
*upa
;
8993 pa
= isl_pw_multi_aff_get_pw_aff(pma
, i
);
8994 upa
= isl_multi_union_pw_aff_apply_pw_aff(
8995 isl_multi_union_pw_aff_copy(mupa
), pa
);
8996 res
= isl_multi_union_pw_aff_set_union_pw_aff(res
, i
, upa
);
8999 isl_pw_multi_aff_free(pma
);
9000 isl_multi_union_pw_aff_free(mupa
);
9003 isl_multi_union_pw_aff_free(mupa
);
9004 isl_pw_multi_aff_free(pma
);
9008 /* Replace the explicit domain of "mupa" by its preimage under "upma".
9009 * If the explicit domain only keeps track of constraints on the parameters,
9010 * then only update those constraints.
9012 static __isl_give isl_multi_union_pw_aff
*preimage_explicit_domain(
9013 __isl_take isl_multi_union_pw_aff
*mupa
,
9014 __isl_keep isl_union_pw_multi_aff
*upma
)
9018 if (isl_multi_union_pw_aff_check_has_explicit_domain(mupa
) < 0)
9019 return isl_multi_union_pw_aff_free(mupa
);
9021 mupa
= isl_multi_union_pw_aff_cow(mupa
);
9025 is_params
= isl_union_set_is_params(mupa
->u
.dom
);
9027 return isl_multi_union_pw_aff_free(mupa
);
9029 upma
= isl_union_pw_multi_aff_copy(upma
);
9031 mupa
->u
.dom
= isl_union_set_intersect_params(mupa
->u
.dom
,
9032 isl_union_set_params(isl_union_pw_multi_aff_domain(upma
)));
9034 mupa
->u
.dom
= isl_union_set_preimage_union_pw_multi_aff(
9037 return isl_multi_union_pw_aff_free(mupa
);
9041 /* Compute the pullback of "mupa" by the function represented by "upma".
9042 * In other words, plug in "upma" in "mupa". The result contains
9043 * expressions defined over the domain space of "upma".
9045 * Run over all elements of "mupa" and plug in "upma" in each of them.
9047 * If "mupa" has an explicit domain, then it is this domain
9048 * that needs to undergo a pullback instead, i.e., a preimage.
9050 __isl_give isl_multi_union_pw_aff
*
9051 isl_multi_union_pw_aff_pullback_union_pw_multi_aff(
9052 __isl_take isl_multi_union_pw_aff
*mupa
,
9053 __isl_take isl_union_pw_multi_aff
*upma
)
9057 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
9058 isl_union_pw_multi_aff_get_space(upma
));
9059 upma
= isl_union_pw_multi_aff_align_params(upma
,
9060 isl_multi_union_pw_aff_get_space(mupa
));
9061 mupa
= isl_multi_union_pw_aff_cow(mupa
);
9065 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
9066 for (i
= 0; i
< n
; ++i
) {
9067 isl_union_pw_aff
*upa
;
9069 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
9070 upa
= isl_union_pw_aff_pullback_union_pw_multi_aff(upa
,
9071 isl_union_pw_multi_aff_copy(upma
));
9072 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
9075 if (isl_multi_union_pw_aff_has_explicit_domain(mupa
))
9076 mupa
= preimage_explicit_domain(mupa
, upma
);
9078 isl_union_pw_multi_aff_free(upma
);
9081 isl_multi_union_pw_aff_free(mupa
);
9082 isl_union_pw_multi_aff_free(upma
);
9086 /* Extract the sequence of elements in "mupa" with domain space "space"
9087 * (ignoring parameters).
9089 * For the elements of "mupa" that are not defined on the specified space,
9090 * the corresponding element in the result is empty.
9092 __isl_give isl_multi_pw_aff
*isl_multi_union_pw_aff_extract_multi_pw_aff(
9093 __isl_keep isl_multi_union_pw_aff
*mupa
, __isl_take isl_space
*space
)
9096 isl_space
*space_mpa
;
9097 isl_multi_pw_aff
*mpa
;
9099 if (!mupa
|| !space
)
9102 space_mpa
= isl_multi_union_pw_aff_get_space(mupa
);
9103 space
= isl_space_replace_params(space
, space_mpa
);
9104 space_mpa
= isl_space_map_from_domain_and_range(isl_space_copy(space
),
9106 mpa
= isl_multi_pw_aff_alloc(space_mpa
);
9108 space
= isl_space_from_domain(space
);
9109 space
= isl_space_add_dims(space
, isl_dim_out
, 1);
9110 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
9111 for (i
= 0; i
< n
; ++i
) {
9112 isl_union_pw_aff
*upa
;
9115 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
9116 pa
= isl_union_pw_aff_extract_pw_aff(upa
,
9117 isl_space_copy(space
));
9118 mpa
= isl_multi_pw_aff_set_pw_aff(mpa
, i
, pa
);
9119 isl_union_pw_aff_free(upa
);
9122 isl_space_free(space
);
9125 isl_space_free(space
);
9129 /* Evaluate the affine function "aff" in the void point "pnt".
9130 * In particular, return the value NaN.
9132 static __isl_give isl_val
*eval_void(__isl_take isl_aff
*aff
,
9133 __isl_take isl_point
*pnt
)
9137 ctx
= isl_point_get_ctx(pnt
);
9139 isl_point_free(pnt
);
9140 return isl_val_nan(ctx
);
9143 /* Evaluate the affine expression "aff"
9144 * in the coordinates (with denominator) "pnt".
9146 static __isl_give isl_val
*eval(__isl_keep isl_vec
*aff
,
9147 __isl_keep isl_vec
*pnt
)
9156 ctx
= isl_vec_get_ctx(aff
);
9159 isl_seq_inner_product(aff
->el
+ 1, pnt
->el
, pnt
->size
, &n
);
9160 isl_int_mul(d
, aff
->el
[0], pnt
->el
[0]);
9161 v
= isl_val_rat_from_isl_int(ctx
, n
, d
);
9162 v
= isl_val_normalize(v
);
9169 /* Check that the domain space of "aff" is equal to "space".
9171 static isl_stat
isl_aff_check_has_domain_space(__isl_keep isl_aff
*aff
,
9172 __isl_keep isl_space
*space
)
9176 ok
= isl_space_is_equal(isl_aff_peek_domain_space(aff
), space
);
9178 return isl_stat_error
;
9180 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
9181 "incompatible spaces", return isl_stat_error
);
9185 /* Evaluate the affine function "aff" in "pnt".
9187 __isl_give isl_val
*isl_aff_eval(__isl_take isl_aff
*aff
,
9188 __isl_take isl_point
*pnt
)
9192 isl_local_space
*ls
;
9194 if (isl_aff_check_has_domain_space(aff
, isl_point_peek_space(pnt
)) < 0)
9196 is_void
= isl_point_is_void(pnt
);
9200 return eval_void(aff
, pnt
);
9202 ls
= isl_aff_get_domain_local_space(aff
);
9203 pnt
= isl_local_space_lift_point(ls
, pnt
);
9205 v
= eval(aff
->v
, isl_point_peek_vec(pnt
));
9208 isl_point_free(pnt
);
9213 isl_point_free(pnt
);