2 * Copyright 2011 INRIA Saclay
3 * Copyright 2011 Sven Verdoolaege
4 * Copyright 2012-2014 Ecole Normale Superieure
5 * Copyright 2014 INRIA Rocquencourt
7 * Use of this software is governed by the MIT license
9 * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
10 * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
12 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
13 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
14 * B.P. 105 - 78153 Le Chesnay, France
17 #include <isl_ctx_private.h>
18 #include <isl_map_private.h>
19 #include <isl_union_map_private.h>
20 #include <isl_aff_private.h>
21 #include <isl_space_private.h>
22 #include <isl_local_space_private.h>
23 #include <isl_vec_private.h>
24 #include <isl_mat_private.h>
26 #include <isl/constraint.h>
29 #include <isl_val_private.h>
30 #include <isl_point_private.h>
31 #include <isl_config.h>
36 #include <isl_list_templ.c>
41 #include <isl_list_templ.c>
44 #define BASE union_pw_aff
46 #include <isl_list_templ.c>
49 #define BASE union_pw_multi_aff
51 #include <isl_list_templ.c>
53 __isl_give isl_aff
*isl_aff_alloc_vec(__isl_take isl_local_space
*ls
,
54 __isl_take isl_vec
*v
)
61 aff
= isl_calloc_type(v
->ctx
, struct isl_aff
);
71 isl_local_space_free(ls
);
76 __isl_give isl_aff
*isl_aff_alloc(__isl_take isl_local_space
*ls
)
85 ctx
= isl_local_space_get_ctx(ls
);
86 if (!isl_local_space_divs_known(ls
))
87 isl_die(ctx
, isl_error_invalid
, "local space has unknown divs",
89 if (!isl_local_space_is_set(ls
))
90 isl_die(ctx
, isl_error_invalid
,
91 "domain of affine expression should be a set",
94 total
= isl_local_space_dim(ls
, isl_dim_all
);
95 v
= isl_vec_alloc(ctx
, 1 + 1 + total
);
96 return isl_aff_alloc_vec(ls
, v
);
98 isl_local_space_free(ls
);
102 __isl_give isl_aff
*isl_aff_zero_on_domain(__isl_take isl_local_space
*ls
)
106 aff
= isl_aff_alloc(ls
);
110 isl_int_set_si(aff
->v
->el
[0], 1);
111 isl_seq_clr(aff
->v
->el
+ 1, aff
->v
->size
- 1);
116 /* Return a piecewise affine expression defined on the specified domain
117 * that is equal to zero.
119 __isl_give isl_pw_aff
*isl_pw_aff_zero_on_domain(__isl_take isl_local_space
*ls
)
121 return isl_pw_aff_from_aff(isl_aff_zero_on_domain(ls
));
124 /* Return an affine expression defined on the specified domain
125 * that represents NaN.
127 __isl_give isl_aff
*isl_aff_nan_on_domain(__isl_take isl_local_space
*ls
)
131 aff
= isl_aff_alloc(ls
);
135 isl_seq_clr(aff
->v
->el
, aff
->v
->size
);
140 /* Return a piecewise affine expression defined on the specified domain
141 * that represents NaN.
143 __isl_give isl_pw_aff
*isl_pw_aff_nan_on_domain(__isl_take isl_local_space
*ls
)
145 return isl_pw_aff_from_aff(isl_aff_nan_on_domain(ls
));
148 /* Return an affine expression that is equal to "val" on
149 * domain local space "ls".
151 __isl_give isl_aff
*isl_aff_val_on_domain(__isl_take isl_local_space
*ls
,
152 __isl_take isl_val
*val
)
158 if (!isl_val_is_rat(val
))
159 isl_die(isl_val_get_ctx(val
), isl_error_invalid
,
160 "expecting rational value", goto error
);
162 aff
= isl_aff_alloc(isl_local_space_copy(ls
));
166 isl_seq_clr(aff
->v
->el
+ 2, aff
->v
->size
- 2);
167 isl_int_set(aff
->v
->el
[1], val
->n
);
168 isl_int_set(aff
->v
->el
[0], val
->d
);
170 isl_local_space_free(ls
);
174 isl_local_space_free(ls
);
179 /* Return an affine expression that is equal to the specified dimension
182 __isl_give isl_aff
*isl_aff_var_on_domain(__isl_take isl_local_space
*ls
,
183 enum isl_dim_type type
, unsigned pos
)
191 space
= isl_local_space_get_space(ls
);
194 if (isl_space_is_map(space
))
195 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
196 "expecting (parameter) set space", goto error
);
197 if (pos
>= isl_local_space_dim(ls
, type
))
198 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
199 "position out of bounds", goto error
);
201 isl_space_free(space
);
202 aff
= isl_aff_alloc(ls
);
206 pos
+= isl_local_space_offset(aff
->ls
, type
);
208 isl_int_set_si(aff
->v
->el
[0], 1);
209 isl_seq_clr(aff
->v
->el
+ 1, aff
->v
->size
- 1);
210 isl_int_set_si(aff
->v
->el
[1 + pos
], 1);
214 isl_local_space_free(ls
);
215 isl_space_free(space
);
219 /* Return a piecewise affine expression that is equal to
220 * the specified dimension in "ls".
222 __isl_give isl_pw_aff
*isl_pw_aff_var_on_domain(__isl_take isl_local_space
*ls
,
223 enum isl_dim_type type
, unsigned pos
)
225 return isl_pw_aff_from_aff(isl_aff_var_on_domain(ls
, type
, pos
));
228 /* Return an affine expression that is equal to the parameter
229 * in the domain space "space" with identifier "id".
231 __isl_give isl_aff
*isl_aff_param_on_domain_space_id(
232 __isl_take isl_space
*space
, __isl_take isl_id
*id
)
239 pos
= isl_space_find_dim_by_id(space
, isl_dim_param
, id
);
241 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
242 "parameter not found in space", goto error
);
244 ls
= isl_local_space_from_space(space
);
245 return isl_aff_var_on_domain(ls
, isl_dim_param
, pos
);
247 isl_space_free(space
);
252 __isl_give isl_aff
*isl_aff_copy(__isl_keep isl_aff
*aff
)
261 __isl_give isl_aff
*isl_aff_dup(__isl_keep isl_aff
*aff
)
266 return isl_aff_alloc_vec(isl_local_space_copy(aff
->ls
),
267 isl_vec_copy(aff
->v
));
270 __isl_give isl_aff
*isl_aff_cow(__isl_take isl_aff
*aff
)
278 return isl_aff_dup(aff
);
281 __isl_null isl_aff
*isl_aff_free(__isl_take isl_aff
*aff
)
289 isl_local_space_free(aff
->ls
);
290 isl_vec_free(aff
->v
);
297 isl_ctx
*isl_aff_get_ctx(__isl_keep isl_aff
*aff
)
299 return aff
? isl_local_space_get_ctx(aff
->ls
) : NULL
;
302 /* Return a hash value that digests "aff".
304 uint32_t isl_aff_get_hash(__isl_keep isl_aff
*aff
)
306 uint32_t hash
, ls_hash
, v_hash
;
311 hash
= isl_hash_init();
312 ls_hash
= isl_local_space_get_hash(aff
->ls
);
313 isl_hash_hash(hash
, ls_hash
);
314 v_hash
= isl_vec_get_hash(aff
->v
);
315 isl_hash_hash(hash
, v_hash
);
320 /* Externally, an isl_aff has a map space, but internally, the
321 * ls field corresponds to the domain of that space.
323 int isl_aff_dim(__isl_keep isl_aff
*aff
, enum isl_dim_type type
)
327 if (type
== isl_dim_out
)
329 if (type
== isl_dim_in
)
331 return isl_local_space_dim(aff
->ls
, type
);
334 /* Return the position of the dimension of the given type and name
336 * Return -1 if no such dimension can be found.
338 int isl_aff_find_dim_by_name(__isl_keep isl_aff
*aff
, enum isl_dim_type type
,
343 if (type
== isl_dim_out
)
345 if (type
== isl_dim_in
)
347 return isl_local_space_find_dim_by_name(aff
->ls
, type
, name
);
350 /* Return the domain space of "aff".
352 static __isl_keep isl_space
*isl_aff_peek_domain_space(__isl_keep isl_aff
*aff
)
354 return aff
? isl_local_space_peek_space(aff
->ls
) : NULL
;
357 __isl_give isl_space
*isl_aff_get_domain_space(__isl_keep isl_aff
*aff
)
359 return isl_space_copy(isl_aff_peek_domain_space(aff
));
362 __isl_give isl_space
*isl_aff_get_space(__isl_keep isl_aff
*aff
)
367 space
= isl_local_space_get_space(aff
->ls
);
368 space
= isl_space_from_domain(space
);
369 space
= isl_space_add_dims(space
, isl_dim_out
, 1);
373 __isl_give isl_local_space
*isl_aff_get_domain_local_space(
374 __isl_keep isl_aff
*aff
)
376 return aff
? isl_local_space_copy(aff
->ls
) : NULL
;
379 __isl_give isl_local_space
*isl_aff_get_local_space(__isl_keep isl_aff
*aff
)
384 ls
= isl_local_space_copy(aff
->ls
);
385 ls
= isl_local_space_from_domain(ls
);
386 ls
= isl_local_space_add_dims(ls
, isl_dim_out
, 1);
390 /* Return the local space of the domain of "aff".
391 * This may be either a copy or the local space itself
392 * if there is only one reference to "aff".
393 * This allows the local space to be modified inplace
394 * if both the expression and its local space have only a single reference.
395 * The caller is not allowed to modify "aff" between this call and
396 * a subsequent call to isl_aff_restore_domain_local_space.
397 * The only exception is that isl_aff_free can be called instead.
399 __isl_give isl_local_space
*isl_aff_take_domain_local_space(
400 __isl_keep isl_aff
*aff
)
407 return isl_aff_get_domain_local_space(aff
);
413 /* Set the local space of the domain of "aff" to "ls",
414 * where the local space of "aff" may be missing
415 * due to a preceding call to isl_aff_take_domain_local_space.
416 * However, in this case, "aff" only has a single reference and
417 * then the call to isl_aff_cow has no effect.
419 __isl_give isl_aff
*isl_aff_restore_domain_local_space(
420 __isl_keep isl_aff
*aff
, __isl_take isl_local_space
*ls
)
426 isl_local_space_free(ls
);
430 aff
= isl_aff_cow(aff
);
433 isl_local_space_free(aff
->ls
);
439 isl_local_space_free(ls
);
443 /* Externally, an isl_aff has a map space, but internally, the
444 * ls field corresponds to the domain of that space.
446 const char *isl_aff_get_dim_name(__isl_keep isl_aff
*aff
,
447 enum isl_dim_type type
, unsigned pos
)
451 if (type
== isl_dim_out
)
453 if (type
== isl_dim_in
)
455 return isl_local_space_get_dim_name(aff
->ls
, type
, pos
);
458 __isl_give isl_aff
*isl_aff_reset_domain_space(__isl_take isl_aff
*aff
,
459 __isl_take isl_space
*dim
)
461 aff
= isl_aff_cow(aff
);
465 aff
->ls
= isl_local_space_reset_space(aff
->ls
, dim
);
467 return isl_aff_free(aff
);
476 /* Reset the space of "aff". This function is called from isl_pw_templ.c
477 * and doesn't know if the space of an element object is represented
478 * directly or through its domain. It therefore passes along both.
480 __isl_give isl_aff
*isl_aff_reset_space_and_domain(__isl_take isl_aff
*aff
,
481 __isl_take isl_space
*space
, __isl_take isl_space
*domain
)
483 isl_space_free(space
);
484 return isl_aff_reset_domain_space(aff
, domain
);
487 /* Reorder the coefficients of the affine expression based
488 * on the given reordering.
489 * The reordering r is assumed to have been extended with the local
492 static __isl_give isl_vec
*vec_reorder(__isl_take isl_vec
*vec
,
493 __isl_take isl_reordering
*r
, int n_div
)
501 res
= isl_vec_alloc(vec
->ctx
,
502 2 + isl_space_dim(r
->dim
, isl_dim_all
) + n_div
);
505 isl_seq_cpy(res
->el
, vec
->el
, 2);
506 isl_seq_clr(res
->el
+ 2, res
->size
- 2);
507 for (i
= 0; i
< r
->len
; ++i
)
508 isl_int_set(res
->el
[2 + r
->pos
[i
]], vec
->el
[2 + i
]);
510 isl_reordering_free(r
);
515 isl_reordering_free(r
);
519 /* Reorder the dimensions of the domain of "aff" according
520 * to the given reordering.
522 __isl_give isl_aff
*isl_aff_realign_domain(__isl_take isl_aff
*aff
,
523 __isl_take isl_reordering
*r
)
525 aff
= isl_aff_cow(aff
);
529 r
= isl_reordering_extend(r
, aff
->ls
->div
->n_row
);
530 aff
->v
= vec_reorder(aff
->v
, isl_reordering_copy(r
),
531 aff
->ls
->div
->n_row
);
532 aff
->ls
= isl_local_space_realign(aff
->ls
, r
);
534 if (!aff
->v
|| !aff
->ls
)
535 return isl_aff_free(aff
);
540 isl_reordering_free(r
);
544 __isl_give isl_aff
*isl_aff_align_params(__isl_take isl_aff
*aff
,
545 __isl_take isl_space
*model
)
547 isl_bool equal_params
;
552 equal_params
= isl_space_has_equal_params(aff
->ls
->dim
, model
);
553 if (equal_params
< 0)
558 model
= isl_space_drop_dims(model
, isl_dim_in
,
559 0, isl_space_dim(model
, isl_dim_in
));
560 model
= isl_space_drop_dims(model
, isl_dim_out
,
561 0, isl_space_dim(model
, isl_dim_out
));
562 exp
= isl_parameter_alignment_reordering(aff
->ls
->dim
, model
);
563 exp
= isl_reordering_extend_space(exp
,
564 isl_aff_get_domain_space(aff
));
565 aff
= isl_aff_realign_domain(aff
, exp
);
568 isl_space_free(model
);
571 isl_space_free(model
);
576 /* Is "aff" obviously equal to zero?
578 * If the denominator is zero, then "aff" is not equal to zero.
580 isl_bool
isl_aff_plain_is_zero(__isl_keep isl_aff
*aff
)
583 return isl_bool_error
;
585 if (isl_int_is_zero(aff
->v
->el
[0]))
586 return isl_bool_false
;
587 return isl_seq_first_non_zero(aff
->v
->el
+ 1, aff
->v
->size
- 1) < 0;
590 /* Does "aff" represent NaN?
592 isl_bool
isl_aff_is_nan(__isl_keep isl_aff
*aff
)
595 return isl_bool_error
;
597 return isl_seq_first_non_zero(aff
->v
->el
, 2) < 0;
600 /* Are "aff1" and "aff2" obviously equal?
602 * NaN is not equal to anything, not even to another NaN.
604 isl_bool
isl_aff_plain_is_equal(__isl_keep isl_aff
*aff1
,
605 __isl_keep isl_aff
*aff2
)
610 return isl_bool_error
;
612 if (isl_aff_is_nan(aff1
) || isl_aff_is_nan(aff2
))
613 return isl_bool_false
;
615 equal
= isl_local_space_is_equal(aff1
->ls
, aff2
->ls
);
616 if (equal
< 0 || !equal
)
619 return isl_vec_is_equal(aff1
->v
, aff2
->v
);
622 /* Return the common denominator of "aff" in "v".
624 * We cannot return anything meaningful in case of a NaN.
626 isl_stat
isl_aff_get_denominator(__isl_keep isl_aff
*aff
, isl_int
*v
)
629 return isl_stat_error
;
630 if (isl_aff_is_nan(aff
))
631 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
632 "cannot get denominator of NaN", return isl_stat_error
);
633 isl_int_set(*v
, aff
->v
->el
[0]);
637 /* Return the common denominator of "aff".
639 __isl_give isl_val
*isl_aff_get_denominator_val(__isl_keep isl_aff
*aff
)
646 ctx
= isl_aff_get_ctx(aff
);
647 if (isl_aff_is_nan(aff
))
648 return isl_val_nan(ctx
);
649 return isl_val_int_from_isl_int(ctx
, aff
->v
->el
[0]);
652 /* Return the constant term of "aff".
654 __isl_give isl_val
*isl_aff_get_constant_val(__isl_keep isl_aff
*aff
)
662 ctx
= isl_aff_get_ctx(aff
);
663 if (isl_aff_is_nan(aff
))
664 return isl_val_nan(ctx
);
665 v
= isl_val_rat_from_isl_int(ctx
, aff
->v
->el
[1], aff
->v
->el
[0]);
666 return isl_val_normalize(v
);
669 /* Return the coefficient of the variable of type "type" at position "pos"
672 __isl_give isl_val
*isl_aff_get_coefficient_val(__isl_keep isl_aff
*aff
,
673 enum isl_dim_type type
, int pos
)
681 ctx
= isl_aff_get_ctx(aff
);
682 if (type
== isl_dim_out
)
683 isl_die(ctx
, isl_error_invalid
,
684 "output/set dimension does not have a coefficient",
686 if (type
== isl_dim_in
)
689 if (pos
>= isl_local_space_dim(aff
->ls
, type
))
690 isl_die(ctx
, isl_error_invalid
,
691 "position out of bounds", return NULL
);
693 if (isl_aff_is_nan(aff
))
694 return isl_val_nan(ctx
);
695 pos
+= isl_local_space_offset(aff
->ls
, type
);
696 v
= isl_val_rat_from_isl_int(ctx
, aff
->v
->el
[1 + pos
], aff
->v
->el
[0]);
697 return isl_val_normalize(v
);
700 /* Return the sign of the coefficient of the variable of type "type"
701 * at position "pos" of "aff".
703 int isl_aff_coefficient_sgn(__isl_keep isl_aff
*aff
, enum isl_dim_type type
,
711 ctx
= isl_aff_get_ctx(aff
);
712 if (type
== isl_dim_out
)
713 isl_die(ctx
, isl_error_invalid
,
714 "output/set dimension does not have a coefficient",
716 if (type
== isl_dim_in
)
719 if (pos
>= isl_local_space_dim(aff
->ls
, type
))
720 isl_die(ctx
, isl_error_invalid
,
721 "position out of bounds", return 0);
723 pos
+= isl_local_space_offset(aff
->ls
, type
);
724 return isl_int_sgn(aff
->v
->el
[1 + pos
]);
727 /* Replace the numerator of the constant term of "aff" by "v".
729 * A NaN is unaffected by this operation.
731 __isl_give isl_aff
*isl_aff_set_constant(__isl_take isl_aff
*aff
, isl_int v
)
735 if (isl_aff_is_nan(aff
))
737 aff
= isl_aff_cow(aff
);
741 aff
->v
= isl_vec_cow(aff
->v
);
743 return isl_aff_free(aff
);
745 isl_int_set(aff
->v
->el
[1], v
);
750 /* Replace the constant term of "aff" by "v".
752 * A NaN is unaffected by this operation.
754 __isl_give isl_aff
*isl_aff_set_constant_val(__isl_take isl_aff
*aff
,
755 __isl_take isl_val
*v
)
760 if (isl_aff_is_nan(aff
)) {
765 if (!isl_val_is_rat(v
))
766 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
767 "expecting rational value", goto error
);
769 if (isl_int_eq(aff
->v
->el
[1], v
->n
) &&
770 isl_int_eq(aff
->v
->el
[0], v
->d
)) {
775 aff
= isl_aff_cow(aff
);
778 aff
->v
= isl_vec_cow(aff
->v
);
782 if (isl_int_eq(aff
->v
->el
[0], v
->d
)) {
783 isl_int_set(aff
->v
->el
[1], v
->n
);
784 } else if (isl_int_is_one(v
->d
)) {
785 isl_int_mul(aff
->v
->el
[1], aff
->v
->el
[0], v
->n
);
787 isl_seq_scale(aff
->v
->el
+ 1,
788 aff
->v
->el
+ 1, v
->d
, aff
->v
->size
- 1);
789 isl_int_mul(aff
->v
->el
[1], aff
->v
->el
[0], v
->n
);
790 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], v
->d
);
791 aff
->v
= isl_vec_normalize(aff
->v
);
804 /* Add "v" to the constant term of "aff".
806 * A NaN is unaffected by this operation.
808 __isl_give isl_aff
*isl_aff_add_constant(__isl_take isl_aff
*aff
, isl_int v
)
810 if (isl_int_is_zero(v
))
815 if (isl_aff_is_nan(aff
))
817 aff
= isl_aff_cow(aff
);
821 aff
->v
= isl_vec_cow(aff
->v
);
823 return isl_aff_free(aff
);
825 isl_int_addmul(aff
->v
->el
[1], aff
->v
->el
[0], v
);
830 /* Add "v" to the constant term of "aff".
832 * A NaN is unaffected by this operation.
834 __isl_give isl_aff
*isl_aff_add_constant_val(__isl_take isl_aff
*aff
,
835 __isl_take isl_val
*v
)
840 if (isl_aff_is_nan(aff
) || isl_val_is_zero(v
)) {
845 if (!isl_val_is_rat(v
))
846 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
847 "expecting rational value", goto error
);
849 aff
= isl_aff_cow(aff
);
853 aff
->v
= isl_vec_cow(aff
->v
);
857 if (isl_int_is_one(v
->d
)) {
858 isl_int_addmul(aff
->v
->el
[1], aff
->v
->el
[0], v
->n
);
859 } else if (isl_int_eq(aff
->v
->el
[0], v
->d
)) {
860 isl_int_add(aff
->v
->el
[1], aff
->v
->el
[1], v
->n
);
861 aff
->v
= isl_vec_normalize(aff
->v
);
865 isl_seq_scale(aff
->v
->el
+ 1,
866 aff
->v
->el
+ 1, v
->d
, aff
->v
->size
- 1);
867 isl_int_addmul(aff
->v
->el
[1], aff
->v
->el
[0], v
->n
);
868 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], v
->d
);
869 aff
->v
= isl_vec_normalize(aff
->v
);
882 __isl_give isl_aff
*isl_aff_add_constant_si(__isl_take isl_aff
*aff
, int v
)
887 isl_int_set_si(t
, v
);
888 aff
= isl_aff_add_constant(aff
, t
);
894 /* Add "v" to the numerator of the constant term of "aff".
896 * A NaN is unaffected by this operation.
898 __isl_give isl_aff
*isl_aff_add_constant_num(__isl_take isl_aff
*aff
, isl_int v
)
900 if (isl_int_is_zero(v
))
905 if (isl_aff_is_nan(aff
))
907 aff
= isl_aff_cow(aff
);
911 aff
->v
= isl_vec_cow(aff
->v
);
913 return isl_aff_free(aff
);
915 isl_int_add(aff
->v
->el
[1], aff
->v
->el
[1], v
);
920 /* Add "v" to the numerator of the constant term of "aff".
922 * A NaN is unaffected by this operation.
924 __isl_give isl_aff
*isl_aff_add_constant_num_si(__isl_take isl_aff
*aff
, int v
)
932 isl_int_set_si(t
, v
);
933 aff
= isl_aff_add_constant_num(aff
, t
);
939 /* Replace the numerator of the constant term of "aff" by "v".
941 * A NaN is unaffected by this operation.
943 __isl_give isl_aff
*isl_aff_set_constant_si(__isl_take isl_aff
*aff
, int v
)
947 if (isl_aff_is_nan(aff
))
949 aff
= isl_aff_cow(aff
);
953 aff
->v
= isl_vec_cow(aff
->v
);
955 return isl_aff_free(aff
);
957 isl_int_set_si(aff
->v
->el
[1], v
);
962 /* Replace the numerator of the coefficient of the variable of type "type"
963 * at position "pos" of "aff" by "v".
965 * A NaN is unaffected by this operation.
967 __isl_give isl_aff
*isl_aff_set_coefficient(__isl_take isl_aff
*aff
,
968 enum isl_dim_type type
, int pos
, isl_int v
)
973 if (type
== isl_dim_out
)
974 isl_die(aff
->v
->ctx
, isl_error_invalid
,
975 "output/set dimension does not have a coefficient",
976 return isl_aff_free(aff
));
977 if (type
== isl_dim_in
)
980 if (pos
>= isl_local_space_dim(aff
->ls
, type
))
981 isl_die(aff
->v
->ctx
, isl_error_invalid
,
982 "position out of bounds", return isl_aff_free(aff
));
984 if (isl_aff_is_nan(aff
))
986 aff
= isl_aff_cow(aff
);
990 aff
->v
= isl_vec_cow(aff
->v
);
992 return isl_aff_free(aff
);
994 pos
+= isl_local_space_offset(aff
->ls
, type
);
995 isl_int_set(aff
->v
->el
[1 + pos
], v
);
1000 /* Replace the numerator of the coefficient of the variable of type "type"
1001 * at position "pos" of "aff" by "v".
1003 * A NaN is unaffected by this operation.
1005 __isl_give isl_aff
*isl_aff_set_coefficient_si(__isl_take isl_aff
*aff
,
1006 enum isl_dim_type type
, int pos
, int v
)
1011 if (type
== isl_dim_out
)
1012 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1013 "output/set dimension does not have a coefficient",
1014 return isl_aff_free(aff
));
1015 if (type
== isl_dim_in
)
1018 if (pos
< 0 || pos
>= isl_local_space_dim(aff
->ls
, type
))
1019 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1020 "position out of bounds", return isl_aff_free(aff
));
1022 if (isl_aff_is_nan(aff
))
1024 pos
+= isl_local_space_offset(aff
->ls
, type
);
1025 if (isl_int_cmp_si(aff
->v
->el
[1 + pos
], v
) == 0)
1028 aff
= isl_aff_cow(aff
);
1032 aff
->v
= isl_vec_cow(aff
->v
);
1034 return isl_aff_free(aff
);
1036 isl_int_set_si(aff
->v
->el
[1 + pos
], v
);
1041 /* Replace the coefficient of the variable of type "type" at position "pos"
1044 * A NaN is unaffected by this operation.
1046 __isl_give isl_aff
*isl_aff_set_coefficient_val(__isl_take isl_aff
*aff
,
1047 enum isl_dim_type type
, int pos
, __isl_take isl_val
*v
)
1052 if (type
== isl_dim_out
)
1053 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1054 "output/set dimension does not have a coefficient",
1056 if (type
== isl_dim_in
)
1059 if (pos
>= isl_local_space_dim(aff
->ls
, type
))
1060 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1061 "position out of bounds", goto error
);
1063 if (isl_aff_is_nan(aff
)) {
1067 if (!isl_val_is_rat(v
))
1068 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1069 "expecting rational value", goto error
);
1071 pos
+= isl_local_space_offset(aff
->ls
, type
);
1072 if (isl_int_eq(aff
->v
->el
[1 + pos
], v
->n
) &&
1073 isl_int_eq(aff
->v
->el
[0], v
->d
)) {
1078 aff
= isl_aff_cow(aff
);
1081 aff
->v
= isl_vec_cow(aff
->v
);
1085 if (isl_int_eq(aff
->v
->el
[0], v
->d
)) {
1086 isl_int_set(aff
->v
->el
[1 + pos
], v
->n
);
1087 } else if (isl_int_is_one(v
->d
)) {
1088 isl_int_mul(aff
->v
->el
[1 + pos
], aff
->v
->el
[0], v
->n
);
1090 isl_seq_scale(aff
->v
->el
+ 1,
1091 aff
->v
->el
+ 1, v
->d
, aff
->v
->size
- 1);
1092 isl_int_mul(aff
->v
->el
[1 + pos
], aff
->v
->el
[0], v
->n
);
1093 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], v
->d
);
1094 aff
->v
= isl_vec_normalize(aff
->v
);
1107 /* Add "v" to the coefficient of the variable of type "type"
1108 * at position "pos" of "aff".
1110 * A NaN is unaffected by this operation.
1112 __isl_give isl_aff
*isl_aff_add_coefficient(__isl_take isl_aff
*aff
,
1113 enum isl_dim_type type
, int pos
, isl_int v
)
1118 if (type
== isl_dim_out
)
1119 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1120 "output/set dimension does not have a coefficient",
1121 return isl_aff_free(aff
));
1122 if (type
== isl_dim_in
)
1125 if (pos
>= isl_local_space_dim(aff
->ls
, type
))
1126 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1127 "position out of bounds", return isl_aff_free(aff
));
1129 if (isl_aff_is_nan(aff
))
1131 aff
= isl_aff_cow(aff
);
1135 aff
->v
= isl_vec_cow(aff
->v
);
1137 return isl_aff_free(aff
);
1139 pos
+= isl_local_space_offset(aff
->ls
, type
);
1140 isl_int_addmul(aff
->v
->el
[1 + pos
], aff
->v
->el
[0], v
);
1145 /* Add "v" to the coefficient of the variable of type "type"
1146 * at position "pos" of "aff".
1148 * A NaN is unaffected by this operation.
1150 __isl_give isl_aff
*isl_aff_add_coefficient_val(__isl_take isl_aff
*aff
,
1151 enum isl_dim_type type
, int pos
, __isl_take isl_val
*v
)
1156 if (isl_val_is_zero(v
)) {
1161 if (type
== isl_dim_out
)
1162 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1163 "output/set dimension does not have a coefficient",
1165 if (type
== isl_dim_in
)
1168 if (pos
>= isl_local_space_dim(aff
->ls
, type
))
1169 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1170 "position out of bounds", goto error
);
1172 if (isl_aff_is_nan(aff
)) {
1176 if (!isl_val_is_rat(v
))
1177 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1178 "expecting rational value", goto error
);
1180 aff
= isl_aff_cow(aff
);
1184 aff
->v
= isl_vec_cow(aff
->v
);
1188 pos
+= isl_local_space_offset(aff
->ls
, type
);
1189 if (isl_int_is_one(v
->d
)) {
1190 isl_int_addmul(aff
->v
->el
[1 + pos
], aff
->v
->el
[0], v
->n
);
1191 } else if (isl_int_eq(aff
->v
->el
[0], v
->d
)) {
1192 isl_int_add(aff
->v
->el
[1 + pos
], aff
->v
->el
[1 + pos
], v
->n
);
1193 aff
->v
= isl_vec_normalize(aff
->v
);
1197 isl_seq_scale(aff
->v
->el
+ 1,
1198 aff
->v
->el
+ 1, v
->d
, aff
->v
->size
- 1);
1199 isl_int_addmul(aff
->v
->el
[1 + pos
], aff
->v
->el
[0], v
->n
);
1200 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], v
->d
);
1201 aff
->v
= isl_vec_normalize(aff
->v
);
1214 __isl_give isl_aff
*isl_aff_add_coefficient_si(__isl_take isl_aff
*aff
,
1215 enum isl_dim_type type
, int pos
, int v
)
1220 isl_int_set_si(t
, v
);
1221 aff
= isl_aff_add_coefficient(aff
, type
, pos
, t
);
1227 __isl_give isl_aff
*isl_aff_get_div(__isl_keep isl_aff
*aff
, int pos
)
1232 return isl_local_space_get_div(aff
->ls
, pos
);
1235 /* Return the negation of "aff".
1237 * As a special case, -NaN = NaN.
1239 __isl_give isl_aff
*isl_aff_neg(__isl_take isl_aff
*aff
)
1243 if (isl_aff_is_nan(aff
))
1245 aff
= isl_aff_cow(aff
);
1248 aff
->v
= isl_vec_cow(aff
->v
);
1250 return isl_aff_free(aff
);
1252 isl_seq_neg(aff
->v
->el
+ 1, aff
->v
->el
+ 1, aff
->v
->size
- 1);
1257 /* Remove divs from the local space that do not appear in the affine
1259 * We currently only remove divs at the end.
1260 * Some intermediate divs may also not appear directly in the affine
1261 * expression, but we would also need to check that no other divs are
1262 * defined in terms of them.
1264 __isl_give isl_aff
*isl_aff_remove_unused_divs(__isl_take isl_aff
*aff
)
1273 n
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
1274 off
= isl_local_space_offset(aff
->ls
, isl_dim_div
);
1276 pos
= isl_seq_last_non_zero(aff
->v
->el
+ 1 + off
, n
) + 1;
1280 aff
= isl_aff_cow(aff
);
1284 aff
->ls
= isl_local_space_drop_dims(aff
->ls
, isl_dim_div
, pos
, n
- pos
);
1285 aff
->v
= isl_vec_drop_els(aff
->v
, 1 + off
+ pos
, n
- pos
);
1286 if (!aff
->ls
|| !aff
->v
)
1287 return isl_aff_free(aff
);
1292 /* Look for any divs in the aff->ls with a denominator equal to one
1293 * and plug them into the affine expression and any subsequent divs
1294 * that may reference the div.
1296 static __isl_give isl_aff
*plug_in_integral_divs(__isl_take isl_aff
*aff
)
1302 isl_local_space
*ls
;
1308 n
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
1310 for (i
= 0; i
< n
; ++i
) {
1311 if (!isl_int_is_one(aff
->ls
->div
->row
[i
][0]))
1313 ls
= isl_local_space_copy(aff
->ls
);
1314 ls
= isl_local_space_substitute_seq(ls
, isl_dim_div
, i
,
1315 aff
->ls
->div
->row
[i
], len
, i
+ 1, n
- (i
+ 1));
1316 vec
= isl_vec_copy(aff
->v
);
1317 vec
= isl_vec_cow(vec
);
1323 pos
= isl_local_space_offset(aff
->ls
, isl_dim_div
) + i
;
1324 isl_seq_substitute(vec
->el
, pos
, aff
->ls
->div
->row
[i
],
1329 isl_vec_free(aff
->v
);
1331 isl_local_space_free(aff
->ls
);
1338 isl_local_space_free(ls
);
1339 return isl_aff_free(aff
);
1342 /* Look for any divs j that appear with a unit coefficient inside
1343 * the definitions of other divs i and plug them into the definitions
1346 * In particular, an expression of the form
1348 * floor((f(..) + floor(g(..)/n))/m)
1352 * floor((n * f(..) + g(..))/(n * m))
1354 * This simplification is correct because we can move the expression
1355 * f(..) into the inner floor in the original expression to obtain
1357 * floor(floor((n * f(..) + g(..))/n)/m)
1359 * from which we can derive the simplified expression.
1361 static __isl_give isl_aff
*plug_in_unit_divs(__isl_take isl_aff
*aff
)
1369 n
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
1370 off
= isl_local_space_offset(aff
->ls
, isl_dim_div
);
1371 for (i
= 1; i
< n
; ++i
) {
1372 for (j
= 0; j
< i
; ++j
) {
1373 if (!isl_int_is_one(aff
->ls
->div
->row
[i
][1 + off
+ j
]))
1375 aff
->ls
= isl_local_space_substitute_seq(aff
->ls
,
1376 isl_dim_div
, j
, aff
->ls
->div
->row
[j
],
1377 aff
->v
->size
, i
, 1);
1379 return isl_aff_free(aff
);
1386 /* Swap divs "a" and "b" in "aff", which is assumed to be non-NULL.
1388 * Even though this function is only called on isl_affs with a single
1389 * reference, we are careful to only change aff->v and aff->ls together.
1391 static __isl_give isl_aff
*swap_div(__isl_take isl_aff
*aff
, int a
, int b
)
1393 unsigned off
= isl_local_space_offset(aff
->ls
, isl_dim_div
);
1394 isl_local_space
*ls
;
1397 ls
= isl_local_space_copy(aff
->ls
);
1398 ls
= isl_local_space_swap_div(ls
, a
, b
);
1399 v
= isl_vec_copy(aff
->v
);
1404 isl_int_swap(v
->el
[1 + off
+ a
], v
->el
[1 + off
+ b
]);
1405 isl_vec_free(aff
->v
);
1407 isl_local_space_free(aff
->ls
);
1413 isl_local_space_free(ls
);
1414 return isl_aff_free(aff
);
1417 /* Merge divs "a" and "b" in "aff", which is assumed to be non-NULL.
1419 * We currently do not actually remove div "b", but simply add its
1420 * coefficient to that of "a" and then zero it out.
1422 static __isl_give isl_aff
*merge_divs(__isl_take isl_aff
*aff
, int a
, int b
)
1424 unsigned off
= isl_local_space_offset(aff
->ls
, isl_dim_div
);
1426 if (isl_int_is_zero(aff
->v
->el
[1 + off
+ b
]))
1429 aff
->v
= isl_vec_cow(aff
->v
);
1431 return isl_aff_free(aff
);
1433 isl_int_add(aff
->v
->el
[1 + off
+ a
],
1434 aff
->v
->el
[1 + off
+ a
], aff
->v
->el
[1 + off
+ b
]);
1435 isl_int_set_si(aff
->v
->el
[1 + off
+ b
], 0);
1440 /* Sort the divs in the local space of "aff" according to
1441 * the comparison function "cmp_row" in isl_local_space.c,
1442 * combining the coefficients of identical divs.
1444 * Reordering divs does not change the semantics of "aff",
1445 * so there is no need to call isl_aff_cow.
1446 * Moreover, this function is currently only called on isl_affs
1447 * with a single reference.
1449 static __isl_give isl_aff
*sort_divs(__isl_take isl_aff
*aff
)
1456 n
= isl_aff_dim(aff
, isl_dim_div
);
1457 for (i
= 1; i
< n
; ++i
) {
1458 for (j
= i
- 1; j
>= 0; --j
) {
1459 int cmp
= isl_mat_cmp_div(aff
->ls
->div
, j
, j
+ 1);
1463 aff
= merge_divs(aff
, j
, j
+ 1);
1465 aff
= swap_div(aff
, j
, j
+ 1);
1474 /* Normalize the representation of "aff".
1476 * This function should only be called of "new" isl_affs, i.e.,
1477 * with only a single reference. We therefore do not need to
1478 * worry about affecting other instances.
1480 __isl_give isl_aff
*isl_aff_normalize(__isl_take isl_aff
*aff
)
1484 aff
->v
= isl_vec_normalize(aff
->v
);
1486 return isl_aff_free(aff
);
1487 aff
= plug_in_integral_divs(aff
);
1488 aff
= plug_in_unit_divs(aff
);
1489 aff
= sort_divs(aff
);
1490 aff
= isl_aff_remove_unused_divs(aff
);
1494 /* Given f, return floor(f).
1495 * If f is an integer expression, then just return f.
1496 * If f is a constant, then return the constant floor(f).
1497 * Otherwise, if f = g/m, write g = q m + r,
1498 * create a new div d = [r/m] and return the expression q + d.
1499 * The coefficients in r are taken to lie between -m/2 and m/2.
1501 * reduce_div_coefficients performs the same normalization.
1503 * As a special case, floor(NaN) = NaN.
1505 __isl_give isl_aff
*isl_aff_floor(__isl_take isl_aff
*aff
)
1515 if (isl_aff_is_nan(aff
))
1517 if (isl_int_is_one(aff
->v
->el
[0]))
1520 aff
= isl_aff_cow(aff
);
1524 aff
->v
= isl_vec_cow(aff
->v
);
1526 return isl_aff_free(aff
);
1528 if (isl_aff_is_cst(aff
)) {
1529 isl_int_fdiv_q(aff
->v
->el
[1], aff
->v
->el
[1], aff
->v
->el
[0]);
1530 isl_int_set_si(aff
->v
->el
[0], 1);
1534 div
= isl_vec_copy(aff
->v
);
1535 div
= isl_vec_cow(div
);
1537 return isl_aff_free(aff
);
1539 ctx
= isl_aff_get_ctx(aff
);
1540 isl_int_fdiv_q(aff
->v
->el
[0], aff
->v
->el
[0], ctx
->two
);
1541 for (i
= 1; i
< aff
->v
->size
; ++i
) {
1542 isl_int_fdiv_r(div
->el
[i
], div
->el
[i
], div
->el
[0]);
1543 isl_int_fdiv_q(aff
->v
->el
[i
], aff
->v
->el
[i
], div
->el
[0]);
1544 if (isl_int_gt(div
->el
[i
], aff
->v
->el
[0])) {
1545 isl_int_sub(div
->el
[i
], div
->el
[i
], div
->el
[0]);
1546 isl_int_add_ui(aff
->v
->el
[i
], aff
->v
->el
[i
], 1);
1550 aff
->ls
= isl_local_space_add_div(aff
->ls
, div
);
1552 return isl_aff_free(aff
);
1554 size
= aff
->v
->size
;
1555 aff
->v
= isl_vec_extend(aff
->v
, size
+ 1);
1557 return isl_aff_free(aff
);
1558 isl_int_set_si(aff
->v
->el
[0], 1);
1559 isl_int_set_si(aff
->v
->el
[size
], 1);
1561 aff
= isl_aff_normalize(aff
);
1568 * aff mod m = aff - m * floor(aff/m)
1570 * with m an integer value.
1572 __isl_give isl_aff
*isl_aff_mod_val(__isl_take isl_aff
*aff
,
1573 __isl_take isl_val
*m
)
1580 if (!isl_val_is_int(m
))
1581 isl_die(isl_val_get_ctx(m
), isl_error_invalid
,
1582 "expecting integer modulo", goto error
);
1584 res
= isl_aff_copy(aff
);
1585 aff
= isl_aff_scale_down_val(aff
, isl_val_copy(m
));
1586 aff
= isl_aff_floor(aff
);
1587 aff
= isl_aff_scale_val(aff
, m
);
1588 res
= isl_aff_sub(res
, aff
);
1599 * pwaff mod m = pwaff - m * floor(pwaff/m)
1601 __isl_give isl_pw_aff
*isl_pw_aff_mod(__isl_take isl_pw_aff
*pwaff
, isl_int m
)
1605 res
= isl_pw_aff_copy(pwaff
);
1606 pwaff
= isl_pw_aff_scale_down(pwaff
, m
);
1607 pwaff
= isl_pw_aff_floor(pwaff
);
1608 pwaff
= isl_pw_aff_scale(pwaff
, m
);
1609 res
= isl_pw_aff_sub(res
, pwaff
);
1616 * pa mod m = pa - m * floor(pa/m)
1618 * with m an integer value.
1620 __isl_give isl_pw_aff
*isl_pw_aff_mod_val(__isl_take isl_pw_aff
*pa
,
1621 __isl_take isl_val
*m
)
1625 if (!isl_val_is_int(m
))
1626 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
1627 "expecting integer modulo", goto error
);
1628 pa
= isl_pw_aff_mod(pa
, m
->n
);
1632 isl_pw_aff_free(pa
);
1637 /* Given f, return ceil(f).
1638 * If f is an integer expression, then just return f.
1639 * Otherwise, let f be the expression
1645 * floor((e + m - 1)/m)
1647 * As a special case, ceil(NaN) = NaN.
1649 __isl_give isl_aff
*isl_aff_ceil(__isl_take isl_aff
*aff
)
1654 if (isl_aff_is_nan(aff
))
1656 if (isl_int_is_one(aff
->v
->el
[0]))
1659 aff
= isl_aff_cow(aff
);
1662 aff
->v
= isl_vec_cow(aff
->v
);
1664 return isl_aff_free(aff
);
1666 isl_int_add(aff
->v
->el
[1], aff
->v
->el
[1], aff
->v
->el
[0]);
1667 isl_int_sub_ui(aff
->v
->el
[1], aff
->v
->el
[1], 1);
1668 aff
= isl_aff_floor(aff
);
1673 /* Apply the expansion computed by isl_merge_divs.
1674 * The expansion itself is given by "exp" while the resulting
1675 * list of divs is given by "div".
1677 __isl_give isl_aff
*isl_aff_expand_divs(__isl_take isl_aff
*aff
,
1678 __isl_take isl_mat
*div
, int *exp
)
1684 aff
= isl_aff_cow(aff
);
1688 old_n_div
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
1689 new_n_div
= isl_mat_rows(div
);
1690 offset
= 1 + isl_local_space_offset(aff
->ls
, isl_dim_div
);
1692 aff
->v
= isl_vec_expand(aff
->v
, offset
, old_n_div
, exp
, new_n_div
);
1693 aff
->ls
= isl_local_space_replace_divs(aff
->ls
, div
);
1694 if (!aff
->v
|| !aff
->ls
)
1695 return isl_aff_free(aff
);
1703 /* Add two affine expressions that live in the same local space.
1705 static __isl_give isl_aff
*add_expanded(__isl_take isl_aff
*aff1
,
1706 __isl_take isl_aff
*aff2
)
1710 aff1
= isl_aff_cow(aff1
);
1714 aff1
->v
= isl_vec_cow(aff1
->v
);
1720 isl_int_gcd(gcd
, aff1
->v
->el
[0], aff2
->v
->el
[0]);
1721 isl_int_divexact(f
, aff2
->v
->el
[0], gcd
);
1722 isl_seq_scale(aff1
->v
->el
+ 1, aff1
->v
->el
+ 1, f
, aff1
->v
->size
- 1);
1723 isl_int_divexact(f
, aff1
->v
->el
[0], gcd
);
1724 isl_seq_addmul(aff1
->v
->el
+ 1, f
, aff2
->v
->el
+ 1, aff1
->v
->size
- 1);
1725 isl_int_divexact(f
, aff2
->v
->el
[0], gcd
);
1726 isl_int_mul(aff1
->v
->el
[0], aff1
->v
->el
[0], f
);
1738 /* Return the sum of "aff1" and "aff2".
1740 * If either of the two is NaN, then the result is NaN.
1742 __isl_give isl_aff
*isl_aff_add(__isl_take isl_aff
*aff1
,
1743 __isl_take isl_aff
*aff2
)
1754 ctx
= isl_aff_get_ctx(aff1
);
1755 if (!isl_space_is_equal(aff1
->ls
->dim
, aff2
->ls
->dim
))
1756 isl_die(ctx
, isl_error_invalid
,
1757 "spaces don't match", goto error
);
1759 if (isl_aff_is_nan(aff1
)) {
1763 if (isl_aff_is_nan(aff2
)) {
1768 n_div1
= isl_aff_dim(aff1
, isl_dim_div
);
1769 n_div2
= isl_aff_dim(aff2
, isl_dim_div
);
1770 if (n_div1
== 0 && n_div2
== 0)
1771 return add_expanded(aff1
, aff2
);
1773 exp1
= isl_alloc_array(ctx
, int, n_div1
);
1774 exp2
= isl_alloc_array(ctx
, int, n_div2
);
1775 if ((n_div1
&& !exp1
) || (n_div2
&& !exp2
))
1778 div
= isl_merge_divs(aff1
->ls
->div
, aff2
->ls
->div
, exp1
, exp2
);
1779 aff1
= isl_aff_expand_divs(aff1
, isl_mat_copy(div
), exp1
);
1780 aff2
= isl_aff_expand_divs(aff2
, div
, exp2
);
1784 return add_expanded(aff1
, aff2
);
1793 __isl_give isl_aff
*isl_aff_sub(__isl_take isl_aff
*aff1
,
1794 __isl_take isl_aff
*aff2
)
1796 return isl_aff_add(aff1
, isl_aff_neg(aff2
));
1799 /* Return the result of scaling "aff" by a factor of "f".
1801 * As a special case, f * NaN = NaN.
1803 __isl_give isl_aff
*isl_aff_scale(__isl_take isl_aff
*aff
, isl_int f
)
1809 if (isl_aff_is_nan(aff
))
1812 if (isl_int_is_one(f
))
1815 aff
= isl_aff_cow(aff
);
1818 aff
->v
= isl_vec_cow(aff
->v
);
1820 return isl_aff_free(aff
);
1822 if (isl_int_is_pos(f
) && isl_int_is_divisible_by(aff
->v
->el
[0], f
)) {
1823 isl_int_divexact(aff
->v
->el
[0], aff
->v
->el
[0], f
);
1828 isl_int_gcd(gcd
, aff
->v
->el
[0], f
);
1829 isl_int_divexact(aff
->v
->el
[0], aff
->v
->el
[0], gcd
);
1830 isl_int_divexact(gcd
, f
, gcd
);
1831 isl_seq_scale(aff
->v
->el
+ 1, aff
->v
->el
+ 1, gcd
, aff
->v
->size
- 1);
1837 /* Multiple "aff" by "v".
1839 __isl_give isl_aff
*isl_aff_scale_val(__isl_take isl_aff
*aff
,
1840 __isl_take isl_val
*v
)
1845 if (isl_val_is_one(v
)) {
1850 if (!isl_val_is_rat(v
))
1851 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1852 "expecting rational factor", goto error
);
1854 aff
= isl_aff_scale(aff
, v
->n
);
1855 aff
= isl_aff_scale_down(aff
, v
->d
);
1865 /* Return the result of scaling "aff" down by a factor of "f".
1867 * As a special case, NaN/f = NaN.
1869 __isl_give isl_aff
*isl_aff_scale_down(__isl_take isl_aff
*aff
, isl_int f
)
1875 if (isl_aff_is_nan(aff
))
1878 if (isl_int_is_one(f
))
1881 aff
= isl_aff_cow(aff
);
1885 if (isl_int_is_zero(f
))
1886 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1887 "cannot scale down by zero", return isl_aff_free(aff
));
1889 aff
->v
= isl_vec_cow(aff
->v
);
1891 return isl_aff_free(aff
);
1894 isl_seq_gcd(aff
->v
->el
+ 1, aff
->v
->size
- 1, &gcd
);
1895 isl_int_gcd(gcd
, gcd
, f
);
1896 isl_seq_scale_down(aff
->v
->el
+ 1, aff
->v
->el
+ 1, gcd
, aff
->v
->size
- 1);
1897 isl_int_divexact(gcd
, f
, gcd
);
1898 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], gcd
);
1904 /* Divide "aff" by "v".
1906 __isl_give isl_aff
*isl_aff_scale_down_val(__isl_take isl_aff
*aff
,
1907 __isl_take isl_val
*v
)
1912 if (isl_val_is_one(v
)) {
1917 if (!isl_val_is_rat(v
))
1918 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1919 "expecting rational factor", goto error
);
1920 if (!isl_val_is_pos(v
))
1921 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1922 "factor needs to be positive", goto error
);
1924 aff
= isl_aff_scale(aff
, v
->d
);
1925 aff
= isl_aff_scale_down(aff
, v
->n
);
1935 __isl_give isl_aff
*isl_aff_scale_down_ui(__isl_take isl_aff
*aff
, unsigned f
)
1943 isl_int_set_ui(v
, f
);
1944 aff
= isl_aff_scale_down(aff
, v
);
1950 __isl_give isl_aff
*isl_aff_set_dim_name(__isl_take isl_aff
*aff
,
1951 enum isl_dim_type type
, unsigned pos
, const char *s
)
1953 aff
= isl_aff_cow(aff
);
1956 if (type
== isl_dim_out
)
1957 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1958 "cannot set name of output/set dimension",
1959 return isl_aff_free(aff
));
1960 if (type
== isl_dim_in
)
1962 aff
->ls
= isl_local_space_set_dim_name(aff
->ls
, type
, pos
, s
);
1964 return isl_aff_free(aff
);
1969 __isl_give isl_aff
*isl_aff_set_dim_id(__isl_take isl_aff
*aff
,
1970 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
1972 aff
= isl_aff_cow(aff
);
1975 if (type
== isl_dim_out
)
1976 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1977 "cannot set name of output/set dimension",
1979 if (type
== isl_dim_in
)
1981 aff
->ls
= isl_local_space_set_dim_id(aff
->ls
, type
, pos
, id
);
1983 return isl_aff_free(aff
);
1992 /* Replace the identifier of the input tuple of "aff" by "id".
1993 * type is currently required to be equal to isl_dim_in
1995 __isl_give isl_aff
*isl_aff_set_tuple_id(__isl_take isl_aff
*aff
,
1996 enum isl_dim_type type
, __isl_take isl_id
*id
)
1998 aff
= isl_aff_cow(aff
);
2001 if (type
!= isl_dim_out
)
2002 isl_die(aff
->v
->ctx
, isl_error_invalid
,
2003 "cannot only set id of input tuple", goto error
);
2004 aff
->ls
= isl_local_space_set_tuple_id(aff
->ls
, isl_dim_set
, id
);
2006 return isl_aff_free(aff
);
2015 /* Exploit the equalities in "eq" to simplify the affine expression
2016 * and the expressions of the integer divisions in the local space.
2017 * The integer divisions in this local space are assumed to appear
2018 * as regular dimensions in "eq".
2020 static __isl_give isl_aff
*isl_aff_substitute_equalities_lifted(
2021 __isl_take isl_aff
*aff
, __isl_take isl_basic_set
*eq
)
2029 if (eq
->n_eq
== 0) {
2030 isl_basic_set_free(eq
);
2034 aff
= isl_aff_cow(aff
);
2038 aff
->ls
= isl_local_space_substitute_equalities(aff
->ls
,
2039 isl_basic_set_copy(eq
));
2040 aff
->v
= isl_vec_cow(aff
->v
);
2041 if (!aff
->ls
|| !aff
->v
)
2044 total
= 1 + isl_space_dim(eq
->dim
, isl_dim_all
);
2046 for (i
= 0; i
< eq
->n_eq
; ++i
) {
2047 j
= isl_seq_last_non_zero(eq
->eq
[i
], total
+ n_div
);
2048 if (j
< 0 || j
== 0 || j
>= total
)
2051 isl_seq_elim(aff
->v
->el
+ 1, eq
->eq
[i
], j
, total
,
2055 isl_basic_set_free(eq
);
2056 aff
= isl_aff_normalize(aff
);
2059 isl_basic_set_free(eq
);
2064 /* Exploit the equalities in "eq" to simplify the affine expression
2065 * and the expressions of the integer divisions in the local space.
2067 __isl_give isl_aff
*isl_aff_substitute_equalities(__isl_take isl_aff
*aff
,
2068 __isl_take isl_basic_set
*eq
)
2074 n_div
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
2076 eq
= isl_basic_set_add_dims(eq
, isl_dim_set
, n_div
);
2077 return isl_aff_substitute_equalities_lifted(aff
, eq
);
2079 isl_basic_set_free(eq
);
2084 /* Look for equalities among the variables shared by context and aff
2085 * and the integer divisions of aff, if any.
2086 * The equalities are then used to eliminate coefficients and/or integer
2087 * divisions from aff.
2089 __isl_give isl_aff
*isl_aff_gist(__isl_take isl_aff
*aff
,
2090 __isl_take isl_set
*context
)
2092 isl_basic_set
*hull
;
2097 n_div
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
2099 isl_basic_set
*bset
;
2100 isl_local_space
*ls
;
2101 context
= isl_set_add_dims(context
, isl_dim_set
, n_div
);
2102 ls
= isl_aff_get_domain_local_space(aff
);
2103 bset
= isl_basic_set_from_local_space(ls
);
2104 bset
= isl_basic_set_lift(bset
);
2105 bset
= isl_basic_set_flatten(bset
);
2106 context
= isl_set_intersect(context
,
2107 isl_set_from_basic_set(bset
));
2110 hull
= isl_set_affine_hull(context
);
2111 return isl_aff_substitute_equalities_lifted(aff
, hull
);
2114 isl_set_free(context
);
2118 __isl_give isl_aff
*isl_aff_gist_params(__isl_take isl_aff
*aff
,
2119 __isl_take isl_set
*context
)
2121 isl_set
*dom_context
= isl_set_universe(isl_aff_get_domain_space(aff
));
2122 dom_context
= isl_set_intersect_params(dom_context
, context
);
2123 return isl_aff_gist(aff
, dom_context
);
2126 /* Return a basic set containing those elements in the space
2127 * of aff where it is positive. "rational" should not be set.
2129 * If "aff" is NaN, then it is not positive.
2131 static __isl_give isl_basic_set
*aff_pos_basic_set(__isl_take isl_aff
*aff
,
2134 isl_constraint
*ineq
;
2135 isl_basic_set
*bset
;
2140 if (isl_aff_is_nan(aff
)) {
2141 isl_space
*space
= isl_aff_get_domain_space(aff
);
2143 return isl_basic_set_empty(space
);
2146 isl_die(isl_aff_get_ctx(aff
), isl_error_unsupported
,
2147 "rational sets not supported", goto error
);
2149 ineq
= isl_inequality_from_aff(aff
);
2150 c
= isl_constraint_get_constant_val(ineq
);
2151 c
= isl_val_sub_ui(c
, 1);
2152 ineq
= isl_constraint_set_constant_val(ineq
, c
);
2154 bset
= isl_basic_set_from_constraint(ineq
);
2155 bset
= isl_basic_set_simplify(bset
);
2162 /* Return a basic set containing those elements in the space
2163 * of aff where it is non-negative.
2164 * If "rational" is set, then return a rational basic set.
2166 * If "aff" is NaN, then it is not non-negative (it's not negative either).
2168 static __isl_give isl_basic_set
*aff_nonneg_basic_set(
2169 __isl_take isl_aff
*aff
, int rational
)
2171 isl_constraint
*ineq
;
2172 isl_basic_set
*bset
;
2176 if (isl_aff_is_nan(aff
)) {
2177 isl_space
*space
= isl_aff_get_domain_space(aff
);
2179 return isl_basic_set_empty(space
);
2182 ineq
= isl_inequality_from_aff(aff
);
2184 bset
= isl_basic_set_from_constraint(ineq
);
2186 bset
= isl_basic_set_set_rational(bset
);
2187 bset
= isl_basic_set_simplify(bset
);
2191 /* Return a basic set containing those elements in the space
2192 * of aff where it is non-negative.
2194 __isl_give isl_basic_set
*isl_aff_nonneg_basic_set(__isl_take isl_aff
*aff
)
2196 return aff_nonneg_basic_set(aff
, 0);
2199 /* Return a basic set containing those elements in the domain space
2200 * of "aff" where it is positive.
2202 __isl_give isl_basic_set
*isl_aff_pos_basic_set(__isl_take isl_aff
*aff
)
2204 aff
= isl_aff_add_constant_num_si(aff
, -1);
2205 return isl_aff_nonneg_basic_set(aff
);
2208 /* Return a basic set containing those elements in the domain space
2209 * of aff where it is negative.
2211 __isl_give isl_basic_set
*isl_aff_neg_basic_set(__isl_take isl_aff
*aff
)
2213 aff
= isl_aff_neg(aff
);
2214 return isl_aff_pos_basic_set(aff
);
2217 /* Return a basic set containing those elements in the space
2218 * of aff where it is zero.
2219 * If "rational" is set, then return a rational basic set.
2221 * If "aff" is NaN, then it is not zero.
2223 static __isl_give isl_basic_set
*aff_zero_basic_set(__isl_take isl_aff
*aff
,
2226 isl_constraint
*ineq
;
2227 isl_basic_set
*bset
;
2231 if (isl_aff_is_nan(aff
)) {
2232 isl_space
*space
= isl_aff_get_domain_space(aff
);
2234 return isl_basic_set_empty(space
);
2237 ineq
= isl_equality_from_aff(aff
);
2239 bset
= isl_basic_set_from_constraint(ineq
);
2241 bset
= isl_basic_set_set_rational(bset
);
2242 bset
= isl_basic_set_simplify(bset
);
2246 /* Return a basic set containing those elements in the space
2247 * of aff where it is zero.
2249 __isl_give isl_basic_set
*isl_aff_zero_basic_set(__isl_take isl_aff
*aff
)
2251 return aff_zero_basic_set(aff
, 0);
2254 /* Return a basic set containing those elements in the shared space
2255 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
2257 __isl_give isl_basic_set
*isl_aff_ge_basic_set(__isl_take isl_aff
*aff1
,
2258 __isl_take isl_aff
*aff2
)
2260 aff1
= isl_aff_sub(aff1
, aff2
);
2262 return isl_aff_nonneg_basic_set(aff1
);
2265 /* Return a basic set containing those elements in the shared domain space
2266 * of "aff1" and "aff2" where "aff1" is greater than "aff2".
2268 __isl_give isl_basic_set
*isl_aff_gt_basic_set(__isl_take isl_aff
*aff1
,
2269 __isl_take isl_aff
*aff2
)
2271 aff1
= isl_aff_sub(aff1
, aff2
);
2273 return isl_aff_pos_basic_set(aff1
);
2276 /* Return a set containing those elements in the shared space
2277 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
2279 __isl_give isl_set
*isl_aff_ge_set(__isl_take isl_aff
*aff1
,
2280 __isl_take isl_aff
*aff2
)
2282 return isl_set_from_basic_set(isl_aff_ge_basic_set(aff1
, aff2
));
2285 /* Return a set containing those elements in the shared domain space
2286 * of aff1 and aff2 where aff1 is greater than aff2.
2288 * If either of the two inputs is NaN, then the result is empty,
2289 * as comparisons with NaN always return false.
2291 __isl_give isl_set
*isl_aff_gt_set(__isl_take isl_aff
*aff1
,
2292 __isl_take isl_aff
*aff2
)
2294 return isl_set_from_basic_set(isl_aff_gt_basic_set(aff1
, aff2
));
2297 /* Return a basic set containing those elements in the shared space
2298 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
2300 __isl_give isl_basic_set
*isl_aff_le_basic_set(__isl_take isl_aff
*aff1
,
2301 __isl_take isl_aff
*aff2
)
2303 return isl_aff_ge_basic_set(aff2
, aff1
);
2306 /* Return a basic set containing those elements in the shared domain space
2307 * of "aff1" and "aff2" where "aff1" is smaller than "aff2".
2309 __isl_give isl_basic_set
*isl_aff_lt_basic_set(__isl_take isl_aff
*aff1
,
2310 __isl_take isl_aff
*aff2
)
2312 return isl_aff_gt_basic_set(aff2
, aff1
);
2315 /* Return a set containing those elements in the shared space
2316 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
2318 __isl_give isl_set
*isl_aff_le_set(__isl_take isl_aff
*aff1
,
2319 __isl_take isl_aff
*aff2
)
2321 return isl_aff_ge_set(aff2
, aff1
);
2324 /* Return a set containing those elements in the shared domain space
2325 * of "aff1" and "aff2" where "aff1" is smaller than "aff2".
2327 __isl_give isl_set
*isl_aff_lt_set(__isl_take isl_aff
*aff1
,
2328 __isl_take isl_aff
*aff2
)
2330 return isl_set_from_basic_set(isl_aff_lt_basic_set(aff1
, aff2
));
2333 /* Return a basic set containing those elements in the shared space
2334 * of aff1 and aff2 where aff1 and aff2 are equal.
2336 __isl_give isl_basic_set
*isl_aff_eq_basic_set(__isl_take isl_aff
*aff1
,
2337 __isl_take isl_aff
*aff2
)
2339 aff1
= isl_aff_sub(aff1
, aff2
);
2341 return isl_aff_zero_basic_set(aff1
);
2344 /* Return a set containing those elements in the shared space
2345 * of aff1 and aff2 where aff1 and aff2 are equal.
2347 __isl_give isl_set
*isl_aff_eq_set(__isl_take isl_aff
*aff1
,
2348 __isl_take isl_aff
*aff2
)
2350 return isl_set_from_basic_set(isl_aff_eq_basic_set(aff1
, aff2
));
2353 /* Return a set containing those elements in the shared domain space
2354 * of aff1 and aff2 where aff1 and aff2 are not equal.
2356 * If either of the two inputs is NaN, then the result is empty,
2357 * as comparisons with NaN always return false.
2359 __isl_give isl_set
*isl_aff_ne_set(__isl_take isl_aff
*aff1
,
2360 __isl_take isl_aff
*aff2
)
2362 isl_set
*set_lt
, *set_gt
;
2364 set_lt
= isl_aff_lt_set(isl_aff_copy(aff1
),
2365 isl_aff_copy(aff2
));
2366 set_gt
= isl_aff_gt_set(aff1
, aff2
);
2367 return isl_set_union_disjoint(set_lt
, set_gt
);
2370 __isl_give isl_aff
*isl_aff_add_on_domain(__isl_keep isl_set
*dom
,
2371 __isl_take isl_aff
*aff1
, __isl_take isl_aff
*aff2
)
2373 aff1
= isl_aff_add(aff1
, aff2
);
2374 aff1
= isl_aff_gist(aff1
, isl_set_copy(dom
));
2378 int isl_aff_is_empty(__isl_keep isl_aff
*aff
)
2386 /* Check whether the given affine expression has non-zero coefficient
2387 * for any dimension in the given range or if any of these dimensions
2388 * appear with non-zero coefficients in any of the integer divisions
2389 * involved in the affine expression.
2391 isl_bool
isl_aff_involves_dims(__isl_keep isl_aff
*aff
,
2392 enum isl_dim_type type
, unsigned first
, unsigned n
)
2397 isl_bool involves
= isl_bool_false
;
2400 return isl_bool_error
;
2402 return isl_bool_false
;
2404 ctx
= isl_aff_get_ctx(aff
);
2405 if (first
+ n
> isl_aff_dim(aff
, type
))
2406 isl_die(ctx
, isl_error_invalid
,
2407 "range out of bounds", return isl_bool_error
);
2409 active
= isl_local_space_get_active(aff
->ls
, aff
->v
->el
+ 2);
2413 first
+= isl_local_space_offset(aff
->ls
, type
) - 1;
2414 for (i
= 0; i
< n
; ++i
)
2415 if (active
[first
+ i
]) {
2416 involves
= isl_bool_true
;
2425 return isl_bool_error
;
2428 __isl_give isl_aff
*isl_aff_drop_dims(__isl_take isl_aff
*aff
,
2429 enum isl_dim_type type
, unsigned first
, unsigned n
)
2435 if (type
== isl_dim_out
)
2436 isl_die(aff
->v
->ctx
, isl_error_invalid
,
2437 "cannot drop output/set dimension",
2438 return isl_aff_free(aff
));
2439 if (type
== isl_dim_in
)
2441 if (n
== 0 && !isl_local_space_is_named_or_nested(aff
->ls
, type
))
2444 ctx
= isl_aff_get_ctx(aff
);
2445 if (first
+ n
> isl_local_space_dim(aff
->ls
, type
))
2446 isl_die(ctx
, isl_error_invalid
, "range out of bounds",
2447 return isl_aff_free(aff
));
2449 aff
= isl_aff_cow(aff
);
2453 aff
->ls
= isl_local_space_drop_dims(aff
->ls
, type
, first
, n
);
2455 return isl_aff_free(aff
);
2457 first
+= 1 + isl_local_space_offset(aff
->ls
, type
);
2458 aff
->v
= isl_vec_drop_els(aff
->v
, first
, n
);
2460 return isl_aff_free(aff
);
2465 /* 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_eval.c>
2648 #include <isl_pw_hash.c>
2649 #include <isl_pw_union_opt.c>
2652 #define UNION isl_union_pw_aff
2654 #define PART isl_pw_aff
2656 #define PARTS pw_aff
2658 #include <isl_union_single.c>
2659 #include <isl_union_neg.c>
2661 static __isl_give isl_set
*align_params_pw_pw_set_and(
2662 __isl_take isl_pw_aff
*pwaff1
, __isl_take isl_pw_aff
*pwaff2
,
2663 __isl_give isl_set
*(*fn
)(__isl_take isl_pw_aff
*pwaff1
,
2664 __isl_take isl_pw_aff
*pwaff2
))
2666 isl_bool equal_params
;
2668 if (!pwaff1
|| !pwaff2
)
2670 equal_params
= isl_space_has_equal_params(pwaff1
->dim
, pwaff2
->dim
);
2671 if (equal_params
< 0)
2674 return fn(pwaff1
, pwaff2
);
2675 if (!isl_space_has_named_params(pwaff1
->dim
) ||
2676 !isl_space_has_named_params(pwaff2
->dim
))
2677 isl_die(isl_pw_aff_get_ctx(pwaff1
), isl_error_invalid
,
2678 "unaligned unnamed parameters", goto error
);
2679 pwaff1
= isl_pw_aff_align_params(pwaff1
, isl_pw_aff_get_space(pwaff2
));
2680 pwaff2
= isl_pw_aff_align_params(pwaff2
, isl_pw_aff_get_space(pwaff1
));
2681 return fn(pwaff1
, pwaff2
);
2683 isl_pw_aff_free(pwaff1
);
2684 isl_pw_aff_free(pwaff2
);
2688 /* Align the parameters of the to isl_pw_aff arguments and
2689 * then apply a function "fn" on them that returns an isl_map.
2691 static __isl_give isl_map
*align_params_pw_pw_map_and(
2692 __isl_take isl_pw_aff
*pa1
, __isl_take isl_pw_aff
*pa2
,
2693 __isl_give isl_map
*(*fn
)(__isl_take isl_pw_aff
*pa1
,
2694 __isl_take isl_pw_aff
*pa2
))
2696 isl_bool equal_params
;
2700 equal_params
= isl_space_has_equal_params(pa1
->dim
, pa2
->dim
);
2701 if (equal_params
< 0)
2704 return fn(pa1
, pa2
);
2705 if (!isl_space_has_named_params(pa1
->dim
) ||
2706 !isl_space_has_named_params(pa2
->dim
))
2707 isl_die(isl_pw_aff_get_ctx(pa1
), isl_error_invalid
,
2708 "unaligned unnamed parameters", goto error
);
2709 pa1
= isl_pw_aff_align_params(pa1
, isl_pw_aff_get_space(pa2
));
2710 pa2
= isl_pw_aff_align_params(pa2
, isl_pw_aff_get_space(pa1
));
2711 return fn(pa1
, pa2
);
2713 isl_pw_aff_free(pa1
);
2714 isl_pw_aff_free(pa2
);
2718 /* Compute a piecewise quasi-affine expression with a domain that
2719 * is the union of those of pwaff1 and pwaff2 and such that on each
2720 * cell, the quasi-affine expression is the maximum of those of pwaff1
2721 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2722 * cell, then the associated expression is the defined one.
2724 static __isl_give isl_pw_aff
*pw_aff_union_max(__isl_take isl_pw_aff
*pwaff1
,
2725 __isl_take isl_pw_aff
*pwaff2
)
2727 return isl_pw_aff_union_opt_cmp(pwaff1
, pwaff2
, &isl_aff_ge_set
);
2730 __isl_give isl_pw_aff
*isl_pw_aff_union_max(__isl_take isl_pw_aff
*pwaff1
,
2731 __isl_take isl_pw_aff
*pwaff2
)
2733 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
,
2737 /* Compute a piecewise quasi-affine expression with a domain that
2738 * is the union of those of pwaff1 and pwaff2 and such that on each
2739 * cell, the quasi-affine expression is the minimum of those of pwaff1
2740 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2741 * cell, then the associated expression is the defined one.
2743 static __isl_give isl_pw_aff
*pw_aff_union_min(__isl_take isl_pw_aff
*pwaff1
,
2744 __isl_take isl_pw_aff
*pwaff2
)
2746 return isl_pw_aff_union_opt_cmp(pwaff1
, pwaff2
, &isl_aff_le_set
);
2749 __isl_give isl_pw_aff
*isl_pw_aff_union_min(__isl_take isl_pw_aff
*pwaff1
,
2750 __isl_take isl_pw_aff
*pwaff2
)
2752 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
,
2756 __isl_give isl_pw_aff
*isl_pw_aff_union_opt(__isl_take isl_pw_aff
*pwaff1
,
2757 __isl_take isl_pw_aff
*pwaff2
, int max
)
2760 return isl_pw_aff_union_max(pwaff1
, pwaff2
);
2762 return isl_pw_aff_union_min(pwaff1
, pwaff2
);
2765 /* Construct a map with as domain the domain of pwaff and
2766 * one-dimensional range corresponding to the affine expressions.
2768 static __isl_give isl_map
*map_from_pw_aff(__isl_take isl_pw_aff
*pwaff
)
2777 dim
= isl_pw_aff_get_space(pwaff
);
2778 map
= isl_map_empty(dim
);
2780 for (i
= 0; i
< pwaff
->n
; ++i
) {
2781 isl_basic_map
*bmap
;
2784 bmap
= isl_basic_map_from_aff(isl_aff_copy(pwaff
->p
[i
].aff
));
2785 map_i
= isl_map_from_basic_map(bmap
);
2786 map_i
= isl_map_intersect_domain(map_i
,
2787 isl_set_copy(pwaff
->p
[i
].set
));
2788 map
= isl_map_union_disjoint(map
, map_i
);
2791 isl_pw_aff_free(pwaff
);
2796 /* Construct a map with as domain the domain of pwaff and
2797 * one-dimensional range corresponding to the affine expressions.
2799 __isl_give isl_map
*isl_map_from_pw_aff(__isl_take isl_pw_aff
*pwaff
)
2803 if (isl_space_is_set(pwaff
->dim
))
2804 isl_die(isl_pw_aff_get_ctx(pwaff
), isl_error_invalid
,
2805 "space of input is not a map", goto error
);
2806 return map_from_pw_aff(pwaff
);
2808 isl_pw_aff_free(pwaff
);
2812 /* Construct a one-dimensional set with as parameter domain
2813 * the domain of pwaff and the single set dimension
2814 * corresponding to the affine expressions.
2816 __isl_give isl_set
*isl_set_from_pw_aff(__isl_take isl_pw_aff
*pwaff
)
2820 if (!isl_space_is_set(pwaff
->dim
))
2821 isl_die(isl_pw_aff_get_ctx(pwaff
), isl_error_invalid
,
2822 "space of input is not a set", goto error
);
2823 return map_from_pw_aff(pwaff
);
2825 isl_pw_aff_free(pwaff
);
2829 /* Return a set containing those elements in the domain
2830 * of "pwaff" where it satisfies "fn" (if complement is 0) or
2831 * does not satisfy "fn" (if complement is 1).
2833 * The pieces with a NaN never belong to the result since
2834 * NaN does not satisfy any property.
2836 static __isl_give isl_set
*pw_aff_locus(__isl_take isl_pw_aff
*pwaff
,
2837 __isl_give isl_basic_set
*(*fn
)(__isl_take isl_aff
*aff
, int rational
),
2846 set
= isl_set_empty(isl_pw_aff_get_domain_space(pwaff
));
2848 for (i
= 0; i
< pwaff
->n
; ++i
) {
2849 isl_basic_set
*bset
;
2850 isl_set
*set_i
, *locus
;
2853 if (isl_aff_is_nan(pwaff
->p
[i
].aff
))
2856 rational
= isl_set_has_rational(pwaff
->p
[i
].set
);
2857 bset
= fn(isl_aff_copy(pwaff
->p
[i
].aff
), rational
);
2858 locus
= isl_set_from_basic_set(bset
);
2859 set_i
= isl_set_copy(pwaff
->p
[i
].set
);
2861 set_i
= isl_set_subtract(set_i
, locus
);
2863 set_i
= isl_set_intersect(set_i
, locus
);
2864 set
= isl_set_union_disjoint(set
, set_i
);
2867 isl_pw_aff_free(pwaff
);
2872 /* Return a set containing those elements in the domain
2873 * of "pa" where it is positive.
2875 __isl_give isl_set
*isl_pw_aff_pos_set(__isl_take isl_pw_aff
*pa
)
2877 return pw_aff_locus(pa
, &aff_pos_basic_set
, 0);
2880 /* Return a set containing those elements in the domain
2881 * of pwaff where it is non-negative.
2883 __isl_give isl_set
*isl_pw_aff_nonneg_set(__isl_take isl_pw_aff
*pwaff
)
2885 return pw_aff_locus(pwaff
, &aff_nonneg_basic_set
, 0);
2888 /* Return a set containing those elements in the domain
2889 * of pwaff where it is zero.
2891 __isl_give isl_set
*isl_pw_aff_zero_set(__isl_take isl_pw_aff
*pwaff
)
2893 return pw_aff_locus(pwaff
, &aff_zero_basic_set
, 0);
2896 /* Return a set containing those elements in the domain
2897 * of pwaff where it is not zero.
2899 __isl_give isl_set
*isl_pw_aff_non_zero_set(__isl_take isl_pw_aff
*pwaff
)
2901 return pw_aff_locus(pwaff
, &aff_zero_basic_set
, 1);
2904 /* Return a set containing those elements in the shared domain
2905 * of pwaff1 and pwaff2 where pwaff1 is greater than (or equal) to pwaff2.
2907 * We compute the difference on the shared domain and then construct
2908 * the set of values where this difference is non-negative.
2909 * If strict is set, we first subtract 1 from the difference.
2910 * If equal is set, we only return the elements where pwaff1 and pwaff2
2913 static __isl_give isl_set
*pw_aff_gte_set(__isl_take isl_pw_aff
*pwaff1
,
2914 __isl_take isl_pw_aff
*pwaff2
, int strict
, int equal
)
2916 isl_set
*set1
, *set2
;
2918 set1
= isl_pw_aff_domain(isl_pw_aff_copy(pwaff1
));
2919 set2
= isl_pw_aff_domain(isl_pw_aff_copy(pwaff2
));
2920 set1
= isl_set_intersect(set1
, set2
);
2921 pwaff1
= isl_pw_aff_intersect_domain(pwaff1
, isl_set_copy(set1
));
2922 pwaff2
= isl_pw_aff_intersect_domain(pwaff2
, isl_set_copy(set1
));
2923 pwaff1
= isl_pw_aff_add(pwaff1
, isl_pw_aff_neg(pwaff2
));
2926 isl_space
*dim
= isl_set_get_space(set1
);
2928 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(dim
));
2929 aff
= isl_aff_add_constant_si(aff
, -1);
2930 pwaff1
= isl_pw_aff_add(pwaff1
, isl_pw_aff_alloc(set1
, aff
));
2935 return isl_pw_aff_zero_set(pwaff1
);
2936 return isl_pw_aff_nonneg_set(pwaff1
);
2939 /* Return a set containing those elements in the shared domain
2940 * of pwaff1 and pwaff2 where pwaff1 is equal to pwaff2.
2942 static __isl_give isl_set
*pw_aff_eq_set(__isl_take isl_pw_aff
*pwaff1
,
2943 __isl_take isl_pw_aff
*pwaff2
)
2945 return pw_aff_gte_set(pwaff1
, pwaff2
, 0, 1);
2948 __isl_give isl_set
*isl_pw_aff_eq_set(__isl_take isl_pw_aff
*pwaff1
,
2949 __isl_take isl_pw_aff
*pwaff2
)
2951 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_eq_set
);
2954 /* Return a set containing those elements in the shared domain
2955 * of pwaff1 and pwaff2 where pwaff1 is greater than or equal to pwaff2.
2957 static __isl_give isl_set
*pw_aff_ge_set(__isl_take isl_pw_aff
*pwaff1
,
2958 __isl_take isl_pw_aff
*pwaff2
)
2960 return pw_aff_gte_set(pwaff1
, pwaff2
, 0, 0);
2963 __isl_give isl_set
*isl_pw_aff_ge_set(__isl_take isl_pw_aff
*pwaff1
,
2964 __isl_take isl_pw_aff
*pwaff2
)
2966 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_ge_set
);
2969 /* Return a set containing those elements in the shared domain
2970 * of pwaff1 and pwaff2 where pwaff1 is strictly greater than pwaff2.
2972 static __isl_give isl_set
*pw_aff_gt_set(__isl_take isl_pw_aff
*pwaff1
,
2973 __isl_take isl_pw_aff
*pwaff2
)
2975 return pw_aff_gte_set(pwaff1
, pwaff2
, 1, 0);
2978 __isl_give isl_set
*isl_pw_aff_gt_set(__isl_take isl_pw_aff
*pwaff1
,
2979 __isl_take isl_pw_aff
*pwaff2
)
2981 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_gt_set
);
2984 __isl_give isl_set
*isl_pw_aff_le_set(__isl_take isl_pw_aff
*pwaff1
,
2985 __isl_take isl_pw_aff
*pwaff2
)
2987 return isl_pw_aff_ge_set(pwaff2
, pwaff1
);
2990 __isl_give isl_set
*isl_pw_aff_lt_set(__isl_take isl_pw_aff
*pwaff1
,
2991 __isl_take isl_pw_aff
*pwaff2
)
2993 return isl_pw_aff_gt_set(pwaff2
, pwaff1
);
2996 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
2997 * where the function values are ordered in the same way as "order",
2998 * which returns a set in the shared domain of its two arguments.
2999 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3001 * Let "pa1" and "pa2" be defined on domains A and B respectively.
3002 * We first pull back the two functions such that they are defined on
3003 * the domain [A -> B]. Then we apply "order", resulting in a set
3004 * in the space [A -> B]. Finally, we unwrap this set to obtain
3005 * a map in the space A -> B.
3007 static __isl_give isl_map
*isl_pw_aff_order_map_aligned(
3008 __isl_take isl_pw_aff
*pa1
, __isl_take isl_pw_aff
*pa2
,
3009 __isl_give isl_set
*(*order
)(__isl_take isl_pw_aff
*pa1
,
3010 __isl_take isl_pw_aff
*pa2
))
3012 isl_space
*space1
, *space2
;
3016 space1
= isl_space_domain(isl_pw_aff_get_space(pa1
));
3017 space2
= isl_space_domain(isl_pw_aff_get_space(pa2
));
3018 space1
= isl_space_map_from_domain_and_range(space1
, space2
);
3019 ma
= isl_multi_aff_domain_map(isl_space_copy(space1
));
3020 pa1
= isl_pw_aff_pullback_multi_aff(pa1
, ma
);
3021 ma
= isl_multi_aff_range_map(space1
);
3022 pa2
= isl_pw_aff_pullback_multi_aff(pa2
, ma
);
3023 set
= order(pa1
, pa2
);
3025 return isl_set_unwrap(set
);
3028 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3029 * where the function values are equal.
3030 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3032 static __isl_give isl_map
*isl_pw_aff_eq_map_aligned(__isl_take isl_pw_aff
*pa1
,
3033 __isl_take isl_pw_aff
*pa2
)
3035 return isl_pw_aff_order_map_aligned(pa1
, pa2
, &isl_pw_aff_eq_set
);
3038 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3039 * where the function values are equal.
3041 __isl_give isl_map
*isl_pw_aff_eq_map(__isl_take isl_pw_aff
*pa1
,
3042 __isl_take isl_pw_aff
*pa2
)
3044 return align_params_pw_pw_map_and(pa1
, pa2
, &isl_pw_aff_eq_map_aligned
);
3047 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3048 * where the function value of "pa1" is less than the function value of "pa2".
3049 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3051 static __isl_give isl_map
*isl_pw_aff_lt_map_aligned(__isl_take isl_pw_aff
*pa1
,
3052 __isl_take isl_pw_aff
*pa2
)
3054 return isl_pw_aff_order_map_aligned(pa1
, pa2
, &isl_pw_aff_lt_set
);
3057 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3058 * where the function value of "pa1" is less than the function value of "pa2".
3060 __isl_give isl_map
*isl_pw_aff_lt_map(__isl_take isl_pw_aff
*pa1
,
3061 __isl_take isl_pw_aff
*pa2
)
3063 return align_params_pw_pw_map_and(pa1
, pa2
, &isl_pw_aff_lt_map_aligned
);
3066 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3067 * where the function value of "pa1" is greater than the function value
3069 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3071 static __isl_give isl_map
*isl_pw_aff_gt_map_aligned(__isl_take isl_pw_aff
*pa1
,
3072 __isl_take isl_pw_aff
*pa2
)
3074 return isl_pw_aff_order_map_aligned(pa1
, pa2
, &isl_pw_aff_gt_set
);
3077 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3078 * where the function value of "pa1" is greater than the function value
3081 __isl_give isl_map
*isl_pw_aff_gt_map(__isl_take isl_pw_aff
*pa1
,
3082 __isl_take isl_pw_aff
*pa2
)
3084 return align_params_pw_pw_map_and(pa1
, pa2
, &isl_pw_aff_gt_map_aligned
);
3087 /* Return a set containing those elements in the shared domain
3088 * of the elements of list1 and list2 where each element in list1
3089 * has the relation specified by "fn" with each element in list2.
3091 static __isl_give isl_set
*pw_aff_list_set(__isl_take isl_pw_aff_list
*list1
,
3092 __isl_take isl_pw_aff_list
*list2
,
3093 __isl_give isl_set
*(*fn
)(__isl_take isl_pw_aff
*pwaff1
,
3094 __isl_take isl_pw_aff
*pwaff2
))
3100 if (!list1
|| !list2
)
3103 ctx
= isl_pw_aff_list_get_ctx(list1
);
3104 if (list1
->n
< 1 || list2
->n
< 1)
3105 isl_die(ctx
, isl_error_invalid
,
3106 "list should contain at least one element", goto error
);
3108 set
= isl_set_universe(isl_pw_aff_get_domain_space(list1
->p
[0]));
3109 for (i
= 0; i
< list1
->n
; ++i
)
3110 for (j
= 0; j
< list2
->n
; ++j
) {
3113 set_ij
= fn(isl_pw_aff_copy(list1
->p
[i
]),
3114 isl_pw_aff_copy(list2
->p
[j
]));
3115 set
= isl_set_intersect(set
, set_ij
);
3118 isl_pw_aff_list_free(list1
);
3119 isl_pw_aff_list_free(list2
);
3122 isl_pw_aff_list_free(list1
);
3123 isl_pw_aff_list_free(list2
);
3127 /* Return a set containing those elements in the shared domain
3128 * of the elements of list1 and list2 where each element in list1
3129 * is equal to each element in list2.
3131 __isl_give isl_set
*isl_pw_aff_list_eq_set(__isl_take isl_pw_aff_list
*list1
,
3132 __isl_take isl_pw_aff_list
*list2
)
3134 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_eq_set
);
3137 __isl_give isl_set
*isl_pw_aff_list_ne_set(__isl_take isl_pw_aff_list
*list1
,
3138 __isl_take isl_pw_aff_list
*list2
)
3140 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_ne_set
);
3143 /* Return a set containing those elements in the shared domain
3144 * of the elements of list1 and list2 where each element in list1
3145 * is less than or equal to each element in list2.
3147 __isl_give isl_set
*isl_pw_aff_list_le_set(__isl_take isl_pw_aff_list
*list1
,
3148 __isl_take isl_pw_aff_list
*list2
)
3150 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_le_set
);
3153 __isl_give isl_set
*isl_pw_aff_list_lt_set(__isl_take isl_pw_aff_list
*list1
,
3154 __isl_take isl_pw_aff_list
*list2
)
3156 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_lt_set
);
3159 __isl_give isl_set
*isl_pw_aff_list_ge_set(__isl_take isl_pw_aff_list
*list1
,
3160 __isl_take isl_pw_aff_list
*list2
)
3162 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_ge_set
);
3165 __isl_give isl_set
*isl_pw_aff_list_gt_set(__isl_take isl_pw_aff_list
*list1
,
3166 __isl_take isl_pw_aff_list
*list2
)
3168 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_gt_set
);
3172 /* Return a set containing those elements in the shared domain
3173 * of pwaff1 and pwaff2 where pwaff1 is not equal to pwaff2.
3175 static __isl_give isl_set
*pw_aff_ne_set(__isl_take isl_pw_aff
*pwaff1
,
3176 __isl_take isl_pw_aff
*pwaff2
)
3178 isl_set
*set_lt
, *set_gt
;
3180 set_lt
= isl_pw_aff_lt_set(isl_pw_aff_copy(pwaff1
),
3181 isl_pw_aff_copy(pwaff2
));
3182 set_gt
= isl_pw_aff_gt_set(pwaff1
, pwaff2
);
3183 return isl_set_union_disjoint(set_lt
, set_gt
);
3186 __isl_give isl_set
*isl_pw_aff_ne_set(__isl_take isl_pw_aff
*pwaff1
,
3187 __isl_take isl_pw_aff
*pwaff2
)
3189 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_ne_set
);
3192 __isl_give isl_pw_aff
*isl_pw_aff_scale_down(__isl_take isl_pw_aff
*pwaff
,
3197 if (isl_int_is_one(v
))
3199 if (!isl_int_is_pos(v
))
3200 isl_die(isl_pw_aff_get_ctx(pwaff
), isl_error_invalid
,
3201 "factor needs to be positive",
3202 return isl_pw_aff_free(pwaff
));
3203 pwaff
= isl_pw_aff_cow(pwaff
);
3209 for (i
= 0; i
< pwaff
->n
; ++i
) {
3210 pwaff
->p
[i
].aff
= isl_aff_scale_down(pwaff
->p
[i
].aff
, v
);
3211 if (!pwaff
->p
[i
].aff
)
3212 return isl_pw_aff_free(pwaff
);
3218 __isl_give isl_pw_aff
*isl_pw_aff_floor(__isl_take isl_pw_aff
*pwaff
)
3222 pwaff
= isl_pw_aff_cow(pwaff
);
3228 for (i
= 0; i
< pwaff
->n
; ++i
) {
3229 pwaff
->p
[i
].aff
= isl_aff_floor(pwaff
->p
[i
].aff
);
3230 if (!pwaff
->p
[i
].aff
)
3231 return isl_pw_aff_free(pwaff
);
3237 __isl_give isl_pw_aff
*isl_pw_aff_ceil(__isl_take isl_pw_aff
*pwaff
)
3241 pwaff
= isl_pw_aff_cow(pwaff
);
3247 for (i
= 0; i
< pwaff
->n
; ++i
) {
3248 pwaff
->p
[i
].aff
= isl_aff_ceil(pwaff
->p
[i
].aff
);
3249 if (!pwaff
->p
[i
].aff
)
3250 return isl_pw_aff_free(pwaff
);
3256 /* Assuming that "cond1" and "cond2" are disjoint,
3257 * return an affine expression that is equal to pwaff1 on cond1
3258 * and to pwaff2 on cond2.
3260 static __isl_give isl_pw_aff
*isl_pw_aff_select(
3261 __isl_take isl_set
*cond1
, __isl_take isl_pw_aff
*pwaff1
,
3262 __isl_take isl_set
*cond2
, __isl_take isl_pw_aff
*pwaff2
)
3264 pwaff1
= isl_pw_aff_intersect_domain(pwaff1
, cond1
);
3265 pwaff2
= isl_pw_aff_intersect_domain(pwaff2
, cond2
);
3267 return isl_pw_aff_add_disjoint(pwaff1
, pwaff2
);
3270 /* Return an affine expression that is equal to pwaff_true for elements
3271 * where "cond" is non-zero and to pwaff_false for elements where "cond"
3273 * That is, return cond ? pwaff_true : pwaff_false;
3275 * If "cond" involves and NaN, then we conservatively return a NaN
3276 * on its entire domain. In principle, we could consider the pieces
3277 * where it is NaN separately from those where it is not.
3279 * If "pwaff_true" and "pwaff_false" are obviously equal to each other,
3280 * then only use the domain of "cond" to restrict the domain.
3282 __isl_give isl_pw_aff
*isl_pw_aff_cond(__isl_take isl_pw_aff
*cond
,
3283 __isl_take isl_pw_aff
*pwaff_true
, __isl_take isl_pw_aff
*pwaff_false
)
3285 isl_set
*cond_true
, *cond_false
;
3290 if (isl_pw_aff_involves_nan(cond
)) {
3291 isl_space
*space
= isl_pw_aff_get_domain_space(cond
);
3292 isl_local_space
*ls
= isl_local_space_from_space(space
);
3293 isl_pw_aff_free(cond
);
3294 isl_pw_aff_free(pwaff_true
);
3295 isl_pw_aff_free(pwaff_false
);
3296 return isl_pw_aff_nan_on_domain(ls
);
3299 pwaff_true
= isl_pw_aff_align_params(pwaff_true
,
3300 isl_pw_aff_get_space(pwaff_false
));
3301 pwaff_false
= isl_pw_aff_align_params(pwaff_false
,
3302 isl_pw_aff_get_space(pwaff_true
));
3303 equal
= isl_pw_aff_plain_is_equal(pwaff_true
, pwaff_false
);
3309 dom
= isl_set_coalesce(isl_pw_aff_domain(cond
));
3310 isl_pw_aff_free(pwaff_false
);
3311 return isl_pw_aff_intersect_domain(pwaff_true
, dom
);
3314 cond_true
= isl_pw_aff_non_zero_set(isl_pw_aff_copy(cond
));
3315 cond_false
= isl_pw_aff_zero_set(cond
);
3316 return isl_pw_aff_select(cond_true
, pwaff_true
,
3317 cond_false
, pwaff_false
);
3319 isl_pw_aff_free(cond
);
3320 isl_pw_aff_free(pwaff_true
);
3321 isl_pw_aff_free(pwaff_false
);
3325 isl_bool
isl_aff_is_cst(__isl_keep isl_aff
*aff
)
3328 return isl_bool_error
;
3330 return isl_seq_first_non_zero(aff
->v
->el
+ 2, aff
->v
->size
- 2) == -1;
3333 /* Check whether pwaff is a piecewise constant.
3335 isl_bool
isl_pw_aff_is_cst(__isl_keep isl_pw_aff
*pwaff
)
3340 return isl_bool_error
;
3342 for (i
= 0; i
< pwaff
->n
; ++i
) {
3343 isl_bool is_cst
= isl_aff_is_cst(pwaff
->p
[i
].aff
);
3344 if (is_cst
< 0 || !is_cst
)
3348 return isl_bool_true
;
3351 /* Are all elements of "mpa" piecewise constants?
3353 isl_bool
isl_multi_pw_aff_is_cst(__isl_keep isl_multi_pw_aff
*mpa
)
3358 return isl_bool_error
;
3360 for (i
= 0; i
< mpa
->n
; ++i
) {
3361 isl_bool is_cst
= isl_pw_aff_is_cst(mpa
->u
.p
[i
]);
3362 if (is_cst
< 0 || !is_cst
)
3366 return isl_bool_true
;
3369 /* Return the product of "aff1" and "aff2".
3371 * If either of the two is NaN, then the result is NaN.
3373 * Otherwise, at least one of "aff1" or "aff2" needs to be a constant.
3375 __isl_give isl_aff
*isl_aff_mul(__isl_take isl_aff
*aff1
,
3376 __isl_take isl_aff
*aff2
)
3381 if (isl_aff_is_nan(aff1
)) {
3385 if (isl_aff_is_nan(aff2
)) {
3390 if (!isl_aff_is_cst(aff2
) && isl_aff_is_cst(aff1
))
3391 return isl_aff_mul(aff2
, aff1
);
3393 if (!isl_aff_is_cst(aff2
))
3394 isl_die(isl_aff_get_ctx(aff1
), isl_error_invalid
,
3395 "at least one affine expression should be constant",
3398 aff1
= isl_aff_cow(aff1
);
3402 aff1
= isl_aff_scale(aff1
, aff2
->v
->el
[1]);
3403 aff1
= isl_aff_scale_down(aff1
, aff2
->v
->el
[0]);
3413 /* Divide "aff1" by "aff2", assuming "aff2" is a constant.
3415 * If either of the two is NaN, then the result is NaN.
3417 __isl_give isl_aff
*isl_aff_div(__isl_take isl_aff
*aff1
,
3418 __isl_take isl_aff
*aff2
)
3426 if (isl_aff_is_nan(aff1
)) {
3430 if (isl_aff_is_nan(aff2
)) {
3435 is_cst
= isl_aff_is_cst(aff2
);
3439 isl_die(isl_aff_get_ctx(aff2
), isl_error_invalid
,
3440 "second argument should be a constant", goto error
);
3445 neg
= isl_int_is_neg(aff2
->v
->el
[1]);
3447 isl_int_neg(aff2
->v
->el
[0], aff2
->v
->el
[0]);
3448 isl_int_neg(aff2
->v
->el
[1], aff2
->v
->el
[1]);
3451 aff1
= isl_aff_scale(aff1
, aff2
->v
->el
[0]);
3452 aff1
= isl_aff_scale_down(aff1
, aff2
->v
->el
[1]);
3455 isl_int_neg(aff2
->v
->el
[0], aff2
->v
->el
[0]);
3456 isl_int_neg(aff2
->v
->el
[1], aff2
->v
->el
[1]);
3467 static __isl_give isl_pw_aff
*pw_aff_add(__isl_take isl_pw_aff
*pwaff1
,
3468 __isl_take isl_pw_aff
*pwaff2
)
3470 return isl_pw_aff_on_shared_domain(pwaff1
, pwaff2
, &isl_aff_add
);
3473 __isl_give isl_pw_aff
*isl_pw_aff_add(__isl_take isl_pw_aff
*pwaff1
,
3474 __isl_take isl_pw_aff
*pwaff2
)
3476 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
, &pw_aff_add
);
3479 __isl_give isl_pw_aff
*isl_pw_aff_union_add(__isl_take isl_pw_aff
*pwaff1
,
3480 __isl_take isl_pw_aff
*pwaff2
)
3482 return isl_pw_aff_union_add_(pwaff1
, pwaff2
);
3485 static __isl_give isl_pw_aff
*pw_aff_mul(__isl_take isl_pw_aff
*pwaff1
,
3486 __isl_take isl_pw_aff
*pwaff2
)
3488 return isl_pw_aff_on_shared_domain(pwaff1
, pwaff2
, &isl_aff_mul
);
3491 __isl_give isl_pw_aff
*isl_pw_aff_mul(__isl_take isl_pw_aff
*pwaff1
,
3492 __isl_take isl_pw_aff
*pwaff2
)
3494 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
, &pw_aff_mul
);
3497 static __isl_give isl_pw_aff
*pw_aff_div(__isl_take isl_pw_aff
*pa1
,
3498 __isl_take isl_pw_aff
*pa2
)
3500 return isl_pw_aff_on_shared_domain(pa1
, pa2
, &isl_aff_div
);
3503 /* Divide "pa1" by "pa2", assuming "pa2" is a piecewise constant.
3505 __isl_give isl_pw_aff
*isl_pw_aff_div(__isl_take isl_pw_aff
*pa1
,
3506 __isl_take isl_pw_aff
*pa2
)
3510 is_cst
= isl_pw_aff_is_cst(pa2
);
3514 isl_die(isl_pw_aff_get_ctx(pa2
), isl_error_invalid
,
3515 "second argument should be a piecewise constant",
3517 return isl_pw_aff_align_params_pw_pw_and(pa1
, pa2
, &pw_aff_div
);
3519 isl_pw_aff_free(pa1
);
3520 isl_pw_aff_free(pa2
);
3524 /* Compute the quotient of the integer division of "pa1" by "pa2"
3525 * with rounding towards zero.
3526 * "pa2" is assumed to be a piecewise constant.
3528 * In particular, return
3530 * pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2)
3533 __isl_give isl_pw_aff
*isl_pw_aff_tdiv_q(__isl_take isl_pw_aff
*pa1
,
3534 __isl_take isl_pw_aff
*pa2
)
3540 is_cst
= isl_pw_aff_is_cst(pa2
);
3544 isl_die(isl_pw_aff_get_ctx(pa2
), isl_error_invalid
,
3545 "second argument should be a piecewise constant",
3548 pa1
= isl_pw_aff_div(pa1
, pa2
);
3550 cond
= isl_pw_aff_nonneg_set(isl_pw_aff_copy(pa1
));
3551 f
= isl_pw_aff_floor(isl_pw_aff_copy(pa1
));
3552 c
= isl_pw_aff_ceil(pa1
);
3553 return isl_pw_aff_cond(isl_set_indicator_function(cond
), f
, c
);
3555 isl_pw_aff_free(pa1
);
3556 isl_pw_aff_free(pa2
);
3560 /* Compute the remainder of the integer division of "pa1" by "pa2"
3561 * with rounding towards zero.
3562 * "pa2" is assumed to be a piecewise constant.
3564 * In particular, return
3566 * pa1 - pa2 * (pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2))
3569 __isl_give isl_pw_aff
*isl_pw_aff_tdiv_r(__isl_take isl_pw_aff
*pa1
,
3570 __isl_take isl_pw_aff
*pa2
)
3575 is_cst
= isl_pw_aff_is_cst(pa2
);
3579 isl_die(isl_pw_aff_get_ctx(pa2
), isl_error_invalid
,
3580 "second argument should be a piecewise constant",
3582 res
= isl_pw_aff_tdiv_q(isl_pw_aff_copy(pa1
), isl_pw_aff_copy(pa2
));
3583 res
= isl_pw_aff_mul(pa2
, res
);
3584 res
= isl_pw_aff_sub(pa1
, res
);
3587 isl_pw_aff_free(pa1
);
3588 isl_pw_aff_free(pa2
);
3592 /* Does either of "pa1" or "pa2" involve any NaN2?
3594 static isl_bool
either_involves_nan(__isl_keep isl_pw_aff
*pa1
,
3595 __isl_keep isl_pw_aff
*pa2
)
3599 has_nan
= isl_pw_aff_involves_nan(pa1
);
3600 if (has_nan
< 0 || has_nan
)
3602 return isl_pw_aff_involves_nan(pa2
);
3605 /* Replace "pa1" and "pa2" (at least one of which involves a NaN)
3606 * by a NaN on their shared domain.
3608 * In principle, the result could be refined to only being NaN
3609 * on the parts of this domain where at least one of "pa1" or "pa2" is NaN.
3611 static __isl_give isl_pw_aff
*replace_by_nan(__isl_take isl_pw_aff
*pa1
,
3612 __isl_take isl_pw_aff
*pa2
)
3614 isl_local_space
*ls
;
3618 dom
= isl_set_intersect(isl_pw_aff_domain(pa1
), isl_pw_aff_domain(pa2
));
3619 ls
= isl_local_space_from_space(isl_set_get_space(dom
));
3620 pa
= isl_pw_aff_nan_on_domain(ls
);
3621 pa
= isl_pw_aff_intersect_domain(pa
, dom
);
3626 static __isl_give isl_pw_aff
*pw_aff_min(__isl_take isl_pw_aff
*pwaff1
,
3627 __isl_take isl_pw_aff
*pwaff2
)
3632 dom
= isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1
)),
3633 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2
)));
3634 le
= isl_pw_aff_le_set(isl_pw_aff_copy(pwaff1
),
3635 isl_pw_aff_copy(pwaff2
));
3636 dom
= isl_set_subtract(dom
, isl_set_copy(le
));
3637 return isl_pw_aff_select(le
, pwaff1
, dom
, pwaff2
);
3640 static __isl_give isl_pw_aff
*pw_aff_max(__isl_take isl_pw_aff
*pwaff1
,
3641 __isl_take isl_pw_aff
*pwaff2
)
3646 dom
= isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1
)),
3647 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2
)));
3648 ge
= isl_pw_aff_ge_set(isl_pw_aff_copy(pwaff1
),
3649 isl_pw_aff_copy(pwaff2
));
3650 dom
= isl_set_subtract(dom
, isl_set_copy(ge
));
3651 return isl_pw_aff_select(ge
, pwaff1
, dom
, pwaff2
);
3654 /* Return an expression for the minimum (if "max" is not set) or
3655 * the maximum (if "max" is set) of "pa1" and "pa2".
3656 * If either expression involves any NaN, then return a NaN
3657 * on the shared domain as result.
3659 static __isl_give isl_pw_aff
*pw_aff_min_max(__isl_take isl_pw_aff
*pa1
,
3660 __isl_take isl_pw_aff
*pa2
, int max
)
3664 has_nan
= either_involves_nan(pa1
, pa2
);
3666 pa1
= isl_pw_aff_free(pa1
);
3668 return replace_by_nan(pa1
, pa2
);
3671 return isl_pw_aff_align_params_pw_pw_and(pa1
, pa2
, &pw_aff_max
);
3673 return isl_pw_aff_align_params_pw_pw_and(pa1
, pa2
, &pw_aff_min
);
3676 /* Return an expression for the minimum of "pwaff1" and "pwaff2".
3678 __isl_give isl_pw_aff
*isl_pw_aff_min(__isl_take isl_pw_aff
*pwaff1
,
3679 __isl_take isl_pw_aff
*pwaff2
)
3681 return pw_aff_min_max(pwaff1
, pwaff2
, 0);
3684 /* Return an expression for the maximum of "pwaff1" and "pwaff2".
3686 __isl_give isl_pw_aff
*isl_pw_aff_max(__isl_take isl_pw_aff
*pwaff1
,
3687 __isl_take isl_pw_aff
*pwaff2
)
3689 return pw_aff_min_max(pwaff1
, pwaff2
, 1);
3692 static __isl_give isl_pw_aff
*pw_aff_list_reduce(
3693 __isl_take isl_pw_aff_list
*list
,
3694 __isl_give isl_pw_aff
*(*fn
)(__isl_take isl_pw_aff
*pwaff1
,
3695 __isl_take isl_pw_aff
*pwaff2
))
3704 ctx
= isl_pw_aff_list_get_ctx(list
);
3706 isl_die(ctx
, isl_error_invalid
,
3707 "list should contain at least one element", goto error
);
3709 res
= isl_pw_aff_copy(list
->p
[0]);
3710 for (i
= 1; i
< list
->n
; ++i
)
3711 res
= fn(res
, isl_pw_aff_copy(list
->p
[i
]));
3713 isl_pw_aff_list_free(list
);
3716 isl_pw_aff_list_free(list
);
3720 /* Return an isl_pw_aff that maps each element in the intersection of the
3721 * domains of the elements of list to the minimal corresponding affine
3724 __isl_give isl_pw_aff
*isl_pw_aff_list_min(__isl_take isl_pw_aff_list
*list
)
3726 return pw_aff_list_reduce(list
, &isl_pw_aff_min
);
3729 /* Return an isl_pw_aff that maps each element in the intersection of the
3730 * domains of the elements of list to the maximal corresponding affine
3733 __isl_give isl_pw_aff
*isl_pw_aff_list_max(__isl_take isl_pw_aff_list
*list
)
3735 return pw_aff_list_reduce(list
, &isl_pw_aff_max
);
3738 /* Mark the domains of "pwaff" as rational.
3740 __isl_give isl_pw_aff
*isl_pw_aff_set_rational(__isl_take isl_pw_aff
*pwaff
)
3744 pwaff
= isl_pw_aff_cow(pwaff
);
3750 for (i
= 0; i
< pwaff
->n
; ++i
) {
3751 pwaff
->p
[i
].set
= isl_set_set_rational(pwaff
->p
[i
].set
);
3752 if (!pwaff
->p
[i
].set
)
3753 return isl_pw_aff_free(pwaff
);
3759 /* Mark the domains of the elements of "list" as rational.
3761 __isl_give isl_pw_aff_list
*isl_pw_aff_list_set_rational(
3762 __isl_take isl_pw_aff_list
*list
)
3772 for (i
= 0; i
< n
; ++i
) {
3775 pa
= isl_pw_aff_list_get_pw_aff(list
, i
);
3776 pa
= isl_pw_aff_set_rational(pa
);
3777 list
= isl_pw_aff_list_set_pw_aff(list
, i
, pa
);
3783 /* Do the parameters of "aff" match those of "space"?
3785 isl_bool
isl_aff_matching_params(__isl_keep isl_aff
*aff
,
3786 __isl_keep isl_space
*space
)
3788 isl_space
*aff_space
;
3792 return isl_bool_error
;
3794 aff_space
= isl_aff_get_domain_space(aff
);
3796 match
= isl_space_has_equal_params(space
, aff_space
);
3798 isl_space_free(aff_space
);
3802 /* Check that the domain space of "aff" matches "space".
3804 isl_stat
isl_aff_check_match_domain_space(__isl_keep isl_aff
*aff
,
3805 __isl_keep isl_space
*space
)
3807 isl_space
*aff_space
;
3811 return isl_stat_error
;
3813 aff_space
= isl_aff_get_domain_space(aff
);
3815 match
= isl_space_has_equal_params(space
, aff_space
);
3819 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
3820 "parameters don't match", goto error
);
3821 match
= isl_space_tuple_is_equal(space
, isl_dim_in
,
3822 aff_space
, isl_dim_set
);
3826 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
3827 "domains don't match", goto error
);
3828 isl_space_free(aff_space
);
3831 isl_space_free(aff_space
);
3832 return isl_stat_error
;
3841 #include <isl_multi_no_explicit_domain.c>
3842 #include <isl_multi_templ.c>
3843 #include <isl_multi_apply_set.c>
3844 #include <isl_multi_cmp.c>
3845 #include <isl_multi_dims.c>
3846 #include <isl_multi_floor.c>
3847 #include <isl_multi_gist.c>
3851 /* Construct an isl_multi_aff living in "space" that corresponds
3852 * to the affine transformation matrix "mat".
3854 __isl_give isl_multi_aff
*isl_multi_aff_from_aff_mat(
3855 __isl_take isl_space
*space
, __isl_take isl_mat
*mat
)
3858 isl_local_space
*ls
= NULL
;
3859 isl_multi_aff
*ma
= NULL
;
3860 int n_row
, n_col
, n_out
, total
;
3866 ctx
= isl_mat_get_ctx(mat
);
3868 n_row
= isl_mat_rows(mat
);
3869 n_col
= isl_mat_cols(mat
);
3871 isl_die(ctx
, isl_error_invalid
,
3872 "insufficient number of rows", goto error
);
3874 isl_die(ctx
, isl_error_invalid
,
3875 "insufficient number of columns", goto error
);
3876 n_out
= isl_space_dim(space
, isl_dim_out
);
3877 total
= isl_space_dim(space
, isl_dim_all
);
3878 if (1 + n_out
!= n_row
|| 2 + total
!= n_row
+ n_col
)
3879 isl_die(ctx
, isl_error_invalid
,
3880 "dimension mismatch", goto error
);
3882 ma
= isl_multi_aff_zero(isl_space_copy(space
));
3883 ls
= isl_local_space_from_space(isl_space_domain(space
));
3885 for (i
= 0; i
< n_row
- 1; ++i
) {
3889 v
= isl_vec_alloc(ctx
, 1 + n_col
);
3892 isl_int_set(v
->el
[0], mat
->row
[0][0]);
3893 isl_seq_cpy(v
->el
+ 1, mat
->row
[1 + i
], n_col
);
3894 v
= isl_vec_normalize(v
);
3895 aff
= isl_aff_alloc_vec(isl_local_space_copy(ls
), v
);
3896 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
3899 isl_local_space_free(ls
);
3903 isl_local_space_free(ls
);
3905 isl_multi_aff_free(ma
);
3909 /* Remove any internal structure of the domain of "ma".
3910 * If there is any such internal structure in the input,
3911 * then the name of the corresponding space is also removed.
3913 __isl_give isl_multi_aff
*isl_multi_aff_flatten_domain(
3914 __isl_take isl_multi_aff
*ma
)
3921 if (!ma
->space
->nested
[0])
3924 space
= isl_multi_aff_get_space(ma
);
3925 space
= isl_space_flatten_domain(space
);
3926 ma
= isl_multi_aff_reset_space(ma
, space
);
3931 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
3932 * of the space to its domain.
3934 __isl_give isl_multi_aff
*isl_multi_aff_domain_map(__isl_take isl_space
*space
)
3937 isl_local_space
*ls
;
3942 if (!isl_space_is_map(space
))
3943 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
3944 "not a map space", goto error
);
3946 n_in
= isl_space_dim(space
, isl_dim_in
);
3947 space
= isl_space_domain_map(space
);
3949 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
3951 isl_space_free(space
);
3955 space
= isl_space_domain(space
);
3956 ls
= isl_local_space_from_space(space
);
3957 for (i
= 0; i
< n_in
; ++i
) {
3960 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
3962 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
3964 isl_local_space_free(ls
);
3967 isl_space_free(space
);
3971 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
3972 * of the space to its range.
3974 __isl_give isl_multi_aff
*isl_multi_aff_range_map(__isl_take isl_space
*space
)
3977 isl_local_space
*ls
;
3982 if (!isl_space_is_map(space
))
3983 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
3984 "not a map space", goto error
);
3986 n_in
= isl_space_dim(space
, isl_dim_in
);
3987 n_out
= isl_space_dim(space
, isl_dim_out
);
3988 space
= isl_space_range_map(space
);
3990 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
3992 isl_space_free(space
);
3996 space
= isl_space_domain(space
);
3997 ls
= isl_local_space_from_space(space
);
3998 for (i
= 0; i
< n_out
; ++i
) {
4001 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
4002 isl_dim_set
, n_in
+ i
);
4003 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
4005 isl_local_space_free(ls
);
4008 isl_space_free(space
);
4012 /* Given a map space, return an isl_pw_multi_aff that maps a wrapped copy
4013 * of the space to its range.
4015 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_range_map(
4016 __isl_take isl_space
*space
)
4018 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_range_map(space
));
4021 /* Given the space of a set and a range of set dimensions,
4022 * construct an isl_multi_aff that projects out those dimensions.
4024 __isl_give isl_multi_aff
*isl_multi_aff_project_out_map(
4025 __isl_take isl_space
*space
, enum isl_dim_type type
,
4026 unsigned first
, unsigned n
)
4029 isl_local_space
*ls
;
4034 if (!isl_space_is_set(space
))
4035 isl_die(isl_space_get_ctx(space
), isl_error_unsupported
,
4036 "expecting set space", goto error
);
4037 if (type
!= isl_dim_set
)
4038 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
4039 "only set dimensions can be projected out", goto error
);
4041 dim
= isl_space_dim(space
, isl_dim_set
);
4042 if (first
+ n
> dim
)
4043 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
4044 "range out of bounds", goto error
);
4046 space
= isl_space_from_domain(space
);
4047 space
= isl_space_add_dims(space
, isl_dim_out
, dim
- n
);
4050 return isl_multi_aff_alloc(space
);
4052 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
4053 space
= isl_space_domain(space
);
4054 ls
= isl_local_space_from_space(space
);
4056 for (i
= 0; i
< first
; ++i
) {
4059 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
4061 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
4064 for (i
= 0; i
< dim
- (first
+ n
); ++i
) {
4067 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
4068 isl_dim_set
, first
+ n
+ i
);
4069 ma
= isl_multi_aff_set_aff(ma
, first
+ i
, aff
);
4072 isl_local_space_free(ls
);
4075 isl_space_free(space
);
4079 /* Given the space of a set and a range of set dimensions,
4080 * construct an isl_pw_multi_aff that projects out those dimensions.
4082 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_project_out_map(
4083 __isl_take isl_space
*space
, enum isl_dim_type type
,
4084 unsigned first
, unsigned n
)
4088 ma
= isl_multi_aff_project_out_map(space
, type
, first
, n
);
4089 return isl_pw_multi_aff_from_multi_aff(ma
);
4092 /* Create an isl_pw_multi_aff with the given isl_multi_aff on a universe
4095 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_multi_aff(
4096 __isl_take isl_multi_aff
*ma
)
4098 isl_set
*dom
= isl_set_universe(isl_multi_aff_get_domain_space(ma
));
4099 return isl_pw_multi_aff_alloc(dom
, ma
);
4102 /* Create a piecewise multi-affine expression in the given space that maps each
4103 * input dimension to the corresponding output dimension.
4105 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_identity(
4106 __isl_take isl_space
*space
)
4108 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_identity(space
));
4111 /* Exploit the equalities in "eq" to simplify the affine expressions.
4113 static __isl_give isl_multi_aff
*isl_multi_aff_substitute_equalities(
4114 __isl_take isl_multi_aff
*maff
, __isl_take isl_basic_set
*eq
)
4118 maff
= isl_multi_aff_cow(maff
);
4122 for (i
= 0; i
< maff
->n
; ++i
) {
4123 maff
->u
.p
[i
] = isl_aff_substitute_equalities(maff
->u
.p
[i
],
4124 isl_basic_set_copy(eq
));
4129 isl_basic_set_free(eq
);
4132 isl_basic_set_free(eq
);
4133 isl_multi_aff_free(maff
);
4137 __isl_give isl_multi_aff
*isl_multi_aff_scale(__isl_take isl_multi_aff
*maff
,
4142 maff
= isl_multi_aff_cow(maff
);
4146 for (i
= 0; i
< maff
->n
; ++i
) {
4147 maff
->u
.p
[i
] = isl_aff_scale(maff
->u
.p
[i
], f
);
4149 return isl_multi_aff_free(maff
);
4155 __isl_give isl_multi_aff
*isl_multi_aff_add_on_domain(__isl_keep isl_set
*dom
,
4156 __isl_take isl_multi_aff
*maff1
, __isl_take isl_multi_aff
*maff2
)
4158 maff1
= isl_multi_aff_add(maff1
, maff2
);
4159 maff1
= isl_multi_aff_gist(maff1
, isl_set_copy(dom
));
4163 int isl_multi_aff_is_empty(__isl_keep isl_multi_aff
*maff
)
4171 /* Return the set of domain elements where "ma1" is lexicographically
4172 * smaller than or equal to "ma2".
4174 __isl_give isl_set
*isl_multi_aff_lex_le_set(__isl_take isl_multi_aff
*ma1
,
4175 __isl_take isl_multi_aff
*ma2
)
4177 return isl_multi_aff_lex_ge_set(ma2
, ma1
);
4180 /* Return the set of domain elements where "ma1" is lexicographically
4181 * smaller than "ma2".
4183 __isl_give isl_set
*isl_multi_aff_lex_lt_set(__isl_take isl_multi_aff
*ma1
,
4184 __isl_take isl_multi_aff
*ma2
)
4186 return isl_multi_aff_lex_gt_set(ma2
, ma1
);
4189 /* Return the set of domain elements where "ma1" and "ma2"
4192 static __isl_give isl_set
*isl_multi_aff_order_set(
4193 __isl_take isl_multi_aff
*ma1
, __isl_take isl_multi_aff
*ma2
,
4194 __isl_give isl_map
*order(__isl_take isl_space
*set_space
))
4197 isl_map
*map1
, *map2
;
4200 map1
= isl_map_from_multi_aff(ma1
);
4201 map2
= isl_map_from_multi_aff(ma2
);
4202 map
= isl_map_range_product(map1
, map2
);
4203 space
= isl_space_range(isl_map_get_space(map
));
4204 space
= isl_space_domain(isl_space_unwrap(space
));
4206 map
= isl_map_intersect_range(map
, isl_map_wrap(ge
));
4208 return isl_map_domain(map
);
4211 /* Return the set of domain elements where "ma1" is lexicographically
4212 * greater than or equal to "ma2".
4214 __isl_give isl_set
*isl_multi_aff_lex_ge_set(__isl_take isl_multi_aff
*ma1
,
4215 __isl_take isl_multi_aff
*ma2
)
4217 return isl_multi_aff_order_set(ma1
, ma2
, &isl_map_lex_ge
);
4220 /* Return the set of domain elements where "ma1" is lexicographically
4221 * greater than "ma2".
4223 __isl_give isl_set
*isl_multi_aff_lex_gt_set(__isl_take isl_multi_aff
*ma1
,
4224 __isl_take isl_multi_aff
*ma2
)
4226 return isl_multi_aff_order_set(ma1
, ma2
, &isl_map_lex_gt
);
4230 #define PW isl_pw_multi_aff
4232 #define EL isl_multi_aff
4234 #define EL_IS_ZERO is_empty
4238 #define IS_ZERO is_empty
4241 #undef DEFAULT_IS_ZERO
4242 #define DEFAULT_IS_ZERO 0
4246 #define NO_INVOLVES_DIMS
4247 #define NO_INSERT_DIMS
4251 #include <isl_pw_templ.c>
4252 #include <isl_pw_union_opt.c>
4257 #define UNION isl_union_pw_multi_aff
4259 #define PART isl_pw_multi_aff
4261 #define PARTS pw_multi_aff
4263 #include <isl_union_multi.c>
4264 #include <isl_union_neg.c>
4266 static __isl_give isl_pw_multi_aff
*pw_multi_aff_union_lexmax(
4267 __isl_take isl_pw_multi_aff
*pma1
,
4268 __isl_take isl_pw_multi_aff
*pma2
)
4270 return isl_pw_multi_aff_union_opt_cmp(pma1
, pma2
,
4271 &isl_multi_aff_lex_ge_set
);
4274 /* Given two piecewise multi affine expressions, return a piecewise
4275 * multi-affine expression defined on the union of the definition domains
4276 * of the inputs that is equal to the lexicographic maximum of the two
4277 * inputs on each cell. If only one of the two inputs is defined on
4278 * a given cell, then it is considered to be the maximum.
4280 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_union_lexmax(
4281 __isl_take isl_pw_multi_aff
*pma1
,
4282 __isl_take isl_pw_multi_aff
*pma2
)
4284 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
4285 &pw_multi_aff_union_lexmax
);
4288 static __isl_give isl_pw_multi_aff
*pw_multi_aff_union_lexmin(
4289 __isl_take isl_pw_multi_aff
*pma1
,
4290 __isl_take isl_pw_multi_aff
*pma2
)
4292 return isl_pw_multi_aff_union_opt_cmp(pma1
, pma2
,
4293 &isl_multi_aff_lex_le_set
);
4296 /* Given two piecewise multi affine expressions, return a piecewise
4297 * multi-affine expression defined on the union of the definition domains
4298 * of the inputs that is equal to the lexicographic minimum of the two
4299 * inputs on each cell. If only one of the two inputs is defined on
4300 * a given cell, then it is considered to be the minimum.
4302 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_union_lexmin(
4303 __isl_take isl_pw_multi_aff
*pma1
,
4304 __isl_take isl_pw_multi_aff
*pma2
)
4306 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
4307 &pw_multi_aff_union_lexmin
);
4310 static __isl_give isl_pw_multi_aff
*pw_multi_aff_add(
4311 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4313 return isl_pw_multi_aff_on_shared_domain(pma1
, pma2
,
4314 &isl_multi_aff_add
);
4317 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_add(
4318 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4320 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
4324 static __isl_give isl_pw_multi_aff
*pw_multi_aff_sub(
4325 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4327 return isl_pw_multi_aff_on_shared_domain(pma1
, pma2
,
4328 &isl_multi_aff_sub
);
4331 /* Subtract "pma2" from "pma1" and return the result.
4333 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_sub(
4334 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4336 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
4340 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_union_add(
4341 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4343 return isl_pw_multi_aff_union_add_(pma1
, pma2
);
4346 /* Compute the sum of "upa1" and "upa2" on the union of their domains,
4347 * with the actual sum on the shared domain and
4348 * the defined expression on the symmetric difference of the domains.
4350 __isl_give isl_union_pw_aff
*isl_union_pw_aff_union_add(
4351 __isl_take isl_union_pw_aff
*upa1
, __isl_take isl_union_pw_aff
*upa2
)
4353 return isl_union_pw_aff_union_add_(upa1
, upa2
);
4356 /* Compute the sum of "upma1" and "upma2" on the union of their domains,
4357 * with the actual sum on the shared domain and
4358 * the defined expression on the symmetric difference of the domains.
4360 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_union_add(
4361 __isl_take isl_union_pw_multi_aff
*upma1
,
4362 __isl_take isl_union_pw_multi_aff
*upma2
)
4364 return isl_union_pw_multi_aff_union_add_(upma1
, upma2
);
4367 /* Given two piecewise multi-affine expressions A -> B and C -> D,
4368 * construct a piecewise multi-affine expression [A -> C] -> [B -> D].
4370 static __isl_give isl_pw_multi_aff
*pw_multi_aff_product(
4371 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4375 isl_pw_multi_aff
*res
;
4380 n
= pma1
->n
* pma2
->n
;
4381 space
= isl_space_product(isl_space_copy(pma1
->dim
),
4382 isl_space_copy(pma2
->dim
));
4383 res
= isl_pw_multi_aff_alloc_size(space
, n
);
4385 for (i
= 0; i
< pma1
->n
; ++i
) {
4386 for (j
= 0; j
< pma2
->n
; ++j
) {
4390 domain
= isl_set_product(isl_set_copy(pma1
->p
[i
].set
),
4391 isl_set_copy(pma2
->p
[j
].set
));
4392 ma
= isl_multi_aff_product(
4393 isl_multi_aff_copy(pma1
->p
[i
].maff
),
4394 isl_multi_aff_copy(pma2
->p
[j
].maff
));
4395 res
= isl_pw_multi_aff_add_piece(res
, domain
, ma
);
4399 isl_pw_multi_aff_free(pma1
);
4400 isl_pw_multi_aff_free(pma2
);
4403 isl_pw_multi_aff_free(pma1
);
4404 isl_pw_multi_aff_free(pma2
);
4408 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_product(
4409 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4411 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
4412 &pw_multi_aff_product
);
4415 /* Construct a map mapping the domain of the piecewise multi-affine expression
4416 * to its range, with each dimension in the range equated to the
4417 * corresponding affine expression on its cell.
4419 * If the domain of "pma" is rational, then so is the constructed "map".
4421 __isl_give isl_map
*isl_map_from_pw_multi_aff(__isl_take isl_pw_multi_aff
*pma
)
4429 map
= isl_map_empty(isl_pw_multi_aff_get_space(pma
));
4431 for (i
= 0; i
< pma
->n
; ++i
) {
4433 isl_multi_aff
*maff
;
4434 isl_basic_map
*bmap
;
4437 rational
= isl_set_is_rational(pma
->p
[i
].set
);
4439 map
= isl_map_free(map
);
4440 maff
= isl_multi_aff_copy(pma
->p
[i
].maff
);
4441 bmap
= isl_basic_map_from_multi_aff2(maff
, rational
);
4442 map_i
= isl_map_from_basic_map(bmap
);
4443 map_i
= isl_map_intersect_domain(map_i
,
4444 isl_set_copy(pma
->p
[i
].set
));
4445 map
= isl_map_union_disjoint(map
, map_i
);
4448 isl_pw_multi_aff_free(pma
);
4452 __isl_give isl_set
*isl_set_from_pw_multi_aff(__isl_take isl_pw_multi_aff
*pma
)
4457 if (!isl_space_is_set(pma
->dim
))
4458 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
4459 "isl_pw_multi_aff cannot be converted into an isl_set",
4462 return isl_map_from_pw_multi_aff(pma
);
4464 isl_pw_multi_aff_free(pma
);
4468 /* Subtract the initial "n" elements in "ma" with coefficients in "c" and
4469 * denominator "denom".
4470 * "denom" is allowed to be negative, in which case the actual denominator
4471 * is -denom and the expressions are added instead.
4473 static __isl_give isl_aff
*subtract_initial(__isl_take isl_aff
*aff
,
4474 __isl_keep isl_multi_aff
*ma
, int n
, isl_int
*c
, isl_int denom
)
4480 first
= isl_seq_first_non_zero(c
, n
);
4484 sign
= isl_int_sgn(denom
);
4486 isl_int_abs(d
, denom
);
4487 for (i
= first
; i
< n
; ++i
) {
4490 if (isl_int_is_zero(c
[i
]))
4492 aff_i
= isl_multi_aff_get_aff(ma
, i
);
4493 aff_i
= isl_aff_scale(aff_i
, c
[i
]);
4494 aff_i
= isl_aff_scale_down(aff_i
, d
);
4496 aff
= isl_aff_sub(aff
, aff_i
);
4498 aff
= isl_aff_add(aff
, aff_i
);
4505 /* Extract an affine expression that expresses the output dimension "pos"
4506 * of "bmap" in terms of the parameters and input dimensions from
4508 * Note that this expression may involve integer divisions defined
4509 * in terms of parameters and input dimensions.
4510 * The equality may also involve references to earlier (but not later)
4511 * output dimensions. These are replaced by the corresponding elements
4514 * If the equality is of the form
4516 * f(i) + h(j) + a x + g(i) = 0,
4518 * with f(i) a linear combinations of the parameters and input dimensions,
4519 * g(i) a linear combination of integer divisions defined in terms of the same
4520 * and h(j) a linear combinations of earlier output dimensions,
4521 * then the affine expression is
4523 * (-f(i) - g(i))/a - h(j)/a
4525 * If the equality is of the form
4527 * f(i) + h(j) - a x + g(i) = 0,
4529 * then the affine expression is
4531 * (f(i) + g(i))/a - h(j)/(-a)
4534 * If "div" refers to an integer division (i.e., it is smaller than
4535 * the number of integer divisions), then the equality constraint
4536 * does involve an integer division (the one at position "div") that
4537 * is defined in terms of output dimensions. However, this integer
4538 * division can be eliminated by exploiting a pair of constraints
4539 * x >= l and x <= l + n, with n smaller than the coefficient of "div"
4540 * in the equality constraint. "ineq" refers to inequality x >= l, i.e.,
4542 * In particular, let
4544 * x = e(i) + m floor(...)
4546 * with e(i) the expression derived above and floor(...) the integer
4547 * division involving output dimensions.
4558 * e(i) + m floor(...) - l = (e(i) + m floor(...) - l) mod m
4559 * = (e(i) - l) mod m
4563 * x - l = (e(i) - l) mod m
4567 * x = ((e(i) - l) mod m) + l
4569 * The variable "shift" below contains the expression -l, which may
4570 * also involve a linear combination of earlier output dimensions.
4572 static __isl_give isl_aff
*extract_aff_from_equality(
4573 __isl_keep isl_basic_map
*bmap
, int pos
, int eq
, int div
, int ineq
,
4574 __isl_keep isl_multi_aff
*ma
)
4577 unsigned n_div
, n_out
;
4579 isl_local_space
*ls
;
4580 isl_aff
*aff
, *shift
;
4583 ctx
= isl_basic_map_get_ctx(bmap
);
4584 ls
= isl_basic_map_get_local_space(bmap
);
4585 ls
= isl_local_space_domain(ls
);
4586 aff
= isl_aff_alloc(isl_local_space_copy(ls
));
4589 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
4590 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4591 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4592 if (isl_int_is_neg(bmap
->eq
[eq
][o_out
+ pos
])) {
4593 isl_seq_cpy(aff
->v
->el
+ 1, bmap
->eq
[eq
], o_out
);
4594 isl_seq_cpy(aff
->v
->el
+ 1 + o_out
,
4595 bmap
->eq
[eq
] + o_out
+ n_out
, n_div
);
4597 isl_seq_neg(aff
->v
->el
+ 1, bmap
->eq
[eq
], o_out
);
4598 isl_seq_neg(aff
->v
->el
+ 1 + o_out
,
4599 bmap
->eq
[eq
] + o_out
+ n_out
, n_div
);
4602 isl_int_set_si(aff
->v
->el
[1 + o_out
+ div
], 0);
4603 isl_int_abs(aff
->v
->el
[0], bmap
->eq
[eq
][o_out
+ pos
]);
4604 aff
= subtract_initial(aff
, ma
, pos
, bmap
->eq
[eq
] + o_out
,
4605 bmap
->eq
[eq
][o_out
+ pos
]);
4607 shift
= isl_aff_alloc(isl_local_space_copy(ls
));
4610 isl_seq_cpy(shift
->v
->el
+ 1, bmap
->ineq
[ineq
], o_out
);
4611 isl_seq_cpy(shift
->v
->el
+ 1 + o_out
,
4612 bmap
->ineq
[ineq
] + o_out
+ n_out
, n_div
);
4613 isl_int_set_si(shift
->v
->el
[0], 1);
4614 shift
= subtract_initial(shift
, ma
, pos
,
4615 bmap
->ineq
[ineq
] + o_out
, ctx
->negone
);
4616 aff
= isl_aff_add(aff
, isl_aff_copy(shift
));
4617 mod
= isl_val_int_from_isl_int(ctx
,
4618 bmap
->eq
[eq
][o_out
+ n_out
+ div
]);
4619 mod
= isl_val_abs(mod
);
4620 aff
= isl_aff_mod_val(aff
, mod
);
4621 aff
= isl_aff_sub(aff
, shift
);
4624 isl_local_space_free(ls
);
4627 isl_local_space_free(ls
);
4632 /* Given a basic map with output dimensions defined
4633 * in terms of the parameters input dimensions and earlier
4634 * output dimensions using an equality (and possibly a pair on inequalities),
4635 * extract an isl_aff that expresses output dimension "pos" in terms
4636 * of the parameters and input dimensions.
4637 * Note that this expression may involve integer divisions defined
4638 * in terms of parameters and input dimensions.
4639 * "ma" contains the expressions corresponding to earlier output dimensions.
4641 * This function shares some similarities with
4642 * isl_basic_map_has_defining_equality and isl_constraint_get_bound.
4644 static __isl_give isl_aff
*extract_isl_aff_from_basic_map(
4645 __isl_keep isl_basic_map
*bmap
, int pos
, __isl_keep isl_multi_aff
*ma
)
4652 eq
= isl_basic_map_output_defining_equality(bmap
, pos
, &div
, &ineq
);
4653 if (eq
>= bmap
->n_eq
)
4654 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
4655 "unable to find suitable equality", return NULL
);
4656 aff
= extract_aff_from_equality(bmap
, pos
, eq
, div
, ineq
, ma
);
4658 aff
= isl_aff_remove_unused_divs(aff
);
4662 /* Given a basic map where each output dimension is defined
4663 * in terms of the parameters and input dimensions using an equality,
4664 * extract an isl_multi_aff that expresses the output dimensions in terms
4665 * of the parameters and input dimensions.
4667 static __isl_give isl_multi_aff
*extract_isl_multi_aff_from_basic_map(
4668 __isl_take isl_basic_map
*bmap
)
4677 ma
= isl_multi_aff_alloc(isl_basic_map_get_space(bmap
));
4678 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4680 for (i
= 0; i
< n_out
; ++i
) {
4683 aff
= extract_isl_aff_from_basic_map(bmap
, i
, ma
);
4684 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
4687 isl_basic_map_free(bmap
);
4692 /* Given a basic set where each set dimension is defined
4693 * in terms of the parameters using an equality,
4694 * extract an isl_multi_aff that expresses the set dimensions in terms
4695 * of the parameters.
4697 __isl_give isl_multi_aff
*isl_multi_aff_from_basic_set_equalities(
4698 __isl_take isl_basic_set
*bset
)
4700 return extract_isl_multi_aff_from_basic_map(bset
);
4703 /* Create an isl_pw_multi_aff that is equivalent to
4704 * isl_map_intersect_domain(isl_map_from_basic_map(bmap), domain).
4705 * The given basic map is such that each output dimension is defined
4706 * in terms of the parameters and input dimensions using an equality.
4708 * Since some applications expect the result of isl_pw_multi_aff_from_map
4709 * to only contain integer affine expressions, we compute the floor
4710 * of the expression before returning.
4712 * Remove all constraints involving local variables without
4713 * an explicit representation (resulting in the removal of those
4714 * local variables) prior to the actual extraction to ensure
4715 * that the local spaces in which the resulting affine expressions
4716 * are created do not contain any unknown local variables.
4717 * Removing such constraints is safe because constraints involving
4718 * unknown local variables are not used to determine whether
4719 * a basic map is obviously single-valued.
4721 static __isl_give isl_pw_multi_aff
*plain_pw_multi_aff_from_map(
4722 __isl_take isl_set
*domain
, __isl_take isl_basic_map
*bmap
)
4726 bmap
= isl_basic_map_drop_constraint_involving_unknown_divs(bmap
);
4727 ma
= extract_isl_multi_aff_from_basic_map(bmap
);
4728 ma
= isl_multi_aff_floor(ma
);
4729 return isl_pw_multi_aff_alloc(domain
, ma
);
4732 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4733 * This obviously only works if the input "map" is single-valued.
4734 * If so, we compute the lexicographic minimum of the image in the form
4735 * of an isl_pw_multi_aff. Since the image is unique, it is equal
4736 * to its lexicographic minimum.
4737 * If the input is not single-valued, we produce an error.
4739 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_base(
4740 __isl_take isl_map
*map
)
4744 isl_pw_multi_aff
*pma
;
4746 sv
= isl_map_is_single_valued(map
);
4750 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
4751 "map is not single-valued", goto error
);
4752 map
= isl_map_make_disjoint(map
);
4756 pma
= isl_pw_multi_aff_empty(isl_map_get_space(map
));
4758 for (i
= 0; i
< map
->n
; ++i
) {
4759 isl_pw_multi_aff
*pma_i
;
4760 isl_basic_map
*bmap
;
4761 bmap
= isl_basic_map_copy(map
->p
[i
]);
4762 pma_i
= isl_basic_map_lexmin_pw_multi_aff(bmap
);
4763 pma
= isl_pw_multi_aff_add_disjoint(pma
, pma_i
);
4773 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
4774 * taking into account that the output dimension at position "d"
4775 * can be represented as
4777 * x = floor((e(...) + c1) / m)
4779 * given that constraint "i" is of the form
4781 * e(...) + c1 - m x >= 0
4784 * Let "map" be of the form
4788 * We construct a mapping
4790 * A -> [A -> x = floor(...)]
4792 * apply that to the map, obtaining
4794 * [A -> x = floor(...)] -> B
4796 * and equate dimension "d" to x.
4797 * We then compute a isl_pw_multi_aff representation of the resulting map
4798 * and plug in the mapping above.
4800 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_div(
4801 __isl_take isl_map
*map
, __isl_take isl_basic_map
*hull
, int d
, int i
)
4805 isl_local_space
*ls
;
4813 isl_pw_multi_aff
*pma
;
4816 is_set
= isl_map_is_set(map
);
4820 offset
= isl_basic_map_offset(hull
, isl_dim_out
);
4821 ctx
= isl_map_get_ctx(map
);
4822 space
= isl_space_domain(isl_map_get_space(map
));
4823 n_in
= isl_space_dim(space
, isl_dim_set
);
4824 n
= isl_space_dim(space
, isl_dim_all
);
4826 v
= isl_vec_alloc(ctx
, 1 + 1 + n
);
4828 isl_int_neg(v
->el
[0], hull
->ineq
[i
][offset
+ d
]);
4829 isl_seq_cpy(v
->el
+ 1, hull
->ineq
[i
], 1 + n
);
4831 isl_basic_map_free(hull
);
4833 ls
= isl_local_space_from_space(isl_space_copy(space
));
4834 aff
= isl_aff_alloc_vec(ls
, v
);
4835 aff
= isl_aff_floor(aff
);
4837 isl_space_free(space
);
4838 ma
= isl_multi_aff_from_aff(aff
);
4840 ma
= isl_multi_aff_identity(isl_space_map_from_set(space
));
4841 ma
= isl_multi_aff_range_product(ma
,
4842 isl_multi_aff_from_aff(aff
));
4845 insert
= isl_map_from_multi_aff(isl_multi_aff_copy(ma
));
4846 map
= isl_map_apply_domain(map
, insert
);
4847 map
= isl_map_equate(map
, isl_dim_in
, n_in
, isl_dim_out
, d
);
4848 pma
= isl_pw_multi_aff_from_map(map
);
4849 pma
= isl_pw_multi_aff_pullback_multi_aff(pma
, ma
);
4854 isl_basic_map_free(hull
);
4858 /* Is constraint "c" of the form
4860 * e(...) + c1 - m x >= 0
4864 * -e(...) + c2 + m x >= 0
4866 * where m > 1 and e only depends on parameters and input dimemnsions?
4868 * "offset" is the offset of the output dimensions
4869 * "pos" is the position of output dimension x.
4871 static int is_potential_div_constraint(isl_int
*c
, int offset
, int d
, int total
)
4873 if (isl_int_is_zero(c
[offset
+ d
]))
4875 if (isl_int_is_one(c
[offset
+ d
]))
4877 if (isl_int_is_negone(c
[offset
+ d
]))
4879 if (isl_seq_first_non_zero(c
+ offset
, d
) != -1)
4881 if (isl_seq_first_non_zero(c
+ offset
+ d
+ 1,
4882 total
- (offset
+ d
+ 1)) != -1)
4887 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4889 * As a special case, we first check if there is any pair of constraints,
4890 * shared by all the basic maps in "map" that force a given dimension
4891 * to be equal to the floor of some affine combination of the input dimensions.
4893 * In particular, if we can find two constraints
4895 * e(...) + c1 - m x >= 0 i.e., m x <= e(...) + c1
4899 * -e(...) + c2 + m x >= 0 i.e., m x >= e(...) - c2
4901 * where m > 1 and e only depends on parameters and input dimemnsions,
4904 * c1 + c2 < m i.e., -c2 >= c1 - (m - 1)
4906 * then we know that we can take
4908 * x = floor((e(...) + c1) / m)
4910 * without having to perform any computation.
4912 * Note that we know that
4916 * If c1 + c2 were 0, then we would have detected an equality during
4917 * simplification. If c1 + c2 were negative, then we would have detected
4920 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_check_div(
4921 __isl_take isl_map
*map
)
4927 isl_basic_map
*hull
;
4929 hull
= isl_map_unshifted_simple_hull(isl_map_copy(map
));
4934 dim
= isl_map_dim(map
, isl_dim_out
);
4935 offset
= isl_basic_map_offset(hull
, isl_dim_out
);
4936 total
= 1 + isl_basic_map_total_dim(hull
);
4938 for (d
= 0; d
< dim
; ++d
) {
4939 for (i
= 0; i
< n
; ++i
) {
4940 if (!is_potential_div_constraint(hull
->ineq
[i
],
4943 for (j
= i
+ 1; j
< n
; ++j
) {
4944 if (!isl_seq_is_neg(hull
->ineq
[i
] + 1,
4945 hull
->ineq
[j
] + 1, total
- 1))
4947 isl_int_add(sum
, hull
->ineq
[i
][0],
4949 if (isl_int_abs_lt(sum
,
4950 hull
->ineq
[i
][offset
+ d
]))
4957 if (isl_int_is_pos(hull
->ineq
[j
][offset
+ d
]))
4959 return pw_multi_aff_from_map_div(map
, hull
, d
, j
);
4963 isl_basic_map_free(hull
);
4964 return pw_multi_aff_from_map_base(map
);
4967 isl_basic_map_free(hull
);
4971 /* Given an affine expression
4973 * [A -> B] -> f(A,B)
4975 * construct an isl_multi_aff
4979 * such that dimension "d" in B' is set to "aff" and the remaining
4980 * dimensions are set equal to the corresponding dimensions in B.
4981 * "n_in" is the dimension of the space A.
4982 * "n_out" is the dimension of the space B.
4984 * If "is_set" is set, then the affine expression is of the form
4988 * and we construct an isl_multi_aff
4992 static __isl_give isl_multi_aff
*range_map(__isl_take isl_aff
*aff
, int d
,
4993 unsigned n_in
, unsigned n_out
, int is_set
)
4997 isl_space
*space
, *space2
;
4998 isl_local_space
*ls
;
5000 space
= isl_aff_get_domain_space(aff
);
5001 ls
= isl_local_space_from_space(isl_space_copy(space
));
5002 space2
= isl_space_copy(space
);
5004 space2
= isl_space_range(isl_space_unwrap(space2
));
5005 space
= isl_space_map_from_domain_and_range(space
, space2
);
5006 ma
= isl_multi_aff_alloc(space
);
5007 ma
= isl_multi_aff_set_aff(ma
, d
, aff
);
5009 for (i
= 0; i
< n_out
; ++i
) {
5012 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
5013 isl_dim_set
, n_in
+ i
);
5014 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
5017 isl_local_space_free(ls
);
5022 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
5023 * taking into account that the dimension at position "d" can be written as
5025 * x = m a + f(..) (1)
5027 * where m is equal to "gcd".
5028 * "i" is the index of the equality in "hull" that defines f(..).
5029 * In particular, the equality is of the form
5031 * f(..) - x + m g(existentials) = 0
5035 * -f(..) + x + m g(existentials) = 0
5037 * We basically plug (1) into "map", resulting in a map with "a"
5038 * in the range instead of "x". The corresponding isl_pw_multi_aff
5039 * defining "a" is then plugged back into (1) to obtain a definition for "x".
5041 * Specifically, given the input map
5045 * We first wrap it into a set
5049 * and define (1) on top of the corresponding space, resulting in "aff".
5050 * We use this to create an isl_multi_aff that maps the output position "d"
5051 * from "a" to "x", leaving all other (intput and output) dimensions unchanged.
5052 * We plug this into the wrapped map, unwrap the result and compute the
5053 * corresponding isl_pw_multi_aff.
5054 * The result is an expression
5062 * so that we can plug that into "aff", after extending the latter to
5068 * If "map" is actually a set, then there is no "A" space, meaning
5069 * that we do not need to perform any wrapping, and that the result
5070 * of the recursive call is of the form
5074 * which is plugged into a mapping of the form
5078 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_stride(
5079 __isl_take isl_map
*map
, __isl_take isl_basic_map
*hull
, int d
, int i
,
5084 isl_local_space
*ls
;
5087 isl_pw_multi_aff
*pma
, *id
;
5093 is_set
= isl_map_is_set(map
);
5097 n_in
= isl_basic_map_dim(hull
, isl_dim_in
);
5098 n_out
= isl_basic_map_dim(hull
, isl_dim_out
);
5099 o_out
= isl_basic_map_offset(hull
, isl_dim_out
);
5104 set
= isl_map_wrap(map
);
5105 space
= isl_space_map_from_set(isl_set_get_space(set
));
5106 ma
= isl_multi_aff_identity(space
);
5107 ls
= isl_local_space_from_space(isl_set_get_space(set
));
5108 aff
= isl_aff_alloc(ls
);
5110 isl_int_set_si(aff
->v
->el
[0], 1);
5111 if (isl_int_is_one(hull
->eq
[i
][o_out
+ d
]))
5112 isl_seq_neg(aff
->v
->el
+ 1, hull
->eq
[i
],
5115 isl_seq_cpy(aff
->v
->el
+ 1, hull
->eq
[i
],
5117 isl_int_set(aff
->v
->el
[1 + o_out
+ d
], gcd
);
5119 ma
= isl_multi_aff_set_aff(ma
, n_in
+ d
, isl_aff_copy(aff
));
5120 set
= isl_set_preimage_multi_aff(set
, ma
);
5122 ma
= range_map(aff
, d
, n_in
, n_out
, is_set
);
5127 map
= isl_set_unwrap(set
);
5128 pma
= isl_pw_multi_aff_from_map(map
);
5131 space
= isl_pw_multi_aff_get_domain_space(pma
);
5132 space
= isl_space_map_from_set(space
);
5133 id
= isl_pw_multi_aff_identity(space
);
5134 pma
= isl_pw_multi_aff_range_product(id
, pma
);
5136 id
= isl_pw_multi_aff_from_multi_aff(ma
);
5137 pma
= isl_pw_multi_aff_pullback_pw_multi_aff(id
, pma
);
5139 isl_basic_map_free(hull
);
5143 isl_basic_map_free(hull
);
5147 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5148 * "hull" contains the equalities valid for "map".
5150 * Check if any of the output dimensions is "strided".
5151 * That is, we check if it can be written as
5155 * with m greater than 1, a some combination of existentially quantified
5156 * variables and f an expression in the parameters and input dimensions.
5157 * If so, we remove the stride in pw_multi_aff_from_map_stride.
5159 * Otherwise, we continue with pw_multi_aff_from_map_check_div for a further
5162 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_check_strides(
5163 __isl_take isl_map
*map
, __isl_take isl_basic_map
*hull
)
5172 n_div
= isl_basic_map_dim(hull
, isl_dim_div
);
5173 o_div
= isl_basic_map_offset(hull
, isl_dim_div
);
5176 isl_basic_map_free(hull
);
5177 return pw_multi_aff_from_map_check_div(map
);
5182 n_out
= isl_basic_map_dim(hull
, isl_dim_out
);
5183 o_out
= isl_basic_map_offset(hull
, isl_dim_out
);
5185 for (i
= 0; i
< n_out
; ++i
) {
5186 for (j
= 0; j
< hull
->n_eq
; ++j
) {
5187 isl_int
*eq
= hull
->eq
[j
];
5188 isl_pw_multi_aff
*res
;
5190 if (!isl_int_is_one(eq
[o_out
+ i
]) &&
5191 !isl_int_is_negone(eq
[o_out
+ i
]))
5193 if (isl_seq_first_non_zero(eq
+ o_out
, i
) != -1)
5195 if (isl_seq_first_non_zero(eq
+ o_out
+ i
+ 1,
5196 n_out
- (i
+ 1)) != -1)
5198 isl_seq_gcd(eq
+ o_div
, n_div
, &gcd
);
5199 if (isl_int_is_zero(gcd
))
5201 if (isl_int_is_one(gcd
))
5204 res
= pw_multi_aff_from_map_stride(map
, hull
,
5212 isl_basic_map_free(hull
);
5213 return pw_multi_aff_from_map_check_div(map
);
5216 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5218 * As a special case, we first check if all output dimensions are uniquely
5219 * defined in terms of the parameters and input dimensions over the entire
5220 * domain. If so, we extract the desired isl_pw_multi_aff directly
5221 * from the affine hull of "map" and its domain.
5223 * Otherwise, continue with pw_multi_aff_from_map_check_strides for more
5226 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_map(__isl_take isl_map
*map
)
5229 isl_basic_map
*hull
;
5234 if (isl_map_n_basic_map(map
) == 1) {
5235 hull
= isl_map_unshifted_simple_hull(isl_map_copy(map
));
5236 hull
= isl_basic_map_plain_affine_hull(hull
);
5237 sv
= isl_basic_map_plain_is_single_valued(hull
);
5239 return plain_pw_multi_aff_from_map(isl_map_domain(map
),
5241 isl_basic_map_free(hull
);
5243 map
= isl_map_detect_equalities(map
);
5244 hull
= isl_map_unshifted_simple_hull(isl_map_copy(map
));
5245 sv
= isl_basic_map_plain_is_single_valued(hull
);
5247 return plain_pw_multi_aff_from_map(isl_map_domain(map
), hull
);
5249 return pw_multi_aff_from_map_check_strides(map
, hull
);
5250 isl_basic_map_free(hull
);
5255 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_set(__isl_take isl_set
*set
)
5257 return isl_pw_multi_aff_from_map(set
);
5260 /* Convert "map" into an isl_pw_multi_aff (if possible) and
5263 static isl_stat
pw_multi_aff_from_map(__isl_take isl_map
*map
, void *user
)
5265 isl_union_pw_multi_aff
**upma
= user
;
5266 isl_pw_multi_aff
*pma
;
5268 pma
= isl_pw_multi_aff_from_map(map
);
5269 *upma
= isl_union_pw_multi_aff_add_pw_multi_aff(*upma
, pma
);
5271 return *upma
? isl_stat_ok
: isl_stat_error
;
5274 /* Create an isl_union_pw_multi_aff with the given isl_aff on a universe
5277 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_aff(
5278 __isl_take isl_aff
*aff
)
5281 isl_pw_multi_aff
*pma
;
5283 ma
= isl_multi_aff_from_aff(aff
);
5284 pma
= isl_pw_multi_aff_from_multi_aff(ma
);
5285 return isl_union_pw_multi_aff_from_pw_multi_aff(pma
);
5288 /* Try and create an isl_union_pw_multi_aff that is equivalent
5289 * to the given isl_union_map.
5290 * The isl_union_map is required to be single-valued in each space.
5291 * Otherwise, an error is produced.
5293 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_union_map(
5294 __isl_take isl_union_map
*umap
)
5297 isl_union_pw_multi_aff
*upma
;
5299 space
= isl_union_map_get_space(umap
);
5300 upma
= isl_union_pw_multi_aff_empty(space
);
5301 if (isl_union_map_foreach_map(umap
, &pw_multi_aff_from_map
, &upma
) < 0)
5302 upma
= isl_union_pw_multi_aff_free(upma
);
5303 isl_union_map_free(umap
);
5308 /* Try and create an isl_union_pw_multi_aff that is equivalent
5309 * to the given isl_union_set.
5310 * The isl_union_set is required to be a singleton in each space.
5311 * Otherwise, an error is produced.
5313 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_union_set(
5314 __isl_take isl_union_set
*uset
)
5316 return isl_union_pw_multi_aff_from_union_map(uset
);
5319 /* Return the piecewise affine expression "set ? 1 : 0".
5321 __isl_give isl_pw_aff
*isl_set_indicator_function(__isl_take isl_set
*set
)
5324 isl_space
*space
= isl_set_get_space(set
);
5325 isl_local_space
*ls
= isl_local_space_from_space(space
);
5326 isl_aff
*zero
= isl_aff_zero_on_domain(isl_local_space_copy(ls
));
5327 isl_aff
*one
= isl_aff_zero_on_domain(ls
);
5329 one
= isl_aff_add_constant_si(one
, 1);
5330 pa
= isl_pw_aff_alloc(isl_set_copy(set
), one
);
5331 set
= isl_set_complement(set
);
5332 pa
= isl_pw_aff_add_disjoint(pa
, isl_pw_aff_alloc(set
, zero
));
5337 /* Plug in "subs" for dimension "type", "pos" of "aff".
5339 * Let i be the dimension to replace and let "subs" be of the form
5343 * and "aff" of the form
5349 * (a f + d g')/(m d)
5351 * where g' is the result of plugging in "subs" in each of the integer
5354 __isl_give isl_aff
*isl_aff_substitute(__isl_take isl_aff
*aff
,
5355 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
5360 aff
= isl_aff_cow(aff
);
5362 return isl_aff_free(aff
);
5364 ctx
= isl_aff_get_ctx(aff
);
5365 if (!isl_space_is_equal(aff
->ls
->dim
, subs
->ls
->dim
))
5366 isl_die(ctx
, isl_error_invalid
,
5367 "spaces don't match", return isl_aff_free(aff
));
5368 if (isl_local_space_dim(subs
->ls
, isl_dim_div
) != 0)
5369 isl_die(ctx
, isl_error_unsupported
,
5370 "cannot handle divs yet", return isl_aff_free(aff
));
5372 aff
->ls
= isl_local_space_substitute(aff
->ls
, type
, pos
, subs
);
5374 return isl_aff_free(aff
);
5376 aff
->v
= isl_vec_cow(aff
->v
);
5378 return isl_aff_free(aff
);
5380 pos
+= isl_local_space_offset(aff
->ls
, type
);
5383 isl_seq_substitute(aff
->v
->el
, pos
, subs
->v
->el
,
5384 aff
->v
->size
, subs
->v
->size
, v
);
5390 /* Plug in "subs" for dimension "type", "pos" in each of the affine
5391 * expressions in "maff".
5393 __isl_give isl_multi_aff
*isl_multi_aff_substitute(
5394 __isl_take isl_multi_aff
*maff
, enum isl_dim_type type
, unsigned pos
,
5395 __isl_keep isl_aff
*subs
)
5399 maff
= isl_multi_aff_cow(maff
);
5401 return isl_multi_aff_free(maff
);
5403 if (type
== isl_dim_in
)
5406 for (i
= 0; i
< maff
->n
; ++i
) {
5407 maff
->u
.p
[i
] = isl_aff_substitute(maff
->u
.p
[i
],
5410 return isl_multi_aff_free(maff
);
5416 /* Plug in "subs" for dimension "type", "pos" of "pma".
5418 * pma is of the form
5422 * while subs is of the form
5424 * v' = B_j(v) -> S_j
5426 * Each pair i,j such that C_ij = A_i \cap B_i is non-empty
5427 * has a contribution in the result, in particular
5429 * C_ij(S_j) -> M_i(S_j)
5431 * Note that plugging in S_j in C_ij may also result in an empty set
5432 * and this contribution should simply be discarded.
5434 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_substitute(
5435 __isl_take isl_pw_multi_aff
*pma
, enum isl_dim_type type
, unsigned pos
,
5436 __isl_keep isl_pw_aff
*subs
)
5439 isl_pw_multi_aff
*res
;
5442 return isl_pw_multi_aff_free(pma
);
5444 n
= pma
->n
* subs
->n
;
5445 res
= isl_pw_multi_aff_alloc_size(isl_space_copy(pma
->dim
), n
);
5447 for (i
= 0; i
< pma
->n
; ++i
) {
5448 for (j
= 0; j
< subs
->n
; ++j
) {
5450 isl_multi_aff
*res_ij
;
5453 common
= isl_set_intersect(
5454 isl_set_copy(pma
->p
[i
].set
),
5455 isl_set_copy(subs
->p
[j
].set
));
5456 common
= isl_set_substitute(common
,
5457 type
, pos
, subs
->p
[j
].aff
);
5458 empty
= isl_set_plain_is_empty(common
);
5459 if (empty
< 0 || empty
) {
5460 isl_set_free(common
);
5466 res_ij
= isl_multi_aff_substitute(
5467 isl_multi_aff_copy(pma
->p
[i
].maff
),
5468 type
, pos
, subs
->p
[j
].aff
);
5470 res
= isl_pw_multi_aff_add_piece(res
, common
, res_ij
);
5474 isl_pw_multi_aff_free(pma
);
5477 isl_pw_multi_aff_free(pma
);
5478 isl_pw_multi_aff_free(res
);
5482 /* Compute the preimage of a range of dimensions in the affine expression "src"
5483 * under "ma" and put the result in "dst". The number of dimensions in "src"
5484 * that precede the range is given by "n_before". The number of dimensions
5485 * in the range is given by the number of output dimensions of "ma".
5486 * The number of dimensions that follow the range is given by "n_after".
5487 * If "has_denom" is set (to one),
5488 * then "src" and "dst" have an extra initial denominator.
5489 * "n_div_ma" is the number of existentials in "ma"
5490 * "n_div_bset" is the number of existentials in "src"
5491 * The resulting "dst" (which is assumed to have been allocated by
5492 * the caller) contains coefficients for both sets of existentials,
5493 * first those in "ma" and then those in "src".
5494 * f, c1, c2 and g are temporary objects that have been initialized
5497 * Let src represent the expression
5499 * (a(p) + f_u u + b v + f_w w + c(divs))/d
5501 * and let ma represent the expressions
5503 * v_i = (r_i(p) + s_i(y) + t_i(divs'))/m_i
5505 * We start out with the following expression for dst:
5507 * (a(p) + f_u u + 0 y + f_w w + 0 divs' + c(divs) + f \sum_i b_i v_i)/d
5509 * with the multiplication factor f initially equal to 1
5510 * and f \sum_i b_i v_i kept separately.
5511 * For each x_i that we substitute, we multiply the numerator
5512 * (and denominator) of dst by c_1 = m_i and add the numerator
5513 * of the x_i expression multiplied by c_2 = f b_i,
5514 * after removing the common factors of c_1 and c_2.
5515 * The multiplication factor f also needs to be multiplied by c_1
5516 * for the next x_j, j > i.
5518 void isl_seq_preimage(isl_int
*dst
, isl_int
*src
,
5519 __isl_keep isl_multi_aff
*ma
, int n_before
, int n_after
,
5520 int n_div_ma
, int n_div_bmap
,
5521 isl_int f
, isl_int c1
, isl_int c2
, isl_int g
, int has_denom
)
5524 int n_param
, n_in
, n_out
;
5527 n_param
= isl_multi_aff_dim(ma
, isl_dim_param
);
5528 n_in
= isl_multi_aff_dim(ma
, isl_dim_in
);
5529 n_out
= isl_multi_aff_dim(ma
, isl_dim_out
);
5531 isl_seq_cpy(dst
, src
, has_denom
+ 1 + n_param
+ n_before
);
5532 o_dst
= o_src
= has_denom
+ 1 + n_param
+ n_before
;
5533 isl_seq_clr(dst
+ o_dst
, n_in
);
5536 isl_seq_cpy(dst
+ o_dst
, src
+ o_src
, n_after
);
5539 isl_seq_clr(dst
+ o_dst
, n_div_ma
);
5541 isl_seq_cpy(dst
+ o_dst
, src
+ o_src
, n_div_bmap
);
5543 isl_int_set_si(f
, 1);
5545 for (i
= 0; i
< n_out
; ++i
) {
5546 int offset
= has_denom
+ 1 + n_param
+ n_before
+ i
;
5548 if (isl_int_is_zero(src
[offset
]))
5550 isl_int_set(c1
, ma
->u
.p
[i
]->v
->el
[0]);
5551 isl_int_mul(c2
, f
, src
[offset
]);
5552 isl_int_gcd(g
, c1
, c2
);
5553 isl_int_divexact(c1
, c1
, g
);
5554 isl_int_divexact(c2
, c2
, g
);
5556 isl_int_mul(f
, f
, c1
);
5559 isl_seq_combine(dst
+ o_dst
, c1
, dst
+ o_dst
,
5560 c2
, ma
->u
.p
[i
]->v
->el
+ o_src
, 1 + n_param
);
5561 o_dst
+= 1 + n_param
;
5562 o_src
+= 1 + n_param
;
5563 isl_seq_scale(dst
+ o_dst
, dst
+ o_dst
, c1
, n_before
);
5565 isl_seq_combine(dst
+ o_dst
, c1
, dst
+ o_dst
,
5566 c2
, ma
->u
.p
[i
]->v
->el
+ o_src
, n_in
);
5569 isl_seq_scale(dst
+ o_dst
, dst
+ o_dst
, c1
, n_after
);
5571 isl_seq_combine(dst
+ o_dst
, c1
, dst
+ o_dst
,
5572 c2
, ma
->u
.p
[i
]->v
->el
+ o_src
, n_div_ma
);
5575 isl_seq_scale(dst
+ o_dst
, dst
+ o_dst
, c1
, n_div_bmap
);
5577 isl_int_mul(dst
[0], dst
[0], c1
);
5581 /* Compute the pullback of "aff" by the function represented by "ma".
5582 * In other words, plug in "ma" in "aff". The result is an affine expression
5583 * defined over the domain space of "ma".
5585 * If "aff" is represented by
5587 * (a(p) + b x + c(divs))/d
5589 * and ma is represented by
5591 * x = D(p) + F(y) + G(divs')
5593 * then the result is
5595 * (a(p) + b D(p) + b F(y) + b G(divs') + c(divs))/d
5597 * The divs in the local space of the input are similarly adjusted
5598 * through a call to isl_local_space_preimage_multi_aff.
5600 __isl_give isl_aff
*isl_aff_pullback_multi_aff(__isl_take isl_aff
*aff
,
5601 __isl_take isl_multi_aff
*ma
)
5603 isl_aff
*res
= NULL
;
5604 isl_local_space
*ls
;
5605 int n_div_aff
, n_div_ma
;
5606 isl_int f
, c1
, c2
, g
;
5608 ma
= isl_multi_aff_align_divs(ma
);
5612 n_div_aff
= isl_aff_dim(aff
, isl_dim_div
);
5613 n_div_ma
= ma
->n
? isl_aff_dim(ma
->u
.p
[0], isl_dim_div
) : 0;
5615 ls
= isl_aff_get_domain_local_space(aff
);
5616 ls
= isl_local_space_preimage_multi_aff(ls
, isl_multi_aff_copy(ma
));
5617 res
= isl_aff_alloc(ls
);
5626 isl_seq_preimage(res
->v
->el
, aff
->v
->el
, ma
, 0, 0, n_div_ma
, n_div_aff
,
5635 isl_multi_aff_free(ma
);
5636 res
= isl_aff_normalize(res
);
5640 isl_multi_aff_free(ma
);
5645 /* Compute the pullback of "aff1" by the function represented by "aff2".
5646 * In other words, plug in "aff2" in "aff1". The result is an affine expression
5647 * defined over the domain space of "aff1".
5649 * The domain of "aff1" should match the range of "aff2", which means
5650 * that it should be single-dimensional.
5652 __isl_give isl_aff
*isl_aff_pullback_aff(__isl_take isl_aff
*aff1
,
5653 __isl_take isl_aff
*aff2
)
5657 ma
= isl_multi_aff_from_aff(aff2
);
5658 return isl_aff_pullback_multi_aff(aff1
, ma
);
5661 /* Compute the pullback of "ma1" by the function represented by "ma2".
5662 * In other words, plug in "ma2" in "ma1".
5664 * The parameters of "ma1" and "ma2" are assumed to have been aligned.
5666 static __isl_give isl_multi_aff
*isl_multi_aff_pullback_multi_aff_aligned(
5667 __isl_take isl_multi_aff
*ma1
, __isl_take isl_multi_aff
*ma2
)
5670 isl_space
*space
= NULL
;
5672 ma2
= isl_multi_aff_align_divs(ma2
);
5673 ma1
= isl_multi_aff_cow(ma1
);
5677 space
= isl_space_join(isl_multi_aff_get_space(ma2
),
5678 isl_multi_aff_get_space(ma1
));
5680 for (i
= 0; i
< ma1
->n
; ++i
) {
5681 ma1
->u
.p
[i
] = isl_aff_pullback_multi_aff(ma1
->u
.p
[i
],
5682 isl_multi_aff_copy(ma2
));
5687 ma1
= isl_multi_aff_reset_space(ma1
, space
);
5688 isl_multi_aff_free(ma2
);
5691 isl_space_free(space
);
5692 isl_multi_aff_free(ma2
);
5693 isl_multi_aff_free(ma1
);
5697 /* Compute the pullback of "ma1" by the function represented by "ma2".
5698 * In other words, plug in "ma2" in "ma1".
5700 __isl_give isl_multi_aff
*isl_multi_aff_pullback_multi_aff(
5701 __isl_take isl_multi_aff
*ma1
, __isl_take isl_multi_aff
*ma2
)
5703 return isl_multi_aff_align_params_multi_multi_and(ma1
, ma2
,
5704 &isl_multi_aff_pullback_multi_aff_aligned
);
5707 /* Extend the local space of "dst" to include the divs
5708 * in the local space of "src".
5710 * If "src" does not have any divs or if the local spaces of "dst" and
5711 * "src" are the same, then no extension is required.
5713 __isl_give isl_aff
*isl_aff_align_divs(__isl_take isl_aff
*dst
,
5714 __isl_keep isl_aff
*src
)
5717 int src_n_div
, dst_n_div
;
5724 return isl_aff_free(dst
);
5726 ctx
= isl_aff_get_ctx(src
);
5727 equal
= isl_local_space_has_equal_space(src
->ls
, dst
->ls
);
5729 return isl_aff_free(dst
);
5731 isl_die(ctx
, isl_error_invalid
,
5732 "spaces don't match", goto error
);
5734 src_n_div
= isl_local_space_dim(src
->ls
, isl_dim_div
);
5737 equal
= isl_local_space_is_equal(src
->ls
, dst
->ls
);
5739 return isl_aff_free(dst
);
5743 dst_n_div
= isl_local_space_dim(dst
->ls
, isl_dim_div
);
5744 exp1
= isl_alloc_array(ctx
, int, src_n_div
);
5745 exp2
= isl_alloc_array(ctx
, int, dst_n_div
);
5746 if (!exp1
|| (dst_n_div
&& !exp2
))
5749 div
= isl_merge_divs(src
->ls
->div
, dst
->ls
->div
, exp1
, exp2
);
5750 dst
= isl_aff_expand_divs(dst
, div
, exp2
);
5758 return isl_aff_free(dst
);
5761 /* Adjust the local spaces of the affine expressions in "maff"
5762 * such that they all have the save divs.
5764 __isl_give isl_multi_aff
*isl_multi_aff_align_divs(
5765 __isl_take isl_multi_aff
*maff
)
5773 maff
= isl_multi_aff_cow(maff
);
5777 for (i
= 1; i
< maff
->n
; ++i
)
5778 maff
->u
.p
[0] = isl_aff_align_divs(maff
->u
.p
[0], maff
->u
.p
[i
]);
5779 for (i
= 1; i
< maff
->n
; ++i
) {
5780 maff
->u
.p
[i
] = isl_aff_align_divs(maff
->u
.p
[i
], maff
->u
.p
[0]);
5782 return isl_multi_aff_free(maff
);
5788 __isl_give isl_aff
*isl_aff_lift(__isl_take isl_aff
*aff
)
5790 aff
= isl_aff_cow(aff
);
5794 aff
->ls
= isl_local_space_lift(aff
->ls
);
5796 return isl_aff_free(aff
);
5801 /* Lift "maff" to a space with extra dimensions such that the result
5802 * has no more existentially quantified variables.
5803 * If "ls" is not NULL, then *ls is assigned the local space that lies
5804 * at the basis of the lifting applied to "maff".
5806 __isl_give isl_multi_aff
*isl_multi_aff_lift(__isl_take isl_multi_aff
*maff
,
5807 __isl_give isl_local_space
**ls
)
5821 isl_space
*space
= isl_multi_aff_get_domain_space(maff
);
5822 *ls
= isl_local_space_from_space(space
);
5824 return isl_multi_aff_free(maff
);
5829 maff
= isl_multi_aff_cow(maff
);
5830 maff
= isl_multi_aff_align_divs(maff
);
5834 n_div
= isl_aff_dim(maff
->u
.p
[0], isl_dim_div
);
5835 space
= isl_multi_aff_get_space(maff
);
5836 space
= isl_space_lift(isl_space_domain(space
), n_div
);
5837 space
= isl_space_extend_domain_with_range(space
,
5838 isl_multi_aff_get_space(maff
));
5840 return isl_multi_aff_free(maff
);
5841 isl_space_free(maff
->space
);
5842 maff
->space
= space
;
5845 *ls
= isl_aff_get_domain_local_space(maff
->u
.p
[0]);
5847 return isl_multi_aff_free(maff
);
5850 for (i
= 0; i
< maff
->n
; ++i
) {
5851 maff
->u
.p
[i
] = isl_aff_lift(maff
->u
.p
[i
]);
5859 isl_local_space_free(*ls
);
5860 return isl_multi_aff_free(maff
);
5864 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma".
5866 __isl_give isl_pw_aff
*isl_pw_multi_aff_get_pw_aff(
5867 __isl_keep isl_pw_multi_aff
*pma
, int pos
)
5877 n_out
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
5878 if (pos
< 0 || pos
>= n_out
)
5879 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
5880 "index out of bounds", return NULL
);
5882 space
= isl_pw_multi_aff_get_space(pma
);
5883 space
= isl_space_drop_dims(space
, isl_dim_out
,
5884 pos
+ 1, n_out
- pos
- 1);
5885 space
= isl_space_drop_dims(space
, isl_dim_out
, 0, pos
);
5887 pa
= isl_pw_aff_alloc_size(space
, pma
->n
);
5888 for (i
= 0; i
< pma
->n
; ++i
) {
5890 aff
= isl_multi_aff_get_aff(pma
->p
[i
].maff
, pos
);
5891 pa
= isl_pw_aff_add_piece(pa
, isl_set_copy(pma
->p
[i
].set
), aff
);
5897 /* Return an isl_pw_multi_aff with the given "set" as domain and
5898 * an unnamed zero-dimensional range.
5900 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_domain(
5901 __isl_take isl_set
*set
)
5906 space
= isl_set_get_space(set
);
5907 space
= isl_space_from_domain(space
);
5908 ma
= isl_multi_aff_zero(space
);
5909 return isl_pw_multi_aff_alloc(set
, ma
);
5912 /* Add an isl_pw_multi_aff with the given "set" as domain and
5913 * an unnamed zero-dimensional range to *user.
5915 static isl_stat
add_pw_multi_aff_from_domain(__isl_take isl_set
*set
,
5918 isl_union_pw_multi_aff
**upma
= user
;
5919 isl_pw_multi_aff
*pma
;
5921 pma
= isl_pw_multi_aff_from_domain(set
);
5922 *upma
= isl_union_pw_multi_aff_add_pw_multi_aff(*upma
, pma
);
5927 /* Return an isl_union_pw_multi_aff with the given "uset" as domain and
5928 * an unnamed zero-dimensional range.
5930 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_domain(
5931 __isl_take isl_union_set
*uset
)
5934 isl_union_pw_multi_aff
*upma
;
5939 space
= isl_union_set_get_space(uset
);
5940 upma
= isl_union_pw_multi_aff_empty(space
);
5942 if (isl_union_set_foreach_set(uset
,
5943 &add_pw_multi_aff_from_domain
, &upma
) < 0)
5946 isl_union_set_free(uset
);
5949 isl_union_set_free(uset
);
5950 isl_union_pw_multi_aff_free(upma
);
5954 /* Convert "pma" to an isl_map and add it to *umap.
5956 static isl_stat
map_from_pw_multi_aff(__isl_take isl_pw_multi_aff
*pma
,
5959 isl_union_map
**umap
= user
;
5962 map
= isl_map_from_pw_multi_aff(pma
);
5963 *umap
= isl_union_map_add_map(*umap
, map
);
5968 /* Construct a union map mapping the domain of the union
5969 * piecewise multi-affine expression to its range, with each dimension
5970 * in the range equated to the corresponding affine expression on its cell.
5972 __isl_give isl_union_map
*isl_union_map_from_union_pw_multi_aff(
5973 __isl_take isl_union_pw_multi_aff
*upma
)
5976 isl_union_map
*umap
;
5981 space
= isl_union_pw_multi_aff_get_space(upma
);
5982 umap
= isl_union_map_empty(space
);
5984 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma
,
5985 &map_from_pw_multi_aff
, &umap
) < 0)
5988 isl_union_pw_multi_aff_free(upma
);
5991 isl_union_pw_multi_aff_free(upma
);
5992 isl_union_map_free(umap
);
5996 /* Local data for bin_entry and the callback "fn".
5998 struct isl_union_pw_multi_aff_bin_data
{
5999 isl_union_pw_multi_aff
*upma2
;
6000 isl_union_pw_multi_aff
*res
;
6001 isl_pw_multi_aff
*pma
;
6002 isl_stat (*fn
)(__isl_take isl_pw_multi_aff
*pma
, void *user
);
6005 /* Given an isl_pw_multi_aff from upma1, store it in data->pma
6006 * and call data->fn for each isl_pw_multi_aff in data->upma2.
6008 static isl_stat
bin_entry(__isl_take isl_pw_multi_aff
*pma
, void *user
)
6010 struct isl_union_pw_multi_aff_bin_data
*data
= user
;
6014 r
= isl_union_pw_multi_aff_foreach_pw_multi_aff(data
->upma2
,
6016 isl_pw_multi_aff_free(pma
);
6021 /* Call "fn" on each pair of isl_pw_multi_affs in "upma1" and "upma2".
6022 * The isl_pw_multi_aff from upma1 is stored in data->pma (where data is
6023 * passed as user field) and the isl_pw_multi_aff from upma2 is available
6024 * as *entry. The callback should adjust data->res if desired.
6026 static __isl_give isl_union_pw_multi_aff
*bin_op(
6027 __isl_take isl_union_pw_multi_aff
*upma1
,
6028 __isl_take isl_union_pw_multi_aff
*upma2
,
6029 isl_stat (*fn
)(__isl_take isl_pw_multi_aff
*pma
, void *user
))
6032 struct isl_union_pw_multi_aff_bin_data data
= { NULL
, NULL
, NULL
, fn
};
6034 space
= isl_union_pw_multi_aff_get_space(upma2
);
6035 upma1
= isl_union_pw_multi_aff_align_params(upma1
, space
);
6036 space
= isl_union_pw_multi_aff_get_space(upma1
);
6037 upma2
= isl_union_pw_multi_aff_align_params(upma2
, space
);
6039 if (!upma1
|| !upma2
)
6043 data
.res
= isl_union_pw_multi_aff_alloc_same_size(upma1
);
6044 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma1
,
6045 &bin_entry
, &data
) < 0)
6048 isl_union_pw_multi_aff_free(upma1
);
6049 isl_union_pw_multi_aff_free(upma2
);
6052 isl_union_pw_multi_aff_free(upma1
);
6053 isl_union_pw_multi_aff_free(upma2
);
6054 isl_union_pw_multi_aff_free(data
.res
);
6058 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
6059 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
6061 static __isl_give isl_pw_multi_aff
*pw_multi_aff_range_product(
6062 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
6066 space
= isl_space_range_product(isl_pw_multi_aff_get_space(pma1
),
6067 isl_pw_multi_aff_get_space(pma2
));
6068 return isl_pw_multi_aff_on_shared_domain_in(pma1
, pma2
, space
,
6069 &isl_multi_aff_range_product
);
6072 /* Given two isl_pw_multi_affs A -> B and C -> D,
6073 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
6075 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_range_product(
6076 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
6078 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
6079 &pw_multi_aff_range_product
);
6082 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
6083 * construct an isl_pw_multi_aff (A * C) -> (B, D).
6085 static __isl_give isl_pw_multi_aff
*pw_multi_aff_flat_range_product(
6086 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
6090 space
= isl_space_range_product(isl_pw_multi_aff_get_space(pma1
),
6091 isl_pw_multi_aff_get_space(pma2
));
6092 space
= isl_space_flatten_range(space
);
6093 return isl_pw_multi_aff_on_shared_domain_in(pma1
, pma2
, space
,
6094 &isl_multi_aff_flat_range_product
);
6097 /* Given two isl_pw_multi_affs A -> B and C -> D,
6098 * construct an isl_pw_multi_aff (A * C) -> (B, D).
6100 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_flat_range_product(
6101 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
6103 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
6104 &pw_multi_aff_flat_range_product
);
6107 /* If data->pma and "pma2" have the same domain space, then compute
6108 * their flat range product and the result to data->res.
6110 static isl_stat
flat_range_product_entry(__isl_take isl_pw_multi_aff
*pma2
,
6113 struct isl_union_pw_multi_aff_bin_data
*data
= user
;
6115 if (!isl_space_tuple_is_equal(data
->pma
->dim
, isl_dim_in
,
6116 pma2
->dim
, isl_dim_in
)) {
6117 isl_pw_multi_aff_free(pma2
);
6121 pma2
= isl_pw_multi_aff_flat_range_product(
6122 isl_pw_multi_aff_copy(data
->pma
), pma2
);
6124 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma2
);
6129 /* Given two isl_union_pw_multi_affs A -> B and C -> D,
6130 * construct an isl_union_pw_multi_aff (A * C) -> (B, D).
6132 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_flat_range_product(
6133 __isl_take isl_union_pw_multi_aff
*upma1
,
6134 __isl_take isl_union_pw_multi_aff
*upma2
)
6136 return bin_op(upma1
, upma2
, &flat_range_product_entry
);
6139 /* Replace the affine expressions at position "pos" in "pma" by "pa".
6140 * The parameters are assumed to have been aligned.
6142 * The implementation essentially performs an isl_pw_*_on_shared_domain,
6143 * except that it works on two different isl_pw_* types.
6145 static __isl_give isl_pw_multi_aff
*pw_multi_aff_set_pw_aff(
6146 __isl_take isl_pw_multi_aff
*pma
, unsigned pos
,
6147 __isl_take isl_pw_aff
*pa
)
6150 isl_pw_multi_aff
*res
= NULL
;
6155 if (!isl_space_tuple_is_equal(pma
->dim
, isl_dim_in
,
6156 pa
->dim
, isl_dim_in
))
6157 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
6158 "domains don't match", goto error
);
6159 if (pos
>= isl_pw_multi_aff_dim(pma
, isl_dim_out
))
6160 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
6161 "index out of bounds", goto error
);
6164 res
= isl_pw_multi_aff_alloc_size(isl_pw_multi_aff_get_space(pma
), n
);
6166 for (i
= 0; i
< pma
->n
; ++i
) {
6167 for (j
= 0; j
< pa
->n
; ++j
) {
6169 isl_multi_aff
*res_ij
;
6172 common
= isl_set_intersect(isl_set_copy(pma
->p
[i
].set
),
6173 isl_set_copy(pa
->p
[j
].set
));
6174 empty
= isl_set_plain_is_empty(common
);
6175 if (empty
< 0 || empty
) {
6176 isl_set_free(common
);
6182 res_ij
= isl_multi_aff_set_aff(
6183 isl_multi_aff_copy(pma
->p
[i
].maff
), pos
,
6184 isl_aff_copy(pa
->p
[j
].aff
));
6185 res_ij
= isl_multi_aff_gist(res_ij
,
6186 isl_set_copy(common
));
6188 res
= isl_pw_multi_aff_add_piece(res
, common
, res_ij
);
6192 isl_pw_multi_aff_free(pma
);
6193 isl_pw_aff_free(pa
);
6196 isl_pw_multi_aff_free(pma
);
6197 isl_pw_aff_free(pa
);
6198 return isl_pw_multi_aff_free(res
);
6201 /* Replace the affine expressions at position "pos" in "pma" by "pa".
6203 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_set_pw_aff(
6204 __isl_take isl_pw_multi_aff
*pma
, unsigned pos
,
6205 __isl_take isl_pw_aff
*pa
)
6207 isl_bool equal_params
;
6211 equal_params
= isl_space_has_equal_params(pma
->dim
, pa
->dim
);
6212 if (equal_params
< 0)
6215 return pw_multi_aff_set_pw_aff(pma
, pos
, pa
);
6216 if (!isl_space_has_named_params(pma
->dim
) ||
6217 !isl_space_has_named_params(pa
->dim
))
6218 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
6219 "unaligned unnamed parameters", goto error
);
6220 pma
= isl_pw_multi_aff_align_params(pma
, isl_pw_aff_get_space(pa
));
6221 pa
= isl_pw_aff_align_params(pa
, isl_pw_multi_aff_get_space(pma
));
6222 return pw_multi_aff_set_pw_aff(pma
, pos
, pa
);
6224 isl_pw_multi_aff_free(pma
);
6225 isl_pw_aff_free(pa
);
6229 /* Do the parameters of "pa" match those of "space"?
6231 isl_bool
isl_pw_aff_matching_params(__isl_keep isl_pw_aff
*pa
,
6232 __isl_keep isl_space
*space
)
6234 isl_space
*pa_space
;
6238 return isl_bool_error
;
6240 pa_space
= isl_pw_aff_get_space(pa
);
6242 match
= isl_space_has_equal_params(space
, pa_space
);
6244 isl_space_free(pa_space
);
6248 /* Check that the domain space of "pa" matches "space".
6250 isl_stat
isl_pw_aff_check_match_domain_space(__isl_keep isl_pw_aff
*pa
,
6251 __isl_keep isl_space
*space
)
6253 isl_space
*pa_space
;
6257 return isl_stat_error
;
6259 pa_space
= isl_pw_aff_get_space(pa
);
6261 match
= isl_space_has_equal_params(space
, pa_space
);
6265 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
6266 "parameters don't match", goto error
);
6267 match
= isl_space_tuple_is_equal(space
, isl_dim_in
,
6268 pa_space
, isl_dim_in
);
6272 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
6273 "domains don't match", goto error
);
6274 isl_space_free(pa_space
);
6277 isl_space_free(pa_space
);
6278 return isl_stat_error
;
6286 #include <isl_multi_explicit_domain.c>
6287 #include <isl_multi_pw_aff_explicit_domain.c>
6288 #include <isl_multi_templ.c>
6289 #include <isl_multi_apply_set.c>
6290 #include <isl_multi_coalesce.c>
6291 #include <isl_multi_dims.c>
6292 #include <isl_multi_gist.c>
6293 #include <isl_multi_hash.c>
6294 #include <isl_multi_align_set.c>
6295 #include <isl_multi_intersect.c>
6297 /* Does "mpa" have a non-trivial explicit domain?
6299 * The explicit domain, if present, is trivial if it represents
6300 * an (obviously) universe set.
6302 isl_bool
isl_multi_pw_aff_has_non_trivial_domain(
6303 __isl_keep isl_multi_pw_aff
*mpa
)
6306 return isl_bool_error
;
6307 if (!isl_multi_pw_aff_has_explicit_domain(mpa
))
6308 return isl_bool_false
;
6309 return isl_bool_not(isl_set_plain_is_universe(mpa
->u
.dom
));
6312 /* Scale the elements of "pma" by the corresponding elements of "mv".
6314 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_scale_multi_val(
6315 __isl_take isl_pw_multi_aff
*pma
, __isl_take isl_multi_val
*mv
)
6318 isl_bool equal_params
;
6320 pma
= isl_pw_multi_aff_cow(pma
);
6323 if (!isl_space_tuple_is_equal(pma
->dim
, isl_dim_out
,
6324 mv
->space
, isl_dim_set
))
6325 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
6326 "spaces don't match", goto error
);
6327 equal_params
= isl_space_has_equal_params(pma
->dim
, mv
->space
);
6328 if (equal_params
< 0)
6330 if (!equal_params
) {
6331 pma
= isl_pw_multi_aff_align_params(pma
,
6332 isl_multi_val_get_space(mv
));
6333 mv
= isl_multi_val_align_params(mv
,
6334 isl_pw_multi_aff_get_space(pma
));
6339 for (i
= 0; i
< pma
->n
; ++i
) {
6340 pma
->p
[i
].maff
= isl_multi_aff_scale_multi_val(pma
->p
[i
].maff
,
6341 isl_multi_val_copy(mv
));
6342 if (!pma
->p
[i
].maff
)
6346 isl_multi_val_free(mv
);
6349 isl_multi_val_free(mv
);
6350 isl_pw_multi_aff_free(pma
);
6354 /* This function is called for each entry of an isl_union_pw_multi_aff.
6355 * If the space of the entry matches that of data->mv,
6356 * then apply isl_pw_multi_aff_scale_multi_val and return the result.
6357 * Otherwise, return an empty isl_pw_multi_aff.
6359 static __isl_give isl_pw_multi_aff
*union_pw_multi_aff_scale_multi_val_entry(
6360 __isl_take isl_pw_multi_aff
*pma
, void *user
)
6362 isl_multi_val
*mv
= user
;
6366 if (!isl_space_tuple_is_equal(pma
->dim
, isl_dim_out
,
6367 mv
->space
, isl_dim_set
)) {
6368 isl_space
*space
= isl_pw_multi_aff_get_space(pma
);
6369 isl_pw_multi_aff_free(pma
);
6370 return isl_pw_multi_aff_empty(space
);
6373 return isl_pw_multi_aff_scale_multi_val(pma
, isl_multi_val_copy(mv
));
6376 /* Scale the elements of "upma" by the corresponding elements of "mv",
6377 * for those entries that match the space of "mv".
6379 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_scale_multi_val(
6380 __isl_take isl_union_pw_multi_aff
*upma
, __isl_take isl_multi_val
*mv
)
6382 upma
= isl_union_pw_multi_aff_align_params(upma
,
6383 isl_multi_val_get_space(mv
));
6384 mv
= isl_multi_val_align_params(mv
,
6385 isl_union_pw_multi_aff_get_space(upma
));
6389 return isl_union_pw_multi_aff_transform(upma
,
6390 &union_pw_multi_aff_scale_multi_val_entry
, mv
);
6392 isl_multi_val_free(mv
);
6395 isl_multi_val_free(mv
);
6396 isl_union_pw_multi_aff_free(upma
);
6400 /* Construct and return a piecewise multi affine expression
6401 * in the given space with value zero in each of the output dimensions and
6402 * a universe domain.
6404 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_zero(__isl_take isl_space
*space
)
6406 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_zero(space
));
6409 /* Construct and return a piecewise multi affine expression
6410 * that is equal to the given piecewise affine expression.
6412 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_pw_aff(
6413 __isl_take isl_pw_aff
*pa
)
6417 isl_pw_multi_aff
*pma
;
6422 space
= isl_pw_aff_get_space(pa
);
6423 pma
= isl_pw_multi_aff_alloc_size(space
, pa
->n
);
6425 for (i
= 0; i
< pa
->n
; ++i
) {
6429 set
= isl_set_copy(pa
->p
[i
].set
);
6430 ma
= isl_multi_aff_from_aff(isl_aff_copy(pa
->p
[i
].aff
));
6431 pma
= isl_pw_multi_aff_add_piece(pma
, set
, ma
);
6434 isl_pw_aff_free(pa
);
6438 /* Construct a set or map mapping the shared (parameter) domain
6439 * of the piecewise affine expressions to the range of "mpa"
6440 * with each dimension in the range equated to the
6441 * corresponding piecewise affine expression.
6443 static __isl_give isl_map
*map_from_multi_pw_aff(
6444 __isl_take isl_multi_pw_aff
*mpa
)
6453 if (isl_space_dim(mpa
->space
, isl_dim_out
) != mpa
->n
)
6454 isl_die(isl_multi_pw_aff_get_ctx(mpa
), isl_error_internal
,
6455 "invalid space", goto error
);
6457 space
= isl_multi_pw_aff_get_domain_space(mpa
);
6458 map
= isl_map_universe(isl_space_from_domain(space
));
6460 for (i
= 0; i
< mpa
->n
; ++i
) {
6464 pa
= isl_pw_aff_copy(mpa
->u
.p
[i
]);
6465 map_i
= map_from_pw_aff(pa
);
6467 map
= isl_map_flat_range_product(map
, map_i
);
6470 map
= isl_map_reset_space(map
, isl_multi_pw_aff_get_space(mpa
));
6472 isl_multi_pw_aff_free(mpa
);
6475 isl_multi_pw_aff_free(mpa
);
6479 /* Construct a map mapping the shared domain
6480 * of the piecewise affine expressions to the range of "mpa"
6481 * with each dimension in the range equated to the
6482 * corresponding piecewise affine expression.
6484 __isl_give isl_map
*isl_map_from_multi_pw_aff(__isl_take isl_multi_pw_aff
*mpa
)
6488 if (isl_space_is_set(mpa
->space
))
6489 isl_die(isl_multi_pw_aff_get_ctx(mpa
), isl_error_internal
,
6490 "space of input is not a map", goto error
);
6492 return map_from_multi_pw_aff(mpa
);
6494 isl_multi_pw_aff_free(mpa
);
6498 /* Construct a set mapping the shared parameter domain
6499 * of the piecewise affine expressions to the space of "mpa"
6500 * with each dimension in the range equated to the
6501 * corresponding piecewise affine expression.
6503 __isl_give isl_set
*isl_set_from_multi_pw_aff(__isl_take isl_multi_pw_aff
*mpa
)
6507 if (!isl_space_is_set(mpa
->space
))
6508 isl_die(isl_multi_pw_aff_get_ctx(mpa
), isl_error_internal
,
6509 "space of input is not a set", goto error
);
6511 return map_from_multi_pw_aff(mpa
);
6513 isl_multi_pw_aff_free(mpa
);
6517 /* Construct and return a piecewise multi affine expression
6518 * that is equal to the given multi piecewise affine expression
6519 * on the shared domain of the piecewise affine expressions,
6520 * in the special case of a 0D multi piecewise affine expression.
6522 * Create a piecewise multi affine expression with the explicit domain of
6523 * the 0D multi piecewise affine expression as domain.
6525 static __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_multi_pw_aff_0D(
6526 __isl_take isl_multi_pw_aff
*mpa
)
6532 space
= isl_multi_pw_aff_get_space(mpa
);
6533 dom
= isl_multi_pw_aff_get_explicit_domain(mpa
);
6534 isl_multi_pw_aff_free(mpa
);
6536 ma
= isl_multi_aff_zero(space
);
6537 return isl_pw_multi_aff_alloc(dom
, ma
);
6540 /* Construct and return a piecewise multi affine expression
6541 * that is equal to the given multi piecewise affine expression
6542 * on the shared domain of the piecewise affine expressions.
6544 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_multi_pw_aff(
6545 __isl_take isl_multi_pw_aff
*mpa
)
6550 isl_pw_multi_aff
*pma
;
6556 return isl_pw_multi_aff_from_multi_pw_aff_0D(mpa
);
6558 space
= isl_multi_pw_aff_get_space(mpa
);
6559 pa
= isl_multi_pw_aff_get_pw_aff(mpa
, 0);
6560 pma
= isl_pw_multi_aff_from_pw_aff(pa
);
6562 for (i
= 1; i
< mpa
->n
; ++i
) {
6563 isl_pw_multi_aff
*pma_i
;
6565 pa
= isl_multi_pw_aff_get_pw_aff(mpa
, i
);
6566 pma_i
= isl_pw_multi_aff_from_pw_aff(pa
);
6567 pma
= isl_pw_multi_aff_range_product(pma
, pma_i
);
6570 pma
= isl_pw_multi_aff_reset_space(pma
, space
);
6572 isl_multi_pw_aff_free(mpa
);
6576 /* Construct and return a multi piecewise affine expression
6577 * that is equal to the given multi affine expression.
6579 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_from_multi_aff(
6580 __isl_take isl_multi_aff
*ma
)
6583 isl_multi_pw_aff
*mpa
;
6588 n
= isl_multi_aff_dim(ma
, isl_dim_out
);
6589 mpa
= isl_multi_pw_aff_alloc(isl_multi_aff_get_space(ma
));
6591 for (i
= 0; i
< n
; ++i
) {
6594 pa
= isl_pw_aff_from_aff(isl_multi_aff_get_aff(ma
, i
));
6595 mpa
= isl_multi_pw_aff_set_pw_aff(mpa
, i
, pa
);
6598 isl_multi_aff_free(ma
);
6602 /* Construct and return a multi piecewise affine expression
6603 * that is equal to the given piecewise multi affine expression.
6605 * If the resulting multi piecewise affine expression has
6606 * an explicit domain, then assign it the domain of the input.
6607 * In other cases, the domain is stored in the individual elements.
6609 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_from_pw_multi_aff(
6610 __isl_take isl_pw_multi_aff
*pma
)
6614 isl_multi_pw_aff
*mpa
;
6619 n
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
6620 space
= isl_pw_multi_aff_get_space(pma
);
6621 mpa
= isl_multi_pw_aff_alloc(space
);
6623 for (i
= 0; i
< n
; ++i
) {
6626 pa
= isl_pw_multi_aff_get_pw_aff(pma
, i
);
6627 mpa
= isl_multi_pw_aff_set_pw_aff(mpa
, i
, pa
);
6629 if (isl_multi_pw_aff_has_explicit_domain(mpa
)) {
6632 dom
= isl_pw_multi_aff_domain(isl_pw_multi_aff_copy(pma
));
6633 mpa
= isl_multi_pw_aff_intersect_domain(mpa
, dom
);
6636 isl_pw_multi_aff_free(pma
);
6640 /* Do "pa1" and "pa2" represent the same function?
6642 * We first check if they are obviously equal.
6643 * If not, we convert them to maps and check if those are equal.
6645 * If "pa1" or "pa2" contain any NaNs, then they are considered
6646 * not to be the same. A NaN is not equal to anything, not even
6649 isl_bool
isl_pw_aff_is_equal(__isl_keep isl_pw_aff
*pa1
,
6650 __isl_keep isl_pw_aff
*pa2
)
6654 isl_map
*map1
, *map2
;
6657 return isl_bool_error
;
6659 equal
= isl_pw_aff_plain_is_equal(pa1
, pa2
);
6660 if (equal
< 0 || equal
)
6662 has_nan
= either_involves_nan(pa1
, pa2
);
6664 return isl_bool_error
;
6666 return isl_bool_false
;
6668 map1
= map_from_pw_aff(isl_pw_aff_copy(pa1
));
6669 map2
= map_from_pw_aff(isl_pw_aff_copy(pa2
));
6670 equal
= isl_map_is_equal(map1
, map2
);
6677 /* Do "mpa1" and "mpa2" represent the same function?
6679 * Note that we cannot convert the entire isl_multi_pw_aff
6680 * to a map because the domains of the piecewise affine expressions
6681 * may not be the same.
6683 isl_bool
isl_multi_pw_aff_is_equal(__isl_keep isl_multi_pw_aff
*mpa1
,
6684 __isl_keep isl_multi_pw_aff
*mpa2
)
6687 isl_bool equal
, equal_params
;
6690 return isl_bool_error
;
6692 equal_params
= isl_space_has_equal_params(mpa1
->space
, mpa2
->space
);
6693 if (equal_params
< 0)
6694 return isl_bool_error
;
6695 if (!equal_params
) {
6696 if (!isl_space_has_named_params(mpa1
->space
))
6697 return isl_bool_false
;
6698 if (!isl_space_has_named_params(mpa2
->space
))
6699 return isl_bool_false
;
6700 mpa1
= isl_multi_pw_aff_copy(mpa1
);
6701 mpa2
= isl_multi_pw_aff_copy(mpa2
);
6702 mpa1
= isl_multi_pw_aff_align_params(mpa1
,
6703 isl_multi_pw_aff_get_space(mpa2
));
6704 mpa2
= isl_multi_pw_aff_align_params(mpa2
,
6705 isl_multi_pw_aff_get_space(mpa1
));
6706 equal
= isl_multi_pw_aff_is_equal(mpa1
, mpa2
);
6707 isl_multi_pw_aff_free(mpa1
);
6708 isl_multi_pw_aff_free(mpa2
);
6712 equal
= isl_space_is_equal(mpa1
->space
, mpa2
->space
);
6713 if (equal
< 0 || !equal
)
6716 for (i
= 0; i
< mpa1
->n
; ++i
) {
6717 equal
= isl_pw_aff_is_equal(mpa1
->u
.p
[i
], mpa2
->u
.p
[i
]);
6718 if (equal
< 0 || !equal
)
6722 return isl_bool_true
;
6725 /* Do "pma1" and "pma2" represent the same function?
6727 * First check if they are obviously equal.
6728 * If not, then convert them to maps and check if those are equal.
6730 * If "pa1" or "pa2" contain any NaNs, then they are considered
6731 * not to be the same. A NaN is not equal to anything, not even
6734 isl_bool
isl_pw_multi_aff_is_equal(__isl_keep isl_pw_multi_aff
*pma1
,
6735 __isl_keep isl_pw_multi_aff
*pma2
)
6739 isl_map
*map1
, *map2
;
6742 return isl_bool_error
;
6744 equal
= isl_pw_multi_aff_plain_is_equal(pma1
, pma2
);
6745 if (equal
< 0 || equal
)
6747 has_nan
= isl_pw_multi_aff_involves_nan(pma1
);
6748 if (has_nan
>= 0 && !has_nan
)
6749 has_nan
= isl_pw_multi_aff_involves_nan(pma2
);
6750 if (has_nan
< 0 || has_nan
)
6751 return isl_bool_not(has_nan
);
6753 map1
= isl_map_from_pw_multi_aff(isl_pw_multi_aff_copy(pma1
));
6754 map2
= isl_map_from_pw_multi_aff(isl_pw_multi_aff_copy(pma2
));
6755 equal
= isl_map_is_equal(map1
, map2
);
6762 /* Compute the pullback of "mpa" by the function represented by "ma".
6763 * In other words, plug in "ma" in "mpa".
6765 * The parameters of "mpa" and "ma" are assumed to have been aligned.
6767 * If "mpa" has an explicit domain, then it is this domain
6768 * that needs to undergo a pullback, i.e., a preimage.
6770 static __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_pullback_multi_aff_aligned(
6771 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_multi_aff
*ma
)
6774 isl_space
*space
= NULL
;
6776 mpa
= isl_multi_pw_aff_cow(mpa
);
6780 space
= isl_space_join(isl_multi_aff_get_space(ma
),
6781 isl_multi_pw_aff_get_space(mpa
));
6785 for (i
= 0; i
< mpa
->n
; ++i
) {
6786 mpa
->u
.p
[i
] = isl_pw_aff_pullback_multi_aff(mpa
->u
.p
[i
],
6787 isl_multi_aff_copy(ma
));
6791 if (isl_multi_pw_aff_has_explicit_domain(mpa
)) {
6792 mpa
->u
.dom
= isl_set_preimage_multi_aff(mpa
->u
.dom
,
6793 isl_multi_aff_copy(ma
));
6798 isl_multi_aff_free(ma
);
6799 isl_space_free(mpa
->space
);
6803 isl_space_free(space
);
6804 isl_multi_pw_aff_free(mpa
);
6805 isl_multi_aff_free(ma
);
6809 /* Compute the pullback of "mpa" by the function represented by "ma".
6810 * In other words, plug in "ma" in "mpa".
6812 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_pullback_multi_aff(
6813 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_multi_aff
*ma
)
6815 isl_bool equal_params
;
6819 equal_params
= isl_space_has_equal_params(mpa
->space
, ma
->space
);
6820 if (equal_params
< 0)
6823 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa
, ma
);
6824 mpa
= isl_multi_pw_aff_align_params(mpa
, isl_multi_aff_get_space(ma
));
6825 ma
= isl_multi_aff_align_params(ma
, isl_multi_pw_aff_get_space(mpa
));
6826 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa
, ma
);
6828 isl_multi_pw_aff_free(mpa
);
6829 isl_multi_aff_free(ma
);
6833 /* Compute the pullback of "mpa" by the function represented by "pma".
6834 * In other words, plug in "pma" in "mpa".
6836 * The parameters of "mpa" and "mpa" are assumed to have been aligned.
6838 * If "mpa" has an explicit domain, then it is this domain
6839 * that needs to undergo a pullback, i.e., a preimage.
6841 static __isl_give isl_multi_pw_aff
*
6842 isl_multi_pw_aff_pullback_pw_multi_aff_aligned(
6843 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_pw_multi_aff
*pma
)
6846 isl_space
*space
= NULL
;
6848 mpa
= isl_multi_pw_aff_cow(mpa
);
6852 space
= isl_space_join(isl_pw_multi_aff_get_space(pma
),
6853 isl_multi_pw_aff_get_space(mpa
));
6855 for (i
= 0; i
< mpa
->n
; ++i
) {
6856 mpa
->u
.p
[i
] = isl_pw_aff_pullback_pw_multi_aff_aligned(
6857 mpa
->u
.p
[i
], isl_pw_multi_aff_copy(pma
));
6861 if (isl_multi_pw_aff_has_explicit_domain(mpa
)) {
6862 mpa
->u
.dom
= isl_set_preimage_pw_multi_aff(mpa
->u
.dom
,
6863 isl_pw_multi_aff_copy(pma
));
6868 isl_pw_multi_aff_free(pma
);
6869 isl_space_free(mpa
->space
);
6873 isl_space_free(space
);
6874 isl_multi_pw_aff_free(mpa
);
6875 isl_pw_multi_aff_free(pma
);
6879 /* Compute the pullback of "mpa" by the function represented by "pma".
6880 * In other words, plug in "pma" in "mpa".
6882 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_pullback_pw_multi_aff(
6883 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_pw_multi_aff
*pma
)
6885 isl_bool equal_params
;
6889 equal_params
= isl_space_has_equal_params(mpa
->space
, pma
->dim
);
6890 if (equal_params
< 0)
6893 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa
, pma
);
6894 mpa
= isl_multi_pw_aff_align_params(mpa
,
6895 isl_pw_multi_aff_get_space(pma
));
6896 pma
= isl_pw_multi_aff_align_params(pma
,
6897 isl_multi_pw_aff_get_space(mpa
));
6898 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa
, pma
);
6900 isl_multi_pw_aff_free(mpa
);
6901 isl_pw_multi_aff_free(pma
);
6905 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
6906 * with the domain of "aff". The domain of the result is the same
6908 * "mpa" and "aff" are assumed to have been aligned.
6910 * We first extract the parametric constant from "aff", defined
6911 * over the correct domain.
6912 * Then we add the appropriate combinations of the members of "mpa".
6913 * Finally, we add the integer divisions through recursive calls.
6915 static __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_aff_aligned(
6916 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_aff
*aff
)
6924 n_in
= isl_aff_dim(aff
, isl_dim_in
);
6925 n_div
= isl_aff_dim(aff
, isl_dim_div
);
6927 space
= isl_space_domain(isl_multi_pw_aff_get_space(mpa
));
6928 tmp
= isl_aff_copy(aff
);
6929 tmp
= isl_aff_drop_dims(tmp
, isl_dim_div
, 0, n_div
);
6930 tmp
= isl_aff_drop_dims(tmp
, isl_dim_in
, 0, n_in
);
6931 tmp
= isl_aff_add_dims(tmp
, isl_dim_in
,
6932 isl_space_dim(space
, isl_dim_set
));
6933 tmp
= isl_aff_reset_domain_space(tmp
, space
);
6934 pa
= isl_pw_aff_from_aff(tmp
);
6936 for (i
= 0; i
< n_in
; ++i
) {
6939 if (!isl_aff_involves_dims(aff
, isl_dim_in
, i
, 1))
6941 v
= isl_aff_get_coefficient_val(aff
, isl_dim_in
, i
);
6942 pa_i
= isl_multi_pw_aff_get_pw_aff(mpa
, i
);
6943 pa_i
= isl_pw_aff_scale_val(pa_i
, v
);
6944 pa
= isl_pw_aff_add(pa
, pa_i
);
6947 for (i
= 0; i
< n_div
; ++i
) {
6951 if (!isl_aff_involves_dims(aff
, isl_dim_div
, i
, 1))
6953 div
= isl_aff_get_div(aff
, i
);
6954 pa_i
= isl_multi_pw_aff_apply_aff_aligned(
6955 isl_multi_pw_aff_copy(mpa
), div
);
6956 pa_i
= isl_pw_aff_floor(pa_i
);
6957 v
= isl_aff_get_coefficient_val(aff
, isl_dim_div
, i
);
6958 pa_i
= isl_pw_aff_scale_val(pa_i
, v
);
6959 pa
= isl_pw_aff_add(pa
, pa_i
);
6962 isl_multi_pw_aff_free(mpa
);
6968 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
6969 * with the domain of "aff". The domain of the result is the same
6972 __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_aff(
6973 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_aff
*aff
)
6975 isl_bool equal_params
;
6979 equal_params
= isl_space_has_equal_params(aff
->ls
->dim
, mpa
->space
);
6980 if (equal_params
< 0)
6983 return isl_multi_pw_aff_apply_aff_aligned(mpa
, aff
);
6985 aff
= isl_aff_align_params(aff
, isl_multi_pw_aff_get_space(mpa
));
6986 mpa
= isl_multi_pw_aff_align_params(mpa
, isl_aff_get_space(aff
));
6988 return isl_multi_pw_aff_apply_aff_aligned(mpa
, aff
);
6991 isl_multi_pw_aff_free(mpa
);
6995 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
6996 * with the domain of "pa". The domain of the result is the same
6998 * "mpa" and "pa" are assumed to have been aligned.
7000 * We consider each piece in turn. Note that the domains of the
7001 * pieces are assumed to be disjoint and they remain disjoint
7002 * after taking the preimage (over the same function).
7004 static __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_pw_aff_aligned(
7005 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_pw_aff
*pa
)
7014 space
= isl_space_join(isl_multi_pw_aff_get_space(mpa
),
7015 isl_pw_aff_get_space(pa
));
7016 res
= isl_pw_aff_empty(space
);
7018 for (i
= 0; i
< pa
->n
; ++i
) {
7022 pa_i
= isl_multi_pw_aff_apply_aff_aligned(
7023 isl_multi_pw_aff_copy(mpa
),
7024 isl_aff_copy(pa
->p
[i
].aff
));
7025 domain
= isl_set_copy(pa
->p
[i
].set
);
7026 domain
= isl_set_preimage_multi_pw_aff(domain
,
7027 isl_multi_pw_aff_copy(mpa
));
7028 pa_i
= isl_pw_aff_intersect_domain(pa_i
, domain
);
7029 res
= isl_pw_aff_add_disjoint(res
, pa_i
);
7032 isl_pw_aff_free(pa
);
7033 isl_multi_pw_aff_free(mpa
);
7036 isl_pw_aff_free(pa
);
7037 isl_multi_pw_aff_free(mpa
);
7041 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
7042 * with the domain of "pa". The domain of the result is the same
7045 __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_pw_aff(
7046 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_pw_aff
*pa
)
7048 isl_bool equal_params
;
7052 equal_params
= isl_space_has_equal_params(pa
->dim
, mpa
->space
);
7053 if (equal_params
< 0)
7056 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa
, pa
);
7058 pa
= isl_pw_aff_align_params(pa
, isl_multi_pw_aff_get_space(mpa
));
7059 mpa
= isl_multi_pw_aff_align_params(mpa
, isl_pw_aff_get_space(pa
));
7061 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa
, pa
);
7063 isl_pw_aff_free(pa
);
7064 isl_multi_pw_aff_free(mpa
);
7068 /* Compute the pullback of "pa" by the function represented by "mpa".
7069 * In other words, plug in "mpa" in "pa".
7070 * "pa" and "mpa" are assumed to have been aligned.
7072 * The pullback is computed by applying "pa" to "mpa".
7074 static __isl_give isl_pw_aff
*isl_pw_aff_pullback_multi_pw_aff_aligned(
7075 __isl_take isl_pw_aff
*pa
, __isl_take isl_multi_pw_aff
*mpa
)
7077 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa
, pa
);
7080 /* Compute the pullback of "pa" by the function represented by "mpa".
7081 * In other words, plug in "mpa" in "pa".
7083 * The pullback is computed by applying "pa" to "mpa".
7085 __isl_give isl_pw_aff
*isl_pw_aff_pullback_multi_pw_aff(
7086 __isl_take isl_pw_aff
*pa
, __isl_take isl_multi_pw_aff
*mpa
)
7088 return isl_multi_pw_aff_apply_pw_aff(mpa
, pa
);
7091 /* Compute the pullback of "mpa1" by the function represented by "mpa2".
7092 * In other words, plug in "mpa2" in "mpa1".
7094 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7096 * We pullback each member of "mpa1" in turn.
7098 * If "mpa1" has an explicit domain, then it is this domain
7099 * that needs to undergo a pullback instead, i.e., a preimage.
7101 static __isl_give isl_multi_pw_aff
*
7102 isl_multi_pw_aff_pullback_multi_pw_aff_aligned(
7103 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
)
7106 isl_space
*space
= NULL
;
7108 mpa1
= isl_multi_pw_aff_cow(mpa1
);
7112 space
= isl_space_join(isl_multi_pw_aff_get_space(mpa2
),
7113 isl_multi_pw_aff_get_space(mpa1
));
7115 for (i
= 0; i
< mpa1
->n
; ++i
) {
7116 mpa1
->u
.p
[i
] = isl_pw_aff_pullback_multi_pw_aff_aligned(
7117 mpa1
->u
.p
[i
], isl_multi_pw_aff_copy(mpa2
));
7122 if (isl_multi_pw_aff_has_explicit_domain(mpa1
)) {
7123 mpa1
->u
.dom
= isl_set_preimage_multi_pw_aff(mpa1
->u
.dom
,
7124 isl_multi_pw_aff_copy(mpa2
));
7128 mpa1
= isl_multi_pw_aff_reset_space(mpa1
, space
);
7130 isl_multi_pw_aff_free(mpa2
);
7133 isl_space_free(space
);
7134 isl_multi_pw_aff_free(mpa1
);
7135 isl_multi_pw_aff_free(mpa2
);
7139 /* Compute the pullback of "mpa1" by the function represented by "mpa2".
7140 * In other words, plug in "mpa2" in "mpa1".
7142 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_pullback_multi_pw_aff(
7143 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
)
7145 return isl_multi_pw_aff_align_params_multi_multi_and(mpa1
, mpa2
,
7146 &isl_multi_pw_aff_pullback_multi_pw_aff_aligned
);
7149 /* Align the parameters of "mpa1" and "mpa2", check that the ranges
7150 * of "mpa1" and "mpa2" live in the same space, construct map space
7151 * between the domain spaces of "mpa1" and "mpa2" and call "order"
7152 * with this map space as extract argument.
7154 static __isl_give isl_map
*isl_multi_pw_aff_order_map(
7155 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
,
7156 __isl_give isl_map
*(*order
)(__isl_keep isl_multi_pw_aff
*mpa1
,
7157 __isl_keep isl_multi_pw_aff
*mpa2
, __isl_take isl_space
*space
))
7160 isl_space
*space1
, *space2
;
7163 mpa1
= isl_multi_pw_aff_align_params(mpa1
,
7164 isl_multi_pw_aff_get_space(mpa2
));
7165 mpa2
= isl_multi_pw_aff_align_params(mpa2
,
7166 isl_multi_pw_aff_get_space(mpa1
));
7169 match
= isl_space_tuple_is_equal(mpa1
->space
, isl_dim_out
,
7170 mpa2
->space
, isl_dim_out
);
7174 isl_die(isl_multi_pw_aff_get_ctx(mpa1
), isl_error_invalid
,
7175 "range spaces don't match", goto error
);
7176 space1
= isl_space_domain(isl_multi_pw_aff_get_space(mpa1
));
7177 space2
= isl_space_domain(isl_multi_pw_aff_get_space(mpa2
));
7178 space1
= isl_space_map_from_domain_and_range(space1
, space2
);
7180 res
= order(mpa1
, mpa2
, space1
);
7181 isl_multi_pw_aff_free(mpa1
);
7182 isl_multi_pw_aff_free(mpa2
);
7185 isl_multi_pw_aff_free(mpa1
);
7186 isl_multi_pw_aff_free(mpa2
);
7190 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7191 * where the function values are equal. "space" is the space of the result.
7192 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7194 * "mpa1" and "mpa2" are equal when each of the pairs of elements
7195 * in the sequences are equal.
7197 static __isl_give isl_map
*isl_multi_pw_aff_eq_map_on_space(
7198 __isl_keep isl_multi_pw_aff
*mpa1
, __isl_keep isl_multi_pw_aff
*mpa2
,
7199 __isl_take isl_space
*space
)
7204 res
= isl_map_universe(space
);
7206 n
= isl_multi_pw_aff_dim(mpa1
, isl_dim_out
);
7207 for (i
= 0; i
< n
; ++i
) {
7208 isl_pw_aff
*pa1
, *pa2
;
7211 pa1
= isl_multi_pw_aff_get_pw_aff(mpa1
, i
);
7212 pa2
= isl_multi_pw_aff_get_pw_aff(mpa2
, i
);
7213 map
= isl_pw_aff_eq_map(pa1
, pa2
);
7214 res
= isl_map_intersect(res
, map
);
7220 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7221 * where the function values are equal.
7223 __isl_give isl_map
*isl_multi_pw_aff_eq_map(__isl_take isl_multi_pw_aff
*mpa1
,
7224 __isl_take isl_multi_pw_aff
*mpa2
)
7226 return isl_multi_pw_aff_order_map(mpa1
, mpa2
,
7227 &isl_multi_pw_aff_eq_map_on_space
);
7230 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7231 * where the function values of "mpa1" is lexicographically satisfies "base"
7232 * compared to that of "mpa2". "space" is the space of the result.
7233 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7235 * "mpa1" lexicographically satisfies "base" compared to "mpa2"
7236 * if its i-th element satisfies "base" when compared to
7237 * the i-th element of "mpa2" while all previous elements are
7240 static __isl_give isl_map
*isl_multi_pw_aff_lex_map_on_space(
7241 __isl_keep isl_multi_pw_aff
*mpa1
, __isl_keep isl_multi_pw_aff
*mpa2
,
7242 __isl_give isl_map
*(*base
)(__isl_take isl_pw_aff
*pa1
,
7243 __isl_take isl_pw_aff
*pa2
),
7244 __isl_take isl_space
*space
)
7247 isl_map
*res
, *rest
;
7249 res
= isl_map_empty(isl_space_copy(space
));
7250 rest
= isl_map_universe(space
);
7252 n
= isl_multi_pw_aff_dim(mpa1
, isl_dim_out
);
7253 for (i
= 0; i
< n
; ++i
) {
7254 isl_pw_aff
*pa1
, *pa2
;
7257 pa1
= isl_multi_pw_aff_get_pw_aff(mpa1
, i
);
7258 pa2
= isl_multi_pw_aff_get_pw_aff(mpa2
, i
);
7259 map
= base(pa1
, pa2
);
7260 map
= isl_map_intersect(map
, isl_map_copy(rest
));
7261 res
= isl_map_union(res
, map
);
7266 pa1
= isl_multi_pw_aff_get_pw_aff(mpa1
, i
);
7267 pa2
= isl_multi_pw_aff_get_pw_aff(mpa2
, i
);
7268 map
= isl_pw_aff_eq_map(pa1
, pa2
);
7269 rest
= isl_map_intersect(rest
, map
);
7276 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7277 * where the function value of "mpa1" is lexicographically less than that
7278 * of "mpa2". "space" is the space of the result.
7279 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7281 * "mpa1" is less than "mpa2" if its i-th element is smaller
7282 * than the i-th element of "mpa2" while all previous elements are
7285 __isl_give isl_map
*isl_multi_pw_aff_lex_lt_map_on_space(
7286 __isl_keep isl_multi_pw_aff
*mpa1
, __isl_keep isl_multi_pw_aff
*mpa2
,
7287 __isl_take isl_space
*space
)
7289 return isl_multi_pw_aff_lex_map_on_space(mpa1
, mpa2
,
7290 &isl_pw_aff_lt_map
, space
);
7293 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7294 * where the function value of "mpa1" is lexicographically less than that
7297 __isl_give isl_map
*isl_multi_pw_aff_lex_lt_map(
7298 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
)
7300 return isl_multi_pw_aff_order_map(mpa1
, mpa2
,
7301 &isl_multi_pw_aff_lex_lt_map_on_space
);
7304 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7305 * where the function value of "mpa1" is lexicographically greater than that
7306 * of "mpa2". "space" is the space of the result.
7307 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7309 * "mpa1" is greater than "mpa2" if its i-th element is greater
7310 * than the i-th element of "mpa2" while all previous elements are
7313 __isl_give isl_map
*isl_multi_pw_aff_lex_gt_map_on_space(
7314 __isl_keep isl_multi_pw_aff
*mpa1
, __isl_keep isl_multi_pw_aff
*mpa2
,
7315 __isl_take isl_space
*space
)
7317 return isl_multi_pw_aff_lex_map_on_space(mpa1
, mpa2
,
7318 &isl_pw_aff_gt_map
, space
);
7321 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7322 * where the function value of "mpa1" is lexicographically greater than that
7325 __isl_give isl_map
*isl_multi_pw_aff_lex_gt_map(
7326 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
)
7328 return isl_multi_pw_aff_order_map(mpa1
, mpa2
,
7329 &isl_multi_pw_aff_lex_gt_map_on_space
);
7332 /* Compare two isl_affs.
7334 * Return -1 if "aff1" is "smaller" than "aff2", 1 if "aff1" is "greater"
7335 * than "aff2" and 0 if they are equal.
7337 * The order is fairly arbitrary. We do consider expressions that only involve
7338 * earlier dimensions as "smaller".
7340 int isl_aff_plain_cmp(__isl_keep isl_aff
*aff1
, __isl_keep isl_aff
*aff2
)
7353 cmp
= isl_local_space_cmp(aff1
->ls
, aff2
->ls
);
7357 last1
= isl_seq_last_non_zero(aff1
->v
->el
+ 1, aff1
->v
->size
- 1);
7358 last2
= isl_seq_last_non_zero(aff2
->v
->el
+ 1, aff1
->v
->size
- 1);
7360 return last1
- last2
;
7362 return isl_seq_cmp(aff1
->v
->el
, aff2
->v
->el
, aff1
->v
->size
);
7365 /* Compare two isl_pw_affs.
7367 * Return -1 if "pa1" is "smaller" than "pa2", 1 if "pa1" is "greater"
7368 * than "pa2" and 0 if they are equal.
7370 * The order is fairly arbitrary. We do consider expressions that only involve
7371 * earlier dimensions as "smaller".
7373 int isl_pw_aff_plain_cmp(__isl_keep isl_pw_aff
*pa1
,
7374 __isl_keep isl_pw_aff
*pa2
)
7387 cmp
= isl_space_cmp(pa1
->dim
, pa2
->dim
);
7391 if (pa1
->n
!= pa2
->n
)
7392 return pa1
->n
- pa2
->n
;
7394 for (i
= 0; i
< pa1
->n
; ++i
) {
7395 cmp
= isl_set_plain_cmp(pa1
->p
[i
].set
, pa2
->p
[i
].set
);
7398 cmp
= isl_aff_plain_cmp(pa1
->p
[i
].aff
, pa2
->p
[i
].aff
);
7406 /* Return a piecewise affine expression that is equal to "v" on "domain".
7408 __isl_give isl_pw_aff
*isl_pw_aff_val_on_domain(__isl_take isl_set
*domain
,
7409 __isl_take isl_val
*v
)
7412 isl_local_space
*ls
;
7415 space
= isl_set_get_space(domain
);
7416 ls
= isl_local_space_from_space(space
);
7417 aff
= isl_aff_val_on_domain(ls
, v
);
7419 return isl_pw_aff_alloc(domain
, aff
);
7422 /* Return a multi affine expression that is equal to "mv" on domain
7425 __isl_give isl_multi_aff
*isl_multi_aff_multi_val_on_space(
7426 __isl_take isl_space
*space
, __isl_take isl_multi_val
*mv
)
7430 isl_local_space
*ls
;
7436 n
= isl_multi_val_dim(mv
, isl_dim_set
);
7437 space2
= isl_multi_val_get_space(mv
);
7438 space2
= isl_space_align_params(space2
, isl_space_copy(space
));
7439 space
= isl_space_align_params(space
, isl_space_copy(space2
));
7440 space
= isl_space_map_from_domain_and_range(space
, space2
);
7441 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
7442 ls
= isl_local_space_from_space(isl_space_domain(space
));
7443 for (i
= 0; i
< n
; ++i
) {
7447 v
= isl_multi_val_get_val(mv
, i
);
7448 aff
= isl_aff_val_on_domain(isl_local_space_copy(ls
), v
);
7449 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
7451 isl_local_space_free(ls
);
7453 isl_multi_val_free(mv
);
7456 isl_space_free(space
);
7457 isl_multi_val_free(mv
);
7461 /* Return a piecewise multi-affine expression
7462 * that is equal to "mv" on "domain".
7464 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_multi_val_on_domain(
7465 __isl_take isl_set
*domain
, __isl_take isl_multi_val
*mv
)
7470 space
= isl_set_get_space(domain
);
7471 ma
= isl_multi_aff_multi_val_on_space(space
, mv
);
7473 return isl_pw_multi_aff_alloc(domain
, ma
);
7476 /* Internal data structure for isl_union_pw_multi_aff_multi_val_on_domain.
7477 * mv is the value that should be attained on each domain set
7478 * res collects the results
7480 struct isl_union_pw_multi_aff_multi_val_on_domain_data
{
7482 isl_union_pw_multi_aff
*res
;
7485 /* Create an isl_pw_multi_aff equal to data->mv on "domain"
7486 * and add it to data->res.
7488 static isl_stat
pw_multi_aff_multi_val_on_domain(__isl_take isl_set
*domain
,
7491 struct isl_union_pw_multi_aff_multi_val_on_domain_data
*data
= user
;
7492 isl_pw_multi_aff
*pma
;
7495 mv
= isl_multi_val_copy(data
->mv
);
7496 pma
= isl_pw_multi_aff_multi_val_on_domain(domain
, mv
);
7497 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma
);
7499 return data
->res
? isl_stat_ok
: isl_stat_error
;
7502 /* Return a union piecewise multi-affine expression
7503 * that is equal to "mv" on "domain".
7505 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_multi_val_on_domain(
7506 __isl_take isl_union_set
*domain
, __isl_take isl_multi_val
*mv
)
7508 struct isl_union_pw_multi_aff_multi_val_on_domain_data data
;
7511 space
= isl_union_set_get_space(domain
);
7512 data
.res
= isl_union_pw_multi_aff_empty(space
);
7514 if (isl_union_set_foreach_set(domain
,
7515 &pw_multi_aff_multi_val_on_domain
, &data
) < 0)
7516 data
.res
= isl_union_pw_multi_aff_free(data
.res
);
7517 isl_union_set_free(domain
);
7518 isl_multi_val_free(mv
);
7522 /* Compute the pullback of data->pma by the function represented by "pma2",
7523 * provided the spaces match, and add the results to data->res.
7525 static isl_stat
pullback_entry(__isl_take isl_pw_multi_aff
*pma2
, void *user
)
7527 struct isl_union_pw_multi_aff_bin_data
*data
= user
;
7529 if (!isl_space_tuple_is_equal(data
->pma
->dim
, isl_dim_in
,
7530 pma2
->dim
, isl_dim_out
)) {
7531 isl_pw_multi_aff_free(pma2
);
7535 pma2
= isl_pw_multi_aff_pullback_pw_multi_aff(
7536 isl_pw_multi_aff_copy(data
->pma
), pma2
);
7538 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma2
);
7540 return isl_stat_error
;
7545 /* Compute the pullback of "upma1" by the function represented by "upma2".
7547 __isl_give isl_union_pw_multi_aff
*
7548 isl_union_pw_multi_aff_pullback_union_pw_multi_aff(
7549 __isl_take isl_union_pw_multi_aff
*upma1
,
7550 __isl_take isl_union_pw_multi_aff
*upma2
)
7552 return bin_op(upma1
, upma2
, &pullback_entry
);
7555 /* Check that the domain space of "upa" matches "space".
7557 * This function is called from isl_multi_union_pw_aff_set_union_pw_aff and
7558 * can in principle never fail since the space "space" is that
7559 * of the isl_multi_union_pw_aff and is a set space such that
7560 * there is no domain space to match.
7562 * We check the parameters and double-check that "space" is
7563 * indeed that of a set.
7565 static isl_stat
isl_union_pw_aff_check_match_domain_space(
7566 __isl_keep isl_union_pw_aff
*upa
, __isl_keep isl_space
*space
)
7568 isl_space
*upa_space
;
7572 return isl_stat_error
;
7574 match
= isl_space_is_set(space
);
7576 return isl_stat_error
;
7578 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
7579 "expecting set space", return -1);
7581 upa_space
= isl_union_pw_aff_get_space(upa
);
7582 match
= isl_space_has_equal_params(space
, upa_space
);
7586 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
7587 "parameters don't match", goto error
);
7589 isl_space_free(upa_space
);
7592 isl_space_free(upa_space
);
7593 return isl_stat_error
;
7596 /* Do the parameters of "upa" match those of "space"?
7598 static isl_bool
isl_union_pw_aff_matching_params(
7599 __isl_keep isl_union_pw_aff
*upa
, __isl_keep isl_space
*space
)
7601 isl_space
*upa_space
;
7605 return isl_bool_error
;
7607 upa_space
= isl_union_pw_aff_get_space(upa
);
7609 match
= isl_space_has_equal_params(space
, upa_space
);
7611 isl_space_free(upa_space
);
7615 /* Internal data structure for isl_union_pw_aff_reset_domain_space.
7616 * space represents the new parameters.
7617 * res collects the results.
7619 struct isl_union_pw_aff_reset_params_data
{
7621 isl_union_pw_aff
*res
;
7624 /* Replace the parameters of "pa" by data->space and
7625 * add the result to data->res.
7627 static isl_stat
reset_params(__isl_take isl_pw_aff
*pa
, void *user
)
7629 struct isl_union_pw_aff_reset_params_data
*data
= user
;
7632 space
= isl_pw_aff_get_space(pa
);
7633 space
= isl_space_replace_params(space
, data
->space
);
7634 pa
= isl_pw_aff_reset_space(pa
, space
);
7635 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
7637 return data
->res
? isl_stat_ok
: isl_stat_error
;
7640 /* Replace the domain space of "upa" by "space".
7641 * Since a union expression does not have a (single) domain space,
7642 * "space" is necessarily a parameter space.
7644 * Since the order and the names of the parameters determine
7645 * the hash value, we need to create a new hash table.
7647 static __isl_give isl_union_pw_aff
*isl_union_pw_aff_reset_domain_space(
7648 __isl_take isl_union_pw_aff
*upa
, __isl_take isl_space
*space
)
7650 struct isl_union_pw_aff_reset_params_data data
= { space
};
7653 match
= isl_union_pw_aff_matching_params(upa
, space
);
7655 upa
= isl_union_pw_aff_free(upa
);
7657 isl_space_free(space
);
7661 data
.res
= isl_union_pw_aff_empty(isl_space_copy(space
));
7662 if (isl_union_pw_aff_foreach_pw_aff(upa
, &reset_params
, &data
) < 0)
7663 data
.res
= isl_union_pw_aff_free(data
.res
);
7665 isl_union_pw_aff_free(upa
);
7666 isl_space_free(space
);
7670 /* Return the floor of "pa".
7672 static __isl_give isl_pw_aff
*floor_entry(__isl_take isl_pw_aff
*pa
, void *user
)
7674 return isl_pw_aff_floor(pa
);
7677 /* Given f, return floor(f).
7679 __isl_give isl_union_pw_aff
*isl_union_pw_aff_floor(
7680 __isl_take isl_union_pw_aff
*upa
)
7682 return isl_union_pw_aff_transform_inplace(upa
, &floor_entry
, NULL
);
7687 * upa mod m = upa - m * floor(upa/m)
7689 * with m an integer value.
7691 __isl_give isl_union_pw_aff
*isl_union_pw_aff_mod_val(
7692 __isl_take isl_union_pw_aff
*upa
, __isl_take isl_val
*m
)
7694 isl_union_pw_aff
*res
;
7699 if (!isl_val_is_int(m
))
7700 isl_die(isl_val_get_ctx(m
), isl_error_invalid
,
7701 "expecting integer modulo", goto error
);
7702 if (!isl_val_is_pos(m
))
7703 isl_die(isl_val_get_ctx(m
), isl_error_invalid
,
7704 "expecting positive modulo", goto error
);
7706 res
= isl_union_pw_aff_copy(upa
);
7707 upa
= isl_union_pw_aff_scale_down_val(upa
, isl_val_copy(m
));
7708 upa
= isl_union_pw_aff_floor(upa
);
7709 upa
= isl_union_pw_aff_scale_val(upa
, m
);
7710 res
= isl_union_pw_aff_sub(res
, upa
);
7715 isl_union_pw_aff_free(upa
);
7719 /* Internal data structure for isl_union_pw_multi_aff_get_union_pw_aff.
7720 * pos is the output position that needs to be extracted.
7721 * res collects the results.
7723 struct isl_union_pw_multi_aff_get_union_pw_aff_data
{
7725 isl_union_pw_aff
*res
;
7728 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma"
7729 * (assuming it has such a dimension) and add it to data->res.
7731 static isl_stat
get_union_pw_aff(__isl_take isl_pw_multi_aff
*pma
, void *user
)
7733 struct isl_union_pw_multi_aff_get_union_pw_aff_data
*data
= user
;
7738 return isl_stat_error
;
7740 n_out
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
7741 if (data
->pos
>= n_out
) {
7742 isl_pw_multi_aff_free(pma
);
7746 pa
= isl_pw_multi_aff_get_pw_aff(pma
, data
->pos
);
7747 isl_pw_multi_aff_free(pma
);
7749 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
7751 return data
->res
? isl_stat_ok
: isl_stat_error
;
7754 /* Extract an isl_union_pw_aff corresponding to
7755 * output dimension "pos" of "upma".
7757 __isl_give isl_union_pw_aff
*isl_union_pw_multi_aff_get_union_pw_aff(
7758 __isl_keep isl_union_pw_multi_aff
*upma
, int pos
)
7760 struct isl_union_pw_multi_aff_get_union_pw_aff_data data
;
7767 isl_die(isl_union_pw_multi_aff_get_ctx(upma
), isl_error_invalid
,
7768 "cannot extract at negative position", return NULL
);
7770 space
= isl_union_pw_multi_aff_get_space(upma
);
7771 data
.res
= isl_union_pw_aff_empty(space
);
7773 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma
,
7774 &get_union_pw_aff
, &data
) < 0)
7775 data
.res
= isl_union_pw_aff_free(data
.res
);
7780 /* Return a union piecewise affine expression
7781 * that is equal to "aff" on "domain".
7783 __isl_give isl_union_pw_aff
*isl_union_pw_aff_aff_on_domain(
7784 __isl_take isl_union_set
*domain
, __isl_take isl_aff
*aff
)
7788 pa
= isl_pw_aff_from_aff(aff
);
7789 return isl_union_pw_aff_pw_aff_on_domain(domain
, pa
);
7792 /* Return a union piecewise affine expression
7793 * that is equal to the parameter identified by "id" on "domain".
7795 * Make sure the parameter appears in the space passed to
7796 * isl_aff_param_on_domain_space_id.
7798 __isl_give isl_union_pw_aff
*isl_union_pw_aff_param_on_domain_id(
7799 __isl_take isl_union_set
*domain
, __isl_take isl_id
*id
)
7804 space
= isl_union_set_get_space(domain
);
7805 space
= isl_space_add_param_id(space
, isl_id_copy(id
));
7806 aff
= isl_aff_param_on_domain_space_id(space
, id
);
7807 return isl_union_pw_aff_aff_on_domain(domain
, aff
);
7810 /* Internal data structure for isl_union_pw_aff_pw_aff_on_domain.
7811 * "pa" is the piecewise symbolic value that the resulting isl_union_pw_aff
7813 * "res" collects the results.
7815 struct isl_union_pw_aff_pw_aff_on_domain_data
{
7817 isl_union_pw_aff
*res
;
7820 /* Construct a piecewise affine expression that is equal to data->pa
7821 * on "domain" and add the result to data->res.
7823 static isl_stat
pw_aff_on_domain(__isl_take isl_set
*domain
, void *user
)
7825 struct isl_union_pw_aff_pw_aff_on_domain_data
*data
= user
;
7829 pa
= isl_pw_aff_copy(data
->pa
);
7830 dim
= isl_set_dim(domain
, isl_dim_set
);
7831 pa
= isl_pw_aff_from_range(pa
);
7832 pa
= isl_pw_aff_add_dims(pa
, isl_dim_in
, dim
);
7833 pa
= isl_pw_aff_reset_domain_space(pa
, isl_set_get_space(domain
));
7834 pa
= isl_pw_aff_intersect_domain(pa
, domain
);
7835 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
7837 return data
->res
? isl_stat_ok
: isl_stat_error
;
7840 /* Return a union piecewise affine expression
7841 * that is equal to "pa" on "domain", assuming "domain" and "pa"
7842 * have been aligned.
7844 * Construct an isl_pw_aff on each of the sets in "domain" and
7845 * collect the results.
7847 static __isl_give isl_union_pw_aff
*isl_union_pw_aff_pw_aff_on_domain_aligned(
7848 __isl_take isl_union_set
*domain
, __isl_take isl_pw_aff
*pa
)
7850 struct isl_union_pw_aff_pw_aff_on_domain_data data
;
7853 space
= isl_union_set_get_space(domain
);
7854 data
.res
= isl_union_pw_aff_empty(space
);
7856 if (isl_union_set_foreach_set(domain
, &pw_aff_on_domain
, &data
) < 0)
7857 data
.res
= isl_union_pw_aff_free(data
.res
);
7858 isl_union_set_free(domain
);
7859 isl_pw_aff_free(pa
);
7863 /* Return a union piecewise affine expression
7864 * that is equal to "pa" on "domain".
7866 * Check that "pa" is a parametric expression,
7867 * align the parameters if needed and call
7868 * isl_union_pw_aff_pw_aff_on_domain_aligned.
7870 __isl_give isl_union_pw_aff
*isl_union_pw_aff_pw_aff_on_domain(
7871 __isl_take isl_union_set
*domain
, __isl_take isl_pw_aff
*pa
)
7874 isl_bool equal_params
;
7875 isl_space
*domain_space
, *pa_space
;
7877 pa_space
= isl_pw_aff_peek_space(pa
);
7878 is_set
= isl_space_is_set(pa_space
);
7882 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
7883 "expecting parametric expression", goto error
);
7885 domain_space
= isl_union_set_get_space(domain
);
7886 pa_space
= isl_pw_aff_get_space(pa
);
7887 equal_params
= isl_space_has_equal_params(domain_space
, pa_space
);
7888 if (equal_params
>= 0 && !equal_params
) {
7891 space
= isl_space_align_params(domain_space
, pa_space
);
7892 pa
= isl_pw_aff_align_params(pa
, isl_space_copy(space
));
7893 domain
= isl_union_set_align_params(domain
, space
);
7895 isl_space_free(domain_space
);
7896 isl_space_free(pa_space
);
7899 if (equal_params
< 0)
7901 return isl_union_pw_aff_pw_aff_on_domain_aligned(domain
, pa
);
7903 isl_union_set_free(domain
);
7904 isl_pw_aff_free(pa
);
7908 /* Internal data structure for isl_union_pw_aff_val_on_domain.
7909 * "v" is the value that the resulting isl_union_pw_aff needs to attain.
7910 * "res" collects the results.
7912 struct isl_union_pw_aff_val_on_domain_data
{
7914 isl_union_pw_aff
*res
;
7917 /* Construct a piecewise affine expression that is equal to data->v
7918 * on "domain" and add the result to data->res.
7920 static isl_stat
pw_aff_val_on_domain(__isl_take isl_set
*domain
, void *user
)
7922 struct isl_union_pw_aff_val_on_domain_data
*data
= user
;
7926 v
= isl_val_copy(data
->v
);
7927 pa
= isl_pw_aff_val_on_domain(domain
, v
);
7928 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
7930 return data
->res
? isl_stat_ok
: isl_stat_error
;
7933 /* Return a union piecewise affine expression
7934 * that is equal to "v" on "domain".
7936 * Construct an isl_pw_aff on each of the sets in "domain" and
7937 * collect the results.
7939 __isl_give isl_union_pw_aff
*isl_union_pw_aff_val_on_domain(
7940 __isl_take isl_union_set
*domain
, __isl_take isl_val
*v
)
7942 struct isl_union_pw_aff_val_on_domain_data data
;
7945 space
= isl_union_set_get_space(domain
);
7946 data
.res
= isl_union_pw_aff_empty(space
);
7948 if (isl_union_set_foreach_set(domain
, &pw_aff_val_on_domain
, &data
) < 0)
7949 data
.res
= isl_union_pw_aff_free(data
.res
);
7950 isl_union_set_free(domain
);
7955 /* Construct a piecewise multi affine expression
7956 * that is equal to "pa" and add it to upma.
7958 static isl_stat
pw_multi_aff_from_pw_aff_entry(__isl_take isl_pw_aff
*pa
,
7961 isl_union_pw_multi_aff
**upma
= user
;
7962 isl_pw_multi_aff
*pma
;
7964 pma
= isl_pw_multi_aff_from_pw_aff(pa
);
7965 *upma
= isl_union_pw_multi_aff_add_pw_multi_aff(*upma
, pma
);
7967 return *upma
? isl_stat_ok
: isl_stat_error
;
7970 /* Construct and return a union piecewise multi affine expression
7971 * that is equal to the given union piecewise affine expression.
7973 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_union_pw_aff(
7974 __isl_take isl_union_pw_aff
*upa
)
7977 isl_union_pw_multi_aff
*upma
;
7982 space
= isl_union_pw_aff_get_space(upa
);
7983 upma
= isl_union_pw_multi_aff_empty(space
);
7985 if (isl_union_pw_aff_foreach_pw_aff(upa
,
7986 &pw_multi_aff_from_pw_aff_entry
, &upma
) < 0)
7987 upma
= isl_union_pw_multi_aff_free(upma
);
7989 isl_union_pw_aff_free(upa
);
7993 /* Compute the set of elements in the domain of "pa" where it is zero and
7994 * add this set to "uset".
7996 static isl_stat
zero_union_set(__isl_take isl_pw_aff
*pa
, void *user
)
7998 isl_union_set
**uset
= (isl_union_set
**)user
;
8000 *uset
= isl_union_set_add_set(*uset
, isl_pw_aff_zero_set(pa
));
8002 return *uset
? isl_stat_ok
: isl_stat_error
;
8005 /* Return a union set containing those elements in the domain
8006 * of "upa" where it is zero.
8008 __isl_give isl_union_set
*isl_union_pw_aff_zero_union_set(
8009 __isl_take isl_union_pw_aff
*upa
)
8011 isl_union_set
*zero
;
8013 zero
= isl_union_set_empty(isl_union_pw_aff_get_space(upa
));
8014 if (isl_union_pw_aff_foreach_pw_aff(upa
, &zero_union_set
, &zero
) < 0)
8015 zero
= isl_union_set_free(zero
);
8017 isl_union_pw_aff_free(upa
);
8021 /* Convert "pa" to an isl_map and add it to *umap.
8023 static isl_stat
map_from_pw_aff_entry(__isl_take isl_pw_aff
*pa
, void *user
)
8025 isl_union_map
**umap
= user
;
8028 map
= isl_map_from_pw_aff(pa
);
8029 *umap
= isl_union_map_add_map(*umap
, map
);
8031 return *umap
? isl_stat_ok
: isl_stat_error
;
8034 /* Construct a union map mapping the domain of the union
8035 * piecewise affine expression to its range, with the single output dimension
8036 * equated to the corresponding affine expressions on their cells.
8038 __isl_give isl_union_map
*isl_union_map_from_union_pw_aff(
8039 __isl_take isl_union_pw_aff
*upa
)
8042 isl_union_map
*umap
;
8047 space
= isl_union_pw_aff_get_space(upa
);
8048 umap
= isl_union_map_empty(space
);
8050 if (isl_union_pw_aff_foreach_pw_aff(upa
, &map_from_pw_aff_entry
,
8052 umap
= isl_union_map_free(umap
);
8054 isl_union_pw_aff_free(upa
);
8058 /* Internal data structure for isl_union_pw_aff_pullback_union_pw_multi_aff.
8059 * upma is the function that is plugged in.
8060 * pa is the current part of the function in which upma is plugged in.
8061 * res collects the results.
8063 struct isl_union_pw_aff_pullback_upma_data
{
8064 isl_union_pw_multi_aff
*upma
;
8066 isl_union_pw_aff
*res
;
8069 /* Check if "pma" can be plugged into data->pa.
8070 * If so, perform the pullback and add the result to data->res.
8072 static isl_stat
pa_pb_pma(__isl_take isl_pw_multi_aff
*pma
, void *user
)
8074 struct isl_union_pw_aff_pullback_upma_data
*data
= user
;
8077 if (!isl_space_tuple_is_equal(data
->pa
->dim
, isl_dim_in
,
8078 pma
->dim
, isl_dim_out
)) {
8079 isl_pw_multi_aff_free(pma
);
8083 pa
= isl_pw_aff_copy(data
->pa
);
8084 pa
= isl_pw_aff_pullback_pw_multi_aff(pa
, pma
);
8086 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
8088 return data
->res
? isl_stat_ok
: isl_stat_error
;
8091 /* Check if any of the elements of data->upma can be plugged into pa,
8092 * add if so add the result to data->res.
8094 static isl_stat
upa_pb_upma(__isl_take isl_pw_aff
*pa
, void *user
)
8096 struct isl_union_pw_aff_pullback_upma_data
*data
= user
;
8100 r
= isl_union_pw_multi_aff_foreach_pw_multi_aff(data
->upma
,
8102 isl_pw_aff_free(pa
);
8107 /* Compute the pullback of "upa" by the function represented by "upma".
8108 * In other words, plug in "upma" in "upa". The result contains
8109 * expressions defined over the domain space of "upma".
8111 * Run over all pairs of elements in "upa" and "upma", perform
8112 * the pullback when appropriate and collect the results.
8113 * If the hash value were based on the domain space rather than
8114 * the function space, then we could run through all elements
8115 * of "upma" and directly pick out the corresponding element of "upa".
8117 __isl_give isl_union_pw_aff
*isl_union_pw_aff_pullback_union_pw_multi_aff(
8118 __isl_take isl_union_pw_aff
*upa
,
8119 __isl_take isl_union_pw_multi_aff
*upma
)
8121 struct isl_union_pw_aff_pullback_upma_data data
= { NULL
, NULL
};
8124 space
= isl_union_pw_multi_aff_get_space(upma
);
8125 upa
= isl_union_pw_aff_align_params(upa
, space
);
8126 space
= isl_union_pw_aff_get_space(upa
);
8127 upma
= isl_union_pw_multi_aff_align_params(upma
, space
);
8133 data
.res
= isl_union_pw_aff_alloc_same_size(upa
);
8134 if (isl_union_pw_aff_foreach_pw_aff(upa
, &upa_pb_upma
, &data
) < 0)
8135 data
.res
= isl_union_pw_aff_free(data
.res
);
8137 isl_union_pw_aff_free(upa
);
8138 isl_union_pw_multi_aff_free(upma
);
8141 isl_union_pw_aff_free(upa
);
8142 isl_union_pw_multi_aff_free(upma
);
8147 #define BASE union_pw_aff
8149 #define DOMBASE union_set
8151 #define NO_MOVE_DIMS
8159 #include <isl_multi_explicit_domain.c>
8160 #include <isl_multi_union_pw_aff_explicit_domain.c>
8161 #include <isl_multi_templ.c>
8162 #include <isl_multi_apply_set.c>
8163 #include <isl_multi_apply_union_set.c>
8164 #include <isl_multi_coalesce.c>
8165 #include <isl_multi_floor.c>
8166 #include <isl_multi_gist.c>
8167 #include <isl_multi_align_set.c>
8168 #include <isl_multi_align_union_set.c>
8169 #include <isl_multi_intersect.c>
8171 /* Does "mupa" have a non-trivial explicit domain?
8173 * The explicit domain, if present, is trivial if it represents
8174 * an (obviously) universe parameter set.
8176 isl_bool
isl_multi_union_pw_aff_has_non_trivial_domain(
8177 __isl_keep isl_multi_union_pw_aff
*mupa
)
8179 isl_bool is_params
, trivial
;
8183 return isl_bool_error
;
8184 if (!isl_multi_union_pw_aff_has_explicit_domain(mupa
))
8185 return isl_bool_false
;
8186 is_params
= isl_union_set_is_params(mupa
->u
.dom
);
8187 if (is_params
< 0 || !is_params
)
8188 return isl_bool_not(is_params
);
8189 set
= isl_set_from_union_set(isl_union_set_copy(mupa
->u
.dom
));
8190 trivial
= isl_set_plain_is_universe(set
);
8192 return isl_bool_not(trivial
);
8195 /* Construct a multiple union piecewise affine expression
8196 * in the given space with value zero in each of the output dimensions.
8198 * Since there is no canonical zero value for
8199 * a union piecewise affine expression, we can only construct
8200 * a zero-dimensional "zero" value.
8202 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_zero(
8203 __isl_take isl_space
*space
)
8210 params
= isl_space_is_params(space
);
8214 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
8215 "expecting proper set space", goto error
);
8216 if (!isl_space_is_set(space
))
8217 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
8218 "expecting set space", goto error
);
8219 if (isl_space_dim(space
, isl_dim_out
) != 0)
8220 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
8221 "expecting 0D space", goto error
);
8223 return isl_multi_union_pw_aff_alloc(space
);
8225 isl_space_free(space
);
8229 /* Compute the sum of "mupa1" and "mupa2" on the union of their domains,
8230 * with the actual sum on the shared domain and
8231 * the defined expression on the symmetric difference of the domains.
8233 * We simply iterate over the elements in both arguments and
8234 * call isl_union_pw_aff_union_add on each of them, if there is
8235 * at least one element.
8237 * Otherwise, the two expressions have an explicit domain and
8238 * the union of these explicit domains is computed.
8239 * This assumes that the explicit domains are either both in terms
8240 * of specific domains elements or both in terms of parameters.
8241 * However, if one of the expressions does not have any constraints
8242 * on its explicit domain, then this is allowed as well and the result
8243 * is the expression with no constraints on its explicit domain.
8245 static __isl_give isl_multi_union_pw_aff
*
8246 isl_multi_union_pw_aff_union_add_aligned(
8247 __isl_take isl_multi_union_pw_aff
*mupa1
,
8248 __isl_take isl_multi_union_pw_aff
*mupa2
)
8250 isl_bool has_domain
, is_params1
, is_params2
;
8252 if (isl_multi_union_pw_aff_check_equal_space(mupa1
, mupa2
) < 0)
8255 return isl_multi_union_pw_aff_bin_op(mupa1
, mupa2
,
8256 &isl_union_pw_aff_union_add
);
8257 if (isl_multi_union_pw_aff_check_has_explicit_domain(mupa1
) < 0 ||
8258 isl_multi_union_pw_aff_check_has_explicit_domain(mupa2
) < 0)
8261 has_domain
= isl_multi_union_pw_aff_has_non_trivial_domain(mupa1
);
8265 isl_multi_union_pw_aff_free(mupa2
);
8268 has_domain
= isl_multi_union_pw_aff_has_non_trivial_domain(mupa2
);
8272 isl_multi_union_pw_aff_free(mupa1
);
8276 is_params1
= isl_union_set_is_params(mupa1
->u
.dom
);
8277 is_params2
= isl_union_set_is_params(mupa2
->u
.dom
);
8278 if (is_params1
< 0 || is_params2
< 0)
8280 if (is_params1
!= is_params2
)
8281 isl_die(isl_multi_union_pw_aff_get_ctx(mupa1
),
8283 "cannot compute union of concrete domain and "
8284 "parameter constraints", goto error
);
8285 mupa1
= isl_multi_union_pw_aff_cow(mupa1
);
8288 mupa1
->u
.dom
= isl_union_set_union(mupa1
->u
.dom
,
8289 isl_union_set_copy(mupa2
->u
.dom
));
8292 isl_multi_union_pw_aff_free(mupa2
);
8295 isl_multi_union_pw_aff_free(mupa1
);
8296 isl_multi_union_pw_aff_free(mupa2
);
8300 /* Compute the sum of "mupa1" and "mupa2" on the union of their domains,
8301 * with the actual sum on the shared domain and
8302 * the defined expression on the symmetric difference of the domains.
8304 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_union_add(
8305 __isl_take isl_multi_union_pw_aff
*mupa1
,
8306 __isl_take isl_multi_union_pw_aff
*mupa2
)
8308 return isl_multi_union_pw_aff_align_params_multi_multi_and(mupa1
, mupa2
,
8309 &isl_multi_union_pw_aff_union_add_aligned
);
8312 /* Construct and return a multi union piecewise affine expression
8313 * that is equal to the given multi affine expression.
8315 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_from_multi_aff(
8316 __isl_take isl_multi_aff
*ma
)
8318 isl_multi_pw_aff
*mpa
;
8320 mpa
= isl_multi_pw_aff_from_multi_aff(ma
);
8321 return isl_multi_union_pw_aff_from_multi_pw_aff(mpa
);
8324 /* Construct and return a multi union piecewise affine expression
8325 * that is equal to the given multi piecewise affine expression.
8327 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_from_multi_pw_aff(
8328 __isl_take isl_multi_pw_aff
*mpa
)
8332 isl_multi_union_pw_aff
*mupa
;
8337 space
= isl_multi_pw_aff_get_space(mpa
);
8338 space
= isl_space_range(space
);
8339 mupa
= isl_multi_union_pw_aff_alloc(space
);
8341 n
= isl_multi_pw_aff_dim(mpa
, isl_dim_out
);
8342 for (i
= 0; i
< n
; ++i
) {
8344 isl_union_pw_aff
*upa
;
8346 pa
= isl_multi_pw_aff_get_pw_aff(mpa
, i
);
8347 upa
= isl_union_pw_aff_from_pw_aff(pa
);
8348 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
8351 isl_multi_pw_aff_free(mpa
);
8356 /* Extract the range space of "pma" and assign it to *space.
8357 * If *space has already been set (through a previous call to this function),
8358 * then check that the range space is the same.
8360 static isl_stat
extract_space(__isl_take isl_pw_multi_aff
*pma
, void *user
)
8362 isl_space
**space
= user
;
8363 isl_space
*pma_space
;
8366 pma_space
= isl_space_range(isl_pw_multi_aff_get_space(pma
));
8367 isl_pw_multi_aff_free(pma
);
8370 return isl_stat_error
;
8376 equal
= isl_space_is_equal(pma_space
, *space
);
8377 isl_space_free(pma_space
);
8380 return isl_stat_error
;
8382 isl_die(isl_space_get_ctx(*space
), isl_error_invalid
,
8383 "range spaces not the same", return isl_stat_error
);
8387 /* Construct and return a multi union piecewise affine expression
8388 * that is equal to the given union piecewise multi affine expression.
8390 * In order to be able to perform the conversion, the input
8391 * needs to be non-empty and may only involve a single range space.
8393 * If the resulting multi union piecewise affine expression has
8394 * an explicit domain, then assign it the domain of the input.
8395 * In other cases, the domain is stored in the individual elements.
8397 __isl_give isl_multi_union_pw_aff
*
8398 isl_multi_union_pw_aff_from_union_pw_multi_aff(
8399 __isl_take isl_union_pw_multi_aff
*upma
)
8401 isl_space
*space
= NULL
;
8402 isl_multi_union_pw_aff
*mupa
;
8407 if (isl_union_pw_multi_aff_n_pw_multi_aff(upma
) == 0)
8408 isl_die(isl_union_pw_multi_aff_get_ctx(upma
), isl_error_invalid
,
8409 "cannot extract range space from empty input",
8411 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma
, &extract_space
,
8418 n
= isl_space_dim(space
, isl_dim_set
);
8419 mupa
= isl_multi_union_pw_aff_alloc(space
);
8421 for (i
= 0; i
< n
; ++i
) {
8422 isl_union_pw_aff
*upa
;
8424 upa
= isl_union_pw_multi_aff_get_union_pw_aff(upma
, i
);
8425 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
8427 if (isl_multi_union_pw_aff_has_explicit_domain(mupa
)) {
8429 isl_union_pw_multi_aff
*copy
;
8431 copy
= isl_union_pw_multi_aff_copy(upma
);
8432 dom
= isl_union_pw_multi_aff_domain(copy
);
8433 mupa
= isl_multi_union_pw_aff_intersect_domain(mupa
, dom
);
8436 isl_union_pw_multi_aff_free(upma
);
8439 isl_space_free(space
);
8440 isl_union_pw_multi_aff_free(upma
);
8444 /* Try and create an isl_multi_union_pw_aff that is equivalent
8445 * to the given isl_union_map.
8446 * The isl_union_map is required to be single-valued in each space.
8447 * Moreover, it cannot be empty and all range spaces need to be the same.
8448 * Otherwise, an error is produced.
8450 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_from_union_map(
8451 __isl_take isl_union_map
*umap
)
8453 isl_union_pw_multi_aff
*upma
;
8455 upma
= isl_union_pw_multi_aff_from_union_map(umap
);
8456 return isl_multi_union_pw_aff_from_union_pw_multi_aff(upma
);
8459 /* Return a multiple union piecewise affine expression
8460 * that is equal to "mv" on "domain", assuming "domain" and "mv"
8461 * have been aligned.
8463 static __isl_give isl_multi_union_pw_aff
*
8464 isl_multi_union_pw_aff_multi_val_on_domain_aligned(
8465 __isl_take isl_union_set
*domain
, __isl_take isl_multi_val
*mv
)
8469 isl_multi_union_pw_aff
*mupa
;
8474 n
= isl_multi_val_dim(mv
, isl_dim_set
);
8475 space
= isl_multi_val_get_space(mv
);
8476 mupa
= isl_multi_union_pw_aff_alloc(space
);
8477 for (i
= 0; i
< n
; ++i
) {
8479 isl_union_pw_aff
*upa
;
8481 v
= isl_multi_val_get_val(mv
, i
);
8482 upa
= isl_union_pw_aff_val_on_domain(isl_union_set_copy(domain
),
8484 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
8487 isl_union_set_free(domain
);
8488 isl_multi_val_free(mv
);
8491 isl_union_set_free(domain
);
8492 isl_multi_val_free(mv
);
8496 /* Return a multiple union piecewise affine expression
8497 * that is equal to "mv" on "domain".
8499 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_multi_val_on_domain(
8500 __isl_take isl_union_set
*domain
, __isl_take isl_multi_val
*mv
)
8502 isl_bool equal_params
;
8506 equal_params
= isl_space_has_equal_params(domain
->dim
, mv
->space
);
8507 if (equal_params
< 0)
8510 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(
8512 domain
= isl_union_set_align_params(domain
,
8513 isl_multi_val_get_space(mv
));
8514 mv
= isl_multi_val_align_params(mv
, isl_union_set_get_space(domain
));
8515 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(domain
, mv
);
8517 isl_union_set_free(domain
);
8518 isl_multi_val_free(mv
);
8522 /* Return a multiple union piecewise affine expression
8523 * that is equal to "ma" on "domain".
8525 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_multi_aff_on_domain(
8526 __isl_take isl_union_set
*domain
, __isl_take isl_multi_aff
*ma
)
8528 isl_pw_multi_aff
*pma
;
8530 pma
= isl_pw_multi_aff_from_multi_aff(ma
);
8531 return isl_multi_union_pw_aff_pw_multi_aff_on_domain(domain
, pma
);
8534 /* Return a multiple union piecewise affine expression
8535 * that is equal to "pma" on "domain", assuming "domain" and "pma"
8536 * have been aligned.
8538 * If the resulting multi union piecewise affine expression has
8539 * an explicit domain, then assign it the input domain.
8540 * In other cases, the domain is stored in the individual elements.
8542 static __isl_give isl_multi_union_pw_aff
*
8543 isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(
8544 __isl_take isl_union_set
*domain
, __isl_take isl_pw_multi_aff
*pma
)
8548 isl_multi_union_pw_aff
*mupa
;
8550 if (!domain
|| !pma
)
8553 n
= isl_pw_multi_aff_dim(pma
, isl_dim_set
);
8554 space
= isl_pw_multi_aff_get_space(pma
);
8555 mupa
= isl_multi_union_pw_aff_alloc(space
);
8556 for (i
= 0; i
< n
; ++i
) {
8558 isl_union_pw_aff
*upa
;
8560 pa
= isl_pw_multi_aff_get_pw_aff(pma
, i
);
8561 upa
= isl_union_pw_aff_pw_aff_on_domain(
8562 isl_union_set_copy(domain
), pa
);
8563 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
8565 if (isl_multi_union_pw_aff_has_explicit_domain(mupa
))
8566 mupa
= isl_multi_union_pw_aff_intersect_domain(mupa
,
8567 isl_union_set_copy(domain
));
8569 isl_union_set_free(domain
);
8570 isl_pw_multi_aff_free(pma
);
8573 isl_union_set_free(domain
);
8574 isl_pw_multi_aff_free(pma
);
8578 /* Return a multiple union piecewise affine expression
8579 * that is equal to "pma" on "domain".
8581 __isl_give isl_multi_union_pw_aff
*
8582 isl_multi_union_pw_aff_pw_multi_aff_on_domain(__isl_take isl_union_set
*domain
,
8583 __isl_take isl_pw_multi_aff
*pma
)
8585 isl_bool equal_params
;
8588 space
= isl_pw_multi_aff_peek_space(pma
);
8589 equal_params
= isl_union_set_space_has_equal_params(domain
, space
);
8590 if (equal_params
< 0)
8593 return isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(
8595 domain
= isl_union_set_align_params(domain
,
8596 isl_pw_multi_aff_get_space(pma
));
8597 pma
= isl_pw_multi_aff_align_params(pma
,
8598 isl_union_set_get_space(domain
));
8599 return isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(domain
,
8602 isl_union_set_free(domain
);
8603 isl_pw_multi_aff_free(pma
);
8607 /* Return a union set containing those elements in the domains
8608 * of the elements of "mupa" where they are all zero.
8610 * If there are no elements, then simply return the entire domain.
8612 __isl_give isl_union_set
*isl_multi_union_pw_aff_zero_union_set(
8613 __isl_take isl_multi_union_pw_aff
*mupa
)
8616 isl_union_pw_aff
*upa
;
8617 isl_union_set
*zero
;
8622 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8624 return isl_multi_union_pw_aff_domain(mupa
);
8626 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, 0);
8627 zero
= isl_union_pw_aff_zero_union_set(upa
);
8629 for (i
= 1; i
< n
; ++i
) {
8630 isl_union_set
*zero_i
;
8632 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
8633 zero_i
= isl_union_pw_aff_zero_union_set(upa
);
8635 zero
= isl_union_set_intersect(zero
, zero_i
);
8638 isl_multi_union_pw_aff_free(mupa
);
8642 /* Construct a union map mapping the shared domain
8643 * of the union piecewise affine expressions to the range of "mupa"
8644 * in the special case of a 0D multi union piecewise affine expression.
8646 * Construct a map between the explicit domain of "mupa" and
8648 * Note that this assumes that the domain consists of explicit elements.
8650 static __isl_give isl_union_map
*isl_union_map_from_multi_union_pw_aff_0D(
8651 __isl_take isl_multi_union_pw_aff
*mupa
)
8655 isl_union_set
*dom
, *ran
;
8657 space
= isl_multi_union_pw_aff_get_space(mupa
);
8658 dom
= isl_multi_union_pw_aff_domain(mupa
);
8659 ran
= isl_union_set_from_set(isl_set_universe(space
));
8661 is_params
= isl_union_set_is_params(dom
);
8663 dom
= isl_union_set_free(dom
);
8665 isl_die(isl_union_set_get_ctx(dom
), isl_error_invalid
,
8666 "cannot create union map from expression without "
8667 "explicit domain elements",
8668 dom
= isl_union_set_free(dom
));
8670 return isl_union_map_from_domain_and_range(dom
, ran
);
8673 /* Construct a union map mapping the shared domain
8674 * of the union piecewise affine expressions to the range of "mupa"
8675 * with each dimension in the range equated to the
8676 * corresponding union piecewise affine expression.
8678 * If the input is zero-dimensional, then construct a mapping
8679 * from its explicit domain.
8681 __isl_give isl_union_map
*isl_union_map_from_multi_union_pw_aff(
8682 __isl_take isl_multi_union_pw_aff
*mupa
)
8686 isl_union_map
*umap
;
8687 isl_union_pw_aff
*upa
;
8692 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8694 return isl_union_map_from_multi_union_pw_aff_0D(mupa
);
8696 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, 0);
8697 umap
= isl_union_map_from_union_pw_aff(upa
);
8699 for (i
= 1; i
< n
; ++i
) {
8700 isl_union_map
*umap_i
;
8702 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
8703 umap_i
= isl_union_map_from_union_pw_aff(upa
);
8704 umap
= isl_union_map_flat_range_product(umap
, umap_i
);
8707 space
= isl_multi_union_pw_aff_get_space(mupa
);
8708 umap
= isl_union_map_reset_range_space(umap
, space
);
8710 isl_multi_union_pw_aff_free(mupa
);
8714 /* Internal data structure for isl_union_pw_multi_aff_reset_range_space.
8715 * "range" is the space from which to set the range space.
8716 * "res" collects the results.
8718 struct isl_union_pw_multi_aff_reset_range_space_data
{
8720 isl_union_pw_multi_aff
*res
;
8723 /* Replace the range space of "pma" by the range space of data->range and
8724 * add the result to data->res.
8726 static isl_stat
reset_range_space(__isl_take isl_pw_multi_aff
*pma
, void *user
)
8728 struct isl_union_pw_multi_aff_reset_range_space_data
*data
= user
;
8731 space
= isl_pw_multi_aff_get_space(pma
);
8732 space
= isl_space_domain(space
);
8733 space
= isl_space_extend_domain_with_range(space
,
8734 isl_space_copy(data
->range
));
8735 pma
= isl_pw_multi_aff_reset_space(pma
, space
);
8736 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma
);
8738 return data
->res
? isl_stat_ok
: isl_stat_error
;
8741 /* Replace the range space of all the piecewise affine expressions in "upma" by
8742 * the range space of "space".
8744 * This assumes that all these expressions have the same output dimension.
8746 * Since the spaces of the expressions change, so do their hash values.
8747 * We therefore need to create a new isl_union_pw_multi_aff.
8748 * Note that the hash value is currently computed based on the entire
8749 * space even though there can only be a single expression with a given
8752 static __isl_give isl_union_pw_multi_aff
*
8753 isl_union_pw_multi_aff_reset_range_space(
8754 __isl_take isl_union_pw_multi_aff
*upma
, __isl_take isl_space
*space
)
8756 struct isl_union_pw_multi_aff_reset_range_space_data data
= { space
};
8757 isl_space
*space_upma
;
8759 space_upma
= isl_union_pw_multi_aff_get_space(upma
);
8760 data
.res
= isl_union_pw_multi_aff_empty(space_upma
);
8761 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma
,
8762 &reset_range_space
, &data
) < 0)
8763 data
.res
= isl_union_pw_multi_aff_free(data
.res
);
8765 isl_space_free(space
);
8766 isl_union_pw_multi_aff_free(upma
);
8770 /* Construct and return a union piecewise multi affine expression
8771 * that is equal to the given multi union piecewise affine expression,
8772 * in the special case of a 0D multi union piecewise affine expression.
8774 * Construct a union piecewise multi affine expression
8775 * on top of the explicit domain of the input.
8777 __isl_give isl_union_pw_multi_aff
*
8778 isl_union_pw_multi_aff_from_multi_union_pw_aff_0D(
8779 __isl_take isl_multi_union_pw_aff
*mupa
)
8783 isl_union_set
*domain
;
8785 space
= isl_multi_union_pw_aff_get_space(mupa
);
8786 mv
= isl_multi_val_zero(space
);
8787 domain
= isl_multi_union_pw_aff_domain(mupa
);
8788 return isl_union_pw_multi_aff_multi_val_on_domain(domain
, mv
);
8791 /* Construct and return a union piecewise multi affine expression
8792 * that is equal to the given multi union piecewise affine expression.
8794 * If the input is zero-dimensional, then
8795 * construct a union piecewise multi affine expression
8796 * on top of the explicit domain of the input.
8798 __isl_give isl_union_pw_multi_aff
*
8799 isl_union_pw_multi_aff_from_multi_union_pw_aff(
8800 __isl_take isl_multi_union_pw_aff
*mupa
)
8804 isl_union_pw_multi_aff
*upma
;
8805 isl_union_pw_aff
*upa
;
8810 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8812 return isl_union_pw_multi_aff_from_multi_union_pw_aff_0D(mupa
);
8814 space
= isl_multi_union_pw_aff_get_space(mupa
);
8815 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, 0);
8816 upma
= isl_union_pw_multi_aff_from_union_pw_aff(upa
);
8818 for (i
= 1; i
< n
; ++i
) {
8819 isl_union_pw_multi_aff
*upma_i
;
8821 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
8822 upma_i
= isl_union_pw_multi_aff_from_union_pw_aff(upa
);
8823 upma
= isl_union_pw_multi_aff_flat_range_product(upma
, upma_i
);
8826 upma
= isl_union_pw_multi_aff_reset_range_space(upma
, space
);
8828 isl_multi_union_pw_aff_free(mupa
);
8832 /* Intersect the range of "mupa" with "range",
8833 * in the special case where "mupa" is 0D.
8835 * Intersect the domain of "mupa" with the constraints on the parameters
8838 static __isl_give isl_multi_union_pw_aff
*mupa_intersect_range_0D(
8839 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_set
*range
)
8841 range
= isl_set_params(range
);
8842 mupa
= isl_multi_union_pw_aff_intersect_params(mupa
, range
);
8846 /* Intersect the range of "mupa" with "range".
8847 * That is, keep only those domain elements that have a function value
8850 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_intersect_range(
8851 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_set
*range
)
8853 isl_union_pw_multi_aff
*upma
;
8854 isl_union_set
*domain
;
8859 if (!mupa
|| !range
)
8862 space
= isl_set_get_space(range
);
8863 match
= isl_space_tuple_is_equal(mupa
->space
, isl_dim_set
,
8864 space
, isl_dim_set
);
8865 isl_space_free(space
);
8869 isl_die(isl_multi_union_pw_aff_get_ctx(mupa
), isl_error_invalid
,
8870 "space don't match", goto error
);
8871 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8873 return mupa_intersect_range_0D(mupa
, range
);
8875 upma
= isl_union_pw_multi_aff_from_multi_union_pw_aff(
8876 isl_multi_union_pw_aff_copy(mupa
));
8877 domain
= isl_union_set_from_set(range
);
8878 domain
= isl_union_set_preimage_union_pw_multi_aff(domain
, upma
);
8879 mupa
= isl_multi_union_pw_aff_intersect_domain(mupa
, domain
);
8883 isl_multi_union_pw_aff_free(mupa
);
8884 isl_set_free(range
);
8888 /* Return the shared domain of the elements of "mupa",
8889 * in the special case where "mupa" is zero-dimensional.
8891 * Return the explicit domain of "mupa".
8892 * Note that this domain may be a parameter set, either
8893 * because "mupa" is meant to live in a set space or
8894 * because no explicit domain has been set.
8896 __isl_give isl_union_set
*isl_multi_union_pw_aff_domain_0D(
8897 __isl_take isl_multi_union_pw_aff
*mupa
)
8901 dom
= isl_multi_union_pw_aff_get_explicit_domain(mupa
);
8902 isl_multi_union_pw_aff_free(mupa
);
8907 /* Return the shared domain of the elements of "mupa".
8909 * If "mupa" is zero-dimensional, then return its explicit domain.
8911 __isl_give isl_union_set
*isl_multi_union_pw_aff_domain(
8912 __isl_take isl_multi_union_pw_aff
*mupa
)
8915 isl_union_pw_aff
*upa
;
8921 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8923 return isl_multi_union_pw_aff_domain_0D(mupa
);
8925 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, 0);
8926 dom
= isl_union_pw_aff_domain(upa
);
8927 for (i
= 1; i
< n
; ++i
) {
8928 isl_union_set
*dom_i
;
8930 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
8931 dom_i
= isl_union_pw_aff_domain(upa
);
8932 dom
= isl_union_set_intersect(dom
, dom_i
);
8935 isl_multi_union_pw_aff_free(mupa
);
8939 /* Apply "aff" to "mupa". The space of "mupa" is equal to the domain of "aff".
8940 * In particular, the spaces have been aligned.
8941 * The result is defined over the shared domain of the elements of "mupa"
8943 * We first extract the parametric constant part of "aff" and
8944 * define that over the shared domain.
8945 * Then we iterate over all input dimensions of "aff" and add the corresponding
8946 * multiples of the elements of "mupa".
8947 * Finally, we consider the integer divisions, calling the function
8948 * recursively to obtain an isl_union_pw_aff corresponding to the
8949 * integer division argument.
8951 static __isl_give isl_union_pw_aff
*multi_union_pw_aff_apply_aff(
8952 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_aff
*aff
)
8955 isl_union_pw_aff
*upa
;
8956 isl_union_set
*uset
;
8960 n_in
= isl_aff_dim(aff
, isl_dim_in
);
8961 n_div
= isl_aff_dim(aff
, isl_dim_div
);
8963 uset
= isl_multi_union_pw_aff_domain(isl_multi_union_pw_aff_copy(mupa
));
8964 cst
= isl_aff_copy(aff
);
8965 cst
= isl_aff_drop_dims(cst
, isl_dim_div
, 0, n_div
);
8966 cst
= isl_aff_drop_dims(cst
, isl_dim_in
, 0, n_in
);
8967 cst
= isl_aff_project_domain_on_params(cst
);
8968 upa
= isl_union_pw_aff_aff_on_domain(uset
, cst
);
8970 for (i
= 0; i
< n_in
; ++i
) {
8971 isl_union_pw_aff
*upa_i
;
8973 if (!isl_aff_involves_dims(aff
, isl_dim_in
, i
, 1))
8975 v
= isl_aff_get_coefficient_val(aff
, isl_dim_in
, i
);
8976 upa_i
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
8977 upa_i
= isl_union_pw_aff_scale_val(upa_i
, v
);
8978 upa
= isl_union_pw_aff_add(upa
, upa_i
);
8981 for (i
= 0; i
< n_div
; ++i
) {
8983 isl_union_pw_aff
*upa_i
;
8985 if (!isl_aff_involves_dims(aff
, isl_dim_div
, i
, 1))
8987 div
= isl_aff_get_div(aff
, i
);
8988 upa_i
= multi_union_pw_aff_apply_aff(
8989 isl_multi_union_pw_aff_copy(mupa
), div
);
8990 upa_i
= isl_union_pw_aff_floor(upa_i
);
8991 v
= isl_aff_get_coefficient_val(aff
, isl_dim_div
, i
);
8992 upa_i
= isl_union_pw_aff_scale_val(upa_i
, v
);
8993 upa
= isl_union_pw_aff_add(upa
, upa_i
);
8996 isl_multi_union_pw_aff_free(mupa
);
9002 /* Apply "aff" to "mupa". The space of "mupa" needs to be compatible
9003 * with the domain of "aff".
9004 * Furthermore, the dimension of this space needs to be greater than zero.
9005 * The result is defined over the shared domain of the elements of "mupa"
9007 * We perform these checks and then hand over control to
9008 * multi_union_pw_aff_apply_aff.
9010 __isl_give isl_union_pw_aff
*isl_multi_union_pw_aff_apply_aff(
9011 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_aff
*aff
)
9013 isl_space
*space1
, *space2
;
9016 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
9017 isl_aff_get_space(aff
));
9018 aff
= isl_aff_align_params(aff
, isl_multi_union_pw_aff_get_space(mupa
));
9022 space1
= isl_multi_union_pw_aff_get_space(mupa
);
9023 space2
= isl_aff_get_domain_space(aff
);
9024 equal
= isl_space_is_equal(space1
, space2
);
9025 isl_space_free(space1
);
9026 isl_space_free(space2
);
9030 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
9031 "spaces don't match", goto error
);
9032 if (isl_aff_dim(aff
, isl_dim_in
) == 0)
9033 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
9034 "cannot determine domains", goto error
);
9036 return multi_union_pw_aff_apply_aff(mupa
, aff
);
9038 isl_multi_union_pw_aff_free(mupa
);
9043 /* Apply "ma" to "mupa", in the special case where "mupa" is 0D.
9044 * The space of "mupa" is known to be compatible with the domain of "ma".
9046 * Construct an isl_multi_union_pw_aff that is equal to "ma"
9047 * on the domain of "mupa".
9049 static __isl_give isl_multi_union_pw_aff
*mupa_apply_multi_aff_0D(
9050 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_multi_aff
*ma
)
9054 dom
= isl_multi_union_pw_aff_domain(mupa
);
9055 ma
= isl_multi_aff_project_domain_on_params(ma
);
9057 return isl_multi_union_pw_aff_multi_aff_on_domain(dom
, ma
);
9060 /* Apply "ma" to "mupa". The space of "mupa" needs to be compatible
9061 * with the domain of "ma".
9062 * The result is defined over the shared domain of the elements of "mupa"
9064 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_apply_multi_aff(
9065 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_multi_aff
*ma
)
9067 isl_space
*space1
, *space2
;
9068 isl_multi_union_pw_aff
*res
;
9072 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
9073 isl_multi_aff_get_space(ma
));
9074 ma
= isl_multi_aff_align_params(ma
,
9075 isl_multi_union_pw_aff_get_space(mupa
));
9079 space1
= isl_multi_union_pw_aff_get_space(mupa
);
9080 space2
= isl_multi_aff_get_domain_space(ma
);
9081 equal
= isl_space_is_equal(space1
, space2
);
9082 isl_space_free(space1
);
9083 isl_space_free(space2
);
9087 isl_die(isl_multi_aff_get_ctx(ma
), isl_error_invalid
,
9088 "spaces don't match", goto error
);
9089 n_out
= isl_multi_aff_dim(ma
, isl_dim_out
);
9090 if (isl_multi_aff_dim(ma
, isl_dim_in
) == 0)
9091 return mupa_apply_multi_aff_0D(mupa
, ma
);
9093 space1
= isl_space_range(isl_multi_aff_get_space(ma
));
9094 res
= isl_multi_union_pw_aff_alloc(space1
);
9096 for (i
= 0; i
< n_out
; ++i
) {
9098 isl_union_pw_aff
*upa
;
9100 aff
= isl_multi_aff_get_aff(ma
, i
);
9101 upa
= multi_union_pw_aff_apply_aff(
9102 isl_multi_union_pw_aff_copy(mupa
), aff
);
9103 res
= isl_multi_union_pw_aff_set_union_pw_aff(res
, i
, upa
);
9106 isl_multi_aff_free(ma
);
9107 isl_multi_union_pw_aff_free(mupa
);
9110 isl_multi_union_pw_aff_free(mupa
);
9111 isl_multi_aff_free(ma
);
9115 /* Apply "pa" to "mupa", in the special case where "mupa" is 0D.
9116 * The space of "mupa" is known to be compatible with the domain of "pa".
9118 * Construct an isl_multi_union_pw_aff that is equal to "pa"
9119 * on the domain of "mupa".
9121 static __isl_give isl_union_pw_aff
*isl_multi_union_pw_aff_apply_pw_aff_0D(
9122 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_pw_aff
*pa
)
9126 dom
= isl_multi_union_pw_aff_domain(mupa
);
9127 pa
= isl_pw_aff_project_domain_on_params(pa
);
9129 return isl_union_pw_aff_pw_aff_on_domain(dom
, pa
);
9132 /* Apply "pa" to "mupa". The space of "mupa" needs to be compatible
9133 * with the domain of "pa".
9134 * Furthermore, the dimension of this space needs to be greater than zero.
9135 * The result is defined over the shared domain of the elements of "mupa"
9137 __isl_give isl_union_pw_aff
*isl_multi_union_pw_aff_apply_pw_aff(
9138 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_pw_aff
*pa
)
9142 isl_space
*space
, *space2
;
9143 isl_union_pw_aff
*upa
;
9145 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
9146 isl_pw_aff_get_space(pa
));
9147 pa
= isl_pw_aff_align_params(pa
,
9148 isl_multi_union_pw_aff_get_space(mupa
));
9152 space
= isl_multi_union_pw_aff_get_space(mupa
);
9153 space2
= isl_pw_aff_get_domain_space(pa
);
9154 equal
= isl_space_is_equal(space
, space2
);
9155 isl_space_free(space
);
9156 isl_space_free(space2
);
9160 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
9161 "spaces don't match", goto error
);
9162 if (isl_pw_aff_dim(pa
, isl_dim_in
) == 0)
9163 return isl_multi_union_pw_aff_apply_pw_aff_0D(mupa
, pa
);
9165 space
= isl_space_params(isl_multi_union_pw_aff_get_space(mupa
));
9166 upa
= isl_union_pw_aff_empty(space
);
9168 for (i
= 0; i
< pa
->n
; ++i
) {
9171 isl_multi_union_pw_aff
*mupa_i
;
9172 isl_union_pw_aff
*upa_i
;
9174 mupa_i
= isl_multi_union_pw_aff_copy(mupa
);
9175 domain
= isl_set_copy(pa
->p
[i
].set
);
9176 mupa_i
= isl_multi_union_pw_aff_intersect_range(mupa_i
, domain
);
9177 aff
= isl_aff_copy(pa
->p
[i
].aff
);
9178 upa_i
= multi_union_pw_aff_apply_aff(mupa_i
, aff
);
9179 upa
= isl_union_pw_aff_union_add(upa
, upa_i
);
9182 isl_multi_union_pw_aff_free(mupa
);
9183 isl_pw_aff_free(pa
);
9186 isl_multi_union_pw_aff_free(mupa
);
9187 isl_pw_aff_free(pa
);
9191 /* Apply "pma" to "mupa", in the special case where "mupa" is 0D.
9192 * The space of "mupa" is known to be compatible with the domain of "pma".
9194 * Construct an isl_multi_union_pw_aff that is equal to "pma"
9195 * on the domain of "mupa".
9197 static __isl_give isl_multi_union_pw_aff
*mupa_apply_pw_multi_aff_0D(
9198 __isl_take isl_multi_union_pw_aff
*mupa
,
9199 __isl_take isl_pw_multi_aff
*pma
)
9203 dom
= isl_multi_union_pw_aff_domain(mupa
);
9204 pma
= isl_pw_multi_aff_project_domain_on_params(pma
);
9206 return isl_multi_union_pw_aff_pw_multi_aff_on_domain(dom
, pma
);
9209 /* Apply "pma" to "mupa". The space of "mupa" needs to be compatible
9210 * with the domain of "pma".
9211 * The result is defined over the shared domain of the elements of "mupa"
9213 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_apply_pw_multi_aff(
9214 __isl_take isl_multi_union_pw_aff
*mupa
,
9215 __isl_take isl_pw_multi_aff
*pma
)
9217 isl_space
*space1
, *space2
;
9218 isl_multi_union_pw_aff
*res
;
9222 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
9223 isl_pw_multi_aff_get_space(pma
));
9224 pma
= isl_pw_multi_aff_align_params(pma
,
9225 isl_multi_union_pw_aff_get_space(mupa
));
9229 space1
= isl_multi_union_pw_aff_get_space(mupa
);
9230 space2
= isl_pw_multi_aff_get_domain_space(pma
);
9231 equal
= isl_space_is_equal(space1
, space2
);
9232 isl_space_free(space1
);
9233 isl_space_free(space2
);
9237 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
9238 "spaces don't match", goto error
);
9239 n_out
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
9240 if (isl_pw_multi_aff_dim(pma
, isl_dim_in
) == 0)
9241 return mupa_apply_pw_multi_aff_0D(mupa
, pma
);
9243 space1
= isl_space_range(isl_pw_multi_aff_get_space(pma
));
9244 res
= isl_multi_union_pw_aff_alloc(space1
);
9246 for (i
= 0; i
< n_out
; ++i
) {
9248 isl_union_pw_aff
*upa
;
9250 pa
= isl_pw_multi_aff_get_pw_aff(pma
, i
);
9251 upa
= isl_multi_union_pw_aff_apply_pw_aff(
9252 isl_multi_union_pw_aff_copy(mupa
), pa
);
9253 res
= isl_multi_union_pw_aff_set_union_pw_aff(res
, i
, upa
);
9256 isl_pw_multi_aff_free(pma
);
9257 isl_multi_union_pw_aff_free(mupa
);
9260 isl_multi_union_pw_aff_free(mupa
);
9261 isl_pw_multi_aff_free(pma
);
9265 /* Replace the explicit domain of "mupa" by its preimage under "upma".
9266 * If the explicit domain only keeps track of constraints on the parameters,
9267 * then only update those constraints.
9269 static __isl_give isl_multi_union_pw_aff
*preimage_explicit_domain(
9270 __isl_take isl_multi_union_pw_aff
*mupa
,
9271 __isl_keep isl_union_pw_multi_aff
*upma
)
9275 if (isl_multi_union_pw_aff_check_has_explicit_domain(mupa
) < 0)
9276 return isl_multi_union_pw_aff_free(mupa
);
9278 mupa
= isl_multi_union_pw_aff_cow(mupa
);
9282 is_params
= isl_union_set_is_params(mupa
->u
.dom
);
9284 return isl_multi_union_pw_aff_free(mupa
);
9286 upma
= isl_union_pw_multi_aff_copy(upma
);
9288 mupa
->u
.dom
= isl_union_set_intersect_params(mupa
->u
.dom
,
9289 isl_union_set_params(isl_union_pw_multi_aff_domain(upma
)));
9291 mupa
->u
.dom
= isl_union_set_preimage_union_pw_multi_aff(
9294 return isl_multi_union_pw_aff_free(mupa
);
9298 /* Compute the pullback of "mupa" by the function represented by "upma".
9299 * In other words, plug in "upma" in "mupa". The result contains
9300 * expressions defined over the domain space of "upma".
9302 * Run over all elements of "mupa" and plug in "upma" in each of them.
9304 * If "mupa" has an explicit domain, then it is this domain
9305 * that needs to undergo a pullback instead, i.e., a preimage.
9307 __isl_give isl_multi_union_pw_aff
*
9308 isl_multi_union_pw_aff_pullback_union_pw_multi_aff(
9309 __isl_take isl_multi_union_pw_aff
*mupa
,
9310 __isl_take isl_union_pw_multi_aff
*upma
)
9314 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
9315 isl_union_pw_multi_aff_get_space(upma
));
9316 upma
= isl_union_pw_multi_aff_align_params(upma
,
9317 isl_multi_union_pw_aff_get_space(mupa
));
9318 mupa
= isl_multi_union_pw_aff_cow(mupa
);
9322 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
9323 for (i
= 0; i
< n
; ++i
) {
9324 isl_union_pw_aff
*upa
;
9326 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
9327 upa
= isl_union_pw_aff_pullback_union_pw_multi_aff(upa
,
9328 isl_union_pw_multi_aff_copy(upma
));
9329 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
9332 if (isl_multi_union_pw_aff_has_explicit_domain(mupa
))
9333 mupa
= preimage_explicit_domain(mupa
, upma
);
9335 isl_union_pw_multi_aff_free(upma
);
9338 isl_multi_union_pw_aff_free(mupa
);
9339 isl_union_pw_multi_aff_free(upma
);
9343 /* Extract the sequence of elements in "mupa" with domain space "space"
9344 * (ignoring parameters).
9346 * For the elements of "mupa" that are not defined on the specified space,
9347 * the corresponding element in the result is empty.
9349 __isl_give isl_multi_pw_aff
*isl_multi_union_pw_aff_extract_multi_pw_aff(
9350 __isl_keep isl_multi_union_pw_aff
*mupa
, __isl_take isl_space
*space
)
9353 isl_space
*space_mpa
;
9354 isl_multi_pw_aff
*mpa
;
9356 if (!mupa
|| !space
)
9359 space_mpa
= isl_multi_union_pw_aff_get_space(mupa
);
9360 space
= isl_space_replace_params(space
, space_mpa
);
9361 space_mpa
= isl_space_map_from_domain_and_range(isl_space_copy(space
),
9363 mpa
= isl_multi_pw_aff_alloc(space_mpa
);
9365 space
= isl_space_from_domain(space
);
9366 space
= isl_space_add_dims(space
, isl_dim_out
, 1);
9367 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
9368 for (i
= 0; i
< n
; ++i
) {
9369 isl_union_pw_aff
*upa
;
9372 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
9373 pa
= isl_union_pw_aff_extract_pw_aff(upa
,
9374 isl_space_copy(space
));
9375 mpa
= isl_multi_pw_aff_set_pw_aff(mpa
, i
, pa
);
9376 isl_union_pw_aff_free(upa
);
9379 isl_space_free(space
);
9382 isl_space_free(space
);
9386 /* Evaluate the affine function "aff" in the void point "pnt".
9387 * In particular, return the value NaN.
9389 static __isl_give isl_val
*eval_void(__isl_take isl_aff
*aff
,
9390 __isl_take isl_point
*pnt
)
9394 ctx
= isl_point_get_ctx(pnt
);
9396 isl_point_free(pnt
);
9397 return isl_val_nan(ctx
);
9400 /* Evaluate the affine expression "aff"
9401 * in the coordinates (with denominator) "pnt".
9403 static __isl_give isl_val
*eval(__isl_keep isl_vec
*aff
,
9404 __isl_keep isl_vec
*pnt
)
9413 ctx
= isl_vec_get_ctx(aff
);
9416 isl_seq_inner_product(aff
->el
+ 1, pnt
->el
, pnt
->size
, &n
);
9417 isl_int_mul(d
, aff
->el
[0], pnt
->el
[0]);
9418 v
= isl_val_rat_from_isl_int(ctx
, n
, d
);
9419 v
= isl_val_normalize(v
);
9426 /* Check that the domain space of "aff" is equal to "space".
9428 static isl_stat
isl_aff_check_has_domain_space(__isl_keep isl_aff
*aff
,
9429 __isl_keep isl_space
*space
)
9433 ok
= isl_space_is_equal(isl_aff_peek_domain_space(aff
), space
);
9435 return isl_stat_error
;
9437 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
9438 "incompatible spaces", return isl_stat_error
);
9442 /* Evaluate the affine function "aff" in "pnt".
9444 __isl_give isl_val
*isl_aff_eval(__isl_take isl_aff
*aff
,
9445 __isl_take isl_point
*pnt
)
9449 isl_local_space
*ls
;
9451 if (isl_aff_check_has_domain_space(aff
, isl_point_peek_space(pnt
)) < 0)
9453 is_void
= isl_point_is_void(pnt
);
9457 return eval_void(aff
, pnt
);
9459 ls
= isl_aff_get_domain_local_space(aff
);
9460 pnt
= isl_local_space_lift_point(ls
, pnt
);
9462 v
= eval(aff
->v
, isl_point_peek_vec(pnt
));
9465 isl_point_free(pnt
);
9470 isl_point_free(pnt
);