2 * Copyright 2011 INRIA Saclay
3 * Copyright 2011 Sven Verdoolaege
4 * Copyright 2012-2014 Ecole Normale Superieure
5 * Copyright 2014 INRIA Rocquencourt
7 * Use of this software is governed by the MIT license
9 * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
10 * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
12 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
13 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
14 * B.P. 105 - 78153 Le Chesnay, France
17 #include <isl_ctx_private.h>
19 #include <isl_map_private.h>
20 #include <isl_union_map_private.h>
21 #include <isl_aff_private.h>
22 #include <isl_space_private.h>
23 #include <isl_local_space_private.h>
24 #include <isl_vec_private.h>
25 #include <isl_mat_private.h>
26 #include <isl/constraint.h>
29 #include <isl_val_private.h>
30 #include <isl/deprecated/aff_int.h>
31 #include <isl_config.h>
36 #include <isl_list_templ.c>
41 #include <isl_list_templ.c>
44 #define BASE union_pw_aff
46 #include <isl_list_templ.c>
49 #define BASE union_pw_multi_aff
51 #include <isl_list_templ.c>
53 __isl_give isl_aff
*isl_aff_alloc_vec(__isl_take isl_local_space
*ls
,
54 __isl_take isl_vec
*v
)
61 aff
= isl_calloc_type(v
->ctx
, struct isl_aff
);
71 isl_local_space_free(ls
);
76 __isl_give isl_aff
*isl_aff_alloc(__isl_take isl_local_space
*ls
)
85 ctx
= isl_local_space_get_ctx(ls
);
86 if (!isl_local_space_divs_known(ls
))
87 isl_die(ctx
, isl_error_invalid
, "local space has unknown divs",
89 if (!isl_local_space_is_set(ls
))
90 isl_die(ctx
, isl_error_invalid
,
91 "domain of affine expression should be a set",
94 total
= isl_local_space_dim(ls
, isl_dim_all
);
95 v
= isl_vec_alloc(ctx
, 1 + 1 + total
);
96 return isl_aff_alloc_vec(ls
, v
);
98 isl_local_space_free(ls
);
102 __isl_give isl_aff
*isl_aff_zero_on_domain(__isl_take isl_local_space
*ls
)
106 aff
= isl_aff_alloc(ls
);
110 isl_int_set_si(aff
->v
->el
[0], 1);
111 isl_seq_clr(aff
->v
->el
+ 1, aff
->v
->size
- 1);
116 /* Return a piecewise affine expression defined on the specified domain
117 * that is equal to zero.
119 __isl_give isl_pw_aff
*isl_pw_aff_zero_on_domain(__isl_take isl_local_space
*ls
)
121 return isl_pw_aff_from_aff(isl_aff_zero_on_domain(ls
));
124 /* Return an affine expression defined on the specified domain
125 * that represents NaN.
127 __isl_give isl_aff
*isl_aff_nan_on_domain(__isl_take isl_local_space
*ls
)
131 aff
= isl_aff_alloc(ls
);
135 isl_seq_clr(aff
->v
->el
, aff
->v
->size
);
140 /* Return a piecewise affine expression defined on the specified domain
141 * that represents NaN.
143 __isl_give isl_pw_aff
*isl_pw_aff_nan_on_domain(__isl_take isl_local_space
*ls
)
145 return isl_pw_aff_from_aff(isl_aff_nan_on_domain(ls
));
148 /* Return an affine expression that is equal to "val" on
149 * domain local space "ls".
151 __isl_give isl_aff
*isl_aff_val_on_domain(__isl_take isl_local_space
*ls
,
152 __isl_take isl_val
*val
)
158 if (!isl_val_is_rat(val
))
159 isl_die(isl_val_get_ctx(val
), isl_error_invalid
,
160 "expecting rational value", goto error
);
162 aff
= isl_aff_alloc(isl_local_space_copy(ls
));
166 isl_seq_clr(aff
->v
->el
+ 2, aff
->v
->size
- 2);
167 isl_int_set(aff
->v
->el
[1], val
->n
);
168 isl_int_set(aff
->v
->el
[0], val
->d
);
170 isl_local_space_free(ls
);
174 isl_local_space_free(ls
);
179 /* Return an affine expression that is equal to the specified dimension
182 __isl_give isl_aff
*isl_aff_var_on_domain(__isl_take isl_local_space
*ls
,
183 enum isl_dim_type type
, unsigned pos
)
191 space
= isl_local_space_get_space(ls
);
194 if (isl_space_is_map(space
))
195 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
196 "expecting (parameter) set space", goto error
);
197 if (pos
>= isl_local_space_dim(ls
, type
))
198 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
199 "position out of bounds", goto error
);
201 isl_space_free(space
);
202 aff
= isl_aff_alloc(ls
);
206 pos
+= isl_local_space_offset(aff
->ls
, type
);
208 isl_int_set_si(aff
->v
->el
[0], 1);
209 isl_seq_clr(aff
->v
->el
+ 1, aff
->v
->size
- 1);
210 isl_int_set_si(aff
->v
->el
[1 + pos
], 1);
214 isl_local_space_free(ls
);
215 isl_space_free(space
);
219 /* Return a piecewise affine expression that is equal to
220 * the specified dimension in "ls".
222 __isl_give isl_pw_aff
*isl_pw_aff_var_on_domain(__isl_take isl_local_space
*ls
,
223 enum isl_dim_type type
, unsigned pos
)
225 return isl_pw_aff_from_aff(isl_aff_var_on_domain(ls
, type
, pos
));
228 __isl_give isl_aff
*isl_aff_copy(__isl_keep isl_aff
*aff
)
237 __isl_give isl_aff
*isl_aff_dup(__isl_keep isl_aff
*aff
)
242 return isl_aff_alloc_vec(isl_local_space_copy(aff
->ls
),
243 isl_vec_copy(aff
->v
));
246 __isl_give isl_aff
*isl_aff_cow(__isl_take isl_aff
*aff
)
254 return isl_aff_dup(aff
);
257 __isl_null isl_aff
*isl_aff_free(__isl_take isl_aff
*aff
)
265 isl_local_space_free(aff
->ls
);
266 isl_vec_free(aff
->v
);
273 isl_ctx
*isl_aff_get_ctx(__isl_keep isl_aff
*aff
)
275 return aff
? isl_local_space_get_ctx(aff
->ls
) : NULL
;
278 /* Externally, an isl_aff has a map space, but internally, the
279 * ls field corresponds to the domain of that space.
281 int isl_aff_dim(__isl_keep isl_aff
*aff
, enum isl_dim_type type
)
285 if (type
== isl_dim_out
)
287 if (type
== isl_dim_in
)
289 return isl_local_space_dim(aff
->ls
, type
);
292 /* Return the position of the dimension of the given type and name
294 * Return -1 if no such dimension can be found.
296 int isl_aff_find_dim_by_name(__isl_keep isl_aff
*aff
, enum isl_dim_type type
,
301 if (type
== isl_dim_out
)
303 if (type
== isl_dim_in
)
305 return isl_local_space_find_dim_by_name(aff
->ls
, type
, name
);
308 __isl_give isl_space
*isl_aff_get_domain_space(__isl_keep isl_aff
*aff
)
310 return aff
? isl_local_space_get_space(aff
->ls
) : NULL
;
313 __isl_give isl_space
*isl_aff_get_space(__isl_keep isl_aff
*aff
)
318 space
= isl_local_space_get_space(aff
->ls
);
319 space
= isl_space_from_domain(space
);
320 space
= isl_space_add_dims(space
, isl_dim_out
, 1);
324 __isl_give isl_local_space
*isl_aff_get_domain_local_space(
325 __isl_keep isl_aff
*aff
)
327 return aff
? isl_local_space_copy(aff
->ls
) : NULL
;
330 __isl_give isl_local_space
*isl_aff_get_local_space(__isl_keep isl_aff
*aff
)
335 ls
= isl_local_space_copy(aff
->ls
);
336 ls
= isl_local_space_from_domain(ls
);
337 ls
= isl_local_space_add_dims(ls
, isl_dim_out
, 1);
341 /* Externally, an isl_aff has a map space, but internally, the
342 * ls field corresponds to the domain of that space.
344 const char *isl_aff_get_dim_name(__isl_keep isl_aff
*aff
,
345 enum isl_dim_type type
, unsigned pos
)
349 if (type
== isl_dim_out
)
351 if (type
== isl_dim_in
)
353 return isl_local_space_get_dim_name(aff
->ls
, type
, pos
);
356 __isl_give isl_aff
*isl_aff_reset_domain_space(__isl_take isl_aff
*aff
,
357 __isl_take isl_space
*dim
)
359 aff
= isl_aff_cow(aff
);
363 aff
->ls
= isl_local_space_reset_space(aff
->ls
, dim
);
365 return isl_aff_free(aff
);
374 /* Reset the space of "aff". This function is called from isl_pw_templ.c
375 * and doesn't know if the space of an element object is represented
376 * directly or through its domain. It therefore passes along both.
378 __isl_give isl_aff
*isl_aff_reset_space_and_domain(__isl_take isl_aff
*aff
,
379 __isl_take isl_space
*space
, __isl_take isl_space
*domain
)
381 isl_space_free(space
);
382 return isl_aff_reset_domain_space(aff
, domain
);
385 /* Reorder the coefficients of the affine expression based
386 * on the given reodering.
387 * The reordering r is assumed to have been extended with the local
390 static __isl_give isl_vec
*vec_reorder(__isl_take isl_vec
*vec
,
391 __isl_take isl_reordering
*r
, int n_div
)
399 res
= isl_vec_alloc(vec
->ctx
,
400 2 + isl_space_dim(r
->dim
, isl_dim_all
) + n_div
);
401 isl_seq_cpy(res
->el
, vec
->el
, 2);
402 isl_seq_clr(res
->el
+ 2, res
->size
- 2);
403 for (i
= 0; i
< r
->len
; ++i
)
404 isl_int_set(res
->el
[2 + r
->pos
[i
]], vec
->el
[2 + i
]);
406 isl_reordering_free(r
);
411 isl_reordering_free(r
);
415 /* Reorder the dimensions of the domain of "aff" according
416 * to the given reordering.
418 __isl_give isl_aff
*isl_aff_realign_domain(__isl_take isl_aff
*aff
,
419 __isl_take isl_reordering
*r
)
421 aff
= isl_aff_cow(aff
);
425 r
= isl_reordering_extend(r
, aff
->ls
->div
->n_row
);
426 aff
->v
= vec_reorder(aff
->v
, isl_reordering_copy(r
),
427 aff
->ls
->div
->n_row
);
428 aff
->ls
= isl_local_space_realign(aff
->ls
, r
);
430 if (!aff
->v
|| !aff
->ls
)
431 return isl_aff_free(aff
);
436 isl_reordering_free(r
);
440 __isl_give isl_aff
*isl_aff_align_params(__isl_take isl_aff
*aff
,
441 __isl_take isl_space
*model
)
446 if (!isl_space_match(aff
->ls
->dim
, isl_dim_param
,
447 model
, isl_dim_param
)) {
450 model
= isl_space_drop_dims(model
, isl_dim_in
,
451 0, isl_space_dim(model
, isl_dim_in
));
452 model
= isl_space_drop_dims(model
, isl_dim_out
,
453 0, isl_space_dim(model
, isl_dim_out
));
454 exp
= isl_parameter_alignment_reordering(aff
->ls
->dim
, model
);
455 exp
= isl_reordering_extend_space(exp
,
456 isl_aff_get_domain_space(aff
));
457 aff
= isl_aff_realign_domain(aff
, exp
);
460 isl_space_free(model
);
463 isl_space_free(model
);
468 /* Is "aff" obviously equal to zero?
470 * If the denominator is zero, then "aff" is not equal to zero.
472 int isl_aff_plain_is_zero(__isl_keep isl_aff
*aff
)
477 if (isl_int_is_zero(aff
->v
->el
[0]))
479 return isl_seq_first_non_zero(aff
->v
->el
+ 1, aff
->v
->size
- 1) < 0;
482 /* Does "aff" represent NaN?
484 int isl_aff_is_nan(__isl_keep isl_aff
*aff
)
489 return isl_seq_first_non_zero(aff
->v
->el
, 2) < 0;
492 /* Does "pa" involve any NaNs?
494 int isl_pw_aff_involves_nan(__isl_keep isl_pw_aff
*pa
)
503 for (i
= 0; i
< pa
->n
; ++i
) {
504 int is_nan
= isl_aff_is_nan(pa
->p
[i
].aff
);
505 if (is_nan
< 0 || is_nan
)
512 /* Are "aff1" and "aff2" obviously equal?
514 * NaN is not equal to anything, not even to another NaN.
516 int isl_aff_plain_is_equal(__isl_keep isl_aff
*aff1
, __isl_keep isl_aff
*aff2
)
523 if (isl_aff_is_nan(aff1
) || isl_aff_is_nan(aff2
))
526 equal
= isl_local_space_is_equal(aff1
->ls
, aff2
->ls
);
527 if (equal
< 0 || !equal
)
530 return isl_vec_is_equal(aff1
->v
, aff2
->v
);
533 /* Return the common denominator of "aff" in "v".
535 * We cannot return anything meaningful in case of a NaN.
537 int isl_aff_get_denominator(__isl_keep isl_aff
*aff
, isl_int
*v
)
541 if (isl_aff_is_nan(aff
))
542 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
543 "cannot get denominator of NaN", return -1);
544 isl_int_set(*v
, aff
->v
->el
[0]);
548 /* Return the common denominator of "aff".
550 __isl_give isl_val
*isl_aff_get_denominator_val(__isl_keep isl_aff
*aff
)
557 ctx
= isl_aff_get_ctx(aff
);
558 if (isl_aff_is_nan(aff
))
559 return isl_val_nan(ctx
);
560 return isl_val_int_from_isl_int(ctx
, aff
->v
->el
[0]);
563 /* Return the constant term of "aff" in "v".
565 * We cannot return anything meaningful in case of a NaN.
567 int isl_aff_get_constant(__isl_keep isl_aff
*aff
, isl_int
*v
)
571 if (isl_aff_is_nan(aff
))
572 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
573 "cannot get constant term of NaN", return -1);
574 isl_int_set(*v
, aff
->v
->el
[1]);
578 /* Return the constant term of "aff".
580 __isl_give isl_val
*isl_aff_get_constant_val(__isl_keep isl_aff
*aff
)
588 ctx
= isl_aff_get_ctx(aff
);
589 if (isl_aff_is_nan(aff
))
590 return isl_val_nan(ctx
);
591 v
= isl_val_rat_from_isl_int(ctx
, aff
->v
->el
[1], aff
->v
->el
[0]);
592 return isl_val_normalize(v
);
595 /* Return the coefficient of the variable of type "type" at position "pos"
598 * We cannot return anything meaningful in case of a NaN.
600 int isl_aff_get_coefficient(__isl_keep isl_aff
*aff
,
601 enum isl_dim_type type
, int pos
, isl_int
*v
)
606 if (type
== isl_dim_out
)
607 isl_die(aff
->v
->ctx
, isl_error_invalid
,
608 "output/set dimension does not have a coefficient",
610 if (type
== isl_dim_in
)
613 if (pos
>= isl_local_space_dim(aff
->ls
, type
))
614 isl_die(aff
->v
->ctx
, isl_error_invalid
,
615 "position out of bounds", return -1);
617 if (isl_aff_is_nan(aff
))
618 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
619 "cannot get coefficient of NaN", return -1);
620 pos
+= isl_local_space_offset(aff
->ls
, type
);
621 isl_int_set(*v
, aff
->v
->el
[1 + pos
]);
626 /* Return the coefficient of the variable of type "type" at position "pos"
629 __isl_give isl_val
*isl_aff_get_coefficient_val(__isl_keep isl_aff
*aff
,
630 enum isl_dim_type type
, int pos
)
638 ctx
= isl_aff_get_ctx(aff
);
639 if (type
== isl_dim_out
)
640 isl_die(ctx
, isl_error_invalid
,
641 "output/set dimension does not have a coefficient",
643 if (type
== isl_dim_in
)
646 if (pos
>= isl_local_space_dim(aff
->ls
, type
))
647 isl_die(ctx
, isl_error_invalid
,
648 "position out of bounds", return NULL
);
650 if (isl_aff_is_nan(aff
))
651 return isl_val_nan(ctx
);
652 pos
+= isl_local_space_offset(aff
->ls
, type
);
653 v
= isl_val_rat_from_isl_int(ctx
, aff
->v
->el
[1 + pos
], aff
->v
->el
[0]);
654 return isl_val_normalize(v
);
657 /* Return the sign of the coefficient of the variable of type "type"
658 * at position "pos" of "aff".
660 int isl_aff_coefficient_sgn(__isl_keep isl_aff
*aff
, enum isl_dim_type type
,
668 ctx
= isl_aff_get_ctx(aff
);
669 if (type
== isl_dim_out
)
670 isl_die(ctx
, isl_error_invalid
,
671 "output/set dimension does not have a coefficient",
673 if (type
== isl_dim_in
)
676 if (pos
>= isl_local_space_dim(aff
->ls
, type
))
677 isl_die(ctx
, isl_error_invalid
,
678 "position out of bounds", return 0);
680 pos
+= isl_local_space_offset(aff
->ls
, type
);
681 return isl_int_sgn(aff
->v
->el
[1 + pos
]);
684 /* Replace the denominator of "aff" by "v".
686 * A NaN is unaffected by this operation.
688 __isl_give isl_aff
*isl_aff_set_denominator(__isl_take isl_aff
*aff
, isl_int v
)
692 if (isl_aff_is_nan(aff
))
694 aff
= isl_aff_cow(aff
);
698 aff
->v
= isl_vec_cow(aff
->v
);
700 return isl_aff_free(aff
);
702 isl_int_set(aff
->v
->el
[0], v
);
707 /* Replace the numerator of the constant term of "aff" by "v".
709 * A NaN is unaffected by this operation.
711 __isl_give isl_aff
*isl_aff_set_constant(__isl_take isl_aff
*aff
, isl_int v
)
715 if (isl_aff_is_nan(aff
))
717 aff
= isl_aff_cow(aff
);
721 aff
->v
= isl_vec_cow(aff
->v
);
723 return isl_aff_free(aff
);
725 isl_int_set(aff
->v
->el
[1], v
);
730 /* Replace the constant term of "aff" by "v".
732 * A NaN is unaffected by this operation.
734 __isl_give isl_aff
*isl_aff_set_constant_val(__isl_take isl_aff
*aff
,
735 __isl_take isl_val
*v
)
740 if (isl_aff_is_nan(aff
)) {
745 if (!isl_val_is_rat(v
))
746 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
747 "expecting rational value", goto error
);
749 if (isl_int_eq(aff
->v
->el
[1], v
->n
) &&
750 isl_int_eq(aff
->v
->el
[0], v
->d
)) {
755 aff
= isl_aff_cow(aff
);
758 aff
->v
= isl_vec_cow(aff
->v
);
762 if (isl_int_eq(aff
->v
->el
[0], v
->d
)) {
763 isl_int_set(aff
->v
->el
[1], v
->n
);
764 } else if (isl_int_is_one(v
->d
)) {
765 isl_int_mul(aff
->v
->el
[1], aff
->v
->el
[0], v
->n
);
767 isl_seq_scale(aff
->v
->el
+ 1,
768 aff
->v
->el
+ 1, v
->d
, aff
->v
->size
- 1);
769 isl_int_mul(aff
->v
->el
[1], aff
->v
->el
[0], v
->n
);
770 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], v
->d
);
771 aff
->v
= isl_vec_normalize(aff
->v
);
784 /* Add "v" to the constant term of "aff".
786 * A NaN is unaffected by this operation.
788 __isl_give isl_aff
*isl_aff_add_constant(__isl_take isl_aff
*aff
, isl_int v
)
790 if (isl_int_is_zero(v
))
795 if (isl_aff_is_nan(aff
))
797 aff
= isl_aff_cow(aff
);
801 aff
->v
= isl_vec_cow(aff
->v
);
803 return isl_aff_free(aff
);
805 isl_int_addmul(aff
->v
->el
[1], aff
->v
->el
[0], v
);
810 /* Add "v" to the constant term of "aff".
812 * A NaN is unaffected by this operation.
814 __isl_give isl_aff
*isl_aff_add_constant_val(__isl_take isl_aff
*aff
,
815 __isl_take isl_val
*v
)
820 if (isl_aff_is_nan(aff
) || isl_val_is_zero(v
)) {
825 if (!isl_val_is_rat(v
))
826 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
827 "expecting rational value", goto error
);
829 aff
= isl_aff_cow(aff
);
833 aff
->v
= isl_vec_cow(aff
->v
);
837 if (isl_int_is_one(v
->d
)) {
838 isl_int_addmul(aff
->v
->el
[1], aff
->v
->el
[0], v
->n
);
839 } else if (isl_int_eq(aff
->v
->el
[0], v
->d
)) {
840 isl_int_add(aff
->v
->el
[1], aff
->v
->el
[1], v
->n
);
841 aff
->v
= isl_vec_normalize(aff
->v
);
845 isl_seq_scale(aff
->v
->el
+ 1,
846 aff
->v
->el
+ 1, v
->d
, aff
->v
->size
- 1);
847 isl_int_addmul(aff
->v
->el
[1], aff
->v
->el
[0], v
->n
);
848 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], v
->d
);
849 aff
->v
= isl_vec_normalize(aff
->v
);
862 __isl_give isl_aff
*isl_aff_add_constant_si(__isl_take isl_aff
*aff
, int v
)
867 isl_int_set_si(t
, v
);
868 aff
= isl_aff_add_constant(aff
, t
);
874 /* Add "v" to the numerator of the constant term of "aff".
876 * A NaN is unaffected by this operation.
878 __isl_give isl_aff
*isl_aff_add_constant_num(__isl_take isl_aff
*aff
, isl_int v
)
880 if (isl_int_is_zero(v
))
885 if (isl_aff_is_nan(aff
))
887 aff
= isl_aff_cow(aff
);
891 aff
->v
= isl_vec_cow(aff
->v
);
893 return isl_aff_free(aff
);
895 isl_int_add(aff
->v
->el
[1], aff
->v
->el
[1], v
);
900 /* Add "v" to the numerator of the constant term of "aff".
902 * A NaN is unaffected by this operation.
904 __isl_give isl_aff
*isl_aff_add_constant_num_si(__isl_take isl_aff
*aff
, int v
)
912 isl_int_set_si(t
, v
);
913 aff
= isl_aff_add_constant_num(aff
, t
);
919 /* Replace the numerator of the constant term of "aff" by "v".
921 * A NaN is unaffected by this operation.
923 __isl_give isl_aff
*isl_aff_set_constant_si(__isl_take isl_aff
*aff
, int v
)
927 if (isl_aff_is_nan(aff
))
929 aff
= isl_aff_cow(aff
);
933 aff
->v
= isl_vec_cow(aff
->v
);
935 return isl_aff_free(aff
);
937 isl_int_set_si(aff
->v
->el
[1], v
);
942 /* Replace the numerator of the coefficient of the variable of type "type"
943 * at position "pos" of "aff" by "v".
945 * A NaN is unaffected by this operation.
947 __isl_give isl_aff
*isl_aff_set_coefficient(__isl_take isl_aff
*aff
,
948 enum isl_dim_type type
, int pos
, isl_int v
)
953 if (type
== isl_dim_out
)
954 isl_die(aff
->v
->ctx
, isl_error_invalid
,
955 "output/set dimension does not have a coefficient",
956 return isl_aff_free(aff
));
957 if (type
== isl_dim_in
)
960 if (pos
>= isl_local_space_dim(aff
->ls
, type
))
961 isl_die(aff
->v
->ctx
, isl_error_invalid
,
962 "position out of bounds", return isl_aff_free(aff
));
964 if (isl_aff_is_nan(aff
))
966 aff
= isl_aff_cow(aff
);
970 aff
->v
= isl_vec_cow(aff
->v
);
972 return isl_aff_free(aff
);
974 pos
+= isl_local_space_offset(aff
->ls
, type
);
975 isl_int_set(aff
->v
->el
[1 + pos
], v
);
980 /* Replace the numerator of the coefficient of the variable of type "type"
981 * at position "pos" of "aff" by "v".
983 * A NaN is unaffected by this operation.
985 __isl_give isl_aff
*isl_aff_set_coefficient_si(__isl_take isl_aff
*aff
,
986 enum isl_dim_type type
, int pos
, int v
)
991 if (type
== isl_dim_out
)
992 isl_die(aff
->v
->ctx
, isl_error_invalid
,
993 "output/set dimension does not have a coefficient",
994 return isl_aff_free(aff
));
995 if (type
== isl_dim_in
)
998 if (pos
< 0 || pos
>= isl_local_space_dim(aff
->ls
, type
))
999 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1000 "position out of bounds", return isl_aff_free(aff
));
1002 if (isl_aff_is_nan(aff
))
1004 pos
+= isl_local_space_offset(aff
->ls
, type
);
1005 if (isl_int_cmp_si(aff
->v
->el
[1 + pos
], v
) == 0)
1008 aff
= isl_aff_cow(aff
);
1012 aff
->v
= isl_vec_cow(aff
->v
);
1014 return isl_aff_free(aff
);
1016 isl_int_set_si(aff
->v
->el
[1 + pos
], v
);
1021 /* Replace the coefficient of the variable of type "type" at position "pos"
1024 * A NaN is unaffected by this operation.
1026 __isl_give isl_aff
*isl_aff_set_coefficient_val(__isl_take isl_aff
*aff
,
1027 enum isl_dim_type type
, int pos
, __isl_take isl_val
*v
)
1032 if (type
== isl_dim_out
)
1033 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1034 "output/set dimension does not have a coefficient",
1036 if (type
== isl_dim_in
)
1039 if (pos
>= isl_local_space_dim(aff
->ls
, type
))
1040 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1041 "position out of bounds", goto error
);
1043 if (isl_aff_is_nan(aff
)) {
1047 if (!isl_val_is_rat(v
))
1048 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1049 "expecting rational value", goto error
);
1051 pos
+= isl_local_space_offset(aff
->ls
, type
);
1052 if (isl_int_eq(aff
->v
->el
[1 + pos
], v
->n
) &&
1053 isl_int_eq(aff
->v
->el
[0], v
->d
)) {
1058 aff
= isl_aff_cow(aff
);
1061 aff
->v
= isl_vec_cow(aff
->v
);
1065 if (isl_int_eq(aff
->v
->el
[0], v
->d
)) {
1066 isl_int_set(aff
->v
->el
[1 + pos
], v
->n
);
1067 } else if (isl_int_is_one(v
->d
)) {
1068 isl_int_mul(aff
->v
->el
[1 + pos
], aff
->v
->el
[0], v
->n
);
1070 isl_seq_scale(aff
->v
->el
+ 1,
1071 aff
->v
->el
+ 1, v
->d
, aff
->v
->size
- 1);
1072 isl_int_mul(aff
->v
->el
[1 + pos
], aff
->v
->el
[0], v
->n
);
1073 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], v
->d
);
1074 aff
->v
= isl_vec_normalize(aff
->v
);
1087 /* Add "v" to the coefficient of the variable of type "type"
1088 * at position "pos" of "aff".
1090 * A NaN is unaffected by this operation.
1092 __isl_give isl_aff
*isl_aff_add_coefficient(__isl_take isl_aff
*aff
,
1093 enum isl_dim_type type
, int pos
, isl_int v
)
1098 if (type
== isl_dim_out
)
1099 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1100 "output/set dimension does not have a coefficient",
1101 return isl_aff_free(aff
));
1102 if (type
== isl_dim_in
)
1105 if (pos
>= isl_local_space_dim(aff
->ls
, type
))
1106 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1107 "position out of bounds", return isl_aff_free(aff
));
1109 if (isl_aff_is_nan(aff
))
1111 aff
= isl_aff_cow(aff
);
1115 aff
->v
= isl_vec_cow(aff
->v
);
1117 return isl_aff_free(aff
);
1119 pos
+= isl_local_space_offset(aff
->ls
, type
);
1120 isl_int_addmul(aff
->v
->el
[1 + pos
], aff
->v
->el
[0], v
);
1125 /* Add "v" to the coefficient of the variable of type "type"
1126 * at position "pos" of "aff".
1128 * A NaN is unaffected by this operation.
1130 __isl_give isl_aff
*isl_aff_add_coefficient_val(__isl_take isl_aff
*aff
,
1131 enum isl_dim_type type
, int pos
, __isl_take isl_val
*v
)
1136 if (isl_val_is_zero(v
)) {
1141 if (type
== isl_dim_out
)
1142 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1143 "output/set dimension does not have a coefficient",
1145 if (type
== isl_dim_in
)
1148 if (pos
>= isl_local_space_dim(aff
->ls
, type
))
1149 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1150 "position out of bounds", goto error
);
1152 if (isl_aff_is_nan(aff
)) {
1156 if (!isl_val_is_rat(v
))
1157 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1158 "expecting rational value", goto error
);
1160 aff
= isl_aff_cow(aff
);
1164 aff
->v
= isl_vec_cow(aff
->v
);
1168 pos
+= isl_local_space_offset(aff
->ls
, type
);
1169 if (isl_int_is_one(v
->d
)) {
1170 isl_int_addmul(aff
->v
->el
[1 + pos
], aff
->v
->el
[0], v
->n
);
1171 } else if (isl_int_eq(aff
->v
->el
[0], v
->d
)) {
1172 isl_int_add(aff
->v
->el
[1 + pos
], aff
->v
->el
[1 + pos
], v
->n
);
1173 aff
->v
= isl_vec_normalize(aff
->v
);
1177 isl_seq_scale(aff
->v
->el
+ 1,
1178 aff
->v
->el
+ 1, v
->d
, aff
->v
->size
- 1);
1179 isl_int_addmul(aff
->v
->el
[1 + pos
], aff
->v
->el
[0], v
->n
);
1180 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], v
->d
);
1181 aff
->v
= isl_vec_normalize(aff
->v
);
1194 __isl_give isl_aff
*isl_aff_add_coefficient_si(__isl_take isl_aff
*aff
,
1195 enum isl_dim_type type
, int pos
, int v
)
1200 isl_int_set_si(t
, v
);
1201 aff
= isl_aff_add_coefficient(aff
, type
, pos
, t
);
1207 __isl_give isl_aff
*isl_aff_get_div(__isl_keep isl_aff
*aff
, int pos
)
1212 return isl_local_space_get_div(aff
->ls
, pos
);
1215 /* Return the negation of "aff".
1217 * As a special case, -NaN = NaN.
1219 __isl_give isl_aff
*isl_aff_neg(__isl_take isl_aff
*aff
)
1223 if (isl_aff_is_nan(aff
))
1225 aff
= isl_aff_cow(aff
);
1228 aff
->v
= isl_vec_cow(aff
->v
);
1230 return isl_aff_free(aff
);
1232 isl_seq_neg(aff
->v
->el
+ 1, aff
->v
->el
+ 1, aff
->v
->size
- 1);
1237 /* Remove divs from the local space that do not appear in the affine
1239 * We currently only remove divs at the end.
1240 * Some intermediate divs may also not appear directly in the affine
1241 * expression, but we would also need to check that no other divs are
1242 * defined in terms of them.
1244 __isl_give isl_aff
*isl_aff_remove_unused_divs( __isl_take isl_aff
*aff
)
1253 n
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
1254 off
= isl_local_space_offset(aff
->ls
, isl_dim_div
);
1256 pos
= isl_seq_last_non_zero(aff
->v
->el
+ 1 + off
, n
) + 1;
1260 aff
= isl_aff_cow(aff
);
1264 aff
->ls
= isl_local_space_drop_dims(aff
->ls
, isl_dim_div
, pos
, n
- pos
);
1265 aff
->v
= isl_vec_drop_els(aff
->v
, 1 + off
+ pos
, n
- pos
);
1266 if (!aff
->ls
|| !aff
->v
)
1267 return isl_aff_free(aff
);
1272 /* Given two affine expressions "p" of length p_len (including the
1273 * denominator and the constant term) and "subs" of length subs_len,
1274 * plug in "subs" for the variable at position "pos".
1275 * The variables of "subs" and "p" are assumed to match up to subs_len,
1276 * but "p" may have additional variables.
1277 * "v" is an initialized isl_int that can be used internally.
1279 * In particular, if "p" represents the expression
1283 * with i the variable at position "pos" and "subs" represents the expression
1287 * then the result represents the expression
1292 void isl_seq_substitute(isl_int
*p
, int pos
, isl_int
*subs
,
1293 int p_len
, int subs_len
, isl_int v
)
1295 isl_int_set(v
, p
[1 + pos
]);
1296 isl_int_set_si(p
[1 + pos
], 0);
1297 isl_seq_combine(p
+ 1, subs
[0], p
+ 1, v
, subs
+ 1, subs_len
- 1);
1298 isl_seq_scale(p
+ subs_len
, p
+ subs_len
, subs
[0], p_len
- subs_len
);
1299 isl_int_mul(p
[0], p
[0], subs
[0]);
1302 /* Look for any divs in the aff->ls with a denominator equal to one
1303 * and plug them into the affine expression and any subsequent divs
1304 * that may reference the div.
1306 static __isl_give isl_aff
*plug_in_integral_divs(__isl_take isl_aff
*aff
)
1312 isl_local_space
*ls
;
1318 n
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
1320 for (i
= 0; i
< n
; ++i
) {
1321 if (!isl_int_is_one(aff
->ls
->div
->row
[i
][0]))
1323 ls
= isl_local_space_copy(aff
->ls
);
1324 ls
= isl_local_space_substitute_seq(ls
, isl_dim_div
, i
,
1325 aff
->ls
->div
->row
[i
], len
, i
+ 1, n
- (i
+ 1));
1326 vec
= isl_vec_copy(aff
->v
);
1327 vec
= isl_vec_cow(vec
);
1333 pos
= isl_local_space_offset(aff
->ls
, isl_dim_div
) + i
;
1334 isl_seq_substitute(vec
->el
, pos
, aff
->ls
->div
->row
[i
],
1339 isl_vec_free(aff
->v
);
1341 isl_local_space_free(aff
->ls
);
1348 isl_local_space_free(ls
);
1349 return isl_aff_free(aff
);
1352 /* Look for any divs j that appear with a unit coefficient inside
1353 * the definitions of other divs i and plug them into the definitions
1356 * In particular, an expression of the form
1358 * floor((f(..) + floor(g(..)/n))/m)
1362 * floor((n * f(..) + g(..))/(n * m))
1364 * This simplification is correct because we can move the expression
1365 * f(..) into the inner floor in the original expression to obtain
1367 * floor(floor((n * f(..) + g(..))/n)/m)
1369 * from which we can derive the simplified expression.
1371 static __isl_give isl_aff
*plug_in_unit_divs(__isl_take isl_aff
*aff
)
1379 n
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
1380 off
= isl_local_space_offset(aff
->ls
, isl_dim_div
);
1381 for (i
= 1; i
< n
; ++i
) {
1382 for (j
= 0; j
< i
; ++j
) {
1383 if (!isl_int_is_one(aff
->ls
->div
->row
[i
][1 + off
+ j
]))
1385 aff
->ls
= isl_local_space_substitute_seq(aff
->ls
,
1386 isl_dim_div
, j
, aff
->ls
->div
->row
[j
],
1387 aff
->v
->size
, i
, 1);
1389 return isl_aff_free(aff
);
1396 /* Swap divs "a" and "b" in "aff", which is assumed to be non-NULL.
1398 * Even though this function is only called on isl_affs with a single
1399 * reference, we are careful to only change aff->v and aff->ls together.
1401 static __isl_give isl_aff
*swap_div(__isl_take isl_aff
*aff
, int a
, int b
)
1403 unsigned off
= isl_local_space_offset(aff
->ls
, isl_dim_div
);
1404 isl_local_space
*ls
;
1407 ls
= isl_local_space_copy(aff
->ls
);
1408 ls
= isl_local_space_swap_div(ls
, a
, b
);
1409 v
= isl_vec_copy(aff
->v
);
1414 isl_int_swap(v
->el
[1 + off
+ a
], v
->el
[1 + off
+ b
]);
1415 isl_vec_free(aff
->v
);
1417 isl_local_space_free(aff
->ls
);
1423 isl_local_space_free(ls
);
1424 return isl_aff_free(aff
);
1427 /* Merge divs "a" and "b" in "aff", which is assumed to be non-NULL.
1429 * We currently do not actually remove div "b", but simply add its
1430 * coefficient to that of "a" and then zero it out.
1432 static __isl_give isl_aff
*merge_divs(__isl_take isl_aff
*aff
, int a
, int b
)
1434 unsigned off
= isl_local_space_offset(aff
->ls
, isl_dim_div
);
1436 if (isl_int_is_zero(aff
->v
->el
[1 + off
+ b
]))
1439 aff
->v
= isl_vec_cow(aff
->v
);
1441 return isl_aff_free(aff
);
1443 isl_int_add(aff
->v
->el
[1 + off
+ a
],
1444 aff
->v
->el
[1 + off
+ a
], aff
->v
->el
[1 + off
+ b
]);
1445 isl_int_set_si(aff
->v
->el
[1 + off
+ b
], 0);
1450 /* Sort the divs in the local space of "aff" according to
1451 * the comparison function "cmp_row" in isl_local_space.c,
1452 * combining the coefficients of identical divs.
1454 * Reordering divs does not change the semantics of "aff",
1455 * so there is no need to call isl_aff_cow.
1456 * Moreover, this function is currently only called on isl_affs
1457 * with a single reference.
1459 static __isl_give isl_aff
*sort_divs(__isl_take isl_aff
*aff
)
1466 n
= isl_aff_dim(aff
, isl_dim_div
);
1467 for (i
= 1; i
< n
; ++i
) {
1468 for (j
= i
- 1; j
>= 0; --j
) {
1469 int cmp
= isl_mat_cmp_div(aff
->ls
->div
, j
, j
+ 1);
1473 aff
= merge_divs(aff
, j
, j
+ 1);
1475 aff
= swap_div(aff
, j
, j
+ 1);
1484 /* Normalize the representation of "aff".
1486 * This function should only be called of "new" isl_affs, i.e.,
1487 * with only a single reference. We therefore do not need to
1488 * worry about affecting other instances.
1490 __isl_give isl_aff
*isl_aff_normalize(__isl_take isl_aff
*aff
)
1494 aff
->v
= isl_vec_normalize(aff
->v
);
1496 return isl_aff_free(aff
);
1497 aff
= plug_in_integral_divs(aff
);
1498 aff
= plug_in_unit_divs(aff
);
1499 aff
= sort_divs(aff
);
1500 aff
= isl_aff_remove_unused_divs(aff
);
1504 /* Given f, return floor(f).
1505 * If f is an integer expression, then just return f.
1506 * If f is a constant, then return the constant floor(f).
1507 * Otherwise, if f = g/m, write g = q m + r,
1508 * create a new div d = [r/m] and return the expression q + d.
1509 * The coefficients in r are taken to lie between -m/2 and m/2.
1511 * As a special case, floor(NaN) = NaN.
1513 __isl_give isl_aff
*isl_aff_floor(__isl_take isl_aff
*aff
)
1523 if (isl_aff_is_nan(aff
))
1525 if (isl_int_is_one(aff
->v
->el
[0]))
1528 aff
= isl_aff_cow(aff
);
1532 aff
->v
= isl_vec_cow(aff
->v
);
1534 return isl_aff_free(aff
);
1536 if (isl_aff_is_cst(aff
)) {
1537 isl_int_fdiv_q(aff
->v
->el
[1], aff
->v
->el
[1], aff
->v
->el
[0]);
1538 isl_int_set_si(aff
->v
->el
[0], 1);
1542 div
= isl_vec_copy(aff
->v
);
1543 div
= isl_vec_cow(div
);
1545 return isl_aff_free(aff
);
1547 ctx
= isl_aff_get_ctx(aff
);
1548 isl_int_fdiv_q(aff
->v
->el
[0], aff
->v
->el
[0], ctx
->two
);
1549 for (i
= 1; i
< aff
->v
->size
; ++i
) {
1550 isl_int_fdiv_r(div
->el
[i
], div
->el
[i
], div
->el
[0]);
1551 isl_int_fdiv_q(aff
->v
->el
[i
], aff
->v
->el
[i
], div
->el
[0]);
1552 if (isl_int_gt(div
->el
[i
], aff
->v
->el
[0])) {
1553 isl_int_sub(div
->el
[i
], div
->el
[i
], div
->el
[0]);
1554 isl_int_add_ui(aff
->v
->el
[i
], aff
->v
->el
[i
], 1);
1558 aff
->ls
= isl_local_space_add_div(aff
->ls
, div
);
1560 return isl_aff_free(aff
);
1562 size
= aff
->v
->size
;
1563 aff
->v
= isl_vec_extend(aff
->v
, size
+ 1);
1565 return isl_aff_free(aff
);
1566 isl_int_set_si(aff
->v
->el
[0], 1);
1567 isl_int_set_si(aff
->v
->el
[size
], 1);
1569 aff
= isl_aff_normalize(aff
);
1576 * aff mod m = aff - m * floor(aff/m)
1578 __isl_give isl_aff
*isl_aff_mod(__isl_take isl_aff
*aff
, isl_int m
)
1582 res
= isl_aff_copy(aff
);
1583 aff
= isl_aff_scale_down(aff
, m
);
1584 aff
= isl_aff_floor(aff
);
1585 aff
= isl_aff_scale(aff
, m
);
1586 res
= isl_aff_sub(res
, aff
);
1593 * aff mod m = aff - m * floor(aff/m)
1595 * with m an integer value.
1597 __isl_give isl_aff
*isl_aff_mod_val(__isl_take isl_aff
*aff
,
1598 __isl_take isl_val
*m
)
1605 if (!isl_val_is_int(m
))
1606 isl_die(isl_val_get_ctx(m
), isl_error_invalid
,
1607 "expecting integer modulo", goto error
);
1609 res
= isl_aff_copy(aff
);
1610 aff
= isl_aff_scale_down_val(aff
, isl_val_copy(m
));
1611 aff
= isl_aff_floor(aff
);
1612 aff
= isl_aff_scale_val(aff
, m
);
1613 res
= isl_aff_sub(res
, aff
);
1624 * pwaff mod m = pwaff - m * floor(pwaff/m)
1626 __isl_give isl_pw_aff
*isl_pw_aff_mod(__isl_take isl_pw_aff
*pwaff
, isl_int m
)
1630 res
= isl_pw_aff_copy(pwaff
);
1631 pwaff
= isl_pw_aff_scale_down(pwaff
, m
);
1632 pwaff
= isl_pw_aff_floor(pwaff
);
1633 pwaff
= isl_pw_aff_scale(pwaff
, m
);
1634 res
= isl_pw_aff_sub(res
, pwaff
);
1641 * pa mod m = pa - m * floor(pa/m)
1643 * with m an integer value.
1645 __isl_give isl_pw_aff
*isl_pw_aff_mod_val(__isl_take isl_pw_aff
*pa
,
1646 __isl_take isl_val
*m
)
1650 if (!isl_val_is_int(m
))
1651 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
1652 "expecting integer modulo", goto error
);
1653 pa
= isl_pw_aff_mod(pa
, m
->n
);
1657 isl_pw_aff_free(pa
);
1662 /* Given f, return ceil(f).
1663 * If f is an integer expression, then just return f.
1664 * Otherwise, let f be the expression
1670 * floor((e + m - 1)/m)
1672 * As a special case, ceil(NaN) = NaN.
1674 __isl_give isl_aff
*isl_aff_ceil(__isl_take isl_aff
*aff
)
1679 if (isl_aff_is_nan(aff
))
1681 if (isl_int_is_one(aff
->v
->el
[0]))
1684 aff
= isl_aff_cow(aff
);
1687 aff
->v
= isl_vec_cow(aff
->v
);
1689 return isl_aff_free(aff
);
1691 isl_int_add(aff
->v
->el
[1], aff
->v
->el
[1], aff
->v
->el
[0]);
1692 isl_int_sub_ui(aff
->v
->el
[1], aff
->v
->el
[1], 1);
1693 aff
= isl_aff_floor(aff
);
1698 /* Apply the expansion computed by isl_merge_divs.
1699 * The expansion itself is given by "exp" while the resulting
1700 * list of divs is given by "div".
1702 __isl_give isl_aff
*isl_aff_expand_divs( __isl_take isl_aff
*aff
,
1703 __isl_take isl_mat
*div
, int *exp
)
1710 aff
= isl_aff_cow(aff
);
1714 old_n_div
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
1715 new_n_div
= isl_mat_rows(div
);
1716 if (new_n_div
< old_n_div
)
1717 isl_die(isl_mat_get_ctx(div
), isl_error_invalid
,
1718 "not an expansion", goto error
);
1720 aff
->v
= isl_vec_extend(aff
->v
, aff
->v
->size
+ new_n_div
- old_n_div
);
1724 offset
= 1 + isl_local_space_offset(aff
->ls
, isl_dim_div
);
1726 for (i
= new_n_div
- 1; i
>= 0; --i
) {
1727 if (j
>= 0 && exp
[j
] == i
) {
1729 isl_int_swap(aff
->v
->el
[offset
+ i
],
1730 aff
->v
->el
[offset
+ j
]);
1733 isl_int_set_si(aff
->v
->el
[offset
+ i
], 0);
1736 aff
->ls
= isl_local_space_replace_divs(aff
->ls
, isl_mat_copy(div
));
1747 /* Add two affine expressions that live in the same local space.
1749 static __isl_give isl_aff
*add_expanded(__isl_take isl_aff
*aff1
,
1750 __isl_take isl_aff
*aff2
)
1754 aff1
= isl_aff_cow(aff1
);
1758 aff1
->v
= isl_vec_cow(aff1
->v
);
1764 isl_int_gcd(gcd
, aff1
->v
->el
[0], aff2
->v
->el
[0]);
1765 isl_int_divexact(f
, aff2
->v
->el
[0], gcd
);
1766 isl_seq_scale(aff1
->v
->el
+ 1, aff1
->v
->el
+ 1, f
, aff1
->v
->size
- 1);
1767 isl_int_divexact(f
, aff1
->v
->el
[0], gcd
);
1768 isl_seq_addmul(aff1
->v
->el
+ 1, f
, aff2
->v
->el
+ 1, aff1
->v
->size
- 1);
1769 isl_int_divexact(f
, aff2
->v
->el
[0], gcd
);
1770 isl_int_mul(aff1
->v
->el
[0], aff1
->v
->el
[0], f
);
1782 /* Return the sum of "aff1" and "aff2".
1784 * If either of the two is NaN, then the result is NaN.
1786 __isl_give isl_aff
*isl_aff_add(__isl_take isl_aff
*aff1
,
1787 __isl_take isl_aff
*aff2
)
1798 ctx
= isl_aff_get_ctx(aff1
);
1799 if (!isl_space_is_equal(aff1
->ls
->dim
, aff2
->ls
->dim
))
1800 isl_die(ctx
, isl_error_invalid
,
1801 "spaces don't match", goto error
);
1803 if (isl_aff_is_nan(aff1
)) {
1807 if (isl_aff_is_nan(aff2
)) {
1812 n_div1
= isl_aff_dim(aff1
, isl_dim_div
);
1813 n_div2
= isl_aff_dim(aff2
, isl_dim_div
);
1814 if (n_div1
== 0 && n_div2
== 0)
1815 return add_expanded(aff1
, aff2
);
1817 exp1
= isl_alloc_array(ctx
, int, n_div1
);
1818 exp2
= isl_alloc_array(ctx
, int, n_div2
);
1819 if ((n_div1
&& !exp1
) || (n_div2
&& !exp2
))
1822 div
= isl_merge_divs(aff1
->ls
->div
, aff2
->ls
->div
, exp1
, exp2
);
1823 aff1
= isl_aff_expand_divs(aff1
, isl_mat_copy(div
), exp1
);
1824 aff2
= isl_aff_expand_divs(aff2
, div
, exp2
);
1828 return add_expanded(aff1
, aff2
);
1837 __isl_give isl_aff
*isl_aff_sub(__isl_take isl_aff
*aff1
,
1838 __isl_take isl_aff
*aff2
)
1840 return isl_aff_add(aff1
, isl_aff_neg(aff2
));
1843 /* Return the result of scaling "aff" by a factor of "f".
1845 * As a special case, f * NaN = NaN.
1847 __isl_give isl_aff
*isl_aff_scale(__isl_take isl_aff
*aff
, isl_int f
)
1853 if (isl_aff_is_nan(aff
))
1856 if (isl_int_is_one(f
))
1859 aff
= isl_aff_cow(aff
);
1862 aff
->v
= isl_vec_cow(aff
->v
);
1864 return isl_aff_free(aff
);
1866 if (isl_int_is_pos(f
) && isl_int_is_divisible_by(aff
->v
->el
[0], f
)) {
1867 isl_int_divexact(aff
->v
->el
[0], aff
->v
->el
[0], f
);
1872 isl_int_gcd(gcd
, aff
->v
->el
[0], f
);
1873 isl_int_divexact(aff
->v
->el
[0], aff
->v
->el
[0], gcd
);
1874 isl_int_divexact(gcd
, f
, gcd
);
1875 isl_seq_scale(aff
->v
->el
+ 1, aff
->v
->el
+ 1, gcd
, aff
->v
->size
- 1);
1881 /* Multiple "aff" by "v".
1883 __isl_give isl_aff
*isl_aff_scale_val(__isl_take isl_aff
*aff
,
1884 __isl_take isl_val
*v
)
1889 if (isl_val_is_one(v
)) {
1894 if (!isl_val_is_rat(v
))
1895 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1896 "expecting rational factor", goto error
);
1898 aff
= isl_aff_scale(aff
, v
->n
);
1899 aff
= isl_aff_scale_down(aff
, v
->d
);
1909 /* Return the result of scaling "aff" down by a factor of "f".
1911 * As a special case, NaN/f = NaN.
1913 __isl_give isl_aff
*isl_aff_scale_down(__isl_take isl_aff
*aff
, isl_int f
)
1919 if (isl_aff_is_nan(aff
))
1922 if (isl_int_is_one(f
))
1925 aff
= isl_aff_cow(aff
);
1929 if (isl_int_is_zero(f
))
1930 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1931 "cannot scale down by zero", return isl_aff_free(aff
));
1933 aff
->v
= isl_vec_cow(aff
->v
);
1935 return isl_aff_free(aff
);
1938 isl_seq_gcd(aff
->v
->el
+ 1, aff
->v
->size
- 1, &gcd
);
1939 isl_int_gcd(gcd
, gcd
, f
);
1940 isl_seq_scale_down(aff
->v
->el
+ 1, aff
->v
->el
+ 1, gcd
, aff
->v
->size
- 1);
1941 isl_int_divexact(gcd
, f
, gcd
);
1942 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], gcd
);
1948 /* Divide "aff" by "v".
1950 __isl_give isl_aff
*isl_aff_scale_down_val(__isl_take isl_aff
*aff
,
1951 __isl_take isl_val
*v
)
1956 if (isl_val_is_one(v
)) {
1961 if (!isl_val_is_rat(v
))
1962 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1963 "expecting rational factor", goto error
);
1964 if (!isl_val_is_pos(v
))
1965 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1966 "factor needs to be positive", goto error
);
1968 aff
= isl_aff_scale(aff
, v
->d
);
1969 aff
= isl_aff_scale_down(aff
, v
->n
);
1979 __isl_give isl_aff
*isl_aff_scale_down_ui(__isl_take isl_aff
*aff
, unsigned f
)
1987 isl_int_set_ui(v
, f
);
1988 aff
= isl_aff_scale_down(aff
, v
);
1994 __isl_give isl_aff
*isl_aff_set_dim_name(__isl_take isl_aff
*aff
,
1995 enum isl_dim_type type
, unsigned pos
, const char *s
)
1997 aff
= isl_aff_cow(aff
);
2000 if (type
== isl_dim_out
)
2001 isl_die(aff
->v
->ctx
, isl_error_invalid
,
2002 "cannot set name of output/set dimension",
2003 return isl_aff_free(aff
));
2004 if (type
== isl_dim_in
)
2006 aff
->ls
= isl_local_space_set_dim_name(aff
->ls
, type
, pos
, s
);
2008 return isl_aff_free(aff
);
2013 __isl_give isl_aff
*isl_aff_set_dim_id(__isl_take isl_aff
*aff
,
2014 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
2016 aff
= isl_aff_cow(aff
);
2019 if (type
== isl_dim_out
)
2020 isl_die(aff
->v
->ctx
, isl_error_invalid
,
2021 "cannot set name of output/set dimension",
2023 if (type
== isl_dim_in
)
2025 aff
->ls
= isl_local_space_set_dim_id(aff
->ls
, type
, pos
, id
);
2027 return isl_aff_free(aff
);
2036 /* Replace the identifier of the input tuple of "aff" by "id".
2037 * type is currently required to be equal to isl_dim_in
2039 __isl_give isl_aff
*isl_aff_set_tuple_id(__isl_take isl_aff
*aff
,
2040 enum isl_dim_type type
, __isl_take isl_id
*id
)
2042 aff
= isl_aff_cow(aff
);
2045 if (type
!= isl_dim_out
)
2046 isl_die(aff
->v
->ctx
, isl_error_invalid
,
2047 "cannot only set id of input tuple", goto error
);
2048 aff
->ls
= isl_local_space_set_tuple_id(aff
->ls
, isl_dim_set
, id
);
2050 return isl_aff_free(aff
);
2059 /* Exploit the equalities in "eq" to simplify the affine expression
2060 * and the expressions of the integer divisions in the local space.
2061 * The integer divisions in this local space are assumed to appear
2062 * as regular dimensions in "eq".
2064 static __isl_give isl_aff
*isl_aff_substitute_equalities_lifted(
2065 __isl_take isl_aff
*aff
, __isl_take isl_basic_set
*eq
)
2073 if (eq
->n_eq
== 0) {
2074 isl_basic_set_free(eq
);
2078 aff
= isl_aff_cow(aff
);
2082 aff
->ls
= isl_local_space_substitute_equalities(aff
->ls
,
2083 isl_basic_set_copy(eq
));
2084 aff
->v
= isl_vec_cow(aff
->v
);
2085 if (!aff
->ls
|| !aff
->v
)
2088 total
= 1 + isl_space_dim(eq
->dim
, isl_dim_all
);
2090 for (i
= 0; i
< eq
->n_eq
; ++i
) {
2091 j
= isl_seq_last_non_zero(eq
->eq
[i
], total
+ n_div
);
2092 if (j
< 0 || j
== 0 || j
>= total
)
2095 isl_seq_elim(aff
->v
->el
+ 1, eq
->eq
[i
], j
, total
,
2099 isl_basic_set_free(eq
);
2100 aff
= isl_aff_normalize(aff
);
2103 isl_basic_set_free(eq
);
2108 /* Exploit the equalities in "eq" to simplify the affine expression
2109 * and the expressions of the integer divisions in the local space.
2111 __isl_give isl_aff
*isl_aff_substitute_equalities(__isl_take isl_aff
*aff
,
2112 __isl_take isl_basic_set
*eq
)
2118 n_div
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
2120 eq
= isl_basic_set_add_dims(eq
, isl_dim_set
, n_div
);
2121 return isl_aff_substitute_equalities_lifted(aff
, eq
);
2123 isl_basic_set_free(eq
);
2128 /* Look for equalities among the variables shared by context and aff
2129 * and the integer divisions of aff, if any.
2130 * The equalities are then used to eliminate coefficients and/or integer
2131 * divisions from aff.
2133 __isl_give isl_aff
*isl_aff_gist(__isl_take isl_aff
*aff
,
2134 __isl_take isl_set
*context
)
2136 isl_basic_set
*hull
;
2141 n_div
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
2143 isl_basic_set
*bset
;
2144 isl_local_space
*ls
;
2145 context
= isl_set_add_dims(context
, isl_dim_set
, n_div
);
2146 ls
= isl_aff_get_domain_local_space(aff
);
2147 bset
= isl_basic_set_from_local_space(ls
);
2148 bset
= isl_basic_set_lift(bset
);
2149 bset
= isl_basic_set_flatten(bset
);
2150 context
= isl_set_intersect(context
,
2151 isl_set_from_basic_set(bset
));
2154 hull
= isl_set_affine_hull(context
);
2155 return isl_aff_substitute_equalities_lifted(aff
, hull
);
2158 isl_set_free(context
);
2162 __isl_give isl_aff
*isl_aff_gist_params(__isl_take isl_aff
*aff
,
2163 __isl_take isl_set
*context
)
2165 isl_set
*dom_context
= isl_set_universe(isl_aff_get_domain_space(aff
));
2166 dom_context
= isl_set_intersect_params(dom_context
, context
);
2167 return isl_aff_gist(aff
, dom_context
);
2170 /* Return a basic set containing those elements in the space
2171 * of aff where it is positive. "rational" should not be set.
2173 * If "aff" is NaN, then it is not positive.
2175 static __isl_give isl_basic_set
*aff_pos_basic_set(__isl_take isl_aff
*aff
,
2178 isl_constraint
*ineq
;
2179 isl_basic_set
*bset
;
2184 if (isl_aff_is_nan(aff
)) {
2185 isl_space
*space
= isl_aff_get_domain_space(aff
);
2187 return isl_basic_set_empty(space
);
2190 isl_die(isl_aff_get_ctx(aff
), isl_error_unsupported
,
2191 "rational sets not supported", goto error
);
2193 ineq
= isl_inequality_from_aff(aff
);
2194 c
= isl_constraint_get_constant_val(ineq
);
2195 c
= isl_val_sub_ui(c
, 1);
2196 ineq
= isl_constraint_set_constant_val(ineq
, c
);
2198 bset
= isl_basic_set_from_constraint(ineq
);
2199 bset
= isl_basic_set_simplify(bset
);
2206 /* Return a basic set containing those elements in the space
2207 * of aff where it is non-negative.
2208 * If "rational" is set, then return a rational basic set.
2210 * If "aff" is NaN, then it is not non-negative (it's not negative either).
2212 static __isl_give isl_basic_set
*aff_nonneg_basic_set(
2213 __isl_take isl_aff
*aff
, int rational
)
2215 isl_constraint
*ineq
;
2216 isl_basic_set
*bset
;
2220 if (isl_aff_is_nan(aff
)) {
2221 isl_space
*space
= isl_aff_get_domain_space(aff
);
2223 return isl_basic_set_empty(space
);
2226 ineq
= isl_inequality_from_aff(aff
);
2228 bset
= isl_basic_set_from_constraint(ineq
);
2230 bset
= isl_basic_set_set_rational(bset
);
2231 bset
= isl_basic_set_simplify(bset
);
2235 /* Return a basic set containing those elements in the space
2236 * of aff where it is non-negative.
2238 __isl_give isl_basic_set
*isl_aff_nonneg_basic_set(__isl_take isl_aff
*aff
)
2240 return aff_nonneg_basic_set(aff
, 0);
2243 /* Return a basic set containing those elements in the domain space
2244 * of aff where it is negative.
2246 __isl_give isl_basic_set
*isl_aff_neg_basic_set(__isl_take isl_aff
*aff
)
2248 aff
= isl_aff_neg(aff
);
2249 aff
= isl_aff_add_constant_num_si(aff
, -1);
2250 return isl_aff_nonneg_basic_set(aff
);
2253 /* Return a basic set containing those elements in the space
2254 * of aff where it is zero.
2255 * If "rational" is set, then return a rational basic set.
2257 * If "aff" is NaN, then it is not zero.
2259 static __isl_give isl_basic_set
*aff_zero_basic_set(__isl_take isl_aff
*aff
,
2262 isl_constraint
*ineq
;
2263 isl_basic_set
*bset
;
2267 if (isl_aff_is_nan(aff
)) {
2268 isl_space
*space
= isl_aff_get_domain_space(aff
);
2270 return isl_basic_set_empty(space
);
2273 ineq
= isl_equality_from_aff(aff
);
2275 bset
= isl_basic_set_from_constraint(ineq
);
2277 bset
= isl_basic_set_set_rational(bset
);
2278 bset
= isl_basic_set_simplify(bset
);
2282 /* Return a basic set containing those elements in the space
2283 * of aff where it is zero.
2285 __isl_give isl_basic_set
*isl_aff_zero_basic_set(__isl_take isl_aff
*aff
)
2287 return aff_zero_basic_set(aff
, 0);
2290 /* Return a basic set containing those elements in the shared space
2291 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
2293 __isl_give isl_basic_set
*isl_aff_ge_basic_set(__isl_take isl_aff
*aff1
,
2294 __isl_take isl_aff
*aff2
)
2296 aff1
= isl_aff_sub(aff1
, aff2
);
2298 return isl_aff_nonneg_basic_set(aff1
);
2301 /* Return a basic set containing those elements in the shared space
2302 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
2304 __isl_give isl_basic_set
*isl_aff_le_basic_set(__isl_take isl_aff
*aff1
,
2305 __isl_take isl_aff
*aff2
)
2307 return isl_aff_ge_basic_set(aff2
, aff1
);
2310 __isl_give isl_aff
*isl_aff_add_on_domain(__isl_keep isl_set
*dom
,
2311 __isl_take isl_aff
*aff1
, __isl_take isl_aff
*aff2
)
2313 aff1
= isl_aff_add(aff1
, aff2
);
2314 aff1
= isl_aff_gist(aff1
, isl_set_copy(dom
));
2318 int isl_aff_is_empty(__isl_keep isl_aff
*aff
)
2326 /* Check whether the given affine expression has non-zero coefficient
2327 * for any dimension in the given range or if any of these dimensions
2328 * appear with non-zero coefficients in any of the integer divisions
2329 * involved in the affine expression.
2331 int isl_aff_involves_dims(__isl_keep isl_aff
*aff
,
2332 enum isl_dim_type type
, unsigned first
, unsigned n
)
2344 ctx
= isl_aff_get_ctx(aff
);
2345 if (first
+ n
> isl_aff_dim(aff
, type
))
2346 isl_die(ctx
, isl_error_invalid
,
2347 "range out of bounds", return -1);
2349 active
= isl_local_space_get_active(aff
->ls
, aff
->v
->el
+ 2);
2353 first
+= isl_local_space_offset(aff
->ls
, type
) - 1;
2354 for (i
= 0; i
< n
; ++i
)
2355 if (active
[first
+ i
]) {
2368 __isl_give isl_aff
*isl_aff_drop_dims(__isl_take isl_aff
*aff
,
2369 enum isl_dim_type type
, unsigned first
, unsigned n
)
2375 if (type
== isl_dim_out
)
2376 isl_die(aff
->v
->ctx
, isl_error_invalid
,
2377 "cannot drop output/set dimension",
2378 return isl_aff_free(aff
));
2379 if (type
== isl_dim_in
)
2381 if (n
== 0 && !isl_local_space_is_named_or_nested(aff
->ls
, type
))
2384 ctx
= isl_aff_get_ctx(aff
);
2385 if (first
+ n
> isl_local_space_dim(aff
->ls
, type
))
2386 isl_die(ctx
, isl_error_invalid
, "range out of bounds",
2387 return isl_aff_free(aff
));
2389 aff
= isl_aff_cow(aff
);
2393 aff
->ls
= isl_local_space_drop_dims(aff
->ls
, type
, first
, n
);
2395 return isl_aff_free(aff
);
2397 first
+= 1 + isl_local_space_offset(aff
->ls
, type
);
2398 aff
->v
= isl_vec_drop_els(aff
->v
, first
, n
);
2400 return isl_aff_free(aff
);
2405 /* Project the domain of the affine expression onto its parameter space.
2406 * The affine expression may not involve any of the domain dimensions.
2408 __isl_give isl_aff
*isl_aff_project_domain_on_params(__isl_take isl_aff
*aff
)
2414 n
= isl_aff_dim(aff
, isl_dim_in
);
2415 involves
= isl_aff_involves_dims(aff
, isl_dim_in
, 0, n
);
2417 return isl_aff_free(aff
);
2419 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2420 "affine expression involves some of the domain dimensions",
2421 return isl_aff_free(aff
));
2422 aff
= isl_aff_drop_dims(aff
, isl_dim_in
, 0, n
);
2423 space
= isl_aff_get_domain_space(aff
);
2424 space
= isl_space_params(space
);
2425 aff
= isl_aff_reset_domain_space(aff
, space
);
2429 __isl_give isl_aff
*isl_aff_insert_dims(__isl_take isl_aff
*aff
,
2430 enum isl_dim_type type
, unsigned first
, unsigned n
)
2436 if (type
== isl_dim_out
)
2437 isl_die(aff
->v
->ctx
, isl_error_invalid
,
2438 "cannot insert output/set dimensions",
2439 return isl_aff_free(aff
));
2440 if (type
== isl_dim_in
)
2442 if (n
== 0 && !isl_local_space_is_named_or_nested(aff
->ls
, type
))
2445 ctx
= isl_aff_get_ctx(aff
);
2446 if (first
> isl_local_space_dim(aff
->ls
, type
))
2447 isl_die(ctx
, isl_error_invalid
, "position out of bounds",
2448 return isl_aff_free(aff
));
2450 aff
= isl_aff_cow(aff
);
2454 aff
->ls
= isl_local_space_insert_dims(aff
->ls
, type
, first
, n
);
2456 return isl_aff_free(aff
);
2458 first
+= 1 + isl_local_space_offset(aff
->ls
, type
);
2459 aff
->v
= isl_vec_insert_zero_els(aff
->v
, first
, n
);
2461 return isl_aff_free(aff
);
2466 __isl_give isl_aff
*isl_aff_add_dims(__isl_take isl_aff
*aff
,
2467 enum isl_dim_type type
, unsigned n
)
2471 pos
= isl_aff_dim(aff
, type
);
2473 return isl_aff_insert_dims(aff
, type
, pos
, n
);
2476 __isl_give isl_pw_aff
*isl_pw_aff_add_dims(__isl_take isl_pw_aff
*pwaff
,
2477 enum isl_dim_type type
, unsigned n
)
2481 pos
= isl_pw_aff_dim(pwaff
, type
);
2483 return isl_pw_aff_insert_dims(pwaff
, type
, pos
, n
);
2486 /* Move the "n" dimensions of "src_type" starting at "src_pos" of "aff"
2487 * to dimensions of "dst_type" at "dst_pos".
2489 * We only support moving input dimensions to parameters and vice versa.
2491 __isl_give isl_aff
*isl_aff_move_dims(__isl_take isl_aff
*aff
,
2492 enum isl_dim_type dst_type
, unsigned dst_pos
,
2493 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
2501 !isl_local_space_is_named_or_nested(aff
->ls
, src_type
) &&
2502 !isl_local_space_is_named_or_nested(aff
->ls
, dst_type
))
2505 if (dst_type
== isl_dim_out
|| src_type
== isl_dim_out
)
2506 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2507 "cannot move output/set dimension", isl_aff_free(aff
));
2508 if (dst_type
== isl_dim_div
|| src_type
== isl_dim_div
)
2509 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2510 "cannot move divs", isl_aff_free(aff
));
2511 if (dst_type
== isl_dim_in
)
2512 dst_type
= isl_dim_set
;
2513 if (src_type
== isl_dim_in
)
2514 src_type
= isl_dim_set
;
2516 if (src_pos
+ n
> isl_local_space_dim(aff
->ls
, src_type
))
2517 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2518 "range out of bounds", isl_aff_free(aff
));
2519 if (dst_type
== src_type
)
2520 isl_die(isl_aff_get_ctx(aff
), isl_error_unsupported
,
2521 "moving dims within the same type not supported",
2524 aff
= isl_aff_cow(aff
);
2528 g_src_pos
= 1 + isl_local_space_offset(aff
->ls
, src_type
) + src_pos
;
2529 g_dst_pos
= 1 + isl_local_space_offset(aff
->ls
, dst_type
) + dst_pos
;
2530 if (dst_type
> src_type
)
2533 aff
->v
= isl_vec_move_els(aff
->v
, g_dst_pos
, g_src_pos
, n
);
2534 aff
->ls
= isl_local_space_move_dims(aff
->ls
, dst_type
, dst_pos
,
2535 src_type
, src_pos
, n
);
2536 if (!aff
->v
|| !aff
->ls
)
2537 return isl_aff_free(aff
);
2539 aff
= sort_divs(aff
);
2544 __isl_give isl_pw_aff
*isl_pw_aff_from_aff(__isl_take isl_aff
*aff
)
2546 isl_set
*dom
= isl_set_universe(isl_aff_get_domain_space(aff
));
2547 return isl_pw_aff_alloc(dom
, aff
);
2551 #define PW isl_pw_aff
2555 #define EL_IS_ZERO is_empty
2559 #define IS_ZERO is_empty
2562 #undef DEFAULT_IS_ZERO
2563 #define DEFAULT_IS_ZERO 0
2570 #include <isl_pw_templ.c>
2573 #define UNION isl_union_pw_aff
2575 #define PART isl_pw_aff
2577 #define PARTS pw_aff
2581 #include <isl_union_templ.c>
2583 static __isl_give isl_set
*align_params_pw_pw_set_and(
2584 __isl_take isl_pw_aff
*pwaff1
, __isl_take isl_pw_aff
*pwaff2
,
2585 __isl_give isl_set
*(*fn
)(__isl_take isl_pw_aff
*pwaff1
,
2586 __isl_take isl_pw_aff
*pwaff2
))
2588 if (!pwaff1
|| !pwaff2
)
2590 if (isl_space_match(pwaff1
->dim
, isl_dim_param
,
2591 pwaff2
->dim
, isl_dim_param
))
2592 return fn(pwaff1
, pwaff2
);
2593 if (!isl_space_has_named_params(pwaff1
->dim
) ||
2594 !isl_space_has_named_params(pwaff2
->dim
))
2595 isl_die(isl_pw_aff_get_ctx(pwaff1
), isl_error_invalid
,
2596 "unaligned unnamed parameters", goto error
);
2597 pwaff1
= isl_pw_aff_align_params(pwaff1
, isl_pw_aff_get_space(pwaff2
));
2598 pwaff2
= isl_pw_aff_align_params(pwaff2
, isl_pw_aff_get_space(pwaff1
));
2599 return fn(pwaff1
, pwaff2
);
2601 isl_pw_aff_free(pwaff1
);
2602 isl_pw_aff_free(pwaff2
);
2606 /* Align the parameters of the to isl_pw_aff arguments and
2607 * then apply a function "fn" on them that returns an isl_map.
2609 static __isl_give isl_map
*align_params_pw_pw_map_and(
2610 __isl_take isl_pw_aff
*pa1
, __isl_take isl_pw_aff
*pa2
,
2611 __isl_give isl_map
*(*fn
)(__isl_take isl_pw_aff
*pa1
,
2612 __isl_take isl_pw_aff
*pa2
))
2616 if (isl_space_match(pa1
->dim
, isl_dim_param
, pa2
->dim
, isl_dim_param
))
2617 return fn(pa1
, pa2
);
2618 if (!isl_space_has_named_params(pa1
->dim
) ||
2619 !isl_space_has_named_params(pa2
->dim
))
2620 isl_die(isl_pw_aff_get_ctx(pa1
), isl_error_invalid
,
2621 "unaligned unnamed parameters", goto error
);
2622 pa1
= isl_pw_aff_align_params(pa1
, isl_pw_aff_get_space(pa2
));
2623 pa2
= isl_pw_aff_align_params(pa2
, isl_pw_aff_get_space(pa1
));
2624 return fn(pa1
, pa2
);
2626 isl_pw_aff_free(pa1
);
2627 isl_pw_aff_free(pa2
);
2631 /* Compute a piecewise quasi-affine expression with a domain that
2632 * is the union of those of pwaff1 and pwaff2 and such that on each
2633 * cell, the quasi-affine expression is the better (according to cmp)
2634 * of those of pwaff1 and pwaff2. If only one of pwaff1 or pwaff2
2635 * is defined on a given cell, then the associated expression
2636 * is the defined one.
2638 static __isl_give isl_pw_aff
*pw_aff_union_opt(__isl_take isl_pw_aff
*pwaff1
,
2639 __isl_take isl_pw_aff
*pwaff2
,
2640 __isl_give isl_basic_set
*(*cmp
)(__isl_take isl_aff
*aff1
,
2641 __isl_take isl_aff
*aff2
))
2648 if (!pwaff1
|| !pwaff2
)
2651 ctx
= isl_space_get_ctx(pwaff1
->dim
);
2652 if (!isl_space_is_equal(pwaff1
->dim
, pwaff2
->dim
))
2653 isl_die(ctx
, isl_error_invalid
,
2654 "arguments should live in same space", goto error
);
2656 if (isl_pw_aff_is_empty(pwaff1
)) {
2657 isl_pw_aff_free(pwaff1
);
2661 if (isl_pw_aff_is_empty(pwaff2
)) {
2662 isl_pw_aff_free(pwaff2
);
2666 n
= 2 * (pwaff1
->n
+ 1) * (pwaff2
->n
+ 1);
2667 res
= isl_pw_aff_alloc_size(isl_space_copy(pwaff1
->dim
), n
);
2669 for (i
= 0; i
< pwaff1
->n
; ++i
) {
2670 set
= isl_set_copy(pwaff1
->p
[i
].set
);
2671 for (j
= 0; j
< pwaff2
->n
; ++j
) {
2672 struct isl_set
*common
;
2675 common
= isl_set_intersect(
2676 isl_set_copy(pwaff1
->p
[i
].set
),
2677 isl_set_copy(pwaff2
->p
[j
].set
));
2678 better
= isl_set_from_basic_set(cmp(
2679 isl_aff_copy(pwaff2
->p
[j
].aff
),
2680 isl_aff_copy(pwaff1
->p
[i
].aff
)));
2681 better
= isl_set_intersect(common
, better
);
2682 if (isl_set_plain_is_empty(better
)) {
2683 isl_set_free(better
);
2686 set
= isl_set_subtract(set
, isl_set_copy(better
));
2688 res
= isl_pw_aff_add_piece(res
, better
,
2689 isl_aff_copy(pwaff2
->p
[j
].aff
));
2691 res
= isl_pw_aff_add_piece(res
, set
,
2692 isl_aff_copy(pwaff1
->p
[i
].aff
));
2695 for (j
= 0; j
< pwaff2
->n
; ++j
) {
2696 set
= isl_set_copy(pwaff2
->p
[j
].set
);
2697 for (i
= 0; i
< pwaff1
->n
; ++i
)
2698 set
= isl_set_subtract(set
,
2699 isl_set_copy(pwaff1
->p
[i
].set
));
2700 res
= isl_pw_aff_add_piece(res
, set
,
2701 isl_aff_copy(pwaff2
->p
[j
].aff
));
2704 isl_pw_aff_free(pwaff1
);
2705 isl_pw_aff_free(pwaff2
);
2709 isl_pw_aff_free(pwaff1
);
2710 isl_pw_aff_free(pwaff2
);
2714 /* Compute a piecewise quasi-affine expression with a domain that
2715 * is the union of those of pwaff1 and pwaff2 and such that on each
2716 * cell, the quasi-affine expression is the maximum of those of pwaff1
2717 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2718 * cell, then the associated expression is the defined one.
2720 static __isl_give isl_pw_aff
*pw_aff_union_max(__isl_take isl_pw_aff
*pwaff1
,
2721 __isl_take isl_pw_aff
*pwaff2
)
2723 return pw_aff_union_opt(pwaff1
, pwaff2
, &isl_aff_ge_basic_set
);
2726 __isl_give isl_pw_aff
*isl_pw_aff_union_max(__isl_take isl_pw_aff
*pwaff1
,
2727 __isl_take isl_pw_aff
*pwaff2
)
2729 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
,
2733 /* Compute a piecewise quasi-affine expression with a domain that
2734 * is the union of those of pwaff1 and pwaff2 and such that on each
2735 * cell, the quasi-affine expression is the minimum of those of pwaff1
2736 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2737 * cell, then the associated expression is the defined one.
2739 static __isl_give isl_pw_aff
*pw_aff_union_min(__isl_take isl_pw_aff
*pwaff1
,
2740 __isl_take isl_pw_aff
*pwaff2
)
2742 return pw_aff_union_opt(pwaff1
, pwaff2
, &isl_aff_le_basic_set
);
2745 __isl_give isl_pw_aff
*isl_pw_aff_union_min(__isl_take isl_pw_aff
*pwaff1
,
2746 __isl_take isl_pw_aff
*pwaff2
)
2748 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
,
2752 __isl_give isl_pw_aff
*isl_pw_aff_union_opt(__isl_take isl_pw_aff
*pwaff1
,
2753 __isl_take isl_pw_aff
*pwaff2
, int max
)
2756 return isl_pw_aff_union_max(pwaff1
, pwaff2
);
2758 return isl_pw_aff_union_min(pwaff1
, pwaff2
);
2761 /* Construct a map with as domain the domain of pwaff and
2762 * one-dimensional range corresponding to the affine expressions.
2764 static __isl_give isl_map
*map_from_pw_aff(__isl_take isl_pw_aff
*pwaff
)
2773 dim
= isl_pw_aff_get_space(pwaff
);
2774 map
= isl_map_empty(dim
);
2776 for (i
= 0; i
< pwaff
->n
; ++i
) {
2777 isl_basic_map
*bmap
;
2780 bmap
= isl_basic_map_from_aff(isl_aff_copy(pwaff
->p
[i
].aff
));
2781 map_i
= isl_map_from_basic_map(bmap
);
2782 map_i
= isl_map_intersect_domain(map_i
,
2783 isl_set_copy(pwaff
->p
[i
].set
));
2784 map
= isl_map_union_disjoint(map
, map_i
);
2787 isl_pw_aff_free(pwaff
);
2792 /* Construct a map with as domain the domain of pwaff and
2793 * one-dimensional range corresponding to the affine expressions.
2795 __isl_give isl_map
*isl_map_from_pw_aff(__isl_take isl_pw_aff
*pwaff
)
2799 if (isl_space_is_set(pwaff
->dim
))
2800 isl_die(isl_pw_aff_get_ctx(pwaff
), isl_error_invalid
,
2801 "space of input is not a map", goto error
);
2802 return map_from_pw_aff(pwaff
);
2804 isl_pw_aff_free(pwaff
);
2808 /* Construct a one-dimensional set with as parameter domain
2809 * the domain of pwaff and the single set dimension
2810 * corresponding to the affine expressions.
2812 __isl_give isl_set
*isl_set_from_pw_aff(__isl_take isl_pw_aff
*pwaff
)
2816 if (!isl_space_is_set(pwaff
->dim
))
2817 isl_die(isl_pw_aff_get_ctx(pwaff
), isl_error_invalid
,
2818 "space of input is not a set", goto error
);
2819 return map_from_pw_aff(pwaff
);
2821 isl_pw_aff_free(pwaff
);
2825 /* Return a set containing those elements in the domain
2826 * of "pwaff" where it satisfies "fn" (if complement is 0) or
2827 * does not satisfy "fn" (if complement is 1).
2829 * The pieces with a NaN never belong to the result since
2830 * NaN does not satisfy any property.
2832 static __isl_give isl_set
*pw_aff_locus(__isl_take isl_pw_aff
*pwaff
,
2833 __isl_give isl_basic_set
*(*fn
)(__isl_take isl_aff
*aff
, int rational
),
2842 set
= isl_set_empty(isl_pw_aff_get_domain_space(pwaff
));
2844 for (i
= 0; i
< pwaff
->n
; ++i
) {
2845 isl_basic_set
*bset
;
2846 isl_set
*set_i
, *locus
;
2849 if (isl_aff_is_nan(pwaff
->p
[i
].aff
))
2852 rational
= isl_set_has_rational(pwaff
->p
[i
].set
);
2853 bset
= fn(isl_aff_copy(pwaff
->p
[i
].aff
), rational
);
2854 locus
= isl_set_from_basic_set(bset
);
2855 set_i
= isl_set_copy(pwaff
->p
[i
].set
);
2857 set_i
= isl_set_subtract(set_i
, locus
);
2859 set_i
= isl_set_intersect(set_i
, locus
);
2860 set
= isl_set_union_disjoint(set
, set_i
);
2863 isl_pw_aff_free(pwaff
);
2868 /* Return a set containing those elements in the domain
2869 * of "pa" where it is positive.
2871 __isl_give isl_set
*isl_pw_aff_pos_set(__isl_take isl_pw_aff
*pa
)
2873 return pw_aff_locus(pa
, &aff_pos_basic_set
, 0);
2876 /* Return a set containing those elements in the domain
2877 * of pwaff where it is non-negative.
2879 __isl_give isl_set
*isl_pw_aff_nonneg_set(__isl_take isl_pw_aff
*pwaff
)
2881 return pw_aff_locus(pwaff
, &aff_nonneg_basic_set
, 0);
2884 /* Return a set containing those elements in the domain
2885 * of pwaff where it is zero.
2887 __isl_give isl_set
*isl_pw_aff_zero_set(__isl_take isl_pw_aff
*pwaff
)
2889 return pw_aff_locus(pwaff
, &aff_zero_basic_set
, 0);
2892 /* Return a set containing those elements in the domain
2893 * of pwaff where it is not zero.
2895 __isl_give isl_set
*isl_pw_aff_non_zero_set(__isl_take isl_pw_aff
*pwaff
)
2897 return pw_aff_locus(pwaff
, &aff_zero_basic_set
, 1);
2900 /* Return a set containing those elements in the shared domain
2901 * of pwaff1 and pwaff2 where pwaff1 is greater than (or equal) to pwaff2.
2903 * We compute the difference on the shared domain and then construct
2904 * the set of values where this difference is non-negative.
2905 * If strict is set, we first subtract 1 from the difference.
2906 * If equal is set, we only return the elements where pwaff1 and pwaff2
2909 static __isl_give isl_set
*pw_aff_gte_set(__isl_take isl_pw_aff
*pwaff1
,
2910 __isl_take isl_pw_aff
*pwaff2
, int strict
, int equal
)
2912 isl_set
*set1
, *set2
;
2914 set1
= isl_pw_aff_domain(isl_pw_aff_copy(pwaff1
));
2915 set2
= isl_pw_aff_domain(isl_pw_aff_copy(pwaff2
));
2916 set1
= isl_set_intersect(set1
, set2
);
2917 pwaff1
= isl_pw_aff_intersect_domain(pwaff1
, isl_set_copy(set1
));
2918 pwaff2
= isl_pw_aff_intersect_domain(pwaff2
, isl_set_copy(set1
));
2919 pwaff1
= isl_pw_aff_add(pwaff1
, isl_pw_aff_neg(pwaff2
));
2922 isl_space
*dim
= isl_set_get_space(set1
);
2924 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(dim
));
2925 aff
= isl_aff_add_constant_si(aff
, -1);
2926 pwaff1
= isl_pw_aff_add(pwaff1
, isl_pw_aff_alloc(set1
, aff
));
2931 return isl_pw_aff_zero_set(pwaff1
);
2932 return isl_pw_aff_nonneg_set(pwaff1
);
2935 /* Return a set containing those elements in the shared domain
2936 * of pwaff1 and pwaff2 where pwaff1 is equal to pwaff2.
2938 static __isl_give isl_set
*pw_aff_eq_set(__isl_take isl_pw_aff
*pwaff1
,
2939 __isl_take isl_pw_aff
*pwaff2
)
2941 return pw_aff_gte_set(pwaff1
, pwaff2
, 0, 1);
2944 __isl_give isl_set
*isl_pw_aff_eq_set(__isl_take isl_pw_aff
*pwaff1
,
2945 __isl_take isl_pw_aff
*pwaff2
)
2947 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_eq_set
);
2950 /* Return a set containing those elements in the shared domain
2951 * of pwaff1 and pwaff2 where pwaff1 is greater than or equal to pwaff2.
2953 static __isl_give isl_set
*pw_aff_ge_set(__isl_take isl_pw_aff
*pwaff1
,
2954 __isl_take isl_pw_aff
*pwaff2
)
2956 return pw_aff_gte_set(pwaff1
, pwaff2
, 0, 0);
2959 __isl_give isl_set
*isl_pw_aff_ge_set(__isl_take isl_pw_aff
*pwaff1
,
2960 __isl_take isl_pw_aff
*pwaff2
)
2962 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_ge_set
);
2965 /* Return a set containing those elements in the shared domain
2966 * of pwaff1 and pwaff2 where pwaff1 is strictly greater than pwaff2.
2968 static __isl_give isl_set
*pw_aff_gt_set(__isl_take isl_pw_aff
*pwaff1
,
2969 __isl_take isl_pw_aff
*pwaff2
)
2971 return pw_aff_gte_set(pwaff1
, pwaff2
, 1, 0);
2974 __isl_give isl_set
*isl_pw_aff_gt_set(__isl_take isl_pw_aff
*pwaff1
,
2975 __isl_take isl_pw_aff
*pwaff2
)
2977 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_gt_set
);
2980 __isl_give isl_set
*isl_pw_aff_le_set(__isl_take isl_pw_aff
*pwaff1
,
2981 __isl_take isl_pw_aff
*pwaff2
)
2983 return isl_pw_aff_ge_set(pwaff2
, pwaff1
);
2986 __isl_give isl_set
*isl_pw_aff_lt_set(__isl_take isl_pw_aff
*pwaff1
,
2987 __isl_take isl_pw_aff
*pwaff2
)
2989 return isl_pw_aff_gt_set(pwaff2
, pwaff1
);
2992 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
2993 * where the function values are ordered in the same way as "order",
2994 * which returns a set in the shared domain of its two arguments.
2995 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
2997 * Let "pa1" and "pa2" be defined on domains A and B respectively.
2998 * We first pull back the two functions such that they are defined on
2999 * the domain [A -> B]. Then we apply "order", resulting in a set
3000 * in the space [A -> B]. Finally, we unwrap this set to obtain
3001 * a map in the space A -> B.
3003 static __isl_give isl_map
*isl_pw_aff_order_map_aligned(
3004 __isl_take isl_pw_aff
*pa1
, __isl_take isl_pw_aff
*pa2
,
3005 __isl_give isl_set
*(*order
)(__isl_take isl_pw_aff
*pa1
,
3006 __isl_take isl_pw_aff
*pa2
))
3008 isl_space
*space1
, *space2
;
3012 space1
= isl_space_domain(isl_pw_aff_get_space(pa1
));
3013 space2
= isl_space_domain(isl_pw_aff_get_space(pa2
));
3014 space1
= isl_space_map_from_domain_and_range(space1
, space2
);
3015 ma
= isl_multi_aff_domain_map(isl_space_copy(space1
));
3016 pa1
= isl_pw_aff_pullback_multi_aff(pa1
, ma
);
3017 ma
= isl_multi_aff_range_map(space1
);
3018 pa2
= isl_pw_aff_pullback_multi_aff(pa2
, ma
);
3019 set
= order(pa1
, pa2
);
3021 return isl_set_unwrap(set
);
3024 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3025 * where the function values are equal.
3026 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3028 static __isl_give isl_map
*isl_pw_aff_eq_map_aligned(__isl_take isl_pw_aff
*pa1
,
3029 __isl_take isl_pw_aff
*pa2
)
3031 return isl_pw_aff_order_map_aligned(pa1
, pa2
, &isl_pw_aff_eq_set
);
3034 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3035 * where the function values are equal.
3037 __isl_give isl_map
*isl_pw_aff_eq_map(__isl_take isl_pw_aff
*pa1
,
3038 __isl_take isl_pw_aff
*pa2
)
3040 return align_params_pw_pw_map_and(pa1
, pa2
, &isl_pw_aff_eq_map_aligned
);
3043 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3044 * where the function value of "pa1" is less than the function value of "pa2".
3045 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3047 static __isl_give isl_map
*isl_pw_aff_lt_map_aligned(__isl_take isl_pw_aff
*pa1
,
3048 __isl_take isl_pw_aff
*pa2
)
3050 return isl_pw_aff_order_map_aligned(pa1
, pa2
, &isl_pw_aff_lt_set
);
3053 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3054 * where the function value of "pa1" is less than the function value of "pa2".
3056 __isl_give isl_map
*isl_pw_aff_lt_map(__isl_take isl_pw_aff
*pa1
,
3057 __isl_take isl_pw_aff
*pa2
)
3059 return align_params_pw_pw_map_and(pa1
, pa2
, &isl_pw_aff_lt_map_aligned
);
3062 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3063 * where the function value of "pa1" is greater than the function value
3065 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3067 static __isl_give isl_map
*isl_pw_aff_gt_map_aligned(__isl_take isl_pw_aff
*pa1
,
3068 __isl_take isl_pw_aff
*pa2
)
3070 return isl_pw_aff_order_map_aligned(pa1
, pa2
, &isl_pw_aff_gt_set
);
3073 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3074 * where the function value of "pa1" is greater than the function value
3077 __isl_give isl_map
*isl_pw_aff_gt_map(__isl_take isl_pw_aff
*pa1
,
3078 __isl_take isl_pw_aff
*pa2
)
3080 return align_params_pw_pw_map_and(pa1
, pa2
, &isl_pw_aff_gt_map_aligned
);
3083 /* Return a set containing those elements in the shared domain
3084 * of the elements of list1 and list2 where each element in list1
3085 * has the relation specified by "fn" with each element in list2.
3087 static __isl_give isl_set
*pw_aff_list_set(__isl_take isl_pw_aff_list
*list1
,
3088 __isl_take isl_pw_aff_list
*list2
,
3089 __isl_give isl_set
*(*fn
)(__isl_take isl_pw_aff
*pwaff1
,
3090 __isl_take isl_pw_aff
*pwaff2
))
3096 if (!list1
|| !list2
)
3099 ctx
= isl_pw_aff_list_get_ctx(list1
);
3100 if (list1
->n
< 1 || list2
->n
< 1)
3101 isl_die(ctx
, isl_error_invalid
,
3102 "list should contain at least one element", goto error
);
3104 set
= isl_set_universe(isl_pw_aff_get_domain_space(list1
->p
[0]));
3105 for (i
= 0; i
< list1
->n
; ++i
)
3106 for (j
= 0; j
< list2
->n
; ++j
) {
3109 set_ij
= fn(isl_pw_aff_copy(list1
->p
[i
]),
3110 isl_pw_aff_copy(list2
->p
[j
]));
3111 set
= isl_set_intersect(set
, set_ij
);
3114 isl_pw_aff_list_free(list1
);
3115 isl_pw_aff_list_free(list2
);
3118 isl_pw_aff_list_free(list1
);
3119 isl_pw_aff_list_free(list2
);
3123 /* Return a set containing those elements in the shared domain
3124 * of the elements of list1 and list2 where each element in list1
3125 * is equal to each element in list2.
3127 __isl_give isl_set
*isl_pw_aff_list_eq_set(__isl_take isl_pw_aff_list
*list1
,
3128 __isl_take isl_pw_aff_list
*list2
)
3130 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_eq_set
);
3133 __isl_give isl_set
*isl_pw_aff_list_ne_set(__isl_take isl_pw_aff_list
*list1
,
3134 __isl_take isl_pw_aff_list
*list2
)
3136 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_ne_set
);
3139 /* Return a set containing those elements in the shared domain
3140 * of the elements of list1 and list2 where each element in list1
3141 * is less than or equal to each element in list2.
3143 __isl_give isl_set
*isl_pw_aff_list_le_set(__isl_take isl_pw_aff_list
*list1
,
3144 __isl_take isl_pw_aff_list
*list2
)
3146 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_le_set
);
3149 __isl_give isl_set
*isl_pw_aff_list_lt_set(__isl_take isl_pw_aff_list
*list1
,
3150 __isl_take isl_pw_aff_list
*list2
)
3152 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_lt_set
);
3155 __isl_give isl_set
*isl_pw_aff_list_ge_set(__isl_take isl_pw_aff_list
*list1
,
3156 __isl_take isl_pw_aff_list
*list2
)
3158 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_ge_set
);
3161 __isl_give isl_set
*isl_pw_aff_list_gt_set(__isl_take isl_pw_aff_list
*list1
,
3162 __isl_take isl_pw_aff_list
*list2
)
3164 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_gt_set
);
3168 /* Return a set containing those elements in the shared domain
3169 * of pwaff1 and pwaff2 where pwaff1 is not equal to pwaff2.
3171 static __isl_give isl_set
*pw_aff_ne_set(__isl_take isl_pw_aff
*pwaff1
,
3172 __isl_take isl_pw_aff
*pwaff2
)
3174 isl_set
*set_lt
, *set_gt
;
3176 set_lt
= isl_pw_aff_lt_set(isl_pw_aff_copy(pwaff1
),
3177 isl_pw_aff_copy(pwaff2
));
3178 set_gt
= isl_pw_aff_gt_set(pwaff1
, pwaff2
);
3179 return isl_set_union_disjoint(set_lt
, set_gt
);
3182 __isl_give isl_set
*isl_pw_aff_ne_set(__isl_take isl_pw_aff
*pwaff1
,
3183 __isl_take isl_pw_aff
*pwaff2
)
3185 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_ne_set
);
3188 __isl_give isl_pw_aff
*isl_pw_aff_scale_down(__isl_take isl_pw_aff
*pwaff
,
3193 if (isl_int_is_one(v
))
3195 if (!isl_int_is_pos(v
))
3196 isl_die(isl_pw_aff_get_ctx(pwaff
), isl_error_invalid
,
3197 "factor needs to be positive",
3198 return isl_pw_aff_free(pwaff
));
3199 pwaff
= isl_pw_aff_cow(pwaff
);
3205 for (i
= 0; i
< pwaff
->n
; ++i
) {
3206 pwaff
->p
[i
].aff
= isl_aff_scale_down(pwaff
->p
[i
].aff
, v
);
3207 if (!pwaff
->p
[i
].aff
)
3208 return isl_pw_aff_free(pwaff
);
3214 __isl_give isl_pw_aff
*isl_pw_aff_floor(__isl_take isl_pw_aff
*pwaff
)
3218 pwaff
= isl_pw_aff_cow(pwaff
);
3224 for (i
= 0; i
< pwaff
->n
; ++i
) {
3225 pwaff
->p
[i
].aff
= isl_aff_floor(pwaff
->p
[i
].aff
);
3226 if (!pwaff
->p
[i
].aff
)
3227 return isl_pw_aff_free(pwaff
);
3233 __isl_give isl_pw_aff
*isl_pw_aff_ceil(__isl_take isl_pw_aff
*pwaff
)
3237 pwaff
= isl_pw_aff_cow(pwaff
);
3243 for (i
= 0; i
< pwaff
->n
; ++i
) {
3244 pwaff
->p
[i
].aff
= isl_aff_ceil(pwaff
->p
[i
].aff
);
3245 if (!pwaff
->p
[i
].aff
)
3246 return isl_pw_aff_free(pwaff
);
3252 /* Assuming that "cond1" and "cond2" are disjoint,
3253 * return an affine expression that is equal to pwaff1 on cond1
3254 * and to pwaff2 on cond2.
3256 static __isl_give isl_pw_aff
*isl_pw_aff_select(
3257 __isl_take isl_set
*cond1
, __isl_take isl_pw_aff
*pwaff1
,
3258 __isl_take isl_set
*cond2
, __isl_take isl_pw_aff
*pwaff2
)
3260 pwaff1
= isl_pw_aff_intersect_domain(pwaff1
, cond1
);
3261 pwaff2
= isl_pw_aff_intersect_domain(pwaff2
, cond2
);
3263 return isl_pw_aff_add_disjoint(pwaff1
, pwaff2
);
3266 /* Return an affine expression that is equal to pwaff_true for elements
3267 * where "cond" is non-zero and to pwaff_false for elements where "cond"
3269 * That is, return cond ? pwaff_true : pwaff_false;
3271 * If "cond" involves and NaN, then we conservatively return a NaN
3272 * on its entire domain. In principle, we could consider the pieces
3273 * where it is NaN separately from those where it is not.
3275 __isl_give isl_pw_aff
*isl_pw_aff_cond(__isl_take isl_pw_aff
*cond
,
3276 __isl_take isl_pw_aff
*pwaff_true
, __isl_take isl_pw_aff
*pwaff_false
)
3278 isl_set
*cond_true
, *cond_false
;
3282 if (isl_pw_aff_involves_nan(cond
)) {
3283 isl_space
*space
= isl_pw_aff_get_domain_space(cond
);
3284 isl_local_space
*ls
= isl_local_space_from_space(space
);
3285 isl_pw_aff_free(cond
);
3286 isl_pw_aff_free(pwaff_true
);
3287 isl_pw_aff_free(pwaff_false
);
3288 return isl_pw_aff_nan_on_domain(ls
);
3291 cond_true
= isl_pw_aff_non_zero_set(isl_pw_aff_copy(cond
));
3292 cond_false
= isl_pw_aff_zero_set(cond
);
3293 return isl_pw_aff_select(cond_true
, pwaff_true
,
3294 cond_false
, pwaff_false
);
3296 isl_pw_aff_free(cond
);
3297 isl_pw_aff_free(pwaff_true
);
3298 isl_pw_aff_free(pwaff_false
);
3302 int isl_aff_is_cst(__isl_keep isl_aff
*aff
)
3307 return isl_seq_first_non_zero(aff
->v
->el
+ 2, aff
->v
->size
- 2) == -1;
3310 /* Check whether pwaff is a piecewise constant.
3312 int isl_pw_aff_is_cst(__isl_keep isl_pw_aff
*pwaff
)
3319 for (i
= 0; i
< pwaff
->n
; ++i
) {
3320 int is_cst
= isl_aff_is_cst(pwaff
->p
[i
].aff
);
3321 if (is_cst
< 0 || !is_cst
)
3328 /* Return the product of "aff1" and "aff2".
3330 * If either of the two is NaN, then the result is NaN.
3332 * Otherwise, at least one of "aff1" or "aff2" needs to be a constant.
3334 __isl_give isl_aff
*isl_aff_mul(__isl_take isl_aff
*aff1
,
3335 __isl_take isl_aff
*aff2
)
3340 if (isl_aff_is_nan(aff1
)) {
3344 if (isl_aff_is_nan(aff2
)) {
3349 if (!isl_aff_is_cst(aff2
) && isl_aff_is_cst(aff1
))
3350 return isl_aff_mul(aff2
, aff1
);
3352 if (!isl_aff_is_cst(aff2
))
3353 isl_die(isl_aff_get_ctx(aff1
), isl_error_invalid
,
3354 "at least one affine expression should be constant",
3357 aff1
= isl_aff_cow(aff1
);
3361 aff1
= isl_aff_scale(aff1
, aff2
->v
->el
[1]);
3362 aff1
= isl_aff_scale_down(aff1
, aff2
->v
->el
[0]);
3372 /* Divide "aff1" by "aff2", assuming "aff2" is a constant.
3374 * If either of the two is NaN, then the result is NaN.
3376 __isl_give isl_aff
*isl_aff_div(__isl_take isl_aff
*aff1
,
3377 __isl_take isl_aff
*aff2
)
3385 if (isl_aff_is_nan(aff1
)) {
3389 if (isl_aff_is_nan(aff2
)) {
3394 is_cst
= isl_aff_is_cst(aff2
);
3398 isl_die(isl_aff_get_ctx(aff2
), isl_error_invalid
,
3399 "second argument should be a constant", goto error
);
3404 neg
= isl_int_is_neg(aff2
->v
->el
[1]);
3406 isl_int_neg(aff2
->v
->el
[0], aff2
->v
->el
[0]);
3407 isl_int_neg(aff2
->v
->el
[1], aff2
->v
->el
[1]);
3410 aff1
= isl_aff_scale(aff1
, aff2
->v
->el
[0]);
3411 aff1
= isl_aff_scale_down(aff1
, aff2
->v
->el
[1]);
3414 isl_int_neg(aff2
->v
->el
[0], aff2
->v
->el
[0]);
3415 isl_int_neg(aff2
->v
->el
[1], aff2
->v
->el
[1]);
3426 static __isl_give isl_pw_aff
*pw_aff_add(__isl_take isl_pw_aff
*pwaff1
,
3427 __isl_take isl_pw_aff
*pwaff2
)
3429 return isl_pw_aff_on_shared_domain(pwaff1
, pwaff2
, &isl_aff_add
);
3432 __isl_give isl_pw_aff
*isl_pw_aff_add(__isl_take isl_pw_aff
*pwaff1
,
3433 __isl_take isl_pw_aff
*pwaff2
)
3435 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
, &pw_aff_add
);
3438 __isl_give isl_pw_aff
*isl_pw_aff_union_add(__isl_take isl_pw_aff
*pwaff1
,
3439 __isl_take isl_pw_aff
*pwaff2
)
3441 return isl_pw_aff_union_add_(pwaff1
, pwaff2
);
3444 static __isl_give isl_pw_aff
*pw_aff_mul(__isl_take isl_pw_aff
*pwaff1
,
3445 __isl_take isl_pw_aff
*pwaff2
)
3447 return isl_pw_aff_on_shared_domain(pwaff1
, pwaff2
, &isl_aff_mul
);
3450 __isl_give isl_pw_aff
*isl_pw_aff_mul(__isl_take isl_pw_aff
*pwaff1
,
3451 __isl_take isl_pw_aff
*pwaff2
)
3453 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
, &pw_aff_mul
);
3456 static __isl_give isl_pw_aff
*pw_aff_div(__isl_take isl_pw_aff
*pa1
,
3457 __isl_take isl_pw_aff
*pa2
)
3459 return isl_pw_aff_on_shared_domain(pa1
, pa2
, &isl_aff_div
);
3462 /* Divide "pa1" by "pa2", assuming "pa2" is a piecewise constant.
3464 __isl_give isl_pw_aff
*isl_pw_aff_div(__isl_take isl_pw_aff
*pa1
,
3465 __isl_take isl_pw_aff
*pa2
)
3469 is_cst
= isl_pw_aff_is_cst(pa2
);
3473 isl_die(isl_pw_aff_get_ctx(pa2
), isl_error_invalid
,
3474 "second argument should be a piecewise constant",
3476 return isl_pw_aff_align_params_pw_pw_and(pa1
, pa2
, &pw_aff_div
);
3478 isl_pw_aff_free(pa1
);
3479 isl_pw_aff_free(pa2
);
3483 /* Compute the quotient of the integer division of "pa1" by "pa2"
3484 * with rounding towards zero.
3485 * "pa2" is assumed to be a piecewise constant.
3487 * In particular, return
3489 * pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2)
3492 __isl_give isl_pw_aff
*isl_pw_aff_tdiv_q(__isl_take isl_pw_aff
*pa1
,
3493 __isl_take isl_pw_aff
*pa2
)
3499 is_cst
= isl_pw_aff_is_cst(pa2
);
3503 isl_die(isl_pw_aff_get_ctx(pa2
), isl_error_invalid
,
3504 "second argument should be a piecewise constant",
3507 pa1
= isl_pw_aff_div(pa1
, pa2
);
3509 cond
= isl_pw_aff_nonneg_set(isl_pw_aff_copy(pa1
));
3510 f
= isl_pw_aff_floor(isl_pw_aff_copy(pa1
));
3511 c
= isl_pw_aff_ceil(pa1
);
3512 return isl_pw_aff_cond(isl_set_indicator_function(cond
), f
, c
);
3514 isl_pw_aff_free(pa1
);
3515 isl_pw_aff_free(pa2
);
3519 /* Compute the remainder of the integer division of "pa1" by "pa2"
3520 * with rounding towards zero.
3521 * "pa2" is assumed to be a piecewise constant.
3523 * In particular, return
3525 * pa1 - pa2 * (pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2))
3528 __isl_give isl_pw_aff
*isl_pw_aff_tdiv_r(__isl_take isl_pw_aff
*pa1
,
3529 __isl_take isl_pw_aff
*pa2
)
3534 is_cst
= isl_pw_aff_is_cst(pa2
);
3538 isl_die(isl_pw_aff_get_ctx(pa2
), isl_error_invalid
,
3539 "second argument should be a piecewise constant",
3541 res
= isl_pw_aff_tdiv_q(isl_pw_aff_copy(pa1
), isl_pw_aff_copy(pa2
));
3542 res
= isl_pw_aff_mul(pa2
, res
);
3543 res
= isl_pw_aff_sub(pa1
, res
);
3546 isl_pw_aff_free(pa1
);
3547 isl_pw_aff_free(pa2
);
3551 static __isl_give isl_pw_aff
*pw_aff_min(__isl_take isl_pw_aff
*pwaff1
,
3552 __isl_take isl_pw_aff
*pwaff2
)
3557 dom
= isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1
)),
3558 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2
)));
3559 le
= isl_pw_aff_le_set(isl_pw_aff_copy(pwaff1
),
3560 isl_pw_aff_copy(pwaff2
));
3561 dom
= isl_set_subtract(dom
, isl_set_copy(le
));
3562 return isl_pw_aff_select(le
, pwaff1
, dom
, pwaff2
);
3565 __isl_give isl_pw_aff
*isl_pw_aff_min(__isl_take isl_pw_aff
*pwaff1
,
3566 __isl_take isl_pw_aff
*pwaff2
)
3568 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
, &pw_aff_min
);
3571 static __isl_give isl_pw_aff
*pw_aff_max(__isl_take isl_pw_aff
*pwaff1
,
3572 __isl_take isl_pw_aff
*pwaff2
)
3577 dom
= isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1
)),
3578 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2
)));
3579 ge
= isl_pw_aff_ge_set(isl_pw_aff_copy(pwaff1
),
3580 isl_pw_aff_copy(pwaff2
));
3581 dom
= isl_set_subtract(dom
, isl_set_copy(ge
));
3582 return isl_pw_aff_select(ge
, pwaff1
, dom
, pwaff2
);
3585 __isl_give isl_pw_aff
*isl_pw_aff_max(__isl_take isl_pw_aff
*pwaff1
,
3586 __isl_take isl_pw_aff
*pwaff2
)
3588 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
, &pw_aff_max
);
3591 static __isl_give isl_pw_aff
*pw_aff_list_reduce(
3592 __isl_take isl_pw_aff_list
*list
,
3593 __isl_give isl_pw_aff
*(*fn
)(__isl_take isl_pw_aff
*pwaff1
,
3594 __isl_take isl_pw_aff
*pwaff2
))
3603 ctx
= isl_pw_aff_list_get_ctx(list
);
3605 isl_die(ctx
, isl_error_invalid
,
3606 "list should contain at least one element", goto error
);
3608 res
= isl_pw_aff_copy(list
->p
[0]);
3609 for (i
= 1; i
< list
->n
; ++i
)
3610 res
= fn(res
, isl_pw_aff_copy(list
->p
[i
]));
3612 isl_pw_aff_list_free(list
);
3615 isl_pw_aff_list_free(list
);
3619 /* Return an isl_pw_aff that maps each element in the intersection of the
3620 * domains of the elements of list to the minimal corresponding affine
3623 __isl_give isl_pw_aff
*isl_pw_aff_list_min(__isl_take isl_pw_aff_list
*list
)
3625 return pw_aff_list_reduce(list
, &isl_pw_aff_min
);
3628 /* Return an isl_pw_aff that maps each element in the intersection of the
3629 * domains of the elements of list to the maximal corresponding affine
3632 __isl_give isl_pw_aff
*isl_pw_aff_list_max(__isl_take isl_pw_aff_list
*list
)
3634 return pw_aff_list_reduce(list
, &isl_pw_aff_max
);
3637 /* Mark the domains of "pwaff" as rational.
3639 __isl_give isl_pw_aff
*isl_pw_aff_set_rational(__isl_take isl_pw_aff
*pwaff
)
3643 pwaff
= isl_pw_aff_cow(pwaff
);
3649 for (i
= 0; i
< pwaff
->n
; ++i
) {
3650 pwaff
->p
[i
].set
= isl_set_set_rational(pwaff
->p
[i
].set
);
3651 if (!pwaff
->p
[i
].set
)
3652 return isl_pw_aff_free(pwaff
);
3658 /* Mark the domains of the elements of "list" as rational.
3660 __isl_give isl_pw_aff_list
*isl_pw_aff_list_set_rational(
3661 __isl_take isl_pw_aff_list
*list
)
3671 for (i
= 0; i
< n
; ++i
) {
3674 pa
= isl_pw_aff_list_get_pw_aff(list
, i
);
3675 pa
= isl_pw_aff_set_rational(pa
);
3676 list
= isl_pw_aff_list_set_pw_aff(list
, i
, pa
);
3682 /* Do the parameters of "aff" match those of "space"?
3684 int isl_aff_matching_params(__isl_keep isl_aff
*aff
,
3685 __isl_keep isl_space
*space
)
3687 isl_space
*aff_space
;
3693 aff_space
= isl_aff_get_domain_space(aff
);
3695 match
= isl_space_match(space
, isl_dim_param
, aff_space
, isl_dim_param
);
3697 isl_space_free(aff_space
);
3701 /* Check that the domain space of "aff" matches "space".
3703 * Return 0 on success and -1 on error.
3705 int isl_aff_check_match_domain_space(__isl_keep isl_aff
*aff
,
3706 __isl_keep isl_space
*space
)
3708 isl_space
*aff_space
;
3714 aff_space
= isl_aff_get_domain_space(aff
);
3716 match
= isl_space_match(space
, isl_dim_param
, aff_space
, isl_dim_param
);
3720 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
3721 "parameters don't match", goto error
);
3722 match
= isl_space_tuple_is_equal(space
, isl_dim_in
,
3723 aff_space
, isl_dim_set
);
3727 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
3728 "domains don't match", goto error
);
3729 isl_space_free(aff_space
);
3732 isl_space_free(aff_space
);
3742 #include <isl_multi_templ.c>
3743 #include <isl_multi_apply_set.c>
3744 #include <isl_multi_floor.c>
3745 #include <isl_multi_gist.c>
3749 /* Remove any internal structure of the domain of "ma".
3750 * If there is any such internal structure in the input,
3751 * then the name of the corresponding space is also removed.
3753 __isl_give isl_multi_aff
*isl_multi_aff_flatten_domain(
3754 __isl_take isl_multi_aff
*ma
)
3761 if (!ma
->space
->nested
[0])
3764 space
= isl_multi_aff_get_space(ma
);
3765 space
= isl_space_flatten_domain(space
);
3766 ma
= isl_multi_aff_reset_space(ma
, space
);
3771 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
3772 * of the space to its domain.
3774 __isl_give isl_multi_aff
*isl_multi_aff_domain_map(__isl_take isl_space
*space
)
3777 isl_local_space
*ls
;
3782 if (!isl_space_is_map(space
))
3783 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
3784 "not a map space", goto error
);
3786 n_in
= isl_space_dim(space
, isl_dim_in
);
3787 space
= isl_space_domain_map(space
);
3789 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
3791 isl_space_free(space
);
3795 space
= isl_space_domain(space
);
3796 ls
= isl_local_space_from_space(space
);
3797 for (i
= 0; i
< n_in
; ++i
) {
3800 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
3802 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
3804 isl_local_space_free(ls
);
3807 isl_space_free(space
);
3811 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
3812 * of the space to its range.
3814 __isl_give isl_multi_aff
*isl_multi_aff_range_map(__isl_take isl_space
*space
)
3817 isl_local_space
*ls
;
3822 if (!isl_space_is_map(space
))
3823 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
3824 "not a map space", goto error
);
3826 n_in
= isl_space_dim(space
, isl_dim_in
);
3827 n_out
= isl_space_dim(space
, isl_dim_out
);
3828 space
= isl_space_range_map(space
);
3830 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
3832 isl_space_free(space
);
3836 space
= isl_space_domain(space
);
3837 ls
= isl_local_space_from_space(space
);
3838 for (i
= 0; i
< n_out
; ++i
) {
3841 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
3842 isl_dim_set
, n_in
+ i
);
3843 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
3845 isl_local_space_free(ls
);
3848 isl_space_free(space
);
3852 /* Given a map space, return an isl_pw_multi_aff that maps a wrapped copy
3853 * of the space to its range.
3855 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_range_map(
3856 __isl_take isl_space
*space
)
3858 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_range_map(space
));
3861 /* Given the space of a set and a range of set dimensions,
3862 * construct an isl_multi_aff that projects out those dimensions.
3864 __isl_give isl_multi_aff
*isl_multi_aff_project_out_map(
3865 __isl_take isl_space
*space
, enum isl_dim_type type
,
3866 unsigned first
, unsigned n
)
3869 isl_local_space
*ls
;
3874 if (!isl_space_is_set(space
))
3875 isl_die(isl_space_get_ctx(space
), isl_error_unsupported
,
3876 "expecting set space", goto error
);
3877 if (type
!= isl_dim_set
)
3878 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
3879 "only set dimensions can be projected out", goto error
);
3881 dim
= isl_space_dim(space
, isl_dim_set
);
3882 if (first
+ n
> dim
)
3883 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
3884 "range out of bounds", goto error
);
3886 space
= isl_space_from_domain(space
);
3887 space
= isl_space_add_dims(space
, isl_dim_out
, dim
- n
);
3890 return isl_multi_aff_alloc(space
);
3892 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
3893 space
= isl_space_domain(space
);
3894 ls
= isl_local_space_from_space(space
);
3896 for (i
= 0; i
< first
; ++i
) {
3899 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
3901 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
3904 for (i
= 0; i
< dim
- (first
+ n
); ++i
) {
3907 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
3908 isl_dim_set
, first
+ n
+ i
);
3909 ma
= isl_multi_aff_set_aff(ma
, first
+ i
, aff
);
3912 isl_local_space_free(ls
);
3915 isl_space_free(space
);
3919 /* Given the space of a set and a range of set dimensions,
3920 * construct an isl_pw_multi_aff that projects out those dimensions.
3922 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_project_out_map(
3923 __isl_take isl_space
*space
, enum isl_dim_type type
,
3924 unsigned first
, unsigned n
)
3928 ma
= isl_multi_aff_project_out_map(space
, type
, first
, n
);
3929 return isl_pw_multi_aff_from_multi_aff(ma
);
3932 /* Create an isl_pw_multi_aff with the given isl_multi_aff on a universe
3935 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_multi_aff(
3936 __isl_take isl_multi_aff
*ma
)
3938 isl_set
*dom
= isl_set_universe(isl_multi_aff_get_domain_space(ma
));
3939 return isl_pw_multi_aff_alloc(dom
, ma
);
3942 /* Create a piecewise multi-affine expression in the given space that maps each
3943 * input dimension to the corresponding output dimension.
3945 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_identity(
3946 __isl_take isl_space
*space
)
3948 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_identity(space
));
3951 /* Add "ma2" to "ma1" and return the result.
3953 * The parameters of "ma1" and "ma2" are assumed to have been aligned.
3955 static __isl_give isl_multi_aff
*isl_multi_aff_add_aligned(
3956 __isl_take isl_multi_aff
*maff1
, __isl_take isl_multi_aff
*maff2
)
3958 return isl_multi_aff_bin_op(maff1
, maff2
, &isl_aff_add
);
3961 /* Add "ma2" to "ma1" and return the result.
3963 __isl_give isl_multi_aff
*isl_multi_aff_add(__isl_take isl_multi_aff
*ma1
,
3964 __isl_take isl_multi_aff
*ma2
)
3966 return isl_multi_aff_align_params_multi_multi_and(ma1
, ma2
,
3967 &isl_multi_aff_add_aligned
);
3970 /* Exploit the equalities in "eq" to simplify the affine expressions.
3972 static __isl_give isl_multi_aff
*isl_multi_aff_substitute_equalities(
3973 __isl_take isl_multi_aff
*maff
, __isl_take isl_basic_set
*eq
)
3977 maff
= isl_multi_aff_cow(maff
);
3981 for (i
= 0; i
< maff
->n
; ++i
) {
3982 maff
->p
[i
] = isl_aff_substitute_equalities(maff
->p
[i
],
3983 isl_basic_set_copy(eq
));
3988 isl_basic_set_free(eq
);
3991 isl_basic_set_free(eq
);
3992 isl_multi_aff_free(maff
);
3996 __isl_give isl_multi_aff
*isl_multi_aff_scale(__isl_take isl_multi_aff
*maff
,
4001 maff
= isl_multi_aff_cow(maff
);
4005 for (i
= 0; i
< maff
->n
; ++i
) {
4006 maff
->p
[i
] = isl_aff_scale(maff
->p
[i
], f
);
4008 return isl_multi_aff_free(maff
);
4014 __isl_give isl_multi_aff
*isl_multi_aff_add_on_domain(__isl_keep isl_set
*dom
,
4015 __isl_take isl_multi_aff
*maff1
, __isl_take isl_multi_aff
*maff2
)
4017 maff1
= isl_multi_aff_add(maff1
, maff2
);
4018 maff1
= isl_multi_aff_gist(maff1
, isl_set_copy(dom
));
4022 int isl_multi_aff_is_empty(__isl_keep isl_multi_aff
*maff
)
4030 /* Return the set of domain elements where "ma1" is lexicographically
4031 * smaller than or equal to "ma2".
4033 __isl_give isl_set
*isl_multi_aff_lex_le_set(__isl_take isl_multi_aff
*ma1
,
4034 __isl_take isl_multi_aff
*ma2
)
4036 return isl_multi_aff_lex_ge_set(ma2
, ma1
);
4039 /* Return the set of domain elements where "ma1" is lexicographically
4040 * greater than or equal to "ma2".
4042 __isl_give isl_set
*isl_multi_aff_lex_ge_set(__isl_take isl_multi_aff
*ma1
,
4043 __isl_take isl_multi_aff
*ma2
)
4046 isl_map
*map1
, *map2
;
4049 map1
= isl_map_from_multi_aff(ma1
);
4050 map2
= isl_map_from_multi_aff(ma2
);
4051 map
= isl_map_range_product(map1
, map2
);
4052 space
= isl_space_range(isl_map_get_space(map
));
4053 space
= isl_space_domain(isl_space_unwrap(space
));
4054 ge
= isl_map_lex_ge(space
);
4055 map
= isl_map_intersect_range(map
, isl_map_wrap(ge
));
4057 return isl_map_domain(map
);
4061 #define PW isl_pw_multi_aff
4063 #define EL isl_multi_aff
4065 #define EL_IS_ZERO is_empty
4069 #define IS_ZERO is_empty
4072 #undef DEFAULT_IS_ZERO
4073 #define DEFAULT_IS_ZERO 0
4078 #define NO_INVOLVES_DIMS
4079 #define NO_INSERT_DIMS
4083 #include <isl_pw_templ.c>
4088 #define UNION isl_union_pw_multi_aff
4090 #define PART isl_pw_multi_aff
4092 #define PARTS pw_multi_aff
4096 #include <isl_union_templ.c>
4098 /* Given a function "cmp" that returns the set of elements where
4099 * "ma1" is "better" than "ma2", return the intersection of this
4100 * set with "dom1" and "dom2".
4102 static __isl_give isl_set
*shared_and_better(__isl_keep isl_set
*dom1
,
4103 __isl_keep isl_set
*dom2
, __isl_keep isl_multi_aff
*ma1
,
4104 __isl_keep isl_multi_aff
*ma2
,
4105 __isl_give isl_set
*(*cmp
)(__isl_take isl_multi_aff
*ma1
,
4106 __isl_take isl_multi_aff
*ma2
))
4112 common
= isl_set_intersect(isl_set_copy(dom1
), isl_set_copy(dom2
));
4113 is_empty
= isl_set_plain_is_empty(common
);
4114 if (is_empty
>= 0 && is_empty
)
4117 return isl_set_free(common
);
4118 better
= cmp(isl_multi_aff_copy(ma1
), isl_multi_aff_copy(ma2
));
4119 better
= isl_set_intersect(common
, better
);
4124 /* Given a function "cmp" that returns the set of elements where
4125 * "ma1" is "better" than "ma2", return a piecewise multi affine
4126 * expression defined on the union of the definition domains
4127 * of "pma1" and "pma2" that maps to the "best" of "pma1" and
4128 * "pma2" on each cell. If only one of the two input functions
4129 * is defined on a given cell, then it is considered the best.
4131 static __isl_give isl_pw_multi_aff
*pw_multi_aff_union_opt(
4132 __isl_take isl_pw_multi_aff
*pma1
,
4133 __isl_take isl_pw_multi_aff
*pma2
,
4134 __isl_give isl_set
*(*cmp
)(__isl_take isl_multi_aff
*ma1
,
4135 __isl_take isl_multi_aff
*ma2
))
4138 isl_pw_multi_aff
*res
= NULL
;
4140 isl_set
*set
= NULL
;
4145 ctx
= isl_space_get_ctx(pma1
->dim
);
4146 if (!isl_space_is_equal(pma1
->dim
, pma2
->dim
))
4147 isl_die(ctx
, isl_error_invalid
,
4148 "arguments should live in the same space", goto error
);
4150 if (isl_pw_multi_aff_is_empty(pma1
)) {
4151 isl_pw_multi_aff_free(pma1
);
4155 if (isl_pw_multi_aff_is_empty(pma2
)) {
4156 isl_pw_multi_aff_free(pma2
);
4160 n
= 2 * (pma1
->n
+ 1) * (pma2
->n
+ 1);
4161 res
= isl_pw_multi_aff_alloc_size(isl_space_copy(pma1
->dim
), n
);
4163 for (i
= 0; i
< pma1
->n
; ++i
) {
4164 set
= isl_set_copy(pma1
->p
[i
].set
);
4165 for (j
= 0; j
< pma2
->n
; ++j
) {
4169 better
= shared_and_better(pma2
->p
[j
].set
,
4170 pma1
->p
[i
].set
, pma2
->p
[j
].maff
,
4171 pma1
->p
[i
].maff
, cmp
);
4172 is_empty
= isl_set_plain_is_empty(better
);
4173 if (is_empty
< 0 || is_empty
) {
4174 isl_set_free(better
);
4179 set
= isl_set_subtract(set
, isl_set_copy(better
));
4181 res
= isl_pw_multi_aff_add_piece(res
, better
,
4182 isl_multi_aff_copy(pma2
->p
[j
].maff
));
4184 res
= isl_pw_multi_aff_add_piece(res
, set
,
4185 isl_multi_aff_copy(pma1
->p
[i
].maff
));
4188 for (j
= 0; j
< pma2
->n
; ++j
) {
4189 set
= isl_set_copy(pma2
->p
[j
].set
);
4190 for (i
= 0; i
< pma1
->n
; ++i
)
4191 set
= isl_set_subtract(set
,
4192 isl_set_copy(pma1
->p
[i
].set
));
4193 res
= isl_pw_multi_aff_add_piece(res
, set
,
4194 isl_multi_aff_copy(pma2
->p
[j
].maff
));
4197 isl_pw_multi_aff_free(pma1
);
4198 isl_pw_multi_aff_free(pma2
);
4202 isl_pw_multi_aff_free(pma1
);
4203 isl_pw_multi_aff_free(pma2
);
4205 return isl_pw_multi_aff_free(res
);
4208 static __isl_give isl_pw_multi_aff
*pw_multi_aff_union_lexmax(
4209 __isl_take isl_pw_multi_aff
*pma1
,
4210 __isl_take isl_pw_multi_aff
*pma2
)
4212 return pw_multi_aff_union_opt(pma1
, pma2
, &isl_multi_aff_lex_ge_set
);
4215 /* Given two piecewise multi affine expressions, return a piecewise
4216 * multi-affine expression defined on the union of the definition domains
4217 * of the inputs that is equal to the lexicographic maximum of the two
4218 * inputs on each cell. If only one of the two inputs is defined on
4219 * a given cell, then it is considered to be the maximum.
4221 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_union_lexmax(
4222 __isl_take isl_pw_multi_aff
*pma1
,
4223 __isl_take isl_pw_multi_aff
*pma2
)
4225 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
4226 &pw_multi_aff_union_lexmax
);
4229 static __isl_give isl_pw_multi_aff
*pw_multi_aff_union_lexmin(
4230 __isl_take isl_pw_multi_aff
*pma1
,
4231 __isl_take isl_pw_multi_aff
*pma2
)
4233 return pw_multi_aff_union_opt(pma1
, pma2
, &isl_multi_aff_lex_le_set
);
4236 /* Given two piecewise multi affine expressions, return a piecewise
4237 * multi-affine expression defined on the union of the definition domains
4238 * of the inputs that is equal to the lexicographic minimum of the two
4239 * inputs on each cell. If only one of the two inputs is defined on
4240 * a given cell, then it is considered to be the minimum.
4242 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_union_lexmin(
4243 __isl_take isl_pw_multi_aff
*pma1
,
4244 __isl_take isl_pw_multi_aff
*pma2
)
4246 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
4247 &pw_multi_aff_union_lexmin
);
4250 static __isl_give isl_pw_multi_aff
*pw_multi_aff_add(
4251 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4253 return isl_pw_multi_aff_on_shared_domain(pma1
, pma2
,
4254 &isl_multi_aff_add
);
4257 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_add(
4258 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4260 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
4264 static __isl_give isl_pw_multi_aff
*pw_multi_aff_sub(
4265 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4267 return isl_pw_multi_aff_on_shared_domain(pma1
, pma2
,
4268 &isl_multi_aff_sub
);
4271 /* Subtract "pma2" from "pma1" and return the result.
4273 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_sub(
4274 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4276 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
4280 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_union_add(
4281 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4283 return isl_pw_multi_aff_union_add_(pma1
, pma2
);
4286 /* Compute the sum of "upa1" and "upa2" on the union of their domains,
4287 * with the actual sum on the shared domain and
4288 * the defined expression on the symmetric difference of the domains.
4290 __isl_give isl_union_pw_aff
*isl_union_pw_aff_union_add(
4291 __isl_take isl_union_pw_aff
*upa1
, __isl_take isl_union_pw_aff
*upa2
)
4293 return isl_union_pw_aff_union_add_(upa1
, upa2
);
4296 /* Compute the sum of "upma1" and "upma2" on the union of their domains,
4297 * with the actual sum on the shared domain and
4298 * the defined expression on the symmetric difference of the domains.
4300 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_union_add(
4301 __isl_take isl_union_pw_multi_aff
*upma1
,
4302 __isl_take isl_union_pw_multi_aff
*upma2
)
4304 return isl_union_pw_multi_aff_union_add_(upma1
, upma2
);
4307 /* Given two piecewise multi-affine expressions A -> B and C -> D,
4308 * construct a piecewise multi-affine expression [A -> C] -> [B -> D].
4310 static __isl_give isl_pw_multi_aff
*pw_multi_aff_product(
4311 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4315 isl_pw_multi_aff
*res
;
4320 n
= pma1
->n
* pma2
->n
;
4321 space
= isl_space_product(isl_space_copy(pma1
->dim
),
4322 isl_space_copy(pma2
->dim
));
4323 res
= isl_pw_multi_aff_alloc_size(space
, n
);
4325 for (i
= 0; i
< pma1
->n
; ++i
) {
4326 for (j
= 0; j
< pma2
->n
; ++j
) {
4330 domain
= isl_set_product(isl_set_copy(pma1
->p
[i
].set
),
4331 isl_set_copy(pma2
->p
[j
].set
));
4332 ma
= isl_multi_aff_product(
4333 isl_multi_aff_copy(pma1
->p
[i
].maff
),
4334 isl_multi_aff_copy(pma2
->p
[j
].maff
));
4335 res
= isl_pw_multi_aff_add_piece(res
, domain
, ma
);
4339 isl_pw_multi_aff_free(pma1
);
4340 isl_pw_multi_aff_free(pma2
);
4343 isl_pw_multi_aff_free(pma1
);
4344 isl_pw_multi_aff_free(pma2
);
4348 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_product(
4349 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4351 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
4352 &pw_multi_aff_product
);
4355 /* Construct a map mapping the domain of the piecewise multi-affine expression
4356 * to its range, with each dimension in the range equated to the
4357 * corresponding affine expression on its cell.
4359 __isl_give isl_map
*isl_map_from_pw_multi_aff(__isl_take isl_pw_multi_aff
*pma
)
4367 map
= isl_map_empty(isl_pw_multi_aff_get_space(pma
));
4369 for (i
= 0; i
< pma
->n
; ++i
) {
4370 isl_multi_aff
*maff
;
4371 isl_basic_map
*bmap
;
4374 maff
= isl_multi_aff_copy(pma
->p
[i
].maff
);
4375 bmap
= isl_basic_map_from_multi_aff(maff
);
4376 map_i
= isl_map_from_basic_map(bmap
);
4377 map_i
= isl_map_intersect_domain(map_i
,
4378 isl_set_copy(pma
->p
[i
].set
));
4379 map
= isl_map_union_disjoint(map
, map_i
);
4382 isl_pw_multi_aff_free(pma
);
4386 __isl_give isl_set
*isl_set_from_pw_multi_aff(__isl_take isl_pw_multi_aff
*pma
)
4391 if (!isl_space_is_set(pma
->dim
))
4392 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
4393 "isl_pw_multi_aff cannot be converted into an isl_set",
4396 return isl_map_from_pw_multi_aff(pma
);
4398 isl_pw_multi_aff_free(pma
);
4402 /* Given a basic map with a single output dimension that is defined
4403 * in terms of the parameters and input dimensions using an equality,
4404 * extract an isl_aff that expresses the output dimension in terms
4405 * of the parameters and input dimensions.
4406 * Note that this expression may involve integer divisions defined
4407 * in terms of parameters and input dimensions.
4409 * This function shares some similarities with
4410 * isl_basic_map_has_defining_equality and isl_constraint_get_bound.
4412 static __isl_give isl_aff
*extract_isl_aff_from_basic_map(
4413 __isl_take isl_basic_map
*bmap
)
4418 isl_local_space
*ls
;
4423 if (isl_basic_map_dim(bmap
, isl_dim_out
) != 1)
4424 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
4425 "basic map should have a single output dimension",
4427 eq
= isl_basic_map_output_defining_equality(bmap
, 0);
4428 if (eq
>= bmap
->n_eq
)
4429 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
4430 "unable to find suitable equality", goto error
);
4431 ls
= isl_basic_map_get_local_space(bmap
);
4432 aff
= isl_aff_alloc(isl_local_space_domain(ls
));
4435 offset
= isl_basic_map_offset(bmap
, isl_dim_out
);
4436 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4437 if (isl_int_is_neg(bmap
->eq
[eq
][offset
])) {
4438 isl_seq_cpy(aff
->v
->el
+ 1, bmap
->eq
[eq
], offset
);
4439 isl_seq_cpy(aff
->v
->el
+ 1 + offset
, bmap
->eq
[eq
] + offset
+ 1,
4442 isl_seq_neg(aff
->v
->el
+ 1, bmap
->eq
[eq
], offset
);
4443 isl_seq_neg(aff
->v
->el
+ 1 + offset
, bmap
->eq
[eq
] + offset
+ 1,
4446 isl_int_abs(aff
->v
->el
[0], bmap
->eq
[eq
][offset
]);
4447 isl_basic_map_free(bmap
);
4449 aff
= isl_aff_remove_unused_divs(aff
);
4452 isl_basic_map_free(bmap
);
4456 /* Given a basic map where each output dimension is defined
4457 * in terms of the parameters and input dimensions using an equality,
4458 * extract an isl_multi_aff that expresses the output dimensions in terms
4459 * of the parameters and input dimensions.
4461 static __isl_give isl_multi_aff
*extract_isl_multi_aff_from_basic_map(
4462 __isl_take isl_basic_map
*bmap
)
4471 ma
= isl_multi_aff_alloc(isl_basic_map_get_space(bmap
));
4472 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4474 for (i
= 0; i
< n_out
; ++i
) {
4475 isl_basic_map
*bmap_i
;
4478 bmap_i
= isl_basic_map_copy(bmap
);
4479 bmap_i
= isl_basic_map_project_out(bmap_i
, isl_dim_out
,
4480 i
+ 1, n_out
- (1 + i
));
4481 bmap_i
= isl_basic_map_project_out(bmap_i
, isl_dim_out
, 0, i
);
4482 aff
= extract_isl_aff_from_basic_map(bmap_i
);
4483 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
4486 isl_basic_map_free(bmap
);
4491 /* Given a basic set where each set dimension is defined
4492 * in terms of the parameters using an equality,
4493 * extract an isl_multi_aff that expresses the set dimensions in terms
4494 * of the parameters.
4496 __isl_give isl_multi_aff
*isl_multi_aff_from_basic_set_equalities(
4497 __isl_take isl_basic_set
*bset
)
4499 return extract_isl_multi_aff_from_basic_map(bset
);
4502 /* Create an isl_pw_multi_aff that is equivalent to
4503 * isl_map_intersect_domain(isl_map_from_basic_map(bmap), domain).
4504 * The given basic map is such that each output dimension is defined
4505 * in terms of the parameters and input dimensions using an equality.
4507 * Since some applications expect the result of isl_pw_multi_aff_from_map
4508 * to only contain integer affine expressions, we compute the floor
4509 * of the expression before returning.
4511 static __isl_give isl_pw_multi_aff
*plain_pw_multi_aff_from_map(
4512 __isl_take isl_set
*domain
, __isl_take isl_basic_map
*bmap
)
4516 ma
= extract_isl_multi_aff_from_basic_map(bmap
);
4517 ma
= isl_multi_aff_floor(ma
);
4518 return isl_pw_multi_aff_alloc(domain
, ma
);
4521 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4522 * This obviously only works if the input "map" is single-valued.
4523 * If so, we compute the lexicographic minimum of the image in the form
4524 * of an isl_pw_multi_aff. Since the image is unique, it is equal
4525 * to its lexicographic minimum.
4526 * If the input is not single-valued, we produce an error.
4528 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_base(
4529 __isl_take isl_map
*map
)
4533 isl_pw_multi_aff
*pma
;
4535 sv
= isl_map_is_single_valued(map
);
4539 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
4540 "map is not single-valued", goto error
);
4541 map
= isl_map_make_disjoint(map
);
4545 pma
= isl_pw_multi_aff_empty(isl_map_get_space(map
));
4547 for (i
= 0; i
< map
->n
; ++i
) {
4548 isl_pw_multi_aff
*pma_i
;
4549 isl_basic_map
*bmap
;
4550 bmap
= isl_basic_map_copy(map
->p
[i
]);
4551 pma_i
= isl_basic_map_lexmin_pw_multi_aff(bmap
);
4552 pma
= isl_pw_multi_aff_add_disjoint(pma
, pma_i
);
4562 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
4563 * taking into account that the output dimension at position "d"
4564 * can be represented as
4566 * x = floor((e(...) + c1) / m)
4568 * given that constraint "i" is of the form
4570 * e(...) + c1 - m x >= 0
4573 * Let "map" be of the form
4577 * We construct a mapping
4579 * A -> [A -> x = floor(...)]
4581 * apply that to the map, obtaining
4583 * [A -> x = floor(...)] -> B
4585 * and equate dimension "d" to x.
4586 * We then compute a isl_pw_multi_aff representation of the resulting map
4587 * and plug in the mapping above.
4589 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_div(
4590 __isl_take isl_map
*map
, __isl_take isl_basic_map
*hull
, int d
, int i
)
4594 isl_local_space
*ls
;
4602 isl_pw_multi_aff
*pma
;
4605 is_set
= isl_map_is_set(map
);
4607 offset
= isl_basic_map_offset(hull
, isl_dim_out
);
4608 ctx
= isl_map_get_ctx(map
);
4609 space
= isl_space_domain(isl_map_get_space(map
));
4610 n_in
= isl_space_dim(space
, isl_dim_set
);
4611 n
= isl_space_dim(space
, isl_dim_all
);
4613 v
= isl_vec_alloc(ctx
, 1 + 1 + n
);
4615 isl_int_neg(v
->el
[0], hull
->ineq
[i
][offset
+ d
]);
4616 isl_seq_cpy(v
->el
+ 1, hull
->ineq
[i
], 1 + n
);
4618 isl_basic_map_free(hull
);
4620 ls
= isl_local_space_from_space(isl_space_copy(space
));
4621 aff
= isl_aff_alloc_vec(ls
, v
);
4622 aff
= isl_aff_floor(aff
);
4624 isl_space_free(space
);
4625 ma
= isl_multi_aff_from_aff(aff
);
4627 ma
= isl_multi_aff_identity(isl_space_map_from_set(space
));
4628 ma
= isl_multi_aff_range_product(ma
,
4629 isl_multi_aff_from_aff(aff
));
4632 insert
= isl_map_from_multi_aff(isl_multi_aff_copy(ma
));
4633 map
= isl_map_apply_domain(map
, insert
);
4634 map
= isl_map_equate(map
, isl_dim_in
, n_in
, isl_dim_out
, d
);
4635 pma
= isl_pw_multi_aff_from_map(map
);
4636 pma
= isl_pw_multi_aff_pullback_multi_aff(pma
, ma
);
4641 /* Is constraint "c" of the form
4643 * e(...) + c1 - m x >= 0
4647 * -e(...) + c2 + m x >= 0
4649 * where m > 1 and e only depends on parameters and input dimemnsions?
4651 * "offset" is the offset of the output dimensions
4652 * "pos" is the position of output dimension x.
4654 static int is_potential_div_constraint(isl_int
*c
, int offset
, int d
, int total
)
4656 if (isl_int_is_zero(c
[offset
+ d
]))
4658 if (isl_int_is_one(c
[offset
+ d
]))
4660 if (isl_int_is_negone(c
[offset
+ d
]))
4662 if (isl_seq_first_non_zero(c
+ offset
, d
) != -1)
4664 if (isl_seq_first_non_zero(c
+ offset
+ d
+ 1,
4665 total
- (offset
+ d
+ 1)) != -1)
4670 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4672 * As a special case, we first check if there is any pair of constraints,
4673 * shared by all the basic maps in "map" that force a given dimension
4674 * to be equal to the floor of some affine combination of the input dimensions.
4676 * In particular, if we can find two constraints
4678 * e(...) + c1 - m x >= 0 i.e., m x <= e(...) + c1
4682 * -e(...) + c2 + m x >= 0 i.e., m x >= e(...) - c2
4684 * where m > 1 and e only depends on parameters and input dimemnsions,
4687 * c1 + c2 < m i.e., -c2 >= c1 - (m - 1)
4689 * then we know that we can take
4691 * x = floor((e(...) + c1) / m)
4693 * without having to perform any computation.
4695 * Note that we know that
4699 * If c1 + c2 were 0, then we would have detected an equality during
4700 * simplification. If c1 + c2 were negative, then we would have detected
4703 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_check_div(
4704 __isl_take isl_map
*map
)
4710 isl_basic_map
*hull
;
4712 hull
= isl_map_unshifted_simple_hull(isl_map_copy(map
));
4717 dim
= isl_map_dim(map
, isl_dim_out
);
4718 offset
= isl_basic_map_offset(hull
, isl_dim_out
);
4719 total
= 1 + isl_basic_map_total_dim(hull
);
4721 for (d
= 0; d
< dim
; ++d
) {
4722 for (i
= 0; i
< n
; ++i
) {
4723 if (!is_potential_div_constraint(hull
->ineq
[i
],
4726 for (j
= i
+ 1; j
< n
; ++j
) {
4727 if (!isl_seq_is_neg(hull
->ineq
[i
] + 1,
4728 hull
->ineq
[j
] + 1, total
- 1))
4730 isl_int_add(sum
, hull
->ineq
[i
][0],
4732 if (isl_int_abs_lt(sum
,
4733 hull
->ineq
[i
][offset
+ d
]))
4740 if (isl_int_is_pos(hull
->ineq
[j
][offset
+ d
]))
4742 return pw_multi_aff_from_map_div(map
, hull
, d
, j
);
4746 isl_basic_map_free(hull
);
4747 return pw_multi_aff_from_map_base(map
);
4750 isl_basic_map_free(hull
);
4754 /* Given an affine expression
4756 * [A -> B] -> f(A,B)
4758 * construct an isl_multi_aff
4762 * such that dimension "d" in B' is set to "aff" and the remaining
4763 * dimensions are set equal to the corresponding dimensions in B.
4764 * "n_in" is the dimension of the space A.
4765 * "n_out" is the dimension of the space B.
4767 * If "is_set" is set, then the affine expression is of the form
4771 * and we construct an isl_multi_aff
4775 static __isl_give isl_multi_aff
*range_map(__isl_take isl_aff
*aff
, int d
,
4776 unsigned n_in
, unsigned n_out
, int is_set
)
4780 isl_space
*space
, *space2
;
4781 isl_local_space
*ls
;
4783 space
= isl_aff_get_domain_space(aff
);
4784 ls
= isl_local_space_from_space(isl_space_copy(space
));
4785 space2
= isl_space_copy(space
);
4787 space2
= isl_space_range(isl_space_unwrap(space2
));
4788 space
= isl_space_map_from_domain_and_range(space
, space2
);
4789 ma
= isl_multi_aff_alloc(space
);
4790 ma
= isl_multi_aff_set_aff(ma
, d
, aff
);
4792 for (i
= 0; i
< n_out
; ++i
) {
4795 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
4796 isl_dim_set
, n_in
+ i
);
4797 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
4800 isl_local_space_free(ls
);
4805 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
4806 * taking into account that the dimension at position "d" can be written as
4808 * x = m a + f(..) (1)
4810 * where m is equal to "gcd".
4811 * "i" is the index of the equality in "hull" that defines f(..).
4812 * In particular, the equality is of the form
4814 * f(..) - x + m g(existentials) = 0
4818 * -f(..) + x + m g(existentials) = 0
4820 * We basically plug (1) into "map", resulting in a map with "a"
4821 * in the range instead of "x". The corresponding isl_pw_multi_aff
4822 * defining "a" is then plugged back into (1) to obtain a definition fro "x".
4824 * Specifically, given the input map
4828 * We first wrap it into a set
4832 * and define (1) on top of the corresponding space, resulting in "aff".
4833 * We use this to create an isl_multi_aff that maps the output position "d"
4834 * from "a" to "x", leaving all other (intput and output) dimensions unchanged.
4835 * We plug this into the wrapped map, unwrap the result and compute the
4836 * corresponding isl_pw_multi_aff.
4837 * The result is an expression
4845 * so that we can plug that into "aff", after extending the latter to
4851 * If "map" is actually a set, then there is no "A" space, meaning
4852 * that we do not need to perform any wrapping, and that the result
4853 * of the recursive call is of the form
4857 * which is plugged into a mapping of the form
4861 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_stride(
4862 __isl_take isl_map
*map
, __isl_take isl_basic_map
*hull
, int d
, int i
,
4867 isl_local_space
*ls
;
4870 isl_pw_multi_aff
*pma
, *id
;
4876 is_set
= isl_map_is_set(map
);
4878 n_in
= isl_basic_map_dim(hull
, isl_dim_in
);
4879 n_out
= isl_basic_map_dim(hull
, isl_dim_out
);
4880 o_out
= isl_basic_map_offset(hull
, isl_dim_out
);
4885 set
= isl_map_wrap(map
);
4886 space
= isl_space_map_from_set(isl_set_get_space(set
));
4887 ma
= isl_multi_aff_identity(space
);
4888 ls
= isl_local_space_from_space(isl_set_get_space(set
));
4889 aff
= isl_aff_alloc(ls
);
4891 isl_int_set_si(aff
->v
->el
[0], 1);
4892 if (isl_int_is_one(hull
->eq
[i
][o_out
+ d
]))
4893 isl_seq_neg(aff
->v
->el
+ 1, hull
->eq
[i
],
4896 isl_seq_cpy(aff
->v
->el
+ 1, hull
->eq
[i
],
4898 isl_int_set(aff
->v
->el
[1 + o_out
+ d
], gcd
);
4900 ma
= isl_multi_aff_set_aff(ma
, n_in
+ d
, isl_aff_copy(aff
));
4901 set
= isl_set_preimage_multi_aff(set
, ma
);
4903 ma
= range_map(aff
, d
, n_in
, n_out
, is_set
);
4908 map
= isl_set_unwrap(set
);
4909 pma
= isl_pw_multi_aff_from_map(map
);
4912 space
= isl_pw_multi_aff_get_domain_space(pma
);
4913 space
= isl_space_map_from_set(space
);
4914 id
= isl_pw_multi_aff_identity(space
);
4915 pma
= isl_pw_multi_aff_range_product(id
, pma
);
4917 id
= isl_pw_multi_aff_from_multi_aff(ma
);
4918 pma
= isl_pw_multi_aff_pullback_pw_multi_aff(id
, pma
);
4920 isl_basic_map_free(hull
);
4924 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4926 * As a special case, we first check if all output dimensions are uniquely
4927 * defined in terms of the parameters and input dimensions over the entire
4928 * domain. If so, we extract the desired isl_pw_multi_aff directly
4929 * from the affine hull of "map" and its domain.
4931 * Otherwise, we check if any of the output dimensions is "strided".
4932 * That is, we check if can be written as
4936 * with m greater than 1, a some combination of existentiall quantified
4937 * variables and f and expression in the parameters and input dimensions.
4938 * If so, we remove the stride in pw_multi_aff_from_map_stride.
4940 * Otherwise, we continue with pw_multi_aff_from_map_check_div for a further
4943 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_map(__isl_take isl_map
*map
)
4947 isl_basic_map
*hull
;
4957 hull
= isl_map_affine_hull(isl_map_copy(map
));
4958 sv
= isl_basic_map_plain_is_single_valued(hull
);
4960 return plain_pw_multi_aff_from_map(isl_map_domain(map
), hull
);
4962 hull
= isl_basic_map_free(hull
);
4966 n_div
= isl_basic_map_dim(hull
, isl_dim_div
);
4967 o_div
= isl_basic_map_offset(hull
, isl_dim_div
);
4970 isl_basic_map_free(hull
);
4971 return pw_multi_aff_from_map_check_div(map
);
4976 n_out
= isl_basic_map_dim(hull
, isl_dim_out
);
4977 o_out
= isl_basic_map_offset(hull
, isl_dim_out
);
4979 for (i
= 0; i
< n_out
; ++i
) {
4980 for (j
= 0; j
< hull
->n_eq
; ++j
) {
4981 isl_int
*eq
= hull
->eq
[j
];
4982 isl_pw_multi_aff
*res
;
4984 if (!isl_int_is_one(eq
[o_out
+ i
]) &&
4985 !isl_int_is_negone(eq
[o_out
+ i
]))
4987 if (isl_seq_first_non_zero(eq
+ o_out
, i
) != -1)
4989 if (isl_seq_first_non_zero(eq
+ o_out
+ i
+ 1,
4990 n_out
- (i
+ 1)) != -1)
4992 isl_seq_gcd(eq
+ o_div
, n_div
, &gcd
);
4993 if (isl_int_is_zero(gcd
))
4995 if (isl_int_is_one(gcd
))
4998 res
= pw_multi_aff_from_map_stride(map
, hull
,
5006 isl_basic_map_free(hull
);
5007 return pw_multi_aff_from_map_check_div(map
);
5013 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_set(__isl_take isl_set
*set
)
5015 return isl_pw_multi_aff_from_map(set
);
5018 /* Convert "map" into an isl_pw_multi_aff (if possible) and
5021 static int pw_multi_aff_from_map(__isl_take isl_map
*map
, void *user
)
5023 isl_union_pw_multi_aff
**upma
= user
;
5024 isl_pw_multi_aff
*pma
;
5026 pma
= isl_pw_multi_aff_from_map(map
);
5027 *upma
= isl_union_pw_multi_aff_add_pw_multi_aff(*upma
, pma
);
5029 return *upma
? 0 : -1;
5032 /* Create an isl_union_pw_multi_aff with the given isl_aff on a universe
5035 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_aff(
5036 __isl_take isl_aff
*aff
)
5039 isl_pw_multi_aff
*pma
;
5041 ma
= isl_multi_aff_from_aff(aff
);
5042 pma
= isl_pw_multi_aff_from_multi_aff(ma
);
5043 return isl_union_pw_multi_aff_from_pw_multi_aff(pma
);
5046 /* Try and create an isl_union_pw_multi_aff that is equivalent
5047 * to the given isl_union_map.
5048 * The isl_union_map is required to be single-valued in each space.
5049 * Otherwise, an error is produced.
5051 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_union_map(
5052 __isl_take isl_union_map
*umap
)
5055 isl_union_pw_multi_aff
*upma
;
5057 space
= isl_union_map_get_space(umap
);
5058 upma
= isl_union_pw_multi_aff_empty(space
);
5059 if (isl_union_map_foreach_map(umap
, &pw_multi_aff_from_map
, &upma
) < 0)
5060 upma
= isl_union_pw_multi_aff_free(upma
);
5061 isl_union_map_free(umap
);
5066 /* Try and create an isl_union_pw_multi_aff that is equivalent
5067 * to the given isl_union_set.
5068 * The isl_union_set is required to be a singleton in each space.
5069 * Otherwise, an error is produced.
5071 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_union_set(
5072 __isl_take isl_union_set
*uset
)
5074 return isl_union_pw_multi_aff_from_union_map(uset
);
5077 /* Return the piecewise affine expression "set ? 1 : 0".
5079 __isl_give isl_pw_aff
*isl_set_indicator_function(__isl_take isl_set
*set
)
5082 isl_space
*space
= isl_set_get_space(set
);
5083 isl_local_space
*ls
= isl_local_space_from_space(space
);
5084 isl_aff
*zero
= isl_aff_zero_on_domain(isl_local_space_copy(ls
));
5085 isl_aff
*one
= isl_aff_zero_on_domain(ls
);
5087 one
= isl_aff_add_constant_si(one
, 1);
5088 pa
= isl_pw_aff_alloc(isl_set_copy(set
), one
);
5089 set
= isl_set_complement(set
);
5090 pa
= isl_pw_aff_add_disjoint(pa
, isl_pw_aff_alloc(set
, zero
));
5095 /* Plug in "subs" for dimension "type", "pos" of "aff".
5097 * Let i be the dimension to replace and let "subs" be of the form
5101 * and "aff" of the form
5107 * (a f + d g')/(m d)
5109 * where g' is the result of plugging in "subs" in each of the integer
5112 __isl_give isl_aff
*isl_aff_substitute(__isl_take isl_aff
*aff
,
5113 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
5118 aff
= isl_aff_cow(aff
);
5120 return isl_aff_free(aff
);
5122 ctx
= isl_aff_get_ctx(aff
);
5123 if (!isl_space_is_equal(aff
->ls
->dim
, subs
->ls
->dim
))
5124 isl_die(ctx
, isl_error_invalid
,
5125 "spaces don't match", return isl_aff_free(aff
));
5126 if (isl_local_space_dim(subs
->ls
, isl_dim_div
) != 0)
5127 isl_die(ctx
, isl_error_unsupported
,
5128 "cannot handle divs yet", return isl_aff_free(aff
));
5130 aff
->ls
= isl_local_space_substitute(aff
->ls
, type
, pos
, subs
);
5132 return isl_aff_free(aff
);
5134 aff
->v
= isl_vec_cow(aff
->v
);
5136 return isl_aff_free(aff
);
5138 pos
+= isl_local_space_offset(aff
->ls
, type
);
5141 isl_seq_substitute(aff
->v
->el
, pos
, subs
->v
->el
,
5142 aff
->v
->size
, subs
->v
->size
, v
);
5148 /* Plug in "subs" for dimension "type", "pos" in each of the affine
5149 * expressions in "maff".
5151 __isl_give isl_multi_aff
*isl_multi_aff_substitute(
5152 __isl_take isl_multi_aff
*maff
, enum isl_dim_type type
, unsigned pos
,
5153 __isl_keep isl_aff
*subs
)
5157 maff
= isl_multi_aff_cow(maff
);
5159 return isl_multi_aff_free(maff
);
5161 if (type
== isl_dim_in
)
5164 for (i
= 0; i
< maff
->n
; ++i
) {
5165 maff
->p
[i
] = isl_aff_substitute(maff
->p
[i
], type
, pos
, subs
);
5167 return isl_multi_aff_free(maff
);
5173 /* Plug in "subs" for dimension "type", "pos" of "pma".
5175 * pma is of the form
5179 * while subs is of the form
5181 * v' = B_j(v) -> S_j
5183 * Each pair i,j such that C_ij = A_i \cap B_i is non-empty
5184 * has a contribution in the result, in particular
5186 * C_ij(S_j) -> M_i(S_j)
5188 * Note that plugging in S_j in C_ij may also result in an empty set
5189 * and this contribution should simply be discarded.
5191 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_substitute(
5192 __isl_take isl_pw_multi_aff
*pma
, enum isl_dim_type type
, unsigned pos
,
5193 __isl_keep isl_pw_aff
*subs
)
5196 isl_pw_multi_aff
*res
;
5199 return isl_pw_multi_aff_free(pma
);
5201 n
= pma
->n
* subs
->n
;
5202 res
= isl_pw_multi_aff_alloc_size(isl_space_copy(pma
->dim
), n
);
5204 for (i
= 0; i
< pma
->n
; ++i
) {
5205 for (j
= 0; j
< subs
->n
; ++j
) {
5207 isl_multi_aff
*res_ij
;
5210 common
= isl_set_intersect(
5211 isl_set_copy(pma
->p
[i
].set
),
5212 isl_set_copy(subs
->p
[j
].set
));
5213 common
= isl_set_substitute(common
,
5214 type
, pos
, subs
->p
[j
].aff
);
5215 empty
= isl_set_plain_is_empty(common
);
5216 if (empty
< 0 || empty
) {
5217 isl_set_free(common
);
5223 res_ij
= isl_multi_aff_substitute(
5224 isl_multi_aff_copy(pma
->p
[i
].maff
),
5225 type
, pos
, subs
->p
[j
].aff
);
5227 res
= isl_pw_multi_aff_add_piece(res
, common
, res_ij
);
5231 isl_pw_multi_aff_free(pma
);
5234 isl_pw_multi_aff_free(pma
);
5235 isl_pw_multi_aff_free(res
);
5239 /* Compute the preimage of a range of dimensions in the affine expression "src"
5240 * under "ma" and put the result in "dst". The number of dimensions in "src"
5241 * that precede the range is given by "n_before". The number of dimensions
5242 * in the range is given by the number of output dimensions of "ma".
5243 * The number of dimensions that follow the range is given by "n_after".
5244 * If "has_denom" is set (to one),
5245 * then "src" and "dst" have an extra initial denominator.
5246 * "n_div_ma" is the number of existentials in "ma"
5247 * "n_div_bset" is the number of existentials in "src"
5248 * The resulting "dst" (which is assumed to have been allocated by
5249 * the caller) contains coefficients for both sets of existentials,
5250 * first those in "ma" and then those in "src".
5251 * f, c1, c2 and g are temporary objects that have been initialized
5254 * Let src represent the expression
5256 * (a(p) + f_u u + b v + f_w w + c(divs))/d
5258 * and let ma represent the expressions
5260 * v_i = (r_i(p) + s_i(y) + t_i(divs'))/m_i
5262 * We start out with the following expression for dst:
5264 * (a(p) + f_u u + 0 y + f_w w + 0 divs' + c(divs) + f \sum_i b_i v_i)/d
5266 * with the multiplication factor f initially equal to 1
5267 * and f \sum_i b_i v_i kept separately.
5268 * For each x_i that we substitute, we multiply the numerator
5269 * (and denominator) of dst by c_1 = m_i and add the numerator
5270 * of the x_i expression multiplied by c_2 = f b_i,
5271 * after removing the common factors of c_1 and c_2.
5272 * The multiplication factor f also needs to be multiplied by c_1
5273 * for the next x_j, j > i.
5275 void isl_seq_preimage(isl_int
*dst
, isl_int
*src
,
5276 __isl_keep isl_multi_aff
*ma
, int n_before
, int n_after
,
5277 int n_div_ma
, int n_div_bmap
,
5278 isl_int f
, isl_int c1
, isl_int c2
, isl_int g
, int has_denom
)
5281 int n_param
, n_in
, n_out
;
5284 n_param
= isl_multi_aff_dim(ma
, isl_dim_param
);
5285 n_in
= isl_multi_aff_dim(ma
, isl_dim_in
);
5286 n_out
= isl_multi_aff_dim(ma
, isl_dim_out
);
5288 isl_seq_cpy(dst
, src
, has_denom
+ 1 + n_param
+ n_before
);
5289 o_dst
= o_src
= has_denom
+ 1 + n_param
+ n_before
;
5290 isl_seq_clr(dst
+ o_dst
, n_in
);
5293 isl_seq_cpy(dst
+ o_dst
, src
+ o_src
, n_after
);
5296 isl_seq_clr(dst
+ o_dst
, n_div_ma
);
5298 isl_seq_cpy(dst
+ o_dst
, src
+ o_src
, n_div_bmap
);
5300 isl_int_set_si(f
, 1);
5302 for (i
= 0; i
< n_out
; ++i
) {
5303 int offset
= has_denom
+ 1 + n_param
+ n_before
+ i
;
5305 if (isl_int_is_zero(src
[offset
]))
5307 isl_int_set(c1
, ma
->p
[i
]->v
->el
[0]);
5308 isl_int_mul(c2
, f
, src
[offset
]);
5309 isl_int_gcd(g
, c1
, c2
);
5310 isl_int_divexact(c1
, c1
, g
);
5311 isl_int_divexact(c2
, c2
, g
);
5313 isl_int_mul(f
, f
, c1
);
5316 isl_seq_combine(dst
+ o_dst
, c1
, dst
+ o_dst
,
5317 c2
, ma
->p
[i
]->v
->el
+ o_src
, 1 + n_param
);
5318 o_dst
+= 1 + n_param
;
5319 o_src
+= 1 + n_param
;
5320 isl_seq_scale(dst
+ o_dst
, dst
+ o_dst
, c1
, n_before
);
5322 isl_seq_combine(dst
+ o_dst
, c1
, dst
+ o_dst
,
5323 c2
, ma
->p
[i
]->v
->el
+ o_src
, n_in
);
5326 isl_seq_scale(dst
+ o_dst
, dst
+ o_dst
, c1
, n_after
);
5328 isl_seq_combine(dst
+ o_dst
, c1
, dst
+ o_dst
,
5329 c2
, ma
->p
[i
]->v
->el
+ o_src
, n_div_ma
);
5332 isl_seq_scale(dst
+ o_dst
, dst
+ o_dst
, c1
, n_div_bmap
);
5334 isl_int_mul(dst
[0], dst
[0], c1
);
5338 /* Compute the pullback of "aff" by the function represented by "ma".
5339 * In other words, plug in "ma" in "aff". The result is an affine expression
5340 * defined over the domain space of "ma".
5342 * If "aff" is represented by
5344 * (a(p) + b x + c(divs))/d
5346 * and ma is represented by
5348 * x = D(p) + F(y) + G(divs')
5350 * then the result is
5352 * (a(p) + b D(p) + b F(y) + b G(divs') + c(divs))/d
5354 * The divs in the local space of the input are similarly adjusted
5355 * through a call to isl_local_space_preimage_multi_aff.
5357 __isl_give isl_aff
*isl_aff_pullback_multi_aff(__isl_take isl_aff
*aff
,
5358 __isl_take isl_multi_aff
*ma
)
5360 isl_aff
*res
= NULL
;
5361 isl_local_space
*ls
;
5362 int n_div_aff
, n_div_ma
;
5363 isl_int f
, c1
, c2
, g
;
5365 ma
= isl_multi_aff_align_divs(ma
);
5369 n_div_aff
= isl_aff_dim(aff
, isl_dim_div
);
5370 n_div_ma
= ma
->n
? isl_aff_dim(ma
->p
[0], isl_dim_div
) : 0;
5372 ls
= isl_aff_get_domain_local_space(aff
);
5373 ls
= isl_local_space_preimage_multi_aff(ls
, isl_multi_aff_copy(ma
));
5374 res
= isl_aff_alloc(ls
);
5383 isl_seq_preimage(res
->v
->el
, aff
->v
->el
, ma
, 0, 0, n_div_ma
, n_div_aff
,
5392 isl_multi_aff_free(ma
);
5393 res
= isl_aff_normalize(res
);
5397 isl_multi_aff_free(ma
);
5402 /* Compute the pullback of "aff1" by the function represented by "aff2".
5403 * In other words, plug in "aff2" in "aff1". The result is an affine expression
5404 * defined over the domain space of "aff1".
5406 * The domain of "aff1" should match the range of "aff2", which means
5407 * that it should be single-dimensional.
5409 __isl_give isl_aff
*isl_aff_pullback_aff(__isl_take isl_aff
*aff1
,
5410 __isl_take isl_aff
*aff2
)
5414 ma
= isl_multi_aff_from_aff(aff2
);
5415 return isl_aff_pullback_multi_aff(aff1
, ma
);
5418 /* Compute the pullback of "ma1" by the function represented by "ma2".
5419 * In other words, plug in "ma2" in "ma1".
5421 * The parameters of "ma1" and "ma2" are assumed to have been aligned.
5423 static __isl_give isl_multi_aff
*isl_multi_aff_pullback_multi_aff_aligned(
5424 __isl_take isl_multi_aff
*ma1
, __isl_take isl_multi_aff
*ma2
)
5427 isl_space
*space
= NULL
;
5429 ma2
= isl_multi_aff_align_divs(ma2
);
5430 ma1
= isl_multi_aff_cow(ma1
);
5434 space
= isl_space_join(isl_multi_aff_get_space(ma2
),
5435 isl_multi_aff_get_space(ma1
));
5437 for (i
= 0; i
< ma1
->n
; ++i
) {
5438 ma1
->p
[i
] = isl_aff_pullback_multi_aff(ma1
->p
[i
],
5439 isl_multi_aff_copy(ma2
));
5444 ma1
= isl_multi_aff_reset_space(ma1
, space
);
5445 isl_multi_aff_free(ma2
);
5448 isl_space_free(space
);
5449 isl_multi_aff_free(ma2
);
5450 isl_multi_aff_free(ma1
);
5454 /* Compute the pullback of "ma1" by the function represented by "ma2".
5455 * In other words, plug in "ma2" in "ma1".
5457 __isl_give isl_multi_aff
*isl_multi_aff_pullback_multi_aff(
5458 __isl_take isl_multi_aff
*ma1
, __isl_take isl_multi_aff
*ma2
)
5460 return isl_multi_aff_align_params_multi_multi_and(ma1
, ma2
,
5461 &isl_multi_aff_pullback_multi_aff_aligned
);
5464 /* Extend the local space of "dst" to include the divs
5465 * in the local space of "src".
5467 __isl_give isl_aff
*isl_aff_align_divs(__isl_take isl_aff
*dst
,
5468 __isl_keep isl_aff
*src
)
5476 return isl_aff_free(dst
);
5478 ctx
= isl_aff_get_ctx(src
);
5479 if (!isl_space_is_equal(src
->ls
->dim
, dst
->ls
->dim
))
5480 isl_die(ctx
, isl_error_invalid
,
5481 "spaces don't match", goto error
);
5483 if (src
->ls
->div
->n_row
== 0)
5486 exp1
= isl_alloc_array(ctx
, int, src
->ls
->div
->n_row
);
5487 exp2
= isl_alloc_array(ctx
, int, dst
->ls
->div
->n_row
);
5488 if (!exp1
|| (dst
->ls
->div
->n_row
&& !exp2
))
5491 div
= isl_merge_divs(src
->ls
->div
, dst
->ls
->div
, exp1
, exp2
);
5492 dst
= isl_aff_expand_divs(dst
, div
, exp2
);
5500 return isl_aff_free(dst
);
5503 /* Adjust the local spaces of the affine expressions in "maff"
5504 * such that they all have the save divs.
5506 __isl_give isl_multi_aff
*isl_multi_aff_align_divs(
5507 __isl_take isl_multi_aff
*maff
)
5515 maff
= isl_multi_aff_cow(maff
);
5519 for (i
= 1; i
< maff
->n
; ++i
)
5520 maff
->p
[0] = isl_aff_align_divs(maff
->p
[0], maff
->p
[i
]);
5521 for (i
= 1; i
< maff
->n
; ++i
) {
5522 maff
->p
[i
] = isl_aff_align_divs(maff
->p
[i
], maff
->p
[0]);
5524 return isl_multi_aff_free(maff
);
5530 __isl_give isl_aff
*isl_aff_lift(__isl_take isl_aff
*aff
)
5532 aff
= isl_aff_cow(aff
);
5536 aff
->ls
= isl_local_space_lift(aff
->ls
);
5538 return isl_aff_free(aff
);
5543 /* Lift "maff" to a space with extra dimensions such that the result
5544 * has no more existentially quantified variables.
5545 * If "ls" is not NULL, then *ls is assigned the local space that lies
5546 * at the basis of the lifting applied to "maff".
5548 __isl_give isl_multi_aff
*isl_multi_aff_lift(__isl_take isl_multi_aff
*maff
,
5549 __isl_give isl_local_space
**ls
)
5563 isl_space
*space
= isl_multi_aff_get_domain_space(maff
);
5564 *ls
= isl_local_space_from_space(space
);
5566 return isl_multi_aff_free(maff
);
5571 maff
= isl_multi_aff_cow(maff
);
5572 maff
= isl_multi_aff_align_divs(maff
);
5576 n_div
= isl_aff_dim(maff
->p
[0], isl_dim_div
);
5577 space
= isl_multi_aff_get_space(maff
);
5578 space
= isl_space_lift(isl_space_domain(space
), n_div
);
5579 space
= isl_space_extend_domain_with_range(space
,
5580 isl_multi_aff_get_space(maff
));
5582 return isl_multi_aff_free(maff
);
5583 isl_space_free(maff
->space
);
5584 maff
->space
= space
;
5587 *ls
= isl_aff_get_domain_local_space(maff
->p
[0]);
5589 return isl_multi_aff_free(maff
);
5592 for (i
= 0; i
< maff
->n
; ++i
) {
5593 maff
->p
[i
] = isl_aff_lift(maff
->p
[i
]);
5601 isl_local_space_free(*ls
);
5602 return isl_multi_aff_free(maff
);
5606 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma".
5608 __isl_give isl_pw_aff
*isl_pw_multi_aff_get_pw_aff(
5609 __isl_keep isl_pw_multi_aff
*pma
, int pos
)
5619 n_out
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
5620 if (pos
< 0 || pos
>= n_out
)
5621 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
5622 "index out of bounds", return NULL
);
5624 space
= isl_pw_multi_aff_get_space(pma
);
5625 space
= isl_space_drop_dims(space
, isl_dim_out
,
5626 pos
+ 1, n_out
- pos
- 1);
5627 space
= isl_space_drop_dims(space
, isl_dim_out
, 0, pos
);
5629 pa
= isl_pw_aff_alloc_size(space
, pma
->n
);
5630 for (i
= 0; i
< pma
->n
; ++i
) {
5632 aff
= isl_multi_aff_get_aff(pma
->p
[i
].maff
, pos
);
5633 pa
= isl_pw_aff_add_piece(pa
, isl_set_copy(pma
->p
[i
].set
), aff
);
5639 /* Return an isl_pw_multi_aff with the given "set" as domain and
5640 * an unnamed zero-dimensional range.
5642 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_domain(
5643 __isl_take isl_set
*set
)
5648 space
= isl_set_get_space(set
);
5649 space
= isl_space_from_domain(space
);
5650 ma
= isl_multi_aff_zero(space
);
5651 return isl_pw_multi_aff_alloc(set
, ma
);
5654 /* Add an isl_pw_multi_aff with the given "set" as domain and
5655 * an unnamed zero-dimensional range to *user.
5657 static int add_pw_multi_aff_from_domain(__isl_take isl_set
*set
, void *user
)
5659 isl_union_pw_multi_aff
**upma
= user
;
5660 isl_pw_multi_aff
*pma
;
5662 pma
= isl_pw_multi_aff_from_domain(set
);
5663 *upma
= isl_union_pw_multi_aff_add_pw_multi_aff(*upma
, pma
);
5668 /* Return an isl_union_pw_multi_aff with the given "uset" as domain and
5669 * an unnamed zero-dimensional range.
5671 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_domain(
5672 __isl_take isl_union_set
*uset
)
5675 isl_union_pw_multi_aff
*upma
;
5680 space
= isl_union_set_get_space(uset
);
5681 upma
= isl_union_pw_multi_aff_empty(space
);
5683 if (isl_union_set_foreach_set(uset
,
5684 &add_pw_multi_aff_from_domain
, &upma
) < 0)
5687 isl_union_set_free(uset
);
5690 isl_union_set_free(uset
);
5691 isl_union_pw_multi_aff_free(upma
);
5695 /* Convert "pma" to an isl_map and add it to *umap.
5697 static int map_from_pw_multi_aff(__isl_take isl_pw_multi_aff
*pma
, void *user
)
5699 isl_union_map
**umap
= user
;
5702 map
= isl_map_from_pw_multi_aff(pma
);
5703 *umap
= isl_union_map_add_map(*umap
, map
);
5708 /* Construct a union map mapping the domain of the union
5709 * piecewise multi-affine expression to its range, with each dimension
5710 * in the range equated to the corresponding affine expression on its cell.
5712 __isl_give isl_union_map
*isl_union_map_from_union_pw_multi_aff(
5713 __isl_take isl_union_pw_multi_aff
*upma
)
5716 isl_union_map
*umap
;
5721 space
= isl_union_pw_multi_aff_get_space(upma
);
5722 umap
= isl_union_map_empty(space
);
5724 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma
,
5725 &map_from_pw_multi_aff
, &umap
) < 0)
5728 isl_union_pw_multi_aff_free(upma
);
5731 isl_union_pw_multi_aff_free(upma
);
5732 isl_union_map_free(umap
);
5736 /* Local data for bin_entry and the callback "fn".
5738 struct isl_union_pw_multi_aff_bin_data
{
5739 isl_union_pw_multi_aff
*upma2
;
5740 isl_union_pw_multi_aff
*res
;
5741 isl_pw_multi_aff
*pma
;
5742 int (*fn
)(void **entry
, void *user
);
5745 /* Given an isl_pw_multi_aff from upma1, store it in data->pma
5746 * and call data->fn for each isl_pw_multi_aff in data->upma2.
5748 static int bin_entry(void **entry
, void *user
)
5750 struct isl_union_pw_multi_aff_bin_data
*data
= user
;
5751 isl_pw_multi_aff
*pma
= *entry
;
5754 if (isl_hash_table_foreach(data
->upma2
->space
->ctx
, &data
->upma2
->table
,
5755 data
->fn
, data
) < 0)
5761 /* Call "fn" on each pair of isl_pw_multi_affs in "upma1" and "upma2".
5762 * The isl_pw_multi_aff from upma1 is stored in data->pma (where data is
5763 * passed as user field) and the isl_pw_multi_aff from upma2 is available
5764 * as *entry. The callback should adjust data->res if desired.
5766 static __isl_give isl_union_pw_multi_aff
*bin_op(
5767 __isl_take isl_union_pw_multi_aff
*upma1
,
5768 __isl_take isl_union_pw_multi_aff
*upma2
,
5769 int (*fn
)(void **entry
, void *user
))
5772 struct isl_union_pw_multi_aff_bin_data data
= { NULL
, NULL
, NULL
, fn
};
5774 space
= isl_union_pw_multi_aff_get_space(upma2
);
5775 upma1
= isl_union_pw_multi_aff_align_params(upma1
, space
);
5776 space
= isl_union_pw_multi_aff_get_space(upma1
);
5777 upma2
= isl_union_pw_multi_aff_align_params(upma2
, space
);
5779 if (!upma1
|| !upma2
)
5783 data
.res
= isl_union_pw_multi_aff_alloc(isl_space_copy(upma1
->space
),
5785 if (isl_hash_table_foreach(upma1
->space
->ctx
, &upma1
->table
,
5786 &bin_entry
, &data
) < 0)
5789 isl_union_pw_multi_aff_free(upma1
);
5790 isl_union_pw_multi_aff_free(upma2
);
5793 isl_union_pw_multi_aff_free(upma1
);
5794 isl_union_pw_multi_aff_free(upma2
);
5795 isl_union_pw_multi_aff_free(data
.res
);
5799 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
5800 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
5802 static __isl_give isl_pw_multi_aff
*pw_multi_aff_range_product(
5803 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
5807 space
= isl_space_range_product(isl_pw_multi_aff_get_space(pma1
),
5808 isl_pw_multi_aff_get_space(pma2
));
5809 return isl_pw_multi_aff_on_shared_domain_in(pma1
, pma2
, space
,
5810 &isl_multi_aff_range_product
);
5813 /* Given two isl_pw_multi_affs A -> B and C -> D,
5814 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
5816 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_range_product(
5817 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
5819 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
5820 &pw_multi_aff_range_product
);
5823 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
5824 * construct an isl_pw_multi_aff (A * C) -> (B, D).
5826 static __isl_give isl_pw_multi_aff
*pw_multi_aff_flat_range_product(
5827 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
5831 space
= isl_space_range_product(isl_pw_multi_aff_get_space(pma1
),
5832 isl_pw_multi_aff_get_space(pma2
));
5833 space
= isl_space_flatten_range(space
);
5834 return isl_pw_multi_aff_on_shared_domain_in(pma1
, pma2
, space
,
5835 &isl_multi_aff_flat_range_product
);
5838 /* Given two isl_pw_multi_affs A -> B and C -> D,
5839 * construct an isl_pw_multi_aff (A * C) -> (B, D).
5841 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_flat_range_product(
5842 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
5844 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
5845 &pw_multi_aff_flat_range_product
);
5848 /* If data->pma and *entry have the same domain space, then compute
5849 * their flat range product and the result to data->res.
5851 static int flat_range_product_entry(void **entry
, void *user
)
5853 struct isl_union_pw_multi_aff_bin_data
*data
= user
;
5854 isl_pw_multi_aff
*pma2
= *entry
;
5856 if (!isl_space_tuple_is_equal(data
->pma
->dim
, isl_dim_in
,
5857 pma2
->dim
, isl_dim_in
))
5860 pma2
= isl_pw_multi_aff_flat_range_product(
5861 isl_pw_multi_aff_copy(data
->pma
),
5862 isl_pw_multi_aff_copy(pma2
));
5864 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma2
);
5869 /* Given two isl_union_pw_multi_affs A -> B and C -> D,
5870 * construct an isl_union_pw_multi_aff (A * C) -> (B, D).
5872 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_flat_range_product(
5873 __isl_take isl_union_pw_multi_aff
*upma1
,
5874 __isl_take isl_union_pw_multi_aff
*upma2
)
5876 return bin_op(upma1
, upma2
, &flat_range_product_entry
);
5879 /* Replace the affine expressions at position "pos" in "pma" by "pa".
5880 * The parameters are assumed to have been aligned.
5882 * The implementation essentially performs an isl_pw_*_on_shared_domain,
5883 * except that it works on two different isl_pw_* types.
5885 static __isl_give isl_pw_multi_aff
*pw_multi_aff_set_pw_aff(
5886 __isl_take isl_pw_multi_aff
*pma
, unsigned pos
,
5887 __isl_take isl_pw_aff
*pa
)
5890 isl_pw_multi_aff
*res
= NULL
;
5895 if (!isl_space_tuple_is_equal(pma
->dim
, isl_dim_in
,
5896 pa
->dim
, isl_dim_in
))
5897 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
5898 "domains don't match", goto error
);
5899 if (pos
>= isl_pw_multi_aff_dim(pma
, isl_dim_out
))
5900 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
5901 "index out of bounds", goto error
);
5904 res
= isl_pw_multi_aff_alloc_size(isl_pw_multi_aff_get_space(pma
), n
);
5906 for (i
= 0; i
< pma
->n
; ++i
) {
5907 for (j
= 0; j
< pa
->n
; ++j
) {
5909 isl_multi_aff
*res_ij
;
5912 common
= isl_set_intersect(isl_set_copy(pma
->p
[i
].set
),
5913 isl_set_copy(pa
->p
[j
].set
));
5914 empty
= isl_set_plain_is_empty(common
);
5915 if (empty
< 0 || empty
) {
5916 isl_set_free(common
);
5922 res_ij
= isl_multi_aff_set_aff(
5923 isl_multi_aff_copy(pma
->p
[i
].maff
), pos
,
5924 isl_aff_copy(pa
->p
[j
].aff
));
5925 res_ij
= isl_multi_aff_gist(res_ij
,
5926 isl_set_copy(common
));
5928 res
= isl_pw_multi_aff_add_piece(res
, common
, res_ij
);
5932 isl_pw_multi_aff_free(pma
);
5933 isl_pw_aff_free(pa
);
5936 isl_pw_multi_aff_free(pma
);
5937 isl_pw_aff_free(pa
);
5938 return isl_pw_multi_aff_free(res
);
5941 /* Replace the affine expressions at position "pos" in "pma" by "pa".
5943 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_set_pw_aff(
5944 __isl_take isl_pw_multi_aff
*pma
, unsigned pos
,
5945 __isl_take isl_pw_aff
*pa
)
5949 if (isl_space_match(pma
->dim
, isl_dim_param
, pa
->dim
, isl_dim_param
))
5950 return pw_multi_aff_set_pw_aff(pma
, pos
, pa
);
5951 if (!isl_space_has_named_params(pma
->dim
) ||
5952 !isl_space_has_named_params(pa
->dim
))
5953 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
5954 "unaligned unnamed parameters", goto error
);
5955 pma
= isl_pw_multi_aff_align_params(pma
, isl_pw_aff_get_space(pa
));
5956 pa
= isl_pw_aff_align_params(pa
, isl_pw_multi_aff_get_space(pma
));
5957 return pw_multi_aff_set_pw_aff(pma
, pos
, pa
);
5959 isl_pw_multi_aff_free(pma
);
5960 isl_pw_aff_free(pa
);
5964 /* Do the parameters of "pa" match those of "space"?
5966 int isl_pw_aff_matching_params(__isl_keep isl_pw_aff
*pa
,
5967 __isl_keep isl_space
*space
)
5969 isl_space
*pa_space
;
5975 pa_space
= isl_pw_aff_get_space(pa
);
5977 match
= isl_space_match(space
, isl_dim_param
, pa_space
, isl_dim_param
);
5979 isl_space_free(pa_space
);
5983 /* Check that the domain space of "pa" matches "space".
5985 * Return 0 on success and -1 on error.
5987 int isl_pw_aff_check_match_domain_space(__isl_keep isl_pw_aff
*pa
,
5988 __isl_keep isl_space
*space
)
5990 isl_space
*pa_space
;
5996 pa_space
= isl_pw_aff_get_space(pa
);
5998 match
= isl_space_match(space
, isl_dim_param
, pa_space
, isl_dim_param
);
6002 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
6003 "parameters don't match", goto error
);
6004 match
= isl_space_tuple_is_equal(space
, isl_dim_in
,
6005 pa_space
, isl_dim_in
);
6009 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
6010 "domains don't match", goto error
);
6011 isl_space_free(pa_space
);
6014 isl_space_free(pa_space
);
6023 #include <isl_multi_templ.c>
6024 #include <isl_multi_apply_set.c>
6025 #include <isl_multi_gist.c>
6026 #include <isl_multi_intersect.c>
6028 /* Scale the elements of "pma" by the corresponding elements of "mv".
6030 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_scale_multi_val(
6031 __isl_take isl_pw_multi_aff
*pma
, __isl_take isl_multi_val
*mv
)
6035 pma
= isl_pw_multi_aff_cow(pma
);
6038 if (!isl_space_tuple_is_equal(pma
->dim
, isl_dim_out
,
6039 mv
->space
, isl_dim_set
))
6040 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
6041 "spaces don't match", goto error
);
6042 if (!isl_space_match(pma
->dim
, isl_dim_param
,
6043 mv
->space
, isl_dim_param
)) {
6044 pma
= isl_pw_multi_aff_align_params(pma
,
6045 isl_multi_val_get_space(mv
));
6046 mv
= isl_multi_val_align_params(mv
,
6047 isl_pw_multi_aff_get_space(pma
));
6052 for (i
= 0; i
< pma
->n
; ++i
) {
6053 pma
->p
[i
].maff
= isl_multi_aff_scale_multi_val(pma
->p
[i
].maff
,
6054 isl_multi_val_copy(mv
));
6055 if (!pma
->p
[i
].maff
)
6059 isl_multi_val_free(mv
);
6062 isl_multi_val_free(mv
);
6063 isl_pw_multi_aff_free(pma
);
6067 /* Internal data structure for isl_union_pw_multi_aff_scale_multi_val.
6068 * mv contains the mv argument.
6069 * res collects the results.
6071 struct isl_union_pw_multi_aff_scale_multi_val_data
{
6073 isl_union_pw_multi_aff
*res
;
6076 /* This function is called for each entry of an isl_union_pw_multi_aff.
6077 * If the space of the entry matches that of data->mv,
6078 * then apply isl_pw_multi_aff_scale_multi_val and add the result
6081 static int union_pw_multi_aff_scale_multi_val_entry(void **entry
, void *user
)
6083 struct isl_union_pw_multi_aff_scale_multi_val_data
*data
= user
;
6084 isl_pw_multi_aff
*pma
= *entry
;
6088 if (!isl_space_tuple_is_equal(pma
->dim
, isl_dim_out
,
6089 data
->mv
->space
, isl_dim_set
))
6092 pma
= isl_pw_multi_aff_copy(pma
);
6093 pma
= isl_pw_multi_aff_scale_multi_val(pma
,
6094 isl_multi_val_copy(data
->mv
));
6095 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma
);
6102 /* Scale the elements of "upma" by the corresponding elements of "mv",
6103 * for those entries that match the space of "mv".
6105 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_scale_multi_val(
6106 __isl_take isl_union_pw_multi_aff
*upma
, __isl_take isl_multi_val
*mv
)
6108 struct isl_union_pw_multi_aff_scale_multi_val_data data
;
6110 upma
= isl_union_pw_multi_aff_align_params(upma
,
6111 isl_multi_val_get_space(mv
));
6112 mv
= isl_multi_val_align_params(mv
,
6113 isl_union_pw_multi_aff_get_space(upma
));
6118 data
.res
= isl_union_pw_multi_aff_alloc(isl_space_copy(upma
->space
),
6120 if (isl_hash_table_foreach(upma
->space
->ctx
, &upma
->table
,
6121 &union_pw_multi_aff_scale_multi_val_entry
, &data
) < 0)
6124 isl_multi_val_free(mv
);
6125 isl_union_pw_multi_aff_free(upma
);
6128 isl_multi_val_free(mv
);
6129 isl_union_pw_multi_aff_free(upma
);
6133 /* Construct and return a piecewise multi affine expression
6134 * in the given space with value zero in each of the output dimensions and
6135 * a universe domain.
6137 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_zero(__isl_take isl_space
*space
)
6139 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_zero(space
));
6142 /* Construct and return a piecewise multi affine expression
6143 * that is equal to the given piecewise affine expression.
6145 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_pw_aff(
6146 __isl_take isl_pw_aff
*pa
)
6150 isl_pw_multi_aff
*pma
;
6155 space
= isl_pw_aff_get_space(pa
);
6156 pma
= isl_pw_multi_aff_alloc_size(space
, pa
->n
);
6158 for (i
= 0; i
< pa
->n
; ++i
) {
6162 set
= isl_set_copy(pa
->p
[i
].set
);
6163 ma
= isl_multi_aff_from_aff(isl_aff_copy(pa
->p
[i
].aff
));
6164 pma
= isl_pw_multi_aff_add_piece(pma
, set
, ma
);
6167 isl_pw_aff_free(pa
);
6171 /* Construct a set or map mapping the shared (parameter) domain
6172 * of the piecewise affine expressions to the range of "mpa"
6173 * with each dimension in the range equated to the
6174 * corresponding piecewise affine expression.
6176 static __isl_give isl_map
*map_from_multi_pw_aff(
6177 __isl_take isl_multi_pw_aff
*mpa
)
6186 if (isl_space_dim(mpa
->space
, isl_dim_out
) != mpa
->n
)
6187 isl_die(isl_multi_pw_aff_get_ctx(mpa
), isl_error_internal
,
6188 "invalid space", goto error
);
6190 space
= isl_multi_pw_aff_get_domain_space(mpa
);
6191 map
= isl_map_universe(isl_space_from_domain(space
));
6193 for (i
= 0; i
< mpa
->n
; ++i
) {
6197 pa
= isl_pw_aff_copy(mpa
->p
[i
]);
6198 map_i
= map_from_pw_aff(pa
);
6200 map
= isl_map_flat_range_product(map
, map_i
);
6203 map
= isl_map_reset_space(map
, isl_multi_pw_aff_get_space(mpa
));
6205 isl_multi_pw_aff_free(mpa
);
6208 isl_multi_pw_aff_free(mpa
);
6212 /* Construct a map mapping the shared domain
6213 * of the piecewise affine expressions to the range of "mpa"
6214 * with each dimension in the range equated to the
6215 * corresponding piecewise affine expression.
6217 __isl_give isl_map
*isl_map_from_multi_pw_aff(__isl_take isl_multi_pw_aff
*mpa
)
6221 if (isl_space_is_set(mpa
->space
))
6222 isl_die(isl_multi_pw_aff_get_ctx(mpa
), isl_error_internal
,
6223 "space of input is not a map", goto error
);
6225 return map_from_multi_pw_aff(mpa
);
6227 isl_multi_pw_aff_free(mpa
);
6231 /* Construct a set mapping the shared parameter domain
6232 * of the piecewise affine expressions to the space of "mpa"
6233 * with each dimension in the range equated to the
6234 * corresponding piecewise affine expression.
6236 __isl_give isl_set
*isl_set_from_multi_pw_aff(__isl_take isl_multi_pw_aff
*mpa
)
6240 if (!isl_space_is_set(mpa
->space
))
6241 isl_die(isl_multi_pw_aff_get_ctx(mpa
), isl_error_internal
,
6242 "space of input is not a set", goto error
);
6244 return map_from_multi_pw_aff(mpa
);
6246 isl_multi_pw_aff_free(mpa
);
6250 /* Construct and return a piecewise multi affine expression
6251 * that is equal to the given multi piecewise affine expression
6252 * on the shared domain of the piecewise affine expressions.
6254 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_multi_pw_aff(
6255 __isl_take isl_multi_pw_aff
*mpa
)
6260 isl_pw_multi_aff
*pma
;
6265 space
= isl_multi_pw_aff_get_space(mpa
);
6268 isl_multi_pw_aff_free(mpa
);
6269 return isl_pw_multi_aff_zero(space
);
6272 pa
= isl_multi_pw_aff_get_pw_aff(mpa
, 0);
6273 pma
= isl_pw_multi_aff_from_pw_aff(pa
);
6275 for (i
= 1; i
< mpa
->n
; ++i
) {
6276 isl_pw_multi_aff
*pma_i
;
6278 pa
= isl_multi_pw_aff_get_pw_aff(mpa
, i
);
6279 pma_i
= isl_pw_multi_aff_from_pw_aff(pa
);
6280 pma
= isl_pw_multi_aff_range_product(pma
, pma_i
);
6283 pma
= isl_pw_multi_aff_reset_space(pma
, space
);
6285 isl_multi_pw_aff_free(mpa
);
6289 /* Construct and return a multi piecewise affine expression
6290 * that is equal to the given multi affine expression.
6292 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_from_multi_aff(
6293 __isl_take isl_multi_aff
*ma
)
6296 isl_multi_pw_aff
*mpa
;
6301 n
= isl_multi_aff_dim(ma
, isl_dim_out
);
6302 mpa
= isl_multi_pw_aff_alloc(isl_multi_aff_get_space(ma
));
6304 for (i
= 0; i
< n
; ++i
) {
6307 pa
= isl_pw_aff_from_aff(isl_multi_aff_get_aff(ma
, i
));
6308 mpa
= isl_multi_pw_aff_set_pw_aff(mpa
, i
, pa
);
6311 isl_multi_aff_free(ma
);
6315 /* Construct and return a multi piecewise affine expression
6316 * that is equal to the given piecewise multi affine expression.
6318 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_from_pw_multi_aff(
6319 __isl_take isl_pw_multi_aff
*pma
)
6323 isl_multi_pw_aff
*mpa
;
6328 n
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
6329 space
= isl_pw_multi_aff_get_space(pma
);
6330 mpa
= isl_multi_pw_aff_alloc(space
);
6332 for (i
= 0; i
< n
; ++i
) {
6335 pa
= isl_pw_multi_aff_get_pw_aff(pma
, i
);
6336 mpa
= isl_multi_pw_aff_set_pw_aff(mpa
, i
, pa
);
6339 isl_pw_multi_aff_free(pma
);
6343 /* Do "pa1" and "pa2" represent the same function?
6345 * We first check if they are obviously equal.
6346 * If not, we convert them to maps and check if those are equal.
6348 int isl_pw_aff_is_equal(__isl_keep isl_pw_aff
*pa1
, __isl_keep isl_pw_aff
*pa2
)
6351 isl_map
*map1
, *map2
;
6356 equal
= isl_pw_aff_plain_is_equal(pa1
, pa2
);
6357 if (equal
< 0 || equal
)
6360 map1
= map_from_pw_aff(isl_pw_aff_copy(pa1
));
6361 map2
= map_from_pw_aff(isl_pw_aff_copy(pa2
));
6362 equal
= isl_map_is_equal(map1
, map2
);
6369 /* Do "mpa1" and "mpa2" represent the same function?
6371 * Note that we cannot convert the entire isl_multi_pw_aff
6372 * to a map because the domains of the piecewise affine expressions
6373 * may not be the same.
6375 int isl_multi_pw_aff_is_equal(__isl_keep isl_multi_pw_aff
*mpa1
,
6376 __isl_keep isl_multi_pw_aff
*mpa2
)
6384 if (!isl_space_match(mpa1
->space
, isl_dim_param
,
6385 mpa2
->space
, isl_dim_param
)) {
6386 if (!isl_space_has_named_params(mpa1
->space
))
6388 if (!isl_space_has_named_params(mpa2
->space
))
6390 mpa1
= isl_multi_pw_aff_copy(mpa1
);
6391 mpa2
= isl_multi_pw_aff_copy(mpa2
);
6392 mpa1
= isl_multi_pw_aff_align_params(mpa1
,
6393 isl_multi_pw_aff_get_space(mpa2
));
6394 mpa2
= isl_multi_pw_aff_align_params(mpa2
,
6395 isl_multi_pw_aff_get_space(mpa1
));
6396 equal
= isl_multi_pw_aff_is_equal(mpa1
, mpa2
);
6397 isl_multi_pw_aff_free(mpa1
);
6398 isl_multi_pw_aff_free(mpa2
);
6402 equal
= isl_space_is_equal(mpa1
->space
, mpa2
->space
);
6403 if (equal
< 0 || !equal
)
6406 for (i
= 0; i
< mpa1
->n
; ++i
) {
6407 equal
= isl_pw_aff_is_equal(mpa1
->p
[i
], mpa2
->p
[i
]);
6408 if (equal
< 0 || !equal
)
6415 /* Coalesce the elements of "mpa".
6417 * Note that such coalescing does not change the meaning of "mpa"
6418 * so there is no need to cow. We do need to be careful not to
6419 * destroy any other copies of "mpa" in case of failure.
6421 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_coalesce(
6422 __isl_take isl_multi_pw_aff
*mpa
)
6429 for (i
= 0; i
< mpa
->n
; ++i
) {
6430 isl_pw_aff
*pa
= isl_pw_aff_copy(mpa
->p
[i
]);
6431 pa
= isl_pw_aff_coalesce(pa
);
6433 return isl_multi_pw_aff_free(mpa
);
6434 isl_pw_aff_free(mpa
->p
[i
]);
6441 /* Compute the pullback of "mpa" by the function represented by "ma".
6442 * In other words, plug in "ma" in "mpa".
6444 * The parameters of "mpa" and "ma" are assumed to have been aligned.
6446 static __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_pullback_multi_aff_aligned(
6447 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_multi_aff
*ma
)
6450 isl_space
*space
= NULL
;
6452 mpa
= isl_multi_pw_aff_cow(mpa
);
6456 space
= isl_space_join(isl_multi_aff_get_space(ma
),
6457 isl_multi_pw_aff_get_space(mpa
));
6461 for (i
= 0; i
< mpa
->n
; ++i
) {
6462 mpa
->p
[i
] = isl_pw_aff_pullback_multi_aff(mpa
->p
[i
],
6463 isl_multi_aff_copy(ma
));
6468 isl_multi_aff_free(ma
);
6469 isl_space_free(mpa
->space
);
6473 isl_space_free(space
);
6474 isl_multi_pw_aff_free(mpa
);
6475 isl_multi_aff_free(ma
);
6479 /* Compute the pullback of "mpa" by the function represented by "ma".
6480 * In other words, plug in "ma" in "mpa".
6482 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_pullback_multi_aff(
6483 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_multi_aff
*ma
)
6487 if (isl_space_match(mpa
->space
, isl_dim_param
,
6488 ma
->space
, isl_dim_param
))
6489 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa
, ma
);
6490 mpa
= isl_multi_pw_aff_align_params(mpa
, isl_multi_aff_get_space(ma
));
6491 ma
= isl_multi_aff_align_params(ma
, isl_multi_pw_aff_get_space(mpa
));
6492 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa
, ma
);
6494 isl_multi_pw_aff_free(mpa
);
6495 isl_multi_aff_free(ma
);
6499 /* Compute the pullback of "mpa" by the function represented by "pma".
6500 * In other words, plug in "pma" in "mpa".
6502 * The parameters of "mpa" and "mpa" are assumed to have been aligned.
6504 static __isl_give isl_multi_pw_aff
*
6505 isl_multi_pw_aff_pullback_pw_multi_aff_aligned(
6506 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_pw_multi_aff
*pma
)
6509 isl_space
*space
= NULL
;
6511 mpa
= isl_multi_pw_aff_cow(mpa
);
6515 space
= isl_space_join(isl_pw_multi_aff_get_space(pma
),
6516 isl_multi_pw_aff_get_space(mpa
));
6518 for (i
= 0; i
< mpa
->n
; ++i
) {
6519 mpa
->p
[i
] = isl_pw_aff_pullback_pw_multi_aff_aligned(mpa
->p
[i
],
6520 isl_pw_multi_aff_copy(pma
));
6525 isl_pw_multi_aff_free(pma
);
6526 isl_space_free(mpa
->space
);
6530 isl_space_free(space
);
6531 isl_multi_pw_aff_free(mpa
);
6532 isl_pw_multi_aff_free(pma
);
6536 /* Compute the pullback of "mpa" by the function represented by "pma".
6537 * In other words, plug in "pma" in "mpa".
6539 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_pullback_pw_multi_aff(
6540 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_pw_multi_aff
*pma
)
6544 if (isl_space_match(mpa
->space
, isl_dim_param
, pma
->dim
, isl_dim_param
))
6545 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa
, pma
);
6546 mpa
= isl_multi_pw_aff_align_params(mpa
,
6547 isl_pw_multi_aff_get_space(pma
));
6548 pma
= isl_pw_multi_aff_align_params(pma
,
6549 isl_multi_pw_aff_get_space(mpa
));
6550 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa
, pma
);
6552 isl_multi_pw_aff_free(mpa
);
6553 isl_pw_multi_aff_free(pma
);
6557 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
6558 * with the domain of "aff". The domain of the result is the same
6560 * "mpa" and "aff" are assumed to have been aligned.
6562 * We first extract the parametric constant from "aff", defined
6563 * over the correct domain.
6564 * Then we add the appropriate combinations of the members of "mpa".
6565 * Finally, we add the integer divisions through recursive calls.
6567 static __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_aff_aligned(
6568 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_aff
*aff
)
6576 n_in
= isl_aff_dim(aff
, isl_dim_in
);
6577 n_div
= isl_aff_dim(aff
, isl_dim_div
);
6579 space
= isl_space_domain(isl_multi_pw_aff_get_space(mpa
));
6580 tmp
= isl_aff_copy(aff
);
6581 tmp
= isl_aff_drop_dims(tmp
, isl_dim_div
, 0, n_div
);
6582 tmp
= isl_aff_drop_dims(tmp
, isl_dim_in
, 0, n_in
);
6583 tmp
= isl_aff_add_dims(tmp
, isl_dim_in
,
6584 isl_space_dim(space
, isl_dim_set
));
6585 tmp
= isl_aff_reset_domain_space(tmp
, space
);
6586 pa
= isl_pw_aff_from_aff(tmp
);
6588 for (i
= 0; i
< n_in
; ++i
) {
6591 if (!isl_aff_involves_dims(aff
, isl_dim_in
, i
, 1))
6593 v
= isl_aff_get_coefficient_val(aff
, isl_dim_in
, i
);
6594 pa_i
= isl_multi_pw_aff_get_pw_aff(mpa
, i
);
6595 pa_i
= isl_pw_aff_scale_val(pa_i
, v
);
6596 pa
= isl_pw_aff_add(pa
, pa_i
);
6599 for (i
= 0; i
< n_div
; ++i
) {
6603 if (!isl_aff_involves_dims(aff
, isl_dim_div
, i
, 1))
6605 div
= isl_aff_get_div(aff
, i
);
6606 pa_i
= isl_multi_pw_aff_apply_aff_aligned(
6607 isl_multi_pw_aff_copy(mpa
), div
);
6608 pa_i
= isl_pw_aff_floor(pa_i
);
6609 v
= isl_aff_get_coefficient_val(aff
, isl_dim_div
, i
);
6610 pa_i
= isl_pw_aff_scale_val(pa_i
, v
);
6611 pa
= isl_pw_aff_add(pa
, pa_i
);
6614 isl_multi_pw_aff_free(mpa
);
6620 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
6621 * with the domain of "aff". The domain of the result is the same
6624 __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_aff(
6625 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_aff
*aff
)
6629 if (isl_space_match(aff
->ls
->dim
, isl_dim_param
,
6630 mpa
->space
, isl_dim_param
))
6631 return isl_multi_pw_aff_apply_aff_aligned(mpa
, aff
);
6633 aff
= isl_aff_align_params(aff
, isl_multi_pw_aff_get_space(mpa
));
6634 mpa
= isl_multi_pw_aff_align_params(mpa
, isl_aff_get_space(aff
));
6636 return isl_multi_pw_aff_apply_aff_aligned(mpa
, aff
);
6639 isl_multi_pw_aff_free(mpa
);
6643 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
6644 * with the domain of "pa". The domain of the result is the same
6646 * "mpa" and "pa" are assumed to have been aligned.
6648 * We consider each piece in turn. Note that the domains of the
6649 * pieces are assumed to be disjoint and they remain disjoint
6650 * after taking the preimage (over the same function).
6652 static __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_pw_aff_aligned(
6653 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_pw_aff
*pa
)
6662 space
= isl_space_join(isl_multi_pw_aff_get_space(mpa
),
6663 isl_pw_aff_get_space(pa
));
6664 res
= isl_pw_aff_empty(space
);
6666 for (i
= 0; i
< pa
->n
; ++i
) {
6670 pa_i
= isl_multi_pw_aff_apply_aff_aligned(
6671 isl_multi_pw_aff_copy(mpa
),
6672 isl_aff_copy(pa
->p
[i
].aff
));
6673 domain
= isl_set_copy(pa
->p
[i
].set
);
6674 domain
= isl_set_preimage_multi_pw_aff(domain
,
6675 isl_multi_pw_aff_copy(mpa
));
6676 pa_i
= isl_pw_aff_intersect_domain(pa_i
, domain
);
6677 res
= isl_pw_aff_add_disjoint(res
, pa_i
);
6680 isl_pw_aff_free(pa
);
6681 isl_multi_pw_aff_free(mpa
);
6684 isl_pw_aff_free(pa
);
6685 isl_multi_pw_aff_free(mpa
);
6689 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
6690 * with the domain of "pa". The domain of the result is the same
6693 __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_pw_aff(
6694 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_pw_aff
*pa
)
6698 if (isl_space_match(pa
->dim
, isl_dim_param
, mpa
->space
, isl_dim_param
))
6699 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa
, pa
);
6701 pa
= isl_pw_aff_align_params(pa
, isl_multi_pw_aff_get_space(mpa
));
6702 mpa
= isl_multi_pw_aff_align_params(mpa
, isl_pw_aff_get_space(pa
));
6704 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa
, pa
);
6706 isl_pw_aff_free(pa
);
6707 isl_multi_pw_aff_free(mpa
);
6711 /* Compute the pullback of "pa" by the function represented by "mpa".
6712 * In other words, plug in "mpa" in "pa".
6713 * "pa" and "mpa" are assumed to have been aligned.
6715 * The pullback is computed by applying "pa" to "mpa".
6717 static __isl_give isl_pw_aff
*isl_pw_aff_pullback_multi_pw_aff_aligned(
6718 __isl_take isl_pw_aff
*pa
, __isl_take isl_multi_pw_aff
*mpa
)
6720 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa
, pa
);
6723 /* Compute the pullback of "pa" by the function represented by "mpa".
6724 * In other words, plug in "mpa" in "pa".
6726 * The pullback is computed by applying "pa" to "mpa".
6728 __isl_give isl_pw_aff
*isl_pw_aff_pullback_multi_pw_aff(
6729 __isl_take isl_pw_aff
*pa
, __isl_take isl_multi_pw_aff
*mpa
)
6731 return isl_multi_pw_aff_apply_pw_aff(mpa
, pa
);
6734 /* Compute the pullback of "mpa1" by the function represented by "mpa2".
6735 * In other words, plug in "mpa2" in "mpa1".
6737 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
6739 * We pullback each member of "mpa1" in turn.
6741 static __isl_give isl_multi_pw_aff
*
6742 isl_multi_pw_aff_pullback_multi_pw_aff_aligned(
6743 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
)
6746 isl_space
*space
= NULL
;
6748 mpa1
= isl_multi_pw_aff_cow(mpa1
);
6752 space
= isl_space_join(isl_multi_pw_aff_get_space(mpa2
),
6753 isl_multi_pw_aff_get_space(mpa1
));
6755 for (i
= 0; i
< mpa1
->n
; ++i
) {
6756 mpa1
->p
[i
] = isl_pw_aff_pullback_multi_pw_aff_aligned(
6757 mpa1
->p
[i
], isl_multi_pw_aff_copy(mpa2
));
6762 mpa1
= isl_multi_pw_aff_reset_space(mpa1
, space
);
6764 isl_multi_pw_aff_free(mpa2
);
6767 isl_space_free(space
);
6768 isl_multi_pw_aff_free(mpa1
);
6769 isl_multi_pw_aff_free(mpa2
);
6773 /* Compute the pullback of "mpa1" by the function represented by "mpa2".
6774 * In other words, plug in "mpa2" in "mpa1".
6776 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_pullback_multi_pw_aff(
6777 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
)
6779 return isl_multi_pw_aff_align_params_multi_multi_and(mpa1
, mpa2
,
6780 &isl_multi_pw_aff_pullback_multi_pw_aff_aligned
);
6783 /* Align the parameters of "mpa1" and "mpa2", check that the ranges
6784 * of "mpa1" and "mpa2" live in the same space, construct map space
6785 * between the domain spaces of "mpa1" and "mpa2" and call "order"
6786 * with this map space as extract argument.
6788 static __isl_give isl_map
*isl_multi_pw_aff_order_map(
6789 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
,
6790 __isl_give isl_map
*(*order
)(__isl_keep isl_multi_pw_aff
*mpa1
,
6791 __isl_keep isl_multi_pw_aff
*mpa2
, __isl_take isl_space
*space
))
6794 isl_space
*space1
, *space2
;
6797 mpa1
= isl_multi_pw_aff_align_params(mpa1
,
6798 isl_multi_pw_aff_get_space(mpa2
));
6799 mpa2
= isl_multi_pw_aff_align_params(mpa2
,
6800 isl_multi_pw_aff_get_space(mpa1
));
6803 match
= isl_space_tuple_is_equal(mpa1
->space
, isl_dim_out
,
6804 mpa2
->space
, isl_dim_out
);
6808 isl_die(isl_multi_pw_aff_get_ctx(mpa1
), isl_error_invalid
,
6809 "range spaces don't match", goto error
);
6810 space1
= isl_space_domain(isl_multi_pw_aff_get_space(mpa1
));
6811 space2
= isl_space_domain(isl_multi_pw_aff_get_space(mpa2
));
6812 space1
= isl_space_map_from_domain_and_range(space1
, space2
);
6814 res
= order(mpa1
, mpa2
, space1
);
6815 isl_multi_pw_aff_free(mpa1
);
6816 isl_multi_pw_aff_free(mpa2
);
6819 isl_multi_pw_aff_free(mpa1
);
6820 isl_multi_pw_aff_free(mpa2
);
6824 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
6825 * where the function values are equal. "space" is the space of the result.
6826 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
6828 * "mpa1" and "mpa2" are equal when each of the pairs of elements
6829 * in the sequences are equal.
6831 static __isl_give isl_map
*isl_multi_pw_aff_eq_map_on_space(
6832 __isl_keep isl_multi_pw_aff
*mpa1
, __isl_keep isl_multi_pw_aff
*mpa2
,
6833 __isl_take isl_space
*space
)
6838 res
= isl_map_universe(space
);
6840 n
= isl_multi_pw_aff_dim(mpa1
, isl_dim_out
);
6841 for (i
= 0; i
< n
; ++i
) {
6842 isl_pw_aff
*pa1
, *pa2
;
6845 pa1
= isl_multi_pw_aff_get_pw_aff(mpa1
, i
);
6846 pa2
= isl_multi_pw_aff_get_pw_aff(mpa2
, i
);
6847 map
= isl_pw_aff_eq_map(pa1
, pa2
);
6848 res
= isl_map_intersect(res
, map
);
6854 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
6855 * where the function values are equal.
6857 __isl_give isl_map
*isl_multi_pw_aff_eq_map(__isl_take isl_multi_pw_aff
*mpa1
,
6858 __isl_take isl_multi_pw_aff
*mpa2
)
6860 return isl_multi_pw_aff_order_map(mpa1
, mpa2
,
6861 &isl_multi_pw_aff_eq_map_on_space
);
6864 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
6865 * where the function values of "mpa1" is lexicographically satisfies "base"
6866 * compared to that of "mpa2". "space" is the space of the result.
6867 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
6869 * "mpa1" lexicographically satisfies "base" compared to "mpa2"
6870 * if its i-th element satisfies "base" when compared to
6871 * the i-th element of "mpa2" while all previous elements are
6874 static __isl_give isl_map
*isl_multi_pw_aff_lex_map_on_space(
6875 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
,
6876 __isl_give isl_map
*(*base
)(__isl_take isl_pw_aff
*pa1
,
6877 __isl_take isl_pw_aff
*pa2
),
6878 __isl_take isl_space
*space
)
6881 isl_map
*res
, *rest
;
6883 res
= isl_map_empty(isl_space_copy(space
));
6884 rest
= isl_map_universe(space
);
6886 n
= isl_multi_pw_aff_dim(mpa1
, isl_dim_out
);
6887 for (i
= 0; i
< n
; ++i
) {
6888 isl_pw_aff
*pa1
, *pa2
;
6891 pa1
= isl_multi_pw_aff_get_pw_aff(mpa1
, i
);
6892 pa2
= isl_multi_pw_aff_get_pw_aff(mpa2
, i
);
6893 map
= base(pa1
, pa2
);
6894 map
= isl_map_intersect(map
, isl_map_copy(rest
));
6895 res
= isl_map_union(res
, map
);
6900 pa1
= isl_multi_pw_aff_get_pw_aff(mpa1
, i
);
6901 pa2
= isl_multi_pw_aff_get_pw_aff(mpa2
, i
);
6902 map
= isl_pw_aff_eq_map(pa1
, pa2
);
6903 rest
= isl_map_intersect(rest
, map
);
6910 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
6911 * where the function value of "mpa1" is lexicographically less than that
6912 * of "mpa2". "space" is the space of the result.
6913 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
6915 * "mpa1" is less than "mpa2" if its i-th element is smaller
6916 * than the i-th element of "mpa2" while all previous elements are
6919 __isl_give isl_map
*isl_multi_pw_aff_lex_lt_map_on_space(
6920 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
,
6921 __isl_take isl_space
*space
)
6923 return isl_multi_pw_aff_lex_map_on_space(mpa1
, mpa2
,
6924 &isl_pw_aff_lt_map
, space
);
6927 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
6928 * where the function value of "mpa1" is lexicographically less than that
6931 __isl_give isl_map
*isl_multi_pw_aff_lex_lt_map(
6932 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
)
6934 return isl_multi_pw_aff_order_map(mpa1
, mpa2
,
6935 &isl_multi_pw_aff_lex_lt_map_on_space
);
6938 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
6939 * where the function value of "mpa1" is lexicographically greater than that
6940 * of "mpa2". "space" is the space of the result.
6941 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
6943 * "mpa1" is greater than "mpa2" if its i-th element is greater
6944 * than the i-th element of "mpa2" while all previous elements are
6947 __isl_give isl_map
*isl_multi_pw_aff_lex_gt_map_on_space(
6948 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
,
6949 __isl_take isl_space
*space
)
6951 return isl_multi_pw_aff_lex_map_on_space(mpa1
, mpa2
,
6952 &isl_pw_aff_gt_map
, space
);
6955 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
6956 * where the function value of "mpa1" is lexicographically greater than that
6959 __isl_give isl_map
*isl_multi_pw_aff_lex_gt_map(
6960 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
)
6962 return isl_multi_pw_aff_order_map(mpa1
, mpa2
,
6963 &isl_multi_pw_aff_lex_gt_map_on_space
);
6966 /* Compare two isl_affs.
6968 * Return -1 if "aff1" is "smaller" than "aff2", 1 if "aff1" is "greater"
6969 * than "aff2" and 0 if they are equal.
6971 * The order is fairly arbitrary. We do consider expressions that only involve
6972 * earlier dimensions as "smaller".
6974 int isl_aff_plain_cmp(__isl_keep isl_aff
*aff1
, __isl_keep isl_aff
*aff2
)
6987 cmp
= isl_local_space_cmp(aff1
->ls
, aff2
->ls
);
6991 last1
= isl_seq_last_non_zero(aff1
->v
->el
+ 1, aff1
->v
->size
- 1);
6992 last2
= isl_seq_last_non_zero(aff2
->v
->el
+ 1, aff1
->v
->size
- 1);
6994 return last1
- last2
;
6996 return isl_seq_cmp(aff1
->v
->el
, aff2
->v
->el
, aff1
->v
->size
);
6999 /* Compare two isl_pw_affs.
7001 * Return -1 if "pa1" is "smaller" than "pa2", 1 if "pa1" is "greater"
7002 * than "pa2" and 0 if they are equal.
7004 * The order is fairly arbitrary. We do consider expressions that only involve
7005 * earlier dimensions as "smaller".
7007 int isl_pw_aff_plain_cmp(__isl_keep isl_pw_aff
*pa1
,
7008 __isl_keep isl_pw_aff
*pa2
)
7021 cmp
= isl_space_cmp(pa1
->dim
, pa2
->dim
);
7025 if (pa1
->n
!= pa2
->n
)
7026 return pa1
->n
- pa2
->n
;
7028 for (i
= 0; i
< pa1
->n
; ++i
) {
7029 cmp
= isl_set_plain_cmp(pa1
->p
[i
].set
, pa2
->p
[i
].set
);
7032 cmp
= isl_aff_plain_cmp(pa1
->p
[i
].aff
, pa2
->p
[i
].aff
);
7040 /* Return a piecewise affine expression that is equal to "v" on "domain".
7042 __isl_give isl_pw_aff
*isl_pw_aff_val_on_domain(__isl_take isl_set
*domain
,
7043 __isl_take isl_val
*v
)
7046 isl_local_space
*ls
;
7049 space
= isl_set_get_space(domain
);
7050 ls
= isl_local_space_from_space(space
);
7051 aff
= isl_aff_val_on_domain(ls
, v
);
7053 return isl_pw_aff_alloc(domain
, aff
);
7056 /* Return a multi affine expression that is equal to "mv" on domain
7059 __isl_give isl_multi_aff
*isl_multi_aff_multi_val_on_space(
7060 __isl_take isl_space
*space
, __isl_take isl_multi_val
*mv
)
7064 isl_local_space
*ls
;
7070 n
= isl_multi_val_dim(mv
, isl_dim_set
);
7071 space2
= isl_multi_val_get_space(mv
);
7072 space2
= isl_space_align_params(space2
, isl_space_copy(space
));
7073 space
= isl_space_align_params(space
, isl_space_copy(space2
));
7074 space
= isl_space_map_from_domain_and_range(space
, space2
);
7075 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
7076 ls
= isl_local_space_from_space(isl_space_domain(space
));
7077 for (i
= 0; i
< n
; ++i
) {
7081 v
= isl_multi_val_get_val(mv
, i
);
7082 aff
= isl_aff_val_on_domain(isl_local_space_copy(ls
), v
);
7083 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
7085 isl_local_space_free(ls
);
7087 isl_multi_val_free(mv
);
7090 isl_space_free(space
);
7091 isl_multi_val_free(mv
);
7095 /* Return a piecewise multi-affine expression
7096 * that is equal to "mv" on "domain".
7098 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_multi_val_on_domain(
7099 __isl_take isl_set
*domain
, __isl_take isl_multi_val
*mv
)
7104 space
= isl_set_get_space(domain
);
7105 ma
= isl_multi_aff_multi_val_on_space(space
, mv
);
7107 return isl_pw_multi_aff_alloc(domain
, ma
);
7110 /* Internal data structure for isl_union_pw_multi_aff_multi_val_on_domain.
7111 * mv is the value that should be attained on each domain set
7112 * res collects the results
7114 struct isl_union_pw_multi_aff_multi_val_on_domain_data
{
7116 isl_union_pw_multi_aff
*res
;
7119 /* Create an isl_pw_multi_aff equal to data->mv on "domain"
7120 * and add it to data->res.
7122 static int pw_multi_aff_multi_val_on_domain(__isl_take isl_set
*domain
,
7125 struct isl_union_pw_multi_aff_multi_val_on_domain_data
*data
= user
;
7126 isl_pw_multi_aff
*pma
;
7129 mv
= isl_multi_val_copy(data
->mv
);
7130 pma
= isl_pw_multi_aff_multi_val_on_domain(domain
, mv
);
7131 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma
);
7133 return data
->res
? 0 : -1;
7136 /* Return a union piecewise multi-affine expression
7137 * that is equal to "mv" on "domain".
7139 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_multi_val_on_domain(
7140 __isl_take isl_union_set
*domain
, __isl_take isl_multi_val
*mv
)
7142 struct isl_union_pw_multi_aff_multi_val_on_domain_data data
;
7145 space
= isl_union_set_get_space(domain
);
7146 data
.res
= isl_union_pw_multi_aff_empty(space
);
7148 if (isl_union_set_foreach_set(domain
,
7149 &pw_multi_aff_multi_val_on_domain
, &data
) < 0)
7150 data
.res
= isl_union_pw_multi_aff_free(data
.res
);
7151 isl_union_set_free(domain
);
7152 isl_multi_val_free(mv
);
7156 /* Compute the pullback of data->pma by the function represented by "pma2",
7157 * provided the spaces match, and add the results to data->res.
7159 static int pullback_entry(void **entry
, void *user
)
7161 struct isl_union_pw_multi_aff_bin_data
*data
= user
;
7162 isl_pw_multi_aff
*pma2
= *entry
;
7164 if (!isl_space_tuple_is_equal(data
->pma
->dim
, isl_dim_in
,
7165 pma2
->dim
, isl_dim_out
))
7168 pma2
= isl_pw_multi_aff_pullback_pw_multi_aff(
7169 isl_pw_multi_aff_copy(data
->pma
),
7170 isl_pw_multi_aff_copy(pma2
));
7172 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma2
);
7179 /* Compute the pullback of "upma1" by the function represented by "upma2".
7181 __isl_give isl_union_pw_multi_aff
*
7182 isl_union_pw_multi_aff_pullback_union_pw_multi_aff(
7183 __isl_take isl_union_pw_multi_aff
*upma1
,
7184 __isl_take isl_union_pw_multi_aff
*upma2
)
7186 return bin_op(upma1
, upma2
, &pullback_entry
);
7189 /* Check that the domain space of "upa" matches "space".
7191 * Return 0 on success and -1 on error.
7193 * This function is called from isl_multi_union_pw_aff_set_union_pw_aff and
7194 * can in principle never fail since the space "space" is that
7195 * of the isl_multi_union_pw_aff and is a set space such that
7196 * there is no domain space to match.
7198 * We check the parameters and double-check that "space" is
7199 * indeed that of a set.
7201 static int isl_union_pw_aff_check_match_domain_space(
7202 __isl_keep isl_union_pw_aff
*upa
, __isl_keep isl_space
*space
)
7204 isl_space
*upa_space
;
7210 match
= isl_space_is_set(space
);
7214 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
7215 "expecting set space", return -1);
7217 upa_space
= isl_union_pw_aff_get_space(upa
);
7218 match
= isl_space_match(space
, isl_dim_param
, upa_space
, isl_dim_param
);
7222 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
7223 "parameters don't match", goto error
);
7225 isl_space_free(upa_space
);
7228 isl_space_free(upa_space
);
7232 /* Do the parameters of "upa" match those of "space"?
7234 static int isl_union_pw_aff_matching_params(__isl_keep isl_union_pw_aff
*upa
,
7235 __isl_keep isl_space
*space
)
7237 isl_space
*upa_space
;
7243 upa_space
= isl_union_pw_aff_get_space(upa
);
7245 match
= isl_space_match(space
, isl_dim_param
, upa_space
, isl_dim_param
);
7247 isl_space_free(upa_space
);
7251 /* Internal data structure for isl_union_pw_aff_reset_domain_space.
7252 * space represents the new parameters.
7253 * res collects the results.
7255 struct isl_union_pw_aff_reset_params_data
{
7257 isl_union_pw_aff
*res
;
7260 /* Replace the parameters of "pa" by data->space and
7261 * add the result to data->res.
7263 static int reset_params(__isl_take isl_pw_aff
*pa
, void *user
)
7265 struct isl_union_pw_aff_reset_params_data
*data
= user
;
7268 space
= isl_pw_aff_get_space(pa
);
7269 space
= isl_space_replace(space
, isl_dim_param
, data
->space
);
7270 pa
= isl_pw_aff_reset_space(pa
, space
);
7271 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
7273 return data
->res
? 0 : -1;
7276 /* Replace the domain space of "upa" by "space".
7277 * Since a union expression does not have a (single) domain space,
7278 * "space" is necessarily a parameter space.
7280 * Since the order and the names of the parameters determine
7281 * the hash value, we need to create a new hash table.
7283 static __isl_give isl_union_pw_aff
*isl_union_pw_aff_reset_domain_space(
7284 __isl_take isl_union_pw_aff
*upa
, __isl_take isl_space
*space
)
7286 struct isl_union_pw_aff_reset_params_data data
= { space
};
7289 match
= isl_union_pw_aff_matching_params(upa
, space
);
7291 upa
= isl_union_pw_aff_free(upa
);
7293 isl_space_free(space
);
7297 data
.res
= isl_union_pw_aff_empty(isl_space_copy(space
));
7298 if (isl_union_pw_aff_foreach_pw_aff(upa
, &reset_params
, &data
) < 0)
7299 data
.res
= isl_union_pw_aff_free(data
.res
);
7301 isl_union_pw_aff_free(upa
);
7302 isl_space_free(space
);
7306 /* Replace the entry of isl_union_pw_aff to which "entry" points
7309 static int floor_entry(void **entry
, void *user
)
7311 isl_pw_aff
**pa
= (isl_pw_aff
**) entry
;
7313 *pa
= isl_pw_aff_floor(*pa
);
7320 /* Given f, return floor(f).
7322 __isl_give isl_union_pw_aff
*isl_union_pw_aff_floor(
7323 __isl_take isl_union_pw_aff
*upa
)
7327 upa
= isl_union_pw_aff_cow(upa
);
7331 ctx
= isl_union_pw_aff_get_ctx(upa
);
7332 if (isl_hash_table_foreach(ctx
, &upa
->table
, &floor_entry
, NULL
) < 0)
7333 upa
= isl_union_pw_aff_free(upa
);
7340 * upa mod m = upa - m * floor(upa/m)
7342 * with m an integer value.
7344 __isl_give isl_union_pw_aff
*isl_union_pw_aff_mod_val(
7345 __isl_take isl_union_pw_aff
*upa
, __isl_take isl_val
*m
)
7347 isl_union_pw_aff
*res
;
7352 if (!isl_val_is_int(m
))
7353 isl_die(isl_val_get_ctx(m
), isl_error_invalid
,
7354 "expecting integer modulo", goto error
);
7355 if (!isl_val_is_pos(m
))
7356 isl_die(isl_val_get_ctx(m
), isl_error_invalid
,
7357 "expecting positive modulo", goto error
);
7359 res
= isl_union_pw_aff_copy(upa
);
7360 upa
= isl_union_pw_aff_scale_down_val(upa
, isl_val_copy(m
));
7361 upa
= isl_union_pw_aff_floor(upa
);
7362 upa
= isl_union_pw_aff_scale_val(upa
, m
);
7363 res
= isl_union_pw_aff_sub(res
, upa
);
7368 isl_union_pw_aff_free(upa
);
7372 /* Internal data structure for isl_union_pw_aff_aff_on_domain.
7373 * "aff" is the symbolic value that the resulting isl_union_pw_aff
7375 * "res" collects the results.
7377 struct isl_union_pw_aff_aff_on_domain_data
{
7379 isl_union_pw_aff
*res
;
7382 /* Construct a piecewise affine expression that is equal to data->aff
7383 * on "domain" and add the result to data->res.
7385 static int pw_aff_aff_on_domain(__isl_take isl_set
*domain
, void *user
)
7387 struct isl_union_pw_aff_aff_on_domain_data
*data
= user
;
7392 aff
= isl_aff_copy(data
->aff
);
7393 dim
= isl_set_dim(domain
, isl_dim_set
);
7394 aff
= isl_aff_add_dims(aff
, isl_dim_in
, dim
);
7395 aff
= isl_aff_reset_domain_space(aff
, isl_set_get_space(domain
));
7396 pa
= isl_pw_aff_alloc(domain
, aff
);
7397 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
7399 return data
->res
? 0 : -1;
7402 /* Internal data structure for isl_union_pw_multi_aff_get_union_pw_aff.
7403 * pos is the output position that needs to be extracted.
7404 * res collects the results.
7406 struct isl_union_pw_multi_aff_get_union_pw_aff_data
{
7408 isl_union_pw_aff
*res
;
7411 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma"
7412 * (assuming it has such a dimension) and add it to data->res.
7414 static int get_union_pw_aff(__isl_take isl_pw_multi_aff
*pma
, void *user
)
7416 struct isl_union_pw_multi_aff_get_union_pw_aff_data
*data
= user
;
7423 n_out
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
7424 if (data
->pos
>= n_out
) {
7425 isl_pw_multi_aff_free(pma
);
7429 pa
= isl_pw_multi_aff_get_pw_aff(pma
, data
->pos
);
7430 isl_pw_multi_aff_free(pma
);
7432 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
7434 return data
->res
? 0 : -1;
7437 /* Extract an isl_union_pw_aff corresponding to
7438 * output dimension "pos" of "upma".
7440 __isl_give isl_union_pw_aff
*isl_union_pw_multi_aff_get_union_pw_aff(
7441 __isl_keep isl_union_pw_multi_aff
*upma
, int pos
)
7443 struct isl_union_pw_multi_aff_get_union_pw_aff_data data
;
7450 isl_die(isl_union_pw_multi_aff_get_ctx(upma
), isl_error_invalid
,
7451 "cannot extract at negative position", return NULL
);
7453 space
= isl_union_pw_multi_aff_get_space(upma
);
7454 data
.res
= isl_union_pw_aff_empty(space
);
7456 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma
,
7457 &get_union_pw_aff
, &data
) < 0)
7458 data
.res
= isl_union_pw_aff_free(data
.res
);
7463 /* Return a union piecewise affine expression
7464 * that is equal to "aff" on "domain".
7466 * Construct an isl_pw_aff on each of the sets in "domain" and
7467 * collect the results.
7469 __isl_give isl_union_pw_aff
*isl_union_pw_aff_aff_on_domain(
7470 __isl_take isl_union_set
*domain
, __isl_take isl_aff
*aff
)
7472 struct isl_union_pw_aff_aff_on_domain_data data
;
7475 if (!domain
|| !aff
)
7477 if (!isl_local_space_is_params(aff
->ls
))
7478 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
7479 "expecting parametric expression", goto error
);
7481 space
= isl_union_set_get_space(domain
);
7482 data
.res
= isl_union_pw_aff_empty(space
);
7484 if (isl_union_set_foreach_set(domain
, &pw_aff_aff_on_domain
, &data
) < 0)
7485 data
.res
= isl_union_pw_aff_free(data
.res
);
7486 isl_union_set_free(domain
);
7490 isl_union_set_free(domain
);
7495 /* Internal data structure for isl_union_pw_aff_val_on_domain.
7496 * "v" is the value that the resulting isl_union_pw_aff needs to attain.
7497 * "res" collects the results.
7499 struct isl_union_pw_aff_val_on_domain_data
{
7501 isl_union_pw_aff
*res
;
7504 /* Construct a piecewise affine expression that is equal to data->v
7505 * on "domain" and add the result to data->res.
7507 static int pw_aff_val_on_domain(__isl_take isl_set
*domain
, void *user
)
7509 struct isl_union_pw_aff_val_on_domain_data
*data
= user
;
7513 v
= isl_val_copy(data
->v
);
7514 pa
= isl_pw_aff_val_on_domain(domain
, v
);
7515 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
7517 return data
->res
? 0 : -1;
7520 /* Return a union piecewise affine expression
7521 * that is equal to "v" on "domain".
7523 * Construct an isl_pw_aff on each of the sets in "domain" and
7524 * collect the results.
7526 __isl_give isl_union_pw_aff
*isl_union_pw_aff_val_on_domain(
7527 __isl_take isl_union_set
*domain
, __isl_take isl_val
*v
)
7529 struct isl_union_pw_aff_val_on_domain_data data
;
7532 space
= isl_union_set_get_space(domain
);
7533 data
.res
= isl_union_pw_aff_empty(space
);
7535 if (isl_union_set_foreach_set(domain
, &pw_aff_val_on_domain
, &data
) < 0)
7536 data
.res
= isl_union_pw_aff_free(data
.res
);
7537 isl_union_set_free(domain
);
7542 /* Construct a piecewise multi affine expression
7543 * that is equal to "pa" and add it to upma.
7545 static int pw_multi_aff_from_pw_aff_entry(__isl_take isl_pw_aff
*pa
, void *user
)
7547 isl_union_pw_multi_aff
**upma
= user
;
7548 isl_pw_multi_aff
*pma
;
7550 pma
= isl_pw_multi_aff_from_pw_aff(pa
);
7551 *upma
= isl_union_pw_multi_aff_add_pw_multi_aff(*upma
, pma
);
7553 return *upma
? 0 : -1;
7556 /* Construct and return a union piecewise multi affine expression
7557 * that is equal to the given union piecewise affine expression.
7559 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_union_pw_aff(
7560 __isl_take isl_union_pw_aff
*upa
)
7563 isl_union_pw_multi_aff
*upma
;
7568 space
= isl_union_pw_aff_get_space(upa
);
7569 upma
= isl_union_pw_multi_aff_empty(space
);
7571 if (isl_union_pw_aff_foreach_pw_aff(upa
,
7572 &pw_multi_aff_from_pw_aff_entry
, &upma
) < 0)
7573 upma
= isl_union_pw_multi_aff_free(upma
);
7575 isl_union_pw_aff_free(upa
);
7579 /* Compute the set of elements in the domain of "pa" where it is zero and
7580 * add this set to "uset".
7582 static int zero_union_set(__isl_take isl_pw_aff
*pa
, void *user
)
7584 isl_union_set
**uset
= (isl_union_set
**)user
;
7586 *uset
= isl_union_set_add_set(*uset
, isl_pw_aff_zero_set(pa
));
7588 return *uset
? 0 : -1;
7591 /* Return a union set containing those elements in the domain
7592 * of "upa" where it is zero.
7594 __isl_give isl_union_set
*isl_union_pw_aff_zero_union_set(
7595 __isl_take isl_union_pw_aff
*upa
)
7597 isl_union_set
*zero
;
7599 zero
= isl_union_set_empty(isl_union_pw_aff_get_space(upa
));
7600 if (isl_union_pw_aff_foreach_pw_aff(upa
, &zero_union_set
, &zero
) < 0)
7601 zero
= isl_union_set_free(zero
);
7603 isl_union_pw_aff_free(upa
);
7607 /* Convert "pa" to an isl_map and add it to *umap.
7609 static int map_from_pw_aff_entry(__isl_take isl_pw_aff
*pa
, void *user
)
7611 isl_union_map
**umap
= user
;
7614 map
= isl_map_from_pw_aff(pa
);
7615 *umap
= isl_union_map_add_map(*umap
, map
);
7617 return *umap
? 0 : -1;
7620 /* Construct a union map mapping the domain of the union
7621 * piecewise affine expression to its range, with the single output dimension
7622 * equated to the corresponding affine expressions on their cells.
7624 __isl_give isl_union_map
*isl_union_map_from_union_pw_aff(
7625 __isl_take isl_union_pw_aff
*upa
)
7628 isl_union_map
*umap
;
7633 space
= isl_union_pw_aff_get_space(upa
);
7634 umap
= isl_union_map_empty(space
);
7636 if (isl_union_pw_aff_foreach_pw_aff(upa
, &map_from_pw_aff_entry
,
7638 umap
= isl_union_map_free(umap
);
7640 isl_union_pw_aff_free(upa
);
7644 /* Internal data structure for isl_union_pw_aff_pullback_union_pw_multi_aff.
7645 * upma is the function that is plugged in.
7646 * pa is the current part of the function in which upma is plugged in.
7647 * res collects the results.
7649 struct isl_union_pw_aff_pullback_upma_data
{
7650 isl_union_pw_multi_aff
*upma
;
7652 isl_union_pw_aff
*res
;
7655 /* Check if "pma" can be plugged into data->pa.
7656 * If so, perform the pullback and add the result to data->res.
7658 static int pa_pb_pma(void **entry
, void *user
)
7660 struct isl_union_pw_aff_pullback_upma_data
*data
= user
;
7661 isl_pw_multi_aff
*pma
= *entry
;
7664 if (!isl_space_tuple_is_equal(data
->pa
->dim
, isl_dim_in
,
7665 pma
->dim
, isl_dim_out
))
7668 pma
= isl_pw_multi_aff_copy(pma
);
7669 pa
= isl_pw_aff_copy(data
->pa
);
7670 pa
= isl_pw_aff_pullback_pw_multi_aff(pa
, pma
);
7672 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
7674 return data
->res
? 0 : -1;
7677 /* Check if any of the elements of data->upma can be plugged into pa,
7678 * add if so add the result to data->res.
7680 static int upa_pb_upma(void **entry
, void *user
)
7682 struct isl_union_pw_aff_pullback_upma_data
*data
= user
;
7684 isl_pw_aff
*pa
= *entry
;
7687 ctx
= isl_union_pw_multi_aff_get_ctx(data
->upma
);
7688 if (isl_hash_table_foreach(ctx
, &data
->upma
->table
,
7689 &pa_pb_pma
, data
) < 0)
7695 /* Compute the pullback of "upa" by the function represented by "upma".
7696 * In other words, plug in "upma" in "upa". The result contains
7697 * expressions defined over the domain space of "upma".
7699 * Run over all pairs of elements in "upa" and "upma", perform
7700 * the pullback when appropriate and collect the results.
7701 * If the hash value were based on the domain space rather than
7702 * the function space, then we could run through all elements
7703 * of "upma" and directly pick out the corresponding element of "upa".
7705 __isl_give isl_union_pw_aff
*isl_union_pw_aff_pullback_union_pw_multi_aff(
7706 __isl_take isl_union_pw_aff
*upa
,
7707 __isl_take isl_union_pw_multi_aff
*upma
)
7709 struct isl_union_pw_aff_pullback_upma_data data
= { NULL
, NULL
};
7713 space
= isl_union_pw_multi_aff_get_space(upma
);
7714 upa
= isl_union_pw_aff_align_params(upa
, space
);
7715 space
= isl_union_pw_aff_get_space(upa
);
7716 upma
= isl_union_pw_multi_aff_align_params(upma
, space
);
7721 ctx
= isl_union_pw_aff_get_ctx(upa
);
7723 space
= isl_union_pw_aff_get_space(upa
);
7724 data
.res
= isl_union_pw_aff_alloc(space
, upa
->table
.n
);
7725 if (isl_hash_table_foreach(ctx
, &upa
->table
, &upa_pb_upma
, &data
) < 0)
7726 data
.res
= isl_union_pw_aff_free(data
.res
);
7728 isl_union_pw_aff_free(upa
);
7729 isl_union_pw_multi_aff_free(upma
);
7732 isl_union_pw_aff_free(upa
);
7733 isl_union_pw_multi_aff_free(upma
);
7738 #define BASE union_pw_aff
7740 #define DOMBASE union_set
7742 #define NO_MOVE_DIMS
7751 #include <isl_multi_templ.c>
7752 #include <isl_multi_apply_set.c>
7753 #include <isl_multi_apply_union_set.c>
7754 #include <isl_multi_floor.c>
7755 #include <isl_multi_gist.c>
7756 #include <isl_multi_intersect.c>
7758 /* Construct a multiple union piecewise affine expression
7759 * in the given space with value zero in each of the output dimensions.
7761 * Since there is no canonical zero value for
7762 * a union piecewise affine expression, we can only construct
7763 * zero-dimensional "zero" value.
7765 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_zero(
7766 __isl_take isl_space
*space
)
7771 if (!isl_space_is_set(space
))
7772 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
7773 "expecting set space", goto error
);
7774 if (isl_space_dim(space
, isl_dim_out
) != 0)
7775 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
7776 "expecting 0D space", goto error
);
7778 return isl_multi_union_pw_aff_alloc(space
);
7780 isl_space_free(space
);
7784 /* Compute the sum of "mupa1" and "mupa2" on the union of their domains,
7785 * with the actual sum on the shared domain and
7786 * the defined expression on the symmetric difference of the domains.
7788 * We simply iterate over the elements in both arguments and
7789 * call isl_union_pw_aff_union_add on each of them.
7791 static __isl_give isl_multi_union_pw_aff
*
7792 isl_multi_union_pw_aff_union_add_aligned(
7793 __isl_take isl_multi_union_pw_aff
*mupa1
,
7794 __isl_take isl_multi_union_pw_aff
*mupa2
)
7796 return isl_multi_union_pw_aff_bin_op(mupa1
, mupa2
,
7797 &isl_union_pw_aff_union_add
);
7800 /* Compute the sum of "mupa1" and "mupa2" on the union of their domains,
7801 * with the actual sum on the shared domain and
7802 * the defined expression on the symmetric difference of the domains.
7804 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_union_add(
7805 __isl_take isl_multi_union_pw_aff
*mupa1
,
7806 __isl_take isl_multi_union_pw_aff
*mupa2
)
7808 return isl_multi_union_pw_aff_align_params_multi_multi_and(mupa1
, mupa2
,
7809 &isl_multi_union_pw_aff_union_add_aligned
);
7812 /* Construct and return a multi union piecewise affine expression
7813 * that is equal to the given multi affine expression.
7815 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_from_multi_aff(
7816 __isl_take isl_multi_aff
*ma
)
7818 isl_multi_pw_aff
*mpa
;
7820 mpa
= isl_multi_pw_aff_from_multi_aff(ma
);
7821 return isl_multi_union_pw_aff_from_multi_pw_aff(mpa
);
7824 /* Construct and return a multi union piecewise affine expression
7825 * that is equal to the given multi piecewise affine expression.
7827 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_from_multi_pw_aff(
7828 __isl_take isl_multi_pw_aff
*mpa
)
7832 isl_multi_union_pw_aff
*mupa
;
7837 space
= isl_multi_pw_aff_get_space(mpa
);
7838 space
= isl_space_range(space
);
7839 mupa
= isl_multi_union_pw_aff_alloc(space
);
7841 n
= isl_multi_pw_aff_dim(mpa
, isl_dim_out
);
7842 for (i
= 0; i
< n
; ++i
) {
7844 isl_union_pw_aff
*upa
;
7846 pa
= isl_multi_pw_aff_get_pw_aff(mpa
, i
);
7847 upa
= isl_union_pw_aff_from_pw_aff(pa
);
7848 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
7851 isl_multi_pw_aff_free(mpa
);
7856 /* Extract the range space of "pma" and assign it to *space.
7857 * If *space has already been set (through a previous call to this function),
7858 * then check that the range space is the same.
7860 static int extract_space(__isl_take isl_pw_multi_aff
*pma
, void *user
)
7862 isl_space
**space
= user
;
7863 isl_space
*pma_space
;
7866 pma_space
= isl_space_range(isl_pw_multi_aff_get_space(pma
));
7867 isl_pw_multi_aff_free(pma
);
7876 equal
= isl_space_is_equal(pma_space
, *space
);
7877 isl_space_free(pma_space
);
7882 isl_die(isl_space_get_ctx(*space
), isl_error_invalid
,
7883 "range spaces not the same", return -1);
7887 /* Construct and return a multi union piecewise affine expression
7888 * that is equal to the given union piecewise multi affine expression.
7890 * In order to be able to perform the conversion, the input
7891 * needs to be non-empty and may only involve a single range space.
7893 __isl_give isl_multi_union_pw_aff
*
7894 isl_multi_union_pw_aff_from_union_pw_multi_aff(
7895 __isl_take isl_union_pw_multi_aff
*upma
)
7897 isl_space
*space
= NULL
;
7898 isl_multi_union_pw_aff
*mupa
;
7903 if (isl_union_pw_multi_aff_n_pw_multi_aff(upma
) == 0)
7904 isl_die(isl_union_pw_multi_aff_get_ctx(upma
), isl_error_invalid
,
7905 "cannot extract range space from empty input",
7907 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma
, &extract_space
,
7914 n
= isl_space_dim(space
, isl_dim_set
);
7915 mupa
= isl_multi_union_pw_aff_alloc(space
);
7917 for (i
= 0; i
< n
; ++i
) {
7918 isl_union_pw_aff
*upa
;
7920 upa
= isl_union_pw_multi_aff_get_union_pw_aff(upma
, i
);
7921 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
7924 isl_union_pw_multi_aff_free(upma
);
7927 isl_space_free(space
);
7928 isl_union_pw_multi_aff_free(upma
);
7932 /* Try and create an isl_multi_union_pw_aff that is equivalent
7933 * to the given isl_union_map.
7934 * The isl_union_map is required to be single-valued in each space.
7935 * Moreover, it cannot be empty and all range spaces need to be the same.
7936 * Otherwise, an error is produced.
7938 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_from_union_map(
7939 __isl_take isl_union_map
*umap
)
7941 isl_union_pw_multi_aff
*upma
;
7943 upma
= isl_union_pw_multi_aff_from_union_map(umap
);
7944 return isl_multi_union_pw_aff_from_union_pw_multi_aff(upma
);
7947 /* Return a multiple union piecewise affine expression
7948 * that is equal to "mv" on "domain", assuming "domain" and "mv"
7949 * have been aligned.
7951 static __isl_give isl_multi_union_pw_aff
*
7952 isl_multi_union_pw_aff_multi_val_on_domain_aligned(
7953 __isl_take isl_union_set
*domain
, __isl_take isl_multi_val
*mv
)
7957 isl_multi_union_pw_aff
*mupa
;
7962 n
= isl_multi_val_dim(mv
, isl_dim_set
);
7963 space
= isl_multi_val_get_space(mv
);
7964 mupa
= isl_multi_union_pw_aff_alloc(space
);
7965 for (i
= 0; i
< n
; ++i
) {
7967 isl_union_pw_aff
*upa
;
7969 v
= isl_multi_val_get_val(mv
, i
);
7970 upa
= isl_union_pw_aff_val_on_domain(isl_union_set_copy(domain
),
7972 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
7975 isl_union_set_free(domain
);
7976 isl_multi_val_free(mv
);
7979 isl_union_set_free(domain
);
7980 isl_multi_val_free(mv
);
7984 /* Return a multiple union piecewise affine expression
7985 * that is equal to "mv" on "domain".
7987 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_multi_val_on_domain(
7988 __isl_take isl_union_set
*domain
, __isl_take isl_multi_val
*mv
)
7992 if (isl_space_match(domain
->dim
, isl_dim_param
,
7993 mv
->space
, isl_dim_param
))
7994 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(
7996 domain
= isl_union_set_align_params(domain
,
7997 isl_multi_val_get_space(mv
));
7998 mv
= isl_multi_val_align_params(mv
, isl_union_set_get_space(domain
));
7999 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(domain
, mv
);
8001 isl_union_set_free(domain
);
8002 isl_multi_val_free(mv
);
8006 /* Return a multiple union piecewise affine expression
8007 * that is equal to "ma" on "domain", assuming "domain" and "ma"
8008 * have been aligned.
8010 static __isl_give isl_multi_union_pw_aff
*
8011 isl_multi_union_pw_aff_multi_aff_on_domain_aligned(
8012 __isl_take isl_union_set
*domain
, __isl_take isl_multi_aff
*ma
)
8016 isl_multi_union_pw_aff
*mupa
;
8021 n
= isl_multi_aff_dim(ma
, isl_dim_set
);
8022 space
= isl_multi_aff_get_space(ma
);
8023 mupa
= isl_multi_union_pw_aff_alloc(space
);
8024 for (i
= 0; i
< n
; ++i
) {
8026 isl_union_pw_aff
*upa
;
8028 aff
= isl_multi_aff_get_aff(ma
, i
);
8029 upa
= isl_union_pw_aff_aff_on_domain(isl_union_set_copy(domain
),
8031 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
8034 isl_union_set_free(domain
);
8035 isl_multi_aff_free(ma
);
8038 isl_union_set_free(domain
);
8039 isl_multi_aff_free(ma
);
8043 /* Return a multiple union piecewise affine expression
8044 * that is equal to "ma" on "domain".
8046 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_multi_aff_on_domain(
8047 __isl_take isl_union_set
*domain
, __isl_take isl_multi_aff
*ma
)
8051 if (isl_space_match(domain
->dim
, isl_dim_param
,
8052 ma
->space
, isl_dim_param
))
8053 return isl_multi_union_pw_aff_multi_aff_on_domain_aligned(
8055 domain
= isl_union_set_align_params(domain
,
8056 isl_multi_aff_get_space(ma
));
8057 ma
= isl_multi_aff_align_params(ma
, isl_union_set_get_space(domain
));
8058 return isl_multi_union_pw_aff_multi_aff_on_domain_aligned(domain
, ma
);
8060 isl_union_set_free(domain
);
8061 isl_multi_aff_free(ma
);
8065 /* Return a union set containing those elements in the domains
8066 * of the elements of "mupa" where they are all zero.
8068 __isl_give isl_union_set
*isl_multi_union_pw_aff_zero_union_set(
8069 __isl_take isl_multi_union_pw_aff
*mupa
)
8072 isl_union_pw_aff
*upa
;
8073 isl_union_set
*zero
;
8078 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8080 isl_die(isl_multi_union_pw_aff_get_ctx(mupa
), isl_error_invalid
,
8081 "cannot determine zero set "
8082 "of zero-dimensional function", goto error
);
8084 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, 0);
8085 zero
= isl_union_pw_aff_zero_union_set(upa
);
8087 for (i
= 1; i
< n
; ++i
) {
8088 isl_union_set
*zero_i
;
8090 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
8091 zero_i
= isl_union_pw_aff_zero_union_set(upa
);
8093 zero
= isl_union_set_intersect(zero
, zero_i
);
8096 isl_multi_union_pw_aff_free(mupa
);
8099 isl_multi_union_pw_aff_free(mupa
);
8103 /* Construct a union map mapping the shared domain
8104 * of the union piecewise affine expressions to the range of "mupa"
8105 * with each dimension in the range equated to the
8106 * corresponding union piecewise affine expression.
8108 * The input cannot be zero-dimensional as there is
8109 * no way to extract a domain from a zero-dimensional isl_multi_union_pw_aff.
8111 __isl_give isl_union_map
*isl_union_map_from_multi_union_pw_aff(
8112 __isl_take isl_multi_union_pw_aff
*mupa
)
8116 isl_union_map
*umap
;
8117 isl_union_pw_aff
*upa
;
8122 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8124 isl_die(isl_multi_union_pw_aff_get_ctx(mupa
), isl_error_invalid
,
8125 "cannot determine domain of zero-dimensional "
8126 "isl_multi_union_pw_aff", goto error
);
8128 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, 0);
8129 umap
= isl_union_map_from_union_pw_aff(upa
);
8131 for (i
= 1; i
< n
; ++i
) {
8132 isl_union_map
*umap_i
;
8134 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
8135 umap_i
= isl_union_map_from_union_pw_aff(upa
);
8136 umap
= isl_union_map_flat_range_product(umap
, umap_i
);
8139 space
= isl_multi_union_pw_aff_get_space(mupa
);
8140 umap
= isl_union_map_reset_range_space(umap
, space
);
8142 isl_multi_union_pw_aff_free(mupa
);
8145 isl_multi_union_pw_aff_free(mupa
);
8149 /* Internal data structure for isl_union_pw_multi_aff_reset_range_space.
8150 * "range" is the space from which to set the range space.
8151 * "res" collects the results.
8153 struct isl_union_pw_multi_aff_reset_range_space_data
{
8155 isl_union_pw_multi_aff
*res
;
8158 /* Replace the range space of "pma" by the range space of data->range and
8159 * add the result to data->res.
8161 static int reset_range_space(__isl_take isl_pw_multi_aff
*pma
, void *user
)
8163 struct isl_union_pw_multi_aff_reset_range_space_data
*data
= user
;
8166 space
= isl_pw_multi_aff_get_space(pma
);
8167 space
= isl_space_domain(space
);
8168 space
= isl_space_extend_domain_with_range(space
,
8169 isl_space_copy(data
->range
));
8170 pma
= isl_pw_multi_aff_reset_space(pma
, space
);
8171 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma
);
8173 return data
->res
? 0 : -1;
8176 /* Replace the range space of all the piecewise affine expressions in "upma" by
8177 * the range space of "space".
8179 * This assumes that all these expressions have the same output dimension.
8181 * Since the spaces of the expressions change, so do their hash values.
8182 * We therefore need to create a new isl_union_pw_multi_aff.
8183 * Note that the hash value is currently computed based on the entire
8184 * space even though there can only be a single expression with a given
8187 static __isl_give isl_union_pw_multi_aff
*
8188 isl_union_pw_multi_aff_reset_range_space(
8189 __isl_take isl_union_pw_multi_aff
*upma
, __isl_take isl_space
*space
)
8191 struct isl_union_pw_multi_aff_reset_range_space_data data
= { space
};
8192 isl_space
*space_upma
;
8194 space_upma
= isl_union_pw_multi_aff_get_space(upma
);
8195 data
.res
= isl_union_pw_multi_aff_empty(space_upma
);
8196 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma
,
8197 &reset_range_space
, &data
) < 0)
8198 data
.res
= isl_union_pw_multi_aff_free(data
.res
);
8200 isl_space_free(space
);
8201 isl_union_pw_multi_aff_free(upma
);
8205 /* Construct and return a union piecewise multi affine expression
8206 * that is equal to the given multi union piecewise affine expression.
8208 * In order to be able to perform the conversion, the input
8209 * needs to have a least one output dimension.
8211 __isl_give isl_union_pw_multi_aff
*
8212 isl_union_pw_multi_aff_from_multi_union_pw_aff(
8213 __isl_take isl_multi_union_pw_aff
*mupa
)
8217 isl_union_pw_multi_aff
*upma
;
8218 isl_union_pw_aff
*upa
;
8223 space
= isl_multi_union_pw_aff_get_space(mupa
);
8225 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8227 isl_die(isl_multi_union_pw_aff_get_ctx(mupa
), isl_error_invalid
,
8228 "cannot determine domain of zero-dimensional "
8229 "isl_multi_union_pw_aff", goto error
);
8231 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, 0);
8232 upma
= isl_union_pw_multi_aff_from_union_pw_aff(upa
);
8234 for (i
= 1; i
< n
; ++i
) {
8235 isl_union_pw_multi_aff
*upma_i
;
8237 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
8238 upma_i
= isl_union_pw_multi_aff_from_union_pw_aff(upa
);
8239 upma
= isl_union_pw_multi_aff_flat_range_product(upma
, upma_i
);
8242 upma
= isl_union_pw_multi_aff_reset_range_space(upma
, space
);
8244 isl_multi_union_pw_aff_free(mupa
);
8247 isl_multi_union_pw_aff_free(mupa
);
8251 /* Intersect the range of "mupa" with "range".
8252 * That is, keep only those domain elements that have a function value
8255 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_intersect_range(
8256 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_set
*range
)
8258 isl_union_pw_multi_aff
*upma
;
8259 isl_union_set
*domain
;
8264 if (!mupa
|| !range
)
8267 space
= isl_set_get_space(range
);
8268 match
= isl_space_tuple_is_equal(mupa
->space
, isl_dim_set
,
8269 space
, isl_dim_set
);
8270 isl_space_free(space
);
8274 isl_die(isl_multi_union_pw_aff_get_ctx(mupa
), isl_error_invalid
,
8275 "space don't match", goto error
);
8276 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8278 isl_die(isl_multi_union_pw_aff_get_ctx(mupa
), isl_error_invalid
,
8279 "cannot intersect range of zero-dimensional "
8280 "isl_multi_union_pw_aff", goto error
);
8282 upma
= isl_union_pw_multi_aff_from_multi_union_pw_aff(
8283 isl_multi_union_pw_aff_copy(mupa
));
8284 domain
= isl_union_set_from_set(range
);
8285 domain
= isl_union_set_preimage_union_pw_multi_aff(domain
, upma
);
8286 mupa
= isl_multi_union_pw_aff_intersect_domain(mupa
, domain
);
8290 isl_multi_union_pw_aff_free(mupa
);
8291 isl_set_free(range
);
8295 /* Return the shared domain of the elements of "mupa".
8297 __isl_give isl_union_set
*isl_multi_union_pw_aff_domain(
8298 __isl_take isl_multi_union_pw_aff
*mupa
)
8301 isl_union_pw_aff
*upa
;
8307 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8309 isl_die(isl_multi_union_pw_aff_get_ctx(mupa
), isl_error_invalid
,
8310 "cannot determine domain", goto error
);
8312 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, 0);
8313 dom
= isl_union_pw_aff_domain(upa
);
8314 for (i
= 1; i
< n
; ++i
) {
8315 isl_union_set
*dom_i
;
8317 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
8318 dom_i
= isl_union_pw_aff_domain(upa
);
8319 dom
= isl_union_set_intersect(dom
, dom_i
);
8322 isl_multi_union_pw_aff_free(mupa
);
8325 isl_multi_union_pw_aff_free(mupa
);
8329 /* Apply "aff" to "mupa". The space of "mupa" is equal to the domain of "aff".
8330 * In particular, the spaces have been aligned.
8331 * The result is defined over the shared domain of the elements of "mupa"
8333 * We first extract the parametric constant part of "aff" and
8334 * define that over the shared domain.
8335 * Then we iterate over all input dimensions of "aff" and add the corresponding
8336 * multiples of the elements of "mupa".
8337 * Finally, we consider the integer divisions, calling the function
8338 * recursively to obtain an isl_union_pw_aff corresponding to the
8339 * integer division argument.
8341 static __isl_give isl_union_pw_aff
*multi_union_pw_aff_apply_aff(
8342 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_aff
*aff
)
8345 isl_union_pw_aff
*upa
;
8346 isl_union_set
*uset
;
8350 n_in
= isl_aff_dim(aff
, isl_dim_in
);
8351 n_div
= isl_aff_dim(aff
, isl_dim_div
);
8353 uset
= isl_multi_union_pw_aff_domain(isl_multi_union_pw_aff_copy(mupa
));
8354 cst
= isl_aff_copy(aff
);
8355 cst
= isl_aff_drop_dims(cst
, isl_dim_div
, 0, n_div
);
8356 cst
= isl_aff_drop_dims(cst
, isl_dim_in
, 0, n_in
);
8357 cst
= isl_aff_project_domain_on_params(cst
);
8358 upa
= isl_union_pw_aff_aff_on_domain(uset
, cst
);
8360 for (i
= 0; i
< n_in
; ++i
) {
8361 isl_union_pw_aff
*upa_i
;
8363 if (!isl_aff_involves_dims(aff
, isl_dim_in
, i
, 1))
8365 v
= isl_aff_get_coefficient_val(aff
, isl_dim_in
, i
);
8366 upa_i
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
8367 upa_i
= isl_union_pw_aff_scale_val(upa_i
, v
);
8368 upa
= isl_union_pw_aff_add(upa
, upa_i
);
8371 for (i
= 0; i
< n_div
; ++i
) {
8373 isl_union_pw_aff
*upa_i
;
8375 if (!isl_aff_involves_dims(aff
, isl_dim_div
, i
, 1))
8377 div
= isl_aff_get_div(aff
, i
);
8378 upa_i
= multi_union_pw_aff_apply_aff(
8379 isl_multi_union_pw_aff_copy(mupa
), div
);
8380 upa_i
= isl_union_pw_aff_floor(upa_i
);
8381 v
= isl_aff_get_coefficient_val(aff
, isl_dim_div
, i
);
8382 upa_i
= isl_union_pw_aff_scale_val(upa_i
, v
);
8383 upa
= isl_union_pw_aff_add(upa
, upa_i
);
8386 isl_multi_union_pw_aff_free(mupa
);
8392 /* Apply "aff" to "mupa". The space of "mupa" needs to be compatible
8393 * with the domain of "aff".
8394 * Furthermore, the dimension of this space needs to be greater than zero.
8395 * The result is defined over the shared domain of the elements of "mupa"
8397 * We perform these checks and then hand over control to
8398 * multi_union_pw_aff_apply_aff.
8400 __isl_give isl_union_pw_aff
*isl_multi_union_pw_aff_apply_aff(
8401 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_aff
*aff
)
8403 isl_space
*space1
, *space2
;
8406 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
8407 isl_aff_get_space(aff
));
8408 aff
= isl_aff_align_params(aff
, isl_multi_union_pw_aff_get_space(mupa
));
8412 space1
= isl_multi_union_pw_aff_get_space(mupa
);
8413 space2
= isl_aff_get_domain_space(aff
);
8414 equal
= isl_space_is_equal(space1
, space2
);
8415 isl_space_free(space1
);
8416 isl_space_free(space2
);
8420 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
8421 "spaces don't match", goto error
);
8422 if (isl_aff_dim(aff
, isl_dim_in
) == 0)
8423 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
8424 "cannot determine domains", goto error
);
8426 return multi_union_pw_aff_apply_aff(mupa
, aff
);
8428 isl_multi_union_pw_aff_free(mupa
);
8433 /* Apply "ma" to "mupa". The space of "mupa" needs to be compatible
8434 * with the domain of "ma".
8435 * Furthermore, the dimension of this space needs to be greater than zero,
8436 * unless the dimension of the target space of "ma" is also zero.
8437 * The result is defined over the shared domain of the elements of "mupa"
8439 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_apply_multi_aff(
8440 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_multi_aff
*ma
)
8442 isl_space
*space1
, *space2
;
8443 isl_multi_union_pw_aff
*res
;
8447 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
8448 isl_multi_aff_get_space(ma
));
8449 ma
= isl_multi_aff_align_params(ma
,
8450 isl_multi_union_pw_aff_get_space(mupa
));
8454 space1
= isl_multi_union_pw_aff_get_space(mupa
);
8455 space2
= isl_multi_aff_get_domain_space(ma
);
8456 equal
= isl_space_is_equal(space1
, space2
);
8457 isl_space_free(space1
);
8458 isl_space_free(space2
);
8462 isl_die(isl_multi_aff_get_ctx(ma
), isl_error_invalid
,
8463 "spaces don't match", goto error
);
8464 n_out
= isl_multi_aff_dim(ma
, isl_dim_out
);
8465 if (isl_multi_aff_dim(ma
, isl_dim_in
) == 0 && n_out
!= 0)
8466 isl_die(isl_multi_aff_get_ctx(ma
), isl_error_invalid
,
8467 "cannot determine domains", goto error
);
8469 space1
= isl_space_range(isl_multi_aff_get_space(ma
));
8470 res
= isl_multi_union_pw_aff_alloc(space1
);
8472 for (i
= 0; i
< n_out
; ++i
) {
8474 isl_union_pw_aff
*upa
;
8476 aff
= isl_multi_aff_get_aff(ma
, i
);
8477 upa
= multi_union_pw_aff_apply_aff(
8478 isl_multi_union_pw_aff_copy(mupa
), aff
);
8479 res
= isl_multi_union_pw_aff_set_union_pw_aff(res
, i
, upa
);
8482 isl_multi_aff_free(ma
);
8483 isl_multi_union_pw_aff_free(mupa
);
8486 isl_multi_union_pw_aff_free(mupa
);
8487 isl_multi_aff_free(ma
);
8491 /* Apply "pa" to "mupa". The space of "mupa" needs to be compatible
8492 * with the domain of "pa".
8493 * Furthermore, the dimension of this space needs to be greater than zero.
8494 * The result is defined over the shared domain of the elements of "mupa"
8496 __isl_give isl_union_pw_aff
*isl_multi_union_pw_aff_apply_pw_aff(
8497 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_pw_aff
*pa
)
8501 isl_space
*space
, *space2
;
8502 isl_union_pw_aff
*upa
;
8504 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
8505 isl_pw_aff_get_space(pa
));
8506 pa
= isl_pw_aff_align_params(pa
,
8507 isl_multi_union_pw_aff_get_space(mupa
));
8511 space
= isl_multi_union_pw_aff_get_space(mupa
);
8512 space2
= isl_pw_aff_get_domain_space(pa
);
8513 equal
= isl_space_is_equal(space
, space2
);
8514 isl_space_free(space
);
8515 isl_space_free(space2
);
8519 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
8520 "spaces don't match", goto error
);
8521 if (isl_pw_aff_dim(pa
, isl_dim_in
) == 0)
8522 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
8523 "cannot determine domains", goto error
);
8525 space
= isl_space_params(isl_multi_union_pw_aff_get_space(mupa
));
8526 upa
= isl_union_pw_aff_empty(space
);
8528 for (i
= 0; i
< pa
->n
; ++i
) {
8531 isl_multi_union_pw_aff
*mupa_i
;
8532 isl_union_pw_aff
*upa_i
;
8534 mupa_i
= isl_multi_union_pw_aff_copy(mupa
);
8535 domain
= isl_set_copy(pa
->p
[i
].set
);
8536 mupa_i
= isl_multi_union_pw_aff_intersect_range(mupa_i
, domain
);
8537 aff
= isl_aff_copy(pa
->p
[i
].aff
);
8538 upa_i
= multi_union_pw_aff_apply_aff(mupa_i
, aff
);
8539 upa
= isl_union_pw_aff_union_add(upa
, upa_i
);
8542 isl_multi_union_pw_aff_free(mupa
);
8543 isl_pw_aff_free(pa
);
8546 isl_multi_union_pw_aff_free(mupa
);
8547 isl_pw_aff_free(pa
);
8551 /* Apply "pma" to "mupa". The space of "mupa" needs to be compatible
8552 * with the domain of "pma".
8553 * Furthermore, the dimension of this space needs to be greater than zero,
8554 * unless the dimension of the target space of "pma" is also zero.
8555 * The result is defined over the shared domain of the elements of "mupa"
8557 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_apply_pw_multi_aff(
8558 __isl_take isl_multi_union_pw_aff
*mupa
,
8559 __isl_take isl_pw_multi_aff
*pma
)
8561 isl_space
*space1
, *space2
;
8562 isl_multi_union_pw_aff
*res
;
8566 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
8567 isl_pw_multi_aff_get_space(pma
));
8568 pma
= isl_pw_multi_aff_align_params(pma
,
8569 isl_multi_union_pw_aff_get_space(mupa
));
8573 space1
= isl_multi_union_pw_aff_get_space(mupa
);
8574 space2
= isl_pw_multi_aff_get_domain_space(pma
);
8575 equal
= isl_space_is_equal(space1
, space2
);
8576 isl_space_free(space1
);
8577 isl_space_free(space2
);
8581 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
8582 "spaces don't match", goto error
);
8583 n_out
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
8584 if (isl_pw_multi_aff_dim(pma
, isl_dim_in
) == 0 && n_out
!= 0)
8585 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
8586 "cannot determine domains", goto error
);
8588 space1
= isl_space_range(isl_pw_multi_aff_get_space(pma
));
8589 res
= isl_multi_union_pw_aff_alloc(space1
);
8591 for (i
= 0; i
< n_out
; ++i
) {
8593 isl_union_pw_aff
*upa
;
8595 pa
= isl_pw_multi_aff_get_pw_aff(pma
, i
);
8596 upa
= isl_multi_union_pw_aff_apply_pw_aff(
8597 isl_multi_union_pw_aff_copy(mupa
), pa
);
8598 res
= isl_multi_union_pw_aff_set_union_pw_aff(res
, i
, upa
);
8601 isl_pw_multi_aff_free(pma
);
8602 isl_multi_union_pw_aff_free(mupa
);
8605 isl_multi_union_pw_aff_free(mupa
);
8606 isl_pw_multi_aff_free(pma
);
8610 /* Compute the pullback of "mupa" by the function represented by "upma".
8611 * In other words, plug in "upma" in "mupa". The result contains
8612 * expressions defined over the domain space of "upma".
8614 * Run over all elements of "mupa" and plug in "upma" in each of them.
8616 __isl_give isl_multi_union_pw_aff
*
8617 isl_multi_union_pw_aff_pullback_union_pw_multi_aff(
8618 __isl_take isl_multi_union_pw_aff
*mupa
,
8619 __isl_take isl_union_pw_multi_aff
*upma
)
8623 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
8624 isl_union_pw_multi_aff_get_space(upma
));
8625 upma
= isl_union_pw_multi_aff_align_params(upma
,
8626 isl_multi_union_pw_aff_get_space(mupa
));
8630 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8631 for (i
= 0; i
< n
; ++i
) {
8632 isl_union_pw_aff
*upa
;
8634 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
8635 upa
= isl_union_pw_aff_pullback_union_pw_multi_aff(upa
,
8636 isl_union_pw_multi_aff_copy(upma
));
8637 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
8640 isl_union_pw_multi_aff_free(upma
);
8643 isl_multi_union_pw_aff_free(mupa
);
8644 isl_union_pw_multi_aff_free(upma
);
8648 /* Extract the sequence of elements in "mupa" with domain space "space"
8649 * (ignoring parameters).
8651 * For the elements of "mupa" that are not defined on the specified space,
8652 * the corresponding element in the result is empty.
8654 __isl_give isl_multi_pw_aff
*isl_multi_union_pw_aff_extract_multi_pw_aff(
8655 __isl_keep isl_multi_union_pw_aff
*mupa
, __isl_take isl_space
*space
)
8658 isl_space
*space_mpa
= NULL
;
8659 isl_multi_pw_aff
*mpa
;
8661 if (!mupa
|| !space
)
8664 space_mpa
= isl_multi_union_pw_aff_get_space(mupa
);
8665 if (!isl_space_match(space_mpa
, isl_dim_param
, space
, isl_dim_param
)) {
8666 space
= isl_space_drop_dims(space
, isl_dim_param
,
8667 0, isl_space_dim(space
, isl_dim_param
));
8668 space
= isl_space_align_params(space
,
8669 isl_space_copy(space_mpa
));
8673 space_mpa
= isl_space_map_from_domain_and_range(isl_space_copy(space
),
8675 mpa
= isl_multi_pw_aff_alloc(space_mpa
);
8677 space
= isl_space_from_domain(space
);
8678 space
= isl_space_add_dims(space
, isl_dim_out
, 1);
8679 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8680 for (i
= 0; i
< n
; ++i
) {
8681 isl_union_pw_aff
*upa
;
8684 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
8685 pa
= isl_union_pw_aff_extract_pw_aff(upa
,
8686 isl_space_copy(space
));
8687 mpa
= isl_multi_pw_aff_set_pw_aff(mpa
, i
, pa
);
8688 isl_union_pw_aff_free(upa
);
8691 isl_space_free(space
);
8694 isl_space_free(space_mpa
);
8695 isl_space_free(space
);