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 union_pw_aff
46 #include <isl_list_templ.c>
49 #define BASE union_pw_multi_aff
51 #include <isl_list_templ.c>
53 __isl_give isl_aff
*isl_aff_alloc_vec(__isl_take isl_local_space
*ls
,
54 __isl_take isl_vec
*v
)
61 aff
= isl_calloc_type(v
->ctx
, struct isl_aff
);
71 isl_local_space_free(ls
);
76 __isl_give isl_aff
*isl_aff_alloc(__isl_take isl_local_space
*ls
)
85 ctx
= isl_local_space_get_ctx(ls
);
86 if (!isl_local_space_divs_known(ls
))
87 isl_die(ctx
, isl_error_invalid
, "local space has unknown divs",
89 if (!isl_local_space_is_set(ls
))
90 isl_die(ctx
, isl_error_invalid
,
91 "domain of affine expression should be a set",
94 total
= isl_local_space_dim(ls
, isl_dim_all
);
95 v
= isl_vec_alloc(ctx
, 1 + 1 + total
);
96 return isl_aff_alloc_vec(ls
, v
);
98 isl_local_space_free(ls
);
102 __isl_give isl_aff
*isl_aff_zero_on_domain(__isl_take isl_local_space
*ls
)
106 aff
= isl_aff_alloc(ls
);
110 isl_int_set_si(aff
->v
->el
[0], 1);
111 isl_seq_clr(aff
->v
->el
+ 1, aff
->v
->size
- 1);
116 /* Return a piecewise affine expression defined on the specified domain
117 * that is equal to zero.
119 __isl_give isl_pw_aff
*isl_pw_aff_zero_on_domain(__isl_take isl_local_space
*ls
)
121 return isl_pw_aff_from_aff(isl_aff_zero_on_domain(ls
));
124 /* Return an affine expression defined on the specified domain
125 * that represents NaN.
127 __isl_give isl_aff
*isl_aff_nan_on_domain(__isl_take isl_local_space
*ls
)
131 aff
= isl_aff_alloc(ls
);
135 isl_seq_clr(aff
->v
->el
, aff
->v
->size
);
140 /* Return a piecewise affine expression defined on the specified domain
141 * that represents NaN.
143 __isl_give isl_pw_aff
*isl_pw_aff_nan_on_domain(__isl_take isl_local_space
*ls
)
145 return isl_pw_aff_from_aff(isl_aff_nan_on_domain(ls
));
148 /* Return an affine expression that is equal to "val" on
149 * domain local space "ls".
151 __isl_give isl_aff
*isl_aff_val_on_domain(__isl_take isl_local_space
*ls
,
152 __isl_take isl_val
*val
)
158 if (!isl_val_is_rat(val
))
159 isl_die(isl_val_get_ctx(val
), isl_error_invalid
,
160 "expecting rational value", goto error
);
162 aff
= isl_aff_alloc(isl_local_space_copy(ls
));
166 isl_seq_clr(aff
->v
->el
+ 2, aff
->v
->size
- 2);
167 isl_int_set(aff
->v
->el
[1], val
->n
);
168 isl_int_set(aff
->v
->el
[0], val
->d
);
170 isl_local_space_free(ls
);
174 isl_local_space_free(ls
);
179 /* Return an affine expression that is equal to the specified dimension
182 __isl_give isl_aff
*isl_aff_var_on_domain(__isl_take isl_local_space
*ls
,
183 enum isl_dim_type type
, unsigned pos
)
191 space
= isl_local_space_get_space(ls
);
194 if (isl_space_is_map(space
))
195 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
196 "expecting (parameter) set space", goto error
);
197 if (pos
>= isl_local_space_dim(ls
, type
))
198 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
199 "position out of bounds", goto error
);
201 isl_space_free(space
);
202 aff
= isl_aff_alloc(ls
);
206 pos
+= isl_local_space_offset(aff
->ls
, type
);
208 isl_int_set_si(aff
->v
->el
[0], 1);
209 isl_seq_clr(aff
->v
->el
+ 1, aff
->v
->size
- 1);
210 isl_int_set_si(aff
->v
->el
[1 + pos
], 1);
214 isl_local_space_free(ls
);
215 isl_space_free(space
);
219 /* Return a piecewise affine expression that is equal to
220 * the specified dimension in "ls".
222 __isl_give isl_pw_aff
*isl_pw_aff_var_on_domain(__isl_take isl_local_space
*ls
,
223 enum isl_dim_type type
, unsigned pos
)
225 return isl_pw_aff_from_aff(isl_aff_var_on_domain(ls
, type
, pos
));
228 /* Return an affine expression that is equal to the parameter
229 * in the domain space "space" with identifier "id".
231 __isl_give isl_aff
*isl_aff_param_on_domain_space_id(
232 __isl_take isl_space
*space
, __isl_take isl_id
*id
)
239 pos
= isl_space_find_dim_by_id(space
, isl_dim_param
, id
);
241 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
242 "parameter not found in space", goto error
);
244 ls
= isl_local_space_from_space(space
);
245 return isl_aff_var_on_domain(ls
, isl_dim_param
, pos
);
247 isl_space_free(space
);
252 __isl_give isl_aff
*isl_aff_copy(__isl_keep isl_aff
*aff
)
261 __isl_give isl_aff
*isl_aff_dup(__isl_keep isl_aff
*aff
)
266 return isl_aff_alloc_vec(isl_local_space_copy(aff
->ls
),
267 isl_vec_copy(aff
->v
));
270 __isl_give isl_aff
*isl_aff_cow(__isl_take isl_aff
*aff
)
278 return isl_aff_dup(aff
);
281 __isl_null isl_aff
*isl_aff_free(__isl_take isl_aff
*aff
)
289 isl_local_space_free(aff
->ls
);
290 isl_vec_free(aff
->v
);
297 isl_ctx
*isl_aff_get_ctx(__isl_keep isl_aff
*aff
)
299 return aff
? isl_local_space_get_ctx(aff
->ls
) : NULL
;
302 /* Return a hash value that digests "aff".
304 uint32_t isl_aff_get_hash(__isl_keep isl_aff
*aff
)
306 uint32_t hash
, ls_hash
, v_hash
;
311 hash
= isl_hash_init();
312 ls_hash
= isl_local_space_get_hash(aff
->ls
);
313 isl_hash_hash(hash
, ls_hash
);
314 v_hash
= isl_vec_get_hash(aff
->v
);
315 isl_hash_hash(hash
, v_hash
);
320 /* Externally, an isl_aff has a map space, but internally, the
321 * ls field corresponds to the domain of that space.
323 int isl_aff_dim(__isl_keep isl_aff
*aff
, enum isl_dim_type type
)
327 if (type
== isl_dim_out
)
329 if (type
== isl_dim_in
)
331 return isl_local_space_dim(aff
->ls
, type
);
334 /* Return the position of the dimension of the given type and name
336 * Return -1 if no such dimension can be found.
338 int isl_aff_find_dim_by_name(__isl_keep isl_aff
*aff
, enum isl_dim_type type
,
343 if (type
== isl_dim_out
)
345 if (type
== isl_dim_in
)
347 return isl_local_space_find_dim_by_name(aff
->ls
, type
, name
);
350 /* Return the domain space of "aff".
352 static __isl_keep isl_space
*isl_aff_peek_domain_space(__isl_keep isl_aff
*aff
)
354 return aff
? isl_local_space_peek_space(aff
->ls
) : NULL
;
357 __isl_give isl_space
*isl_aff_get_domain_space(__isl_keep isl_aff
*aff
)
359 return isl_space_copy(isl_aff_peek_domain_space(aff
));
362 __isl_give isl_space
*isl_aff_get_space(__isl_keep isl_aff
*aff
)
367 space
= isl_local_space_get_space(aff
->ls
);
368 space
= isl_space_from_domain(space
);
369 space
= isl_space_add_dims(space
, isl_dim_out
, 1);
373 __isl_give isl_local_space
*isl_aff_get_domain_local_space(
374 __isl_keep isl_aff
*aff
)
376 return aff
? isl_local_space_copy(aff
->ls
) : NULL
;
379 __isl_give isl_local_space
*isl_aff_get_local_space(__isl_keep isl_aff
*aff
)
384 ls
= isl_local_space_copy(aff
->ls
);
385 ls
= isl_local_space_from_domain(ls
);
386 ls
= isl_local_space_add_dims(ls
, isl_dim_out
, 1);
390 /* Return the local space of the domain of "aff".
391 * This may be either a copy or the local space itself
392 * if there is only one reference to "aff".
393 * This allows the local space to be modified inplace
394 * if both the expression and its local space have only a single reference.
395 * The caller is not allowed to modify "aff" between this call and
396 * a subsequent call to isl_aff_restore_domain_local_space.
397 * The only exception is that isl_aff_free can be called instead.
399 __isl_give isl_local_space
*isl_aff_take_domain_local_space(
400 __isl_keep isl_aff
*aff
)
407 return isl_aff_get_domain_local_space(aff
);
413 /* Set the local space of the domain of "aff" to "ls",
414 * where the local space of "aff" may be missing
415 * due to a preceding call to isl_aff_take_domain_local_space.
416 * However, in this case, "aff" only has a single reference and
417 * then the call to isl_aff_cow has no effect.
419 __isl_give isl_aff
*isl_aff_restore_domain_local_space(
420 __isl_keep isl_aff
*aff
, __isl_take isl_local_space
*ls
)
426 isl_local_space_free(ls
);
430 aff
= isl_aff_cow(aff
);
433 isl_local_space_free(aff
->ls
);
439 isl_local_space_free(ls
);
443 /* Externally, an isl_aff has a map space, but internally, the
444 * ls field corresponds to the domain of that space.
446 const char *isl_aff_get_dim_name(__isl_keep isl_aff
*aff
,
447 enum isl_dim_type type
, unsigned pos
)
451 if (type
== isl_dim_out
)
453 if (type
== isl_dim_in
)
455 return isl_local_space_get_dim_name(aff
->ls
, type
, pos
);
458 __isl_give isl_aff
*isl_aff_reset_domain_space(__isl_take isl_aff
*aff
,
459 __isl_take isl_space
*dim
)
461 aff
= isl_aff_cow(aff
);
465 aff
->ls
= isl_local_space_reset_space(aff
->ls
, dim
);
467 return isl_aff_free(aff
);
476 /* Reset the space of "aff". This function is called from isl_pw_templ.c
477 * and doesn't know if the space of an element object is represented
478 * directly or through its domain. It therefore passes along both.
480 __isl_give isl_aff
*isl_aff_reset_space_and_domain(__isl_take isl_aff
*aff
,
481 __isl_take isl_space
*space
, __isl_take isl_space
*domain
)
483 isl_space_free(space
);
484 return isl_aff_reset_domain_space(aff
, domain
);
487 /* Reorder the coefficients of the affine expression based
488 * on the given reordering.
489 * The reordering r is assumed to have been extended with the local
492 static __isl_give isl_vec
*vec_reorder(__isl_take isl_vec
*vec
,
493 __isl_take isl_reordering
*r
, int n_div
)
501 res
= isl_vec_alloc(vec
->ctx
,
502 2 + isl_space_dim(r
->dim
, isl_dim_all
) + n_div
);
505 isl_seq_cpy(res
->el
, vec
->el
, 2);
506 isl_seq_clr(res
->el
+ 2, res
->size
- 2);
507 for (i
= 0; i
< r
->len
; ++i
)
508 isl_int_set(res
->el
[2 + r
->pos
[i
]], vec
->el
[2 + i
]);
510 isl_reordering_free(r
);
515 isl_reordering_free(r
);
519 /* Reorder the dimensions of the domain of "aff" according
520 * to the given reordering.
522 __isl_give isl_aff
*isl_aff_realign_domain(__isl_take isl_aff
*aff
,
523 __isl_take isl_reordering
*r
)
525 aff
= isl_aff_cow(aff
);
529 r
= isl_reordering_extend(r
, aff
->ls
->div
->n_row
);
530 aff
->v
= vec_reorder(aff
->v
, isl_reordering_copy(r
),
531 aff
->ls
->div
->n_row
);
532 aff
->ls
= isl_local_space_realign(aff
->ls
, r
);
534 if (!aff
->v
|| !aff
->ls
)
535 return isl_aff_free(aff
);
540 isl_reordering_free(r
);
544 __isl_give isl_aff
*isl_aff_align_params(__isl_take isl_aff
*aff
,
545 __isl_take isl_space
*model
)
547 isl_bool equal_params
;
552 equal_params
= isl_space_has_equal_params(aff
->ls
->dim
, model
);
553 if (equal_params
< 0)
558 model
= isl_space_drop_dims(model
, isl_dim_in
,
559 0, isl_space_dim(model
, isl_dim_in
));
560 model
= isl_space_drop_dims(model
, isl_dim_out
,
561 0, isl_space_dim(model
, isl_dim_out
));
562 exp
= isl_parameter_alignment_reordering(aff
->ls
->dim
, model
);
563 exp
= isl_reordering_extend_space(exp
,
564 isl_aff_get_domain_space(aff
));
565 aff
= isl_aff_realign_domain(aff
, exp
);
568 isl_space_free(model
);
571 isl_space_free(model
);
576 /* Is "aff" obviously equal to zero?
578 * If the denominator is zero, then "aff" is not equal to zero.
580 isl_bool
isl_aff_plain_is_zero(__isl_keep isl_aff
*aff
)
583 return isl_bool_error
;
585 if (isl_int_is_zero(aff
->v
->el
[0]))
586 return isl_bool_false
;
587 return isl_seq_first_non_zero(aff
->v
->el
+ 1, aff
->v
->size
- 1) < 0;
590 /* Does "aff" represent NaN?
592 isl_bool
isl_aff_is_nan(__isl_keep isl_aff
*aff
)
595 return isl_bool_error
;
597 return isl_seq_first_non_zero(aff
->v
->el
, 2) < 0;
600 /* Are "aff1" and "aff2" obviously equal?
602 * NaN is not equal to anything, not even to another NaN.
604 isl_bool
isl_aff_plain_is_equal(__isl_keep isl_aff
*aff1
,
605 __isl_keep isl_aff
*aff2
)
610 return isl_bool_error
;
612 if (isl_aff_is_nan(aff1
) || isl_aff_is_nan(aff2
))
613 return isl_bool_false
;
615 equal
= isl_local_space_is_equal(aff1
->ls
, aff2
->ls
);
616 if (equal
< 0 || !equal
)
619 return isl_vec_is_equal(aff1
->v
, aff2
->v
);
622 /* Return the common denominator of "aff" in "v".
624 * We cannot return anything meaningful in case of a NaN.
626 isl_stat
isl_aff_get_denominator(__isl_keep isl_aff
*aff
, isl_int
*v
)
629 return isl_stat_error
;
630 if (isl_aff_is_nan(aff
))
631 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
632 "cannot get denominator of NaN", return isl_stat_error
);
633 isl_int_set(*v
, aff
->v
->el
[0]);
637 /* Return the common denominator of "aff".
639 __isl_give isl_val
*isl_aff_get_denominator_val(__isl_keep isl_aff
*aff
)
646 ctx
= isl_aff_get_ctx(aff
);
647 if (isl_aff_is_nan(aff
))
648 return isl_val_nan(ctx
);
649 return isl_val_int_from_isl_int(ctx
, aff
->v
->el
[0]);
652 /* Return the constant term of "aff".
654 __isl_give isl_val
*isl_aff_get_constant_val(__isl_keep isl_aff
*aff
)
662 ctx
= isl_aff_get_ctx(aff
);
663 if (isl_aff_is_nan(aff
))
664 return isl_val_nan(ctx
);
665 v
= isl_val_rat_from_isl_int(ctx
, aff
->v
->el
[1], aff
->v
->el
[0]);
666 return isl_val_normalize(v
);
669 /* Return the coefficient of the variable of type "type" at position "pos"
672 __isl_give isl_val
*isl_aff_get_coefficient_val(__isl_keep isl_aff
*aff
,
673 enum isl_dim_type type
, int pos
)
681 ctx
= isl_aff_get_ctx(aff
);
682 if (type
== isl_dim_out
)
683 isl_die(ctx
, isl_error_invalid
,
684 "output/set dimension does not have a coefficient",
686 if (type
== isl_dim_in
)
689 if (pos
>= isl_local_space_dim(aff
->ls
, type
))
690 isl_die(ctx
, isl_error_invalid
,
691 "position out of bounds", return NULL
);
693 if (isl_aff_is_nan(aff
))
694 return isl_val_nan(ctx
);
695 pos
+= isl_local_space_offset(aff
->ls
, type
);
696 v
= isl_val_rat_from_isl_int(ctx
, aff
->v
->el
[1 + pos
], aff
->v
->el
[0]);
697 return isl_val_normalize(v
);
700 /* Return the sign of the coefficient of the variable of type "type"
701 * at position "pos" of "aff".
703 int isl_aff_coefficient_sgn(__isl_keep isl_aff
*aff
, enum isl_dim_type type
,
711 ctx
= isl_aff_get_ctx(aff
);
712 if (type
== isl_dim_out
)
713 isl_die(ctx
, isl_error_invalid
,
714 "output/set dimension does not have a coefficient",
716 if (type
== isl_dim_in
)
719 if (pos
>= isl_local_space_dim(aff
->ls
, type
))
720 isl_die(ctx
, isl_error_invalid
,
721 "position out of bounds", return 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 (pos
>= isl_local_space_dim(aff
->ls
, type
))
981 isl_die(aff
->v
->ctx
, isl_error_invalid
,
982 "position out of bounds", return isl_aff_free(aff
));
984 if (isl_aff_is_nan(aff
))
986 aff
= isl_aff_cow(aff
);
990 aff
->v
= isl_vec_cow(aff
->v
);
992 return isl_aff_free(aff
);
994 pos
+= isl_local_space_offset(aff
->ls
, type
);
995 isl_int_set(aff
->v
->el
[1 + pos
], v
);
1000 /* Replace the numerator of the coefficient of the variable of type "type"
1001 * at position "pos" of "aff" by "v".
1003 * A NaN is unaffected by this operation.
1005 __isl_give isl_aff
*isl_aff_set_coefficient_si(__isl_take isl_aff
*aff
,
1006 enum isl_dim_type type
, int pos
, int v
)
1011 if (type
== isl_dim_out
)
1012 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1013 "output/set dimension does not have a coefficient",
1014 return isl_aff_free(aff
));
1015 if (type
== isl_dim_in
)
1018 if (pos
< 0 || pos
>= isl_local_space_dim(aff
->ls
, type
))
1019 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1020 "position out of bounds", return isl_aff_free(aff
));
1022 if (isl_aff_is_nan(aff
))
1024 pos
+= isl_local_space_offset(aff
->ls
, type
);
1025 if (isl_int_cmp_si(aff
->v
->el
[1 + pos
], v
) == 0)
1028 aff
= isl_aff_cow(aff
);
1032 aff
->v
= isl_vec_cow(aff
->v
);
1034 return isl_aff_free(aff
);
1036 isl_int_set_si(aff
->v
->el
[1 + pos
], v
);
1041 /* Replace the coefficient of the variable of type "type" at position "pos"
1044 * A NaN is unaffected by this operation.
1046 __isl_give isl_aff
*isl_aff_set_coefficient_val(__isl_take isl_aff
*aff
,
1047 enum isl_dim_type type
, int pos
, __isl_take isl_val
*v
)
1052 if (type
== isl_dim_out
)
1053 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1054 "output/set dimension does not have a coefficient",
1056 if (type
== isl_dim_in
)
1059 if (pos
>= isl_local_space_dim(aff
->ls
, type
))
1060 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1061 "position out of bounds", goto error
);
1063 if (isl_aff_is_nan(aff
)) {
1067 if (!isl_val_is_rat(v
))
1068 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1069 "expecting rational value", goto error
);
1071 pos
+= isl_local_space_offset(aff
->ls
, type
);
1072 if (isl_int_eq(aff
->v
->el
[1 + pos
], v
->n
) &&
1073 isl_int_eq(aff
->v
->el
[0], v
->d
)) {
1078 aff
= isl_aff_cow(aff
);
1081 aff
->v
= isl_vec_cow(aff
->v
);
1085 if (isl_int_eq(aff
->v
->el
[0], v
->d
)) {
1086 isl_int_set(aff
->v
->el
[1 + pos
], v
->n
);
1087 } else if (isl_int_is_one(v
->d
)) {
1088 isl_int_mul(aff
->v
->el
[1 + pos
], aff
->v
->el
[0], v
->n
);
1090 isl_seq_scale(aff
->v
->el
+ 1,
1091 aff
->v
->el
+ 1, v
->d
, aff
->v
->size
- 1);
1092 isl_int_mul(aff
->v
->el
[1 + pos
], aff
->v
->el
[0], v
->n
);
1093 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], v
->d
);
1094 aff
->v
= isl_vec_normalize(aff
->v
);
1107 /* Add "v" to the coefficient of the variable of type "type"
1108 * at position "pos" of "aff".
1110 * A NaN is unaffected by this operation.
1112 __isl_give isl_aff
*isl_aff_add_coefficient(__isl_take isl_aff
*aff
,
1113 enum isl_dim_type type
, int pos
, isl_int v
)
1118 if (type
== isl_dim_out
)
1119 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1120 "output/set dimension does not have a coefficient",
1121 return isl_aff_free(aff
));
1122 if (type
== isl_dim_in
)
1125 if (pos
>= isl_local_space_dim(aff
->ls
, type
))
1126 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1127 "position out of bounds", return isl_aff_free(aff
));
1129 if (isl_aff_is_nan(aff
))
1131 aff
= isl_aff_cow(aff
);
1135 aff
->v
= isl_vec_cow(aff
->v
);
1137 return isl_aff_free(aff
);
1139 pos
+= isl_local_space_offset(aff
->ls
, type
);
1140 isl_int_addmul(aff
->v
->el
[1 + pos
], aff
->v
->el
[0], v
);
1145 /* Add "v" to the coefficient of the variable of type "type"
1146 * at position "pos" of "aff".
1148 * A NaN is unaffected by this operation.
1150 __isl_give isl_aff
*isl_aff_add_coefficient_val(__isl_take isl_aff
*aff
,
1151 enum isl_dim_type type
, int pos
, __isl_take isl_val
*v
)
1156 if (isl_val_is_zero(v
)) {
1161 if (type
== isl_dim_out
)
1162 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1163 "output/set dimension does not have a coefficient",
1165 if (type
== isl_dim_in
)
1168 if (pos
>= isl_local_space_dim(aff
->ls
, type
))
1169 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1170 "position out of bounds", goto error
);
1172 if (isl_aff_is_nan(aff
)) {
1176 if (!isl_val_is_rat(v
))
1177 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1178 "expecting rational value", goto error
);
1180 aff
= isl_aff_cow(aff
);
1184 aff
->v
= isl_vec_cow(aff
->v
);
1188 pos
+= isl_local_space_offset(aff
->ls
, type
);
1189 if (isl_int_is_one(v
->d
)) {
1190 isl_int_addmul(aff
->v
->el
[1 + pos
], aff
->v
->el
[0], v
->n
);
1191 } else if (isl_int_eq(aff
->v
->el
[0], v
->d
)) {
1192 isl_int_add(aff
->v
->el
[1 + pos
], aff
->v
->el
[1 + pos
], v
->n
);
1193 aff
->v
= isl_vec_normalize(aff
->v
);
1197 isl_seq_scale(aff
->v
->el
+ 1,
1198 aff
->v
->el
+ 1, v
->d
, aff
->v
->size
- 1);
1199 isl_int_addmul(aff
->v
->el
[1 + pos
], aff
->v
->el
[0], v
->n
);
1200 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], v
->d
);
1201 aff
->v
= isl_vec_normalize(aff
->v
);
1214 __isl_give isl_aff
*isl_aff_add_coefficient_si(__isl_take isl_aff
*aff
,
1215 enum isl_dim_type type
, int pos
, int v
)
1220 isl_int_set_si(t
, v
);
1221 aff
= isl_aff_add_coefficient(aff
, type
, pos
, t
);
1227 __isl_give isl_aff
*isl_aff_get_div(__isl_keep isl_aff
*aff
, int pos
)
1232 return isl_local_space_get_div(aff
->ls
, pos
);
1235 /* Return the negation of "aff".
1237 * As a special case, -NaN = NaN.
1239 __isl_give isl_aff
*isl_aff_neg(__isl_take isl_aff
*aff
)
1243 if (isl_aff_is_nan(aff
))
1245 aff
= isl_aff_cow(aff
);
1248 aff
->v
= isl_vec_cow(aff
->v
);
1250 return isl_aff_free(aff
);
1252 isl_seq_neg(aff
->v
->el
+ 1, aff
->v
->el
+ 1, aff
->v
->size
- 1);
1257 /* Remove divs from the local space that do not appear in the affine
1259 * We currently only remove divs at the end.
1260 * Some intermediate divs may also not appear directly in the affine
1261 * expression, but we would also need to check that no other divs are
1262 * defined in terms of them.
1264 __isl_give isl_aff
*isl_aff_remove_unused_divs(__isl_take isl_aff
*aff
)
1273 n
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
1274 off
= isl_local_space_offset(aff
->ls
, isl_dim_div
);
1276 pos
= isl_seq_last_non_zero(aff
->v
->el
+ 1 + off
, n
) + 1;
1280 aff
= isl_aff_cow(aff
);
1284 aff
->ls
= isl_local_space_drop_dims(aff
->ls
, isl_dim_div
, pos
, n
- pos
);
1285 aff
->v
= isl_vec_drop_els(aff
->v
, 1 + off
+ pos
, n
- pos
);
1286 if (!aff
->ls
|| !aff
->v
)
1287 return isl_aff_free(aff
);
1292 /* Look for any divs in the aff->ls with a denominator equal to one
1293 * and plug them into the affine expression and any subsequent divs
1294 * that may reference the div.
1296 static __isl_give isl_aff
*plug_in_integral_divs(__isl_take isl_aff
*aff
)
1302 isl_local_space
*ls
;
1308 n
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
1310 for (i
= 0; i
< n
; ++i
) {
1311 if (!isl_int_is_one(aff
->ls
->div
->row
[i
][0]))
1313 ls
= isl_local_space_copy(aff
->ls
);
1314 ls
= isl_local_space_substitute_seq(ls
, isl_dim_div
, i
,
1315 aff
->ls
->div
->row
[i
], len
, i
+ 1, n
- (i
+ 1));
1316 vec
= isl_vec_copy(aff
->v
);
1317 vec
= isl_vec_cow(vec
);
1323 pos
= isl_local_space_offset(aff
->ls
, isl_dim_div
) + i
;
1324 isl_seq_substitute(vec
->el
, pos
, aff
->ls
->div
->row
[i
],
1329 isl_vec_free(aff
->v
);
1331 isl_local_space_free(aff
->ls
);
1338 isl_local_space_free(ls
);
1339 return isl_aff_free(aff
);
1342 /* Look for any divs j that appear with a unit coefficient inside
1343 * the definitions of other divs i and plug them into the definitions
1346 * In particular, an expression of the form
1348 * floor((f(..) + floor(g(..)/n))/m)
1352 * floor((n * f(..) + g(..))/(n * m))
1354 * This simplification is correct because we can move the expression
1355 * f(..) into the inner floor in the original expression to obtain
1357 * floor(floor((n * f(..) + g(..))/n)/m)
1359 * from which we can derive the simplified expression.
1361 static __isl_give isl_aff
*plug_in_unit_divs(__isl_take isl_aff
*aff
)
1369 n
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
1370 off
= isl_local_space_offset(aff
->ls
, isl_dim_div
);
1371 for (i
= 1; i
< n
; ++i
) {
1372 for (j
= 0; j
< i
; ++j
) {
1373 if (!isl_int_is_one(aff
->ls
->div
->row
[i
][1 + off
+ j
]))
1375 aff
->ls
= isl_local_space_substitute_seq(aff
->ls
,
1376 isl_dim_div
, j
, aff
->ls
->div
->row
[j
],
1377 aff
->v
->size
, i
, 1);
1379 return isl_aff_free(aff
);
1386 /* Swap divs "a" and "b" in "aff", which is assumed to be non-NULL.
1388 * Even though this function is only called on isl_affs with a single
1389 * reference, we are careful to only change aff->v and aff->ls together.
1391 static __isl_give isl_aff
*swap_div(__isl_take isl_aff
*aff
, int a
, int b
)
1393 unsigned off
= isl_local_space_offset(aff
->ls
, isl_dim_div
);
1394 isl_local_space
*ls
;
1397 ls
= isl_local_space_copy(aff
->ls
);
1398 ls
= isl_local_space_swap_div(ls
, a
, b
);
1399 v
= isl_vec_copy(aff
->v
);
1404 isl_int_swap(v
->el
[1 + off
+ a
], v
->el
[1 + off
+ b
]);
1405 isl_vec_free(aff
->v
);
1407 isl_local_space_free(aff
->ls
);
1413 isl_local_space_free(ls
);
1414 return isl_aff_free(aff
);
1417 /* Merge divs "a" and "b" in "aff", which is assumed to be non-NULL.
1419 * We currently do not actually remove div "b", but simply add its
1420 * coefficient to that of "a" and then zero it out.
1422 static __isl_give isl_aff
*merge_divs(__isl_take isl_aff
*aff
, int a
, int b
)
1424 unsigned off
= isl_local_space_offset(aff
->ls
, isl_dim_div
);
1426 if (isl_int_is_zero(aff
->v
->el
[1 + off
+ b
]))
1429 aff
->v
= isl_vec_cow(aff
->v
);
1431 return isl_aff_free(aff
);
1433 isl_int_add(aff
->v
->el
[1 + off
+ a
],
1434 aff
->v
->el
[1 + off
+ a
], aff
->v
->el
[1 + off
+ b
]);
1435 isl_int_set_si(aff
->v
->el
[1 + off
+ b
], 0);
1440 /* Sort the divs in the local space of "aff" according to
1441 * the comparison function "cmp_row" in isl_local_space.c,
1442 * combining the coefficients of identical divs.
1444 * Reordering divs does not change the semantics of "aff",
1445 * so there is no need to call isl_aff_cow.
1446 * Moreover, this function is currently only called on isl_affs
1447 * with a single reference.
1449 static __isl_give isl_aff
*sort_divs(__isl_take isl_aff
*aff
)
1456 n
= isl_aff_dim(aff
, isl_dim_div
);
1457 for (i
= 1; i
< n
; ++i
) {
1458 for (j
= i
- 1; j
>= 0; --j
) {
1459 int cmp
= isl_mat_cmp_div(aff
->ls
->div
, j
, j
+ 1);
1463 aff
= merge_divs(aff
, j
, j
+ 1);
1465 aff
= swap_div(aff
, j
, j
+ 1);
1474 /* Normalize the representation of "aff".
1476 * This function should only be called of "new" isl_affs, i.e.,
1477 * with only a single reference. We therefore do not need to
1478 * worry about affecting other instances.
1480 __isl_give isl_aff
*isl_aff_normalize(__isl_take isl_aff
*aff
)
1484 aff
->v
= isl_vec_normalize(aff
->v
);
1486 return isl_aff_free(aff
);
1487 aff
= plug_in_integral_divs(aff
);
1488 aff
= plug_in_unit_divs(aff
);
1489 aff
= sort_divs(aff
);
1490 aff
= isl_aff_remove_unused_divs(aff
);
1494 /* Given f, return floor(f).
1495 * If f is an integer expression, then just return f.
1496 * If f is a constant, then return the constant floor(f).
1497 * Otherwise, if f = g/m, write g = q m + r,
1498 * create a new div d = [r/m] and return the expression q + d.
1499 * The coefficients in r are taken to lie between -m/2 and m/2.
1501 * reduce_div_coefficients performs the same normalization.
1503 * As a special case, floor(NaN) = NaN.
1505 __isl_give isl_aff
*isl_aff_floor(__isl_take isl_aff
*aff
)
1515 if (isl_aff_is_nan(aff
))
1517 if (isl_int_is_one(aff
->v
->el
[0]))
1520 aff
= isl_aff_cow(aff
);
1524 aff
->v
= isl_vec_cow(aff
->v
);
1526 return isl_aff_free(aff
);
1528 if (isl_aff_is_cst(aff
)) {
1529 isl_int_fdiv_q(aff
->v
->el
[1], aff
->v
->el
[1], aff
->v
->el
[0]);
1530 isl_int_set_si(aff
->v
->el
[0], 1);
1534 div
= isl_vec_copy(aff
->v
);
1535 div
= isl_vec_cow(div
);
1537 return isl_aff_free(aff
);
1539 ctx
= isl_aff_get_ctx(aff
);
1540 isl_int_fdiv_q(aff
->v
->el
[0], aff
->v
->el
[0], ctx
->two
);
1541 for (i
= 1; i
< aff
->v
->size
; ++i
) {
1542 isl_int_fdiv_r(div
->el
[i
], div
->el
[i
], div
->el
[0]);
1543 isl_int_fdiv_q(aff
->v
->el
[i
], aff
->v
->el
[i
], div
->el
[0]);
1544 if (isl_int_gt(div
->el
[i
], aff
->v
->el
[0])) {
1545 isl_int_sub(div
->el
[i
], div
->el
[i
], div
->el
[0]);
1546 isl_int_add_ui(aff
->v
->el
[i
], aff
->v
->el
[i
], 1);
1550 aff
->ls
= isl_local_space_add_div(aff
->ls
, div
);
1552 return isl_aff_free(aff
);
1554 size
= aff
->v
->size
;
1555 aff
->v
= isl_vec_extend(aff
->v
, size
+ 1);
1557 return isl_aff_free(aff
);
1558 isl_int_set_si(aff
->v
->el
[0], 1);
1559 isl_int_set_si(aff
->v
->el
[size
], 1);
1561 aff
= isl_aff_normalize(aff
);
1568 * aff mod m = aff - m * floor(aff/m)
1570 * with m an integer value.
1572 __isl_give isl_aff
*isl_aff_mod_val(__isl_take isl_aff
*aff
,
1573 __isl_take isl_val
*m
)
1580 if (!isl_val_is_int(m
))
1581 isl_die(isl_val_get_ctx(m
), isl_error_invalid
,
1582 "expecting integer modulo", goto error
);
1584 res
= isl_aff_copy(aff
);
1585 aff
= isl_aff_scale_down_val(aff
, isl_val_copy(m
));
1586 aff
= isl_aff_floor(aff
);
1587 aff
= isl_aff_scale_val(aff
, m
);
1588 res
= isl_aff_sub(res
, aff
);
1599 * pwaff mod m = pwaff - m * floor(pwaff/m)
1601 __isl_give isl_pw_aff
*isl_pw_aff_mod(__isl_take isl_pw_aff
*pwaff
, isl_int m
)
1605 res
= isl_pw_aff_copy(pwaff
);
1606 pwaff
= isl_pw_aff_scale_down(pwaff
, m
);
1607 pwaff
= isl_pw_aff_floor(pwaff
);
1608 pwaff
= isl_pw_aff_scale(pwaff
, m
);
1609 res
= isl_pw_aff_sub(res
, pwaff
);
1616 * pa mod m = pa - m * floor(pa/m)
1618 * with m an integer value.
1620 __isl_give isl_pw_aff
*isl_pw_aff_mod_val(__isl_take isl_pw_aff
*pa
,
1621 __isl_take isl_val
*m
)
1625 if (!isl_val_is_int(m
))
1626 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
1627 "expecting integer modulo", goto error
);
1628 pa
= isl_pw_aff_mod(pa
, m
->n
);
1632 isl_pw_aff_free(pa
);
1637 /* Given f, return ceil(f).
1638 * If f is an integer expression, then just return f.
1639 * Otherwise, let f be the expression
1645 * floor((e + m - 1)/m)
1647 * As a special case, ceil(NaN) = NaN.
1649 __isl_give isl_aff
*isl_aff_ceil(__isl_take isl_aff
*aff
)
1654 if (isl_aff_is_nan(aff
))
1656 if (isl_int_is_one(aff
->v
->el
[0]))
1659 aff
= isl_aff_cow(aff
);
1662 aff
->v
= isl_vec_cow(aff
->v
);
1664 return isl_aff_free(aff
);
1666 isl_int_add(aff
->v
->el
[1], aff
->v
->el
[1], aff
->v
->el
[0]);
1667 isl_int_sub_ui(aff
->v
->el
[1], aff
->v
->el
[1], 1);
1668 aff
= isl_aff_floor(aff
);
1673 /* Apply the expansion computed by isl_merge_divs.
1674 * The expansion itself is given by "exp" while the resulting
1675 * list of divs is given by "div".
1677 __isl_give isl_aff
*isl_aff_expand_divs(__isl_take isl_aff
*aff
,
1678 __isl_take isl_mat
*div
, int *exp
)
1684 aff
= isl_aff_cow(aff
);
1688 old_n_div
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
1689 new_n_div
= isl_mat_rows(div
);
1690 offset
= 1 + isl_local_space_offset(aff
->ls
, isl_dim_div
);
1692 aff
->v
= isl_vec_expand(aff
->v
, offset
, old_n_div
, exp
, new_n_div
);
1693 aff
->ls
= isl_local_space_replace_divs(aff
->ls
, div
);
1694 if (!aff
->v
|| !aff
->ls
)
1695 return isl_aff_free(aff
);
1703 /* Add two affine expressions that live in the same local space.
1705 static __isl_give isl_aff
*add_expanded(__isl_take isl_aff
*aff1
,
1706 __isl_take isl_aff
*aff2
)
1710 aff1
= isl_aff_cow(aff1
);
1714 aff1
->v
= isl_vec_cow(aff1
->v
);
1720 isl_int_gcd(gcd
, aff1
->v
->el
[0], aff2
->v
->el
[0]);
1721 isl_int_divexact(f
, aff2
->v
->el
[0], gcd
);
1722 isl_seq_scale(aff1
->v
->el
+ 1, aff1
->v
->el
+ 1, f
, aff1
->v
->size
- 1);
1723 isl_int_divexact(f
, aff1
->v
->el
[0], gcd
);
1724 isl_seq_addmul(aff1
->v
->el
+ 1, f
, aff2
->v
->el
+ 1, aff1
->v
->size
- 1);
1725 isl_int_divexact(f
, aff2
->v
->el
[0], gcd
);
1726 isl_int_mul(aff1
->v
->el
[0], aff1
->v
->el
[0], f
);
1738 /* Return the sum of "aff1" and "aff2".
1740 * If either of the two is NaN, then the result is NaN.
1742 __isl_give isl_aff
*isl_aff_add(__isl_take isl_aff
*aff1
,
1743 __isl_take isl_aff
*aff2
)
1754 ctx
= isl_aff_get_ctx(aff1
);
1755 if (!isl_space_is_equal(aff1
->ls
->dim
, aff2
->ls
->dim
))
1756 isl_die(ctx
, isl_error_invalid
,
1757 "spaces don't match", goto error
);
1759 if (isl_aff_is_nan(aff1
)) {
1763 if (isl_aff_is_nan(aff2
)) {
1768 n_div1
= isl_aff_dim(aff1
, isl_dim_div
);
1769 n_div2
= isl_aff_dim(aff2
, isl_dim_div
);
1770 if (n_div1
== 0 && n_div2
== 0)
1771 return add_expanded(aff1
, aff2
);
1773 exp1
= isl_alloc_array(ctx
, int, n_div1
);
1774 exp2
= isl_alloc_array(ctx
, int, n_div2
);
1775 if ((n_div1
&& !exp1
) || (n_div2
&& !exp2
))
1778 div
= isl_merge_divs(aff1
->ls
->div
, aff2
->ls
->div
, exp1
, exp2
);
1779 aff1
= isl_aff_expand_divs(aff1
, isl_mat_copy(div
), exp1
);
1780 aff2
= isl_aff_expand_divs(aff2
, div
, exp2
);
1784 return add_expanded(aff1
, aff2
);
1793 __isl_give isl_aff
*isl_aff_sub(__isl_take isl_aff
*aff1
,
1794 __isl_take isl_aff
*aff2
)
1796 return isl_aff_add(aff1
, isl_aff_neg(aff2
));
1799 /* Return the result of scaling "aff" by a factor of "f".
1801 * As a special case, f * NaN = NaN.
1803 __isl_give isl_aff
*isl_aff_scale(__isl_take isl_aff
*aff
, isl_int f
)
1809 if (isl_aff_is_nan(aff
))
1812 if (isl_int_is_one(f
))
1815 aff
= isl_aff_cow(aff
);
1818 aff
->v
= isl_vec_cow(aff
->v
);
1820 return isl_aff_free(aff
);
1822 if (isl_int_is_pos(f
) && isl_int_is_divisible_by(aff
->v
->el
[0], f
)) {
1823 isl_int_divexact(aff
->v
->el
[0], aff
->v
->el
[0], f
);
1828 isl_int_gcd(gcd
, aff
->v
->el
[0], f
);
1829 isl_int_divexact(aff
->v
->el
[0], aff
->v
->el
[0], gcd
);
1830 isl_int_divexact(gcd
, f
, gcd
);
1831 isl_seq_scale(aff
->v
->el
+ 1, aff
->v
->el
+ 1, gcd
, aff
->v
->size
- 1);
1837 /* Multiple "aff" by "v".
1839 __isl_give isl_aff
*isl_aff_scale_val(__isl_take isl_aff
*aff
,
1840 __isl_take isl_val
*v
)
1845 if (isl_val_is_one(v
)) {
1850 if (!isl_val_is_rat(v
))
1851 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1852 "expecting rational factor", goto error
);
1854 aff
= isl_aff_scale(aff
, v
->n
);
1855 aff
= isl_aff_scale_down(aff
, v
->d
);
1865 /* Return the result of scaling "aff" down by a factor of "f".
1867 * As a special case, NaN/f = NaN.
1869 __isl_give isl_aff
*isl_aff_scale_down(__isl_take isl_aff
*aff
, isl_int f
)
1875 if (isl_aff_is_nan(aff
))
1878 if (isl_int_is_one(f
))
1881 aff
= isl_aff_cow(aff
);
1885 if (isl_int_is_zero(f
))
1886 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1887 "cannot scale down by zero", return isl_aff_free(aff
));
1889 aff
->v
= isl_vec_cow(aff
->v
);
1891 return isl_aff_free(aff
);
1894 isl_seq_gcd(aff
->v
->el
+ 1, aff
->v
->size
- 1, &gcd
);
1895 isl_int_gcd(gcd
, gcd
, f
);
1896 isl_seq_scale_down(aff
->v
->el
+ 1, aff
->v
->el
+ 1, gcd
, aff
->v
->size
- 1);
1897 isl_int_divexact(gcd
, f
, gcd
);
1898 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], gcd
);
1904 /* Divide "aff" by "v".
1906 __isl_give isl_aff
*isl_aff_scale_down_val(__isl_take isl_aff
*aff
,
1907 __isl_take isl_val
*v
)
1912 if (isl_val_is_one(v
)) {
1917 if (!isl_val_is_rat(v
))
1918 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1919 "expecting rational factor", goto error
);
1920 if (!isl_val_is_pos(v
))
1921 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1922 "factor needs to be positive", goto error
);
1924 aff
= isl_aff_scale(aff
, v
->d
);
1925 aff
= isl_aff_scale_down(aff
, v
->n
);
1935 __isl_give isl_aff
*isl_aff_scale_down_ui(__isl_take isl_aff
*aff
, unsigned f
)
1943 isl_int_set_ui(v
, f
);
1944 aff
= isl_aff_scale_down(aff
, v
);
1950 __isl_give isl_aff
*isl_aff_set_dim_name(__isl_take isl_aff
*aff
,
1951 enum isl_dim_type type
, unsigned pos
, const char *s
)
1953 aff
= isl_aff_cow(aff
);
1956 if (type
== isl_dim_out
)
1957 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1958 "cannot set name of output/set dimension",
1959 return isl_aff_free(aff
));
1960 if (type
== isl_dim_in
)
1962 aff
->ls
= isl_local_space_set_dim_name(aff
->ls
, type
, pos
, s
);
1964 return isl_aff_free(aff
);
1969 __isl_give isl_aff
*isl_aff_set_dim_id(__isl_take isl_aff
*aff
,
1970 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
1972 aff
= isl_aff_cow(aff
);
1975 if (type
== isl_dim_out
)
1976 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1977 "cannot set name of output/set dimension",
1979 if (type
== isl_dim_in
)
1981 aff
->ls
= isl_local_space_set_dim_id(aff
->ls
, type
, pos
, id
);
1983 return isl_aff_free(aff
);
1992 /* Replace the identifier of the input tuple of "aff" by "id".
1993 * type is currently required to be equal to isl_dim_in
1995 __isl_give isl_aff
*isl_aff_set_tuple_id(__isl_take isl_aff
*aff
,
1996 enum isl_dim_type type
, __isl_take isl_id
*id
)
1998 aff
= isl_aff_cow(aff
);
2001 if (type
!= isl_dim_out
)
2002 isl_die(aff
->v
->ctx
, isl_error_invalid
,
2003 "cannot only set id of input tuple", goto error
);
2004 aff
->ls
= isl_local_space_set_tuple_id(aff
->ls
, isl_dim_set
, id
);
2006 return isl_aff_free(aff
);
2015 /* Exploit the equalities in "eq" to simplify the affine expression
2016 * and the expressions of the integer divisions in the local space.
2017 * The integer divisions in this local space are assumed to appear
2018 * as regular dimensions in "eq".
2020 static __isl_give isl_aff
*isl_aff_substitute_equalities_lifted(
2021 __isl_take isl_aff
*aff
, __isl_take isl_basic_set
*eq
)
2029 if (eq
->n_eq
== 0) {
2030 isl_basic_set_free(eq
);
2034 aff
= isl_aff_cow(aff
);
2038 aff
->ls
= isl_local_space_substitute_equalities(aff
->ls
,
2039 isl_basic_set_copy(eq
));
2040 aff
->v
= isl_vec_cow(aff
->v
);
2041 if (!aff
->ls
|| !aff
->v
)
2044 total
= 1 + isl_space_dim(eq
->dim
, isl_dim_all
);
2046 for (i
= 0; i
< eq
->n_eq
; ++i
) {
2047 j
= isl_seq_last_non_zero(eq
->eq
[i
], total
+ n_div
);
2048 if (j
< 0 || j
== 0 || j
>= total
)
2051 isl_seq_elim(aff
->v
->el
+ 1, eq
->eq
[i
], j
, total
,
2055 isl_basic_set_free(eq
);
2056 aff
= isl_aff_normalize(aff
);
2059 isl_basic_set_free(eq
);
2064 /* Exploit the equalities in "eq" to simplify the affine expression
2065 * and the expressions of the integer divisions in the local space.
2067 __isl_give isl_aff
*isl_aff_substitute_equalities(__isl_take isl_aff
*aff
,
2068 __isl_take isl_basic_set
*eq
)
2074 n_div
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
2076 eq
= isl_basic_set_add_dims(eq
, isl_dim_set
, n_div
);
2077 return isl_aff_substitute_equalities_lifted(aff
, eq
);
2079 isl_basic_set_free(eq
);
2084 /* Look for equalities among the variables shared by context and aff
2085 * and the integer divisions of aff, if any.
2086 * The equalities are then used to eliminate coefficients and/or integer
2087 * divisions from aff.
2089 __isl_give isl_aff
*isl_aff_gist(__isl_take isl_aff
*aff
,
2090 __isl_take isl_set
*context
)
2092 isl_basic_set
*hull
;
2097 n_div
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
2099 isl_basic_set
*bset
;
2100 isl_local_space
*ls
;
2101 context
= isl_set_add_dims(context
, isl_dim_set
, n_div
);
2102 ls
= isl_aff_get_domain_local_space(aff
);
2103 bset
= isl_basic_set_from_local_space(ls
);
2104 bset
= isl_basic_set_lift(bset
);
2105 bset
= isl_basic_set_flatten(bset
);
2106 context
= isl_set_intersect(context
,
2107 isl_set_from_basic_set(bset
));
2110 hull
= isl_set_affine_hull(context
);
2111 return isl_aff_substitute_equalities_lifted(aff
, hull
);
2114 isl_set_free(context
);
2118 __isl_give isl_aff
*isl_aff_gist_params(__isl_take isl_aff
*aff
,
2119 __isl_take isl_set
*context
)
2121 isl_set
*dom_context
= isl_set_universe(isl_aff_get_domain_space(aff
));
2122 dom_context
= isl_set_intersect_params(dom_context
, context
);
2123 return isl_aff_gist(aff
, dom_context
);
2126 /* Return a basic set containing those elements in the space
2127 * of aff where it is positive. "rational" should not be set.
2129 * If "aff" is NaN, then it is not positive.
2131 static __isl_give isl_basic_set
*aff_pos_basic_set(__isl_take isl_aff
*aff
,
2134 isl_constraint
*ineq
;
2135 isl_basic_set
*bset
;
2140 if (isl_aff_is_nan(aff
)) {
2141 isl_space
*space
= isl_aff_get_domain_space(aff
);
2143 return isl_basic_set_empty(space
);
2146 isl_die(isl_aff_get_ctx(aff
), isl_error_unsupported
,
2147 "rational sets not supported", goto error
);
2149 ineq
= isl_inequality_from_aff(aff
);
2150 c
= isl_constraint_get_constant_val(ineq
);
2151 c
= isl_val_sub_ui(c
, 1);
2152 ineq
= isl_constraint_set_constant_val(ineq
, c
);
2154 bset
= isl_basic_set_from_constraint(ineq
);
2155 bset
= isl_basic_set_simplify(bset
);
2162 /* Return a basic set containing those elements in the space
2163 * of aff where it is non-negative.
2164 * If "rational" is set, then return a rational basic set.
2166 * If "aff" is NaN, then it is not non-negative (it's not negative either).
2168 static __isl_give isl_basic_set
*aff_nonneg_basic_set(
2169 __isl_take isl_aff
*aff
, int rational
)
2171 isl_constraint
*ineq
;
2172 isl_basic_set
*bset
;
2176 if (isl_aff_is_nan(aff
)) {
2177 isl_space
*space
= isl_aff_get_domain_space(aff
);
2179 return isl_basic_set_empty(space
);
2182 ineq
= isl_inequality_from_aff(aff
);
2184 bset
= isl_basic_set_from_constraint(ineq
);
2186 bset
= isl_basic_set_set_rational(bset
);
2187 bset
= isl_basic_set_simplify(bset
);
2191 /* Return a basic set containing those elements in the space
2192 * of aff where it is non-negative.
2194 __isl_give isl_basic_set
*isl_aff_nonneg_basic_set(__isl_take isl_aff
*aff
)
2196 return aff_nonneg_basic_set(aff
, 0);
2199 /* Return a basic set containing those elements in the domain space
2200 * of "aff" where it is positive.
2202 __isl_give isl_basic_set
*isl_aff_pos_basic_set(__isl_take isl_aff
*aff
)
2204 aff
= isl_aff_add_constant_num_si(aff
, -1);
2205 return isl_aff_nonneg_basic_set(aff
);
2208 /* Return a basic set containing those elements in the domain space
2209 * of aff where it is negative.
2211 __isl_give isl_basic_set
*isl_aff_neg_basic_set(__isl_take isl_aff
*aff
)
2213 aff
= isl_aff_neg(aff
);
2214 return isl_aff_pos_basic_set(aff
);
2217 /* Return a basic set containing those elements in the space
2218 * of aff where it is zero.
2219 * If "rational" is set, then return a rational basic set.
2221 * If "aff" is NaN, then it is not zero.
2223 static __isl_give isl_basic_set
*aff_zero_basic_set(__isl_take isl_aff
*aff
,
2226 isl_constraint
*ineq
;
2227 isl_basic_set
*bset
;
2231 if (isl_aff_is_nan(aff
)) {
2232 isl_space
*space
= isl_aff_get_domain_space(aff
);
2234 return isl_basic_set_empty(space
);
2237 ineq
= isl_equality_from_aff(aff
);
2239 bset
= isl_basic_set_from_constraint(ineq
);
2241 bset
= isl_basic_set_set_rational(bset
);
2242 bset
= isl_basic_set_simplify(bset
);
2246 /* Return a basic set containing those elements in the space
2247 * of aff where it is zero.
2249 __isl_give isl_basic_set
*isl_aff_zero_basic_set(__isl_take isl_aff
*aff
)
2251 return aff_zero_basic_set(aff
, 0);
2254 /* Return a basic set containing those elements in the shared space
2255 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
2257 __isl_give isl_basic_set
*isl_aff_ge_basic_set(__isl_take isl_aff
*aff1
,
2258 __isl_take isl_aff
*aff2
)
2260 aff1
= isl_aff_sub(aff1
, aff2
);
2262 return isl_aff_nonneg_basic_set(aff1
);
2265 /* Return a basic set containing those elements in the shared domain space
2266 * of "aff1" and "aff2" where "aff1" is greater than "aff2".
2268 __isl_give isl_basic_set
*isl_aff_gt_basic_set(__isl_take isl_aff
*aff1
,
2269 __isl_take isl_aff
*aff2
)
2271 aff1
= isl_aff_sub(aff1
, aff2
);
2273 return isl_aff_pos_basic_set(aff1
);
2276 /* Return a set containing those elements in the shared space
2277 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
2279 __isl_give isl_set
*isl_aff_ge_set(__isl_take isl_aff
*aff1
,
2280 __isl_take isl_aff
*aff2
)
2282 return isl_set_from_basic_set(isl_aff_ge_basic_set(aff1
, aff2
));
2285 /* Return a set containing those elements in the shared domain space
2286 * of aff1 and aff2 where aff1 is greater than aff2.
2288 * If either of the two inputs is NaN, then the result is empty,
2289 * as comparisons with NaN always return false.
2291 __isl_give isl_set
*isl_aff_gt_set(__isl_take isl_aff
*aff1
,
2292 __isl_take isl_aff
*aff2
)
2294 return isl_set_from_basic_set(isl_aff_gt_basic_set(aff1
, aff2
));
2297 /* Return a basic set containing those elements in the shared space
2298 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
2300 __isl_give isl_basic_set
*isl_aff_le_basic_set(__isl_take isl_aff
*aff1
,
2301 __isl_take isl_aff
*aff2
)
2303 return isl_aff_ge_basic_set(aff2
, aff1
);
2306 /* Return a basic set containing those elements in the shared domain space
2307 * of "aff1" and "aff2" where "aff1" is smaller than "aff2".
2309 __isl_give isl_basic_set
*isl_aff_lt_basic_set(__isl_take isl_aff
*aff1
,
2310 __isl_take isl_aff
*aff2
)
2312 return isl_aff_gt_basic_set(aff2
, aff1
);
2315 /* Return a set containing those elements in the shared space
2316 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
2318 __isl_give isl_set
*isl_aff_le_set(__isl_take isl_aff
*aff1
,
2319 __isl_take isl_aff
*aff2
)
2321 return isl_aff_ge_set(aff2
, aff1
);
2324 /* Return a set containing those elements in the shared domain space
2325 * of "aff1" and "aff2" where "aff1" is smaller than "aff2".
2327 __isl_give isl_set
*isl_aff_lt_set(__isl_take isl_aff
*aff1
,
2328 __isl_take isl_aff
*aff2
)
2330 return isl_set_from_basic_set(isl_aff_lt_basic_set(aff1
, aff2
));
2333 /* Return a basic set containing those elements in the shared space
2334 * of aff1 and aff2 where aff1 and aff2 are equal.
2336 __isl_give isl_basic_set
*isl_aff_eq_basic_set(__isl_take isl_aff
*aff1
,
2337 __isl_take isl_aff
*aff2
)
2339 aff1
= isl_aff_sub(aff1
, aff2
);
2341 return isl_aff_zero_basic_set(aff1
);
2344 /* Return a set containing those elements in the shared space
2345 * of aff1 and aff2 where aff1 and aff2 are equal.
2347 __isl_give isl_set
*isl_aff_eq_set(__isl_take isl_aff
*aff1
,
2348 __isl_take isl_aff
*aff2
)
2350 return isl_set_from_basic_set(isl_aff_eq_basic_set(aff1
, aff2
));
2353 /* Return a set containing those elements in the shared domain space
2354 * of aff1 and aff2 where aff1 and aff2 are not equal.
2356 * If either of the two inputs is NaN, then the result is empty,
2357 * as comparisons with NaN always return false.
2359 __isl_give isl_set
*isl_aff_ne_set(__isl_take isl_aff
*aff1
,
2360 __isl_take isl_aff
*aff2
)
2362 isl_set
*set_lt
, *set_gt
;
2364 set_lt
= isl_aff_lt_set(isl_aff_copy(aff1
),
2365 isl_aff_copy(aff2
));
2366 set_gt
= isl_aff_gt_set(aff1
, aff2
);
2367 return isl_set_union_disjoint(set_lt
, set_gt
);
2370 __isl_give isl_aff
*isl_aff_add_on_domain(__isl_keep isl_set
*dom
,
2371 __isl_take isl_aff
*aff1
, __isl_take isl_aff
*aff2
)
2373 aff1
= isl_aff_add(aff1
, aff2
);
2374 aff1
= isl_aff_gist(aff1
, isl_set_copy(dom
));
2378 int isl_aff_is_empty(__isl_keep isl_aff
*aff
)
2386 /* Check whether the given affine expression has non-zero coefficient
2387 * for any dimension in the given range or if any of these dimensions
2388 * appear with non-zero coefficients in any of the integer divisions
2389 * involved in the affine expression.
2391 isl_bool
isl_aff_involves_dims(__isl_keep isl_aff
*aff
,
2392 enum isl_dim_type type
, unsigned first
, unsigned n
)
2397 isl_bool involves
= isl_bool_false
;
2400 return isl_bool_error
;
2402 return isl_bool_false
;
2404 ctx
= isl_aff_get_ctx(aff
);
2405 if (first
+ n
> isl_aff_dim(aff
, type
))
2406 isl_die(ctx
, isl_error_invalid
,
2407 "range out of bounds", return isl_bool_error
);
2409 active
= isl_local_space_get_active(aff
->ls
, aff
->v
->el
+ 2);
2413 first
+= isl_local_space_offset(aff
->ls
, type
) - 1;
2414 for (i
= 0; i
< n
; ++i
)
2415 if (active
[first
+ i
]) {
2416 involves
= isl_bool_true
;
2425 return isl_bool_error
;
2428 __isl_give isl_aff
*isl_aff_drop_dims(__isl_take isl_aff
*aff
,
2429 enum isl_dim_type type
, unsigned first
, unsigned n
)
2435 if (type
== isl_dim_out
)
2436 isl_die(aff
->v
->ctx
, isl_error_invalid
,
2437 "cannot drop output/set dimension",
2438 return isl_aff_free(aff
));
2439 if (type
== isl_dim_in
)
2441 if (n
== 0 && !isl_local_space_is_named_or_nested(aff
->ls
, type
))
2444 ctx
= isl_aff_get_ctx(aff
);
2445 if (first
+ n
> isl_local_space_dim(aff
->ls
, type
))
2446 isl_die(ctx
, isl_error_invalid
, "range out of bounds",
2447 return isl_aff_free(aff
));
2449 aff
= isl_aff_cow(aff
);
2453 aff
->ls
= isl_local_space_drop_dims(aff
->ls
, type
, first
, n
);
2455 return isl_aff_free(aff
);
2457 first
+= 1 + isl_local_space_offset(aff
->ls
, type
);
2458 aff
->v
= isl_vec_drop_els(aff
->v
, first
, n
);
2460 return isl_aff_free(aff
);
2465 /* Drop the "n" domain dimensions starting at "first" from "aff",
2466 * after checking that they do not appear in the affine expression.
2468 static __isl_give isl_aff
*drop_domain(__isl_take isl_aff
*aff
, unsigned first
,
2473 involves
= isl_aff_involves_dims(aff
, isl_dim_in
, first
, n
);
2475 return isl_aff_free(aff
);
2477 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2478 "affine expression involves some of the domain dimensions",
2479 return isl_aff_free(aff
));
2480 return isl_aff_drop_dims(aff
, isl_dim_in
, first
, n
);
2483 /* Project the domain of the affine expression onto its parameter space.
2484 * The affine expression may not involve any of the domain dimensions.
2486 __isl_give isl_aff
*isl_aff_project_domain_on_params(__isl_take isl_aff
*aff
)
2491 n
= isl_aff_dim(aff
, isl_dim_in
);
2492 aff
= drop_domain(aff
, 0, n
);
2493 space
= isl_aff_get_domain_space(aff
);
2494 space
= isl_space_params(space
);
2495 aff
= isl_aff_reset_domain_space(aff
, space
);
2499 /* Check that the domain of "aff" is a product.
2501 static isl_stat
check_domain_product(__isl_keep isl_aff
*aff
)
2503 isl_bool is_product
;
2505 is_product
= isl_space_is_product(isl_aff_peek_domain_space(aff
));
2507 return isl_stat_error
;
2509 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2510 "domain is not a product", return isl_stat_error
);
2514 /* Given an affine function with a domain of the form [A -> B] that
2515 * does not depend on B, return the same function on domain A.
2517 __isl_give isl_aff
*isl_aff_domain_factor_domain(__isl_take isl_aff
*aff
)
2522 if (check_domain_product(aff
) < 0)
2523 return isl_aff_free(aff
);
2524 space
= isl_aff_get_domain_space(aff
);
2525 n
= isl_space_dim(space
, isl_dim_set
);
2526 space
= isl_space_factor_domain(space
);
2527 n_in
= isl_space_dim(space
, isl_dim_set
);
2528 aff
= drop_domain(aff
, n_in
, n
- n_in
);
2529 aff
= isl_aff_reset_domain_space(aff
, space
);
2533 /* Convert an affine expression defined over a parameter domain
2534 * into one that is defined over a zero-dimensional set.
2536 __isl_give isl_aff
*isl_aff_from_range(__isl_take isl_aff
*aff
)
2538 isl_local_space
*ls
;
2540 ls
= isl_aff_take_domain_local_space(aff
);
2541 ls
= isl_local_space_set_from_params(ls
);
2542 aff
= isl_aff_restore_domain_local_space(aff
, ls
);
2547 __isl_give isl_aff
*isl_aff_insert_dims(__isl_take isl_aff
*aff
,
2548 enum isl_dim_type type
, unsigned first
, unsigned n
)
2554 if (type
== isl_dim_out
)
2555 isl_die(aff
->v
->ctx
, isl_error_invalid
,
2556 "cannot insert output/set dimensions",
2557 return isl_aff_free(aff
));
2558 if (type
== isl_dim_in
)
2560 if (n
== 0 && !isl_local_space_is_named_or_nested(aff
->ls
, type
))
2563 ctx
= isl_aff_get_ctx(aff
);
2564 if (first
> isl_local_space_dim(aff
->ls
, type
))
2565 isl_die(ctx
, isl_error_invalid
, "position out of bounds",
2566 return isl_aff_free(aff
));
2568 aff
= isl_aff_cow(aff
);
2572 aff
->ls
= isl_local_space_insert_dims(aff
->ls
, type
, first
, n
);
2574 return isl_aff_free(aff
);
2576 first
+= 1 + isl_local_space_offset(aff
->ls
, type
);
2577 aff
->v
= isl_vec_insert_zero_els(aff
->v
, first
, n
);
2579 return isl_aff_free(aff
);
2584 __isl_give isl_aff
*isl_aff_add_dims(__isl_take isl_aff
*aff
,
2585 enum isl_dim_type type
, unsigned n
)
2589 pos
= isl_aff_dim(aff
, type
);
2591 return isl_aff_insert_dims(aff
, type
, pos
, n
);
2594 __isl_give isl_pw_aff
*isl_pw_aff_add_dims(__isl_take isl_pw_aff
*pwaff
,
2595 enum isl_dim_type type
, unsigned n
)
2599 pos
= isl_pw_aff_dim(pwaff
, type
);
2601 return isl_pw_aff_insert_dims(pwaff
, type
, pos
, n
);
2604 /* Move the "n" dimensions of "src_type" starting at "src_pos" of "aff"
2605 * to dimensions of "dst_type" at "dst_pos".
2607 * We only support moving input dimensions to parameters and vice versa.
2609 __isl_give isl_aff
*isl_aff_move_dims(__isl_take isl_aff
*aff
,
2610 enum isl_dim_type dst_type
, unsigned dst_pos
,
2611 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
2619 !isl_local_space_is_named_or_nested(aff
->ls
, src_type
) &&
2620 !isl_local_space_is_named_or_nested(aff
->ls
, dst_type
))
2623 if (dst_type
== isl_dim_out
|| src_type
== isl_dim_out
)
2624 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2625 "cannot move output/set dimension",
2626 return isl_aff_free(aff
));
2627 if (dst_type
== isl_dim_div
|| src_type
== isl_dim_div
)
2628 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2629 "cannot move divs", return isl_aff_free(aff
));
2630 if (dst_type
== isl_dim_in
)
2631 dst_type
= isl_dim_set
;
2632 if (src_type
== isl_dim_in
)
2633 src_type
= isl_dim_set
;
2635 if (src_pos
+ n
> isl_local_space_dim(aff
->ls
, src_type
))
2636 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2637 "range out of bounds", return isl_aff_free(aff
));
2638 if (dst_type
== src_type
)
2639 isl_die(isl_aff_get_ctx(aff
), isl_error_unsupported
,
2640 "moving dims within the same type not supported",
2641 return isl_aff_free(aff
));
2643 aff
= isl_aff_cow(aff
);
2647 g_src_pos
= 1 + isl_local_space_offset(aff
->ls
, src_type
) + src_pos
;
2648 g_dst_pos
= 1 + isl_local_space_offset(aff
->ls
, dst_type
) + dst_pos
;
2649 if (dst_type
> src_type
)
2652 aff
->v
= isl_vec_move_els(aff
->v
, g_dst_pos
, g_src_pos
, n
);
2653 aff
->ls
= isl_local_space_move_dims(aff
->ls
, dst_type
, dst_pos
,
2654 src_type
, src_pos
, n
);
2655 if (!aff
->v
|| !aff
->ls
)
2656 return isl_aff_free(aff
);
2658 aff
= sort_divs(aff
);
2663 __isl_give isl_pw_aff
*isl_pw_aff_from_aff(__isl_take isl_aff
*aff
)
2665 isl_set
*dom
= isl_set_universe(isl_aff_get_domain_space(aff
));
2666 return isl_pw_aff_alloc(dom
, aff
);
2669 #define isl_aff_involves_nan isl_aff_is_nan
2672 #define PW isl_pw_aff
2676 #define EL_IS_ZERO is_empty
2680 #define IS_ZERO is_empty
2683 #undef DEFAULT_IS_ZERO
2684 #define DEFAULT_IS_ZERO 0
2690 #include <isl_pw_templ.c>
2691 #include <isl_pw_eval.c>
2692 #include <isl_pw_hash.c>
2693 #include <isl_pw_union_opt.c>
2696 #define UNION isl_union_pw_aff
2698 #define PART isl_pw_aff
2700 #define PARTS pw_aff
2702 #include <isl_union_single.c>
2703 #include <isl_union_neg.c>
2705 static __isl_give isl_set
*align_params_pw_pw_set_and(
2706 __isl_take isl_pw_aff
*pwaff1
, __isl_take isl_pw_aff
*pwaff2
,
2707 __isl_give isl_set
*(*fn
)(__isl_take isl_pw_aff
*pwaff1
,
2708 __isl_take isl_pw_aff
*pwaff2
))
2710 isl_bool equal_params
;
2712 if (!pwaff1
|| !pwaff2
)
2714 equal_params
= isl_space_has_equal_params(pwaff1
->dim
, pwaff2
->dim
);
2715 if (equal_params
< 0)
2718 return fn(pwaff1
, pwaff2
);
2719 if (isl_pw_aff_check_named_params(pwaff1
) < 0 ||
2720 isl_pw_aff_check_named_params(pwaff2
) < 0)
2722 pwaff1
= isl_pw_aff_align_params(pwaff1
, isl_pw_aff_get_space(pwaff2
));
2723 pwaff2
= isl_pw_aff_align_params(pwaff2
, isl_pw_aff_get_space(pwaff1
));
2724 return fn(pwaff1
, pwaff2
);
2726 isl_pw_aff_free(pwaff1
);
2727 isl_pw_aff_free(pwaff2
);
2731 /* Align the parameters of the to isl_pw_aff arguments and
2732 * then apply a function "fn" on them that returns an isl_map.
2734 static __isl_give isl_map
*align_params_pw_pw_map_and(
2735 __isl_take isl_pw_aff
*pa1
, __isl_take isl_pw_aff
*pa2
,
2736 __isl_give isl_map
*(*fn
)(__isl_take isl_pw_aff
*pa1
,
2737 __isl_take isl_pw_aff
*pa2
))
2739 isl_bool equal_params
;
2743 equal_params
= isl_space_has_equal_params(pa1
->dim
, pa2
->dim
);
2744 if (equal_params
< 0)
2747 return fn(pa1
, pa2
);
2748 if (isl_pw_aff_check_named_params(pa1
) < 0 ||
2749 isl_pw_aff_check_named_params(pa2
) < 0)
2751 pa1
= isl_pw_aff_align_params(pa1
, isl_pw_aff_get_space(pa2
));
2752 pa2
= isl_pw_aff_align_params(pa2
, isl_pw_aff_get_space(pa1
));
2753 return fn(pa1
, pa2
);
2755 isl_pw_aff_free(pa1
);
2756 isl_pw_aff_free(pa2
);
2760 /* Compute a piecewise quasi-affine expression with a domain that
2761 * is the union of those of pwaff1 and pwaff2 and such that on each
2762 * cell, the quasi-affine expression is the maximum of those of pwaff1
2763 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2764 * cell, then the associated expression is the defined one.
2766 static __isl_give isl_pw_aff
*pw_aff_union_max(__isl_take isl_pw_aff
*pwaff1
,
2767 __isl_take isl_pw_aff
*pwaff2
)
2769 return isl_pw_aff_union_opt_cmp(pwaff1
, pwaff2
, &isl_aff_ge_set
);
2772 __isl_give isl_pw_aff
*isl_pw_aff_union_max(__isl_take isl_pw_aff
*pwaff1
,
2773 __isl_take isl_pw_aff
*pwaff2
)
2775 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
,
2779 /* Compute a piecewise quasi-affine expression with a domain that
2780 * is the union of those of pwaff1 and pwaff2 and such that on each
2781 * cell, the quasi-affine expression is the minimum of those of pwaff1
2782 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2783 * cell, then the associated expression is the defined one.
2785 static __isl_give isl_pw_aff
*pw_aff_union_min(__isl_take isl_pw_aff
*pwaff1
,
2786 __isl_take isl_pw_aff
*pwaff2
)
2788 return isl_pw_aff_union_opt_cmp(pwaff1
, pwaff2
, &isl_aff_le_set
);
2791 __isl_give isl_pw_aff
*isl_pw_aff_union_min(__isl_take isl_pw_aff
*pwaff1
,
2792 __isl_take isl_pw_aff
*pwaff2
)
2794 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
,
2798 __isl_give isl_pw_aff
*isl_pw_aff_union_opt(__isl_take isl_pw_aff
*pwaff1
,
2799 __isl_take isl_pw_aff
*pwaff2
, int max
)
2802 return isl_pw_aff_union_max(pwaff1
, pwaff2
);
2804 return isl_pw_aff_union_min(pwaff1
, pwaff2
);
2807 /* Construct a map with as domain the domain of pwaff and
2808 * one-dimensional range corresponding to the affine expressions.
2810 static __isl_give isl_map
*map_from_pw_aff(__isl_take isl_pw_aff
*pwaff
)
2819 dim
= isl_pw_aff_get_space(pwaff
);
2820 map
= isl_map_empty(dim
);
2822 for (i
= 0; i
< pwaff
->n
; ++i
) {
2823 isl_basic_map
*bmap
;
2826 bmap
= isl_basic_map_from_aff(isl_aff_copy(pwaff
->p
[i
].aff
));
2827 map_i
= isl_map_from_basic_map(bmap
);
2828 map_i
= isl_map_intersect_domain(map_i
,
2829 isl_set_copy(pwaff
->p
[i
].set
));
2830 map
= isl_map_union_disjoint(map
, map_i
);
2833 isl_pw_aff_free(pwaff
);
2838 /* Construct a map with as domain the domain of pwaff and
2839 * one-dimensional range corresponding to the affine expressions.
2841 __isl_give isl_map
*isl_map_from_pw_aff(__isl_take isl_pw_aff
*pwaff
)
2845 if (isl_space_is_set(pwaff
->dim
))
2846 isl_die(isl_pw_aff_get_ctx(pwaff
), isl_error_invalid
,
2847 "space of input is not a map", goto error
);
2848 return map_from_pw_aff(pwaff
);
2850 isl_pw_aff_free(pwaff
);
2854 /* Construct a one-dimensional set with as parameter domain
2855 * the domain of pwaff and the single set dimension
2856 * corresponding to the affine expressions.
2858 __isl_give isl_set
*isl_set_from_pw_aff(__isl_take isl_pw_aff
*pwaff
)
2862 if (!isl_space_is_set(pwaff
->dim
))
2863 isl_die(isl_pw_aff_get_ctx(pwaff
), isl_error_invalid
,
2864 "space of input is not a set", goto error
);
2865 return map_from_pw_aff(pwaff
);
2867 isl_pw_aff_free(pwaff
);
2871 /* Return a set containing those elements in the domain
2872 * of "pwaff" where it satisfies "fn" (if complement is 0) or
2873 * does not satisfy "fn" (if complement is 1).
2875 * The pieces with a NaN never belong to the result since
2876 * NaN does not satisfy any property.
2878 static __isl_give isl_set
*pw_aff_locus(__isl_take isl_pw_aff
*pwaff
,
2879 __isl_give isl_basic_set
*(*fn
)(__isl_take isl_aff
*aff
, int rational
),
2888 set
= isl_set_empty(isl_pw_aff_get_domain_space(pwaff
));
2890 for (i
= 0; i
< pwaff
->n
; ++i
) {
2891 isl_basic_set
*bset
;
2892 isl_set
*set_i
, *locus
;
2895 if (isl_aff_is_nan(pwaff
->p
[i
].aff
))
2898 rational
= isl_set_has_rational(pwaff
->p
[i
].set
);
2899 bset
= fn(isl_aff_copy(pwaff
->p
[i
].aff
), rational
);
2900 locus
= isl_set_from_basic_set(bset
);
2901 set_i
= isl_set_copy(pwaff
->p
[i
].set
);
2903 set_i
= isl_set_subtract(set_i
, locus
);
2905 set_i
= isl_set_intersect(set_i
, locus
);
2906 set
= isl_set_union_disjoint(set
, set_i
);
2909 isl_pw_aff_free(pwaff
);
2914 /* Return a set containing those elements in the domain
2915 * of "pa" where it is positive.
2917 __isl_give isl_set
*isl_pw_aff_pos_set(__isl_take isl_pw_aff
*pa
)
2919 return pw_aff_locus(pa
, &aff_pos_basic_set
, 0);
2922 /* Return a set containing those elements in the domain
2923 * of pwaff where it is non-negative.
2925 __isl_give isl_set
*isl_pw_aff_nonneg_set(__isl_take isl_pw_aff
*pwaff
)
2927 return pw_aff_locus(pwaff
, &aff_nonneg_basic_set
, 0);
2930 /* Return a set containing those elements in the domain
2931 * of pwaff where it is zero.
2933 __isl_give isl_set
*isl_pw_aff_zero_set(__isl_take isl_pw_aff
*pwaff
)
2935 return pw_aff_locus(pwaff
, &aff_zero_basic_set
, 0);
2938 /* Return a set containing those elements in the domain
2939 * of pwaff where it is not zero.
2941 __isl_give isl_set
*isl_pw_aff_non_zero_set(__isl_take isl_pw_aff
*pwaff
)
2943 return pw_aff_locus(pwaff
, &aff_zero_basic_set
, 1);
2946 /* Return a set containing those elements in the shared domain
2947 * of pwaff1 and pwaff2 where pwaff1 is greater than (or equal) to pwaff2.
2949 * We compute the difference on the shared domain and then construct
2950 * the set of values where this difference is non-negative.
2951 * If strict is set, we first subtract 1 from the difference.
2952 * If equal is set, we only return the elements where pwaff1 and pwaff2
2955 static __isl_give isl_set
*pw_aff_gte_set(__isl_take isl_pw_aff
*pwaff1
,
2956 __isl_take isl_pw_aff
*pwaff2
, int strict
, int equal
)
2958 isl_set
*set1
, *set2
;
2960 set1
= isl_pw_aff_domain(isl_pw_aff_copy(pwaff1
));
2961 set2
= isl_pw_aff_domain(isl_pw_aff_copy(pwaff2
));
2962 set1
= isl_set_intersect(set1
, set2
);
2963 pwaff1
= isl_pw_aff_intersect_domain(pwaff1
, isl_set_copy(set1
));
2964 pwaff2
= isl_pw_aff_intersect_domain(pwaff2
, isl_set_copy(set1
));
2965 pwaff1
= isl_pw_aff_add(pwaff1
, isl_pw_aff_neg(pwaff2
));
2968 isl_space
*dim
= isl_set_get_space(set1
);
2970 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(dim
));
2971 aff
= isl_aff_add_constant_si(aff
, -1);
2972 pwaff1
= isl_pw_aff_add(pwaff1
, isl_pw_aff_alloc(set1
, aff
));
2977 return isl_pw_aff_zero_set(pwaff1
);
2978 return isl_pw_aff_nonneg_set(pwaff1
);
2981 /* Return a set containing those elements in the shared domain
2982 * of pwaff1 and pwaff2 where pwaff1 is equal to pwaff2.
2984 static __isl_give isl_set
*pw_aff_eq_set(__isl_take isl_pw_aff
*pwaff1
,
2985 __isl_take isl_pw_aff
*pwaff2
)
2987 return pw_aff_gte_set(pwaff1
, pwaff2
, 0, 1);
2990 __isl_give isl_set
*isl_pw_aff_eq_set(__isl_take isl_pw_aff
*pwaff1
,
2991 __isl_take isl_pw_aff
*pwaff2
)
2993 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_eq_set
);
2996 /* Return a set containing those elements in the shared domain
2997 * of pwaff1 and pwaff2 where pwaff1 is greater than or equal to pwaff2.
2999 static __isl_give isl_set
*pw_aff_ge_set(__isl_take isl_pw_aff
*pwaff1
,
3000 __isl_take isl_pw_aff
*pwaff2
)
3002 return pw_aff_gte_set(pwaff1
, pwaff2
, 0, 0);
3005 __isl_give isl_set
*isl_pw_aff_ge_set(__isl_take isl_pw_aff
*pwaff1
,
3006 __isl_take isl_pw_aff
*pwaff2
)
3008 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_ge_set
);
3011 /* Return a set containing those elements in the shared domain
3012 * of pwaff1 and pwaff2 where pwaff1 is strictly greater than pwaff2.
3014 static __isl_give isl_set
*pw_aff_gt_set(__isl_take isl_pw_aff
*pwaff1
,
3015 __isl_take isl_pw_aff
*pwaff2
)
3017 return pw_aff_gte_set(pwaff1
, pwaff2
, 1, 0);
3020 __isl_give isl_set
*isl_pw_aff_gt_set(__isl_take isl_pw_aff
*pwaff1
,
3021 __isl_take isl_pw_aff
*pwaff2
)
3023 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_gt_set
);
3026 __isl_give isl_set
*isl_pw_aff_le_set(__isl_take isl_pw_aff
*pwaff1
,
3027 __isl_take isl_pw_aff
*pwaff2
)
3029 return isl_pw_aff_ge_set(pwaff2
, pwaff1
);
3032 __isl_give isl_set
*isl_pw_aff_lt_set(__isl_take isl_pw_aff
*pwaff1
,
3033 __isl_take isl_pw_aff
*pwaff2
)
3035 return isl_pw_aff_gt_set(pwaff2
, pwaff1
);
3038 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3039 * where the function values are ordered in the same way as "order",
3040 * which returns a set in the shared domain of its two arguments.
3041 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3043 * Let "pa1" and "pa2" be defined on domains A and B respectively.
3044 * We first pull back the two functions such that they are defined on
3045 * the domain [A -> B]. Then we apply "order", resulting in a set
3046 * in the space [A -> B]. Finally, we unwrap this set to obtain
3047 * a map in the space A -> B.
3049 static __isl_give isl_map
*isl_pw_aff_order_map_aligned(
3050 __isl_take isl_pw_aff
*pa1
, __isl_take isl_pw_aff
*pa2
,
3051 __isl_give isl_set
*(*order
)(__isl_take isl_pw_aff
*pa1
,
3052 __isl_take isl_pw_aff
*pa2
))
3054 isl_space
*space1
, *space2
;
3058 space1
= isl_space_domain(isl_pw_aff_get_space(pa1
));
3059 space2
= isl_space_domain(isl_pw_aff_get_space(pa2
));
3060 space1
= isl_space_map_from_domain_and_range(space1
, space2
);
3061 ma
= isl_multi_aff_domain_map(isl_space_copy(space1
));
3062 pa1
= isl_pw_aff_pullback_multi_aff(pa1
, ma
);
3063 ma
= isl_multi_aff_range_map(space1
);
3064 pa2
= isl_pw_aff_pullback_multi_aff(pa2
, ma
);
3065 set
= order(pa1
, pa2
);
3067 return isl_set_unwrap(set
);
3070 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3071 * where the function values are equal.
3072 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3074 static __isl_give isl_map
*isl_pw_aff_eq_map_aligned(__isl_take isl_pw_aff
*pa1
,
3075 __isl_take isl_pw_aff
*pa2
)
3077 return isl_pw_aff_order_map_aligned(pa1
, pa2
, &isl_pw_aff_eq_set
);
3080 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3081 * where the function values are equal.
3083 __isl_give isl_map
*isl_pw_aff_eq_map(__isl_take isl_pw_aff
*pa1
,
3084 __isl_take isl_pw_aff
*pa2
)
3086 return align_params_pw_pw_map_and(pa1
, pa2
, &isl_pw_aff_eq_map_aligned
);
3089 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3090 * where the function value of "pa1" is less than the function value of "pa2".
3091 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3093 static __isl_give isl_map
*isl_pw_aff_lt_map_aligned(__isl_take isl_pw_aff
*pa1
,
3094 __isl_take isl_pw_aff
*pa2
)
3096 return isl_pw_aff_order_map_aligned(pa1
, pa2
, &isl_pw_aff_lt_set
);
3099 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3100 * where the function value of "pa1" is less than the function value of "pa2".
3102 __isl_give isl_map
*isl_pw_aff_lt_map(__isl_take isl_pw_aff
*pa1
,
3103 __isl_take isl_pw_aff
*pa2
)
3105 return align_params_pw_pw_map_and(pa1
, pa2
, &isl_pw_aff_lt_map_aligned
);
3108 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3109 * where the function value of "pa1" is greater than the function value
3111 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3113 static __isl_give isl_map
*isl_pw_aff_gt_map_aligned(__isl_take isl_pw_aff
*pa1
,
3114 __isl_take isl_pw_aff
*pa2
)
3116 return isl_pw_aff_order_map_aligned(pa1
, pa2
, &isl_pw_aff_gt_set
);
3119 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3120 * where the function value of "pa1" is greater than the function value
3123 __isl_give isl_map
*isl_pw_aff_gt_map(__isl_take isl_pw_aff
*pa1
,
3124 __isl_take isl_pw_aff
*pa2
)
3126 return align_params_pw_pw_map_and(pa1
, pa2
, &isl_pw_aff_gt_map_aligned
);
3129 /* Return a set containing those elements in the shared domain
3130 * of the elements of list1 and list2 where each element in list1
3131 * has the relation specified by "fn" with each element in list2.
3133 static __isl_give isl_set
*pw_aff_list_set(__isl_take isl_pw_aff_list
*list1
,
3134 __isl_take isl_pw_aff_list
*list2
,
3135 __isl_give isl_set
*(*fn
)(__isl_take isl_pw_aff
*pwaff1
,
3136 __isl_take isl_pw_aff
*pwaff2
))
3142 if (!list1
|| !list2
)
3145 ctx
= isl_pw_aff_list_get_ctx(list1
);
3146 if (list1
->n
< 1 || list2
->n
< 1)
3147 isl_die(ctx
, isl_error_invalid
,
3148 "list should contain at least one element", goto error
);
3150 set
= isl_set_universe(isl_pw_aff_get_domain_space(list1
->p
[0]));
3151 for (i
= 0; i
< list1
->n
; ++i
)
3152 for (j
= 0; j
< list2
->n
; ++j
) {
3155 set_ij
= fn(isl_pw_aff_copy(list1
->p
[i
]),
3156 isl_pw_aff_copy(list2
->p
[j
]));
3157 set
= isl_set_intersect(set
, set_ij
);
3160 isl_pw_aff_list_free(list1
);
3161 isl_pw_aff_list_free(list2
);
3164 isl_pw_aff_list_free(list1
);
3165 isl_pw_aff_list_free(list2
);
3169 /* Return a set containing those elements in the shared domain
3170 * of the elements of list1 and list2 where each element in list1
3171 * is equal to each element in list2.
3173 __isl_give isl_set
*isl_pw_aff_list_eq_set(__isl_take isl_pw_aff_list
*list1
,
3174 __isl_take isl_pw_aff_list
*list2
)
3176 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_eq_set
);
3179 __isl_give isl_set
*isl_pw_aff_list_ne_set(__isl_take isl_pw_aff_list
*list1
,
3180 __isl_take isl_pw_aff_list
*list2
)
3182 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_ne_set
);
3185 /* Return a set containing those elements in the shared domain
3186 * of the elements of list1 and list2 where each element in list1
3187 * is less than or equal to each element in list2.
3189 __isl_give isl_set
*isl_pw_aff_list_le_set(__isl_take isl_pw_aff_list
*list1
,
3190 __isl_take isl_pw_aff_list
*list2
)
3192 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_le_set
);
3195 __isl_give isl_set
*isl_pw_aff_list_lt_set(__isl_take isl_pw_aff_list
*list1
,
3196 __isl_take isl_pw_aff_list
*list2
)
3198 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_lt_set
);
3201 __isl_give isl_set
*isl_pw_aff_list_ge_set(__isl_take isl_pw_aff_list
*list1
,
3202 __isl_take isl_pw_aff_list
*list2
)
3204 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_ge_set
);
3207 __isl_give isl_set
*isl_pw_aff_list_gt_set(__isl_take isl_pw_aff_list
*list1
,
3208 __isl_take isl_pw_aff_list
*list2
)
3210 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_gt_set
);
3214 /* Return a set containing those elements in the shared domain
3215 * of pwaff1 and pwaff2 where pwaff1 is not equal to pwaff2.
3217 static __isl_give isl_set
*pw_aff_ne_set(__isl_take isl_pw_aff
*pwaff1
,
3218 __isl_take isl_pw_aff
*pwaff2
)
3220 isl_set
*set_lt
, *set_gt
;
3222 set_lt
= isl_pw_aff_lt_set(isl_pw_aff_copy(pwaff1
),
3223 isl_pw_aff_copy(pwaff2
));
3224 set_gt
= isl_pw_aff_gt_set(pwaff1
, pwaff2
);
3225 return isl_set_union_disjoint(set_lt
, set_gt
);
3228 __isl_give isl_set
*isl_pw_aff_ne_set(__isl_take isl_pw_aff
*pwaff1
,
3229 __isl_take isl_pw_aff
*pwaff2
)
3231 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_ne_set
);
3234 __isl_give isl_pw_aff
*isl_pw_aff_scale_down(__isl_take isl_pw_aff
*pwaff
,
3239 if (isl_int_is_one(v
))
3241 if (!isl_int_is_pos(v
))
3242 isl_die(isl_pw_aff_get_ctx(pwaff
), isl_error_invalid
,
3243 "factor needs to be positive",
3244 return isl_pw_aff_free(pwaff
));
3245 pwaff
= isl_pw_aff_cow(pwaff
);
3251 for (i
= 0; i
< pwaff
->n
; ++i
) {
3252 pwaff
->p
[i
].aff
= isl_aff_scale_down(pwaff
->p
[i
].aff
, v
);
3253 if (!pwaff
->p
[i
].aff
)
3254 return isl_pw_aff_free(pwaff
);
3260 __isl_give isl_pw_aff
*isl_pw_aff_floor(__isl_take isl_pw_aff
*pwaff
)
3264 pwaff
= isl_pw_aff_cow(pwaff
);
3270 for (i
= 0; i
< pwaff
->n
; ++i
) {
3271 pwaff
->p
[i
].aff
= isl_aff_floor(pwaff
->p
[i
].aff
);
3272 if (!pwaff
->p
[i
].aff
)
3273 return isl_pw_aff_free(pwaff
);
3279 __isl_give isl_pw_aff
*isl_pw_aff_ceil(__isl_take isl_pw_aff
*pwaff
)
3283 pwaff
= isl_pw_aff_cow(pwaff
);
3289 for (i
= 0; i
< pwaff
->n
; ++i
) {
3290 pwaff
->p
[i
].aff
= isl_aff_ceil(pwaff
->p
[i
].aff
);
3291 if (!pwaff
->p
[i
].aff
)
3292 return isl_pw_aff_free(pwaff
);
3298 /* Assuming that "cond1" and "cond2" are disjoint,
3299 * return an affine expression that is equal to pwaff1 on cond1
3300 * and to pwaff2 on cond2.
3302 static __isl_give isl_pw_aff
*isl_pw_aff_select(
3303 __isl_take isl_set
*cond1
, __isl_take isl_pw_aff
*pwaff1
,
3304 __isl_take isl_set
*cond2
, __isl_take isl_pw_aff
*pwaff2
)
3306 pwaff1
= isl_pw_aff_intersect_domain(pwaff1
, cond1
);
3307 pwaff2
= isl_pw_aff_intersect_domain(pwaff2
, cond2
);
3309 return isl_pw_aff_add_disjoint(pwaff1
, pwaff2
);
3312 /* Return an affine expression that is equal to pwaff_true for elements
3313 * where "cond" is non-zero and to pwaff_false for elements where "cond"
3315 * That is, return cond ? pwaff_true : pwaff_false;
3317 * If "cond" involves and NaN, then we conservatively return a NaN
3318 * on its entire domain. In principle, we could consider the pieces
3319 * where it is NaN separately from those where it is not.
3321 * If "pwaff_true" and "pwaff_false" are obviously equal to each other,
3322 * then only use the domain of "cond" to restrict the domain.
3324 __isl_give isl_pw_aff
*isl_pw_aff_cond(__isl_take isl_pw_aff
*cond
,
3325 __isl_take isl_pw_aff
*pwaff_true
, __isl_take isl_pw_aff
*pwaff_false
)
3327 isl_set
*cond_true
, *cond_false
;
3332 if (isl_pw_aff_involves_nan(cond
)) {
3333 isl_space
*space
= isl_pw_aff_get_domain_space(cond
);
3334 isl_local_space
*ls
= isl_local_space_from_space(space
);
3335 isl_pw_aff_free(cond
);
3336 isl_pw_aff_free(pwaff_true
);
3337 isl_pw_aff_free(pwaff_false
);
3338 return isl_pw_aff_nan_on_domain(ls
);
3341 pwaff_true
= isl_pw_aff_align_params(pwaff_true
,
3342 isl_pw_aff_get_space(pwaff_false
));
3343 pwaff_false
= isl_pw_aff_align_params(pwaff_false
,
3344 isl_pw_aff_get_space(pwaff_true
));
3345 equal
= isl_pw_aff_plain_is_equal(pwaff_true
, pwaff_false
);
3351 dom
= isl_set_coalesce(isl_pw_aff_domain(cond
));
3352 isl_pw_aff_free(pwaff_false
);
3353 return isl_pw_aff_intersect_domain(pwaff_true
, dom
);
3356 cond_true
= isl_pw_aff_non_zero_set(isl_pw_aff_copy(cond
));
3357 cond_false
= isl_pw_aff_zero_set(cond
);
3358 return isl_pw_aff_select(cond_true
, pwaff_true
,
3359 cond_false
, pwaff_false
);
3361 isl_pw_aff_free(cond
);
3362 isl_pw_aff_free(pwaff_true
);
3363 isl_pw_aff_free(pwaff_false
);
3367 isl_bool
isl_aff_is_cst(__isl_keep isl_aff
*aff
)
3370 return isl_bool_error
;
3372 return isl_seq_first_non_zero(aff
->v
->el
+ 2, aff
->v
->size
- 2) == -1;
3375 /* Check whether pwaff is a piecewise constant.
3377 isl_bool
isl_pw_aff_is_cst(__isl_keep isl_pw_aff
*pwaff
)
3382 return isl_bool_error
;
3384 for (i
= 0; i
< pwaff
->n
; ++i
) {
3385 isl_bool is_cst
= isl_aff_is_cst(pwaff
->p
[i
].aff
);
3386 if (is_cst
< 0 || !is_cst
)
3390 return isl_bool_true
;
3393 /* Are all elements of "mpa" piecewise constants?
3395 isl_bool
isl_multi_pw_aff_is_cst(__isl_keep isl_multi_pw_aff
*mpa
)
3400 return isl_bool_error
;
3402 for (i
= 0; i
< mpa
->n
; ++i
) {
3403 isl_bool is_cst
= isl_pw_aff_is_cst(mpa
->u
.p
[i
]);
3404 if (is_cst
< 0 || !is_cst
)
3408 return isl_bool_true
;
3411 /* Return the product of "aff1" and "aff2".
3413 * If either of the two is NaN, then the result is NaN.
3415 * Otherwise, at least one of "aff1" or "aff2" needs to be a constant.
3417 __isl_give isl_aff
*isl_aff_mul(__isl_take isl_aff
*aff1
,
3418 __isl_take isl_aff
*aff2
)
3423 if (isl_aff_is_nan(aff1
)) {
3427 if (isl_aff_is_nan(aff2
)) {
3432 if (!isl_aff_is_cst(aff2
) && isl_aff_is_cst(aff1
))
3433 return isl_aff_mul(aff2
, aff1
);
3435 if (!isl_aff_is_cst(aff2
))
3436 isl_die(isl_aff_get_ctx(aff1
), isl_error_invalid
,
3437 "at least one affine expression should be constant",
3440 aff1
= isl_aff_cow(aff1
);
3444 aff1
= isl_aff_scale(aff1
, aff2
->v
->el
[1]);
3445 aff1
= isl_aff_scale_down(aff1
, aff2
->v
->el
[0]);
3455 /* Divide "aff1" by "aff2", assuming "aff2" is a constant.
3457 * If either of the two is NaN, then the result is NaN.
3459 __isl_give isl_aff
*isl_aff_div(__isl_take isl_aff
*aff1
,
3460 __isl_take isl_aff
*aff2
)
3468 if (isl_aff_is_nan(aff1
)) {
3472 if (isl_aff_is_nan(aff2
)) {
3477 is_cst
= isl_aff_is_cst(aff2
);
3481 isl_die(isl_aff_get_ctx(aff2
), isl_error_invalid
,
3482 "second argument should be a constant", goto error
);
3487 neg
= isl_int_is_neg(aff2
->v
->el
[1]);
3489 isl_int_neg(aff2
->v
->el
[0], aff2
->v
->el
[0]);
3490 isl_int_neg(aff2
->v
->el
[1], aff2
->v
->el
[1]);
3493 aff1
= isl_aff_scale(aff1
, aff2
->v
->el
[0]);
3494 aff1
= isl_aff_scale_down(aff1
, aff2
->v
->el
[1]);
3497 isl_int_neg(aff2
->v
->el
[0], aff2
->v
->el
[0]);
3498 isl_int_neg(aff2
->v
->el
[1], aff2
->v
->el
[1]);
3509 static __isl_give isl_pw_aff
*pw_aff_add(__isl_take isl_pw_aff
*pwaff1
,
3510 __isl_take isl_pw_aff
*pwaff2
)
3512 return isl_pw_aff_on_shared_domain(pwaff1
, pwaff2
, &isl_aff_add
);
3515 __isl_give isl_pw_aff
*isl_pw_aff_add(__isl_take isl_pw_aff
*pwaff1
,
3516 __isl_take isl_pw_aff
*pwaff2
)
3518 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
, &pw_aff_add
);
3521 __isl_give isl_pw_aff
*isl_pw_aff_union_add(__isl_take isl_pw_aff
*pwaff1
,
3522 __isl_take isl_pw_aff
*pwaff2
)
3524 return isl_pw_aff_union_add_(pwaff1
, pwaff2
);
3527 static __isl_give isl_pw_aff
*pw_aff_mul(__isl_take isl_pw_aff
*pwaff1
,
3528 __isl_take isl_pw_aff
*pwaff2
)
3530 return isl_pw_aff_on_shared_domain(pwaff1
, pwaff2
, &isl_aff_mul
);
3533 __isl_give isl_pw_aff
*isl_pw_aff_mul(__isl_take isl_pw_aff
*pwaff1
,
3534 __isl_take isl_pw_aff
*pwaff2
)
3536 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
, &pw_aff_mul
);
3539 static __isl_give isl_pw_aff
*pw_aff_div(__isl_take isl_pw_aff
*pa1
,
3540 __isl_take isl_pw_aff
*pa2
)
3542 return isl_pw_aff_on_shared_domain(pa1
, pa2
, &isl_aff_div
);
3545 /* Divide "pa1" by "pa2", assuming "pa2" is a piecewise constant.
3547 __isl_give isl_pw_aff
*isl_pw_aff_div(__isl_take isl_pw_aff
*pa1
,
3548 __isl_take isl_pw_aff
*pa2
)
3552 is_cst
= isl_pw_aff_is_cst(pa2
);
3556 isl_die(isl_pw_aff_get_ctx(pa2
), isl_error_invalid
,
3557 "second argument should be a piecewise constant",
3559 return isl_pw_aff_align_params_pw_pw_and(pa1
, pa2
, &pw_aff_div
);
3561 isl_pw_aff_free(pa1
);
3562 isl_pw_aff_free(pa2
);
3566 /* Compute the quotient of the integer division of "pa1" by "pa2"
3567 * with rounding towards zero.
3568 * "pa2" is assumed to be a piecewise constant.
3570 * In particular, return
3572 * pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2)
3575 __isl_give isl_pw_aff
*isl_pw_aff_tdiv_q(__isl_take isl_pw_aff
*pa1
,
3576 __isl_take isl_pw_aff
*pa2
)
3582 is_cst
= isl_pw_aff_is_cst(pa2
);
3586 isl_die(isl_pw_aff_get_ctx(pa2
), isl_error_invalid
,
3587 "second argument should be a piecewise constant",
3590 pa1
= isl_pw_aff_div(pa1
, pa2
);
3592 cond
= isl_pw_aff_nonneg_set(isl_pw_aff_copy(pa1
));
3593 f
= isl_pw_aff_floor(isl_pw_aff_copy(pa1
));
3594 c
= isl_pw_aff_ceil(pa1
);
3595 return isl_pw_aff_cond(isl_set_indicator_function(cond
), f
, c
);
3597 isl_pw_aff_free(pa1
);
3598 isl_pw_aff_free(pa2
);
3602 /* Compute the remainder of the integer division of "pa1" by "pa2"
3603 * with rounding towards zero.
3604 * "pa2" is assumed to be a piecewise constant.
3606 * In particular, return
3608 * pa1 - pa2 * (pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2))
3611 __isl_give isl_pw_aff
*isl_pw_aff_tdiv_r(__isl_take isl_pw_aff
*pa1
,
3612 __isl_take isl_pw_aff
*pa2
)
3617 is_cst
= isl_pw_aff_is_cst(pa2
);
3621 isl_die(isl_pw_aff_get_ctx(pa2
), isl_error_invalid
,
3622 "second argument should be a piecewise constant",
3624 res
= isl_pw_aff_tdiv_q(isl_pw_aff_copy(pa1
), isl_pw_aff_copy(pa2
));
3625 res
= isl_pw_aff_mul(pa2
, res
);
3626 res
= isl_pw_aff_sub(pa1
, res
);
3629 isl_pw_aff_free(pa1
);
3630 isl_pw_aff_free(pa2
);
3634 /* Does either of "pa1" or "pa2" involve any NaN2?
3636 static isl_bool
either_involves_nan(__isl_keep isl_pw_aff
*pa1
,
3637 __isl_keep isl_pw_aff
*pa2
)
3641 has_nan
= isl_pw_aff_involves_nan(pa1
);
3642 if (has_nan
< 0 || has_nan
)
3644 return isl_pw_aff_involves_nan(pa2
);
3647 /* Replace "pa1" and "pa2" (at least one of which involves a NaN)
3648 * by a NaN on their shared domain.
3650 * In principle, the result could be refined to only being NaN
3651 * on the parts of this domain where at least one of "pa1" or "pa2" is NaN.
3653 static __isl_give isl_pw_aff
*replace_by_nan(__isl_take isl_pw_aff
*pa1
,
3654 __isl_take isl_pw_aff
*pa2
)
3656 isl_local_space
*ls
;
3660 dom
= isl_set_intersect(isl_pw_aff_domain(pa1
), isl_pw_aff_domain(pa2
));
3661 ls
= isl_local_space_from_space(isl_set_get_space(dom
));
3662 pa
= isl_pw_aff_nan_on_domain(ls
);
3663 pa
= isl_pw_aff_intersect_domain(pa
, dom
);
3668 static __isl_give isl_pw_aff
*pw_aff_min(__isl_take isl_pw_aff
*pwaff1
,
3669 __isl_take isl_pw_aff
*pwaff2
)
3674 dom
= isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1
)),
3675 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2
)));
3676 le
= isl_pw_aff_le_set(isl_pw_aff_copy(pwaff1
),
3677 isl_pw_aff_copy(pwaff2
));
3678 dom
= isl_set_subtract(dom
, isl_set_copy(le
));
3679 return isl_pw_aff_select(le
, pwaff1
, dom
, pwaff2
);
3682 static __isl_give isl_pw_aff
*pw_aff_max(__isl_take isl_pw_aff
*pwaff1
,
3683 __isl_take isl_pw_aff
*pwaff2
)
3688 dom
= isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1
)),
3689 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2
)));
3690 ge
= isl_pw_aff_ge_set(isl_pw_aff_copy(pwaff1
),
3691 isl_pw_aff_copy(pwaff2
));
3692 dom
= isl_set_subtract(dom
, isl_set_copy(ge
));
3693 return isl_pw_aff_select(ge
, pwaff1
, dom
, pwaff2
);
3696 /* Return an expression for the minimum (if "max" is not set) or
3697 * the maximum (if "max" is set) of "pa1" and "pa2".
3698 * If either expression involves any NaN, then return a NaN
3699 * on the shared domain as result.
3701 static __isl_give isl_pw_aff
*pw_aff_min_max(__isl_take isl_pw_aff
*pa1
,
3702 __isl_take isl_pw_aff
*pa2
, int max
)
3706 has_nan
= either_involves_nan(pa1
, pa2
);
3708 pa1
= isl_pw_aff_free(pa1
);
3710 return replace_by_nan(pa1
, pa2
);
3713 return isl_pw_aff_align_params_pw_pw_and(pa1
, pa2
, &pw_aff_max
);
3715 return isl_pw_aff_align_params_pw_pw_and(pa1
, pa2
, &pw_aff_min
);
3718 /* Return an expression for the minimum of "pwaff1" and "pwaff2".
3720 __isl_give isl_pw_aff
*isl_pw_aff_min(__isl_take isl_pw_aff
*pwaff1
,
3721 __isl_take isl_pw_aff
*pwaff2
)
3723 return pw_aff_min_max(pwaff1
, pwaff2
, 0);
3726 /* Return an expression for the maximum of "pwaff1" and "pwaff2".
3728 __isl_give isl_pw_aff
*isl_pw_aff_max(__isl_take isl_pw_aff
*pwaff1
,
3729 __isl_take isl_pw_aff
*pwaff2
)
3731 return pw_aff_min_max(pwaff1
, pwaff2
, 1);
3734 static __isl_give isl_pw_aff
*pw_aff_list_reduce(
3735 __isl_take isl_pw_aff_list
*list
,
3736 __isl_give isl_pw_aff
*(*fn
)(__isl_take isl_pw_aff
*pwaff1
,
3737 __isl_take isl_pw_aff
*pwaff2
))
3746 ctx
= isl_pw_aff_list_get_ctx(list
);
3748 isl_die(ctx
, isl_error_invalid
,
3749 "list should contain at least one element", goto error
);
3751 res
= isl_pw_aff_copy(list
->p
[0]);
3752 for (i
= 1; i
< list
->n
; ++i
)
3753 res
= fn(res
, isl_pw_aff_copy(list
->p
[i
]));
3755 isl_pw_aff_list_free(list
);
3758 isl_pw_aff_list_free(list
);
3762 /* Return an isl_pw_aff that maps each element in the intersection of the
3763 * domains of the elements of list to the minimal corresponding affine
3766 __isl_give isl_pw_aff
*isl_pw_aff_list_min(__isl_take isl_pw_aff_list
*list
)
3768 return pw_aff_list_reduce(list
, &isl_pw_aff_min
);
3771 /* Return an isl_pw_aff that maps each element in the intersection of the
3772 * domains of the elements of list to the maximal corresponding affine
3775 __isl_give isl_pw_aff
*isl_pw_aff_list_max(__isl_take isl_pw_aff_list
*list
)
3777 return pw_aff_list_reduce(list
, &isl_pw_aff_max
);
3780 /* Mark the domains of "pwaff" as rational.
3782 __isl_give isl_pw_aff
*isl_pw_aff_set_rational(__isl_take isl_pw_aff
*pwaff
)
3786 pwaff
= isl_pw_aff_cow(pwaff
);
3792 for (i
= 0; i
< pwaff
->n
; ++i
) {
3793 pwaff
->p
[i
].set
= isl_set_set_rational(pwaff
->p
[i
].set
);
3794 if (!pwaff
->p
[i
].set
)
3795 return isl_pw_aff_free(pwaff
);
3801 /* Mark the domains of the elements of "list" as rational.
3803 __isl_give isl_pw_aff_list
*isl_pw_aff_list_set_rational(
3804 __isl_take isl_pw_aff_list
*list
)
3814 for (i
= 0; i
< n
; ++i
) {
3817 pa
= isl_pw_aff_list_get_pw_aff(list
, i
);
3818 pa
= isl_pw_aff_set_rational(pa
);
3819 list
= isl_pw_aff_list_set_pw_aff(list
, i
, pa
);
3825 /* Do the parameters of "aff" match those of "space"?
3827 isl_bool
isl_aff_matching_params(__isl_keep isl_aff
*aff
,
3828 __isl_keep isl_space
*space
)
3830 isl_space
*aff_space
;
3834 return isl_bool_error
;
3836 aff_space
= isl_aff_get_domain_space(aff
);
3838 match
= isl_space_has_equal_params(space
, aff_space
);
3840 isl_space_free(aff_space
);
3844 /* Check that the domain space of "aff" matches "space".
3846 isl_stat
isl_aff_check_match_domain_space(__isl_keep isl_aff
*aff
,
3847 __isl_keep isl_space
*space
)
3849 isl_space
*aff_space
;
3853 return isl_stat_error
;
3855 aff_space
= isl_aff_get_domain_space(aff
);
3857 match
= isl_space_has_equal_params(space
, aff_space
);
3861 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
3862 "parameters don't match", goto error
);
3863 match
= isl_space_tuple_is_equal(space
, isl_dim_in
,
3864 aff_space
, isl_dim_set
);
3868 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
3869 "domains don't match", goto error
);
3870 isl_space_free(aff_space
);
3873 isl_space_free(aff_space
);
3874 return isl_stat_error
;
3883 #include <isl_multi_no_explicit_domain.c>
3884 #include <isl_multi_templ.c>
3885 #include <isl_multi_apply_set.c>
3886 #include <isl_multi_cmp.c>
3887 #include <isl_multi_dims.c>
3888 #include <isl_multi_floor.c>
3889 #include <isl_multi_gist.c>
3893 /* Construct an isl_multi_aff living in "space" that corresponds
3894 * to the affine transformation matrix "mat".
3896 __isl_give isl_multi_aff
*isl_multi_aff_from_aff_mat(
3897 __isl_take isl_space
*space
, __isl_take isl_mat
*mat
)
3900 isl_local_space
*ls
= NULL
;
3901 isl_multi_aff
*ma
= NULL
;
3902 int n_row
, n_col
, n_out
, total
;
3908 ctx
= isl_mat_get_ctx(mat
);
3910 n_row
= isl_mat_rows(mat
);
3911 n_col
= isl_mat_cols(mat
);
3913 isl_die(ctx
, isl_error_invalid
,
3914 "insufficient number of rows", goto error
);
3916 isl_die(ctx
, isl_error_invalid
,
3917 "insufficient number of columns", goto error
);
3918 n_out
= isl_space_dim(space
, isl_dim_out
);
3919 total
= isl_space_dim(space
, isl_dim_all
);
3920 if (1 + n_out
!= n_row
|| 2 + total
!= n_row
+ n_col
)
3921 isl_die(ctx
, isl_error_invalid
,
3922 "dimension mismatch", goto error
);
3924 ma
= isl_multi_aff_zero(isl_space_copy(space
));
3925 ls
= isl_local_space_from_space(isl_space_domain(space
));
3927 for (i
= 0; i
< n_row
- 1; ++i
) {
3931 v
= isl_vec_alloc(ctx
, 1 + n_col
);
3934 isl_int_set(v
->el
[0], mat
->row
[0][0]);
3935 isl_seq_cpy(v
->el
+ 1, mat
->row
[1 + i
], n_col
);
3936 v
= isl_vec_normalize(v
);
3937 aff
= isl_aff_alloc_vec(isl_local_space_copy(ls
), v
);
3938 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
3941 isl_local_space_free(ls
);
3945 isl_local_space_free(ls
);
3947 isl_multi_aff_free(ma
);
3951 /* Remove any internal structure of the domain of "ma".
3952 * If there is any such internal structure in the input,
3953 * then the name of the corresponding space is also removed.
3955 __isl_give isl_multi_aff
*isl_multi_aff_flatten_domain(
3956 __isl_take isl_multi_aff
*ma
)
3963 if (!ma
->space
->nested
[0])
3966 space
= isl_multi_aff_get_space(ma
);
3967 space
= isl_space_flatten_domain(space
);
3968 ma
= isl_multi_aff_reset_space(ma
, space
);
3973 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
3974 * of the space to its domain.
3976 __isl_give isl_multi_aff
*isl_multi_aff_domain_map(__isl_take isl_space
*space
)
3979 isl_local_space
*ls
;
3984 if (!isl_space_is_map(space
))
3985 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
3986 "not a map space", goto error
);
3988 n_in
= isl_space_dim(space
, isl_dim_in
);
3989 space
= isl_space_domain_map(space
);
3991 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
3993 isl_space_free(space
);
3997 space
= isl_space_domain(space
);
3998 ls
= isl_local_space_from_space(space
);
3999 for (i
= 0; i
< n_in
; ++i
) {
4002 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
4004 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
4006 isl_local_space_free(ls
);
4009 isl_space_free(space
);
4013 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
4014 * of the space to its range.
4016 __isl_give isl_multi_aff
*isl_multi_aff_range_map(__isl_take isl_space
*space
)
4019 isl_local_space
*ls
;
4024 if (!isl_space_is_map(space
))
4025 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
4026 "not a map space", goto error
);
4028 n_in
= isl_space_dim(space
, isl_dim_in
);
4029 n_out
= isl_space_dim(space
, isl_dim_out
);
4030 space
= isl_space_range_map(space
);
4032 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
4034 isl_space_free(space
);
4038 space
= isl_space_domain(space
);
4039 ls
= isl_local_space_from_space(space
);
4040 for (i
= 0; i
< n_out
; ++i
) {
4043 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
4044 isl_dim_set
, n_in
+ i
);
4045 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
4047 isl_local_space_free(ls
);
4050 isl_space_free(space
);
4054 /* Given a map space, return an isl_pw_multi_aff that maps a wrapped copy
4055 * of the space to its range.
4057 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_range_map(
4058 __isl_take isl_space
*space
)
4060 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_range_map(space
));
4063 /* Given the space of a set and a range of set dimensions,
4064 * construct an isl_multi_aff that projects out those dimensions.
4066 __isl_give isl_multi_aff
*isl_multi_aff_project_out_map(
4067 __isl_take isl_space
*space
, enum isl_dim_type type
,
4068 unsigned first
, unsigned n
)
4071 isl_local_space
*ls
;
4076 if (!isl_space_is_set(space
))
4077 isl_die(isl_space_get_ctx(space
), isl_error_unsupported
,
4078 "expecting set space", goto error
);
4079 if (type
!= isl_dim_set
)
4080 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
4081 "only set dimensions can be projected out", goto error
);
4083 dim
= isl_space_dim(space
, isl_dim_set
);
4084 if (first
+ n
> dim
)
4085 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
4086 "range out of bounds", goto error
);
4088 space
= isl_space_from_domain(space
);
4089 space
= isl_space_add_dims(space
, isl_dim_out
, dim
- n
);
4092 return isl_multi_aff_alloc(space
);
4094 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
4095 space
= isl_space_domain(space
);
4096 ls
= isl_local_space_from_space(space
);
4098 for (i
= 0; i
< first
; ++i
) {
4101 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
4103 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
4106 for (i
= 0; i
< dim
- (first
+ n
); ++i
) {
4109 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
4110 isl_dim_set
, first
+ n
+ i
);
4111 ma
= isl_multi_aff_set_aff(ma
, first
+ i
, aff
);
4114 isl_local_space_free(ls
);
4117 isl_space_free(space
);
4121 /* Given the space of a set and a range of set dimensions,
4122 * construct an isl_pw_multi_aff that projects out those dimensions.
4124 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_project_out_map(
4125 __isl_take isl_space
*space
, enum isl_dim_type type
,
4126 unsigned first
, unsigned n
)
4130 ma
= isl_multi_aff_project_out_map(space
, type
, first
, n
);
4131 return isl_pw_multi_aff_from_multi_aff(ma
);
4134 /* Create an isl_pw_multi_aff with the given isl_multi_aff on a universe
4137 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_multi_aff(
4138 __isl_take isl_multi_aff
*ma
)
4140 isl_set
*dom
= isl_set_universe(isl_multi_aff_get_domain_space(ma
));
4141 return isl_pw_multi_aff_alloc(dom
, ma
);
4144 /* Create a piecewise multi-affine expression in the given space that maps each
4145 * input dimension to the corresponding output dimension.
4147 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_identity(
4148 __isl_take isl_space
*space
)
4150 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_identity(space
));
4153 /* Exploit the equalities in "eq" to simplify the affine expressions.
4155 static __isl_give isl_multi_aff
*isl_multi_aff_substitute_equalities(
4156 __isl_take isl_multi_aff
*maff
, __isl_take isl_basic_set
*eq
)
4160 maff
= isl_multi_aff_cow(maff
);
4164 for (i
= 0; i
< maff
->n
; ++i
) {
4165 maff
->u
.p
[i
] = isl_aff_substitute_equalities(maff
->u
.p
[i
],
4166 isl_basic_set_copy(eq
));
4171 isl_basic_set_free(eq
);
4174 isl_basic_set_free(eq
);
4175 isl_multi_aff_free(maff
);
4179 __isl_give isl_multi_aff
*isl_multi_aff_scale(__isl_take isl_multi_aff
*maff
,
4184 maff
= isl_multi_aff_cow(maff
);
4188 for (i
= 0; i
< maff
->n
; ++i
) {
4189 maff
->u
.p
[i
] = isl_aff_scale(maff
->u
.p
[i
], f
);
4191 return isl_multi_aff_free(maff
);
4197 __isl_give isl_multi_aff
*isl_multi_aff_add_on_domain(__isl_keep isl_set
*dom
,
4198 __isl_take isl_multi_aff
*maff1
, __isl_take isl_multi_aff
*maff2
)
4200 maff1
= isl_multi_aff_add(maff1
, maff2
);
4201 maff1
= isl_multi_aff_gist(maff1
, isl_set_copy(dom
));
4205 int isl_multi_aff_is_empty(__isl_keep isl_multi_aff
*maff
)
4213 /* Return the set of domain elements where "ma1" is lexicographically
4214 * smaller than or equal to "ma2".
4216 __isl_give isl_set
*isl_multi_aff_lex_le_set(__isl_take isl_multi_aff
*ma1
,
4217 __isl_take isl_multi_aff
*ma2
)
4219 return isl_multi_aff_lex_ge_set(ma2
, ma1
);
4222 /* Return the set of domain elements where "ma1" is lexicographically
4223 * smaller than "ma2".
4225 __isl_give isl_set
*isl_multi_aff_lex_lt_set(__isl_take isl_multi_aff
*ma1
,
4226 __isl_take isl_multi_aff
*ma2
)
4228 return isl_multi_aff_lex_gt_set(ma2
, ma1
);
4231 /* Return the set of domain elements where "ma1" and "ma2"
4234 static __isl_give isl_set
*isl_multi_aff_order_set(
4235 __isl_take isl_multi_aff
*ma1
, __isl_take isl_multi_aff
*ma2
,
4236 __isl_give isl_map
*order(__isl_take isl_space
*set_space
))
4239 isl_map
*map1
, *map2
;
4242 map1
= isl_map_from_multi_aff(ma1
);
4243 map2
= isl_map_from_multi_aff(ma2
);
4244 map
= isl_map_range_product(map1
, map2
);
4245 space
= isl_space_range(isl_map_get_space(map
));
4246 space
= isl_space_domain(isl_space_unwrap(space
));
4248 map
= isl_map_intersect_range(map
, isl_map_wrap(ge
));
4250 return isl_map_domain(map
);
4253 /* Return the set of domain elements where "ma1" is lexicographically
4254 * greater than or equal to "ma2".
4256 __isl_give isl_set
*isl_multi_aff_lex_ge_set(__isl_take isl_multi_aff
*ma1
,
4257 __isl_take isl_multi_aff
*ma2
)
4259 return isl_multi_aff_order_set(ma1
, ma2
, &isl_map_lex_ge
);
4262 /* Return the set of domain elements where "ma1" is lexicographically
4263 * greater than "ma2".
4265 __isl_give isl_set
*isl_multi_aff_lex_gt_set(__isl_take isl_multi_aff
*ma1
,
4266 __isl_take isl_multi_aff
*ma2
)
4268 return isl_multi_aff_order_set(ma1
, ma2
, &isl_map_lex_gt
);
4272 #define PW isl_pw_multi_aff
4274 #define EL isl_multi_aff
4276 #define EL_IS_ZERO is_empty
4280 #define IS_ZERO is_empty
4283 #undef DEFAULT_IS_ZERO
4284 #define DEFAULT_IS_ZERO 0
4288 #define NO_INSERT_DIMS
4292 #include <isl_pw_templ.c>
4293 #include <isl_pw_union_opt.c>
4298 #define UNION isl_union_pw_multi_aff
4300 #define PART isl_pw_multi_aff
4302 #define PARTS pw_multi_aff
4304 #include <isl_union_multi.c>
4305 #include <isl_union_neg.c>
4307 static __isl_give isl_pw_multi_aff
*pw_multi_aff_union_lexmax(
4308 __isl_take isl_pw_multi_aff
*pma1
,
4309 __isl_take isl_pw_multi_aff
*pma2
)
4311 return isl_pw_multi_aff_union_opt_cmp(pma1
, pma2
,
4312 &isl_multi_aff_lex_ge_set
);
4315 /* Given two piecewise multi affine expressions, return a piecewise
4316 * multi-affine expression defined on the union of the definition domains
4317 * of the inputs that is equal to the lexicographic maximum of the two
4318 * inputs on each cell. If only one of the two inputs is defined on
4319 * a given cell, then it is considered to be the maximum.
4321 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_union_lexmax(
4322 __isl_take isl_pw_multi_aff
*pma1
,
4323 __isl_take isl_pw_multi_aff
*pma2
)
4325 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
4326 &pw_multi_aff_union_lexmax
);
4329 static __isl_give isl_pw_multi_aff
*pw_multi_aff_union_lexmin(
4330 __isl_take isl_pw_multi_aff
*pma1
,
4331 __isl_take isl_pw_multi_aff
*pma2
)
4333 return isl_pw_multi_aff_union_opt_cmp(pma1
, pma2
,
4334 &isl_multi_aff_lex_le_set
);
4337 /* Given two piecewise multi affine expressions, return a piecewise
4338 * multi-affine expression defined on the union of the definition domains
4339 * of the inputs that is equal to the lexicographic minimum of the two
4340 * inputs on each cell. If only one of the two inputs is defined on
4341 * a given cell, then it is considered to be the minimum.
4343 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_union_lexmin(
4344 __isl_take isl_pw_multi_aff
*pma1
,
4345 __isl_take isl_pw_multi_aff
*pma2
)
4347 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
4348 &pw_multi_aff_union_lexmin
);
4351 static __isl_give isl_pw_multi_aff
*pw_multi_aff_add(
4352 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4354 return isl_pw_multi_aff_on_shared_domain(pma1
, pma2
,
4355 &isl_multi_aff_add
);
4358 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_add(
4359 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4361 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
4365 static __isl_give isl_pw_multi_aff
*pw_multi_aff_sub(
4366 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4368 return isl_pw_multi_aff_on_shared_domain(pma1
, pma2
,
4369 &isl_multi_aff_sub
);
4372 /* Subtract "pma2" from "pma1" and return the result.
4374 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_sub(
4375 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4377 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
4381 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_union_add(
4382 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4384 return isl_pw_multi_aff_union_add_(pma1
, pma2
);
4387 /* Compute the sum of "upa1" and "upa2" on the union of their domains,
4388 * with the actual sum on the shared domain and
4389 * the defined expression on the symmetric difference of the domains.
4391 __isl_give isl_union_pw_aff
*isl_union_pw_aff_union_add(
4392 __isl_take isl_union_pw_aff
*upa1
, __isl_take isl_union_pw_aff
*upa2
)
4394 return isl_union_pw_aff_union_add_(upa1
, upa2
);
4397 /* Compute the sum of "upma1" and "upma2" on the union of their domains,
4398 * with the actual sum on the shared domain and
4399 * the defined expression on the symmetric difference of the domains.
4401 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_union_add(
4402 __isl_take isl_union_pw_multi_aff
*upma1
,
4403 __isl_take isl_union_pw_multi_aff
*upma2
)
4405 return isl_union_pw_multi_aff_union_add_(upma1
, upma2
);
4408 /* Given two piecewise multi-affine expressions A -> B and C -> D,
4409 * construct a piecewise multi-affine expression [A -> C] -> [B -> D].
4411 static __isl_give isl_pw_multi_aff
*pw_multi_aff_product(
4412 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4416 isl_pw_multi_aff
*res
;
4421 n
= pma1
->n
* pma2
->n
;
4422 space
= isl_space_product(isl_space_copy(pma1
->dim
),
4423 isl_space_copy(pma2
->dim
));
4424 res
= isl_pw_multi_aff_alloc_size(space
, n
);
4426 for (i
= 0; i
< pma1
->n
; ++i
) {
4427 for (j
= 0; j
< pma2
->n
; ++j
) {
4431 domain
= isl_set_product(isl_set_copy(pma1
->p
[i
].set
),
4432 isl_set_copy(pma2
->p
[j
].set
));
4433 ma
= isl_multi_aff_product(
4434 isl_multi_aff_copy(pma1
->p
[i
].maff
),
4435 isl_multi_aff_copy(pma2
->p
[j
].maff
));
4436 res
= isl_pw_multi_aff_add_piece(res
, domain
, ma
);
4440 isl_pw_multi_aff_free(pma1
);
4441 isl_pw_multi_aff_free(pma2
);
4444 isl_pw_multi_aff_free(pma1
);
4445 isl_pw_multi_aff_free(pma2
);
4449 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_product(
4450 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4452 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
4453 &pw_multi_aff_product
);
4456 /* Construct a map mapping the domain of the piecewise multi-affine expression
4457 * to its range, with each dimension in the range equated to the
4458 * corresponding affine expression on its cell.
4460 * If the domain of "pma" is rational, then so is the constructed "map".
4462 __isl_give isl_map
*isl_map_from_pw_multi_aff(__isl_take isl_pw_multi_aff
*pma
)
4470 map
= isl_map_empty(isl_pw_multi_aff_get_space(pma
));
4472 for (i
= 0; i
< pma
->n
; ++i
) {
4474 isl_multi_aff
*maff
;
4475 isl_basic_map
*bmap
;
4478 rational
= isl_set_is_rational(pma
->p
[i
].set
);
4480 map
= isl_map_free(map
);
4481 maff
= isl_multi_aff_copy(pma
->p
[i
].maff
);
4482 bmap
= isl_basic_map_from_multi_aff2(maff
, rational
);
4483 map_i
= isl_map_from_basic_map(bmap
);
4484 map_i
= isl_map_intersect_domain(map_i
,
4485 isl_set_copy(pma
->p
[i
].set
));
4486 map
= isl_map_union_disjoint(map
, map_i
);
4489 isl_pw_multi_aff_free(pma
);
4493 __isl_give isl_set
*isl_set_from_pw_multi_aff(__isl_take isl_pw_multi_aff
*pma
)
4498 if (!isl_space_is_set(pma
->dim
))
4499 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
4500 "isl_pw_multi_aff cannot be converted into an isl_set",
4503 return isl_map_from_pw_multi_aff(pma
);
4505 isl_pw_multi_aff_free(pma
);
4509 /* Subtract the initial "n" elements in "ma" with coefficients in "c" and
4510 * denominator "denom".
4511 * "denom" is allowed to be negative, in which case the actual denominator
4512 * is -denom and the expressions are added instead.
4514 static __isl_give isl_aff
*subtract_initial(__isl_take isl_aff
*aff
,
4515 __isl_keep isl_multi_aff
*ma
, int n
, isl_int
*c
, isl_int denom
)
4521 first
= isl_seq_first_non_zero(c
, n
);
4525 sign
= isl_int_sgn(denom
);
4527 isl_int_abs(d
, denom
);
4528 for (i
= first
; i
< n
; ++i
) {
4531 if (isl_int_is_zero(c
[i
]))
4533 aff_i
= isl_multi_aff_get_aff(ma
, i
);
4534 aff_i
= isl_aff_scale(aff_i
, c
[i
]);
4535 aff_i
= isl_aff_scale_down(aff_i
, d
);
4537 aff
= isl_aff_sub(aff
, aff_i
);
4539 aff
= isl_aff_add(aff
, aff_i
);
4546 /* Extract an affine expression that expresses the output dimension "pos"
4547 * of "bmap" in terms of the parameters and input dimensions from
4549 * Note that this expression may involve integer divisions defined
4550 * in terms of parameters and input dimensions.
4551 * The equality may also involve references to earlier (but not later)
4552 * output dimensions. These are replaced by the corresponding elements
4555 * If the equality is of the form
4557 * f(i) + h(j) + a x + g(i) = 0,
4559 * with f(i) a linear combinations of the parameters and input dimensions,
4560 * g(i) a linear combination of integer divisions defined in terms of the same
4561 * and h(j) a linear combinations of earlier output dimensions,
4562 * then the affine expression is
4564 * (-f(i) - g(i))/a - h(j)/a
4566 * If the equality is of the form
4568 * f(i) + h(j) - a x + g(i) = 0,
4570 * then the affine expression is
4572 * (f(i) + g(i))/a - h(j)/(-a)
4575 * If "div" refers to an integer division (i.e., it is smaller than
4576 * the number of integer divisions), then the equality constraint
4577 * does involve an integer division (the one at position "div") that
4578 * is defined in terms of output dimensions. However, this integer
4579 * division can be eliminated by exploiting a pair of constraints
4580 * x >= l and x <= l + n, with n smaller than the coefficient of "div"
4581 * in the equality constraint. "ineq" refers to inequality x >= l, i.e.,
4583 * In particular, let
4585 * x = e(i) + m floor(...)
4587 * with e(i) the expression derived above and floor(...) the integer
4588 * division involving output dimensions.
4599 * e(i) + m floor(...) - l = (e(i) + m floor(...) - l) mod m
4600 * = (e(i) - l) mod m
4604 * x - l = (e(i) - l) mod m
4608 * x = ((e(i) - l) mod m) + l
4610 * The variable "shift" below contains the expression -l, which may
4611 * also involve a linear combination of earlier output dimensions.
4613 static __isl_give isl_aff
*extract_aff_from_equality(
4614 __isl_keep isl_basic_map
*bmap
, int pos
, int eq
, int div
, int ineq
,
4615 __isl_keep isl_multi_aff
*ma
)
4618 unsigned n_div
, n_out
;
4620 isl_local_space
*ls
;
4621 isl_aff
*aff
, *shift
;
4624 ctx
= isl_basic_map_get_ctx(bmap
);
4625 ls
= isl_basic_map_get_local_space(bmap
);
4626 ls
= isl_local_space_domain(ls
);
4627 aff
= isl_aff_alloc(isl_local_space_copy(ls
));
4630 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
4631 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4632 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4633 if (isl_int_is_neg(bmap
->eq
[eq
][o_out
+ pos
])) {
4634 isl_seq_cpy(aff
->v
->el
+ 1, bmap
->eq
[eq
], o_out
);
4635 isl_seq_cpy(aff
->v
->el
+ 1 + o_out
,
4636 bmap
->eq
[eq
] + o_out
+ n_out
, n_div
);
4638 isl_seq_neg(aff
->v
->el
+ 1, bmap
->eq
[eq
], o_out
);
4639 isl_seq_neg(aff
->v
->el
+ 1 + o_out
,
4640 bmap
->eq
[eq
] + o_out
+ n_out
, n_div
);
4643 isl_int_set_si(aff
->v
->el
[1 + o_out
+ div
], 0);
4644 isl_int_abs(aff
->v
->el
[0], bmap
->eq
[eq
][o_out
+ pos
]);
4645 aff
= subtract_initial(aff
, ma
, pos
, bmap
->eq
[eq
] + o_out
,
4646 bmap
->eq
[eq
][o_out
+ pos
]);
4648 shift
= isl_aff_alloc(isl_local_space_copy(ls
));
4651 isl_seq_cpy(shift
->v
->el
+ 1, bmap
->ineq
[ineq
], o_out
);
4652 isl_seq_cpy(shift
->v
->el
+ 1 + o_out
,
4653 bmap
->ineq
[ineq
] + o_out
+ n_out
, n_div
);
4654 isl_int_set_si(shift
->v
->el
[0], 1);
4655 shift
= subtract_initial(shift
, ma
, pos
,
4656 bmap
->ineq
[ineq
] + o_out
, ctx
->negone
);
4657 aff
= isl_aff_add(aff
, isl_aff_copy(shift
));
4658 mod
= isl_val_int_from_isl_int(ctx
,
4659 bmap
->eq
[eq
][o_out
+ n_out
+ div
]);
4660 mod
= isl_val_abs(mod
);
4661 aff
= isl_aff_mod_val(aff
, mod
);
4662 aff
= isl_aff_sub(aff
, shift
);
4665 isl_local_space_free(ls
);
4668 isl_local_space_free(ls
);
4673 /* Given a basic map with output dimensions defined
4674 * in terms of the parameters input dimensions and earlier
4675 * output dimensions using an equality (and possibly a pair on inequalities),
4676 * extract an isl_aff that expresses output dimension "pos" in terms
4677 * of the parameters and input dimensions.
4678 * Note that this expression may involve integer divisions defined
4679 * in terms of parameters and input dimensions.
4680 * "ma" contains the expressions corresponding to earlier output dimensions.
4682 * This function shares some similarities with
4683 * isl_basic_map_has_defining_equality and isl_constraint_get_bound.
4685 static __isl_give isl_aff
*extract_isl_aff_from_basic_map(
4686 __isl_keep isl_basic_map
*bmap
, int pos
, __isl_keep isl_multi_aff
*ma
)
4693 eq
= isl_basic_map_output_defining_equality(bmap
, pos
, &div
, &ineq
);
4694 if (eq
>= bmap
->n_eq
)
4695 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
4696 "unable to find suitable equality", return NULL
);
4697 aff
= extract_aff_from_equality(bmap
, pos
, eq
, div
, ineq
, ma
);
4699 aff
= isl_aff_remove_unused_divs(aff
);
4703 /* Given a basic map where each output dimension is defined
4704 * in terms of the parameters and input dimensions using an equality,
4705 * extract an isl_multi_aff that expresses the output dimensions in terms
4706 * of the parameters and input dimensions.
4708 static __isl_give isl_multi_aff
*extract_isl_multi_aff_from_basic_map(
4709 __isl_take isl_basic_map
*bmap
)
4718 ma
= isl_multi_aff_alloc(isl_basic_map_get_space(bmap
));
4719 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4721 for (i
= 0; i
< n_out
; ++i
) {
4724 aff
= extract_isl_aff_from_basic_map(bmap
, i
, ma
);
4725 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
4728 isl_basic_map_free(bmap
);
4733 /* Given a basic set where each set dimension is defined
4734 * in terms of the parameters using an equality,
4735 * extract an isl_multi_aff that expresses the set dimensions in terms
4736 * of the parameters.
4738 __isl_give isl_multi_aff
*isl_multi_aff_from_basic_set_equalities(
4739 __isl_take isl_basic_set
*bset
)
4741 return extract_isl_multi_aff_from_basic_map(bset
);
4744 /* Create an isl_pw_multi_aff that is equivalent to
4745 * isl_map_intersect_domain(isl_map_from_basic_map(bmap), domain).
4746 * The given basic map is such that each output dimension is defined
4747 * in terms of the parameters and input dimensions using an equality.
4749 * Since some applications expect the result of isl_pw_multi_aff_from_map
4750 * to only contain integer affine expressions, we compute the floor
4751 * of the expression before returning.
4753 * Remove all constraints involving local variables without
4754 * an explicit representation (resulting in the removal of those
4755 * local variables) prior to the actual extraction to ensure
4756 * that the local spaces in which the resulting affine expressions
4757 * are created do not contain any unknown local variables.
4758 * Removing such constraints is safe because constraints involving
4759 * unknown local variables are not used to determine whether
4760 * a basic map is obviously single-valued.
4762 static __isl_give isl_pw_multi_aff
*plain_pw_multi_aff_from_map(
4763 __isl_take isl_set
*domain
, __isl_take isl_basic_map
*bmap
)
4767 bmap
= isl_basic_map_drop_constraint_involving_unknown_divs(bmap
);
4768 ma
= extract_isl_multi_aff_from_basic_map(bmap
);
4769 ma
= isl_multi_aff_floor(ma
);
4770 return isl_pw_multi_aff_alloc(domain
, ma
);
4773 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4774 * This obviously only works if the input "map" is single-valued.
4775 * If so, we compute the lexicographic minimum of the image in the form
4776 * of an isl_pw_multi_aff. Since the image is unique, it is equal
4777 * to its lexicographic minimum.
4778 * If the input is not single-valued, we produce an error.
4780 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_base(
4781 __isl_take isl_map
*map
)
4785 isl_pw_multi_aff
*pma
;
4787 sv
= isl_map_is_single_valued(map
);
4791 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
4792 "map is not single-valued", goto error
);
4793 map
= isl_map_make_disjoint(map
);
4797 pma
= isl_pw_multi_aff_empty(isl_map_get_space(map
));
4799 for (i
= 0; i
< map
->n
; ++i
) {
4800 isl_pw_multi_aff
*pma_i
;
4801 isl_basic_map
*bmap
;
4802 bmap
= isl_basic_map_copy(map
->p
[i
]);
4803 pma_i
= isl_basic_map_lexmin_pw_multi_aff(bmap
);
4804 pma
= isl_pw_multi_aff_add_disjoint(pma
, pma_i
);
4814 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
4815 * taking into account that the output dimension at position "d"
4816 * can be represented as
4818 * x = floor((e(...) + c1) / m)
4820 * given that constraint "i" is of the form
4822 * e(...) + c1 - m x >= 0
4825 * Let "map" be of the form
4829 * We construct a mapping
4831 * A -> [A -> x = floor(...)]
4833 * apply that to the map, obtaining
4835 * [A -> x = floor(...)] -> B
4837 * and equate dimension "d" to x.
4838 * We then compute a isl_pw_multi_aff representation of the resulting map
4839 * and plug in the mapping above.
4841 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_div(
4842 __isl_take isl_map
*map
, __isl_take isl_basic_map
*hull
, int d
, int i
)
4846 isl_local_space
*ls
;
4854 isl_pw_multi_aff
*pma
;
4857 is_set
= isl_map_is_set(map
);
4861 offset
= isl_basic_map_offset(hull
, isl_dim_out
);
4862 ctx
= isl_map_get_ctx(map
);
4863 space
= isl_space_domain(isl_map_get_space(map
));
4864 n_in
= isl_space_dim(space
, isl_dim_set
);
4865 n
= isl_space_dim(space
, isl_dim_all
);
4867 v
= isl_vec_alloc(ctx
, 1 + 1 + n
);
4869 isl_int_neg(v
->el
[0], hull
->ineq
[i
][offset
+ d
]);
4870 isl_seq_cpy(v
->el
+ 1, hull
->ineq
[i
], 1 + n
);
4872 isl_basic_map_free(hull
);
4874 ls
= isl_local_space_from_space(isl_space_copy(space
));
4875 aff
= isl_aff_alloc_vec(ls
, v
);
4876 aff
= isl_aff_floor(aff
);
4878 isl_space_free(space
);
4879 ma
= isl_multi_aff_from_aff(aff
);
4881 ma
= isl_multi_aff_identity(isl_space_map_from_set(space
));
4882 ma
= isl_multi_aff_range_product(ma
,
4883 isl_multi_aff_from_aff(aff
));
4886 insert
= isl_map_from_multi_aff(isl_multi_aff_copy(ma
));
4887 map
= isl_map_apply_domain(map
, insert
);
4888 map
= isl_map_equate(map
, isl_dim_in
, n_in
, isl_dim_out
, d
);
4889 pma
= isl_pw_multi_aff_from_map(map
);
4890 pma
= isl_pw_multi_aff_pullback_multi_aff(pma
, ma
);
4895 isl_basic_map_free(hull
);
4899 /* Is constraint "c" of the form
4901 * e(...) + c1 - m x >= 0
4905 * -e(...) + c2 + m x >= 0
4907 * where m > 1 and e only depends on parameters and input dimemnsions?
4909 * "offset" is the offset of the output dimensions
4910 * "pos" is the position of output dimension x.
4912 static int is_potential_div_constraint(isl_int
*c
, int offset
, int d
, int total
)
4914 if (isl_int_is_zero(c
[offset
+ d
]))
4916 if (isl_int_is_one(c
[offset
+ d
]))
4918 if (isl_int_is_negone(c
[offset
+ d
]))
4920 if (isl_seq_first_non_zero(c
+ offset
, d
) != -1)
4922 if (isl_seq_first_non_zero(c
+ offset
+ d
+ 1,
4923 total
- (offset
+ d
+ 1)) != -1)
4928 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4930 * As a special case, we first check if there is any pair of constraints,
4931 * shared by all the basic maps in "map" that force a given dimension
4932 * to be equal to the floor of some affine combination of the input dimensions.
4934 * In particular, if we can find two constraints
4936 * e(...) + c1 - m x >= 0 i.e., m x <= e(...) + c1
4940 * -e(...) + c2 + m x >= 0 i.e., m x >= e(...) - c2
4942 * where m > 1 and e only depends on parameters and input dimemnsions,
4945 * c1 + c2 < m i.e., -c2 >= c1 - (m - 1)
4947 * then we know that we can take
4949 * x = floor((e(...) + c1) / m)
4951 * without having to perform any computation.
4953 * Note that we know that
4957 * If c1 + c2 were 0, then we would have detected an equality during
4958 * simplification. If c1 + c2 were negative, then we would have detected
4961 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_check_div(
4962 __isl_take isl_map
*map
)
4968 isl_basic_map
*hull
;
4970 hull
= isl_map_unshifted_simple_hull(isl_map_copy(map
));
4975 dim
= isl_map_dim(map
, isl_dim_out
);
4976 offset
= isl_basic_map_offset(hull
, isl_dim_out
);
4977 total
= 1 + isl_basic_map_total_dim(hull
);
4979 for (d
= 0; d
< dim
; ++d
) {
4980 for (i
= 0; i
< n
; ++i
) {
4981 if (!is_potential_div_constraint(hull
->ineq
[i
],
4984 for (j
= i
+ 1; j
< n
; ++j
) {
4985 if (!isl_seq_is_neg(hull
->ineq
[i
] + 1,
4986 hull
->ineq
[j
] + 1, total
- 1))
4988 isl_int_add(sum
, hull
->ineq
[i
][0],
4990 if (isl_int_abs_lt(sum
,
4991 hull
->ineq
[i
][offset
+ d
]))
4998 if (isl_int_is_pos(hull
->ineq
[j
][offset
+ d
]))
5000 return pw_multi_aff_from_map_div(map
, hull
, d
, j
);
5004 isl_basic_map_free(hull
);
5005 return pw_multi_aff_from_map_base(map
);
5008 isl_basic_map_free(hull
);
5012 /* Given an affine expression
5014 * [A -> B] -> f(A,B)
5016 * construct an isl_multi_aff
5020 * such that dimension "d" in B' is set to "aff" and the remaining
5021 * dimensions are set equal to the corresponding dimensions in B.
5022 * "n_in" is the dimension of the space A.
5023 * "n_out" is the dimension of the space B.
5025 * If "is_set" is set, then the affine expression is of the form
5029 * and we construct an isl_multi_aff
5033 static __isl_give isl_multi_aff
*range_map(__isl_take isl_aff
*aff
, int d
,
5034 unsigned n_in
, unsigned n_out
, int is_set
)
5038 isl_space
*space
, *space2
;
5039 isl_local_space
*ls
;
5041 space
= isl_aff_get_domain_space(aff
);
5042 ls
= isl_local_space_from_space(isl_space_copy(space
));
5043 space2
= isl_space_copy(space
);
5045 space2
= isl_space_range(isl_space_unwrap(space2
));
5046 space
= isl_space_map_from_domain_and_range(space
, space2
);
5047 ma
= isl_multi_aff_alloc(space
);
5048 ma
= isl_multi_aff_set_aff(ma
, d
, aff
);
5050 for (i
= 0; i
< n_out
; ++i
) {
5053 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
5054 isl_dim_set
, n_in
+ i
);
5055 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
5058 isl_local_space_free(ls
);
5063 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
5064 * taking into account that the dimension at position "d" can be written as
5066 * x = m a + f(..) (1)
5068 * where m is equal to "gcd".
5069 * "i" is the index of the equality in "hull" that defines f(..).
5070 * In particular, the equality is of the form
5072 * f(..) - x + m g(existentials) = 0
5076 * -f(..) + x + m g(existentials) = 0
5078 * We basically plug (1) into "map", resulting in a map with "a"
5079 * in the range instead of "x". The corresponding isl_pw_multi_aff
5080 * defining "a" is then plugged back into (1) to obtain a definition for "x".
5082 * Specifically, given the input map
5086 * We first wrap it into a set
5090 * and define (1) on top of the corresponding space, resulting in "aff".
5091 * We use this to create an isl_multi_aff that maps the output position "d"
5092 * from "a" to "x", leaving all other (intput and output) dimensions unchanged.
5093 * We plug this into the wrapped map, unwrap the result and compute the
5094 * corresponding isl_pw_multi_aff.
5095 * The result is an expression
5103 * so that we can plug that into "aff", after extending the latter to
5109 * If "map" is actually a set, then there is no "A" space, meaning
5110 * that we do not need to perform any wrapping, and that the result
5111 * of the recursive call is of the form
5115 * which is plugged into a mapping of the form
5119 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_stride(
5120 __isl_take isl_map
*map
, __isl_take isl_basic_map
*hull
, int d
, int i
,
5125 isl_local_space
*ls
;
5128 isl_pw_multi_aff
*pma
, *id
;
5134 is_set
= isl_map_is_set(map
);
5138 n_in
= isl_basic_map_dim(hull
, isl_dim_in
);
5139 n_out
= isl_basic_map_dim(hull
, isl_dim_out
);
5140 o_out
= isl_basic_map_offset(hull
, isl_dim_out
);
5145 set
= isl_map_wrap(map
);
5146 space
= isl_space_map_from_set(isl_set_get_space(set
));
5147 ma
= isl_multi_aff_identity(space
);
5148 ls
= isl_local_space_from_space(isl_set_get_space(set
));
5149 aff
= isl_aff_alloc(ls
);
5151 isl_int_set_si(aff
->v
->el
[0], 1);
5152 if (isl_int_is_one(hull
->eq
[i
][o_out
+ d
]))
5153 isl_seq_neg(aff
->v
->el
+ 1, hull
->eq
[i
],
5156 isl_seq_cpy(aff
->v
->el
+ 1, hull
->eq
[i
],
5158 isl_int_set(aff
->v
->el
[1 + o_out
+ d
], gcd
);
5160 ma
= isl_multi_aff_set_aff(ma
, n_in
+ d
, isl_aff_copy(aff
));
5161 set
= isl_set_preimage_multi_aff(set
, ma
);
5163 ma
= range_map(aff
, d
, n_in
, n_out
, is_set
);
5168 map
= isl_set_unwrap(set
);
5169 pma
= isl_pw_multi_aff_from_map(map
);
5172 space
= isl_pw_multi_aff_get_domain_space(pma
);
5173 space
= isl_space_map_from_set(space
);
5174 id
= isl_pw_multi_aff_identity(space
);
5175 pma
= isl_pw_multi_aff_range_product(id
, pma
);
5177 id
= isl_pw_multi_aff_from_multi_aff(ma
);
5178 pma
= isl_pw_multi_aff_pullback_pw_multi_aff(id
, pma
);
5180 isl_basic_map_free(hull
);
5184 isl_basic_map_free(hull
);
5188 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5189 * "hull" contains the equalities valid for "map".
5191 * Check if any of the output dimensions is "strided".
5192 * That is, we check if it can be written as
5196 * with m greater than 1, a some combination of existentially quantified
5197 * variables and f an expression in the parameters and input dimensions.
5198 * If so, we remove the stride in pw_multi_aff_from_map_stride.
5200 * Otherwise, we continue with pw_multi_aff_from_map_check_div for a further
5203 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_check_strides(
5204 __isl_take isl_map
*map
, __isl_take isl_basic_map
*hull
)
5213 n_div
= isl_basic_map_dim(hull
, isl_dim_div
);
5214 o_div
= isl_basic_map_offset(hull
, isl_dim_div
);
5217 isl_basic_map_free(hull
);
5218 return pw_multi_aff_from_map_check_div(map
);
5223 n_out
= isl_basic_map_dim(hull
, isl_dim_out
);
5224 o_out
= isl_basic_map_offset(hull
, isl_dim_out
);
5226 for (i
= 0; i
< n_out
; ++i
) {
5227 for (j
= 0; j
< hull
->n_eq
; ++j
) {
5228 isl_int
*eq
= hull
->eq
[j
];
5229 isl_pw_multi_aff
*res
;
5231 if (!isl_int_is_one(eq
[o_out
+ i
]) &&
5232 !isl_int_is_negone(eq
[o_out
+ i
]))
5234 if (isl_seq_first_non_zero(eq
+ o_out
, i
) != -1)
5236 if (isl_seq_first_non_zero(eq
+ o_out
+ i
+ 1,
5237 n_out
- (i
+ 1)) != -1)
5239 isl_seq_gcd(eq
+ o_div
, n_div
, &gcd
);
5240 if (isl_int_is_zero(gcd
))
5242 if (isl_int_is_one(gcd
))
5245 res
= pw_multi_aff_from_map_stride(map
, hull
,
5253 isl_basic_map_free(hull
);
5254 return pw_multi_aff_from_map_check_div(map
);
5257 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5259 * As a special case, we first check if all output dimensions are uniquely
5260 * defined in terms of the parameters and input dimensions over the entire
5261 * domain. If so, we extract the desired isl_pw_multi_aff directly
5262 * from the affine hull of "map" and its domain.
5264 * Otherwise, continue with pw_multi_aff_from_map_check_strides for more
5267 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_map(__isl_take isl_map
*map
)
5270 isl_basic_map
*hull
;
5275 if (isl_map_n_basic_map(map
) == 1) {
5276 hull
= isl_map_unshifted_simple_hull(isl_map_copy(map
));
5277 hull
= isl_basic_map_plain_affine_hull(hull
);
5278 sv
= isl_basic_map_plain_is_single_valued(hull
);
5280 return plain_pw_multi_aff_from_map(isl_map_domain(map
),
5282 isl_basic_map_free(hull
);
5284 map
= isl_map_detect_equalities(map
);
5285 hull
= isl_map_unshifted_simple_hull(isl_map_copy(map
));
5286 sv
= isl_basic_map_plain_is_single_valued(hull
);
5288 return plain_pw_multi_aff_from_map(isl_map_domain(map
), hull
);
5290 return pw_multi_aff_from_map_check_strides(map
, hull
);
5291 isl_basic_map_free(hull
);
5296 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_set(__isl_take isl_set
*set
)
5298 return isl_pw_multi_aff_from_map(set
);
5301 /* Convert "map" into an isl_pw_multi_aff (if possible) and
5304 static isl_stat
pw_multi_aff_from_map(__isl_take isl_map
*map
, void *user
)
5306 isl_union_pw_multi_aff
**upma
= user
;
5307 isl_pw_multi_aff
*pma
;
5309 pma
= isl_pw_multi_aff_from_map(map
);
5310 *upma
= isl_union_pw_multi_aff_add_pw_multi_aff(*upma
, pma
);
5312 return *upma
? isl_stat_ok
: isl_stat_error
;
5315 /* Create an isl_union_pw_multi_aff with the given isl_aff on a universe
5318 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_aff(
5319 __isl_take isl_aff
*aff
)
5322 isl_pw_multi_aff
*pma
;
5324 ma
= isl_multi_aff_from_aff(aff
);
5325 pma
= isl_pw_multi_aff_from_multi_aff(ma
);
5326 return isl_union_pw_multi_aff_from_pw_multi_aff(pma
);
5329 /* Try and create an isl_union_pw_multi_aff that is equivalent
5330 * to the given isl_union_map.
5331 * The isl_union_map is required to be single-valued in each space.
5332 * Otherwise, an error is produced.
5334 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_union_map(
5335 __isl_take isl_union_map
*umap
)
5338 isl_union_pw_multi_aff
*upma
;
5340 space
= isl_union_map_get_space(umap
);
5341 upma
= isl_union_pw_multi_aff_empty(space
);
5342 if (isl_union_map_foreach_map(umap
, &pw_multi_aff_from_map
, &upma
) < 0)
5343 upma
= isl_union_pw_multi_aff_free(upma
);
5344 isl_union_map_free(umap
);
5349 /* Try and create an isl_union_pw_multi_aff that is equivalent
5350 * to the given isl_union_set.
5351 * The isl_union_set is required to be a singleton in each space.
5352 * Otherwise, an error is produced.
5354 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_union_set(
5355 __isl_take isl_union_set
*uset
)
5357 return isl_union_pw_multi_aff_from_union_map(uset
);
5360 /* Return the piecewise affine expression "set ? 1 : 0".
5362 __isl_give isl_pw_aff
*isl_set_indicator_function(__isl_take isl_set
*set
)
5365 isl_space
*space
= isl_set_get_space(set
);
5366 isl_local_space
*ls
= isl_local_space_from_space(space
);
5367 isl_aff
*zero
= isl_aff_zero_on_domain(isl_local_space_copy(ls
));
5368 isl_aff
*one
= isl_aff_zero_on_domain(ls
);
5370 one
= isl_aff_add_constant_si(one
, 1);
5371 pa
= isl_pw_aff_alloc(isl_set_copy(set
), one
);
5372 set
= isl_set_complement(set
);
5373 pa
= isl_pw_aff_add_disjoint(pa
, isl_pw_aff_alloc(set
, zero
));
5378 /* Plug in "subs" for dimension "type", "pos" of "aff".
5380 * Let i be the dimension to replace and let "subs" be of the form
5384 * and "aff" of the form
5390 * (a f + d g')/(m d)
5392 * where g' is the result of plugging in "subs" in each of the integer
5395 __isl_give isl_aff
*isl_aff_substitute(__isl_take isl_aff
*aff
,
5396 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
5401 aff
= isl_aff_cow(aff
);
5403 return isl_aff_free(aff
);
5405 ctx
= isl_aff_get_ctx(aff
);
5406 if (!isl_space_is_equal(aff
->ls
->dim
, subs
->ls
->dim
))
5407 isl_die(ctx
, isl_error_invalid
,
5408 "spaces don't match", return isl_aff_free(aff
));
5409 if (isl_local_space_dim(subs
->ls
, isl_dim_div
) != 0)
5410 isl_die(ctx
, isl_error_unsupported
,
5411 "cannot handle divs yet", return isl_aff_free(aff
));
5413 aff
->ls
= isl_local_space_substitute(aff
->ls
, type
, pos
, subs
);
5415 return isl_aff_free(aff
);
5417 aff
->v
= isl_vec_cow(aff
->v
);
5419 return isl_aff_free(aff
);
5421 pos
+= isl_local_space_offset(aff
->ls
, type
);
5424 isl_seq_substitute(aff
->v
->el
, pos
, subs
->v
->el
,
5425 aff
->v
->size
, subs
->v
->size
, v
);
5431 /* Plug in "subs" for dimension "type", "pos" in each of the affine
5432 * expressions in "maff".
5434 __isl_give isl_multi_aff
*isl_multi_aff_substitute(
5435 __isl_take isl_multi_aff
*maff
, enum isl_dim_type type
, unsigned pos
,
5436 __isl_keep isl_aff
*subs
)
5440 maff
= isl_multi_aff_cow(maff
);
5442 return isl_multi_aff_free(maff
);
5444 if (type
== isl_dim_in
)
5447 for (i
= 0; i
< maff
->n
; ++i
) {
5448 maff
->u
.p
[i
] = isl_aff_substitute(maff
->u
.p
[i
],
5451 return isl_multi_aff_free(maff
);
5457 /* Plug in "subs" for dimension "type", "pos" of "pma".
5459 * pma is of the form
5463 * while subs is of the form
5465 * v' = B_j(v) -> S_j
5467 * Each pair i,j such that C_ij = A_i \cap B_i is non-empty
5468 * has a contribution in the result, in particular
5470 * C_ij(S_j) -> M_i(S_j)
5472 * Note that plugging in S_j in C_ij may also result in an empty set
5473 * and this contribution should simply be discarded.
5475 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_substitute(
5476 __isl_take isl_pw_multi_aff
*pma
, enum isl_dim_type type
, unsigned pos
,
5477 __isl_keep isl_pw_aff
*subs
)
5480 isl_pw_multi_aff
*res
;
5483 return isl_pw_multi_aff_free(pma
);
5485 n
= pma
->n
* subs
->n
;
5486 res
= isl_pw_multi_aff_alloc_size(isl_space_copy(pma
->dim
), n
);
5488 for (i
= 0; i
< pma
->n
; ++i
) {
5489 for (j
= 0; j
< subs
->n
; ++j
) {
5491 isl_multi_aff
*res_ij
;
5494 common
= isl_set_intersect(
5495 isl_set_copy(pma
->p
[i
].set
),
5496 isl_set_copy(subs
->p
[j
].set
));
5497 common
= isl_set_substitute(common
,
5498 type
, pos
, subs
->p
[j
].aff
);
5499 empty
= isl_set_plain_is_empty(common
);
5500 if (empty
< 0 || empty
) {
5501 isl_set_free(common
);
5507 res_ij
= isl_multi_aff_substitute(
5508 isl_multi_aff_copy(pma
->p
[i
].maff
),
5509 type
, pos
, subs
->p
[j
].aff
);
5511 res
= isl_pw_multi_aff_add_piece(res
, common
, res_ij
);
5515 isl_pw_multi_aff_free(pma
);
5518 isl_pw_multi_aff_free(pma
);
5519 isl_pw_multi_aff_free(res
);
5523 /* Compute the preimage of a range of dimensions in the affine expression "src"
5524 * under "ma" and put the result in "dst". The number of dimensions in "src"
5525 * that precede the range is given by "n_before". The number of dimensions
5526 * in the range is given by the number of output dimensions of "ma".
5527 * The number of dimensions that follow the range is given by "n_after".
5528 * If "has_denom" is set (to one),
5529 * then "src" and "dst" have an extra initial denominator.
5530 * "n_div_ma" is the number of existentials in "ma"
5531 * "n_div_bset" is the number of existentials in "src"
5532 * The resulting "dst" (which is assumed to have been allocated by
5533 * the caller) contains coefficients for both sets of existentials,
5534 * first those in "ma" and then those in "src".
5535 * f, c1, c2 and g are temporary objects that have been initialized
5538 * Let src represent the expression
5540 * (a(p) + f_u u + b v + f_w w + c(divs))/d
5542 * and let ma represent the expressions
5544 * v_i = (r_i(p) + s_i(y) + t_i(divs'))/m_i
5546 * We start out with the following expression for dst:
5548 * (a(p) + f_u u + 0 y + f_w w + 0 divs' + c(divs) + f \sum_i b_i v_i)/d
5550 * with the multiplication factor f initially equal to 1
5551 * and f \sum_i b_i v_i kept separately.
5552 * For each x_i that we substitute, we multiply the numerator
5553 * (and denominator) of dst by c_1 = m_i and add the numerator
5554 * of the x_i expression multiplied by c_2 = f b_i,
5555 * after removing the common factors of c_1 and c_2.
5556 * The multiplication factor f also needs to be multiplied by c_1
5557 * for the next x_j, j > i.
5559 void isl_seq_preimage(isl_int
*dst
, isl_int
*src
,
5560 __isl_keep isl_multi_aff
*ma
, int n_before
, int n_after
,
5561 int n_div_ma
, int n_div_bmap
,
5562 isl_int f
, isl_int c1
, isl_int c2
, isl_int g
, int has_denom
)
5565 int n_param
, n_in
, n_out
;
5568 n_param
= isl_multi_aff_dim(ma
, isl_dim_param
);
5569 n_in
= isl_multi_aff_dim(ma
, isl_dim_in
);
5570 n_out
= isl_multi_aff_dim(ma
, isl_dim_out
);
5572 isl_seq_cpy(dst
, src
, has_denom
+ 1 + n_param
+ n_before
);
5573 o_dst
= o_src
= has_denom
+ 1 + n_param
+ n_before
;
5574 isl_seq_clr(dst
+ o_dst
, n_in
);
5577 isl_seq_cpy(dst
+ o_dst
, src
+ o_src
, n_after
);
5580 isl_seq_clr(dst
+ o_dst
, n_div_ma
);
5582 isl_seq_cpy(dst
+ o_dst
, src
+ o_src
, n_div_bmap
);
5584 isl_int_set_si(f
, 1);
5586 for (i
= 0; i
< n_out
; ++i
) {
5587 int offset
= has_denom
+ 1 + n_param
+ n_before
+ i
;
5589 if (isl_int_is_zero(src
[offset
]))
5591 isl_int_set(c1
, ma
->u
.p
[i
]->v
->el
[0]);
5592 isl_int_mul(c2
, f
, src
[offset
]);
5593 isl_int_gcd(g
, c1
, c2
);
5594 isl_int_divexact(c1
, c1
, g
);
5595 isl_int_divexact(c2
, c2
, g
);
5597 isl_int_mul(f
, f
, c1
);
5600 isl_seq_combine(dst
+ o_dst
, c1
, dst
+ o_dst
,
5601 c2
, ma
->u
.p
[i
]->v
->el
+ o_src
, 1 + n_param
);
5602 o_dst
+= 1 + n_param
;
5603 o_src
+= 1 + n_param
;
5604 isl_seq_scale(dst
+ o_dst
, dst
+ o_dst
, c1
, n_before
);
5606 isl_seq_combine(dst
+ o_dst
, c1
, dst
+ o_dst
,
5607 c2
, ma
->u
.p
[i
]->v
->el
+ o_src
, n_in
);
5610 isl_seq_scale(dst
+ o_dst
, dst
+ o_dst
, c1
, n_after
);
5612 isl_seq_combine(dst
+ o_dst
, c1
, dst
+ o_dst
,
5613 c2
, ma
->u
.p
[i
]->v
->el
+ o_src
, n_div_ma
);
5616 isl_seq_scale(dst
+ o_dst
, dst
+ o_dst
, c1
, n_div_bmap
);
5618 isl_int_mul(dst
[0], dst
[0], c1
);
5622 /* Compute the pullback of "aff" by the function represented by "ma".
5623 * In other words, plug in "ma" in "aff". The result is an affine expression
5624 * defined over the domain space of "ma".
5626 * If "aff" is represented by
5628 * (a(p) + b x + c(divs))/d
5630 * and ma is represented by
5632 * x = D(p) + F(y) + G(divs')
5634 * then the result is
5636 * (a(p) + b D(p) + b F(y) + b G(divs') + c(divs))/d
5638 * The divs in the local space of the input are similarly adjusted
5639 * through a call to isl_local_space_preimage_multi_aff.
5641 __isl_give isl_aff
*isl_aff_pullback_multi_aff(__isl_take isl_aff
*aff
,
5642 __isl_take isl_multi_aff
*ma
)
5644 isl_aff
*res
= NULL
;
5645 isl_local_space
*ls
;
5646 int n_div_aff
, n_div_ma
;
5647 isl_int f
, c1
, c2
, g
;
5649 ma
= isl_multi_aff_align_divs(ma
);
5653 n_div_aff
= isl_aff_dim(aff
, isl_dim_div
);
5654 n_div_ma
= ma
->n
? isl_aff_dim(ma
->u
.p
[0], isl_dim_div
) : 0;
5656 ls
= isl_aff_get_domain_local_space(aff
);
5657 ls
= isl_local_space_preimage_multi_aff(ls
, isl_multi_aff_copy(ma
));
5658 res
= isl_aff_alloc(ls
);
5667 isl_seq_preimage(res
->v
->el
, aff
->v
->el
, ma
, 0, 0, n_div_ma
, n_div_aff
,
5676 isl_multi_aff_free(ma
);
5677 res
= isl_aff_normalize(res
);
5681 isl_multi_aff_free(ma
);
5686 /* Compute the pullback of "aff1" by the function represented by "aff2".
5687 * In other words, plug in "aff2" in "aff1". The result is an affine expression
5688 * defined over the domain space of "aff1".
5690 * The domain of "aff1" should match the range of "aff2", which means
5691 * that it should be single-dimensional.
5693 __isl_give isl_aff
*isl_aff_pullback_aff(__isl_take isl_aff
*aff1
,
5694 __isl_take isl_aff
*aff2
)
5698 ma
= isl_multi_aff_from_aff(aff2
);
5699 return isl_aff_pullback_multi_aff(aff1
, ma
);
5702 /* Compute the pullback of "ma1" by the function represented by "ma2".
5703 * In other words, plug in "ma2" in "ma1".
5705 * The parameters of "ma1" and "ma2" are assumed to have been aligned.
5707 static __isl_give isl_multi_aff
*isl_multi_aff_pullback_multi_aff_aligned(
5708 __isl_take isl_multi_aff
*ma1
, __isl_take isl_multi_aff
*ma2
)
5711 isl_space
*space
= NULL
;
5713 ma2
= isl_multi_aff_align_divs(ma2
);
5714 ma1
= isl_multi_aff_cow(ma1
);
5718 space
= isl_space_join(isl_multi_aff_get_space(ma2
),
5719 isl_multi_aff_get_space(ma1
));
5721 for (i
= 0; i
< ma1
->n
; ++i
) {
5722 ma1
->u
.p
[i
] = isl_aff_pullback_multi_aff(ma1
->u
.p
[i
],
5723 isl_multi_aff_copy(ma2
));
5728 ma1
= isl_multi_aff_reset_space(ma1
, space
);
5729 isl_multi_aff_free(ma2
);
5732 isl_space_free(space
);
5733 isl_multi_aff_free(ma2
);
5734 isl_multi_aff_free(ma1
);
5738 /* Compute the pullback of "ma1" by the function represented by "ma2".
5739 * In other words, plug in "ma2" in "ma1".
5741 __isl_give isl_multi_aff
*isl_multi_aff_pullback_multi_aff(
5742 __isl_take isl_multi_aff
*ma1
, __isl_take isl_multi_aff
*ma2
)
5744 return isl_multi_aff_align_params_multi_multi_and(ma1
, ma2
,
5745 &isl_multi_aff_pullback_multi_aff_aligned
);
5748 /* Extend the local space of "dst" to include the divs
5749 * in the local space of "src".
5751 * If "src" does not have any divs or if the local spaces of "dst" and
5752 * "src" are the same, then no extension is required.
5754 __isl_give isl_aff
*isl_aff_align_divs(__isl_take isl_aff
*dst
,
5755 __isl_keep isl_aff
*src
)
5758 int src_n_div
, dst_n_div
;
5765 return isl_aff_free(dst
);
5767 ctx
= isl_aff_get_ctx(src
);
5768 equal
= isl_local_space_has_equal_space(src
->ls
, dst
->ls
);
5770 return isl_aff_free(dst
);
5772 isl_die(ctx
, isl_error_invalid
,
5773 "spaces don't match", goto error
);
5775 src_n_div
= isl_local_space_dim(src
->ls
, isl_dim_div
);
5778 equal
= isl_local_space_is_equal(src
->ls
, dst
->ls
);
5780 return isl_aff_free(dst
);
5784 dst_n_div
= isl_local_space_dim(dst
->ls
, isl_dim_div
);
5785 exp1
= isl_alloc_array(ctx
, int, src_n_div
);
5786 exp2
= isl_alloc_array(ctx
, int, dst_n_div
);
5787 if (!exp1
|| (dst_n_div
&& !exp2
))
5790 div
= isl_merge_divs(src
->ls
->div
, dst
->ls
->div
, exp1
, exp2
);
5791 dst
= isl_aff_expand_divs(dst
, div
, exp2
);
5799 return isl_aff_free(dst
);
5802 /* Adjust the local spaces of the affine expressions in "maff"
5803 * such that they all have the save divs.
5805 __isl_give isl_multi_aff
*isl_multi_aff_align_divs(
5806 __isl_take isl_multi_aff
*maff
)
5814 maff
= isl_multi_aff_cow(maff
);
5818 for (i
= 1; i
< maff
->n
; ++i
)
5819 maff
->u
.p
[0] = isl_aff_align_divs(maff
->u
.p
[0], maff
->u
.p
[i
]);
5820 for (i
= 1; i
< maff
->n
; ++i
) {
5821 maff
->u
.p
[i
] = isl_aff_align_divs(maff
->u
.p
[i
], maff
->u
.p
[0]);
5823 return isl_multi_aff_free(maff
);
5829 __isl_give isl_aff
*isl_aff_lift(__isl_take isl_aff
*aff
)
5831 aff
= isl_aff_cow(aff
);
5835 aff
->ls
= isl_local_space_lift(aff
->ls
);
5837 return isl_aff_free(aff
);
5842 /* Lift "maff" to a space with extra dimensions such that the result
5843 * has no more existentially quantified variables.
5844 * If "ls" is not NULL, then *ls is assigned the local space that lies
5845 * at the basis of the lifting applied to "maff".
5847 __isl_give isl_multi_aff
*isl_multi_aff_lift(__isl_take isl_multi_aff
*maff
,
5848 __isl_give isl_local_space
**ls
)
5862 isl_space
*space
= isl_multi_aff_get_domain_space(maff
);
5863 *ls
= isl_local_space_from_space(space
);
5865 return isl_multi_aff_free(maff
);
5870 maff
= isl_multi_aff_cow(maff
);
5871 maff
= isl_multi_aff_align_divs(maff
);
5875 n_div
= isl_aff_dim(maff
->u
.p
[0], isl_dim_div
);
5876 space
= isl_multi_aff_get_space(maff
);
5877 space
= isl_space_lift(isl_space_domain(space
), n_div
);
5878 space
= isl_space_extend_domain_with_range(space
,
5879 isl_multi_aff_get_space(maff
));
5881 return isl_multi_aff_free(maff
);
5882 isl_space_free(maff
->space
);
5883 maff
->space
= space
;
5886 *ls
= isl_aff_get_domain_local_space(maff
->u
.p
[0]);
5888 return isl_multi_aff_free(maff
);
5891 for (i
= 0; i
< maff
->n
; ++i
) {
5892 maff
->u
.p
[i
] = isl_aff_lift(maff
->u
.p
[i
]);
5900 isl_local_space_free(*ls
);
5901 return isl_multi_aff_free(maff
);
5905 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma".
5907 __isl_give isl_pw_aff
*isl_pw_multi_aff_get_pw_aff(
5908 __isl_keep isl_pw_multi_aff
*pma
, int pos
)
5918 n_out
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
5919 if (pos
< 0 || pos
>= n_out
)
5920 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
5921 "index out of bounds", return NULL
);
5923 space
= isl_pw_multi_aff_get_space(pma
);
5924 space
= isl_space_drop_dims(space
, isl_dim_out
,
5925 pos
+ 1, n_out
- pos
- 1);
5926 space
= isl_space_drop_dims(space
, isl_dim_out
, 0, pos
);
5928 pa
= isl_pw_aff_alloc_size(space
, pma
->n
);
5929 for (i
= 0; i
< pma
->n
; ++i
) {
5931 aff
= isl_multi_aff_get_aff(pma
->p
[i
].maff
, pos
);
5932 pa
= isl_pw_aff_add_piece(pa
, isl_set_copy(pma
->p
[i
].set
), aff
);
5938 /* Return an isl_pw_multi_aff with the given "set" as domain and
5939 * an unnamed zero-dimensional range.
5941 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_domain(
5942 __isl_take isl_set
*set
)
5947 space
= isl_set_get_space(set
);
5948 space
= isl_space_from_domain(space
);
5949 ma
= isl_multi_aff_zero(space
);
5950 return isl_pw_multi_aff_alloc(set
, ma
);
5953 /* Add an isl_pw_multi_aff with the given "set" as domain and
5954 * an unnamed zero-dimensional range to *user.
5956 static isl_stat
add_pw_multi_aff_from_domain(__isl_take isl_set
*set
,
5959 isl_union_pw_multi_aff
**upma
= user
;
5960 isl_pw_multi_aff
*pma
;
5962 pma
= isl_pw_multi_aff_from_domain(set
);
5963 *upma
= isl_union_pw_multi_aff_add_pw_multi_aff(*upma
, pma
);
5968 /* Return an isl_union_pw_multi_aff with the given "uset" as domain and
5969 * an unnamed zero-dimensional range.
5971 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_domain(
5972 __isl_take isl_union_set
*uset
)
5975 isl_union_pw_multi_aff
*upma
;
5980 space
= isl_union_set_get_space(uset
);
5981 upma
= isl_union_pw_multi_aff_empty(space
);
5983 if (isl_union_set_foreach_set(uset
,
5984 &add_pw_multi_aff_from_domain
, &upma
) < 0)
5987 isl_union_set_free(uset
);
5990 isl_union_set_free(uset
);
5991 isl_union_pw_multi_aff_free(upma
);
5995 /* Convert "pma" to an isl_map and add it to *umap.
5997 static isl_stat
map_from_pw_multi_aff(__isl_take isl_pw_multi_aff
*pma
,
6000 isl_union_map
**umap
= user
;
6003 map
= isl_map_from_pw_multi_aff(pma
);
6004 *umap
= isl_union_map_add_map(*umap
, map
);
6009 /* Construct a union map mapping the domain of the union
6010 * piecewise multi-affine expression to its range, with each dimension
6011 * in the range equated to the corresponding affine expression on its cell.
6013 __isl_give isl_union_map
*isl_union_map_from_union_pw_multi_aff(
6014 __isl_take isl_union_pw_multi_aff
*upma
)
6017 isl_union_map
*umap
;
6022 space
= isl_union_pw_multi_aff_get_space(upma
);
6023 umap
= isl_union_map_empty(space
);
6025 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma
,
6026 &map_from_pw_multi_aff
, &umap
) < 0)
6029 isl_union_pw_multi_aff_free(upma
);
6032 isl_union_pw_multi_aff_free(upma
);
6033 isl_union_map_free(umap
);
6037 /* Local data for bin_entry and the callback "fn".
6039 struct isl_union_pw_multi_aff_bin_data
{
6040 isl_union_pw_multi_aff
*upma2
;
6041 isl_union_pw_multi_aff
*res
;
6042 isl_pw_multi_aff
*pma
;
6043 isl_stat (*fn
)(__isl_take isl_pw_multi_aff
*pma
, void *user
);
6046 /* Given an isl_pw_multi_aff from upma1, store it in data->pma
6047 * and call data->fn for each isl_pw_multi_aff in data->upma2.
6049 static isl_stat
bin_entry(__isl_take isl_pw_multi_aff
*pma
, void *user
)
6051 struct isl_union_pw_multi_aff_bin_data
*data
= user
;
6055 r
= isl_union_pw_multi_aff_foreach_pw_multi_aff(data
->upma2
,
6057 isl_pw_multi_aff_free(pma
);
6062 /* Call "fn" on each pair of isl_pw_multi_affs in "upma1" and "upma2".
6063 * The isl_pw_multi_aff from upma1 is stored in data->pma (where data is
6064 * passed as user field) and the isl_pw_multi_aff from upma2 is available
6065 * as *entry. The callback should adjust data->res if desired.
6067 static __isl_give isl_union_pw_multi_aff
*bin_op(
6068 __isl_take isl_union_pw_multi_aff
*upma1
,
6069 __isl_take isl_union_pw_multi_aff
*upma2
,
6070 isl_stat (*fn
)(__isl_take isl_pw_multi_aff
*pma
, void *user
))
6073 struct isl_union_pw_multi_aff_bin_data data
= { NULL
, NULL
, NULL
, fn
};
6075 space
= isl_union_pw_multi_aff_get_space(upma2
);
6076 upma1
= isl_union_pw_multi_aff_align_params(upma1
, space
);
6077 space
= isl_union_pw_multi_aff_get_space(upma1
);
6078 upma2
= isl_union_pw_multi_aff_align_params(upma2
, space
);
6080 if (!upma1
|| !upma2
)
6084 data
.res
= isl_union_pw_multi_aff_alloc_same_size(upma1
);
6085 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma1
,
6086 &bin_entry
, &data
) < 0)
6089 isl_union_pw_multi_aff_free(upma1
);
6090 isl_union_pw_multi_aff_free(upma2
);
6093 isl_union_pw_multi_aff_free(upma1
);
6094 isl_union_pw_multi_aff_free(upma2
);
6095 isl_union_pw_multi_aff_free(data
.res
);
6099 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
6100 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
6102 static __isl_give isl_pw_multi_aff
*pw_multi_aff_range_product(
6103 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
6107 space
= isl_space_range_product(isl_pw_multi_aff_get_space(pma1
),
6108 isl_pw_multi_aff_get_space(pma2
));
6109 return isl_pw_multi_aff_on_shared_domain_in(pma1
, pma2
, space
,
6110 &isl_multi_aff_range_product
);
6113 /* Given two isl_pw_multi_affs A -> B and C -> D,
6114 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
6116 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_range_product(
6117 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
6119 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
6120 &pw_multi_aff_range_product
);
6123 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
6124 * construct an isl_pw_multi_aff (A * C) -> (B, D).
6126 static __isl_give isl_pw_multi_aff
*pw_multi_aff_flat_range_product(
6127 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
6131 space
= isl_space_range_product(isl_pw_multi_aff_get_space(pma1
),
6132 isl_pw_multi_aff_get_space(pma2
));
6133 space
= isl_space_flatten_range(space
);
6134 return isl_pw_multi_aff_on_shared_domain_in(pma1
, pma2
, space
,
6135 &isl_multi_aff_flat_range_product
);
6138 /* Given two isl_pw_multi_affs A -> B and C -> D,
6139 * construct an isl_pw_multi_aff (A * C) -> (B, D).
6141 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_flat_range_product(
6142 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
6144 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
6145 &pw_multi_aff_flat_range_product
);
6148 /* If data->pma and "pma2" have the same domain space, then compute
6149 * their flat range product and the result to data->res.
6151 static isl_stat
flat_range_product_entry(__isl_take isl_pw_multi_aff
*pma2
,
6154 struct isl_union_pw_multi_aff_bin_data
*data
= user
;
6156 if (!isl_space_tuple_is_equal(data
->pma
->dim
, isl_dim_in
,
6157 pma2
->dim
, isl_dim_in
)) {
6158 isl_pw_multi_aff_free(pma2
);
6162 pma2
= isl_pw_multi_aff_flat_range_product(
6163 isl_pw_multi_aff_copy(data
->pma
), pma2
);
6165 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma2
);
6170 /* Given two isl_union_pw_multi_affs A -> B and C -> D,
6171 * construct an isl_union_pw_multi_aff (A * C) -> (B, D).
6173 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_flat_range_product(
6174 __isl_take isl_union_pw_multi_aff
*upma1
,
6175 __isl_take isl_union_pw_multi_aff
*upma2
)
6177 return bin_op(upma1
, upma2
, &flat_range_product_entry
);
6180 /* Replace the affine expressions at position "pos" in "pma" by "pa".
6181 * The parameters are assumed to have been aligned.
6183 * The implementation essentially performs an isl_pw_*_on_shared_domain,
6184 * except that it works on two different isl_pw_* types.
6186 static __isl_give isl_pw_multi_aff
*pw_multi_aff_set_pw_aff(
6187 __isl_take isl_pw_multi_aff
*pma
, unsigned pos
,
6188 __isl_take isl_pw_aff
*pa
)
6191 isl_pw_multi_aff
*res
= NULL
;
6196 if (!isl_space_tuple_is_equal(pma
->dim
, isl_dim_in
,
6197 pa
->dim
, isl_dim_in
))
6198 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
6199 "domains don't match", goto error
);
6200 if (pos
>= isl_pw_multi_aff_dim(pma
, isl_dim_out
))
6201 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
6202 "index out of bounds", goto error
);
6205 res
= isl_pw_multi_aff_alloc_size(isl_pw_multi_aff_get_space(pma
), n
);
6207 for (i
= 0; i
< pma
->n
; ++i
) {
6208 for (j
= 0; j
< pa
->n
; ++j
) {
6210 isl_multi_aff
*res_ij
;
6213 common
= isl_set_intersect(isl_set_copy(pma
->p
[i
].set
),
6214 isl_set_copy(pa
->p
[j
].set
));
6215 empty
= isl_set_plain_is_empty(common
);
6216 if (empty
< 0 || empty
) {
6217 isl_set_free(common
);
6223 res_ij
= isl_multi_aff_set_aff(
6224 isl_multi_aff_copy(pma
->p
[i
].maff
), pos
,
6225 isl_aff_copy(pa
->p
[j
].aff
));
6226 res_ij
= isl_multi_aff_gist(res_ij
,
6227 isl_set_copy(common
));
6229 res
= isl_pw_multi_aff_add_piece(res
, common
, res_ij
);
6233 isl_pw_multi_aff_free(pma
);
6234 isl_pw_aff_free(pa
);
6237 isl_pw_multi_aff_free(pma
);
6238 isl_pw_aff_free(pa
);
6239 return isl_pw_multi_aff_free(res
);
6242 /* Replace the affine expressions at position "pos" in "pma" by "pa".
6244 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_set_pw_aff(
6245 __isl_take isl_pw_multi_aff
*pma
, unsigned pos
,
6246 __isl_take isl_pw_aff
*pa
)
6248 isl_bool equal_params
;
6252 equal_params
= isl_space_has_equal_params(pma
->dim
, pa
->dim
);
6253 if (equal_params
< 0)
6256 return pw_multi_aff_set_pw_aff(pma
, pos
, pa
);
6257 if (isl_pw_multi_aff_check_named_params(pma
) < 0 ||
6258 isl_pw_aff_check_named_params(pa
) < 0)
6260 pma
= isl_pw_multi_aff_align_params(pma
, isl_pw_aff_get_space(pa
));
6261 pa
= isl_pw_aff_align_params(pa
, isl_pw_multi_aff_get_space(pma
));
6262 return pw_multi_aff_set_pw_aff(pma
, pos
, pa
);
6264 isl_pw_multi_aff_free(pma
);
6265 isl_pw_aff_free(pa
);
6269 /* Do the parameters of "pa" match those of "space"?
6271 isl_bool
isl_pw_aff_matching_params(__isl_keep isl_pw_aff
*pa
,
6272 __isl_keep isl_space
*space
)
6274 isl_space
*pa_space
;
6278 return isl_bool_error
;
6280 pa_space
= isl_pw_aff_get_space(pa
);
6282 match
= isl_space_has_equal_params(space
, pa_space
);
6284 isl_space_free(pa_space
);
6288 /* Check that the domain space of "pa" matches "space".
6290 isl_stat
isl_pw_aff_check_match_domain_space(__isl_keep isl_pw_aff
*pa
,
6291 __isl_keep isl_space
*space
)
6293 isl_space
*pa_space
;
6297 return isl_stat_error
;
6299 pa_space
= isl_pw_aff_get_space(pa
);
6301 match
= isl_space_has_equal_params(space
, pa_space
);
6305 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
6306 "parameters don't match", goto error
);
6307 match
= isl_space_tuple_is_equal(space
, isl_dim_in
,
6308 pa_space
, isl_dim_in
);
6312 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
6313 "domains don't match", goto error
);
6314 isl_space_free(pa_space
);
6317 isl_space_free(pa_space
);
6318 return isl_stat_error
;
6326 #include <isl_multi_explicit_domain.c>
6327 #include <isl_multi_pw_aff_explicit_domain.c>
6328 #include <isl_multi_templ.c>
6329 #include <isl_multi_apply_set.c>
6330 #include <isl_multi_coalesce.c>
6331 #include <isl_multi_dims.c>
6332 #include <isl_multi_gist.c>
6333 #include <isl_multi_hash.c>
6334 #include <isl_multi_align_set.c>
6335 #include <isl_multi_intersect.c>
6337 /* Does "mpa" have a non-trivial explicit domain?
6339 * The explicit domain, if present, is trivial if it represents
6340 * an (obviously) universe set.
6342 isl_bool
isl_multi_pw_aff_has_non_trivial_domain(
6343 __isl_keep isl_multi_pw_aff
*mpa
)
6346 return isl_bool_error
;
6347 if (!isl_multi_pw_aff_has_explicit_domain(mpa
))
6348 return isl_bool_false
;
6349 return isl_bool_not(isl_set_plain_is_universe(mpa
->u
.dom
));
6352 /* Scale the elements of "pma" by the corresponding elements of "mv".
6354 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_scale_multi_val(
6355 __isl_take isl_pw_multi_aff
*pma
, __isl_take isl_multi_val
*mv
)
6358 isl_bool equal_params
;
6360 pma
= isl_pw_multi_aff_cow(pma
);
6363 if (!isl_space_tuple_is_equal(pma
->dim
, isl_dim_out
,
6364 mv
->space
, isl_dim_set
))
6365 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
6366 "spaces don't match", goto error
);
6367 equal_params
= isl_space_has_equal_params(pma
->dim
, mv
->space
);
6368 if (equal_params
< 0)
6370 if (!equal_params
) {
6371 pma
= isl_pw_multi_aff_align_params(pma
,
6372 isl_multi_val_get_space(mv
));
6373 mv
= isl_multi_val_align_params(mv
,
6374 isl_pw_multi_aff_get_space(pma
));
6379 for (i
= 0; i
< pma
->n
; ++i
) {
6380 pma
->p
[i
].maff
= isl_multi_aff_scale_multi_val(pma
->p
[i
].maff
,
6381 isl_multi_val_copy(mv
));
6382 if (!pma
->p
[i
].maff
)
6386 isl_multi_val_free(mv
);
6389 isl_multi_val_free(mv
);
6390 isl_pw_multi_aff_free(pma
);
6394 /* This function is called for each entry of an isl_union_pw_multi_aff.
6395 * If the space of the entry matches that of data->mv,
6396 * then apply isl_pw_multi_aff_scale_multi_val and return the result.
6397 * Otherwise, return an empty isl_pw_multi_aff.
6399 static __isl_give isl_pw_multi_aff
*union_pw_multi_aff_scale_multi_val_entry(
6400 __isl_take isl_pw_multi_aff
*pma
, void *user
)
6402 isl_multi_val
*mv
= user
;
6406 if (!isl_space_tuple_is_equal(pma
->dim
, isl_dim_out
,
6407 mv
->space
, isl_dim_set
)) {
6408 isl_space
*space
= isl_pw_multi_aff_get_space(pma
);
6409 isl_pw_multi_aff_free(pma
);
6410 return isl_pw_multi_aff_empty(space
);
6413 return isl_pw_multi_aff_scale_multi_val(pma
, isl_multi_val_copy(mv
));
6416 /* Scale the elements of "upma" by the corresponding elements of "mv",
6417 * for those entries that match the space of "mv".
6419 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_scale_multi_val(
6420 __isl_take isl_union_pw_multi_aff
*upma
, __isl_take isl_multi_val
*mv
)
6422 upma
= isl_union_pw_multi_aff_align_params(upma
,
6423 isl_multi_val_get_space(mv
));
6424 mv
= isl_multi_val_align_params(mv
,
6425 isl_union_pw_multi_aff_get_space(upma
));
6429 return isl_union_pw_multi_aff_transform(upma
,
6430 &union_pw_multi_aff_scale_multi_val_entry
, mv
);
6432 isl_multi_val_free(mv
);
6435 isl_multi_val_free(mv
);
6436 isl_union_pw_multi_aff_free(upma
);
6440 /* Construct and return a piecewise multi affine expression
6441 * in the given space with value zero in each of the output dimensions and
6442 * a universe domain.
6444 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_zero(__isl_take isl_space
*space
)
6446 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_zero(space
));
6449 /* Construct and return a piecewise multi affine expression
6450 * that is equal to the given piecewise affine expression.
6452 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_pw_aff(
6453 __isl_take isl_pw_aff
*pa
)
6457 isl_pw_multi_aff
*pma
;
6462 space
= isl_pw_aff_get_space(pa
);
6463 pma
= isl_pw_multi_aff_alloc_size(space
, pa
->n
);
6465 for (i
= 0; i
< pa
->n
; ++i
) {
6469 set
= isl_set_copy(pa
->p
[i
].set
);
6470 ma
= isl_multi_aff_from_aff(isl_aff_copy(pa
->p
[i
].aff
));
6471 pma
= isl_pw_multi_aff_add_piece(pma
, set
, ma
);
6474 isl_pw_aff_free(pa
);
6478 /* Construct a set or map mapping the shared (parameter) domain
6479 * of the piecewise affine expressions to the range of "mpa"
6480 * with each dimension in the range equated to the
6481 * corresponding piecewise affine expression.
6483 static __isl_give isl_map
*map_from_multi_pw_aff(
6484 __isl_take isl_multi_pw_aff
*mpa
)
6493 if (isl_space_dim(mpa
->space
, isl_dim_out
) != mpa
->n
)
6494 isl_die(isl_multi_pw_aff_get_ctx(mpa
), isl_error_internal
,
6495 "invalid space", goto error
);
6497 space
= isl_multi_pw_aff_get_domain_space(mpa
);
6498 map
= isl_map_universe(isl_space_from_domain(space
));
6500 for (i
= 0; i
< mpa
->n
; ++i
) {
6504 pa
= isl_pw_aff_copy(mpa
->u
.p
[i
]);
6505 map_i
= map_from_pw_aff(pa
);
6507 map
= isl_map_flat_range_product(map
, map_i
);
6510 map
= isl_map_reset_space(map
, isl_multi_pw_aff_get_space(mpa
));
6512 isl_multi_pw_aff_free(mpa
);
6515 isl_multi_pw_aff_free(mpa
);
6519 /* Construct a map mapping the shared domain
6520 * of the piecewise affine expressions to the range of "mpa"
6521 * with each dimension in the range equated to the
6522 * corresponding piecewise affine expression.
6524 __isl_give isl_map
*isl_map_from_multi_pw_aff(__isl_take isl_multi_pw_aff
*mpa
)
6528 if (isl_space_is_set(mpa
->space
))
6529 isl_die(isl_multi_pw_aff_get_ctx(mpa
), isl_error_internal
,
6530 "space of input is not a map", goto error
);
6532 return map_from_multi_pw_aff(mpa
);
6534 isl_multi_pw_aff_free(mpa
);
6538 /* Construct a set mapping the shared parameter domain
6539 * of the piecewise affine expressions to the space of "mpa"
6540 * with each dimension in the range equated to the
6541 * corresponding piecewise affine expression.
6543 __isl_give isl_set
*isl_set_from_multi_pw_aff(__isl_take isl_multi_pw_aff
*mpa
)
6547 if (!isl_space_is_set(mpa
->space
))
6548 isl_die(isl_multi_pw_aff_get_ctx(mpa
), isl_error_internal
,
6549 "space of input is not a set", goto error
);
6551 return map_from_multi_pw_aff(mpa
);
6553 isl_multi_pw_aff_free(mpa
);
6557 /* Construct and return a piecewise multi affine expression
6558 * that is equal to the given multi piecewise affine expression
6559 * on the shared domain of the piecewise affine expressions,
6560 * in the special case of a 0D multi piecewise affine expression.
6562 * Create a piecewise multi affine expression with the explicit domain of
6563 * the 0D multi piecewise affine expression as domain.
6565 static __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_multi_pw_aff_0D(
6566 __isl_take isl_multi_pw_aff
*mpa
)
6572 space
= isl_multi_pw_aff_get_space(mpa
);
6573 dom
= isl_multi_pw_aff_get_explicit_domain(mpa
);
6574 isl_multi_pw_aff_free(mpa
);
6576 ma
= isl_multi_aff_zero(space
);
6577 return isl_pw_multi_aff_alloc(dom
, ma
);
6580 /* Construct and return a piecewise multi affine expression
6581 * that is equal to the given multi piecewise affine expression
6582 * on the shared domain of the piecewise affine expressions.
6584 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_multi_pw_aff(
6585 __isl_take isl_multi_pw_aff
*mpa
)
6590 isl_pw_multi_aff
*pma
;
6596 return isl_pw_multi_aff_from_multi_pw_aff_0D(mpa
);
6598 space
= isl_multi_pw_aff_get_space(mpa
);
6599 pa
= isl_multi_pw_aff_get_pw_aff(mpa
, 0);
6600 pma
= isl_pw_multi_aff_from_pw_aff(pa
);
6602 for (i
= 1; i
< mpa
->n
; ++i
) {
6603 isl_pw_multi_aff
*pma_i
;
6605 pa
= isl_multi_pw_aff_get_pw_aff(mpa
, i
);
6606 pma_i
= isl_pw_multi_aff_from_pw_aff(pa
);
6607 pma
= isl_pw_multi_aff_range_product(pma
, pma_i
);
6610 pma
= isl_pw_multi_aff_reset_space(pma
, space
);
6612 isl_multi_pw_aff_free(mpa
);
6616 /* Construct and return a multi piecewise affine expression
6617 * that is equal to the given multi affine expression.
6619 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_from_multi_aff(
6620 __isl_take isl_multi_aff
*ma
)
6623 isl_multi_pw_aff
*mpa
;
6628 n
= isl_multi_aff_dim(ma
, isl_dim_out
);
6629 mpa
= isl_multi_pw_aff_alloc(isl_multi_aff_get_space(ma
));
6631 for (i
= 0; i
< n
; ++i
) {
6634 pa
= isl_pw_aff_from_aff(isl_multi_aff_get_aff(ma
, i
));
6635 mpa
= isl_multi_pw_aff_set_pw_aff(mpa
, i
, pa
);
6638 isl_multi_aff_free(ma
);
6642 /* Construct and return a multi piecewise affine expression
6643 * that is equal to the given piecewise multi affine expression.
6645 * If the resulting multi piecewise affine expression has
6646 * an explicit domain, then assign it the domain of the input.
6647 * In other cases, the domain is stored in the individual elements.
6649 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_from_pw_multi_aff(
6650 __isl_take isl_pw_multi_aff
*pma
)
6654 isl_multi_pw_aff
*mpa
;
6659 n
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
6660 space
= isl_pw_multi_aff_get_space(pma
);
6661 mpa
= isl_multi_pw_aff_alloc(space
);
6663 for (i
= 0; i
< n
; ++i
) {
6666 pa
= isl_pw_multi_aff_get_pw_aff(pma
, i
);
6667 mpa
= isl_multi_pw_aff_set_pw_aff(mpa
, i
, pa
);
6669 if (isl_multi_pw_aff_has_explicit_domain(mpa
)) {
6672 dom
= isl_pw_multi_aff_domain(isl_pw_multi_aff_copy(pma
));
6673 mpa
= isl_multi_pw_aff_intersect_domain(mpa
, dom
);
6676 isl_pw_multi_aff_free(pma
);
6680 /* Do "pa1" and "pa2" represent the same function?
6682 * We first check if they are obviously equal.
6683 * If not, we convert them to maps and check if those are equal.
6685 * If "pa1" or "pa2" contain any NaNs, then they are considered
6686 * not to be the same. A NaN is not equal to anything, not even
6689 isl_bool
isl_pw_aff_is_equal(__isl_keep isl_pw_aff
*pa1
,
6690 __isl_keep isl_pw_aff
*pa2
)
6694 isl_map
*map1
, *map2
;
6697 return isl_bool_error
;
6699 equal
= isl_pw_aff_plain_is_equal(pa1
, pa2
);
6700 if (equal
< 0 || equal
)
6702 has_nan
= either_involves_nan(pa1
, pa2
);
6704 return isl_bool_error
;
6706 return isl_bool_false
;
6708 map1
= map_from_pw_aff(isl_pw_aff_copy(pa1
));
6709 map2
= map_from_pw_aff(isl_pw_aff_copy(pa2
));
6710 equal
= isl_map_is_equal(map1
, map2
);
6717 /* Do "mpa1" and "mpa2" represent the same function?
6719 * Note that we cannot convert the entire isl_multi_pw_aff
6720 * to a map because the domains of the piecewise affine expressions
6721 * may not be the same.
6723 isl_bool
isl_multi_pw_aff_is_equal(__isl_keep isl_multi_pw_aff
*mpa1
,
6724 __isl_keep isl_multi_pw_aff
*mpa2
)
6727 isl_bool equal
, equal_params
;
6730 return isl_bool_error
;
6732 equal_params
= isl_space_has_equal_params(mpa1
->space
, mpa2
->space
);
6733 if (equal_params
< 0)
6734 return isl_bool_error
;
6735 if (!equal_params
) {
6736 if (!isl_space_has_named_params(mpa1
->space
))
6737 return isl_bool_false
;
6738 if (!isl_space_has_named_params(mpa2
->space
))
6739 return isl_bool_false
;
6740 mpa1
= isl_multi_pw_aff_copy(mpa1
);
6741 mpa2
= isl_multi_pw_aff_copy(mpa2
);
6742 mpa1
= isl_multi_pw_aff_align_params(mpa1
,
6743 isl_multi_pw_aff_get_space(mpa2
));
6744 mpa2
= isl_multi_pw_aff_align_params(mpa2
,
6745 isl_multi_pw_aff_get_space(mpa1
));
6746 equal
= isl_multi_pw_aff_is_equal(mpa1
, mpa2
);
6747 isl_multi_pw_aff_free(mpa1
);
6748 isl_multi_pw_aff_free(mpa2
);
6752 equal
= isl_space_is_equal(mpa1
->space
, mpa2
->space
);
6753 if (equal
< 0 || !equal
)
6756 for (i
= 0; i
< mpa1
->n
; ++i
) {
6757 equal
= isl_pw_aff_is_equal(mpa1
->u
.p
[i
], mpa2
->u
.p
[i
]);
6758 if (equal
< 0 || !equal
)
6762 return isl_bool_true
;
6765 /* Do "pma1" and "pma2" represent the same function?
6767 * First check if they are obviously equal.
6768 * If not, then convert them to maps and check if those are equal.
6770 * If "pa1" or "pa2" contain any NaNs, then they are considered
6771 * not to be the same. A NaN is not equal to anything, not even
6774 isl_bool
isl_pw_multi_aff_is_equal(__isl_keep isl_pw_multi_aff
*pma1
,
6775 __isl_keep isl_pw_multi_aff
*pma2
)
6779 isl_map
*map1
, *map2
;
6782 return isl_bool_error
;
6784 equal
= isl_pw_multi_aff_plain_is_equal(pma1
, pma2
);
6785 if (equal
< 0 || equal
)
6787 has_nan
= isl_pw_multi_aff_involves_nan(pma1
);
6788 if (has_nan
>= 0 && !has_nan
)
6789 has_nan
= isl_pw_multi_aff_involves_nan(pma2
);
6790 if (has_nan
< 0 || has_nan
)
6791 return isl_bool_not(has_nan
);
6793 map1
= isl_map_from_pw_multi_aff(isl_pw_multi_aff_copy(pma1
));
6794 map2
= isl_map_from_pw_multi_aff(isl_pw_multi_aff_copy(pma2
));
6795 equal
= isl_map_is_equal(map1
, map2
);
6802 /* Compute the pullback of "mpa" by the function represented by "ma".
6803 * In other words, plug in "ma" in "mpa".
6805 * The parameters of "mpa" and "ma" are assumed to have been aligned.
6807 * If "mpa" has an explicit domain, then it is this domain
6808 * that needs to undergo a pullback, i.e., a preimage.
6810 static __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_pullback_multi_aff_aligned(
6811 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_multi_aff
*ma
)
6814 isl_space
*space
= NULL
;
6816 mpa
= isl_multi_pw_aff_cow(mpa
);
6820 space
= isl_space_join(isl_multi_aff_get_space(ma
),
6821 isl_multi_pw_aff_get_space(mpa
));
6825 for (i
= 0; i
< mpa
->n
; ++i
) {
6826 mpa
->u
.p
[i
] = isl_pw_aff_pullback_multi_aff(mpa
->u
.p
[i
],
6827 isl_multi_aff_copy(ma
));
6831 if (isl_multi_pw_aff_has_explicit_domain(mpa
)) {
6832 mpa
->u
.dom
= isl_set_preimage_multi_aff(mpa
->u
.dom
,
6833 isl_multi_aff_copy(ma
));
6838 isl_multi_aff_free(ma
);
6839 isl_space_free(mpa
->space
);
6843 isl_space_free(space
);
6844 isl_multi_pw_aff_free(mpa
);
6845 isl_multi_aff_free(ma
);
6849 /* Compute the pullback of "mpa" by the function represented by "ma".
6850 * In other words, plug in "ma" in "mpa".
6852 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_pullback_multi_aff(
6853 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_multi_aff
*ma
)
6855 isl_bool equal_params
;
6859 equal_params
= isl_space_has_equal_params(mpa
->space
, ma
->space
);
6860 if (equal_params
< 0)
6863 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa
, ma
);
6864 mpa
= isl_multi_pw_aff_align_params(mpa
, isl_multi_aff_get_space(ma
));
6865 ma
= isl_multi_aff_align_params(ma
, isl_multi_pw_aff_get_space(mpa
));
6866 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa
, ma
);
6868 isl_multi_pw_aff_free(mpa
);
6869 isl_multi_aff_free(ma
);
6873 /* Compute the pullback of "mpa" by the function represented by "pma".
6874 * In other words, plug in "pma" in "mpa".
6876 * The parameters of "mpa" and "mpa" are assumed to have been aligned.
6878 * If "mpa" has an explicit domain, then it is this domain
6879 * that needs to undergo a pullback, i.e., a preimage.
6881 static __isl_give isl_multi_pw_aff
*
6882 isl_multi_pw_aff_pullback_pw_multi_aff_aligned(
6883 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_pw_multi_aff
*pma
)
6886 isl_space
*space
= NULL
;
6888 mpa
= isl_multi_pw_aff_cow(mpa
);
6892 space
= isl_space_join(isl_pw_multi_aff_get_space(pma
),
6893 isl_multi_pw_aff_get_space(mpa
));
6895 for (i
= 0; i
< mpa
->n
; ++i
) {
6896 mpa
->u
.p
[i
] = isl_pw_aff_pullback_pw_multi_aff_aligned(
6897 mpa
->u
.p
[i
], isl_pw_multi_aff_copy(pma
));
6901 if (isl_multi_pw_aff_has_explicit_domain(mpa
)) {
6902 mpa
->u
.dom
= isl_set_preimage_pw_multi_aff(mpa
->u
.dom
,
6903 isl_pw_multi_aff_copy(pma
));
6908 isl_pw_multi_aff_free(pma
);
6909 isl_space_free(mpa
->space
);
6913 isl_space_free(space
);
6914 isl_multi_pw_aff_free(mpa
);
6915 isl_pw_multi_aff_free(pma
);
6919 /* Compute the pullback of "mpa" by the function represented by "pma".
6920 * In other words, plug in "pma" in "mpa".
6922 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_pullback_pw_multi_aff(
6923 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_pw_multi_aff
*pma
)
6925 isl_bool equal_params
;
6929 equal_params
= isl_space_has_equal_params(mpa
->space
, pma
->dim
);
6930 if (equal_params
< 0)
6933 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa
, pma
);
6934 mpa
= isl_multi_pw_aff_align_params(mpa
,
6935 isl_pw_multi_aff_get_space(pma
));
6936 pma
= isl_pw_multi_aff_align_params(pma
,
6937 isl_multi_pw_aff_get_space(mpa
));
6938 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa
, pma
);
6940 isl_multi_pw_aff_free(mpa
);
6941 isl_pw_multi_aff_free(pma
);
6945 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
6946 * with the domain of "aff". The domain of the result is the same
6948 * "mpa" and "aff" are assumed to have been aligned.
6950 * We first extract the parametric constant from "aff", defined
6951 * over the correct domain.
6952 * Then we add the appropriate combinations of the members of "mpa".
6953 * Finally, we add the integer divisions through recursive calls.
6955 static __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_aff_aligned(
6956 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_aff
*aff
)
6964 n_in
= isl_aff_dim(aff
, isl_dim_in
);
6965 n_div
= isl_aff_dim(aff
, isl_dim_div
);
6967 space
= isl_space_domain(isl_multi_pw_aff_get_space(mpa
));
6968 tmp
= isl_aff_copy(aff
);
6969 tmp
= isl_aff_drop_dims(tmp
, isl_dim_div
, 0, n_div
);
6970 tmp
= isl_aff_drop_dims(tmp
, isl_dim_in
, 0, n_in
);
6971 tmp
= isl_aff_add_dims(tmp
, isl_dim_in
,
6972 isl_space_dim(space
, isl_dim_set
));
6973 tmp
= isl_aff_reset_domain_space(tmp
, space
);
6974 pa
= isl_pw_aff_from_aff(tmp
);
6976 for (i
= 0; i
< n_in
; ++i
) {
6979 if (!isl_aff_involves_dims(aff
, isl_dim_in
, i
, 1))
6981 v
= isl_aff_get_coefficient_val(aff
, isl_dim_in
, i
);
6982 pa_i
= isl_multi_pw_aff_get_pw_aff(mpa
, i
);
6983 pa_i
= isl_pw_aff_scale_val(pa_i
, v
);
6984 pa
= isl_pw_aff_add(pa
, pa_i
);
6987 for (i
= 0; i
< n_div
; ++i
) {
6991 if (!isl_aff_involves_dims(aff
, isl_dim_div
, i
, 1))
6993 div
= isl_aff_get_div(aff
, i
);
6994 pa_i
= isl_multi_pw_aff_apply_aff_aligned(
6995 isl_multi_pw_aff_copy(mpa
), div
);
6996 pa_i
= isl_pw_aff_floor(pa_i
);
6997 v
= isl_aff_get_coefficient_val(aff
, isl_dim_div
, i
);
6998 pa_i
= isl_pw_aff_scale_val(pa_i
, v
);
6999 pa
= isl_pw_aff_add(pa
, pa_i
);
7002 isl_multi_pw_aff_free(mpa
);
7008 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
7009 * with the domain of "aff". The domain of the result is the same
7012 __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_aff(
7013 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_aff
*aff
)
7015 isl_bool equal_params
;
7019 equal_params
= isl_space_has_equal_params(aff
->ls
->dim
, mpa
->space
);
7020 if (equal_params
< 0)
7023 return isl_multi_pw_aff_apply_aff_aligned(mpa
, aff
);
7025 aff
= isl_aff_align_params(aff
, isl_multi_pw_aff_get_space(mpa
));
7026 mpa
= isl_multi_pw_aff_align_params(mpa
, isl_aff_get_space(aff
));
7028 return isl_multi_pw_aff_apply_aff_aligned(mpa
, aff
);
7031 isl_multi_pw_aff_free(mpa
);
7035 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
7036 * with the domain of "pa". The domain of the result is the same
7038 * "mpa" and "pa" are assumed to have been aligned.
7040 * We consider each piece in turn. Note that the domains of the
7041 * pieces are assumed to be disjoint and they remain disjoint
7042 * after taking the preimage (over the same function).
7044 static __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_pw_aff_aligned(
7045 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_pw_aff
*pa
)
7054 space
= isl_space_join(isl_multi_pw_aff_get_space(mpa
),
7055 isl_pw_aff_get_space(pa
));
7056 res
= isl_pw_aff_empty(space
);
7058 for (i
= 0; i
< pa
->n
; ++i
) {
7062 pa_i
= isl_multi_pw_aff_apply_aff_aligned(
7063 isl_multi_pw_aff_copy(mpa
),
7064 isl_aff_copy(pa
->p
[i
].aff
));
7065 domain
= isl_set_copy(pa
->p
[i
].set
);
7066 domain
= isl_set_preimage_multi_pw_aff(domain
,
7067 isl_multi_pw_aff_copy(mpa
));
7068 pa_i
= isl_pw_aff_intersect_domain(pa_i
, domain
);
7069 res
= isl_pw_aff_add_disjoint(res
, pa_i
);
7072 isl_pw_aff_free(pa
);
7073 isl_multi_pw_aff_free(mpa
);
7076 isl_pw_aff_free(pa
);
7077 isl_multi_pw_aff_free(mpa
);
7081 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
7082 * with the domain of "pa". The domain of the result is the same
7085 __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_pw_aff(
7086 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_pw_aff
*pa
)
7088 isl_bool equal_params
;
7092 equal_params
= isl_space_has_equal_params(pa
->dim
, mpa
->space
);
7093 if (equal_params
< 0)
7096 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa
, pa
);
7098 pa
= isl_pw_aff_align_params(pa
, isl_multi_pw_aff_get_space(mpa
));
7099 mpa
= isl_multi_pw_aff_align_params(mpa
, isl_pw_aff_get_space(pa
));
7101 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa
, pa
);
7103 isl_pw_aff_free(pa
);
7104 isl_multi_pw_aff_free(mpa
);
7108 /* Compute the pullback of "pa" by the function represented by "mpa".
7109 * In other words, plug in "mpa" in "pa".
7110 * "pa" and "mpa" are assumed to have been aligned.
7112 * The pullback is computed by applying "pa" to "mpa".
7114 static __isl_give isl_pw_aff
*isl_pw_aff_pullback_multi_pw_aff_aligned(
7115 __isl_take isl_pw_aff
*pa
, __isl_take isl_multi_pw_aff
*mpa
)
7117 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa
, pa
);
7120 /* Compute the pullback of "pa" by the function represented by "mpa".
7121 * In other words, plug in "mpa" in "pa".
7123 * The pullback is computed by applying "pa" to "mpa".
7125 __isl_give isl_pw_aff
*isl_pw_aff_pullback_multi_pw_aff(
7126 __isl_take isl_pw_aff
*pa
, __isl_take isl_multi_pw_aff
*mpa
)
7128 return isl_multi_pw_aff_apply_pw_aff(mpa
, pa
);
7131 /* Compute the pullback of "mpa1" by the function represented by "mpa2".
7132 * In other words, plug in "mpa2" in "mpa1".
7134 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7136 * We pullback each member of "mpa1" in turn.
7138 * If "mpa1" has an explicit domain, then it is this domain
7139 * that needs to undergo a pullback instead, i.e., a preimage.
7141 static __isl_give isl_multi_pw_aff
*
7142 isl_multi_pw_aff_pullback_multi_pw_aff_aligned(
7143 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
)
7146 isl_space
*space
= NULL
;
7148 mpa1
= isl_multi_pw_aff_cow(mpa1
);
7152 space
= isl_space_join(isl_multi_pw_aff_get_space(mpa2
),
7153 isl_multi_pw_aff_get_space(mpa1
));
7155 for (i
= 0; i
< mpa1
->n
; ++i
) {
7156 mpa1
->u
.p
[i
] = isl_pw_aff_pullback_multi_pw_aff_aligned(
7157 mpa1
->u
.p
[i
], isl_multi_pw_aff_copy(mpa2
));
7162 if (isl_multi_pw_aff_has_explicit_domain(mpa1
)) {
7163 mpa1
->u
.dom
= isl_set_preimage_multi_pw_aff(mpa1
->u
.dom
,
7164 isl_multi_pw_aff_copy(mpa2
));
7168 mpa1
= isl_multi_pw_aff_reset_space(mpa1
, space
);
7170 isl_multi_pw_aff_free(mpa2
);
7173 isl_space_free(space
);
7174 isl_multi_pw_aff_free(mpa1
);
7175 isl_multi_pw_aff_free(mpa2
);
7179 /* Compute the pullback of "mpa1" by the function represented by "mpa2".
7180 * In other words, plug in "mpa2" in "mpa1".
7182 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_pullback_multi_pw_aff(
7183 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
)
7185 return isl_multi_pw_aff_align_params_multi_multi_and(mpa1
, mpa2
,
7186 &isl_multi_pw_aff_pullback_multi_pw_aff_aligned
);
7189 /* Align the parameters of "mpa1" and "mpa2", check that the ranges
7190 * of "mpa1" and "mpa2" live in the same space, construct map space
7191 * between the domain spaces of "mpa1" and "mpa2" and call "order"
7192 * with this map space as extract argument.
7194 static __isl_give isl_map
*isl_multi_pw_aff_order_map(
7195 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
,
7196 __isl_give isl_map
*(*order
)(__isl_keep isl_multi_pw_aff
*mpa1
,
7197 __isl_keep isl_multi_pw_aff
*mpa2
, __isl_take isl_space
*space
))
7200 isl_space
*space1
, *space2
;
7203 mpa1
= isl_multi_pw_aff_align_params(mpa1
,
7204 isl_multi_pw_aff_get_space(mpa2
));
7205 mpa2
= isl_multi_pw_aff_align_params(mpa2
,
7206 isl_multi_pw_aff_get_space(mpa1
));
7209 match
= isl_space_tuple_is_equal(mpa1
->space
, isl_dim_out
,
7210 mpa2
->space
, isl_dim_out
);
7214 isl_die(isl_multi_pw_aff_get_ctx(mpa1
), isl_error_invalid
,
7215 "range spaces don't match", goto error
);
7216 space1
= isl_space_domain(isl_multi_pw_aff_get_space(mpa1
));
7217 space2
= isl_space_domain(isl_multi_pw_aff_get_space(mpa2
));
7218 space1
= isl_space_map_from_domain_and_range(space1
, space2
);
7220 res
= order(mpa1
, mpa2
, space1
);
7221 isl_multi_pw_aff_free(mpa1
);
7222 isl_multi_pw_aff_free(mpa2
);
7225 isl_multi_pw_aff_free(mpa1
);
7226 isl_multi_pw_aff_free(mpa2
);
7230 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7231 * where the function values are equal. "space" is the space of the result.
7232 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7234 * "mpa1" and "mpa2" are equal when each of the pairs of elements
7235 * in the sequences are equal.
7237 static __isl_give isl_map
*isl_multi_pw_aff_eq_map_on_space(
7238 __isl_keep isl_multi_pw_aff
*mpa1
, __isl_keep isl_multi_pw_aff
*mpa2
,
7239 __isl_take isl_space
*space
)
7244 res
= isl_map_universe(space
);
7246 n
= isl_multi_pw_aff_dim(mpa1
, isl_dim_out
);
7247 for (i
= 0; i
< n
; ++i
) {
7248 isl_pw_aff
*pa1
, *pa2
;
7251 pa1
= isl_multi_pw_aff_get_pw_aff(mpa1
, i
);
7252 pa2
= isl_multi_pw_aff_get_pw_aff(mpa2
, i
);
7253 map
= isl_pw_aff_eq_map(pa1
, pa2
);
7254 res
= isl_map_intersect(res
, map
);
7260 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7261 * where the function values are equal.
7263 __isl_give isl_map
*isl_multi_pw_aff_eq_map(__isl_take isl_multi_pw_aff
*mpa1
,
7264 __isl_take isl_multi_pw_aff
*mpa2
)
7266 return isl_multi_pw_aff_order_map(mpa1
, mpa2
,
7267 &isl_multi_pw_aff_eq_map_on_space
);
7270 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7271 * where the function values of "mpa1" is lexicographically satisfies "base"
7272 * compared to that of "mpa2". "space" is the space of the result.
7273 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7275 * "mpa1" lexicographically satisfies "base" compared to "mpa2"
7276 * if its i-th element satisfies "base" when compared to
7277 * the i-th element of "mpa2" while all previous elements are
7280 static __isl_give isl_map
*isl_multi_pw_aff_lex_map_on_space(
7281 __isl_keep isl_multi_pw_aff
*mpa1
, __isl_keep isl_multi_pw_aff
*mpa2
,
7282 __isl_give isl_map
*(*base
)(__isl_take isl_pw_aff
*pa1
,
7283 __isl_take isl_pw_aff
*pa2
),
7284 __isl_take isl_space
*space
)
7287 isl_map
*res
, *rest
;
7289 res
= isl_map_empty(isl_space_copy(space
));
7290 rest
= isl_map_universe(space
);
7292 n
= isl_multi_pw_aff_dim(mpa1
, isl_dim_out
);
7293 for (i
= 0; i
< n
; ++i
) {
7294 isl_pw_aff
*pa1
, *pa2
;
7297 pa1
= isl_multi_pw_aff_get_pw_aff(mpa1
, i
);
7298 pa2
= isl_multi_pw_aff_get_pw_aff(mpa2
, i
);
7299 map
= base(pa1
, pa2
);
7300 map
= isl_map_intersect(map
, isl_map_copy(rest
));
7301 res
= isl_map_union(res
, map
);
7306 pa1
= isl_multi_pw_aff_get_pw_aff(mpa1
, i
);
7307 pa2
= isl_multi_pw_aff_get_pw_aff(mpa2
, i
);
7308 map
= isl_pw_aff_eq_map(pa1
, pa2
);
7309 rest
= isl_map_intersect(rest
, map
);
7316 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7317 * where the function value of "mpa1" is lexicographically less than that
7318 * of "mpa2". "space" is the space of the result.
7319 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7321 * "mpa1" is less than "mpa2" if its i-th element is smaller
7322 * than the i-th element of "mpa2" while all previous elements are
7325 __isl_give isl_map
*isl_multi_pw_aff_lex_lt_map_on_space(
7326 __isl_keep isl_multi_pw_aff
*mpa1
, __isl_keep isl_multi_pw_aff
*mpa2
,
7327 __isl_take isl_space
*space
)
7329 return isl_multi_pw_aff_lex_map_on_space(mpa1
, mpa2
,
7330 &isl_pw_aff_lt_map
, space
);
7333 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7334 * where the function value of "mpa1" is lexicographically less than that
7337 __isl_give isl_map
*isl_multi_pw_aff_lex_lt_map(
7338 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
)
7340 return isl_multi_pw_aff_order_map(mpa1
, mpa2
,
7341 &isl_multi_pw_aff_lex_lt_map_on_space
);
7344 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7345 * where the function value of "mpa1" is lexicographically greater than that
7346 * of "mpa2". "space" is the space of the result.
7347 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7349 * "mpa1" is greater than "mpa2" if its i-th element is greater
7350 * than the i-th element of "mpa2" while all previous elements are
7353 __isl_give isl_map
*isl_multi_pw_aff_lex_gt_map_on_space(
7354 __isl_keep isl_multi_pw_aff
*mpa1
, __isl_keep isl_multi_pw_aff
*mpa2
,
7355 __isl_take isl_space
*space
)
7357 return isl_multi_pw_aff_lex_map_on_space(mpa1
, mpa2
,
7358 &isl_pw_aff_gt_map
, space
);
7361 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7362 * where the function value of "mpa1" is lexicographically greater than that
7365 __isl_give isl_map
*isl_multi_pw_aff_lex_gt_map(
7366 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
)
7368 return isl_multi_pw_aff_order_map(mpa1
, mpa2
,
7369 &isl_multi_pw_aff_lex_gt_map_on_space
);
7372 /* Compare two isl_affs.
7374 * Return -1 if "aff1" is "smaller" than "aff2", 1 if "aff1" is "greater"
7375 * than "aff2" and 0 if they are equal.
7377 * The order is fairly arbitrary. We do consider expressions that only involve
7378 * earlier dimensions as "smaller".
7380 int isl_aff_plain_cmp(__isl_keep isl_aff
*aff1
, __isl_keep isl_aff
*aff2
)
7393 cmp
= isl_local_space_cmp(aff1
->ls
, aff2
->ls
);
7397 last1
= isl_seq_last_non_zero(aff1
->v
->el
+ 1, aff1
->v
->size
- 1);
7398 last2
= isl_seq_last_non_zero(aff2
->v
->el
+ 1, aff1
->v
->size
- 1);
7400 return last1
- last2
;
7402 return isl_seq_cmp(aff1
->v
->el
, aff2
->v
->el
, aff1
->v
->size
);
7405 /* Compare two isl_pw_affs.
7407 * Return -1 if "pa1" is "smaller" than "pa2", 1 if "pa1" is "greater"
7408 * than "pa2" and 0 if they are equal.
7410 * The order is fairly arbitrary. We do consider expressions that only involve
7411 * earlier dimensions as "smaller".
7413 int isl_pw_aff_plain_cmp(__isl_keep isl_pw_aff
*pa1
,
7414 __isl_keep isl_pw_aff
*pa2
)
7427 cmp
= isl_space_cmp(pa1
->dim
, pa2
->dim
);
7431 if (pa1
->n
!= pa2
->n
)
7432 return pa1
->n
- pa2
->n
;
7434 for (i
= 0; i
< pa1
->n
; ++i
) {
7435 cmp
= isl_set_plain_cmp(pa1
->p
[i
].set
, pa2
->p
[i
].set
);
7438 cmp
= isl_aff_plain_cmp(pa1
->p
[i
].aff
, pa2
->p
[i
].aff
);
7446 /* Return a piecewise affine expression that is equal to "v" on "domain".
7448 __isl_give isl_pw_aff
*isl_pw_aff_val_on_domain(__isl_take isl_set
*domain
,
7449 __isl_take isl_val
*v
)
7452 isl_local_space
*ls
;
7455 space
= isl_set_get_space(domain
);
7456 ls
= isl_local_space_from_space(space
);
7457 aff
= isl_aff_val_on_domain(ls
, v
);
7459 return isl_pw_aff_alloc(domain
, aff
);
7462 /* Return a multi affine expression that is equal to "mv" on domain
7465 __isl_give isl_multi_aff
*isl_multi_aff_multi_val_on_space(
7466 __isl_take isl_space
*space
, __isl_take isl_multi_val
*mv
)
7470 isl_local_space
*ls
;
7476 n
= isl_multi_val_dim(mv
, isl_dim_set
);
7477 space2
= isl_multi_val_get_space(mv
);
7478 space2
= isl_space_align_params(space2
, isl_space_copy(space
));
7479 space
= isl_space_align_params(space
, isl_space_copy(space2
));
7480 space
= isl_space_map_from_domain_and_range(space
, space2
);
7481 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
7482 ls
= isl_local_space_from_space(isl_space_domain(space
));
7483 for (i
= 0; i
< n
; ++i
) {
7487 v
= isl_multi_val_get_val(mv
, i
);
7488 aff
= isl_aff_val_on_domain(isl_local_space_copy(ls
), v
);
7489 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
7491 isl_local_space_free(ls
);
7493 isl_multi_val_free(mv
);
7496 isl_space_free(space
);
7497 isl_multi_val_free(mv
);
7501 /* Return a piecewise multi-affine expression
7502 * that is equal to "mv" on "domain".
7504 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_multi_val_on_domain(
7505 __isl_take isl_set
*domain
, __isl_take isl_multi_val
*mv
)
7510 space
= isl_set_get_space(domain
);
7511 ma
= isl_multi_aff_multi_val_on_space(space
, mv
);
7513 return isl_pw_multi_aff_alloc(domain
, ma
);
7516 /* Internal data structure for isl_union_pw_multi_aff_multi_val_on_domain.
7517 * mv is the value that should be attained on each domain set
7518 * res collects the results
7520 struct isl_union_pw_multi_aff_multi_val_on_domain_data
{
7522 isl_union_pw_multi_aff
*res
;
7525 /* Create an isl_pw_multi_aff equal to data->mv on "domain"
7526 * and add it to data->res.
7528 static isl_stat
pw_multi_aff_multi_val_on_domain(__isl_take isl_set
*domain
,
7531 struct isl_union_pw_multi_aff_multi_val_on_domain_data
*data
= user
;
7532 isl_pw_multi_aff
*pma
;
7535 mv
= isl_multi_val_copy(data
->mv
);
7536 pma
= isl_pw_multi_aff_multi_val_on_domain(domain
, mv
);
7537 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma
);
7539 return data
->res
? isl_stat_ok
: isl_stat_error
;
7542 /* Return a union piecewise multi-affine expression
7543 * that is equal to "mv" on "domain".
7545 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_multi_val_on_domain(
7546 __isl_take isl_union_set
*domain
, __isl_take isl_multi_val
*mv
)
7548 struct isl_union_pw_multi_aff_multi_val_on_domain_data data
;
7551 space
= isl_union_set_get_space(domain
);
7552 data
.res
= isl_union_pw_multi_aff_empty(space
);
7554 if (isl_union_set_foreach_set(domain
,
7555 &pw_multi_aff_multi_val_on_domain
, &data
) < 0)
7556 data
.res
= isl_union_pw_multi_aff_free(data
.res
);
7557 isl_union_set_free(domain
);
7558 isl_multi_val_free(mv
);
7562 /* Compute the pullback of data->pma by the function represented by "pma2",
7563 * provided the spaces match, and add the results to data->res.
7565 static isl_stat
pullback_entry(__isl_take isl_pw_multi_aff
*pma2
, void *user
)
7567 struct isl_union_pw_multi_aff_bin_data
*data
= user
;
7569 if (!isl_space_tuple_is_equal(data
->pma
->dim
, isl_dim_in
,
7570 pma2
->dim
, isl_dim_out
)) {
7571 isl_pw_multi_aff_free(pma2
);
7575 pma2
= isl_pw_multi_aff_pullback_pw_multi_aff(
7576 isl_pw_multi_aff_copy(data
->pma
), pma2
);
7578 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma2
);
7580 return isl_stat_error
;
7585 /* Compute the pullback of "upma1" by the function represented by "upma2".
7587 __isl_give isl_union_pw_multi_aff
*
7588 isl_union_pw_multi_aff_pullback_union_pw_multi_aff(
7589 __isl_take isl_union_pw_multi_aff
*upma1
,
7590 __isl_take isl_union_pw_multi_aff
*upma2
)
7592 return bin_op(upma1
, upma2
, &pullback_entry
);
7595 /* Check that the domain space of "upa" matches "space".
7597 * This function is called from isl_multi_union_pw_aff_set_union_pw_aff and
7598 * can in principle never fail since the space "space" is that
7599 * of the isl_multi_union_pw_aff and is a set space such that
7600 * there is no domain space to match.
7602 * We check the parameters and double-check that "space" is
7603 * indeed that of a set.
7605 static isl_stat
isl_union_pw_aff_check_match_domain_space(
7606 __isl_keep isl_union_pw_aff
*upa
, __isl_keep isl_space
*space
)
7608 isl_space
*upa_space
;
7612 return isl_stat_error
;
7614 match
= isl_space_is_set(space
);
7616 return isl_stat_error
;
7618 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
7619 "expecting set space", return -1);
7621 upa_space
= isl_union_pw_aff_get_space(upa
);
7622 match
= isl_space_has_equal_params(space
, upa_space
);
7626 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
7627 "parameters don't match", goto error
);
7629 isl_space_free(upa_space
);
7632 isl_space_free(upa_space
);
7633 return isl_stat_error
;
7636 /* Do the parameters of "upa" match those of "space"?
7638 static isl_bool
isl_union_pw_aff_matching_params(
7639 __isl_keep isl_union_pw_aff
*upa
, __isl_keep isl_space
*space
)
7641 isl_space
*upa_space
;
7645 return isl_bool_error
;
7647 upa_space
= isl_union_pw_aff_get_space(upa
);
7649 match
= isl_space_has_equal_params(space
, upa_space
);
7651 isl_space_free(upa_space
);
7655 /* Internal data structure for isl_union_pw_aff_reset_domain_space.
7656 * space represents the new parameters.
7657 * res collects the results.
7659 struct isl_union_pw_aff_reset_params_data
{
7661 isl_union_pw_aff
*res
;
7664 /* Replace the parameters of "pa" by data->space and
7665 * add the result to data->res.
7667 static isl_stat
reset_params(__isl_take isl_pw_aff
*pa
, void *user
)
7669 struct isl_union_pw_aff_reset_params_data
*data
= user
;
7672 space
= isl_pw_aff_get_space(pa
);
7673 space
= isl_space_replace_params(space
, data
->space
);
7674 pa
= isl_pw_aff_reset_space(pa
, space
);
7675 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
7677 return data
->res
? isl_stat_ok
: isl_stat_error
;
7680 /* Replace the domain space of "upa" by "space".
7681 * Since a union expression does not have a (single) domain space,
7682 * "space" is necessarily a parameter space.
7684 * Since the order and the names of the parameters determine
7685 * the hash value, we need to create a new hash table.
7687 static __isl_give isl_union_pw_aff
*isl_union_pw_aff_reset_domain_space(
7688 __isl_take isl_union_pw_aff
*upa
, __isl_take isl_space
*space
)
7690 struct isl_union_pw_aff_reset_params_data data
= { space
};
7693 match
= isl_union_pw_aff_matching_params(upa
, space
);
7695 upa
= isl_union_pw_aff_free(upa
);
7697 isl_space_free(space
);
7701 data
.res
= isl_union_pw_aff_empty(isl_space_copy(space
));
7702 if (isl_union_pw_aff_foreach_pw_aff(upa
, &reset_params
, &data
) < 0)
7703 data
.res
= isl_union_pw_aff_free(data
.res
);
7705 isl_union_pw_aff_free(upa
);
7706 isl_space_free(space
);
7710 /* Return the floor of "pa".
7712 static __isl_give isl_pw_aff
*floor_entry(__isl_take isl_pw_aff
*pa
, void *user
)
7714 return isl_pw_aff_floor(pa
);
7717 /* Given f, return floor(f).
7719 __isl_give isl_union_pw_aff
*isl_union_pw_aff_floor(
7720 __isl_take isl_union_pw_aff
*upa
)
7722 return isl_union_pw_aff_transform_inplace(upa
, &floor_entry
, NULL
);
7727 * upa mod m = upa - m * floor(upa/m)
7729 * with m an integer value.
7731 __isl_give isl_union_pw_aff
*isl_union_pw_aff_mod_val(
7732 __isl_take isl_union_pw_aff
*upa
, __isl_take isl_val
*m
)
7734 isl_union_pw_aff
*res
;
7739 if (!isl_val_is_int(m
))
7740 isl_die(isl_val_get_ctx(m
), isl_error_invalid
,
7741 "expecting integer modulo", goto error
);
7742 if (!isl_val_is_pos(m
))
7743 isl_die(isl_val_get_ctx(m
), isl_error_invalid
,
7744 "expecting positive modulo", goto error
);
7746 res
= isl_union_pw_aff_copy(upa
);
7747 upa
= isl_union_pw_aff_scale_down_val(upa
, isl_val_copy(m
));
7748 upa
= isl_union_pw_aff_floor(upa
);
7749 upa
= isl_union_pw_aff_scale_val(upa
, m
);
7750 res
= isl_union_pw_aff_sub(res
, upa
);
7755 isl_union_pw_aff_free(upa
);
7759 /* Internal data structure for isl_union_pw_multi_aff_get_union_pw_aff.
7760 * pos is the output position that needs to be extracted.
7761 * res collects the results.
7763 struct isl_union_pw_multi_aff_get_union_pw_aff_data
{
7765 isl_union_pw_aff
*res
;
7768 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma"
7769 * (assuming it has such a dimension) and add it to data->res.
7771 static isl_stat
get_union_pw_aff(__isl_take isl_pw_multi_aff
*pma
, void *user
)
7773 struct isl_union_pw_multi_aff_get_union_pw_aff_data
*data
= user
;
7778 return isl_stat_error
;
7780 n_out
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
7781 if (data
->pos
>= n_out
) {
7782 isl_pw_multi_aff_free(pma
);
7786 pa
= isl_pw_multi_aff_get_pw_aff(pma
, data
->pos
);
7787 isl_pw_multi_aff_free(pma
);
7789 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
7791 return data
->res
? isl_stat_ok
: isl_stat_error
;
7794 /* Extract an isl_union_pw_aff corresponding to
7795 * output dimension "pos" of "upma".
7797 __isl_give isl_union_pw_aff
*isl_union_pw_multi_aff_get_union_pw_aff(
7798 __isl_keep isl_union_pw_multi_aff
*upma
, int pos
)
7800 struct isl_union_pw_multi_aff_get_union_pw_aff_data data
;
7807 isl_die(isl_union_pw_multi_aff_get_ctx(upma
), isl_error_invalid
,
7808 "cannot extract at negative position", return NULL
);
7810 space
= isl_union_pw_multi_aff_get_space(upma
);
7811 data
.res
= isl_union_pw_aff_empty(space
);
7813 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma
,
7814 &get_union_pw_aff
, &data
) < 0)
7815 data
.res
= isl_union_pw_aff_free(data
.res
);
7820 /* Return a union piecewise affine expression
7821 * that is equal to "aff" on "domain".
7823 __isl_give isl_union_pw_aff
*isl_union_pw_aff_aff_on_domain(
7824 __isl_take isl_union_set
*domain
, __isl_take isl_aff
*aff
)
7828 pa
= isl_pw_aff_from_aff(aff
);
7829 return isl_union_pw_aff_pw_aff_on_domain(domain
, pa
);
7832 /* Return a union piecewise affine expression
7833 * that is equal to the parameter identified by "id" on "domain".
7835 * Make sure the parameter appears in the space passed to
7836 * isl_aff_param_on_domain_space_id.
7838 __isl_give isl_union_pw_aff
*isl_union_pw_aff_param_on_domain_id(
7839 __isl_take isl_union_set
*domain
, __isl_take isl_id
*id
)
7844 space
= isl_union_set_get_space(domain
);
7845 space
= isl_space_add_param_id(space
, isl_id_copy(id
));
7846 aff
= isl_aff_param_on_domain_space_id(space
, id
);
7847 return isl_union_pw_aff_aff_on_domain(domain
, aff
);
7850 /* Internal data structure for isl_union_pw_aff_pw_aff_on_domain.
7851 * "pa" is the piecewise symbolic value that the resulting isl_union_pw_aff
7853 * "res" collects the results.
7855 struct isl_union_pw_aff_pw_aff_on_domain_data
{
7857 isl_union_pw_aff
*res
;
7860 /* Construct a piecewise affine expression that is equal to data->pa
7861 * on "domain" and add the result to data->res.
7863 static isl_stat
pw_aff_on_domain(__isl_take isl_set
*domain
, void *user
)
7865 struct isl_union_pw_aff_pw_aff_on_domain_data
*data
= user
;
7869 pa
= isl_pw_aff_copy(data
->pa
);
7870 dim
= isl_set_dim(domain
, isl_dim_set
);
7871 pa
= isl_pw_aff_from_range(pa
);
7872 pa
= isl_pw_aff_add_dims(pa
, isl_dim_in
, dim
);
7873 pa
= isl_pw_aff_reset_domain_space(pa
, isl_set_get_space(domain
));
7874 pa
= isl_pw_aff_intersect_domain(pa
, domain
);
7875 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
7877 return data
->res
? isl_stat_ok
: isl_stat_error
;
7880 /* Return a union piecewise affine expression
7881 * that is equal to "pa" on "domain", assuming "domain" and "pa"
7882 * have been aligned.
7884 * Construct an isl_pw_aff on each of the sets in "domain" and
7885 * collect the results.
7887 static __isl_give isl_union_pw_aff
*isl_union_pw_aff_pw_aff_on_domain_aligned(
7888 __isl_take isl_union_set
*domain
, __isl_take isl_pw_aff
*pa
)
7890 struct isl_union_pw_aff_pw_aff_on_domain_data data
;
7893 space
= isl_union_set_get_space(domain
);
7894 data
.res
= isl_union_pw_aff_empty(space
);
7896 if (isl_union_set_foreach_set(domain
, &pw_aff_on_domain
, &data
) < 0)
7897 data
.res
= isl_union_pw_aff_free(data
.res
);
7898 isl_union_set_free(domain
);
7899 isl_pw_aff_free(pa
);
7903 /* Return a union piecewise affine expression
7904 * that is equal to "pa" on "domain".
7906 * Check that "pa" is a parametric expression,
7907 * align the parameters if needed and call
7908 * isl_union_pw_aff_pw_aff_on_domain_aligned.
7910 __isl_give isl_union_pw_aff
*isl_union_pw_aff_pw_aff_on_domain(
7911 __isl_take isl_union_set
*domain
, __isl_take isl_pw_aff
*pa
)
7914 isl_bool equal_params
;
7915 isl_space
*domain_space
, *pa_space
;
7917 pa_space
= isl_pw_aff_peek_space(pa
);
7918 is_set
= isl_space_is_set(pa_space
);
7922 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
7923 "expecting parametric expression", goto error
);
7925 domain_space
= isl_union_set_get_space(domain
);
7926 pa_space
= isl_pw_aff_get_space(pa
);
7927 equal_params
= isl_space_has_equal_params(domain_space
, pa_space
);
7928 if (equal_params
>= 0 && !equal_params
) {
7931 space
= isl_space_align_params(domain_space
, pa_space
);
7932 pa
= isl_pw_aff_align_params(pa
, isl_space_copy(space
));
7933 domain
= isl_union_set_align_params(domain
, space
);
7935 isl_space_free(domain_space
);
7936 isl_space_free(pa_space
);
7939 if (equal_params
< 0)
7941 return isl_union_pw_aff_pw_aff_on_domain_aligned(domain
, pa
);
7943 isl_union_set_free(domain
);
7944 isl_pw_aff_free(pa
);
7948 /* Internal data structure for isl_union_pw_aff_val_on_domain.
7949 * "v" is the value that the resulting isl_union_pw_aff needs to attain.
7950 * "res" collects the results.
7952 struct isl_union_pw_aff_val_on_domain_data
{
7954 isl_union_pw_aff
*res
;
7957 /* Construct a piecewise affine expression that is equal to data->v
7958 * on "domain" and add the result to data->res.
7960 static isl_stat
pw_aff_val_on_domain(__isl_take isl_set
*domain
, void *user
)
7962 struct isl_union_pw_aff_val_on_domain_data
*data
= user
;
7966 v
= isl_val_copy(data
->v
);
7967 pa
= isl_pw_aff_val_on_domain(domain
, v
);
7968 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
7970 return data
->res
? isl_stat_ok
: isl_stat_error
;
7973 /* Return a union piecewise affine expression
7974 * that is equal to "v" on "domain".
7976 * Construct an isl_pw_aff on each of the sets in "domain" and
7977 * collect the results.
7979 __isl_give isl_union_pw_aff
*isl_union_pw_aff_val_on_domain(
7980 __isl_take isl_union_set
*domain
, __isl_take isl_val
*v
)
7982 struct isl_union_pw_aff_val_on_domain_data data
;
7985 space
= isl_union_set_get_space(domain
);
7986 data
.res
= isl_union_pw_aff_empty(space
);
7988 if (isl_union_set_foreach_set(domain
, &pw_aff_val_on_domain
, &data
) < 0)
7989 data
.res
= isl_union_pw_aff_free(data
.res
);
7990 isl_union_set_free(domain
);
7995 /* Construct a piecewise multi affine expression
7996 * that is equal to "pa" and add it to upma.
7998 static isl_stat
pw_multi_aff_from_pw_aff_entry(__isl_take isl_pw_aff
*pa
,
8001 isl_union_pw_multi_aff
**upma
= user
;
8002 isl_pw_multi_aff
*pma
;
8004 pma
= isl_pw_multi_aff_from_pw_aff(pa
);
8005 *upma
= isl_union_pw_multi_aff_add_pw_multi_aff(*upma
, pma
);
8007 return *upma
? isl_stat_ok
: isl_stat_error
;
8010 /* Construct and return a union piecewise multi affine expression
8011 * that is equal to the given union piecewise affine expression.
8013 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_union_pw_aff(
8014 __isl_take isl_union_pw_aff
*upa
)
8017 isl_union_pw_multi_aff
*upma
;
8022 space
= isl_union_pw_aff_get_space(upa
);
8023 upma
= isl_union_pw_multi_aff_empty(space
);
8025 if (isl_union_pw_aff_foreach_pw_aff(upa
,
8026 &pw_multi_aff_from_pw_aff_entry
, &upma
) < 0)
8027 upma
= isl_union_pw_multi_aff_free(upma
);
8029 isl_union_pw_aff_free(upa
);
8033 /* Compute the set of elements in the domain of "pa" where it is zero and
8034 * add this set to "uset".
8036 static isl_stat
zero_union_set(__isl_take isl_pw_aff
*pa
, void *user
)
8038 isl_union_set
**uset
= (isl_union_set
**)user
;
8040 *uset
= isl_union_set_add_set(*uset
, isl_pw_aff_zero_set(pa
));
8042 return *uset
? isl_stat_ok
: isl_stat_error
;
8045 /* Return a union set containing those elements in the domain
8046 * of "upa" where it is zero.
8048 __isl_give isl_union_set
*isl_union_pw_aff_zero_union_set(
8049 __isl_take isl_union_pw_aff
*upa
)
8051 isl_union_set
*zero
;
8053 zero
= isl_union_set_empty(isl_union_pw_aff_get_space(upa
));
8054 if (isl_union_pw_aff_foreach_pw_aff(upa
, &zero_union_set
, &zero
) < 0)
8055 zero
= isl_union_set_free(zero
);
8057 isl_union_pw_aff_free(upa
);
8061 /* Convert "pa" to an isl_map and add it to *umap.
8063 static isl_stat
map_from_pw_aff_entry(__isl_take isl_pw_aff
*pa
, void *user
)
8065 isl_union_map
**umap
= user
;
8068 map
= isl_map_from_pw_aff(pa
);
8069 *umap
= isl_union_map_add_map(*umap
, map
);
8071 return *umap
? isl_stat_ok
: isl_stat_error
;
8074 /* Construct a union map mapping the domain of the union
8075 * piecewise affine expression to its range, with the single output dimension
8076 * equated to the corresponding affine expressions on their cells.
8078 __isl_give isl_union_map
*isl_union_map_from_union_pw_aff(
8079 __isl_take isl_union_pw_aff
*upa
)
8082 isl_union_map
*umap
;
8087 space
= isl_union_pw_aff_get_space(upa
);
8088 umap
= isl_union_map_empty(space
);
8090 if (isl_union_pw_aff_foreach_pw_aff(upa
, &map_from_pw_aff_entry
,
8092 umap
= isl_union_map_free(umap
);
8094 isl_union_pw_aff_free(upa
);
8098 /* Internal data structure for isl_union_pw_aff_pullback_union_pw_multi_aff.
8099 * upma is the function that is plugged in.
8100 * pa is the current part of the function in which upma is plugged in.
8101 * res collects the results.
8103 struct isl_union_pw_aff_pullback_upma_data
{
8104 isl_union_pw_multi_aff
*upma
;
8106 isl_union_pw_aff
*res
;
8109 /* Check if "pma" can be plugged into data->pa.
8110 * If so, perform the pullback and add the result to data->res.
8112 static isl_stat
pa_pb_pma(__isl_take isl_pw_multi_aff
*pma
, void *user
)
8114 struct isl_union_pw_aff_pullback_upma_data
*data
= user
;
8117 if (!isl_space_tuple_is_equal(data
->pa
->dim
, isl_dim_in
,
8118 pma
->dim
, isl_dim_out
)) {
8119 isl_pw_multi_aff_free(pma
);
8123 pa
= isl_pw_aff_copy(data
->pa
);
8124 pa
= isl_pw_aff_pullback_pw_multi_aff(pa
, pma
);
8126 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
8128 return data
->res
? isl_stat_ok
: isl_stat_error
;
8131 /* Check if any of the elements of data->upma can be plugged into pa,
8132 * add if so add the result to data->res.
8134 static isl_stat
upa_pb_upma(__isl_take isl_pw_aff
*pa
, void *user
)
8136 struct isl_union_pw_aff_pullback_upma_data
*data
= user
;
8140 r
= isl_union_pw_multi_aff_foreach_pw_multi_aff(data
->upma
,
8142 isl_pw_aff_free(pa
);
8147 /* Compute the pullback of "upa" by the function represented by "upma".
8148 * In other words, plug in "upma" in "upa". The result contains
8149 * expressions defined over the domain space of "upma".
8151 * Run over all pairs of elements in "upa" and "upma", perform
8152 * the pullback when appropriate and collect the results.
8153 * If the hash value were based on the domain space rather than
8154 * the function space, then we could run through all elements
8155 * of "upma" and directly pick out the corresponding element of "upa".
8157 __isl_give isl_union_pw_aff
*isl_union_pw_aff_pullback_union_pw_multi_aff(
8158 __isl_take isl_union_pw_aff
*upa
,
8159 __isl_take isl_union_pw_multi_aff
*upma
)
8161 struct isl_union_pw_aff_pullback_upma_data data
= { NULL
, NULL
};
8164 space
= isl_union_pw_multi_aff_get_space(upma
);
8165 upa
= isl_union_pw_aff_align_params(upa
, space
);
8166 space
= isl_union_pw_aff_get_space(upa
);
8167 upma
= isl_union_pw_multi_aff_align_params(upma
, space
);
8173 data
.res
= isl_union_pw_aff_alloc_same_size(upa
);
8174 if (isl_union_pw_aff_foreach_pw_aff(upa
, &upa_pb_upma
, &data
) < 0)
8175 data
.res
= isl_union_pw_aff_free(data
.res
);
8177 isl_union_pw_aff_free(upa
);
8178 isl_union_pw_multi_aff_free(upma
);
8181 isl_union_pw_aff_free(upa
);
8182 isl_union_pw_multi_aff_free(upma
);
8187 #define BASE union_pw_aff
8189 #define DOMBASE union_set
8191 #define NO_MOVE_DIMS
8199 #include <isl_multi_explicit_domain.c>
8200 #include <isl_multi_union_pw_aff_explicit_domain.c>
8201 #include <isl_multi_templ.c>
8202 #include <isl_multi_apply_set.c>
8203 #include <isl_multi_apply_union_set.c>
8204 #include <isl_multi_coalesce.c>
8205 #include <isl_multi_floor.c>
8206 #include <isl_multi_gist.c>
8207 #include <isl_multi_align_set.c>
8208 #include <isl_multi_align_union_set.c>
8209 #include <isl_multi_intersect.c>
8211 /* Does "mupa" have a non-trivial explicit domain?
8213 * The explicit domain, if present, is trivial if it represents
8214 * an (obviously) universe parameter set.
8216 isl_bool
isl_multi_union_pw_aff_has_non_trivial_domain(
8217 __isl_keep isl_multi_union_pw_aff
*mupa
)
8219 isl_bool is_params
, trivial
;
8223 return isl_bool_error
;
8224 if (!isl_multi_union_pw_aff_has_explicit_domain(mupa
))
8225 return isl_bool_false
;
8226 is_params
= isl_union_set_is_params(mupa
->u
.dom
);
8227 if (is_params
< 0 || !is_params
)
8228 return isl_bool_not(is_params
);
8229 set
= isl_set_from_union_set(isl_union_set_copy(mupa
->u
.dom
));
8230 trivial
= isl_set_plain_is_universe(set
);
8232 return isl_bool_not(trivial
);
8235 /* Construct a multiple union piecewise affine expression
8236 * in the given space with value zero in each of the output dimensions.
8238 * Since there is no canonical zero value for
8239 * a union piecewise affine expression, we can only construct
8240 * a zero-dimensional "zero" value.
8242 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_zero(
8243 __isl_take isl_space
*space
)
8250 params
= isl_space_is_params(space
);
8254 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
8255 "expecting proper set space", goto error
);
8256 if (!isl_space_is_set(space
))
8257 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
8258 "expecting set space", goto error
);
8259 if (isl_space_dim(space
, isl_dim_out
) != 0)
8260 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
8261 "expecting 0D space", goto error
);
8263 return isl_multi_union_pw_aff_alloc(space
);
8265 isl_space_free(space
);
8269 /* Compute the sum of "mupa1" and "mupa2" on the union of their domains,
8270 * with the actual sum on the shared domain and
8271 * the defined expression on the symmetric difference of the domains.
8273 * We simply iterate over the elements in both arguments and
8274 * call isl_union_pw_aff_union_add on each of them, if there is
8275 * at least one element.
8277 * Otherwise, the two expressions have an explicit domain and
8278 * the union of these explicit domains is computed.
8279 * This assumes that the explicit domains are either both in terms
8280 * of specific domains elements or both in terms of parameters.
8281 * However, if one of the expressions does not have any constraints
8282 * on its explicit domain, then this is allowed as well and the result
8283 * is the expression with no constraints on its explicit domain.
8285 static __isl_give isl_multi_union_pw_aff
*
8286 isl_multi_union_pw_aff_union_add_aligned(
8287 __isl_take isl_multi_union_pw_aff
*mupa1
,
8288 __isl_take isl_multi_union_pw_aff
*mupa2
)
8290 isl_bool has_domain
, is_params1
, is_params2
;
8292 if (isl_multi_union_pw_aff_check_equal_space(mupa1
, mupa2
) < 0)
8295 return isl_multi_union_pw_aff_bin_op(mupa1
, mupa2
,
8296 &isl_union_pw_aff_union_add
);
8297 if (isl_multi_union_pw_aff_check_has_explicit_domain(mupa1
) < 0 ||
8298 isl_multi_union_pw_aff_check_has_explicit_domain(mupa2
) < 0)
8301 has_domain
= isl_multi_union_pw_aff_has_non_trivial_domain(mupa1
);
8305 isl_multi_union_pw_aff_free(mupa2
);
8308 has_domain
= isl_multi_union_pw_aff_has_non_trivial_domain(mupa2
);
8312 isl_multi_union_pw_aff_free(mupa1
);
8316 is_params1
= isl_union_set_is_params(mupa1
->u
.dom
);
8317 is_params2
= isl_union_set_is_params(mupa2
->u
.dom
);
8318 if (is_params1
< 0 || is_params2
< 0)
8320 if (is_params1
!= is_params2
)
8321 isl_die(isl_multi_union_pw_aff_get_ctx(mupa1
),
8323 "cannot compute union of concrete domain and "
8324 "parameter constraints", goto error
);
8325 mupa1
= isl_multi_union_pw_aff_cow(mupa1
);
8328 mupa1
->u
.dom
= isl_union_set_union(mupa1
->u
.dom
,
8329 isl_union_set_copy(mupa2
->u
.dom
));
8332 isl_multi_union_pw_aff_free(mupa2
);
8335 isl_multi_union_pw_aff_free(mupa1
);
8336 isl_multi_union_pw_aff_free(mupa2
);
8340 /* Compute the sum of "mupa1" and "mupa2" on the union of their domains,
8341 * with the actual sum on the shared domain and
8342 * the defined expression on the symmetric difference of the domains.
8344 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_union_add(
8345 __isl_take isl_multi_union_pw_aff
*mupa1
,
8346 __isl_take isl_multi_union_pw_aff
*mupa2
)
8348 return isl_multi_union_pw_aff_align_params_multi_multi_and(mupa1
, mupa2
,
8349 &isl_multi_union_pw_aff_union_add_aligned
);
8352 /* Construct and return a multi union piecewise affine expression
8353 * that is equal to the given multi affine expression.
8355 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_from_multi_aff(
8356 __isl_take isl_multi_aff
*ma
)
8358 isl_multi_pw_aff
*mpa
;
8360 mpa
= isl_multi_pw_aff_from_multi_aff(ma
);
8361 return isl_multi_union_pw_aff_from_multi_pw_aff(mpa
);
8364 /* Construct and return a multi union piecewise affine expression
8365 * that is equal to the given multi piecewise affine expression.
8367 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_from_multi_pw_aff(
8368 __isl_take isl_multi_pw_aff
*mpa
)
8372 isl_multi_union_pw_aff
*mupa
;
8377 space
= isl_multi_pw_aff_get_space(mpa
);
8378 space
= isl_space_range(space
);
8379 mupa
= isl_multi_union_pw_aff_alloc(space
);
8381 n
= isl_multi_pw_aff_dim(mpa
, isl_dim_out
);
8382 for (i
= 0; i
< n
; ++i
) {
8384 isl_union_pw_aff
*upa
;
8386 pa
= isl_multi_pw_aff_get_pw_aff(mpa
, i
);
8387 upa
= isl_union_pw_aff_from_pw_aff(pa
);
8388 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
8391 isl_multi_pw_aff_free(mpa
);
8396 /* Extract the range space of "pma" and assign it to *space.
8397 * If *space has already been set (through a previous call to this function),
8398 * then check that the range space is the same.
8400 static isl_stat
extract_space(__isl_take isl_pw_multi_aff
*pma
, void *user
)
8402 isl_space
**space
= user
;
8403 isl_space
*pma_space
;
8406 pma_space
= isl_space_range(isl_pw_multi_aff_get_space(pma
));
8407 isl_pw_multi_aff_free(pma
);
8410 return isl_stat_error
;
8416 equal
= isl_space_is_equal(pma_space
, *space
);
8417 isl_space_free(pma_space
);
8420 return isl_stat_error
;
8422 isl_die(isl_space_get_ctx(*space
), isl_error_invalid
,
8423 "range spaces not the same", return isl_stat_error
);
8427 /* Construct and return a multi union piecewise affine expression
8428 * that is equal to the given union piecewise multi affine expression.
8430 * In order to be able to perform the conversion, the input
8431 * needs to be non-empty and may only involve a single range space.
8433 * If the resulting multi union piecewise affine expression has
8434 * an explicit domain, then assign it the domain of the input.
8435 * In other cases, the domain is stored in the individual elements.
8437 __isl_give isl_multi_union_pw_aff
*
8438 isl_multi_union_pw_aff_from_union_pw_multi_aff(
8439 __isl_take isl_union_pw_multi_aff
*upma
)
8441 isl_space
*space
= NULL
;
8442 isl_multi_union_pw_aff
*mupa
;
8447 if (isl_union_pw_multi_aff_n_pw_multi_aff(upma
) == 0)
8448 isl_die(isl_union_pw_multi_aff_get_ctx(upma
), isl_error_invalid
,
8449 "cannot extract range space from empty input",
8451 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma
, &extract_space
,
8458 n
= isl_space_dim(space
, isl_dim_set
);
8459 mupa
= isl_multi_union_pw_aff_alloc(space
);
8461 for (i
= 0; i
< n
; ++i
) {
8462 isl_union_pw_aff
*upa
;
8464 upa
= isl_union_pw_multi_aff_get_union_pw_aff(upma
, i
);
8465 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
8467 if (isl_multi_union_pw_aff_has_explicit_domain(mupa
)) {
8469 isl_union_pw_multi_aff
*copy
;
8471 copy
= isl_union_pw_multi_aff_copy(upma
);
8472 dom
= isl_union_pw_multi_aff_domain(copy
);
8473 mupa
= isl_multi_union_pw_aff_intersect_domain(mupa
, dom
);
8476 isl_union_pw_multi_aff_free(upma
);
8479 isl_space_free(space
);
8480 isl_union_pw_multi_aff_free(upma
);
8484 /* Try and create an isl_multi_union_pw_aff that is equivalent
8485 * to the given isl_union_map.
8486 * The isl_union_map is required to be single-valued in each space.
8487 * Moreover, it cannot be empty and all range spaces need to be the same.
8488 * Otherwise, an error is produced.
8490 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_from_union_map(
8491 __isl_take isl_union_map
*umap
)
8493 isl_union_pw_multi_aff
*upma
;
8495 upma
= isl_union_pw_multi_aff_from_union_map(umap
);
8496 return isl_multi_union_pw_aff_from_union_pw_multi_aff(upma
);
8499 /* Return a multiple union piecewise affine expression
8500 * that is equal to "mv" on "domain", assuming "domain" and "mv"
8501 * have been aligned.
8503 static __isl_give isl_multi_union_pw_aff
*
8504 isl_multi_union_pw_aff_multi_val_on_domain_aligned(
8505 __isl_take isl_union_set
*domain
, __isl_take isl_multi_val
*mv
)
8509 isl_multi_union_pw_aff
*mupa
;
8514 n
= isl_multi_val_dim(mv
, isl_dim_set
);
8515 space
= isl_multi_val_get_space(mv
);
8516 mupa
= isl_multi_union_pw_aff_alloc(space
);
8517 for (i
= 0; i
< n
; ++i
) {
8519 isl_union_pw_aff
*upa
;
8521 v
= isl_multi_val_get_val(mv
, i
);
8522 upa
= isl_union_pw_aff_val_on_domain(isl_union_set_copy(domain
),
8524 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
8527 isl_union_set_free(domain
);
8528 isl_multi_val_free(mv
);
8531 isl_union_set_free(domain
);
8532 isl_multi_val_free(mv
);
8536 /* Return a multiple union piecewise affine expression
8537 * that is equal to "mv" on "domain".
8539 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_multi_val_on_domain(
8540 __isl_take isl_union_set
*domain
, __isl_take isl_multi_val
*mv
)
8542 isl_bool equal_params
;
8546 equal_params
= isl_space_has_equal_params(domain
->dim
, mv
->space
);
8547 if (equal_params
< 0)
8550 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(
8552 domain
= isl_union_set_align_params(domain
,
8553 isl_multi_val_get_space(mv
));
8554 mv
= isl_multi_val_align_params(mv
, isl_union_set_get_space(domain
));
8555 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(domain
, mv
);
8557 isl_union_set_free(domain
);
8558 isl_multi_val_free(mv
);
8562 /* Return a multiple union piecewise affine expression
8563 * that is equal to "ma" on "domain".
8565 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_multi_aff_on_domain(
8566 __isl_take isl_union_set
*domain
, __isl_take isl_multi_aff
*ma
)
8568 isl_pw_multi_aff
*pma
;
8570 pma
= isl_pw_multi_aff_from_multi_aff(ma
);
8571 return isl_multi_union_pw_aff_pw_multi_aff_on_domain(domain
, pma
);
8574 /* Return a multiple union piecewise affine expression
8575 * that is equal to "pma" on "domain", assuming "domain" and "pma"
8576 * have been aligned.
8578 * If the resulting multi union piecewise affine expression has
8579 * an explicit domain, then assign it the input domain.
8580 * In other cases, the domain is stored in the individual elements.
8582 static __isl_give isl_multi_union_pw_aff
*
8583 isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(
8584 __isl_take isl_union_set
*domain
, __isl_take isl_pw_multi_aff
*pma
)
8588 isl_multi_union_pw_aff
*mupa
;
8590 if (!domain
|| !pma
)
8593 n
= isl_pw_multi_aff_dim(pma
, isl_dim_set
);
8594 space
= isl_pw_multi_aff_get_space(pma
);
8595 mupa
= isl_multi_union_pw_aff_alloc(space
);
8596 for (i
= 0; i
< n
; ++i
) {
8598 isl_union_pw_aff
*upa
;
8600 pa
= isl_pw_multi_aff_get_pw_aff(pma
, i
);
8601 upa
= isl_union_pw_aff_pw_aff_on_domain(
8602 isl_union_set_copy(domain
), pa
);
8603 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
8605 if (isl_multi_union_pw_aff_has_explicit_domain(mupa
))
8606 mupa
= isl_multi_union_pw_aff_intersect_domain(mupa
,
8607 isl_union_set_copy(domain
));
8609 isl_union_set_free(domain
);
8610 isl_pw_multi_aff_free(pma
);
8613 isl_union_set_free(domain
);
8614 isl_pw_multi_aff_free(pma
);
8618 /* Return a multiple union piecewise affine expression
8619 * that is equal to "pma" on "domain".
8621 __isl_give isl_multi_union_pw_aff
*
8622 isl_multi_union_pw_aff_pw_multi_aff_on_domain(__isl_take isl_union_set
*domain
,
8623 __isl_take isl_pw_multi_aff
*pma
)
8625 isl_bool equal_params
;
8628 space
= isl_pw_multi_aff_peek_space(pma
);
8629 equal_params
= isl_union_set_space_has_equal_params(domain
, space
);
8630 if (equal_params
< 0)
8633 return isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(
8635 domain
= isl_union_set_align_params(domain
,
8636 isl_pw_multi_aff_get_space(pma
));
8637 pma
= isl_pw_multi_aff_align_params(pma
,
8638 isl_union_set_get_space(domain
));
8639 return isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(domain
,
8642 isl_union_set_free(domain
);
8643 isl_pw_multi_aff_free(pma
);
8647 /* Return a union set containing those elements in the domains
8648 * of the elements of "mupa" where they are all zero.
8650 * If there are no elements, then simply return the entire domain.
8652 __isl_give isl_union_set
*isl_multi_union_pw_aff_zero_union_set(
8653 __isl_take isl_multi_union_pw_aff
*mupa
)
8656 isl_union_pw_aff
*upa
;
8657 isl_union_set
*zero
;
8662 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8664 return isl_multi_union_pw_aff_domain(mupa
);
8666 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, 0);
8667 zero
= isl_union_pw_aff_zero_union_set(upa
);
8669 for (i
= 1; i
< n
; ++i
) {
8670 isl_union_set
*zero_i
;
8672 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
8673 zero_i
= isl_union_pw_aff_zero_union_set(upa
);
8675 zero
= isl_union_set_intersect(zero
, zero_i
);
8678 isl_multi_union_pw_aff_free(mupa
);
8682 /* Construct a union map mapping the shared domain
8683 * of the union piecewise affine expressions to the range of "mupa"
8684 * in the special case of a 0D multi union piecewise affine expression.
8686 * Construct a map between the explicit domain of "mupa" and
8688 * Note that this assumes that the domain consists of explicit elements.
8690 static __isl_give isl_union_map
*isl_union_map_from_multi_union_pw_aff_0D(
8691 __isl_take isl_multi_union_pw_aff
*mupa
)
8695 isl_union_set
*dom
, *ran
;
8697 space
= isl_multi_union_pw_aff_get_space(mupa
);
8698 dom
= isl_multi_union_pw_aff_domain(mupa
);
8699 ran
= isl_union_set_from_set(isl_set_universe(space
));
8701 is_params
= isl_union_set_is_params(dom
);
8703 dom
= isl_union_set_free(dom
);
8705 isl_die(isl_union_set_get_ctx(dom
), isl_error_invalid
,
8706 "cannot create union map from expression without "
8707 "explicit domain elements",
8708 dom
= isl_union_set_free(dom
));
8710 return isl_union_map_from_domain_and_range(dom
, ran
);
8713 /* Construct a union map mapping the shared domain
8714 * of the union piecewise affine expressions to the range of "mupa"
8715 * with each dimension in the range equated to the
8716 * corresponding union piecewise affine expression.
8718 * If the input is zero-dimensional, then construct a mapping
8719 * from its explicit domain.
8721 __isl_give isl_union_map
*isl_union_map_from_multi_union_pw_aff(
8722 __isl_take isl_multi_union_pw_aff
*mupa
)
8726 isl_union_map
*umap
;
8727 isl_union_pw_aff
*upa
;
8732 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8734 return isl_union_map_from_multi_union_pw_aff_0D(mupa
);
8736 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, 0);
8737 umap
= isl_union_map_from_union_pw_aff(upa
);
8739 for (i
= 1; i
< n
; ++i
) {
8740 isl_union_map
*umap_i
;
8742 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
8743 umap_i
= isl_union_map_from_union_pw_aff(upa
);
8744 umap
= isl_union_map_flat_range_product(umap
, umap_i
);
8747 space
= isl_multi_union_pw_aff_get_space(mupa
);
8748 umap
= isl_union_map_reset_range_space(umap
, space
);
8750 isl_multi_union_pw_aff_free(mupa
);
8754 /* Internal data structure for isl_union_pw_multi_aff_reset_range_space.
8755 * "range" is the space from which to set the range space.
8756 * "res" collects the results.
8758 struct isl_union_pw_multi_aff_reset_range_space_data
{
8760 isl_union_pw_multi_aff
*res
;
8763 /* Replace the range space of "pma" by the range space of data->range and
8764 * add the result to data->res.
8766 static isl_stat
reset_range_space(__isl_take isl_pw_multi_aff
*pma
, void *user
)
8768 struct isl_union_pw_multi_aff_reset_range_space_data
*data
= user
;
8771 space
= isl_pw_multi_aff_get_space(pma
);
8772 space
= isl_space_domain(space
);
8773 space
= isl_space_extend_domain_with_range(space
,
8774 isl_space_copy(data
->range
));
8775 pma
= isl_pw_multi_aff_reset_space(pma
, space
);
8776 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma
);
8778 return data
->res
? isl_stat_ok
: isl_stat_error
;
8781 /* Replace the range space of all the piecewise affine expressions in "upma" by
8782 * the range space of "space".
8784 * This assumes that all these expressions have the same output dimension.
8786 * Since the spaces of the expressions change, so do their hash values.
8787 * We therefore need to create a new isl_union_pw_multi_aff.
8788 * Note that the hash value is currently computed based on the entire
8789 * space even though there can only be a single expression with a given
8792 static __isl_give isl_union_pw_multi_aff
*
8793 isl_union_pw_multi_aff_reset_range_space(
8794 __isl_take isl_union_pw_multi_aff
*upma
, __isl_take isl_space
*space
)
8796 struct isl_union_pw_multi_aff_reset_range_space_data data
= { space
};
8797 isl_space
*space_upma
;
8799 space_upma
= isl_union_pw_multi_aff_get_space(upma
);
8800 data
.res
= isl_union_pw_multi_aff_empty(space_upma
);
8801 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma
,
8802 &reset_range_space
, &data
) < 0)
8803 data
.res
= isl_union_pw_multi_aff_free(data
.res
);
8805 isl_space_free(space
);
8806 isl_union_pw_multi_aff_free(upma
);
8810 /* Construct and return a union piecewise multi affine expression
8811 * that is equal to the given multi union piecewise affine expression,
8812 * in the special case of a 0D multi union piecewise affine expression.
8814 * Construct a union piecewise multi affine expression
8815 * on top of the explicit domain of the input.
8817 __isl_give isl_union_pw_multi_aff
*
8818 isl_union_pw_multi_aff_from_multi_union_pw_aff_0D(
8819 __isl_take isl_multi_union_pw_aff
*mupa
)
8823 isl_union_set
*domain
;
8825 space
= isl_multi_union_pw_aff_get_space(mupa
);
8826 mv
= isl_multi_val_zero(space
);
8827 domain
= isl_multi_union_pw_aff_domain(mupa
);
8828 return isl_union_pw_multi_aff_multi_val_on_domain(domain
, mv
);
8831 /* Construct and return a union piecewise multi affine expression
8832 * that is equal to the given multi union piecewise affine expression.
8834 * If the input is zero-dimensional, then
8835 * construct a union piecewise multi affine expression
8836 * on top of the explicit domain of the input.
8838 __isl_give isl_union_pw_multi_aff
*
8839 isl_union_pw_multi_aff_from_multi_union_pw_aff(
8840 __isl_take isl_multi_union_pw_aff
*mupa
)
8844 isl_union_pw_multi_aff
*upma
;
8845 isl_union_pw_aff
*upa
;
8850 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8852 return isl_union_pw_multi_aff_from_multi_union_pw_aff_0D(mupa
);
8854 space
= isl_multi_union_pw_aff_get_space(mupa
);
8855 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, 0);
8856 upma
= isl_union_pw_multi_aff_from_union_pw_aff(upa
);
8858 for (i
= 1; i
< n
; ++i
) {
8859 isl_union_pw_multi_aff
*upma_i
;
8861 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
8862 upma_i
= isl_union_pw_multi_aff_from_union_pw_aff(upa
);
8863 upma
= isl_union_pw_multi_aff_flat_range_product(upma
, upma_i
);
8866 upma
= isl_union_pw_multi_aff_reset_range_space(upma
, space
);
8868 isl_multi_union_pw_aff_free(mupa
);
8872 /* Intersect the range of "mupa" with "range",
8873 * in the special case where "mupa" is 0D.
8875 * Intersect the domain of "mupa" with the constraints on the parameters
8878 static __isl_give isl_multi_union_pw_aff
*mupa_intersect_range_0D(
8879 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_set
*range
)
8881 range
= isl_set_params(range
);
8882 mupa
= isl_multi_union_pw_aff_intersect_params(mupa
, range
);
8886 /* Intersect the range of "mupa" with "range".
8887 * That is, keep only those domain elements that have a function value
8890 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_intersect_range(
8891 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_set
*range
)
8893 isl_union_pw_multi_aff
*upma
;
8894 isl_union_set
*domain
;
8899 if (!mupa
|| !range
)
8902 space
= isl_set_get_space(range
);
8903 match
= isl_space_tuple_is_equal(mupa
->space
, isl_dim_set
,
8904 space
, isl_dim_set
);
8905 isl_space_free(space
);
8909 isl_die(isl_multi_union_pw_aff_get_ctx(mupa
), isl_error_invalid
,
8910 "space don't match", goto error
);
8911 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8913 return mupa_intersect_range_0D(mupa
, range
);
8915 upma
= isl_union_pw_multi_aff_from_multi_union_pw_aff(
8916 isl_multi_union_pw_aff_copy(mupa
));
8917 domain
= isl_union_set_from_set(range
);
8918 domain
= isl_union_set_preimage_union_pw_multi_aff(domain
, upma
);
8919 mupa
= isl_multi_union_pw_aff_intersect_domain(mupa
, domain
);
8923 isl_multi_union_pw_aff_free(mupa
);
8924 isl_set_free(range
);
8928 /* Return the shared domain of the elements of "mupa",
8929 * in the special case where "mupa" is zero-dimensional.
8931 * Return the explicit domain of "mupa".
8932 * Note that this domain may be a parameter set, either
8933 * because "mupa" is meant to live in a set space or
8934 * because no explicit domain has been set.
8936 __isl_give isl_union_set
*isl_multi_union_pw_aff_domain_0D(
8937 __isl_take isl_multi_union_pw_aff
*mupa
)
8941 dom
= isl_multi_union_pw_aff_get_explicit_domain(mupa
);
8942 isl_multi_union_pw_aff_free(mupa
);
8947 /* Return the shared domain of the elements of "mupa".
8949 * If "mupa" is zero-dimensional, then return its explicit domain.
8951 __isl_give isl_union_set
*isl_multi_union_pw_aff_domain(
8952 __isl_take isl_multi_union_pw_aff
*mupa
)
8955 isl_union_pw_aff
*upa
;
8961 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8963 return isl_multi_union_pw_aff_domain_0D(mupa
);
8965 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, 0);
8966 dom
= isl_union_pw_aff_domain(upa
);
8967 for (i
= 1; i
< n
; ++i
) {
8968 isl_union_set
*dom_i
;
8970 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
8971 dom_i
= isl_union_pw_aff_domain(upa
);
8972 dom
= isl_union_set_intersect(dom
, dom_i
);
8975 isl_multi_union_pw_aff_free(mupa
);
8979 /* Apply "aff" to "mupa". The space of "mupa" is equal to the domain of "aff".
8980 * In particular, the spaces have been aligned.
8981 * The result is defined over the shared domain of the elements of "mupa"
8983 * We first extract the parametric constant part of "aff" and
8984 * define that over the shared domain.
8985 * Then we iterate over all input dimensions of "aff" and add the corresponding
8986 * multiples of the elements of "mupa".
8987 * Finally, we consider the integer divisions, calling the function
8988 * recursively to obtain an isl_union_pw_aff corresponding to the
8989 * integer division argument.
8991 static __isl_give isl_union_pw_aff
*multi_union_pw_aff_apply_aff(
8992 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_aff
*aff
)
8995 isl_union_pw_aff
*upa
;
8996 isl_union_set
*uset
;
9000 n_in
= isl_aff_dim(aff
, isl_dim_in
);
9001 n_div
= isl_aff_dim(aff
, isl_dim_div
);
9003 uset
= isl_multi_union_pw_aff_domain(isl_multi_union_pw_aff_copy(mupa
));
9004 cst
= isl_aff_copy(aff
);
9005 cst
= isl_aff_drop_dims(cst
, isl_dim_div
, 0, n_div
);
9006 cst
= isl_aff_drop_dims(cst
, isl_dim_in
, 0, n_in
);
9007 cst
= isl_aff_project_domain_on_params(cst
);
9008 upa
= isl_union_pw_aff_aff_on_domain(uset
, cst
);
9010 for (i
= 0; i
< n_in
; ++i
) {
9011 isl_union_pw_aff
*upa_i
;
9013 if (!isl_aff_involves_dims(aff
, isl_dim_in
, i
, 1))
9015 v
= isl_aff_get_coefficient_val(aff
, isl_dim_in
, i
);
9016 upa_i
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
9017 upa_i
= isl_union_pw_aff_scale_val(upa_i
, v
);
9018 upa
= isl_union_pw_aff_add(upa
, upa_i
);
9021 for (i
= 0; i
< n_div
; ++i
) {
9023 isl_union_pw_aff
*upa_i
;
9025 if (!isl_aff_involves_dims(aff
, isl_dim_div
, i
, 1))
9027 div
= isl_aff_get_div(aff
, i
);
9028 upa_i
= multi_union_pw_aff_apply_aff(
9029 isl_multi_union_pw_aff_copy(mupa
), div
);
9030 upa_i
= isl_union_pw_aff_floor(upa_i
);
9031 v
= isl_aff_get_coefficient_val(aff
, isl_dim_div
, i
);
9032 upa_i
= isl_union_pw_aff_scale_val(upa_i
, v
);
9033 upa
= isl_union_pw_aff_add(upa
, upa_i
);
9036 isl_multi_union_pw_aff_free(mupa
);
9042 /* Apply "aff" to "mupa". The space of "mupa" needs to be compatible
9043 * with the domain of "aff".
9044 * Furthermore, the dimension of this space needs to be greater than zero.
9045 * The result is defined over the shared domain of the elements of "mupa"
9047 * We perform these checks and then hand over control to
9048 * multi_union_pw_aff_apply_aff.
9050 __isl_give isl_union_pw_aff
*isl_multi_union_pw_aff_apply_aff(
9051 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_aff
*aff
)
9053 isl_space
*space1
, *space2
;
9056 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
9057 isl_aff_get_space(aff
));
9058 aff
= isl_aff_align_params(aff
, isl_multi_union_pw_aff_get_space(mupa
));
9062 space1
= isl_multi_union_pw_aff_get_space(mupa
);
9063 space2
= isl_aff_get_domain_space(aff
);
9064 equal
= isl_space_is_equal(space1
, space2
);
9065 isl_space_free(space1
);
9066 isl_space_free(space2
);
9070 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
9071 "spaces don't match", goto error
);
9072 if (isl_aff_dim(aff
, isl_dim_in
) == 0)
9073 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
9074 "cannot determine domains", goto error
);
9076 return multi_union_pw_aff_apply_aff(mupa
, aff
);
9078 isl_multi_union_pw_aff_free(mupa
);
9083 /* Apply "ma" to "mupa", in the special case where "mupa" is 0D.
9084 * The space of "mupa" is known to be compatible with the domain of "ma".
9086 * Construct an isl_multi_union_pw_aff that is equal to "ma"
9087 * on the domain of "mupa".
9089 static __isl_give isl_multi_union_pw_aff
*mupa_apply_multi_aff_0D(
9090 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_multi_aff
*ma
)
9094 dom
= isl_multi_union_pw_aff_domain(mupa
);
9095 ma
= isl_multi_aff_project_domain_on_params(ma
);
9097 return isl_multi_union_pw_aff_multi_aff_on_domain(dom
, ma
);
9100 /* Apply "ma" to "mupa". The space of "mupa" needs to be compatible
9101 * with the domain of "ma".
9102 * The result is defined over the shared domain of the elements of "mupa"
9104 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_apply_multi_aff(
9105 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_multi_aff
*ma
)
9107 isl_space
*space1
, *space2
;
9108 isl_multi_union_pw_aff
*res
;
9112 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
9113 isl_multi_aff_get_space(ma
));
9114 ma
= isl_multi_aff_align_params(ma
,
9115 isl_multi_union_pw_aff_get_space(mupa
));
9119 space1
= isl_multi_union_pw_aff_get_space(mupa
);
9120 space2
= isl_multi_aff_get_domain_space(ma
);
9121 equal
= isl_space_is_equal(space1
, space2
);
9122 isl_space_free(space1
);
9123 isl_space_free(space2
);
9127 isl_die(isl_multi_aff_get_ctx(ma
), isl_error_invalid
,
9128 "spaces don't match", goto error
);
9129 n_out
= isl_multi_aff_dim(ma
, isl_dim_out
);
9130 if (isl_multi_aff_dim(ma
, isl_dim_in
) == 0)
9131 return mupa_apply_multi_aff_0D(mupa
, ma
);
9133 space1
= isl_space_range(isl_multi_aff_get_space(ma
));
9134 res
= isl_multi_union_pw_aff_alloc(space1
);
9136 for (i
= 0; i
< n_out
; ++i
) {
9138 isl_union_pw_aff
*upa
;
9140 aff
= isl_multi_aff_get_aff(ma
, i
);
9141 upa
= multi_union_pw_aff_apply_aff(
9142 isl_multi_union_pw_aff_copy(mupa
), aff
);
9143 res
= isl_multi_union_pw_aff_set_union_pw_aff(res
, i
, upa
);
9146 isl_multi_aff_free(ma
);
9147 isl_multi_union_pw_aff_free(mupa
);
9150 isl_multi_union_pw_aff_free(mupa
);
9151 isl_multi_aff_free(ma
);
9155 /* Apply "pa" to "mupa", in the special case where "mupa" is 0D.
9156 * The space of "mupa" is known to be compatible with the domain of "pa".
9158 * Construct an isl_multi_union_pw_aff that is equal to "pa"
9159 * on the domain of "mupa".
9161 static __isl_give isl_union_pw_aff
*isl_multi_union_pw_aff_apply_pw_aff_0D(
9162 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_pw_aff
*pa
)
9166 dom
= isl_multi_union_pw_aff_domain(mupa
);
9167 pa
= isl_pw_aff_project_domain_on_params(pa
);
9169 return isl_union_pw_aff_pw_aff_on_domain(dom
, pa
);
9172 /* Apply "pa" to "mupa". The space of "mupa" needs to be compatible
9173 * with the domain of "pa".
9174 * Furthermore, the dimension of this space needs to be greater than zero.
9175 * The result is defined over the shared domain of the elements of "mupa"
9177 __isl_give isl_union_pw_aff
*isl_multi_union_pw_aff_apply_pw_aff(
9178 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_pw_aff
*pa
)
9182 isl_space
*space
, *space2
;
9183 isl_union_pw_aff
*upa
;
9185 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
9186 isl_pw_aff_get_space(pa
));
9187 pa
= isl_pw_aff_align_params(pa
,
9188 isl_multi_union_pw_aff_get_space(mupa
));
9192 space
= isl_multi_union_pw_aff_get_space(mupa
);
9193 space2
= isl_pw_aff_get_domain_space(pa
);
9194 equal
= isl_space_is_equal(space
, space2
);
9195 isl_space_free(space
);
9196 isl_space_free(space2
);
9200 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
9201 "spaces don't match", goto error
);
9202 if (isl_pw_aff_dim(pa
, isl_dim_in
) == 0)
9203 return isl_multi_union_pw_aff_apply_pw_aff_0D(mupa
, pa
);
9205 space
= isl_space_params(isl_multi_union_pw_aff_get_space(mupa
));
9206 upa
= isl_union_pw_aff_empty(space
);
9208 for (i
= 0; i
< pa
->n
; ++i
) {
9211 isl_multi_union_pw_aff
*mupa_i
;
9212 isl_union_pw_aff
*upa_i
;
9214 mupa_i
= isl_multi_union_pw_aff_copy(mupa
);
9215 domain
= isl_set_copy(pa
->p
[i
].set
);
9216 mupa_i
= isl_multi_union_pw_aff_intersect_range(mupa_i
, domain
);
9217 aff
= isl_aff_copy(pa
->p
[i
].aff
);
9218 upa_i
= multi_union_pw_aff_apply_aff(mupa_i
, aff
);
9219 upa
= isl_union_pw_aff_union_add(upa
, upa_i
);
9222 isl_multi_union_pw_aff_free(mupa
);
9223 isl_pw_aff_free(pa
);
9226 isl_multi_union_pw_aff_free(mupa
);
9227 isl_pw_aff_free(pa
);
9231 /* Apply "pma" to "mupa", in the special case where "mupa" is 0D.
9232 * The space of "mupa" is known to be compatible with the domain of "pma".
9234 * Construct an isl_multi_union_pw_aff that is equal to "pma"
9235 * on the domain of "mupa".
9237 static __isl_give isl_multi_union_pw_aff
*mupa_apply_pw_multi_aff_0D(
9238 __isl_take isl_multi_union_pw_aff
*mupa
,
9239 __isl_take isl_pw_multi_aff
*pma
)
9243 dom
= isl_multi_union_pw_aff_domain(mupa
);
9244 pma
= isl_pw_multi_aff_project_domain_on_params(pma
);
9246 return isl_multi_union_pw_aff_pw_multi_aff_on_domain(dom
, pma
);
9249 /* Apply "pma" to "mupa". The space of "mupa" needs to be compatible
9250 * with the domain of "pma".
9251 * The result is defined over the shared domain of the elements of "mupa"
9253 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_apply_pw_multi_aff(
9254 __isl_take isl_multi_union_pw_aff
*mupa
,
9255 __isl_take isl_pw_multi_aff
*pma
)
9257 isl_space
*space1
, *space2
;
9258 isl_multi_union_pw_aff
*res
;
9262 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
9263 isl_pw_multi_aff_get_space(pma
));
9264 pma
= isl_pw_multi_aff_align_params(pma
,
9265 isl_multi_union_pw_aff_get_space(mupa
));
9269 space1
= isl_multi_union_pw_aff_get_space(mupa
);
9270 space2
= isl_pw_multi_aff_get_domain_space(pma
);
9271 equal
= isl_space_is_equal(space1
, space2
);
9272 isl_space_free(space1
);
9273 isl_space_free(space2
);
9277 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
9278 "spaces don't match", goto error
);
9279 n_out
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
9280 if (isl_pw_multi_aff_dim(pma
, isl_dim_in
) == 0)
9281 return mupa_apply_pw_multi_aff_0D(mupa
, pma
);
9283 space1
= isl_space_range(isl_pw_multi_aff_get_space(pma
));
9284 res
= isl_multi_union_pw_aff_alloc(space1
);
9286 for (i
= 0; i
< n_out
; ++i
) {
9288 isl_union_pw_aff
*upa
;
9290 pa
= isl_pw_multi_aff_get_pw_aff(pma
, i
);
9291 upa
= isl_multi_union_pw_aff_apply_pw_aff(
9292 isl_multi_union_pw_aff_copy(mupa
), pa
);
9293 res
= isl_multi_union_pw_aff_set_union_pw_aff(res
, i
, upa
);
9296 isl_pw_multi_aff_free(pma
);
9297 isl_multi_union_pw_aff_free(mupa
);
9300 isl_multi_union_pw_aff_free(mupa
);
9301 isl_pw_multi_aff_free(pma
);
9305 /* Replace the explicit domain of "mupa" by its preimage under "upma".
9306 * If the explicit domain only keeps track of constraints on the parameters,
9307 * then only update those constraints.
9309 static __isl_give isl_multi_union_pw_aff
*preimage_explicit_domain(
9310 __isl_take isl_multi_union_pw_aff
*mupa
,
9311 __isl_keep isl_union_pw_multi_aff
*upma
)
9315 if (isl_multi_union_pw_aff_check_has_explicit_domain(mupa
) < 0)
9316 return isl_multi_union_pw_aff_free(mupa
);
9318 mupa
= isl_multi_union_pw_aff_cow(mupa
);
9322 is_params
= isl_union_set_is_params(mupa
->u
.dom
);
9324 return isl_multi_union_pw_aff_free(mupa
);
9326 upma
= isl_union_pw_multi_aff_copy(upma
);
9328 mupa
->u
.dom
= isl_union_set_intersect_params(mupa
->u
.dom
,
9329 isl_union_set_params(isl_union_pw_multi_aff_domain(upma
)));
9331 mupa
->u
.dom
= isl_union_set_preimage_union_pw_multi_aff(
9334 return isl_multi_union_pw_aff_free(mupa
);
9338 /* Compute the pullback of "mupa" by the function represented by "upma".
9339 * In other words, plug in "upma" in "mupa". The result contains
9340 * expressions defined over the domain space of "upma".
9342 * Run over all elements of "mupa" and plug in "upma" in each of them.
9344 * If "mupa" has an explicit domain, then it is this domain
9345 * that needs to undergo a pullback instead, i.e., a preimage.
9347 __isl_give isl_multi_union_pw_aff
*
9348 isl_multi_union_pw_aff_pullback_union_pw_multi_aff(
9349 __isl_take isl_multi_union_pw_aff
*mupa
,
9350 __isl_take isl_union_pw_multi_aff
*upma
)
9354 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
9355 isl_union_pw_multi_aff_get_space(upma
));
9356 upma
= isl_union_pw_multi_aff_align_params(upma
,
9357 isl_multi_union_pw_aff_get_space(mupa
));
9358 mupa
= isl_multi_union_pw_aff_cow(mupa
);
9362 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
9363 for (i
= 0; i
< n
; ++i
) {
9364 isl_union_pw_aff
*upa
;
9366 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
9367 upa
= isl_union_pw_aff_pullback_union_pw_multi_aff(upa
,
9368 isl_union_pw_multi_aff_copy(upma
));
9369 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
9372 if (isl_multi_union_pw_aff_has_explicit_domain(mupa
))
9373 mupa
= preimage_explicit_domain(mupa
, upma
);
9375 isl_union_pw_multi_aff_free(upma
);
9378 isl_multi_union_pw_aff_free(mupa
);
9379 isl_union_pw_multi_aff_free(upma
);
9383 /* Extract the sequence of elements in "mupa" with domain space "space"
9384 * (ignoring parameters).
9386 * For the elements of "mupa" that are not defined on the specified space,
9387 * the corresponding element in the result is empty.
9389 __isl_give isl_multi_pw_aff
*isl_multi_union_pw_aff_extract_multi_pw_aff(
9390 __isl_keep isl_multi_union_pw_aff
*mupa
, __isl_take isl_space
*space
)
9393 isl_space
*space_mpa
;
9394 isl_multi_pw_aff
*mpa
;
9396 if (!mupa
|| !space
)
9399 space_mpa
= isl_multi_union_pw_aff_get_space(mupa
);
9400 space
= isl_space_replace_params(space
, space_mpa
);
9401 space_mpa
= isl_space_map_from_domain_and_range(isl_space_copy(space
),
9403 mpa
= isl_multi_pw_aff_alloc(space_mpa
);
9405 space
= isl_space_from_domain(space
);
9406 space
= isl_space_add_dims(space
, isl_dim_out
, 1);
9407 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
9408 for (i
= 0; i
< n
; ++i
) {
9409 isl_union_pw_aff
*upa
;
9412 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
9413 pa
= isl_union_pw_aff_extract_pw_aff(upa
,
9414 isl_space_copy(space
));
9415 mpa
= isl_multi_pw_aff_set_pw_aff(mpa
, i
, pa
);
9416 isl_union_pw_aff_free(upa
);
9419 isl_space_free(space
);
9422 isl_space_free(space
);
9426 /* Evaluate the affine function "aff" in the void point "pnt".
9427 * In particular, return the value NaN.
9429 static __isl_give isl_val
*eval_void(__isl_take isl_aff
*aff
,
9430 __isl_take isl_point
*pnt
)
9434 ctx
= isl_point_get_ctx(pnt
);
9436 isl_point_free(pnt
);
9437 return isl_val_nan(ctx
);
9440 /* Evaluate the affine expression "aff"
9441 * in the coordinates (with denominator) "pnt".
9443 static __isl_give isl_val
*eval(__isl_keep isl_vec
*aff
,
9444 __isl_keep isl_vec
*pnt
)
9453 ctx
= isl_vec_get_ctx(aff
);
9456 isl_seq_inner_product(aff
->el
+ 1, pnt
->el
, pnt
->size
, &n
);
9457 isl_int_mul(d
, aff
->el
[0], pnt
->el
[0]);
9458 v
= isl_val_rat_from_isl_int(ctx
, n
, d
);
9459 v
= isl_val_normalize(v
);
9466 /* Check that the domain space of "aff" is equal to "space".
9468 static isl_stat
isl_aff_check_has_domain_space(__isl_keep isl_aff
*aff
,
9469 __isl_keep isl_space
*space
)
9473 ok
= isl_space_is_equal(isl_aff_peek_domain_space(aff
), space
);
9475 return isl_stat_error
;
9477 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
9478 "incompatible spaces", return isl_stat_error
);
9482 /* Evaluate the affine function "aff" in "pnt".
9484 __isl_give isl_val
*isl_aff_eval(__isl_take isl_aff
*aff
,
9485 __isl_take isl_point
*pnt
)
9489 isl_local_space
*ls
;
9491 if (isl_aff_check_has_domain_space(aff
, isl_point_peek_space(pnt
)) < 0)
9493 is_void
= isl_point_is_void(pnt
);
9497 return eval_void(aff
, pnt
);
9499 ls
= isl_aff_get_domain_local_space(aff
);
9500 pnt
= isl_local_space_lift_point(ls
, pnt
);
9502 v
= eval(aff
->v
, isl_point_peek_vec(pnt
));
9505 isl_point_free(pnt
);
9510 isl_point_free(pnt
);