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>
19 #include <isl_map_private.h>
20 #include <isl_union_map_private.h>
21 #include <isl_aff_private.h>
22 #include <isl_space_private.h>
23 #include <isl_local_space_private.h>
24 #include <isl_vec_private.h>
25 #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 /* Project the domain of the affine expression onto its parameter space.
2466 * The affine expression may not involve any of the domain dimensions.
2468 __isl_give isl_aff
*isl_aff_project_domain_on_params(__isl_take isl_aff
*aff
)
2474 n
= isl_aff_dim(aff
, isl_dim_in
);
2475 involves
= isl_aff_involves_dims(aff
, isl_dim_in
, 0, n
);
2477 return isl_aff_free(aff
);
2479 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2480 "affine expression involves some of the domain dimensions",
2481 return isl_aff_free(aff
));
2482 aff
= isl_aff_drop_dims(aff
, isl_dim_in
, 0, n
);
2483 space
= isl_aff_get_domain_space(aff
);
2484 space
= isl_space_params(space
);
2485 aff
= isl_aff_reset_domain_space(aff
, space
);
2489 /* Convert an affine expression defined over a parameter domain
2490 * into one that is defined over a zero-dimensional set.
2492 __isl_give isl_aff
*isl_aff_from_range(__isl_take isl_aff
*aff
)
2494 isl_local_space
*ls
;
2496 ls
= isl_aff_take_domain_local_space(aff
);
2497 ls
= isl_local_space_set_from_params(ls
);
2498 aff
= isl_aff_restore_domain_local_space(aff
, ls
);
2503 __isl_give isl_aff
*isl_aff_insert_dims(__isl_take isl_aff
*aff
,
2504 enum isl_dim_type type
, unsigned first
, unsigned n
)
2510 if (type
== isl_dim_out
)
2511 isl_die(aff
->v
->ctx
, isl_error_invalid
,
2512 "cannot insert output/set dimensions",
2513 return isl_aff_free(aff
));
2514 if (type
== isl_dim_in
)
2516 if (n
== 0 && !isl_local_space_is_named_or_nested(aff
->ls
, type
))
2519 ctx
= isl_aff_get_ctx(aff
);
2520 if (first
> isl_local_space_dim(aff
->ls
, type
))
2521 isl_die(ctx
, isl_error_invalid
, "position out of bounds",
2522 return isl_aff_free(aff
));
2524 aff
= isl_aff_cow(aff
);
2528 aff
->ls
= isl_local_space_insert_dims(aff
->ls
, type
, first
, n
);
2530 return isl_aff_free(aff
);
2532 first
+= 1 + isl_local_space_offset(aff
->ls
, type
);
2533 aff
->v
= isl_vec_insert_zero_els(aff
->v
, first
, n
);
2535 return isl_aff_free(aff
);
2540 __isl_give isl_aff
*isl_aff_add_dims(__isl_take isl_aff
*aff
,
2541 enum isl_dim_type type
, unsigned n
)
2545 pos
= isl_aff_dim(aff
, type
);
2547 return isl_aff_insert_dims(aff
, type
, pos
, n
);
2550 __isl_give isl_pw_aff
*isl_pw_aff_add_dims(__isl_take isl_pw_aff
*pwaff
,
2551 enum isl_dim_type type
, unsigned n
)
2555 pos
= isl_pw_aff_dim(pwaff
, type
);
2557 return isl_pw_aff_insert_dims(pwaff
, type
, pos
, n
);
2560 /* Move the "n" dimensions of "src_type" starting at "src_pos" of "aff"
2561 * to dimensions of "dst_type" at "dst_pos".
2563 * We only support moving input dimensions to parameters and vice versa.
2565 __isl_give isl_aff
*isl_aff_move_dims(__isl_take isl_aff
*aff
,
2566 enum isl_dim_type dst_type
, unsigned dst_pos
,
2567 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
2575 !isl_local_space_is_named_or_nested(aff
->ls
, src_type
) &&
2576 !isl_local_space_is_named_or_nested(aff
->ls
, dst_type
))
2579 if (dst_type
== isl_dim_out
|| src_type
== isl_dim_out
)
2580 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2581 "cannot move output/set dimension",
2582 return isl_aff_free(aff
));
2583 if (dst_type
== isl_dim_div
|| src_type
== isl_dim_div
)
2584 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2585 "cannot move divs", return isl_aff_free(aff
));
2586 if (dst_type
== isl_dim_in
)
2587 dst_type
= isl_dim_set
;
2588 if (src_type
== isl_dim_in
)
2589 src_type
= isl_dim_set
;
2591 if (src_pos
+ n
> isl_local_space_dim(aff
->ls
, src_type
))
2592 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2593 "range out of bounds", return isl_aff_free(aff
));
2594 if (dst_type
== src_type
)
2595 isl_die(isl_aff_get_ctx(aff
), isl_error_unsupported
,
2596 "moving dims within the same type not supported",
2597 return isl_aff_free(aff
));
2599 aff
= isl_aff_cow(aff
);
2603 g_src_pos
= 1 + isl_local_space_offset(aff
->ls
, src_type
) + src_pos
;
2604 g_dst_pos
= 1 + isl_local_space_offset(aff
->ls
, dst_type
) + dst_pos
;
2605 if (dst_type
> src_type
)
2608 aff
->v
= isl_vec_move_els(aff
->v
, g_dst_pos
, g_src_pos
, n
);
2609 aff
->ls
= isl_local_space_move_dims(aff
->ls
, dst_type
, dst_pos
,
2610 src_type
, src_pos
, n
);
2611 if (!aff
->v
|| !aff
->ls
)
2612 return isl_aff_free(aff
);
2614 aff
= sort_divs(aff
);
2619 __isl_give isl_pw_aff
*isl_pw_aff_from_aff(__isl_take isl_aff
*aff
)
2621 isl_set
*dom
= isl_set_universe(isl_aff_get_domain_space(aff
));
2622 return isl_pw_aff_alloc(dom
, aff
);
2625 #define isl_aff_involves_nan isl_aff_is_nan
2628 #define PW isl_pw_aff
2632 #define EL_IS_ZERO is_empty
2636 #define IS_ZERO is_empty
2639 #undef DEFAULT_IS_ZERO
2640 #define DEFAULT_IS_ZERO 0
2646 #include <isl_pw_templ.c>
2647 #include <isl_pw_hash.c>
2648 #include <isl_pw_union_opt.c>
2651 #define UNION isl_union_pw_aff
2653 #define PART isl_pw_aff
2655 #define PARTS pw_aff
2657 #include <isl_union_single.c>
2658 #include <isl_union_neg.c>
2660 static __isl_give isl_set
*align_params_pw_pw_set_and(
2661 __isl_take isl_pw_aff
*pwaff1
, __isl_take isl_pw_aff
*pwaff2
,
2662 __isl_give isl_set
*(*fn
)(__isl_take isl_pw_aff
*pwaff1
,
2663 __isl_take isl_pw_aff
*pwaff2
))
2665 isl_bool equal_params
;
2667 if (!pwaff1
|| !pwaff2
)
2669 equal_params
= isl_space_has_equal_params(pwaff1
->dim
, pwaff2
->dim
);
2670 if (equal_params
< 0)
2673 return fn(pwaff1
, pwaff2
);
2674 if (!isl_space_has_named_params(pwaff1
->dim
) ||
2675 !isl_space_has_named_params(pwaff2
->dim
))
2676 isl_die(isl_pw_aff_get_ctx(pwaff1
), isl_error_invalid
,
2677 "unaligned unnamed parameters", goto error
);
2678 pwaff1
= isl_pw_aff_align_params(pwaff1
, isl_pw_aff_get_space(pwaff2
));
2679 pwaff2
= isl_pw_aff_align_params(pwaff2
, isl_pw_aff_get_space(pwaff1
));
2680 return fn(pwaff1
, pwaff2
);
2682 isl_pw_aff_free(pwaff1
);
2683 isl_pw_aff_free(pwaff2
);
2687 /* Align the parameters of the to isl_pw_aff arguments and
2688 * then apply a function "fn" on them that returns an isl_map.
2690 static __isl_give isl_map
*align_params_pw_pw_map_and(
2691 __isl_take isl_pw_aff
*pa1
, __isl_take isl_pw_aff
*pa2
,
2692 __isl_give isl_map
*(*fn
)(__isl_take isl_pw_aff
*pa1
,
2693 __isl_take isl_pw_aff
*pa2
))
2695 isl_bool equal_params
;
2699 equal_params
= isl_space_has_equal_params(pa1
->dim
, pa2
->dim
);
2700 if (equal_params
< 0)
2703 return fn(pa1
, pa2
);
2704 if (!isl_space_has_named_params(pa1
->dim
) ||
2705 !isl_space_has_named_params(pa2
->dim
))
2706 isl_die(isl_pw_aff_get_ctx(pa1
), isl_error_invalid
,
2707 "unaligned unnamed parameters", goto error
);
2708 pa1
= isl_pw_aff_align_params(pa1
, isl_pw_aff_get_space(pa2
));
2709 pa2
= isl_pw_aff_align_params(pa2
, isl_pw_aff_get_space(pa1
));
2710 return fn(pa1
, pa2
);
2712 isl_pw_aff_free(pa1
);
2713 isl_pw_aff_free(pa2
);
2717 /* Compute a piecewise quasi-affine expression with a domain that
2718 * is the union of those of pwaff1 and pwaff2 and such that on each
2719 * cell, the quasi-affine expression is the maximum of those of pwaff1
2720 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2721 * cell, then the associated expression is the defined one.
2723 static __isl_give isl_pw_aff
*pw_aff_union_max(__isl_take isl_pw_aff
*pwaff1
,
2724 __isl_take isl_pw_aff
*pwaff2
)
2726 return isl_pw_aff_union_opt_cmp(pwaff1
, pwaff2
, &isl_aff_ge_set
);
2729 __isl_give isl_pw_aff
*isl_pw_aff_union_max(__isl_take isl_pw_aff
*pwaff1
,
2730 __isl_take isl_pw_aff
*pwaff2
)
2732 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
,
2736 /* Compute a piecewise quasi-affine expression with a domain that
2737 * is the union of those of pwaff1 and pwaff2 and such that on each
2738 * cell, the quasi-affine expression is the minimum of those of pwaff1
2739 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2740 * cell, then the associated expression is the defined one.
2742 static __isl_give isl_pw_aff
*pw_aff_union_min(__isl_take isl_pw_aff
*pwaff1
,
2743 __isl_take isl_pw_aff
*pwaff2
)
2745 return isl_pw_aff_union_opt_cmp(pwaff1
, pwaff2
, &isl_aff_le_set
);
2748 __isl_give isl_pw_aff
*isl_pw_aff_union_min(__isl_take isl_pw_aff
*pwaff1
,
2749 __isl_take isl_pw_aff
*pwaff2
)
2751 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
,
2755 __isl_give isl_pw_aff
*isl_pw_aff_union_opt(__isl_take isl_pw_aff
*pwaff1
,
2756 __isl_take isl_pw_aff
*pwaff2
, int max
)
2759 return isl_pw_aff_union_max(pwaff1
, pwaff2
);
2761 return isl_pw_aff_union_min(pwaff1
, pwaff2
);
2764 /* Construct a map with as domain the domain of pwaff and
2765 * one-dimensional range corresponding to the affine expressions.
2767 static __isl_give isl_map
*map_from_pw_aff(__isl_take isl_pw_aff
*pwaff
)
2776 dim
= isl_pw_aff_get_space(pwaff
);
2777 map
= isl_map_empty(dim
);
2779 for (i
= 0; i
< pwaff
->n
; ++i
) {
2780 isl_basic_map
*bmap
;
2783 bmap
= isl_basic_map_from_aff(isl_aff_copy(pwaff
->p
[i
].aff
));
2784 map_i
= isl_map_from_basic_map(bmap
);
2785 map_i
= isl_map_intersect_domain(map_i
,
2786 isl_set_copy(pwaff
->p
[i
].set
));
2787 map
= isl_map_union_disjoint(map
, map_i
);
2790 isl_pw_aff_free(pwaff
);
2795 /* Construct a map with as domain the domain of pwaff and
2796 * one-dimensional range corresponding to the affine expressions.
2798 __isl_give isl_map
*isl_map_from_pw_aff(__isl_take isl_pw_aff
*pwaff
)
2802 if (isl_space_is_set(pwaff
->dim
))
2803 isl_die(isl_pw_aff_get_ctx(pwaff
), isl_error_invalid
,
2804 "space of input is not a map", goto error
);
2805 return map_from_pw_aff(pwaff
);
2807 isl_pw_aff_free(pwaff
);
2811 /* Construct a one-dimensional set with as parameter domain
2812 * the domain of pwaff and the single set dimension
2813 * corresponding to the affine expressions.
2815 __isl_give isl_set
*isl_set_from_pw_aff(__isl_take isl_pw_aff
*pwaff
)
2819 if (!isl_space_is_set(pwaff
->dim
))
2820 isl_die(isl_pw_aff_get_ctx(pwaff
), isl_error_invalid
,
2821 "space of input is not a set", goto error
);
2822 return map_from_pw_aff(pwaff
);
2824 isl_pw_aff_free(pwaff
);
2828 /* Return a set containing those elements in the domain
2829 * of "pwaff" where it satisfies "fn" (if complement is 0) or
2830 * does not satisfy "fn" (if complement is 1).
2832 * The pieces with a NaN never belong to the result since
2833 * NaN does not satisfy any property.
2835 static __isl_give isl_set
*pw_aff_locus(__isl_take isl_pw_aff
*pwaff
,
2836 __isl_give isl_basic_set
*(*fn
)(__isl_take isl_aff
*aff
, int rational
),
2845 set
= isl_set_empty(isl_pw_aff_get_domain_space(pwaff
));
2847 for (i
= 0; i
< pwaff
->n
; ++i
) {
2848 isl_basic_set
*bset
;
2849 isl_set
*set_i
, *locus
;
2852 if (isl_aff_is_nan(pwaff
->p
[i
].aff
))
2855 rational
= isl_set_has_rational(pwaff
->p
[i
].set
);
2856 bset
= fn(isl_aff_copy(pwaff
->p
[i
].aff
), rational
);
2857 locus
= isl_set_from_basic_set(bset
);
2858 set_i
= isl_set_copy(pwaff
->p
[i
].set
);
2860 set_i
= isl_set_subtract(set_i
, locus
);
2862 set_i
= isl_set_intersect(set_i
, locus
);
2863 set
= isl_set_union_disjoint(set
, set_i
);
2866 isl_pw_aff_free(pwaff
);
2871 /* Return a set containing those elements in the domain
2872 * of "pa" where it is positive.
2874 __isl_give isl_set
*isl_pw_aff_pos_set(__isl_take isl_pw_aff
*pa
)
2876 return pw_aff_locus(pa
, &aff_pos_basic_set
, 0);
2879 /* Return a set containing those elements in the domain
2880 * of pwaff where it is non-negative.
2882 __isl_give isl_set
*isl_pw_aff_nonneg_set(__isl_take isl_pw_aff
*pwaff
)
2884 return pw_aff_locus(pwaff
, &aff_nonneg_basic_set
, 0);
2887 /* Return a set containing those elements in the domain
2888 * of pwaff where it is zero.
2890 __isl_give isl_set
*isl_pw_aff_zero_set(__isl_take isl_pw_aff
*pwaff
)
2892 return pw_aff_locus(pwaff
, &aff_zero_basic_set
, 0);
2895 /* Return a set containing those elements in the domain
2896 * of pwaff where it is not zero.
2898 __isl_give isl_set
*isl_pw_aff_non_zero_set(__isl_take isl_pw_aff
*pwaff
)
2900 return pw_aff_locus(pwaff
, &aff_zero_basic_set
, 1);
2903 /* Return a set containing those elements in the shared domain
2904 * of pwaff1 and pwaff2 where pwaff1 is greater than (or equal) to pwaff2.
2906 * We compute the difference on the shared domain and then construct
2907 * the set of values where this difference is non-negative.
2908 * If strict is set, we first subtract 1 from the difference.
2909 * If equal is set, we only return the elements where pwaff1 and pwaff2
2912 static __isl_give isl_set
*pw_aff_gte_set(__isl_take isl_pw_aff
*pwaff1
,
2913 __isl_take isl_pw_aff
*pwaff2
, int strict
, int equal
)
2915 isl_set
*set1
, *set2
;
2917 set1
= isl_pw_aff_domain(isl_pw_aff_copy(pwaff1
));
2918 set2
= isl_pw_aff_domain(isl_pw_aff_copy(pwaff2
));
2919 set1
= isl_set_intersect(set1
, set2
);
2920 pwaff1
= isl_pw_aff_intersect_domain(pwaff1
, isl_set_copy(set1
));
2921 pwaff2
= isl_pw_aff_intersect_domain(pwaff2
, isl_set_copy(set1
));
2922 pwaff1
= isl_pw_aff_add(pwaff1
, isl_pw_aff_neg(pwaff2
));
2925 isl_space
*dim
= isl_set_get_space(set1
);
2927 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(dim
));
2928 aff
= isl_aff_add_constant_si(aff
, -1);
2929 pwaff1
= isl_pw_aff_add(pwaff1
, isl_pw_aff_alloc(set1
, aff
));
2934 return isl_pw_aff_zero_set(pwaff1
);
2935 return isl_pw_aff_nonneg_set(pwaff1
);
2938 /* Return a set containing those elements in the shared domain
2939 * of pwaff1 and pwaff2 where pwaff1 is equal to pwaff2.
2941 static __isl_give isl_set
*pw_aff_eq_set(__isl_take isl_pw_aff
*pwaff1
,
2942 __isl_take isl_pw_aff
*pwaff2
)
2944 return pw_aff_gte_set(pwaff1
, pwaff2
, 0, 1);
2947 __isl_give isl_set
*isl_pw_aff_eq_set(__isl_take isl_pw_aff
*pwaff1
,
2948 __isl_take isl_pw_aff
*pwaff2
)
2950 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_eq_set
);
2953 /* Return a set containing those elements in the shared domain
2954 * of pwaff1 and pwaff2 where pwaff1 is greater than or equal to pwaff2.
2956 static __isl_give isl_set
*pw_aff_ge_set(__isl_take isl_pw_aff
*pwaff1
,
2957 __isl_take isl_pw_aff
*pwaff2
)
2959 return pw_aff_gte_set(pwaff1
, pwaff2
, 0, 0);
2962 __isl_give isl_set
*isl_pw_aff_ge_set(__isl_take isl_pw_aff
*pwaff1
,
2963 __isl_take isl_pw_aff
*pwaff2
)
2965 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_ge_set
);
2968 /* Return a set containing those elements in the shared domain
2969 * of pwaff1 and pwaff2 where pwaff1 is strictly greater than pwaff2.
2971 static __isl_give isl_set
*pw_aff_gt_set(__isl_take isl_pw_aff
*pwaff1
,
2972 __isl_take isl_pw_aff
*pwaff2
)
2974 return pw_aff_gte_set(pwaff1
, pwaff2
, 1, 0);
2977 __isl_give isl_set
*isl_pw_aff_gt_set(__isl_take isl_pw_aff
*pwaff1
,
2978 __isl_take isl_pw_aff
*pwaff2
)
2980 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_gt_set
);
2983 __isl_give isl_set
*isl_pw_aff_le_set(__isl_take isl_pw_aff
*pwaff1
,
2984 __isl_take isl_pw_aff
*pwaff2
)
2986 return isl_pw_aff_ge_set(pwaff2
, pwaff1
);
2989 __isl_give isl_set
*isl_pw_aff_lt_set(__isl_take isl_pw_aff
*pwaff1
,
2990 __isl_take isl_pw_aff
*pwaff2
)
2992 return isl_pw_aff_gt_set(pwaff2
, pwaff1
);
2995 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
2996 * where the function values are ordered in the same way as "order",
2997 * which returns a set in the shared domain of its two arguments.
2998 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3000 * Let "pa1" and "pa2" be defined on domains A and B respectively.
3001 * We first pull back the two functions such that they are defined on
3002 * the domain [A -> B]. Then we apply "order", resulting in a set
3003 * in the space [A -> B]. Finally, we unwrap this set to obtain
3004 * a map in the space A -> B.
3006 static __isl_give isl_map
*isl_pw_aff_order_map_aligned(
3007 __isl_take isl_pw_aff
*pa1
, __isl_take isl_pw_aff
*pa2
,
3008 __isl_give isl_set
*(*order
)(__isl_take isl_pw_aff
*pa1
,
3009 __isl_take isl_pw_aff
*pa2
))
3011 isl_space
*space1
, *space2
;
3015 space1
= isl_space_domain(isl_pw_aff_get_space(pa1
));
3016 space2
= isl_space_domain(isl_pw_aff_get_space(pa2
));
3017 space1
= isl_space_map_from_domain_and_range(space1
, space2
);
3018 ma
= isl_multi_aff_domain_map(isl_space_copy(space1
));
3019 pa1
= isl_pw_aff_pullback_multi_aff(pa1
, ma
);
3020 ma
= isl_multi_aff_range_map(space1
);
3021 pa2
= isl_pw_aff_pullback_multi_aff(pa2
, ma
);
3022 set
= order(pa1
, pa2
);
3024 return isl_set_unwrap(set
);
3027 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3028 * where the function values are equal.
3029 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3031 static __isl_give isl_map
*isl_pw_aff_eq_map_aligned(__isl_take isl_pw_aff
*pa1
,
3032 __isl_take isl_pw_aff
*pa2
)
3034 return isl_pw_aff_order_map_aligned(pa1
, pa2
, &isl_pw_aff_eq_set
);
3037 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3038 * where the function values are equal.
3040 __isl_give isl_map
*isl_pw_aff_eq_map(__isl_take isl_pw_aff
*pa1
,
3041 __isl_take isl_pw_aff
*pa2
)
3043 return align_params_pw_pw_map_and(pa1
, pa2
, &isl_pw_aff_eq_map_aligned
);
3046 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3047 * where the function value of "pa1" is less than the function value of "pa2".
3048 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3050 static __isl_give isl_map
*isl_pw_aff_lt_map_aligned(__isl_take isl_pw_aff
*pa1
,
3051 __isl_take isl_pw_aff
*pa2
)
3053 return isl_pw_aff_order_map_aligned(pa1
, pa2
, &isl_pw_aff_lt_set
);
3056 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3057 * where the function value of "pa1" is less than the function value of "pa2".
3059 __isl_give isl_map
*isl_pw_aff_lt_map(__isl_take isl_pw_aff
*pa1
,
3060 __isl_take isl_pw_aff
*pa2
)
3062 return align_params_pw_pw_map_and(pa1
, pa2
, &isl_pw_aff_lt_map_aligned
);
3065 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3066 * where the function value of "pa1" is greater than the function value
3068 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3070 static __isl_give isl_map
*isl_pw_aff_gt_map_aligned(__isl_take isl_pw_aff
*pa1
,
3071 __isl_take isl_pw_aff
*pa2
)
3073 return isl_pw_aff_order_map_aligned(pa1
, pa2
, &isl_pw_aff_gt_set
);
3076 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3077 * where the function value of "pa1" is greater than the function value
3080 __isl_give isl_map
*isl_pw_aff_gt_map(__isl_take isl_pw_aff
*pa1
,
3081 __isl_take isl_pw_aff
*pa2
)
3083 return align_params_pw_pw_map_and(pa1
, pa2
, &isl_pw_aff_gt_map_aligned
);
3086 /* Return a set containing those elements in the shared domain
3087 * of the elements of list1 and list2 where each element in list1
3088 * has the relation specified by "fn" with each element in list2.
3090 static __isl_give isl_set
*pw_aff_list_set(__isl_take isl_pw_aff_list
*list1
,
3091 __isl_take isl_pw_aff_list
*list2
,
3092 __isl_give isl_set
*(*fn
)(__isl_take isl_pw_aff
*pwaff1
,
3093 __isl_take isl_pw_aff
*pwaff2
))
3099 if (!list1
|| !list2
)
3102 ctx
= isl_pw_aff_list_get_ctx(list1
);
3103 if (list1
->n
< 1 || list2
->n
< 1)
3104 isl_die(ctx
, isl_error_invalid
,
3105 "list should contain at least one element", goto error
);
3107 set
= isl_set_universe(isl_pw_aff_get_domain_space(list1
->p
[0]));
3108 for (i
= 0; i
< list1
->n
; ++i
)
3109 for (j
= 0; j
< list2
->n
; ++j
) {
3112 set_ij
= fn(isl_pw_aff_copy(list1
->p
[i
]),
3113 isl_pw_aff_copy(list2
->p
[j
]));
3114 set
= isl_set_intersect(set
, set_ij
);
3117 isl_pw_aff_list_free(list1
);
3118 isl_pw_aff_list_free(list2
);
3121 isl_pw_aff_list_free(list1
);
3122 isl_pw_aff_list_free(list2
);
3126 /* Return a set containing those elements in the shared domain
3127 * of the elements of list1 and list2 where each element in list1
3128 * is equal to each element in list2.
3130 __isl_give isl_set
*isl_pw_aff_list_eq_set(__isl_take isl_pw_aff_list
*list1
,
3131 __isl_take isl_pw_aff_list
*list2
)
3133 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_eq_set
);
3136 __isl_give isl_set
*isl_pw_aff_list_ne_set(__isl_take isl_pw_aff_list
*list1
,
3137 __isl_take isl_pw_aff_list
*list2
)
3139 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_ne_set
);
3142 /* Return a set containing those elements in the shared domain
3143 * of the elements of list1 and list2 where each element in list1
3144 * is less than or equal to each element in list2.
3146 __isl_give isl_set
*isl_pw_aff_list_le_set(__isl_take isl_pw_aff_list
*list1
,
3147 __isl_take isl_pw_aff_list
*list2
)
3149 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_le_set
);
3152 __isl_give isl_set
*isl_pw_aff_list_lt_set(__isl_take isl_pw_aff_list
*list1
,
3153 __isl_take isl_pw_aff_list
*list2
)
3155 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_lt_set
);
3158 __isl_give isl_set
*isl_pw_aff_list_ge_set(__isl_take isl_pw_aff_list
*list1
,
3159 __isl_take isl_pw_aff_list
*list2
)
3161 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_ge_set
);
3164 __isl_give isl_set
*isl_pw_aff_list_gt_set(__isl_take isl_pw_aff_list
*list1
,
3165 __isl_take isl_pw_aff_list
*list2
)
3167 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_gt_set
);
3171 /* Return a set containing those elements in the shared domain
3172 * of pwaff1 and pwaff2 where pwaff1 is not equal to pwaff2.
3174 static __isl_give isl_set
*pw_aff_ne_set(__isl_take isl_pw_aff
*pwaff1
,
3175 __isl_take isl_pw_aff
*pwaff2
)
3177 isl_set
*set_lt
, *set_gt
;
3179 set_lt
= isl_pw_aff_lt_set(isl_pw_aff_copy(pwaff1
),
3180 isl_pw_aff_copy(pwaff2
));
3181 set_gt
= isl_pw_aff_gt_set(pwaff1
, pwaff2
);
3182 return isl_set_union_disjoint(set_lt
, set_gt
);
3185 __isl_give isl_set
*isl_pw_aff_ne_set(__isl_take isl_pw_aff
*pwaff1
,
3186 __isl_take isl_pw_aff
*pwaff2
)
3188 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_ne_set
);
3191 __isl_give isl_pw_aff
*isl_pw_aff_scale_down(__isl_take isl_pw_aff
*pwaff
,
3196 if (isl_int_is_one(v
))
3198 if (!isl_int_is_pos(v
))
3199 isl_die(isl_pw_aff_get_ctx(pwaff
), isl_error_invalid
,
3200 "factor needs to be positive",
3201 return isl_pw_aff_free(pwaff
));
3202 pwaff
= isl_pw_aff_cow(pwaff
);
3208 for (i
= 0; i
< pwaff
->n
; ++i
) {
3209 pwaff
->p
[i
].aff
= isl_aff_scale_down(pwaff
->p
[i
].aff
, v
);
3210 if (!pwaff
->p
[i
].aff
)
3211 return isl_pw_aff_free(pwaff
);
3217 __isl_give isl_pw_aff
*isl_pw_aff_floor(__isl_take isl_pw_aff
*pwaff
)
3221 pwaff
= isl_pw_aff_cow(pwaff
);
3227 for (i
= 0; i
< pwaff
->n
; ++i
) {
3228 pwaff
->p
[i
].aff
= isl_aff_floor(pwaff
->p
[i
].aff
);
3229 if (!pwaff
->p
[i
].aff
)
3230 return isl_pw_aff_free(pwaff
);
3236 __isl_give isl_pw_aff
*isl_pw_aff_ceil(__isl_take isl_pw_aff
*pwaff
)
3240 pwaff
= isl_pw_aff_cow(pwaff
);
3246 for (i
= 0; i
< pwaff
->n
; ++i
) {
3247 pwaff
->p
[i
].aff
= isl_aff_ceil(pwaff
->p
[i
].aff
);
3248 if (!pwaff
->p
[i
].aff
)
3249 return isl_pw_aff_free(pwaff
);
3255 /* Assuming that "cond1" and "cond2" are disjoint,
3256 * return an affine expression that is equal to pwaff1 on cond1
3257 * and to pwaff2 on cond2.
3259 static __isl_give isl_pw_aff
*isl_pw_aff_select(
3260 __isl_take isl_set
*cond1
, __isl_take isl_pw_aff
*pwaff1
,
3261 __isl_take isl_set
*cond2
, __isl_take isl_pw_aff
*pwaff2
)
3263 pwaff1
= isl_pw_aff_intersect_domain(pwaff1
, cond1
);
3264 pwaff2
= isl_pw_aff_intersect_domain(pwaff2
, cond2
);
3266 return isl_pw_aff_add_disjoint(pwaff1
, pwaff2
);
3269 /* Return an affine expression that is equal to pwaff_true for elements
3270 * where "cond" is non-zero and to pwaff_false for elements where "cond"
3272 * That is, return cond ? pwaff_true : pwaff_false;
3274 * If "cond" involves and NaN, then we conservatively return a NaN
3275 * on its entire domain. In principle, we could consider the pieces
3276 * where it is NaN separately from those where it is not.
3278 * If "pwaff_true" and "pwaff_false" are obviously equal to each other,
3279 * then only use the domain of "cond" to restrict the domain.
3281 __isl_give isl_pw_aff
*isl_pw_aff_cond(__isl_take isl_pw_aff
*cond
,
3282 __isl_take isl_pw_aff
*pwaff_true
, __isl_take isl_pw_aff
*pwaff_false
)
3284 isl_set
*cond_true
, *cond_false
;
3289 if (isl_pw_aff_involves_nan(cond
)) {
3290 isl_space
*space
= isl_pw_aff_get_domain_space(cond
);
3291 isl_local_space
*ls
= isl_local_space_from_space(space
);
3292 isl_pw_aff_free(cond
);
3293 isl_pw_aff_free(pwaff_true
);
3294 isl_pw_aff_free(pwaff_false
);
3295 return isl_pw_aff_nan_on_domain(ls
);
3298 pwaff_true
= isl_pw_aff_align_params(pwaff_true
,
3299 isl_pw_aff_get_space(pwaff_false
));
3300 pwaff_false
= isl_pw_aff_align_params(pwaff_false
,
3301 isl_pw_aff_get_space(pwaff_true
));
3302 equal
= isl_pw_aff_plain_is_equal(pwaff_true
, pwaff_false
);
3308 dom
= isl_set_coalesce(isl_pw_aff_domain(cond
));
3309 isl_pw_aff_free(pwaff_false
);
3310 return isl_pw_aff_intersect_domain(pwaff_true
, dom
);
3313 cond_true
= isl_pw_aff_non_zero_set(isl_pw_aff_copy(cond
));
3314 cond_false
= isl_pw_aff_zero_set(cond
);
3315 return isl_pw_aff_select(cond_true
, pwaff_true
,
3316 cond_false
, pwaff_false
);
3318 isl_pw_aff_free(cond
);
3319 isl_pw_aff_free(pwaff_true
);
3320 isl_pw_aff_free(pwaff_false
);
3324 isl_bool
isl_aff_is_cst(__isl_keep isl_aff
*aff
)
3327 return isl_bool_error
;
3329 return isl_seq_first_non_zero(aff
->v
->el
+ 2, aff
->v
->size
- 2) == -1;
3332 /* Check whether pwaff is a piecewise constant.
3334 isl_bool
isl_pw_aff_is_cst(__isl_keep isl_pw_aff
*pwaff
)
3339 return isl_bool_error
;
3341 for (i
= 0; i
< pwaff
->n
; ++i
) {
3342 isl_bool is_cst
= isl_aff_is_cst(pwaff
->p
[i
].aff
);
3343 if (is_cst
< 0 || !is_cst
)
3347 return isl_bool_true
;
3350 /* Are all elements of "mpa" piecewise constants?
3352 isl_bool
isl_multi_pw_aff_is_cst(__isl_keep isl_multi_pw_aff
*mpa
)
3357 return isl_bool_error
;
3359 for (i
= 0; i
< mpa
->n
; ++i
) {
3360 isl_bool is_cst
= isl_pw_aff_is_cst(mpa
->u
.p
[i
]);
3361 if (is_cst
< 0 || !is_cst
)
3365 return isl_bool_true
;
3368 /* Return the product of "aff1" and "aff2".
3370 * If either of the two is NaN, then the result is NaN.
3372 * Otherwise, at least one of "aff1" or "aff2" needs to be a constant.
3374 __isl_give isl_aff
*isl_aff_mul(__isl_take isl_aff
*aff1
,
3375 __isl_take isl_aff
*aff2
)
3380 if (isl_aff_is_nan(aff1
)) {
3384 if (isl_aff_is_nan(aff2
)) {
3389 if (!isl_aff_is_cst(aff2
) && isl_aff_is_cst(aff1
))
3390 return isl_aff_mul(aff2
, aff1
);
3392 if (!isl_aff_is_cst(aff2
))
3393 isl_die(isl_aff_get_ctx(aff1
), isl_error_invalid
,
3394 "at least one affine expression should be constant",
3397 aff1
= isl_aff_cow(aff1
);
3401 aff1
= isl_aff_scale(aff1
, aff2
->v
->el
[1]);
3402 aff1
= isl_aff_scale_down(aff1
, aff2
->v
->el
[0]);
3412 /* Divide "aff1" by "aff2", assuming "aff2" is a constant.
3414 * If either of the two is NaN, then the result is NaN.
3416 __isl_give isl_aff
*isl_aff_div(__isl_take isl_aff
*aff1
,
3417 __isl_take isl_aff
*aff2
)
3425 if (isl_aff_is_nan(aff1
)) {
3429 if (isl_aff_is_nan(aff2
)) {
3434 is_cst
= isl_aff_is_cst(aff2
);
3438 isl_die(isl_aff_get_ctx(aff2
), isl_error_invalid
,
3439 "second argument should be a constant", goto error
);
3444 neg
= isl_int_is_neg(aff2
->v
->el
[1]);
3446 isl_int_neg(aff2
->v
->el
[0], aff2
->v
->el
[0]);
3447 isl_int_neg(aff2
->v
->el
[1], aff2
->v
->el
[1]);
3450 aff1
= isl_aff_scale(aff1
, aff2
->v
->el
[0]);
3451 aff1
= isl_aff_scale_down(aff1
, aff2
->v
->el
[1]);
3454 isl_int_neg(aff2
->v
->el
[0], aff2
->v
->el
[0]);
3455 isl_int_neg(aff2
->v
->el
[1], aff2
->v
->el
[1]);
3466 static __isl_give isl_pw_aff
*pw_aff_add(__isl_take isl_pw_aff
*pwaff1
,
3467 __isl_take isl_pw_aff
*pwaff2
)
3469 return isl_pw_aff_on_shared_domain(pwaff1
, pwaff2
, &isl_aff_add
);
3472 __isl_give isl_pw_aff
*isl_pw_aff_add(__isl_take isl_pw_aff
*pwaff1
,
3473 __isl_take isl_pw_aff
*pwaff2
)
3475 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
, &pw_aff_add
);
3478 __isl_give isl_pw_aff
*isl_pw_aff_union_add(__isl_take isl_pw_aff
*pwaff1
,
3479 __isl_take isl_pw_aff
*pwaff2
)
3481 return isl_pw_aff_union_add_(pwaff1
, pwaff2
);
3484 static __isl_give isl_pw_aff
*pw_aff_mul(__isl_take isl_pw_aff
*pwaff1
,
3485 __isl_take isl_pw_aff
*pwaff2
)
3487 return isl_pw_aff_on_shared_domain(pwaff1
, pwaff2
, &isl_aff_mul
);
3490 __isl_give isl_pw_aff
*isl_pw_aff_mul(__isl_take isl_pw_aff
*pwaff1
,
3491 __isl_take isl_pw_aff
*pwaff2
)
3493 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
, &pw_aff_mul
);
3496 static __isl_give isl_pw_aff
*pw_aff_div(__isl_take isl_pw_aff
*pa1
,
3497 __isl_take isl_pw_aff
*pa2
)
3499 return isl_pw_aff_on_shared_domain(pa1
, pa2
, &isl_aff_div
);
3502 /* Divide "pa1" by "pa2", assuming "pa2" is a piecewise constant.
3504 __isl_give isl_pw_aff
*isl_pw_aff_div(__isl_take isl_pw_aff
*pa1
,
3505 __isl_take isl_pw_aff
*pa2
)
3509 is_cst
= isl_pw_aff_is_cst(pa2
);
3513 isl_die(isl_pw_aff_get_ctx(pa2
), isl_error_invalid
,
3514 "second argument should be a piecewise constant",
3516 return isl_pw_aff_align_params_pw_pw_and(pa1
, pa2
, &pw_aff_div
);
3518 isl_pw_aff_free(pa1
);
3519 isl_pw_aff_free(pa2
);
3523 /* Compute the quotient of the integer division of "pa1" by "pa2"
3524 * with rounding towards zero.
3525 * "pa2" is assumed to be a piecewise constant.
3527 * In particular, return
3529 * pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2)
3532 __isl_give isl_pw_aff
*isl_pw_aff_tdiv_q(__isl_take isl_pw_aff
*pa1
,
3533 __isl_take isl_pw_aff
*pa2
)
3539 is_cst
= isl_pw_aff_is_cst(pa2
);
3543 isl_die(isl_pw_aff_get_ctx(pa2
), isl_error_invalid
,
3544 "second argument should be a piecewise constant",
3547 pa1
= isl_pw_aff_div(pa1
, pa2
);
3549 cond
= isl_pw_aff_nonneg_set(isl_pw_aff_copy(pa1
));
3550 f
= isl_pw_aff_floor(isl_pw_aff_copy(pa1
));
3551 c
= isl_pw_aff_ceil(pa1
);
3552 return isl_pw_aff_cond(isl_set_indicator_function(cond
), f
, c
);
3554 isl_pw_aff_free(pa1
);
3555 isl_pw_aff_free(pa2
);
3559 /* Compute the remainder of the integer division of "pa1" by "pa2"
3560 * with rounding towards zero.
3561 * "pa2" is assumed to be a piecewise constant.
3563 * In particular, return
3565 * pa1 - pa2 * (pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2))
3568 __isl_give isl_pw_aff
*isl_pw_aff_tdiv_r(__isl_take isl_pw_aff
*pa1
,
3569 __isl_take isl_pw_aff
*pa2
)
3574 is_cst
= isl_pw_aff_is_cst(pa2
);
3578 isl_die(isl_pw_aff_get_ctx(pa2
), isl_error_invalid
,
3579 "second argument should be a piecewise constant",
3581 res
= isl_pw_aff_tdiv_q(isl_pw_aff_copy(pa1
), isl_pw_aff_copy(pa2
));
3582 res
= isl_pw_aff_mul(pa2
, res
);
3583 res
= isl_pw_aff_sub(pa1
, res
);
3586 isl_pw_aff_free(pa1
);
3587 isl_pw_aff_free(pa2
);
3591 /* Does either of "pa1" or "pa2" involve any NaN2?
3593 static isl_bool
either_involves_nan(__isl_keep isl_pw_aff
*pa1
,
3594 __isl_keep isl_pw_aff
*pa2
)
3598 has_nan
= isl_pw_aff_involves_nan(pa1
);
3599 if (has_nan
< 0 || has_nan
)
3601 return isl_pw_aff_involves_nan(pa2
);
3604 /* Replace "pa1" and "pa2" (at least one of which involves a NaN)
3605 * by a NaN on their shared domain.
3607 * In principle, the result could be refined to only being NaN
3608 * on the parts of this domain where at least one of "pa1" or "pa2" is NaN.
3610 static __isl_give isl_pw_aff
*replace_by_nan(__isl_take isl_pw_aff
*pa1
,
3611 __isl_take isl_pw_aff
*pa2
)
3613 isl_local_space
*ls
;
3617 dom
= isl_set_intersect(isl_pw_aff_domain(pa1
), isl_pw_aff_domain(pa2
));
3618 ls
= isl_local_space_from_space(isl_set_get_space(dom
));
3619 pa
= isl_pw_aff_nan_on_domain(ls
);
3620 pa
= isl_pw_aff_intersect_domain(pa
, dom
);
3625 static __isl_give isl_pw_aff
*pw_aff_min(__isl_take isl_pw_aff
*pwaff1
,
3626 __isl_take isl_pw_aff
*pwaff2
)
3631 dom
= isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1
)),
3632 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2
)));
3633 le
= isl_pw_aff_le_set(isl_pw_aff_copy(pwaff1
),
3634 isl_pw_aff_copy(pwaff2
));
3635 dom
= isl_set_subtract(dom
, isl_set_copy(le
));
3636 return isl_pw_aff_select(le
, pwaff1
, dom
, pwaff2
);
3639 static __isl_give isl_pw_aff
*pw_aff_max(__isl_take isl_pw_aff
*pwaff1
,
3640 __isl_take isl_pw_aff
*pwaff2
)
3645 dom
= isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1
)),
3646 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2
)));
3647 ge
= isl_pw_aff_ge_set(isl_pw_aff_copy(pwaff1
),
3648 isl_pw_aff_copy(pwaff2
));
3649 dom
= isl_set_subtract(dom
, isl_set_copy(ge
));
3650 return isl_pw_aff_select(ge
, pwaff1
, dom
, pwaff2
);
3653 /* Return an expression for the minimum (if "max" is not set) or
3654 * the maximum (if "max" is set) of "pa1" and "pa2".
3655 * If either expression involves any NaN, then return a NaN
3656 * on the shared domain as result.
3658 static __isl_give isl_pw_aff
*pw_aff_min_max(__isl_take isl_pw_aff
*pa1
,
3659 __isl_take isl_pw_aff
*pa2
, int max
)
3663 has_nan
= either_involves_nan(pa1
, pa2
);
3665 pa1
= isl_pw_aff_free(pa1
);
3667 return replace_by_nan(pa1
, pa2
);
3670 return isl_pw_aff_align_params_pw_pw_and(pa1
, pa2
, &pw_aff_max
);
3672 return isl_pw_aff_align_params_pw_pw_and(pa1
, pa2
, &pw_aff_min
);
3675 /* Return an expression for the minimum of "pwaff1" and "pwaff2".
3677 __isl_give isl_pw_aff
*isl_pw_aff_min(__isl_take isl_pw_aff
*pwaff1
,
3678 __isl_take isl_pw_aff
*pwaff2
)
3680 return pw_aff_min_max(pwaff1
, pwaff2
, 0);
3683 /* Return an expression for the maximum of "pwaff1" and "pwaff2".
3685 __isl_give isl_pw_aff
*isl_pw_aff_max(__isl_take isl_pw_aff
*pwaff1
,
3686 __isl_take isl_pw_aff
*pwaff2
)
3688 return pw_aff_min_max(pwaff1
, pwaff2
, 1);
3691 static __isl_give isl_pw_aff
*pw_aff_list_reduce(
3692 __isl_take isl_pw_aff_list
*list
,
3693 __isl_give isl_pw_aff
*(*fn
)(__isl_take isl_pw_aff
*pwaff1
,
3694 __isl_take isl_pw_aff
*pwaff2
))
3703 ctx
= isl_pw_aff_list_get_ctx(list
);
3705 isl_die(ctx
, isl_error_invalid
,
3706 "list should contain at least one element", goto error
);
3708 res
= isl_pw_aff_copy(list
->p
[0]);
3709 for (i
= 1; i
< list
->n
; ++i
)
3710 res
= fn(res
, isl_pw_aff_copy(list
->p
[i
]));
3712 isl_pw_aff_list_free(list
);
3715 isl_pw_aff_list_free(list
);
3719 /* Return an isl_pw_aff that maps each element in the intersection of the
3720 * domains of the elements of list to the minimal corresponding affine
3723 __isl_give isl_pw_aff
*isl_pw_aff_list_min(__isl_take isl_pw_aff_list
*list
)
3725 return pw_aff_list_reduce(list
, &isl_pw_aff_min
);
3728 /* Return an isl_pw_aff that maps each element in the intersection of the
3729 * domains of the elements of list to the maximal corresponding affine
3732 __isl_give isl_pw_aff
*isl_pw_aff_list_max(__isl_take isl_pw_aff_list
*list
)
3734 return pw_aff_list_reduce(list
, &isl_pw_aff_max
);
3737 /* Mark the domains of "pwaff" as rational.
3739 __isl_give isl_pw_aff
*isl_pw_aff_set_rational(__isl_take isl_pw_aff
*pwaff
)
3743 pwaff
= isl_pw_aff_cow(pwaff
);
3749 for (i
= 0; i
< pwaff
->n
; ++i
) {
3750 pwaff
->p
[i
].set
= isl_set_set_rational(pwaff
->p
[i
].set
);
3751 if (!pwaff
->p
[i
].set
)
3752 return isl_pw_aff_free(pwaff
);
3758 /* Mark the domains of the elements of "list" as rational.
3760 __isl_give isl_pw_aff_list
*isl_pw_aff_list_set_rational(
3761 __isl_take isl_pw_aff_list
*list
)
3771 for (i
= 0; i
< n
; ++i
) {
3774 pa
= isl_pw_aff_list_get_pw_aff(list
, i
);
3775 pa
= isl_pw_aff_set_rational(pa
);
3776 list
= isl_pw_aff_list_set_pw_aff(list
, i
, pa
);
3782 /* Do the parameters of "aff" match those of "space"?
3784 isl_bool
isl_aff_matching_params(__isl_keep isl_aff
*aff
,
3785 __isl_keep isl_space
*space
)
3787 isl_space
*aff_space
;
3791 return isl_bool_error
;
3793 aff_space
= isl_aff_get_domain_space(aff
);
3795 match
= isl_space_has_equal_params(space
, aff_space
);
3797 isl_space_free(aff_space
);
3801 /* Check that the domain space of "aff" matches "space".
3803 isl_stat
isl_aff_check_match_domain_space(__isl_keep isl_aff
*aff
,
3804 __isl_keep isl_space
*space
)
3806 isl_space
*aff_space
;
3810 return isl_stat_error
;
3812 aff_space
= isl_aff_get_domain_space(aff
);
3814 match
= isl_space_has_equal_params(space
, aff_space
);
3818 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
3819 "parameters don't match", goto error
);
3820 match
= isl_space_tuple_is_equal(space
, isl_dim_in
,
3821 aff_space
, isl_dim_set
);
3825 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
3826 "domains don't match", goto error
);
3827 isl_space_free(aff_space
);
3830 isl_space_free(aff_space
);
3831 return isl_stat_error
;
3840 #include <isl_multi_no_explicit_domain.c>
3841 #include <isl_multi_templ.c>
3842 #include <isl_multi_apply_set.c>
3843 #include <isl_multi_cmp.c>
3844 #include <isl_multi_dims.c>
3845 #include <isl_multi_floor.c>
3846 #include <isl_multi_gist.c>
3850 /* Construct an isl_multi_aff living in "space" that corresponds
3851 * to the affine transformation matrix "mat".
3853 __isl_give isl_multi_aff
*isl_multi_aff_from_aff_mat(
3854 __isl_take isl_space
*space
, __isl_take isl_mat
*mat
)
3857 isl_local_space
*ls
= NULL
;
3858 isl_multi_aff
*ma
= NULL
;
3859 int n_row
, n_col
, n_out
, total
;
3865 ctx
= isl_mat_get_ctx(mat
);
3867 n_row
= isl_mat_rows(mat
);
3868 n_col
= isl_mat_cols(mat
);
3870 isl_die(ctx
, isl_error_invalid
,
3871 "insufficient number of rows", goto error
);
3873 isl_die(ctx
, isl_error_invalid
,
3874 "insufficient number of columns", goto error
);
3875 n_out
= isl_space_dim(space
, isl_dim_out
);
3876 total
= isl_space_dim(space
, isl_dim_all
);
3877 if (1 + n_out
!= n_row
|| 2 + total
!= n_row
+ n_col
)
3878 isl_die(ctx
, isl_error_invalid
,
3879 "dimension mismatch", goto error
);
3881 ma
= isl_multi_aff_zero(isl_space_copy(space
));
3882 ls
= isl_local_space_from_space(isl_space_domain(space
));
3884 for (i
= 0; i
< n_row
- 1; ++i
) {
3888 v
= isl_vec_alloc(ctx
, 1 + n_col
);
3891 isl_int_set(v
->el
[0], mat
->row
[0][0]);
3892 isl_seq_cpy(v
->el
+ 1, mat
->row
[1 + i
], n_col
);
3893 v
= isl_vec_normalize(v
);
3894 aff
= isl_aff_alloc_vec(isl_local_space_copy(ls
), v
);
3895 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
3898 isl_local_space_free(ls
);
3902 isl_local_space_free(ls
);
3904 isl_multi_aff_free(ma
);
3908 /* Remove any internal structure of the domain of "ma".
3909 * If there is any such internal structure in the input,
3910 * then the name of the corresponding space is also removed.
3912 __isl_give isl_multi_aff
*isl_multi_aff_flatten_domain(
3913 __isl_take isl_multi_aff
*ma
)
3920 if (!ma
->space
->nested
[0])
3923 space
= isl_multi_aff_get_space(ma
);
3924 space
= isl_space_flatten_domain(space
);
3925 ma
= isl_multi_aff_reset_space(ma
, space
);
3930 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
3931 * of the space to its domain.
3933 __isl_give isl_multi_aff
*isl_multi_aff_domain_map(__isl_take isl_space
*space
)
3936 isl_local_space
*ls
;
3941 if (!isl_space_is_map(space
))
3942 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
3943 "not a map space", goto error
);
3945 n_in
= isl_space_dim(space
, isl_dim_in
);
3946 space
= isl_space_domain_map(space
);
3948 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
3950 isl_space_free(space
);
3954 space
= isl_space_domain(space
);
3955 ls
= isl_local_space_from_space(space
);
3956 for (i
= 0; i
< n_in
; ++i
) {
3959 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
3961 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
3963 isl_local_space_free(ls
);
3966 isl_space_free(space
);
3970 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
3971 * of the space to its range.
3973 __isl_give isl_multi_aff
*isl_multi_aff_range_map(__isl_take isl_space
*space
)
3976 isl_local_space
*ls
;
3981 if (!isl_space_is_map(space
))
3982 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
3983 "not a map space", goto error
);
3985 n_in
= isl_space_dim(space
, isl_dim_in
);
3986 n_out
= isl_space_dim(space
, isl_dim_out
);
3987 space
= isl_space_range_map(space
);
3989 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
3991 isl_space_free(space
);
3995 space
= isl_space_domain(space
);
3996 ls
= isl_local_space_from_space(space
);
3997 for (i
= 0; i
< n_out
; ++i
) {
4000 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
4001 isl_dim_set
, n_in
+ i
);
4002 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
4004 isl_local_space_free(ls
);
4007 isl_space_free(space
);
4011 /* Given a map space, return an isl_pw_multi_aff that maps a wrapped copy
4012 * of the space to its range.
4014 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_range_map(
4015 __isl_take isl_space
*space
)
4017 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_range_map(space
));
4020 /* Given the space of a set and a range of set dimensions,
4021 * construct an isl_multi_aff that projects out those dimensions.
4023 __isl_give isl_multi_aff
*isl_multi_aff_project_out_map(
4024 __isl_take isl_space
*space
, enum isl_dim_type type
,
4025 unsigned first
, unsigned n
)
4028 isl_local_space
*ls
;
4033 if (!isl_space_is_set(space
))
4034 isl_die(isl_space_get_ctx(space
), isl_error_unsupported
,
4035 "expecting set space", goto error
);
4036 if (type
!= isl_dim_set
)
4037 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
4038 "only set dimensions can be projected out", goto error
);
4040 dim
= isl_space_dim(space
, isl_dim_set
);
4041 if (first
+ n
> dim
)
4042 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
4043 "range out of bounds", goto error
);
4045 space
= isl_space_from_domain(space
);
4046 space
= isl_space_add_dims(space
, isl_dim_out
, dim
- n
);
4049 return isl_multi_aff_alloc(space
);
4051 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
4052 space
= isl_space_domain(space
);
4053 ls
= isl_local_space_from_space(space
);
4055 for (i
= 0; i
< first
; ++i
) {
4058 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
4060 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
4063 for (i
= 0; i
< dim
- (first
+ n
); ++i
) {
4066 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
4067 isl_dim_set
, first
+ n
+ i
);
4068 ma
= isl_multi_aff_set_aff(ma
, first
+ i
, aff
);
4071 isl_local_space_free(ls
);
4074 isl_space_free(space
);
4078 /* Given the space of a set and a range of set dimensions,
4079 * construct an isl_pw_multi_aff that projects out those dimensions.
4081 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_project_out_map(
4082 __isl_take isl_space
*space
, enum isl_dim_type type
,
4083 unsigned first
, unsigned n
)
4087 ma
= isl_multi_aff_project_out_map(space
, type
, first
, n
);
4088 return isl_pw_multi_aff_from_multi_aff(ma
);
4091 /* Create an isl_pw_multi_aff with the given isl_multi_aff on a universe
4094 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_multi_aff(
4095 __isl_take isl_multi_aff
*ma
)
4097 isl_set
*dom
= isl_set_universe(isl_multi_aff_get_domain_space(ma
));
4098 return isl_pw_multi_aff_alloc(dom
, ma
);
4101 /* Create a piecewise multi-affine expression in the given space that maps each
4102 * input dimension to the corresponding output dimension.
4104 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_identity(
4105 __isl_take isl_space
*space
)
4107 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_identity(space
));
4110 /* Exploit the equalities in "eq" to simplify the affine expressions.
4112 static __isl_give isl_multi_aff
*isl_multi_aff_substitute_equalities(
4113 __isl_take isl_multi_aff
*maff
, __isl_take isl_basic_set
*eq
)
4117 maff
= isl_multi_aff_cow(maff
);
4121 for (i
= 0; i
< maff
->n
; ++i
) {
4122 maff
->u
.p
[i
] = isl_aff_substitute_equalities(maff
->u
.p
[i
],
4123 isl_basic_set_copy(eq
));
4128 isl_basic_set_free(eq
);
4131 isl_basic_set_free(eq
);
4132 isl_multi_aff_free(maff
);
4136 __isl_give isl_multi_aff
*isl_multi_aff_scale(__isl_take isl_multi_aff
*maff
,
4141 maff
= isl_multi_aff_cow(maff
);
4145 for (i
= 0; i
< maff
->n
; ++i
) {
4146 maff
->u
.p
[i
] = isl_aff_scale(maff
->u
.p
[i
], f
);
4148 return isl_multi_aff_free(maff
);
4154 __isl_give isl_multi_aff
*isl_multi_aff_add_on_domain(__isl_keep isl_set
*dom
,
4155 __isl_take isl_multi_aff
*maff1
, __isl_take isl_multi_aff
*maff2
)
4157 maff1
= isl_multi_aff_add(maff1
, maff2
);
4158 maff1
= isl_multi_aff_gist(maff1
, isl_set_copy(dom
));
4162 int isl_multi_aff_is_empty(__isl_keep isl_multi_aff
*maff
)
4170 /* Return the set of domain elements where "ma1" is lexicographically
4171 * smaller than or equal to "ma2".
4173 __isl_give isl_set
*isl_multi_aff_lex_le_set(__isl_take isl_multi_aff
*ma1
,
4174 __isl_take isl_multi_aff
*ma2
)
4176 return isl_multi_aff_lex_ge_set(ma2
, ma1
);
4179 /* Return the set of domain elements where "ma1" is lexicographically
4180 * smaller than "ma2".
4182 __isl_give isl_set
*isl_multi_aff_lex_lt_set(__isl_take isl_multi_aff
*ma1
,
4183 __isl_take isl_multi_aff
*ma2
)
4185 return isl_multi_aff_lex_gt_set(ma2
, ma1
);
4188 /* Return the set of domain elements where "ma1" and "ma2"
4191 static __isl_give isl_set
*isl_multi_aff_order_set(
4192 __isl_take isl_multi_aff
*ma1
, __isl_take isl_multi_aff
*ma2
,
4193 __isl_give isl_map
*order(__isl_take isl_space
*set_space
))
4196 isl_map
*map1
, *map2
;
4199 map1
= isl_map_from_multi_aff(ma1
);
4200 map2
= isl_map_from_multi_aff(ma2
);
4201 map
= isl_map_range_product(map1
, map2
);
4202 space
= isl_space_range(isl_map_get_space(map
));
4203 space
= isl_space_domain(isl_space_unwrap(space
));
4205 map
= isl_map_intersect_range(map
, isl_map_wrap(ge
));
4207 return isl_map_domain(map
);
4210 /* Return the set of domain elements where "ma1" is lexicographically
4211 * greater than or equal to "ma2".
4213 __isl_give isl_set
*isl_multi_aff_lex_ge_set(__isl_take isl_multi_aff
*ma1
,
4214 __isl_take isl_multi_aff
*ma2
)
4216 return isl_multi_aff_order_set(ma1
, ma2
, &isl_map_lex_ge
);
4219 /* Return the set of domain elements where "ma1" is lexicographically
4220 * greater than "ma2".
4222 __isl_give isl_set
*isl_multi_aff_lex_gt_set(__isl_take isl_multi_aff
*ma1
,
4223 __isl_take isl_multi_aff
*ma2
)
4225 return isl_multi_aff_order_set(ma1
, ma2
, &isl_map_lex_gt
);
4229 #define PW isl_pw_multi_aff
4231 #define EL isl_multi_aff
4233 #define EL_IS_ZERO is_empty
4237 #define IS_ZERO is_empty
4240 #undef DEFAULT_IS_ZERO
4241 #define DEFAULT_IS_ZERO 0
4245 #define NO_INVOLVES_DIMS
4246 #define NO_INSERT_DIMS
4250 #include <isl_pw_templ.c>
4251 #include <isl_pw_union_opt.c>
4256 #define UNION isl_union_pw_multi_aff
4258 #define PART isl_pw_multi_aff
4260 #define PARTS pw_multi_aff
4262 #include <isl_union_multi.c>
4263 #include <isl_union_neg.c>
4265 static __isl_give isl_pw_multi_aff
*pw_multi_aff_union_lexmax(
4266 __isl_take isl_pw_multi_aff
*pma1
,
4267 __isl_take isl_pw_multi_aff
*pma2
)
4269 return isl_pw_multi_aff_union_opt_cmp(pma1
, pma2
,
4270 &isl_multi_aff_lex_ge_set
);
4273 /* Given two piecewise multi affine expressions, return a piecewise
4274 * multi-affine expression defined on the union of the definition domains
4275 * of the inputs that is equal to the lexicographic maximum of the two
4276 * inputs on each cell. If only one of the two inputs is defined on
4277 * a given cell, then it is considered to be the maximum.
4279 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_union_lexmax(
4280 __isl_take isl_pw_multi_aff
*pma1
,
4281 __isl_take isl_pw_multi_aff
*pma2
)
4283 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
4284 &pw_multi_aff_union_lexmax
);
4287 static __isl_give isl_pw_multi_aff
*pw_multi_aff_union_lexmin(
4288 __isl_take isl_pw_multi_aff
*pma1
,
4289 __isl_take isl_pw_multi_aff
*pma2
)
4291 return isl_pw_multi_aff_union_opt_cmp(pma1
, pma2
,
4292 &isl_multi_aff_lex_le_set
);
4295 /* Given two piecewise multi affine expressions, return a piecewise
4296 * multi-affine expression defined on the union of the definition domains
4297 * of the inputs that is equal to the lexicographic minimum of the two
4298 * inputs on each cell. If only one of the two inputs is defined on
4299 * a given cell, then it is considered to be the minimum.
4301 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_union_lexmin(
4302 __isl_take isl_pw_multi_aff
*pma1
,
4303 __isl_take isl_pw_multi_aff
*pma2
)
4305 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
4306 &pw_multi_aff_union_lexmin
);
4309 static __isl_give isl_pw_multi_aff
*pw_multi_aff_add(
4310 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4312 return isl_pw_multi_aff_on_shared_domain(pma1
, pma2
,
4313 &isl_multi_aff_add
);
4316 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_add(
4317 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4319 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
4323 static __isl_give isl_pw_multi_aff
*pw_multi_aff_sub(
4324 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4326 return isl_pw_multi_aff_on_shared_domain(pma1
, pma2
,
4327 &isl_multi_aff_sub
);
4330 /* Subtract "pma2" from "pma1" and return the result.
4332 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_sub(
4333 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4335 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
4339 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_union_add(
4340 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4342 return isl_pw_multi_aff_union_add_(pma1
, pma2
);
4345 /* Compute the sum of "upa1" and "upa2" on the union of their domains,
4346 * with the actual sum on the shared domain and
4347 * the defined expression on the symmetric difference of the domains.
4349 __isl_give isl_union_pw_aff
*isl_union_pw_aff_union_add(
4350 __isl_take isl_union_pw_aff
*upa1
, __isl_take isl_union_pw_aff
*upa2
)
4352 return isl_union_pw_aff_union_add_(upa1
, upa2
);
4355 /* Compute the sum of "upma1" and "upma2" on the union of their domains,
4356 * with the actual sum on the shared domain and
4357 * the defined expression on the symmetric difference of the domains.
4359 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_union_add(
4360 __isl_take isl_union_pw_multi_aff
*upma1
,
4361 __isl_take isl_union_pw_multi_aff
*upma2
)
4363 return isl_union_pw_multi_aff_union_add_(upma1
, upma2
);
4366 /* Given two piecewise multi-affine expressions A -> B and C -> D,
4367 * construct a piecewise multi-affine expression [A -> C] -> [B -> D].
4369 static __isl_give isl_pw_multi_aff
*pw_multi_aff_product(
4370 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4374 isl_pw_multi_aff
*res
;
4379 n
= pma1
->n
* pma2
->n
;
4380 space
= isl_space_product(isl_space_copy(pma1
->dim
),
4381 isl_space_copy(pma2
->dim
));
4382 res
= isl_pw_multi_aff_alloc_size(space
, n
);
4384 for (i
= 0; i
< pma1
->n
; ++i
) {
4385 for (j
= 0; j
< pma2
->n
; ++j
) {
4389 domain
= isl_set_product(isl_set_copy(pma1
->p
[i
].set
),
4390 isl_set_copy(pma2
->p
[j
].set
));
4391 ma
= isl_multi_aff_product(
4392 isl_multi_aff_copy(pma1
->p
[i
].maff
),
4393 isl_multi_aff_copy(pma2
->p
[j
].maff
));
4394 res
= isl_pw_multi_aff_add_piece(res
, domain
, ma
);
4398 isl_pw_multi_aff_free(pma1
);
4399 isl_pw_multi_aff_free(pma2
);
4402 isl_pw_multi_aff_free(pma1
);
4403 isl_pw_multi_aff_free(pma2
);
4407 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_product(
4408 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4410 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
4411 &pw_multi_aff_product
);
4414 /* Construct a map mapping the domain of the piecewise multi-affine expression
4415 * to its range, with each dimension in the range equated to the
4416 * corresponding affine expression on its cell.
4418 * If the domain of "pma" is rational, then so is the constructed "map".
4420 __isl_give isl_map
*isl_map_from_pw_multi_aff(__isl_take isl_pw_multi_aff
*pma
)
4428 map
= isl_map_empty(isl_pw_multi_aff_get_space(pma
));
4430 for (i
= 0; i
< pma
->n
; ++i
) {
4432 isl_multi_aff
*maff
;
4433 isl_basic_map
*bmap
;
4436 rational
= isl_set_is_rational(pma
->p
[i
].set
);
4438 map
= isl_map_free(map
);
4439 maff
= isl_multi_aff_copy(pma
->p
[i
].maff
);
4440 bmap
= isl_basic_map_from_multi_aff2(maff
, rational
);
4441 map_i
= isl_map_from_basic_map(bmap
);
4442 map_i
= isl_map_intersect_domain(map_i
,
4443 isl_set_copy(pma
->p
[i
].set
));
4444 map
= isl_map_union_disjoint(map
, map_i
);
4447 isl_pw_multi_aff_free(pma
);
4451 __isl_give isl_set
*isl_set_from_pw_multi_aff(__isl_take isl_pw_multi_aff
*pma
)
4456 if (!isl_space_is_set(pma
->dim
))
4457 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
4458 "isl_pw_multi_aff cannot be converted into an isl_set",
4461 return isl_map_from_pw_multi_aff(pma
);
4463 isl_pw_multi_aff_free(pma
);
4467 /* Subtract the initial "n" elements in "ma" with coefficients in "c" and
4468 * denominator "denom".
4469 * "denom" is allowed to be negative, in which case the actual denominator
4470 * is -denom and the expressions are added instead.
4472 static __isl_give isl_aff
*subtract_initial(__isl_take isl_aff
*aff
,
4473 __isl_keep isl_multi_aff
*ma
, int n
, isl_int
*c
, isl_int denom
)
4479 first
= isl_seq_first_non_zero(c
, n
);
4483 sign
= isl_int_sgn(denom
);
4485 isl_int_abs(d
, denom
);
4486 for (i
= first
; i
< n
; ++i
) {
4489 if (isl_int_is_zero(c
[i
]))
4491 aff_i
= isl_multi_aff_get_aff(ma
, i
);
4492 aff_i
= isl_aff_scale(aff_i
, c
[i
]);
4493 aff_i
= isl_aff_scale_down(aff_i
, d
);
4495 aff
= isl_aff_sub(aff
, aff_i
);
4497 aff
= isl_aff_add(aff
, aff_i
);
4504 /* Extract an affine expression that expresses the output dimension "pos"
4505 * of "bmap" in terms of the parameters and input dimensions from
4507 * Note that this expression may involve integer divisions defined
4508 * in terms of parameters and input dimensions.
4509 * The equality may also involve references to earlier (but not later)
4510 * output dimensions. These are replaced by the corresponding elements
4513 * If the equality is of the form
4515 * f(i) + h(j) + a x + g(i) = 0,
4517 * with f(i) a linear combinations of the parameters and input dimensions,
4518 * g(i) a linear combination of integer divisions defined in terms of the same
4519 * and h(j) a linear combinations of earlier output dimensions,
4520 * then the affine expression is
4522 * (-f(i) - g(i))/a - h(j)/a
4524 * If the equality is of the form
4526 * f(i) + h(j) - a x + g(i) = 0,
4528 * then the affine expression is
4530 * (f(i) + g(i))/a - h(j)/(-a)
4533 * If "div" refers to an integer division (i.e., it is smaller than
4534 * the number of integer divisions), then the equality constraint
4535 * does involve an integer division (the one at position "div") that
4536 * is defined in terms of output dimensions. However, this integer
4537 * division can be eliminated by exploiting a pair of constraints
4538 * x >= l and x <= l + n, with n smaller than the coefficient of "div"
4539 * in the equality constraint. "ineq" refers to inequality x >= l, i.e.,
4541 * In particular, let
4543 * x = e(i) + m floor(...)
4545 * with e(i) the expression derived above and floor(...) the integer
4546 * division involving output dimensions.
4557 * e(i) + m floor(...) - l = (e(i) + m floor(...) - l) mod m
4558 * = (e(i) - l) mod m
4562 * x - l = (e(i) - l) mod m
4566 * x = ((e(i) - l) mod m) + l
4568 * The variable "shift" below contains the expression -l, which may
4569 * also involve a linear combination of earlier output dimensions.
4571 static __isl_give isl_aff
*extract_aff_from_equality(
4572 __isl_keep isl_basic_map
*bmap
, int pos
, int eq
, int div
, int ineq
,
4573 __isl_keep isl_multi_aff
*ma
)
4576 unsigned n_div
, n_out
;
4578 isl_local_space
*ls
;
4579 isl_aff
*aff
, *shift
;
4582 ctx
= isl_basic_map_get_ctx(bmap
);
4583 ls
= isl_basic_map_get_local_space(bmap
);
4584 ls
= isl_local_space_domain(ls
);
4585 aff
= isl_aff_alloc(isl_local_space_copy(ls
));
4588 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
4589 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4590 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4591 if (isl_int_is_neg(bmap
->eq
[eq
][o_out
+ pos
])) {
4592 isl_seq_cpy(aff
->v
->el
+ 1, bmap
->eq
[eq
], o_out
);
4593 isl_seq_cpy(aff
->v
->el
+ 1 + o_out
,
4594 bmap
->eq
[eq
] + o_out
+ n_out
, n_div
);
4596 isl_seq_neg(aff
->v
->el
+ 1, bmap
->eq
[eq
], o_out
);
4597 isl_seq_neg(aff
->v
->el
+ 1 + o_out
,
4598 bmap
->eq
[eq
] + o_out
+ n_out
, n_div
);
4601 isl_int_set_si(aff
->v
->el
[1 + o_out
+ div
], 0);
4602 isl_int_abs(aff
->v
->el
[0], bmap
->eq
[eq
][o_out
+ pos
]);
4603 aff
= subtract_initial(aff
, ma
, pos
, bmap
->eq
[eq
] + o_out
,
4604 bmap
->eq
[eq
][o_out
+ pos
]);
4606 shift
= isl_aff_alloc(isl_local_space_copy(ls
));
4609 isl_seq_cpy(shift
->v
->el
+ 1, bmap
->ineq
[ineq
], o_out
);
4610 isl_seq_cpy(shift
->v
->el
+ 1 + o_out
,
4611 bmap
->ineq
[ineq
] + o_out
+ n_out
, n_div
);
4612 isl_int_set_si(shift
->v
->el
[0], 1);
4613 shift
= subtract_initial(shift
, ma
, pos
,
4614 bmap
->ineq
[ineq
] + o_out
, ctx
->negone
);
4615 aff
= isl_aff_add(aff
, isl_aff_copy(shift
));
4616 mod
= isl_val_int_from_isl_int(ctx
,
4617 bmap
->eq
[eq
][o_out
+ n_out
+ div
]);
4618 mod
= isl_val_abs(mod
);
4619 aff
= isl_aff_mod_val(aff
, mod
);
4620 aff
= isl_aff_sub(aff
, shift
);
4623 isl_local_space_free(ls
);
4626 isl_local_space_free(ls
);
4631 /* Given a basic map with output dimensions defined
4632 * in terms of the parameters input dimensions and earlier
4633 * output dimensions using an equality (and possibly a pair on inequalities),
4634 * extract an isl_aff that expresses output dimension "pos" in terms
4635 * of the parameters and input dimensions.
4636 * Note that this expression may involve integer divisions defined
4637 * in terms of parameters and input dimensions.
4638 * "ma" contains the expressions corresponding to earlier output dimensions.
4640 * This function shares some similarities with
4641 * isl_basic_map_has_defining_equality and isl_constraint_get_bound.
4643 static __isl_give isl_aff
*extract_isl_aff_from_basic_map(
4644 __isl_keep isl_basic_map
*bmap
, int pos
, __isl_keep isl_multi_aff
*ma
)
4651 eq
= isl_basic_map_output_defining_equality(bmap
, pos
, &div
, &ineq
);
4652 if (eq
>= bmap
->n_eq
)
4653 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
4654 "unable to find suitable equality", return NULL
);
4655 aff
= extract_aff_from_equality(bmap
, pos
, eq
, div
, ineq
, ma
);
4657 aff
= isl_aff_remove_unused_divs(aff
);
4661 /* Given a basic map where each output dimension is defined
4662 * in terms of the parameters and input dimensions using an equality,
4663 * extract an isl_multi_aff that expresses the output dimensions in terms
4664 * of the parameters and input dimensions.
4666 static __isl_give isl_multi_aff
*extract_isl_multi_aff_from_basic_map(
4667 __isl_take isl_basic_map
*bmap
)
4676 ma
= isl_multi_aff_alloc(isl_basic_map_get_space(bmap
));
4677 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4679 for (i
= 0; i
< n_out
; ++i
) {
4682 aff
= extract_isl_aff_from_basic_map(bmap
, i
, ma
);
4683 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
4686 isl_basic_map_free(bmap
);
4691 /* Given a basic set where each set dimension is defined
4692 * in terms of the parameters using an equality,
4693 * extract an isl_multi_aff that expresses the set dimensions in terms
4694 * of the parameters.
4696 __isl_give isl_multi_aff
*isl_multi_aff_from_basic_set_equalities(
4697 __isl_take isl_basic_set
*bset
)
4699 return extract_isl_multi_aff_from_basic_map(bset
);
4702 /* Create an isl_pw_multi_aff that is equivalent to
4703 * isl_map_intersect_domain(isl_map_from_basic_map(bmap), domain).
4704 * The given basic map is such that each output dimension is defined
4705 * in terms of the parameters and input dimensions using an equality.
4707 * Since some applications expect the result of isl_pw_multi_aff_from_map
4708 * to only contain integer affine expressions, we compute the floor
4709 * of the expression before returning.
4711 * Remove all constraints involving local variables without
4712 * an explicit representation (resulting in the removal of those
4713 * local variables) prior to the actual extraction to ensure
4714 * that the local spaces in which the resulting affine expressions
4715 * are created do not contain any unknown local variables.
4716 * Removing such constraints is safe because constraints involving
4717 * unknown local variables are not used to determine whether
4718 * a basic map is obviously single-valued.
4720 static __isl_give isl_pw_multi_aff
*plain_pw_multi_aff_from_map(
4721 __isl_take isl_set
*domain
, __isl_take isl_basic_map
*bmap
)
4725 bmap
= isl_basic_map_drop_constraint_involving_unknown_divs(bmap
);
4726 ma
= extract_isl_multi_aff_from_basic_map(bmap
);
4727 ma
= isl_multi_aff_floor(ma
);
4728 return isl_pw_multi_aff_alloc(domain
, ma
);
4731 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4732 * This obviously only works if the input "map" is single-valued.
4733 * If so, we compute the lexicographic minimum of the image in the form
4734 * of an isl_pw_multi_aff. Since the image is unique, it is equal
4735 * to its lexicographic minimum.
4736 * If the input is not single-valued, we produce an error.
4738 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_base(
4739 __isl_take isl_map
*map
)
4743 isl_pw_multi_aff
*pma
;
4745 sv
= isl_map_is_single_valued(map
);
4749 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
4750 "map is not single-valued", goto error
);
4751 map
= isl_map_make_disjoint(map
);
4755 pma
= isl_pw_multi_aff_empty(isl_map_get_space(map
));
4757 for (i
= 0; i
< map
->n
; ++i
) {
4758 isl_pw_multi_aff
*pma_i
;
4759 isl_basic_map
*bmap
;
4760 bmap
= isl_basic_map_copy(map
->p
[i
]);
4761 pma_i
= isl_basic_map_lexmin_pw_multi_aff(bmap
);
4762 pma
= isl_pw_multi_aff_add_disjoint(pma
, pma_i
);
4772 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
4773 * taking into account that the output dimension at position "d"
4774 * can be represented as
4776 * x = floor((e(...) + c1) / m)
4778 * given that constraint "i" is of the form
4780 * e(...) + c1 - m x >= 0
4783 * Let "map" be of the form
4787 * We construct a mapping
4789 * A -> [A -> x = floor(...)]
4791 * apply that to the map, obtaining
4793 * [A -> x = floor(...)] -> B
4795 * and equate dimension "d" to x.
4796 * We then compute a isl_pw_multi_aff representation of the resulting map
4797 * and plug in the mapping above.
4799 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_div(
4800 __isl_take isl_map
*map
, __isl_take isl_basic_map
*hull
, int d
, int i
)
4804 isl_local_space
*ls
;
4812 isl_pw_multi_aff
*pma
;
4815 is_set
= isl_map_is_set(map
);
4819 offset
= isl_basic_map_offset(hull
, isl_dim_out
);
4820 ctx
= isl_map_get_ctx(map
);
4821 space
= isl_space_domain(isl_map_get_space(map
));
4822 n_in
= isl_space_dim(space
, isl_dim_set
);
4823 n
= isl_space_dim(space
, isl_dim_all
);
4825 v
= isl_vec_alloc(ctx
, 1 + 1 + n
);
4827 isl_int_neg(v
->el
[0], hull
->ineq
[i
][offset
+ d
]);
4828 isl_seq_cpy(v
->el
+ 1, hull
->ineq
[i
], 1 + n
);
4830 isl_basic_map_free(hull
);
4832 ls
= isl_local_space_from_space(isl_space_copy(space
));
4833 aff
= isl_aff_alloc_vec(ls
, v
);
4834 aff
= isl_aff_floor(aff
);
4836 isl_space_free(space
);
4837 ma
= isl_multi_aff_from_aff(aff
);
4839 ma
= isl_multi_aff_identity(isl_space_map_from_set(space
));
4840 ma
= isl_multi_aff_range_product(ma
,
4841 isl_multi_aff_from_aff(aff
));
4844 insert
= isl_map_from_multi_aff(isl_multi_aff_copy(ma
));
4845 map
= isl_map_apply_domain(map
, insert
);
4846 map
= isl_map_equate(map
, isl_dim_in
, n_in
, isl_dim_out
, d
);
4847 pma
= isl_pw_multi_aff_from_map(map
);
4848 pma
= isl_pw_multi_aff_pullback_multi_aff(pma
, ma
);
4853 isl_basic_map_free(hull
);
4857 /* Is constraint "c" of the form
4859 * e(...) + c1 - m x >= 0
4863 * -e(...) + c2 + m x >= 0
4865 * where m > 1 and e only depends on parameters and input dimemnsions?
4867 * "offset" is the offset of the output dimensions
4868 * "pos" is the position of output dimension x.
4870 static int is_potential_div_constraint(isl_int
*c
, int offset
, int d
, int total
)
4872 if (isl_int_is_zero(c
[offset
+ d
]))
4874 if (isl_int_is_one(c
[offset
+ d
]))
4876 if (isl_int_is_negone(c
[offset
+ d
]))
4878 if (isl_seq_first_non_zero(c
+ offset
, d
) != -1)
4880 if (isl_seq_first_non_zero(c
+ offset
+ d
+ 1,
4881 total
- (offset
+ d
+ 1)) != -1)
4886 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4888 * As a special case, we first check if there is any pair of constraints,
4889 * shared by all the basic maps in "map" that force a given dimension
4890 * to be equal to the floor of some affine combination of the input dimensions.
4892 * In particular, if we can find two constraints
4894 * e(...) + c1 - m x >= 0 i.e., m x <= e(...) + c1
4898 * -e(...) + c2 + m x >= 0 i.e., m x >= e(...) - c2
4900 * where m > 1 and e only depends on parameters and input dimemnsions,
4903 * c1 + c2 < m i.e., -c2 >= c1 - (m - 1)
4905 * then we know that we can take
4907 * x = floor((e(...) + c1) / m)
4909 * without having to perform any computation.
4911 * Note that we know that
4915 * If c1 + c2 were 0, then we would have detected an equality during
4916 * simplification. If c1 + c2 were negative, then we would have detected
4919 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_check_div(
4920 __isl_take isl_map
*map
)
4926 isl_basic_map
*hull
;
4928 hull
= isl_map_unshifted_simple_hull(isl_map_copy(map
));
4933 dim
= isl_map_dim(map
, isl_dim_out
);
4934 offset
= isl_basic_map_offset(hull
, isl_dim_out
);
4935 total
= 1 + isl_basic_map_total_dim(hull
);
4937 for (d
= 0; d
< dim
; ++d
) {
4938 for (i
= 0; i
< n
; ++i
) {
4939 if (!is_potential_div_constraint(hull
->ineq
[i
],
4942 for (j
= i
+ 1; j
< n
; ++j
) {
4943 if (!isl_seq_is_neg(hull
->ineq
[i
] + 1,
4944 hull
->ineq
[j
] + 1, total
- 1))
4946 isl_int_add(sum
, hull
->ineq
[i
][0],
4948 if (isl_int_abs_lt(sum
,
4949 hull
->ineq
[i
][offset
+ d
]))
4956 if (isl_int_is_pos(hull
->ineq
[j
][offset
+ d
]))
4958 return pw_multi_aff_from_map_div(map
, hull
, d
, j
);
4962 isl_basic_map_free(hull
);
4963 return pw_multi_aff_from_map_base(map
);
4966 isl_basic_map_free(hull
);
4970 /* Given an affine expression
4972 * [A -> B] -> f(A,B)
4974 * construct an isl_multi_aff
4978 * such that dimension "d" in B' is set to "aff" and the remaining
4979 * dimensions are set equal to the corresponding dimensions in B.
4980 * "n_in" is the dimension of the space A.
4981 * "n_out" is the dimension of the space B.
4983 * If "is_set" is set, then the affine expression is of the form
4987 * and we construct an isl_multi_aff
4991 static __isl_give isl_multi_aff
*range_map(__isl_take isl_aff
*aff
, int d
,
4992 unsigned n_in
, unsigned n_out
, int is_set
)
4996 isl_space
*space
, *space2
;
4997 isl_local_space
*ls
;
4999 space
= isl_aff_get_domain_space(aff
);
5000 ls
= isl_local_space_from_space(isl_space_copy(space
));
5001 space2
= isl_space_copy(space
);
5003 space2
= isl_space_range(isl_space_unwrap(space2
));
5004 space
= isl_space_map_from_domain_and_range(space
, space2
);
5005 ma
= isl_multi_aff_alloc(space
);
5006 ma
= isl_multi_aff_set_aff(ma
, d
, aff
);
5008 for (i
= 0; i
< n_out
; ++i
) {
5011 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
5012 isl_dim_set
, n_in
+ i
);
5013 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
5016 isl_local_space_free(ls
);
5021 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
5022 * taking into account that the dimension at position "d" can be written as
5024 * x = m a + f(..) (1)
5026 * where m is equal to "gcd".
5027 * "i" is the index of the equality in "hull" that defines f(..).
5028 * In particular, the equality is of the form
5030 * f(..) - x + m g(existentials) = 0
5034 * -f(..) + x + m g(existentials) = 0
5036 * We basically plug (1) into "map", resulting in a map with "a"
5037 * in the range instead of "x". The corresponding isl_pw_multi_aff
5038 * defining "a" is then plugged back into (1) to obtain a definition for "x".
5040 * Specifically, given the input map
5044 * We first wrap it into a set
5048 * and define (1) on top of the corresponding space, resulting in "aff".
5049 * We use this to create an isl_multi_aff that maps the output position "d"
5050 * from "a" to "x", leaving all other (intput and output) dimensions unchanged.
5051 * We plug this into the wrapped map, unwrap the result and compute the
5052 * corresponding isl_pw_multi_aff.
5053 * The result is an expression
5061 * so that we can plug that into "aff", after extending the latter to
5067 * If "map" is actually a set, then there is no "A" space, meaning
5068 * that we do not need to perform any wrapping, and that the result
5069 * of the recursive call is of the form
5073 * which is plugged into a mapping of the form
5077 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_stride(
5078 __isl_take isl_map
*map
, __isl_take isl_basic_map
*hull
, int d
, int i
,
5083 isl_local_space
*ls
;
5086 isl_pw_multi_aff
*pma
, *id
;
5092 is_set
= isl_map_is_set(map
);
5096 n_in
= isl_basic_map_dim(hull
, isl_dim_in
);
5097 n_out
= isl_basic_map_dim(hull
, isl_dim_out
);
5098 o_out
= isl_basic_map_offset(hull
, isl_dim_out
);
5103 set
= isl_map_wrap(map
);
5104 space
= isl_space_map_from_set(isl_set_get_space(set
));
5105 ma
= isl_multi_aff_identity(space
);
5106 ls
= isl_local_space_from_space(isl_set_get_space(set
));
5107 aff
= isl_aff_alloc(ls
);
5109 isl_int_set_si(aff
->v
->el
[0], 1);
5110 if (isl_int_is_one(hull
->eq
[i
][o_out
+ d
]))
5111 isl_seq_neg(aff
->v
->el
+ 1, hull
->eq
[i
],
5114 isl_seq_cpy(aff
->v
->el
+ 1, hull
->eq
[i
],
5116 isl_int_set(aff
->v
->el
[1 + o_out
+ d
], gcd
);
5118 ma
= isl_multi_aff_set_aff(ma
, n_in
+ d
, isl_aff_copy(aff
));
5119 set
= isl_set_preimage_multi_aff(set
, ma
);
5121 ma
= range_map(aff
, d
, n_in
, n_out
, is_set
);
5126 map
= isl_set_unwrap(set
);
5127 pma
= isl_pw_multi_aff_from_map(map
);
5130 space
= isl_pw_multi_aff_get_domain_space(pma
);
5131 space
= isl_space_map_from_set(space
);
5132 id
= isl_pw_multi_aff_identity(space
);
5133 pma
= isl_pw_multi_aff_range_product(id
, pma
);
5135 id
= isl_pw_multi_aff_from_multi_aff(ma
);
5136 pma
= isl_pw_multi_aff_pullback_pw_multi_aff(id
, pma
);
5138 isl_basic_map_free(hull
);
5142 isl_basic_map_free(hull
);
5146 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5147 * "hull" contains the equalities valid for "map".
5149 * Check if any of the output dimensions is "strided".
5150 * That is, we check if it can be written as
5154 * with m greater than 1, a some combination of existentially quantified
5155 * variables and f an expression in the parameters and input dimensions.
5156 * If so, we remove the stride in pw_multi_aff_from_map_stride.
5158 * Otherwise, we continue with pw_multi_aff_from_map_check_div for a further
5161 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_check_strides(
5162 __isl_take isl_map
*map
, __isl_take isl_basic_map
*hull
)
5171 n_div
= isl_basic_map_dim(hull
, isl_dim_div
);
5172 o_div
= isl_basic_map_offset(hull
, isl_dim_div
);
5175 isl_basic_map_free(hull
);
5176 return pw_multi_aff_from_map_check_div(map
);
5181 n_out
= isl_basic_map_dim(hull
, isl_dim_out
);
5182 o_out
= isl_basic_map_offset(hull
, isl_dim_out
);
5184 for (i
= 0; i
< n_out
; ++i
) {
5185 for (j
= 0; j
< hull
->n_eq
; ++j
) {
5186 isl_int
*eq
= hull
->eq
[j
];
5187 isl_pw_multi_aff
*res
;
5189 if (!isl_int_is_one(eq
[o_out
+ i
]) &&
5190 !isl_int_is_negone(eq
[o_out
+ i
]))
5192 if (isl_seq_first_non_zero(eq
+ o_out
, i
) != -1)
5194 if (isl_seq_first_non_zero(eq
+ o_out
+ i
+ 1,
5195 n_out
- (i
+ 1)) != -1)
5197 isl_seq_gcd(eq
+ o_div
, n_div
, &gcd
);
5198 if (isl_int_is_zero(gcd
))
5200 if (isl_int_is_one(gcd
))
5203 res
= pw_multi_aff_from_map_stride(map
, hull
,
5211 isl_basic_map_free(hull
);
5212 return pw_multi_aff_from_map_check_div(map
);
5215 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5217 * As a special case, we first check if all output dimensions are uniquely
5218 * defined in terms of the parameters and input dimensions over the entire
5219 * domain. If so, we extract the desired isl_pw_multi_aff directly
5220 * from the affine hull of "map" and its domain.
5222 * Otherwise, continue with pw_multi_aff_from_map_check_strides for more
5225 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_map(__isl_take isl_map
*map
)
5228 isl_basic_map
*hull
;
5233 if (isl_map_n_basic_map(map
) == 1) {
5234 hull
= isl_map_unshifted_simple_hull(isl_map_copy(map
));
5235 hull
= isl_basic_map_plain_affine_hull(hull
);
5236 sv
= isl_basic_map_plain_is_single_valued(hull
);
5238 return plain_pw_multi_aff_from_map(isl_map_domain(map
),
5240 isl_basic_map_free(hull
);
5242 map
= isl_map_detect_equalities(map
);
5243 hull
= isl_map_unshifted_simple_hull(isl_map_copy(map
));
5244 sv
= isl_basic_map_plain_is_single_valued(hull
);
5246 return plain_pw_multi_aff_from_map(isl_map_domain(map
), hull
);
5248 return pw_multi_aff_from_map_check_strides(map
, hull
);
5249 isl_basic_map_free(hull
);
5254 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_set(__isl_take isl_set
*set
)
5256 return isl_pw_multi_aff_from_map(set
);
5259 /* Convert "map" into an isl_pw_multi_aff (if possible) and
5262 static isl_stat
pw_multi_aff_from_map(__isl_take isl_map
*map
, void *user
)
5264 isl_union_pw_multi_aff
**upma
= user
;
5265 isl_pw_multi_aff
*pma
;
5267 pma
= isl_pw_multi_aff_from_map(map
);
5268 *upma
= isl_union_pw_multi_aff_add_pw_multi_aff(*upma
, pma
);
5270 return *upma
? isl_stat_ok
: isl_stat_error
;
5273 /* Create an isl_union_pw_multi_aff with the given isl_aff on a universe
5276 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_aff(
5277 __isl_take isl_aff
*aff
)
5280 isl_pw_multi_aff
*pma
;
5282 ma
= isl_multi_aff_from_aff(aff
);
5283 pma
= isl_pw_multi_aff_from_multi_aff(ma
);
5284 return isl_union_pw_multi_aff_from_pw_multi_aff(pma
);
5287 /* Try and create an isl_union_pw_multi_aff that is equivalent
5288 * to the given isl_union_map.
5289 * The isl_union_map is required to be single-valued in each space.
5290 * Otherwise, an error is produced.
5292 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_union_map(
5293 __isl_take isl_union_map
*umap
)
5296 isl_union_pw_multi_aff
*upma
;
5298 space
= isl_union_map_get_space(umap
);
5299 upma
= isl_union_pw_multi_aff_empty(space
);
5300 if (isl_union_map_foreach_map(umap
, &pw_multi_aff_from_map
, &upma
) < 0)
5301 upma
= isl_union_pw_multi_aff_free(upma
);
5302 isl_union_map_free(umap
);
5307 /* Try and create an isl_union_pw_multi_aff that is equivalent
5308 * to the given isl_union_set.
5309 * The isl_union_set is required to be a singleton in each space.
5310 * Otherwise, an error is produced.
5312 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_union_set(
5313 __isl_take isl_union_set
*uset
)
5315 return isl_union_pw_multi_aff_from_union_map(uset
);
5318 /* Return the piecewise affine expression "set ? 1 : 0".
5320 __isl_give isl_pw_aff
*isl_set_indicator_function(__isl_take isl_set
*set
)
5323 isl_space
*space
= isl_set_get_space(set
);
5324 isl_local_space
*ls
= isl_local_space_from_space(space
);
5325 isl_aff
*zero
= isl_aff_zero_on_domain(isl_local_space_copy(ls
));
5326 isl_aff
*one
= isl_aff_zero_on_domain(ls
);
5328 one
= isl_aff_add_constant_si(one
, 1);
5329 pa
= isl_pw_aff_alloc(isl_set_copy(set
), one
);
5330 set
= isl_set_complement(set
);
5331 pa
= isl_pw_aff_add_disjoint(pa
, isl_pw_aff_alloc(set
, zero
));
5336 /* Plug in "subs" for dimension "type", "pos" of "aff".
5338 * Let i be the dimension to replace and let "subs" be of the form
5342 * and "aff" of the form
5348 * (a f + d g')/(m d)
5350 * where g' is the result of plugging in "subs" in each of the integer
5353 __isl_give isl_aff
*isl_aff_substitute(__isl_take isl_aff
*aff
,
5354 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
5359 aff
= isl_aff_cow(aff
);
5361 return isl_aff_free(aff
);
5363 ctx
= isl_aff_get_ctx(aff
);
5364 if (!isl_space_is_equal(aff
->ls
->dim
, subs
->ls
->dim
))
5365 isl_die(ctx
, isl_error_invalid
,
5366 "spaces don't match", return isl_aff_free(aff
));
5367 if (isl_local_space_dim(subs
->ls
, isl_dim_div
) != 0)
5368 isl_die(ctx
, isl_error_unsupported
,
5369 "cannot handle divs yet", return isl_aff_free(aff
));
5371 aff
->ls
= isl_local_space_substitute(aff
->ls
, type
, pos
, subs
);
5373 return isl_aff_free(aff
);
5375 aff
->v
= isl_vec_cow(aff
->v
);
5377 return isl_aff_free(aff
);
5379 pos
+= isl_local_space_offset(aff
->ls
, type
);
5382 isl_seq_substitute(aff
->v
->el
, pos
, subs
->v
->el
,
5383 aff
->v
->size
, subs
->v
->size
, v
);
5389 /* Plug in "subs" for dimension "type", "pos" in each of the affine
5390 * expressions in "maff".
5392 __isl_give isl_multi_aff
*isl_multi_aff_substitute(
5393 __isl_take isl_multi_aff
*maff
, enum isl_dim_type type
, unsigned pos
,
5394 __isl_keep isl_aff
*subs
)
5398 maff
= isl_multi_aff_cow(maff
);
5400 return isl_multi_aff_free(maff
);
5402 if (type
== isl_dim_in
)
5405 for (i
= 0; i
< maff
->n
; ++i
) {
5406 maff
->u
.p
[i
] = isl_aff_substitute(maff
->u
.p
[i
],
5409 return isl_multi_aff_free(maff
);
5415 /* Plug in "subs" for dimension "type", "pos" of "pma".
5417 * pma is of the form
5421 * while subs is of the form
5423 * v' = B_j(v) -> S_j
5425 * Each pair i,j such that C_ij = A_i \cap B_i is non-empty
5426 * has a contribution in the result, in particular
5428 * C_ij(S_j) -> M_i(S_j)
5430 * Note that plugging in S_j in C_ij may also result in an empty set
5431 * and this contribution should simply be discarded.
5433 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_substitute(
5434 __isl_take isl_pw_multi_aff
*pma
, enum isl_dim_type type
, unsigned pos
,
5435 __isl_keep isl_pw_aff
*subs
)
5438 isl_pw_multi_aff
*res
;
5441 return isl_pw_multi_aff_free(pma
);
5443 n
= pma
->n
* subs
->n
;
5444 res
= isl_pw_multi_aff_alloc_size(isl_space_copy(pma
->dim
), n
);
5446 for (i
= 0; i
< pma
->n
; ++i
) {
5447 for (j
= 0; j
< subs
->n
; ++j
) {
5449 isl_multi_aff
*res_ij
;
5452 common
= isl_set_intersect(
5453 isl_set_copy(pma
->p
[i
].set
),
5454 isl_set_copy(subs
->p
[j
].set
));
5455 common
= isl_set_substitute(common
,
5456 type
, pos
, subs
->p
[j
].aff
);
5457 empty
= isl_set_plain_is_empty(common
);
5458 if (empty
< 0 || empty
) {
5459 isl_set_free(common
);
5465 res_ij
= isl_multi_aff_substitute(
5466 isl_multi_aff_copy(pma
->p
[i
].maff
),
5467 type
, pos
, subs
->p
[j
].aff
);
5469 res
= isl_pw_multi_aff_add_piece(res
, common
, res_ij
);
5473 isl_pw_multi_aff_free(pma
);
5476 isl_pw_multi_aff_free(pma
);
5477 isl_pw_multi_aff_free(res
);
5481 /* Compute the preimage of a range of dimensions in the affine expression "src"
5482 * under "ma" and put the result in "dst". The number of dimensions in "src"
5483 * that precede the range is given by "n_before". The number of dimensions
5484 * in the range is given by the number of output dimensions of "ma".
5485 * The number of dimensions that follow the range is given by "n_after".
5486 * If "has_denom" is set (to one),
5487 * then "src" and "dst" have an extra initial denominator.
5488 * "n_div_ma" is the number of existentials in "ma"
5489 * "n_div_bset" is the number of existentials in "src"
5490 * The resulting "dst" (which is assumed to have been allocated by
5491 * the caller) contains coefficients for both sets of existentials,
5492 * first those in "ma" and then those in "src".
5493 * f, c1, c2 and g are temporary objects that have been initialized
5496 * Let src represent the expression
5498 * (a(p) + f_u u + b v + f_w w + c(divs))/d
5500 * and let ma represent the expressions
5502 * v_i = (r_i(p) + s_i(y) + t_i(divs'))/m_i
5504 * We start out with the following expression for dst:
5506 * (a(p) + f_u u + 0 y + f_w w + 0 divs' + c(divs) + f \sum_i b_i v_i)/d
5508 * with the multiplication factor f initially equal to 1
5509 * and f \sum_i b_i v_i kept separately.
5510 * For each x_i that we substitute, we multiply the numerator
5511 * (and denominator) of dst by c_1 = m_i and add the numerator
5512 * of the x_i expression multiplied by c_2 = f b_i,
5513 * after removing the common factors of c_1 and c_2.
5514 * The multiplication factor f also needs to be multiplied by c_1
5515 * for the next x_j, j > i.
5517 void isl_seq_preimage(isl_int
*dst
, isl_int
*src
,
5518 __isl_keep isl_multi_aff
*ma
, int n_before
, int n_after
,
5519 int n_div_ma
, int n_div_bmap
,
5520 isl_int f
, isl_int c1
, isl_int c2
, isl_int g
, int has_denom
)
5523 int n_param
, n_in
, n_out
;
5526 n_param
= isl_multi_aff_dim(ma
, isl_dim_param
);
5527 n_in
= isl_multi_aff_dim(ma
, isl_dim_in
);
5528 n_out
= isl_multi_aff_dim(ma
, isl_dim_out
);
5530 isl_seq_cpy(dst
, src
, has_denom
+ 1 + n_param
+ n_before
);
5531 o_dst
= o_src
= has_denom
+ 1 + n_param
+ n_before
;
5532 isl_seq_clr(dst
+ o_dst
, n_in
);
5535 isl_seq_cpy(dst
+ o_dst
, src
+ o_src
, n_after
);
5538 isl_seq_clr(dst
+ o_dst
, n_div_ma
);
5540 isl_seq_cpy(dst
+ o_dst
, src
+ o_src
, n_div_bmap
);
5542 isl_int_set_si(f
, 1);
5544 for (i
= 0; i
< n_out
; ++i
) {
5545 int offset
= has_denom
+ 1 + n_param
+ n_before
+ i
;
5547 if (isl_int_is_zero(src
[offset
]))
5549 isl_int_set(c1
, ma
->u
.p
[i
]->v
->el
[0]);
5550 isl_int_mul(c2
, f
, src
[offset
]);
5551 isl_int_gcd(g
, c1
, c2
);
5552 isl_int_divexact(c1
, c1
, g
);
5553 isl_int_divexact(c2
, c2
, g
);
5555 isl_int_mul(f
, f
, c1
);
5558 isl_seq_combine(dst
+ o_dst
, c1
, dst
+ o_dst
,
5559 c2
, ma
->u
.p
[i
]->v
->el
+ o_src
, 1 + n_param
);
5560 o_dst
+= 1 + n_param
;
5561 o_src
+= 1 + n_param
;
5562 isl_seq_scale(dst
+ o_dst
, dst
+ o_dst
, c1
, n_before
);
5564 isl_seq_combine(dst
+ o_dst
, c1
, dst
+ o_dst
,
5565 c2
, ma
->u
.p
[i
]->v
->el
+ o_src
, n_in
);
5568 isl_seq_scale(dst
+ o_dst
, dst
+ o_dst
, c1
, n_after
);
5570 isl_seq_combine(dst
+ o_dst
, c1
, dst
+ o_dst
,
5571 c2
, ma
->u
.p
[i
]->v
->el
+ o_src
, n_div_ma
);
5574 isl_seq_scale(dst
+ o_dst
, dst
+ o_dst
, c1
, n_div_bmap
);
5576 isl_int_mul(dst
[0], dst
[0], c1
);
5580 /* Compute the pullback of "aff" by the function represented by "ma".
5581 * In other words, plug in "ma" in "aff". The result is an affine expression
5582 * defined over the domain space of "ma".
5584 * If "aff" is represented by
5586 * (a(p) + b x + c(divs))/d
5588 * and ma is represented by
5590 * x = D(p) + F(y) + G(divs')
5592 * then the result is
5594 * (a(p) + b D(p) + b F(y) + b G(divs') + c(divs))/d
5596 * The divs in the local space of the input are similarly adjusted
5597 * through a call to isl_local_space_preimage_multi_aff.
5599 __isl_give isl_aff
*isl_aff_pullback_multi_aff(__isl_take isl_aff
*aff
,
5600 __isl_take isl_multi_aff
*ma
)
5602 isl_aff
*res
= NULL
;
5603 isl_local_space
*ls
;
5604 int n_div_aff
, n_div_ma
;
5605 isl_int f
, c1
, c2
, g
;
5607 ma
= isl_multi_aff_align_divs(ma
);
5611 n_div_aff
= isl_aff_dim(aff
, isl_dim_div
);
5612 n_div_ma
= ma
->n
? isl_aff_dim(ma
->u
.p
[0], isl_dim_div
) : 0;
5614 ls
= isl_aff_get_domain_local_space(aff
);
5615 ls
= isl_local_space_preimage_multi_aff(ls
, isl_multi_aff_copy(ma
));
5616 res
= isl_aff_alloc(ls
);
5625 isl_seq_preimage(res
->v
->el
, aff
->v
->el
, ma
, 0, 0, n_div_ma
, n_div_aff
,
5634 isl_multi_aff_free(ma
);
5635 res
= isl_aff_normalize(res
);
5639 isl_multi_aff_free(ma
);
5644 /* Compute the pullback of "aff1" by the function represented by "aff2".
5645 * In other words, plug in "aff2" in "aff1". The result is an affine expression
5646 * defined over the domain space of "aff1".
5648 * The domain of "aff1" should match the range of "aff2", which means
5649 * that it should be single-dimensional.
5651 __isl_give isl_aff
*isl_aff_pullback_aff(__isl_take isl_aff
*aff1
,
5652 __isl_take isl_aff
*aff2
)
5656 ma
= isl_multi_aff_from_aff(aff2
);
5657 return isl_aff_pullback_multi_aff(aff1
, ma
);
5660 /* Compute the pullback of "ma1" by the function represented by "ma2".
5661 * In other words, plug in "ma2" in "ma1".
5663 * The parameters of "ma1" and "ma2" are assumed to have been aligned.
5665 static __isl_give isl_multi_aff
*isl_multi_aff_pullback_multi_aff_aligned(
5666 __isl_take isl_multi_aff
*ma1
, __isl_take isl_multi_aff
*ma2
)
5669 isl_space
*space
= NULL
;
5671 ma2
= isl_multi_aff_align_divs(ma2
);
5672 ma1
= isl_multi_aff_cow(ma1
);
5676 space
= isl_space_join(isl_multi_aff_get_space(ma2
),
5677 isl_multi_aff_get_space(ma1
));
5679 for (i
= 0; i
< ma1
->n
; ++i
) {
5680 ma1
->u
.p
[i
] = isl_aff_pullback_multi_aff(ma1
->u
.p
[i
],
5681 isl_multi_aff_copy(ma2
));
5686 ma1
= isl_multi_aff_reset_space(ma1
, space
);
5687 isl_multi_aff_free(ma2
);
5690 isl_space_free(space
);
5691 isl_multi_aff_free(ma2
);
5692 isl_multi_aff_free(ma1
);
5696 /* Compute the pullback of "ma1" by the function represented by "ma2".
5697 * In other words, plug in "ma2" in "ma1".
5699 __isl_give isl_multi_aff
*isl_multi_aff_pullback_multi_aff(
5700 __isl_take isl_multi_aff
*ma1
, __isl_take isl_multi_aff
*ma2
)
5702 return isl_multi_aff_align_params_multi_multi_and(ma1
, ma2
,
5703 &isl_multi_aff_pullback_multi_aff_aligned
);
5706 /* Extend the local space of "dst" to include the divs
5707 * in the local space of "src".
5709 * If "src" does not have any divs or if the local spaces of "dst" and
5710 * "src" are the same, then no extension is required.
5712 __isl_give isl_aff
*isl_aff_align_divs(__isl_take isl_aff
*dst
,
5713 __isl_keep isl_aff
*src
)
5716 int src_n_div
, dst_n_div
;
5723 return isl_aff_free(dst
);
5725 ctx
= isl_aff_get_ctx(src
);
5726 equal
= isl_local_space_has_equal_space(src
->ls
, dst
->ls
);
5728 return isl_aff_free(dst
);
5730 isl_die(ctx
, isl_error_invalid
,
5731 "spaces don't match", goto error
);
5733 src_n_div
= isl_local_space_dim(src
->ls
, isl_dim_div
);
5736 equal
= isl_local_space_is_equal(src
->ls
, dst
->ls
);
5738 return isl_aff_free(dst
);
5742 dst_n_div
= isl_local_space_dim(dst
->ls
, isl_dim_div
);
5743 exp1
= isl_alloc_array(ctx
, int, src_n_div
);
5744 exp2
= isl_alloc_array(ctx
, int, dst_n_div
);
5745 if (!exp1
|| (dst_n_div
&& !exp2
))
5748 div
= isl_merge_divs(src
->ls
->div
, dst
->ls
->div
, exp1
, exp2
);
5749 dst
= isl_aff_expand_divs(dst
, div
, exp2
);
5757 return isl_aff_free(dst
);
5760 /* Adjust the local spaces of the affine expressions in "maff"
5761 * such that they all have the save divs.
5763 __isl_give isl_multi_aff
*isl_multi_aff_align_divs(
5764 __isl_take isl_multi_aff
*maff
)
5772 maff
= isl_multi_aff_cow(maff
);
5776 for (i
= 1; i
< maff
->n
; ++i
)
5777 maff
->u
.p
[0] = isl_aff_align_divs(maff
->u
.p
[0], maff
->u
.p
[i
]);
5778 for (i
= 1; i
< maff
->n
; ++i
) {
5779 maff
->u
.p
[i
] = isl_aff_align_divs(maff
->u
.p
[i
], maff
->u
.p
[0]);
5781 return isl_multi_aff_free(maff
);
5787 __isl_give isl_aff
*isl_aff_lift(__isl_take isl_aff
*aff
)
5789 aff
= isl_aff_cow(aff
);
5793 aff
->ls
= isl_local_space_lift(aff
->ls
);
5795 return isl_aff_free(aff
);
5800 /* Lift "maff" to a space with extra dimensions such that the result
5801 * has no more existentially quantified variables.
5802 * If "ls" is not NULL, then *ls is assigned the local space that lies
5803 * at the basis of the lifting applied to "maff".
5805 __isl_give isl_multi_aff
*isl_multi_aff_lift(__isl_take isl_multi_aff
*maff
,
5806 __isl_give isl_local_space
**ls
)
5820 isl_space
*space
= isl_multi_aff_get_domain_space(maff
);
5821 *ls
= isl_local_space_from_space(space
);
5823 return isl_multi_aff_free(maff
);
5828 maff
= isl_multi_aff_cow(maff
);
5829 maff
= isl_multi_aff_align_divs(maff
);
5833 n_div
= isl_aff_dim(maff
->u
.p
[0], isl_dim_div
);
5834 space
= isl_multi_aff_get_space(maff
);
5835 space
= isl_space_lift(isl_space_domain(space
), n_div
);
5836 space
= isl_space_extend_domain_with_range(space
,
5837 isl_multi_aff_get_space(maff
));
5839 return isl_multi_aff_free(maff
);
5840 isl_space_free(maff
->space
);
5841 maff
->space
= space
;
5844 *ls
= isl_aff_get_domain_local_space(maff
->u
.p
[0]);
5846 return isl_multi_aff_free(maff
);
5849 for (i
= 0; i
< maff
->n
; ++i
) {
5850 maff
->u
.p
[i
] = isl_aff_lift(maff
->u
.p
[i
]);
5858 isl_local_space_free(*ls
);
5859 return isl_multi_aff_free(maff
);
5863 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma".
5865 __isl_give isl_pw_aff
*isl_pw_multi_aff_get_pw_aff(
5866 __isl_keep isl_pw_multi_aff
*pma
, int pos
)
5876 n_out
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
5877 if (pos
< 0 || pos
>= n_out
)
5878 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
5879 "index out of bounds", return NULL
);
5881 space
= isl_pw_multi_aff_get_space(pma
);
5882 space
= isl_space_drop_dims(space
, isl_dim_out
,
5883 pos
+ 1, n_out
- pos
- 1);
5884 space
= isl_space_drop_dims(space
, isl_dim_out
, 0, pos
);
5886 pa
= isl_pw_aff_alloc_size(space
, pma
->n
);
5887 for (i
= 0; i
< pma
->n
; ++i
) {
5889 aff
= isl_multi_aff_get_aff(pma
->p
[i
].maff
, pos
);
5890 pa
= isl_pw_aff_add_piece(pa
, isl_set_copy(pma
->p
[i
].set
), aff
);
5896 /* Return an isl_pw_multi_aff with the given "set" as domain and
5897 * an unnamed zero-dimensional range.
5899 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_domain(
5900 __isl_take isl_set
*set
)
5905 space
= isl_set_get_space(set
);
5906 space
= isl_space_from_domain(space
);
5907 ma
= isl_multi_aff_zero(space
);
5908 return isl_pw_multi_aff_alloc(set
, ma
);
5911 /* Add an isl_pw_multi_aff with the given "set" as domain and
5912 * an unnamed zero-dimensional range to *user.
5914 static isl_stat
add_pw_multi_aff_from_domain(__isl_take isl_set
*set
,
5917 isl_union_pw_multi_aff
**upma
= user
;
5918 isl_pw_multi_aff
*pma
;
5920 pma
= isl_pw_multi_aff_from_domain(set
);
5921 *upma
= isl_union_pw_multi_aff_add_pw_multi_aff(*upma
, pma
);
5926 /* Return an isl_union_pw_multi_aff with the given "uset" as domain and
5927 * an unnamed zero-dimensional range.
5929 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_domain(
5930 __isl_take isl_union_set
*uset
)
5933 isl_union_pw_multi_aff
*upma
;
5938 space
= isl_union_set_get_space(uset
);
5939 upma
= isl_union_pw_multi_aff_empty(space
);
5941 if (isl_union_set_foreach_set(uset
,
5942 &add_pw_multi_aff_from_domain
, &upma
) < 0)
5945 isl_union_set_free(uset
);
5948 isl_union_set_free(uset
);
5949 isl_union_pw_multi_aff_free(upma
);
5953 /* Convert "pma" to an isl_map and add it to *umap.
5955 static isl_stat
map_from_pw_multi_aff(__isl_take isl_pw_multi_aff
*pma
,
5958 isl_union_map
**umap
= user
;
5961 map
= isl_map_from_pw_multi_aff(pma
);
5962 *umap
= isl_union_map_add_map(*umap
, map
);
5967 /* Construct a union map mapping the domain of the union
5968 * piecewise multi-affine expression to its range, with each dimension
5969 * in the range equated to the corresponding affine expression on its cell.
5971 __isl_give isl_union_map
*isl_union_map_from_union_pw_multi_aff(
5972 __isl_take isl_union_pw_multi_aff
*upma
)
5975 isl_union_map
*umap
;
5980 space
= isl_union_pw_multi_aff_get_space(upma
);
5981 umap
= isl_union_map_empty(space
);
5983 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma
,
5984 &map_from_pw_multi_aff
, &umap
) < 0)
5987 isl_union_pw_multi_aff_free(upma
);
5990 isl_union_pw_multi_aff_free(upma
);
5991 isl_union_map_free(umap
);
5995 /* Local data for bin_entry and the callback "fn".
5997 struct isl_union_pw_multi_aff_bin_data
{
5998 isl_union_pw_multi_aff
*upma2
;
5999 isl_union_pw_multi_aff
*res
;
6000 isl_pw_multi_aff
*pma
;
6001 isl_stat (*fn
)(__isl_take isl_pw_multi_aff
*pma
, void *user
);
6004 /* Given an isl_pw_multi_aff from upma1, store it in data->pma
6005 * and call data->fn for each isl_pw_multi_aff in data->upma2.
6007 static isl_stat
bin_entry(__isl_take isl_pw_multi_aff
*pma
, void *user
)
6009 struct isl_union_pw_multi_aff_bin_data
*data
= user
;
6013 r
= isl_union_pw_multi_aff_foreach_pw_multi_aff(data
->upma2
,
6015 isl_pw_multi_aff_free(pma
);
6020 /* Call "fn" on each pair of isl_pw_multi_affs in "upma1" and "upma2".
6021 * The isl_pw_multi_aff from upma1 is stored in data->pma (where data is
6022 * passed as user field) and the isl_pw_multi_aff from upma2 is available
6023 * as *entry. The callback should adjust data->res if desired.
6025 static __isl_give isl_union_pw_multi_aff
*bin_op(
6026 __isl_take isl_union_pw_multi_aff
*upma1
,
6027 __isl_take isl_union_pw_multi_aff
*upma2
,
6028 isl_stat (*fn
)(__isl_take isl_pw_multi_aff
*pma
, void *user
))
6031 struct isl_union_pw_multi_aff_bin_data data
= { NULL
, NULL
, NULL
, fn
};
6033 space
= isl_union_pw_multi_aff_get_space(upma2
);
6034 upma1
= isl_union_pw_multi_aff_align_params(upma1
, space
);
6035 space
= isl_union_pw_multi_aff_get_space(upma1
);
6036 upma2
= isl_union_pw_multi_aff_align_params(upma2
, space
);
6038 if (!upma1
|| !upma2
)
6042 data
.res
= isl_union_pw_multi_aff_alloc_same_size(upma1
);
6043 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma1
,
6044 &bin_entry
, &data
) < 0)
6047 isl_union_pw_multi_aff_free(upma1
);
6048 isl_union_pw_multi_aff_free(upma2
);
6051 isl_union_pw_multi_aff_free(upma1
);
6052 isl_union_pw_multi_aff_free(upma2
);
6053 isl_union_pw_multi_aff_free(data
.res
);
6057 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
6058 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
6060 static __isl_give isl_pw_multi_aff
*pw_multi_aff_range_product(
6061 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
6065 space
= isl_space_range_product(isl_pw_multi_aff_get_space(pma1
),
6066 isl_pw_multi_aff_get_space(pma2
));
6067 return isl_pw_multi_aff_on_shared_domain_in(pma1
, pma2
, space
,
6068 &isl_multi_aff_range_product
);
6071 /* Given two isl_pw_multi_affs A -> B and C -> D,
6072 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
6074 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_range_product(
6075 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
6077 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
6078 &pw_multi_aff_range_product
);
6081 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
6082 * construct an isl_pw_multi_aff (A * C) -> (B, D).
6084 static __isl_give isl_pw_multi_aff
*pw_multi_aff_flat_range_product(
6085 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
6089 space
= isl_space_range_product(isl_pw_multi_aff_get_space(pma1
),
6090 isl_pw_multi_aff_get_space(pma2
));
6091 space
= isl_space_flatten_range(space
);
6092 return isl_pw_multi_aff_on_shared_domain_in(pma1
, pma2
, space
,
6093 &isl_multi_aff_flat_range_product
);
6096 /* Given two isl_pw_multi_affs A -> B and C -> D,
6097 * construct an isl_pw_multi_aff (A * C) -> (B, D).
6099 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_flat_range_product(
6100 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
6102 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
6103 &pw_multi_aff_flat_range_product
);
6106 /* If data->pma and "pma2" have the same domain space, then compute
6107 * their flat range product and the result to data->res.
6109 static isl_stat
flat_range_product_entry(__isl_take isl_pw_multi_aff
*pma2
,
6112 struct isl_union_pw_multi_aff_bin_data
*data
= user
;
6114 if (!isl_space_tuple_is_equal(data
->pma
->dim
, isl_dim_in
,
6115 pma2
->dim
, isl_dim_in
)) {
6116 isl_pw_multi_aff_free(pma2
);
6120 pma2
= isl_pw_multi_aff_flat_range_product(
6121 isl_pw_multi_aff_copy(data
->pma
), pma2
);
6123 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma2
);
6128 /* Given two isl_union_pw_multi_affs A -> B and C -> D,
6129 * construct an isl_union_pw_multi_aff (A * C) -> (B, D).
6131 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_flat_range_product(
6132 __isl_take isl_union_pw_multi_aff
*upma1
,
6133 __isl_take isl_union_pw_multi_aff
*upma2
)
6135 return bin_op(upma1
, upma2
, &flat_range_product_entry
);
6138 /* Replace the affine expressions at position "pos" in "pma" by "pa".
6139 * The parameters are assumed to have been aligned.
6141 * The implementation essentially performs an isl_pw_*_on_shared_domain,
6142 * except that it works on two different isl_pw_* types.
6144 static __isl_give isl_pw_multi_aff
*pw_multi_aff_set_pw_aff(
6145 __isl_take isl_pw_multi_aff
*pma
, unsigned pos
,
6146 __isl_take isl_pw_aff
*pa
)
6149 isl_pw_multi_aff
*res
= NULL
;
6154 if (!isl_space_tuple_is_equal(pma
->dim
, isl_dim_in
,
6155 pa
->dim
, isl_dim_in
))
6156 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
6157 "domains don't match", goto error
);
6158 if (pos
>= isl_pw_multi_aff_dim(pma
, isl_dim_out
))
6159 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
6160 "index out of bounds", goto error
);
6163 res
= isl_pw_multi_aff_alloc_size(isl_pw_multi_aff_get_space(pma
), n
);
6165 for (i
= 0; i
< pma
->n
; ++i
) {
6166 for (j
= 0; j
< pa
->n
; ++j
) {
6168 isl_multi_aff
*res_ij
;
6171 common
= isl_set_intersect(isl_set_copy(pma
->p
[i
].set
),
6172 isl_set_copy(pa
->p
[j
].set
));
6173 empty
= isl_set_plain_is_empty(common
);
6174 if (empty
< 0 || empty
) {
6175 isl_set_free(common
);
6181 res_ij
= isl_multi_aff_set_aff(
6182 isl_multi_aff_copy(pma
->p
[i
].maff
), pos
,
6183 isl_aff_copy(pa
->p
[j
].aff
));
6184 res_ij
= isl_multi_aff_gist(res_ij
,
6185 isl_set_copy(common
));
6187 res
= isl_pw_multi_aff_add_piece(res
, common
, res_ij
);
6191 isl_pw_multi_aff_free(pma
);
6192 isl_pw_aff_free(pa
);
6195 isl_pw_multi_aff_free(pma
);
6196 isl_pw_aff_free(pa
);
6197 return isl_pw_multi_aff_free(res
);
6200 /* Replace the affine expressions at position "pos" in "pma" by "pa".
6202 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_set_pw_aff(
6203 __isl_take isl_pw_multi_aff
*pma
, unsigned pos
,
6204 __isl_take isl_pw_aff
*pa
)
6206 isl_bool equal_params
;
6210 equal_params
= isl_space_has_equal_params(pma
->dim
, pa
->dim
);
6211 if (equal_params
< 0)
6214 return pw_multi_aff_set_pw_aff(pma
, pos
, pa
);
6215 if (!isl_space_has_named_params(pma
->dim
) ||
6216 !isl_space_has_named_params(pa
->dim
))
6217 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
6218 "unaligned unnamed parameters", goto error
);
6219 pma
= isl_pw_multi_aff_align_params(pma
, isl_pw_aff_get_space(pa
));
6220 pa
= isl_pw_aff_align_params(pa
, isl_pw_multi_aff_get_space(pma
));
6221 return pw_multi_aff_set_pw_aff(pma
, pos
, pa
);
6223 isl_pw_multi_aff_free(pma
);
6224 isl_pw_aff_free(pa
);
6228 /* Do the parameters of "pa" match those of "space"?
6230 isl_bool
isl_pw_aff_matching_params(__isl_keep isl_pw_aff
*pa
,
6231 __isl_keep isl_space
*space
)
6233 isl_space
*pa_space
;
6237 return isl_bool_error
;
6239 pa_space
= isl_pw_aff_get_space(pa
);
6241 match
= isl_space_has_equal_params(space
, pa_space
);
6243 isl_space_free(pa_space
);
6247 /* Check that the domain space of "pa" matches "space".
6249 isl_stat
isl_pw_aff_check_match_domain_space(__isl_keep isl_pw_aff
*pa
,
6250 __isl_keep isl_space
*space
)
6252 isl_space
*pa_space
;
6256 return isl_stat_error
;
6258 pa_space
= isl_pw_aff_get_space(pa
);
6260 match
= isl_space_has_equal_params(space
, pa_space
);
6264 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
6265 "parameters don't match", goto error
);
6266 match
= isl_space_tuple_is_equal(space
, isl_dim_in
,
6267 pa_space
, isl_dim_in
);
6271 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
6272 "domains don't match", goto error
);
6273 isl_space_free(pa_space
);
6276 isl_space_free(pa_space
);
6277 return isl_stat_error
;
6285 #include <isl_multi_explicit_domain.c>
6286 #include <isl_multi_pw_aff_explicit_domain.c>
6287 #include <isl_multi_templ.c>
6288 #include <isl_multi_apply_set.c>
6289 #include <isl_multi_coalesce.c>
6290 #include <isl_multi_dims.c>
6291 #include <isl_multi_gist.c>
6292 #include <isl_multi_hash.c>
6293 #include <isl_multi_align_set.c>
6294 #include <isl_multi_intersect.c>
6296 /* Does "mpa" have a non-trivial explicit domain?
6298 * The explicit domain, if present, is trivial if it represents
6299 * an (obviously) universe set.
6301 isl_bool
isl_multi_pw_aff_has_non_trivial_domain(
6302 __isl_keep isl_multi_pw_aff
*mpa
)
6305 return isl_bool_error
;
6306 if (!isl_multi_pw_aff_has_explicit_domain(mpa
))
6307 return isl_bool_false
;
6308 return isl_bool_not(isl_set_plain_is_universe(mpa
->u
.dom
));
6311 /* Scale the elements of "pma" by the corresponding elements of "mv".
6313 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_scale_multi_val(
6314 __isl_take isl_pw_multi_aff
*pma
, __isl_take isl_multi_val
*mv
)
6317 isl_bool equal_params
;
6319 pma
= isl_pw_multi_aff_cow(pma
);
6322 if (!isl_space_tuple_is_equal(pma
->dim
, isl_dim_out
,
6323 mv
->space
, isl_dim_set
))
6324 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
6325 "spaces don't match", goto error
);
6326 equal_params
= isl_space_has_equal_params(pma
->dim
, mv
->space
);
6327 if (equal_params
< 0)
6329 if (!equal_params
) {
6330 pma
= isl_pw_multi_aff_align_params(pma
,
6331 isl_multi_val_get_space(mv
));
6332 mv
= isl_multi_val_align_params(mv
,
6333 isl_pw_multi_aff_get_space(pma
));
6338 for (i
= 0; i
< pma
->n
; ++i
) {
6339 pma
->p
[i
].maff
= isl_multi_aff_scale_multi_val(pma
->p
[i
].maff
,
6340 isl_multi_val_copy(mv
));
6341 if (!pma
->p
[i
].maff
)
6345 isl_multi_val_free(mv
);
6348 isl_multi_val_free(mv
);
6349 isl_pw_multi_aff_free(pma
);
6353 /* This function is called for each entry of an isl_union_pw_multi_aff.
6354 * If the space of the entry matches that of data->mv,
6355 * then apply isl_pw_multi_aff_scale_multi_val and return the result.
6356 * Otherwise, return an empty isl_pw_multi_aff.
6358 static __isl_give isl_pw_multi_aff
*union_pw_multi_aff_scale_multi_val_entry(
6359 __isl_take isl_pw_multi_aff
*pma
, void *user
)
6361 isl_multi_val
*mv
= user
;
6365 if (!isl_space_tuple_is_equal(pma
->dim
, isl_dim_out
,
6366 mv
->space
, isl_dim_set
)) {
6367 isl_space
*space
= isl_pw_multi_aff_get_space(pma
);
6368 isl_pw_multi_aff_free(pma
);
6369 return isl_pw_multi_aff_empty(space
);
6372 return isl_pw_multi_aff_scale_multi_val(pma
, isl_multi_val_copy(mv
));
6375 /* Scale the elements of "upma" by the corresponding elements of "mv",
6376 * for those entries that match the space of "mv".
6378 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_scale_multi_val(
6379 __isl_take isl_union_pw_multi_aff
*upma
, __isl_take isl_multi_val
*mv
)
6381 upma
= isl_union_pw_multi_aff_align_params(upma
,
6382 isl_multi_val_get_space(mv
));
6383 mv
= isl_multi_val_align_params(mv
,
6384 isl_union_pw_multi_aff_get_space(upma
));
6388 return isl_union_pw_multi_aff_transform(upma
,
6389 &union_pw_multi_aff_scale_multi_val_entry
, mv
);
6391 isl_multi_val_free(mv
);
6394 isl_multi_val_free(mv
);
6395 isl_union_pw_multi_aff_free(upma
);
6399 /* Construct and return a piecewise multi affine expression
6400 * in the given space with value zero in each of the output dimensions and
6401 * a universe domain.
6403 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_zero(__isl_take isl_space
*space
)
6405 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_zero(space
));
6408 /* Construct and return a piecewise multi affine expression
6409 * that is equal to the given piecewise affine expression.
6411 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_pw_aff(
6412 __isl_take isl_pw_aff
*pa
)
6416 isl_pw_multi_aff
*pma
;
6421 space
= isl_pw_aff_get_space(pa
);
6422 pma
= isl_pw_multi_aff_alloc_size(space
, pa
->n
);
6424 for (i
= 0; i
< pa
->n
; ++i
) {
6428 set
= isl_set_copy(pa
->p
[i
].set
);
6429 ma
= isl_multi_aff_from_aff(isl_aff_copy(pa
->p
[i
].aff
));
6430 pma
= isl_pw_multi_aff_add_piece(pma
, set
, ma
);
6433 isl_pw_aff_free(pa
);
6437 /* Construct a set or map mapping the shared (parameter) domain
6438 * of the piecewise affine expressions to the range of "mpa"
6439 * with each dimension in the range equated to the
6440 * corresponding piecewise affine expression.
6442 static __isl_give isl_map
*map_from_multi_pw_aff(
6443 __isl_take isl_multi_pw_aff
*mpa
)
6452 if (isl_space_dim(mpa
->space
, isl_dim_out
) != mpa
->n
)
6453 isl_die(isl_multi_pw_aff_get_ctx(mpa
), isl_error_internal
,
6454 "invalid space", goto error
);
6456 space
= isl_multi_pw_aff_get_domain_space(mpa
);
6457 map
= isl_map_universe(isl_space_from_domain(space
));
6459 for (i
= 0; i
< mpa
->n
; ++i
) {
6463 pa
= isl_pw_aff_copy(mpa
->u
.p
[i
]);
6464 map_i
= map_from_pw_aff(pa
);
6466 map
= isl_map_flat_range_product(map
, map_i
);
6469 map
= isl_map_reset_space(map
, isl_multi_pw_aff_get_space(mpa
));
6471 isl_multi_pw_aff_free(mpa
);
6474 isl_multi_pw_aff_free(mpa
);
6478 /* Construct a map mapping the shared 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 __isl_give isl_map
*isl_map_from_multi_pw_aff(__isl_take isl_multi_pw_aff
*mpa
)
6487 if (isl_space_is_set(mpa
->space
))
6488 isl_die(isl_multi_pw_aff_get_ctx(mpa
), isl_error_internal
,
6489 "space of input is not a map", goto error
);
6491 return map_from_multi_pw_aff(mpa
);
6493 isl_multi_pw_aff_free(mpa
);
6497 /* Construct a set mapping the shared parameter domain
6498 * of the piecewise affine expressions to the space of "mpa"
6499 * with each dimension in the range equated to the
6500 * corresponding piecewise affine expression.
6502 __isl_give isl_set
*isl_set_from_multi_pw_aff(__isl_take isl_multi_pw_aff
*mpa
)
6506 if (!isl_space_is_set(mpa
->space
))
6507 isl_die(isl_multi_pw_aff_get_ctx(mpa
), isl_error_internal
,
6508 "space of input is not a set", goto error
);
6510 return map_from_multi_pw_aff(mpa
);
6512 isl_multi_pw_aff_free(mpa
);
6516 /* Construct and return a piecewise multi affine expression
6517 * that is equal to the given multi piecewise affine expression
6518 * on the shared domain of the piecewise affine expressions,
6519 * in the special case of a 0D multi piecewise affine expression.
6521 * Create a piecewise multi affine expression with the explicit domain of
6522 * the 0D multi piecewise affine expression as domain.
6524 static __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_multi_pw_aff_0D(
6525 __isl_take isl_multi_pw_aff
*mpa
)
6531 space
= isl_multi_pw_aff_get_space(mpa
);
6532 dom
= isl_multi_pw_aff_get_explicit_domain(mpa
);
6533 isl_multi_pw_aff_free(mpa
);
6535 ma
= isl_multi_aff_zero(space
);
6536 return isl_pw_multi_aff_alloc(dom
, ma
);
6539 /* Construct and return a piecewise multi affine expression
6540 * that is equal to the given multi piecewise affine expression
6541 * on the shared domain of the piecewise affine expressions.
6543 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_multi_pw_aff(
6544 __isl_take isl_multi_pw_aff
*mpa
)
6549 isl_pw_multi_aff
*pma
;
6555 return isl_pw_multi_aff_from_multi_pw_aff_0D(mpa
);
6557 space
= isl_multi_pw_aff_get_space(mpa
);
6558 pa
= isl_multi_pw_aff_get_pw_aff(mpa
, 0);
6559 pma
= isl_pw_multi_aff_from_pw_aff(pa
);
6561 for (i
= 1; i
< mpa
->n
; ++i
) {
6562 isl_pw_multi_aff
*pma_i
;
6564 pa
= isl_multi_pw_aff_get_pw_aff(mpa
, i
);
6565 pma_i
= isl_pw_multi_aff_from_pw_aff(pa
);
6566 pma
= isl_pw_multi_aff_range_product(pma
, pma_i
);
6569 pma
= isl_pw_multi_aff_reset_space(pma
, space
);
6571 isl_multi_pw_aff_free(mpa
);
6575 /* Construct and return a multi piecewise affine expression
6576 * that is equal to the given multi affine expression.
6578 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_from_multi_aff(
6579 __isl_take isl_multi_aff
*ma
)
6582 isl_multi_pw_aff
*mpa
;
6587 n
= isl_multi_aff_dim(ma
, isl_dim_out
);
6588 mpa
= isl_multi_pw_aff_alloc(isl_multi_aff_get_space(ma
));
6590 for (i
= 0; i
< n
; ++i
) {
6593 pa
= isl_pw_aff_from_aff(isl_multi_aff_get_aff(ma
, i
));
6594 mpa
= isl_multi_pw_aff_set_pw_aff(mpa
, i
, pa
);
6597 isl_multi_aff_free(ma
);
6601 /* Construct and return a multi piecewise affine expression
6602 * that is equal to the given piecewise multi affine expression.
6604 * If the resulting multi piecewise affine expression has
6605 * an explicit domain, then assign it the domain of the input.
6606 * In other cases, the domain is stored in the individual elements.
6608 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_from_pw_multi_aff(
6609 __isl_take isl_pw_multi_aff
*pma
)
6613 isl_multi_pw_aff
*mpa
;
6618 n
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
6619 space
= isl_pw_multi_aff_get_space(pma
);
6620 mpa
= isl_multi_pw_aff_alloc(space
);
6622 for (i
= 0; i
< n
; ++i
) {
6625 pa
= isl_pw_multi_aff_get_pw_aff(pma
, i
);
6626 mpa
= isl_multi_pw_aff_set_pw_aff(mpa
, i
, pa
);
6628 if (isl_multi_pw_aff_has_explicit_domain(mpa
)) {
6631 dom
= isl_pw_multi_aff_domain(isl_pw_multi_aff_copy(pma
));
6632 mpa
= isl_multi_pw_aff_intersect_domain(mpa
, dom
);
6635 isl_pw_multi_aff_free(pma
);
6639 /* Do "pa1" and "pa2" represent the same function?
6641 * We first check if they are obviously equal.
6642 * If not, we convert them to maps and check if those are equal.
6644 * If "pa1" or "pa2" contain any NaNs, then they are considered
6645 * not to be the same. A NaN is not equal to anything, not even
6648 isl_bool
isl_pw_aff_is_equal(__isl_keep isl_pw_aff
*pa1
,
6649 __isl_keep isl_pw_aff
*pa2
)
6653 isl_map
*map1
, *map2
;
6656 return isl_bool_error
;
6658 equal
= isl_pw_aff_plain_is_equal(pa1
, pa2
);
6659 if (equal
< 0 || equal
)
6661 has_nan
= either_involves_nan(pa1
, pa2
);
6663 return isl_bool_error
;
6665 return isl_bool_false
;
6667 map1
= map_from_pw_aff(isl_pw_aff_copy(pa1
));
6668 map2
= map_from_pw_aff(isl_pw_aff_copy(pa2
));
6669 equal
= isl_map_is_equal(map1
, map2
);
6676 /* Do "mpa1" and "mpa2" represent the same function?
6678 * Note that we cannot convert the entire isl_multi_pw_aff
6679 * to a map because the domains of the piecewise affine expressions
6680 * may not be the same.
6682 isl_bool
isl_multi_pw_aff_is_equal(__isl_keep isl_multi_pw_aff
*mpa1
,
6683 __isl_keep isl_multi_pw_aff
*mpa2
)
6686 isl_bool equal
, equal_params
;
6689 return isl_bool_error
;
6691 equal_params
= isl_space_has_equal_params(mpa1
->space
, mpa2
->space
);
6692 if (equal_params
< 0)
6693 return isl_bool_error
;
6694 if (!equal_params
) {
6695 if (!isl_space_has_named_params(mpa1
->space
))
6696 return isl_bool_false
;
6697 if (!isl_space_has_named_params(mpa2
->space
))
6698 return isl_bool_false
;
6699 mpa1
= isl_multi_pw_aff_copy(mpa1
);
6700 mpa2
= isl_multi_pw_aff_copy(mpa2
);
6701 mpa1
= isl_multi_pw_aff_align_params(mpa1
,
6702 isl_multi_pw_aff_get_space(mpa2
));
6703 mpa2
= isl_multi_pw_aff_align_params(mpa2
,
6704 isl_multi_pw_aff_get_space(mpa1
));
6705 equal
= isl_multi_pw_aff_is_equal(mpa1
, mpa2
);
6706 isl_multi_pw_aff_free(mpa1
);
6707 isl_multi_pw_aff_free(mpa2
);
6711 equal
= isl_space_is_equal(mpa1
->space
, mpa2
->space
);
6712 if (equal
< 0 || !equal
)
6715 for (i
= 0; i
< mpa1
->n
; ++i
) {
6716 equal
= isl_pw_aff_is_equal(mpa1
->u
.p
[i
], mpa2
->u
.p
[i
]);
6717 if (equal
< 0 || !equal
)
6721 return isl_bool_true
;
6724 /* Do "pma1" and "pma2" represent the same function?
6726 * First check if they are obviously equal.
6727 * If not, then convert them to maps and check if those are equal.
6729 * If "pa1" or "pa2" contain any NaNs, then they are considered
6730 * not to be the same. A NaN is not equal to anything, not even
6733 isl_bool
isl_pw_multi_aff_is_equal(__isl_keep isl_pw_multi_aff
*pma1
,
6734 __isl_keep isl_pw_multi_aff
*pma2
)
6738 isl_map
*map1
, *map2
;
6741 return isl_bool_error
;
6743 equal
= isl_pw_multi_aff_plain_is_equal(pma1
, pma2
);
6744 if (equal
< 0 || equal
)
6746 has_nan
= isl_pw_multi_aff_involves_nan(pma1
);
6747 if (has_nan
>= 0 && !has_nan
)
6748 has_nan
= isl_pw_multi_aff_involves_nan(pma2
);
6749 if (has_nan
< 0 || has_nan
)
6750 return isl_bool_not(has_nan
);
6752 map1
= isl_map_from_pw_multi_aff(isl_pw_multi_aff_copy(pma1
));
6753 map2
= isl_map_from_pw_multi_aff(isl_pw_multi_aff_copy(pma2
));
6754 equal
= isl_map_is_equal(map1
, map2
);
6761 /* Compute the pullback of "mpa" by the function represented by "ma".
6762 * In other words, plug in "ma" in "mpa".
6764 * The parameters of "mpa" and "ma" are assumed to have been aligned.
6766 * If "mpa" has an explicit domain, then it is this domain
6767 * that needs to undergo a pullback, i.e., a preimage.
6769 static __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_pullback_multi_aff_aligned(
6770 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_multi_aff
*ma
)
6773 isl_space
*space
= NULL
;
6775 mpa
= isl_multi_pw_aff_cow(mpa
);
6779 space
= isl_space_join(isl_multi_aff_get_space(ma
),
6780 isl_multi_pw_aff_get_space(mpa
));
6784 for (i
= 0; i
< mpa
->n
; ++i
) {
6785 mpa
->u
.p
[i
] = isl_pw_aff_pullback_multi_aff(mpa
->u
.p
[i
],
6786 isl_multi_aff_copy(ma
));
6790 if (isl_multi_pw_aff_has_explicit_domain(mpa
)) {
6791 mpa
->u
.dom
= isl_set_preimage_multi_aff(mpa
->u
.dom
,
6792 isl_multi_aff_copy(ma
));
6797 isl_multi_aff_free(ma
);
6798 isl_space_free(mpa
->space
);
6802 isl_space_free(space
);
6803 isl_multi_pw_aff_free(mpa
);
6804 isl_multi_aff_free(ma
);
6808 /* Compute the pullback of "mpa" by the function represented by "ma".
6809 * In other words, plug in "ma" in "mpa".
6811 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_pullback_multi_aff(
6812 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_multi_aff
*ma
)
6814 isl_bool equal_params
;
6818 equal_params
= isl_space_has_equal_params(mpa
->space
, ma
->space
);
6819 if (equal_params
< 0)
6822 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa
, ma
);
6823 mpa
= isl_multi_pw_aff_align_params(mpa
, isl_multi_aff_get_space(ma
));
6824 ma
= isl_multi_aff_align_params(ma
, isl_multi_pw_aff_get_space(mpa
));
6825 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa
, ma
);
6827 isl_multi_pw_aff_free(mpa
);
6828 isl_multi_aff_free(ma
);
6832 /* Compute the pullback of "mpa" by the function represented by "pma".
6833 * In other words, plug in "pma" in "mpa".
6835 * The parameters of "mpa" and "mpa" are assumed to have been aligned.
6837 * If "mpa" has an explicit domain, then it is this domain
6838 * that needs to undergo a pullback, i.e., a preimage.
6840 static __isl_give isl_multi_pw_aff
*
6841 isl_multi_pw_aff_pullback_pw_multi_aff_aligned(
6842 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_pw_multi_aff
*pma
)
6845 isl_space
*space
= NULL
;
6847 mpa
= isl_multi_pw_aff_cow(mpa
);
6851 space
= isl_space_join(isl_pw_multi_aff_get_space(pma
),
6852 isl_multi_pw_aff_get_space(mpa
));
6854 for (i
= 0; i
< mpa
->n
; ++i
) {
6855 mpa
->u
.p
[i
] = isl_pw_aff_pullback_pw_multi_aff_aligned(
6856 mpa
->u
.p
[i
], isl_pw_multi_aff_copy(pma
));
6860 if (isl_multi_pw_aff_has_explicit_domain(mpa
)) {
6861 mpa
->u
.dom
= isl_set_preimage_pw_multi_aff(mpa
->u
.dom
,
6862 isl_pw_multi_aff_copy(pma
));
6867 isl_pw_multi_aff_free(pma
);
6868 isl_space_free(mpa
->space
);
6872 isl_space_free(space
);
6873 isl_multi_pw_aff_free(mpa
);
6874 isl_pw_multi_aff_free(pma
);
6878 /* Compute the pullback of "mpa" by the function represented by "pma".
6879 * In other words, plug in "pma" in "mpa".
6881 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_pullback_pw_multi_aff(
6882 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_pw_multi_aff
*pma
)
6884 isl_bool equal_params
;
6888 equal_params
= isl_space_has_equal_params(mpa
->space
, pma
->dim
);
6889 if (equal_params
< 0)
6892 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa
, pma
);
6893 mpa
= isl_multi_pw_aff_align_params(mpa
,
6894 isl_pw_multi_aff_get_space(pma
));
6895 pma
= isl_pw_multi_aff_align_params(pma
,
6896 isl_multi_pw_aff_get_space(mpa
));
6897 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa
, pma
);
6899 isl_multi_pw_aff_free(mpa
);
6900 isl_pw_multi_aff_free(pma
);
6904 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
6905 * with the domain of "aff". The domain of the result is the same
6907 * "mpa" and "aff" are assumed to have been aligned.
6909 * We first extract the parametric constant from "aff", defined
6910 * over the correct domain.
6911 * Then we add the appropriate combinations of the members of "mpa".
6912 * Finally, we add the integer divisions through recursive calls.
6914 static __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_aff_aligned(
6915 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_aff
*aff
)
6923 n_in
= isl_aff_dim(aff
, isl_dim_in
);
6924 n_div
= isl_aff_dim(aff
, isl_dim_div
);
6926 space
= isl_space_domain(isl_multi_pw_aff_get_space(mpa
));
6927 tmp
= isl_aff_copy(aff
);
6928 tmp
= isl_aff_drop_dims(tmp
, isl_dim_div
, 0, n_div
);
6929 tmp
= isl_aff_drop_dims(tmp
, isl_dim_in
, 0, n_in
);
6930 tmp
= isl_aff_add_dims(tmp
, isl_dim_in
,
6931 isl_space_dim(space
, isl_dim_set
));
6932 tmp
= isl_aff_reset_domain_space(tmp
, space
);
6933 pa
= isl_pw_aff_from_aff(tmp
);
6935 for (i
= 0; i
< n_in
; ++i
) {
6938 if (!isl_aff_involves_dims(aff
, isl_dim_in
, i
, 1))
6940 v
= isl_aff_get_coefficient_val(aff
, isl_dim_in
, i
);
6941 pa_i
= isl_multi_pw_aff_get_pw_aff(mpa
, i
);
6942 pa_i
= isl_pw_aff_scale_val(pa_i
, v
);
6943 pa
= isl_pw_aff_add(pa
, pa_i
);
6946 for (i
= 0; i
< n_div
; ++i
) {
6950 if (!isl_aff_involves_dims(aff
, isl_dim_div
, i
, 1))
6952 div
= isl_aff_get_div(aff
, i
);
6953 pa_i
= isl_multi_pw_aff_apply_aff_aligned(
6954 isl_multi_pw_aff_copy(mpa
), div
);
6955 pa_i
= isl_pw_aff_floor(pa_i
);
6956 v
= isl_aff_get_coefficient_val(aff
, isl_dim_div
, i
);
6957 pa_i
= isl_pw_aff_scale_val(pa_i
, v
);
6958 pa
= isl_pw_aff_add(pa
, pa_i
);
6961 isl_multi_pw_aff_free(mpa
);
6967 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
6968 * with the domain of "aff". The domain of the result is the same
6971 __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_aff(
6972 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_aff
*aff
)
6974 isl_bool equal_params
;
6978 equal_params
= isl_space_has_equal_params(aff
->ls
->dim
, mpa
->space
);
6979 if (equal_params
< 0)
6982 return isl_multi_pw_aff_apply_aff_aligned(mpa
, aff
);
6984 aff
= isl_aff_align_params(aff
, isl_multi_pw_aff_get_space(mpa
));
6985 mpa
= isl_multi_pw_aff_align_params(mpa
, isl_aff_get_space(aff
));
6987 return isl_multi_pw_aff_apply_aff_aligned(mpa
, aff
);
6990 isl_multi_pw_aff_free(mpa
);
6994 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
6995 * with the domain of "pa". The domain of the result is the same
6997 * "mpa" and "pa" are assumed to have been aligned.
6999 * We consider each piece in turn. Note that the domains of the
7000 * pieces are assumed to be disjoint and they remain disjoint
7001 * after taking the preimage (over the same function).
7003 static __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_pw_aff_aligned(
7004 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_pw_aff
*pa
)
7013 space
= isl_space_join(isl_multi_pw_aff_get_space(mpa
),
7014 isl_pw_aff_get_space(pa
));
7015 res
= isl_pw_aff_empty(space
);
7017 for (i
= 0; i
< pa
->n
; ++i
) {
7021 pa_i
= isl_multi_pw_aff_apply_aff_aligned(
7022 isl_multi_pw_aff_copy(mpa
),
7023 isl_aff_copy(pa
->p
[i
].aff
));
7024 domain
= isl_set_copy(pa
->p
[i
].set
);
7025 domain
= isl_set_preimage_multi_pw_aff(domain
,
7026 isl_multi_pw_aff_copy(mpa
));
7027 pa_i
= isl_pw_aff_intersect_domain(pa_i
, domain
);
7028 res
= isl_pw_aff_add_disjoint(res
, pa_i
);
7031 isl_pw_aff_free(pa
);
7032 isl_multi_pw_aff_free(mpa
);
7035 isl_pw_aff_free(pa
);
7036 isl_multi_pw_aff_free(mpa
);
7040 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
7041 * with the domain of "pa". The domain of the result is the same
7044 __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_pw_aff(
7045 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_pw_aff
*pa
)
7047 isl_bool equal_params
;
7051 equal_params
= isl_space_has_equal_params(pa
->dim
, mpa
->space
);
7052 if (equal_params
< 0)
7055 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa
, pa
);
7057 pa
= isl_pw_aff_align_params(pa
, isl_multi_pw_aff_get_space(mpa
));
7058 mpa
= isl_multi_pw_aff_align_params(mpa
, isl_pw_aff_get_space(pa
));
7060 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa
, pa
);
7062 isl_pw_aff_free(pa
);
7063 isl_multi_pw_aff_free(mpa
);
7067 /* Compute the pullback of "pa" by the function represented by "mpa".
7068 * In other words, plug in "mpa" in "pa".
7069 * "pa" and "mpa" are assumed to have been aligned.
7071 * The pullback is computed by applying "pa" to "mpa".
7073 static __isl_give isl_pw_aff
*isl_pw_aff_pullback_multi_pw_aff_aligned(
7074 __isl_take isl_pw_aff
*pa
, __isl_take isl_multi_pw_aff
*mpa
)
7076 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa
, pa
);
7079 /* Compute the pullback of "pa" by the function represented by "mpa".
7080 * In other words, plug in "mpa" in "pa".
7082 * The pullback is computed by applying "pa" to "mpa".
7084 __isl_give isl_pw_aff
*isl_pw_aff_pullback_multi_pw_aff(
7085 __isl_take isl_pw_aff
*pa
, __isl_take isl_multi_pw_aff
*mpa
)
7087 return isl_multi_pw_aff_apply_pw_aff(mpa
, pa
);
7090 /* Compute the pullback of "mpa1" by the function represented by "mpa2".
7091 * In other words, plug in "mpa2" in "mpa1".
7093 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7095 * We pullback each member of "mpa1" in turn.
7097 * If "mpa1" has an explicit domain, then it is this domain
7098 * that needs to undergo a pullback instead, i.e., a preimage.
7100 static __isl_give isl_multi_pw_aff
*
7101 isl_multi_pw_aff_pullback_multi_pw_aff_aligned(
7102 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
)
7105 isl_space
*space
= NULL
;
7107 mpa1
= isl_multi_pw_aff_cow(mpa1
);
7111 space
= isl_space_join(isl_multi_pw_aff_get_space(mpa2
),
7112 isl_multi_pw_aff_get_space(mpa1
));
7114 for (i
= 0; i
< mpa1
->n
; ++i
) {
7115 mpa1
->u
.p
[i
] = isl_pw_aff_pullback_multi_pw_aff_aligned(
7116 mpa1
->u
.p
[i
], isl_multi_pw_aff_copy(mpa2
));
7121 if (isl_multi_pw_aff_has_explicit_domain(mpa1
)) {
7122 mpa1
->u
.dom
= isl_set_preimage_multi_pw_aff(mpa1
->u
.dom
,
7123 isl_multi_pw_aff_copy(mpa2
));
7127 mpa1
= isl_multi_pw_aff_reset_space(mpa1
, space
);
7129 isl_multi_pw_aff_free(mpa2
);
7132 isl_space_free(space
);
7133 isl_multi_pw_aff_free(mpa1
);
7134 isl_multi_pw_aff_free(mpa2
);
7138 /* Compute the pullback of "mpa1" by the function represented by "mpa2".
7139 * In other words, plug in "mpa2" in "mpa1".
7141 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_pullback_multi_pw_aff(
7142 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
)
7144 return isl_multi_pw_aff_align_params_multi_multi_and(mpa1
, mpa2
,
7145 &isl_multi_pw_aff_pullback_multi_pw_aff_aligned
);
7148 /* Align the parameters of "mpa1" and "mpa2", check that the ranges
7149 * of "mpa1" and "mpa2" live in the same space, construct map space
7150 * between the domain spaces of "mpa1" and "mpa2" and call "order"
7151 * with this map space as extract argument.
7153 static __isl_give isl_map
*isl_multi_pw_aff_order_map(
7154 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
,
7155 __isl_give isl_map
*(*order
)(__isl_keep isl_multi_pw_aff
*mpa1
,
7156 __isl_keep isl_multi_pw_aff
*mpa2
, __isl_take isl_space
*space
))
7159 isl_space
*space1
, *space2
;
7162 mpa1
= isl_multi_pw_aff_align_params(mpa1
,
7163 isl_multi_pw_aff_get_space(mpa2
));
7164 mpa2
= isl_multi_pw_aff_align_params(mpa2
,
7165 isl_multi_pw_aff_get_space(mpa1
));
7168 match
= isl_space_tuple_is_equal(mpa1
->space
, isl_dim_out
,
7169 mpa2
->space
, isl_dim_out
);
7173 isl_die(isl_multi_pw_aff_get_ctx(mpa1
), isl_error_invalid
,
7174 "range spaces don't match", goto error
);
7175 space1
= isl_space_domain(isl_multi_pw_aff_get_space(mpa1
));
7176 space2
= isl_space_domain(isl_multi_pw_aff_get_space(mpa2
));
7177 space1
= isl_space_map_from_domain_and_range(space1
, space2
);
7179 res
= order(mpa1
, mpa2
, space1
);
7180 isl_multi_pw_aff_free(mpa1
);
7181 isl_multi_pw_aff_free(mpa2
);
7184 isl_multi_pw_aff_free(mpa1
);
7185 isl_multi_pw_aff_free(mpa2
);
7189 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7190 * where the function values are equal. "space" is the space of the result.
7191 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7193 * "mpa1" and "mpa2" are equal when each of the pairs of elements
7194 * in the sequences are equal.
7196 static __isl_give isl_map
*isl_multi_pw_aff_eq_map_on_space(
7197 __isl_keep isl_multi_pw_aff
*mpa1
, __isl_keep isl_multi_pw_aff
*mpa2
,
7198 __isl_take isl_space
*space
)
7203 res
= isl_map_universe(space
);
7205 n
= isl_multi_pw_aff_dim(mpa1
, isl_dim_out
);
7206 for (i
= 0; i
< n
; ++i
) {
7207 isl_pw_aff
*pa1
, *pa2
;
7210 pa1
= isl_multi_pw_aff_get_pw_aff(mpa1
, i
);
7211 pa2
= isl_multi_pw_aff_get_pw_aff(mpa2
, i
);
7212 map
= isl_pw_aff_eq_map(pa1
, pa2
);
7213 res
= isl_map_intersect(res
, map
);
7219 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7220 * where the function values are equal.
7222 __isl_give isl_map
*isl_multi_pw_aff_eq_map(__isl_take isl_multi_pw_aff
*mpa1
,
7223 __isl_take isl_multi_pw_aff
*mpa2
)
7225 return isl_multi_pw_aff_order_map(mpa1
, mpa2
,
7226 &isl_multi_pw_aff_eq_map_on_space
);
7229 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7230 * where the function values of "mpa1" is lexicographically satisfies "base"
7231 * compared to that of "mpa2". "space" is the space of the result.
7232 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7234 * "mpa1" lexicographically satisfies "base" compared to "mpa2"
7235 * if its i-th element satisfies "base" when compared to
7236 * the i-th element of "mpa2" while all previous elements are
7239 static __isl_give isl_map
*isl_multi_pw_aff_lex_map_on_space(
7240 __isl_keep isl_multi_pw_aff
*mpa1
, __isl_keep isl_multi_pw_aff
*mpa2
,
7241 __isl_give isl_map
*(*base
)(__isl_take isl_pw_aff
*pa1
,
7242 __isl_take isl_pw_aff
*pa2
),
7243 __isl_take isl_space
*space
)
7246 isl_map
*res
, *rest
;
7248 res
= isl_map_empty(isl_space_copy(space
));
7249 rest
= isl_map_universe(space
);
7251 n
= isl_multi_pw_aff_dim(mpa1
, isl_dim_out
);
7252 for (i
= 0; i
< n
; ++i
) {
7253 isl_pw_aff
*pa1
, *pa2
;
7256 pa1
= isl_multi_pw_aff_get_pw_aff(mpa1
, i
);
7257 pa2
= isl_multi_pw_aff_get_pw_aff(mpa2
, i
);
7258 map
= base(pa1
, pa2
);
7259 map
= isl_map_intersect(map
, isl_map_copy(rest
));
7260 res
= isl_map_union(res
, map
);
7265 pa1
= isl_multi_pw_aff_get_pw_aff(mpa1
, i
);
7266 pa2
= isl_multi_pw_aff_get_pw_aff(mpa2
, i
);
7267 map
= isl_pw_aff_eq_map(pa1
, pa2
);
7268 rest
= isl_map_intersect(rest
, map
);
7275 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7276 * where the function value of "mpa1" is lexicographically less than that
7277 * of "mpa2". "space" is the space of the result.
7278 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7280 * "mpa1" is less than "mpa2" if its i-th element is smaller
7281 * than the i-th element of "mpa2" while all previous elements are
7284 __isl_give isl_map
*isl_multi_pw_aff_lex_lt_map_on_space(
7285 __isl_keep isl_multi_pw_aff
*mpa1
, __isl_keep isl_multi_pw_aff
*mpa2
,
7286 __isl_take isl_space
*space
)
7288 return isl_multi_pw_aff_lex_map_on_space(mpa1
, mpa2
,
7289 &isl_pw_aff_lt_map
, space
);
7292 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7293 * where the function value of "mpa1" is lexicographically less than that
7296 __isl_give isl_map
*isl_multi_pw_aff_lex_lt_map(
7297 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
)
7299 return isl_multi_pw_aff_order_map(mpa1
, mpa2
,
7300 &isl_multi_pw_aff_lex_lt_map_on_space
);
7303 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7304 * where the function value of "mpa1" is lexicographically greater than that
7305 * of "mpa2". "space" is the space of the result.
7306 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7308 * "mpa1" is greater than "mpa2" if its i-th element is greater
7309 * than the i-th element of "mpa2" while all previous elements are
7312 __isl_give isl_map
*isl_multi_pw_aff_lex_gt_map_on_space(
7313 __isl_keep isl_multi_pw_aff
*mpa1
, __isl_keep isl_multi_pw_aff
*mpa2
,
7314 __isl_take isl_space
*space
)
7316 return isl_multi_pw_aff_lex_map_on_space(mpa1
, mpa2
,
7317 &isl_pw_aff_gt_map
, space
);
7320 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7321 * where the function value of "mpa1" is lexicographically greater than that
7324 __isl_give isl_map
*isl_multi_pw_aff_lex_gt_map(
7325 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
)
7327 return isl_multi_pw_aff_order_map(mpa1
, mpa2
,
7328 &isl_multi_pw_aff_lex_gt_map_on_space
);
7331 /* Compare two isl_affs.
7333 * Return -1 if "aff1" is "smaller" than "aff2", 1 if "aff1" is "greater"
7334 * than "aff2" and 0 if they are equal.
7336 * The order is fairly arbitrary. We do consider expressions that only involve
7337 * earlier dimensions as "smaller".
7339 int isl_aff_plain_cmp(__isl_keep isl_aff
*aff1
, __isl_keep isl_aff
*aff2
)
7352 cmp
= isl_local_space_cmp(aff1
->ls
, aff2
->ls
);
7356 last1
= isl_seq_last_non_zero(aff1
->v
->el
+ 1, aff1
->v
->size
- 1);
7357 last2
= isl_seq_last_non_zero(aff2
->v
->el
+ 1, aff1
->v
->size
- 1);
7359 return last1
- last2
;
7361 return isl_seq_cmp(aff1
->v
->el
, aff2
->v
->el
, aff1
->v
->size
);
7364 /* Compare two isl_pw_affs.
7366 * Return -1 if "pa1" is "smaller" than "pa2", 1 if "pa1" is "greater"
7367 * than "pa2" and 0 if they are equal.
7369 * The order is fairly arbitrary. We do consider expressions that only involve
7370 * earlier dimensions as "smaller".
7372 int isl_pw_aff_plain_cmp(__isl_keep isl_pw_aff
*pa1
,
7373 __isl_keep isl_pw_aff
*pa2
)
7386 cmp
= isl_space_cmp(pa1
->dim
, pa2
->dim
);
7390 if (pa1
->n
!= pa2
->n
)
7391 return pa1
->n
- pa2
->n
;
7393 for (i
= 0; i
< pa1
->n
; ++i
) {
7394 cmp
= isl_set_plain_cmp(pa1
->p
[i
].set
, pa2
->p
[i
].set
);
7397 cmp
= isl_aff_plain_cmp(pa1
->p
[i
].aff
, pa2
->p
[i
].aff
);
7405 /* Return a piecewise affine expression that is equal to "v" on "domain".
7407 __isl_give isl_pw_aff
*isl_pw_aff_val_on_domain(__isl_take isl_set
*domain
,
7408 __isl_take isl_val
*v
)
7411 isl_local_space
*ls
;
7414 space
= isl_set_get_space(domain
);
7415 ls
= isl_local_space_from_space(space
);
7416 aff
= isl_aff_val_on_domain(ls
, v
);
7418 return isl_pw_aff_alloc(domain
, aff
);
7421 /* Return a multi affine expression that is equal to "mv" on domain
7424 __isl_give isl_multi_aff
*isl_multi_aff_multi_val_on_space(
7425 __isl_take isl_space
*space
, __isl_take isl_multi_val
*mv
)
7429 isl_local_space
*ls
;
7435 n
= isl_multi_val_dim(mv
, isl_dim_set
);
7436 space2
= isl_multi_val_get_space(mv
);
7437 space2
= isl_space_align_params(space2
, isl_space_copy(space
));
7438 space
= isl_space_align_params(space
, isl_space_copy(space2
));
7439 space
= isl_space_map_from_domain_and_range(space
, space2
);
7440 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
7441 ls
= isl_local_space_from_space(isl_space_domain(space
));
7442 for (i
= 0; i
< n
; ++i
) {
7446 v
= isl_multi_val_get_val(mv
, i
);
7447 aff
= isl_aff_val_on_domain(isl_local_space_copy(ls
), v
);
7448 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
7450 isl_local_space_free(ls
);
7452 isl_multi_val_free(mv
);
7455 isl_space_free(space
);
7456 isl_multi_val_free(mv
);
7460 /* Return a piecewise multi-affine expression
7461 * that is equal to "mv" on "domain".
7463 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_multi_val_on_domain(
7464 __isl_take isl_set
*domain
, __isl_take isl_multi_val
*mv
)
7469 space
= isl_set_get_space(domain
);
7470 ma
= isl_multi_aff_multi_val_on_space(space
, mv
);
7472 return isl_pw_multi_aff_alloc(domain
, ma
);
7475 /* Internal data structure for isl_union_pw_multi_aff_multi_val_on_domain.
7476 * mv is the value that should be attained on each domain set
7477 * res collects the results
7479 struct isl_union_pw_multi_aff_multi_val_on_domain_data
{
7481 isl_union_pw_multi_aff
*res
;
7484 /* Create an isl_pw_multi_aff equal to data->mv on "domain"
7485 * and add it to data->res.
7487 static isl_stat
pw_multi_aff_multi_val_on_domain(__isl_take isl_set
*domain
,
7490 struct isl_union_pw_multi_aff_multi_val_on_domain_data
*data
= user
;
7491 isl_pw_multi_aff
*pma
;
7494 mv
= isl_multi_val_copy(data
->mv
);
7495 pma
= isl_pw_multi_aff_multi_val_on_domain(domain
, mv
);
7496 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma
);
7498 return data
->res
? isl_stat_ok
: isl_stat_error
;
7501 /* Return a union piecewise multi-affine expression
7502 * that is equal to "mv" on "domain".
7504 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_multi_val_on_domain(
7505 __isl_take isl_union_set
*domain
, __isl_take isl_multi_val
*mv
)
7507 struct isl_union_pw_multi_aff_multi_val_on_domain_data data
;
7510 space
= isl_union_set_get_space(domain
);
7511 data
.res
= isl_union_pw_multi_aff_empty(space
);
7513 if (isl_union_set_foreach_set(domain
,
7514 &pw_multi_aff_multi_val_on_domain
, &data
) < 0)
7515 data
.res
= isl_union_pw_multi_aff_free(data
.res
);
7516 isl_union_set_free(domain
);
7517 isl_multi_val_free(mv
);
7521 /* Compute the pullback of data->pma by the function represented by "pma2",
7522 * provided the spaces match, and add the results to data->res.
7524 static isl_stat
pullback_entry(__isl_take isl_pw_multi_aff
*pma2
, void *user
)
7526 struct isl_union_pw_multi_aff_bin_data
*data
= user
;
7528 if (!isl_space_tuple_is_equal(data
->pma
->dim
, isl_dim_in
,
7529 pma2
->dim
, isl_dim_out
)) {
7530 isl_pw_multi_aff_free(pma2
);
7534 pma2
= isl_pw_multi_aff_pullback_pw_multi_aff(
7535 isl_pw_multi_aff_copy(data
->pma
), pma2
);
7537 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma2
);
7539 return isl_stat_error
;
7544 /* Compute the pullback of "upma1" by the function represented by "upma2".
7546 __isl_give isl_union_pw_multi_aff
*
7547 isl_union_pw_multi_aff_pullback_union_pw_multi_aff(
7548 __isl_take isl_union_pw_multi_aff
*upma1
,
7549 __isl_take isl_union_pw_multi_aff
*upma2
)
7551 return bin_op(upma1
, upma2
, &pullback_entry
);
7554 /* Check that the domain space of "upa" matches "space".
7556 * This function is called from isl_multi_union_pw_aff_set_union_pw_aff and
7557 * can in principle never fail since the space "space" is that
7558 * of the isl_multi_union_pw_aff and is a set space such that
7559 * there is no domain space to match.
7561 * We check the parameters and double-check that "space" is
7562 * indeed that of a set.
7564 static isl_stat
isl_union_pw_aff_check_match_domain_space(
7565 __isl_keep isl_union_pw_aff
*upa
, __isl_keep isl_space
*space
)
7567 isl_space
*upa_space
;
7571 return isl_stat_error
;
7573 match
= isl_space_is_set(space
);
7575 return isl_stat_error
;
7577 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
7578 "expecting set space", return -1);
7580 upa_space
= isl_union_pw_aff_get_space(upa
);
7581 match
= isl_space_has_equal_params(space
, upa_space
);
7585 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
7586 "parameters don't match", goto error
);
7588 isl_space_free(upa_space
);
7591 isl_space_free(upa_space
);
7592 return isl_stat_error
;
7595 /* Do the parameters of "upa" match those of "space"?
7597 static isl_bool
isl_union_pw_aff_matching_params(
7598 __isl_keep isl_union_pw_aff
*upa
, __isl_keep isl_space
*space
)
7600 isl_space
*upa_space
;
7604 return isl_bool_error
;
7606 upa_space
= isl_union_pw_aff_get_space(upa
);
7608 match
= isl_space_has_equal_params(space
, upa_space
);
7610 isl_space_free(upa_space
);
7614 /* Internal data structure for isl_union_pw_aff_reset_domain_space.
7615 * space represents the new parameters.
7616 * res collects the results.
7618 struct isl_union_pw_aff_reset_params_data
{
7620 isl_union_pw_aff
*res
;
7623 /* Replace the parameters of "pa" by data->space and
7624 * add the result to data->res.
7626 static isl_stat
reset_params(__isl_take isl_pw_aff
*pa
, void *user
)
7628 struct isl_union_pw_aff_reset_params_data
*data
= user
;
7631 space
= isl_pw_aff_get_space(pa
);
7632 space
= isl_space_replace_params(space
, data
->space
);
7633 pa
= isl_pw_aff_reset_space(pa
, space
);
7634 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
7636 return data
->res
? isl_stat_ok
: isl_stat_error
;
7639 /* Replace the domain space of "upa" by "space".
7640 * Since a union expression does not have a (single) domain space,
7641 * "space" is necessarily a parameter space.
7643 * Since the order and the names of the parameters determine
7644 * the hash value, we need to create a new hash table.
7646 static __isl_give isl_union_pw_aff
*isl_union_pw_aff_reset_domain_space(
7647 __isl_take isl_union_pw_aff
*upa
, __isl_take isl_space
*space
)
7649 struct isl_union_pw_aff_reset_params_data data
= { space
};
7652 match
= isl_union_pw_aff_matching_params(upa
, space
);
7654 upa
= isl_union_pw_aff_free(upa
);
7656 isl_space_free(space
);
7660 data
.res
= isl_union_pw_aff_empty(isl_space_copy(space
));
7661 if (isl_union_pw_aff_foreach_pw_aff(upa
, &reset_params
, &data
) < 0)
7662 data
.res
= isl_union_pw_aff_free(data
.res
);
7664 isl_union_pw_aff_free(upa
);
7665 isl_space_free(space
);
7669 /* Return the floor of "pa".
7671 static __isl_give isl_pw_aff
*floor_entry(__isl_take isl_pw_aff
*pa
, void *user
)
7673 return isl_pw_aff_floor(pa
);
7676 /* Given f, return floor(f).
7678 __isl_give isl_union_pw_aff
*isl_union_pw_aff_floor(
7679 __isl_take isl_union_pw_aff
*upa
)
7681 return isl_union_pw_aff_transform_inplace(upa
, &floor_entry
, NULL
);
7686 * upa mod m = upa - m * floor(upa/m)
7688 * with m an integer value.
7690 __isl_give isl_union_pw_aff
*isl_union_pw_aff_mod_val(
7691 __isl_take isl_union_pw_aff
*upa
, __isl_take isl_val
*m
)
7693 isl_union_pw_aff
*res
;
7698 if (!isl_val_is_int(m
))
7699 isl_die(isl_val_get_ctx(m
), isl_error_invalid
,
7700 "expecting integer modulo", goto error
);
7701 if (!isl_val_is_pos(m
))
7702 isl_die(isl_val_get_ctx(m
), isl_error_invalid
,
7703 "expecting positive modulo", goto error
);
7705 res
= isl_union_pw_aff_copy(upa
);
7706 upa
= isl_union_pw_aff_scale_down_val(upa
, isl_val_copy(m
));
7707 upa
= isl_union_pw_aff_floor(upa
);
7708 upa
= isl_union_pw_aff_scale_val(upa
, m
);
7709 res
= isl_union_pw_aff_sub(res
, upa
);
7714 isl_union_pw_aff_free(upa
);
7718 /* Internal data structure for isl_union_pw_multi_aff_get_union_pw_aff.
7719 * pos is the output position that needs to be extracted.
7720 * res collects the results.
7722 struct isl_union_pw_multi_aff_get_union_pw_aff_data
{
7724 isl_union_pw_aff
*res
;
7727 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma"
7728 * (assuming it has such a dimension) and add it to data->res.
7730 static isl_stat
get_union_pw_aff(__isl_take isl_pw_multi_aff
*pma
, void *user
)
7732 struct isl_union_pw_multi_aff_get_union_pw_aff_data
*data
= user
;
7737 return isl_stat_error
;
7739 n_out
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
7740 if (data
->pos
>= n_out
) {
7741 isl_pw_multi_aff_free(pma
);
7745 pa
= isl_pw_multi_aff_get_pw_aff(pma
, data
->pos
);
7746 isl_pw_multi_aff_free(pma
);
7748 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
7750 return data
->res
? isl_stat_ok
: isl_stat_error
;
7753 /* Extract an isl_union_pw_aff corresponding to
7754 * output dimension "pos" of "upma".
7756 __isl_give isl_union_pw_aff
*isl_union_pw_multi_aff_get_union_pw_aff(
7757 __isl_keep isl_union_pw_multi_aff
*upma
, int pos
)
7759 struct isl_union_pw_multi_aff_get_union_pw_aff_data data
;
7766 isl_die(isl_union_pw_multi_aff_get_ctx(upma
), isl_error_invalid
,
7767 "cannot extract at negative position", return NULL
);
7769 space
= isl_union_pw_multi_aff_get_space(upma
);
7770 data
.res
= isl_union_pw_aff_empty(space
);
7772 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma
,
7773 &get_union_pw_aff
, &data
) < 0)
7774 data
.res
= isl_union_pw_aff_free(data
.res
);
7779 /* Return a union piecewise affine expression
7780 * that is equal to "aff" on "domain".
7782 __isl_give isl_union_pw_aff
*isl_union_pw_aff_aff_on_domain(
7783 __isl_take isl_union_set
*domain
, __isl_take isl_aff
*aff
)
7787 pa
= isl_pw_aff_from_aff(aff
);
7788 return isl_union_pw_aff_pw_aff_on_domain(domain
, pa
);
7791 /* Return a union piecewise affine expression
7792 * that is equal to the parameter identified by "id" on "domain".
7794 * Make sure the parameter appears in the space passed to
7795 * isl_aff_param_on_domain_space_id.
7797 __isl_give isl_union_pw_aff
*isl_union_pw_aff_param_on_domain_id(
7798 __isl_take isl_union_set
*domain
, __isl_take isl_id
*id
)
7803 space
= isl_union_set_get_space(domain
);
7804 space
= isl_space_add_param_id(space
, isl_id_copy(id
));
7805 aff
= isl_aff_param_on_domain_space_id(space
, id
);
7806 return isl_union_pw_aff_aff_on_domain(domain
, aff
);
7809 /* Internal data structure for isl_union_pw_aff_pw_aff_on_domain.
7810 * "pa" is the piecewise symbolic value that the resulting isl_union_pw_aff
7812 * "res" collects the results.
7814 struct isl_union_pw_aff_pw_aff_on_domain_data
{
7816 isl_union_pw_aff
*res
;
7819 /* Construct a piecewise affine expression that is equal to data->pa
7820 * on "domain" and add the result to data->res.
7822 static isl_stat
pw_aff_on_domain(__isl_take isl_set
*domain
, void *user
)
7824 struct isl_union_pw_aff_pw_aff_on_domain_data
*data
= user
;
7828 pa
= isl_pw_aff_copy(data
->pa
);
7829 dim
= isl_set_dim(domain
, isl_dim_set
);
7830 pa
= isl_pw_aff_from_range(pa
);
7831 pa
= isl_pw_aff_add_dims(pa
, isl_dim_in
, dim
);
7832 pa
= isl_pw_aff_reset_domain_space(pa
, isl_set_get_space(domain
));
7833 pa
= isl_pw_aff_intersect_domain(pa
, domain
);
7834 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
7836 return data
->res
? isl_stat_ok
: isl_stat_error
;
7839 /* Return a union piecewise affine expression
7840 * that is equal to "pa" on "domain", assuming "domain" and "pa"
7841 * have been aligned.
7843 * Construct an isl_pw_aff on each of the sets in "domain" and
7844 * collect the results.
7846 static __isl_give isl_union_pw_aff
*isl_union_pw_aff_pw_aff_on_domain_aligned(
7847 __isl_take isl_union_set
*domain
, __isl_take isl_pw_aff
*pa
)
7849 struct isl_union_pw_aff_pw_aff_on_domain_data data
;
7852 space
= isl_union_set_get_space(domain
);
7853 data
.res
= isl_union_pw_aff_empty(space
);
7855 if (isl_union_set_foreach_set(domain
, &pw_aff_on_domain
, &data
) < 0)
7856 data
.res
= isl_union_pw_aff_free(data
.res
);
7857 isl_union_set_free(domain
);
7858 isl_pw_aff_free(pa
);
7862 /* Return a union piecewise affine expression
7863 * that is equal to "pa" on "domain".
7865 * Check that "pa" is a parametric expression,
7866 * align the parameters if needed and call
7867 * isl_union_pw_aff_pw_aff_on_domain_aligned.
7869 __isl_give isl_union_pw_aff
*isl_union_pw_aff_pw_aff_on_domain(
7870 __isl_take isl_union_set
*domain
, __isl_take isl_pw_aff
*pa
)
7873 isl_bool equal_params
;
7874 isl_space
*domain_space
, *pa_space
;
7876 pa_space
= isl_pw_aff_peek_space(pa
);
7877 is_set
= isl_space_is_set(pa_space
);
7881 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
7882 "expecting parametric expression", goto error
);
7884 domain_space
= isl_union_set_get_space(domain
);
7885 pa_space
= isl_pw_aff_get_space(pa
);
7886 equal_params
= isl_space_has_equal_params(domain_space
, pa_space
);
7887 if (equal_params
>= 0 && !equal_params
) {
7890 space
= isl_space_align_params(domain_space
, pa_space
);
7891 pa
= isl_pw_aff_align_params(pa
, isl_space_copy(space
));
7892 domain
= isl_union_set_align_params(domain
, space
);
7894 isl_space_free(domain_space
);
7895 isl_space_free(pa_space
);
7898 if (equal_params
< 0)
7900 return isl_union_pw_aff_pw_aff_on_domain_aligned(domain
, pa
);
7902 isl_union_set_free(domain
);
7903 isl_pw_aff_free(pa
);
7907 /* Internal data structure for isl_union_pw_aff_val_on_domain.
7908 * "v" is the value that the resulting isl_union_pw_aff needs to attain.
7909 * "res" collects the results.
7911 struct isl_union_pw_aff_val_on_domain_data
{
7913 isl_union_pw_aff
*res
;
7916 /* Construct a piecewise affine expression that is equal to data->v
7917 * on "domain" and add the result to data->res.
7919 static isl_stat
pw_aff_val_on_domain(__isl_take isl_set
*domain
, void *user
)
7921 struct isl_union_pw_aff_val_on_domain_data
*data
= user
;
7925 v
= isl_val_copy(data
->v
);
7926 pa
= isl_pw_aff_val_on_domain(domain
, v
);
7927 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
7929 return data
->res
? isl_stat_ok
: isl_stat_error
;
7932 /* Return a union piecewise affine expression
7933 * that is equal to "v" on "domain".
7935 * Construct an isl_pw_aff on each of the sets in "domain" and
7936 * collect the results.
7938 __isl_give isl_union_pw_aff
*isl_union_pw_aff_val_on_domain(
7939 __isl_take isl_union_set
*domain
, __isl_take isl_val
*v
)
7941 struct isl_union_pw_aff_val_on_domain_data data
;
7944 space
= isl_union_set_get_space(domain
);
7945 data
.res
= isl_union_pw_aff_empty(space
);
7947 if (isl_union_set_foreach_set(domain
, &pw_aff_val_on_domain
, &data
) < 0)
7948 data
.res
= isl_union_pw_aff_free(data
.res
);
7949 isl_union_set_free(domain
);
7954 /* Construct a piecewise multi affine expression
7955 * that is equal to "pa" and add it to upma.
7957 static isl_stat
pw_multi_aff_from_pw_aff_entry(__isl_take isl_pw_aff
*pa
,
7960 isl_union_pw_multi_aff
**upma
= user
;
7961 isl_pw_multi_aff
*pma
;
7963 pma
= isl_pw_multi_aff_from_pw_aff(pa
);
7964 *upma
= isl_union_pw_multi_aff_add_pw_multi_aff(*upma
, pma
);
7966 return *upma
? isl_stat_ok
: isl_stat_error
;
7969 /* Construct and return a union piecewise multi affine expression
7970 * that is equal to the given union piecewise affine expression.
7972 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_union_pw_aff(
7973 __isl_take isl_union_pw_aff
*upa
)
7976 isl_union_pw_multi_aff
*upma
;
7981 space
= isl_union_pw_aff_get_space(upa
);
7982 upma
= isl_union_pw_multi_aff_empty(space
);
7984 if (isl_union_pw_aff_foreach_pw_aff(upa
,
7985 &pw_multi_aff_from_pw_aff_entry
, &upma
) < 0)
7986 upma
= isl_union_pw_multi_aff_free(upma
);
7988 isl_union_pw_aff_free(upa
);
7992 /* Compute the set of elements in the domain of "pa" where it is zero and
7993 * add this set to "uset".
7995 static isl_stat
zero_union_set(__isl_take isl_pw_aff
*pa
, void *user
)
7997 isl_union_set
**uset
= (isl_union_set
**)user
;
7999 *uset
= isl_union_set_add_set(*uset
, isl_pw_aff_zero_set(pa
));
8001 return *uset
? isl_stat_ok
: isl_stat_error
;
8004 /* Return a union set containing those elements in the domain
8005 * of "upa" where it is zero.
8007 __isl_give isl_union_set
*isl_union_pw_aff_zero_union_set(
8008 __isl_take isl_union_pw_aff
*upa
)
8010 isl_union_set
*zero
;
8012 zero
= isl_union_set_empty(isl_union_pw_aff_get_space(upa
));
8013 if (isl_union_pw_aff_foreach_pw_aff(upa
, &zero_union_set
, &zero
) < 0)
8014 zero
= isl_union_set_free(zero
);
8016 isl_union_pw_aff_free(upa
);
8020 /* Convert "pa" to an isl_map and add it to *umap.
8022 static isl_stat
map_from_pw_aff_entry(__isl_take isl_pw_aff
*pa
, void *user
)
8024 isl_union_map
**umap
= user
;
8027 map
= isl_map_from_pw_aff(pa
);
8028 *umap
= isl_union_map_add_map(*umap
, map
);
8030 return *umap
? isl_stat_ok
: isl_stat_error
;
8033 /* Construct a union map mapping the domain of the union
8034 * piecewise affine expression to its range, with the single output dimension
8035 * equated to the corresponding affine expressions on their cells.
8037 __isl_give isl_union_map
*isl_union_map_from_union_pw_aff(
8038 __isl_take isl_union_pw_aff
*upa
)
8041 isl_union_map
*umap
;
8046 space
= isl_union_pw_aff_get_space(upa
);
8047 umap
= isl_union_map_empty(space
);
8049 if (isl_union_pw_aff_foreach_pw_aff(upa
, &map_from_pw_aff_entry
,
8051 umap
= isl_union_map_free(umap
);
8053 isl_union_pw_aff_free(upa
);
8057 /* Internal data structure for isl_union_pw_aff_pullback_union_pw_multi_aff.
8058 * upma is the function that is plugged in.
8059 * pa is the current part of the function in which upma is plugged in.
8060 * res collects the results.
8062 struct isl_union_pw_aff_pullback_upma_data
{
8063 isl_union_pw_multi_aff
*upma
;
8065 isl_union_pw_aff
*res
;
8068 /* Check if "pma" can be plugged into data->pa.
8069 * If so, perform the pullback and add the result to data->res.
8071 static isl_stat
pa_pb_pma(__isl_take isl_pw_multi_aff
*pma
, void *user
)
8073 struct isl_union_pw_aff_pullback_upma_data
*data
= user
;
8076 if (!isl_space_tuple_is_equal(data
->pa
->dim
, isl_dim_in
,
8077 pma
->dim
, isl_dim_out
)) {
8078 isl_pw_multi_aff_free(pma
);
8082 pa
= isl_pw_aff_copy(data
->pa
);
8083 pa
= isl_pw_aff_pullback_pw_multi_aff(pa
, pma
);
8085 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
8087 return data
->res
? isl_stat_ok
: isl_stat_error
;
8090 /* Check if any of the elements of data->upma can be plugged into pa,
8091 * add if so add the result to data->res.
8093 static isl_stat
upa_pb_upma(__isl_take isl_pw_aff
*pa
, void *user
)
8095 struct isl_union_pw_aff_pullback_upma_data
*data
= user
;
8099 r
= isl_union_pw_multi_aff_foreach_pw_multi_aff(data
->upma
,
8101 isl_pw_aff_free(pa
);
8106 /* Compute the pullback of "upa" by the function represented by "upma".
8107 * In other words, plug in "upma" in "upa". The result contains
8108 * expressions defined over the domain space of "upma".
8110 * Run over all pairs of elements in "upa" and "upma", perform
8111 * the pullback when appropriate and collect the results.
8112 * If the hash value were based on the domain space rather than
8113 * the function space, then we could run through all elements
8114 * of "upma" and directly pick out the corresponding element of "upa".
8116 __isl_give isl_union_pw_aff
*isl_union_pw_aff_pullback_union_pw_multi_aff(
8117 __isl_take isl_union_pw_aff
*upa
,
8118 __isl_take isl_union_pw_multi_aff
*upma
)
8120 struct isl_union_pw_aff_pullback_upma_data data
= { NULL
, NULL
};
8123 space
= isl_union_pw_multi_aff_get_space(upma
);
8124 upa
= isl_union_pw_aff_align_params(upa
, space
);
8125 space
= isl_union_pw_aff_get_space(upa
);
8126 upma
= isl_union_pw_multi_aff_align_params(upma
, space
);
8132 data
.res
= isl_union_pw_aff_alloc_same_size(upa
);
8133 if (isl_union_pw_aff_foreach_pw_aff(upa
, &upa_pb_upma
, &data
) < 0)
8134 data
.res
= isl_union_pw_aff_free(data
.res
);
8136 isl_union_pw_aff_free(upa
);
8137 isl_union_pw_multi_aff_free(upma
);
8140 isl_union_pw_aff_free(upa
);
8141 isl_union_pw_multi_aff_free(upma
);
8146 #define BASE union_pw_aff
8148 #define DOMBASE union_set
8150 #define NO_MOVE_DIMS
8158 #include <isl_multi_explicit_domain.c>
8159 #include <isl_multi_union_pw_aff_explicit_domain.c>
8160 #include <isl_multi_templ.c>
8161 #include <isl_multi_apply_set.c>
8162 #include <isl_multi_apply_union_set.c>
8163 #include <isl_multi_coalesce.c>
8164 #include <isl_multi_floor.c>
8165 #include <isl_multi_gist.c>
8166 #include <isl_multi_align_set.c>
8167 #include <isl_multi_align_union_set.c>
8168 #include <isl_multi_intersect.c>
8170 /* Does "mupa" have a non-trivial explicit domain?
8172 * The explicit domain, if present, is trivial if it represents
8173 * an (obviously) universe parameter set.
8175 isl_bool
isl_multi_union_pw_aff_has_non_trivial_domain(
8176 __isl_keep isl_multi_union_pw_aff
*mupa
)
8178 isl_bool is_params
, trivial
;
8182 return isl_bool_error
;
8183 if (!isl_multi_union_pw_aff_has_explicit_domain(mupa
))
8184 return isl_bool_false
;
8185 is_params
= isl_union_set_is_params(mupa
->u
.dom
);
8186 if (is_params
< 0 || !is_params
)
8187 return isl_bool_not(is_params
);
8188 set
= isl_set_from_union_set(isl_union_set_copy(mupa
->u
.dom
));
8189 trivial
= isl_set_plain_is_universe(set
);
8191 return isl_bool_not(trivial
);
8194 /* Construct a multiple union piecewise affine expression
8195 * in the given space with value zero in each of the output dimensions.
8197 * Since there is no canonical zero value for
8198 * a union piecewise affine expression, we can only construct
8199 * a zero-dimensional "zero" value.
8201 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_zero(
8202 __isl_take isl_space
*space
)
8209 params
= isl_space_is_params(space
);
8213 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
8214 "expecting proper set space", goto error
);
8215 if (!isl_space_is_set(space
))
8216 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
8217 "expecting set space", goto error
);
8218 if (isl_space_dim(space
, isl_dim_out
) != 0)
8219 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
8220 "expecting 0D space", goto error
);
8222 return isl_multi_union_pw_aff_alloc(space
);
8224 isl_space_free(space
);
8228 /* Compute the sum of "mupa1" and "mupa2" on the union of their domains,
8229 * with the actual sum on the shared domain and
8230 * the defined expression on the symmetric difference of the domains.
8232 * We simply iterate over the elements in both arguments and
8233 * call isl_union_pw_aff_union_add on each of them, if there is
8234 * at least one element.
8236 * Otherwise, the two expressions have an explicit domain and
8237 * the union of these explicit domains is computed.
8238 * This assumes that the explicit domains are either both in terms
8239 * of specific domains elements or both in terms of parameters.
8240 * However, if one of the expressions does not have any constraints
8241 * on its explicit domain, then this is allowed as well and the result
8242 * is the expression with no constraints on its explicit domain.
8244 static __isl_give isl_multi_union_pw_aff
*
8245 isl_multi_union_pw_aff_union_add_aligned(
8246 __isl_take isl_multi_union_pw_aff
*mupa1
,
8247 __isl_take isl_multi_union_pw_aff
*mupa2
)
8249 isl_bool has_domain
, is_params1
, is_params2
;
8251 if (isl_multi_union_pw_aff_check_equal_space(mupa1
, mupa2
) < 0)
8254 return isl_multi_union_pw_aff_bin_op(mupa1
, mupa2
,
8255 &isl_union_pw_aff_union_add
);
8256 if (isl_multi_union_pw_aff_check_has_explicit_domain(mupa1
) < 0 ||
8257 isl_multi_union_pw_aff_check_has_explicit_domain(mupa2
) < 0)
8260 has_domain
= isl_multi_union_pw_aff_has_non_trivial_domain(mupa1
);
8264 isl_multi_union_pw_aff_free(mupa2
);
8267 has_domain
= isl_multi_union_pw_aff_has_non_trivial_domain(mupa2
);
8271 isl_multi_union_pw_aff_free(mupa1
);
8275 is_params1
= isl_union_set_is_params(mupa1
->u
.dom
);
8276 is_params2
= isl_union_set_is_params(mupa2
->u
.dom
);
8277 if (is_params1
< 0 || is_params2
< 0)
8279 if (is_params1
!= is_params2
)
8280 isl_die(isl_multi_union_pw_aff_get_ctx(mupa1
),
8282 "cannot compute union of concrete domain and "
8283 "parameter constraints", goto error
);
8284 mupa1
= isl_multi_union_pw_aff_cow(mupa1
);
8287 mupa1
->u
.dom
= isl_union_set_union(mupa1
->u
.dom
,
8288 isl_union_set_copy(mupa2
->u
.dom
));
8291 isl_multi_union_pw_aff_free(mupa2
);
8294 isl_multi_union_pw_aff_free(mupa1
);
8295 isl_multi_union_pw_aff_free(mupa2
);
8299 /* Compute the sum of "mupa1" and "mupa2" on the union of their domains,
8300 * with the actual sum on the shared domain and
8301 * the defined expression on the symmetric difference of the domains.
8303 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_union_add(
8304 __isl_take isl_multi_union_pw_aff
*mupa1
,
8305 __isl_take isl_multi_union_pw_aff
*mupa2
)
8307 return isl_multi_union_pw_aff_align_params_multi_multi_and(mupa1
, mupa2
,
8308 &isl_multi_union_pw_aff_union_add_aligned
);
8311 /* Construct and return a multi union piecewise affine expression
8312 * that is equal to the given multi affine expression.
8314 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_from_multi_aff(
8315 __isl_take isl_multi_aff
*ma
)
8317 isl_multi_pw_aff
*mpa
;
8319 mpa
= isl_multi_pw_aff_from_multi_aff(ma
);
8320 return isl_multi_union_pw_aff_from_multi_pw_aff(mpa
);
8323 /* Construct and return a multi union piecewise affine expression
8324 * that is equal to the given multi piecewise affine expression.
8326 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_from_multi_pw_aff(
8327 __isl_take isl_multi_pw_aff
*mpa
)
8331 isl_multi_union_pw_aff
*mupa
;
8336 space
= isl_multi_pw_aff_get_space(mpa
);
8337 space
= isl_space_range(space
);
8338 mupa
= isl_multi_union_pw_aff_alloc(space
);
8340 n
= isl_multi_pw_aff_dim(mpa
, isl_dim_out
);
8341 for (i
= 0; i
< n
; ++i
) {
8343 isl_union_pw_aff
*upa
;
8345 pa
= isl_multi_pw_aff_get_pw_aff(mpa
, i
);
8346 upa
= isl_union_pw_aff_from_pw_aff(pa
);
8347 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
8350 isl_multi_pw_aff_free(mpa
);
8355 /* Extract the range space of "pma" and assign it to *space.
8356 * If *space has already been set (through a previous call to this function),
8357 * then check that the range space is the same.
8359 static isl_stat
extract_space(__isl_take isl_pw_multi_aff
*pma
, void *user
)
8361 isl_space
**space
= user
;
8362 isl_space
*pma_space
;
8365 pma_space
= isl_space_range(isl_pw_multi_aff_get_space(pma
));
8366 isl_pw_multi_aff_free(pma
);
8369 return isl_stat_error
;
8375 equal
= isl_space_is_equal(pma_space
, *space
);
8376 isl_space_free(pma_space
);
8379 return isl_stat_error
;
8381 isl_die(isl_space_get_ctx(*space
), isl_error_invalid
,
8382 "range spaces not the same", return isl_stat_error
);
8386 /* Construct and return a multi union piecewise affine expression
8387 * that is equal to the given union piecewise multi affine expression.
8389 * In order to be able to perform the conversion, the input
8390 * needs to be non-empty and may only involve a single range space.
8392 * If the resulting multi union piecewise affine expression has
8393 * an explicit domain, then assign it the domain of the input.
8394 * In other cases, the domain is stored in the individual elements.
8396 __isl_give isl_multi_union_pw_aff
*
8397 isl_multi_union_pw_aff_from_union_pw_multi_aff(
8398 __isl_take isl_union_pw_multi_aff
*upma
)
8400 isl_space
*space
= NULL
;
8401 isl_multi_union_pw_aff
*mupa
;
8406 if (isl_union_pw_multi_aff_n_pw_multi_aff(upma
) == 0)
8407 isl_die(isl_union_pw_multi_aff_get_ctx(upma
), isl_error_invalid
,
8408 "cannot extract range space from empty input",
8410 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma
, &extract_space
,
8417 n
= isl_space_dim(space
, isl_dim_set
);
8418 mupa
= isl_multi_union_pw_aff_alloc(space
);
8420 for (i
= 0; i
< n
; ++i
) {
8421 isl_union_pw_aff
*upa
;
8423 upa
= isl_union_pw_multi_aff_get_union_pw_aff(upma
, i
);
8424 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
8426 if (isl_multi_union_pw_aff_has_explicit_domain(mupa
)) {
8428 isl_union_pw_multi_aff
*copy
;
8430 copy
= isl_union_pw_multi_aff_copy(upma
);
8431 dom
= isl_union_pw_multi_aff_domain(copy
);
8432 mupa
= isl_multi_union_pw_aff_intersect_domain(mupa
, dom
);
8435 isl_union_pw_multi_aff_free(upma
);
8438 isl_space_free(space
);
8439 isl_union_pw_multi_aff_free(upma
);
8443 /* Try and create an isl_multi_union_pw_aff that is equivalent
8444 * to the given isl_union_map.
8445 * The isl_union_map is required to be single-valued in each space.
8446 * Moreover, it cannot be empty and all range spaces need to be the same.
8447 * Otherwise, an error is produced.
8449 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_from_union_map(
8450 __isl_take isl_union_map
*umap
)
8452 isl_union_pw_multi_aff
*upma
;
8454 upma
= isl_union_pw_multi_aff_from_union_map(umap
);
8455 return isl_multi_union_pw_aff_from_union_pw_multi_aff(upma
);
8458 /* Return a multiple union piecewise affine expression
8459 * that is equal to "mv" on "domain", assuming "domain" and "mv"
8460 * have been aligned.
8462 static __isl_give isl_multi_union_pw_aff
*
8463 isl_multi_union_pw_aff_multi_val_on_domain_aligned(
8464 __isl_take isl_union_set
*domain
, __isl_take isl_multi_val
*mv
)
8468 isl_multi_union_pw_aff
*mupa
;
8473 n
= isl_multi_val_dim(mv
, isl_dim_set
);
8474 space
= isl_multi_val_get_space(mv
);
8475 mupa
= isl_multi_union_pw_aff_alloc(space
);
8476 for (i
= 0; i
< n
; ++i
) {
8478 isl_union_pw_aff
*upa
;
8480 v
= isl_multi_val_get_val(mv
, i
);
8481 upa
= isl_union_pw_aff_val_on_domain(isl_union_set_copy(domain
),
8483 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
8486 isl_union_set_free(domain
);
8487 isl_multi_val_free(mv
);
8490 isl_union_set_free(domain
);
8491 isl_multi_val_free(mv
);
8495 /* Return a multiple union piecewise affine expression
8496 * that is equal to "mv" on "domain".
8498 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_multi_val_on_domain(
8499 __isl_take isl_union_set
*domain
, __isl_take isl_multi_val
*mv
)
8501 isl_bool equal_params
;
8505 equal_params
= isl_space_has_equal_params(domain
->dim
, mv
->space
);
8506 if (equal_params
< 0)
8509 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(
8511 domain
= isl_union_set_align_params(domain
,
8512 isl_multi_val_get_space(mv
));
8513 mv
= isl_multi_val_align_params(mv
, isl_union_set_get_space(domain
));
8514 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(domain
, mv
);
8516 isl_union_set_free(domain
);
8517 isl_multi_val_free(mv
);
8521 /* Return a multiple union piecewise affine expression
8522 * that is equal to "ma" on "domain".
8524 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_multi_aff_on_domain(
8525 __isl_take isl_union_set
*domain
, __isl_take isl_multi_aff
*ma
)
8527 isl_pw_multi_aff
*pma
;
8529 pma
= isl_pw_multi_aff_from_multi_aff(ma
);
8530 return isl_multi_union_pw_aff_pw_multi_aff_on_domain(domain
, pma
);
8533 /* Return a multiple union piecewise affine expression
8534 * that is equal to "pma" on "domain", assuming "domain" and "pma"
8535 * have been aligned.
8537 * If the resulting multi union piecewise affine expression has
8538 * an explicit domain, then assign it the input domain.
8539 * In other cases, the domain is stored in the individual elements.
8541 static __isl_give isl_multi_union_pw_aff
*
8542 isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(
8543 __isl_take isl_union_set
*domain
, __isl_take isl_pw_multi_aff
*pma
)
8547 isl_multi_union_pw_aff
*mupa
;
8549 if (!domain
|| !pma
)
8552 n
= isl_pw_multi_aff_dim(pma
, isl_dim_set
);
8553 space
= isl_pw_multi_aff_get_space(pma
);
8554 mupa
= isl_multi_union_pw_aff_alloc(space
);
8555 for (i
= 0; i
< n
; ++i
) {
8557 isl_union_pw_aff
*upa
;
8559 pa
= isl_pw_multi_aff_get_pw_aff(pma
, i
);
8560 upa
= isl_union_pw_aff_pw_aff_on_domain(
8561 isl_union_set_copy(domain
), pa
);
8562 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
8564 if (isl_multi_union_pw_aff_has_explicit_domain(mupa
))
8565 mupa
= isl_multi_union_pw_aff_intersect_domain(mupa
,
8566 isl_union_set_copy(domain
));
8568 isl_union_set_free(domain
);
8569 isl_pw_multi_aff_free(pma
);
8572 isl_union_set_free(domain
);
8573 isl_pw_multi_aff_free(pma
);
8577 /* Return a multiple union piecewise affine expression
8578 * that is equal to "pma" on "domain".
8580 __isl_give isl_multi_union_pw_aff
*
8581 isl_multi_union_pw_aff_pw_multi_aff_on_domain(__isl_take isl_union_set
*domain
,
8582 __isl_take isl_pw_multi_aff
*pma
)
8584 isl_bool equal_params
;
8587 space
= isl_pw_multi_aff_peek_space(pma
);
8588 equal_params
= isl_union_set_space_has_equal_params(domain
, space
);
8589 if (equal_params
< 0)
8592 return isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(
8594 domain
= isl_union_set_align_params(domain
,
8595 isl_pw_multi_aff_get_space(pma
));
8596 pma
= isl_pw_multi_aff_align_params(pma
,
8597 isl_union_set_get_space(domain
));
8598 return isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(domain
,
8601 isl_union_set_free(domain
);
8602 isl_pw_multi_aff_free(pma
);
8606 /* Return a union set containing those elements in the domains
8607 * of the elements of "mupa" where they are all zero.
8609 * If there are no elements, then simply return the entire domain.
8611 __isl_give isl_union_set
*isl_multi_union_pw_aff_zero_union_set(
8612 __isl_take isl_multi_union_pw_aff
*mupa
)
8615 isl_union_pw_aff
*upa
;
8616 isl_union_set
*zero
;
8621 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8623 return isl_multi_union_pw_aff_domain(mupa
);
8625 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, 0);
8626 zero
= isl_union_pw_aff_zero_union_set(upa
);
8628 for (i
= 1; i
< n
; ++i
) {
8629 isl_union_set
*zero_i
;
8631 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
8632 zero_i
= isl_union_pw_aff_zero_union_set(upa
);
8634 zero
= isl_union_set_intersect(zero
, zero_i
);
8637 isl_multi_union_pw_aff_free(mupa
);
8641 /* Construct a union map mapping the shared domain
8642 * of the union piecewise affine expressions to the range of "mupa"
8643 * in the special case of a 0D multi union piecewise affine expression.
8645 * Construct a map between the explicit domain of "mupa" and
8647 * Note that this assumes that the domain consists of explicit elements.
8649 static __isl_give isl_union_map
*isl_union_map_from_multi_union_pw_aff_0D(
8650 __isl_take isl_multi_union_pw_aff
*mupa
)
8654 isl_union_set
*dom
, *ran
;
8656 space
= isl_multi_union_pw_aff_get_space(mupa
);
8657 dom
= isl_multi_union_pw_aff_domain(mupa
);
8658 ran
= isl_union_set_from_set(isl_set_universe(space
));
8660 is_params
= isl_union_set_is_params(dom
);
8662 dom
= isl_union_set_free(dom
);
8664 isl_die(isl_union_set_get_ctx(dom
), isl_error_invalid
,
8665 "cannot create union map from expression without "
8666 "explicit domain elements",
8667 dom
= isl_union_set_free(dom
));
8669 return isl_union_map_from_domain_and_range(dom
, ran
);
8672 /* Construct a union map mapping the shared domain
8673 * of the union piecewise affine expressions to the range of "mupa"
8674 * with each dimension in the range equated to the
8675 * corresponding union piecewise affine expression.
8677 * If the input is zero-dimensional, then construct a mapping
8678 * from its explicit domain.
8680 __isl_give isl_union_map
*isl_union_map_from_multi_union_pw_aff(
8681 __isl_take isl_multi_union_pw_aff
*mupa
)
8685 isl_union_map
*umap
;
8686 isl_union_pw_aff
*upa
;
8691 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8693 return isl_union_map_from_multi_union_pw_aff_0D(mupa
);
8695 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, 0);
8696 umap
= isl_union_map_from_union_pw_aff(upa
);
8698 for (i
= 1; i
< n
; ++i
) {
8699 isl_union_map
*umap_i
;
8701 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
8702 umap_i
= isl_union_map_from_union_pw_aff(upa
);
8703 umap
= isl_union_map_flat_range_product(umap
, umap_i
);
8706 space
= isl_multi_union_pw_aff_get_space(mupa
);
8707 umap
= isl_union_map_reset_range_space(umap
, space
);
8709 isl_multi_union_pw_aff_free(mupa
);
8713 /* Internal data structure for isl_union_pw_multi_aff_reset_range_space.
8714 * "range" is the space from which to set the range space.
8715 * "res" collects the results.
8717 struct isl_union_pw_multi_aff_reset_range_space_data
{
8719 isl_union_pw_multi_aff
*res
;
8722 /* Replace the range space of "pma" by the range space of data->range and
8723 * add the result to data->res.
8725 static isl_stat
reset_range_space(__isl_take isl_pw_multi_aff
*pma
, void *user
)
8727 struct isl_union_pw_multi_aff_reset_range_space_data
*data
= user
;
8730 space
= isl_pw_multi_aff_get_space(pma
);
8731 space
= isl_space_domain(space
);
8732 space
= isl_space_extend_domain_with_range(space
,
8733 isl_space_copy(data
->range
));
8734 pma
= isl_pw_multi_aff_reset_space(pma
, space
);
8735 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma
);
8737 return data
->res
? isl_stat_ok
: isl_stat_error
;
8740 /* Replace the range space of all the piecewise affine expressions in "upma" by
8741 * the range space of "space".
8743 * This assumes that all these expressions have the same output dimension.
8745 * Since the spaces of the expressions change, so do their hash values.
8746 * We therefore need to create a new isl_union_pw_multi_aff.
8747 * Note that the hash value is currently computed based on the entire
8748 * space even though there can only be a single expression with a given
8751 static __isl_give isl_union_pw_multi_aff
*
8752 isl_union_pw_multi_aff_reset_range_space(
8753 __isl_take isl_union_pw_multi_aff
*upma
, __isl_take isl_space
*space
)
8755 struct isl_union_pw_multi_aff_reset_range_space_data data
= { space
};
8756 isl_space
*space_upma
;
8758 space_upma
= isl_union_pw_multi_aff_get_space(upma
);
8759 data
.res
= isl_union_pw_multi_aff_empty(space_upma
);
8760 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma
,
8761 &reset_range_space
, &data
) < 0)
8762 data
.res
= isl_union_pw_multi_aff_free(data
.res
);
8764 isl_space_free(space
);
8765 isl_union_pw_multi_aff_free(upma
);
8769 /* Construct and return a union piecewise multi affine expression
8770 * that is equal to the given multi union piecewise affine expression,
8771 * in the special case of a 0D multi union piecewise affine expression.
8773 * Construct a union piecewise multi affine expression
8774 * on top of the explicit domain of the input.
8776 __isl_give isl_union_pw_multi_aff
*
8777 isl_union_pw_multi_aff_from_multi_union_pw_aff_0D(
8778 __isl_take isl_multi_union_pw_aff
*mupa
)
8782 isl_union_set
*domain
;
8784 space
= isl_multi_union_pw_aff_get_space(mupa
);
8785 mv
= isl_multi_val_zero(space
);
8786 domain
= isl_multi_union_pw_aff_domain(mupa
);
8787 return isl_union_pw_multi_aff_multi_val_on_domain(domain
, mv
);
8790 /* Construct and return a union piecewise multi affine expression
8791 * that is equal to the given multi union piecewise affine expression.
8793 * If the input is zero-dimensional, then
8794 * construct a union piecewise multi affine expression
8795 * on top of the explicit domain of the input.
8797 __isl_give isl_union_pw_multi_aff
*
8798 isl_union_pw_multi_aff_from_multi_union_pw_aff(
8799 __isl_take isl_multi_union_pw_aff
*mupa
)
8803 isl_union_pw_multi_aff
*upma
;
8804 isl_union_pw_aff
*upa
;
8809 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8811 return isl_union_pw_multi_aff_from_multi_union_pw_aff_0D(mupa
);
8813 space
= isl_multi_union_pw_aff_get_space(mupa
);
8814 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, 0);
8815 upma
= isl_union_pw_multi_aff_from_union_pw_aff(upa
);
8817 for (i
= 1; i
< n
; ++i
) {
8818 isl_union_pw_multi_aff
*upma_i
;
8820 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
8821 upma_i
= isl_union_pw_multi_aff_from_union_pw_aff(upa
);
8822 upma
= isl_union_pw_multi_aff_flat_range_product(upma
, upma_i
);
8825 upma
= isl_union_pw_multi_aff_reset_range_space(upma
, space
);
8827 isl_multi_union_pw_aff_free(mupa
);
8831 /* Intersect the range of "mupa" with "range",
8832 * in the special case where "mupa" is 0D.
8834 * Intersect the domain of "mupa" with the constraints on the parameters
8837 static __isl_give isl_multi_union_pw_aff
*mupa_intersect_range_0D(
8838 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_set
*range
)
8840 range
= isl_set_params(range
);
8841 mupa
= isl_multi_union_pw_aff_intersect_params(mupa
, range
);
8845 /* Intersect the range of "mupa" with "range".
8846 * That is, keep only those domain elements that have a function value
8849 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_intersect_range(
8850 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_set
*range
)
8852 isl_union_pw_multi_aff
*upma
;
8853 isl_union_set
*domain
;
8858 if (!mupa
|| !range
)
8861 space
= isl_set_get_space(range
);
8862 match
= isl_space_tuple_is_equal(mupa
->space
, isl_dim_set
,
8863 space
, isl_dim_set
);
8864 isl_space_free(space
);
8868 isl_die(isl_multi_union_pw_aff_get_ctx(mupa
), isl_error_invalid
,
8869 "space don't match", goto error
);
8870 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8872 return mupa_intersect_range_0D(mupa
, range
);
8874 upma
= isl_union_pw_multi_aff_from_multi_union_pw_aff(
8875 isl_multi_union_pw_aff_copy(mupa
));
8876 domain
= isl_union_set_from_set(range
);
8877 domain
= isl_union_set_preimage_union_pw_multi_aff(domain
, upma
);
8878 mupa
= isl_multi_union_pw_aff_intersect_domain(mupa
, domain
);
8882 isl_multi_union_pw_aff_free(mupa
);
8883 isl_set_free(range
);
8887 /* Return the shared domain of the elements of "mupa",
8888 * in the special case where "mupa" is zero-dimensional.
8890 * Return the explicit domain of "mupa".
8891 * Note that this domain may be a parameter set, either
8892 * because "mupa" is meant to live in a set space or
8893 * because no explicit domain has been set.
8895 __isl_give isl_union_set
*isl_multi_union_pw_aff_domain_0D(
8896 __isl_take isl_multi_union_pw_aff
*mupa
)
8900 dom
= isl_multi_union_pw_aff_get_explicit_domain(mupa
);
8901 isl_multi_union_pw_aff_free(mupa
);
8906 /* Return the shared domain of the elements of "mupa".
8908 * If "mupa" is zero-dimensional, then return its explicit domain.
8910 __isl_give isl_union_set
*isl_multi_union_pw_aff_domain(
8911 __isl_take isl_multi_union_pw_aff
*mupa
)
8914 isl_union_pw_aff
*upa
;
8920 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8922 return isl_multi_union_pw_aff_domain_0D(mupa
);
8924 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, 0);
8925 dom
= isl_union_pw_aff_domain(upa
);
8926 for (i
= 1; i
< n
; ++i
) {
8927 isl_union_set
*dom_i
;
8929 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
8930 dom_i
= isl_union_pw_aff_domain(upa
);
8931 dom
= isl_union_set_intersect(dom
, dom_i
);
8934 isl_multi_union_pw_aff_free(mupa
);
8938 /* Apply "aff" to "mupa". The space of "mupa" is equal to the domain of "aff".
8939 * In particular, the spaces have been aligned.
8940 * The result is defined over the shared domain of the elements of "mupa"
8942 * We first extract the parametric constant part of "aff" and
8943 * define that over the shared domain.
8944 * Then we iterate over all input dimensions of "aff" and add the corresponding
8945 * multiples of the elements of "mupa".
8946 * Finally, we consider the integer divisions, calling the function
8947 * recursively to obtain an isl_union_pw_aff corresponding to the
8948 * integer division argument.
8950 static __isl_give isl_union_pw_aff
*multi_union_pw_aff_apply_aff(
8951 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_aff
*aff
)
8954 isl_union_pw_aff
*upa
;
8955 isl_union_set
*uset
;
8959 n_in
= isl_aff_dim(aff
, isl_dim_in
);
8960 n_div
= isl_aff_dim(aff
, isl_dim_div
);
8962 uset
= isl_multi_union_pw_aff_domain(isl_multi_union_pw_aff_copy(mupa
));
8963 cst
= isl_aff_copy(aff
);
8964 cst
= isl_aff_drop_dims(cst
, isl_dim_div
, 0, n_div
);
8965 cst
= isl_aff_drop_dims(cst
, isl_dim_in
, 0, n_in
);
8966 cst
= isl_aff_project_domain_on_params(cst
);
8967 upa
= isl_union_pw_aff_aff_on_domain(uset
, cst
);
8969 for (i
= 0; i
< n_in
; ++i
) {
8970 isl_union_pw_aff
*upa_i
;
8972 if (!isl_aff_involves_dims(aff
, isl_dim_in
, i
, 1))
8974 v
= isl_aff_get_coefficient_val(aff
, isl_dim_in
, i
);
8975 upa_i
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
8976 upa_i
= isl_union_pw_aff_scale_val(upa_i
, v
);
8977 upa
= isl_union_pw_aff_add(upa
, upa_i
);
8980 for (i
= 0; i
< n_div
; ++i
) {
8982 isl_union_pw_aff
*upa_i
;
8984 if (!isl_aff_involves_dims(aff
, isl_dim_div
, i
, 1))
8986 div
= isl_aff_get_div(aff
, i
);
8987 upa_i
= multi_union_pw_aff_apply_aff(
8988 isl_multi_union_pw_aff_copy(mupa
), div
);
8989 upa_i
= isl_union_pw_aff_floor(upa_i
);
8990 v
= isl_aff_get_coefficient_val(aff
, isl_dim_div
, i
);
8991 upa_i
= isl_union_pw_aff_scale_val(upa_i
, v
);
8992 upa
= isl_union_pw_aff_add(upa
, upa_i
);
8995 isl_multi_union_pw_aff_free(mupa
);
9001 /* Apply "aff" to "mupa". The space of "mupa" needs to be compatible
9002 * with the domain of "aff".
9003 * Furthermore, the dimension of this space needs to be greater than zero.
9004 * The result is defined over the shared domain of the elements of "mupa"
9006 * We perform these checks and then hand over control to
9007 * multi_union_pw_aff_apply_aff.
9009 __isl_give isl_union_pw_aff
*isl_multi_union_pw_aff_apply_aff(
9010 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_aff
*aff
)
9012 isl_space
*space1
, *space2
;
9015 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
9016 isl_aff_get_space(aff
));
9017 aff
= isl_aff_align_params(aff
, isl_multi_union_pw_aff_get_space(mupa
));
9021 space1
= isl_multi_union_pw_aff_get_space(mupa
);
9022 space2
= isl_aff_get_domain_space(aff
);
9023 equal
= isl_space_is_equal(space1
, space2
);
9024 isl_space_free(space1
);
9025 isl_space_free(space2
);
9029 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
9030 "spaces don't match", goto error
);
9031 if (isl_aff_dim(aff
, isl_dim_in
) == 0)
9032 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
9033 "cannot determine domains", goto error
);
9035 return multi_union_pw_aff_apply_aff(mupa
, aff
);
9037 isl_multi_union_pw_aff_free(mupa
);
9042 /* Apply "ma" to "mupa", in the special case where "mupa" is 0D.
9043 * The space of "mupa" is known to be compatible with the domain of "ma".
9045 * Construct an isl_multi_union_pw_aff that is equal to "ma"
9046 * on the domain of "mupa".
9048 static __isl_give isl_multi_union_pw_aff
*mupa_apply_multi_aff_0D(
9049 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_multi_aff
*ma
)
9053 dom
= isl_multi_union_pw_aff_domain(mupa
);
9054 ma
= isl_multi_aff_project_domain_on_params(ma
);
9056 return isl_multi_union_pw_aff_multi_aff_on_domain(dom
, ma
);
9059 /* Apply "ma" to "mupa". The space of "mupa" needs to be compatible
9060 * with the domain of "ma".
9061 * The result is defined over the shared domain of the elements of "mupa"
9063 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_apply_multi_aff(
9064 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_multi_aff
*ma
)
9066 isl_space
*space1
, *space2
;
9067 isl_multi_union_pw_aff
*res
;
9071 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
9072 isl_multi_aff_get_space(ma
));
9073 ma
= isl_multi_aff_align_params(ma
,
9074 isl_multi_union_pw_aff_get_space(mupa
));
9078 space1
= isl_multi_union_pw_aff_get_space(mupa
);
9079 space2
= isl_multi_aff_get_domain_space(ma
);
9080 equal
= isl_space_is_equal(space1
, space2
);
9081 isl_space_free(space1
);
9082 isl_space_free(space2
);
9086 isl_die(isl_multi_aff_get_ctx(ma
), isl_error_invalid
,
9087 "spaces don't match", goto error
);
9088 n_out
= isl_multi_aff_dim(ma
, isl_dim_out
);
9089 if (isl_multi_aff_dim(ma
, isl_dim_in
) == 0)
9090 return mupa_apply_multi_aff_0D(mupa
, ma
);
9092 space1
= isl_space_range(isl_multi_aff_get_space(ma
));
9093 res
= isl_multi_union_pw_aff_alloc(space1
);
9095 for (i
= 0; i
< n_out
; ++i
) {
9097 isl_union_pw_aff
*upa
;
9099 aff
= isl_multi_aff_get_aff(ma
, i
);
9100 upa
= multi_union_pw_aff_apply_aff(
9101 isl_multi_union_pw_aff_copy(mupa
), aff
);
9102 res
= isl_multi_union_pw_aff_set_union_pw_aff(res
, i
, upa
);
9105 isl_multi_aff_free(ma
);
9106 isl_multi_union_pw_aff_free(mupa
);
9109 isl_multi_union_pw_aff_free(mupa
);
9110 isl_multi_aff_free(ma
);
9114 /* Apply "pa" to "mupa", in the special case where "mupa" is 0D.
9115 * The space of "mupa" is known to be compatible with the domain of "pa".
9117 * Construct an isl_multi_union_pw_aff that is equal to "pa"
9118 * on the domain of "mupa".
9120 static __isl_give isl_union_pw_aff
*isl_multi_union_pw_aff_apply_pw_aff_0D(
9121 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_pw_aff
*pa
)
9125 dom
= isl_multi_union_pw_aff_domain(mupa
);
9126 pa
= isl_pw_aff_project_domain_on_params(pa
);
9128 return isl_union_pw_aff_pw_aff_on_domain(dom
, pa
);
9131 /* Apply "pa" to "mupa". The space of "mupa" needs to be compatible
9132 * with the domain of "pa".
9133 * Furthermore, the dimension of this space needs to be greater than zero.
9134 * The result is defined over the shared domain of the elements of "mupa"
9136 __isl_give isl_union_pw_aff
*isl_multi_union_pw_aff_apply_pw_aff(
9137 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_pw_aff
*pa
)
9141 isl_space
*space
, *space2
;
9142 isl_union_pw_aff
*upa
;
9144 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
9145 isl_pw_aff_get_space(pa
));
9146 pa
= isl_pw_aff_align_params(pa
,
9147 isl_multi_union_pw_aff_get_space(mupa
));
9151 space
= isl_multi_union_pw_aff_get_space(mupa
);
9152 space2
= isl_pw_aff_get_domain_space(pa
);
9153 equal
= isl_space_is_equal(space
, space2
);
9154 isl_space_free(space
);
9155 isl_space_free(space2
);
9159 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
9160 "spaces don't match", goto error
);
9161 if (isl_pw_aff_dim(pa
, isl_dim_in
) == 0)
9162 return isl_multi_union_pw_aff_apply_pw_aff_0D(mupa
, pa
);
9164 space
= isl_space_params(isl_multi_union_pw_aff_get_space(mupa
));
9165 upa
= isl_union_pw_aff_empty(space
);
9167 for (i
= 0; i
< pa
->n
; ++i
) {
9170 isl_multi_union_pw_aff
*mupa_i
;
9171 isl_union_pw_aff
*upa_i
;
9173 mupa_i
= isl_multi_union_pw_aff_copy(mupa
);
9174 domain
= isl_set_copy(pa
->p
[i
].set
);
9175 mupa_i
= isl_multi_union_pw_aff_intersect_range(mupa_i
, domain
);
9176 aff
= isl_aff_copy(pa
->p
[i
].aff
);
9177 upa_i
= multi_union_pw_aff_apply_aff(mupa_i
, aff
);
9178 upa
= isl_union_pw_aff_union_add(upa
, upa_i
);
9181 isl_multi_union_pw_aff_free(mupa
);
9182 isl_pw_aff_free(pa
);
9185 isl_multi_union_pw_aff_free(mupa
);
9186 isl_pw_aff_free(pa
);
9190 /* Apply "pma" to "mupa", in the special case where "mupa" is 0D.
9191 * The space of "mupa" is known to be compatible with the domain of "pma".
9193 * Construct an isl_multi_union_pw_aff that is equal to "pma"
9194 * on the domain of "mupa".
9196 static __isl_give isl_multi_union_pw_aff
*mupa_apply_pw_multi_aff_0D(
9197 __isl_take isl_multi_union_pw_aff
*mupa
,
9198 __isl_take isl_pw_multi_aff
*pma
)
9202 dom
= isl_multi_union_pw_aff_domain(mupa
);
9203 pma
= isl_pw_multi_aff_project_domain_on_params(pma
);
9205 return isl_multi_union_pw_aff_pw_multi_aff_on_domain(dom
, pma
);
9208 /* Apply "pma" to "mupa". The space of "mupa" needs to be compatible
9209 * with the domain of "pma".
9210 * The result is defined over the shared domain of the elements of "mupa"
9212 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_apply_pw_multi_aff(
9213 __isl_take isl_multi_union_pw_aff
*mupa
,
9214 __isl_take isl_pw_multi_aff
*pma
)
9216 isl_space
*space1
, *space2
;
9217 isl_multi_union_pw_aff
*res
;
9221 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
9222 isl_pw_multi_aff_get_space(pma
));
9223 pma
= isl_pw_multi_aff_align_params(pma
,
9224 isl_multi_union_pw_aff_get_space(mupa
));
9228 space1
= isl_multi_union_pw_aff_get_space(mupa
);
9229 space2
= isl_pw_multi_aff_get_domain_space(pma
);
9230 equal
= isl_space_is_equal(space1
, space2
);
9231 isl_space_free(space1
);
9232 isl_space_free(space2
);
9236 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
9237 "spaces don't match", goto error
);
9238 n_out
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
9239 if (isl_pw_multi_aff_dim(pma
, isl_dim_in
) == 0)
9240 return mupa_apply_pw_multi_aff_0D(mupa
, pma
);
9242 space1
= isl_space_range(isl_pw_multi_aff_get_space(pma
));
9243 res
= isl_multi_union_pw_aff_alloc(space1
);
9245 for (i
= 0; i
< n_out
; ++i
) {
9247 isl_union_pw_aff
*upa
;
9249 pa
= isl_pw_multi_aff_get_pw_aff(pma
, i
);
9250 upa
= isl_multi_union_pw_aff_apply_pw_aff(
9251 isl_multi_union_pw_aff_copy(mupa
), pa
);
9252 res
= isl_multi_union_pw_aff_set_union_pw_aff(res
, i
, upa
);
9255 isl_pw_multi_aff_free(pma
);
9256 isl_multi_union_pw_aff_free(mupa
);
9259 isl_multi_union_pw_aff_free(mupa
);
9260 isl_pw_multi_aff_free(pma
);
9264 /* Replace the explicit domain of "mupa" by its preimage under "upma".
9265 * If the explicit domain only keeps track of constraints on the parameters,
9266 * then only update those constraints.
9268 static __isl_give isl_multi_union_pw_aff
*preimage_explicit_domain(
9269 __isl_take isl_multi_union_pw_aff
*mupa
,
9270 __isl_keep isl_union_pw_multi_aff
*upma
)
9274 if (isl_multi_union_pw_aff_check_has_explicit_domain(mupa
) < 0)
9275 return isl_multi_union_pw_aff_free(mupa
);
9277 mupa
= isl_multi_union_pw_aff_cow(mupa
);
9281 is_params
= isl_union_set_is_params(mupa
->u
.dom
);
9283 return isl_multi_union_pw_aff_free(mupa
);
9285 upma
= isl_union_pw_multi_aff_copy(upma
);
9287 mupa
->u
.dom
= isl_union_set_intersect_params(mupa
->u
.dom
,
9288 isl_union_set_params(isl_union_pw_multi_aff_domain(upma
)));
9290 mupa
->u
.dom
= isl_union_set_preimage_union_pw_multi_aff(
9293 return isl_multi_union_pw_aff_free(mupa
);
9297 /* Compute the pullback of "mupa" by the function represented by "upma".
9298 * In other words, plug in "upma" in "mupa". The result contains
9299 * expressions defined over the domain space of "upma".
9301 * Run over all elements of "mupa" and plug in "upma" in each of them.
9303 * If "mupa" has an explicit domain, then it is this domain
9304 * that needs to undergo a pullback instead, i.e., a preimage.
9306 __isl_give isl_multi_union_pw_aff
*
9307 isl_multi_union_pw_aff_pullback_union_pw_multi_aff(
9308 __isl_take isl_multi_union_pw_aff
*mupa
,
9309 __isl_take isl_union_pw_multi_aff
*upma
)
9313 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
9314 isl_union_pw_multi_aff_get_space(upma
));
9315 upma
= isl_union_pw_multi_aff_align_params(upma
,
9316 isl_multi_union_pw_aff_get_space(mupa
));
9317 mupa
= isl_multi_union_pw_aff_cow(mupa
);
9321 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
9322 for (i
= 0; i
< n
; ++i
) {
9323 isl_union_pw_aff
*upa
;
9325 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
9326 upa
= isl_union_pw_aff_pullback_union_pw_multi_aff(upa
,
9327 isl_union_pw_multi_aff_copy(upma
));
9328 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
9331 if (isl_multi_union_pw_aff_has_explicit_domain(mupa
))
9332 mupa
= preimage_explicit_domain(mupa
, upma
);
9334 isl_union_pw_multi_aff_free(upma
);
9337 isl_multi_union_pw_aff_free(mupa
);
9338 isl_union_pw_multi_aff_free(upma
);
9342 /* Extract the sequence of elements in "mupa" with domain space "space"
9343 * (ignoring parameters).
9345 * For the elements of "mupa" that are not defined on the specified space,
9346 * the corresponding element in the result is empty.
9348 __isl_give isl_multi_pw_aff
*isl_multi_union_pw_aff_extract_multi_pw_aff(
9349 __isl_keep isl_multi_union_pw_aff
*mupa
, __isl_take isl_space
*space
)
9352 isl_bool equal_params
;
9353 isl_space
*space_mpa
= NULL
;
9354 isl_multi_pw_aff
*mpa
;
9356 if (!mupa
|| !space
)
9359 space_mpa
= isl_multi_union_pw_aff_get_space(mupa
);
9360 equal_params
= isl_space_has_equal_params(space_mpa
, space
);
9361 if (equal_params
< 0)
9363 if (!equal_params
) {
9364 space
= isl_space_drop_dims(space
, isl_dim_param
,
9365 0, isl_space_dim(space
, isl_dim_param
));
9366 space
= isl_space_align_params(space
,
9367 isl_space_copy(space_mpa
));
9371 space_mpa
= isl_space_map_from_domain_and_range(isl_space_copy(space
),
9373 mpa
= isl_multi_pw_aff_alloc(space_mpa
);
9375 space
= isl_space_from_domain(space
);
9376 space
= isl_space_add_dims(space
, isl_dim_out
, 1);
9377 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
9378 for (i
= 0; i
< n
; ++i
) {
9379 isl_union_pw_aff
*upa
;
9382 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
9383 pa
= isl_union_pw_aff_extract_pw_aff(upa
,
9384 isl_space_copy(space
));
9385 mpa
= isl_multi_pw_aff_set_pw_aff(mpa
, i
, pa
);
9386 isl_union_pw_aff_free(upa
);
9389 isl_space_free(space
);
9392 isl_space_free(space_mpa
);
9393 isl_space_free(space
);
9397 /* Evaluate the affine function "aff" in the void point "pnt".
9398 * In particular, return the value NaN.
9400 static __isl_give isl_val
*eval_void(__isl_take isl_aff
*aff
,
9401 __isl_take isl_point
*pnt
)
9405 ctx
= isl_point_get_ctx(pnt
);
9407 isl_point_free(pnt
);
9408 return isl_val_nan(ctx
);
9411 /* Evaluate the affine expression "aff"
9412 * in the coordinates (with denominator) "pnt".
9414 static __isl_give isl_val
*eval(__isl_keep isl_vec
*aff
,
9415 __isl_keep isl_vec
*pnt
)
9424 ctx
= isl_vec_get_ctx(aff
);
9427 isl_seq_inner_product(aff
->el
+ 1, pnt
->el
, pnt
->size
, &n
);
9428 isl_int_mul(d
, aff
->el
[0], pnt
->el
[0]);
9429 v
= isl_val_rat_from_isl_int(ctx
, n
, d
);
9430 v
= isl_val_normalize(v
);
9437 /* Check that the domain space of "aff" is equal to "space".
9439 static isl_stat
isl_aff_check_has_domain_space(__isl_keep isl_aff
*aff
,
9440 __isl_keep isl_space
*space
)
9444 ok
= isl_space_is_equal(isl_aff_peek_domain_space(aff
), space
);
9446 return isl_stat_error
;
9448 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
9449 "incompatible spaces", return isl_stat_error
);
9453 /* Evaluate the affine function "aff" in "pnt".
9455 __isl_give isl_val
*isl_aff_eval(__isl_take isl_aff
*aff
,
9456 __isl_take isl_point
*pnt
)
9460 isl_local_space
*ls
;
9462 if (isl_aff_check_has_domain_space(aff
, isl_point_peek_space(pnt
)) < 0)
9464 is_void
= isl_point_is_void(pnt
);
9468 return eval_void(aff
, pnt
);
9470 ls
= isl_aff_get_domain_local_space(aff
);
9471 pnt
= isl_local_space_lift_point(ls
, pnt
);
9473 v
= eval(aff
->v
, isl_point_peek_vec(pnt
));
9476 isl_point_free(pnt
);
9481 isl_point_free(pnt
);