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 exp
= isl_parameter_alignment_reordering(aff
->ls
->dim
, model
);
561 exp
= isl_reordering_extend_space(exp
,
562 isl_aff_get_domain_space(aff
));
563 aff
= isl_aff_realign_domain(aff
, exp
);
566 isl_space_free(model
);
569 isl_space_free(model
);
574 /* Is "aff" obviously equal to zero?
576 * If the denominator is zero, then "aff" is not equal to zero.
578 isl_bool
isl_aff_plain_is_zero(__isl_keep isl_aff
*aff
)
581 return isl_bool_error
;
583 if (isl_int_is_zero(aff
->v
->el
[0]))
584 return isl_bool_false
;
585 return isl_seq_first_non_zero(aff
->v
->el
+ 1, aff
->v
->size
- 1) < 0;
588 /* Does "aff" represent NaN?
590 isl_bool
isl_aff_is_nan(__isl_keep isl_aff
*aff
)
593 return isl_bool_error
;
595 return isl_seq_first_non_zero(aff
->v
->el
, 2) < 0;
598 /* Are "aff1" and "aff2" obviously equal?
600 * NaN is not equal to anything, not even to another NaN.
602 isl_bool
isl_aff_plain_is_equal(__isl_keep isl_aff
*aff1
,
603 __isl_keep isl_aff
*aff2
)
608 return isl_bool_error
;
610 if (isl_aff_is_nan(aff1
) || isl_aff_is_nan(aff2
))
611 return isl_bool_false
;
613 equal
= isl_local_space_is_equal(aff1
->ls
, aff2
->ls
);
614 if (equal
< 0 || !equal
)
617 return isl_vec_is_equal(aff1
->v
, aff2
->v
);
620 /* Return the common denominator of "aff" in "v".
622 * We cannot return anything meaningful in case of a NaN.
624 isl_stat
isl_aff_get_denominator(__isl_keep isl_aff
*aff
, isl_int
*v
)
627 return isl_stat_error
;
628 if (isl_aff_is_nan(aff
))
629 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
630 "cannot get denominator of NaN", return isl_stat_error
);
631 isl_int_set(*v
, aff
->v
->el
[0]);
635 /* Return the common denominator of "aff".
637 __isl_give isl_val
*isl_aff_get_denominator_val(__isl_keep isl_aff
*aff
)
644 ctx
= isl_aff_get_ctx(aff
);
645 if (isl_aff_is_nan(aff
))
646 return isl_val_nan(ctx
);
647 return isl_val_int_from_isl_int(ctx
, aff
->v
->el
[0]);
650 /* Return the constant term of "aff".
652 __isl_give isl_val
*isl_aff_get_constant_val(__isl_keep isl_aff
*aff
)
660 ctx
= isl_aff_get_ctx(aff
);
661 if (isl_aff_is_nan(aff
))
662 return isl_val_nan(ctx
);
663 v
= isl_val_rat_from_isl_int(ctx
, aff
->v
->el
[1], aff
->v
->el
[0]);
664 return isl_val_normalize(v
);
667 /* Return the coefficient of the variable of type "type" at position "pos"
670 __isl_give isl_val
*isl_aff_get_coefficient_val(__isl_keep isl_aff
*aff
,
671 enum isl_dim_type type
, int pos
)
679 ctx
= isl_aff_get_ctx(aff
);
680 if (type
== isl_dim_out
)
681 isl_die(ctx
, isl_error_invalid
,
682 "output/set dimension does not have a coefficient",
684 if (type
== isl_dim_in
)
687 if (pos
>= isl_local_space_dim(aff
->ls
, type
))
688 isl_die(ctx
, isl_error_invalid
,
689 "position out of bounds", return NULL
);
691 if (isl_aff_is_nan(aff
))
692 return isl_val_nan(ctx
);
693 pos
+= isl_local_space_offset(aff
->ls
, type
);
694 v
= isl_val_rat_from_isl_int(ctx
, aff
->v
->el
[1 + pos
], aff
->v
->el
[0]);
695 return isl_val_normalize(v
);
698 /* Return the sign of the coefficient of the variable of type "type"
699 * at position "pos" of "aff".
701 int isl_aff_coefficient_sgn(__isl_keep isl_aff
*aff
, enum isl_dim_type type
,
709 ctx
= isl_aff_get_ctx(aff
);
710 if (type
== isl_dim_out
)
711 isl_die(ctx
, isl_error_invalid
,
712 "output/set dimension does not have a coefficient",
714 if (type
== isl_dim_in
)
717 if (pos
>= isl_local_space_dim(aff
->ls
, type
))
718 isl_die(ctx
, isl_error_invalid
,
719 "position out of bounds", return 0);
721 pos
+= isl_local_space_offset(aff
->ls
, type
);
722 return isl_int_sgn(aff
->v
->el
[1 + pos
]);
725 /* Replace the numerator of the constant term of "aff" by "v".
727 * A NaN is unaffected by this operation.
729 __isl_give isl_aff
*isl_aff_set_constant(__isl_take isl_aff
*aff
, isl_int v
)
733 if (isl_aff_is_nan(aff
))
735 aff
= isl_aff_cow(aff
);
739 aff
->v
= isl_vec_cow(aff
->v
);
741 return isl_aff_free(aff
);
743 isl_int_set(aff
->v
->el
[1], v
);
748 /* Replace the constant term of "aff" by "v".
750 * A NaN is unaffected by this operation.
752 __isl_give isl_aff
*isl_aff_set_constant_val(__isl_take isl_aff
*aff
,
753 __isl_take isl_val
*v
)
758 if (isl_aff_is_nan(aff
)) {
763 if (!isl_val_is_rat(v
))
764 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
765 "expecting rational value", goto error
);
767 if (isl_int_eq(aff
->v
->el
[1], v
->n
) &&
768 isl_int_eq(aff
->v
->el
[0], v
->d
)) {
773 aff
= isl_aff_cow(aff
);
776 aff
->v
= isl_vec_cow(aff
->v
);
780 if (isl_int_eq(aff
->v
->el
[0], v
->d
)) {
781 isl_int_set(aff
->v
->el
[1], v
->n
);
782 } else if (isl_int_is_one(v
->d
)) {
783 isl_int_mul(aff
->v
->el
[1], aff
->v
->el
[0], v
->n
);
785 isl_seq_scale(aff
->v
->el
+ 1,
786 aff
->v
->el
+ 1, v
->d
, aff
->v
->size
- 1);
787 isl_int_mul(aff
->v
->el
[1], aff
->v
->el
[0], v
->n
);
788 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], v
->d
);
789 aff
->v
= isl_vec_normalize(aff
->v
);
802 /* Add "v" to the constant term of "aff".
804 * A NaN is unaffected by this operation.
806 __isl_give isl_aff
*isl_aff_add_constant(__isl_take isl_aff
*aff
, isl_int v
)
808 if (isl_int_is_zero(v
))
813 if (isl_aff_is_nan(aff
))
815 aff
= isl_aff_cow(aff
);
819 aff
->v
= isl_vec_cow(aff
->v
);
821 return isl_aff_free(aff
);
823 isl_int_addmul(aff
->v
->el
[1], aff
->v
->el
[0], v
);
828 /* Add "v" to the constant term of "aff".
830 * A NaN is unaffected by this operation.
832 __isl_give isl_aff
*isl_aff_add_constant_val(__isl_take isl_aff
*aff
,
833 __isl_take isl_val
*v
)
838 if (isl_aff_is_nan(aff
) || isl_val_is_zero(v
)) {
843 if (!isl_val_is_rat(v
))
844 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
845 "expecting rational value", goto error
);
847 aff
= isl_aff_cow(aff
);
851 aff
->v
= isl_vec_cow(aff
->v
);
855 if (isl_int_is_one(v
->d
)) {
856 isl_int_addmul(aff
->v
->el
[1], aff
->v
->el
[0], v
->n
);
857 } else if (isl_int_eq(aff
->v
->el
[0], v
->d
)) {
858 isl_int_add(aff
->v
->el
[1], aff
->v
->el
[1], v
->n
);
859 aff
->v
= isl_vec_normalize(aff
->v
);
863 isl_seq_scale(aff
->v
->el
+ 1,
864 aff
->v
->el
+ 1, v
->d
, aff
->v
->size
- 1);
865 isl_int_addmul(aff
->v
->el
[1], aff
->v
->el
[0], v
->n
);
866 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], v
->d
);
867 aff
->v
= isl_vec_normalize(aff
->v
);
880 __isl_give isl_aff
*isl_aff_add_constant_si(__isl_take isl_aff
*aff
, int v
)
885 isl_int_set_si(t
, v
);
886 aff
= isl_aff_add_constant(aff
, t
);
892 /* Add "v" to the numerator of the constant term of "aff".
894 * A NaN is unaffected by this operation.
896 __isl_give isl_aff
*isl_aff_add_constant_num(__isl_take isl_aff
*aff
, isl_int v
)
898 if (isl_int_is_zero(v
))
903 if (isl_aff_is_nan(aff
))
905 aff
= isl_aff_cow(aff
);
909 aff
->v
= isl_vec_cow(aff
->v
);
911 return isl_aff_free(aff
);
913 isl_int_add(aff
->v
->el
[1], aff
->v
->el
[1], v
);
918 /* Add "v" to the numerator of the constant term of "aff".
920 * A NaN is unaffected by this operation.
922 __isl_give isl_aff
*isl_aff_add_constant_num_si(__isl_take isl_aff
*aff
, int v
)
930 isl_int_set_si(t
, v
);
931 aff
= isl_aff_add_constant_num(aff
, t
);
937 /* Replace the numerator of the constant term of "aff" by "v".
939 * A NaN is unaffected by this operation.
941 __isl_give isl_aff
*isl_aff_set_constant_si(__isl_take isl_aff
*aff
, int v
)
945 if (isl_aff_is_nan(aff
))
947 aff
= isl_aff_cow(aff
);
951 aff
->v
= isl_vec_cow(aff
->v
);
953 return isl_aff_free(aff
);
955 isl_int_set_si(aff
->v
->el
[1], v
);
960 /* Replace the numerator of the coefficient of the variable of type "type"
961 * at position "pos" of "aff" by "v".
963 * A NaN is unaffected by this operation.
965 __isl_give isl_aff
*isl_aff_set_coefficient(__isl_take isl_aff
*aff
,
966 enum isl_dim_type type
, int pos
, isl_int v
)
971 if (type
== isl_dim_out
)
972 isl_die(aff
->v
->ctx
, isl_error_invalid
,
973 "output/set dimension does not have a coefficient",
974 return isl_aff_free(aff
));
975 if (type
== isl_dim_in
)
978 if (pos
>= isl_local_space_dim(aff
->ls
, type
))
979 isl_die(aff
->v
->ctx
, isl_error_invalid
,
980 "position out of bounds", return isl_aff_free(aff
));
982 if (isl_aff_is_nan(aff
))
984 aff
= isl_aff_cow(aff
);
988 aff
->v
= isl_vec_cow(aff
->v
);
990 return isl_aff_free(aff
);
992 pos
+= isl_local_space_offset(aff
->ls
, type
);
993 isl_int_set(aff
->v
->el
[1 + pos
], v
);
998 /* Replace the numerator of the coefficient of the variable of type "type"
999 * at position "pos" of "aff" by "v".
1001 * A NaN is unaffected by this operation.
1003 __isl_give isl_aff
*isl_aff_set_coefficient_si(__isl_take isl_aff
*aff
,
1004 enum isl_dim_type type
, int pos
, int v
)
1009 if (type
== isl_dim_out
)
1010 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1011 "output/set dimension does not have a coefficient",
1012 return isl_aff_free(aff
));
1013 if (type
== isl_dim_in
)
1016 if (pos
< 0 || pos
>= isl_local_space_dim(aff
->ls
, type
))
1017 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1018 "position out of bounds", return isl_aff_free(aff
));
1020 if (isl_aff_is_nan(aff
))
1022 pos
+= isl_local_space_offset(aff
->ls
, type
);
1023 if (isl_int_cmp_si(aff
->v
->el
[1 + pos
], v
) == 0)
1026 aff
= isl_aff_cow(aff
);
1030 aff
->v
= isl_vec_cow(aff
->v
);
1032 return isl_aff_free(aff
);
1034 isl_int_set_si(aff
->v
->el
[1 + pos
], v
);
1039 /* Replace the coefficient of the variable of type "type" at position "pos"
1042 * A NaN is unaffected by this operation.
1044 __isl_give isl_aff
*isl_aff_set_coefficient_val(__isl_take isl_aff
*aff
,
1045 enum isl_dim_type type
, int pos
, __isl_take isl_val
*v
)
1050 if (type
== isl_dim_out
)
1051 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1052 "output/set dimension does not have a coefficient",
1054 if (type
== isl_dim_in
)
1057 if (pos
>= isl_local_space_dim(aff
->ls
, type
))
1058 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1059 "position out of bounds", goto error
);
1061 if (isl_aff_is_nan(aff
)) {
1065 if (!isl_val_is_rat(v
))
1066 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1067 "expecting rational value", goto error
);
1069 pos
+= isl_local_space_offset(aff
->ls
, type
);
1070 if (isl_int_eq(aff
->v
->el
[1 + pos
], v
->n
) &&
1071 isl_int_eq(aff
->v
->el
[0], v
->d
)) {
1076 aff
= isl_aff_cow(aff
);
1079 aff
->v
= isl_vec_cow(aff
->v
);
1083 if (isl_int_eq(aff
->v
->el
[0], v
->d
)) {
1084 isl_int_set(aff
->v
->el
[1 + pos
], v
->n
);
1085 } else if (isl_int_is_one(v
->d
)) {
1086 isl_int_mul(aff
->v
->el
[1 + pos
], aff
->v
->el
[0], v
->n
);
1088 isl_seq_scale(aff
->v
->el
+ 1,
1089 aff
->v
->el
+ 1, v
->d
, aff
->v
->size
- 1);
1090 isl_int_mul(aff
->v
->el
[1 + pos
], aff
->v
->el
[0], v
->n
);
1091 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], v
->d
);
1092 aff
->v
= isl_vec_normalize(aff
->v
);
1105 /* Add "v" to the coefficient of the variable of type "type"
1106 * at position "pos" of "aff".
1108 * A NaN is unaffected by this operation.
1110 __isl_give isl_aff
*isl_aff_add_coefficient(__isl_take isl_aff
*aff
,
1111 enum isl_dim_type type
, int pos
, isl_int v
)
1116 if (type
== isl_dim_out
)
1117 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1118 "output/set dimension does not have a coefficient",
1119 return isl_aff_free(aff
));
1120 if (type
== isl_dim_in
)
1123 if (pos
>= isl_local_space_dim(aff
->ls
, type
))
1124 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1125 "position out of bounds", return isl_aff_free(aff
));
1127 if (isl_aff_is_nan(aff
))
1129 aff
= isl_aff_cow(aff
);
1133 aff
->v
= isl_vec_cow(aff
->v
);
1135 return isl_aff_free(aff
);
1137 pos
+= isl_local_space_offset(aff
->ls
, type
);
1138 isl_int_addmul(aff
->v
->el
[1 + pos
], aff
->v
->el
[0], v
);
1143 /* Add "v" to the coefficient of the variable of type "type"
1144 * at position "pos" of "aff".
1146 * A NaN is unaffected by this operation.
1148 __isl_give isl_aff
*isl_aff_add_coefficient_val(__isl_take isl_aff
*aff
,
1149 enum isl_dim_type type
, int pos
, __isl_take isl_val
*v
)
1154 if (isl_val_is_zero(v
)) {
1159 if (type
== isl_dim_out
)
1160 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1161 "output/set dimension does not have a coefficient",
1163 if (type
== isl_dim_in
)
1166 if (pos
>= isl_local_space_dim(aff
->ls
, type
))
1167 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1168 "position out of bounds", goto error
);
1170 if (isl_aff_is_nan(aff
)) {
1174 if (!isl_val_is_rat(v
))
1175 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1176 "expecting rational value", goto error
);
1178 aff
= isl_aff_cow(aff
);
1182 aff
->v
= isl_vec_cow(aff
->v
);
1186 pos
+= isl_local_space_offset(aff
->ls
, type
);
1187 if (isl_int_is_one(v
->d
)) {
1188 isl_int_addmul(aff
->v
->el
[1 + pos
], aff
->v
->el
[0], v
->n
);
1189 } else if (isl_int_eq(aff
->v
->el
[0], v
->d
)) {
1190 isl_int_add(aff
->v
->el
[1 + pos
], aff
->v
->el
[1 + pos
], v
->n
);
1191 aff
->v
= isl_vec_normalize(aff
->v
);
1195 isl_seq_scale(aff
->v
->el
+ 1,
1196 aff
->v
->el
+ 1, v
->d
, aff
->v
->size
- 1);
1197 isl_int_addmul(aff
->v
->el
[1 + pos
], aff
->v
->el
[0], v
->n
);
1198 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], v
->d
);
1199 aff
->v
= isl_vec_normalize(aff
->v
);
1212 __isl_give isl_aff
*isl_aff_add_coefficient_si(__isl_take isl_aff
*aff
,
1213 enum isl_dim_type type
, int pos
, int v
)
1218 isl_int_set_si(t
, v
);
1219 aff
= isl_aff_add_coefficient(aff
, type
, pos
, t
);
1225 __isl_give isl_aff
*isl_aff_get_div(__isl_keep isl_aff
*aff
, int pos
)
1230 return isl_local_space_get_div(aff
->ls
, pos
);
1233 /* Return the negation of "aff".
1235 * As a special case, -NaN = NaN.
1237 __isl_give isl_aff
*isl_aff_neg(__isl_take isl_aff
*aff
)
1241 if (isl_aff_is_nan(aff
))
1243 aff
= isl_aff_cow(aff
);
1246 aff
->v
= isl_vec_cow(aff
->v
);
1248 return isl_aff_free(aff
);
1250 isl_seq_neg(aff
->v
->el
+ 1, aff
->v
->el
+ 1, aff
->v
->size
- 1);
1255 /* Remove divs from the local space that do not appear in the affine
1257 * We currently only remove divs at the end.
1258 * Some intermediate divs may also not appear directly in the affine
1259 * expression, but we would also need to check that no other divs are
1260 * defined in terms of them.
1262 __isl_give isl_aff
*isl_aff_remove_unused_divs(__isl_take isl_aff
*aff
)
1271 n
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
1272 off
= isl_local_space_offset(aff
->ls
, isl_dim_div
);
1274 pos
= isl_seq_last_non_zero(aff
->v
->el
+ 1 + off
, n
) + 1;
1278 aff
= isl_aff_cow(aff
);
1282 aff
->ls
= isl_local_space_drop_dims(aff
->ls
, isl_dim_div
, pos
, n
- pos
);
1283 aff
->v
= isl_vec_drop_els(aff
->v
, 1 + off
+ pos
, n
- pos
);
1284 if (!aff
->ls
|| !aff
->v
)
1285 return isl_aff_free(aff
);
1290 /* Look for any divs in the aff->ls with a denominator equal to one
1291 * and plug them into the affine expression and any subsequent divs
1292 * that may reference the div.
1294 static __isl_give isl_aff
*plug_in_integral_divs(__isl_take isl_aff
*aff
)
1300 isl_local_space
*ls
;
1306 n
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
1308 for (i
= 0; i
< n
; ++i
) {
1309 if (!isl_int_is_one(aff
->ls
->div
->row
[i
][0]))
1311 ls
= isl_local_space_copy(aff
->ls
);
1312 ls
= isl_local_space_substitute_seq(ls
, isl_dim_div
, i
,
1313 aff
->ls
->div
->row
[i
], len
, i
+ 1, n
- (i
+ 1));
1314 vec
= isl_vec_copy(aff
->v
);
1315 vec
= isl_vec_cow(vec
);
1321 pos
= isl_local_space_offset(aff
->ls
, isl_dim_div
) + i
;
1322 isl_seq_substitute(vec
->el
, pos
, aff
->ls
->div
->row
[i
],
1327 isl_vec_free(aff
->v
);
1329 isl_local_space_free(aff
->ls
);
1336 isl_local_space_free(ls
);
1337 return isl_aff_free(aff
);
1340 /* Look for any divs j that appear with a unit coefficient inside
1341 * the definitions of other divs i and plug them into the definitions
1344 * In particular, an expression of the form
1346 * floor((f(..) + floor(g(..)/n))/m)
1350 * floor((n * f(..) + g(..))/(n * m))
1352 * This simplification is correct because we can move the expression
1353 * f(..) into the inner floor in the original expression to obtain
1355 * floor(floor((n * f(..) + g(..))/n)/m)
1357 * from which we can derive the simplified expression.
1359 static __isl_give isl_aff
*plug_in_unit_divs(__isl_take isl_aff
*aff
)
1367 n
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
1368 off
= isl_local_space_offset(aff
->ls
, isl_dim_div
);
1369 for (i
= 1; i
< n
; ++i
) {
1370 for (j
= 0; j
< i
; ++j
) {
1371 if (!isl_int_is_one(aff
->ls
->div
->row
[i
][1 + off
+ j
]))
1373 aff
->ls
= isl_local_space_substitute_seq(aff
->ls
,
1374 isl_dim_div
, j
, aff
->ls
->div
->row
[j
],
1375 aff
->v
->size
, i
, 1);
1377 return isl_aff_free(aff
);
1384 /* Swap divs "a" and "b" in "aff", which is assumed to be non-NULL.
1386 * Even though this function is only called on isl_affs with a single
1387 * reference, we are careful to only change aff->v and aff->ls together.
1389 static __isl_give isl_aff
*swap_div(__isl_take isl_aff
*aff
, int a
, int b
)
1391 unsigned off
= isl_local_space_offset(aff
->ls
, isl_dim_div
);
1392 isl_local_space
*ls
;
1395 ls
= isl_local_space_copy(aff
->ls
);
1396 ls
= isl_local_space_swap_div(ls
, a
, b
);
1397 v
= isl_vec_copy(aff
->v
);
1402 isl_int_swap(v
->el
[1 + off
+ a
], v
->el
[1 + off
+ b
]);
1403 isl_vec_free(aff
->v
);
1405 isl_local_space_free(aff
->ls
);
1411 isl_local_space_free(ls
);
1412 return isl_aff_free(aff
);
1415 /* Merge divs "a" and "b" in "aff", which is assumed to be non-NULL.
1417 * We currently do not actually remove div "b", but simply add its
1418 * coefficient to that of "a" and then zero it out.
1420 static __isl_give isl_aff
*merge_divs(__isl_take isl_aff
*aff
, int a
, int b
)
1422 unsigned off
= isl_local_space_offset(aff
->ls
, isl_dim_div
);
1424 if (isl_int_is_zero(aff
->v
->el
[1 + off
+ b
]))
1427 aff
->v
= isl_vec_cow(aff
->v
);
1429 return isl_aff_free(aff
);
1431 isl_int_add(aff
->v
->el
[1 + off
+ a
],
1432 aff
->v
->el
[1 + off
+ a
], aff
->v
->el
[1 + off
+ b
]);
1433 isl_int_set_si(aff
->v
->el
[1 + off
+ b
], 0);
1438 /* Sort the divs in the local space of "aff" according to
1439 * the comparison function "cmp_row" in isl_local_space.c,
1440 * combining the coefficients of identical divs.
1442 * Reordering divs does not change the semantics of "aff",
1443 * so there is no need to call isl_aff_cow.
1444 * Moreover, this function is currently only called on isl_affs
1445 * with a single reference.
1447 static __isl_give isl_aff
*sort_divs(__isl_take isl_aff
*aff
)
1454 n
= isl_aff_dim(aff
, isl_dim_div
);
1455 for (i
= 1; i
< n
; ++i
) {
1456 for (j
= i
- 1; j
>= 0; --j
) {
1457 int cmp
= isl_mat_cmp_div(aff
->ls
->div
, j
, j
+ 1);
1461 aff
= merge_divs(aff
, j
, j
+ 1);
1463 aff
= swap_div(aff
, j
, j
+ 1);
1472 /* Normalize the representation of "aff".
1474 * This function should only be called of "new" isl_affs, i.e.,
1475 * with only a single reference. We therefore do not need to
1476 * worry about affecting other instances.
1478 __isl_give isl_aff
*isl_aff_normalize(__isl_take isl_aff
*aff
)
1482 aff
->v
= isl_vec_normalize(aff
->v
);
1484 return isl_aff_free(aff
);
1485 aff
= plug_in_integral_divs(aff
);
1486 aff
= plug_in_unit_divs(aff
);
1487 aff
= sort_divs(aff
);
1488 aff
= isl_aff_remove_unused_divs(aff
);
1492 /* Given f, return floor(f).
1493 * If f is an integer expression, then just return f.
1494 * If f is a constant, then return the constant floor(f).
1495 * Otherwise, if f = g/m, write g = q m + r,
1496 * create a new div d = [r/m] and return the expression q + d.
1497 * The coefficients in r are taken to lie between -m/2 and m/2.
1499 * reduce_div_coefficients performs the same normalization.
1501 * As a special case, floor(NaN) = NaN.
1503 __isl_give isl_aff
*isl_aff_floor(__isl_take isl_aff
*aff
)
1513 if (isl_aff_is_nan(aff
))
1515 if (isl_int_is_one(aff
->v
->el
[0]))
1518 aff
= isl_aff_cow(aff
);
1522 aff
->v
= isl_vec_cow(aff
->v
);
1524 return isl_aff_free(aff
);
1526 if (isl_aff_is_cst(aff
)) {
1527 isl_int_fdiv_q(aff
->v
->el
[1], aff
->v
->el
[1], aff
->v
->el
[0]);
1528 isl_int_set_si(aff
->v
->el
[0], 1);
1532 div
= isl_vec_copy(aff
->v
);
1533 div
= isl_vec_cow(div
);
1535 return isl_aff_free(aff
);
1537 ctx
= isl_aff_get_ctx(aff
);
1538 isl_int_fdiv_q(aff
->v
->el
[0], aff
->v
->el
[0], ctx
->two
);
1539 for (i
= 1; i
< aff
->v
->size
; ++i
) {
1540 isl_int_fdiv_r(div
->el
[i
], div
->el
[i
], div
->el
[0]);
1541 isl_int_fdiv_q(aff
->v
->el
[i
], aff
->v
->el
[i
], div
->el
[0]);
1542 if (isl_int_gt(div
->el
[i
], aff
->v
->el
[0])) {
1543 isl_int_sub(div
->el
[i
], div
->el
[i
], div
->el
[0]);
1544 isl_int_add_ui(aff
->v
->el
[i
], aff
->v
->el
[i
], 1);
1548 aff
->ls
= isl_local_space_add_div(aff
->ls
, div
);
1550 return isl_aff_free(aff
);
1552 size
= aff
->v
->size
;
1553 aff
->v
= isl_vec_extend(aff
->v
, size
+ 1);
1555 return isl_aff_free(aff
);
1556 isl_int_set_si(aff
->v
->el
[0], 1);
1557 isl_int_set_si(aff
->v
->el
[size
], 1);
1559 aff
= isl_aff_normalize(aff
);
1566 * aff mod m = aff - m * floor(aff/m)
1568 * with m an integer value.
1570 __isl_give isl_aff
*isl_aff_mod_val(__isl_take isl_aff
*aff
,
1571 __isl_take isl_val
*m
)
1578 if (!isl_val_is_int(m
))
1579 isl_die(isl_val_get_ctx(m
), isl_error_invalid
,
1580 "expecting integer modulo", goto error
);
1582 res
= isl_aff_copy(aff
);
1583 aff
= isl_aff_scale_down_val(aff
, isl_val_copy(m
));
1584 aff
= isl_aff_floor(aff
);
1585 aff
= isl_aff_scale_val(aff
, m
);
1586 res
= isl_aff_sub(res
, aff
);
1597 * pwaff mod m = pwaff - m * floor(pwaff/m)
1599 __isl_give isl_pw_aff
*isl_pw_aff_mod(__isl_take isl_pw_aff
*pwaff
, isl_int m
)
1603 res
= isl_pw_aff_copy(pwaff
);
1604 pwaff
= isl_pw_aff_scale_down(pwaff
, m
);
1605 pwaff
= isl_pw_aff_floor(pwaff
);
1606 pwaff
= isl_pw_aff_scale(pwaff
, m
);
1607 res
= isl_pw_aff_sub(res
, pwaff
);
1614 * pa mod m = pa - m * floor(pa/m)
1616 * with m an integer value.
1618 __isl_give isl_pw_aff
*isl_pw_aff_mod_val(__isl_take isl_pw_aff
*pa
,
1619 __isl_take isl_val
*m
)
1623 if (!isl_val_is_int(m
))
1624 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
1625 "expecting integer modulo", goto error
);
1626 pa
= isl_pw_aff_mod(pa
, m
->n
);
1630 isl_pw_aff_free(pa
);
1635 /* Given f, return ceil(f).
1636 * If f is an integer expression, then just return f.
1637 * Otherwise, let f be the expression
1643 * floor((e + m - 1)/m)
1645 * As a special case, ceil(NaN) = NaN.
1647 __isl_give isl_aff
*isl_aff_ceil(__isl_take isl_aff
*aff
)
1652 if (isl_aff_is_nan(aff
))
1654 if (isl_int_is_one(aff
->v
->el
[0]))
1657 aff
= isl_aff_cow(aff
);
1660 aff
->v
= isl_vec_cow(aff
->v
);
1662 return isl_aff_free(aff
);
1664 isl_int_add(aff
->v
->el
[1], aff
->v
->el
[1], aff
->v
->el
[0]);
1665 isl_int_sub_ui(aff
->v
->el
[1], aff
->v
->el
[1], 1);
1666 aff
= isl_aff_floor(aff
);
1671 /* Apply the expansion computed by isl_merge_divs.
1672 * The expansion itself is given by "exp" while the resulting
1673 * list of divs is given by "div".
1675 __isl_give isl_aff
*isl_aff_expand_divs(__isl_take isl_aff
*aff
,
1676 __isl_take isl_mat
*div
, int *exp
)
1682 aff
= isl_aff_cow(aff
);
1686 old_n_div
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
1687 new_n_div
= isl_mat_rows(div
);
1688 offset
= 1 + isl_local_space_offset(aff
->ls
, isl_dim_div
);
1690 aff
->v
= isl_vec_expand(aff
->v
, offset
, old_n_div
, exp
, new_n_div
);
1691 aff
->ls
= isl_local_space_replace_divs(aff
->ls
, div
);
1692 if (!aff
->v
|| !aff
->ls
)
1693 return isl_aff_free(aff
);
1701 /* Add two affine expressions that live in the same local space.
1703 static __isl_give isl_aff
*add_expanded(__isl_take isl_aff
*aff1
,
1704 __isl_take isl_aff
*aff2
)
1708 aff1
= isl_aff_cow(aff1
);
1712 aff1
->v
= isl_vec_cow(aff1
->v
);
1718 isl_int_gcd(gcd
, aff1
->v
->el
[0], aff2
->v
->el
[0]);
1719 isl_int_divexact(f
, aff2
->v
->el
[0], gcd
);
1720 isl_seq_scale(aff1
->v
->el
+ 1, aff1
->v
->el
+ 1, f
, aff1
->v
->size
- 1);
1721 isl_int_divexact(f
, aff1
->v
->el
[0], gcd
);
1722 isl_seq_addmul(aff1
->v
->el
+ 1, f
, aff2
->v
->el
+ 1, aff1
->v
->size
- 1);
1723 isl_int_divexact(f
, aff2
->v
->el
[0], gcd
);
1724 isl_int_mul(aff1
->v
->el
[0], aff1
->v
->el
[0], f
);
1736 /* Return the sum of "aff1" and "aff2".
1738 * If either of the two is NaN, then the result is NaN.
1740 __isl_give isl_aff
*isl_aff_add(__isl_take isl_aff
*aff1
,
1741 __isl_take isl_aff
*aff2
)
1752 ctx
= isl_aff_get_ctx(aff1
);
1753 if (!isl_space_is_equal(aff1
->ls
->dim
, aff2
->ls
->dim
))
1754 isl_die(ctx
, isl_error_invalid
,
1755 "spaces don't match", goto error
);
1757 if (isl_aff_is_nan(aff1
)) {
1761 if (isl_aff_is_nan(aff2
)) {
1766 n_div1
= isl_aff_dim(aff1
, isl_dim_div
);
1767 n_div2
= isl_aff_dim(aff2
, isl_dim_div
);
1768 if (n_div1
== 0 && n_div2
== 0)
1769 return add_expanded(aff1
, aff2
);
1771 exp1
= isl_alloc_array(ctx
, int, n_div1
);
1772 exp2
= isl_alloc_array(ctx
, int, n_div2
);
1773 if ((n_div1
&& !exp1
) || (n_div2
&& !exp2
))
1776 div
= isl_merge_divs(aff1
->ls
->div
, aff2
->ls
->div
, exp1
, exp2
);
1777 aff1
= isl_aff_expand_divs(aff1
, isl_mat_copy(div
), exp1
);
1778 aff2
= isl_aff_expand_divs(aff2
, div
, exp2
);
1782 return add_expanded(aff1
, aff2
);
1791 __isl_give isl_aff
*isl_aff_sub(__isl_take isl_aff
*aff1
,
1792 __isl_take isl_aff
*aff2
)
1794 return isl_aff_add(aff1
, isl_aff_neg(aff2
));
1797 /* Return the result of scaling "aff" by a factor of "f".
1799 * As a special case, f * NaN = NaN.
1801 __isl_give isl_aff
*isl_aff_scale(__isl_take isl_aff
*aff
, isl_int f
)
1807 if (isl_aff_is_nan(aff
))
1810 if (isl_int_is_one(f
))
1813 aff
= isl_aff_cow(aff
);
1816 aff
->v
= isl_vec_cow(aff
->v
);
1818 return isl_aff_free(aff
);
1820 if (isl_int_is_pos(f
) && isl_int_is_divisible_by(aff
->v
->el
[0], f
)) {
1821 isl_int_divexact(aff
->v
->el
[0], aff
->v
->el
[0], f
);
1826 isl_int_gcd(gcd
, aff
->v
->el
[0], f
);
1827 isl_int_divexact(aff
->v
->el
[0], aff
->v
->el
[0], gcd
);
1828 isl_int_divexact(gcd
, f
, gcd
);
1829 isl_seq_scale(aff
->v
->el
+ 1, aff
->v
->el
+ 1, gcd
, aff
->v
->size
- 1);
1835 /* Multiple "aff" by "v".
1837 __isl_give isl_aff
*isl_aff_scale_val(__isl_take isl_aff
*aff
,
1838 __isl_take isl_val
*v
)
1843 if (isl_val_is_one(v
)) {
1848 if (!isl_val_is_rat(v
))
1849 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1850 "expecting rational factor", goto error
);
1852 aff
= isl_aff_scale(aff
, v
->n
);
1853 aff
= isl_aff_scale_down(aff
, v
->d
);
1863 /* Return the result of scaling "aff" down by a factor of "f".
1865 * As a special case, NaN/f = NaN.
1867 __isl_give isl_aff
*isl_aff_scale_down(__isl_take isl_aff
*aff
, isl_int f
)
1873 if (isl_aff_is_nan(aff
))
1876 if (isl_int_is_one(f
))
1879 aff
= isl_aff_cow(aff
);
1883 if (isl_int_is_zero(f
))
1884 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1885 "cannot scale down by zero", return isl_aff_free(aff
));
1887 aff
->v
= isl_vec_cow(aff
->v
);
1889 return isl_aff_free(aff
);
1892 isl_seq_gcd(aff
->v
->el
+ 1, aff
->v
->size
- 1, &gcd
);
1893 isl_int_gcd(gcd
, gcd
, f
);
1894 isl_seq_scale_down(aff
->v
->el
+ 1, aff
->v
->el
+ 1, gcd
, aff
->v
->size
- 1);
1895 isl_int_divexact(gcd
, f
, gcd
);
1896 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], gcd
);
1902 /* Divide "aff" by "v".
1904 __isl_give isl_aff
*isl_aff_scale_down_val(__isl_take isl_aff
*aff
,
1905 __isl_take isl_val
*v
)
1910 if (isl_val_is_one(v
)) {
1915 if (!isl_val_is_rat(v
))
1916 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1917 "expecting rational factor", goto error
);
1918 if (!isl_val_is_pos(v
))
1919 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1920 "factor needs to be positive", goto error
);
1922 aff
= isl_aff_scale(aff
, v
->d
);
1923 aff
= isl_aff_scale_down(aff
, v
->n
);
1933 __isl_give isl_aff
*isl_aff_scale_down_ui(__isl_take isl_aff
*aff
, unsigned f
)
1941 isl_int_set_ui(v
, f
);
1942 aff
= isl_aff_scale_down(aff
, v
);
1948 __isl_give isl_aff
*isl_aff_set_dim_name(__isl_take isl_aff
*aff
,
1949 enum isl_dim_type type
, unsigned pos
, const char *s
)
1951 aff
= isl_aff_cow(aff
);
1954 if (type
== isl_dim_out
)
1955 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1956 "cannot set name of output/set dimension",
1957 return isl_aff_free(aff
));
1958 if (type
== isl_dim_in
)
1960 aff
->ls
= isl_local_space_set_dim_name(aff
->ls
, type
, pos
, s
);
1962 return isl_aff_free(aff
);
1967 __isl_give isl_aff
*isl_aff_set_dim_id(__isl_take isl_aff
*aff
,
1968 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
1970 aff
= isl_aff_cow(aff
);
1973 if (type
== isl_dim_out
)
1974 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1975 "cannot set name of output/set dimension",
1977 if (type
== isl_dim_in
)
1979 aff
->ls
= isl_local_space_set_dim_id(aff
->ls
, type
, pos
, id
);
1981 return isl_aff_free(aff
);
1990 /* Replace the identifier of the input tuple of "aff" by "id".
1991 * type is currently required to be equal to isl_dim_in
1993 __isl_give isl_aff
*isl_aff_set_tuple_id(__isl_take isl_aff
*aff
,
1994 enum isl_dim_type type
, __isl_take isl_id
*id
)
1996 aff
= isl_aff_cow(aff
);
1999 if (type
!= isl_dim_in
)
2000 isl_die(aff
->v
->ctx
, isl_error_invalid
,
2001 "cannot only set id of input tuple", goto error
);
2002 aff
->ls
= isl_local_space_set_tuple_id(aff
->ls
, isl_dim_set
, id
);
2004 return isl_aff_free(aff
);
2013 /* Exploit the equalities in "eq" to simplify the affine expression
2014 * and the expressions of the integer divisions in the local space.
2015 * The integer divisions in this local space are assumed to appear
2016 * as regular dimensions in "eq".
2018 static __isl_give isl_aff
*isl_aff_substitute_equalities_lifted(
2019 __isl_take isl_aff
*aff
, __isl_take isl_basic_set
*eq
)
2027 if (eq
->n_eq
== 0) {
2028 isl_basic_set_free(eq
);
2032 aff
= isl_aff_cow(aff
);
2036 aff
->ls
= isl_local_space_substitute_equalities(aff
->ls
,
2037 isl_basic_set_copy(eq
));
2038 aff
->v
= isl_vec_cow(aff
->v
);
2039 if (!aff
->ls
|| !aff
->v
)
2042 total
= 1 + isl_space_dim(eq
->dim
, isl_dim_all
);
2044 for (i
= 0; i
< eq
->n_eq
; ++i
) {
2045 j
= isl_seq_last_non_zero(eq
->eq
[i
], total
+ n_div
);
2046 if (j
< 0 || j
== 0 || j
>= total
)
2049 isl_seq_elim(aff
->v
->el
+ 1, eq
->eq
[i
], j
, total
,
2053 isl_basic_set_free(eq
);
2054 aff
= isl_aff_normalize(aff
);
2057 isl_basic_set_free(eq
);
2062 /* Exploit the equalities in "eq" to simplify the affine expression
2063 * and the expressions of the integer divisions in the local space.
2065 __isl_give isl_aff
*isl_aff_substitute_equalities(__isl_take isl_aff
*aff
,
2066 __isl_take isl_basic_set
*eq
)
2072 n_div
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
2074 eq
= isl_basic_set_add_dims(eq
, isl_dim_set
, n_div
);
2075 return isl_aff_substitute_equalities_lifted(aff
, eq
);
2077 isl_basic_set_free(eq
);
2082 /* Look for equalities among the variables shared by context and aff
2083 * and the integer divisions of aff, if any.
2084 * The equalities are then used to eliminate coefficients and/or integer
2085 * divisions from aff.
2087 __isl_give isl_aff
*isl_aff_gist(__isl_take isl_aff
*aff
,
2088 __isl_take isl_set
*context
)
2090 isl_basic_set
*hull
;
2095 n_div
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
2097 isl_basic_set
*bset
;
2098 isl_local_space
*ls
;
2099 context
= isl_set_add_dims(context
, isl_dim_set
, n_div
);
2100 ls
= isl_aff_get_domain_local_space(aff
);
2101 bset
= isl_basic_set_from_local_space(ls
);
2102 bset
= isl_basic_set_lift(bset
);
2103 bset
= isl_basic_set_flatten(bset
);
2104 context
= isl_set_intersect(context
,
2105 isl_set_from_basic_set(bset
));
2108 hull
= isl_set_affine_hull(context
);
2109 return isl_aff_substitute_equalities_lifted(aff
, hull
);
2112 isl_set_free(context
);
2116 __isl_give isl_aff
*isl_aff_gist_params(__isl_take isl_aff
*aff
,
2117 __isl_take isl_set
*context
)
2119 isl_set
*dom_context
= isl_set_universe(isl_aff_get_domain_space(aff
));
2120 dom_context
= isl_set_intersect_params(dom_context
, context
);
2121 return isl_aff_gist(aff
, dom_context
);
2124 /* Return a basic set containing those elements in the space
2125 * of aff where it is positive. "rational" should not be set.
2127 * If "aff" is NaN, then it is not positive.
2129 static __isl_give isl_basic_set
*aff_pos_basic_set(__isl_take isl_aff
*aff
,
2132 isl_constraint
*ineq
;
2133 isl_basic_set
*bset
;
2138 if (isl_aff_is_nan(aff
)) {
2139 isl_space
*space
= isl_aff_get_domain_space(aff
);
2141 return isl_basic_set_empty(space
);
2144 isl_die(isl_aff_get_ctx(aff
), isl_error_unsupported
,
2145 "rational sets not supported", goto error
);
2147 ineq
= isl_inequality_from_aff(aff
);
2148 c
= isl_constraint_get_constant_val(ineq
);
2149 c
= isl_val_sub_ui(c
, 1);
2150 ineq
= isl_constraint_set_constant_val(ineq
, c
);
2152 bset
= isl_basic_set_from_constraint(ineq
);
2153 bset
= isl_basic_set_simplify(bset
);
2160 /* Return a basic set containing those elements in the space
2161 * of aff where it is non-negative.
2162 * If "rational" is set, then return a rational basic set.
2164 * If "aff" is NaN, then it is not non-negative (it's not negative either).
2166 static __isl_give isl_basic_set
*aff_nonneg_basic_set(
2167 __isl_take isl_aff
*aff
, int rational
)
2169 isl_constraint
*ineq
;
2170 isl_basic_set
*bset
;
2174 if (isl_aff_is_nan(aff
)) {
2175 isl_space
*space
= isl_aff_get_domain_space(aff
);
2177 return isl_basic_set_empty(space
);
2180 ineq
= isl_inequality_from_aff(aff
);
2182 bset
= isl_basic_set_from_constraint(ineq
);
2184 bset
= isl_basic_set_set_rational(bset
);
2185 bset
= isl_basic_set_simplify(bset
);
2189 /* Return a basic set containing those elements in the space
2190 * of aff where it is non-negative.
2192 __isl_give isl_basic_set
*isl_aff_nonneg_basic_set(__isl_take isl_aff
*aff
)
2194 return aff_nonneg_basic_set(aff
, 0);
2197 /* Return a basic set containing those elements in the domain space
2198 * of "aff" where it is positive.
2200 __isl_give isl_basic_set
*isl_aff_pos_basic_set(__isl_take isl_aff
*aff
)
2202 aff
= isl_aff_add_constant_num_si(aff
, -1);
2203 return isl_aff_nonneg_basic_set(aff
);
2206 /* Return a basic set containing those elements in the domain space
2207 * of aff where it is negative.
2209 __isl_give isl_basic_set
*isl_aff_neg_basic_set(__isl_take isl_aff
*aff
)
2211 aff
= isl_aff_neg(aff
);
2212 return isl_aff_pos_basic_set(aff
);
2215 /* Return a basic set containing those elements in the space
2216 * of aff where it is zero.
2217 * If "rational" is set, then return a rational basic set.
2219 * If "aff" is NaN, then it is not zero.
2221 static __isl_give isl_basic_set
*aff_zero_basic_set(__isl_take isl_aff
*aff
,
2224 isl_constraint
*ineq
;
2225 isl_basic_set
*bset
;
2229 if (isl_aff_is_nan(aff
)) {
2230 isl_space
*space
= isl_aff_get_domain_space(aff
);
2232 return isl_basic_set_empty(space
);
2235 ineq
= isl_equality_from_aff(aff
);
2237 bset
= isl_basic_set_from_constraint(ineq
);
2239 bset
= isl_basic_set_set_rational(bset
);
2240 bset
= isl_basic_set_simplify(bset
);
2244 /* Return a basic set containing those elements in the space
2245 * of aff where it is zero.
2247 __isl_give isl_basic_set
*isl_aff_zero_basic_set(__isl_take isl_aff
*aff
)
2249 return aff_zero_basic_set(aff
, 0);
2252 /* Return a basic set containing those elements in the shared space
2253 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
2255 __isl_give isl_basic_set
*isl_aff_ge_basic_set(__isl_take isl_aff
*aff1
,
2256 __isl_take isl_aff
*aff2
)
2258 aff1
= isl_aff_sub(aff1
, aff2
);
2260 return isl_aff_nonneg_basic_set(aff1
);
2263 /* Return a basic set containing those elements in the shared domain space
2264 * of "aff1" and "aff2" where "aff1" is greater than "aff2".
2266 __isl_give isl_basic_set
*isl_aff_gt_basic_set(__isl_take isl_aff
*aff1
,
2267 __isl_take isl_aff
*aff2
)
2269 aff1
= isl_aff_sub(aff1
, aff2
);
2271 return isl_aff_pos_basic_set(aff1
);
2274 /* Return a set containing those elements in the shared space
2275 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
2277 __isl_give isl_set
*isl_aff_ge_set(__isl_take isl_aff
*aff1
,
2278 __isl_take isl_aff
*aff2
)
2280 return isl_set_from_basic_set(isl_aff_ge_basic_set(aff1
, aff2
));
2283 /* Return a set containing those elements in the shared domain space
2284 * of aff1 and aff2 where aff1 is greater than aff2.
2286 * If either of the two inputs is NaN, then the result is empty,
2287 * as comparisons with NaN always return false.
2289 __isl_give isl_set
*isl_aff_gt_set(__isl_take isl_aff
*aff1
,
2290 __isl_take isl_aff
*aff2
)
2292 return isl_set_from_basic_set(isl_aff_gt_basic_set(aff1
, aff2
));
2295 /* Return a basic set containing those elements in the shared space
2296 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
2298 __isl_give isl_basic_set
*isl_aff_le_basic_set(__isl_take isl_aff
*aff1
,
2299 __isl_take isl_aff
*aff2
)
2301 return isl_aff_ge_basic_set(aff2
, aff1
);
2304 /* Return a basic set containing those elements in the shared domain space
2305 * of "aff1" and "aff2" where "aff1" is smaller than "aff2".
2307 __isl_give isl_basic_set
*isl_aff_lt_basic_set(__isl_take isl_aff
*aff1
,
2308 __isl_take isl_aff
*aff2
)
2310 return isl_aff_gt_basic_set(aff2
, aff1
);
2313 /* Return a set containing those elements in the shared space
2314 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
2316 __isl_give isl_set
*isl_aff_le_set(__isl_take isl_aff
*aff1
,
2317 __isl_take isl_aff
*aff2
)
2319 return isl_aff_ge_set(aff2
, aff1
);
2322 /* Return a set containing those elements in the shared domain space
2323 * of "aff1" and "aff2" where "aff1" is smaller than "aff2".
2325 __isl_give isl_set
*isl_aff_lt_set(__isl_take isl_aff
*aff1
,
2326 __isl_take isl_aff
*aff2
)
2328 return isl_set_from_basic_set(isl_aff_lt_basic_set(aff1
, aff2
));
2331 /* Return a basic set containing those elements in the shared space
2332 * of aff1 and aff2 where aff1 and aff2 are equal.
2334 __isl_give isl_basic_set
*isl_aff_eq_basic_set(__isl_take isl_aff
*aff1
,
2335 __isl_take isl_aff
*aff2
)
2337 aff1
= isl_aff_sub(aff1
, aff2
);
2339 return isl_aff_zero_basic_set(aff1
);
2342 /* Return a set containing those elements in the shared space
2343 * of aff1 and aff2 where aff1 and aff2 are equal.
2345 __isl_give isl_set
*isl_aff_eq_set(__isl_take isl_aff
*aff1
,
2346 __isl_take isl_aff
*aff2
)
2348 return isl_set_from_basic_set(isl_aff_eq_basic_set(aff1
, aff2
));
2351 /* Return a set containing those elements in the shared domain space
2352 * of aff1 and aff2 where aff1 and aff2 are not equal.
2354 * If either of the two inputs is NaN, then the result is empty,
2355 * as comparisons with NaN always return false.
2357 __isl_give isl_set
*isl_aff_ne_set(__isl_take isl_aff
*aff1
,
2358 __isl_take isl_aff
*aff2
)
2360 isl_set
*set_lt
, *set_gt
;
2362 set_lt
= isl_aff_lt_set(isl_aff_copy(aff1
),
2363 isl_aff_copy(aff2
));
2364 set_gt
= isl_aff_gt_set(aff1
, aff2
);
2365 return isl_set_union_disjoint(set_lt
, set_gt
);
2368 __isl_give isl_aff
*isl_aff_add_on_domain(__isl_keep isl_set
*dom
,
2369 __isl_take isl_aff
*aff1
, __isl_take isl_aff
*aff2
)
2371 aff1
= isl_aff_add(aff1
, aff2
);
2372 aff1
= isl_aff_gist(aff1
, isl_set_copy(dom
));
2376 int isl_aff_is_empty(__isl_keep isl_aff
*aff
)
2384 /* Check whether the given affine expression has non-zero coefficient
2385 * for any dimension in the given range or if any of these dimensions
2386 * appear with non-zero coefficients in any of the integer divisions
2387 * involved in the affine expression.
2389 isl_bool
isl_aff_involves_dims(__isl_keep isl_aff
*aff
,
2390 enum isl_dim_type type
, unsigned first
, unsigned n
)
2395 isl_bool involves
= isl_bool_false
;
2398 return isl_bool_error
;
2400 return isl_bool_false
;
2402 ctx
= isl_aff_get_ctx(aff
);
2403 if (first
+ n
> isl_aff_dim(aff
, type
))
2404 isl_die(ctx
, isl_error_invalid
,
2405 "range out of bounds", return isl_bool_error
);
2407 active
= isl_local_space_get_active(aff
->ls
, aff
->v
->el
+ 2);
2411 first
+= isl_local_space_offset(aff
->ls
, type
) - 1;
2412 for (i
= 0; i
< n
; ++i
)
2413 if (active
[first
+ i
]) {
2414 involves
= isl_bool_true
;
2423 return isl_bool_error
;
2426 __isl_give isl_aff
*isl_aff_drop_dims(__isl_take isl_aff
*aff
,
2427 enum isl_dim_type type
, unsigned first
, unsigned n
)
2433 if (type
== isl_dim_out
)
2434 isl_die(aff
->v
->ctx
, isl_error_invalid
,
2435 "cannot drop output/set dimension",
2436 return isl_aff_free(aff
));
2437 if (type
== isl_dim_in
)
2439 if (n
== 0 && !isl_local_space_is_named_or_nested(aff
->ls
, type
))
2442 ctx
= isl_aff_get_ctx(aff
);
2443 if (first
+ n
> isl_local_space_dim(aff
->ls
, type
))
2444 isl_die(ctx
, isl_error_invalid
, "range out of bounds",
2445 return isl_aff_free(aff
));
2447 aff
= isl_aff_cow(aff
);
2451 aff
->ls
= isl_local_space_drop_dims(aff
->ls
, type
, first
, n
);
2453 return isl_aff_free(aff
);
2455 first
+= 1 + isl_local_space_offset(aff
->ls
, type
);
2456 aff
->v
= isl_vec_drop_els(aff
->v
, first
, n
);
2458 return isl_aff_free(aff
);
2463 /* Drop the "n" domain dimensions starting at "first" from "aff",
2464 * after checking that they do not appear in the affine expression.
2466 static __isl_give isl_aff
*drop_domain(__isl_take isl_aff
*aff
, unsigned first
,
2471 involves
= isl_aff_involves_dims(aff
, isl_dim_in
, first
, n
);
2473 return isl_aff_free(aff
);
2475 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2476 "affine expression involves some of the domain dimensions",
2477 return isl_aff_free(aff
));
2478 return isl_aff_drop_dims(aff
, isl_dim_in
, first
, n
);
2481 /* Project the domain of the affine expression onto its parameter space.
2482 * The affine expression may not involve any of the domain dimensions.
2484 __isl_give isl_aff
*isl_aff_project_domain_on_params(__isl_take isl_aff
*aff
)
2489 n
= isl_aff_dim(aff
, isl_dim_in
);
2490 aff
= drop_domain(aff
, 0, n
);
2491 space
= isl_aff_get_domain_space(aff
);
2492 space
= isl_space_params(space
);
2493 aff
= isl_aff_reset_domain_space(aff
, space
);
2497 /* Check that the domain of "aff" is a product.
2499 static isl_stat
check_domain_product(__isl_keep isl_aff
*aff
)
2501 isl_bool is_product
;
2503 is_product
= isl_space_is_product(isl_aff_peek_domain_space(aff
));
2505 return isl_stat_error
;
2507 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2508 "domain is not a product", return isl_stat_error
);
2512 /* Given an affine function with a domain of the form [A -> B] that
2513 * does not depend on B, return the same function on domain A.
2515 __isl_give isl_aff
*isl_aff_domain_factor_domain(__isl_take isl_aff
*aff
)
2520 if (check_domain_product(aff
) < 0)
2521 return isl_aff_free(aff
);
2522 space
= isl_aff_get_domain_space(aff
);
2523 n
= isl_space_dim(space
, isl_dim_set
);
2524 space
= isl_space_factor_domain(space
);
2525 n_in
= isl_space_dim(space
, isl_dim_set
);
2526 aff
= drop_domain(aff
, n_in
, n
- n_in
);
2527 aff
= isl_aff_reset_domain_space(aff
, space
);
2531 /* Convert an affine expression defined over a parameter domain
2532 * into one that is defined over a zero-dimensional set.
2534 __isl_give isl_aff
*isl_aff_from_range(__isl_take isl_aff
*aff
)
2536 isl_local_space
*ls
;
2538 ls
= isl_aff_take_domain_local_space(aff
);
2539 ls
= isl_local_space_set_from_params(ls
);
2540 aff
= isl_aff_restore_domain_local_space(aff
, ls
);
2545 __isl_give isl_aff
*isl_aff_insert_dims(__isl_take isl_aff
*aff
,
2546 enum isl_dim_type type
, unsigned first
, unsigned n
)
2552 if (type
== isl_dim_out
)
2553 isl_die(aff
->v
->ctx
, isl_error_invalid
,
2554 "cannot insert output/set dimensions",
2555 return isl_aff_free(aff
));
2556 if (type
== isl_dim_in
)
2558 if (n
== 0 && !isl_local_space_is_named_or_nested(aff
->ls
, type
))
2561 ctx
= isl_aff_get_ctx(aff
);
2562 if (first
> isl_local_space_dim(aff
->ls
, type
))
2563 isl_die(ctx
, isl_error_invalid
, "position out of bounds",
2564 return isl_aff_free(aff
));
2566 aff
= isl_aff_cow(aff
);
2570 aff
->ls
= isl_local_space_insert_dims(aff
->ls
, type
, first
, n
);
2572 return isl_aff_free(aff
);
2574 first
+= 1 + isl_local_space_offset(aff
->ls
, type
);
2575 aff
->v
= isl_vec_insert_zero_els(aff
->v
, first
, n
);
2577 return isl_aff_free(aff
);
2582 __isl_give isl_aff
*isl_aff_add_dims(__isl_take isl_aff
*aff
,
2583 enum isl_dim_type type
, unsigned n
)
2587 pos
= isl_aff_dim(aff
, type
);
2589 return isl_aff_insert_dims(aff
, type
, pos
, n
);
2592 __isl_give isl_pw_aff
*isl_pw_aff_add_dims(__isl_take isl_pw_aff
*pwaff
,
2593 enum isl_dim_type type
, unsigned n
)
2597 pos
= isl_pw_aff_dim(pwaff
, type
);
2599 return isl_pw_aff_insert_dims(pwaff
, type
, pos
, n
);
2602 /* Move the "n" dimensions of "src_type" starting at "src_pos" of "aff"
2603 * to dimensions of "dst_type" at "dst_pos".
2605 * We only support moving input dimensions to parameters and vice versa.
2607 __isl_give isl_aff
*isl_aff_move_dims(__isl_take isl_aff
*aff
,
2608 enum isl_dim_type dst_type
, unsigned dst_pos
,
2609 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
2617 !isl_local_space_is_named_or_nested(aff
->ls
, src_type
) &&
2618 !isl_local_space_is_named_or_nested(aff
->ls
, dst_type
))
2621 if (dst_type
== isl_dim_out
|| src_type
== isl_dim_out
)
2622 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2623 "cannot move output/set dimension",
2624 return isl_aff_free(aff
));
2625 if (dst_type
== isl_dim_div
|| src_type
== isl_dim_div
)
2626 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2627 "cannot move divs", return isl_aff_free(aff
));
2628 if (dst_type
== isl_dim_in
)
2629 dst_type
= isl_dim_set
;
2630 if (src_type
== isl_dim_in
)
2631 src_type
= isl_dim_set
;
2633 if (src_pos
+ n
> isl_local_space_dim(aff
->ls
, src_type
))
2634 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2635 "range out of bounds", return isl_aff_free(aff
));
2636 if (dst_type
== src_type
)
2637 isl_die(isl_aff_get_ctx(aff
), isl_error_unsupported
,
2638 "moving dims within the same type not supported",
2639 return isl_aff_free(aff
));
2641 aff
= isl_aff_cow(aff
);
2645 g_src_pos
= 1 + isl_local_space_offset(aff
->ls
, src_type
) + src_pos
;
2646 g_dst_pos
= 1 + isl_local_space_offset(aff
->ls
, dst_type
) + dst_pos
;
2647 if (dst_type
> src_type
)
2650 aff
->v
= isl_vec_move_els(aff
->v
, g_dst_pos
, g_src_pos
, n
);
2651 aff
->ls
= isl_local_space_move_dims(aff
->ls
, dst_type
, dst_pos
,
2652 src_type
, src_pos
, n
);
2653 if (!aff
->v
|| !aff
->ls
)
2654 return isl_aff_free(aff
);
2656 aff
= sort_divs(aff
);
2661 __isl_give isl_pw_aff
*isl_pw_aff_from_aff(__isl_take isl_aff
*aff
)
2663 isl_set
*dom
= isl_set_universe(isl_aff_get_domain_space(aff
));
2664 return isl_pw_aff_alloc(dom
, aff
);
2667 #define isl_aff_involves_nan isl_aff_is_nan
2670 #define PW isl_pw_aff
2674 #define EL_IS_ZERO is_empty
2678 #define IS_ZERO is_empty
2681 #undef DEFAULT_IS_ZERO
2682 #define DEFAULT_IS_ZERO 0
2688 #include <isl_pw_templ.c>
2689 #include <isl_pw_eval.c>
2690 #include <isl_pw_hash.c>
2691 #include <isl_pw_union_opt.c>
2694 #define UNION isl_union_pw_aff
2696 #define PART isl_pw_aff
2698 #define PARTS pw_aff
2700 #include <isl_union_single.c>
2701 #include <isl_union_neg.c>
2703 static __isl_give isl_set
*align_params_pw_pw_set_and(
2704 __isl_take isl_pw_aff
*pwaff1
, __isl_take isl_pw_aff
*pwaff2
,
2705 __isl_give isl_set
*(*fn
)(__isl_take isl_pw_aff
*pwaff1
,
2706 __isl_take isl_pw_aff
*pwaff2
))
2708 isl_bool equal_params
;
2710 if (!pwaff1
|| !pwaff2
)
2712 equal_params
= isl_space_has_equal_params(pwaff1
->dim
, pwaff2
->dim
);
2713 if (equal_params
< 0)
2716 return fn(pwaff1
, pwaff2
);
2717 if (isl_pw_aff_check_named_params(pwaff1
) < 0 ||
2718 isl_pw_aff_check_named_params(pwaff2
) < 0)
2720 pwaff1
= isl_pw_aff_align_params(pwaff1
, isl_pw_aff_get_space(pwaff2
));
2721 pwaff2
= isl_pw_aff_align_params(pwaff2
, isl_pw_aff_get_space(pwaff1
));
2722 return fn(pwaff1
, pwaff2
);
2724 isl_pw_aff_free(pwaff1
);
2725 isl_pw_aff_free(pwaff2
);
2729 /* Align the parameters of the to isl_pw_aff arguments and
2730 * then apply a function "fn" on them that returns an isl_map.
2732 static __isl_give isl_map
*align_params_pw_pw_map_and(
2733 __isl_take isl_pw_aff
*pa1
, __isl_take isl_pw_aff
*pa2
,
2734 __isl_give isl_map
*(*fn
)(__isl_take isl_pw_aff
*pa1
,
2735 __isl_take isl_pw_aff
*pa2
))
2737 isl_bool equal_params
;
2741 equal_params
= isl_space_has_equal_params(pa1
->dim
, pa2
->dim
);
2742 if (equal_params
< 0)
2745 return fn(pa1
, pa2
);
2746 if (isl_pw_aff_check_named_params(pa1
) < 0 ||
2747 isl_pw_aff_check_named_params(pa2
) < 0)
2749 pa1
= isl_pw_aff_align_params(pa1
, isl_pw_aff_get_space(pa2
));
2750 pa2
= isl_pw_aff_align_params(pa2
, isl_pw_aff_get_space(pa1
));
2751 return fn(pa1
, pa2
);
2753 isl_pw_aff_free(pa1
);
2754 isl_pw_aff_free(pa2
);
2758 /* Compute a piecewise quasi-affine expression with a domain that
2759 * is the union of those of pwaff1 and pwaff2 and such that on each
2760 * cell, the quasi-affine expression is the maximum of those of pwaff1
2761 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2762 * cell, then the associated expression is the defined one.
2764 static __isl_give isl_pw_aff
*pw_aff_union_max(__isl_take isl_pw_aff
*pwaff1
,
2765 __isl_take isl_pw_aff
*pwaff2
)
2767 return isl_pw_aff_union_opt_cmp(pwaff1
, pwaff2
, &isl_aff_ge_set
);
2770 __isl_give isl_pw_aff
*isl_pw_aff_union_max(__isl_take isl_pw_aff
*pwaff1
,
2771 __isl_take isl_pw_aff
*pwaff2
)
2773 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
,
2777 /* Compute a piecewise quasi-affine expression with a domain that
2778 * is the union of those of pwaff1 and pwaff2 and such that on each
2779 * cell, the quasi-affine expression is the minimum of those of pwaff1
2780 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2781 * cell, then the associated expression is the defined one.
2783 static __isl_give isl_pw_aff
*pw_aff_union_min(__isl_take isl_pw_aff
*pwaff1
,
2784 __isl_take isl_pw_aff
*pwaff2
)
2786 return isl_pw_aff_union_opt_cmp(pwaff1
, pwaff2
, &isl_aff_le_set
);
2789 __isl_give isl_pw_aff
*isl_pw_aff_union_min(__isl_take isl_pw_aff
*pwaff1
,
2790 __isl_take isl_pw_aff
*pwaff2
)
2792 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
,
2796 __isl_give isl_pw_aff
*isl_pw_aff_union_opt(__isl_take isl_pw_aff
*pwaff1
,
2797 __isl_take isl_pw_aff
*pwaff2
, int max
)
2800 return isl_pw_aff_union_max(pwaff1
, pwaff2
);
2802 return isl_pw_aff_union_min(pwaff1
, pwaff2
);
2805 /* Construct a map with as domain the domain of pwaff and
2806 * one-dimensional range corresponding to the affine expressions.
2808 static __isl_give isl_map
*map_from_pw_aff(__isl_take isl_pw_aff
*pwaff
)
2817 dim
= isl_pw_aff_get_space(pwaff
);
2818 map
= isl_map_empty(dim
);
2820 for (i
= 0; i
< pwaff
->n
; ++i
) {
2821 isl_basic_map
*bmap
;
2824 bmap
= isl_basic_map_from_aff(isl_aff_copy(pwaff
->p
[i
].aff
));
2825 map_i
= isl_map_from_basic_map(bmap
);
2826 map_i
= isl_map_intersect_domain(map_i
,
2827 isl_set_copy(pwaff
->p
[i
].set
));
2828 map
= isl_map_union_disjoint(map
, map_i
);
2831 isl_pw_aff_free(pwaff
);
2836 /* Construct a map with as domain the domain of pwaff and
2837 * one-dimensional range corresponding to the affine expressions.
2839 __isl_give isl_map
*isl_map_from_pw_aff(__isl_take isl_pw_aff
*pwaff
)
2843 if (isl_space_is_set(pwaff
->dim
))
2844 isl_die(isl_pw_aff_get_ctx(pwaff
), isl_error_invalid
,
2845 "space of input is not a map", goto error
);
2846 return map_from_pw_aff(pwaff
);
2848 isl_pw_aff_free(pwaff
);
2852 /* Construct a one-dimensional set with as parameter domain
2853 * the domain of pwaff and the single set dimension
2854 * corresponding to the affine expressions.
2856 __isl_give isl_set
*isl_set_from_pw_aff(__isl_take isl_pw_aff
*pwaff
)
2860 if (!isl_space_is_set(pwaff
->dim
))
2861 isl_die(isl_pw_aff_get_ctx(pwaff
), isl_error_invalid
,
2862 "space of input is not a set", goto error
);
2863 return map_from_pw_aff(pwaff
);
2865 isl_pw_aff_free(pwaff
);
2869 /* Return a set containing those elements in the domain
2870 * of "pwaff" where it satisfies "fn" (if complement is 0) or
2871 * does not satisfy "fn" (if complement is 1).
2873 * The pieces with a NaN never belong to the result since
2874 * NaN does not satisfy any property.
2876 static __isl_give isl_set
*pw_aff_locus(__isl_take isl_pw_aff
*pwaff
,
2877 __isl_give isl_basic_set
*(*fn
)(__isl_take isl_aff
*aff
, int rational
),
2886 set
= isl_set_empty(isl_pw_aff_get_domain_space(pwaff
));
2888 for (i
= 0; i
< pwaff
->n
; ++i
) {
2889 isl_basic_set
*bset
;
2890 isl_set
*set_i
, *locus
;
2893 if (isl_aff_is_nan(pwaff
->p
[i
].aff
))
2896 rational
= isl_set_has_rational(pwaff
->p
[i
].set
);
2897 bset
= fn(isl_aff_copy(pwaff
->p
[i
].aff
), rational
);
2898 locus
= isl_set_from_basic_set(bset
);
2899 set_i
= isl_set_copy(pwaff
->p
[i
].set
);
2901 set_i
= isl_set_subtract(set_i
, locus
);
2903 set_i
= isl_set_intersect(set_i
, locus
);
2904 set
= isl_set_union_disjoint(set
, set_i
);
2907 isl_pw_aff_free(pwaff
);
2912 /* Return a set containing those elements in the domain
2913 * of "pa" where it is positive.
2915 __isl_give isl_set
*isl_pw_aff_pos_set(__isl_take isl_pw_aff
*pa
)
2917 return pw_aff_locus(pa
, &aff_pos_basic_set
, 0);
2920 /* Return a set containing those elements in the domain
2921 * of pwaff where it is non-negative.
2923 __isl_give isl_set
*isl_pw_aff_nonneg_set(__isl_take isl_pw_aff
*pwaff
)
2925 return pw_aff_locus(pwaff
, &aff_nonneg_basic_set
, 0);
2928 /* Return a set containing those elements in the domain
2929 * of pwaff where it is zero.
2931 __isl_give isl_set
*isl_pw_aff_zero_set(__isl_take isl_pw_aff
*pwaff
)
2933 return pw_aff_locus(pwaff
, &aff_zero_basic_set
, 0);
2936 /* Return a set containing those elements in the domain
2937 * of pwaff where it is not zero.
2939 __isl_give isl_set
*isl_pw_aff_non_zero_set(__isl_take isl_pw_aff
*pwaff
)
2941 return pw_aff_locus(pwaff
, &aff_zero_basic_set
, 1);
2944 /* Return a set containing those elements in the shared domain
2945 * of pwaff1 and pwaff2 where pwaff1 is greater than (or equal) to pwaff2.
2947 * We compute the difference on the shared domain and then construct
2948 * the set of values where this difference is non-negative.
2949 * If strict is set, we first subtract 1 from the difference.
2950 * If equal is set, we only return the elements where pwaff1 and pwaff2
2953 static __isl_give isl_set
*pw_aff_gte_set(__isl_take isl_pw_aff
*pwaff1
,
2954 __isl_take isl_pw_aff
*pwaff2
, int strict
, int equal
)
2956 isl_set
*set1
, *set2
;
2958 set1
= isl_pw_aff_domain(isl_pw_aff_copy(pwaff1
));
2959 set2
= isl_pw_aff_domain(isl_pw_aff_copy(pwaff2
));
2960 set1
= isl_set_intersect(set1
, set2
);
2961 pwaff1
= isl_pw_aff_intersect_domain(pwaff1
, isl_set_copy(set1
));
2962 pwaff2
= isl_pw_aff_intersect_domain(pwaff2
, isl_set_copy(set1
));
2963 pwaff1
= isl_pw_aff_add(pwaff1
, isl_pw_aff_neg(pwaff2
));
2966 isl_space
*dim
= isl_set_get_space(set1
);
2968 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(dim
));
2969 aff
= isl_aff_add_constant_si(aff
, -1);
2970 pwaff1
= isl_pw_aff_add(pwaff1
, isl_pw_aff_alloc(set1
, aff
));
2975 return isl_pw_aff_zero_set(pwaff1
);
2976 return isl_pw_aff_nonneg_set(pwaff1
);
2979 /* Return a set containing those elements in the shared domain
2980 * of pwaff1 and pwaff2 where pwaff1 is equal to pwaff2.
2982 static __isl_give isl_set
*pw_aff_eq_set(__isl_take isl_pw_aff
*pwaff1
,
2983 __isl_take isl_pw_aff
*pwaff2
)
2985 return pw_aff_gte_set(pwaff1
, pwaff2
, 0, 1);
2988 __isl_give isl_set
*isl_pw_aff_eq_set(__isl_take isl_pw_aff
*pwaff1
,
2989 __isl_take isl_pw_aff
*pwaff2
)
2991 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_eq_set
);
2994 /* Return a set containing those elements in the shared domain
2995 * of pwaff1 and pwaff2 where pwaff1 is greater than or equal to pwaff2.
2997 static __isl_give isl_set
*pw_aff_ge_set(__isl_take isl_pw_aff
*pwaff1
,
2998 __isl_take isl_pw_aff
*pwaff2
)
3000 return pw_aff_gte_set(pwaff1
, pwaff2
, 0, 0);
3003 __isl_give isl_set
*isl_pw_aff_ge_set(__isl_take isl_pw_aff
*pwaff1
,
3004 __isl_take isl_pw_aff
*pwaff2
)
3006 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_ge_set
);
3009 /* Return a set containing those elements in the shared domain
3010 * of pwaff1 and pwaff2 where pwaff1 is strictly greater than pwaff2.
3012 static __isl_give isl_set
*pw_aff_gt_set(__isl_take isl_pw_aff
*pwaff1
,
3013 __isl_take isl_pw_aff
*pwaff2
)
3015 return pw_aff_gte_set(pwaff1
, pwaff2
, 1, 0);
3018 __isl_give isl_set
*isl_pw_aff_gt_set(__isl_take isl_pw_aff
*pwaff1
,
3019 __isl_take isl_pw_aff
*pwaff2
)
3021 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_gt_set
);
3024 __isl_give isl_set
*isl_pw_aff_le_set(__isl_take isl_pw_aff
*pwaff1
,
3025 __isl_take isl_pw_aff
*pwaff2
)
3027 return isl_pw_aff_ge_set(pwaff2
, pwaff1
);
3030 __isl_give isl_set
*isl_pw_aff_lt_set(__isl_take isl_pw_aff
*pwaff1
,
3031 __isl_take isl_pw_aff
*pwaff2
)
3033 return isl_pw_aff_gt_set(pwaff2
, pwaff1
);
3036 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3037 * where the function values are ordered in the same way as "order",
3038 * which returns a set in the shared domain of its two arguments.
3039 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3041 * Let "pa1" and "pa2" be defined on domains A and B respectively.
3042 * We first pull back the two functions such that they are defined on
3043 * the domain [A -> B]. Then we apply "order", resulting in a set
3044 * in the space [A -> B]. Finally, we unwrap this set to obtain
3045 * a map in the space A -> B.
3047 static __isl_give isl_map
*isl_pw_aff_order_map_aligned(
3048 __isl_take isl_pw_aff
*pa1
, __isl_take isl_pw_aff
*pa2
,
3049 __isl_give isl_set
*(*order
)(__isl_take isl_pw_aff
*pa1
,
3050 __isl_take isl_pw_aff
*pa2
))
3052 isl_space
*space1
, *space2
;
3056 space1
= isl_space_domain(isl_pw_aff_get_space(pa1
));
3057 space2
= isl_space_domain(isl_pw_aff_get_space(pa2
));
3058 space1
= isl_space_map_from_domain_and_range(space1
, space2
);
3059 ma
= isl_multi_aff_domain_map(isl_space_copy(space1
));
3060 pa1
= isl_pw_aff_pullback_multi_aff(pa1
, ma
);
3061 ma
= isl_multi_aff_range_map(space1
);
3062 pa2
= isl_pw_aff_pullback_multi_aff(pa2
, ma
);
3063 set
= order(pa1
, pa2
);
3065 return isl_set_unwrap(set
);
3068 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3069 * where the function values are equal.
3070 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3072 static __isl_give isl_map
*isl_pw_aff_eq_map_aligned(__isl_take isl_pw_aff
*pa1
,
3073 __isl_take isl_pw_aff
*pa2
)
3075 return isl_pw_aff_order_map_aligned(pa1
, pa2
, &isl_pw_aff_eq_set
);
3078 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3079 * where the function values are equal.
3081 __isl_give isl_map
*isl_pw_aff_eq_map(__isl_take isl_pw_aff
*pa1
,
3082 __isl_take isl_pw_aff
*pa2
)
3084 return align_params_pw_pw_map_and(pa1
, pa2
, &isl_pw_aff_eq_map_aligned
);
3087 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3088 * where the function value of "pa1" is less than the function value of "pa2".
3089 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3091 static __isl_give isl_map
*isl_pw_aff_lt_map_aligned(__isl_take isl_pw_aff
*pa1
,
3092 __isl_take isl_pw_aff
*pa2
)
3094 return isl_pw_aff_order_map_aligned(pa1
, pa2
, &isl_pw_aff_lt_set
);
3097 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3098 * where the function value of "pa1" is less than the function value of "pa2".
3100 __isl_give isl_map
*isl_pw_aff_lt_map(__isl_take isl_pw_aff
*pa1
,
3101 __isl_take isl_pw_aff
*pa2
)
3103 return align_params_pw_pw_map_and(pa1
, pa2
, &isl_pw_aff_lt_map_aligned
);
3106 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3107 * where the function value of "pa1" is greater than the function value
3109 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3111 static __isl_give isl_map
*isl_pw_aff_gt_map_aligned(__isl_take isl_pw_aff
*pa1
,
3112 __isl_take isl_pw_aff
*pa2
)
3114 return isl_pw_aff_order_map_aligned(pa1
, pa2
, &isl_pw_aff_gt_set
);
3117 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3118 * where the function value of "pa1" is greater than the function value
3121 __isl_give isl_map
*isl_pw_aff_gt_map(__isl_take isl_pw_aff
*pa1
,
3122 __isl_take isl_pw_aff
*pa2
)
3124 return align_params_pw_pw_map_and(pa1
, pa2
, &isl_pw_aff_gt_map_aligned
);
3127 /* Return a set containing those elements in the shared domain
3128 * of the elements of list1 and list2 where each element in list1
3129 * has the relation specified by "fn" with each element in list2.
3131 static __isl_give isl_set
*pw_aff_list_set(__isl_take isl_pw_aff_list
*list1
,
3132 __isl_take isl_pw_aff_list
*list2
,
3133 __isl_give isl_set
*(*fn
)(__isl_take isl_pw_aff
*pwaff1
,
3134 __isl_take isl_pw_aff
*pwaff2
))
3140 if (!list1
|| !list2
)
3143 ctx
= isl_pw_aff_list_get_ctx(list1
);
3144 if (list1
->n
< 1 || list2
->n
< 1)
3145 isl_die(ctx
, isl_error_invalid
,
3146 "list should contain at least one element", goto error
);
3148 set
= isl_set_universe(isl_pw_aff_get_domain_space(list1
->p
[0]));
3149 for (i
= 0; i
< list1
->n
; ++i
)
3150 for (j
= 0; j
< list2
->n
; ++j
) {
3153 set_ij
= fn(isl_pw_aff_copy(list1
->p
[i
]),
3154 isl_pw_aff_copy(list2
->p
[j
]));
3155 set
= isl_set_intersect(set
, set_ij
);
3158 isl_pw_aff_list_free(list1
);
3159 isl_pw_aff_list_free(list2
);
3162 isl_pw_aff_list_free(list1
);
3163 isl_pw_aff_list_free(list2
);
3167 /* Return a set containing those elements in the shared domain
3168 * of the elements of list1 and list2 where each element in list1
3169 * is equal to each element in list2.
3171 __isl_give isl_set
*isl_pw_aff_list_eq_set(__isl_take isl_pw_aff_list
*list1
,
3172 __isl_take isl_pw_aff_list
*list2
)
3174 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_eq_set
);
3177 __isl_give isl_set
*isl_pw_aff_list_ne_set(__isl_take isl_pw_aff_list
*list1
,
3178 __isl_take isl_pw_aff_list
*list2
)
3180 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_ne_set
);
3183 /* Return a set containing those elements in the shared domain
3184 * of the elements of list1 and list2 where each element in list1
3185 * is less than or equal to each element in list2.
3187 __isl_give isl_set
*isl_pw_aff_list_le_set(__isl_take isl_pw_aff_list
*list1
,
3188 __isl_take isl_pw_aff_list
*list2
)
3190 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_le_set
);
3193 __isl_give isl_set
*isl_pw_aff_list_lt_set(__isl_take isl_pw_aff_list
*list1
,
3194 __isl_take isl_pw_aff_list
*list2
)
3196 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_lt_set
);
3199 __isl_give isl_set
*isl_pw_aff_list_ge_set(__isl_take isl_pw_aff_list
*list1
,
3200 __isl_take isl_pw_aff_list
*list2
)
3202 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_ge_set
);
3205 __isl_give isl_set
*isl_pw_aff_list_gt_set(__isl_take isl_pw_aff_list
*list1
,
3206 __isl_take isl_pw_aff_list
*list2
)
3208 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_gt_set
);
3212 /* Return a set containing those elements in the shared domain
3213 * of pwaff1 and pwaff2 where pwaff1 is not equal to pwaff2.
3215 static __isl_give isl_set
*pw_aff_ne_set(__isl_take isl_pw_aff
*pwaff1
,
3216 __isl_take isl_pw_aff
*pwaff2
)
3218 isl_set
*set_lt
, *set_gt
;
3220 set_lt
= isl_pw_aff_lt_set(isl_pw_aff_copy(pwaff1
),
3221 isl_pw_aff_copy(pwaff2
));
3222 set_gt
= isl_pw_aff_gt_set(pwaff1
, pwaff2
);
3223 return isl_set_union_disjoint(set_lt
, set_gt
);
3226 __isl_give isl_set
*isl_pw_aff_ne_set(__isl_take isl_pw_aff
*pwaff1
,
3227 __isl_take isl_pw_aff
*pwaff2
)
3229 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_ne_set
);
3232 __isl_give isl_pw_aff
*isl_pw_aff_scale_down(__isl_take isl_pw_aff
*pwaff
,
3237 if (isl_int_is_one(v
))
3239 if (!isl_int_is_pos(v
))
3240 isl_die(isl_pw_aff_get_ctx(pwaff
), isl_error_invalid
,
3241 "factor needs to be positive",
3242 return isl_pw_aff_free(pwaff
));
3243 pwaff
= isl_pw_aff_cow(pwaff
);
3249 for (i
= 0; i
< pwaff
->n
; ++i
) {
3250 pwaff
->p
[i
].aff
= isl_aff_scale_down(pwaff
->p
[i
].aff
, v
);
3251 if (!pwaff
->p
[i
].aff
)
3252 return isl_pw_aff_free(pwaff
);
3258 __isl_give isl_pw_aff
*isl_pw_aff_floor(__isl_take isl_pw_aff
*pwaff
)
3262 pwaff
= isl_pw_aff_cow(pwaff
);
3268 for (i
= 0; i
< pwaff
->n
; ++i
) {
3269 pwaff
->p
[i
].aff
= isl_aff_floor(pwaff
->p
[i
].aff
);
3270 if (!pwaff
->p
[i
].aff
)
3271 return isl_pw_aff_free(pwaff
);
3277 __isl_give isl_pw_aff
*isl_pw_aff_ceil(__isl_take isl_pw_aff
*pwaff
)
3281 pwaff
= isl_pw_aff_cow(pwaff
);
3287 for (i
= 0; i
< pwaff
->n
; ++i
) {
3288 pwaff
->p
[i
].aff
= isl_aff_ceil(pwaff
->p
[i
].aff
);
3289 if (!pwaff
->p
[i
].aff
)
3290 return isl_pw_aff_free(pwaff
);
3296 /* Assuming that "cond1" and "cond2" are disjoint,
3297 * return an affine expression that is equal to pwaff1 on cond1
3298 * and to pwaff2 on cond2.
3300 static __isl_give isl_pw_aff
*isl_pw_aff_select(
3301 __isl_take isl_set
*cond1
, __isl_take isl_pw_aff
*pwaff1
,
3302 __isl_take isl_set
*cond2
, __isl_take isl_pw_aff
*pwaff2
)
3304 pwaff1
= isl_pw_aff_intersect_domain(pwaff1
, cond1
);
3305 pwaff2
= isl_pw_aff_intersect_domain(pwaff2
, cond2
);
3307 return isl_pw_aff_add_disjoint(pwaff1
, pwaff2
);
3310 /* Return an affine expression that is equal to pwaff_true for elements
3311 * where "cond" is non-zero and to pwaff_false for elements where "cond"
3313 * That is, return cond ? pwaff_true : pwaff_false;
3315 * If "cond" involves and NaN, then we conservatively return a NaN
3316 * on its entire domain. In principle, we could consider the pieces
3317 * where it is NaN separately from those where it is not.
3319 * If "pwaff_true" and "pwaff_false" are obviously equal to each other,
3320 * then only use the domain of "cond" to restrict the domain.
3322 __isl_give isl_pw_aff
*isl_pw_aff_cond(__isl_take isl_pw_aff
*cond
,
3323 __isl_take isl_pw_aff
*pwaff_true
, __isl_take isl_pw_aff
*pwaff_false
)
3325 isl_set
*cond_true
, *cond_false
;
3330 if (isl_pw_aff_involves_nan(cond
)) {
3331 isl_space
*space
= isl_pw_aff_get_domain_space(cond
);
3332 isl_local_space
*ls
= isl_local_space_from_space(space
);
3333 isl_pw_aff_free(cond
);
3334 isl_pw_aff_free(pwaff_true
);
3335 isl_pw_aff_free(pwaff_false
);
3336 return isl_pw_aff_nan_on_domain(ls
);
3339 pwaff_true
= isl_pw_aff_align_params(pwaff_true
,
3340 isl_pw_aff_get_space(pwaff_false
));
3341 pwaff_false
= isl_pw_aff_align_params(pwaff_false
,
3342 isl_pw_aff_get_space(pwaff_true
));
3343 equal
= isl_pw_aff_plain_is_equal(pwaff_true
, pwaff_false
);
3349 dom
= isl_set_coalesce(isl_pw_aff_domain(cond
));
3350 isl_pw_aff_free(pwaff_false
);
3351 return isl_pw_aff_intersect_domain(pwaff_true
, dom
);
3354 cond_true
= isl_pw_aff_non_zero_set(isl_pw_aff_copy(cond
));
3355 cond_false
= isl_pw_aff_zero_set(cond
);
3356 return isl_pw_aff_select(cond_true
, pwaff_true
,
3357 cond_false
, pwaff_false
);
3359 isl_pw_aff_free(cond
);
3360 isl_pw_aff_free(pwaff_true
);
3361 isl_pw_aff_free(pwaff_false
);
3365 isl_bool
isl_aff_is_cst(__isl_keep isl_aff
*aff
)
3368 return isl_bool_error
;
3370 return isl_seq_first_non_zero(aff
->v
->el
+ 2, aff
->v
->size
- 2) == -1;
3373 /* Check whether pwaff is a piecewise constant.
3375 isl_bool
isl_pw_aff_is_cst(__isl_keep isl_pw_aff
*pwaff
)
3380 return isl_bool_error
;
3382 for (i
= 0; i
< pwaff
->n
; ++i
) {
3383 isl_bool is_cst
= isl_aff_is_cst(pwaff
->p
[i
].aff
);
3384 if (is_cst
< 0 || !is_cst
)
3388 return isl_bool_true
;
3391 /* Are all elements of "mpa" piecewise constants?
3393 isl_bool
isl_multi_pw_aff_is_cst(__isl_keep isl_multi_pw_aff
*mpa
)
3398 return isl_bool_error
;
3400 for (i
= 0; i
< mpa
->n
; ++i
) {
3401 isl_bool is_cst
= isl_pw_aff_is_cst(mpa
->u
.p
[i
]);
3402 if (is_cst
< 0 || !is_cst
)
3406 return isl_bool_true
;
3409 /* Return the product of "aff1" and "aff2".
3411 * If either of the two is NaN, then the result is NaN.
3413 * Otherwise, at least one of "aff1" or "aff2" needs to be a constant.
3415 __isl_give isl_aff
*isl_aff_mul(__isl_take isl_aff
*aff1
,
3416 __isl_take isl_aff
*aff2
)
3421 if (isl_aff_is_nan(aff1
)) {
3425 if (isl_aff_is_nan(aff2
)) {
3430 if (!isl_aff_is_cst(aff2
) && isl_aff_is_cst(aff1
))
3431 return isl_aff_mul(aff2
, aff1
);
3433 if (!isl_aff_is_cst(aff2
))
3434 isl_die(isl_aff_get_ctx(aff1
), isl_error_invalid
,
3435 "at least one affine expression should be constant",
3438 aff1
= isl_aff_cow(aff1
);
3442 aff1
= isl_aff_scale(aff1
, aff2
->v
->el
[1]);
3443 aff1
= isl_aff_scale_down(aff1
, aff2
->v
->el
[0]);
3453 /* Divide "aff1" by "aff2", assuming "aff2" is a constant.
3455 * If either of the two is NaN, then the result is NaN.
3457 __isl_give isl_aff
*isl_aff_div(__isl_take isl_aff
*aff1
,
3458 __isl_take isl_aff
*aff2
)
3466 if (isl_aff_is_nan(aff1
)) {
3470 if (isl_aff_is_nan(aff2
)) {
3475 is_cst
= isl_aff_is_cst(aff2
);
3479 isl_die(isl_aff_get_ctx(aff2
), isl_error_invalid
,
3480 "second argument should be a constant", goto error
);
3485 neg
= isl_int_is_neg(aff2
->v
->el
[1]);
3487 isl_int_neg(aff2
->v
->el
[0], aff2
->v
->el
[0]);
3488 isl_int_neg(aff2
->v
->el
[1], aff2
->v
->el
[1]);
3491 aff1
= isl_aff_scale(aff1
, aff2
->v
->el
[0]);
3492 aff1
= isl_aff_scale_down(aff1
, aff2
->v
->el
[1]);
3495 isl_int_neg(aff2
->v
->el
[0], aff2
->v
->el
[0]);
3496 isl_int_neg(aff2
->v
->el
[1], aff2
->v
->el
[1]);
3507 static __isl_give isl_pw_aff
*pw_aff_add(__isl_take isl_pw_aff
*pwaff1
,
3508 __isl_take isl_pw_aff
*pwaff2
)
3510 return isl_pw_aff_on_shared_domain(pwaff1
, pwaff2
, &isl_aff_add
);
3513 __isl_give isl_pw_aff
*isl_pw_aff_add(__isl_take isl_pw_aff
*pwaff1
,
3514 __isl_take isl_pw_aff
*pwaff2
)
3516 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
, &pw_aff_add
);
3519 __isl_give isl_pw_aff
*isl_pw_aff_union_add(__isl_take isl_pw_aff
*pwaff1
,
3520 __isl_take isl_pw_aff
*pwaff2
)
3522 return isl_pw_aff_union_add_(pwaff1
, pwaff2
);
3525 static __isl_give isl_pw_aff
*pw_aff_mul(__isl_take isl_pw_aff
*pwaff1
,
3526 __isl_take isl_pw_aff
*pwaff2
)
3528 return isl_pw_aff_on_shared_domain(pwaff1
, pwaff2
, &isl_aff_mul
);
3531 __isl_give isl_pw_aff
*isl_pw_aff_mul(__isl_take isl_pw_aff
*pwaff1
,
3532 __isl_take isl_pw_aff
*pwaff2
)
3534 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
, &pw_aff_mul
);
3537 static __isl_give isl_pw_aff
*pw_aff_div(__isl_take isl_pw_aff
*pa1
,
3538 __isl_take isl_pw_aff
*pa2
)
3540 return isl_pw_aff_on_shared_domain(pa1
, pa2
, &isl_aff_div
);
3543 /* Divide "pa1" by "pa2", assuming "pa2" is a piecewise constant.
3545 __isl_give isl_pw_aff
*isl_pw_aff_div(__isl_take isl_pw_aff
*pa1
,
3546 __isl_take isl_pw_aff
*pa2
)
3550 is_cst
= isl_pw_aff_is_cst(pa2
);
3554 isl_die(isl_pw_aff_get_ctx(pa2
), isl_error_invalid
,
3555 "second argument should be a piecewise constant",
3557 return isl_pw_aff_align_params_pw_pw_and(pa1
, pa2
, &pw_aff_div
);
3559 isl_pw_aff_free(pa1
);
3560 isl_pw_aff_free(pa2
);
3564 /* Compute the quotient of the integer division of "pa1" by "pa2"
3565 * with rounding towards zero.
3566 * "pa2" is assumed to be a piecewise constant.
3568 * In particular, return
3570 * pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2)
3573 __isl_give isl_pw_aff
*isl_pw_aff_tdiv_q(__isl_take isl_pw_aff
*pa1
,
3574 __isl_take isl_pw_aff
*pa2
)
3580 is_cst
= isl_pw_aff_is_cst(pa2
);
3584 isl_die(isl_pw_aff_get_ctx(pa2
), isl_error_invalid
,
3585 "second argument should be a piecewise constant",
3588 pa1
= isl_pw_aff_div(pa1
, pa2
);
3590 cond
= isl_pw_aff_nonneg_set(isl_pw_aff_copy(pa1
));
3591 f
= isl_pw_aff_floor(isl_pw_aff_copy(pa1
));
3592 c
= isl_pw_aff_ceil(pa1
);
3593 return isl_pw_aff_cond(isl_set_indicator_function(cond
), f
, c
);
3595 isl_pw_aff_free(pa1
);
3596 isl_pw_aff_free(pa2
);
3600 /* Compute the remainder of the integer division of "pa1" by "pa2"
3601 * with rounding towards zero.
3602 * "pa2" is assumed to be a piecewise constant.
3604 * In particular, return
3606 * pa1 - pa2 * (pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2))
3609 __isl_give isl_pw_aff
*isl_pw_aff_tdiv_r(__isl_take isl_pw_aff
*pa1
,
3610 __isl_take isl_pw_aff
*pa2
)
3615 is_cst
= isl_pw_aff_is_cst(pa2
);
3619 isl_die(isl_pw_aff_get_ctx(pa2
), isl_error_invalid
,
3620 "second argument should be a piecewise constant",
3622 res
= isl_pw_aff_tdiv_q(isl_pw_aff_copy(pa1
), isl_pw_aff_copy(pa2
));
3623 res
= isl_pw_aff_mul(pa2
, res
);
3624 res
= isl_pw_aff_sub(pa1
, res
);
3627 isl_pw_aff_free(pa1
);
3628 isl_pw_aff_free(pa2
);
3632 /* Does either of "pa1" or "pa2" involve any NaN2?
3634 static isl_bool
either_involves_nan(__isl_keep isl_pw_aff
*pa1
,
3635 __isl_keep isl_pw_aff
*pa2
)
3639 has_nan
= isl_pw_aff_involves_nan(pa1
);
3640 if (has_nan
< 0 || has_nan
)
3642 return isl_pw_aff_involves_nan(pa2
);
3645 /* Replace "pa1" and "pa2" (at least one of which involves a NaN)
3646 * by a NaN on their shared domain.
3648 * In principle, the result could be refined to only being NaN
3649 * on the parts of this domain where at least one of "pa1" or "pa2" is NaN.
3651 static __isl_give isl_pw_aff
*replace_by_nan(__isl_take isl_pw_aff
*pa1
,
3652 __isl_take isl_pw_aff
*pa2
)
3654 isl_local_space
*ls
;
3658 dom
= isl_set_intersect(isl_pw_aff_domain(pa1
), isl_pw_aff_domain(pa2
));
3659 ls
= isl_local_space_from_space(isl_set_get_space(dom
));
3660 pa
= isl_pw_aff_nan_on_domain(ls
);
3661 pa
= isl_pw_aff_intersect_domain(pa
, dom
);
3666 static __isl_give isl_pw_aff
*pw_aff_min(__isl_take isl_pw_aff
*pwaff1
,
3667 __isl_take isl_pw_aff
*pwaff2
)
3672 dom
= isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1
)),
3673 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2
)));
3674 le
= isl_pw_aff_le_set(isl_pw_aff_copy(pwaff1
),
3675 isl_pw_aff_copy(pwaff2
));
3676 dom
= isl_set_subtract(dom
, isl_set_copy(le
));
3677 return isl_pw_aff_select(le
, pwaff1
, dom
, pwaff2
);
3680 static __isl_give isl_pw_aff
*pw_aff_max(__isl_take isl_pw_aff
*pwaff1
,
3681 __isl_take isl_pw_aff
*pwaff2
)
3686 dom
= isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1
)),
3687 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2
)));
3688 ge
= isl_pw_aff_ge_set(isl_pw_aff_copy(pwaff1
),
3689 isl_pw_aff_copy(pwaff2
));
3690 dom
= isl_set_subtract(dom
, isl_set_copy(ge
));
3691 return isl_pw_aff_select(ge
, pwaff1
, dom
, pwaff2
);
3694 /* Return an expression for the minimum (if "max" is not set) or
3695 * the maximum (if "max" is set) of "pa1" and "pa2".
3696 * If either expression involves any NaN, then return a NaN
3697 * on the shared domain as result.
3699 static __isl_give isl_pw_aff
*pw_aff_min_max(__isl_take isl_pw_aff
*pa1
,
3700 __isl_take isl_pw_aff
*pa2
, int max
)
3704 has_nan
= either_involves_nan(pa1
, pa2
);
3706 pa1
= isl_pw_aff_free(pa1
);
3708 return replace_by_nan(pa1
, pa2
);
3711 return isl_pw_aff_align_params_pw_pw_and(pa1
, pa2
, &pw_aff_max
);
3713 return isl_pw_aff_align_params_pw_pw_and(pa1
, pa2
, &pw_aff_min
);
3716 /* Return an expression for the minimum of "pwaff1" and "pwaff2".
3718 __isl_give isl_pw_aff
*isl_pw_aff_min(__isl_take isl_pw_aff
*pwaff1
,
3719 __isl_take isl_pw_aff
*pwaff2
)
3721 return pw_aff_min_max(pwaff1
, pwaff2
, 0);
3724 /* Return an expression for the maximum of "pwaff1" and "pwaff2".
3726 __isl_give isl_pw_aff
*isl_pw_aff_max(__isl_take isl_pw_aff
*pwaff1
,
3727 __isl_take isl_pw_aff
*pwaff2
)
3729 return pw_aff_min_max(pwaff1
, pwaff2
, 1);
3732 static __isl_give isl_pw_aff
*pw_aff_list_reduce(
3733 __isl_take isl_pw_aff_list
*list
,
3734 __isl_give isl_pw_aff
*(*fn
)(__isl_take isl_pw_aff
*pwaff1
,
3735 __isl_take isl_pw_aff
*pwaff2
))
3744 ctx
= isl_pw_aff_list_get_ctx(list
);
3746 isl_die(ctx
, isl_error_invalid
,
3747 "list should contain at least one element", goto error
);
3749 res
= isl_pw_aff_copy(list
->p
[0]);
3750 for (i
= 1; i
< list
->n
; ++i
)
3751 res
= fn(res
, isl_pw_aff_copy(list
->p
[i
]));
3753 isl_pw_aff_list_free(list
);
3756 isl_pw_aff_list_free(list
);
3760 /* Return an isl_pw_aff that maps each element in the intersection of the
3761 * domains of the elements of list to the minimal corresponding affine
3764 __isl_give isl_pw_aff
*isl_pw_aff_list_min(__isl_take isl_pw_aff_list
*list
)
3766 return pw_aff_list_reduce(list
, &isl_pw_aff_min
);
3769 /* Return an isl_pw_aff that maps each element in the intersection of the
3770 * domains of the elements of list to the maximal corresponding affine
3773 __isl_give isl_pw_aff
*isl_pw_aff_list_max(__isl_take isl_pw_aff_list
*list
)
3775 return pw_aff_list_reduce(list
, &isl_pw_aff_max
);
3778 /* Mark the domains of "pwaff" as rational.
3780 __isl_give isl_pw_aff
*isl_pw_aff_set_rational(__isl_take isl_pw_aff
*pwaff
)
3784 pwaff
= isl_pw_aff_cow(pwaff
);
3790 for (i
= 0; i
< pwaff
->n
; ++i
) {
3791 pwaff
->p
[i
].set
= isl_set_set_rational(pwaff
->p
[i
].set
);
3792 if (!pwaff
->p
[i
].set
)
3793 return isl_pw_aff_free(pwaff
);
3799 /* Mark the domains of the elements of "list" as rational.
3801 __isl_give isl_pw_aff_list
*isl_pw_aff_list_set_rational(
3802 __isl_take isl_pw_aff_list
*list
)
3812 for (i
= 0; i
< n
; ++i
) {
3815 pa
= isl_pw_aff_list_get_pw_aff(list
, i
);
3816 pa
= isl_pw_aff_set_rational(pa
);
3817 list
= isl_pw_aff_list_set_pw_aff(list
, i
, pa
);
3823 /* Do the parameters of "aff" match those of "space"?
3825 isl_bool
isl_aff_matching_params(__isl_keep isl_aff
*aff
,
3826 __isl_keep isl_space
*space
)
3828 isl_space
*aff_space
;
3832 return isl_bool_error
;
3834 aff_space
= isl_aff_get_domain_space(aff
);
3836 match
= isl_space_has_equal_params(space
, aff_space
);
3838 isl_space_free(aff_space
);
3842 /* Check that the domain space of "aff" matches "space".
3844 isl_stat
isl_aff_check_match_domain_space(__isl_keep isl_aff
*aff
,
3845 __isl_keep isl_space
*space
)
3847 isl_space
*aff_space
;
3851 return isl_stat_error
;
3853 aff_space
= isl_aff_get_domain_space(aff
);
3855 match
= isl_space_has_equal_params(space
, aff_space
);
3859 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
3860 "parameters don't match", goto error
);
3861 match
= isl_space_tuple_is_equal(space
, isl_dim_in
,
3862 aff_space
, isl_dim_set
);
3866 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
3867 "domains don't match", goto error
);
3868 isl_space_free(aff_space
);
3871 isl_space_free(aff_space
);
3872 return isl_stat_error
;
3881 #include <isl_multi_no_explicit_domain.c>
3882 #include <isl_multi_templ.c>
3883 #include <isl_multi_apply_set.c>
3884 #include <isl_multi_cmp.c>
3885 #include <isl_multi_dims.c>
3886 #include <isl_multi_floor.c>
3887 #include <isl_multi_gist.c>
3891 /* Construct an isl_multi_aff living in "space" that corresponds
3892 * to the affine transformation matrix "mat".
3894 __isl_give isl_multi_aff
*isl_multi_aff_from_aff_mat(
3895 __isl_take isl_space
*space
, __isl_take isl_mat
*mat
)
3898 isl_local_space
*ls
= NULL
;
3899 isl_multi_aff
*ma
= NULL
;
3900 int n_row
, n_col
, n_out
, total
;
3906 ctx
= isl_mat_get_ctx(mat
);
3908 n_row
= isl_mat_rows(mat
);
3909 n_col
= isl_mat_cols(mat
);
3911 isl_die(ctx
, isl_error_invalid
,
3912 "insufficient number of rows", goto error
);
3914 isl_die(ctx
, isl_error_invalid
,
3915 "insufficient number of columns", goto error
);
3916 n_out
= isl_space_dim(space
, isl_dim_out
);
3917 total
= isl_space_dim(space
, isl_dim_all
);
3918 if (1 + n_out
!= n_row
|| 2 + total
!= n_row
+ n_col
)
3919 isl_die(ctx
, isl_error_invalid
,
3920 "dimension mismatch", goto error
);
3922 ma
= isl_multi_aff_zero(isl_space_copy(space
));
3923 ls
= isl_local_space_from_space(isl_space_domain(space
));
3925 for (i
= 0; i
< n_row
- 1; ++i
) {
3929 v
= isl_vec_alloc(ctx
, 1 + n_col
);
3932 isl_int_set(v
->el
[0], mat
->row
[0][0]);
3933 isl_seq_cpy(v
->el
+ 1, mat
->row
[1 + i
], n_col
);
3934 v
= isl_vec_normalize(v
);
3935 aff
= isl_aff_alloc_vec(isl_local_space_copy(ls
), v
);
3936 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
3939 isl_local_space_free(ls
);
3943 isl_local_space_free(ls
);
3945 isl_multi_aff_free(ma
);
3949 /* Remove any internal structure of the domain of "ma".
3950 * If there is any such internal structure in the input,
3951 * then the name of the corresponding space is also removed.
3953 __isl_give isl_multi_aff
*isl_multi_aff_flatten_domain(
3954 __isl_take isl_multi_aff
*ma
)
3961 if (!ma
->space
->nested
[0])
3964 space
= isl_multi_aff_get_space(ma
);
3965 space
= isl_space_flatten_domain(space
);
3966 ma
= isl_multi_aff_reset_space(ma
, space
);
3971 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
3972 * of the space to its domain.
3974 __isl_give isl_multi_aff
*isl_multi_aff_domain_map(__isl_take isl_space
*space
)
3977 isl_local_space
*ls
;
3982 if (!isl_space_is_map(space
))
3983 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
3984 "not a map space", goto error
);
3986 n_in
= isl_space_dim(space
, isl_dim_in
);
3987 space
= isl_space_domain_map(space
);
3989 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
3991 isl_space_free(space
);
3995 space
= isl_space_domain(space
);
3996 ls
= isl_local_space_from_space(space
);
3997 for (i
= 0; i
< n_in
; ++i
) {
4000 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
4002 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
4004 isl_local_space_free(ls
);
4007 isl_space_free(space
);
4011 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
4012 * of the space to its range.
4014 __isl_give isl_multi_aff
*isl_multi_aff_range_map(__isl_take isl_space
*space
)
4017 isl_local_space
*ls
;
4022 if (!isl_space_is_map(space
))
4023 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
4024 "not a map space", goto error
);
4026 n_in
= isl_space_dim(space
, isl_dim_in
);
4027 n_out
= isl_space_dim(space
, isl_dim_out
);
4028 space
= isl_space_range_map(space
);
4030 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
4032 isl_space_free(space
);
4036 space
= isl_space_domain(space
);
4037 ls
= isl_local_space_from_space(space
);
4038 for (i
= 0; i
< n_out
; ++i
) {
4041 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
4042 isl_dim_set
, n_in
+ i
);
4043 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
4045 isl_local_space_free(ls
);
4048 isl_space_free(space
);
4052 /* Given a map space, return an isl_pw_multi_aff that maps a wrapped copy
4053 * of the space to its range.
4055 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_range_map(
4056 __isl_take isl_space
*space
)
4058 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_range_map(space
));
4061 /* Given the space of a set and a range of set dimensions,
4062 * construct an isl_multi_aff that projects out those dimensions.
4064 __isl_give isl_multi_aff
*isl_multi_aff_project_out_map(
4065 __isl_take isl_space
*space
, enum isl_dim_type type
,
4066 unsigned first
, unsigned n
)
4069 isl_local_space
*ls
;
4074 if (!isl_space_is_set(space
))
4075 isl_die(isl_space_get_ctx(space
), isl_error_unsupported
,
4076 "expecting set space", goto error
);
4077 if (type
!= isl_dim_set
)
4078 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
4079 "only set dimensions can be projected out", goto error
);
4081 dim
= isl_space_dim(space
, isl_dim_set
);
4082 if (first
+ n
> dim
)
4083 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
4084 "range out of bounds", goto error
);
4086 space
= isl_space_from_domain(space
);
4087 space
= isl_space_add_dims(space
, isl_dim_out
, dim
- n
);
4090 return isl_multi_aff_alloc(space
);
4092 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
4093 space
= isl_space_domain(space
);
4094 ls
= isl_local_space_from_space(space
);
4096 for (i
= 0; i
< first
; ++i
) {
4099 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
4101 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
4104 for (i
= 0; i
< dim
- (first
+ n
); ++i
) {
4107 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
4108 isl_dim_set
, first
+ n
+ i
);
4109 ma
= isl_multi_aff_set_aff(ma
, first
+ i
, aff
);
4112 isl_local_space_free(ls
);
4115 isl_space_free(space
);
4119 /* Given the space of a set and a range of set dimensions,
4120 * construct an isl_pw_multi_aff that projects out those dimensions.
4122 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_project_out_map(
4123 __isl_take isl_space
*space
, enum isl_dim_type type
,
4124 unsigned first
, unsigned n
)
4128 ma
= isl_multi_aff_project_out_map(space
, type
, first
, n
);
4129 return isl_pw_multi_aff_from_multi_aff(ma
);
4132 /* Create an isl_pw_multi_aff with the given isl_multi_aff on a universe
4135 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_multi_aff(
4136 __isl_take isl_multi_aff
*ma
)
4138 isl_set
*dom
= isl_set_universe(isl_multi_aff_get_domain_space(ma
));
4139 return isl_pw_multi_aff_alloc(dom
, ma
);
4142 /* Create a piecewise multi-affine expression in the given space that maps each
4143 * input dimension to the corresponding output dimension.
4145 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_identity(
4146 __isl_take isl_space
*space
)
4148 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_identity(space
));
4151 /* Exploit the equalities in "eq" to simplify the affine expressions.
4153 static __isl_give isl_multi_aff
*isl_multi_aff_substitute_equalities(
4154 __isl_take isl_multi_aff
*maff
, __isl_take isl_basic_set
*eq
)
4158 maff
= isl_multi_aff_cow(maff
);
4162 for (i
= 0; i
< maff
->n
; ++i
) {
4163 maff
->u
.p
[i
] = isl_aff_substitute_equalities(maff
->u
.p
[i
],
4164 isl_basic_set_copy(eq
));
4169 isl_basic_set_free(eq
);
4172 isl_basic_set_free(eq
);
4173 isl_multi_aff_free(maff
);
4177 __isl_give isl_multi_aff
*isl_multi_aff_scale(__isl_take isl_multi_aff
*maff
,
4182 maff
= isl_multi_aff_cow(maff
);
4186 for (i
= 0; i
< maff
->n
; ++i
) {
4187 maff
->u
.p
[i
] = isl_aff_scale(maff
->u
.p
[i
], f
);
4189 return isl_multi_aff_free(maff
);
4195 __isl_give isl_multi_aff
*isl_multi_aff_add_on_domain(__isl_keep isl_set
*dom
,
4196 __isl_take isl_multi_aff
*maff1
, __isl_take isl_multi_aff
*maff2
)
4198 maff1
= isl_multi_aff_add(maff1
, maff2
);
4199 maff1
= isl_multi_aff_gist(maff1
, isl_set_copy(dom
));
4203 int isl_multi_aff_is_empty(__isl_keep isl_multi_aff
*maff
)
4211 /* Return the set of domain elements where "ma1" is lexicographically
4212 * smaller than or equal to "ma2".
4214 __isl_give isl_set
*isl_multi_aff_lex_le_set(__isl_take isl_multi_aff
*ma1
,
4215 __isl_take isl_multi_aff
*ma2
)
4217 return isl_multi_aff_lex_ge_set(ma2
, ma1
);
4220 /* Return the set of domain elements where "ma1" is lexicographically
4221 * smaller than "ma2".
4223 __isl_give isl_set
*isl_multi_aff_lex_lt_set(__isl_take isl_multi_aff
*ma1
,
4224 __isl_take isl_multi_aff
*ma2
)
4226 return isl_multi_aff_lex_gt_set(ma2
, ma1
);
4229 /* Return the set of domain elements where "ma1" and "ma2"
4232 static __isl_give isl_set
*isl_multi_aff_order_set(
4233 __isl_take isl_multi_aff
*ma1
, __isl_take isl_multi_aff
*ma2
,
4234 __isl_give isl_map
*order(__isl_take isl_space
*set_space
))
4237 isl_map
*map1
, *map2
;
4240 map1
= isl_map_from_multi_aff(ma1
);
4241 map2
= isl_map_from_multi_aff(ma2
);
4242 map
= isl_map_range_product(map1
, map2
);
4243 space
= isl_space_range(isl_map_get_space(map
));
4244 space
= isl_space_domain(isl_space_unwrap(space
));
4246 map
= isl_map_intersect_range(map
, isl_map_wrap(ge
));
4248 return isl_map_domain(map
);
4251 /* Return the set of domain elements where "ma1" is lexicographically
4252 * greater than or equal to "ma2".
4254 __isl_give isl_set
*isl_multi_aff_lex_ge_set(__isl_take isl_multi_aff
*ma1
,
4255 __isl_take isl_multi_aff
*ma2
)
4257 return isl_multi_aff_order_set(ma1
, ma2
, &isl_map_lex_ge
);
4260 /* Return the set of domain elements where "ma1" is lexicographically
4261 * greater than "ma2".
4263 __isl_give isl_set
*isl_multi_aff_lex_gt_set(__isl_take isl_multi_aff
*ma1
,
4264 __isl_take isl_multi_aff
*ma2
)
4266 return isl_multi_aff_order_set(ma1
, ma2
, &isl_map_lex_gt
);
4270 #define PW isl_pw_multi_aff
4272 #define EL isl_multi_aff
4274 #define EL_IS_ZERO is_empty
4278 #define IS_ZERO is_empty
4281 #undef DEFAULT_IS_ZERO
4282 #define DEFAULT_IS_ZERO 0
4286 #define NO_INSERT_DIMS
4290 #include <isl_pw_templ.c>
4291 #include <isl_pw_union_opt.c>
4296 #define UNION isl_union_pw_multi_aff
4298 #define PART isl_pw_multi_aff
4300 #define PARTS pw_multi_aff
4302 #include <isl_union_multi.c>
4303 #include <isl_union_neg.c>
4305 static __isl_give isl_pw_multi_aff
*pw_multi_aff_union_lexmax(
4306 __isl_take isl_pw_multi_aff
*pma1
,
4307 __isl_take isl_pw_multi_aff
*pma2
)
4309 return isl_pw_multi_aff_union_opt_cmp(pma1
, pma2
,
4310 &isl_multi_aff_lex_ge_set
);
4313 /* Given two piecewise multi affine expressions, return a piecewise
4314 * multi-affine expression defined on the union of the definition domains
4315 * of the inputs that is equal to the lexicographic maximum of the two
4316 * inputs on each cell. If only one of the two inputs is defined on
4317 * a given cell, then it is considered to be the maximum.
4319 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_union_lexmax(
4320 __isl_take isl_pw_multi_aff
*pma1
,
4321 __isl_take isl_pw_multi_aff
*pma2
)
4323 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
4324 &pw_multi_aff_union_lexmax
);
4327 static __isl_give isl_pw_multi_aff
*pw_multi_aff_union_lexmin(
4328 __isl_take isl_pw_multi_aff
*pma1
,
4329 __isl_take isl_pw_multi_aff
*pma2
)
4331 return isl_pw_multi_aff_union_opt_cmp(pma1
, pma2
,
4332 &isl_multi_aff_lex_le_set
);
4335 /* Given two piecewise multi affine expressions, return a piecewise
4336 * multi-affine expression defined on the union of the definition domains
4337 * of the inputs that is equal to the lexicographic minimum of the two
4338 * inputs on each cell. If only one of the two inputs is defined on
4339 * a given cell, then it is considered to be the minimum.
4341 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_union_lexmin(
4342 __isl_take isl_pw_multi_aff
*pma1
,
4343 __isl_take isl_pw_multi_aff
*pma2
)
4345 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
4346 &pw_multi_aff_union_lexmin
);
4349 static __isl_give isl_pw_multi_aff
*pw_multi_aff_add(
4350 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4352 return isl_pw_multi_aff_on_shared_domain(pma1
, pma2
,
4353 &isl_multi_aff_add
);
4356 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_add(
4357 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4359 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
4363 static __isl_give isl_pw_multi_aff
*pw_multi_aff_sub(
4364 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4366 return isl_pw_multi_aff_on_shared_domain(pma1
, pma2
,
4367 &isl_multi_aff_sub
);
4370 /* Subtract "pma2" from "pma1" and return the result.
4372 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_sub(
4373 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4375 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
4379 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_union_add(
4380 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4382 return isl_pw_multi_aff_union_add_(pma1
, pma2
);
4385 /* Compute the sum of "upa1" and "upa2" on the union of their domains,
4386 * with the actual sum on the shared domain and
4387 * the defined expression on the symmetric difference of the domains.
4389 __isl_give isl_union_pw_aff
*isl_union_pw_aff_union_add(
4390 __isl_take isl_union_pw_aff
*upa1
, __isl_take isl_union_pw_aff
*upa2
)
4392 return isl_union_pw_aff_union_add_(upa1
, upa2
);
4395 /* Compute the sum of "upma1" and "upma2" on the union of their domains,
4396 * with the actual sum on the shared domain and
4397 * the defined expression on the symmetric difference of the domains.
4399 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_union_add(
4400 __isl_take isl_union_pw_multi_aff
*upma1
,
4401 __isl_take isl_union_pw_multi_aff
*upma2
)
4403 return isl_union_pw_multi_aff_union_add_(upma1
, upma2
);
4406 /* Given two piecewise multi-affine expressions A -> B and C -> D,
4407 * construct a piecewise multi-affine expression [A -> C] -> [B -> D].
4409 static __isl_give isl_pw_multi_aff
*pw_multi_aff_product(
4410 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4414 isl_pw_multi_aff
*res
;
4419 n
= pma1
->n
* pma2
->n
;
4420 space
= isl_space_product(isl_space_copy(pma1
->dim
),
4421 isl_space_copy(pma2
->dim
));
4422 res
= isl_pw_multi_aff_alloc_size(space
, n
);
4424 for (i
= 0; i
< pma1
->n
; ++i
) {
4425 for (j
= 0; j
< pma2
->n
; ++j
) {
4429 domain
= isl_set_product(isl_set_copy(pma1
->p
[i
].set
),
4430 isl_set_copy(pma2
->p
[j
].set
));
4431 ma
= isl_multi_aff_product(
4432 isl_multi_aff_copy(pma1
->p
[i
].maff
),
4433 isl_multi_aff_copy(pma2
->p
[j
].maff
));
4434 res
= isl_pw_multi_aff_add_piece(res
, domain
, ma
);
4438 isl_pw_multi_aff_free(pma1
);
4439 isl_pw_multi_aff_free(pma2
);
4442 isl_pw_multi_aff_free(pma1
);
4443 isl_pw_multi_aff_free(pma2
);
4447 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_product(
4448 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4450 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
4451 &pw_multi_aff_product
);
4454 /* Construct a map mapping the domain of the piecewise multi-affine expression
4455 * to its range, with each dimension in the range equated to the
4456 * corresponding affine expression on its cell.
4458 * If the domain of "pma" is rational, then so is the constructed "map".
4460 __isl_give isl_map
*isl_map_from_pw_multi_aff(__isl_take isl_pw_multi_aff
*pma
)
4468 map
= isl_map_empty(isl_pw_multi_aff_get_space(pma
));
4470 for (i
= 0; i
< pma
->n
; ++i
) {
4472 isl_multi_aff
*maff
;
4473 isl_basic_map
*bmap
;
4476 rational
= isl_set_is_rational(pma
->p
[i
].set
);
4478 map
= isl_map_free(map
);
4479 maff
= isl_multi_aff_copy(pma
->p
[i
].maff
);
4480 bmap
= isl_basic_map_from_multi_aff2(maff
, rational
);
4481 map_i
= isl_map_from_basic_map(bmap
);
4482 map_i
= isl_map_intersect_domain(map_i
,
4483 isl_set_copy(pma
->p
[i
].set
));
4484 map
= isl_map_union_disjoint(map
, map_i
);
4487 isl_pw_multi_aff_free(pma
);
4491 __isl_give isl_set
*isl_set_from_pw_multi_aff(__isl_take isl_pw_multi_aff
*pma
)
4496 if (!isl_space_is_set(pma
->dim
))
4497 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
4498 "isl_pw_multi_aff cannot be converted into an isl_set",
4501 return isl_map_from_pw_multi_aff(pma
);
4503 isl_pw_multi_aff_free(pma
);
4507 /* Subtract the initial "n" elements in "ma" with coefficients in "c" and
4508 * denominator "denom".
4509 * "denom" is allowed to be negative, in which case the actual denominator
4510 * is -denom and the expressions are added instead.
4512 static __isl_give isl_aff
*subtract_initial(__isl_take isl_aff
*aff
,
4513 __isl_keep isl_multi_aff
*ma
, int n
, isl_int
*c
, isl_int denom
)
4519 first
= isl_seq_first_non_zero(c
, n
);
4523 sign
= isl_int_sgn(denom
);
4525 isl_int_abs(d
, denom
);
4526 for (i
= first
; i
< n
; ++i
) {
4529 if (isl_int_is_zero(c
[i
]))
4531 aff_i
= isl_multi_aff_get_aff(ma
, i
);
4532 aff_i
= isl_aff_scale(aff_i
, c
[i
]);
4533 aff_i
= isl_aff_scale_down(aff_i
, d
);
4535 aff
= isl_aff_sub(aff
, aff_i
);
4537 aff
= isl_aff_add(aff
, aff_i
);
4544 /* Extract an affine expression that expresses the output dimension "pos"
4545 * of "bmap" in terms of the parameters and input dimensions from
4547 * Note that this expression may involve integer divisions defined
4548 * in terms of parameters and input dimensions.
4549 * The equality may also involve references to earlier (but not later)
4550 * output dimensions. These are replaced by the corresponding elements
4553 * If the equality is of the form
4555 * f(i) + h(j) + a x + g(i) = 0,
4557 * with f(i) a linear combinations of the parameters and input dimensions,
4558 * g(i) a linear combination of integer divisions defined in terms of the same
4559 * and h(j) a linear combinations of earlier output dimensions,
4560 * then the affine expression is
4562 * (-f(i) - g(i))/a - h(j)/a
4564 * If the equality is of the form
4566 * f(i) + h(j) - a x + g(i) = 0,
4568 * then the affine expression is
4570 * (f(i) + g(i))/a - h(j)/(-a)
4573 * If "div" refers to an integer division (i.e., it is smaller than
4574 * the number of integer divisions), then the equality constraint
4575 * does involve an integer division (the one at position "div") that
4576 * is defined in terms of output dimensions. However, this integer
4577 * division can be eliminated by exploiting a pair of constraints
4578 * x >= l and x <= l + n, with n smaller than the coefficient of "div"
4579 * in the equality constraint. "ineq" refers to inequality x >= l, i.e.,
4581 * In particular, let
4583 * x = e(i) + m floor(...)
4585 * with e(i) the expression derived above and floor(...) the integer
4586 * division involving output dimensions.
4597 * e(i) + m floor(...) - l = (e(i) + m floor(...) - l) mod m
4598 * = (e(i) - l) mod m
4602 * x - l = (e(i) - l) mod m
4606 * x = ((e(i) - l) mod m) + l
4608 * The variable "shift" below contains the expression -l, which may
4609 * also involve a linear combination of earlier output dimensions.
4611 static __isl_give isl_aff
*extract_aff_from_equality(
4612 __isl_keep isl_basic_map
*bmap
, int pos
, int eq
, int div
, int ineq
,
4613 __isl_keep isl_multi_aff
*ma
)
4616 unsigned n_div
, n_out
;
4618 isl_local_space
*ls
;
4619 isl_aff
*aff
, *shift
;
4622 ctx
= isl_basic_map_get_ctx(bmap
);
4623 ls
= isl_basic_map_get_local_space(bmap
);
4624 ls
= isl_local_space_domain(ls
);
4625 aff
= isl_aff_alloc(isl_local_space_copy(ls
));
4628 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
4629 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4630 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4631 if (isl_int_is_neg(bmap
->eq
[eq
][o_out
+ pos
])) {
4632 isl_seq_cpy(aff
->v
->el
+ 1, bmap
->eq
[eq
], o_out
);
4633 isl_seq_cpy(aff
->v
->el
+ 1 + o_out
,
4634 bmap
->eq
[eq
] + o_out
+ n_out
, n_div
);
4636 isl_seq_neg(aff
->v
->el
+ 1, bmap
->eq
[eq
], o_out
);
4637 isl_seq_neg(aff
->v
->el
+ 1 + o_out
,
4638 bmap
->eq
[eq
] + o_out
+ n_out
, n_div
);
4641 isl_int_set_si(aff
->v
->el
[1 + o_out
+ div
], 0);
4642 isl_int_abs(aff
->v
->el
[0], bmap
->eq
[eq
][o_out
+ pos
]);
4643 aff
= subtract_initial(aff
, ma
, pos
, bmap
->eq
[eq
] + o_out
,
4644 bmap
->eq
[eq
][o_out
+ pos
]);
4646 shift
= isl_aff_alloc(isl_local_space_copy(ls
));
4649 isl_seq_cpy(shift
->v
->el
+ 1, bmap
->ineq
[ineq
], o_out
);
4650 isl_seq_cpy(shift
->v
->el
+ 1 + o_out
,
4651 bmap
->ineq
[ineq
] + o_out
+ n_out
, n_div
);
4652 isl_int_set_si(shift
->v
->el
[0], 1);
4653 shift
= subtract_initial(shift
, ma
, pos
,
4654 bmap
->ineq
[ineq
] + o_out
, ctx
->negone
);
4655 aff
= isl_aff_add(aff
, isl_aff_copy(shift
));
4656 mod
= isl_val_int_from_isl_int(ctx
,
4657 bmap
->eq
[eq
][o_out
+ n_out
+ div
]);
4658 mod
= isl_val_abs(mod
);
4659 aff
= isl_aff_mod_val(aff
, mod
);
4660 aff
= isl_aff_sub(aff
, shift
);
4663 isl_local_space_free(ls
);
4666 isl_local_space_free(ls
);
4671 /* Given a basic map with output dimensions defined
4672 * in terms of the parameters input dimensions and earlier
4673 * output dimensions using an equality (and possibly a pair on inequalities),
4674 * extract an isl_aff that expresses output dimension "pos" in terms
4675 * of the parameters and input dimensions.
4676 * Note that this expression may involve integer divisions defined
4677 * in terms of parameters and input dimensions.
4678 * "ma" contains the expressions corresponding to earlier output dimensions.
4680 * This function shares some similarities with
4681 * isl_basic_map_has_defining_equality and isl_constraint_get_bound.
4683 static __isl_give isl_aff
*extract_isl_aff_from_basic_map(
4684 __isl_keep isl_basic_map
*bmap
, int pos
, __isl_keep isl_multi_aff
*ma
)
4691 eq
= isl_basic_map_output_defining_equality(bmap
, pos
, &div
, &ineq
);
4692 if (eq
>= bmap
->n_eq
)
4693 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
4694 "unable to find suitable equality", return NULL
);
4695 aff
= extract_aff_from_equality(bmap
, pos
, eq
, div
, ineq
, ma
);
4697 aff
= isl_aff_remove_unused_divs(aff
);
4701 /* Given a basic map where each output dimension is defined
4702 * in terms of the parameters and input dimensions using an equality,
4703 * extract an isl_multi_aff that expresses the output dimensions in terms
4704 * of the parameters and input dimensions.
4706 static __isl_give isl_multi_aff
*extract_isl_multi_aff_from_basic_map(
4707 __isl_take isl_basic_map
*bmap
)
4716 ma
= isl_multi_aff_alloc(isl_basic_map_get_space(bmap
));
4717 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4719 for (i
= 0; i
< n_out
; ++i
) {
4722 aff
= extract_isl_aff_from_basic_map(bmap
, i
, ma
);
4723 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
4726 isl_basic_map_free(bmap
);
4731 /* Given a basic set where each set dimension is defined
4732 * in terms of the parameters using an equality,
4733 * extract an isl_multi_aff that expresses the set dimensions in terms
4734 * of the parameters.
4736 __isl_give isl_multi_aff
*isl_multi_aff_from_basic_set_equalities(
4737 __isl_take isl_basic_set
*bset
)
4739 return extract_isl_multi_aff_from_basic_map(bset
);
4742 /* Create an isl_pw_multi_aff that is equivalent to
4743 * isl_map_intersect_domain(isl_map_from_basic_map(bmap), domain).
4744 * The given basic map is such that each output dimension is defined
4745 * in terms of the parameters and input dimensions using an equality.
4747 * Since some applications expect the result of isl_pw_multi_aff_from_map
4748 * to only contain integer affine expressions, we compute the floor
4749 * of the expression before returning.
4751 * Remove all constraints involving local variables without
4752 * an explicit representation (resulting in the removal of those
4753 * local variables) prior to the actual extraction to ensure
4754 * that the local spaces in which the resulting affine expressions
4755 * are created do not contain any unknown local variables.
4756 * Removing such constraints is safe because constraints involving
4757 * unknown local variables are not used to determine whether
4758 * a basic map is obviously single-valued.
4760 static __isl_give isl_pw_multi_aff
*plain_pw_multi_aff_from_map(
4761 __isl_take isl_set
*domain
, __isl_take isl_basic_map
*bmap
)
4765 bmap
= isl_basic_map_drop_constraint_involving_unknown_divs(bmap
);
4766 ma
= extract_isl_multi_aff_from_basic_map(bmap
);
4767 ma
= isl_multi_aff_floor(ma
);
4768 return isl_pw_multi_aff_alloc(domain
, ma
);
4771 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4772 * This obviously only works if the input "map" is single-valued.
4773 * If so, we compute the lexicographic minimum of the image in the form
4774 * of an isl_pw_multi_aff. Since the image is unique, it is equal
4775 * to its lexicographic minimum.
4776 * If the input is not single-valued, we produce an error.
4778 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_base(
4779 __isl_take isl_map
*map
)
4783 isl_pw_multi_aff
*pma
;
4785 sv
= isl_map_is_single_valued(map
);
4789 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
4790 "map is not single-valued", goto error
);
4791 map
= isl_map_make_disjoint(map
);
4795 pma
= isl_pw_multi_aff_empty(isl_map_get_space(map
));
4797 for (i
= 0; i
< map
->n
; ++i
) {
4798 isl_pw_multi_aff
*pma_i
;
4799 isl_basic_map
*bmap
;
4800 bmap
= isl_basic_map_copy(map
->p
[i
]);
4801 pma_i
= isl_basic_map_lexmin_pw_multi_aff(bmap
);
4802 pma
= isl_pw_multi_aff_add_disjoint(pma
, pma_i
);
4812 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
4813 * taking into account that the output dimension at position "d"
4814 * can be represented as
4816 * x = floor((e(...) + c1) / m)
4818 * given that constraint "i" is of the form
4820 * e(...) + c1 - m x >= 0
4823 * Let "map" be of the form
4827 * We construct a mapping
4829 * A -> [A -> x = floor(...)]
4831 * apply that to the map, obtaining
4833 * [A -> x = floor(...)] -> B
4835 * and equate dimension "d" to x.
4836 * We then compute a isl_pw_multi_aff representation of the resulting map
4837 * and plug in the mapping above.
4839 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_div(
4840 __isl_take isl_map
*map
, __isl_take isl_basic_map
*hull
, int d
, int i
)
4844 isl_local_space
*ls
;
4852 isl_pw_multi_aff
*pma
;
4855 is_set
= isl_map_is_set(map
);
4859 offset
= isl_basic_map_offset(hull
, isl_dim_out
);
4860 ctx
= isl_map_get_ctx(map
);
4861 space
= isl_space_domain(isl_map_get_space(map
));
4862 n_in
= isl_space_dim(space
, isl_dim_set
);
4863 n
= isl_space_dim(space
, isl_dim_all
);
4865 v
= isl_vec_alloc(ctx
, 1 + 1 + n
);
4867 isl_int_neg(v
->el
[0], hull
->ineq
[i
][offset
+ d
]);
4868 isl_seq_cpy(v
->el
+ 1, hull
->ineq
[i
], 1 + n
);
4870 isl_basic_map_free(hull
);
4872 ls
= isl_local_space_from_space(isl_space_copy(space
));
4873 aff
= isl_aff_alloc_vec(ls
, v
);
4874 aff
= isl_aff_floor(aff
);
4876 isl_space_free(space
);
4877 ma
= isl_multi_aff_from_aff(aff
);
4879 ma
= isl_multi_aff_identity(isl_space_map_from_set(space
));
4880 ma
= isl_multi_aff_range_product(ma
,
4881 isl_multi_aff_from_aff(aff
));
4884 insert
= isl_map_from_multi_aff(isl_multi_aff_copy(ma
));
4885 map
= isl_map_apply_domain(map
, insert
);
4886 map
= isl_map_equate(map
, isl_dim_in
, n_in
, isl_dim_out
, d
);
4887 pma
= isl_pw_multi_aff_from_map(map
);
4888 pma
= isl_pw_multi_aff_pullback_multi_aff(pma
, ma
);
4893 isl_basic_map_free(hull
);
4897 /* Is constraint "c" of the form
4899 * e(...) + c1 - m x >= 0
4903 * -e(...) + c2 + m x >= 0
4905 * where m > 1 and e only depends on parameters and input dimemnsions?
4907 * "offset" is the offset of the output dimensions
4908 * "pos" is the position of output dimension x.
4910 static int is_potential_div_constraint(isl_int
*c
, int offset
, int d
, int total
)
4912 if (isl_int_is_zero(c
[offset
+ d
]))
4914 if (isl_int_is_one(c
[offset
+ d
]))
4916 if (isl_int_is_negone(c
[offset
+ d
]))
4918 if (isl_seq_first_non_zero(c
+ offset
, d
) != -1)
4920 if (isl_seq_first_non_zero(c
+ offset
+ d
+ 1,
4921 total
- (offset
+ d
+ 1)) != -1)
4926 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4928 * As a special case, we first check if there is any pair of constraints,
4929 * shared by all the basic maps in "map" that force a given dimension
4930 * to be equal to the floor of some affine combination of the input dimensions.
4932 * In particular, if we can find two constraints
4934 * e(...) + c1 - m x >= 0 i.e., m x <= e(...) + c1
4938 * -e(...) + c2 + m x >= 0 i.e., m x >= e(...) - c2
4940 * where m > 1 and e only depends on parameters and input dimemnsions,
4943 * c1 + c2 < m i.e., -c2 >= c1 - (m - 1)
4945 * then we know that we can take
4947 * x = floor((e(...) + c1) / m)
4949 * without having to perform any computation.
4951 * Note that we know that
4955 * If c1 + c2 were 0, then we would have detected an equality during
4956 * simplification. If c1 + c2 were negative, then we would have detected
4959 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_check_div(
4960 __isl_take isl_map
*map
)
4966 isl_basic_map
*hull
;
4968 hull
= isl_map_unshifted_simple_hull(isl_map_copy(map
));
4973 dim
= isl_map_dim(map
, isl_dim_out
);
4974 offset
= isl_basic_map_offset(hull
, isl_dim_out
);
4975 total
= 1 + isl_basic_map_total_dim(hull
);
4977 for (d
= 0; d
< dim
; ++d
) {
4978 for (i
= 0; i
< n
; ++i
) {
4979 if (!is_potential_div_constraint(hull
->ineq
[i
],
4982 for (j
= i
+ 1; j
< n
; ++j
) {
4983 if (!isl_seq_is_neg(hull
->ineq
[i
] + 1,
4984 hull
->ineq
[j
] + 1, total
- 1))
4986 isl_int_add(sum
, hull
->ineq
[i
][0],
4988 if (isl_int_abs_lt(sum
,
4989 hull
->ineq
[i
][offset
+ d
]))
4996 if (isl_int_is_pos(hull
->ineq
[j
][offset
+ d
]))
4998 return pw_multi_aff_from_map_div(map
, hull
, d
, j
);
5002 isl_basic_map_free(hull
);
5003 return pw_multi_aff_from_map_base(map
);
5006 isl_basic_map_free(hull
);
5010 /* Given an affine expression
5012 * [A -> B] -> f(A,B)
5014 * construct an isl_multi_aff
5018 * such that dimension "d" in B' is set to "aff" and the remaining
5019 * dimensions are set equal to the corresponding dimensions in B.
5020 * "n_in" is the dimension of the space A.
5021 * "n_out" is the dimension of the space B.
5023 * If "is_set" is set, then the affine expression is of the form
5027 * and we construct an isl_multi_aff
5031 static __isl_give isl_multi_aff
*range_map(__isl_take isl_aff
*aff
, int d
,
5032 unsigned n_in
, unsigned n_out
, int is_set
)
5036 isl_space
*space
, *space2
;
5037 isl_local_space
*ls
;
5039 space
= isl_aff_get_domain_space(aff
);
5040 ls
= isl_local_space_from_space(isl_space_copy(space
));
5041 space2
= isl_space_copy(space
);
5043 space2
= isl_space_range(isl_space_unwrap(space2
));
5044 space
= isl_space_map_from_domain_and_range(space
, space2
);
5045 ma
= isl_multi_aff_alloc(space
);
5046 ma
= isl_multi_aff_set_aff(ma
, d
, aff
);
5048 for (i
= 0; i
< n_out
; ++i
) {
5051 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
5052 isl_dim_set
, n_in
+ i
);
5053 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
5056 isl_local_space_free(ls
);
5061 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
5062 * taking into account that the dimension at position "d" can be written as
5064 * x = m a + f(..) (1)
5066 * where m is equal to "gcd".
5067 * "i" is the index of the equality in "hull" that defines f(..).
5068 * In particular, the equality is of the form
5070 * f(..) - x + m g(existentials) = 0
5074 * -f(..) + x + m g(existentials) = 0
5076 * We basically plug (1) into "map", resulting in a map with "a"
5077 * in the range instead of "x". The corresponding isl_pw_multi_aff
5078 * defining "a" is then plugged back into (1) to obtain a definition for "x".
5080 * Specifically, given the input map
5084 * We first wrap it into a set
5088 * and define (1) on top of the corresponding space, resulting in "aff".
5089 * We use this to create an isl_multi_aff that maps the output position "d"
5090 * from "a" to "x", leaving all other (intput and output) dimensions unchanged.
5091 * We plug this into the wrapped map, unwrap the result and compute the
5092 * corresponding isl_pw_multi_aff.
5093 * The result is an expression
5101 * so that we can plug that into "aff", after extending the latter to
5107 * If "map" is actually a set, then there is no "A" space, meaning
5108 * that we do not need to perform any wrapping, and that the result
5109 * of the recursive call is of the form
5113 * which is plugged into a mapping of the form
5117 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_stride(
5118 __isl_take isl_map
*map
, __isl_take isl_basic_map
*hull
, int d
, int i
,
5123 isl_local_space
*ls
;
5126 isl_pw_multi_aff
*pma
, *id
;
5132 is_set
= isl_map_is_set(map
);
5136 n_in
= isl_basic_map_dim(hull
, isl_dim_in
);
5137 n_out
= isl_basic_map_dim(hull
, isl_dim_out
);
5138 o_out
= isl_basic_map_offset(hull
, isl_dim_out
);
5143 set
= isl_map_wrap(map
);
5144 space
= isl_space_map_from_set(isl_set_get_space(set
));
5145 ma
= isl_multi_aff_identity(space
);
5146 ls
= isl_local_space_from_space(isl_set_get_space(set
));
5147 aff
= isl_aff_alloc(ls
);
5149 isl_int_set_si(aff
->v
->el
[0], 1);
5150 if (isl_int_is_one(hull
->eq
[i
][o_out
+ d
]))
5151 isl_seq_neg(aff
->v
->el
+ 1, hull
->eq
[i
],
5154 isl_seq_cpy(aff
->v
->el
+ 1, hull
->eq
[i
],
5156 isl_int_set(aff
->v
->el
[1 + o_out
+ d
], gcd
);
5158 ma
= isl_multi_aff_set_aff(ma
, n_in
+ d
, isl_aff_copy(aff
));
5159 set
= isl_set_preimage_multi_aff(set
, ma
);
5161 ma
= range_map(aff
, d
, n_in
, n_out
, is_set
);
5166 map
= isl_set_unwrap(set
);
5167 pma
= isl_pw_multi_aff_from_map(map
);
5170 space
= isl_pw_multi_aff_get_domain_space(pma
);
5171 space
= isl_space_map_from_set(space
);
5172 id
= isl_pw_multi_aff_identity(space
);
5173 pma
= isl_pw_multi_aff_range_product(id
, pma
);
5175 id
= isl_pw_multi_aff_from_multi_aff(ma
);
5176 pma
= isl_pw_multi_aff_pullback_pw_multi_aff(id
, pma
);
5178 isl_basic_map_free(hull
);
5182 isl_basic_map_free(hull
);
5186 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5187 * "hull" contains the equalities valid for "map".
5189 * Check if any of the output dimensions is "strided".
5190 * That is, we check if it can be written as
5194 * with m greater than 1, a some combination of existentially quantified
5195 * variables and f an expression in the parameters and input dimensions.
5196 * If so, we remove the stride in pw_multi_aff_from_map_stride.
5198 * Otherwise, we continue with pw_multi_aff_from_map_check_div for a further
5201 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_check_strides(
5202 __isl_take isl_map
*map
, __isl_take isl_basic_map
*hull
)
5211 n_div
= isl_basic_map_dim(hull
, isl_dim_div
);
5212 o_div
= isl_basic_map_offset(hull
, isl_dim_div
);
5215 isl_basic_map_free(hull
);
5216 return pw_multi_aff_from_map_check_div(map
);
5221 n_out
= isl_basic_map_dim(hull
, isl_dim_out
);
5222 o_out
= isl_basic_map_offset(hull
, isl_dim_out
);
5224 for (i
= 0; i
< n_out
; ++i
) {
5225 for (j
= 0; j
< hull
->n_eq
; ++j
) {
5226 isl_int
*eq
= hull
->eq
[j
];
5227 isl_pw_multi_aff
*res
;
5229 if (!isl_int_is_one(eq
[o_out
+ i
]) &&
5230 !isl_int_is_negone(eq
[o_out
+ i
]))
5232 if (isl_seq_first_non_zero(eq
+ o_out
, i
) != -1)
5234 if (isl_seq_first_non_zero(eq
+ o_out
+ i
+ 1,
5235 n_out
- (i
+ 1)) != -1)
5237 isl_seq_gcd(eq
+ o_div
, n_div
, &gcd
);
5238 if (isl_int_is_zero(gcd
))
5240 if (isl_int_is_one(gcd
))
5243 res
= pw_multi_aff_from_map_stride(map
, hull
,
5251 isl_basic_map_free(hull
);
5252 return pw_multi_aff_from_map_check_div(map
);
5255 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5257 * As a special case, we first check if all output dimensions are uniquely
5258 * defined in terms of the parameters and input dimensions over the entire
5259 * domain. If so, we extract the desired isl_pw_multi_aff directly
5260 * from the affine hull of "map" and its domain.
5262 * Otherwise, continue with pw_multi_aff_from_map_check_strides for more
5265 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_map(__isl_take isl_map
*map
)
5268 isl_basic_map
*hull
;
5273 if (isl_map_n_basic_map(map
) == 1) {
5274 hull
= isl_map_unshifted_simple_hull(isl_map_copy(map
));
5275 hull
= isl_basic_map_plain_affine_hull(hull
);
5276 sv
= isl_basic_map_plain_is_single_valued(hull
);
5278 return plain_pw_multi_aff_from_map(isl_map_domain(map
),
5280 isl_basic_map_free(hull
);
5282 map
= isl_map_detect_equalities(map
);
5283 hull
= isl_map_unshifted_simple_hull(isl_map_copy(map
));
5284 sv
= isl_basic_map_plain_is_single_valued(hull
);
5286 return plain_pw_multi_aff_from_map(isl_map_domain(map
), hull
);
5288 return pw_multi_aff_from_map_check_strides(map
, hull
);
5289 isl_basic_map_free(hull
);
5294 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_set(__isl_take isl_set
*set
)
5296 return isl_pw_multi_aff_from_map(set
);
5299 /* Convert "map" into an isl_pw_multi_aff (if possible) and
5302 static isl_stat
pw_multi_aff_from_map(__isl_take isl_map
*map
, void *user
)
5304 isl_union_pw_multi_aff
**upma
= user
;
5305 isl_pw_multi_aff
*pma
;
5307 pma
= isl_pw_multi_aff_from_map(map
);
5308 *upma
= isl_union_pw_multi_aff_add_pw_multi_aff(*upma
, pma
);
5310 return *upma
? isl_stat_ok
: isl_stat_error
;
5313 /* Create an isl_union_pw_multi_aff with the given isl_aff on a universe
5316 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_aff(
5317 __isl_take isl_aff
*aff
)
5320 isl_pw_multi_aff
*pma
;
5322 ma
= isl_multi_aff_from_aff(aff
);
5323 pma
= isl_pw_multi_aff_from_multi_aff(ma
);
5324 return isl_union_pw_multi_aff_from_pw_multi_aff(pma
);
5327 /* Try and create an isl_union_pw_multi_aff that is equivalent
5328 * to the given isl_union_map.
5329 * The isl_union_map is required to be single-valued in each space.
5330 * Otherwise, an error is produced.
5332 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_union_map(
5333 __isl_take isl_union_map
*umap
)
5336 isl_union_pw_multi_aff
*upma
;
5338 space
= isl_union_map_get_space(umap
);
5339 upma
= isl_union_pw_multi_aff_empty(space
);
5340 if (isl_union_map_foreach_map(umap
, &pw_multi_aff_from_map
, &upma
) < 0)
5341 upma
= isl_union_pw_multi_aff_free(upma
);
5342 isl_union_map_free(umap
);
5347 /* Try and create an isl_union_pw_multi_aff that is equivalent
5348 * to the given isl_union_set.
5349 * The isl_union_set is required to be a singleton in each space.
5350 * Otherwise, an error is produced.
5352 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_union_set(
5353 __isl_take isl_union_set
*uset
)
5355 return isl_union_pw_multi_aff_from_union_map(uset
);
5358 /* Return the piecewise affine expression "set ? 1 : 0".
5360 __isl_give isl_pw_aff
*isl_set_indicator_function(__isl_take isl_set
*set
)
5363 isl_space
*space
= isl_set_get_space(set
);
5364 isl_local_space
*ls
= isl_local_space_from_space(space
);
5365 isl_aff
*zero
= isl_aff_zero_on_domain(isl_local_space_copy(ls
));
5366 isl_aff
*one
= isl_aff_zero_on_domain(ls
);
5368 one
= isl_aff_add_constant_si(one
, 1);
5369 pa
= isl_pw_aff_alloc(isl_set_copy(set
), one
);
5370 set
= isl_set_complement(set
);
5371 pa
= isl_pw_aff_add_disjoint(pa
, isl_pw_aff_alloc(set
, zero
));
5376 /* Plug in "subs" for dimension "type", "pos" of "aff".
5378 * Let i be the dimension to replace and let "subs" be of the form
5382 * and "aff" of the form
5388 * (a f + d g')/(m d)
5390 * where g' is the result of plugging in "subs" in each of the integer
5393 __isl_give isl_aff
*isl_aff_substitute(__isl_take isl_aff
*aff
,
5394 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
5399 aff
= isl_aff_cow(aff
);
5401 return isl_aff_free(aff
);
5403 ctx
= isl_aff_get_ctx(aff
);
5404 if (!isl_space_is_equal(aff
->ls
->dim
, subs
->ls
->dim
))
5405 isl_die(ctx
, isl_error_invalid
,
5406 "spaces don't match", return isl_aff_free(aff
));
5407 if (isl_local_space_dim(subs
->ls
, isl_dim_div
) != 0)
5408 isl_die(ctx
, isl_error_unsupported
,
5409 "cannot handle divs yet", return isl_aff_free(aff
));
5411 aff
->ls
= isl_local_space_substitute(aff
->ls
, type
, pos
, subs
);
5413 return isl_aff_free(aff
);
5415 aff
->v
= isl_vec_cow(aff
->v
);
5417 return isl_aff_free(aff
);
5419 pos
+= isl_local_space_offset(aff
->ls
, type
);
5422 isl_seq_substitute(aff
->v
->el
, pos
, subs
->v
->el
,
5423 aff
->v
->size
, subs
->v
->size
, v
);
5429 /* Plug in "subs" for dimension "type", "pos" in each of the affine
5430 * expressions in "maff".
5432 __isl_give isl_multi_aff
*isl_multi_aff_substitute(
5433 __isl_take isl_multi_aff
*maff
, enum isl_dim_type type
, unsigned pos
,
5434 __isl_keep isl_aff
*subs
)
5438 maff
= isl_multi_aff_cow(maff
);
5440 return isl_multi_aff_free(maff
);
5442 if (type
== isl_dim_in
)
5445 for (i
= 0; i
< maff
->n
; ++i
) {
5446 maff
->u
.p
[i
] = isl_aff_substitute(maff
->u
.p
[i
],
5449 return isl_multi_aff_free(maff
);
5455 /* Plug in "subs" for dimension "type", "pos" of "pma".
5457 * pma is of the form
5461 * while subs is of the form
5463 * v' = B_j(v) -> S_j
5465 * Each pair i,j such that C_ij = A_i \cap B_i is non-empty
5466 * has a contribution in the result, in particular
5468 * C_ij(S_j) -> M_i(S_j)
5470 * Note that plugging in S_j in C_ij may also result in an empty set
5471 * and this contribution should simply be discarded.
5473 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_substitute(
5474 __isl_take isl_pw_multi_aff
*pma
, enum isl_dim_type type
, unsigned pos
,
5475 __isl_keep isl_pw_aff
*subs
)
5478 isl_pw_multi_aff
*res
;
5481 return isl_pw_multi_aff_free(pma
);
5483 n
= pma
->n
* subs
->n
;
5484 res
= isl_pw_multi_aff_alloc_size(isl_space_copy(pma
->dim
), n
);
5486 for (i
= 0; i
< pma
->n
; ++i
) {
5487 for (j
= 0; j
< subs
->n
; ++j
) {
5489 isl_multi_aff
*res_ij
;
5492 common
= isl_set_intersect(
5493 isl_set_copy(pma
->p
[i
].set
),
5494 isl_set_copy(subs
->p
[j
].set
));
5495 common
= isl_set_substitute(common
,
5496 type
, pos
, subs
->p
[j
].aff
);
5497 empty
= isl_set_plain_is_empty(common
);
5498 if (empty
< 0 || empty
) {
5499 isl_set_free(common
);
5505 res_ij
= isl_multi_aff_substitute(
5506 isl_multi_aff_copy(pma
->p
[i
].maff
),
5507 type
, pos
, subs
->p
[j
].aff
);
5509 res
= isl_pw_multi_aff_add_piece(res
, common
, res_ij
);
5513 isl_pw_multi_aff_free(pma
);
5516 isl_pw_multi_aff_free(pma
);
5517 isl_pw_multi_aff_free(res
);
5521 /* Compute the preimage of a range of dimensions in the affine expression "src"
5522 * under "ma" and put the result in "dst". The number of dimensions in "src"
5523 * that precede the range is given by "n_before". The number of dimensions
5524 * in the range is given by the number of output dimensions of "ma".
5525 * The number of dimensions that follow the range is given by "n_after".
5526 * If "has_denom" is set (to one),
5527 * then "src" and "dst" have an extra initial denominator.
5528 * "n_div_ma" is the number of existentials in "ma"
5529 * "n_div_bset" is the number of existentials in "src"
5530 * The resulting "dst" (which is assumed to have been allocated by
5531 * the caller) contains coefficients for both sets of existentials,
5532 * first those in "ma" and then those in "src".
5533 * f, c1, c2 and g are temporary objects that have been initialized
5536 * Let src represent the expression
5538 * (a(p) + f_u u + b v + f_w w + c(divs))/d
5540 * and let ma represent the expressions
5542 * v_i = (r_i(p) + s_i(y) + t_i(divs'))/m_i
5544 * We start out with the following expression for dst:
5546 * (a(p) + f_u u + 0 y + f_w w + 0 divs' + c(divs) + f \sum_i b_i v_i)/d
5548 * with the multiplication factor f initially equal to 1
5549 * and f \sum_i b_i v_i kept separately.
5550 * For each x_i that we substitute, we multiply the numerator
5551 * (and denominator) of dst by c_1 = m_i and add the numerator
5552 * of the x_i expression multiplied by c_2 = f b_i,
5553 * after removing the common factors of c_1 and c_2.
5554 * The multiplication factor f also needs to be multiplied by c_1
5555 * for the next x_j, j > i.
5557 void isl_seq_preimage(isl_int
*dst
, isl_int
*src
,
5558 __isl_keep isl_multi_aff
*ma
, int n_before
, int n_after
,
5559 int n_div_ma
, int n_div_bmap
,
5560 isl_int f
, isl_int c1
, isl_int c2
, isl_int g
, int has_denom
)
5563 int n_param
, n_in
, n_out
;
5566 n_param
= isl_multi_aff_dim(ma
, isl_dim_param
);
5567 n_in
= isl_multi_aff_dim(ma
, isl_dim_in
);
5568 n_out
= isl_multi_aff_dim(ma
, isl_dim_out
);
5570 isl_seq_cpy(dst
, src
, has_denom
+ 1 + n_param
+ n_before
);
5571 o_dst
= o_src
= has_denom
+ 1 + n_param
+ n_before
;
5572 isl_seq_clr(dst
+ o_dst
, n_in
);
5575 isl_seq_cpy(dst
+ o_dst
, src
+ o_src
, n_after
);
5578 isl_seq_clr(dst
+ o_dst
, n_div_ma
);
5580 isl_seq_cpy(dst
+ o_dst
, src
+ o_src
, n_div_bmap
);
5582 isl_int_set_si(f
, 1);
5584 for (i
= 0; i
< n_out
; ++i
) {
5585 int offset
= has_denom
+ 1 + n_param
+ n_before
+ i
;
5587 if (isl_int_is_zero(src
[offset
]))
5589 isl_int_set(c1
, ma
->u
.p
[i
]->v
->el
[0]);
5590 isl_int_mul(c2
, f
, src
[offset
]);
5591 isl_int_gcd(g
, c1
, c2
);
5592 isl_int_divexact(c1
, c1
, g
);
5593 isl_int_divexact(c2
, c2
, g
);
5595 isl_int_mul(f
, f
, c1
);
5598 isl_seq_combine(dst
+ o_dst
, c1
, dst
+ o_dst
,
5599 c2
, ma
->u
.p
[i
]->v
->el
+ o_src
, 1 + n_param
);
5600 o_dst
+= 1 + n_param
;
5601 o_src
+= 1 + n_param
;
5602 isl_seq_scale(dst
+ o_dst
, dst
+ o_dst
, c1
, n_before
);
5604 isl_seq_combine(dst
+ o_dst
, c1
, dst
+ o_dst
,
5605 c2
, ma
->u
.p
[i
]->v
->el
+ o_src
, n_in
);
5608 isl_seq_scale(dst
+ o_dst
, dst
+ o_dst
, c1
, n_after
);
5610 isl_seq_combine(dst
+ o_dst
, c1
, dst
+ o_dst
,
5611 c2
, ma
->u
.p
[i
]->v
->el
+ o_src
, n_div_ma
);
5614 isl_seq_scale(dst
+ o_dst
, dst
+ o_dst
, c1
, n_div_bmap
);
5616 isl_int_mul(dst
[0], dst
[0], c1
);
5620 /* Compute the pullback of "aff" by the function represented by "ma".
5621 * In other words, plug in "ma" in "aff". The result is an affine expression
5622 * defined over the domain space of "ma".
5624 * If "aff" is represented by
5626 * (a(p) + b x + c(divs))/d
5628 * and ma is represented by
5630 * x = D(p) + F(y) + G(divs')
5632 * then the result is
5634 * (a(p) + b D(p) + b F(y) + b G(divs') + c(divs))/d
5636 * The divs in the local space of the input are similarly adjusted
5637 * through a call to isl_local_space_preimage_multi_aff.
5639 __isl_give isl_aff
*isl_aff_pullback_multi_aff(__isl_take isl_aff
*aff
,
5640 __isl_take isl_multi_aff
*ma
)
5642 isl_aff
*res
= NULL
;
5643 isl_local_space
*ls
;
5644 int n_div_aff
, n_div_ma
;
5645 isl_int f
, c1
, c2
, g
;
5647 ma
= isl_multi_aff_align_divs(ma
);
5651 n_div_aff
= isl_aff_dim(aff
, isl_dim_div
);
5652 n_div_ma
= ma
->n
? isl_aff_dim(ma
->u
.p
[0], isl_dim_div
) : 0;
5654 ls
= isl_aff_get_domain_local_space(aff
);
5655 ls
= isl_local_space_preimage_multi_aff(ls
, isl_multi_aff_copy(ma
));
5656 res
= isl_aff_alloc(ls
);
5665 isl_seq_preimage(res
->v
->el
, aff
->v
->el
, ma
, 0, 0, n_div_ma
, n_div_aff
,
5674 isl_multi_aff_free(ma
);
5675 res
= isl_aff_normalize(res
);
5679 isl_multi_aff_free(ma
);
5684 /* Compute the pullback of "aff1" by the function represented by "aff2".
5685 * In other words, plug in "aff2" in "aff1". The result is an affine expression
5686 * defined over the domain space of "aff1".
5688 * The domain of "aff1" should match the range of "aff2", which means
5689 * that it should be single-dimensional.
5691 __isl_give isl_aff
*isl_aff_pullback_aff(__isl_take isl_aff
*aff1
,
5692 __isl_take isl_aff
*aff2
)
5696 ma
= isl_multi_aff_from_aff(aff2
);
5697 return isl_aff_pullback_multi_aff(aff1
, ma
);
5700 /* Compute the pullback of "ma1" by the function represented by "ma2".
5701 * In other words, plug in "ma2" in "ma1".
5703 * The parameters of "ma1" and "ma2" are assumed to have been aligned.
5705 static __isl_give isl_multi_aff
*isl_multi_aff_pullback_multi_aff_aligned(
5706 __isl_take isl_multi_aff
*ma1
, __isl_take isl_multi_aff
*ma2
)
5709 isl_space
*space
= NULL
;
5711 ma2
= isl_multi_aff_align_divs(ma2
);
5712 ma1
= isl_multi_aff_cow(ma1
);
5716 space
= isl_space_join(isl_multi_aff_get_space(ma2
),
5717 isl_multi_aff_get_space(ma1
));
5719 for (i
= 0; i
< ma1
->n
; ++i
) {
5720 ma1
->u
.p
[i
] = isl_aff_pullback_multi_aff(ma1
->u
.p
[i
],
5721 isl_multi_aff_copy(ma2
));
5726 ma1
= isl_multi_aff_reset_space(ma1
, space
);
5727 isl_multi_aff_free(ma2
);
5730 isl_space_free(space
);
5731 isl_multi_aff_free(ma2
);
5732 isl_multi_aff_free(ma1
);
5736 /* Compute the pullback of "ma1" by the function represented by "ma2".
5737 * In other words, plug in "ma2" in "ma1".
5739 __isl_give isl_multi_aff
*isl_multi_aff_pullback_multi_aff(
5740 __isl_take isl_multi_aff
*ma1
, __isl_take isl_multi_aff
*ma2
)
5742 return isl_multi_aff_align_params_multi_multi_and(ma1
, ma2
,
5743 &isl_multi_aff_pullback_multi_aff_aligned
);
5746 /* Extend the local space of "dst" to include the divs
5747 * in the local space of "src".
5749 * If "src" does not have any divs or if the local spaces of "dst" and
5750 * "src" are the same, then no extension is required.
5752 __isl_give isl_aff
*isl_aff_align_divs(__isl_take isl_aff
*dst
,
5753 __isl_keep isl_aff
*src
)
5756 int src_n_div
, dst_n_div
;
5763 return isl_aff_free(dst
);
5765 ctx
= isl_aff_get_ctx(src
);
5766 equal
= isl_local_space_has_equal_space(src
->ls
, dst
->ls
);
5768 return isl_aff_free(dst
);
5770 isl_die(ctx
, isl_error_invalid
,
5771 "spaces don't match", goto error
);
5773 src_n_div
= isl_local_space_dim(src
->ls
, isl_dim_div
);
5776 equal
= isl_local_space_is_equal(src
->ls
, dst
->ls
);
5778 return isl_aff_free(dst
);
5782 dst_n_div
= isl_local_space_dim(dst
->ls
, isl_dim_div
);
5783 exp1
= isl_alloc_array(ctx
, int, src_n_div
);
5784 exp2
= isl_alloc_array(ctx
, int, dst_n_div
);
5785 if (!exp1
|| (dst_n_div
&& !exp2
))
5788 div
= isl_merge_divs(src
->ls
->div
, dst
->ls
->div
, exp1
, exp2
);
5789 dst
= isl_aff_expand_divs(dst
, div
, exp2
);
5797 return isl_aff_free(dst
);
5800 /* Adjust the local spaces of the affine expressions in "maff"
5801 * such that they all have the save divs.
5803 __isl_give isl_multi_aff
*isl_multi_aff_align_divs(
5804 __isl_take isl_multi_aff
*maff
)
5812 maff
= isl_multi_aff_cow(maff
);
5816 for (i
= 1; i
< maff
->n
; ++i
)
5817 maff
->u
.p
[0] = isl_aff_align_divs(maff
->u
.p
[0], maff
->u
.p
[i
]);
5818 for (i
= 1; i
< maff
->n
; ++i
) {
5819 maff
->u
.p
[i
] = isl_aff_align_divs(maff
->u
.p
[i
], maff
->u
.p
[0]);
5821 return isl_multi_aff_free(maff
);
5827 __isl_give isl_aff
*isl_aff_lift(__isl_take isl_aff
*aff
)
5829 aff
= isl_aff_cow(aff
);
5833 aff
->ls
= isl_local_space_lift(aff
->ls
);
5835 return isl_aff_free(aff
);
5840 /* Lift "maff" to a space with extra dimensions such that the result
5841 * has no more existentially quantified variables.
5842 * If "ls" is not NULL, then *ls is assigned the local space that lies
5843 * at the basis of the lifting applied to "maff".
5845 __isl_give isl_multi_aff
*isl_multi_aff_lift(__isl_take isl_multi_aff
*maff
,
5846 __isl_give isl_local_space
**ls
)
5860 isl_space
*space
= isl_multi_aff_get_domain_space(maff
);
5861 *ls
= isl_local_space_from_space(space
);
5863 return isl_multi_aff_free(maff
);
5868 maff
= isl_multi_aff_cow(maff
);
5869 maff
= isl_multi_aff_align_divs(maff
);
5873 n_div
= isl_aff_dim(maff
->u
.p
[0], isl_dim_div
);
5874 space
= isl_multi_aff_get_space(maff
);
5875 space
= isl_space_lift(isl_space_domain(space
), n_div
);
5876 space
= isl_space_extend_domain_with_range(space
,
5877 isl_multi_aff_get_space(maff
));
5879 return isl_multi_aff_free(maff
);
5880 isl_space_free(maff
->space
);
5881 maff
->space
= space
;
5884 *ls
= isl_aff_get_domain_local_space(maff
->u
.p
[0]);
5886 return isl_multi_aff_free(maff
);
5889 for (i
= 0; i
< maff
->n
; ++i
) {
5890 maff
->u
.p
[i
] = isl_aff_lift(maff
->u
.p
[i
]);
5898 isl_local_space_free(*ls
);
5899 return isl_multi_aff_free(maff
);
5903 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma".
5905 __isl_give isl_pw_aff
*isl_pw_multi_aff_get_pw_aff(
5906 __isl_keep isl_pw_multi_aff
*pma
, int pos
)
5916 n_out
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
5917 if (pos
< 0 || pos
>= n_out
)
5918 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
5919 "index out of bounds", return NULL
);
5921 space
= isl_pw_multi_aff_get_space(pma
);
5922 space
= isl_space_drop_dims(space
, isl_dim_out
,
5923 pos
+ 1, n_out
- pos
- 1);
5924 space
= isl_space_drop_dims(space
, isl_dim_out
, 0, pos
);
5926 pa
= isl_pw_aff_alloc_size(space
, pma
->n
);
5927 for (i
= 0; i
< pma
->n
; ++i
) {
5929 aff
= isl_multi_aff_get_aff(pma
->p
[i
].maff
, pos
);
5930 pa
= isl_pw_aff_add_piece(pa
, isl_set_copy(pma
->p
[i
].set
), aff
);
5936 /* Return an isl_pw_multi_aff with the given "set" as domain and
5937 * an unnamed zero-dimensional range.
5939 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_domain(
5940 __isl_take isl_set
*set
)
5945 space
= isl_set_get_space(set
);
5946 space
= isl_space_from_domain(space
);
5947 ma
= isl_multi_aff_zero(space
);
5948 return isl_pw_multi_aff_alloc(set
, ma
);
5951 /* Add an isl_pw_multi_aff with the given "set" as domain and
5952 * an unnamed zero-dimensional range to *user.
5954 static isl_stat
add_pw_multi_aff_from_domain(__isl_take isl_set
*set
,
5957 isl_union_pw_multi_aff
**upma
= user
;
5958 isl_pw_multi_aff
*pma
;
5960 pma
= isl_pw_multi_aff_from_domain(set
);
5961 *upma
= isl_union_pw_multi_aff_add_pw_multi_aff(*upma
, pma
);
5966 /* Return an isl_union_pw_multi_aff with the given "uset" as domain and
5967 * an unnamed zero-dimensional range.
5969 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_domain(
5970 __isl_take isl_union_set
*uset
)
5973 isl_union_pw_multi_aff
*upma
;
5978 space
= isl_union_set_get_space(uset
);
5979 upma
= isl_union_pw_multi_aff_empty(space
);
5981 if (isl_union_set_foreach_set(uset
,
5982 &add_pw_multi_aff_from_domain
, &upma
) < 0)
5985 isl_union_set_free(uset
);
5988 isl_union_set_free(uset
);
5989 isl_union_pw_multi_aff_free(upma
);
5993 /* Convert "pma" to an isl_map and add it to *umap.
5995 static isl_stat
map_from_pw_multi_aff(__isl_take isl_pw_multi_aff
*pma
,
5998 isl_union_map
**umap
= user
;
6001 map
= isl_map_from_pw_multi_aff(pma
);
6002 *umap
= isl_union_map_add_map(*umap
, map
);
6007 /* Construct a union map mapping the domain of the union
6008 * piecewise multi-affine expression to its range, with each dimension
6009 * in the range equated to the corresponding affine expression on its cell.
6011 __isl_give isl_union_map
*isl_union_map_from_union_pw_multi_aff(
6012 __isl_take isl_union_pw_multi_aff
*upma
)
6015 isl_union_map
*umap
;
6020 space
= isl_union_pw_multi_aff_get_space(upma
);
6021 umap
= isl_union_map_empty(space
);
6023 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma
,
6024 &map_from_pw_multi_aff
, &umap
) < 0)
6027 isl_union_pw_multi_aff_free(upma
);
6030 isl_union_pw_multi_aff_free(upma
);
6031 isl_union_map_free(umap
);
6035 /* Local data for bin_entry and the callback "fn".
6037 struct isl_union_pw_multi_aff_bin_data
{
6038 isl_union_pw_multi_aff
*upma2
;
6039 isl_union_pw_multi_aff
*res
;
6040 isl_pw_multi_aff
*pma
;
6041 isl_stat (*fn
)(__isl_take isl_pw_multi_aff
*pma
, void *user
);
6044 /* Given an isl_pw_multi_aff from upma1, store it in data->pma
6045 * and call data->fn for each isl_pw_multi_aff in data->upma2.
6047 static isl_stat
bin_entry(__isl_take isl_pw_multi_aff
*pma
, void *user
)
6049 struct isl_union_pw_multi_aff_bin_data
*data
= user
;
6053 r
= isl_union_pw_multi_aff_foreach_pw_multi_aff(data
->upma2
,
6055 isl_pw_multi_aff_free(pma
);
6060 /* Call "fn" on each pair of isl_pw_multi_affs in "upma1" and "upma2".
6061 * The isl_pw_multi_aff from upma1 is stored in data->pma (where data is
6062 * passed as user field) and the isl_pw_multi_aff from upma2 is available
6063 * as *entry. The callback should adjust data->res if desired.
6065 static __isl_give isl_union_pw_multi_aff
*bin_op(
6066 __isl_take isl_union_pw_multi_aff
*upma1
,
6067 __isl_take isl_union_pw_multi_aff
*upma2
,
6068 isl_stat (*fn
)(__isl_take isl_pw_multi_aff
*pma
, void *user
))
6071 struct isl_union_pw_multi_aff_bin_data data
= { NULL
, NULL
, NULL
, fn
};
6073 space
= isl_union_pw_multi_aff_get_space(upma2
);
6074 upma1
= isl_union_pw_multi_aff_align_params(upma1
, space
);
6075 space
= isl_union_pw_multi_aff_get_space(upma1
);
6076 upma2
= isl_union_pw_multi_aff_align_params(upma2
, space
);
6078 if (!upma1
|| !upma2
)
6082 data
.res
= isl_union_pw_multi_aff_alloc_same_size(upma1
);
6083 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma1
,
6084 &bin_entry
, &data
) < 0)
6087 isl_union_pw_multi_aff_free(upma1
);
6088 isl_union_pw_multi_aff_free(upma2
);
6091 isl_union_pw_multi_aff_free(upma1
);
6092 isl_union_pw_multi_aff_free(upma2
);
6093 isl_union_pw_multi_aff_free(data
.res
);
6097 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
6098 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
6100 static __isl_give isl_pw_multi_aff
*pw_multi_aff_range_product(
6101 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
6105 space
= isl_space_range_product(isl_pw_multi_aff_get_space(pma1
),
6106 isl_pw_multi_aff_get_space(pma2
));
6107 return isl_pw_multi_aff_on_shared_domain_in(pma1
, pma2
, space
,
6108 &isl_multi_aff_range_product
);
6111 /* Given two isl_pw_multi_affs A -> B and C -> D,
6112 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
6114 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_range_product(
6115 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
6117 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
6118 &pw_multi_aff_range_product
);
6121 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
6122 * construct an isl_pw_multi_aff (A * C) -> (B, D).
6124 static __isl_give isl_pw_multi_aff
*pw_multi_aff_flat_range_product(
6125 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
6129 space
= isl_space_range_product(isl_pw_multi_aff_get_space(pma1
),
6130 isl_pw_multi_aff_get_space(pma2
));
6131 space
= isl_space_flatten_range(space
);
6132 return isl_pw_multi_aff_on_shared_domain_in(pma1
, pma2
, space
,
6133 &isl_multi_aff_flat_range_product
);
6136 /* Given two isl_pw_multi_affs A -> B and C -> D,
6137 * construct an isl_pw_multi_aff (A * C) -> (B, D).
6139 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_flat_range_product(
6140 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
6142 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
6143 &pw_multi_aff_flat_range_product
);
6146 /* If data->pma and "pma2" have the same domain space, then compute
6147 * their flat range product and the result to data->res.
6149 static isl_stat
flat_range_product_entry(__isl_take isl_pw_multi_aff
*pma2
,
6152 struct isl_union_pw_multi_aff_bin_data
*data
= user
;
6154 if (!isl_space_tuple_is_equal(data
->pma
->dim
, isl_dim_in
,
6155 pma2
->dim
, isl_dim_in
)) {
6156 isl_pw_multi_aff_free(pma2
);
6160 pma2
= isl_pw_multi_aff_flat_range_product(
6161 isl_pw_multi_aff_copy(data
->pma
), pma2
);
6163 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma2
);
6168 /* Given two isl_union_pw_multi_affs A -> B and C -> D,
6169 * construct an isl_union_pw_multi_aff (A * C) -> (B, D).
6171 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_flat_range_product(
6172 __isl_take isl_union_pw_multi_aff
*upma1
,
6173 __isl_take isl_union_pw_multi_aff
*upma2
)
6175 return bin_op(upma1
, upma2
, &flat_range_product_entry
);
6178 /* Replace the affine expressions at position "pos" in "pma" by "pa".
6179 * The parameters are assumed to have been aligned.
6181 * The implementation essentially performs an isl_pw_*_on_shared_domain,
6182 * except that it works on two different isl_pw_* types.
6184 static __isl_give isl_pw_multi_aff
*pw_multi_aff_set_pw_aff(
6185 __isl_take isl_pw_multi_aff
*pma
, unsigned pos
,
6186 __isl_take isl_pw_aff
*pa
)
6189 isl_pw_multi_aff
*res
= NULL
;
6194 if (!isl_space_tuple_is_equal(pma
->dim
, isl_dim_in
,
6195 pa
->dim
, isl_dim_in
))
6196 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
6197 "domains don't match", goto error
);
6198 if (pos
>= isl_pw_multi_aff_dim(pma
, isl_dim_out
))
6199 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
6200 "index out of bounds", goto error
);
6203 res
= isl_pw_multi_aff_alloc_size(isl_pw_multi_aff_get_space(pma
), n
);
6205 for (i
= 0; i
< pma
->n
; ++i
) {
6206 for (j
= 0; j
< pa
->n
; ++j
) {
6208 isl_multi_aff
*res_ij
;
6211 common
= isl_set_intersect(isl_set_copy(pma
->p
[i
].set
),
6212 isl_set_copy(pa
->p
[j
].set
));
6213 empty
= isl_set_plain_is_empty(common
);
6214 if (empty
< 0 || empty
) {
6215 isl_set_free(common
);
6221 res_ij
= isl_multi_aff_set_aff(
6222 isl_multi_aff_copy(pma
->p
[i
].maff
), pos
,
6223 isl_aff_copy(pa
->p
[j
].aff
));
6224 res_ij
= isl_multi_aff_gist(res_ij
,
6225 isl_set_copy(common
));
6227 res
= isl_pw_multi_aff_add_piece(res
, common
, res_ij
);
6231 isl_pw_multi_aff_free(pma
);
6232 isl_pw_aff_free(pa
);
6235 isl_pw_multi_aff_free(pma
);
6236 isl_pw_aff_free(pa
);
6237 return isl_pw_multi_aff_free(res
);
6240 /* Replace the affine expressions at position "pos" in "pma" by "pa".
6242 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_set_pw_aff(
6243 __isl_take isl_pw_multi_aff
*pma
, unsigned pos
,
6244 __isl_take isl_pw_aff
*pa
)
6246 isl_bool equal_params
;
6250 equal_params
= isl_space_has_equal_params(pma
->dim
, pa
->dim
);
6251 if (equal_params
< 0)
6254 return pw_multi_aff_set_pw_aff(pma
, pos
, pa
);
6255 if (isl_pw_multi_aff_check_named_params(pma
) < 0 ||
6256 isl_pw_aff_check_named_params(pa
) < 0)
6258 pma
= isl_pw_multi_aff_align_params(pma
, isl_pw_aff_get_space(pa
));
6259 pa
= isl_pw_aff_align_params(pa
, isl_pw_multi_aff_get_space(pma
));
6260 return pw_multi_aff_set_pw_aff(pma
, pos
, pa
);
6262 isl_pw_multi_aff_free(pma
);
6263 isl_pw_aff_free(pa
);
6267 /* Do the parameters of "pa" match those of "space"?
6269 isl_bool
isl_pw_aff_matching_params(__isl_keep isl_pw_aff
*pa
,
6270 __isl_keep isl_space
*space
)
6272 isl_space
*pa_space
;
6276 return isl_bool_error
;
6278 pa_space
= isl_pw_aff_get_space(pa
);
6280 match
= isl_space_has_equal_params(space
, pa_space
);
6282 isl_space_free(pa_space
);
6286 /* Check that the domain space of "pa" matches "space".
6288 isl_stat
isl_pw_aff_check_match_domain_space(__isl_keep isl_pw_aff
*pa
,
6289 __isl_keep isl_space
*space
)
6291 isl_space
*pa_space
;
6295 return isl_stat_error
;
6297 pa_space
= isl_pw_aff_get_space(pa
);
6299 match
= isl_space_has_equal_params(space
, pa_space
);
6303 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
6304 "parameters don't match", goto error
);
6305 match
= isl_space_tuple_is_equal(space
, isl_dim_in
,
6306 pa_space
, isl_dim_in
);
6310 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
6311 "domains don't match", goto error
);
6312 isl_space_free(pa_space
);
6315 isl_space_free(pa_space
);
6316 return isl_stat_error
;
6324 #include <isl_multi_explicit_domain.c>
6325 #include <isl_multi_pw_aff_explicit_domain.c>
6326 #include <isl_multi_templ.c>
6327 #include <isl_multi_apply_set.c>
6328 #include <isl_multi_coalesce.c>
6329 #include <isl_multi_dims.c>
6330 #include <isl_multi_gist.c>
6331 #include <isl_multi_hash.c>
6332 #include <isl_multi_align_set.c>
6333 #include <isl_multi_intersect.c>
6335 /* Does "mpa" have a non-trivial explicit domain?
6337 * The explicit domain, if present, is trivial if it represents
6338 * an (obviously) universe set.
6340 isl_bool
isl_multi_pw_aff_has_non_trivial_domain(
6341 __isl_keep isl_multi_pw_aff
*mpa
)
6344 return isl_bool_error
;
6345 if (!isl_multi_pw_aff_has_explicit_domain(mpa
))
6346 return isl_bool_false
;
6347 return isl_bool_not(isl_set_plain_is_universe(mpa
->u
.dom
));
6350 /* Scale the elements of "pma" by the corresponding elements of "mv".
6352 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_scale_multi_val(
6353 __isl_take isl_pw_multi_aff
*pma
, __isl_take isl_multi_val
*mv
)
6356 isl_bool equal_params
;
6358 pma
= isl_pw_multi_aff_cow(pma
);
6361 if (!isl_space_tuple_is_equal(pma
->dim
, isl_dim_out
,
6362 mv
->space
, isl_dim_set
))
6363 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
6364 "spaces don't match", goto error
);
6365 equal_params
= isl_space_has_equal_params(pma
->dim
, mv
->space
);
6366 if (equal_params
< 0)
6368 if (!equal_params
) {
6369 pma
= isl_pw_multi_aff_align_params(pma
,
6370 isl_multi_val_get_space(mv
));
6371 mv
= isl_multi_val_align_params(mv
,
6372 isl_pw_multi_aff_get_space(pma
));
6377 for (i
= 0; i
< pma
->n
; ++i
) {
6378 pma
->p
[i
].maff
= isl_multi_aff_scale_multi_val(pma
->p
[i
].maff
,
6379 isl_multi_val_copy(mv
));
6380 if (!pma
->p
[i
].maff
)
6384 isl_multi_val_free(mv
);
6387 isl_multi_val_free(mv
);
6388 isl_pw_multi_aff_free(pma
);
6392 /* This function is called for each entry of an isl_union_pw_multi_aff.
6393 * If the space of the entry matches that of data->mv,
6394 * then apply isl_pw_multi_aff_scale_multi_val and return the result.
6395 * Otherwise, return an empty isl_pw_multi_aff.
6397 static __isl_give isl_pw_multi_aff
*union_pw_multi_aff_scale_multi_val_entry(
6398 __isl_take isl_pw_multi_aff
*pma
, void *user
)
6400 isl_multi_val
*mv
= user
;
6404 if (!isl_space_tuple_is_equal(pma
->dim
, isl_dim_out
,
6405 mv
->space
, isl_dim_set
)) {
6406 isl_space
*space
= isl_pw_multi_aff_get_space(pma
);
6407 isl_pw_multi_aff_free(pma
);
6408 return isl_pw_multi_aff_empty(space
);
6411 return isl_pw_multi_aff_scale_multi_val(pma
, isl_multi_val_copy(mv
));
6414 /* Scale the elements of "upma" by the corresponding elements of "mv",
6415 * for those entries that match the space of "mv".
6417 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_scale_multi_val(
6418 __isl_take isl_union_pw_multi_aff
*upma
, __isl_take isl_multi_val
*mv
)
6420 upma
= isl_union_pw_multi_aff_align_params(upma
,
6421 isl_multi_val_get_space(mv
));
6422 mv
= isl_multi_val_align_params(mv
,
6423 isl_union_pw_multi_aff_get_space(upma
));
6427 return isl_union_pw_multi_aff_transform(upma
,
6428 &union_pw_multi_aff_scale_multi_val_entry
, mv
);
6430 isl_multi_val_free(mv
);
6433 isl_multi_val_free(mv
);
6434 isl_union_pw_multi_aff_free(upma
);
6438 /* Construct and return a piecewise multi affine expression
6439 * in the given space with value zero in each of the output dimensions and
6440 * a universe domain.
6442 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_zero(__isl_take isl_space
*space
)
6444 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_zero(space
));
6447 /* Construct and return a piecewise multi affine expression
6448 * that is equal to the given piecewise affine expression.
6450 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_pw_aff(
6451 __isl_take isl_pw_aff
*pa
)
6455 isl_pw_multi_aff
*pma
;
6460 space
= isl_pw_aff_get_space(pa
);
6461 pma
= isl_pw_multi_aff_alloc_size(space
, pa
->n
);
6463 for (i
= 0; i
< pa
->n
; ++i
) {
6467 set
= isl_set_copy(pa
->p
[i
].set
);
6468 ma
= isl_multi_aff_from_aff(isl_aff_copy(pa
->p
[i
].aff
));
6469 pma
= isl_pw_multi_aff_add_piece(pma
, set
, ma
);
6472 isl_pw_aff_free(pa
);
6476 /* Construct a set or map mapping the shared (parameter) domain
6477 * of the piecewise affine expressions to the range of "mpa"
6478 * with each dimension in the range equated to the
6479 * corresponding piecewise affine expression.
6481 static __isl_give isl_map
*map_from_multi_pw_aff(
6482 __isl_take isl_multi_pw_aff
*mpa
)
6491 if (isl_space_dim(mpa
->space
, isl_dim_out
) != mpa
->n
)
6492 isl_die(isl_multi_pw_aff_get_ctx(mpa
), isl_error_internal
,
6493 "invalid space", goto error
);
6495 space
= isl_multi_pw_aff_get_domain_space(mpa
);
6496 map
= isl_map_universe(isl_space_from_domain(space
));
6498 for (i
= 0; i
< mpa
->n
; ++i
) {
6502 pa
= isl_pw_aff_copy(mpa
->u
.p
[i
]);
6503 map_i
= map_from_pw_aff(pa
);
6505 map
= isl_map_flat_range_product(map
, map_i
);
6508 map
= isl_map_reset_space(map
, isl_multi_pw_aff_get_space(mpa
));
6510 isl_multi_pw_aff_free(mpa
);
6513 isl_multi_pw_aff_free(mpa
);
6517 /* Construct a map mapping the shared domain
6518 * of the piecewise affine expressions to the range of "mpa"
6519 * with each dimension in the range equated to the
6520 * corresponding piecewise affine expression.
6522 __isl_give isl_map
*isl_map_from_multi_pw_aff(__isl_take isl_multi_pw_aff
*mpa
)
6526 if (isl_space_is_set(mpa
->space
))
6527 isl_die(isl_multi_pw_aff_get_ctx(mpa
), isl_error_internal
,
6528 "space of input is not a map", goto error
);
6530 return map_from_multi_pw_aff(mpa
);
6532 isl_multi_pw_aff_free(mpa
);
6536 /* Construct a set mapping the shared parameter domain
6537 * of the piecewise affine expressions to the space of "mpa"
6538 * with each dimension in the range equated to the
6539 * corresponding piecewise affine expression.
6541 __isl_give isl_set
*isl_set_from_multi_pw_aff(__isl_take isl_multi_pw_aff
*mpa
)
6545 if (!isl_space_is_set(mpa
->space
))
6546 isl_die(isl_multi_pw_aff_get_ctx(mpa
), isl_error_internal
,
6547 "space of input is not a set", goto error
);
6549 return map_from_multi_pw_aff(mpa
);
6551 isl_multi_pw_aff_free(mpa
);
6555 /* Construct and return a piecewise multi affine expression
6556 * that is equal to the given multi piecewise affine expression
6557 * on the shared domain of the piecewise affine expressions,
6558 * in the special case of a 0D multi piecewise affine expression.
6560 * Create a piecewise multi affine expression with the explicit domain of
6561 * the 0D multi piecewise affine expression as domain.
6563 static __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_multi_pw_aff_0D(
6564 __isl_take isl_multi_pw_aff
*mpa
)
6570 space
= isl_multi_pw_aff_get_space(mpa
);
6571 dom
= isl_multi_pw_aff_get_explicit_domain(mpa
);
6572 isl_multi_pw_aff_free(mpa
);
6574 ma
= isl_multi_aff_zero(space
);
6575 return isl_pw_multi_aff_alloc(dom
, ma
);
6578 /* Construct and return a piecewise multi affine expression
6579 * that is equal to the given multi piecewise affine expression
6580 * on the shared domain of the piecewise affine expressions.
6582 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_multi_pw_aff(
6583 __isl_take isl_multi_pw_aff
*mpa
)
6588 isl_pw_multi_aff
*pma
;
6594 return isl_pw_multi_aff_from_multi_pw_aff_0D(mpa
);
6596 space
= isl_multi_pw_aff_get_space(mpa
);
6597 pa
= isl_multi_pw_aff_get_pw_aff(mpa
, 0);
6598 pma
= isl_pw_multi_aff_from_pw_aff(pa
);
6600 for (i
= 1; i
< mpa
->n
; ++i
) {
6601 isl_pw_multi_aff
*pma_i
;
6603 pa
= isl_multi_pw_aff_get_pw_aff(mpa
, i
);
6604 pma_i
= isl_pw_multi_aff_from_pw_aff(pa
);
6605 pma
= isl_pw_multi_aff_range_product(pma
, pma_i
);
6608 pma
= isl_pw_multi_aff_reset_space(pma
, space
);
6610 isl_multi_pw_aff_free(mpa
);
6614 /* Construct and return a multi piecewise affine expression
6615 * that is equal to the given multi affine expression.
6617 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_from_multi_aff(
6618 __isl_take isl_multi_aff
*ma
)
6621 isl_multi_pw_aff
*mpa
;
6626 n
= isl_multi_aff_dim(ma
, isl_dim_out
);
6627 mpa
= isl_multi_pw_aff_alloc(isl_multi_aff_get_space(ma
));
6629 for (i
= 0; i
< n
; ++i
) {
6632 pa
= isl_pw_aff_from_aff(isl_multi_aff_get_aff(ma
, i
));
6633 mpa
= isl_multi_pw_aff_set_pw_aff(mpa
, i
, pa
);
6636 isl_multi_aff_free(ma
);
6640 /* Construct and return a multi piecewise affine expression
6641 * that is equal to the given piecewise multi affine expression.
6643 * If the resulting multi piecewise affine expression has
6644 * an explicit domain, then assign it the domain of the input.
6645 * In other cases, the domain is stored in the individual elements.
6647 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_from_pw_multi_aff(
6648 __isl_take isl_pw_multi_aff
*pma
)
6652 isl_multi_pw_aff
*mpa
;
6657 n
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
6658 space
= isl_pw_multi_aff_get_space(pma
);
6659 mpa
= isl_multi_pw_aff_alloc(space
);
6661 for (i
= 0; i
< n
; ++i
) {
6664 pa
= isl_pw_multi_aff_get_pw_aff(pma
, i
);
6665 mpa
= isl_multi_pw_aff_set_pw_aff(mpa
, i
, pa
);
6667 if (isl_multi_pw_aff_has_explicit_domain(mpa
)) {
6670 dom
= isl_pw_multi_aff_domain(isl_pw_multi_aff_copy(pma
));
6671 mpa
= isl_multi_pw_aff_intersect_domain(mpa
, dom
);
6674 isl_pw_multi_aff_free(pma
);
6678 /* Do "pa1" and "pa2" represent the same function?
6680 * We first check if they are obviously equal.
6681 * If not, we convert them to maps and check if those are equal.
6683 * If "pa1" or "pa2" contain any NaNs, then they are considered
6684 * not to be the same. A NaN is not equal to anything, not even
6687 isl_bool
isl_pw_aff_is_equal(__isl_keep isl_pw_aff
*pa1
,
6688 __isl_keep isl_pw_aff
*pa2
)
6692 isl_map
*map1
, *map2
;
6695 return isl_bool_error
;
6697 equal
= isl_pw_aff_plain_is_equal(pa1
, pa2
);
6698 if (equal
< 0 || equal
)
6700 has_nan
= either_involves_nan(pa1
, pa2
);
6702 return isl_bool_error
;
6704 return isl_bool_false
;
6706 map1
= map_from_pw_aff(isl_pw_aff_copy(pa1
));
6707 map2
= map_from_pw_aff(isl_pw_aff_copy(pa2
));
6708 equal
= isl_map_is_equal(map1
, map2
);
6715 /* Do "mpa1" and "mpa2" represent the same function?
6717 * Note that we cannot convert the entire isl_multi_pw_aff
6718 * to a map because the domains of the piecewise affine expressions
6719 * may not be the same.
6721 isl_bool
isl_multi_pw_aff_is_equal(__isl_keep isl_multi_pw_aff
*mpa1
,
6722 __isl_keep isl_multi_pw_aff
*mpa2
)
6725 isl_bool equal
, equal_params
;
6728 return isl_bool_error
;
6730 equal_params
= isl_space_has_equal_params(mpa1
->space
, mpa2
->space
);
6731 if (equal_params
< 0)
6732 return isl_bool_error
;
6733 if (!equal_params
) {
6734 if (!isl_space_has_named_params(mpa1
->space
))
6735 return isl_bool_false
;
6736 if (!isl_space_has_named_params(mpa2
->space
))
6737 return isl_bool_false
;
6738 mpa1
= isl_multi_pw_aff_copy(mpa1
);
6739 mpa2
= isl_multi_pw_aff_copy(mpa2
);
6740 mpa1
= isl_multi_pw_aff_align_params(mpa1
,
6741 isl_multi_pw_aff_get_space(mpa2
));
6742 mpa2
= isl_multi_pw_aff_align_params(mpa2
,
6743 isl_multi_pw_aff_get_space(mpa1
));
6744 equal
= isl_multi_pw_aff_is_equal(mpa1
, mpa2
);
6745 isl_multi_pw_aff_free(mpa1
);
6746 isl_multi_pw_aff_free(mpa2
);
6750 equal
= isl_space_is_equal(mpa1
->space
, mpa2
->space
);
6751 if (equal
< 0 || !equal
)
6754 for (i
= 0; i
< mpa1
->n
; ++i
) {
6755 equal
= isl_pw_aff_is_equal(mpa1
->u
.p
[i
], mpa2
->u
.p
[i
]);
6756 if (equal
< 0 || !equal
)
6760 return isl_bool_true
;
6763 /* Do "pma1" and "pma2" represent the same function?
6765 * First check if they are obviously equal.
6766 * If not, then convert them to maps and check if those are equal.
6768 * If "pa1" or "pa2" contain any NaNs, then they are considered
6769 * not to be the same. A NaN is not equal to anything, not even
6772 isl_bool
isl_pw_multi_aff_is_equal(__isl_keep isl_pw_multi_aff
*pma1
,
6773 __isl_keep isl_pw_multi_aff
*pma2
)
6777 isl_map
*map1
, *map2
;
6780 return isl_bool_error
;
6782 equal
= isl_pw_multi_aff_plain_is_equal(pma1
, pma2
);
6783 if (equal
< 0 || equal
)
6785 has_nan
= isl_pw_multi_aff_involves_nan(pma1
);
6786 if (has_nan
>= 0 && !has_nan
)
6787 has_nan
= isl_pw_multi_aff_involves_nan(pma2
);
6788 if (has_nan
< 0 || has_nan
)
6789 return isl_bool_not(has_nan
);
6791 map1
= isl_map_from_pw_multi_aff(isl_pw_multi_aff_copy(pma1
));
6792 map2
= isl_map_from_pw_multi_aff(isl_pw_multi_aff_copy(pma2
));
6793 equal
= isl_map_is_equal(map1
, map2
);
6800 /* Compute the pullback of "mpa" by the function represented by "ma".
6801 * In other words, plug in "ma" in "mpa".
6803 * The parameters of "mpa" and "ma" are assumed to have been aligned.
6805 * If "mpa" has an explicit domain, then it is this domain
6806 * that needs to undergo a pullback, i.e., a preimage.
6808 static __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_pullback_multi_aff_aligned(
6809 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_multi_aff
*ma
)
6812 isl_space
*space
= NULL
;
6814 mpa
= isl_multi_pw_aff_cow(mpa
);
6818 space
= isl_space_join(isl_multi_aff_get_space(ma
),
6819 isl_multi_pw_aff_get_space(mpa
));
6823 for (i
= 0; i
< mpa
->n
; ++i
) {
6824 mpa
->u
.p
[i
] = isl_pw_aff_pullback_multi_aff(mpa
->u
.p
[i
],
6825 isl_multi_aff_copy(ma
));
6829 if (isl_multi_pw_aff_has_explicit_domain(mpa
)) {
6830 mpa
->u
.dom
= isl_set_preimage_multi_aff(mpa
->u
.dom
,
6831 isl_multi_aff_copy(ma
));
6836 isl_multi_aff_free(ma
);
6837 isl_space_free(mpa
->space
);
6841 isl_space_free(space
);
6842 isl_multi_pw_aff_free(mpa
);
6843 isl_multi_aff_free(ma
);
6847 /* Compute the pullback of "mpa" by the function represented by "ma".
6848 * In other words, plug in "ma" in "mpa".
6850 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_pullback_multi_aff(
6851 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_multi_aff
*ma
)
6853 isl_bool equal_params
;
6857 equal_params
= isl_space_has_equal_params(mpa
->space
, ma
->space
);
6858 if (equal_params
< 0)
6861 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa
, ma
);
6862 mpa
= isl_multi_pw_aff_align_params(mpa
, isl_multi_aff_get_space(ma
));
6863 ma
= isl_multi_aff_align_params(ma
, isl_multi_pw_aff_get_space(mpa
));
6864 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa
, ma
);
6866 isl_multi_pw_aff_free(mpa
);
6867 isl_multi_aff_free(ma
);
6871 /* Compute the pullback of "mpa" by the function represented by "pma".
6872 * In other words, plug in "pma" in "mpa".
6874 * The parameters of "mpa" and "mpa" are assumed to have been aligned.
6876 * If "mpa" has an explicit domain, then it is this domain
6877 * that needs to undergo a pullback, i.e., a preimage.
6879 static __isl_give isl_multi_pw_aff
*
6880 isl_multi_pw_aff_pullback_pw_multi_aff_aligned(
6881 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_pw_multi_aff
*pma
)
6884 isl_space
*space
= NULL
;
6886 mpa
= isl_multi_pw_aff_cow(mpa
);
6890 space
= isl_space_join(isl_pw_multi_aff_get_space(pma
),
6891 isl_multi_pw_aff_get_space(mpa
));
6893 for (i
= 0; i
< mpa
->n
; ++i
) {
6894 mpa
->u
.p
[i
] = isl_pw_aff_pullback_pw_multi_aff_aligned(
6895 mpa
->u
.p
[i
], isl_pw_multi_aff_copy(pma
));
6899 if (isl_multi_pw_aff_has_explicit_domain(mpa
)) {
6900 mpa
->u
.dom
= isl_set_preimage_pw_multi_aff(mpa
->u
.dom
,
6901 isl_pw_multi_aff_copy(pma
));
6906 isl_pw_multi_aff_free(pma
);
6907 isl_space_free(mpa
->space
);
6911 isl_space_free(space
);
6912 isl_multi_pw_aff_free(mpa
);
6913 isl_pw_multi_aff_free(pma
);
6917 /* Compute the pullback of "mpa" by the function represented by "pma".
6918 * In other words, plug in "pma" in "mpa".
6920 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_pullback_pw_multi_aff(
6921 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_pw_multi_aff
*pma
)
6923 isl_bool equal_params
;
6927 equal_params
= isl_space_has_equal_params(mpa
->space
, pma
->dim
);
6928 if (equal_params
< 0)
6931 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa
, pma
);
6932 mpa
= isl_multi_pw_aff_align_params(mpa
,
6933 isl_pw_multi_aff_get_space(pma
));
6934 pma
= isl_pw_multi_aff_align_params(pma
,
6935 isl_multi_pw_aff_get_space(mpa
));
6936 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa
, pma
);
6938 isl_multi_pw_aff_free(mpa
);
6939 isl_pw_multi_aff_free(pma
);
6943 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
6944 * with the domain of "aff". The domain of the result is the same
6946 * "mpa" and "aff" are assumed to have been aligned.
6948 * We first extract the parametric constant from "aff", defined
6949 * over the correct domain.
6950 * Then we add the appropriate combinations of the members of "mpa".
6951 * Finally, we add the integer divisions through recursive calls.
6953 static __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_aff_aligned(
6954 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_aff
*aff
)
6962 n_in
= isl_aff_dim(aff
, isl_dim_in
);
6963 n_div
= isl_aff_dim(aff
, isl_dim_div
);
6965 space
= isl_space_domain(isl_multi_pw_aff_get_space(mpa
));
6966 tmp
= isl_aff_copy(aff
);
6967 tmp
= isl_aff_drop_dims(tmp
, isl_dim_div
, 0, n_div
);
6968 tmp
= isl_aff_drop_dims(tmp
, isl_dim_in
, 0, n_in
);
6969 tmp
= isl_aff_add_dims(tmp
, isl_dim_in
,
6970 isl_space_dim(space
, isl_dim_set
));
6971 tmp
= isl_aff_reset_domain_space(tmp
, space
);
6972 pa
= isl_pw_aff_from_aff(tmp
);
6974 for (i
= 0; i
< n_in
; ++i
) {
6977 if (!isl_aff_involves_dims(aff
, isl_dim_in
, i
, 1))
6979 v
= isl_aff_get_coefficient_val(aff
, isl_dim_in
, i
);
6980 pa_i
= isl_multi_pw_aff_get_pw_aff(mpa
, i
);
6981 pa_i
= isl_pw_aff_scale_val(pa_i
, v
);
6982 pa
= isl_pw_aff_add(pa
, pa_i
);
6985 for (i
= 0; i
< n_div
; ++i
) {
6989 if (!isl_aff_involves_dims(aff
, isl_dim_div
, i
, 1))
6991 div
= isl_aff_get_div(aff
, i
);
6992 pa_i
= isl_multi_pw_aff_apply_aff_aligned(
6993 isl_multi_pw_aff_copy(mpa
), div
);
6994 pa_i
= isl_pw_aff_floor(pa_i
);
6995 v
= isl_aff_get_coefficient_val(aff
, isl_dim_div
, i
);
6996 pa_i
= isl_pw_aff_scale_val(pa_i
, v
);
6997 pa
= isl_pw_aff_add(pa
, pa_i
);
7000 isl_multi_pw_aff_free(mpa
);
7006 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
7007 * with the domain of "aff". The domain of the result is the same
7010 __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_aff(
7011 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_aff
*aff
)
7013 isl_bool equal_params
;
7017 equal_params
= isl_space_has_equal_params(aff
->ls
->dim
, mpa
->space
);
7018 if (equal_params
< 0)
7021 return isl_multi_pw_aff_apply_aff_aligned(mpa
, aff
);
7023 aff
= isl_aff_align_params(aff
, isl_multi_pw_aff_get_space(mpa
));
7024 mpa
= isl_multi_pw_aff_align_params(mpa
, isl_aff_get_space(aff
));
7026 return isl_multi_pw_aff_apply_aff_aligned(mpa
, aff
);
7029 isl_multi_pw_aff_free(mpa
);
7033 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
7034 * with the domain of "pa". The domain of the result is the same
7036 * "mpa" and "pa" are assumed to have been aligned.
7038 * We consider each piece in turn. Note that the domains of the
7039 * pieces are assumed to be disjoint and they remain disjoint
7040 * after taking the preimage (over the same function).
7042 static __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_pw_aff_aligned(
7043 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_pw_aff
*pa
)
7052 space
= isl_space_join(isl_multi_pw_aff_get_space(mpa
),
7053 isl_pw_aff_get_space(pa
));
7054 res
= isl_pw_aff_empty(space
);
7056 for (i
= 0; i
< pa
->n
; ++i
) {
7060 pa_i
= isl_multi_pw_aff_apply_aff_aligned(
7061 isl_multi_pw_aff_copy(mpa
),
7062 isl_aff_copy(pa
->p
[i
].aff
));
7063 domain
= isl_set_copy(pa
->p
[i
].set
);
7064 domain
= isl_set_preimage_multi_pw_aff(domain
,
7065 isl_multi_pw_aff_copy(mpa
));
7066 pa_i
= isl_pw_aff_intersect_domain(pa_i
, domain
);
7067 res
= isl_pw_aff_add_disjoint(res
, pa_i
);
7070 isl_pw_aff_free(pa
);
7071 isl_multi_pw_aff_free(mpa
);
7074 isl_pw_aff_free(pa
);
7075 isl_multi_pw_aff_free(mpa
);
7079 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
7080 * with the domain of "pa". The domain of the result is the same
7083 __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_pw_aff(
7084 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_pw_aff
*pa
)
7086 isl_bool equal_params
;
7090 equal_params
= isl_space_has_equal_params(pa
->dim
, mpa
->space
);
7091 if (equal_params
< 0)
7094 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa
, pa
);
7096 pa
= isl_pw_aff_align_params(pa
, isl_multi_pw_aff_get_space(mpa
));
7097 mpa
= isl_multi_pw_aff_align_params(mpa
, isl_pw_aff_get_space(pa
));
7099 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa
, pa
);
7101 isl_pw_aff_free(pa
);
7102 isl_multi_pw_aff_free(mpa
);
7106 /* Compute the pullback of "pa" by the function represented by "mpa".
7107 * In other words, plug in "mpa" in "pa".
7108 * "pa" and "mpa" are assumed to have been aligned.
7110 * The pullback is computed by applying "pa" to "mpa".
7112 static __isl_give isl_pw_aff
*isl_pw_aff_pullback_multi_pw_aff_aligned(
7113 __isl_take isl_pw_aff
*pa
, __isl_take isl_multi_pw_aff
*mpa
)
7115 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa
, pa
);
7118 /* Compute the pullback of "pa" by the function represented by "mpa".
7119 * In other words, plug in "mpa" in "pa".
7121 * The pullback is computed by applying "pa" to "mpa".
7123 __isl_give isl_pw_aff
*isl_pw_aff_pullback_multi_pw_aff(
7124 __isl_take isl_pw_aff
*pa
, __isl_take isl_multi_pw_aff
*mpa
)
7126 return isl_multi_pw_aff_apply_pw_aff(mpa
, pa
);
7129 /* Compute the pullback of "mpa1" by the function represented by "mpa2".
7130 * In other words, plug in "mpa2" in "mpa1".
7132 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7134 * We pullback each member of "mpa1" in turn.
7136 * If "mpa1" has an explicit domain, then it is this domain
7137 * that needs to undergo a pullback instead, i.e., a preimage.
7139 static __isl_give isl_multi_pw_aff
*
7140 isl_multi_pw_aff_pullback_multi_pw_aff_aligned(
7141 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
)
7144 isl_space
*space
= NULL
;
7146 mpa1
= isl_multi_pw_aff_cow(mpa1
);
7150 space
= isl_space_join(isl_multi_pw_aff_get_space(mpa2
),
7151 isl_multi_pw_aff_get_space(mpa1
));
7153 for (i
= 0; i
< mpa1
->n
; ++i
) {
7154 mpa1
->u
.p
[i
] = isl_pw_aff_pullback_multi_pw_aff_aligned(
7155 mpa1
->u
.p
[i
], isl_multi_pw_aff_copy(mpa2
));
7160 if (isl_multi_pw_aff_has_explicit_domain(mpa1
)) {
7161 mpa1
->u
.dom
= isl_set_preimage_multi_pw_aff(mpa1
->u
.dom
,
7162 isl_multi_pw_aff_copy(mpa2
));
7166 mpa1
= isl_multi_pw_aff_reset_space(mpa1
, space
);
7168 isl_multi_pw_aff_free(mpa2
);
7171 isl_space_free(space
);
7172 isl_multi_pw_aff_free(mpa1
);
7173 isl_multi_pw_aff_free(mpa2
);
7177 /* Compute the pullback of "mpa1" by the function represented by "mpa2".
7178 * In other words, plug in "mpa2" in "mpa1".
7180 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_pullback_multi_pw_aff(
7181 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
)
7183 return isl_multi_pw_aff_align_params_multi_multi_and(mpa1
, mpa2
,
7184 &isl_multi_pw_aff_pullback_multi_pw_aff_aligned
);
7187 /* Align the parameters of "mpa1" and "mpa2", check that the ranges
7188 * of "mpa1" and "mpa2" live in the same space, construct map space
7189 * between the domain spaces of "mpa1" and "mpa2" and call "order"
7190 * with this map space as extract argument.
7192 static __isl_give isl_map
*isl_multi_pw_aff_order_map(
7193 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
,
7194 __isl_give isl_map
*(*order
)(__isl_keep isl_multi_pw_aff
*mpa1
,
7195 __isl_keep isl_multi_pw_aff
*mpa2
, __isl_take isl_space
*space
))
7198 isl_space
*space1
, *space2
;
7201 mpa1
= isl_multi_pw_aff_align_params(mpa1
,
7202 isl_multi_pw_aff_get_space(mpa2
));
7203 mpa2
= isl_multi_pw_aff_align_params(mpa2
,
7204 isl_multi_pw_aff_get_space(mpa1
));
7207 match
= isl_space_tuple_is_equal(mpa1
->space
, isl_dim_out
,
7208 mpa2
->space
, isl_dim_out
);
7212 isl_die(isl_multi_pw_aff_get_ctx(mpa1
), isl_error_invalid
,
7213 "range spaces don't match", goto error
);
7214 space1
= isl_space_domain(isl_multi_pw_aff_get_space(mpa1
));
7215 space2
= isl_space_domain(isl_multi_pw_aff_get_space(mpa2
));
7216 space1
= isl_space_map_from_domain_and_range(space1
, space2
);
7218 res
= order(mpa1
, mpa2
, space1
);
7219 isl_multi_pw_aff_free(mpa1
);
7220 isl_multi_pw_aff_free(mpa2
);
7223 isl_multi_pw_aff_free(mpa1
);
7224 isl_multi_pw_aff_free(mpa2
);
7228 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7229 * where the function values are equal. "space" is the space of the result.
7230 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7232 * "mpa1" and "mpa2" are equal when each of the pairs of elements
7233 * in the sequences are equal.
7235 static __isl_give isl_map
*isl_multi_pw_aff_eq_map_on_space(
7236 __isl_keep isl_multi_pw_aff
*mpa1
, __isl_keep isl_multi_pw_aff
*mpa2
,
7237 __isl_take isl_space
*space
)
7242 res
= isl_map_universe(space
);
7244 n
= isl_multi_pw_aff_dim(mpa1
, isl_dim_out
);
7245 for (i
= 0; i
< n
; ++i
) {
7246 isl_pw_aff
*pa1
, *pa2
;
7249 pa1
= isl_multi_pw_aff_get_pw_aff(mpa1
, i
);
7250 pa2
= isl_multi_pw_aff_get_pw_aff(mpa2
, i
);
7251 map
= isl_pw_aff_eq_map(pa1
, pa2
);
7252 res
= isl_map_intersect(res
, map
);
7258 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7259 * where the function values are equal.
7261 __isl_give isl_map
*isl_multi_pw_aff_eq_map(__isl_take isl_multi_pw_aff
*mpa1
,
7262 __isl_take isl_multi_pw_aff
*mpa2
)
7264 return isl_multi_pw_aff_order_map(mpa1
, mpa2
,
7265 &isl_multi_pw_aff_eq_map_on_space
);
7268 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7269 * where the function values of "mpa1" is lexicographically satisfies "base"
7270 * compared to that of "mpa2". "space" is the space of the result.
7271 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7273 * "mpa1" lexicographically satisfies "base" compared to "mpa2"
7274 * if its i-th element satisfies "base" when compared to
7275 * the i-th element of "mpa2" while all previous elements are
7278 static __isl_give isl_map
*isl_multi_pw_aff_lex_map_on_space(
7279 __isl_keep isl_multi_pw_aff
*mpa1
, __isl_keep isl_multi_pw_aff
*mpa2
,
7280 __isl_give isl_map
*(*base
)(__isl_take isl_pw_aff
*pa1
,
7281 __isl_take isl_pw_aff
*pa2
),
7282 __isl_take isl_space
*space
)
7285 isl_map
*res
, *rest
;
7287 res
= isl_map_empty(isl_space_copy(space
));
7288 rest
= isl_map_universe(space
);
7290 n
= isl_multi_pw_aff_dim(mpa1
, isl_dim_out
);
7291 for (i
= 0; i
< n
; ++i
) {
7292 isl_pw_aff
*pa1
, *pa2
;
7295 pa1
= isl_multi_pw_aff_get_pw_aff(mpa1
, i
);
7296 pa2
= isl_multi_pw_aff_get_pw_aff(mpa2
, i
);
7297 map
= base(pa1
, pa2
);
7298 map
= isl_map_intersect(map
, isl_map_copy(rest
));
7299 res
= isl_map_union(res
, map
);
7304 pa1
= isl_multi_pw_aff_get_pw_aff(mpa1
, i
);
7305 pa2
= isl_multi_pw_aff_get_pw_aff(mpa2
, i
);
7306 map
= isl_pw_aff_eq_map(pa1
, pa2
);
7307 rest
= isl_map_intersect(rest
, map
);
7314 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7315 * where the function value of "mpa1" is lexicographically less than that
7316 * of "mpa2". "space" is the space of the result.
7317 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7319 * "mpa1" is less than "mpa2" if its i-th element is smaller
7320 * than the i-th element of "mpa2" while all previous elements are
7323 __isl_give isl_map
*isl_multi_pw_aff_lex_lt_map_on_space(
7324 __isl_keep isl_multi_pw_aff
*mpa1
, __isl_keep isl_multi_pw_aff
*mpa2
,
7325 __isl_take isl_space
*space
)
7327 return isl_multi_pw_aff_lex_map_on_space(mpa1
, mpa2
,
7328 &isl_pw_aff_lt_map
, space
);
7331 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7332 * where the function value of "mpa1" is lexicographically less than that
7335 __isl_give isl_map
*isl_multi_pw_aff_lex_lt_map(
7336 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
)
7338 return isl_multi_pw_aff_order_map(mpa1
, mpa2
,
7339 &isl_multi_pw_aff_lex_lt_map_on_space
);
7342 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7343 * where the function value of "mpa1" is lexicographically greater than that
7344 * of "mpa2". "space" is the space of the result.
7345 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7347 * "mpa1" is greater than "mpa2" if its i-th element is greater
7348 * than the i-th element of "mpa2" while all previous elements are
7351 __isl_give isl_map
*isl_multi_pw_aff_lex_gt_map_on_space(
7352 __isl_keep isl_multi_pw_aff
*mpa1
, __isl_keep isl_multi_pw_aff
*mpa2
,
7353 __isl_take isl_space
*space
)
7355 return isl_multi_pw_aff_lex_map_on_space(mpa1
, mpa2
,
7356 &isl_pw_aff_gt_map
, space
);
7359 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7360 * where the function value of "mpa1" is lexicographically greater than that
7363 __isl_give isl_map
*isl_multi_pw_aff_lex_gt_map(
7364 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
)
7366 return isl_multi_pw_aff_order_map(mpa1
, mpa2
,
7367 &isl_multi_pw_aff_lex_gt_map_on_space
);
7370 /* Compare two isl_affs.
7372 * Return -1 if "aff1" is "smaller" than "aff2", 1 if "aff1" is "greater"
7373 * than "aff2" and 0 if they are equal.
7375 * The order is fairly arbitrary. We do consider expressions that only involve
7376 * earlier dimensions as "smaller".
7378 int isl_aff_plain_cmp(__isl_keep isl_aff
*aff1
, __isl_keep isl_aff
*aff2
)
7391 cmp
= isl_local_space_cmp(aff1
->ls
, aff2
->ls
);
7395 last1
= isl_seq_last_non_zero(aff1
->v
->el
+ 1, aff1
->v
->size
- 1);
7396 last2
= isl_seq_last_non_zero(aff2
->v
->el
+ 1, aff1
->v
->size
- 1);
7398 return last1
- last2
;
7400 return isl_seq_cmp(aff1
->v
->el
, aff2
->v
->el
, aff1
->v
->size
);
7403 /* Compare two isl_pw_affs.
7405 * Return -1 if "pa1" is "smaller" than "pa2", 1 if "pa1" is "greater"
7406 * than "pa2" and 0 if they are equal.
7408 * The order is fairly arbitrary. We do consider expressions that only involve
7409 * earlier dimensions as "smaller".
7411 int isl_pw_aff_plain_cmp(__isl_keep isl_pw_aff
*pa1
,
7412 __isl_keep isl_pw_aff
*pa2
)
7425 cmp
= isl_space_cmp(pa1
->dim
, pa2
->dim
);
7429 if (pa1
->n
!= pa2
->n
)
7430 return pa1
->n
- pa2
->n
;
7432 for (i
= 0; i
< pa1
->n
; ++i
) {
7433 cmp
= isl_set_plain_cmp(pa1
->p
[i
].set
, pa2
->p
[i
].set
);
7436 cmp
= isl_aff_plain_cmp(pa1
->p
[i
].aff
, pa2
->p
[i
].aff
);
7444 /* Return a piecewise affine expression that is equal to "v" on "domain".
7446 __isl_give isl_pw_aff
*isl_pw_aff_val_on_domain(__isl_take isl_set
*domain
,
7447 __isl_take isl_val
*v
)
7450 isl_local_space
*ls
;
7453 space
= isl_set_get_space(domain
);
7454 ls
= isl_local_space_from_space(space
);
7455 aff
= isl_aff_val_on_domain(ls
, v
);
7457 return isl_pw_aff_alloc(domain
, aff
);
7460 /* Return a multi affine expression that is equal to "mv" on domain
7463 __isl_give isl_multi_aff
*isl_multi_aff_multi_val_on_space(
7464 __isl_take isl_space
*space
, __isl_take isl_multi_val
*mv
)
7468 isl_local_space
*ls
;
7474 n
= isl_multi_val_dim(mv
, isl_dim_set
);
7475 space2
= isl_multi_val_get_space(mv
);
7476 space2
= isl_space_align_params(space2
, isl_space_copy(space
));
7477 space
= isl_space_align_params(space
, isl_space_copy(space2
));
7478 space
= isl_space_map_from_domain_and_range(space
, space2
);
7479 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
7480 ls
= isl_local_space_from_space(isl_space_domain(space
));
7481 for (i
= 0; i
< n
; ++i
) {
7485 v
= isl_multi_val_get_val(mv
, i
);
7486 aff
= isl_aff_val_on_domain(isl_local_space_copy(ls
), v
);
7487 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
7489 isl_local_space_free(ls
);
7491 isl_multi_val_free(mv
);
7494 isl_space_free(space
);
7495 isl_multi_val_free(mv
);
7499 /* Return a piecewise multi-affine expression
7500 * that is equal to "mv" on "domain".
7502 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_multi_val_on_domain(
7503 __isl_take isl_set
*domain
, __isl_take isl_multi_val
*mv
)
7508 space
= isl_set_get_space(domain
);
7509 ma
= isl_multi_aff_multi_val_on_space(space
, mv
);
7511 return isl_pw_multi_aff_alloc(domain
, ma
);
7514 /* Internal data structure for isl_union_pw_multi_aff_multi_val_on_domain.
7515 * mv is the value that should be attained on each domain set
7516 * res collects the results
7518 struct isl_union_pw_multi_aff_multi_val_on_domain_data
{
7520 isl_union_pw_multi_aff
*res
;
7523 /* Create an isl_pw_multi_aff equal to data->mv on "domain"
7524 * and add it to data->res.
7526 static isl_stat
pw_multi_aff_multi_val_on_domain(__isl_take isl_set
*domain
,
7529 struct isl_union_pw_multi_aff_multi_val_on_domain_data
*data
= user
;
7530 isl_pw_multi_aff
*pma
;
7533 mv
= isl_multi_val_copy(data
->mv
);
7534 pma
= isl_pw_multi_aff_multi_val_on_domain(domain
, mv
);
7535 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma
);
7537 return data
->res
? isl_stat_ok
: isl_stat_error
;
7540 /* Return a union piecewise multi-affine expression
7541 * that is equal to "mv" on "domain".
7543 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_multi_val_on_domain(
7544 __isl_take isl_union_set
*domain
, __isl_take isl_multi_val
*mv
)
7546 struct isl_union_pw_multi_aff_multi_val_on_domain_data data
;
7549 space
= isl_union_set_get_space(domain
);
7550 data
.res
= isl_union_pw_multi_aff_empty(space
);
7552 if (isl_union_set_foreach_set(domain
,
7553 &pw_multi_aff_multi_val_on_domain
, &data
) < 0)
7554 data
.res
= isl_union_pw_multi_aff_free(data
.res
);
7555 isl_union_set_free(domain
);
7556 isl_multi_val_free(mv
);
7560 /* Compute the pullback of data->pma by the function represented by "pma2",
7561 * provided the spaces match, and add the results to data->res.
7563 static isl_stat
pullback_entry(__isl_take isl_pw_multi_aff
*pma2
, void *user
)
7565 struct isl_union_pw_multi_aff_bin_data
*data
= user
;
7567 if (!isl_space_tuple_is_equal(data
->pma
->dim
, isl_dim_in
,
7568 pma2
->dim
, isl_dim_out
)) {
7569 isl_pw_multi_aff_free(pma2
);
7573 pma2
= isl_pw_multi_aff_pullback_pw_multi_aff(
7574 isl_pw_multi_aff_copy(data
->pma
), pma2
);
7576 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma2
);
7578 return isl_stat_error
;
7583 /* Compute the pullback of "upma1" by the function represented by "upma2".
7585 __isl_give isl_union_pw_multi_aff
*
7586 isl_union_pw_multi_aff_pullback_union_pw_multi_aff(
7587 __isl_take isl_union_pw_multi_aff
*upma1
,
7588 __isl_take isl_union_pw_multi_aff
*upma2
)
7590 return bin_op(upma1
, upma2
, &pullback_entry
);
7593 /* Check that the domain space of "upa" matches "space".
7595 * This function is called from isl_multi_union_pw_aff_set_union_pw_aff and
7596 * can in principle never fail since the space "space" is that
7597 * of the isl_multi_union_pw_aff and is a set space such that
7598 * there is no domain space to match.
7600 * We check the parameters and double-check that "space" is
7601 * indeed that of a set.
7603 static isl_stat
isl_union_pw_aff_check_match_domain_space(
7604 __isl_keep isl_union_pw_aff
*upa
, __isl_keep isl_space
*space
)
7606 isl_space
*upa_space
;
7610 return isl_stat_error
;
7612 match
= isl_space_is_set(space
);
7614 return isl_stat_error
;
7616 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
7617 "expecting set space", return isl_stat_error
);
7619 upa_space
= isl_union_pw_aff_get_space(upa
);
7620 match
= isl_space_has_equal_params(space
, upa_space
);
7624 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
7625 "parameters don't match", goto error
);
7627 isl_space_free(upa_space
);
7630 isl_space_free(upa_space
);
7631 return isl_stat_error
;
7634 /* Do the parameters of "upa" match those of "space"?
7636 static isl_bool
isl_union_pw_aff_matching_params(
7637 __isl_keep isl_union_pw_aff
*upa
, __isl_keep isl_space
*space
)
7639 isl_space
*upa_space
;
7643 return isl_bool_error
;
7645 upa_space
= isl_union_pw_aff_get_space(upa
);
7647 match
= isl_space_has_equal_params(space
, upa_space
);
7649 isl_space_free(upa_space
);
7653 /* Internal data structure for isl_union_pw_aff_reset_domain_space.
7654 * space represents the new parameters.
7655 * res collects the results.
7657 struct isl_union_pw_aff_reset_params_data
{
7659 isl_union_pw_aff
*res
;
7662 /* Replace the parameters of "pa" by data->space and
7663 * add the result to data->res.
7665 static isl_stat
reset_params(__isl_take isl_pw_aff
*pa
, void *user
)
7667 struct isl_union_pw_aff_reset_params_data
*data
= user
;
7670 space
= isl_pw_aff_get_space(pa
);
7671 space
= isl_space_replace_params(space
, data
->space
);
7672 pa
= isl_pw_aff_reset_space(pa
, space
);
7673 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
7675 return data
->res
? isl_stat_ok
: isl_stat_error
;
7678 /* Replace the domain space of "upa" by "space".
7679 * Since a union expression does not have a (single) domain space,
7680 * "space" is necessarily a parameter space.
7682 * Since the order and the names of the parameters determine
7683 * the hash value, we need to create a new hash table.
7685 static __isl_give isl_union_pw_aff
*isl_union_pw_aff_reset_domain_space(
7686 __isl_take isl_union_pw_aff
*upa
, __isl_take isl_space
*space
)
7688 struct isl_union_pw_aff_reset_params_data data
= { space
};
7691 match
= isl_union_pw_aff_matching_params(upa
, space
);
7693 upa
= isl_union_pw_aff_free(upa
);
7695 isl_space_free(space
);
7699 data
.res
= isl_union_pw_aff_empty(isl_space_copy(space
));
7700 if (isl_union_pw_aff_foreach_pw_aff(upa
, &reset_params
, &data
) < 0)
7701 data
.res
= isl_union_pw_aff_free(data
.res
);
7703 isl_union_pw_aff_free(upa
);
7704 isl_space_free(space
);
7708 /* Return the floor of "pa".
7710 static __isl_give isl_pw_aff
*floor_entry(__isl_take isl_pw_aff
*pa
, void *user
)
7712 return isl_pw_aff_floor(pa
);
7715 /* Given f, return floor(f).
7717 __isl_give isl_union_pw_aff
*isl_union_pw_aff_floor(
7718 __isl_take isl_union_pw_aff
*upa
)
7720 return isl_union_pw_aff_transform_inplace(upa
, &floor_entry
, NULL
);
7725 * upa mod m = upa - m * floor(upa/m)
7727 * with m an integer value.
7729 __isl_give isl_union_pw_aff
*isl_union_pw_aff_mod_val(
7730 __isl_take isl_union_pw_aff
*upa
, __isl_take isl_val
*m
)
7732 isl_union_pw_aff
*res
;
7737 if (!isl_val_is_int(m
))
7738 isl_die(isl_val_get_ctx(m
), isl_error_invalid
,
7739 "expecting integer modulo", goto error
);
7740 if (!isl_val_is_pos(m
))
7741 isl_die(isl_val_get_ctx(m
), isl_error_invalid
,
7742 "expecting positive modulo", goto error
);
7744 res
= isl_union_pw_aff_copy(upa
);
7745 upa
= isl_union_pw_aff_scale_down_val(upa
, isl_val_copy(m
));
7746 upa
= isl_union_pw_aff_floor(upa
);
7747 upa
= isl_union_pw_aff_scale_val(upa
, m
);
7748 res
= isl_union_pw_aff_sub(res
, upa
);
7753 isl_union_pw_aff_free(upa
);
7757 /* Internal data structure for isl_union_pw_multi_aff_get_union_pw_aff.
7758 * pos is the output position that needs to be extracted.
7759 * res collects the results.
7761 struct isl_union_pw_multi_aff_get_union_pw_aff_data
{
7763 isl_union_pw_aff
*res
;
7766 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma"
7767 * (assuming it has such a dimension) and add it to data->res.
7769 static isl_stat
get_union_pw_aff(__isl_take isl_pw_multi_aff
*pma
, void *user
)
7771 struct isl_union_pw_multi_aff_get_union_pw_aff_data
*data
= user
;
7776 return isl_stat_error
;
7778 n_out
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
7779 if (data
->pos
>= n_out
) {
7780 isl_pw_multi_aff_free(pma
);
7784 pa
= isl_pw_multi_aff_get_pw_aff(pma
, data
->pos
);
7785 isl_pw_multi_aff_free(pma
);
7787 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
7789 return data
->res
? isl_stat_ok
: isl_stat_error
;
7792 /* Extract an isl_union_pw_aff corresponding to
7793 * output dimension "pos" of "upma".
7795 __isl_give isl_union_pw_aff
*isl_union_pw_multi_aff_get_union_pw_aff(
7796 __isl_keep isl_union_pw_multi_aff
*upma
, int pos
)
7798 struct isl_union_pw_multi_aff_get_union_pw_aff_data data
;
7805 isl_die(isl_union_pw_multi_aff_get_ctx(upma
), isl_error_invalid
,
7806 "cannot extract at negative position", return NULL
);
7808 space
= isl_union_pw_multi_aff_get_space(upma
);
7809 data
.res
= isl_union_pw_aff_empty(space
);
7811 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma
,
7812 &get_union_pw_aff
, &data
) < 0)
7813 data
.res
= isl_union_pw_aff_free(data
.res
);
7818 /* Return a union piecewise affine expression
7819 * that is equal to "aff" on "domain".
7821 __isl_give isl_union_pw_aff
*isl_union_pw_aff_aff_on_domain(
7822 __isl_take isl_union_set
*domain
, __isl_take isl_aff
*aff
)
7826 pa
= isl_pw_aff_from_aff(aff
);
7827 return isl_union_pw_aff_pw_aff_on_domain(domain
, pa
);
7830 /* Return a union piecewise affine expression
7831 * that is equal to the parameter identified by "id" on "domain".
7833 * Make sure the parameter appears in the space passed to
7834 * isl_aff_param_on_domain_space_id.
7836 __isl_give isl_union_pw_aff
*isl_union_pw_aff_param_on_domain_id(
7837 __isl_take isl_union_set
*domain
, __isl_take isl_id
*id
)
7842 space
= isl_union_set_get_space(domain
);
7843 space
= isl_space_add_param_id(space
, isl_id_copy(id
));
7844 aff
= isl_aff_param_on_domain_space_id(space
, id
);
7845 return isl_union_pw_aff_aff_on_domain(domain
, aff
);
7848 /* Internal data structure for isl_union_pw_aff_pw_aff_on_domain.
7849 * "pa" is the piecewise symbolic value that the resulting isl_union_pw_aff
7851 * "res" collects the results.
7853 struct isl_union_pw_aff_pw_aff_on_domain_data
{
7855 isl_union_pw_aff
*res
;
7858 /* Construct a piecewise affine expression that is equal to data->pa
7859 * on "domain" and add the result to data->res.
7861 static isl_stat
pw_aff_on_domain(__isl_take isl_set
*domain
, void *user
)
7863 struct isl_union_pw_aff_pw_aff_on_domain_data
*data
= user
;
7867 pa
= isl_pw_aff_copy(data
->pa
);
7868 dim
= isl_set_dim(domain
, isl_dim_set
);
7869 pa
= isl_pw_aff_from_range(pa
);
7870 pa
= isl_pw_aff_add_dims(pa
, isl_dim_in
, dim
);
7871 pa
= isl_pw_aff_reset_domain_space(pa
, isl_set_get_space(domain
));
7872 pa
= isl_pw_aff_intersect_domain(pa
, domain
);
7873 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
7875 return data
->res
? isl_stat_ok
: isl_stat_error
;
7878 /* Return a union piecewise affine expression
7879 * that is equal to "pa" on "domain", assuming "domain" and "pa"
7880 * have been aligned.
7882 * Construct an isl_pw_aff on each of the sets in "domain" and
7883 * collect the results.
7885 static __isl_give isl_union_pw_aff
*isl_union_pw_aff_pw_aff_on_domain_aligned(
7886 __isl_take isl_union_set
*domain
, __isl_take isl_pw_aff
*pa
)
7888 struct isl_union_pw_aff_pw_aff_on_domain_data data
;
7891 space
= isl_union_set_get_space(domain
);
7892 data
.res
= isl_union_pw_aff_empty(space
);
7894 if (isl_union_set_foreach_set(domain
, &pw_aff_on_domain
, &data
) < 0)
7895 data
.res
= isl_union_pw_aff_free(data
.res
);
7896 isl_union_set_free(domain
);
7897 isl_pw_aff_free(pa
);
7901 /* Return a union piecewise affine expression
7902 * that is equal to "pa" on "domain".
7904 * Check that "pa" is a parametric expression,
7905 * align the parameters if needed and call
7906 * isl_union_pw_aff_pw_aff_on_domain_aligned.
7908 __isl_give isl_union_pw_aff
*isl_union_pw_aff_pw_aff_on_domain(
7909 __isl_take isl_union_set
*domain
, __isl_take isl_pw_aff
*pa
)
7912 isl_bool equal_params
;
7913 isl_space
*domain_space
, *pa_space
;
7915 pa_space
= isl_pw_aff_peek_space(pa
);
7916 is_set
= isl_space_is_set(pa_space
);
7920 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
7921 "expecting parametric expression", goto error
);
7923 domain_space
= isl_union_set_get_space(domain
);
7924 pa_space
= isl_pw_aff_get_space(pa
);
7925 equal_params
= isl_space_has_equal_params(domain_space
, pa_space
);
7926 if (equal_params
>= 0 && !equal_params
) {
7929 space
= isl_space_align_params(domain_space
, pa_space
);
7930 pa
= isl_pw_aff_align_params(pa
, isl_space_copy(space
));
7931 domain
= isl_union_set_align_params(domain
, space
);
7933 isl_space_free(domain_space
);
7934 isl_space_free(pa_space
);
7937 if (equal_params
< 0)
7939 return isl_union_pw_aff_pw_aff_on_domain_aligned(domain
, pa
);
7941 isl_union_set_free(domain
);
7942 isl_pw_aff_free(pa
);
7946 /* Internal data structure for isl_union_pw_aff_val_on_domain.
7947 * "v" is the value that the resulting isl_union_pw_aff needs to attain.
7948 * "res" collects the results.
7950 struct isl_union_pw_aff_val_on_domain_data
{
7952 isl_union_pw_aff
*res
;
7955 /* Construct a piecewise affine expression that is equal to data->v
7956 * on "domain" and add the result to data->res.
7958 static isl_stat
pw_aff_val_on_domain(__isl_take isl_set
*domain
, void *user
)
7960 struct isl_union_pw_aff_val_on_domain_data
*data
= user
;
7964 v
= isl_val_copy(data
->v
);
7965 pa
= isl_pw_aff_val_on_domain(domain
, v
);
7966 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
7968 return data
->res
? isl_stat_ok
: isl_stat_error
;
7971 /* Return a union piecewise affine expression
7972 * that is equal to "v" on "domain".
7974 * Construct an isl_pw_aff on each of the sets in "domain" and
7975 * collect the results.
7977 __isl_give isl_union_pw_aff
*isl_union_pw_aff_val_on_domain(
7978 __isl_take isl_union_set
*domain
, __isl_take isl_val
*v
)
7980 struct isl_union_pw_aff_val_on_domain_data data
;
7983 space
= isl_union_set_get_space(domain
);
7984 data
.res
= isl_union_pw_aff_empty(space
);
7986 if (isl_union_set_foreach_set(domain
, &pw_aff_val_on_domain
, &data
) < 0)
7987 data
.res
= isl_union_pw_aff_free(data
.res
);
7988 isl_union_set_free(domain
);
7993 /* Construct a piecewise multi affine expression
7994 * that is equal to "pa" and add it to upma.
7996 static isl_stat
pw_multi_aff_from_pw_aff_entry(__isl_take isl_pw_aff
*pa
,
7999 isl_union_pw_multi_aff
**upma
= user
;
8000 isl_pw_multi_aff
*pma
;
8002 pma
= isl_pw_multi_aff_from_pw_aff(pa
);
8003 *upma
= isl_union_pw_multi_aff_add_pw_multi_aff(*upma
, pma
);
8005 return *upma
? isl_stat_ok
: isl_stat_error
;
8008 /* Construct and return a union piecewise multi affine expression
8009 * that is equal to the given union piecewise affine expression.
8011 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_union_pw_aff(
8012 __isl_take isl_union_pw_aff
*upa
)
8015 isl_union_pw_multi_aff
*upma
;
8020 space
= isl_union_pw_aff_get_space(upa
);
8021 upma
= isl_union_pw_multi_aff_empty(space
);
8023 if (isl_union_pw_aff_foreach_pw_aff(upa
,
8024 &pw_multi_aff_from_pw_aff_entry
, &upma
) < 0)
8025 upma
= isl_union_pw_multi_aff_free(upma
);
8027 isl_union_pw_aff_free(upa
);
8031 /* Compute the set of elements in the domain of "pa" where it is zero and
8032 * add this set to "uset".
8034 static isl_stat
zero_union_set(__isl_take isl_pw_aff
*pa
, void *user
)
8036 isl_union_set
**uset
= (isl_union_set
**)user
;
8038 *uset
= isl_union_set_add_set(*uset
, isl_pw_aff_zero_set(pa
));
8040 return *uset
? isl_stat_ok
: isl_stat_error
;
8043 /* Return a union set containing those elements in the domain
8044 * of "upa" where it is zero.
8046 __isl_give isl_union_set
*isl_union_pw_aff_zero_union_set(
8047 __isl_take isl_union_pw_aff
*upa
)
8049 isl_union_set
*zero
;
8051 zero
= isl_union_set_empty(isl_union_pw_aff_get_space(upa
));
8052 if (isl_union_pw_aff_foreach_pw_aff(upa
, &zero_union_set
, &zero
) < 0)
8053 zero
= isl_union_set_free(zero
);
8055 isl_union_pw_aff_free(upa
);
8059 /* Convert "pa" to an isl_map and add it to *umap.
8061 static isl_stat
map_from_pw_aff_entry(__isl_take isl_pw_aff
*pa
, void *user
)
8063 isl_union_map
**umap
= user
;
8066 map
= isl_map_from_pw_aff(pa
);
8067 *umap
= isl_union_map_add_map(*umap
, map
);
8069 return *umap
? isl_stat_ok
: isl_stat_error
;
8072 /* Construct a union map mapping the domain of the union
8073 * piecewise affine expression to its range, with the single output dimension
8074 * equated to the corresponding affine expressions on their cells.
8076 __isl_give isl_union_map
*isl_union_map_from_union_pw_aff(
8077 __isl_take isl_union_pw_aff
*upa
)
8080 isl_union_map
*umap
;
8085 space
= isl_union_pw_aff_get_space(upa
);
8086 umap
= isl_union_map_empty(space
);
8088 if (isl_union_pw_aff_foreach_pw_aff(upa
, &map_from_pw_aff_entry
,
8090 umap
= isl_union_map_free(umap
);
8092 isl_union_pw_aff_free(upa
);
8096 /* Internal data structure for isl_union_pw_aff_pullback_union_pw_multi_aff.
8097 * upma is the function that is plugged in.
8098 * pa is the current part of the function in which upma is plugged in.
8099 * res collects the results.
8101 struct isl_union_pw_aff_pullback_upma_data
{
8102 isl_union_pw_multi_aff
*upma
;
8104 isl_union_pw_aff
*res
;
8107 /* Check if "pma" can be plugged into data->pa.
8108 * If so, perform the pullback and add the result to data->res.
8110 static isl_stat
pa_pb_pma(__isl_take isl_pw_multi_aff
*pma
, void *user
)
8112 struct isl_union_pw_aff_pullback_upma_data
*data
= user
;
8115 if (!isl_space_tuple_is_equal(data
->pa
->dim
, isl_dim_in
,
8116 pma
->dim
, isl_dim_out
)) {
8117 isl_pw_multi_aff_free(pma
);
8121 pa
= isl_pw_aff_copy(data
->pa
);
8122 pa
= isl_pw_aff_pullback_pw_multi_aff(pa
, pma
);
8124 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
8126 return data
->res
? isl_stat_ok
: isl_stat_error
;
8129 /* Check if any of the elements of data->upma can be plugged into pa,
8130 * add if so add the result to data->res.
8132 static isl_stat
upa_pb_upma(__isl_take isl_pw_aff
*pa
, void *user
)
8134 struct isl_union_pw_aff_pullback_upma_data
*data
= user
;
8138 r
= isl_union_pw_multi_aff_foreach_pw_multi_aff(data
->upma
,
8140 isl_pw_aff_free(pa
);
8145 /* Compute the pullback of "upa" by the function represented by "upma".
8146 * In other words, plug in "upma" in "upa". The result contains
8147 * expressions defined over the domain space of "upma".
8149 * Run over all pairs of elements in "upa" and "upma", perform
8150 * the pullback when appropriate and collect the results.
8151 * If the hash value were based on the domain space rather than
8152 * the function space, then we could run through all elements
8153 * of "upma" and directly pick out the corresponding element of "upa".
8155 __isl_give isl_union_pw_aff
*isl_union_pw_aff_pullback_union_pw_multi_aff(
8156 __isl_take isl_union_pw_aff
*upa
,
8157 __isl_take isl_union_pw_multi_aff
*upma
)
8159 struct isl_union_pw_aff_pullback_upma_data data
= { NULL
, NULL
};
8162 space
= isl_union_pw_multi_aff_get_space(upma
);
8163 upa
= isl_union_pw_aff_align_params(upa
, space
);
8164 space
= isl_union_pw_aff_get_space(upa
);
8165 upma
= isl_union_pw_multi_aff_align_params(upma
, space
);
8171 data
.res
= isl_union_pw_aff_alloc_same_size(upa
);
8172 if (isl_union_pw_aff_foreach_pw_aff(upa
, &upa_pb_upma
, &data
) < 0)
8173 data
.res
= isl_union_pw_aff_free(data
.res
);
8175 isl_union_pw_aff_free(upa
);
8176 isl_union_pw_multi_aff_free(upma
);
8179 isl_union_pw_aff_free(upa
);
8180 isl_union_pw_multi_aff_free(upma
);
8185 #define BASE union_pw_aff
8187 #define DOMBASE union_set
8189 #define NO_MOVE_DIMS
8196 #include <isl_multi_explicit_domain.c>
8197 #include <isl_multi_union_pw_aff_explicit_domain.c>
8198 #include <isl_multi_templ.c>
8199 #include <isl_multi_apply_set.c>
8200 #include <isl_multi_apply_union_set.c>
8201 #include <isl_multi_coalesce.c>
8202 #include <isl_multi_floor.c>
8203 #include <isl_multi_gist.c>
8204 #include <isl_multi_align_set.c>
8205 #include <isl_multi_align_union_set.c>
8206 #include <isl_multi_intersect.c>
8208 /* Does "mupa" have a non-trivial explicit domain?
8210 * The explicit domain, if present, is trivial if it represents
8211 * an (obviously) universe parameter set.
8213 isl_bool
isl_multi_union_pw_aff_has_non_trivial_domain(
8214 __isl_keep isl_multi_union_pw_aff
*mupa
)
8216 isl_bool is_params
, trivial
;
8220 return isl_bool_error
;
8221 if (!isl_multi_union_pw_aff_has_explicit_domain(mupa
))
8222 return isl_bool_false
;
8223 is_params
= isl_union_set_is_params(mupa
->u
.dom
);
8224 if (is_params
< 0 || !is_params
)
8225 return isl_bool_not(is_params
);
8226 set
= isl_set_from_union_set(isl_union_set_copy(mupa
->u
.dom
));
8227 trivial
= isl_set_plain_is_universe(set
);
8229 return isl_bool_not(trivial
);
8232 /* Construct a multiple union piecewise affine expression
8233 * in the given space with value zero in each of the output dimensions.
8235 * Since there is no canonical zero value for
8236 * a union piecewise affine expression, we can only construct
8237 * a zero-dimensional "zero" value.
8239 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_zero(
8240 __isl_take isl_space
*space
)
8247 params
= isl_space_is_params(space
);
8251 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
8252 "expecting proper set space", goto error
);
8253 if (!isl_space_is_set(space
))
8254 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
8255 "expecting set space", goto error
);
8256 if (isl_space_dim(space
, isl_dim_out
) != 0)
8257 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
8258 "expecting 0D space", goto error
);
8260 return isl_multi_union_pw_aff_alloc(space
);
8262 isl_space_free(space
);
8266 /* Compute the sum of "mupa1" and "mupa2" on the union of their domains,
8267 * with the actual sum on the shared domain and
8268 * the defined expression on the symmetric difference of the domains.
8270 * We simply iterate over the elements in both arguments and
8271 * call isl_union_pw_aff_union_add on each of them, if there is
8272 * at least one element.
8274 * Otherwise, the two expressions have an explicit domain and
8275 * the union of these explicit domains is computed.
8276 * This assumes that the explicit domains are either both in terms
8277 * of specific domains elements or both in terms of parameters.
8278 * However, if one of the expressions does not have any constraints
8279 * on its explicit domain, then this is allowed as well and the result
8280 * is the expression with no constraints on its explicit domain.
8282 static __isl_give isl_multi_union_pw_aff
*
8283 isl_multi_union_pw_aff_union_add_aligned(
8284 __isl_take isl_multi_union_pw_aff
*mupa1
,
8285 __isl_take isl_multi_union_pw_aff
*mupa2
)
8287 isl_bool has_domain
, is_params1
, is_params2
;
8289 if (isl_multi_union_pw_aff_check_equal_space(mupa1
, mupa2
) < 0)
8292 return isl_multi_union_pw_aff_bin_op(mupa1
, mupa2
,
8293 &isl_union_pw_aff_union_add
);
8294 if (isl_multi_union_pw_aff_check_has_explicit_domain(mupa1
) < 0 ||
8295 isl_multi_union_pw_aff_check_has_explicit_domain(mupa2
) < 0)
8298 has_domain
= isl_multi_union_pw_aff_has_non_trivial_domain(mupa1
);
8302 isl_multi_union_pw_aff_free(mupa2
);
8305 has_domain
= isl_multi_union_pw_aff_has_non_trivial_domain(mupa2
);
8309 isl_multi_union_pw_aff_free(mupa1
);
8313 is_params1
= isl_union_set_is_params(mupa1
->u
.dom
);
8314 is_params2
= isl_union_set_is_params(mupa2
->u
.dom
);
8315 if (is_params1
< 0 || is_params2
< 0)
8317 if (is_params1
!= is_params2
)
8318 isl_die(isl_multi_union_pw_aff_get_ctx(mupa1
),
8320 "cannot compute union of concrete domain and "
8321 "parameter constraints", goto error
);
8322 mupa1
= isl_multi_union_pw_aff_cow(mupa1
);
8325 mupa1
->u
.dom
= isl_union_set_union(mupa1
->u
.dom
,
8326 isl_union_set_copy(mupa2
->u
.dom
));
8329 isl_multi_union_pw_aff_free(mupa2
);
8332 isl_multi_union_pw_aff_free(mupa1
);
8333 isl_multi_union_pw_aff_free(mupa2
);
8337 /* Compute the sum of "mupa1" and "mupa2" on the union of their domains,
8338 * with the actual sum on the shared domain and
8339 * the defined expression on the symmetric difference of the domains.
8341 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_union_add(
8342 __isl_take isl_multi_union_pw_aff
*mupa1
,
8343 __isl_take isl_multi_union_pw_aff
*mupa2
)
8345 return isl_multi_union_pw_aff_align_params_multi_multi_and(mupa1
, mupa2
,
8346 &isl_multi_union_pw_aff_union_add_aligned
);
8349 /* Construct and return a multi union piecewise affine expression
8350 * that is equal to the given multi affine expression.
8352 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_from_multi_aff(
8353 __isl_take isl_multi_aff
*ma
)
8355 isl_multi_pw_aff
*mpa
;
8357 mpa
= isl_multi_pw_aff_from_multi_aff(ma
);
8358 return isl_multi_union_pw_aff_from_multi_pw_aff(mpa
);
8361 /* Construct and return a multi union piecewise affine expression
8362 * that is equal to the given multi piecewise affine expression.
8364 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_from_multi_pw_aff(
8365 __isl_take isl_multi_pw_aff
*mpa
)
8369 isl_multi_union_pw_aff
*mupa
;
8374 space
= isl_multi_pw_aff_get_space(mpa
);
8375 space
= isl_space_range(space
);
8376 mupa
= isl_multi_union_pw_aff_alloc(space
);
8378 n
= isl_multi_pw_aff_dim(mpa
, isl_dim_out
);
8379 for (i
= 0; i
< n
; ++i
) {
8381 isl_union_pw_aff
*upa
;
8383 pa
= isl_multi_pw_aff_get_pw_aff(mpa
, i
);
8384 upa
= isl_union_pw_aff_from_pw_aff(pa
);
8385 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
8388 isl_multi_pw_aff_free(mpa
);
8393 /* Extract the range space of "pma" and assign it to *space.
8394 * If *space has already been set (through a previous call to this function),
8395 * then check that the range space is the same.
8397 static isl_stat
extract_space(__isl_take isl_pw_multi_aff
*pma
, void *user
)
8399 isl_space
**space
= user
;
8400 isl_space
*pma_space
;
8403 pma_space
= isl_space_range(isl_pw_multi_aff_get_space(pma
));
8404 isl_pw_multi_aff_free(pma
);
8407 return isl_stat_error
;
8413 equal
= isl_space_is_equal(pma_space
, *space
);
8414 isl_space_free(pma_space
);
8417 return isl_stat_error
;
8419 isl_die(isl_space_get_ctx(*space
), isl_error_invalid
,
8420 "range spaces not the same", return isl_stat_error
);
8424 /* Construct and return a multi union piecewise affine expression
8425 * that is equal to the given union piecewise multi affine expression.
8427 * In order to be able to perform the conversion, the input
8428 * needs to be non-empty and may only involve a single range space.
8430 * If the resulting multi union piecewise affine expression has
8431 * an explicit domain, then assign it the domain of the input.
8432 * In other cases, the domain is stored in the individual elements.
8434 __isl_give isl_multi_union_pw_aff
*
8435 isl_multi_union_pw_aff_from_union_pw_multi_aff(
8436 __isl_take isl_union_pw_multi_aff
*upma
)
8438 isl_space
*space
= NULL
;
8439 isl_multi_union_pw_aff
*mupa
;
8444 if (isl_union_pw_multi_aff_n_pw_multi_aff(upma
) == 0)
8445 isl_die(isl_union_pw_multi_aff_get_ctx(upma
), isl_error_invalid
,
8446 "cannot extract range space from empty input",
8448 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma
, &extract_space
,
8455 n
= isl_space_dim(space
, isl_dim_set
);
8456 mupa
= isl_multi_union_pw_aff_alloc(space
);
8458 for (i
= 0; i
< n
; ++i
) {
8459 isl_union_pw_aff
*upa
;
8461 upa
= isl_union_pw_multi_aff_get_union_pw_aff(upma
, i
);
8462 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
8464 if (isl_multi_union_pw_aff_has_explicit_domain(mupa
)) {
8466 isl_union_pw_multi_aff
*copy
;
8468 copy
= isl_union_pw_multi_aff_copy(upma
);
8469 dom
= isl_union_pw_multi_aff_domain(copy
);
8470 mupa
= isl_multi_union_pw_aff_intersect_domain(mupa
, dom
);
8473 isl_union_pw_multi_aff_free(upma
);
8476 isl_space_free(space
);
8477 isl_union_pw_multi_aff_free(upma
);
8481 /* Try and create an isl_multi_union_pw_aff that is equivalent
8482 * to the given isl_union_map.
8483 * The isl_union_map is required to be single-valued in each space.
8484 * Moreover, it cannot be empty and all range spaces need to be the same.
8485 * Otherwise, an error is produced.
8487 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_from_union_map(
8488 __isl_take isl_union_map
*umap
)
8490 isl_union_pw_multi_aff
*upma
;
8492 upma
= isl_union_pw_multi_aff_from_union_map(umap
);
8493 return isl_multi_union_pw_aff_from_union_pw_multi_aff(upma
);
8496 /* Return a multiple union piecewise affine expression
8497 * that is equal to "mv" on "domain", assuming "domain" and "mv"
8498 * have been aligned.
8500 * If the resulting multi union piecewise affine expression has
8501 * an explicit domain, then assign it the input domain.
8502 * In other cases, the domain is stored in the individual elements.
8504 static __isl_give isl_multi_union_pw_aff
*
8505 isl_multi_union_pw_aff_multi_val_on_domain_aligned(
8506 __isl_take isl_union_set
*domain
, __isl_take isl_multi_val
*mv
)
8510 isl_multi_union_pw_aff
*mupa
;
8515 n
= isl_multi_val_dim(mv
, isl_dim_set
);
8516 space
= isl_multi_val_get_space(mv
);
8517 mupa
= isl_multi_union_pw_aff_alloc(space
);
8518 for (i
= 0; i
< n
; ++i
) {
8520 isl_union_pw_aff
*upa
;
8522 v
= isl_multi_val_get_val(mv
, i
);
8523 upa
= isl_union_pw_aff_val_on_domain(isl_union_set_copy(domain
),
8525 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
8527 if (isl_multi_union_pw_aff_has_explicit_domain(mupa
))
8528 mupa
= isl_multi_union_pw_aff_intersect_domain(mupa
,
8529 isl_union_set_copy(domain
));
8531 isl_union_set_free(domain
);
8532 isl_multi_val_free(mv
);
8535 isl_union_set_free(domain
);
8536 isl_multi_val_free(mv
);
8540 /* Return a multiple union piecewise affine expression
8541 * that is equal to "mv" on "domain".
8543 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_multi_val_on_domain(
8544 __isl_take isl_union_set
*domain
, __isl_take isl_multi_val
*mv
)
8546 isl_bool equal_params
;
8550 equal_params
= isl_space_has_equal_params(domain
->dim
, mv
->space
);
8551 if (equal_params
< 0)
8554 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(
8556 domain
= isl_union_set_align_params(domain
,
8557 isl_multi_val_get_space(mv
));
8558 mv
= isl_multi_val_align_params(mv
, isl_union_set_get_space(domain
));
8559 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(domain
, mv
);
8561 isl_union_set_free(domain
);
8562 isl_multi_val_free(mv
);
8566 /* Return a multiple union piecewise affine expression
8567 * that is equal to "ma" on "domain".
8569 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_multi_aff_on_domain(
8570 __isl_take isl_union_set
*domain
, __isl_take isl_multi_aff
*ma
)
8572 isl_pw_multi_aff
*pma
;
8574 pma
= isl_pw_multi_aff_from_multi_aff(ma
);
8575 return isl_multi_union_pw_aff_pw_multi_aff_on_domain(domain
, pma
);
8578 /* Return a multiple union piecewise affine expression
8579 * that is equal to "pma" on "domain", assuming "domain" and "pma"
8580 * have been aligned.
8582 * If the resulting multi union piecewise affine expression has
8583 * an explicit domain, then assign it the input domain.
8584 * In other cases, the domain is stored in the individual elements.
8586 static __isl_give isl_multi_union_pw_aff
*
8587 isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(
8588 __isl_take isl_union_set
*domain
, __isl_take isl_pw_multi_aff
*pma
)
8592 isl_multi_union_pw_aff
*mupa
;
8594 if (!domain
|| !pma
)
8597 n
= isl_pw_multi_aff_dim(pma
, isl_dim_set
);
8598 space
= isl_pw_multi_aff_get_space(pma
);
8599 mupa
= isl_multi_union_pw_aff_alloc(space
);
8600 for (i
= 0; i
< n
; ++i
) {
8602 isl_union_pw_aff
*upa
;
8604 pa
= isl_pw_multi_aff_get_pw_aff(pma
, i
);
8605 upa
= isl_union_pw_aff_pw_aff_on_domain(
8606 isl_union_set_copy(domain
), pa
);
8607 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
8609 if (isl_multi_union_pw_aff_has_explicit_domain(mupa
))
8610 mupa
= isl_multi_union_pw_aff_intersect_domain(mupa
,
8611 isl_union_set_copy(domain
));
8613 isl_union_set_free(domain
);
8614 isl_pw_multi_aff_free(pma
);
8617 isl_union_set_free(domain
);
8618 isl_pw_multi_aff_free(pma
);
8622 /* Return a multiple union piecewise affine expression
8623 * that is equal to "pma" on "domain".
8625 __isl_give isl_multi_union_pw_aff
*
8626 isl_multi_union_pw_aff_pw_multi_aff_on_domain(__isl_take isl_union_set
*domain
,
8627 __isl_take isl_pw_multi_aff
*pma
)
8629 isl_bool equal_params
;
8632 space
= isl_pw_multi_aff_peek_space(pma
);
8633 equal_params
= isl_union_set_space_has_equal_params(domain
, space
);
8634 if (equal_params
< 0)
8637 return isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(
8639 domain
= isl_union_set_align_params(domain
,
8640 isl_pw_multi_aff_get_space(pma
));
8641 pma
= isl_pw_multi_aff_align_params(pma
,
8642 isl_union_set_get_space(domain
));
8643 return isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(domain
,
8646 isl_union_set_free(domain
);
8647 isl_pw_multi_aff_free(pma
);
8651 /* Return a union set containing those elements in the domains
8652 * of the elements of "mupa" where they are all zero.
8654 * If there are no elements, then simply return the entire domain.
8656 __isl_give isl_union_set
*isl_multi_union_pw_aff_zero_union_set(
8657 __isl_take isl_multi_union_pw_aff
*mupa
)
8660 isl_union_pw_aff
*upa
;
8661 isl_union_set
*zero
;
8666 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8668 return isl_multi_union_pw_aff_domain(mupa
);
8670 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, 0);
8671 zero
= isl_union_pw_aff_zero_union_set(upa
);
8673 for (i
= 1; i
< n
; ++i
) {
8674 isl_union_set
*zero_i
;
8676 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
8677 zero_i
= isl_union_pw_aff_zero_union_set(upa
);
8679 zero
= isl_union_set_intersect(zero
, zero_i
);
8682 isl_multi_union_pw_aff_free(mupa
);
8686 /* Construct a union map mapping the shared domain
8687 * of the union piecewise affine expressions to the range of "mupa"
8688 * in the special case of a 0D multi union piecewise affine expression.
8690 * Construct a map between the explicit domain of "mupa" and
8692 * Note that this assumes that the domain consists of explicit elements.
8694 static __isl_give isl_union_map
*isl_union_map_from_multi_union_pw_aff_0D(
8695 __isl_take isl_multi_union_pw_aff
*mupa
)
8699 isl_union_set
*dom
, *ran
;
8701 space
= isl_multi_union_pw_aff_get_space(mupa
);
8702 dom
= isl_multi_union_pw_aff_domain(mupa
);
8703 ran
= isl_union_set_from_set(isl_set_universe(space
));
8705 is_params
= isl_union_set_is_params(dom
);
8707 dom
= isl_union_set_free(dom
);
8709 isl_die(isl_union_set_get_ctx(dom
), isl_error_invalid
,
8710 "cannot create union map from expression without "
8711 "explicit domain elements",
8712 dom
= isl_union_set_free(dom
));
8714 return isl_union_map_from_domain_and_range(dom
, ran
);
8717 /* Construct a union map mapping the shared domain
8718 * of the union piecewise affine expressions to the range of "mupa"
8719 * with each dimension in the range equated to the
8720 * corresponding union piecewise affine expression.
8722 * If the input is zero-dimensional, then construct a mapping
8723 * from its explicit domain.
8725 __isl_give isl_union_map
*isl_union_map_from_multi_union_pw_aff(
8726 __isl_take isl_multi_union_pw_aff
*mupa
)
8730 isl_union_map
*umap
;
8731 isl_union_pw_aff
*upa
;
8736 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8738 return isl_union_map_from_multi_union_pw_aff_0D(mupa
);
8740 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, 0);
8741 umap
= isl_union_map_from_union_pw_aff(upa
);
8743 for (i
= 1; i
< n
; ++i
) {
8744 isl_union_map
*umap_i
;
8746 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
8747 umap_i
= isl_union_map_from_union_pw_aff(upa
);
8748 umap
= isl_union_map_flat_range_product(umap
, umap_i
);
8751 space
= isl_multi_union_pw_aff_get_space(mupa
);
8752 umap
= isl_union_map_reset_range_space(umap
, space
);
8754 isl_multi_union_pw_aff_free(mupa
);
8758 /* Internal data structure for isl_union_pw_multi_aff_reset_range_space.
8759 * "range" is the space from which to set the range space.
8760 * "res" collects the results.
8762 struct isl_union_pw_multi_aff_reset_range_space_data
{
8764 isl_union_pw_multi_aff
*res
;
8767 /* Replace the range space of "pma" by the range space of data->range and
8768 * add the result to data->res.
8770 static isl_stat
reset_range_space(__isl_take isl_pw_multi_aff
*pma
, void *user
)
8772 struct isl_union_pw_multi_aff_reset_range_space_data
*data
= user
;
8775 space
= isl_pw_multi_aff_get_space(pma
);
8776 space
= isl_space_domain(space
);
8777 space
= isl_space_extend_domain_with_range(space
,
8778 isl_space_copy(data
->range
));
8779 pma
= isl_pw_multi_aff_reset_space(pma
, space
);
8780 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma
);
8782 return data
->res
? isl_stat_ok
: isl_stat_error
;
8785 /* Replace the range space of all the piecewise affine expressions in "upma" by
8786 * the range space of "space".
8788 * This assumes that all these expressions have the same output dimension.
8790 * Since the spaces of the expressions change, so do their hash values.
8791 * We therefore need to create a new isl_union_pw_multi_aff.
8792 * Note that the hash value is currently computed based on the entire
8793 * space even though there can only be a single expression with a given
8796 static __isl_give isl_union_pw_multi_aff
*
8797 isl_union_pw_multi_aff_reset_range_space(
8798 __isl_take isl_union_pw_multi_aff
*upma
, __isl_take isl_space
*space
)
8800 struct isl_union_pw_multi_aff_reset_range_space_data data
= { space
};
8801 isl_space
*space_upma
;
8803 space_upma
= isl_union_pw_multi_aff_get_space(upma
);
8804 data
.res
= isl_union_pw_multi_aff_empty(space_upma
);
8805 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma
,
8806 &reset_range_space
, &data
) < 0)
8807 data
.res
= isl_union_pw_multi_aff_free(data
.res
);
8809 isl_space_free(space
);
8810 isl_union_pw_multi_aff_free(upma
);
8814 /* Construct and return a union piecewise multi affine expression
8815 * that is equal to the given multi union piecewise affine expression,
8816 * in the special case of a 0D multi union piecewise affine expression.
8818 * Construct a union piecewise multi affine expression
8819 * on top of the explicit domain of the input.
8821 __isl_give isl_union_pw_multi_aff
*
8822 isl_union_pw_multi_aff_from_multi_union_pw_aff_0D(
8823 __isl_take isl_multi_union_pw_aff
*mupa
)
8827 isl_union_set
*domain
;
8829 space
= isl_multi_union_pw_aff_get_space(mupa
);
8830 mv
= isl_multi_val_zero(space
);
8831 domain
= isl_multi_union_pw_aff_domain(mupa
);
8832 return isl_union_pw_multi_aff_multi_val_on_domain(domain
, mv
);
8835 /* Construct and return a union piecewise multi affine expression
8836 * that is equal to the given multi union piecewise affine expression.
8838 * If the input is zero-dimensional, then
8839 * construct a union piecewise multi affine expression
8840 * on top of the explicit domain of the input.
8842 __isl_give isl_union_pw_multi_aff
*
8843 isl_union_pw_multi_aff_from_multi_union_pw_aff(
8844 __isl_take isl_multi_union_pw_aff
*mupa
)
8848 isl_union_pw_multi_aff
*upma
;
8849 isl_union_pw_aff
*upa
;
8854 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8856 return isl_union_pw_multi_aff_from_multi_union_pw_aff_0D(mupa
);
8858 space
= isl_multi_union_pw_aff_get_space(mupa
);
8859 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, 0);
8860 upma
= isl_union_pw_multi_aff_from_union_pw_aff(upa
);
8862 for (i
= 1; i
< n
; ++i
) {
8863 isl_union_pw_multi_aff
*upma_i
;
8865 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
8866 upma_i
= isl_union_pw_multi_aff_from_union_pw_aff(upa
);
8867 upma
= isl_union_pw_multi_aff_flat_range_product(upma
, upma_i
);
8870 upma
= isl_union_pw_multi_aff_reset_range_space(upma
, space
);
8872 isl_multi_union_pw_aff_free(mupa
);
8876 /* Intersect the range of "mupa" with "range",
8877 * in the special case where "mupa" is 0D.
8879 * Intersect the domain of "mupa" with the constraints on the parameters
8882 static __isl_give isl_multi_union_pw_aff
*mupa_intersect_range_0D(
8883 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_set
*range
)
8885 range
= isl_set_params(range
);
8886 mupa
= isl_multi_union_pw_aff_intersect_params(mupa
, range
);
8890 /* Intersect the range of "mupa" with "range".
8891 * That is, keep only those domain elements that have a function value
8894 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_intersect_range(
8895 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_set
*range
)
8897 isl_union_pw_multi_aff
*upma
;
8898 isl_union_set
*domain
;
8903 if (!mupa
|| !range
)
8906 space
= isl_set_get_space(range
);
8907 match
= isl_space_tuple_is_equal(mupa
->space
, isl_dim_set
,
8908 space
, isl_dim_set
);
8909 isl_space_free(space
);
8913 isl_die(isl_multi_union_pw_aff_get_ctx(mupa
), isl_error_invalid
,
8914 "space don't match", goto error
);
8915 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8917 return mupa_intersect_range_0D(mupa
, range
);
8919 upma
= isl_union_pw_multi_aff_from_multi_union_pw_aff(
8920 isl_multi_union_pw_aff_copy(mupa
));
8921 domain
= isl_union_set_from_set(range
);
8922 domain
= isl_union_set_preimage_union_pw_multi_aff(domain
, upma
);
8923 mupa
= isl_multi_union_pw_aff_intersect_domain(mupa
, domain
);
8927 isl_multi_union_pw_aff_free(mupa
);
8928 isl_set_free(range
);
8932 /* Return the shared domain of the elements of "mupa",
8933 * in the special case where "mupa" is zero-dimensional.
8935 * Return the explicit domain of "mupa".
8936 * Note that this domain may be a parameter set, either
8937 * because "mupa" is meant to live in a set space or
8938 * because no explicit domain has been set.
8940 __isl_give isl_union_set
*isl_multi_union_pw_aff_domain_0D(
8941 __isl_take isl_multi_union_pw_aff
*mupa
)
8945 dom
= isl_multi_union_pw_aff_get_explicit_domain(mupa
);
8946 isl_multi_union_pw_aff_free(mupa
);
8951 /* Return the shared domain of the elements of "mupa".
8953 * If "mupa" is zero-dimensional, then return its explicit domain.
8955 __isl_give isl_union_set
*isl_multi_union_pw_aff_domain(
8956 __isl_take isl_multi_union_pw_aff
*mupa
)
8959 isl_union_pw_aff
*upa
;
8965 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8967 return isl_multi_union_pw_aff_domain_0D(mupa
);
8969 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, 0);
8970 dom
= isl_union_pw_aff_domain(upa
);
8971 for (i
= 1; i
< n
; ++i
) {
8972 isl_union_set
*dom_i
;
8974 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
8975 dom_i
= isl_union_pw_aff_domain(upa
);
8976 dom
= isl_union_set_intersect(dom
, dom_i
);
8979 isl_multi_union_pw_aff_free(mupa
);
8983 /* Apply "aff" to "mupa". The space of "mupa" is equal to the domain of "aff".
8984 * In particular, the spaces have been aligned.
8985 * The result is defined over the shared domain of the elements of "mupa"
8987 * We first extract the parametric constant part of "aff" and
8988 * define that over the shared domain.
8989 * Then we iterate over all input dimensions of "aff" and add the corresponding
8990 * multiples of the elements of "mupa".
8991 * Finally, we consider the integer divisions, calling the function
8992 * recursively to obtain an isl_union_pw_aff corresponding to the
8993 * integer division argument.
8995 static __isl_give isl_union_pw_aff
*multi_union_pw_aff_apply_aff(
8996 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_aff
*aff
)
8999 isl_union_pw_aff
*upa
;
9000 isl_union_set
*uset
;
9004 n_in
= isl_aff_dim(aff
, isl_dim_in
);
9005 n_div
= isl_aff_dim(aff
, isl_dim_div
);
9007 uset
= isl_multi_union_pw_aff_domain(isl_multi_union_pw_aff_copy(mupa
));
9008 cst
= isl_aff_copy(aff
);
9009 cst
= isl_aff_drop_dims(cst
, isl_dim_div
, 0, n_div
);
9010 cst
= isl_aff_drop_dims(cst
, isl_dim_in
, 0, n_in
);
9011 cst
= isl_aff_project_domain_on_params(cst
);
9012 upa
= isl_union_pw_aff_aff_on_domain(uset
, cst
);
9014 for (i
= 0; i
< n_in
; ++i
) {
9015 isl_union_pw_aff
*upa_i
;
9017 if (!isl_aff_involves_dims(aff
, isl_dim_in
, i
, 1))
9019 v
= isl_aff_get_coefficient_val(aff
, isl_dim_in
, i
);
9020 upa_i
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
9021 upa_i
= isl_union_pw_aff_scale_val(upa_i
, v
);
9022 upa
= isl_union_pw_aff_add(upa
, upa_i
);
9025 for (i
= 0; i
< n_div
; ++i
) {
9027 isl_union_pw_aff
*upa_i
;
9029 if (!isl_aff_involves_dims(aff
, isl_dim_div
, i
, 1))
9031 div
= isl_aff_get_div(aff
, i
);
9032 upa_i
= multi_union_pw_aff_apply_aff(
9033 isl_multi_union_pw_aff_copy(mupa
), div
);
9034 upa_i
= isl_union_pw_aff_floor(upa_i
);
9035 v
= isl_aff_get_coefficient_val(aff
, isl_dim_div
, i
);
9036 upa_i
= isl_union_pw_aff_scale_val(upa_i
, v
);
9037 upa
= isl_union_pw_aff_add(upa
, upa_i
);
9040 isl_multi_union_pw_aff_free(mupa
);
9046 /* Apply "aff" to "mupa". The space of "mupa" needs to be compatible
9047 * with the domain of "aff".
9048 * Furthermore, the dimension of this space needs to be greater than zero.
9049 * The result is defined over the shared domain of the elements of "mupa"
9051 * We perform these checks and then hand over control to
9052 * multi_union_pw_aff_apply_aff.
9054 __isl_give isl_union_pw_aff
*isl_multi_union_pw_aff_apply_aff(
9055 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_aff
*aff
)
9057 isl_space
*space1
, *space2
;
9060 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
9061 isl_aff_get_space(aff
));
9062 aff
= isl_aff_align_params(aff
, isl_multi_union_pw_aff_get_space(mupa
));
9066 space1
= isl_multi_union_pw_aff_get_space(mupa
);
9067 space2
= isl_aff_get_domain_space(aff
);
9068 equal
= isl_space_is_equal(space1
, space2
);
9069 isl_space_free(space1
);
9070 isl_space_free(space2
);
9074 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
9075 "spaces don't match", goto error
);
9076 if (isl_aff_dim(aff
, isl_dim_in
) == 0)
9077 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
9078 "cannot determine domains", goto error
);
9080 return multi_union_pw_aff_apply_aff(mupa
, aff
);
9082 isl_multi_union_pw_aff_free(mupa
);
9087 /* Apply "ma" to "mupa", in the special case where "mupa" is 0D.
9088 * The space of "mupa" is known to be compatible with the domain of "ma".
9090 * Construct an isl_multi_union_pw_aff that is equal to "ma"
9091 * on the domain of "mupa".
9093 static __isl_give isl_multi_union_pw_aff
*mupa_apply_multi_aff_0D(
9094 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_multi_aff
*ma
)
9098 dom
= isl_multi_union_pw_aff_domain(mupa
);
9099 ma
= isl_multi_aff_project_domain_on_params(ma
);
9101 return isl_multi_union_pw_aff_multi_aff_on_domain(dom
, ma
);
9104 /* Apply "ma" to "mupa". The space of "mupa" needs to be compatible
9105 * with the domain of "ma".
9106 * The result is defined over the shared domain of the elements of "mupa"
9108 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_apply_multi_aff(
9109 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_multi_aff
*ma
)
9111 isl_space
*space1
, *space2
;
9112 isl_multi_union_pw_aff
*res
;
9116 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
9117 isl_multi_aff_get_space(ma
));
9118 ma
= isl_multi_aff_align_params(ma
,
9119 isl_multi_union_pw_aff_get_space(mupa
));
9123 space1
= isl_multi_union_pw_aff_get_space(mupa
);
9124 space2
= isl_multi_aff_get_domain_space(ma
);
9125 equal
= isl_space_is_equal(space1
, space2
);
9126 isl_space_free(space1
);
9127 isl_space_free(space2
);
9131 isl_die(isl_multi_aff_get_ctx(ma
), isl_error_invalid
,
9132 "spaces don't match", goto error
);
9133 n_out
= isl_multi_aff_dim(ma
, isl_dim_out
);
9134 if (isl_multi_aff_dim(ma
, isl_dim_in
) == 0)
9135 return mupa_apply_multi_aff_0D(mupa
, ma
);
9137 space1
= isl_space_range(isl_multi_aff_get_space(ma
));
9138 res
= isl_multi_union_pw_aff_alloc(space1
);
9140 for (i
= 0; i
< n_out
; ++i
) {
9142 isl_union_pw_aff
*upa
;
9144 aff
= isl_multi_aff_get_aff(ma
, i
);
9145 upa
= multi_union_pw_aff_apply_aff(
9146 isl_multi_union_pw_aff_copy(mupa
), aff
);
9147 res
= isl_multi_union_pw_aff_set_union_pw_aff(res
, i
, upa
);
9150 isl_multi_aff_free(ma
);
9151 isl_multi_union_pw_aff_free(mupa
);
9154 isl_multi_union_pw_aff_free(mupa
);
9155 isl_multi_aff_free(ma
);
9159 /* Apply "pa" to "mupa", in the special case where "mupa" is 0D.
9160 * The space of "mupa" is known to be compatible with the domain of "pa".
9162 * Construct an isl_multi_union_pw_aff that is equal to "pa"
9163 * on the domain of "mupa".
9165 static __isl_give isl_union_pw_aff
*isl_multi_union_pw_aff_apply_pw_aff_0D(
9166 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_pw_aff
*pa
)
9170 dom
= isl_multi_union_pw_aff_domain(mupa
);
9171 pa
= isl_pw_aff_project_domain_on_params(pa
);
9173 return isl_union_pw_aff_pw_aff_on_domain(dom
, pa
);
9176 /* Apply "pa" to "mupa". The space of "mupa" needs to be compatible
9177 * with the domain of "pa".
9178 * Furthermore, the dimension of this space needs to be greater than zero.
9179 * The result is defined over the shared domain of the elements of "mupa"
9181 __isl_give isl_union_pw_aff
*isl_multi_union_pw_aff_apply_pw_aff(
9182 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_pw_aff
*pa
)
9186 isl_space
*space
, *space2
;
9187 isl_union_pw_aff
*upa
;
9189 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
9190 isl_pw_aff_get_space(pa
));
9191 pa
= isl_pw_aff_align_params(pa
,
9192 isl_multi_union_pw_aff_get_space(mupa
));
9196 space
= isl_multi_union_pw_aff_get_space(mupa
);
9197 space2
= isl_pw_aff_get_domain_space(pa
);
9198 equal
= isl_space_is_equal(space
, space2
);
9199 isl_space_free(space
);
9200 isl_space_free(space2
);
9204 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
9205 "spaces don't match", goto error
);
9206 if (isl_pw_aff_dim(pa
, isl_dim_in
) == 0)
9207 return isl_multi_union_pw_aff_apply_pw_aff_0D(mupa
, pa
);
9209 space
= isl_space_params(isl_multi_union_pw_aff_get_space(mupa
));
9210 upa
= isl_union_pw_aff_empty(space
);
9212 for (i
= 0; i
< pa
->n
; ++i
) {
9215 isl_multi_union_pw_aff
*mupa_i
;
9216 isl_union_pw_aff
*upa_i
;
9218 mupa_i
= isl_multi_union_pw_aff_copy(mupa
);
9219 domain
= isl_set_copy(pa
->p
[i
].set
);
9220 mupa_i
= isl_multi_union_pw_aff_intersect_range(mupa_i
, domain
);
9221 aff
= isl_aff_copy(pa
->p
[i
].aff
);
9222 upa_i
= multi_union_pw_aff_apply_aff(mupa_i
, aff
);
9223 upa
= isl_union_pw_aff_union_add(upa
, upa_i
);
9226 isl_multi_union_pw_aff_free(mupa
);
9227 isl_pw_aff_free(pa
);
9230 isl_multi_union_pw_aff_free(mupa
);
9231 isl_pw_aff_free(pa
);
9235 /* Apply "pma" to "mupa", in the special case where "mupa" is 0D.
9236 * The space of "mupa" is known to be compatible with the domain of "pma".
9238 * Construct an isl_multi_union_pw_aff that is equal to "pma"
9239 * on the domain of "mupa".
9241 static __isl_give isl_multi_union_pw_aff
*mupa_apply_pw_multi_aff_0D(
9242 __isl_take isl_multi_union_pw_aff
*mupa
,
9243 __isl_take isl_pw_multi_aff
*pma
)
9247 dom
= isl_multi_union_pw_aff_domain(mupa
);
9248 pma
= isl_pw_multi_aff_project_domain_on_params(pma
);
9250 return isl_multi_union_pw_aff_pw_multi_aff_on_domain(dom
, pma
);
9253 /* Apply "pma" to "mupa". The space of "mupa" needs to be compatible
9254 * with the domain of "pma".
9255 * The result is defined over the shared domain of the elements of "mupa"
9257 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_apply_pw_multi_aff(
9258 __isl_take isl_multi_union_pw_aff
*mupa
,
9259 __isl_take isl_pw_multi_aff
*pma
)
9261 isl_space
*space1
, *space2
;
9262 isl_multi_union_pw_aff
*res
;
9266 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
9267 isl_pw_multi_aff_get_space(pma
));
9268 pma
= isl_pw_multi_aff_align_params(pma
,
9269 isl_multi_union_pw_aff_get_space(mupa
));
9273 space1
= isl_multi_union_pw_aff_get_space(mupa
);
9274 space2
= isl_pw_multi_aff_get_domain_space(pma
);
9275 equal
= isl_space_is_equal(space1
, space2
);
9276 isl_space_free(space1
);
9277 isl_space_free(space2
);
9281 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
9282 "spaces don't match", goto error
);
9283 n_out
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
9284 if (isl_pw_multi_aff_dim(pma
, isl_dim_in
) == 0)
9285 return mupa_apply_pw_multi_aff_0D(mupa
, pma
);
9287 space1
= isl_space_range(isl_pw_multi_aff_get_space(pma
));
9288 res
= isl_multi_union_pw_aff_alloc(space1
);
9290 for (i
= 0; i
< n_out
; ++i
) {
9292 isl_union_pw_aff
*upa
;
9294 pa
= isl_pw_multi_aff_get_pw_aff(pma
, i
);
9295 upa
= isl_multi_union_pw_aff_apply_pw_aff(
9296 isl_multi_union_pw_aff_copy(mupa
), pa
);
9297 res
= isl_multi_union_pw_aff_set_union_pw_aff(res
, i
, upa
);
9300 isl_pw_multi_aff_free(pma
);
9301 isl_multi_union_pw_aff_free(mupa
);
9304 isl_multi_union_pw_aff_free(mupa
);
9305 isl_pw_multi_aff_free(pma
);
9309 /* Replace the explicit domain of "mupa" by its preimage under "upma".
9310 * If the explicit domain only keeps track of constraints on the parameters,
9311 * then only update those constraints.
9313 static __isl_give isl_multi_union_pw_aff
*preimage_explicit_domain(
9314 __isl_take isl_multi_union_pw_aff
*mupa
,
9315 __isl_keep isl_union_pw_multi_aff
*upma
)
9319 if (isl_multi_union_pw_aff_check_has_explicit_domain(mupa
) < 0)
9320 return isl_multi_union_pw_aff_free(mupa
);
9322 mupa
= isl_multi_union_pw_aff_cow(mupa
);
9326 is_params
= isl_union_set_is_params(mupa
->u
.dom
);
9328 return isl_multi_union_pw_aff_free(mupa
);
9330 upma
= isl_union_pw_multi_aff_copy(upma
);
9332 mupa
->u
.dom
= isl_union_set_intersect_params(mupa
->u
.dom
,
9333 isl_union_set_params(isl_union_pw_multi_aff_domain(upma
)));
9335 mupa
->u
.dom
= isl_union_set_preimage_union_pw_multi_aff(
9338 return isl_multi_union_pw_aff_free(mupa
);
9342 /* Compute the pullback of "mupa" by the function represented by "upma".
9343 * In other words, plug in "upma" in "mupa". The result contains
9344 * expressions defined over the domain space of "upma".
9346 * Run over all elements of "mupa" and plug in "upma" in each of them.
9348 * If "mupa" has an explicit domain, then it is this domain
9349 * that needs to undergo a pullback instead, i.e., a preimage.
9351 __isl_give isl_multi_union_pw_aff
*
9352 isl_multi_union_pw_aff_pullback_union_pw_multi_aff(
9353 __isl_take isl_multi_union_pw_aff
*mupa
,
9354 __isl_take isl_union_pw_multi_aff
*upma
)
9358 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
9359 isl_union_pw_multi_aff_get_space(upma
));
9360 upma
= isl_union_pw_multi_aff_align_params(upma
,
9361 isl_multi_union_pw_aff_get_space(mupa
));
9362 mupa
= isl_multi_union_pw_aff_cow(mupa
);
9366 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
9367 for (i
= 0; i
< n
; ++i
) {
9368 isl_union_pw_aff
*upa
;
9370 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
9371 upa
= isl_union_pw_aff_pullback_union_pw_multi_aff(upa
,
9372 isl_union_pw_multi_aff_copy(upma
));
9373 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
9376 if (isl_multi_union_pw_aff_has_explicit_domain(mupa
))
9377 mupa
= preimage_explicit_domain(mupa
, upma
);
9379 isl_union_pw_multi_aff_free(upma
);
9382 isl_multi_union_pw_aff_free(mupa
);
9383 isl_union_pw_multi_aff_free(upma
);
9387 /* Extract the sequence of elements in "mupa" with domain space "space"
9388 * (ignoring parameters).
9390 * For the elements of "mupa" that are not defined on the specified space,
9391 * the corresponding element in the result is empty.
9393 __isl_give isl_multi_pw_aff
*isl_multi_union_pw_aff_extract_multi_pw_aff(
9394 __isl_keep isl_multi_union_pw_aff
*mupa
, __isl_take isl_space
*space
)
9397 isl_space
*space_mpa
;
9398 isl_multi_pw_aff
*mpa
;
9400 if (!mupa
|| !space
)
9403 space_mpa
= isl_multi_union_pw_aff_get_space(mupa
);
9404 space
= isl_space_replace_params(space
, space_mpa
);
9405 space_mpa
= isl_space_map_from_domain_and_range(isl_space_copy(space
),
9407 mpa
= isl_multi_pw_aff_alloc(space_mpa
);
9409 space
= isl_space_from_domain(space
);
9410 space
= isl_space_add_dims(space
, isl_dim_out
, 1);
9411 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
9412 for (i
= 0; i
< n
; ++i
) {
9413 isl_union_pw_aff
*upa
;
9416 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
9417 pa
= isl_union_pw_aff_extract_pw_aff(upa
,
9418 isl_space_copy(space
));
9419 mpa
= isl_multi_pw_aff_set_pw_aff(mpa
, i
, pa
);
9420 isl_union_pw_aff_free(upa
);
9423 isl_space_free(space
);
9426 isl_space_free(space
);
9430 /* Evaluate the affine function "aff" in the void point "pnt".
9431 * In particular, return the value NaN.
9433 static __isl_give isl_val
*eval_void(__isl_take isl_aff
*aff
,
9434 __isl_take isl_point
*pnt
)
9438 ctx
= isl_point_get_ctx(pnt
);
9440 isl_point_free(pnt
);
9441 return isl_val_nan(ctx
);
9444 /* Evaluate the affine expression "aff"
9445 * in the coordinates (with denominator) "pnt".
9447 static __isl_give isl_val
*eval(__isl_keep isl_vec
*aff
,
9448 __isl_keep isl_vec
*pnt
)
9457 ctx
= isl_vec_get_ctx(aff
);
9460 isl_seq_inner_product(aff
->el
+ 1, pnt
->el
, pnt
->size
, &n
);
9461 isl_int_mul(d
, aff
->el
[0], pnt
->el
[0]);
9462 v
= isl_val_rat_from_isl_int(ctx
, n
, d
);
9463 v
= isl_val_normalize(v
);
9470 /* Check that the domain space of "aff" is equal to "space".
9472 static isl_stat
isl_aff_check_has_domain_space(__isl_keep isl_aff
*aff
,
9473 __isl_keep isl_space
*space
)
9477 ok
= isl_space_is_equal(isl_aff_peek_domain_space(aff
), space
);
9479 return isl_stat_error
;
9481 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
9482 "incompatible spaces", return isl_stat_error
);
9486 /* Evaluate the affine function "aff" in "pnt".
9488 __isl_give isl_val
*isl_aff_eval(__isl_take isl_aff
*aff
,
9489 __isl_take isl_point
*pnt
)
9493 isl_local_space
*ls
;
9495 if (isl_aff_check_has_domain_space(aff
, isl_point_peek_space(pnt
)) < 0)
9497 is_void
= isl_point_is_void(pnt
);
9501 return eval_void(aff
, pnt
);
9503 ls
= isl_aff_get_domain_local_space(aff
);
9504 pnt
= isl_local_space_lift_point(ls
, pnt
);
9506 v
= eval(aff
->v
, isl_point_peek_vec(pnt
));
9509 isl_point_free(pnt
);
9514 isl_point_free(pnt
);