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
)
502 space
= isl_reordering_peek_space(r
);
503 res
= isl_vec_alloc(vec
->ctx
,
504 2 + isl_space_dim(space
, isl_dim_all
) + n_div
);
507 isl_seq_cpy(res
->el
, vec
->el
, 2);
508 isl_seq_clr(res
->el
+ 2, res
->size
- 2);
509 for (i
= 0; i
< r
->len
; ++i
)
510 isl_int_set(res
->el
[2 + r
->pos
[i
]], vec
->el
[2 + i
]);
512 isl_reordering_free(r
);
517 isl_reordering_free(r
);
521 /* Reorder the dimensions of the domain of "aff" according
522 * to the given reordering.
524 __isl_give isl_aff
*isl_aff_realign_domain(__isl_take isl_aff
*aff
,
525 __isl_take isl_reordering
*r
)
527 aff
= isl_aff_cow(aff
);
531 r
= isl_reordering_extend(r
, aff
->ls
->div
->n_row
);
532 aff
->v
= vec_reorder(aff
->v
, isl_reordering_copy(r
),
533 aff
->ls
->div
->n_row
);
534 aff
->ls
= isl_local_space_realign(aff
->ls
, r
);
536 if (!aff
->v
|| !aff
->ls
)
537 return isl_aff_free(aff
);
542 isl_reordering_free(r
);
546 __isl_give isl_aff
*isl_aff_align_params(__isl_take isl_aff
*aff
,
547 __isl_take isl_space
*model
)
549 isl_bool equal_params
;
554 equal_params
= isl_space_has_equal_params(aff
->ls
->dim
, model
);
555 if (equal_params
< 0)
560 model
= isl_space_drop_dims(model
, isl_dim_in
,
561 0, isl_space_dim(model
, isl_dim_in
));
562 model
= isl_space_drop_dims(model
, isl_dim_out
,
563 0, isl_space_dim(model
, isl_dim_out
));
564 exp
= isl_parameter_alignment_reordering(aff
->ls
->dim
, model
);
565 exp
= isl_reordering_extend_space(exp
,
566 isl_aff_get_domain_space(aff
));
567 aff
= isl_aff_realign_domain(aff
, exp
);
570 isl_space_free(model
);
573 isl_space_free(model
);
578 /* Is "aff" obviously equal to zero?
580 * If the denominator is zero, then "aff" is not equal to zero.
582 isl_bool
isl_aff_plain_is_zero(__isl_keep isl_aff
*aff
)
585 return isl_bool_error
;
587 if (isl_int_is_zero(aff
->v
->el
[0]))
588 return isl_bool_false
;
589 return isl_seq_first_non_zero(aff
->v
->el
+ 1, aff
->v
->size
- 1) < 0;
592 /* Does "aff" represent NaN?
594 isl_bool
isl_aff_is_nan(__isl_keep isl_aff
*aff
)
597 return isl_bool_error
;
599 return isl_seq_first_non_zero(aff
->v
->el
, 2) < 0;
602 /* Are "aff1" and "aff2" obviously equal?
604 * NaN is not equal to anything, not even to another NaN.
606 isl_bool
isl_aff_plain_is_equal(__isl_keep isl_aff
*aff1
,
607 __isl_keep isl_aff
*aff2
)
612 return isl_bool_error
;
614 if (isl_aff_is_nan(aff1
) || isl_aff_is_nan(aff2
))
615 return isl_bool_false
;
617 equal
= isl_local_space_is_equal(aff1
->ls
, aff2
->ls
);
618 if (equal
< 0 || !equal
)
621 return isl_vec_is_equal(aff1
->v
, aff2
->v
);
624 /* Return the common denominator of "aff" in "v".
626 * We cannot return anything meaningful in case of a NaN.
628 isl_stat
isl_aff_get_denominator(__isl_keep isl_aff
*aff
, isl_int
*v
)
631 return isl_stat_error
;
632 if (isl_aff_is_nan(aff
))
633 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
634 "cannot get denominator of NaN", return isl_stat_error
);
635 isl_int_set(*v
, aff
->v
->el
[0]);
639 /* Return the common denominator of "aff".
641 __isl_give isl_val
*isl_aff_get_denominator_val(__isl_keep isl_aff
*aff
)
648 ctx
= isl_aff_get_ctx(aff
);
649 if (isl_aff_is_nan(aff
))
650 return isl_val_nan(ctx
);
651 return isl_val_int_from_isl_int(ctx
, aff
->v
->el
[0]);
654 /* Return the constant term of "aff".
656 __isl_give isl_val
*isl_aff_get_constant_val(__isl_keep isl_aff
*aff
)
664 ctx
= isl_aff_get_ctx(aff
);
665 if (isl_aff_is_nan(aff
))
666 return isl_val_nan(ctx
);
667 v
= isl_val_rat_from_isl_int(ctx
, aff
->v
->el
[1], aff
->v
->el
[0]);
668 return isl_val_normalize(v
);
671 /* Return the coefficient of the variable of type "type" at position "pos"
674 __isl_give isl_val
*isl_aff_get_coefficient_val(__isl_keep isl_aff
*aff
,
675 enum isl_dim_type type
, int pos
)
683 ctx
= isl_aff_get_ctx(aff
);
684 if (type
== isl_dim_out
)
685 isl_die(ctx
, isl_error_invalid
,
686 "output/set dimension does not have a coefficient",
688 if (type
== isl_dim_in
)
691 if (pos
>= isl_local_space_dim(aff
->ls
, type
))
692 isl_die(ctx
, isl_error_invalid
,
693 "position out of bounds", return NULL
);
695 if (isl_aff_is_nan(aff
))
696 return isl_val_nan(ctx
);
697 pos
+= isl_local_space_offset(aff
->ls
, type
);
698 v
= isl_val_rat_from_isl_int(ctx
, aff
->v
->el
[1 + pos
], aff
->v
->el
[0]);
699 return isl_val_normalize(v
);
702 /* Return the sign of the coefficient of the variable of type "type"
703 * at position "pos" of "aff".
705 int isl_aff_coefficient_sgn(__isl_keep isl_aff
*aff
, enum isl_dim_type type
,
713 ctx
= isl_aff_get_ctx(aff
);
714 if (type
== isl_dim_out
)
715 isl_die(ctx
, isl_error_invalid
,
716 "output/set dimension does not have a coefficient",
718 if (type
== isl_dim_in
)
721 if (pos
>= isl_local_space_dim(aff
->ls
, type
))
722 isl_die(ctx
, isl_error_invalid
,
723 "position out of bounds", return 0);
725 pos
+= isl_local_space_offset(aff
->ls
, type
);
726 return isl_int_sgn(aff
->v
->el
[1 + pos
]);
729 /* Replace the numerator of the constant term of "aff" by "v".
731 * A NaN is unaffected by this operation.
733 __isl_give isl_aff
*isl_aff_set_constant(__isl_take isl_aff
*aff
, isl_int v
)
737 if (isl_aff_is_nan(aff
))
739 aff
= isl_aff_cow(aff
);
743 aff
->v
= isl_vec_cow(aff
->v
);
745 return isl_aff_free(aff
);
747 isl_int_set(aff
->v
->el
[1], v
);
752 /* Replace the constant term of "aff" by "v".
754 * A NaN is unaffected by this operation.
756 __isl_give isl_aff
*isl_aff_set_constant_val(__isl_take isl_aff
*aff
,
757 __isl_take isl_val
*v
)
762 if (isl_aff_is_nan(aff
)) {
767 if (!isl_val_is_rat(v
))
768 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
769 "expecting rational value", goto error
);
771 if (isl_int_eq(aff
->v
->el
[1], v
->n
) &&
772 isl_int_eq(aff
->v
->el
[0], v
->d
)) {
777 aff
= isl_aff_cow(aff
);
780 aff
->v
= isl_vec_cow(aff
->v
);
784 if (isl_int_eq(aff
->v
->el
[0], v
->d
)) {
785 isl_int_set(aff
->v
->el
[1], v
->n
);
786 } else if (isl_int_is_one(v
->d
)) {
787 isl_int_mul(aff
->v
->el
[1], aff
->v
->el
[0], v
->n
);
789 isl_seq_scale(aff
->v
->el
+ 1,
790 aff
->v
->el
+ 1, v
->d
, aff
->v
->size
- 1);
791 isl_int_mul(aff
->v
->el
[1], aff
->v
->el
[0], v
->n
);
792 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], v
->d
);
793 aff
->v
= isl_vec_normalize(aff
->v
);
806 /* Add "v" to the constant term of "aff".
808 * A NaN is unaffected by this operation.
810 __isl_give isl_aff
*isl_aff_add_constant(__isl_take isl_aff
*aff
, isl_int v
)
812 if (isl_int_is_zero(v
))
817 if (isl_aff_is_nan(aff
))
819 aff
= isl_aff_cow(aff
);
823 aff
->v
= isl_vec_cow(aff
->v
);
825 return isl_aff_free(aff
);
827 isl_int_addmul(aff
->v
->el
[1], aff
->v
->el
[0], v
);
832 /* Add "v" to the constant term of "aff".
834 * A NaN is unaffected by this operation.
836 __isl_give isl_aff
*isl_aff_add_constant_val(__isl_take isl_aff
*aff
,
837 __isl_take isl_val
*v
)
842 if (isl_aff_is_nan(aff
) || isl_val_is_zero(v
)) {
847 if (!isl_val_is_rat(v
))
848 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
849 "expecting rational value", goto error
);
851 aff
= isl_aff_cow(aff
);
855 aff
->v
= isl_vec_cow(aff
->v
);
859 if (isl_int_is_one(v
->d
)) {
860 isl_int_addmul(aff
->v
->el
[1], aff
->v
->el
[0], v
->n
);
861 } else if (isl_int_eq(aff
->v
->el
[0], v
->d
)) {
862 isl_int_add(aff
->v
->el
[1], aff
->v
->el
[1], v
->n
);
863 aff
->v
= isl_vec_normalize(aff
->v
);
867 isl_seq_scale(aff
->v
->el
+ 1,
868 aff
->v
->el
+ 1, v
->d
, aff
->v
->size
- 1);
869 isl_int_addmul(aff
->v
->el
[1], aff
->v
->el
[0], v
->n
);
870 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], v
->d
);
871 aff
->v
= isl_vec_normalize(aff
->v
);
884 __isl_give isl_aff
*isl_aff_add_constant_si(__isl_take isl_aff
*aff
, int v
)
889 isl_int_set_si(t
, v
);
890 aff
= isl_aff_add_constant(aff
, t
);
896 /* Add "v" to the numerator of the constant term of "aff".
898 * A NaN is unaffected by this operation.
900 __isl_give isl_aff
*isl_aff_add_constant_num(__isl_take isl_aff
*aff
, isl_int v
)
902 if (isl_int_is_zero(v
))
907 if (isl_aff_is_nan(aff
))
909 aff
= isl_aff_cow(aff
);
913 aff
->v
= isl_vec_cow(aff
->v
);
915 return isl_aff_free(aff
);
917 isl_int_add(aff
->v
->el
[1], aff
->v
->el
[1], v
);
922 /* Add "v" to the numerator of the constant term of "aff".
924 * A NaN is unaffected by this operation.
926 __isl_give isl_aff
*isl_aff_add_constant_num_si(__isl_take isl_aff
*aff
, int v
)
934 isl_int_set_si(t
, v
);
935 aff
= isl_aff_add_constant_num(aff
, t
);
941 /* Replace the numerator of the constant term of "aff" by "v".
943 * A NaN is unaffected by this operation.
945 __isl_give isl_aff
*isl_aff_set_constant_si(__isl_take isl_aff
*aff
, int v
)
949 if (isl_aff_is_nan(aff
))
951 aff
= isl_aff_cow(aff
);
955 aff
->v
= isl_vec_cow(aff
->v
);
957 return isl_aff_free(aff
);
959 isl_int_set_si(aff
->v
->el
[1], v
);
964 /* Replace the numerator of the coefficient of the variable of type "type"
965 * at position "pos" of "aff" by "v".
967 * A NaN is unaffected by this operation.
969 __isl_give isl_aff
*isl_aff_set_coefficient(__isl_take isl_aff
*aff
,
970 enum isl_dim_type type
, int pos
, isl_int v
)
975 if (type
== isl_dim_out
)
976 isl_die(aff
->v
->ctx
, isl_error_invalid
,
977 "output/set dimension does not have a coefficient",
978 return isl_aff_free(aff
));
979 if (type
== isl_dim_in
)
982 if (pos
>= isl_local_space_dim(aff
->ls
, type
))
983 isl_die(aff
->v
->ctx
, isl_error_invalid
,
984 "position out of bounds", return isl_aff_free(aff
));
986 if (isl_aff_is_nan(aff
))
988 aff
= isl_aff_cow(aff
);
992 aff
->v
= isl_vec_cow(aff
->v
);
994 return isl_aff_free(aff
);
996 pos
+= isl_local_space_offset(aff
->ls
, type
);
997 isl_int_set(aff
->v
->el
[1 + pos
], v
);
1002 /* Replace the numerator of the coefficient of the variable of type "type"
1003 * at position "pos" of "aff" by "v".
1005 * A NaN is unaffected by this operation.
1007 __isl_give isl_aff
*isl_aff_set_coefficient_si(__isl_take isl_aff
*aff
,
1008 enum isl_dim_type type
, int pos
, int v
)
1013 if (type
== isl_dim_out
)
1014 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1015 "output/set dimension does not have a coefficient",
1016 return isl_aff_free(aff
));
1017 if (type
== isl_dim_in
)
1020 if (pos
< 0 || pos
>= isl_local_space_dim(aff
->ls
, type
))
1021 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1022 "position out of bounds", return isl_aff_free(aff
));
1024 if (isl_aff_is_nan(aff
))
1026 pos
+= isl_local_space_offset(aff
->ls
, type
);
1027 if (isl_int_cmp_si(aff
->v
->el
[1 + pos
], v
) == 0)
1030 aff
= isl_aff_cow(aff
);
1034 aff
->v
= isl_vec_cow(aff
->v
);
1036 return isl_aff_free(aff
);
1038 isl_int_set_si(aff
->v
->el
[1 + pos
], v
);
1043 /* Replace the coefficient of the variable of type "type" at position "pos"
1046 * A NaN is unaffected by this operation.
1048 __isl_give isl_aff
*isl_aff_set_coefficient_val(__isl_take isl_aff
*aff
,
1049 enum isl_dim_type type
, int pos
, __isl_take isl_val
*v
)
1054 if (type
== isl_dim_out
)
1055 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1056 "output/set dimension does not have a coefficient",
1058 if (type
== isl_dim_in
)
1061 if (pos
>= isl_local_space_dim(aff
->ls
, type
))
1062 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1063 "position out of bounds", goto error
);
1065 if (isl_aff_is_nan(aff
)) {
1069 if (!isl_val_is_rat(v
))
1070 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1071 "expecting rational value", goto error
);
1073 pos
+= isl_local_space_offset(aff
->ls
, type
);
1074 if (isl_int_eq(aff
->v
->el
[1 + pos
], v
->n
) &&
1075 isl_int_eq(aff
->v
->el
[0], v
->d
)) {
1080 aff
= isl_aff_cow(aff
);
1083 aff
->v
= isl_vec_cow(aff
->v
);
1087 if (isl_int_eq(aff
->v
->el
[0], v
->d
)) {
1088 isl_int_set(aff
->v
->el
[1 + pos
], v
->n
);
1089 } else if (isl_int_is_one(v
->d
)) {
1090 isl_int_mul(aff
->v
->el
[1 + pos
], aff
->v
->el
[0], v
->n
);
1092 isl_seq_scale(aff
->v
->el
+ 1,
1093 aff
->v
->el
+ 1, v
->d
, aff
->v
->size
- 1);
1094 isl_int_mul(aff
->v
->el
[1 + pos
], aff
->v
->el
[0], v
->n
);
1095 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], v
->d
);
1096 aff
->v
= isl_vec_normalize(aff
->v
);
1109 /* Add "v" to the coefficient of the variable of type "type"
1110 * at position "pos" of "aff".
1112 * A NaN is unaffected by this operation.
1114 __isl_give isl_aff
*isl_aff_add_coefficient(__isl_take isl_aff
*aff
,
1115 enum isl_dim_type type
, int pos
, isl_int v
)
1120 if (type
== isl_dim_out
)
1121 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1122 "output/set dimension does not have a coefficient",
1123 return isl_aff_free(aff
));
1124 if (type
== isl_dim_in
)
1127 if (pos
>= isl_local_space_dim(aff
->ls
, type
))
1128 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1129 "position out of bounds", return isl_aff_free(aff
));
1131 if (isl_aff_is_nan(aff
))
1133 aff
= isl_aff_cow(aff
);
1137 aff
->v
= isl_vec_cow(aff
->v
);
1139 return isl_aff_free(aff
);
1141 pos
+= isl_local_space_offset(aff
->ls
, type
);
1142 isl_int_addmul(aff
->v
->el
[1 + pos
], aff
->v
->el
[0], v
);
1147 /* Add "v" to the coefficient of the variable of type "type"
1148 * at position "pos" of "aff".
1150 * A NaN is unaffected by this operation.
1152 __isl_give isl_aff
*isl_aff_add_coefficient_val(__isl_take isl_aff
*aff
,
1153 enum isl_dim_type type
, int pos
, __isl_take isl_val
*v
)
1158 if (isl_val_is_zero(v
)) {
1163 if (type
== isl_dim_out
)
1164 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1165 "output/set dimension does not have a coefficient",
1167 if (type
== isl_dim_in
)
1170 if (pos
>= isl_local_space_dim(aff
->ls
, type
))
1171 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1172 "position out of bounds", goto error
);
1174 if (isl_aff_is_nan(aff
)) {
1178 if (!isl_val_is_rat(v
))
1179 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1180 "expecting rational value", goto error
);
1182 aff
= isl_aff_cow(aff
);
1186 aff
->v
= isl_vec_cow(aff
->v
);
1190 pos
+= isl_local_space_offset(aff
->ls
, type
);
1191 if (isl_int_is_one(v
->d
)) {
1192 isl_int_addmul(aff
->v
->el
[1 + pos
], aff
->v
->el
[0], v
->n
);
1193 } else if (isl_int_eq(aff
->v
->el
[0], v
->d
)) {
1194 isl_int_add(aff
->v
->el
[1 + pos
], aff
->v
->el
[1 + pos
], v
->n
);
1195 aff
->v
= isl_vec_normalize(aff
->v
);
1199 isl_seq_scale(aff
->v
->el
+ 1,
1200 aff
->v
->el
+ 1, v
->d
, aff
->v
->size
- 1);
1201 isl_int_addmul(aff
->v
->el
[1 + pos
], aff
->v
->el
[0], v
->n
);
1202 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], v
->d
);
1203 aff
->v
= isl_vec_normalize(aff
->v
);
1216 __isl_give isl_aff
*isl_aff_add_coefficient_si(__isl_take isl_aff
*aff
,
1217 enum isl_dim_type type
, int pos
, int v
)
1222 isl_int_set_si(t
, v
);
1223 aff
= isl_aff_add_coefficient(aff
, type
, pos
, t
);
1229 __isl_give isl_aff
*isl_aff_get_div(__isl_keep isl_aff
*aff
, int pos
)
1234 return isl_local_space_get_div(aff
->ls
, pos
);
1237 /* Return the negation of "aff".
1239 * As a special case, -NaN = NaN.
1241 __isl_give isl_aff
*isl_aff_neg(__isl_take isl_aff
*aff
)
1245 if (isl_aff_is_nan(aff
))
1247 aff
= isl_aff_cow(aff
);
1250 aff
->v
= isl_vec_cow(aff
->v
);
1252 return isl_aff_free(aff
);
1254 isl_seq_neg(aff
->v
->el
+ 1, aff
->v
->el
+ 1, aff
->v
->size
- 1);
1259 /* Remove divs from the local space that do not appear in the affine
1261 * We currently only remove divs at the end.
1262 * Some intermediate divs may also not appear directly in the affine
1263 * expression, but we would also need to check that no other divs are
1264 * defined in terms of them.
1266 __isl_give isl_aff
*isl_aff_remove_unused_divs(__isl_take isl_aff
*aff
)
1275 n
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
1276 off
= isl_local_space_offset(aff
->ls
, isl_dim_div
);
1278 pos
= isl_seq_last_non_zero(aff
->v
->el
+ 1 + off
, n
) + 1;
1282 aff
= isl_aff_cow(aff
);
1286 aff
->ls
= isl_local_space_drop_dims(aff
->ls
, isl_dim_div
, pos
, n
- pos
);
1287 aff
->v
= isl_vec_drop_els(aff
->v
, 1 + off
+ pos
, n
- pos
);
1288 if (!aff
->ls
|| !aff
->v
)
1289 return isl_aff_free(aff
);
1294 /* Look for any divs in the aff->ls with a denominator equal to one
1295 * and plug them into the affine expression and any subsequent divs
1296 * that may reference the div.
1298 static __isl_give isl_aff
*plug_in_integral_divs(__isl_take isl_aff
*aff
)
1304 isl_local_space
*ls
;
1310 n
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
1312 for (i
= 0; i
< n
; ++i
) {
1313 if (!isl_int_is_one(aff
->ls
->div
->row
[i
][0]))
1315 ls
= isl_local_space_copy(aff
->ls
);
1316 ls
= isl_local_space_substitute_seq(ls
, isl_dim_div
, i
,
1317 aff
->ls
->div
->row
[i
], len
, i
+ 1, n
- (i
+ 1));
1318 vec
= isl_vec_copy(aff
->v
);
1319 vec
= isl_vec_cow(vec
);
1325 pos
= isl_local_space_offset(aff
->ls
, isl_dim_div
) + i
;
1326 isl_seq_substitute(vec
->el
, pos
, aff
->ls
->div
->row
[i
],
1331 isl_vec_free(aff
->v
);
1333 isl_local_space_free(aff
->ls
);
1340 isl_local_space_free(ls
);
1341 return isl_aff_free(aff
);
1344 /* Look for any divs j that appear with a unit coefficient inside
1345 * the definitions of other divs i and plug them into the definitions
1348 * In particular, an expression of the form
1350 * floor((f(..) + floor(g(..)/n))/m)
1354 * floor((n * f(..) + g(..))/(n * m))
1356 * This simplification is correct because we can move the expression
1357 * f(..) into the inner floor in the original expression to obtain
1359 * floor(floor((n * f(..) + g(..))/n)/m)
1361 * from which we can derive the simplified expression.
1363 static __isl_give isl_aff
*plug_in_unit_divs(__isl_take isl_aff
*aff
)
1371 n
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
1372 off
= isl_local_space_offset(aff
->ls
, isl_dim_div
);
1373 for (i
= 1; i
< n
; ++i
) {
1374 for (j
= 0; j
< i
; ++j
) {
1375 if (!isl_int_is_one(aff
->ls
->div
->row
[i
][1 + off
+ j
]))
1377 aff
->ls
= isl_local_space_substitute_seq(aff
->ls
,
1378 isl_dim_div
, j
, aff
->ls
->div
->row
[j
],
1379 aff
->v
->size
, i
, 1);
1381 return isl_aff_free(aff
);
1388 /* Swap divs "a" and "b" in "aff", which is assumed to be non-NULL.
1390 * Even though this function is only called on isl_affs with a single
1391 * reference, we are careful to only change aff->v and aff->ls together.
1393 static __isl_give isl_aff
*swap_div(__isl_take isl_aff
*aff
, int a
, int b
)
1395 unsigned off
= isl_local_space_offset(aff
->ls
, isl_dim_div
);
1396 isl_local_space
*ls
;
1399 ls
= isl_local_space_copy(aff
->ls
);
1400 ls
= isl_local_space_swap_div(ls
, a
, b
);
1401 v
= isl_vec_copy(aff
->v
);
1406 isl_int_swap(v
->el
[1 + off
+ a
], v
->el
[1 + off
+ b
]);
1407 isl_vec_free(aff
->v
);
1409 isl_local_space_free(aff
->ls
);
1415 isl_local_space_free(ls
);
1416 return isl_aff_free(aff
);
1419 /* Merge divs "a" and "b" in "aff", which is assumed to be non-NULL.
1421 * We currently do not actually remove div "b", but simply add its
1422 * coefficient to that of "a" and then zero it out.
1424 static __isl_give isl_aff
*merge_divs(__isl_take isl_aff
*aff
, int a
, int b
)
1426 unsigned off
= isl_local_space_offset(aff
->ls
, isl_dim_div
);
1428 if (isl_int_is_zero(aff
->v
->el
[1 + off
+ b
]))
1431 aff
->v
= isl_vec_cow(aff
->v
);
1433 return isl_aff_free(aff
);
1435 isl_int_add(aff
->v
->el
[1 + off
+ a
],
1436 aff
->v
->el
[1 + off
+ a
], aff
->v
->el
[1 + off
+ b
]);
1437 isl_int_set_si(aff
->v
->el
[1 + off
+ b
], 0);
1442 /* Sort the divs in the local space of "aff" according to
1443 * the comparison function "cmp_row" in isl_local_space.c,
1444 * combining the coefficients of identical divs.
1446 * Reordering divs does not change the semantics of "aff",
1447 * so there is no need to call isl_aff_cow.
1448 * Moreover, this function is currently only called on isl_affs
1449 * with a single reference.
1451 static __isl_give isl_aff
*sort_divs(__isl_take isl_aff
*aff
)
1458 n
= isl_aff_dim(aff
, isl_dim_div
);
1459 for (i
= 1; i
< n
; ++i
) {
1460 for (j
= i
- 1; j
>= 0; --j
) {
1461 int cmp
= isl_mat_cmp_div(aff
->ls
->div
, j
, j
+ 1);
1465 aff
= merge_divs(aff
, j
, j
+ 1);
1467 aff
= swap_div(aff
, j
, j
+ 1);
1476 /* Normalize the representation of "aff".
1478 * This function should only be called of "new" isl_affs, i.e.,
1479 * with only a single reference. We therefore do not need to
1480 * worry about affecting other instances.
1482 __isl_give isl_aff
*isl_aff_normalize(__isl_take isl_aff
*aff
)
1486 aff
->v
= isl_vec_normalize(aff
->v
);
1488 return isl_aff_free(aff
);
1489 aff
= plug_in_integral_divs(aff
);
1490 aff
= plug_in_unit_divs(aff
);
1491 aff
= sort_divs(aff
);
1492 aff
= isl_aff_remove_unused_divs(aff
);
1496 /* Given f, return floor(f).
1497 * If f is an integer expression, then just return f.
1498 * If f is a constant, then return the constant floor(f).
1499 * Otherwise, if f = g/m, write g = q m + r,
1500 * create a new div d = [r/m] and return the expression q + d.
1501 * The coefficients in r are taken to lie between -m/2 and m/2.
1503 * reduce_div_coefficients performs the same normalization.
1505 * As a special case, floor(NaN) = NaN.
1507 __isl_give isl_aff
*isl_aff_floor(__isl_take isl_aff
*aff
)
1517 if (isl_aff_is_nan(aff
))
1519 if (isl_int_is_one(aff
->v
->el
[0]))
1522 aff
= isl_aff_cow(aff
);
1526 aff
->v
= isl_vec_cow(aff
->v
);
1528 return isl_aff_free(aff
);
1530 if (isl_aff_is_cst(aff
)) {
1531 isl_int_fdiv_q(aff
->v
->el
[1], aff
->v
->el
[1], aff
->v
->el
[0]);
1532 isl_int_set_si(aff
->v
->el
[0], 1);
1536 div
= isl_vec_copy(aff
->v
);
1537 div
= isl_vec_cow(div
);
1539 return isl_aff_free(aff
);
1541 ctx
= isl_aff_get_ctx(aff
);
1542 isl_int_fdiv_q(aff
->v
->el
[0], aff
->v
->el
[0], ctx
->two
);
1543 for (i
= 1; i
< aff
->v
->size
; ++i
) {
1544 isl_int_fdiv_r(div
->el
[i
], div
->el
[i
], div
->el
[0]);
1545 isl_int_fdiv_q(aff
->v
->el
[i
], aff
->v
->el
[i
], div
->el
[0]);
1546 if (isl_int_gt(div
->el
[i
], aff
->v
->el
[0])) {
1547 isl_int_sub(div
->el
[i
], div
->el
[i
], div
->el
[0]);
1548 isl_int_add_ui(aff
->v
->el
[i
], aff
->v
->el
[i
], 1);
1552 aff
->ls
= isl_local_space_add_div(aff
->ls
, div
);
1554 return isl_aff_free(aff
);
1556 size
= aff
->v
->size
;
1557 aff
->v
= isl_vec_extend(aff
->v
, size
+ 1);
1559 return isl_aff_free(aff
);
1560 isl_int_set_si(aff
->v
->el
[0], 1);
1561 isl_int_set_si(aff
->v
->el
[size
], 1);
1563 aff
= isl_aff_normalize(aff
);
1570 * aff mod m = aff - m * floor(aff/m)
1572 * with m an integer value.
1574 __isl_give isl_aff
*isl_aff_mod_val(__isl_take isl_aff
*aff
,
1575 __isl_take isl_val
*m
)
1582 if (!isl_val_is_int(m
))
1583 isl_die(isl_val_get_ctx(m
), isl_error_invalid
,
1584 "expecting integer modulo", goto error
);
1586 res
= isl_aff_copy(aff
);
1587 aff
= isl_aff_scale_down_val(aff
, isl_val_copy(m
));
1588 aff
= isl_aff_floor(aff
);
1589 aff
= isl_aff_scale_val(aff
, m
);
1590 res
= isl_aff_sub(res
, aff
);
1601 * pwaff mod m = pwaff - m * floor(pwaff/m)
1603 __isl_give isl_pw_aff
*isl_pw_aff_mod(__isl_take isl_pw_aff
*pwaff
, isl_int m
)
1607 res
= isl_pw_aff_copy(pwaff
);
1608 pwaff
= isl_pw_aff_scale_down(pwaff
, m
);
1609 pwaff
= isl_pw_aff_floor(pwaff
);
1610 pwaff
= isl_pw_aff_scale(pwaff
, m
);
1611 res
= isl_pw_aff_sub(res
, pwaff
);
1618 * pa mod m = pa - m * floor(pa/m)
1620 * with m an integer value.
1622 __isl_give isl_pw_aff
*isl_pw_aff_mod_val(__isl_take isl_pw_aff
*pa
,
1623 __isl_take isl_val
*m
)
1627 if (!isl_val_is_int(m
))
1628 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
1629 "expecting integer modulo", goto error
);
1630 pa
= isl_pw_aff_mod(pa
, m
->n
);
1634 isl_pw_aff_free(pa
);
1639 /* Given f, return ceil(f).
1640 * If f is an integer expression, then just return f.
1641 * Otherwise, let f be the expression
1647 * floor((e + m - 1)/m)
1649 * As a special case, ceil(NaN) = NaN.
1651 __isl_give isl_aff
*isl_aff_ceil(__isl_take isl_aff
*aff
)
1656 if (isl_aff_is_nan(aff
))
1658 if (isl_int_is_one(aff
->v
->el
[0]))
1661 aff
= isl_aff_cow(aff
);
1664 aff
->v
= isl_vec_cow(aff
->v
);
1666 return isl_aff_free(aff
);
1668 isl_int_add(aff
->v
->el
[1], aff
->v
->el
[1], aff
->v
->el
[0]);
1669 isl_int_sub_ui(aff
->v
->el
[1], aff
->v
->el
[1], 1);
1670 aff
= isl_aff_floor(aff
);
1675 /* Apply the expansion computed by isl_merge_divs.
1676 * The expansion itself is given by "exp" while the resulting
1677 * list of divs is given by "div".
1679 __isl_give isl_aff
*isl_aff_expand_divs(__isl_take isl_aff
*aff
,
1680 __isl_take isl_mat
*div
, int *exp
)
1686 aff
= isl_aff_cow(aff
);
1690 old_n_div
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
1691 new_n_div
= isl_mat_rows(div
);
1692 offset
= 1 + isl_local_space_offset(aff
->ls
, isl_dim_div
);
1694 aff
->v
= isl_vec_expand(aff
->v
, offset
, old_n_div
, exp
, new_n_div
);
1695 aff
->ls
= isl_local_space_replace_divs(aff
->ls
, div
);
1696 if (!aff
->v
|| !aff
->ls
)
1697 return isl_aff_free(aff
);
1705 /* Add two affine expressions that live in the same local space.
1707 static __isl_give isl_aff
*add_expanded(__isl_take isl_aff
*aff1
,
1708 __isl_take isl_aff
*aff2
)
1712 aff1
= isl_aff_cow(aff1
);
1716 aff1
->v
= isl_vec_cow(aff1
->v
);
1722 isl_int_gcd(gcd
, aff1
->v
->el
[0], aff2
->v
->el
[0]);
1723 isl_int_divexact(f
, aff2
->v
->el
[0], gcd
);
1724 isl_seq_scale(aff1
->v
->el
+ 1, aff1
->v
->el
+ 1, f
, aff1
->v
->size
- 1);
1725 isl_int_divexact(f
, aff1
->v
->el
[0], gcd
);
1726 isl_seq_addmul(aff1
->v
->el
+ 1, f
, aff2
->v
->el
+ 1, aff1
->v
->size
- 1);
1727 isl_int_divexact(f
, aff2
->v
->el
[0], gcd
);
1728 isl_int_mul(aff1
->v
->el
[0], aff1
->v
->el
[0], f
);
1740 /* Return the sum of "aff1" and "aff2".
1742 * If either of the two is NaN, then the result is NaN.
1744 __isl_give isl_aff
*isl_aff_add(__isl_take isl_aff
*aff1
,
1745 __isl_take isl_aff
*aff2
)
1756 ctx
= isl_aff_get_ctx(aff1
);
1757 if (!isl_space_is_equal(aff1
->ls
->dim
, aff2
->ls
->dim
))
1758 isl_die(ctx
, isl_error_invalid
,
1759 "spaces don't match", goto error
);
1761 if (isl_aff_is_nan(aff1
)) {
1765 if (isl_aff_is_nan(aff2
)) {
1770 n_div1
= isl_aff_dim(aff1
, isl_dim_div
);
1771 n_div2
= isl_aff_dim(aff2
, isl_dim_div
);
1772 if (n_div1
== 0 && n_div2
== 0)
1773 return add_expanded(aff1
, aff2
);
1775 exp1
= isl_alloc_array(ctx
, int, n_div1
);
1776 exp2
= isl_alloc_array(ctx
, int, n_div2
);
1777 if ((n_div1
&& !exp1
) || (n_div2
&& !exp2
))
1780 div
= isl_merge_divs(aff1
->ls
->div
, aff2
->ls
->div
, exp1
, exp2
);
1781 aff1
= isl_aff_expand_divs(aff1
, isl_mat_copy(div
), exp1
);
1782 aff2
= isl_aff_expand_divs(aff2
, div
, exp2
);
1786 return add_expanded(aff1
, aff2
);
1795 __isl_give isl_aff
*isl_aff_sub(__isl_take isl_aff
*aff1
,
1796 __isl_take isl_aff
*aff2
)
1798 return isl_aff_add(aff1
, isl_aff_neg(aff2
));
1801 /* Return the result of scaling "aff" by a factor of "f".
1803 * As a special case, f * NaN = NaN.
1805 __isl_give isl_aff
*isl_aff_scale(__isl_take isl_aff
*aff
, isl_int f
)
1811 if (isl_aff_is_nan(aff
))
1814 if (isl_int_is_one(f
))
1817 aff
= isl_aff_cow(aff
);
1820 aff
->v
= isl_vec_cow(aff
->v
);
1822 return isl_aff_free(aff
);
1824 if (isl_int_is_pos(f
) && isl_int_is_divisible_by(aff
->v
->el
[0], f
)) {
1825 isl_int_divexact(aff
->v
->el
[0], aff
->v
->el
[0], f
);
1830 isl_int_gcd(gcd
, aff
->v
->el
[0], f
);
1831 isl_int_divexact(aff
->v
->el
[0], aff
->v
->el
[0], gcd
);
1832 isl_int_divexact(gcd
, f
, gcd
);
1833 isl_seq_scale(aff
->v
->el
+ 1, aff
->v
->el
+ 1, gcd
, aff
->v
->size
- 1);
1839 /* Multiple "aff" by "v".
1841 __isl_give isl_aff
*isl_aff_scale_val(__isl_take isl_aff
*aff
,
1842 __isl_take isl_val
*v
)
1847 if (isl_val_is_one(v
)) {
1852 if (!isl_val_is_rat(v
))
1853 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1854 "expecting rational factor", goto error
);
1856 aff
= isl_aff_scale(aff
, v
->n
);
1857 aff
= isl_aff_scale_down(aff
, v
->d
);
1867 /* Return the result of scaling "aff" down by a factor of "f".
1869 * As a special case, NaN/f = NaN.
1871 __isl_give isl_aff
*isl_aff_scale_down(__isl_take isl_aff
*aff
, isl_int f
)
1877 if (isl_aff_is_nan(aff
))
1880 if (isl_int_is_one(f
))
1883 aff
= isl_aff_cow(aff
);
1887 if (isl_int_is_zero(f
))
1888 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1889 "cannot scale down by zero", return isl_aff_free(aff
));
1891 aff
->v
= isl_vec_cow(aff
->v
);
1893 return isl_aff_free(aff
);
1896 isl_seq_gcd(aff
->v
->el
+ 1, aff
->v
->size
- 1, &gcd
);
1897 isl_int_gcd(gcd
, gcd
, f
);
1898 isl_seq_scale_down(aff
->v
->el
+ 1, aff
->v
->el
+ 1, gcd
, aff
->v
->size
- 1);
1899 isl_int_divexact(gcd
, f
, gcd
);
1900 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], gcd
);
1906 /* Divide "aff" by "v".
1908 __isl_give isl_aff
*isl_aff_scale_down_val(__isl_take isl_aff
*aff
,
1909 __isl_take isl_val
*v
)
1914 if (isl_val_is_one(v
)) {
1919 if (!isl_val_is_rat(v
))
1920 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1921 "expecting rational factor", goto error
);
1922 if (!isl_val_is_pos(v
))
1923 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1924 "factor needs to be positive", goto error
);
1926 aff
= isl_aff_scale(aff
, v
->d
);
1927 aff
= isl_aff_scale_down(aff
, v
->n
);
1937 __isl_give isl_aff
*isl_aff_scale_down_ui(__isl_take isl_aff
*aff
, unsigned f
)
1945 isl_int_set_ui(v
, f
);
1946 aff
= isl_aff_scale_down(aff
, v
);
1952 __isl_give isl_aff
*isl_aff_set_dim_name(__isl_take isl_aff
*aff
,
1953 enum isl_dim_type type
, unsigned pos
, const char *s
)
1955 aff
= isl_aff_cow(aff
);
1958 if (type
== isl_dim_out
)
1959 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1960 "cannot set name of output/set dimension",
1961 return isl_aff_free(aff
));
1962 if (type
== isl_dim_in
)
1964 aff
->ls
= isl_local_space_set_dim_name(aff
->ls
, type
, pos
, s
);
1966 return isl_aff_free(aff
);
1971 __isl_give isl_aff
*isl_aff_set_dim_id(__isl_take isl_aff
*aff
,
1972 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
1974 aff
= isl_aff_cow(aff
);
1977 if (type
== isl_dim_out
)
1978 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1979 "cannot set name of output/set dimension",
1981 if (type
== isl_dim_in
)
1983 aff
->ls
= isl_local_space_set_dim_id(aff
->ls
, type
, pos
, id
);
1985 return isl_aff_free(aff
);
1994 /* Replace the identifier of the input tuple of "aff" by "id".
1995 * type is currently required to be equal to isl_dim_in
1997 __isl_give isl_aff
*isl_aff_set_tuple_id(__isl_take isl_aff
*aff
,
1998 enum isl_dim_type type
, __isl_take isl_id
*id
)
2000 aff
= isl_aff_cow(aff
);
2003 if (type
!= isl_dim_in
)
2004 isl_die(aff
->v
->ctx
, isl_error_invalid
,
2005 "cannot only set id of input tuple", goto error
);
2006 aff
->ls
= isl_local_space_set_tuple_id(aff
->ls
, isl_dim_set
, id
);
2008 return isl_aff_free(aff
);
2017 /* Exploit the equalities in "eq" to simplify the affine expression
2018 * and the expressions of the integer divisions in the local space.
2019 * The integer divisions in this local space are assumed to appear
2020 * as regular dimensions in "eq".
2022 static __isl_give isl_aff
*isl_aff_substitute_equalities_lifted(
2023 __isl_take isl_aff
*aff
, __isl_take isl_basic_set
*eq
)
2031 if (eq
->n_eq
== 0) {
2032 isl_basic_set_free(eq
);
2036 aff
= isl_aff_cow(aff
);
2040 aff
->ls
= isl_local_space_substitute_equalities(aff
->ls
,
2041 isl_basic_set_copy(eq
));
2042 aff
->v
= isl_vec_cow(aff
->v
);
2043 if (!aff
->ls
|| !aff
->v
)
2046 total
= 1 + isl_space_dim(eq
->dim
, isl_dim_all
);
2048 for (i
= 0; i
< eq
->n_eq
; ++i
) {
2049 j
= isl_seq_last_non_zero(eq
->eq
[i
], total
+ n_div
);
2050 if (j
< 0 || j
== 0 || j
>= total
)
2053 isl_seq_elim(aff
->v
->el
+ 1, eq
->eq
[i
], j
, total
,
2057 isl_basic_set_free(eq
);
2058 aff
= isl_aff_normalize(aff
);
2061 isl_basic_set_free(eq
);
2066 /* Exploit the equalities in "eq" to simplify the affine expression
2067 * and the expressions of the integer divisions in the local space.
2069 __isl_give isl_aff
*isl_aff_substitute_equalities(__isl_take isl_aff
*aff
,
2070 __isl_take isl_basic_set
*eq
)
2076 n_div
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
2078 eq
= isl_basic_set_add_dims(eq
, isl_dim_set
, n_div
);
2079 return isl_aff_substitute_equalities_lifted(aff
, eq
);
2081 isl_basic_set_free(eq
);
2086 /* Look for equalities among the variables shared by context and aff
2087 * and the integer divisions of aff, if any.
2088 * The equalities are then used to eliminate coefficients and/or integer
2089 * divisions from aff.
2091 __isl_give isl_aff
*isl_aff_gist(__isl_take isl_aff
*aff
,
2092 __isl_take isl_set
*context
)
2094 isl_basic_set
*hull
;
2099 n_div
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
2101 isl_basic_set
*bset
;
2102 isl_local_space
*ls
;
2103 context
= isl_set_add_dims(context
, isl_dim_set
, n_div
);
2104 ls
= isl_aff_get_domain_local_space(aff
);
2105 bset
= isl_basic_set_from_local_space(ls
);
2106 bset
= isl_basic_set_lift(bset
);
2107 bset
= isl_basic_set_flatten(bset
);
2108 context
= isl_set_intersect(context
,
2109 isl_set_from_basic_set(bset
));
2112 hull
= isl_set_affine_hull(context
);
2113 return isl_aff_substitute_equalities_lifted(aff
, hull
);
2116 isl_set_free(context
);
2120 __isl_give isl_aff
*isl_aff_gist_params(__isl_take isl_aff
*aff
,
2121 __isl_take isl_set
*context
)
2123 isl_set
*dom_context
= isl_set_universe(isl_aff_get_domain_space(aff
));
2124 dom_context
= isl_set_intersect_params(dom_context
, context
);
2125 return isl_aff_gist(aff
, dom_context
);
2128 /* Return a basic set containing those elements in the space
2129 * of aff where it is positive. "rational" should not be set.
2131 * If "aff" is NaN, then it is not positive.
2133 static __isl_give isl_basic_set
*aff_pos_basic_set(__isl_take isl_aff
*aff
,
2136 isl_constraint
*ineq
;
2137 isl_basic_set
*bset
;
2142 if (isl_aff_is_nan(aff
)) {
2143 isl_space
*space
= isl_aff_get_domain_space(aff
);
2145 return isl_basic_set_empty(space
);
2148 isl_die(isl_aff_get_ctx(aff
), isl_error_unsupported
,
2149 "rational sets not supported", goto error
);
2151 ineq
= isl_inequality_from_aff(aff
);
2152 c
= isl_constraint_get_constant_val(ineq
);
2153 c
= isl_val_sub_ui(c
, 1);
2154 ineq
= isl_constraint_set_constant_val(ineq
, c
);
2156 bset
= isl_basic_set_from_constraint(ineq
);
2157 bset
= isl_basic_set_simplify(bset
);
2164 /* Return a basic set containing those elements in the space
2165 * of aff where it is non-negative.
2166 * If "rational" is set, then return a rational basic set.
2168 * If "aff" is NaN, then it is not non-negative (it's not negative either).
2170 static __isl_give isl_basic_set
*aff_nonneg_basic_set(
2171 __isl_take isl_aff
*aff
, int rational
)
2173 isl_constraint
*ineq
;
2174 isl_basic_set
*bset
;
2178 if (isl_aff_is_nan(aff
)) {
2179 isl_space
*space
= isl_aff_get_domain_space(aff
);
2181 return isl_basic_set_empty(space
);
2184 ineq
= isl_inequality_from_aff(aff
);
2186 bset
= isl_basic_set_from_constraint(ineq
);
2188 bset
= isl_basic_set_set_rational(bset
);
2189 bset
= isl_basic_set_simplify(bset
);
2193 /* Return a basic set containing those elements in the space
2194 * of aff where it is non-negative.
2196 __isl_give isl_basic_set
*isl_aff_nonneg_basic_set(__isl_take isl_aff
*aff
)
2198 return aff_nonneg_basic_set(aff
, 0);
2201 /* Return a basic set containing those elements in the domain space
2202 * of "aff" where it is positive.
2204 __isl_give isl_basic_set
*isl_aff_pos_basic_set(__isl_take isl_aff
*aff
)
2206 aff
= isl_aff_add_constant_num_si(aff
, -1);
2207 return isl_aff_nonneg_basic_set(aff
);
2210 /* Return a basic set containing those elements in the domain space
2211 * of aff where it is negative.
2213 __isl_give isl_basic_set
*isl_aff_neg_basic_set(__isl_take isl_aff
*aff
)
2215 aff
= isl_aff_neg(aff
);
2216 return isl_aff_pos_basic_set(aff
);
2219 /* Return a basic set containing those elements in the space
2220 * of aff where it is zero.
2221 * If "rational" is set, then return a rational basic set.
2223 * If "aff" is NaN, then it is not zero.
2225 static __isl_give isl_basic_set
*aff_zero_basic_set(__isl_take isl_aff
*aff
,
2228 isl_constraint
*ineq
;
2229 isl_basic_set
*bset
;
2233 if (isl_aff_is_nan(aff
)) {
2234 isl_space
*space
= isl_aff_get_domain_space(aff
);
2236 return isl_basic_set_empty(space
);
2239 ineq
= isl_equality_from_aff(aff
);
2241 bset
= isl_basic_set_from_constraint(ineq
);
2243 bset
= isl_basic_set_set_rational(bset
);
2244 bset
= isl_basic_set_simplify(bset
);
2248 /* Return a basic set containing those elements in the space
2249 * of aff where it is zero.
2251 __isl_give isl_basic_set
*isl_aff_zero_basic_set(__isl_take isl_aff
*aff
)
2253 return aff_zero_basic_set(aff
, 0);
2256 /* Return a basic set containing those elements in the shared space
2257 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
2259 __isl_give isl_basic_set
*isl_aff_ge_basic_set(__isl_take isl_aff
*aff1
,
2260 __isl_take isl_aff
*aff2
)
2262 aff1
= isl_aff_sub(aff1
, aff2
);
2264 return isl_aff_nonneg_basic_set(aff1
);
2267 /* Return a basic set containing those elements in the shared domain space
2268 * of "aff1" and "aff2" where "aff1" is greater than "aff2".
2270 __isl_give isl_basic_set
*isl_aff_gt_basic_set(__isl_take isl_aff
*aff1
,
2271 __isl_take isl_aff
*aff2
)
2273 aff1
= isl_aff_sub(aff1
, aff2
);
2275 return isl_aff_pos_basic_set(aff1
);
2278 /* Return a set containing those elements in the shared space
2279 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
2281 __isl_give isl_set
*isl_aff_ge_set(__isl_take isl_aff
*aff1
,
2282 __isl_take isl_aff
*aff2
)
2284 return isl_set_from_basic_set(isl_aff_ge_basic_set(aff1
, aff2
));
2287 /* Return a set containing those elements in the shared domain space
2288 * of aff1 and aff2 where aff1 is greater than aff2.
2290 * If either of the two inputs is NaN, then the result is empty,
2291 * as comparisons with NaN always return false.
2293 __isl_give isl_set
*isl_aff_gt_set(__isl_take isl_aff
*aff1
,
2294 __isl_take isl_aff
*aff2
)
2296 return isl_set_from_basic_set(isl_aff_gt_basic_set(aff1
, aff2
));
2299 /* Return a basic set containing those elements in the shared space
2300 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
2302 __isl_give isl_basic_set
*isl_aff_le_basic_set(__isl_take isl_aff
*aff1
,
2303 __isl_take isl_aff
*aff2
)
2305 return isl_aff_ge_basic_set(aff2
, aff1
);
2308 /* Return a basic set containing those elements in the shared domain space
2309 * of "aff1" and "aff2" where "aff1" is smaller than "aff2".
2311 __isl_give isl_basic_set
*isl_aff_lt_basic_set(__isl_take isl_aff
*aff1
,
2312 __isl_take isl_aff
*aff2
)
2314 return isl_aff_gt_basic_set(aff2
, aff1
);
2317 /* Return a set containing those elements in the shared space
2318 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
2320 __isl_give isl_set
*isl_aff_le_set(__isl_take isl_aff
*aff1
,
2321 __isl_take isl_aff
*aff2
)
2323 return isl_aff_ge_set(aff2
, aff1
);
2326 /* Return a set containing those elements in the shared domain space
2327 * of "aff1" and "aff2" where "aff1" is smaller than "aff2".
2329 __isl_give isl_set
*isl_aff_lt_set(__isl_take isl_aff
*aff1
,
2330 __isl_take isl_aff
*aff2
)
2332 return isl_set_from_basic_set(isl_aff_lt_basic_set(aff1
, aff2
));
2335 /* Return a basic set containing those elements in the shared space
2336 * of aff1 and aff2 where aff1 and aff2 are equal.
2338 __isl_give isl_basic_set
*isl_aff_eq_basic_set(__isl_take isl_aff
*aff1
,
2339 __isl_take isl_aff
*aff2
)
2341 aff1
= isl_aff_sub(aff1
, aff2
);
2343 return isl_aff_zero_basic_set(aff1
);
2346 /* Return a set containing those elements in the shared space
2347 * of aff1 and aff2 where aff1 and aff2 are equal.
2349 __isl_give isl_set
*isl_aff_eq_set(__isl_take isl_aff
*aff1
,
2350 __isl_take isl_aff
*aff2
)
2352 return isl_set_from_basic_set(isl_aff_eq_basic_set(aff1
, aff2
));
2355 /* Return a set containing those elements in the shared domain space
2356 * of aff1 and aff2 where aff1 and aff2 are not equal.
2358 * If either of the two inputs is NaN, then the result is empty,
2359 * as comparisons with NaN always return false.
2361 __isl_give isl_set
*isl_aff_ne_set(__isl_take isl_aff
*aff1
,
2362 __isl_take isl_aff
*aff2
)
2364 isl_set
*set_lt
, *set_gt
;
2366 set_lt
= isl_aff_lt_set(isl_aff_copy(aff1
),
2367 isl_aff_copy(aff2
));
2368 set_gt
= isl_aff_gt_set(aff1
, aff2
);
2369 return isl_set_union_disjoint(set_lt
, set_gt
);
2372 __isl_give isl_aff
*isl_aff_add_on_domain(__isl_keep isl_set
*dom
,
2373 __isl_take isl_aff
*aff1
, __isl_take isl_aff
*aff2
)
2375 aff1
= isl_aff_add(aff1
, aff2
);
2376 aff1
= isl_aff_gist(aff1
, isl_set_copy(dom
));
2380 int isl_aff_is_empty(__isl_keep isl_aff
*aff
)
2388 /* Check whether the given affine expression has non-zero coefficient
2389 * for any dimension in the given range or if any of these dimensions
2390 * appear with non-zero coefficients in any of the integer divisions
2391 * involved in the affine expression.
2393 isl_bool
isl_aff_involves_dims(__isl_keep isl_aff
*aff
,
2394 enum isl_dim_type type
, unsigned first
, unsigned n
)
2399 isl_bool involves
= isl_bool_false
;
2402 return isl_bool_error
;
2404 return isl_bool_false
;
2406 ctx
= isl_aff_get_ctx(aff
);
2407 if (first
+ n
> isl_aff_dim(aff
, type
))
2408 isl_die(ctx
, isl_error_invalid
,
2409 "range out of bounds", return isl_bool_error
);
2411 active
= isl_local_space_get_active(aff
->ls
, aff
->v
->el
+ 2);
2415 first
+= isl_local_space_offset(aff
->ls
, type
) - 1;
2416 for (i
= 0; i
< n
; ++i
)
2417 if (active
[first
+ i
]) {
2418 involves
= isl_bool_true
;
2427 return isl_bool_error
;
2430 __isl_give isl_aff
*isl_aff_drop_dims(__isl_take isl_aff
*aff
,
2431 enum isl_dim_type type
, unsigned first
, unsigned n
)
2437 if (type
== isl_dim_out
)
2438 isl_die(aff
->v
->ctx
, isl_error_invalid
,
2439 "cannot drop output/set dimension",
2440 return isl_aff_free(aff
));
2441 if (type
== isl_dim_in
)
2443 if (n
== 0 && !isl_local_space_is_named_or_nested(aff
->ls
, type
))
2446 ctx
= isl_aff_get_ctx(aff
);
2447 if (first
+ n
> isl_local_space_dim(aff
->ls
, type
))
2448 isl_die(ctx
, isl_error_invalid
, "range out of bounds",
2449 return isl_aff_free(aff
));
2451 aff
= isl_aff_cow(aff
);
2455 aff
->ls
= isl_local_space_drop_dims(aff
->ls
, type
, first
, n
);
2457 return isl_aff_free(aff
);
2459 first
+= 1 + isl_local_space_offset(aff
->ls
, type
);
2460 aff
->v
= isl_vec_drop_els(aff
->v
, first
, n
);
2462 return isl_aff_free(aff
);
2467 /* Drop the "n" domain dimensions starting at "first" from "aff",
2468 * after checking that they do not appear in the affine expression.
2470 static __isl_give isl_aff
*drop_domain(__isl_take isl_aff
*aff
, unsigned first
,
2475 involves
= isl_aff_involves_dims(aff
, isl_dim_in
, first
, 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 return isl_aff_drop_dims(aff
, isl_dim_in
, first
, n
);
2485 /* Project the domain of the affine expression onto its parameter space.
2486 * The affine expression may not involve any of the domain dimensions.
2488 __isl_give isl_aff
*isl_aff_project_domain_on_params(__isl_take isl_aff
*aff
)
2493 n
= isl_aff_dim(aff
, isl_dim_in
);
2494 aff
= drop_domain(aff
, 0, n
);
2495 space
= isl_aff_get_domain_space(aff
);
2496 space
= isl_space_params(space
);
2497 aff
= isl_aff_reset_domain_space(aff
, space
);
2501 /* Check that the domain of "aff" is a product.
2503 static isl_stat
check_domain_product(__isl_keep isl_aff
*aff
)
2505 isl_bool is_product
;
2507 is_product
= isl_space_is_product(isl_aff_peek_domain_space(aff
));
2509 return isl_stat_error
;
2511 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2512 "domain is not a product", return isl_stat_error
);
2516 /* Given an affine function with a domain of the form [A -> B] that
2517 * does not depend on B, return the same function on domain A.
2519 __isl_give isl_aff
*isl_aff_domain_factor_domain(__isl_take isl_aff
*aff
)
2524 if (check_domain_product(aff
) < 0)
2525 return isl_aff_free(aff
);
2526 space
= isl_aff_get_domain_space(aff
);
2527 n
= isl_space_dim(space
, isl_dim_set
);
2528 space
= isl_space_factor_domain(space
);
2529 n_in
= isl_space_dim(space
, isl_dim_set
);
2530 aff
= drop_domain(aff
, n_in
, n
- n_in
);
2531 aff
= isl_aff_reset_domain_space(aff
, space
);
2535 /* Convert an affine expression defined over a parameter domain
2536 * into one that is defined over a zero-dimensional set.
2538 __isl_give isl_aff
*isl_aff_from_range(__isl_take isl_aff
*aff
)
2540 isl_local_space
*ls
;
2542 ls
= isl_aff_take_domain_local_space(aff
);
2543 ls
= isl_local_space_set_from_params(ls
);
2544 aff
= isl_aff_restore_domain_local_space(aff
, ls
);
2549 __isl_give isl_aff
*isl_aff_insert_dims(__isl_take isl_aff
*aff
,
2550 enum isl_dim_type type
, unsigned first
, unsigned n
)
2556 if (type
== isl_dim_out
)
2557 isl_die(aff
->v
->ctx
, isl_error_invalid
,
2558 "cannot insert output/set dimensions",
2559 return isl_aff_free(aff
));
2560 if (type
== isl_dim_in
)
2562 if (n
== 0 && !isl_local_space_is_named_or_nested(aff
->ls
, type
))
2565 ctx
= isl_aff_get_ctx(aff
);
2566 if (first
> isl_local_space_dim(aff
->ls
, type
))
2567 isl_die(ctx
, isl_error_invalid
, "position out of bounds",
2568 return isl_aff_free(aff
));
2570 aff
= isl_aff_cow(aff
);
2574 aff
->ls
= isl_local_space_insert_dims(aff
->ls
, type
, first
, n
);
2576 return isl_aff_free(aff
);
2578 first
+= 1 + isl_local_space_offset(aff
->ls
, type
);
2579 aff
->v
= isl_vec_insert_zero_els(aff
->v
, first
, n
);
2581 return isl_aff_free(aff
);
2586 __isl_give isl_aff
*isl_aff_add_dims(__isl_take isl_aff
*aff
,
2587 enum isl_dim_type type
, unsigned n
)
2591 pos
= isl_aff_dim(aff
, type
);
2593 return isl_aff_insert_dims(aff
, type
, pos
, n
);
2596 __isl_give isl_pw_aff
*isl_pw_aff_add_dims(__isl_take isl_pw_aff
*pwaff
,
2597 enum isl_dim_type type
, unsigned n
)
2601 pos
= isl_pw_aff_dim(pwaff
, type
);
2603 return isl_pw_aff_insert_dims(pwaff
, type
, pos
, n
);
2606 /* Move the "n" dimensions of "src_type" starting at "src_pos" of "aff"
2607 * to dimensions of "dst_type" at "dst_pos".
2609 * We only support moving input dimensions to parameters and vice versa.
2611 __isl_give isl_aff
*isl_aff_move_dims(__isl_take isl_aff
*aff
,
2612 enum isl_dim_type dst_type
, unsigned dst_pos
,
2613 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
2621 !isl_local_space_is_named_or_nested(aff
->ls
, src_type
) &&
2622 !isl_local_space_is_named_or_nested(aff
->ls
, dst_type
))
2625 if (dst_type
== isl_dim_out
|| src_type
== isl_dim_out
)
2626 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2627 "cannot move output/set dimension",
2628 return isl_aff_free(aff
));
2629 if (dst_type
== isl_dim_div
|| src_type
== isl_dim_div
)
2630 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2631 "cannot move divs", return isl_aff_free(aff
));
2632 if (dst_type
== isl_dim_in
)
2633 dst_type
= isl_dim_set
;
2634 if (src_type
== isl_dim_in
)
2635 src_type
= isl_dim_set
;
2637 if (src_pos
+ n
> isl_local_space_dim(aff
->ls
, src_type
))
2638 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2639 "range out of bounds", return isl_aff_free(aff
));
2640 if (dst_type
== src_type
)
2641 isl_die(isl_aff_get_ctx(aff
), isl_error_unsupported
,
2642 "moving dims within the same type not supported",
2643 return isl_aff_free(aff
));
2645 aff
= isl_aff_cow(aff
);
2649 g_src_pos
= 1 + isl_local_space_offset(aff
->ls
, src_type
) + src_pos
;
2650 g_dst_pos
= 1 + isl_local_space_offset(aff
->ls
, dst_type
) + dst_pos
;
2651 if (dst_type
> src_type
)
2654 aff
->v
= isl_vec_move_els(aff
->v
, g_dst_pos
, g_src_pos
, n
);
2655 aff
->ls
= isl_local_space_move_dims(aff
->ls
, dst_type
, dst_pos
,
2656 src_type
, src_pos
, n
);
2657 if (!aff
->v
|| !aff
->ls
)
2658 return isl_aff_free(aff
);
2660 aff
= sort_divs(aff
);
2665 __isl_give isl_pw_aff
*isl_pw_aff_from_aff(__isl_take isl_aff
*aff
)
2667 isl_set
*dom
= isl_set_universe(isl_aff_get_domain_space(aff
));
2668 return isl_pw_aff_alloc(dom
, aff
);
2671 #define isl_aff_involves_nan isl_aff_is_nan
2674 #define PW isl_pw_aff
2678 #define EL_IS_ZERO is_empty
2682 #define IS_ZERO is_empty
2685 #undef DEFAULT_IS_ZERO
2686 #define DEFAULT_IS_ZERO 0
2692 #include <isl_pw_templ.c>
2693 #include <isl_pw_eval.c>
2694 #include <isl_pw_hash.c>
2695 #include <isl_pw_union_opt.c>
2698 #define UNION isl_union_pw_aff
2700 #define PART isl_pw_aff
2702 #define PARTS pw_aff
2704 #include <isl_union_single.c>
2705 #include <isl_union_neg.c>
2707 static __isl_give isl_set
*align_params_pw_pw_set_and(
2708 __isl_take isl_pw_aff
*pwaff1
, __isl_take isl_pw_aff
*pwaff2
,
2709 __isl_give isl_set
*(*fn
)(__isl_take isl_pw_aff
*pwaff1
,
2710 __isl_take isl_pw_aff
*pwaff2
))
2712 isl_bool equal_params
;
2714 if (!pwaff1
|| !pwaff2
)
2716 equal_params
= isl_space_has_equal_params(pwaff1
->dim
, pwaff2
->dim
);
2717 if (equal_params
< 0)
2720 return fn(pwaff1
, pwaff2
);
2721 if (isl_pw_aff_check_named_params(pwaff1
) < 0 ||
2722 isl_pw_aff_check_named_params(pwaff2
) < 0)
2724 pwaff1
= isl_pw_aff_align_params(pwaff1
, isl_pw_aff_get_space(pwaff2
));
2725 pwaff2
= isl_pw_aff_align_params(pwaff2
, isl_pw_aff_get_space(pwaff1
));
2726 return fn(pwaff1
, pwaff2
);
2728 isl_pw_aff_free(pwaff1
);
2729 isl_pw_aff_free(pwaff2
);
2733 /* Align the parameters of the to isl_pw_aff arguments and
2734 * then apply a function "fn" on them that returns an isl_map.
2736 static __isl_give isl_map
*align_params_pw_pw_map_and(
2737 __isl_take isl_pw_aff
*pa1
, __isl_take isl_pw_aff
*pa2
,
2738 __isl_give isl_map
*(*fn
)(__isl_take isl_pw_aff
*pa1
,
2739 __isl_take isl_pw_aff
*pa2
))
2741 isl_bool equal_params
;
2745 equal_params
= isl_space_has_equal_params(pa1
->dim
, pa2
->dim
);
2746 if (equal_params
< 0)
2749 return fn(pa1
, pa2
);
2750 if (isl_pw_aff_check_named_params(pa1
) < 0 ||
2751 isl_pw_aff_check_named_params(pa2
) < 0)
2753 pa1
= isl_pw_aff_align_params(pa1
, isl_pw_aff_get_space(pa2
));
2754 pa2
= isl_pw_aff_align_params(pa2
, isl_pw_aff_get_space(pa1
));
2755 return fn(pa1
, pa2
);
2757 isl_pw_aff_free(pa1
);
2758 isl_pw_aff_free(pa2
);
2762 /* Compute a piecewise quasi-affine expression with a domain that
2763 * is the union of those of pwaff1 and pwaff2 and such that on each
2764 * cell, the quasi-affine expression is the maximum of those of pwaff1
2765 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2766 * cell, then the associated expression is the defined one.
2768 static __isl_give isl_pw_aff
*pw_aff_union_max(__isl_take isl_pw_aff
*pwaff1
,
2769 __isl_take isl_pw_aff
*pwaff2
)
2771 return isl_pw_aff_union_opt_cmp(pwaff1
, pwaff2
, &isl_aff_ge_set
);
2774 __isl_give isl_pw_aff
*isl_pw_aff_union_max(__isl_take isl_pw_aff
*pwaff1
,
2775 __isl_take isl_pw_aff
*pwaff2
)
2777 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
,
2781 /* Compute a piecewise quasi-affine expression with a domain that
2782 * is the union of those of pwaff1 and pwaff2 and such that on each
2783 * cell, the quasi-affine expression is the minimum of those of pwaff1
2784 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2785 * cell, then the associated expression is the defined one.
2787 static __isl_give isl_pw_aff
*pw_aff_union_min(__isl_take isl_pw_aff
*pwaff1
,
2788 __isl_take isl_pw_aff
*pwaff2
)
2790 return isl_pw_aff_union_opt_cmp(pwaff1
, pwaff2
, &isl_aff_le_set
);
2793 __isl_give isl_pw_aff
*isl_pw_aff_union_min(__isl_take isl_pw_aff
*pwaff1
,
2794 __isl_take isl_pw_aff
*pwaff2
)
2796 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
,
2800 __isl_give isl_pw_aff
*isl_pw_aff_union_opt(__isl_take isl_pw_aff
*pwaff1
,
2801 __isl_take isl_pw_aff
*pwaff2
, int max
)
2804 return isl_pw_aff_union_max(pwaff1
, pwaff2
);
2806 return isl_pw_aff_union_min(pwaff1
, pwaff2
);
2809 /* Construct a map with as domain the domain of pwaff and
2810 * one-dimensional range corresponding to the affine expressions.
2812 static __isl_give isl_map
*map_from_pw_aff(__isl_take isl_pw_aff
*pwaff
)
2821 dim
= isl_pw_aff_get_space(pwaff
);
2822 map
= isl_map_empty(dim
);
2824 for (i
= 0; i
< pwaff
->n
; ++i
) {
2825 isl_basic_map
*bmap
;
2828 bmap
= isl_basic_map_from_aff(isl_aff_copy(pwaff
->p
[i
].aff
));
2829 map_i
= isl_map_from_basic_map(bmap
);
2830 map_i
= isl_map_intersect_domain(map_i
,
2831 isl_set_copy(pwaff
->p
[i
].set
));
2832 map
= isl_map_union_disjoint(map
, map_i
);
2835 isl_pw_aff_free(pwaff
);
2840 /* Construct a map with as domain the domain of pwaff and
2841 * one-dimensional range corresponding to the affine expressions.
2843 __isl_give isl_map
*isl_map_from_pw_aff(__isl_take isl_pw_aff
*pwaff
)
2847 if (isl_space_is_set(pwaff
->dim
))
2848 isl_die(isl_pw_aff_get_ctx(pwaff
), isl_error_invalid
,
2849 "space of input is not a map", goto error
);
2850 return map_from_pw_aff(pwaff
);
2852 isl_pw_aff_free(pwaff
);
2856 /* Construct a one-dimensional set with as parameter domain
2857 * the domain of pwaff and the single set dimension
2858 * corresponding to the affine expressions.
2860 __isl_give isl_set
*isl_set_from_pw_aff(__isl_take isl_pw_aff
*pwaff
)
2864 if (!isl_space_is_set(pwaff
->dim
))
2865 isl_die(isl_pw_aff_get_ctx(pwaff
), isl_error_invalid
,
2866 "space of input is not a set", goto error
);
2867 return map_from_pw_aff(pwaff
);
2869 isl_pw_aff_free(pwaff
);
2873 /* Return a set containing those elements in the domain
2874 * of "pwaff" where it satisfies "fn" (if complement is 0) or
2875 * does not satisfy "fn" (if complement is 1).
2877 * The pieces with a NaN never belong to the result since
2878 * NaN does not satisfy any property.
2880 static __isl_give isl_set
*pw_aff_locus(__isl_take isl_pw_aff
*pwaff
,
2881 __isl_give isl_basic_set
*(*fn
)(__isl_take isl_aff
*aff
, int rational
),
2890 set
= isl_set_empty(isl_pw_aff_get_domain_space(pwaff
));
2892 for (i
= 0; i
< pwaff
->n
; ++i
) {
2893 isl_basic_set
*bset
;
2894 isl_set
*set_i
, *locus
;
2897 if (isl_aff_is_nan(pwaff
->p
[i
].aff
))
2900 rational
= isl_set_has_rational(pwaff
->p
[i
].set
);
2901 bset
= fn(isl_aff_copy(pwaff
->p
[i
].aff
), rational
);
2902 locus
= isl_set_from_basic_set(bset
);
2903 set_i
= isl_set_copy(pwaff
->p
[i
].set
);
2905 set_i
= isl_set_subtract(set_i
, locus
);
2907 set_i
= isl_set_intersect(set_i
, locus
);
2908 set
= isl_set_union_disjoint(set
, set_i
);
2911 isl_pw_aff_free(pwaff
);
2916 /* Return a set containing those elements in the domain
2917 * of "pa" where it is positive.
2919 __isl_give isl_set
*isl_pw_aff_pos_set(__isl_take isl_pw_aff
*pa
)
2921 return pw_aff_locus(pa
, &aff_pos_basic_set
, 0);
2924 /* Return a set containing those elements in the domain
2925 * of pwaff where it is non-negative.
2927 __isl_give isl_set
*isl_pw_aff_nonneg_set(__isl_take isl_pw_aff
*pwaff
)
2929 return pw_aff_locus(pwaff
, &aff_nonneg_basic_set
, 0);
2932 /* Return a set containing those elements in the domain
2933 * of pwaff where it is zero.
2935 __isl_give isl_set
*isl_pw_aff_zero_set(__isl_take isl_pw_aff
*pwaff
)
2937 return pw_aff_locus(pwaff
, &aff_zero_basic_set
, 0);
2940 /* Return a set containing those elements in the domain
2941 * of pwaff where it is not zero.
2943 __isl_give isl_set
*isl_pw_aff_non_zero_set(__isl_take isl_pw_aff
*pwaff
)
2945 return pw_aff_locus(pwaff
, &aff_zero_basic_set
, 1);
2948 /* Return a set containing those elements in the shared domain
2949 * of pwaff1 and pwaff2 where pwaff1 is greater than (or equal) to pwaff2.
2951 * We compute the difference on the shared domain and then construct
2952 * the set of values where this difference is non-negative.
2953 * If strict is set, we first subtract 1 from the difference.
2954 * If equal is set, we only return the elements where pwaff1 and pwaff2
2957 static __isl_give isl_set
*pw_aff_gte_set(__isl_take isl_pw_aff
*pwaff1
,
2958 __isl_take isl_pw_aff
*pwaff2
, int strict
, int equal
)
2960 isl_set
*set1
, *set2
;
2962 set1
= isl_pw_aff_domain(isl_pw_aff_copy(pwaff1
));
2963 set2
= isl_pw_aff_domain(isl_pw_aff_copy(pwaff2
));
2964 set1
= isl_set_intersect(set1
, set2
);
2965 pwaff1
= isl_pw_aff_intersect_domain(pwaff1
, isl_set_copy(set1
));
2966 pwaff2
= isl_pw_aff_intersect_domain(pwaff2
, isl_set_copy(set1
));
2967 pwaff1
= isl_pw_aff_add(pwaff1
, isl_pw_aff_neg(pwaff2
));
2970 isl_space
*dim
= isl_set_get_space(set1
);
2972 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(dim
));
2973 aff
= isl_aff_add_constant_si(aff
, -1);
2974 pwaff1
= isl_pw_aff_add(pwaff1
, isl_pw_aff_alloc(set1
, aff
));
2979 return isl_pw_aff_zero_set(pwaff1
);
2980 return isl_pw_aff_nonneg_set(pwaff1
);
2983 /* Return a set containing those elements in the shared domain
2984 * of pwaff1 and pwaff2 where pwaff1 is equal to pwaff2.
2986 static __isl_give isl_set
*pw_aff_eq_set(__isl_take isl_pw_aff
*pwaff1
,
2987 __isl_take isl_pw_aff
*pwaff2
)
2989 return pw_aff_gte_set(pwaff1
, pwaff2
, 0, 1);
2992 __isl_give isl_set
*isl_pw_aff_eq_set(__isl_take isl_pw_aff
*pwaff1
,
2993 __isl_take isl_pw_aff
*pwaff2
)
2995 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_eq_set
);
2998 /* Return a set containing those elements in the shared domain
2999 * of pwaff1 and pwaff2 where pwaff1 is greater than or equal to pwaff2.
3001 static __isl_give isl_set
*pw_aff_ge_set(__isl_take isl_pw_aff
*pwaff1
,
3002 __isl_take isl_pw_aff
*pwaff2
)
3004 return pw_aff_gte_set(pwaff1
, pwaff2
, 0, 0);
3007 __isl_give isl_set
*isl_pw_aff_ge_set(__isl_take isl_pw_aff
*pwaff1
,
3008 __isl_take isl_pw_aff
*pwaff2
)
3010 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_ge_set
);
3013 /* Return a set containing those elements in the shared domain
3014 * of pwaff1 and pwaff2 where pwaff1 is strictly greater than pwaff2.
3016 static __isl_give isl_set
*pw_aff_gt_set(__isl_take isl_pw_aff
*pwaff1
,
3017 __isl_take isl_pw_aff
*pwaff2
)
3019 return pw_aff_gte_set(pwaff1
, pwaff2
, 1, 0);
3022 __isl_give isl_set
*isl_pw_aff_gt_set(__isl_take isl_pw_aff
*pwaff1
,
3023 __isl_take isl_pw_aff
*pwaff2
)
3025 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_gt_set
);
3028 __isl_give isl_set
*isl_pw_aff_le_set(__isl_take isl_pw_aff
*pwaff1
,
3029 __isl_take isl_pw_aff
*pwaff2
)
3031 return isl_pw_aff_ge_set(pwaff2
, pwaff1
);
3034 __isl_give isl_set
*isl_pw_aff_lt_set(__isl_take isl_pw_aff
*pwaff1
,
3035 __isl_take isl_pw_aff
*pwaff2
)
3037 return isl_pw_aff_gt_set(pwaff2
, pwaff1
);
3040 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3041 * where the function values are ordered in the same way as "order",
3042 * which returns a set in the shared domain of its two arguments.
3043 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3045 * Let "pa1" and "pa2" be defined on domains A and B respectively.
3046 * We first pull back the two functions such that they are defined on
3047 * the domain [A -> B]. Then we apply "order", resulting in a set
3048 * in the space [A -> B]. Finally, we unwrap this set to obtain
3049 * a map in the space A -> B.
3051 static __isl_give isl_map
*isl_pw_aff_order_map_aligned(
3052 __isl_take isl_pw_aff
*pa1
, __isl_take isl_pw_aff
*pa2
,
3053 __isl_give isl_set
*(*order
)(__isl_take isl_pw_aff
*pa1
,
3054 __isl_take isl_pw_aff
*pa2
))
3056 isl_space
*space1
, *space2
;
3060 space1
= isl_space_domain(isl_pw_aff_get_space(pa1
));
3061 space2
= isl_space_domain(isl_pw_aff_get_space(pa2
));
3062 space1
= isl_space_map_from_domain_and_range(space1
, space2
);
3063 ma
= isl_multi_aff_domain_map(isl_space_copy(space1
));
3064 pa1
= isl_pw_aff_pullback_multi_aff(pa1
, ma
);
3065 ma
= isl_multi_aff_range_map(space1
);
3066 pa2
= isl_pw_aff_pullback_multi_aff(pa2
, ma
);
3067 set
= order(pa1
, pa2
);
3069 return isl_set_unwrap(set
);
3072 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3073 * where the function values are equal.
3074 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3076 static __isl_give isl_map
*isl_pw_aff_eq_map_aligned(__isl_take isl_pw_aff
*pa1
,
3077 __isl_take isl_pw_aff
*pa2
)
3079 return isl_pw_aff_order_map_aligned(pa1
, pa2
, &isl_pw_aff_eq_set
);
3082 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3083 * where the function values are equal.
3085 __isl_give isl_map
*isl_pw_aff_eq_map(__isl_take isl_pw_aff
*pa1
,
3086 __isl_take isl_pw_aff
*pa2
)
3088 return align_params_pw_pw_map_and(pa1
, pa2
, &isl_pw_aff_eq_map_aligned
);
3091 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3092 * where the function value of "pa1" is less than the function value of "pa2".
3093 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3095 static __isl_give isl_map
*isl_pw_aff_lt_map_aligned(__isl_take isl_pw_aff
*pa1
,
3096 __isl_take isl_pw_aff
*pa2
)
3098 return isl_pw_aff_order_map_aligned(pa1
, pa2
, &isl_pw_aff_lt_set
);
3101 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3102 * where the function value of "pa1" is less than the function value of "pa2".
3104 __isl_give isl_map
*isl_pw_aff_lt_map(__isl_take isl_pw_aff
*pa1
,
3105 __isl_take isl_pw_aff
*pa2
)
3107 return align_params_pw_pw_map_and(pa1
, pa2
, &isl_pw_aff_lt_map_aligned
);
3110 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3111 * where the function value of "pa1" is greater than the function value
3113 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3115 static __isl_give isl_map
*isl_pw_aff_gt_map_aligned(__isl_take isl_pw_aff
*pa1
,
3116 __isl_take isl_pw_aff
*pa2
)
3118 return isl_pw_aff_order_map_aligned(pa1
, pa2
, &isl_pw_aff_gt_set
);
3121 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3122 * where the function value of "pa1" is greater than the function value
3125 __isl_give isl_map
*isl_pw_aff_gt_map(__isl_take isl_pw_aff
*pa1
,
3126 __isl_take isl_pw_aff
*pa2
)
3128 return align_params_pw_pw_map_and(pa1
, pa2
, &isl_pw_aff_gt_map_aligned
);
3131 /* Return a set containing those elements in the shared domain
3132 * of the elements of list1 and list2 where each element in list1
3133 * has the relation specified by "fn" with each element in list2.
3135 static __isl_give isl_set
*pw_aff_list_set(__isl_take isl_pw_aff_list
*list1
,
3136 __isl_take isl_pw_aff_list
*list2
,
3137 __isl_give isl_set
*(*fn
)(__isl_take isl_pw_aff
*pwaff1
,
3138 __isl_take isl_pw_aff
*pwaff2
))
3144 if (!list1
|| !list2
)
3147 ctx
= isl_pw_aff_list_get_ctx(list1
);
3148 if (list1
->n
< 1 || list2
->n
< 1)
3149 isl_die(ctx
, isl_error_invalid
,
3150 "list should contain at least one element", goto error
);
3152 set
= isl_set_universe(isl_pw_aff_get_domain_space(list1
->p
[0]));
3153 for (i
= 0; i
< list1
->n
; ++i
)
3154 for (j
= 0; j
< list2
->n
; ++j
) {
3157 set_ij
= fn(isl_pw_aff_copy(list1
->p
[i
]),
3158 isl_pw_aff_copy(list2
->p
[j
]));
3159 set
= isl_set_intersect(set
, set_ij
);
3162 isl_pw_aff_list_free(list1
);
3163 isl_pw_aff_list_free(list2
);
3166 isl_pw_aff_list_free(list1
);
3167 isl_pw_aff_list_free(list2
);
3171 /* Return a set containing those elements in the shared domain
3172 * of the elements of list1 and list2 where each element in list1
3173 * is equal to each element in list2.
3175 __isl_give isl_set
*isl_pw_aff_list_eq_set(__isl_take isl_pw_aff_list
*list1
,
3176 __isl_take isl_pw_aff_list
*list2
)
3178 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_eq_set
);
3181 __isl_give isl_set
*isl_pw_aff_list_ne_set(__isl_take isl_pw_aff_list
*list1
,
3182 __isl_take isl_pw_aff_list
*list2
)
3184 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_ne_set
);
3187 /* Return a set containing those elements in the shared domain
3188 * of the elements of list1 and list2 where each element in list1
3189 * is less than or equal to each element in list2.
3191 __isl_give isl_set
*isl_pw_aff_list_le_set(__isl_take isl_pw_aff_list
*list1
,
3192 __isl_take isl_pw_aff_list
*list2
)
3194 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_le_set
);
3197 __isl_give isl_set
*isl_pw_aff_list_lt_set(__isl_take isl_pw_aff_list
*list1
,
3198 __isl_take isl_pw_aff_list
*list2
)
3200 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_lt_set
);
3203 __isl_give isl_set
*isl_pw_aff_list_ge_set(__isl_take isl_pw_aff_list
*list1
,
3204 __isl_take isl_pw_aff_list
*list2
)
3206 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_ge_set
);
3209 __isl_give isl_set
*isl_pw_aff_list_gt_set(__isl_take isl_pw_aff_list
*list1
,
3210 __isl_take isl_pw_aff_list
*list2
)
3212 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_gt_set
);
3216 /* Return a set containing those elements in the shared domain
3217 * of pwaff1 and pwaff2 where pwaff1 is not equal to pwaff2.
3219 static __isl_give isl_set
*pw_aff_ne_set(__isl_take isl_pw_aff
*pwaff1
,
3220 __isl_take isl_pw_aff
*pwaff2
)
3222 isl_set
*set_lt
, *set_gt
;
3224 set_lt
= isl_pw_aff_lt_set(isl_pw_aff_copy(pwaff1
),
3225 isl_pw_aff_copy(pwaff2
));
3226 set_gt
= isl_pw_aff_gt_set(pwaff1
, pwaff2
);
3227 return isl_set_union_disjoint(set_lt
, set_gt
);
3230 __isl_give isl_set
*isl_pw_aff_ne_set(__isl_take isl_pw_aff
*pwaff1
,
3231 __isl_take isl_pw_aff
*pwaff2
)
3233 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_ne_set
);
3236 __isl_give isl_pw_aff
*isl_pw_aff_scale_down(__isl_take isl_pw_aff
*pwaff
,
3241 if (isl_int_is_one(v
))
3243 if (!isl_int_is_pos(v
))
3244 isl_die(isl_pw_aff_get_ctx(pwaff
), isl_error_invalid
,
3245 "factor needs to be positive",
3246 return isl_pw_aff_free(pwaff
));
3247 pwaff
= isl_pw_aff_cow(pwaff
);
3253 for (i
= 0; i
< pwaff
->n
; ++i
) {
3254 pwaff
->p
[i
].aff
= isl_aff_scale_down(pwaff
->p
[i
].aff
, v
);
3255 if (!pwaff
->p
[i
].aff
)
3256 return isl_pw_aff_free(pwaff
);
3262 __isl_give isl_pw_aff
*isl_pw_aff_floor(__isl_take isl_pw_aff
*pwaff
)
3266 pwaff
= isl_pw_aff_cow(pwaff
);
3272 for (i
= 0; i
< pwaff
->n
; ++i
) {
3273 pwaff
->p
[i
].aff
= isl_aff_floor(pwaff
->p
[i
].aff
);
3274 if (!pwaff
->p
[i
].aff
)
3275 return isl_pw_aff_free(pwaff
);
3281 __isl_give isl_pw_aff
*isl_pw_aff_ceil(__isl_take isl_pw_aff
*pwaff
)
3285 pwaff
= isl_pw_aff_cow(pwaff
);
3291 for (i
= 0; i
< pwaff
->n
; ++i
) {
3292 pwaff
->p
[i
].aff
= isl_aff_ceil(pwaff
->p
[i
].aff
);
3293 if (!pwaff
->p
[i
].aff
)
3294 return isl_pw_aff_free(pwaff
);
3300 /* Assuming that "cond1" and "cond2" are disjoint,
3301 * return an affine expression that is equal to pwaff1 on cond1
3302 * and to pwaff2 on cond2.
3304 static __isl_give isl_pw_aff
*isl_pw_aff_select(
3305 __isl_take isl_set
*cond1
, __isl_take isl_pw_aff
*pwaff1
,
3306 __isl_take isl_set
*cond2
, __isl_take isl_pw_aff
*pwaff2
)
3308 pwaff1
= isl_pw_aff_intersect_domain(pwaff1
, cond1
);
3309 pwaff2
= isl_pw_aff_intersect_domain(pwaff2
, cond2
);
3311 return isl_pw_aff_add_disjoint(pwaff1
, pwaff2
);
3314 /* Return an affine expression that is equal to pwaff_true for elements
3315 * where "cond" is non-zero and to pwaff_false for elements where "cond"
3317 * That is, return cond ? pwaff_true : pwaff_false;
3319 * If "cond" involves and NaN, then we conservatively return a NaN
3320 * on its entire domain. In principle, we could consider the pieces
3321 * where it is NaN separately from those where it is not.
3323 * If "pwaff_true" and "pwaff_false" are obviously equal to each other,
3324 * then only use the domain of "cond" to restrict the domain.
3326 __isl_give isl_pw_aff
*isl_pw_aff_cond(__isl_take isl_pw_aff
*cond
,
3327 __isl_take isl_pw_aff
*pwaff_true
, __isl_take isl_pw_aff
*pwaff_false
)
3329 isl_set
*cond_true
, *cond_false
;
3334 if (isl_pw_aff_involves_nan(cond
)) {
3335 isl_space
*space
= isl_pw_aff_get_domain_space(cond
);
3336 isl_local_space
*ls
= isl_local_space_from_space(space
);
3337 isl_pw_aff_free(cond
);
3338 isl_pw_aff_free(pwaff_true
);
3339 isl_pw_aff_free(pwaff_false
);
3340 return isl_pw_aff_nan_on_domain(ls
);
3343 pwaff_true
= isl_pw_aff_align_params(pwaff_true
,
3344 isl_pw_aff_get_space(pwaff_false
));
3345 pwaff_false
= isl_pw_aff_align_params(pwaff_false
,
3346 isl_pw_aff_get_space(pwaff_true
));
3347 equal
= isl_pw_aff_plain_is_equal(pwaff_true
, pwaff_false
);
3353 dom
= isl_set_coalesce(isl_pw_aff_domain(cond
));
3354 isl_pw_aff_free(pwaff_false
);
3355 return isl_pw_aff_intersect_domain(pwaff_true
, dom
);
3358 cond_true
= isl_pw_aff_non_zero_set(isl_pw_aff_copy(cond
));
3359 cond_false
= isl_pw_aff_zero_set(cond
);
3360 return isl_pw_aff_select(cond_true
, pwaff_true
,
3361 cond_false
, pwaff_false
);
3363 isl_pw_aff_free(cond
);
3364 isl_pw_aff_free(pwaff_true
);
3365 isl_pw_aff_free(pwaff_false
);
3369 isl_bool
isl_aff_is_cst(__isl_keep isl_aff
*aff
)
3372 return isl_bool_error
;
3374 return isl_seq_first_non_zero(aff
->v
->el
+ 2, aff
->v
->size
- 2) == -1;
3377 /* Check whether pwaff is a piecewise constant.
3379 isl_bool
isl_pw_aff_is_cst(__isl_keep isl_pw_aff
*pwaff
)
3384 return isl_bool_error
;
3386 for (i
= 0; i
< pwaff
->n
; ++i
) {
3387 isl_bool is_cst
= isl_aff_is_cst(pwaff
->p
[i
].aff
);
3388 if (is_cst
< 0 || !is_cst
)
3392 return isl_bool_true
;
3395 /* Are all elements of "mpa" piecewise constants?
3397 isl_bool
isl_multi_pw_aff_is_cst(__isl_keep isl_multi_pw_aff
*mpa
)
3402 return isl_bool_error
;
3404 for (i
= 0; i
< mpa
->n
; ++i
) {
3405 isl_bool is_cst
= isl_pw_aff_is_cst(mpa
->u
.p
[i
]);
3406 if (is_cst
< 0 || !is_cst
)
3410 return isl_bool_true
;
3413 /* Return the product of "aff1" and "aff2".
3415 * If either of the two is NaN, then the result is NaN.
3417 * Otherwise, at least one of "aff1" or "aff2" needs to be a constant.
3419 __isl_give isl_aff
*isl_aff_mul(__isl_take isl_aff
*aff1
,
3420 __isl_take isl_aff
*aff2
)
3425 if (isl_aff_is_nan(aff1
)) {
3429 if (isl_aff_is_nan(aff2
)) {
3434 if (!isl_aff_is_cst(aff2
) && isl_aff_is_cst(aff1
))
3435 return isl_aff_mul(aff2
, aff1
);
3437 if (!isl_aff_is_cst(aff2
))
3438 isl_die(isl_aff_get_ctx(aff1
), isl_error_invalid
,
3439 "at least one affine expression should be constant",
3442 aff1
= isl_aff_cow(aff1
);
3446 aff1
= isl_aff_scale(aff1
, aff2
->v
->el
[1]);
3447 aff1
= isl_aff_scale_down(aff1
, aff2
->v
->el
[0]);
3457 /* Divide "aff1" by "aff2", assuming "aff2" is a constant.
3459 * If either of the two is NaN, then the result is NaN.
3461 __isl_give isl_aff
*isl_aff_div(__isl_take isl_aff
*aff1
,
3462 __isl_take isl_aff
*aff2
)
3470 if (isl_aff_is_nan(aff1
)) {
3474 if (isl_aff_is_nan(aff2
)) {
3479 is_cst
= isl_aff_is_cst(aff2
);
3483 isl_die(isl_aff_get_ctx(aff2
), isl_error_invalid
,
3484 "second argument should be a constant", goto error
);
3489 neg
= isl_int_is_neg(aff2
->v
->el
[1]);
3491 isl_int_neg(aff2
->v
->el
[0], aff2
->v
->el
[0]);
3492 isl_int_neg(aff2
->v
->el
[1], aff2
->v
->el
[1]);
3495 aff1
= isl_aff_scale(aff1
, aff2
->v
->el
[0]);
3496 aff1
= isl_aff_scale_down(aff1
, aff2
->v
->el
[1]);
3499 isl_int_neg(aff2
->v
->el
[0], aff2
->v
->el
[0]);
3500 isl_int_neg(aff2
->v
->el
[1], aff2
->v
->el
[1]);
3511 static __isl_give isl_pw_aff
*pw_aff_add(__isl_take isl_pw_aff
*pwaff1
,
3512 __isl_take isl_pw_aff
*pwaff2
)
3514 return isl_pw_aff_on_shared_domain(pwaff1
, pwaff2
, &isl_aff_add
);
3517 __isl_give isl_pw_aff
*isl_pw_aff_add(__isl_take isl_pw_aff
*pwaff1
,
3518 __isl_take isl_pw_aff
*pwaff2
)
3520 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
, &pw_aff_add
);
3523 __isl_give isl_pw_aff
*isl_pw_aff_union_add(__isl_take isl_pw_aff
*pwaff1
,
3524 __isl_take isl_pw_aff
*pwaff2
)
3526 return isl_pw_aff_union_add_(pwaff1
, pwaff2
);
3529 static __isl_give isl_pw_aff
*pw_aff_mul(__isl_take isl_pw_aff
*pwaff1
,
3530 __isl_take isl_pw_aff
*pwaff2
)
3532 return isl_pw_aff_on_shared_domain(pwaff1
, pwaff2
, &isl_aff_mul
);
3535 __isl_give isl_pw_aff
*isl_pw_aff_mul(__isl_take isl_pw_aff
*pwaff1
,
3536 __isl_take isl_pw_aff
*pwaff2
)
3538 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
, &pw_aff_mul
);
3541 static __isl_give isl_pw_aff
*pw_aff_div(__isl_take isl_pw_aff
*pa1
,
3542 __isl_take isl_pw_aff
*pa2
)
3544 return isl_pw_aff_on_shared_domain(pa1
, pa2
, &isl_aff_div
);
3547 /* Divide "pa1" by "pa2", assuming "pa2" is a piecewise constant.
3549 __isl_give isl_pw_aff
*isl_pw_aff_div(__isl_take isl_pw_aff
*pa1
,
3550 __isl_take isl_pw_aff
*pa2
)
3554 is_cst
= isl_pw_aff_is_cst(pa2
);
3558 isl_die(isl_pw_aff_get_ctx(pa2
), isl_error_invalid
,
3559 "second argument should be a piecewise constant",
3561 return isl_pw_aff_align_params_pw_pw_and(pa1
, pa2
, &pw_aff_div
);
3563 isl_pw_aff_free(pa1
);
3564 isl_pw_aff_free(pa2
);
3568 /* Compute the quotient of the integer division of "pa1" by "pa2"
3569 * with rounding towards zero.
3570 * "pa2" is assumed to be a piecewise constant.
3572 * In particular, return
3574 * pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2)
3577 __isl_give isl_pw_aff
*isl_pw_aff_tdiv_q(__isl_take isl_pw_aff
*pa1
,
3578 __isl_take isl_pw_aff
*pa2
)
3584 is_cst
= isl_pw_aff_is_cst(pa2
);
3588 isl_die(isl_pw_aff_get_ctx(pa2
), isl_error_invalid
,
3589 "second argument should be a piecewise constant",
3592 pa1
= isl_pw_aff_div(pa1
, pa2
);
3594 cond
= isl_pw_aff_nonneg_set(isl_pw_aff_copy(pa1
));
3595 f
= isl_pw_aff_floor(isl_pw_aff_copy(pa1
));
3596 c
= isl_pw_aff_ceil(pa1
);
3597 return isl_pw_aff_cond(isl_set_indicator_function(cond
), f
, c
);
3599 isl_pw_aff_free(pa1
);
3600 isl_pw_aff_free(pa2
);
3604 /* Compute the remainder of the integer division of "pa1" by "pa2"
3605 * with rounding towards zero.
3606 * "pa2" is assumed to be a piecewise constant.
3608 * In particular, return
3610 * pa1 - pa2 * (pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2))
3613 __isl_give isl_pw_aff
*isl_pw_aff_tdiv_r(__isl_take isl_pw_aff
*pa1
,
3614 __isl_take isl_pw_aff
*pa2
)
3619 is_cst
= isl_pw_aff_is_cst(pa2
);
3623 isl_die(isl_pw_aff_get_ctx(pa2
), isl_error_invalid
,
3624 "second argument should be a piecewise constant",
3626 res
= isl_pw_aff_tdiv_q(isl_pw_aff_copy(pa1
), isl_pw_aff_copy(pa2
));
3627 res
= isl_pw_aff_mul(pa2
, res
);
3628 res
= isl_pw_aff_sub(pa1
, res
);
3631 isl_pw_aff_free(pa1
);
3632 isl_pw_aff_free(pa2
);
3636 /* Does either of "pa1" or "pa2" involve any NaN2?
3638 static isl_bool
either_involves_nan(__isl_keep isl_pw_aff
*pa1
,
3639 __isl_keep isl_pw_aff
*pa2
)
3643 has_nan
= isl_pw_aff_involves_nan(pa1
);
3644 if (has_nan
< 0 || has_nan
)
3646 return isl_pw_aff_involves_nan(pa2
);
3649 /* Replace "pa1" and "pa2" (at least one of which involves a NaN)
3650 * by a NaN on their shared domain.
3652 * In principle, the result could be refined to only being NaN
3653 * on the parts of this domain where at least one of "pa1" or "pa2" is NaN.
3655 static __isl_give isl_pw_aff
*replace_by_nan(__isl_take isl_pw_aff
*pa1
,
3656 __isl_take isl_pw_aff
*pa2
)
3658 isl_local_space
*ls
;
3662 dom
= isl_set_intersect(isl_pw_aff_domain(pa1
), isl_pw_aff_domain(pa2
));
3663 ls
= isl_local_space_from_space(isl_set_get_space(dom
));
3664 pa
= isl_pw_aff_nan_on_domain(ls
);
3665 pa
= isl_pw_aff_intersect_domain(pa
, dom
);
3670 static __isl_give isl_pw_aff
*pw_aff_min(__isl_take isl_pw_aff
*pwaff1
,
3671 __isl_take isl_pw_aff
*pwaff2
)
3676 dom
= isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1
)),
3677 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2
)));
3678 le
= isl_pw_aff_le_set(isl_pw_aff_copy(pwaff1
),
3679 isl_pw_aff_copy(pwaff2
));
3680 dom
= isl_set_subtract(dom
, isl_set_copy(le
));
3681 return isl_pw_aff_select(le
, pwaff1
, dom
, pwaff2
);
3684 static __isl_give isl_pw_aff
*pw_aff_max(__isl_take isl_pw_aff
*pwaff1
,
3685 __isl_take isl_pw_aff
*pwaff2
)
3690 dom
= isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1
)),
3691 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2
)));
3692 ge
= isl_pw_aff_ge_set(isl_pw_aff_copy(pwaff1
),
3693 isl_pw_aff_copy(pwaff2
));
3694 dom
= isl_set_subtract(dom
, isl_set_copy(ge
));
3695 return isl_pw_aff_select(ge
, pwaff1
, dom
, pwaff2
);
3698 /* Return an expression for the minimum (if "max" is not set) or
3699 * the maximum (if "max" is set) of "pa1" and "pa2".
3700 * If either expression involves any NaN, then return a NaN
3701 * on the shared domain as result.
3703 static __isl_give isl_pw_aff
*pw_aff_min_max(__isl_take isl_pw_aff
*pa1
,
3704 __isl_take isl_pw_aff
*pa2
, int max
)
3708 has_nan
= either_involves_nan(pa1
, pa2
);
3710 pa1
= isl_pw_aff_free(pa1
);
3712 return replace_by_nan(pa1
, pa2
);
3715 return isl_pw_aff_align_params_pw_pw_and(pa1
, pa2
, &pw_aff_max
);
3717 return isl_pw_aff_align_params_pw_pw_and(pa1
, pa2
, &pw_aff_min
);
3720 /* Return an expression for the minimum of "pwaff1" and "pwaff2".
3722 __isl_give isl_pw_aff
*isl_pw_aff_min(__isl_take isl_pw_aff
*pwaff1
,
3723 __isl_take isl_pw_aff
*pwaff2
)
3725 return pw_aff_min_max(pwaff1
, pwaff2
, 0);
3728 /* Return an expression for the maximum of "pwaff1" and "pwaff2".
3730 __isl_give isl_pw_aff
*isl_pw_aff_max(__isl_take isl_pw_aff
*pwaff1
,
3731 __isl_take isl_pw_aff
*pwaff2
)
3733 return pw_aff_min_max(pwaff1
, pwaff2
, 1);
3736 static __isl_give isl_pw_aff
*pw_aff_list_reduce(
3737 __isl_take isl_pw_aff_list
*list
,
3738 __isl_give isl_pw_aff
*(*fn
)(__isl_take isl_pw_aff
*pwaff1
,
3739 __isl_take isl_pw_aff
*pwaff2
))
3748 ctx
= isl_pw_aff_list_get_ctx(list
);
3750 isl_die(ctx
, isl_error_invalid
,
3751 "list should contain at least one element", goto error
);
3753 res
= isl_pw_aff_copy(list
->p
[0]);
3754 for (i
= 1; i
< list
->n
; ++i
)
3755 res
= fn(res
, isl_pw_aff_copy(list
->p
[i
]));
3757 isl_pw_aff_list_free(list
);
3760 isl_pw_aff_list_free(list
);
3764 /* Return an isl_pw_aff that maps each element in the intersection of the
3765 * domains of the elements of list to the minimal corresponding affine
3768 __isl_give isl_pw_aff
*isl_pw_aff_list_min(__isl_take isl_pw_aff_list
*list
)
3770 return pw_aff_list_reduce(list
, &isl_pw_aff_min
);
3773 /* Return an isl_pw_aff that maps each element in the intersection of the
3774 * domains of the elements of list to the maximal corresponding affine
3777 __isl_give isl_pw_aff
*isl_pw_aff_list_max(__isl_take isl_pw_aff_list
*list
)
3779 return pw_aff_list_reduce(list
, &isl_pw_aff_max
);
3782 /* Mark the domains of "pwaff" as rational.
3784 __isl_give isl_pw_aff
*isl_pw_aff_set_rational(__isl_take isl_pw_aff
*pwaff
)
3788 pwaff
= isl_pw_aff_cow(pwaff
);
3794 for (i
= 0; i
< pwaff
->n
; ++i
) {
3795 pwaff
->p
[i
].set
= isl_set_set_rational(pwaff
->p
[i
].set
);
3796 if (!pwaff
->p
[i
].set
)
3797 return isl_pw_aff_free(pwaff
);
3803 /* Mark the domains of the elements of "list" as rational.
3805 __isl_give isl_pw_aff_list
*isl_pw_aff_list_set_rational(
3806 __isl_take isl_pw_aff_list
*list
)
3816 for (i
= 0; i
< n
; ++i
) {
3819 pa
= isl_pw_aff_list_get_pw_aff(list
, i
);
3820 pa
= isl_pw_aff_set_rational(pa
);
3821 list
= isl_pw_aff_list_set_pw_aff(list
, i
, pa
);
3827 /* Do the parameters of "aff" match those of "space"?
3829 isl_bool
isl_aff_matching_params(__isl_keep isl_aff
*aff
,
3830 __isl_keep isl_space
*space
)
3832 isl_space
*aff_space
;
3836 return isl_bool_error
;
3838 aff_space
= isl_aff_get_domain_space(aff
);
3840 match
= isl_space_has_equal_params(space
, aff_space
);
3842 isl_space_free(aff_space
);
3846 /* Check that the domain space of "aff" matches "space".
3848 isl_stat
isl_aff_check_match_domain_space(__isl_keep isl_aff
*aff
,
3849 __isl_keep isl_space
*space
)
3851 isl_space
*aff_space
;
3855 return isl_stat_error
;
3857 aff_space
= isl_aff_get_domain_space(aff
);
3859 match
= isl_space_has_equal_params(space
, aff_space
);
3863 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
3864 "parameters don't match", goto error
);
3865 match
= isl_space_tuple_is_equal(space
, isl_dim_in
,
3866 aff_space
, isl_dim_set
);
3870 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
3871 "domains don't match", goto error
);
3872 isl_space_free(aff_space
);
3875 isl_space_free(aff_space
);
3876 return isl_stat_error
;
3885 #include <isl_multi_no_explicit_domain.c>
3886 #include <isl_multi_templ.c>
3887 #include <isl_multi_apply_set.c>
3888 #include <isl_multi_cmp.c>
3889 #include <isl_multi_dims.c>
3890 #include <isl_multi_floor.c>
3891 #include <isl_multi_gist.c>
3895 /* Construct an isl_multi_aff living in "space" that corresponds
3896 * to the affine transformation matrix "mat".
3898 __isl_give isl_multi_aff
*isl_multi_aff_from_aff_mat(
3899 __isl_take isl_space
*space
, __isl_take isl_mat
*mat
)
3902 isl_local_space
*ls
= NULL
;
3903 isl_multi_aff
*ma
= NULL
;
3904 int n_row
, n_col
, n_out
, total
;
3910 ctx
= isl_mat_get_ctx(mat
);
3912 n_row
= isl_mat_rows(mat
);
3913 n_col
= isl_mat_cols(mat
);
3915 isl_die(ctx
, isl_error_invalid
,
3916 "insufficient number of rows", goto error
);
3918 isl_die(ctx
, isl_error_invalid
,
3919 "insufficient number of columns", goto error
);
3920 n_out
= isl_space_dim(space
, isl_dim_out
);
3921 total
= isl_space_dim(space
, isl_dim_all
);
3922 if (1 + n_out
!= n_row
|| 2 + total
!= n_row
+ n_col
)
3923 isl_die(ctx
, isl_error_invalid
,
3924 "dimension mismatch", goto error
);
3926 ma
= isl_multi_aff_zero(isl_space_copy(space
));
3927 ls
= isl_local_space_from_space(isl_space_domain(space
));
3929 for (i
= 0; i
< n_row
- 1; ++i
) {
3933 v
= isl_vec_alloc(ctx
, 1 + n_col
);
3936 isl_int_set(v
->el
[0], mat
->row
[0][0]);
3937 isl_seq_cpy(v
->el
+ 1, mat
->row
[1 + i
], n_col
);
3938 v
= isl_vec_normalize(v
);
3939 aff
= isl_aff_alloc_vec(isl_local_space_copy(ls
), v
);
3940 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
3943 isl_local_space_free(ls
);
3947 isl_local_space_free(ls
);
3949 isl_multi_aff_free(ma
);
3953 /* Remove any internal structure of the domain of "ma".
3954 * If there is any such internal structure in the input,
3955 * then the name of the corresponding space is also removed.
3957 __isl_give isl_multi_aff
*isl_multi_aff_flatten_domain(
3958 __isl_take isl_multi_aff
*ma
)
3965 if (!ma
->space
->nested
[0])
3968 space
= isl_multi_aff_get_space(ma
);
3969 space
= isl_space_flatten_domain(space
);
3970 ma
= isl_multi_aff_reset_space(ma
, space
);
3975 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
3976 * of the space to its domain.
3978 __isl_give isl_multi_aff
*isl_multi_aff_domain_map(__isl_take isl_space
*space
)
3981 isl_local_space
*ls
;
3986 if (!isl_space_is_map(space
))
3987 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
3988 "not a map space", goto error
);
3990 n_in
= isl_space_dim(space
, isl_dim_in
);
3991 space
= isl_space_domain_map(space
);
3993 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
3995 isl_space_free(space
);
3999 space
= isl_space_domain(space
);
4000 ls
= isl_local_space_from_space(space
);
4001 for (i
= 0; i
< n_in
; ++i
) {
4004 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
4006 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
4008 isl_local_space_free(ls
);
4011 isl_space_free(space
);
4015 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
4016 * of the space to its range.
4018 __isl_give isl_multi_aff
*isl_multi_aff_range_map(__isl_take isl_space
*space
)
4021 isl_local_space
*ls
;
4026 if (!isl_space_is_map(space
))
4027 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
4028 "not a map space", goto error
);
4030 n_in
= isl_space_dim(space
, isl_dim_in
);
4031 n_out
= isl_space_dim(space
, isl_dim_out
);
4032 space
= isl_space_range_map(space
);
4034 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
4036 isl_space_free(space
);
4040 space
= isl_space_domain(space
);
4041 ls
= isl_local_space_from_space(space
);
4042 for (i
= 0; i
< n_out
; ++i
) {
4045 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
4046 isl_dim_set
, n_in
+ i
);
4047 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
4049 isl_local_space_free(ls
);
4052 isl_space_free(space
);
4056 /* Given a map space, return an isl_pw_multi_aff that maps a wrapped copy
4057 * of the space to its range.
4059 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_range_map(
4060 __isl_take isl_space
*space
)
4062 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_range_map(space
));
4065 /* Given the space of a set and a range of set dimensions,
4066 * construct an isl_multi_aff that projects out those dimensions.
4068 __isl_give isl_multi_aff
*isl_multi_aff_project_out_map(
4069 __isl_take isl_space
*space
, enum isl_dim_type type
,
4070 unsigned first
, unsigned n
)
4073 isl_local_space
*ls
;
4078 if (!isl_space_is_set(space
))
4079 isl_die(isl_space_get_ctx(space
), isl_error_unsupported
,
4080 "expecting set space", goto error
);
4081 if (type
!= isl_dim_set
)
4082 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
4083 "only set dimensions can be projected out", goto error
);
4085 dim
= isl_space_dim(space
, isl_dim_set
);
4086 if (first
+ n
> dim
)
4087 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
4088 "range out of bounds", goto error
);
4090 space
= isl_space_from_domain(space
);
4091 space
= isl_space_add_dims(space
, isl_dim_out
, dim
- n
);
4094 return isl_multi_aff_alloc(space
);
4096 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
4097 space
= isl_space_domain(space
);
4098 ls
= isl_local_space_from_space(space
);
4100 for (i
= 0; i
< first
; ++i
) {
4103 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
4105 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
4108 for (i
= 0; i
< dim
- (first
+ n
); ++i
) {
4111 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
4112 isl_dim_set
, first
+ n
+ i
);
4113 ma
= isl_multi_aff_set_aff(ma
, first
+ i
, aff
);
4116 isl_local_space_free(ls
);
4119 isl_space_free(space
);
4123 /* Given the space of a set and a range of set dimensions,
4124 * construct an isl_pw_multi_aff that projects out those dimensions.
4126 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_project_out_map(
4127 __isl_take isl_space
*space
, enum isl_dim_type type
,
4128 unsigned first
, unsigned n
)
4132 ma
= isl_multi_aff_project_out_map(space
, type
, first
, n
);
4133 return isl_pw_multi_aff_from_multi_aff(ma
);
4136 /* Create an isl_pw_multi_aff with the given isl_multi_aff on a universe
4139 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_multi_aff(
4140 __isl_take isl_multi_aff
*ma
)
4142 isl_set
*dom
= isl_set_universe(isl_multi_aff_get_domain_space(ma
));
4143 return isl_pw_multi_aff_alloc(dom
, ma
);
4146 /* Create a piecewise multi-affine expression in the given space that maps each
4147 * input dimension to the corresponding output dimension.
4149 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_identity(
4150 __isl_take isl_space
*space
)
4152 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_identity(space
));
4155 /* Exploit the equalities in "eq" to simplify the affine expressions.
4157 static __isl_give isl_multi_aff
*isl_multi_aff_substitute_equalities(
4158 __isl_take isl_multi_aff
*maff
, __isl_take isl_basic_set
*eq
)
4162 maff
= isl_multi_aff_cow(maff
);
4166 for (i
= 0; i
< maff
->n
; ++i
) {
4167 maff
->u
.p
[i
] = isl_aff_substitute_equalities(maff
->u
.p
[i
],
4168 isl_basic_set_copy(eq
));
4173 isl_basic_set_free(eq
);
4176 isl_basic_set_free(eq
);
4177 isl_multi_aff_free(maff
);
4181 __isl_give isl_multi_aff
*isl_multi_aff_scale(__isl_take isl_multi_aff
*maff
,
4186 maff
= isl_multi_aff_cow(maff
);
4190 for (i
= 0; i
< maff
->n
; ++i
) {
4191 maff
->u
.p
[i
] = isl_aff_scale(maff
->u
.p
[i
], f
);
4193 return isl_multi_aff_free(maff
);
4199 __isl_give isl_multi_aff
*isl_multi_aff_add_on_domain(__isl_keep isl_set
*dom
,
4200 __isl_take isl_multi_aff
*maff1
, __isl_take isl_multi_aff
*maff2
)
4202 maff1
= isl_multi_aff_add(maff1
, maff2
);
4203 maff1
= isl_multi_aff_gist(maff1
, isl_set_copy(dom
));
4207 int isl_multi_aff_is_empty(__isl_keep isl_multi_aff
*maff
)
4215 /* Return the set of domain elements where "ma1" is lexicographically
4216 * smaller than or equal to "ma2".
4218 __isl_give isl_set
*isl_multi_aff_lex_le_set(__isl_take isl_multi_aff
*ma1
,
4219 __isl_take isl_multi_aff
*ma2
)
4221 return isl_multi_aff_lex_ge_set(ma2
, ma1
);
4224 /* Return the set of domain elements where "ma1" is lexicographically
4225 * smaller than "ma2".
4227 __isl_give isl_set
*isl_multi_aff_lex_lt_set(__isl_take isl_multi_aff
*ma1
,
4228 __isl_take isl_multi_aff
*ma2
)
4230 return isl_multi_aff_lex_gt_set(ma2
, ma1
);
4233 /* Return the set of domain elements where "ma1" and "ma2"
4236 static __isl_give isl_set
*isl_multi_aff_order_set(
4237 __isl_take isl_multi_aff
*ma1
, __isl_take isl_multi_aff
*ma2
,
4238 __isl_give isl_map
*order(__isl_take isl_space
*set_space
))
4241 isl_map
*map1
, *map2
;
4244 map1
= isl_map_from_multi_aff(ma1
);
4245 map2
= isl_map_from_multi_aff(ma2
);
4246 map
= isl_map_range_product(map1
, map2
);
4247 space
= isl_space_range(isl_map_get_space(map
));
4248 space
= isl_space_domain(isl_space_unwrap(space
));
4250 map
= isl_map_intersect_range(map
, isl_map_wrap(ge
));
4252 return isl_map_domain(map
);
4255 /* Return the set of domain elements where "ma1" is lexicographically
4256 * greater than or equal to "ma2".
4258 __isl_give isl_set
*isl_multi_aff_lex_ge_set(__isl_take isl_multi_aff
*ma1
,
4259 __isl_take isl_multi_aff
*ma2
)
4261 return isl_multi_aff_order_set(ma1
, ma2
, &isl_map_lex_ge
);
4264 /* Return the set of domain elements where "ma1" is lexicographically
4265 * greater than "ma2".
4267 __isl_give isl_set
*isl_multi_aff_lex_gt_set(__isl_take isl_multi_aff
*ma1
,
4268 __isl_take isl_multi_aff
*ma2
)
4270 return isl_multi_aff_order_set(ma1
, ma2
, &isl_map_lex_gt
);
4274 #define PW isl_pw_multi_aff
4276 #define EL isl_multi_aff
4278 #define EL_IS_ZERO is_empty
4282 #define IS_ZERO is_empty
4285 #undef DEFAULT_IS_ZERO
4286 #define DEFAULT_IS_ZERO 0
4290 #define NO_INSERT_DIMS
4294 #include <isl_pw_templ.c>
4295 #include <isl_pw_union_opt.c>
4300 #define UNION isl_union_pw_multi_aff
4302 #define PART isl_pw_multi_aff
4304 #define PARTS pw_multi_aff
4306 #include <isl_union_multi.c>
4307 #include <isl_union_neg.c>
4309 static __isl_give isl_pw_multi_aff
*pw_multi_aff_union_lexmax(
4310 __isl_take isl_pw_multi_aff
*pma1
,
4311 __isl_take isl_pw_multi_aff
*pma2
)
4313 return isl_pw_multi_aff_union_opt_cmp(pma1
, pma2
,
4314 &isl_multi_aff_lex_ge_set
);
4317 /* Given two piecewise multi affine expressions, return a piecewise
4318 * multi-affine expression defined on the union of the definition domains
4319 * of the inputs that is equal to the lexicographic maximum of the two
4320 * inputs on each cell. If only one of the two inputs is defined on
4321 * a given cell, then it is considered to be the maximum.
4323 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_union_lexmax(
4324 __isl_take isl_pw_multi_aff
*pma1
,
4325 __isl_take isl_pw_multi_aff
*pma2
)
4327 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
4328 &pw_multi_aff_union_lexmax
);
4331 static __isl_give isl_pw_multi_aff
*pw_multi_aff_union_lexmin(
4332 __isl_take isl_pw_multi_aff
*pma1
,
4333 __isl_take isl_pw_multi_aff
*pma2
)
4335 return isl_pw_multi_aff_union_opt_cmp(pma1
, pma2
,
4336 &isl_multi_aff_lex_le_set
);
4339 /* Given two piecewise multi affine expressions, return a piecewise
4340 * multi-affine expression defined on the union of the definition domains
4341 * of the inputs that is equal to the lexicographic minimum of the two
4342 * inputs on each cell. If only one of the two inputs is defined on
4343 * a given cell, then it is considered to be the minimum.
4345 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_union_lexmin(
4346 __isl_take isl_pw_multi_aff
*pma1
,
4347 __isl_take isl_pw_multi_aff
*pma2
)
4349 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
4350 &pw_multi_aff_union_lexmin
);
4353 static __isl_give isl_pw_multi_aff
*pw_multi_aff_add(
4354 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4356 return isl_pw_multi_aff_on_shared_domain(pma1
, pma2
,
4357 &isl_multi_aff_add
);
4360 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_add(
4361 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4363 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
4367 static __isl_give isl_pw_multi_aff
*pw_multi_aff_sub(
4368 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4370 return isl_pw_multi_aff_on_shared_domain(pma1
, pma2
,
4371 &isl_multi_aff_sub
);
4374 /* Subtract "pma2" from "pma1" and return the result.
4376 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_sub(
4377 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4379 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
4383 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_union_add(
4384 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4386 return isl_pw_multi_aff_union_add_(pma1
, pma2
);
4389 /* Compute the sum of "upa1" and "upa2" on the union of their domains,
4390 * with the actual sum on the shared domain and
4391 * the defined expression on the symmetric difference of the domains.
4393 __isl_give isl_union_pw_aff
*isl_union_pw_aff_union_add(
4394 __isl_take isl_union_pw_aff
*upa1
, __isl_take isl_union_pw_aff
*upa2
)
4396 return isl_union_pw_aff_union_add_(upa1
, upa2
);
4399 /* Compute the sum of "upma1" and "upma2" on the union of their domains,
4400 * with the actual sum on the shared domain and
4401 * the defined expression on the symmetric difference of the domains.
4403 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_union_add(
4404 __isl_take isl_union_pw_multi_aff
*upma1
,
4405 __isl_take isl_union_pw_multi_aff
*upma2
)
4407 return isl_union_pw_multi_aff_union_add_(upma1
, upma2
);
4410 /* Given two piecewise multi-affine expressions A -> B and C -> D,
4411 * construct a piecewise multi-affine expression [A -> C] -> [B -> D].
4413 static __isl_give isl_pw_multi_aff
*pw_multi_aff_product(
4414 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4418 isl_pw_multi_aff
*res
;
4423 n
= pma1
->n
* pma2
->n
;
4424 space
= isl_space_product(isl_space_copy(pma1
->dim
),
4425 isl_space_copy(pma2
->dim
));
4426 res
= isl_pw_multi_aff_alloc_size(space
, n
);
4428 for (i
= 0; i
< pma1
->n
; ++i
) {
4429 for (j
= 0; j
< pma2
->n
; ++j
) {
4433 domain
= isl_set_product(isl_set_copy(pma1
->p
[i
].set
),
4434 isl_set_copy(pma2
->p
[j
].set
));
4435 ma
= isl_multi_aff_product(
4436 isl_multi_aff_copy(pma1
->p
[i
].maff
),
4437 isl_multi_aff_copy(pma2
->p
[j
].maff
));
4438 res
= isl_pw_multi_aff_add_piece(res
, domain
, ma
);
4442 isl_pw_multi_aff_free(pma1
);
4443 isl_pw_multi_aff_free(pma2
);
4446 isl_pw_multi_aff_free(pma1
);
4447 isl_pw_multi_aff_free(pma2
);
4451 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_product(
4452 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4454 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
4455 &pw_multi_aff_product
);
4458 /* Construct a map mapping the domain of the piecewise multi-affine expression
4459 * to its range, with each dimension in the range equated to the
4460 * corresponding affine expression on its cell.
4462 * If the domain of "pma" is rational, then so is the constructed "map".
4464 __isl_give isl_map
*isl_map_from_pw_multi_aff(__isl_take isl_pw_multi_aff
*pma
)
4472 map
= isl_map_empty(isl_pw_multi_aff_get_space(pma
));
4474 for (i
= 0; i
< pma
->n
; ++i
) {
4476 isl_multi_aff
*maff
;
4477 isl_basic_map
*bmap
;
4480 rational
= isl_set_is_rational(pma
->p
[i
].set
);
4482 map
= isl_map_free(map
);
4483 maff
= isl_multi_aff_copy(pma
->p
[i
].maff
);
4484 bmap
= isl_basic_map_from_multi_aff2(maff
, rational
);
4485 map_i
= isl_map_from_basic_map(bmap
);
4486 map_i
= isl_map_intersect_domain(map_i
,
4487 isl_set_copy(pma
->p
[i
].set
));
4488 map
= isl_map_union_disjoint(map
, map_i
);
4491 isl_pw_multi_aff_free(pma
);
4495 __isl_give isl_set
*isl_set_from_pw_multi_aff(__isl_take isl_pw_multi_aff
*pma
)
4500 if (!isl_space_is_set(pma
->dim
))
4501 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
4502 "isl_pw_multi_aff cannot be converted into an isl_set",
4505 return isl_map_from_pw_multi_aff(pma
);
4507 isl_pw_multi_aff_free(pma
);
4511 /* Subtract the initial "n" elements in "ma" with coefficients in "c" and
4512 * denominator "denom".
4513 * "denom" is allowed to be negative, in which case the actual denominator
4514 * is -denom and the expressions are added instead.
4516 static __isl_give isl_aff
*subtract_initial(__isl_take isl_aff
*aff
,
4517 __isl_keep isl_multi_aff
*ma
, int n
, isl_int
*c
, isl_int denom
)
4523 first
= isl_seq_first_non_zero(c
, n
);
4527 sign
= isl_int_sgn(denom
);
4529 isl_int_abs(d
, denom
);
4530 for (i
= first
; i
< n
; ++i
) {
4533 if (isl_int_is_zero(c
[i
]))
4535 aff_i
= isl_multi_aff_get_aff(ma
, i
);
4536 aff_i
= isl_aff_scale(aff_i
, c
[i
]);
4537 aff_i
= isl_aff_scale_down(aff_i
, d
);
4539 aff
= isl_aff_sub(aff
, aff_i
);
4541 aff
= isl_aff_add(aff
, aff_i
);
4548 /* Extract an affine expression that expresses the output dimension "pos"
4549 * of "bmap" in terms of the parameters and input dimensions from
4551 * Note that this expression may involve integer divisions defined
4552 * in terms of parameters and input dimensions.
4553 * The equality may also involve references to earlier (but not later)
4554 * output dimensions. These are replaced by the corresponding elements
4557 * If the equality is of the form
4559 * f(i) + h(j) + a x + g(i) = 0,
4561 * with f(i) a linear combinations of the parameters and input dimensions,
4562 * g(i) a linear combination of integer divisions defined in terms of the same
4563 * and h(j) a linear combinations of earlier output dimensions,
4564 * then the affine expression is
4566 * (-f(i) - g(i))/a - h(j)/a
4568 * If the equality is of the form
4570 * f(i) + h(j) - a x + g(i) = 0,
4572 * then the affine expression is
4574 * (f(i) + g(i))/a - h(j)/(-a)
4577 * If "div" refers to an integer division (i.e., it is smaller than
4578 * the number of integer divisions), then the equality constraint
4579 * does involve an integer division (the one at position "div") that
4580 * is defined in terms of output dimensions. However, this integer
4581 * division can be eliminated by exploiting a pair of constraints
4582 * x >= l and x <= l + n, with n smaller than the coefficient of "div"
4583 * in the equality constraint. "ineq" refers to inequality x >= l, i.e.,
4585 * In particular, let
4587 * x = e(i) + m floor(...)
4589 * with e(i) the expression derived above and floor(...) the integer
4590 * division involving output dimensions.
4601 * e(i) + m floor(...) - l = (e(i) + m floor(...) - l) mod m
4602 * = (e(i) - l) mod m
4606 * x - l = (e(i) - l) mod m
4610 * x = ((e(i) - l) mod m) + l
4612 * The variable "shift" below contains the expression -l, which may
4613 * also involve a linear combination of earlier output dimensions.
4615 static __isl_give isl_aff
*extract_aff_from_equality(
4616 __isl_keep isl_basic_map
*bmap
, int pos
, int eq
, int div
, int ineq
,
4617 __isl_keep isl_multi_aff
*ma
)
4620 unsigned n_div
, n_out
;
4622 isl_local_space
*ls
;
4623 isl_aff
*aff
, *shift
;
4626 ctx
= isl_basic_map_get_ctx(bmap
);
4627 ls
= isl_basic_map_get_local_space(bmap
);
4628 ls
= isl_local_space_domain(ls
);
4629 aff
= isl_aff_alloc(isl_local_space_copy(ls
));
4632 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
4633 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4634 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4635 if (isl_int_is_neg(bmap
->eq
[eq
][o_out
+ pos
])) {
4636 isl_seq_cpy(aff
->v
->el
+ 1, bmap
->eq
[eq
], o_out
);
4637 isl_seq_cpy(aff
->v
->el
+ 1 + o_out
,
4638 bmap
->eq
[eq
] + o_out
+ n_out
, n_div
);
4640 isl_seq_neg(aff
->v
->el
+ 1, bmap
->eq
[eq
], o_out
);
4641 isl_seq_neg(aff
->v
->el
+ 1 + o_out
,
4642 bmap
->eq
[eq
] + o_out
+ n_out
, n_div
);
4645 isl_int_set_si(aff
->v
->el
[1 + o_out
+ div
], 0);
4646 isl_int_abs(aff
->v
->el
[0], bmap
->eq
[eq
][o_out
+ pos
]);
4647 aff
= subtract_initial(aff
, ma
, pos
, bmap
->eq
[eq
] + o_out
,
4648 bmap
->eq
[eq
][o_out
+ pos
]);
4650 shift
= isl_aff_alloc(isl_local_space_copy(ls
));
4653 isl_seq_cpy(shift
->v
->el
+ 1, bmap
->ineq
[ineq
], o_out
);
4654 isl_seq_cpy(shift
->v
->el
+ 1 + o_out
,
4655 bmap
->ineq
[ineq
] + o_out
+ n_out
, n_div
);
4656 isl_int_set_si(shift
->v
->el
[0], 1);
4657 shift
= subtract_initial(shift
, ma
, pos
,
4658 bmap
->ineq
[ineq
] + o_out
, ctx
->negone
);
4659 aff
= isl_aff_add(aff
, isl_aff_copy(shift
));
4660 mod
= isl_val_int_from_isl_int(ctx
,
4661 bmap
->eq
[eq
][o_out
+ n_out
+ div
]);
4662 mod
= isl_val_abs(mod
);
4663 aff
= isl_aff_mod_val(aff
, mod
);
4664 aff
= isl_aff_sub(aff
, shift
);
4667 isl_local_space_free(ls
);
4670 isl_local_space_free(ls
);
4675 /* Given a basic map with output dimensions defined
4676 * in terms of the parameters input dimensions and earlier
4677 * output dimensions using an equality (and possibly a pair on inequalities),
4678 * extract an isl_aff that expresses output dimension "pos" in terms
4679 * of the parameters and input dimensions.
4680 * Note that this expression may involve integer divisions defined
4681 * in terms of parameters and input dimensions.
4682 * "ma" contains the expressions corresponding to earlier output dimensions.
4684 * This function shares some similarities with
4685 * isl_basic_map_has_defining_equality and isl_constraint_get_bound.
4687 static __isl_give isl_aff
*extract_isl_aff_from_basic_map(
4688 __isl_keep isl_basic_map
*bmap
, int pos
, __isl_keep isl_multi_aff
*ma
)
4695 eq
= isl_basic_map_output_defining_equality(bmap
, pos
, &div
, &ineq
);
4696 if (eq
>= bmap
->n_eq
)
4697 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
4698 "unable to find suitable equality", return NULL
);
4699 aff
= extract_aff_from_equality(bmap
, pos
, eq
, div
, ineq
, ma
);
4701 aff
= isl_aff_remove_unused_divs(aff
);
4705 /* Given a basic map where each output dimension is defined
4706 * in terms of the parameters and input dimensions using an equality,
4707 * extract an isl_multi_aff that expresses the output dimensions in terms
4708 * of the parameters and input dimensions.
4710 static __isl_give isl_multi_aff
*extract_isl_multi_aff_from_basic_map(
4711 __isl_take isl_basic_map
*bmap
)
4720 ma
= isl_multi_aff_alloc(isl_basic_map_get_space(bmap
));
4721 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4723 for (i
= 0; i
< n_out
; ++i
) {
4726 aff
= extract_isl_aff_from_basic_map(bmap
, i
, ma
);
4727 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
4730 isl_basic_map_free(bmap
);
4735 /* Given a basic set where each set dimension is defined
4736 * in terms of the parameters using an equality,
4737 * extract an isl_multi_aff that expresses the set dimensions in terms
4738 * of the parameters.
4740 __isl_give isl_multi_aff
*isl_multi_aff_from_basic_set_equalities(
4741 __isl_take isl_basic_set
*bset
)
4743 return extract_isl_multi_aff_from_basic_map(bset
);
4746 /* Create an isl_pw_multi_aff that is equivalent to
4747 * isl_map_intersect_domain(isl_map_from_basic_map(bmap), domain).
4748 * The given basic map is such that each output dimension is defined
4749 * in terms of the parameters and input dimensions using an equality.
4751 * Since some applications expect the result of isl_pw_multi_aff_from_map
4752 * to only contain integer affine expressions, we compute the floor
4753 * of the expression before returning.
4755 * Remove all constraints involving local variables without
4756 * an explicit representation (resulting in the removal of those
4757 * local variables) prior to the actual extraction to ensure
4758 * that the local spaces in which the resulting affine expressions
4759 * are created do not contain any unknown local variables.
4760 * Removing such constraints is safe because constraints involving
4761 * unknown local variables are not used to determine whether
4762 * a basic map is obviously single-valued.
4764 static __isl_give isl_pw_multi_aff
*plain_pw_multi_aff_from_map(
4765 __isl_take isl_set
*domain
, __isl_take isl_basic_map
*bmap
)
4769 bmap
= isl_basic_map_drop_constraint_involving_unknown_divs(bmap
);
4770 ma
= extract_isl_multi_aff_from_basic_map(bmap
);
4771 ma
= isl_multi_aff_floor(ma
);
4772 return isl_pw_multi_aff_alloc(domain
, ma
);
4775 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4776 * This obviously only works if the input "map" is single-valued.
4777 * If so, we compute the lexicographic minimum of the image in the form
4778 * of an isl_pw_multi_aff. Since the image is unique, it is equal
4779 * to its lexicographic minimum.
4780 * If the input is not single-valued, we produce an error.
4782 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_base(
4783 __isl_take isl_map
*map
)
4787 isl_pw_multi_aff
*pma
;
4789 sv
= isl_map_is_single_valued(map
);
4793 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
4794 "map is not single-valued", goto error
);
4795 map
= isl_map_make_disjoint(map
);
4799 pma
= isl_pw_multi_aff_empty(isl_map_get_space(map
));
4801 for (i
= 0; i
< map
->n
; ++i
) {
4802 isl_pw_multi_aff
*pma_i
;
4803 isl_basic_map
*bmap
;
4804 bmap
= isl_basic_map_copy(map
->p
[i
]);
4805 pma_i
= isl_basic_map_lexmin_pw_multi_aff(bmap
);
4806 pma
= isl_pw_multi_aff_add_disjoint(pma
, pma_i
);
4816 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
4817 * taking into account that the output dimension at position "d"
4818 * can be represented as
4820 * x = floor((e(...) + c1) / m)
4822 * given that constraint "i" is of the form
4824 * e(...) + c1 - m x >= 0
4827 * Let "map" be of the form
4831 * We construct a mapping
4833 * A -> [A -> x = floor(...)]
4835 * apply that to the map, obtaining
4837 * [A -> x = floor(...)] -> B
4839 * and equate dimension "d" to x.
4840 * We then compute a isl_pw_multi_aff representation of the resulting map
4841 * and plug in the mapping above.
4843 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_div(
4844 __isl_take isl_map
*map
, __isl_take isl_basic_map
*hull
, int d
, int i
)
4848 isl_local_space
*ls
;
4856 isl_pw_multi_aff
*pma
;
4859 is_set
= isl_map_is_set(map
);
4863 offset
= isl_basic_map_offset(hull
, isl_dim_out
);
4864 ctx
= isl_map_get_ctx(map
);
4865 space
= isl_space_domain(isl_map_get_space(map
));
4866 n_in
= isl_space_dim(space
, isl_dim_set
);
4867 n
= isl_space_dim(space
, isl_dim_all
);
4869 v
= isl_vec_alloc(ctx
, 1 + 1 + n
);
4871 isl_int_neg(v
->el
[0], hull
->ineq
[i
][offset
+ d
]);
4872 isl_seq_cpy(v
->el
+ 1, hull
->ineq
[i
], 1 + n
);
4874 isl_basic_map_free(hull
);
4876 ls
= isl_local_space_from_space(isl_space_copy(space
));
4877 aff
= isl_aff_alloc_vec(ls
, v
);
4878 aff
= isl_aff_floor(aff
);
4880 isl_space_free(space
);
4881 ma
= isl_multi_aff_from_aff(aff
);
4883 ma
= isl_multi_aff_identity(isl_space_map_from_set(space
));
4884 ma
= isl_multi_aff_range_product(ma
,
4885 isl_multi_aff_from_aff(aff
));
4888 insert
= isl_map_from_multi_aff(isl_multi_aff_copy(ma
));
4889 map
= isl_map_apply_domain(map
, insert
);
4890 map
= isl_map_equate(map
, isl_dim_in
, n_in
, isl_dim_out
, d
);
4891 pma
= isl_pw_multi_aff_from_map(map
);
4892 pma
= isl_pw_multi_aff_pullback_multi_aff(pma
, ma
);
4897 isl_basic_map_free(hull
);
4901 /* Is constraint "c" of the form
4903 * e(...) + c1 - m x >= 0
4907 * -e(...) + c2 + m x >= 0
4909 * where m > 1 and e only depends on parameters and input dimemnsions?
4911 * "offset" is the offset of the output dimensions
4912 * "pos" is the position of output dimension x.
4914 static int is_potential_div_constraint(isl_int
*c
, int offset
, int d
, int total
)
4916 if (isl_int_is_zero(c
[offset
+ d
]))
4918 if (isl_int_is_one(c
[offset
+ d
]))
4920 if (isl_int_is_negone(c
[offset
+ d
]))
4922 if (isl_seq_first_non_zero(c
+ offset
, d
) != -1)
4924 if (isl_seq_first_non_zero(c
+ offset
+ d
+ 1,
4925 total
- (offset
+ d
+ 1)) != -1)
4930 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4932 * As a special case, we first check if there is any pair of constraints,
4933 * shared by all the basic maps in "map" that force a given dimension
4934 * to be equal to the floor of some affine combination of the input dimensions.
4936 * In particular, if we can find two constraints
4938 * e(...) + c1 - m x >= 0 i.e., m x <= e(...) + c1
4942 * -e(...) + c2 + m x >= 0 i.e., m x >= e(...) - c2
4944 * where m > 1 and e only depends on parameters and input dimemnsions,
4947 * c1 + c2 < m i.e., -c2 >= c1 - (m - 1)
4949 * then we know that we can take
4951 * x = floor((e(...) + c1) / m)
4953 * without having to perform any computation.
4955 * Note that we know that
4959 * If c1 + c2 were 0, then we would have detected an equality during
4960 * simplification. If c1 + c2 were negative, then we would have detected
4963 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_check_div(
4964 __isl_take isl_map
*map
)
4970 isl_basic_map
*hull
;
4972 hull
= isl_map_unshifted_simple_hull(isl_map_copy(map
));
4977 dim
= isl_map_dim(map
, isl_dim_out
);
4978 offset
= isl_basic_map_offset(hull
, isl_dim_out
);
4979 total
= 1 + isl_basic_map_total_dim(hull
);
4981 for (d
= 0; d
< dim
; ++d
) {
4982 for (i
= 0; i
< n
; ++i
) {
4983 if (!is_potential_div_constraint(hull
->ineq
[i
],
4986 for (j
= i
+ 1; j
< n
; ++j
) {
4987 if (!isl_seq_is_neg(hull
->ineq
[i
] + 1,
4988 hull
->ineq
[j
] + 1, total
- 1))
4990 isl_int_add(sum
, hull
->ineq
[i
][0],
4992 if (isl_int_abs_lt(sum
,
4993 hull
->ineq
[i
][offset
+ d
]))
5000 if (isl_int_is_pos(hull
->ineq
[j
][offset
+ d
]))
5002 return pw_multi_aff_from_map_div(map
, hull
, d
, j
);
5006 isl_basic_map_free(hull
);
5007 return pw_multi_aff_from_map_base(map
);
5010 isl_basic_map_free(hull
);
5014 /* Given an affine expression
5016 * [A -> B] -> f(A,B)
5018 * construct an isl_multi_aff
5022 * such that dimension "d" in B' is set to "aff" and the remaining
5023 * dimensions are set equal to the corresponding dimensions in B.
5024 * "n_in" is the dimension of the space A.
5025 * "n_out" is the dimension of the space B.
5027 * If "is_set" is set, then the affine expression is of the form
5031 * and we construct an isl_multi_aff
5035 static __isl_give isl_multi_aff
*range_map(__isl_take isl_aff
*aff
, int d
,
5036 unsigned n_in
, unsigned n_out
, int is_set
)
5040 isl_space
*space
, *space2
;
5041 isl_local_space
*ls
;
5043 space
= isl_aff_get_domain_space(aff
);
5044 ls
= isl_local_space_from_space(isl_space_copy(space
));
5045 space2
= isl_space_copy(space
);
5047 space2
= isl_space_range(isl_space_unwrap(space2
));
5048 space
= isl_space_map_from_domain_and_range(space
, space2
);
5049 ma
= isl_multi_aff_alloc(space
);
5050 ma
= isl_multi_aff_set_aff(ma
, d
, aff
);
5052 for (i
= 0; i
< n_out
; ++i
) {
5055 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
5056 isl_dim_set
, n_in
+ i
);
5057 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
5060 isl_local_space_free(ls
);
5065 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
5066 * taking into account that the dimension at position "d" can be written as
5068 * x = m a + f(..) (1)
5070 * where m is equal to "gcd".
5071 * "i" is the index of the equality in "hull" that defines f(..).
5072 * In particular, the equality is of the form
5074 * f(..) - x + m g(existentials) = 0
5078 * -f(..) + x + m g(existentials) = 0
5080 * We basically plug (1) into "map", resulting in a map with "a"
5081 * in the range instead of "x". The corresponding isl_pw_multi_aff
5082 * defining "a" is then plugged back into (1) to obtain a definition for "x".
5084 * Specifically, given the input map
5088 * We first wrap it into a set
5092 * and define (1) on top of the corresponding space, resulting in "aff".
5093 * We use this to create an isl_multi_aff that maps the output position "d"
5094 * from "a" to "x", leaving all other (intput and output) dimensions unchanged.
5095 * We plug this into the wrapped map, unwrap the result and compute the
5096 * corresponding isl_pw_multi_aff.
5097 * The result is an expression
5105 * so that we can plug that into "aff", after extending the latter to
5111 * If "map" is actually a set, then there is no "A" space, meaning
5112 * that we do not need to perform any wrapping, and that the result
5113 * of the recursive call is of the form
5117 * which is plugged into a mapping of the form
5121 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_stride(
5122 __isl_take isl_map
*map
, __isl_take isl_basic_map
*hull
, int d
, int i
,
5127 isl_local_space
*ls
;
5130 isl_pw_multi_aff
*pma
, *id
;
5136 is_set
= isl_map_is_set(map
);
5140 n_in
= isl_basic_map_dim(hull
, isl_dim_in
);
5141 n_out
= isl_basic_map_dim(hull
, isl_dim_out
);
5142 o_out
= isl_basic_map_offset(hull
, isl_dim_out
);
5147 set
= isl_map_wrap(map
);
5148 space
= isl_space_map_from_set(isl_set_get_space(set
));
5149 ma
= isl_multi_aff_identity(space
);
5150 ls
= isl_local_space_from_space(isl_set_get_space(set
));
5151 aff
= isl_aff_alloc(ls
);
5153 isl_int_set_si(aff
->v
->el
[0], 1);
5154 if (isl_int_is_one(hull
->eq
[i
][o_out
+ d
]))
5155 isl_seq_neg(aff
->v
->el
+ 1, hull
->eq
[i
],
5158 isl_seq_cpy(aff
->v
->el
+ 1, hull
->eq
[i
],
5160 isl_int_set(aff
->v
->el
[1 + o_out
+ d
], gcd
);
5162 ma
= isl_multi_aff_set_aff(ma
, n_in
+ d
, isl_aff_copy(aff
));
5163 set
= isl_set_preimage_multi_aff(set
, ma
);
5165 ma
= range_map(aff
, d
, n_in
, n_out
, is_set
);
5170 map
= isl_set_unwrap(set
);
5171 pma
= isl_pw_multi_aff_from_map(map
);
5174 space
= isl_pw_multi_aff_get_domain_space(pma
);
5175 space
= isl_space_map_from_set(space
);
5176 id
= isl_pw_multi_aff_identity(space
);
5177 pma
= isl_pw_multi_aff_range_product(id
, pma
);
5179 id
= isl_pw_multi_aff_from_multi_aff(ma
);
5180 pma
= isl_pw_multi_aff_pullback_pw_multi_aff(id
, pma
);
5182 isl_basic_map_free(hull
);
5186 isl_basic_map_free(hull
);
5190 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5191 * "hull" contains the equalities valid for "map".
5193 * Check if any of the output dimensions is "strided".
5194 * That is, we check if it can be written as
5198 * with m greater than 1, a some combination of existentially quantified
5199 * variables and f an expression in the parameters and input dimensions.
5200 * If so, we remove the stride in pw_multi_aff_from_map_stride.
5202 * Otherwise, we continue with pw_multi_aff_from_map_check_div for a further
5205 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_check_strides(
5206 __isl_take isl_map
*map
, __isl_take isl_basic_map
*hull
)
5215 n_div
= isl_basic_map_dim(hull
, isl_dim_div
);
5216 o_div
= isl_basic_map_offset(hull
, isl_dim_div
);
5219 isl_basic_map_free(hull
);
5220 return pw_multi_aff_from_map_check_div(map
);
5225 n_out
= isl_basic_map_dim(hull
, isl_dim_out
);
5226 o_out
= isl_basic_map_offset(hull
, isl_dim_out
);
5228 for (i
= 0; i
< n_out
; ++i
) {
5229 for (j
= 0; j
< hull
->n_eq
; ++j
) {
5230 isl_int
*eq
= hull
->eq
[j
];
5231 isl_pw_multi_aff
*res
;
5233 if (!isl_int_is_one(eq
[o_out
+ i
]) &&
5234 !isl_int_is_negone(eq
[o_out
+ i
]))
5236 if (isl_seq_first_non_zero(eq
+ o_out
, i
) != -1)
5238 if (isl_seq_first_non_zero(eq
+ o_out
+ i
+ 1,
5239 n_out
- (i
+ 1)) != -1)
5241 isl_seq_gcd(eq
+ o_div
, n_div
, &gcd
);
5242 if (isl_int_is_zero(gcd
))
5244 if (isl_int_is_one(gcd
))
5247 res
= pw_multi_aff_from_map_stride(map
, hull
,
5255 isl_basic_map_free(hull
);
5256 return pw_multi_aff_from_map_check_div(map
);
5259 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5261 * As a special case, we first check if all output dimensions are uniquely
5262 * defined in terms of the parameters and input dimensions over the entire
5263 * domain. If so, we extract the desired isl_pw_multi_aff directly
5264 * from the affine hull of "map" and its domain.
5266 * Otherwise, continue with pw_multi_aff_from_map_check_strides for more
5269 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_map(__isl_take isl_map
*map
)
5272 isl_basic_map
*hull
;
5277 if (isl_map_n_basic_map(map
) == 1) {
5278 hull
= isl_map_unshifted_simple_hull(isl_map_copy(map
));
5279 hull
= isl_basic_map_plain_affine_hull(hull
);
5280 sv
= isl_basic_map_plain_is_single_valued(hull
);
5282 return plain_pw_multi_aff_from_map(isl_map_domain(map
),
5284 isl_basic_map_free(hull
);
5286 map
= isl_map_detect_equalities(map
);
5287 hull
= isl_map_unshifted_simple_hull(isl_map_copy(map
));
5288 sv
= isl_basic_map_plain_is_single_valued(hull
);
5290 return plain_pw_multi_aff_from_map(isl_map_domain(map
), hull
);
5292 return pw_multi_aff_from_map_check_strides(map
, hull
);
5293 isl_basic_map_free(hull
);
5298 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_set(__isl_take isl_set
*set
)
5300 return isl_pw_multi_aff_from_map(set
);
5303 /* Convert "map" into an isl_pw_multi_aff (if possible) and
5306 static isl_stat
pw_multi_aff_from_map(__isl_take isl_map
*map
, void *user
)
5308 isl_union_pw_multi_aff
**upma
= user
;
5309 isl_pw_multi_aff
*pma
;
5311 pma
= isl_pw_multi_aff_from_map(map
);
5312 *upma
= isl_union_pw_multi_aff_add_pw_multi_aff(*upma
, pma
);
5314 return *upma
? isl_stat_ok
: isl_stat_error
;
5317 /* Create an isl_union_pw_multi_aff with the given isl_aff on a universe
5320 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_aff(
5321 __isl_take isl_aff
*aff
)
5324 isl_pw_multi_aff
*pma
;
5326 ma
= isl_multi_aff_from_aff(aff
);
5327 pma
= isl_pw_multi_aff_from_multi_aff(ma
);
5328 return isl_union_pw_multi_aff_from_pw_multi_aff(pma
);
5331 /* Try and create an isl_union_pw_multi_aff that is equivalent
5332 * to the given isl_union_map.
5333 * The isl_union_map is required to be single-valued in each space.
5334 * Otherwise, an error is produced.
5336 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_union_map(
5337 __isl_take isl_union_map
*umap
)
5340 isl_union_pw_multi_aff
*upma
;
5342 space
= isl_union_map_get_space(umap
);
5343 upma
= isl_union_pw_multi_aff_empty(space
);
5344 if (isl_union_map_foreach_map(umap
, &pw_multi_aff_from_map
, &upma
) < 0)
5345 upma
= isl_union_pw_multi_aff_free(upma
);
5346 isl_union_map_free(umap
);
5351 /* Try and create an isl_union_pw_multi_aff that is equivalent
5352 * to the given isl_union_set.
5353 * The isl_union_set is required to be a singleton in each space.
5354 * Otherwise, an error is produced.
5356 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_union_set(
5357 __isl_take isl_union_set
*uset
)
5359 return isl_union_pw_multi_aff_from_union_map(uset
);
5362 /* Return the piecewise affine expression "set ? 1 : 0".
5364 __isl_give isl_pw_aff
*isl_set_indicator_function(__isl_take isl_set
*set
)
5367 isl_space
*space
= isl_set_get_space(set
);
5368 isl_local_space
*ls
= isl_local_space_from_space(space
);
5369 isl_aff
*zero
= isl_aff_zero_on_domain(isl_local_space_copy(ls
));
5370 isl_aff
*one
= isl_aff_zero_on_domain(ls
);
5372 one
= isl_aff_add_constant_si(one
, 1);
5373 pa
= isl_pw_aff_alloc(isl_set_copy(set
), one
);
5374 set
= isl_set_complement(set
);
5375 pa
= isl_pw_aff_add_disjoint(pa
, isl_pw_aff_alloc(set
, zero
));
5380 /* Plug in "subs" for dimension "type", "pos" of "aff".
5382 * Let i be the dimension to replace and let "subs" be of the form
5386 * and "aff" of the form
5392 * (a f + d g')/(m d)
5394 * where g' is the result of plugging in "subs" in each of the integer
5397 __isl_give isl_aff
*isl_aff_substitute(__isl_take isl_aff
*aff
,
5398 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
5403 aff
= isl_aff_cow(aff
);
5405 return isl_aff_free(aff
);
5407 ctx
= isl_aff_get_ctx(aff
);
5408 if (!isl_space_is_equal(aff
->ls
->dim
, subs
->ls
->dim
))
5409 isl_die(ctx
, isl_error_invalid
,
5410 "spaces don't match", return isl_aff_free(aff
));
5411 if (isl_local_space_dim(subs
->ls
, isl_dim_div
) != 0)
5412 isl_die(ctx
, isl_error_unsupported
,
5413 "cannot handle divs yet", return isl_aff_free(aff
));
5415 aff
->ls
= isl_local_space_substitute(aff
->ls
, type
, pos
, subs
);
5417 return isl_aff_free(aff
);
5419 aff
->v
= isl_vec_cow(aff
->v
);
5421 return isl_aff_free(aff
);
5423 pos
+= isl_local_space_offset(aff
->ls
, type
);
5426 isl_seq_substitute(aff
->v
->el
, pos
, subs
->v
->el
,
5427 aff
->v
->size
, subs
->v
->size
, v
);
5433 /* Plug in "subs" for dimension "type", "pos" in each of the affine
5434 * expressions in "maff".
5436 __isl_give isl_multi_aff
*isl_multi_aff_substitute(
5437 __isl_take isl_multi_aff
*maff
, enum isl_dim_type type
, unsigned pos
,
5438 __isl_keep isl_aff
*subs
)
5442 maff
= isl_multi_aff_cow(maff
);
5444 return isl_multi_aff_free(maff
);
5446 if (type
== isl_dim_in
)
5449 for (i
= 0; i
< maff
->n
; ++i
) {
5450 maff
->u
.p
[i
] = isl_aff_substitute(maff
->u
.p
[i
],
5453 return isl_multi_aff_free(maff
);
5459 /* Plug in "subs" for dimension "type", "pos" of "pma".
5461 * pma is of the form
5465 * while subs is of the form
5467 * v' = B_j(v) -> S_j
5469 * Each pair i,j such that C_ij = A_i \cap B_i is non-empty
5470 * has a contribution in the result, in particular
5472 * C_ij(S_j) -> M_i(S_j)
5474 * Note that plugging in S_j in C_ij may also result in an empty set
5475 * and this contribution should simply be discarded.
5477 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_substitute(
5478 __isl_take isl_pw_multi_aff
*pma
, enum isl_dim_type type
, unsigned pos
,
5479 __isl_keep isl_pw_aff
*subs
)
5482 isl_pw_multi_aff
*res
;
5485 return isl_pw_multi_aff_free(pma
);
5487 n
= pma
->n
* subs
->n
;
5488 res
= isl_pw_multi_aff_alloc_size(isl_space_copy(pma
->dim
), n
);
5490 for (i
= 0; i
< pma
->n
; ++i
) {
5491 for (j
= 0; j
< subs
->n
; ++j
) {
5493 isl_multi_aff
*res_ij
;
5496 common
= isl_set_intersect(
5497 isl_set_copy(pma
->p
[i
].set
),
5498 isl_set_copy(subs
->p
[j
].set
));
5499 common
= isl_set_substitute(common
,
5500 type
, pos
, subs
->p
[j
].aff
);
5501 empty
= isl_set_plain_is_empty(common
);
5502 if (empty
< 0 || empty
) {
5503 isl_set_free(common
);
5509 res_ij
= isl_multi_aff_substitute(
5510 isl_multi_aff_copy(pma
->p
[i
].maff
),
5511 type
, pos
, subs
->p
[j
].aff
);
5513 res
= isl_pw_multi_aff_add_piece(res
, common
, res_ij
);
5517 isl_pw_multi_aff_free(pma
);
5520 isl_pw_multi_aff_free(pma
);
5521 isl_pw_multi_aff_free(res
);
5525 /* Compute the preimage of a range of dimensions in the affine expression "src"
5526 * under "ma" and put the result in "dst". The number of dimensions in "src"
5527 * that precede the range is given by "n_before". The number of dimensions
5528 * in the range is given by the number of output dimensions of "ma".
5529 * The number of dimensions that follow the range is given by "n_after".
5530 * If "has_denom" is set (to one),
5531 * then "src" and "dst" have an extra initial denominator.
5532 * "n_div_ma" is the number of existentials in "ma"
5533 * "n_div_bset" is the number of existentials in "src"
5534 * The resulting "dst" (which is assumed to have been allocated by
5535 * the caller) contains coefficients for both sets of existentials,
5536 * first those in "ma" and then those in "src".
5537 * f, c1, c2 and g are temporary objects that have been initialized
5540 * Let src represent the expression
5542 * (a(p) + f_u u + b v + f_w w + c(divs))/d
5544 * and let ma represent the expressions
5546 * v_i = (r_i(p) + s_i(y) + t_i(divs'))/m_i
5548 * We start out with the following expression for dst:
5550 * (a(p) + f_u u + 0 y + f_w w + 0 divs' + c(divs) + f \sum_i b_i v_i)/d
5552 * with the multiplication factor f initially equal to 1
5553 * and f \sum_i b_i v_i kept separately.
5554 * For each x_i that we substitute, we multiply the numerator
5555 * (and denominator) of dst by c_1 = m_i and add the numerator
5556 * of the x_i expression multiplied by c_2 = f b_i,
5557 * after removing the common factors of c_1 and c_2.
5558 * The multiplication factor f also needs to be multiplied by c_1
5559 * for the next x_j, j > i.
5561 void isl_seq_preimage(isl_int
*dst
, isl_int
*src
,
5562 __isl_keep isl_multi_aff
*ma
, int n_before
, int n_after
,
5563 int n_div_ma
, int n_div_bmap
,
5564 isl_int f
, isl_int c1
, isl_int c2
, isl_int g
, int has_denom
)
5567 int n_param
, n_in
, n_out
;
5570 n_param
= isl_multi_aff_dim(ma
, isl_dim_param
);
5571 n_in
= isl_multi_aff_dim(ma
, isl_dim_in
);
5572 n_out
= isl_multi_aff_dim(ma
, isl_dim_out
);
5574 isl_seq_cpy(dst
, src
, has_denom
+ 1 + n_param
+ n_before
);
5575 o_dst
= o_src
= has_denom
+ 1 + n_param
+ n_before
;
5576 isl_seq_clr(dst
+ o_dst
, n_in
);
5579 isl_seq_cpy(dst
+ o_dst
, src
+ o_src
, n_after
);
5582 isl_seq_clr(dst
+ o_dst
, n_div_ma
);
5584 isl_seq_cpy(dst
+ o_dst
, src
+ o_src
, n_div_bmap
);
5586 isl_int_set_si(f
, 1);
5588 for (i
= 0; i
< n_out
; ++i
) {
5589 int offset
= has_denom
+ 1 + n_param
+ n_before
+ i
;
5591 if (isl_int_is_zero(src
[offset
]))
5593 isl_int_set(c1
, ma
->u
.p
[i
]->v
->el
[0]);
5594 isl_int_mul(c2
, f
, src
[offset
]);
5595 isl_int_gcd(g
, c1
, c2
);
5596 isl_int_divexact(c1
, c1
, g
);
5597 isl_int_divexact(c2
, c2
, g
);
5599 isl_int_mul(f
, f
, c1
);
5602 isl_seq_combine(dst
+ o_dst
, c1
, dst
+ o_dst
,
5603 c2
, ma
->u
.p
[i
]->v
->el
+ o_src
, 1 + n_param
);
5604 o_dst
+= 1 + n_param
;
5605 o_src
+= 1 + n_param
;
5606 isl_seq_scale(dst
+ o_dst
, dst
+ o_dst
, c1
, n_before
);
5608 isl_seq_combine(dst
+ o_dst
, c1
, dst
+ o_dst
,
5609 c2
, ma
->u
.p
[i
]->v
->el
+ o_src
, n_in
);
5612 isl_seq_scale(dst
+ o_dst
, dst
+ o_dst
, c1
, n_after
);
5614 isl_seq_combine(dst
+ o_dst
, c1
, dst
+ o_dst
,
5615 c2
, ma
->u
.p
[i
]->v
->el
+ o_src
, n_div_ma
);
5618 isl_seq_scale(dst
+ o_dst
, dst
+ o_dst
, c1
, n_div_bmap
);
5620 isl_int_mul(dst
[0], dst
[0], c1
);
5624 /* Compute the pullback of "aff" by the function represented by "ma".
5625 * In other words, plug in "ma" in "aff". The result is an affine expression
5626 * defined over the domain space of "ma".
5628 * If "aff" is represented by
5630 * (a(p) + b x + c(divs))/d
5632 * and ma is represented by
5634 * x = D(p) + F(y) + G(divs')
5636 * then the result is
5638 * (a(p) + b D(p) + b F(y) + b G(divs') + c(divs))/d
5640 * The divs in the local space of the input are similarly adjusted
5641 * through a call to isl_local_space_preimage_multi_aff.
5643 __isl_give isl_aff
*isl_aff_pullback_multi_aff(__isl_take isl_aff
*aff
,
5644 __isl_take isl_multi_aff
*ma
)
5646 isl_aff
*res
= NULL
;
5647 isl_local_space
*ls
;
5648 int n_div_aff
, n_div_ma
;
5649 isl_int f
, c1
, c2
, g
;
5651 ma
= isl_multi_aff_align_divs(ma
);
5655 n_div_aff
= isl_aff_dim(aff
, isl_dim_div
);
5656 n_div_ma
= ma
->n
? isl_aff_dim(ma
->u
.p
[0], isl_dim_div
) : 0;
5658 ls
= isl_aff_get_domain_local_space(aff
);
5659 ls
= isl_local_space_preimage_multi_aff(ls
, isl_multi_aff_copy(ma
));
5660 res
= isl_aff_alloc(ls
);
5669 isl_seq_preimage(res
->v
->el
, aff
->v
->el
, ma
, 0, 0, n_div_ma
, n_div_aff
,
5678 isl_multi_aff_free(ma
);
5679 res
= isl_aff_normalize(res
);
5683 isl_multi_aff_free(ma
);
5688 /* Compute the pullback of "aff1" by the function represented by "aff2".
5689 * In other words, plug in "aff2" in "aff1". The result is an affine expression
5690 * defined over the domain space of "aff1".
5692 * The domain of "aff1" should match the range of "aff2", which means
5693 * that it should be single-dimensional.
5695 __isl_give isl_aff
*isl_aff_pullback_aff(__isl_take isl_aff
*aff1
,
5696 __isl_take isl_aff
*aff2
)
5700 ma
= isl_multi_aff_from_aff(aff2
);
5701 return isl_aff_pullback_multi_aff(aff1
, ma
);
5704 /* Compute the pullback of "ma1" by the function represented by "ma2".
5705 * In other words, plug in "ma2" in "ma1".
5707 * The parameters of "ma1" and "ma2" are assumed to have been aligned.
5709 static __isl_give isl_multi_aff
*isl_multi_aff_pullback_multi_aff_aligned(
5710 __isl_take isl_multi_aff
*ma1
, __isl_take isl_multi_aff
*ma2
)
5713 isl_space
*space
= NULL
;
5715 ma2
= isl_multi_aff_align_divs(ma2
);
5716 ma1
= isl_multi_aff_cow(ma1
);
5720 space
= isl_space_join(isl_multi_aff_get_space(ma2
),
5721 isl_multi_aff_get_space(ma1
));
5723 for (i
= 0; i
< ma1
->n
; ++i
) {
5724 ma1
->u
.p
[i
] = isl_aff_pullback_multi_aff(ma1
->u
.p
[i
],
5725 isl_multi_aff_copy(ma2
));
5730 ma1
= isl_multi_aff_reset_space(ma1
, space
);
5731 isl_multi_aff_free(ma2
);
5734 isl_space_free(space
);
5735 isl_multi_aff_free(ma2
);
5736 isl_multi_aff_free(ma1
);
5740 /* Compute the pullback of "ma1" by the function represented by "ma2".
5741 * In other words, plug in "ma2" in "ma1".
5743 __isl_give isl_multi_aff
*isl_multi_aff_pullback_multi_aff(
5744 __isl_take isl_multi_aff
*ma1
, __isl_take isl_multi_aff
*ma2
)
5746 return isl_multi_aff_align_params_multi_multi_and(ma1
, ma2
,
5747 &isl_multi_aff_pullback_multi_aff_aligned
);
5750 /* Extend the local space of "dst" to include the divs
5751 * in the local space of "src".
5753 * If "src" does not have any divs or if the local spaces of "dst" and
5754 * "src" are the same, then no extension is required.
5756 __isl_give isl_aff
*isl_aff_align_divs(__isl_take isl_aff
*dst
,
5757 __isl_keep isl_aff
*src
)
5760 int src_n_div
, dst_n_div
;
5767 return isl_aff_free(dst
);
5769 ctx
= isl_aff_get_ctx(src
);
5770 equal
= isl_local_space_has_equal_space(src
->ls
, dst
->ls
);
5772 return isl_aff_free(dst
);
5774 isl_die(ctx
, isl_error_invalid
,
5775 "spaces don't match", goto error
);
5777 src_n_div
= isl_local_space_dim(src
->ls
, isl_dim_div
);
5780 equal
= isl_local_space_is_equal(src
->ls
, dst
->ls
);
5782 return isl_aff_free(dst
);
5786 dst_n_div
= isl_local_space_dim(dst
->ls
, isl_dim_div
);
5787 exp1
= isl_alloc_array(ctx
, int, src_n_div
);
5788 exp2
= isl_alloc_array(ctx
, int, dst_n_div
);
5789 if (!exp1
|| (dst_n_div
&& !exp2
))
5792 div
= isl_merge_divs(src
->ls
->div
, dst
->ls
->div
, exp1
, exp2
);
5793 dst
= isl_aff_expand_divs(dst
, div
, exp2
);
5801 return isl_aff_free(dst
);
5804 /* Adjust the local spaces of the affine expressions in "maff"
5805 * such that they all have the save divs.
5807 __isl_give isl_multi_aff
*isl_multi_aff_align_divs(
5808 __isl_take isl_multi_aff
*maff
)
5816 maff
= isl_multi_aff_cow(maff
);
5820 for (i
= 1; i
< maff
->n
; ++i
)
5821 maff
->u
.p
[0] = isl_aff_align_divs(maff
->u
.p
[0], maff
->u
.p
[i
]);
5822 for (i
= 1; i
< maff
->n
; ++i
) {
5823 maff
->u
.p
[i
] = isl_aff_align_divs(maff
->u
.p
[i
], maff
->u
.p
[0]);
5825 return isl_multi_aff_free(maff
);
5831 __isl_give isl_aff
*isl_aff_lift(__isl_take isl_aff
*aff
)
5833 aff
= isl_aff_cow(aff
);
5837 aff
->ls
= isl_local_space_lift(aff
->ls
);
5839 return isl_aff_free(aff
);
5844 /* Lift "maff" to a space with extra dimensions such that the result
5845 * has no more existentially quantified variables.
5846 * If "ls" is not NULL, then *ls is assigned the local space that lies
5847 * at the basis of the lifting applied to "maff".
5849 __isl_give isl_multi_aff
*isl_multi_aff_lift(__isl_take isl_multi_aff
*maff
,
5850 __isl_give isl_local_space
**ls
)
5864 isl_space
*space
= isl_multi_aff_get_domain_space(maff
);
5865 *ls
= isl_local_space_from_space(space
);
5867 return isl_multi_aff_free(maff
);
5872 maff
= isl_multi_aff_cow(maff
);
5873 maff
= isl_multi_aff_align_divs(maff
);
5877 n_div
= isl_aff_dim(maff
->u
.p
[0], isl_dim_div
);
5878 space
= isl_multi_aff_get_space(maff
);
5879 space
= isl_space_lift(isl_space_domain(space
), n_div
);
5880 space
= isl_space_extend_domain_with_range(space
,
5881 isl_multi_aff_get_space(maff
));
5883 return isl_multi_aff_free(maff
);
5884 isl_space_free(maff
->space
);
5885 maff
->space
= space
;
5888 *ls
= isl_aff_get_domain_local_space(maff
->u
.p
[0]);
5890 return isl_multi_aff_free(maff
);
5893 for (i
= 0; i
< maff
->n
; ++i
) {
5894 maff
->u
.p
[i
] = isl_aff_lift(maff
->u
.p
[i
]);
5902 isl_local_space_free(*ls
);
5903 return isl_multi_aff_free(maff
);
5907 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma".
5909 __isl_give isl_pw_aff
*isl_pw_multi_aff_get_pw_aff(
5910 __isl_keep isl_pw_multi_aff
*pma
, int pos
)
5920 n_out
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
5921 if (pos
< 0 || pos
>= n_out
)
5922 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
5923 "index out of bounds", return NULL
);
5925 space
= isl_pw_multi_aff_get_space(pma
);
5926 space
= isl_space_drop_dims(space
, isl_dim_out
,
5927 pos
+ 1, n_out
- pos
- 1);
5928 space
= isl_space_drop_dims(space
, isl_dim_out
, 0, pos
);
5930 pa
= isl_pw_aff_alloc_size(space
, pma
->n
);
5931 for (i
= 0; i
< pma
->n
; ++i
) {
5933 aff
= isl_multi_aff_get_aff(pma
->p
[i
].maff
, pos
);
5934 pa
= isl_pw_aff_add_piece(pa
, isl_set_copy(pma
->p
[i
].set
), aff
);
5940 /* Return an isl_pw_multi_aff with the given "set" as domain and
5941 * an unnamed zero-dimensional range.
5943 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_domain(
5944 __isl_take isl_set
*set
)
5949 space
= isl_set_get_space(set
);
5950 space
= isl_space_from_domain(space
);
5951 ma
= isl_multi_aff_zero(space
);
5952 return isl_pw_multi_aff_alloc(set
, ma
);
5955 /* Add an isl_pw_multi_aff with the given "set" as domain and
5956 * an unnamed zero-dimensional range to *user.
5958 static isl_stat
add_pw_multi_aff_from_domain(__isl_take isl_set
*set
,
5961 isl_union_pw_multi_aff
**upma
= user
;
5962 isl_pw_multi_aff
*pma
;
5964 pma
= isl_pw_multi_aff_from_domain(set
);
5965 *upma
= isl_union_pw_multi_aff_add_pw_multi_aff(*upma
, pma
);
5970 /* Return an isl_union_pw_multi_aff with the given "uset" as domain and
5971 * an unnamed zero-dimensional range.
5973 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_domain(
5974 __isl_take isl_union_set
*uset
)
5977 isl_union_pw_multi_aff
*upma
;
5982 space
= isl_union_set_get_space(uset
);
5983 upma
= isl_union_pw_multi_aff_empty(space
);
5985 if (isl_union_set_foreach_set(uset
,
5986 &add_pw_multi_aff_from_domain
, &upma
) < 0)
5989 isl_union_set_free(uset
);
5992 isl_union_set_free(uset
);
5993 isl_union_pw_multi_aff_free(upma
);
5997 /* Convert "pma" to an isl_map and add it to *umap.
5999 static isl_stat
map_from_pw_multi_aff(__isl_take isl_pw_multi_aff
*pma
,
6002 isl_union_map
**umap
= user
;
6005 map
= isl_map_from_pw_multi_aff(pma
);
6006 *umap
= isl_union_map_add_map(*umap
, map
);
6011 /* Construct a union map mapping the domain of the union
6012 * piecewise multi-affine expression to its range, with each dimension
6013 * in the range equated to the corresponding affine expression on its cell.
6015 __isl_give isl_union_map
*isl_union_map_from_union_pw_multi_aff(
6016 __isl_take isl_union_pw_multi_aff
*upma
)
6019 isl_union_map
*umap
;
6024 space
= isl_union_pw_multi_aff_get_space(upma
);
6025 umap
= isl_union_map_empty(space
);
6027 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma
,
6028 &map_from_pw_multi_aff
, &umap
) < 0)
6031 isl_union_pw_multi_aff_free(upma
);
6034 isl_union_pw_multi_aff_free(upma
);
6035 isl_union_map_free(umap
);
6039 /* Local data for bin_entry and the callback "fn".
6041 struct isl_union_pw_multi_aff_bin_data
{
6042 isl_union_pw_multi_aff
*upma2
;
6043 isl_union_pw_multi_aff
*res
;
6044 isl_pw_multi_aff
*pma
;
6045 isl_stat (*fn
)(__isl_take isl_pw_multi_aff
*pma
, void *user
);
6048 /* Given an isl_pw_multi_aff from upma1, store it in data->pma
6049 * and call data->fn for each isl_pw_multi_aff in data->upma2.
6051 static isl_stat
bin_entry(__isl_take isl_pw_multi_aff
*pma
, void *user
)
6053 struct isl_union_pw_multi_aff_bin_data
*data
= user
;
6057 r
= isl_union_pw_multi_aff_foreach_pw_multi_aff(data
->upma2
,
6059 isl_pw_multi_aff_free(pma
);
6064 /* Call "fn" on each pair of isl_pw_multi_affs in "upma1" and "upma2".
6065 * The isl_pw_multi_aff from upma1 is stored in data->pma (where data is
6066 * passed as user field) and the isl_pw_multi_aff from upma2 is available
6067 * as *entry. The callback should adjust data->res if desired.
6069 static __isl_give isl_union_pw_multi_aff
*bin_op(
6070 __isl_take isl_union_pw_multi_aff
*upma1
,
6071 __isl_take isl_union_pw_multi_aff
*upma2
,
6072 isl_stat (*fn
)(__isl_take isl_pw_multi_aff
*pma
, void *user
))
6075 struct isl_union_pw_multi_aff_bin_data data
= { NULL
, NULL
, NULL
, fn
};
6077 space
= isl_union_pw_multi_aff_get_space(upma2
);
6078 upma1
= isl_union_pw_multi_aff_align_params(upma1
, space
);
6079 space
= isl_union_pw_multi_aff_get_space(upma1
);
6080 upma2
= isl_union_pw_multi_aff_align_params(upma2
, space
);
6082 if (!upma1
|| !upma2
)
6086 data
.res
= isl_union_pw_multi_aff_alloc_same_size(upma1
);
6087 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma1
,
6088 &bin_entry
, &data
) < 0)
6091 isl_union_pw_multi_aff_free(upma1
);
6092 isl_union_pw_multi_aff_free(upma2
);
6095 isl_union_pw_multi_aff_free(upma1
);
6096 isl_union_pw_multi_aff_free(upma2
);
6097 isl_union_pw_multi_aff_free(data
.res
);
6101 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
6102 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
6104 static __isl_give isl_pw_multi_aff
*pw_multi_aff_range_product(
6105 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
6109 space
= isl_space_range_product(isl_pw_multi_aff_get_space(pma1
),
6110 isl_pw_multi_aff_get_space(pma2
));
6111 return isl_pw_multi_aff_on_shared_domain_in(pma1
, pma2
, space
,
6112 &isl_multi_aff_range_product
);
6115 /* Given two isl_pw_multi_affs A -> B and C -> D,
6116 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
6118 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_range_product(
6119 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
6121 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
6122 &pw_multi_aff_range_product
);
6125 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
6126 * construct an isl_pw_multi_aff (A * C) -> (B, D).
6128 static __isl_give isl_pw_multi_aff
*pw_multi_aff_flat_range_product(
6129 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
6133 space
= isl_space_range_product(isl_pw_multi_aff_get_space(pma1
),
6134 isl_pw_multi_aff_get_space(pma2
));
6135 space
= isl_space_flatten_range(space
);
6136 return isl_pw_multi_aff_on_shared_domain_in(pma1
, pma2
, space
,
6137 &isl_multi_aff_flat_range_product
);
6140 /* Given two isl_pw_multi_affs A -> B and C -> D,
6141 * construct an isl_pw_multi_aff (A * C) -> (B, D).
6143 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_flat_range_product(
6144 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
6146 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
6147 &pw_multi_aff_flat_range_product
);
6150 /* If data->pma and "pma2" have the same domain space, then compute
6151 * their flat range product and the result to data->res.
6153 static isl_stat
flat_range_product_entry(__isl_take isl_pw_multi_aff
*pma2
,
6156 struct isl_union_pw_multi_aff_bin_data
*data
= user
;
6158 if (!isl_space_tuple_is_equal(data
->pma
->dim
, isl_dim_in
,
6159 pma2
->dim
, isl_dim_in
)) {
6160 isl_pw_multi_aff_free(pma2
);
6164 pma2
= isl_pw_multi_aff_flat_range_product(
6165 isl_pw_multi_aff_copy(data
->pma
), pma2
);
6167 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma2
);
6172 /* Given two isl_union_pw_multi_affs A -> B and C -> D,
6173 * construct an isl_union_pw_multi_aff (A * C) -> (B, D).
6175 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_flat_range_product(
6176 __isl_take isl_union_pw_multi_aff
*upma1
,
6177 __isl_take isl_union_pw_multi_aff
*upma2
)
6179 return bin_op(upma1
, upma2
, &flat_range_product_entry
);
6182 /* Replace the affine expressions at position "pos" in "pma" by "pa".
6183 * The parameters are assumed to have been aligned.
6185 * The implementation essentially performs an isl_pw_*_on_shared_domain,
6186 * except that it works on two different isl_pw_* types.
6188 static __isl_give isl_pw_multi_aff
*pw_multi_aff_set_pw_aff(
6189 __isl_take isl_pw_multi_aff
*pma
, unsigned pos
,
6190 __isl_take isl_pw_aff
*pa
)
6193 isl_pw_multi_aff
*res
= NULL
;
6198 if (!isl_space_tuple_is_equal(pma
->dim
, isl_dim_in
,
6199 pa
->dim
, isl_dim_in
))
6200 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
6201 "domains don't match", goto error
);
6202 if (pos
>= isl_pw_multi_aff_dim(pma
, isl_dim_out
))
6203 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
6204 "index out of bounds", goto error
);
6207 res
= isl_pw_multi_aff_alloc_size(isl_pw_multi_aff_get_space(pma
), n
);
6209 for (i
= 0; i
< pma
->n
; ++i
) {
6210 for (j
= 0; j
< pa
->n
; ++j
) {
6212 isl_multi_aff
*res_ij
;
6215 common
= isl_set_intersect(isl_set_copy(pma
->p
[i
].set
),
6216 isl_set_copy(pa
->p
[j
].set
));
6217 empty
= isl_set_plain_is_empty(common
);
6218 if (empty
< 0 || empty
) {
6219 isl_set_free(common
);
6225 res_ij
= isl_multi_aff_set_aff(
6226 isl_multi_aff_copy(pma
->p
[i
].maff
), pos
,
6227 isl_aff_copy(pa
->p
[j
].aff
));
6228 res_ij
= isl_multi_aff_gist(res_ij
,
6229 isl_set_copy(common
));
6231 res
= isl_pw_multi_aff_add_piece(res
, common
, res_ij
);
6235 isl_pw_multi_aff_free(pma
);
6236 isl_pw_aff_free(pa
);
6239 isl_pw_multi_aff_free(pma
);
6240 isl_pw_aff_free(pa
);
6241 return isl_pw_multi_aff_free(res
);
6244 /* Replace the affine expressions at position "pos" in "pma" by "pa".
6246 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_set_pw_aff(
6247 __isl_take isl_pw_multi_aff
*pma
, unsigned pos
,
6248 __isl_take isl_pw_aff
*pa
)
6250 isl_bool equal_params
;
6254 equal_params
= isl_space_has_equal_params(pma
->dim
, pa
->dim
);
6255 if (equal_params
< 0)
6258 return pw_multi_aff_set_pw_aff(pma
, pos
, pa
);
6259 if (isl_pw_multi_aff_check_named_params(pma
) < 0 ||
6260 isl_pw_aff_check_named_params(pa
) < 0)
6262 pma
= isl_pw_multi_aff_align_params(pma
, isl_pw_aff_get_space(pa
));
6263 pa
= isl_pw_aff_align_params(pa
, isl_pw_multi_aff_get_space(pma
));
6264 return pw_multi_aff_set_pw_aff(pma
, pos
, pa
);
6266 isl_pw_multi_aff_free(pma
);
6267 isl_pw_aff_free(pa
);
6271 /* Do the parameters of "pa" match those of "space"?
6273 isl_bool
isl_pw_aff_matching_params(__isl_keep isl_pw_aff
*pa
,
6274 __isl_keep isl_space
*space
)
6276 isl_space
*pa_space
;
6280 return isl_bool_error
;
6282 pa_space
= isl_pw_aff_get_space(pa
);
6284 match
= isl_space_has_equal_params(space
, pa_space
);
6286 isl_space_free(pa_space
);
6290 /* Check that the domain space of "pa" matches "space".
6292 isl_stat
isl_pw_aff_check_match_domain_space(__isl_keep isl_pw_aff
*pa
,
6293 __isl_keep isl_space
*space
)
6295 isl_space
*pa_space
;
6299 return isl_stat_error
;
6301 pa_space
= isl_pw_aff_get_space(pa
);
6303 match
= isl_space_has_equal_params(space
, pa_space
);
6307 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
6308 "parameters don't match", goto error
);
6309 match
= isl_space_tuple_is_equal(space
, isl_dim_in
,
6310 pa_space
, isl_dim_in
);
6314 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
6315 "domains don't match", goto error
);
6316 isl_space_free(pa_space
);
6319 isl_space_free(pa_space
);
6320 return isl_stat_error
;
6328 #include <isl_multi_explicit_domain.c>
6329 #include <isl_multi_pw_aff_explicit_domain.c>
6330 #include <isl_multi_templ.c>
6331 #include <isl_multi_apply_set.c>
6332 #include <isl_multi_coalesce.c>
6333 #include <isl_multi_dims.c>
6334 #include <isl_multi_gist.c>
6335 #include <isl_multi_hash.c>
6336 #include <isl_multi_align_set.c>
6337 #include <isl_multi_intersect.c>
6339 /* Does "mpa" have a non-trivial explicit domain?
6341 * The explicit domain, if present, is trivial if it represents
6342 * an (obviously) universe set.
6344 isl_bool
isl_multi_pw_aff_has_non_trivial_domain(
6345 __isl_keep isl_multi_pw_aff
*mpa
)
6348 return isl_bool_error
;
6349 if (!isl_multi_pw_aff_has_explicit_domain(mpa
))
6350 return isl_bool_false
;
6351 return isl_bool_not(isl_set_plain_is_universe(mpa
->u
.dom
));
6354 /* Scale the elements of "pma" by the corresponding elements of "mv".
6356 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_scale_multi_val(
6357 __isl_take isl_pw_multi_aff
*pma
, __isl_take isl_multi_val
*mv
)
6360 isl_bool equal_params
;
6362 pma
= isl_pw_multi_aff_cow(pma
);
6365 if (!isl_space_tuple_is_equal(pma
->dim
, isl_dim_out
,
6366 mv
->space
, isl_dim_set
))
6367 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
6368 "spaces don't match", goto error
);
6369 equal_params
= isl_space_has_equal_params(pma
->dim
, mv
->space
);
6370 if (equal_params
< 0)
6372 if (!equal_params
) {
6373 pma
= isl_pw_multi_aff_align_params(pma
,
6374 isl_multi_val_get_space(mv
));
6375 mv
= isl_multi_val_align_params(mv
,
6376 isl_pw_multi_aff_get_space(pma
));
6381 for (i
= 0; i
< pma
->n
; ++i
) {
6382 pma
->p
[i
].maff
= isl_multi_aff_scale_multi_val(pma
->p
[i
].maff
,
6383 isl_multi_val_copy(mv
));
6384 if (!pma
->p
[i
].maff
)
6388 isl_multi_val_free(mv
);
6391 isl_multi_val_free(mv
);
6392 isl_pw_multi_aff_free(pma
);
6396 /* This function is called for each entry of an isl_union_pw_multi_aff.
6397 * If the space of the entry matches that of data->mv,
6398 * then apply isl_pw_multi_aff_scale_multi_val and return the result.
6399 * Otherwise, return an empty isl_pw_multi_aff.
6401 static __isl_give isl_pw_multi_aff
*union_pw_multi_aff_scale_multi_val_entry(
6402 __isl_take isl_pw_multi_aff
*pma
, void *user
)
6404 isl_multi_val
*mv
= user
;
6408 if (!isl_space_tuple_is_equal(pma
->dim
, isl_dim_out
,
6409 mv
->space
, isl_dim_set
)) {
6410 isl_space
*space
= isl_pw_multi_aff_get_space(pma
);
6411 isl_pw_multi_aff_free(pma
);
6412 return isl_pw_multi_aff_empty(space
);
6415 return isl_pw_multi_aff_scale_multi_val(pma
, isl_multi_val_copy(mv
));
6418 /* Scale the elements of "upma" by the corresponding elements of "mv",
6419 * for those entries that match the space of "mv".
6421 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_scale_multi_val(
6422 __isl_take isl_union_pw_multi_aff
*upma
, __isl_take isl_multi_val
*mv
)
6424 upma
= isl_union_pw_multi_aff_align_params(upma
,
6425 isl_multi_val_get_space(mv
));
6426 mv
= isl_multi_val_align_params(mv
,
6427 isl_union_pw_multi_aff_get_space(upma
));
6431 return isl_union_pw_multi_aff_transform(upma
,
6432 &union_pw_multi_aff_scale_multi_val_entry
, mv
);
6434 isl_multi_val_free(mv
);
6437 isl_multi_val_free(mv
);
6438 isl_union_pw_multi_aff_free(upma
);
6442 /* Construct and return a piecewise multi affine expression
6443 * in the given space with value zero in each of the output dimensions and
6444 * a universe domain.
6446 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_zero(__isl_take isl_space
*space
)
6448 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_zero(space
));
6451 /* Construct and return a piecewise multi affine expression
6452 * that is equal to the given piecewise affine expression.
6454 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_pw_aff(
6455 __isl_take isl_pw_aff
*pa
)
6459 isl_pw_multi_aff
*pma
;
6464 space
= isl_pw_aff_get_space(pa
);
6465 pma
= isl_pw_multi_aff_alloc_size(space
, pa
->n
);
6467 for (i
= 0; i
< pa
->n
; ++i
) {
6471 set
= isl_set_copy(pa
->p
[i
].set
);
6472 ma
= isl_multi_aff_from_aff(isl_aff_copy(pa
->p
[i
].aff
));
6473 pma
= isl_pw_multi_aff_add_piece(pma
, set
, ma
);
6476 isl_pw_aff_free(pa
);
6480 /* Construct a set or map mapping the shared (parameter) domain
6481 * of the piecewise affine expressions to the range of "mpa"
6482 * with each dimension in the range equated to the
6483 * corresponding piecewise affine expression.
6485 static __isl_give isl_map
*map_from_multi_pw_aff(
6486 __isl_take isl_multi_pw_aff
*mpa
)
6495 if (isl_space_dim(mpa
->space
, isl_dim_out
) != mpa
->n
)
6496 isl_die(isl_multi_pw_aff_get_ctx(mpa
), isl_error_internal
,
6497 "invalid space", goto error
);
6499 space
= isl_multi_pw_aff_get_domain_space(mpa
);
6500 map
= isl_map_universe(isl_space_from_domain(space
));
6502 for (i
= 0; i
< mpa
->n
; ++i
) {
6506 pa
= isl_pw_aff_copy(mpa
->u
.p
[i
]);
6507 map_i
= map_from_pw_aff(pa
);
6509 map
= isl_map_flat_range_product(map
, map_i
);
6512 map
= isl_map_reset_space(map
, isl_multi_pw_aff_get_space(mpa
));
6514 isl_multi_pw_aff_free(mpa
);
6517 isl_multi_pw_aff_free(mpa
);
6521 /* Construct a map mapping the shared domain
6522 * of the piecewise affine expressions to the range of "mpa"
6523 * with each dimension in the range equated to the
6524 * corresponding piecewise affine expression.
6526 __isl_give isl_map
*isl_map_from_multi_pw_aff(__isl_take isl_multi_pw_aff
*mpa
)
6530 if (isl_space_is_set(mpa
->space
))
6531 isl_die(isl_multi_pw_aff_get_ctx(mpa
), isl_error_internal
,
6532 "space of input is not a map", goto error
);
6534 return map_from_multi_pw_aff(mpa
);
6536 isl_multi_pw_aff_free(mpa
);
6540 /* Construct a set mapping the shared parameter domain
6541 * of the piecewise affine expressions to the space of "mpa"
6542 * with each dimension in the range equated to the
6543 * corresponding piecewise affine expression.
6545 __isl_give isl_set
*isl_set_from_multi_pw_aff(__isl_take isl_multi_pw_aff
*mpa
)
6549 if (!isl_space_is_set(mpa
->space
))
6550 isl_die(isl_multi_pw_aff_get_ctx(mpa
), isl_error_internal
,
6551 "space of input is not a set", goto error
);
6553 return map_from_multi_pw_aff(mpa
);
6555 isl_multi_pw_aff_free(mpa
);
6559 /* Construct and return a piecewise multi affine expression
6560 * that is equal to the given multi piecewise affine expression
6561 * on the shared domain of the piecewise affine expressions,
6562 * in the special case of a 0D multi piecewise affine expression.
6564 * Create a piecewise multi affine expression with the explicit domain of
6565 * the 0D multi piecewise affine expression as domain.
6567 static __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_multi_pw_aff_0D(
6568 __isl_take isl_multi_pw_aff
*mpa
)
6574 space
= isl_multi_pw_aff_get_space(mpa
);
6575 dom
= isl_multi_pw_aff_get_explicit_domain(mpa
);
6576 isl_multi_pw_aff_free(mpa
);
6578 ma
= isl_multi_aff_zero(space
);
6579 return isl_pw_multi_aff_alloc(dom
, ma
);
6582 /* Construct and return a piecewise multi affine expression
6583 * that is equal to the given multi piecewise affine expression
6584 * on the shared domain of the piecewise affine expressions.
6586 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_multi_pw_aff(
6587 __isl_take isl_multi_pw_aff
*mpa
)
6592 isl_pw_multi_aff
*pma
;
6598 return isl_pw_multi_aff_from_multi_pw_aff_0D(mpa
);
6600 space
= isl_multi_pw_aff_get_space(mpa
);
6601 pa
= isl_multi_pw_aff_get_pw_aff(mpa
, 0);
6602 pma
= isl_pw_multi_aff_from_pw_aff(pa
);
6604 for (i
= 1; i
< mpa
->n
; ++i
) {
6605 isl_pw_multi_aff
*pma_i
;
6607 pa
= isl_multi_pw_aff_get_pw_aff(mpa
, i
);
6608 pma_i
= isl_pw_multi_aff_from_pw_aff(pa
);
6609 pma
= isl_pw_multi_aff_range_product(pma
, pma_i
);
6612 pma
= isl_pw_multi_aff_reset_space(pma
, space
);
6614 isl_multi_pw_aff_free(mpa
);
6618 /* Construct and return a multi piecewise affine expression
6619 * that is equal to the given multi affine expression.
6621 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_from_multi_aff(
6622 __isl_take isl_multi_aff
*ma
)
6625 isl_multi_pw_aff
*mpa
;
6630 n
= isl_multi_aff_dim(ma
, isl_dim_out
);
6631 mpa
= isl_multi_pw_aff_alloc(isl_multi_aff_get_space(ma
));
6633 for (i
= 0; i
< n
; ++i
) {
6636 pa
= isl_pw_aff_from_aff(isl_multi_aff_get_aff(ma
, i
));
6637 mpa
= isl_multi_pw_aff_set_pw_aff(mpa
, i
, pa
);
6640 isl_multi_aff_free(ma
);
6644 /* Construct and return a multi piecewise affine expression
6645 * that is equal to the given piecewise multi affine expression.
6647 * If the resulting multi piecewise affine expression has
6648 * an explicit domain, then assign it the domain of the input.
6649 * In other cases, the domain is stored in the individual elements.
6651 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_from_pw_multi_aff(
6652 __isl_take isl_pw_multi_aff
*pma
)
6656 isl_multi_pw_aff
*mpa
;
6661 n
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
6662 space
= isl_pw_multi_aff_get_space(pma
);
6663 mpa
= isl_multi_pw_aff_alloc(space
);
6665 for (i
= 0; i
< n
; ++i
) {
6668 pa
= isl_pw_multi_aff_get_pw_aff(pma
, i
);
6669 mpa
= isl_multi_pw_aff_set_pw_aff(mpa
, i
, pa
);
6671 if (isl_multi_pw_aff_has_explicit_domain(mpa
)) {
6674 dom
= isl_pw_multi_aff_domain(isl_pw_multi_aff_copy(pma
));
6675 mpa
= isl_multi_pw_aff_intersect_domain(mpa
, dom
);
6678 isl_pw_multi_aff_free(pma
);
6682 /* Do "pa1" and "pa2" represent the same function?
6684 * We first check if they are obviously equal.
6685 * If not, we convert them to maps and check if those are equal.
6687 * If "pa1" or "pa2" contain any NaNs, then they are considered
6688 * not to be the same. A NaN is not equal to anything, not even
6691 isl_bool
isl_pw_aff_is_equal(__isl_keep isl_pw_aff
*pa1
,
6692 __isl_keep isl_pw_aff
*pa2
)
6696 isl_map
*map1
, *map2
;
6699 return isl_bool_error
;
6701 equal
= isl_pw_aff_plain_is_equal(pa1
, pa2
);
6702 if (equal
< 0 || equal
)
6704 has_nan
= either_involves_nan(pa1
, pa2
);
6706 return isl_bool_error
;
6708 return isl_bool_false
;
6710 map1
= map_from_pw_aff(isl_pw_aff_copy(pa1
));
6711 map2
= map_from_pw_aff(isl_pw_aff_copy(pa2
));
6712 equal
= isl_map_is_equal(map1
, map2
);
6719 /* Do "mpa1" and "mpa2" represent the same function?
6721 * Note that we cannot convert the entire isl_multi_pw_aff
6722 * to a map because the domains of the piecewise affine expressions
6723 * may not be the same.
6725 isl_bool
isl_multi_pw_aff_is_equal(__isl_keep isl_multi_pw_aff
*mpa1
,
6726 __isl_keep isl_multi_pw_aff
*mpa2
)
6729 isl_bool equal
, equal_params
;
6732 return isl_bool_error
;
6734 equal_params
= isl_space_has_equal_params(mpa1
->space
, mpa2
->space
);
6735 if (equal_params
< 0)
6736 return isl_bool_error
;
6737 if (!equal_params
) {
6738 if (!isl_space_has_named_params(mpa1
->space
))
6739 return isl_bool_false
;
6740 if (!isl_space_has_named_params(mpa2
->space
))
6741 return isl_bool_false
;
6742 mpa1
= isl_multi_pw_aff_copy(mpa1
);
6743 mpa2
= isl_multi_pw_aff_copy(mpa2
);
6744 mpa1
= isl_multi_pw_aff_align_params(mpa1
,
6745 isl_multi_pw_aff_get_space(mpa2
));
6746 mpa2
= isl_multi_pw_aff_align_params(mpa2
,
6747 isl_multi_pw_aff_get_space(mpa1
));
6748 equal
= isl_multi_pw_aff_is_equal(mpa1
, mpa2
);
6749 isl_multi_pw_aff_free(mpa1
);
6750 isl_multi_pw_aff_free(mpa2
);
6754 equal
= isl_space_is_equal(mpa1
->space
, mpa2
->space
);
6755 if (equal
< 0 || !equal
)
6758 for (i
= 0; i
< mpa1
->n
; ++i
) {
6759 equal
= isl_pw_aff_is_equal(mpa1
->u
.p
[i
], mpa2
->u
.p
[i
]);
6760 if (equal
< 0 || !equal
)
6764 return isl_bool_true
;
6767 /* Do "pma1" and "pma2" represent the same function?
6769 * First check if they are obviously equal.
6770 * If not, then convert them to maps and check if those are equal.
6772 * If "pa1" or "pa2" contain any NaNs, then they are considered
6773 * not to be the same. A NaN is not equal to anything, not even
6776 isl_bool
isl_pw_multi_aff_is_equal(__isl_keep isl_pw_multi_aff
*pma1
,
6777 __isl_keep isl_pw_multi_aff
*pma2
)
6781 isl_map
*map1
, *map2
;
6784 return isl_bool_error
;
6786 equal
= isl_pw_multi_aff_plain_is_equal(pma1
, pma2
);
6787 if (equal
< 0 || equal
)
6789 has_nan
= isl_pw_multi_aff_involves_nan(pma1
);
6790 if (has_nan
>= 0 && !has_nan
)
6791 has_nan
= isl_pw_multi_aff_involves_nan(pma2
);
6792 if (has_nan
< 0 || has_nan
)
6793 return isl_bool_not(has_nan
);
6795 map1
= isl_map_from_pw_multi_aff(isl_pw_multi_aff_copy(pma1
));
6796 map2
= isl_map_from_pw_multi_aff(isl_pw_multi_aff_copy(pma2
));
6797 equal
= isl_map_is_equal(map1
, map2
);
6804 /* Compute the pullback of "mpa" by the function represented by "ma".
6805 * In other words, plug in "ma" in "mpa".
6807 * The parameters of "mpa" and "ma" are assumed to have been aligned.
6809 * If "mpa" has an explicit domain, then it is this domain
6810 * that needs to undergo a pullback, i.e., a preimage.
6812 static __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_pullback_multi_aff_aligned(
6813 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_multi_aff
*ma
)
6816 isl_space
*space
= NULL
;
6818 mpa
= isl_multi_pw_aff_cow(mpa
);
6822 space
= isl_space_join(isl_multi_aff_get_space(ma
),
6823 isl_multi_pw_aff_get_space(mpa
));
6827 for (i
= 0; i
< mpa
->n
; ++i
) {
6828 mpa
->u
.p
[i
] = isl_pw_aff_pullback_multi_aff(mpa
->u
.p
[i
],
6829 isl_multi_aff_copy(ma
));
6833 if (isl_multi_pw_aff_has_explicit_domain(mpa
)) {
6834 mpa
->u
.dom
= isl_set_preimage_multi_aff(mpa
->u
.dom
,
6835 isl_multi_aff_copy(ma
));
6840 isl_multi_aff_free(ma
);
6841 isl_space_free(mpa
->space
);
6845 isl_space_free(space
);
6846 isl_multi_pw_aff_free(mpa
);
6847 isl_multi_aff_free(ma
);
6851 /* Compute the pullback of "mpa" by the function represented by "ma".
6852 * In other words, plug in "ma" in "mpa".
6854 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_pullback_multi_aff(
6855 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_multi_aff
*ma
)
6857 isl_bool equal_params
;
6861 equal_params
= isl_space_has_equal_params(mpa
->space
, ma
->space
);
6862 if (equal_params
< 0)
6865 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa
, ma
);
6866 mpa
= isl_multi_pw_aff_align_params(mpa
, isl_multi_aff_get_space(ma
));
6867 ma
= isl_multi_aff_align_params(ma
, isl_multi_pw_aff_get_space(mpa
));
6868 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa
, ma
);
6870 isl_multi_pw_aff_free(mpa
);
6871 isl_multi_aff_free(ma
);
6875 /* Compute the pullback of "mpa" by the function represented by "pma".
6876 * In other words, plug in "pma" in "mpa".
6878 * The parameters of "mpa" and "mpa" are assumed to have been aligned.
6880 * If "mpa" has an explicit domain, then it is this domain
6881 * that needs to undergo a pullback, i.e., a preimage.
6883 static __isl_give isl_multi_pw_aff
*
6884 isl_multi_pw_aff_pullback_pw_multi_aff_aligned(
6885 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_pw_multi_aff
*pma
)
6888 isl_space
*space
= NULL
;
6890 mpa
= isl_multi_pw_aff_cow(mpa
);
6894 space
= isl_space_join(isl_pw_multi_aff_get_space(pma
),
6895 isl_multi_pw_aff_get_space(mpa
));
6897 for (i
= 0; i
< mpa
->n
; ++i
) {
6898 mpa
->u
.p
[i
] = isl_pw_aff_pullback_pw_multi_aff_aligned(
6899 mpa
->u
.p
[i
], isl_pw_multi_aff_copy(pma
));
6903 if (isl_multi_pw_aff_has_explicit_domain(mpa
)) {
6904 mpa
->u
.dom
= isl_set_preimage_pw_multi_aff(mpa
->u
.dom
,
6905 isl_pw_multi_aff_copy(pma
));
6910 isl_pw_multi_aff_free(pma
);
6911 isl_space_free(mpa
->space
);
6915 isl_space_free(space
);
6916 isl_multi_pw_aff_free(mpa
);
6917 isl_pw_multi_aff_free(pma
);
6921 /* Compute the pullback of "mpa" by the function represented by "pma".
6922 * In other words, plug in "pma" in "mpa".
6924 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_pullback_pw_multi_aff(
6925 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_pw_multi_aff
*pma
)
6927 isl_bool equal_params
;
6931 equal_params
= isl_space_has_equal_params(mpa
->space
, pma
->dim
);
6932 if (equal_params
< 0)
6935 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa
, pma
);
6936 mpa
= isl_multi_pw_aff_align_params(mpa
,
6937 isl_pw_multi_aff_get_space(pma
));
6938 pma
= isl_pw_multi_aff_align_params(pma
,
6939 isl_multi_pw_aff_get_space(mpa
));
6940 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa
, pma
);
6942 isl_multi_pw_aff_free(mpa
);
6943 isl_pw_multi_aff_free(pma
);
6947 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
6948 * with the domain of "aff". The domain of the result is the same
6950 * "mpa" and "aff" are assumed to have been aligned.
6952 * We first extract the parametric constant from "aff", defined
6953 * over the correct domain.
6954 * Then we add the appropriate combinations of the members of "mpa".
6955 * Finally, we add the integer divisions through recursive calls.
6957 static __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_aff_aligned(
6958 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_aff
*aff
)
6966 n_in
= isl_aff_dim(aff
, isl_dim_in
);
6967 n_div
= isl_aff_dim(aff
, isl_dim_div
);
6969 space
= isl_space_domain(isl_multi_pw_aff_get_space(mpa
));
6970 tmp
= isl_aff_copy(aff
);
6971 tmp
= isl_aff_drop_dims(tmp
, isl_dim_div
, 0, n_div
);
6972 tmp
= isl_aff_drop_dims(tmp
, isl_dim_in
, 0, n_in
);
6973 tmp
= isl_aff_add_dims(tmp
, isl_dim_in
,
6974 isl_space_dim(space
, isl_dim_set
));
6975 tmp
= isl_aff_reset_domain_space(tmp
, space
);
6976 pa
= isl_pw_aff_from_aff(tmp
);
6978 for (i
= 0; i
< n_in
; ++i
) {
6981 if (!isl_aff_involves_dims(aff
, isl_dim_in
, i
, 1))
6983 v
= isl_aff_get_coefficient_val(aff
, isl_dim_in
, i
);
6984 pa_i
= isl_multi_pw_aff_get_pw_aff(mpa
, i
);
6985 pa_i
= isl_pw_aff_scale_val(pa_i
, v
);
6986 pa
= isl_pw_aff_add(pa
, pa_i
);
6989 for (i
= 0; i
< n_div
; ++i
) {
6993 if (!isl_aff_involves_dims(aff
, isl_dim_div
, i
, 1))
6995 div
= isl_aff_get_div(aff
, i
);
6996 pa_i
= isl_multi_pw_aff_apply_aff_aligned(
6997 isl_multi_pw_aff_copy(mpa
), div
);
6998 pa_i
= isl_pw_aff_floor(pa_i
);
6999 v
= isl_aff_get_coefficient_val(aff
, isl_dim_div
, i
);
7000 pa_i
= isl_pw_aff_scale_val(pa_i
, v
);
7001 pa
= isl_pw_aff_add(pa
, pa_i
);
7004 isl_multi_pw_aff_free(mpa
);
7010 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
7011 * with the domain of "aff". The domain of the result is the same
7014 __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_aff(
7015 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_aff
*aff
)
7017 isl_bool equal_params
;
7021 equal_params
= isl_space_has_equal_params(aff
->ls
->dim
, mpa
->space
);
7022 if (equal_params
< 0)
7025 return isl_multi_pw_aff_apply_aff_aligned(mpa
, aff
);
7027 aff
= isl_aff_align_params(aff
, isl_multi_pw_aff_get_space(mpa
));
7028 mpa
= isl_multi_pw_aff_align_params(mpa
, isl_aff_get_space(aff
));
7030 return isl_multi_pw_aff_apply_aff_aligned(mpa
, aff
);
7033 isl_multi_pw_aff_free(mpa
);
7037 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
7038 * with the domain of "pa". The domain of the result is the same
7040 * "mpa" and "pa" are assumed to have been aligned.
7042 * We consider each piece in turn. Note that the domains of the
7043 * pieces are assumed to be disjoint and they remain disjoint
7044 * after taking the preimage (over the same function).
7046 static __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_pw_aff_aligned(
7047 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_pw_aff
*pa
)
7056 space
= isl_space_join(isl_multi_pw_aff_get_space(mpa
),
7057 isl_pw_aff_get_space(pa
));
7058 res
= isl_pw_aff_empty(space
);
7060 for (i
= 0; i
< pa
->n
; ++i
) {
7064 pa_i
= isl_multi_pw_aff_apply_aff_aligned(
7065 isl_multi_pw_aff_copy(mpa
),
7066 isl_aff_copy(pa
->p
[i
].aff
));
7067 domain
= isl_set_copy(pa
->p
[i
].set
);
7068 domain
= isl_set_preimage_multi_pw_aff(domain
,
7069 isl_multi_pw_aff_copy(mpa
));
7070 pa_i
= isl_pw_aff_intersect_domain(pa_i
, domain
);
7071 res
= isl_pw_aff_add_disjoint(res
, pa_i
);
7074 isl_pw_aff_free(pa
);
7075 isl_multi_pw_aff_free(mpa
);
7078 isl_pw_aff_free(pa
);
7079 isl_multi_pw_aff_free(mpa
);
7083 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
7084 * with the domain of "pa". The domain of the result is the same
7087 __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_pw_aff(
7088 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_pw_aff
*pa
)
7090 isl_bool equal_params
;
7094 equal_params
= isl_space_has_equal_params(pa
->dim
, mpa
->space
);
7095 if (equal_params
< 0)
7098 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa
, pa
);
7100 pa
= isl_pw_aff_align_params(pa
, isl_multi_pw_aff_get_space(mpa
));
7101 mpa
= isl_multi_pw_aff_align_params(mpa
, isl_pw_aff_get_space(pa
));
7103 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa
, pa
);
7105 isl_pw_aff_free(pa
);
7106 isl_multi_pw_aff_free(mpa
);
7110 /* Compute the pullback of "pa" by the function represented by "mpa".
7111 * In other words, plug in "mpa" in "pa".
7112 * "pa" and "mpa" are assumed to have been aligned.
7114 * The pullback is computed by applying "pa" to "mpa".
7116 static __isl_give isl_pw_aff
*isl_pw_aff_pullback_multi_pw_aff_aligned(
7117 __isl_take isl_pw_aff
*pa
, __isl_take isl_multi_pw_aff
*mpa
)
7119 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa
, pa
);
7122 /* Compute the pullback of "pa" by the function represented by "mpa".
7123 * In other words, plug in "mpa" in "pa".
7125 * The pullback is computed by applying "pa" to "mpa".
7127 __isl_give isl_pw_aff
*isl_pw_aff_pullback_multi_pw_aff(
7128 __isl_take isl_pw_aff
*pa
, __isl_take isl_multi_pw_aff
*mpa
)
7130 return isl_multi_pw_aff_apply_pw_aff(mpa
, pa
);
7133 /* Compute the pullback of "mpa1" by the function represented by "mpa2".
7134 * In other words, plug in "mpa2" in "mpa1".
7136 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7138 * We pullback each member of "mpa1" in turn.
7140 * If "mpa1" has an explicit domain, then it is this domain
7141 * that needs to undergo a pullback instead, i.e., a preimage.
7143 static __isl_give isl_multi_pw_aff
*
7144 isl_multi_pw_aff_pullback_multi_pw_aff_aligned(
7145 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
)
7148 isl_space
*space
= NULL
;
7150 mpa1
= isl_multi_pw_aff_cow(mpa1
);
7154 space
= isl_space_join(isl_multi_pw_aff_get_space(mpa2
),
7155 isl_multi_pw_aff_get_space(mpa1
));
7157 for (i
= 0; i
< mpa1
->n
; ++i
) {
7158 mpa1
->u
.p
[i
] = isl_pw_aff_pullback_multi_pw_aff_aligned(
7159 mpa1
->u
.p
[i
], isl_multi_pw_aff_copy(mpa2
));
7164 if (isl_multi_pw_aff_has_explicit_domain(mpa1
)) {
7165 mpa1
->u
.dom
= isl_set_preimage_multi_pw_aff(mpa1
->u
.dom
,
7166 isl_multi_pw_aff_copy(mpa2
));
7170 mpa1
= isl_multi_pw_aff_reset_space(mpa1
, space
);
7172 isl_multi_pw_aff_free(mpa2
);
7175 isl_space_free(space
);
7176 isl_multi_pw_aff_free(mpa1
);
7177 isl_multi_pw_aff_free(mpa2
);
7181 /* Compute the pullback of "mpa1" by the function represented by "mpa2".
7182 * In other words, plug in "mpa2" in "mpa1".
7184 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_pullback_multi_pw_aff(
7185 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
)
7187 return isl_multi_pw_aff_align_params_multi_multi_and(mpa1
, mpa2
,
7188 &isl_multi_pw_aff_pullback_multi_pw_aff_aligned
);
7191 /* Align the parameters of "mpa1" and "mpa2", check that the ranges
7192 * of "mpa1" and "mpa2" live in the same space, construct map space
7193 * between the domain spaces of "mpa1" and "mpa2" and call "order"
7194 * with this map space as extract argument.
7196 static __isl_give isl_map
*isl_multi_pw_aff_order_map(
7197 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
,
7198 __isl_give isl_map
*(*order
)(__isl_keep isl_multi_pw_aff
*mpa1
,
7199 __isl_keep isl_multi_pw_aff
*mpa2
, __isl_take isl_space
*space
))
7202 isl_space
*space1
, *space2
;
7205 mpa1
= isl_multi_pw_aff_align_params(mpa1
,
7206 isl_multi_pw_aff_get_space(mpa2
));
7207 mpa2
= isl_multi_pw_aff_align_params(mpa2
,
7208 isl_multi_pw_aff_get_space(mpa1
));
7211 match
= isl_space_tuple_is_equal(mpa1
->space
, isl_dim_out
,
7212 mpa2
->space
, isl_dim_out
);
7216 isl_die(isl_multi_pw_aff_get_ctx(mpa1
), isl_error_invalid
,
7217 "range spaces don't match", goto error
);
7218 space1
= isl_space_domain(isl_multi_pw_aff_get_space(mpa1
));
7219 space2
= isl_space_domain(isl_multi_pw_aff_get_space(mpa2
));
7220 space1
= isl_space_map_from_domain_and_range(space1
, space2
);
7222 res
= order(mpa1
, mpa2
, space1
);
7223 isl_multi_pw_aff_free(mpa1
);
7224 isl_multi_pw_aff_free(mpa2
);
7227 isl_multi_pw_aff_free(mpa1
);
7228 isl_multi_pw_aff_free(mpa2
);
7232 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7233 * where the function values are equal. "space" is the space of the result.
7234 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7236 * "mpa1" and "mpa2" are equal when each of the pairs of elements
7237 * in the sequences are equal.
7239 static __isl_give isl_map
*isl_multi_pw_aff_eq_map_on_space(
7240 __isl_keep isl_multi_pw_aff
*mpa1
, __isl_keep isl_multi_pw_aff
*mpa2
,
7241 __isl_take isl_space
*space
)
7246 res
= isl_map_universe(space
);
7248 n
= isl_multi_pw_aff_dim(mpa1
, isl_dim_out
);
7249 for (i
= 0; i
< n
; ++i
) {
7250 isl_pw_aff
*pa1
, *pa2
;
7253 pa1
= isl_multi_pw_aff_get_pw_aff(mpa1
, i
);
7254 pa2
= isl_multi_pw_aff_get_pw_aff(mpa2
, i
);
7255 map
= isl_pw_aff_eq_map(pa1
, pa2
);
7256 res
= isl_map_intersect(res
, map
);
7262 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7263 * where the function values are equal.
7265 __isl_give isl_map
*isl_multi_pw_aff_eq_map(__isl_take isl_multi_pw_aff
*mpa1
,
7266 __isl_take isl_multi_pw_aff
*mpa2
)
7268 return isl_multi_pw_aff_order_map(mpa1
, mpa2
,
7269 &isl_multi_pw_aff_eq_map_on_space
);
7272 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7273 * where the function values of "mpa1" is lexicographically satisfies "base"
7274 * compared to that of "mpa2". "space" is the space of the result.
7275 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7277 * "mpa1" lexicographically satisfies "base" compared to "mpa2"
7278 * if its i-th element satisfies "base" when compared to
7279 * the i-th element of "mpa2" while all previous elements are
7282 static __isl_give isl_map
*isl_multi_pw_aff_lex_map_on_space(
7283 __isl_keep isl_multi_pw_aff
*mpa1
, __isl_keep isl_multi_pw_aff
*mpa2
,
7284 __isl_give isl_map
*(*base
)(__isl_take isl_pw_aff
*pa1
,
7285 __isl_take isl_pw_aff
*pa2
),
7286 __isl_take isl_space
*space
)
7289 isl_map
*res
, *rest
;
7291 res
= isl_map_empty(isl_space_copy(space
));
7292 rest
= isl_map_universe(space
);
7294 n
= isl_multi_pw_aff_dim(mpa1
, isl_dim_out
);
7295 for (i
= 0; i
< n
; ++i
) {
7296 isl_pw_aff
*pa1
, *pa2
;
7299 pa1
= isl_multi_pw_aff_get_pw_aff(mpa1
, i
);
7300 pa2
= isl_multi_pw_aff_get_pw_aff(mpa2
, i
);
7301 map
= base(pa1
, pa2
);
7302 map
= isl_map_intersect(map
, isl_map_copy(rest
));
7303 res
= isl_map_union(res
, map
);
7308 pa1
= isl_multi_pw_aff_get_pw_aff(mpa1
, i
);
7309 pa2
= isl_multi_pw_aff_get_pw_aff(mpa2
, i
);
7310 map
= isl_pw_aff_eq_map(pa1
, pa2
);
7311 rest
= isl_map_intersect(rest
, map
);
7318 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7319 * where the function value of "mpa1" is lexicographically less than that
7320 * of "mpa2". "space" is the space of the result.
7321 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7323 * "mpa1" is less than "mpa2" if its i-th element is smaller
7324 * than the i-th element of "mpa2" while all previous elements are
7327 __isl_give isl_map
*isl_multi_pw_aff_lex_lt_map_on_space(
7328 __isl_keep isl_multi_pw_aff
*mpa1
, __isl_keep isl_multi_pw_aff
*mpa2
,
7329 __isl_take isl_space
*space
)
7331 return isl_multi_pw_aff_lex_map_on_space(mpa1
, mpa2
,
7332 &isl_pw_aff_lt_map
, space
);
7335 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7336 * where the function value of "mpa1" is lexicographically less than that
7339 __isl_give isl_map
*isl_multi_pw_aff_lex_lt_map(
7340 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
)
7342 return isl_multi_pw_aff_order_map(mpa1
, mpa2
,
7343 &isl_multi_pw_aff_lex_lt_map_on_space
);
7346 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7347 * where the function value of "mpa1" is lexicographically greater than that
7348 * of "mpa2". "space" is the space of the result.
7349 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7351 * "mpa1" is greater than "mpa2" if its i-th element is greater
7352 * than the i-th element of "mpa2" while all previous elements are
7355 __isl_give isl_map
*isl_multi_pw_aff_lex_gt_map_on_space(
7356 __isl_keep isl_multi_pw_aff
*mpa1
, __isl_keep isl_multi_pw_aff
*mpa2
,
7357 __isl_take isl_space
*space
)
7359 return isl_multi_pw_aff_lex_map_on_space(mpa1
, mpa2
,
7360 &isl_pw_aff_gt_map
, space
);
7363 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7364 * where the function value of "mpa1" is lexicographically greater than that
7367 __isl_give isl_map
*isl_multi_pw_aff_lex_gt_map(
7368 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
)
7370 return isl_multi_pw_aff_order_map(mpa1
, mpa2
,
7371 &isl_multi_pw_aff_lex_gt_map_on_space
);
7374 /* Compare two isl_affs.
7376 * Return -1 if "aff1" is "smaller" than "aff2", 1 if "aff1" is "greater"
7377 * than "aff2" and 0 if they are equal.
7379 * The order is fairly arbitrary. We do consider expressions that only involve
7380 * earlier dimensions as "smaller".
7382 int isl_aff_plain_cmp(__isl_keep isl_aff
*aff1
, __isl_keep isl_aff
*aff2
)
7395 cmp
= isl_local_space_cmp(aff1
->ls
, aff2
->ls
);
7399 last1
= isl_seq_last_non_zero(aff1
->v
->el
+ 1, aff1
->v
->size
- 1);
7400 last2
= isl_seq_last_non_zero(aff2
->v
->el
+ 1, aff1
->v
->size
- 1);
7402 return last1
- last2
;
7404 return isl_seq_cmp(aff1
->v
->el
, aff2
->v
->el
, aff1
->v
->size
);
7407 /* Compare two isl_pw_affs.
7409 * Return -1 if "pa1" is "smaller" than "pa2", 1 if "pa1" is "greater"
7410 * than "pa2" and 0 if they are equal.
7412 * The order is fairly arbitrary. We do consider expressions that only involve
7413 * earlier dimensions as "smaller".
7415 int isl_pw_aff_plain_cmp(__isl_keep isl_pw_aff
*pa1
,
7416 __isl_keep isl_pw_aff
*pa2
)
7429 cmp
= isl_space_cmp(pa1
->dim
, pa2
->dim
);
7433 if (pa1
->n
!= pa2
->n
)
7434 return pa1
->n
- pa2
->n
;
7436 for (i
= 0; i
< pa1
->n
; ++i
) {
7437 cmp
= isl_set_plain_cmp(pa1
->p
[i
].set
, pa2
->p
[i
].set
);
7440 cmp
= isl_aff_plain_cmp(pa1
->p
[i
].aff
, pa2
->p
[i
].aff
);
7448 /* Return a piecewise affine expression that is equal to "v" on "domain".
7450 __isl_give isl_pw_aff
*isl_pw_aff_val_on_domain(__isl_take isl_set
*domain
,
7451 __isl_take isl_val
*v
)
7454 isl_local_space
*ls
;
7457 space
= isl_set_get_space(domain
);
7458 ls
= isl_local_space_from_space(space
);
7459 aff
= isl_aff_val_on_domain(ls
, v
);
7461 return isl_pw_aff_alloc(domain
, aff
);
7464 /* Return a multi affine expression that is equal to "mv" on domain
7467 __isl_give isl_multi_aff
*isl_multi_aff_multi_val_on_space(
7468 __isl_take isl_space
*space
, __isl_take isl_multi_val
*mv
)
7472 isl_local_space
*ls
;
7478 n
= isl_multi_val_dim(mv
, isl_dim_set
);
7479 space2
= isl_multi_val_get_space(mv
);
7480 space2
= isl_space_align_params(space2
, isl_space_copy(space
));
7481 space
= isl_space_align_params(space
, isl_space_copy(space2
));
7482 space
= isl_space_map_from_domain_and_range(space
, space2
);
7483 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
7484 ls
= isl_local_space_from_space(isl_space_domain(space
));
7485 for (i
= 0; i
< n
; ++i
) {
7489 v
= isl_multi_val_get_val(mv
, i
);
7490 aff
= isl_aff_val_on_domain(isl_local_space_copy(ls
), v
);
7491 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
7493 isl_local_space_free(ls
);
7495 isl_multi_val_free(mv
);
7498 isl_space_free(space
);
7499 isl_multi_val_free(mv
);
7503 /* Return a piecewise multi-affine expression
7504 * that is equal to "mv" on "domain".
7506 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_multi_val_on_domain(
7507 __isl_take isl_set
*domain
, __isl_take isl_multi_val
*mv
)
7512 space
= isl_set_get_space(domain
);
7513 ma
= isl_multi_aff_multi_val_on_space(space
, mv
);
7515 return isl_pw_multi_aff_alloc(domain
, ma
);
7518 /* Internal data structure for isl_union_pw_multi_aff_multi_val_on_domain.
7519 * mv is the value that should be attained on each domain set
7520 * res collects the results
7522 struct isl_union_pw_multi_aff_multi_val_on_domain_data
{
7524 isl_union_pw_multi_aff
*res
;
7527 /* Create an isl_pw_multi_aff equal to data->mv on "domain"
7528 * and add it to data->res.
7530 static isl_stat
pw_multi_aff_multi_val_on_domain(__isl_take isl_set
*domain
,
7533 struct isl_union_pw_multi_aff_multi_val_on_domain_data
*data
= user
;
7534 isl_pw_multi_aff
*pma
;
7537 mv
= isl_multi_val_copy(data
->mv
);
7538 pma
= isl_pw_multi_aff_multi_val_on_domain(domain
, mv
);
7539 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma
);
7541 return data
->res
? isl_stat_ok
: isl_stat_error
;
7544 /* Return a union piecewise multi-affine expression
7545 * that is equal to "mv" on "domain".
7547 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_multi_val_on_domain(
7548 __isl_take isl_union_set
*domain
, __isl_take isl_multi_val
*mv
)
7550 struct isl_union_pw_multi_aff_multi_val_on_domain_data data
;
7553 space
= isl_union_set_get_space(domain
);
7554 data
.res
= isl_union_pw_multi_aff_empty(space
);
7556 if (isl_union_set_foreach_set(domain
,
7557 &pw_multi_aff_multi_val_on_domain
, &data
) < 0)
7558 data
.res
= isl_union_pw_multi_aff_free(data
.res
);
7559 isl_union_set_free(domain
);
7560 isl_multi_val_free(mv
);
7564 /* Compute the pullback of data->pma by the function represented by "pma2",
7565 * provided the spaces match, and add the results to data->res.
7567 static isl_stat
pullback_entry(__isl_take isl_pw_multi_aff
*pma2
, void *user
)
7569 struct isl_union_pw_multi_aff_bin_data
*data
= user
;
7571 if (!isl_space_tuple_is_equal(data
->pma
->dim
, isl_dim_in
,
7572 pma2
->dim
, isl_dim_out
)) {
7573 isl_pw_multi_aff_free(pma2
);
7577 pma2
= isl_pw_multi_aff_pullback_pw_multi_aff(
7578 isl_pw_multi_aff_copy(data
->pma
), pma2
);
7580 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma2
);
7582 return isl_stat_error
;
7587 /* Compute the pullback of "upma1" by the function represented by "upma2".
7589 __isl_give isl_union_pw_multi_aff
*
7590 isl_union_pw_multi_aff_pullback_union_pw_multi_aff(
7591 __isl_take isl_union_pw_multi_aff
*upma1
,
7592 __isl_take isl_union_pw_multi_aff
*upma2
)
7594 return bin_op(upma1
, upma2
, &pullback_entry
);
7597 /* Check that the domain space of "upa" matches "space".
7599 * This function is called from isl_multi_union_pw_aff_set_union_pw_aff and
7600 * can in principle never fail since the space "space" is that
7601 * of the isl_multi_union_pw_aff and is a set space such that
7602 * there is no domain space to match.
7604 * We check the parameters and double-check that "space" is
7605 * indeed that of a set.
7607 static isl_stat
isl_union_pw_aff_check_match_domain_space(
7608 __isl_keep isl_union_pw_aff
*upa
, __isl_keep isl_space
*space
)
7610 isl_space
*upa_space
;
7614 return isl_stat_error
;
7616 match
= isl_space_is_set(space
);
7618 return isl_stat_error
;
7620 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
7621 "expecting set space", return -1);
7623 upa_space
= isl_union_pw_aff_get_space(upa
);
7624 match
= isl_space_has_equal_params(space
, upa_space
);
7628 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
7629 "parameters don't match", goto error
);
7631 isl_space_free(upa_space
);
7634 isl_space_free(upa_space
);
7635 return isl_stat_error
;
7638 /* Do the parameters of "upa" match those of "space"?
7640 static isl_bool
isl_union_pw_aff_matching_params(
7641 __isl_keep isl_union_pw_aff
*upa
, __isl_keep isl_space
*space
)
7643 isl_space
*upa_space
;
7647 return isl_bool_error
;
7649 upa_space
= isl_union_pw_aff_get_space(upa
);
7651 match
= isl_space_has_equal_params(space
, upa_space
);
7653 isl_space_free(upa_space
);
7657 /* Internal data structure for isl_union_pw_aff_reset_domain_space.
7658 * space represents the new parameters.
7659 * res collects the results.
7661 struct isl_union_pw_aff_reset_params_data
{
7663 isl_union_pw_aff
*res
;
7666 /* Replace the parameters of "pa" by data->space and
7667 * add the result to data->res.
7669 static isl_stat
reset_params(__isl_take isl_pw_aff
*pa
, void *user
)
7671 struct isl_union_pw_aff_reset_params_data
*data
= user
;
7674 space
= isl_pw_aff_get_space(pa
);
7675 space
= isl_space_replace_params(space
, data
->space
);
7676 pa
= isl_pw_aff_reset_space(pa
, space
);
7677 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
7679 return data
->res
? isl_stat_ok
: isl_stat_error
;
7682 /* Replace the domain space of "upa" by "space".
7683 * Since a union expression does not have a (single) domain space,
7684 * "space" is necessarily a parameter space.
7686 * Since the order and the names of the parameters determine
7687 * the hash value, we need to create a new hash table.
7689 static __isl_give isl_union_pw_aff
*isl_union_pw_aff_reset_domain_space(
7690 __isl_take isl_union_pw_aff
*upa
, __isl_take isl_space
*space
)
7692 struct isl_union_pw_aff_reset_params_data data
= { space
};
7695 match
= isl_union_pw_aff_matching_params(upa
, space
);
7697 upa
= isl_union_pw_aff_free(upa
);
7699 isl_space_free(space
);
7703 data
.res
= isl_union_pw_aff_empty(isl_space_copy(space
));
7704 if (isl_union_pw_aff_foreach_pw_aff(upa
, &reset_params
, &data
) < 0)
7705 data
.res
= isl_union_pw_aff_free(data
.res
);
7707 isl_union_pw_aff_free(upa
);
7708 isl_space_free(space
);
7712 /* Return the floor of "pa".
7714 static __isl_give isl_pw_aff
*floor_entry(__isl_take isl_pw_aff
*pa
, void *user
)
7716 return isl_pw_aff_floor(pa
);
7719 /* Given f, return floor(f).
7721 __isl_give isl_union_pw_aff
*isl_union_pw_aff_floor(
7722 __isl_take isl_union_pw_aff
*upa
)
7724 return isl_union_pw_aff_transform_inplace(upa
, &floor_entry
, NULL
);
7729 * upa mod m = upa - m * floor(upa/m)
7731 * with m an integer value.
7733 __isl_give isl_union_pw_aff
*isl_union_pw_aff_mod_val(
7734 __isl_take isl_union_pw_aff
*upa
, __isl_take isl_val
*m
)
7736 isl_union_pw_aff
*res
;
7741 if (!isl_val_is_int(m
))
7742 isl_die(isl_val_get_ctx(m
), isl_error_invalid
,
7743 "expecting integer modulo", goto error
);
7744 if (!isl_val_is_pos(m
))
7745 isl_die(isl_val_get_ctx(m
), isl_error_invalid
,
7746 "expecting positive modulo", goto error
);
7748 res
= isl_union_pw_aff_copy(upa
);
7749 upa
= isl_union_pw_aff_scale_down_val(upa
, isl_val_copy(m
));
7750 upa
= isl_union_pw_aff_floor(upa
);
7751 upa
= isl_union_pw_aff_scale_val(upa
, m
);
7752 res
= isl_union_pw_aff_sub(res
, upa
);
7757 isl_union_pw_aff_free(upa
);
7761 /* Internal data structure for isl_union_pw_multi_aff_get_union_pw_aff.
7762 * pos is the output position that needs to be extracted.
7763 * res collects the results.
7765 struct isl_union_pw_multi_aff_get_union_pw_aff_data
{
7767 isl_union_pw_aff
*res
;
7770 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma"
7771 * (assuming it has such a dimension) and add it to data->res.
7773 static isl_stat
get_union_pw_aff(__isl_take isl_pw_multi_aff
*pma
, void *user
)
7775 struct isl_union_pw_multi_aff_get_union_pw_aff_data
*data
= user
;
7780 return isl_stat_error
;
7782 n_out
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
7783 if (data
->pos
>= n_out
) {
7784 isl_pw_multi_aff_free(pma
);
7788 pa
= isl_pw_multi_aff_get_pw_aff(pma
, data
->pos
);
7789 isl_pw_multi_aff_free(pma
);
7791 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
7793 return data
->res
? isl_stat_ok
: isl_stat_error
;
7796 /* Extract an isl_union_pw_aff corresponding to
7797 * output dimension "pos" of "upma".
7799 __isl_give isl_union_pw_aff
*isl_union_pw_multi_aff_get_union_pw_aff(
7800 __isl_keep isl_union_pw_multi_aff
*upma
, int pos
)
7802 struct isl_union_pw_multi_aff_get_union_pw_aff_data data
;
7809 isl_die(isl_union_pw_multi_aff_get_ctx(upma
), isl_error_invalid
,
7810 "cannot extract at negative position", return NULL
);
7812 space
= isl_union_pw_multi_aff_get_space(upma
);
7813 data
.res
= isl_union_pw_aff_empty(space
);
7815 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma
,
7816 &get_union_pw_aff
, &data
) < 0)
7817 data
.res
= isl_union_pw_aff_free(data
.res
);
7822 /* Return a union piecewise affine expression
7823 * that is equal to "aff" on "domain".
7825 __isl_give isl_union_pw_aff
*isl_union_pw_aff_aff_on_domain(
7826 __isl_take isl_union_set
*domain
, __isl_take isl_aff
*aff
)
7830 pa
= isl_pw_aff_from_aff(aff
);
7831 return isl_union_pw_aff_pw_aff_on_domain(domain
, pa
);
7834 /* Return a union piecewise affine expression
7835 * that is equal to the parameter identified by "id" on "domain".
7837 * Make sure the parameter appears in the space passed to
7838 * isl_aff_param_on_domain_space_id.
7840 __isl_give isl_union_pw_aff
*isl_union_pw_aff_param_on_domain_id(
7841 __isl_take isl_union_set
*domain
, __isl_take isl_id
*id
)
7846 space
= isl_union_set_get_space(domain
);
7847 space
= isl_space_add_param_id(space
, isl_id_copy(id
));
7848 aff
= isl_aff_param_on_domain_space_id(space
, id
);
7849 return isl_union_pw_aff_aff_on_domain(domain
, aff
);
7852 /* Internal data structure for isl_union_pw_aff_pw_aff_on_domain.
7853 * "pa" is the piecewise symbolic value that the resulting isl_union_pw_aff
7855 * "res" collects the results.
7857 struct isl_union_pw_aff_pw_aff_on_domain_data
{
7859 isl_union_pw_aff
*res
;
7862 /* Construct a piecewise affine expression that is equal to data->pa
7863 * on "domain" and add the result to data->res.
7865 static isl_stat
pw_aff_on_domain(__isl_take isl_set
*domain
, void *user
)
7867 struct isl_union_pw_aff_pw_aff_on_domain_data
*data
= user
;
7871 pa
= isl_pw_aff_copy(data
->pa
);
7872 dim
= isl_set_dim(domain
, isl_dim_set
);
7873 pa
= isl_pw_aff_from_range(pa
);
7874 pa
= isl_pw_aff_add_dims(pa
, isl_dim_in
, dim
);
7875 pa
= isl_pw_aff_reset_domain_space(pa
, isl_set_get_space(domain
));
7876 pa
= isl_pw_aff_intersect_domain(pa
, domain
);
7877 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
7879 return data
->res
? isl_stat_ok
: isl_stat_error
;
7882 /* Return a union piecewise affine expression
7883 * that is equal to "pa" on "domain", assuming "domain" and "pa"
7884 * have been aligned.
7886 * Construct an isl_pw_aff on each of the sets in "domain" and
7887 * collect the results.
7889 static __isl_give isl_union_pw_aff
*isl_union_pw_aff_pw_aff_on_domain_aligned(
7890 __isl_take isl_union_set
*domain
, __isl_take isl_pw_aff
*pa
)
7892 struct isl_union_pw_aff_pw_aff_on_domain_data data
;
7895 space
= isl_union_set_get_space(domain
);
7896 data
.res
= isl_union_pw_aff_empty(space
);
7898 if (isl_union_set_foreach_set(domain
, &pw_aff_on_domain
, &data
) < 0)
7899 data
.res
= isl_union_pw_aff_free(data
.res
);
7900 isl_union_set_free(domain
);
7901 isl_pw_aff_free(pa
);
7905 /* Return a union piecewise affine expression
7906 * that is equal to "pa" on "domain".
7908 * Check that "pa" is a parametric expression,
7909 * align the parameters if needed and call
7910 * isl_union_pw_aff_pw_aff_on_domain_aligned.
7912 __isl_give isl_union_pw_aff
*isl_union_pw_aff_pw_aff_on_domain(
7913 __isl_take isl_union_set
*domain
, __isl_take isl_pw_aff
*pa
)
7916 isl_bool equal_params
;
7917 isl_space
*domain_space
, *pa_space
;
7919 pa_space
= isl_pw_aff_peek_space(pa
);
7920 is_set
= isl_space_is_set(pa_space
);
7924 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
7925 "expecting parametric expression", goto error
);
7927 domain_space
= isl_union_set_get_space(domain
);
7928 pa_space
= isl_pw_aff_get_space(pa
);
7929 equal_params
= isl_space_has_equal_params(domain_space
, pa_space
);
7930 if (equal_params
>= 0 && !equal_params
) {
7933 space
= isl_space_align_params(domain_space
, pa_space
);
7934 pa
= isl_pw_aff_align_params(pa
, isl_space_copy(space
));
7935 domain
= isl_union_set_align_params(domain
, space
);
7937 isl_space_free(domain_space
);
7938 isl_space_free(pa_space
);
7941 if (equal_params
< 0)
7943 return isl_union_pw_aff_pw_aff_on_domain_aligned(domain
, pa
);
7945 isl_union_set_free(domain
);
7946 isl_pw_aff_free(pa
);
7950 /* Internal data structure for isl_union_pw_aff_val_on_domain.
7951 * "v" is the value that the resulting isl_union_pw_aff needs to attain.
7952 * "res" collects the results.
7954 struct isl_union_pw_aff_val_on_domain_data
{
7956 isl_union_pw_aff
*res
;
7959 /* Construct a piecewise affine expression that is equal to data->v
7960 * on "domain" and add the result to data->res.
7962 static isl_stat
pw_aff_val_on_domain(__isl_take isl_set
*domain
, void *user
)
7964 struct isl_union_pw_aff_val_on_domain_data
*data
= user
;
7968 v
= isl_val_copy(data
->v
);
7969 pa
= isl_pw_aff_val_on_domain(domain
, v
);
7970 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
7972 return data
->res
? isl_stat_ok
: isl_stat_error
;
7975 /* Return a union piecewise affine expression
7976 * that is equal to "v" on "domain".
7978 * Construct an isl_pw_aff on each of the sets in "domain" and
7979 * collect the results.
7981 __isl_give isl_union_pw_aff
*isl_union_pw_aff_val_on_domain(
7982 __isl_take isl_union_set
*domain
, __isl_take isl_val
*v
)
7984 struct isl_union_pw_aff_val_on_domain_data data
;
7987 space
= isl_union_set_get_space(domain
);
7988 data
.res
= isl_union_pw_aff_empty(space
);
7990 if (isl_union_set_foreach_set(domain
, &pw_aff_val_on_domain
, &data
) < 0)
7991 data
.res
= isl_union_pw_aff_free(data
.res
);
7992 isl_union_set_free(domain
);
7997 /* Construct a piecewise multi affine expression
7998 * that is equal to "pa" and add it to upma.
8000 static isl_stat
pw_multi_aff_from_pw_aff_entry(__isl_take isl_pw_aff
*pa
,
8003 isl_union_pw_multi_aff
**upma
= user
;
8004 isl_pw_multi_aff
*pma
;
8006 pma
= isl_pw_multi_aff_from_pw_aff(pa
);
8007 *upma
= isl_union_pw_multi_aff_add_pw_multi_aff(*upma
, pma
);
8009 return *upma
? isl_stat_ok
: isl_stat_error
;
8012 /* Construct and return a union piecewise multi affine expression
8013 * that is equal to the given union piecewise affine expression.
8015 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_union_pw_aff(
8016 __isl_take isl_union_pw_aff
*upa
)
8019 isl_union_pw_multi_aff
*upma
;
8024 space
= isl_union_pw_aff_get_space(upa
);
8025 upma
= isl_union_pw_multi_aff_empty(space
);
8027 if (isl_union_pw_aff_foreach_pw_aff(upa
,
8028 &pw_multi_aff_from_pw_aff_entry
, &upma
) < 0)
8029 upma
= isl_union_pw_multi_aff_free(upma
);
8031 isl_union_pw_aff_free(upa
);
8035 /* Compute the set of elements in the domain of "pa" where it is zero and
8036 * add this set to "uset".
8038 static isl_stat
zero_union_set(__isl_take isl_pw_aff
*pa
, void *user
)
8040 isl_union_set
**uset
= (isl_union_set
**)user
;
8042 *uset
= isl_union_set_add_set(*uset
, isl_pw_aff_zero_set(pa
));
8044 return *uset
? isl_stat_ok
: isl_stat_error
;
8047 /* Return a union set containing those elements in the domain
8048 * of "upa" where it is zero.
8050 __isl_give isl_union_set
*isl_union_pw_aff_zero_union_set(
8051 __isl_take isl_union_pw_aff
*upa
)
8053 isl_union_set
*zero
;
8055 zero
= isl_union_set_empty(isl_union_pw_aff_get_space(upa
));
8056 if (isl_union_pw_aff_foreach_pw_aff(upa
, &zero_union_set
, &zero
) < 0)
8057 zero
= isl_union_set_free(zero
);
8059 isl_union_pw_aff_free(upa
);
8063 /* Convert "pa" to an isl_map and add it to *umap.
8065 static isl_stat
map_from_pw_aff_entry(__isl_take isl_pw_aff
*pa
, void *user
)
8067 isl_union_map
**umap
= user
;
8070 map
= isl_map_from_pw_aff(pa
);
8071 *umap
= isl_union_map_add_map(*umap
, map
);
8073 return *umap
? isl_stat_ok
: isl_stat_error
;
8076 /* Construct a union map mapping the domain of the union
8077 * piecewise affine expression to its range, with the single output dimension
8078 * equated to the corresponding affine expressions on their cells.
8080 __isl_give isl_union_map
*isl_union_map_from_union_pw_aff(
8081 __isl_take isl_union_pw_aff
*upa
)
8084 isl_union_map
*umap
;
8089 space
= isl_union_pw_aff_get_space(upa
);
8090 umap
= isl_union_map_empty(space
);
8092 if (isl_union_pw_aff_foreach_pw_aff(upa
, &map_from_pw_aff_entry
,
8094 umap
= isl_union_map_free(umap
);
8096 isl_union_pw_aff_free(upa
);
8100 /* Internal data structure for isl_union_pw_aff_pullback_union_pw_multi_aff.
8101 * upma is the function that is plugged in.
8102 * pa is the current part of the function in which upma is plugged in.
8103 * res collects the results.
8105 struct isl_union_pw_aff_pullback_upma_data
{
8106 isl_union_pw_multi_aff
*upma
;
8108 isl_union_pw_aff
*res
;
8111 /* Check if "pma" can be plugged into data->pa.
8112 * If so, perform the pullback and add the result to data->res.
8114 static isl_stat
pa_pb_pma(__isl_take isl_pw_multi_aff
*pma
, void *user
)
8116 struct isl_union_pw_aff_pullback_upma_data
*data
= user
;
8119 if (!isl_space_tuple_is_equal(data
->pa
->dim
, isl_dim_in
,
8120 pma
->dim
, isl_dim_out
)) {
8121 isl_pw_multi_aff_free(pma
);
8125 pa
= isl_pw_aff_copy(data
->pa
);
8126 pa
= isl_pw_aff_pullback_pw_multi_aff(pa
, pma
);
8128 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
8130 return data
->res
? isl_stat_ok
: isl_stat_error
;
8133 /* Check if any of the elements of data->upma can be plugged into pa,
8134 * add if so add the result to data->res.
8136 static isl_stat
upa_pb_upma(__isl_take isl_pw_aff
*pa
, void *user
)
8138 struct isl_union_pw_aff_pullback_upma_data
*data
= user
;
8142 r
= isl_union_pw_multi_aff_foreach_pw_multi_aff(data
->upma
,
8144 isl_pw_aff_free(pa
);
8149 /* Compute the pullback of "upa" by the function represented by "upma".
8150 * In other words, plug in "upma" in "upa". The result contains
8151 * expressions defined over the domain space of "upma".
8153 * Run over all pairs of elements in "upa" and "upma", perform
8154 * the pullback when appropriate and collect the results.
8155 * If the hash value were based on the domain space rather than
8156 * the function space, then we could run through all elements
8157 * of "upma" and directly pick out the corresponding element of "upa".
8159 __isl_give isl_union_pw_aff
*isl_union_pw_aff_pullback_union_pw_multi_aff(
8160 __isl_take isl_union_pw_aff
*upa
,
8161 __isl_take isl_union_pw_multi_aff
*upma
)
8163 struct isl_union_pw_aff_pullback_upma_data data
= { NULL
, NULL
};
8166 space
= isl_union_pw_multi_aff_get_space(upma
);
8167 upa
= isl_union_pw_aff_align_params(upa
, space
);
8168 space
= isl_union_pw_aff_get_space(upa
);
8169 upma
= isl_union_pw_multi_aff_align_params(upma
, space
);
8175 data
.res
= isl_union_pw_aff_alloc_same_size(upa
);
8176 if (isl_union_pw_aff_foreach_pw_aff(upa
, &upa_pb_upma
, &data
) < 0)
8177 data
.res
= isl_union_pw_aff_free(data
.res
);
8179 isl_union_pw_aff_free(upa
);
8180 isl_union_pw_multi_aff_free(upma
);
8183 isl_union_pw_aff_free(upa
);
8184 isl_union_pw_multi_aff_free(upma
);
8189 #define BASE union_pw_aff
8191 #define DOMBASE union_set
8193 #define NO_MOVE_DIMS
8200 #include <isl_multi_explicit_domain.c>
8201 #include <isl_multi_union_pw_aff_explicit_domain.c>
8202 #include <isl_multi_templ.c>
8203 #include <isl_multi_apply_set.c>
8204 #include <isl_multi_apply_union_set.c>
8205 #include <isl_multi_coalesce.c>
8206 #include <isl_multi_floor.c>
8207 #include <isl_multi_gist.c>
8208 #include <isl_multi_align_set.c>
8209 #include <isl_multi_align_union_set.c>
8210 #include <isl_multi_intersect.c>
8212 /* Does "mupa" have a non-trivial explicit domain?
8214 * The explicit domain, if present, is trivial if it represents
8215 * an (obviously) universe parameter set.
8217 isl_bool
isl_multi_union_pw_aff_has_non_trivial_domain(
8218 __isl_keep isl_multi_union_pw_aff
*mupa
)
8220 isl_bool is_params
, trivial
;
8224 return isl_bool_error
;
8225 if (!isl_multi_union_pw_aff_has_explicit_domain(mupa
))
8226 return isl_bool_false
;
8227 is_params
= isl_union_set_is_params(mupa
->u
.dom
);
8228 if (is_params
< 0 || !is_params
)
8229 return isl_bool_not(is_params
);
8230 set
= isl_set_from_union_set(isl_union_set_copy(mupa
->u
.dom
));
8231 trivial
= isl_set_plain_is_universe(set
);
8233 return isl_bool_not(trivial
);
8236 /* Construct a multiple union piecewise affine expression
8237 * in the given space with value zero in each of the output dimensions.
8239 * Since there is no canonical zero value for
8240 * a union piecewise affine expression, we can only construct
8241 * a zero-dimensional "zero" value.
8243 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_zero(
8244 __isl_take isl_space
*space
)
8251 params
= isl_space_is_params(space
);
8255 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
8256 "expecting proper set space", goto error
);
8257 if (!isl_space_is_set(space
))
8258 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
8259 "expecting set space", goto error
);
8260 if (isl_space_dim(space
, isl_dim_out
) != 0)
8261 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
8262 "expecting 0D space", goto error
);
8264 return isl_multi_union_pw_aff_alloc(space
);
8266 isl_space_free(space
);
8270 /* Compute the sum of "mupa1" and "mupa2" on the union of their domains,
8271 * with the actual sum on the shared domain and
8272 * the defined expression on the symmetric difference of the domains.
8274 * We simply iterate over the elements in both arguments and
8275 * call isl_union_pw_aff_union_add on each of them, if there is
8276 * at least one element.
8278 * Otherwise, the two expressions have an explicit domain and
8279 * the union of these explicit domains is computed.
8280 * This assumes that the explicit domains are either both in terms
8281 * of specific domains elements or both in terms of parameters.
8282 * However, if one of the expressions does not have any constraints
8283 * on its explicit domain, then this is allowed as well and the result
8284 * is the expression with no constraints on its explicit domain.
8286 static __isl_give isl_multi_union_pw_aff
*
8287 isl_multi_union_pw_aff_union_add_aligned(
8288 __isl_take isl_multi_union_pw_aff
*mupa1
,
8289 __isl_take isl_multi_union_pw_aff
*mupa2
)
8291 isl_bool has_domain
, is_params1
, is_params2
;
8293 if (isl_multi_union_pw_aff_check_equal_space(mupa1
, mupa2
) < 0)
8296 return isl_multi_union_pw_aff_bin_op(mupa1
, mupa2
,
8297 &isl_union_pw_aff_union_add
);
8298 if (isl_multi_union_pw_aff_check_has_explicit_domain(mupa1
) < 0 ||
8299 isl_multi_union_pw_aff_check_has_explicit_domain(mupa2
) < 0)
8302 has_domain
= isl_multi_union_pw_aff_has_non_trivial_domain(mupa1
);
8306 isl_multi_union_pw_aff_free(mupa2
);
8309 has_domain
= isl_multi_union_pw_aff_has_non_trivial_domain(mupa2
);
8313 isl_multi_union_pw_aff_free(mupa1
);
8317 is_params1
= isl_union_set_is_params(mupa1
->u
.dom
);
8318 is_params2
= isl_union_set_is_params(mupa2
->u
.dom
);
8319 if (is_params1
< 0 || is_params2
< 0)
8321 if (is_params1
!= is_params2
)
8322 isl_die(isl_multi_union_pw_aff_get_ctx(mupa1
),
8324 "cannot compute union of concrete domain and "
8325 "parameter constraints", goto error
);
8326 mupa1
= isl_multi_union_pw_aff_cow(mupa1
);
8329 mupa1
->u
.dom
= isl_union_set_union(mupa1
->u
.dom
,
8330 isl_union_set_copy(mupa2
->u
.dom
));
8333 isl_multi_union_pw_aff_free(mupa2
);
8336 isl_multi_union_pw_aff_free(mupa1
);
8337 isl_multi_union_pw_aff_free(mupa2
);
8341 /* Compute the sum of "mupa1" and "mupa2" on the union of their domains,
8342 * with the actual sum on the shared domain and
8343 * the defined expression on the symmetric difference of the domains.
8345 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_union_add(
8346 __isl_take isl_multi_union_pw_aff
*mupa1
,
8347 __isl_take isl_multi_union_pw_aff
*mupa2
)
8349 return isl_multi_union_pw_aff_align_params_multi_multi_and(mupa1
, mupa2
,
8350 &isl_multi_union_pw_aff_union_add_aligned
);
8353 /* Construct and return a multi union piecewise affine expression
8354 * that is equal to the given multi affine expression.
8356 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_from_multi_aff(
8357 __isl_take isl_multi_aff
*ma
)
8359 isl_multi_pw_aff
*mpa
;
8361 mpa
= isl_multi_pw_aff_from_multi_aff(ma
);
8362 return isl_multi_union_pw_aff_from_multi_pw_aff(mpa
);
8365 /* Construct and return a multi union piecewise affine expression
8366 * that is equal to the given multi piecewise affine expression.
8368 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_from_multi_pw_aff(
8369 __isl_take isl_multi_pw_aff
*mpa
)
8373 isl_multi_union_pw_aff
*mupa
;
8378 space
= isl_multi_pw_aff_get_space(mpa
);
8379 space
= isl_space_range(space
);
8380 mupa
= isl_multi_union_pw_aff_alloc(space
);
8382 n
= isl_multi_pw_aff_dim(mpa
, isl_dim_out
);
8383 for (i
= 0; i
< n
; ++i
) {
8385 isl_union_pw_aff
*upa
;
8387 pa
= isl_multi_pw_aff_get_pw_aff(mpa
, i
);
8388 upa
= isl_union_pw_aff_from_pw_aff(pa
);
8389 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
8392 isl_multi_pw_aff_free(mpa
);
8397 /* Extract the range space of "pma" and assign it to *space.
8398 * If *space has already been set (through a previous call to this function),
8399 * then check that the range space is the same.
8401 static isl_stat
extract_space(__isl_take isl_pw_multi_aff
*pma
, void *user
)
8403 isl_space
**space
= user
;
8404 isl_space
*pma_space
;
8407 pma_space
= isl_space_range(isl_pw_multi_aff_get_space(pma
));
8408 isl_pw_multi_aff_free(pma
);
8411 return isl_stat_error
;
8417 equal
= isl_space_is_equal(pma_space
, *space
);
8418 isl_space_free(pma_space
);
8421 return isl_stat_error
;
8423 isl_die(isl_space_get_ctx(*space
), isl_error_invalid
,
8424 "range spaces not the same", return isl_stat_error
);
8428 /* Construct and return a multi union piecewise affine expression
8429 * that is equal to the given union piecewise multi affine expression.
8431 * In order to be able to perform the conversion, the input
8432 * needs to be non-empty and may only involve a single range space.
8434 * If the resulting multi union piecewise affine expression has
8435 * an explicit domain, then assign it the domain of the input.
8436 * In other cases, the domain is stored in the individual elements.
8438 __isl_give isl_multi_union_pw_aff
*
8439 isl_multi_union_pw_aff_from_union_pw_multi_aff(
8440 __isl_take isl_union_pw_multi_aff
*upma
)
8442 isl_space
*space
= NULL
;
8443 isl_multi_union_pw_aff
*mupa
;
8448 if (isl_union_pw_multi_aff_n_pw_multi_aff(upma
) == 0)
8449 isl_die(isl_union_pw_multi_aff_get_ctx(upma
), isl_error_invalid
,
8450 "cannot extract range space from empty input",
8452 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma
, &extract_space
,
8459 n
= isl_space_dim(space
, isl_dim_set
);
8460 mupa
= isl_multi_union_pw_aff_alloc(space
);
8462 for (i
= 0; i
< n
; ++i
) {
8463 isl_union_pw_aff
*upa
;
8465 upa
= isl_union_pw_multi_aff_get_union_pw_aff(upma
, i
);
8466 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
8468 if (isl_multi_union_pw_aff_has_explicit_domain(mupa
)) {
8470 isl_union_pw_multi_aff
*copy
;
8472 copy
= isl_union_pw_multi_aff_copy(upma
);
8473 dom
= isl_union_pw_multi_aff_domain(copy
);
8474 mupa
= isl_multi_union_pw_aff_intersect_domain(mupa
, dom
);
8477 isl_union_pw_multi_aff_free(upma
);
8480 isl_space_free(space
);
8481 isl_union_pw_multi_aff_free(upma
);
8485 /* Try and create an isl_multi_union_pw_aff that is equivalent
8486 * to the given isl_union_map.
8487 * The isl_union_map is required to be single-valued in each space.
8488 * Moreover, it cannot be empty and all range spaces need to be the same.
8489 * Otherwise, an error is produced.
8491 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_from_union_map(
8492 __isl_take isl_union_map
*umap
)
8494 isl_union_pw_multi_aff
*upma
;
8496 upma
= isl_union_pw_multi_aff_from_union_map(umap
);
8497 return isl_multi_union_pw_aff_from_union_pw_multi_aff(upma
);
8500 /* Return a multiple union piecewise affine expression
8501 * that is equal to "mv" on "domain", assuming "domain" and "mv"
8502 * have been aligned.
8504 * If the resulting multi union piecewise affine expression has
8505 * an explicit domain, then assign it the input domain.
8506 * In other cases, the domain is stored in the individual elements.
8508 static __isl_give isl_multi_union_pw_aff
*
8509 isl_multi_union_pw_aff_multi_val_on_domain_aligned(
8510 __isl_take isl_union_set
*domain
, __isl_take isl_multi_val
*mv
)
8514 isl_multi_union_pw_aff
*mupa
;
8519 n
= isl_multi_val_dim(mv
, isl_dim_set
);
8520 space
= isl_multi_val_get_space(mv
);
8521 mupa
= isl_multi_union_pw_aff_alloc(space
);
8522 for (i
= 0; i
< n
; ++i
) {
8524 isl_union_pw_aff
*upa
;
8526 v
= isl_multi_val_get_val(mv
, i
);
8527 upa
= isl_union_pw_aff_val_on_domain(isl_union_set_copy(domain
),
8529 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
8531 if (isl_multi_union_pw_aff_has_explicit_domain(mupa
))
8532 mupa
= isl_multi_union_pw_aff_intersect_domain(mupa
,
8533 isl_union_set_copy(domain
));
8535 isl_union_set_free(domain
);
8536 isl_multi_val_free(mv
);
8539 isl_union_set_free(domain
);
8540 isl_multi_val_free(mv
);
8544 /* Return a multiple union piecewise affine expression
8545 * that is equal to "mv" on "domain".
8547 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_multi_val_on_domain(
8548 __isl_take isl_union_set
*domain
, __isl_take isl_multi_val
*mv
)
8550 isl_bool equal_params
;
8554 equal_params
= isl_space_has_equal_params(domain
->dim
, mv
->space
);
8555 if (equal_params
< 0)
8558 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(
8560 domain
= isl_union_set_align_params(domain
,
8561 isl_multi_val_get_space(mv
));
8562 mv
= isl_multi_val_align_params(mv
, isl_union_set_get_space(domain
));
8563 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(domain
, mv
);
8565 isl_union_set_free(domain
);
8566 isl_multi_val_free(mv
);
8570 /* Return a multiple union piecewise affine expression
8571 * that is equal to "ma" on "domain".
8573 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_multi_aff_on_domain(
8574 __isl_take isl_union_set
*domain
, __isl_take isl_multi_aff
*ma
)
8576 isl_pw_multi_aff
*pma
;
8578 pma
= isl_pw_multi_aff_from_multi_aff(ma
);
8579 return isl_multi_union_pw_aff_pw_multi_aff_on_domain(domain
, pma
);
8582 /* Return a multiple union piecewise affine expression
8583 * that is equal to "pma" on "domain", assuming "domain" and "pma"
8584 * have been aligned.
8586 * If the resulting multi union piecewise affine expression has
8587 * an explicit domain, then assign it the input domain.
8588 * In other cases, the domain is stored in the individual elements.
8590 static __isl_give isl_multi_union_pw_aff
*
8591 isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(
8592 __isl_take isl_union_set
*domain
, __isl_take isl_pw_multi_aff
*pma
)
8596 isl_multi_union_pw_aff
*mupa
;
8598 if (!domain
|| !pma
)
8601 n
= isl_pw_multi_aff_dim(pma
, isl_dim_set
);
8602 space
= isl_pw_multi_aff_get_space(pma
);
8603 mupa
= isl_multi_union_pw_aff_alloc(space
);
8604 for (i
= 0; i
< n
; ++i
) {
8606 isl_union_pw_aff
*upa
;
8608 pa
= isl_pw_multi_aff_get_pw_aff(pma
, i
);
8609 upa
= isl_union_pw_aff_pw_aff_on_domain(
8610 isl_union_set_copy(domain
), pa
);
8611 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
8613 if (isl_multi_union_pw_aff_has_explicit_domain(mupa
))
8614 mupa
= isl_multi_union_pw_aff_intersect_domain(mupa
,
8615 isl_union_set_copy(domain
));
8617 isl_union_set_free(domain
);
8618 isl_pw_multi_aff_free(pma
);
8621 isl_union_set_free(domain
);
8622 isl_pw_multi_aff_free(pma
);
8626 /* Return a multiple union piecewise affine expression
8627 * that is equal to "pma" on "domain".
8629 __isl_give isl_multi_union_pw_aff
*
8630 isl_multi_union_pw_aff_pw_multi_aff_on_domain(__isl_take isl_union_set
*domain
,
8631 __isl_take isl_pw_multi_aff
*pma
)
8633 isl_bool equal_params
;
8636 space
= isl_pw_multi_aff_peek_space(pma
);
8637 equal_params
= isl_union_set_space_has_equal_params(domain
, space
);
8638 if (equal_params
< 0)
8641 return isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(
8643 domain
= isl_union_set_align_params(domain
,
8644 isl_pw_multi_aff_get_space(pma
));
8645 pma
= isl_pw_multi_aff_align_params(pma
,
8646 isl_union_set_get_space(domain
));
8647 return isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(domain
,
8650 isl_union_set_free(domain
);
8651 isl_pw_multi_aff_free(pma
);
8655 /* Return a union set containing those elements in the domains
8656 * of the elements of "mupa" where they are all zero.
8658 * If there are no elements, then simply return the entire domain.
8660 __isl_give isl_union_set
*isl_multi_union_pw_aff_zero_union_set(
8661 __isl_take isl_multi_union_pw_aff
*mupa
)
8664 isl_union_pw_aff
*upa
;
8665 isl_union_set
*zero
;
8670 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8672 return isl_multi_union_pw_aff_domain(mupa
);
8674 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, 0);
8675 zero
= isl_union_pw_aff_zero_union_set(upa
);
8677 for (i
= 1; i
< n
; ++i
) {
8678 isl_union_set
*zero_i
;
8680 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
8681 zero_i
= isl_union_pw_aff_zero_union_set(upa
);
8683 zero
= isl_union_set_intersect(zero
, zero_i
);
8686 isl_multi_union_pw_aff_free(mupa
);
8690 /* Construct a union map mapping the shared domain
8691 * of the union piecewise affine expressions to the range of "mupa"
8692 * in the special case of a 0D multi union piecewise affine expression.
8694 * Construct a map between the explicit domain of "mupa" and
8696 * Note that this assumes that the domain consists of explicit elements.
8698 static __isl_give isl_union_map
*isl_union_map_from_multi_union_pw_aff_0D(
8699 __isl_take isl_multi_union_pw_aff
*mupa
)
8703 isl_union_set
*dom
, *ran
;
8705 space
= isl_multi_union_pw_aff_get_space(mupa
);
8706 dom
= isl_multi_union_pw_aff_domain(mupa
);
8707 ran
= isl_union_set_from_set(isl_set_universe(space
));
8709 is_params
= isl_union_set_is_params(dom
);
8711 dom
= isl_union_set_free(dom
);
8713 isl_die(isl_union_set_get_ctx(dom
), isl_error_invalid
,
8714 "cannot create union map from expression without "
8715 "explicit domain elements",
8716 dom
= isl_union_set_free(dom
));
8718 return isl_union_map_from_domain_and_range(dom
, ran
);
8721 /* Construct a union map mapping the shared domain
8722 * of the union piecewise affine expressions to the range of "mupa"
8723 * with each dimension in the range equated to the
8724 * corresponding union piecewise affine expression.
8726 * If the input is zero-dimensional, then construct a mapping
8727 * from its explicit domain.
8729 __isl_give isl_union_map
*isl_union_map_from_multi_union_pw_aff(
8730 __isl_take isl_multi_union_pw_aff
*mupa
)
8734 isl_union_map
*umap
;
8735 isl_union_pw_aff
*upa
;
8740 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8742 return isl_union_map_from_multi_union_pw_aff_0D(mupa
);
8744 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, 0);
8745 umap
= isl_union_map_from_union_pw_aff(upa
);
8747 for (i
= 1; i
< n
; ++i
) {
8748 isl_union_map
*umap_i
;
8750 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
8751 umap_i
= isl_union_map_from_union_pw_aff(upa
);
8752 umap
= isl_union_map_flat_range_product(umap
, umap_i
);
8755 space
= isl_multi_union_pw_aff_get_space(mupa
);
8756 umap
= isl_union_map_reset_range_space(umap
, space
);
8758 isl_multi_union_pw_aff_free(mupa
);
8762 /* Internal data structure for isl_union_pw_multi_aff_reset_range_space.
8763 * "range" is the space from which to set the range space.
8764 * "res" collects the results.
8766 struct isl_union_pw_multi_aff_reset_range_space_data
{
8768 isl_union_pw_multi_aff
*res
;
8771 /* Replace the range space of "pma" by the range space of data->range and
8772 * add the result to data->res.
8774 static isl_stat
reset_range_space(__isl_take isl_pw_multi_aff
*pma
, void *user
)
8776 struct isl_union_pw_multi_aff_reset_range_space_data
*data
= user
;
8779 space
= isl_pw_multi_aff_get_space(pma
);
8780 space
= isl_space_domain(space
);
8781 space
= isl_space_extend_domain_with_range(space
,
8782 isl_space_copy(data
->range
));
8783 pma
= isl_pw_multi_aff_reset_space(pma
, space
);
8784 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma
);
8786 return data
->res
? isl_stat_ok
: isl_stat_error
;
8789 /* Replace the range space of all the piecewise affine expressions in "upma" by
8790 * the range space of "space".
8792 * This assumes that all these expressions have the same output dimension.
8794 * Since the spaces of the expressions change, so do their hash values.
8795 * We therefore need to create a new isl_union_pw_multi_aff.
8796 * Note that the hash value is currently computed based on the entire
8797 * space even though there can only be a single expression with a given
8800 static __isl_give isl_union_pw_multi_aff
*
8801 isl_union_pw_multi_aff_reset_range_space(
8802 __isl_take isl_union_pw_multi_aff
*upma
, __isl_take isl_space
*space
)
8804 struct isl_union_pw_multi_aff_reset_range_space_data data
= { space
};
8805 isl_space
*space_upma
;
8807 space_upma
= isl_union_pw_multi_aff_get_space(upma
);
8808 data
.res
= isl_union_pw_multi_aff_empty(space_upma
);
8809 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma
,
8810 &reset_range_space
, &data
) < 0)
8811 data
.res
= isl_union_pw_multi_aff_free(data
.res
);
8813 isl_space_free(space
);
8814 isl_union_pw_multi_aff_free(upma
);
8818 /* Construct and return a union piecewise multi affine expression
8819 * that is equal to the given multi union piecewise affine expression,
8820 * in the special case of a 0D multi union piecewise affine expression.
8822 * Construct a union piecewise multi affine expression
8823 * on top of the explicit domain of the input.
8825 __isl_give isl_union_pw_multi_aff
*
8826 isl_union_pw_multi_aff_from_multi_union_pw_aff_0D(
8827 __isl_take isl_multi_union_pw_aff
*mupa
)
8831 isl_union_set
*domain
;
8833 space
= isl_multi_union_pw_aff_get_space(mupa
);
8834 mv
= isl_multi_val_zero(space
);
8835 domain
= isl_multi_union_pw_aff_domain(mupa
);
8836 return isl_union_pw_multi_aff_multi_val_on_domain(domain
, mv
);
8839 /* Construct and return a union piecewise multi affine expression
8840 * that is equal to the given multi union piecewise affine expression.
8842 * If the input is zero-dimensional, then
8843 * construct a union piecewise multi affine expression
8844 * on top of the explicit domain of the input.
8846 __isl_give isl_union_pw_multi_aff
*
8847 isl_union_pw_multi_aff_from_multi_union_pw_aff(
8848 __isl_take isl_multi_union_pw_aff
*mupa
)
8852 isl_union_pw_multi_aff
*upma
;
8853 isl_union_pw_aff
*upa
;
8858 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8860 return isl_union_pw_multi_aff_from_multi_union_pw_aff_0D(mupa
);
8862 space
= isl_multi_union_pw_aff_get_space(mupa
);
8863 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, 0);
8864 upma
= isl_union_pw_multi_aff_from_union_pw_aff(upa
);
8866 for (i
= 1; i
< n
; ++i
) {
8867 isl_union_pw_multi_aff
*upma_i
;
8869 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
8870 upma_i
= isl_union_pw_multi_aff_from_union_pw_aff(upa
);
8871 upma
= isl_union_pw_multi_aff_flat_range_product(upma
, upma_i
);
8874 upma
= isl_union_pw_multi_aff_reset_range_space(upma
, space
);
8876 isl_multi_union_pw_aff_free(mupa
);
8880 /* Intersect the range of "mupa" with "range",
8881 * in the special case where "mupa" is 0D.
8883 * Intersect the domain of "mupa" with the constraints on the parameters
8886 static __isl_give isl_multi_union_pw_aff
*mupa_intersect_range_0D(
8887 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_set
*range
)
8889 range
= isl_set_params(range
);
8890 mupa
= isl_multi_union_pw_aff_intersect_params(mupa
, range
);
8894 /* Intersect the range of "mupa" with "range".
8895 * That is, keep only those domain elements that have a function value
8898 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_intersect_range(
8899 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_set
*range
)
8901 isl_union_pw_multi_aff
*upma
;
8902 isl_union_set
*domain
;
8907 if (!mupa
|| !range
)
8910 space
= isl_set_get_space(range
);
8911 match
= isl_space_tuple_is_equal(mupa
->space
, isl_dim_set
,
8912 space
, isl_dim_set
);
8913 isl_space_free(space
);
8917 isl_die(isl_multi_union_pw_aff_get_ctx(mupa
), isl_error_invalid
,
8918 "space don't match", goto error
);
8919 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8921 return mupa_intersect_range_0D(mupa
, range
);
8923 upma
= isl_union_pw_multi_aff_from_multi_union_pw_aff(
8924 isl_multi_union_pw_aff_copy(mupa
));
8925 domain
= isl_union_set_from_set(range
);
8926 domain
= isl_union_set_preimage_union_pw_multi_aff(domain
, upma
);
8927 mupa
= isl_multi_union_pw_aff_intersect_domain(mupa
, domain
);
8931 isl_multi_union_pw_aff_free(mupa
);
8932 isl_set_free(range
);
8936 /* Return the shared domain of the elements of "mupa",
8937 * in the special case where "mupa" is zero-dimensional.
8939 * Return the explicit domain of "mupa".
8940 * Note that this domain may be a parameter set, either
8941 * because "mupa" is meant to live in a set space or
8942 * because no explicit domain has been set.
8944 __isl_give isl_union_set
*isl_multi_union_pw_aff_domain_0D(
8945 __isl_take isl_multi_union_pw_aff
*mupa
)
8949 dom
= isl_multi_union_pw_aff_get_explicit_domain(mupa
);
8950 isl_multi_union_pw_aff_free(mupa
);
8955 /* Return the shared domain of the elements of "mupa".
8957 * If "mupa" is zero-dimensional, then return its explicit domain.
8959 __isl_give isl_union_set
*isl_multi_union_pw_aff_domain(
8960 __isl_take isl_multi_union_pw_aff
*mupa
)
8963 isl_union_pw_aff
*upa
;
8969 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8971 return isl_multi_union_pw_aff_domain_0D(mupa
);
8973 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, 0);
8974 dom
= isl_union_pw_aff_domain(upa
);
8975 for (i
= 1; i
< n
; ++i
) {
8976 isl_union_set
*dom_i
;
8978 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
8979 dom_i
= isl_union_pw_aff_domain(upa
);
8980 dom
= isl_union_set_intersect(dom
, dom_i
);
8983 isl_multi_union_pw_aff_free(mupa
);
8987 /* Apply "aff" to "mupa". The space of "mupa" is equal to the domain of "aff".
8988 * In particular, the spaces have been aligned.
8989 * The result is defined over the shared domain of the elements of "mupa"
8991 * We first extract the parametric constant part of "aff" and
8992 * define that over the shared domain.
8993 * Then we iterate over all input dimensions of "aff" and add the corresponding
8994 * multiples of the elements of "mupa".
8995 * Finally, we consider the integer divisions, calling the function
8996 * recursively to obtain an isl_union_pw_aff corresponding to the
8997 * integer division argument.
8999 static __isl_give isl_union_pw_aff
*multi_union_pw_aff_apply_aff(
9000 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_aff
*aff
)
9003 isl_union_pw_aff
*upa
;
9004 isl_union_set
*uset
;
9008 n_in
= isl_aff_dim(aff
, isl_dim_in
);
9009 n_div
= isl_aff_dim(aff
, isl_dim_div
);
9011 uset
= isl_multi_union_pw_aff_domain(isl_multi_union_pw_aff_copy(mupa
));
9012 cst
= isl_aff_copy(aff
);
9013 cst
= isl_aff_drop_dims(cst
, isl_dim_div
, 0, n_div
);
9014 cst
= isl_aff_drop_dims(cst
, isl_dim_in
, 0, n_in
);
9015 cst
= isl_aff_project_domain_on_params(cst
);
9016 upa
= isl_union_pw_aff_aff_on_domain(uset
, cst
);
9018 for (i
= 0; i
< n_in
; ++i
) {
9019 isl_union_pw_aff
*upa_i
;
9021 if (!isl_aff_involves_dims(aff
, isl_dim_in
, i
, 1))
9023 v
= isl_aff_get_coefficient_val(aff
, isl_dim_in
, i
);
9024 upa_i
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
9025 upa_i
= isl_union_pw_aff_scale_val(upa_i
, v
);
9026 upa
= isl_union_pw_aff_add(upa
, upa_i
);
9029 for (i
= 0; i
< n_div
; ++i
) {
9031 isl_union_pw_aff
*upa_i
;
9033 if (!isl_aff_involves_dims(aff
, isl_dim_div
, i
, 1))
9035 div
= isl_aff_get_div(aff
, i
);
9036 upa_i
= multi_union_pw_aff_apply_aff(
9037 isl_multi_union_pw_aff_copy(mupa
), div
);
9038 upa_i
= isl_union_pw_aff_floor(upa_i
);
9039 v
= isl_aff_get_coefficient_val(aff
, isl_dim_div
, i
);
9040 upa_i
= isl_union_pw_aff_scale_val(upa_i
, v
);
9041 upa
= isl_union_pw_aff_add(upa
, upa_i
);
9044 isl_multi_union_pw_aff_free(mupa
);
9050 /* Apply "aff" to "mupa". The space of "mupa" needs to be compatible
9051 * with the domain of "aff".
9052 * Furthermore, the dimension of this space needs to be greater than zero.
9053 * The result is defined over the shared domain of the elements of "mupa"
9055 * We perform these checks and then hand over control to
9056 * multi_union_pw_aff_apply_aff.
9058 __isl_give isl_union_pw_aff
*isl_multi_union_pw_aff_apply_aff(
9059 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_aff
*aff
)
9061 isl_space
*space1
, *space2
;
9064 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
9065 isl_aff_get_space(aff
));
9066 aff
= isl_aff_align_params(aff
, isl_multi_union_pw_aff_get_space(mupa
));
9070 space1
= isl_multi_union_pw_aff_get_space(mupa
);
9071 space2
= isl_aff_get_domain_space(aff
);
9072 equal
= isl_space_is_equal(space1
, space2
);
9073 isl_space_free(space1
);
9074 isl_space_free(space2
);
9078 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
9079 "spaces don't match", goto error
);
9080 if (isl_aff_dim(aff
, isl_dim_in
) == 0)
9081 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
9082 "cannot determine domains", goto error
);
9084 return multi_union_pw_aff_apply_aff(mupa
, aff
);
9086 isl_multi_union_pw_aff_free(mupa
);
9091 /* Apply "ma" to "mupa", in the special case where "mupa" is 0D.
9092 * The space of "mupa" is known to be compatible with the domain of "ma".
9094 * Construct an isl_multi_union_pw_aff that is equal to "ma"
9095 * on the domain of "mupa".
9097 static __isl_give isl_multi_union_pw_aff
*mupa_apply_multi_aff_0D(
9098 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_multi_aff
*ma
)
9102 dom
= isl_multi_union_pw_aff_domain(mupa
);
9103 ma
= isl_multi_aff_project_domain_on_params(ma
);
9105 return isl_multi_union_pw_aff_multi_aff_on_domain(dom
, ma
);
9108 /* Apply "ma" to "mupa". The space of "mupa" needs to be compatible
9109 * with the domain of "ma".
9110 * The result is defined over the shared domain of the elements of "mupa"
9112 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_apply_multi_aff(
9113 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_multi_aff
*ma
)
9115 isl_space
*space1
, *space2
;
9116 isl_multi_union_pw_aff
*res
;
9120 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
9121 isl_multi_aff_get_space(ma
));
9122 ma
= isl_multi_aff_align_params(ma
,
9123 isl_multi_union_pw_aff_get_space(mupa
));
9127 space1
= isl_multi_union_pw_aff_get_space(mupa
);
9128 space2
= isl_multi_aff_get_domain_space(ma
);
9129 equal
= isl_space_is_equal(space1
, space2
);
9130 isl_space_free(space1
);
9131 isl_space_free(space2
);
9135 isl_die(isl_multi_aff_get_ctx(ma
), isl_error_invalid
,
9136 "spaces don't match", goto error
);
9137 n_out
= isl_multi_aff_dim(ma
, isl_dim_out
);
9138 if (isl_multi_aff_dim(ma
, isl_dim_in
) == 0)
9139 return mupa_apply_multi_aff_0D(mupa
, ma
);
9141 space1
= isl_space_range(isl_multi_aff_get_space(ma
));
9142 res
= isl_multi_union_pw_aff_alloc(space1
);
9144 for (i
= 0; i
< n_out
; ++i
) {
9146 isl_union_pw_aff
*upa
;
9148 aff
= isl_multi_aff_get_aff(ma
, i
);
9149 upa
= multi_union_pw_aff_apply_aff(
9150 isl_multi_union_pw_aff_copy(mupa
), aff
);
9151 res
= isl_multi_union_pw_aff_set_union_pw_aff(res
, i
, upa
);
9154 isl_multi_aff_free(ma
);
9155 isl_multi_union_pw_aff_free(mupa
);
9158 isl_multi_union_pw_aff_free(mupa
);
9159 isl_multi_aff_free(ma
);
9163 /* Apply "pa" to "mupa", in the special case where "mupa" is 0D.
9164 * The space of "mupa" is known to be compatible with the domain of "pa".
9166 * Construct an isl_multi_union_pw_aff that is equal to "pa"
9167 * on the domain of "mupa".
9169 static __isl_give isl_union_pw_aff
*isl_multi_union_pw_aff_apply_pw_aff_0D(
9170 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_pw_aff
*pa
)
9174 dom
= isl_multi_union_pw_aff_domain(mupa
);
9175 pa
= isl_pw_aff_project_domain_on_params(pa
);
9177 return isl_union_pw_aff_pw_aff_on_domain(dom
, pa
);
9180 /* Apply "pa" to "mupa". The space of "mupa" needs to be compatible
9181 * with the domain of "pa".
9182 * Furthermore, the dimension of this space needs to be greater than zero.
9183 * The result is defined over the shared domain of the elements of "mupa"
9185 __isl_give isl_union_pw_aff
*isl_multi_union_pw_aff_apply_pw_aff(
9186 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_pw_aff
*pa
)
9190 isl_space
*space
, *space2
;
9191 isl_union_pw_aff
*upa
;
9193 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
9194 isl_pw_aff_get_space(pa
));
9195 pa
= isl_pw_aff_align_params(pa
,
9196 isl_multi_union_pw_aff_get_space(mupa
));
9200 space
= isl_multi_union_pw_aff_get_space(mupa
);
9201 space2
= isl_pw_aff_get_domain_space(pa
);
9202 equal
= isl_space_is_equal(space
, space2
);
9203 isl_space_free(space
);
9204 isl_space_free(space2
);
9208 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
9209 "spaces don't match", goto error
);
9210 if (isl_pw_aff_dim(pa
, isl_dim_in
) == 0)
9211 return isl_multi_union_pw_aff_apply_pw_aff_0D(mupa
, pa
);
9213 space
= isl_space_params(isl_multi_union_pw_aff_get_space(mupa
));
9214 upa
= isl_union_pw_aff_empty(space
);
9216 for (i
= 0; i
< pa
->n
; ++i
) {
9219 isl_multi_union_pw_aff
*mupa_i
;
9220 isl_union_pw_aff
*upa_i
;
9222 mupa_i
= isl_multi_union_pw_aff_copy(mupa
);
9223 domain
= isl_set_copy(pa
->p
[i
].set
);
9224 mupa_i
= isl_multi_union_pw_aff_intersect_range(mupa_i
, domain
);
9225 aff
= isl_aff_copy(pa
->p
[i
].aff
);
9226 upa_i
= multi_union_pw_aff_apply_aff(mupa_i
, aff
);
9227 upa
= isl_union_pw_aff_union_add(upa
, upa_i
);
9230 isl_multi_union_pw_aff_free(mupa
);
9231 isl_pw_aff_free(pa
);
9234 isl_multi_union_pw_aff_free(mupa
);
9235 isl_pw_aff_free(pa
);
9239 /* Apply "pma" to "mupa", in the special case where "mupa" is 0D.
9240 * The space of "mupa" is known to be compatible with the domain of "pma".
9242 * Construct an isl_multi_union_pw_aff that is equal to "pma"
9243 * on the domain of "mupa".
9245 static __isl_give isl_multi_union_pw_aff
*mupa_apply_pw_multi_aff_0D(
9246 __isl_take isl_multi_union_pw_aff
*mupa
,
9247 __isl_take isl_pw_multi_aff
*pma
)
9251 dom
= isl_multi_union_pw_aff_domain(mupa
);
9252 pma
= isl_pw_multi_aff_project_domain_on_params(pma
);
9254 return isl_multi_union_pw_aff_pw_multi_aff_on_domain(dom
, pma
);
9257 /* Apply "pma" to "mupa". The space of "mupa" needs to be compatible
9258 * with the domain of "pma".
9259 * The result is defined over the shared domain of the elements of "mupa"
9261 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_apply_pw_multi_aff(
9262 __isl_take isl_multi_union_pw_aff
*mupa
,
9263 __isl_take isl_pw_multi_aff
*pma
)
9265 isl_space
*space1
, *space2
;
9266 isl_multi_union_pw_aff
*res
;
9270 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
9271 isl_pw_multi_aff_get_space(pma
));
9272 pma
= isl_pw_multi_aff_align_params(pma
,
9273 isl_multi_union_pw_aff_get_space(mupa
));
9277 space1
= isl_multi_union_pw_aff_get_space(mupa
);
9278 space2
= isl_pw_multi_aff_get_domain_space(pma
);
9279 equal
= isl_space_is_equal(space1
, space2
);
9280 isl_space_free(space1
);
9281 isl_space_free(space2
);
9285 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
9286 "spaces don't match", goto error
);
9287 n_out
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
9288 if (isl_pw_multi_aff_dim(pma
, isl_dim_in
) == 0)
9289 return mupa_apply_pw_multi_aff_0D(mupa
, pma
);
9291 space1
= isl_space_range(isl_pw_multi_aff_get_space(pma
));
9292 res
= isl_multi_union_pw_aff_alloc(space1
);
9294 for (i
= 0; i
< n_out
; ++i
) {
9296 isl_union_pw_aff
*upa
;
9298 pa
= isl_pw_multi_aff_get_pw_aff(pma
, i
);
9299 upa
= isl_multi_union_pw_aff_apply_pw_aff(
9300 isl_multi_union_pw_aff_copy(mupa
), pa
);
9301 res
= isl_multi_union_pw_aff_set_union_pw_aff(res
, i
, upa
);
9304 isl_pw_multi_aff_free(pma
);
9305 isl_multi_union_pw_aff_free(mupa
);
9308 isl_multi_union_pw_aff_free(mupa
);
9309 isl_pw_multi_aff_free(pma
);
9313 /* Replace the explicit domain of "mupa" by its preimage under "upma".
9314 * If the explicit domain only keeps track of constraints on the parameters,
9315 * then only update those constraints.
9317 static __isl_give isl_multi_union_pw_aff
*preimage_explicit_domain(
9318 __isl_take isl_multi_union_pw_aff
*mupa
,
9319 __isl_keep isl_union_pw_multi_aff
*upma
)
9323 if (isl_multi_union_pw_aff_check_has_explicit_domain(mupa
) < 0)
9324 return isl_multi_union_pw_aff_free(mupa
);
9326 mupa
= isl_multi_union_pw_aff_cow(mupa
);
9330 is_params
= isl_union_set_is_params(mupa
->u
.dom
);
9332 return isl_multi_union_pw_aff_free(mupa
);
9334 upma
= isl_union_pw_multi_aff_copy(upma
);
9336 mupa
->u
.dom
= isl_union_set_intersect_params(mupa
->u
.dom
,
9337 isl_union_set_params(isl_union_pw_multi_aff_domain(upma
)));
9339 mupa
->u
.dom
= isl_union_set_preimage_union_pw_multi_aff(
9342 return isl_multi_union_pw_aff_free(mupa
);
9346 /* Compute the pullback of "mupa" by the function represented by "upma".
9347 * In other words, plug in "upma" in "mupa". The result contains
9348 * expressions defined over the domain space of "upma".
9350 * Run over all elements of "mupa" and plug in "upma" in each of them.
9352 * If "mupa" has an explicit domain, then it is this domain
9353 * that needs to undergo a pullback instead, i.e., a preimage.
9355 __isl_give isl_multi_union_pw_aff
*
9356 isl_multi_union_pw_aff_pullback_union_pw_multi_aff(
9357 __isl_take isl_multi_union_pw_aff
*mupa
,
9358 __isl_take isl_union_pw_multi_aff
*upma
)
9362 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
9363 isl_union_pw_multi_aff_get_space(upma
));
9364 upma
= isl_union_pw_multi_aff_align_params(upma
,
9365 isl_multi_union_pw_aff_get_space(mupa
));
9366 mupa
= isl_multi_union_pw_aff_cow(mupa
);
9370 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
9371 for (i
= 0; i
< n
; ++i
) {
9372 isl_union_pw_aff
*upa
;
9374 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
9375 upa
= isl_union_pw_aff_pullback_union_pw_multi_aff(upa
,
9376 isl_union_pw_multi_aff_copy(upma
));
9377 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
9380 if (isl_multi_union_pw_aff_has_explicit_domain(mupa
))
9381 mupa
= preimage_explicit_domain(mupa
, upma
);
9383 isl_union_pw_multi_aff_free(upma
);
9386 isl_multi_union_pw_aff_free(mupa
);
9387 isl_union_pw_multi_aff_free(upma
);
9391 /* Extract the sequence of elements in "mupa" with domain space "space"
9392 * (ignoring parameters).
9394 * For the elements of "mupa" that are not defined on the specified space,
9395 * the corresponding element in the result is empty.
9397 __isl_give isl_multi_pw_aff
*isl_multi_union_pw_aff_extract_multi_pw_aff(
9398 __isl_keep isl_multi_union_pw_aff
*mupa
, __isl_take isl_space
*space
)
9401 isl_space
*space_mpa
;
9402 isl_multi_pw_aff
*mpa
;
9404 if (!mupa
|| !space
)
9407 space_mpa
= isl_multi_union_pw_aff_get_space(mupa
);
9408 space
= isl_space_replace_params(space
, space_mpa
);
9409 space_mpa
= isl_space_map_from_domain_and_range(isl_space_copy(space
),
9411 mpa
= isl_multi_pw_aff_alloc(space_mpa
);
9413 space
= isl_space_from_domain(space
);
9414 space
= isl_space_add_dims(space
, isl_dim_out
, 1);
9415 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
9416 for (i
= 0; i
< n
; ++i
) {
9417 isl_union_pw_aff
*upa
;
9420 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
9421 pa
= isl_union_pw_aff_extract_pw_aff(upa
,
9422 isl_space_copy(space
));
9423 mpa
= isl_multi_pw_aff_set_pw_aff(mpa
, i
, pa
);
9424 isl_union_pw_aff_free(upa
);
9427 isl_space_free(space
);
9430 isl_space_free(space
);
9434 /* Evaluate the affine function "aff" in the void point "pnt".
9435 * In particular, return the value NaN.
9437 static __isl_give isl_val
*eval_void(__isl_take isl_aff
*aff
,
9438 __isl_take isl_point
*pnt
)
9442 ctx
= isl_point_get_ctx(pnt
);
9444 isl_point_free(pnt
);
9445 return isl_val_nan(ctx
);
9448 /* Evaluate the affine expression "aff"
9449 * in the coordinates (with denominator) "pnt".
9451 static __isl_give isl_val
*eval(__isl_keep isl_vec
*aff
,
9452 __isl_keep isl_vec
*pnt
)
9461 ctx
= isl_vec_get_ctx(aff
);
9464 isl_seq_inner_product(aff
->el
+ 1, pnt
->el
, pnt
->size
, &n
);
9465 isl_int_mul(d
, aff
->el
[0], pnt
->el
[0]);
9466 v
= isl_val_rat_from_isl_int(ctx
, n
, d
);
9467 v
= isl_val_normalize(v
);
9474 /* Check that the domain space of "aff" is equal to "space".
9476 static isl_stat
isl_aff_check_has_domain_space(__isl_keep isl_aff
*aff
,
9477 __isl_keep isl_space
*space
)
9481 ok
= isl_space_is_equal(isl_aff_peek_domain_space(aff
), space
);
9483 return isl_stat_error
;
9485 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
9486 "incompatible spaces", return isl_stat_error
);
9490 /* Evaluate the affine function "aff" in "pnt".
9492 __isl_give isl_val
*isl_aff_eval(__isl_take isl_aff
*aff
,
9493 __isl_take isl_point
*pnt
)
9497 isl_local_space
*ls
;
9499 if (isl_aff_check_has_domain_space(aff
, isl_point_peek_space(pnt
)) < 0)
9501 is_void
= isl_point_is_void(pnt
);
9505 return eval_void(aff
, pnt
);
9507 ls
= isl_aff_get_domain_local_space(aff
);
9508 pnt
= isl_local_space_lift_point(ls
, pnt
);
9510 v
= eval(aff
->v
, isl_point_peek_vec(pnt
));
9513 isl_point_free(pnt
);
9518 isl_point_free(pnt
);