2 * Copyright 2011 INRIA Saclay
3 * Copyright 2011 Sven Verdoolaege
4 * Copyright 2012-2014 Ecole Normale Superieure
5 * Copyright 2014 INRIA Rocquencourt
6 * Copyright 2016 Sven Verdoolaege
7 * Copyright 2018,2020 Cerebras Systems
8 * Copyright 2021 Sven Verdoolaege
10 * Use of this software is governed by the MIT license
12 * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
13 * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
15 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
16 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
17 * B.P. 105 - 78153 Le Chesnay, France
18 * and Cerebras Systems, 175 S San Antonio Rd, Los Altos, CA, USA
21 #include <isl_ctx_private.h>
22 #include <isl_map_private.h>
23 #include <isl_union_map_private.h>
24 #include <isl_aff_private.h>
25 #include <isl_space_private.h>
26 #include <isl_local_space_private.h>
27 #include <isl_vec_private.h>
28 #include <isl_mat_private.h>
29 #include <isl_id_private.h>
30 #include <isl/constraint.h>
33 #include <isl_val_private.h>
34 #include <isl_point_private.h>
35 #include <isl_config.h>
40 #include <isl_list_templ.c>
43 #define EL_BASE pw_aff
45 #include <isl_list_templ.c>
48 #define EL_BASE pw_multi_aff
50 #include <isl_list_templ.c>
53 #define EL_BASE union_pw_aff
55 #include <isl_list_templ.c>
58 #define EL_BASE union_pw_multi_aff
60 #include <isl_list_templ.c>
62 __isl_give isl_aff
*isl_aff_alloc_vec(__isl_take isl_local_space
*ls
,
63 __isl_take isl_vec
*v
)
70 aff
= isl_calloc_type(v
->ctx
, struct isl_aff
);
80 isl_local_space_free(ls
);
85 __isl_give isl_aff
*isl_aff_alloc(__isl_take isl_local_space
*ls
)
94 ctx
= isl_local_space_get_ctx(ls
);
95 if (!isl_local_space_divs_known(ls
))
96 isl_die(ctx
, isl_error_invalid
, "local space has unknown divs",
98 if (!isl_local_space_is_set(ls
))
99 isl_die(ctx
, isl_error_invalid
,
100 "domain of affine expression should be a set",
103 total
= isl_local_space_dim(ls
, isl_dim_all
);
106 v
= isl_vec_alloc(ctx
, 1 + 1 + total
);
107 return isl_aff_alloc_vec(ls
, v
);
109 isl_local_space_free(ls
);
113 __isl_give isl_aff
*isl_aff_copy(__isl_keep isl_aff
*aff
)
122 __isl_give isl_aff
*isl_aff_dup(__isl_keep isl_aff
*aff
)
127 return isl_aff_alloc_vec(isl_local_space_copy(aff
->ls
),
128 isl_vec_copy(aff
->v
));
131 __isl_give isl_aff
*isl_aff_cow(__isl_take isl_aff
*aff
)
139 return isl_aff_dup(aff
);
142 __isl_give isl_aff
*isl_aff_zero_on_domain(__isl_take isl_local_space
*ls
)
146 aff
= isl_aff_alloc(ls
);
150 isl_int_set_si(aff
->v
->el
[0], 1);
151 isl_seq_clr(aff
->v
->el
+ 1, aff
->v
->size
- 1);
156 /* Return an affine expression that is equal to zero on domain space "space".
158 __isl_give isl_aff
*isl_aff_zero_on_domain_space(__isl_take isl_space
*space
)
160 return isl_aff_zero_on_domain(isl_local_space_from_space(space
));
163 /* This function performs the same operation as isl_aff_zero_on_domain_space,
164 * but is considered as a function on an isl_space when exported.
166 __isl_give isl_aff
*isl_space_zero_aff_on_domain(__isl_take isl_space
*space
)
168 return isl_aff_zero_on_domain_space(space
);
171 /* Return a piecewise affine expression defined on the specified domain
172 * that is equal to zero.
174 __isl_give isl_pw_aff
*isl_pw_aff_zero_on_domain(__isl_take isl_local_space
*ls
)
176 return isl_pw_aff_from_aff(isl_aff_zero_on_domain(ls
));
179 /* Change "aff" into a NaN.
181 * Note that this function gets called from isl_aff_nan_on_domain,
182 * so "aff" may not have been initialized yet.
184 static __isl_give isl_aff
*isl_aff_set_nan(__isl_take isl_aff
*aff
)
186 aff
= isl_aff_cow(aff
);
190 aff
->v
= isl_vec_clr(aff
->v
);
192 return isl_aff_free(aff
);
197 /* Return an affine expression defined on the specified domain
198 * that represents NaN.
200 __isl_give isl_aff
*isl_aff_nan_on_domain(__isl_take isl_local_space
*ls
)
204 aff
= isl_aff_alloc(ls
);
205 return isl_aff_set_nan(aff
);
208 /* Return an affine expression defined on the specified domain space
209 * that represents NaN.
211 __isl_give isl_aff
*isl_aff_nan_on_domain_space(__isl_take isl_space
*space
)
213 return isl_aff_nan_on_domain(isl_local_space_from_space(space
));
216 /* Return a piecewise affine expression defined on the specified domain space
217 * that represents NaN.
219 __isl_give isl_pw_aff
*isl_pw_aff_nan_on_domain_space(
220 __isl_take isl_space
*space
)
222 return isl_pw_aff_from_aff(isl_aff_nan_on_domain_space(space
));
225 /* Return a piecewise affine expression defined on the specified domain
226 * that represents NaN.
228 __isl_give isl_pw_aff
*isl_pw_aff_nan_on_domain(__isl_take isl_local_space
*ls
)
230 return isl_pw_aff_from_aff(isl_aff_nan_on_domain(ls
));
233 /* Return an affine expression that is equal to "val" on
234 * domain local space "ls".
236 __isl_give isl_aff
*isl_aff_val_on_domain(__isl_take isl_local_space
*ls
,
237 __isl_take isl_val
*val
)
243 if (!isl_val_is_rat(val
))
244 isl_die(isl_val_get_ctx(val
), isl_error_invalid
,
245 "expecting rational value", goto error
);
247 aff
= isl_aff_alloc(isl_local_space_copy(ls
));
251 isl_seq_clr(aff
->v
->el
+ 2, aff
->v
->size
- 2);
252 isl_int_set(aff
->v
->el
[1], val
->n
);
253 isl_int_set(aff
->v
->el
[0], val
->d
);
255 isl_local_space_free(ls
);
259 isl_local_space_free(ls
);
264 /* Return an affine expression that is equal to "val" on domain space "space".
266 __isl_give isl_aff
*isl_aff_val_on_domain_space(__isl_take isl_space
*space
,
267 __isl_take isl_val
*val
)
269 return isl_aff_val_on_domain(isl_local_space_from_space(space
), val
);
272 /* Return an affine expression that is equal to the specified dimension
275 __isl_give isl_aff
*isl_aff_var_on_domain(__isl_take isl_local_space
*ls
,
276 enum isl_dim_type type
, unsigned pos
)
284 space
= isl_local_space_get_space(ls
);
287 if (isl_space_is_map(space
))
288 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
289 "expecting (parameter) set space", goto error
);
290 if (isl_local_space_check_range(ls
, type
, pos
, 1) < 0)
293 isl_space_free(space
);
294 aff
= isl_aff_alloc(ls
);
298 pos
+= isl_local_space_offset(aff
->ls
, type
);
300 isl_int_set_si(aff
->v
->el
[0], 1);
301 isl_seq_clr(aff
->v
->el
+ 1, aff
->v
->size
- 1);
302 isl_int_set_si(aff
->v
->el
[1 + pos
], 1);
306 isl_local_space_free(ls
);
307 isl_space_free(space
);
311 /* Return a piecewise affine expression that is equal to
312 * the specified dimension in "ls".
314 __isl_give isl_pw_aff
*isl_pw_aff_var_on_domain(__isl_take isl_local_space
*ls
,
315 enum isl_dim_type type
, unsigned pos
)
317 return isl_pw_aff_from_aff(isl_aff_var_on_domain(ls
, type
, pos
));
320 /* Return an affine expression that is equal to the parameter
321 * in the domain space "space" with identifier "id".
323 __isl_give isl_aff
*isl_aff_param_on_domain_space_id(
324 __isl_take isl_space
*space
, __isl_take isl_id
*id
)
331 pos
= isl_space_find_dim_by_id(space
, isl_dim_param
, id
);
333 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
334 "parameter not found in space", goto error
);
336 ls
= isl_local_space_from_space(space
);
337 return isl_aff_var_on_domain(ls
, isl_dim_param
, pos
);
339 isl_space_free(space
);
344 __isl_null isl_aff
*isl_aff_free(__isl_take isl_aff
*aff
)
352 isl_local_space_free(aff
->ls
);
353 isl_vec_free(aff
->v
);
360 isl_ctx
*isl_aff_get_ctx(__isl_keep isl_aff
*aff
)
362 return aff
? isl_local_space_get_ctx(aff
->ls
) : NULL
;
365 /* Return a hash value that digests "aff".
367 uint32_t isl_aff_get_hash(__isl_keep isl_aff
*aff
)
369 uint32_t hash
, ls_hash
, v_hash
;
374 hash
= isl_hash_init();
375 ls_hash
= isl_local_space_get_hash(aff
->ls
);
376 isl_hash_hash(hash
, ls_hash
);
377 v_hash
= isl_vec_get_hash(aff
->v
);
378 isl_hash_hash(hash
, v_hash
);
383 /* Return the domain local space of "aff".
385 static __isl_keep isl_local_space
*isl_aff_peek_domain_local_space(
386 __isl_keep isl_aff
*aff
)
388 return aff
? aff
->ls
: NULL
;
391 /* Return the number of variables of the given type in the domain of "aff".
393 isl_size
isl_aff_domain_dim(__isl_keep isl_aff
*aff
, enum isl_dim_type type
)
397 ls
= isl_aff_peek_domain_local_space(aff
);
398 return isl_local_space_dim(ls
, type
);
401 /* Externally, an isl_aff has a map space, but internally, the
402 * ls field corresponds to the domain of that space.
404 isl_size
isl_aff_dim(__isl_keep isl_aff
*aff
, enum isl_dim_type type
)
407 return isl_size_error
;
408 if (type
== isl_dim_out
)
410 if (type
== isl_dim_in
)
412 return isl_aff_domain_dim(aff
, type
);
415 /* Return the offset of the first coefficient of type "type" in
416 * the domain of "aff".
418 isl_size
isl_aff_domain_offset(__isl_keep isl_aff
*aff
, enum isl_dim_type type
)
422 ls
= isl_aff_peek_domain_local_space(aff
);
423 return isl_local_space_offset(ls
, type
);
426 /* Return the position of the dimension of the given type and name
428 * Return -1 if no such dimension can be found.
430 int isl_aff_find_dim_by_name(__isl_keep isl_aff
*aff
, enum isl_dim_type type
,
435 if (type
== isl_dim_out
)
437 if (type
== isl_dim_in
)
439 return isl_local_space_find_dim_by_name(aff
->ls
, type
, name
);
442 /* Return the domain space of "aff".
444 static __isl_keep isl_space
*isl_aff_peek_domain_space(__isl_keep isl_aff
*aff
)
446 return aff
? isl_local_space_peek_space(aff
->ls
) : NULL
;
449 __isl_give isl_space
*isl_aff_get_domain_space(__isl_keep isl_aff
*aff
)
451 return isl_space_copy(isl_aff_peek_domain_space(aff
));
454 __isl_give isl_space
*isl_aff_get_space(__isl_keep isl_aff
*aff
)
459 space
= isl_local_space_get_space(aff
->ls
);
460 space
= isl_space_from_domain(space
);
461 space
= isl_space_add_dims(space
, isl_dim_out
, 1);
465 /* Return a copy of the domain space of "aff".
467 __isl_give isl_local_space
*isl_aff_get_domain_local_space(
468 __isl_keep isl_aff
*aff
)
470 return isl_local_space_copy(isl_aff_peek_domain_local_space(aff
));
473 __isl_give isl_local_space
*isl_aff_get_local_space(__isl_keep isl_aff
*aff
)
478 ls
= isl_local_space_copy(aff
->ls
);
479 ls
= isl_local_space_from_domain(ls
);
480 ls
= isl_local_space_add_dims(ls
, isl_dim_out
, 1);
484 /* Return the local space of the domain of "aff".
485 * This may be either a copy or the local space itself
486 * if there is only one reference to "aff".
487 * This allows the local space to be modified inplace
488 * if both the expression and its local space have only a single reference.
489 * The caller is not allowed to modify "aff" between this call and
490 * a subsequent call to isl_aff_restore_domain_local_space.
491 * The only exception is that isl_aff_free can be called instead.
493 __isl_give isl_local_space
*isl_aff_take_domain_local_space(
494 __isl_keep isl_aff
*aff
)
501 return isl_aff_get_domain_local_space(aff
);
507 /* Set the local space of the domain of "aff" to "ls",
508 * where the local space of "aff" may be missing
509 * due to a preceding call to isl_aff_take_domain_local_space.
510 * However, in this case, "aff" only has a single reference and
511 * then the call to isl_aff_cow has no effect.
513 __isl_give isl_aff
*isl_aff_restore_domain_local_space(
514 __isl_keep isl_aff
*aff
, __isl_take isl_local_space
*ls
)
520 isl_local_space_free(ls
);
524 aff
= isl_aff_cow(aff
);
527 isl_local_space_free(aff
->ls
);
533 isl_local_space_free(ls
);
537 /* Externally, an isl_aff has a map space, but internally, the
538 * ls field corresponds to the domain of that space.
540 const char *isl_aff_get_dim_name(__isl_keep isl_aff
*aff
,
541 enum isl_dim_type type
, unsigned pos
)
545 if (type
== isl_dim_out
)
547 if (type
== isl_dim_in
)
549 return isl_local_space_get_dim_name(aff
->ls
, type
, pos
);
552 __isl_give isl_aff
*isl_aff_reset_domain_space(__isl_take isl_aff
*aff
,
553 __isl_take isl_space
*space
)
555 aff
= isl_aff_cow(aff
);
559 aff
->ls
= isl_local_space_reset_space(aff
->ls
, space
);
561 return isl_aff_free(aff
);
566 isl_space_free(space
);
570 /* Reset the space of "aff". This function is called from isl_pw_templ.c
571 * and doesn't know if the space of an element object is represented
572 * directly or through its domain. It therefore passes along both.
574 __isl_give isl_aff
*isl_aff_reset_space_and_domain(__isl_take isl_aff
*aff
,
575 __isl_take isl_space
*space
, __isl_take isl_space
*domain
)
577 isl_space_free(space
);
578 return isl_aff_reset_domain_space(aff
, domain
);
581 /* Reorder the coefficients of the affine expression based
582 * on the given reordering.
583 * The reordering r is assumed to have been extended with the local
586 static __isl_give isl_vec
*vec_reorder(__isl_take isl_vec
*vec
,
587 __isl_take isl_reordering
*r
, int n_div
)
597 space
= isl_reordering_peek_space(r
);
598 dim
= isl_space_dim(space
, isl_dim_all
);
601 res
= isl_vec_alloc(vec
->ctx
, 2 + dim
+ n_div
);
604 isl_seq_cpy(res
->el
, vec
->el
, 2);
605 isl_seq_clr(res
->el
+ 2, res
->size
- 2);
606 for (i
= 0; i
< r
->len
; ++i
)
607 isl_int_set(res
->el
[2 + r
->pos
[i
]], vec
->el
[2 + i
]);
609 isl_reordering_free(r
);
614 isl_reordering_free(r
);
618 /* Reorder the dimensions of the domain of "aff" according
619 * to the given reordering.
621 __isl_give isl_aff
*isl_aff_realign_domain(__isl_take isl_aff
*aff
,
622 __isl_take isl_reordering
*r
)
624 aff
= isl_aff_cow(aff
);
628 r
= isl_reordering_extend(r
, aff
->ls
->div
->n_row
);
629 aff
->v
= vec_reorder(aff
->v
, isl_reordering_copy(r
),
630 aff
->ls
->div
->n_row
);
631 aff
->ls
= isl_local_space_realign(aff
->ls
, r
);
633 if (!aff
->v
|| !aff
->ls
)
634 return isl_aff_free(aff
);
639 isl_reordering_free(r
);
643 __isl_give isl_aff
*isl_aff_align_params(__isl_take isl_aff
*aff
,
644 __isl_take isl_space
*model
)
646 isl_bool equal_params
;
651 equal_params
= isl_space_has_equal_params(aff
->ls
->dim
, model
);
652 if (equal_params
< 0)
657 exp
= isl_parameter_alignment_reordering(aff
->ls
->dim
, model
);
658 exp
= isl_reordering_extend_space(exp
,
659 isl_aff_get_domain_space(aff
));
660 aff
= isl_aff_realign_domain(aff
, exp
);
663 isl_space_free(model
);
666 isl_space_free(model
);
673 #include "isl_unbind_params_templ.c"
675 /* Is "aff" obviously equal to zero?
677 * If the denominator is zero, then "aff" is not equal to zero.
679 isl_bool
isl_aff_plain_is_zero(__isl_keep isl_aff
*aff
)
684 return isl_bool_error
;
686 if (isl_int_is_zero(aff
->v
->el
[0]))
687 return isl_bool_false
;
688 pos
= isl_seq_first_non_zero(aff
->v
->el
+ 1, aff
->v
->size
- 1);
689 return isl_bool_ok(pos
< 0);
692 /* Does "aff" represent NaN?
694 isl_bool
isl_aff_is_nan(__isl_keep isl_aff
*aff
)
697 return isl_bool_error
;
699 return isl_bool_ok(isl_seq_first_non_zero(aff
->v
->el
, 2) < 0);
702 /* Are "aff1" and "aff2" obviously equal?
704 * NaN is not equal to anything, not even to another NaN.
706 isl_bool
isl_aff_plain_is_equal(__isl_keep isl_aff
*aff1
,
707 __isl_keep isl_aff
*aff2
)
712 return isl_bool_error
;
714 if (isl_aff_is_nan(aff1
) || isl_aff_is_nan(aff2
))
715 return isl_bool_false
;
717 equal
= isl_local_space_is_equal(aff1
->ls
, aff2
->ls
);
718 if (equal
< 0 || !equal
)
721 return isl_vec_is_equal(aff1
->v
, aff2
->v
);
724 /* Return the common denominator of "aff" in "v".
726 * We cannot return anything meaningful in case of a NaN.
728 isl_stat
isl_aff_get_denominator(__isl_keep isl_aff
*aff
, isl_int
*v
)
731 return isl_stat_error
;
732 if (isl_aff_is_nan(aff
))
733 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
734 "cannot get denominator of NaN", return isl_stat_error
);
735 isl_int_set(*v
, aff
->v
->el
[0]);
739 /* Return the common denominator of "aff".
741 __isl_give isl_val
*isl_aff_get_denominator_val(__isl_keep isl_aff
*aff
)
748 ctx
= isl_aff_get_ctx(aff
);
749 if (isl_aff_is_nan(aff
))
750 return isl_val_nan(ctx
);
751 return isl_val_int_from_isl_int(ctx
, aff
->v
->el
[0]);
754 /* Return the constant term of "aff".
756 __isl_give isl_val
*isl_aff_get_constant_val(__isl_keep isl_aff
*aff
)
764 ctx
= isl_aff_get_ctx(aff
);
765 if (isl_aff_is_nan(aff
))
766 return isl_val_nan(ctx
);
767 v
= isl_val_rat_from_isl_int(ctx
, aff
->v
->el
[1], aff
->v
->el
[0]);
768 return isl_val_normalize(v
);
771 /* Return the coefficient of the variable of type "type" at position "pos"
774 __isl_give isl_val
*isl_aff_get_coefficient_val(__isl_keep isl_aff
*aff
,
775 enum isl_dim_type type
, int pos
)
783 ctx
= isl_aff_get_ctx(aff
);
784 if (type
== isl_dim_out
)
785 isl_die(ctx
, isl_error_invalid
,
786 "output/set dimension does not have a coefficient",
788 if (type
== isl_dim_in
)
791 if (isl_local_space_check_range(aff
->ls
, type
, pos
, 1) < 0)
794 if (isl_aff_is_nan(aff
))
795 return isl_val_nan(ctx
);
796 pos
+= isl_local_space_offset(aff
->ls
, type
);
797 v
= isl_val_rat_from_isl_int(ctx
, aff
->v
->el
[1 + pos
], aff
->v
->el
[0]);
798 return isl_val_normalize(v
);
801 /* Return the sign of the coefficient of the variable of type "type"
802 * at position "pos" of "aff".
804 int isl_aff_coefficient_sgn(__isl_keep isl_aff
*aff
, enum isl_dim_type type
,
812 ctx
= isl_aff_get_ctx(aff
);
813 if (type
== isl_dim_out
)
814 isl_die(ctx
, isl_error_invalid
,
815 "output/set dimension does not have a coefficient",
817 if (type
== isl_dim_in
)
820 if (isl_local_space_check_range(aff
->ls
, type
, pos
, 1) < 0)
823 pos
+= isl_local_space_offset(aff
->ls
, type
);
824 return isl_int_sgn(aff
->v
->el
[1 + pos
]);
827 /* Replace the numerator of the constant term of "aff" by "v".
829 * A NaN is unaffected by this operation.
831 __isl_give isl_aff
*isl_aff_set_constant(__isl_take isl_aff
*aff
, isl_int v
)
835 if (isl_aff_is_nan(aff
))
837 aff
= isl_aff_cow(aff
);
841 aff
->v
= isl_vec_cow(aff
->v
);
843 return isl_aff_free(aff
);
845 isl_int_set(aff
->v
->el
[1], v
);
850 /* Replace the constant term of "aff" by "v".
852 * A NaN is unaffected by this operation.
854 __isl_give isl_aff
*isl_aff_set_constant_val(__isl_take isl_aff
*aff
,
855 __isl_take isl_val
*v
)
860 if (isl_aff_is_nan(aff
)) {
865 if (!isl_val_is_rat(v
))
866 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
867 "expecting rational value", goto error
);
869 if (isl_int_eq(aff
->v
->el
[1], v
->n
) &&
870 isl_int_eq(aff
->v
->el
[0], v
->d
)) {
875 aff
= isl_aff_cow(aff
);
878 aff
->v
= isl_vec_cow(aff
->v
);
882 if (isl_int_eq(aff
->v
->el
[0], v
->d
)) {
883 isl_int_set(aff
->v
->el
[1], v
->n
);
884 } else if (isl_int_is_one(v
->d
)) {
885 isl_int_mul(aff
->v
->el
[1], aff
->v
->el
[0], v
->n
);
887 isl_seq_scale(aff
->v
->el
+ 1,
888 aff
->v
->el
+ 1, v
->d
, aff
->v
->size
- 1);
889 isl_int_mul(aff
->v
->el
[1], aff
->v
->el
[0], v
->n
);
890 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], v
->d
);
891 aff
->v
= isl_vec_normalize(aff
->v
);
904 /* Add "v" to the constant term of "aff".
906 * A NaN is unaffected by this operation.
908 __isl_give isl_aff
*isl_aff_add_constant(__isl_take isl_aff
*aff
, isl_int v
)
910 if (isl_int_is_zero(v
))
915 if (isl_aff_is_nan(aff
))
917 aff
= isl_aff_cow(aff
);
921 aff
->v
= isl_vec_cow(aff
->v
);
923 return isl_aff_free(aff
);
925 isl_int_addmul(aff
->v
->el
[1], aff
->v
->el
[0], v
);
930 /* Add "v" to the constant term of "aff",
931 * in case "aff" is a rational expression.
933 static __isl_give isl_aff
*isl_aff_add_rat_constant_val(__isl_take isl_aff
*aff
,
934 __isl_take isl_val
*v
)
936 aff
= isl_aff_cow(aff
);
940 aff
->v
= isl_vec_cow(aff
->v
);
944 if (isl_int_is_one(v
->d
)) {
945 isl_int_addmul(aff
->v
->el
[1], aff
->v
->el
[0], v
->n
);
946 } else if (isl_int_eq(aff
->v
->el
[0], v
->d
)) {
947 isl_int_add(aff
->v
->el
[1], aff
->v
->el
[1], v
->n
);
948 aff
->v
= isl_vec_normalize(aff
->v
);
952 isl_seq_scale(aff
->v
->el
+ 1,
953 aff
->v
->el
+ 1, v
->d
, aff
->v
->size
- 1);
954 isl_int_addmul(aff
->v
->el
[1], aff
->v
->el
[0], v
->n
);
955 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], v
->d
);
956 aff
->v
= isl_vec_normalize(aff
->v
);
969 /* Return the first argument and free the second.
971 static __isl_give isl_aff
*pick_free(__isl_take isl_aff
*aff
,
972 __isl_take isl_val
*v
)
978 /* Replace the first argument by NaN and free the second argument.
980 static __isl_give isl_aff
*set_nan_free_val(__isl_take isl_aff
*aff
,
981 __isl_take isl_val
*v
)
984 return isl_aff_set_nan(aff
);
987 /* Add "v" to the constant term of "aff".
989 * A NaN is unaffected by this operation.
990 * Conversely, adding a NaN turns "aff" into a NaN.
992 __isl_give isl_aff
*isl_aff_add_constant_val(__isl_take isl_aff
*aff
,
993 __isl_take isl_val
*v
)
995 isl_bool is_nan
, is_zero
, is_rat
;
997 is_nan
= isl_aff_is_nan(aff
);
998 is_zero
= isl_val_is_zero(v
);
999 if (is_nan
< 0 || is_zero
< 0)
1001 if (is_nan
|| is_zero
)
1002 return pick_free(aff
, v
);
1004 is_nan
= isl_val_is_nan(v
);
1005 is_rat
= isl_val_is_rat(v
);
1006 if (is_nan
< 0 || is_rat
< 0)
1009 return set_nan_free_val(aff
, v
);
1011 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1012 "expecting rational value or NaN", goto error
);
1014 return isl_aff_add_rat_constant_val(aff
, v
);
1021 __isl_give isl_aff
*isl_aff_add_constant_si(__isl_take isl_aff
*aff
, int v
)
1026 isl_int_set_si(t
, v
);
1027 aff
= isl_aff_add_constant(aff
, t
);
1033 /* Add "v" to the numerator of the constant term of "aff".
1035 * A NaN is unaffected by this operation.
1037 __isl_give isl_aff
*isl_aff_add_constant_num(__isl_take isl_aff
*aff
, isl_int v
)
1039 if (isl_int_is_zero(v
))
1044 if (isl_aff_is_nan(aff
))
1046 aff
= isl_aff_cow(aff
);
1050 aff
->v
= isl_vec_cow(aff
->v
);
1052 return isl_aff_free(aff
);
1054 isl_int_add(aff
->v
->el
[1], aff
->v
->el
[1], v
);
1059 /* Add "v" to the numerator of the constant term of "aff".
1061 * A NaN is unaffected by this operation.
1063 __isl_give isl_aff
*isl_aff_add_constant_num_si(__isl_take isl_aff
*aff
, int v
)
1071 isl_int_set_si(t
, v
);
1072 aff
= isl_aff_add_constant_num(aff
, t
);
1078 /* Replace the numerator of the constant term of "aff" by "v".
1080 * A NaN is unaffected by this operation.
1082 __isl_give isl_aff
*isl_aff_set_constant_si(__isl_take isl_aff
*aff
, int v
)
1086 if (isl_aff_is_nan(aff
))
1088 aff
= isl_aff_cow(aff
);
1092 aff
->v
= isl_vec_cow(aff
->v
);
1094 return isl_aff_free(aff
);
1096 isl_int_set_si(aff
->v
->el
[1], v
);
1101 /* Replace the numerator of the coefficient of the variable of type "type"
1102 * at position "pos" of "aff" by "v".
1104 * A NaN is unaffected by this operation.
1106 __isl_give isl_aff
*isl_aff_set_coefficient(__isl_take isl_aff
*aff
,
1107 enum isl_dim_type type
, int pos
, isl_int v
)
1112 if (type
== isl_dim_out
)
1113 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1114 "output/set dimension does not have a coefficient",
1115 return isl_aff_free(aff
));
1116 if (type
== isl_dim_in
)
1119 if (isl_local_space_check_range(aff
->ls
, type
, pos
, 1) < 0)
1120 return isl_aff_free(aff
);
1122 if (isl_aff_is_nan(aff
))
1124 aff
= isl_aff_cow(aff
);
1128 aff
->v
= isl_vec_cow(aff
->v
);
1130 return isl_aff_free(aff
);
1132 pos
+= isl_local_space_offset(aff
->ls
, type
);
1133 isl_int_set(aff
->v
->el
[1 + pos
], v
);
1138 /* Replace the numerator of the coefficient of the variable of type "type"
1139 * at position "pos" of "aff" by "v".
1141 * A NaN is unaffected by this operation.
1143 __isl_give isl_aff
*isl_aff_set_coefficient_si(__isl_take isl_aff
*aff
,
1144 enum isl_dim_type type
, int pos
, int v
)
1149 if (type
== isl_dim_out
)
1150 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1151 "output/set dimension does not have a coefficient",
1152 return isl_aff_free(aff
));
1153 if (type
== isl_dim_in
)
1156 if (isl_local_space_check_range(aff
->ls
, type
, pos
, 1) < 0)
1157 return isl_aff_free(aff
);
1159 if (isl_aff_is_nan(aff
))
1161 pos
+= isl_local_space_offset(aff
->ls
, type
);
1162 if (isl_int_cmp_si(aff
->v
->el
[1 + pos
], v
) == 0)
1165 aff
= isl_aff_cow(aff
);
1169 aff
->v
= isl_vec_cow(aff
->v
);
1171 return isl_aff_free(aff
);
1173 isl_int_set_si(aff
->v
->el
[1 + pos
], v
);
1178 /* Replace the coefficient of the variable of type "type" at position "pos"
1181 * A NaN is unaffected by this operation.
1183 __isl_give isl_aff
*isl_aff_set_coefficient_val(__isl_take isl_aff
*aff
,
1184 enum isl_dim_type type
, int pos
, __isl_take isl_val
*v
)
1189 if (type
== isl_dim_out
)
1190 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1191 "output/set dimension does not have a coefficient",
1193 if (type
== isl_dim_in
)
1196 if (isl_local_space_check_range(aff
->ls
, type
, pos
, 1) < 0)
1197 return isl_aff_free(aff
);
1199 if (isl_aff_is_nan(aff
)) {
1203 if (!isl_val_is_rat(v
))
1204 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1205 "expecting rational value", goto error
);
1207 pos
+= isl_local_space_offset(aff
->ls
, type
);
1208 if (isl_int_eq(aff
->v
->el
[1 + pos
], v
->n
) &&
1209 isl_int_eq(aff
->v
->el
[0], v
->d
)) {
1214 aff
= isl_aff_cow(aff
);
1217 aff
->v
= isl_vec_cow(aff
->v
);
1221 if (isl_int_eq(aff
->v
->el
[0], v
->d
)) {
1222 isl_int_set(aff
->v
->el
[1 + pos
], v
->n
);
1223 } else if (isl_int_is_one(v
->d
)) {
1224 isl_int_mul(aff
->v
->el
[1 + pos
], aff
->v
->el
[0], v
->n
);
1226 isl_seq_scale(aff
->v
->el
+ 1,
1227 aff
->v
->el
+ 1, v
->d
, aff
->v
->size
- 1);
1228 isl_int_mul(aff
->v
->el
[1 + pos
], aff
->v
->el
[0], v
->n
);
1229 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], v
->d
);
1230 aff
->v
= isl_vec_normalize(aff
->v
);
1243 /* Add "v" to the coefficient of the variable of type "type"
1244 * at position "pos" of "aff".
1246 * A NaN is unaffected by this operation.
1248 __isl_give isl_aff
*isl_aff_add_coefficient(__isl_take isl_aff
*aff
,
1249 enum isl_dim_type type
, int pos
, isl_int v
)
1254 if (type
== isl_dim_out
)
1255 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1256 "output/set dimension does not have a coefficient",
1257 return isl_aff_free(aff
));
1258 if (type
== isl_dim_in
)
1261 if (isl_local_space_check_range(aff
->ls
, type
, pos
, 1) < 0)
1262 return isl_aff_free(aff
);
1264 if (isl_aff_is_nan(aff
))
1266 aff
= isl_aff_cow(aff
);
1270 aff
->v
= isl_vec_cow(aff
->v
);
1272 return isl_aff_free(aff
);
1274 pos
+= isl_local_space_offset(aff
->ls
, type
);
1275 isl_int_addmul(aff
->v
->el
[1 + pos
], aff
->v
->el
[0], v
);
1280 /* Add "v" to the coefficient of the variable of type "type"
1281 * at position "pos" of "aff".
1283 * A NaN is unaffected by this operation.
1285 __isl_give isl_aff
*isl_aff_add_coefficient_val(__isl_take isl_aff
*aff
,
1286 enum isl_dim_type type
, int pos
, __isl_take isl_val
*v
)
1291 if (isl_val_is_zero(v
)) {
1296 if (type
== isl_dim_out
)
1297 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1298 "output/set dimension does not have a coefficient",
1300 if (type
== isl_dim_in
)
1303 if (isl_local_space_check_range(aff
->ls
, type
, pos
, 1) < 0)
1306 if (isl_aff_is_nan(aff
)) {
1310 if (!isl_val_is_rat(v
))
1311 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1312 "expecting rational value", goto error
);
1314 aff
= isl_aff_cow(aff
);
1318 aff
->v
= isl_vec_cow(aff
->v
);
1322 pos
+= isl_local_space_offset(aff
->ls
, type
);
1323 if (isl_int_is_one(v
->d
)) {
1324 isl_int_addmul(aff
->v
->el
[1 + pos
], aff
->v
->el
[0], v
->n
);
1325 } else if (isl_int_eq(aff
->v
->el
[0], v
->d
)) {
1326 isl_int_add(aff
->v
->el
[1 + pos
], aff
->v
->el
[1 + pos
], v
->n
);
1327 aff
->v
= isl_vec_normalize(aff
->v
);
1331 isl_seq_scale(aff
->v
->el
+ 1,
1332 aff
->v
->el
+ 1, v
->d
, aff
->v
->size
- 1);
1333 isl_int_addmul(aff
->v
->el
[1 + pos
], aff
->v
->el
[0], v
->n
);
1334 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], v
->d
);
1335 aff
->v
= isl_vec_normalize(aff
->v
);
1348 __isl_give isl_aff
*isl_aff_add_coefficient_si(__isl_take isl_aff
*aff
,
1349 enum isl_dim_type type
, int pos
, int v
)
1354 isl_int_set_si(t
, v
);
1355 aff
= isl_aff_add_coefficient(aff
, type
, pos
, t
);
1361 __isl_give isl_aff
*isl_aff_get_div(__isl_keep isl_aff
*aff
, int pos
)
1366 return isl_local_space_get_div(aff
->ls
, pos
);
1369 /* Return the negation of "aff".
1371 * As a special case, -NaN = NaN.
1373 __isl_give isl_aff
*isl_aff_neg(__isl_take isl_aff
*aff
)
1377 if (isl_aff_is_nan(aff
))
1379 aff
= isl_aff_cow(aff
);
1382 aff
->v
= isl_vec_cow(aff
->v
);
1384 return isl_aff_free(aff
);
1386 isl_seq_neg(aff
->v
->el
+ 1, aff
->v
->el
+ 1, aff
->v
->size
- 1);
1391 /* Remove divs from the local space that do not appear in the affine
1393 * We currently only remove divs at the end.
1394 * Some intermediate divs may also not appear directly in the affine
1395 * expression, but we would also need to check that no other divs are
1396 * defined in terms of them.
1398 __isl_give isl_aff
*isl_aff_remove_unused_divs(__isl_take isl_aff
*aff
)
1404 n
= isl_aff_domain_dim(aff
, isl_dim_div
);
1405 off
= isl_aff_domain_offset(aff
, isl_dim_div
);
1406 if (n
< 0 || off
< 0)
1407 return isl_aff_free(aff
);
1409 pos
= isl_seq_last_non_zero(aff
->v
->el
+ 1 + off
, n
) + 1;
1413 aff
= isl_aff_cow(aff
);
1417 aff
->ls
= isl_local_space_drop_dims(aff
->ls
, isl_dim_div
, pos
, n
- pos
);
1418 aff
->v
= isl_vec_drop_els(aff
->v
, 1 + off
+ pos
, n
- pos
);
1419 if (!aff
->ls
|| !aff
->v
)
1420 return isl_aff_free(aff
);
1425 /* Look for any divs in the aff->ls with a denominator equal to one
1426 * and plug them into the affine expression and any subsequent divs
1427 * that may reference the div.
1429 static __isl_give isl_aff
*plug_in_integral_divs(__isl_take isl_aff
*aff
)
1436 isl_local_space
*ls
;
1439 n
= isl_aff_domain_dim(aff
, isl_dim_div
);
1440 off
= isl_aff_domain_offset(aff
, isl_dim_div
);
1441 if (n
< 0 || off
< 0)
1442 return isl_aff_free(aff
);
1444 for (i
= 0; i
< n
; ++i
) {
1445 if (!isl_int_is_one(aff
->ls
->div
->row
[i
][0]))
1447 ls
= isl_local_space_copy(aff
->ls
);
1448 ls
= isl_local_space_substitute_seq(ls
, isl_dim_div
, i
,
1449 aff
->ls
->div
->row
[i
], len
, i
+ 1, n
- (i
+ 1));
1450 vec
= isl_vec_copy(aff
->v
);
1451 vec
= isl_vec_cow(vec
);
1457 isl_seq_substitute(vec
->el
, off
+ i
, aff
->ls
->div
->row
[i
],
1462 isl_vec_free(aff
->v
);
1464 isl_local_space_free(aff
->ls
);
1471 isl_local_space_free(ls
);
1472 return isl_aff_free(aff
);
1475 /* Look for any divs j that appear with a unit coefficient inside
1476 * the definitions of other divs i and plug them into the definitions
1479 * In particular, an expression of the form
1481 * floor((f(..) + floor(g(..)/n))/m)
1485 * floor((n * f(..) + g(..))/(n * m))
1487 * This simplification is correct because we can move the expression
1488 * f(..) into the inner floor in the original expression to obtain
1490 * floor(floor((n * f(..) + g(..))/n)/m)
1492 * from which we can derive the simplified expression.
1494 static __isl_give isl_aff
*plug_in_unit_divs(__isl_take isl_aff
*aff
)
1500 n
= isl_aff_domain_dim(aff
, isl_dim_div
);
1501 off
= isl_aff_domain_offset(aff
, isl_dim_div
);
1502 if (n
< 0 || off
< 0)
1503 return isl_aff_free(aff
);
1504 for (i
= 1; i
< n
; ++i
) {
1505 for (j
= 0; j
< i
; ++j
) {
1506 if (!isl_int_is_one(aff
->ls
->div
->row
[i
][1 + off
+ j
]))
1508 aff
->ls
= isl_local_space_substitute_seq(aff
->ls
,
1509 isl_dim_div
, j
, aff
->ls
->div
->row
[j
],
1510 aff
->v
->size
, i
, 1);
1512 return isl_aff_free(aff
);
1519 /* Swap divs "a" and "b" in "aff", which is assumed to be non-NULL.
1521 * Even though this function is only called on isl_affs with a single
1522 * reference, we are careful to only change aff->v and aff->ls together.
1524 static __isl_give isl_aff
*swap_div(__isl_take isl_aff
*aff
, int a
, int b
)
1526 isl_size off
= isl_aff_domain_offset(aff
, isl_dim_div
);
1527 isl_local_space
*ls
;
1531 return isl_aff_free(aff
);
1533 ls
= isl_local_space_copy(aff
->ls
);
1534 ls
= isl_local_space_swap_div(ls
, a
, b
);
1535 v
= isl_vec_copy(aff
->v
);
1540 isl_int_swap(v
->el
[1 + off
+ a
], v
->el
[1 + off
+ b
]);
1541 isl_vec_free(aff
->v
);
1543 isl_local_space_free(aff
->ls
);
1549 isl_local_space_free(ls
);
1550 return isl_aff_free(aff
);
1553 /* Merge divs "a" and "b" in "aff", which is assumed to be non-NULL.
1555 * We currently do not actually remove div "b", but simply add its
1556 * coefficient to that of "a" and then zero it out.
1558 static __isl_give isl_aff
*merge_divs(__isl_take isl_aff
*aff
, int a
, int b
)
1560 isl_size off
= isl_aff_domain_offset(aff
, isl_dim_div
);
1563 return isl_aff_free(aff
);
1565 if (isl_int_is_zero(aff
->v
->el
[1 + off
+ b
]))
1568 aff
->v
= isl_vec_cow(aff
->v
);
1570 return isl_aff_free(aff
);
1572 isl_int_add(aff
->v
->el
[1 + off
+ a
],
1573 aff
->v
->el
[1 + off
+ a
], aff
->v
->el
[1 + off
+ b
]);
1574 isl_int_set_si(aff
->v
->el
[1 + off
+ b
], 0);
1579 /* Sort the divs in the local space of "aff" according to
1580 * the comparison function "cmp_row" in isl_local_space.c,
1581 * combining the coefficients of identical divs.
1583 * Reordering divs does not change the semantics of "aff",
1584 * so there is no need to call isl_aff_cow.
1585 * Moreover, this function is currently only called on isl_affs
1586 * with a single reference.
1588 static __isl_give isl_aff
*sort_divs(__isl_take isl_aff
*aff
)
1593 n
= isl_aff_dim(aff
, isl_dim_div
);
1595 return isl_aff_free(aff
);
1596 for (i
= 1; i
< n
; ++i
) {
1597 for (j
= i
- 1; j
>= 0; --j
) {
1598 int cmp
= isl_mat_cmp_div(aff
->ls
->div
, j
, j
+ 1);
1602 aff
= merge_divs(aff
, j
, j
+ 1);
1604 aff
= swap_div(aff
, j
, j
+ 1);
1613 /* Normalize the representation of "aff".
1615 * This function should only be called on "new" isl_affs, i.e.,
1616 * with only a single reference. We therefore do not need to
1617 * worry about affecting other instances.
1619 __isl_give isl_aff
*isl_aff_normalize(__isl_take isl_aff
*aff
)
1623 aff
->v
= isl_vec_normalize(aff
->v
);
1625 return isl_aff_free(aff
);
1626 aff
= plug_in_integral_divs(aff
);
1627 aff
= plug_in_unit_divs(aff
);
1628 aff
= sort_divs(aff
);
1629 aff
= isl_aff_remove_unused_divs(aff
);
1633 /* Given f, return floor(f).
1634 * If f is an integer expression, then just return f.
1635 * If f is a constant, then return the constant floor(f).
1636 * Otherwise, if f = g/m, write g = q m + r,
1637 * create a new div d = [r/m] and return the expression q + d.
1638 * The coefficients in r are taken to lie between -m/2 and m/2.
1640 * reduce_div_coefficients performs the same normalization.
1642 * As a special case, floor(NaN) = NaN.
1644 __isl_give isl_aff
*isl_aff_floor(__isl_take isl_aff
*aff
)
1654 if (isl_aff_is_nan(aff
))
1656 if (isl_int_is_one(aff
->v
->el
[0]))
1659 aff
= isl_aff_cow(aff
);
1663 aff
->v
= isl_vec_cow(aff
->v
);
1665 return isl_aff_free(aff
);
1667 if (isl_aff_is_cst(aff
)) {
1668 isl_int_fdiv_q(aff
->v
->el
[1], aff
->v
->el
[1], aff
->v
->el
[0]);
1669 isl_int_set_si(aff
->v
->el
[0], 1);
1673 div
= isl_vec_copy(aff
->v
);
1674 div
= isl_vec_cow(div
);
1676 return isl_aff_free(aff
);
1678 ctx
= isl_aff_get_ctx(aff
);
1679 isl_int_fdiv_q(aff
->v
->el
[0], aff
->v
->el
[0], ctx
->two
);
1680 for (i
= 1; i
< aff
->v
->size
; ++i
) {
1681 isl_int_fdiv_r(div
->el
[i
], div
->el
[i
], div
->el
[0]);
1682 isl_int_fdiv_q(aff
->v
->el
[i
], aff
->v
->el
[i
], div
->el
[0]);
1683 if (isl_int_gt(div
->el
[i
], aff
->v
->el
[0])) {
1684 isl_int_sub(div
->el
[i
], div
->el
[i
], div
->el
[0]);
1685 isl_int_add_ui(aff
->v
->el
[i
], aff
->v
->el
[i
], 1);
1689 aff
->ls
= isl_local_space_add_div(aff
->ls
, div
);
1691 return isl_aff_free(aff
);
1693 size
= aff
->v
->size
;
1694 aff
->v
= isl_vec_extend(aff
->v
, size
+ 1);
1696 return isl_aff_free(aff
);
1697 isl_int_set_si(aff
->v
->el
[0], 1);
1698 isl_int_set_si(aff
->v
->el
[size
], 1);
1700 aff
= isl_aff_normalize(aff
);
1707 * aff mod m = aff - m * floor(aff/m)
1709 * with m an integer value.
1711 __isl_give isl_aff
*isl_aff_mod_val(__isl_take isl_aff
*aff
,
1712 __isl_take isl_val
*m
)
1719 if (!isl_val_is_int(m
))
1720 isl_die(isl_val_get_ctx(m
), isl_error_invalid
,
1721 "expecting integer modulo", goto error
);
1723 res
= isl_aff_copy(aff
);
1724 aff
= isl_aff_scale_down_val(aff
, isl_val_copy(m
));
1725 aff
= isl_aff_floor(aff
);
1726 aff
= isl_aff_scale_val(aff
, m
);
1727 res
= isl_aff_sub(res
, aff
);
1738 * pwaff mod m = pwaff - m * floor(pwaff/m)
1740 __isl_give isl_pw_aff
*isl_pw_aff_mod(__isl_take isl_pw_aff
*pwaff
, isl_int m
)
1744 res
= isl_pw_aff_copy(pwaff
);
1745 pwaff
= isl_pw_aff_scale_down(pwaff
, m
);
1746 pwaff
= isl_pw_aff_floor(pwaff
);
1747 pwaff
= isl_pw_aff_scale(pwaff
, m
);
1748 res
= isl_pw_aff_sub(res
, pwaff
);
1755 * pa mod m = pa - m * floor(pa/m)
1757 * with m an integer value.
1759 __isl_give isl_pw_aff
*isl_pw_aff_mod_val(__isl_take isl_pw_aff
*pa
,
1760 __isl_take isl_val
*m
)
1764 if (!isl_val_is_int(m
))
1765 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
1766 "expecting integer modulo", goto error
);
1767 pa
= isl_pw_aff_mod(pa
, m
->n
);
1771 isl_pw_aff_free(pa
);
1776 /* Given f, return ceil(f).
1777 * If f is an integer expression, then just return f.
1778 * Otherwise, let f be the expression
1784 * floor((e + m - 1)/m)
1786 * As a special case, ceil(NaN) = NaN.
1788 __isl_give isl_aff
*isl_aff_ceil(__isl_take isl_aff
*aff
)
1793 if (isl_aff_is_nan(aff
))
1795 if (isl_int_is_one(aff
->v
->el
[0]))
1798 aff
= isl_aff_cow(aff
);
1801 aff
->v
= isl_vec_cow(aff
->v
);
1803 return isl_aff_free(aff
);
1805 isl_int_add(aff
->v
->el
[1], aff
->v
->el
[1], aff
->v
->el
[0]);
1806 isl_int_sub_ui(aff
->v
->el
[1], aff
->v
->el
[1], 1);
1807 aff
= isl_aff_floor(aff
);
1812 /* Apply the expansion computed by isl_merge_divs.
1813 * The expansion itself is given by "exp" while the resulting
1814 * list of divs is given by "div".
1816 __isl_give isl_aff
*isl_aff_expand_divs(__isl_take isl_aff
*aff
,
1817 __isl_take isl_mat
*div
, int *exp
)
1823 aff
= isl_aff_cow(aff
);
1825 offset
= isl_aff_domain_offset(aff
, isl_dim_div
);
1826 old_n_div
= isl_aff_domain_dim(aff
, isl_dim_div
);
1827 new_n_div
= isl_mat_rows(div
);
1828 if (offset
< 0 || old_n_div
< 0 || new_n_div
< 0)
1831 aff
->v
= isl_vec_expand(aff
->v
, 1 + offset
, old_n_div
, exp
, new_n_div
);
1832 aff
->ls
= isl_local_space_replace_divs(aff
->ls
, div
);
1833 if (!aff
->v
|| !aff
->ls
)
1834 return isl_aff_free(aff
);
1842 /* Add two affine expressions that live in the same local space.
1844 static __isl_give isl_aff
*add_expanded(__isl_take isl_aff
*aff1
,
1845 __isl_take isl_aff
*aff2
)
1849 aff1
= isl_aff_cow(aff1
);
1853 aff1
->v
= isl_vec_cow(aff1
->v
);
1859 isl_int_gcd(gcd
, aff1
->v
->el
[0], aff2
->v
->el
[0]);
1860 isl_int_divexact(f
, aff2
->v
->el
[0], gcd
);
1861 isl_seq_scale(aff1
->v
->el
+ 1, aff1
->v
->el
+ 1, f
, aff1
->v
->size
- 1);
1862 isl_int_divexact(f
, aff1
->v
->el
[0], gcd
);
1863 isl_seq_addmul(aff1
->v
->el
+ 1, f
, aff2
->v
->el
+ 1, aff1
->v
->size
- 1);
1864 isl_int_divexact(f
, aff2
->v
->el
[0], gcd
);
1865 isl_int_mul(aff1
->v
->el
[0], aff1
->v
->el
[0], f
);
1870 aff1
= isl_aff_normalize(aff1
);
1878 /* Replace one of the arguments by a NaN and free the other one.
1880 static __isl_give isl_aff
*set_nan_free(__isl_take isl_aff
*aff1
,
1881 __isl_take isl_aff
*aff2
)
1884 return isl_aff_set_nan(aff1
);
1887 /* Return the sum of "aff1" and "aff2".
1889 * If either of the two is NaN, then the result is NaN.
1891 __isl_give isl_aff
*isl_aff_add(__isl_take isl_aff
*aff1
,
1892 __isl_take isl_aff
*aff2
)
1898 isl_size n_div1
, n_div2
;
1903 ctx
= isl_aff_get_ctx(aff1
);
1904 if (!isl_space_is_equal(aff1
->ls
->dim
, aff2
->ls
->dim
))
1905 isl_die(ctx
, isl_error_invalid
,
1906 "spaces don't match", goto error
);
1908 if (isl_aff_is_nan(aff1
)) {
1912 if (isl_aff_is_nan(aff2
)) {
1917 n_div1
= isl_aff_dim(aff1
, isl_dim_div
);
1918 n_div2
= isl_aff_dim(aff2
, isl_dim_div
);
1919 if (n_div1
< 0 || n_div2
< 0)
1921 if (n_div1
== 0 && n_div2
== 0)
1922 return add_expanded(aff1
, aff2
);
1924 exp1
= isl_alloc_array(ctx
, int, n_div1
);
1925 exp2
= isl_alloc_array(ctx
, int, n_div2
);
1926 if ((n_div1
&& !exp1
) || (n_div2
&& !exp2
))
1929 div
= isl_merge_divs(aff1
->ls
->div
, aff2
->ls
->div
, exp1
, exp2
);
1930 aff1
= isl_aff_expand_divs(aff1
, isl_mat_copy(div
), exp1
);
1931 aff2
= isl_aff_expand_divs(aff2
, div
, exp2
);
1935 return add_expanded(aff1
, aff2
);
1944 __isl_give isl_aff
*isl_aff_sub(__isl_take isl_aff
*aff1
,
1945 __isl_take isl_aff
*aff2
)
1947 return isl_aff_add(aff1
, isl_aff_neg(aff2
));
1950 /* Return the result of scaling "aff" by a factor of "f".
1952 * As a special case, f * NaN = NaN.
1954 __isl_give isl_aff
*isl_aff_scale(__isl_take isl_aff
*aff
, isl_int f
)
1960 if (isl_aff_is_nan(aff
))
1963 if (isl_int_is_one(f
))
1966 aff
= isl_aff_cow(aff
);
1969 aff
->v
= isl_vec_cow(aff
->v
);
1971 return isl_aff_free(aff
);
1973 if (isl_int_is_pos(f
) && isl_int_is_divisible_by(aff
->v
->el
[0], f
)) {
1974 isl_int_divexact(aff
->v
->el
[0], aff
->v
->el
[0], f
);
1979 isl_int_gcd(gcd
, aff
->v
->el
[0], f
);
1980 isl_int_divexact(aff
->v
->el
[0], aff
->v
->el
[0], gcd
);
1981 isl_int_divexact(gcd
, f
, gcd
);
1982 isl_seq_scale(aff
->v
->el
+ 1, aff
->v
->el
+ 1, gcd
, aff
->v
->size
- 1);
1988 /* Multiple "aff" by "v".
1990 __isl_give isl_aff
*isl_aff_scale_val(__isl_take isl_aff
*aff
,
1991 __isl_take isl_val
*v
)
1996 if (isl_val_is_one(v
)) {
2001 if (!isl_val_is_rat(v
))
2002 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2003 "expecting rational factor", goto error
);
2005 aff
= isl_aff_scale(aff
, v
->n
);
2006 aff
= isl_aff_scale_down(aff
, v
->d
);
2016 /* Return the result of scaling "aff" down by a factor of "f".
2018 * As a special case, NaN/f = NaN.
2020 __isl_give isl_aff
*isl_aff_scale_down(__isl_take isl_aff
*aff
, isl_int f
)
2026 if (isl_aff_is_nan(aff
))
2029 if (isl_int_is_one(f
))
2032 aff
= isl_aff_cow(aff
);
2036 if (isl_int_is_zero(f
))
2037 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2038 "cannot scale down by zero", return isl_aff_free(aff
));
2040 aff
->v
= isl_vec_cow(aff
->v
);
2042 return isl_aff_free(aff
);
2045 isl_seq_gcd(aff
->v
->el
+ 1, aff
->v
->size
- 1, &gcd
);
2046 isl_int_gcd(gcd
, gcd
, f
);
2047 isl_seq_scale_down(aff
->v
->el
+ 1, aff
->v
->el
+ 1, gcd
, aff
->v
->size
- 1);
2048 isl_int_divexact(gcd
, f
, gcd
);
2049 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], gcd
);
2055 /* Divide "aff" by "v".
2057 __isl_give isl_aff
*isl_aff_scale_down_val(__isl_take isl_aff
*aff
,
2058 __isl_take isl_val
*v
)
2063 if (isl_val_is_one(v
)) {
2068 if (!isl_val_is_rat(v
))
2069 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2070 "expecting rational factor", goto error
);
2071 if (!isl_val_is_pos(v
))
2072 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2073 "factor needs to be positive", goto error
);
2075 aff
= isl_aff_scale(aff
, v
->d
);
2076 aff
= isl_aff_scale_down(aff
, v
->n
);
2086 __isl_give isl_aff
*isl_aff_scale_down_ui(__isl_take isl_aff
*aff
, unsigned f
)
2094 isl_int_set_ui(v
, f
);
2095 aff
= isl_aff_scale_down(aff
, v
);
2101 __isl_give isl_aff
*isl_aff_set_dim_name(__isl_take isl_aff
*aff
,
2102 enum isl_dim_type type
, unsigned pos
, const char *s
)
2104 aff
= isl_aff_cow(aff
);
2107 if (type
== isl_dim_out
)
2108 isl_die(aff
->v
->ctx
, isl_error_invalid
,
2109 "cannot set name of output/set dimension",
2110 return isl_aff_free(aff
));
2111 if (type
== isl_dim_in
)
2113 aff
->ls
= isl_local_space_set_dim_name(aff
->ls
, type
, pos
, s
);
2115 return isl_aff_free(aff
);
2120 __isl_give isl_aff
*isl_aff_set_dim_id(__isl_take isl_aff
*aff
,
2121 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
2123 aff
= isl_aff_cow(aff
);
2126 if (type
== isl_dim_out
)
2127 isl_die(aff
->v
->ctx
, isl_error_invalid
,
2128 "cannot set name of output/set dimension",
2130 if (type
== isl_dim_in
)
2132 aff
->ls
= isl_local_space_set_dim_id(aff
->ls
, type
, pos
, id
);
2134 return isl_aff_free(aff
);
2143 /* Replace the identifier of the input tuple of "aff" by "id".
2144 * type is currently required to be equal to isl_dim_in
2146 __isl_give isl_aff
*isl_aff_set_tuple_id(__isl_take isl_aff
*aff
,
2147 enum isl_dim_type type
, __isl_take isl_id
*id
)
2149 aff
= isl_aff_cow(aff
);
2152 if (type
!= isl_dim_in
)
2153 isl_die(aff
->v
->ctx
, isl_error_invalid
,
2154 "cannot only set id of input tuple", goto error
);
2155 aff
->ls
= isl_local_space_set_tuple_id(aff
->ls
, isl_dim_set
, id
);
2157 return isl_aff_free(aff
);
2166 /* Exploit the equalities in "eq" to simplify the affine expression
2167 * and the expressions of the integer divisions in the local space.
2168 * The integer divisions in this local space are assumed to appear
2169 * as regular dimensions in "eq".
2171 static __isl_give isl_aff
*isl_aff_substitute_equalities_lifted(
2172 __isl_take isl_aff
*aff
, __isl_take isl_basic_set
*eq
)
2180 if (eq
->n_eq
== 0) {
2181 isl_basic_set_free(eq
);
2185 aff
= isl_aff_cow(aff
);
2189 aff
->ls
= isl_local_space_substitute_equalities(aff
->ls
,
2190 isl_basic_set_copy(eq
));
2191 aff
->v
= isl_vec_cow(aff
->v
);
2192 if (!aff
->ls
|| !aff
->v
)
2195 o_div
= isl_basic_set_offset(eq
, isl_dim_div
);
2197 for (i
= 0; i
< eq
->n_eq
; ++i
) {
2198 j
= isl_seq_last_non_zero(eq
->eq
[i
], o_div
+ n_div
);
2199 if (j
< 0 || j
== 0 || j
>= o_div
)
2202 isl_seq_elim(aff
->v
->el
+ 1, eq
->eq
[i
], j
, o_div
,
2206 isl_basic_set_free(eq
);
2207 aff
= isl_aff_normalize(aff
);
2210 isl_basic_set_free(eq
);
2215 /* Exploit the equalities in "eq" to simplify the affine expression
2216 * and the expressions of the integer divisions in the local space.
2218 __isl_give isl_aff
*isl_aff_substitute_equalities(__isl_take isl_aff
*aff
,
2219 __isl_take isl_basic_set
*eq
)
2223 n_div
= isl_aff_domain_dim(aff
, isl_dim_div
);
2227 eq
= isl_basic_set_add_dims(eq
, isl_dim_set
, n_div
);
2228 return isl_aff_substitute_equalities_lifted(aff
, eq
);
2230 isl_basic_set_free(eq
);
2235 /* Look for equalities among the variables shared by context and aff
2236 * and the integer divisions of aff, if any.
2237 * The equalities are then used to eliminate coefficients and/or integer
2238 * divisions from aff.
2240 __isl_give isl_aff
*isl_aff_gist(__isl_take isl_aff
*aff
,
2241 __isl_take isl_set
*context
)
2243 isl_local_space
*ls
;
2244 isl_basic_set
*hull
;
2246 ls
= isl_aff_get_domain_local_space(aff
);
2247 context
= isl_local_space_lift_set(ls
, context
);
2249 hull
= isl_set_affine_hull(context
);
2250 return isl_aff_substitute_equalities_lifted(aff
, hull
);
2253 __isl_give isl_aff
*isl_aff_gist_params(__isl_take isl_aff
*aff
,
2254 __isl_take isl_set
*context
)
2256 isl_set
*dom_context
= isl_set_universe(isl_aff_get_domain_space(aff
));
2257 dom_context
= isl_set_intersect_params(dom_context
, context
);
2258 return isl_aff_gist(aff
, dom_context
);
2261 /* Return a basic set containing those elements in the space
2262 * of aff where it is positive. "rational" should not be set.
2264 * If "aff" is NaN, then it is not positive.
2266 static __isl_give isl_basic_set
*aff_pos_basic_set(__isl_take isl_aff
*aff
,
2267 int rational
, void *user
)
2269 isl_constraint
*ineq
;
2270 isl_basic_set
*bset
;
2275 if (isl_aff_is_nan(aff
)) {
2276 isl_space
*space
= isl_aff_get_domain_space(aff
);
2278 return isl_basic_set_empty(space
);
2281 isl_die(isl_aff_get_ctx(aff
), isl_error_unsupported
,
2282 "rational sets not supported", goto error
);
2284 ineq
= isl_inequality_from_aff(aff
);
2285 c
= isl_constraint_get_constant_val(ineq
);
2286 c
= isl_val_sub_ui(c
, 1);
2287 ineq
= isl_constraint_set_constant_val(ineq
, c
);
2289 bset
= isl_basic_set_from_constraint(ineq
);
2290 bset
= isl_basic_set_simplify(bset
);
2297 /* Return a basic set containing those elements in the space
2298 * of aff where it is non-negative.
2299 * If "rational" is set, then return a rational basic set.
2301 * If "aff" is NaN, then it is not non-negative (it's not negative either).
2303 static __isl_give isl_basic_set
*aff_nonneg_basic_set(
2304 __isl_take isl_aff
*aff
, int rational
, void *user
)
2306 isl_constraint
*ineq
;
2307 isl_basic_set
*bset
;
2311 if (isl_aff_is_nan(aff
)) {
2312 isl_space
*space
= isl_aff_get_domain_space(aff
);
2314 return isl_basic_set_empty(space
);
2317 ineq
= isl_inequality_from_aff(aff
);
2319 bset
= isl_basic_set_from_constraint(ineq
);
2321 bset
= isl_basic_set_set_rational(bset
);
2322 bset
= isl_basic_set_simplify(bset
);
2326 /* Return a basic set containing those elements in the space
2327 * of aff where it is non-negative.
2329 __isl_give isl_basic_set
*isl_aff_nonneg_basic_set(__isl_take isl_aff
*aff
)
2331 return aff_nonneg_basic_set(aff
, 0, NULL
);
2334 /* Return a basic set containing those elements in the domain space
2335 * of "aff" where it is positive.
2337 __isl_give isl_basic_set
*isl_aff_pos_basic_set(__isl_take isl_aff
*aff
)
2339 aff
= isl_aff_add_constant_num_si(aff
, -1);
2340 return isl_aff_nonneg_basic_set(aff
);
2343 /* Return a basic set containing those elements in the domain space
2344 * of aff where it is negative.
2346 __isl_give isl_basic_set
*isl_aff_neg_basic_set(__isl_take isl_aff
*aff
)
2348 aff
= isl_aff_neg(aff
);
2349 return isl_aff_pos_basic_set(aff
);
2352 /* Return a basic set containing those elements in the space
2353 * of aff where it is zero.
2354 * If "rational" is set, then return a rational basic set.
2356 * If "aff" is NaN, then it is not zero.
2358 static __isl_give isl_basic_set
*aff_zero_basic_set(__isl_take isl_aff
*aff
,
2359 int rational
, void *user
)
2361 isl_constraint
*ineq
;
2362 isl_basic_set
*bset
;
2366 if (isl_aff_is_nan(aff
)) {
2367 isl_space
*space
= isl_aff_get_domain_space(aff
);
2369 return isl_basic_set_empty(space
);
2372 ineq
= isl_equality_from_aff(aff
);
2374 bset
= isl_basic_set_from_constraint(ineq
);
2376 bset
= isl_basic_set_set_rational(bset
);
2377 bset
= isl_basic_set_simplify(bset
);
2381 /* Return a basic set containing those elements in the space
2382 * of aff where it is zero.
2384 __isl_give isl_basic_set
*isl_aff_zero_basic_set(__isl_take isl_aff
*aff
)
2386 return aff_zero_basic_set(aff
, 0, NULL
);
2389 /* Return a basic set containing those elements in the shared space
2390 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
2392 __isl_give isl_basic_set
*isl_aff_ge_basic_set(__isl_take isl_aff
*aff1
,
2393 __isl_take isl_aff
*aff2
)
2395 aff1
= isl_aff_sub(aff1
, aff2
);
2397 return isl_aff_nonneg_basic_set(aff1
);
2400 /* Return a basic set containing those elements in the shared domain space
2401 * of "aff1" and "aff2" where "aff1" is greater than "aff2".
2403 __isl_give isl_basic_set
*isl_aff_gt_basic_set(__isl_take isl_aff
*aff1
,
2404 __isl_take isl_aff
*aff2
)
2406 aff1
= isl_aff_sub(aff1
, aff2
);
2408 return isl_aff_pos_basic_set(aff1
);
2411 /* Return a set containing those elements in the shared space
2412 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
2414 __isl_give isl_set
*isl_aff_ge_set(__isl_take isl_aff
*aff1
,
2415 __isl_take isl_aff
*aff2
)
2417 return isl_set_from_basic_set(isl_aff_ge_basic_set(aff1
, aff2
));
2420 /* Return a set containing those elements in the shared domain space
2421 * of aff1 and aff2 where aff1 is greater than aff2.
2423 * If either of the two inputs is NaN, then the result is empty,
2424 * as comparisons with NaN always return false.
2426 __isl_give isl_set
*isl_aff_gt_set(__isl_take isl_aff
*aff1
,
2427 __isl_take isl_aff
*aff2
)
2429 return isl_set_from_basic_set(isl_aff_gt_basic_set(aff1
, aff2
));
2432 /* Return a basic set containing those elements in the shared space
2433 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
2435 __isl_give isl_basic_set
*isl_aff_le_basic_set(__isl_take isl_aff
*aff1
,
2436 __isl_take isl_aff
*aff2
)
2438 return isl_aff_ge_basic_set(aff2
, aff1
);
2441 /* Return a basic set containing those elements in the shared domain space
2442 * of "aff1" and "aff2" where "aff1" is smaller than "aff2".
2444 __isl_give isl_basic_set
*isl_aff_lt_basic_set(__isl_take isl_aff
*aff1
,
2445 __isl_take isl_aff
*aff2
)
2447 return isl_aff_gt_basic_set(aff2
, aff1
);
2450 /* Return a set containing those elements in the shared space
2451 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
2453 __isl_give isl_set
*isl_aff_le_set(__isl_take isl_aff
*aff1
,
2454 __isl_take isl_aff
*aff2
)
2456 return isl_aff_ge_set(aff2
, aff1
);
2459 /* Return a set containing those elements in the shared domain space
2460 * of "aff1" and "aff2" where "aff1" is smaller than "aff2".
2462 __isl_give isl_set
*isl_aff_lt_set(__isl_take isl_aff
*aff1
,
2463 __isl_take isl_aff
*aff2
)
2465 return isl_set_from_basic_set(isl_aff_lt_basic_set(aff1
, aff2
));
2468 /* Return a basic set containing those elements in the shared space
2469 * of aff1 and aff2 where aff1 and aff2 are equal.
2471 __isl_give isl_basic_set
*isl_aff_eq_basic_set(__isl_take isl_aff
*aff1
,
2472 __isl_take isl_aff
*aff2
)
2474 aff1
= isl_aff_sub(aff1
, aff2
);
2476 return isl_aff_zero_basic_set(aff1
);
2479 /* Return a set containing those elements in the shared space
2480 * of aff1 and aff2 where aff1 and aff2 are equal.
2482 __isl_give isl_set
*isl_aff_eq_set(__isl_take isl_aff
*aff1
,
2483 __isl_take isl_aff
*aff2
)
2485 return isl_set_from_basic_set(isl_aff_eq_basic_set(aff1
, aff2
));
2488 /* Return a set containing those elements in the shared domain space
2489 * of aff1 and aff2 where aff1 and aff2 are not equal.
2491 * If either of the two inputs is NaN, then the result is empty,
2492 * as comparisons with NaN always return false.
2494 __isl_give isl_set
*isl_aff_ne_set(__isl_take isl_aff
*aff1
,
2495 __isl_take isl_aff
*aff2
)
2497 isl_set
*set_lt
, *set_gt
;
2499 set_lt
= isl_aff_lt_set(isl_aff_copy(aff1
),
2500 isl_aff_copy(aff2
));
2501 set_gt
= isl_aff_gt_set(aff1
, aff2
);
2502 return isl_set_union_disjoint(set_lt
, set_gt
);
2505 __isl_give isl_aff
*isl_aff_add_on_domain(__isl_keep isl_set
*dom
,
2506 __isl_take isl_aff
*aff1
, __isl_take isl_aff
*aff2
)
2508 aff1
= isl_aff_add(aff1
, aff2
);
2509 aff1
= isl_aff_gist(aff1
, isl_set_copy(dom
));
2513 isl_bool
isl_aff_is_empty(__isl_keep isl_aff
*aff
)
2516 return isl_bool_error
;
2518 return isl_bool_false
;
2522 #define TYPE isl_aff
2524 #include "check_type_range_templ.c"
2526 /* Check whether the given affine expression has non-zero coefficient
2527 * for any dimension in the given range or if any of these dimensions
2528 * appear with non-zero coefficients in any of the integer divisions
2529 * involved in the affine expression.
2531 isl_bool
isl_aff_involves_dims(__isl_keep isl_aff
*aff
,
2532 enum isl_dim_type type
, unsigned first
, unsigned n
)
2536 isl_bool involves
= isl_bool_false
;
2539 return isl_bool_error
;
2541 return isl_bool_false
;
2542 if (isl_aff_check_range(aff
, type
, first
, n
) < 0)
2543 return isl_bool_error
;
2545 active
= isl_local_space_get_active(aff
->ls
, aff
->v
->el
+ 2);
2549 first
+= isl_local_space_offset(aff
->ls
, type
) - 1;
2550 for (i
= 0; i
< n
; ++i
)
2551 if (active
[first
+ i
]) {
2552 involves
= isl_bool_true
;
2561 return isl_bool_error
;
2564 /* Does "aff" involve any local variables, i.e., integer divisions?
2566 isl_bool
isl_aff_involves_locals(__isl_keep isl_aff
*aff
)
2570 n
= isl_aff_dim(aff
, isl_dim_div
);
2572 return isl_bool_error
;
2573 return isl_bool_ok(n
> 0);
2576 __isl_give isl_aff
*isl_aff_drop_dims(__isl_take isl_aff
*aff
,
2577 enum isl_dim_type type
, unsigned first
, unsigned n
)
2583 if (type
== isl_dim_out
)
2584 isl_die(aff
->v
->ctx
, isl_error_invalid
,
2585 "cannot drop output/set dimension",
2586 return isl_aff_free(aff
));
2587 if (type
== isl_dim_in
)
2589 if (n
== 0 && !isl_local_space_is_named_or_nested(aff
->ls
, type
))
2592 ctx
= isl_aff_get_ctx(aff
);
2593 if (isl_local_space_check_range(aff
->ls
, type
, first
, n
) < 0)
2594 return isl_aff_free(aff
);
2596 aff
= isl_aff_cow(aff
);
2600 aff
->ls
= isl_local_space_drop_dims(aff
->ls
, type
, first
, n
);
2602 return isl_aff_free(aff
);
2604 first
+= 1 + isl_local_space_offset(aff
->ls
, type
);
2605 aff
->v
= isl_vec_drop_els(aff
->v
, first
, n
);
2607 return isl_aff_free(aff
);
2612 /* Is the domain of "aff" a product?
2614 static isl_bool
isl_aff_domain_is_product(__isl_keep isl_aff
*aff
)
2616 return isl_space_is_product(isl_aff_peek_domain_space(aff
));
2620 #define TYPE isl_aff
2621 #include <isl_domain_factor_templ.c>
2623 /* Project the domain of the affine expression onto its parameter space.
2624 * The affine expression may not involve any of the domain dimensions.
2626 __isl_give isl_aff
*isl_aff_project_domain_on_params(__isl_take isl_aff
*aff
)
2631 n
= isl_aff_dim(aff
, isl_dim_in
);
2633 return isl_aff_free(aff
);
2634 aff
= isl_aff_drop_domain(aff
, 0, n
);
2635 space
= isl_aff_get_domain_space(aff
);
2636 space
= isl_space_params(space
);
2637 aff
= isl_aff_reset_domain_space(aff
, space
);
2641 /* Convert an affine expression defined over a parameter domain
2642 * into one that is defined over a zero-dimensional set.
2644 __isl_give isl_aff
*isl_aff_from_range(__isl_take isl_aff
*aff
)
2646 isl_local_space
*ls
;
2648 ls
= isl_aff_take_domain_local_space(aff
);
2649 ls
= isl_local_space_set_from_params(ls
);
2650 aff
= isl_aff_restore_domain_local_space(aff
, ls
);
2655 __isl_give isl_aff
*isl_aff_insert_dims(__isl_take isl_aff
*aff
,
2656 enum isl_dim_type type
, unsigned first
, unsigned n
)
2662 if (type
== isl_dim_out
)
2663 isl_die(aff
->v
->ctx
, isl_error_invalid
,
2664 "cannot insert output/set dimensions",
2665 return isl_aff_free(aff
));
2666 if (type
== isl_dim_in
)
2668 if (n
== 0 && !isl_local_space_is_named_or_nested(aff
->ls
, type
))
2671 ctx
= isl_aff_get_ctx(aff
);
2672 if (isl_local_space_check_range(aff
->ls
, type
, first
, 0) < 0)
2673 return isl_aff_free(aff
);
2675 aff
= isl_aff_cow(aff
);
2679 aff
->ls
= isl_local_space_insert_dims(aff
->ls
, type
, first
, n
);
2681 return isl_aff_free(aff
);
2683 first
+= 1 + isl_local_space_offset(aff
->ls
, type
);
2684 aff
->v
= isl_vec_insert_zero_els(aff
->v
, first
, n
);
2686 return isl_aff_free(aff
);
2691 __isl_give isl_aff
*isl_aff_add_dims(__isl_take isl_aff
*aff
,
2692 enum isl_dim_type type
, unsigned n
)
2696 pos
= isl_aff_dim(aff
, type
);
2698 return isl_aff_free(aff
);
2700 return isl_aff_insert_dims(aff
, type
, pos
, n
);
2703 /* Move the "n" dimensions of "src_type" starting at "src_pos" of "aff"
2704 * to dimensions of "dst_type" at "dst_pos".
2706 * We only support moving input dimensions to parameters and vice versa.
2708 __isl_give isl_aff
*isl_aff_move_dims(__isl_take isl_aff
*aff
,
2709 enum isl_dim_type dst_type
, unsigned dst_pos
,
2710 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
2714 isl_size src_off
, dst_off
;
2719 !isl_local_space_is_named_or_nested(aff
->ls
, src_type
) &&
2720 !isl_local_space_is_named_or_nested(aff
->ls
, dst_type
))
2723 if (dst_type
== isl_dim_out
|| src_type
== isl_dim_out
)
2724 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2725 "cannot move output/set dimension",
2726 return isl_aff_free(aff
));
2727 if (dst_type
== isl_dim_div
|| src_type
== isl_dim_div
)
2728 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2729 "cannot move divs", return isl_aff_free(aff
));
2730 if (dst_type
== isl_dim_in
)
2731 dst_type
= isl_dim_set
;
2732 if (src_type
== isl_dim_in
)
2733 src_type
= isl_dim_set
;
2735 if (isl_local_space_check_range(aff
->ls
, src_type
, src_pos
, n
) < 0)
2736 return isl_aff_free(aff
);
2737 if (dst_type
== src_type
)
2738 isl_die(isl_aff_get_ctx(aff
), isl_error_unsupported
,
2739 "moving dims within the same type not supported",
2740 return isl_aff_free(aff
));
2742 aff
= isl_aff_cow(aff
);
2743 src_off
= isl_aff_domain_offset(aff
, src_type
);
2744 dst_off
= isl_aff_domain_offset(aff
, dst_type
);
2745 if (src_off
< 0 || dst_off
< 0)
2746 return isl_aff_free(aff
);
2748 g_src_pos
= 1 + src_off
+ src_pos
;
2749 g_dst_pos
= 1 + dst_off
+ dst_pos
;
2750 if (dst_type
> src_type
)
2753 aff
->v
= isl_vec_move_els(aff
->v
, g_dst_pos
, g_src_pos
, n
);
2754 aff
->ls
= isl_local_space_move_dims(aff
->ls
, dst_type
, dst_pos
,
2755 src_type
, src_pos
, n
);
2756 if (!aff
->v
|| !aff
->ls
)
2757 return isl_aff_free(aff
);
2759 aff
= sort_divs(aff
);
2764 /* Return a zero isl_aff in the given space.
2766 * This is a helper function for isl_pw_*_as_* that ensures a uniform
2767 * interface over all piecewise types.
2769 static __isl_give isl_aff
*isl_aff_zero_in_space(__isl_take isl_space
*space
)
2771 isl_local_space
*ls
;
2773 ls
= isl_local_space_from_space(isl_space_domain(space
));
2774 return isl_aff_zero_on_domain(ls
);
2777 #define isl_aff_involves_nan isl_aff_is_nan
2780 #define PW isl_pw_aff
2784 #define EL_IS_ZERO is_empty
2788 #define IS_ZERO is_empty
2791 #undef DEFAULT_IS_ZERO
2792 #define DEFAULT_IS_ZERO 0
2794 #include <isl_pw_templ.c>
2795 #include <isl_pw_add_constant_val_templ.c>
2796 #include <isl_pw_bind_domain_templ.c>
2797 #include <isl_pw_eval.c>
2798 #include <isl_pw_hash.c>
2799 #include <isl_pw_insert_dims_templ.c>
2800 #include <isl_pw_insert_domain_templ.c>
2801 #include <isl_pw_move_dims_templ.c>
2802 #include <isl_pw_neg_templ.c>
2803 #include <isl_pw_pullback_templ.c>
2804 #include <isl_pw_sub_templ.c>
2805 #include <isl_pw_union_opt.c>
2810 #include <isl_union_single.c>
2811 #include <isl_union_neg.c>
2816 #include <isl_union_pw_templ.c>
2818 /* Compute a piecewise quasi-affine expression with a domain that
2819 * is the union of those of pwaff1 and pwaff2 and such that on each
2820 * cell, the quasi-affine expression is the maximum of those of pwaff1
2821 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2822 * cell, then the associated expression is the defined one.
2824 __isl_give isl_pw_aff
*isl_pw_aff_union_max(__isl_take isl_pw_aff
*pwaff1
,
2825 __isl_take isl_pw_aff
*pwaff2
)
2827 isl_pw_aff_align_params_bin(&pwaff1
, &pwaff2
);
2828 return isl_pw_aff_union_opt_cmp(pwaff1
, pwaff2
, &isl_aff_ge_set
);
2831 /* Compute a piecewise quasi-affine expression with a domain that
2832 * is the union of those of pwaff1 and pwaff2 and such that on each
2833 * cell, the quasi-affine expression is the minimum of those of pwaff1
2834 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2835 * cell, then the associated expression is the defined one.
2837 __isl_give isl_pw_aff
*isl_pw_aff_union_min(__isl_take isl_pw_aff
*pwaff1
,
2838 __isl_take isl_pw_aff
*pwaff2
)
2840 isl_pw_aff_align_params_bin(&pwaff1
, &pwaff2
);
2841 return isl_pw_aff_union_opt_cmp(pwaff1
, pwaff2
, &isl_aff_le_set
);
2844 __isl_give isl_pw_aff
*isl_pw_aff_union_opt(__isl_take isl_pw_aff
*pwaff1
,
2845 __isl_take isl_pw_aff
*pwaff2
, int max
)
2848 return isl_pw_aff_union_max(pwaff1
, pwaff2
);
2850 return isl_pw_aff_union_min(pwaff1
, pwaff2
);
2853 /* Is the domain of "pa" a product?
2855 static isl_bool
isl_pw_aff_domain_is_product(__isl_keep isl_pw_aff
*pa
)
2857 return isl_space_domain_is_wrapping(isl_pw_aff_peek_space(pa
));
2861 #define TYPE isl_pw_aff
2862 #include <isl_domain_factor_templ.c>
2864 /* Return a set containing those elements in the domain
2865 * of "pwaff" where it satisfies "fn" (if complement is 0) or
2866 * does not satisfy "fn" (if complement is 1).
2868 * The pieces with a NaN never belong to the result since
2869 * NaN does not satisfy any property.
2871 static __isl_give isl_set
*pw_aff_locus(__isl_take isl_pw_aff
*pwaff
,
2872 __isl_give isl_basic_set
*(*fn
)(__isl_take isl_aff
*aff
, int rational
,
2874 int complement
, void *user
)
2882 set
= isl_set_empty(isl_pw_aff_get_domain_space(pwaff
));
2884 for (i
= 0; i
< pwaff
->n
; ++i
) {
2885 isl_basic_set
*bset
;
2886 isl_set
*set_i
, *locus
;
2889 if (isl_aff_is_nan(pwaff
->p
[i
].aff
))
2892 rational
= isl_set_has_rational(pwaff
->p
[i
].set
);
2893 bset
= fn(isl_aff_copy(pwaff
->p
[i
].aff
), rational
, user
);
2894 locus
= isl_set_from_basic_set(bset
);
2895 set_i
= isl_set_copy(pwaff
->p
[i
].set
);
2897 set_i
= isl_set_subtract(set_i
, locus
);
2899 set_i
= isl_set_intersect(set_i
, locus
);
2900 set
= isl_set_union_disjoint(set
, set_i
);
2903 isl_pw_aff_free(pwaff
);
2908 /* Return a set containing those elements in the domain
2909 * of "pa" where it is positive.
2911 __isl_give isl_set
*isl_pw_aff_pos_set(__isl_take isl_pw_aff
*pa
)
2913 return pw_aff_locus(pa
, &aff_pos_basic_set
, 0, NULL
);
2916 /* Return a set containing those elements in the domain
2917 * of pwaff where it is non-negative.
2919 __isl_give isl_set
*isl_pw_aff_nonneg_set(__isl_take isl_pw_aff
*pwaff
)
2921 return pw_aff_locus(pwaff
, &aff_nonneg_basic_set
, 0, NULL
);
2924 /* Return a set containing those elements in the domain
2925 * of pwaff where it is zero.
2927 __isl_give isl_set
*isl_pw_aff_zero_set(__isl_take isl_pw_aff
*pwaff
)
2929 return pw_aff_locus(pwaff
, &aff_zero_basic_set
, 0, NULL
);
2932 /* Return a set containing those elements in the domain
2933 * of pwaff where it is not zero.
2935 __isl_give isl_set
*isl_pw_aff_non_zero_set(__isl_take isl_pw_aff
*pwaff
)
2937 return pw_aff_locus(pwaff
, &aff_zero_basic_set
, 1, NULL
);
2940 /* Bind the affine function "aff" to the parameter "id",
2941 * returning the elements in the domain where the affine expression
2942 * is equal to the parameter.
2944 __isl_give isl_basic_set
*isl_aff_bind_id(__isl_take isl_aff
*aff
,
2945 __isl_take isl_id
*id
)
2950 space
= isl_aff_get_domain_space(aff
);
2951 space
= isl_space_add_param_id(space
, isl_id_copy(id
));
2953 aff
= isl_aff_align_params(aff
, isl_space_copy(space
));
2954 aff_id
= isl_aff_param_on_domain_space_id(space
, id
);
2956 return isl_aff_eq_basic_set(aff
, aff_id
);
2959 /* Wrapper around isl_aff_bind_id for use as pw_aff_locus callback.
2960 * "rational" should not be set.
2962 static __isl_give isl_basic_set
*aff_bind_id(__isl_take isl_aff
*aff
,
2963 int rational
, void *user
)
2970 isl_die(isl_aff_get_ctx(aff
), isl_error_unsupported
,
2971 "rational binding not supported", goto error
);
2972 return isl_aff_bind_id(aff
, isl_id_copy(id
));
2978 /* Bind the piecewise affine function "pa" to the parameter "id",
2979 * returning the elements in the domain where the expression
2980 * is equal to the parameter.
2982 __isl_give isl_set
*isl_pw_aff_bind_id(__isl_take isl_pw_aff
*pa
,
2983 __isl_take isl_id
*id
)
2987 bound
= pw_aff_locus(pa
, &aff_bind_id
, 0, id
);
2993 /* Return a set containing those elements in the shared domain
2994 * of pwaff1 and pwaff2 where pwaff1 is greater than (or equal) to pwaff2.
2996 * We compute the difference on the shared domain and then construct
2997 * the set of values where this difference is non-negative.
2998 * If strict is set, we first subtract 1 from the difference.
2999 * If equal is set, we only return the elements where pwaff1 and pwaff2
3002 static __isl_give isl_set
*pw_aff_gte_set(__isl_take isl_pw_aff
*pwaff1
,
3003 __isl_take isl_pw_aff
*pwaff2
, int strict
, int equal
)
3005 isl_set
*set1
, *set2
;
3007 set1
= isl_pw_aff_domain(isl_pw_aff_copy(pwaff1
));
3008 set2
= isl_pw_aff_domain(isl_pw_aff_copy(pwaff2
));
3009 set1
= isl_set_intersect(set1
, set2
);
3010 pwaff1
= isl_pw_aff_intersect_domain(pwaff1
, isl_set_copy(set1
));
3011 pwaff2
= isl_pw_aff_intersect_domain(pwaff2
, isl_set_copy(set1
));
3012 pwaff1
= isl_pw_aff_add(pwaff1
, isl_pw_aff_neg(pwaff2
));
3015 isl_space
*space
= isl_set_get_space(set1
);
3017 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(space
));
3018 aff
= isl_aff_add_constant_si(aff
, -1);
3019 pwaff1
= isl_pw_aff_add(pwaff1
, isl_pw_aff_alloc(set1
, aff
));
3024 return isl_pw_aff_zero_set(pwaff1
);
3025 return isl_pw_aff_nonneg_set(pwaff1
);
3028 /* Return a set containing those elements in the shared domain
3029 * of pwaff1 and pwaff2 where pwaff1 is equal to pwaff2.
3031 __isl_give isl_set
*isl_pw_aff_eq_set(__isl_take isl_pw_aff
*pwaff1
,
3032 __isl_take isl_pw_aff
*pwaff2
)
3034 isl_pw_aff_align_params_bin(&pwaff1
, &pwaff2
);
3035 return pw_aff_gte_set(pwaff1
, pwaff2
, 0, 1);
3038 /* Return a set containing those elements in the shared domain
3039 * of pwaff1 and pwaff2 where pwaff1 is greater than or equal to pwaff2.
3041 __isl_give isl_set
*isl_pw_aff_ge_set(__isl_take isl_pw_aff
*pwaff1
,
3042 __isl_take isl_pw_aff
*pwaff2
)
3044 isl_pw_aff_align_params_bin(&pwaff1
, &pwaff2
);
3045 return pw_aff_gte_set(pwaff1
, pwaff2
, 0, 0);
3048 /* Return a set containing those elements in the shared domain
3049 * of pwaff1 and pwaff2 where pwaff1 is strictly greater than pwaff2.
3051 __isl_give isl_set
*isl_pw_aff_gt_set(__isl_take isl_pw_aff
*pwaff1
,
3052 __isl_take isl_pw_aff
*pwaff2
)
3054 isl_pw_aff_align_params_bin(&pwaff1
, &pwaff2
);
3055 return pw_aff_gte_set(pwaff1
, pwaff2
, 1, 0);
3058 __isl_give isl_set
*isl_pw_aff_le_set(__isl_take isl_pw_aff
*pwaff1
,
3059 __isl_take isl_pw_aff
*pwaff2
)
3061 return isl_pw_aff_ge_set(pwaff2
, pwaff1
);
3064 __isl_give isl_set
*isl_pw_aff_lt_set(__isl_take isl_pw_aff
*pwaff1
,
3065 __isl_take isl_pw_aff
*pwaff2
)
3067 return isl_pw_aff_gt_set(pwaff2
, pwaff1
);
3070 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3071 * where the function values are ordered in the same way as "order",
3072 * which returns a set in the shared domain of its two arguments.
3074 * Let "pa1" and "pa2" be defined on domains A and B respectively.
3075 * We first pull back the two functions such that they are defined on
3076 * the domain [A -> B]. Then we apply "order", resulting in a set
3077 * in the space [A -> B]. Finally, we unwrap this set to obtain
3078 * a map in the space A -> B.
3080 static __isl_give isl_map
*isl_pw_aff_order_map(
3081 __isl_take isl_pw_aff
*pa1
, __isl_take isl_pw_aff
*pa2
,
3082 __isl_give isl_set
*(*order
)(__isl_take isl_pw_aff
*pa1
,
3083 __isl_take isl_pw_aff
*pa2
))
3085 isl_space
*space1
, *space2
;
3089 isl_pw_aff_align_params_bin(&pa1
, &pa2
);
3090 space1
= isl_space_domain(isl_pw_aff_get_space(pa1
));
3091 space2
= isl_space_domain(isl_pw_aff_get_space(pa2
));
3092 space1
= isl_space_map_from_domain_and_range(space1
, space2
);
3093 ma
= isl_multi_aff_domain_map(isl_space_copy(space1
));
3094 pa1
= isl_pw_aff_pullback_multi_aff(pa1
, ma
);
3095 ma
= isl_multi_aff_range_map(space1
);
3096 pa2
= isl_pw_aff_pullback_multi_aff(pa2
, ma
);
3097 set
= order(pa1
, pa2
);
3099 return isl_set_unwrap(set
);
3102 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3103 * where the function values are equal.
3105 __isl_give isl_map
*isl_pw_aff_eq_map(__isl_take isl_pw_aff
*pa1
,
3106 __isl_take isl_pw_aff
*pa2
)
3108 return isl_pw_aff_order_map(pa1
, pa2
, &isl_pw_aff_eq_set
);
3111 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3112 * where the function value of "pa1" is less than or equal to
3113 * the function value of "pa2".
3115 __isl_give isl_map
*isl_pw_aff_le_map(__isl_take isl_pw_aff
*pa1
,
3116 __isl_take isl_pw_aff
*pa2
)
3118 return isl_pw_aff_order_map(pa1
, pa2
, &isl_pw_aff_le_set
);
3121 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3122 * where the function value of "pa1" is less than the function value of "pa2".
3124 __isl_give isl_map
*isl_pw_aff_lt_map(__isl_take isl_pw_aff
*pa1
,
3125 __isl_take isl_pw_aff
*pa2
)
3127 return isl_pw_aff_order_map(pa1
, pa2
, &isl_pw_aff_lt_set
);
3130 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3131 * where the function value of "pa1" is greater than or equal to
3132 * the function value of "pa2".
3134 __isl_give isl_map
*isl_pw_aff_ge_map(__isl_take isl_pw_aff
*pa1
,
3135 __isl_take isl_pw_aff
*pa2
)
3137 return isl_pw_aff_order_map(pa1
, pa2
, &isl_pw_aff_ge_set
);
3140 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3141 * where the function value of "pa1" is greater than the function value
3144 __isl_give isl_map
*isl_pw_aff_gt_map(__isl_take isl_pw_aff
*pa1
,
3145 __isl_take isl_pw_aff
*pa2
)
3147 return isl_pw_aff_order_map(pa1
, pa2
, &isl_pw_aff_gt_set
);
3150 /* Return a set containing those elements in the shared domain
3151 * of the elements of list1 and list2 where each element in list1
3152 * has the relation specified by "fn" with each element in list2.
3154 static __isl_give isl_set
*pw_aff_list_set(__isl_take isl_pw_aff_list
*list1
,
3155 __isl_take isl_pw_aff_list
*list2
,
3156 __isl_give isl_set
*(*fn
)(__isl_take isl_pw_aff
*pwaff1
,
3157 __isl_take isl_pw_aff
*pwaff2
))
3163 if (!list1
|| !list2
)
3166 ctx
= isl_pw_aff_list_get_ctx(list1
);
3167 if (list1
->n
< 1 || list2
->n
< 1)
3168 isl_die(ctx
, isl_error_invalid
,
3169 "list should contain at least one element", goto error
);
3171 set
= isl_set_universe(isl_pw_aff_get_domain_space(list1
->p
[0]));
3172 for (i
= 0; i
< list1
->n
; ++i
)
3173 for (j
= 0; j
< list2
->n
; ++j
) {
3176 set_ij
= fn(isl_pw_aff_copy(list1
->p
[i
]),
3177 isl_pw_aff_copy(list2
->p
[j
]));
3178 set
= isl_set_intersect(set
, set_ij
);
3181 isl_pw_aff_list_free(list1
);
3182 isl_pw_aff_list_free(list2
);
3185 isl_pw_aff_list_free(list1
);
3186 isl_pw_aff_list_free(list2
);
3190 /* Return a set containing those elements in the shared domain
3191 * of the elements of list1 and list2 where each element in list1
3192 * is equal to each element in list2.
3194 __isl_give isl_set
*isl_pw_aff_list_eq_set(__isl_take isl_pw_aff_list
*list1
,
3195 __isl_take isl_pw_aff_list
*list2
)
3197 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_eq_set
);
3200 __isl_give isl_set
*isl_pw_aff_list_ne_set(__isl_take isl_pw_aff_list
*list1
,
3201 __isl_take isl_pw_aff_list
*list2
)
3203 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_ne_set
);
3206 /* Return a set containing those elements in the shared domain
3207 * of the elements of list1 and list2 where each element in list1
3208 * is less than or equal to each element in list2.
3210 __isl_give isl_set
*isl_pw_aff_list_le_set(__isl_take isl_pw_aff_list
*list1
,
3211 __isl_take isl_pw_aff_list
*list2
)
3213 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_le_set
);
3216 __isl_give isl_set
*isl_pw_aff_list_lt_set(__isl_take isl_pw_aff_list
*list1
,
3217 __isl_take isl_pw_aff_list
*list2
)
3219 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_lt_set
);
3222 __isl_give isl_set
*isl_pw_aff_list_ge_set(__isl_take isl_pw_aff_list
*list1
,
3223 __isl_take isl_pw_aff_list
*list2
)
3225 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_ge_set
);
3228 __isl_give isl_set
*isl_pw_aff_list_gt_set(__isl_take isl_pw_aff_list
*list1
,
3229 __isl_take isl_pw_aff_list
*list2
)
3231 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_gt_set
);
3235 /* Return a set containing those elements in the shared domain
3236 * of pwaff1 and pwaff2 where pwaff1 is not equal to pwaff2.
3238 __isl_give isl_set
*isl_pw_aff_ne_set(__isl_take isl_pw_aff
*pwaff1
,
3239 __isl_take isl_pw_aff
*pwaff2
)
3241 isl_set
*set_lt
, *set_gt
;
3243 isl_pw_aff_align_params_bin(&pwaff1
, &pwaff2
);
3244 set_lt
= isl_pw_aff_lt_set(isl_pw_aff_copy(pwaff1
),
3245 isl_pw_aff_copy(pwaff2
));
3246 set_gt
= isl_pw_aff_gt_set(pwaff1
, pwaff2
);
3247 return isl_set_union_disjoint(set_lt
, set_gt
);
3250 __isl_give isl_pw_aff
*isl_pw_aff_scale_down(__isl_take isl_pw_aff
*pwaff
,
3255 if (isl_int_is_one(v
))
3257 if (!isl_int_is_pos(v
))
3258 isl_die(isl_pw_aff_get_ctx(pwaff
), isl_error_invalid
,
3259 "factor needs to be positive",
3260 return isl_pw_aff_free(pwaff
));
3261 pwaff
= isl_pw_aff_cow(pwaff
);
3267 for (i
= 0; i
< pwaff
->n
; ++i
) {
3268 pwaff
->p
[i
].aff
= isl_aff_scale_down(pwaff
->p
[i
].aff
, v
);
3269 if (!pwaff
->p
[i
].aff
)
3270 return isl_pw_aff_free(pwaff
);
3276 __isl_give isl_pw_aff
*isl_pw_aff_floor(__isl_take isl_pw_aff
*pwaff
)
3280 pwaff
= isl_pw_aff_cow(pwaff
);
3286 for (i
= 0; i
< pwaff
->n
; ++i
) {
3287 pwaff
->p
[i
].aff
= isl_aff_floor(pwaff
->p
[i
].aff
);
3288 if (!pwaff
->p
[i
].aff
)
3289 return isl_pw_aff_free(pwaff
);
3295 __isl_give isl_pw_aff
*isl_pw_aff_ceil(__isl_take isl_pw_aff
*pwaff
)
3299 pwaff
= isl_pw_aff_cow(pwaff
);
3305 for (i
= 0; i
< pwaff
->n
; ++i
) {
3306 pwaff
->p
[i
].aff
= isl_aff_ceil(pwaff
->p
[i
].aff
);
3307 if (!pwaff
->p
[i
].aff
)
3308 return isl_pw_aff_free(pwaff
);
3314 /* Assuming that "cond1" and "cond2" are disjoint,
3315 * return an affine expression that is equal to pwaff1 on cond1
3316 * and to pwaff2 on cond2.
3318 static __isl_give isl_pw_aff
*isl_pw_aff_select(
3319 __isl_take isl_set
*cond1
, __isl_take isl_pw_aff
*pwaff1
,
3320 __isl_take isl_set
*cond2
, __isl_take isl_pw_aff
*pwaff2
)
3322 pwaff1
= isl_pw_aff_intersect_domain(pwaff1
, cond1
);
3323 pwaff2
= isl_pw_aff_intersect_domain(pwaff2
, cond2
);
3325 return isl_pw_aff_add_disjoint(pwaff1
, pwaff2
);
3328 /* Return an affine expression that is equal to pwaff_true for elements
3329 * where "cond" is non-zero and to pwaff_false for elements where "cond"
3331 * That is, return cond ? pwaff_true : pwaff_false;
3333 * If "cond" involves and NaN, then we conservatively return a NaN
3334 * on its entire domain. In principle, we could consider the pieces
3335 * where it is NaN separately from those where it is not.
3337 * If "pwaff_true" and "pwaff_false" are obviously equal to each other,
3338 * then only use the domain of "cond" to restrict the domain.
3340 __isl_give isl_pw_aff
*isl_pw_aff_cond(__isl_take isl_pw_aff
*cond
,
3341 __isl_take isl_pw_aff
*pwaff_true
, __isl_take isl_pw_aff
*pwaff_false
)
3343 isl_set
*cond_true
, *cond_false
;
3348 if (isl_pw_aff_involves_nan(cond
)) {
3349 isl_space
*space
= isl_pw_aff_get_domain_space(cond
);
3350 isl_local_space
*ls
= isl_local_space_from_space(space
);
3351 isl_pw_aff_free(cond
);
3352 isl_pw_aff_free(pwaff_true
);
3353 isl_pw_aff_free(pwaff_false
);
3354 return isl_pw_aff_nan_on_domain(ls
);
3357 pwaff_true
= isl_pw_aff_align_params(pwaff_true
,
3358 isl_pw_aff_get_space(pwaff_false
));
3359 pwaff_false
= isl_pw_aff_align_params(pwaff_false
,
3360 isl_pw_aff_get_space(pwaff_true
));
3361 equal
= isl_pw_aff_plain_is_equal(pwaff_true
, pwaff_false
);
3367 dom
= isl_set_coalesce(isl_pw_aff_domain(cond
));
3368 isl_pw_aff_free(pwaff_false
);
3369 return isl_pw_aff_intersect_domain(pwaff_true
, dom
);
3372 cond_true
= isl_pw_aff_non_zero_set(isl_pw_aff_copy(cond
));
3373 cond_false
= isl_pw_aff_zero_set(cond
);
3374 return isl_pw_aff_select(cond_true
, pwaff_true
,
3375 cond_false
, pwaff_false
);
3377 isl_pw_aff_free(cond
);
3378 isl_pw_aff_free(pwaff_true
);
3379 isl_pw_aff_free(pwaff_false
);
3383 isl_bool
isl_aff_is_cst(__isl_keep isl_aff
*aff
)
3388 return isl_bool_error
;
3390 pos
= isl_seq_first_non_zero(aff
->v
->el
+ 2, aff
->v
->size
- 2);
3391 return isl_bool_ok(pos
== -1);
3394 /* Check whether pwaff is a piecewise constant.
3396 isl_bool
isl_pw_aff_is_cst(__isl_keep isl_pw_aff
*pwaff
)
3401 return isl_bool_error
;
3403 for (i
= 0; i
< pwaff
->n
; ++i
) {
3404 isl_bool is_cst
= isl_aff_is_cst(pwaff
->p
[i
].aff
);
3405 if (is_cst
< 0 || !is_cst
)
3409 return isl_bool_true
;
3412 /* Return the product of "aff1" and "aff2".
3414 * If either of the two is NaN, then the result is NaN.
3416 * Otherwise, at least one of "aff1" or "aff2" needs to be a constant.
3418 __isl_give isl_aff
*isl_aff_mul(__isl_take isl_aff
*aff1
,
3419 __isl_take isl_aff
*aff2
)
3424 if (isl_aff_is_nan(aff1
)) {
3428 if (isl_aff_is_nan(aff2
)) {
3433 if (!isl_aff_is_cst(aff2
) && isl_aff_is_cst(aff1
))
3434 return isl_aff_mul(aff2
, aff1
);
3436 if (!isl_aff_is_cst(aff2
))
3437 isl_die(isl_aff_get_ctx(aff1
), isl_error_invalid
,
3438 "at least one affine expression should be constant",
3441 aff1
= isl_aff_cow(aff1
);
3445 aff1
= isl_aff_scale(aff1
, aff2
->v
->el
[1]);
3446 aff1
= isl_aff_scale_down(aff1
, aff2
->v
->el
[0]);
3456 /* Divide "aff1" by "aff2", assuming "aff2" is a constant.
3458 * If either of the two is NaN, then the result is NaN.
3459 * A division by zero also results in NaN.
3461 __isl_give isl_aff
*isl_aff_div(__isl_take isl_aff
*aff1
,
3462 __isl_take isl_aff
*aff2
)
3464 isl_bool is_cst
, is_zero
;
3470 if (isl_aff_is_nan(aff1
)) {
3474 if (isl_aff_is_nan(aff2
)) {
3479 is_cst
= isl_aff_is_cst(aff2
);
3483 isl_die(isl_aff_get_ctx(aff2
), isl_error_invalid
,
3484 "second argument should be a constant", goto error
);
3485 is_zero
= isl_aff_plain_is_zero(aff2
);
3489 return set_nan_free(aff1
, aff2
);
3491 neg
= isl_int_is_neg(aff2
->v
->el
[1]);
3493 isl_int_neg(aff2
->v
->el
[0], aff2
->v
->el
[0]);
3494 isl_int_neg(aff2
->v
->el
[1], aff2
->v
->el
[1]);
3497 aff1
= isl_aff_scale(aff1
, aff2
->v
->el
[0]);
3498 aff1
= isl_aff_scale_down(aff1
, aff2
->v
->el
[1]);
3501 isl_int_neg(aff2
->v
->el
[0], aff2
->v
->el
[0]);
3502 isl_int_neg(aff2
->v
->el
[1], aff2
->v
->el
[1]);
3513 __isl_give isl_pw_aff
*isl_pw_aff_add(__isl_take isl_pw_aff
*pwaff1
,
3514 __isl_take isl_pw_aff
*pwaff2
)
3516 isl_pw_aff_align_params_bin(&pwaff1
, &pwaff2
);
3517 return isl_pw_aff_on_shared_domain(pwaff1
, pwaff2
, &isl_aff_add
);
3520 __isl_give isl_pw_aff
*isl_pw_aff_union_add(__isl_take isl_pw_aff
*pwaff1
,
3521 __isl_take isl_pw_aff
*pwaff2
)
3523 return isl_pw_aff_union_add_(pwaff1
, pwaff2
);
3526 __isl_give isl_pw_aff
*isl_pw_aff_mul(__isl_take isl_pw_aff
*pwaff1
,
3527 __isl_take isl_pw_aff
*pwaff2
)
3529 isl_pw_aff_align_params_bin(&pwaff1
, &pwaff2
);
3530 return isl_pw_aff_on_shared_domain(pwaff1
, pwaff2
, &isl_aff_mul
);
3533 /* Divide "pa1" by "pa2", assuming "pa2" is a piecewise constant.
3535 __isl_give isl_pw_aff
*isl_pw_aff_div(__isl_take isl_pw_aff
*pa1
,
3536 __isl_take isl_pw_aff
*pa2
)
3540 is_cst
= isl_pw_aff_is_cst(pa2
);
3544 isl_die(isl_pw_aff_get_ctx(pa2
), isl_error_invalid
,
3545 "second argument should be a piecewise constant",
3547 isl_pw_aff_align_params_bin(&pa1
, &pa2
);
3548 return isl_pw_aff_on_shared_domain(pa1
, pa2
, &isl_aff_div
);
3550 isl_pw_aff_free(pa1
);
3551 isl_pw_aff_free(pa2
);
3555 /* Compute the quotient of the integer division of "pa1" by "pa2"
3556 * with rounding towards zero.
3557 * "pa2" is assumed to be a piecewise constant.
3559 * In particular, return
3561 * pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2)
3564 __isl_give isl_pw_aff
*isl_pw_aff_tdiv_q(__isl_take isl_pw_aff
*pa1
,
3565 __isl_take isl_pw_aff
*pa2
)
3571 is_cst
= isl_pw_aff_is_cst(pa2
);
3575 isl_die(isl_pw_aff_get_ctx(pa2
), isl_error_invalid
,
3576 "second argument should be a piecewise constant",
3579 pa1
= isl_pw_aff_div(pa1
, pa2
);
3581 cond
= isl_pw_aff_nonneg_set(isl_pw_aff_copy(pa1
));
3582 f
= isl_pw_aff_floor(isl_pw_aff_copy(pa1
));
3583 c
= isl_pw_aff_ceil(pa1
);
3584 return isl_pw_aff_cond(isl_set_indicator_function(cond
), f
, c
);
3586 isl_pw_aff_free(pa1
);
3587 isl_pw_aff_free(pa2
);
3591 /* Compute the remainder of the integer division of "pa1" by "pa2"
3592 * with rounding towards zero.
3593 * "pa2" is assumed to be a piecewise constant.
3595 * In particular, return
3597 * pa1 - pa2 * (pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2))
3600 __isl_give isl_pw_aff
*isl_pw_aff_tdiv_r(__isl_take isl_pw_aff
*pa1
,
3601 __isl_take isl_pw_aff
*pa2
)
3606 is_cst
= isl_pw_aff_is_cst(pa2
);
3610 isl_die(isl_pw_aff_get_ctx(pa2
), isl_error_invalid
,
3611 "second argument should be a piecewise constant",
3613 res
= isl_pw_aff_tdiv_q(isl_pw_aff_copy(pa1
), isl_pw_aff_copy(pa2
));
3614 res
= isl_pw_aff_mul(pa2
, res
);
3615 res
= isl_pw_aff_sub(pa1
, res
);
3618 isl_pw_aff_free(pa1
);
3619 isl_pw_aff_free(pa2
);
3623 /* Does either of "pa1" or "pa2" involve any NaN2?
3625 static isl_bool
either_involves_nan(__isl_keep isl_pw_aff
*pa1
,
3626 __isl_keep isl_pw_aff
*pa2
)
3630 has_nan
= isl_pw_aff_involves_nan(pa1
);
3631 if (has_nan
< 0 || has_nan
)
3633 return isl_pw_aff_involves_nan(pa2
);
3636 /* Replace "pa1" and "pa2" (at least one of which involves a NaN)
3637 * by a NaN on their shared domain.
3639 * In principle, the result could be refined to only being NaN
3640 * on the parts of this domain where at least one of "pa1" or "pa2" is NaN.
3642 static __isl_give isl_pw_aff
*replace_by_nan(__isl_take isl_pw_aff
*pa1
,
3643 __isl_take isl_pw_aff
*pa2
)
3645 isl_local_space
*ls
;
3649 dom
= isl_set_intersect(isl_pw_aff_domain(pa1
), isl_pw_aff_domain(pa2
));
3650 ls
= isl_local_space_from_space(isl_set_get_space(dom
));
3651 pa
= isl_pw_aff_nan_on_domain(ls
);
3652 pa
= isl_pw_aff_intersect_domain(pa
, dom
);
3657 static __isl_give isl_pw_aff
*pw_aff_min(__isl_take isl_pw_aff
*pwaff1
,
3658 __isl_take isl_pw_aff
*pwaff2
)
3663 dom
= isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1
)),
3664 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2
)));
3665 le
= isl_pw_aff_le_set(isl_pw_aff_copy(pwaff1
),
3666 isl_pw_aff_copy(pwaff2
));
3667 dom
= isl_set_subtract(dom
, isl_set_copy(le
));
3668 return isl_pw_aff_select(le
, pwaff1
, dom
, pwaff2
);
3671 static __isl_give isl_pw_aff
*pw_aff_max(__isl_take isl_pw_aff
*pwaff1
,
3672 __isl_take isl_pw_aff
*pwaff2
)
3677 dom
= isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1
)),
3678 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2
)));
3679 ge
= isl_pw_aff_ge_set(isl_pw_aff_copy(pwaff1
),
3680 isl_pw_aff_copy(pwaff2
));
3681 dom
= isl_set_subtract(dom
, isl_set_copy(ge
));
3682 return isl_pw_aff_select(ge
, pwaff1
, dom
, pwaff2
);
3685 /* Return an expression for the minimum (if "max" is not set) or
3686 * the maximum (if "max" is set) of "pa1" and "pa2".
3687 * If either expression involves any NaN, then return a NaN
3688 * on the shared domain as result.
3690 static __isl_give isl_pw_aff
*pw_aff_min_max(__isl_take isl_pw_aff
*pa1
,
3691 __isl_take isl_pw_aff
*pa2
, int max
)
3695 has_nan
= either_involves_nan(pa1
, pa2
);
3697 pa1
= isl_pw_aff_free(pa1
);
3699 return replace_by_nan(pa1
, pa2
);
3701 isl_pw_aff_align_params_bin(&pa1
, &pa2
);
3703 return pw_aff_max(pa1
, pa2
);
3705 return pw_aff_min(pa1
, pa2
);
3708 /* Return an expression for the minimum of "pwaff1" and "pwaff2".
3710 __isl_give isl_pw_aff
*isl_pw_aff_min(__isl_take isl_pw_aff
*pwaff1
,
3711 __isl_take isl_pw_aff
*pwaff2
)
3713 return pw_aff_min_max(pwaff1
, pwaff2
, 0);
3716 /* Return an expression for the maximum of "pwaff1" and "pwaff2".
3718 __isl_give isl_pw_aff
*isl_pw_aff_max(__isl_take isl_pw_aff
*pwaff1
,
3719 __isl_take isl_pw_aff
*pwaff2
)
3721 return pw_aff_min_max(pwaff1
, pwaff2
, 1);
3724 static __isl_give isl_pw_aff
*pw_aff_list_reduce(
3725 __isl_take isl_pw_aff_list
*list
,
3726 __isl_give isl_pw_aff
*(*fn
)(__isl_take isl_pw_aff
*pwaff1
,
3727 __isl_take isl_pw_aff
*pwaff2
))
3736 ctx
= isl_pw_aff_list_get_ctx(list
);
3738 isl_die(ctx
, isl_error_invalid
,
3739 "list should contain at least one element", goto error
);
3741 res
= isl_pw_aff_copy(list
->p
[0]);
3742 for (i
= 1; i
< list
->n
; ++i
)
3743 res
= fn(res
, isl_pw_aff_copy(list
->p
[i
]));
3745 isl_pw_aff_list_free(list
);
3748 isl_pw_aff_list_free(list
);
3752 /* Return an isl_pw_aff that maps each element in the intersection of the
3753 * domains of the elements of list to the minimal corresponding affine
3756 __isl_give isl_pw_aff
*isl_pw_aff_list_min(__isl_take isl_pw_aff_list
*list
)
3758 return pw_aff_list_reduce(list
, &isl_pw_aff_min
);
3761 /* Return an isl_pw_aff that maps each element in the intersection of the
3762 * domains of the elements of list to the maximal corresponding affine
3765 __isl_give isl_pw_aff
*isl_pw_aff_list_max(__isl_take isl_pw_aff_list
*list
)
3767 return pw_aff_list_reduce(list
, &isl_pw_aff_max
);
3770 /* Mark the domains of "pwaff" as rational.
3772 __isl_give isl_pw_aff
*isl_pw_aff_set_rational(__isl_take isl_pw_aff
*pwaff
)
3776 pwaff
= isl_pw_aff_cow(pwaff
);
3782 for (i
= 0; i
< pwaff
->n
; ++i
) {
3783 pwaff
->p
[i
].set
= isl_set_set_rational(pwaff
->p
[i
].set
);
3784 if (!pwaff
->p
[i
].set
)
3785 return isl_pw_aff_free(pwaff
);
3791 /* Mark the domains of the elements of "list" as rational.
3793 __isl_give isl_pw_aff_list
*isl_pw_aff_list_set_rational(
3794 __isl_take isl_pw_aff_list
*list
)
3804 for (i
= 0; i
< n
; ++i
) {
3807 pa
= isl_pw_aff_list_get_pw_aff(list
, i
);
3808 pa
= isl_pw_aff_set_rational(pa
);
3809 list
= isl_pw_aff_list_set_pw_aff(list
, i
, pa
);
3815 /* Do the parameters of "aff" match those of "space"?
3817 isl_bool
isl_aff_matching_params(__isl_keep isl_aff
*aff
,
3818 __isl_keep isl_space
*space
)
3820 isl_space
*aff_space
;
3824 return isl_bool_error
;
3826 aff_space
= isl_aff_get_domain_space(aff
);
3828 match
= isl_space_has_equal_params(space
, aff_space
);
3830 isl_space_free(aff_space
);
3834 /* Check that the domain space of "aff" matches "space".
3836 isl_stat
isl_aff_check_match_domain_space(__isl_keep isl_aff
*aff
,
3837 __isl_keep isl_space
*space
)
3839 isl_space
*aff_space
;
3843 return isl_stat_error
;
3845 aff_space
= isl_aff_get_domain_space(aff
);
3847 match
= isl_space_has_equal_params(space
, aff_space
);
3851 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
3852 "parameters don't match", goto error
);
3853 match
= isl_space_tuple_is_equal(space
, isl_dim_in
,
3854 aff_space
, isl_dim_set
);
3858 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
3859 "domains don't match", goto error
);
3860 isl_space_free(aff_space
);
3863 isl_space_free(aff_space
);
3864 return isl_stat_error
;
3867 /* Return the shared (universe) domain of the elements of "ma".
3869 * Since an isl_multi_aff (and an isl_aff) is always total,
3870 * the domain is always the universe set in its domain space.
3871 * This is a helper function for use in the generic isl_multi_*_bind.
3873 static __isl_give isl_basic_set
*isl_multi_aff_domain(
3874 __isl_take isl_multi_aff
*ma
)
3878 space
= isl_multi_aff_get_space(ma
);
3879 isl_multi_aff_free(ma
);
3881 return isl_basic_set_universe(isl_space_domain(space
));
3887 #include <isl_multi_no_explicit_domain.c>
3888 #include <isl_multi_templ.c>
3889 #include <isl_multi_add_constant_templ.c>
3890 #include <isl_multi_apply_set.c>
3891 #include <isl_multi_arith_templ.c>
3892 #include <isl_multi_bind_domain_templ.c>
3893 #include <isl_multi_cmp.c>
3894 #include <isl_multi_dim_id_templ.c>
3895 #include <isl_multi_dims.c>
3896 #include <isl_multi_floor.c>
3897 #include <isl_multi_from_base_templ.c>
3898 #include <isl_multi_identity_templ.c>
3899 #include <isl_multi_insert_domain_templ.c>
3900 #include <isl_multi_locals_templ.c>
3901 #include <isl_multi_move_dims_templ.c>
3902 #include <isl_multi_nan_templ.c>
3903 #include <isl_multi_product_templ.c>
3904 #include <isl_multi_splice_templ.c>
3905 #include <isl_multi_tuple_id_templ.c>
3906 #include <isl_multi_unbind_params_templ.c>
3907 #include <isl_multi_zero_templ.c>
3911 #include <isl_multi_gist.c>
3914 #define DOMBASE basic_set
3915 #include <isl_multi_bind_templ.c>
3917 /* Construct an isl_multi_aff living in "space" that corresponds
3918 * to the affine transformation matrix "mat".
3920 __isl_give isl_multi_aff
*isl_multi_aff_from_aff_mat(
3921 __isl_take isl_space
*space
, __isl_take isl_mat
*mat
)
3924 isl_local_space
*ls
= NULL
;
3925 isl_multi_aff
*ma
= NULL
;
3926 isl_size n_row
, n_col
, n_out
, total
;
3932 ctx
= isl_mat_get_ctx(mat
);
3934 n_row
= isl_mat_rows(mat
);
3935 n_col
= isl_mat_cols(mat
);
3936 n_out
= isl_space_dim(space
, isl_dim_out
);
3937 total
= isl_space_dim(space
, isl_dim_all
);
3938 if (n_row
< 0 || n_col
< 0 || n_out
< 0 || total
< 0)
3941 isl_die(ctx
, isl_error_invalid
,
3942 "insufficient number of rows", goto error
);
3944 isl_die(ctx
, isl_error_invalid
,
3945 "insufficient number of columns", goto error
);
3946 if (1 + n_out
!= n_row
|| 2 + total
!= n_row
+ n_col
)
3947 isl_die(ctx
, isl_error_invalid
,
3948 "dimension mismatch", goto error
);
3950 ma
= isl_multi_aff_zero(isl_space_copy(space
));
3951 space
= isl_space_domain(space
);
3952 ls
= isl_local_space_from_space(isl_space_copy(space
));
3954 for (i
= 0; i
< n_row
- 1; ++i
) {
3958 v
= isl_vec_alloc(ctx
, 1 + n_col
);
3961 isl_int_set(v
->el
[0], mat
->row
[0][0]);
3962 isl_seq_cpy(v
->el
+ 1, mat
->row
[1 + i
], n_col
);
3963 v
= isl_vec_normalize(v
);
3964 aff
= isl_aff_alloc_vec(isl_local_space_copy(ls
), v
);
3965 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
3968 isl_space_free(space
);
3969 isl_local_space_free(ls
);
3973 isl_space_free(space
);
3974 isl_local_space_free(ls
);
3976 isl_multi_aff_free(ma
);
3980 /* Return the constant terms of the affine expressions of "ma".
3982 __isl_give isl_multi_val
*isl_multi_aff_get_constant_multi_val(
3983 __isl_keep isl_multi_aff
*ma
)
3990 n
= isl_multi_aff_size(ma
);
3993 space
= isl_space_range(isl_multi_aff_get_space(ma
));
3994 space
= isl_space_drop_all_params(space
);
3995 mv
= isl_multi_val_zero(space
);
3997 for (i
= 0; i
< n
; ++i
) {
4001 aff
= isl_multi_aff_get_at(ma
, i
);
4002 val
= isl_aff_get_constant_val(aff
);
4004 mv
= isl_multi_val_set_at(mv
, i
, val
);
4010 /* Remove any internal structure of the domain of "ma".
4011 * If there is any such internal structure in the input,
4012 * then the name of the corresponding space is also removed.
4014 __isl_give isl_multi_aff
*isl_multi_aff_flatten_domain(
4015 __isl_take isl_multi_aff
*ma
)
4022 if (!ma
->space
->nested
[0])
4025 space
= isl_multi_aff_get_space(ma
);
4026 space
= isl_space_flatten_domain(space
);
4027 ma
= isl_multi_aff_reset_space(ma
, space
);
4032 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
4033 * of the space to its domain.
4035 __isl_give isl_multi_aff
*isl_multi_aff_domain_map(__isl_take isl_space
*space
)
4039 isl_local_space
*ls
;
4044 if (!isl_space_is_map(space
))
4045 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
4046 "not a map space", goto error
);
4048 n_in
= isl_space_dim(space
, isl_dim_in
);
4051 space
= isl_space_domain_map(space
);
4053 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
4055 isl_space_free(space
);
4059 space
= isl_space_domain(space
);
4060 ls
= isl_local_space_from_space(space
);
4061 for (i
= 0; i
< n_in
; ++i
) {
4064 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
4066 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
4068 isl_local_space_free(ls
);
4071 isl_space_free(space
);
4075 /* This function performs the same operation as isl_multi_aff_domain_map,
4076 * but is considered as a function on an isl_space when exported.
4078 __isl_give isl_multi_aff
*isl_space_domain_map_multi_aff(
4079 __isl_take isl_space
*space
)
4081 return isl_multi_aff_domain_map(space
);
4084 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
4085 * of the space to its range.
4087 __isl_give isl_multi_aff
*isl_multi_aff_range_map(__isl_take isl_space
*space
)
4090 isl_size n_in
, n_out
;
4091 isl_local_space
*ls
;
4096 if (!isl_space_is_map(space
))
4097 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
4098 "not a map space", goto error
);
4100 n_in
= isl_space_dim(space
, isl_dim_in
);
4101 n_out
= isl_space_dim(space
, isl_dim_out
);
4102 if (n_in
< 0 || n_out
< 0)
4104 space
= isl_space_range_map(space
);
4106 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
4108 isl_space_free(space
);
4112 space
= isl_space_domain(space
);
4113 ls
= isl_local_space_from_space(space
);
4114 for (i
= 0; i
< n_out
; ++i
) {
4117 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
4118 isl_dim_set
, n_in
+ i
);
4119 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
4121 isl_local_space_free(ls
);
4124 isl_space_free(space
);
4128 /* This function performs the same operation as isl_multi_aff_range_map,
4129 * but is considered as a function on an isl_space when exported.
4131 __isl_give isl_multi_aff
*isl_space_range_map_multi_aff(
4132 __isl_take isl_space
*space
)
4134 return isl_multi_aff_range_map(space
);
4137 /* Given a map space, return an isl_pw_multi_aff that maps a wrapped copy
4138 * of the space to its domain.
4140 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_domain_map(
4141 __isl_take isl_space
*space
)
4143 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_domain_map(space
));
4146 /* This function performs the same operation as isl_pw_multi_aff_domain_map,
4147 * but is considered as a function on an isl_space when exported.
4149 __isl_give isl_pw_multi_aff
*isl_space_domain_map_pw_multi_aff(
4150 __isl_take isl_space
*space
)
4152 return isl_pw_multi_aff_domain_map(space
);
4155 /* Given a map space, return an isl_pw_multi_aff that maps a wrapped copy
4156 * of the space to its range.
4158 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_range_map(
4159 __isl_take isl_space
*space
)
4161 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_range_map(space
));
4164 /* This function performs the same operation as isl_pw_multi_aff_range_map,
4165 * but is considered as a function on an isl_space when exported.
4167 __isl_give isl_pw_multi_aff
*isl_space_range_map_pw_multi_aff(
4168 __isl_take isl_space
*space
)
4170 return isl_pw_multi_aff_range_map(space
);
4173 /* Given the space of a set and a range of set dimensions,
4174 * construct an isl_multi_aff that projects out those dimensions.
4176 __isl_give isl_multi_aff
*isl_multi_aff_project_out_map(
4177 __isl_take isl_space
*space
, enum isl_dim_type type
,
4178 unsigned first
, unsigned n
)
4182 isl_local_space
*ls
;
4187 if (!isl_space_is_set(space
))
4188 isl_die(isl_space_get_ctx(space
), isl_error_unsupported
,
4189 "expecting set space", goto error
);
4190 if (type
!= isl_dim_set
)
4191 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
4192 "only set dimensions can be projected out", goto error
);
4193 if (isl_space_check_range(space
, type
, first
, n
) < 0)
4196 dim
= isl_space_dim(space
, isl_dim_set
);
4200 space
= isl_space_from_domain(space
);
4201 space
= isl_space_add_dims(space
, isl_dim_out
, dim
- n
);
4204 return isl_multi_aff_alloc(space
);
4206 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
4207 space
= isl_space_domain(space
);
4208 ls
= isl_local_space_from_space(space
);
4210 for (i
= 0; i
< first
; ++i
) {
4213 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
4215 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
4218 for (i
= 0; i
< dim
- (first
+ n
); ++i
) {
4221 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
4222 isl_dim_set
, first
+ n
+ i
);
4223 ma
= isl_multi_aff_set_aff(ma
, first
+ i
, aff
);
4226 isl_local_space_free(ls
);
4229 isl_space_free(space
);
4233 /* Given the space of a set and a range of set dimensions,
4234 * construct an isl_pw_multi_aff that projects out those dimensions.
4236 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_project_out_map(
4237 __isl_take isl_space
*space
, enum isl_dim_type type
,
4238 unsigned first
, unsigned n
)
4242 ma
= isl_multi_aff_project_out_map(space
, type
, first
, n
);
4243 return isl_pw_multi_aff_from_multi_aff(ma
);
4246 /* This function performs the same operation as isl_pw_multi_aff_from_multi_aff,
4247 * but is considered as a function on an isl_multi_aff when exported.
4249 __isl_give isl_pw_multi_aff
*isl_multi_aff_to_pw_multi_aff(
4250 __isl_take isl_multi_aff
*ma
)
4252 return isl_pw_multi_aff_from_multi_aff(ma
);
4255 /* Create a piecewise multi-affine expression in the given space that maps each
4256 * input dimension to the corresponding output dimension.
4258 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_identity(
4259 __isl_take isl_space
*space
)
4261 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_identity(space
));
4264 /* Create a piecewise multi expression that maps elements in the given space
4267 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_identity_on_domain_space(
4268 __isl_take isl_space
*space
)
4272 ma
= isl_multi_aff_identity_on_domain_space(space
);
4273 return isl_pw_multi_aff_from_multi_aff(ma
);
4276 /* This function performs the same operation as
4277 * isl_pw_multi_aff_identity_on_domain_space,
4278 * but is considered as a function on an isl_space when exported.
4280 __isl_give isl_pw_multi_aff
*isl_space_identity_pw_multi_aff_on_domain(
4281 __isl_take isl_space
*space
)
4283 return isl_pw_multi_aff_identity_on_domain_space(space
);
4286 /* Exploit the equalities in "eq" to simplify the affine expressions.
4288 static __isl_give isl_multi_aff
*isl_multi_aff_substitute_equalities(
4289 __isl_take isl_multi_aff
*maff
, __isl_take isl_basic_set
*eq
)
4293 maff
= isl_multi_aff_cow(maff
);
4297 for (i
= 0; i
< maff
->n
; ++i
) {
4298 maff
->u
.p
[i
] = isl_aff_substitute_equalities(maff
->u
.p
[i
],
4299 isl_basic_set_copy(eq
));
4304 isl_basic_set_free(eq
);
4307 isl_basic_set_free(eq
);
4308 isl_multi_aff_free(maff
);
4312 __isl_give isl_multi_aff
*isl_multi_aff_scale(__isl_take isl_multi_aff
*maff
,
4317 maff
= isl_multi_aff_cow(maff
);
4321 for (i
= 0; i
< maff
->n
; ++i
) {
4322 maff
->u
.p
[i
] = isl_aff_scale(maff
->u
.p
[i
], f
);
4324 return isl_multi_aff_free(maff
);
4330 __isl_give isl_multi_aff
*isl_multi_aff_add_on_domain(__isl_keep isl_set
*dom
,
4331 __isl_take isl_multi_aff
*maff1
, __isl_take isl_multi_aff
*maff2
)
4333 maff1
= isl_multi_aff_add(maff1
, maff2
);
4334 maff1
= isl_multi_aff_gist(maff1
, isl_set_copy(dom
));
4338 isl_bool
isl_multi_aff_is_empty(__isl_keep isl_multi_aff
*maff
)
4341 return isl_bool_error
;
4343 return isl_bool_false
;
4346 /* Return the set of domain elements where "ma1" is lexicographically
4347 * smaller than or equal to "ma2".
4349 __isl_give isl_set
*isl_multi_aff_lex_le_set(__isl_take isl_multi_aff
*ma1
,
4350 __isl_take isl_multi_aff
*ma2
)
4352 return isl_multi_aff_lex_ge_set(ma2
, ma1
);
4355 /* Return the set of domain elements where "ma1" is lexicographically
4356 * smaller than "ma2".
4358 __isl_give isl_set
*isl_multi_aff_lex_lt_set(__isl_take isl_multi_aff
*ma1
,
4359 __isl_take isl_multi_aff
*ma2
)
4361 return isl_multi_aff_lex_gt_set(ma2
, ma1
);
4364 /* Return the set of domain elements where "ma1" is lexicographically
4365 * greater than to "ma2". If "equal" is set, then include the domain
4366 * elements where they are equal.
4367 * Do this for the case where there are no entries.
4368 * In this case, "ma1" cannot be greater than "ma2",
4369 * but it is (greater than or) equal to "ma2".
4371 static __isl_give isl_set
*isl_multi_aff_lex_gte_set_0d(
4372 __isl_take isl_multi_aff
*ma1
, __isl_take isl_multi_aff
*ma2
, int equal
)
4376 space
= isl_multi_aff_get_domain_space(ma1
);
4378 isl_multi_aff_free(ma1
);
4379 isl_multi_aff_free(ma2
);
4382 return isl_set_universe(space
);
4384 return isl_set_empty(space
);
4387 /* Return the set where entry "i" of "ma1" and "ma2"
4388 * satisfy the relation prescribed by "cmp".
4390 static __isl_give isl_set
*isl_multi_aff_order_at(__isl_keep isl_multi_aff
*ma1
,
4391 __isl_keep isl_multi_aff
*ma2
, int i
,
4392 __isl_give isl_set
*(*cmp
)(__isl_take isl_aff
*aff1
,
4393 __isl_take isl_aff
*aff2
))
4395 isl_aff
*aff1
, *aff2
;
4397 aff1
= isl_multi_aff_get_at(ma1
, i
);
4398 aff2
= isl_multi_aff_get_at(ma2
, i
);
4399 return cmp(aff1
, aff2
);
4402 /* Return the set of domain elements where "ma1" is lexicographically
4403 * greater than to "ma2". If "equal" is set, then include the domain
4404 * elements where they are equal.
4406 * In particular, for all but the final entry,
4407 * include the set of elements where this entry is strictly greater in "ma1"
4408 * and all previous entries are equal.
4409 * The final entry is also allowed to be equal in the two functions
4410 * if "equal" is set.
4412 * The case where there are no entries is handled separately.
4414 static __isl_give isl_set
*isl_multi_aff_lex_gte_set(
4415 __isl_take isl_multi_aff
*ma1
, __isl_take isl_multi_aff
*ma2
, int equal
)
4424 if (isl_multi_aff_check_equal_space(ma1
, ma2
) < 0)
4426 n
= isl_multi_aff_size(ma1
);
4430 return isl_multi_aff_lex_gte_set_0d(ma1
, ma2
, equal
);
4432 space
= isl_multi_aff_get_domain_space(ma1
);
4433 res
= isl_set_empty(isl_space_copy(space
));
4434 equal_set
= isl_set_universe(space
);
4436 for (i
= 0; i
+ 1 < n
; ++i
) {
4440 gt
= isl_multi_aff_order_at(ma1
, ma2
, i
, &isl_aff_gt_set
);
4441 gt
= isl_set_intersect(gt
, isl_set_copy(equal_set
));
4442 res
= isl_set_union(res
, gt
);
4443 eq
= isl_multi_aff_order_at(ma1
, ma2
, i
, &isl_aff_eq_set
);
4444 equal_set
= isl_set_intersect(equal_set
, eq
);
4446 empty
= isl_set_is_empty(equal_set
);
4447 if (empty
>= 0 && empty
)
4452 gte
= isl_multi_aff_order_at(ma1
, ma2
, n
- 1, &isl_aff_ge_set
);
4454 gte
= isl_multi_aff_order_at(ma1
, ma2
, n
- 1, &isl_aff_gt_set
);
4455 isl_multi_aff_free(ma1
);
4456 isl_multi_aff_free(ma2
);
4458 gte
= isl_set_intersect(gte
, equal_set
);
4459 return isl_set_union(res
, gte
);
4461 isl_multi_aff_free(ma1
);
4462 isl_multi_aff_free(ma2
);
4466 /* Return the set of domain elements where "ma1" is lexicographically
4467 * greater than or equal to "ma2".
4469 __isl_give isl_set
*isl_multi_aff_lex_ge_set(__isl_take isl_multi_aff
*ma1
,
4470 __isl_take isl_multi_aff
*ma2
)
4472 return isl_multi_aff_lex_gte_set(ma1
, ma2
, 1);
4475 /* Return the set of domain elements where "ma1" is lexicographically
4476 * greater than "ma2".
4478 __isl_give isl_set
*isl_multi_aff_lex_gt_set(__isl_take isl_multi_aff
*ma1
,
4479 __isl_take isl_multi_aff
*ma2
)
4481 return isl_multi_aff_lex_gte_set(ma1
, ma2
, 0);
4484 #define isl_multi_aff_zero_in_space isl_multi_aff_zero
4487 #define PW isl_pw_multi_aff
4489 #define BASE multi_aff
4491 #define EL_IS_ZERO is_empty
4495 #define IS_ZERO is_empty
4498 #undef DEFAULT_IS_ZERO
4499 #define DEFAULT_IS_ZERO 0
4501 #include <isl_pw_templ.c>
4502 #include <isl_pw_add_constant_multi_val_templ.c>
4503 #include <isl_pw_add_constant_val_templ.c>
4504 #include <isl_pw_bind_domain_templ.c>
4505 #include <isl_pw_insert_dims_templ.c>
4506 #include <isl_pw_insert_domain_templ.c>
4507 #include <isl_pw_locals_templ.c>
4508 #include <isl_pw_move_dims_templ.c>
4509 #include <isl_pw_neg_templ.c>
4510 #include <isl_pw_pullback_templ.c>
4511 #include <isl_pw_range_tuple_id_templ.c>
4512 #include <isl_pw_union_opt.c>
4515 #define BASE pw_multi_aff
4517 #include <isl_union_multi.c>
4518 #include "isl_union_locals_templ.c"
4519 #include <isl_union_neg.c>
4522 #define BASE multi_aff
4524 #include <isl_union_pw_templ.c>
4526 /* Generic function for extracting a factor from a product "pma".
4527 * "check_space" checks that the space is that of the right kind of product.
4528 * "space_factor" extracts the factor from the space.
4529 * "multi_aff_factor" extracts the factor from the constituent functions.
4531 static __isl_give isl_pw_multi_aff
*pw_multi_aff_factor(
4532 __isl_take isl_pw_multi_aff
*pma
,
4533 isl_stat (*check_space
)(__isl_keep isl_pw_multi_aff
*pma
),
4534 __isl_give isl_space
*(*space_factor
)(__isl_take isl_space
*space
),
4535 __isl_give isl_multi_aff
*(*multi_aff_factor
)(
4536 __isl_take isl_multi_aff
*ma
))
4541 if (check_space(pma
) < 0)
4542 return isl_pw_multi_aff_free(pma
);
4544 space
= isl_pw_multi_aff_take_space(pma
);
4545 space
= space_factor(space
);
4547 for (i
= 0; pma
&& i
< pma
->n
; ++i
) {
4550 ma
= isl_pw_multi_aff_take_base_at(pma
, i
);
4551 ma
= multi_aff_factor(ma
);
4552 pma
= isl_pw_multi_aff_restore_base_at(pma
, i
, ma
);
4555 pma
= isl_pw_multi_aff_restore_space(pma
, space
);
4560 /* Is the range of "pma" a wrapped relation?
4562 static isl_bool
isl_pw_multi_aff_range_is_wrapping(
4563 __isl_keep isl_pw_multi_aff
*pma
)
4565 return isl_space_range_is_wrapping(isl_pw_multi_aff_peek_space(pma
));
4568 /* Check that the range of "pma" is a product.
4570 static isl_stat
pw_multi_aff_check_range_product(
4571 __isl_keep isl_pw_multi_aff
*pma
)
4575 wraps
= isl_pw_multi_aff_range_is_wrapping(pma
);
4577 return isl_stat_error
;
4579 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
4580 "range is not a product", return isl_stat_error
);
4584 /* Given a function A -> [B -> C], extract the function A -> B.
4586 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_range_factor_domain(
4587 __isl_take isl_pw_multi_aff
*pma
)
4589 return pw_multi_aff_factor(pma
, &pw_multi_aff_check_range_product
,
4590 &isl_space_range_factor_domain
,
4591 &isl_multi_aff_range_factor_domain
);
4594 /* Given a function A -> [B -> C], extract the function A -> C.
4596 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_range_factor_range(
4597 __isl_take isl_pw_multi_aff
*pma
)
4599 return pw_multi_aff_factor(pma
, &pw_multi_aff_check_range_product
,
4600 &isl_space_range_factor_range
,
4601 &isl_multi_aff_range_factor_range
);
4604 /* Given two piecewise multi affine expressions, return a piecewise
4605 * multi-affine expression defined on the union of the definition domains
4606 * of the inputs that is equal to the lexicographic maximum of the two
4607 * inputs on each cell. If only one of the two inputs is defined on
4608 * a given cell, then it is considered to be the maximum.
4610 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_union_lexmax(
4611 __isl_take isl_pw_multi_aff
*pma1
,
4612 __isl_take isl_pw_multi_aff
*pma2
)
4614 isl_pw_multi_aff_align_params_bin(&pma1
, &pma2
);
4615 return isl_pw_multi_aff_union_opt_cmp(pma1
, pma2
,
4616 &isl_multi_aff_lex_ge_set
);
4619 /* Given two piecewise multi affine expressions, return a piecewise
4620 * multi-affine expression defined on the union of the definition domains
4621 * of the inputs that is equal to the lexicographic minimum of the two
4622 * inputs on each cell. If only one of the two inputs is defined on
4623 * a given cell, then it is considered to be the minimum.
4625 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_union_lexmin(
4626 __isl_take isl_pw_multi_aff
*pma1
,
4627 __isl_take isl_pw_multi_aff
*pma2
)
4629 isl_pw_multi_aff_align_params_bin(&pma1
, &pma2
);
4630 return isl_pw_multi_aff_union_opt_cmp(pma1
, pma2
,
4631 &isl_multi_aff_lex_le_set
);
4634 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_add(
4635 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4637 isl_pw_multi_aff_align_params_bin(&pma1
, &pma2
);
4638 return isl_pw_multi_aff_on_shared_domain(pma1
, pma2
,
4639 &isl_multi_aff_add
);
4642 /* Subtract "pma2" from "pma1" and return the result.
4644 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_sub(
4645 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4647 isl_pw_multi_aff_align_params_bin(&pma1
, &pma2
);
4648 return isl_pw_multi_aff_on_shared_domain(pma1
, pma2
,
4649 &isl_multi_aff_sub
);
4652 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_union_add(
4653 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4655 return isl_pw_multi_aff_union_add_(pma1
, pma2
);
4658 /* Compute the sum of "upa1" and "upa2" on the union of their domains,
4659 * with the actual sum on the shared domain and
4660 * the defined expression on the symmetric difference of the domains.
4662 __isl_give isl_union_pw_aff
*isl_union_pw_aff_union_add(
4663 __isl_take isl_union_pw_aff
*upa1
, __isl_take isl_union_pw_aff
*upa2
)
4665 return isl_union_pw_aff_union_add_(upa1
, upa2
);
4668 /* Compute the sum of "upma1" and "upma2" on the union of their domains,
4669 * with the actual sum on the shared domain and
4670 * the defined expression on the symmetric difference of the domains.
4672 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_union_add(
4673 __isl_take isl_union_pw_multi_aff
*upma1
,
4674 __isl_take isl_union_pw_multi_aff
*upma2
)
4676 return isl_union_pw_multi_aff_union_add_(upma1
, upma2
);
4679 /* Given two piecewise multi-affine expressions A -> B and C -> D,
4680 * construct a piecewise multi-affine expression [A -> C] -> [B -> D].
4682 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_product(
4683 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4687 isl_pw_multi_aff
*res
;
4689 if (isl_pw_multi_aff_align_params_bin(&pma1
, &pma2
) < 0)
4692 n
= pma1
->n
* pma2
->n
;
4693 space
= isl_space_product(isl_space_copy(pma1
->dim
),
4694 isl_space_copy(pma2
->dim
));
4695 res
= isl_pw_multi_aff_alloc_size(space
, n
);
4697 for (i
= 0; i
< pma1
->n
; ++i
) {
4698 for (j
= 0; j
< pma2
->n
; ++j
) {
4702 domain
= isl_set_product(isl_set_copy(pma1
->p
[i
].set
),
4703 isl_set_copy(pma2
->p
[j
].set
));
4704 ma
= isl_multi_aff_product(
4705 isl_multi_aff_copy(pma1
->p
[i
].maff
),
4706 isl_multi_aff_copy(pma2
->p
[j
].maff
));
4707 res
= isl_pw_multi_aff_add_piece(res
, domain
, ma
);
4711 isl_pw_multi_aff_free(pma1
);
4712 isl_pw_multi_aff_free(pma2
);
4715 isl_pw_multi_aff_free(pma1
);
4716 isl_pw_multi_aff_free(pma2
);
4720 /* Subtract the initial "n" elements in "ma" with coefficients in "c" and
4721 * denominator "denom".
4722 * "denom" is allowed to be negative, in which case the actual denominator
4723 * is -denom and the expressions are added instead.
4725 static __isl_give isl_aff
*subtract_initial(__isl_take isl_aff
*aff
,
4726 __isl_keep isl_multi_aff
*ma
, int n
, isl_int
*c
, isl_int denom
)
4732 first
= isl_seq_first_non_zero(c
, n
);
4736 sign
= isl_int_sgn(denom
);
4738 isl_int_abs(d
, denom
);
4739 for (i
= first
; i
< n
; ++i
) {
4742 if (isl_int_is_zero(c
[i
]))
4744 aff_i
= isl_multi_aff_get_aff(ma
, i
);
4745 aff_i
= isl_aff_scale(aff_i
, c
[i
]);
4746 aff_i
= isl_aff_scale_down(aff_i
, d
);
4748 aff
= isl_aff_sub(aff
, aff_i
);
4750 aff
= isl_aff_add(aff
, aff_i
);
4757 /* Extract an affine expression that expresses the output dimension "pos"
4758 * of "bmap" in terms of the parameters and input dimensions from
4760 * Note that this expression may involve integer divisions defined
4761 * in terms of parameters and input dimensions.
4762 * The equality may also involve references to earlier (but not later)
4763 * output dimensions. These are replaced by the corresponding elements
4766 * If the equality is of the form
4768 * f(i) + h(j) + a x + g(i) = 0,
4770 * with f(i) a linear combinations of the parameters and input dimensions,
4771 * g(i) a linear combination of integer divisions defined in terms of the same
4772 * and h(j) a linear combinations of earlier output dimensions,
4773 * then the affine expression is
4775 * (-f(i) - g(i))/a - h(j)/a
4777 * If the equality is of the form
4779 * f(i) + h(j) - a x + g(i) = 0,
4781 * then the affine expression is
4783 * (f(i) + g(i))/a - h(j)/(-a)
4786 * If "div" refers to an integer division (i.e., it is smaller than
4787 * the number of integer divisions), then the equality constraint
4788 * does involve an integer division (the one at position "div") that
4789 * is defined in terms of output dimensions. However, this integer
4790 * division can be eliminated by exploiting a pair of constraints
4791 * x >= l and x <= l + n, with n smaller than the coefficient of "div"
4792 * in the equality constraint. "ineq" refers to inequality x >= l, i.e.,
4794 * In particular, let
4796 * x = e(i) + m floor(...)
4798 * with e(i) the expression derived above and floor(...) the integer
4799 * division involving output dimensions.
4810 * e(i) + m floor(...) - l = (e(i) + m floor(...) - l) mod m
4811 * = (e(i) - l) mod m
4815 * x - l = (e(i) - l) mod m
4819 * x = ((e(i) - l) mod m) + l
4821 * The variable "shift" below contains the expression -l, which may
4822 * also involve a linear combination of earlier output dimensions.
4824 static __isl_give isl_aff
*extract_aff_from_equality(
4825 __isl_keep isl_basic_map
*bmap
, int pos
, int eq
, int div
, int ineq
,
4826 __isl_keep isl_multi_aff
*ma
)
4829 isl_size n_div
, n_out
;
4831 isl_local_space
*ls
;
4832 isl_aff
*aff
, *shift
;
4835 ctx
= isl_basic_map_get_ctx(bmap
);
4836 ls
= isl_basic_map_get_local_space(bmap
);
4837 ls
= isl_local_space_domain(ls
);
4838 aff
= isl_aff_alloc(isl_local_space_copy(ls
));
4841 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
4842 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4843 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4844 if (n_out
< 0 || n_div
< 0)
4846 if (isl_int_is_neg(bmap
->eq
[eq
][o_out
+ pos
])) {
4847 isl_seq_cpy(aff
->v
->el
+ 1, bmap
->eq
[eq
], o_out
);
4848 isl_seq_cpy(aff
->v
->el
+ 1 + o_out
,
4849 bmap
->eq
[eq
] + o_out
+ n_out
, n_div
);
4851 isl_seq_neg(aff
->v
->el
+ 1, bmap
->eq
[eq
], o_out
);
4852 isl_seq_neg(aff
->v
->el
+ 1 + o_out
,
4853 bmap
->eq
[eq
] + o_out
+ n_out
, n_div
);
4856 isl_int_set_si(aff
->v
->el
[1 + o_out
+ div
], 0);
4857 isl_int_abs(aff
->v
->el
[0], bmap
->eq
[eq
][o_out
+ pos
]);
4858 aff
= subtract_initial(aff
, ma
, pos
, bmap
->eq
[eq
] + o_out
,
4859 bmap
->eq
[eq
][o_out
+ pos
]);
4861 shift
= isl_aff_alloc(isl_local_space_copy(ls
));
4864 isl_seq_cpy(shift
->v
->el
+ 1, bmap
->ineq
[ineq
], o_out
);
4865 isl_seq_cpy(shift
->v
->el
+ 1 + o_out
,
4866 bmap
->ineq
[ineq
] + o_out
+ n_out
, n_div
);
4867 isl_int_set_si(shift
->v
->el
[0], 1);
4868 shift
= subtract_initial(shift
, ma
, pos
,
4869 bmap
->ineq
[ineq
] + o_out
, ctx
->negone
);
4870 aff
= isl_aff_add(aff
, isl_aff_copy(shift
));
4871 mod
= isl_val_int_from_isl_int(ctx
,
4872 bmap
->eq
[eq
][o_out
+ n_out
+ div
]);
4873 mod
= isl_val_abs(mod
);
4874 aff
= isl_aff_mod_val(aff
, mod
);
4875 aff
= isl_aff_sub(aff
, shift
);
4878 isl_local_space_free(ls
);
4881 isl_local_space_free(ls
);
4886 /* Given a basic map with output dimensions defined
4887 * in terms of the parameters input dimensions and earlier
4888 * output dimensions using an equality (and possibly a pair on inequalities),
4889 * extract an isl_aff that expresses output dimension "pos" in terms
4890 * of the parameters and input dimensions.
4891 * Note that this expression may involve integer divisions defined
4892 * in terms of parameters and input dimensions.
4893 * "ma" contains the expressions corresponding to earlier output dimensions.
4895 * This function shares some similarities with
4896 * isl_basic_map_has_defining_equality and isl_constraint_get_bound.
4898 static __isl_give isl_aff
*extract_isl_aff_from_basic_map(
4899 __isl_keep isl_basic_map
*bmap
, int pos
, __isl_keep isl_multi_aff
*ma
)
4906 eq
= isl_basic_map_output_defining_equality(bmap
, pos
, &div
, &ineq
);
4907 if (eq
>= bmap
->n_eq
)
4908 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
4909 "unable to find suitable equality", return NULL
);
4910 aff
= extract_aff_from_equality(bmap
, pos
, eq
, div
, ineq
, ma
);
4912 aff
= isl_aff_remove_unused_divs(aff
);
4916 /* Given a basic map where each output dimension is defined
4917 * in terms of the parameters and input dimensions using an equality,
4918 * extract an isl_multi_aff that expresses the output dimensions in terms
4919 * of the parameters and input dimensions.
4921 static __isl_give isl_multi_aff
*extract_isl_multi_aff_from_basic_map(
4922 __isl_take isl_basic_map
*bmap
)
4931 ma
= isl_multi_aff_alloc(isl_basic_map_get_space(bmap
));
4932 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4934 ma
= isl_multi_aff_free(ma
);
4936 for (i
= 0; i
< n_out
; ++i
) {
4939 aff
= extract_isl_aff_from_basic_map(bmap
, i
, ma
);
4940 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
4943 isl_basic_map_free(bmap
);
4948 /* Given a basic set where each set dimension is defined
4949 * in terms of the parameters using an equality,
4950 * extract an isl_multi_aff that expresses the set dimensions in terms
4951 * of the parameters.
4953 __isl_give isl_multi_aff
*isl_multi_aff_from_basic_set_equalities(
4954 __isl_take isl_basic_set
*bset
)
4956 return extract_isl_multi_aff_from_basic_map(bset
);
4959 /* Create an isl_pw_multi_aff that is equivalent to
4960 * isl_map_intersect_domain(isl_map_from_basic_map(bmap), domain).
4961 * The given basic map is such that each output dimension is defined
4962 * in terms of the parameters and input dimensions using an equality.
4964 * Since some applications expect the result of isl_pw_multi_aff_from_map
4965 * to only contain integer affine expressions, we compute the floor
4966 * of the expression before returning.
4968 * Remove all constraints involving local variables without
4969 * an explicit representation (resulting in the removal of those
4970 * local variables) prior to the actual extraction to ensure
4971 * that the local spaces in which the resulting affine expressions
4972 * are created do not contain any unknown local variables.
4973 * Removing such constraints is safe because constraints involving
4974 * unknown local variables are not used to determine whether
4975 * a basic map is obviously single-valued.
4977 static __isl_give isl_pw_multi_aff
*plain_pw_multi_aff_from_map(
4978 __isl_take isl_set
*domain
, __isl_take isl_basic_map
*bmap
)
4982 bmap
= isl_basic_map_drop_constraints_involving_unknown_divs(bmap
);
4983 ma
= extract_isl_multi_aff_from_basic_map(bmap
);
4984 ma
= isl_multi_aff_floor(ma
);
4985 return isl_pw_multi_aff_alloc(domain
, ma
);
4988 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4989 * This obviously only works if the input "map" is single-valued.
4990 * If so, we compute the lexicographic minimum of the image in the form
4991 * of an isl_pw_multi_aff. Since the image is unique, it is equal
4992 * to its lexicographic minimum.
4993 * If the input is not single-valued, we produce an error.
4995 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_base(
4996 __isl_take isl_map
*map
)
5000 isl_pw_multi_aff
*pma
;
5002 sv
= isl_map_is_single_valued(map
);
5006 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
5007 "map is not single-valued", goto error
);
5008 map
= isl_map_make_disjoint(map
);
5012 pma
= isl_pw_multi_aff_empty(isl_map_get_space(map
));
5014 for (i
= 0; i
< map
->n
; ++i
) {
5015 isl_pw_multi_aff
*pma_i
;
5016 isl_basic_map
*bmap
;
5017 bmap
= isl_basic_map_copy(map
->p
[i
]);
5018 pma_i
= isl_basic_map_lexmin_pw_multi_aff(bmap
);
5019 pma
= isl_pw_multi_aff_add_disjoint(pma
, pma_i
);
5029 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
5030 * taking into account that the output dimension at position "d"
5031 * can be represented as
5033 * x = floor((e(...) + c1) / m)
5035 * given that constraint "i" is of the form
5037 * e(...) + c1 - m x >= 0
5040 * Let "map" be of the form
5044 * We construct a mapping
5046 * A -> [A -> x = floor(...)]
5048 * apply that to the map, obtaining
5050 * [A -> x = floor(...)] -> B
5052 * and equate dimension "d" to x.
5053 * We then compute a isl_pw_multi_aff representation of the resulting map
5054 * and plug in the mapping above.
5056 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_div(
5057 __isl_take isl_map
*map
, __isl_take isl_basic_map
*hull
, int d
, int i
)
5060 isl_space
*space
= NULL
;
5061 isl_local_space
*ls
;
5069 isl_pw_multi_aff
*pma
;
5072 is_set
= isl_map_is_set(map
);
5076 offset
= isl_basic_map_offset(hull
, isl_dim_out
);
5077 ctx
= isl_map_get_ctx(map
);
5078 space
= isl_space_domain(isl_map_get_space(map
));
5079 n_in
= isl_space_dim(space
, isl_dim_set
);
5080 n
= isl_space_dim(space
, isl_dim_all
);
5081 if (n_in
< 0 || n
< 0)
5084 v
= isl_vec_alloc(ctx
, 1 + 1 + n
);
5086 isl_int_neg(v
->el
[0], hull
->ineq
[i
][offset
+ d
]);
5087 isl_seq_cpy(v
->el
+ 1, hull
->ineq
[i
], 1 + n
);
5089 isl_basic_map_free(hull
);
5091 ls
= isl_local_space_from_space(isl_space_copy(space
));
5092 aff
= isl_aff_alloc_vec(ls
, v
);
5093 aff
= isl_aff_floor(aff
);
5095 isl_space_free(space
);
5096 ma
= isl_multi_aff_from_aff(aff
);
5098 ma
= isl_multi_aff_identity(isl_space_map_from_set(space
));
5099 ma
= isl_multi_aff_range_product(ma
,
5100 isl_multi_aff_from_aff(aff
));
5103 insert
= isl_map_from_multi_aff_internal(isl_multi_aff_copy(ma
));
5104 map
= isl_map_apply_domain(map
, insert
);
5105 map
= isl_map_equate(map
, isl_dim_in
, n_in
, isl_dim_out
, d
);
5106 pma
= isl_pw_multi_aff_from_map(map
);
5107 pma
= isl_pw_multi_aff_pullback_multi_aff(pma
, ma
);
5111 isl_space_free(space
);
5113 isl_basic_map_free(hull
);
5117 /* Is constraint "c" of the form
5119 * e(...) + c1 - m x >= 0
5123 * -e(...) + c2 + m x >= 0
5125 * where m > 1 and e only depends on parameters and input dimensions?
5127 * "offset" is the offset of the output dimensions
5128 * "pos" is the position of output dimension x.
5130 static int is_potential_div_constraint(isl_int
*c
, int offset
, int d
, int total
)
5132 if (isl_int_is_zero(c
[offset
+ d
]))
5134 if (isl_int_is_one(c
[offset
+ d
]))
5136 if (isl_int_is_negone(c
[offset
+ d
]))
5138 if (isl_seq_first_non_zero(c
+ offset
, d
) != -1)
5140 if (isl_seq_first_non_zero(c
+ offset
+ d
+ 1,
5141 total
- (offset
+ d
+ 1)) != -1)
5146 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5148 * As a special case, we first check if there is any pair of constraints,
5149 * shared by all the basic maps in "map" that force a given dimension
5150 * to be equal to the floor of some affine combination of the input dimensions.
5152 * In particular, if we can find two constraints
5154 * e(...) + c1 - m x >= 0 i.e., m x <= e(...) + c1
5158 * -e(...) + c2 + m x >= 0 i.e., m x >= e(...) - c2
5160 * where m > 1 and e only depends on parameters and input dimensions,
5163 * c1 + c2 < m i.e., -c2 >= c1 - (m - 1)
5165 * then we know that we can take
5167 * x = floor((e(...) + c1) / m)
5169 * without having to perform any computation.
5171 * Note that we know that
5175 * If c1 + c2 were 0, then we would have detected an equality during
5176 * simplification. If c1 + c2 were negative, then we would have detected
5179 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_check_div(
5180 __isl_take isl_map
*map
)
5188 isl_basic_map
*hull
;
5190 hull
= isl_map_unshifted_simple_hull(isl_map_copy(map
));
5191 dim
= isl_map_dim(map
, isl_dim_out
);
5192 total
= isl_basic_map_dim(hull
, isl_dim_all
);
5193 if (dim
< 0 || total
< 0)
5197 offset
= isl_basic_map_offset(hull
, isl_dim_out
);
5199 for (d
= 0; d
< dim
; ++d
) {
5200 for (i
= 0; i
< n
; ++i
) {
5201 if (!is_potential_div_constraint(hull
->ineq
[i
],
5202 offset
, d
, 1 + total
))
5204 for (j
= i
+ 1; j
< n
; ++j
) {
5205 if (!isl_seq_is_neg(hull
->ineq
[i
] + 1,
5206 hull
->ineq
[j
] + 1, total
))
5208 isl_int_add(sum
, hull
->ineq
[i
][0],
5210 if (isl_int_abs_lt(sum
,
5211 hull
->ineq
[i
][offset
+ d
]))
5218 if (isl_int_is_pos(hull
->ineq
[j
][offset
+ d
]))
5220 return pw_multi_aff_from_map_div(map
, hull
, d
, j
);
5224 isl_basic_map_free(hull
);
5225 return pw_multi_aff_from_map_base(map
);
5228 isl_basic_map_free(hull
);
5232 /* Given an affine expression
5234 * [A -> B] -> f(A,B)
5236 * construct an isl_multi_aff
5240 * such that dimension "d" in B' is set to "aff" and the remaining
5241 * dimensions are set equal to the corresponding dimensions in B.
5242 * "n_in" is the dimension of the space A.
5243 * "n_out" is the dimension of the space B.
5245 * If "is_set" is set, then the affine expression is of the form
5249 * and we construct an isl_multi_aff
5253 static __isl_give isl_multi_aff
*range_map(__isl_take isl_aff
*aff
, int d
,
5254 unsigned n_in
, unsigned n_out
, int is_set
)
5258 isl_space
*space
, *space2
;
5259 isl_local_space
*ls
;
5261 space
= isl_aff_get_domain_space(aff
);
5262 ls
= isl_local_space_from_space(isl_space_copy(space
));
5263 space2
= isl_space_copy(space
);
5265 space2
= isl_space_range(isl_space_unwrap(space2
));
5266 space
= isl_space_map_from_domain_and_range(space
, space2
);
5267 ma
= isl_multi_aff_alloc(space
);
5268 ma
= isl_multi_aff_set_aff(ma
, d
, aff
);
5270 for (i
= 0; i
< n_out
; ++i
) {
5273 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
5274 isl_dim_set
, n_in
+ i
);
5275 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
5278 isl_local_space_free(ls
);
5283 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
5284 * taking into account that the dimension at position "d" can be written as
5286 * x = m a + f(..) (1)
5288 * where m is equal to "gcd".
5289 * "i" is the index of the equality in "hull" that defines f(..).
5290 * In particular, the equality is of the form
5292 * f(..) - x + m g(existentials) = 0
5296 * -f(..) + x + m g(existentials) = 0
5298 * We basically plug (1) into "map", resulting in a map with "a"
5299 * in the range instead of "x". The corresponding isl_pw_multi_aff
5300 * defining "a" is then plugged back into (1) to obtain a definition for "x".
5302 * Specifically, given the input map
5306 * We first wrap it into a set
5310 * and define (1) on top of the corresponding space, resulting in "aff".
5311 * We use this to create an isl_multi_aff that maps the output position "d"
5312 * from "a" to "x", leaving all other (intput and output) dimensions unchanged.
5313 * We plug this into the wrapped map, unwrap the result and compute the
5314 * corresponding isl_pw_multi_aff.
5315 * The result is an expression
5323 * so that we can plug that into "aff", after extending the latter to
5329 * If "map" is actually a set, then there is no "A" space, meaning
5330 * that we do not need to perform any wrapping, and that the result
5331 * of the recursive call is of the form
5335 * which is plugged into a mapping of the form
5339 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_stride(
5340 __isl_take isl_map
*map
, __isl_take isl_basic_map
*hull
, int d
, int i
,
5345 isl_local_space
*ls
;
5348 isl_pw_multi_aff
*pma
, *id
;
5354 is_set
= isl_map_is_set(map
);
5358 n_in
= isl_basic_map_dim(hull
, isl_dim_in
);
5359 n_out
= isl_basic_map_dim(hull
, isl_dim_out
);
5360 if (n_in
< 0 || n_out
< 0)
5362 o_out
= isl_basic_map_offset(hull
, isl_dim_out
);
5367 set
= isl_map_wrap(map
);
5368 space
= isl_space_map_from_set(isl_set_get_space(set
));
5369 ma
= isl_multi_aff_identity(space
);
5370 ls
= isl_local_space_from_space(isl_set_get_space(set
));
5371 aff
= isl_aff_alloc(ls
);
5373 isl_int_set_si(aff
->v
->el
[0], 1);
5374 if (isl_int_is_one(hull
->eq
[i
][o_out
+ d
]))
5375 isl_seq_neg(aff
->v
->el
+ 1, hull
->eq
[i
],
5378 isl_seq_cpy(aff
->v
->el
+ 1, hull
->eq
[i
],
5380 isl_int_set(aff
->v
->el
[1 + o_out
+ d
], gcd
);
5382 ma
= isl_multi_aff_set_aff(ma
, n_in
+ d
, isl_aff_copy(aff
));
5383 set
= isl_set_preimage_multi_aff(set
, ma
);
5385 ma
= range_map(aff
, d
, n_in
, n_out
, is_set
);
5390 map
= isl_set_unwrap(set
);
5391 pma
= isl_pw_multi_aff_from_map(map
);
5394 space
= isl_pw_multi_aff_get_domain_space(pma
);
5395 space
= isl_space_map_from_set(space
);
5396 id
= isl_pw_multi_aff_identity(space
);
5397 pma
= isl_pw_multi_aff_range_product(id
, pma
);
5399 id
= isl_pw_multi_aff_from_multi_aff(ma
);
5400 pma
= isl_pw_multi_aff_pullback_pw_multi_aff(id
, pma
);
5402 isl_basic_map_free(hull
);
5406 isl_basic_map_free(hull
);
5410 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5411 * "hull" contains the equalities valid for "map".
5413 * Check if any of the output dimensions is "strided".
5414 * That is, we check if it can be written as
5418 * with m greater than 1, a some combination of existentially quantified
5419 * variables and f an expression in the parameters and input dimensions.
5420 * If so, we remove the stride in pw_multi_aff_from_map_stride.
5422 * Otherwise, we continue with pw_multi_aff_from_map_check_div for a further
5425 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_check_strides(
5426 __isl_take isl_map
*map
, __isl_take isl_basic_map
*hull
)
5435 n_div
= isl_basic_map_dim(hull
, isl_dim_div
);
5436 n_out
= isl_basic_map_dim(hull
, isl_dim_out
);
5437 if (n_div
< 0 || n_out
< 0)
5441 isl_basic_map_free(hull
);
5442 return pw_multi_aff_from_map_check_div(map
);
5447 o_div
= isl_basic_map_offset(hull
, isl_dim_div
);
5448 o_out
= isl_basic_map_offset(hull
, isl_dim_out
);
5450 for (i
= 0; i
< n_out
; ++i
) {
5451 for (j
= 0; j
< hull
->n_eq
; ++j
) {
5452 isl_int
*eq
= hull
->eq
[j
];
5453 isl_pw_multi_aff
*res
;
5455 if (!isl_int_is_one(eq
[o_out
+ i
]) &&
5456 !isl_int_is_negone(eq
[o_out
+ i
]))
5458 if (isl_seq_first_non_zero(eq
+ o_out
, i
) != -1)
5460 if (isl_seq_first_non_zero(eq
+ o_out
+ i
+ 1,
5461 n_out
- (i
+ 1)) != -1)
5463 isl_seq_gcd(eq
+ o_div
, n_div
, &gcd
);
5464 if (isl_int_is_zero(gcd
))
5466 if (isl_int_is_one(gcd
))
5469 res
= pw_multi_aff_from_map_stride(map
, hull
,
5477 isl_basic_map_free(hull
);
5478 return pw_multi_aff_from_map_check_div(map
);
5481 isl_basic_map_free(hull
);
5485 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5487 * As a special case, we first check if all output dimensions are uniquely
5488 * defined in terms of the parameters and input dimensions over the entire
5489 * domain. If so, we extract the desired isl_pw_multi_aff directly
5490 * from the affine hull of "map" and its domain.
5492 * Otherwise, continue with pw_multi_aff_from_map_check_strides for more
5495 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_map(__isl_take isl_map
*map
)
5499 isl_basic_map
*hull
;
5501 n
= isl_map_n_basic_map(map
);
5506 hull
= isl_map_unshifted_simple_hull(isl_map_copy(map
));
5507 hull
= isl_basic_map_plain_affine_hull(hull
);
5508 sv
= isl_basic_map_plain_is_single_valued(hull
);
5510 return plain_pw_multi_aff_from_map(isl_map_domain(map
),
5512 isl_basic_map_free(hull
);
5514 map
= isl_map_detect_equalities(map
);
5515 hull
= isl_map_unshifted_simple_hull(isl_map_copy(map
));
5516 sv
= isl_basic_map_plain_is_single_valued(hull
);
5518 return plain_pw_multi_aff_from_map(isl_map_domain(map
), hull
);
5520 return pw_multi_aff_from_map_check_strides(map
, hull
);
5521 isl_basic_map_free(hull
);
5527 /* This function performs the same operation as isl_pw_multi_aff_from_map,
5528 * but is considered as a function on an isl_map when exported.
5530 __isl_give isl_pw_multi_aff
*isl_map_as_pw_multi_aff(__isl_take isl_map
*map
)
5532 return isl_pw_multi_aff_from_map(map
);
5535 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_set(__isl_take isl_set
*set
)
5537 return isl_pw_multi_aff_from_map(set
);
5540 /* This function performs the same operation as isl_pw_multi_aff_from_set,
5541 * but is considered as a function on an isl_set when exported.
5543 __isl_give isl_pw_multi_aff
*isl_set_as_pw_multi_aff(__isl_take isl_set
*set
)
5545 return isl_pw_multi_aff_from_set(set
);
5548 /* Convert "map" into an isl_pw_multi_aff (if possible) and
5551 static isl_stat
pw_multi_aff_from_map(__isl_take isl_map
*map
, void *user
)
5553 isl_union_pw_multi_aff
**upma
= user
;
5554 isl_pw_multi_aff
*pma
;
5556 pma
= isl_pw_multi_aff_from_map(map
);
5557 *upma
= isl_union_pw_multi_aff_add_pw_multi_aff(*upma
, pma
);
5559 return *upma
? isl_stat_ok
: isl_stat_error
;
5562 /* Create an isl_union_pw_multi_aff with the given isl_aff on a universe
5565 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_aff(
5566 __isl_take isl_aff
*aff
)
5569 isl_pw_multi_aff
*pma
;
5571 ma
= isl_multi_aff_from_aff(aff
);
5572 pma
= isl_pw_multi_aff_from_multi_aff(ma
);
5573 return isl_union_pw_multi_aff_from_pw_multi_aff(pma
);
5576 /* Try and create an isl_union_pw_multi_aff that is equivalent
5577 * to the given isl_union_map.
5578 * The isl_union_map is required to be single-valued in each space.
5579 * Otherwise, an error is produced.
5581 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_union_map(
5582 __isl_take isl_union_map
*umap
)
5585 isl_union_pw_multi_aff
*upma
;
5587 space
= isl_union_map_get_space(umap
);
5588 upma
= isl_union_pw_multi_aff_empty(space
);
5589 if (isl_union_map_foreach_map(umap
, &pw_multi_aff_from_map
, &upma
) < 0)
5590 upma
= isl_union_pw_multi_aff_free(upma
);
5591 isl_union_map_free(umap
);
5596 /* This function performs the same operation as
5597 * isl_union_pw_multi_aff_from_union_map,
5598 * but is considered as a function on an isl_union_map when exported.
5600 __isl_give isl_union_pw_multi_aff
*isl_union_map_as_union_pw_multi_aff(
5601 __isl_take isl_union_map
*umap
)
5603 return isl_union_pw_multi_aff_from_union_map(umap
);
5606 /* Try and create an isl_union_pw_multi_aff that is equivalent
5607 * to the given isl_union_set.
5608 * The isl_union_set is required to be a singleton in each space.
5609 * Otherwise, an error is produced.
5611 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_union_set(
5612 __isl_take isl_union_set
*uset
)
5614 return isl_union_pw_multi_aff_from_union_map(uset
);
5617 /* Return the piecewise affine expression "set ? 1 : 0".
5619 __isl_give isl_pw_aff
*isl_set_indicator_function(__isl_take isl_set
*set
)
5622 isl_space
*space
= isl_set_get_space(set
);
5623 isl_local_space
*ls
= isl_local_space_from_space(space
);
5624 isl_aff
*zero
= isl_aff_zero_on_domain(isl_local_space_copy(ls
));
5625 isl_aff
*one
= isl_aff_zero_on_domain(ls
);
5627 one
= isl_aff_add_constant_si(one
, 1);
5628 pa
= isl_pw_aff_alloc(isl_set_copy(set
), one
);
5629 set
= isl_set_complement(set
);
5630 pa
= isl_pw_aff_add_disjoint(pa
, isl_pw_aff_alloc(set
, zero
));
5635 /* Plug in "subs" for dimension "type", "pos" of "aff".
5637 * Let i be the dimension to replace and let "subs" be of the form
5641 * and "aff" of the form
5647 * (a f + d g')/(m d)
5649 * where g' is the result of plugging in "subs" in each of the integer
5652 __isl_give isl_aff
*isl_aff_substitute(__isl_take isl_aff
*aff
,
5653 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
5659 aff
= isl_aff_cow(aff
);
5661 return isl_aff_free(aff
);
5663 ctx
= isl_aff_get_ctx(aff
);
5664 if (!isl_space_is_equal(aff
->ls
->dim
, subs
->ls
->dim
))
5665 isl_die(ctx
, isl_error_invalid
,
5666 "spaces don't match", return isl_aff_free(aff
));
5667 n_div
= isl_aff_domain_dim(subs
, isl_dim_div
);
5669 return isl_aff_free(aff
);
5671 isl_die(ctx
, isl_error_unsupported
,
5672 "cannot handle divs yet", return isl_aff_free(aff
));
5674 aff
->ls
= isl_local_space_substitute(aff
->ls
, type
, pos
, subs
);
5676 return isl_aff_free(aff
);
5678 aff
->v
= isl_vec_cow(aff
->v
);
5680 return isl_aff_free(aff
);
5682 pos
+= isl_local_space_offset(aff
->ls
, type
);
5685 isl_seq_substitute(aff
->v
->el
, pos
, subs
->v
->el
,
5686 aff
->v
->size
, subs
->v
->size
, v
);
5692 /* Plug in "subs" for dimension "type", "pos" in each of the affine
5693 * expressions in "maff".
5695 __isl_give isl_multi_aff
*isl_multi_aff_substitute(
5696 __isl_take isl_multi_aff
*maff
, enum isl_dim_type type
, unsigned pos
,
5697 __isl_keep isl_aff
*subs
)
5701 maff
= isl_multi_aff_cow(maff
);
5703 return isl_multi_aff_free(maff
);
5705 if (type
== isl_dim_in
)
5708 for (i
= 0; i
< maff
->n
; ++i
) {
5709 maff
->u
.p
[i
] = isl_aff_substitute(maff
->u
.p
[i
],
5712 return isl_multi_aff_free(maff
);
5718 /* Plug in "subs" for dimension "type", "pos" of "pma".
5720 * pma is of the form
5724 * while subs is of the form
5726 * v' = B_j(v) -> S_j
5728 * Each pair i,j such that C_ij = A_i \cap B_i is non-empty
5729 * has a contribution in the result, in particular
5731 * C_ij(S_j) -> M_i(S_j)
5733 * Note that plugging in S_j in C_ij may also result in an empty set
5734 * and this contribution should simply be discarded.
5736 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_substitute(
5737 __isl_take isl_pw_multi_aff
*pma
, enum isl_dim_type type
, unsigned pos
,
5738 __isl_keep isl_pw_aff
*subs
)
5741 isl_pw_multi_aff
*res
;
5744 return isl_pw_multi_aff_free(pma
);
5746 n
= pma
->n
* subs
->n
;
5747 res
= isl_pw_multi_aff_alloc_size(isl_space_copy(pma
->dim
), n
);
5749 for (i
= 0; i
< pma
->n
; ++i
) {
5750 for (j
= 0; j
< subs
->n
; ++j
) {
5752 isl_multi_aff
*res_ij
;
5755 common
= isl_set_intersect(
5756 isl_set_copy(pma
->p
[i
].set
),
5757 isl_set_copy(subs
->p
[j
].set
));
5758 common
= isl_set_substitute(common
,
5759 type
, pos
, subs
->p
[j
].aff
);
5760 empty
= isl_set_plain_is_empty(common
);
5761 if (empty
< 0 || empty
) {
5762 isl_set_free(common
);
5768 res_ij
= isl_multi_aff_substitute(
5769 isl_multi_aff_copy(pma
->p
[i
].maff
),
5770 type
, pos
, subs
->p
[j
].aff
);
5772 res
= isl_pw_multi_aff_add_piece(res
, common
, res_ij
);
5776 isl_pw_multi_aff_free(pma
);
5779 isl_pw_multi_aff_free(pma
);
5780 isl_pw_multi_aff_free(res
);
5784 /* Compute the preimage of a range of dimensions in the affine expression "src"
5785 * under "ma" and put the result in "dst". The number of dimensions in "src"
5786 * that precede the range is given by "n_before". The number of dimensions
5787 * in the range is given by the number of output dimensions of "ma".
5788 * The number of dimensions that follow the range is given by "n_after".
5789 * If "has_denom" is set (to one),
5790 * then "src" and "dst" have an extra initial denominator.
5791 * "n_div_ma" is the number of existentials in "ma"
5792 * "n_div_bset" is the number of existentials in "src"
5793 * The resulting "dst" (which is assumed to have been allocated by
5794 * the caller) contains coefficients for both sets of existentials,
5795 * first those in "ma" and then those in "src".
5796 * f, c1, c2 and g are temporary objects that have been initialized
5799 * Let src represent the expression
5801 * (a(p) + f_u u + b v + f_w w + c(divs))/d
5803 * and let ma represent the expressions
5805 * v_i = (r_i(p) + s_i(y) + t_i(divs'))/m_i
5807 * We start out with the following expression for dst:
5809 * (a(p) + f_u u + 0 y + f_w w + 0 divs' + c(divs) + f \sum_i b_i v_i)/d
5811 * with the multiplication factor f initially equal to 1
5812 * and f \sum_i b_i v_i kept separately.
5813 * For each x_i that we substitute, we multiply the numerator
5814 * (and denominator) of dst by c_1 = m_i and add the numerator
5815 * of the x_i expression multiplied by c_2 = f b_i,
5816 * after removing the common factors of c_1 and c_2.
5817 * The multiplication factor f also needs to be multiplied by c_1
5818 * for the next x_j, j > i.
5820 isl_stat
isl_seq_preimage(isl_int
*dst
, isl_int
*src
,
5821 __isl_keep isl_multi_aff
*ma
, int n_before
, int n_after
,
5822 int n_div_ma
, int n_div_bmap
,
5823 isl_int f
, isl_int c1
, isl_int c2
, isl_int g
, int has_denom
)
5826 isl_size n_param
, n_in
, n_out
;
5829 n_param
= isl_multi_aff_dim(ma
, isl_dim_param
);
5830 n_in
= isl_multi_aff_dim(ma
, isl_dim_in
);
5831 n_out
= isl_multi_aff_dim(ma
, isl_dim_out
);
5832 if (n_param
< 0 || n_in
< 0 || n_out
< 0)
5833 return isl_stat_error
;
5835 isl_seq_cpy(dst
, src
, has_denom
+ 1 + n_param
+ n_before
);
5836 o_dst
= o_src
= has_denom
+ 1 + n_param
+ n_before
;
5837 isl_seq_clr(dst
+ o_dst
, n_in
);
5840 isl_seq_cpy(dst
+ o_dst
, src
+ o_src
, n_after
);
5843 isl_seq_clr(dst
+ o_dst
, n_div_ma
);
5845 isl_seq_cpy(dst
+ o_dst
, src
+ o_src
, n_div_bmap
);
5847 isl_int_set_si(f
, 1);
5849 for (i
= 0; i
< n_out
; ++i
) {
5850 int offset
= has_denom
+ 1 + n_param
+ n_before
+ i
;
5852 if (isl_int_is_zero(src
[offset
]))
5854 isl_int_set(c1
, ma
->u
.p
[i
]->v
->el
[0]);
5855 isl_int_mul(c2
, f
, src
[offset
]);
5856 isl_int_gcd(g
, c1
, c2
);
5857 isl_int_divexact(c1
, c1
, g
);
5858 isl_int_divexact(c2
, c2
, g
);
5860 isl_int_mul(f
, f
, c1
);
5863 isl_seq_combine(dst
+ o_dst
, c1
, dst
+ o_dst
,
5864 c2
, ma
->u
.p
[i
]->v
->el
+ o_src
, 1 + n_param
);
5865 o_dst
+= 1 + n_param
;
5866 o_src
+= 1 + n_param
;
5867 isl_seq_scale(dst
+ o_dst
, dst
+ o_dst
, c1
, n_before
);
5869 isl_seq_combine(dst
+ o_dst
, c1
, dst
+ o_dst
,
5870 c2
, ma
->u
.p
[i
]->v
->el
+ o_src
, n_in
);
5873 isl_seq_scale(dst
+ o_dst
, dst
+ o_dst
, c1
, n_after
);
5875 isl_seq_combine(dst
+ o_dst
, c1
, dst
+ o_dst
,
5876 c2
, ma
->u
.p
[i
]->v
->el
+ o_src
, n_div_ma
);
5879 isl_seq_scale(dst
+ o_dst
, dst
+ o_dst
, c1
, n_div_bmap
);
5881 isl_int_mul(dst
[0], dst
[0], c1
);
5887 /* Compute the pullback of "aff" by the function represented by "ma".
5888 * In other words, plug in "ma" in "aff". The result is an affine expression
5889 * defined over the domain space of "ma".
5891 * If "aff" is represented by
5893 * (a(p) + b x + c(divs))/d
5895 * and ma is represented by
5897 * x = D(p) + F(y) + G(divs')
5899 * then the result is
5901 * (a(p) + b D(p) + b F(y) + b G(divs') + c(divs))/d
5903 * The divs in the local space of the input are similarly adjusted
5904 * through a call to isl_local_space_preimage_multi_aff.
5906 __isl_give isl_aff
*isl_aff_pullback_multi_aff(__isl_take isl_aff
*aff
,
5907 __isl_take isl_multi_aff
*ma
)
5909 isl_aff
*res
= NULL
;
5910 isl_local_space
*ls
;
5911 isl_size n_div_aff
, n_div_ma
;
5912 isl_int f
, c1
, c2
, g
;
5914 ma
= isl_multi_aff_align_divs(ma
);
5918 n_div_aff
= isl_aff_dim(aff
, isl_dim_div
);
5919 n_div_ma
= ma
->n
? isl_aff_dim(ma
->u
.p
[0], isl_dim_div
) : 0;
5920 if (n_div_aff
< 0 || n_div_ma
< 0)
5923 ls
= isl_aff_get_domain_local_space(aff
);
5924 ls
= isl_local_space_preimage_multi_aff(ls
, isl_multi_aff_copy(ma
));
5925 res
= isl_aff_alloc(ls
);
5934 if (isl_seq_preimage(res
->v
->el
, aff
->v
->el
, ma
, 0, 0,
5935 n_div_ma
, n_div_aff
, f
, c1
, c2
, g
, 1) < 0)
5936 res
= isl_aff_free(res
);
5944 isl_multi_aff_free(ma
);
5945 res
= isl_aff_normalize(res
);
5949 isl_multi_aff_free(ma
);
5954 /* Compute the pullback of "aff1" by the function represented by "aff2".
5955 * In other words, plug in "aff2" in "aff1". The result is an affine expression
5956 * defined over the domain space of "aff1".
5958 * The domain of "aff1" should match the range of "aff2", which means
5959 * that it should be single-dimensional.
5961 __isl_give isl_aff
*isl_aff_pullback_aff(__isl_take isl_aff
*aff1
,
5962 __isl_take isl_aff
*aff2
)
5966 ma
= isl_multi_aff_from_aff(aff2
);
5967 return isl_aff_pullback_multi_aff(aff1
, ma
);
5970 /* Compute the pullback of "ma1" by the function represented by "ma2".
5971 * In other words, plug in "ma2" in "ma1".
5973 __isl_give isl_multi_aff
*isl_multi_aff_pullback_multi_aff(
5974 __isl_take isl_multi_aff
*ma1
, __isl_take isl_multi_aff
*ma2
)
5977 isl_space
*space
= NULL
;
5979 isl_multi_aff_align_params_bin(&ma1
, &ma2
);
5980 ma2
= isl_multi_aff_align_divs(ma2
);
5981 ma1
= isl_multi_aff_cow(ma1
);
5985 space
= isl_space_join(isl_multi_aff_get_space(ma2
),
5986 isl_multi_aff_get_space(ma1
));
5988 for (i
= 0; i
< ma1
->n
; ++i
) {
5989 ma1
->u
.p
[i
] = isl_aff_pullback_multi_aff(ma1
->u
.p
[i
],
5990 isl_multi_aff_copy(ma2
));
5995 ma1
= isl_multi_aff_reset_space(ma1
, space
);
5996 isl_multi_aff_free(ma2
);
5999 isl_space_free(space
);
6000 isl_multi_aff_free(ma2
);
6001 isl_multi_aff_free(ma1
);
6005 /* Extend the local space of "dst" to include the divs
6006 * in the local space of "src".
6008 * If "src" does not have any divs or if the local spaces of "dst" and
6009 * "src" are the same, then no extension is required.
6011 __isl_give isl_aff
*isl_aff_align_divs(__isl_take isl_aff
*dst
,
6012 __isl_keep isl_aff
*src
)
6015 isl_size src_n_div
, dst_n_div
;
6022 return isl_aff_free(dst
);
6024 ctx
= isl_aff_get_ctx(src
);
6025 equal
= isl_local_space_has_equal_space(src
->ls
, dst
->ls
);
6027 return isl_aff_free(dst
);
6029 isl_die(ctx
, isl_error_invalid
,
6030 "spaces don't match", goto error
);
6032 src_n_div
= isl_aff_domain_dim(src
, isl_dim_div
);
6033 dst_n_div
= isl_aff_domain_dim(dst
, isl_dim_div
);
6036 equal
= isl_local_space_is_equal(src
->ls
, dst
->ls
);
6037 if (equal
< 0 || src_n_div
< 0 || dst_n_div
< 0)
6038 return isl_aff_free(dst
);
6042 exp1
= isl_alloc_array(ctx
, int, src_n_div
);
6043 exp2
= isl_alloc_array(ctx
, int, dst_n_div
);
6044 if (!exp1
|| (dst_n_div
&& !exp2
))
6047 div
= isl_merge_divs(src
->ls
->div
, dst
->ls
->div
, exp1
, exp2
);
6048 dst
= isl_aff_expand_divs(dst
, div
, exp2
);
6056 return isl_aff_free(dst
);
6059 /* Adjust the local spaces of the affine expressions in "maff"
6060 * such that they all have the save divs.
6062 __isl_give isl_multi_aff
*isl_multi_aff_align_divs(
6063 __isl_take isl_multi_aff
*maff
)
6071 maff
= isl_multi_aff_cow(maff
);
6075 for (i
= 1; i
< maff
->n
; ++i
)
6076 maff
->u
.p
[0] = isl_aff_align_divs(maff
->u
.p
[0], maff
->u
.p
[i
]);
6077 for (i
= 1; i
< maff
->n
; ++i
) {
6078 maff
->u
.p
[i
] = isl_aff_align_divs(maff
->u
.p
[i
], maff
->u
.p
[0]);
6080 return isl_multi_aff_free(maff
);
6086 __isl_give isl_aff
*isl_aff_lift(__isl_take isl_aff
*aff
)
6088 aff
= isl_aff_cow(aff
);
6092 aff
->ls
= isl_local_space_lift(aff
->ls
);
6094 return isl_aff_free(aff
);
6099 /* Lift "maff" to a space with extra dimensions such that the result
6100 * has no more existentially quantified variables.
6101 * If "ls" is not NULL, then *ls is assigned the local space that lies
6102 * at the basis of the lifting applied to "maff".
6104 __isl_give isl_multi_aff
*isl_multi_aff_lift(__isl_take isl_multi_aff
*maff
,
6105 __isl_give isl_local_space
**ls
)
6119 isl_space
*space
= isl_multi_aff_get_domain_space(maff
);
6120 *ls
= isl_local_space_from_space(space
);
6122 return isl_multi_aff_free(maff
);
6127 maff
= isl_multi_aff_cow(maff
);
6128 maff
= isl_multi_aff_align_divs(maff
);
6132 n_div
= isl_aff_dim(maff
->u
.p
[0], isl_dim_div
);
6134 return isl_multi_aff_free(maff
);
6135 space
= isl_multi_aff_get_space(maff
);
6136 space
= isl_space_lift(isl_space_domain(space
), n_div
);
6137 space
= isl_space_extend_domain_with_range(space
,
6138 isl_multi_aff_get_space(maff
));
6140 return isl_multi_aff_free(maff
);
6141 isl_space_free(maff
->space
);
6142 maff
->space
= space
;
6145 *ls
= isl_aff_get_domain_local_space(maff
->u
.p
[0]);
6147 return isl_multi_aff_free(maff
);
6150 for (i
= 0; i
< maff
->n
; ++i
) {
6151 maff
->u
.p
[i
] = isl_aff_lift(maff
->u
.p
[i
]);
6159 isl_local_space_free(*ls
);
6160 return isl_multi_aff_free(maff
);
6164 #define TYPE isl_pw_multi_aff
6166 #include "check_type_range_templ.c"
6168 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma".
6170 __isl_give isl_pw_aff
*isl_pw_multi_aff_get_at(
6171 __isl_keep isl_pw_multi_aff
*pma
, int pos
)
6178 if (isl_pw_multi_aff_check_range(pma
, isl_dim_out
, pos
, 1) < 0)
6181 n_out
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
6185 space
= isl_pw_multi_aff_get_space(pma
);
6186 space
= isl_space_drop_dims(space
, isl_dim_out
,
6187 pos
+ 1, n_out
- pos
- 1);
6188 space
= isl_space_drop_dims(space
, isl_dim_out
, 0, pos
);
6190 pa
= isl_pw_aff_alloc_size(space
, pma
->n
);
6191 for (i
= 0; i
< pma
->n
; ++i
) {
6193 aff
= isl_multi_aff_get_aff(pma
->p
[i
].maff
, pos
);
6194 pa
= isl_pw_aff_add_piece(pa
, isl_set_copy(pma
->p
[i
].set
), aff
);
6200 /* This is an alternative name for the function above.
6202 __isl_give isl_pw_aff
*isl_pw_multi_aff_get_pw_aff(
6203 __isl_keep isl_pw_multi_aff
*pma
, int pos
)
6205 return isl_pw_multi_aff_get_at(pma
, pos
);
6208 /* Return an isl_pw_multi_aff with the given "set" as domain and
6209 * an unnamed zero-dimensional range.
6211 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_domain(
6212 __isl_take isl_set
*set
)
6217 space
= isl_set_get_space(set
);
6218 space
= isl_space_from_domain(space
);
6219 ma
= isl_multi_aff_zero(space
);
6220 return isl_pw_multi_aff_alloc(set
, ma
);
6223 /* Add an isl_pw_multi_aff with the given "set" as domain and
6224 * an unnamed zero-dimensional range to *user.
6226 static isl_stat
add_pw_multi_aff_from_domain(__isl_take isl_set
*set
,
6229 isl_union_pw_multi_aff
**upma
= user
;
6230 isl_pw_multi_aff
*pma
;
6232 pma
= isl_pw_multi_aff_from_domain(set
);
6233 *upma
= isl_union_pw_multi_aff_add_pw_multi_aff(*upma
, pma
);
6238 /* Return an isl_union_pw_multi_aff with the given "uset" as domain and
6239 * an unnamed zero-dimensional range.
6241 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_domain(
6242 __isl_take isl_union_set
*uset
)
6245 isl_union_pw_multi_aff
*upma
;
6250 space
= isl_union_set_get_space(uset
);
6251 upma
= isl_union_pw_multi_aff_empty(space
);
6253 if (isl_union_set_foreach_set(uset
,
6254 &add_pw_multi_aff_from_domain
, &upma
) < 0)
6257 isl_union_set_free(uset
);
6260 isl_union_set_free(uset
);
6261 isl_union_pw_multi_aff_free(upma
);
6265 /* Local data for bin_entry and the callback "fn".
6267 struct isl_union_pw_multi_aff_bin_data
{
6268 isl_union_pw_multi_aff
*upma2
;
6269 isl_union_pw_multi_aff
*res
;
6270 isl_pw_multi_aff
*pma
;
6271 isl_stat (*fn
)(__isl_take isl_pw_multi_aff
*pma
, void *user
);
6274 /* Given an isl_pw_multi_aff from upma1, store it in data->pma
6275 * and call data->fn for each isl_pw_multi_aff in data->upma2.
6277 static isl_stat
bin_entry(__isl_take isl_pw_multi_aff
*pma
, void *user
)
6279 struct isl_union_pw_multi_aff_bin_data
*data
= user
;
6283 r
= isl_union_pw_multi_aff_foreach_pw_multi_aff(data
->upma2
,
6285 isl_pw_multi_aff_free(pma
);
6290 /* Call "fn" on each pair of isl_pw_multi_affs in "upma1" and "upma2".
6291 * The isl_pw_multi_aff from upma1 is stored in data->pma (where data is
6292 * passed as user field) and the isl_pw_multi_aff from upma2 is available
6293 * as *entry. The callback should adjust data->res if desired.
6295 static __isl_give isl_union_pw_multi_aff
*bin_op(
6296 __isl_take isl_union_pw_multi_aff
*upma1
,
6297 __isl_take isl_union_pw_multi_aff
*upma2
,
6298 isl_stat (*fn
)(__isl_take isl_pw_multi_aff
*pma
, void *user
))
6301 struct isl_union_pw_multi_aff_bin_data data
= { NULL
, NULL
, NULL
, fn
};
6303 space
= isl_union_pw_multi_aff_get_space(upma2
);
6304 upma1
= isl_union_pw_multi_aff_align_params(upma1
, space
);
6305 space
= isl_union_pw_multi_aff_get_space(upma1
);
6306 upma2
= isl_union_pw_multi_aff_align_params(upma2
, space
);
6308 if (!upma1
|| !upma2
)
6312 data
.res
= isl_union_pw_multi_aff_alloc_same_size(upma1
);
6313 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma1
,
6314 &bin_entry
, &data
) < 0)
6317 isl_union_pw_multi_aff_free(upma1
);
6318 isl_union_pw_multi_aff_free(upma2
);
6321 isl_union_pw_multi_aff_free(upma1
);
6322 isl_union_pw_multi_aff_free(upma2
);
6323 isl_union_pw_multi_aff_free(data
.res
);
6327 /* Given two isl_pw_multi_affs A -> B and C -> D,
6328 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
6330 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_range_product(
6331 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
6335 isl_pw_multi_aff_align_params_bin(&pma1
, &pma2
);
6336 space
= isl_space_range_product(isl_pw_multi_aff_get_space(pma1
),
6337 isl_pw_multi_aff_get_space(pma2
));
6338 return isl_pw_multi_aff_on_shared_domain_in(pma1
, pma2
, space
,
6339 &isl_multi_aff_range_product
);
6342 /* Given two isl_pw_multi_affs A -> B and C -> D,
6343 * construct an isl_pw_multi_aff (A * C) -> (B, D).
6345 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_flat_range_product(
6346 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
6350 isl_pw_multi_aff_align_params_bin(&pma1
, &pma2
);
6351 space
= isl_space_range_product(isl_pw_multi_aff_get_space(pma1
),
6352 isl_pw_multi_aff_get_space(pma2
));
6353 space
= isl_space_flatten_range(space
);
6354 return isl_pw_multi_aff_on_shared_domain_in(pma1
, pma2
, space
,
6355 &isl_multi_aff_flat_range_product
);
6358 /* If data->pma and "pma2" have the same domain space, then use "range_product"
6359 * to compute some form of range product and add the result to data->res.
6361 static isl_stat
gen_range_product_entry(__isl_take isl_pw_multi_aff
*pma2
,
6362 __isl_give isl_pw_multi_aff
*(*range_product
)(
6363 __isl_take isl_pw_multi_aff
*pma1
,
6364 __isl_take isl_pw_multi_aff
*pma2
),
6367 struct isl_union_pw_multi_aff_bin_data
*data
= user
;
6369 isl_space
*space1
, *space2
;
6371 space1
= isl_pw_multi_aff_peek_space(data
->pma
);
6372 space2
= isl_pw_multi_aff_peek_space(pma2
);
6373 match
= isl_space_tuple_is_equal(space1
, isl_dim_in
,
6374 space2
, isl_dim_in
);
6375 if (match
< 0 || !match
) {
6376 isl_pw_multi_aff_free(pma2
);
6377 return match
< 0 ? isl_stat_error
: isl_stat_ok
;
6380 pma2
= range_product(isl_pw_multi_aff_copy(data
->pma
), pma2
);
6382 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma2
);
6387 /* If data->pma and "pma2" have the same domain space, then compute
6388 * their flat range product and add the result to data->res.
6390 static isl_stat
flat_range_product_entry(__isl_take isl_pw_multi_aff
*pma2
,
6393 return gen_range_product_entry(pma2
,
6394 &isl_pw_multi_aff_flat_range_product
, user
);
6397 /* Given two isl_union_pw_multi_affs A -> B and C -> D,
6398 * construct an isl_union_pw_multi_aff (A * C) -> (B, D).
6400 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_flat_range_product(
6401 __isl_take isl_union_pw_multi_aff
*upma1
,
6402 __isl_take isl_union_pw_multi_aff
*upma2
)
6404 return bin_op(upma1
, upma2
, &flat_range_product_entry
);
6407 /* If data->pma and "pma2" have the same domain space, then compute
6408 * their range product and add the result to data->res.
6410 static isl_stat
range_product_entry(__isl_take isl_pw_multi_aff
*pma2
,
6413 return gen_range_product_entry(pma2
,
6414 &isl_pw_multi_aff_range_product
, user
);
6417 /* Given two isl_union_pw_multi_affs A -> B and C -> D,
6418 * construct an isl_union_pw_multi_aff (A * C) -> [B -> D].
6420 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_range_product(
6421 __isl_take isl_union_pw_multi_aff
*upma1
,
6422 __isl_take isl_union_pw_multi_aff
*upma2
)
6424 return bin_op(upma1
, upma2
, &range_product_entry
);
6427 /* Replace the affine expressions at position "pos" in "pma" by "pa".
6428 * The parameters are assumed to have been aligned.
6430 * The implementation essentially performs an isl_pw_*_on_shared_domain,
6431 * except that it works on two different isl_pw_* types.
6433 static __isl_give isl_pw_multi_aff
*pw_multi_aff_set_pw_aff(
6434 __isl_take isl_pw_multi_aff
*pma
, unsigned pos
,
6435 __isl_take isl_pw_aff
*pa
)
6438 isl_pw_multi_aff
*res
= NULL
;
6443 if (!isl_space_tuple_is_equal(pma
->dim
, isl_dim_in
,
6444 pa
->dim
, isl_dim_in
))
6445 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
6446 "domains don't match", goto error
);
6447 if (isl_pw_multi_aff_check_range(pma
, isl_dim_out
, pos
, 1) < 0)
6451 res
= isl_pw_multi_aff_alloc_size(isl_pw_multi_aff_get_space(pma
), n
);
6453 for (i
= 0; i
< pma
->n
; ++i
) {
6454 for (j
= 0; j
< pa
->n
; ++j
) {
6456 isl_multi_aff
*res_ij
;
6459 common
= isl_set_intersect(isl_set_copy(pma
->p
[i
].set
),
6460 isl_set_copy(pa
->p
[j
].set
));
6461 empty
= isl_set_plain_is_empty(common
);
6462 if (empty
< 0 || empty
) {
6463 isl_set_free(common
);
6469 res_ij
= isl_multi_aff_set_aff(
6470 isl_multi_aff_copy(pma
->p
[i
].maff
), pos
,
6471 isl_aff_copy(pa
->p
[j
].aff
));
6472 res_ij
= isl_multi_aff_gist(res_ij
,
6473 isl_set_copy(common
));
6475 res
= isl_pw_multi_aff_add_piece(res
, common
, res_ij
);
6479 isl_pw_multi_aff_free(pma
);
6480 isl_pw_aff_free(pa
);
6483 isl_pw_multi_aff_free(pma
);
6484 isl_pw_aff_free(pa
);
6485 return isl_pw_multi_aff_free(res
);
6488 /* Replace the affine expressions at position "pos" in "pma" by "pa".
6490 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_set_pw_aff(
6491 __isl_take isl_pw_multi_aff
*pma
, unsigned pos
,
6492 __isl_take isl_pw_aff
*pa
)
6494 isl_bool equal_params
;
6498 equal_params
= isl_space_has_equal_params(pma
->dim
, pa
->dim
);
6499 if (equal_params
< 0)
6502 return pw_multi_aff_set_pw_aff(pma
, pos
, pa
);
6503 if (isl_pw_multi_aff_check_named_params(pma
) < 0 ||
6504 isl_pw_aff_check_named_params(pa
) < 0)
6506 pma
= isl_pw_multi_aff_align_params(pma
, isl_pw_aff_get_space(pa
));
6507 pa
= isl_pw_aff_align_params(pa
, isl_pw_multi_aff_get_space(pma
));
6508 return pw_multi_aff_set_pw_aff(pma
, pos
, pa
);
6510 isl_pw_multi_aff_free(pma
);
6511 isl_pw_aff_free(pa
);
6515 /* Do the parameters of "pa" match those of "space"?
6517 isl_bool
isl_pw_aff_matching_params(__isl_keep isl_pw_aff
*pa
,
6518 __isl_keep isl_space
*space
)
6520 isl_space
*pa_space
;
6524 return isl_bool_error
;
6526 pa_space
= isl_pw_aff_get_space(pa
);
6528 match
= isl_space_has_equal_params(space
, pa_space
);
6530 isl_space_free(pa_space
);
6534 /* Check that the domain space of "pa" matches "space".
6536 isl_stat
isl_pw_aff_check_match_domain_space(__isl_keep isl_pw_aff
*pa
,
6537 __isl_keep isl_space
*space
)
6539 isl_space
*pa_space
;
6543 return isl_stat_error
;
6545 pa_space
= isl_pw_aff_get_space(pa
);
6547 match
= isl_space_has_equal_params(space
, pa_space
);
6551 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
6552 "parameters don't match", goto error
);
6553 match
= isl_space_tuple_is_equal(space
, isl_dim_in
,
6554 pa_space
, isl_dim_in
);
6558 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
6559 "domains don't match", goto error
);
6560 isl_space_free(pa_space
);
6563 isl_space_free(pa_space
);
6564 return isl_stat_error
;
6572 #include <isl_multi_explicit_domain.c>
6573 #include <isl_multi_pw_aff_explicit_domain.c>
6574 #include <isl_multi_templ.c>
6575 #include <isl_multi_add_constant_templ.c>
6576 #include <isl_multi_apply_set.c>
6577 #include <isl_multi_arith_templ.c>
6578 #include <isl_multi_bind_templ.c>
6579 #include <isl_multi_bind_domain_templ.c>
6580 #include <isl_multi_coalesce.c>
6581 #include <isl_multi_domain_templ.c>
6582 #include <isl_multi_dim_id_templ.c>
6583 #include <isl_multi_dims.c>
6584 #include <isl_multi_from_base_templ.c>
6585 #include <isl_multi_gist.c>
6586 #include <isl_multi_hash.c>
6587 #include <isl_multi_identity_templ.c>
6588 #include <isl_multi_align_set.c>
6589 #include <isl_multi_insert_domain_templ.c>
6590 #include <isl_multi_intersect.c>
6591 #include <isl_multi_min_max_templ.c>
6592 #include <isl_multi_move_dims_templ.c>
6593 #include <isl_multi_nan_templ.c>
6594 #include <isl_multi_param_templ.c>
6595 #include <isl_multi_product_templ.c>
6596 #include <isl_multi_splice_templ.c>
6597 #include <isl_multi_tuple_id_templ.c>
6598 #include <isl_multi_union_add_templ.c>
6599 #include <isl_multi_zero_templ.c>
6600 #include <isl_multi_unbind_params_templ.c>
6602 /* Is every element of "mpa" defined over a single universe domain?
6604 isl_bool
isl_multi_pw_aff_isa_multi_aff(__isl_keep isl_multi_pw_aff
*mpa
)
6606 return isl_multi_pw_aff_every(mpa
, &isl_pw_aff_isa_aff
);
6609 /* Given that every element of "mpa" is defined over a single universe domain,
6610 * return the corresponding base expressions.
6612 __isl_give isl_multi_aff
*isl_multi_pw_aff_as_multi_aff(
6613 __isl_take isl_multi_pw_aff
*mpa
)
6619 n
= isl_multi_pw_aff_size(mpa
);
6621 mpa
= isl_multi_pw_aff_free(mpa
);
6622 ma
= isl_multi_aff_alloc(isl_multi_pw_aff_get_space(mpa
));
6623 for (i
= 0; i
< n
; ++i
) {
6626 aff
= isl_pw_aff_as_aff(isl_multi_pw_aff_get_at(mpa
, i
));
6627 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
6629 isl_multi_pw_aff_free(mpa
);
6633 /* If "mpa" has an explicit domain, then intersect the domain of "map"
6634 * with this explicit domain.
6636 __isl_give isl_map
*isl_map_intersect_multi_pw_aff_explicit_domain(
6637 __isl_take isl_map
*map
, __isl_keep isl_multi_pw_aff
*mpa
)
6641 if (!isl_multi_pw_aff_has_explicit_domain(mpa
))
6644 dom
= isl_multi_pw_aff_domain(isl_multi_pw_aff_copy(mpa
));
6645 map
= isl_map_intersect_domain(map
, dom
);
6650 /* Are all elements of "mpa" piecewise constants?
6652 isl_bool
isl_multi_pw_aff_is_cst(__isl_keep isl_multi_pw_aff
*mpa
)
6654 return isl_multi_pw_aff_every(mpa
, &isl_pw_aff_is_cst
);
6657 /* Does "mpa" have a non-trivial explicit domain?
6659 * The explicit domain, if present, is trivial if it represents
6660 * an (obviously) universe set.
6662 isl_bool
isl_multi_pw_aff_has_non_trivial_domain(
6663 __isl_keep isl_multi_pw_aff
*mpa
)
6666 return isl_bool_error
;
6667 if (!isl_multi_pw_aff_has_explicit_domain(mpa
))
6668 return isl_bool_false
;
6669 return isl_bool_not(isl_set_plain_is_universe(mpa
->u
.dom
));
6675 #include "isl_opt_mpa_templ.c"
6677 /* Compute the minima of the set dimensions as a function of the
6678 * parameters, but independently of the other set dimensions.
6680 __isl_give isl_multi_pw_aff
*isl_set_min_multi_pw_aff(__isl_take isl_set
*set
)
6682 return set_opt_mpa(set
, &isl_set_dim_min
);
6685 /* Compute the maxima of the set dimensions as a function of the
6686 * parameters, but independently of the other set dimensions.
6688 __isl_give isl_multi_pw_aff
*isl_set_max_multi_pw_aff(__isl_take isl_set
*set
)
6690 return set_opt_mpa(set
, &isl_set_dim_max
);
6696 #include "isl_opt_mpa_templ.c"
6698 /* Compute the minima of the output dimensions as a function of the
6699 * parameters and input dimensions, but independently of
6700 * the other output dimensions.
6702 __isl_give isl_multi_pw_aff
*isl_map_min_multi_pw_aff(__isl_take isl_map
*map
)
6704 return map_opt_mpa(map
, &isl_map_dim_min
);
6707 /* Compute the maxima of the output dimensions as a function of the
6708 * parameters and input dimensions, but independently of
6709 * the other output dimensions.
6711 __isl_give isl_multi_pw_aff
*isl_map_max_multi_pw_aff(__isl_take isl_map
*map
)
6713 return map_opt_mpa(map
, &isl_map_dim_max
);
6716 /* Scale the elements of "pma" by the corresponding elements of "mv".
6718 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_scale_multi_val(
6719 __isl_take isl_pw_multi_aff
*pma
, __isl_take isl_multi_val
*mv
)
6722 isl_bool equal_params
;
6724 pma
= isl_pw_multi_aff_cow(pma
);
6727 if (!isl_space_tuple_is_equal(pma
->dim
, isl_dim_out
,
6728 mv
->space
, isl_dim_set
))
6729 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
6730 "spaces don't match", goto error
);
6731 equal_params
= isl_space_has_equal_params(pma
->dim
, mv
->space
);
6732 if (equal_params
< 0)
6734 if (!equal_params
) {
6735 pma
= isl_pw_multi_aff_align_params(pma
,
6736 isl_multi_val_get_space(mv
));
6737 mv
= isl_multi_val_align_params(mv
,
6738 isl_pw_multi_aff_get_space(pma
));
6743 for (i
= 0; i
< pma
->n
; ++i
) {
6744 pma
->p
[i
].maff
= isl_multi_aff_scale_multi_val(pma
->p
[i
].maff
,
6745 isl_multi_val_copy(mv
));
6746 if (!pma
->p
[i
].maff
)
6750 isl_multi_val_free(mv
);
6753 isl_multi_val_free(mv
);
6754 isl_pw_multi_aff_free(pma
);
6758 /* This function is called for each entry of an isl_union_pw_multi_aff.
6759 * If the space of the entry matches that of data->mv,
6760 * then apply isl_pw_multi_aff_scale_multi_val and return the result.
6761 * Otherwise, return an empty isl_pw_multi_aff.
6763 static __isl_give isl_pw_multi_aff
*union_pw_multi_aff_scale_multi_val_entry(
6764 __isl_take isl_pw_multi_aff
*pma
, void *user
)
6766 isl_multi_val
*mv
= user
;
6770 if (!isl_space_tuple_is_equal(pma
->dim
, isl_dim_out
,
6771 mv
->space
, isl_dim_set
)) {
6772 isl_space
*space
= isl_pw_multi_aff_get_space(pma
);
6773 isl_pw_multi_aff_free(pma
);
6774 return isl_pw_multi_aff_empty(space
);
6777 return isl_pw_multi_aff_scale_multi_val(pma
, isl_multi_val_copy(mv
));
6780 /* Scale the elements of "upma" by the corresponding elements of "mv",
6781 * for those entries that match the space of "mv".
6783 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_scale_multi_val(
6784 __isl_take isl_union_pw_multi_aff
*upma
, __isl_take isl_multi_val
*mv
)
6786 struct isl_union_pw_multi_aff_transform_control control
= {
6787 .fn
= &union_pw_multi_aff_scale_multi_val_entry
,
6791 upma
= isl_union_pw_multi_aff_align_params(upma
,
6792 isl_multi_val_get_space(mv
));
6793 mv
= isl_multi_val_align_params(mv
,
6794 isl_union_pw_multi_aff_get_space(upma
));
6798 return isl_union_pw_multi_aff_transform(upma
, &control
);
6800 isl_multi_val_free(mv
);
6803 isl_multi_val_free(mv
);
6804 isl_union_pw_multi_aff_free(upma
);
6808 /* Construct and return a piecewise multi affine expression
6809 * in the given space with value zero in each of the output dimensions and
6810 * a universe domain.
6812 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_zero(__isl_take isl_space
*space
)
6814 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_zero(space
));
6817 /* Construct and return a piecewise multi affine expression
6818 * that is equal to the given piecewise affine expression.
6820 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_pw_aff(
6821 __isl_take isl_pw_aff
*pa
)
6825 isl_pw_multi_aff
*pma
;
6830 space
= isl_pw_aff_get_space(pa
);
6831 pma
= isl_pw_multi_aff_alloc_size(space
, pa
->n
);
6833 for (i
= 0; i
< pa
->n
; ++i
) {
6837 set
= isl_set_copy(pa
->p
[i
].set
);
6838 ma
= isl_multi_aff_from_aff(isl_aff_copy(pa
->p
[i
].aff
));
6839 pma
= isl_pw_multi_aff_add_piece(pma
, set
, ma
);
6842 isl_pw_aff_free(pa
);
6846 /* Construct and return a piecewise multi affine expression
6847 * that is equal to the given multi piecewise affine expression
6848 * on the shared domain of the piecewise affine expressions,
6849 * in the special case of a 0D multi piecewise affine expression.
6851 * Create a piecewise multi affine expression with the explicit domain of
6852 * the 0D multi piecewise affine expression as domain.
6854 static __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_multi_pw_aff_0D(
6855 __isl_take isl_multi_pw_aff
*mpa
)
6861 space
= isl_multi_pw_aff_get_space(mpa
);
6862 dom
= isl_multi_pw_aff_get_explicit_domain(mpa
);
6863 isl_multi_pw_aff_free(mpa
);
6865 ma
= isl_multi_aff_zero(space
);
6866 return isl_pw_multi_aff_alloc(dom
, ma
);
6869 /* Construct and return a piecewise multi affine expression
6870 * that is equal to the given multi piecewise affine expression
6871 * on the shared domain of the piecewise affine expressions.
6873 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_multi_pw_aff(
6874 __isl_take isl_multi_pw_aff
*mpa
)
6879 isl_pw_multi_aff
*pma
;
6885 return isl_pw_multi_aff_from_multi_pw_aff_0D(mpa
);
6887 space
= isl_multi_pw_aff_get_space(mpa
);
6888 pa
= isl_multi_pw_aff_get_pw_aff(mpa
, 0);
6889 pma
= isl_pw_multi_aff_from_pw_aff(pa
);
6891 for (i
= 1; i
< mpa
->n
; ++i
) {
6892 isl_pw_multi_aff
*pma_i
;
6894 pa
= isl_multi_pw_aff_get_pw_aff(mpa
, i
);
6895 pma_i
= isl_pw_multi_aff_from_pw_aff(pa
);
6896 pma
= isl_pw_multi_aff_range_product(pma
, pma_i
);
6899 pma
= isl_pw_multi_aff_reset_space(pma
, space
);
6901 isl_multi_pw_aff_free(mpa
);
6905 /* Convenience function that constructs an isl_multi_pw_aff
6906 * directly from an isl_aff.
6908 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_from_aff(__isl_take isl_aff
*aff
)
6910 return isl_multi_pw_aff_from_pw_aff(isl_pw_aff_from_aff(aff
));
6913 /* Construct and return a multi piecewise affine expression
6914 * that is equal to the given multi affine expression.
6916 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_from_multi_aff(
6917 __isl_take isl_multi_aff
*ma
)
6921 isl_multi_pw_aff
*mpa
;
6923 n
= isl_multi_aff_dim(ma
, isl_dim_out
);
6925 ma
= isl_multi_aff_free(ma
);
6929 mpa
= isl_multi_pw_aff_alloc(isl_multi_aff_get_space(ma
));
6931 for (i
= 0; i
< n
; ++i
) {
6934 pa
= isl_pw_aff_from_aff(isl_multi_aff_get_aff(ma
, i
));
6935 mpa
= isl_multi_pw_aff_set_pw_aff(mpa
, i
, pa
);
6938 isl_multi_aff_free(ma
);
6942 /* This function performs the same operation as isl_multi_pw_aff_from_multi_aff,
6943 * but is considered as a function on an isl_multi_aff when exported.
6945 __isl_give isl_multi_pw_aff
*isl_multi_aff_to_multi_pw_aff(
6946 __isl_take isl_multi_aff
*ma
)
6948 return isl_multi_pw_aff_from_multi_aff(ma
);
6951 /* Construct and return a multi piecewise affine expression
6952 * that is equal to the given piecewise multi affine expression.
6954 * If the resulting multi piecewise affine expression has
6955 * an explicit domain, then assign it the domain of the input.
6956 * In other cases, the domain is stored in the individual elements.
6958 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_from_pw_multi_aff(
6959 __isl_take isl_pw_multi_aff
*pma
)
6964 isl_multi_pw_aff
*mpa
;
6966 n
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
6968 pma
= isl_pw_multi_aff_free(pma
);
6969 space
= isl_pw_multi_aff_get_space(pma
);
6970 mpa
= isl_multi_pw_aff_alloc(space
);
6972 for (i
= 0; i
< n
; ++i
) {
6975 pa
= isl_pw_multi_aff_get_pw_aff(pma
, i
);
6976 mpa
= isl_multi_pw_aff_set_pw_aff(mpa
, i
, pa
);
6978 if (isl_multi_pw_aff_has_explicit_domain(mpa
)) {
6981 dom
= isl_pw_multi_aff_domain(isl_pw_multi_aff_copy(pma
));
6982 mpa
= isl_multi_pw_aff_intersect_domain(mpa
, dom
);
6985 isl_pw_multi_aff_free(pma
);
6989 /* This function performs the same operation as
6990 * isl_multi_pw_aff_from_pw_multi_aff,
6991 * but is considered as a function on an isl_pw_multi_aff when exported.
6993 __isl_give isl_multi_pw_aff
*isl_pw_multi_aff_to_multi_pw_aff(
6994 __isl_take isl_pw_multi_aff
*pma
)
6996 return isl_multi_pw_aff_from_pw_multi_aff(pma
);
6999 /* Do "pa1" and "pa2" represent the same function?
7001 * We first check if they are obviously equal.
7002 * If not, we convert them to maps and check if those are equal.
7004 * If "pa1" or "pa2" contain any NaNs, then they are considered
7005 * not to be the same. A NaN is not equal to anything, not even
7008 isl_bool
isl_pw_aff_is_equal(__isl_keep isl_pw_aff
*pa1
,
7009 __isl_keep isl_pw_aff
*pa2
)
7013 isl_map
*map1
, *map2
;
7016 return isl_bool_error
;
7018 equal
= isl_pw_aff_plain_is_equal(pa1
, pa2
);
7019 if (equal
< 0 || equal
)
7021 has_nan
= either_involves_nan(pa1
, pa2
);
7023 return isl_bool_error
;
7025 return isl_bool_false
;
7027 map1
= isl_map_from_pw_aff_internal(isl_pw_aff_copy(pa1
));
7028 map2
= isl_map_from_pw_aff_internal(isl_pw_aff_copy(pa2
));
7029 equal
= isl_map_is_equal(map1
, map2
);
7036 /* Do "mpa1" and "mpa2" represent the same function?
7038 * Note that we cannot convert the entire isl_multi_pw_aff
7039 * to a map because the domains of the piecewise affine expressions
7040 * may not be the same.
7042 isl_bool
isl_multi_pw_aff_is_equal(__isl_keep isl_multi_pw_aff
*mpa1
,
7043 __isl_keep isl_multi_pw_aff
*mpa2
)
7046 isl_bool equal
, equal_params
;
7049 return isl_bool_error
;
7051 equal_params
= isl_space_has_equal_params(mpa1
->space
, mpa2
->space
);
7052 if (equal_params
< 0)
7053 return isl_bool_error
;
7054 if (!equal_params
) {
7055 if (!isl_space_has_named_params(mpa1
->space
))
7056 return isl_bool_false
;
7057 if (!isl_space_has_named_params(mpa2
->space
))
7058 return isl_bool_false
;
7059 mpa1
= isl_multi_pw_aff_copy(mpa1
);
7060 mpa2
= isl_multi_pw_aff_copy(mpa2
);
7061 mpa1
= isl_multi_pw_aff_align_params(mpa1
,
7062 isl_multi_pw_aff_get_space(mpa2
));
7063 mpa2
= isl_multi_pw_aff_align_params(mpa2
,
7064 isl_multi_pw_aff_get_space(mpa1
));
7065 equal
= isl_multi_pw_aff_is_equal(mpa1
, mpa2
);
7066 isl_multi_pw_aff_free(mpa1
);
7067 isl_multi_pw_aff_free(mpa2
);
7071 equal
= isl_space_is_equal(mpa1
->space
, mpa2
->space
);
7072 if (equal
< 0 || !equal
)
7075 for (i
= 0; i
< mpa1
->n
; ++i
) {
7076 equal
= isl_pw_aff_is_equal(mpa1
->u
.p
[i
], mpa2
->u
.p
[i
]);
7077 if (equal
< 0 || !equal
)
7081 return isl_bool_true
;
7084 /* Do "pma1" and "pma2" represent the same function?
7086 * First check if they are obviously equal.
7087 * If not, then convert them to maps and check if those are equal.
7089 * If "pa1" or "pa2" contain any NaNs, then they are considered
7090 * not to be the same. A NaN is not equal to anything, not even
7093 isl_bool
isl_pw_multi_aff_is_equal(__isl_keep isl_pw_multi_aff
*pma1
,
7094 __isl_keep isl_pw_multi_aff
*pma2
)
7098 isl_map
*map1
, *map2
;
7101 return isl_bool_error
;
7103 equal
= isl_pw_multi_aff_plain_is_equal(pma1
, pma2
);
7104 if (equal
< 0 || equal
)
7106 has_nan
= isl_pw_multi_aff_involves_nan(pma1
);
7107 if (has_nan
>= 0 && !has_nan
)
7108 has_nan
= isl_pw_multi_aff_involves_nan(pma2
);
7109 if (has_nan
< 0 || has_nan
)
7110 return isl_bool_not(has_nan
);
7112 map1
= isl_map_from_pw_multi_aff_internal(isl_pw_multi_aff_copy(pma1
));
7113 map2
= isl_map_from_pw_multi_aff_internal(isl_pw_multi_aff_copy(pma2
));
7114 equal
= isl_map_is_equal(map1
, map2
);
7121 /* Compute the pullback of "mpa" by the function represented by "ma".
7122 * In other words, plug in "ma" in "mpa".
7124 * The parameters of "mpa" and "ma" are assumed to have been aligned.
7126 * If "mpa" has an explicit domain, then it is this domain
7127 * that needs to undergo a pullback, i.e., a preimage.
7129 static __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_pullback_multi_aff_aligned(
7130 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_multi_aff
*ma
)
7133 isl_space
*space
= NULL
;
7135 mpa
= isl_multi_pw_aff_cow(mpa
);
7139 space
= isl_space_join(isl_multi_aff_get_space(ma
),
7140 isl_multi_pw_aff_get_space(mpa
));
7144 for (i
= 0; i
< mpa
->n
; ++i
) {
7145 mpa
->u
.p
[i
] = isl_pw_aff_pullback_multi_aff(mpa
->u
.p
[i
],
7146 isl_multi_aff_copy(ma
));
7150 if (isl_multi_pw_aff_has_explicit_domain(mpa
)) {
7151 mpa
->u
.dom
= isl_set_preimage_multi_aff(mpa
->u
.dom
,
7152 isl_multi_aff_copy(ma
));
7157 isl_multi_aff_free(ma
);
7158 isl_space_free(mpa
->space
);
7162 isl_space_free(space
);
7163 isl_multi_pw_aff_free(mpa
);
7164 isl_multi_aff_free(ma
);
7168 /* Compute the pullback of "mpa" by the function represented by "ma".
7169 * In other words, plug in "ma" in "mpa".
7171 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_pullback_multi_aff(
7172 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_multi_aff
*ma
)
7174 isl_bool equal_params
;
7178 equal_params
= isl_space_has_equal_params(mpa
->space
, ma
->space
);
7179 if (equal_params
< 0)
7182 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa
, ma
);
7183 mpa
= isl_multi_pw_aff_align_params(mpa
, isl_multi_aff_get_space(ma
));
7184 ma
= isl_multi_aff_align_params(ma
, isl_multi_pw_aff_get_space(mpa
));
7185 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa
, ma
);
7187 isl_multi_pw_aff_free(mpa
);
7188 isl_multi_aff_free(ma
);
7192 /* Compute the pullback of "mpa" by the function represented by "pma".
7193 * In other words, plug in "pma" in "mpa".
7195 * The parameters of "mpa" and "mpa" are assumed to have been aligned.
7197 * If "mpa" has an explicit domain, then it is this domain
7198 * that needs to undergo a pullback, i.e., a preimage.
7200 static __isl_give isl_multi_pw_aff
*
7201 isl_multi_pw_aff_pullback_pw_multi_aff_aligned(
7202 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_pw_multi_aff
*pma
)
7205 isl_space
*space
= NULL
;
7207 mpa
= isl_multi_pw_aff_cow(mpa
);
7211 space
= isl_space_join(isl_pw_multi_aff_get_space(pma
),
7212 isl_multi_pw_aff_get_space(mpa
));
7214 for (i
= 0; i
< mpa
->n
; ++i
) {
7215 mpa
->u
.p
[i
] = isl_pw_aff_pullback_pw_multi_aff_aligned(
7216 mpa
->u
.p
[i
], isl_pw_multi_aff_copy(pma
));
7220 if (isl_multi_pw_aff_has_explicit_domain(mpa
)) {
7221 mpa
->u
.dom
= isl_set_preimage_pw_multi_aff(mpa
->u
.dom
,
7222 isl_pw_multi_aff_copy(pma
));
7227 isl_pw_multi_aff_free(pma
);
7228 isl_space_free(mpa
->space
);
7232 isl_space_free(space
);
7233 isl_multi_pw_aff_free(mpa
);
7234 isl_pw_multi_aff_free(pma
);
7238 /* Compute the pullback of "mpa" by the function represented by "pma".
7239 * In other words, plug in "pma" in "mpa".
7241 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_pullback_pw_multi_aff(
7242 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_pw_multi_aff
*pma
)
7244 isl_bool equal_params
;
7248 equal_params
= isl_space_has_equal_params(mpa
->space
, pma
->dim
);
7249 if (equal_params
< 0)
7252 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa
, pma
);
7253 mpa
= isl_multi_pw_aff_align_params(mpa
,
7254 isl_pw_multi_aff_get_space(pma
));
7255 pma
= isl_pw_multi_aff_align_params(pma
,
7256 isl_multi_pw_aff_get_space(mpa
));
7257 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa
, pma
);
7259 isl_multi_pw_aff_free(mpa
);
7260 isl_pw_multi_aff_free(pma
);
7264 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
7265 * with the domain of "aff". The domain of the result is the same
7267 * "mpa" and "aff" are assumed to have been aligned.
7269 * We first extract the parametric constant from "aff", defined
7270 * over the correct domain.
7271 * Then we add the appropriate combinations of the members of "mpa".
7272 * Finally, we add the integer divisions through recursive calls.
7274 static __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_aff_aligned(
7275 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_aff
*aff
)
7278 isl_size n_in
, n_div
, n_mpa_in
;
7284 n_in
= isl_aff_dim(aff
, isl_dim_in
);
7285 n_div
= isl_aff_dim(aff
, isl_dim_div
);
7286 n_mpa_in
= isl_multi_pw_aff_dim(mpa
, isl_dim_in
);
7287 if (n_in
< 0 || n_div
< 0 || n_mpa_in
< 0)
7290 space
= isl_space_domain(isl_multi_pw_aff_get_space(mpa
));
7291 tmp
= isl_aff_copy(aff
);
7292 tmp
= isl_aff_drop_dims(tmp
, isl_dim_div
, 0, n_div
);
7293 tmp
= isl_aff_drop_dims(tmp
, isl_dim_in
, 0, n_in
);
7294 tmp
= isl_aff_add_dims(tmp
, isl_dim_in
, n_mpa_in
);
7295 tmp
= isl_aff_reset_domain_space(tmp
, space
);
7296 pa
= isl_pw_aff_from_aff(tmp
);
7298 for (i
= 0; i
< n_in
; ++i
) {
7301 if (!isl_aff_involves_dims(aff
, isl_dim_in
, i
, 1))
7303 v
= isl_aff_get_coefficient_val(aff
, isl_dim_in
, i
);
7304 pa_i
= isl_multi_pw_aff_get_pw_aff(mpa
, i
);
7305 pa_i
= isl_pw_aff_scale_val(pa_i
, v
);
7306 pa
= isl_pw_aff_add(pa
, pa_i
);
7309 for (i
= 0; i
< n_div
; ++i
) {
7313 if (!isl_aff_involves_dims(aff
, isl_dim_div
, i
, 1))
7315 div
= isl_aff_get_div(aff
, i
);
7316 pa_i
= isl_multi_pw_aff_apply_aff_aligned(
7317 isl_multi_pw_aff_copy(mpa
), div
);
7318 pa_i
= isl_pw_aff_floor(pa_i
);
7319 v
= isl_aff_get_coefficient_val(aff
, isl_dim_div
, i
);
7320 pa_i
= isl_pw_aff_scale_val(pa_i
, v
);
7321 pa
= isl_pw_aff_add(pa
, pa_i
);
7324 isl_multi_pw_aff_free(mpa
);
7329 isl_multi_pw_aff_free(mpa
);
7334 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
7335 * with the domain of "aff". The domain of the result is the same
7338 __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_aff(
7339 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_aff
*aff
)
7341 isl_bool equal_params
;
7345 equal_params
= isl_space_has_equal_params(aff
->ls
->dim
, mpa
->space
);
7346 if (equal_params
< 0)
7349 return isl_multi_pw_aff_apply_aff_aligned(mpa
, aff
);
7351 aff
= isl_aff_align_params(aff
, isl_multi_pw_aff_get_space(mpa
));
7352 mpa
= isl_multi_pw_aff_align_params(mpa
, isl_aff_get_space(aff
));
7354 return isl_multi_pw_aff_apply_aff_aligned(mpa
, aff
);
7357 isl_multi_pw_aff_free(mpa
);
7361 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
7362 * with the domain of "pa". The domain of the result is the same
7364 * "mpa" and "pa" are assumed to have been aligned.
7366 * We consider each piece in turn. Note that the domains of the
7367 * pieces are assumed to be disjoint and they remain disjoint
7368 * after taking the preimage (over the same function).
7370 static __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_pw_aff_aligned(
7371 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_pw_aff
*pa
)
7380 space
= isl_space_join(isl_multi_pw_aff_get_space(mpa
),
7381 isl_pw_aff_get_space(pa
));
7382 res
= isl_pw_aff_empty(space
);
7384 for (i
= 0; i
< pa
->n
; ++i
) {
7388 pa_i
= isl_multi_pw_aff_apply_aff_aligned(
7389 isl_multi_pw_aff_copy(mpa
),
7390 isl_aff_copy(pa
->p
[i
].aff
));
7391 domain
= isl_set_copy(pa
->p
[i
].set
);
7392 domain
= isl_set_preimage_multi_pw_aff(domain
,
7393 isl_multi_pw_aff_copy(mpa
));
7394 pa_i
= isl_pw_aff_intersect_domain(pa_i
, domain
);
7395 res
= isl_pw_aff_add_disjoint(res
, pa_i
);
7398 isl_pw_aff_free(pa
);
7399 isl_multi_pw_aff_free(mpa
);
7402 isl_pw_aff_free(pa
);
7403 isl_multi_pw_aff_free(mpa
);
7407 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
7408 * with the domain of "pa". The domain of the result is the same
7411 __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_pw_aff(
7412 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_pw_aff
*pa
)
7414 isl_bool equal_params
;
7418 equal_params
= isl_space_has_equal_params(pa
->dim
, mpa
->space
);
7419 if (equal_params
< 0)
7422 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa
, pa
);
7424 pa
= isl_pw_aff_align_params(pa
, isl_multi_pw_aff_get_space(mpa
));
7425 mpa
= isl_multi_pw_aff_align_params(mpa
, isl_pw_aff_get_space(pa
));
7427 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa
, pa
);
7429 isl_pw_aff_free(pa
);
7430 isl_multi_pw_aff_free(mpa
);
7434 /* Compute the pullback of "pa" by the function represented by "mpa".
7435 * In other words, plug in "mpa" in "pa".
7436 * "pa" and "mpa" are assumed to have been aligned.
7438 * The pullback is computed by applying "pa" to "mpa".
7440 static __isl_give isl_pw_aff
*isl_pw_aff_pullback_multi_pw_aff_aligned(
7441 __isl_take isl_pw_aff
*pa
, __isl_take isl_multi_pw_aff
*mpa
)
7443 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa
, pa
);
7446 /* Compute the pullback of "pa" by the function represented by "mpa".
7447 * In other words, plug in "mpa" in "pa".
7449 * The pullback is computed by applying "pa" to "mpa".
7451 __isl_give isl_pw_aff
*isl_pw_aff_pullback_multi_pw_aff(
7452 __isl_take isl_pw_aff
*pa
, __isl_take isl_multi_pw_aff
*mpa
)
7454 return isl_multi_pw_aff_apply_pw_aff(mpa
, pa
);
7457 /* Compute the pullback of "mpa1" by the function represented by "mpa2".
7458 * In other words, plug in "mpa2" in "mpa1".
7460 * We pullback each member of "mpa1" in turn.
7462 * If "mpa1" has an explicit domain, then it is this domain
7463 * that needs to undergo a pullback instead, i.e., a preimage.
7465 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_pullback_multi_pw_aff(
7466 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
)
7469 isl_space
*space
= NULL
;
7471 isl_multi_pw_aff_align_params_bin(&mpa1
, &mpa2
);
7472 mpa1
= isl_multi_pw_aff_cow(mpa1
);
7476 space
= isl_space_join(isl_multi_pw_aff_get_space(mpa2
),
7477 isl_multi_pw_aff_get_space(mpa1
));
7479 for (i
= 0; i
< mpa1
->n
; ++i
) {
7480 mpa1
->u
.p
[i
] = isl_pw_aff_pullback_multi_pw_aff_aligned(
7481 mpa1
->u
.p
[i
], isl_multi_pw_aff_copy(mpa2
));
7486 if (isl_multi_pw_aff_has_explicit_domain(mpa1
)) {
7487 mpa1
->u
.dom
= isl_set_preimage_multi_pw_aff(mpa1
->u
.dom
,
7488 isl_multi_pw_aff_copy(mpa2
));
7492 mpa1
= isl_multi_pw_aff_reset_space(mpa1
, space
);
7494 isl_multi_pw_aff_free(mpa2
);
7497 isl_space_free(space
);
7498 isl_multi_pw_aff_free(mpa1
);
7499 isl_multi_pw_aff_free(mpa2
);
7503 /* Align the parameters of "mpa1" and "mpa2", check that the ranges
7504 * of "mpa1" and "mpa2" live in the same space, construct map space
7505 * between the domain spaces of "mpa1" and "mpa2" and call "order"
7506 * with this map space as extract argument.
7508 static __isl_give isl_map
*isl_multi_pw_aff_order_map(
7509 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
,
7510 __isl_give isl_map
*(*order
)(__isl_keep isl_multi_pw_aff
*mpa1
,
7511 __isl_keep isl_multi_pw_aff
*mpa2
, __isl_take isl_space
*space
))
7514 isl_space
*space1
, *space2
;
7517 mpa1
= isl_multi_pw_aff_align_params(mpa1
,
7518 isl_multi_pw_aff_get_space(mpa2
));
7519 mpa2
= isl_multi_pw_aff_align_params(mpa2
,
7520 isl_multi_pw_aff_get_space(mpa1
));
7523 match
= isl_space_tuple_is_equal(mpa1
->space
, isl_dim_out
,
7524 mpa2
->space
, isl_dim_out
);
7528 isl_die(isl_multi_pw_aff_get_ctx(mpa1
), isl_error_invalid
,
7529 "range spaces don't match", goto error
);
7530 space1
= isl_space_domain(isl_multi_pw_aff_get_space(mpa1
));
7531 space2
= isl_space_domain(isl_multi_pw_aff_get_space(mpa2
));
7532 space1
= isl_space_map_from_domain_and_range(space1
, space2
);
7534 res
= order(mpa1
, mpa2
, space1
);
7535 isl_multi_pw_aff_free(mpa1
);
7536 isl_multi_pw_aff_free(mpa2
);
7539 isl_multi_pw_aff_free(mpa1
);
7540 isl_multi_pw_aff_free(mpa2
);
7544 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7545 * where the function values are equal. "space" is the space of the result.
7546 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7548 * "mpa1" and "mpa2" are equal when each of the pairs of elements
7549 * in the sequences are equal.
7551 static __isl_give isl_map
*isl_multi_pw_aff_eq_map_on_space(
7552 __isl_keep isl_multi_pw_aff
*mpa1
, __isl_keep isl_multi_pw_aff
*mpa2
,
7553 __isl_take isl_space
*space
)
7559 n
= isl_multi_pw_aff_dim(mpa1
, isl_dim_out
);
7561 space
= isl_space_free(space
);
7562 res
= isl_map_universe(space
);
7564 for (i
= 0; i
< n
; ++i
) {
7565 isl_pw_aff
*pa1
, *pa2
;
7568 pa1
= isl_multi_pw_aff_get_pw_aff(mpa1
, i
);
7569 pa2
= isl_multi_pw_aff_get_pw_aff(mpa2
, i
);
7570 map
= isl_pw_aff_eq_map(pa1
, pa2
);
7571 res
= isl_map_intersect(res
, map
);
7577 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7578 * where the function values are equal.
7580 __isl_give isl_map
*isl_multi_pw_aff_eq_map(__isl_take isl_multi_pw_aff
*mpa1
,
7581 __isl_take isl_multi_pw_aff
*mpa2
)
7583 return isl_multi_pw_aff_order_map(mpa1
, mpa2
,
7584 &isl_multi_pw_aff_eq_map_on_space
);
7587 /* Intersect "map" with the result of applying "order"
7588 * on two copies of "mpa".
7590 static __isl_give isl_map
*isl_map_order_at_multi_pw_aff(
7591 __isl_take isl_map
*map
, __isl_take isl_multi_pw_aff
*mpa
,
7592 __isl_give isl_map
*(*order
)(__isl_take isl_multi_pw_aff
*mpa1
,
7593 __isl_take isl_multi_pw_aff
*mpa2
))
7595 return isl_map_intersect(map
, order(mpa
, isl_multi_pw_aff_copy(mpa
)));
7598 /* Return the subset of "map" where the domain and the range
7599 * have equal "mpa" values.
7601 __isl_give isl_map
*isl_map_eq_at_multi_pw_aff(__isl_take isl_map
*map
,
7602 __isl_take isl_multi_pw_aff
*mpa
)
7604 return isl_map_order_at_multi_pw_aff(map
, mpa
,
7605 &isl_multi_pw_aff_eq_map
);
7608 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7609 * where the function values of "mpa1" lexicographically satisfies
7610 * "strict_base"/"base" compared to that of "mpa2".
7611 * "space" is the space of the result.
7612 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7614 * "mpa1" lexicographically satisfies "strict_base"/"base" compared to "mpa2"
7615 * if, for some i, the i-th element of "mpa1" satisfies "strict_base"/"base"
7616 * when compared to the i-th element of "mpa2" while all previous elements are
7618 * In particular, if i corresponds to the final elements
7619 * then they need to satisfy "base", while "strict_base" needs to be satisfied
7620 * for other values of i.
7621 * If "base" is a strict order, then "base" and "strict_base" are the same.
7623 static __isl_give isl_map
*isl_multi_pw_aff_lex_map_on_space(
7624 __isl_keep isl_multi_pw_aff
*mpa1
, __isl_keep isl_multi_pw_aff
*mpa2
,
7625 __isl_give isl_map
*(*strict_base
)(__isl_take isl_pw_aff
*pa1
,
7626 __isl_take isl_pw_aff
*pa2
),
7627 __isl_give isl_map
*(*base
)(__isl_take isl_pw_aff
*pa1
,
7628 __isl_take isl_pw_aff
*pa2
),
7629 __isl_take isl_space
*space
)
7633 isl_map
*res
, *rest
;
7635 n
= isl_multi_pw_aff_dim(mpa1
, isl_dim_out
);
7637 space
= isl_space_free(space
);
7638 res
= isl_map_empty(isl_space_copy(space
));
7639 rest
= isl_map_universe(space
);
7641 for (i
= 0; i
< n
; ++i
) {
7643 isl_pw_aff
*pa1
, *pa2
;
7648 pa1
= isl_multi_pw_aff_get_pw_aff(mpa1
, i
);
7649 pa2
= isl_multi_pw_aff_get_pw_aff(mpa2
, i
);
7650 map
= last
? base(pa1
, pa2
) : strict_base(pa1
, pa2
);
7651 map
= isl_map_intersect(map
, isl_map_copy(rest
));
7652 res
= isl_map_union(res
, map
);
7657 pa1
= isl_multi_pw_aff_get_pw_aff(mpa1
, i
);
7658 pa2
= isl_multi_pw_aff_get_pw_aff(mpa2
, i
);
7659 map
= isl_pw_aff_eq_map(pa1
, pa2
);
7660 rest
= isl_map_intersect(rest
, map
);
7670 #define STRICT_ORDER lt
7671 #include "isl_aff_lex_templ.c"
7676 #define STRICT_ORDER lt
7677 #include "isl_aff_lex_templ.c"
7682 #define STRICT_ORDER gt
7683 #include "isl_aff_lex_templ.c"
7688 #define STRICT_ORDER gt
7689 #include "isl_aff_lex_templ.c"
7691 /* Compare two isl_affs.
7693 * Return -1 if "aff1" is "smaller" than "aff2", 1 if "aff1" is "greater"
7694 * than "aff2" and 0 if they are equal.
7696 * The order is fairly arbitrary. We do consider expressions that only involve
7697 * earlier dimensions as "smaller".
7699 int isl_aff_plain_cmp(__isl_keep isl_aff
*aff1
, __isl_keep isl_aff
*aff2
)
7712 cmp
= isl_local_space_cmp(aff1
->ls
, aff2
->ls
);
7716 last1
= isl_seq_last_non_zero(aff1
->v
->el
+ 1, aff1
->v
->size
- 1);
7717 last2
= isl_seq_last_non_zero(aff2
->v
->el
+ 1, aff1
->v
->size
- 1);
7719 return last1
- last2
;
7721 return isl_seq_cmp(aff1
->v
->el
, aff2
->v
->el
, aff1
->v
->size
);
7724 /* Compare two isl_pw_affs.
7726 * Return -1 if "pa1" is "smaller" than "pa2", 1 if "pa1" is "greater"
7727 * than "pa2" and 0 if they are equal.
7729 * The order is fairly arbitrary. We do consider expressions that only involve
7730 * earlier dimensions as "smaller".
7732 int isl_pw_aff_plain_cmp(__isl_keep isl_pw_aff
*pa1
,
7733 __isl_keep isl_pw_aff
*pa2
)
7746 cmp
= isl_space_cmp(pa1
->dim
, pa2
->dim
);
7750 if (pa1
->n
!= pa2
->n
)
7751 return pa1
->n
- pa2
->n
;
7753 for (i
= 0; i
< pa1
->n
; ++i
) {
7754 cmp
= isl_set_plain_cmp(pa1
->p
[i
].set
, pa2
->p
[i
].set
);
7757 cmp
= isl_aff_plain_cmp(pa1
->p
[i
].aff
, pa2
->p
[i
].aff
);
7765 /* Return a piecewise affine expression that is equal to "v" on "domain".
7767 __isl_give isl_pw_aff
*isl_pw_aff_val_on_domain(__isl_take isl_set
*domain
,
7768 __isl_take isl_val
*v
)
7771 isl_local_space
*ls
;
7774 space
= isl_set_get_space(domain
);
7775 ls
= isl_local_space_from_space(space
);
7776 aff
= isl_aff_val_on_domain(ls
, v
);
7778 return isl_pw_aff_alloc(domain
, aff
);
7781 /* Return a piecewise affine expression that is equal to the parameter
7782 * with identifier "id" on "domain".
7784 __isl_give isl_pw_aff
*isl_pw_aff_param_on_domain_id(
7785 __isl_take isl_set
*domain
, __isl_take isl_id
*id
)
7790 space
= isl_set_get_space(domain
);
7791 space
= isl_space_add_param_id(space
, isl_id_copy(id
));
7792 domain
= isl_set_align_params(domain
, isl_space_copy(space
));
7793 aff
= isl_aff_param_on_domain_space_id(space
, id
);
7795 return isl_pw_aff_alloc(domain
, aff
);
7798 /* Return a multi affine expression that is equal to "mv" on domain
7801 __isl_give isl_multi_aff
*isl_multi_aff_multi_val_on_domain_space(
7802 __isl_take isl_space
*space
, __isl_take isl_multi_val
*mv
)
7807 isl_local_space
*ls
;
7810 n
= isl_multi_val_dim(mv
, isl_dim_set
);
7811 if (!space
|| n
< 0)
7814 space2
= isl_multi_val_get_space(mv
);
7815 space2
= isl_space_align_params(space2
, isl_space_copy(space
));
7816 space
= isl_space_align_params(space
, isl_space_copy(space2
));
7817 space
= isl_space_map_from_domain_and_range(space
, space2
);
7818 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
7819 ls
= isl_local_space_from_space(isl_space_domain(space
));
7820 for (i
= 0; i
< n
; ++i
) {
7824 v
= isl_multi_val_get_val(mv
, i
);
7825 aff
= isl_aff_val_on_domain(isl_local_space_copy(ls
), v
);
7826 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
7828 isl_local_space_free(ls
);
7830 isl_multi_val_free(mv
);
7833 isl_space_free(space
);
7834 isl_multi_val_free(mv
);
7838 /* This is an alternative name for the function above.
7840 __isl_give isl_multi_aff
*isl_multi_aff_multi_val_on_space(
7841 __isl_take isl_space
*space
, __isl_take isl_multi_val
*mv
)
7843 return isl_multi_aff_multi_val_on_domain_space(space
, mv
);
7846 /* This function performs the same operation as
7847 * isl_multi_aff_multi_val_on_domain_space,
7848 * but is considered as a function on an isl_space when exported.
7850 __isl_give isl_multi_aff
*isl_space_multi_aff_on_domain_multi_val(
7851 __isl_take isl_space
*space
, __isl_take isl_multi_val
*mv
)
7853 return isl_multi_aff_multi_val_on_domain_space(space
, mv
);
7856 /* Return a piecewise multi-affine expression
7857 * that is equal to "mv" on "domain".
7859 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_multi_val_on_domain(
7860 __isl_take isl_set
*domain
, __isl_take isl_multi_val
*mv
)
7865 space
= isl_set_get_space(domain
);
7866 ma
= isl_multi_aff_multi_val_on_space(space
, mv
);
7868 return isl_pw_multi_aff_alloc(domain
, ma
);
7871 /* This function performs the same operation as
7872 * isl_pw_multi_aff_multi_val_on_domain,
7873 * but is considered as a function on an isl_set when exported.
7875 __isl_give isl_pw_multi_aff
*isl_set_pw_multi_aff_on_domain_multi_val(
7876 __isl_take isl_set
*domain
, __isl_take isl_multi_val
*mv
)
7878 return isl_pw_multi_aff_multi_val_on_domain(domain
, mv
);
7881 /* Internal data structure for isl_union_pw_multi_aff_multi_val_on_domain.
7882 * mv is the value that should be attained on each domain set
7883 * res collects the results
7885 struct isl_union_pw_multi_aff_multi_val_on_domain_data
{
7887 isl_union_pw_multi_aff
*res
;
7890 /* Create an isl_pw_multi_aff equal to data->mv on "domain"
7891 * and add it to data->res.
7893 static isl_stat
pw_multi_aff_multi_val_on_domain(__isl_take isl_set
*domain
,
7896 struct isl_union_pw_multi_aff_multi_val_on_domain_data
*data
= user
;
7897 isl_pw_multi_aff
*pma
;
7900 mv
= isl_multi_val_copy(data
->mv
);
7901 pma
= isl_pw_multi_aff_multi_val_on_domain(domain
, mv
);
7902 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma
);
7904 return data
->res
? isl_stat_ok
: isl_stat_error
;
7907 /* Return a union piecewise multi-affine expression
7908 * that is equal to "mv" on "domain".
7910 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_multi_val_on_domain(
7911 __isl_take isl_union_set
*domain
, __isl_take isl_multi_val
*mv
)
7913 struct isl_union_pw_multi_aff_multi_val_on_domain_data data
;
7916 space
= isl_union_set_get_space(domain
);
7917 data
.res
= isl_union_pw_multi_aff_empty(space
);
7919 if (isl_union_set_foreach_set(domain
,
7920 &pw_multi_aff_multi_val_on_domain
, &data
) < 0)
7921 data
.res
= isl_union_pw_multi_aff_free(data
.res
);
7922 isl_union_set_free(domain
);
7923 isl_multi_val_free(mv
);
7927 /* Compute the pullback of data->pma by the function represented by "pma2",
7928 * provided the spaces match, and add the results to data->res.
7930 static isl_stat
pullback_entry(__isl_take isl_pw_multi_aff
*pma2
, void *user
)
7932 struct isl_union_pw_multi_aff_bin_data
*data
= user
;
7934 if (!isl_space_tuple_is_equal(data
->pma
->dim
, isl_dim_in
,
7935 pma2
->dim
, isl_dim_out
)) {
7936 isl_pw_multi_aff_free(pma2
);
7940 pma2
= isl_pw_multi_aff_pullback_pw_multi_aff(
7941 isl_pw_multi_aff_copy(data
->pma
), pma2
);
7943 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma2
);
7945 return isl_stat_error
;
7950 /* Compute the pullback of "upma1" by the function represented by "upma2".
7952 __isl_give isl_union_pw_multi_aff
*
7953 isl_union_pw_multi_aff_pullback_union_pw_multi_aff(
7954 __isl_take isl_union_pw_multi_aff
*upma1
,
7955 __isl_take isl_union_pw_multi_aff
*upma2
)
7957 return bin_op(upma1
, upma2
, &pullback_entry
);
7960 /* Apply "upma2" to "upma1".
7962 * That is, compute the pullback of "upma2" by "upma1".
7964 __isl_give isl_union_pw_multi_aff
*
7965 isl_union_pw_multi_aff_apply_union_pw_multi_aff(
7966 __isl_take isl_union_pw_multi_aff
*upma1
,
7967 __isl_take isl_union_pw_multi_aff
*upma2
)
7969 return isl_union_pw_multi_aff_pullback_union_pw_multi_aff(upma2
, upma1
);
7973 #define TYPE isl_pw_multi_aff
7975 #include "isl_copy_tuple_id_templ.c"
7977 /* Given a function "pma1" of the form A[B -> C] -> D and
7978 * a function "pma2" of the form E -> B,
7979 * replace the domain of the wrapped relation inside the domain of "pma1"
7980 * by the preimage with respect to "pma2".
7981 * In other words, plug in "pma2" in this nested domain.
7982 * The result is of the form A[E -> C] -> D.
7984 * In particular, extend E -> B to A[E -> C] -> A[B -> C] and
7985 * plug that into "pma1".
7987 __isl_give isl_pw_multi_aff
*
7988 isl_pw_multi_aff_preimage_domain_wrapped_domain_pw_multi_aff(
7989 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
7991 isl_space
*pma1_space
, *pma2_space
;
7993 isl_pw_multi_aff
*id
;
7995 pma1_space
= isl_pw_multi_aff_peek_space(pma1
);
7996 pma2_space
= isl_pw_multi_aff_peek_space(pma2
);
7998 if (isl_space_check_domain_is_wrapping(pma1_space
) < 0)
8000 if (isl_space_check_wrapped_tuple_is_equal(pma1_space
,
8001 isl_dim_in
, isl_dim_in
, pma2_space
, isl_dim_out
) < 0)
8004 space
= isl_space_domain(isl_space_copy(pma1_space
));
8005 space
= isl_space_range(isl_space_unwrap(space
));
8006 id
= isl_pw_multi_aff_identity_on_domain_space(space
);
8007 pma2
= isl_pw_multi_aff_product(pma2
, id
);
8009 pma2
= isl_pw_multi_aff_copy_tuple_id(pma2
, isl_dim_in
,
8010 pma1_space
, isl_dim_in
);
8011 pma2
= isl_pw_multi_aff_copy_tuple_id(pma2
, isl_dim_out
,
8012 pma1_space
, isl_dim_in
);
8014 return isl_pw_multi_aff_pullback_pw_multi_aff(pma1
, pma2
);
8016 isl_pw_multi_aff_free(pma1
);
8017 isl_pw_multi_aff_free(pma2
);
8021 /* If data->pma and "pma2" are such that
8022 * data->pma is of the form A[B -> C] -> D and
8023 * "pma2" is of the form E -> B,
8024 * then replace the domain of the wrapped relation
8025 * inside the domain of data->pma by the preimage with respect to "pma2" and
8026 * add the result to data->res.
8028 static isl_stat
preimage_domain_wrapped_domain_entry(
8029 __isl_take isl_pw_multi_aff
*pma2
, void *user
)
8031 struct isl_union_pw_multi_aff_bin_data
*data
= user
;
8032 isl_space
*pma1_space
, *pma2_space
;
8035 pma1_space
= isl_pw_multi_aff_peek_space(data
->pma
);
8036 pma2_space
= isl_pw_multi_aff_peek_space(pma2
);
8038 match
= isl_space_domain_is_wrapping(pma1_space
);
8039 if (match
>= 0 && match
)
8040 match
= isl_space_wrapped_tuple_is_equal(pma1_space
, isl_dim_in
,
8041 isl_dim_in
, pma2_space
, isl_dim_out
);
8042 if (match
< 0 || !match
) {
8043 isl_pw_multi_aff_free(pma2
);
8044 return match
< 0 ? isl_stat_error
: isl_stat_ok
;
8047 pma2
= isl_pw_multi_aff_preimage_domain_wrapped_domain_pw_multi_aff(
8048 isl_pw_multi_aff_copy(data
->pma
), pma2
);
8050 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma2
);
8052 return isl_stat_non_null(data
->res
);
8055 /* For each pair of functions A[B -> C] -> D in "upma1" and
8056 * E -> B in "upma2",
8057 * replace the domain of the wrapped relation inside the domain of the first
8058 * by the preimage with respect to the second and collect the results.
8059 * In other words, plug in the second function in this nested domain.
8060 * The results are of the form A[E -> C] -> D.
8062 __isl_give isl_union_pw_multi_aff
*
8063 isl_union_pw_multi_aff_preimage_domain_wrapped_domain_union_pw_multi_aff(
8064 __isl_take isl_union_pw_multi_aff
*upma1
,
8065 __isl_take isl_union_pw_multi_aff
*upma2
)
8067 return bin_op(upma1
, upma2
, &preimage_domain_wrapped_domain_entry
);
8070 /* Check that the domain space of "upa" matches "space".
8072 * This function is called from isl_multi_union_pw_aff_set_union_pw_aff and
8073 * can in principle never fail since the space "space" is that
8074 * of the isl_multi_union_pw_aff and is a set space such that
8075 * there is no domain space to match.
8077 * We check the parameters and double-check that "space" is
8078 * indeed that of a set.
8080 static isl_stat
isl_union_pw_aff_check_match_domain_space(
8081 __isl_keep isl_union_pw_aff
*upa
, __isl_keep isl_space
*space
)
8083 isl_space
*upa_space
;
8087 return isl_stat_error
;
8089 match
= isl_space_is_set(space
);
8091 return isl_stat_error
;
8093 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
8094 "expecting set space", return isl_stat_error
);
8096 upa_space
= isl_union_pw_aff_get_space(upa
);
8097 match
= isl_space_has_equal_params(space
, upa_space
);
8101 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
8102 "parameters don't match", goto error
);
8104 isl_space_free(upa_space
);
8107 isl_space_free(upa_space
);
8108 return isl_stat_error
;
8111 /* Do the parameters of "upa" match those of "space"?
8113 static isl_bool
isl_union_pw_aff_matching_params(
8114 __isl_keep isl_union_pw_aff
*upa
, __isl_keep isl_space
*space
)
8116 isl_space
*upa_space
;
8120 return isl_bool_error
;
8122 upa_space
= isl_union_pw_aff_get_space(upa
);
8124 match
= isl_space_has_equal_params(space
, upa_space
);
8126 isl_space_free(upa_space
);
8130 /* Internal data structure for isl_union_pw_aff_reset_domain_space.
8131 * space represents the new parameters.
8132 * res collects the results.
8134 struct isl_union_pw_aff_reset_params_data
{
8136 isl_union_pw_aff
*res
;
8139 /* Replace the parameters of "pa" by data->space and
8140 * add the result to data->res.
8142 static isl_stat
reset_params(__isl_take isl_pw_aff
*pa
, void *user
)
8144 struct isl_union_pw_aff_reset_params_data
*data
= user
;
8147 space
= isl_pw_aff_get_space(pa
);
8148 space
= isl_space_replace_params(space
, data
->space
);
8149 pa
= isl_pw_aff_reset_space(pa
, space
);
8150 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
8152 return data
->res
? isl_stat_ok
: isl_stat_error
;
8155 /* Replace the domain space of "upa" by "space".
8156 * Since a union expression does not have a (single) domain space,
8157 * "space" is necessarily a parameter space.
8159 * Since the order and the names of the parameters determine
8160 * the hash value, we need to create a new hash table.
8162 static __isl_give isl_union_pw_aff
*isl_union_pw_aff_reset_domain_space(
8163 __isl_take isl_union_pw_aff
*upa
, __isl_take isl_space
*space
)
8165 struct isl_union_pw_aff_reset_params_data data
= { space
};
8168 match
= isl_union_pw_aff_matching_params(upa
, space
);
8170 upa
= isl_union_pw_aff_free(upa
);
8172 isl_space_free(space
);
8176 data
.res
= isl_union_pw_aff_empty(isl_space_copy(space
));
8177 if (isl_union_pw_aff_foreach_pw_aff(upa
, &reset_params
, &data
) < 0)
8178 data
.res
= isl_union_pw_aff_free(data
.res
);
8180 isl_union_pw_aff_free(upa
);
8181 isl_space_free(space
);
8185 /* Return the floor of "pa".
8187 static __isl_give isl_pw_aff
*floor_entry(__isl_take isl_pw_aff
*pa
, void *user
)
8189 return isl_pw_aff_floor(pa
);
8192 /* Given f, return floor(f).
8194 __isl_give isl_union_pw_aff
*isl_union_pw_aff_floor(
8195 __isl_take isl_union_pw_aff
*upa
)
8197 return isl_union_pw_aff_transform_inplace(upa
, &floor_entry
, NULL
);
8202 * upa mod m = upa - m * floor(upa/m)
8204 * with m an integer value.
8206 __isl_give isl_union_pw_aff
*isl_union_pw_aff_mod_val(
8207 __isl_take isl_union_pw_aff
*upa
, __isl_take isl_val
*m
)
8209 isl_union_pw_aff
*res
;
8214 if (!isl_val_is_int(m
))
8215 isl_die(isl_val_get_ctx(m
), isl_error_invalid
,
8216 "expecting integer modulo", goto error
);
8217 if (!isl_val_is_pos(m
))
8218 isl_die(isl_val_get_ctx(m
), isl_error_invalid
,
8219 "expecting positive modulo", goto error
);
8221 res
= isl_union_pw_aff_copy(upa
);
8222 upa
= isl_union_pw_aff_scale_down_val(upa
, isl_val_copy(m
));
8223 upa
= isl_union_pw_aff_floor(upa
);
8224 upa
= isl_union_pw_aff_scale_val(upa
, m
);
8225 res
= isl_union_pw_aff_sub(res
, upa
);
8230 isl_union_pw_aff_free(upa
);
8234 /* Internal data structure for isl_union_pw_multi_aff_get_union_pw_aff.
8235 * pos is the output position that needs to be extracted.
8236 * res collects the results.
8238 struct isl_union_pw_multi_aff_get_union_pw_aff_data
{
8240 isl_union_pw_aff
*res
;
8243 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma"
8244 * (assuming it has such a dimension) and add it to data->res.
8246 static isl_stat
get_union_pw_aff(__isl_take isl_pw_multi_aff
*pma
, void *user
)
8248 struct isl_union_pw_multi_aff_get_union_pw_aff_data
*data
= user
;
8252 n_out
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
8254 return isl_stat_error
;
8255 if (data
->pos
>= n_out
) {
8256 isl_pw_multi_aff_free(pma
);
8260 pa
= isl_pw_multi_aff_get_pw_aff(pma
, data
->pos
);
8261 isl_pw_multi_aff_free(pma
);
8263 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
8265 return data
->res
? isl_stat_ok
: isl_stat_error
;
8268 /* Extract an isl_union_pw_aff corresponding to
8269 * output dimension "pos" of "upma".
8271 __isl_give isl_union_pw_aff
*isl_union_pw_multi_aff_get_union_pw_aff(
8272 __isl_keep isl_union_pw_multi_aff
*upma
, int pos
)
8274 struct isl_union_pw_multi_aff_get_union_pw_aff_data data
;
8281 isl_die(isl_union_pw_multi_aff_get_ctx(upma
), isl_error_invalid
,
8282 "cannot extract at negative position", return NULL
);
8284 space
= isl_union_pw_multi_aff_get_space(upma
);
8285 data
.res
= isl_union_pw_aff_empty(space
);
8287 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma
,
8288 &get_union_pw_aff
, &data
) < 0)
8289 data
.res
= isl_union_pw_aff_free(data
.res
);
8294 /* Return a union piecewise affine expression
8295 * that is equal to "aff" on "domain".
8297 __isl_give isl_union_pw_aff
*isl_union_pw_aff_aff_on_domain(
8298 __isl_take isl_union_set
*domain
, __isl_take isl_aff
*aff
)
8302 pa
= isl_pw_aff_from_aff(aff
);
8303 return isl_union_pw_aff_pw_aff_on_domain(domain
, pa
);
8306 /* Return a union piecewise affine expression
8307 * that is equal to the parameter identified by "id" on "domain".
8309 * Make sure the parameter appears in the space passed to
8310 * isl_aff_param_on_domain_space_id.
8312 __isl_give isl_union_pw_aff
*isl_union_pw_aff_param_on_domain_id(
8313 __isl_take isl_union_set
*domain
, __isl_take isl_id
*id
)
8318 space
= isl_union_set_get_space(domain
);
8319 space
= isl_space_add_param_id(space
, isl_id_copy(id
));
8320 aff
= isl_aff_param_on_domain_space_id(space
, id
);
8321 return isl_union_pw_aff_aff_on_domain(domain
, aff
);
8324 /* Internal data structure for isl_union_pw_aff_pw_aff_on_domain.
8325 * "pa" is the piecewise symbolic value that the resulting isl_union_pw_aff
8327 * "res" collects the results.
8329 struct isl_union_pw_aff_pw_aff_on_domain_data
{
8331 isl_union_pw_aff
*res
;
8334 /* Construct a piecewise affine expression that is equal to data->pa
8335 * on "domain" and add the result to data->res.
8337 static isl_stat
pw_aff_on_domain(__isl_take isl_set
*domain
, void *user
)
8339 struct isl_union_pw_aff_pw_aff_on_domain_data
*data
= user
;
8343 pa
= isl_pw_aff_copy(data
->pa
);
8344 dim
= isl_set_dim(domain
, isl_dim_set
);
8346 pa
= isl_pw_aff_free(pa
);
8347 pa
= isl_pw_aff_from_range(pa
);
8348 pa
= isl_pw_aff_add_dims(pa
, isl_dim_in
, dim
);
8349 pa
= isl_pw_aff_reset_domain_space(pa
, isl_set_get_space(domain
));
8350 pa
= isl_pw_aff_intersect_domain(pa
, domain
);
8351 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
8353 return data
->res
? isl_stat_ok
: isl_stat_error
;
8356 /* Return a union piecewise affine expression
8357 * that is equal to "pa" on "domain", assuming "domain" and "pa"
8358 * have been aligned.
8360 * Construct an isl_pw_aff on each of the sets in "domain" and
8361 * collect the results.
8363 static __isl_give isl_union_pw_aff
*isl_union_pw_aff_pw_aff_on_domain_aligned(
8364 __isl_take isl_union_set
*domain
, __isl_take isl_pw_aff
*pa
)
8366 struct isl_union_pw_aff_pw_aff_on_domain_data data
;
8369 space
= isl_union_set_get_space(domain
);
8370 data
.res
= isl_union_pw_aff_empty(space
);
8372 if (isl_union_set_foreach_set(domain
, &pw_aff_on_domain
, &data
) < 0)
8373 data
.res
= isl_union_pw_aff_free(data
.res
);
8374 isl_union_set_free(domain
);
8375 isl_pw_aff_free(pa
);
8379 /* Return a union piecewise affine expression
8380 * that is equal to "pa" on "domain".
8382 * Check that "pa" is a parametric expression,
8383 * align the parameters if needed and call
8384 * isl_union_pw_aff_pw_aff_on_domain_aligned.
8386 __isl_give isl_union_pw_aff
*isl_union_pw_aff_pw_aff_on_domain(
8387 __isl_take isl_union_set
*domain
, __isl_take isl_pw_aff
*pa
)
8390 isl_bool equal_params
;
8391 isl_space
*domain_space
, *pa_space
;
8393 pa_space
= isl_pw_aff_peek_space(pa
);
8394 is_set
= isl_space_is_set(pa_space
);
8398 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
8399 "expecting parametric expression", goto error
);
8401 domain_space
= isl_union_set_get_space(domain
);
8402 pa_space
= isl_pw_aff_get_space(pa
);
8403 equal_params
= isl_space_has_equal_params(domain_space
, pa_space
);
8404 if (equal_params
>= 0 && !equal_params
) {
8407 space
= isl_space_align_params(domain_space
, pa_space
);
8408 pa
= isl_pw_aff_align_params(pa
, isl_space_copy(space
));
8409 domain
= isl_union_set_align_params(domain
, space
);
8411 isl_space_free(domain_space
);
8412 isl_space_free(pa_space
);
8415 if (equal_params
< 0)
8417 return isl_union_pw_aff_pw_aff_on_domain_aligned(domain
, pa
);
8419 isl_union_set_free(domain
);
8420 isl_pw_aff_free(pa
);
8424 /* Internal data structure for isl_union_pw_aff_val_on_domain.
8425 * "v" is the value that the resulting isl_union_pw_aff needs to attain.
8426 * "res" collects the results.
8428 struct isl_union_pw_aff_val_on_domain_data
{
8430 isl_union_pw_aff
*res
;
8433 /* Construct a piecewise affine expression that is equal to data->v
8434 * on "domain" and add the result to data->res.
8436 static isl_stat
pw_aff_val_on_domain(__isl_take isl_set
*domain
, void *user
)
8438 struct isl_union_pw_aff_val_on_domain_data
*data
= user
;
8442 v
= isl_val_copy(data
->v
);
8443 pa
= isl_pw_aff_val_on_domain(domain
, v
);
8444 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
8446 return data
->res
? isl_stat_ok
: isl_stat_error
;
8449 /* Return a union piecewise affine expression
8450 * that is equal to "v" on "domain".
8452 * Construct an isl_pw_aff on each of the sets in "domain" and
8453 * collect the results.
8455 __isl_give isl_union_pw_aff
*isl_union_pw_aff_val_on_domain(
8456 __isl_take isl_union_set
*domain
, __isl_take isl_val
*v
)
8458 struct isl_union_pw_aff_val_on_domain_data data
;
8461 space
= isl_union_set_get_space(domain
);
8462 data
.res
= isl_union_pw_aff_empty(space
);
8464 if (isl_union_set_foreach_set(domain
, &pw_aff_val_on_domain
, &data
) < 0)
8465 data
.res
= isl_union_pw_aff_free(data
.res
);
8466 isl_union_set_free(domain
);
8471 /* Construct a piecewise multi affine expression
8472 * that is equal to "pa" and add it to upma.
8474 static isl_stat
pw_multi_aff_from_pw_aff_entry(__isl_take isl_pw_aff
*pa
,
8477 isl_union_pw_multi_aff
**upma
= user
;
8478 isl_pw_multi_aff
*pma
;
8480 pma
= isl_pw_multi_aff_from_pw_aff(pa
);
8481 *upma
= isl_union_pw_multi_aff_add_pw_multi_aff(*upma
, pma
);
8483 return *upma
? isl_stat_ok
: isl_stat_error
;
8486 /* Construct and return a union piecewise multi affine expression
8487 * that is equal to the given union piecewise affine expression.
8489 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_union_pw_aff(
8490 __isl_take isl_union_pw_aff
*upa
)
8493 isl_union_pw_multi_aff
*upma
;
8498 space
= isl_union_pw_aff_get_space(upa
);
8499 upma
= isl_union_pw_multi_aff_empty(space
);
8501 if (isl_union_pw_aff_foreach_pw_aff(upa
,
8502 &pw_multi_aff_from_pw_aff_entry
, &upma
) < 0)
8503 upma
= isl_union_pw_multi_aff_free(upma
);
8505 isl_union_pw_aff_free(upa
);
8509 /* Compute the set of elements in the domain of "pa" where it is zero and
8510 * add this set to "uset".
8512 static isl_stat
zero_union_set(__isl_take isl_pw_aff
*pa
, void *user
)
8514 isl_union_set
**uset
= (isl_union_set
**)user
;
8516 *uset
= isl_union_set_add_set(*uset
, isl_pw_aff_zero_set(pa
));
8518 return *uset
? isl_stat_ok
: isl_stat_error
;
8521 /* Return a union set containing those elements in the domain
8522 * of "upa" where it is zero.
8524 __isl_give isl_union_set
*isl_union_pw_aff_zero_union_set(
8525 __isl_take isl_union_pw_aff
*upa
)
8527 isl_union_set
*zero
;
8529 zero
= isl_union_set_empty(isl_union_pw_aff_get_space(upa
));
8530 if (isl_union_pw_aff_foreach_pw_aff(upa
, &zero_union_set
, &zero
) < 0)
8531 zero
= isl_union_set_free(zero
);
8533 isl_union_pw_aff_free(upa
);
8537 /* Internal data structure for isl_union_pw_aff_bind_id,
8538 * storing the parameter that needs to be bound and
8539 * the accumulated results.
8541 struct isl_bind_id_data
{
8543 isl_union_set
*bound
;
8546 /* Bind the piecewise affine function "pa" to the parameter data->id,
8547 * adding the resulting elements in the domain where the expression
8548 * is equal to the parameter to data->bound.
8550 static isl_stat
bind_id(__isl_take isl_pw_aff
*pa
, void *user
)
8552 struct isl_bind_id_data
*data
= user
;
8555 bound
= isl_pw_aff_bind_id(pa
, isl_id_copy(data
->id
));
8556 data
->bound
= isl_union_set_add_set(data
->bound
, bound
);
8558 return data
->bound
? isl_stat_ok
: isl_stat_error
;
8561 /* Bind the union piecewise affine function "upa" to the parameter "id",
8562 * returning the elements in the domain where the expression
8563 * is equal to the parameter.
8565 __isl_give isl_union_set
*isl_union_pw_aff_bind_id(
8566 __isl_take isl_union_pw_aff
*upa
, __isl_take isl_id
*id
)
8568 struct isl_bind_id_data data
= { id
};
8570 data
.bound
= isl_union_set_empty(isl_union_pw_aff_get_space(upa
));
8571 if (isl_union_pw_aff_foreach_pw_aff(upa
, &bind_id
, &data
) < 0)
8572 data
.bound
= isl_union_set_free(data
.bound
);
8574 isl_union_pw_aff_free(upa
);
8579 /* Internal data structure for isl_union_pw_aff_pullback_union_pw_multi_aff.
8580 * upma is the function that is plugged in.
8581 * pa is the current part of the function in which upma is plugged in.
8582 * res collects the results.
8584 struct isl_union_pw_aff_pullback_upma_data
{
8585 isl_union_pw_multi_aff
*upma
;
8587 isl_union_pw_aff
*res
;
8590 /* Check if "pma" can be plugged into data->pa.
8591 * If so, perform the pullback and add the result to data->res.
8593 static isl_stat
pa_pb_pma(__isl_take isl_pw_multi_aff
*pma
, void *user
)
8595 struct isl_union_pw_aff_pullback_upma_data
*data
= user
;
8598 if (!isl_space_tuple_is_equal(data
->pa
->dim
, isl_dim_in
,
8599 pma
->dim
, isl_dim_out
)) {
8600 isl_pw_multi_aff_free(pma
);
8604 pa
= isl_pw_aff_copy(data
->pa
);
8605 pa
= isl_pw_aff_pullback_pw_multi_aff(pa
, pma
);
8607 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
8609 return data
->res
? isl_stat_ok
: isl_stat_error
;
8612 /* Check if any of the elements of data->upma can be plugged into pa,
8613 * add if so add the result to data->res.
8615 static isl_stat
upa_pb_upma(__isl_take isl_pw_aff
*pa
, void *user
)
8617 struct isl_union_pw_aff_pullback_upma_data
*data
= user
;
8621 r
= isl_union_pw_multi_aff_foreach_pw_multi_aff(data
->upma
,
8623 isl_pw_aff_free(pa
);
8628 /* Compute the pullback of "upa" by the function represented by "upma".
8629 * In other words, plug in "upma" in "upa". The result contains
8630 * expressions defined over the domain space of "upma".
8632 * Run over all pairs of elements in "upa" and "upma", perform
8633 * the pullback when appropriate and collect the results.
8634 * If the hash value were based on the domain space rather than
8635 * the function space, then we could run through all elements
8636 * of "upma" and directly pick out the corresponding element of "upa".
8638 __isl_give isl_union_pw_aff
*isl_union_pw_aff_pullback_union_pw_multi_aff(
8639 __isl_take isl_union_pw_aff
*upa
,
8640 __isl_take isl_union_pw_multi_aff
*upma
)
8642 struct isl_union_pw_aff_pullback_upma_data data
= { NULL
, NULL
};
8645 space
= isl_union_pw_multi_aff_get_space(upma
);
8646 upa
= isl_union_pw_aff_align_params(upa
, space
);
8647 space
= isl_union_pw_aff_get_space(upa
);
8648 upma
= isl_union_pw_multi_aff_align_params(upma
, space
);
8654 data
.res
= isl_union_pw_aff_alloc_same_size(upa
);
8655 if (isl_union_pw_aff_foreach_pw_aff(upa
, &upa_pb_upma
, &data
) < 0)
8656 data
.res
= isl_union_pw_aff_free(data
.res
);
8658 isl_union_pw_aff_free(upa
);
8659 isl_union_pw_multi_aff_free(upma
);
8662 isl_union_pw_aff_free(upa
);
8663 isl_union_pw_multi_aff_free(upma
);
8668 #define BASE union_pw_aff
8670 #define DOMBASE union_set
8672 #include <isl_multi_explicit_domain.c>
8673 #include <isl_multi_union_pw_aff_explicit_domain.c>
8674 #include <isl_multi_templ.c>
8675 #include <isl_multi_apply_set.c>
8676 #include <isl_multi_apply_union_set.c>
8677 #include <isl_multi_arith_templ.c>
8678 #include <isl_multi_bind_templ.c>
8679 #include <isl_multi_coalesce.c>
8680 #include <isl_multi_dim_id_templ.c>
8681 #include <isl_multi_floor.c>
8682 #include <isl_multi_from_base_templ.c>
8683 #include <isl_multi_gist.c>
8684 #include <isl_multi_align_set.c>
8685 #include <isl_multi_align_union_set.c>
8686 #include <isl_multi_intersect.c>
8687 #include <isl_multi_nan_templ.c>
8688 #include <isl_multi_tuple_id_templ.c>
8689 #include <isl_multi_union_add_templ.c>
8690 #include <isl_multi_zero_space_templ.c>
8692 /* Does "mupa" have a non-trivial explicit domain?
8694 * The explicit domain, if present, is trivial if it represents
8695 * an (obviously) universe parameter set.
8697 isl_bool
isl_multi_union_pw_aff_has_non_trivial_domain(
8698 __isl_keep isl_multi_union_pw_aff
*mupa
)
8700 isl_bool is_params
, trivial
;
8704 return isl_bool_error
;
8705 if (!isl_multi_union_pw_aff_has_explicit_domain(mupa
))
8706 return isl_bool_false
;
8707 is_params
= isl_union_set_is_params(mupa
->u
.dom
);
8708 if (is_params
< 0 || !is_params
)
8709 return isl_bool_not(is_params
);
8710 set
= isl_set_from_union_set(isl_union_set_copy(mupa
->u
.dom
));
8711 trivial
= isl_set_plain_is_universe(set
);
8713 return isl_bool_not(trivial
);
8716 /* Construct a multiple union piecewise affine expression
8717 * in the given space with value zero in each of the output dimensions.
8719 * Since there is no canonical zero value for
8720 * a union piecewise affine expression, we can only construct
8721 * a zero-dimensional "zero" value.
8723 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_zero(
8724 __isl_take isl_space
*space
)
8732 params
= isl_space_is_params(space
);
8736 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
8737 "expecting proper set space", goto error
);
8738 if (!isl_space_is_set(space
))
8739 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
8740 "expecting set space", goto error
);
8741 dim
= isl_space_dim(space
, isl_dim_out
);
8745 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
8746 "expecting 0D space", goto error
);
8748 return isl_multi_union_pw_aff_alloc(space
);
8750 isl_space_free(space
);
8754 /* Construct and return a multi union piecewise affine expression
8755 * that is equal to the given multi affine expression.
8757 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_from_multi_aff(
8758 __isl_take isl_multi_aff
*ma
)
8760 isl_multi_pw_aff
*mpa
;
8762 mpa
= isl_multi_pw_aff_from_multi_aff(ma
);
8763 return isl_multi_union_pw_aff_from_multi_pw_aff(mpa
);
8766 /* This function performs the same operation as
8767 * isl_multi_union_pw_aff_from_multi_aff, but is considered as a function on an
8768 * isl_multi_aff when exported.
8770 __isl_give isl_multi_union_pw_aff
*isl_multi_aff_to_multi_union_pw_aff(
8771 __isl_take isl_multi_aff
*ma
)
8773 return isl_multi_union_pw_aff_from_multi_aff(ma
);
8776 /* Construct and return a multi union piecewise affine expression
8777 * that is equal to the given multi piecewise affine expression.
8779 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_from_multi_pw_aff(
8780 __isl_take isl_multi_pw_aff
*mpa
)
8785 isl_multi_union_pw_aff
*mupa
;
8787 n
= isl_multi_pw_aff_dim(mpa
, isl_dim_out
);
8789 mpa
= isl_multi_pw_aff_free(mpa
);
8793 space
= isl_multi_pw_aff_get_space(mpa
);
8794 space
= isl_space_range(space
);
8795 mupa
= isl_multi_union_pw_aff_alloc(space
);
8797 for (i
= 0; i
< n
; ++i
) {
8799 isl_union_pw_aff
*upa
;
8801 pa
= isl_multi_pw_aff_get_pw_aff(mpa
, i
);
8802 upa
= isl_union_pw_aff_from_pw_aff(pa
);
8803 mupa
= isl_multi_union_pw_aff_restore_check_space(mupa
, i
, upa
);
8806 isl_multi_pw_aff_free(mpa
);
8811 /* Extract the range space of "pma" and assign it to *space.
8812 * If *space has already been set (through a previous call to this function),
8813 * then check that the range space is the same.
8815 static isl_stat
extract_space(__isl_take isl_pw_multi_aff
*pma
, void *user
)
8817 isl_space
**space
= user
;
8818 isl_space
*pma_space
;
8821 pma_space
= isl_space_range(isl_pw_multi_aff_get_space(pma
));
8822 isl_pw_multi_aff_free(pma
);
8825 return isl_stat_error
;
8831 equal
= isl_space_is_equal(pma_space
, *space
);
8832 isl_space_free(pma_space
);
8835 return isl_stat_error
;
8837 isl_die(isl_space_get_ctx(*space
), isl_error_invalid
,
8838 "range spaces not the same", return isl_stat_error
);
8842 /* Construct and return a multi union piecewise affine expression
8843 * that is equal to the given union piecewise multi affine expression.
8845 * In order to be able to perform the conversion, the input
8846 * needs to be non-empty and may only involve a single range space.
8848 * If the resulting multi union piecewise affine expression has
8849 * an explicit domain, then assign it the domain of the input.
8850 * In other cases, the domain is stored in the individual elements.
8852 __isl_give isl_multi_union_pw_aff
*
8853 isl_multi_union_pw_aff_from_union_pw_multi_aff(
8854 __isl_take isl_union_pw_multi_aff
*upma
)
8856 isl_space
*space
= NULL
;
8857 isl_multi_union_pw_aff
*mupa
;
8861 n
= isl_union_pw_multi_aff_n_pw_multi_aff(upma
);
8865 isl_die(isl_union_pw_multi_aff_get_ctx(upma
), isl_error_invalid
,
8866 "cannot extract range space from empty input",
8868 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma
, &extract_space
,
8875 n
= isl_space_dim(space
, isl_dim_set
);
8877 space
= isl_space_free(space
);
8878 mupa
= isl_multi_union_pw_aff_alloc(space
);
8880 for (i
= 0; i
< n
; ++i
) {
8881 isl_union_pw_aff
*upa
;
8883 upa
= isl_union_pw_multi_aff_get_union_pw_aff(upma
, i
);
8884 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
8886 if (isl_multi_union_pw_aff_has_explicit_domain(mupa
)) {
8888 isl_union_pw_multi_aff
*copy
;
8890 copy
= isl_union_pw_multi_aff_copy(upma
);
8891 dom
= isl_union_pw_multi_aff_domain(copy
);
8892 mupa
= isl_multi_union_pw_aff_intersect_domain(mupa
, dom
);
8895 isl_union_pw_multi_aff_free(upma
);
8898 isl_space_free(space
);
8899 isl_union_pw_multi_aff_free(upma
);
8903 /* This function performs the same operation as
8904 * isl_multi_union_pw_aff_from_union_pw_multi_aff,
8905 * but is considered as a function on an isl_union_pw_multi_aff when exported.
8907 __isl_give isl_multi_union_pw_aff
*
8908 isl_union_pw_multi_aff_as_multi_union_pw_aff(
8909 __isl_take isl_union_pw_multi_aff
*upma
)
8911 return isl_multi_union_pw_aff_from_union_pw_multi_aff(upma
);
8914 /* Try and create an isl_multi_union_pw_aff that is equivalent
8915 * to the given isl_union_map.
8916 * The isl_union_map is required to be single-valued in each space.
8917 * Moreover, it cannot be empty and all range spaces need to be the same.
8918 * Otherwise, an error is produced.
8920 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_from_union_map(
8921 __isl_take isl_union_map
*umap
)
8923 isl_union_pw_multi_aff
*upma
;
8925 upma
= isl_union_pw_multi_aff_from_union_map(umap
);
8926 return isl_multi_union_pw_aff_from_union_pw_multi_aff(upma
);
8929 /* This function performs the same operation as
8930 * isl_multi_union_pw_aff_from_union_map,
8931 * but is considered as a function on an isl_union_map when exported.
8933 __isl_give isl_multi_union_pw_aff
*isl_union_map_as_multi_union_pw_aff(
8934 __isl_take isl_union_map
*umap
)
8936 return isl_multi_union_pw_aff_from_union_map(umap
);
8939 /* Return a multiple union piecewise affine expression
8940 * that is equal to "mv" on "domain", assuming "domain" and "mv"
8941 * have been aligned.
8943 * If the resulting multi union piecewise affine expression has
8944 * an explicit domain, then assign it the input domain.
8945 * In other cases, the domain is stored in the individual elements.
8947 static __isl_give isl_multi_union_pw_aff
*
8948 isl_multi_union_pw_aff_multi_val_on_domain_aligned(
8949 __isl_take isl_union_set
*domain
, __isl_take isl_multi_val
*mv
)
8954 isl_multi_union_pw_aff
*mupa
;
8956 n
= isl_multi_val_dim(mv
, isl_dim_set
);
8957 if (!domain
|| n
< 0)
8960 space
= isl_multi_val_get_space(mv
);
8961 mupa
= isl_multi_union_pw_aff_alloc(space
);
8962 for (i
= 0; i
< n
; ++i
) {
8964 isl_union_pw_aff
*upa
;
8966 v
= isl_multi_val_get_val(mv
, i
);
8967 upa
= isl_union_pw_aff_val_on_domain(isl_union_set_copy(domain
),
8969 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
8971 if (isl_multi_union_pw_aff_has_explicit_domain(mupa
))
8972 mupa
= isl_multi_union_pw_aff_intersect_domain(mupa
,
8973 isl_union_set_copy(domain
));
8975 isl_union_set_free(domain
);
8976 isl_multi_val_free(mv
);
8979 isl_union_set_free(domain
);
8980 isl_multi_val_free(mv
);
8984 /* Return a multiple union piecewise affine expression
8985 * that is equal to "mv" on "domain".
8987 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_multi_val_on_domain(
8988 __isl_take isl_union_set
*domain
, __isl_take isl_multi_val
*mv
)
8990 isl_bool equal_params
;
8994 equal_params
= isl_space_has_equal_params(domain
->dim
, mv
->space
);
8995 if (equal_params
< 0)
8998 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(
9000 domain
= isl_union_set_align_params(domain
,
9001 isl_multi_val_get_space(mv
));
9002 mv
= isl_multi_val_align_params(mv
, isl_union_set_get_space(domain
));
9003 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(domain
, mv
);
9005 isl_union_set_free(domain
);
9006 isl_multi_val_free(mv
);
9010 /* Return a multiple union piecewise affine expression
9011 * that is equal to "ma" on "domain".
9013 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_multi_aff_on_domain(
9014 __isl_take isl_union_set
*domain
, __isl_take isl_multi_aff
*ma
)
9016 isl_pw_multi_aff
*pma
;
9018 pma
= isl_pw_multi_aff_from_multi_aff(ma
);
9019 return isl_multi_union_pw_aff_pw_multi_aff_on_domain(domain
, pma
);
9022 /* Return a multiple union piecewise affine expression
9023 * that is equal to "pma" on "domain", assuming "domain" and "pma"
9024 * have been aligned.
9026 * If the resulting multi union piecewise affine expression has
9027 * an explicit domain, then assign it the input domain.
9028 * In other cases, the domain is stored in the individual elements.
9030 static __isl_give isl_multi_union_pw_aff
*
9031 isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(
9032 __isl_take isl_union_set
*domain
, __isl_take isl_pw_multi_aff
*pma
)
9037 isl_multi_union_pw_aff
*mupa
;
9039 n
= isl_pw_multi_aff_dim(pma
, isl_dim_set
);
9040 if (!domain
|| n
< 0)
9042 space
= isl_pw_multi_aff_get_space(pma
);
9043 mupa
= isl_multi_union_pw_aff_alloc(space
);
9044 for (i
= 0; i
< n
; ++i
) {
9046 isl_union_pw_aff
*upa
;
9048 pa
= isl_pw_multi_aff_get_pw_aff(pma
, i
);
9049 upa
= isl_union_pw_aff_pw_aff_on_domain(
9050 isl_union_set_copy(domain
), pa
);
9051 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
9053 if (isl_multi_union_pw_aff_has_explicit_domain(mupa
))
9054 mupa
= isl_multi_union_pw_aff_intersect_domain(mupa
,
9055 isl_union_set_copy(domain
));
9057 isl_union_set_free(domain
);
9058 isl_pw_multi_aff_free(pma
);
9061 isl_union_set_free(domain
);
9062 isl_pw_multi_aff_free(pma
);
9066 /* Return a multiple union piecewise affine expression
9067 * that is equal to "pma" on "domain".
9069 __isl_give isl_multi_union_pw_aff
*
9070 isl_multi_union_pw_aff_pw_multi_aff_on_domain(__isl_take isl_union_set
*domain
,
9071 __isl_take isl_pw_multi_aff
*pma
)
9073 isl_bool equal_params
;
9076 space
= isl_pw_multi_aff_peek_space(pma
);
9077 equal_params
= isl_union_set_space_has_equal_params(domain
, space
);
9078 if (equal_params
< 0)
9081 return isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(
9083 domain
= isl_union_set_align_params(domain
,
9084 isl_pw_multi_aff_get_space(pma
));
9085 pma
= isl_pw_multi_aff_align_params(pma
,
9086 isl_union_set_get_space(domain
));
9087 return isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(domain
,
9090 isl_union_set_free(domain
);
9091 isl_pw_multi_aff_free(pma
);
9095 /* Return a union set containing those elements in the domains
9096 * of the elements of "mupa" where they are all zero.
9098 * If there are no elements, then simply return the entire domain.
9100 __isl_give isl_union_set
*isl_multi_union_pw_aff_zero_union_set(
9101 __isl_take isl_multi_union_pw_aff
*mupa
)
9105 isl_union_pw_aff
*upa
;
9106 isl_union_set
*zero
;
9108 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
9110 mupa
= isl_multi_union_pw_aff_free(mupa
);
9115 return isl_multi_union_pw_aff_domain(mupa
);
9117 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, 0);
9118 zero
= isl_union_pw_aff_zero_union_set(upa
);
9120 for (i
= 1; i
< n
; ++i
) {
9121 isl_union_set
*zero_i
;
9123 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
9124 zero_i
= isl_union_pw_aff_zero_union_set(upa
);
9126 zero
= isl_union_set_intersect(zero
, zero_i
);
9129 isl_multi_union_pw_aff_free(mupa
);
9133 /* Construct a union map mapping the shared domain
9134 * of the union piecewise affine expressions to the range of "mupa"
9135 * in the special case of a 0D multi union piecewise affine expression.
9137 * Construct a map between the explicit domain of "mupa" and
9139 * Note that this assumes that the domain consists of explicit elements.
9141 static __isl_give isl_union_map
*isl_union_map_from_multi_union_pw_aff_0D(
9142 __isl_take isl_multi_union_pw_aff
*mupa
)
9146 isl_union_set
*dom
, *ran
;
9148 space
= isl_multi_union_pw_aff_get_space(mupa
);
9149 dom
= isl_multi_union_pw_aff_domain(mupa
);
9150 ran
= isl_union_set_from_set(isl_set_universe(space
));
9152 is_params
= isl_union_set_is_params(dom
);
9154 dom
= isl_union_set_free(dom
);
9156 isl_die(isl_union_set_get_ctx(dom
), isl_error_invalid
,
9157 "cannot create union map from expression without "
9158 "explicit domain elements",
9159 dom
= isl_union_set_free(dom
));
9161 return isl_union_map_from_domain_and_range(dom
, ran
);
9164 /* Construct a union map mapping the shared domain
9165 * of the union piecewise affine expressions to the range of "mupa"
9166 * with each dimension in the range equated to the
9167 * corresponding union piecewise affine expression.
9169 * If the input is zero-dimensional, then construct a mapping
9170 * from its explicit domain.
9172 __isl_give isl_union_map
*isl_union_map_from_multi_union_pw_aff(
9173 __isl_take isl_multi_union_pw_aff
*mupa
)
9178 isl_union_map
*umap
;
9179 isl_union_pw_aff
*upa
;
9181 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
9183 mupa
= isl_multi_union_pw_aff_free(mupa
);
9188 return isl_union_map_from_multi_union_pw_aff_0D(mupa
);
9190 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, 0);
9191 umap
= isl_union_map_from_union_pw_aff(upa
);
9193 for (i
= 1; i
< n
; ++i
) {
9194 isl_union_map
*umap_i
;
9196 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
9197 umap_i
= isl_union_map_from_union_pw_aff(upa
);
9198 umap
= isl_union_map_flat_range_product(umap
, umap_i
);
9201 space
= isl_multi_union_pw_aff_get_space(mupa
);
9202 umap
= isl_union_map_reset_range_space(umap
, space
);
9204 isl_multi_union_pw_aff_free(mupa
);
9208 /* Internal data structure for isl_union_pw_multi_aff_reset_range_space.
9209 * "range" is the space from which to set the range space.
9210 * "res" collects the results.
9212 struct isl_union_pw_multi_aff_reset_range_space_data
{
9214 isl_union_pw_multi_aff
*res
;
9217 /* Replace the range space of "pma" by the range space of data->range and
9218 * add the result to data->res.
9220 static isl_stat
reset_range_space(__isl_take isl_pw_multi_aff
*pma
, void *user
)
9222 struct isl_union_pw_multi_aff_reset_range_space_data
*data
= user
;
9225 space
= isl_pw_multi_aff_get_space(pma
);
9226 space
= isl_space_domain(space
);
9227 space
= isl_space_extend_domain_with_range(space
,
9228 isl_space_copy(data
->range
));
9229 pma
= isl_pw_multi_aff_reset_space(pma
, space
);
9230 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma
);
9232 return data
->res
? isl_stat_ok
: isl_stat_error
;
9235 /* Replace the range space of all the piecewise affine expressions in "upma" by
9236 * the range space of "space".
9238 * This assumes that all these expressions have the same output dimension.
9240 * Since the spaces of the expressions change, so do their hash values.
9241 * We therefore need to create a new isl_union_pw_multi_aff.
9242 * Note that the hash value is currently computed based on the entire
9243 * space even though there can only be a single expression with a given
9246 static __isl_give isl_union_pw_multi_aff
*
9247 isl_union_pw_multi_aff_reset_range_space(
9248 __isl_take isl_union_pw_multi_aff
*upma
, __isl_take isl_space
*space
)
9250 struct isl_union_pw_multi_aff_reset_range_space_data data
= { space
};
9251 isl_space
*space_upma
;
9253 space_upma
= isl_union_pw_multi_aff_get_space(upma
);
9254 data
.res
= isl_union_pw_multi_aff_empty(space_upma
);
9255 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma
,
9256 &reset_range_space
, &data
) < 0)
9257 data
.res
= isl_union_pw_multi_aff_free(data
.res
);
9259 isl_space_free(space
);
9260 isl_union_pw_multi_aff_free(upma
);
9264 /* Construct and return a union piecewise multi affine expression
9265 * that is equal to the given multi union piecewise affine expression,
9266 * in the special case of a 0D multi union piecewise affine expression.
9268 * Construct a union piecewise multi affine expression
9269 * on top of the explicit domain of the input.
9271 __isl_give isl_union_pw_multi_aff
*
9272 isl_union_pw_multi_aff_from_multi_union_pw_aff_0D(
9273 __isl_take isl_multi_union_pw_aff
*mupa
)
9277 isl_union_set
*domain
;
9279 space
= isl_multi_union_pw_aff_get_space(mupa
);
9280 mv
= isl_multi_val_zero(space
);
9281 domain
= isl_multi_union_pw_aff_domain(mupa
);
9282 return isl_union_pw_multi_aff_multi_val_on_domain(domain
, mv
);
9285 /* Construct and return a union piecewise multi affine expression
9286 * that is equal to the given multi union piecewise affine expression.
9288 * If the input is zero-dimensional, then
9289 * construct a union piecewise multi affine expression
9290 * on top of the explicit domain of the input.
9292 __isl_give isl_union_pw_multi_aff
*
9293 isl_union_pw_multi_aff_from_multi_union_pw_aff(
9294 __isl_take isl_multi_union_pw_aff
*mupa
)
9299 isl_union_pw_multi_aff
*upma
;
9300 isl_union_pw_aff
*upa
;
9302 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
9304 mupa
= isl_multi_union_pw_aff_free(mupa
);
9309 return isl_union_pw_multi_aff_from_multi_union_pw_aff_0D(mupa
);
9311 space
= isl_multi_union_pw_aff_get_space(mupa
);
9312 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, 0);
9313 upma
= isl_union_pw_multi_aff_from_union_pw_aff(upa
);
9315 for (i
= 1; i
< n
; ++i
) {
9316 isl_union_pw_multi_aff
*upma_i
;
9318 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
9319 upma_i
= isl_union_pw_multi_aff_from_union_pw_aff(upa
);
9320 upma
= isl_union_pw_multi_aff_flat_range_product(upma
, upma_i
);
9323 upma
= isl_union_pw_multi_aff_reset_range_space(upma
, space
);
9325 isl_multi_union_pw_aff_free(mupa
);
9329 /* Intersect the range of "mupa" with "range",
9330 * in the special case where "mupa" is 0D.
9332 * Intersect the domain of "mupa" with the constraints on the parameters
9335 static __isl_give isl_multi_union_pw_aff
*mupa_intersect_range_0D(
9336 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_set
*range
)
9338 range
= isl_set_params(range
);
9339 mupa
= isl_multi_union_pw_aff_intersect_params(mupa
, range
);
9343 /* Intersect the range of "mupa" with "range".
9344 * That is, keep only those domain elements that have a function value
9347 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_intersect_range(
9348 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_set
*range
)
9350 isl_union_pw_multi_aff
*upma
;
9351 isl_union_set
*domain
;
9356 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
9357 if (n
< 0 || !range
)
9360 space
= isl_set_get_space(range
);
9361 match
= isl_space_tuple_is_equal(mupa
->space
, isl_dim_set
,
9362 space
, isl_dim_set
);
9363 isl_space_free(space
);
9367 isl_die(isl_multi_union_pw_aff_get_ctx(mupa
), isl_error_invalid
,
9368 "space don't match", goto error
);
9370 return mupa_intersect_range_0D(mupa
, range
);
9372 upma
= isl_union_pw_multi_aff_from_multi_union_pw_aff(
9373 isl_multi_union_pw_aff_copy(mupa
));
9374 domain
= isl_union_set_from_set(range
);
9375 domain
= isl_union_set_preimage_union_pw_multi_aff(domain
, upma
);
9376 mupa
= isl_multi_union_pw_aff_intersect_domain(mupa
, domain
);
9380 isl_multi_union_pw_aff_free(mupa
);
9381 isl_set_free(range
);
9385 /* Return the shared domain of the elements of "mupa",
9386 * in the special case where "mupa" is zero-dimensional.
9388 * Return the explicit domain of "mupa".
9389 * Note that this domain may be a parameter set, either
9390 * because "mupa" is meant to live in a set space or
9391 * because no explicit domain has been set.
9393 __isl_give isl_union_set
*isl_multi_union_pw_aff_domain_0D(
9394 __isl_take isl_multi_union_pw_aff
*mupa
)
9398 dom
= isl_multi_union_pw_aff_get_explicit_domain(mupa
);
9399 isl_multi_union_pw_aff_free(mupa
);
9404 /* Return the shared domain of the elements of "mupa".
9406 * If "mupa" is zero-dimensional, then return its explicit domain.
9408 __isl_give isl_union_set
*isl_multi_union_pw_aff_domain(
9409 __isl_take isl_multi_union_pw_aff
*mupa
)
9413 isl_union_pw_aff
*upa
;
9416 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
9418 mupa
= isl_multi_union_pw_aff_free(mupa
);
9423 return isl_multi_union_pw_aff_domain_0D(mupa
);
9425 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, 0);
9426 dom
= isl_union_pw_aff_domain(upa
);
9427 for (i
= 1; i
< n
; ++i
) {
9428 isl_union_set
*dom_i
;
9430 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
9431 dom_i
= isl_union_pw_aff_domain(upa
);
9432 dom
= isl_union_set_intersect(dom
, dom_i
);
9435 isl_multi_union_pw_aff_free(mupa
);
9439 /* Apply "aff" to "mupa". The space of "mupa" is equal to the domain of "aff".
9440 * In particular, the spaces have been aligned.
9441 * The result is defined over the shared domain of the elements of "mupa"
9443 * We first extract the parametric constant part of "aff" and
9444 * define that over the shared domain.
9445 * Then we iterate over all input dimensions of "aff" and add the corresponding
9446 * multiples of the elements of "mupa".
9447 * Finally, we consider the integer divisions, calling the function
9448 * recursively to obtain an isl_union_pw_aff corresponding to the
9449 * integer division argument.
9451 static __isl_give isl_union_pw_aff
*multi_union_pw_aff_apply_aff(
9452 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_aff
*aff
)
9455 isl_size n_in
, n_div
;
9456 isl_union_pw_aff
*upa
;
9457 isl_union_set
*uset
;
9461 n_in
= isl_aff_dim(aff
, isl_dim_in
);
9462 n_div
= isl_aff_dim(aff
, isl_dim_div
);
9463 if (n_in
< 0 || n_div
< 0)
9466 uset
= isl_multi_union_pw_aff_domain(isl_multi_union_pw_aff_copy(mupa
));
9467 cst
= isl_aff_copy(aff
);
9468 cst
= isl_aff_drop_dims(cst
, isl_dim_div
, 0, n_div
);
9469 cst
= isl_aff_drop_dims(cst
, isl_dim_in
, 0, n_in
);
9470 cst
= isl_aff_project_domain_on_params(cst
);
9471 upa
= isl_union_pw_aff_aff_on_domain(uset
, cst
);
9473 for (i
= 0; i
< n_in
; ++i
) {
9474 isl_union_pw_aff
*upa_i
;
9476 if (!isl_aff_involves_dims(aff
, isl_dim_in
, i
, 1))
9478 v
= isl_aff_get_coefficient_val(aff
, isl_dim_in
, i
);
9479 upa_i
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
9480 upa_i
= isl_union_pw_aff_scale_val(upa_i
, v
);
9481 upa
= isl_union_pw_aff_add(upa
, upa_i
);
9484 for (i
= 0; i
< n_div
; ++i
) {
9486 isl_union_pw_aff
*upa_i
;
9488 if (!isl_aff_involves_dims(aff
, isl_dim_div
, i
, 1))
9490 div
= isl_aff_get_div(aff
, i
);
9491 upa_i
= multi_union_pw_aff_apply_aff(
9492 isl_multi_union_pw_aff_copy(mupa
), div
);
9493 upa_i
= isl_union_pw_aff_floor(upa_i
);
9494 v
= isl_aff_get_coefficient_val(aff
, isl_dim_div
, i
);
9495 upa_i
= isl_union_pw_aff_scale_val(upa_i
, v
);
9496 upa
= isl_union_pw_aff_add(upa
, upa_i
);
9499 isl_multi_union_pw_aff_free(mupa
);
9504 isl_multi_union_pw_aff_free(mupa
);
9509 /* Apply "aff" to "mupa". The space of "mupa" needs to be compatible
9510 * with the domain of "aff".
9511 * Furthermore, the dimension of this space needs to be greater than zero.
9512 * The result is defined over the shared domain of the elements of "mupa"
9514 * We perform these checks and then hand over control to
9515 * multi_union_pw_aff_apply_aff.
9517 __isl_give isl_union_pw_aff
*isl_multi_union_pw_aff_apply_aff(
9518 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_aff
*aff
)
9521 isl_space
*space1
, *space2
;
9524 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
9525 isl_aff_get_space(aff
));
9526 aff
= isl_aff_align_params(aff
, isl_multi_union_pw_aff_get_space(mupa
));
9530 space1
= isl_multi_union_pw_aff_get_space(mupa
);
9531 space2
= isl_aff_get_domain_space(aff
);
9532 equal
= isl_space_is_equal(space1
, space2
);
9533 isl_space_free(space1
);
9534 isl_space_free(space2
);
9538 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
9539 "spaces don't match", goto error
);
9540 dim
= isl_aff_dim(aff
, isl_dim_in
);
9544 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
9545 "cannot determine domains", goto error
);
9547 return multi_union_pw_aff_apply_aff(mupa
, aff
);
9549 isl_multi_union_pw_aff_free(mupa
);
9554 /* Apply "ma" to "mupa", in the special case where "mupa" is 0D.
9555 * The space of "mupa" is known to be compatible with the domain of "ma".
9557 * Construct an isl_multi_union_pw_aff that is equal to "ma"
9558 * on the domain of "mupa".
9560 static __isl_give isl_multi_union_pw_aff
*mupa_apply_multi_aff_0D(
9561 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_multi_aff
*ma
)
9565 dom
= isl_multi_union_pw_aff_domain(mupa
);
9566 ma
= isl_multi_aff_project_domain_on_params(ma
);
9568 return isl_multi_union_pw_aff_multi_aff_on_domain(dom
, ma
);
9571 /* Apply "ma" to "mupa". The space of "mupa" needs to be compatible
9572 * with the domain of "ma".
9573 * The result is defined over the shared domain of the elements of "mupa"
9575 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_apply_multi_aff(
9576 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_multi_aff
*ma
)
9578 isl_space
*space1
, *space2
;
9579 isl_multi_union_pw_aff
*res
;
9582 isl_size n_in
, n_out
;
9584 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
9585 isl_multi_aff_get_space(ma
));
9586 ma
= isl_multi_aff_align_params(ma
,
9587 isl_multi_union_pw_aff_get_space(mupa
));
9588 n_in
= isl_multi_aff_dim(ma
, isl_dim_in
);
9589 n_out
= isl_multi_aff_dim(ma
, isl_dim_out
);
9590 if (!mupa
|| n_in
< 0 || n_out
< 0)
9593 space1
= isl_multi_union_pw_aff_get_space(mupa
);
9594 space2
= isl_multi_aff_get_domain_space(ma
);
9595 equal
= isl_space_is_equal(space1
, space2
);
9596 isl_space_free(space1
);
9597 isl_space_free(space2
);
9601 isl_die(isl_multi_aff_get_ctx(ma
), isl_error_invalid
,
9602 "spaces don't match", goto error
);
9604 return mupa_apply_multi_aff_0D(mupa
, ma
);
9606 space1
= isl_space_range(isl_multi_aff_get_space(ma
));
9607 res
= isl_multi_union_pw_aff_alloc(space1
);
9609 for (i
= 0; i
< n_out
; ++i
) {
9611 isl_union_pw_aff
*upa
;
9613 aff
= isl_multi_aff_get_aff(ma
, i
);
9614 upa
= multi_union_pw_aff_apply_aff(
9615 isl_multi_union_pw_aff_copy(mupa
), aff
);
9616 res
= isl_multi_union_pw_aff_set_union_pw_aff(res
, i
, upa
);
9619 isl_multi_aff_free(ma
);
9620 isl_multi_union_pw_aff_free(mupa
);
9623 isl_multi_union_pw_aff_free(mupa
);
9624 isl_multi_aff_free(ma
);
9628 /* Apply "pa" to "mupa", in the special case where "mupa" is 0D.
9629 * The space of "mupa" is known to be compatible with the domain of "pa".
9631 * Construct an isl_multi_union_pw_aff that is equal to "pa"
9632 * on the domain of "mupa".
9634 static __isl_give isl_union_pw_aff
*isl_multi_union_pw_aff_apply_pw_aff_0D(
9635 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_pw_aff
*pa
)
9639 dom
= isl_multi_union_pw_aff_domain(mupa
);
9640 pa
= isl_pw_aff_project_domain_on_params(pa
);
9642 return isl_union_pw_aff_pw_aff_on_domain(dom
, pa
);
9645 /* Apply "pa" to "mupa". The space of "mupa" needs to be compatible
9646 * with the domain of "pa".
9647 * Furthermore, the dimension of this space needs to be greater than zero.
9648 * The result is defined over the shared domain of the elements of "mupa"
9650 __isl_give isl_union_pw_aff
*isl_multi_union_pw_aff_apply_pw_aff(
9651 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_pw_aff
*pa
)
9656 isl_space
*space
, *space2
;
9657 isl_union_pw_aff
*upa
;
9659 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
9660 isl_pw_aff_get_space(pa
));
9661 pa
= isl_pw_aff_align_params(pa
,
9662 isl_multi_union_pw_aff_get_space(mupa
));
9666 space
= isl_multi_union_pw_aff_get_space(mupa
);
9667 space2
= isl_pw_aff_get_domain_space(pa
);
9668 equal
= isl_space_is_equal(space
, space2
);
9669 isl_space_free(space
);
9670 isl_space_free(space2
);
9674 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
9675 "spaces don't match", goto error
);
9676 n_in
= isl_pw_aff_dim(pa
, isl_dim_in
);
9680 return isl_multi_union_pw_aff_apply_pw_aff_0D(mupa
, pa
);
9682 space
= isl_space_params(isl_multi_union_pw_aff_get_space(mupa
));
9683 upa
= isl_union_pw_aff_empty(space
);
9685 for (i
= 0; i
< pa
->n
; ++i
) {
9688 isl_multi_union_pw_aff
*mupa_i
;
9689 isl_union_pw_aff
*upa_i
;
9691 mupa_i
= isl_multi_union_pw_aff_copy(mupa
);
9692 domain
= isl_set_copy(pa
->p
[i
].set
);
9693 mupa_i
= isl_multi_union_pw_aff_intersect_range(mupa_i
, domain
);
9694 aff
= isl_aff_copy(pa
->p
[i
].aff
);
9695 upa_i
= multi_union_pw_aff_apply_aff(mupa_i
, aff
);
9696 upa
= isl_union_pw_aff_union_add(upa
, upa_i
);
9699 isl_multi_union_pw_aff_free(mupa
);
9700 isl_pw_aff_free(pa
);
9703 isl_multi_union_pw_aff_free(mupa
);
9704 isl_pw_aff_free(pa
);
9708 /* Apply "pma" to "mupa", in the special case where "mupa" is 0D.
9709 * The space of "mupa" is known to be compatible with the domain of "pma".
9711 * Construct an isl_multi_union_pw_aff that is equal to "pma"
9712 * on the domain of "mupa".
9714 static __isl_give isl_multi_union_pw_aff
*mupa_apply_pw_multi_aff_0D(
9715 __isl_take isl_multi_union_pw_aff
*mupa
,
9716 __isl_take isl_pw_multi_aff
*pma
)
9720 dom
= isl_multi_union_pw_aff_domain(mupa
);
9721 pma
= isl_pw_multi_aff_project_domain_on_params(pma
);
9723 return isl_multi_union_pw_aff_pw_multi_aff_on_domain(dom
, pma
);
9726 /* Apply "pma" to "mupa". The space of "mupa" needs to be compatible
9727 * with the domain of "pma".
9728 * The result is defined over the shared domain of the elements of "mupa"
9730 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_apply_pw_multi_aff(
9731 __isl_take isl_multi_union_pw_aff
*mupa
,
9732 __isl_take isl_pw_multi_aff
*pma
)
9734 isl_space
*space1
, *space2
;
9735 isl_multi_union_pw_aff
*res
;
9738 isl_size n_in
, n_out
;
9740 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
9741 isl_pw_multi_aff_get_space(pma
));
9742 pma
= isl_pw_multi_aff_align_params(pma
,
9743 isl_multi_union_pw_aff_get_space(mupa
));
9747 space1
= isl_multi_union_pw_aff_get_space(mupa
);
9748 space2
= isl_pw_multi_aff_get_domain_space(pma
);
9749 equal
= isl_space_is_equal(space1
, space2
);
9750 isl_space_free(space1
);
9751 isl_space_free(space2
);
9755 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
9756 "spaces don't match", goto error
);
9757 n_in
= isl_pw_multi_aff_dim(pma
, isl_dim_in
);
9758 n_out
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
9759 if (n_in
< 0 || n_out
< 0)
9762 return mupa_apply_pw_multi_aff_0D(mupa
, pma
);
9764 space1
= isl_space_range(isl_pw_multi_aff_get_space(pma
));
9765 res
= isl_multi_union_pw_aff_alloc(space1
);
9767 for (i
= 0; i
< n_out
; ++i
) {
9769 isl_union_pw_aff
*upa
;
9771 pa
= isl_pw_multi_aff_get_pw_aff(pma
, i
);
9772 upa
= isl_multi_union_pw_aff_apply_pw_aff(
9773 isl_multi_union_pw_aff_copy(mupa
), pa
);
9774 res
= isl_multi_union_pw_aff_set_union_pw_aff(res
, i
, upa
);
9777 isl_pw_multi_aff_free(pma
);
9778 isl_multi_union_pw_aff_free(mupa
);
9781 isl_multi_union_pw_aff_free(mupa
);
9782 isl_pw_multi_aff_free(pma
);
9786 /* Replace the explicit domain of "mupa" by its preimage under "upma".
9787 * If the explicit domain only keeps track of constraints on the parameters,
9788 * then only update those constraints.
9790 static __isl_give isl_multi_union_pw_aff
*preimage_explicit_domain(
9791 __isl_take isl_multi_union_pw_aff
*mupa
,
9792 __isl_keep isl_union_pw_multi_aff
*upma
)
9796 if (isl_multi_union_pw_aff_check_has_explicit_domain(mupa
) < 0)
9797 return isl_multi_union_pw_aff_free(mupa
);
9799 mupa
= isl_multi_union_pw_aff_cow(mupa
);
9803 is_params
= isl_union_set_is_params(mupa
->u
.dom
);
9805 return isl_multi_union_pw_aff_free(mupa
);
9807 upma
= isl_union_pw_multi_aff_copy(upma
);
9809 mupa
->u
.dom
= isl_union_set_intersect_params(mupa
->u
.dom
,
9810 isl_union_set_params(isl_union_pw_multi_aff_domain(upma
)));
9812 mupa
->u
.dom
= isl_union_set_preimage_union_pw_multi_aff(
9815 return isl_multi_union_pw_aff_free(mupa
);
9819 /* Compute the pullback of "mupa" by the function represented by "upma".
9820 * In other words, plug in "upma" in "mupa". The result contains
9821 * expressions defined over the domain space of "upma".
9823 * Run over all elements of "mupa" and plug in "upma" in each of them.
9825 * If "mupa" has an explicit domain, then it is this domain
9826 * that needs to undergo a pullback instead, i.e., a preimage.
9828 __isl_give isl_multi_union_pw_aff
*
9829 isl_multi_union_pw_aff_pullback_union_pw_multi_aff(
9830 __isl_take isl_multi_union_pw_aff
*mupa
,
9831 __isl_take isl_union_pw_multi_aff
*upma
)
9836 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
9837 isl_union_pw_multi_aff_get_space(upma
));
9838 upma
= isl_union_pw_multi_aff_align_params(upma
,
9839 isl_multi_union_pw_aff_get_space(mupa
));
9840 mupa
= isl_multi_union_pw_aff_cow(mupa
);
9841 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
9845 for (i
= 0; i
< n
; ++i
) {
9846 isl_union_pw_aff
*upa
;
9848 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
9849 upa
= isl_union_pw_aff_pullback_union_pw_multi_aff(upa
,
9850 isl_union_pw_multi_aff_copy(upma
));
9851 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
9854 if (isl_multi_union_pw_aff_has_explicit_domain(mupa
))
9855 mupa
= preimage_explicit_domain(mupa
, upma
);
9857 isl_union_pw_multi_aff_free(upma
);
9860 isl_multi_union_pw_aff_free(mupa
);
9861 isl_union_pw_multi_aff_free(upma
);
9865 /* Extract the sequence of elements in "mupa" with domain space "space"
9866 * (ignoring parameters).
9868 * For the elements of "mupa" that are not defined on the specified space,
9869 * the corresponding element in the result is empty.
9871 __isl_give isl_multi_pw_aff
*isl_multi_union_pw_aff_extract_multi_pw_aff(
9872 __isl_keep isl_multi_union_pw_aff
*mupa
, __isl_take isl_space
*space
)
9876 isl_space
*space_mpa
;
9877 isl_multi_pw_aff
*mpa
;
9879 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
9880 if (n
< 0 || !space
)
9883 space_mpa
= isl_multi_union_pw_aff_get_space(mupa
);
9884 space
= isl_space_replace_params(space
, space_mpa
);
9885 space_mpa
= isl_space_map_from_domain_and_range(isl_space_copy(space
),
9887 mpa
= isl_multi_pw_aff_alloc(space_mpa
);
9889 space
= isl_space_from_domain(space
);
9890 space
= isl_space_add_dims(space
, isl_dim_out
, 1);
9891 for (i
= 0; i
< n
; ++i
) {
9892 isl_union_pw_aff
*upa
;
9895 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
9896 pa
= isl_union_pw_aff_extract_pw_aff(upa
,
9897 isl_space_copy(space
));
9898 mpa
= isl_multi_pw_aff_set_pw_aff(mpa
, i
, pa
);
9899 isl_union_pw_aff_free(upa
);
9902 isl_space_free(space
);
9905 isl_space_free(space
);
9909 /* Data structure that specifies how isl_union_pw_multi_aff_un_op
9910 * should modify the base expressions in the input.
9912 * If "filter" is not NULL, then only the base expressions that satisfy "filter"
9913 * are taken into account.
9914 * "fn" is applied to each entry in the input.
9916 struct isl_union_pw_multi_aff_un_op_control
{
9917 isl_bool (*filter
)(__isl_keep isl_pw_multi_aff
*part
);
9918 __isl_give isl_pw_multi_aff
*(*fn
)(__isl_take isl_pw_multi_aff
*pma
);
9921 /* Wrapper for isl_union_pw_multi_aff_un_op filter functions (which do not take
9922 * a second argument) for use as an isl_union_pw_multi_aff_transform
9923 * filter function (which does take a second argument).
9924 * Simply call control->filter without the second argument.
9926 static isl_bool
isl_union_pw_multi_aff_un_op_filter_drop_user(
9927 __isl_take isl_pw_multi_aff
*pma
, void *user
)
9929 struct isl_union_pw_multi_aff_un_op_control
*control
= user
;
9931 return control
->filter(pma
);
9934 /* Wrapper for isl_union_pw_multi_aff_un_op base functions (which do not take
9935 * a second argument) for use as an isl_union_pw_multi_aff_transform
9936 * base function (which does take a second argument).
9937 * Simply call control->fn without the second argument.
9939 static __isl_give isl_pw_multi_aff
*isl_union_pw_multi_aff_un_op_drop_user(
9940 __isl_take isl_pw_multi_aff
*pma
, void *user
)
9942 struct isl_union_pw_multi_aff_un_op_control
*control
= user
;
9944 return control
->fn(pma
);
9947 /* Construct an isl_union_pw_multi_aff that is obtained by
9948 * modifying "upma" according to "control".
9950 * isl_union_pw_multi_aff_transform performs essentially
9951 * the same operation, but takes a filter and a callback function
9952 * of a different form (with an extra argument).
9953 * Call isl_union_pw_multi_aff_transform with wrappers
9954 * that remove this extra argument.
9956 static __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_un_op(
9957 __isl_take isl_union_pw_multi_aff
*upma
,
9958 struct isl_union_pw_multi_aff_un_op_control
*control
)
9960 struct isl_union_pw_multi_aff_transform_control t_control
= {
9961 .filter
= &isl_union_pw_multi_aff_un_op_filter_drop_user
,
9962 .filter_user
= control
,
9963 .fn
= &isl_union_pw_multi_aff_un_op_drop_user
,
9967 return isl_union_pw_multi_aff_transform(upma
, &t_control
);
9970 /* For each function in "upma" of the form A -> [B -> C],
9971 * extract the function A -> B and collect the results.
9973 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_range_factor_domain(
9974 __isl_take isl_union_pw_multi_aff
*upma
)
9976 struct isl_union_pw_multi_aff_un_op_control control
= {
9977 .filter
= &isl_pw_multi_aff_range_is_wrapping
,
9978 .fn
= &isl_pw_multi_aff_range_factor_domain
,
9980 return isl_union_pw_multi_aff_un_op(upma
, &control
);
9983 /* For each function in "upma" of the form A -> [B -> C],
9984 * extract the function A -> C and collect the results.
9986 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_range_factor_range(
9987 __isl_take isl_union_pw_multi_aff
*upma
)
9989 struct isl_union_pw_multi_aff_un_op_control control
= {
9990 .filter
= &isl_pw_multi_aff_range_is_wrapping
,
9991 .fn
= &isl_pw_multi_aff_range_factor_range
,
9993 return isl_union_pw_multi_aff_un_op(upma
, &control
);
9996 /* Evaluate the affine function "aff" in the void point "pnt".
9997 * In particular, return the value NaN.
9999 static __isl_give isl_val
*eval_void(__isl_take isl_aff
*aff
,
10000 __isl_take isl_point
*pnt
)
10004 ctx
= isl_point_get_ctx(pnt
);
10006 isl_point_free(pnt
);
10007 return isl_val_nan(ctx
);
10010 /* Evaluate the affine expression "aff"
10011 * in the coordinates (with denominator) "pnt".
10013 static __isl_give isl_val
*eval(__isl_keep isl_vec
*aff
,
10014 __isl_keep isl_vec
*pnt
)
10023 ctx
= isl_vec_get_ctx(aff
);
10026 isl_seq_inner_product(aff
->el
+ 1, pnt
->el
, pnt
->size
, &n
);
10027 isl_int_mul(d
, aff
->el
[0], pnt
->el
[0]);
10028 v
= isl_val_rat_from_isl_int(ctx
, n
, d
);
10029 v
= isl_val_normalize(v
);
10036 /* Check that the domain space of "aff" is equal to "space".
10038 static isl_stat
isl_aff_check_has_domain_space(__isl_keep isl_aff
*aff
,
10039 __isl_keep isl_space
*space
)
10043 ok
= isl_space_is_equal(isl_aff_peek_domain_space(aff
), space
);
10045 return isl_stat_error
;
10047 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
10048 "incompatible spaces", return isl_stat_error
);
10049 return isl_stat_ok
;
10052 /* Evaluate the affine function "aff" in "pnt".
10054 __isl_give isl_val
*isl_aff_eval(__isl_take isl_aff
*aff
,
10055 __isl_take isl_point
*pnt
)
10059 isl_local_space
*ls
;
10061 if (isl_aff_check_has_domain_space(aff
, isl_point_peek_space(pnt
)) < 0)
10063 is_void
= isl_point_is_void(pnt
);
10067 return eval_void(aff
, pnt
);
10069 ls
= isl_aff_get_domain_local_space(aff
);
10070 pnt
= isl_local_space_lift_point(ls
, pnt
);
10072 v
= eval(aff
->v
, isl_point_peek_vec(pnt
));
10075 isl_point_free(pnt
);
10080 isl_point_free(pnt
);